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
21 * @file ir/opt/ifconv.c
22 * @brief If conversion
23 * @author Christoph Mallon
32 #include "iroptimize.h"
49 DEBUG_ONLY(static firm_dbg_module_t *dbg);
51 /** allow every Psi to be created. */
52 static int default_allow_ifconv(ir_node *sel, ir_node* phi_list, int i, int j)
64 static const ir_settings_if_conv_t default_info = {
65 0, /* doesn't matter for Psi */
70 * Returns non-zero if a Block can be emptied.
72 static int can_empty_block(ir_node *block) {
73 return get_Block_mark(block) == 0;
77 static ir_node* walk_to_projx(ir_node* start, const ir_node* dependency)
82 /* No need to find the conditional block if this block cannot be emptied and
83 * therefore not moved */
84 if (!can_empty_block(start)) return NULL;
86 arity = get_irn_arity(start);
87 for (i = 0; i < arity; ++i) {
88 ir_node* pred = get_irn_n(start, i);
89 ir_node* pred_block = get_nodes_block(pred);
91 if (pred_block == dependency) {
93 assert(get_irn_mode(pred) == mode_X);
100 assert(get_irn_mode(pred) == mode_X);
104 if (is_cdep_on(pred_block, dependency)) {
105 return walk_to_projx(pred_block, dependency);
113 * Copies the DAG starting at node to the ith predecessor block of src_block
114 * -if the node isn't in the src_block, this is a nop and the node is returned
115 * -if the node is a phi in the src_block, the ith predecessor of the phi is
117 * otherwise returns the copy of the passed node
119 static ir_node* copy_to(ir_node* node, ir_node* src_block, int i)
126 if (get_nodes_block(node) != src_block) return node;
127 if (get_irn_op(node) == op_Phi) return get_irn_n(node, i);
129 copy = exact_copy(node);
130 dst_block = get_nodes_block(get_irn_n(src_block, i));
131 set_nodes_block(copy, dst_block);
133 DB((dbg, LEVEL_1, "Copying node %+F to block %+F, copy is %+F\n",
134 node, dst_block, copy));
136 arity = get_irn_arity(node);
137 for (j = 0; j < arity; ++j) {
138 set_irn_n(copy, j, copy_to(get_irn_n(node, j), src_block, i));
139 DB((dbg, LEVEL_2, "-- pred %d is %+F\n", j, get_irn_n(copy, j)));
146 * Remove predecessors i and j from node and add predecessor new_pred
148 static void rewire(ir_node* node, int i, int j, ir_node* new_pred)
150 int arity = get_irn_arity(node);
155 NEW_ARR_A(ir_node *, ins, arity - 1);
158 for (k = 0; k < i; ++k) ins[l++] = get_irn_n(node, k);
159 for (++k; k < j; ++k) ins[l++] = get_irn_n(node, k);
160 for (++k; k < arity; ++k) ins[l++] = get_irn_n(node, k);
162 assert(l == arity - 1);
163 set_irn_in(node, l, ins);
168 * Remove the jth predecessors from the ith predecessor of block and add it to block
170 static void split_block(ir_node* block, int i, int j)
172 ir_node* pred_block = get_nodes_block(get_irn_n(block, i));
173 int arity = get_irn_arity(block);
180 DB((dbg, LEVEL_1, "Splitting predecessor %d of predecessor %d of %+F\n", j, i, block));
182 NEW_ARR_A(ir_node*, ins, arity + 1);
184 for (phi = get_Block_phis(block); phi != NULL; phi = get_Phi_next(phi)) {
185 ir_node* copy = copy_to(get_irn_n(phi, i), pred_block, j);
187 for (k = 0; k < i; ++k) ins[k] = get_irn_n(phi, k);
189 for (; k < arity; ++k) ins[k] = get_irn_n(phi, k);
190 ins[k] = get_irn_n(phi, i);
192 set_irn_in(phi, arity + 1, ins);
195 for (k = 0; k < i; ++k) ins[k] = get_irn_n(block, k);
196 ins[k++] = get_irn_n(pred_block, j);
197 for (; k < arity; ++k) ins[k] = get_irn_n(block, k);
198 ins[k] = get_irn_n(block, i);
200 set_irn_in(block, arity + 1, ins);
202 new_pred_arity = get_irn_arity(pred_block) - 1;
203 NEW_ARR_A(ir_node*, pred_ins, new_pred_arity);
205 for (phi = get_Block_phis(pred_block); phi != NULL; phi = get_Phi_next(phi)) {
206 for (k = 0; k < j; ++k) pred_ins[k] = get_irn_n(phi, k);
207 for (; k < new_pred_arity; ++k) pred_ins[k] = get_irn_n(phi, k + 1);
208 assert(k == new_pred_arity);
209 if (new_pred_arity > 1) {
210 set_irn_in(phi, new_pred_arity, pred_ins);
212 exchange(phi, pred_ins[0]);
216 for (k = 0; k < j; ++k) pred_ins[k] = get_irn_n(pred_block, k);
217 for (; k < new_pred_arity; ++k) pred_ins[k] = get_irn_n(pred_block, k + 1);
218 assert(k == new_pred_arity);
219 if (new_pred_arity > 1) {
220 set_irn_in(pred_block, new_pred_arity, pred_ins);
222 exchange(pred_block, get_nodes_block(pred_ins[0]));
227 static void prepare_path(ir_node* block, int i, const ir_node* dependency)
229 ir_node* pred = get_nodes_block(get_irn_n(block, i));
233 DB((dbg, LEVEL_1, "Preparing predecessor %d of %+F\n", i, block));
235 pred_arity = get_irn_arity(pred);
236 for (j = 0; j < pred_arity; ++j) {
237 ir_node* pred_pred = get_nodes_block(get_irn_n(pred, j));
239 if (is_cdep_on(pred_pred, dependency)) {
240 prepare_path(pred, j, dependency);
241 split_block(block, i, j);
248 static void if_conv_walker(ir_node* block, void* env)
250 ir_settings_if_conv_t* opt_info = env;
254 /* Bail out, if there are no Phis at all */
255 if (get_Block_phis(block) == NULL) return;
258 arity = get_irn_arity(block);
259 for (i = 0; i < arity; ++i) {
263 pred0 = get_Block_cfgpred_block(block, i);
264 for (cdep = find_cdep(pred0); cdep != NULL; cdep = cdep->next) {
265 const ir_node* dependency = cdep->node;
266 ir_node* projx0 = walk_to_projx(pred0, dependency);
270 if (projx0 == NULL) continue;
272 cond = get_Proj_pred(projx0);
273 if (get_irn_op(cond) != op_Cond) continue;
275 /* We only handle boolean decisions, no switches */
276 if (get_irn_mode(get_Cond_selector(cond)) != mode_b) continue;
278 for (j = i + 1; j < arity; ++j) {
286 pred1 = get_Block_cfgpred_block(block, j);
288 if (!is_cdep_on(pred1, dependency)) continue;
290 projx1 = walk_to_projx(pred1, dependency);
292 if (projx1 == NULL) continue;
294 phi = get_Block_phis(block);
295 if (!opt_info->allow_ifconv(get_Cond_selector(cond), phi, i, j)) continue;
297 DB((dbg, LEVEL_1, "Found Cond %+F with proj %+F and %+F\n",
301 prepare_path(block, i, dependency);
302 prepare_path(block, j, dependency);
303 arity = get_irn_arity(block);
305 conds[0] = get_Cond_selector(cond);
307 psi_block = get_nodes_block(cond);
308 cond_dbg = get_irn_dbg_info(cond);
310 ir_node* val_i = get_irn_n(phi, i);
311 ir_node* val_j = get_irn_n(phi, j);
315 if (val_i == val_j) {
317 DB((dbg, LEVEL_2, "Generating no psi, because both values are equal\n"));
321 /* Something is very fishy if two predecessors of a PhiM point into
322 * one block, but not at the same memory node
324 assert(get_irn_mode(phi) != mode_M);
325 if (get_Proj_proj(projx0) == pn_Cond_true) {
333 psi = new_rd_Psi(cond_dbg, current_ir_graph, psi_block, 1, conds, vals, get_irn_mode(phi));
334 DB((dbg, LEVEL_2, "Generating %+F for %+F\n", psi, phi));
337 next_phi = get_Phi_next(phi);
342 rewire(phi, i, j, psi);
346 } while (phi != NULL);
348 exchange(get_nodes_block(get_irn_n(block, i)), psi_block);
349 exchange(get_nodes_block(get_irn_n(block, j)), psi_block);
354 DB((dbg, LEVEL_1, "Welding block %+F and %+F\n", block, psi_block));
355 /* copy the block-info from the Psi-block to the block before merging */
357 mark = get_Block_mark(psi_block) | get_Block_mark(block);
358 set_Block_mark(block, mark);
359 set_Block_phis(block, get_Block_phis(psi_block));
361 set_irn_in(block, get_irn_arity(psi_block), get_irn_in(psi_block) + 1);
362 exchange_cdep(psi_block, block);
363 exchange(psi_block, block);
365 DB((dbg, LEVEL_1, "Welding block %+F to %+F\n", block, psi_block));
366 mark = get_Block_mark(psi_block) | get_Block_mark(block);
367 /* mark both block just to be sure, should be enough to mark psi_block */
368 set_Block_mark(psi_block, mark);
369 exchange(block, psi_block);
373 rewire(block, i, j, new_r_Jmp(current_ir_graph, psi_block));
382 * Block walker: clear block mark and Phi list
384 static void init_block_link(ir_node *block, void *env)
387 set_Block_mark(block, 0);
388 set_Block_phis(block, NULL);
393 * Daisy-chain all phis in a block
394 * If a non-movable node is encountered set the has_pinned flag in its block.
396 static void collect_phis(ir_node *node, void *env) {
400 ir_node *block = get_nodes_block(node);
402 add_Block_phi(block, node);
404 if (is_no_Block(node) && get_irn_pinned(node) == op_pin_state_pinned) {
406 * Ignore control flow nodes, these will be removed.
407 * This ignores Raise. That is surely bad. FIXME.
409 if (!is_cfop(node)) {
410 ir_node *block = get_nodes_block(node);
412 DB((dbg, LEVEL_2, "Node %+F in block %+F is unmovable\n", node, block));
413 set_Block_mark(block, 1);
419 static void optimise_psis_0(ir_node* psi, void* env)
426 if (!is_Psi(psi)) return;
428 t = get_Psi_val(psi, 0);
429 f = get_Psi_default(psi);
431 DB((dbg, LEVEL_3, "Simplify %+F T=%+F F=%+F\n", psi, t, f));
434 DB((dbg, LEVEL_3, "Replace Psi with unknown operand by %+F\n", f));
439 DB((dbg, LEVEL_3, "Replace Psi with unknown operand by %+F\n", t));
445 ir_graph* irg = current_ir_graph;
446 ir_node* block = get_nodes_block(psi);
447 ir_mode* mode = get_irn_mode(psi);
448 ir_node* c0 = get_Psi_cond(psi, 0);
449 ir_node* c1 = get_Psi_cond(t, 0);
450 ir_node* t1 = get_Psi_val(t, 0);
451 ir_node* f1 = get_Psi_default(t);
453 /* Psi(c0, Psi(c1, x, y), y) -> typical if (c0 && c1) x else y */
454 ir_node* and_ = new_r_And(irg, block, c0, c1, mode_b);
455 ir_node* vals[2] = { t1, f1 };
456 ir_node* new_psi = new_r_Psi(irg, block, 1, &and_, vals, mode);
457 exchange(psi, new_psi);
458 } else if (f == t1) {
459 /* Psi(c0, Psi(c1, x, y), x) */
460 ir_node* not_c1 = new_r_Not(irg, block, c1, mode_b);
461 ir_node* and_ = new_r_And(irg, block, c0, not_c1, mode_b);
462 ir_node* vals[2] = { f1, t1 };
463 ir_node* new_psi = new_r_Psi(irg, block, 1, &and_, vals, mode);
464 exchange(psi, new_psi);
466 } else if (is_Psi(f)) {
467 ir_graph* irg = current_ir_graph;
468 ir_node* block = get_nodes_block(psi);
469 ir_mode* mode = get_irn_mode(psi);
470 ir_node* c0 = get_Psi_cond(psi, 0);
471 ir_node* c1 = get_Psi_cond(f, 0);
472 ir_node* t1 = get_Psi_val(f, 0);
473 ir_node* f1 = get_Psi_default(f);
475 /* Psi(c0, x, Psi(c1, x, y)) -> typical if (c0 || c1) x else y */
476 ir_node* or_ = new_r_Or(irg, block, c0, c1, mode_b);
477 ir_node* vals[2] = { t1, f1 };
478 ir_node* new_psi = new_r_Psi(irg, block, 1, &or_, vals, mode);
479 exchange(psi, new_psi);
480 } else if (t == f1) {
481 /* Psi(c0, x, Psi(c1, y, x)) */
482 ir_node* not_c1 = new_r_Not(irg, block, c1, mode_b);
483 ir_node* or_ = new_r_Or(irg, block, c0, not_c1, mode_b);
484 ir_node* vals[2] = { f1, t1 };
485 ir_node* new_psi = new_r_Psi(irg, block, 1, &or_, vals, mode);
486 exchange(psi, new_psi);
492 static void optimise_psis_1(ir_node* psi, void* env)
500 if (!is_Psi(psi)) return;
502 t = get_Psi_val(psi, 0);
503 f = get_Psi_default(psi);
505 DB((dbg, LEVEL_3, "Simplify %+F T=%+F F=%+F\n", psi, t, f));
507 mode = get_irn_mode(psi);
509 if (is_Const(t) && is_Const(f) && (mode_is_int(mode))) {
510 ir_node* block = get_nodes_block(psi);
511 ir_node* c = get_Psi_cond(psi, 0);
512 tarval* tv_t = get_Const_tarval(t);
513 tarval* tv_f = get_Const_tarval(f);
514 if (tarval_is_one(tv_t) && tarval_is_null(tv_f)) {
515 ir_node* conv = new_r_Conv(current_ir_graph, block, c, mode);
517 } else if (tarval_is_null(tv_t) && tarval_is_one(tv_f)) {
518 ir_node* not_ = new_r_Not(current_ir_graph, block, c, mode_b);
519 ir_node* conv = new_r_Conv(current_ir_graph, block, not_, mode);
526 void opt_if_conv(ir_graph *irg, const ir_settings_if_conv_t *params)
528 ir_settings_if_conv_t p;
530 /* get the parameters */
531 p = (params != NULL ? *params : default_info);
533 FIRM_DBG_REGISTER(dbg, "firm.opt.ifconv");
535 DB((dbg, LEVEL_1, "Running if-conversion on %+F\n", irg));
537 normalize_one_return(irg);
538 remove_critical_cf_edges(irg);
543 set_using_block_mark(irg);
545 irg_block_walk_graph(irg, init_block_link, NULL, NULL);
546 irg_walk_graph(irg, collect_phis, NULL, NULL);
547 irg_block_walk_graph(irg, NULL, if_conv_walker, &p);
549 clear_using_block_mark(irg);
551 local_optimize_graph(irg);
553 irg_walk_graph(irg, NULL, optimise_psis_0, NULL);
555 irg_walk_graph(irg, NULL, optimise_psis_1, NULL);
558 /* TODO: graph might be changed, handle more graceful */
559 set_irg_outs_inconsistent(irg);
560 set_irg_extblk_inconsistent(irg);
561 set_irg_loopinfo_inconsistent(irg);