use unknowns instead of Bads while constructing phis
[libfirm] / ir / be / bestate.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 /**
21  * @file
22  * @brief       Handles state switching. This is basically the belady spill
23  *              algorithm optimized for the 1-register case.
24  * @author      Matthias Braun
25  * @date        26.03.2007
26  * @version     $Id$
27  */
28 #ifdef HAVE_CONFIG_H
29 #include "config.h"
30 #endif
31
32 #include "bestate.h"
33
34 #include "obst.h"
35 #include "irgraph_t.h"
36 #include "irnode_t.h"
37 #include "irgwalk.h"
38 #include "irloop.h"
39 #include "iredges_t.h"
40 #include "ircons_t.h"
41 #include "irgmod.h"
42 #include "irnodeset.h"
43 #include "irnodemap.h"
44 #include "adt/cpset.h"
45
46 #include "bearch_t.h"
47 #include "beuses.h"
48 #include "besched_t.h"
49 #include "belive_t.h"
50 #include "bemodule.h"
51 #include "benode_t.h"
52 #include "bessaconstr.h"
53
54 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
55
56 typedef struct spill_info_t {
57         struct spill_info_t *next;
58         ir_node *value;
59         ir_node *spill;
60         ir_node **reloads;
61 } spill_info_t;
62
63 typedef struct minibelady_env_t {
64         struct obstack         obst;
65         const arch_env_t      *arch_env;
66         const arch_register_t *reg;
67         const be_lv_t         *lv;
68         void                  *func_env;
69         create_reload_func     create_reload;
70         create_spill_func      create_spill;
71         spill_info_t          *spills;
72         ir_nodemap_t           spill_infos;
73
74         be_uses_t             *uses;           /**< env for the next-use magic */
75 } minibelady_env_t;
76
77 typedef struct block_info_t {
78         ir_node *start_state;
79         ir_node *end_state;
80 } block_info_t;
81
82 static INLINE
83 block_info_t *new_block_info(struct obstack *obst, ir_node *block)
84 {
85         block_info_t *res = obstack_alloc(obst, sizeof(*res));
86         memset(res, 0, sizeof(res[0]));
87
88         assert(is_Block(block));
89         set_irn_link(block, res);
90         mark_irn_visited(block);
91
92         return res;
93 }
94
95 static INLINE
96 block_info_t *get_block_info(ir_node *block)
97 {
98         assert(irn_visited(block));
99         return (block_info_t*) get_irn_link(block);
100 }
101
102 static INLINE
103 spill_info_t *create_spill_info(minibelady_env_t *env, ir_node *state)
104 {
105         spill_info_t *spill_info = obstack_alloc(&env->obst, sizeof(spill_info[0]));
106         memset(spill_info, 0, sizeof(spill_info[0]));
107         spill_info->value = state;
108         spill_info->reloads = NEW_ARR_F(ir_node*, 0);
109
110         ir_nodemap_insert(&env->spill_infos, state, spill_info);
111         //ir_fprintf(stderr, "Insert %+F -> %p\n", state, spill_info);
112
113         spill_info->next = env->spills;
114         env->spills = spill_info;
115
116         return spill_info;
117 }
118
119 static INLINE
120 spill_info_t *get_spill_info(minibelady_env_t *env, const ir_node *node)
121 {
122         spill_info_t *spill_info
123                 = (spill_info_t*) ir_nodemap_get(&env->spill_infos, node);
124         //ir_fprintf(stderr, "Get %+F -> %p\n", node, spill_info);
125         return spill_info;
126 }
127
128 static
129 spill_info_t *create_spill(minibelady_env_t *env, ir_node *state, int force)
130 {
131         spill_info_t *spill_info;
132         ir_node *next;
133         ir_node *after;
134
135         spill_info = get_spill_info(env, state);
136         if(spill_info == NULL) {
137                 spill_info = create_spill_info(env, state);
138         } else if(spill_info->spill != NULL) {
139                 return spill_info;
140         }
141
142         if(sched_is_scheduled(state)) {
143                 next = state;
144                 do {
145                         after = next;
146                         next = sched_next(after);
147                 } while(is_Proj(next) || is_Phi(next) || be_is_Keep(next));
148         } else {
149                 after = state;
150         }
151         spill_info->spill = env->create_spill(env->func_env, state, force, after);
152
153         return spill_info;
154 }
155
156 static
157 void create_reload(minibelady_env_t *env, ir_node *state, ir_node *before,
158                    ir_node *last_state)
159 {
160         spill_info_t *spill_info = create_spill(env, state, 0);
161         ir_node *spill = spill_info->spill;
162         ir_node *reload;
163
164         reload = env->create_reload(env->func_env, state, spill, before,
165                                     last_state);
166         ARR_APP1(ir_node*, spill_info->reloads, reload);
167 }
168
169 static
170 void spill_phi(minibelady_env_t *env, ir_node *phi)
171 {
172         ir_graph *irg = get_irn_irg(phi);
173         ir_node *block = get_nodes_block(phi);
174         int i, arity = get_irn_arity(phi);
175         ir_node **in = alloca(arity * sizeof(in[0]));
176         ir_node *spill_to_kill = NULL;
177         spill_info_t *spill_info;
178
179         /* does a spill exist for the phis value? */
180         spill_info = get_spill_info(env, phi);
181         if(spill_info != NULL) {
182                 spill_to_kill = spill_info->spill;
183         } else {
184                 spill_info = create_spill_info(env, phi);
185         }
186
187         /* create a new phi-M with bad preds */
188         for(i = 0; i < arity; ++i) {
189                 in[i] = new_r_Unknown(irg, mode_M);
190         }
191
192         DBG((dbg, LEVEL_2, "\tcreate Phi-M for %+F\n", phi));
193
194         /* create a Phi-M */
195         spill_info->spill = new_r_Phi(irg, block, arity, in, mode_M);
196
197         if(spill_to_kill != NULL) {
198                 exchange(spill_to_kill, spill_info->spill);
199                 sched_remove(spill_to_kill);
200         }
201
202         /* create spills for the phi values */
203         for(i = 0; i < arity; ++i) {
204                 ir_node *in = get_irn_n(phi, i);
205                 spill_info_t *pred_spill = create_spill(env, in, 1);
206                 set_irn_n(spill_info->spill, i, pred_spill->spill);
207         }
208 }
209
210 static
211 void belady(minibelady_env_t *env, ir_node *block);
212
213 /**
214  * Collects all values live-in at block @p block and all phi results in this
215  * block.
216  * Then it adds the best values (at most n_regs) to the blocks start_workset.
217  * The phis among the remaining values get spilled: Introduce psudo-copies of
218  * their args to break interference and make it possible to spill them to the
219  * same spill slot.
220  */
221 static
222 block_info_t *compute_block_start_state(minibelady_env_t *env, ir_node *block)
223 {
224         block_info_t  *block_info;
225         be_next_use_t  next_use;
226         ir_loop       *loop;
227         ir_node       *best_starter, *first;
228         ir_node       *node;
229         int            n_cfgpreds;
230         unsigned       best_time;
231         int            outer_loop_allowed;
232         int            i;
233
234         /* Create the block info for this block. */
235         block_info = new_block_info(&env->obst, block);
236         n_cfgpreds = get_Block_n_cfgpreds(block);
237
238         /* no cfgpred -> no value active */
239         if(n_cfgpreds == 0) {
240                 block_info->start_state = NULL;
241                 return block_info;
242         }
243
244         /* for 1 pred only: simply take the the end-state of the pred */
245         if(n_cfgpreds == 1) {
246                 ir_node *pred_block = get_Block_cfgpred_block(block, 0);
247                 block_info_t *pred_info;
248
249                 /* process pred block */
250                 belady(env, pred_block);
251
252                 pred_info = get_block_info(pred_block);
253
254                 DBG((dbg, LEVEL_2, "Taking end state from %+F: %+F\n", pred_block, pred_info->end_state));
255                 block_info->start_state = pred_info->end_state;
256                 return block_info;
257         }
258
259         /* Collect all values living at start of block */
260         DBG((dbg, LEVEL_2, "Living at start of %+F:\n", block));
261         first = sched_first(block);
262         loop = get_irn_loop(block);
263         best_starter = NULL;
264         best_time = USES_INFINITY;
265         outer_loop_allowed = 1;
266
267         /* check all Phis first */
268         sched_foreach(block, node) {
269                 if (!is_Phi(node))
270                         break;
271                 if (arch_get_irn_register(env->arch_env, node) != env->reg)
272                         continue;
273
274                 DBG((dbg, LEVEL_2, "\t...checking %+F\n", node));
275                 next_use = be_get_next_use(env->uses, first, 0, node, 0);
276
277                 if(USES_IS_INFINITE(next_use.time)) {
278                         DBG((dbg, LEVEL_2, "\tnot taken (dead)\n"));
279                         continue;
280                 }
281
282                 if(next_use.outermost_loop >= get_loop_depth(loop)) {
283                         if(outer_loop_allowed || next_use.time < best_time) {
284                                 DBG((dbg, LEVEL_2, "\ttaken (%u, loop %d)\n", next_use.time,
285                                      next_use.outermost_loop));
286
287                                 if(best_starter != NULL) {
288                                         /* spill the phi as it is not used */
289                                         spill_phi(env, best_starter);
290                                 }
291                                 best_starter = node;
292                                 best_time = next_use.time;
293                                 outer_loop_allowed = 0;
294                         }
295                 } else {
296                         if(outer_loop_allowed && next_use.time < best_time) {
297                                 DBG((dbg, LEVEL_2, "\ttaken (%u, loop %d)\n", next_use.time,
298                                      next_use.outermost_loop));
299                                 if(best_starter != NULL) {
300                                         /* spill the phi as it is not used */
301                                         spill_phi(env, best_starter);
302                                 }
303                                 best_starter = node;
304                                 best_time = next_use.time;
305                         }
306                 }
307
308                 if(best_starter != node) {
309                         /* spill the phi as it is not used */
310                         spill_phi(env, best_starter);
311                 }
312         }
313
314         /* check all Live-Ins */
315         be_lv_foreach(env->lv, block, be_lv_state_in, i) {
316                 node = be_lv_get_irn(env->lv, block, i);
317
318                 if(!mode_is_data(get_irn_mode(node)))
319                         continue;
320
321                 if (arch_get_irn_register(env->arch_env, node) != env->reg)
322                         continue;
323
324                 DBG((dbg, LEVEL_2, "\t...checking %+F\n", node));
325                 next_use = be_get_next_use(env->uses, first, 0, node, 0);
326
327                 if(USES_IS_INFINITE(next_use.time)) {
328                         DBG((dbg, LEVEL_2, "\tnot taken (dead)\n"));
329                         continue;
330                 }
331
332                 if(next_use.outermost_loop >= get_loop_depth(loop)) {
333                         if(outer_loop_allowed || next_use.time < best_time) {
334                                 DBG((dbg, LEVEL_2, "\ttaken (%u, loop %d)\n", next_use.time,
335                                      next_use.outermost_loop));
336
337                                 if(best_starter != NULL && is_Phi(best_starter)) {
338                                         /* spill the phi as it is not used */
339                                         spill_phi(env, best_starter);
340                                 }
341                                 best_starter = node;
342                                 best_time = next_use.time;
343                                 outer_loop_allowed = 0;
344                         }
345                 } else {
346                         if(outer_loop_allowed && next_use.time < best_time) {
347                                 DBG((dbg, LEVEL_2, "\ttaken (%u, loop %d)\n", next_use.time,
348                                      next_use.outermost_loop));
349                                 if(best_starter != NULL && is_Phi(best_starter)) {
350                                         /* spill the phi as it is not used */
351                                         spill_phi(env, best_starter);
352                                 }
353                                 best_starter = node;
354                                 best_time = next_use.time;
355                         }
356                 }
357         }
358
359         block_info->start_state = best_starter;
360
361         return block_info;
362 }
363
364 /**
365  * For the given block @p block, decide for each values
366  * whether it is used from a register or is reloaded
367  * before the use.
368  */
369 static
370 void belady(minibelady_env_t *env, ir_node *block)
371 {
372         ir_node *current_state;
373         ir_node *node;
374         block_info_t *block_info;
375
376         /* Don't do a block twice */
377         if(irn_visited(block))
378                 return;
379
380         /* compute value to start with */
381         block_info = compute_block_start_state(env, block);
382
383         /* get the starting workset for this block */
384         DBG((dbg, LEVEL_3, "\n"));
385         DBG((dbg, LEVEL_3, "Decide for %+F\n", block));
386
387         current_state = block_info->start_state;
388         DBG((dbg, LEVEL_3, "Start value: %+F\n", current_state));
389
390         /* process the block from start to end */
391         DBG((dbg, LEVEL_3, "Processing...\n"));
392
393         sched_foreach(block, node) {
394                 int i, arity;
395                 ir_node *need_val = NULL;
396
397                 /* projs are handled with the tuple value.
398                  * Phis are no real instr (see insert_starters()) */
399                 if (is_Proj(node) || is_Phi(node)) {
400                         continue;
401                 }
402
403                 /* check which state is desired for the node */
404                 arity = get_irn_arity(node);
405                 for(i = 0; i < arity; ++i) {
406                         const arch_register_t *reg;
407                         ir_node *in = get_irn_n(node, i);
408
409                         if(!mode_is_data(get_irn_mode(in)))
410                                 continue;
411
412                         reg = arch_get_irn_register(env->arch_env, in);
413                         if(reg == env->reg) {
414                                 assert(need_val == NULL);
415                                 need_val = in;
416                                 DBG((dbg, LEVEL_3, "\t... need state %+F\n", need_val));
417                         }
418                 }
419                 /* create a reload to match state if necessary */
420                 if(need_val != NULL && need_val != current_state) {
421                         DBG((dbg, LEVEL_3, "\t... reloading %+F\n", need_val));
422                         create_reload(env, need_val, node, current_state);
423                         current_state = need_val;
424                 }
425
426                 DBG((dbg, LEVEL_3, "  ...%+F\n", node));
427
428                 /* record state changes by the node */
429                 if (get_irn_mode(node) == mode_T) {
430                         const ir_edge_t *edge;
431
432                         foreach_out_edge(node, edge) {
433                                 const arch_register_t *reg;
434                                 ir_node *proj = get_edge_src_irn(edge);
435
436                                 if(!mode_is_data(get_irn_mode(proj)))
437                                         continue;
438
439                                 reg = arch_get_irn_register(env->arch_env, proj);
440                                 if(reg == env->reg) {
441                                         current_state = proj;
442                                         DBG((dbg, LEVEL_3, "\t... current_state <- %+F\n", current_state));
443                                 }
444                         }
445                 } else {
446                         if(mode_is_data(get_irn_mode(node))) {
447                                 const arch_register_t *reg =
448                                         arch_get_irn_register(env->arch_env, node);
449                                 if(reg == env->reg) {
450                                         current_state = node;
451                                         DBG((dbg, LEVEL_3, "\t... current_state <- %+F\n", current_state));
452                                 }
453                         }
454                 }
455         }
456
457         /* Remember end-workset for this block */
458         block_info->end_state = current_state;
459         DBG((dbg, LEVEL_3, "End value for %+F: %+F\n", block, current_state));
460 }
461
462 static
463 void belady_walker(ir_node *block, void *data)
464 {
465         belady((minibelady_env_t*) data, block);
466 }
467
468 static
469 ir_node *get_end_of_block_insertion_point(ir_node *block)
470 {
471         ir_node *last = sched_last(block);
472
473         /* skip Projs and Keep-alikes behind the jump... */
474         while(is_Proj(last) || be_is_Keep(last)) {
475                 last = sched_prev(last);
476         }
477
478         if(!is_cfop(last)) {
479                 last = sched_next(last);
480                 /* last node must be a cfop, only exception is the start block */
481                 assert(last     == get_irg_start_block(get_irn_irg(block)));
482         }
483
484         return last;
485 }
486
487 /**
488  * We must adapt the live-outs to the live-ins at each block-border.
489  */
490 static
491 void fix_block_borders(ir_node *block, void *data) {
492         minibelady_env_t *env = data;
493         ir_graph *irg = get_irn_irg(block);
494         ir_node *startblock = get_irg_start_block(irg);
495         int i;
496         int arity;
497         block_info_t *block_info;
498
499         if(block == startblock)
500                 return;
501
502         DBG((dbg, LEVEL_3, "\n"));
503
504         block_info = get_block_info(block);
505
506         DBG((dbg, LEVEL_3, "Fixing %+F (needs %+F)\n", block,
507              block_info->start_state));
508
509         /* process all pred blocks */
510         arity = get_irn_arity(block);
511         for (i = 0; i < arity; ++i) {
512                 ir_node      *pred       = get_Block_cfgpred_block(block, i);
513                 block_info_t *pred_info  = get_block_info(pred);
514                 ir_node      *need_state = block_info->start_state;
515
516                 if(need_state == NULL)
517                         continue;
518
519                 if(is_Phi(need_state) && get_nodes_block(need_state) == block) {
520                         need_state = get_irn_n(need_state, i);
521                 }
522
523                 DBG((dbg, LEVEL_3, "  Pred %+F (ends in %+F, we need %+F)\n", pred,
524                      pred_info->end_state, need_state));
525
526                 if(pred_info->end_state != need_state) {
527                         ir_node *insert_point = get_end_of_block_insertion_point(pred);
528
529
530                         DBG((dbg, LEVEL_3, "  Creating reload for %+F\n", need_state));
531                         create_reload(env, need_state, insert_point, pred_info->end_state);
532                 }
533         }
534 }
535
536 void be_assure_state(be_irg_t *birg, const arch_register_t *reg, void *func_env,
537                      create_spill_func create_spill,
538                      create_reload_func create_reload) {
539         minibelady_env_t env;
540         ir_graph *irg = be_get_birg_irg(birg);
541         spill_info_t *info;
542         be_lv_t *lv = be_assure_liveness(birg);
543
544         be_liveness_assure_sets(lv);
545         be_assure_dom_front(birg);
546         /* construct control flow loop tree */
547         if(! (get_irg_loopinfo_state(irg) & loopinfo_cf_consistent)) {
548                 construct_cf_backedges(irg);
549         }
550
551         obstack_init(&env.obst);
552         env.arch_env      = be_get_birg_arch_env(birg);
553         env.reg           = reg;
554         env.func_env      = func_env;
555         env.create_spill  = create_spill;
556         env.create_reload = create_reload;
557         env.lv            = be_get_birg_liveness(birg);
558         env.uses          = be_begin_uses(irg, env.lv);
559         env.spills        = NULL;
560         ir_nodemap_init(&env.spill_infos);
561
562         assure_doms(irg);
563         set_using_visited(irg);
564         set_using_irn_link(irg);
565         inc_irg_visited(irg);
566
567         /* process blocks */
568         irg_block_walk_graph(irg, NULL, belady_walker, &env);
569
570         /* fix block end_states that don't match the next blocks start_state */
571         irg_block_walk_graph(irg, fix_block_borders, NULL, &env);
572
573         clear_using_visited(irg);
574         clear_using_irn_link(irg);
575
576         /* reconstruct ssa-form */
577         info = env.spills;
578         while(info != NULL) {
579                 be_ssa_construction_env_t senv;
580                 int i, len;
581                 ir_node **phis;
582
583                 be_ssa_construction_init(&senv, birg);
584                 if(sched_is_scheduled(info->value))
585                         be_ssa_construction_add_copy(&senv, info->value);
586                 be_ssa_construction_add_copies(&senv,
587                                                info->reloads, ARR_LEN(info->reloads));
588                 be_ssa_construction_fix_users(&senv, info->value);
589
590                 if(lv != NULL) {
591                         be_ssa_construction_update_liveness_phis(&senv, lv);
592
593                         be_liveness_update(lv, info->value);
594                         len = ARR_LEN(info->reloads);
595                         for(i = 0; i < len; ++i) {
596                                 ir_node *reload = info->reloads[i];
597                                 be_liveness_update(lv, reload);
598                         }
599                 }
600
601                 phis = be_ssa_construction_get_new_phis(&senv);
602
603                 /* set register requirements for phis */
604                 len = ARR_LEN(phis);
605                 for(i = 0; i < len; ++i) {
606                         ir_node *phi = phis[i];
607                         be_set_phi_flags(env.arch_env, phi, arch_irn_flags_ignore);
608                         arch_set_irn_register(env.arch_env, phi, env.reg);
609                 }
610                 be_ssa_construction_destroy(&senv);
611
612                 info = info->next;
613         }
614
615         /* some nodes might be dead now. */
616         be_remove_dead_nodes_from_schedule(birg);
617
618         ir_nodemap_destroy(&env.spill_infos);
619         be_end_uses(env.uses);
620         obstack_free(&env.obst, NULL);
621 }
622
623 void be_init_state(void)
624 {
625         FIRM_DBG_REGISTER(dbg, "firm.be.state");
626 }
627
628 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_state);