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