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 ir_builder_set_name(self, modulename);
42 /* globals which always exist */
44 /* for now we give it a vector size */
45 ir_builder_create_global(self, "OFS_RETURN", qc_variant);
50 MEM_VEC_FUNCTIONS(ir_builder, ir_value*, globals)
51 MEM_VEC_FUNCTIONS(ir_builder, ir_function*, functions)
53 void ir_builder_delete(ir_builder* self)
56 mem_d((void*)self->name);
57 for (i = 0; i != self->functions_count; ++i) {
58 ir_function_delete(self->functions[i]);
60 MEM_VECTOR_CLEAR(self, functions);
61 for (i = 0; i != self->globals_count; ++i) {
62 ir_value_delete(self->globals[i]);
64 MEM_VECTOR_CLEAR(self, globals);
68 void ir_builder_set_name(ir_builder *self, const char *name)
71 mem_d((void*)self->name);
72 self->name = util_strdup(name);
75 ir_function* ir_builder_get_function(ir_builder *self, const char *name)
78 for (i = 0; i < self->functions_count; ++i) {
79 if (!strcmp(name, self->functions[i]->name))
80 return self->functions[i];
85 ir_function* ir_builder_create_function(ir_builder *self, const char *name)
87 ir_function *fn = ir_builder_get_function(self, name);
92 fn = ir_function_new(self);
93 ir_function_set_name(fn, name);
94 ir_builder_functions_add(self, fn);
98 ir_value* ir_builder_get_global(ir_builder *self, const char *name)
101 for (i = 0; i < self->globals_count; ++i) {
102 if (!strcmp(self->globals[i]->name, name))
103 return self->globals[i];
108 ir_value* ir_builder_create_global(ir_builder *self, const char *name, int vtype)
110 ir_value *ve = ir_builder_get_global(self, name);
115 ve = ir_value_var(name, store_global, vtype);
116 ir_builder_globals_add(self, ve);
120 /***********************************************************************
124 void ir_function_naive_phi(ir_function*);
125 void ir_function_enumerate(ir_function*);
126 void ir_function_calculate_liferanges(ir_function*);
128 ir_function* ir_function_new(ir_builder* owner)
131 self = (ir_function*)mem_a(sizeof(*self));
133 self->context.file = "<@no context>";
134 self->context.line = 0;
135 self->retype = qc_void;
136 MEM_VECTOR_INIT(self, params);
137 MEM_VECTOR_INIT(self, blocks);
138 MEM_VECTOR_INIT(self, values);
139 MEM_VECTOR_INIT(self, locals);
140 ir_function_set_name(self, "<@unnamed>");
145 MEM_VEC_FUNCTIONS(ir_function, ir_value*, values)
146 MEM_VEC_FUNCTIONS(ir_function, ir_block*, blocks)
147 MEM_VEC_FUNCTIONS(ir_function, ir_value*, locals)
149 void ir_function_set_name(ir_function *self, const char *name)
152 mem_d((void*)self->name);
153 self->name = util_strdup(name);
156 void ir_function_delete(ir_function *self)
159 mem_d((void*)self->name);
161 for (i = 0; i != self->blocks_count; ++i)
162 ir_block_delete(self->blocks[i]);
163 MEM_VECTOR_CLEAR(self, blocks);
165 MEM_VECTOR_CLEAR(self, params);
167 for (i = 0; i != self->values_count; ++i)
168 ir_value_delete(self->values[i]);
169 MEM_VECTOR_CLEAR(self, values);
171 for (i = 0; i != self->locals_count; ++i)
172 ir_value_delete(self->locals[i]);
173 MEM_VECTOR_CLEAR(self, locals);
178 void ir_function_collect_value(ir_function *self, ir_value *v)
180 ir_function_values_add(self, v);
183 ir_block* ir_function_create_block(ir_function *self, const char *label)
185 ir_block* bn = ir_block_new(self, label);
186 memcpy(&bn->context, &self->context, sizeof(self->context));
187 ir_function_blocks_add(self, bn);
191 void ir_function_finalize(ir_function *self)
193 ir_function_naive_phi(self);
194 ir_function_enumerate(self);
195 ir_function_calculate_liferanges(self);
198 ir_value* ir_function_get_local(ir_function *self, const char *name)
201 for (i = 0; i < self->locals_count; ++i) {
202 if (!strcmp(self->locals[i]->name, name))
203 return self->locals[i];
208 ir_value* ir_function_create_local(ir_function *self, const char *name, int vtype)
210 ir_value *ve = ir_function_get_local(self, name);
215 ve = ir_value_var(name, store_local, vtype);
216 ir_function_locals_add(self, ve);
220 /***********************************************************************
224 ir_block* ir_block_new(ir_function* owner, const char *name)
227 self = (ir_block*)mem_a(sizeof(*self));
229 self->context.file = "<@no context>";
230 self->context.line = 0;
232 MEM_VECTOR_INIT(self, instr);
233 MEM_VECTOR_INIT(self, entries);
234 MEM_VECTOR_INIT(self, exits);
236 ir_block_set_label(self, name);
239 self->is_return = false;
241 MEM_VECTOR_INIT(self, living);
244 MEM_VEC_FUNCTIONS(ir_block, ir_instr*, instr)
245 MEM_VEC_FUNCTIONS_ALL(ir_block, ir_block*, entries)
246 MEM_VEC_FUNCTIONS_ALL(ir_block, ir_block*, exits)
247 MEM_VEC_FUNCTIONS_ALL(ir_block, ir_value*, living)
249 void ir_block_delete(ir_block* self)
252 mem_d((void*)self->label);
253 for (i = 0; i != self->instr_count; ++i)
254 ir_instr_delete(self->instr[i]);
255 MEM_VECTOR_CLEAR(self, instr);
256 MEM_VECTOR_CLEAR(self, entries);
257 MEM_VECTOR_CLEAR(self, exits);
258 MEM_VECTOR_CLEAR(self, living);
262 void ir_block_set_label(ir_block *self, const char *name)
265 mem_d((void*)self->label);
266 self->label = util_strdup(name);
269 /***********************************************************************
273 ir_instr* ir_instr_new(ir_block* owner, int op)
276 self = (ir_instr*)mem_a(sizeof(*self));
278 self->context.file = "<@no context>";
279 self->context.line = 0;
281 self->_ops[0] = NULL;
282 self->_ops[1] = NULL;
283 self->_ops[2] = NULL;
284 self->bops[0] = NULL;
285 self->bops[1] = NULL;
286 MEM_VECTOR_INIT(self, phi);
291 MEM_VEC_FUNCTIONS(ir_instr, ir_phi_entry_t, phi)
293 void ir_instr_delete(ir_instr *self)
295 ir_instr_op(self, 0, NULL, false);
296 ir_instr_op(self, 1, NULL, false);
297 ir_instr_op(self, 2, NULL, false);
298 MEM_VECTOR_CLEAR(self, phi);
302 void ir_instr_op(ir_instr *self, int op, ir_value *v, qbool writing)
304 if (self->_ops[op]) {
306 ir_value_writes_add(self->_ops[op], self);
308 ir_value_reads_add(self->_ops[op], self);
312 ir_value_writes_add(v, self);
314 ir_value_reads_add(v, self);
319 /***********************************************************************
323 ir_value* ir_value_var(const char *name, int storetype, int vtype)
326 self = (ir_value*)mem_a(sizeof(*self));
328 self->store = storetype;
329 MEM_VECTOR_INIT(self, reads);
330 MEM_VECTOR_INIT(self, writes);
331 self->isconst = false;
332 self->context.file = "<@no context>";
333 self->context.line = 0;
335 ir_value_set_name(self, name);
337 MEM_VECTOR_INIT(self, life);
340 MEM_VEC_FUNCTIONS(ir_value, ir_life_entry_t, life)
341 MEM_VEC_FUNCTIONS(ir_value, ir_instr*, reads)
342 MEM_VEC_FUNCTIONS(ir_value, ir_instr*, writes)
344 ir_value* ir_value_out(ir_function *owner, const char *name, int storetype, int vtype)
346 ir_value *v = ir_value_var(name, storetype, vtype);
347 ir_function_collect_value(owner, v);
351 void ir_value_delete(ir_value* self)
353 mem_d((void*)self->name);
356 if (self->vtype == qc_string)
357 mem_d((void*)self->constval.vstring);
359 MEM_VECTOR_CLEAR(self, reads);
360 MEM_VECTOR_CLEAR(self, writes);
361 MEM_VECTOR_CLEAR(self, life);
365 void ir_value_set_name(ir_value *self, const char *name)
368 mem_d((void*)self->name);
369 self->name = util_strdup(name);
372 qbool ir_value_set_float(ir_value *self, float f)
374 if (self->vtype != qc_float)
376 self->constval.vfloat = f;
377 self->isconst = true;
381 qbool ir_value_set_vector(ir_value *self, vector_t v)
383 if (self->vtype != qc_vector)
385 self->constval.vvec = v;
386 self->isconst = true;
390 qbool ir_value_set_string(ir_value *self, const char *str)
392 if (self->vtype != qc_string)
394 self->constval.vstring = util_strdup(str);
395 self->isconst = true;
399 qbool ir_value_set_int(ir_value *self, int i)
401 if (self->vtype != qc_int)
403 self->constval.vint = i;
404 self->isconst = true;
408 qbool ir_value_lives(ir_value *self, size_t at)
411 for (i = 0; i < self->life_count; ++i)
413 ir_life_entry_t *life = &self->life[i];
414 if (life->start <= at && at <= life->end)
416 if (life->start > at) /* since it's ordered */
422 void ir_value_life_insert(ir_value *self, size_t idx, ir_life_entry_t e)
425 ir_value_life_add(self, e); /* naive... */
426 for (k = self->life_count-1; k > idx; --k)
427 self->life[k] = self->life[k-1];
431 qbool ir_value_life_merge(ir_value *self, size_t s)
434 ir_life_entry_t *life = NULL;
435 ir_life_entry_t *before = NULL;
436 ir_life_entry_t new_entry;
438 /* Find the first range >= s */
439 for (i = 0; i < self->life_count; ++i)
442 life = &self->life[i];
446 /* nothing found? append */
447 if (i == self->life_count) {
448 if (life && life->end+1 == s)
450 /* previous life range can be merged in */
454 if (life && life->end >= s)
458 ir_value_life_add(self, e);
464 if (before->end + 1 == s &&
465 life->start - 1 == s)
468 before->end = life->end;
469 ir_value_life_remove(self, i);
472 if (before->end + 1 == s)
478 /* already contained */
479 if (before->end >= s)
483 if (life->start - 1 == s)
488 /* insert a new entry */
489 new_entry.start = new_entry.end = s;
490 ir_value_life_insert(self, i, new_entry);
494 /***********************************************************************
498 qbool ir_block_create_store_op(ir_block *self, int op, ir_value *target, ir_value *what)
500 if (target->store == store_value) {
501 fprintf(stderr, "cannot store to an SSA value\n");
504 ir_instr *in = ir_instr_new(self, op);
505 ir_instr_op(in, 0, target, true);
506 ir_instr_op(in, 1, what, false);
507 ir_block_instr_add(self, in);
512 qbool ir_block_create_store(ir_block *self, ir_value *target, ir_value *what)
516 if (target->vtype == qc_variant)
519 vtype = target->vtype;
524 if (what->vtype == qc_int)
525 op = INSTR_CONV_ITOF;
534 op = INSTR_STORE_ENT;
541 if (what->vtype == qc_int)
542 op = INSTR_CONV_FTOI;
551 op = INSTR_STORE_ENT;
555 return ir_block_create_store_op(self, op, target, what);
558 void ir_block_create_return(ir_block *self, ir_value *v)
562 fprintf(stderr, "block already ended (%s)\n", self->label);
566 self->is_return = true;
567 in = ir_instr_new(self, INSTR_RETURN);
568 ir_instr_op(in, 0, v, false);
569 ir_block_instr_add(self, in);
572 void ir_block_create_if(ir_block *self, ir_value *v,
573 ir_block *ontrue, ir_block *onfalse)
577 fprintf(stderr, "block already ended (%s)\n", self->label);
581 //in = ir_instr_new(self, (v->vtype == qc_string ? INSTR_IF_S : INSTR_IF_F));
582 in = ir_instr_new(self, VINSTR_COND);
583 ir_instr_op(in, 0, v, false);
584 in->bops[0] = ontrue;
585 in->bops[1] = onfalse;
586 ir_block_instr_add(self, in);
588 ir_block_exits_add(self, ontrue);
589 ir_block_exits_add(self, onfalse);
590 ir_block_entries_add(ontrue, self);
591 ir_block_entries_add(onfalse, self);
594 void ir_block_create_jump(ir_block *self, ir_block *to)
598 fprintf(stderr, "block already ended (%s)\n", self->label);
602 in = ir_instr_new(self, VINSTR_JUMP);
604 ir_block_instr_add(self, in);
606 ir_block_exits_add(self, to);
607 ir_block_entries_add(to, self);
610 void ir_block_create_goto(ir_block *self, ir_block *to)
614 fprintf(stderr, "block already ended (%s)\n", self->label);
618 in = ir_instr_new(self, INSTR_GOTO);
620 ir_block_instr_add(self, in);
622 ir_block_exits_add(self, to);
623 ir_block_entries_add(to, self);
626 ir_instr* ir_block_create_phi(ir_block *self, const char *label, int ot)
630 in = ir_instr_new(self, VINSTR_PHI);
631 out = ir_value_out(self->owner, label, store_local, ot);
632 ir_instr_op(in, 0, out, true);
633 ir_block_instr_add(self, in);
637 ir_value* ir_phi_value(ir_instr *self)
639 return self->_ops[0];
642 void ir_phi_add(ir_instr* self, ir_block *b, ir_value *v)
646 if (!ir_block_entries_find(self->owner, b, NULL)) {
647 /* Must not be possible to cause this, otherwise the AST
648 * is doing something wrong.
650 fprintf(stderr, "Invalid entry block for PHI\n");
656 ir_value_reads_add(v, self);
657 ir_instr_phi_add(self, pe);
660 /* binary op related code */
662 ir_value* ir_block_create_binop(ir_block *self,
663 const char *label, int opcode,
664 ir_value *left, ir_value *right)
686 case INSTR_SUB_S: /* -- offset of string as float */
693 case INSTR_BITAND_FI:
694 case INSTR_BITAND_IF:
735 /* boolean operations result in floats */
736 if (opcode >= INSTR_EQ_F && opcode <= INSTR_GT)
738 else if (opcode >= INSTR_LE && opcode <= INSTR_GT)
741 else if (opcode >= INSTR_LE_I && opcode <= INSTR_EQ_FI)
747 /* The AST or parser were supposed to check this! */
751 ir_value *out = ir_value_out(self->owner, label, store_local, ot);
752 ir_instr *in = ir_instr_new(self, opcode);
753 ir_instr_op(in, 0, out, true);
754 ir_instr_op(in, 1, left, false);
755 ir_instr_op(in, 2, right, false);
756 ir_block_instr_add(self, in);
760 ir_value* ir_block_create_add(ir_block *self,
762 ir_value *left, ir_value *right)
766 int r = right->vtype;
785 if ( (l == qc_float && r == qc_int) )
787 else if ( (l == qc_int && r == qc_float) )
793 return ir_block_create_binop(self, label, op, left, right);
796 ir_value* ir_block_create_sub(ir_block *self,
798 ir_value *left, ir_value *right)
802 int r = right->vtype;
822 if ( (l == qc_float && r == qc_int) )
824 else if ( (l == qc_int && r == qc_float) )
830 return ir_block_create_binop(self, label, op, left, right);
833 ir_value* ir_block_create_mul(ir_block *self,
835 ir_value *left, ir_value *right)
839 int r = right->vtype;
858 if ( (l == qc_vector && r == qc_float) )
860 else if ( (l == qc_float && r == qc_vector) )
863 else if ( (l == qc_vector && r == qc_int) )
865 else if ( (l == qc_int && r == qc_vector) )
867 else if ( (l == qc_float && r == qc_int) )
869 else if ( (l == qc_int && r == qc_float) )
875 return ir_block_create_binop(self, label, op, left, right);
878 ir_value* ir_block_create_div(ir_block *self,
880 ir_value *left, ir_value *right)
884 int r = right->vtype;
901 if ( (l == qc_vector && r == qc_float) )
903 else if ( (l == qc_float && r == qc_int) )
905 else if ( (l == qc_int && r == qc_float) )
911 return ir_block_create_binop(self, label, op, left, right);
914 /* PHI resolving breaks the SSA, and must thus be the last
915 * step before life-range calculation.
918 static void ir_block_naive_phi(ir_block *self);
919 void ir_function_naive_phi(ir_function *self)
923 for (i = 0; i < self->blocks_count; ++i)
924 ir_block_naive_phi(self->blocks[i]);
927 static void ir_naive_phi_emit_store(ir_block *block, size_t iid, ir_value *old, ir_value *what)
933 ir_block_create_store(block, old, what);
935 /* we now move it up */
936 instr = block->instr[block->instr_count-1];
937 for (i = block->instr_count; i > iid; --i)
938 block->instr[i] = block->instr[i-1];
939 block->instr[i] = instr;
942 static void ir_block_naive_phi(ir_block *self)
945 /* FIXME: optionally, create_phi can add the phis
946 * to a list so we don't need to loop through blocks
947 * - anyway: "don't optimize YET"
949 for (i = 0; i < self->instr_count; ++i)
951 ir_instr *instr = self->instr[i];
952 if (instr->opcode != VINSTR_PHI)
955 ir_block_instr_remove(self, i);
956 --i; /* NOTE: i+1 below */
958 for (p = 0; p < instr->phi_count; ++p)
960 ir_value *v = instr->phi[p].value;
961 for (w = 0; w < v->writes_count; ++w) {
964 if (!v->writes[w]->_ops[0])
967 /* When the write was to a global, we have to emit a mov */
968 old = v->writes[w]->_ops[0];
970 /* The original instruction now writes to the PHI target local */
971 if (v->writes[w]->_ops[0] == v)
972 v->writes[w]->_ops[0] = instr->_ops[0];
974 if (old->store != store_local)
976 /* If it originally wrote to a global we need to store the value
979 ir_naive_phi_emit_store(self, i+1, old, v);
980 if (i+1 < self->instr_count)
981 instr = self->instr[i+1];
984 /* In case I forget and access instr later, it'll be NULL
985 * when it's a problem, to make sure we crash, rather than accessing
991 /* If it didn't, we can replace all reads by the phi target now. */
993 for (r = 0; r < old->reads_count; ++r)
996 ir_instr *ri = old->reads[r];
997 for (op = 0; op < ri->phi_count; ++op) {
998 if (ri->phi[op].value == old)
999 ri->phi[op].value = v;
1001 for (op = 0; op < 3; ++op) {
1002 if (ri->_ops[op] == old)
1009 ir_instr_delete(instr);
1013 /***********************************************************************
1014 *IR Temp allocation code
1015 * Propagating value life ranges by walking through the function backwards
1016 * until no more changes are made.
1017 * In theory this should happen once more than once for every nested loop
1019 * Though this implementation might run an additional time for if nests.
1028 MEM_VEC_FUNCTIONS_ALL(new_reads_t, ir_value*, v)
1030 /* Enumerate instructions used by value's life-ranges
1032 static void ir_block_enumerate(ir_block *self, size_t *_eid)
1036 for (i = 0; i < self->instr_count; ++i)
1038 self->instr[i]->eid = eid++;
1043 /* Enumerate blocks and instructions.
1044 * The block-enumeration is unordered!
1045 * We do not really use the block enumreation, however
1046 * the instruction enumeration is important for life-ranges.
1048 void ir_function_enumerate(ir_function *self)
1051 size_t instruction_id = 0;
1052 for (i = 0; i < self->blocks_count; ++i)
1054 self->blocks[i]->eid = i;
1055 self->blocks[i]->run_id = 0;
1056 ir_block_enumerate(self->blocks[i], &instruction_id);
1060 static void ir_block_life_propagate(ir_block *b, ir_block *prev, qbool *changed);
1061 void ir_function_calculate_liferanges(ir_function *self)
1069 for (i = 0; i != self->blocks_count; ++i)
1071 if (self->blocks[i]->is_return)
1072 ir_block_life_propagate(self->blocks[i], NULL, &changed);
1077 /* Get information about which operand
1078 * is read from, or written to.
1080 static void ir_op_read_write(int op, size_t *read, size_t *write)
1107 static qbool ir_block_living_add_instr(ir_block *self, size_t eid)
1110 qbool changed = false;
1112 for (i = 0; i != self->living_count; ++i)
1114 tempbool = ir_value_life_merge(self->living[i], eid);
1117 fprintf(stderr, "block_living_add_instr() value instruction added %s: %i\n", self->living[i]->_name, (int)eid);
1119 changed = changed || tempbool;
1124 static void ir_block_life_prop_previous(ir_block* self, ir_block *prev, qbool *changed)
1127 /* values which have been read in a previous iteration are now
1128 * in the "living" array even if the previous block doesn't use them.
1129 * So we have to remove whatever does not exist in the previous block.
1130 * They will be re-added on-read, but the liferange merge won't cause
1133 for (i = 0; i < self->living_count; ++i)
1135 if (!ir_block_living_find(prev, self->living[i], NULL)) {
1136 ir_block_living_remove(self, i);
1141 /* Whatever the previous block still has in its living set
1142 * must now be added to ours as well.
1144 for (i = 0; i < prev->living_count; ++i)
1146 if (ir_block_living_find(self, prev->living[i], NULL))
1148 ir_block_living_add(self, prev->living[i]);
1150 printf("%s got from prev: %s\n", self->label, prev->living[i]->_name);
1155 static void ir_block_life_propagate(ir_block *self, ir_block *prev, qbool *changed)
1161 /* bitmasks which operands are read from or written to */
1163 new_reads_t new_reads;
1164 char dbg_ind[16] = { '#', '0' };
1167 MEM_VECTOR_INIT(&new_reads, v);
1170 ir_block_life_prop_previous(self, prev, changed);
1172 i = self->instr_count;
1175 instr = self->instr[i];
1177 /* PHI operands are always read operands */
1178 for (p = 0; p < instr->phi_count; ++p)
1180 value = instr->phi[p].value;
1181 /* used this before new_reads - puts the last read into the life range as well
1182 if (!ir_block_living_find(self, value, NULL))
1183 ir_block_living_add(self, value);
1185 /* fprintf(stderr, "read: %s\n", value->_name); */
1186 if (!new_reads_t_v_find(&new_reads, value, NULL))
1187 new_reads_t_v_add(&new_reads, value);
1190 /* See which operands are read and write operands */
1191 ir_op_read_write(instr->opcode, &read, &write);
1193 /* Go through the 3 main operands */
1194 for (o = 0; o < 3; ++o)
1196 if (!instr->_ops[o]) /* no such operand */
1199 value = instr->_ops[o];
1201 /* We only care about locals */
1202 if (value->store != store_value &&
1203 value->store != store_local)
1209 /* used this before new_reads - puts the last read into the life range as well
1210 if (!ir_block_living_find(self, value, NULL))
1211 ir_block_living_add(self, value);
1213 /* fprintf(stderr, "read: %s\n", value->_name); */
1214 if (!new_reads_t_v_find(&new_reads, value, NULL))
1215 new_reads_t_v_add(&new_reads, value);
1218 /* write operands */
1219 /* When we write to a local, we consider it "dead" for the
1220 * remaining upper part of the function, since in SSA a value
1221 * can only be written once (== created)
1225 size_t idx, readidx;
1226 qbool in_living = ir_block_living_find(self, value, &idx);
1227 qbool in_reads = new_reads_t_v_find(&new_reads, value, &readidx);
1228 if (!in_living && !in_reads)
1230 /* If the value isn't alive it hasn't been read before... */
1231 /* TODO: See if the warning can be emitted during parsing or AST processing
1232 * otherwise have warning printed here.
1233 * IF printing a warning here: include filecontext_t,
1234 * and make sure it's only printed once
1235 * since this function is run multiple times.
1237 /* For now: debug info: */
1238 fprintf(stderr, "Value only written %s\n", value->name);
1239 tempbool = ir_value_life_merge(value, instr->eid);
1240 *changed = *changed || tempbool;
1242 ir_instr_dump(instr, dbg_ind, printf);
1246 /* since 'living' won't contain it
1247 * anymore, merge the value, since
1250 tempbool = ir_value_life_merge(value, instr->eid);
1253 fprintf(stderr, "value added id %s %i\n", value->name, (int)instr->eid);
1255 *changed = *changed || tempbool;
1257 ir_block_living_remove(self, idx);
1259 new_reads_t_v_remove(&new_reads, readidx);
1264 tempbool = ir_block_living_add_instr(self, instr->eid);
1265 //fprintf(stderr, "living added values\n");
1266 *changed = *changed || tempbool;
1269 for (rd = 0; rd < new_reads.v_count; ++rd)
1271 if (!ir_block_living_find(self, new_reads.v[rd], NULL)) {
1272 ir_block_living_add(self, new_reads.v[rd]);
1274 if (!i && !self->entries_count) {
1276 *changed = *changed || ir_value_life_merge(new_reads.v[rd], instr->eid);
1279 new_reads_t_v_clear(&new_reads);
1282 if (self->run_id == self->owner->run_id)
1284 self->run_id = self->owner->run_id;
1286 for (i = 0; i < self->entries_count; ++i)
1288 ir_block *entry = self->entries[i];
1289 ir_block_life_propagate(entry, self, changed);