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