Added beirgmod and benode
[libfirm] / ir / be / becopyilp.c
index 367bf6a..6f7f75b 100644 (file)
 /**
- * Minimizing copies with an exact algorithm using mixed integer programming (MIP).
- * Problem statement as a 'quadratic 0-1 program with linear constraints' with
- * n binary variables. Constraints are knapsack (enforce color for each node) and
- * cliques of ifg (interference constraints).
- * Transformation into a 'mixed integer program' with n binary variables and
- * additional 2n real variables. Constraints are the above the transformed
- * objective function and 'complementary conditions' for two var classes.
- * @author Daniel Grund
- *
- * NOTE: Unfortunately no good solver is available locally (or even for linking)
- *       We use CPLEX 9.0 which runs on a machine residing at the Rechenzentrum.
- * @date 12.04.2005
+ * Author:      Daniel Grund
+ * Date:               17.05.2005
+ * Copyright:   (c) Universitaet Karlsruhe
+ * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
  */
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
 
-#include "becopyopt.h"
+#ifdef HAVE_ALLOCA_H
+#include <alloca.h>
+#endif
+#ifdef HAVE_MALLOC_H
+#include <malloc.h>
+#endif
 
-#define DUMP_MPS                       /**< dumps the problem in "CPLEX"-MPS format. NOT fixed-column-MPS. */
-#define USE_SOS                                /**< uses Special Ordered Sets when using MPS */
-#define DO_SOLVE                       /**< solve the MPS output with CPLEX */
-#define DUMP_MATRICES          /**< dumps all matrices completely. only recommended for small problems */
-#define DUMP_LP                                /**< dumps the problem in LP format. 'human-readable' equations etc... */
-#define DELETE_FILES           /**< deletes all dumped files after use */
-#define EXPECT_FILENAME "runme" /** name of the expect-script */
+#include "irprog.h"
 
-/* CPLEX-account related stuff */
-#define SSH_USER_HOST_PATH "kb61@sp-smp.rz.uni-karlsruhe.de"
-#define SSH_PASSWD "!cplex90"
+#include "lpp.h"
+#include "xmalloc.h"
+#include "becopyopt.h"
+#include "becopystat.h"
 
 #define DEBUG_LVL SET_LEVEL_1
 static firm_dbg_module_t *dbg = NULL;
 
-#define SLOTS_NUM2POS 256
 #define SLOTS_LIVING 32
 
 /**
- * A type storing names of the x variables in the form x[NUMBER]_[COLOR]
- */
-typedef struct _x_name_t {
-       int n, c;
-} x_name_t;
+ * Represents the _costs_ if node n and m have different colors.
+ * Must be >=0.
+ **/
+#define get_weight(n,m) 1
 
-/**
- * For each node taking part in the opt-problem its position in the
- * x-variable-vector is stored in a set. This set maps the node-nr (given by
- * benumb) to the position in the vector.
- */
-typedef struct _num2pos_t {
-       int num, pos;
-} num2pos_t;
+typedef struct _simpl_t {
+       struct list_head chain;
+       if_node_t *ifn;
+} simpl_t;
 
-/**
- * A type storing the unmodified '0-1 quadratic program' of the form
- * min f = xQx
- * udN:  Ax  = e
- *       Bx <= e
- *        x \in {0, 1}
- *
- * This problem is called the original problem
- */
 typedef struct _problem_instance_t {
-       ir_graph* irg;
-       const char *name;
-       int x_dim, A_dim, B_dim;        /**< number of: x variables, rows in A, rows in B */
-       x_name_t *x;                            /**< stores the names of the x variables. all possible colors for a node are ordered and occupy consecutive entries. lives in obstack ob. */
-       set *num2pos;                           /**< maps node numbers to positions in x. */
-       sp_matrix_t *Q, *A, *B;         /**< the (sparse) matrices of this problem */
-
-       /* needed only for linearizations */
-       int bigM, maxQij, minQij;
-
-       /* overhead needed to build this */
-       struct obstack ob;
-       int curr_color;
-       int curr_row;
+       const copy_opt_t *co;                   /** the original copy_opt problem */
+       /* problem size reduction removing simple nodes */
+       struct list_head simplicials;   /**< holds all simpl_t's in right order to color*/
+       pset *removed;                                  /**< holds all removed simplicial irns */
+       /* lp problem */
+       lpp_t *dilp;                                    /**< problem formulation directly as milp */
+       /* overhead stuff */
+       lpp_t *curr_lp;                                 /**< points to the problem currently used */
+       int curr_color, cst_counter, last_x_var;
+       char buf[32];
 } problem_instance_t;
 
-/* Nodes have consecutive numbers so this hash shoud be fine */
-#define HASH_NUM(num) num
-
-static int set_cmp_num2pos(const void *x, const void *y, size_t size) {
-       return ((num2pos_t *)x)->num != ((num2pos_t *)y)->num;
-}
-
-/**
- * Sets the first position of node with number num to pos.
- * See x_name_t *x in _problem_instance_t.
+#define is_removed(irn) pset_find_ptr(pi->removed, irn)
+/*
+ * Some stuff for variable name handling.
  */
-static INLINE void pi_set_first_pos(problem_instance_t *pi, int num, int pos) {
-       num2pos_t find;
-       find.num = num;
-       find.pos = pos;
-       set_insert(pi->num2pos, &find, sizeof(find), HASH_NUM(num));
-}
+#define mangle_cst(buf, prefix, nr) \
+                       snprintf((buf), sizeof(buf), "%c%d", (prefix), (nr))
 
-/**
- * Get position by number. (First possible color)
- * returns -1 if not found.
- */
-static INLINE int pi_get_first_pos(problem_instance_t *pi, int num) {
-       num2pos_t find, *found;
-       find.num = num;
-       found = set_find(pi->num2pos, &find, sizeof(find), HASH_NUM(num));
-       if (found) {
-               assert(pi->x[found->pos].n == num && (found->pos == 0 || pi->x[found->pos-1].n != num) && "pi->num2pos is broken!");
-               return found->pos;
-       } else
-               return -1;
-}
+#define mangle_var(buf, prefix, node_nr, color) \
+                       snprintf((buf), sizeof(buf), "%c%d_%d", (prefix), (node_nr), (color))
 
-/**
- * Get position by number and color.
- * returns -1 if not found.
- */
-static INLINE int pi_get_pos(problem_instance_t *pi, int num, int col) {
-       num2pos_t find, *found;
-       find.num = num;
-       int pos;
-       found = set_find(pi->num2pos, &find, sizeof(find), HASH_NUM(num));
-       if (!found)
-               return -1;
-       pos = found->pos;
-       while (pos < pi->x_dim && pi->x[pos].n == num && pi->x[pos].c < col)
-               pos++;
-
-       if (pi->x[pos].n == num && pi->x[pos].c == col)
-               return pos;
-       else
-               return -1;
-}
+#define mangle_var_irn(buf, prefix, irn, color) \
+                       mangle_var((buf), (prefix), get_irn_graph_nr(irn), (color))
 
-static INLINE FILE *ffopen(const char *base, const char *ext, const char *mode) {
-       FILE *out;
-       char buf[1024];
+#define split_var(var, nnr, col) \
+                       sscanf(var, "x%d_%d", (nnr), (col))
 
-       snprintf(buf, sizeof(buf), "%s.%s", base, ext);
-       if (! (out = fopen(buf, mode))) {
-               fprintf(stderr, "Cannot open file %s in mode %s\n", buf, mode);
-               assert(0);
-       }
-       return out;
-}
 
-#ifdef DUMP_MATRICES
 /**
- * Dump the raw matrices of the problem to a file for debugging.
+ * Checks if a node is simplicial in the graph
+ * heeding the already removed nodes.
  */
-static void pi_dump_matrices(problem_instance_t *pi) {
-       int i;
-       FILE *out = ffopen(pi->name, "matrix", "wt");
-
-       DBG((dbg, LEVEL_1, "Dumping raw...\n"));
-       fprintf(out, "\n\nx-names =\n");
-       for (i=0; i<pi->x_dim; ++i)
-               fprintf(out, "%5d %2d\n", pi->x[i].n, pi->x[i].c);
-
-       fprintf(out, "\n\n-Q =\n");
-       matrix_dump(pi->Q, out, -1);
-
-       fprintf(out, "\n\nA =\n");
-       matrix_dump(pi->A, out, 1);
-
-       fprintf(out, "\n\nB =\n");
-       matrix_dump(pi->B, out, 1);
-
-       fclose(out);
-}
-#endif
-
-#ifdef DUMP_LP
-/**
- * Dumps the problem instance as a MILP. The original problem is transformed into:
- * min f = es - Mex
- * udN:  Qx -y -s +Me = 0
- *       Ax  = e
- *       Bx <= e
- *        y <= 2M(e-x)
- *        x \in N   y, s >= 0
- *
- * with M >= max sum Q'ij * x_j
- *            i   j
- */
-static void pi_dump_lp(problem_instance_t *pi) {
-       int i, max_abs_Qij;
-       matrix_elem_t *e;
-       FILE *out = ffopen(pi->name, "lpo", "wt");
-
-       DBG((dbg, LEVEL_1, "Dumping lp_org...\n"));
-       /* calc the big M for Q */
-       max_abs_Qij = pi->maxQij;
-       if (-pi->minQij > max_abs_Qij)
-               max_abs_Qij = -pi->minQij;
-       pi->bigM = pi->A_dim * max_abs_Qij;
-       DBG((dbg, LEVEL_2, "BigM = %d\n", pi->bigM));
-
-       /* generate objective function */
-       fprintf(out, "min: ");
-       for (i=0; i<pi->x_dim; ++i)
-               fprintf(out, "+s%d_%d -%dx%d_%d ", pi->x[i].n, pi->x[i].c, pi->bigM, pi->x[i].n, pi->x[i].c);
-       fprintf(out, ";\n\n");
-
-       /* constraints for former objective function */
-       for (i=0; i<pi->x_dim; ++i)     {
-               matrix_foreach_in_row(pi->Q, i, e) {
-                       int Qio = e->val;
-                               if (Qio == 1)
-                                       fprintf(out, "+x%d_%d ", pi->x[e->col].n, pi->x[e->col].c);
-                               else if(Qio == -1)
-                                       fprintf(out, "-x%d_%d ", pi->x[e->col].n, pi->x[e->col].c);
-                               else
-                                       fprintf(out, "%+dx%d_%d ", Qio, pi->x[e->col].n, pi->x[e->col].c);
-               }
-               fprintf(out, "-y%d_%d -s%d_%d +%d= 0;\n", pi->x[i].n, pi->x[i].c, pi->x[i].n, pi->x[i].c, pi->bigM);
-       }
-       fprintf(out, "\n\n");
-
-       /* constraints for (special) complementary condition */
-       for (i=0; i<pi->x_dim; ++i)
-               fprintf(out, "y%d_%d <= %d - %dx%d_%d;\n", pi->x[i].n, pi->x[i].c, 2*pi->bigM, 2*pi->bigM, pi->x[i].n, pi->x[i].c);
-       fprintf(out, "\n\n");
-
-       /* knapsack constraints */
-       for (i=0; i<pi->A_dim; ++i)     {
-               matrix_foreach_in_row(pi->Q, i, e)
-                       fprintf(out, "+x%d_%d ", pi->x[e->col].n, pi->x[e->col].c);
-               fprintf(out, " = 1;\n");
-       }
-       fprintf(out, "\n\n");
-
-       /* interference graph constraints */
-       for (i=0; i<pi->B_dim; ++i)     {
-               matrix_foreach_in_row(pi->Q, i, e)
-                       fprintf(out, "+x%d_%d ", pi->x[e->col].n, pi->x[e->col].c);
-               fprintf(out, " <= 1;\n");
-       }
-       fprintf(out, "\n\n");
-
-       /* integer constraints */
-       fprintf(out, "int x%d_%d", pi->x[0].n, pi->x[0].c);
-       for (i=1; i<pi->x_dim; ++i)
-               fprintf(out, ", x%d_%d", pi->x[i].n, pi->x[i].c);
-       fprintf(out, ";\n");
-
-       fclose(out);
-}
-#endif
-
-#ifdef DUMP_MPS
-/**
- * Dumps an mps file representing the problem. This is NOT the old-style,
- * fixed-column format. Some white spaces are important, in general spaces
- * are separators, MARKER-lines are used in COLUMN section to define binaries.
- */
-//BETTER use last 2 fields in COLUMNS section
-static void pi_dump_mps(problem_instance_t *pi) {
-       int i, max_abs_Qij;
-       matrix_elem_t *e;
-       FILE *out = ffopen(pi->name, "mps", "wt");
-
-       DBG((dbg, LEVEL_1, "Dumping mps...\n"));
-       max_abs_Qij = pi->maxQij;
-       if (-pi->minQij > max_abs_Qij)
-               max_abs_Qij = -pi->minQij;
-       pi->bigM = pi->A_dim * max_abs_Qij;
-       DBG((dbg, LEVEL_2, "BigM = %d\n", pi->bigM));
-
-       fprintf(out, "NAME %s\n", pi->name);
-
-       fprintf(out, "ROWS\n");
-       fprintf(out, " N obj\n");
-       for (i=0; i<pi->x_dim; ++i)
-               fprintf(out, " E cQ%d\n", i);
-       for (i=0; i<pi->A_dim; ++i)
-               fprintf(out, " E cA%d\n", i);
-       for (i=0; i<pi->B_dim; ++i)
-               fprintf(out, " L cB%d\n", i);
-       for (i=0; i<pi->x_dim; ++i)
-               fprintf(out, " L cy%d\n", i);
-
-       fprintf(out, "COLUMNS\n");
-       /* the x vars come first */
-       /* mark them as binaries */
-       fprintf(out, "    MARKI0\t'MARKER'\t'INTORG'\n");
-#ifdef USE_SOS
-       int sos_cnt = 0;
-       fprintf(out, " S1 SOS_%d\t'MARKER'\t'SOSORG'\n", sos_cnt++);
-#endif
-       for (i=0; i<pi->x_dim; ++i) {
-#ifdef USE_SOS
-               if (i>0 && pi->x[i].n != pi->x[i-1].n) {
-                       fprintf(out, "    SOS_%d\t'MARKER'\t'SOSEND'\n", sos_cnt++);
-                       fprintf(out, " S1 SOS_%d\t'MARKER'\t'SOSORG'\n", sos_cnt++);
-               }
-#endif
-               /* participation in objective */
-               fprintf(out, "    x%d_%d\tobj\t%d\n", pi->x[i].n, pi->x[i].c, -pi->bigM);
-               /* in Q */
-               matrix_foreach_in_col(pi->Q, i, e)
-                       fprintf(out, "    x%d_%d\tcQ%d\t%d\n", pi->x[i].n, pi->x[i].c, e->row, e->val);
-               /* in A */
-               matrix_foreach_in_col(pi->A, i, e)
-                       fprintf(out, "    x%d_%d\tcA%d\t%d\n", pi->x[i].n, pi->x[i].c, e->row, e->val);
-               /* in B */
-               matrix_foreach_in_col(pi->B, i, e)
-                       fprintf(out, "    x%d_%d\tcB%d\t%d\n", pi->x[i].n, pi->x[i].c, e->row, e->val);
-               /* in y */
-               fprintf(out, "    x%d_%d\tcy%d\t%d\n", pi->x[i].n, pi->x[i].c, i, 2*pi->bigM);
-       }
-
-#ifdef USE_SOS
-       fprintf(out, "    SOS_%d\t'MARKER'\t'SOSEND'\n", sos_cnt++);
-#endif
-       fprintf(out, "    MARKI1\t'MARKER'\t'INTEND'\n"); /* end of marking */
-
-       /* next the s vars */
-       for (i=0; i<pi->x_dim; ++i) {
-               /* participation in objective */
-               fprintf(out, "    s%d_%d\tobj\t%d\n", pi->x[i].n, pi->x[i].c, 1);
-               /* in Q */
-               fprintf(out, "    s%d_%d\tcQ%d\t%d\n", pi->x[i].n, pi->x[i].c, i, -1);
-       }
-
-       /* next the y vars */
-       for (i=0; i<pi->x_dim; ++i) {
-               /* in Q */
-               fprintf(out, "    y%d_%d\tcQ%d\t%d\n", pi->x[i].n, pi->x[i].c, i, -1);
-               /* in y */
-               fprintf(out, "    y%d_%d\tcy%d\t%d\n", pi->x[i].n, pi->x[i].c, i, 1);
-       }
-
-       fprintf(out, "RHS\n");
-       for (i=0; i<pi->x_dim; ++i)
-               fprintf(out, "    rhs\tcQ%d\t%d\n", i, -pi->bigM);
-       for (i=0; i<pi->A_dim; ++i)
-               fprintf(out, "    rhs\tcA%d\t%d\n", i, 1);
-       for (i=0; i<pi->B_dim; ++i)
-               fprintf(out, "    rhs\tcB%d\t%d\n", i, 1);
-       for (i=0; i<pi->x_dim; ++i)
-               fprintf(out, "    rhs\tcy%d\t%d\n", i, 2*pi->bigM);
-
-       fprintf(out, "ENDATA\n");
-       fclose(out);
-
-       out = ffopen(pi->name, "mst", "wt");
-       fprintf(out, "NAME\n");
-       for (i=0; i<pi->x_dim; ++i) {
-               int val, n, c;
-               n = pi->x[i].n;
-               c = pi->x[i].c;
-               if (get_irn_color(get_irn_for_graph_nr(pi->irg, n)) == c)
-                       val = 1;
-               else
-                       val = 0;
-               fprintf(out, "    x%d_%d\t%d\n", n, c, val);
-       }
-       fprintf(out, "ENDATA\n");
-       fclose(out);
-}
-#endif
-
-#ifdef DO_SOLVE
-/**
- * Invoke an external solver
- */
-static void pi_solve_ilp(problem_instance_t *pi) {
-       FILE *out;
-
-       /* write command file for CPLEX */
-       out = ffopen(pi->name, "cmd", "wt");
-       fprintf(out, "read %s.mps\n", pi->name);
-       fprintf(out, "read %s.mst\n", pi->name);
-       fprintf(out, "set mip strategy mipstart 1\n");
-       fprintf(out, "optimize\n");
-       fprintf(out, "set logfile %s.sol\n", pi->name);
-       fprintf(out, "display solution variables 1-%d\n", pi->x_dim);
-       fprintf(out, "set logfile cplex.log\n");
-       fprintf(out, "quit\n");
-       fclose(out);
-
-       /* write expect-file for copying problem to RZ */
-       out = ffopen(EXPECT_FILENAME, "exp", "wt");
-       fprintf(out, "#! /usr/bin/expect\n");
-       fprintf(out, "spawn scp %s.mps %s.mst %s.cmd %s:\n", pi->name, pi->name, pi->name, SSH_USER_HOST_PATH);
-       fprintf(out, "expect \":\"\n");
-       fprintf(out, "send \"%s\\n\"\n", SSH_PASSWD);
-       fprintf(out, "interact\n");
-
-       fprintf(out, "spawn ssh %s \"./cplex90 < %s.cmd\"\n", SSH_USER_HOST_PATH, pi->name);
-       fprintf(out, "expect \":\"\n");
-       fprintf(out, "send \"%s\\n\"\n", SSH_PASSWD);
-       fprintf(out, "interact\n");
-
-       fprintf(out, "spawn scp %s:%s.sol .\n", SSH_USER_HOST_PATH, pi->name);
-       fprintf(out, "expect \":\"\n");
-       fprintf(out, "send \"%s\\n\"\n", SSH_PASSWD);
-       fprintf(out, "interact\n");
-       fclose(out);
-
-       /* call the expect script */
-       chmod(EXPECT_FILENAME ".exp", 0700);
-       system(EXPECT_FILENAME ".exp");
+static INLINE int pi_is_simplicial(problem_instance_t *pi, const if_node_t *ifn) {
+       int i, o, size = 0;
+       if_node_t **all, *curr;
+       all = alloca(ifn_get_degree(ifn) * sizeof(*all));
+
+       /* get all non-removed neighbors */
+       foreach_neighb(ifn, curr)
+               if (!is_removed(curr))
+                       all[size++] = curr;
+
+       /* check if these form a clique */
+       for (i=0; i<size; ++i)
+               for (o=i+1; o<size; ++o)
+                       if (!ifg_has_edge(pi->co->chordal_env, all[i], all[o]))
+                               return 0;
+
+       /* all edges exist so this is a clique */
+       return 1;
 }
 
 /**
- * Sets the colors of irns according to the values of variables found in the
- * output file of the solver.
+ * Iterative finds and 'removes' from the graph all nodes which are
+ * simplicial AND not member of a equal-color-wish
  */
-static void pi_apply_solution(problem_instance_t *pi) {
-       FILE *in = ffopen(pi->name, "sol", "rt");
-
-       if (!in)
-               return;
-       DBG((dbg, LEVEL_1, "Applying solution...\n"));
-       while (!feof(in)) {
-               char buf[1024];
-               int num = -1, col = -1, val = -1;
-               if (fscanf(in, "x%d_%d %d.%s\n", &num, &col, &val, buf) != 3) {
-                       while(fscanf(in, "%1020s\n", buf) != 1);
-                       continue;
-               }
-               if (val == 1) {
-                       DBG((dbg, LEVEL_1, "x%d_%d = %d\n", num, col, val));
-                       set_irn_color(get_irn_for_graph_nr(pi->irg, num), col);
+static void pi_find_simplicials(problem_instance_t *pi) {
+       set *if_nodes;
+       if_node_t *ifn;
+       int redo = 1;
+
+       if_nodes = be_ra_get_ifg_nodes(pi->co->chordal_env);
+       while (redo) {
+               redo = 0;
+               for (ifn = set_first(if_nodes); ifn; ifn = set_next(if_nodes)) {
+                       ir_node *irn = get_irn_for_graph_nr(pi->co->irg, ifn->nnr);
+                       if (!is_removed(irn) && !is_optimizable(irn) &&
+          !is_optimizable_arg(pi->co, irn) && pi_is_simplicial(pi, ifn)) {
+                               simpl_t *s = xmalloc(sizeof(*s));
+                               s->ifn = ifn;
+                               list_add(&s->chain, &pi->simplicials);
+                               pset_insert_ptr(pi->removed, irn);
+                               redo = 1;
+                               DBG((dbg, LEVEL_2, " Removed %n\n", irn));
+                       }
                }
        }
-       fclose(in);
-}
-#endif /* DO_SOLVE */
-
-#ifdef DELETE_FILES
-static void pi_delete_files(problem_instance_t *pi) {
-       char buf[1024];
-       int end = snprintf(buf, sizeof(buf), "%s", pi->name);
-#ifdef DUMP_MATRICES
-       snprintf(buf+end, sizeof(buf)-end, ".matrix");
-       remove(buf);
-#endif
-#ifdef DUMP_MPS
-       snprintf(buf+end, sizeof(buf)-end, ".mps");
-       remove(buf);
-       snprintf(buf+end, sizeof(buf)-end, ".mst");
-       remove(buf);
-       snprintf(buf+end, sizeof(buf)-end, ".cmd");
-       remove(buf);
-       remove(EXPECT_FILENAME ".exp");
-#endif
-#ifdef DUMP_LP
-       snprintf(buf+end, sizeof(buf)-end, ".lp");
-       remove(buf);
-#endif
 }
-#endif
 
 /**
- * Collects all irns in currently processed register class
+ * Add coloring-force conditions
  */
-static void pi_collect_x_names(ir_node *block, void *env) {
+static void pi_add_constr_A(ir_node *block, void *env) {
        problem_instance_t *pi = env;
-       struct list_head *head = &get_ra_block_info(block)->border_head;
+       struct list_head *head = get_block_border_head(pi->co->chordal_env, block);
        border_t *curr;
+       bitset_t *pos_regs = bitset_alloca(pi->co->cls->n_regs);
 
        list_for_each_entry_reverse(border_t, curr, head, list)
-               if (curr->is_def && curr->is_real) {
-                       x_name_t xx;
-                       pi->A_dim++;                    /* one knapsack constraint for each node */
-
-                       xx.n = get_irn_graph_nr(curr->irn);
-                       pi_set_first_pos(pi, xx.n, pi->x_dim);
-                       //TODO iterate over all possible colors !!MUST BE IN ORDER!!
-                       for (xx.c=0; xx.c<MAX_COLORS; ++xx.c) {
-                               if (!is_possible_color(irn, xx.c))
-                                       continue;
-                               DBG((dbg, LEVEL_2, "Adding %n %d\n", curr->irn, xx.c));
-                               obstack_grow(&pi->ob, &xx, sizeof(xx));
-                               pi->x_dim++;            /* one x variable for each node and color */
+               if (curr->is_def && curr->is_real && !is_removed(curr->irn)) {
+                       int cst_idx, nnr, col;
+
+                       nnr = get_irn_graph_nr(curr->irn);
+                       mangle_cst(pi->buf, 'A', nnr);
+                       cst_idx = lpp_add_cst(pi->curr_lp, pi->buf, equal, 1);
+
+                       // iterate over all possible colors in order
+                       bitset_clear_all(pos_regs);
+                       arch_get_allocatable_regs(pi->co->env, curr->irn, arch_pos_make_out(0), pi->co->cls, pos_regs);
+                       bitset_foreach(pos_regs, col) {
+                               int var_idx;
+                               mangle_var(pi->buf, 'x', nnr, col);
+                               var_idx = lpp_add_var(pi->curr_lp, pi->buf, binary, 0);
+                               pi->last_x_var = var_idx;
+                               lpp_set_factor_fast(pi->curr_lp, cst_idx, var_idx, 1);
                        }
                }
 }
 
 /**
- * Checks if all nodes in living are live_out in block block.
+ * Checks if all nodes in @p living are live in in block @p block.
+ * @return 1 if all are live in
+ *         0 else
  */
-static INLINE int all_live_out(ir_node *block, pset *living) {
+static INLINE int all_live_in(ir_node *block, pset *living) {
        ir_node *n;
        for (n = pset_first(living); n; n = pset_next(living))
-               if (!is_live_out(block, n)) {
+               if (!is_live_in(block, n)) {
                        pset_break(living);
                        return 0;
                }
@@ -493,19 +167,20 @@ static INLINE int all_live_out(ir_node *block, pset *living) {
 /**
  * Finds cliques in the interference graph, considering only nodes
  * for which the color pi->curr_color is possible. Finds only 'maximal-cliques',
- * viz cliques which are not conatained in another one.
+ * viz cliques which are not contained in another one.
  * This is used for the matrix B.
+ * TODO check color
  */
-static void pi_clique_finder(ir_node *block, void *env) {
+static void pi_add_constr_B(ir_node *block, void *env) {
        problem_instance_t *pi = env;
        enum phase_t {growing, shrinking} phase = growing;
-       struct list_head *head = &get_ra_block_info(block)->border_head;
+       struct list_head *head = get_block_border_head(pi->co->chordal_env, block);
        border_t *b;
        pset *living = pset_new_ptr(SLOTS_LIVING);
 
        list_for_each_entry_reverse(border_t, b, head, list) {
                const ir_node *irn = b->irn;
-               if (!is_possible_color(n, pi->curr_col))
+               if (is_removed(irn))
                        continue;
 
                if (b->is_def) {
@@ -517,15 +192,20 @@ static void pi_clique_finder(ir_node *block, void *env) {
 
                        /* before shrinking the set, store the current 'maximum' clique;
                         * do NOT if clique is a single node
-                        * do NOT if all values are live_out */
-                       if (phase == growing && pset_count(living) >= 2 && !all_live_out(block, living)) {
+                        * do NOT if all values are live_in (in this case they were contained in a live-out clique elsewhere) */
+                       if (phase == growing && pset_count(living) >= 2 && !all_live_in(block, living)) {
+                               int cst_idx;
                                ir_node *n;
+                               mangle_cst(pi->buf, 'B', pi->cst_counter);
+                               cst_idx = lpp_add_cst(pi->curr_lp, pi->buf, less, 1);
                                for (n = pset_first(living); n; n = pset_next(living)) {
-                                       int pos = pi_get_pos(pi, get_irn_graph_nr(n), pi->curr_color);
-                                       matrix_set(pi->B, pi->curr_row, pos, 1);
-                                       DBG((dbg, LEVEL_2, "B[%d, %d] := %d\n", pi->curr_row, pos, 1));
+                                       int var_idx;
+                                       mangle_var_irn(pi->buf, 'x', n, pi->curr_color);
+                                       var_idx = lpp_get_var_idx(pi->curr_lp, pi->buf);
+                                       assert(var_idx>=1);
+                                       lpp_set_factor_fast(pi->curr_lp, cst_idx, var_idx, 1);
                                }
-                               pi->curr_row++;
+                               pi->cst_counter++;
                        }
                        pset_remove_ptr(living, irn);
                        phase = shrinking;
@@ -535,139 +215,203 @@ static void pi_clique_finder(ir_node *block, void *env) {
        del_pset(living);
 }
 
-/**
- * Generate the initial problem matrices and vectors.
- */
-static problem_instance_t *new_pi(const copy_opt_t *co) {
-       DBG((dbg, LEVEL_1, "Generating new instance...\n"));
-       problem_instance_t *pi = calloc(1, sizeof(*pi));
-       pi->irg = co->irg;
-       pi->name =      get_entity_name(get_irg_entity(co->irg));
-       pi->num2pos = new_set(set_cmp_num2pos, SLOTS_NUM2POS);
-       pi->bigM = 1;
-
-       /* Vector x
-        * one entry per node and possible color */
-       obstack_init(&pi->ob);
-       dom_tree_walk_irg(co->irg, pi_collect_x_names, NULL, &pi->ob);
-       pi->x = obstack_finish(&pi->ob);
-
-       /* Matrix Q
-        * weights for the 'same-color-optimization' target */
-       {
-               unit_t *curr;
-               pi->Q = new_matrix(pi->x_dim, pi->x_dim);
-
-               list_for_each_entry(unit_t, curr, &co->units, units) {
-                       const ir_node *root, *arg;
-                       int rootnr, argnr;
-                       unsigned rootpos, argpos;
-                       int i;
-
-                       root = curr->nodes[0];
-                       rootnr = get_irn_graph_nr(root);
-                       rootpos = pi_get_first_pos(pi, rootnr);
-                       for (i = 1; i < curr->node_count; ++i) {
-                               int weight = -get_weight(root, arg);
-                               arg = curr->nodes[i];
-                               argnr = get_irn_graph_nr(arg);
-                               argpos = pi_get_first_pos(pi, argnr);
-
-                               DBG((dbg, LEVEL_2, "Q[%n, %n] := %d\n", root, arg, weight));
-                               /* for all colors root and arg have in common, set the weight for
-                                * this pair in the objective function matrix Q */
-                               while (rootpos < pi->x_dim && argpos < pi->x_dim &&
-                                               pi->x[rootpos].n == rootnr && pi->x[argpos].n == argnr) {
-                                       if (pi->x[rootpos].c < pi->x[argpos].c)
-                                               ++rootpos;
-                                       else if (pi->x[rootpos].c > pi->x[argpos].c)
-                                               ++argpos;
-                                       else {
-                                               matrix_set(pi->Q, rootpos++, argpos++, weight);
-
-                                               if (weight < pi->minQij) {
-                                                       DBG((dbg, LEVEL_2, "minQij = %d\n", weight));
-                                                       pi->minQij = weight;
-                                               }
-                                               if (weight > pi->maxQij) {
-                                                       DBG((dbg, LEVEL_2, "maxQij = %d\n", weight));
-                                                       pi->maxQij = weight;
-                                               }
-                                       }
-                               }
-                       }
-               }
-       }
-
-       /* Matrix A
-        * knapsack constraint for each node */
-       {
-               int row = 0, col = 0;
-               pi->A = new_matrix(pi->A_dim, pi->x_dim);
-               while (col < pi->x_dim) {
-                       int curr_n = pi->x[col].n;
-                       while (col < pi->x_dim && pi->x[col].n == curr_n) {
-                               DBG((dbg, LEVEL_2, "A[%d, %d] := %d\n", row, col, 1));
-                               matrix_set(pi->A, row, col++, 1);
+static void pi_add_constr_E(problem_instance_t *pi) {
+       unit_t *curr;
+       bitset_t *root_regs, *arg_regs;
+       root_regs = bitset_alloca(pi->co->cls->n_regs);
+       arg_regs = bitset_alloca(pi->co->cls->n_regs);
+
+       /* for all roots of optimization units */
+       list_for_each_entry(unit_t, curr, &pi->co->units, units) {
+               const ir_node *root, *arg;
+               int rootnr, argnr, color;
+               int y_idx, i, cst_counter = 0;
+               char buf[32];
+
+               root = curr->nodes[0];
+               rootnr = get_irn_graph_nr(root);
+               bitset_clear_all(root_regs);
+               arch_get_allocatable_regs(pi->co->env, root, arch_pos_make_out(0), pi->co->cls, root_regs);
+
+               /* for all arguments of root */
+               for (i = 1; i < curr->node_count; ++i) {
+                       arg = curr->nodes[i];
+                       argnr = get_irn_graph_nr(arg);
+                       bitset_clear_all(arg_regs);
+                       arch_get_allocatable_regs(pi->co->env, arg, arch_pos_make_out(0), pi->co->cls, arg_regs);
+
+                       /* Introduce new variable and set factor in objective function */
+                       y_idx = lpp_add_var(pi->curr_lp, NULL, real, get_weight(root, arg));
+
+                       /* For all colors root and arg have in common, add 2 constraints to E */
+                       bitset_and(arg_regs, root_regs);
+                       bitset_foreach(arg_regs, color) {
+                               int root_idx, arg_idx, cst_idx;
+                               mangle_var(buf, 'x', rootnr, color);
+                               root_idx = lpp_get_var_idx(pi->curr_lp, buf);
+                               mangle_var(buf, 'x', argnr, color);
+                               arg_idx = lpp_get_var_idx(pi->curr_lp, buf);
+
+                               /* add root-arg+y <= 1 */
+                               mangle_cst(buf, 'E', cst_counter++);
+                               cst_idx = lpp_add_cst(pi->curr_lp, buf, less, 0);
+                               lpp_set_factor_fast(pi->curr_lp, cst_idx, root_idx, 1);
+                               lpp_set_factor_fast(pi->curr_lp, cst_idx, arg_idx, -1);
+                               lpp_set_factor_fast(pi->curr_lp, cst_idx, y_idx, -1);
+
+                               /* add arg-root+y <= 1 */
+                               mangle_cst(buf, 'E', cst_counter++);
+                               cst_idx = lpp_add_cst(pi->curr_lp, buf, less, 0);
+                               lpp_set_factor_fast(pi->curr_lp, cst_idx, root_idx, -1);
+                               lpp_set_factor_fast(pi->curr_lp, cst_idx, arg_idx, 1);
+                               lpp_set_factor_fast(pi->curr_lp, cst_idx, y_idx, -1);
                        }
-                       ++row;
                }
-               assert(row == pi->A_dim);
        }
+}
 
-       /* Matrix B
-        * interference constraints using exactly those cliques not contained in others. */
-       {
-               int color, expected_clipques = pi->A_dim/3 * MAX_COLORS;
-               pi->B = new_matrix(expected_clipques, pi->x_dim);
-               for (color = 0; color < MAX_COLORS; ++color) {
-                       pi->curr_color = color;
-                       dom_tree_walk_irg(pi->irg, pi_clique_finder, NULL, pi);
-               }
-               pi->B_dim = matrix_get_rowcount(pi->B);
-       }
+/**
+ * Generate the initial problem matrices and vectors.
+ */
+static problem_instance_t *new_pi(const copy_opt_t *co) {
+       problem_instance_t *pi;
 
+       DBG((dbg, LEVEL_1, "Generating new instance...\n"));
+       pi = xcalloc(1, sizeof(*pi));
+       pi->co = co;
+       pi->removed = pset_new_ptr_default();
+       INIT_LIST_HEAD(&pi->simplicials);
+       pi->dilp = new_lpp(co->name, minimize);
+
+       /* problem size reduction */
+       pi_find_simplicials(pi);
+       //TODO dump_ifg_w/o_removed
+
+       pi->curr_lp = pi->dilp;
+
+       /* Matrix A: knapsack constraint for each node */
+       dom_tree_walk_irg(co->irg, pi_add_constr_A, NULL, pi);
+       /* Matrix B: interference constraints using cliques */
+       for (pi->curr_color = 0; pi->curr_color < pi->co->cls->n_regs; ++pi->curr_color)
+               dom_tree_walk_irg(co->irg, pi_add_constr_B, NULL, pi);
+       /* Matrix E weights for the 'same-color-optimization' target */
+       pi_add_constr_E(pi);
        return pi;
 }
 
 /**
- * clean the problem instance
+ * Clean the problem instance
  */
 static void free_pi(problem_instance_t *pi) {
-       del_matrix(pi->Q);
-       del_matrix(pi->A);
-       del_matrix(pi->B);
-       del_set(pi->num2pos);
-       obstack_free(&pi->ob, NULL);
+       DBG((dbg, LEVEL_1, "Free instance...\n"));
+       /* pi->simplicials get freed during apply_solution */
+       free_lpp(pi->dilp);
+       del_pset(pi->removed);
        free(pi);
 }
 
-void co_ilp_opt(copy_opt_t *co) {
-       dbg = firm_dbg_register("ir.be.copyoptilp");
-       firm_dbg_set_mask(dbg, DEBUG_LVL);
+/**
+ * Set starting values for the mip problem according
+ * to the current coloring of the graph.
+ */
+static void pi_set_start_sol(problem_instance_t *pi) {
+       int i;
+       for (i=1; i<=pi->last_x_var; ++i) {
+               int nnr, col;
+               double val;
+               /* get variable name */
+               const char *var_name = lpp_get_var_name(pi->curr_lp, i);
+               /* split into components */
+               if (split_var(var_name, &nnr, &col) == 2) {
+                       assert(get_irn_col(pi->co, get_irn_for_graph_nr(pi->co->irg, nnr)) != -1);
+                       val = (get_irn_col(pi->co, get_irn_for_graph_nr(pi->co->irg, nnr)) == col) ? 1 : 0;
+                       lpp_set_start_value(pi->curr_lp, i, val);
+               } else
+                       assert(0 && "x vars always look like this 'x123_45'");
+       }
+}
 
-       problem_instance_t *pi = new_pi(co);
+/**
+ * Invoke a solver
+ */
+static void pi_solve_ilp(problem_instance_t *pi) {
+       pi_set_start_sol(pi);
+       lpp_solve(pi->curr_lp, 1);
+}
 
-#ifdef DUMP_MATRICES
-       pi_dump_matrices(pi);
-#endif
+/**
+ * Set the color of all simplicial nodes removed form
+ * the graph before transforming it to an ilp.
+ */
+static void pi_set_simplicials(problem_instance_t *pi) {
+       simpl_t *simpl, *tmp;
+       bitset_t *used_cols = bitset_alloca(arch_register_class_n_regs(pi->co->cls));
+
+       /* color the simplicial nodes in right order */
+       list_for_each_entry_safe(simpl_t, simpl, tmp, &pi->simplicials, chain) {
+               int free_col;
+               ir_node *other_irn, *irn;
+               if_node_t *other, *ifn;
+
+               /* get free color by inspecting all neighbors */
+               ifn = simpl->ifn;
+               irn = get_irn_for_graph_nr(pi->co->irg, ifn->nnr);
+               bitset_clear_all(used_cols);
+               foreach_neighb(ifn, other) {
+                       other_irn = get_irn_for_graph_nr(pi->co->irg, other->nnr);
+                       if (!is_removed(other_irn)) /* only inspect nodes which are in graph right now */
+                               bitset_set(used_cols, get_irn_col(pi->co, other_irn));
+               }
 
-#ifdef DUMP_LP
-       pi_dump_lp(pi);
-#endif
+               /* now all bits not set are possible colors */
+               free_col = bitset_next_clear(used_cols, 0);
+               assert(free_col != -1 && "No free color found. This can not be.");
+               set_irn_col(pi->co, irn, free_col);
+               pset_remove_ptr(pi->removed, irn); /* irn is back in graph again */
+               free(simpl);
+       }
+}
 
-#ifdef DUMP_MPS
-       pi_dump_mps(pi);
-#endif
+/**
+ * Sets the colors of irns according to the values of variables
+ * provided by the solution of the solver.
+ */
+static void pi_apply_solution(problem_instance_t *pi) {
+//             else if (vars_section && sscanf(buf, "x%d_%d %d", &num, &col, &val) == 3 && val == 1) {
+//                     set_irn_col(lpp, get_irn_for_graph_nr(lpp->irg, num), col);
+       int i;
+       double *sol;
+       DBG((dbg, LEVEL_1, "Applying solution...\n"));
 
-#ifdef DO_SOLVE
-       pi_solve_ilp(pi);
-       pi_apply_solution(pi);
+#ifdef DO_STAT
+       //TODO
 #endif
 
-#ifdef DELETE_FILES
-       pi_delete_files(pi);
-#endif
+       sol = xmalloc(pi->last_x_var * sizeof(*sol));
+       lpp_get_solution(pi->curr_lp, sol, 1, pi->last_x_var);
+       for (i=0; i<pi->last_x_var; ++i)
+               if (sol[i] == 1) { /* split varibale name into components */
+                       int nnr, col;
+                       const char *var_name = lpp_get_var_name(pi->curr_lp, 1+i);
+                       if (split_var(var_name, &nnr, &col) == 2) {
+                               DBG((dbg, LEVEL_2, " x%d = %d\n", nnr, col));
+                               set_irn_col(pi->co, get_irn_for_graph_nr(pi->co->irg, nnr), col);
+                       } else
+                               assert(0 && "this should be a x-var");
+       }
+       pi_set_simplicials(pi);
+}
+
+void co_ilp_opt(copy_opt_t *co) {
+       problem_instance_t *pi;
+       dbg = firm_dbg_register("ir.be.copyoptilp");
+       if (!strcmp(co->name, DEBUG_IRG))
+               firm_dbg_set_mask(dbg, -1);
+       else
+               firm_dbg_set_mask(dbg, DEBUG_LVL);
 
+       pi = new_pi(co);
+       pi_solve_ilp(pi);
+       pi_apply_solution(pi);
        free_pi(pi);
 }