2 * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
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.
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.
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
22 * @brief Handles state switching. This is basically the belady spill
23 * algorithm optimized for the 1-register case.
24 * @author Matthias Braun
35 #include "irgraph_t.h"
39 #include "iredges_t.h"
42 #include "irnodeset.h"
43 #include "irnodemap.h"
44 #include "adt/cpset.h"
48 #include "besched_t.h"
52 #include "bessaconstr.h"
54 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
56 typedef struct spill_info_t {
57 struct spill_info_t *next;
63 typedef struct minibelady_env_t {
65 const arch_register_t *reg;
68 create_reload_func create_reload;
69 create_spill_func create_spill;
71 ir_nodemap_t spill_infos;
73 be_uses_t *uses; /**< env for the next-use magic */
76 typedef struct block_info_t {
82 block_info_t *new_block_info(struct obstack *obst, ir_node *block)
84 block_info_t *res = obstack_alloc(obst, sizeof(*res));
85 memset(res, 0, sizeof(res[0]));
87 assert(is_Block(block));
88 set_irn_link(block, res);
89 mark_irn_visited(block);
95 block_info_t *get_block_info(ir_node *block)
97 assert(irn_visited(block));
98 return (block_info_t*) get_irn_link(block);
102 spill_info_t *create_spill_info(minibelady_env_t *env, ir_node *state)
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);
109 ir_nodemap_insert(&env->spill_infos, state, spill_info);
110 //ir_fprintf(stderr, "Insert %+F -> %p\n", state, spill_info);
112 spill_info->next = env->spills;
113 env->spills = spill_info;
119 spill_info_t *get_spill_info(minibelady_env_t *env, const ir_node *node)
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);
128 spill_info_t *create_spill(minibelady_env_t *env, ir_node *state, int force)
130 spill_info_t *spill_info;
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) {
141 if(sched_is_scheduled(state)) {
145 next = sched_next(after);
146 } while(is_Proj(next) || is_Phi(next) || be_is_Keep(next));
150 spill_info->spill = env->create_spill(env->func_env, state, force, after);
156 void create_reload(minibelady_env_t *env, ir_node *state, ir_node *before,
159 spill_info_t *spill_info = create_spill(env, state, 0);
160 ir_node *spill = spill_info->spill;
163 reload = env->create_reload(env->func_env, state, spill, before,
165 ARR_APP1(ir_node*, spill_info->reloads, reload);
169 void spill_phi(minibelady_env_t *env, ir_node *phi)
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;
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;
183 spill_info = create_spill_info(env, phi);
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);
191 DBG((dbg, LEVEL_2, "\tcreate Phi-M for %+F\n", phi));
194 spill_info->spill = new_r_Phi(irg, block, arity, in, mode_M);
196 if(spill_to_kill != NULL) {
197 exchange(spill_to_kill, spill_info->spill);
198 sched_remove(spill_to_kill);
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);
210 void belady(minibelady_env_t *env, ir_node *block);
213 * Collects all values live-in at block @p block and all phi results in this
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
221 block_info_t *compute_block_start_state(minibelady_env_t *env, ir_node *block)
223 block_info_t *block_info;
224 be_next_use_t next_use;
226 ir_node *best_starter, *first;
230 int outer_loop_allowed;
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);
237 /* no cfgpred -> no value active */
238 if(n_cfgpreds == 0) {
239 block_info->start_state = NULL;
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;
248 /* process pred block */
249 belady(env, pred_block);
251 pred_info = get_block_info(pred_block);
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;
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);
263 best_time = USES_INFINITY;
264 outer_loop_allowed = 1;
266 /* check all Phis first */
267 sched_foreach(block, node) {
270 if (arch_get_irn_register(node) != env->reg)
273 DBG((dbg, LEVEL_2, "\t...checking %+F\n", node));
274 next_use = be_get_next_use(env->uses, first, 0, node, 0);
276 if(USES_IS_INFINITE(next_use.time)) {
277 DBG((dbg, LEVEL_2, "\tnot taken (dead)\n"));
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));
286 if(best_starter != NULL) {
287 /* spill the phi as it is not used */
288 spill_phi(env, best_starter);
291 best_time = next_use.time;
292 outer_loop_allowed = 0;
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);
303 best_time = next_use.time;
307 if(best_starter != node) {
308 /* spill the phi as it is not used */
309 spill_phi(env, best_starter);
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);
317 if(!mode_is_data(get_irn_mode(node)))
320 if (arch_get_irn_register(node) != env->reg)
323 DBG((dbg, LEVEL_2, "\t...checking %+F\n", node));
324 next_use = be_get_next_use(env->uses, first, 0, node, 0);
326 if(USES_IS_INFINITE(next_use.time)) {
327 DBG((dbg, LEVEL_2, "\tnot taken (dead)\n"));
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));
336 if(best_starter != NULL && is_Phi(best_starter)) {
337 /* spill the phi as it is not used */
338 spill_phi(env, best_starter);
341 best_time = next_use.time;
342 outer_loop_allowed = 0;
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);
353 best_time = next_use.time;
358 block_info->start_state = best_starter;
364 * For the given block @p block, decide for each values
365 * whether it is used from a register or is reloaded
369 void belady(minibelady_env_t *env, ir_node *block)
371 ir_node *current_state;
373 block_info_t *block_info;
375 /* Don't do a block twice */
376 if(irn_visited(block))
379 /* compute value to start with */
380 block_info = compute_block_start_state(env, block);
382 /* get the starting workset for this block */
383 DBG((dbg, LEVEL_3, "\n"));
384 DBG((dbg, LEVEL_3, "Decide for %+F\n", block));
386 current_state = block_info->start_state;
387 DBG((dbg, LEVEL_3, "Start value: %+F\n", current_state));
389 /* process the block from start to end */
390 DBG((dbg, LEVEL_3, "Processing...\n"));
392 sched_foreach(block, node) {
394 ir_node *need_val = NULL;
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)) {
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);
408 if(!mode_is_data(get_irn_mode(in)))
411 reg = arch_get_irn_register(in);
412 if(reg == env->reg) {
413 assert(need_val == NULL);
415 DBG((dbg, LEVEL_3, "\t... need state %+F\n", need_val));
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;
425 DBG((dbg, LEVEL_3, " ...%+F\n", node));
427 /* record state changes by the node */
428 if (get_irn_mode(node) == mode_T) {
429 const ir_edge_t *edge;
431 foreach_out_edge(node, edge) {
432 const arch_register_t *reg;
433 ir_node *proj = get_edge_src_irn(edge);
435 if(!mode_is_data(get_irn_mode(proj)))
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));
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));
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));
461 void belady_walker(ir_node *block, void *data)
463 belady((minibelady_env_t*) data, block);
467 ir_node *get_end_of_block_insertion_point(ir_node *block)
469 ir_node *last = sched_last(block);
471 /* skip Projs and Keep-alikes behind the jump... */
472 while(is_Proj(last) || be_is_Keep(last)) {
473 last = sched_prev(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)));
486 * We must adapt the live-outs to the live-ins at each block-border.
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);
495 block_info_t *block_info;
497 if(block == startblock)
500 DBG((dbg, LEVEL_3, "\n"));
502 block_info = get_block_info(block);
504 DBG((dbg, LEVEL_3, "Fixing %+F (needs %+F)\n", block,
505 block_info->start_state));
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;
514 if(need_state == NULL)
517 if(is_Phi(need_state) && get_nodes_block(need_state) == block) {
518 need_state = get_irn_n(need_state, i);
521 DBG((dbg, LEVEL_3, " Pred %+F (ends in %+F, we need %+F)\n", pred,
522 pred_info->end_state, need_state));
524 if(pred_info->end_state != need_state) {
525 ir_node *insert_point = get_end_of_block_insertion_point(pred);
528 DBG((dbg, LEVEL_3, " Creating reload for %+F\n", need_state));
529 create_reload(env, need_state, insert_point, pred_info->end_state);
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);
540 be_lv_t *lv = be_assure_liveness(birg);
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);
548 obstack_init(&env.obst);
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);
556 ir_nodemap_init(&env.spill_infos);
559 ir_reserve_resources(irg, IR_RESOURCE_IRN_VISITED | IR_RESOURCE_IRN_LINK);
560 inc_irg_visited(irg);
563 irg_block_walk_graph(irg, NULL, belady_walker, &env);
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);
568 ir_free_resources(irg, IR_RESOURCE_IRN_VISITED | IR_RESOURCE_IRN_LINK);
570 /* reconstruct ssa-form */
572 while(info != NULL) {
573 be_ssa_construction_env_t senv;
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);
585 be_ssa_construction_update_liveness_phis(&senv, lv);
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);
595 phis = be_ssa_construction_get_new_phis(&senv);
597 /* set register requirements for 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);
604 be_ssa_construction_destroy(&senv);
609 /* some nodes might be dead now. */
610 be_remove_dead_nodes_from_schedule(birg);
612 ir_nodemap_destroy(&env.spill_infos);
613 be_end_uses(env.uses);
614 obstack_free(&env.obst, NULL);
617 void be_init_state(void)
619 FIRM_DBG_REGISTER(dbg, "firm.be.state");
622 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_state);