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