2 * Copyright (C) 1995-2011 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
32 #include "irgraph_t.h"
36 #include "iredges_t.h"
39 #include "irnodeset.h"
40 #include "irnodehashmap.h"
51 #include "bespillutil.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_nodehashmap_t spill_infos;
73 be_uses_t *uses; /**< env for the next-use magic */
76 typedef struct block_info_t {
81 static inline block_info_t *new_block_info(struct obstack *obst, ir_node *block)
83 block_info_t *res = OALLOCZ(obst, block_info_t);
85 assert(is_Block(block));
86 set_irn_link(block, res);
87 mark_irn_visited(block);
92 static inline block_info_t *get_block_info(ir_node *block)
94 assert(irn_visited(block));
95 return (block_info_t*) get_irn_link(block);
98 static inline spill_info_t *create_spill_info(minibelady_env_t *env, ir_node *state)
100 spill_info_t *spill_info = OALLOCZ(&env->obst, spill_info_t);
101 spill_info->value = state;
102 spill_info->reloads = NEW_ARR_F(ir_node*, 0);
104 ir_nodehashmap_insert(&env->spill_infos, state, spill_info);
105 //ir_fprintf(stderr, "Insert %+F -> %p\n", state, spill_info);
107 spill_info->next = env->spills;
108 env->spills = spill_info;
113 static inline spill_info_t *get_spill_info(minibelady_env_t *env, const ir_node *node)
115 spill_info_t *spill_info = ir_nodehashmap_get(spill_info_t, &env->spill_infos, node);
116 //ir_fprintf(stderr, "Get %+F -> %p\n", node, spill_info);
120 static spill_info_t *create_spill(minibelady_env_t *env, ir_node *state, int force)
122 spill_info_t *spill_info;
126 spill_info = get_spill_info(env, state);
127 if (spill_info == NULL) {
128 spill_info = create_spill_info(env, state);
129 } else if (spill_info->spill != NULL) {
133 if (sched_is_scheduled(state)) {
137 next = sched_next(after);
138 } while (is_Phi(next) || be_is_Keep(next));
142 spill_info->spill = env->create_spill(env->func_env, state, force, after);
147 static void create_reload(minibelady_env_t *env, ir_node *state,
148 ir_node *before, ir_node *last_state)
150 spill_info_t *spill_info = create_spill(env, state, 0);
151 ir_node *spill = spill_info->spill;
154 reload = env->create_reload(env->func_env, state, spill, before,
156 ARR_APP1(ir_node*, spill_info->reloads, reload);
159 static 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 arity = get_irn_arity(phi);
164 ir_node **phi_in = ALLOCAN(ir_node*, arity);
165 ir_node *dummy = new_r_Dummy(irg, mode_M);
166 ir_node *spill_to_kill = NULL;
167 spill_info_t *spill_info;
170 /* does a spill exist for the phis value? */
171 spill_info = get_spill_info(env, phi);
172 if (spill_info != NULL) {
173 spill_to_kill = spill_info->spill;
175 spill_info = create_spill_info(env, phi);
178 /* create a new phi-M with bad preds */
179 for (i = 0; i < arity; ++i) {
183 DBG((dbg, LEVEL_2, "\tcreate Phi-M for %+F\n", phi));
186 spill_info->spill = be_new_Phi(block, arity, phi_in, mode_M,
187 arch_no_register_req);
188 sched_add_after(block, spill_info->spill);
190 if (spill_to_kill != NULL) {
191 exchange(spill_to_kill, spill_info->spill);
192 sched_remove(spill_to_kill);
195 /* create spills for the phi values */
196 for (i = 0; i < arity; ++i) {
197 ir_node *in = get_irn_n(phi, i);
198 spill_info_t *pred_spill = create_spill(env, in, 1);
199 set_irn_n(spill_info->spill, i, pred_spill->spill);
203 static void belady(minibelady_env_t *env, ir_node *block);
206 * Collects all values live-in at block @p block and all phi results in this
208 * Then it adds the best values (at most n_regs) to the blocks start_workset.
209 * The phis among the remaining values get spilled: Introduce pseudo-copies of
210 * their args to break interference and make it possible to spill them to the
213 static block_info_t *compute_block_start_state(minibelady_env_t *env, ir_node *block)
215 block_info_t *block_info;
216 be_next_use_t next_use;
218 ir_node *best_starter, *first;
221 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(node) != env->reg)
263 DBG((dbg, LEVEL_2, "\t...checking %+F\n", node));
264 next_use = be_get_next_use(env->uses, first, 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_cls(env->lv, block, be_lv_state_in, env->reg->reg_class, node) {
305 if (arch_get_irn_register(node) != env->reg)
308 DBG((dbg, LEVEL_2, "\t...checking %+F\n", node));
309 next_use = be_get_next_use(env->uses, first, node, 0);
311 if (USES_IS_INFINITE(next_use.time)) {
312 DBG((dbg, LEVEL_2, "\tnot taken (dead)\n"));
316 if (next_use.outermost_loop >= get_loop_depth(loop)) {
317 if (outer_loop_allowed || next_use.time < best_time) {
318 DBG((dbg, LEVEL_2, "\ttaken (%u, loop %d)\n", next_use.time,
319 next_use.outermost_loop));
321 if (best_starter != NULL && is_Phi(best_starter)) {
322 /* spill the phi as it is not used */
323 spill_phi(env, best_starter);
326 best_time = next_use.time;
327 outer_loop_allowed = 0;
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));
333 if (best_starter != NULL && is_Phi(best_starter)) {
334 /* spill the phi as it is not used */
335 spill_phi(env, best_starter);
338 best_time = next_use.time;
343 block_info->start_state = best_starter;
349 * For the given block @p block, decide for each values
350 * whether it is used from a register or is reloaded
353 static void belady(minibelady_env_t *env, ir_node *block)
355 ir_node *current_state;
356 block_info_t *block_info;
358 /* Don't do a block twice */
359 if (irn_visited(block))
362 /* compute value to start with */
363 block_info = compute_block_start_state(env, block);
365 /* get the starting workset for this block */
366 DBG((dbg, LEVEL_3, "\n"));
367 DBG((dbg, LEVEL_3, "Decide for %+F\n", block));
369 current_state = block_info->start_state;
370 DBG((dbg, LEVEL_3, "Start value: %+F\n", current_state));
372 /* process the block from start to end */
373 DBG((dbg, LEVEL_3, "Processing...\n"));
375 sched_foreach(block, node) {
377 ir_node *need_val = NULL;
379 /* projs are handled with the tuple value.
380 * Phis are no real instr (see insert_starters()) */
381 if (is_Proj(node) || is_Phi(node)) {
385 /* check which state is desired for the node */
386 arity = get_irn_arity(node);
387 for (i = 0; i < arity; ++i) {
388 const arch_register_t *reg;
389 ir_node *in = get_irn_n(node, i);
391 if (!mode_is_data(get_irn_mode(in)))
394 reg = arch_get_irn_register(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 ir_node *before = node;
404 DBG((dbg, LEVEL_3, "\t... reloading %+F\n", need_val));
405 create_reload(env, need_val, before, current_state);
406 current_state = need_val;
409 DBG((dbg, LEVEL_3, " ...%+F\n", node));
411 /* record state changes by the node */
412 be_foreach_value(node, value,
413 if (!mode_is_data(get_irn_mode(value)))
415 arch_register_t const *const reg = arch_get_irn_register(value);
418 current_state = value;
419 DBG((dbg, LEVEL_3, "\t... current_state <- %+F\n", current_state));
423 /* Remember end-workset for this block */
424 block_info->end_state = current_state;
425 DBG((dbg, LEVEL_3, "End value for %+F: %+F\n", block, current_state));
428 static void belady_walker(ir_node *block, void *data)
430 belady((minibelady_env_t*) data, block);
434 * We must adapt the live-outs to the live-ins at each block-border.
436 static void fix_block_borders(ir_node *block, void *data)
438 minibelady_env_t *env = (minibelady_env_t*)data;
439 ir_graph *irg = get_irn_irg(block);
440 ir_node *startblock = get_irg_start_block(irg);
443 block_info_t *block_info;
445 if (block == startblock)
448 DBG((dbg, LEVEL_3, "\n"));
450 block_info = get_block_info(block);
452 DBG((dbg, LEVEL_3, "Fixing %+F (needs %+F)\n", block,
453 block_info->start_state));
455 /* process all pred blocks */
456 arity = get_irn_arity(block);
457 for (i = 0; i < arity; ++i) {
458 ir_node *pred = get_Block_cfgpred_block(block, i);
459 block_info_t *pred_info = get_block_info(pred);
460 ir_node *need_state = block_info->start_state;
462 if (need_state == NULL)
465 if (is_Phi(need_state) && get_nodes_block(need_state) == block) {
466 need_state = get_irn_n(need_state, i);
469 DBG((dbg, LEVEL_3, " Pred %+F (ends in %+F, we need %+F)\n", pred,
470 pred_info->end_state, need_state));
472 if (pred_info->end_state != need_state) {
473 DBG((dbg, LEVEL_3, " Creating reload for %+F\n", need_state));
474 ir_node *const insert_point = be_get_end_of_block_insertion_point(pred);
475 create_reload(env, need_state, insert_point, pred_info->end_state);
480 void be_assure_state(ir_graph *irg, const arch_register_t *reg, void *func_env,
481 create_spill_func create_spill,
482 create_reload_func create_reload)
484 minibelady_env_t env;
486 be_lv_t *lv = be_get_irg_liveness(irg);
488 be_assure_live_sets(irg);
489 assure_loopinfo(irg);
491 obstack_init(&env.obst);
493 env.func_env = func_env;
494 env.create_spill = create_spill;
495 env.create_reload = create_reload;
496 env.lv = be_get_irg_liveness(irg);
497 env.uses = be_begin_uses(irg, env.lv);
499 ir_nodehashmap_init(&env.spill_infos);
502 ir_reserve_resources(irg, IR_RESOURCE_IRN_VISITED | IR_RESOURCE_IRN_LINK);
503 inc_irg_visited(irg);
506 irg_block_walk_graph(irg, NULL, belady_walker, &env);
508 /* fix block end_states that don't match the next blocks start_state */
509 irg_block_walk_graph(irg, fix_block_borders, NULL, &env);
511 ir_free_resources(irg, IR_RESOURCE_IRN_VISITED | IR_RESOURCE_IRN_LINK);
513 /* reconstruct ssa-form */
515 while (info != NULL) {
516 be_ssa_construction_env_t senv;
520 be_ssa_construction_init(&senv, irg);
521 if (sched_is_scheduled(info->value))
522 be_ssa_construction_add_copy(&senv, info->value);
523 be_ssa_construction_add_copies(&senv,
524 info->reloads, ARR_LEN(info->reloads));
525 be_ssa_construction_fix_users(&senv, info->value);
528 be_ssa_construction_update_liveness_phis(&senv, lv);
530 be_liveness_update(lv, info->value);
531 len = ARR_LEN(info->reloads);
532 for (i = 0; i < len; ++i) {
533 ir_node *reload = info->reloads[i];
534 be_liveness_update(lv, reload);
538 phis = be_ssa_construction_get_new_phis(&senv);
540 /* set register requirements for phis */
542 for (i = 0; i < len; ++i) {
543 ir_node *phi = phis[i];
544 arch_set_irn_register(phi, env.reg);
546 be_ssa_construction_destroy(&senv);
551 /* some nodes might be dead now. */
552 be_remove_dead_nodes_from_schedule(irg);
554 ir_nodehashmap_destroy(&env.spill_infos);
555 be_end_uses(env.uses);
556 obstack_free(&env.obst, NULL);
559 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_state)
560 void be_init_state(void)
562 FIRM_DBG_REGISTER(dbg, "firm.be.state");