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 ARR_APP1(ir_node*, env->new_phis, phi);
245 DBG((dbg, LEVEL_2, "\tcreating phi %+F in %+F\n", phi, block));
246 introduce_definition(env, phi);
248 waitq_put(env->worklist, phi);
254 * @return Last definition of the immediate dominator.
256 static ir_node *get_def_at_idom(be_ssa_construction_env_t *env, ir_node *block)
258 ir_node *dom = get_Block_idom(block);
260 DBG((dbg, LEVEL_3, "\t...continue at idom %+F\n", dom));
261 return search_def_end_of_block(env, dom);
265 * Fixes all operands of a use.
267 * If an operand of the use is a (original) definition,
268 * it will be replaced by the given definition.
270 static void set_operands(be_ssa_construction_env_t *env, ir_node *use, ir_node *def)
272 constr_info *info = phase_get_irn_data(env->phase, use);
273 int arity = get_irn_arity(use);
276 for (i = 0; i < arity; ++i) {
277 ir_node *op = get_irn_n(use, i);
279 if (is_definition(env, op)) {
280 DBG((dbg, LEVEL_1, "\t...%+F(%d) -> %+F\n", use, i, def));
281 set_irn_n(use, i, def);
285 info->already_processed = true;
289 * Fixes all uses of the given block.
291 static void process_block(be_ssa_construction_env_t *env, ir_node *block)
295 constr_info *block_info = phase_get_or_set_irn_data(env->phase, block);
297 assert(has_definition(env, block));
298 assert(!block_info->already_processed && "Block already processed");
300 DBG((dbg, LEVEL_3, "\tprocessing block %+F\n", block));
302 sched_foreach(block, node) {
303 if (is_use(env, node) && !is_Phi(node)) {
304 DBG((dbg, LEVEL_3, "\t...found use %+F\n", node));
307 /* Create a phi if the block is in the dominance frontier. */
308 if (Block_block_visited(block)) {
309 def = insert_dummy_phi(env, block);
312 def = get_def_at_idom(env, block);
316 set_operands(env, node, def);
319 if (is_definition(env, node)) {
320 constr_info *info = phase_get_irn_data(env->phase, node);
321 def = info->definition;
322 DBG((dbg, LEVEL_3, "\t...found definition %+F\n", def));
326 block_info->already_processed = true;
327 block_info->last_definition = def;
331 * @return Last definition of the given block.
333 static ir_node *search_def_end_of_block(be_ssa_construction_env_t *env,
336 constr_info *block_info = phase_get_or_set_irn_data(env->phase, block);
337 ir_node *last_definition = block_info->last_definition;
339 if (last_definition != NULL)
340 return last_definition;
342 if (has_definition(env, block)) {
343 if (has_use(env, block)) {
344 if (!block_info->already_processed) {
345 process_block(env, block);
351 /* Search the last definition of the block. */
352 sched_foreach_reverse(block, def) {
353 if (is_definition(env, def)) {
354 constr_info *info = phase_get_irn_data(env->phase, def);
355 def = info->definition;
356 DBG((dbg, LEVEL_3, "\t...found definition %+F\n", def));
362 assert(def && "No definition found");
364 block_info->last_definition = def;
367 return block_info->last_definition;
368 } else if (Block_block_visited(block)) {
369 ir_node *phi = insert_dummy_phi(env, block);
371 block_info->last_definition = phi;
375 ir_node *def = get_def_at_idom(env, block);
377 block_info->last_definition = def;
384 * Fixes all operands of the given use.
386 static void search_def_at_block(be_ssa_construction_env_t *env, ir_node *use)
388 ir_node *block = get_nodes_block(use);
389 constr_info *block_info = phase_get_or_set_irn_data(env->phase, block);
391 if (block_info->already_processed)
394 if (has_definition(env, block)) {
395 process_block(env, block);
396 } else if (Block_block_visited(block)) {
397 ir_node *phi = insert_dummy_phi(env, block);
399 set_operands(env, use, phi);
401 ir_node *def = get_def_at_idom(env, block);
403 set_operands(env, use, def);
407 static void *init_constr_info(ir_phase *phase, const ir_node *node)
409 constr_info *info = phase_alloc(phase, sizeof(constr_info));
412 info->is_definition = false;
413 info->is_use = false;
414 info->already_processed = false;
415 info->definition = NULL;
420 void be_ssa_construction_init(be_ssa_construction_env_t *env, ir_graph *irg)
422 ir_node *sb = get_irg_start_block(irg);
423 int n_blocks = get_Block_dom_max_subtree_pre_num(sb);
425 stat_ev_ctx_push_fobj("bessaconstr", irg);
429 stat_ev_dbl("bessaconstr_n_blocks", n_blocks);
431 memset(env, 0, sizeof(env[0]));
432 be_assure_dom_front(irg);
435 env->domfronts = be_get_irg_dom_front(irg);
436 env->new_phis = NEW_ARR_F(ir_node*, 0);
437 env->worklist = new_waitq();
438 env->phase = new_phase(irg, init_constr_info);
440 ir_reserve_resources(irg, IR_RESOURCE_IRN_VISITED
441 | IR_RESOURCE_BLOCK_VISITED | IR_RESOURCE_IRN_LINK);
443 /* we use the visited flag to indicate blocks in the dominance frontier
444 * and blocks that already have the relevant value at the end calculated */
445 inc_irg_visited(irg);
446 /* We use the block visited flag to indicate blocks in the dominance
447 * frontier of some values (and this potentially needing phis) */
448 inc_irg_block_visited(irg);
451 void be_ssa_construction_destroy(be_ssa_construction_env_t *env)
453 stat_ev_int("bessaconstr_phis", ARR_LEN(env->new_phis));
454 phase_free(env->phase);
455 del_waitq(env->worklist);
456 DEL_ARR_F(env->new_phis);
458 ir_free_resources(env->irg, IR_RESOURCE_IRN_VISITED
459 | IR_RESOURCE_BLOCK_VISITED | IR_RESOURCE_IRN_LINK);
461 stat_ev_tim_pop("bessaconstr_total_time");
462 stat_ev_ctx_pop("bessaconstr");
465 void be_ssa_construction_add_copy(be_ssa_construction_env_t *env,
470 assert(env->iterated_domfront_calculated == 0);
472 if (env->mode == NULL) {
473 env->mode = get_irn_mode(copy);
474 env->phi_cls = arch_get_irn_reg_class(copy);
476 assert(env->mode == get_irn_mode(copy));
479 block = get_nodes_block(copy);
481 if (!has_definition(env, block)) {
482 waitq_put(env->worklist, block);
484 introduce_definition(env, copy);
487 void be_ssa_construction_add_copies(be_ssa_construction_env_t *env,
488 ir_node **copies, size_t copies_len)
492 assert(env->iterated_domfront_calculated == 0);
494 if (env->mode == NULL) {
495 env->mode = get_irn_mode(copies[0]);
496 env->phi_cls = arch_get_irn_reg_class(copies[0]);
499 for (i = 0; i < copies_len; ++i) {
500 ir_node *copy = copies[i];
501 ir_node *block = get_nodes_block(copy);
503 assert(env->mode == get_irn_mode(copy));
504 if (!has_definition(env, block)) {
505 waitq_put(env->worklist, block);
507 introduce_definition(env, copy);
511 void be_ssa_construction_set_ignore_uses(be_ssa_construction_env_t *env,
512 const ir_nodeset_t *ignore_uses)
514 env->ignore_uses = ignore_uses;
517 ir_node **be_ssa_construction_get_new_phis(be_ssa_construction_env_t *env)
519 return env->new_phis;
523 * Fixes all arguments of a newly constructed phi.
525 * @see insert_dummy_phi
527 static void fix_phi_arguments(be_ssa_construction_env_t *env, ir_node *phi)
529 constr_info *info = phase_get_irn_data(env->phase, phi);
530 ir_node *block = get_nodes_block(phi);
532 int n_preds = get_Block_n_cfgpreds(block);
534 DBG((dbg, LEVEL_3, "\tfixing phi arguments %+F\n", phi));
536 for (i = 0; i < n_preds; ++i) {
537 ir_node *op = get_irn_n(phi, i);
539 if (is_definition(env, op) || is_Dummy(op)) {
540 ir_node *pred_block = get_Block_cfgpred_block(block, i);
541 ir_node *pred_def = search_def_end_of_block(env, pred_block);
543 DBG((dbg, LEVEL_1, "\t...%+F(%d) -> %+F\n", phi, i, pred_def));
544 set_irn_n(phi, i, pred_def);
548 info->already_processed = true;
551 void be_ssa_construction_fix_users_array(be_ssa_construction_env_t *env,
552 ir_node **nodes, size_t nodes_len)
555 stat_ev_cnt_decl(uses);
557 be_timer_push(T_SSA_CONSTR);
559 if (!env->iterated_domfront_calculated) {
560 mark_iterated_dominance_frontiers(env);
561 env->iterated_domfront_calculated = 1;
564 DBG((dbg, LEVEL_1, "\tfixing users array\n"));
566 assert(waitq_empty(env->worklist));
570 for (i = 0; i < nodes_len; ++i) {
571 const ir_edge_t *edge, *next;
572 ir_node *value = nodes[i];
573 DBG((dbg, LEVEL_3, "\tfixing users of %+F\n", value));
574 introduce_definition(env, value);
576 foreach_out_edge_safe(value, edge, next) {
577 ir_node *use = get_edge_src_irn(edge);
579 if (env->ignore_uses != NULL &&
580 ir_nodeset_contains(env->ignore_uses, use))
583 if (is_Anchor(use) || is_End(use))
586 introduce_use(env, use);
590 assert(!waitq_empty(env->worklist));
592 while (!waitq_empty(env->worklist)) {
593 ir_node *use = (ir_node *)waitq_get(env->worklist);
594 constr_info *info = phase_get_irn_data(env->phase, use);
596 if (info->already_processed)
600 fix_phi_arguments(env, use);
603 DBG((dbg, LEVEL_3, "\tsearching def for %+F at %+F\n", use, get_nodes_block(use)));
604 search_def_at_block(env, use);
607 stat_ev_cnt_inc(uses);
610 be_timer_pop(T_SSA_CONSTR);
612 stat_ev_tim_pop("bessaconstr_fix_time");
613 stat_ev_cnt_done(uses, "bessaconstr_uses");
616 void be_ssa_construction_fix_users(be_ssa_construction_env_t *env, ir_node *value)
618 be_ssa_construction_fix_users_array(env, &value, 1);
622 void be_ssa_construction_update_liveness_phis(be_ssa_construction_env_t *env,
627 be_timer_push(T_SSA_CONSTR);
629 n = ARR_LEN(env->new_phis);
630 for (i = 0; i < n; ++i) {
631 ir_node *phi = env->new_phis[i];
632 be_liveness_introduce(lv, phi);
635 be_timer_pop(T_SSA_CONSTR);
638 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_ssaconstr)
639 void be_init_ssaconstr(void)
641 FIRM_DBG_REGISTER(dbg, "firm.be.ssaconstr");