2 * Copyright (C) 1995-2007 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_env_t *arch_env;
66 const arch_register_t *reg;
69 create_reload_func create_reload;
70 create_spill_func create_spill;
72 ir_nodemap_t spill_infos;
74 be_uses_t *uses; /**< env for the next-use magic */
77 typedef struct block_info_t {
83 block_info_t *new_block_info(struct obstack *obst, ir_node *block)
85 block_info_t *res = obstack_alloc(obst, sizeof(*res));
86 memset(res, 0, sizeof(res[0]));
88 assert(is_Block(block));
89 set_irn_link(block, res);
90 mark_irn_visited(block);
96 block_info_t *get_block_info(ir_node *block)
98 assert(irn_visited(block));
99 return (block_info_t*) get_irn_link(block);
103 spill_info_t *create_spill_info(minibelady_env_t *env, ir_node *state)
105 spill_info_t *spill_info = obstack_alloc(&env->obst, sizeof(spill_info[0]));
106 memset(spill_info, 0, sizeof(spill_info[0]));
107 spill_info->value = state;
108 spill_info->reloads = NEW_ARR_F(ir_node*, 0);
110 ir_nodemap_insert(&env->spill_infos, state, spill_info);
111 //ir_fprintf(stderr, "Insert %+F -> %p\n", state, spill_info);
113 spill_info->next = env->spills;
114 env->spills = spill_info;
120 spill_info_t *get_spill_info(minibelady_env_t *env, const ir_node *node)
122 spill_info_t *spill_info
123 = (spill_info_t*) ir_nodemap_get(&env->spill_infos, node);
124 //ir_fprintf(stderr, "Get %+F -> %p\n", node, spill_info);
129 spill_info_t *create_spill(minibelady_env_t *env, ir_node *state, int force)
131 spill_info_t *spill_info;
135 spill_info = get_spill_info(env, state);
136 if(spill_info == NULL) {
137 spill_info = create_spill_info(env, state);
138 } else if(spill_info->spill != NULL) {
142 if(sched_is_scheduled(state)) {
146 next = sched_next(after);
147 } while(is_Proj(next) || is_Phi(next) || be_is_Keep(next));
151 spill_info->spill = env->create_spill(env->func_env, state, force, after);
157 void create_reload(minibelady_env_t *env, ir_node *state, ir_node *before,
160 spill_info_t *spill_info = create_spill(env, state, 0);
161 ir_node *spill = spill_info->spill;
164 reload = env->create_reload(env->func_env, state, spill, before,
166 ARR_APP1(ir_node*, spill_info->reloads, reload);
170 void spill_phi(minibelady_env_t *env, ir_node *phi)
172 ir_graph *irg = get_irn_irg(phi);
173 ir_node *block = get_nodes_block(phi);
174 int i, arity = get_irn_arity(phi);
175 ir_node **in = alloca(arity * sizeof(in[0]));
176 ir_node *spill_to_kill = NULL;
177 spill_info_t *spill_info;
179 /* does a spill exist for the phis value? */
180 spill_info = get_spill_info(env, phi);
181 if(spill_info != NULL) {
182 spill_to_kill = spill_info->spill;
184 spill_info = create_spill_info(env, phi);
187 /* create a new phi-M with bad preds */
188 for(i = 0; i < arity; ++i) {
189 in[i] = new_r_Bad(irg);
192 DBG((dbg, LEVEL_2, "\tcreate Phi-M for %+F\n", phi));
195 spill_info->spill = new_r_Phi(irg, block, arity, in, mode_M);
197 if(spill_to_kill != NULL) {
198 exchange(spill_to_kill, spill_info->spill);
199 sched_remove(spill_to_kill);
202 /* create spills for the phi values */
203 for(i = 0; i < arity; ++i) {
204 ir_node *in = get_irn_n(phi, i);
205 spill_info_t *pred_spill = create_spill(env, in, 1);
206 set_irn_n(spill_info->spill, i, pred_spill->spill);
211 void belady(minibelady_env_t *env, ir_node *block);
214 * Collects all values live-in at block @p block and all phi results in this
216 * Then it adds the best values (at most n_regs) to the blocks start_workset.
217 * The phis among the remaining values get spilled: Introduce psudo-copies of
218 * their args to break interference and make it possible to spill them to the
222 block_info_t *compute_block_start_state(minibelady_env_t *env, ir_node *block)
224 block_info_t *block_info;
225 be_next_use_t next_use;
227 ir_node *best_starter, *first;
231 int outer_loop_allowed;
234 /* Create the block info for this block. */
235 block_info = new_block_info(&env->obst, block);
236 n_cfgpreds = get_Block_n_cfgpreds(block);
238 /* no cfgpred -> no value active */
239 if(n_cfgpreds == 0) {
240 block_info->start_state = NULL;
244 /* for 1 pred only: simply take the the end-state of the pred */
245 if(n_cfgpreds == 1) {
246 ir_node *pred_block = get_Block_cfgpred_block(block, 0);
247 block_info_t *pred_info;
249 /* process pred block */
250 belady(env, pred_block);
252 pred_info = get_block_info(pred_block);
254 DBG((dbg, LEVEL_2, "Taking end state from %+F: %+F\n", pred_block, pred_info->end_state));
255 block_info->start_state = pred_info->end_state;
259 /* Collect all values living at start of block */
260 DBG((dbg, LEVEL_2, "Living at start of %+F:\n", block));
261 first = sched_first(block);
262 loop = get_irn_loop(block);
264 best_time = USES_INFINITY;
265 outer_loop_allowed = 1;
267 /* check all Phis first */
268 sched_foreach(block, node) {
271 if (arch_get_irn_register(env->arch_env, node) != env->reg)
274 DBG((dbg, LEVEL_2, "\t...checking %+F\n", node));
275 next_use = be_get_next_use(env->uses, first, 0, node, 0);
277 if(USES_IS_INFINITE(next_use.time)) {
278 DBG((dbg, LEVEL_2, "\tnot taken (dead)\n"));
282 if(next_use.outermost_loop >= get_loop_depth(loop)) {
283 if(outer_loop_allowed || next_use.time < best_time) {
284 DBG((dbg, LEVEL_2, "\ttaken (%u, loop %d)\n", next_use.time,
285 next_use.outermost_loop));
287 if(best_starter != NULL) {
288 /* spill the phi as it is not used */
289 spill_phi(env, best_starter);
292 best_time = next_use.time;
293 outer_loop_allowed = 0;
296 if(outer_loop_allowed && next_use.time < best_time) {
297 DBG((dbg, LEVEL_2, "\ttaken (%u, loop %d)\n", next_use.time,
298 next_use.outermost_loop));
299 if(best_starter != NULL) {
300 /* spill the phi as it is not used */
301 spill_phi(env, best_starter);
304 best_time = next_use.time;
308 if(best_starter != node) {
309 /* spill the phi as it is not used */
310 spill_phi(env, best_starter);
314 /* check all Live-Ins */
315 be_lv_foreach(env->lv, block, be_lv_state_in, i) {
316 node = be_lv_get_irn(env->lv, block, i);
318 if (arch_get_irn_register(env->arch_env, node) != env->reg)
321 DBG((dbg, LEVEL_2, "\t...checking %+F\n", node));
322 next_use = be_get_next_use(env->uses, first, 0, node, 0);
324 if(USES_IS_INFINITE(next_use.time)) {
325 DBG((dbg, LEVEL_2, "\tnot taken (dead)\n"));
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));
334 if(best_starter != NULL && is_Phi(best_starter)) {
335 /* spill the phi as it is not used */
336 spill_phi(env, best_starter);
339 best_time = next_use.time;
340 outer_loop_allowed = 0;
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);
351 best_time = next_use.time;
356 block_info->start_state = best_starter;
362 * For the given block @p block, decide for each values
363 * whether it is used from a register or is reloaded
367 void belady(minibelady_env_t *env, ir_node *block)
369 ir_node *current_state;
371 block_info_t *block_info;
373 /* Don't do a block twice */
374 if(irn_visited(block))
377 /* compute value to start with */
378 block_info = compute_block_start_state(env, block);
380 /* get the starting workset for this block */
381 DBG((dbg, LEVEL_3, "\n"));
382 DBG((dbg, LEVEL_3, "Decide for %+F\n", block));
384 current_state = block_info->start_state;
385 DBG((dbg, LEVEL_3, "Start value: %+F\n", current_state));
387 /* process the block from start to end */
388 DBG((dbg, LEVEL_3, "Processing...\n"));
390 sched_foreach(block, node) {
392 ir_node *need_val = NULL;
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)) {
400 /* check which state is desired for the node */
401 arity = get_irn_arity(node);
402 for(i = 0; i < arity; ++i) {
403 ir_node *in = get_irn_n(node, i);
405 if(!mode_is_data(get_irn_mode(in)))
408 const arch_register_t *reg =
409 arch_get_irn_register(env->arch_env, in);
410 if(reg == env->reg) {
411 assert(need_val == NULL);
413 DBG((dbg, LEVEL_3, "\t... need state %+F\n", need_val));
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;
423 DBG((dbg, LEVEL_3, " ...%+F\n", node));
425 /* record state changes by the node */
426 if (get_irn_mode(node) == mode_T) {
427 const ir_edge_t *edge;
429 foreach_out_edge(node, edge) {
430 ir_node *proj = get_edge_src_irn(edge);
432 if(!mode_is_data(get_irn_mode(proj)))
435 const arch_register_t *reg =
436 arch_get_irn_register(env->arch_env, proj);
437 if(reg == env->reg) {
438 current_state = proj;
439 DBG((dbg, LEVEL_3, "\t... current_state <- %+F\n", current_state));
443 if(mode_is_data(get_irn_mode(node))) {
444 const arch_register_t *reg =
445 arch_get_irn_register(env->arch_env, node);
446 if(reg == env->reg) {
447 current_state = node;
448 DBG((dbg, LEVEL_3, "\t... current_state <- %+F\n", current_state));
454 /* Remember end-workset for this block */
455 block_info->end_state = current_state;
456 DBG((dbg, LEVEL_3, "End value for %+F: %+F\n", block, current_state));
460 void belady_walker(ir_node *block, void *data)
462 belady((minibelady_env_t*) data, block);
466 ir_node *get_end_of_block_insertion_point(ir_node *block)
468 ir_node *last = sched_last(block);
470 /* skip Projs and Keep-alikes behind the jump... */
471 while(is_Proj(last) || be_is_Keep(last)) {
472 last = sched_prev(last);
476 last = sched_next(last);
477 /* last node must be a cfop, only exception is the start block */
478 assert(last == get_irg_start_block(get_irn_irg(block)));
485 * We must adapt the live-outs to the live-ins at each block-border.
488 void fix_block_borders(ir_node *block, void *data) {
489 minibelady_env_t *env = data;
490 ir_graph *irg = get_irn_irg(block);
491 ir_node *startblock = get_irg_start_block(irg);
494 block_info_t *block_info;
496 if(block == startblock)
499 DBG((dbg, LEVEL_3, "\n"));
501 block_info = get_block_info(block);
503 DBG((dbg, LEVEL_3, "Fixing %+F (needs %+F)\n", block,
504 block_info->start_state));
506 /* process all pred blocks */
507 arity = get_irn_arity(block);
508 for (i = 0; i < arity; ++i) {
509 ir_node *pred = get_Block_cfgpred_block(block, i);
510 block_info_t *pred_info = get_block_info(pred);
511 ir_node *need_state = block_info->start_state;
513 if(need_state == NULL)
516 if(is_Phi(need_state) && get_nodes_block(need_state) == block) {
517 need_state = get_irn_n(need_state, i);
520 DBG((dbg, LEVEL_3, " Pred %+F (ends in %+F, we need %+F)\n", pred,
521 pred_info->end_state, need_state));
523 if(pred_info->end_state != need_state) {
524 ir_node *insert_point = get_end_of_block_insertion_point(pred);
527 DBG((dbg, LEVEL_3, " Creating reload for %+F\n", need_state));
528 create_reload(env, need_state, insert_point, pred_info->end_state);
533 void be_assure_state(be_irg_t *birg, const arch_register_t *reg, void *func_env,
534 create_spill_func create_spill,
535 create_reload_func create_reload) {
536 minibelady_env_t env;
537 ir_graph *irg = be_get_birg_irg(birg);
539 be_lv_t *lv = be_assure_liveness(birg);
541 be_liveness_assure_sets(lv);
542 be_assure_dom_front(birg);
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);
549 env.arch_env = be_get_birg_arch_env(birg);
551 env.func_env = func_env;
552 env.create_spill = create_spill;
553 env.create_reload = create_reload;
554 env.lv = be_get_birg_liveness(birg);
555 env.uses = be_begin_uses(irg, env.lv);
557 ir_nodemap_init(&env.spill_infos);
560 set_using_visited(irg);
561 set_using_irn_link(irg);
562 inc_irg_visited(irg);
565 irg_block_walk_graph(irg, NULL, belady_walker, &env);
567 /* fix block end_states that don't match the next blocks start_state */
568 irg_block_walk_graph(irg, fix_block_borders, NULL, &env);
570 clear_using_visited(irg);
571 clear_using_irn_link(irg);
573 /* reconstruct ssa-form */
575 while(info != NULL) {
576 be_ssa_construction_env_t senv;
580 be_ssa_construction_init(&senv, birg);
581 if(sched_is_scheduled(info->value))
582 be_ssa_construction_add_copy(&senv, info->value);
583 be_ssa_construction_add_copies(&senv,
584 info->reloads, ARR_LEN(info->reloads));
585 be_ssa_construction_fix_users(&senv, info->value);
588 be_ssa_construction_update_liveness_phis(&senv, lv);
590 be_liveness_update(lv, info->value);
591 len = ARR_LEN(info->reloads);
592 for(i = 0; i < len; ++i) {
593 ir_node *reload = info->reloads[i];
594 be_liveness_update(lv, reload);
598 phis = be_ssa_construction_get_new_phis(&senv);
600 /* set register requirements for phis */
602 for(i = 0; i < len; ++i) {
603 ir_node *phi = phis[i];
604 be_set_phi_flags(env.arch_env, phi, arch_irn_flags_ignore);
605 arch_set_irn_register(env.arch_env, phi, env.reg);
607 be_ssa_construction_destroy(&senv);
612 /* some nodes might be dead now. */
613 be_remove_dead_nodes_from_schedule(birg);
615 ir_nodemap_destroy(&env.spill_infos);
616 be_end_uses(env.uses);
617 obstack_free(&env.obst, NULL);
620 void be_init_state(void)
622 FIRM_DBG_REGISTER(dbg, "firm.be.state");
625 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_state);