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 its 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"
67 #include "irnodeset.h"
70 #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(constr_info, &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 ir_nodemap_get(constr_info, &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 * Introduces a definition at the corresponding block.
140 static void introduce_definition(be_ssa_construction_env_t *env, ir_node *def)
142 ir_node *block = get_nodes_block(def);
143 constr_info *def_info = get_or_set_info(env, def);
144 ir_node *skip = skip_Proj(def);
145 constr_info *skip_info = get_or_set_info(env, skip);
146 constr_info *block_info = get_or_set_info(env, block);
148 DBG((dbg, LEVEL_2, "\tintroducing definition %+F in %+F\n", def, block));
150 def_info->is_definition = true;
152 skip_info->is_definition = true;
153 skip_info->u.definition = def;
155 // Set the last definition if we only introduce one definition for the block
156 if (has_definition(block)) {
157 assert(!block_info->already_processed);
158 block_info->u.last_definition = NULL;
160 mark_irn_visited(block);
161 block_info->u.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 constr_info *info = get_or_set_info(env, use);
169 constr_info *block_info = get_or_set_info(env, block);
171 DBG((dbg, LEVEL_2, "\tintroducing use %+F in %+F\n", use, block));
174 block_info->is_use = true;
176 waitq_put(env->worklist, use);
180 * Calculates the iterated dominance frontier of a set of blocks. Marks the
181 * blocks as visited. Sets the link fields of the blocks in the dominance
182 * frontier to the block itself.
184 static void mark_iterated_dominance_frontiers(
185 const be_ssa_construction_env_t *env)
187 stat_ev_cnt_decl(blocks);
188 DBG((dbg, LEVEL_3, "Dominance Frontier:"));
190 while (!waitq_empty(env->worklist)) {
192 ir_node *block = (ir_node*)waitq_get(env->worklist);
193 ir_node **domfront = ir_get_dominance_frontier(block);
194 int domfront_len = ARR_LEN(domfront);
196 for (i = 0; i < domfront_len; ++i) {
197 ir_node *y = domfront[i];
198 if (Block_block_visited(y))
201 if (!irn_visited(y)) {
202 set_irn_link(y, NULL);
203 waitq_put(env->worklist, y);
206 DBG((dbg, LEVEL_3, " %+F", y));
207 mark_Block_block_visited(y);
208 stat_ev_cnt_inc(blocks);
211 stat_ev_tim_pop("bessaconstr_idf_time");
212 stat_ev_cnt_done(blocks, "bessaconstr_idf_blocks");
213 DBG((dbg, LEVEL_3, "\n"));
217 * Inserts a new phi at the given block.
219 * The constructed phi has only Dummy operands,
220 * but can be used as definition for other nodes.
222 * @see fix_phi_arguments
224 static ir_node *insert_dummy_phi(be_ssa_construction_env_t *env, ir_node *block)
226 int i, n_preds = get_Block_n_cfgpreds(block);
227 ir_graph *irg = get_Block_irg(block);
228 ir_node **ins = ALLOCAN(ir_node*, n_preds);
232 DBG((dbg, LEVEL_3, "\t...create phi at block %+F\n", block));
236 dummy = new_r_Dummy(irg, env->mode);
237 for (i = 0; i < n_preds; ++i) {
240 phi = be_new_Phi(block, n_preds, ins, env->mode, env->phi_req);
241 sched_add_after(block, phi);
242 ARR_APP1(ir_node*, env->new_phis, phi);
244 DBG((dbg, LEVEL_2, "\tcreating phi %+F in %+F\n", phi, block));
245 introduce_definition(env, phi);
247 waitq_put(env->worklist, phi);
253 * @return Last definition of the immediate dominator.
255 static ir_node *get_def_at_idom(be_ssa_construction_env_t *env, ir_node *block)
257 ir_node *dom = get_Block_idom(block);
259 DBG((dbg, LEVEL_3, "\t...continue at idom %+F\n", dom));
260 return search_def_end_of_block(env, dom);
263 static ir_node *get_def_from_preds(be_ssa_construction_env_t *const env, ir_node *const block)
265 /* Create a phi if the block is in the dominance frontier. */
266 if (Block_block_visited(block)) {
267 return insert_dummy_phi(env, block);
269 return get_def_at_idom(env, block);
274 * Fixes all operands of a use.
276 * If an operand of the use is a (original) definition,
277 * it will be replaced by the given definition.
279 static void set_operands(be_ssa_construction_env_t *env, ir_node *use, ir_node *def, constr_info *const use_info)
281 int arity = get_irn_arity(use);
282 for (int i = 0; i < arity; ++i) {
283 ir_node *op = get_irn_n(use, i);
285 if (is_definition(env, op)) {
286 DBG((dbg, LEVEL_1, "\t...%+F(%d) -> %+F\n", use, i, def));
287 set_irn_n(use, i, def);
291 use_info->already_processed = true;
295 * Fixes all uses of the given block.
297 static void process_block(be_ssa_construction_env_t *env, ir_node *block)
300 constr_info *block_info = get_or_set_info(env, block);
302 assert(has_definition(block));
303 assert(!block_info->already_processed && "Block already processed");
305 DBG((dbg, LEVEL_3, "\tprocessing block %+F\n", block));
307 sched_foreach(block, node) {
308 constr_info *const info = get_info(env, node);
312 if (info->is_use && !is_Phi(node)) {
313 DBG((dbg, LEVEL_3, "\t...found use %+F\n", node));
316 /* Create a phi if the block is in the dominance frontier. */
317 def = get_def_from_preds(env, block);
320 set_operands(env, node, def, info);
323 if (info->is_definition) {
324 def = info->u.definition;
325 DBG((dbg, LEVEL_3, "\t...found definition %+F\n", def));
329 block_info->already_processed = true;
330 block_info->u.last_definition = def;
334 * @return Last definition of the given block.
336 static ir_node *search_def_end_of_block(be_ssa_construction_env_t *env,
339 constr_info *block_info = get_or_set_info(env, block);
340 ir_node *last_definition = block_info->u.last_definition;
342 if (last_definition != NULL)
343 return last_definition;
345 if (has_definition(block)) {
346 if (has_use(env, block)) {
347 if (!block_info->already_processed) {
348 process_block(env, block);
352 /* Search the last definition of the block. */
353 sched_foreach_reverse(block, def) {
354 constr_info const *const info = get_info(env, def);
355 if (info && info->is_definition) {
356 DBG((dbg, LEVEL_3, "\t...found definition %+F\n", info->u.definition));
357 block_info->u.last_definition = info->u.definition;
362 assert(block_info->u.last_definition && "No definition found");
365 return block_info->u.last_definition;
367 ir_node *const def = get_def_from_preds(env, block);
368 block_info->u.last_definition = def;
374 * Fixes all operands of the given use.
376 static void search_def_at_block(be_ssa_construction_env_t *const env, ir_node *const use, constr_info *const info)
378 ir_node *block = get_nodes_block(use);
379 constr_info *block_info = get_or_set_info(env, block);
381 if (block_info->already_processed)
384 if (has_definition(block)) {
385 process_block(env, block);
387 ir_node *const def = get_def_from_preds(env, block);
388 set_operands(env, use, def, info);
392 void be_ssa_construction_init(be_ssa_construction_env_t *env, ir_graph *irg)
394 ir_node *sb = get_irg_start_block(irg);
395 int n_blocks = get_Block_dom_max_subtree_pre_num(sb);
397 stat_ev_ctx_push_fmt("bessaconstr", "%+F", irg);
401 stat_ev_dbl("bessaconstr_n_blocks", n_blocks);
403 memset(env, 0, sizeof(env[0]));
406 env->new_phis = NEW_ARR_F(ir_node*, 0);
407 env->worklist = new_waitq();
408 ir_nodemap_init(&env->infos, irg);
409 obstack_init(&env->obst);
411 assure_irg_properties(env->irg,
412 IR_GRAPH_PROPERTY_CONSISTENT_DOMINANCE_FRONTIERS);
414 ir_reserve_resources(irg, IR_RESOURCE_IRN_VISITED
415 | IR_RESOURCE_BLOCK_VISITED | IR_RESOURCE_IRN_LINK);
417 /* we use the visited flag to indicate blocks in the dominance frontier
418 * and blocks that already have the relevant value at the end calculated */
419 inc_irg_visited(irg);
420 /* We use the block visited flag to indicate blocks in the dominance
421 * frontier of some values (and this potentially needing phis) */
422 inc_irg_block_visited(irg);
425 void be_ssa_construction_destroy(be_ssa_construction_env_t *env)
427 stat_ev_int("bessaconstr_phis", ARR_LEN(env->new_phis));
428 obstack_free(&env->obst, NULL);
429 ir_nodemap_destroy(&env->infos);
430 del_waitq(env->worklist);
431 DEL_ARR_F(env->new_phis);
433 ir_free_resources(env->irg, IR_RESOURCE_IRN_VISITED
434 | IR_RESOURCE_BLOCK_VISITED | IR_RESOURCE_IRN_LINK);
436 stat_ev_tim_pop("bessaconstr_total_time");
437 stat_ev_ctx_pop("bessaconstr");
440 static void determine_phi_req(be_ssa_construction_env_t *env, ir_node *value)
442 const arch_register_req_t *req = arch_get_irn_register_req(value);
443 env->mode = get_irn_mode(value);
444 if (req->width == 1) {
445 env->phi_req = req->cls->class_req;
447 /* construct a new register req... */
448 ir_graph *irg = get_irn_irg(value);
449 struct obstack *obst = be_get_be_obst(irg);
450 arch_register_req_t *new_req = OALLOCZ(obst, arch_register_req_t);
451 new_req->cls = req->cls;
452 new_req->type = req->type & arch_register_req_type_aligned;
453 new_req->width = req->width;
454 env->phi_req = new_req;
458 void be_ssa_construction_add_copy(be_ssa_construction_env_t *env,
463 assert(env->iterated_domfront_calculated == 0);
465 if (env->mode == NULL) {
466 determine_phi_req(env, copy);
468 assert(env->mode == get_irn_mode(copy));
471 block = get_nodes_block(copy);
473 if (!has_definition(block)) {
474 waitq_put(env->worklist, block);
476 introduce_definition(env, copy);
479 void be_ssa_construction_add_copies(be_ssa_construction_env_t *env,
480 ir_node **copies, size_t copies_len)
484 assert(env->iterated_domfront_calculated == 0);
486 if (env->mode == NULL) {
487 determine_phi_req(env, copies[0]);
490 for (i = 0; i < copies_len; ++i) {
491 ir_node *copy = copies[i];
492 ir_node *block = get_nodes_block(copy);
494 assert(env->mode == get_irn_mode(copy));
495 if (!has_definition(block)) {
496 waitq_put(env->worklist, block);
498 introduce_definition(env, copy);
502 ir_node **be_ssa_construction_get_new_phis(be_ssa_construction_env_t *env)
504 return env->new_phis;
508 * Fixes all arguments of a newly constructed phi.
510 * @see insert_dummy_phi
512 static void fix_phi_arguments(be_ssa_construction_env_t *const env, ir_node *const phi, constr_info *const info)
514 ir_node *block = get_nodes_block(phi);
515 int n_preds = get_Block_n_cfgpreds(block);
517 DBG((dbg, LEVEL_3, "\tfixing phi arguments %+F\n", phi));
519 for (int i = 0; i < n_preds; ++i) {
520 ir_node *op = get_irn_n(phi, i);
522 if (is_definition(env, op) || is_Dummy(op)) {
523 ir_node *pred_block = get_Block_cfgpred_block(block, i);
524 ir_node *pred_def = search_def_end_of_block(env, pred_block);
526 DBG((dbg, LEVEL_1, "\t...%+F(%d) -> %+F\n", phi, i, pred_def));
527 set_irn_n(phi, i, pred_def);
531 info->already_processed = true;
534 void be_ssa_construction_fix_users_array(be_ssa_construction_env_t *env,
535 ir_node **nodes, size_t nodes_len)
538 stat_ev_cnt_decl(uses);
540 be_timer_push(T_SSA_CONSTR);
542 if (!env->iterated_domfront_calculated) {
543 mark_iterated_dominance_frontiers(env);
544 env->iterated_domfront_calculated = 1;
547 DBG((dbg, LEVEL_1, "\tfixing users array\n"));
549 assert(waitq_empty(env->worklist));
553 for (i = 0; i < nodes_len; ++i) {
554 ir_node *value = nodes[i];
555 DBG((dbg, LEVEL_3, "\tfixing users of %+F\n", value));
556 introduce_definition(env, value);
558 foreach_out_edge_safe(value, edge) {
559 ir_node *const use = get_edge_src_irn(edge);
560 if (is_Anchor(use) || is_End(use))
563 introduce_use(env, use);
567 assert(!waitq_empty(env->worklist));
569 while (!waitq_empty(env->worklist)) {
570 ir_node *use = (ir_node *)waitq_get(env->worklist);
571 constr_info *info = get_info(env, use);
573 if (info->already_processed)
577 fix_phi_arguments(env, use, info);
580 DBG((dbg, LEVEL_3, "\tsearching def for %+F at %+F\n", use, get_nodes_block(use)));
581 search_def_at_block(env, use, info);
584 stat_ev_cnt_inc(uses);
587 be_timer_pop(T_SSA_CONSTR);
589 stat_ev_tim_pop("bessaconstr_fix_time");
590 stat_ev_cnt_done(uses, "bessaconstr_uses");
593 void be_ssa_construction_fix_users(be_ssa_construction_env_t *env, ir_node *value)
595 be_ssa_construction_fix_users_array(env, &value, 1);
599 void be_ssa_construction_update_liveness_phis(be_ssa_construction_env_t *env,
604 be_timer_push(T_SSA_CONSTR);
606 n = ARR_LEN(env->new_phis);
607 for (i = 0; i < n; ++i) {
608 ir_node *phi = env->new_phis[i];
609 be_liveness_introduce(lv, phi);
612 be_timer_pop(T_SSA_CONSTR);
615 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_ssaconstr)
616 void be_init_ssaconstr(void)
618 FIRM_DBG_REGISTER(dbg, "firm.be.ssaconstr");