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