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", qc_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 = qc_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 == qc_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 != qc_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 != qc_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 != qc_string)
455 self->constval.vstring = util_strdup(str);
456 self->isconst = true;
460 bool ir_value_set_int(ir_value *self, int i)
462 if (self->vtype != qc_int)
464 self->constval.vint = i;
465 self->isconst = true;
469 bool ir_value_lives(ir_value *self, size_t at)
472 for (i = 0; i < self->life_count; ++i)
474 ir_life_entry_t *life = &self->life[i];
475 if (life->start <= at && at <= life->end)
477 if (life->start > at) /* since it's ordered */
483 bool ir_value_life_insert(ir_value *self, size_t idx, ir_life_entry_t e)
486 if (!ir_value_life_add(self, e)) /* naive... */
488 for (k = self->life_count-1; k > idx; --k)
489 self->life[k] = self->life[k-1];
494 bool ir_value_life_merge(ir_value *self, size_t s)
497 ir_life_entry_t *life = NULL;
498 ir_life_entry_t *before = NULL;
499 ir_life_entry_t new_entry;
501 /* Find the first range >= s */
502 for (i = 0; i < self->life_count; ++i)
505 life = &self->life[i];
509 /* nothing found? append */
510 if (i == self->life_count) {
511 if (life && life->end+1 == s)
513 /* previous life range can be merged in */
517 if (life && life->end >= s)
521 if (!ir_value_life_add(self, e))
522 return false; /* failing */
528 if (before->end + 1 == s &&
529 life->start - 1 == s)
532 before->end = life->end;
533 if (!ir_value_life_remove(self, i))
534 return false; /* failing */
537 if (before->end + 1 == s)
543 /* already contained */
544 if (before->end >= s)
548 if (life->start - 1 == s)
553 /* insert a new entry */
554 new_entry.start = new_entry.end = s;
555 return ir_value_life_insert(self, i, new_entry);
558 /***********************************************************************
562 bool ir_block_create_store_op(ir_block *self, int op, ir_value *target, ir_value *what)
564 if (target->store == store_value) {
565 fprintf(stderr, "cannot store to an SSA value\n");
568 ir_instr *in = ir_instr_new(self, op);
571 if (!ir_instr_op(in, 0, target, true) ||
572 !ir_instr_op(in, 1, what, false) ||
573 !ir_block_instr_add(self, in) )
581 bool ir_block_create_store(ir_block *self, ir_value *target, ir_value *what)
585 if (target->vtype == qc_variant)
588 vtype = target->vtype;
593 if (what->vtype == qc_int)
594 op = INSTR_CONV_ITOF;
603 op = INSTR_STORE_ENT;
610 if (what->vtype == qc_int)
611 op = INSTR_CONV_FTOI;
620 op = INSTR_STORE_ENT;
624 return ir_block_create_store_op(self, op, target, what);
627 bool ir_block_create_return(ir_block *self, ir_value *v)
631 fprintf(stderr, "block already ended (%s)\n", self->label);
635 self->is_return = true;
636 in = ir_instr_new(self, INSTR_RETURN);
640 if (!ir_instr_op(in, 0, v, false) ||
641 !ir_block_instr_add(self, in) )
648 bool ir_block_create_if(ir_block *self, ir_value *v,
649 ir_block *ontrue, ir_block *onfalse)
653 fprintf(stderr, "block already ended (%s)\n", self->label);
657 //in = ir_instr_new(self, (v->vtype == qc_string ? INSTR_IF_S : INSTR_IF_F));
658 in = ir_instr_new(self, VINSTR_COND);
662 if (!ir_instr_op(in, 0, v, false)) {
667 in->bops[0] = ontrue;
668 in->bops[1] = onfalse;
670 if (!ir_block_instr_add(self, in))
673 if (!ir_block_exits_add(self, ontrue) ||
674 !ir_block_exits_add(self, onfalse) ||
675 !ir_block_entries_add(ontrue, self) ||
676 !ir_block_entries_add(onfalse, self) )
683 bool ir_block_create_jump(ir_block *self, ir_block *to)
687 fprintf(stderr, "block already ended (%s)\n", self->label);
691 in = ir_instr_new(self, VINSTR_JUMP);
696 if (!ir_block_instr_add(self, in))
699 if (!ir_block_exits_add(self, to) ||
700 !ir_block_entries_add(to, self) )
707 bool ir_block_create_goto(ir_block *self, ir_block *to)
711 fprintf(stderr, "block already ended (%s)\n", self->label);
715 in = ir_instr_new(self, INSTR_GOTO);
720 if (!ir_block_instr_add(self, in))
723 if (!ir_block_exits_add(self, to) ||
724 !ir_block_entries_add(to, self) )
731 ir_instr* ir_block_create_phi(ir_block *self, const char *label, int ot)
735 in = ir_instr_new(self, VINSTR_PHI);
738 out = ir_value_out(self->owner, label, store_local, ot);
743 if (!ir_instr_op(in, 0, out, true)) {
745 ir_value_delete(out);
748 if (!ir_block_instr_add(self, in)) {
750 ir_value_delete(out);
756 ir_value* ir_phi_value(ir_instr *self)
758 return self->_ops[0];
761 bool ir_phi_add(ir_instr* self, ir_block *b, ir_value *v)
765 if (!ir_block_entries_find(self->owner, b, NULL)) {
766 /* Must not be possible to cause this, otherwise the AST
767 * is doing something wrong.
769 fprintf(stderr, "Invalid entry block for PHI\n");
775 if (!ir_value_reads_add(v, self))
777 return ir_instr_phi_add(self, pe);
780 /* binary op related code */
782 ir_value* ir_block_create_binop(ir_block *self,
783 const char *label, int opcode,
784 ir_value *left, ir_value *right)
806 case INSTR_SUB_S: /* -- offset of string as float */
813 case INSTR_BITAND_FI:
814 case INSTR_BITAND_IF:
855 /* boolean operations result in floats */
856 if (opcode >= INSTR_EQ_F && opcode <= INSTR_GT)
858 else if (opcode >= INSTR_LE && opcode <= INSTR_GT)
861 else if (opcode >= INSTR_LE_I && opcode <= INSTR_EQ_FI)
867 /* The AST or parser were supposed to check this! */
871 ir_value *out = ir_value_out(self->owner, label, store_local, ot);
875 ir_instr *in = ir_instr_new(self, opcode);
877 ir_value_delete(out);
881 if (!ir_instr_op(in, 0, out, true) ||
882 !ir_instr_op(in, 1, left, false) ||
883 !ir_instr_op(in, 2, right, false) )
888 if (!ir_block_instr_add(self, in))
893 ir_value_delete(out);
898 ir_value* ir_block_create_add(ir_block *self,
900 ir_value *left, ir_value *right)
904 int r = right->vtype;
923 if ( (l == qc_float && r == qc_int) )
925 else if ( (l == qc_int && r == qc_float) )
931 return ir_block_create_binop(self, label, op, left, right);
934 ir_value* ir_block_create_sub(ir_block *self,
936 ir_value *left, ir_value *right)
940 int r = right->vtype;
960 if ( (l == qc_float && r == qc_int) )
962 else if ( (l == qc_int && r == qc_float) )
968 return ir_block_create_binop(self, label, op, left, right);
971 ir_value* ir_block_create_mul(ir_block *self,
973 ir_value *left, ir_value *right)
977 int r = right->vtype;
996 if ( (l == qc_vector && r == qc_float) )
998 else if ( (l == qc_float && r == qc_vector) )
1001 else if ( (l == qc_vector && r == qc_int) )
1003 else if ( (l == qc_int && r == qc_vector) )
1005 else if ( (l == qc_float && r == qc_int) )
1007 else if ( (l == qc_int && r == qc_float) )
1013 return ir_block_create_binop(self, label, op, left, right);
1016 ir_value* ir_block_create_div(ir_block *self,
1018 ir_value *left, ir_value *right)
1021 int l = left->vtype;
1022 int r = right->vtype;
1039 if ( (l == qc_vector && r == qc_float) )
1041 else if ( (l == qc_float && r == qc_int) )
1043 else if ( (l == qc_int && r == qc_float) )
1049 return ir_block_create_binop(self, label, op, left, right);
1052 /* PHI resolving breaks the SSA, and must thus be the last
1053 * step before life-range calculation.
1056 static bool ir_block_naive_phi(ir_block *self);
1057 bool ir_function_naive_phi(ir_function *self)
1061 for (i = 0; i < self->blocks_count; ++i)
1063 if (!ir_block_naive_phi(self->blocks[i]))
1069 static bool ir_naive_phi_emit_store(ir_block *block, size_t iid, ir_value *old, ir_value *what)
1074 /* create a store */
1075 if (!ir_block_create_store(block, old, what))
1078 /* we now move it up */
1079 instr = block->instr[block->instr_count-1];
1080 for (i = block->instr_count; i > iid; --i)
1081 block->instr[i] = block->instr[i-1];
1082 block->instr[i] = instr;
1087 static bool ir_block_naive_phi(ir_block *self)
1090 /* FIXME: optionally, create_phi can add the phis
1091 * to a list so we don't need to loop through blocks
1092 * - anyway: "don't optimize YET"
1094 for (i = 0; i < self->instr_count; ++i)
1096 ir_instr *instr = self->instr[i];
1097 if (instr->opcode != VINSTR_PHI)
1100 if (!ir_block_instr_remove(self, i))
1102 --i; /* NOTE: i+1 below */
1104 for (p = 0; p < instr->phi_count; ++p)
1106 ir_value *v = instr->phi[p].value;
1107 for (w = 0; w < v->writes_count; ++w) {
1110 if (!v->writes[w]->_ops[0])
1113 /* When the write was to a global, we have to emit a mov */
1114 old = v->writes[w]->_ops[0];
1116 /* The original instruction now writes to the PHI target local */
1117 if (v->writes[w]->_ops[0] == v)
1118 v->writes[w]->_ops[0] = instr->_ops[0];
1120 if (old->store != store_local)
1122 /* If it originally wrote to a global we need to store the value
1125 if (!ir_naive_phi_emit_store(self, i+1, old, v))
1127 if (i+1 < self->instr_count)
1128 instr = self->instr[i+1];
1131 /* In case I forget and access instr later, it'll be NULL
1132 * when it's a problem, to make sure we crash, rather than accessing
1138 /* If it didn't, we can replace all reads by the phi target now. */
1140 for (r = 0; r < old->reads_count; ++r)
1143 ir_instr *ri = old->reads[r];
1144 for (op = 0; op < ri->phi_count; ++op) {
1145 if (ri->phi[op].value == old)
1146 ri->phi[op].value = v;
1148 for (op = 0; op < 3; ++op) {
1149 if (ri->_ops[op] == old)
1156 ir_instr_delete(instr);
1161 /***********************************************************************
1162 *IR Temp allocation code
1163 * Propagating value life ranges by walking through the function backwards
1164 * until no more changes are made.
1165 * In theory this should happen once more than once for every nested loop
1167 * Though this implementation might run an additional time for if nests.
1176 MEM_VEC_FUNCTIONS_ALL(new_reads_t, ir_value*, v)
1178 /* Enumerate instructions used by value's life-ranges
1180 static void ir_block_enumerate(ir_block *self, size_t *_eid)
1184 for (i = 0; i < self->instr_count; ++i)
1186 self->instr[i]->eid = eid++;
1191 /* Enumerate blocks and instructions.
1192 * The block-enumeration is unordered!
1193 * We do not really use the block enumreation, however
1194 * the instruction enumeration is important for life-ranges.
1196 void ir_function_enumerate(ir_function *self)
1199 size_t instruction_id = 0;
1200 for (i = 0; i < self->blocks_count; ++i)
1202 self->blocks[i]->eid = i;
1203 self->blocks[i]->run_id = 0;
1204 ir_block_enumerate(self->blocks[i], &instruction_id);
1208 static bool ir_block_life_propagate(ir_block *b, ir_block *prev, bool *changed);
1209 bool ir_function_calculate_liferanges(ir_function *self)
1217 for (i = 0; i != self->blocks_count; ++i)
1219 if (self->blocks[i]->is_return)
1221 if (!ir_block_life_propagate(self->blocks[i], NULL, &changed))
1229 /* Get information about which operand
1230 * is read from, or written to.
1232 static void ir_op_read_write(int op, size_t *read, size_t *write)
1259 static bool ir_block_living_add_instr(ir_block *self, size_t eid)
1262 bool changed = false;
1264 for (i = 0; i != self->living_count; ++i)
1266 tempbool = ir_value_life_merge(self->living[i], eid);
1269 fprintf(stderr, "block_living_add_instr() value instruction added %s: %i\n", self->living[i]->_name, (int)eid);
1271 changed = changed || tempbool;
1276 static bool ir_block_life_prop_previous(ir_block* self, ir_block *prev, bool *changed)
1279 /* values which have been read in a previous iteration are now
1280 * in the "living" array even if the previous block doesn't use them.
1281 * So we have to remove whatever does not exist in the previous block.
1282 * They will be re-added on-read, but the liferange merge won't cause
1285 for (i = 0; i < self->living_count; ++i)
1287 if (!ir_block_living_find(prev, self->living[i], NULL)) {
1288 if (!ir_block_living_remove(self, i))
1294 /* Whatever the previous block still has in its living set
1295 * must now be added to ours as well.
1297 for (i = 0; i < prev->living_count; ++i)
1299 if (ir_block_living_find(self, prev->living[i], NULL))
1301 if (!ir_block_living_add(self, prev->living[i]))
1304 printf("%s got from prev: %s\n", self->label, prev->living[i]->_name);
1309 static bool ir_block_life_propagate(ir_block *self, ir_block *prev, bool *changed)
1315 /* bitmasks which operands are read from or written to */
1317 new_reads_t new_reads;
1318 char dbg_ind[16] = { '#', '0' };
1321 MEM_VECTOR_INIT(&new_reads, v);
1325 if (!ir_block_life_prop_previous(self, prev, changed))
1329 i = self->instr_count;
1332 instr = self->instr[i];
1334 /* PHI operands are always read operands */
1335 for (p = 0; p < instr->phi_count; ++p)
1337 value = instr->phi[p].value;
1338 /* used this before new_reads - puts the last read into the life range as well
1339 if (!ir_block_living_find(self, value, NULL))
1340 ir_block_living_add(self, value);
1342 /* fprintf(stderr, "read: %s\n", value->_name); */
1343 if (!new_reads_t_v_find(&new_reads, value, NULL))
1345 if (!new_reads_t_v_add(&new_reads, value))
1350 /* See which operands are read and write operands */
1351 ir_op_read_write(instr->opcode, &read, &write);
1353 /* Go through the 3 main operands */
1354 for (o = 0; o < 3; ++o)
1356 if (!instr->_ops[o]) /* no such operand */
1359 value = instr->_ops[o];
1361 /* We only care about locals */
1362 if (value->store != store_value &&
1363 value->store != store_local)
1369 /* used this before new_reads - puts the last read into the life range as well
1370 if (!ir_block_living_find(self, value, NULL))
1371 ir_block_living_add(self, value);
1373 /* fprintf(stderr, "read: %s\n", value->_name); */
1374 if (!new_reads_t_v_find(&new_reads, value, NULL))
1376 if (!new_reads_t_v_add(&new_reads, value))
1381 /* write operands */
1382 /* When we write to a local, we consider it "dead" for the
1383 * remaining upper part of the function, since in SSA a value
1384 * can only be written once (== created)
1388 size_t idx, readidx;
1389 bool in_living = ir_block_living_find(self, value, &idx);
1390 bool in_reads = new_reads_t_v_find(&new_reads, value, &readidx);
1391 if (!in_living && !in_reads)
1393 /* If the value isn't alive it hasn't been read before... */
1394 /* TODO: See if the warning can be emitted during parsing or AST processing
1395 * otherwise have warning printed here.
1396 * IF printing a warning here: include filecontext_t,
1397 * and make sure it's only printed once
1398 * since this function is run multiple times.
1400 /* For now: debug info: */
1401 fprintf(stderr, "Value only written %s\n", value->name);
1402 tempbool = ir_value_life_merge(value, instr->eid);
1403 *changed = *changed || tempbool;
1405 ir_instr_dump(instr, dbg_ind, printf);
1409 /* since 'living' won't contain it
1410 * anymore, merge the value, since
1413 tempbool = ir_value_life_merge(value, instr->eid);
1416 fprintf(stderr, "value added id %s %i\n", value->name, (int)instr->eid);
1418 *changed = *changed || tempbool;
1420 if (!ir_block_living_remove(self, idx))
1424 if (!new_reads_t_v_remove(&new_reads, readidx))
1431 tempbool = ir_block_living_add_instr(self, instr->eid);
1432 //fprintf(stderr, "living added values\n");
1433 *changed = *changed || tempbool;
1436 for (rd = 0; rd < new_reads.v_count; ++rd)
1438 if (!ir_block_living_find(self, new_reads.v[rd], NULL)) {
1439 if (!ir_block_living_add(self, new_reads.v[rd]))
1442 if (!i && !self->entries_count) {
1444 *changed = *changed || ir_value_life_merge(new_reads.v[rd], instr->eid);
1447 MEM_VECTOR_CLEAR(&new_reads, v);
1450 if (self->run_id == self->owner->run_id)
1452 self->run_id = self->owner->run_id;
1454 for (i = 0; i < self->entries_count; ++i)
1456 ir_block *entry = self->entries[i];
1457 ir_block_life_propagate(entry, self, changed);
1462 MEM_VECTOR_CLEAR(&new_reads, v);