seems to spill again a bit
[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 /**
855  * Insert (so far unused) remats into the irg to
856  * recompute the potential liveness of all values
857  */
858 static void
859 walker_remat_insertor(ir_node * bb, void * data)
860 {
861         spill_ilp_t    *si = data;
862         spill_bb_t     *spill_bb;
863         ir_node        *irn;
864         int             n;
865         irn_live_t     *li;
866         pset           *live = pset_new_ptr_default();
867
868         DBG((si->dbg, LEVEL_3, "\t Entering %+F\n\n", bb));
869
870         live_foreach(bb, li) {
871                 ir_node        *value = (ir_node *) li->irn;
872
873                 /* add remats at end of block */
874                 if (live_is_end(li) && has_reg_class(si, value)) {
875                         pset_insert_ptr(live, value);
876                 }
877         }
878
879         spill_bb = obstack_alloc(si->obst, sizeof(*spill_bb));
880         set_irn_link(bb, spill_bb);
881
882         irn = sched_last(bb);
883         while(!sched_is_end(irn)) {
884                 ir_node   *next;
885                 op_t      *op;
886                 pset      *args;
887                 ir_node   *arg;
888
889                 next = sched_prev(irn);
890
891                 DBG((si->dbg, LEVEL_5, "\t at %+F (next: %+F)\n", irn, next));
892
893                 if(is_Phi(irn) || is_Proj(irn)) {
894                         op_t      *op;
895
896                         if(has_reg_class(si, irn)) {
897                                 pset_remove_ptr(live, irn);
898                         }
899
900                         op = obstack_alloc(si->obst, sizeof(*op));
901                         op->is_remat = 0;
902                         op->attr.live_range.args.reloads = NULL;
903                         op->attr.live_range.ilp = ILP_UNDEF;
904                         set_irn_link(irn, op);
905
906                         irn = next;
907                         continue;
908                 }
909
910                 op = obstack_alloc(si->obst, sizeof(*op));
911                 op->is_remat = 0;
912                 op->attr.live_range.ilp = ILP_UNDEF;
913                 op->attr.live_range.args.reloads = obstack_alloc(si->obst, sizeof(*op->attr.live_range.args.reloads) * get_irn_arity(irn));
914                 memset(op->attr.live_range.args.reloads, 0xFF, sizeof(*op->attr.live_range.args.reloads) * get_irn_arity(irn));
915                 set_irn_link(irn, op);
916
917                 args = pset_new_ptr_default();
918
919                 /* collect arguments of op */
920                 for (n = get_irn_arity(irn)-1; n>=0; --n) {
921                         ir_node        *arg = get_irn_n(irn, n);
922
923                         pset_insert_ptr(args, arg);
924                 }
925
926                 /* set args of op already live in epilog */
927                 pset_foreach(args, arg) {
928                         if(has_reg_class(si, arg)) {
929                                 pset_insert_ptr(live, arg);
930                         }
931                 }
932
933                 /* insert all possible remats after irn */
934                 pset_foreach(args, arg) {
935                         remat_info_t   *remat_info,
936                                                     query;
937                         remat_t        *remat;
938
939                         /* continue if the operand has the wrong reg class */
940                         if(!has_reg_class(si, arg))
941                                 continue;
942
943                         query.irn = arg;
944                         query.remats = NULL;
945                         query.remats_by_operand = NULL;
946                         remat_info = set_find(si->remat_info, &query, sizeof(query), HASH_PTR(arg));
947
948                         if(!remat_info) {
949                                 continue;
950                         }
951
952                         /* do not place post remats after jumps */
953                         if(sched_skip_cf_predicator(irn, si->chordal_env->birg->main_env->arch_env)) continue;
954
955                         if(remat_info->remats_by_operand) {
956                                 pset_foreach(remat_info->remats_by_operand, remat) {
957                                         /* do not insert remats producing the same value as one of the operands */
958                                         if(!pset_find_ptr(args, remat->value)) {
959                                                 DBG((si->dbg, LEVEL_4, "\t  considering remat %+F with arg %+F\n", remat->op, arg));
960 #ifdef REMAT_WHILE_LIVE
961                                                 if(pset_find_ptr(live, remat->value)) {
962                                                         insert_remat_after(si, remat, irn, live);
963                                                 }
964 #else
965                                                 insert_remat_after(si, remat, irn, live);
966 #endif
967                                         }
968                                 }
969                         }
970                 }
971
972                 /* delete defined value from live set */
973                 if(has_reg_class(si, irn)) {
974                         pset_remove_ptr(live, irn);
975                 }
976
977                 /* insert all possible remats before irn */
978                 pset_foreach(args, arg) {
979                         remat_info_t   *remat_info,
980                                                     query;
981                         remat_t        *remat;
982
983                         /* continue if the operand has the wrong reg class
984                          */
985                         if(!has_reg_class(si, arg))
986                                 continue;
987
988                         query.irn = arg;
989                         query.remats = NULL;
990                         query.remats_by_operand = NULL;
991                         remat_info = set_find(si->remat_info, &query, sizeof(query), HASH_PTR(arg));
992
993                         if(!remat_info) {
994                                 continue;
995                         }
996
997                         if(remat_info->remats) {
998                                 pset_foreach(remat_info->remats, remat) {
999                                         DBG((si->dbg, LEVEL_4, "\t  considering remat %+F for arg %+F\n", remat->op, arg));
1000 #ifdef REMAT_WHILE_LIVE
1001                                         if(pset_find_ptr(live, remat->value)) {
1002                                                 insert_remat_before(si, remat, irn, live);
1003                                         }
1004 #else
1005                                         insert_remat_before(si, remat, irn, live);
1006 #endif
1007                                 }
1008                         }
1009                 }
1010
1011                 del_pset(args);
1012                 irn = next;
1013         }
1014
1015         live_foreach(bb, li) {
1016                 ir_node        *value = (ir_node *) li->irn;
1017
1018                 /* add remats at end if successor has multiple predecessors */
1019                 if(is_merge_edge(bb)) {
1020                         /* add remats at end of block */
1021                         if (live_is_end(li) && has_reg_class(si, value)) {
1022                                 remat_info_t   *remat_info,
1023                                                            query;
1024                                 remat_t        *remat;
1025
1026                                 query.irn = value;
1027                                 query.remats = NULL;
1028                                 query.remats_by_operand = NULL;
1029                                 remat_info = set_find(si->remat_info, &query, sizeof(query), HASH_PTR(value));
1030
1031                                 if(remat_info && remat_info->remats) {
1032                                         pset_foreach(remat_info->remats, remat) {
1033                                                 DBG((si->dbg, LEVEL_4, "\t  considering remat %+F at end of block %+F\n", remat->op, bb));
1034
1035                                                 insert_remat_before(si, remat, bb, NULL);
1036                                         }
1037                                 }
1038                         }
1039                 }
1040                 if(is_diverge_edge(bb)) {
1041                         /* add remat2s at beginning of block */
1042                         if ((live_is_in(li) || (is_Phi(value) && get_nodes_block(value)==bb)) && has_reg_class(si, value)) {
1043                                 remat_info_t   *remat_info,
1044                                                            query;
1045                                 remat_t        *remat;
1046
1047                                 query.irn = value;
1048                                 query.remats = NULL;
1049                                 query.remats_by_operand = NULL;
1050                                 remat_info = set_find(si->remat_info, &query, sizeof(query), HASH_PTR(value));
1051
1052                                 if(remat_info && remat_info->remats) {
1053                                         pset_foreach(remat_info->remats, remat) {
1054                                                 DBG((si->dbg, LEVEL_4, "\t  considering remat %+F at beginning of block %+F\n", remat->op, bb));
1055
1056                                                 /* put the remat here if all its args are available */
1057                                                 insert_remat_after(si, remat, bb, NULL);
1058
1059                                         }
1060                                 }
1061                         }
1062                 }
1063         }
1064 }
1065
1066 /**
1067  * Preparation of blocks' ends for Luke Blockwalker(tm)(R)
1068  */
1069 static void
1070 luke_endwalker(ir_node * bb, void * data)
1071 {
1072         spill_ilp_t    *si = (spill_ilp_t*)data;
1073         irn_live_t     *li;
1074         pset           *live;
1075         pset           *use_end;
1076         char            buf[256];
1077         ilp_cst_t       cst;
1078         ir_node        *irn;
1079         spill_bb_t     *spill_bb = get_irn_link(bb);
1080
1081
1082         live = pset_new_ptr_default();
1083         use_end = pset_new_ptr_default();
1084
1085         live_foreach(bb, li) {
1086                 irn = (ir_node *) li->irn;
1087                 if (live_is_end(li) && has_reg_class(si, irn) && !pset_find_ptr(si->all_possible_remats, irn)) {
1088                         op_t      *op;
1089
1090                         pset_insert_ptr(live, irn);
1091                         op = get_irn_link(irn);
1092                         assert(!op->is_remat);
1093                 }
1094         }
1095
1096         /* collect values used by cond jumps etc. at bb end (use_end) -> always live */
1097         /* their reg_out must always be set */
1098         sched_foreach_reverse(bb, irn) {
1099                 int   n;
1100
1101                 if(!sched_skip_cf_predicator(irn, si->chordal_env->birg->main_env->arch_env)) break;
1102
1103                 for (n=get_irn_arity(irn)-1; n>=0; --n) {
1104                         ir_node        *irn_arg = get_irn_n(irn, n);
1105
1106                         if(has_reg_class(si, irn_arg)) {
1107                                 pset_insert_ptr(use_end, irn_arg);
1108                         }
1109                 }
1110         }
1111
1112         ir_snprintf(buf, sizeof(buf), "check_end_%N", bb);
1113         //cst = lpp_add_cst(si->lpp, buf, lpp_less, si->n_regs);
1114         cst = lpp_add_cst(si->lpp, buf, lpp_less, si->n_regs - pset_count(use_end));
1115
1116         spill_bb->ilp = new_set(cmp_spill, pset_count(live)+pset_count(use_end));
1117
1118         pset_foreach(live,irn) {
1119                 spill_t     query,
1120                                         *spill;
1121                 double      spill_cost;
1122
1123
1124                 /* handle values used by control flow nodes later separately */
1125                 if(pset_find_ptr(use_end, irn)) continue;
1126
1127                 query.irn = irn;
1128                 spill = set_insert(spill_bb->ilp, &query, sizeof(query), HASH_PTR(irn));
1129
1130                 spill_cost = is_Unknown(irn)?0.0001:COST_STORE*execution_frequency(si, bb);
1131
1132                 ir_snprintf(buf, sizeof(buf), "reg_out_%N_%N", irn, bb);
1133                 spill->reg_out = lpp_add_var(si->lpp, buf, lpp_binary, 0.0);
1134                 lpp_set_factor_fast(si->lpp, cst, spill->reg_out, 1.0);
1135
1136                 ir_snprintf(buf, sizeof(buf), "mem_out_%N_%N", irn, bb);
1137                 spill->mem_out = lpp_add_var(si->lpp, buf, lpp_binary, 0.0);
1138
1139                 ir_snprintf(buf, sizeof(buf), "spill_%N_%N", irn, bb);
1140                 spill->spill = lpp_add_var(si->lpp, buf, lpp_binary, spill_cost);
1141
1142                 spill->reg_in = ILP_UNDEF;
1143                 spill->mem_in = ILP_UNDEF;
1144         }
1145
1146         pset_foreach(use_end,irn) {
1147                 spill_t     query,
1148                                         *spill;
1149                 double      spill_cost;
1150                 ilp_cst_t   end_use_req;
1151
1152                 query.irn = irn;
1153                 spill = set_insert(spill_bb->ilp, &query, sizeof(query), HASH_PTR(irn));
1154
1155                 spill_cost = is_Unknown(irn)?0.0001:COST_STORE*execution_frequency(si, bb);
1156
1157                 ir_snprintf(buf, sizeof(buf), "reg_out_%N_%N", irn, bb);
1158                 spill->reg_out = lpp_add_var(si->lpp, buf, lpp_binary, 0.0);
1159                 /* if irn is used at the end of the block, then it is live anyway */
1160                 //lpp_set_factor_fast(si->lpp, cst, spill->reg_out, 1.0);
1161
1162                 ir_snprintf(buf, sizeof(buf), "mem_out_%N_%N", irn, bb);
1163                 spill->mem_out = lpp_add_var(si->lpp, buf, lpp_binary, 0.0);
1164
1165                 ir_snprintf(buf, sizeof(buf), "spill_%N_%N", irn, bb);
1166                 spill->spill = lpp_add_var(si->lpp, buf, lpp_binary, spill_cost);
1167
1168                 spill->reg_in = ILP_UNDEF;
1169                 spill->mem_in = ILP_UNDEF;
1170
1171                 ir_snprintf(buf, sizeof(buf), "req_cf_end_%N_%N", irn, bb);
1172                 end_use_req = lpp_add_cst(si->lpp, buf, lpp_equal, 1);
1173                 lpp_set_factor_fast(si->lpp, end_use_req, spill->reg_out, 1.0);
1174         }
1175
1176         del_pset(live);
1177         del_pset(use_end);
1178 }
1179
1180 static ir_node *
1181 next_post_remat(const ir_node * irn)
1182 {
1183         op_t      *op;
1184
1185         if(is_Block(irn)) {
1186                 irn = sched_block_first_nonphi(irn);
1187         } else {
1188                 irn = sched_next_op(irn);
1189         }
1190
1191         if(sched_is_end(irn))
1192                 return NULL;
1193
1194         op = (op_t*)get_irn_link(irn);
1195         if(op->is_remat && !op->attr.remat.pre) {
1196                 return irn;
1197         }
1198
1199         return NULL;
1200 }
1201
1202
1203 static ir_node *
1204 next_pre_remat(const spill_ilp_t * si, const ir_node * irn)
1205 {
1206         op_t      *op;
1207         ir_node   *ret;
1208
1209         if(is_Block(irn)) {
1210                 ret = sched_block_last_noncf(si, irn);
1211                 ret = sched_next(ret);
1212                 ret = sched_prev_op(ret);
1213         } else {
1214                 ret = sched_prev_op(irn);
1215         }
1216
1217         if(sched_is_end(ret) || is_Phi(ret))
1218                 return NULL;
1219
1220         op = (op_t*)get_irn_link(ret);
1221         if(op->is_remat && op->attr.remat.pre) {
1222                 return ret;
1223         }
1224
1225         return NULL;
1226 }
1227
1228 /**
1229  * Find a remat of value @p value in the epilog of @p pos
1230  */
1231 static ir_node *
1232 find_post_remat(const ir_node * value, const ir_node * pos)
1233 {
1234         while((pos = next_post_remat(pos)) != NULL) {
1235                 op_t   *op;
1236
1237                 op = get_irn_link(pos);
1238                 assert(op->is_remat && !op->attr.remat.pre);
1239
1240                 if(op->attr.remat.remat->value == value)
1241                         return (ir_node*)pos;
1242
1243 #if 0
1244         const ir_edge_t *edge;
1245                 foreach_out_edge(pos, edge) {
1246                         ir_node   *proj = get_edge_src_irn(edge);
1247                         assert(is_Proj(proj));
1248                 }
1249 #endif
1250
1251         }
1252
1253         return NULL;
1254 }
1255
1256 static spill_t *
1257 add_to_spill_bb(spill_ilp_t * si, ir_node * bb, ir_node * irn)
1258 {
1259         spill_bb_t  *spill_bb = get_irn_link(bb);
1260         spill_t     *spill,
1261                                  query;
1262         char         buf[256];
1263
1264         query.irn = irn;
1265         spill = set_find(spill_bb->ilp, &query, sizeof(query), HASH_PTR(irn));
1266         if(!spill) {
1267                 double   spill_cost = is_Unknown(irn)?0.0001:COST_STORE*execution_frequency(si, bb);
1268
1269                 spill = set_insert(spill_bb->ilp, &query, sizeof(query), HASH_PTR(irn));
1270
1271                 spill->reg_out = ILP_UNDEF;
1272                 spill->reg_in  = ILP_UNDEF;
1273                 spill->mem_in  = ILP_UNDEF;
1274
1275                 ir_snprintf(buf, sizeof(buf), "mem_out_%N_%N", irn, bb);
1276                 spill->mem_out = lpp_add_var(si->lpp, buf, lpp_binary, 0.0);
1277
1278                 ir_snprintf(buf, sizeof(buf), "spill_%N_%N", irn, bb);
1279                 spill->spill = lpp_add_var(si->lpp, buf, lpp_binary, spill_cost);
1280         }
1281
1282         return spill;
1283 }
1284
1285 static void
1286 get_live_end(spill_ilp_t * si, ir_node * bb, pset * live)
1287 {
1288         irn_live_t     *li;
1289         ir_node        *irn;
1290
1291         live_foreach(bb, li) {
1292                 irn = (ir_node *) li->irn;
1293
1294                 if (live_is_end(li) && has_reg_class(si, irn) && !pset_find_ptr(si->all_possible_remats, irn)) {
1295                         pset_insert_ptr(live, irn);
1296                 }
1297         }
1298
1299         irn = sched_last(bb);
1300
1301         /* all values eaten by control flow operations are also live until the end of the block */
1302         sched_foreach_reverse(bb, irn) {
1303                 int  i;
1304
1305                 if(!sched_skip_cf_predicator(irn, si->chordal_env->birg->main_env->arch_env)) break;
1306
1307                 for(i=get_irn_arity(irn)-1; i>=0; --i) {
1308                         ir_node *arg = get_irn_n(irn,i);
1309
1310                         if(has_reg_class(si, arg)) {
1311                                 pset_insert_ptr(live, arg);
1312                         }
1313                 }
1314         }
1315 }
1316
1317 /**
1318  * Walk all irg blocks and emit this ILP
1319  */
1320 static void
1321 luke_blockwalker(ir_node * bb, void * data)
1322 {
1323         spill_ilp_t    *si = (spill_ilp_t*)data;
1324         ir_node        *irn;
1325         pset           *live;
1326         char            buf[256];
1327         ilp_cst_t       cst;
1328         spill_bb_t     *spill_bb = get_irn_link(bb);
1329         int             i;
1330         ir_node        *tmp;
1331         spill_t        *spill;
1332         irn_live_t     *li;
1333         pset           *defs = pset_new_ptr_default();
1334
1335
1336         live = pset_new_ptr_default();
1337
1338         /****************************************
1339          *      B A S I C  B L O C K  E N D
1340          ***************************************/
1341
1342
1343         /* init live values at end of block */
1344         get_live_end(si, bb, live);
1345
1346
1347         /* if this is a merge edge we can reload at the end of this block */
1348         if(is_merge_edge(bb)) {
1349                 spill_bb->reloads = new_set(cmp_keyval, pset_count(live));
1350         } else {
1351                 spill_bb->reloads = NULL;
1352         }
1353
1354         pset_foreach(live, irn) {
1355                 op_t           *op;
1356                 ilp_var_t       reload;
1357
1358                 spill = set_find_spill(spill_bb->ilp, irn);
1359                 assert(spill);
1360
1361                 if(spill_bb->reloads) {
1362                         ir_snprintf(buf, sizeof(buf), "reload_%N_%N", bb, irn);
1363                         reload = lpp_add_var(si->lpp, buf, lpp_binary, COST_LOAD*execution_frequency(si, bb));
1364
1365                         set_insert_keyval(spill_bb->reloads, irn, INT_TO_PTR(reload));
1366
1367                         /* reload <= mem_out */
1368                         cst = lpp_add_cst(si->lpp, buf, lpp_less, 0.0);
1369                         lpp_set_factor_fast(si->lpp, cst, reload, 1.0);
1370                         lpp_set_factor_fast(si->lpp, cst, spill->mem_out, -1.0);
1371                 }
1372
1373                 op = get_irn_link(irn);
1374                 assert(!op->is_remat);
1375
1376                 ir_snprintf(buf, sizeof(buf), "lr_%N_%N", irn, bb);
1377                 op->attr.live_range.ilp = lpp_add_var(si->lpp, buf, lpp_binary, 0.0);
1378                 op->attr.live_range.op = bb;
1379
1380                 ir_snprintf(buf, sizeof(buf), "reg_out_%N_%N", bb, irn);
1381                 cst = lpp_add_cst(si->lpp, buf, lpp_less, 0.0);
1382
1383                 /* reg_out - reload - remat - live_range <= 0 */
1384                 lpp_set_factor_fast(si->lpp, cst, spill->reg_out, 1.0);
1385                 if(spill_bb->reloads) lpp_set_factor_fast(si->lpp, cst, reload, -1.0);
1386                 lpp_set_factor_fast(si->lpp, cst, op->attr.live_range.ilp, -1.0);
1387                 foreach_pre_remat(si, bb, tmp) {
1388                         op_t     *remat_op = get_irn_link(tmp);
1389                         if(remat_op->attr.remat.remat->value == irn) {
1390                                 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, -1.0);
1391                         }
1392                 }
1393                 /* maybe we should also assure that reg_out >= live_range etc. */
1394         }
1395
1396         /* start new live ranges for values used by remats at end of block */
1397         foreach_pre_remat(si, bb, tmp) {
1398                 int       n;
1399
1400                 for (n=get_irn_arity(tmp)-1; n>=0; --n) {
1401                         ir_node        *remat_arg = get_irn_n(tmp, n);
1402                         op_t           *arg_op = get_irn_link(remat_arg);
1403                         ilp_var_t       prev_lr;
1404
1405                         if(!has_reg_class(si, remat_arg)) continue;
1406
1407                         /* if value is becoming live through use by remat */
1408                         if(!pset_find_ptr(live, remat_arg)) {
1409                                 ir_snprintf(buf, sizeof(buf), "lr_%N_begin%N", remat_arg, bb);
1410                                 prev_lr = lpp_add_var(si->lpp, buf, lpp_binary, 0.0);
1411
1412                                 arg_op->attr.live_range.ilp = prev_lr;
1413                                 arg_op->attr.live_range.op = bb;
1414
1415                                 DBG((si->dbg, LEVEL_4, "  value %+F becoming live through use by remat at end of block %+F\n", remat_arg, tmp));
1416
1417                                 pset_insert_ptr(live, remat_arg);
1418                                 add_to_spill_bb(si, bb, remat_arg);
1419                         }
1420                 }
1421         }
1422
1423         DBG((si->dbg, LEVEL_4, "\t   %d values live at end of block %+F\n", pset_count(live), bb));
1424
1425
1426
1427
1428         /**************************************
1429          *    B A S I C  B L O C K  B O D Y
1430          **************************************/
1431
1432         sched_foreach_reverse_from(sched_block_last_noncf(si, bb), irn) {
1433                 op_t       *op;
1434                 op_t       *tmp_op;
1435                 int         n,
1436                                         u = 0,
1437                                         d = 0;
1438                 ilp_cst_t       check_pre,
1439                                         check_post;
1440 #ifdef CHECK_POST_REMAT
1441                 ilp_cst_t       check_post_remat;
1442 #endif
1443                 set        *args;
1444                 keyval_t   *keyval;
1445
1446                 /* iterate only until first phi */
1447                 if(is_Phi(irn))
1448                         break;
1449
1450                 op = get_irn_link(irn);
1451                 /* skip remats */
1452                 if(op->is_remat) continue;
1453                 DBG((si->dbg, LEVEL_4, "\t  at node %+F\n", irn));
1454
1455                 /* collect defined values */
1456                 if(has_reg_class(si, irn)) {
1457                         pset_insert_ptr(defs, irn);
1458                 }
1459
1460                 /* skip projs */
1461                 if(is_Proj(irn)) continue;
1462
1463 #if 0
1464                 /* remove defined values from live set */
1465                 if(has_reg_class(si, irn)) {
1466                         assert(pset_find_ptr(live, irn));
1467                         pset_remove_ptr(live, irn);
1468                 }
1469 #endif
1470
1471                 /* init set of irn's arguments */
1472                 args =  new_set(cmp_keyval, get_irn_arity(irn));
1473                 for (n=get_irn_arity(irn)-1; n>=0; --n) {
1474                         ir_node        *irn_arg = get_irn_n(irn, n);
1475                         if(has_reg_class(si, irn_arg)) {
1476                                 set_insert_keyval(args, irn_arg, (void*)n);
1477                         }
1478                 }
1479
1480                 /**********************************
1481                  *   I N  E P I L O G  O F  irn
1482                  **********************************/
1483
1484                 /* new live ranges for values from L\U defined by post remats */
1485                 pset_foreach(live, tmp) {
1486                         ir_node     *value = tmp;//remat_op->attr.remat.remat->value;
1487                         op_t        *value_op = get_irn_link(value);
1488
1489                         if(!set_find_keyval(args, value) && !pset_find_ptr(defs, value)) {
1490                                 ilp_var_t    prev_lr = ILP_UNDEF;
1491                                 ir_node     *remat;
1492                                 cst = ILP_UNDEF;
1493
1494                                 foreach_post_remat(irn, remat) {
1495                                         op_t        *remat_op = get_irn_link(remat);
1496
1497                                         /* if value is being rematerialized by this remat */
1498                                         if(value == remat_op->attr.remat.remat->value) {
1499                                                 if(cst == ILP_UNDEF) {
1500                                                         /* next_live_range <= prev_live_range + sum remat2s */
1501                                                         ir_snprintf(buf, sizeof(buf), "next_lr_%N_%N", value, irn);
1502                                                         cst = lpp_add_cst(si->lpp, buf, lpp_less, 0.0);
1503
1504                                                         ir_snprintf(buf, sizeof(buf), "lr_%N_%N", value, irn);
1505                                                         prev_lr = lpp_add_var(si->lpp, buf, lpp_binary, 0.0);
1506
1507                                                         lpp_set_factor_fast(si->lpp, cst, value_op->attr.live_range.ilp, 1.0);
1508                                                         lpp_set_factor_fast(si->lpp, cst, prev_lr, -1.0);
1509                                                 }
1510
1511                                                 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, -1.0);
1512                                         }
1513                                 }
1514
1515                                 if(prev_lr != ILP_UNDEF) {
1516                                         value_op->attr.live_range.ilp = prev_lr;
1517                                         value_op->attr.live_range.op = irn;
1518                                 }
1519                         }
1520                 }
1521
1522                 /* requirements for post remats and start live ranges from L/U for values dying here */
1523                 foreach_post_remat(irn, tmp) {
1524                         op_t        *remat_op = get_irn_link(tmp);
1525                         int          n;
1526
1527                         for (n=get_irn_arity(tmp)-1; n>=0; --n) {
1528                                 ir_node        *remat_arg = get_irn_n(tmp, n);
1529                                 op_t           *arg_op = get_irn_link(remat_arg);
1530
1531                                 if(!has_reg_class(si, remat_arg)) continue;
1532
1533                                 /* only for values in L\U (TODO and D?), the others are handled with post_use */
1534                                 if(!set_find_keyval(args, remat_arg)) {
1535                                         /* remat <= live_rang(remat_arg) */
1536                                         ir_snprintf(buf, sizeof(buf), "req_remat2_%N_arg_%N", tmp, remat_arg);
1537                                         cst = lpp_add_cst(si->lpp, buf, lpp_less, 0.0);
1538
1539                                         /* if value is becoming live through use by remat2 */
1540                                         if(!pset_find_ptr(live, remat_arg)) {
1541                                                 ilp_var_t     lr;
1542
1543                                                 ir_snprintf(buf, sizeof(buf), "lr_%N_%N", remat_arg, irn);
1544                                                 lr = lpp_add_var(si->lpp, buf, lpp_binary, 0.0);
1545
1546                                                 arg_op->attr.live_range.ilp = lr;
1547                                                 arg_op->attr.live_range.op = irn;
1548
1549                                                 DBG((si->dbg, LEVEL_3, "  value %+F becoming live through use by remat2 %+F\n", remat_arg, tmp));
1550
1551                                                 pset_insert_ptr(live, remat_arg);
1552                                                 add_to_spill_bb(si, bb, remat_arg);
1553                                         }
1554
1555                                         lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, 1.0);
1556                                         lpp_set_factor_fast(si->lpp, cst, arg_op->attr.live_range.ilp, -1.0);
1557                                 }
1558                         }
1559                 }
1560
1561 #ifdef CHECK_POST_REMAT
1562                 /* check the register pressure after the epilog */
1563                 ir_snprintf(buf, sizeof(buf), "check_post_remat_%N", irn);
1564                 check_post_remat = lpp_add_cst(si->lpp, buf, lpp_less, si->n_regs);
1565
1566                 /* iterate over L\U */
1567                 pset_foreach(live, tmp) {
1568                         if(!set_find_keyval(args, tmp)) {
1569                                 /* if a live value is not used by irn */
1570                                 tmp_op = get_irn_link(tmp);
1571                                 lpp_set_factor_fast(si->lpp, check_post_remat, tmp_op->attr.live_range.ilp, 1.0);
1572                         }
1573                 }
1574                 /* iterate over remats in epilog and remove possibly defined values again from check_post_remat */
1575                 foreach_post_remat(irn, tmp) {
1576                         op_t           *remat_op = get_irn_link(tmp);
1577                         const ir_node  *value = remat_op->attr.remat.remat->value;
1578                         op_t           *val_op = get_irn_link(value);
1579
1580                         assert(remat_op->is_remat && !remat_op->attr.remat.pre);
1581
1582                         /* values that are defined by remats are not counted */
1583 #ifdef REMAT_WHILE_LIVE
1584                         assert(val_op->attr.live_range.ilp);
1585                         lpp_set_factor_fast(si->lpp, check_post_remat, val_op->attr.live_range.ilp, 0.0);
1586 #else
1587                         if(val_op->attr.live_range.ilp != ILP_UNDEF) {
1588                                 lpp_set_factor_fast(si->lpp, check_post_remat, val_op->attr.live_range.ilp, 0.0);
1589                         }
1590 #endif /* REMAT_WHILE_LIVE */
1591
1592                         /* instead add these post remats to the check */
1593                         lpp_set_factor_fast(si->lpp, check_post_remat, remat_op->attr.remat.ilp, 1.0);
1594                 }
1595
1596 #if 0 /* defs are still in live */
1597                 /* get count of values in my register class defined by irn */
1598                 /* also add defined values to check_post_remat; do this before iterating over args */
1599                 pset_foreach(defs,tmp) {
1600                         op_t     *def_op = get_irn_link(tmp);
1601
1602                         lpp_set_factor_fast(si->lpp, check_post_remat, def_op->attr.live_range.ilp, 1.0);
1603                 }
1604 #endif
1605 #endif /* CHECK_POST_REMAT */
1606
1607
1608                 d = pset_count(defs);
1609                 DBG((si->dbg, LEVEL_4, "\t   %+F produces %d values in my register class\n", irn, d));
1610
1611                 /* count how many regs irn needs for arguments */
1612                 u = set_count(args);
1613
1614
1615                 /* check the register pressure in the epilog */
1616                 /* sum_{L\U} lr + sum_{U} post_use <= k - |D| */
1617                 ir_snprintf(buf, sizeof(buf), "check_post_%N", irn);
1618                 check_post = lpp_add_cst(si->lpp, buf, lpp_less, si->n_regs - d);
1619
1620                 /* add L\U to check_post */
1621                 pset_foreach(live, tmp) {
1622                         if(!set_find_keyval(args, tmp) && !pset_find_ptr(defs, tmp)) {
1623                                 /* if a live value is not used by irn */
1624                                 tmp_op = get_irn_link(tmp);
1625                                 lpp_set_factor_fast(si->lpp, check_post, tmp_op->attr.live_range.ilp, 1.0);
1626                         }
1627                 }
1628
1629                 /***********************************************************
1630                  *  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
1631                  **********************************************************/
1632
1633
1634                 set_foreach(args, keyval) {
1635                         ilp_var_t       next_lr;
1636                         ilp_var_t       post_use;
1637                         int             p = 0;
1638                         spill_t        *spill;
1639                         ir_node        *arg = keyval->key;
1640                         op_t           *arg_op = get_irn_link(arg);
1641
1642                         spill = add_to_spill_bb(si, bb, arg);
1643
1644                         /* new live range for each argument */
1645                         ir_snprintf(buf, sizeof(buf), "lr_%N_%N", arg, irn);
1646                         next_lr = lpp_add_var(si->lpp, buf, lpp_binary, 0.0);
1647
1648                         i = (int)keyval->val;
1649
1650                         /* the epilog stuff - including post_use, check_post, check_post_remat */
1651                         ir_snprintf(buf, sizeof(buf), "post_use_%N_%N", arg, irn);
1652                         post_use = lpp_add_var(si->lpp, buf, lpp_binary, 0.0);
1653
1654                         lpp_set_factor_fast(si->lpp, check_post, post_use, 1.0);
1655
1656                         /* arg is live throughout epilog if the next live_range is in a register */
1657                         if(pset_find_ptr(live, arg)) {
1658                                 DBG((si->dbg, LEVEL_3, "\t  arg %+F is possibly live in epilog of %+F\n", arg, irn));
1659
1660                                 ir_snprintf(buf, sizeof(buf), "post_use_%N_%N-%d", arg, irn, p++);
1661                                 cst = lpp_add_cst(si->lpp, buf, lpp_less, 0.0);
1662                                 lpp_set_factor_fast(si->lpp, cst, post_use, -1.0);
1663                                 lpp_set_factor_fast(si->lpp, cst, arg_op->attr.live_range.ilp, 1.0);
1664
1665 #ifdef CHECK_POST_REMAT
1666                                 //lpp_set_factor_fast(si->lpp, check_post_remat, arg_op->attr.live_range.ilp, 1.0);
1667                                 lpp_set_factor_fast(si->lpp, check_post_remat, post_use, 1.0);
1668 #endif
1669                         }
1670
1671                         /*forall remat2 which use arg add a similar cst*/
1672                         foreach_post_remat(irn, tmp) {
1673                                 int      n;
1674
1675                                 for (n=get_irn_arity(tmp)-1; n>=0; --n) {
1676                                         ir_node    *remat_arg = get_irn_n(tmp, n);
1677                                         op_t       *remat_op = get_irn_link(tmp);
1678
1679                                         if(remat_arg == arg) {
1680                                                 DBG((si->dbg, LEVEL_3, "\t  found remat with arg %+F in epilog of %+F\n", arg, irn));
1681
1682                                                 ir_snprintf(buf, sizeof(buf), "post_use_%N_%N-%d", arg, irn, p++);
1683                                                 cst = lpp_add_cst(si->lpp, buf, lpp_greater, 0.0);
1684                                                 lpp_set_factor_fast(si->lpp, cst, post_use, 1.0);
1685                                                 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, -1.0);
1686                                         }
1687                                 }
1688                         }
1689
1690                         /* new live range begins for each argument */
1691                         arg_op->attr.live_range.ilp = next_lr;
1692                         arg_op->attr.live_range.op = irn;
1693
1694                         pset_insert_ptr(live, arg);
1695                 }
1696
1697                 /* just to be sure */
1698                 check_post = ILP_UNDEF;
1699 #ifdef CHECK_POST_REMAT
1700                 check_post_remat = ILP_UNDEF;
1701 #endif
1702
1703
1704
1705
1706                 /******************
1707                  *   P R O L O G
1708                  ******************/
1709
1710                 /* check the register pressure in the prolog */
1711                 /* sum_{L\U} lr <= k - |U| */
1712                 ir_snprintf(buf, sizeof(buf), "check_pre_%N", irn);
1713                 check_pre = lpp_add_cst(si->lpp, buf, lpp_less, si->n_regs - u);
1714
1715                 /* for the prolog remove defined values from the live set */
1716                 pset_foreach(defs, tmp) {
1717                         pset_remove_ptr(live, tmp);
1718                 }
1719
1720                 /***********************************************************
1721                  *  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
1722                  **********************************************************/
1723
1724
1725                 set_foreach(args, keyval) {
1726                         spill_t        *spill;
1727                         ir_node        *arg = keyval->key;
1728                         int             i = PTR_TO_INT(keyval->val);
1729                         op_t           *arg_op = get_irn_link(arg);
1730
1731                         spill = set_find_spill(spill_bb->ilp, arg);
1732                         assert(spill);
1733
1734                         ir_snprintf(buf, sizeof(buf), "reload_%N_%N", arg, irn);
1735                         op->attr.live_range.args.reloads[i] = lpp_add_var(si->lpp, buf, lpp_binary, COST_LOAD*execution_frequency(si, bb));
1736
1737                         /* reload <= mem_out */
1738                         ir_snprintf(buf, sizeof(buf), "req_reload_%N_%N", arg, irn);
1739                         cst = lpp_add_cst(si->lpp, buf, lpp_less, 0.0);
1740                         lpp_set_factor_fast(si->lpp, cst, op->attr.live_range.args.reloads[i], 1.0);
1741                         lpp_set_factor_fast(si->lpp, cst, spill->mem_out, -1.0);
1742
1743                         /* requirement: arg must be in register for use */
1744                         /* reload + remat + live_range == 1 */
1745                         ir_snprintf(buf, sizeof(buf), "req_%N_%N", irn, arg);
1746                         cst = lpp_add_cst(si->lpp, buf, lpp_equal, 1.0);
1747
1748                         lpp_set_factor_fast(si->lpp, cst, arg_op->attr.live_range.ilp, 1.0);
1749                         lpp_set_factor_fast(si->lpp, cst, op->attr.live_range.args.reloads[i], 1.0);
1750                         foreach_pre_remat(si, irn, tmp) {
1751                                 op_t     *remat_op = get_irn_link(tmp);
1752                                 if(remat_op->attr.remat.remat->value == arg) {
1753                                         lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, 1.0);
1754                                 }
1755                         }
1756                 }
1757
1758                 /* iterate over L\U */
1759                 pset_foreach(live, tmp) {
1760                         if(!set_find_keyval(args, tmp)) {
1761                                 /* if a live value is not used by irn */
1762                                 tmp_op = get_irn_link(tmp);
1763                                 lpp_set_factor_fast(si->lpp, check_pre, tmp_op->attr.live_range.ilp, 1.0);
1764                         }
1765                 }
1766
1767
1768                 /* TODO allow new live ranges even if value does not die after remat??? MAY_DIE_AT_PRE_REMAT */
1769
1770                 /* requirements for remats */
1771                 /* start new live ranges for values used by remats */
1772                 foreach_pre_remat(si, irn, tmp) {
1773                         op_t        *remat_op = get_irn_link(tmp);
1774                         int          n;
1775
1776                         for (n=get_irn_arity(tmp)-1; n>=0; --n) {
1777                                 ir_node        *remat_arg = get_irn_n(tmp, n);
1778                                 op_t           *arg_op = get_irn_link(remat_arg);
1779                                 ilp_var_t       prev_lr;
1780
1781                                 if(!has_reg_class(si, remat_arg)) continue;
1782
1783                                 /* if value is becoming live through use by remat */
1784                                 if(!pset_find_ptr(live, remat_arg)) {
1785                                         ir_snprintf(buf, sizeof(buf), "lr_%N_%N", remat_arg, irn);
1786                                         prev_lr = lpp_add_var(si->lpp, buf, lpp_binary, 0.0);
1787
1788                                         arg_op->attr.live_range.ilp = prev_lr;
1789                                         arg_op->attr.live_range.op = irn;
1790
1791                                         DBG((si->dbg, LEVEL_4, "  value %+F becoming live through use by remat %+F\n", remat_arg, tmp));
1792
1793                                         pset_insert_ptr(live, remat_arg);
1794                                         add_to_spill_bb(si, bb, remat_arg);
1795                                 }
1796
1797                                 /* remat <= live_rang(remat_arg) [ + reload(remat_arg) ] */
1798                                 ir_snprintf(buf, sizeof(buf), "req_remat_%N_arg_%N", tmp, remat_arg);
1799                                 cst = lpp_add_cst(si->lpp, buf, lpp_less, 0.0);
1800
1801                                 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, 1.0);
1802                                 lpp_set_factor_fast(si->lpp, cst, arg_op->attr.live_range.ilp, -1.0);
1803
1804                                 /* if remat arg is also used by current op then we can use reload placed for this argument */
1805                                 if((keyval = set_find_keyval(args, remat_arg)) != NULL) {
1806                                         int    index = (int)keyval->val;
1807
1808                                         lpp_set_factor_fast(si->lpp, cst, op->attr.live_range.args.reloads[index], -1.0);
1809                                 }
1810                         }
1811                 }
1812
1813
1814
1815
1816                 /*************************
1817                  *  D O N E  W I T H  O P
1818                  *************************/
1819
1820                 DBG((si->dbg, LEVEL_4, "\t   %d values live at %+F\n", pset_count(live), irn));
1821
1822                 pset_foreach(live, tmp) {
1823                         assert(has_reg_class(si, tmp));
1824                 }
1825
1826                 for (n=get_irn_arity(irn)-1; n>=0; --n) {
1827                         ir_node        *arg = get_irn_n(irn, n);
1828
1829                         assert(!find_post_remat(arg, irn) && "there should be no post remat for an argument of an op");
1830                 }
1831
1832                 del_set(args);
1833                 del_pset(defs);
1834                 defs = pset_new_ptr_default();
1835         }
1836
1837
1838
1839         /***************************************
1840          *   B E G I N N I N G  O F  B L O C K
1841          ***************************************/
1842
1843
1844         /* we are now at the beginning of the basic block, there are only \Phis in front of us */
1845         DBG((si->dbg, LEVEL_3, "\t   %d values live at beginning of block %+F\n", pset_count(live), bb));
1846
1847         pset_foreach(live, irn) {
1848                 assert(is_Phi(irn) || get_nodes_block(irn) != bb);
1849         }
1850
1851         /* construct mem_outs for all values */
1852
1853         set_foreach(spill_bb->ilp, spill) {
1854                 ir_snprintf(buf, sizeof(buf), "mem_out_%N_%N", spill->irn, bb);
1855                 cst = lpp_add_cst(si->lpp, buf, lpp_less, 0.0);
1856
1857                 lpp_set_factor_fast(si->lpp, cst, spill->mem_out, 1.0);
1858                 lpp_set_factor_fast(si->lpp, cst, spill->spill, -1.0);
1859
1860                 if(pset_find_ptr(live, spill->irn)) {
1861                         DBG((si->dbg, LEVEL_5, "\t     %+F live at beginning of block %+F\n", spill->irn, bb));
1862
1863                         ir_snprintf(buf, sizeof(buf), "mem_in_%N_%N", spill->irn, bb);
1864                         spill->mem_in = lpp_add_var(si->lpp, buf, lpp_binary, 0.0);
1865                         lpp_set_factor_fast(si->lpp, cst, spill->mem_in, -1.0);
1866
1867                         if(is_Phi(spill->irn) && get_nodes_block(spill->irn) == bb) {
1868                                 int   n;
1869                                 op_t *op = get_irn_link(spill->irn);
1870
1871                                 /* do we have to copy a phi argument? */
1872                                 op->attr.live_range.args.copies = obstack_alloc(si->obst, sizeof(*op->attr.live_range.args.copies) * get_irn_arity(spill->irn));
1873                                 memset(op->attr.live_range.args.copies, 0xFF, sizeof(*op->attr.live_range.args.copies) * get_irn_arity(spill->irn));
1874
1875                                 for(n=get_irn_arity(spill->irn)-1; n>=0; --n) {
1876                                         const ir_node  *arg = get_irn_n(spill->irn, n);
1877                                         double          freq=0.0;
1878                                         int             m;
1879                                         ilp_var_t       var;
1880
1881
1882                                         /* argument already done? */
1883                                         if(op->attr.live_range.args.copies[n] != ILP_UNDEF) continue;
1884
1885                                         /* get sum of execution frequencies of blocks with the same phi argument */
1886                                         for(m=n; m>=0; --m) {
1887                                                 const ir_node  *arg2 = get_irn_n(spill->irn, m);
1888
1889                                                 if(arg==arg2) {
1890                                                         freq += execution_frequency(si, get_Block_cfgpred_block(bb, m));
1891                                                 }
1892                                         }
1893
1894                                         /* copies are not for free */
1895                                         ir_snprintf(buf, sizeof(buf), "copy_%N_%N", arg, spill->irn);
1896                                         var = lpp_add_var(si->lpp, buf, lpp_binary, COST_STORE * freq);
1897
1898                                         for(m=n; m>=0; --m) {
1899                                                 const ir_node  *arg2 = get_irn_n(spill->irn, m);
1900
1901                                                 if(arg==arg2) {
1902                                                         op->attr.live_range.args.copies[m] = var;
1903                                                 }
1904                                         }
1905
1906                                         /* copy <= mem_in */
1907                                         ir_snprintf(buf, sizeof(buf), "nocopy_%N_%N", arg, spill->irn);
1908                                         cst = lpp_add_cst(si->lpp, buf, lpp_less, 0.0);
1909                                         lpp_set_factor_fast(si->lpp, cst, var, 1.0);
1910                                         lpp_set_factor_fast(si->lpp, cst, spill->mem_in, -1.0);
1911                                 }
1912                         }
1913                 }
1914         }
1915
1916
1917         /* L\U is empty at bb start */
1918         /* arg is live throughout epilog if it is reg_in into this block */
1919
1920         /* check the register pressure at the beginning of the block
1921          * including remats
1922          */
1923         ir_snprintf(buf, sizeof(buf), "check_start_%N", bb);
1924         cst = lpp_add_cst(si->lpp, buf, lpp_less, si->n_regs);
1925
1926         pset_foreach(live, irn) {
1927         ilp_cst_t  nospill;
1928
1929                 spill = set_find_spill(spill_bb->ilp, irn);
1930                 assert(spill);
1931
1932                 ir_snprintf(buf, sizeof(buf), "reg_in_%N_%N", irn, bb);
1933                 spill->reg_in = lpp_add_var(si->lpp, buf, lpp_binary, 0.0);
1934
1935                 lpp_set_factor_fast(si->lpp, cst, spill->reg_in, 1.0);
1936
1937                 /* spill + reg_in <= 1 */
1938                 ir_snprintf(buf, sizeof(buf), "nospill_%N_%N", irn, bb);
1939                 nospill = lpp_add_cst(si->lpp, buf, lpp_less, 1);
1940
1941                 lpp_set_factor_fast(si->lpp, nospill, spill->reg_in, 1.0);
1942                 lpp_set_factor_fast(si->lpp, nospill, spill->spill, 1.0);
1943
1944         }
1945         foreach_post_remat(bb, irn) {
1946                 op_t     *remat_op = get_irn_link(irn);
1947
1948                 DBG((si->dbg, LEVEL_4, "\t  next post remat: %+F\n", irn));
1949                 assert(remat_op->is_remat && !remat_op->attr.remat.pre);
1950
1951                 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, 1.0);
1952         }
1953
1954         /* forall remat2 add requirements */
1955         foreach_post_remat(bb, tmp) {
1956                 int         n;
1957
1958                 for (n=get_irn_arity(tmp)-1; n>=0; --n) {
1959                         ir_node    *remat_arg = get_irn_n(tmp, n);
1960                         op_t       *remat_op = get_irn_link(tmp);
1961
1962                         if(!has_reg_class(si, remat_arg)) continue;
1963
1964                         spill = set_find_spill(spill_bb->ilp, remat_arg);
1965                         assert(spill);
1966
1967                         /* TODO verify this is placed correctly */
1968                         ir_snprintf(buf, sizeof(buf), "req_remat2_%N_%N_arg_%N", tmp, bb, remat_arg);
1969                         cst = lpp_add_cst(si->lpp, buf, lpp_less, 0.0);
1970                         lpp_set_factor_fast(si->lpp, cst, spill->reg_in, -1.0);
1971                         lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, 1.0);
1972                 }
1973         }
1974
1975         /* mem_in/reg_in for live_in values, especially phis and their arguments */
1976         pset_foreach(live, irn) {
1977                 int          p = 0,
1978                                          n;
1979
1980                 spill = set_find_spill(spill_bb->ilp, irn);
1981                 assert(spill && spill->irn == irn);
1982
1983                 if(is_Phi(irn) && get_nodes_block(irn) == bb) {
1984                         for (n=get_Phi_n_preds(irn)-1; n>=0; --n) {
1985                                 ilp_cst_t       mem_in,
1986                                                                 reg_in;
1987                                 ir_node        *phi_arg = get_Phi_pred(irn, n);
1988                                 ir_node        *bb_p = get_Block_cfgpred_block(bb, n);
1989                                 spill_bb_t     *spill_bb_p = get_irn_link(bb_p);
1990                                 spill_t        *spill_p;
1991
1992                                 /* although the phi is in the right regclass one or more of
1993                                  * its arguments can be in a different one or at least to
1994                                  * ignore
1995                                  */
1996                                 if(has_reg_class(si, phi_arg)) {
1997                                         ir_snprintf(buf, sizeof(buf), "mem_in_%N_%N-%d", irn, bb, p);
1998                                         mem_in = lpp_add_cst(si->lpp, buf, lpp_less, 0.0);
1999                                         ir_snprintf(buf, sizeof(buf), "reg_in_%N_%N-%d", irn, bb, p++);
2000                                         reg_in = lpp_add_cst(si->lpp, buf, lpp_less, 0.0);
2001
2002                                         lpp_set_factor_fast(si->lpp, mem_in, spill->mem_in, 1.0);
2003                                         lpp_set_factor_fast(si->lpp, reg_in, spill->reg_in, 1.0);
2004
2005                                         spill_p = set_find_spill(spill_bb_p->ilp, phi_arg);
2006                                         assert(spill_p);
2007
2008                                         lpp_set_factor_fast(si->lpp, mem_in, spill_p->mem_out, -1.0);
2009                                         lpp_set_factor_fast(si->lpp, reg_in, spill_p->reg_out, -1.0);
2010                                 }
2011                         }
2012                 } else {
2013                         /* else assure the value arrives on all paths in the same resource */
2014
2015                         for (n=get_Block_n_cfgpreds(bb)-1; n>=0; --n) {
2016                                 ilp_cst_t       mem_in,
2017                                                                 reg_in;
2018                                 ir_node        *bb_p = get_Block_cfgpred_block(bb, n);
2019                                 spill_bb_t     *spill_bb_p = get_irn_link(bb_p);
2020                                 spill_t        *spill_p;
2021
2022                                 ir_snprintf(buf, sizeof(buf), "mem_in_%N_%N-%d", irn, bb, p);
2023                                 mem_in = lpp_add_cst(si->lpp, buf, lpp_less, 0.0);
2024                                 ir_snprintf(buf, sizeof(buf), "reg_in_%N_%N-%d", irn, bb, p++);
2025                                 reg_in = lpp_add_cst(si->lpp, buf, lpp_less, 0.0);
2026
2027                                 lpp_set_factor_fast(si->lpp, mem_in, spill->mem_in, 1.0);
2028                                 lpp_set_factor_fast(si->lpp, reg_in, spill->reg_in, 1.0);
2029
2030                                 spill_p = set_find_spill(spill_bb_p->ilp, irn);
2031                                 assert(spill_p);
2032
2033                                 lpp_set_factor_fast(si->lpp, mem_in, spill_p->mem_out, -1.0);
2034                                 lpp_set_factor_fast(si->lpp, reg_in, spill_p->reg_out, -1.0);
2035                         }
2036                 }
2037         }
2038
2039         /* first live ranges from reg_ins */
2040         pset_foreach(live, irn) {
2041                 op_t      *op = get_irn_link(irn);
2042
2043                 spill = set_find_spill(spill_bb->ilp, irn);
2044                 assert(spill && spill->irn == irn);
2045
2046                 ir_snprintf(buf, sizeof(buf), "first_lr_%N_%N", irn, bb);
2047                 cst = lpp_add_cst(si->lpp, buf, lpp_less, 0.0);
2048                 lpp_set_factor_fast(si->lpp, cst, op->attr.live_range.ilp, 1.0);
2049                 lpp_set_factor_fast(si->lpp, cst, spill->reg_in, -1.0);
2050
2051                 foreach_post_remat(bb, tmp) {
2052                         op_t     *remat_op = get_irn_link(tmp);
2053
2054                         if(remat_op->attr.remat.remat->value == irn) {
2055                                 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, -1.0);
2056                         }
2057                 }
2058         }
2059
2060         /* walk forward now and compute constraints for placing spills */
2061         /* this must only be done for values that are not defined in this block */
2062         /* TODO are these values at start of block? if yes, just check whether this is a diverge edge and skip the loop */
2063         pset_foreach(live, irn) {
2064                 spill = set_find_spill(spill_bb->ilp, irn);
2065                 assert(spill);
2066
2067                 ir_snprintf(buf, sizeof(buf), "req_spill_%N_%N", irn, bb);
2068                 cst = lpp_add_cst(si->lpp, buf, lpp_less, 0.0);
2069
2070                 lpp_set_factor_fast(si->lpp, cst, spill->spill, 1.0);
2071                 if(is_diverge_edge(bb)) lpp_set_factor_fast(si->lpp, cst, spill->reg_in, -1.0);
2072
2073                 sched_foreach_op(bb, tmp) {
2074                         op_t   *op = get_irn_link(tmp);
2075
2076                         if(is_Phi(tmp)) continue;
2077                         assert(!is_Proj(tmp));
2078
2079                         if(op->is_remat) {
2080                                 ir_node   *value = op->attr.remat.remat->value;
2081
2082                                 if(value == irn) {
2083                                         /* only collect remats up to the first use of a value */
2084                                         lpp_set_factor_fast(si->lpp, cst, op->attr.remat.ilp, -1.0);
2085                                 }
2086                         } else {
2087                                 int   n;
2088
2089                                 for (n=get_irn_arity(tmp)-1; n>=0; --n) {
2090                                         ir_node    *arg = get_irn_n(tmp, n);
2091
2092                                         if(arg == irn) {
2093                                                 /* if a value is used stop collecting remats */
2094                                                 cst = ILP_UNDEF;
2095                                         }
2096                                         break;
2097                                 }
2098                         }
2099                         if(cst == ILP_UNDEF) break;
2100                 }
2101         }
2102
2103
2104         /* if a value is used by a mem-phi, then mem_in of this value is 0 (has to be spilled again into a different slot)
2105            mem_in(phi) -> not mem_in(orig_value) TODO: how does this depend on a certain predecessor?
2106          */
2107
2108         /* mem_in of mem-phi has associated costs (but first one is free) */
2109         /* define n_mem_copies as positive integer in each predecessor block,
2110            #mem_in into this block from predecessor block - 1 weighted with SPILL_COST*execfreq(predecessor)
2111            TODO
2112          */
2113
2114
2115         del_pset(live);
2116 }
2117
2118 typedef struct _irnlist_t {
2119         struct list_head   list;
2120         ir_node           *irn;
2121 } irnlist_t;
2122
2123 typedef struct _interference_t {
2124         struct list_head    blocklist;
2125         ir_node            *a;
2126         ir_node            *b;
2127 } interference_t;
2128
2129 static int
2130 cmp_interference(const void *a, const void *b, size_t size)
2131 {
2132         const interference_t *p = a;
2133         const interference_t *q = b;
2134
2135         return !(p->a == q->a && p->b == q->b);
2136 }
2137
2138 static interference_t *
2139 set_find_interference(set * set, ir_node * a, ir_node * b)
2140 {
2141         interference_t     query;
2142
2143         query.a = (a>b)?a:b;
2144         query.b = (a>b)?b:a;
2145
2146         return set_find(set, &query, sizeof(query), HASH_PTR(PTR_TO_INT(a) ^ PTR_TO_INT(b)));
2147 }
2148
2149 static interference_t *
2150 set_insert_interference(spill_ilp_t * si, set * set, ir_node * a, ir_node * b, ir_node * bb)
2151 {
2152         interference_t     query,
2153                                           *result;
2154         irnlist_t         *list = obstack_alloc(si->obst, sizeof(*list));
2155
2156         list->irn = bb;
2157
2158         result = set_find_interference(set, a, b);
2159         if(result) {
2160
2161                 list_add(&list->list, &result->blocklist);
2162                 return result;
2163         }
2164
2165         query.a = (a>b)?a:b;
2166         query.b = (a>b)?b:a;
2167
2168         result = set_insert(set, &query, sizeof(query), HASH_PTR(PTR_TO_INT(a) ^ PTR_TO_INT(b)));
2169
2170         INIT_LIST_HEAD(&result->blocklist);
2171         list_add(&list->list, &result->blocklist);
2172
2173         return result;
2174 }
2175
2176 static int
2177 values_interfere_in_block(ir_node * bb, ir_node * a, ir_node * b)
2178 {
2179         const ir_edge_t *edge;
2180
2181         if(get_nodes_block(a) != bb && get_nodes_block(b) != bb) {
2182                 /* both values are live in, so they interfere */
2183                 return 1;
2184         }
2185
2186         /* ensure a dominates b */
2187         if(value_dominates(b,a)) {
2188                 const ir_node * t;
2189                 t = b;
2190                 b = a;
2191                 a = t;
2192         }
2193         assert(get_nodes_block(b) == bb && "at least b should be defined here in this block");
2194
2195
2196         /* the following code is stolen from bera.c */
2197         if(is_live_end(bb, a))
2198                 return 1;
2199
2200         foreach_out_edge(a, edge) {
2201                 const ir_node *user = edge->src;
2202                 if(get_nodes_block(user) == bb
2203                                 && !is_Phi(user)
2204                                 && b != user
2205                                 && value_dominates(b, user))
2206                         return 1;
2207         }
2208
2209         return 0;
2210 }
2211
2212 /**
2213  * Walk all irg blocks and collect interfering values inside of phi classes
2214  */
2215 static void
2216 luke_interferencewalker(ir_node * bb, void * data)
2217 {
2218         spill_ilp_t    *si = (spill_ilp_t*)data;
2219         irn_live_t     *li1,
2220                        *li2;
2221
2222         live_foreach(bb, li1) {
2223                 ir_node        *a = (ir_node *) li1->irn;
2224                 op_t           *a_op = get_irn_link(a);
2225
2226                 if(a_op->is_remat) continue;
2227
2228                 /* a is only interesting if it is in my register class and if it is inside a phi class */
2229                 if (has_reg_class(si, a) && get_phi_class(a)) {
2230                         for(li2=li1->next; li2; li2 = li2->next) {
2231                                 ir_node        *b = (ir_node *) li2->irn;
2232                                 op_t           *b_op = get_irn_link(b);
2233
2234                                 if(b_op->is_remat) continue;
2235
2236                                 /* a and b are only interesting if they are in the same phi class */
2237                                 if(has_reg_class(si, b) && get_phi_class(a) == get_phi_class(b)) {
2238                                         if(values_interfere_in_block(bb, a, b)) {
2239                                                 //DBG((si->dbg, LEVEL_1, "\tvalues interfere in %+F: %+F, %+F\n", bb, a, b));
2240                                                 ir_fprintf(stderr, "\tvalues interfere in %+F: %+F, %+F\n", bb, a, b);
2241                                                 set_insert_interference(si, si->interferences, a, b, bb);
2242                                         }
2243                                 }
2244                         }
2245                 }
2246         }
2247 }
2248
2249 static unsigned int copy_path_id = 0;
2250
2251 static void
2252 write_copy_path_cst(spill_ilp_t *si, pset * copies, ilp_var_t any_interfere)
2253 {
2254         ilp_cst_t  cst;
2255         ilp_var_t  copy;
2256         char       buf[256];
2257         void      *ptr;
2258
2259         ir_snprintf(buf, sizeof(buf), "copy_path-%d", copy_path_id++);
2260         cst = lpp_add_cst(si->lpp, buf, lpp_less, 0);
2261
2262         lpp_set_factor_fast(si->lpp, cst, any_interfere, 1.0);
2263
2264         pset_foreach(copies, ptr) {
2265                 copy = PTR_TO_INT(ptr);
2266                 lpp_set_factor_fast(si->lpp, cst, copy, -1.0);
2267         }
2268 }
2269
2270 /**
2271  * @parameter copies   contains a path of copies which lead us to irn
2272  * @parameter visited  contains a set of nodes already visited on this path
2273  */
2274 static void
2275 find_copy_path(spill_ilp_t * si, ir_node * irn, ir_node * target, ilp_var_t any_interfere, pset * copies, pset * visited)
2276 {
2277         ir_edge_t *edge;
2278         op_t      *op = get_irn_link(irn);
2279
2280         if(op->is_remat) return;
2281
2282         pset_insert_ptr(visited, irn);
2283
2284         if(is_Phi(irn)) {
2285                 int    n;
2286
2287                 /* visit all operands */
2288                 for(n=get_irn_arity(irn)-1; n>=0; --n) {
2289                         ir_node  *arg = get_irn_n(irn, n);
2290                         ilp_var_t  copy = op->attr.live_range.args.copies[n];
2291
2292                         if(!has_reg_class(si, arg)) continue;
2293
2294                         if(arg == target) {
2295                                 pset_insert(copies, INT_TO_PTR(copy), copy);
2296                                 write_copy_path_cst(si, copies, any_interfere);
2297                                 pset_remove(copies, INT_TO_PTR(copy), copy);
2298                         } else {
2299                                 if(!pset_find_ptr(visited, arg)) {
2300                                         pset_insert(copies, INT_TO_PTR(copy), copy);
2301                                         find_copy_path(si, arg, target, any_interfere, copies, visited);
2302                                         pset_remove(copies, INT_TO_PTR(copy), copy);
2303                                 }
2304                         }
2305                 }
2306         }
2307
2308         /* visit all uses which are phis */
2309         foreach_out_edge(irn, edge) {
2310                 ir_node  *user = edge->src;
2311                 int       pos  = edge->pos;
2312                 op_t     *op = get_irn_link(user);
2313                 ilp_var_t copy;
2314
2315                 if(!is_Phi(user)) continue;
2316                 if(!has_reg_class(si, user)) continue;
2317
2318                 copy = op->attr.live_range.args.copies[pos];
2319
2320                 if(user == target) {
2321                         pset_insert(copies, INT_TO_PTR(copy), copy);
2322                         write_copy_path_cst(si, copies, any_interfere);
2323                         pset_remove(copies, INT_TO_PTR(copy), copy);
2324                 } else {
2325                         if(!pset_find_ptr(visited, user)) {
2326                                 pset_insert(copies, INT_TO_PTR(copy), copy);
2327                                 find_copy_path(si, user, target, any_interfere, copies, visited);
2328                                 pset_remove(copies, INT_TO_PTR(copy), copy);
2329                         }
2330                 }
2331         }
2332
2333         pset_remove_ptr(visited, irn);
2334 }
2335
2336 static void
2337 gen_copy_constraints(spill_ilp_t * si, ir_node * a, ir_node * b, ilp_var_t any_interfere)
2338 {
2339         pset * copies = pset_new_ptr_default();
2340         pset * visited = pset_new_ptr_default();
2341
2342         find_copy_path(si, a, b, any_interfere, copies, visited);
2343
2344         del_pset(visited);
2345         del_pset(copies);
2346 }
2347
2348
2349 static void
2350 memcopyhandler(spill_ilp_t * si)
2351 {
2352         interference_t   *interference;
2353         char              buf[256];
2354         /* teste Speicherwerte auf Interferenz */
2355
2356         /* analyze phi classes */
2357         phi_class_compute(si->chordal_env->irg);
2358
2359         DBG((si->dbg, LEVEL_2, "\t calling interferencewalker\n"));
2360         irg_block_walk_graph(si->chordal_env->irg, luke_interferencewalker, NULL, si);
2361
2362 //      phi_class_free(si->chordal_env->irg);
2363
2364         /* now lets emit the ILP unequations for the crap */
2365         set_foreach(si->interferences, interference) {
2366                 irnlist_t      *irnlist;
2367                 ilp_var_t       interfere,
2368                                                 any_interfere;
2369                 ilp_cst_t       any_interfere_cst,
2370                                                 cst;
2371                 const ir_node  *a  = interference->a;
2372                 const ir_node  *b  = interference->b;
2373                 struct list_head *pos;
2374
2375                 /* any_interf <= \sum interf */
2376                 ir_snprintf(buf, sizeof(buf), "interfere_%N_%N", a, b);
2377                 any_interfere_cst = lpp_add_cst(si->lpp, buf, lpp_less, 0);
2378                 any_interfere = lpp_add_var(si->lpp, buf, lpp_binary, 0.0);
2379
2380                 lpp_set_factor_fast(si->lpp, any_interfere_cst, any_interfere, 1.0);
2381
2382                 list_for_each_entry(irnlist_t, irnlist, &interference->blocklist, list) {
2383                         const ir_node  *bb = irnlist->irn;
2384                         spill_bb_t     *spill_bb = get_irn_link(bb);
2385                         spill_t        *spilla,
2386                                                    *spillb,
2387                                                    query;
2388                         char           buf[256];
2389
2390                         query.irn = a;
2391                         spilla = set_find_spill(spill_bb->ilp, a);
2392                         assert(spilla);
2393
2394                         query.irn = b;
2395                         spillb = set_find_spill(spill_bb->ilp, b);
2396                         assert(spillb);
2397
2398                         /* interfere <-> (mem_in_a or spill_a) and (mem_in_b or spill_b): */
2399                         /* 1:   mem_in_a + mem_in_b + spill_a + spill_b - interfere <= 1 */
2400                         /* 2: - mem_in_a - spill_a + interfere <= 0 */
2401                         /* 3: - mem_in_b - spill_b + interfere <= 0 */
2402                         ir_snprintf(buf, sizeof(buf), "interfere_%N_%N_%N", bb, a, b);
2403                         interfere = lpp_add_var(si->lpp, buf, lpp_binary, 0.0);
2404
2405                         ir_snprintf(buf, sizeof(buf), "interfere_%N_%N_%N-1", bb, a, b);
2406                         cst = lpp_add_cst(si->lpp, buf, lpp_less, 1);
2407
2408                         lpp_set_factor_fast(si->lpp, cst, interfere, -1.0);
2409                         if(spilla->mem_in != ILP_UNDEF) lpp_set_factor_fast(si->lpp, cst, spilla->mem_in, 1.0);
2410                         lpp_set_factor_fast(si->lpp, cst, spilla->spill, 1.0);
2411                         if(spillb->mem_in != ILP_UNDEF) lpp_set_factor_fast(si->lpp, cst, spillb->mem_in, 1.0);
2412                         lpp_set_factor_fast(si->lpp, cst, spillb->spill, 1.0);
2413
2414                         ir_snprintf(buf, sizeof(buf), "interfere_%N_%N_%N-2", bb, a, b);
2415                         cst = lpp_add_cst(si->lpp, buf, lpp_less, 0);
2416
2417                         lpp_set_factor_fast(si->lpp, cst, interfere, 1.0);
2418                         if(spilla->mem_in != ILP_UNDEF) lpp_set_factor_fast(si->lpp, cst, spilla->mem_in, -1.0);
2419                         lpp_set_factor_fast(si->lpp, cst, spilla->spill, -1.0);
2420
2421                         ir_snprintf(buf, sizeof(buf), "interfere_%N_%N_%N-3", bb, a, b);
2422                         cst = lpp_add_cst(si->lpp, buf, lpp_less, 0);
2423
2424                         lpp_set_factor_fast(si->lpp, cst, interfere, 1.0);
2425                         if(spillb->mem_in != ILP_UNDEF) lpp_set_factor_fast(si->lpp, cst, spillb->mem_in, -1.0);
2426                         lpp_set_factor_fast(si->lpp, cst, spillb->spill, -1.0);
2427
2428
2429                         lpp_set_factor_fast(si->lpp, any_interfere_cst, interfere, -1.0);
2430
2431                         /* any_interfere >= interf */
2432                         ir_snprintf(buf, sizeof(buf), "interfere_%N_%N-%N", a, b, bb);
2433                         cst = lpp_add_cst(si->lpp, buf, lpp_less, 0);
2434
2435                         lpp_set_factor_fast(si->lpp, cst, interfere, 1.0);
2436                         lpp_set_factor_fast(si->lpp, cst, any_interfere, -1.0);
2437                 }
2438
2439                 /* now that we know whether the two values interfere in memory we can drop constraints to enforce copies */
2440                 gen_copy_constraints(si,a,b,any_interfere);
2441         }
2442 }
2443
2444
2445
2446 static void
2447 memcopyinsertor(spill_ilp_t * si)
2448 {
2449         /* weise Spillkontexte zu. Sorge bei Phis dafuer, dass gleiche
2450          * Kontexte zusammenfliessen (Operanden und Ergebnis hat gleichen
2451          * Kontext)
2452          */
2453
2454
2455
2456
2457
2458 }
2459
2460
2461
2462
2463 static INLINE int
2464 is_zero(double x)
2465 {
2466         return fabs(x) < 0.00001;
2467 }
2468
2469 #ifdef KEEPALIVE
2470 static int mark_remat_nodes_hook(FILE *F, ir_node *n, ir_node *l)
2471 {
2472         spill_ilp_t *si = get_irg_link(current_ir_graph);
2473
2474         if(pset_find_ptr(si->all_possible_remats, n)) {
2475                 op_t   *op = (op_t*)get_irn_link(n);
2476                 assert(op && op->is_remat);
2477
2478                 if(!op->attr.remat.remat->inverse) {
2479                         if(op->attr.remat.pre) {
2480                                 ir_fprintf(F, "color:red info3:\"remat value: %+F\"", op->attr.remat.remat->value);
2481                         } else {
2482                                 ir_fprintf(F, "color:orange info3:\"remat2 value: %+F\"", op->attr.remat.remat->value);
2483                         }
2484
2485                         return 1;
2486                 } else {
2487                         op_t   *op = (op_t*)get_irn_link(n);
2488                         assert(op && op->is_remat);
2489
2490                         if(op->attr.remat.pre) {
2491                                 ir_fprintf(F, "color:cyan info3:\"remat inverse value: %+F\"", op->attr.remat.remat->value);
2492                         } else {
2493                                 ir_fprintf(F, "color:lightcyan info3:\"remat2 inverse value: %+F\"", op->attr.remat.remat->value);
2494                         }
2495
2496                         return 1;
2497                 }
2498         }
2499
2500         return 0;
2501 }
2502
2503 static void
2504 dump_graph_with_remats(ir_graph * irg, const char * suffix)
2505 {
2506         set_dump_node_vcgattr_hook(mark_remat_nodes_hook);
2507         be_dump(irg, suffix, dump_ir_block_graph_sched);
2508         set_dump_node_vcgattr_hook(NULL);
2509 }
2510 #endif
2511
2512 /**
2513  * Edge hook to dump the schedule edges with annotated register pressure.
2514  */
2515 static int
2516 sched_pressure_edge_hook(FILE *F, ir_node *irn)
2517 {
2518         if(sched_is_scheduled(irn) && sched_has_prev(irn)) {
2519                 ir_node *prev = sched_prev(irn);
2520                 fprintf(F, "edge:{sourcename:\"");
2521                 PRINT_NODEID(irn);
2522                 fprintf(F, "\" targetname:\"");
2523                 PRINT_NODEID(prev);
2524                 fprintf(F, "\" label:\"%d", (int)get_irn_link(irn));
2525                 fprintf(F, "\" color:magenta}\n");
2526         }
2527         return 1;
2528 }
2529
2530 static void
2531 dump_ir_block_graph_sched_pressure(ir_graph *irg, const char *suffix)
2532 {
2533         DUMP_NODE_EDGE_FUNC old = get_dump_node_edge_hook();
2534
2535         dump_consts_local(0);
2536         set_dump_node_edge_hook(sched_pressure_edge_hook);
2537         dump_ir_block_graph(irg, suffix);
2538         set_dump_node_edge_hook(old);
2539 }
2540
2541 static void
2542 walker_pressure_annotator(ir_node * bb, void * data)
2543 {
2544         spill_ilp_t  *si = data;
2545         ir_node      *irn;
2546         irn_live_t   *li;
2547         int           n;
2548         pset         *live = pset_new_ptr_default();
2549         int           projs = 0;
2550
2551         live_foreach(bb, li) {
2552                 irn = (ir_node *) li->irn;
2553
2554                 if (live_is_end(li) && has_reg_class(si, irn)) {
2555                         pset_insert_ptr(live, irn);
2556                 }
2557         }
2558
2559         set_irn_link(bb, INT_TO_PTR(pset_count(live)));
2560
2561         sched_foreach_reverse(bb, irn) {
2562                 if(is_Phi(irn)) {
2563                         set_irn_link(irn, INT_TO_PTR(pset_count(live)));
2564                         continue;
2565                 }
2566
2567                 if(has_reg_class(si, irn)) {
2568                         pset_remove_ptr(live, irn);
2569                         if(is_Proj(irn)) ++projs;
2570                 }
2571
2572                 if(!is_Proj(irn)) projs = 0;
2573
2574                 for (n=get_irn_arity(irn)-1; n>=0; --n) {
2575                         ir_node    *arg = get_irn_n(irn, n);
2576
2577                         if(has_reg_class(si, arg)) pset_insert_ptr(live, arg);
2578                 }
2579                 set_irn_link(irn, INT_TO_PTR(pset_count(live)+projs));
2580         }
2581
2582         del_pset(live);
2583 }
2584
2585 static void
2586 dump_pressure_graph(spill_ilp_t * si, const char *suffix)
2587 {
2588         be_dump(si->chordal_env->irg, suffix, dump_ir_block_graph_sched_pressure);
2589 }
2590
2591 #ifdef KEEPALIVE
2592 static void
2593 connect_all_remats_with_keep(spill_ilp_t * si)
2594 {
2595         ir_node   *irn;
2596         ir_node  **ins,
2597                          **pos;
2598         int        n_remats;
2599
2600
2601         n_remats = pset_count(si->all_possible_remats);
2602         if(n_remats) {
2603                 ins = obstack_alloc(si->obst, n_remats * sizeof(*ins));
2604
2605                 pos = ins;
2606                 pset_foreach(si->all_possible_remats, irn) {
2607                         *pos = irn;
2608                         ++pos;
2609                 }
2610
2611                 si->keep = be_new_Keep(si->chordal_env->cls, si->chordal_env->irg, get_irg_end_block(si->chordal_env->irg), n_remats, ins);
2612
2613                 obstack_free(si->obst, ins);
2614         }
2615 }
2616 #endif
2617
2618 static void
2619 connect_all_spills_with_keep(spill_ilp_t * si)
2620 {
2621         ir_node   *irn;
2622         ir_node  **ins,
2623                          **pos;
2624         int        n_spills;
2625         ir_node   *keep;
2626
2627
2628         n_spills = pset_count(si->spills);
2629         if(n_spills) {
2630                 ins = obstack_alloc(si->obst, n_spills * sizeof(*ins));
2631
2632                 pos = ins;
2633                 pset_foreach(si->spills, irn) {
2634                         *pos = irn;
2635                         ++pos;
2636                 }
2637
2638                 keep = be_new_Keep(si->chordal_env->cls, si->chordal_env->irg, get_irg_end_block(si->chordal_env->irg), n_spills, ins);
2639
2640                 obstack_free(si->obst, ins);
2641         }
2642 }
2643
2644 /** insert a spill at an arbitrary position */
2645 ir_node *be_spill2(const arch_env_t *arch_env, ir_node *irn, ir_node *insert, ir_node *ctx)
2646 {
2647         ir_node *bl     = is_Block(insert)?insert:get_nodes_block(insert);
2648         ir_graph *irg   = get_irn_irg(bl);
2649         ir_node *frame  = get_irg_frame(irg);
2650         ir_node *spill;
2651         ir_node *next;
2652
2653         const arch_register_class_t *cls       = arch_get_irn_reg_class(arch_env, irn, -1);
2654         const arch_register_class_t *cls_frame = arch_get_irn_reg_class(arch_env, frame, -1);
2655
2656         spill = be_new_Spill(cls, cls_frame, irg, bl, frame, irn, ctx);
2657
2658         /*
2659          * search the right insertion point. a spill of a phi cannot be put
2660          * directly after the phi, if there are some phis behind the one which
2661          * is spilled. Also, a spill of a Proj must be after all Projs of the
2662          * same tuple node.
2663          *
2664          * Here's one special case:
2665          * If the spill is in the start block, the spill must be after the frame
2666          * pointer is set up. This is done by setting insert to the end of the block
2667          * which is its default initialization (see above).
2668          */
2669
2670         if(bl == get_irg_start_block(irg) && sched_get_time_step(frame) >= sched_get_time_step(insert))
2671                 insert = frame;
2672
2673         for (next = sched_next(insert); is_Phi(next) || is_Proj(next); next = sched_next(insert))
2674                 insert = next;
2675
2676         sched_add_after(insert, spill);
2677         return spill;
2678 }
2679
2680 static void
2681 delete_remat(spill_ilp_t * si, ir_node * remat) {
2682         int       n;
2683         ir_node  *bad = get_irg_bad(si->chordal_env->irg);
2684
2685         sched_remove(remat);
2686
2687         /* kill links to operands */
2688         for (n=get_irn_arity(remat)-1; n>=-1; --n) {
2689                 set_irn_n(remat, n, bad);
2690         }
2691 }
2692
2693 static void
2694 clean_remat_info(spill_ilp_t * si)
2695 {
2696         int            n;
2697         remat_t       *remat;
2698         remat_info_t  *remat_info;
2699         ir_node       *bad = get_irg_bad(si->chordal_env->irg);
2700
2701         set_foreach(si->remat_info, remat_info) {
2702                 if(!remat_info->remats) continue;
2703
2704                 pset_foreach(remat_info->remats, remat)
2705                 {
2706                         if(remat->proj && get_irn_n_edges(remat->proj) == 0) {
2707                                 set_irn_n(remat->proj, -1, bad);
2708                                 set_irn_n(remat->proj, 0, bad);
2709                         }
2710
2711                         if(get_irn_n_edges(remat->op) == 0) {
2712                                 for (n=get_irn_arity(remat->op)-1; n>=-1; --n) {
2713                                         set_irn_n(remat->op, n, bad);
2714                                 }
2715                         }
2716                 }
2717
2718                 if(remat_info->remats) del_pset(remat_info->remats);
2719                 if(remat_info->remats_by_operand) del_pset(remat_info->remats_by_operand);
2720         }
2721 }
2722
2723 static void
2724 delete_unnecessary_remats(spill_ilp_t * si)
2725 {
2726 #ifdef KEEPALIVE
2727         int       n;
2728         ir_node  *bad = get_irg_bad(si->chordal_env->irg);
2729
2730         if(si->keep) {
2731                 ir_node   *end = get_irg_end(si->chordal_env->irg);
2732                 ir_node  **keeps;
2733
2734                 for (n=get_irn_arity(si->keep)-1; n>=0; --n) {
2735                         ir_node        *keep_arg = get_irn_n(si->keep, n);
2736                         op_t           *arg_op = get_irn_link(keep_arg);
2737                         lpp_name_t     *name;
2738
2739                         assert(arg_op->is_remat);
2740
2741                         name = si->lpp->vars[arg_op->attr.remat.ilp];
2742
2743                         if(is_zero(name->value)) {
2744                                 DBG((si->dbg, LEVEL_3, "\t  deleting remat %+F\n", keep_arg));
2745                                 /* TODO check whether reload is preferred over remat (could be bug) */
2746                                 delete_remat(si, keep_arg);
2747                         } else {
2748                                 if(!arg_op->attr.remat.remat->inverse) {
2749                                         if(arg_op->attr.remat.pre) {
2750                                                 DBG((si->dbg, LEVEL_2, "\t**remat kept: %+F\n", keep_arg));
2751                                         } else {
2752                                                 DBG((si->dbg, LEVEL_2, "\t%%%%remat2 kept: %+F\n", keep_arg));
2753                                         }
2754                                 } else {
2755                                         if(arg_op->attr.remat.pre) {
2756                                                 DBG((si->dbg, LEVEL_2, "\t**INVERSE remat kept: %+F\n", keep_arg));
2757                                         } else {
2758                                                 DBG((si->dbg, LEVEL_2, "\t%%%%INVERSE remat2 kept: %+F\n", keep_arg));
2759                                         }
2760                                 }
2761                         }
2762
2763                         set_irn_n(si->keep, n, bad);
2764                 }
2765 #if 0
2766                 for (i = 0, n = get_End_n_keepalives(end); i < n; ++i) {
2767                         ir_node        *end_arg = get_End_keepalive(end, i);
2768
2769                         if(end_arg != si->keep) {
2770                                 obstack_grow(si->obst, &end_arg, sizeof(end_arg));
2771                         }
2772                 }
2773                 keeps = obstack_finish(si->obst);
2774                 set_End_keepalives(end, n-1, keeps);
2775                 obstack_free(si->obst, keeps);
2776 #endif
2777         } else {
2778                 DBG((si->dbg, LEVEL_2, "\t  no remats to delete (none have been inserted)\n"));
2779         }
2780 #else
2781         ir_node  *remat;
2782
2783         pset_foreach(si->all_possible_remats, remat) {
2784                 op_t           *remat_op = get_irn_link(remat);
2785                 lpp_name_t     *name = si->lpp->vars[remat_op->attr.remat.ilp];
2786
2787                 if(is_zero(name->value)) {
2788                         DBG((si->dbg, LEVEL_3, "\t  deleting remat %+F\n", remat));
2789                         /* TODO check whether reload is preferred over remat (could be bug) */
2790                         delete_remat(si, remat);
2791                 } else {
2792                         if(!remat_op->attr.remat.remat->inverse) {
2793                                 if(remat_op->attr.remat.pre) {
2794                                         DBG((si->dbg, LEVEL_2, "\t**remat kept: %+F\n", remat));
2795                                 } else {
2796                                         DBG((si->dbg, LEVEL_2, "\t%%%%remat2 kept: %+F\n", remat));
2797                                 }
2798                         } else {
2799                                 if(remat_op->attr.remat.pre) {
2800                                         DBG((si->dbg, LEVEL_2, "\t**INVERSE remat kept: %+F\n", remat));
2801                                 } else {
2802                                         DBG((si->dbg, LEVEL_2, "\t%%%%INVERSE remat2 kept: %+F\n", remat));
2803                                 }
2804                         }
2805                 }
2806         }
2807 #endif
2808 }
2809
2810 /**
2811  * @param before   The node after which the spill will be placed in the schedule
2812  */
2813 /* TODO set context properly */
2814 static ir_node *
2815 insert_spill(spill_ilp_t * si, ir_node * irn, ir_node * value, ir_node * before)
2816 {
2817         defs_t   *defs;
2818         ir_node  *spill;
2819         const arch_env_t *arch_env = si->chordal_env->birg->main_env->arch_env;
2820
2821         DBG((si->dbg, LEVEL_3, "\t  inserting spill for value %+F after %+F\n", irn, before));
2822
2823         spill = be_spill2(arch_env, irn, before, irn);
2824
2825         defs = set_insert_def(si->values, value);
2826         assert(defs);
2827
2828         /* enter into the linked list */
2829         set_irn_link(spill, defs->spills);
2830         defs->spills = spill;
2831
2832 #ifdef KEEPALIVE_SPILLS
2833         pset_insert_ptr(si->spills, spill);
2834 #endif
2835
2836         return spill;
2837 }
2838
2839 /**
2840  * @param before   The Phi node which has to be spilled
2841  */
2842 static ir_node *
2843 insert_mem_phi(spill_ilp_t * si, const ir_node * phi)
2844 {
2845         ir_node   *mem_phi;
2846         ir_node  **ins;
2847         defs_t    *defs;
2848         int        n;
2849
2850         NEW_ARR_A(ir_node*, ins, get_irn_arity(phi));
2851
2852         for(n=get_irn_arity(phi)-1; n>=0; --n) {
2853                 ins[n] = si->m_unknown;
2854         }
2855
2856         mem_phi =  new_r_Phi(si->chordal_env->irg, get_nodes_block(phi), get_irn_arity(phi), ins, mode_M);
2857
2858         defs = set_insert_def(si->values, phi);
2859         assert(defs);
2860
2861         /* enter into the linked list */
2862         set_irn_link(mem_phi, defs->spills);
2863         defs->spills = mem_phi;
2864
2865         sched_add_after(phi, mem_phi);
2866
2867 #ifdef KEEPALIVE_SPILLS
2868         pset_insert_ptr(si->spills, mem_phi);
2869 #endif
2870
2871         return mem_phi;
2872 }
2873
2874 /**
2875  * Add remat to list of defs, destroys link field!
2876  */
2877 static void
2878 insert_remat(spill_ilp_t * si, ir_node * remat)
2879 {
2880         defs_t   *defs;
2881         op_t     *remat_op = get_irn_link(remat);
2882
2883         assert(remat_op->is_remat);
2884
2885         defs = set_insert_def(si->values, remat_op->attr.remat.remat->value);
2886         assert(defs);
2887
2888         /* enter into the linked list */
2889         set_irn_link(remat, defs->remats);
2890         defs->remats = remat;
2891 }
2892
2893 #if 0
2894 static void
2895 collect_spills(spill_ilp_t * si, ir_node * value, pset * spills, pset * visited)
2896 {
2897         ir_node  *next;
2898         defs_t   *defs;
2899
2900         defs = set_find_def(si->values, value);
2901
2902         if(defs && defs->spills) {
2903                 for(next = defs->spills; next; next = get_irn_link(next)) {
2904                         pset_insert_ptr(spills, next);
2905                 }
2906         } else if (is_Phi(value)) {
2907                 /* recursion */
2908                 if(!pset_find_ptr(visited, value)) {
2909                         int    i,
2910                                    n;
2911
2912                         pset_insert_ptr(visited, value);
2913                         for(i=0, n=get_irn_arity(value); i<n; ++i) {
2914                                 ir_node    *arg = get_irn_n(value, i);
2915
2916                                 collect_spills(si, arg, spills, visited);
2917                         }
2918                 }
2919         } else {
2920 //              assert(0 && "Phi operand not spilled");
2921         }
2922 }
2923 #endif
2924
2925 static pset *
2926 get_spills_for_value(spill_ilp_t * si, ir_node * value)
2927 {
2928         pset     *spills = pset_new_ptr_default();
2929 //      pset     *visited = pset_new_ptr_default();
2930
2931 //      collect_spills(si, value, spills, visited);
2932 //      del_pset(visited);
2933         ir_node  *next;
2934         defs_t   *defs;
2935
2936         defs = set_find_def(si->values, value);
2937
2938         if(defs && defs->spills) {
2939                 for(next = defs->spills; next; next = get_irn_link(next)) {
2940                         pset_insert_ptr(spills, next);
2941                 }
2942         }
2943
2944         return spills;
2945 }
2946
2947 /**
2948  * Add reload before operation and add to list of defs
2949  */
2950 static ir_node *
2951 insert_reload(spill_ilp_t * si, const ir_node * value, const ir_node * after)
2952 {
2953         defs_t   *defs;
2954         ir_node  *reload,
2955                          *spill;
2956         const arch_env_t *arch_env = si->chordal_env->birg->main_env->arch_env;
2957
2958         DBG((si->dbg, LEVEL_3, "\t  inserting reload for value %+F before %+F\n", value, after));
2959
2960         defs = set_find_def(si->values, value);
2961         /* get a spill of this value */
2962 #if 0
2963         if((!defs || !defs->spills) && is_Phi(value)) {
2964                 pset  *spills;
2965
2966                 spills = get_spills_for_value(si, value);
2967
2968                 spill = pset_first(spills);
2969                 del_pset(spills);
2970
2971                 if(!defs) {
2972                         defs = set_insert_def(si->values, value);
2973                 }
2974                 defs->spills = spill;
2975                 set_irn_link(spill, NULL);
2976         } else {
2977                 spill = defs->spills;
2978         }
2979 #endif
2980         spill = defs->spills;
2981         assert(spill && "no spill placed before reload");
2982
2983         reload = be_reload(arch_env, si->cls, after, get_irn_mode(value), spill);
2984
2985         /* enter into the linked list */
2986         set_irn_link(reload, defs->remats);
2987         defs->remats = reload;
2988
2989         return reload;
2990 }
2991
2992 static void
2993 walker_spill_placer(ir_node * bb, void * data) {
2994         spill_ilp_t   *si = (spill_ilp_t*)data;
2995         ir_node       *irn;
2996         spill_bb_t    *spill_bb = get_irn_link(bb);
2997         pset          *spills_to_do = pset_new_ptr_default();
2998         spill_t       *spill;
2999
3000         set_foreach(spill_bb->ilp, spill) {
3001                 lpp_name_t    *name;
3002
3003                 if(is_Phi(spill->irn) && get_nodes_block(spill->irn) == bb) {
3004                         name = si->lpp->vars[spill->mem_in];
3005                         if(!is_zero(name->value)) {
3006                                 ir_node   *mem_phi;
3007
3008                                 mem_phi = insert_mem_phi(si, spill->irn);
3009
3010                                 DBG((si->dbg, LEVEL_2, "\t >>spilled Phi %+F -> %+F\n", spill->irn, mem_phi));
3011                         }
3012                 }
3013
3014                 name = si->lpp->vars[spill->spill];
3015                 if(!is_zero(name->value)) {
3016                         if(spill->reg_in > 0) {
3017                                 name = si->lpp->vars[spill->reg_in];
3018                                 if(!is_zero(name->value)) {
3019                                         insert_spill(si, spill->irn, spill->irn, bb);
3020                                         continue;
3021                                 }
3022                         }
3023                         pset_insert_ptr(spills_to_do, spill->irn);
3024                 }
3025         }
3026         DBG((si->dbg, LEVEL_3, "\t  %d spills to do in block %+F\n", pset_count(spills_to_do), bb));
3027
3028
3029         for(irn = sched_block_first_nonphi(bb); !sched_is_end(irn); irn = sched_next(irn)) {
3030                 op_t     *op = get_irn_link(irn);
3031
3032                 if(be_is_Spill(irn)) continue;
3033
3034                 if(op->is_remat) {
3035                         /* TODO fix this if we want to support remats with more than two nodes */
3036                         if(get_irn_mode(irn) != mode_T && pset_find_ptr(spills_to_do, op->attr.remat.remat->value)) {
3037                                 pset_remove_ptr(spills_to_do, op->attr.remat.remat->value);
3038
3039                                 insert_spill(si, irn, op->attr.remat.remat->value, irn);
3040                         }
3041                 } else {
3042                         if(pset_find_ptr(spills_to_do, irn)) {
3043                                 pset_remove_ptr(spills_to_do, irn);
3044
3045                                 insert_spill(si, irn, irn, irn);
3046                         }
3047                 }
3048
3049         }
3050
3051         assert(pset_count(spills_to_do) == 0);
3052
3053         /* afterwards free data in block */
3054         del_pset(spills_to_do);
3055 }
3056
3057 static void
3058 phim_fixer(spill_ilp_t *si) {
3059         defs_t  *defs;
3060
3061         set_foreach(si->values, defs) {
3062                 const ir_node  *phi = defs->value;
3063                 ir_node  *phi_m = NULL;
3064                 ir_node  *next = defs->spills;
3065                 int       n;
3066
3067                 if(!is_Phi(phi)) continue;
3068
3069                 while(next) {
3070                         if(is_Phi(next) && get_irn_mode(next) == mode_M) {
3071                                 phi_m = next;
3072                                 break;
3073                         } else {
3074                                 next = get_irn_link(next);
3075                         }
3076                 }
3077                 if(!phi_m) continue;
3078
3079                 for(n=get_irn_arity(phi)-1; n>=0; --n) {
3080                         const ir_node  *value = get_irn_n(phi, n);
3081                         defs_t         *val_defs = set_find_def(si->values, value);
3082
3083                         /* get a spill of this value */
3084                         ir_node      *spill = val_defs->spills;
3085
3086                         assert(spill && "no spill placed before PhiM");
3087
3088                         set_irn_n(phi_m, n, spill);
3089                 }
3090         }
3091 }
3092
3093 static void
3094 walker_reload_placer(ir_node * bb, void * data) {
3095         spill_ilp_t   *si = (spill_ilp_t*)data;
3096         ir_node       *irn;
3097         spill_bb_t    *spill_bb = get_irn_link(bb);
3098         int            i;
3099         irn_live_t    *li;
3100
3101         sched_foreach_reverse(bb, irn) {
3102                 op_t     *op = get_irn_link(irn);
3103
3104                 if(be_is_Reload(irn) || be_is_Spill(irn)) continue;
3105                 if(is_Phi(irn)) break;
3106
3107                 if(op->is_remat) {
3108                         if(get_irn_mode(irn) != mode_T) {
3109                                 insert_remat(si, irn);
3110                         }
3111                 } else {
3112                         int    n;
3113
3114                         for (n=get_irn_arity(irn)-1; n>=0; --n) {
3115                                 ir_node    *arg = get_irn_n(irn, n);
3116
3117                                 if(op->attr.live_range.args.reloads && op->attr.live_range.args.reloads[n] != ILP_UNDEF) {
3118                                         lpp_name_t    *name;
3119
3120                                         name = si->lpp->vars[op->attr.live_range.args.reloads[n]];
3121                                         if(!is_zero(name->value)) {
3122                                                 ir_node    *reload;
3123                                                 ir_node    *insert_pos = irn;
3124                                                 ir_node    *prev = insert_pos;
3125                                                 op_t       *prev_op;
3126
3127                                                 do {
3128                                                         prev = sched_prev(prev);
3129                                                 } while(be_is_Spill(prev));
3130
3131                                                 prev_op = get_irn_link(prev);
3132
3133                                                 /* insert reload before pre-remats */
3134                                                 while(!sched_is_end(prev) && !be_is_Reload(prev) && !is_Phi(prev)
3135                                                                 && prev_op->is_remat && prev_op->attr.remat.pre) {
3136                                                         insert_pos = prev;
3137
3138                                                         do {
3139                                                                 prev = sched_prev(prev);
3140                                                         } while(be_is_Spill(prev));
3141
3142                                                         prev_op = get_irn_link(prev);
3143
3144                                                 }
3145
3146                                                 reload = insert_reload(si, arg, insert_pos);
3147
3148                                                 set_irn_n(irn, n, reload);
3149
3150 #ifdef KEEPALIVE_RELOADS
3151                                                 pset_insert_ptr(si->spills, reload);
3152 #endif
3153                                         }
3154                                 }
3155                         }
3156                 }
3157         }
3158
3159         /* reloads at end of block */
3160         if(spill_bb->reloads) {
3161                 keyval_t    *keyval;
3162
3163                 set_foreach(spill_bb->reloads, keyval) {
3164                         ir_node        *irn = (ir_node*)keyval->key;
3165                         ilp_var_t       reload = PTR_TO_INT(keyval->val);
3166                         lpp_name_t     *name;
3167
3168                         name = si->lpp->vars[reload];
3169                         if(!is_zero(name->value)) {
3170                                 ir_node    *reload;
3171                                 ir_node    *insert_pos = bb;
3172                                 ir_node    *prev = sched_prev(insert_pos);
3173                                 op_t       *prev_op = get_irn_link(prev);
3174
3175                                 /* insert reload before pre-remats */
3176                                 while(!sched_is_end(prev) && !be_is_Reload(prev) && !be_is_Spill(prev)
3177                                                 && prev_op->is_remat && prev_op->attr.remat.pre) {
3178                                         insert_pos = prev;
3179
3180                                         prev = sched_prev(insert_pos);
3181                                         prev_op = get_irn_link(prev);
3182                                 }
3183
3184                                 reload = insert_reload(si, irn, insert_pos);
3185
3186 #ifdef KEEPALIVE_RELOADS
3187                                 pset_insert_ptr(si->spills, reload);
3188 #endif
3189                         }
3190                 }
3191         }
3192
3193         del_set(spill_bb->ilp);
3194         if(spill_bb->reloads) del_set(spill_bb->reloads);
3195 }
3196
3197 static void
3198 walker_collect_used(ir_node * irn, void * data)
3199 {
3200         lc_bitset_t   *used = data;
3201
3202         lc_bitset_set(used, get_irn_idx(irn));
3203 }
3204
3205 struct kill_helper {
3206         lc_bitset_t  *used;
3207         spill_ilp_t  *si;
3208 };
3209
3210 static void
3211 walker_kill_unused(ir_node * bb, void * data)
3212 {
3213         struct kill_helper *kh = data;
3214         const ir_node      *bad = get_irg_bad(get_irn_irg(bb));
3215         ir_node            *irn;
3216
3217
3218         for(irn=sched_first(bb); !sched_is_end(irn);) {
3219                 ir_node     *next = sched_next(irn);
3220                 int          n;
3221
3222                 if(!lc_bitset_is_set(kh->used, get_irn_idx(irn))) {
3223                         if(be_is_Spill(irn) || be_is_Reload(irn)) {
3224                                 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)));
3225 #if !defined(KEEPALIVE_SPILLS) && !defined(KEEPALIVE_RELOADS)
3226                                 assert(lpp_get_sol_state(kh->si->lpp) != lpp_optimal && "optimal solution is suboptimal?");
3227 #endif
3228                         }
3229
3230                         sched_remove(irn);
3231
3232                         set_nodes_block(irn, bad);
3233                         for (n=get_irn_arity(irn)-1; n>=0; --n) {
3234                                 set_irn_n(irn, n, bad);
3235                         }
3236                 }
3237                 irn = next;
3238         }
3239 }
3240
3241 static void
3242 kill_all_unused_values_in_schedule(spill_ilp_t * si)
3243 {
3244         struct kill_helper kh;
3245
3246         kh.used = lc_bitset_malloc(get_irg_last_idx(si->chordal_env->irg));
3247         kh.si = si;
3248
3249         irg_walk_graph(si->chordal_env->irg, walker_collect_used, NULL, kh.used);
3250         irg_block_walk_graph(si->chordal_env->irg, walker_kill_unused, NULL, &kh);
3251
3252         lc_bitset_free(kh.used);
3253 }
3254
3255 static void
3256 print_irn_pset(pset * p)
3257 {
3258         ir_node   *irn;
3259
3260         pset_foreach(p, irn) {
3261                 ir_printf("%+F\n", irn);
3262         }
3263 }
3264
3265 static void
3266 rewire_uses(spill_ilp_t * si)
3267 {
3268         dom_front_info_t     *dfi = be_compute_dominance_frontiers(si->chordal_env->irg);
3269         defs_t               *defs;
3270         pset                 *ignore = pset_new_ptr(1);
3271
3272         pset_insert_ptr(ignore, get_irg_end(si->chordal_env->irg));
3273
3274         /* then fix uses of spills */
3275         set_foreach(si->values, defs) {
3276                 pset     *reloads;
3277                 pset     *spills;
3278                 ir_node  *next = defs->remats;
3279                 int remats = 0;
3280
3281                 reloads = pset_new_ptr_default();
3282
3283                 while(next) {
3284                         if(be_is_Reload(next)) {
3285                                 pset_insert_ptr(reloads, next);
3286                         } else {
3287                                 ++remats;
3288                         }
3289                         next = get_irn_link(next);
3290                 }
3291
3292                 spills = get_spills_for_value(si, defs->value);
3293                 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));
3294                 if(pset_count(spills) > 1) {
3295                         //assert(pset_count(reloads) > 0);
3296                         //                              print_irn_pset(spills);
3297                         //                              print_irn_pset(reloads);
3298
3299                         be_ssa_constr_set_ignore(dfi, spills, ignore);
3300                 }
3301
3302                 del_pset(reloads);
3303                 del_pset(spills);
3304         }
3305
3306         /* first fix uses of remats and reloads */
3307         set_foreach(si->values, defs) {
3308                 pset     *nodes;
3309                 ir_node  *next = defs->remats;
3310
3311                 if(next) {
3312                         nodes = pset_new_ptr_default();
3313                         pset_insert_ptr(nodes, defs->value);
3314
3315                         while(next) {
3316                                 pset_insert_ptr(nodes, next);
3317                                 next = get_irn_link(next);
3318                         }
3319
3320                         if(pset_count(nodes) > 1) {
3321                                 DBG((si->dbg, LEVEL_4, "\t    %d new definitions for value %+F\n", pset_count(nodes)-1, defs->value));
3322                                 be_ssa_constr_set(dfi, nodes);
3323                         }
3324
3325                         del_pset(nodes);
3326                 }
3327         }
3328
3329 //      remove_unused_defs(si);
3330
3331         be_free_dominance_frontiers(dfi);
3332 }
3333
3334 static void
3335 writeback_results(spill_ilp_t * si)
3336 {
3337         /* walk through the graph and collect all spills, reloads and remats for a value */
3338
3339         si->values = new_set(cmp_defs, 4096);
3340
3341         DBG((si->dbg, LEVEL_1, "Applying results\n"));
3342         delete_unnecessary_remats(si);
3343         si->m_unknown = new_r_Unknown(si->chordal_env->irg, mode_M);
3344         irg_block_walk_graph(si->chordal_env->irg, walker_spill_placer, NULL, si);
3345         phim_fixer(si);
3346         irg_block_walk_graph(si->chordal_env->irg, walker_reload_placer, NULL, si);
3347
3348         /* clean the remat info! there are still back-edges leading there! */
3349         clean_remat_info(si);
3350
3351         rewire_uses(si);
3352
3353         connect_all_spills_with_keep(si);
3354
3355         del_set(si->values);
3356 }
3357
3358 static int
3359 get_n_regs(spill_ilp_t * si)
3360 {
3361         int     arch_n_regs = arch_register_class_n_regs(si->cls);
3362         int     free = 0;
3363         int     i;
3364
3365         for(i=0; i<arch_n_regs; i++) {
3366                 if(!arch_register_type_is(&si->cls->regs[i], ignore)) {
3367                         free++;
3368                 }
3369         }
3370
3371         DBG((si->dbg, LEVEL_1, "\tArchitecture has %d free registers in class %s\n", free, si->cls->name));
3372         return free;
3373 }
3374
3375 static void
3376 walker_reload_mover(ir_node * bb, void * data)
3377 {
3378         spill_ilp_t   *si = data;
3379         ir_node           *tmp;
3380
3381         sched_foreach(bb, tmp) {
3382                 if(be_is_Reload(tmp) && has_reg_class(si, tmp)) {
3383                         ir_node       *reload = tmp;
3384                         ir_node       *irn = tmp;
3385
3386                         /* move reload upwards */
3387
3388                         int pressure = (int)get_irn_link(reload);
3389                         if(pressure < si->n_regs) {
3390                                 irn = sched_prev(reload);
3391                                 DBG((si->dbg, LEVEL_5, "regpressure before %+F: %d\n", reload, pressure));
3392                                 sched_remove(reload);
3393                                 pressure = (int)get_irn_link(irn);
3394
3395                                 while(pressure < si->n_regs) {
3396                                         if(sched_is_end(irn) || (be_is_Reload(irn) && has_reg_class(si, irn))) break;
3397
3398                                         set_irn_link(irn, INT_TO_PTR(pressure+1));
3399                                         DBG((si->dbg, LEVEL_5, "new regpressure before %+F: %d\n", irn, pressure+1));
3400                                         irn = sched_prev(irn);
3401
3402                                         pressure = (int)get_irn_link(irn);
3403                                 }
3404
3405                                 DBG((si->dbg, LEVEL_3, "putting reload %+F after %+F\n", reload, irn));
3406                                 sched_put_after(irn, reload);
3407                         }
3408                 }
3409         }
3410 }
3411
3412 static void
3413 move_reloads_upward(spill_ilp_t * si)
3414 {
3415         irg_block_walk_graph(si->chordal_env->irg, walker_reload_mover, NULL, si);
3416 }
3417
3418 void
3419 be_spill_remat(const be_chordal_env_t * chordal_env)
3420 {
3421         char            problem_name[256];
3422         char            dump_suffix[256];
3423         char            dump_suffix2[256];
3424         char            dump_suffix3[256];
3425         struct obstack  obst;
3426         spill_ilp_t     si;
3427
3428         ir_snprintf(problem_name, sizeof(problem_name), "%F_%s", chordal_env->irg, chordal_env->cls->name);
3429         ir_snprintf(dump_suffix, sizeof(dump_suffix), "-%s-remats", chordal_env->cls->name);
3430         ir_snprintf(dump_suffix2, sizeof(dump_suffix2), "-%s-pressure", chordal_env->cls->name);
3431
3432         FIRM_DBG_REGISTER(si.dbg, "firm.be.ra.spillremat");
3433         DBG((si.dbg, LEVEL_1, "\n\n\t\t===== Processing %s =====\n\n", problem_name));
3434
3435         obstack_init(&obst);
3436         si.chordal_env = chordal_env;
3437         si.obst = &obst;
3438         si.cls = chordal_env->cls;
3439         si.lpp = new_lpp(problem_name, lpp_minimize);
3440         si.remat_info = new_set(cmp_remat_info, 4096);
3441         si.interferences = new_set(cmp_interference, 4096);
3442         si.all_possible_remats = pset_new_ptr_default();
3443         si.spills = pset_new_ptr_default();
3444         si.inverse_ops = pset_new_ptr_default();
3445 #ifndef EXECFREQ_LOOPDEPH
3446         si.execfreqs = compute_execfreq(chordal_env->irg);
3447 #else
3448         si.execfreqs = NULL;
3449 #endif
3450 #ifdef KEEPALIVE
3451         si.keep = NULL;
3452 #endif
3453         si.n_regs = get_n_regs(&si);
3454
3455         set_irg_link(chordal_env->irg, &si);
3456         compute_doms(chordal_env->irg);
3457
3458         /* compute phi classes */
3459 //      phi_class_compute(chordal_env->irg);
3460
3461         be_analyze_regpressure(chordal_env, "-pre");
3462
3463 #ifdef COLLECT_REMATS
3464         /* collect remats */
3465         DBG((si.dbg, LEVEL_1, "Collecting remats\n"));
3466         irg_walk_graph(chordal_env->irg, walker_remat_collector, NULL, &si);
3467 #endif
3468
3469         /* insert possible remats */
3470         DBG((si.dbg, LEVEL_1, "Inserting possible remats\n"));
3471         irg_block_walk_graph(chordal_env->irg, walker_remat_insertor, NULL, &si);
3472         DBG((si.dbg, LEVEL_2, " -> inserted %d possible remats\n", pset_count(si.all_possible_remats)));
3473
3474 #ifdef KEEPALIVE
3475         DBG((si.dbg, LEVEL_1, "Connecting remats with keep and dumping\n"));
3476         connect_all_remats_with_keep(&si);
3477         /* dump graph with inserted remats */
3478         dump_graph_with_remats(chordal_env->irg, dump_suffix);
3479 #endif
3480
3481
3482         /* recompute liveness */
3483         DBG((si.dbg, LEVEL_1, "Recomputing liveness\n"));
3484         be_liveness(chordal_env->irg);
3485
3486         /* build the ILP */
3487
3488         DBG((si.dbg, LEVEL_1, "\tBuilding ILP\n"));
3489         DBG((si.dbg, LEVEL_2, "\t endwalker\n"));
3490         irg_block_walk_graph(chordal_env->irg, luke_endwalker, NULL, &si);
3491
3492         DBG((si.dbg, LEVEL_2, "\t blockwalker\n"));
3493         irg_block_walk_graph(chordal_env->irg, luke_blockwalker, NULL, &si);
3494
3495 #ifndef NO_MEMCOPIES
3496         DBG((si.dbg, LEVEL_2, "\t memcopyhandler\n"));
3497         memcopyhandler(&si);
3498 #endif
3499
3500 #ifdef DUMP_ILP
3501         {
3502                 FILE           *f;
3503                 char            buf[256];
3504
3505                 ir_snprintf(buf, sizeof(buf), "%s-spillremat.ilp", problem_name);
3506                 if ((f = fopen(buf, "wt")) != NULL) {
3507                         lpp_dump_plain(si.lpp, f);
3508                         fclose(f);
3509                 }
3510         }
3511 #endif
3512
3513 #ifdef SOLVE
3514         DBG((si.dbg, LEVEL_1, "\tSolving %F\n", chordal_env->irg));
3515 #ifdef ILP_TIMEOUT
3516         lpp_set_time_limit(si.lpp, ILP_TIMEOUT);
3517 #endif
3518
3519 #ifdef SOLVE_LOCAL
3520         lpp_solve_cplex(si.lpp);
3521 #else
3522         lpp_solve_net(si.lpp, LPP_SERVER, LPP_SOLVER);
3523 #endif
3524         assert(lpp_is_sol_valid(si.lpp)
3525                && "solution of ILP must be valid");
3526
3527         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));
3528
3529 #ifdef DUMP_SOLUTION
3530         {
3531                 FILE           *f;
3532                 char            buf[256];
3533
3534                 ir_snprintf(buf, sizeof(buf), "%s-spillremat.sol", problem_name);
3535                 if ((f = fopen(buf, "wt")) != NULL) {
3536                         int             i;
3537                         for (i = 0; i < si.lpp->var_next; ++i) {
3538                                 lpp_name_t     *name = si.lpp->vars[i];
3539                                 fprintf(f, "%20s %4d %10f\n", name->name, name->nr, name->value);
3540                         }
3541                         fclose(f);
3542                 }
3543         }
3544 #endif
3545
3546         writeback_results(&si);
3547
3548 #endif                          /* SOLVE */
3549
3550         kill_all_unused_values_in_schedule(&si);
3551
3552 #if defined(KEEPALIVE_SPILLS) || defined(KEEPALIVE_RELOADS)
3553         be_dump(chordal_env->irg, "-spills-placed", dump_ir_block_graph);
3554 #endif
3555
3556         // move reloads upwards
3557         be_liveness(chordal_env->irg);
3558         irg_block_walk_graph(chordal_env->irg, walker_pressure_annotator, NULL, &si);
3559         move_reloads_upward(&si);
3560
3561         irg_block_walk_graph(chordal_env->irg, walker_pressure_annotator, NULL, &si);
3562
3563         dump_pressure_graph(&si, dump_suffix2);
3564
3565         // TODO fix temporarily exceeded regpressure due to remat2s
3566
3567         // TODO insert copys to fix interferences in memory
3568
3569         be_analyze_regpressure(chordal_env, "-post");
3570
3571         free_dom(chordal_env->irg);
3572         del_set(si.interferences);
3573         del_pset(si.inverse_ops);
3574         del_pset(si.all_possible_remats);
3575         del_pset(si.spills);
3576 #ifndef EXECFREQ_LOOPDEPH
3577         free_execfreq(si.execfreqs);
3578 #endif
3579         free_lpp(si.lpp);
3580         obstack_free(&obst, NULL);
3581         DBG((si.dbg, LEVEL_1, "\tdone.\n"));
3582 }
3583
3584 #else                           /* WITH_ILP */
3585
3586 static void
3587 only_that_you_can_compile_without_WITH_ILP_defined(void)
3588 {
3589 }
3590
3591 #endif                          /* WITH_ILP */