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