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