1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
|
/*
* Storage - associate pseudos with "storage" that keeps them alive
* between basic blocks. The aim is to be able to turn as much of
* the global storage allocation problem as possible into a local
* per-basic-block one.
*
* Copyright (C) 2004 Linus Torvalds
*/
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "symbol.h"
#include "expression.h"
#include "linearize.h"
#include "storage.h"
ALLOCATOR(storage, "storages");
ALLOCATOR(storage_hash, "storage hash");
#define MAX_STORAGE_HASH 64
static struct storage_hash_list *storage_hash_table[MAX_STORAGE_HASH];
static inline unsigned int storage_hash(struct basic_block *bb, pseudo_t pseudo, enum inout_enum inout)
{
unsigned hash = hashval(bb) + hashval(pseudo) + hashval(inout);
hash += hash / MAX_STORAGE_HASH;
return hash & (MAX_STORAGE_HASH-1);
}
static int hash_list_cmp(const void *_a, const void *_b)
{
const struct storage_hash *a = _a;
const struct storage_hash *b = _b;
if (a->pseudo != b->pseudo)
return a->pseudo < b->pseudo ? -1 : 1;
return 0;
}
static void sort_hash_list(struct storage_hash_list **listp)
{
sort_list((struct ptr_list **)listp, hash_list_cmp);
}
struct storage_hash_list *gather_storage(struct basic_block *bb, enum inout_enum inout)
{
int i;
struct storage_hash *entry, *prev;
struct storage_hash_list *list = NULL;
for (i = 0; i < MAX_STORAGE_HASH; i++) {
struct storage_hash *hash;
FOR_EACH_PTR(storage_hash_table[i], hash) {
if (hash->bb == bb && hash->inout == inout)
add_ptr_list(&list, hash);
} END_FOR_EACH_PTR(hash);
}
sort_hash_list(&list);
prev = NULL;
FOR_EACH_PTR(list, entry) {
if (prev && entry->pseudo == prev->pseudo) {
assert(entry == prev);
DELETE_CURRENT_PTR(entry);
}
prev = entry;
} END_FOR_EACH_PTR(entry);
PACK_PTR_LIST(&list);
return list;
}
static void name_storage(void)
{
int i;
int name = 0;
for (i = 0; i < MAX_STORAGE_HASH; i++) {
struct storage_hash *hash;
FOR_EACH_PTR(storage_hash_table[i], hash) {
struct storage *storage = hash->storage;
if (storage->name)
continue;
storage->name = ++name;
} END_FOR_EACH_PTR(hash);
}
}
struct storage *lookup_storage(struct basic_block *bb, pseudo_t pseudo, enum inout_enum inout)
{
struct storage_hash_list *list = storage_hash_table[storage_hash(bb,pseudo,inout)];
struct storage_hash *hash;
FOR_EACH_PTR(list, hash) {
if (hash->bb == bb && hash->pseudo == pseudo && hash->inout == inout)
return hash->storage;
} END_FOR_EACH_PTR(hash);
return NULL;
}
void add_storage(struct storage *storage, struct basic_block *bb, pseudo_t pseudo, enum inout_enum inout)
{
struct storage_hash_list **listp = storage_hash_table + storage_hash(bb,pseudo,inout);
struct storage_hash *hash = alloc_storage_hash(storage);
hash->bb = bb;
hash->pseudo = pseudo;
hash->inout = inout;
add_ptr_list(listp, hash);
}
static int storage_hash_cmp(const void *_a, const void *_b)
{
const struct storage_hash *a = _a;
const struct storage_hash *b = _b;
struct storage *aa = a->storage;
struct storage *bb = b->storage;
if (a->bb != b->bb)
return a->bb < b->bb ? -1 : 1;
if (a->inout != b->inout)
return a->inout < b->inout ? -1 : 1;
if (aa->type != bb->type)
return aa->type < bb->type ? -1 : 1;
if (aa->regno != bb->regno)
return aa->regno < bb->regno ? -1 : 1;
return 0;
}
static void vrfy_storage(struct storage_hash_list **listp)
{
struct storage_hash *entry, *last;
sort_list((struct ptr_list **)listp, storage_hash_cmp);
last = NULL;
FOR_EACH_PTR(*listp, entry) {
if (last) {
struct storage *a = last->storage;
struct storage *b = entry->storage;
if (a == b)
continue;
if (last->bb == entry->bb
&& last->inout == entry->inout
&& a->type != REG_UDEF
&& a->type == b->type
&& a->regno == b->regno) {
printf("\t BAD: same storage as %s in %p: %s (%s and %s)\n",
last->inout == STOR_IN ? "input" : "output",
last->bb,
show_storage(a),
show_pseudo(last->pseudo),
show_pseudo(entry->pseudo));
}
}
last = entry;
} END_FOR_EACH_PTR(entry);
}
void free_storage(void)
{
int i;
for (i = 0; i < MAX_STORAGE_HASH; i++) {
vrfy_storage(storage_hash_table + i);
free_ptr_list(storage_hash_table + i);
}
}
const char *show_storage(struct storage *s)
{
static char buffer[1024];
if (!s)
return "none";
switch (s->type) {
case REG_REG:
sprintf(buffer, "reg%d (%d)", s->regno, s->name);
break;
case REG_STACK:
sprintf(buffer, "%d(SP) (%d)", s->offset, s->name);
break;
case REG_ARG:
sprintf(buffer, "ARG%d (%d)", s->regno, s->name);
break;
default:
sprintf(buffer, "%d:%d (%d)", s->type, s->regno, s->name);
break;
}
return buffer;
}
/*
* Combine two storage allocations into one.
*
* We just randomly pick one over the other, and replace
* the other uses.
*/
static struct storage * combine_storage(struct storage *src, struct storage *dst)
{
struct storage **usep;
/* Remove uses of "src_storage", replace with "dst" */
FOR_EACH_PTR(src->users, usep) {
assert(*usep == src);
*usep = dst;
add_ptr_list(&dst->users, usep);
} END_FOR_EACH_PTR(usep);
/* Mark it unused */
src->type = REG_BAD;
src->users = NULL;
return dst;
}
static void set_up_bb_storage(struct basic_block *bb)
{
struct basic_block *child;
FOR_EACH_PTR(bb->children, child) {
pseudo_t pseudo;
FOR_EACH_PTR(child->needs, pseudo) {
struct storage *child_in, *parent_out;
parent_out = lookup_storage(bb, pseudo, STOR_OUT);
child_in = lookup_storage(child, pseudo, STOR_IN);
if (parent_out) {
if (!child_in) {
add_storage(parent_out, child, pseudo, STOR_IN);
continue;
}
if (parent_out == child_in)
continue;
combine_storage(parent_out, child_in);
continue;
}
if (child_in) {
add_storage(child_in, bb, pseudo, STOR_OUT);
continue;
}
parent_out = alloc_storage();
add_storage(parent_out, bb, pseudo, STOR_OUT);
add_storage(parent_out, child, pseudo, STOR_IN);
} END_FOR_EACH_PTR(pseudo);
} END_FOR_EACH_PTR(child);
}
static void set_up_argument_storage(struct entrypoint *ep, struct basic_block *bb)
{
pseudo_t arg;
FOR_EACH_PTR(bb->needs, arg) {
struct storage *storage = alloc_storage();
/* FIXME! Totally made-up argument passing conventions */
if (arg->type == PSEUDO_ARG) {
storage->type = REG_ARG;
storage->regno = arg->nr;
}
add_storage(storage, bb, arg, STOR_IN);
} END_FOR_EACH_PTR(arg);
}
/*
* One phi-source may feed multiple phi nodes. If so, combine
* the storage output for this bb into one entry to reduce
* storage pressure.
*/
static void combine_phi_storage(struct basic_block *bb)
{
struct instruction *insn;
FOR_EACH_PTR(bb->insns, insn) {
struct instruction *phi;
struct storage *last;
if (!insn->bb || insn->opcode != OP_PHISOURCE)
continue;
last = NULL;
FOR_EACH_PTR(insn->phi_users, phi) {
struct storage *storage = lookup_storage(bb, phi->target, STOR_OUT);
if (!storage) {
DELETE_CURRENT_PTR(phi);
continue;
}
if (last && storage != last)
storage = combine_storage(storage, last);
last = storage;
} END_FOR_EACH_PTR(phi);
PACK_PTR_LIST(&insn->phi_users);
} END_FOR_EACH_PTR(insn);
}
void set_up_storage(struct entrypoint *ep)
{
struct basic_block *bb;
/* First set up storage for the incoming arguments */
set_up_argument_storage(ep, ep->entry->bb);
/* Then do a list of all the inter-bb storage */
FOR_EACH_PTR(ep->bbs, bb) {
set_up_bb_storage(bb);
combine_phi_storage(bb);
} END_FOR_EACH_PTR(bb);
name_storage();
}
|