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));
143 if (!ir_function_set_name(self, "<@unnamed>")) {
148 self->context.file = "<@no context>";
149 self->context.line = 0;
150 self->retype = TYPE_VOID;
151 MEM_VECTOR_INIT(self, params);
152 MEM_VECTOR_INIT(self, blocks);
153 MEM_VECTOR_INIT(self, values);
154 MEM_VECTOR_INIT(self, locals);
159 MEM_VEC_FUNCTIONS(ir_function, ir_value*, values)
160 MEM_VEC_FUNCTIONS(ir_function, ir_block*, blocks)
161 MEM_VEC_FUNCTIONS(ir_function, ir_value*, locals)
163 bool ir_function_set_name(ir_function *self, const char *name)
166 mem_d((void*)self->name);
167 self->name = util_strdup(name);
171 void ir_function_delete(ir_function *self)
174 mem_d((void*)self->name);
176 for (i = 0; i != self->blocks_count; ++i)
177 ir_block_delete(self->blocks[i]);
178 MEM_VECTOR_CLEAR(self, blocks);
180 MEM_VECTOR_CLEAR(self, params);
182 for (i = 0; i != self->values_count; ++i)
183 ir_value_delete(self->values[i]);
184 MEM_VECTOR_CLEAR(self, values);
186 for (i = 0; i != self->locals_count; ++i)
187 ir_value_delete(self->locals[i]);
188 MEM_VECTOR_CLEAR(self, locals);
193 bool GMQCC_WARN ir_function_collect_value(ir_function *self, ir_value *v)
195 return ir_function_values_add(self, v);
198 ir_block* ir_function_create_block(ir_function *self, const char *label)
200 ir_block* bn = ir_block_new(self, label);
201 memcpy(&bn->context, &self->context, sizeof(self->context));
202 if (!ir_function_blocks_add(self, bn)) {
209 bool ir_function_finalize(ir_function *self)
211 if (!ir_function_naive_phi(self))
214 ir_function_enumerate(self);
216 if (!ir_function_calculate_liferanges(self))
221 ir_value* ir_function_get_local(ir_function *self, const char *name)
224 for (i = 0; i < self->locals_count; ++i) {
225 if (!strcmp(self->locals[i]->name, name))
226 return self->locals[i];
231 ir_value* ir_function_create_local(ir_function *self, const char *name, int vtype)
233 ir_value *ve = ir_function_get_local(self, name);
238 ve = ir_value_var(name, store_local, vtype);
239 if (!ir_function_locals_add(self, ve)) {
246 /***********************************************************************
250 ir_block* ir_block_new(ir_function* owner, const char *name)
253 self = (ir_block*)mem_a(sizeof(*self));
254 if (!ir_block_set_label(self, name)) {
259 self->context.file = "<@no context>";
260 self->context.line = 0;
262 MEM_VECTOR_INIT(self, instr);
263 MEM_VECTOR_INIT(self, entries);
264 MEM_VECTOR_INIT(self, exits);
268 self->is_return = false;
270 MEM_VECTOR_INIT(self, living);
273 MEM_VEC_FUNCTIONS(ir_block, ir_instr*, instr)
274 MEM_VEC_FUNCTIONS_ALL(ir_block, ir_block*, entries)
275 MEM_VEC_FUNCTIONS_ALL(ir_block, ir_block*, exits)
276 MEM_VEC_FUNCTIONS_ALL(ir_block, ir_value*, living)
278 void ir_block_delete(ir_block* self)
281 mem_d((void*)self->label);
282 for (i = 0; i != self->instr_count; ++i)
283 ir_instr_delete(self->instr[i]);
284 MEM_VECTOR_CLEAR(self, instr);
285 MEM_VECTOR_CLEAR(self, entries);
286 MEM_VECTOR_CLEAR(self, exits);
287 MEM_VECTOR_CLEAR(self, living);
291 bool ir_block_set_label(ir_block *self, const char *name)
294 mem_d((void*)self->label);
295 self->label = util_strdup(name);
296 return !!self->label;
299 /***********************************************************************
303 ir_instr* ir_instr_new(ir_block* owner, int op)
306 self = (ir_instr*)mem_a(sizeof(*self));
308 self->context.file = "<@no context>";
309 self->context.line = 0;
311 self->_ops[0] = NULL;
312 self->_ops[1] = NULL;
313 self->_ops[2] = NULL;
314 self->bops[0] = NULL;
315 self->bops[1] = NULL;
316 MEM_VECTOR_INIT(self, phi);
321 MEM_VEC_FUNCTIONS(ir_instr, ir_phi_entry_t, phi)
323 void ir_instr_delete(ir_instr *self)
326 /* The following calls can only delete from
327 * vectors, we still want to delete this instruction
328 * so ignore the return value. Since with the warn_unused_result attribute
329 * gcc doesn't care about an explicit: (void)foo(); to ignore the result,
330 * I have to improvise here and use if(foo());
332 for (i = 0; i < self->phi_count; ++i) {
334 if (ir_value_writes_find(self->phi[i].value, self, &idx))
335 if (ir_value_writes_remove(self->phi[i].value, idx));
336 if (ir_value_reads_find(self->phi[i].value, self, &idx))
337 if (ir_value_reads_remove(self->phi[i].value, idx));
339 MEM_VECTOR_CLEAR(self, phi);
340 if (ir_instr_op(self, 0, NULL, false));
341 if (ir_instr_op(self, 1, NULL, false));
342 if (ir_instr_op(self, 2, NULL, false));
346 bool ir_instr_op(ir_instr *self, int op, ir_value *v, bool writing)
348 if (self->_ops[op]) {
350 if (writing && ir_value_writes_find(self->_ops[op], self, &idx))
352 if (!ir_value_writes_remove(self->_ops[op], idx))
355 else if (ir_value_reads_find(self->_ops[op], self, &idx))
357 if (!ir_value_reads_remove(self->_ops[op], idx))
363 if (!ir_value_writes_add(v, self))
366 if (!ir_value_reads_add(v, self))
374 /***********************************************************************
378 ir_value* ir_value_var(const char *name, int storetype, int vtype)
381 self = (ir_value*)mem_a(sizeof(*self));
383 self->store = storetype;
384 MEM_VECTOR_INIT(self, reads);
385 MEM_VECTOR_INIT(self, writes);
386 self->isconst = false;
387 self->context.file = "<@no context>";
388 self->context.line = 0;
390 ir_value_set_name(self, name);
392 MEM_VECTOR_INIT(self, life);
395 MEM_VEC_FUNCTIONS(ir_value, ir_life_entry_t, life)
396 MEM_VEC_FUNCTIONS_ALL(ir_value, ir_instr*, reads)
397 MEM_VEC_FUNCTIONS_ALL(ir_value, ir_instr*, writes)
399 ir_value* ir_value_out(ir_function *owner, const char *name, int storetype, int vtype)
401 ir_value *v = ir_value_var(name, storetype, vtype);
404 if (!ir_function_collect_value(owner, v))
412 void ir_value_delete(ir_value* self)
414 mem_d((void*)self->name);
417 if (self->vtype == TYPE_STRING)
418 mem_d((void*)self->constval.vstring);
420 MEM_VECTOR_CLEAR(self, reads);
421 MEM_VECTOR_CLEAR(self, writes);
422 MEM_VECTOR_CLEAR(self, life);
426 void ir_value_set_name(ir_value *self, const char *name)
429 mem_d((void*)self->name);
430 self->name = util_strdup(name);
433 bool ir_value_set_float(ir_value *self, float f)
435 if (self->vtype != TYPE_FLOAT)
437 self->constval.vfloat = f;
438 self->isconst = true;
442 bool ir_value_set_vector(ir_value *self, vector_t v)
444 if (self->vtype != TYPE_VECTOR)
446 self->constval.vvec = v;
447 self->isconst = true;
451 bool ir_value_set_string(ir_value *self, const char *str)
453 if (self->vtype != TYPE_STRING)
455 self->constval.vstring = util_strdup(str);
456 self->isconst = true;
461 bool ir_value_set_int(ir_value *self, int i)
463 if (self->vtype != TYPE_INTEGER)
465 self->constval.vint = i;
466 self->isconst = true;
471 bool ir_value_lives(ir_value *self, size_t at)
474 for (i = 0; i < self->life_count; ++i)
476 ir_life_entry_t *life = &self->life[i];
477 if (life->start <= at && at <= life->end)
479 if (life->start > at) /* since it's ordered */
485 bool ir_value_life_insert(ir_value *self, size_t idx, ir_life_entry_t e)
488 if (!ir_value_life_add(self, e)) /* naive... */
490 for (k = self->life_count-1; k > idx; --k)
491 self->life[k] = self->life[k-1];
496 bool ir_value_life_merge(ir_value *self, size_t s)
499 ir_life_entry_t *life = NULL;
500 ir_life_entry_t *before = NULL;
501 ir_life_entry_t new_entry;
503 /* Find the first range >= s */
504 for (i = 0; i < self->life_count; ++i)
507 life = &self->life[i];
511 /* nothing found? append */
512 if (i == self->life_count) {
513 if (life && life->end+1 == s)
515 /* previous life range can be merged in */
519 if (life && life->end >= s)
523 if (!ir_value_life_add(self, e))
524 return false; /* failing */
530 if (before->end + 1 == s &&
531 life->start - 1 == s)
534 before->end = life->end;
535 if (!ir_value_life_remove(self, i))
536 return false; /* failing */
539 if (before->end + 1 == s)
545 /* already contained */
546 if (before->end >= s)
550 if (life->start - 1 == s)
555 /* insert a new entry */
556 new_entry.start = new_entry.end = s;
557 return ir_value_life_insert(self, i, new_entry);
560 /***********************************************************************
564 bool ir_block_create_store_op(ir_block *self, int op, ir_value *target, ir_value *what)
566 if (target->store == store_value) {
567 fprintf(stderr, "cannot store to an SSA value\n");
570 ir_instr *in = ir_instr_new(self, op);
573 if (!ir_instr_op(in, 0, target, true) ||
574 !ir_instr_op(in, 1, what, false) ||
575 !ir_block_instr_add(self, in) )
583 bool ir_block_create_store(ir_block *self, ir_value *target, ir_value *what)
587 if (target->vtype == TYPE_VARIANT)
590 vtype = target->vtype;
595 if (what->vtype == TYPE_INTEGER)
596 op = INSTR_CONV_ITOF;
605 op = INSTR_STORE_ENT;
612 if (what->vtype == TYPE_INTEGER)
613 op = INSTR_CONV_FTOI;
622 op = INSTR_STORE_ENT;
626 return ir_block_create_store_op(self, op, target, what);
629 bool ir_block_create_return(ir_block *self, ir_value *v)
633 fprintf(stderr, "block already ended (%s)\n", self->label);
637 self->is_return = true;
638 in = ir_instr_new(self, INSTR_RETURN);
642 if (!ir_instr_op(in, 0, v, false) ||
643 !ir_block_instr_add(self, in) )
650 bool ir_block_create_if(ir_block *self, ir_value *v,
651 ir_block *ontrue, ir_block *onfalse)
655 fprintf(stderr, "block already ended (%s)\n", self->label);
659 //in = ir_instr_new(self, (v->vtype == TYPE_STRING ? INSTR_IF_S : INSTR_IF_F));
660 in = ir_instr_new(self, VINSTR_COND);
664 if (!ir_instr_op(in, 0, v, false)) {
669 in->bops[0] = ontrue;
670 in->bops[1] = onfalse;
672 if (!ir_block_instr_add(self, in))
675 if (!ir_block_exits_add(self, ontrue) ||
676 !ir_block_exits_add(self, onfalse) ||
677 !ir_block_entries_add(ontrue, self) ||
678 !ir_block_entries_add(onfalse, self) )
685 bool ir_block_create_jump(ir_block *self, ir_block *to)
689 fprintf(stderr, "block already ended (%s)\n", self->label);
693 in = ir_instr_new(self, VINSTR_JUMP);
698 if (!ir_block_instr_add(self, in))
701 if (!ir_block_exits_add(self, to) ||
702 !ir_block_entries_add(to, self) )
709 bool ir_block_create_goto(ir_block *self, ir_block *to)
713 fprintf(stderr, "block already ended (%s)\n", self->label);
717 in = ir_instr_new(self, INSTR_GOTO);
722 if (!ir_block_instr_add(self, in))
725 if (!ir_block_exits_add(self, to) ||
726 !ir_block_entries_add(to, self) )
733 ir_instr* ir_block_create_phi(ir_block *self, const char *label, int ot)
737 in = ir_instr_new(self, VINSTR_PHI);
740 out = ir_value_out(self->owner, label, store_local, ot);
745 if (!ir_instr_op(in, 0, out, true)) {
747 ir_value_delete(out);
750 if (!ir_block_instr_add(self, in)) {
752 ir_value_delete(out);
758 ir_value* ir_phi_value(ir_instr *self)
760 return self->_ops[0];
763 bool ir_phi_add(ir_instr* self, ir_block *b, ir_value *v)
767 if (!ir_block_entries_find(self->owner, b, NULL)) {
768 /* Must not be possible to cause this, otherwise the AST
769 * is doing something wrong.
771 fprintf(stderr, "Invalid entry block for PHI\n");
777 if (!ir_value_reads_add(v, self))
779 return ir_instr_phi_add(self, pe);
782 /* binary op related code */
784 ir_value* ir_block_create_binop(ir_block *self,
785 const char *label, int opcode,
786 ir_value *left, ir_value *right)
808 case INSTR_SUB_S: /* -- offset of string as float */
815 case INSTR_BITAND_FI:
816 case INSTR_BITAND_IF:
857 /* boolean operations result in floats */
858 if (opcode >= INSTR_EQ_F && opcode <= INSTR_GT)
860 else if (opcode >= INSTR_LE && opcode <= INSTR_GT)
863 else if (opcode >= INSTR_LE_I && opcode <= INSTR_EQ_FI)
868 if (ot == TYPE_VOID) {
869 /* The AST or parser were supposed to check this! */
873 ir_value *out = ir_value_out(self->owner, label, store_local, ot);
877 ir_instr *in = ir_instr_new(self, opcode);
879 ir_value_delete(out);
883 if (!ir_instr_op(in, 0, out, true) ||
884 !ir_instr_op(in, 1, left, false) ||
885 !ir_instr_op(in, 2, right, false) )
890 if (!ir_block_instr_add(self, in))
895 ir_value_delete(out);
900 ir_value* ir_block_create_add(ir_block *self,
902 ir_value *left, ir_value *right)
906 int r = right->vtype;
925 if ( (l == TYPE_FLOAT && r == TYPE_INTEGER) )
927 else if ( (l == TYPE_INTEGER && r == TYPE_FLOAT) )
933 return ir_block_create_binop(self, label, op, left, right);
936 ir_value* ir_block_create_sub(ir_block *self,
938 ir_value *left, ir_value *right)
942 int r = right->vtype;
962 if ( (l == TYPE_FLOAT && r == TYPE_INTEGER) )
964 else if ( (l == TYPE_INTEGER && r == TYPE_FLOAT) )
970 return ir_block_create_binop(self, label, op, left, right);
973 ir_value* ir_block_create_mul(ir_block *self,
975 ir_value *left, ir_value *right)
979 int r = right->vtype;
998 if ( (l == TYPE_VECTOR && r == TYPE_FLOAT) )
1000 else if ( (l == TYPE_FLOAT && r == TYPE_VECTOR) )
1003 else if ( (l == TYPE_VECTOR && r == TYPE_INTEGER) )
1005 else if ( (l == TYPE_INTEGER && r == TYPE_VECTOR) )
1007 else if ( (l == TYPE_FLOAT && r == TYPE_INTEGER) )
1009 else if ( (l == TYPE_INTEGER && r == TYPE_FLOAT) )
1015 return ir_block_create_binop(self, label, op, left, right);
1018 ir_value* ir_block_create_div(ir_block *self,
1020 ir_value *left, ir_value *right)
1023 int l = left->vtype;
1024 int r = right->vtype;
1041 if ( (l == TYPE_VECTOR && r == TYPE_FLOAT) )
1043 else if ( (l == TYPE_FLOAT && r == TYPE_INTEGER) )
1045 else if ( (l == TYPE_INTEGER && r == TYPE_FLOAT) )
1051 return ir_block_create_binop(self, label, op, left, right);
1054 /* PHI resolving breaks the SSA, and must thus be the last
1055 * step before life-range calculation.
1058 static bool ir_block_naive_phi(ir_block *self);
1059 bool ir_function_naive_phi(ir_function *self)
1063 for (i = 0; i < self->blocks_count; ++i)
1065 if (!ir_block_naive_phi(self->blocks[i]))
1071 static bool ir_naive_phi_emit_store(ir_block *block, size_t iid, ir_value *old, ir_value *what)
1076 /* create a store */
1077 if (!ir_block_create_store(block, old, what))
1080 /* we now move it up */
1081 instr = block->instr[block->instr_count-1];
1082 for (i = block->instr_count; i > iid; --i)
1083 block->instr[i] = block->instr[i-1];
1084 block->instr[i] = instr;
1089 static bool ir_block_naive_phi(ir_block *self)
1092 /* FIXME: optionally, create_phi can add the phis
1093 * to a list so we don't need to loop through blocks
1094 * - anyway: "don't optimize YET"
1096 for (i = 0; i < self->instr_count; ++i)
1098 ir_instr *instr = self->instr[i];
1099 if (instr->opcode != VINSTR_PHI)
1102 if (!ir_block_instr_remove(self, i))
1104 --i; /* NOTE: i+1 below */
1106 for (p = 0; p < instr->phi_count; ++p)
1108 ir_value *v = instr->phi[p].value;
1109 for (w = 0; w < v->writes_count; ++w) {
1112 if (!v->writes[w]->_ops[0])
1115 /* When the write was to a global, we have to emit a mov */
1116 old = v->writes[w]->_ops[0];
1118 /* The original instruction now writes to the PHI target local */
1119 if (v->writes[w]->_ops[0] == v)
1120 v->writes[w]->_ops[0] = instr->_ops[0];
1122 if (old->store != store_local)
1124 /* If it originally wrote to a global we need to store the value
1127 if (!ir_naive_phi_emit_store(self, i+1, old, v))
1129 if (i+1 < self->instr_count)
1130 instr = self->instr[i+1];
1133 /* In case I forget and access instr later, it'll be NULL
1134 * when it's a problem, to make sure we crash, rather than accessing
1140 /* If it didn't, we can replace all reads by the phi target now. */
1142 for (r = 0; r < old->reads_count; ++r)
1145 ir_instr *ri = old->reads[r];
1146 for (op = 0; op < ri->phi_count; ++op) {
1147 if (ri->phi[op].value == old)
1148 ri->phi[op].value = v;
1150 for (op = 0; op < 3; ++op) {
1151 if (ri->_ops[op] == old)
1158 ir_instr_delete(instr);
1163 /***********************************************************************
1164 *IR Temp allocation code
1165 * Propagating value life ranges by walking through the function backwards
1166 * until no more changes are made.
1167 * In theory this should happen once more than once for every nested loop
1169 * Though this implementation might run an additional time for if nests.
1178 MEM_VEC_FUNCTIONS_ALL(new_reads_t, ir_value*, v)
1180 /* Enumerate instructions used by value's life-ranges
1182 static void ir_block_enumerate(ir_block *self, size_t *_eid)
1186 for (i = 0; i < self->instr_count; ++i)
1188 self->instr[i]->eid = eid++;
1193 /* Enumerate blocks and instructions.
1194 * The block-enumeration is unordered!
1195 * We do not really use the block enumreation, however
1196 * the instruction enumeration is important for life-ranges.
1198 void ir_function_enumerate(ir_function *self)
1201 size_t instruction_id = 0;
1202 for (i = 0; i < self->blocks_count; ++i)
1204 self->blocks[i]->eid = i;
1205 self->blocks[i]->run_id = 0;
1206 ir_block_enumerate(self->blocks[i], &instruction_id);
1210 static bool ir_block_life_propagate(ir_block *b, ir_block *prev, bool *changed);
1211 bool ir_function_calculate_liferanges(ir_function *self)
1219 for (i = 0; i != self->blocks_count; ++i)
1221 if (self->blocks[i]->is_return)
1223 if (!ir_block_life_propagate(self->blocks[i], NULL, &changed))
1231 /* Get information about which operand
1232 * is read from, or written to.
1234 static void ir_op_read_write(int op, size_t *read, size_t *write)
1261 static bool ir_block_living_add_instr(ir_block *self, size_t eid)
1264 bool changed = false;
1266 for (i = 0; i != self->living_count; ++i)
1268 tempbool = ir_value_life_merge(self->living[i], eid);
1271 fprintf(stderr, "block_living_add_instr() value instruction added %s: %i\n", self->living[i]->_name, (int)eid);
1273 changed = changed || tempbool;
1278 static bool ir_block_life_prop_previous(ir_block* self, ir_block *prev, bool *changed)
1281 /* values which have been read in a previous iteration are now
1282 * in the "living" array even if the previous block doesn't use them.
1283 * So we have to remove whatever does not exist in the previous block.
1284 * They will be re-added on-read, but the liferange merge won't cause
1287 for (i = 0; i < self->living_count; ++i)
1289 if (!ir_block_living_find(prev, self->living[i], NULL)) {
1290 if (!ir_block_living_remove(self, i))
1296 /* Whatever the previous block still has in its living set
1297 * must now be added to ours as well.
1299 for (i = 0; i < prev->living_count; ++i)
1301 if (ir_block_living_find(self, prev->living[i], NULL))
1303 if (!ir_block_living_add(self, prev->living[i]))
1306 printf("%s got from prev: %s\n", self->label, prev->living[i]->_name);
1312 static bool ir_block_life_propagate(ir_block *self, ir_block *prev, bool *changed)
1318 /* bitmasks which operands are read from or written to */
1320 new_reads_t new_reads;
1321 char dbg_ind[16] = { '#', '0' };
1324 MEM_VECTOR_INIT(&new_reads, v);
1328 if (!ir_block_life_prop_previous(self, prev, changed))
1332 i = self->instr_count;
1335 instr = self->instr[i];
1337 /* PHI operands are always read operands */
1338 for (p = 0; p < instr->phi_count; ++p)
1340 value = instr->phi[p].value;
1341 /* used this before new_reads - puts the last read into the life range as well
1342 if (!ir_block_living_find(self, value, NULL))
1343 ir_block_living_add(self, value);
1345 /* fprintf(stderr, "read: %s\n", value->_name); */
1346 if (!new_reads_t_v_find(&new_reads, value, NULL))
1348 if (!new_reads_t_v_add(&new_reads, value))
1353 /* See which operands are read and write operands */
1354 ir_op_read_write(instr->opcode, &read, &write);
1356 /* Go through the 3 main operands */
1357 for (o = 0; o < 3; ++o)
1359 if (!instr->_ops[o]) /* no such operand */
1362 value = instr->_ops[o];
1364 /* We only care about locals */
1365 if (value->store != store_value &&
1366 value->store != store_local)
1372 /* used this before new_reads - puts the last read into the life range as well
1373 if (!ir_block_living_find(self, value, NULL))
1374 ir_block_living_add(self, value);
1376 /* fprintf(stderr, "read: %s\n", value->_name); */
1377 if (!new_reads_t_v_find(&new_reads, value, NULL))
1379 if (!new_reads_t_v_add(&new_reads, value))
1384 /* write operands */
1385 /* When we write to a local, we consider it "dead" for the
1386 * remaining upper part of the function, since in SSA a value
1387 * can only be written once (== created)
1391 size_t idx, readidx;
1392 bool in_living = ir_block_living_find(self, value, &idx);
1393 bool in_reads = new_reads_t_v_find(&new_reads, value, &readidx);
1394 if (!in_living && !in_reads)
1396 /* If the value isn't alive it hasn't been read before... */
1397 /* TODO: See if the warning can be emitted during parsing or AST processing
1398 * otherwise have warning printed here.
1399 * IF printing a warning here: include filecontext_t,
1400 * and make sure it's only printed once
1401 * since this function is run multiple times.
1403 /* For now: debug info: */
1404 fprintf(stderr, "Value only written %s\n", value->name);
1405 tempbool = ir_value_life_merge(value, instr->eid);
1406 *changed = *changed || tempbool;
1408 ir_instr_dump(instr, dbg_ind, printf);
1412 /* since 'living' won't contain it
1413 * anymore, merge the value, since
1416 tempbool = ir_value_life_merge(value, instr->eid);
1419 fprintf(stderr, "value added id %s %i\n", value->name, (int)instr->eid);
1421 *changed = *changed || tempbool;
1423 if (!ir_block_living_remove(self, idx))
1427 if (!new_reads_t_v_remove(&new_reads, readidx))
1434 tempbool = ir_block_living_add_instr(self, instr->eid);
1435 //fprintf(stderr, "living added values\n");
1436 *changed = *changed || tempbool;
1439 for (rd = 0; rd < new_reads.v_count; ++rd)
1441 if (!ir_block_living_find(self, new_reads.v[rd], NULL)) {
1442 if (!ir_block_living_add(self, new_reads.v[rd]))
1445 if (!i && !self->entries_count) {
1447 *changed = *changed || ir_value_life_merge(new_reads.v[rd], instr->eid);
1450 MEM_VECTOR_CLEAR(&new_reads, v);
1453 if (self->run_id == self->owner->run_id)
1456 self->run_id = self->owner->run_id;
1458 for (i = 0; i < self->entries_count; ++i)
1460 ir_block *entry = self->entries[i];
1461 ir_block_life_propagate(entry, self, changed);
1466 MEM_VECTOR_CLEAR(&new_reads, v);