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 /***********************************************************************
565 bool ir_block_create_store_op(ir_block *self, int op, ir_value *target, ir_value *what)
567 if (target->store == store_value) {
568 fprintf(stderr, "cannot store to an SSA value\n");
571 ir_instr *in = ir_instr_new(self, op);
574 if (!ir_instr_op(in, 0, target, true) ||
575 !ir_instr_op(in, 1, what, false) ||
576 !ir_block_instr_add(self, in) )
584 bool ir_block_create_store(ir_block *self, ir_value *target, ir_value *what)
588 if (target->vtype == TYPE_VARIANT)
591 vtype = target->vtype;
596 if (what->vtype == TYPE_INTEGER)
597 op = INSTR_CONV_ITOF;
606 op = INSTR_STORE_ENT;
613 if (what->vtype == TYPE_INTEGER)
614 op = INSTR_CONV_FTOI;
623 op = INSTR_STORE_ENT;
627 return ir_block_create_store_op(self, op, target, what);
630 bool ir_block_create_return(ir_block *self, ir_value *v)
634 fprintf(stderr, "block already ended (%s)\n", self->label);
638 self->is_return = true;
639 in = ir_instr_new(self, INSTR_RETURN);
643 if (!ir_instr_op(in, 0, v, false) ||
644 !ir_block_instr_add(self, in) )
651 bool ir_block_create_if(ir_block *self, ir_value *v,
652 ir_block *ontrue, ir_block *onfalse)
656 fprintf(stderr, "block already ended (%s)\n", self->label);
660 /*in = ir_instr_new(self, (v->vtype == TYPE_STRING ? INSTR_IF_S : INSTR_IF_F));*/
661 in = ir_instr_new(self, VINSTR_COND);
665 if (!ir_instr_op(in, 0, v, false)) {
670 in->bops[0] = ontrue;
671 in->bops[1] = onfalse;
673 if (!ir_block_instr_add(self, in))
676 if (!ir_block_exits_add(self, ontrue) ||
677 !ir_block_exits_add(self, onfalse) ||
678 !ir_block_entries_add(ontrue, self) ||
679 !ir_block_entries_add(onfalse, self) )
686 bool ir_block_create_jump(ir_block *self, ir_block *to)
690 fprintf(stderr, "block already ended (%s)\n", self->label);
694 in = ir_instr_new(self, VINSTR_JUMP);
699 if (!ir_block_instr_add(self, in))
702 if (!ir_block_exits_add(self, to) ||
703 !ir_block_entries_add(to, self) )
710 bool ir_block_create_goto(ir_block *self, ir_block *to)
714 fprintf(stderr, "block already ended (%s)\n", self->label);
718 in = ir_instr_new(self, INSTR_GOTO);
723 if (!ir_block_instr_add(self, in))
726 if (!ir_block_exits_add(self, to) ||
727 !ir_block_entries_add(to, self) )
734 ir_instr* ir_block_create_phi(ir_block *self, const char *label, int ot)
738 in = ir_instr_new(self, VINSTR_PHI);
741 out = ir_value_out(self->owner, label, store_local, ot);
746 if (!ir_instr_op(in, 0, out, true)) {
748 ir_value_delete(out);
751 if (!ir_block_instr_add(self, in)) {
753 ir_value_delete(out);
759 ir_value* ir_phi_value(ir_instr *self)
761 return self->_ops[0];
764 bool ir_phi_add(ir_instr* self, ir_block *b, ir_value *v)
768 if (!ir_block_entries_find(self->owner, b, NULL)) {
769 /* Must not be possible to cause this, otherwise the AST
770 * is doing something wrong.
772 fprintf(stderr, "Invalid entry block for PHI\n");
778 if (!ir_value_reads_add(v, self))
780 return ir_instr_phi_add(self, pe);
783 /* binary op related code */
785 ir_value* ir_block_create_binop(ir_block *self,
786 const char *label, int opcode,
787 ir_value *left, ir_value *right)
789 ir_value *out = NULL;
812 case INSTR_SUB_S: /* -- offset of string as float */
819 case INSTR_BITAND_FI:
820 case INSTR_BITAND_IF:
861 /* boolean operations result in floats */
862 if (opcode >= INSTR_EQ_F && opcode <= INSTR_GT)
864 else if (opcode >= INSTR_LE && opcode <= INSTR_GT)
867 else if (opcode >= INSTR_LE_I && opcode <= INSTR_EQ_FI)
872 if (ot == TYPE_VOID) {
873 /* The AST or parser were supposed to check this! */
877 out = ir_value_out(self->owner, label, store_local, ot);
881 in = ir_instr_new(self, opcode);
883 ir_value_delete(out);
887 if (!ir_instr_op(in, 0, out, true) ||
888 !ir_instr_op(in, 1, left, false) ||
889 !ir_instr_op(in, 2, right, false) )
894 if (!ir_block_instr_add(self, in))
899 ir_value_delete(out);
904 ir_value* ir_block_create_add(ir_block *self,
906 ir_value *left, ir_value *right)
910 int r = right->vtype;
929 if ( (l == TYPE_FLOAT && r == TYPE_INTEGER) )
931 else if ( (l == TYPE_INTEGER && r == TYPE_FLOAT) )
937 return ir_block_create_binop(self, label, op, left, right);
940 ir_value* ir_block_create_sub(ir_block *self,
942 ir_value *left, ir_value *right)
946 int r = right->vtype;
966 if ( (l == TYPE_FLOAT && r == TYPE_INTEGER) )
968 else if ( (l == TYPE_INTEGER && r == TYPE_FLOAT) )
974 return ir_block_create_binop(self, label, op, left, right);
977 ir_value* ir_block_create_mul(ir_block *self,
979 ir_value *left, ir_value *right)
983 int r = right->vtype;
1002 if ( (l == TYPE_VECTOR && r == TYPE_FLOAT) )
1004 else if ( (l == TYPE_FLOAT && r == TYPE_VECTOR) )
1007 else if ( (l == TYPE_VECTOR && r == TYPE_INTEGER) )
1009 else if ( (l == TYPE_INTEGER && r == TYPE_VECTOR) )
1011 else if ( (l == TYPE_FLOAT && r == TYPE_INTEGER) )
1013 else if ( (l == TYPE_INTEGER && r == TYPE_FLOAT) )
1019 return ir_block_create_binop(self, label, op, left, right);
1022 ir_value* ir_block_create_div(ir_block *self,
1024 ir_value *left, ir_value *right)
1027 int l = left->vtype;
1028 int r = right->vtype;
1045 if ( (l == TYPE_VECTOR && r == TYPE_FLOAT) )
1047 else if ( (l == TYPE_FLOAT && r == TYPE_INTEGER) )
1049 else if ( (l == TYPE_INTEGER && r == TYPE_FLOAT) )
1055 return ir_block_create_binop(self, label, op, left, right);
1058 /* PHI resolving breaks the SSA, and must thus be the last
1059 * step before life-range calculation.
1062 static bool ir_block_naive_phi(ir_block *self);
1063 bool ir_function_naive_phi(ir_function *self)
1067 for (i = 0; i < self->blocks_count; ++i)
1069 if (!ir_block_naive_phi(self->blocks[i]))
1075 static bool ir_naive_phi_emit_store(ir_block *block, size_t iid, ir_value *old, ir_value *what)
1080 /* create a store */
1081 if (!ir_block_create_store(block, old, what))
1084 /* we now move it up */
1085 instr = block->instr[block->instr_count-1];
1086 for (i = block->instr_count; i > iid; --i)
1087 block->instr[i] = block->instr[i-1];
1088 block->instr[i] = instr;
1093 static bool ir_block_naive_phi(ir_block *self)
1096 /* FIXME: optionally, create_phi can add the phis
1097 * to a list so we don't need to loop through blocks
1098 * - anyway: "don't optimize YET"
1100 for (i = 0; i < self->instr_count; ++i)
1102 ir_instr *instr = self->instr[i];
1103 if (instr->opcode != VINSTR_PHI)
1106 if (!ir_block_instr_remove(self, i))
1108 --i; /* NOTE: i+1 below */
1110 for (p = 0; p < instr->phi_count; ++p)
1112 ir_value *v = instr->phi[p].value;
1113 for (w = 0; w < v->writes_count; ++w) {
1116 if (!v->writes[w]->_ops[0])
1119 /* When the write was to a global, we have to emit a mov */
1120 old = v->writes[w]->_ops[0];
1122 /* The original instruction now writes to the PHI target local */
1123 if (v->writes[w]->_ops[0] == v)
1124 v->writes[w]->_ops[0] = instr->_ops[0];
1126 if (old->store != store_local)
1128 /* If it originally wrote to a global we need to store the value
1131 if (!ir_naive_phi_emit_store(self, i+1, old, v))
1133 if (i+1 < self->instr_count)
1134 instr = self->instr[i+1];
1137 /* In case I forget and access instr later, it'll be NULL
1138 * when it's a problem, to make sure we crash, rather than accessing
1144 /* If it didn't, we can replace all reads by the phi target now. */
1146 for (r = 0; r < old->reads_count; ++r)
1149 ir_instr *ri = old->reads[r];
1150 for (op = 0; op < ri->phi_count; ++op) {
1151 if (ri->phi[op].value == old)
1152 ri->phi[op].value = v;
1154 for (op = 0; op < 3; ++op) {
1155 if (ri->_ops[op] == old)
1162 ir_instr_delete(instr);
1167 /***********************************************************************
1168 *IR Temp allocation code
1169 * Propagating value life ranges by walking through the function backwards
1170 * until no more changes are made.
1171 * In theory this should happen once more than once for every nested loop
1173 * Though this implementation might run an additional time for if nests.
1182 MEM_VEC_FUNCTIONS_ALL(new_reads_t, ir_value*, v)
1184 /* Enumerate instructions used by value's life-ranges
1186 static void ir_block_enumerate(ir_block *self, size_t *_eid)
1190 for (i = 0; i < self->instr_count; ++i)
1192 self->instr[i]->eid = eid++;
1197 /* Enumerate blocks and instructions.
1198 * The block-enumeration is unordered!
1199 * We do not really use the block enumreation, however
1200 * the instruction enumeration is important for life-ranges.
1202 void ir_function_enumerate(ir_function *self)
1205 size_t instruction_id = 0;
1206 for (i = 0; i < self->blocks_count; ++i)
1208 self->blocks[i]->eid = i;
1209 self->blocks[i]->run_id = 0;
1210 ir_block_enumerate(self->blocks[i], &instruction_id);
1214 static bool ir_block_life_propagate(ir_block *b, ir_block *prev, bool *changed);
1215 bool ir_function_calculate_liferanges(ir_function *self)
1223 for (i = 0; i != self->blocks_count; ++i)
1225 if (self->blocks[i]->is_return)
1227 if (!ir_block_life_propagate(self->blocks[i], NULL, &changed))
1235 /* Get information about which operand
1236 * is read from, or written to.
1238 static void ir_op_read_write(int op, size_t *read, size_t *write)
1265 static bool ir_block_living_add_instr(ir_block *self, size_t eid)
1268 bool changed = false;
1270 for (i = 0; i != self->living_count; ++i)
1272 tempbool = ir_value_life_merge(self->living[i], eid);
1275 fprintf(stderr, "block_living_add_instr() value instruction added %s: %i\n", self->living[i]->_name, (int)eid);
1277 changed = changed || tempbool;
1282 static bool ir_block_life_prop_previous(ir_block* self, ir_block *prev, bool *changed)
1285 /* values which have been read in a previous iteration are now
1286 * in the "living" array even if the previous block doesn't use them.
1287 * So we have to remove whatever does not exist in the previous block.
1288 * They will be re-added on-read, but the liferange merge won't cause
1291 for (i = 0; i < self->living_count; ++i)
1293 if (!ir_block_living_find(prev, self->living[i], NULL)) {
1294 if (!ir_block_living_remove(self, i))
1300 /* Whatever the previous block still has in its living set
1301 * must now be added to ours as well.
1303 for (i = 0; i < prev->living_count; ++i)
1305 if (ir_block_living_find(self, prev->living[i], NULL))
1307 if (!ir_block_living_add(self, prev->living[i]))
1310 printf("%s got from prev: %s\n", self->label, prev->living[i]->_name);
1316 static bool ir_block_life_propagate(ir_block *self, ir_block *prev, bool *changed)
1322 /* bitmasks which operands are read from or written to */
1324 new_reads_t new_reads;
1325 char dbg_ind[16] = { '#', '0' };
1328 MEM_VECTOR_INIT(&new_reads, v);
1332 if (!ir_block_life_prop_previous(self, prev, changed))
1336 i = self->instr_count;
1339 instr = self->instr[i];
1341 /* PHI operands are always read operands */
1342 for (p = 0; p < instr->phi_count; ++p)
1344 value = instr->phi[p].value;
1345 /* used this before new_reads - puts the last read into the life range as well
1346 if (!ir_block_living_find(self, value, NULL))
1347 ir_block_living_add(self, value);
1349 /* fprintf(stderr, "read: %s\n", value->_name); */
1350 if (!new_reads_t_v_find(&new_reads, value, NULL))
1352 if (!new_reads_t_v_add(&new_reads, value))
1357 /* See which operands are read and write operands */
1358 ir_op_read_write(instr->opcode, &read, &write);
1360 /* Go through the 3 main operands */
1361 for (o = 0; o < 3; ++o)
1363 if (!instr->_ops[o]) /* no such operand */
1366 value = instr->_ops[o];
1368 /* We only care about locals */
1369 if (value->store != store_value &&
1370 value->store != store_local)
1376 /* used this before new_reads - puts the last read into the life range as well
1377 if (!ir_block_living_find(self, value, NULL))
1378 ir_block_living_add(self, value);
1380 /* fprintf(stderr, "read: %s\n", value->_name); */
1381 if (!new_reads_t_v_find(&new_reads, value, NULL))
1383 if (!new_reads_t_v_add(&new_reads, value))
1388 /* write operands */
1389 /* When we write to a local, we consider it "dead" for the
1390 * remaining upper part of the function, since in SSA a value
1391 * can only be written once (== created)
1395 size_t idx, readidx;
1396 bool in_living = ir_block_living_find(self, value, &idx);
1397 bool in_reads = new_reads_t_v_find(&new_reads, value, &readidx);
1398 if (!in_living && !in_reads)
1400 /* If the value isn't alive it hasn't been read before... */
1401 /* TODO: See if the warning can be emitted during parsing or AST processing
1402 * otherwise have warning printed here.
1403 * IF printing a warning here: include filecontext_t,
1404 * and make sure it's only printed once
1405 * since this function is run multiple times.
1407 /* For now: debug info: */
1408 fprintf(stderr, "Value only written %s\n", value->name);
1409 tempbool = ir_value_life_merge(value, instr->eid);
1410 *changed = *changed || tempbool;
1412 ir_instr_dump(instr, dbg_ind, printf);
1416 /* since 'living' won't contain it
1417 * anymore, merge the value, since
1420 tempbool = ir_value_life_merge(value, instr->eid);
1423 fprintf(stderr, "value added id %s %i\n", value->name, (int)instr->eid);
1425 *changed = *changed || tempbool;
1427 if (!ir_block_living_remove(self, idx))
1431 if (!new_reads_t_v_remove(&new_reads, readidx))
1438 tempbool = ir_block_living_add_instr(self, instr->eid);
1439 /*fprintf(stderr, "living added values\n");*/
1440 *changed = *changed || tempbool;
1443 for (rd = 0; rd < new_reads.v_count; ++rd)
1445 if (!ir_block_living_find(self, new_reads.v[rd], NULL)) {
1446 if (!ir_block_living_add(self, new_reads.v[rd]))
1449 if (!i && !self->entries_count) {
1451 *changed = *changed || ir_value_life_merge(new_reads.v[rd], instr->eid);
1454 MEM_VECTOR_CLEAR(&new_reads, v);
1457 if (self->run_id == self->owner->run_id)
1460 self->run_id = self->owner->run_id;
1462 for (i = 0; i < self->entries_count; ++i)
1464 ir_block *entry = self->entries[i];
1465 ir_block_life_propagate(entry, self, changed);
1470 MEM_VECTOR_CLEAR(&new_reads, v);
1474 /***********************************************************************
1475 *IR DEBUG Dump functions...
1478 #define IND_BUFSZ 1024
1480 const char *qc_opname(int op)
1482 if (op < 0) return "<INVALID>";
1483 if (op < ( sizeof(asm_instr) / sizeof(asm_instr[0]) ))
1484 return asm_instr[op].m;
1486 case VINSTR_PHI: return "PHI";
1487 case VINSTR_JUMP: return "JUMP";
1488 case VINSTR_COND: return "COND";
1489 default: return "<UNK>";
1493 void ir_builder_dump(ir_builder *b, int (*oprintf)(const char*, ...))
1496 char indent[IND_BUFSZ];
1500 oprintf("module %s\n", b->name);
1501 for (i = 0; i < b->globals_count; ++i)
1504 if (b->globals[i]->isconst)
1505 oprintf("%s = ", b->globals[i]->name);
1506 ir_value_dump(b->globals[i], oprintf);
1509 for (i = 0; i < b->functions_count; ++i)
1510 ir_function_dump(b->functions[i], indent, oprintf);
1511 oprintf("endmodule %s\n", b->name);
1514 void ir_function_dump(ir_function *f, char *ind,
1515 int (*oprintf)(const char*, ...))
1518 oprintf("%sfunction %s\n", ind, f->name);
1519 strncat(ind, "\t", IND_BUFSZ);
1520 if (f->locals_count)
1522 oprintf("%s%i locals:\n", ind, (int)f->locals_count);
1523 for (i = 0; i < f->locals_count; ++i) {
1524 oprintf("%s\t", ind);
1525 ir_value_dump(f->locals[i], oprintf);
1529 if (f->blocks_count)
1532 oprintf("%slife passes: %i\n", ind, (int)f->blocks[0]->run_id);
1533 for (i = 0; i < f->blocks_count; ++i)
1534 ir_block_dump(f->blocks[i], ind, oprintf);
1537 ind[strlen(ind)-1] = 0;
1538 oprintf("%sendfunction %s\n", ind, f->name);
1541 void ir_block_dump(ir_block* b, char *ind,
1542 int (*oprintf)(const char*, ...))
1545 oprintf("%s:%s\n", ind, b->label);
1546 strncat(ind, "\t", IND_BUFSZ);
1548 for (i = 0; i < b->instr_count; ++i)
1549 ir_instr_dump(b->instr[i], ind, oprintf);
1550 ind[strlen(ind)-1] = 0;
1553 void dump_phi(ir_instr *in, char *ind,
1554 int (*oprintf)(const char*, ...))
1557 oprintf("%s <- phi ", in->_ops[0]->name);
1558 for (i = 0; i < in->phi_count; ++i)
1560 oprintf("([%s] : %s) ", in->phi[i].from->label,
1561 in->phi[i].value->name);
1566 void ir_instr_dump(ir_instr *in, char *ind,
1567 int (*oprintf)(const char*, ...))
1570 const char *comma = NULL;
1572 oprintf("%s (%i) ", ind, (int)in->eid);
1574 if (in->opcode == VINSTR_PHI) {
1575 dump_phi(in, ind, oprintf);
1579 strncat(ind, "\t", IND_BUFSZ);
1581 if (in->_ops[0] && (in->_ops[1] || in->_ops[2])) {
1582 ir_value_dump(in->_ops[0], oprintf);
1583 if (in->_ops[1] || in->_ops[2])
1586 oprintf("%s\t", qc_opname(in->opcode));
1587 if (in->_ops[0] && !(in->_ops[1] || in->_ops[2])) {
1588 ir_value_dump(in->_ops[0], oprintf);
1593 for (i = 1; i != 3; ++i) {
1597 ir_value_dump(in->_ops[i], oprintf);
1605 oprintf("[%s]", in->bops[0]->label);
1609 oprintf("%s[%s]", comma, in->bops[1]->label);
1611 ind[strlen(ind)-1] = 0;
1614 void ir_value_dump(ir_value* v, int (*oprintf)(const char*, ...))
1622 oprintf("%g", v->constval.vfloat);
1625 oprintf("'%g %g %g'",
1628 v->constval.vvec.z);
1631 oprintf("(entity)");
1634 oprintf("\"%s\"", v->constval.vstring);
1638 oprintf("%i", v->constval.vint);
1643 v->constval.vpointer->name);
1647 oprintf("%s", v->name);
1651 void ir_value_dump_life(ir_value *self, int (*oprintf)(const char*,...))
1654 oprintf("Life of %s:\n", self->name);
1655 for (i = 0; i < self->life_count; ++i)
1657 oprintf(" + [%i, %i]\n", self->life[i].start, self->life[i].end);