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
33 #include "irgraph_t.h"
37 #include "iredges_t.h"
40 #include "irnodeset.h"
41 #include "irnodemap.h"
42 #include "adt/cpset.h"
50 #include "bessaconstr.h"
52 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
54 typedef struct spill_info_t {
55 struct spill_info_t *next;
61 typedef struct minibelady_env_t {
63 const arch_register_t *reg;
66 create_reload_func create_reload;
67 create_spill_func create_spill;
69 ir_nodemap_t spill_infos;
71 be_uses_t *uses; /**< env for the next-use magic */
74 typedef struct block_info_t {
79 static inline block_info_t *new_block_info(struct obstack *obst, ir_node *block)
81 block_info_t *res = OALLOCZ(obst, block_info_t);
83 assert(is_Block(block));
84 set_irn_link(block, res);
85 mark_irn_visited(block);
90 static inline block_info_t *get_block_info(ir_node *block)
92 assert(irn_visited(block));
93 return (block_info_t*) get_irn_link(block);
96 static inline spill_info_t *create_spill_info(minibelady_env_t *env, ir_node *state)
98 spill_info_t *spill_info = OALLOCZ(&env->obst, spill_info_t);
99 spill_info->value = state;
100 spill_info->reloads = NEW_ARR_F(ir_node*, 0);
102 ir_nodemap_insert(&env->spill_infos, state, spill_info);
103 //ir_fprintf(stderr, "Insert %+F -> %p\n", state, spill_info);
105 spill_info->next = env->spills;
106 env->spills = spill_info;
111 static inline spill_info_t *get_spill_info(minibelady_env_t *env, const ir_node *node)
113 spill_info_t *spill_info
114 = (spill_info_t*) ir_nodemap_get(&env->spill_infos, node);
115 //ir_fprintf(stderr, "Get %+F -> %p\n", node, spill_info);
119 static spill_info_t *create_spill(minibelady_env_t *env, ir_node *state, int force)
121 spill_info_t *spill_info;
125 spill_info = get_spill_info(env, state);
126 if (spill_info == NULL) {
127 spill_info = create_spill_info(env, state);
128 } else if (spill_info->spill != NULL) {
132 if (sched_is_scheduled(state)) {
136 next = sched_next(after);
137 } while (is_Proj(next) || is_Phi(next) || be_is_Keep(next));
141 spill_info->spill = env->create_spill(env->func_env, state, force, after);
146 static void create_reload(minibelady_env_t *env, ir_node *state,
147 ir_node *before, ir_node *last_state)
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);
158 static void spill_phi(minibelady_env_t *env, ir_node *phi)
160 ir_graph *irg = get_irn_irg(phi);
161 ir_node *block = get_nodes_block(phi);
162 int arity = get_irn_arity(phi);
163 ir_node **in = ALLOCAN(ir_node*, arity);
164 ir_node *spill_to_kill = NULL;
165 spill_info_t *spill_info;
168 /* does a spill exist for the phis value? */
169 spill_info = get_spill_info(env, phi);
170 if (spill_info != NULL) {
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_Unknown(irg, mode_M);
181 DBG((dbg, LEVEL_2, "\tcreate Phi-M for %+F\n", phi));
184 spill_info->spill = new_r_Phi(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);
199 static void belady(minibelady_env_t *env, ir_node *block);
202 * Collects all values live-in at block @p block and all phi results in this
204 * Then it adds the best values (at most n_regs) to the blocks start_workset.
205 * The phis among the remaining values get spilled: Introduce psudo-copies of
206 * their args to break interference and make it possible to spill them to the
209 static block_info_t *compute_block_start_state(minibelady_env_t *env, ir_node *block)
211 block_info_t *block_info;
212 be_next_use_t next_use;
214 ir_node *best_starter, *first;
218 int outer_loop_allowed;
221 /* Create the block info for this block. */
222 block_info = new_block_info(&env->obst, block);
223 n_cfgpreds = get_Block_n_cfgpreds(block);
225 /* no cfgpred -> no value active */
226 if (n_cfgpreds == 0) {
227 block_info->start_state = NULL;
231 /* for 1 pred only: simply take the the end-state of the pred */
232 if (n_cfgpreds == 1) {
233 ir_node *pred_block = get_Block_cfgpred_block(block, 0);
234 block_info_t *pred_info;
236 /* process pred block */
237 belady(env, pred_block);
239 pred_info = get_block_info(pred_block);
241 DBG((dbg, LEVEL_2, "Taking end state from %+F: %+F\n", pred_block, pred_info->end_state));
242 block_info->start_state = pred_info->end_state;
246 /* Collect all values living at start of block */
247 DBG((dbg, LEVEL_2, "Living at start of %+F:\n", block));
248 first = sched_first(block);
249 loop = get_irn_loop(block);
251 best_time = USES_INFINITY;
252 outer_loop_allowed = 1;
254 /* check all Phis first */
255 sched_foreach(block, node) {
258 if (arch_get_irn_register(node) != env->reg)
261 DBG((dbg, LEVEL_2, "\t...checking %+F\n", node));
262 next_use = be_get_next_use(env->uses, first, 0, node, 0);
264 if (USES_IS_INFINITE(next_use.time)) {
265 DBG((dbg, LEVEL_2, "\tnot taken (dead)\n"));
269 if (next_use.outermost_loop >= get_loop_depth(loop)) {
270 if (outer_loop_allowed || next_use.time < best_time) {
271 DBG((dbg, LEVEL_2, "\ttaken (%u, loop %d)\n", next_use.time,
272 next_use.outermost_loop));
274 if (best_starter != NULL) {
275 /* spill the phi as it is not used */
276 spill_phi(env, best_starter);
279 best_time = next_use.time;
280 outer_loop_allowed = 0;
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));
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;
295 if (best_starter != node) {
296 /* spill the phi as it is not used */
297 spill_phi(env, best_starter);
301 /* check all Live-Ins */
302 be_lv_foreach(env->lv, block, be_lv_state_in, i) {
303 node = be_lv_get_irn(env->lv, block, i);
305 if (!mode_is_data(get_irn_mode(node)))
308 if (arch_get_irn_register(node) != env->reg)
311 DBG((dbg, LEVEL_2, "\t...checking %+F\n", node));
312 next_use = be_get_next_use(env->uses, first, 0, node, 0);
314 if (USES_IS_INFINITE(next_use.time)) {
315 DBG((dbg, LEVEL_2, "\tnot taken (dead)\n"));
319 if (next_use.outermost_loop >= get_loop_depth(loop)) {
320 if (outer_loop_allowed || next_use.time < best_time) {
321 DBG((dbg, LEVEL_2, "\ttaken (%u, loop %d)\n", next_use.time,
322 next_use.outermost_loop));
324 if (best_starter != NULL && is_Phi(best_starter)) {
325 /* spill the phi as it is not used */
326 spill_phi(env, best_starter);
329 best_time = next_use.time;
330 outer_loop_allowed = 0;
333 if (outer_loop_allowed && next_use.time < best_time) {
334 DBG((dbg, LEVEL_2, "\ttaken (%u, loop %d)\n", next_use.time,
335 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;
346 block_info->start_state = best_starter;
352 * For the given block @p block, decide for each values
353 * whether it is used from a register or is reloaded
356 static 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 const arch_register_t *reg;
393 ir_node *in = get_irn_n(node, i);
395 if (!mode_is_data(get_irn_mode(in)))
398 reg = arch_get_irn_register(in);
399 if (reg == env->reg) {
400 assert(need_val == NULL);
402 DBG((dbg, LEVEL_3, "\t... need state %+F\n", need_val));
405 /* create a reload to match state if necessary */
406 if (need_val != NULL && need_val != current_state) {
407 DBG((dbg, LEVEL_3, "\t... reloading %+F\n", need_val));
408 create_reload(env, need_val, node, current_state);
409 current_state = need_val;
412 DBG((dbg, LEVEL_3, " ...%+F\n", node));
414 /* record state changes by the node */
415 if (get_irn_mode(node) == mode_T) {
416 const ir_edge_t *edge;
418 foreach_out_edge(node, edge) {
419 const arch_register_t *reg;
420 ir_node *proj = get_edge_src_irn(edge);
422 if (!mode_is_data(get_irn_mode(proj)))
425 reg = arch_get_irn_register(proj);
426 if (reg == env->reg) {
427 current_state = proj;
428 DBG((dbg, LEVEL_3, "\t... current_state <- %+F\n", current_state));
432 if (mode_is_data(get_irn_mode(node))) {
433 const arch_register_t *reg = arch_get_irn_register(node);
434 if (reg == env->reg) {
435 current_state = node;
436 DBG((dbg, LEVEL_3, "\t... current_state <- %+F\n", current_state));
442 /* Remember end-workset for this block */
443 block_info->end_state = current_state;
444 DBG((dbg, LEVEL_3, "End value for %+F: %+F\n", block, current_state));
447 static void belady_walker(ir_node *block, void *data)
449 belady((minibelady_env_t*) data, block);
452 static ir_node *get_end_of_block_insertion_point(ir_node *block)
454 ir_node *last = sched_last(block);
456 /* skip Projs and Keep-alikes behind the jump... */
457 while (is_Proj(last) || be_is_Keep(last)) {
458 last = sched_prev(last);
461 if (!is_cfop(last)) {
462 last = sched_next(last);
463 /* last node must be a cfop, only exception is the start block */
464 assert(last == get_irg_start_block(get_irn_irg(block)));
471 * We must adapt the live-outs to the live-ins at each block-border.
473 static void fix_block_borders(ir_node *block, void *data)
475 minibelady_env_t *env = data;
476 ir_graph *irg = get_irn_irg(block);
477 ir_node *startblock = get_irg_start_block(irg);
480 block_info_t *block_info;
482 if (block == startblock)
485 DBG((dbg, LEVEL_3, "\n"));
487 block_info = get_block_info(block);
489 DBG((dbg, LEVEL_3, "Fixing %+F (needs %+F)\n", block,
490 block_info->start_state));
492 /* process all pred blocks */
493 arity = get_irn_arity(block);
494 for (i = 0; i < arity; ++i) {
495 ir_node *pred = get_Block_cfgpred_block(block, i);
496 block_info_t *pred_info = get_block_info(pred);
497 ir_node *need_state = block_info->start_state;
499 if (need_state == NULL)
502 if (is_Phi(need_state) && get_nodes_block(need_state) == block) {
503 need_state = get_irn_n(need_state, i);
506 DBG((dbg, LEVEL_3, " Pred %+F (ends in %+F, we need %+F)\n", pred,
507 pred_info->end_state, need_state));
509 if (pred_info->end_state != need_state) {
510 ir_node *insert_point = get_end_of_block_insertion_point(pred);
513 DBG((dbg, LEVEL_3, " Creating reload for %+F\n", need_state));
514 create_reload(env, need_state, insert_point, pred_info->end_state);
519 void be_assure_state(ir_graph *irg, const arch_register_t *reg, void *func_env,
520 create_spill_func create_spill,
521 create_reload_func create_reload)
523 minibelady_env_t env;
525 be_lv_t *lv = be_assure_liveness(irg);
527 be_liveness_assure_sets(lv);
528 /* construct control flow loop tree */
529 if (! (get_irg_loopinfo_state(irg) & loopinfo_cf_consistent)) {
530 construct_cf_backedges(irg);
533 obstack_init(&env.obst);
535 env.func_env = func_env;
536 env.create_spill = create_spill;
537 env.create_reload = create_reload;
538 env.lv = be_get_irg_liveness(irg);
539 env.uses = be_begin_uses(irg, env.lv);
541 ir_nodemap_init(&env.spill_infos);
544 ir_reserve_resources(irg, IR_RESOURCE_IRN_VISITED | IR_RESOURCE_IRN_LINK);
545 inc_irg_visited(irg);
548 irg_block_walk_graph(irg, NULL, belady_walker, &env);
550 /* fix block end_states that don't match the next blocks start_state */
551 irg_block_walk_graph(irg, fix_block_borders, NULL, &env);
553 ir_free_resources(irg, IR_RESOURCE_IRN_VISITED | IR_RESOURCE_IRN_LINK);
555 /* reconstruct ssa-form */
557 while (info != NULL) {
558 be_ssa_construction_env_t senv;
562 be_ssa_construction_init(&senv, irg);
563 if (sched_is_scheduled(info->value))
564 be_ssa_construction_add_copy(&senv, info->value);
565 be_ssa_construction_add_copies(&senv,
566 info->reloads, ARR_LEN(info->reloads));
567 be_ssa_construction_fix_users(&senv, info->value);
570 be_ssa_construction_update_liveness_phis(&senv, lv);
572 be_liveness_update(lv, info->value);
573 len = ARR_LEN(info->reloads);
574 for (i = 0; i < len; ++i) {
575 ir_node *reload = info->reloads[i];
576 be_liveness_update(lv, reload);
580 phis = be_ssa_construction_get_new_phis(&senv);
582 /* set register requirements for phis */
584 for (i = 0; i < len; ++i) {
585 ir_node *phi = phis[i];
586 arch_set_irn_register(phi, env.reg);
588 be_ssa_construction_destroy(&senv);
593 /* some nodes might be dead now. */
594 be_remove_dead_nodes_from_schedule(irg);
596 ir_nodemap_destroy(&env.spill_infos);
597 be_end_uses(env.uses);
598 obstack_free(&env.obst, NULL);
601 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_state);
602 void be_init_state(void)
604 FIRM_DBG_REGISTER(dbg, "firm.be.state");