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