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