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