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