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
27 * Copyright: (c) Universitaet Karlsruhe
28 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
37 #include "irgraph_t.h"
41 #include "iredges_t.h"
47 #include "besched_t.h"
51 #include "bessaconstr.h"
53 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
55 typedef struct spill_info_t {
56 struct spill_info_t *next;
62 typedef struct minibelady_env_t {
64 const arch_env_t *arch_env;
65 const arch_register_t *reg;
68 create_reload_func create_reload;
69 create_spill_func create_spill;
72 be_uses_t *uses; /**< env for the next-use magic */
75 typedef struct block_info_t {
81 block_info_t *new_block_info(struct obstack *obst, ir_node *block)
83 block_info_t *res = obstack_alloc(obst, sizeof(*res));
84 memset(res, 0, sizeof(res[0]));
86 set_irn_link(block, res);
87 mark_irn_visited(block);
93 block_info_t *get_block_info(ir_node *block)
95 assert(irn_visited(block));
96 return (block_info_t*) get_irn_link(block);
100 spill_info_t *create_spill_info(minibelady_env_t *env, ir_node *state)
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);
107 set_irn_link(state, spill_info);
108 mark_irn_visited(state);
110 spill_info->next = env->spills;
111 env->spills = spill_info;
117 spill_info_t *create_spill(minibelady_env_t *env, ir_node *state, int force)
119 spill_info_t *spill_info;
123 if(irn_visited(state)) {
124 spill_info = (spill_info_t*) get_irn_link(state);
125 if(spill_info->spill != NULL || !force)
128 spill_info = create_spill_info(env, state);
131 if(sched_is_scheduled(state)) {
135 next = sched_next(after);
136 } while(is_Proj(next) || is_Phi(next) || be_is_Keep(next));
140 spill_info->spill = env->create_spill(env->func_env, state, force, after);
146 void create_reload(minibelady_env_t *env, ir_node *state, ir_node *before,
149 spill_info_t *spill_info = create_spill(env, state, 0);
150 ir_node *spill = spill_info->spill;
153 reload = env->create_reload(env->func_env, state, spill, before,
155 ARR_APP1(ir_node*, spill_info->reloads, reload);
159 void spill_phi(minibelady_env_t *env, ir_node *phi)
161 ir_graph *irg = get_irn_irg(phi);
162 ir_node *block = get_nodes_block(phi);
163 int i, arity = get_irn_arity(phi);
164 ir_node **in = alloca(arity * sizeof(in[0]));
165 ir_node *spill_to_kill = NULL;
166 spill_info_t *spill_info;
168 /* does a spill exist for the phis value? */
169 if(irn_visited(phi)) {
170 spill_info = (spill_info_t*) get_irn_link(phi);
171 spill_to_kill = spill_info->spill;
173 spill_info = create_spill_info(env, phi);
176 /* create a new phi-M with bad preds */
177 for(i = 0; i < arity; ++i) {
178 in[i] = new_r_Bad(irg);
181 DBG((dbg, LEVEL_2, "\tcreate Phi-M for %+F\n", phi));
184 spill_info->spill = new_r_Phi(irg, block, arity, in, mode_M);
186 if(spill_to_kill != NULL) {
187 exchange(spill_to_kill, spill_info->spill);
188 sched_remove(spill_to_kill);
191 /* create spills for the phi values */
192 for(i = 0; i < arity; ++i) {
193 ir_node *in = get_irn_n(phi, i);
194 spill_info_t *pred_spill = create_spill(env, in, 1);
195 set_irn_n(spill_info->spill, i, pred_spill->spill);
200 void belady(minibelady_env_t *env, ir_node *block);
203 * Collects all values live-in at block @p block and all phi results in this
205 * Then it adds the best values (at most n_regs) to the blocks start_workset.
206 * The phis among the remaining values get spilled: Introduce psudo-copies of
207 * their args to break interference and make it possible to spill them to the
211 block_info_t *compute_block_start_state(minibelady_env_t *env, ir_node *block)
213 block_info_t *block_info;
214 be_next_use_t next_use;
216 ir_node *best_starter, *first;
220 int outer_loop_allowed;
223 /* Create the block info for this block. */
224 block_info = new_block_info(&env->obst, block);
225 n_cfgpreds = get_Block_n_cfgpreds(block);
227 /* no cfgpred -> no value active */
228 if(n_cfgpreds == 0) {
229 block_info->start_state = NULL;
233 /* for 1 pred only: simply take the the end-state of the pred */
234 if(n_cfgpreds == 1) {
235 ir_node *pred_block = get_Block_cfgpred_block(block, 0);
236 block_info_t *pred_info;
238 /* process pred block */
239 belady(env, pred_block);
241 pred_info = get_block_info(pred_block);
243 DBG((dbg, LEVEL_2, "Taking end state from %+F: %+F\n", pred_block, pred_info->end_state));
244 block_info->start_state = pred_info->end_state;
248 /* Collect all values living at start of block */
249 DBG((dbg, LEVEL_2, "Living at start of %+F:\n", block));
250 first = sched_first(block);
251 loop = get_irn_loop(block);
253 best_time = USES_INFINITY;
254 outer_loop_allowed = 1;
256 /* check all Phis first */
257 sched_foreach(block, node) {
260 if (arch_get_irn_register(env->arch_env, node) != env->reg)
263 DBG((dbg, LEVEL_2, "\t...checking %+F\n", node));
264 next_use = be_get_next_use(env->uses, first, 0, node, 0);
266 if(USES_IS_INFINITE(next_use.time)) {
267 DBG((dbg, LEVEL_2, "\tnot taken (dead)\n"));
271 if(next_use.outermost_loop >= get_loop_depth(loop)) {
272 if(outer_loop_allowed || next_use.time < best_time) {
273 DBG((dbg, LEVEL_2, "\ttaken (%u, loop %d)\n", next_use.time,
274 next_use.outermost_loop));
276 if(best_starter != NULL) {
277 /* spill the phi as it is not used */
278 spill_phi(env, best_starter);
281 best_time = next_use.time;
282 outer_loop_allowed = 0;
285 if(outer_loop_allowed && next_use.time < best_time) {
286 DBG((dbg, LEVEL_2, "\ttaken (%u, loop %d)\n", next_use.time,
287 next_use.outermost_loop));
288 if(best_starter != NULL) {
289 /* spill the phi as it is not used */
290 spill_phi(env, best_starter);
293 best_time = next_use.time;
297 if(best_starter != node) {
298 /* spill the phi as it is not used */
299 spill_phi(env, best_starter);
303 /* check all Live-Ins */
304 be_lv_foreach(env->lv, block, be_lv_state_in, i) {
305 node = be_lv_get_irn(env->lv, block, i);
307 if (arch_get_irn_register(env->arch_env, node) != env->reg)
310 DBG((dbg, LEVEL_2, "\t...checking %+F\n", node));
311 next_use = be_get_next_use(env->uses, first, 0, node, 0);
313 if(USES_IS_INFINITE(next_use.time)) {
314 DBG((dbg, LEVEL_2, "\tnot taken (dead)\n"));
318 if(next_use.outermost_loop >= get_loop_depth(loop)) {
319 if(outer_loop_allowed || next_use.time < best_time) {
320 DBG((dbg, LEVEL_2, "\ttaken (%u, loop %d)\n", next_use.time,
321 next_use.outermost_loop));
323 if(best_starter != NULL && is_Phi(best_starter)) {
324 /* spill the phi as it is not used */
325 spill_phi(env, best_starter);
328 best_time = next_use.time;
329 outer_loop_allowed = 0;
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 if(best_starter != NULL && is_Phi(best_starter)) {
336 /* spill the phi as it is not used */
337 spill_phi(env, best_starter);
340 best_time = next_use.time;
345 block_info->start_state = best_starter;
351 * For the given block @p block, decide for each values
352 * whether it is used from a register or is reloaded
356 void belady(minibelady_env_t *env, ir_node *block)
358 ir_node *current_state;
360 block_info_t *block_info;
362 /* Don't do a block twice */
363 if(irn_visited(block))
366 /* compute value to start with */
367 block_info = compute_block_start_state(env, block);
369 /* get the starting workset for this block */
370 DBG((dbg, LEVEL_3, "\n"));
371 DBG((dbg, LEVEL_3, "Decide for %+F\n", block));
373 current_state = block_info->start_state;
374 DBG((dbg, LEVEL_3, "Start value: %+F\n", current_state));
376 /* process the block from start to end */
377 DBG((dbg, LEVEL_3, "Processing...\n"));
379 sched_foreach(block, node) {
381 ir_node *need_val = NULL;
383 /* projs are handled with the tuple value.
384 * Phis are no real instr (see insert_starters()) */
385 if (is_Proj(node) || is_Phi(node)) {
389 /* check which state is desired for the node */
390 arity = get_irn_arity(node);
391 for(i = 0; i < arity; ++i) {
392 ir_node *in = get_irn_n(node, i);
393 const arch_register_t *reg =
394 arch_get_irn_register(env->arch_env, in);
395 if(reg == env->reg) {
396 assert(need_val == NULL);
398 DBG((dbg, LEVEL_3, "\t... need state %+F\n", need_val));
401 /* create a reload to match state if necessary */
402 if(need_val != NULL && need_val != current_state) {
403 DBG((dbg, LEVEL_3, "\t... reloading %+F\n", need_val));
404 create_reload(env, need_val, node, current_state);
405 current_state = need_val;
408 DBG((dbg, LEVEL_3, " ...%+F\n", node));
410 /* record state changes by the node */
411 if (get_irn_mode(node) == mode_T) {
413 for(proj = sched_next(node); is_Proj(proj);
414 proj = sched_next(proj)) {
415 const arch_register_t *reg =
416 arch_get_irn_register(env->arch_env, proj);
417 if(reg == env->reg) {
418 current_state = proj;
419 DBG((dbg, LEVEL_3, "\t... current_state <- %+F\n", current_state));
423 const arch_register_t *reg =
424 arch_get_irn_register(env->arch_env, node);
425 if(reg == env->reg) {
426 current_state = node;
427 DBG((dbg, LEVEL_3, "\t... current_state <- %+F\n", current_state));
432 /* Remember end-workset for this block */
433 block_info->end_state = current_state;
434 DBG((dbg, LEVEL_3, "End value for %+F: %+F\n", block, current_state));
438 void belady_walker(ir_node *block, void *data)
440 belady((minibelady_env_t*) data, block);
444 ir_node *get_end_of_block_insertion_point(ir_node *block)
446 ir_node *last = sched_last(block);
448 /* skip projs and keepanies behind the jump... */
449 while(is_Proj(last) || be_is_Keep(last)) {
450 last = sched_prev(last);
454 last = sched_next(last);
455 // last node must be a cfop, only exception is the start block
456 assert(last == get_irg_start_block(get_irn_irg(block)));
463 * We must adapt the live-outs to the live-ins at each block-border.
466 void fix_block_borders(ir_node *block, void *data) {
467 minibelady_env_t *env = data;
468 ir_graph *irg = get_irn_irg(block);
469 ir_node *startblock = get_irg_start_block(irg);
472 block_info_t *block_info;
474 if(block == startblock)
477 DBG((dbg, LEVEL_3, "\n"));
478 DBG((dbg, LEVEL_3, "Fixing %+F\n", block));
480 block_info = get_block_info(block);
482 /* process all pred blocks */
483 arity = get_irn_arity(block);
484 for (i = 0; i < arity; ++i) {
485 ir_node *pred = get_Block_cfgpred_block(block, i);
486 block_info_t *pred_info = get_block_info(pred);
488 DBG((dbg, LEVEL_3, " Pred %+F\n", pred));
490 if(pred_info->end_state != block_info->start_state &&
491 block_info->start_state != NULL) {
492 ir_node *need_state = block_info->start_state;
493 ir_node *insert_point =
494 get_end_of_block_insertion_point(pred);
496 create_reload(env, need_state, insert_point, pred_info->end_state);
501 void be_assure_state(be_irg_t *birg, const arch_register_t *reg, void *func_env,
502 create_spill_func create_spill,
503 create_reload_func create_reload) {
504 minibelady_env_t env;
505 ir_graph *irg = be_get_birg_irg(birg);
507 be_lv_t *lv = be_get_birg_liveness(birg);
509 be_assure_liveness(birg);
510 be_assure_dom_front(birg);
511 /* construct control flow loop tree */
512 if(! (get_irg_loopinfo_state(irg) & loopinfo_cf_consistent)) {
513 construct_cf_backedges(irg);
516 obstack_init(&env.obst);
517 env.arch_env = be_get_birg_arch_env(birg);
519 env.func_env = func_env;
520 env.create_spill = create_spill;
521 env.create_reload = create_reload;
522 env.lv = be_get_birg_liveness(birg);
523 env.uses = be_begin_uses(irg, env.lv);
526 set_using_visited(irg);
527 set_using_irn_link(irg);
528 inc_irg_visited(irg);
531 irg_block_walk_graph(irg, NULL, belady_walker, &env);
533 /* fix block end_states that don't match the next blocks start_state */
534 irg_block_walk_graph(irg, fix_block_borders, NULL, &env);
536 clear_using_visited(irg);
537 clear_using_irn_link(irg);
539 /* reconstruct ssa-form */
541 while(info != NULL) {
542 be_ssa_construction_env_t senv;
546 be_ssa_construction_init(&senv, birg);
547 if(sched_is_scheduled(info->value))
548 be_ssa_construction_add_copy(&senv, info->value);
549 be_ssa_construction_add_copies(&senv,
550 info->reloads, ARR_LEN(info->reloads));
551 be_ssa_construction_fix_users(&senv, info->value);
554 be_ssa_construction_update_liveness_phis(&senv, lv);
556 be_liveness_update(lv, info->value);
557 len = ARR_LEN(info->reloads);
558 for(i = 0; i < len; ++i) {
559 ir_node *reload = info->reloads[i];
560 be_liveness_update(lv, reload);
564 phis = be_ssa_construction_get_new_phis(&senv);
566 /* set register requirements for phis */
568 for(i = 0; i < len; ++i) {
569 ir_node *phi = phis[i];
570 be_set_phi_flags(env.arch_env, phi, arch_irn_flags_ignore);
571 arch_set_irn_register(env.arch_env, phi, env.reg);
573 be_ssa_construction_destroy(&senv);
578 /* some nodes might be dead now. */
579 be_remove_dead_nodes_from_schedule(irg);
581 be_end_uses(env.uses);
582 obstack_free(&env.obst, NULL);
585 void be_init_state(void)
587 FIRM_DBG_REGISTER(dbg, "firm.be.state");
590 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_state);