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
31 #include "../adt/array_t.h"
32 #include "iroptimize.h"
41 #include "opt_manage.h"
43 /** Describes a pair of relative conditions lo < hi, lo rel_lo x, hi rel_hi x */
44 typedef struct cond_pair {
45 ir_node *cmp_lo; /**< The lo compare node. */
46 ir_node *cmp_hi; /**< The hi compare node. */
47 ir_relation rel_lo; /**< The lo relation node. */
48 ir_relation rel_hi; /**< The hi relation node. */
49 ir_tarval *tv_lo; /**< The tarval of cmp_lo node. */
50 ir_tarval *tv_hi; /**< The tarval of cmp_hi node. */
51 ir_mode *lo_mode; /**< The mode of the cmp_lo operands. */
54 /** Environment for all walker in boolopt. */
56 int changed; /**< Set if the graph was changed. */
59 DEBUG_ONLY(static firm_dbg_module_t *dbg;)
62 * Check if tho given nodes, l and r, represent two compares with
63 * ... . If yes, return non-zero and fill the res struct.
65 static bool find_cond_pair(ir_node *const l, ir_node *const r, cond_pair *const res)
67 if (is_Cmp(l) && is_Cmp(r)) {
68 ir_node *const lol = get_Cmp_left(l);
69 ir_node *const lor = get_Cmp_right(l);
70 ir_node *const rol = get_Cmp_left(r);
71 ir_node *const ror = get_Cmp_right(r);
72 ir_relation const pnc_l = get_Cmp_relation(l);
73 ir_relation const pnc_r = get_Cmp_relation(r);
75 if (is_Const(lor) && is_Const_null(lor) &&
76 is_Const(ror) && is_Const_null(ror) &&
78 (pnc_l == ir_relation_less_greater || pnc_l == ir_relation_equal)) {
79 /* l == (lol !=|== NULL) && r == (rol !=|== NULL) */
84 res->tv_lo = get_Const_tarval(lor);
85 res->tv_hi = get_Const_tarval(ror);
86 res->lo_mode = get_irn_mode(lor);
91 if (lol == rol && lor != ror && is_Const(lor) && is_Const(ror)) {
92 /* l == (x CMP c_l), r == (x cmp c_r) */
93 ir_tarval *const tv_l = get_Const_tarval(lor);
94 ir_tarval *const tv_r = get_Const_tarval(ror);
95 ir_relation const rel = tarval_cmp(tv_l, tv_r);
97 res->lo_mode = get_irn_mode(lol);
99 if (rel == ir_relation_less) {
107 } else if (rel == ir_relation_greater) {
116 /* The constants shall be unequal but comparable.
117 * Local optimizations handle the equal case. */
127 * Handle (lo rel_lo x) AND (hi rel_hi x)
129 static ir_node *bool_and(cond_pair* const cpair, ir_node *dst_block)
131 ir_node *const cmp_lo = cpair->cmp_lo;
132 ir_node *const cmp_hi = cpair->cmp_hi;
133 ir_relation rel_lo = cpair->rel_lo;
134 ir_relation const rel_hi = cpair->rel_hi;
135 ir_tarval * tv_lo = cpair->tv_lo;
136 ir_tarval * tv_hi = cpair->tv_hi;
137 ir_mode * mode = cpair->lo_mode;
138 ir_graph * irg = get_irn_irg(cmp_lo);
140 if (rel_lo == ir_relation_equal && rel_hi == rel_lo &&
141 tarval_is_null(tv_lo) && tarval_is_null(tv_hi) &&
142 mode == get_tarval_mode(tv_hi)) {
143 /* p == NULL && q == NULL ==> (p&q) == NULL) */
144 ir_node *lol, *hil, *cmp, *c, *p;
146 if (mode_is_reference(mode)) {
147 mode = find_unsigned_mode(mode);
150 tv_lo = tarval_convert_to(tv_lo, mode);
151 if (tv_lo == tarval_bad)
154 if (mode_is_int(mode)) {
155 lol = get_Cmp_left(cmp_lo);
156 lol = new_r_Conv(dst_block, lol, mode);
157 hil = get_Cmp_left(cmp_hi);
158 hil = new_r_Conv(dst_block, hil, mode);
159 p = new_r_And(dst_block, lol, hil, mode);
160 c = new_r_Const(irg, tv_lo);
161 cmp = new_r_Cmp(dst_block, p, c, ir_relation_equal);
166 /* the following tests expect one common operand */
167 if (get_Cmp_left(cmp_lo) != get_Cmp_left(cmp_hi))
170 /* TODO: for now reject float modes */
171 if (! mode_is_int(mode))
174 /* Beware of NaN's, we can only check for (ordered) != here (which is Lg, not Ne) */
175 if ((rel_lo == ir_relation_less || rel_lo == ir_relation_less_equal || rel_lo == ir_relation_equal) &&
176 (rel_hi == ir_relation_equal || rel_hi == ir_relation_greater_equal || rel_hi == ir_relation_greater)) {
177 /* x <|<=|== lo && x ==|>=|> hi ==> false */
178 ir_node *const t = new_r_Const(irg, tarval_b_false);
180 } else if ((rel_lo == ir_relation_less || rel_lo == ir_relation_less_equal || rel_lo == ir_relation_equal) &&
181 (rel_hi == ir_relation_less || rel_hi == ir_relation_less_equal || rel_hi == ir_relation_less_greater)) {
182 /* x <|<=|== lo && x <|<=|!= hi ==> x <|<=|== lo */
184 } else if ((rel_lo == ir_relation_greater_equal || rel_lo == ir_relation_greater || rel_lo == ir_relation_less_greater) &&
185 (rel_hi == ir_relation_equal || rel_hi == ir_relation_greater_equal || rel_hi == ir_relation_greater)) {
186 /* x >=|>|!= lo && x ==|>=|> hi ==> x ==|>=|> hi */
188 } else if (tarval_is_one(tarval_sub(tv_hi, tv_lo, NULL))) { /* lo + 1 == hi */
189 if (rel_lo == ir_relation_greater_equal && rel_hi == ir_relation_less) {
190 /* x >= c && x < c + 1 ==> x == c */
191 ir_node *const p = new_r_Proj(cmp_lo, mode_b, ir_relation_equal);
193 } else if (rel_lo == ir_relation_greater) {
194 if (rel_hi == ir_relation_less_greater) {
195 /* x > c && x != c + 1 ==> x > c + 1 */
196 ir_node *const p = new_r_Proj(cmp_hi, mode_b, ir_relation_greater);
198 } else if (rel_hi == ir_relation_less) {
199 /* x > c && x < c + 1 ==> false */
200 ir_node *const t = new_r_Const(irg, tarval_b_false);
202 } else if (rel_hi == ir_relation_less_equal) {
203 /* x > c && x <= c + 1 ==> x != c + 1 */
204 ir_node *const p = new_r_Proj(cmp_hi, mode_b, ir_relation_equal);
207 } else if (rel_lo == ir_relation_less_greater && rel_hi == ir_relation_less) {
208 /* x != c && c < c + 1 ==> x < c */
209 ir_node *const p = new_r_Proj(cmp_lo, mode_b, ir_relation_less);
212 } else if ((rel_lo == ir_relation_greater || rel_lo == ir_relation_greater_equal) &&
213 (rel_hi == ir_relation_less || rel_lo == ir_relation_less_equal) &&
214 get_mode_arithmetic(mode) == irma_twos_complement) {
215 /* works for two-complements only */
216 /* x >|\= lo && x <|<= hi ==> (x - lo) <u|<=u (hi-lo) */
217 if (rel_lo == ir_relation_greater) {
218 /* must convert to >= */
219 ir_mode *mode = get_tarval_mode(tv_lo);
220 ir_tarval *n = tarval_add(tv_lo, get_mode_one(mode));
221 if (n != tarval_bad && tarval_cmp(n, tv_lo) == ir_relation_greater) {
224 rel_lo = ir_relation_greater_equal;
227 if (rel_lo == ir_relation_greater_equal) {
229 ir_node *const block = get_nodes_block(cmp_hi);
230 ir_node * x = get_Cmp_left(cmp_hi);
231 ir_mode * mode = get_irn_mode(x);
232 ir_node *sub, *cmp, *c, *subc;
234 if (mode_is_signed(mode)) {
235 /* convert to unsigned */
236 mode = find_unsigned_mode(mode);
239 x = new_r_Conv(block, x, mode);
240 tv_lo = tarval_convert_to(tv_lo, mode);
241 tv_hi = tarval_convert_to(tv_hi, mode);
242 if (tv_lo == tarval_bad || tv_hi == tarval_bad)
245 c = new_r_Const(irg, tv_lo);
246 sub = new_r_Sub(block, x, c, mode);
247 subc = new_r_Sub(block, new_r_Const(irg, tv_hi), c, mode);
248 cmp = new_r_Cmp(block, sub, subc, rel_hi);
256 * Handle (lo rel_lo x) OR (hi rel_hi x)
258 static ir_node *bool_or(cond_pair *const cpair, ir_node *dst_block)
260 ir_node *const cmp_lo = cpair->cmp_lo;
261 ir_node *const cmp_hi = cpair->cmp_hi;
262 ir_relation rel_lo = cpair->rel_lo;
263 ir_relation const rel_hi = cpair->rel_hi;
264 ir_tarval * tv_lo = cpair->tv_lo;
265 ir_tarval * tv_hi = cpair->tv_hi;
266 ir_mode * mode = cpair->lo_mode;
267 ir_graph * irg = get_irn_irg(cmp_lo);
269 if (rel_lo == ir_relation_less_greater && rel_hi == ir_relation_less_greater &&
270 tarval_is_null(tv_lo) && tarval_is_null(tv_hi) &&
271 mode == get_tarval_mode(tv_hi)) {
272 /* p != NULL || q != NULL ==> (p|q) != NULL) */
273 ir_node *lol, *hil, *cmp, *c, *p;
275 if (mode_is_reference(mode)) {
276 mode = find_unsigned_mode(mode);
279 tv_lo = tarval_convert_to(tv_lo, mode);
280 if (tv_lo == tarval_bad)
283 if (mode_is_int(mode)) {
284 lol = get_Cmp_left(cmp_lo);
285 lol = new_r_Conv(dst_block, lol, mode);
286 hil = get_Cmp_left(cmp_hi);
287 hil = new_r_Conv(dst_block, hil, mode);
288 p = new_r_Or(dst_block, lol, hil, mode);
289 c = new_r_Const(irg, tv_lo);
290 cmp = new_r_Cmp(dst_block, p, c, ir_relation_less_greater);
295 /* the following tests expect one common operand */
296 if (get_Cmp_left(cmp_lo) != get_Cmp_left(cmp_hi))
299 /* TODO: for now reject float modes */
300 if (! mode_is_int(mode))
303 /* Beware of NaN's, we can only check for (ordered) != here (which is Lg, not Ne) */
304 if ((rel_lo == ir_relation_greater_equal || rel_lo == ir_relation_greater || rel_lo == ir_relation_less_greater) &&
305 (rel_hi == ir_relation_less || rel_hi == ir_relation_less_equal || rel_hi == ir_relation_less_greater)) {
306 /* x >=|>|!= lo | x <|<=|!= hi ==> true */
307 ir_node *const t = new_r_Const(irg, tarval_b_true);
309 } else if ((rel_lo == ir_relation_less || rel_lo == ir_relation_less_equal || rel_lo == ir_relation_equal) &&
310 (rel_hi == ir_relation_less || rel_hi == ir_relation_less_equal || rel_hi == ir_relation_less_greater)) {
311 /* x <|<=|== lo || x <|<=|!= hi ==> x <|<=|!= hi */
313 } else if ((rel_lo == ir_relation_greater_equal || rel_lo == ir_relation_greater || rel_lo == ir_relation_less_greater) &&
314 (rel_hi == ir_relation_equal || rel_hi == ir_relation_greater_equal || rel_hi == ir_relation_greater)) {
315 /* x >=|>|!= lo || x ==|>=|> hi ==> x >=|>|!= lo */
317 } else if (tarval_is_one(tarval_sub(tv_hi, tv_lo, NULL))) { /* lo + 1 == hi */
318 if (rel_lo == ir_relation_less && rel_hi == ir_relation_greater_equal) {
319 /* x < c || x >= c + 1 ==> x != c */
320 ir_node *const p = new_r_Proj(cmp_lo, mode_b, ir_relation_less_greater);
322 } else if (rel_lo == ir_relation_less_equal) {
323 if (rel_hi == ir_relation_equal) {
324 /* x <= c || x == c + 1 ==> x <= c + 1 */
325 ir_node *const p = new_r_Proj(cmp_hi, mode_b, ir_relation_less_equal);
327 } else if (rel_hi == ir_relation_greater_equal) {
328 /* x <= c || x >= c + 1 ==> true */
329 ir_node *const t = new_r_Const(irg, tarval_b_true);
331 } else if (rel_hi == ir_relation_greater) {
332 /* x <= c || x > c + 1 ==> x != c + 1 */
333 ir_node *const p = new_r_Proj(cmp_hi, mode_b, ir_relation_less_greater);
336 } else if (rel_lo == ir_relation_equal && rel_hi == ir_relation_greater_equal) {
337 /* x == c || x >= c + 1 ==> x >= c */
338 ir_node *const p = new_r_Proj(cmp_lo, mode_b, ir_relation_greater_equal);
341 } else if ((rel_lo == ir_relation_less || rel_lo == ir_relation_less_equal) &&
342 (rel_hi == ir_relation_greater || rel_lo == ir_relation_greater_equal) &&
343 get_mode_arithmetic(mode) == irma_twos_complement) {
344 /* works for two-complements only */
345 /* x <|<= lo || x >|>= hi ==> (x - lo) >u|>=u (hi-lo) */
346 if (rel_lo == ir_relation_less_equal) {
347 /* must convert to < */
348 ir_mode *mode = get_tarval_mode(tv_lo);
349 ir_tarval *n = tarval_add(tv_lo, get_mode_one(mode));
350 if (n != tarval_bad && tarval_cmp(n, tv_lo) == ir_relation_greater) {
353 rel_lo = ir_relation_less;
356 if (rel_lo == ir_relation_less) {
358 ir_node *const block = get_nodes_block(cmp_hi);
359 ir_node * x = get_Cmp_left(cmp_hi);
360 ir_mode * mode = get_irn_mode(x);
361 ir_node *sub, *cmp, *c, *subc;
363 if (mode_is_signed(mode)) {
364 /* convert to unsigned */
365 mode = find_unsigned_mode(mode);
368 x = new_r_Conv(block, x, mode);
369 tv_lo = tarval_convert_to(tv_lo, mode);
370 tv_hi = tarval_convert_to(tv_hi, mode);
371 if (tv_lo == tarval_bad || tv_hi == tarval_bad)
374 c = new_r_Const(irg, tv_lo);
375 sub = new_r_Sub(block, x, c, mode);
376 subc = new_r_Sub(block, new_r_Const(irg, tv_hi), c, mode);
377 cmp = new_r_Cmp(block, sub, subc, rel_hi);
385 * Walker, tries to optimize Andb and Orb nodes.
387 static void bool_walk(ir_node *n, void *ctx)
389 bool_opt_env_t *env = (bool_opt_env_t*)ctx;
391 if (get_irn_mode(n) != mode_b)
395 ir_node *const l = get_And_left(n);
396 ir_node *const r = get_And_right(n);
397 ir_node * replacement;
399 if (!find_cond_pair(l, r, &cpair))
401 replacement = bool_and(&cpair, get_nodes_block(n));
403 exchange(n, replacement);
406 } else if (is_Or(n)) {
407 ir_node *const l = get_Or_left(n);
408 ir_node *const r = get_Or_right(n);
409 ir_node * replacement;
411 if (!find_cond_pair(l, r, &cpair))
413 replacement = bool_or(&cpair, get_nodes_block(n));
415 exchange(n, replacement);
422 * Walker, clear Block marker and Phi lists.
424 static void clear_block_infos(ir_node *node, void *env)
428 /* we visit blocks before any other nodes (from the block) */
432 /* clear the PHI list */
433 set_Block_phis(node, NULL);
434 set_Block_mark(node, 0);
438 * Walker: collect Phi nodes and mark
440 static void collect_phis(ir_node *node, void *env)
445 ir_node *block = get_nodes_block(node);
446 add_Block_phi(block, node);
450 /* Ignore control flow nodes, these will be removed. */
451 if (get_irn_pinned(node) == op_pin_state_pinned &&
452 !is_Block(node) && !is_cfop(node)) {
453 /* found a pinned non-cf node, mark its block */
454 ir_node *block = get_nodes_block(node);
455 set_Block_mark(block, 1);
460 * If node is a Jmp in a block containing no pinned instruction
461 * and having only one predecessor, skip the block and return its
462 * cf predecessor, else the node itself.
464 static ir_node *skip_empty_blocks(ir_node *node)
466 while (is_Jmp(node)) {
467 ir_node *block = get_nodes_block(node);
469 if (get_Block_n_cfgpreds(block) != 1)
472 if (get_Block_mark(block))
475 node = get_Block_cfgpred(block, 0);
481 * Check if two block inputs can be fused.
482 * This can be done, if block contains no Phi node that depends on
483 * different inputs idx_i and idx_j.
485 static int can_fuse_block_inputs(const ir_node *block, int idx_i, int idx_j)
489 for (phi = get_Block_phis(block); phi != NULL; phi = get_Phi_next(phi)) {
490 if (get_Phi_pred(phi, idx_i) != get_Phi_pred(phi, idx_j))
497 * Remove block input with given index.
499 static void remove_block_input(ir_node *block, int idx)
501 int i, j, n = get_Block_n_cfgpreds(block) - 1;
504 NEW_ARR_A(ir_node *, ins, n);
507 /* all Phis will be deleted */
510 for (phi = get_Block_phis(block); phi != NULL; phi = next_phi) {
511 next_phi = get_Phi_next(phi);
512 exchange(phi, get_Phi_pred(phi, idx ^ 1));
514 set_Block_phis(block, NULL);
516 for (phi = get_Block_phis(block); phi != NULL; phi = get_Phi_next(phi)) {
517 for (i = j = 0; i <= n; ++i) {
519 ins[j++] = get_Phi_pred(phi, i);
521 set_irn_in(phi, n, ins);
524 for (i = j = 0; i <= n; ++i) {
526 ins[j++] = get_Block_cfgpred(block, i);
528 set_irn_in(block, n, ins);
532 * Under the preposition that we have a chain of blocks from
533 * from_block to to_block, collapse them all into to_block.
535 static void move_nodes_to_block(ir_node *jmp, ir_node *to_block)
537 ir_node *new_jmp = NULL;
538 ir_node *block, *next_block;
540 for (block = get_nodes_block(jmp); block != to_block; block = next_block) {
541 new_jmp = get_Block_cfgpred(block, 0);
542 next_block = get_nodes_block(new_jmp);
543 exchange(block, to_block);
546 exchange(jmp, new_jmp);
552 * if we can find the following structure,
563 * try to convert it into a (x rel_lo c_lo) || (x rel_hi c_hi)
566 static void find_cf_and_or_walker(ir_node *block, void *ctx)
568 bool_opt_env_t *env = (bool_opt_env_t*)ctx;
572 /* because we modify the graph in regions we might not visited yet,
573 * Id nodes might arise here. Ignore them.
578 n_cfgpreds = get_Block_n_cfgpreds(block);
583 for (low_idx = 0; low_idx < n_cfgpreds; ++low_idx) {
584 ir_node *lower_block;
587 ir_node *cond_selector;
590 lower_cf = get_Block_cfgpred(block, low_idx);
591 lower_cf = skip_empty_blocks(lower_cf);
592 if (!is_Proj(lower_cf))
595 cond = get_Proj_pred(lower_cf);
599 lower_block = get_nodes_block(cond);
600 if (get_Block_n_cfgpreds(lower_block) != 1)
603 /* the block must not produce any side-effects */
604 if (get_Block_mark(lower_block))
607 cond_selector = get_Cond_selector(cond);
608 if (get_irn_mode(cond_selector) != mode_b)
611 lower_pred = get_Block_cfgpred_block(lower_block, 0);
613 for (up_idx = 0; up_idx < n_cfgpreds; ++up_idx) {
614 ir_node *upper_block;
617 ir_node *upper_cond_selector;
618 ir_node *replacement;
621 upper_cf = get_Block_cfgpred(block, up_idx);
622 upper_cf = skip_empty_blocks(upper_cf);
623 if (is_Bad(upper_cf))
625 upper_block = get_nodes_block(upper_cf);
626 if (upper_block != lower_pred)
628 if (!block_dominates(upper_block, block))
631 assert(is_Proj(upper_cf));
632 upper_cond = get_Proj_pred(upper_cf);
633 assert(is_Cond(upper_cond));
634 upper_cond_selector = get_Cond_selector(upper_cond);
635 if (get_irn_mode(upper_cond_selector) != mode_b)
638 /* we have found the structure */
639 /* check Phis: There must be NO Phi in block that
640 depends on the existence of low block */
641 if (!can_fuse_block_inputs(block, low_idx, up_idx))
644 /* all fine, try it */
645 if (!find_cond_pair(cond_selector, upper_cond_selector, &cpair))
648 /* normalize pncs: we need the true case to jump into the
649 * common block (ie. conjunctive normal form) */
650 if (get_Proj_proj(lower_cf) == pn_Cond_false) {
651 if (cpair.cmp_lo == cond_selector) {
652 ir_node *cmp = cpair.cmp_lo;
653 ir_node *block = get_nodes_block(cmp);
654 dbg_info *dbgi = get_irn_dbg_info(cmp);
655 cpair.rel_lo = get_negated_relation(cpair.rel_lo);
656 cpair.cmp_lo = new_rd_Cmp(dbgi, block,
657 get_Cmp_left(cmp), get_Cmp_right(cmp), cpair.rel_lo);
659 ir_node *cmp = cpair.cmp_hi;
660 ir_node *block = get_nodes_block(cmp);
661 dbg_info *dbgi = get_irn_dbg_info(cmp);
662 assert(cmp == cond_selector);
663 cpair.rel_hi = get_negated_relation(cpair.rel_hi);
664 cpair.cmp_hi = new_rd_Cmp(dbgi, block,
665 get_Cmp_left(cmp), get_Cmp_right(cmp), cpair.rel_hi);
668 if (get_Proj_proj(upper_cf) == pn_Cond_false) {
669 if (cpair.cmp_lo == upper_cond_selector) {
670 ir_node *cmp = cpair.cmp_lo;
671 ir_node *block = get_nodes_block(cmp);
672 dbg_info *dbgi = get_irn_dbg_info(cmp);
673 cpair.rel_lo = get_negated_relation(cpair.rel_lo);
674 cpair.cmp_lo = new_rd_Cmp(dbgi, block,
675 get_Cmp_left(cmp), get_Cmp_right(cmp), cpair.rel_lo);
677 ir_node *cmp = cpair.cmp_hi;
678 ir_node *block = get_nodes_block(cmp);
679 dbg_info *dbgi = get_irn_dbg_info(cmp);
680 assert(cmp == upper_cond_selector);
681 cpair.rel_hi = get_negated_relation(cpair.rel_hi);
682 cpair.cmp_hi = new_rd_Cmp(dbgi, block,
683 get_Cmp_left(cmp), get_Cmp_right(cmp), cpair.rel_hi);
687 /* can we optimize the case? */
688 replacement = bool_or(&cpair, upper_block);
689 if (replacement == NULL)
694 DB((dbg, LEVEL_1, "boolopt: %+F: fusing (ub %+F lb %+F)\n",
695 get_irn_irg(upper_block), upper_block, lower_block));
697 /* move all expressions on the path to lower/upper block */
698 move_nodes_to_block(get_Block_cfgpred(block, up_idx), upper_block);
699 move_nodes_to_block(get_Block_cfgpred(block, low_idx), lower_block);
701 /* move all nodes from lower block to upper block */
702 exchange(lower_block, upper_block);
704 remove_block_input(block, up_idx);
707 /* the optimizations expected the true case to jump */
708 if (get_Proj_proj(lower_cf) == pn_Cond_false) {
709 ir_node *block = get_nodes_block(replacement);
710 replacement = new_rd_Not(NULL, block, replacement, mode_b);
712 set_Cond_selector(cond, replacement);
719 static ir_graph_state_t do_simplify_bool(ir_graph *const irg)
722 ir_graph_state_t res = 0;
724 /* register a debug mask */
725 FIRM_DBG_REGISTER(dbg, "firm.opt.bool");
729 /* optimize simple Andb and Orb cases */
730 irg_walk_graph(irg, NULL, bool_walk, &env);
732 ir_reserve_resources(irg, IR_RESOURCE_BLOCK_MARK | IR_RESOURCE_PHI_LIST);
734 /* now more complicated cases: find control flow And/Or and optimize. */
735 irg_walk_graph(irg, clear_block_infos, collect_phis, NULL);
736 irg_block_walk_graph(irg, NULL, find_cf_and_or_walker, &env);
739 res |= IR_GRAPH_STATE_CONSISTENT_DOMINANCE;
742 ir_free_resources(irg, IR_RESOURCE_BLOCK_MARK | IR_RESOURCE_PHI_LIST);
747 static optdesc_t opt_simplify_bool = {
748 "bool-simplification",
749 IR_GRAPH_STATE_ONE_RETURN, /* works better with one return block only */
753 void opt_bool(ir_graph *irg)
755 perform_irg_optimization(irg, &opt_simplify_bool);
758 /* Creates an ir_graph pass for opt_bool. */
759 ir_graph_pass_t *opt_bool_pass(const char *name)
761 return def_graph_pass(name ? name : "opt_bool", opt_bool);