-#define is_Branch(irn) (arch_irn_classify(arch_env, irn) == arch_irn_class_branch)
-#define is_Perm(irn) (arch_irn_classify(arch_env, irn) == arch_irn_class_perm)
-#define get_reg_cls(irn) (arch_get_irn_reg_class(arch_env, irn, arch_pos_make_out(0)))
-#define is_curr_reg_class(irn) (get_reg_cls(p) == chordal_env->cls)
-
-static ir_node *get_or_insert_perm(be_main_session_env_t *session, be_chordal_env_t *chordal_env, ir_node *block) {
- block2perm_t find, *found;
- ir_node *p;
- set *b2p = chordal_env->data;
- const arch_env_t *arch_env = chordal_env->arch_env;
-
- /* iff needed insert perm node */
-
- /* .if the perm is in the pset return it */
- find.block = block;
- find.perm = NULL;
- found = set_insert(b2p, &find, sizeof(find), HASH_PTR(find.block));
- if (found->perm)
- return found->perm;
-
- /* .else look for a perm of right register class in the schedule */
- p = sched_last(find.block);
- while (!is_Block(p) && (is_Branch(p) || (is_Perm(p) && !is_curr_reg_class(p))))
- p = sched_prev(p);
-
- /* if we haven't found a perm of the right register class create a new one */
- if (! (is_Perm(p) && is_curr_reg_class(p)))
- p = insert_Perm_after(session, chordal_env->cls, p);
-
- /* insert perm into pset */
- found->perm = p;
- return p;
+/**
+ * Insert Perms in all predecessors of a block containing a phi
+ */
+static void insert_all_perms_walker(ir_node *bl, void *data)
+{
+ be_chordal_env_t *const chordal_env = (be_chordal_env_t*)data;
+ be_lv_t *lv = be_get_irg_liveness(chordal_env->irg);
+ 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;
+ 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;
+
+ /*
+ * Note that all phis in the list are in the same
+ * register class by construction.
+ */
+ for (phi = (ir_node*)get_irn_link(bl); phi != NULL;
+ phi = (ir_node*)get_irn_link(phi)) {
+ ir_node *arg = get_irn_n(phi, i);
+ unsigned hash;
+ perm_proj_t templ;
+
+ hash = hash_irn(arg);
+ templ.arg = arg;
+ perm_proj_t *const pp = set_find(perm_proj_t, 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 later on.
+ */
+ if (!pp && !be_is_live_in(lv, bl, arg)) {
+ templ.pos = n_projs++;
+ (void)set_insert(perm_proj_t, 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 = XMALLOCN(ir_node*, n_projs);
+ foreach_set(arg_set, perm_proj_t, pp) {
+ in[pp->pos] = pp->arg;
+ }
+
+ perm = be_new_Perm(chordal_env->cls, pred_bl, n_projs, in);
+ stat_ev_int("phi_perm", n_projs);
+
+ insert_after = pred_bl;
+ do {
+ insert_after = sched_prev(insert_after);
+ } while (is_cfop(insert_after));
+ 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).
+ */
+ foreach_set(arg_set, perm_proj_t, pp) {
+ ir_node *proj = new_r_Proj(perm, get_irn_mode(pp->arg), pp->pos);
+ pp->proj = proj;
+ assert(arch_get_irn_register(pp->arg));
+ arch_set_irn_register(proj, arch_get_irn_register(pp->arg));
+ DBG((dbg, LEVEL_2, "Copy register assignment %s from %+F to %+F\n", arch_get_irn_register(pp->arg)->name, pp->arg, pp->proj));
+ }
+
+ /*
+ * Set the phi nodes to their new arguments: The Projs of the Perm
+ */
+ for (phi = (ir_node*)get_irn_link(bl); phi != NULL;
+ phi = (ir_node*)get_irn_link(phi)) {
+ perm_proj_t templ;
+
+ templ.arg = get_irn_n(phi, i);
+ perm_proj_t *const pp = set_find(perm_proj_t, arg_set, &templ, sizeof(templ), hash_irn(templ.arg));
+
+ /* If not found, it was an interfering argument */
+ if (pp) {
+ set_irn_n(phi, i, pp->proj);
+ be_liveness_introduce(lv, pp->proj);
+ }
+ }
+
+ /* update the liveness of the Perm's operands. It might be changed. */
+ {
+ int i;
+ for (i = 0; i < n_projs; ++i)
+ be_liveness_update(lv, in[i]);
+ }
+ free(in);
+ }
+
+ del_set(arg_set);
+ }