2 * This file is part of libFirm.
3 * Copyright (C) 2012 University of Karlsruhe.
9 * @author Christoph Mallon
16 #include "iroptimize.h"
33 * Environment for if-conversion.
35 typedef struct walker_env {
36 arch_allow_ifconv_func allow_ifconv;
37 bool changed; /**< Set if the graph was changed. */
40 DEBUG_ONLY(static firm_dbg_module_t *dbg;)
43 * Returns non-zero if a Block can be emptied.
45 * @param block the block
47 static bool can_empty_block(ir_node *block)
49 return get_Block_mark(block) == 0;
53 * Find the ProjX node leading from block dependency to block start.
55 * @param start a block that is control depended on dependency
56 * @param dependency the block that decides whether start is executed
58 * @return a ProjX node that represent the decision control flow or
59 * NULL is start is not dependent at all or a block on the way
62 static ir_node* walk_to_projx(ir_node* start, const ir_node* dependency)
67 /* No need to find the conditional block if this block cannot be emptied and
68 * therefore not moved */
69 if (!can_empty_block(start)) return NULL;
71 arity = get_irn_arity(start);
72 for (i = 0; i < arity; ++i) {
73 ir_node* pred = get_irn_n(start, i);
74 ir_node* pred_block = get_nodes_block(skip_Proj(pred));
76 if (pred_block == dependency) {
78 assert(get_irn_mode(pred) == mode_X);
82 /* Not a Proj? Should not happen. */
87 assert(get_irn_mode(pred) == mode_X);
88 /* another Proj but not from the control block */
92 if (is_cdep_on(pred_block, dependency)) {
93 return walk_to_projx(pred_block, dependency);
101 * Recursively copies the DAG starting at node to the i-th predecessor
103 * - if node isn't in the src_block, recursion ends and node is returned
104 * - if node is a Phi in the src_block, the i-th predecessor of this Phi is
105 * returned and recursion ends
106 * otherwise returns a copy of the passed node created in the i-th predecessor of
109 * @param node a root of a DAG
110 * @param src_block the block of the DAG
111 * @param i the position of the predecessor the DAG
114 * @return the root of the copied DAG
116 static ir_node* copy_to(ir_node* node, ir_node* src_block, int i)
122 if (get_nodes_block(node) != src_block) {
123 /* already outside src_block, do not copy */
127 /* move through the Phi to the i-th predecessor */
128 return get_irn_n(node, i);
131 /* else really need a copy */
132 copy = exact_copy(node);
133 dst_block = get_nodes_block(get_irn_n(src_block, i));
134 set_nodes_block(copy, dst_block);
136 DB((dbg, LEVEL_1, "Copying node %+F to block %+F, copy is %+F\n",
137 node, dst_block, copy));
139 /* move recursively all predecessors */
140 for (j = get_irn_arity(node) - 1; j >= 0; --j) {
141 set_irn_n(copy, j, copy_to(get_irn_n(node, j), src_block, i));
142 DB((dbg, LEVEL_2, "-- pred %d is %+F\n", j, get_irn_n(copy, j)));
149 * Remove predecessors i and j (i < j) from a node and
150 * add an additional predecessor new_pred.
152 * @param node the node whose inputs are changed
153 * @param i the first index to remove
154 * @param j the second index to remove
155 * @param new_pred a node that is added as a new input to node
157 static void rewire(ir_node* node, int i, int j, ir_node* new_pred)
159 int arity = get_irn_arity(node);
164 NEW_ARR_A(ir_node *, ins, arity - 1);
167 for (k = 0; k < i; ++k) ins[l++] = get_irn_n(node, k);
168 for (++k; k < j; ++k) ins[l++] = get_irn_n(node, k);
169 for (++k; k < arity; ++k) ins[l++] = get_irn_n(node, k);
171 assert(l == arity - 1);
172 set_irn_in(node, l, ins);
177 * Remove the j-th predecessor from the i-th predecessor of block and add it to block
179 static void split_block(ir_node* block, int i, int j)
181 ir_node *pred_block = get_nodes_block(get_Block_cfgpred(block, i));
182 int arity = get_Block_n_cfgpreds(block);
183 ir_node **ins = ALLOCAN(ir_node*, arity + 1);
190 DB((dbg, LEVEL_1, "Splitting predecessor %d of predecessor %d of %+F\n", j, i, block));
192 for (phi = get_Block_phis(block); phi != NULL; phi = get_Phi_next(phi)) {
193 ir_node* copy = copy_to(get_irn_n(phi, i), pred_block, j);
195 for (k = 0; k < i; ++k) ins[k] = get_irn_n(phi, k);
197 for (; k < arity; ++k) ins[k] = get_irn_n(phi, k);
198 ins[k++] = get_irn_n(phi, i);
199 set_irn_in(phi, k, ins);
202 for (k = 0; k < i; ++k) ins[k] = get_Block_cfgpred(block, k);
203 ins[k++] = get_irn_n(pred_block, j);
204 for (; k < arity; ++k) ins[k] = get_Block_cfgpred(block, k);
205 ins[k++] = get_Block_cfgpred(block, i);
206 set_irn_in(block, k, ins);
208 new_pred_arity = get_irn_arity(pred_block) - 1;
209 pred_ins = ALLOCAN(ir_node*, new_pred_arity);
211 for (phi = get_Block_phis(pred_block); phi != NULL; phi = next) {
212 next = get_Phi_next(phi);
213 for (k = 0; k != j; ++k) pred_ins[k] = get_irn_n(phi, k);
214 for (; k != new_pred_arity; ++k) pred_ins[k] = get_irn_n(phi, k + 1);
216 exchange(phi, pred_ins[0]);
218 set_irn_in(phi, k, pred_ins);
222 for (k = 0; k != j; ++k) pred_ins[k] = get_irn_n(pred_block, k);
223 for (; k != new_pred_arity; ++k) pred_ins[k] = get_irn_n(pred_block, k + 1);
225 exchange(pred_block, get_nodes_block(pred_ins[0]));
227 set_irn_in(pred_block, k, pred_ins);
232 static void prepare_path(ir_node* block, int i, const ir_node* dependency)
234 ir_node* pred = get_nodes_block(get_Block_cfgpred(block, i));
238 DB((dbg, LEVEL_1, "Preparing predecessor %d of %+F\n", i, block));
240 pred_arity = get_irn_arity(pred);
241 for (j = 0; j < pred_arity; ++j) {
242 ir_node* pred_pred = get_nodes_block(get_irn_n(pred, j));
244 if (pred_pred != dependency && is_cdep_on(pred_pred, dependency)) {
245 prepare_path(pred, j, dependency);
246 split_block(block, i, j);
253 * Block walker: Search for diamonds and do the if conversion.
255 static void if_conv_walker(ir_node *block, void *ctx)
257 walker_env *env = (walker_env*)ctx;
261 /* Bail out, if there are no Phis at all */
262 if (get_Block_phis(block) == NULL) return;
265 arity = get_irn_arity(block);
266 for (i = 0; i < arity; ++i) {
270 pred0 = get_Block_cfgpred_block(block, i);
271 if (pred0 == block) continue;
273 for (cdep = find_cdep(pred0); cdep != NULL; cdep = get_cdep_next(cdep)) {
274 const ir_node* dependency = get_cdep_node(cdep);
275 ir_node* projx0 = walk_to_projx(pred0, dependency);
279 if (projx0 == NULL) continue;
281 cond = get_Proj_pred(projx0);
285 for (j = i + 1; j < arity; ++j) {
296 pred1 = get_Block_cfgpred_block(block, j);
297 if (pred1 == block) continue;
299 if (!is_cdep_on(pred1, dependency)) continue;
301 projx1 = walk_to_projx(pred1, dependency);
303 if (projx1 == NULL) continue;
305 sel = get_Cond_selector(cond);
306 phi = get_Block_phis(block);
308 negated = get_Proj_proj(projx0) == pn_Cond_false;
309 for (p = phi; p != NULL; p = get_Phi_next(p)) {
313 mux_true = get_Phi_pred(p, j);
314 mux_false = get_Phi_pred(p, i);
316 mux_true = get_Phi_pred(p, i);
317 mux_false = get_Phi_pred(p, j);
319 if (mux_true == mux_false)
321 ir_mode *mode = get_irn_mode(mux_true);
323 || !env->allow_ifconv(sel, mux_false, mux_true)) {
331 DB((dbg, LEVEL_1, "Found Cond %+F with proj %+F and %+F\n",
335 /* remove critical edges */
337 prepare_path(block, i, dependency);
338 prepare_path(block, j, dependency);
339 arity = get_irn_arity(block);
341 mux_block = get_nodes_block(cond);
342 cond_dbg = get_irn_dbg_info(cond);
343 do { /* generate Mux nodes in mux_block for Phis in block */
344 ir_node* val_i = get_irn_n(phi, i);
345 ir_node* val_j = get_irn_n(phi, j);
349 if (val_i == val_j) {
351 DB((dbg, LEVEL_2, "Generating no Mux, because both values are equal\n"));
355 /* Something is very fishy if two predecessors of a PhiM point into
356 * one block, but not at the same memory node
358 assert(get_irn_mode(phi) != mode_M);
367 mux = new_rd_Mux(cond_dbg, mux_block, sel, f, t, get_irn_mode(phi));
368 DB((dbg, LEVEL_2, "Generating %+F for %+F\n", mux, phi));
371 next_phi = get_Phi_next(phi);
376 rewire(phi, i, j, mux);
379 } while (phi != NULL);
381 /* move mux operands into mux_block */
382 exchange(get_nodes_block(get_Block_cfgpred(block, i)), mux_block);
383 exchange(get_nodes_block(get_Block_cfgpred(block, j)), mux_block);
387 DB((dbg, LEVEL_1, "Welding block %+F to %+F\n", block, mux_block));
388 mark = get_Block_mark(mux_block) | get_Block_mark(block);
389 /* mark both block just to be sure, should be enough to mark mux_block */
390 set_Block_mark(mux_block, mark);
391 exchange(block, mux_block);
394 rewire(block, i, j, new_r_Jmp(mux_block));
403 * Block walker: clear block marks and Phi lists.
405 static void init_block_link(ir_node *block, void *env)
408 set_Block_mark(block, 0);
409 set_Block_phis(block, NULL);
414 * Daisy-chain all Phis in a block.
415 * If a non-movable node is encountered set the has_pinned flag in its block.
417 static void collect_phis(ir_node *node, void *env)
422 ir_node *block = get_nodes_block(node);
424 add_Block_phi(block, node);
426 if (!is_Block(node) && get_irn_pinned(node) == op_pin_state_pinned) {
428 * Ignore control flow nodes (except Raise), these will be removed.
430 if (!is_cfop(node) && !is_Raise(node)) {
431 ir_node *block = get_nodes_block(node);
433 DB((dbg, LEVEL_2, "Node %+F in block %+F is unmovable\n", node, block));
434 set_Block_mark(block, 1);
440 void opt_if_conv(ir_graph *irg)
443 const backend_params *be_params = be_get_backend_param();
445 assure_irg_properties(irg,
446 IR_GRAPH_PROPERTY_NO_CRITICAL_EDGES
447 | IR_GRAPH_PROPERTY_NO_UNREACHABLE_CODE
448 | IR_GRAPH_PROPERTY_NO_BADS
449 | IR_GRAPH_PROPERTY_ONE_RETURN);
451 /* get the parameters */
452 env.allow_ifconv = be_params->allow_ifconv;
455 FIRM_DBG_REGISTER(dbg, "firm.opt.ifconv");
457 DB((dbg, LEVEL_1, "Running if-conversion on %+F\n", irg));
461 ir_reserve_resources(irg, IR_RESOURCE_BLOCK_MARK | IR_RESOURCE_PHI_LIST);
463 irg_block_walk_graph(irg, init_block_link, NULL, NULL);
464 irg_walk_graph(irg, collect_phis, NULL, NULL);
465 irg_block_walk_graph(irg, NULL, if_conv_walker, &env);
467 ir_free_resources(irg, IR_RESOURCE_BLOCK_MARK | IR_RESOURCE_PHI_LIST);
470 local_optimize_graph(irg);
475 confirm_irg_properties(irg,
476 IR_GRAPH_PROPERTY_NO_CRITICAL_EDGES
477 | IR_GRAPH_PROPERTY_ONE_RETURN);
480 ir_graph_pass_t *opt_if_conv_pass(const char *name)
482 return def_graph_pass(name ? name : "ifconv", opt_if_conv);