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