Added support for SymConst(ofs_ent)
[libfirm] / ir / be / bespillremat.c
1 /** vim: set sw=4 ts=4:
2  * @file   bespillremat.c
3  * @date   2006-04-06
4  * @author Adam M. Szalkowski & Sebastian Hack
5  *
6  * ILP based spilling & rematerialization
7  *
8  * Copyright (C) 2006 Universitaet Karlsruhe
9  * Released under the GPL
10  */
11 #ifdef HAVE_CONFIG_H
12 #include "config.h"
13 #endif
14
15 #ifdef WITH_ILP
16
17 #include <math.h>
18
19 #include "hashptr.h"
20 #include "debug.h"
21 #include "obst.h"
22 #include "set.h"
23 #include "list.h"
24 #include "pmap.h"
25
26 #include "irprintf.h"
27 #include "irgwalk.h"
28 #include "irdump_t.h"
29 #include "irnode_t.h"
30 #include "ircons_t.h"
31 #include "irloop_t.h"
32 #include "phiclass_t.h"
33 #include "iredges.h"
34 #include "execfreq.h"
35 #include "irvrfy.h"
36 #include "irbackedge_t.h"
37
38 #include <lpp/lpp.h>
39 #include <lpp/mps.h>
40 #include <lpp/lpp_net.h>
41 #include <lpp/lpp_cplex.h>
42 //#include <lc_pset.h>
43 //#include <libcore/lc_bitset.h>
44
45 #include "be_t.h"
46 #include "belive_t.h"
47 #include "besched_t.h"
48 #include "beirgmod.h"
49 #include "bearch.h"
50 #include "beabi.h"
51 #include "benode_t.h"
52 #include "beutil.h"
53 #include "bespillremat.h"
54 #include "bespill.h"
55 #include "bepressurestat.h"
56 #include "beprofile.h"
57
58 #include "bechordal_t.h"
59
60 #ifdef WITH_LIBCORE
61 #include <libcore/lc_opts.h>
62 #include <libcore/lc_opts_enum.h>
63 #endif /* WITH_LIBCORE */
64
65 #define DUMP_PROBLEM       1
66 #define DUMP_MPS           2
67 #define DUMP_SOLUTION      4
68 #define DUMP_STATS         8
69 #define DUMP_PRESSURE      16
70
71 #define KEEPALIVE_REMATS   1
72 #define KEEPALIVE_SPILLS   2
73 #define KEEPALIVE_RELOADS  4
74
75 #define VERIFY_MEMINTERF   1
76 #define VERIFY_DOMINANCE   2
77
78 #define REMATS_NONE        0
79 #define REMATS_BRIGGS      1
80 #define REMATS_NOINVERSE   2
81 #define REMATS_ALL         3
82
83 static int opt_dump_flags   = 0;
84 static int opt_log = 0;
85 static int opt_keep_alive   = 0;
86 static int opt_goodwin = 1;
87 static int opt_memcopies = 1;
88 static int opt_memoperands = 1;
89 static int opt_verify = VERIFY_MEMINTERF;
90 static int opt_remats = REMATS_ALL;
91 static int opt_repair_schedule = 0;
92 static int opt_no_enlarge_liveness = 0;
93 static int opt_remat_while_live = 1;
94 static int opt_timeout = 300;
95 static double opt_cost_reload = 8.0;
96 static double opt_cost_memoperand =  7.0;
97 static double opt_cost_spill =  50.0;
98 static double opt_cost_remat =  1.0;
99
100
101 #ifdef WITH_LIBCORE
102 static const lc_opt_enum_mask_items_t dump_items[] = {
103         { "problem",  DUMP_PROBLEM  },
104         { "mps",      DUMP_MPS      },
105         { "solution", DUMP_SOLUTION },
106         { "stats",    DUMP_STATS },
107         { "pressure", DUMP_PRESSURE },
108         { NULL,       0 }
109 };
110
111 static lc_opt_enum_mask_var_t dump_var = {
112         &opt_dump_flags, dump_items
113 };
114
115 static const lc_opt_enum_mask_items_t keepalive_items[] = {
116         { "remats",  KEEPALIVE_REMATS  },
117         { "spills",  KEEPALIVE_SPILLS  },
118         { "reloads", KEEPALIVE_RELOADS },
119         { NULL,      0 }
120 };
121
122 static lc_opt_enum_mask_var_t keep_alive_var = {
123         &opt_keep_alive, keepalive_items
124 };
125
126 static const lc_opt_enum_mask_items_t remats_items[] = {
127         { "none",      REMATS_NONE      },
128         { "briggs",    REMATS_BRIGGS    },
129         { "noinverse", REMATS_NOINVERSE },
130         { "all",       REMATS_ALL       },
131         { NULL,        0 }
132 };
133
134 static lc_opt_enum_mask_var_t remats_var = {
135         &opt_remats, remats_items
136 };
137
138 static const lc_opt_table_entry_t options[] = {
139         LC_OPT_ENT_ENUM_MASK("keepalive", "keep alive remats, spills or reloads",                   &keep_alive_var),
140
141         LC_OPT_ENT_BOOL     ("goodwin",  "activate goodwin reduction",                              &opt_goodwin),
142         LC_OPT_ENT_BOOL     ("memcopies",  "activate memcopy handling",                             &opt_memcopies),
143         LC_OPT_ENT_BOOL     ("memoperands",  "activate memoperands",                                &opt_memoperands),
144         LC_OPT_ENT_ENUM_INT ("remats",  "type of remats to insert (none, briggs, noinverse or all)",&remats_var),
145         LC_OPT_ENT_BOOL     ("repair_schedule",  "repair the schedule by rematting once used nodes",&opt_repair_schedule),
146         LC_OPT_ENT_BOOL     ("no_enlage_liveness",  "do not enlarge liveness of operands of remats",&opt_no_enlarge_liveness),
147         LC_OPT_ENT_BOOL     ("remat_while_live",  "only remat where rematted value was live",       &opt_remat_while_live),
148
149         LC_OPT_ENT_ENUM_MASK("dump", "dump problem, mps, solution, stats or pressure",              &dump_var),
150         LC_OPT_ENT_BOOL     ("log",  "activate the lpp log",                                        &opt_log),
151         LC_OPT_ENT_INT      ("timeout",  "ILP solver timeout",                                      &opt_timeout),
152
153         LC_OPT_ENT_DBL      ("cost_reload",  "cost of a reload",                                    &opt_cost_reload),
154         LC_OPT_ENT_DBL      ("cost_memoperand",  "cost of a memory operand",                        &opt_cost_memoperand),
155         LC_OPT_ENT_DBL      ("cost_spill",  "cost of a spill instruction",                          &opt_cost_spill),
156         LC_OPT_ENT_DBL      ("cost_remat",  "cost of a rematerialization",                          &opt_cost_remat),
157         { NULL }
158 };
159
160 void be_spill_remat_register_options(lc_opt_entry_t *grp)
161 {
162         lc_opt_entry_t *my_grp = lc_opt_get_grp(grp, "remat");
163         lc_opt_add_table(my_grp, options);
164 }
165 #endif
166
167
168 //#define EXECFREQ_LOOPDEPH   /* compute execution frequency from loop depth only */
169 //#define SCHEDULE_PHIM   /* insert phim nodes into schedule */
170
171 #define  SOLVE
172 //#define  SOLVE_LOCAL
173 #define LPP_SERVER "i44pc52"
174 #define LPP_SOLVER "cplex"
175
176
177 #define MAX_PATHS      INT_MAX
178 #define ILP_UNDEF               -1
179
180 typedef struct _spill_ilp_t {
181         const arch_register_class_t  *cls;
182         int                           n_regs;
183         const be_chordal_env_t       *chordal_env;
184         be_lv_t                      *lv;
185         lpp_t                        *lpp;
186         struct obstack               *obst;
187         set                          *remat_info;
188         pset                         *all_possible_remats;
189         pset                         *inverse_ops;
190         ir_node                      *keep;
191         set                          *values; /**< for collecting all definitions of values before running ssa-construction */
192         pset                         *spills;
193         set                          *interferences;
194         ir_node                      *m_unknown;
195         set                          *memoperands;
196 #ifndef SCHEDULE_PHIM
197         pset                         *phims;
198 #endif
199         DEBUG_ONLY(firm_dbg_module_t * dbg);
200 } spill_ilp_t;
201
202 typedef int ilp_var_t;
203 typedef int ilp_cst_t;
204
205 typedef struct _spill_bb_t {
206         set      *ilp;
207         set      *reloads;
208 } spill_bb_t;
209
210 typedef struct _remat_t {
211         const ir_node        *op;      /**< for copy_irn */
212         const ir_node        *value;   /**< the value which is being recomputed by this remat */
213         const ir_node        *proj;    /**< not NULL if the above op produces a tuple */
214         int                   cost;    /**< cost of this remat */
215         int                   inverse; /**< nonzero if this is an inverse remat */
216 } remat_t;
217
218 /**
219  * Data to be attached to each IR node. For remats this contains the ilp_var
220  * for this remat and for normal ops this contains the ilp_vars for
221  * reloading each operand
222  */
223 typedef struct _op_t {
224         int             is_remat;
225         union {
226                 struct {
227                         ilp_var_t       ilp;
228                         const remat_t  *remat; /** the remat this op belongs to */
229                         int             pre; /** 1, if this is a pressure-increasing remat */
230                 } remat;
231                 struct {
232                         ilp_var_t       ilp;
233                         ir_node        *op; /** the operation this live range belongs to */
234                         union {
235                                 ilp_var_t      *reloads;
236                                 ilp_var_t      *copies;
237                         } args;
238                 } live_range;
239         } attr;
240 } op_t;
241
242 typedef struct _defs_t {
243         const ir_node   *value;
244         ir_node         *spills;  /**< points to the first spill for this value (linked by link field) */
245         ir_node         *remats;  /**< points to the first definition for this value (linked by link field) */
246 } defs_t;
247
248 typedef struct _remat_info_t {
249         const ir_node       *irn; /**< the irn to which these remats belong */
250         pset                *remats; /**< possible remats for this value */
251         pset                *remats_by_operand; /**< remats with this value as operand */
252 } remat_info_t;
253
254 typedef struct _keyval_t {
255         const void          *key;
256         const void          *val;
257 } keyval_t;
258
259 typedef struct _spill_t {
260         ir_node            *irn;
261         ilp_var_t           reg_in;
262         ilp_var_t           mem_in;
263         ilp_var_t           reg_out;
264         ilp_var_t           mem_out;
265         ilp_var_t           spill;
266 } spill_t;
267
268 typedef struct _memoperand_t {
269         ir_node             *irn; /**< the irn */
270         unsigned int         pos; /**< the position of the argument */
271         ilp_var_t            ilp; /**< the ilp var for this memory operand */
272 } memoperand_t;
273
274 static INLINE int
275 has_reg_class(const spill_ilp_t * si, const ir_node * irn)
276 {
277         return chordal_has_class(si->chordal_env, irn);
278 }
279
280 #if 0
281 static int
282 cmp_remat(const void *a, const void *b)
283 {
284         const keyval_t *p = a;
285         const keyval_t *q = b;
286         const remat_t  *r = p->val;
287         const remat_t  *s = q->val;
288
289         assert(r && s);
290
291         return !(r == s || r->op == s->op);
292 }
293 #endif
294 static int
295 cmp_remat(const void *a, const void *b)
296 {
297         const remat_t  *r = a;
298         const remat_t  *s = a;
299
300         return !(r == s || r->op == s->op);
301 }
302
303 static int
304 cmp_spill(const void *a, const void *b, size_t size)
305 {
306         const spill_t *p = a;
307         const spill_t *q = b;
308
309 //      return !(p->irn == q->irn && p->bb == q->bb);
310         return !(p->irn == q->irn);
311 }
312
313 static int
314 cmp_memoperands(const void *a, const void *b, size_t size)
315 {
316         const memoperand_t *p = a;
317         const memoperand_t *q = b;
318
319         return !(p->irn == q->irn && p->pos == q->pos);
320 }
321
322 static keyval_t *
323 set_find_keyval(set * set, const void * key)
324 {
325         keyval_t     query;
326
327         query.key = key;
328         return set_find(set, &query, sizeof(query), HASH_PTR(key));
329 }
330
331 static keyval_t *
332 set_insert_keyval(set * set, void * key, void * val)
333 {
334         keyval_t     query;
335
336         query.key = key;
337         query.val = val;
338         return set_insert(set, &query, sizeof(query), HASH_PTR(key));
339 }
340
341 static defs_t *
342 set_find_def(set * set, const ir_node * value)
343 {
344         defs_t     query;
345
346         query.value = value;
347         return set_find(set, &query, sizeof(query), HASH_PTR(value));
348 }
349
350 static defs_t *
351 set_insert_def(set * set, const ir_node * value)
352 {
353         defs_t     query;
354
355         query.value = value;
356         query.spills = NULL;
357         query.remats = NULL;
358         return set_insert(set, &query, sizeof(query), HASH_PTR(value));
359 }
360
361 static memoperand_t *
362 set_insert_memoperand(set * set, ir_node * irn, unsigned int pos, ilp_var_t ilp)
363 {
364         memoperand_t     query;
365
366         query.irn = irn;
367         query.pos = pos;
368         query.ilp = ilp;
369         return set_insert(set, &query, sizeof(query), HASH_PTR(irn)+pos);
370 }
371
372 static memoperand_t *
373 set_find_memoperand(set * set, const ir_node * irn, unsigned int pos)
374 {
375         memoperand_t     query;
376
377         query.irn = (ir_node*)irn;
378         query.pos = pos;
379         return set_find(set, &query, sizeof(query), HASH_PTR(irn)+pos);
380 }
381
382
383 static spill_t *
384 set_find_spill(set * set, const ir_node * value)
385 {
386         spill_t     query;
387
388         query.irn = (ir_node*)value;
389         return set_find(set, &query, sizeof(query), HASH_PTR(value));
390 }
391
392 #define pset_foreach(s,i) for((i)=pset_first((s)); (i); (i)=pset_next((s)))
393 #define set_foreach(s,i) for((i)=set_first((s)); (i); (i)=set_next((s)))
394 #define foreach_post_remat(s,i) for((i)=next_post_remat((s)); (i); (i)=next_post_remat((i)))
395 #define foreach_pre_remat(si,s,i) for((i)=next_pre_remat((si),(s)); (i); (i)=next_pre_remat((si),(i)))
396 #define sched_foreach_op(s,i) for((i)=sched_next_op((s));!sched_is_end((i));(i)=sched_next_op((i)))
397
398 static int
399 cmp_remat_info(const void *a, const void *b, size_t size)
400 {
401         const remat_info_t *p = a;
402         const remat_info_t *q = b;
403
404         return !(p->irn == q->irn);
405 }
406
407 static int
408 cmp_defs(const void *a, const void *b, size_t size)
409 {
410         const defs_t *p = a;
411         const defs_t *q = b;
412
413         return !(p->value == q->value);
414 }
415
416 static int
417 cmp_keyval(const void *a, const void *b, size_t size)
418 {
419         const keyval_t *p = a;
420         const keyval_t *q = b;
421
422         return !(p->key == q->key);
423 }
424
425 static double
426 execution_frequency(const spill_ilp_t *si, const ir_node * irn)
427 {
428 #define FUDGE 0.001
429         if(be_profile_has_data())
430                 return ((double)be_profile_get_block_execcount(get_block(irn))) + FUDGE;
431
432 #ifndef EXECFREQ_LOOPDEPH
433         return get_block_execfreq(si->chordal_env->exec_freq, get_block(irn)) + FUDGE;
434 #else
435         if(is_Block(irn))
436                 return exp(get_loop_depth(get_irn_loop(irn)) * log(10)) + FUDGE;
437         else
438                 return exp(get_loop_depth(get_irn_loop(get_nodes_block(irn))) * log(10)) + FUDGE;
439 #endif
440 }
441
442 static double
443 get_cost(const spill_ilp_t * si, const ir_node * irn)
444 {
445         if(be_is_Spill(irn)) {
446                 return opt_cost_spill;
447         } else if(be_is_Reload(irn)){
448                 return opt_cost_reload;
449         } else {
450                 return arch_get_op_estimated_cost(si->chordal_env->birg->main_env->arch_env, irn);
451         }
452 }
453
454 /**
455  * Checks, whether node and its operands have suitable reg classes
456  */
457 static INLINE int
458 is_rematerializable(const spill_ilp_t * si, const ir_node * irn)
459 {
460         int               n;
461         const arch_env_t *arch_env = si->chordal_env->birg->main_env->arch_env;
462         int               remat = (arch_irn_get_flags(arch_env, irn) & arch_irn_flags_rematerializable) != 0;
463
464 #if 0
465         if(!remat)
466                 ir_fprintf(stderr, "  Node %+F is not rematerializable\n", irn);
467 #endif
468
469         for (n = get_irn_arity(irn)-1; n>=0 && remat; --n) {
470                 ir_node        *op = get_irn_n(irn, n);
471                 remat &= has_reg_class(si, op) || arch_irn_get_flags(arch_env, op) & arch_irn_flags_ignore || (get_irn_op(op) == op_NoMem);
472
473 //              if(!remat)
474 //                      ir_fprintf(stderr, "  Argument %d (%+F) of Node %+F has wrong regclass\n", i, op, irn);
475         }
476
477         return remat;
478 }
479
480 /**
481  * Try to create a remat from @p op with destination value @p dest_value
482  */
483 static INLINE remat_t *
484 get_remat_from_op(spill_ilp_t * si, const ir_node * dest_value, const ir_node * op)
485 {
486         remat_t  *remat = NULL;
487
488 //      if(!mode_is_datab(get_irn_mode(dest_value)))
489 //              return NULL;
490
491         if(dest_value == op) {
492                 const ir_node *proj = NULL;
493
494                 if(is_Proj(dest_value)) {
495                         op = get_Proj_pred(op);
496                         proj = dest_value;
497                 }
498
499                 if(!is_rematerializable(si, op))
500                         return NULL;
501
502                 remat          = obstack_alloc(si->obst, sizeof(*remat));
503                 remat->op      = op;
504                 remat->cost    = get_cost(si, op);
505                 remat->value   = dest_value;
506                 remat->proj    = proj;
507                 remat->inverse = 0;
508         } else {
509                 arch_inverse_t     inverse;
510                 int                n;
511
512                 /* get the index of the operand we want to retrieve by the inverse op */
513                 for (n = get_irn_arity(op)-1; n>=0; --n) {
514                         ir_node        *arg = get_irn_n(op, n);
515
516                         if(arg == dest_value) break;
517                 }
518                 if(n<0) return NULL;
519
520                 DBG((si->dbg, LEVEL_5, "\t  requesting inverse op for argument %d of op %+F\n", n, op));
521
522                 /* else ask the backend to give an inverse op */
523                 if(arch_get_inverse(si->chordal_env->birg->main_env->arch_env, op, n, &inverse, si->obst)) {
524                         int   i;
525
526                         DBG((si->dbg, LEVEL_4, "\t  backend gave us an inverse op with %d nodes and cost %d\n", inverse.n, inverse.costs));
527
528                         assert(inverse.n > 0 && "inverse op should have at least one node");
529
530                         for(i=inverse.n-1; i>=0; --i) {
531                                 pset_insert_ptr(si->inverse_ops, inverse.nodes[i]);
532                         }
533
534                         if(inverse.n <= 2) {
535                                 remat = obstack_alloc(si->obst, sizeof(*remat));
536                                 remat->op = inverse.nodes[0];
537                                 remat->cost = inverse.costs;
538                                 remat->value = dest_value;
539                                 remat->proj = (inverse.n==2)?inverse.nodes[1]:NULL;
540                                 remat->inverse = 1;
541
542                                 assert(is_Proj(remat->proj));
543                         } else {
544                                 assert(0 && "I can not handle remats with more than 2 nodes");
545                         }
546                 }
547         }
548
549         if(remat) {
550                 if(remat->proj) {
551                         DBG((si->dbg, LEVEL_3, "\t >Found remat %+F for %+F from %+F with %+F\n", remat->op, dest_value, op, remat->proj));
552                 } else {
553                         DBG((si->dbg, LEVEL_3, "\t >Found remat %+F for %+F from %+F\n", remat->op, dest_value, op));
554                 }
555         }
556         return remat;
557 }
558
559
560 static INLINE void
561 add_remat(const spill_ilp_t * si, const remat_t * remat)
562 {
563         remat_info_t    *remat_info,
564                      query;
565         int              n;
566
567         assert(remat->op);
568         assert(remat->value);
569
570         query.irn = remat->value;
571         query.remats = NULL;
572         query.remats_by_operand = NULL;
573         remat_info = set_insert(si->remat_info, &query, sizeof(query), HASH_PTR(remat->value));
574
575         if(remat_info->remats == NULL) {
576                 remat_info->remats = new_pset(cmp_remat, 4096);
577         }
578         pset_insert(remat_info->remats, remat, HASH_PTR(remat->op));
579
580         /* insert the remat into the remats_be_operand set of each argument of the remat op */
581         for (n = get_irn_arity(remat->op)-1; n>=0; --n) {
582                 ir_node        *arg = get_irn_n(remat->op, n);
583
584                 query.irn = arg;
585                 query.remats = NULL;
586                 query.remats_by_operand = NULL;
587                 remat_info = set_insert(si->remat_info, &query, sizeof(query), HASH_PTR(arg));
588
589                 if(remat_info->remats_by_operand == NULL) {
590                         remat_info->remats_by_operand = new_pset(cmp_remat, 4096);
591                 }
592                 pset_insert(remat_info->remats_by_operand, remat, HASH_PTR(remat->op));
593         }
594 }
595
596 static int
597 get_irn_n_nonremat_edges(const spill_ilp_t * si, const ir_node * irn)
598 {
599         const ir_edge_t   *edge = get_irn_out_edge_first(irn);
600         int                i = 0;
601
602         while(edge) {
603                 if(!pset_find_ptr(si->inverse_ops, edge->src)) {
604                         ++i;
605                 }
606                 edge = get_irn_out_edge_next(irn, edge);
607         }
608
609         return i;
610 }
611
612 static int
613 get_irn_n_nonignore_args(const spill_ilp_t * si, const ir_node * irn)
614 {
615         int n;
616         int ret = 0;
617
618         if(is_Proj(irn))
619                 irn = get_Proj_pred(irn);
620
621         for(n=get_irn_arity(irn)-1; n>=0; --n) {
622                 const ir_node  *arg = get_irn_n(irn, n);
623
624                 if(has_reg_class(si, arg)) ++ret;
625         }
626
627         return ret;
628 }
629
630 static INLINE void
631 get_remats_from_op(spill_ilp_t * si, const ir_node * op)
632 {
633         int      n;
634         remat_t *remat;
635
636         if( has_reg_class(si, op)
637         && (opt_repair_schedule || get_irn_n_nonremat_edges(si, op) > 1)
638         && (opt_remats !=  REMATS_BRIGGS || get_irn_n_nonignore_args(si, op) == 0)
639         ) {
640                 remat = get_remat_from_op(si, op, op);
641                 if(remat) {
642                         add_remat(si, remat);
643                 }
644         }
645
646         if(opt_remats == REMATS_ALL) {
647                 /* repeat the whole stuff for each remat retrieved by get_remat_from_op(op, arg)
648                    for each arg */
649                 for (n = get_irn_arity(op)-1; n>=0; --n) {
650                         ir_node        *arg = get_irn_n(op, n);
651
652                         if(has_reg_class(si, arg)) {
653                                 /* try to get an inverse remat */
654                                 remat = get_remat_from_op(si, arg, op);
655                                 if(remat) {
656                                         add_remat(si, remat);
657                                 }
658                         }
659                 }
660         }
661 }
662
663 static INLINE int
664 value_is_defined_before(const spill_ilp_t * si, const ir_node * pos, const ir_node * val)
665 {
666         ir_node *block;
667         ir_node *def_block = get_nodes_block(val);
668         int      ret;
669
670         if(val == pos)
671                 return 0;
672
673         /* if pos is at end of a basic block */
674         if(is_Block(pos)) {
675                 ret = (pos == def_block || block_dominates(def_block, pos));
676 //              ir_fprintf(stderr, "(def(bb)=%d) ", ret);
677                 return ret;
678         }
679
680         /* else if this is a normal operation */
681         block = get_nodes_block(pos);
682         if(block == def_block) {
683                 if(!sched_is_scheduled(val)) return 1;
684
685                 ret = sched_comes_after(val, pos);
686 //              ir_fprintf(stderr, "(def(same block)=%d) ",ret);
687                 return ret;
688         }
689
690         ret = block_dominates(def_block, block);
691 //      ir_fprintf(stderr, "(def(other block)=%d) ", ret);
692         return ret;
693 }
694
695 static INLINE ir_node *
696 sched_block_last_noncf(const spill_ilp_t * si, const ir_node * bb)
697 {
698     return sched_skip((ir_node*)bb, 0, sched_skip_cf_predicator, (void *) si->chordal_env->birg->main_env->arch_env);
699 }
700
701 /**
702  * Returns first non-Phi node of block @p bb
703  */
704 static INLINE ir_node *
705 sched_block_first_nonphi(const ir_node * bb)
706 {
707         return sched_skip((ir_node*)bb, 1, sched_skip_phi_predicator, NULL);
708 }
709
710 static int
711 sched_skip_proj_predicator(const ir_node * irn, void * data)
712 {
713         return (is_Proj(irn));
714 }
715
716 static INLINE ir_node *
717 sched_next_nonproj(const ir_node * irn, int forward)
718 {
719         return sched_skip((ir_node*)irn, forward, sched_skip_proj_predicator, NULL);
720 }
721
722 /**
723  * Returns next operation node (non-Proj) after @p irn
724  * or the basic block of this node
725  */
726 static INLINE ir_node *
727 sched_next_op(const ir_node * irn)
728 {
729         ir_node *next = sched_next(irn);
730
731         if(is_Block(next))
732                 return next;
733
734         return sched_next_nonproj(next, 1);
735 }
736
737 /**
738  * Returns previous operation node (non-Proj) before @p irn
739  * or the basic block of this node
740  */
741 static INLINE ir_node *
742 sched_prev_op(const ir_node * irn)
743 {
744         ir_node *prev = sched_prev(irn);
745
746         if(is_Block(prev))
747                 return prev;
748
749         return sched_next_nonproj(prev, 0);
750 }
751
752 static void
753 sched_put_after(ir_node * insert, ir_node * irn)
754 {
755         if(is_Block(insert)) {
756                 insert = sched_block_first_nonphi(insert);
757         } else {
758                 insert = sched_next_op(insert);
759         }
760         sched_add_before(insert, irn);
761 }
762
763 static void
764 sched_put_before(const spill_ilp_t * si, ir_node * insert, ir_node * irn)
765 {
766   if(is_Block(insert)) {
767           insert = sched_block_last_noncf(si, insert);
768   } else {
769           insert = sched_next_nonproj(insert, 0);
770           insert = sched_prev(insert);
771   }
772   sched_add_after(insert, irn);
773 }
774
775 static ir_node *
776 next_post_remat(const ir_node * irn)
777 {
778         op_t      *op;
779     ir_node   *next;
780
781         if(is_Block(irn)) {
782                 next = sched_block_first_nonphi(irn);
783         } else {
784                 next = sched_next_op(irn);
785         }
786
787         if(sched_is_end(next))
788                 return NULL;
789
790         op = get_irn_link(next);
791         if(op->is_remat && !op->attr.remat.pre) {
792                 return next;
793         }
794
795         return NULL;
796 }
797
798
799 static ir_node *
800 next_pre_remat(const spill_ilp_t * si, const ir_node * irn)
801 {
802         op_t      *op;
803         ir_node   *ret;
804
805         if(is_Block(irn)) {
806                 ret = sched_block_last_noncf(si, irn);
807                 ret = sched_next(ret);
808                 ret = sched_prev_op(ret);
809         } else {
810                 ret = sched_prev_op(irn);
811         }
812
813         if(sched_is_end(ret) || is_Phi(ret))
814                 return NULL;
815
816         op = (op_t*)get_irn_link(ret);
817         if(op->is_remat && op->attr.remat.pre) {
818                 return ret;
819         }
820
821         return NULL;
822 }
823
824 /**
825  * Tells you whether a @p remat can be placed before the irn @p pos
826  */
827 static INLINE int
828 can_remat_before(const spill_ilp_t * si, const remat_t * remat, const ir_node * pos, const pset * live)
829 {
830         const ir_node   *op = remat->op;
831         const ir_node   *prev;
832         int        n,
833                            res = 1;
834
835         if(is_Block(pos)) {
836                 prev = sched_block_last_noncf(si, pos);
837                 prev = sched_next_nonproj(prev, 0);
838         } else {
839                 prev = sched_prev_op(pos);
840         }
841         /* do not remat if the rematted value is defined immediately before this op */
842         if(prev == remat->op) {
843                 return 0;
844         }
845
846 #if 0
847         /* this should be just fine, the following OP will be using this value, right? */
848
849         /* only remat AFTER the real definition of a value (?) */
850         if(!value_is_defined_before(si, pos, remat->value)) {
851 //              ir_fprintf(stderr, "error(not defined)");
852                 return 0;
853         }
854 #endif
855
856         for(n=get_irn_arity(op)-1; n>=0 && res; --n) {
857                 const ir_node   *arg = get_irn_n(op, n);
858
859                 if(opt_no_enlarge_liveness) {
860                         if(has_reg_class(si, arg) && live) {
861                                 res &= pset_find_ptr((pset*)live, arg)?1:0;
862                         } else {
863                                 res &= value_is_defined_before(si, pos, arg);
864                         }
865                 } else {
866                         res &= value_is_defined_before(si, pos, arg);
867                 }
868         }
869
870         return res;
871 }
872
873 /**
874  * Tells you whether a @p remat can be placed after the irn @p pos
875  */
876 static INLINE int
877 can_remat_after(const spill_ilp_t * si, const remat_t * remat, const ir_node * pos, const pset * live)
878 {
879         if(is_Block(pos)) {
880                 pos = sched_block_first_nonphi(pos);
881         } else {
882                 pos = sched_next_op(pos);
883         }
884
885         /* only remat AFTER the real definition of a value (?) */
886         if(!value_is_defined_before(si, pos, remat->value)) {
887                 return 0;
888         }
889
890         return can_remat_before(si, remat, pos, live);
891 }
892
893 /**
894  * Collect potetially rematerializable OPs
895  */
896 static void
897 walker_remat_collector(ir_node * irn, void * data)
898 {
899         spill_ilp_t    *si = data;
900
901         if(!is_Block(irn) && !is_Phi(irn)) {
902                 DBG((si->dbg, LEVEL_4, "\t  Processing %+F\n", irn));
903                 get_remats_from_op(si, irn);
904         }
905 }
906
907 /**
908  * Inserts a copy of @p irn before @p pos
909  */
910 static ir_node *
911 insert_copy_before(const spill_ilp_t * si, const ir_node * irn, ir_node * pos)
912 {
913         ir_node     *bb;
914         ir_node     *copy;
915
916         bb = is_Block(pos)?pos:get_nodes_block(pos);
917         copy = exact_copy(irn);
918
919         _set_phi_class(copy, NULL);
920         set_nodes_block(copy, bb);
921         sched_put_before(si, pos, copy);
922
923         return copy;
924 }
925
926 /**
927  * Inserts a copy of @p irn after @p pos
928  */
929 static ir_node *
930 insert_copy_after(const spill_ilp_t * si, const ir_node * irn, ir_node * pos)
931 {
932         ir_node     *bb;
933         ir_node     *copy;
934
935         bb = is_Block(pos)?pos:get_nodes_block(pos);
936         copy = exact_copy(irn);
937
938         _set_phi_class(copy, NULL);
939         set_nodes_block(copy, bb);
940         sched_put_after(pos, copy);
941
942         return copy;
943 }
944
945 static ir_node *
946 insert_remat_after(spill_ilp_t * si, const remat_t * remat, ir_node * pos, const pset * live)
947 {
948         char     buf[256];
949
950         if(can_remat_after(si, remat, pos, live)) {
951                 ir_node         *copy,
952                                                 *proj_copy;
953                 op_t            *op;
954
955                 DBG((si->dbg, LEVEL_3, "\t  >inserting remat2 %+F\n", remat->op));
956
957                 copy = insert_copy_after(si, remat->op, pos);
958
959                 ir_snprintf(buf, sizeof(buf), "remat2_%N_%N", copy, pos);
960                 op = obstack_alloc(si->obst, sizeof(*op));
961                 op->is_remat = 1;
962                 op->attr.remat.remat = remat;
963                 op->attr.remat.pre = 0;
964                 op->attr.remat.ilp = lpp_add_var_default(si->lpp, buf, lpp_binary, remat->cost*execution_frequency(si, pos), 0.0);
965
966                 set_irn_link(copy, op);
967                 pset_insert_ptr(si->all_possible_remats, copy);
968                 if(remat->proj) {
969                         proj_copy = insert_copy_after(si, remat->proj, copy);
970                         set_irn_n(proj_copy, 0, copy);
971                         set_irn_link(proj_copy, op);
972                         pset_insert_ptr(si->all_possible_remats, proj_copy);
973                 } else {
974                         proj_copy = NULL;
975                 }
976
977                 return copy;
978         }
979
980         return NULL;
981 }
982
983 static ir_node *
984 insert_remat_before(spill_ilp_t * si, const remat_t * remat, ir_node * pos, const pset * live)
985 {
986         char     buf[256];
987
988         if(can_remat_before(si, remat, pos, live)) {
989                 ir_node         *copy,
990                                                 *proj_copy;
991                 op_t            *op;
992
993                 DBG((si->dbg, LEVEL_3, "\t  >inserting remat %+F\n", remat->op));
994
995                 copy = insert_copy_before(si, remat->op, pos);
996
997                 ir_snprintf(buf, sizeof(buf), "remat_%N_%N", copy, pos);
998                 op = obstack_alloc(si->obst, sizeof(*op));
999                 op->is_remat = 1;
1000                 op->attr.remat.remat = remat;
1001                 op->attr.remat.pre = 1;
1002                 op->attr.remat.ilp = lpp_add_var_default(si->lpp, buf, lpp_binary, remat->cost*execution_frequency(si, pos), 0.0);
1003
1004                 set_irn_link(copy, op);
1005                 pset_insert_ptr(si->all_possible_remats, copy);
1006                 if(remat->proj) {
1007                         proj_copy = insert_copy_after(si, remat->proj, copy);
1008                         set_irn_n(proj_copy, 0, copy);
1009                         set_irn_link(proj_copy, op);
1010                         pset_insert_ptr(si->all_possible_remats, proj_copy);
1011                 } else {
1012                         proj_copy = NULL;
1013                 }
1014
1015                 return copy;
1016         }
1017
1018         return NULL;
1019 }
1020
1021 static int
1022 get_block_n_succs(const ir_node *block) {
1023         const ir_edge_t *edge;
1024
1025         assert(edges_activated(current_ir_graph));
1026
1027         edge = get_block_succ_first(block);
1028         if (! edge)
1029                 return 0;
1030
1031         edge = get_block_succ_next(block, edge);
1032         return edge ? 2 : 1;
1033 }
1034
1035 static int
1036 is_start_block(const ir_node * bb)
1037 {
1038         return get_irg_start_block(get_irn_irg(bb)) == bb;
1039 }
1040
1041 static int
1042 is_merge_edge(const ir_node * bb)
1043 {
1044         if(is_start_block(bb))
1045                 return 0;
1046
1047         if(opt_goodwin)
1048                 return get_block_n_succs(bb) == 1;
1049         else
1050                 return 1;
1051 }
1052
1053 static int
1054 is_diverge_edge(const ir_node * bb)
1055 {
1056         if(is_start_block(bb))
1057                 return 0;
1058
1059         if(opt_goodwin)
1060                 return get_Block_n_cfgpreds(bb) == 1;
1061         else
1062                 return 1;
1063 }
1064
1065 static void
1066 get_live_end(spill_ilp_t * si, ir_node * bb, pset * live)
1067 {
1068         ir_node        *irn;
1069         int i;
1070
1071         be_lv_foreach(si->lv, bb, be_lv_state_end, i) {
1072                 irn = be_lv_get_irn(si->lv, bb, i);
1073
1074                 if (has_reg_class(si, irn) && !pset_find_ptr(si->all_possible_remats, irn)) {
1075                         pset_insert_ptr(live, irn);
1076                 }
1077         }
1078
1079         irn = sched_last(bb);
1080
1081         /* all values eaten by control flow operations are also live until the end of the block */
1082         sched_foreach_reverse(bb, irn) {
1083                 int  i;
1084
1085                 if(!sched_skip_cf_predicator(irn, si->chordal_env->birg->main_env->arch_env)) break;
1086
1087                 for(i=get_irn_arity(irn)-1; i>=0; --i) {
1088                         ir_node *arg = get_irn_n(irn,i);
1089
1090                         if(has_reg_class(si, arg)) {
1091                                 pset_insert_ptr(live, arg);
1092                         }
1093                 }
1094         }
1095         /*
1096          * find values that are used by remats at end of block
1097          * and insert them into live set
1098          */
1099         foreach_pre_remat(si, bb, irn) {
1100                 int       n;
1101
1102                 for (n=get_irn_arity(irn)-1; n>=0; --n) {
1103                         ir_node        *remat_arg = get_irn_n(irn, n);
1104
1105                         if(!has_reg_class(si, remat_arg)) continue;
1106
1107                         /* if value is becoming live through use by remat */
1108                         if(!pset_find_ptr(live, remat_arg)) {
1109                                 DBG((si->dbg, LEVEL_4, "  value %+F becoming live through use by remat at end of block %+F\n", remat_arg, irn));
1110
1111                                 pset_insert_ptr(live, remat_arg);
1112                         }
1113                 }
1114         }
1115 }
1116
1117 static void
1118 walker_regclass_copy_insertor(ir_node * irn, void * data)
1119 {
1120         spill_ilp_t    *si = data;
1121
1122         if(is_Phi(irn) && has_reg_class(si, irn)) {
1123                 int n;
1124
1125                 for(n=get_irn_arity(irn)-1; n>=0; --n) {
1126                         ir_node  *phi_arg = get_irn_n(irn, n);
1127                         ir_node  *bb = get_Block_cfgpred_block(get_nodes_block(irn), n);
1128
1129                         if(!has_reg_class(si, phi_arg)) {
1130                                 ir_node   *copy = be_new_Copy(si->cls, si->chordal_env->irg, bb, phi_arg);
1131                                 ir_node   *pos = sched_block_last_noncf(si, bb);
1132                                 op_t      *op = obstack_alloc(si->obst, sizeof(*op));
1133
1134                                 DBG((si->dbg, LEVEL_2, "\t copy to my regclass for arg %+F of %+F\n", phi_arg, irn));
1135                                 sched_add_after(pos, copy);
1136                                 set_irn_n(irn, n, copy);
1137
1138                                 op->is_remat = 0;
1139                                 op->attr.live_range.args.reloads = NULL;
1140                                 op->attr.live_range.ilp = ILP_UNDEF;
1141                                 set_irn_link(copy, op);
1142                         }
1143                 }
1144         }
1145 }
1146
1147 /**
1148  * Insert (so far unused) remats into the irg to
1149  * recompute the potential liveness of all values
1150  */
1151 static void
1152 walker_remat_insertor(ir_node * bb, void * data)
1153 {
1154         spill_ilp_t    *si = data;
1155         ir_node        *irn;
1156         int             n, i;
1157         pset           *live;
1158         pset           *post_remats;
1159         remat_t        *remat;
1160
1161         /* skip start block, no remats to do there */
1162         if(is_start_block(bb)) return;
1163
1164         DBG((si->dbg, LEVEL_3, "\t Entering %+F\n\n", bb));
1165
1166         live = pset_new_ptr_default();
1167         be_lv_foreach(si->lv, bb, be_lv_state_end, i) {
1168                 ir_node        *value = be_lv_get_irn(si->lv, bb, i);
1169
1170                 /* add remats at end of block */
1171                 if (has_reg_class(si, value)) {
1172                         pset_insert_ptr(live, value);
1173                 }
1174         }
1175
1176         irn = sched_last(bb);
1177         while(!sched_is_end(irn)) {
1178                 ir_node   *next;
1179                 pset      *args;
1180                 ir_node   *arg;
1181                 pset      *used;
1182
1183                 next = sched_prev(irn);
1184
1185                 /* delete defined value from live set */
1186                 if(has_reg_class(si, irn)) {
1187                         pset_remove_ptr(live, irn);
1188                 }
1189
1190                 if(is_Phi(irn) || is_Proj(irn)) {
1191                         irn = next;
1192                         continue;
1193                 }
1194
1195                 args = pset_new_ptr_default();
1196                 used = pset_new_ptr_default();
1197
1198                 /* collect arguments of op and set args of op already live in epilog */
1199                 for (n = get_irn_arity(irn)-1; n>=0; --n) {
1200                         ir_node        *arg = get_irn_n(irn, n);
1201
1202                         pset_insert_ptr(args, arg);
1203                         if(has_reg_class(si, arg)) {
1204                                 pset_insert_ptr(live, arg);
1205                                 pset_insert_ptr(used, arg);
1206                         }
1207                 }
1208
1209                 /* insert all possible remats before irn */
1210                 pset_foreach(args, arg) {
1211                         remat_info_t   *remat_info,
1212                                                     query;
1213
1214                         /* continue if the operand has the wrong reg class */
1215                         if(!has_reg_class(si, arg))
1216                                 continue;
1217
1218                         query.irn = arg;
1219                         query.remats = NULL;
1220                         query.remats_by_operand = NULL;
1221                         remat_info = set_find(si->remat_info, &query, sizeof(query), HASH_PTR(arg));
1222
1223                         if(!remat_info) {
1224                                 continue;
1225                         }
1226
1227                         if(remat_info->remats) {
1228                                 pset_foreach(remat_info->remats, remat) {
1229                                         ir_node  *remat_irn = NULL;
1230
1231                                         DBG((si->dbg, LEVEL_4, "\t  considering remat %+F for arg %+F\n", remat->op, arg));
1232                                         remat_irn = insert_remat_before(si, remat, irn, live);
1233
1234                                         if(remat_irn) {
1235                                                 for(n=get_irn_arity(remat_irn)-1; n>=0; --n) {
1236                                                         ir_node  *remat_arg = get_irn_n(remat_irn, n);
1237
1238                                                         /* collect args of remats which are not args of op */
1239                                                         if(has_reg_class(si, remat_arg) && !pset_find_ptr(args, remat_arg)) {
1240                                                                 pset_insert_ptr(used, remat_arg);
1241                                                         }
1242                                                 }
1243                                         }
1244                                 }
1245                         }
1246                 }
1247
1248                 /* do not place post remats after jumps */
1249                 if(sched_skip_cf_predicator(irn, si->chordal_env->birg->main_env->arch_env)) {
1250                         del_pset(used);
1251                         del_pset(args);
1252                         break;
1253                 }
1254
1255                 /* insert all possible remats after irn */
1256                 post_remats = pset_new_ptr_default();
1257                 pset_foreach(used, arg) {
1258                         remat_info_t   *remat_info,
1259                                                     query;
1260
1261                         /* continue if the operand has the wrong reg class */
1262                         if(!has_reg_class(si, arg))
1263                                 continue;
1264
1265                         query.irn = arg;
1266                         query.remats = NULL;
1267                         query.remats_by_operand = NULL;
1268                         remat_info = set_find(si->remat_info, &query, sizeof(query), HASH_PTR(arg));
1269
1270                         if(!remat_info) {
1271                                 continue;
1272                         }
1273
1274                         if(remat_info->remats_by_operand) {
1275                                 pset_foreach(remat_info->remats_by_operand, remat) {
1276                                         /* do not insert remats producing the same value as one of the operands */
1277                                         if(!pset_find_ptr(args, remat->value)) {
1278                                                 DBG((si->dbg, LEVEL_4, "\t  considering remat %+F with arg %+F\n", remat->op, arg));
1279
1280                                                 /* only remat values that can be used by real ops */
1281                                                 if(!opt_remat_while_live || pset_find_ptr(live, remat->value)) {
1282                                                         pset_insert_ptr(post_remats, remat);
1283                                                 }
1284                                         }
1285                                 }
1286                         }
1287                 }
1288                 pset_foreach(post_remats, remat) {
1289                         insert_remat_after(si, remat, irn, live);
1290                 }
1291                 del_pset(post_remats);
1292
1293                 del_pset(used);
1294                 del_pset(args);
1295                 irn = next;
1296         }
1297
1298         /* add remats at end if successor has multiple predecessors */
1299         if(is_merge_edge(bb)) {
1300                 pset     *live_out = pset_new_ptr_default();
1301                 ir_node  *value;
1302
1303                 get_live_end(si, bb, live_out);
1304
1305                 /* add remats at end of block */
1306                 pset_foreach(live_out, value) {
1307                         remat_info_t   *remat_info,
1308                                                    query;
1309
1310                         query.irn = value;
1311                         query.remats = NULL;
1312                         query.remats_by_operand = NULL;
1313                         remat_info = set_find(si->remat_info, &query, sizeof(query), HASH_PTR(value));
1314
1315                         if(remat_info && remat_info->remats) {
1316                                 pset_foreach(remat_info->remats, remat) {
1317                                         DBG((si->dbg, LEVEL_4, "\t  considering remat %+F at end of block %+F\n", remat->op, bb));
1318
1319                                         insert_remat_before(si, remat, bb, live_out);
1320                                 }
1321                         }
1322                 }
1323                 del_pset(live_out);
1324         }
1325
1326         if(is_diverge_edge(bb)) {
1327                 pset     *live_in = pset_new_ptr_default();
1328                 ir_node  *value;
1329
1330                 be_lv_foreach(si->lv, bb, be_lv_state_in, i) {
1331                         value = be_lv_get_irn(si->lv, bb, i);
1332
1333                         if(has_reg_class(si, value)) {
1334                                 pset_insert_ptr(live_in, value);
1335                         }
1336                 }
1337                 /* add phis to live_in */
1338                 sched_foreach(bb, value) {
1339                         if(!is_Phi(value)) break;
1340
1341                         if(has_reg_class(si, value)) {
1342                                 pset_insert_ptr(live_in, value);
1343                         }
1344                 }
1345
1346                 /* add remat2s at beginning of block */
1347                 post_remats = pset_new_ptr_default();
1348                 pset_foreach(live_in, value) {
1349                         remat_info_t   *remat_info,
1350                                                    query;
1351
1352                         query.irn = value;
1353                         query.remats = NULL;
1354                         query.remats_by_operand = NULL;
1355                         remat_info = set_find(si->remat_info, &query, sizeof(query), HASH_PTR(value));
1356
1357                         if(remat_info && remat_info->remats_by_operand) {
1358                                 pset_foreach(remat_info->remats_by_operand, remat) {
1359                                         DBG((si->dbg, LEVEL_4, "\t  considering remat2 %+F at beginning of block %+F\n", remat->op, bb));
1360
1361                                         /* put the remat here if all its args are available and result is still live */
1362                                         if(!opt_remat_while_live || pset_find_ptr(live_in, remat->value)) {
1363                                                 pset_insert_ptr(post_remats, remat);
1364                                         }
1365                                 }
1366                         }
1367                 }
1368                 pset_foreach(post_remats, remat) {
1369                         insert_remat_after(si, remat, bb, live_in);
1370                 }
1371                 del_pset(post_remats);
1372                 del_pset(live_in);
1373         }
1374 }
1375
1376 static int
1377 can_be_copied(const ir_node * bb, const ir_node * irn)
1378 {
1379         const ir_edge_t *edge    = get_block_succ_first(bb);
1380         const ir_node   *next_bb = edge->src;
1381         int             pos      = edge->pos;
1382         const ir_node   *phi;
1383
1384         assert(is_merge_edge(bb));
1385
1386         sched_foreach(next_bb, phi) {
1387                 const ir_node  *phi_arg;
1388
1389                 if(!is_Phi(phi)) break;
1390
1391                 phi_arg = get_irn_n(phi, pos);
1392
1393                 if(phi_arg == irn) {
1394                         return 1;
1395                 }
1396         }
1397         return 0;
1398 }
1399
1400 /**
1401  * Initialize additional node info
1402  */
1403 static void
1404 luke_initializer(ir_node * bb, void * data)
1405 {
1406         spill_ilp_t    *si = (spill_ilp_t*)data;
1407         spill_bb_t     *spill_bb;
1408         ir_node        *irn;
1409
1410         spill_bb = obstack_alloc(si->obst, sizeof(*spill_bb));
1411         set_irn_link(bb, spill_bb);
1412
1413         sched_foreach(bb, irn) {
1414                 op_t      *op;
1415
1416                 op = obstack_alloc(si->obst, sizeof(*op));
1417                 op->is_remat = 0;
1418                 op->attr.live_range.ilp = ILP_UNDEF;
1419                 if(is_Phi(irn)) {
1420                         if(opt_memcopies) {
1421                                 op->attr.live_range.args.copies = obstack_alloc(si->obst, sizeof(*op->attr.live_range.args.copies) * get_irn_arity(irn));
1422                                 memset(op->attr.live_range.args.copies, 0xFF, sizeof(*op->attr.live_range.args.copies) * get_irn_arity(irn));
1423                         }
1424                 } else if(!is_Proj(irn)) {
1425                         op->attr.live_range.args.reloads = obstack_alloc(si->obst, sizeof(*op->attr.live_range.args.reloads) * get_irn_arity(irn));
1426                         memset(op->attr.live_range.args.reloads, 0xFF, sizeof(*op->attr.live_range.args.reloads) * get_irn_arity(irn));
1427                 } else {
1428                         op->attr.live_range.args.reloads = NULL;
1429                 }
1430                 set_irn_link(irn, op);
1431         }
1432 }
1433
1434
1435 /**
1436  * Preparation of blocks' ends for Luke Blockwalker(tm)(R)
1437  */
1438 static void
1439 luke_endwalker(ir_node * bb, void * data)
1440 {
1441         spill_ilp_t    *si = (spill_ilp_t*)data;
1442         pset           *live;
1443         pset           *use_end;
1444         char            buf[256];
1445         ilp_cst_t       cst;
1446         ir_node        *irn;
1447         spill_bb_t     *spill_bb = get_irn_link(bb);
1448         int             i;
1449
1450         live = pset_new_ptr_default();
1451         use_end = pset_new_ptr_default();
1452
1453         be_lv_foreach(si->lv, bb, be_lv_state_end, i) {
1454                 irn = be_lv_get_irn(si->lv, bb, i);
1455                 if (has_reg_class(si, irn) && !pset_find_ptr(si->all_possible_remats, irn)) {
1456                         pset_insert_ptr(live, irn);
1457                 }
1458         }
1459         /*
1460          * find values that are used by remats at end of block
1461          * and insert them into live set
1462          */
1463         foreach_pre_remat(si, bb, irn) {
1464                 int       n;
1465
1466                 for (n=get_irn_arity(irn)-1; n>=0; --n) {
1467                         ir_node        *remat_arg = get_irn_n(irn, n);
1468
1469                         if(has_reg_class(si, remat_arg)) {
1470                                 pset_insert_ptr(live, remat_arg);
1471                         }
1472                 }
1473         }
1474
1475         /* collect values used by cond jumps etc. at bb end (use_end) -> always live */
1476         /* their reg_out must always be set */
1477         sched_foreach_reverse(bb, irn) {
1478                 int   n;
1479
1480                 if(!sched_skip_cf_predicator(irn, si->chordal_env->birg->main_env->arch_env)) break;
1481
1482                 for (n=get_irn_arity(irn)-1; n>=0; --n) {
1483                         ir_node        *irn_arg = get_irn_n(irn, n);
1484
1485                         if(has_reg_class(si, irn_arg)) {
1486                                 pset_insert_ptr(use_end, irn_arg);
1487                         }
1488                 }
1489         }
1490
1491         ir_snprintf(buf, sizeof(buf), "check_end_%N", bb);
1492         //cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, si->n_regs);
1493         cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, si->n_regs - pset_count(use_end));
1494
1495         spill_bb->ilp = new_set(cmp_spill, pset_count(live)+pset_count(use_end));
1496
1497         /* if this is a merge edge we can reload at the end of this block */
1498         if(is_merge_edge(bb)) {
1499                 spill_bb->reloads = new_set(cmp_keyval, pset_count(live)+pset_count(use_end));
1500         } else if(pset_count(use_end)){
1501                 spill_bb->reloads = new_set(cmp_keyval, pset_count(use_end));
1502         } else {
1503                 spill_bb->reloads = NULL;
1504         }
1505
1506         pset_foreach(live,irn) {
1507                 spill_t     query,
1508                                         *spill;
1509                 double      spill_cost;
1510                 int         default_spilled;
1511
1512
1513                 /* handle values used by control flow nodes later separately */
1514                 if(pset_find_ptr(use_end, irn)) continue;
1515
1516                 query.irn = irn;
1517                 spill = set_insert(spill_bb->ilp, &query, sizeof(query), HASH_PTR(irn));
1518
1519                 spill_cost = is_Unknown(irn)?0.0001:opt_cost_spill*execution_frequency(si, bb);
1520
1521                 ir_snprintf(buf, sizeof(buf), "reg_out_%N_%N", irn, bb);
1522                 spill->reg_out = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, 0.0);
1523                 lpp_set_factor_fast(si->lpp, cst, spill->reg_out, 1.0);
1524
1525                 ir_snprintf(buf, sizeof(buf), "mem_out_%N_%N", irn, bb);
1526                 spill->mem_out = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, 1.0);
1527
1528                 ir_snprintf(buf, sizeof(buf), "spill_%N_%N", irn, bb);
1529                 /* by default spill value right after definition */
1530                 default_spilled = be_is_live_in(si->lv, bb, irn) || is_Phi(irn);
1531                 spill->spill    = lpp_add_var_default(si->lpp, buf, lpp_binary, spill_cost, !default_spilled);
1532
1533                 if(is_merge_edge(bb)) {
1534                         ilp_var_t   reload;
1535                         ilp_cst_t   rel_cst;
1536
1537                         ir_snprintf(buf, sizeof(buf), "reload_%N_%N", bb, irn);
1538                         reload = lpp_add_var_default(si->lpp, buf, lpp_binary, opt_cost_reload*execution_frequency(si, bb), can_be_copied(bb, irn));
1539                         set_insert_keyval(spill_bb->reloads, irn, INT_TO_PTR(reload));
1540
1541                         /* reload <= mem_out */
1542                         rel_cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
1543                         lpp_set_factor_fast(si->lpp, rel_cst, reload, 1.0);
1544                         lpp_set_factor_fast(si->lpp, rel_cst, spill->mem_out, -1.0);
1545                 }
1546
1547                 spill->reg_in = ILP_UNDEF;
1548                 spill->mem_in = ILP_UNDEF;
1549         }
1550
1551         pset_foreach(use_end,irn) {
1552                 spill_t     query,
1553                                         *spill;
1554                 double      spill_cost;
1555                 ilp_cst_t   end_use_req,
1556                                         rel_cst;
1557                 ilp_var_t   reload;
1558                 int         default_spilled;
1559
1560                 query.irn = irn;
1561                 spill = set_insert(spill_bb->ilp, &query, sizeof(query), HASH_PTR(irn));
1562
1563                 spill_cost = is_Unknown(irn)?0.0001:opt_cost_spill*execution_frequency(si, bb);
1564
1565                 ir_snprintf(buf, sizeof(buf), "reg_out_%N_%N", irn, bb);
1566                 spill->reg_out = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, 1.0);
1567                 /* if irn is used at the end of the block, then it is live anyway */
1568                 //lpp_set_factor_fast(si->lpp, cst, spill->reg_out, 1.0);
1569
1570                 ir_snprintf(buf, sizeof(buf), "mem_out_%N_%N", irn, bb);
1571                 spill->mem_out = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, 1.0);
1572
1573                 ir_snprintf(buf, sizeof(buf), "spill_%N_%N", irn, bb);
1574                 default_spilled = be_is_live_in(si->lv, bb, irn) || is_Phi(irn);
1575                 spill->spill    = lpp_add_var_default(si->lpp, buf, lpp_binary, spill_cost, !default_spilled);
1576
1577                 /* reload for use be control flow op */
1578                 ir_snprintf(buf, sizeof(buf), "reload_%N_%N", bb, irn);
1579                 reload = lpp_add_var_default(si->lpp, buf, lpp_binary, opt_cost_reload*execution_frequency(si, bb), 1.0);
1580                 set_insert_keyval(spill_bb->reloads, irn, INT_TO_PTR(reload));
1581
1582                 /* reload <= mem_out */
1583                 rel_cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
1584                 lpp_set_factor_fast(si->lpp, rel_cst, reload, 1.0);
1585                 lpp_set_factor_fast(si->lpp, rel_cst, spill->mem_out, -1.0);
1586
1587                 spill->reg_in = ILP_UNDEF;
1588                 spill->mem_in = ILP_UNDEF;
1589
1590                 ir_snprintf(buf, sizeof(buf), "req_cf_end_%N_%N", irn, bb);
1591                 end_use_req = lpp_add_cst_uniq(si->lpp, buf, lpp_equal, 1);
1592                 lpp_set_factor_fast(si->lpp, end_use_req, spill->reg_out, 1.0);
1593         }
1594
1595         del_pset(live);
1596         del_pset(use_end);
1597 }
1598
1599 /**
1600  * Find a remat of value @p value in the epilog of @p pos
1601  */
1602 static ir_node *
1603 find_post_remat(const ir_node * value, const ir_node * pos)
1604 {
1605         while((pos = next_post_remat(pos)) != NULL) {
1606                 op_t   *op;
1607
1608                 op = get_irn_link(pos);
1609                 assert(op->is_remat && !op->attr.remat.pre);
1610
1611                 if(op->attr.remat.remat->value == value)
1612                         return (ir_node*)pos;
1613
1614 #if 0
1615         const ir_edge_t *edge;
1616                 foreach_out_edge(pos, edge) {
1617                         ir_node   *proj = get_edge_src_irn(edge);
1618                         assert(is_Proj(proj));
1619                 }
1620 #endif
1621
1622         }
1623
1624         return NULL;
1625 }
1626
1627 static spill_t *
1628 add_to_spill_bb(spill_ilp_t * si, ir_node * bb, ir_node * irn)
1629 {
1630         spill_bb_t  *spill_bb = get_irn_link(bb);
1631         spill_t     *spill,
1632                                  query;
1633         char         buf[256];
1634         int          default_spilled;
1635
1636         query.irn = irn;
1637         spill = set_find(spill_bb->ilp, &query, sizeof(query), HASH_PTR(irn));
1638         if(!spill) {
1639                 double   spill_cost = is_Unknown(irn)?0.0001:opt_cost_spill*execution_frequency(si, bb);
1640
1641                 spill = set_insert(spill_bb->ilp, &query, sizeof(query), HASH_PTR(irn));
1642
1643                 spill->reg_out = ILP_UNDEF;
1644                 spill->reg_in  = ILP_UNDEF;
1645                 spill->mem_in  = ILP_UNDEF;
1646
1647                 ir_snprintf(buf, sizeof(buf), "mem_out_%N_%N", irn, bb);
1648                 spill->mem_out = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, 1.0);
1649
1650                 ir_snprintf(buf, sizeof(buf), "spill_%N_%N", irn, bb);
1651                 default_spilled = be_is_live_in(si->lv, bb, irn) || is_Phi(irn);
1652                 spill->spill    = lpp_add_var_default(si->lpp, buf, lpp_binary, spill_cost, !default_spilled);
1653         }
1654
1655         return spill;
1656 }
1657
1658 /**
1659  *  Inserts ILP-constraints and variables for memory copying before the given position
1660  */
1661 static void
1662 insert_mem_copy_position(spill_ilp_t * si, pset * live, const ir_node * block)
1663 {
1664         const ir_node    *succ;
1665         const ir_edge_t  *edge;
1666         spill_bb_t       *spill_bb = get_irn_link(block);
1667         ir_node          *phi;
1668         int               pos;
1669         ilp_cst_t         cst;
1670         ilp_var_t         copyreg;
1671         char              buf[256];
1672         ir_node          *tmp;
1673
1674
1675         assert(edges_activated(current_ir_graph));
1676
1677         edge = get_block_succ_first(block);
1678         if(!edge) return;
1679
1680         succ = edge->src;
1681         pos = edge->pos;
1682
1683         edge = get_block_succ_next(block, edge);
1684         /* next block can only contain phis, if this is a merge edge */
1685         if(edge) return;
1686
1687         ir_snprintf(buf, sizeof(buf), "copyreg_%N", block);
1688         copyreg = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, 1.0);
1689
1690         ir_snprintf(buf, sizeof(buf), "check_copyreg_%N", block);
1691         cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, si->n_regs);
1692
1693         pset_foreach(live, tmp) {
1694                 spill_t  *spill;
1695 #if 0
1696                 op_t  *op = get_irn_link(irn);
1697                 lpp_set_factor_fast(si->lpp, cst, op->attr.live_range.ilp, 1.0);
1698 #endif
1699                 spill = set_find_spill(spill_bb->ilp, tmp);
1700                 assert(spill);
1701
1702                 lpp_set_factor_fast(si->lpp, cst, spill->reg_out, 1.0);
1703         }
1704         lpp_set_factor_fast(si->lpp, cst, copyreg, 1.0);
1705
1706         sched_foreach(succ, phi) {
1707                 const ir_node  *to_copy;
1708                 op_t           *to_copy_op;
1709                 spill_t        *to_copy_spill;
1710                 op_t           *phi_op = get_irn_link(phi);
1711                 ilp_var_t       reload = ILP_UNDEF;
1712
1713
1714                 if(!is_Phi(phi)) break;
1715                 if(!has_reg_class(si, phi)) continue;
1716
1717                 to_copy = get_irn_n(phi, pos);
1718                 to_copy_op = get_irn_link(to_copy);
1719
1720                 to_copy_spill = set_find_spill(spill_bb->ilp, to_copy);
1721                 assert(to_copy_spill);
1722
1723                 if(spill_bb->reloads) {
1724                         keyval_t *keyval = set_find_keyval(spill_bb->reloads, to_copy);
1725
1726                         if(keyval) {
1727                                 reload = PTR_TO_INT(keyval->val);
1728                         }
1729                 }
1730
1731                 ir_snprintf(buf, sizeof(buf), "req_copy_%N_%N_%N", block, phi, to_copy);
1732                 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
1733
1734                 /* copy - reg_out - reload - remat - live_range <= 0 */
1735                 lpp_set_factor_fast(si->lpp, cst, phi_op->attr.live_range.args.copies[pos], 1.0);
1736                 lpp_set_factor_fast(si->lpp, cst, to_copy_spill->reg_out, -1.0);
1737                 if(reload != ILP_UNDEF) lpp_set_factor_fast(si->lpp, cst, reload, -1.0);
1738                 lpp_set_factor_fast(si->lpp, cst, to_copy_op->attr.live_range.ilp, -1.0);
1739                 foreach_pre_remat(si, block, tmp) {
1740                         op_t     *remat_op = get_irn_link(tmp);
1741                         if(remat_op->attr.remat.remat->value == to_copy) {
1742                                 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, -1.0);
1743                         }
1744                 }
1745
1746                 ir_snprintf(buf, sizeof(buf), "copyreg_%N_%N_%N", block, phi, to_copy);
1747                 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
1748
1749                 /* copy - reg_out - copyreg <= 0 */
1750                 lpp_set_factor_fast(si->lpp, cst, phi_op->attr.live_range.args.copies[pos], 1.0);
1751                 lpp_set_factor_fast(si->lpp, cst, to_copy_spill->reg_out, -1.0);
1752                 lpp_set_factor_fast(si->lpp, cst, copyreg, -1.0);
1753         }
1754 }
1755
1756
1757 /**
1758  * Walk all irg blocks and emit this ILP
1759  */
1760 static void
1761 luke_blockwalker(ir_node * bb, void * data)
1762 {
1763         spill_ilp_t    *si = (spill_ilp_t*)data;
1764         ir_node        *irn;
1765         pset           *live;
1766         char            buf[256];
1767         ilp_cst_t       cst;
1768         spill_bb_t     *spill_bb = get_irn_link(bb);
1769         ir_node        *tmp;
1770         spill_t        *spill;
1771         pset           *defs = pset_new_ptr_default();
1772         const arch_env_t *arch_env = si->chordal_env->birg->main_env->arch_env;
1773
1774         live = pset_new_ptr_default();
1775
1776         /****************************************
1777          *      B A S I C  B L O C K  E N D
1778          ***************************************/
1779
1780
1781         /* init live values at end of block */
1782         get_live_end(si, bb, live);
1783
1784         pset_foreach(live, irn) {
1785                 op_t           *op;
1786                 ilp_var_t       reload = ILP_UNDEF;
1787
1788                 spill = set_find_spill(spill_bb->ilp, irn);
1789                 assert(spill);
1790
1791                 if(spill_bb->reloads) {
1792                         keyval_t *keyval = set_find_keyval(spill_bb->reloads, irn);
1793
1794                         if(keyval) {
1795                                 reload = PTR_TO_INT(keyval->val);
1796                         }
1797                 }
1798
1799                 op = get_irn_link(irn);
1800                 assert(!op->is_remat);
1801
1802                 ir_snprintf(buf, sizeof(buf), "lr_%N_%N", irn, bb);
1803                 op->attr.live_range.ilp = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, 0.0);
1804                 op->attr.live_range.op = bb;
1805
1806                 ir_snprintf(buf, sizeof(buf), "reg_out_%N_%N", bb, irn);
1807                 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
1808
1809                 /* reg_out - reload - remat - live_range <= 0 */
1810                 lpp_set_factor_fast(si->lpp, cst, spill->reg_out, 1.0);
1811                 if(reload != ILP_UNDEF) lpp_set_factor_fast(si->lpp, cst, reload, -1.0);
1812                 lpp_set_factor_fast(si->lpp, cst, op->attr.live_range.ilp, -1.0);
1813                 foreach_pre_remat(si, bb, tmp) {
1814                         op_t     *remat_op = get_irn_link(tmp);
1815                         if(remat_op->attr.remat.remat->value == irn) {
1816                                 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, -1.0);
1817                         }
1818                 }
1819                 ir_snprintf(buf, sizeof(buf), "reg_out2_%N_%N", bb, irn);
1820                 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_greater, 0.0);
1821
1822                 /* value may only die at bb end if it is used for a mem copy */
1823                 /* reg_out + \sum copy - reload - remat - live_range >= 0 */
1824                 lpp_set_factor_fast(si->lpp, cst, spill->reg_out, 1.0);
1825                 if(reload != ILP_UNDEF) lpp_set_factor_fast(si->lpp, cst, reload, -1.0);
1826                 lpp_set_factor_fast(si->lpp, cst, op->attr.live_range.ilp, -1.0);
1827                 foreach_pre_remat(si, bb, tmp) {
1828                         op_t     *remat_op = get_irn_link(tmp);
1829                         if(remat_op->attr.remat.remat->value == irn) {
1830                                 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, -1.0);
1831                         }
1832                 }
1833                 if(is_merge_edge(bb)) {
1834                         const ir_edge_t *edge = get_block_succ_first(bb);
1835                         const ir_node   *next_bb = edge->src;
1836                         int              pos = edge->pos;
1837                         const ir_node   *phi;
1838
1839                         sched_foreach(next_bb, phi) {
1840                                 const ir_node  *phi_arg;
1841
1842                                 if(!is_Phi(phi)) break;
1843
1844                                 phi_arg = get_irn_n(phi, pos);
1845
1846                                 if(phi_arg == irn) {
1847                                         op_t      *phi_op = get_irn_link(phi);
1848                                         ilp_var_t  copy = phi_op->attr.live_range.args.copies[pos];
1849
1850                                         lpp_set_factor_fast(si->lpp, cst, copy, 1.0);
1851                                 }
1852                         }
1853                 }
1854         }
1855
1856         if(opt_memcopies)
1857                 insert_mem_copy_position(si, live, bb);
1858
1859         /*
1860          * assure the remat args are available
1861          */
1862         foreach_pre_remat(si, bb, tmp) {
1863                 op_t     *remat_op = get_irn_link(tmp);
1864                 int       n;
1865
1866                 for (n=get_irn_arity(tmp)-1; n>=0; --n) {
1867                         ir_node        *remat_arg = get_irn_n(tmp, n);
1868                         op_t           *arg_op = get_irn_link(remat_arg);
1869
1870                         if(!has_reg_class(si, remat_arg)) continue;
1871
1872                         spill = set_find_spill(spill_bb->ilp, remat_arg);
1873                         assert(spill);
1874
1875                         /* arguments of remats have to be live until the very end of the block
1876                          * remat = reg_out(remat_arg) and (reload(remat_arg) or live_range(remat_arg)),
1877                          * no remats, they could be in wrong order
1878                          */
1879
1880                         ir_snprintf(buf, sizeof(buf), "req_remat_%N_arg_%N", tmp, remat_arg);
1881                         cst = lpp_add_cst(si->lpp, buf, lpp_less, 0.0);
1882
1883                         lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, 3.0);
1884                         lpp_set_factor_fast(si->lpp, cst, spill->reg_out, -2.0);
1885                         lpp_set_factor_fast(si->lpp, cst, arg_op->attr.live_range.ilp, -1.0);
1886
1887                         /* use reload placed for this argument */
1888                         if(spill_bb->reloads) {
1889                                 keyval_t *keyval = set_find_keyval(spill_bb->reloads, remat_arg);
1890
1891                                 if(keyval) {
1892                                         ilp_var_t       reload = PTR_TO_INT(keyval->val);
1893
1894                                         lpp_set_factor_fast(si->lpp, cst, reload, -1.0);
1895                                 }
1896                         }
1897                 }
1898         }
1899         DBG((si->dbg, LEVEL_4, "\t   %d values live at end of block %+F\n", pset_count(live), bb));
1900
1901
1902
1903
1904         /**************************************
1905          *    B A S I C  B L O C K  B O D Y
1906          **************************************/
1907
1908         sched_foreach_reverse_from(sched_block_last_noncf(si, bb), irn) {
1909                 op_t       *op;
1910                 op_t       *tmp_op;
1911                 int         n,
1912                                         u = 0,
1913                                         d = 0;
1914                 ilp_cst_t       check_pre,
1915                                         check_post;
1916                 set        *args;
1917                 pset       *used;
1918                 pset       *remat_defs;
1919                 keyval_t   *keyval;
1920                 ilp_cst_t   one_memoperand;
1921
1922                 /* iterate only until first phi */
1923                 if(is_Phi(irn))
1924                         break;
1925
1926                 op = get_irn_link(irn);
1927                 /* skip remats */
1928                 if(op->is_remat) continue;
1929
1930                 DBG((si->dbg, LEVEL_4, "\t  at node %+F\n", irn));
1931
1932                 /* collect defined values */
1933                 if(has_reg_class(si, irn)) {
1934                         pset_insert_ptr(defs, irn);
1935                 }
1936
1937                 /* skip projs */
1938                 if(is_Proj(irn)) continue;
1939
1940                 /*
1941                  * init set of irn's arguments
1942                  * and all possibly used values around this op
1943                  * and values defined by post remats
1944                  */
1945                 args =       new_set(cmp_keyval, get_irn_arity(irn));
1946                 used =       pset_new_ptr(pset_count(live) + get_irn_arity(irn));
1947                 remat_defs = pset_new_ptr(pset_count(live));
1948
1949                 if(!is_start_block(bb) || !be_is_Barrier(irn)) {
1950                         for (n=get_irn_arity(irn)-1; n>=0; --n) {
1951                                 ir_node        *irn_arg = get_irn_n(irn, n);
1952                                 if(has_reg_class(si, irn_arg)) {
1953                                         set_insert_keyval(args, irn_arg, (void*)n);
1954                                         pset_insert_ptr(used, irn_arg);
1955                                 }
1956                         }
1957                         foreach_post_remat(irn, tmp) {
1958                                 op_t    *remat_op = get_irn_link(tmp);
1959
1960                                 pset_insert_ptr(remat_defs, remat_op->attr.remat.remat->value);
1961
1962                                 for (n=get_irn_arity(tmp)-1; n>=0; --n) {
1963                                         ir_node        *remat_arg = get_irn_n(tmp, n);
1964                                         if(has_reg_class(si, remat_arg)) {
1965                                                 pset_insert_ptr(used, remat_arg);
1966                                         }
1967                                 }
1968                         }
1969                         foreach_pre_remat(si, irn, tmp) {
1970                                 for (n=get_irn_arity(tmp)-1; n>=0; --n) {
1971                                         ir_node        *remat_arg = get_irn_n(tmp, n);
1972                                         if(has_reg_class(si, remat_arg)) {
1973                                                 pset_insert_ptr(used, remat_arg);
1974                                         }
1975                                 }
1976                         }
1977                 }
1978
1979                 /**********************************
1980                  *   I N  E P I L O G  O F  irn
1981                  **********************************/
1982
1983                 /* ensure each dying value is used by only one post remat */
1984                 pset_foreach(used, tmp) {
1985                         ir_node     *value = tmp;
1986                         op_t        *value_op = get_irn_link(value);
1987                         ir_node     *remat;
1988                         int          n_remats = 0;
1989
1990                         cst = ILP_UNDEF;
1991                         foreach_post_remat(irn, remat) {
1992                                 op_t  *remat_op = get_irn_link(remat);
1993
1994                                 for(n=get_irn_arity(remat)-1; n>=0; --n) {
1995                                         ir_node   *remat_arg = get_irn_n(remat, n);
1996
1997                                         /* if value is used by this remat add it to constraint */
1998                                         if(remat_arg == value) {
1999                                                 if(n_remats == 0) {
2000                                                         /* sum remat2s <= 1 + n_remats*live_range */
2001                                                         ir_snprintf(buf, sizeof(buf), "dying_lr_%N_%N", value, irn);
2002                                                         cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 1.0);
2003                                                 }
2004
2005                                                 n_remats++;
2006                                                 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, 1.0);
2007                                                 break;
2008                                         }
2009                                 }
2010                         }
2011
2012                         if(pset_find_ptr(live, value) && cst != ILP_UNDEF) {
2013                                 lpp_set_factor_fast(si->lpp, cst, value_op->attr.live_range.ilp, -n_remats);
2014                         }
2015                 }
2016
2017         /* ensure at least one value dies at post remat */
2018         foreach_post_remat(irn, tmp) {
2019             op_t     *remat_op = get_irn_link(tmp);
2020             pset     *remat_args = pset_new_ptr(get_irn_arity(tmp));
2021             ir_node  *remat_arg;
2022
2023             for(n=get_irn_arity(tmp)-1; n>=0; --n) {
2024                 remat_arg = get_irn_n(tmp, n);
2025
2026                 if(has_reg_class(si, remat_arg)) {
2027
2028                     /* does arg always die at this op? */
2029                     if(!pset_find_ptr(live, remat_arg))
2030                         goto skip_one_must_die;
2031
2032                     pset_insert_ptr(remat_args, remat_arg);
2033                 }
2034             }
2035
2036             /* remat + \sum live_range(remat_arg) <= |args| */
2037             ir_snprintf(buf, sizeof(buf), "one_must_die_%+F", tmp);
2038             cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, pset_count(remat_args));
2039             lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, 1.0);
2040
2041             pset_foreach(remat_args, remat_arg) {
2042                 op_t  *arg_op = get_irn_link(remat_arg);
2043
2044                 lpp_set_factor_fast(si->lpp, cst, arg_op->attr.live_range.ilp, 1.0);
2045             }
2046
2047 skip_one_must_die:
2048             del_pset(remat_args);
2049         }
2050
2051                 /* new live ranges for values from L\U defined by post remats */
2052                 pset_foreach(live, tmp) {
2053                         ir_node     *value = tmp;
2054                         op_t        *value_op = get_irn_link(value);
2055
2056                         if(!set_find_keyval(args, value) && !pset_find_ptr(defs, value)) {
2057                                 ilp_var_t    prev_lr = ILP_UNDEF;
2058                                 ir_node     *remat;
2059
2060                                 if(pset_find_ptr(remat_defs, value)) {
2061
2062                                         /* next_live_range <= prev_live_range + sum remat2s */
2063                                         ir_snprintf(buf, sizeof(buf), "next_lr_%N_%N", value, irn);
2064                                         cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
2065
2066                                         ir_snprintf(buf, sizeof(buf), "lr_%N_%N", value, irn);
2067                                         prev_lr = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, 0.0);
2068
2069                                         lpp_set_factor_fast(si->lpp, cst, value_op->attr.live_range.ilp, 1.0);
2070                                         lpp_set_factor_fast(si->lpp, cst, prev_lr, -1.0);
2071
2072                                         foreach_post_remat(irn, remat) {
2073                                                 op_t        *remat_op = get_irn_link(remat);
2074
2075                                                 /* if value is being rematerialized by this remat */
2076                                                 if(value == remat_op->attr.remat.remat->value) {
2077                                                         lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, -1.0);
2078                                                 }
2079                                         }
2080
2081                                         value_op->attr.live_range.ilp = prev_lr;
2082                                         value_op->attr.live_range.op = irn;
2083                                 }
2084                         }
2085                 }
2086
2087                 /* requirements for post remats and start live ranges from L/U' for values dying here */
2088                 foreach_post_remat(irn, tmp) {
2089                         op_t        *remat_op = get_irn_link(tmp);
2090                         int          n;
2091
2092                         for (n=get_irn_arity(tmp)-1; n>=0; --n) {
2093                                 ir_node        *remat_arg = get_irn_n(tmp, n);
2094                                 op_t           *arg_op = get_irn_link(remat_arg);
2095
2096                                 if(!has_reg_class(si, remat_arg)) continue;
2097
2098                                 /* only for values in L\U (TODO and D?), the others are handled with post_use */
2099                                 if(!pset_find_ptr(used, remat_arg)) {
2100                                         /* remat <= live_range(remat_arg) */
2101                                         ir_snprintf(buf, sizeof(buf), "req_remat2_%N_arg_%N", tmp, remat_arg);
2102                                         cst = lpp_add_cst(si->lpp, buf, lpp_less, 0.0);
2103
2104                                         /* if value is becoming live through use by remat2 */
2105                                         if(!pset_find_ptr(live, remat_arg)) {
2106                                                 ilp_var_t     lr;
2107
2108                                                 ir_snprintf(buf, sizeof(buf), "lr_%N_%N", remat_arg, irn);
2109                                                 lr = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, 0.0);
2110
2111                                                 arg_op->attr.live_range.ilp = lr;
2112                                                 arg_op->attr.live_range.op = irn;
2113
2114                                                 DBG((si->dbg, LEVEL_3, "  value %+F becoming live through use by remat2 %+F\n", remat_arg, tmp));
2115
2116                                                 pset_insert_ptr(live, remat_arg);
2117                                                 add_to_spill_bb(si, bb, remat_arg);
2118                                         }
2119
2120                                         lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, 1.0);
2121                                         lpp_set_factor_fast(si->lpp, cst, arg_op->attr.live_range.ilp, -1.0);
2122                                 }
2123                         }
2124                 }
2125
2126                 d = pset_count(defs);
2127                 DBG((si->dbg, LEVEL_4, "\t   %+F produces %d values in my register class\n", irn, d));
2128
2129                 /* count how many regs irn needs for arguments */
2130                 u = set_count(args);
2131
2132
2133                 /* check the register pressure in the epilog */
2134                 /* sum_{L\U'} lr + sum_{U'} post_use <= k - |D| */
2135                 ir_snprintf(buf, sizeof(buf), "check_post_%N", irn);
2136                 check_post = lpp_add_cst_uniq(si->lpp, buf, lpp_less, si->n_regs - d);
2137
2138                 /* add L\U' to check_post */
2139                 pset_foreach(live, tmp) {
2140                         if(!pset_find_ptr(used, tmp) && !pset_find_ptr(defs, tmp)) {
2141                                 /* if a live value is not used by irn */
2142                                 tmp_op = get_irn_link(tmp);
2143                                 lpp_set_factor_fast(si->lpp, check_post, tmp_op->attr.live_range.ilp, 1.0);
2144                         }
2145                 }
2146
2147                 /***********************************************************
2148                  *  I T E R A T I O N  O V E R  U S E S  F O R  E P I L O G
2149                  **********************************************************/
2150
2151
2152                 pset_foreach(used, tmp) {
2153                         ilp_var_t       prev_lr;
2154                         ilp_var_t       post_use;
2155                         int             p = 0;
2156                         spill_t        *spill;
2157                         ir_node        *arg = tmp;
2158                         op_t           *arg_op = get_irn_link(arg);
2159                         ir_node        *remat;
2160
2161                         spill = add_to_spill_bb(si, bb, arg);
2162
2163                         /* new live range for each used value */
2164                         ir_snprintf(buf, sizeof(buf), "lr_%N_%N", arg, irn);
2165                         prev_lr = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, 0.0);
2166
2167                         /* the epilog stuff - including post_use, check_post, check_post_remat */
2168                         ir_snprintf(buf, sizeof(buf), "post_use_%N_%N", arg, irn);
2169                         post_use = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, 0.0);
2170
2171                         lpp_set_factor_fast(si->lpp, check_post, post_use, 1.0);
2172
2173                         /* arg is live throughout epilog if the next live_range is in a register */
2174                         if(pset_find_ptr(live, arg)) {
2175                                 DBG((si->dbg, LEVEL_3, "\t  arg %+F is possibly live in epilog of %+F\n", arg, irn));
2176
2177                                 /* post_use >= next_lr + remat */
2178                                 ir_snprintf(buf, sizeof(buf), "post_use_%N_%N-%d", arg, irn, p++);
2179                                 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
2180                                 lpp_set_factor_fast(si->lpp, cst, post_use, -1.0);
2181                                 lpp_set_factor_fast(si->lpp, cst, arg_op->attr.live_range.ilp, 1.0);
2182                         }
2183
2184                         /* forall post remat which use arg add a similar cst */
2185                         foreach_post_remat(irn, remat) {
2186                                 int      n;
2187
2188                                 for (n=get_irn_arity(remat)-1; n>=0; --n) {
2189                                         ir_node    *remat_arg = get_irn_n(remat, n);
2190                                         op_t       *remat_op = get_irn_link(remat);
2191
2192                                         if(remat_arg == arg) {
2193                                                 DBG((si->dbg, LEVEL_3, "\t  found remat with arg %+F in epilog of %+F\n", arg, irn));
2194
2195                                                 /* post_use >= remat */
2196                                                 ir_snprintf(buf, sizeof(buf), "post_use_%N_%N-%d", arg, irn, p++);
2197                                                 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
2198                                                 lpp_set_factor_fast(si->lpp, cst, post_use, -1.0);
2199                                                 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, 1.0);
2200                                         }
2201                                 }
2202                         }
2203
2204                         /* if value is not an arg of op and not possibly defined by post remat
2205                          * then it may only die and not become live
2206                          */
2207                         if(!set_find_keyval(args, arg)) {
2208                                 /* post_use <= prev_lr */
2209                                 ir_snprintf(buf, sizeof(buf), "req_post_use_%N_%N", arg, irn);
2210                                 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
2211                                 lpp_set_factor_fast(si->lpp, cst, post_use, 1.0);
2212                                 lpp_set_factor_fast(si->lpp, cst, prev_lr, -1.0);
2213
2214                                 if(!pset_find_ptr(remat_defs, arg) && pset_find_ptr(live, arg)) {
2215                                         /* next_lr <= prev_lr */
2216                                         ir_snprintf(buf, sizeof(buf), "next_lr_%N_%N", arg, irn);
2217                                         cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
2218                                         lpp_set_factor_fast(si->lpp, cst, arg_op->attr.live_range.ilp, 1.0);
2219                                         lpp_set_factor_fast(si->lpp, cst, prev_lr, -1.0);
2220                                 }
2221                         }
2222
2223                         if(opt_memoperands && (!is_start_block(bb) || be_is_Barrier(irn))) {
2224                                 for(n = get_irn_arity(irn)-1; n>=0; --n) {
2225                                         if(get_irn_n(irn, n) == arg && arch_possible_memory_operand(arch_env, irn, n)) {
2226                                                 ilp_var_t       memoperand;
2227
2228                                                 ir_snprintf(buf, sizeof(buf), "memoperand_%N_%d", irn, n);
2229                                                 memoperand = lpp_add_var_default(si->lpp, buf, lpp_binary, opt_cost_memoperand*execution_frequency(si, bb), 0.0);
2230                                                 set_insert_memoperand(si->memoperands, irn, n, memoperand);
2231
2232                                                 ir_snprintf(buf, sizeof(buf), "nolivepost_%N_%d", irn, n);
2233                                                 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 1.0);
2234
2235                                                 lpp_set_factor_fast(si->lpp, cst, memoperand, 1.0);
2236                                                 lpp_set_factor_fast(si->lpp, cst, post_use, 1.0);
2237                                         }
2238                                 }
2239                         }
2240
2241                         /* new live range begins for each used value */
2242                         arg_op->attr.live_range.ilp = prev_lr;
2243                         arg_op->attr.live_range.op = irn;
2244
2245                         pset_insert_ptr(live, arg);
2246                 }
2247
2248                 /* just to be sure */
2249                 check_post = ILP_UNDEF;
2250
2251                 /* allow original defintions to be removed */
2252                 if(opt_repair_schedule) {
2253                         pset_foreach(defs, tmp) {
2254                                 op_t      *tmp_op = get_irn_link(tmp);
2255                                 spill_t   *spill = set_find_spill(spill_bb->ilp, tmp);
2256 #if 1
2257                                 ilp_var_t  delete;
2258                                 assert(spill);
2259
2260                                 ir_snprintf(buf, sizeof(buf), "delete_%N", tmp);
2261                                 delete = lpp_add_var_default(si->lpp, buf, lpp_binary, -1.0*get_cost(si, irn)*execution_frequency(si, bb), 0.0);
2262
2263                                 /* op may not be killed if its first live_range is 1 */
2264                                 ir_snprintf(buf, sizeof(buf), "killorig-lr_%N", tmp);
2265                                 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 1.0);
2266                                 lpp_set_factor_fast(si->lpp, cst, delete, 1.0);
2267                                 lpp_set_factor_fast(si->lpp, cst, tmp_op->attr.live_range.ilp, 1.0);
2268
2269                                 /* op may not be killed if it is spilled after the definition */
2270                                 ir_snprintf(buf, sizeof(buf), "killorig-spill_%N", tmp);
2271                                 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 1.0);
2272                                 lpp_set_factor_fast(si->lpp, cst, delete, 1.0);
2273                                 lpp_set_factor_fast(si->lpp, cst, spill->spill, 1.0);
2274 #else
2275                                 ilp_var_t  keep;
2276                                 assert(spill);
2277
2278                                 ir_snprintf(buf, sizeof(buf), "keep_%N", tmp);
2279                                 keep = lpp_add_var_default(si->lpp, buf, lpp_binary, get_cost(si, irn)*execution_frequency(si, bb), 1.0);
2280
2281                                 /* op may not be killed if its first live_range is 1 */
2282                                 ir_snprintf(buf, sizeof(buf), "killorig-lr_%N", tmp);
2283                                 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_greater, 0.0);
2284                                 lpp_set_factor_fast(si->lpp, cst, keep, 1.0);
2285                                 lpp_set_factor_fast(si->lpp, cst, tmp_op->attr.live_range.ilp, -1.0);
2286
2287                                 /* op may not be killed if it is spilled after the definition */
2288                                 ir_snprintf(buf, sizeof(buf), "killorig-spill_%N", tmp);
2289                                 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_greater, 0.0);
2290                                 lpp_set_factor_fast(si->lpp, cst, keep, 1.0);
2291                                 lpp_set_factor_fast(si->lpp, cst, spill->spill, -1.0);
2292 #endif
2293                         }
2294                 } else {
2295                         pset_foreach(defs, tmp) {
2296                                 op_t      *tmp_op = get_irn_link(tmp);
2297                                 spill_t   *spill = set_find_spill(spill_bb->ilp, tmp);
2298                                 assert(spill);
2299
2300                                 /* live_range or spill should be 1
2301                                    TODO: lr should be live until first use */
2302                                 ir_snprintf(buf, sizeof(buf), "nokillorig_%N", tmp);
2303                                 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_greater, 1.0);
2304                                 lpp_set_factor_fast(si->lpp, cst, tmp_op->attr.live_range.ilp, 1.0);
2305                                 lpp_set_factor_fast(si->lpp, cst, spill->spill, 1.0);
2306                         }
2307                 }
2308
2309
2310                 /******************
2311                  *   P R O L O G
2312                  ******************/
2313
2314                 /* check the register pressure in the prolog */
2315                 /* sum_{L\U} lr <= k - |U| */
2316                 ir_snprintf(buf, sizeof(buf), "check_pre_%N", irn);
2317                 check_pre = lpp_add_cst_uniq(si->lpp, buf, lpp_less, si->n_regs - u);
2318
2319                 /* for the prolog remove defined values from the live set */
2320                 pset_foreach(defs, tmp) {
2321                         pset_remove_ptr(live, tmp);
2322                 }
2323
2324                 if(opt_memoperands && (!is_start_block(bb) || be_is_Barrier(irn))) {
2325                         ir_snprintf(buf, sizeof(buf), "one_memoperand_%N", irn);
2326                         one_memoperand = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 1.0);
2327                 }
2328
2329                 /***********************************************************
2330                  *  I T E R A T I O N  O V E R  A R G S  F O R  P R O L O G
2331                  **********************************************************/
2332
2333
2334                 set_foreach(args, keyval) {
2335                         spill_t          *spill;
2336                         const ir_node    *arg = keyval->key;
2337                         int               i = PTR_TO_INT(keyval->val);
2338                         op_t             *arg_op = get_irn_link(arg);
2339                         ilp_cst_t         requirements;
2340                         int               n_memoperands;
2341
2342                         spill = set_find_spill(spill_bb->ilp, arg);
2343                         assert(spill);
2344
2345                         ir_snprintf(buf, sizeof(buf), "reload_%N_%N", arg, irn);
2346                         op->attr.live_range.args.reloads[i] = lpp_add_var_default(si->lpp, buf, lpp_binary, opt_cost_reload*execution_frequency(si, bb), 1.0);
2347
2348                         /* reload <= mem_out */
2349                         ir_snprintf(buf, sizeof(buf), "req_reload_%N_%N", arg, irn);
2350                         cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
2351                         lpp_set_factor_fast(si->lpp, cst, op->attr.live_range.args.reloads[i], 1.0);
2352                         lpp_set_factor_fast(si->lpp, cst, spill->mem_out, -1.0);
2353
2354                         /* requirement: arg must be in register for use */
2355                         /* reload + remat + live_range == 1 */
2356                         ir_snprintf(buf, sizeof(buf), "req_%N_%N", irn, arg);
2357                         requirements = lpp_add_cst_uniq(si->lpp, buf, lpp_equal, 1.0);
2358
2359                         lpp_set_factor_fast(si->lpp, requirements, arg_op->attr.live_range.ilp, 1.0);
2360                         lpp_set_factor_fast(si->lpp, requirements, op->attr.live_range.args.reloads[i], 1.0);
2361                         foreach_pre_remat(si, irn, tmp) {
2362                                 op_t     *remat_op = get_irn_link(tmp);
2363                                 if(remat_op->attr.remat.remat->value == arg) {
2364                                         lpp_set_factor_fast(si->lpp, requirements, remat_op->attr.remat.ilp, 1.0);
2365                                 }
2366                         }
2367
2368                         if(opt_memoperands && (!is_start_block(bb) || be_is_Barrier(irn))) {
2369                                 n_memoperands = 0;
2370                                 for(n = get_irn_arity(irn)-1; n>=0; --n) {
2371                                         if(get_irn_n(irn, n) == arg) {
2372                                                 n_memoperands++;
2373                                         }
2374                                 }
2375                                 for(n = get_irn_arity(irn)-1; n>=0; --n) {
2376                                         if(get_irn_n(irn, n) == arg && arch_possible_memory_operand(arch_env, irn, n)) {
2377                                                 memoperand_t  *memoperand;
2378                                                 memoperand = set_find_memoperand(si->memoperands, irn, n);
2379
2380                                                 /* memoperand <= mem_out */
2381                                                 ir_snprintf(buf, sizeof(buf), "req_memoperand_%N_%d", irn, n);
2382                                                 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
2383                                                 lpp_set_factor_fast(si->lpp, cst, memoperand->ilp, 1.0);
2384                                                 lpp_set_factor_fast(si->lpp, cst, spill->mem_out, -1.0);
2385
2386                                                 /* the memoperand is only sufficient if it is used once by the op */
2387                                                 if(n_memoperands == 1)
2388                                                         lpp_set_factor_fast(si->lpp, requirements, memoperand->ilp, 1.0);
2389
2390                                                 lpp_set_factor_fast(si->lpp, one_memoperand, memoperand->ilp, 1.0);
2391
2392                                                 /* we have one more free register if we use a memory operand */
2393                                                 lpp_set_factor_fast(si->lpp, check_pre, memoperand->ilp, -1.0);
2394                                         }
2395                                 }
2396                         }
2397                 }
2398
2399                 /* iterate over L\U */
2400                 pset_foreach(live, tmp) {
2401                         if(!set_find_keyval(args, tmp)) {
2402                                 /* if a live value is not used by irn */
2403                                 tmp_op = get_irn_link(tmp);
2404                                 lpp_set_factor_fast(si->lpp, check_pre, tmp_op->attr.live_range.ilp, 1.0);
2405                         }
2406                 }
2407
2408                 /* requirements for remats */
2409                 foreach_pre_remat(si, irn, tmp) {
2410                         op_t        *remat_op = get_irn_link(tmp);
2411                         int          n;
2412
2413                         for (n=get_irn_arity(tmp)-1; n>=0; --n) {
2414                                 ir_node        *remat_arg = get_irn_n(tmp, n);
2415                                 op_t           *arg_op = get_irn_link(remat_arg);
2416
2417                                 if(!has_reg_class(si, remat_arg)) continue;
2418
2419                                 /* remat <= live_rang(remat_arg) [ + reload(remat_arg) ] */
2420                                 ir_snprintf(buf, sizeof(buf), "req_remat_%N_arg_%N", tmp, remat_arg);
2421                                 cst = lpp_add_cst(si->lpp, buf, lpp_less, 0.0);
2422
2423                                 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, 1.0);
2424                                 lpp_set_factor_fast(si->lpp, cst, arg_op->attr.live_range.ilp, -1.0);
2425
2426                                 /* if remat arg is also used by current op then we can use reload placed for this argument */
2427                                 if((keyval = set_find_keyval(args, remat_arg)) != NULL) {
2428                                         int    index = (int)keyval->val;
2429
2430                                         lpp_set_factor_fast(si->lpp, cst, op->attr.live_range.args.reloads[index], -1.0);
2431                                 }
2432                         }
2433                 }
2434
2435
2436
2437
2438                 /*************************
2439                  *  D O N E  W I T H  O P
2440                  *************************/
2441
2442                 DBG((si->dbg, LEVEL_4, "\t   %d values live at %+F\n", pset_count(live), irn));
2443
2444                 pset_foreach(live, tmp) {
2445                         assert(has_reg_class(si, tmp));
2446                 }
2447
2448                 for (n=get_irn_arity(irn)-1; n>=0; --n) {
2449                         ir_node        *arg = get_irn_n(irn, n);
2450
2451                         assert(!find_post_remat(arg, irn) && "there should be no post remat for an argument of an op");
2452                 }
2453
2454                 del_pset(remat_defs);
2455                 del_pset(used);
2456                 del_set(args);
2457                 del_pset(defs);
2458                 defs = pset_new_ptr_default();
2459
2460                 /* skip everything above barrier in start block */
2461                 if(is_start_block(bb) && be_is_Barrier(irn)) {
2462                         assert(pset_count(live) == 0);
2463                         break;
2464                 }
2465
2466         }
2467         del_pset(defs);
2468
2469
2470
2471         /***************************************
2472          *   B E G I N N I N G  O F  B L O C K
2473          ***************************************/
2474
2475
2476         /* we are now at the beginning of the basic block, there are only \Phis in front of us */
2477         DBG((si->dbg, LEVEL_3, "\t   %d values live at beginning of block %+F\n", pset_count(live), bb));
2478
2479         pset_foreach(live, irn) {
2480                 assert(is_Phi(irn) || get_nodes_block(irn) != bb);
2481         }
2482
2483         /* construct mem_outs for all values */
2484         set_foreach(spill_bb->ilp, spill) {
2485                 ir_snprintf(buf, sizeof(buf), "mem_out_%N_%N", spill->irn, bb);
2486                 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
2487
2488                 lpp_set_factor_fast(si->lpp, cst, spill->mem_out, 1.0);
2489                 lpp_set_factor_fast(si->lpp, cst, spill->spill, -1.0);
2490
2491                 if(pset_find_ptr(live, spill->irn)) {
2492                         int default_spilled;
2493                         DBG((si->dbg, LEVEL_5, "\t     %+F live at beginning of block %+F\n", spill->irn, bb));
2494
2495                         ir_snprintf(buf, sizeof(buf), "mem_in_%N_%N", spill->irn, bb);
2496                         default_spilled = be_is_live_in(si->lv, bb, spill->irn) || is_Phi(spill->irn);
2497                         spill->mem_in   = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, default_spilled);
2498                         lpp_set_factor_fast(si->lpp, cst, spill->mem_in, -1.0);
2499
2500                         if(opt_memcopies && is_Phi(spill->irn) && get_nodes_block(spill->irn) == bb) {
2501                                 int   n;
2502                                 op_t *op = get_irn_link(spill->irn);
2503
2504                                 for(n=get_irn_arity(spill->irn)-1; n>=0; --n) {
2505                                         const ir_node  *arg = get_irn_n(spill->irn, n);
2506                                         double          freq=0.0;
2507                                         int             m;
2508                                         ilp_var_t       var;
2509
2510
2511                                         /* argument already done? */
2512                                         if(op->attr.live_range.args.copies[n] != ILP_UNDEF) continue;
2513
2514                                         /* get sum of execution frequencies of blocks with the same phi argument */
2515                                         for(m=n; m>=0; --m) {
2516                                                 const ir_node  *arg2 = get_irn_n(spill->irn, m);
2517
2518                                                 if(arg==arg2) {
2519                                                         freq += execution_frequency(si, get_Block_cfgpred_block(bb, m));
2520                                                 }
2521                                         }
2522
2523                                         /* copies are not for free */
2524                                         ir_snprintf(buf, sizeof(buf), "copy_%N_%N", arg, spill->irn);
2525                                         var = lpp_add_var_default(si->lpp, buf, lpp_binary, opt_cost_spill * freq, 1.0);
2526
2527                                         for(m=n; m>=0; --m) {
2528                                                 const ir_node  *arg2 = get_irn_n(spill->irn, m);
2529
2530                                                 if(arg==arg2) {
2531                                                         op->attr.live_range.args.copies[m] = var;
2532                                                 }
2533                                         }
2534
2535 #if 0
2536                                         /* copy <= mem_in */
2537                                         ir_snprintf(buf, sizeof(buf), "nocopy_%N_%N", arg, spill->irn);
2538                                         cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
2539                                         lpp_set_factor_fast(si->lpp, cst, var, 1.0);
2540                                         lpp_set_factor_fast(si->lpp, cst, spill->mem_in, -1.0);
2541 #endif
2542                                 }
2543                         }
2544                 }
2545         }
2546
2547         foreach_post_remat(bb, tmp) {
2548                 int         n;
2549                 op_t       *remat_op = get_irn_link(tmp);
2550                 pset       *remat_args = pset_new_ptr(get_irn_arity(tmp));
2551                 ir_node    *remat_arg;
2552
2553                 for (n=get_irn_arity(tmp)-1; n>=0; --n) {
2554                         remat_arg = get_irn_n(tmp, n);
2555
2556                         if(has_reg_class(si, remat_arg)) {
2557                                 pset_insert_ptr(remat_args, remat_arg);
2558                         }
2559                 }
2560
2561                 /* remat + \sum live_range(remat_arg) <= |args| */
2562                 ir_snprintf(buf, sizeof(buf), "one_must_die_%N", tmp);
2563                 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, pset_count(remat_args));
2564                 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, 1.0);
2565
2566                 pset_foreach(remat_args, remat_arg) {
2567                         if(pset_find_ptr(live, remat_arg)) {
2568                                 op_t       *remat_arg_op = get_irn_link(remat_arg);
2569                                 lpp_set_factor_fast(si->lpp, cst, remat_arg_op->attr.live_range.ilp, 1.0);
2570                         }
2571                 }
2572                 del_pset(remat_args);
2573         }
2574
2575         foreach_post_remat(bb, tmp) {
2576                 int  n;
2577
2578                 for(n=get_irn_arity(tmp)-1; n>=0; --n) {
2579                         ir_node  *remat_arg = get_irn_n(tmp, n);
2580
2581                         /* if value is becoming live through use by remat2 */
2582                         if(has_reg_class(si, remat_arg) && !pset_find_ptr(live, remat_arg)) {
2583                                 op_t       *remat_arg_op = get_irn_link(remat_arg);
2584                                 ilp_cst_t   nomem;
2585
2586                                 DBG((si->dbg, LEVEL_3, "  value %+F becoming live through use by remat2 at bb start %+F\n", remat_arg, tmp));
2587
2588                                 pset_insert_ptr(live, remat_arg);
2589                                 spill = add_to_spill_bb(si, bb, remat_arg);
2590                                 remat_arg_op->attr.live_range.ilp = ILP_UNDEF;
2591
2592                                 /* we need reg_in and mem_in for this value; they will be referenced later */
2593                                 ir_snprintf(buf, sizeof(buf), "reg_in_%N_%N", remat_arg, bb);
2594                                 spill->reg_in = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, 0.0);
2595                                 ir_snprintf(buf, sizeof(buf), "mem_in_%N_%N", remat_arg, bb);
2596                                 spill->mem_in = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, 1.0);
2597
2598
2599                                 /* optimization: all memory stuff should be 0, for we do not want to insert reloads for remats */
2600                                 ir_snprintf(buf, sizeof(buf), "nomem_%N_%N", remat_arg, bb);
2601                                 nomem = lpp_add_cst_uniq(si->lpp, buf, lpp_equal, 0.0);
2602                                 lpp_set_factor_fast(si->lpp, nomem, spill->spill, 1.0);
2603                         }
2604                 }
2605         }
2606
2607         /* L\U is empty at bb start */
2608         /* arg is live throughout epilog if it is reg_in into this block */
2609
2610         /* check the register pressure at the beginning of the block
2611          * including remats
2612          */
2613         /* reg_in entspricht post_use */
2614
2615         ir_snprintf(buf, sizeof(buf), "check_start_%N", bb);
2616         cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, si->n_regs);
2617
2618         pset_foreach(live, irn) {
2619         ilp_cst_t  nospill;
2620
2621                 spill = set_find_spill(spill_bb->ilp, irn);
2622                 assert(spill);
2623
2624                 ir_snprintf(buf, sizeof(buf), "reg_in_%N_%N", irn, bb);
2625                 spill->reg_in = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, 0.0);
2626
2627                 lpp_set_factor_fast(si->lpp, cst, spill->reg_in, 1.0);
2628
2629                 /* spill + mem_in <= 1 */
2630                 ir_snprintf(buf, sizeof(buf), "nospill_%N_%N", irn, bb);
2631                 nospill = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 1);
2632
2633                 lpp_set_factor_fast(si->lpp, nospill, spill->mem_in, 1.0);
2634                 lpp_set_factor_fast(si->lpp, nospill, spill->spill, 1.0);
2635
2636         } /* post_remats are NOT included in register pressure check because
2637            they do not increase regpressure */
2638
2639         /* mem_in/reg_in for live_in values, especially phis and their arguments */
2640         pset_foreach(live, irn) {
2641                 int          p = 0,
2642                                          n;
2643
2644                 spill = set_find_spill(spill_bb->ilp, irn);
2645                 assert(spill && spill->irn == irn);
2646
2647                 if(is_Phi(irn) && get_nodes_block(irn) == bb) {
2648                         for (n=get_Phi_n_preds(irn)-1; n>=0; --n) {
2649                                 ilp_cst_t       mem_in,
2650                                                                 reg_in;
2651                                 ir_node        *phi_arg = get_Phi_pred(irn, n);
2652                                 ir_node        *bb_p = get_Block_cfgpred_block(bb, n);
2653                                 spill_bb_t     *spill_bb_p = get_irn_link(bb_p);
2654                                 spill_t        *spill_p;
2655                                 op_t           *op = get_irn_link(irn);
2656
2657                                 /* although the phi is in the right regclass one or more of
2658                                  * its arguments can be in a different one or at least to
2659                                  * ignore
2660                                  */
2661                                 if(has_reg_class(si, phi_arg)) {
2662                                         /* mem_in < mem_out_arg + copy */
2663                                         ir_snprintf(buf, sizeof(buf), "mem_in_%N_%N-%d", irn, bb, p);
2664                                         mem_in = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
2665
2666                                         /* reg_in < reg_out_arg */
2667                                         ir_snprintf(buf, sizeof(buf), "reg_in_%N_%N-%d", irn, bb, p++);
2668                                         reg_in = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
2669
2670                                         lpp_set_factor_fast(si->lpp, mem_in, spill->mem_in, 1.0);
2671                                         lpp_set_factor_fast(si->lpp, reg_in, spill->reg_in, 1.0);
2672
2673                                         spill_p = set_find_spill(spill_bb_p->ilp, phi_arg);
2674                                         assert(spill_p);
2675
2676                                         lpp_set_factor_fast(si->lpp, mem_in, spill_p->mem_out, -1.0);
2677                                         if(opt_memcopies)
2678                                                 lpp_set_factor_fast(si->lpp, mem_in, op->attr.live_range.args.copies[n], -1.0);
2679
2680                                         lpp_set_factor_fast(si->lpp, reg_in, spill_p->reg_out, -1.0);
2681                                 }
2682                         }
2683                 } else {
2684                         /* else assure the value arrives on all paths in the same resource */
2685
2686                         for (n=get_Block_n_cfgpreds(bb)-1; n>=0; --n) {
2687                                 ilp_cst_t       mem_in,
2688                                                                 reg_in;
2689                                 ir_node        *bb_p = get_Block_cfgpred_block(bb, n);
2690                                 spill_bb_t     *spill_bb_p = get_irn_link(bb_p);
2691                                 spill_t        *spill_p;
2692
2693                                 ir_snprintf(buf, sizeof(buf), "mem_in_%N_%N-%d", irn, bb, p);
2694                                 mem_in = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
2695                                 ir_snprintf(buf, sizeof(buf), "reg_in_%N_%N-%d", irn, bb, p++);
2696                                 reg_in = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
2697
2698                                 lpp_set_factor_fast(si->lpp, mem_in, spill->mem_in, 1.0);
2699                                 lpp_set_factor_fast(si->lpp, reg_in, spill->reg_in, 1.0);
2700
2701                                 spill_p = set_find_spill(spill_bb_p->ilp, irn);
2702                                 assert(spill_p);
2703
2704                                 lpp_set_factor_fast(si->lpp, mem_in, spill_p->mem_out, -1.0);
2705                                 lpp_set_factor_fast(si->lpp, reg_in, spill_p->reg_out, -1.0);
2706                         }
2707                 }
2708         }
2709
2710         foreach_post_remat(bb, tmp) {
2711                 int         n;
2712
2713                 for (n=get_irn_arity(tmp)-1; n>=0; --n) {
2714                         ir_node    *remat_arg = get_irn_n(tmp, n);
2715                         op_t       *remat_op = get_irn_link(tmp);
2716
2717                         if(!has_reg_class(si, remat_arg)) continue;
2718
2719                         spill = set_find_spill(spill_bb->ilp, remat_arg);
2720                         assert(spill);
2721
2722                         ir_snprintf(buf, sizeof(buf), "req_remat2_%N_%N_arg_%N", tmp, bb, remat_arg);
2723                         cst = lpp_add_cst(si->lpp, buf, lpp_less, 0.0);
2724                         lpp_set_factor_fast(si->lpp, cst, spill->reg_in, -1.0);
2725                         lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, 1.0);
2726                 }
2727         }
2728
2729         pset_foreach(live, irn) {
2730                 const op_t      *op = get_irn_link(irn);
2731                 const ir_node   *remat;
2732                 int              n_remats = 0;
2733
2734                 cst = ILP_UNDEF;
2735
2736                 foreach_post_remat(bb, remat) {
2737                         int   n;
2738
2739                         for (n=get_irn_arity(remat)-1; n>=0; --n) {
2740                                 const ir_node  *arg = get_irn_n(remat, n);
2741
2742                                 if(arg == irn) {
2743                                         const op_t   *remat_op = get_irn_link(remat);
2744
2745                                         if(cst == ILP_UNDEF) {
2746                                                 /* sum remat2s <= 1 + n_remats*live_range */
2747                                                 ir_snprintf(buf, sizeof(buf), "dying_lr_%N_%N", irn, bb);
2748                                                 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 1.0);
2749                                         }
2750                                         lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, 1.0);
2751                                         ++n_remats;
2752                                         break;
2753                                 }
2754                         }
2755                 }
2756                 if(cst != ILP_UNDEF && op->attr.live_range.ilp != ILP_UNDEF) {
2757                         lpp_set_factor_fast(si->lpp, cst, op->attr.live_range.ilp, -n_remats);
2758                 }
2759         }
2760
2761         /* first live ranges from reg_ins */
2762         pset_foreach(live, irn) {
2763                 op_t      *op = get_irn_link(irn);
2764
2765                 if(op->attr.live_range.ilp != ILP_UNDEF) {
2766
2767                         spill = set_find_spill(spill_bb->ilp, irn);
2768                         assert(spill && spill->irn == irn);
2769
2770                         ir_snprintf(buf, sizeof(buf), "first_lr_%N_%N", irn, bb);
2771                         cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
2772                         lpp_set_factor_fast(si->lpp, cst, op->attr.live_range.ilp, 1.0);
2773                         lpp_set_factor_fast(si->lpp, cst, spill->reg_in, -1.0);
2774
2775                         foreach_post_remat(bb, tmp) {
2776                                 op_t     *remat_op = get_irn_link(tmp);
2777
2778                                 if(remat_op->attr.remat.remat->value == irn) {
2779                                         lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, -1.0);
2780                                 }
2781                         }
2782                 }
2783         }
2784
2785         /* walk forward now and compute constraints for placing spills */
2786         /* this must only be done for values that are not defined in this block */
2787         pset_foreach(live, irn) {
2788                 /*
2789                  * if value is defined in this block we can anways place the spill directly after the def
2790                  *    -> no constraint necessary
2791                  */
2792                 if(!is_Phi(irn) && get_nodes_block(irn) == bb) {
2793                         assert(0);
2794                 }
2795
2796
2797                 spill = set_find_spill(spill_bb->ilp, irn);
2798                 assert(spill);
2799
2800                 ir_snprintf(buf, sizeof(buf), "req_spill_%N_%N", irn, bb);
2801                 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
2802
2803                 lpp_set_factor_fast(si->lpp, cst, spill->spill, 1.0);
2804                 if(is_diverge_edge(bb)) lpp_set_factor_fast(si->lpp, cst, spill->reg_in, -1.0);
2805
2806                 if(!is_Phi(irn)) {
2807                         sched_foreach_op(bb, tmp) {
2808                                 op_t   *op = get_irn_link(tmp);
2809
2810                                 if(is_Phi(tmp)) continue;
2811                                 assert(!is_Proj(tmp));
2812
2813                                 if(op->is_remat) {
2814                                         const ir_node   *value = op->attr.remat.remat->value;
2815
2816                                         if(value == irn) {
2817                                                 /* only collect remats up to the first real use of a value */
2818                                                 lpp_set_factor_fast(si->lpp, cst, op->attr.remat.ilp, -1.0);
2819                                         }
2820                                 } else {
2821                                         int   n;
2822
2823                                         for (n=get_irn_arity(tmp)-1; n>=0; --n) {
2824                                                 ir_node    *arg = get_irn_n(tmp, n);
2825
2826                                                 if(arg == irn) {
2827                                                         /* if a value is used stop collecting remats */
2828                             goto next_live;
2829                                                 }
2830                                         }
2831                                 }
2832                         }
2833                 }
2834 next_live: ;
2835         }
2836
2837         del_pset(live);
2838 }
2839
2840 typedef struct _irnlist_t {
2841         struct list_head   list;
2842         ir_node           *irn;
2843 } irnlist_t;
2844
2845 typedef struct _interference_t {
2846         struct list_head    blocklist;
2847         ir_node            *a;
2848         ir_node            *b;
2849 } interference_t;
2850
2851 static int
2852 cmp_interference(const void *a, const void *b, size_t size)
2853 {
2854         const interference_t *p = a;
2855         const interference_t *q = b;
2856
2857         return !(p->a == q->a && p->b == q->b);
2858 }
2859
2860 static interference_t *
2861 set_find_interference(set * set, ir_node * a, ir_node * b)
2862 {
2863         interference_t     query;
2864
2865         query.a = (a>b)?a:b;
2866         query.b = (a>b)?b:a;
2867
2868         return set_find(set, &query, sizeof(query), HASH_PTR(PTR_TO_INT(a) ^ PTR_TO_INT(b)));
2869 }
2870
2871 static interference_t *
2872 set_insert_interference(spill_ilp_t * si, set * set, ir_node * a, ir_node * b, ir_node * bb)
2873 {
2874         interference_t     query,
2875                                           *result;
2876         irnlist_t         *list = obstack_alloc(si->obst, sizeof(*list));
2877
2878         list->irn = bb;
2879
2880         result = set_find_interference(set, a, b);
2881         if(result) {
2882
2883                 list_add(&list->list, &result->blocklist);
2884                 return result;
2885         }
2886
2887         query.a = (a>b)?a:b;
2888         query.b = (a>b)?b:a;
2889
2890         result = set_insert(set, &query, sizeof(query), HASH_PTR(PTR_TO_INT(a) ^ PTR_TO_INT(b)));
2891
2892         INIT_LIST_HEAD(&result->blocklist);
2893         list_add(&list->list, &result->blocklist);
2894
2895         return result;
2896 }
2897
2898 static int
2899 values_interfere_in_block(const spill_ilp_t * si, const ir_node * bb, const ir_node * a, const ir_node * b)
2900 {
2901         const ir_edge_t *edge;
2902
2903         if(get_nodes_block(a) != bb && get_nodes_block(b) != bb) {
2904                 /* both values are live in, so they interfere */
2905                 return 1;
2906         }
2907
2908         /* ensure a dominates b */
2909         if(value_dominates(b,a)) {
2910                 const ir_node * t;
2911                 t = b;
2912                 b = a;
2913                 a = t;
2914         }
2915         assert(get_nodes_block(b) == bb && "at least b should be defined here in this block");
2916
2917
2918         /* the following code is stolen from bera.c */
2919         if(be_is_live_end(si->lv, bb, a))
2920                 return 1;
2921
2922         foreach_out_edge(a, edge) {
2923                 const ir_node *user = edge->src;
2924                 if(get_nodes_block(user) == bb
2925                                 && !is_Phi(user)
2926                                 && b != user
2927                                 && !pset_find_ptr(si->inverse_ops, user)
2928                                 && value_dominates(b, user))
2929                         return 1;
2930         }
2931
2932         return 0;
2933 }
2934
2935 /**
2936  * Walk all irg blocks and collect interfering values inside of phi classes
2937  */
2938 static void
2939 luke_interferencewalker(ir_node * bb, void * data)
2940 {
2941         spill_ilp_t    *si = (spill_ilp_t*)data;
2942         int             l1, l2;
2943
2944         be_lv_foreach(si->lv, bb, be_lv_state_end | be_lv_state_out | be_lv_state_in, l1) {
2945                 ir_node        *a = be_lv_get_irn(si->lv, bb, l1);
2946                 op_t           *a_op = get_irn_link(a);
2947
2948
2949                 /* a is only interesting if it is in my register class and if it is inside a phi class */
2950                 if (has_reg_class(si, a) && get_phi_class(a)) {
2951                         if(a_op->is_remat || pset_find_ptr(si->inverse_ops, a))
2952                                 continue;
2953
2954                         for(l2=_be_lv_next_irn(si->lv, bb, 0xff, l1+1); l2>=0; l2=_be_lv_next_irn(si->lv, bb, 0xff, l2+1)) {
2955                                 ir_node        *b = be_lv_get_irn(si->lv, bb, l2);
2956                                 op_t           *b_op = get_irn_link(b);
2957
2958
2959                                 /* a and b are only interesting if they are in the same phi class */
2960                                 if(has_reg_class(si, b) && get_phi_class(a) == get_phi_class(b)) {
2961                                         if(b_op->is_remat || pset_find_ptr(si->inverse_ops, b))
2962                                                 continue;
2963
2964                                         if(values_interfere_in_block(si, bb, a, b)) {
2965                                                 DBG((si->dbg, LEVEL_4, "\tvalues interfere in %+F: %+F, %+F\n", bb, a, b));
2966                                                 set_insert_interference(si, si->interferences, a, b, bb);
2967                                         }
2968                                 }
2969                         }
2970                 }
2971         }
2972 }
2973
2974 static unsigned int copy_path_id = 0;
2975
2976 static void
2977 write_copy_path_cst(spill_ilp_t *si, pset * copies, ilp_var_t any_interfere)
2978 {
2979         ilp_cst_t  cst;
2980         ilp_var_t  copy;
2981         char       buf[256];
2982         void      *ptr;
2983
2984         ir_snprintf(buf, sizeof(buf), "copy_path-%d", copy_path_id++);
2985         cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0);
2986
2987         lpp_set_factor_fast(si->lpp, cst, any_interfere, 1.0);
2988
2989         pset_foreach(copies, ptr) {
2990                 copy = PTR_TO_INT(ptr);
2991                 lpp_set_factor_fast(si->lpp, cst, copy, -1.0);
2992         }
2993 }
2994
2995 /**
2996  * @parameter copies   contains a path of copies which lead us to irn
2997  * @parameter visited  contains a set of nodes already visited on this path
2998  */
2999 static int
3000 find_copy_path(spill_ilp_t * si, const ir_node * irn, const ir_node * target, ilp_var_t any_interfere, pset * copies, pset * visited)
3001 {
3002         const ir_edge_t *edge;
3003         op_t            *op = get_irn_link(irn);
3004     pset            *visited_users = pset_new_ptr_default();
3005         int              paths = 0;
3006
3007         if(op->is_remat) return 0;
3008
3009         pset_insert_ptr(visited, irn);
3010
3011         if(is_Phi(irn)) {
3012                 int    n;
3013         pset  *visited_operands = pset_new_ptr(get_irn_arity(irn));
3014
3015                 /* visit all operands */
3016                 for(n=get_irn_arity(irn)-1; n>=0; --n) {
3017                         ir_node  *arg = get_irn_n(irn, n);
3018                         ilp_var_t  copy = op->attr.live_range.args.copies[n];
3019
3020                         if(!has_reg_class(si, arg)) continue;
3021             if(pset_find_ptr(visited_operands, arg)) continue;
3022             pset_insert_ptr(visited_operands, arg);
3023
3024                         if(arg == target) {
3025                                 if(++paths > MAX_PATHS && pset_count(copies) != 0) {
3026                                         del_pset(visited_operands);
3027                                         del_pset(visited_users);
3028                                         pset_remove_ptr(visited, irn);
3029                                         return paths;
3030                                 }
3031                                 pset_insert(copies, INT_TO_PTR(copy), copy);
3032                                 write_copy_path_cst(si, copies, any_interfere);
3033                                 pset_remove(copies, INT_TO_PTR(copy), copy);
3034                         } else if(!pset_find_ptr(visited, arg)) {
3035                                 pset_insert(copies, INT_TO_PTR(copy), copy);
3036                                 paths += find_copy_path(si, arg, target, any_interfere, copies, visited);
3037                                 pset_remove(copies, INT_TO_PTR(copy), copy);
3038
3039                 if(paths > MAX_PATHS) {
3040                     if(pset_count(copies) == 0) {
3041                         ilp_cst_t  cst;
3042                         char       buf[256];
3043
3044                         ir_snprintf(buf, sizeof(buf), "always_copy-%d-%d", any_interfere, copy);
3045                         cst = lpp_add_cst_uniq(si->lpp, buf, lpp_equal, 0);
3046                         lpp_set_factor_fast(si->lpp, cst, any_interfere, -1.0);
3047                         lpp_set_factor_fast(si->lpp, cst, copy, 1.0);
3048                         DBG((si->dbg, LEVEL_1, "ALWAYS COPYING %d FOR INTERFERENCE %d\n", copy, any_interfere));
3049
3050                         paths = 0;
3051                     } else {
3052                         del_pset(visited_operands);
3053                         del_pset(visited_users);
3054                         pset_remove_ptr(visited, irn);
3055                         return paths;
3056                     }
3057                 } else if(pset_count(copies) == 0) {
3058                                         paths = 0;
3059                                 }
3060                         }
3061                 }
3062
3063         del_pset(visited_operands);
3064         }
3065
3066         /* visit all uses which are phis */
3067         foreach_out_edge(irn, edge) {
3068                 ir_node  *user = edge->src;
3069                 int       pos  = edge->pos;
3070                 op_t     *op = get_irn_link(user);
3071                 ilp_var_t copy;
3072
3073                 if(!is_Phi(user)) continue;
3074                 if(!has_reg_class(si, user)) continue;
3075         if(pset_find_ptr(visited_users, user)) continue;
3076         pset_insert_ptr(visited_users, user);
3077
3078                 copy = op->attr.live_range.args.copies[pos];
3079
3080                 if(user == target) {
3081                         if(++paths > MAX_PATHS && pset_count(copies) != 0) {
3082                                 del_pset(visited_users);
3083                                 pset_remove_ptr(visited, irn);
3084                                 return paths;
3085                         }
3086                         pset_insert(copies, INT_TO_PTR(copy), copy);
3087                         write_copy_path_cst(si, copies, any_interfere);
3088                         pset_remove(copies, INT_TO_PTR(copy), copy);
3089                 } else if(!pset_find_ptr(visited, user)) {
3090                         pset_insert(copies, INT_TO_PTR(copy), copy);
3091                         paths += find_copy_path(si, user, target, any_interfere, copies, visited);
3092                         pset_remove(copies, INT_TO_PTR(copy), copy);
3093
3094             if(paths > MAX_PATHS) {
3095                 if(pset_count(copies) == 0) {
3096                     ilp_cst_t  cst;
3097                     char       buf[256];
3098
3099                     ir_snprintf(buf, sizeof(buf), "always_copy-%d-%d", any_interfere, copy);
3100                     cst = lpp_add_cst_uniq(si->lpp, buf, lpp_equal, 0);
3101                     lpp_set_factor_fast(si->lpp, cst, any_interfere, -1.0);
3102                     lpp_set_factor_fast(si->lpp, cst, copy, 1.0);
3103                     DBG((si->dbg, LEVEL_1, "ALWAYS COPYING %d FOR INTERFERENCE %d\n", copy, any_interfere));
3104
3105                     paths = 0;
3106                 } else {
3107                     del_pset(visited_users);
3108                     pset_remove_ptr(visited, irn);
3109                     return paths;
3110                 }
3111             } else if(pset_count(copies) == 0) {
3112                                 paths = 0;
3113                         }
3114                 }
3115         }
3116
3117     del_pset(visited_users);
3118         pset_remove_ptr(visited, irn);
3119         return paths;
3120 }
3121
3122 static void
3123 gen_copy_constraints(spill_ilp_t * si, const ir_node * a, const ir_node * b, ilp_var_t any_interfere)
3124 {
3125         pset * copies = pset_new_ptr_default();
3126         pset * visited = pset_new_ptr_default();
3127
3128         find_copy_path(si, a, b, any_interfere, copies, visited);
3129
3130         del_pset(visited);
3131         del_pset(copies);
3132 }
3133
3134
3135 static void
3136 memcopyhandler(spill_ilp_t * si)
3137 {
3138         interference_t   *interference;
3139         char              buf[256];
3140         /* teste Speicherwerte auf Interferenz */
3141
3142         /* analyze phi classes */
3143         phi_class_compute(si->chordal_env->irg);
3144
3145         DBG((si->dbg, LEVEL_2, "\t calling interferencewalker\n"));
3146         irg_block_walk_graph(si->chordal_env->irg, luke_interferencewalker, NULL, si);
3147
3148         /* now lets emit the ILP unequations for the crap */
3149         set_foreach(si->interferences, interference) {
3150                 irnlist_t      *irnlist;
3151                 ilp_var_t       interfere,
3152                                                 any_interfere;
3153                 ilp_cst_t       any_interfere_cst,
3154                                                 cst;
3155                 const ir_node  *a  = interference->a;
3156                 const ir_node  *b  = interference->b;
3157
3158                 /* any_interf <= \sum interf */
3159                 ir_snprintf(buf, sizeof(buf), "interfere_%N_%N", a, b);
3160                 any_interfere_cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0);
3161                 any_interfere = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, 1.0);
3162
3163                 lpp_set_factor_fast(si->lpp, any_interfere_cst, any_interfere, 1.0);
3164
3165                 list_for_each_entry(irnlist_t, irnlist, &interference->blocklist, list) {
3166                         const ir_node  *bb = irnlist->irn;
3167                         spill_bb_t     *spill_bb = get_irn_link(bb);
3168                         spill_t        *spilla,
3169                                                    *spillb;
3170                         char           buf[256];
3171
3172                         spilla = set_find_spill(spill_bb->ilp, a);
3173                         assert(spilla);
3174
3175                         spillb = set_find_spill(spill_bb->ilp, b);
3176                         assert(spillb);
3177
3178                         /* interfere <-> (mem_in_a or spill_a) and (mem_in_b or spill_b): */
3179                         /* 1:   mem_in_a + mem_in_b + spill_a + spill_b - interfere <= 1 */
3180                         /* 2: - mem_in_a - spill_a + interfere <= 0 */
3181                         /* 3: - mem_in_b - spill_b + interfere <= 0 */
3182                         ir_snprintf(buf, sizeof(buf), "interfere_%N_%N_%N", bb, a, b);
3183                         interfere = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, 1.0);
3184
3185                         ir_snprintf(buf, sizeof(buf), "interfere_%N_%N_%N-1", bb, a, b);
3186                         cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 1);
3187
3188                         lpp_set_factor_fast(si->lpp, cst, interfere, -1.0);
3189                         if(spilla->mem_in != ILP_UNDEF) lpp_set_factor_fast(si->lpp, cst, spilla->mem_in, 1.0);
3190                         lpp_set_factor_fast(si->lpp, cst, spilla->spill, 1.0);
3191                         if(spillb->mem_in != ILP_UNDEF) lpp_set_factor_fast(si->lpp, cst, spillb->mem_in, 1.0);
3192                         lpp_set_factor_fast(si->lpp, cst, spillb->spill, 1.0);
3193
3194                         ir_snprintf(buf, sizeof(buf), "interfere_%N_%N_%N-2", bb, a, b);
3195                         cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0);
3196
3197                         lpp_set_factor_fast(si->lpp, cst, interfere, 1.0);
3198                         if(spilla->mem_in != ILP_UNDEF) lpp_set_factor_fast(si->lpp, cst, spilla->mem_in, -1.0);
3199                         lpp_set_factor_fast(si->lpp, cst, spilla->spill, -1.0);
3200
3201                         ir_snprintf(buf, sizeof(buf), "interfere_%N_%N_%N-3", bb, a, b);
3202                         cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0);
3203
3204                         lpp_set_factor_fast(si->lpp, cst, interfere, 1.0);
3205                         if(spillb->mem_in != ILP_UNDEF) lpp_set_factor_fast(si->lpp, cst, spillb->mem_in, -1.0);
3206                         lpp_set_factor_fast(si->lpp, cst, spillb->spill, -1.0);
3207
3208
3209                         lpp_set_factor_fast(si->lpp, any_interfere_cst, interfere, -1.0);
3210
3211                         /* any_interfere >= interf */
3212                         ir_snprintf(buf, sizeof(buf), "interfere_%N_%N-%N", a, b, bb);
3213                         cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0);
3214
3215                         lpp_set_factor_fast(si->lpp, cst, interfere, 1.0);
3216                         lpp_set_factor_fast(si->lpp, cst, any_interfere, -1.0);
3217                 }
3218
3219                 /* now that we know whether the two values interfere in memory we can drop constraints to enforce copies */
3220                 gen_copy_constraints(si,a,b,any_interfere);
3221         }
3222 }
3223
3224
3225 static INLINE int
3226 is_zero(double x)
3227 {
3228         return fabs(x) < 0.00001;
3229 }
3230
3231 static int mark_remat_nodes_hook(FILE *F, ir_node *n, ir_node *l)
3232 {
3233         spill_ilp_t *si = get_irg_link(current_ir_graph);
3234
3235         if(pset_find_ptr(si->all_possible_remats, n)) {
3236                 op_t   *op = (op_t*)get_irn_link(n);
3237                 assert(op && op->is_remat);
3238
3239                 if(!op->attr.remat.remat->inverse) {
3240                         if(op->attr.remat.pre) {
3241                                 ir_fprintf(F, "color:red info3:\"remat value: %+F\"", op->attr.remat.remat->value);
3242                         } else {
3243                                 ir_fprintf(F, "color:orange info3:\"remat2 value: %+F\"", op->attr.remat.remat->value);
3244                         }
3245
3246                         return 1;
3247                 } else {
3248                         op_t   *op = (op_t*)get_irn_link(n);
3249                         assert(op && op->is_remat);
3250
3251                         if(op->attr.remat.pre) {
3252                                 ir_fprintf(F, "color:cyan info3:\"remat inverse value: %+F\"", op->attr.remat.remat->value);
3253                         } else {
3254                                 ir_fprintf(F, "color:lightcyan info3:\"remat2 inverse value: %+F\"", op->attr.remat.remat->value);
3255                         }
3256
3257                         return 1;
3258                 }
3259         }
3260
3261         return 0;
3262 }
3263
3264 static void
3265 dump_graph_with_remats(ir_graph * irg, const char * suffix)
3266 {
3267         set_dump_node_vcgattr_hook(mark_remat_nodes_hook);
3268         be_dump(irg, suffix, dump_ir_block_graph_sched);
3269         set_dump_node_vcgattr_hook(NULL);
3270 }
3271
3272 /**
3273  * Edge hook to dump the schedule edges with annotated register pressure.
3274  */
3275 static int
3276 sched_pressure_edge_hook(FILE *F, ir_node *irn)
3277 {
3278         if(sched_is_scheduled(irn) && sched_has_prev(irn)) {
3279                 ir_node *prev = sched_prev(irn);
3280                 fprintf(F, "edge:{sourcename:\"");
3281                 PRINT_NODEID(irn);
3282                 fprintf(F, "\" targetname:\"");
3283                 PRINT_NODEID(prev);
3284                 fprintf(F, "\" label:\"%d", (int)get_irn_link(irn));
3285                 fprintf(F, "\" color:magenta}\n");
3286         }
3287         return 1;
3288 }
3289
3290 static void
3291 dump_ir_block_graph_sched_pressure(ir_graph *irg, const char *suffix)
3292 {
3293         DUMP_NODE_EDGE_FUNC old_edge_hook = get_dump_node_edge_hook();
3294
3295         dump_consts_local(0);
3296         set_dump_node_edge_hook(sched_pressure_edge_hook);
3297         dump_ir_block_graph(irg, suffix);
3298         set_dump_node_edge_hook(old_edge_hook);
3299 }
3300
3301 static void
3302 walker_pressure_annotator(ir_node * bb, void * data)
3303 {
3304         spill_ilp_t  *si = data;
3305         ir_node      *irn;
3306         int           n, i;
3307         pset         *live = pset_new_ptr_default();
3308         int           projs = 0;
3309
3310         be_lv_foreach(si->lv, bb, be_lv_state_end, i) {
3311                 irn = be_lv_get_irn(si->lv, bb, i);
3312
3313                 if (has_reg_class(si, irn)) {
3314                         pset_insert_ptr(live, irn);
3315                 }
3316         }
3317
3318         set_irn_link(bb, INT_TO_PTR(pset_count(live)));
3319
3320         sched_foreach_reverse(bb, irn) {
3321                 if(is_Phi(irn)) {
3322                         set_irn_link(irn, INT_TO_PTR(pset_count(live)));
3323                         continue;
3324                 }
3325
3326                 if(has_reg_class(si, irn)) {
3327                         pset_remove_ptr(live, irn);
3328                         if(is_Proj(irn)) ++projs;
3329                 }
3330
3331                 if(!is_Proj(irn)) projs = 0;
3332
3333                 for (n=get_irn_arity(irn)-1; n>=0; --n) {
3334                         ir_node    *arg = get_irn_n(irn, n);
3335
3336                         if(has_reg_class(si, arg)) pset_insert_ptr(live, arg);
3337                 }
3338                 set_irn_link(irn, INT_TO_PTR(pset_count(live)+projs));
3339         }
3340
3341         del_pset(live);
3342 }
3343
3344 static void
3345 dump_pressure_graph(spill_ilp_t * si, const char *suffix)
3346 {
3347         be_dump(si->chordal_env->irg, suffix, dump_ir_block_graph_sched_pressure);
3348 }
3349
3350 static void
3351 connect_all_remats_with_keep(spill_ilp_t * si)
3352 {
3353         ir_node   *irn;
3354         ir_node  **ins,
3355                          **pos;
3356         int        n_remats;
3357
3358
3359         n_remats = pset_count(si->all_possible_remats);
3360         if(n_remats) {
3361                 ins = obstack_alloc(si->obst, n_remats * sizeof(*ins));
3362
3363                 pos = ins;
3364                 pset_foreach(si->all_possible_remats, irn) {
3365                         *pos = irn;
3366                         ++pos;
3367                 }
3368
3369                 si->keep = be_new_Keep(si->chordal_env->cls, si->chordal_env->irg, get_irg_end_block(si->chordal_env->irg), n_remats, ins);
3370
3371                 obstack_free(si->obst, ins);
3372         }
3373 }
3374
3375 static void
3376 connect_all_spills_with_keep(spill_ilp_t * si)
3377 {
3378         ir_node   *irn;
3379         ir_node  **ins,
3380                          **pos;
3381         int        n_spills;
3382         ir_node   *keep;
3383
3384
3385         n_spills = pset_count(si->spills);
3386         if(n_spills) {
3387                 ins = obstack_alloc(si->obst, n_spills * sizeof(*ins));
3388
3389                 pos = ins;
3390                 pset_foreach(si->spills, irn) {
3391                         *pos = irn;
3392                         ++pos;
3393                 }
3394
3395                 keep = be_new_Keep(si->chordal_env->cls, si->chordal_env->irg, get_irg_end_block(si->chordal_env->irg), n_spills, ins);
3396
3397                 obstack_free(si->obst, ins);
3398         }
3399 }
3400
3401 /** insert a spill at an arbitrary position */
3402 ir_node *be_spill2(const arch_env_t *arch_env, ir_node *irn, ir_node *insert)
3403 {
3404         ir_node *bl     = is_Block(insert)?insert:get_nodes_block(insert);
3405         ir_graph *irg   = get_irn_irg(bl);
3406         ir_node *frame  = get_irg_frame(irg);
3407         ir_node *spill;
3408         ir_node *next;
3409
3410         const arch_register_class_t *cls       = arch_get_irn_reg_class(arch_env, irn, -1);
3411         const arch_register_class_t *cls_frame = arch_get_irn_reg_class(arch_env, frame, -1);
3412
3413         spill = be_new_Spill(cls, cls_frame, irg, bl, frame, irn);
3414
3415         /*
3416          * search the right insertion point. a spill of a phi cannot be put
3417          * directly after the phi, if there are some phis behind the one which
3418          * is spilled. Also, a spill of a Proj must be after all Projs of the
3419          * same tuple node.
3420          *
3421          * Here's one special case:
3422          * If the spill is in the start block, the spill must be after the frame
3423          * pointer is set up. This is done by setting insert to the end of the block
3424          * which is its default initialization (see above).
3425          */
3426
3427         if(bl == get_irg_start_block(irg) && sched_get_time_step(frame) >= sched_get_time_step(insert))
3428                 insert = frame;
3429
3430         for (next = sched_next(insert); is_Phi(next) || is_Proj(next); next = sched_next(insert))
3431                 insert = next;
3432
3433         sched_add_after(insert, spill);
3434         return spill;
3435 }
3436
3437 static void
3438 delete_remat(spill_ilp_t * si, ir_node * remat) {
3439         int       n;
3440         ir_node  *bad = get_irg_bad(si->chordal_env->irg);
3441
3442         sched_remove(remat);
3443
3444         /* kill links to operands */
3445         for (n=get_irn_arity(remat)-1; n>=-1; --n) {
3446                 set_irn_n(remat, n, bad);
3447         }
3448 }
3449
3450 static void
3451 clean_remat_info(spill_ilp_t * si)
3452 {
3453         int            n;
3454         remat_t       *remat;
3455         remat_info_t  *remat_info;
3456         ir_node       *bad = get_irg_bad(si->chordal_env->irg);
3457
3458         set_foreach(si->remat_info, remat_info) {
3459                 if(!remat_info->remats) continue;
3460
3461                 pset_foreach(remat_info->remats, remat)
3462                 {
3463                         if(remat->proj && get_irn_n_edges(remat->proj) == 0) {
3464                                 if(sched_is_scheduled(remat->proj)) {
3465                                         sched_remove((ir_node*)remat->proj);
3466                                 }
3467                                 set_irn_n((ir_node*)remat->proj, -1, bad);
3468                                 set_irn_n((ir_node*)remat->proj, 0, bad);
3469                         }
3470
3471                         if(get_irn_n_edges(remat->op) == 0) {
3472                                 if(sched_is_scheduled(remat->op)) {
3473                                         sched_remove((ir_node*)remat->op);
3474                                 }
3475                                 for (n=get_irn_arity(remat->op)-1; n>=-1; --n) {
3476                                         set_irn_n((ir_node*)remat->op, n, bad);
3477                                 }
3478                         }
3479                 }
3480
3481                 if(remat_info->remats) del_pset(remat_info->remats);
3482                 if(remat_info->remats_by_operand) del_pset(remat_info->remats_by_operand);
3483         }
3484 }
3485
3486 static void
3487 delete_unnecessary_remats(spill_ilp_t * si)
3488 {
3489         if(opt_keep_alive & KEEPALIVE_REMATS) {
3490                 int       n;
3491                 ir_node  *bad = get_irg_bad(si->chordal_env->irg);
3492
3493                 if(si->keep) {
3494                         for (n=get_irn_arity(si->keep)-1; n>=0; --n) {
3495                                 ir_node        *keep_arg = get_irn_n(si->keep, n);
3496                                 op_t           *arg_op = get_irn_link(keep_arg);
3497                                 lpp_name_t     *name;
3498
3499                                 assert(arg_op->is_remat);
3500
3501                                 name = si->lpp->vars[arg_op->attr.remat.ilp];
3502
3503                                 if(is_zero(name->value)) {
3504                                         DBG((si->dbg, LEVEL_3, "\t  deleting remat %+F\n", keep_arg));
3505                                         /* TODO check whether reload is preferred over remat (could be bug) */
3506                                         delete_remat(si, keep_arg);
3507                                 } else {
3508                                         if(!arg_op->attr.remat.remat->inverse) {
3509                                                 if(arg_op->attr.remat.pre) {
3510                                                         DBG((si->dbg, LEVEL_2, "\t**remat kept: %+F\n", keep_arg));
3511                                                 } else {
3512                                                         DBG((si->dbg, LEVEL_2, "\t%%%%remat2 kept: %+F\n", keep_arg));
3513                                                 }
3514                                         } else {
3515                                                 if(arg_op->attr.remat.pre) {
3516                                                         DBG((si->dbg, LEVEL_2, "\t**INVERSE remat kept: %+F\n", keep_arg));
3517                                                 } else {
3518                                                         DBG((si->dbg, LEVEL_2, "\t%%%%INVERSE remat2 kept: %+F\n", keep_arg));
3519                                                 }
3520                                         }
3521                                 }
3522
3523                                 set_irn_n(si->keep, n, bad);
3524                         }
3525                 } else {
3526                         DBG((si->dbg, LEVEL_2, "\t  no remats to delete (none have been inserted)\n"));
3527                 }
3528         } else {
3529                 ir_node  *remat;
3530
3531                 pset_foreach(si->all_possible_remats, remat) {
3532                         op_t           *remat_op = get_irn_link(remat);
3533                         lpp_name_t     *name = si->lpp->vars[remat_op->attr.remat.ilp];
3534
3535                         if(is_zero(name->value)) {
3536                                 DBG((si->dbg, LEVEL_3, "\t  deleting remat %+F\n", remat));
3537                                 /* TODO check whether reload is preferred over remat (could be bug) */
3538                                 delete_remat(si, remat);
3539                         } else {
3540                                 if(!remat_op->attr.remat.remat->inverse) {
3541                                         if(remat_op->attr.remat.pre) {
3542                                                 DBG((si->dbg, LEVEL_2, "\t**remat kept: %+F\n", remat));
3543                                         } else {
3544                                                 DBG((si->dbg, LEVEL_2, "\t%%%%remat2 kept: %+F\n", remat));
3545                                         }
3546                                 } else {
3547                                         if(remat_op->attr.remat.pre) {
3548                                                 DBG((si->dbg, LEVEL_2, "\t**INVERSE remat kept: %+F\n", remat));
3549                                         } else {
3550                                                 DBG((si->dbg, LEVEL_2, "\t%%%%INVERSE remat2 kept: %+F\n", remat));
3551                                         }
3552                                 }
3553                         }
3554                 }
3555         }
3556 }
3557
3558 static pset *
3559 get_spills_for_value(spill_ilp_t * si, const ir_node * value)
3560 {
3561         pset     *spills = pset_new_ptr_default();
3562
3563         const ir_node  *next;
3564         defs_t         *defs;
3565
3566         defs = set_find_def(si->values, value);
3567
3568         if(defs && defs->spills) {
3569                 for(next = defs->spills; next; next = get_irn_link(next)) {
3570                         pset_insert_ptr(spills, next);
3571                 }
3572         }
3573
3574         return spills;
3575 }
3576
3577 static ir_node *
3578 new_r_PhiM_nokeep(ir_graph * irg, ir_node *block, int arity, ir_node **in)
3579 {
3580         ir_node  *res;
3581
3582         assert( get_irn_arity(block) == arity );
3583
3584         res = new_ir_node(NULL, irg, block, op_Phi, mode_M, arity, in);
3585         res->attr.phi_backedge = new_backedge_arr(irg->obst, arity);
3586
3587         return res;
3588 }
3589
3590 /**
3591  * @param before   The node after which the spill will be placed in the schedule
3592  */
3593 static ir_node *
3594 insert_spill(spill_ilp_t * si, ir_node * irn, const ir_node * value, ir_node * before)
3595 {
3596         defs_t   *defs;
3597         ir_node  *spill;
3598         const arch_env_t *arch_env = si->chordal_env->birg->main_env->arch_env;
3599
3600         DBG((si->dbg, LEVEL_3, "\t  inserting spill for value %+F after %+F\n", irn, before));
3601
3602         spill = be_spill2(arch_env, irn, before);
3603
3604         defs = set_insert_def(si->values, value);
3605         assert(defs);
3606
3607         /* enter into the linked list */
3608         set_irn_link(spill, defs->spills);
3609         defs->spills = spill;
3610
3611         if(opt_keep_alive & KEEPALIVE_SPILLS)
3612                 pset_insert_ptr(si->spills, spill);
3613
3614         return spill;
3615 }
3616
3617 /**
3618  * @param before   The Phi node which has to be spilled
3619  */
3620 static ir_node *
3621 insert_mem_phi(spill_ilp_t * si, ir_node * phi)
3622 {
3623         ir_node   *mem_phi;
3624         ir_node  **ins;
3625         defs_t    *defs;
3626         int        n;
3627
3628         NEW_ARR_A(ir_node*, ins, get_irn_arity(phi));
3629
3630         for(n=get_irn_arity(phi)-1; n>=0; --n) {
3631                 ins[n] = si->m_unknown;
3632         }
3633
3634         mem_phi =  new_r_PhiM_nokeep(si->chordal_env->irg, get_nodes_block(phi), get_irn_arity(phi), ins);
3635
3636         defs = set_insert_def(si->values, phi);
3637         assert(defs);
3638
3639         /* enter into the linked list */
3640         set_irn_link(mem_phi, defs->spills);
3641         defs->spills = mem_phi;
3642
3643 #ifdef SCHEDULE_PHIM
3644         sched_add_after(phi, mem_phi);
3645 #else
3646         pset_insert_ptr(si->phims, mem_phi);
3647 #endif
3648
3649         if(opt_keep_alive & KEEPALIVE_SPILLS)
3650                 pset_insert_ptr(si->spills, mem_phi);
3651
3652
3653         return mem_phi;
3654 }
3655
3656 /**
3657  * Add remat to list of defs, destroys link field!
3658  */
3659 static void
3660 insert_remat(spill_ilp_t * si, ir_node * remat)
3661 {
3662         defs_t   *defs;
3663         op_t     *remat_op = get_irn_link(remat);
3664
3665         assert(remat_op->is_remat);
3666
3667         defs = set_insert_def(si->values, remat_op->attr.remat.remat->value);
3668         assert(defs);
3669
3670         /* enter into the linked list */
3671         set_irn_link(remat, defs->remats);
3672         defs->remats = remat;
3673 }
3674
3675
3676 /**
3677  * Add reload before operation and add to list of defs
3678  */
3679 static ir_node *
3680 insert_reload(spill_ilp_t * si, const ir_node * value, ir_node * after)
3681 {
3682         defs_t   *defs;
3683         ir_node  *reload,
3684                          *spill;
3685         const arch_env_t *arch_env = si->chordal_env->birg->main_env->arch_env;
3686
3687         DBG((si->dbg, LEVEL_3, "\t  inserting reload for value %+F before %+F\n", value, after));
3688
3689         defs = set_find_def(si->values, value);
3690
3691         spill = defs->spills;
3692         assert(spill && "no spill placed before reload");
3693
3694         reload = be_reload(arch_env, si->cls, after, get_irn_mode(value), spill);
3695
3696         /* enter into the linked list */
3697         set_irn_link(reload, defs->remats);
3698         defs->remats = reload;
3699
3700         return reload;
3701 }
3702
3703 void perform_memory_operand(spill_ilp_t * si, memoperand_t * memoperand)
3704 {
3705         defs_t           *defs;
3706         ir_node          *value = get_irn_n(memoperand->irn, memoperand->pos);
3707         ir_node          *spill;
3708         const arch_env_t *arch_env = si->chordal_env->birg->main_env->arch_env;
3709
3710         DBG((si->dbg, LEVEL_2, "\t  inserting memory operand for value %+F at %+F\n", value, memoperand->irn));
3711
3712         defs = set_find_def(si->values, value);
3713
3714         spill = defs->spills;
3715         assert(spill && "no spill placed before reload");
3716
3717         arch_perform_memory_operand(arch_env, memoperand->irn, spill, memoperand->pos);
3718 }
3719
3720 void insert_memoperands(spill_ilp_t * si)
3721 {
3722         memoperand_t   *memoperand;
3723         lpp_name_t     *name;
3724
3725         set_foreach(si->memoperands, memoperand) {
3726                 name = si->lpp->vars[memoperand->ilp];
3727                 if(!is_zero(name->value)) {
3728                         perform_memory_operand(si, memoperand);
3729                 }
3730         }
3731 }
3732
3733 static void
3734 walker_spill_placer(ir_node * bb, void * data) {
3735         spill_ilp_t   *si = (spill_ilp_t*)data;
3736         ir_node       *irn;
3737         spill_bb_t    *spill_bb = get_irn_link(bb);
3738         pset          *spills_to_do = pset_new_ptr_default();
3739         spill_t       *spill;
3740
3741         set_foreach(spill_bb->ilp, spill) {
3742                 lpp_name_t    *name;
3743
3744                 if(is_Phi(spill->irn) && get_nodes_block(spill->irn) == bb) {
3745                         name = si->lpp->vars[spill->mem_in];
3746                         if(!is_zero(name->value)) {
3747                                 ir_node   *mem_phi;
3748
3749                                 mem_phi = insert_mem_phi(si, spill->irn);
3750
3751                                 DBG((si->dbg, LEVEL_2, "\t >>spilled Phi %+F -> %+F\n", spill->irn, mem_phi));
3752                         }
3753                 }
3754
3755                 name = si->lpp->vars[spill->spill];
3756                 if(!is_zero(name->value)) {
3757                         /* place spill directly after definition */
3758                         if(get_nodes_block(spill->irn) == bb) {
3759                                 insert_spill(si, spill->irn, spill->irn, spill->irn);
3760                                 continue;
3761                         }
3762
3763                         /* place spill at bb start */
3764                         if(spill->reg_in > 0) {
3765                                 name = si->lpp->vars[spill->reg_in];
3766                                 if(!is_zero(name->value)) {
3767                                         insert_spill(si, spill->irn, spill->irn, bb);
3768                                         continue;
3769                                 }
3770                         }
3771                         /* place spill after a remat */
3772                         pset_insert_ptr(spills_to_do, spill->irn);
3773                 }
3774         }
3775         DBG((si->dbg, LEVEL_3, "\t  %d spills to do in block %+F\n", pset_count(spills_to_do), bb));
3776
3777
3778         for(irn = sched_block_first_nonphi(bb); !sched_is_end(irn); irn = sched_next(irn)) {
3779                 op_t     *op = get_irn_link(irn);
3780
3781                 if(be_is_Spill(irn)) continue;
3782
3783                 if(op->is_remat) {
3784                         /* TODO fix this if we want to support remats with more than two nodes */
3785                         if(get_irn_mode(irn) != mode_T && pset_find_ptr(spills_to_do, op->attr.remat.remat->value)) {
3786                                 pset_remove_ptr(spills_to_do, op->attr.remat.remat->value);
3787
3788                                 insert_spill(si, irn, op->attr.remat.remat->value, irn);
3789                         }
3790                 } else {
3791                         if(pset_find_ptr(spills_to_do, irn)) {
3792                                 pset_remove_ptr(spills_to_do, irn);
3793
3794                                 insert_spill(si, irn, irn, irn);
3795                         }
3796                 }
3797
3798         }
3799
3800         assert(pset_count(spills_to_do) == 0);
3801
3802         /* afterwards free data in block */
3803         del_pset(spills_to_do);
3804 }
3805
3806 static ir_node *
3807 insert_mem_copy(spill_ilp_t * si, ir_node * bb, ir_node * value)
3808 {
3809         ir_node          *insert_pos = bb;
3810         ir_node          *spill;
3811         const arch_env_t *arch_env = si->chordal_env->birg->main_env->arch_env;
3812
3813         /* find last definition of arg value in block */
3814         ir_node  *next;
3815         defs_t   *defs;
3816         int       last = 0;
3817
3818         defs = set_find_def(si->values, value);
3819
3820         if(defs && defs->remats) {
3821                 for(next = defs->remats; next; next = get_irn_link(next)) {
3822                         if(get_nodes_block(next) == bb && sched_get_time_step(next) > last) {
3823                                 last = sched_get_time_step(next);
3824                                 insert_pos = next;
3825                         }
3826                 }
3827         }
3828
3829         if(get_nodes_block(value) == bb && sched_get_time_step(value) > last) {
3830                 last = sched_get_time_step(value);
3831                 insert_pos = value;
3832         }
3833
3834         DBG((si->dbg, LEVEL_2, "\t  inserting mem copy for value %+F after %+F\n", value, insert_pos));
3835
3836         spill = be_spill2(arch_env, is_Block(insert_pos)?value:insert_pos, insert_pos);
3837
3838         return spill;
3839 }
3840
3841 static void
3842 phim_fixer(spill_ilp_t *si) {
3843         defs_t  *defs;
3844
3845         set_foreach(si->values, defs) {
3846                 const ir_node  *phi = defs->value;
3847                 op_t           *op = get_irn_link(phi);
3848                 ir_node        *phi_m = NULL;
3849                 ir_node        *next = defs->spills;
3850                 int             n;
3851
3852                 if(!is_Phi(phi)) continue;
3853
3854                 while(next) {
3855                         if(is_Phi(next) && get_irn_mode(next) == mode_M) {
3856                                 phi_m = next;
3857                                 break;
3858                         } else {
3859                                 next = get_irn_link(next);
3860                         }
3861                 }
3862                 if(!phi_m) continue;
3863
3864                 for(n=get_irn_arity(phi)-1; n>=0; --n) {
3865                         ir_node        *value = get_irn_n(phi, n);
3866                         defs_t         *val_defs = set_find_def(si->values, value);
3867
3868                         /* a spill of this value */
3869                         ir_node      *spill;
3870
3871
3872                         if(opt_memcopies) {
3873                                 ir_node    *pred = get_Block_cfgpred_block(get_nodes_block(phi), n);
3874                                 lpp_name_t *name = si->lpp->vars[op->attr.live_range.args.copies[n]];
3875
3876                                 if(!is_zero(name->value)) {
3877                                         spill = insert_mem_copy(si, pred, value);
3878                                 } else {
3879                                         spill = val_defs->spills;
3880                                 }
3881                         } else {
3882                                 spill = val_defs->spills;
3883                         }
3884
3885                         assert(spill && "no spill placed before PhiM");
3886                         set_irn_n(phi_m, n, spill);
3887                 }
3888         }
3889 }
3890
3891 static void
3892 walker_reload_placer(ir_node * bb, void * data) {
3893         spill_ilp_t   *si = (spill_ilp_t*)data;
3894         ir_node       *irn;
3895         spill_bb_t    *spill_bb = get_irn_link(bb);
3896
3897         /* reloads at end of block */
3898         if(spill_bb->reloads) {
3899                 keyval_t    *keyval;
3900
3901                 set_foreach(spill_bb->reloads, keyval) {
3902                         ir_node        *irn = (ir_node*)keyval->key;
3903                         ilp_var_t       reload = PTR_TO_INT(keyval->val);
3904                         lpp_name_t     *name;
3905
3906                         name = si->lpp->vars[reload];
3907                         if(!is_zero(name->value)) {
3908                                 ir_node    *reload;
3909                                 ir_node    *insert_pos = bb;
3910                                 ir_node    *prev = sched_block_last_noncf(si, bb);
3911                                 op_t       *prev_op = get_irn_link(prev);
3912
3913                                 while(be_is_Spill(prev)) {
3914                                         prev = sched_prev(prev);
3915                                 }
3916
3917                                 prev_op = get_irn_link(prev);
3918
3919                                 /* insert reload before pre-remats */
3920                                 while(!sched_is_end(prev) && !be_is_Reload(prev) && !is_Phi(prev)
3921                                                 && prev_op->is_remat && prev_op->attr.remat.pre) {
3922                                         insert_pos = prev;
3923
3924                                         do {
3925                                                 prev = sched_prev(prev);
3926                                         } while(be_is_Spill(prev));
3927
3928                                         prev_op = get_irn_link(prev);
3929
3930                                 }
3931
3932                                 reload = insert_reload(si, irn, insert_pos);
3933
3934                                 if(opt_keep_alive & KEEPALIVE_RELOADS)
3935                                         pset_insert_ptr(si->spills, reload);
3936                         }
3937                 }
3938         }
3939
3940         /* walk and insert more reloads and collect remats */
3941         sched_foreach_reverse(bb, irn) {
3942                 op_t     *op = get_irn_link(irn);
3943
3944                 if(be_is_Reload(irn) || be_is_Spill(irn)) continue;
3945                 if(is_Phi(irn)) break;
3946
3947                 if(op->is_remat) {
3948                         if(get_irn_mode(irn) != mode_T) {
3949                                 insert_remat(si, irn);
3950                         }
3951                 } else {
3952                         int    n;
3953
3954                         for (n=get_irn_arity(irn)-1; n>=0; --n) {
3955                                 ir_node    *arg = get_irn_n(irn, n);
3956
3957                                 if(op->attr.live_range.args.reloads && op->attr.live_range.args.reloads[n] != ILP_UNDEF) {
3958                                         lpp_name_t    *name;
3959
3960                                         name = si->lpp->vars[op->attr.live_range.args.reloads[n]];
3961                                         if(!is_zero(name->value)) {
3962                                                 ir_node    *reload;
3963                                                 ir_node    *insert_pos = irn;
3964                                                 ir_node    *prev = sched_prev(insert_pos);
3965                                                 op_t       *prev_op;
3966
3967                                                 while(be_is_Spill(prev)) {
3968                                                         prev = sched_prev(prev);
3969                                                 }
3970
3971                                                 prev_op = get_irn_link(prev);
3972
3973                                                 /* insert reload before pre-remats */
3974                                                 while(!sched_is_end(prev) && !be_is_Reload(prev) && !is_Phi(prev)
3975                                                                 && prev_op->is_remat && prev_op->attr.remat.pre) {
3976                                                         insert_pos = prev;
3977
3978                                                         do {
3979                                                                 prev = sched_prev(prev);
3980                                                         } while(be_is_Spill(prev));
3981
3982                                                         prev_op = get_irn_link(prev);
3983
3984                                                 }
3985
3986                                                 reload = insert_reload(si, arg, insert_pos);
3987
3988                                                 assert(reload && "no reload returned");
3989                                                 set_irn_n(irn, n, reload);
3990
3991                                                 if(opt_keep_alive & KEEPALIVE_RELOADS)
3992                                                         pset_insert_ptr(si->spills, reload);
3993                                         }
3994                                 }
3995                         }
3996                 }
3997         }
3998
3999         del_set(spill_bb->ilp);
4000         if(spill_bb->reloads) del_set(spill_bb->reloads);
4001 }
4002
4003 static void
4004 walker_collect_used(ir_node * irn, void * data)
4005 {
4006         bitset_t   *used = data;
4007
4008         bitset_set(used, get_irn_idx(irn));
4009 }
4010
4011 struct kill_helper {
4012         bitset_t  *used;
4013         spill_ilp_t  *si;
4014 };
4015
4016 static void
4017 walker_kill_unused(ir_node * bb, void * data)
4018 {
4019         struct kill_helper *kh = data;
4020         ir_node            *bad = get_irg_bad(get_irn_irg(bb));
4021         ir_node            *irn;
4022
4023
4024         for(irn=sched_first(bb); !sched_is_end(irn);) {
4025                 ir_node     *next = sched_next(irn);
4026                 int          n;
4027
4028                 if(!bitset_is_set(kh->used, get_irn_idx(irn))) {
4029                         if(be_is_Spill(irn) || be_is_Reload(irn)) {
4030                                 DBG((kh->si->dbg, LEVEL_1, "\t SUBOPTIMAL! %+F IS UNUSED (cost: %g)\n", irn, get_cost(kh->si, irn)*execution_frequency(kh->si, bb)));
4031 #if 0
4032                                 assert(lpp_get_sol_state(kh->si->lpp) != lpp_optimal && "optimal solution is suboptimal?");
4033 #endif
4034                         }
4035
4036                         sched_remove(irn);
4037
4038                         set_nodes_block(irn, bad);
4039                         for (n=get_irn_arity(irn)-1; n>=0; --n) {
4040                                 set_irn_n(irn, n, bad);
4041                         }
4042                 }
4043                 irn = next;
4044         }
4045 }
4046
4047 #ifndef SCHEDULE_PHIM
4048 static void
4049 kill_unused_phims(spill_ilp_t * si, struct kill_helper * kh)
4050 {
4051         ir_node  *phi;
4052         ir_node  *bad = get_irg_bad(si->chordal_env->irg);
4053         int       n;
4054
4055         pset_foreach(si->phims, phi) {
4056                 if(!bitset_is_set(kh->used, get_irn_idx(phi))) {
4057
4058                         set_nodes_block(phi, bad);
4059                         for (n=get_irn_arity(phi)-1; n>=0; --n) {
4060                                 set_irn_n(phi, n, bad);
4061                         }
4062                 }
4063         }
4064 }
4065 #endif
4066
4067 static void
4068 kill_all_unused_values_in_schedule(spill_ilp_t * si)
4069 {
4070         struct kill_helper  kh;
4071
4072         kh.used = bitset_malloc(get_irg_last_idx(si->chordal_env->irg));
4073         kh.si = si;
4074
4075         irg_walk_graph(si->chordal_env->irg, walker_collect_used, NULL, kh.used);
4076 #ifndef SCHEDULE_PHIM
4077         kill_unused_phims(si, &kh);
4078 #endif
4079         irg_block_walk_graph(si->chordal_env->irg, walker_kill_unused, NULL, &kh);
4080
4081         bitset_free(kh.used);
4082 }
4083
4084 void
4085 print_irn_pset(pset * p)
4086 {
4087         ir_node   *irn;
4088
4089         pset_foreach(p, irn) {
4090                 ir_printf("%+F\n", irn);
4091         }
4092 }
4093
4094 void
4095 dump_phi_class(spill_ilp_t * si, pset * phiclass, const char * file)
4096 {
4097     FILE           *f = fopen(file, "w");
4098     ir_node        *irn;
4099     interference_t *interference;
4100
4101     pset_break(phiclass);
4102     set_break(si->interferences);
4103
4104     ir_fprintf(f, "digraph phiclass {\n");
4105
4106     pset_foreach(phiclass, irn) {
4107         if(is_Phi(irn))
4108             ir_fprintf(f, "  %F%N [shape=box]\n",irn,irn);
4109     }
4110
4111     pset_foreach(phiclass, irn) {
4112         int n;
4113
4114         if(!is_Phi(irn)) continue;
4115
4116         for(n=get_irn_arity(irn)-1; n>=0; --n) {
4117             ir_node  *arg = get_irn_n(irn, n);
4118
4119             ir_fprintf(f, "  %F%N -> %F%N\n",irn,irn,arg,arg);
4120         }
4121     }
4122
4123     set_foreach(si->interferences, interference) {
4124         const ir_node  *a  = interference->a;
4125         const ir_node  *b  = interference->b;
4126         if(get_phi_class(a) == phiclass) {
4127             ir_fprintf(f, "  %F%N -> %F%N [color=red,dir=none,style=bold]\n",a,a,b,b);
4128         }
4129     }
4130
4131     ir_fprintf(f, "}");
4132     fclose(f);
4133 }
4134
4135 static void
4136 rewire_uses(spill_ilp_t * si)
4137 {
4138         dom_front_info_t     *dfi = be_compute_dominance_frontiers(si->chordal_env->irg);
4139         defs_t               *defs;
4140         pset                 *ignore = pset_new_ptr(1);
4141
4142         pset_insert_ptr(ignore, get_irg_end(si->chordal_env->irg));
4143
4144         /* then fix uses of spills */
4145         set_foreach(si->values, defs) {
4146                 pset           *reloads;
4147                 pset           *spills;
4148                 const ir_node  *next = defs->remats;
4149                 int remats = 0;
4150
4151                 reloads = pset_new_ptr_default();
4152
4153                 while(next) {
4154                         if(be_is_Reload(next)) {
4155                                 pset_insert_ptr(reloads, next);
4156                         } else {
4157                                 ++remats;
4158                         }
4159                         next = get_irn_link(next);
4160                 }
4161
4162                 spills = get_spills_for_value(si, defs->value);
4163                 DBG((si->dbg, LEVEL_2, "\t  %d remats, %d reloads, and %d spills for value %+F\n", remats, pset_count(reloads), pset_count(spills), defs->value));
4164                 if(pset_count(spills) > 1) {
4165                         //assert(pset_count(reloads) > 0);
4166                         //                              print_irn_pset(spills);
4167                         //                              print_irn_pset(reloads);
4168
4169                         be_ssa_constr_set_ignore(dfi, si->lv, spills, ignore);
4170                 }
4171
4172                 del_pset(reloads);
4173                 del_pset(spills);
4174         }
4175
4176         /* first fix uses of remats and reloads */
4177         set_foreach(si->values, defs) {
4178                 pset           *nodes;
4179                 const ir_node  *next = defs->remats;
4180                 int             orig_kept = 0;
4181
4182                 if(next) {
4183                         nodes = pset_new_ptr_default();
4184                         if(sched_is_scheduled(defs->value)) {
4185                                 pset_insert_ptr(nodes, defs->value);
4186                                 orig_kept = 1;
4187                         }
4188
4189                         while(next) {
4190                                 pset_insert_ptr(nodes, next);
4191                                 next = get_irn_link(next);
4192                         }
4193
4194                         DBG((si->dbg, LEVEL_4, "\t    %d new definitions for value %+F\n", pset_count(nodes)-orig_kept, defs->value));
4195                         be_ssa_constr_set(dfi, si->lv, nodes);
4196
4197                         del_pset(nodes);
4198                 }
4199         }
4200
4201 //      remove_unused_defs(si);
4202
4203         be_free_dominance_frontiers(dfi);
4204 }
4205
4206
4207 static void
4208 writeback_results(spill_ilp_t * si)
4209 {
4210         /* walk through the graph and collect all spills, reloads and remats for a value */
4211
4212         si->values = new_set(cmp_defs, 4096);
4213
4214         DBG((si->dbg, LEVEL_1, "Applying results\n"));
4215         delete_unnecessary_remats(si);
4216         si->m_unknown = new_r_Unknown(si->chordal_env->irg, mode_M);
4217         irg_block_walk_graph(si->chordal_env->irg, walker_spill_placer, NULL, si);
4218         irg_block_walk_graph(si->chordal_env->irg, walker_reload_placer, NULL, si);
4219         if(opt_memoperands)
4220                 insert_memoperands(si);
4221         phim_fixer(si);
4222
4223         /* clean the remat info! there are still back-edges leading there! */
4224         clean_remat_info(si);
4225
4226         rewire_uses(si);
4227
4228         connect_all_spills_with_keep(si);
4229
4230         del_set(si->values);
4231 }
4232
4233 static int
4234 get_n_regs(spill_ilp_t * si)
4235 {
4236         int       arch_n_regs = arch_register_class_n_regs(si->cls);
4237
4238         bitset_t *arch_regs = bitset_malloc(arch_n_regs);
4239         bitset_t *abi_regs = bitset_malloc(arch_n_regs);
4240
4241         arch_put_non_ignore_regs(si->chordal_env->birg->main_env->arch_env, si->cls, arch_regs);
4242     be_abi_put_ignore_regs(si->chordal_env->birg->abi, si->cls, abi_regs);
4243
4244         bitset_andnot(arch_regs, abi_regs);
4245         arch_n_regs = bitset_popcnt(arch_regs);
4246
4247         bitset_free(arch_regs);
4248         bitset_free(abi_regs);
4249
4250         DBG((si->dbg, LEVEL_1, "\tArchitecture has %d free registers in class %s\n", arch_n_regs, si->cls->name));
4251         return arch_n_regs;
4252 }
4253
4254 static void
4255 walker_reload_mover(ir_node * bb, void * data)
4256 {
4257         spill_ilp_t   *si = data;
4258         ir_node           *tmp;
4259
4260         sched_foreach(bb, tmp) {
4261                 if(be_is_Reload(tmp) && has_reg_class(si, tmp)) {
4262                         ir_node       *reload = tmp;
4263                         ir_node       *irn = tmp;
4264
4265                         /* move reload upwards */
4266
4267                         int pressure = (int)get_irn_link(reload);
4268                         if(pressure < si->n_regs) {
4269                                 irn = sched_prev(reload);
4270                                 DBG((si->dbg, LEVEL_5, "regpressure before %+F: %d\n", reload, pressure));
4271                                 sched_remove(reload);
4272                                 pressure = (int)get_irn_link(irn);
4273
4274                                 while(pressure < si->n_regs) {
4275                                         if( sched_is_end(irn) ||
4276                                            (be_is_Reload(irn) && has_reg_class(si, irn)) ||
4277                                            /* do not move reload before its spill */
4278                                            (irn == be_get_Reload_mem(reload)) ||
4279                                            /* do not move before phi */
4280                                            is_Phi(irn)) break;
4281
4282                                         set_irn_link(irn, INT_TO_PTR(pressure+1));
4283                                         DBG((si->dbg, LEVEL_5, "new regpressure before %+F: %d\n", irn, pressure+1));
4284                                         irn = sched_prev(irn);
4285
4286                                         pressure = (int)get_irn_link(irn);
4287                                 }
4288
4289                                 DBG((si->dbg, LEVEL_3, "putting reload %+F after %+F\n", reload, irn));
4290                                 sched_put_after(irn, reload);
4291                         }
4292                 }
4293         }
4294 }
4295
4296 static void
4297 move_reloads_upward(spill_ilp_t * si)
4298 {
4299         irg_block_walk_graph(si->chordal_env->irg, walker_reload_mover, NULL, si);
4300 }
4301
4302
4303 /**
4304  * Walk all irg blocks and check for interfering spills inside of phi classes
4305  */
4306 static void
4307 luke_meminterferencechecker(ir_node * bb, void * data)
4308 {
4309         spill_ilp_t    *si = (spill_ilp_t*)data;
4310         int             l1, l2;
4311
4312         be_lv_foreach(si->lv, bb, be_lv_state_end | be_lv_state_out | be_lv_state_in, l1) {
4313                 ir_node        *a = be_lv_get_irn(si->lv, bb, l1);
4314
4315                 if(!be_is_Spill(a) && (!is_Phi(a) || get_irn_mode(a) != mode_T)) continue;
4316
4317                 /* a is only interesting if it is in my register class and if it is inside a phi class */
4318                 if (has_reg_class(si, a) && get_phi_class(a)) {
4319                         for(l2=_be_lv_next_irn(si->lv, bb, 0xff, l1+1); l2>=0; l2=_be_lv_next_irn(si->lv, bb, 0xff, l2+1)) {
4320                                 ir_node        *b = be_lv_get_irn(si->lv, bb, l2);
4321
4322                                 if(!be_is_Spill(b) && (!is_Phi(b) || get_irn_mode(b) != mode_T)) continue;
4323
4324                                 /* a and b are only interesting if they are in the same phi class */
4325                                 if(has_reg_class(si, b) && get_phi_class(a) == get_phi_class(b)) {
4326                                         if(values_interfere_in_block(si, bb, a, b)) {
4327                                                 ir_fprintf(stderr, "$$ Spills interfere in %+F: %+F, %+F \t$$\n", bb, a, b);
4328                                         }
4329                                 }
4330                         }
4331                 }
4332         }
4333 }
4334
4335 static void
4336 verify_phiclasses(spill_ilp_t * si)
4337 {
4338         /* analyze phi classes */
4339         phi_class_compute(si->chordal_env->irg);
4340
4341         DBG((si->dbg, LEVEL_2, "\t calling memory interference checker\n"));
4342         irg_block_walk_graph(si->chordal_env->irg, luke_meminterferencechecker, NULL, si);
4343 }
4344
4345 void
4346 be_spill_remat(const be_chordal_env_t * chordal_env)
4347 {
4348         char            buf[256];
4349         char            problem_name[256];
4350         char            dump_suffix[256];
4351         char            dump_suffix2[256];
4352         struct obstack  obst;
4353         spill_ilp_t     si;
4354
4355         ir_snprintf(problem_name, sizeof(problem_name), "%F_%s", chordal_env->irg, chordal_env->cls->name);
4356         ir_snprintf(dump_suffix, sizeof(dump_suffix), "-%s-remats", chordal_env->cls->name);
4357         ir_snprintf(dump_suffix2, sizeof(dump_suffix2), "-%s-pressure", chordal_env->cls->name);
4358
4359         FIRM_DBG_REGISTER(si.dbg, "firm.be.ra.spillremat");
4360         DBG((si.dbg, LEVEL_1, "\n\n\t\t===== Processing %s =====\n\n", problem_name));
4361
4362         if(opt_verify & VERIFY_DOMINANCE)
4363                 be_check_dominance(chordal_env->irg);
4364
4365         obstack_init(&obst);
4366         si.chordal_env = chordal_env;
4367         si.obst = &obst;
4368         si.cls = chordal_env->cls;
4369         si.lpp = new_lpp(problem_name, lpp_minimize);
4370         si.remat_info = new_set(cmp_remat_info, 4096);
4371         si.interferences = new_set(cmp_interference, 32);
4372         si.memoperands = new_set(cmp_memoperands, 128);
4373         si.all_possible_remats = pset_new_ptr_default();
4374         si.spills = pset_new_ptr_default();
4375         si.inverse_ops = pset_new_ptr_default();
4376         si.lv = chordal_env->lv;
4377         si.keep = NULL;
4378         si.n_regs = get_n_regs(&si);
4379
4380         set_irg_link(chordal_env->irg, &si);
4381         compute_doms(chordal_env->irg);
4382
4383         /* compute phi classes */
4384 //      phi_class_compute(chordal_env->irg);
4385
4386         if(opt_dump_flags & DUMP_STATS)
4387                 be_analyze_regpressure(chordal_env, "-pre");
4388
4389         DBG((si.dbg, LEVEL_2, "\t initializing\n"));
4390         irg_block_walk_graph(chordal_env->irg, luke_initializer, NULL, &si);
4391
4392         if(opt_remats) {
4393                 /* collect remats */
4394                 DBG((si.dbg, LEVEL_1, "Collecting remats\n"));
4395                 irg_walk_graph(chordal_env->irg, walker_remat_collector, NULL, &si);
4396         }
4397
4398         /* insert possible remats */
4399         DBG((si.dbg, LEVEL_1, "Inserting possible remats\n"));
4400         irg_block_walk_graph(chordal_env->irg, walker_remat_insertor, NULL, &si);
4401         DBG((si.dbg, LEVEL_2, " -> inserted %d possible remats\n", pset_count(si.all_possible_remats)));
4402
4403         if(opt_keep_alive & KEEPALIVE_REMATS) {
4404                 DBG((si.dbg, LEVEL_1, "Connecting remats with keep and dumping\n"));
4405                 connect_all_remats_with_keep(&si);
4406                 /* dump graph with inserted remats */
4407                 dump_graph_with_remats(chordal_env->irg, dump_suffix);
4408         }
4409
4410         /* insert copies for phi arguments not in my regclass */
4411         irg_walk_graph(chordal_env->irg, walker_regclass_copy_insertor, NULL, &si);
4412
4413         /* recompute liveness */
4414         DBG((si.dbg, LEVEL_1, "Recomputing liveness\n"));
4415         be_liveness_recompute(si.lv);
4416
4417         /* build the ILP */
4418         DBG((si.dbg, LEVEL_1, "\tBuilding ILP\n"));
4419         DBG((si.dbg, LEVEL_2, "\t endwalker\n"));
4420         irg_block_walk_graph(chordal_env->irg, luke_endwalker, NULL, &si);
4421
4422         DBG((si.dbg, LEVEL_2, "\t blockwalker\n"));
4423         irg_block_walk_graph(chordal_env->irg, luke_blockwalker, NULL, &si);
4424
4425         if(opt_memcopies) {
4426                 DBG((si.dbg, LEVEL_2, "\t memcopyhandler\n"));
4427                 memcopyhandler(&si);
4428         }
4429
4430         if(opt_dump_flags & DUMP_PROBLEM) {
4431                 FILE           *f;
4432                 ir_snprintf(buf, sizeof(buf), "%s-spillremat.ilp", problem_name);
4433                 if ((f = fopen(buf, "wt")) != NULL) {
4434                         lpp_dump_plain(si.lpp, f);
4435                         fclose(f);
4436                 }
4437         }
4438
4439         if(opt_dump_flags & DUMP_MPS) {
4440                 FILE *f;
4441
4442                 ir_snprintf(buf, sizeof(buf), "%s-spillremat.mps", problem_name);
4443                 if((f = fopen(buf, "wt")) != NULL) {
4444                         mps_write_mps(si.lpp, s_mps_fixed, f);
4445                         fclose(f);
4446                 }
4447
4448                 ir_snprintf(buf, sizeof(buf), "%s-spillremat.mst", problem_name);
4449                 if((f = fopen(buf, "wt")) != NULL) {
4450                         mps_write_mst(si.lpp, s_mps_fixed, f);
4451                         fclose(f);
4452                 }
4453         }
4454
4455         lpp_check_startvals(si.lpp);
4456
4457 #ifdef SOLVE
4458         DBG((si.dbg, LEVEL_1, "\tSolving %s (%d variables, %d constraints)\n", problem_name, si.lpp->var_next, si.lpp->cst_next));
4459         lpp_set_time_limit(si.lpp, opt_timeout);
4460
4461         if(opt_log)
4462                 lpp_set_log(si.lpp, stdout);
4463
4464 #ifdef SOLVE_LOCAL
4465         lpp_solve_cplex(si.lpp);
4466 #else
4467         lpp_solve_net(si.lpp, LPP_SERVER, LPP_SOLVER);
4468 #endif
4469         assert(lpp_is_sol_valid(si.lpp)
4470                && "solution of ILP must be valid");
4471
4472         DBG((si.dbg, LEVEL_1, "\t%s: iterations: %d, solution time: %g, objective function: %g, best bound: %g\n", problem_name, si.lpp->iterations, si.lpp->sol_time, is_zero(si.lpp->objval)?0.0:si.lpp->objval, is_zero(si.lpp->best_bound)?0.0:si.lpp->best_bound));
4473
4474         if(opt_dump_flags & DUMP_SOLUTION) {
4475                 FILE           *f;
4476                 char            buf[256];
4477
4478                 ir_snprintf(buf, sizeof(buf), "%s-spillremat.sol", problem_name);
4479                 if ((f = fopen(buf, "wt")) != NULL) {
4480                         int             i;
4481                         for (i = 0; i < si.lpp->var_next; ++i) {
4482                                 lpp_name_t     *name = si.lpp->vars[i];
4483                                 fprintf(f, "%20s %4d %10f\n", name->name, name->nr, name->value);
4484                         }
4485                         fclose(f);
4486                 }
4487         }
4488
4489 #ifndef SCHEDULE_PHIM
4490         si.phims = pset_new_ptr_default();
4491 #endif
4492         writeback_results(&si);
4493
4494
4495 #endif                          /* SOLVE */
4496
4497         kill_all_unused_values_in_schedule(&si);
4498
4499 #if !defined(SCHEDULE_PHIM) && defined(SOLVE)
4500         del_pset(si.phims);
4501 #endif
4502
4503         if(opt_keep_alive & (KEEPALIVE_SPILLS | KEEPALIVE_RELOADS))
4504                 be_dump(chordal_env->irg, "-spills-placed", dump_ir_block_graph);
4505
4506         // move reloads upwards
4507         be_liveness_recompute(si.lv);
4508         irg_block_walk_graph(chordal_env->irg, walker_pressure_annotator, NULL, &si);
4509         move_reloads_upward(&si);
4510
4511         if(opt_memcopies) {
4512                 verify_phiclasses(&si);
4513         }
4514
4515         irg_block_walk_graph(chordal_env->irg, walker_pressure_annotator, NULL, &si);
4516
4517         if(opt_dump_flags & DUMP_PRESSURE)
4518                 dump_pressure_graph(&si, dump_suffix2);
4519
4520         if(opt_dump_flags & DUMP_STATS)
4521                 be_analyze_regpressure(chordal_env, "-post");
4522
4523         if(opt_verify & VERIFY_DOMINANCE)
4524                 be_check_dominance(chordal_env->irg);
4525
4526         free_dom(chordal_env->irg);
4527         del_set(si.interferences);
4528         del_pset(si.inverse_ops);
4529         del_pset(si.all_possible_remats);
4530         del_set(si.memoperands);
4531         del_pset(si.spills);
4532         free_lpp(si.lpp);
4533         obstack_free(&obst, NULL);
4534         DBG((si.dbg, LEVEL_1, "\tdone.\n"));
4535 }
4536
4537 #else                           /* WITH_ILP */
4538
4539 static void
4540 only_that_you_can_compile_without_WITH_ILP_defined(void)
4541 {
4542 }
4543
4544 #endif                          /* WITH_ILP */