+ /* For all colors root and arg have "disjunct", add 1 constraints to E.
+ * If root gets a color the arg is not possible to get then they will
+ * definetly get different colors. So y has to be 1.
+ * Vice versa for arg.
+ */
+ bitset_copy(work_regs, root_regs);
+ bitset_xor(work_regs, arg_regs);
+ bitset_foreach(work_regs, color) {
+ int root_idx, arg_idx, cst_idx;
+ mangle_var2(buf, 'x', rootnr, color);
+ root_idx = lpp_get_var_idx(pi->curr_lp, buf);
+ mangle_var2(buf, 'x', argnr, color);
+ arg_idx = lpp_get_var_idx(pi->curr_lp, buf);
+
+ mangle_cst(buf, 'E', cst_counter++);
+ cst_idx = lpp_add_cst(pi->curr_lp, buf, lpp_less, 0);
+ if (bitset_is_set(root_regs, color)) {
+ /* add root-y <= 0 */
+ lpp_set_factor_fast(pi->curr_lp, cst_idx, root_idx, 1);
+ lpp_set_factor_fast(pi->curr_lp, cst_idx, y_idx, -1);
+ } else {
+ assert(bitset_is_set(arg_regs, color) && "bitset_xor is buggy");
+ /* add arg-y <= 0 */
+ lpp_set_factor_fast(pi->curr_lp, cst_idx, arg_idx, 1);
+ lpp_set_factor_fast(pi->curr_lp, cst_idx, y_idx, -1);
+ }
+ }
+ }
+ }
+}
+
+static INLINE int get_costs(problem_instance_t *pi, ir_node *phi, ir_node *irn) {
+ int i;
+ unit_t *curr;
+ /* search optimization unit for phi */
+ list_for_each_entry(unit_t, curr, &pi->co->units, units)
+ if (curr->nodes[0] == phi) {
+ for (i=1; i<curr->node_count; ++i)
+ if (curr->nodes[i] == irn)
+ return curr->costs[i];
+ assert(0 && "irn must occur in this ou");
+ }
+ assert(0 && "phi must be found in a ou");
+ return 0;
+}
+
+static void clique_path_walker(ir_node *block, void *env) {
+ problem_instance_t *pi = env;
+ int count, arity, row, col, other_row, *costs;
+ ir_node **phis, *phi, *irn, **phi_matrix;
+ pset *done;
+ bitset_t *candidates;
+
+ /* Count all phi nodes of this block */
+ for (count=0, irn = sched_first(block); is_Phi(irn); irn = sched_next(irn))
+ count++;
+
+ /* We at least 2 phi nodes for this class of inequalities */
+ if (count < 2)
+ return;
+
+ /* Build the \Phi-Matrix */
+ arity = get_irn_arity(sched_first(block));
+ phis = alloca(count * sizeof(*phis));
+ costs = alloca(count * sizeof(costs));
+ phi_matrix = alloca(count*arity * sizeof(*phi_matrix));
+ candidates = bitset_alloca(count);
+
+ phi = sched_first(block);
+ for (row=0; row<count; ++row) {
+ phis[row] = phi;
+ for (col=0; col<arity; ++col) {
+ ir_node *arg = get_irn_n(phi, col);
+ /* Sort out all arguments interfering with its phi */
+ if (nodes_interfere(pi->co->chordal_env, phi, arg)) {
+ phi_matrix[row*arity + col] = NULL;
+ } else
+ phi_matrix[row*arity + col] = arg;
+ }
+ phi = sched_next(phi);
+ }
+
+ /* Now find the interesting patterns in the matrix:
+ * All nodes which are used at least twice in a column. */
+ /* columnwise ... */
+ for (col=0; col<arity; ++col) {
+ done = pset_new_ptr_default();
+ for (row=0; row<count; ++row) {
+ irn = phi_matrix[row*arity + col];
+ /*
+ * is this an interfering arg (NULL)
+ * or has the irn already been processed in this col?
+ */
+ if (!irn || pset_find_ptr(done, irn))
+ continue;
+ else
+ pset_insert_ptr(done, irn);
+
+ /* insert irn in candidates */
+ bitset_clear_all(candidates);
+ bitset_set(candidates, row);
+ /* search the irn in the rows below */
+ for (other_row = row+1; other_row<count; ++other_row)
+ if (irn == phi_matrix[other_row*arity + col]) {
+ /* found the irn in the same col in another row */
+ bitset_set(candidates, other_row);
+ }
+
+ /* now we know all occurences of irn in this col */
+ if (bitset_popcnt(candidates) < 2)
+ continue;
+
+ /* generate an unequation finally.
+ * phis are indexed in the bitset,
+ * shared argument is irn
+ * rhs is phi_count - 1 */
+ {
+ char buf[32];
+ ir_node *root;
+ int pos, irnnr, rootnr, cst_idx, y_idx, cst_counter = 0;
+ int minimal_unequal_count = bitset_popcnt(candidates)-1;
+
+ irnnr = get_irn_graph_nr(irn);
+ mangle_cst(buf, 'M', cst_counter++);
+ cst_idx = lpp_add_cst(pi->curr_lp, buf, lpp_greater, minimal_unequal_count);
+
+ /* for all phis */
+ bitset_foreach(candidates, pos) {
+ root = phis[pos];
+ rootnr = get_irn_graph_nr(root);
+ mangle_var2(buf, 'y', rootnr, irnnr);
+ y_idx = lpp_get_var_idx(pi->curr_lp, buf);
+ lpp_set_factor_fast(pi->curr_lp, cst_idx, y_idx, 1);
+ }
+ }
+ }
+ del_pset(done); /* clear set for next row */
+ } /*next col*/
+}
+
+/**
+ * Matrix M: Multi-Arg-Use. Interrelates different \phi-functions
+ * in the same block, iff they use the same arg at the same pos.
+ * Only one of the phis can get the arg.
+ */
+static void pi_add_clique_path_cstr(problem_instance_t *pi) {
+ DBG((dbg, LEVEL_2, "Adding clique path constraints...\n"));
+ dom_tree_walk_irg(get_irg(pi->co), clique_path_walker, NULL, pi);
+}
+
+#ifndef PATH_CONSTRAINTS_FOR_CLASSES
+/**
+ * Matrix P: Path contraints.
+ * If 2 nodes interfere and there is a path of equal-color-edges
+ * connecting them, then at least one of those equal-color-edges
+ * will break and cause some costs.
+ */
+static void pi_add_path_cstr(problem_instance_t *pi) {
+ unit_t *curr;
+ int cst_counter = 0;
+ DBG((dbg, LEVEL_2, "Adding path constraints...\n"));
+
+ /* for all optimization units (only phis) */
+ list_for_each_entry(unit_t, curr, &pi->co->units, units) {
+ int i, o, rootnr;
+
+ if (curr->min_nodes_costs == 0)
+ continue;
+
+ rootnr = get_irn_graph_nr(curr->nodes[0]);
+ /* check all argument pairs for interference */
+ for (i=1; i<curr->node_count; ++i) {
+ const ir_node *arg1 = curr->nodes[i];
+ int arg1nr = get_irn_graph_nr(arg1);
+ for (o=i+1; o<curr->node_count; ++o) {
+ const ir_node *arg2 = curr->nodes[o];
+ int arg2nr = get_irn_graph_nr(arg2);
+ if (nodes_interfere(pi->co->chordal_env, arg1, arg2)) {
+ int cst_idx, y_idx;
+ char buf[32];
+
+ mangle_cst(buf, 'P', cst_counter++);
+ cst_idx = lpp_add_cst(pi->curr_lp, buf, lpp_greater, 1);
+
+ mangle_var2(buf, 'y', rootnr, arg1nr);
+ y_idx = lpp_get_var_idx(pi->curr_lp, buf);
+ lpp_set_factor_fast(pi->curr_lp, cst_idx, y_idx, 1);
+
+ mangle_var2(buf, 'y', rootnr, arg2nr);
+ y_idx = lpp_get_var_idx(pi->curr_lp, buf);
+ lpp_set_factor_fast(pi->curr_lp, cst_idx, y_idx, 1);
+ }
+ }