+ * @param usage The node which uses the original node.
+ * @param pos The position of the argument which corresponds to the original node.
+ * @param copies A set containing all node which are copies from the original node.
+ * @param copy_blocks A set containing all basic block in which copies of the original node are located.
+ * @param phis A set where all created phis are recorded.
+ * @param phi_blocks A set of all blocks where Phis shall be inserted (iterated dominance frontier).
+ * @param mode The mode for the Phi if one has to be created.
+ * @return The valid copy for usage.
+ */
+static ir_node *search_def(ir_node *usage, int pos, pset *copies, pset *copy_blocks, pset *phis, pset *phi_blocks, ir_mode *mode)
+{
+ ir_node *curr_bl;
+ ir_node *start_irn;
+ FIRM_DBG_REGISTER(firm_dbg_module_t *dbg, DBG_MODULE);
+
+ curr_bl = get_nodes_block(usage);
+
+ DBG((dbg, LEVEL_1, "Searching valid def for use %+F at pos %d\n", usage, pos));
+ /*
+ * If the usage is in a phi node, search the copy in the
+ * predecessor denoted by pos.
+ */
+ if(is_Phi(usage)) {
+ curr_bl = get_Block_cfgpred_block(curr_bl, pos);
+ start_irn = sched_last(curr_bl);
+ } else {
+ start_irn = sched_prev(usage);
+ }
+
+ /*
+ * Traverse the dominance tree upwards from the
+ * predecessor block of the usage.
+ */
+ while(curr_bl != NULL) {
+
+ /*
+ * If this block contains a copy, search the block
+ * instruction by instruction.
+ */
+ if(pset_find_ptr(copy_blocks, curr_bl)) {
+ ir_node *irn;
+
+ /* Look at each instruction from last to first. */
+ sched_foreach_reverse_from(start_irn, irn) {
+
+ /* Take the first copy we find. */
+ if(pset_find_ptr(copies, irn))
+ return irn;
+ }
+ }
+
+ if(pset_find_ptr(phi_blocks, curr_bl)) {
+ ir_node *phi = get_irn_link(curr_bl);
+
+ if(!phi) {
+ int i, n_preds = get_irn_arity(curr_bl);
+ ir_graph *irg = get_irn_irg(curr_bl);
+ ir_node **ins = xmalloc(n_preds * sizeof(ins[0]));
+
+ for(i = 0; i < n_preds; ++i)
+ ins[i] = new_r_Bad(irg);
+
+ phi = new_r_Phi(irg, curr_bl, n_preds, ins, mode);
+ DBG((dbg, LEVEL_2, "\tcreating phi %+F in %+F\n", phi, curr_bl));
+
+ set_irn_link(curr_bl, phi);
+ sched_add_after(curr_bl, phi);
+ free(ins);
+
+ for(i = 0; i < n_preds; ++i) {
+ ir_node *arg = search_def(phi, i, copies, copy_blocks, phis, phi_blocks, mode);
+ DBG((dbg, LEVEL_2, "\t\t%+F(%d) -> %+F\n", phi, i, arg));
+ set_irn_n(phi, i, arg);
+ }
+
+ if(phis)
+ pset_insert_ptr(phis, phi);
+ }
+
+ return phi;
+ }
+
+ /* If were not done yet, look in the immediate dominator */
+ curr_bl = get_Block_idom(curr_bl);
+ if(curr_bl)
+ start_irn = sched_last(curr_bl);
+ }
+
+ return NULL;
+}
+
+static void fix_usages(pset *copies, pset *copy_blocks, pset *phi_blocks, pset *phis, pset *ignore_uses)
+{
+ int n_outs = 0;
+ FIRM_DBG_REGISTER(firm_dbg_module_t *dbg, DBG_MODULE);
+
+ struct obstack obst;
+ ir_node *irn;
+ int i;
+
+ struct out {
+ ir_node *irn;
+ int pos;
+ } *outs;
+
+ obstack_init(&obst);
+
+ /*
+ * Put all outs into an array.
+ * This is necessary, since the outs would be modified while
+ * iterating on them what could bring the outs module in trouble.
+ */
+ for(irn = pset_first(copies); irn; irn = pset_next(copies)) {
+ const ir_edge_t *edge;
+ foreach_out_edge(irn, edge) {
+ if(!pset_find_ptr(ignore_uses, get_edge_src_irn(edge))) {
+ struct out tmp;
+ tmp.irn = get_edge_src_irn(edge);
+ tmp.pos = get_edge_src_pos(edge);
+ obstack_grow(&obst, &tmp, sizeof(tmp));
+ n_outs++;
+ }
+ }
+ }
+ outs = obstack_finish(&obst);
+
+ /*
+ * Search the valid def for each out and set it.
+ */
+ for(i = 0; i < n_outs; ++i) {
+ ir_node *irn = outs[i].irn;
+ int pos = outs[i].pos;
+ ir_mode *mode = get_irn_mode(get_irn_n(irn, pos));
+
+ ir_node *def;
+
+ def = search_def(irn, pos, copies, copy_blocks, phis, phi_blocks, mode);
+ DBG((dbg, LEVEL_2, "\t%+F(%d) -> %+F\n", irn, pos, def));
+
+ if(def != NULL)
+ set_irn_n(irn, pos, def);
+ }
+
+ obstack_free(&obst, NULL);
+}
+
+/**
+ * Remove phis which are not necessary.
+ * During place_phi_functions() phi functions are put on the dominance
+ * frontiers blindly. However some of them will never be used (these
+ * have at least one predecessor which is NULL, see search_def() for
+ * this case). Since place_phi_functions() enters them into the
+ * schedule, we have to remove them from there.
+ *
+ * @param copies The set of all copies made (including the phi functions).