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