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
27 * The problem: Given a value and a set of "copies" that are known to
28 * represent the same abstract value, rewire all usages of the original value
29 * to their closest copy while introducing phis as necessary.
31 * Algorithm: Mark all blocks in the iterated dominance frontiers of the value
32 * and it's copies. Link the copies ordered by dominance to the blocks. Then
33 * we search for each use all definitions in the current block, if none is
34 * found, then we search one in the immediate dominator. If we are in a block
35 * of the dominance frontier, create a phi and do the same search for all
38 * A copy in this context means, that you want to introduce several new
39 * abstract values (in Firm: nodes) for which you know, that they
40 * represent the same concrete value. This is the case if you
46 * This function reroutes all uses of the original value to the copies in the
47 * corresponding dominance subtrees and creates Phi functions where necessary.
51 /* statev in this file is extensive, so only enable if needed */
52 #define DISABLE_STATEV
54 #include "bessaconstr.h"
57 #include "beintlive_t.h"
69 #include "iredges_t.h"
71 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
74 * Calculates the iterated dominance frontier of a set of blocks. Marks the
75 * blocks as visited. Sets the link fields of the blocks in the dominance
76 * frontier to the block itself.
78 static void mark_iterated_dominance_frontiers(
79 const be_ssa_construction_env_t *env)
81 stat_ev_cnt_decl(blocks);
82 DBG((dbg, LEVEL_3, "Dominance Frontier:"));
84 while (!waitq_empty(env->worklist)) {
86 ir_node *block = (ir_node*)waitq_get(env->worklist);
87 ir_node **domfront = be_get_dominance_frontier(env->domfronts, block);
88 int domfront_len = ARR_LEN(domfront);
90 for (i = 0; i < domfront_len; ++i) {
91 ir_node *y = domfront[i];
92 if (Block_block_visited(y))
95 if (!irn_visited(y)) {
96 set_irn_link(y, NULL);
97 waitq_put(env->worklist, y);
100 DBG((dbg, LEVEL_3, " %+F", y));
101 mark_Block_block_visited(y);
102 stat_ev_cnt_inc(blocks);
105 stat_ev_tim_pop("bessaconstr_idf_time");
106 stat_ev_cnt_done(blocks, "bessaconstr_idf_blocks");
107 DBG((dbg, LEVEL_3, "\n"));
110 static ir_node *search_def_end_of_block(be_ssa_construction_env_t *env,
113 static ir_node *create_phi(be_ssa_construction_env_t *env, ir_node *block,
116 int i, n_preds = get_Block_n_cfgpreds(block);
117 ir_graph *irg = get_Block_irg(block);
118 ir_node **ins = ALLOCAN(ir_node*, n_preds);
124 dummy = new_r_Dummy(irg, env->mode);
125 for (i = 0; i < n_preds; ++i) {
128 phi = be_new_Phi(block, n_preds, ins, env->mode, env->phi_cls);
129 if (env->new_phis != NULL) {
130 ARR_APP1(ir_node*, env->new_phis, phi);
133 if (env->mode != mode_M) {
134 sched_add_after(block, phi);
137 DBG((dbg, LEVEL_2, "\tcreating phi %+F in %+F\n", phi, block));
138 set_irn_link(link_with, phi);
139 mark_irn_visited(block);
141 for (i = 0; i < n_preds; ++i) {
142 ir_node *pred_block = get_Block_cfgpred_block(block, i);
143 ir_node *pred_def = search_def_end_of_block(env, pred_block);
145 set_irn_n(phi, i, pred_def);
151 static ir_node *get_def_at_idom(be_ssa_construction_env_t *env, ir_node *block)
153 ir_node *dom = get_Block_idom(block);
155 return search_def_end_of_block(env, dom);
158 static ir_node *search_def_end_of_block(be_ssa_construction_env_t *env,
161 if (irn_visited(block)) {
162 assert(get_irn_link(block) != NULL);
163 return (ir_node*)get_irn_link(block);
164 } else if (Block_block_visited(block)) {
165 return create_phi(env, block, block);
167 ir_node *def = get_def_at_idom(env, block);
168 mark_irn_visited(block);
169 set_irn_link(block, def);
174 static ir_node *search_def(be_ssa_construction_env_t *env, ir_node *at)
176 ir_node *block = get_nodes_block(at);
180 DBG((dbg, LEVEL_3, "\t...searching def at %+F\n", at));
182 /* no defs in the current block we can do the normal searching */
183 if (!irn_visited(block) && !Block_block_visited(block)) {
184 DBG((dbg, LEVEL_3, "\t...continue at idom\n"));
185 return get_def_at_idom(env, block);
188 /* there are defs in the current block, walk the linked list to find
189 the one immediately dominating us
192 def = (ir_node*)get_irn_link(node);
193 while (def != NULL) {
194 if (!value_dominates(at, def)) {
195 DBG((dbg, LEVEL_3, "\t...found dominating def %+F\n", def));
200 def = (ir_node*)get_irn_link(node);
203 /* block in dominance frontier? create a phi then */
204 if (Block_block_visited(block)) {
205 DBG((dbg, LEVEL_3, "\t...create phi at block %+F\n", block));
206 assert(!is_Phi(node));
207 return create_phi(env, block, node);
210 DBG((dbg, LEVEL_3, "\t...continue at idom (after checking block)\n"));
211 return get_def_at_idom(env, block);
215 * Adds a definition into the link field of the block. The definitions are
216 * sorted by dominance. A non-visited block means no definition has been
219 static void introduce_def_at_block(ir_node *block, ir_node *def)
221 if (irn_visited_else_mark(block)) {
222 ir_node *node = block;
223 ir_node *current_def;
226 current_def = (ir_node*)get_irn_link(node);
227 if (current_def == def) {
228 /* already in block */
231 if (current_def == NULL)
233 if (value_dominates(current_def, def))
238 set_irn_link(node, def);
239 set_irn_link(def, current_def);
241 set_irn_link(block, def);
242 set_irn_link(def, NULL);
246 void be_ssa_construction_init(be_ssa_construction_env_t *env, ir_graph *irg)
248 ir_node *sb = get_irg_start_block(irg);
249 int n_blocks = get_Block_dom_max_subtree_pre_num(sb);
251 stat_ev_ctx_push_fobj("bessaconstr", irg);
255 stat_ev_dbl("bessaconstr_n_blocks", n_blocks);
257 memset(env, 0, sizeof(env[0]));
258 be_assure_dom_front(irg);
261 env->domfronts = be_get_irg_dom_front(irg);
262 env->new_phis = NEW_ARR_F(ir_node*, 0);
263 env->worklist = new_waitq();
265 ir_reserve_resources(irg, IR_RESOURCE_IRN_VISITED
266 | IR_RESOURCE_BLOCK_VISITED | IR_RESOURCE_IRN_LINK);
268 /* we use the visited flag to indicate blocks in the dominance frontier
269 * and blocks that already have the relevant value at the end calculated */
270 inc_irg_visited(irg);
271 /* We use the block visited flag to indicate blocks in the dominance
272 * frontier of some values (and this potentially needing phis) */
273 inc_irg_block_visited(irg);
276 void be_ssa_construction_destroy(be_ssa_construction_env_t *env)
278 stat_ev_int("bessaconstr_phis", ARR_LEN(env->new_phis));
279 del_waitq(env->worklist);
280 DEL_ARR_F(env->new_phis);
282 ir_free_resources(env->irg, IR_RESOURCE_IRN_VISITED
283 | IR_RESOURCE_BLOCK_VISITED | IR_RESOURCE_IRN_LINK);
285 stat_ev_tim_pop("bessaconstr_total_time");
286 stat_ev_ctx_pop("bessaconstr");
289 void be_ssa_construction_add_copy(be_ssa_construction_env_t *env,
294 assert(env->iterated_domfront_calculated == 0);
296 if (env->mode == NULL) {
297 env->mode = get_irn_mode(copy);
298 env->phi_cls = arch_get_irn_reg_class_out(copy);
300 assert(env->mode == get_irn_mode(copy));
303 block = get_nodes_block(copy);
305 if (!irn_visited(block)) {
306 waitq_put(env->worklist, block);
308 introduce_def_at_block(block, copy);
311 void be_ssa_construction_add_copies(be_ssa_construction_env_t *env,
312 ir_node **copies, size_t copies_len)
316 assert(env->iterated_domfront_calculated == 0);
318 if (env->mode == NULL) {
319 env->mode = get_irn_mode(copies[0]);
320 env->phi_cls = arch_get_irn_reg_class_out(copies[0]);
323 for (i = 0; i < copies_len; ++i) {
324 ir_node *copy = copies[i];
325 ir_node *block = get_nodes_block(copy);
327 assert(env->mode == get_irn_mode(copy));
328 if (!irn_visited(block)) {
329 waitq_put(env->worklist, block);
331 introduce_def_at_block(block, copy);
335 void be_ssa_construction_set_ignore_uses(be_ssa_construction_env_t *env,
336 const ir_nodeset_t *ignore_uses)
338 env->ignore_uses = ignore_uses;
341 ir_node **be_ssa_construction_get_new_phis(be_ssa_construction_env_t *env)
343 return env->new_phis;
346 void be_ssa_construction_fix_users_array(be_ssa_construction_env_t *env,
347 ir_node **nodes, size_t nodes_len)
349 const ir_edge_t *edge, *next;
351 stat_ev_cnt_decl(uses);
353 be_timer_push(T_SSA_CONSTR);
355 if (!env->iterated_domfront_calculated) {
356 mark_iterated_dominance_frontiers(env);
357 env->iterated_domfront_calculated = 1;
361 for (i = 0; i < nodes_len; ++i) {
362 ir_node *value = nodes[i];
365 * Search the valid def for each use and set it.
367 foreach_out_edge_safe(value, edge, next) {
368 ir_node *use = get_edge_src_irn(edge);
370 int pos = get_edge_src_pos(edge);
373 if (env->ignore_uses != NULL &&
374 ir_nodeset_contains(env->ignore_uses, use))
376 if (is_Anchor(use) || is_End(use))
380 ir_node *block = get_nodes_block(use);
381 ir_node *predblock = get_Block_cfgpred_block(block, pos);
382 at = sched_last(predblock);
385 def = search_def(env, at);
388 panic("no definition found for %+F at position %d", use, pos);
391 DBG((dbg, LEVEL_2, "\t%+F(%d) -> %+F\n", use, pos, def));
392 set_irn_n(use, pos, def);
393 stat_ev_cnt_inc(uses);
396 be_timer_pop(T_SSA_CONSTR);
398 stat_ev_tim_pop("bessaconstr_fix_time");
399 stat_ev_cnt_done(uses, "bessaconstr_uses");
402 void be_ssa_construction_fix_users(be_ssa_construction_env_t *env, ir_node *value)
404 be_ssa_construction_fix_users_array(env, &value, 1);
408 void be_ssa_construction_update_liveness_phis(be_ssa_construction_env_t *env,
413 be_timer_push(T_SSA_CONSTR);
415 n = ARR_LEN(env->new_phis);
416 for (i = 0; i < n; ++i) {
417 ir_node *phi = env->new_phis[i];
418 be_liveness_introduce(lv, phi);
421 be_timer_pop(T_SSA_CONSTR);
424 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_ssaconstr);
425 void be_init_ssaconstr(void)
427 FIRM_DBG_REGISTER(dbg, "firm.be.ssaconstr");