ff62102c02df4048ae83ec3e1694f19f9616c28d
[libfirm] / ir / be / bespill.c
1 /*
2  * Copyright (C) 1995-2008 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 /**
21  * @file
22  * @brief       implementation of the spill/reload placement abstraction layer
23  * @author      Daniel Grund, Sebastian Hack, Matthias Braun
24  * @date                29.09.2005
25  * @version     $Id$
26  */
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30
31 #include <stdlib.h>
32
33 #include "pset.h"
34 #include "irnode_t.h"
35 #include "ircons_t.h"
36 #include "iredges_t.h"
37 #include "irbackedge_t.h"
38 #include "irprintf.h"
39 #include "ident_t.h"
40 #include "type_t.h"
41 #include "entity_t.h"
42 #include "debug.h"
43 #include "irgwalk.h"
44 #include "array.h"
45 #include "pdeq.h"
46 #include "execfreq.h"
47 #include "irnodeset.h"
48 #include "error.h"
49
50 #include "bearch_t.h"
51 #include "belive_t.h"
52 #include "besched_t.h"
53 #include "bespill.h"
54 #include "belive_t.h"
55 #include "benode_t.h"
56 #include "bechordal_t.h"
57 #include "bejavacoal.h"
58 #include "bespilloptions.h"
59 #include "bestatevent.h"
60 #include "bessaconstr.h"
61 #include "beirg_t.h"
62 #include "beintlive_t.h"
63 #include "bemodule.h"
64
65 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
66
67 #define REMAT_COST_INFINITE  1000
68
69 typedef struct reloader_t reloader_t;
70 struct reloader_t {
71         reloader_t *next;
72         ir_node    *can_spill_after;
73         ir_node    *reloader;
74         ir_node    *rematted_node;
75         int         remat_cost_delta; /** costs needed for rematerialization,
76                                            compared to placing a reload */
77 };
78
79 typedef struct spill_t spill_t;
80 struct spill_t {
81         spill_t *next;
82         ir_node *before;   /**< spill has to be placed before this node (or earlier) */
83         ir_node *spill;
84 };
85
86 typedef struct spill_info_t spill_info_t;
87 struct spill_info_t {
88         ir_node    *to_spill;  /**< the value that should get spilled */
89         reloader_t *reloaders; /**< list of places where the value should get
90                                     reloaded */
91         spill_t    *spills;    /**< list of latest places where spill must be
92                                     placed */
93         double      spill_costs; /**< costs needed for spilling the value */
94         const arch_register_class_t *reload_cls; /** the register class in which the
95                                                      reload should be placed */
96 };
97
98 struct spill_env_t {
99         const arch_env_t *arch_env;
100         ir_graph         *irg;
101         struct obstack    obst;
102         be_irg_t         *birg;
103         int               spill_cost;     /**< the cost of a single spill node */
104         int               reload_cost;    /**< the cost of a reload node */
105         set              *spills;         /**< all spill_info_t's, which must be
106                                                placed */
107         ir_nodeset_t      mem_phis;       /**< set of all spilled phis. */
108         ir_exec_freq     *exec_freq;
109         unsigned          new_nodes_idx;  /**< all old nodes idx is smaller than
110                                                this */
111
112 #ifdef FIRM_STATISTICS
113         unsigned          spill_count;
114         unsigned          reload_count;
115         unsigned          remat_count;
116         unsigned          spilled_phi_count;
117 #endif
118 };
119
120 /**
121  * Compare two spill infos.
122  */
123 static int cmp_spillinfo(const void *x, const void *y, size_t size)
124 {
125         const spill_info_t *xx = x;
126         const spill_info_t *yy = y;
127         (void) size;
128
129         return xx->to_spill != yy->to_spill;
130 }
131
132 /**
133  * Returns spill info for a specific value (the value that is to be spilled)
134  */
135 static spill_info_t *get_spillinfo(const spill_env_t *env, ir_node *value)
136 {
137         spill_info_t info, *res;
138         int hash = hash_irn(value);
139
140         info.to_spill = value;
141         res = set_find(env->spills, &info, sizeof(info), hash);
142
143         if (res == NULL) {
144                 info.reloaders   = NULL;
145                 info.spills      = NULL;
146                 info.spill_costs = -1;
147                 info.reload_cls  = NULL;
148                 res = set_insert(env->spills, &info, sizeof(info), hash);
149         }
150
151         return res;
152 }
153
154 spill_env_t *be_new_spill_env(be_irg_t *birg)
155 {
156         const arch_env_t *arch_env = birg->main_env->arch_env;
157
158         spill_env_t *env        = xmalloc(sizeof(env[0]));
159         env->spills                     = new_set(cmp_spillinfo, 1024);
160         env->irg            = be_get_birg_irg(birg);
161         env->birg           = birg;
162         env->arch_env       = arch_env;
163         ir_nodeset_init(&env->mem_phis);
164         env->spill_cost     = arch_env->isa->spill_cost;
165         env->reload_cost    = arch_env->isa->reload_cost;
166         env->exec_freq      = be_get_birg_exec_freq(birg);
167         obstack_init(&env->obst);
168
169 #ifdef FIRM_STATISTICS
170         env->spill_count       = 0;
171         env->reload_count      = 0;
172         env->remat_count       = 0;
173         env->spilled_phi_count = 0;
174 #endif
175
176         return env;
177 }
178
179 void be_delete_spill_env(spill_env_t *env)
180 {
181         del_set(env->spills);
182         ir_nodeset_destroy(&env->mem_phis);
183         obstack_free(&env->obst, NULL);
184         free(env);
185 }
186
187 /*
188  *  ____  _                  ____      _                 _
189  * |  _ \| | __ _  ___ ___  |  _ \ ___| | ___   __ _  __| |___
190  * | |_) | |/ _` |/ __/ _ \ | |_) / _ \ |/ _ \ / _` |/ _` / __|
191  * |  __/| | (_| | (_|  __/ |  _ <  __/ | (_) | (_| | (_| \__ \
192  * |_|   |_|\__,_|\___\___| |_| \_\___|_|\___/ \__,_|\__,_|___/
193  *
194  */
195
196 void be_add_spill(spill_env_t *env, ir_node *to_spill, ir_node *before)
197 {
198 #if 1
199         spill_info_t *spill_info = get_spillinfo(env, to_spill);
200         spill_t      *spill;
201         spill_t      *s;
202         spill_t      *last;
203
204         assert(! arch_irn_is(env->arch_env, to_spill, dont_spill));
205         DB((dbg, LEVEL_1, "Add spill of %+F before %+F\n", to_spill, before));
206
207         /* spills that are dominated by others are not needed */
208         last = NULL;
209         s    = spill_info->spills;
210         for( ; s != NULL; s = s->next) {
211                 /* no need to add this spill if it is dominated by another */
212                 if(value_dominates(s->before, before)) {
213                         DB((dbg, LEVEL_1, "...dominated by %+F, not added\n", s->before));
214                         return;
215                 }
216                 /* remove spills that we dominate */
217                 if(value_dominates(before, s->before)) {
218                         DB((dbg, LEVEL_1, "...remove old spill at %+F\n", s->before));
219                         if(last != NULL) {
220                                 last->next         = s->next;
221                         } else {
222                                 spill_info->spills = s->next;
223                         }
224                 } else {
225                         last = s;
226                 }
227         }
228
229         spill         = obstack_alloc(&env->obst, sizeof(spill[0]));
230         spill->before = before;
231         spill->next   = spill_info->spills;
232         spill->spill  = NULL;
233
234         spill_info->spills = spill;
235 #endif
236 }
237
238 void be_add_remat(spill_env_t *env, ir_node *to_spill, ir_node *before,
239                   ir_node *rematted_node)
240 {
241         spill_info_t *spill_info;
242         reloader_t *reloader;
243
244         spill_info = get_spillinfo(env, to_spill);
245
246         /* add the remat information */
247         reloader                   = obstack_alloc(&env->obst, sizeof(reloader[0]));
248         reloader->next             = spill_info->reloaders;
249         reloader->reloader         = before;
250         reloader->rematted_node    = rematted_node;
251         reloader->remat_cost_delta = 0; /* We will never have a cost win over a
252                                            reload since we're not even allowed to
253                                            create a reload */
254
255         spill_info->reloaders  = reloader;
256
257         DBG((dbg, LEVEL_1, "creating spillinfo for %+F, will be rematerialized before %+F\n",
258                 to_spill, before));
259 }
260
261 void be_add_reload2(spill_env_t *env, ir_node *to_spill, ir_node *before,
262                 ir_node *can_spill_after, const arch_register_class_t *reload_cls,
263                 int allow_remat)
264 {
265         spill_info_t *info;
266         reloader_t *rel;
267
268         assert(! arch_irn_is(env->arch_env, to_spill, dont_spill));
269
270         info = get_spillinfo(env, to_spill);
271
272         if (is_Phi(to_spill)) {
273                 int i, arity;
274
275                 /* create spillinfos for the phi arguments */
276                 for (i = 0, arity = get_irn_arity(to_spill); i < arity; ++i) {
277                         ir_node *arg = get_irn_n(to_spill, i);
278                         get_spillinfo(env, arg);
279                 }
280         }
281
282         assert(!is_Proj(before) && !be_is_Keep(before));
283
284         /* put reload into list */
285         rel                   = obstack_alloc(&env->obst, sizeof(rel[0]));
286         rel->next             = info->reloaders;
287         rel->reloader         = before;
288         rel->rematted_node    = NULL;
289         rel->can_spill_after  = can_spill_after;
290         rel->remat_cost_delta = allow_remat ? 0 : REMAT_COST_INFINITE;
291
292         info->reloaders  = rel;
293         assert(info->reload_cls == NULL || info->reload_cls == reload_cls);
294         info->reload_cls = reload_cls;
295
296         DBG((dbg, LEVEL_1, "creating spillinfo for %+F, will be reloaded before %+F, may%s be rematerialized\n",
297                 to_spill, before, allow_remat ? "" : " not"));
298 }
299
300 void be_add_reload(spill_env_t *senv, ir_node *to_spill, ir_node *before,
301                    const arch_register_class_t *reload_cls, int allow_remat)
302 {
303         be_add_reload2(senv, to_spill, before, to_spill, reload_cls, allow_remat);
304
305 }
306
307 ir_node *be_get_end_of_block_insertion_point(const ir_node *block)
308 {
309         ir_node *last = sched_last(block);
310
311         /* we might have keeps behind the jump... */
312         while(be_is_Keep(last)) {
313                 last = sched_prev(last);
314                 assert(!sched_is_end(last));
315         }
316
317         assert(is_cfop(last));
318
319         /* add the reload before the (cond-)jump */
320         return last;
321 }
322
323 static ir_node *skip_keeps_phis(ir_node *node)
324 {
325         node = sched_next(node);
326         while(is_Phi(node) || be_is_Keep(node)) {
327                 node = sched_next(node);
328         }
329         return node;
330 }
331
332 /**
333  * Returns the point at which you can insert a node that should be executed
334  * before block @p block when coming from pred @p pos.
335  */
336 static ir_node *get_block_insertion_point(ir_node *block, int pos)
337 {
338         ir_node *predblock;
339
340         /* simply add the reload to the beginning of the block if we only have 1
341          * predecessor. We don't need to check for phis as there can't be any in a
342          * block with only 1 pred. */
343         if(get_Block_n_cfgpreds(block) == 1) {
344                 assert(!is_Phi(sched_first(block)));
345                 return sched_first(block);
346         }
347
348         /* We have to reload the value in pred-block */
349         predblock = get_Block_cfgpred_block(block, pos);
350         return be_get_end_of_block_insertion_point(predblock);
351 }
352
353 void be_add_reload_at_end(spill_env_t *env, ir_node *to_spill,
354                           const ir_node *block,
355                           const arch_register_class_t *reload_cls,
356                           int allow_remat)
357 {
358         ir_node *before = be_get_end_of_block_insertion_point(block);
359         be_add_reload(env, to_spill, before, reload_cls, allow_remat);
360 }
361
362 void be_add_reload_on_edge(spill_env_t *env, ir_node *to_spill, ir_node *block,
363                            int pos,     const arch_register_class_t *reload_cls,
364                            int allow_remat)
365 {
366         ir_node *before = get_block_insertion_point(block, pos);
367         be_add_reload(env, to_spill, before, reload_cls, allow_remat);
368 }
369
370 void be_spill_phi(spill_env_t *env, ir_node *node)
371 {
372         ir_node *block;
373         spill_info_t* spill;
374         int i, arity;
375
376         assert(is_Phi(node));
377
378         ir_nodeset_insert(&env->mem_phis, node);
379
380         /* create spills for the phi arguments */
381         block = get_nodes_block(node);
382         spill = get_spillinfo(env, node);
383         for(i = 0, arity = get_irn_arity(node); i < arity; ++i) {
384                 ir_node *arg        = get_irn_n(node, i);
385                 ir_node *insert;
386                 //get_spillinfo(env, arg);
387
388                 /* some backends have virtual noreg/unknown nodes that are not scheduled
389                  * and simply always available. */
390                 if(!sched_is_scheduled(arg)) {
391                         ir_node *pred_block = get_Block_cfgpred_block(block, i);
392                         insert = be_get_end_of_block_insertion_point(pred_block);
393                 } else {
394                         insert = skip_keeps_phis(arg);
395                 }
396
397                 be_add_spill(env, arg, insert);
398         }
399 }
400
401 /*
402  *   ____                _         ____        _ _ _
403  *  / ___|_ __ ___  __ _| |_ ___  / ___| _ __ (_) | |___
404  * | |   | '__/ _ \/ _` | __/ _ \ \___ \| '_ \| | | / __|
405  * | |___| | |  __/ (_| | ||  __/  ___) | |_) | | | \__ \
406  *  \____|_|  \___|\__,_|\__\___| |____/| .__/|_|_|_|___/
407  *                                      |_|
408  */
409
410 static void determine_spill_costs(spill_env_t *env, spill_info_t *spillinfo);
411
412 /**
413  * Creates a spill.
414  *
415  * @param senv      the spill environment
416  * @param irn       the node that should be spilled
417  * @param ctx_irn   an user of the spilled node
418  *
419  * @return a be_Spill node
420  */
421 static void spill_irn(spill_env_t *env, spill_info_t *spillinfo)
422 {
423         ir_node *to_spill = spillinfo->to_spill;
424         spill_t *spill;
425
426         /* determine_spill_costs must have been run before */
427         assert(spillinfo->spill_costs >= 0);
428
429         /* some backends have virtual noreg/unknown nodes that are not scheduled
430          * and simply always available. */
431         if(!sched_is_scheduled(to_spill)) {
432                 /* override spillinfos or create a new one */
433                 spillinfo->spills->spill = new_NoMem();
434                 DB((dbg, LEVEL_1, "don't spill %+F use NoMem\n", to_spill));
435                 return;
436         }
437
438         DBG((dbg, LEVEL_1, "spilling %+F ... ", to_spill));
439         spill = spillinfo->spills;
440         for( ; spill != NULL; spill = spill->next) {
441                 ir_node *block  = get_block(spill->before);
442                 ir_node *before = spill->before;
443
444                 /* place all spills before the reloads (as we can't guarantee the
445                  * same order as the be_add_spill and be_add_reload calls */
446                 while(get_irn_idx(sched_prev(before)) > env->new_nodes_idx) {
447                         before = sched_prev(before);
448                 }
449
450                 spill->spill    = be_spill(env->arch_env, block, to_spill);
451                 sched_add_before(before, spill->spill);
452                 DB((dbg, LEVEL_1, "\t%+F before %+F\n", spill->spill, before));
453 #ifdef FIRM_STATISTICS
454                 env->spill_count++;
455 #endif
456         }
457         DBG((dbg, LEVEL_1, "\n"));
458 }
459
460 static void spill_node(spill_env_t *env, spill_info_t *spillinfo);
461
462 /**
463  * If the first usage of a Phi result would be out of memory
464  * there is no sense in allocating a register for it.
465  * Thus we spill it and all its operands to the same spill slot.
466  * Therefore the phi/dataB becomes a phi/Memory
467  *
468  * @param senv      the spill environment
469  * @param phi       the Phi node that should be spilled
470  * @param ctx_irn   an user of the spilled node
471  */
472 static void spill_phi(spill_env_t *env, spill_info_t *spillinfo)
473 {
474         ir_graph *irg   = env->irg;
475         ir_node  *phi   = spillinfo->to_spill;
476         ir_node  *block = get_nodes_block(phi);
477         ir_node  *unknown;
478         ir_node **ins;
479         spill_t  *spill;
480         int       i;
481         int       arity;
482
483         assert(is_Phi(phi));
484         assert(!get_opt_cse());
485         DBG((dbg, LEVEL_1, "spilling Phi %+F:\n", phi));
486
487         /* build a new PhiM */
488         arity   = get_irn_arity(phi);
489         ins     = alloca(sizeof(ir_node*) * arity);
490         unknown = new_r_Unknown(irg, mode_M);
491         for(i = 0; i < arity; ++i) {
492                 ins[i] = unknown;
493         }
494
495         /* override or replace spills list... */
496         spill         = obstack_alloc(&env->obst, sizeof(spill[0]));
497         spill->before = skip_keeps_phis(phi);
498         spill->spill  = new_r_Phi(irg, block, arity, ins, mode_M);
499         spill->next   = NULL;
500
501         spillinfo->spills = spill;
502 #ifdef FIRM_STATISTICS
503         env->spilled_phi_count++;
504 #endif
505
506         for(i = 0; i < arity; ++i) {
507                 ir_node      *arg      = get_irn_n(phi, i);
508                 spill_info_t *arg_info = get_spillinfo(env, arg);
509
510                 determine_spill_costs(env, arg_info);
511                 spill_node(env, arg_info);
512
513                 set_irn_n(spill->spill, i, arg_info->spills->spill);
514         }
515         DBG((dbg, LEVEL_1, "... done spilling Phi %+F, created PhiM %+F\n", phi,
516              spill->spill));
517 }
518
519 /**
520  * Spill a node.
521  *
522  * @param senv      the spill environment
523  * @param to_spill  the node that should be spilled
524  */
525 static void spill_node(spill_env_t *env, spill_info_t *spillinfo)
526 {
527         ir_node *to_spill;
528
529         /* node is already spilled */
530         if(spillinfo->spills != NULL && spillinfo->spills->spill != NULL)
531                 return;
532
533         to_spill = spillinfo->to_spill;
534
535         if (is_Phi(to_spill) && ir_nodeset_contains(&env->mem_phis, to_spill)) {
536                 spill_phi(env, spillinfo);
537         } else {
538                 spill_irn(env, spillinfo);
539         }
540 }
541
542 /*
543  *
544  *  ____                      _            _       _ _
545  * |  _ \ ___ _ __ ___   __ _| |_ ___ _ __(_) __ _| (_)_______
546  * | |_) / _ \ '_ ` _ \ / _` | __/ _ \ '__| |/ _` | | |_  / _ \
547  * |  _ <  __/ | | | | | (_| | ||  __/ |  | | (_| | | |/ /  __/
548  * |_| \_\___|_| |_| |_|\__,_|\__\___|_|  |_|\__,_|_|_/___\___|
549  *
550  */
551
552 /**
553  * Tests whether value @p arg is available before node @p reloader
554  * @returns 1 if value is available, 0 otherwise
555  */
556 static int is_value_available(spill_env_t *env, const ir_node *arg,
557                               const ir_node *reloader)
558 {
559         if(is_Unknown(arg) || arg == new_NoMem())
560                 return 1;
561
562         if(be_is_Spill(arg))
563                 return 1;
564
565         if(arg == get_irg_frame(env->irg))
566                 return 1;
567
568         /* hack for now (happens when command should be inserted at end of block) */
569         if(is_Block(reloader)) {
570                 return 0;
571         }
572
573         /*
574          * Ignore registers are always available
575          */
576         if(arch_irn_is(env->arch_env, arg, ignore)) {
577                 return 1;
578         }
579
580         /* the following test does not work while spilling,
581          * because the liveness info is not adapted yet to the effects of the
582          * additional spills/reloads.
583          */
584 #if 0
585         /* we want to remat before the insn reloader
586          * thus an arguments is alive if
587          *   - it interferes with the reloaders result
588          *   - or it is (last-) used by reloader itself
589          */
590         if (values_interfere(env->birg->lv, reloader, arg)) {
591                 return 1;
592         }
593
594         arity = get_irn_arity(reloader);
595         for (i = 0; i < arity; ++i) {
596                 ir_node *rel_arg = get_irn_n(reloader, i);
597                 if (rel_arg == arg)
598                         return 1;
599         }
600 #endif
601
602         return 0;
603 }
604
605 /**
606  * Checks whether the node can principally be rematerialized
607  */
608 static int is_remat_node(spill_env_t *env, const ir_node *node)
609 {
610         const arch_env_t *arch_env = env->arch_env;
611
612         assert(!be_is_Spill(node));
613
614         if(arch_irn_is(arch_env, node, rematerializable))
615                 return 1;
616
617         return 0;
618 }
619
620 /**
621  * Check if a node is rematerializable. This tests for the following conditions:
622  *
623  * - The node itself is rematerializable
624  * - All arguments of the node are available or also rematerialisable
625  * - The costs for the rematerialisation operation is less or equal a limit
626  *
627  * Returns the costs needed for rematerialisation or something
628  * >= REMAT_COST_INFINITE if remat is not possible.
629  */
630 static int check_remat_conditions_costs(spill_env_t *env,
631                 const ir_node *spilled, const ir_node *reloader, int parentcosts)
632 {
633         int i, arity;
634         int argremats;
635         int costs = 0;
636
637         if(!is_remat_node(env, spilled))
638                 return REMAT_COST_INFINITE;
639
640         if(be_is_Reload(spilled)) {
641                 costs += 2;
642         } else {
643                 costs += arch_get_op_estimated_cost(env->arch_env, spilled);
644         }
645         if(parentcosts + costs >= env->reload_cost + env->spill_cost) {
646                 return REMAT_COST_INFINITE;
647         }
648         if(arch_irn_is(env->arch_env, spilled, modify_flags)) {
649                 return REMAT_COST_INFINITE;
650         }
651
652         argremats = 0;
653         for(i = 0, arity = get_irn_arity(spilled); i < arity; ++i) {
654                 ir_node *arg = get_irn_n(spilled, i);
655
656                 if(is_value_available(env, arg, reloader))
657                         continue;
658
659                 /* we have to rematerialize the argument as well */
660                 if(argremats >= 1) {
661                         /* we only support rematerializing 1 argument at the moment,
662                          * so that we don't have to care about register pressure
663                          */
664                         return REMAT_COST_INFINITE;
665                 }
666                 argremats++;
667
668                 costs += check_remat_conditions_costs(env, arg, reloader,
669                                                       parentcosts + costs);
670                 if(parentcosts + costs >= env->reload_cost + env->spill_cost)
671                         return REMAT_COST_INFINITE;
672         }
673
674         return costs;
675 }
676
677 /**
678  * Re-materialize a node.
679  *
680  * @param senv      the spill environment
681  * @param spilled   the node that was spilled
682  * @param reloader  a irn that requires a reload
683  */
684 static ir_node *do_remat(spill_env_t *env, ir_node *spilled, ir_node *reloader)
685 {
686         int i, arity;
687         ir_node *res;
688         ir_node *bl;
689         ir_node **ins;
690
691         if(is_Block(reloader)) {
692                 bl = reloader;
693         } else {
694                 bl = get_nodes_block(reloader);
695         }
696
697         ins = alloca(get_irn_arity(spilled) * sizeof(ins[0]));
698         for(i = 0, arity = get_irn_arity(spilled); i < arity; ++i) {
699                 ir_node *arg = get_irn_n(spilled, i);
700
701                 if(is_value_available(env, arg, reloader)) {
702                         ins[i] = arg;
703                 } else {
704                         ins[i] = do_remat(env, arg, reloader);
705 #ifdef FIRM_STATISTICS
706                         /* don't count the recursive call as remat */
707                         env->remat_count--;
708 #endif
709                 }
710         }
711
712         /* create a copy of the node */
713         res = new_ir_node(get_irn_dbg_info(spilled), env->irg, bl,
714                           get_irn_op(spilled), get_irn_mode(spilled),
715                           get_irn_arity(spilled), ins);
716         copy_node_attr(spilled, res);
717         new_backedge_info(res);
718
719         DBG((dbg, LEVEL_1, "Insert remat %+F of %+F before reloader %+F\n", res, spilled, reloader));
720
721         if (! is_Proj(res)) {
722                 /* insert in schedule */
723                 sched_reset(res);
724                 sched_add_before(reloader, res);
725 #ifdef FIRM_STATISTICS
726                 env->remat_count++;
727 #endif
728         }
729
730         return res;
731 }
732
733 double be_get_spill_costs(spill_env_t *env, ir_node *to_spill, ir_node *before)
734 {
735         ir_node *block = get_nodes_block(before);
736         double   freq  = get_block_execfreq(env->exec_freq, block);
737         (void) to_spill;
738
739         return env->spill_cost * freq;
740 }
741
742 unsigned be_get_reload_costs_no_weight(spill_env_t *env, const ir_node *to_spill,
743                                        const ir_node *before)
744 {
745         if(be_do_remats) {
746                 /* is the node rematerializable? */
747                 unsigned costs = check_remat_conditions_costs(env, to_spill, before, 0);
748                 if(costs < (unsigned) env->reload_cost)
749                         return costs;
750         }
751
752         return env->reload_cost;
753 }
754
755 double be_get_reload_costs(spill_env_t *env, ir_node *to_spill, ir_node *before)
756 {
757         ir_node      *block = get_nodes_block(before);
758         double        freq  = get_block_execfreq(env->exec_freq, block);
759
760         if(be_do_remats) {
761                 /* is the node rematerializable? */
762                 int costs = check_remat_conditions_costs(env, to_spill, before, 0);
763                 if(costs < env->reload_cost)
764                         return costs * freq;
765         }
766
767         return env->reload_cost * freq;
768 }
769
770 int be_is_rematerializable(spill_env_t *env, const ir_node *to_remat,
771                            const ir_node *before)
772 {
773         return check_remat_conditions_costs(env, to_remat, before, 0) < REMAT_COST_INFINITE;
774 }
775
776 double be_get_reload_costs_on_edge(spill_env_t *env, ir_node *to_spill,
777                                    ir_node *block, int pos)
778 {
779         ir_node *before = get_block_insertion_point(block, pos);
780         return be_get_reload_costs(env, to_spill, before);
781 }
782
783 /*
784  *  ___                     _     ____      _                 _
785  * |_ _|_ __  ___  ___ _ __| |_  |  _ \ ___| | ___   __ _  __| |___
786  *  | || '_ \/ __|/ _ \ '__| __| | |_) / _ \ |/ _ \ / _` |/ _` / __|
787  *  | || | | \__ \  __/ |  | |_  |  _ <  __/ | (_) | (_| | (_| \__ \
788  * |___|_| |_|___/\___|_|   \__| |_| \_\___|_|\___/ \__,_|\__,_|___/
789  *
790  */
791
792 /**
793  * analyzes how to best spill a node and determine costs for that
794  */
795 static void determine_spill_costs(spill_env_t *env, spill_info_t *spillinfo)
796 {
797         ir_node *to_spill = spillinfo->to_spill;
798         ir_node *spill_block;
799         spill_t *spill;
800         double   spill_execfreq;
801
802         /* already calculated? */
803         if(spillinfo->spill_costs >= 0)
804                 return;
805
806         assert(! arch_irn_is(env->arch_env, to_spill, dont_spill));
807         assert(!be_is_Reload(to_spill));
808
809         /* some backends have virtual noreg/unknown nodes that are not scheduled
810          * and simply always available.
811          * TODO: this is kinda hairy, the NoMem is correct for an Unknown as Phi
812          * predecessor (of a PhiM) but this test might match other things too...
813          */
814         if(!sched_is_scheduled(to_spill)) {
815                 /* override spillinfos or create a new one */
816                 spill_t *spill = obstack_alloc(&env->obst, sizeof(spill[0]));
817                 spill->before  = NULL;
818                 spill->next    = NULL;
819                 spill->spill   = new_NoMem();
820
821                 spillinfo->spills      = spill;
822                 spillinfo->spill_costs = 0;
823
824                 DB((dbg, LEVEL_1, "don't spill %+F use NoMem\n", to_spill));
825                 return;
826         }
827
828         spill_block    = get_nodes_block(to_spill);
829         spill_execfreq = get_block_execfreq(env->exec_freq, spill_block);
830
831         if (is_Phi(to_spill) && ir_nodeset_contains(&env->mem_phis, to_spill)) {
832                 /* TODO calculate correct costs...
833                  * (though we can't remat this node anyway so no big problem) */
834                 spillinfo->spill_costs = env->spill_cost * spill_execfreq;
835                 return;
836         }
837
838         if(spillinfo->spills != NULL) {
839                 spill_t *s;
840                 double   spills_execfreq;
841
842                 /* calculate sum of executaion frequencies of individual spills */
843                 spills_execfreq = 0;
844                 s               = spillinfo->spills;
845                 for( ; s != NULL; s = s->next) {
846                         ir_node *spill_block = s->before;
847                         double   freq;
848
849                         if(!is_Block(spill_block)) {
850                                 spill_block = get_nodes_block(spill_block);
851                         }
852                         freq = get_block_execfreq(env->exec_freq, spill_block);
853
854                         spills_execfreq += freq;
855                 }
856
857                 DB((dbg, LEVEL_1, "%+F: latespillcosts %f after def: %f\n", to_spill,
858                     spills_execfreq * env->spill_cost,
859                     spill_execfreq * env->spill_cost));
860
861                 /* multi-/latespill is advantageous -> return*/
862                 if(spills_execfreq < spill_execfreq) {
863                         DB((dbg, LEVEL_1, "use latespills for %+F\n", to_spill));
864                         spillinfo->spill_costs = spills_execfreq * env->spill_cost;
865                         return;
866                 }
867         }
868
869         /* override spillinfos or create a new one */
870         spill         = obstack_alloc(&env->obst, sizeof(spill[0]));
871         spill->before = skip_keeps_phis(to_spill);
872         spill->next   = NULL;
873         spill->spill  = NULL;
874
875         spillinfo->spills      = spill;
876         spillinfo->spill_costs = spill_execfreq * env->spill_cost;
877         DB((dbg, LEVEL_1, "spill %+F after definition\n", to_spill));
878 }
879
880 void be_insert_spills_reloads(spill_env_t *env)
881 {
882         ir_graph              *irg       = env->irg;
883         const arch_env_t      *arch_env  = env->arch_env;
884         const ir_exec_freq    *exec_freq = env->exec_freq;
885         spill_info_t          *si;
886         ir_nodeset_iterator_t  iter;
887         ir_node               *node;
888
889         env->new_nodes_idx = get_irg_last_idx(irg);
890
891         /* create all phi-ms first, this is needed so, that phis, hanging on
892            spilled phis work correctly */
893         foreach_ir_nodeset(&env->mem_phis, node, iter) {
894                 spill_info_t *info = get_spillinfo(env, node);
895                 spill_node(env, info);
896         }
897
898         /* process each spilled node */
899         for (si = set_first(env->spills); si; si = set_next(env->spills)) {
900                 reloader_t *rld;
901                 ir_node  *to_spill        = si->to_spill;
902                 ir_mode  *mode            = get_irn_mode(to_spill);
903                 ir_node **copies          = NEW_ARR_F(ir_node*, 0);
904                 double    all_remat_costs = 0; /** costs when we would remat all nodes */
905                 int       force_remat     = 0;
906
907                 DBG((dbg, LEVEL_1, "\nhandling all reloaders of %+F:\n", to_spill));
908
909                 determine_spill_costs(env, si);
910
911                 /* determine possibility of rematerialisations */
912                 if(be_do_remats) {
913                         /* calculate cost savings for each indivial value when it would
914                            be rematted instead of reloaded */
915                         for (rld = si->reloaders; rld != NULL; rld = rld->next) {
916                                 double   freq;
917                                 int      remat_cost;
918                                 int      remat_cost_delta;
919                                 ir_node *block;
920                                 ir_node *reloader = rld->reloader;
921
922                                 if(rld->rematted_node != NULL) {
923                                         DBG((dbg, LEVEL_2, "\tforced remat %+F before %+F\n",
924                                              rld->rematted_node, reloader));
925                                         continue;
926                                 }
927                                 if(rld->remat_cost_delta >= REMAT_COST_INFINITE) {
928                                         DBG((dbg, LEVEL_2, "\treload before %+F is forbidden\n",
929                                              reloader));
930                                         all_remat_costs = REMAT_COST_INFINITE;
931                                         continue;
932                                 }
933
934                                 remat_cost  = check_remat_conditions_costs(env, to_spill,
935                                                                            reloader, 0);
936                                 if(remat_cost >= REMAT_COST_INFINITE) {
937                                         DBG((dbg, LEVEL_2, "\tremat before %+F not possible\n",
938                                              reloader));
939                                         rld->remat_cost_delta = REMAT_COST_INFINITE;
940                                         all_remat_costs       = REMAT_COST_INFINITE;
941                                         continue;
942                                 }
943
944                                 remat_cost_delta      = remat_cost - env->reload_cost;
945                                 rld->remat_cost_delta = remat_cost_delta;
946                                 block                 = is_Block(reloader) ? reloader : get_nodes_block(reloader);
947                                 freq                  = get_block_execfreq(exec_freq, block);
948                                 all_remat_costs      += remat_cost_delta * freq;
949                                 DBG((dbg, LEVEL_2, "\tremat costs delta before %+F: "
950                                      "%d (rel %f)\n", reloader, remat_cost_delta,
951                                      remat_cost_delta * freq));
952                         }
953                         if(all_remat_costs < REMAT_COST_INFINITE) {
954                                 /* we don't need the costs for the spill if we can remat
955                                    all reloaders */
956                                 all_remat_costs -= si->spill_costs;
957
958                                 DBG((dbg, LEVEL_2, "\tspill costs %d (rel %f)\n",
959                                      env->spill_cost, si->spill_costs));
960                         }
961
962                         if(all_remat_costs < 0) {
963                                 DBG((dbg, LEVEL_1, "\nforcing remats of all reloaders (%f)\n",
964                                      all_remat_costs));
965                                 force_remat = 1;
966                         }
967                 }
968
969                 /* go through all reloads for this spill */
970                 for (rld = si->reloaders; rld != NULL; rld = rld->next) {
971                         ir_node *copy; /* a reload is a "copy" of the original value */
972
973                         if (rld->rematted_node != NULL) {
974                                 copy = rld->rematted_node;
975                                 sched_add_before(rld->reloader, copy);
976                         } else if (be_do_remats &&
977                                         (force_remat || rld->remat_cost_delta < 0)) {
978                                 copy = do_remat(env, to_spill, rld->reloader);
979                         } else {
980                                 /* make sure we have a spill */
981                                 spill_node(env, si);
982
983                                 /* create a reload, use the first spill for now SSA
984                                  * reconstruction for memory comes below */
985                                 assert(si->spills != NULL);
986                                 copy = be_reload(arch_env, si->reload_cls, rld->reloader, mode,
987                                                  si->spills->spill);
988 #ifdef FIRM_STATISTICS
989                                 env->reload_count++;
990 #endif
991                         }
992
993                         DBG((dbg, LEVEL_1, " %+F of %+F before %+F\n",
994                              copy, to_spill, rld->reloader));
995                         ARR_APP1(ir_node*, copies, copy);
996                 }
997
998                 /* if we had any reloads or remats, then we need to reconstruct the
999                  * SSA form for the spilled value */
1000                 if (ARR_LEN(copies) > 0) {
1001                         be_ssa_construction_env_t senv;
1002                         /* be_lv_t *lv = be_get_birg_liveness(env->birg); */
1003
1004                         be_ssa_construction_init(&senv, env->birg);
1005                         be_ssa_construction_add_copy(&senv, to_spill);
1006                         be_ssa_construction_add_copies(&senv, copies, ARR_LEN(copies));
1007                         be_ssa_construction_fix_users(&senv, to_spill);
1008
1009 #if 0
1010                         /* no need to enable this as long as we invalidate liveness
1011                            after this function... */
1012                         be_ssa_construction_update_liveness_phis(&senv);
1013                         be_liveness_update(to_spill);
1014                         len = ARR_LEN(copies);
1015                         for(i = 0; i < len; ++i) {
1016                                 be_liveness_update(lv, copies[i]);
1017                         }
1018 #endif
1019                         be_ssa_construction_destroy(&senv);
1020                 }
1021                 /* need to reconstruct SSA form if we had multiple spills */
1022                 if (si->spills != NULL && si->spills->next != NULL) {
1023                         spill_t *spill;
1024                         int      spill_count = 0;
1025
1026                         be_ssa_construction_env_t senv;
1027
1028                         be_ssa_construction_init(&senv, env->birg);
1029                         spill = si->spills;
1030                         for( ; spill != NULL; spill = spill->next) {
1031                                 /* maybe we rematerialized the value and need no spill */
1032                                 if(spill->spill == NULL)
1033                                         continue;
1034                                 be_ssa_construction_add_copy(&senv, spill->spill);
1035                                 spill_count++;
1036                         }
1037                         if(spill_count > 1) {
1038                                 /* all reloads are attached to the first spill, fix them now */
1039                                 be_ssa_construction_fix_users(&senv, si->spills->spill);
1040                         }
1041
1042                         be_ssa_construction_destroy(&senv);
1043                 }
1044
1045                 DEL_ARR_F(copies);
1046                 si->reloaders = NULL;
1047         }
1048
1049         stat_ev_dbl("spill_spills", env->spill_count);
1050         stat_ev_dbl("spill_reloads", env->reload_count);
1051         stat_ev_dbl("spill_remats", env->remat_count);
1052         stat_ev_dbl("spill_spilled_phis", env->spilled_phi_count);
1053
1054         /* Matze: In theory be_ssa_construction should take care of the liveness...
1055          * try to disable this again in the future */
1056         be_liveness_invalidate(env->birg->lv);
1057
1058         be_remove_dead_nodes_from_schedule(env->birg);
1059 }
1060
1061 void be_init_spill(void)
1062 {
1063         FIRM_DBG_REGISTER(dbg, "firm.be.spill");
1064 }
1065
1066 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_spill);