pressure stats
[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.h"
33 #include "iredges.h"
34 #include "execfreq.h"
35
36 #include <lpp/lpp.h>
37 #include <lpp/lpp_net.h>
38 #include <lpp/lpp_cplex.h>
39 //#include <lc_pset.h>
40 #include <libcore/lc_bitset.h>
41
42 #include "be_t.h"
43 #include "belive_t.h"
44 #include "besched_t.h"
45 #include "beirgmod.h"
46 #include "bearch.h"
47 #include "benode_t.h"
48 #include "beutil.h"
49 #include "bespillremat.h"
50 #include "bespill.h"
51 #include "bepressurestat.h"
52
53 #include "bechordal_t.h"
54
55 #define BIGM 100000.0
56
57 #define DUMP_SOLUTION
58 #define DUMP_ILP
59 //#define KEEPALIVE /* keep alive all inserted remats and dump graph with remats */
60 #define COLLECT_REMATS /* enable rematerialization */
61 #define COLLECT_INVERSE_REMATS /* enable placement of inverse remats */
62 #define REMAT_WHILE_LIVE /* only remat values that are live */
63 //#define NO_ENLARGE_L1V3N355 /* do not remat after the death of some operand */
64 //#define EXECFREQ_LOOPDEPH /* compute execution frequency from loop depth only */
65 //#define MAY_DIE_AT_PRE_REMAT /* allow values to die after a pre remat */
66 #define CHECK_POST_REMAT /* check pressure after post remats (conservative but otherwise we can temporarily exceed the register pressure) */
67 #define NO_SINGLE_USE_REMATS /* do not repair schedule */
68 //#define KEEPALIVE_SPILLS
69 //#define KEEPALIVE_RELOADS
70 #define GOODWIN_REDUCTION
71
72 #define  SOLVE
73 //#define  SOLVE_LOCAL
74 #define LPP_SERVER "i44pc52"
75 #define LPP_SOLVER "cplex"
76
77 #define COST_LOAD      10
78 #define COST_STORE     50
79 #define COST_REMAT     1
80
81 #define ILP_TIMEOUT    120
82
83 #define ILP_UNDEF               -1
84
85 typedef struct _spill_ilp_t {
86         const arch_register_class_t  *cls;
87         int                           n_regs;
88         const be_chordal_env_t       *chordal_env;
89         spill_env_t                  *senv;
90         lpp_t                        *lpp;
91         struct obstack               *obst;
92         set                          *remat_info;
93         pset                         *all_possible_remats;
94         pset                         *inverse_ops;
95 #ifdef KEEPALIVE
96         ir_node                      *keep;
97 #endif
98         set                          *values; /**< for collecting all definitions of values before running ssa-construction */
99         set                          *execfreqs;
100         pset                         *spills;
101         ir_node                      *m_unknown;
102         DEBUG_ONLY(firm_dbg_module_t * dbg);
103 } spill_ilp_t;
104
105 typedef int ilp_var_t;
106 typedef int ilp_cst_t;
107
108 typedef struct _spill_bb_t {
109         set          *ilp;
110         pset         *copys_needed;
111         ilp_var_t    *reloads;
112 } spill_bb_t;
113
114 typedef struct _remat_t {
115         const ir_node        *op; /**< for copy_irn */
116         const ir_node        *proj; /**< not NULL if the above op produces a tuple */
117         const ir_node        *value; /**< the value which is being recomputed by this remat */
118         int                   cost; /**< cost of this remat */
119         int                   inverse; /**< nonzero if this is an inverse remat */
120 } remat_t;
121
122 /**
123  * Data to be attached to each IR node. For remats this contains the ilp_var
124  * for this remat and for normal ops this contains the ilp_vars for
125  * reloading each operand
126  */
127 typedef struct _op_t {
128         int             is_remat;
129         union {
130                 struct {
131                         ilp_var_t       ilp;
132                         remat_t        *remat; /** the remat this op belongs to */
133                         int             pre; /** 1, if this is a pressure-increasing remat */
134                 } remat;
135                 struct {
136                         ilp_var_t       ilp;
137                         ir_node        *op; /** the operation this live range belongs to */
138                         ilp_var_t      *reloads;
139                 } live_range;
140         } attr;
141 } op_t;
142
143 typedef struct _defs_t {
144         ir_node   *value;
145         ir_node   *spills;  /**< points to the first spill for this value (linked by link field) */
146         ir_node   *remats;  /**< points to the first definition for this value (linked by link field) */
147 } defs_t;
148
149 typedef struct _remat_info_t {
150         const ir_node       *irn; /**< the irn to which these remats belong */
151         pset                *remats; /**< possible remats for this value */
152         pset                *remats_by_operand; /**< remats with this value as operand */
153 } remat_info_t;
154
155 typedef struct _keyval_t {
156         const void          *key;
157         const void          *val;
158 } keyval_t;
159
160 typedef struct _spill_t {
161         ir_node      *irn;
162         ilp_var_t     reg_in;
163         ilp_var_t     mem_in;
164         ilp_var_t     reg_out;
165         ilp_var_t     mem_out;
166         ilp_var_t     spill;
167 } spill_t;
168
169 static INLINE int
170 has_reg_class(const spill_ilp_t * si, const ir_node * irn)
171 {
172         return chordal_has_class(si->chordal_env, irn);
173 }
174
175 #if 0
176 static int
177 cmp_remat(const void *a, const void *b)
178 {
179         const keyval_t *p = a;
180         const keyval_t *q = b;
181         const remat_t  *r = p->val;
182         const remat_t  *s = q->val;
183
184         assert(r && s);
185
186         return !(r == s || r->op == s->op);
187 }
188 #endif
189 static int
190 cmp_remat(const void *a, const void *b)
191 {
192         const remat_t  *r = a;
193         const remat_t  *s = a;
194
195         return !(r == s || r->op == s->op);
196 }
197
198 static int
199 cmp_spill(const void *a, const void *b, size_t size)
200 {
201         const spill_t *p = a;
202         const spill_t *q = b;
203
204 //      return !(p->irn == q->irn && p->bb == q->bb);
205         return !(p->irn == q->irn);
206 }
207
208 static keyval_t *
209 set_find_keyval(set * set, void * key)
210 {
211         keyval_t     query;
212
213         query.key = key;
214         return set_find(set, &query, sizeof(query), HASH_PTR(key));
215 }
216
217 static keyval_t *
218 set_insert_keyval(set * set, void * key, void * val)
219 {
220         keyval_t     query;
221
222         query.key = key;
223         query.val = val;
224         return set_insert(set, &query, sizeof(query), HASH_PTR(key));
225 }
226
227 static defs_t *
228 set_find_def(set * set, ir_node * value)
229 {
230         defs_t     query;
231
232         query.value = value;
233         return set_find(set, &query, sizeof(query), HASH_PTR(value));
234 }
235
236 static defs_t *
237 set_insert_def(set * set, ir_node * value)
238 {
239         defs_t     query;
240
241         query.value = value;
242         query.spills = NULL;
243         query.remats = NULL;
244         return set_insert(set, &query, sizeof(query), HASH_PTR(value));
245 }
246
247 static spill_t *
248 set_find_spill(set * set, ir_node * value)
249 {
250         spill_t     query;
251
252         query.irn = value;
253         return set_find(set, &query, sizeof(query), HASH_PTR(value));
254 }
255
256 #define pset_foreach(s,i) for((i)=pset_first((s)); (i); (i)=pset_next((s)))
257 #define set_foreach(s,i) for((i)=set_first((s)); (i); (i)=set_next((s)))
258 #define foreach_post_remat(s,i) for((i)=next_post_remat((s)); (i); (i)=next_post_remat((i)))
259 #define foreach_pre_remat(si,s,i) for((i)=next_pre_remat((si),(s)); (i); (i)=next_pre_remat((si),(i)))
260 #define sched_foreach_op(s,i) for((i)=sched_next_op((s));!sched_is_end((i));(i)=sched_next_op((i)))
261
262 static int
263 cmp_remat_info(const void *a, const void *b, size_t size)
264 {
265         const remat_info_t *p = a;
266         const remat_info_t *q = b;
267
268         return !(p->irn == q->irn);
269 }
270
271 static int
272 cmp_defs(const void *a, const void *b, size_t size)
273 {
274         const defs_t *p = a;
275         const defs_t *q = b;
276
277         return !(p->value == q->value);
278 }
279
280 static int
281 cmp_keyval(const void *a, const void *b, size_t size)
282 {
283         const keyval_t *p = a;
284         const keyval_t *q = b;
285
286         return !(p->key == q->key);
287 }
288
289 static double
290 execution_frequency(const spill_ilp_t * si, const ir_node * irn)
291 {
292 #define FUDGE 0.001
293         if(si->execfreqs) {
294                 if(is_Block(irn)) {
295                         return get_block_execfreq(si->execfreqs, irn) + FUDGE;
296                 } else {
297                         return get_block_execfreq(si->execfreqs, get_nodes_block(irn)) + FUDGE;
298                 }
299         } else {
300                 if(is_Block(irn))
301                         return exp(get_loop_depth(get_irn_loop(irn)) * log(10)) + FUDGE;
302                 else
303                         return exp(get_loop_depth(get_irn_loop(get_nodes_block(irn))) * log(10)) + FUDGE;
304         }
305 }
306
307 static double
308 get_cost(const spill_ilp_t * si, const ir_node * irn)
309 {
310         if(be_is_Spill(irn)) {
311                 return COST_STORE;
312         } else if(be_is_Reload(irn)){
313                 return COST_LOAD;
314         } else {
315                 return arch_get_op_estimated_cost(si->chordal_env->birg->main_env->arch_env, irn);
316         }
317
318 }
319
320 /**
321  * Checks, whether node and its operands have suitable reg classes
322  */
323 static INLINE int
324 is_rematerializable(const spill_ilp_t * si, const ir_node * irn)
325 {
326         int             i,
327                         n;
328         const arch_env_t *arch_env = si->chordal_env->birg->main_env->arch_env;
329         int               remat = (arch_irn_get_flags(arch_env, irn) & arch_irn_flags_rematerializable) != 0;
330
331 #if 0
332         if(!remat)
333                 ir_fprintf(stderr, "  Node %+F is not rematerializable\n", irn);
334 #endif
335
336         for (i = 0, n = get_irn_arity(irn); i < n && remat; ++i) {
337                 ir_node        *op = get_irn_n(irn, i);
338                 remat &= has_reg_class(si, op) || arch_irn_get_flags(arch_env, op) & arch_irn_flags_ignore || (get_irn_op(op) == op_NoMem);
339
340 //              if(!remat)
341 //                      ir_fprintf(stderr, "  Argument %d (%+F) of Node %+F has wrong regclass\n", i, op, irn);
342         }
343
344         return remat;
345 }
346
347 /**
348  * Try to create a remat from @p op with destination value @p dest_value
349  */
350 static INLINE remat_t *
351 get_remat_from_op(spill_ilp_t * si, const ir_node * dest_value, const ir_node * op)
352 {
353         remat_t  *remat = NULL;
354
355 //      if(!mode_is_datab(get_irn_mode(dest_value)))
356 //              return NULL;
357
358         if(dest_value == op) {
359                 const ir_node *proj = NULL;
360
361                 if(is_Proj(dest_value)) {
362                         op = get_irn_n(op, 0);
363                         proj = dest_value;
364                 }
365
366                 if(!is_rematerializable(si, op))
367                         return NULL;
368
369                 remat = obstack_alloc(si->obst, sizeof(*remat));
370                 remat->op = op;
371                 remat->cost = get_cost(si, op);
372                 remat->value = dest_value;
373                 remat->proj = proj;
374                 remat->inverse = 0;
375         } else {
376                 arch_inverse_t     inverse;
377                 int                i,
378                                                    n;
379
380                 /* get the index of the operand we want to retrieve by the inverse op */
381                 for (i = 0, n = get_irn_arity(op); i < n; ++i) {
382                         ir_node        *arg = get_irn_n(op, i);
383
384                         if(arg == dest_value) break;
385                 }
386                 if(i == n) return NULL;
387
388                 DBG((si->dbg, LEVEL_5, "\t  requesting inverse op for argument %d of op %+F\n", i, op));
389
390                 /* else ask the backend to give an inverse op */
391                 if(arch_get_inverse(si->chordal_env->birg->main_env->arch_env, op, i, &inverse, si->obst)) {
392                         int   i;
393
394                         DBG((si->dbg, LEVEL_4, "\t  backend gave us an inverse op with %d nodes and cost %d\n", inverse.n, inverse.costs));
395
396                         assert(inverse.n > 0 && "inverse op should have at least one node");
397
398                         for(i=0; i<inverse.n; ++i) {
399                                 pset_insert_ptr(si->inverse_ops, inverse.nodes[i]);
400                         }
401
402                         if(inverse.n <= 2) {
403                                 remat = obstack_alloc(si->obst, sizeof(*remat));
404                                 remat->op = inverse.nodes[0];
405                                 remat->cost = inverse.costs;
406                                 remat->value = dest_value;
407                                 remat->proj = (inverse.n==2)?inverse.nodes[1]:NULL;
408                                 remat->inverse = 1;
409
410                                 assert(is_Proj(remat->proj));
411                         } else {
412                                 assert(0 && "I can not handle remats with more than 2 nodes");
413                         }
414                 }
415         }
416
417         if(remat) {
418                 if(remat->proj) {
419                         DBG((si->dbg, LEVEL_3, "\t >Found remat %+F for %+F from %+F with %+F\n", remat->op, dest_value, op, remat->proj));
420                 } else {
421                         DBG((si->dbg, LEVEL_3, "\t >Found remat %+F for %+F from %+F\n", remat->op, dest_value, op));
422                 }
423         }
424         return remat;
425 }
426
427
428 static INLINE void
429 add_remat(const spill_ilp_t * si, const remat_t * remat)
430 {
431         remat_info_t    *remat_info,
432                      query;
433         int              i,
434                                          n;
435
436         assert(remat->op);
437         assert(remat->value);
438
439         query.irn = remat->value;
440         query.remats = NULL;
441         query.remats_by_operand = NULL;
442         remat_info = set_insert(si->remat_info, &query, sizeof(query), HASH_PTR(remat->value));
443
444         if(remat_info->remats == NULL) {
445                 remat_info->remats = new_pset(cmp_remat, 4096);
446         }
447         pset_insert(remat_info->remats, remat, HASH_PTR(remat->op));
448
449         /* insert the remat into the remats_be_operand set of each argument of the remat op */
450         for (i = 0, n = get_irn_arity(remat->op); i < n; ++i) {
451                 ir_node        *arg = get_irn_n(remat->op, i);
452
453                 query.irn = arg;
454                 query.remats = NULL;
455                 query.remats_by_operand = NULL;
456                 remat_info = set_insert(si->remat_info, &query, sizeof(query), HASH_PTR(arg));
457
458                 if(remat_info->remats_by_operand == NULL) {
459                         remat_info->remats_by_operand = new_pset(cmp_remat, 4096);
460                 }
461                 pset_insert(remat_info->remats_by_operand, remat, HASH_PTR(remat->op));
462         }
463 }
464
465 static int
466 get_irn_n_nonremat_edges(const spill_ilp_t * si, const ir_node * irn)
467 {
468         const ir_edge_t   *edge = get_irn_out_edge_first(irn);
469         int                i = 0;
470
471         while(edge) {
472                 if(!pset_find_ptr(si->inverse_ops, edge->src)) {
473                         ++i;
474                 }
475                 edge = get_irn_out_edge_next(irn, edge);
476         }
477
478         return i;
479 }
480
481 static INLINE void
482 get_remats_from_op(spill_ilp_t * si, const ir_node * op)
483 {
484         int       i,
485                       n;
486         remat_t *remat;
487
488 #ifdef NO_SINGLE_USE_REMATS
489         if(has_reg_class(si, op) && (get_irn_n_nonremat_edges(si, op) > 1)) {
490 #else
491         if(has_reg_class(si, op)) {
492 #endif
493                 remat = get_remat_from_op(si, op, op);
494                 if(remat) {
495                         add_remat(si, remat);
496                 }
497         }
498
499 #ifdef COLLECT_INVERSE_REMATS
500         /* repeat the whole stuff for each remat retrieved by get_remat_from_op(op, arg)
501            for each arg */
502         for (i = 0, n = get_irn_arity(op); i < n; ++i) {
503                 ir_node        *arg = get_irn_n(op, i);
504
505                 if(has_reg_class(si, arg)) {
506                         /* try to get an inverse remat */
507                         remat = get_remat_from_op(si, arg, op);
508                         if(remat) {
509                                 add_remat(si, remat);
510                         }
511                 }
512         }
513 #endif
514
515 }
516
517 static INLINE int
518 value_is_defined_before(const spill_ilp_t * si, const ir_node * pos, const ir_node * val)
519 {
520         ir_node *block;
521         ir_node *def_block = get_nodes_block(val);
522         int      ret;
523
524         if(val == pos)
525                 return 0;
526
527         /* if pos is at end of a basic block */
528         if(is_Block(pos)) {
529                 ret = (pos == def_block || block_dominates(def_block, pos));
530 //              ir_fprintf(stderr, "(def(bb)=%d) ", ret);
531                 return ret;
532         }
533
534         /* else if this is a normal operation */
535         block = get_nodes_block(pos);
536         if(block == def_block) {
537                 if(!sched_is_scheduled(val)) return 1;
538
539                 ret = sched_comes_after(val, pos);
540 //              ir_fprintf(stderr, "(def(same block)=%d) ",ret);
541                 return ret;
542         }
543
544         ret = block_dominates(def_block, block);
545 //      ir_fprintf(stderr, "(def(other block)=%d) ", ret);
546         return ret;
547 }
548
549 static INLINE ir_node *
550 sched_block_last_noncf(const spill_ilp_t * si, const ir_node * bb)
551 {
552     return sched_skip((ir_node*)bb, 0, sched_skip_cf_predicator, (void *) si->chordal_env->birg->main_env->arch_env);
553 }
554
555 /**
556  * Returns first non-Phi node of block @p bb
557  */
558 static INLINE ir_node *
559 sched_block_first_nonphi(const ir_node * bb)
560 {
561         return sched_skip((ir_node*)bb, 1, sched_skip_phi_predicator, NULL);
562 }
563
564 static int
565 sched_skip_proj_predicator(const ir_node * irn, void * data)
566 {
567         return (is_Proj(irn));
568 }
569
570 static INLINE ir_node *
571 sched_next_nonproj(const ir_node * irn, int forward)
572 {
573         return sched_skip((ir_node*)irn, forward, sched_skip_proj_predicator, NULL);
574 }
575
576 /**
577  * Returns next operation node (non-Proj) after @p irn
578  * or the basic block of this node
579  */
580 static INLINE ir_node *
581 sched_next_op(const ir_node * irn)
582 {
583         ir_node *next = sched_next(irn);
584
585         if(is_Block(next))
586                 return next;
587
588         return sched_next_nonproj(next, 1);
589 }
590
591 /**
592  * Returns previous operation node (non-Proj) before @p irn
593  * or the basic block of this node
594  */
595 static INLINE ir_node *
596 sched_prev_op(const ir_node * irn)
597 {
598         ir_node *prev = sched_prev(irn);
599
600         if(is_Block(prev))
601                 return prev;
602
603         return sched_next_nonproj(prev, 0);
604 }
605
606 static void
607 sched_put_after(ir_node * insert, ir_node * irn)
608 {
609         if(is_Block(insert)) {
610                 insert = sched_block_first_nonphi(insert);
611         } else {
612                 insert = sched_next_op(insert);
613         }
614         sched_add_before(insert, irn);
615 }
616
617 static void
618 sched_put_before(const spill_ilp_t * si, ir_node * insert, ir_node * irn)
619 {
620   if(is_Block(insert)) {
621           insert = sched_block_last_noncf(si, insert);
622   } else {
623           insert = sched_next_nonproj(insert, 0);
624           insert = sched_prev(insert);
625   }
626   sched_add_after(insert, irn);
627 }
628
629 /**
630  * Tells you whether a @p remat can be placed before the irn @p pos
631  */
632 static INLINE int
633 can_remat_before(const spill_ilp_t * si, const remat_t * remat, const ir_node * pos, const pset * live)
634 {
635         const ir_node   *op = remat->op;
636         const ir_node   *prev;
637         int        i,
638                            n,
639                            res = 1;
640
641         if(is_Block(pos)) {
642                 prev = sched_block_last_noncf(si, pos);
643                 prev = sched_next_nonproj(prev, 0);
644         } else {
645                 prev = sched_prev_op(pos);
646         }
647         /* do not remat if the rematted value is defined immediately before this op */
648         if(prev == remat->op) {
649                 return 0;
650         }
651
652 #if 0
653         /* this should be just fine, the following OP will be using this value, right? */
654
655         /* only remat AFTER the real definition of a value (?) */
656         if(!value_is_defined_before(si, pos, remat->value)) {
657 //              ir_fprintf(stderr, "error(not defined)");
658                 return 0;
659         }
660 #endif
661
662         for(i=0, n=get_irn_arity(op); i<n && res; ++i) {
663                 const ir_node   *arg = get_irn_n(op, i);
664
665 #ifdef NO_ENLARGE_L1V3N355
666                 if(has_reg_class(si, arg) && live) {
667                         res &= pset_find_ptr(live, arg)?1:0;
668                 } else {
669                         res &= value_is_defined_before(si, pos, arg);
670                 }
671 #else
672                 res &= value_is_defined_before(si, pos, arg);
673 #endif
674         }
675
676         return res;
677 }
678
679 /**
680  * Tells you whether a @p remat can be placed after the irn @p pos
681  */
682 static INLINE int
683 can_remat_after(const spill_ilp_t * si, const remat_t * remat, const ir_node * pos, const pset * live)
684 {
685         if(is_Block(pos)) {
686                 pos = sched_block_first_nonphi(pos);
687         } else {
688                 pos = sched_next_op(pos);
689         }
690
691         /* only remat AFTER the real definition of a value (?) */
692         if(!value_is_defined_before(si, pos, remat->value)) {
693                 return 0;
694         }
695
696         return can_remat_before(si, remat, pos, live);
697 }
698
699 /**
700  * Collect potetially rematerializable OPs
701  */
702 static void
703 walker_remat_collector(ir_node * irn, void * data)
704 {
705         spill_ilp_t    *si = data;
706
707         if(!is_Block(irn) && !is_Phi(irn)) {
708                 DBG((si->dbg, LEVEL_4, "\t  Processing %+F\n", irn));
709                 get_remats_from_op(si, irn);
710         }
711 }
712
713 /**
714  * Inserts a copy of @p irn before @p pos
715  */
716 static ir_node *
717 insert_copy_before(const spill_ilp_t * si, const ir_node * irn, ir_node * pos)
718 {
719         ir_node     *bb;
720         ir_node     *copy;
721
722         bb = is_Block(pos)?pos:get_nodes_block(pos);
723         copy = exact_copy(irn);
724         set_nodes_block(copy, bb);
725         sched_put_before(si, pos, copy);
726
727         return copy;
728 }
729
730 /**
731  * Inserts a copy of @p irn after @p pos
732  */
733 static ir_node *
734 insert_copy_after(const spill_ilp_t * si, const ir_node * irn, ir_node * pos)
735 {
736         ir_node     *bb;
737         ir_node     *copy;
738
739         bb = is_Block(pos)?pos:get_nodes_block(pos);
740         copy = exact_copy(irn);
741         set_nodes_block(copy, bb);
742         sched_put_after(pos, copy);
743
744         return copy;
745 }
746
747 static void
748 insert_remat_after(spill_ilp_t * si, const remat_t * remat, const ir_node * pos, const pset * live)
749 {
750         char     buf[256];
751
752         if(can_remat_after(si, remat, pos, live)) {
753                 ir_node         *copy,
754                                                 *proj_copy;
755                 op_t            *op;
756
757                 DBG((si->dbg, LEVEL_3, "\t  >inserting remat %+F\n", remat->op));
758
759                 copy = insert_copy_after(si, remat->op, pos);
760
761 //              ir_snprintf(buf, sizeof(buf), "remat2_%N_%N", remat->value, pos);
762                 ir_snprintf(buf, sizeof(buf), "remat2_%N_%N", copy, pos);
763                 op = obstack_alloc(si->obst, sizeof(*op));
764                 op->is_remat = 1;
765                 op->attr.remat.remat = remat;
766                 op->attr.remat.pre = 0;
767                 op->attr.remat.ilp = lpp_add_var(si->lpp, buf, lpp_binary, remat->cost*execution_frequency(si, pos));
768
769                 set_irn_link(copy, op);
770                 pset_insert_ptr(si->all_possible_remats, copy);
771                 if(remat->proj) {
772                         proj_copy = insert_copy_after(si, remat->proj, copy);
773                         set_irn_n(proj_copy, 0, copy);
774                         set_irn_link(proj_copy, op);
775                         pset_insert_ptr(si->all_possible_remats, proj_copy);
776                 } else {
777                         proj_copy = NULL;
778                 }
779         }
780 }
781
782 static void
783 insert_remat_before(spill_ilp_t * si, const remat_t * remat, const ir_node * pos, const pset * live)
784 {
785         char     buf[256];
786
787         if(can_remat_before(si, remat, pos, live)) {
788                 ir_node         *copy,
789                                                 *proj_copy;
790                 op_t            *op;
791
792                 DBG((si->dbg, LEVEL_3, "\t  >inserting remat %+F\n", remat->op));
793
794                 copy = insert_copy_before(si, remat->op, pos);
795
796 //              ir_snprintf(buf, sizeof(buf), "remat_%N_%N", remat->value, pos);
797                 ir_snprintf(buf, sizeof(buf), "remat_%N_%N", copy, pos);
798                 op = obstack_alloc(si->obst, sizeof(*op));
799                 op->is_remat = 1;
800                 op->attr.remat.remat = remat;
801                 op->attr.remat.pre = 1;
802                 op->attr.remat.ilp = lpp_add_var(si->lpp, buf, lpp_binary, remat->cost*execution_frequency(si, pos));
803
804                 set_irn_link(copy, op);
805                 pset_insert_ptr(si->all_possible_remats, copy);
806                 if(remat->proj) {
807                         proj_copy = insert_copy_after(si, remat->proj, copy);
808                         set_irn_n(proj_copy, 0, copy);
809                         set_irn_link(proj_copy, op);
810                         pset_insert_ptr(si->all_possible_remats, proj_copy);
811                 } else {
812                         proj_copy = NULL;
813                 }
814         }
815 }
816
817 static int
818 get_block_n_succs(const ir_node *block) {
819         const ir_edge_t *edge;
820
821         assert(edges_activated(current_ir_graph));
822
823         edge = get_block_succ_first(block);
824         if (! edge)
825                 return 0;
826
827         edge = get_block_succ_next(block, edge);
828         return edge ? 2 : 1;
829 }
830
831 static int
832 is_merge_edge(const ir_node * bb)
833 {
834 #ifdef GOODWIN_REDUCTION
835         return get_block_n_succs(bb) == 1;
836 #else
837         return 1;
838 #endif
839 }
840
841 static int
842 is_diverge_edge(const ir_node * bb)
843 {
844 #ifdef GOODWIN_REDUCTION
845         return get_Block_n_cfgpreds(bb) == 1;
846 #else
847         return 1;
848 #endif
849 }
850
851 /**
852  * Insert (so far unused) remats into the irg to
853  * recompute the potential liveness of all values
854  */
855 static void
856 walker_remat_insertor(ir_node * bb, void * data)
857 {
858         spill_ilp_t    *si = data;
859         spill_bb_t     *spill_bb;
860         ir_node        *irn;
861         int             i,
862                                         n;
863         irn_live_t     *li;
864         pset           *live = pset_new_ptr_default();
865
866         DBG((si->dbg, LEVEL_3, "\t Entering %+F\n\n", bb));
867
868         live_foreach(bb, li) {
869                 ir_node        *value = (ir_node *) li->irn;
870
871                 /* add remats at end of block */
872                 if (live_is_end(li) && has_reg_class(si, value)) {
873                         pset_insert_ptr(live, value);
874                 }
875         }
876
877         spill_bb = obstack_alloc(si->obst, sizeof(*spill_bb));
878         set_irn_link(bb, spill_bb);
879
880         irn = sched_last(bb);
881         while(!sched_is_end(irn)) {
882                 ir_node   *next;
883                 op_t      *op;
884                 pset      *args;
885                 ir_node   *arg;
886
887                 next = sched_prev(irn);
888
889                 DBG((si->dbg, LEVEL_5, "\t at %+F (next: %+F)\n", irn, next));
890
891                 if(is_Phi(irn) || is_Proj(irn)) {
892                         op_t      *op;
893
894                         if(has_reg_class(si, irn)) {
895                                 pset_remove_ptr(live, irn);
896                         }
897
898                         op = obstack_alloc(si->obst, sizeof(*op));
899                         op->is_remat = 0;
900                         op->attr.live_range.reloads = NULL;
901                         op->attr.live_range.ilp = ILP_UNDEF;
902                         set_irn_link(irn, op);
903
904                         irn = next;
905                         continue;
906                 }
907
908                 op = obstack_alloc(si->obst, sizeof(*op));
909                 op->is_remat = 0;
910                 op->attr.live_range.ilp = ILP_UNDEF;
911                 op->attr.live_range.reloads = obstack_alloc(si->obst, sizeof(*op->attr.live_range.reloads) * get_irn_arity(irn));
912                 memset(op->attr.live_range.reloads, 0xFF, sizeof(*op->attr.live_range.reloads) * get_irn_arity(irn));
913                 set_irn_link(irn, op);
914
915                 args = pset_new_ptr_default();
916
917                 /* collect arguments of op */
918                 for (i = 0, n = get_irn_arity(irn); i < n; ++i) {
919                         ir_node        *arg = get_irn_n(irn, i);
920
921                         pset_insert_ptr(args, arg);
922                 }
923
924                 /* set args of op live in epilog */
925                 pset_foreach(args, arg) {
926                         if(has_reg_class(si, arg)) {
927                                 pset_insert_ptr(live, arg);
928                         }
929                 }
930
931                 /* insert all possible remats after irn */
932                 pset_foreach(args, arg) {
933                         remat_info_t   *remat_info,
934                                                     query;
935                         remat_t        *remat;
936
937                         /* continue if the operand has the wrong reg class
938                          */
939                         if(!has_reg_class(si, arg))
940                                 continue;
941
942                         query.irn = arg;
943                         query.remats = NULL;
944                         query.remats_by_operand = NULL;
945                         remat_info = set_find(si->remat_info, &query, sizeof(query), HASH_PTR(arg));
946
947                         if(!remat_info) {
948                                 continue;
949                         }
950
951                         /* do not place post remats after jumps */
952                         if(sched_skip_cf_predicator(irn, si->chordal_env->birg->main_env->arch_env)) continue;
953
954                         if(remat_info->remats_by_operand) {
955                                 pset_foreach(remat_info->remats_by_operand, remat) {
956                                         /* do not insert remats producing the same value as one of the operands */
957                                         if(!pset_find_ptr(args, remat->value)) {
958                                                 DBG((si->dbg, LEVEL_4, "\t  considering remat %+F with arg %+F\n", remat->op, arg));
959 #ifdef REMAT_WHILE_LIVE
960                                                 if(pset_find_ptr(live, remat->value)) {
961                                                         insert_remat_after(si, remat, irn, live);
962                                                 }
963 #else
964                                                 insert_remat_after(si, remat, irn, live);
965 #endif
966                                         }
967                                 }
968                         }
969                 }
970
971                 /* delete defined value from live set */
972                 if(has_reg_class(si, irn)) {
973                         pset_remove_ptr(live, irn);
974                 }
975
976                 /* insert all possible remats before irn */
977                 pset_foreach(args, arg) {
978                         remat_info_t   *remat_info,
979                                                     query;
980                         remat_t        *remat;
981
982                         /* continue if the operand has the wrong reg class
983                          */
984                         if(!has_reg_class(si, arg))
985                                 continue;
986
987                         query.irn = arg;
988                         query.remats = NULL;
989                         query.remats_by_operand = NULL;
990                         remat_info = set_find(si->remat_info, &query, sizeof(query), HASH_PTR(arg));
991
992                         if(!remat_info) {
993                                 continue;
994                         }
995
996                         if(remat_info->remats) {
997                                 pset_foreach(remat_info->remats, remat) {
998                                         DBG((si->dbg, LEVEL_4, "\t  considering remat %+F for arg %+F\n", remat->op, arg));
999 #ifdef REMAT_WHILE_LIVE
1000                                         if(pset_find_ptr(live, remat->value)) {
1001                                                 insert_remat_before(si, remat, irn, live);
1002                                         }
1003 #else
1004                                         insert_remat_before(si, remat, irn, live);
1005 #endif
1006                                 }
1007                         }
1008                 }
1009
1010                 del_pset(args);
1011                 irn = next;
1012         }
1013
1014         live_foreach(bb, li) {
1015                 ir_node        *value = (ir_node *) li->irn;
1016
1017                 /* add remats at end if successor has multiple predecessors */
1018                 if(is_merge_edge(bb)) {
1019                         /* add remats at end of block */
1020                         if (live_is_end(li) && has_reg_class(si, value)) {
1021                                 remat_info_t   *remat_info,
1022                                                            query;
1023                                 remat_t        *remat;
1024
1025                                 query.irn = value;
1026                                 query.remats = NULL;
1027                                 query.remats_by_operand = NULL;
1028                                 remat_info = set_find(si->remat_info, &query, sizeof(query), HASH_PTR(value));
1029
1030                                 if(remat_info && remat_info->remats) {
1031                                         pset_foreach(remat_info->remats, remat) {
1032                                                 DBG((si->dbg, LEVEL_4, "\t  considering remat %+F at end of block %+F\n", remat->op, bb));
1033
1034                                                 insert_remat_before(si, remat, bb, NULL);
1035                                         }
1036                                 }
1037                         }
1038                 }
1039                 if(is_diverge_edge(bb)) {
1040                         /* add remat2s at beginning of block */
1041                         if ((live_is_in(li) || (is_Phi(value) && get_nodes_block(value)==bb)) && has_reg_class(si, value)) {
1042                                 remat_info_t   *remat_info,
1043                                                            query;
1044                                 remat_t        *remat;
1045
1046                                 query.irn = value;
1047                                 query.remats = NULL;
1048                                 query.remats_by_operand = NULL;
1049                                 remat_info = set_find(si->remat_info, &query, sizeof(query), HASH_PTR(value));
1050
1051                                 if(remat_info && remat_info->remats) {
1052                                         pset_foreach(remat_info->remats, remat) {
1053                                                 DBG((si->dbg, LEVEL_4, "\t  considering remat %+F at beginning of block %+F\n", remat->op, bb));
1054
1055                                                 /* put the remat here if all its args are available */
1056                                                 insert_remat_after(si, remat, bb, NULL);
1057
1058                                         }
1059                                 }
1060                         }
1061                 }
1062         }
1063 }
1064
1065 /**
1066  * Preparation of blocks' ends for Luke Blockwalker(tm)(R)
1067  */
1068 static void
1069 luke_endwalker(ir_node * bb, void * data)
1070 {
1071         spill_ilp_t    *si = (spill_ilp_t*)data;
1072         irn_live_t     *li;
1073         pset           *live;
1074         pset           *use_end;
1075         char            buf[256];
1076         ilp_cst_t       cst;
1077         ir_node        *irn;
1078         spill_bb_t     *spill_bb = get_irn_link(bb);
1079
1080
1081         live = pset_new_ptr_default();
1082         use_end = pset_new_ptr_default();
1083
1084         live_foreach(bb, li) {
1085                 irn = (ir_node *) li->irn;
1086                 if (live_is_end(li) && has_reg_class(si, irn) && !pset_find_ptr(si->all_possible_remats, irn)) {
1087                         op_t      *op;
1088
1089                         pset_insert_ptr(live, irn);
1090                         op = get_irn_link(irn);
1091                         assert(!op->is_remat);
1092                 }
1093         }
1094
1095         /* collect values used by cond jumps etc. at bb end (use_end) -> always live */
1096         /* their reg_out is unimportant because it can always be set */
1097         sched_foreach_reverse(bb, irn) {
1098                 int   i,
1099                           n;
1100
1101                 if(!sched_skip_cf_predicator(irn, si->chordal_env->birg->main_env->arch_env)) break;
1102
1103                 for (i = 0, n = get_irn_arity(irn); i < n; ++i) {
1104                         ir_node        *irn_arg = get_irn_n(irn, i);
1105                         if(has_reg_class(si, irn_arg)) {
1106                                 pset_insert_ptr(use_end, irn);
1107                         }
1108                 }
1109         }
1110
1111         ir_snprintf(buf, sizeof(buf), "check_end_%N", bb);
1112         cst = lpp_add_cst(si->lpp, buf, lpp_less, si->n_regs - pset_count(use_end));
1113
1114         spill_bb->ilp = new_set(cmp_spill, 16);
1115
1116         live_foreach(bb, li) {
1117                 irn = (ir_node *) li->irn;
1118                 if (live_is_end(li) && has_reg_class(si, irn) && !pset_find_ptr(si->all_possible_remats, irn)) {
1119                         spill_t     query,
1120                                    *spill;
1121
1122                         query.irn = irn;
1123                         spill = set_insert(spill_bb->ilp, &query, sizeof(query), HASH_PTR(irn));
1124
1125                         ir_snprintf(buf, sizeof(buf), "reg_out_%N_%N", irn, bb);
1126                         spill->reg_out = lpp_add_var(si->lpp, buf, lpp_binary, 0.0);
1127                         /* if irn is used at the end of the block, then it is live anyway */
1128                         if(!pset_find_ptr(use_end, irn))
1129                                 lpp_set_factor_fast(si->lpp, cst, spill->reg_out, 1.0);
1130
1131                         ir_snprintf(buf, sizeof(buf), "mem_out_%N_%N", irn, bb);
1132                         spill->mem_out = lpp_add_var(si->lpp, buf, lpp_binary, 0.0);
1133
1134                         ir_snprintf(buf, sizeof(buf), "spill_%N_%N", irn, bb);
1135                         spill->spill = lpp_add_var(si->lpp, buf, lpp_binary, COST_STORE*execution_frequency(si, bb));
1136
1137                         spill->reg_in = ILP_UNDEF;
1138                         spill->mem_in = ILP_UNDEF;
1139                 }
1140         }
1141
1142         del_pset(live);
1143         del_pset(use_end);
1144 }
1145
1146 static ir_node *
1147 next_post_remat(const ir_node * irn)
1148 {
1149         op_t      *op;
1150
1151         if(is_Block(irn)) {
1152                 irn = sched_block_first_nonphi(irn);
1153         } else {
1154                 irn = sched_next_op(irn);
1155         }
1156
1157         if(sched_is_end(irn))
1158                 return NULL;
1159
1160         op = (op_t*)get_irn_link(irn);
1161         if(op->is_remat && !op->attr.remat.pre) {
1162                 return irn;
1163         }
1164
1165         return NULL;
1166 }
1167
1168
1169 static ir_node *
1170 next_pre_remat(const spill_ilp_t * si, const ir_node * irn)
1171 {
1172         op_t      *op;
1173         ir_node   *ret;
1174
1175         if(is_Block(irn)) {
1176                 ret = sched_block_last_noncf(si, irn);
1177                 ret = sched_next(ret);
1178                 ret = sched_prev_op(ret);
1179         } else {
1180                 ret = sched_prev_op(irn);
1181         }
1182
1183         if(sched_is_end(ret) || is_Phi(ret))
1184                 return NULL;
1185
1186         op = (op_t*)get_irn_link(ret);
1187         if(op->is_remat && op->attr.remat.pre) {
1188                 return ret;
1189         }
1190
1191         return NULL;
1192 }
1193
1194 /**
1195  * Find a remat of value @p value in the epilog of @p pos
1196  */
1197 static ir_node *
1198 find_post_remat(const ir_node * value, const ir_node * pos)
1199 {
1200         while((pos = next_post_remat(pos)) != NULL) {
1201                 op_t   *op;
1202
1203                 op = get_irn_link(pos);
1204                 assert(op->is_remat && !op->attr.remat.pre);
1205
1206                 if(op->attr.remat.remat->value == value)
1207                         return (ir_node*)pos;
1208
1209 #if 0
1210         const ir_edge_t *edge;
1211                 foreach_out_edge(pos, edge) {
1212                         ir_node   *proj = get_edge_src_irn(edge);
1213                         assert(is_Proj(proj));
1214                 }
1215 #endif
1216
1217         }
1218
1219         return NULL;
1220 }
1221
1222 /**
1223  * Find a remat of value @p value in the prolog of @p pos
1224  */
1225 static ir_node *
1226 find_pre_remat(const spill_ilp_t * si, const ir_node * value, const ir_node * pos)
1227 {
1228         while((pos = next_pre_remat(si,pos)) != NULL) {
1229                 op_t   *op;
1230
1231                 op = get_irn_link(pos);
1232                 assert(op->is_remat && op->attr.remat.pre);
1233
1234                 if(op->attr.remat.remat->value == value)
1235                         return (ir_node*)pos;
1236         }
1237
1238         return NULL;
1239 }
1240
1241 static spill_t *
1242 add_to_spill_bb(spill_ilp_t * si, ir_node * bb, ir_node * irn)
1243 {
1244         spill_bb_t  *spill_bb = get_irn_link(bb);
1245         spill_t     *spill,
1246                                  query;
1247         char         buf[256];
1248
1249         query.irn = irn;
1250         spill = set_find(spill_bb->ilp, &query, sizeof(query), HASH_PTR(irn));
1251         if(!spill) {
1252                 spill = set_insert(spill_bb->ilp, &query, sizeof(query), HASH_PTR(irn));
1253
1254                 spill->reg_out = ILP_UNDEF;
1255                 spill->reg_in  = ILP_UNDEF;
1256                 spill->mem_in  = ILP_UNDEF;
1257
1258                 ir_snprintf(buf, sizeof(buf), "mem_out_%N_%N", irn, bb);
1259                 spill->mem_out = lpp_add_var(si->lpp, buf, lpp_binary, 0.0);
1260
1261                 ir_snprintf(buf, sizeof(buf), "spill_%N_%N", irn, bb);
1262                 spill->spill = lpp_add_var(si->lpp, buf, lpp_binary, COST_STORE*execution_frequency(si, bb));
1263         }
1264
1265         return spill;
1266 }
1267
1268 /**
1269  * Walk all irg blocks and emit this ILP
1270  */
1271 static void
1272 luke_blockwalker(ir_node * bb, void * data)
1273 {
1274         spill_ilp_t    *si = (spill_ilp_t*)data;
1275         ir_node        *irn;
1276         irn_live_t     *li;
1277         pset           *live;
1278         char            buf[256];
1279         ilp_cst_t       cst;
1280         spill_bb_t     *spill_bb = get_irn_link(bb);
1281         int             i;
1282         ir_node        *tmp;
1283         spill_t        *spill;
1284
1285
1286         live = pset_new_ptr_default();
1287
1288         /* do something at the end of the block */
1289
1290         /* init live values at end of block */
1291         live_foreach(bb, li) {
1292                 ir_node        *irn = (ir_node *) li->irn;
1293
1294                 if (live_is_end(li) && has_reg_class(si, irn) && !pset_find_ptr(si->all_possible_remats, irn)) {
1295                         pset_insert_ptr(live, irn);
1296                 }
1297         }
1298
1299         if(is_merge_edge(bb)) {
1300                 spill_bb->reloads = obstack_alloc(si->obst, pset_count(live) * sizeof(*spill_bb->reloads));
1301                 memset(spill_bb->reloads, 0xFF, pset_count(live) * sizeof(*spill_bb->reloads));
1302         } else {
1303                 spill_bb->reloads = NULL;
1304         }
1305
1306         i=0;
1307         live_foreach(bb, li) {
1308                 ir_node        *irn = (ir_node *) li->irn;
1309                 op_t           *op;
1310
1311                 if (live_is_end(li) && has_reg_class(si, irn) && !pset_find_ptr(si->all_possible_remats, irn)) {
1312                         spill = set_find_spill(spill_bb->ilp, irn);
1313                         assert(spill);
1314
1315                         if(spill_bb->reloads) {
1316                                 ir_snprintf(buf, sizeof(buf), "reload_%N_%N", bb, irn);
1317                                 spill_bb->reloads[i] = lpp_add_var(si->lpp, buf, lpp_binary, COST_LOAD*execution_frequency(si, bb));
1318
1319                                 /* reload <= mem_out */
1320                                 cst = lpp_add_cst(si->lpp, buf, lpp_less, 0.0);
1321                                 lpp_set_factor_fast(si->lpp, cst, spill_bb->reloads[i], 1.0);
1322                                 lpp_set_factor_fast(si->lpp, cst, spill->mem_out, -1.0);
1323                         }
1324
1325                         op = get_irn_link(irn);
1326                         assert(!op->is_remat);
1327
1328                         ir_snprintf(buf, sizeof(buf), "lr_%N_%N", irn, bb);
1329                         op->attr.live_range.ilp = lpp_add_var(si->lpp, buf, lpp_binary, 0.0);
1330                         op->attr.live_range.op = bb;
1331
1332                         ir_snprintf(buf, sizeof(buf), "reg_out_%N_%N", bb, irn);
1333                         cst = lpp_add_cst(si->lpp, buf, lpp_less, 0.0);
1334
1335                         /* reg_out - reload - remat - live_range <= 0 */
1336                         lpp_set_factor_fast(si->lpp, cst, spill->reg_out, 1.0);
1337                         if(spill_bb->reloads) lpp_set_factor_fast(si->lpp, cst, spill_bb->reloads[i], -1.0);
1338                         lpp_set_factor_fast(si->lpp, cst, op->attr.live_range.ilp, -1.0);
1339                         foreach_pre_remat(si, bb, tmp) {
1340                                 op_t     *remat_op = get_irn_link(tmp);
1341                                 if(remat_op->attr.remat.remat->value == irn) {
1342                                         lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, -1.0);
1343                                 }
1344                         }
1345
1346                         ++i;
1347                 }
1348         }
1349         DBG((si->dbg, LEVEL_4, "\t   %d values live at end of block %+F\n", pset_count(live), bb));
1350
1351         sched_foreach_reverse(bb, irn) {
1352                 op_t       *op;
1353                 op_t       *tmp_op;
1354                 int         n,
1355                                         k,
1356                                         d = 0;
1357                 ilp_cst_t       check_pre,
1358                                         check_post;
1359 #ifdef CHECK_POST_REMAT
1360                 ilp_cst_t       check_post_remat;
1361 #endif
1362                 set        *args = new_set(cmp_keyval, get_irn_arity(irn));
1363                 keyval_t   *keyval;
1364
1365                 if(is_Phi(irn))
1366                         break;
1367
1368                 op = get_irn_link(irn);
1369                 /* skip remats */
1370                 if(op->is_remat) continue;
1371                 DBG((si->dbg, LEVEL_4, "\t  at node %+F\n", irn));
1372
1373                 if(has_reg_class(si, irn)) {
1374                         assert(pset_find_ptr(live, irn));
1375                         pset_remove_ptr(live, irn);
1376                 }
1377
1378                 /* init set of irn's arguments */
1379                 for (i = 0, n = get_irn_arity(irn); i < n; ++i) {
1380                         ir_node        *irn_arg = get_irn_n(irn, i);
1381                         if(has_reg_class(si, irn_arg)) {
1382                                 set_insert_keyval(args, irn_arg, (void*)i);
1383                         }
1384                 }
1385
1386 #ifdef CHECK_POST_REMAT
1387                 /* check the register pressure after the epilog */
1388                 ir_snprintf(buf, sizeof(buf), "check_post_remat_%N", irn);
1389                 check_post_remat = lpp_add_cst(si->lpp, buf, lpp_less, si->n_regs);
1390
1391                 /* iterate over L\U */
1392                 pset_foreach(live, tmp) {
1393                         if(!set_find_keyval(args, tmp)) {
1394                                 /* if a live value is not used by irn */
1395                                 tmp_op = get_irn_link(tmp);
1396 //                              assert(tmp_op->attr.live_range.op != irn);
1397                                 lpp_set_factor_fast(si->lpp, check_post_remat, tmp_op->attr.live_range.ilp, 1.0);
1398                         }
1399                 }
1400                 /* iterate over following remats and remove possibly defined values again from check_post_remat */
1401                 foreach_post_remat(irn, tmp) {
1402                         op_t           *remat_op = get_irn_link(tmp);
1403                         const ir_node  *value = remat_op->attr.remat.remat->value;
1404                         op_t           *val_op = get_irn_link(value);
1405
1406                         assert(remat_op->is_remat && !remat_op->attr.remat.pre);
1407
1408                         /* values that are defined by remat2s are not counted */
1409 #ifdef REMAT_WHILE_LIVE
1410                         assert(val_op->attr.live_range.ilp);
1411                         lpp_set_factor_fast(si->lpp, check_post_remat, val_op->attr.live_range.ilp, 0.0);
1412 #else
1413                         if(val_op->attr.live_range.ilp != ILP_UNDEF) {
1414                                 lpp_set_factor_fast(si->lpp, check_post_remat, val_op->attr.live_range.ilp, 0.0);
1415                         }
1416 #endif /* REMAT_WHILE_LIVE */
1417                 }
1418 #endif /* CHECK_POST_REMAT */
1419
1420
1421                 /* new live ranges for values from L\U defined by remat2s or used by remats */
1422                 pset_foreach(live, tmp) {
1423                         ir_node     *value = tmp;//remat_op->attr.remat.remat->value;
1424                         op_t        *value_op = get_irn_link(value);
1425
1426                         if(!set_find_keyval(args, value)) {
1427                                 ilp_var_t    prev_lr = ILP_UNDEF;
1428                                 ir_node     *remat;
1429                                 cst = ILP_UNDEF;
1430
1431                                 foreach_post_remat(irn, remat) {
1432                                         op_t        *remat_op = get_irn_link(remat);
1433
1434                                         /* if value is being rematerialized by this remat */
1435                                         if(value == remat_op->attr.remat.remat->value) {
1436                                                 if(cst == ILP_UNDEF) {
1437                                                         /* next_live_range <= prev_live_range + sum remat2s */
1438                                                         ir_snprintf(buf, sizeof(buf), "next_lr_%N_%N", value, irn);
1439                                                         cst = lpp_add_cst(si->lpp, buf, lpp_less, 0.0);
1440                                                         ir_snprintf(buf, sizeof(buf), "lr_%N_%N", value, irn);
1441                                                         prev_lr = lpp_add_var(si->lpp, buf, lpp_binary, 0.0);
1442                                                         lpp_set_factor_fast(si->lpp, cst, value_op->attr.live_range.ilp, 1.0);
1443                                                         lpp_set_factor_fast(si->lpp, cst, prev_lr, -1.0);
1444                                                 }
1445
1446                                                 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, -1.0);
1447                                         }
1448                                 }
1449
1450 #ifdef MAY_DIE_AT_PRE_REMAT
1451                                 if(cst == ILP_UNDEF) {
1452                                         foreach_pre_remat(si, irn, remat) {
1453                                                 int          i,
1454                                                                          n;
1455
1456                                                 for (i = 0, n = get_irn_arity(remat); i < n; ++i) {
1457                                                         ir_node        *remat_arg = get_irn_n(remat, i);
1458
1459                                                         /* if value is being used by this remat */
1460                                                         if(value == remat_arg) {
1461                                                                 /* next_live_range <= prev_live_range */
1462                                                                 ir_snprintf(buf, sizeof(buf), "lr_%N_%N", value, irn);
1463                                                                 prev_lr = lpp_add_var(si->lpp, buf, lpp_binary, 0.0);
1464
1465                                                                 ir_snprintf(buf, sizeof(buf), "next_lr_%N_%N", value, irn);
1466                                                                 cst = lpp_add_cst(si->lpp, buf, lpp_less, 0.0);
1467                                                                 lpp_set_factor_fast(si->lpp, cst, value_op->attr.live_range.ilp, 1.0);
1468                                                                 lpp_set_factor_fast(si->lpp, cst, prev_lr, -1.0);
1469                                                                 goto fertig;
1470                                                         }
1471                                                         /* TODO check afterwards whether lr dies after a pre-remat (should not happen) */
1472                                                 }
1473                                         }
1474                                 }
1475 fertig:
1476 #endif
1477
1478                                 if(prev_lr != ILP_UNDEF) {
1479                                         value_op->attr.live_range.ilp = prev_lr;
1480                                         value_op->attr.live_range.op = irn;
1481                                 }
1482                         }
1483                 }
1484
1485                 /* get count of values in my register class defined by irn */
1486                 /* also add defined values to check_post_remat; do this before iterating over args */
1487                 if(get_irn_mode(irn) == mode_T) {
1488                         ir_node  *proj = sched_next(irn);
1489                         op_t     *proj_op = get_irn_link(proj);
1490
1491                         while(is_Proj(proj)) {
1492                                 if(has_reg_class(si, proj)) {
1493                                         ++d;
1494 #ifdef CHECK_POST_REMAT
1495                                         lpp_set_factor_fast(si->lpp, check_post_remat, proj_op->attr.live_range.ilp, 1.0);
1496 #endif
1497                                 }
1498                                 proj = sched_next(proj);
1499                                 proj_op = get_irn_link(proj);
1500                         }
1501                 } else {
1502                         if(has_reg_class(si, irn)) {
1503                                  d = 1;
1504 #ifdef CHECK_POST_REMAT
1505                                  lpp_set_factor_fast(si->lpp, check_post_remat, op->attr.live_range.ilp, 1.0);
1506 #endif
1507                         }
1508                 }
1509                 DBG((si->dbg, LEVEL_4, "\t   %+F produces %d values in my register class\n", irn, d));
1510
1511                 /* count how many regs irn needs for arguments */
1512                 k = set_count(args);
1513
1514                 /* check the register pressure in the prolog */
1515                 /* sum_{L\U} lr <= n - |U| */
1516                 ir_snprintf(buf, sizeof(buf), "check_pre_%N", irn);
1517                 check_pre = lpp_add_cst(si->lpp, buf, lpp_less, si->n_regs - k);
1518
1519                 /* check the register pressure in the epilog */
1520                 ir_snprintf(buf, sizeof(buf), "check_post_%N", irn);
1521                 check_post = lpp_add_cst(si->lpp, buf, lpp_less, si->n_regs - d);
1522
1523                 set_foreach(args, keyval) {
1524                         ilp_var_t       next_lr;
1525                         op_t           *arg_op;
1526                         ilp_var_t       post_use;
1527                         int             p = 0;
1528                         spill_t        *spill;
1529                         ir_node        *arg = keyval->key;
1530
1531                         spill = add_to_spill_bb(si, bb, arg);
1532
1533                         ir_snprintf(buf, sizeof(buf), "lr_%N_%N", arg, irn);
1534                         next_lr = lpp_add_var(si->lpp, buf, lpp_binary, 0.0);
1535
1536                         i = (int)keyval->val;
1537                         assert(i<n);
1538
1539                         ir_snprintf(buf, sizeof(buf), "reload_%N_%N", arg, irn);
1540                         op->attr.live_range.reloads[i] = lpp_add_var(si->lpp, buf, lpp_binary, COST_LOAD*execution_frequency(si, bb));
1541
1542                         /* reload <= mem_out */
1543                         cst = lpp_add_cst(si->lpp, buf, lpp_less, 0.0);
1544                         lpp_set_factor_fast(si->lpp, cst, op->attr.live_range.reloads[i], 1.0);
1545                         lpp_set_factor_fast(si->lpp, cst, spill->mem_out, -1.0);
1546
1547                         arg_op = get_irn_link(arg);
1548
1549                         /* requirement: arg must be in register for use */
1550                         /* reload + remat + live_range == 1 */
1551                         ir_snprintf(buf, sizeof(buf), "req_%N_%N", irn, arg);
1552                         cst = lpp_add_cst(si->lpp, buf, lpp_equal, 1.0);
1553
1554                         lpp_set_factor_fast(si->lpp, cst, next_lr, 1.0);
1555                         lpp_set_factor_fast(si->lpp, cst, op->attr.live_range.reloads[i], 1.0);
1556                         foreach_pre_remat(si, irn, tmp) {
1557                                 op_t     *remat_op = get_irn_link(tmp);
1558                                 if(remat_op->attr.remat.remat->value == arg) {
1559                                         lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, 1.0);
1560                                 }
1561                         }
1562
1563                         /* the epilog stuff - including post_use, post, post_remat */
1564                         ir_snprintf(buf, sizeof(buf), "post_use_%N_%N", arg, irn);
1565                         post_use = lpp_add_var(si->lpp, buf, lpp_binary, 0.0);
1566
1567                         lpp_set_factor_fast(si->lpp, check_post, post_use, 1.0);
1568
1569                         /* arg is live throughout epilog if the next live_range is in a register */
1570                         if(pset_find_ptr(live, arg)) {
1571                                 DBG((si->dbg, LEVEL_3, "\t  arg %+F is possibly live in epilog of %+F\n", arg, irn));
1572
1573                                 ir_snprintf(buf, sizeof(buf), "post_use_%N_%N-%d", arg, irn, p++);
1574                                 cst = lpp_add_cst(si->lpp, buf, lpp_less, 0.0);
1575                                 lpp_set_factor_fast(si->lpp, cst, post_use, -1.0);
1576                                 lpp_set_factor_fast(si->lpp, cst, arg_op->attr.live_range.ilp, 1.0);
1577
1578 #ifdef CHECK_POST_REMAT
1579                                 lpp_set_factor_fast(si->lpp, check_post_remat, arg_op->attr.live_range.ilp, 1.0);
1580 #endif
1581                         }
1582
1583                         /*forall remat2 which use arg add a similar cst*/
1584                         foreach_post_remat(irn, tmp) {
1585                                 int         i,
1586                                                         n;
1587
1588                                 for (i = 0, n = get_irn_arity(tmp); i < n; ++i) {
1589                                         ir_node    *remat_arg = get_irn_n(tmp, i);
1590                                         op_t       *remat_op = get_irn_link(tmp);
1591
1592                                         if(remat_arg == arg) {
1593                                                 DBG((si->dbg, LEVEL_3, "\t  found remat with arg %+F in epilog of %+F\n", arg, irn));
1594
1595                                                 ir_snprintf(buf, sizeof(buf), "post_use_%N_%N-%d", arg, irn, p++);
1596                                                 cst = lpp_add_cst(si->lpp, buf, lpp_greater, 0.0);
1597                                                 lpp_set_factor_fast(si->lpp, cst, post_use, 1.0);
1598                                                 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, -1.0);
1599                                         }
1600                                 }
1601                         }
1602
1603                         /* new live range begins for each argument */
1604                         arg_op->attr.live_range.ilp = next_lr;
1605                         arg_op->attr.live_range.op = irn;
1606
1607                         pset_insert_ptr(live, arg);
1608                 }
1609
1610                 /* start new live ranges for values used by remats */
1611                 foreach_pre_remat(si, irn, tmp) {
1612                         int          i,
1613                                                  n;
1614
1615                         for (i = 0, n = get_irn_arity(tmp); i < n; ++i) {
1616                                 ir_node        *remat_arg = get_irn_n(tmp, i);
1617                                 op_t           *arg_op = get_irn_link(remat_arg);
1618                                 ilp_var_t       prev_lr;
1619
1620                                 if(!has_reg_class(si, remat_arg)) continue;
1621
1622                                 /* if value is becoming live through use by remat */
1623                                 if(!pset_find_ptr(live, remat_arg)) {
1624                                         ir_snprintf(buf, sizeof(buf), "lr_%N_%N", remat_arg, irn);
1625                                         prev_lr = lpp_add_var(si->lpp, buf, lpp_binary, 0.0);
1626
1627                                         arg_op->attr.live_range.ilp = prev_lr;
1628                                         arg_op->attr.live_range.op = irn;
1629
1630                                         DBG((si->dbg, LEVEL_4, "  value %+F becoming live through use by remat %+F\n", remat_arg, tmp));
1631
1632                                         /* TODO ist das hier die richtige Stelle???? */
1633                                         pset_insert_ptr(live, remat_arg);
1634                                         add_to_spill_bb(si, bb, remat_arg);
1635                                 }
1636                                 /* TODO check afterwards whether lr dies after a pre-remat (should not happen) */
1637                         }
1638                 }
1639
1640                 /* iterate over L\U */
1641                 pset_foreach(live, tmp) {
1642                         if(!set_find_keyval(args, tmp)) {
1643                                 /* if a live value is not used by irn */
1644                                 tmp_op = get_irn_link(tmp);
1645 //                              assert(tmp_op->attr.live_range.op != irn);
1646                                 lpp_set_factor_fast(si->lpp, check_pre, tmp_op->attr.live_range.ilp, 1.0);
1647                                 lpp_set_factor_fast(si->lpp, check_post, tmp_op->attr.live_range.ilp, 1.0);
1648                         }
1649                 }
1650
1651                 /* requirements for remats */
1652                 foreach_pre_remat(si, irn, tmp) {
1653                         op_t        *remat_op = get_irn_link(tmp);
1654                         int          i,
1655                                                  n;
1656
1657                         for (i = 0, n = get_irn_arity(tmp); i < n; ++i) {
1658                                 ir_node        *remat_arg = get_irn_n(tmp, i);
1659                                 op_t           *arg_op = get_irn_link(remat_arg);
1660
1661                                 if(!has_reg_class(si, remat_arg)) continue;
1662
1663                                 /* remat <= live_rang(remat_arg) [ + reload(remat_arg) ] */
1664                                 ir_snprintf(buf, sizeof(buf), "req_remat_%N_arg_%N", tmp, remat_arg);
1665                                 cst = lpp_add_cst(si->lpp, buf, lpp_less, 0.0);
1666
1667                                 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, 1.0);
1668                                 lpp_set_factor_fast(si->lpp, cst, arg_op->attr.live_range.ilp, -1.0);
1669
1670                                 /* if remat arg is also used by current op then we can use reload placed for this argument */
1671                                 if((keyval = set_find_keyval(args, remat_arg)) != NULL) {
1672                                         int    index = (int)keyval->val;
1673
1674                                         lpp_set_factor_fast(si->lpp, cst, op->attr.live_range.reloads[index], -1.0);
1675                                 }
1676                         }
1677                 }
1678
1679                 /* requirements for remats2
1680                  *
1681                  *  TODO unsure if this does the right thing.
1682                  *  should insert values into set if they do not become live through remat and
1683                  *  op
1684                  */
1685                 foreach_post_remat(irn, tmp) {
1686                         op_t        *remat_op = get_irn_link(tmp);
1687                         int          i,
1688                                                  n;
1689
1690                         for (i = 0, n = get_irn_arity(tmp); i < n; ++i) {
1691                                 ir_node        *remat_arg = get_irn_n(tmp, i);
1692                                 op_t           *arg_op = get_irn_link(remat_arg);
1693
1694                                 if(!has_reg_class(si, remat_arg)) continue;
1695
1696                                 /* only for values in L\U, the others are handled with post_use */
1697                                 if(!set_find_keyval(args, remat_arg)) {
1698                                         /* remat <= live_rang(remat_arg) */
1699                                         ir_snprintf(buf, sizeof(buf), "req_remat2_%N_arg_%N", tmp, remat_arg);
1700                                         cst = lpp_add_cst(si->lpp, buf, lpp_less, 0.0);
1701
1702                                         /* if value is becoming live through use by remat2 */
1703                                         if(!pset_find_ptr(live, remat_arg)) {
1704                                                 ilp_var_t     lr;
1705
1706                                                 ir_snprintf(buf, sizeof(buf), "lr_%N_%N", remat_arg, irn);
1707                                                 lr = lpp_add_var(si->lpp, buf, lpp_binary, 0.0);
1708
1709                                                 arg_op->attr.live_range.ilp = lr;
1710                                                 arg_op->attr.live_range.op = irn;
1711
1712                                                 DBG((si->dbg, LEVEL_3, "  value %+F becoming live through use by remat2 %+F\n", remat_arg, tmp));
1713
1714                                                 pset_insert_ptr(live, remat_arg);
1715                                                 add_to_spill_bb(si, bb, remat_arg);
1716                                         }
1717
1718                                         lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, 1.0);
1719                                         lpp_set_factor_fast(si->lpp, cst, arg_op->attr.live_range.ilp, -1.0);
1720                                 }
1721                         }
1722                 }
1723
1724 #ifdef CHECK_POST_REMAT
1725                 /* iterate over following remats and add them to check_post_remat */
1726                 foreach_post_remat(irn, tmp) {
1727                         op_t           *remat_op = get_irn_link(tmp);
1728
1729                         assert(remat_op->is_remat && !remat_op->attr.remat.pre);
1730
1731                         lpp_set_factor_fast(si->lpp, check_post_remat, remat_op->attr.remat.ilp, 1.0);
1732                 }
1733 #endif
1734
1735
1736
1737                 DBG((si->dbg, LEVEL_4, "\t   %d values live at %+F\n", pset_count(live), irn));
1738
1739                 pset_foreach(live, tmp) {
1740                         assert(has_reg_class(si, tmp));
1741                 }
1742
1743                 for (i = 0, n = get_irn_arity(irn); i < n; ++i) {
1744                         ir_node        *arg = get_irn_n(irn, i);
1745
1746                         assert(!find_post_remat(arg, irn) && "there should be no post remat for an argument of an op");
1747                 }
1748
1749                 del_set(args);
1750         }
1751
1752
1753
1754         /* do something at the beginning of the block */
1755
1756         /* we are now at the beginning of the basic block, there are only \Phis in front of us */
1757         DBG((si->dbg, LEVEL_3, "\t   %d values live at beginning of block %+F\n", pset_count(live), bb));
1758
1759         pset_foreach(live, irn) {
1760                 assert(is_Phi(irn) || get_nodes_block(irn) != bb);
1761         }
1762
1763         /* construct mem_outs for all values */
1764
1765         set_foreach(spill_bb->ilp, spill) {
1766                 ir_snprintf(buf, sizeof(buf), "mem_out_%N_%N", spill->irn, bb);
1767                 cst = lpp_add_cst(si->lpp, buf, lpp_less, 0.0);
1768
1769                 lpp_set_factor_fast(si->lpp, cst, spill->mem_out, 1.0);
1770                 lpp_set_factor_fast(si->lpp, cst, spill->spill, -1.0);
1771
1772                 if(pset_find_ptr(live, spill->irn)) {
1773                         DBG((si->dbg, LEVEL_5, "\t     %+F live at beginning of block %+F\n", spill->irn, bb));
1774
1775                         ir_snprintf(buf, sizeof(buf), "mem_in_%N_%N", spill->irn, bb);
1776                         spill->mem_in = lpp_add_var(si->lpp, buf, lpp_binary, 0.0);
1777
1778                         lpp_set_factor_fast(si->lpp, cst, spill->mem_in, -1.0);
1779                 }
1780         }
1781
1782
1783         /* L\U is empty at bb start */
1784         /* arg is live throughout epilog if it is reg_in into this block */
1785
1786         /* check the register pressure at the beginning of the block
1787          * including remats
1788          */
1789         ir_snprintf(buf, sizeof(buf), "check_start_%N", bb);
1790         cst = lpp_add_cst(si->lpp, buf, lpp_less, si->n_regs);
1791
1792         pset_foreach(live, irn) {
1793                         spill = set_find_spill(spill_bb->ilp, irn);
1794                         assert(spill);
1795
1796                         ir_snprintf(buf, sizeof(buf), "reg_in_%N_%N", irn, bb);
1797                         spill->reg_in = lpp_add_var(si->lpp, buf, lpp_binary, 0.0);
1798
1799                         lpp_set_factor_fast(si->lpp, cst, spill->reg_in, 1.0);
1800         }
1801         foreach_post_remat(bb, irn) {
1802                 op_t     *remat_op = get_irn_link(irn);
1803
1804                 DBG((si->dbg, LEVEL_4, "\t  next post remat: %+F\n", irn));
1805                 assert(remat_op->is_remat && !remat_op->attr.remat.pre);
1806
1807                 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, 1.0);
1808         }
1809
1810         /* forall remat2 add requirements */
1811         foreach_post_remat(bb, tmp) {
1812                 int         i,
1813                                         n;
1814
1815                 for (i = 0, n = get_irn_arity(tmp); i < n; ++i) {
1816                         ir_node    *remat_arg = get_irn_n(tmp, i);
1817                         op_t       *remat_op = get_irn_link(tmp);
1818
1819                         if(!has_reg_class(si, remat_arg)) continue;
1820
1821                         spill = set_find_spill(spill_bb->ilp, remat_arg);
1822                         assert(spill);
1823
1824                         /* TODO verify this is placed correctly */
1825                         ir_snprintf(buf, sizeof(buf), "req_remat2_%N_%N_arg_%N", tmp, bb, remat_arg);
1826                         cst = lpp_add_cst(si->lpp, buf, lpp_less, 0.0);
1827                         lpp_set_factor_fast(si->lpp, cst, spill->reg_in, -1.0);
1828                         lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, 1.0);
1829                 }
1830         }
1831
1832         /* mem_in/reg_in for live_in values, especially phis and their arguments */
1833         pset_foreach(live, irn) {
1834                 int          p = 0,
1835                                          i,
1836                                          n;
1837
1838                 spill = set_find_spill(spill_bb->ilp, irn);
1839                 assert(spill && spill->irn == irn);
1840
1841                 if(is_Phi(irn) && get_nodes_block(irn) == bb) {
1842                         for (i = 0, n = get_Phi_n_preds(irn); i < n; ++i) {
1843                                 ilp_cst_t       mem_in,
1844                                                                 reg_in;
1845                                 ir_node        *phi_arg = get_Phi_pred(irn, i);
1846                                 ir_node        *bb_p = get_Block_cfgpred_block(bb, i);
1847                                 spill_bb_t     *spill_bb_p = get_irn_link(bb_p);
1848                                 spill_t        *spill_p;
1849
1850                                 /* although the phi is in the right regclass one or more of
1851                                  * its arguments can be in a different one or at least to
1852                                  * ignore
1853                                  */
1854                                 if(has_reg_class(si, phi_arg)) {
1855                                         ir_snprintf(buf, sizeof(buf), "mem_in_%N_%N-%d", irn, bb, p);
1856                                         mem_in = lpp_add_cst(si->lpp, buf, lpp_less, 0.0);
1857                                         ir_snprintf(buf, sizeof(buf), "reg_in_%N_%N-%d", irn, bb, p++);
1858                                         reg_in = lpp_add_cst(si->lpp, buf, lpp_less, 0.0);
1859
1860                                         lpp_set_factor_fast(si->lpp, mem_in, spill->mem_in, 1.0);
1861                                         lpp_set_factor_fast(si->lpp, reg_in, spill->reg_in, 1.0);
1862
1863                                         spill_p = set_find_spill(spill_bb_p->ilp, phi_arg);
1864                                         assert(spill_p);
1865
1866                                         lpp_set_factor_fast(si->lpp, mem_in, spill_p->mem_out, -1.0);
1867                                         lpp_set_factor_fast(si->lpp, reg_in, spill_p->reg_out, -1.0);
1868                                 }
1869                         }
1870                 } else {
1871                         /* else assure the value arrives on all paths in the same resource */
1872
1873                         for (i = 0, n = get_Block_n_cfgpreds(bb); i < n; ++i) {
1874                                 ilp_cst_t       mem_in,
1875                                                                 reg_in;
1876                                 ir_node        *bb_p = get_Block_cfgpred_block(bb, i);
1877                                 spill_bb_t     *spill_bb_p = get_irn_link(bb_p);
1878                                 spill_t        *spill_p;
1879
1880                                 ir_snprintf(buf, sizeof(buf), "mem_in_%N_%N-%d", irn, bb, p);
1881                                 mem_in = lpp_add_cst(si->lpp, buf, lpp_less, 0.0);
1882                                 ir_snprintf(buf, sizeof(buf), "reg_in_%N_%N-%d", irn, bb, p++);
1883                                 reg_in = lpp_add_cst(si->lpp, buf, lpp_less, 0.0);
1884
1885                                 lpp_set_factor_fast(si->lpp, mem_in, spill->mem_in, 1.0);
1886                                 lpp_set_factor_fast(si->lpp, reg_in, spill->reg_in, 1.0);
1887
1888                                 spill_p = set_find_spill(spill_bb_p->ilp, irn);
1889                                 assert(spill_p);
1890
1891                                 lpp_set_factor_fast(si->lpp, mem_in, spill_p->mem_out, -1.0);
1892                                 lpp_set_factor_fast(si->lpp, reg_in, spill_p->reg_out, -1.0);
1893                         }
1894                 }
1895         }
1896
1897         /* first live ranges from reg_ins */
1898         pset_foreach(live, irn) {
1899                 op_t      *op = get_irn_link(irn);
1900
1901                 spill = set_find_spill(spill_bb->ilp, irn);
1902                 assert(spill && spill->irn == irn);
1903
1904                 ir_snprintf(buf, sizeof(buf), "first_lr_%N_%N", irn, bb);
1905                 cst = lpp_add_cst(si->lpp, buf, lpp_less, 0.0);
1906                 lpp_set_factor_fast(si->lpp, cst, op->attr.live_range.ilp, 1.0);
1907                 lpp_set_factor_fast(si->lpp, cst, spill->reg_in, -1.0);
1908
1909                 foreach_post_remat(bb, tmp) {
1910                         op_t     *remat_op = get_irn_link(tmp);
1911
1912                         if(remat_op->attr.remat.remat->value == irn) {
1913                                 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, -1.0);
1914                         }
1915                 }
1916         }
1917
1918         /* walk forward now and compute constraints for placing spills */
1919         /* this must only be done for values that are not defined in this block */
1920         /* TODO are these values at start of block? if yes, just check whether this is a diverge edge and skip the loop */
1921         pset_foreach(live, irn) {
1922                 spill = set_find_spill(spill_bb->ilp, irn);
1923                 assert(spill);
1924
1925                 ir_snprintf(buf, sizeof(buf), "req_spill_%N_%N", irn, bb);
1926                 cst = lpp_add_cst(si->lpp, buf, lpp_less, 0.0);
1927
1928                 lpp_set_factor_fast(si->lpp, cst, spill->spill, 1.0);
1929                 if(is_diverge_edge(bb)) lpp_set_factor_fast(si->lpp, cst, spill->reg_in, -1.0);
1930
1931                 sched_foreach_op(bb, tmp) {
1932                         op_t   *op = get_irn_link(tmp);
1933
1934                         if(is_Phi(tmp)) continue;
1935                         assert(!is_Proj(tmp));
1936
1937                         if(op->is_remat) {
1938                                 ir_node   *value = op->attr.remat.remat->value;
1939
1940                                 if(value == irn) {
1941                                         /* only collect remats up to the first use of a value */
1942                                         lpp_set_factor_fast(si->lpp, cst, op->attr.remat.ilp, -1.0);
1943                                 }
1944                         } else {
1945                                 int i,
1946                                         n;
1947
1948                                 for (i = 0, n = get_irn_arity(tmp); i < n; ++i) {
1949                                         ir_node    *arg = get_irn_n(tmp, i);
1950
1951                                         if(arg == irn) {
1952                                                 /* if a value is used stop collecting remats */
1953                                                 cst = ILP_UNDEF;
1954                                         }
1955                                         break;
1956                                 }
1957                         }
1958                         if(cst == ILP_UNDEF) break;
1959                 }
1960         }
1961
1962
1963         /* if a value is used by a mem-phi, then mem_in of this value is 0 (has to be spilled again into a different slot)
1964            mem_in(phi) -> not mem_in(orig_value) TODO: how does this depend on a certain predecessor?
1965          */
1966
1967         /* mem_in of mem-phi has associated costs (but first one is free) */
1968         /* define n_mem_copies as positive integer in each predecessor block,
1969            #mem_in into this block from predecessor block - 1 weighted with SPILL_COST*execfreq(predecessor)
1970            TODO
1971          */
1972
1973
1974         del_pset(live);
1975 }
1976
1977
1978 #if 0
1979          * Speicherkopienminimierung: teste Speicherwerte auf Interferenz
1980          * und weise Spillkontexte zu. Sorge bei Phis dafuer, dass gleiche
1981          * Kontexte zusammenfliessen (Operanden und Ergebnis hat gleichen
1982          * Kontext)
1983 #endif
1984
1985 static INLINE int
1986 is_zero(double x)
1987 {
1988         return fabs(x) < 0.00001;
1989 }
1990
1991 #if 0
1992 static int
1993 is_spilled(const spill_ilp_t * si, const live_range_t * lr)
1994 {
1995         return !is_zero(lpp_get_var_sol(si->lpp, lr->in_mem_var));
1996 }
1997 #endif
1998
1999 static int
2000 is_mem_phi(const ir_node * phi, void *data)
2001 {
2002         spill_ilp_t    *si = data;
2003 //      return is_spilled(si, get_use_head(si, phi)->closest_use);
2004         return 0;
2005 }
2006
2007 #ifdef KEEPALIVE
2008 static int mark_remat_nodes_hook(FILE *F, ir_node *n, ir_node *l)
2009 {
2010         spill_ilp_t *si = get_irg_link(current_ir_graph);
2011
2012         if(pset_find_ptr(si->all_possible_remats, n)) {
2013                 op_t   *op = (op_t*)get_irn_link(n);
2014                 assert(op && op->is_remat);
2015
2016                 if(!op->attr.remat.remat->inverse) {
2017                         if(op->attr.remat.pre) {
2018                                 ir_fprintf(F, "color:red info3:\"remat value: %+F\"", op->attr.remat.remat->value);
2019                         } else {
2020                                 ir_fprintf(F, "color:orange info3:\"remat2 value: %+F\"", op->attr.remat.remat->value);
2021                         }
2022
2023                         return 1;
2024                 } else {
2025                         op_t   *op = (op_t*)get_irn_link(n);
2026                         assert(op && op->is_remat);
2027
2028                         if(op->attr.remat.pre) {
2029                                 ir_fprintf(F, "color:cyan info3:\"remat inverse value: %+F\"", op->attr.remat.remat->value);
2030                         } else {
2031                                 ir_fprintf(F, "color:lightcyan info3:\"remat2 inverse value: %+F\"", op->attr.remat.remat->value);
2032                         }
2033
2034                         return 1;
2035                 }
2036         }
2037
2038         return 0;
2039 }
2040
2041 static void
2042 dump_graph_with_remats(ir_graph * irg, const char * suffix)
2043 {
2044         set_dump_node_vcgattr_hook(mark_remat_nodes_hook);
2045         be_dump(irg, suffix, dump_ir_block_graph_sched);
2046         set_dump_node_vcgattr_hook(NULL);
2047 }
2048 #endif
2049
2050 /**
2051  * Edge hook to dump the schedule edges with annotated register pressure.
2052  */
2053 static int
2054 sched_pressure_edge_hook(FILE *F, ir_node *irn)
2055 {
2056         if(sched_is_scheduled(irn) && sched_has_prev(irn)) {
2057                 ir_node *prev = sched_prev(irn);
2058                 fprintf(F, "edge:{sourcename:\"");
2059                 PRINT_NODEID(irn);
2060                 fprintf(F, "\" targetname:\"");
2061                 PRINT_NODEID(prev);
2062                 fprintf(F, "\" label:\"%d", (int)get_irn_link(irn));
2063                 fprintf(F, "\" color:magenta}\n");
2064         }
2065         return 1;
2066 }
2067
2068 static void
2069 dump_ir_block_graph_sched_pressure(ir_graph *irg, const char *suffix)
2070 {
2071         DUMP_NODE_EDGE_FUNC old = get_dump_node_edge_hook();
2072
2073         dump_consts_local(0);
2074         set_dump_node_edge_hook(sched_pressure_edge_hook);
2075         dump_ir_block_graph(irg, suffix);
2076         set_dump_node_edge_hook(old);
2077 }
2078
2079 static void
2080 walker_pressure_annotator(ir_node * bb, void * data)
2081 {
2082         spill_ilp_t  *si = data;
2083         ir_node      *irn;
2084         irn_live_t   *li;
2085         int           i,
2086                                   n;
2087         pset         *live = pset_new_ptr_default();
2088         int           projs = 0;
2089
2090         live_foreach(bb, li) {
2091                 irn = (ir_node *) li->irn;
2092
2093                 if (live_is_end(li) && has_reg_class(si, irn)) {
2094                         pset_insert_ptr(live, irn);
2095                 }
2096         }
2097
2098         set_irn_link(bb, INT_TO_PTR(pset_count(live)));
2099
2100         sched_foreach_reverse(bb, irn) {
2101                 if(is_Phi(irn)) {
2102                         set_irn_link(irn, INT_TO_PTR(pset_count(live)));
2103                         continue;
2104                 }
2105
2106                 if(has_reg_class(si, irn)) {
2107                         pset_remove_ptr(live, irn);
2108                         if(is_Proj(irn)) ++projs;
2109                 }
2110
2111                 if(!is_Proj(irn)) projs = 0;
2112
2113                 for (i = 0, n = get_irn_arity(irn); i < n; ++i) {
2114                         ir_node    *arg = get_irn_n(irn, i);
2115
2116                         if(has_reg_class(si, arg)) pset_insert_ptr(live, arg);
2117                 }
2118                 set_irn_link(irn, INT_TO_PTR(pset_count(live)+projs));
2119         }
2120
2121         del_pset(live);
2122 }
2123
2124 static void
2125 dump_pressure_graph(spill_ilp_t * si, const char *suffix)
2126 {
2127         be_dump(si->chordal_env->irg, suffix, dump_ir_block_graph_sched_pressure);
2128 }
2129
2130 #ifdef KEEPALIVE
2131 static void
2132 connect_all_remats_with_keep(spill_ilp_t * si)
2133 {
2134         ir_node   *irn;
2135         ir_node  **ins,
2136                          **pos;
2137         int        n_remats;
2138
2139
2140         n_remats = pset_count(si->all_possible_remats);
2141         if(n_remats) {
2142                 ins = obstack_alloc(si->obst, n_remats * sizeof(*ins));
2143
2144                 pos = ins;
2145                 pset_foreach(si->all_possible_remats, irn) {
2146                         *pos = irn;
2147                         ++pos;
2148                 }
2149
2150                 si->keep = be_new_Keep(si->chordal_env->cls, si->chordal_env->irg, get_irg_end_block(si->chordal_env->irg), n_remats, ins);
2151
2152                 obstack_free(si->obst, ins);
2153         }
2154 }
2155 #endif
2156
2157 static void
2158 connect_all_spills_with_keep(spill_ilp_t * si)
2159 {
2160         ir_node   *irn;
2161         ir_node  **ins,
2162                          **pos;
2163         int        n_spills;
2164         ir_node   *keep;
2165
2166
2167         n_spills = pset_count(si->spills);
2168         if(n_spills) {
2169                 ins = obstack_alloc(si->obst, n_spills * sizeof(*ins));
2170
2171                 pos = ins;
2172                 pset_foreach(si->spills, irn) {
2173                         *pos = irn;
2174                         ++pos;
2175                 }
2176
2177                 keep = be_new_Keep(si->chordal_env->cls, si->chordal_env->irg, get_irg_end_block(si->chordal_env->irg), n_spills, ins);
2178
2179                 obstack_free(si->obst, ins);
2180         }
2181 }
2182
2183 /** insert a spill at an arbitrary position */
2184 ir_node *be_spill2(const arch_env_t *arch_env, ir_node *irn, ir_node *insert, ir_node *ctx)
2185 {
2186         ir_node *bl     = is_Block(insert)?insert:get_nodes_block(insert);
2187         ir_graph *irg   = get_irn_irg(bl);
2188         ir_node *frame  = get_irg_frame(irg);
2189         ir_node *spill;
2190         ir_node *next;
2191
2192         const arch_register_class_t *cls       = arch_get_irn_reg_class(arch_env, irn, -1);
2193         const arch_register_class_t *cls_frame = arch_get_irn_reg_class(arch_env, frame, -1);
2194
2195         spill = be_new_Spill(cls, cls_frame, irg, bl, frame, irn, ctx);
2196
2197         /*
2198          * search the right insertion point. a spill of a phi cannot be put
2199          * directly after the phi, if there are some phis behind the one which
2200          * is spilled. Also, a spill of a Proj must be after all Projs of the
2201          * same tuple node.
2202          *
2203          * Here's one special case:
2204          * If the spill is in the start block, the spill must be after the frame
2205          * pointer is set up. This is done by setting insert to the end of the block
2206          * which is its default initialization (see above).
2207          */
2208
2209         if(bl == get_irg_start_block(irg) && sched_get_time_step(frame) >= sched_get_time_step(insert))
2210                 insert = frame;
2211
2212         for (next = sched_next(insert); is_Phi(next) || is_Proj(next); next = sched_next(insert))
2213                 insert = next;
2214
2215         sched_add_after(insert, spill);
2216         return spill;
2217 }
2218
2219 static void
2220 delete_remat(spill_ilp_t * si, ir_node * remat) {
2221         int       i,
2222                       n;
2223         ir_node  *bad = get_irg_bad(si->chordal_env->irg);
2224
2225         sched_remove(remat);
2226
2227         /* kill links to operands */
2228         for (i = -1, n = get_irn_arity(remat); i < n; ++i) {
2229                 set_irn_n(remat, i, bad);
2230         }
2231 }
2232
2233 static void
2234 clean_remat_info(spill_ilp_t * si)
2235 {
2236         int            i,
2237                                n;
2238         remat_t       *remat;
2239         remat_info_t  *remat_info;
2240         ir_node       *bad = get_irg_bad(si->chordal_env->irg);
2241
2242         set_foreach(si->remat_info, remat_info) {
2243                 if(!remat_info->remats) continue;
2244
2245                 pset_foreach(remat_info->remats, remat)
2246                 {
2247                         if(remat->proj && get_irn_n_edges(remat->proj) == 0) {
2248                                 set_irn_n(remat->proj, -1, bad);
2249                                 set_irn_n(remat->proj, 0, bad);
2250                         }
2251
2252                         if(get_irn_n_edges(remat->op) == 0) {
2253                                 for (i = -1, n = get_irn_arity(remat->op); i < n; ++i) {
2254                                         set_irn_n(remat->op, i, bad);
2255                                 }
2256                         }
2257                 }
2258
2259                 if(remat_info->remats) del_pset(remat_info->remats);
2260                 if(remat_info->remats_by_operand) del_pset(remat_info->remats_by_operand);
2261         }
2262 }
2263
2264 static void
2265 delete_unnecessary_remats(spill_ilp_t * si)
2266 {
2267 #ifdef KEEPALIVE
2268         int       i,
2269                       n;
2270         ir_node  *bad = get_irg_bad(si->chordal_env->irg);
2271
2272         if(si->keep) {
2273                 ir_node   *end = get_irg_end(si->chordal_env->irg);
2274                 ir_node  **keeps;
2275
2276                 for (i = 0, n = get_irn_arity(si->keep); i < n; ++i) {
2277                         ir_node        *keep_arg = get_irn_n(si->keep, i);
2278                         op_t           *arg_op = get_irn_link(keep_arg);
2279                         lpp_name_t     *name;
2280
2281                         assert(arg_op->is_remat);
2282
2283                         name = si->lpp->vars[arg_op->attr.remat.ilp];
2284
2285                         if(is_zero(name->value)) {
2286                                 DBG((si->dbg, LEVEL_3, "\t  deleting remat %+F\n", keep_arg));
2287                                 /* TODO check whether reload is preferred over remat (could be bug) */
2288                                 delete_remat(si, keep_arg);
2289                         } else {
2290                                 if(!arg_op->attr.remat.remat->inverse) {
2291                                         if(arg_op->attr.remat.pre) {
2292                                                 DBG((si->dbg, LEVEL_2, "\t**remat kept: %+F\n", keep_arg));
2293                                         } else {
2294                                                 DBG((si->dbg, LEVEL_2, "\t%%%%remat2 kept: %+F\n", keep_arg));
2295                                         }
2296                                 } else {
2297                                         if(arg_op->attr.remat.pre) {
2298                                                 DBG((si->dbg, LEVEL_2, "\t**INVERSE remat kept: %+F\n", keep_arg));
2299                                         } else {
2300                                                 DBG((si->dbg, LEVEL_2, "\t%%%%INVERSE remat2 kept: %+F\n", keep_arg));
2301                                         }
2302                                 }
2303                         }
2304
2305                         set_irn_n(si->keep, i, bad);
2306                 }
2307 #if 0
2308                 for (i = 0, n = get_End_n_keepalives(end); i < n; ++i) {
2309                         ir_node        *end_arg = get_End_keepalive(end, i);
2310
2311                         if(end_arg != si->keep) {
2312                                 obstack_grow(si->obst, &end_arg, sizeof(end_arg));
2313                         }
2314                 }
2315                 keeps = obstack_finish(si->obst);
2316                 set_End_keepalives(end, n-1, keeps);
2317                 obstack_free(si->obst, keeps);
2318 #endif
2319         } else {
2320                 DBG((si->dbg, LEVEL_2, "\t  no remats to delete (none have been inserted)\n"));
2321         }
2322 #else
2323         ir_node  *remat;
2324
2325         pset_foreach(si->all_possible_remats, remat) {
2326                 op_t           *remat_op = get_irn_link(remat);
2327                 lpp_name_t     *name = si->lpp->vars[remat_op->attr.remat.ilp];
2328
2329                 if(is_zero(name->value)) {
2330                         DBG((si->dbg, LEVEL_3, "\t  deleting remat %+F\n", remat));
2331                         /* TODO check whether reload is preferred over remat (could be bug) */
2332                         delete_remat(si, remat);
2333                 } else {
2334                         if(!remat_op->attr.remat.remat->inverse) {
2335                                 if(remat_op->attr.remat.pre) {
2336                                         DBG((si->dbg, LEVEL_2, "\t**remat kept: %+F\n", remat));
2337                                 } else {
2338                                         DBG((si->dbg, LEVEL_2, "\t%%%%remat2 kept: %+F\n", remat));
2339                                 }
2340                         } else {
2341                                 if(remat_op->attr.remat.pre) {
2342                                         DBG((si->dbg, LEVEL_2, "\t**INVERSE remat kept: %+F\n", remat));
2343                                 } else {
2344                                         DBG((si->dbg, LEVEL_2, "\t%%%%INVERSE remat2 kept: %+F\n", remat));
2345                                 }
2346                         }
2347                 }
2348         }
2349 #endif
2350 }
2351
2352 /**
2353  * @param before   The node after which the spill will be placed in the schedule
2354  */
2355 /* TODO set context properly */
2356 static ir_node *
2357 insert_spill(spill_ilp_t * si, const ir_node * irn, const ir_node * value, const ir_node * before)
2358 {
2359         defs_t   *defs;
2360         ir_node  *spill;
2361         const arch_env_t *arch_env = si->chordal_env->birg->main_env->arch_env;
2362
2363         DBG((si->dbg, LEVEL_3, "\t  inserting spill for value %+F after %+F\n", irn, before));
2364
2365         spill = be_spill2(arch_env, irn, before, irn);
2366
2367         defs = set_insert_def(si->values, value);
2368         assert(defs);
2369
2370         /* enter into the linked list */
2371         set_irn_link(spill, defs->spills);
2372         defs->spills = spill;
2373
2374 #ifdef KEEPALIVE_SPILLS
2375         pset_insert_ptr(si->spills, spill);
2376 #endif
2377
2378         return spill;
2379 }
2380
2381 /**
2382  * @param before   The Phi node which has to be spilled
2383  */
2384 static ir_node *
2385 insert_mem_phi(spill_ilp_t * si, const ir_node * phi)
2386 {
2387         ir_node   *mem_phi;
2388         ir_node  **ins;
2389         defs_t    *defs;
2390         int        i,
2391                            n;
2392
2393         NEW_ARR_A(ir_node*, ins, get_irn_arity(phi));
2394
2395         for(i=0,n=get_irn_arity(phi); i<n; ++i) {
2396                 ins[i] = si->m_unknown;
2397         }
2398
2399         mem_phi =  new_r_Phi(si->chordal_env->irg, get_nodes_block(phi), get_irn_arity(phi), ins, mode_M);
2400
2401         defs = set_insert_def(si->values, phi);
2402         assert(defs);
2403
2404         /* enter into the linked list */
2405         set_irn_link(mem_phi, defs->spills);
2406         defs->spills = mem_phi;
2407
2408         sched_add_after(phi, mem_phi);
2409
2410 #ifdef KEEPALIVE_SPILLS
2411         pset_insert_ptr(si->spills, mem_phi);
2412 #endif
2413
2414         return mem_phi;
2415 }
2416
2417 /**
2418  * Add remat to list of defs, destroys link field!
2419  */
2420 static void
2421 insert_remat(spill_ilp_t * si, ir_node * remat)
2422 {
2423         defs_t   *defs;
2424         op_t     *remat_op = get_irn_link(remat);
2425
2426         assert(remat_op->is_remat);
2427
2428         defs = set_insert_def(si->values, remat_op->attr.remat.remat->value);
2429         assert(defs);
2430
2431         /* enter into the linked list */
2432         set_irn_link(remat, defs->remats);
2433         defs->remats = remat;
2434 }
2435
2436 #if 0
2437 static void
2438 collect_spills(spill_ilp_t * si, ir_node * value, pset * spills, pset * visited)
2439 {
2440         ir_node  *next;
2441         defs_t   *defs;
2442
2443         defs = set_find_def(si->values, value);
2444
2445         if(defs && defs->spills) {
2446                 for(next = defs->spills; next; next = get_irn_link(next)) {
2447                         pset_insert_ptr(spills, next);
2448                 }
2449         } else if (is_Phi(value)) {
2450                 /* recursion */
2451                 if(!pset_find_ptr(visited, value)) {
2452                         int    i,
2453                                    n;
2454
2455                         pset_insert_ptr(visited, value);
2456                         for(i=0, n=get_irn_arity(value); i<n; ++i) {
2457                                 ir_node    *arg = get_irn_n(value, i);
2458
2459                                 collect_spills(si, arg, spills, visited);
2460                         }
2461                 }
2462         } else {
2463 //              assert(0 && "Phi operand not spilled");
2464         }
2465 }
2466 #endif
2467
2468 static pset *
2469 get_spills_for_value(spill_ilp_t * si, ir_node * value)
2470 {
2471         pset     *spills = pset_new_ptr_default();
2472 //      pset     *visited = pset_new_ptr_default();
2473
2474 //      collect_spills(si, value, spills, visited);
2475 //      del_pset(visited);
2476         ir_node  *next;
2477         defs_t   *defs;
2478
2479         defs = set_find_def(si->values, value);
2480
2481         if(defs && defs->spills) {
2482                 for(next = defs->spills; next; next = get_irn_link(next)) {
2483                         pset_insert_ptr(spills, next);
2484                 }
2485         }
2486
2487         return spills;
2488 }
2489
2490 /**
2491  * Add reload before operation and add to list of defs
2492  */
2493 static ir_node *
2494 insert_reload(spill_ilp_t * si, const ir_node * value, const ir_node * after)
2495 {
2496         defs_t   *defs;
2497         ir_node  *reload,
2498                          *spill;
2499         const arch_env_t *arch_env = si->chordal_env->birg->main_env->arch_env;
2500
2501         DBG((si->dbg, LEVEL_3, "\t  inserting reload for value %+F before %+F\n", value, after));
2502
2503         defs = set_find_def(si->values, value);
2504         /* get a spill of this value */
2505 #if 0
2506         if((!defs || !defs->spills) && is_Phi(value)) {
2507                 pset  *spills;
2508
2509                 spills = get_spills_for_value(si, value);
2510
2511                 spill = pset_first(spills);
2512                 del_pset(spills);
2513
2514                 if(!defs) {
2515                         defs = set_insert_def(si->values, value);
2516                 }
2517                 defs->spills = spill;
2518                 set_irn_link(spill, NULL);
2519         } else {
2520                 spill = defs->spills;
2521         }
2522 #endif
2523         spill = defs->spills;
2524         assert(spill && "no spill placed before reload");
2525
2526         reload = be_reload(arch_env, si->cls, after, get_irn_mode(value), spill);
2527
2528         /* enter into the linked list */
2529         set_irn_link(reload, defs->remats);
2530         defs->remats = reload;
2531
2532         return reload;
2533 }
2534
2535 static void
2536 walker_spill_placer(ir_node * bb, void * data) {
2537         spill_ilp_t   *si = (spill_ilp_t*)data;
2538         ir_node       *irn;
2539         spill_bb_t    *spill_bb = get_irn_link(bb);
2540         pset          *spills_to_do = pset_new_ptr_default();
2541         spill_t       *spill;
2542
2543         set_foreach(spill_bb->ilp, spill) {
2544                 lpp_name_t    *name;
2545
2546                 if(is_Phi(spill->irn) && get_nodes_block(spill->irn) == bb) {
2547                         name = si->lpp->vars[spill->mem_in];
2548                         if(!is_zero(name->value)) {
2549                                 ir_node   *mem_phi;
2550
2551                                 mem_phi = insert_mem_phi(si, spill->irn);
2552
2553                                 DBG((si->dbg, LEVEL_2, "\t >>spilled Phi %+F -> %+F\n", spill->irn, mem_phi));
2554                         }
2555                 }
2556
2557                 name = si->lpp->vars[spill->spill];
2558                 if(!is_zero(name->value)) {
2559                         if(spill->reg_in > 0) {
2560                                 name = si->lpp->vars[spill->reg_in];
2561                                 if(!is_zero(name->value)) {
2562                                         insert_spill(si, spill->irn, spill->irn, bb);
2563                                         continue;
2564                                 }
2565                         }
2566                         pset_insert_ptr(spills_to_do, spill->irn);
2567                 }
2568         }
2569         DBG((si->dbg, LEVEL_3, "\t  %d spills to do in block %+F\n", pset_count(spills_to_do), bb));
2570
2571
2572         for(irn = sched_block_first_nonphi(bb); !sched_is_end(irn); irn = sched_next(irn)) {
2573                 op_t     *op = get_irn_link(irn);
2574
2575                 if(be_is_Spill(irn)) continue;
2576
2577                 if(op->is_remat) {
2578                         /* TODO fix this if we want to support remats with more than two nodes */
2579                         if(get_irn_mode(irn) != mode_T && pset_find_ptr(spills_to_do, op->attr.remat.remat->value)) {
2580                                 pset_remove_ptr(spills_to_do, op->attr.remat.remat->value);
2581
2582                                 insert_spill(si, irn, op->attr.remat.remat->value, irn);
2583                         }
2584                 } else {
2585                         if(pset_find_ptr(spills_to_do, irn)) {
2586                                 pset_remove_ptr(spills_to_do, irn);
2587
2588                                 insert_spill(si, irn, irn, irn);
2589                         }
2590                 }
2591
2592         }
2593
2594         assert(pset_count(spills_to_do) == 0);
2595
2596         /* afterwards free data in block */
2597         del_pset(spills_to_do);
2598 }
2599
2600 static void
2601 phim_fixer(spill_ilp_t *si) {
2602         defs_t  *defs;
2603
2604         set_foreach(si->values, defs) {
2605                 const ir_node  *phi = defs->value;
2606                 ir_node  *phi_m = NULL;
2607                 ir_node  *next = defs->spills;
2608                 int       i,
2609                                   n;
2610
2611                 if(!is_Phi(phi)) continue;
2612
2613                 while(next) {
2614                         if(is_Phi(next) && get_irn_mode(next) == mode_M) {
2615                                 phi_m = next;
2616                                 break;
2617                         } else {
2618                                 next = get_irn_link(next);
2619                         }
2620                 }
2621                 if(!phi_m) continue;
2622
2623                 for(i=0,n=get_irn_arity(phi); i<n; ++i) {
2624                         const ir_node  *value = get_irn_n(phi, i);
2625                         defs_t         *val_defs = set_find_def(si->values, value);
2626
2627                         /* get a spill of this value */
2628                         ir_node      *spill = val_defs->spills;
2629
2630                         assert(spill && "no spill placed before PhiM");
2631
2632                         set_irn_n(phi_m, i, spill);
2633                 }
2634         }
2635 }
2636
2637 static void
2638 walker_reload_placer(ir_node * bb, void * data) {
2639         spill_ilp_t   *si = (spill_ilp_t*)data;
2640         ir_node       *irn;
2641         spill_bb_t    *spill_bb = get_irn_link(bb);
2642         int            i;
2643         irn_live_t    *li;
2644
2645         sched_foreach_reverse(bb, irn) {
2646                 op_t     *op = get_irn_link(irn);
2647
2648                 if(be_is_Reload(irn) || be_is_Spill(irn)) continue;
2649                 if(is_Phi(irn)) break;
2650
2651                 if(op->is_remat) {
2652                         if(get_irn_mode(irn) != mode_T) {
2653                                 insert_remat(si, irn);
2654                         }
2655                 } else {
2656                         int    n;
2657
2658                         for (i = 0, n = get_irn_arity(irn); i < n; ++i) {
2659                                 ir_node    *arg = get_irn_n(irn, i);
2660
2661                                 if(op->attr.live_range.reloads && op->attr.live_range.reloads[i] != ILP_UNDEF) {
2662                                         lpp_name_t    *name;
2663
2664                                         name = si->lpp->vars[op->attr.live_range.reloads[i]];
2665                                         if(!is_zero(name->value)) {
2666                                                 ir_node    *reload;
2667                                                 ir_node    *insert_pos = irn;
2668                                                 ir_node    *prev = insert_pos;
2669                                                 op_t       *prev_op;
2670
2671                                                 do {
2672                                                         prev = sched_prev(prev);
2673                                                 } while(be_is_Spill(prev));
2674
2675                                                 prev_op = get_irn_link(prev);
2676
2677                                                 /* insert reload before pre-remats */
2678                                                 while(!sched_is_end(prev) && !be_is_Reload(prev) && !is_Phi(prev)
2679                                                                 && prev_op->is_remat && prev_op->attr.remat.pre) {
2680                                                         insert_pos = prev;
2681
2682                                                         do {
2683                                                                 prev = sched_prev(prev);
2684                                                         } while(be_is_Spill(prev));
2685
2686                                                         prev_op = get_irn_link(prev);
2687
2688                                                 }
2689
2690                                                 reload = insert_reload(si, arg, insert_pos);
2691
2692                                                 set_irn_n(irn, i, reload);
2693
2694 #ifdef KEEPALIVE_RELOADS
2695                                                 pset_insert_ptr(si->spills, reload);
2696 #endif
2697                                         }
2698                                 }
2699                         }
2700                 }
2701         }
2702
2703         /* reloads at end of block */
2704         if(spill_bb->reloads) {
2705                 i=0;
2706                 live_foreach(bb, li) {
2707                         ir_node        *irn = (ir_node *) li->irn;
2708
2709                         if (live_is_end(li) && has_reg_class(si, irn) && !pset_find_ptr(si->all_possible_remats, irn)) {
2710                                 lpp_name_t    *name;
2711
2712                                 name = si->lpp->vars[spill_bb->reloads[i]];
2713                                 if(!is_zero(name->value)) {
2714                                         ir_node    *reload;
2715                                         ir_node    *insert_pos = bb;
2716                                         ir_node    *prev = sched_prev(insert_pos);
2717                                         op_t       *prev_op = get_irn_link(prev);
2718
2719                                         /* insert reload before pre-remats */
2720                                         while(!sched_is_end(prev) && !be_is_Reload(prev) && !be_is_Spill(prev)
2721                                                         && prev_op->is_remat && prev_op->attr.remat.pre) {
2722                                                 insert_pos = prev;
2723
2724                                                 prev = sched_prev(insert_pos);
2725                                                 prev_op = get_irn_link(prev);
2726                                         }
2727
2728                                         reload = insert_reload(si, irn, insert_pos);
2729
2730 #ifdef KEEPALIVE_RELOADS
2731                                         pset_insert_ptr(si->spills, reload);
2732 #endif
2733                                 }
2734                                 ++i;
2735                         }
2736                 }
2737         }
2738
2739         del_set(spill_bb->ilp);
2740 }
2741
2742 static void
2743 walker_collect_used(ir_node * irn, void * data)
2744 {
2745         lc_bitset_t   *used = data;
2746
2747         lc_bitset_set(used, get_irn_idx(irn));
2748 }
2749
2750 struct kill_helper {
2751         lc_bitset_t  *used;
2752         spill_ilp_t  *si;
2753 };
2754
2755 static void
2756 walker_kill_unused(ir_node * bb, void * data)
2757 {
2758         struct kill_helper *kh = data;
2759         const ir_node      *bad = get_irg_bad(get_irn_irg(bb));
2760         ir_node            *irn;
2761
2762
2763         for(irn=sched_first(bb); !sched_is_end(irn);) {
2764                 ir_node     *next = sched_next(irn);
2765                 int          i,
2766                                          n;
2767
2768                 if(!lc_bitset_is_set(kh->used, get_irn_idx(irn))) {
2769                         if(be_is_Spill(irn) || be_is_Reload(irn)) {
2770                                 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)));
2771                                 assert(lpp_get_sol_state(kh->si->lpp) != lpp_optimal && "optimal solution is suboptimal?");
2772                         }
2773
2774                         sched_remove(irn);
2775
2776                         set_nodes_block(irn, bad);
2777                         for (i = 0, n = get_irn_arity(irn); i < n; ++i) {
2778                                 set_irn_n(irn, i, bad);
2779                         }
2780                 }
2781                 irn = next;
2782         }
2783 }
2784
2785 static void
2786 kill_all_unused_values_in_schedule(spill_ilp_t * si)
2787 {
2788         struct kill_helper kh;
2789
2790         kh.used = lc_bitset_malloc(get_irg_last_idx(si->chordal_env->irg));
2791         kh.si = si;
2792
2793         irg_walk_graph(si->chordal_env->irg, walker_collect_used, NULL, kh.used);
2794         irg_block_walk_graph(si->chordal_env->irg, walker_kill_unused, NULL, &kh);
2795
2796         lc_bitset_free(kh.used);
2797 }
2798
2799 static void
2800 print_irn_pset(pset * p)
2801 {
2802         ir_node   *irn;
2803
2804         pset_foreach(p, irn) {
2805                 ir_printf("%+F\n", irn);
2806         }
2807 }
2808
2809 static void
2810 rewire_uses(spill_ilp_t * si)
2811 {
2812         dom_front_info_t     *dfi = be_compute_dominance_frontiers(si->chordal_env->irg);
2813         defs_t               *defs;
2814         pset                 *ignore = pset_new_ptr(1);
2815
2816         pset_insert_ptr(ignore, get_irg_end(si->chordal_env->irg));
2817
2818         /* then fix uses of spills */
2819         set_foreach(si->values, defs) {
2820                 pset     *reloads;
2821                 pset     *spills;
2822                 ir_node  *next = defs->remats;
2823                 int remats = 0;
2824
2825                 reloads = pset_new_ptr_default();
2826
2827                 while(next) {
2828                         if(be_is_Reload(next)) {
2829                                 pset_insert_ptr(reloads, next);
2830                         } else {
2831                                 ++remats;
2832                         }
2833                         next = get_irn_link(next);
2834                 }
2835
2836                 spills = get_spills_for_value(si, defs->value);
2837                 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));
2838                 if(pset_count(spills) > 1) {
2839                         //assert(pset_count(reloads) > 0);
2840                         //                              print_irn_pset(spills);
2841                         //                              print_irn_pset(reloads);
2842
2843                         be_ssa_constr_set_ignore(dfi, spills, ignore);
2844                 }
2845
2846                 del_pset(reloads);
2847                 del_pset(spills);
2848         }
2849
2850         /* first fix uses of remats and reloads */
2851         set_foreach(si->values, defs) {
2852                 pset     *nodes;
2853                 ir_node  *next = defs->remats;
2854
2855                 if(next) {
2856                         nodes = pset_new_ptr_default();
2857                         pset_insert_ptr(nodes, defs->value);
2858
2859                         while(next) {
2860                                 pset_insert_ptr(nodes, next);
2861                                 next = get_irn_link(next);
2862                         }
2863
2864                         if(pset_count(nodes) > 1) {
2865                                 DBG((si->dbg, LEVEL_4, "\t    %d new definitions for value %+F\n", pset_count(nodes)-1, defs->value));
2866                                 be_ssa_constr_set(dfi, nodes);
2867                         }
2868
2869                         del_pset(nodes);
2870                 }
2871         }
2872
2873 //      remove_unused_defs(si);
2874
2875         be_free_dominance_frontiers(dfi);
2876 }
2877
2878 static void
2879 writeback_results(spill_ilp_t * si)
2880 {
2881         /* walk through the graph and collect all spills, reloads and remats for a value */
2882
2883         si->values = new_set(cmp_defs, 4096);
2884
2885         DBG((si->dbg, LEVEL_1, "Applying results\n"));
2886         delete_unnecessary_remats(si);
2887         si->m_unknown = new_r_Unknown(si->chordal_env->irg, mode_M);
2888         irg_block_walk_graph(si->chordal_env->irg, walker_spill_placer, NULL, si);
2889         phim_fixer(si);
2890         irg_block_walk_graph(si->chordal_env->irg, walker_reload_placer, NULL, si);
2891
2892         /* clean the remat info! there are still back-edges leading there! */
2893         clean_remat_info(si);
2894
2895         rewire_uses(si);
2896
2897         connect_all_spills_with_keep(si);
2898
2899         del_set(si->values);
2900 }
2901
2902 static int
2903 get_n_regs(spill_ilp_t * si)
2904 {
2905         int     arch_n_regs = arch_register_class_n_regs(si->cls);
2906         int     free = 0;
2907         int     i;
2908
2909         for(i=0; i<arch_n_regs; i++) {
2910                 if(!arch_register_type_is(&si->cls->regs[i], ignore)) {
2911                         free++;
2912                 }
2913         }
2914
2915         DBG((si->dbg, LEVEL_1, "\tArchitecture has %d free registers in class %s\n", free, si->cls->name));
2916         return free;
2917 }
2918
2919 static void
2920 walker_reload_mover(ir_node * bb, void * data)
2921 {
2922         spill_ilp_t   *si = data;
2923         ir_node           *tmp;
2924
2925         sched_foreach(bb, tmp) {
2926                 if(be_is_Reload(tmp) && has_reg_class(si, tmp)) {
2927                         ir_node       *reload = tmp;
2928                         ir_node       *irn = tmp;
2929
2930                         /* move reload upwards */
2931
2932                         int pressure = (int)get_irn_link(reload);
2933                         if(pressure < si->n_regs) {
2934                                 irn = sched_prev(reload);
2935                                 DBG((si->dbg, LEVEL_5, "regpressure before %+F: %d\n", reload, pressure));
2936                                 sched_remove(reload);
2937                                 pressure = (int)get_irn_link(irn);
2938
2939                                 while(pressure < si->n_regs) {
2940                                         if(sched_is_end(irn) || (be_is_Reload(irn) && has_reg_class(si, irn))) break;
2941
2942                                         set_irn_link(irn, INT_TO_PTR(pressure+1));
2943                                         DBG((si->dbg, LEVEL_5, "new regpressure before %+F: %d\n", irn, pressure+1));
2944                                         irn = sched_prev(irn);
2945
2946                                         pressure = (int)get_irn_link(irn);
2947                                 }
2948
2949                                 DBG((si->dbg, LEVEL_3, "putting reload %+F after %+F\n", reload, irn));
2950                                 sched_put_after(irn, reload);
2951                         }
2952                 }
2953         }
2954 }
2955
2956 static void
2957 move_reloads_upward(spill_ilp_t * si)
2958 {
2959         irg_block_walk_graph(si->chordal_env->irg, walker_reload_mover, NULL, si);
2960 }
2961
2962 void
2963 be_spill_remat(const be_chordal_env_t * chordal_env)
2964 {
2965         char            problem_name[256];
2966         char            dump_suffix[256];
2967         char            dump_suffix2[256];
2968         char            dump_suffix3[256];
2969         struct obstack  obst;
2970         spill_ilp_t     si;
2971
2972         ir_snprintf(problem_name, sizeof(problem_name), "%F_%s", chordal_env->irg, chordal_env->cls->name);
2973         ir_snprintf(dump_suffix, sizeof(dump_suffix), "-%s-remats", chordal_env->cls->name);
2974         ir_snprintf(dump_suffix2, sizeof(dump_suffix2), "-%s-pressure", chordal_env->cls->name);
2975         ir_snprintf(dump_suffix3, sizeof(dump_suffix3), "-%s-reloads_moved", chordal_env->cls->name);
2976
2977         FIRM_DBG_REGISTER(si.dbg, "firm.be.ra.spillremat");
2978         DBG((si.dbg, LEVEL_1, "\n\n\t\t===== Processing %s =====\n\n", problem_name));
2979
2980         obstack_init(&obst);
2981         si.chordal_env = chordal_env;
2982         si.obst = &obst;
2983         si.senv = be_new_spill_env(chordal_env, is_mem_phi, &si);
2984         si.cls = chordal_env->cls;
2985         si.lpp = new_lpp(problem_name, lpp_minimize);
2986         si.remat_info = new_set(cmp_remat_info, 4096);
2987         si.all_possible_remats = pset_new_ptr_default();
2988         si.spills = pset_new_ptr_default();
2989         si.inverse_ops = pset_new_ptr_default();
2990 #ifndef EXECFREQ_LOOPDEPH
2991         si.execfreqs = compute_execfreq(chordal_env->irg);
2992 #else
2993         si.execfreqs = NULL;
2994 #endif
2995 #ifdef KEEPALIVE
2996         si.keep = NULL;
2997 #endif
2998         si.n_regs = get_n_regs(&si);
2999
3000         set_irg_link(chordal_env->irg, &si);
3001         compute_doms(chordal_env->irg);
3002
3003         be_analyze_regpressure(chordal_env, "-pre");
3004
3005 #ifdef COLLECT_REMATS
3006         /* collect remats */
3007         DBG((si.dbg, LEVEL_1, "Collecting remats\n"));
3008         irg_walk_graph(chordal_env->irg, walker_remat_collector, NULL, &si);
3009 #endif
3010
3011         /* insert possible remats */
3012         DBG((si.dbg, LEVEL_1, "Inserting possible remats\n"));
3013         irg_block_walk_graph(chordal_env->irg, walker_remat_insertor, NULL, &si);
3014         DBG((si.dbg, LEVEL_2, " -> inserted %d possible remats\n", pset_count(si.all_possible_remats)));
3015
3016 #ifdef KEEPALIVE
3017         DBG((si.dbg, LEVEL_1, "Connecting remats with keep and dumping\n"));
3018         connect_all_remats_with_keep(&si);
3019         /* dump graph with inserted remats */
3020         dump_graph_with_remats(chordal_env->irg, dump_suffix);
3021 #endif
3022
3023
3024         /* recompute liveness */
3025         DBG((si.dbg, LEVEL_1, "Recomputing liveness\n"));
3026         be_liveness(chordal_env->irg);
3027
3028         /* build the ILP */
3029
3030         DBG((si.dbg, LEVEL_1, "\tBuilding ILP\n"));
3031         DBG((si.dbg, LEVEL_2, "\t endwalker\n"));
3032         irg_block_walk_graph(chordal_env->irg, luke_endwalker, NULL, &si);
3033
3034         DBG((si.dbg, LEVEL_2, "\t blockwalker\n"));
3035         irg_block_walk_graph(chordal_env->irg, luke_blockwalker, NULL, &si);
3036
3037 #ifdef DUMP_ILP
3038         {
3039                 FILE           *f;
3040                 char            buf[256];
3041
3042                 ir_snprintf(buf, sizeof(buf), "%s-spillremat.ilp", problem_name);
3043                 if ((f = fopen(buf, "wt")) != NULL) {
3044                         lpp_dump_plain(si.lpp, f);
3045                         fclose(f);
3046                 }
3047         }
3048 #endif
3049
3050 #ifdef SOLVE
3051         DBG((si.dbg, LEVEL_1, "\tSolving %F\n", chordal_env->irg));
3052 #ifdef ILP_TIMEOUT
3053         lpp_set_time_limit(si.lpp, ILP_TIMEOUT);
3054 #endif
3055
3056 #ifdef SOLVE_LOCAL
3057         lpp_solve_cplex(si.lpp);
3058 #else
3059         lpp_solve_net(si.lpp, LPP_SERVER, LPP_SOLVER);
3060 #endif
3061         assert(lpp_is_sol_valid(si.lpp)
3062                && "solution of ILP must be valid");
3063
3064         DBG((si.dbg, LEVEL_1, "\t%s: iterations: %d, solution time: %g, objective function: %g\n", problem_name, si.lpp->iterations, si.lpp->sol_time, is_zero(si.lpp->objval)?0.0:si.lpp->objval));
3065
3066 #ifdef DUMP_SOLUTION
3067         {
3068                 FILE           *f;
3069                 char            buf[256];
3070
3071                 ir_snprintf(buf, sizeof(buf), "%s-spillremat.sol", problem_name);
3072                 if ((f = fopen(buf, "wt")) != NULL) {
3073                         int             i;
3074                         for (i = 0; i < si.lpp->var_next; ++i) {
3075                                 lpp_name_t     *name = si.lpp->vars[i];
3076                                 fprintf(f, "%20s %4d %10f\n", name->name, name->nr, name->value);
3077                         }
3078                         fclose(f);
3079                 }
3080         }
3081 #endif
3082
3083         writeback_results(&si);
3084
3085 #endif                          /* SOLVE */
3086
3087         kill_all_unused_values_in_schedule(&si);
3088
3089 #if defined(KEEPALIVE_SPILLS) || defined(KEEPALIVE_RELOADS)
3090         be_dump(chordal_env->irg, "-spills-placed", dump_ir_block_graph);
3091 #endif
3092
3093         be_liveness(chordal_env->irg);
3094         irg_block_walk_graph(chordal_env->irg, walker_pressure_annotator, NULL, &si);
3095
3096         dump_pressure_graph(&si, dump_suffix2);
3097
3098         // TODO fix temporarily exceeded regpressure due to remat2s
3099
3100         // TODO insert copys to fix interferences in memory
3101
3102         // move reloads upwards
3103         move_reloads_upward(&si);
3104         irg_block_walk_graph(chordal_env->irg, walker_pressure_annotator, NULL, &si);
3105         dump_pressure_graph(&si, dump_suffix3);
3106
3107         be_analyze_regpressure(chordal_env, "-post");
3108
3109         free_dom(chordal_env->irg);
3110         del_pset(si.inverse_ops);
3111         del_pset(si.all_possible_remats);
3112         del_pset(si.spills);
3113 #ifndef EXECFREQ_LOOPDEPH
3114         free_execfreq(si.execfreqs);
3115 #endif
3116         free_lpp(si.lpp);
3117         obstack_free(&obst, NULL);
3118 //      exit(0);
3119 }
3120
3121 #else                           /* WITH_ILP */
3122
3123 static void
3124 only_that_you_can_compile_without_WITH_ILP_defined(void)
3125 {
3126 }
3127
3128 #endif                          /* WITH_ILP */