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 SSA construction for a set of nodes
23 * @author Sebastian Hack, Daniel Grund, Matthias Braun, Christian Wuerdig,
28 * The problem: Given a value and a set of "copies" that are known to
29 * represent the same abstract value, rewire all usages of the original value
30 * to their closest copy while introducing phis as necessary.
32 * Algorithm: Mark all blocks in the iterated dominance frontiers of the value
33 * and its copies. Link the copies ordered by dominance to the blocks. Then
34 * we search for each use all definitions in the current block, if none is
35 * found, then we search one in the immediate dominator. If we are in a block
36 * of the dominance frontier, create a phi and do the same search for all
39 * A copy in this context means, that you want to introduce several new
40 * abstract values (in Firm: nodes) for which you know, that they
41 * represent the same concrete value. This is the case if you
47 * This function reroutes all uses of the original value to the copies in the
48 * corresponding dominance subtrees and creates Phi functions where necessary.
52 /* statev in this file is extensive, so only enable if needed */
53 #define DISABLE_STATEV
55 #include "bessaconstr.h"
58 #include "beintlive_t.h"
68 #include "irnodeset.h"
71 #include "iredges_t.h"
73 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
75 static ir_node *search_def_end_of_block(be_ssa_construction_env_t *env,
79 bool is_definition : 1;
81 bool already_processed : 1;
83 /* Since we only consider scheduled nodes,
84 * this points to the real definition (e.g. a Proj). */
87 /* Last definition of a block. */
88 ir_node *last_definition;
92 typedef struct constr_info constr_info;
95 * @return Whether the block contains a definition.
97 static bool has_definition(const ir_node *block)
99 return irn_visited(block);
102 static constr_info *get_or_set_info(be_ssa_construction_env_t *env,
105 constr_info *info = ir_nodemap_get(&env->infos, node);
107 info = OALLOCZ(&env->obst, constr_info);
108 ir_nodemap_insert(&env->infos, node, info);
113 static constr_info *get_info(const be_ssa_construction_env_t *env,
116 return (constr_info*)ir_nodemap_get(&env->infos, node);
120 * @return Whether the block contains a use.
122 static inline bool has_use(be_ssa_construction_env_t *env, ir_node *block)
124 constr_info *info = get_or_set_info(env, block);
129 * @return Whether the node is a definition.
131 static bool is_definition(be_ssa_construction_env_t *env, ir_node *node)
133 constr_info *info = get_info(env, node);
134 return info != NULL && info->is_definition;
138 * @return Whether the node is a use.
140 static bool is_use(const be_ssa_construction_env_t *env, ir_node *node)
142 constr_info *info = get_info(env, node);
143 return info != NULL && info->is_use;
147 * Introduces a definition at the corresponding block.
149 static void introduce_definition(be_ssa_construction_env_t *env, ir_node *def)
151 ir_node *block = get_nodes_block(def);
152 constr_info *def_info = get_or_set_info(env, def);
153 ir_node *skip = skip_Proj(def);
154 constr_info *skip_info = get_or_set_info(env, skip);
155 constr_info *block_info = get_or_set_info(env, block);
157 DBG((dbg, LEVEL_2, "\tintroducing definition %+F in %+F\n", def, block));
159 def_info->is_definition = true;
161 skip_info->is_definition = true;
162 skip_info->definition = def;
164 // Set the last definition if we only introduce one definition for the block
165 if (has_definition(block)) {
166 assert(!block_info->already_processed);
167 block_info->last_definition = NULL;
169 mark_irn_visited(block);
170 block_info->last_definition = def;
174 static void introduce_use(be_ssa_construction_env_t *env, ir_node *use)
176 ir_node *block = get_nodes_block(use);
177 constr_info *info = get_or_set_info(env, use);
178 constr_info *block_info = get_or_set_info(env, block);
180 DBG((dbg, LEVEL_2, "\tintroducing use %+F in %+F\n", use, block));
183 block_info->is_use = true;
185 waitq_put(env->worklist, use);
189 * Calculates the iterated dominance frontier of a set of blocks. Marks the
190 * blocks as visited. Sets the link fields of the blocks in the dominance
191 * frontier to the block itself.
193 static void mark_iterated_dominance_frontiers(
194 const be_ssa_construction_env_t *env)
196 stat_ev_cnt_decl(blocks);
197 DBG((dbg, LEVEL_3, "Dominance Frontier:"));
199 while (!waitq_empty(env->worklist)) {
201 ir_node *block = (ir_node*)waitq_get(env->worklist);
202 ir_node **domfront = be_get_dominance_frontier(env->domfronts, block);
203 int domfront_len = ARR_LEN(domfront);
205 for (i = 0; i < domfront_len; ++i) {
206 ir_node *y = domfront[i];
207 if (Block_block_visited(y))
210 if (!irn_visited(y)) {
211 set_irn_link(y, NULL);
212 waitq_put(env->worklist, y);
215 DBG((dbg, LEVEL_3, " %+F", y));
216 mark_Block_block_visited(y);
217 stat_ev_cnt_inc(blocks);
220 stat_ev_tim_pop("bessaconstr_idf_time");
221 stat_ev_cnt_done(blocks, "bessaconstr_idf_blocks");
222 DBG((dbg, LEVEL_3, "\n"));
226 * Inserts a new phi at the given block.
228 * The constructed phi has only Dummy operands,
229 * but can be used as definition for other nodes.
231 * @see fix_phi_arguments
233 static ir_node *insert_dummy_phi(be_ssa_construction_env_t *env, ir_node *block)
235 int i, n_preds = get_Block_n_cfgpreds(block);
236 ir_graph *irg = get_Block_irg(block);
237 ir_node **ins = ALLOCAN(ir_node*, n_preds);
241 DBG((dbg, LEVEL_3, "\t...create phi at block %+F\n", block));
245 dummy = new_r_Dummy(irg, env->mode);
246 for (i = 0; i < n_preds; ++i) {
249 phi = be_new_Phi(block, n_preds, ins, env->mode, env->phi_cls);
250 sched_add_after(block, phi);
251 ARR_APP1(ir_node*, env->new_phis, phi);
253 DBG((dbg, LEVEL_2, "\tcreating phi %+F in %+F\n", phi, block));
254 introduce_definition(env, phi);
256 waitq_put(env->worklist, phi);
262 * @return Last definition of the immediate dominator.
264 static ir_node *get_def_at_idom(be_ssa_construction_env_t *env, ir_node *block)
266 ir_node *dom = get_Block_idom(block);
268 DBG((dbg, LEVEL_3, "\t...continue at idom %+F\n", dom));
269 return search_def_end_of_block(env, dom);
273 * Fixes all operands of a use.
275 * If an operand of the use is a (original) definition,
276 * it will be replaced by the given definition.
278 static void set_operands(be_ssa_construction_env_t *env, ir_node *use, ir_node *def)
280 constr_info *info = get_info(env, use);
281 int arity = get_irn_arity(use);
284 for (i = 0; i < arity; ++i) {
285 ir_node *op = get_irn_n(use, i);
287 if (is_definition(env, op)) {
288 DBG((dbg, LEVEL_1, "\t...%+F(%d) -> %+F\n", use, i, def));
289 set_irn_n(use, i, def);
293 info->already_processed = true;
297 * Fixes all uses of the given block.
299 static void process_block(be_ssa_construction_env_t *env, ir_node *block)
303 constr_info *block_info = get_or_set_info(env, block);
305 assert(has_definition(block));
306 assert(!block_info->already_processed && "Block already processed");
308 DBG((dbg, LEVEL_3, "\tprocessing block %+F\n", block));
310 sched_foreach(block, node) {
311 if (is_use(env, node) && !is_Phi(node)) {
312 DBG((dbg, LEVEL_3, "\t...found use %+F\n", node));
315 /* Create a phi if the block is in the dominance frontier. */
316 if (Block_block_visited(block)) {
317 def = insert_dummy_phi(env, block);
320 def = get_def_at_idom(env, block);
324 set_operands(env, node, def);
327 if (is_definition(env, node)) {
328 constr_info *info = get_info(env, node);
329 def = info->definition;
330 DBG((dbg, LEVEL_3, "\t...found definition %+F\n", def));
334 block_info->already_processed = true;
335 block_info->last_definition = def;
339 * @return Last definition of the given block.
341 static ir_node *search_def_end_of_block(be_ssa_construction_env_t *env,
344 constr_info *block_info = get_or_set_info(env, block);
345 ir_node *last_definition = block_info->last_definition;
347 if (last_definition != NULL)
348 return last_definition;
350 if (has_definition(block)) {
351 if (has_use(env, block)) {
352 if (!block_info->already_processed) {
353 process_block(env, block);
359 /* Search the last definition of the block. */
360 sched_foreach_reverse(block, def) {
361 if (is_definition(env, def)) {
362 constr_info *info = get_info(env, def);
363 def = info->definition;
364 DBG((dbg, LEVEL_3, "\t...found definition %+F\n", def));
370 assert(def && "No definition found");
372 block_info->last_definition = def;
375 return block_info->last_definition;
376 } else if (Block_block_visited(block)) {
377 ir_node *phi = insert_dummy_phi(env, block);
379 block_info->last_definition = phi;
383 ir_node *def = get_def_at_idom(env, block);
385 block_info->last_definition = def;
392 * Fixes all operands of the given use.
394 static void search_def_at_block(be_ssa_construction_env_t *env, ir_node *use)
396 ir_node *block = get_nodes_block(use);
397 constr_info *block_info = get_or_set_info(env, block);
399 if (block_info->already_processed)
402 if (has_definition(block)) {
403 process_block(env, block);
404 } else if (Block_block_visited(block)) {
405 ir_node *phi = insert_dummy_phi(env, block);
407 set_operands(env, use, phi);
409 ir_node *def = get_def_at_idom(env, block);
411 set_operands(env, use, def);
415 void be_ssa_construction_init(be_ssa_construction_env_t *env, ir_graph *irg)
417 ir_node *sb = get_irg_start_block(irg);
418 int n_blocks = get_Block_dom_max_subtree_pre_num(sb);
420 stat_ev_ctx_push_fobj("bessaconstr", irg);
424 stat_ev_dbl("bessaconstr_n_blocks", n_blocks);
426 memset(env, 0, sizeof(env[0]));
427 be_assure_dom_front(irg);
430 env->domfronts = be_get_irg_dom_front(irg);
431 env->new_phis = NEW_ARR_F(ir_node*, 0);
432 env->worklist = new_waitq();
433 ir_nodemap_init(&env->infos, irg);
434 obstack_init(&env->obst);
436 ir_reserve_resources(irg, IR_RESOURCE_IRN_VISITED
437 | IR_RESOURCE_BLOCK_VISITED | IR_RESOURCE_IRN_LINK);
439 /* we use the visited flag to indicate blocks in the dominance frontier
440 * and blocks that already have the relevant value at the end calculated */
441 inc_irg_visited(irg);
442 /* We use the block visited flag to indicate blocks in the dominance
443 * frontier of some values (and this potentially needing phis) */
444 inc_irg_block_visited(irg);
447 void be_ssa_construction_destroy(be_ssa_construction_env_t *env)
449 stat_ev_int("bessaconstr_phis", ARR_LEN(env->new_phis));
450 obstack_free(&env->obst, NULL);
451 ir_nodemap_destroy(&env->infos);
452 del_waitq(env->worklist);
453 DEL_ARR_F(env->new_phis);
455 ir_free_resources(env->irg, IR_RESOURCE_IRN_VISITED
456 | IR_RESOURCE_BLOCK_VISITED | IR_RESOURCE_IRN_LINK);
458 stat_ev_tim_pop("bessaconstr_total_time");
459 stat_ev_ctx_pop("bessaconstr");
462 void be_ssa_construction_add_copy(be_ssa_construction_env_t *env,
467 assert(env->iterated_domfront_calculated == 0);
469 if (env->mode == NULL) {
470 env->mode = get_irn_mode(copy);
471 env->phi_cls = arch_get_irn_reg_class(copy);
473 assert(env->mode == get_irn_mode(copy));
476 block = get_nodes_block(copy);
478 if (!has_definition(block)) {
479 waitq_put(env->worklist, block);
481 introduce_definition(env, copy);
484 void be_ssa_construction_add_copies(be_ssa_construction_env_t *env,
485 ir_node **copies, size_t copies_len)
489 assert(env->iterated_domfront_calculated == 0);
491 if (env->mode == NULL) {
492 env->mode = get_irn_mode(copies[0]);
493 env->phi_cls = arch_get_irn_reg_class(copies[0]);
496 for (i = 0; i < copies_len; ++i) {
497 ir_node *copy = copies[i];
498 ir_node *block = get_nodes_block(copy);
500 assert(env->mode == get_irn_mode(copy));
501 if (!has_definition(block)) {
502 waitq_put(env->worklist, block);
504 introduce_definition(env, copy);
508 void be_ssa_construction_set_ignore_uses(be_ssa_construction_env_t *env,
509 const ir_nodeset_t *ignore_uses)
511 env->ignore_uses = ignore_uses;
514 ir_node **be_ssa_construction_get_new_phis(be_ssa_construction_env_t *env)
516 return env->new_phis;
520 * Fixes all arguments of a newly constructed phi.
522 * @see insert_dummy_phi
524 static void fix_phi_arguments(be_ssa_construction_env_t *env, ir_node *phi)
526 constr_info *info = get_info(env, phi);
527 ir_node *block = get_nodes_block(phi);
529 int n_preds = get_Block_n_cfgpreds(block);
531 DBG((dbg, LEVEL_3, "\tfixing phi arguments %+F\n", phi));
533 for (i = 0; i < n_preds; ++i) {
534 ir_node *op = get_irn_n(phi, i);
536 if (is_definition(env, op) || is_Dummy(op)) {
537 ir_node *pred_block = get_Block_cfgpred_block(block, i);
538 ir_node *pred_def = search_def_end_of_block(env, pred_block);
540 DBG((dbg, LEVEL_1, "\t...%+F(%d) -> %+F\n", phi, i, pred_def));
541 set_irn_n(phi, i, pred_def);
545 info->already_processed = true;
548 void be_ssa_construction_fix_users_array(be_ssa_construction_env_t *env,
549 ir_node **nodes, size_t nodes_len)
552 stat_ev_cnt_decl(uses);
554 be_timer_push(T_SSA_CONSTR);
556 if (!env->iterated_domfront_calculated) {
557 mark_iterated_dominance_frontiers(env);
558 env->iterated_domfront_calculated = 1;
561 DBG((dbg, LEVEL_1, "\tfixing users array\n"));
563 assert(waitq_empty(env->worklist));
567 for (i = 0; i < nodes_len; ++i) {
568 const ir_edge_t *edge, *next;
569 ir_node *value = nodes[i];
570 DBG((dbg, LEVEL_3, "\tfixing users of %+F\n", value));
571 introduce_definition(env, value);
573 foreach_out_edge_safe(value, edge, next) {
574 ir_node *use = get_edge_src_irn(edge);
576 if (env->ignore_uses != NULL &&
577 ir_nodeset_contains(env->ignore_uses, use))
580 if (is_Anchor(use) || is_End(use))
583 introduce_use(env, use);
587 assert(!waitq_empty(env->worklist));
589 while (!waitq_empty(env->worklist)) {
590 ir_node *use = (ir_node *)waitq_get(env->worklist);
591 constr_info *info = get_info(env, use);
593 if (info->already_processed)
597 fix_phi_arguments(env, use);
600 DBG((dbg, LEVEL_3, "\tsearching def for %+F at %+F\n", use, get_nodes_block(use)));
601 search_def_at_block(env, use);
604 stat_ev_cnt_inc(uses);
607 be_timer_pop(T_SSA_CONSTR);
609 stat_ev_tim_pop("bessaconstr_fix_time");
610 stat_ev_cnt_done(uses, "bessaconstr_uses");
613 void be_ssa_construction_fix_users(be_ssa_construction_env_t *env, ir_node *value)
615 be_ssa_construction_fix_users_array(env, &value, 1);
619 void be_ssa_construction_update_liveness_phis(be_ssa_construction_env_t *env,
624 be_timer_push(T_SSA_CONSTR);
626 n = ARR_LEN(env->new_phis);
627 for (i = 0; i < n; ++i) {
628 ir_node *phi = env->new_phis[i];
629 be_liveness_introduce(lv, phi);
632 be_timer_pop(T_SSA_CONSTR);
635 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_ssaconstr)
636 void be_init_ssaconstr(void)
638 FIRM_DBG_REGISTER(dbg, "firm.be.ssaconstr");