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