+static void lower_perm_node(ir_node *irn, lower_env_t *env)
+{
+ const arch_register_class_t *const reg_class = arch_get_irn_register(get_irn_n(irn, 0))->reg_class;
+ ir_node *const block = get_nodes_block(irn);
+ int const arity = get_irn_arity(irn);
+ reg_pair_t *const pairs = ALLOCAN(reg_pair_t, arity);
+ int keep_perm = 0;
+ int do_copy = env->do_copy;
+ /* Get the schedule predecessor node to the perm.
+ * NOTE: This works with auto-magic. If we insert the new copy/exchange
+ * nodes after this node, everything should be ok. */
+ ir_node * sched_point = sched_prev(irn);
+ const ir_edge_t * edge;
+ const ir_edge_t * next;
+ int n;
+ int i;
+
+ DBG((dbg, LEVEL_1, "perm: %+F, sched point is %+F\n", irn, sched_point));
+ assert(sched_point && "Perm is not scheduled or has no predecessor");
+
+ assert(arity == get_irn_n_edges(irn) && "perm's in and out numbers different");
+
+ /* build the list of register pairs (in, out) */
+ n = 0;
+ foreach_out_edge_safe(irn, edge, next) {
+ ir_node *const out = get_edge_src_irn(edge);
+ long const pn = get_Proj_proj(out);
+ ir_node *const in = get_irn_n(irn, pn);
+ arch_register_t const *const in_reg = arch_get_irn_register(in);
+ arch_register_t const *const out_reg = arch_get_irn_register(out);
+ reg_pair_t * pair;
+
+ if (in_reg == out_reg) {
+ DBG((dbg, LEVEL_1, "%+F removing equal perm register pair (%+F, %+F, %s)\n",
+ irn, in, out, out_reg->name));
+ exchange(out, in);
+ continue;
+ }
+
+ pair = &pairs[n++];
+ pair->in_node = in;
+ pair->in_reg = in_reg;
+ pair->out_node = out;
+ pair->out_reg = out_reg;
+ pair->checked = 0;
+ }
+
+ DBG((dbg, LEVEL_1, "%+F has %d unresolved constraints\n", irn, n));
+
+ /* Set do_copy to 0 if it's on but we have no free register */
+ /* TODO check for free register */
+ if (do_copy) {
+ do_copy = 0;
+ }
+
+ /* check for cycles and chains */
+ while (get_n_unchecked_pairs(pairs, n) > 0) {
+ perm_cycle_t cycle;
+ int j;
+
+ /* go to the first not-checked pair */
+ for (i = 0; pairs[i].checked; ++i) {}
+ get_perm_cycle(&cycle, pairs, n, i);
+
+ DB((dbg, LEVEL_1, "%+F: following %s created:\n ", irn, cycle.type == PERM_CHAIN ? "chain" : "cycle"));
+ for (j = 0; j < cycle.n_elems; j++) {
+ DB((dbg, LEVEL_1, " %s", cycle.elems[j]->name));
+ }
+ DB((dbg, LEVEL_1, "\n"));
+
+ if (cycle.type == PERM_CYCLE && arity == 2) {
+ /* We don't need to do anything if we have a Perm with two elements
+ * which represents a cycle, because those nodes already represent
+ * exchange nodes */
+ keep_perm = 1;
+ } else {
+ /* TODO: - iff PERM_CYCLE && do_copy -> determine free temp reg and
+ * insert copy to/from it before/after the copy cascade (this
+ * reduces the cycle into a chain) */
+
+ /* build copy/swap nodes from back to front */
+ for (i = cycle.n_elems - 2; i >= 0; i--) {
+ ir_node *arg1 = get_node_for_in_register(pairs, n, cycle.elems[i]);
+ ir_node *arg2 = get_node_for_in_register(pairs, n, cycle.elems[i + 1]);
+
+ ir_node *res1 = get_node_for_out_register(pairs, n, cycle.elems[i]);
+ ir_node *res2 = get_node_for_out_register(pairs, n, cycle.elems[i + 1]);
+ /* If we have a cycle and don't copy: we need to create exchange
+ * nodes
+ * NOTE: An exchange node is a perm node with 2 INs and 2 OUTs
+ * IN_1 = in node with register i
+ * IN_2 = in node with register i + 1
+ * OUT_1 = out node with register i + 1
+ * OUT_2 = out node with register i */
+ if (cycle.type == PERM_CYCLE && !do_copy) {
+ ir_node *in[2];
+ ir_node *cpyxchg;
+
+ in[0] = arg1;
+ in[1] = arg2;
+
+ /* At this point we have to handle the following problem:
+ *
+ * If we have a cycle with more than two elements, then this
+ * could correspond to the following Perm node:
+ *
+ * +----+ +----+ +----+
+ * | r1 | | r2 | | r3 |
+ * +-+--+ +-+--+ +--+-+
+ * | | |
+ * | | |
+ * +-+--------+---------+-+
+ * | Perm |
+ * +-+--------+---------+-+
+ * | | |
+ * | | |
+ * +-+--+ +-+--+ +--+-+
+ * |Proj| |Proj| |Proj|
+ * | r2 | | r3 | | r1 |
+ * +----+ +----+ +----+
+ *
+ * This node is about to be split up into two 2x Perm's for
+ * which we need 4 Proj's and the one additional Proj of the
+ * first Perm has to be one IN of the second. So in general
+ * we need to create one additional Proj for each "middle"
+ * Perm and set this to one in node of the successor Perm. */
+
+ DBG((dbg, LEVEL_1, "%+F creating exchange node (%+F, %s) and (%+F, %s) with\n",
+ irn, arg1, cycle.elems[i]->name, arg2, cycle.elems[i + 1]->name));
+ DBG((dbg, LEVEL_1, "%+F (%+F, %s) and (%+F, %s)\n",
+ irn, res1, cycle.elems[i]->name, res2, cycle.elems[i + 1]->name));
+
+ cpyxchg = be_new_Perm(reg_class, block, 2, in);
+
+ if (i > 0) {
+ /* cycle is not done yet */
+ int pidx = get_pairidx_for_in_regidx(pairs, n, cycle.elems[i]->index);
+
+ /* create intermediate proj */
+ res1 = new_r_Proj(block, cpyxchg, get_irn_mode(res1), 0);
+
+ /* set as in for next Perm */
+ pairs[pidx].in_node = res1;
+ }
+
+ set_Proj_pred(res2, cpyxchg);
+ set_Proj_proj(res2, 0);
+ set_Proj_pred(res1, cpyxchg);
+ set_Proj_proj(res1, 1);
+
+ arch_set_irn_register(res2, cycle.elems[i + 1]);
+ arch_set_irn_register(res1, cycle.elems[i]);
+
+ /* insert the copy/exchange node in schedule after the magic schedule node (see above) */
+ sched_add_after(skip_Proj(sched_point), cpyxchg);
+
+ DB((dbg, LEVEL_1, "replacing %+F with %+F, placed new node after %+F\n", irn, cpyxchg, sched_point));
+
+ /* set the new scheduling point */
+ sched_point = res1;
+ } else {
+ ir_node *cpyxchg;
+
+ DB((dbg, LEVEL_1, "%+F creating copy node (%+F, %s) -> (%+F, %s)\n",
+ irn, arg1, cycle.elems[i]->name, res2, cycle.elems[i + 1]->name));
+
+ cpyxchg = be_new_Copy(reg_class, block, arg1);
+ arch_set_irn_register(cpyxchg, cycle.elems[i + 1]);
+
+ /* exchange copy node and proj */
+ exchange(res2, cpyxchg);
+
+ /* insert the copy/exchange node in schedule after the magic schedule node (see above) */
+ sched_add_after(skip_Proj(sched_point), cpyxchg);
+
+ /* set the new scheduling point */
+ sched_point = cpyxchg;
+ }
+ }
+ }
+
+ free((void*)cycle.elems);
+ }
+
+ /* remove the perm from schedule */
+ if (!keep_perm) {
+ sched_remove(irn);
+ kill_node(irn);
+ }
+}
+
+
+
+static int has_irn_users(const ir_node *irn) {
+ return get_irn_out_edge_first_kind(irn, EDGE_KIND_NORMAL) != 0;
+}
+
+static ir_node *find_copy(ir_node *irn, ir_node *op)
+{
+ ir_node *cur_node;
+
+ for (cur_node = irn;;) {
+ cur_node = sched_prev(cur_node);
+ if (! be_is_Copy(cur_node))
+ return NULL;
+ if (be_get_Copy_op(cur_node) == op && arch_irn_is(cur_node, dont_spill))
+ return cur_node;
+ }
+}
+
+static void gen_assure_different_pattern(ir_node *irn, ir_node *other_different, constraint_env_t *env) {
+ ir_graph *irg;
+ ir_nodemap_t *op_set;
+ ir_node *block;
+ const arch_register_class_t *cls;
+ ir_node *keep, *cpy;
+ op_copy_assoc_t *entry;
+
+ if (arch_irn_is_ignore(other_different) ||
+ !mode_is_datab(get_irn_mode(other_different))) {
+ DB((dbg_constr, LEVEL_1, "ignore constraint for %+F because other_irn is ignore or not a datab node\n", irn));