- int i;
- ir_node *orig_block = get_nodes_block(orig);
- ir_graph *irg = get_irn_irg(orig);
- ir_mode *mode = get_irn_mode(orig);
- pdeq *worklist = new_pdeq();
- pset *phi_blocks = pset_new_ptr(8);
- ir_node **ins = NULL;
- void *it;
- firm_dbg_module_t *dbg = DBG_MODULE;
-
- /*
- * Allocate an array for all blocks where the copies and the original
- * value were defined.
- */
- int n_orig_blocks = pset_count(copy_blocks) + 1;
- ir_node **orig_blocks = malloc(n_orig_blocks * sizeof(orig_blocks[0]));
-
- /*
- * Fill the array of definition blocks.
- */
- orig_blocks[0] = get_nodes_block(orig);
-
- /*
- * Fill the worklist queue and the rest of the orig blocks array.
- */
- for(it = pset_first(copies), i = 1; it; it = pset_next(copies)) {
- ir_node *copy_block = get_nodes_block(it);
-
- if(!block_dominates(orig_block, copy_block)) {
- assert(block_dominates(orig_block, copy_block)
- && "The block of the copy must be dominated by the block of the value");
- }
-
- pdeq_putr(worklist, it);
- orig_blocks[i++] = copy_block;
- }
-
- while(!pdeq_empty(worklist)) {
- ir_node *irn = pdeq_getl(worklist);
- ir_node *bl = get_nodes_block(irn);
- ir_node *y;
- int n_preds = get_irn_arity(bl);
- pset *df = be_get_dominance_frontier(df_info, bl);
-
- for(y = pset_first(df); y; y = pset_next(df)) {
-
- if(!pset_find_ptr(phi_blocks, y)) {
- ir_node *phi;
- int insert = 1;
-
- /*
- * Set the orig node as the only operand of the
- * phi node.
- */
- ins = realloc(ins, n_preds * sizeof(ins[0]));
- for(i = 0; i < n_preds; ++i)
- ins[0] = new_Unknown(mode);
-
- /* Insert phi node */
- phi = new_r_Phi(irg, bl, n_preds, ins, mode);
- DBG((dbg, LEVEL_2, " inserting phi in block %+F\n", bl));
-
- /*
- * The phi node itself is also a copy of the original
- * value. So put it in the copies set also, so that
- * the rename phase can treat them right.
- */
- pset_insert_ptr(copies, phi);
- pset_insert_ptr(copy_blocks, y);
-
- /* Insert the phi node into the schedule */
- sched_add_before(sched_first(y), phi);
-
- /* Insert the phi node in the phi blocks set. */
- pset_insert_ptr(phi_blocks, y);
-
- /*
- * If orig or a copy of it were not defined in y,
- * add y to the worklist.
- */
- for(i = 0; i < n_orig_blocks; ++i)
- if(orig_blocks[i] == y) {
- insert = 0;
- break;
- }
-
- if(insert)
- pdeq_putr(worklist, y);
-
- }
- }
- }
-
- del_pset(phi_blocks);
- del_pdeq(worklist);
+ ir_node *bl;
+ pdeq *worklist = new_pdeq();
+ FIRM_DBG_REGISTER(firm_dbg_module_t *dbg, DBG_MODULE);
+
+ /*
+ * Fill the worklist queue and the rest of the orig blocks array.
+ */
+ for(bl = pset_first(copy_blocks); bl; bl = pset_next(copy_blocks)) {
+ pdeq_putr(worklist, bl);
+ }
+
+ while(!pdeq_empty(worklist)) {
+ ir_node *bl = pdeq_getl(worklist);
+ pset *df = be_get_dominance_frontier(df_info, bl);
+
+ ir_node *y;
+
+ DBG((dbg, LEVEL_3, "dom front of %+F\n", bl));
+ for(y = pset_first(df); y; y = pset_next(df))
+ DBG((dbg, LEVEL_3, "\t%+F\n", y));
+
+ for(y = pset_first(df); y; y = pset_next(df)) {
+ if(!pset_find_ptr(phi_blocks, y)) {
+ pset_insert_ptr(phi_blocks, y);
+
+ /*
+ * Clear the link field of a possible phi block, since
+ * the possibly created phi will be stored there. See,
+ * search_def()
+ */
+ set_irn_link(y, NULL);
+
+ if(!pset_find_ptr(copy_blocks, y))
+ pdeq_putr(worklist, y);
+
+ }
+ }
+ }
+
+ del_pdeq(worklist);
+}