2 * This file is part of libFirm.
3 * Copyright (C) 2012 University of Karlsruhe.
8 * @brief SSA construction for a set of nodes
9 * @author Sebastian Hack, Daniel Grund, Matthias Braun, Christian Wuerdig,
13 * The problem: Given a value and a set of "copies" that are known to
14 * represent the same abstract value, rewire all usages of the original value
15 * to their closest copy while introducing phis as necessary.
17 * Algorithm: Mark all blocks in the iterated dominance frontiers of the value
18 * and its copies. Link the copies ordered by dominance to the blocks. Then
19 * we search for each use all definitions in the current block, if none is
20 * found, then we search one in the immediate dominator. If we are in a block
21 * of the dominance frontier, create a phi and do the same search for all
24 * A copy in this context means, that you want to introduce several new
25 * abstract values (in Firm: nodes) for which you know, that they
26 * represent the same concrete value. This is the case if you
32 * This function reroutes all uses of the original value to the copies in the
33 * corresponding dominance subtrees and creates Phi functions where necessary.
37 /* statev in this file is extensive, so only enable if needed */
38 #define DISABLE_STATEV
40 #include "bessaconstr.h"
43 #include "beintlive_t.h"
53 #include "irnodeset.h"
56 #include "iredges_t.h"
59 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
61 static ir_node *search_def_end_of_block(be_ssa_construction_env_t *env,
65 bool is_definition : 1;
67 bool already_processed : 1;
69 /* Since we only consider scheduled nodes,
70 * this points to the real definition (e.g. a Proj). */
73 /* Last definition of a block. */
74 ir_node *last_definition;
78 typedef struct constr_info constr_info;
81 * @return Whether the block contains a definition.
83 static bool has_definition(const ir_node *block)
85 return irn_visited(block);
88 static constr_info *get_or_set_info(be_ssa_construction_env_t *env,
91 constr_info *info = ir_nodemap_get(constr_info, &env->infos, node);
93 info = OALLOCZ(&env->obst, constr_info);
94 ir_nodemap_insert(&env->infos, node, info);
99 static constr_info *get_info(const be_ssa_construction_env_t *env,
102 return ir_nodemap_get(constr_info, &env->infos, node);
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 = get_or_set_info(env, block);
115 * @return Whether the node is a definition.
117 static bool is_definition(be_ssa_construction_env_t *env, ir_node *node)
119 constr_info *info = get_info(env, node);
120 return info != NULL && info->is_definition;
124 * Introduces a definition at the corresponding block.
126 static void introduce_definition(be_ssa_construction_env_t *env, ir_node *def)
128 ir_node *block = get_nodes_block(def);
129 constr_info *def_info = get_or_set_info(env, def);
130 ir_node *skip = skip_Proj(def);
131 constr_info *skip_info = get_or_set_info(env, skip);
132 constr_info *block_info = get_or_set_info(env, block);
134 DBG((dbg, LEVEL_2, "\tintroducing definition %+F in %+F\n", def, block));
136 def_info->is_definition = true;
138 skip_info->is_definition = true;
139 skip_info->u.definition = def;
141 // Set the last definition if we only introduce one definition for the block
142 if (has_definition(block)) {
143 assert(!block_info->already_processed);
144 block_info->u.last_definition = NULL;
146 mark_irn_visited(block);
147 block_info->u.last_definition = def;
151 static void introduce_use(be_ssa_construction_env_t *env, ir_node *use)
153 ir_node *block = get_nodes_block(use);
154 constr_info *info = get_or_set_info(env, use);
155 constr_info *block_info = get_or_set_info(env, block);
157 DBG((dbg, LEVEL_2, "\tintroducing use %+F in %+F\n", use, block));
160 block_info->is_use = true;
162 waitq_put(env->worklist, use);
166 * Calculates the iterated dominance frontier of a set of blocks. Marks the
167 * blocks as visited. Sets the link fields of the blocks in the dominance
168 * frontier to the block itself.
170 static void mark_iterated_dominance_frontiers(
171 const be_ssa_construction_env_t *env)
173 stat_ev_cnt_decl(blocks);
174 DBG((dbg, LEVEL_3, "Dominance Frontier:"));
176 while (!waitq_empty(env->worklist)) {
178 ir_node *block = (ir_node*)waitq_get(env->worklist);
179 ir_node **domfront = ir_get_dominance_frontier(block);
180 int domfront_len = ARR_LEN(domfront);
182 for (i = 0; i < domfront_len; ++i) {
183 ir_node *y = domfront[i];
184 if (Block_block_visited(y))
187 if (!irn_visited(y)) {
188 set_irn_link(y, NULL);
189 waitq_put(env->worklist, y);
192 DBG((dbg, LEVEL_3, " %+F", y));
193 mark_Block_block_visited(y);
194 stat_ev_cnt_inc(blocks);
197 stat_ev_tim_pop("bessaconstr_idf_time");
198 stat_ev_cnt_done(blocks, "bessaconstr_idf_blocks");
199 DBG((dbg, LEVEL_3, "\n"));
203 * Inserts a new phi at the given block.
205 * The constructed phi has only Dummy operands,
206 * but can be used as definition for other nodes.
208 * @see fix_phi_arguments
210 static ir_node *insert_dummy_phi(be_ssa_construction_env_t *env, ir_node *block)
212 int i, n_preds = get_Block_n_cfgpreds(block);
213 ir_graph *irg = get_Block_irg(block);
214 ir_node **ins = ALLOCAN(ir_node*, n_preds);
218 DBG((dbg, LEVEL_3, "\t...create phi at block %+F\n", block));
222 dummy = new_r_Dummy(irg, env->mode);
223 for (i = 0; i < n_preds; ++i) {
226 phi = be_new_Phi(block, n_preds, ins, env->mode, env->phi_req);
227 sched_add_after(block, phi);
228 ARR_APP1(ir_node*, env->new_phis, phi);
230 DBG((dbg, LEVEL_2, "\tcreating phi %+F in %+F\n", phi, block));
231 introduce_definition(env, phi);
233 waitq_put(env->worklist, phi);
239 * @return Last definition of the immediate dominator.
241 static ir_node *get_def_at_idom(be_ssa_construction_env_t *env, ir_node *block)
243 ir_node *dom = get_Block_idom(block);
245 DBG((dbg, LEVEL_3, "\t...continue at idom %+F\n", dom));
246 return search_def_end_of_block(env, dom);
249 static ir_node *get_def_from_preds(be_ssa_construction_env_t *const env, ir_node *const block)
251 /* Create a phi if the block is in the dominance frontier. */
252 if (Block_block_visited(block)) {
253 return insert_dummy_phi(env, block);
255 return get_def_at_idom(env, block);
260 * Fixes all operands of a use.
262 * If an operand of the use is a (original) definition,
263 * it will be replaced by the given definition.
265 static void set_operands(be_ssa_construction_env_t *env, ir_node *use, ir_node *def, constr_info *const use_info)
267 int arity = get_irn_arity(use);
268 for (int i = 0; i < arity; ++i) {
269 ir_node *op = get_irn_n(use, i);
271 if (is_definition(env, op)) {
272 DBG((dbg, LEVEL_1, "\t...%+F(%d) -> %+F\n", use, i, def));
273 set_irn_n(use, i, def);
277 use_info->already_processed = true;
281 * Fixes all uses of the given block.
283 static void process_block(be_ssa_construction_env_t *env, ir_node *block)
286 constr_info *block_info = get_or_set_info(env, block);
288 assert(has_definition(block));
289 assert(!block_info->already_processed && "Block already processed");
291 DBG((dbg, LEVEL_3, "\tprocessing block %+F\n", block));
293 sched_foreach(block, node) {
294 constr_info *const info = get_info(env, node);
298 if (info->is_use && !is_Phi(node)) {
299 DBG((dbg, LEVEL_3, "\t...found use %+F\n", node));
302 /* Create a phi if the block is in the dominance frontier. */
303 def = get_def_from_preds(env, block);
306 set_operands(env, node, def, info);
309 if (info->is_definition) {
310 def = info->u.definition;
311 DBG((dbg, LEVEL_3, "\t...found definition %+F\n", def));
315 block_info->already_processed = true;
316 block_info->u.last_definition = def;
320 * @return Last definition of the given block.
322 static ir_node *search_def_end_of_block(be_ssa_construction_env_t *env,
325 constr_info *block_info = get_or_set_info(env, block);
326 ir_node *last_definition = block_info->u.last_definition;
328 if (last_definition != NULL)
329 return last_definition;
331 if (has_definition(block)) {
332 if (has_use(env, block)) {
333 if (!block_info->already_processed) {
334 process_block(env, block);
338 /* Search the last definition of the block. */
339 sched_foreach_reverse(block, def) {
340 constr_info const *const info = get_info(env, def);
341 if (info && info->is_definition) {
342 DBG((dbg, LEVEL_3, "\t...found definition %+F\n", info->u.definition));
343 block_info->u.last_definition = info->u.definition;
348 assert(block_info->u.last_definition && "No definition found");
351 return block_info->u.last_definition;
353 ir_node *const def = get_def_from_preds(env, block);
354 block_info->u.last_definition = def;
360 * Fixes all operands of the given use.
362 static void search_def_at_block(be_ssa_construction_env_t *const env, ir_node *const use, constr_info *const info)
364 ir_node *block = get_nodes_block(use);
365 constr_info *block_info = get_or_set_info(env, block);
367 if (block_info->already_processed)
370 if (has_definition(block)) {
371 process_block(env, block);
373 ir_node *const def = get_def_from_preds(env, block);
374 set_operands(env, use, def, info);
378 void be_ssa_construction_init(be_ssa_construction_env_t *env, ir_graph *irg)
380 stat_ev_ctx_push_fmt("bessaconstr", "%+F", irg);
383 stat_ev_dbl("bessaconstr_n_blocks", get_Block_dom_max_subtree_pre_num(get_irg_start_block(irg)));
385 memset(env, 0, sizeof(env[0]));
388 env->new_phis = NEW_ARR_F(ir_node*, 0);
389 env->worklist = new_waitq();
390 ir_nodemap_init(&env->infos, irg);
391 obstack_init(&env->obst);
393 assure_irg_properties(env->irg,
394 IR_GRAPH_PROPERTY_CONSISTENT_DOMINANCE_FRONTIERS);
396 ir_reserve_resources(irg, IR_RESOURCE_IRN_VISITED
397 | IR_RESOURCE_BLOCK_VISITED | IR_RESOURCE_IRN_LINK);
399 /* we use the visited flag to indicate blocks in the dominance frontier
400 * and blocks that already have the relevant value at the end calculated */
401 inc_irg_visited(irg);
402 /* We use the block visited flag to indicate blocks in the dominance
403 * frontier of some values (and this potentially needing phis) */
404 inc_irg_block_visited(irg);
407 void be_ssa_construction_destroy(be_ssa_construction_env_t *env)
409 stat_ev_int("bessaconstr_phis", ARR_LEN(env->new_phis));
410 obstack_free(&env->obst, NULL);
411 ir_nodemap_destroy(&env->infos);
412 del_waitq(env->worklist);
413 DEL_ARR_F(env->new_phis);
415 ir_free_resources(env->irg, IR_RESOURCE_IRN_VISITED
416 | IR_RESOURCE_BLOCK_VISITED | IR_RESOURCE_IRN_LINK);
418 stat_ev_tim_pop("bessaconstr_total_time");
419 stat_ev_ctx_pop("bessaconstr");
422 static void determine_phi_req(be_ssa_construction_env_t *env, ir_node *value)
424 const arch_register_req_t *req = arch_get_irn_register_req(value);
425 env->mode = get_irn_mode(value);
426 if (req->width == 1) {
427 env->phi_req = req->cls->class_req;
429 /* construct a new register req... */
430 ir_graph *irg = get_irn_irg(value);
431 struct obstack *obst = be_get_be_obst(irg);
432 arch_register_req_t *new_req = OALLOCZ(obst, arch_register_req_t);
433 new_req->cls = req->cls;
434 new_req->type = req->type & arch_register_req_type_aligned;
435 new_req->width = req->width;
436 env->phi_req = new_req;
440 void be_ssa_construction_add_copy(be_ssa_construction_env_t *env,
445 assert(env->iterated_domfront_calculated == 0);
447 if (env->mode == NULL) {
448 determine_phi_req(env, copy);
450 assert(env->mode == get_irn_mode(copy));
453 block = get_nodes_block(copy);
455 if (!has_definition(block)) {
456 waitq_put(env->worklist, block);
458 introduce_definition(env, copy);
461 void be_ssa_construction_add_copies(be_ssa_construction_env_t *env,
462 ir_node **copies, size_t copies_len)
466 assert(env->iterated_domfront_calculated == 0);
468 if (env->mode == NULL) {
469 determine_phi_req(env, copies[0]);
472 for (i = 0; i < copies_len; ++i) {
473 ir_node *copy = copies[i];
474 ir_node *block = get_nodes_block(copy);
476 assert(env->mode == get_irn_mode(copy));
477 if (!has_definition(block)) {
478 waitq_put(env->worklist, block);
480 introduce_definition(env, copy);
484 ir_node **be_ssa_construction_get_new_phis(be_ssa_construction_env_t *env)
486 return env->new_phis;
490 * Fixes all arguments of a newly constructed phi.
492 * @see insert_dummy_phi
494 static void fix_phi_arguments(be_ssa_construction_env_t *const env, ir_node *const phi, constr_info *const info)
496 ir_node *block = get_nodes_block(phi);
497 int n_preds = get_Block_n_cfgpreds(block);
499 DBG((dbg, LEVEL_3, "\tfixing phi arguments %+F\n", phi));
501 for (int i = 0; i < n_preds; ++i) {
502 ir_node *op = get_irn_n(phi, i);
504 if (is_definition(env, op) || is_Dummy(op)) {
505 ir_node *pred_block = get_Block_cfgpred_block(block, i);
506 ir_node *pred_def = search_def_end_of_block(env, pred_block);
508 DBG((dbg, LEVEL_1, "\t...%+F(%d) -> %+F\n", phi, i, pred_def));
509 set_irn_n(phi, i, pred_def);
513 info->already_processed = true;
516 void be_ssa_construction_fix_users_array(be_ssa_construction_env_t *env,
517 ir_node **nodes, size_t nodes_len)
520 stat_ev_cnt_decl(uses);
522 be_timer_push(T_SSA_CONSTR);
524 if (!env->iterated_domfront_calculated) {
525 mark_iterated_dominance_frontiers(env);
526 env->iterated_domfront_calculated = 1;
529 DBG((dbg, LEVEL_1, "\tfixing users array\n"));
531 assert(waitq_empty(env->worklist));
535 for (i = 0; i < nodes_len; ++i) {
536 ir_node *value = nodes[i];
537 DBG((dbg, LEVEL_3, "\tfixing users of %+F\n", value));
538 introduce_definition(env, value);
540 foreach_out_edge_safe(value, edge) {
541 ir_node *const use = get_edge_src_irn(edge);
542 if (is_Anchor(use) || is_End(use))
545 introduce_use(env, use);
549 assert(!waitq_empty(env->worklist));
551 while (!waitq_empty(env->worklist)) {
552 ir_node *use = (ir_node *)waitq_get(env->worklist);
553 constr_info *info = get_info(env, use);
555 if (info->already_processed)
559 fix_phi_arguments(env, use, info);
562 DBG((dbg, LEVEL_3, "\tsearching def for %+F at %+F\n", use, get_nodes_block(use)));
563 search_def_at_block(env, use, info);
566 stat_ev_cnt_inc(uses);
569 be_timer_pop(T_SSA_CONSTR);
571 stat_ev_tim_pop("bessaconstr_fix_time");
572 stat_ev_cnt_done(uses, "bessaconstr_uses");
575 void be_ssa_construction_fix_users(be_ssa_construction_env_t *env, ir_node *value)
577 be_ssa_construction_fix_users_array(env, &value, 1);
581 void be_ssa_construction_update_liveness_phis(be_ssa_construction_env_t *env,
586 be_timer_push(T_SSA_CONSTR);
588 n = ARR_LEN(env->new_phis);
589 for (i = 0; i < n; ++i) {
590 ir_node *phi = env->new_phis[i];
591 be_liveness_introduce(lv, phi);
594 be_timer_pop(T_SSA_CONSTR);
597 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_ssaconstr)
598 void be_init_ssaconstr(void)
600 FIRM_DBG_REGISTER(dbg, "firm.be.ssaconstr");