2 * Copyright (C) 1995-2007 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 Partial condition evaluation
24 * @author Christoph Mallon, Matthias Braun
31 #include "iroptimize.h"
43 #include "iredges_t.h"
48 DEBUG_ONLY(static firm_dbg_module_t *dbg);
51 * Add the new predecessor x to node node, which is either a Block or a Phi
53 static void add_pred(ir_node* node, ir_node* x)
59 assert(is_Block(node) || is_Phi(node));
61 n = get_irn_arity(node);
62 NEW_ARR_A(ir_node*, ins, n + 1);
63 for (i = 0; i < n; i++)
64 ins[i] = get_irn_n(node, i);
66 set_irn_in(node, n + 1, ins);
69 static ir_node *search_def_and_create_phis(ir_node *block, ir_mode *mode)
77 // This is needed because we create bads sometimes
81 // already processed this block?
82 if(irn_visited(block)) {
83 ir_node *value = (ir_node*) get_irn_link(block);
87 // blocks with only 1 pred need no phi
88 n_cfgpreds = get_Block_n_cfgpreds(block);
90 ir_node *pred_block = get_Block_cfgpred_block(block, 0);
91 ir_node *value = search_def_and_create_phis(pred_block, mode);
93 set_irn_link(block, value);
94 mark_irn_visited(block);
99 NEW_ARR_A(ir_node*, in, n_cfgpreds);
100 for(i = 0; i < n_cfgpreds; ++i)
101 in[i] = new_Unknown(mode);
103 irg = get_irn_irg(block);
104 phi = new_r_Phi(irg, block, n_cfgpreds, in, mode);
105 set_irn_link(block, phi);
106 mark_irn_visited(block);
109 for(i = 0; i < n_cfgpreds; ++i) {
110 ir_node *pred_block = get_Block_cfgpred_block(block, i);
111 ir_node *pred_val = search_def_and_create_phis(pred_block, mode);
113 set_irn_n(phi, i, pred_val);
120 * Given a set of values this function constructs SSA-form for the users of the
121 * first value (the users are determined through the out-edges of the value).
122 * Uses the irn_visited flags. Works without using the dominance tree.
124 static void construct_ssa(ir_node * const *blocks, ir_node * const *vals, int n_vals)
129 const ir_edge_t *edge;
130 const ir_edge_t *next;
135 irg = get_irn_irg(vals[0]);
136 inc_irg_visited(irg);
138 mode = get_irn_mode(vals[0]);
139 for(i = 0; i < n_vals; ++i) {
140 ir_node *value = vals[i];
141 ir_node *value_block = blocks[i];
143 assert(get_irn_mode(value) == mode);
145 set_irn_link(value_block, value);
146 mark_irn_visited(value_block);
149 // Only fix the users of the first, i.e. the original node
152 // this can happen when fixing phi preds, we mustn't fix the users
153 if(get_nodes_block(value) != blocks[0]) return;
155 foreach_out_edge_safe(value, edge, next) {
156 ir_node *user = get_edge_src_irn(edge);
157 int j = get_edge_src_pos(edge);
158 ir_node *user_block = get_nodes_block(user);
162 if(get_irn_op(user) == op_End)
165 DB((dbg, LEVEL_3, ">>> Fixing user %+F (pred %d == %+F)\n", user, j, get_irn_n(user, j)));
168 ir_node *pred_block = get_Block_cfgpred_block(user_block, j);
169 newval = search_def_and_create_phis(pred_block, mode);
171 newval = search_def_and_create_phis(user_block, mode);
174 // don't fix newly created phis from the SSA construction
175 if (newval != user) {
176 DB((dbg, LEVEL_4, ">>>> Setting input %d of %+F to %+F\n", j, user, newval));
177 set_irn_n(user, j, newval);
182 static void split_critical_edge(ir_node *block, int pos) {
183 ir_graph *irg = get_irn_irg(block);
188 in[0] = get_Block_cfgpred(block, pos);
189 new_block = new_r_Block(irg, 1, in);
190 new_jmp = new_r_Jmp(irg, new_block);
191 set_Block_cfgpred(block, pos, new_jmp);
194 typedef struct _condeval_env_t {
198 unsigned long visited_nr;
200 ir_node *cnst_pred; /**< the block before the constant */
201 int cnst_pos; /**< the pos to the constant block (needed to kill that edge later) */
204 static void copy_and_fix(ir_node *block, ir_node *copy_block, int j, const condeval_env_t *env) {
205 const ir_edge_t *edge;
207 /* Look at all nodes in the cond_block and copy them into pred */
208 foreach_out_edge(block, edge) {
209 ir_node *node = get_edge_src_irn(edge);
211 ir_mode *mode = get_irn_mode(node);
213 /* ignore control flow */
214 if (mode == mode_X || is_Cond(node))
216 /* we may not copy mode_b nodes, because this could produce phi with mode_bs which can't
217 be handled in all backends. Instead we duplicate the node and move it to it's users */
218 if (mode == mode_b) {
219 const ir_edge_t *edge, *next;
223 assert(is_Proj(node));
225 pred = get_Proj_pred(node);
226 pn = get_Proj_proj(node);
228 foreach_out_edge_safe(node, edge, next) {
230 ir_node *user = get_edge_src_irn(edge);
231 int pos = get_edge_src_pos(edge);
232 ir_node *user_block = get_nodes_block(user);
234 if(user_block == block)
237 cmp_copy = exact_copy(pred);
238 set_nodes_block(cmp_copy, user_block);
239 copy = new_r_Proj(current_ir_graph, user_block, cmp_copy, mode_b, pn);
240 set_irn_n(user, pos, copy);
245 /* we can evaluate Phis right now, all other nodes get copied */
247 copy = get_Phi_pred(node, j);
249 copy = exact_copy(node);
250 set_nodes_block(copy, copy_block);
253 set_irn_link(node, copy);
254 set_irn_visited(node, env->visited_nr);
256 /* we might hit values in blocks that have already been processed by a
257 * recursive find_phi_with_const call */
258 assert(get_irn_visited(copy) <= env->visited_nr);
259 if(get_irn_visited(copy) >= env->visited_nr) {
260 ir_node *prev_copy = get_irn_link(copy);
261 if(prev_copy != NULL)
262 set_irn_link(node, prev_copy);
266 /* fix data-flow (and reconstruct SSA if needed) */
267 foreach_out_edge(block, edge) {
270 ir_node *node = get_edge_src_irn(edge);
271 ir_mode *mode = get_irn_mode(node);
273 if (mode == mode_X || is_Cond(node))
278 DB((dbg, LEVEL_2, ">> Fixing users of %+F\n", node));
282 blocks[1] = copy_block;
283 vals[1] = get_irn_link(node);
284 construct_ssa(blocks, vals, 2);
288 static int eval_cmp(pn_Cmp pnc, tarval *tv1, tarval *tv2) {
289 pn_Cmp cmp_result = tarval_cmp(tv1, tv2);
291 // does the compare evaluate to true?
292 if(cmp_result == pn_Cmp_False)
294 if((cmp_result & pnc) != cmp_result)
300 static ir_node *find_phi_with_const(ir_node *jump, ir_node *value, condeval_env_t *env) {
301 ir_node *block = get_nodes_block(jump);
303 if(irn_visited(value))
305 mark_irn_visited(value);
307 if(is_Const(value)) {
308 tarval *tv_const = get_Const_tarval(env->cnst);
309 tarval *tv = get_Const_tarval(value);
311 if(!eval_cmp(env->pnc, tv, tv_const))
316 "> Found condition evaluation candidate %+F->%+F\n",
317 env->true_block, block
320 // adjust true_block to point directly towards our jump
321 add_pred(env->true_block, jump);
323 split_critical_edge(env->true_block, 0);
325 // we need a bigger visited nr when going back
334 // the phi has to be in the same block as the jump
335 if(get_nodes_block(value) != block)
338 arity = get_irn_arity(value);
339 for(i = 0; i < arity; ++i) {
341 ir_node *phi_pred = get_Phi_pred(value, i);
342 ir_node *cfgpred = get_Block_cfgpred(block, i);
344 copy_block = find_phi_with_const(cfgpred, phi_pred, env);
345 if(copy_block == NULL)
348 /* copy duplicated nodes in copy_block and fix SSA */
349 copy_and_fix(block, copy_block, i, env);
351 if(copy_block == get_nodes_block(cfgpred)) {
352 env->cnst_pred = block;
356 // return now as we can't process more possibilities in 1 run
366 * Block-walker: searches for the following construct
368 * Const or Phi with constants
378 static void cond_eval(ir_node* block, void* data)
382 ir_graph *irg = current_ir_graph;
393 if(get_Block_n_cfgpreds(block) != 1)
396 projx = get_Block_cfgpred(block, 0);
399 assert(get_irn_mode(projx) == mode_X);
401 cond = get_Proj_pred(projx);
405 pred = get_Cond_selector(cond);
406 // TODO handle switches
407 if (get_irn_mode(pred) != mode_b)
411 pnc = get_Proj_proj(pred);
413 cmp = get_Proj_pred(pred);
416 left = get_Cmp_left(cmp);
417 right = get_Cmp_right(cmp);
418 assert(get_irn_mode(left) == get_irn_mode(right));
420 /* we assume that the cond_block is the true case */
421 if (get_Proj_proj(projx) == pn_Cond_false) {
422 pnc = get_negated_pnc(pnc, get_irn_mode(left));
425 /* we assume that the constant is on the right side, swap left/right
432 pnc = get_inversed_pnc(pnc);
438 cond_block = get_nodes_block(cond);
440 // special case: comparing a constant with a constant
442 tarval *tv1 = get_Const_tarval(left);
443 tarval *tv2 = get_Const_tarval(right);
445 if(eval_cmp(pnc, tv1, tv2)) {
446 pred = new_r_Jmp(irg, cond_block);
450 set_Block_cfgpred(block, 0, pred);
452 set_irg_doms_inconsistent(irg);
453 set_irg_extblk_inconsistent(irg);
454 set_irg_loopinfo_inconsistent(irg);
456 if(get_nodes_block(left) != cond_block)
459 // (recursively) look if a pred of a phi is a constant
460 env.true_block = block;
463 inc_irg_visited(current_ir_graph);
464 env.visited_nr = get_irg_visited(irg);
466 copy_block = find_phi_with_const(projx, left, &env);
468 if(copy_block != NULL) {
469 /* we have to remove the edge towards the pred as the pred now
470 * jumps into the true_block. We also have to shorten phis
471 * in our block because of this */
472 const ir_edge_t *edge, *next;
473 ir_node* bad = new_Bad();
474 size_t cnst_pos = env.cnst_pos;
477 foreach_out_edge_safe(env.cnst_pred, edge, next) {
478 ir_node *node = get_edge_src_irn(edge);
481 set_Phi_pred(node, cnst_pos, bad);
484 set_Block_cfgpred(env.cnst_pred, cnst_pos, bad);
486 /* the graph is changed now */
492 void opt_cond_eval(ir_graph* irg)
496 FIRM_DBG_REGISTER(dbg, "firm.opt.condeval");
498 DB((dbg, LEVEL_1, "===> Performing condition evaluation on %+F\n", irg));
500 remove_critical_cf_edges(irg);
501 normalize_proj_nodes(irg);
504 set_using_irn_link(irg);
505 set_using_visited(irg);
510 irg_block_walk_graph(irg, cond_eval, NULL, &rerun);
515 /* control flow changed, some blocks may become dead */
516 set_irg_outs_inconsistent(irg);
517 set_irg_doms_inconsistent(irg);
518 set_irg_extblk_inconsistent(irg);
519 set_irg_loopinfo_inconsistent(irg);
522 clear_using_visited(irg);
523 clear_using_irn_link(irg);