5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
6 * this software and associated documentation files (the "Software"), to deal in
7 * the Software without restriction, including without limitation the rights to
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
9 * of the Software, and to permit persons to whom the Software is furnished to do
10 * so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in all
13 * copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 /***********************************************************************
32 ir_builder* ir_builder_new(const char *modulename)
36 self = (ir_builder*)mem_a(sizeof(*self));
37 MEM_VECTOR_INIT(self, functions);
38 MEM_VECTOR_INIT(self, globals);
40 if (!ir_builder_set_name(self, modulename)) {
45 /* globals which always exist */
47 /* for now we give it a vector size */
48 ir_builder_create_global(self, "OFS_RETURN", TYPE_VARIANT);
53 MEM_VEC_FUNCTIONS(ir_builder, ir_value*, globals)
54 MEM_VEC_FUNCTIONS(ir_builder, ir_function*, functions)
56 void ir_builder_delete(ir_builder* self)
59 mem_d((void*)self->name);
60 for (i = 0; i != self->functions_count; ++i) {
61 ir_function_delete(self->functions[i]);
63 MEM_VECTOR_CLEAR(self, functions);
64 for (i = 0; i != self->globals_count; ++i) {
65 ir_value_delete(self->globals[i]);
67 MEM_VECTOR_CLEAR(self, globals);
71 bool ir_builder_set_name(ir_builder *self, const char *name)
74 mem_d((void*)self->name);
75 self->name = util_strdup(name);
79 ir_function* ir_builder_get_function(ir_builder *self, const char *name)
82 for (i = 0; i < self->functions_count; ++i) {
83 if (!strcmp(name, self->functions[i]->name))
84 return self->functions[i];
89 ir_function* ir_builder_create_function(ir_builder *self, const char *name)
91 ir_function *fn = ir_builder_get_function(self, name);
96 fn = ir_function_new(self);
97 if (!ir_function_set_name(fn, name) ||
98 !ir_builder_functions_add(self, fn) )
100 ir_function_delete(fn);
106 ir_value* ir_builder_get_global(ir_builder *self, const char *name)
109 for (i = 0; i < self->globals_count; ++i) {
110 if (!strcmp(self->globals[i]->name, name))
111 return self->globals[i];
116 ir_value* ir_builder_create_global(ir_builder *self, const char *name, int vtype)
118 ir_value *ve = ir_builder_get_global(self, name);
123 ve = ir_value_var(name, store_global, vtype);
124 if (!ir_builder_globals_add(self, ve)) {
131 /***********************************************************************
135 bool ir_function_naive_phi(ir_function*);
136 void ir_function_enumerate(ir_function*);
137 bool ir_function_calculate_liferanges(ir_function*);
139 ir_function* ir_function_new(ir_builder* owner)
142 self = (ir_function*)mem_a(sizeof(*self));
144 if (!ir_function_set_name(self, "<@unnamed>")) {
149 self->context.file = "<@no context>";
150 self->context.line = 0;
151 self->retype = TYPE_VOID;
152 MEM_VECTOR_INIT(self, params);
153 MEM_VECTOR_INIT(self, blocks);
154 MEM_VECTOR_INIT(self, values);
155 MEM_VECTOR_INIT(self, locals);
160 MEM_VEC_FUNCTIONS(ir_function, ir_value*, values)
161 MEM_VEC_FUNCTIONS(ir_function, ir_block*, blocks)
162 MEM_VEC_FUNCTIONS(ir_function, ir_value*, locals)
164 bool ir_function_set_name(ir_function *self, const char *name)
167 mem_d((void*)self->name);
168 self->name = util_strdup(name);
172 void ir_function_delete(ir_function *self)
175 mem_d((void*)self->name);
177 for (i = 0; i != self->blocks_count; ++i)
178 ir_block_delete(self->blocks[i]);
179 MEM_VECTOR_CLEAR(self, blocks);
181 MEM_VECTOR_CLEAR(self, params);
183 for (i = 0; i != self->values_count; ++i)
184 ir_value_delete(self->values[i]);
185 MEM_VECTOR_CLEAR(self, values);
187 for (i = 0; i != self->locals_count; ++i)
188 ir_value_delete(self->locals[i]);
189 MEM_VECTOR_CLEAR(self, locals);
194 bool GMQCC_WARN ir_function_collect_value(ir_function *self, ir_value *v)
196 return ir_function_values_add(self, v);
199 ir_block* ir_function_create_block(ir_function *self, const char *label)
201 ir_block* bn = ir_block_new(self, label);
202 memcpy(&bn->context, &self->context, sizeof(self->context));
203 if (!ir_function_blocks_add(self, bn)) {
210 bool ir_function_finalize(ir_function *self)
212 if (!ir_function_naive_phi(self))
215 ir_function_enumerate(self);
217 if (!ir_function_calculate_liferanges(self))
222 ir_value* ir_function_get_local(ir_function *self, const char *name)
225 for (i = 0; i < self->locals_count; ++i) {
226 if (!strcmp(self->locals[i]->name, name))
227 return self->locals[i];
232 ir_value* ir_function_create_local(ir_function *self, const char *name, int vtype)
234 ir_value *ve = ir_function_get_local(self, name);
239 ve = ir_value_var(name, store_local, vtype);
240 if (!ir_function_locals_add(self, ve)) {
247 /***********************************************************************
251 ir_block* ir_block_new(ir_function* owner, const char *name)
254 self = (ir_block*)mem_a(sizeof(*self));
256 if (!ir_block_set_label(self, name)) {
261 self->context.file = "<@no context>";
262 self->context.line = 0;
264 MEM_VECTOR_INIT(self, instr);
265 MEM_VECTOR_INIT(self, entries);
266 MEM_VECTOR_INIT(self, exits);
269 self->is_return = false;
271 MEM_VECTOR_INIT(self, living);
274 MEM_VEC_FUNCTIONS(ir_block, ir_instr*, instr)
275 MEM_VEC_FUNCTIONS_ALL(ir_block, ir_block*, entries)
276 MEM_VEC_FUNCTIONS_ALL(ir_block, ir_block*, exits)
277 MEM_VEC_FUNCTIONS_ALL(ir_block, ir_value*, living)
279 void ir_block_delete(ir_block* self)
283 for (i = 0; i != self->instr_count; ++i)
284 ir_instr_delete(self->instr[i]);
285 MEM_VECTOR_CLEAR(self, instr);
286 MEM_VECTOR_CLEAR(self, entries);
287 MEM_VECTOR_CLEAR(self, exits);
288 MEM_VECTOR_CLEAR(self, living);
292 bool ir_block_set_label(ir_block *self, const char *name)
295 mem_d((void*)self->label);
296 self->label = util_strdup(name);
297 return !!self->label;
300 /***********************************************************************
304 ir_instr* ir_instr_new(ir_block* owner, int op)
307 self = (ir_instr*)mem_a(sizeof(*self));
309 self->context.file = "<@no context>";
310 self->context.line = 0;
312 self->_ops[0] = NULL;
313 self->_ops[1] = NULL;
314 self->_ops[2] = NULL;
315 self->bops[0] = NULL;
316 self->bops[1] = NULL;
317 MEM_VECTOR_INIT(self, phi);
322 MEM_VEC_FUNCTIONS(ir_instr, ir_phi_entry_t, phi)
324 void ir_instr_delete(ir_instr *self)
327 /* The following calls can only delete from
328 * vectors, we still want to delete this instruction
329 * so ignore the return value. Since with the warn_unused_result attribute
330 * gcc doesn't care about an explicit: (void)foo(); to ignore the result,
331 * I have to improvise here and use if(foo());
333 for (i = 0; i < self->phi_count; ++i) {
335 if (ir_value_writes_find(self->phi[i].value, self, &idx))
336 if (ir_value_writes_remove(self->phi[i].value, idx)) GMQCC_SUPRESS_EMPTY_BODY;
337 if (ir_value_reads_find(self->phi[i].value, self, &idx))
338 if (ir_value_reads_remove (self->phi[i].value, idx)) GMQCC_SUPRESS_EMPTY_BODY;
340 MEM_VECTOR_CLEAR(self, phi);
341 if (ir_instr_op(self, 0, NULL, false)) GMQCC_SUPRESS_EMPTY_BODY;
342 if (ir_instr_op(self, 1, NULL, false)) GMQCC_SUPRESS_EMPTY_BODY;
343 if (ir_instr_op(self, 2, NULL, false)) GMQCC_SUPRESS_EMPTY_BODY;
347 bool ir_instr_op(ir_instr *self, int op, ir_value *v, bool writing)
349 if (self->_ops[op]) {
351 if (writing && ir_value_writes_find(self->_ops[op], self, &idx))
353 if (!ir_value_writes_remove(self->_ops[op], idx))
356 else if (ir_value_reads_find(self->_ops[op], self, &idx))
358 if (!ir_value_reads_remove(self->_ops[op], idx))
364 if (!ir_value_writes_add(v, self))
367 if (!ir_value_reads_add(v, self))
375 /***********************************************************************
379 ir_value* ir_value_var(const char *name, int storetype, int vtype)
382 self = (ir_value*)mem_a(sizeof(*self));
384 self->store = storetype;
385 MEM_VECTOR_INIT(self, reads);
386 MEM_VECTOR_INIT(self, writes);
387 self->isconst = false;
388 self->context.file = "<@no context>";
389 self->context.line = 0;
391 ir_value_set_name(self, name);
393 MEM_VECTOR_INIT(self, life);
396 MEM_VEC_FUNCTIONS(ir_value, ir_life_entry_t, life)
397 MEM_VEC_FUNCTIONS_ALL(ir_value, ir_instr*, reads)
398 MEM_VEC_FUNCTIONS_ALL(ir_value, ir_instr*, writes)
400 ir_value* ir_value_out(ir_function *owner, const char *name, int storetype, int vtype)
402 ir_value *v = ir_value_var(name, storetype, vtype);
405 if (!ir_function_collect_value(owner, v))
413 void ir_value_delete(ir_value* self)
415 mem_d((void*)self->name);
418 if (self->vtype == TYPE_STRING)
419 mem_d((void*)self->constval.vstring);
421 MEM_VECTOR_CLEAR(self, reads);
422 MEM_VECTOR_CLEAR(self, writes);
423 MEM_VECTOR_CLEAR(self, life);
427 void ir_value_set_name(ir_value *self, const char *name)
430 mem_d((void*)self->name);
431 self->name = util_strdup(name);
434 bool ir_value_set_float(ir_value *self, float f)
436 if (self->vtype != TYPE_FLOAT)
438 self->constval.vfloat = f;
439 self->isconst = true;
443 bool ir_value_set_vector(ir_value *self, vector v)
445 if (self->vtype != TYPE_VECTOR)
447 self->constval.vvec = v;
448 self->isconst = true;
452 bool ir_value_set_string(ir_value *self, const char *str)
454 if (self->vtype != TYPE_STRING)
456 self->constval.vstring = util_strdup(str);
457 self->isconst = true;
462 bool ir_value_set_int(ir_value *self, int i)
464 if (self->vtype != TYPE_INTEGER)
466 self->constval.vint = i;
467 self->isconst = true;
472 bool ir_value_lives(ir_value *self, size_t at)
475 for (i = 0; i < self->life_count; ++i)
477 ir_life_entry_t *life = &self->life[i];
478 if (life->start <= at && at <= life->end)
480 if (life->start > at) /* since it's ordered */
486 bool ir_value_life_insert(ir_value *self, size_t idx, ir_life_entry_t e)
489 if (!ir_value_life_add(self, e)) /* naive... */
491 for (k = self->life_count-1; k > idx; --k)
492 self->life[k] = self->life[k-1];
497 bool ir_value_life_merge(ir_value *self, size_t s)
500 ir_life_entry_t *life = NULL;
501 ir_life_entry_t *before = NULL;
502 ir_life_entry_t new_entry;
504 /* Find the first range >= s */
505 for (i = 0; i < self->life_count; ++i)
508 life = &self->life[i];
512 /* nothing found? append */
513 if (i == self->life_count) {
515 if (life && life->end+1 == s)
517 /* previous life range can be merged in */
521 if (life && life->end >= s)
524 if (!ir_value_life_add(self, e))
525 return false; /* failing */
531 if (before->end + 1 == s &&
532 life->start - 1 == s)
535 before->end = life->end;
536 if (!ir_value_life_remove(self, i))
537 return false; /* failing */
540 if (before->end + 1 == s)
546 /* already contained */
547 if (before->end >= s)
551 if (life->start - 1 == s)
556 /* insert a new entry */
557 new_entry.start = new_entry.end = s;
558 return ir_value_life_insert(self, i, new_entry);
561 bool ir_values_overlap(ir_value *a, ir_value *b)
563 /* For any life entry in A see if it overlaps with
564 * any life entry in B.
565 * Note that the life entries are orderes, so we can make a
566 * more efficient algorithm there than naively translating the
570 ir_life_entry_t *la, *lb, *enda, *endb;
572 /* first of all, if either has no life range, they cannot clash */
573 if (!a->life_count || !b->life_count)
578 enda = la + a->life_count;
579 endb = lb + b->life_count;
582 /* check if the entries overlap, for that,
583 * both must start before the other one ends.
585 if (la->start <= lb->end &&
586 lb->start <= la->end)
591 /* entries are ordered
592 * one entry is earlier than the other
593 * that earlier entry will be moved forward
595 if (la->end < lb->end)
597 /* order: A B, move A forward
598 * check if we hit the end with A
603 else if (lb->end < la->end)
605 /* order: B A, move B forward
606 * check if we hit the end with B
615 /***********************************************************************
619 bool ir_block_create_store_op(ir_block *self, int op, ir_value *target, ir_value *what)
621 if (target->store == store_value) {
622 fprintf(stderr, "cannot store to an SSA value\n");
623 fprintf(stderr, "trying to store: %s <- %s\n", target->name, what->name);
626 ir_instr *in = ir_instr_new(self, op);
629 if (!ir_instr_op(in, 0, target, true) ||
630 !ir_instr_op(in, 1, what, false) ||
631 !ir_block_instr_add(self, in) )
639 bool ir_block_create_store(ir_block *self, ir_value *target, ir_value *what)
643 if (target->vtype == TYPE_VARIANT)
646 vtype = target->vtype;
651 if (what->vtype == TYPE_INTEGER)
652 op = INSTR_CONV_ITOF;
661 op = INSTR_STORE_ENT;
667 op = INSTR_STORE_FLD;
671 if (what->vtype == TYPE_INTEGER)
672 op = INSTR_CONV_FTOI;
681 op = INSTR_STORE_ENT;
688 return ir_block_create_store_op(self, op, target, what);
691 bool ir_block_create_storep(ir_block *self, ir_value *target, ir_value *what)
696 if (target->vtype != TYPE_POINTER)
699 /* storing using pointer - target is a pointer, type must be
700 * inferred from source
712 op = INSTR_STOREP_ENT;
718 op = INSTR_STOREP_FLD;
729 op = INSTR_STOREP_ENT;
736 return ir_block_create_store_op(self, op, target, what);
739 bool ir_block_create_return(ir_block *self, ir_value *v)
743 fprintf(stderr, "block already ended (%s)\n", self->label);
747 self->is_return = true;
748 in = ir_instr_new(self, INSTR_RETURN);
752 if (!ir_instr_op(in, 0, v, false) ||
753 !ir_block_instr_add(self, in) )
760 bool ir_block_create_if(ir_block *self, ir_value *v,
761 ir_block *ontrue, ir_block *onfalse)
765 fprintf(stderr, "block already ended (%s)\n", self->label);
769 /*in = ir_instr_new(self, (v->vtype == TYPE_STRING ? INSTR_IF_S : INSTR_IF_F));*/
770 in = ir_instr_new(self, VINSTR_COND);
774 if (!ir_instr_op(in, 0, v, false)) {
779 in->bops[0] = ontrue;
780 in->bops[1] = onfalse;
782 if (!ir_block_instr_add(self, in))
785 if (!ir_block_exits_add(self, ontrue) ||
786 !ir_block_exits_add(self, onfalse) ||
787 !ir_block_entries_add(ontrue, self) ||
788 !ir_block_entries_add(onfalse, self) )
795 bool ir_block_create_jump(ir_block *self, ir_block *to)
799 fprintf(stderr, "block already ended (%s)\n", self->label);
803 in = ir_instr_new(self, VINSTR_JUMP);
808 if (!ir_block_instr_add(self, in))
811 if (!ir_block_exits_add(self, to) ||
812 !ir_block_entries_add(to, self) )
819 bool ir_block_create_goto(ir_block *self, ir_block *to)
823 fprintf(stderr, "block already ended (%s)\n", self->label);
827 in = ir_instr_new(self, INSTR_GOTO);
832 if (!ir_block_instr_add(self, in))
835 if (!ir_block_exits_add(self, to) ||
836 !ir_block_entries_add(to, self) )
843 ir_instr* ir_block_create_phi(ir_block *self, const char *label, int ot)
847 in = ir_instr_new(self, VINSTR_PHI);
850 out = ir_value_out(self->owner, label, store_value, ot);
855 if (!ir_instr_op(in, 0, out, true)) {
857 ir_value_delete(out);
860 if (!ir_block_instr_add(self, in)) {
862 ir_value_delete(out);
868 ir_value* ir_phi_value(ir_instr *self)
870 return self->_ops[0];
873 bool ir_phi_add(ir_instr* self, ir_block *b, ir_value *v)
877 if (!ir_block_entries_find(self->owner, b, NULL)) {
878 /* Must not be possible to cause this, otherwise the AST
879 * is doing something wrong.
881 fprintf(stderr, "Invalid entry block for PHI\n");
887 if (!ir_value_reads_add(v, self))
889 return ir_instr_phi_add(self, pe);
892 /* binary op related code */
894 ir_value* ir_block_create_binop(ir_block *self,
895 const char *label, int opcode,
896 ir_value *left, ir_value *right)
918 case INSTR_SUB_S: /* -- offset of string as float */
925 case INSTR_BITAND_FI:
926 case INSTR_BITAND_IF:
967 /* boolean operations result in floats */
968 if (opcode >= INSTR_EQ_F && opcode <= INSTR_GT)
970 else if (opcode >= INSTR_LE && opcode <= INSTR_GT)
973 else if (opcode >= INSTR_LE_I && opcode <= INSTR_EQ_FI)
978 if (ot == TYPE_VOID) {
979 /* The AST or parser were supposed to check this! */
983 return ir_block_create_general_instr(self, label, opcode, left, right, ot);
986 ir_value* ir_block_create_general_instr(ir_block *self, const char *label,
987 int op, ir_value *a, ir_value *b, int outype)
992 out = ir_value_out(self->owner, label, store_value, outype);
996 instr = ir_instr_new(self, op);
998 ir_value_delete(out);
1002 if (!ir_instr_op(instr, 0, out, true) ||
1003 !ir_instr_op(instr, 1, a, false) ||
1004 !ir_instr_op(instr, 2, b, false) )
1009 if (!ir_block_instr_add(self, instr))
1014 ir_instr_delete(instr);
1015 ir_value_delete(out);
1019 ir_value* ir_block_create_fieldaddress(ir_block *self, const char *label, ir_value *ent, ir_value *field)
1021 /* Support for various pointer types todo if so desired */
1022 if (ent->vtype != TYPE_ENTITY)
1025 if (field->vtype != TYPE_FIELD)
1028 return ir_block_create_general_instr(self, label, INSTR_ADDRESS, ent, field, TYPE_POINTER);
1031 ir_value* ir_block_create_load_from_ent(ir_block *self, const char *label, ir_value *ent, ir_value *field, int outype)
1034 if (ent->vtype != TYPE_ENTITY)
1037 /* at some point we could redirect for TYPE_POINTER... but that could lead to carelessness */
1038 if (field->vtype != TYPE_FIELD)
1043 case TYPE_FLOAT: op = INSTR_LOAD_F; break;
1044 case TYPE_VECTOR: op = INSTR_LOAD_V; break;
1045 case TYPE_STRING: op = INSTR_LOAD_S; break;
1046 case TYPE_FIELD: op = INSTR_LOAD_FLD; break;
1047 case TYPE_ENTITY: op = INSTR_LOAD_ENT; break;
1049 case TYPE_POINTER: op = INSTR_LOAD_I; break;
1050 case TYPE_INTEGER: op = INSTR_LOAD_I; break;
1056 return ir_block_create_general_instr(self, label, op, ent, field, outype);
1059 ir_value* ir_block_create_add(ir_block *self,
1061 ir_value *left, ir_value *right)
1064 int l = left->vtype;
1065 int r = right->vtype;
1084 if ( (l == TYPE_FLOAT && r == TYPE_INTEGER) )
1086 else if ( (l == TYPE_INTEGER && r == TYPE_FLOAT) )
1092 return ir_block_create_binop(self, label, op, left, right);
1095 ir_value* ir_block_create_sub(ir_block *self,
1097 ir_value *left, ir_value *right)
1100 int l = left->vtype;
1101 int r = right->vtype;
1121 if ( (l == TYPE_FLOAT && r == TYPE_INTEGER) )
1123 else if ( (l == TYPE_INTEGER && r == TYPE_FLOAT) )
1129 return ir_block_create_binop(self, label, op, left, right);
1132 ir_value* ir_block_create_mul(ir_block *self,
1134 ir_value *left, ir_value *right)
1137 int l = left->vtype;
1138 int r = right->vtype;
1157 if ( (l == TYPE_VECTOR && r == TYPE_FLOAT) )
1159 else if ( (l == TYPE_FLOAT && r == TYPE_VECTOR) )
1162 else if ( (l == TYPE_VECTOR && r == TYPE_INTEGER) )
1164 else if ( (l == TYPE_INTEGER && r == TYPE_VECTOR) )
1166 else if ( (l == TYPE_FLOAT && r == TYPE_INTEGER) )
1168 else if ( (l == TYPE_INTEGER && r == TYPE_FLOAT) )
1174 return ir_block_create_binop(self, label, op, left, right);
1177 ir_value* ir_block_create_div(ir_block *self,
1179 ir_value *left, ir_value *right)
1182 int l = left->vtype;
1183 int r = right->vtype;
1200 if ( (l == TYPE_VECTOR && r == TYPE_FLOAT) )
1202 else if ( (l == TYPE_FLOAT && r == TYPE_INTEGER) )
1204 else if ( (l == TYPE_INTEGER && r == TYPE_FLOAT) )
1210 return ir_block_create_binop(self, label, op, left, right);
1213 /* PHI resolving breaks the SSA, and must thus be the last
1214 * step before life-range calculation.
1217 static bool ir_block_naive_phi(ir_block *self);
1218 bool ir_function_naive_phi(ir_function *self)
1222 for (i = 0; i < self->blocks_count; ++i)
1224 if (!ir_block_naive_phi(self->blocks[i]))
1230 static bool ir_naive_phi_emit_store(ir_block *block, size_t iid, ir_value *old, ir_value *what)
1235 /* create a store */
1236 if (!ir_block_create_store(block, old, what))
1239 /* we now move it up */
1240 instr = block->instr[block->instr_count-1];
1241 for (i = block->instr_count; i > iid; --i)
1242 block->instr[i] = block->instr[i-1];
1243 block->instr[i] = instr;
1248 static bool ir_block_naive_phi(ir_block *self)
1251 /* FIXME: optionally, create_phi can add the phis
1252 * to a list so we don't need to loop through blocks
1253 * - anyway: "don't optimize YET"
1255 for (i = 0; i < self->instr_count; ++i)
1257 ir_instr *instr = self->instr[i];
1258 if (instr->opcode != VINSTR_PHI)
1261 if (!ir_block_instr_remove(self, i))
1263 --i; /* NOTE: i+1 below */
1265 for (p = 0; p < instr->phi_count; ++p)
1267 ir_value *v = instr->phi[p].value;
1268 for (w = 0; w < v->writes_count; ++w) {
1271 if (!v->writes[w]->_ops[0])
1274 /* When the write was to a global, we have to emit a mov */
1275 old = v->writes[w]->_ops[0];
1277 /* The original instruction now writes to the PHI target local */
1278 if (v->writes[w]->_ops[0] == v)
1279 v->writes[w]->_ops[0] = instr->_ops[0];
1281 if (old->store != store_value && old->store != store_local)
1283 /* If it originally wrote to a global we need to store the value
1286 if (!ir_naive_phi_emit_store(self, i+1, old, v))
1288 if (i+1 < self->instr_count)
1289 instr = self->instr[i+1];
1292 /* In case I forget and access instr later, it'll be NULL
1293 * when it's a problem, to make sure we crash, rather than accessing
1299 /* If it didn't, we can replace all reads by the phi target now. */
1301 for (r = 0; r < old->reads_count; ++r)
1304 ir_instr *ri = old->reads[r];
1305 for (op = 0; op < ri->phi_count; ++op) {
1306 if (ri->phi[op].value == old)
1307 ri->phi[op].value = v;
1309 for (op = 0; op < 3; ++op) {
1310 if (ri->_ops[op] == old)
1317 ir_instr_delete(instr);
1322 /***********************************************************************
1323 *IR Temp allocation code
1324 * Propagating value life ranges by walking through the function backwards
1325 * until no more changes are made.
1326 * In theory this should happen once more than once for every nested loop
1328 * Though this implementation might run an additional time for if nests.
1337 MEM_VEC_FUNCTIONS_ALL(new_reads_t, ir_value*, v)
1339 /* Enumerate instructions used by value's life-ranges
1341 static void ir_block_enumerate(ir_block *self, size_t *_eid)
1345 for (i = 0; i < self->instr_count; ++i)
1347 self->instr[i]->eid = eid++;
1352 /* Enumerate blocks and instructions.
1353 * The block-enumeration is unordered!
1354 * We do not really use the block enumreation, however
1355 * the instruction enumeration is important for life-ranges.
1357 void ir_function_enumerate(ir_function *self)
1360 size_t instruction_id = 0;
1361 for (i = 0; i < self->blocks_count; ++i)
1363 self->blocks[i]->eid = i;
1364 self->blocks[i]->run_id = 0;
1365 ir_block_enumerate(self->blocks[i], &instruction_id);
1369 static bool ir_block_life_propagate(ir_block *b, ir_block *prev, bool *changed);
1370 bool ir_function_calculate_liferanges(ir_function *self)
1378 for (i = 0; i != self->blocks_count; ++i)
1380 if (self->blocks[i]->is_return)
1382 if (!ir_block_life_propagate(self->blocks[i], NULL, &changed))
1390 /* Get information about which operand
1391 * is read from, or written to.
1393 static void ir_op_read_write(int op, size_t *read, size_t *write)
1420 static bool ir_block_living_add_instr(ir_block *self, size_t eid)
1423 bool changed = false;
1425 for (i = 0; i != self->living_count; ++i)
1427 tempbool = ir_value_life_merge(self->living[i], eid);
1430 fprintf(stderr, "block_living_add_instr() value instruction added %s: %i\n", self->living[i]->_name, (int)eid);
1432 changed = changed || tempbool;
1437 static bool ir_block_life_prop_previous(ir_block* self, ir_block *prev, bool *changed)
1440 /* values which have been read in a previous iteration are now
1441 * in the "living" array even if the previous block doesn't use them.
1442 * So we have to remove whatever does not exist in the previous block.
1443 * They will be re-added on-read, but the liferange merge won't cause
1446 for (i = 0; i < self->living_count; ++i)
1448 if (!ir_block_living_find(prev, self->living[i], NULL)) {
1449 if (!ir_block_living_remove(self, i))
1455 /* Whatever the previous block still has in its living set
1456 * must now be added to ours as well.
1458 for (i = 0; i < prev->living_count; ++i)
1460 if (ir_block_living_find(self, prev->living[i], NULL))
1462 if (!ir_block_living_add(self, prev->living[i]))
1465 printf("%s got from prev: %s\n", self->label, prev->living[i]->_name);
1471 static bool ir_block_life_propagate(ir_block *self, ir_block *prev, bool *changed)
1477 /* bitmasks which operands are read from or written to */
1479 new_reads_t new_reads;
1480 char dbg_ind[16] = { '#', '0' };
1483 MEM_VECTOR_INIT(&new_reads, v);
1487 if (!ir_block_life_prop_previous(self, prev, changed))
1491 i = self->instr_count;
1494 instr = self->instr[i];
1496 /* PHI operands are always read operands */
1497 for (p = 0; p < instr->phi_count; ++p)
1499 value = instr->phi[p].value;
1500 /* used this before new_reads - puts the last read into the life range as well
1501 if (!ir_block_living_find(self, value, NULL))
1502 ir_block_living_add(self, value);
1504 /* fprintf(stderr, "read: %s\n", value->_name); */
1505 if (!new_reads_t_v_find(&new_reads, value, NULL))
1507 if (!new_reads_t_v_add(&new_reads, value))
1512 /* See which operands are read and write operands */
1513 ir_op_read_write(instr->opcode, &read, &write);
1515 /* Go through the 3 main operands */
1516 for (o = 0; o < 3; ++o)
1518 if (!instr->_ops[o]) /* no such operand */
1521 value = instr->_ops[o];
1523 /* We only care about locals */
1524 if (value->store != store_value &&
1525 value->store != store_local)
1531 /* used this before new_reads - puts the last read into the life range as well
1532 if (!ir_block_living_find(self, value, NULL))
1533 ir_block_living_add(self, value);
1535 /* fprintf(stderr, "read: %s\n", value->_name); */
1536 if (!new_reads_t_v_find(&new_reads, value, NULL))
1538 if (!new_reads_t_v_add(&new_reads, value))
1543 /* write operands */
1544 /* When we write to a local, we consider it "dead" for the
1545 * remaining upper part of the function, since in SSA a value
1546 * can only be written once (== created)
1550 size_t idx, readidx;
1551 bool in_living = ir_block_living_find(self, value, &idx);
1552 bool in_reads = new_reads_t_v_find(&new_reads, value, &readidx);
1553 if (!in_living && !in_reads)
1555 /* If the value isn't alive it hasn't been read before... */
1556 /* TODO: See if the warning can be emitted during parsing or AST processing
1557 * otherwise have warning printed here.
1558 * IF printing a warning here: include filecontext_t,
1559 * and make sure it's only printed once
1560 * since this function is run multiple times.
1562 /* For now: debug info: */
1563 fprintf(stderr, "Value only written %s\n", value->name);
1564 tempbool = ir_value_life_merge(value, instr->eid);
1565 *changed = *changed || tempbool;
1567 ir_instr_dump(instr, dbg_ind, printf);
1571 /* since 'living' won't contain it
1572 * anymore, merge the value, since
1575 tempbool = ir_value_life_merge(value, instr->eid);
1578 fprintf(stderr, "value added id %s %i\n", value->name, (int)instr->eid);
1580 *changed = *changed || tempbool;
1582 if (!ir_block_living_remove(self, idx))
1586 if (!new_reads_t_v_remove(&new_reads, readidx))
1593 tempbool = ir_block_living_add_instr(self, instr->eid);
1594 /*fprintf(stderr, "living added values\n");*/
1595 *changed = *changed || tempbool;
1598 for (rd = 0; rd < new_reads.v_count; ++rd)
1600 if (!ir_block_living_find(self, new_reads.v[rd], NULL)) {
1601 if (!ir_block_living_add(self, new_reads.v[rd]))
1604 if (!i && !self->entries_count) {
1606 *changed = *changed || ir_value_life_merge(new_reads.v[rd], instr->eid);
1609 MEM_VECTOR_CLEAR(&new_reads, v);
1612 if (self->run_id == self->owner->run_id)
1615 self->run_id = self->owner->run_id;
1617 for (i = 0; i < self->entries_count; ++i)
1619 ir_block *entry = self->entries[i];
1620 ir_block_life_propagate(entry, self, changed);
1625 MEM_VECTOR_CLEAR(&new_reads, v);
1629 /***********************************************************************
1630 *IR DEBUG Dump functions...
1633 #define IND_BUFSZ 1024
1635 const char *qc_opname(int op)
1637 if (op < 0) return "<INVALID>";
1638 if (op < ( sizeof(asm_instr) / sizeof(asm_instr[0]) ))
1639 return asm_instr[op].m;
1641 case VINSTR_PHI: return "PHI";
1642 case VINSTR_JUMP: return "JUMP";
1643 case VINSTR_COND: return "COND";
1644 default: return "<UNK>";
1648 void ir_builder_dump(ir_builder *b, int (*oprintf)(const char*, ...))
1651 char indent[IND_BUFSZ];
1655 oprintf("module %s\n", b->name);
1656 for (i = 0; i < b->globals_count; ++i)
1659 if (b->globals[i]->isconst)
1660 oprintf("%s = ", b->globals[i]->name);
1661 ir_value_dump(b->globals[i], oprintf);
1664 for (i = 0; i < b->functions_count; ++i)
1665 ir_function_dump(b->functions[i], indent, oprintf);
1666 oprintf("endmodule %s\n", b->name);
1669 void ir_function_dump(ir_function *f, char *ind,
1670 int (*oprintf)(const char*, ...))
1673 oprintf("%sfunction %s\n", ind, f->name);
1674 strncat(ind, "\t", IND_BUFSZ);
1675 if (f->locals_count)
1677 oprintf("%s%i locals:\n", ind, (int)f->locals_count);
1678 for (i = 0; i < f->locals_count; ++i) {
1679 oprintf("%s\t", ind);
1680 ir_value_dump(f->locals[i], oprintf);
1684 if (f->blocks_count)
1686 oprintf("%slife passes (check): %i\n", ind, (int)f->run_id);
1687 for (i = 0; i < f->blocks_count; ++i) {
1688 if (f->blocks[i]->run_id != f->run_id) {
1689 oprintf("%slife pass check fail! %i != %i\n", ind, (int)f->blocks[i]->run_id, (int)f->run_id);
1691 ir_block_dump(f->blocks[i], ind, oprintf);
1695 ind[strlen(ind)-1] = 0;
1696 oprintf("%sendfunction %s\n", ind, f->name);
1699 void ir_block_dump(ir_block* b, char *ind,
1700 int (*oprintf)(const char*, ...))
1703 oprintf("%s:%s\n", ind, b->label);
1704 strncat(ind, "\t", IND_BUFSZ);
1706 for (i = 0; i < b->instr_count; ++i)
1707 ir_instr_dump(b->instr[i], ind, oprintf);
1708 ind[strlen(ind)-1] = 0;
1711 void dump_phi(ir_instr *in, char *ind,
1712 int (*oprintf)(const char*, ...))
1715 oprintf("%s <- phi ", in->_ops[0]->name);
1716 for (i = 0; i < in->phi_count; ++i)
1718 oprintf("([%s] : %s) ", in->phi[i].from->label,
1719 in->phi[i].value->name);
1724 void ir_instr_dump(ir_instr *in, char *ind,
1725 int (*oprintf)(const char*, ...))
1728 const char *comma = NULL;
1730 oprintf("%s (%i) ", ind, (int)in->eid);
1732 if (in->opcode == VINSTR_PHI) {
1733 dump_phi(in, ind, oprintf);
1737 strncat(ind, "\t", IND_BUFSZ);
1739 if (in->_ops[0] && (in->_ops[1] || in->_ops[2])) {
1740 ir_value_dump(in->_ops[0], oprintf);
1741 if (in->_ops[1] || in->_ops[2])
1744 oprintf("%s\t", qc_opname(in->opcode));
1745 if (in->_ops[0] && !(in->_ops[1] || in->_ops[2])) {
1746 ir_value_dump(in->_ops[0], oprintf);
1751 for (i = 1; i != 3; ++i) {
1755 ir_value_dump(in->_ops[i], oprintf);
1763 oprintf("[%s]", in->bops[0]->label);
1767 oprintf("%s[%s]", comma, in->bops[1]->label);
1769 ind[strlen(ind)-1] = 0;
1772 void ir_value_dump(ir_value* v, int (*oprintf)(const char*, ...))
1780 oprintf("%g", v->constval.vfloat);
1783 oprintf("'%g %g %g'",
1786 v->constval.vvec.z);
1789 oprintf("(entity)");
1792 oprintf("\"%s\"", v->constval.vstring);
1796 oprintf("%i", v->constval.vint);
1801 v->constval.vpointer->name);
1805 oprintf("%s", v->name);
1809 void ir_value_dump_life(ir_value *self, int (*oprintf)(const char*,...))
1812 oprintf("Life of %s:\n", self->name);
1813 for (i = 0; i < self->life_count; ++i)
1815 oprintf(" + [%i, %i]\n", self->life[i].start, self->life[i].end);