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"
72 #include "irphase_t.h"
74 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
76 static ir_node *search_def_end_of_block(be_ssa_construction_env_t *env,
82 bool already_processed;
84 /* Since we only consider scheduled nodes,
85 * this points to the real definition (e.g. a Proj). */
88 /* Last definition of a block. */
89 ir_node *last_definition;
93 typedef struct constr_info constr_info;
96 * @return Whether the block contains a definition.
98 static inline bool has_definition(be_ssa_construction_env_t *env, ir_node *block)
102 return irn_visited(block);
106 * @return Whether the block contains a use.
108 static inline bool has_use(be_ssa_construction_env_t *env, ir_node *block)
110 constr_info *info = phase_get_or_set_irn_data(env->phase, block);
116 * @return Whether the node is a definition.
118 static inline bool is_definition(be_ssa_construction_env_t *env, ir_node *node)
120 constr_info *info = phase_get_irn_data(env->phase, node);
122 return info && info->is_definition;
126 * @return Whether the node is a use.
128 static inline bool is_use(const be_ssa_construction_env_t *env, ir_node *node)
130 constr_info *info = phase_get_irn_data(env->phase, node);
132 return info && info->is_use;
136 * Introduces a definition at the corresponding block.
138 static void introduce_definition(be_ssa_construction_env_t *env, ir_node *def)
140 ir_node *block = get_nodes_block(def);
141 ir_phase *phase = env->phase;
142 constr_info *def_info = phase_get_or_set_irn_data(phase, def);
143 ir_node *skip = skip_Proj(def);
144 constr_info *skip_info = phase_get_or_set_irn_data(phase, skip);
145 constr_info *block_info = phase_get_or_set_irn_data(phase, block);
147 DBG((dbg, LEVEL_2, "\tintroducing definition %+F in %+F\n", def, block));
149 def_info->is_definition = true;
151 skip_info->is_definition = true;
152 skip_info->definition = def;
154 // Set the last definition if we only introduce one definition for the block
155 if (has_definition(env, block)) {
156 assert(!block_info->already_processed);
157 block_info->last_definition = NULL;
160 mark_irn_visited(block);
161 block_info->last_definition = def;
165 static void introduce_use(be_ssa_construction_env_t *env, ir_node *use)
167 ir_node *block = get_nodes_block(use);
168 ir_phase *phase = env->phase;
169 constr_info *info = phase_get_or_set_irn_data(phase, use);
170 constr_info *block_info = phase_get_or_set_irn_data(phase, block);
172 DBG((dbg, LEVEL_2, "\tintroducing use %+F in %+F\n", use, block));
175 block_info->is_use = true;
177 waitq_put(env->worklist, use);
181 * Calculates the iterated dominance frontier of a set of blocks. Marks the
182 * blocks as visited. Sets the link fields of the blocks in the dominance
183 * frontier to the block itself.
185 static void mark_iterated_dominance_frontiers(
186 const be_ssa_construction_env_t *env)
188 stat_ev_cnt_decl(blocks);
189 DBG((dbg, LEVEL_3, "Dominance Frontier:"));
191 while (!waitq_empty(env->worklist)) {
193 ir_node *block = (ir_node*)waitq_get(env->worklist);
194 ir_node **domfront = be_get_dominance_frontier(env->domfronts, block);
195 int domfront_len = ARR_LEN(domfront);
197 for (i = 0; i < domfront_len; ++i) {
198 ir_node *y = domfront[i];
199 if (Block_block_visited(y))
202 if (!irn_visited(y)) {
203 set_irn_link(y, NULL);
204 waitq_put(env->worklist, y);
207 DBG((dbg, LEVEL_3, " %+F", y));
208 mark_Block_block_visited(y);
209 stat_ev_cnt_inc(blocks);
212 stat_ev_tim_pop("bessaconstr_idf_time");
213 stat_ev_cnt_done(blocks, "bessaconstr_idf_blocks");
214 DBG((dbg, LEVEL_3, "\n"));
218 * Inserts a new phi at the given block.
220 * The constructed phi has only Dummy operands,
221 * but can be used as definition for other nodes.
223 * @see fix_phi_arguments
225 static ir_node *insert_dummy_phi(be_ssa_construction_env_t *env, ir_node *block)
227 int i, n_preds = get_Block_n_cfgpreds(block);
228 ir_graph *irg = get_Block_irg(block);
229 ir_node **ins = ALLOCAN(ir_node*, n_preds);
233 DBG((dbg, LEVEL_3, "\t...create phi at block %+F\n", block));
237 dummy = new_r_Dummy(irg, env->mode);
238 for (i = 0; i < n_preds; ++i) {
241 phi = be_new_Phi(block, n_preds, ins, env->mode, env->phi_cls);
242 sched_add_after(block, phi);
243 if (env->new_phis != NULL) {
244 ARR_APP1(ir_node*, env->new_phis, phi);
247 DBG((dbg, LEVEL_2, "\tcreating phi %+F in %+F\n", phi, block));
248 introduce_definition(env, phi);
250 waitq_put(env->worklist, phi);
256 * @return Last definition of the immediate dominator.
258 static ir_node *get_def_at_idom(be_ssa_construction_env_t *env, ir_node *block)
260 ir_node *dom = get_Block_idom(block);
262 DBG((dbg, LEVEL_3, "\t...continue at idom %+F\n", dom));
263 return search_def_end_of_block(env, dom);
267 * Fixes all operands of a use.
269 * If an operand of the use is a (original) definition,
270 * it will be replaced by the given definition.
272 static void set_operands(be_ssa_construction_env_t *env, ir_node *use, ir_node *def)
274 constr_info *info = phase_get_irn_data(env->phase, use);
275 int arity = get_irn_arity(use);
278 for (i = 0; i < arity; ++i) {
279 ir_node *op = get_irn_n(use, i);
281 if (is_definition(env, op)) {
282 DBG((dbg, LEVEL_1, "\t...%+F(%d) -> %+F\n", use, i, def));
283 set_irn_n(use, i, def);
287 info->already_processed = true;
291 * Fixes all uses of the given block.
293 static void process_block(be_ssa_construction_env_t *env, ir_node *block)
297 constr_info *block_info = phase_get_or_set_irn_data(env->phase, block);
299 assert(has_definition(env, block));
300 assert(!block_info->already_processed && "Block already processed");
302 DBG((dbg, LEVEL_3, "\tprocessing block %+F\n", block));
304 sched_foreach(block, node) {
305 if (is_use(env, node) && !is_Phi(node)) {
306 DBG((dbg, LEVEL_3, "\t...found use %+F\n", node));
309 /* Create a phi if the block is in the dominance frontier. */
310 if (Block_block_visited(block)) {
311 def = insert_dummy_phi(env, block);
314 def = get_def_at_idom(env, block);
318 set_operands(env, node, def);
321 if (is_definition(env, node)) {
322 constr_info *info = phase_get_irn_data(env->phase, node);
323 def = info->definition;
324 DBG((dbg, LEVEL_3, "\t...found definition %+F\n", def));
328 block_info->already_processed = true;
329 block_info->last_definition = def;
333 * @return Last definition of the given block.
335 static ir_node *search_def_end_of_block(be_ssa_construction_env_t *env,
338 constr_info *block_info = phase_get_or_set_irn_data(env->phase, block);
339 ir_node *last_definition = block_info->last_definition;
341 if (last_definition != NULL)
342 return last_definition;
344 if (has_definition(env, block)) {
345 if (has_use(env, block)) {
346 if (!block_info->already_processed) {
347 process_block(env, block);
353 /* Search the last definition of the block. */
354 sched_foreach_reverse(block, def) {
355 if (is_definition(env, def)) {
356 constr_info *info = phase_get_irn_data(env->phase, def);
357 def = info->definition;
358 DBG((dbg, LEVEL_3, "\t...found definition %+F\n", def));
364 assert(def && "No definition found");
366 block_info->last_definition = def;
369 return block_info->last_definition;
370 } else if (Block_block_visited(block)) {
371 ir_node *phi = insert_dummy_phi(env, block);
373 block_info->last_definition = phi;
377 ir_node *def = get_def_at_idom(env, block);
379 block_info->last_definition = def;
386 * Fixes all operands of the given use.
388 static void search_def_at_block(be_ssa_construction_env_t *env, ir_node *use)
390 ir_node *block = get_nodes_block(use);
391 constr_info *block_info = phase_get_or_set_irn_data(env->phase, block);
393 if (block_info->already_processed)
396 if (has_definition(env, block)) {
397 process_block(env, block);
398 } else if (Block_block_visited(block)) {
399 ir_node *phi = insert_dummy_phi(env, block);
401 set_operands(env, use, phi);
403 ir_node *def = get_def_at_idom(env, block);
405 set_operands(env, use, def);
409 static void *init_constr_info(ir_phase *phase, const ir_node *node)
411 constr_info *info = phase_alloc(phase, sizeof(constr_info));
414 info->is_definition = false;
415 info->is_use = false;
416 info->already_processed = false;
417 info->definition = NULL;
422 void be_ssa_construction_init(be_ssa_construction_env_t *env, ir_graph *irg)
424 ir_node *sb = get_irg_start_block(irg);
425 int n_blocks = get_Block_dom_max_subtree_pre_num(sb);
427 stat_ev_ctx_push_fobj("bessaconstr", irg);
431 stat_ev_dbl("bessaconstr_n_blocks", n_blocks);
433 memset(env, 0, sizeof(env[0]));
434 be_assure_dom_front(irg);
437 env->domfronts = be_get_irg_dom_front(irg);
438 env->new_phis = NEW_ARR_F(ir_node*, 0);
439 env->worklist = new_waitq();
440 env->phase = new_phase(irg, init_constr_info);
442 ir_reserve_resources(irg, IR_RESOURCE_IRN_VISITED
443 | IR_RESOURCE_BLOCK_VISITED | IR_RESOURCE_IRN_LINK);
445 /* we use the visited flag to indicate blocks in the dominance frontier
446 * and blocks that already have the relevant value at the end calculated */
447 inc_irg_visited(irg);
448 /* We use the block visited flag to indicate blocks in the dominance
449 * frontier of some values (and this potentially needing phis) */
450 inc_irg_block_visited(irg);
453 void be_ssa_construction_destroy(be_ssa_construction_env_t *env)
455 stat_ev_int("bessaconstr_phis", ARR_LEN(env->new_phis));
456 phase_free(env->phase);
457 del_waitq(env->worklist);
458 DEL_ARR_F(env->new_phis);
460 ir_free_resources(env->irg, IR_RESOURCE_IRN_VISITED
461 | IR_RESOURCE_BLOCK_VISITED | IR_RESOURCE_IRN_LINK);
463 stat_ev_tim_pop("bessaconstr_total_time");
464 stat_ev_ctx_pop("bessaconstr");
467 void be_ssa_construction_add_copy(be_ssa_construction_env_t *env,
472 assert(env->iterated_domfront_calculated == 0);
474 if (env->mode == NULL) {
475 env->mode = get_irn_mode(copy);
476 env->phi_cls = arch_get_irn_reg_class_out(copy);
478 assert(env->mode == get_irn_mode(copy));
481 block = get_nodes_block(copy);
483 if (!has_definition(env, block)) {
484 waitq_put(env->worklist, block);
486 introduce_definition(env, copy);
489 void be_ssa_construction_add_copies(be_ssa_construction_env_t *env,
490 ir_node **copies, size_t copies_len)
494 assert(env->iterated_domfront_calculated == 0);
496 if (env->mode == NULL) {
497 env->mode = get_irn_mode(copies[0]);
498 env->phi_cls = arch_get_irn_reg_class_out(copies[0]);
501 for (i = 0; i < copies_len; ++i) {
502 ir_node *copy = copies[i];
503 ir_node *block = get_nodes_block(copy);
505 assert(env->mode == get_irn_mode(copy));
506 if (!has_definition(env, block)) {
507 waitq_put(env->worklist, block);
509 introduce_definition(env, copy);
513 void be_ssa_construction_set_ignore_uses(be_ssa_construction_env_t *env,
514 const ir_nodeset_t *ignore_uses)
516 env->ignore_uses = ignore_uses;
519 ir_node **be_ssa_construction_get_new_phis(be_ssa_construction_env_t *env)
521 return env->new_phis;
525 * Fixes all arguments of a newly constructed phi.
527 * @see insert_dummy_phi
529 static void fix_phi_arguments(be_ssa_construction_env_t *env, ir_node *phi)
531 constr_info *info = phase_get_irn_data(env->phase, phi);
532 ir_node *block = get_nodes_block(phi);
534 int n_preds = get_Block_n_cfgpreds(block);
536 DBG((dbg, LEVEL_3, "\tfixing phi arguments %+F\n", phi));
538 for (i = 0; i < n_preds; ++i) {
539 ir_node *op = get_irn_n(phi, i);
541 if (is_definition(env, op) || is_Dummy(op)) {
542 ir_node *pred_block = get_Block_cfgpred_block(block, i);
543 ir_node *pred_def = search_def_end_of_block(env, pred_block);
545 DBG((dbg, LEVEL_1, "\t...%+F(%d) -> %+F\n", phi, i, pred_def));
546 set_irn_n(phi, i, pred_def);
550 info->already_processed = true;
553 void be_ssa_construction_fix_users_array(be_ssa_construction_env_t *env,
554 ir_node **nodes, size_t nodes_len)
557 stat_ev_cnt_decl(uses);
559 be_timer_push(T_SSA_CONSTR);
561 if (!env->iterated_domfront_calculated) {
562 mark_iterated_dominance_frontiers(env);
563 env->iterated_domfront_calculated = 1;
566 DBG((dbg, LEVEL_1, "\tfixing users array\n"));
568 assert(waitq_empty(env->worklist));
572 for (i = 0; i < nodes_len; ++i) {
573 const ir_edge_t *edge, *next;
574 ir_node *value = nodes[i];
575 DBG((dbg, LEVEL_3, "\tfixing users of %+F\n", value));
576 introduce_definition(env, value);
578 foreach_out_edge_safe(value, edge, next) {
579 ir_node *use = get_edge_src_irn(edge);
581 if (env->ignore_uses != NULL &&
582 ir_nodeset_contains(env->ignore_uses, use))
585 if (is_Anchor(use) || is_End(use))
588 introduce_use(env, use);
592 assert(!waitq_empty(env->worklist));
594 while (!waitq_empty(env->worklist)) {
595 ir_node *use = (ir_node *)waitq_get(env->worklist);
596 constr_info *info = phase_get_irn_data(env->phase, use);
598 if (info->already_processed)
602 fix_phi_arguments(env, use);
605 DBG((dbg, LEVEL_3, "\tsearching def for %+F at %+F\n", use, get_nodes_block(use)));
606 search_def_at_block(env, use);
609 stat_ev_cnt_inc(uses);
612 be_timer_pop(T_SSA_CONSTR);
614 stat_ev_tim_pop("bessaconstr_fix_time");
615 stat_ev_cnt_done(uses, "bessaconstr_uses");
618 void be_ssa_construction_fix_users(be_ssa_construction_env_t *env, ir_node *value)
620 be_ssa_construction_fix_users_array(env, &value, 1);
624 void be_ssa_construction_update_liveness_phis(be_ssa_construction_env_t *env,
629 be_timer_push(T_SSA_CONSTR);
631 n = ARR_LEN(env->new_phis);
632 for (i = 0; i < n; ++i) {
633 ir_node *phi = env->new_phis[i];
634 be_liveness_introduce(lv, phi);
637 be_timer_pop(T_SSA_CONSTR);
640 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_ssaconstr);
641 void be_init_ssaconstr(void)
643 FIRM_DBG_REGISTER(dbg, "firm.be.ssaconstr");