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