-static void adjust_arguments(be_chordal_env_t *chordal_env, const ir_node *phi) {
- int i, max;
- ir_node *arg, *perm, *proj;
- const arch_register_t *phi_reg, *arg_reg, *proj_reg;
- const ir_edge_t *edge;
- set *b2p;
- block2perm_t find, *found;
-
- assert(is_Phi(phi) && "Can only handle phi-destruction :)");
-
- b2p = chordal_env->data;
- phi_reg = get_reg(phi);
- /* all arguments of the phi */
- for(i=0, max=get_irn_arity(phi); i<max; ++i) {
- arg = get_irn_n(phi, i);
- arg_reg = get_reg(arg);
- /* if registers don't match ...*/
- if (phi_reg != arg_reg) {
- /* iff needed insert perm node */
- find.block = get_nodes_block(arg);
- find.perm = NULL;
- found = set_insert(b2p, &find, sizeof(find), HASH_PTR(find.block));
- if (!found->perm)
- found->perm = insert_perm(TODO);
-
- /* now we have the perm in the predecessor block */
- perm = found->perm;
- /* adjust assigned registers for the projs */
- foreach_out_edge(perm, edge) {
- proj = get_edge_src_irn(edge);
- proj_reg = get_reg(proj);
- if (proj_reg == arg_reg)
- set_reg(proj, phi_reg);
- else if (proj_reg == phi_reg)
- set_reg(proj, arg_reg);
+typedef struct {
+ ir_node *arg; /**< The phi argument to make the Proj for. */
+ int pos; /**< The proj number the Proj will get.
+ This also denotes the position of @p arg
+ in the in array of the Perm. */
+ ir_node *proj; /**< The proj created for @p arg. */
+} perm_proj_t;
+
+static int cmp_perm_proj(const void *a, const void *b, size_t n) {
+ const perm_proj_t *p = a;
+ const perm_proj_t *q = b;
+ return !(p->arg == q->arg);
+}
+
+typedef struct insert_all_perms_env_t {
+ be_chordal_env_t *chordal_env;
+ pmap *perm_map;
+} insert_all_perms_env_t;
+
+/**
+ * Insert Perms in all predecessors of a block containing a phi
+ */
+static void insert_all_perms_walker(ir_node *bl, void *data) {
+ insert_all_perms_env_t *env = data;
+ be_chordal_env_t *chordal_env = env->chordal_env;
+ pmap *perm_map = env->perm_map;
+ ir_graph *irg = chordal_env->irg;
+ be_lv_t *lv = chordal_env->birg->lv;
+ int i, n;
+
+ assert(is_Block(bl));
+
+ /* If the link flag is NULL, this block has no phis. */
+ if(!get_irn_link(bl))
+ return;
+
+ /* Look at all predecessors of the phi block */
+ for(i = 0, n = get_irn_arity(bl); i < n; ++i) {
+ ir_node *phi, *perm, *insert_after, **in;
+ perm_proj_t *pp;
+ set *arg_set = new_set(cmp_perm_proj, chordal_env->cls->n_regs);
+ ir_node *pred_bl = get_Block_cfgpred_block(bl, i);
+ int n_projs = 0;
+
+ assert(!pmap_contains(perm_map, pred_bl) && "Already permed that block");
+
+ /*
+ * Note that all phis in the list are in the same
+ * register class by construction.
+ */
+ for(phi = get_irn_link(bl); phi; phi = get_irn_link(phi)) {
+ perm_proj_t templ;
+ ir_node *arg = get_irn_n(phi, i);
+ unsigned hash = nodeset_hash(arg);
+
+ templ.arg = arg;
+ pp = set_find(arg_set, &templ, sizeof(templ), hash);
+
+ /*
+ * If a proj_perm_t entry has not been made in the argument set,
+ * create one. The only restriction is, that the phi argument
+ * may not be live in at the current block, since this argument
+ * interferes with the phi and must thus not be member of a
+ * Perm. A copy will be inserted for this argument alter on.
+ */
+ if(!pp && !be_is_live_in(lv, bl, arg)) {
+ templ.pos = n_projs++;
+ set_insert(arg_set, &templ, sizeof(templ), hash);
+ }
+ }
+
+
+ if (n_projs) {
+ /*
+ * Create a new Perm with the arguments just collected
+ * above in the arg_set and insert it into the schedule.
+ */
+ in = xmalloc(n_projs * sizeof(in[0]));
+ for(pp = set_first(arg_set); pp; pp = set_next(arg_set))
+ in[pp->pos] = pp->arg;
+
+ perm = be_new_Perm(chordal_env->cls, irg, pred_bl, n_projs, in);
+ be_stat_ev("phi_perm", n_projs);
+
+ free(in);
+ insert_after = sched_skip(sched_last(pred_bl), 0, sched_skip_cf_predicator, chordal_env->birg->main_env->arch_env);
+ sched_add_after(insert_after, perm);
+
+ /*
+ * Make the Projs for the Perm and insert into schedule.
+ * Register allocation is copied from the former phi
+ * arguments to the projs (new phi arguments).
+ */
+ insert_after = perm;
+ for(pp = set_first(arg_set); pp; pp = set_next(arg_set)) {
+ ir_node *proj = new_r_Proj(irg, pred_bl, perm, get_irn_mode(pp->arg), pp->pos);
+ pp->proj = proj;
+ assert(get_reg(pp->arg));
+ set_reg(proj, get_reg(pp->arg));
+ sched_add_after(insert_after, proj);
+ insert_after = proj;
+ DBG((dbg, LEVEL_2, "Copy register assignment %s from %+F to %+F\n", get_reg(pp->arg)->name, pp->arg, pp->proj));