2 * Copyright (C) 1995-2011 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 boolean condition/control flow optimizations
23 * @author Matthias Braun, Christoph Mallon, Michael Beck
24 * @version $Id: cfopt.c 22579 2008-10-07 14:54:04Z beck $
32 #include "../adt/array_t.h"
33 #include "iroptimize.h"
42 #include "opt_manage.h"
44 /** Describes a pair of relative conditions lo < hi, lo rel_lo x, hi rel_hi x */
45 typedef struct cond_pair {
46 ir_node *cmp_lo; /**< The lo compare node. */
47 ir_node *cmp_hi; /**< The hi compare node. */
48 ir_relation rel_lo; /**< The lo relation node. */
49 ir_relation rel_hi; /**< The hi relation node. */
50 ir_tarval *tv_lo; /**< The tarval of cmp_lo node. */
51 ir_tarval *tv_hi; /**< The tarval of cmp_hi node. */
52 ir_mode *lo_mode; /**< The mode of the cmp_lo operands. */
55 /** Environment for all walker in boolopt. */
57 int changed; /**< Set if the graph was changed. */
60 DEBUG_ONLY(static firm_dbg_module_t *dbg;)
63 * Check if tho given nodes, l and r, represent two compares with
64 * ... . If yes, return non-zero and fill the res struct.
66 static bool find_cond_pair(ir_node *const l, ir_node *const r, cond_pair *const res)
68 if (is_Cmp(l) && is_Cmp(r)) {
69 ir_node *const lol = get_Cmp_left(l);
70 ir_node *const lor = get_Cmp_right(l);
71 ir_node *const rol = get_Cmp_left(r);
72 ir_node *const ror = get_Cmp_right(r);
73 ir_relation const pnc_l = get_Cmp_relation(l);
74 ir_relation const pnc_r = get_Cmp_relation(r);
76 if (is_Const(lor) && is_Const_null(lor) &&
77 is_Const(ror) && is_Const_null(ror) &&
79 (pnc_l == ir_relation_less_greater || pnc_l == ir_relation_equal)) {
80 /* l == (lol !=|== NULL) && r == (rol !=|== NULL) */
85 res->tv_lo = get_Const_tarval(lor);
86 res->tv_hi = get_Const_tarval(ror);
87 res->lo_mode = get_irn_mode(lor);
92 if (lol == rol && lor != ror && is_Const(lor) && is_Const(ror)) {
93 /* l == (x CMP c_l), r == (x cmp c_r) */
94 ir_tarval *const tv_l = get_Const_tarval(lor);
95 ir_tarval *const tv_r = get_Const_tarval(ror);
96 ir_relation const rel = tarval_cmp(tv_l, tv_r);
98 res->lo_mode = get_irn_mode(lol);
100 if (rel == ir_relation_less) {
108 } else if (rel == ir_relation_greater) {
117 /* The constants shall be unequal but comparable.
118 * Local optimizations handle the equal case. */
128 * Handle (lo rel_lo x) AND (hi rel_hi x)
130 static ir_node *bool_and(cond_pair* const cpair, ir_node *dst_block)
132 ir_node *const cmp_lo = cpair->cmp_lo;
133 ir_node *const cmp_hi = cpair->cmp_hi;
134 ir_relation rel_lo = cpair->rel_lo;
135 ir_relation const rel_hi = cpair->rel_hi;
136 ir_tarval * tv_lo = cpair->tv_lo;
137 ir_tarval * tv_hi = cpair->tv_hi;
138 ir_mode * mode = cpair->lo_mode;
139 ir_graph * irg = get_irn_irg(cmp_lo);
141 if (rel_lo == ir_relation_equal && rel_hi == rel_lo &&
142 tarval_is_null(tv_lo) && tarval_is_null(tv_hi) &&
143 mode == get_tarval_mode(tv_hi)) {
144 /* p == NULL && q == NULL ==> (p&q) == NULL) */
145 ir_node *lol, *hil, *cmp, *c, *p;
147 if (mode_is_reference(mode)) {
148 mode = find_unsigned_mode(mode);
151 tv_lo = tarval_convert_to(tv_lo, mode);
152 if (tv_lo == tarval_bad)
155 if (mode_is_int(mode)) {
156 lol = get_Cmp_left(cmp_lo);
157 lol = new_r_Conv(dst_block, lol, mode);
158 hil = get_Cmp_left(cmp_hi);
159 hil = new_r_Conv(dst_block, hil, mode);
160 p = new_r_And(dst_block, lol, hil, mode);
161 c = new_r_Const(irg, tv_lo);
162 cmp = new_r_Cmp(dst_block, p, c, ir_relation_equal);
167 /* the following tests expect one common operand */
168 if (get_Cmp_left(cmp_lo) != get_Cmp_left(cmp_hi))
171 /* TODO: for now reject float modes */
172 if (! mode_is_int(mode))
175 /* Beware of NaN's, we can only check for (ordered) != here (which is Lg, not Ne) */
176 if ((rel_lo == ir_relation_less || rel_lo == ir_relation_less_equal || rel_lo == ir_relation_equal) &&
177 (rel_hi == ir_relation_equal || rel_hi == ir_relation_greater_equal || rel_hi == ir_relation_greater)) {
178 /* x <|<=|== lo && x ==|>=|> hi ==> false */
179 ir_node *const t = new_r_Const(irg, tarval_b_false);
181 } else if ((rel_lo == ir_relation_less || rel_lo == ir_relation_less_equal || rel_lo == ir_relation_equal) &&
182 (rel_hi == ir_relation_less || rel_hi == ir_relation_less_equal || rel_hi == ir_relation_less_greater)) {
183 /* x <|<=|== lo && x <|<=|!= hi ==> x <|<=|== lo */
185 } else if ((rel_lo == ir_relation_greater_equal || rel_lo == ir_relation_greater || rel_lo == ir_relation_less_greater) &&
186 (rel_hi == ir_relation_equal || rel_hi == ir_relation_greater_equal || rel_hi == ir_relation_greater)) {
187 /* x >=|>|!= lo && x ==|>=|> hi ==> x ==|>=|> hi */
189 } else if (tarval_is_one(tarval_sub(tv_hi, tv_lo, NULL))) { /* lo + 1 == hi */
190 if (rel_lo == ir_relation_greater_equal && rel_hi == ir_relation_less) {
191 /* x >= c && x < c + 1 ==> x == c */
192 ir_node *const p = new_r_Proj(cmp_lo, mode_b, ir_relation_equal);
194 } else if (rel_lo == ir_relation_greater) {
195 if (rel_hi == ir_relation_less_greater) {
196 /* x > c && x != c + 1 ==> x > c + 1 */
197 ir_node *const p = new_r_Proj(cmp_hi, mode_b, ir_relation_greater);
199 } else if (rel_hi == ir_relation_less) {
200 /* x > c && x < c + 1 ==> false */
201 ir_node *const t = new_r_Const(irg, tarval_b_false);
203 } else if (rel_hi == ir_relation_less_equal) {
204 /* x > c && x <= c + 1 ==> x != c + 1 */
205 ir_node *const p = new_r_Proj(cmp_hi, mode_b, ir_relation_equal);
208 } else if (rel_lo == ir_relation_less_greater && rel_hi == ir_relation_less) {
209 /* x != c && c < c + 1 ==> x < c */
210 ir_node *const p = new_r_Proj(cmp_lo, mode_b, ir_relation_less);
213 } else if ((rel_lo == ir_relation_greater || rel_lo == ir_relation_greater_equal) &&
214 (rel_hi == ir_relation_less || rel_lo == ir_relation_less_equal) &&
215 get_mode_arithmetic(mode) == irma_twos_complement) {
216 /* works for two-complements only */
217 /* x >|\= lo && x <|<= hi ==> (x - lo) <u|<=u (hi-lo) */
218 if (rel_lo == ir_relation_greater) {
219 /* must convert to >= */
220 ir_mode *mode = get_tarval_mode(tv_lo);
221 ir_tarval *n = tarval_add(tv_lo, get_mode_one(mode));
222 if (n != tarval_bad && tarval_cmp(n, tv_lo) == ir_relation_greater) {
225 rel_lo = ir_relation_greater_equal;
228 if (rel_lo == ir_relation_greater_equal) {
230 ir_node *const block = get_nodes_block(cmp_hi);
231 ir_node * x = get_Cmp_left(cmp_hi);
232 ir_mode * mode = get_irn_mode(x);
233 ir_node *sub, *cmp, *c, *subc;
235 if (mode_is_signed(mode)) {
236 /* convert to unsigned */
237 mode = find_unsigned_mode(mode);
240 x = new_r_Conv(block, x, mode);
241 tv_lo = tarval_convert_to(tv_lo, mode);
242 tv_hi = tarval_convert_to(tv_hi, mode);
243 if (tv_lo == tarval_bad || tv_hi == tarval_bad)
246 c = new_r_Const(irg, tv_lo);
247 sub = new_r_Sub(block, x, c, mode);
248 subc = new_r_Sub(block, new_r_Const(irg, tv_hi), c, mode);
249 cmp = new_r_Cmp(block, sub, subc, rel_hi);
257 * Handle (lo rel_lo x) OR (hi rel_hi x)
259 static ir_node *bool_or(cond_pair *const cpair, ir_node *dst_block)
261 ir_node *const cmp_lo = cpair->cmp_lo;
262 ir_node *const cmp_hi = cpair->cmp_hi;
263 ir_relation rel_lo = cpair->rel_lo;
264 ir_relation const rel_hi = cpair->rel_hi;
265 ir_tarval * tv_lo = cpair->tv_lo;
266 ir_tarval * tv_hi = cpair->tv_hi;
267 ir_mode * mode = cpair->lo_mode;
268 ir_graph * irg = get_irn_irg(cmp_lo);
270 if (rel_lo == ir_relation_less_greater && rel_hi == ir_relation_less_greater &&
271 tarval_is_null(tv_lo) && tarval_is_null(tv_hi) &&
272 mode == get_tarval_mode(tv_hi)) {
273 /* p != NULL || q != NULL ==> (p|q) != NULL) */
274 ir_node *lol, *hil, *cmp, *c, *p;
276 if (mode_is_reference(mode)) {
277 mode = find_unsigned_mode(mode);
280 tv_lo = tarval_convert_to(tv_lo, mode);
281 if (tv_lo == tarval_bad)
284 if (mode_is_int(mode)) {
285 lol = get_Cmp_left(cmp_lo);
286 lol = new_r_Conv(dst_block, lol, mode);
287 hil = get_Cmp_left(cmp_hi);
288 hil = new_r_Conv(dst_block, hil, mode);
289 p = new_r_Or(dst_block, lol, hil, mode);
290 c = new_r_Const(irg, tv_lo);
291 cmp = new_r_Cmp(dst_block, p, c, ir_relation_less_greater);
296 /* the following tests expect one common operand */
297 if (get_Cmp_left(cmp_lo) != get_Cmp_left(cmp_hi))
300 /* TODO: for now reject float modes */
301 if (! mode_is_int(mode))
304 /* Beware of NaN's, we can only check for (ordered) != here (which is Lg, not Ne) */
305 if ((rel_lo == ir_relation_greater_equal || rel_lo == ir_relation_greater || rel_lo == ir_relation_less_greater) &&
306 (rel_hi == ir_relation_less || rel_hi == ir_relation_less_equal || rel_hi == ir_relation_less_greater)) {
307 /* x >=|>|!= lo | x <|<=|!= hi ==> true */
308 ir_node *const t = new_r_Const(irg, tarval_b_true);
310 } else if ((rel_lo == ir_relation_less || rel_lo == ir_relation_less_equal || rel_lo == ir_relation_equal) &&
311 (rel_hi == ir_relation_less || rel_hi == ir_relation_less_equal || rel_hi == ir_relation_less_greater)) {
312 /* x <|<=|== lo || x <|<=|!= hi ==> x <|<=|!= hi */
314 } else if ((rel_lo == ir_relation_greater_equal || rel_lo == ir_relation_greater || rel_lo == ir_relation_less_greater) &&
315 (rel_hi == ir_relation_equal || rel_hi == ir_relation_greater_equal || rel_hi == ir_relation_greater)) {
316 /* x >=|>|!= lo || x ==|>=|> hi ==> x >=|>|!= lo */
318 } else if (tarval_is_one(tarval_sub(tv_hi, tv_lo, NULL))) { /* lo + 1 == hi */
319 if (rel_lo == ir_relation_less && rel_hi == ir_relation_greater_equal) {
320 /* x < c || x >= c + 1 ==> x != c */
321 ir_node *const p = new_r_Proj(cmp_lo, mode_b, ir_relation_less_greater);
323 } else if (rel_lo == ir_relation_less_equal) {
324 if (rel_hi == ir_relation_equal) {
325 /* x <= c || x == c + 1 ==> x <= c + 1 */
326 ir_node *const p = new_r_Proj(cmp_hi, mode_b, ir_relation_less_equal);
328 } else if (rel_hi == ir_relation_greater_equal) {
329 /* x <= c || x >= c + 1 ==> true */
330 ir_node *const t = new_r_Const(irg, tarval_b_true);
332 } else if (rel_hi == ir_relation_greater) {
333 /* x <= c || x > c + 1 ==> x != c + 1 */
334 ir_node *const p = new_r_Proj(cmp_hi, mode_b, ir_relation_less_greater);
337 } else if (rel_lo == ir_relation_equal && rel_hi == ir_relation_greater_equal) {
338 /* x == c || x >= c + 1 ==> x >= c */
339 ir_node *const p = new_r_Proj(cmp_lo, mode_b, ir_relation_greater_equal);
342 } else if ((rel_lo == ir_relation_less || rel_lo == ir_relation_less_equal) &&
343 (rel_hi == ir_relation_greater || rel_lo == ir_relation_greater_equal) &&
344 get_mode_arithmetic(mode) == irma_twos_complement) {
345 /* works for two-complements only */
346 /* x <|<= lo || x >|>= hi ==> (x - lo) >u|>=u (hi-lo) */
347 if (rel_lo == ir_relation_less_equal) {
348 /* must convert to < */
349 ir_mode *mode = get_tarval_mode(tv_lo);
350 ir_tarval *n = tarval_add(tv_lo, get_mode_one(mode));
351 if (n != tarval_bad && tarval_cmp(n, tv_lo) == ir_relation_greater) {
354 rel_lo = ir_relation_less;
357 if (rel_lo == ir_relation_less) {
359 ir_node *const block = get_nodes_block(cmp_hi);
360 ir_node * x = get_Cmp_left(cmp_hi);
361 ir_mode * mode = get_irn_mode(x);
362 ir_node *sub, *cmp, *c, *subc;
364 if (mode_is_signed(mode)) {
365 /* convert to unsigned */
366 mode = find_unsigned_mode(mode);
369 x = new_r_Conv(block, x, mode);
370 tv_lo = tarval_convert_to(tv_lo, mode);
371 tv_hi = tarval_convert_to(tv_hi, mode);
372 if (tv_lo == tarval_bad || tv_hi == tarval_bad)
375 c = new_r_Const(irg, tv_lo);
376 sub = new_r_Sub(block, x, c, mode);
377 subc = new_r_Sub(block, new_r_Const(irg, tv_hi), c, mode);
378 cmp = new_r_Cmp(block, sub, subc, rel_hi);
386 * Walker, tries to optimize Andb and Orb nodes.
388 static void bool_walk(ir_node *n, void *ctx)
390 bool_opt_env_t *env = (bool_opt_env_t*)ctx;
392 if (get_irn_mode(n) != mode_b)
396 ir_node *const l = get_And_left(n);
397 ir_node *const r = get_And_right(n);
398 ir_node * replacement;
400 if (!find_cond_pair(l, r, &cpair))
402 replacement = bool_and(&cpair, get_nodes_block(n));
404 exchange(n, replacement);
407 } else if (is_Or(n)) {
408 ir_node *const l = get_Or_left(n);
409 ir_node *const r = get_Or_right(n);
410 ir_node * replacement;
412 if (!find_cond_pair(l, r, &cpair))
414 replacement = bool_or(&cpair, get_nodes_block(n));
416 exchange(n, replacement);
423 * Walker, clear Block marker and Phi lists.
425 static void clear_block_infos(ir_node *node, void *env)
429 /* we visit blocks before any other nodes (from the block) */
433 /* clear the PHI list */
434 set_Block_phis(node, NULL);
435 set_Block_mark(node, 0);
439 * Walker: collect Phi nodes and mark
441 static void collect_phis(ir_node *node, void *env)
446 ir_node *block = get_nodes_block(node);
447 add_Block_phi(block, node);
451 /* Ignore control flow nodes, these will be removed. */
452 if (get_irn_pinned(node) == op_pin_state_pinned &&
453 !is_Block(node) && !is_cfop(node)) {
454 /* found a pinned non-cf node, mark its block */
455 ir_node *block = get_nodes_block(node);
456 set_Block_mark(block, 1);
461 * If node is a Jmp in a block containing no pinned instruction
462 * and having only one predecessor, skip the block and return its
463 * cf predecessor, else the node itself.
465 static ir_node *skip_empty_blocks(ir_node *node)
467 while (is_Jmp(node)) {
468 ir_node *block = get_nodes_block(node);
470 if (get_Block_n_cfgpreds(block) != 1)
473 if (get_Block_mark(block))
476 node = get_Block_cfgpred(block, 0);
482 * Check if two block inputs can be fused.
483 * This can be done, if block contains no Phi node that depends on
484 * different inputs idx_i and idx_j.
486 static int can_fuse_block_inputs(const ir_node *block, int idx_i, int idx_j)
490 for (phi = get_Block_phis(block); phi != NULL; phi = get_Phi_next(phi)) {
491 if (get_Phi_pred(phi, idx_i) != get_Phi_pred(phi, idx_j))
498 * Remove block input with given index.
500 static void remove_block_input(ir_node *block, int idx)
502 int i, j, n = get_Block_n_cfgpreds(block) - 1;
505 NEW_ARR_A(ir_node *, ins, n);
508 /* all Phis will be deleted */
511 for (phi = get_Block_phis(block); phi != NULL; phi = next_phi) {
512 next_phi = get_Phi_next(phi);
513 exchange(phi, get_Phi_pred(phi, idx ^ 1));
515 set_Block_phis(block, NULL);
517 for (phi = get_Block_phis(block); phi != NULL; phi = get_Phi_next(phi)) {
518 for (i = j = 0; i <= n; ++i) {
520 ins[j++] = get_Phi_pred(phi, i);
522 set_irn_in(phi, n, ins);
525 for (i = j = 0; i <= n; ++i) {
527 ins[j++] = get_Block_cfgpred(block, i);
529 set_irn_in(block, n, ins);
533 * Under the preposition that we have a chain of blocks from
534 * from_block to to_block, collapse them all into to_block.
536 static void move_nodes_to_block(ir_node *jmp, ir_node *to_block)
538 ir_node *new_jmp = NULL;
539 ir_node *block, *next_block;
541 for (block = get_nodes_block(jmp); block != to_block; block = next_block) {
542 new_jmp = get_Block_cfgpred(block, 0);
543 next_block = get_nodes_block(new_jmp);
544 exchange(block, to_block);
547 exchange(jmp, new_jmp);
553 * if we can find the following structure,
564 * try to convert it into a (x rel_lo c_lo) || (x rel_hi c_hi)
567 static void find_cf_and_or_walker(ir_node *block, void *ctx)
569 bool_opt_env_t *env = (bool_opt_env_t*)ctx;
573 /* because we modify the graph in regions we might not visited yet,
574 * Id nodes might arise here. Ignore them.
579 n_cfgpreds = get_Block_n_cfgpreds(block);
584 for (low_idx = 0; low_idx < n_cfgpreds; ++low_idx) {
585 ir_node *lower_block;
588 ir_node *cond_selector;
591 lower_cf = get_Block_cfgpred(block, low_idx);
592 lower_cf = skip_empty_blocks(lower_cf);
593 if (!is_Proj(lower_cf))
596 cond = get_Proj_pred(lower_cf);
600 lower_block = get_nodes_block(cond);
601 if (get_Block_n_cfgpreds(lower_block) != 1)
604 /* the block must not produce any side-effects */
605 if (get_Block_mark(lower_block))
608 cond_selector = get_Cond_selector(cond);
609 if (get_irn_mode(cond_selector) != mode_b)
612 lower_pred = get_Block_cfgpred_block(lower_block, 0);
614 for (up_idx = 0; up_idx < n_cfgpreds; ++up_idx) {
615 ir_node *upper_block;
618 ir_node *upper_cond_selector;
619 ir_node *replacement;
622 upper_cf = get_Block_cfgpred(block, up_idx);
623 upper_cf = skip_empty_blocks(upper_cf);
624 if (is_Bad(upper_cf))
626 upper_block = get_nodes_block(upper_cf);
627 if (upper_block != lower_pred)
629 if (!block_dominates(upper_block, block))
632 assert(is_Proj(upper_cf));
633 upper_cond = get_Proj_pred(upper_cf);
634 assert(is_Cond(upper_cond));
635 upper_cond_selector = get_Cond_selector(upper_cond);
636 if (get_irn_mode(upper_cond_selector) != mode_b)
639 /* we have found the structure */
640 /* check Phis: There must be NO Phi in block that
641 depends on the existence of low block */
642 if (!can_fuse_block_inputs(block, low_idx, up_idx))
645 /* all fine, try it */
646 if (!find_cond_pair(cond_selector, upper_cond_selector, &cpair))
649 /* normalize pncs: we need the true case to jump into the
650 * common block (ie. conjunctive normal form) */
651 if (get_Proj_proj(lower_cf) == pn_Cond_false) {
652 if (cpair.cmp_lo == cond_selector) {
653 ir_node *cmp = cpair.cmp_lo;
654 ir_node *block = get_nodes_block(cmp);
655 dbg_info *dbgi = get_irn_dbg_info(cmp);
656 cpair.rel_lo = get_negated_relation(cpair.rel_lo);
657 cpair.cmp_lo = new_rd_Cmp(dbgi, block,
658 get_Cmp_left(cmp), get_Cmp_right(cmp), cpair.rel_lo);
660 ir_node *cmp = cpair.cmp_hi;
661 ir_node *block = get_nodes_block(cmp);
662 dbg_info *dbgi = get_irn_dbg_info(cmp);
663 assert(cmp == cond_selector);
664 cpair.rel_hi = get_negated_relation(cpair.rel_hi);
665 cpair.cmp_hi = new_rd_Cmp(dbgi, block,
666 get_Cmp_left(cmp), get_Cmp_right(cmp), cpair.rel_hi);
669 if (get_Proj_proj(upper_cf) == pn_Cond_false) {
670 if (cpair.cmp_lo == upper_cond_selector) {
671 ir_node *cmp = cpair.cmp_lo;
672 ir_node *block = get_nodes_block(cmp);
673 dbg_info *dbgi = get_irn_dbg_info(cmp);
674 cpair.rel_lo = get_negated_relation(cpair.rel_lo);
675 cpair.cmp_lo = new_rd_Cmp(dbgi, block,
676 get_Cmp_left(cmp), get_Cmp_right(cmp), cpair.rel_lo);
678 ir_node *cmp = cpair.cmp_hi;
679 ir_node *block = get_nodes_block(cmp);
680 dbg_info *dbgi = get_irn_dbg_info(cmp);
681 assert(cmp == upper_cond_selector);
682 cpair.rel_hi = get_negated_relation(cpair.rel_hi);
683 cpair.cmp_hi = new_rd_Cmp(dbgi, block,
684 get_Cmp_left(cmp), get_Cmp_right(cmp), cpair.rel_hi);
688 /* can we optimize the case? */
689 replacement = bool_or(&cpair, upper_block);
690 if (replacement == NULL)
695 DB((dbg, LEVEL_1, "boolopt: %+F: fusing (ub %+F lb %+F)\n",
696 get_irn_irg(upper_block), upper_block, lower_block));
698 /* move all expressions on the path to lower/upper block */
699 move_nodes_to_block(get_Block_cfgpred(block, up_idx), upper_block);
700 move_nodes_to_block(get_Block_cfgpred(block, low_idx), lower_block);
702 /* move all nodes from lower block to upper block */
703 exchange(lower_block, upper_block);
705 remove_block_input(block, up_idx);
708 /* the optimizations expected the true case to jump */
709 if (get_Proj_proj(lower_cf) == pn_Cond_false) {
710 ir_node *block = get_nodes_block(replacement);
711 replacement = new_rd_Not(NULL, block, replacement, mode_b);
713 set_Cond_selector(cond, replacement);
720 static ir_graph_state_t do_simplify_bool(ir_graph *const irg)
723 ir_graph_state_t res = 0;
725 /* register a debug mask */
726 FIRM_DBG_REGISTER(dbg, "firm.opt.bool");
730 /* optimize simple Andb and Orb cases */
731 irg_walk_graph(irg, NULL, bool_walk, &env);
733 ir_reserve_resources(irg, IR_RESOURCE_BLOCK_MARK | IR_RESOURCE_PHI_LIST);
735 /* now more complicated cases: find control flow And/Or and optimize. */
736 irg_walk_graph(irg, clear_block_infos, collect_phis, NULL);
737 irg_block_walk_graph(irg, NULL, find_cf_and_or_walker, &env);
740 res |= IR_GRAPH_STATE_CONSISTENT_DOMINANCE;
743 ir_free_resources(irg, IR_RESOURCE_BLOCK_MARK | IR_RESOURCE_PHI_LIST);
748 static optdesc_t opt_simplify_bool = {
749 "bool-simplification",
750 IR_GRAPH_STATE_ONE_RETURN, /* works better with one return block only */
754 void opt_bool(ir_graph *irg)
756 perform_irg_optimization(irg, &opt_simplify_bool);
759 /* Creates an ir_graph pass for opt_bool. */
760 ir_graph_pass_t *opt_bool_pass(const char *name)
762 return def_graph_pass(name ? name : "opt_bool", opt_bool);