fixed debug output of unary x87 nodes
[libfirm] / ir / be / bespill.c
1 /*
2  * Author:      Daniel Grund, Sebastian Hack, Matthias Braun
3  * Date:                29.09.2005
4  * Copyright:   (c) Universitaet Karlsruhe
5  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
6  */
7 #ifdef HAVE_CONFIG_H
8 #include "config.h"
9 #endif
10
11 #include <stdlib.h>
12
13 #include "pset.h"
14 #include "irnode_t.h"
15 #include "ircons_t.h"
16 #include "iredges_t.h"
17 #include "irprintf.h"
18 #include "ident_t.h"
19 #include "type_t.h"
20 #include "entity_t.h"
21 #include "debug.h"
22 #include "irgwalk.h"
23 #include "array.h"
24 #include "pdeq.h"
25 #include "unionfind.h"
26 #include "execfreq.h"
27
28 #include "belive_t.h"
29 #include "besched_t.h"
30 #include "bespill.h"
31 #include "belive_t.h"
32 #include "benode_t.h"
33 #include "bechordal_t.h"
34 #include "bejavacoal.h"
35
36 // only rematerialise when costs are less than REMAT_COST_LIMIT
37 // TODO determine a good value here...
38 #define REMAT_COST_LIMIT        80
39
40 typedef struct _reloader_t reloader_t;
41
42 struct _reloader_t {
43         reloader_t *next;
44         ir_node *reloader;
45 };
46
47 typedef struct _spill_info_t {
48         ir_node *spilled_node;
49         reloader_t *reloaders;
50
51         ir_node *spill;
52 } spill_info_t;
53
54 struct _spill_env_t {
55         const arch_register_class_t *cls;
56         const arch_env_t *arch_env;
57         const be_chordal_env_t *chordal_env;
58         struct obstack obst;
59         set *spills;                            /**< all spill_info_t's, which must be placed */
60         pset *mem_phis;                         /**< set of all special spilled phis. allocated and freed separately */
61
62         DEBUG_ONLY(firm_dbg_module_t *dbg;)
63 };
64
65 /**
66  * Compare two spill infos.
67  */
68 static int cmp_spillinfo(const void *x, const void *y, size_t size) {
69         const spill_info_t *xx = x;
70         const spill_info_t *yy = y;
71         return xx->spilled_node != yy->spilled_node;
72 }
73
74 /**
75  * Returns spill info for a specific value (the value that is to be spilled)
76  */
77 static spill_info_t *get_spillinfo(const spill_env_t *env, ir_node *value) {
78         spill_info_t info, *res;
79         int hash = HASH_PTR(value);
80
81         info.spilled_node = value;
82         res = set_find(env->spills, &info, sizeof(info), hash);
83
84         if (res == NULL) {
85                 info.reloaders = NULL;
86                 info.spill = NULL;
87                 res = set_insert(env->spills, &info, sizeof(info), hash);
88         }
89
90         return res;
91 }
92
93 DEBUG_ONLY(
94 /* Sets the debug module of a spill environment. */
95 void be_set_spill_env_dbg_module(spill_env_t *env, firm_dbg_module_t *dbg) {
96         env->dbg = dbg;
97 }
98 )
99
100 /* Creates a new spill environment. */
101 spill_env_t *be_new_spill_env(const be_chordal_env_t *chordal_env) {
102         spill_env_t *env        = xmalloc(sizeof(env[0]));
103         env->spills                     = new_set(cmp_spillinfo, 1024);
104         env->cls                        = chordal_env->cls;
105         env->chordal_env        = chordal_env;
106         env->arch_env       = env->chordal_env->birg->main_env->arch_env;
107         env->mem_phis           = pset_new_ptr_default();
108         obstack_init(&env->obst);
109         return env;
110 }
111
112 /* Deletes a spill environment. */
113 void be_delete_spill_env(spill_env_t *env) {
114         del_set(env->spills);
115         del_pset(env->mem_phis);
116         obstack_free(&env->obst, NULL);
117         free(env);
118 }
119
120 /**
121  *  ____  _                  ____      _                 _
122  * |  _ \| | __ _  ___ ___  |  _ \ ___| | ___   __ _  __| |___
123  * | |_) | |/ _` |/ __/ _ \ | |_) / _ \ |/ _ \ / _` |/ _` / __|
124  * |  __/| | (_| | (_|  __/ |  _ <  __/ | (_) | (_| | (_| \__ \
125  * |_|   |_|\__,_|\___\___| |_| \_\___|_|\___/ \__,_|\__,_|___/
126  *
127  */
128
129 void be_add_reload(spill_env_t *env, ir_node *to_spill, ir_node *before) {
130         spill_info_t *info;
131         reloader_t *rel;
132
133         assert(sched_is_scheduled(before));
134         assert(arch_irn_consider_in_reg_alloc(env->arch_env, env->cls, to_spill));
135
136         info = get_spillinfo(env, to_spill);
137
138         if(is_Phi(to_spill)) {
139                 int i, arity;
140                 // create spillinfos for the phi arguments
141                 for(i = 0, arity = get_irn_arity(to_spill); i < arity; ++i) {
142                         ir_node *arg = get_irn_n(to_spill, i);
143                         get_spillinfo(env, arg);
144                 }
145         }
146
147         rel           = obstack_alloc(&env->obst, sizeof(rel[0]));
148         rel->reloader = before;
149         rel->next     = info->reloaders;
150         info->reloaders = rel;
151         be_liveness_add_missing(env->chordal_env->lv);
152 }
153
154 void be_add_reload_on_edge(spill_env_t *env, ir_node *to_spill, ir_node *block, int pos) {
155         ir_node *predblock, *last;
156
157         /* simply add the reload to the beginning of the block if we only have 1 predecessor
158          * (we don't need to check for phis as there can't be any in a block with only 1 pred)
159          */
160         if(get_Block_n_cfgpreds(block) == 1) {
161                 assert(!is_Phi(sched_first(block)));
162                 be_add_reload(env, to_spill, sched_first(block));
163                 return;
164         }
165
166         /* We have to reload the value in pred-block */
167         predblock = get_Block_cfgpred_block(block, pos);
168         last = sched_last(predblock);
169
170         /* we might have projs and keepanys behind the jump... */
171         while(is_Proj(last) || be_is_Keep(last)) {
172                 last = sched_prev(last);
173                 assert(!sched_is_end(last));
174         }
175         assert(is_cfop(last));
176
177         // add the reload before the (cond-)jump
178         be_add_reload(env, to_spill, last);
179 }
180
181 void be_spill_phi(spill_env_t *env, ir_node *node) {
182         int i, arity;
183
184         assert(is_Phi(node));
185
186         pset_insert_ptr(env->mem_phis, node);
187
188         // create spillinfos for the phi arguments
189         get_spillinfo(env, node);
190         for(i = 0, arity = get_irn_arity(node); i < arity; ++i) {
191                 ir_node *arg = get_irn_n(node, i);
192                 get_spillinfo(env, arg);
193         }
194 }
195
196 /*
197  *   ____                _         ____        _ _ _
198  *  / ___|_ __ ___  __ _| |_ ___  / ___| _ __ (_) | |___
199  * | |   | '__/ _ \/ _` | __/ _ \ \___ \| '_ \| | | / __|
200  * | |___| | |  __/ (_| | ||  __/  ___) | |_) | | | \__ \
201  *  \____|_|  \___|\__,_|\__\___| |____/| .__/|_|_|_|___/
202  *                                      |_|
203  */
204
205 /**
206  * Schedules a node after an instruction. (That is the place after all projs and phis
207  * that are scheduled after the instruction)
208  * This function also skips phi nodes at the beginning of a block
209  */
210 static void sched_add_after_insn(ir_node *sched_after, ir_node *node) {
211         ir_node *next = sched_next(sched_after);
212         while(is_Proj(next) || is_Phi(next)) {
213                 next = sched_next(next);
214         }
215         assert(next != NULL);
216
217         if(sched_is_end(next)) {
218                 sched_add_after(sched_last(get_nodes_block(sched_after)), node);
219         } else {
220                 sched_add_before(next, node);
221         }
222 }
223
224 /**
225  * Creates a spill.
226  *
227  * @param senv      the spill environment
228  * @param irn       the node that should be spilled
229  * @param ctx_irn   an user of the spilled node
230  *
231  * @return a be_Spill node
232  */
233 static void spill_irn(spill_env_t *env, spill_info_t *spillinfo) {
234         ir_node *to_spill = spillinfo->spilled_node;
235
236         DBG((env->dbg, LEVEL_1, "%+F\n", to_spill));
237
238         /* Trying to spill an already spilled value, no need for a new spill
239          * node then, we can simply connect to the same one for this reload
240          *
241          * (although rematerialization code should handle most of these cases
242          * this can still happen when spilling Phis)
243          */
244         if(be_is_Reload(to_spill)) {
245                 spillinfo->spill = get_irn_n(to_spill, be_pos_Reload_mem);
246                 return;
247         }
248
249         if (arch_irn_is(env->arch_env, to_spill, dont_spill)) {
250                 if (env->chordal_env->opts->vrfy_option == BE_CH_VRFY_WARN)
251                         ir_fprintf(stderr, "Verify warning: spilling 'dont_spill' node %+F\n", to_spill);
252                 else if (env->chordal_env->opts->vrfy_option == BE_CH_VRFY_ASSERT)
253                         assert(0 && "Attempt to spill a node marked 'dont_spill'");
254         }
255
256         spillinfo->spill = be_spill(env->arch_env, to_spill);
257         sched_add_after_insn(to_spill, spillinfo->spill);
258 }
259
260 static void spill_node(spill_env_t *env, spill_info_t *spillinfo);
261
262 /**
263  * If the first usage of a Phi result would be out of memory
264  * there is no sense in allocating a register for it.
265  * Thus we spill it and all its operands to the same spill slot.
266  * Therefore the phi/dataB becomes a phi/Memory
267  *
268  * @param senv      the spill environment
269  * @param phi       the Phi node that should be spilled
270  * @param ctx_irn   an user of the spilled node
271  */
272 static void spill_phi(spill_env_t *env, spill_info_t *spillinfo) {
273         ir_node *phi = spillinfo->spilled_node;
274         int i;
275         int arity = get_irn_arity(phi);
276         ir_node     *block    = get_nodes_block(phi);
277         ir_node     **ins;
278
279         assert(is_Phi(phi));
280
281         /* build a new PhiM */
282         ins = alloca(sizeof(ir_node*) * arity);
283         for(i = 0; i < arity; ++i) {
284                 ins[i] = get_irg_bad(env->chordal_env->irg);
285         }
286         spillinfo->spill = new_r_Phi(env->chordal_env->irg, block, arity, ins, mode_M);
287
288         for(i = 0; i < arity; ++i) {
289                 ir_node *arg = get_irn_n(phi, i);
290                 spill_info_t *arg_info = get_spillinfo(env, arg);
291
292                 spill_node(env, arg_info);
293
294                 set_irn_n(spillinfo->spill, i, arg_info->spill);
295         }
296 }
297
298 /**
299  * Spill a node.
300  *
301  * @param senv      the spill environment
302  * @param to_spill  the node that should be spilled
303  */
304 static void spill_node(spill_env_t *env, spill_info_t *spillinfo) {
305         ir_node *to_spill;
306
307         // the node should be tagged for spilling already...
308         if(spillinfo->spill != NULL)
309                 return;
310
311         to_spill = spillinfo->spilled_node;
312         if (is_Phi(to_spill) && pset_find_ptr(env->mem_phis, spillinfo->spilled_node)) {
313                 spill_phi(env, spillinfo);
314         } else {
315                 spill_irn(env, spillinfo);
316         }
317 }
318
319 /*
320  *
321  *  ____                      _            _       _ _
322  * |  _ \ ___ _ __ ___   __ _| |_ ___ _ __(_) __ _| (_)_______
323  * | |_) / _ \ '_ ` _ \ / _` | __/ _ \ '__| |/ _` | | |_  / _ \
324  * |  _ <  __/ | | | | | (_| | ||  __/ |  | | (_| | | |/ /  __/
325  * |_| \_\___|_| |_| |_|\__,_|\__\___|_|  |_|\__,_|_|_/___\___|
326  *
327  */
328
329 /**
330  * Tests whether value @p arg is available before node @p reloader
331  * @returns 1 if value is available, 0 otherwise
332  */
333 static int is_value_available(spill_env_t *env, ir_node *arg, ir_node *reloader) {
334         if(is_Unknown(arg) || arg == new_NoMem())
335                 return 1;
336
337         if(be_is_Spill(arg))
338                 return 1;
339
340         if(arg == get_irg_frame(env->chordal_env->irg))
341                 return 1;
342
343         /* the following test does not work while spilling,
344          * because the liveness info is not adapted yet to the effects of the
345          * additional spills/reloads.
346          *
347          * So we can only do this test for ignore registers (of our register class)
348          */
349         if(arch_get_irn_reg_class(env->arch_env, arg, -1) == env->chordal_env->cls
350            && arch_irn_is(env->arch_env, arg, ignore)) {
351                 int i, arity;
352
353                 /* we want to remat before the insn reloader
354                  * thus an arguments is alive if
355                  *   - it interferes with the reloaders result
356                  *   - or it is (last-) used by reloader itself
357                  */
358                 if (values_interfere(env->chordal_env->lv, reloader, arg)) {
359                         return 1;
360                 }
361
362                 arity = get_irn_arity(reloader);
363                 for (i = 0; i < arity; ++i) {
364                         ir_node *rel_arg = get_irn_n(reloader, i);
365                         if (rel_arg == arg)
366                                 return 1;
367                 }
368         }
369
370         return 0;
371 }
372
373 /**
374  * Checks whether the node can principally be rematerialized
375  */
376 static int is_remat_node(spill_env_t *env, ir_node *node) {
377         const arch_env_t *arch_env = env->arch_env;
378
379         assert(!be_is_Spill(node));
380
381         if(be_is_Reload(node))
382                 return 1;
383
384         // TODO why does arch_irn_is say rematerializable anyway?
385         if(be_is_Barrier(node))
386                 return 0;
387
388         if(arch_irn_is(arch_env, node, rematerializable))
389                 return 1;
390
391         if(be_is_StackParam(node))
392                 return 1;
393
394         return 0;
395 }
396
397 /**
398  * Check if a node is rematerializable. This tests for the following conditions:
399  *
400  * - The node itself is rematerializable
401  * - All arguments of the node are available or also rematerialisable
402  * - The costs for the rematerialisation operation is less or equal a limit
403  *
404  * Returns the costs needed for rematerialisation or something
405  * > REMAT_COST_LIMIT if remat is not possible.
406  */
407 static int check_remat_conditions_costs(spill_env_t *env, ir_node *spilled, ir_node *reloader, int parentcosts) {
408         int i, arity;
409         int argremats;
410         int costs = 0;
411
412         if(!is_remat_node(env, spilled))
413                 return REMAT_COST_LIMIT;
414
415         if(be_is_Reload(spilled)) {
416                 costs += 2;
417         } else {
418                 costs += arch_get_op_estimated_cost(env->arch_env, spilled);
419         }
420         if(parentcosts + costs >= REMAT_COST_LIMIT)
421                 return REMAT_COST_LIMIT;
422
423         argremats = 0;
424         for(i = 0, arity = get_irn_arity(spilled); i < arity; ++i) {
425                 ir_node *arg = get_irn_n(spilled, i);
426
427                 if(is_value_available(env, arg, reloader))
428                         continue;
429
430                 // we have to rematerialize the argument as well...
431                 if(argremats >= 1) {
432                         /* we only support rematerializing 1 argument at the moment,
433                          * so that we don't have to care about register pressure
434                          */
435                         return REMAT_COST_LIMIT;
436                 }
437                 argremats++;
438
439                 // TODO can we get more accurate costs than +1?
440                 costs += check_remat_conditions_costs(env, arg, reloader, parentcosts + costs);
441                 if(parentcosts + costs >= REMAT_COST_LIMIT)
442                         return REMAT_COST_LIMIT;
443         }
444
445         return costs;
446 }
447
448 static int check_remat_conditions(spill_env_t *env, ir_node *spilled, ir_node *reloader) {
449         int costs = check_remat_conditions_costs(env, spilled, reloader, 1);
450
451         return costs < REMAT_COST_LIMIT;
452 }
453
454 /**
455  * Re-materialize a node.
456  *
457  * @param senv      the spill environment
458  * @param spilled   the node that was spilled
459  * @param reloader  a irn that requires a reload
460  */
461 static ir_node *do_remat(spill_env_t *env, ir_node *spilled, ir_node *reloader) {
462         int i, arity;
463         ir_node *res;
464         ir_node *bl = get_nodes_block(reloader);
465         ir_node **ins;
466
467         ins = alloca(get_irn_arity(spilled) * sizeof(ins[0]));
468         for(i = 0, arity = get_irn_arity(spilled); i < arity; ++i) {
469                 ir_node *arg = get_irn_n(spilled, i);
470
471                 if(is_value_available(env, arg, reloader)) {
472                         ins[i] = arg;
473                 } else {
474                         ins[i] = do_remat(env, arg, reloader);
475                 }
476         }
477
478         /* create a copy of the node */
479         res = new_ir_node(get_irn_dbg_info(spilled), env->chordal_env->irg, bl,
480                 get_irn_op(spilled),
481                 get_irn_mode(spilled),
482                 get_irn_arity(spilled),
483                 ins);
484         copy_node_attr(spilled, res);
485
486         DBG((env->dbg, LEVEL_1, "Insert remat %+F before reloader %+F\n", res, reloader));
487
488         /* insert in schedule */
489         assert(!is_Block(reloader));
490         sched_add_before(reloader, res);
491
492         return res;
493 }
494
495 /*
496  *  ___                     _     ____      _                 _
497  * |_ _|_ __  ___  ___ _ __| |_  |  _ \ ___| | ___   __ _  __| |___
498  *  | || '_ \/ __|/ _ \ '__| __| | |_) / _ \ |/ _ \ / _` |/ _` / __|
499  *  | || | | \__ \  __/ |  | |_  |  _ <  __/ | (_) | (_| | (_| \__ \
500  * |___|_| |_|___/\___|_|   \__| |_| \_\___|_|\___/ \__,_|\__,_|___/
501  *
502  */
503
504 void be_insert_spills_reloads(spill_env_t *env) {
505         const arch_env_t *arch_env = env->arch_env;
506         spill_info_t *si;
507
508         /* process each spilled node */
509         DBG((env->dbg, LEVEL_1, "Insert spills and reloads:\n"));
510         for(si = set_first(env->spills); si; si = set_next(env->spills)) {
511                 reloader_t *rld;
512                 ir_mode *mode = get_irn_mode(si->spilled_node);
513                 pset *values = pset_new_ptr(16);
514
515                 /* go through all reloads for this spill */
516                 for(rld = si->reloaders; rld; rld = rld->next) {
517                         ir_node *new_val;
518
519                         if (check_remat_conditions(env, si->spilled_node, rld->reloader)) {
520                                 new_val = do_remat(env, si->spilled_node, rld->reloader);
521                         } else {
522                                 /* make sure we have a spill */
523                                 spill_node(env, si);
524
525                                 /* do a reload */
526                                 new_val = be_reload(arch_env, env->cls, rld->reloader, mode, si->spill);
527                         }
528
529                         DBG((env->dbg, LEVEL_1, " %+F of %+F before %+F\n", new_val, si->spilled_node, rld->reloader));
530                         pset_insert_ptr(values, new_val);
531                 }
532
533                 if(pset_count(values) > 0) {
534                         /* introduce copies, rewire the uses */
535                         pset_insert_ptr(values, si->spilled_node);
536                         be_ssa_constr_set_ignore(env->chordal_env->dom_front, env->chordal_env->lv, values, env->mem_phis);
537                 }
538
539                 del_pset(values);
540         }
541
542         // reloads are placed now, but we might reuse the spill environment for further spilling decisions
543         del_set(env->spills);
544         env->spills = new_set(cmp_spillinfo, 1024);
545 }