Fixed and enhanced boolopt:
[libfirm] / ir / opt / boolopt.c
1 /*
2  * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
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.
10  *
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.
14  *
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
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief   boolean condition/controlflow optimisations
23  * @author  Matthias Braun, Christoph Mallon, Michael Beck
24  * @version $Id: cfopt.c 22579 2008-10-07 14:54:04Z beck $
25  */
26 #include "config.h"
27
28 #include <assert.h>
29 #include <string.h>
30
31 #include "adt/obst.h"
32 #include "../adt/array_t.h"
33 #include "iroptimize.h"
34 #include "ircons.h"
35 #include "irgmod.h"
36 #include "irgwalk.h"
37 #include "irprintf.h"
38 #include "irnode_t.h"
39 #include "tv.h"
40 #include "irpass.h"
41
42 /** Describes a pair of relative conditions lo < hi, lo pnc_lo x, hi pnc_hi x */
43 typedef struct cond_pair {
44         ir_node *cmp_lo;  /**< the lo compare node. */
45         ir_node *cmp_hi;  /**< the hi compare node. */
46         pn_Cmp   pnc_lo;  /**< the lo relation node. */
47         pn_Cmp   pnc_hi;  /**< the hi relation node. */
48         ir_node *proj_lo; /**< the mode_b result proj of cmp_lo */
49         ir_node *proj_hi; /**< the mode_b result proj of cmp_hi */
50         tarval  *tv_lo;   /**< the tarval of lo */
51         tarval  *tv_hi;   /**< the tarval of hi */
52 } cond_pair;
53
54 /** Environment for all walker in boolopt. */
55 typedef struct {
56         int changed;  /**< Set if the graph was changed. */
57 } bool_opt_env_t;
58
59 /**
60  * Check if tho given nodes, l and r, represent two compares with
61  * ... . If yes, return non-zero and fill the res struct.
62  */
63 static int find_cond_pair(ir_node *const l, ir_node *const r, cond_pair *const res)
64 {
65         if (is_Proj(l) && is_Proj(r)) {
66                 ir_node *const lo = get_Proj_pred(l);
67                 ir_node *const ro = get_Proj_pred(r);
68
69                 if (is_Cmp(lo) && is_Cmp(ro)) {
70                         ir_node *const lol = get_Cmp_left(lo);
71                         ir_node *const lor = get_Cmp_right(lo);
72                         ir_node *const rol = get_Cmp_left(ro);
73                         ir_node *const ror = get_Cmp_right(ro);
74
75                         if (is_Const(lor) && is_Const_null(lor) &&
76                                 is_Const(ror) && is_Const_null(ror) &&
77                                 get_Proj_proj(l) == pn_Cmp_Lg &&
78                                 get_Proj_proj(r) == pn_Cmp_Lg) {
79                                 /* lo == (lol != NULL) && ro == (rol != NULL) */
80
81                                 /* TODO: found <null null> */
82                         }
83
84                         /* TODO float */
85                         /* The constants shall be unequal.  Local optimisations handle the
86                          * equal case */
87                         if (lol == rol && mode_is_int(get_irn_mode(lol)) && lor != ror && is_Const(lor) && is_Const(ror)) {
88                                 /* lo == (x CMP c_l), ro == (x cmp c_r), c_l != c_r */
89                                 tarval *const tv_l  = get_Const_tarval(lor);
90                                 tarval *const tv_r  = get_Const_tarval(ror);
91                                 pn_Cmp  const pnc_l = get_Proj_proj(l);
92                                 pn_Cmp  const pnc_r = get_Proj_proj(r);
93                                 pn_Cmp  const rel   = tarval_cmp(tv_l, tv_r);
94
95                                 assert(rel != pn_Cmp_Eq);
96
97                                 if (rel == pn_Cmp_Lt) {
98                                         /* c_l < c_r */
99                                         res->cmp_lo  = lo;
100                                         res->cmp_hi  = ro;
101                                         res->pnc_lo  = pnc_l;
102                                         res->pnc_hi  = pnc_r;
103                                         res->proj_lo = l;
104                                         res->proj_hi = r;
105                                         res->tv_lo   = tv_l;
106                                         res->tv_hi   = tv_r;
107                                 } else {
108                                         assert(rel == pn_Cmp_Gt);
109                                         /* c_l > c_r */
110                                         res->cmp_lo  = ro;
111                                         res->cmp_hi  = lo;
112                                         res->pnc_lo  = pnc_r;
113                                         res->pnc_hi  = pnc_l;
114                                         res->proj_lo = r;
115                                         res->proj_hi = l;
116                                         res->tv_lo   = tv_r;
117                                         res->tv_hi   = tv_l;
118                                 }
119                                 return 1;
120                         }
121                 }
122         }
123         return 0;
124 }
125
126 /**
127  * Handle (lo pnc_lo x) AND (hi pnc_hi x)
128  */
129 static ir_node *bool_and(cond_pair* const cpair)
130 {
131         ir_node *const cmp_lo  = cpair->cmp_lo;
132         ir_node *const cmp_hi  = cpair->cmp_hi;
133         pn_Cmp   const pnc_lo  = cpair->pnc_lo;
134         pn_Cmp   const pnc_hi  = cpair->pnc_hi;
135         ir_node *const proj_lo = cpair->proj_lo;
136         ir_node *const proj_hi = cpair->proj_hi;
137         tarval  *const tv_lo   = cpair->tv_lo;
138         tarval  *const tv_hi   = cpair->tv_hi;
139
140         /* Beware of NaN's, we can only check for (ordered) != here (which is Lg, not Ne) */
141         if ((pnc_lo == pn_Cmp_Lt || pnc_lo == pn_Cmp_Le || pnc_lo == pn_Cmp_Eq) &&
142             (pnc_hi == pn_Cmp_Eq || pnc_hi == pn_Cmp_Ge || pnc_hi == pn_Cmp_Gt)) {
143                 /* x <|<=|== lo | x ==|>=|> hi -> false */
144                 ir_node *const t = new_Const(tarval_b_false);
145                 return t;
146         } else if ((pnc_lo == pn_Cmp_Lt || pnc_lo == pn_Cmp_Le || pnc_lo == pn_Cmp_Eq) &&
147                    (pnc_hi == pn_Cmp_Lt || pnc_hi == pn_Cmp_Le || pnc_hi == pn_Cmp_Lg)) {
148                 /* x <|<=|== lo && x <|<=|!= hi -> x <|<=|== lo */
149                 return proj_lo;
150         } else if ((pnc_lo == pn_Cmp_Ge || pnc_lo == pn_Cmp_Gt || pnc_lo == pn_Cmp_Lg) &&
151                    (pnc_hi == pn_Cmp_Eq || pnc_hi == pn_Cmp_Ge || pnc_hi == pn_Cmp_Gt)) {
152                 /* x >=|>|!= lo || x ==|>=|> hi -> x ==|>=|> hi */
153                 return proj_hi;
154         } else if (tarval_is_one(tarval_sub(tv_hi, tv_lo, NULL))) { /* lo + 1 == hi */
155                 if (pnc_lo == pn_Cmp_Ge && pnc_hi == pn_Cmp_Lt) {
156                         /* x >= c || x < c + 1 -> x == c */
157                         ir_node  *const block = get_nodes_block(cmp_lo);
158                         ir_node  *const p = new_r_Proj(block, cmp_lo, mode_b, pn_Cmp_Eq);
159                         return p;
160                 } else if (pnc_lo == pn_Cmp_Gt) {
161                         if (pnc_hi == pn_Cmp_Lg) {
162                                 /* x > c || x != c + 1 -> x > c + 1 */
163                                 ir_node  *const block = get_nodes_block(cmp_hi);
164                                 ir_node  *const p = new_r_Proj(block, cmp_hi, mode_b, pn_Cmp_Gt);
165                                 return p;
166                         } else if (pnc_hi == pn_Cmp_Lt) {
167                                 /* x > c || x < c + 1 -> false */
168                                 ir_node *const t = new_Const(tarval_b_false);
169                                 return t;
170                         } else if (pnc_hi == pn_Cmp_Le) {
171                                 /* x > c || x <= c + 1 -> x != c + 1 */
172                                 ir_node  *const block = get_nodes_block(cmp_hi);
173                                 ir_node  *const p = new_r_Proj(block, cmp_hi, mode_b, pn_Cmp_Eq);
174                                 return p;
175                         }
176                 } else if (pnc_lo == pn_Cmp_Lg && pnc_hi == pn_Cmp_Lt) {
177                         /* x != c || c < c + 1 -> x < c */
178                         ir_node  *const block = get_nodes_block(cmp_lo);
179                         ir_node  *const p     = new_r_Proj(block, cmp_lo, mode_b, pn_Cmp_Lt);
180                         return p;
181                 }
182         }
183         return NULL;
184 }
185
186 /**
187  * Handle (lo pnc_lo x) OR (hi pnc_hi x)
188  */
189 static ir_node *bool_or(cond_pair *const cpair)
190 {
191         ir_node *const cmp_lo  = cpair->cmp_lo;
192         ir_node *const cmp_hi  = cpair->cmp_hi;
193         pn_Cmp   const pnc_lo  = cpair->pnc_lo;
194         pn_Cmp   const pnc_hi  = cpair->pnc_hi;
195         ir_node *const proj_lo = cpair->proj_lo;
196         ir_node *const proj_hi = cpair->proj_hi;
197         tarval  *const tv_lo   = cpair->tv_lo;
198         tarval  *const tv_hi   = cpair->tv_hi;
199
200         /* Beware of NaN's, we can only check for (ordered) != here (which is Lg, not Ne) */
201         if ((pnc_lo == pn_Cmp_Ge || pnc_lo == pn_Cmp_Gt || pnc_lo == pn_Cmp_Lg) &&
202                         (pnc_hi == pn_Cmp_Lt || pnc_hi == pn_Cmp_Le || pnc_hi == pn_Cmp_Lg)) {
203                 /* x >=|>|!= lo | x <|<=|!= hi -> true */
204                 ir_node *const t = new_Const(tarval_b_true);
205                 return t;
206         } else if ((pnc_lo == pn_Cmp_Lt || pnc_lo == pn_Cmp_Le || pnc_lo == pn_Cmp_Eq) &&
207                                                  (pnc_hi == pn_Cmp_Lt || pnc_hi == pn_Cmp_Le || pnc_hi == pn_Cmp_Lg)) {
208                 /* x <|<=|== lo || x <|<=|!= hi -> x <|<=|!= hi */
209                 return proj_hi;
210         } else if ((pnc_lo == pn_Cmp_Ge || pnc_lo == pn_Cmp_Gt || pnc_lo == pn_Cmp_Lg) &&
211                                                  (pnc_hi == pn_Cmp_Eq || pnc_hi == pn_Cmp_Ge || pnc_hi == pn_Cmp_Gt)) {
212                 /* x >=|>|!= lo || x ==|>=|> hi -> x >=|>|!= lo */
213                 return proj_lo;
214         } else if (tarval_is_one(tarval_sub(tv_hi, tv_lo, NULL))) { /* lo + 1 == hi */
215                 if (pnc_lo == pn_Cmp_Lt && pnc_hi == pn_Cmp_Ge) {
216                         /* x < c || x >= c + 1 -> x != c */
217                         ir_node  *const block = get_nodes_block(cmp_lo);
218                         ir_node  *const p = new_r_Proj(block, cmp_lo, mode_b, pn_Cmp_Lg);
219                         return p;
220                 } else if (pnc_lo == pn_Cmp_Le) {
221                         if (pnc_hi == pn_Cmp_Eq) {
222                                 /* x <= c || x == c + 1 -> x <= c + 1 */
223                                 ir_node  *const block = get_nodes_block(cmp_hi);
224                                 ir_node  *const p = new_r_Proj(block, cmp_hi, mode_b, pn_Cmp_Le);
225                                 return p;
226                         } else if (pnc_hi == pn_Cmp_Ge) {
227                                 /* x <= c || x >= c + 1 -> true */
228                                 ir_node *const t = new_Const(tarval_b_true);
229                                 return t;
230                         } else if (pnc_hi == pn_Cmp_Gt) {
231                                 /* x <= c || x > c + 1 -> x != c + 1 */
232                                 ir_node  *const block = get_nodes_block(cmp_hi);
233                                 ir_node  *const p = new_r_Proj(block, cmp_hi, mode_b, pn_Cmp_Lg);
234                                 return p;
235                         }
236                 } else if (pnc_lo == pn_Cmp_Eq && pnc_hi == pn_Cmp_Ge) {
237                         /* x == c || x >= c + 1 -> x >= c */
238                         ir_node  *const block = get_nodes_block(cmp_lo);
239                         ir_node  *const p     = new_r_Proj(block, cmp_lo, mode_b, pn_Cmp_Ge);
240                         return p;
241                 }
242         }
243         return NULL;
244 }
245
246 /**
247  * Walker, tries to optimize Andb and Orb nodes.
248  */
249 static void bool_walk(ir_node *n, void *ctx)
250 {
251         bool_opt_env_t *env = ctx;
252
253         if (get_irn_mode(n) != mode_b)
254                 return;
255
256         if (is_And(n)) {
257                 ir_node *const l = get_And_left(n);
258                 ir_node *const r = get_And_right(n);
259                 ir_node *      replacement;
260                 cond_pair      cpair;
261                 if (!find_cond_pair(l, r, &cpair))
262                         return;
263                 replacement = bool_and(&cpair);
264                 if (replacement) {
265                         exchange(n, replacement);
266                         env->changed = 1;
267                 }
268         } else if (is_Or(n)) {
269                 ir_node *const l = get_Or_left(n);
270                 ir_node *const r = get_Or_right(n);
271                 ir_node *      replacement;
272                 cond_pair      cpair;
273                 if (!find_cond_pair(l, r, &cpair))
274                         return;
275                 replacement = bool_or(&cpair);
276                 if (replacement) {
277                         exchange(n, replacement);
278                         env->changed = 1;
279                 }
280         }
281 }
282
283 /**
284  * Walker, clear Block marker and Phi lists.
285  */
286 static void clear_block_infos(ir_node *node, void *env)
287 {
288         (void) env;
289
290         /* we visit blocks before any other nodes (from the block) */
291         if (!is_Block(node))
292                 return;
293
294         /* clear the PHI list */
295         set_Block_phis(node, NULL);
296         set_Block_mark(node, 0);
297 }
298
299 /**
300  * Walker: collect Phi nodes and mark
301  */
302 static void collect_phis(ir_node *node, void *env)
303 {
304         (void) env;
305
306         if (is_Phi(node)) {
307                 ir_node *block = get_nodes_block(node);
308                 add_Block_phi(block, node);
309                 return;
310         }
311
312         /* Ignore control flow nodes, these will be removed. */
313         if (get_irn_pinned(node) == op_pin_state_pinned &&
314                         !is_Block(node) && !is_cfop(node)) {
315                                 /* found a pinned non-cf node, mark its block */
316                 ir_node *block = get_nodes_block(node);
317                 set_Block_mark(block, 1);
318         }
319 }
320
321 /**
322  * If node is a Jmp in a block containing no pinned instruction
323  * and having only one predecessor, skip the block and return its
324  * cf predecessor, else the node itself.
325  */
326 static ir_node *skip_empty_blocks(ir_node *node)
327 {
328         while (is_Jmp(node)) {
329                 ir_node *block = get_nodes_block(node);
330
331                 if (get_Block_n_cfgpreds(block) != 1)
332                         break;
333
334                 if (get_Block_mark(block))
335                         break;
336
337                 node = get_Block_cfgpred(block, 0);
338         }
339         return node;
340 }
341
342 /**
343  * Check if two block inputs can be fused.
344  * This can be done, if block contains no Phi node that depends on
345  * different inputs idx_i and idx_j.
346  */
347 static int can_fuse_block_inputs(const ir_node *block, int idx_i, int idx_j) {
348         const ir_node *phi;
349
350         for (phi = get_Block_phis(block); phi != NULL; phi = get_Phi_next(phi)) {
351                 if (get_Phi_pred(phi, idx_i) != get_Phi_pred(phi, idx_j))
352                         return 0;
353         }
354         return 1;
355 }
356
357 /**
358  * Remove block input with given index.
359  */
360 static void remove_block_input(ir_node *block, int idx)
361 {
362         int i, j, n = get_Block_n_cfgpreds(block) - 1;
363         ir_node *phi, **ins;
364
365         NEW_ARR_A(ir_node *, ins, n);
366
367         if (n == 1) {
368                 /* all Phis will be deleted */
369                 ir_node *next_phi;
370
371                 for (phi = get_Block_phis(block); phi != NULL; phi = next_phi) {
372                         next_phi = get_Phi_next(phi);
373                         exchange(phi, get_Phi_pred(phi, idx ^ 1));
374                 }
375                 set_Block_phis(block, NULL);
376         } else {
377                 for (phi = get_Block_phis(block); phi != NULL; phi = get_Phi_next(phi)) {
378                         for (i = j = 0; i <= n; ++i) {
379                                 if (i != idx)
380                                         ins[j++] = get_Phi_pred(phi, i);
381                         }
382                         set_irn_in(phi, n, ins);
383                 }
384         }
385         for (i = j = 0; i <= n; ++i) {
386                 if (i != idx)
387                         ins[j++] = get_Block_cfgpred(block, i);
388         }
389         set_irn_in(block, n, ins);
390 }
391
392 /**
393  * Under the preposition that we have a chain of blocks from
394  * from_block to to_block, collapse them all into to_block.
395  */
396 static void move_nodes_to_block(ir_node *from_block, ir_node *to_block) {
397         ir_node *block, *next_block;
398
399         for (block = from_block; block != to_block; block = next_block) {
400                 next_block = get_Block_cfgpred(block, 0);
401                 exchange(block, to_block);
402         }
403 }
404
405 /**
406  * Block walker:
407  *
408  * if we can find the following structure,
409  *
410  *        upper_block
411  *         /       |
412  *        /        |
413  *   lower_block   |
414  *     /  \        |
415  *   ... low_idx up_idx
416  *          \      |
417  *            block
418  *
419  * try to convert it into a (x pnc_lo c_lo) || (x pnc_hi c_hi)
420  * and optimize.
421  */
422 static void find_cf_and_or_walker(ir_node *block, void *ctx)
423 {
424         int low_idx, up_idx;
425         int n_cfgpreds = get_Block_n_cfgpreds(block);
426         bool_opt_env_t *env = ctx;
427
428         if (n_cfgpreds < 2)
429                 return;
430
431 restart:
432         for (low_idx = 0; low_idx < n_cfgpreds; ++low_idx) {
433                 ir_node      *lower_block;
434                 ir_node      *lower_cf;
435                 ir_node      *cond;
436                 ir_node      *cond_selector;
437                 ir_node      *lower_pred;
438
439                 lower_cf = get_Block_cfgpred(block, low_idx);
440                 lower_cf = skip_empty_blocks(lower_cf);
441                 if (!is_Proj(lower_cf))
442                         continue;
443
444                 cond = get_Proj_pred(lower_cf);
445                 if (!is_Cond(cond))
446                         continue;
447
448                 lower_block = get_nodes_block(cond);
449                 if (get_Block_n_cfgpreds(lower_block) != 1)
450                         continue;
451
452                 /* the block must not produce any side-effects */
453                 if (get_Block_mark(lower_block))
454                         continue;
455
456                 cond_selector = get_Cond_selector(cond);
457                 if (get_irn_mode(cond_selector) != mode_b)
458                         continue;
459
460                 lower_pred = get_Block_cfgpred_block(lower_block, 0);
461
462                 for (up_idx = 0; up_idx < n_cfgpreds; ++up_idx) {
463                         ir_node   *upper_block;
464                         ir_node   *upper_cf;
465                         ir_node   *upper_cond;
466                         ir_node   *upper_cond_selector;
467                         ir_node   *replacement;
468                         cond_pair  cpair;
469
470                         upper_cf    = get_Block_cfgpred(block, up_idx);
471                         upper_cf    = skip_empty_blocks(upper_cf);
472                         if (is_Bad(upper_cf))
473                                 continue;
474                         upper_block = get_nodes_block(upper_cf);
475                         if (upper_block != lower_pred)
476                                 continue;
477
478                         assert(is_Proj(upper_cf));
479                         upper_cond = get_Proj_pred(upper_cf);
480                         assert(is_Cond(upper_cond));
481                         upper_cond_selector = get_Cond_selector(upper_cond);
482                         if (get_irn_mode(upper_cond_selector) != mode_b)
483                                 continue;
484
485                         /* we have found the structure */
486                         /* check Phis: There must be NO Phi in block that
487                            depends on the existence of low block */
488                         if (!can_fuse_block_inputs(block, low_idx, up_idx))
489                                 continue;
490
491                         /* all fine, try it */
492                         if (!find_cond_pair(cond_selector, upper_cond_selector, &cpair))
493                                 continue;
494
495                         /* normalize pncs: we need the true case to jump into the
496                          * common block (ie. conjunctive normal form) */
497                         if (get_Proj_proj(lower_cf) == pn_Cond_false) {
498                                 if (cpair.proj_lo == cond_selector) {
499                                         ir_mode *mode = get_tarval_mode(cpair.tv_lo);
500                                         cpair.pnc_lo  = get_negated_pnc(cpair.pnc_lo, mode);
501                                         cpair.proj_lo = new_r_Proj(lower_block,
502                                                         get_Proj_pred(cpair.proj_lo), mode_b, cpair.pnc_lo);
503                                 } else {
504                                         ir_mode *mode = get_tarval_mode(cpair.tv_hi);
505                                         assert(cpair.proj_hi == cond_selector);
506                                         cpair.pnc_hi  = get_negated_pnc(cpair.pnc_hi, mode);
507                                         cpair.proj_hi = new_r_Proj(lower_block,
508                                                         get_Proj_pred(cpair.proj_hi), mode_b, cpair.pnc_hi);
509                                 }
510                         }
511                         if (get_Proj_proj(upper_cf) == pn_Cond_false) {
512                                 if (cpair.proj_lo == upper_cond_selector) {
513                                         ir_mode *mode = get_tarval_mode(cpair.tv_lo);
514                                         cpair.pnc_lo  = get_negated_pnc(cpair.pnc_lo, mode);
515                                         cpair.proj_lo = new_r_Proj(upper_block,
516                                                         get_Proj_pred(cpair.proj_lo), mode_b, cpair.pnc_lo);
517                                 } else {
518                                         ir_mode *mode = get_tarval_mode(cpair.tv_hi);
519                                         assert(cpair.proj_hi == upper_cond_selector);
520                                         cpair.pnc_hi  = get_negated_pnc(cpair.pnc_hi, mode);
521                                         cpair.proj_hi = new_r_Proj(upper_block,
522                                                         get_Proj_pred(cpair.proj_hi), mode_b, cpair.pnc_hi);
523                                 }
524                         }
525
526                         /* can we optimize the case? */
527                         replacement = bool_or(&cpair);
528                         if (replacement == NULL)
529                                 continue;
530
531                         env->changed = 1;
532
533                         /* move all expressions on the path to lower/upper block */
534                         move_nodes_to_block(get_Block_cfgpred(block, up_idx), upper_block);
535                         move_nodes_to_block(get_Block_cfgpred(block, low_idx), lower_block);
536
537                         /* move all nodes from lower block to upper block */
538                         exchange(lower_block, upper_block);
539
540                         remove_block_input(block, up_idx);
541
542                         /* the optimisations expected the true case to jump */
543                         if (get_Proj_proj(lower_cf) == pn_Cond_false) {
544                                 ir_node *block = get_nodes_block(replacement);
545                                 replacement    = new_rd_Not(NULL, block, replacement, mode_b);
546                         }
547                         set_Cond_selector(cond, replacement);
548
549                         ir_fprintf(stderr, "replaced (ub %+F)\n", upper_block);
550                         goto restart;
551                 }
552         }
553 }
554
555 void opt_bool(ir_graph *const irg)
556 {
557         bool_opt_env_t env;
558
559         /* works better with one return block only */
560         normalize_one_return(irg);
561
562         env.changed = 0;
563
564         /* optimize simple Andb and Orb cases */
565         irg_walk_graph(irg, NULL, bool_walk, &env);
566
567         ir_reserve_resources(irg, IR_RESOURCE_BLOCK_MARK | IR_RESOURCE_PHI_LIST);
568
569         /* now more complicated cases: find control flow And/Or and optimize. */
570         irg_walk_graph(irg, clear_block_infos, collect_phis, NULL);
571         irg_block_walk_graph(irg, NULL, find_cf_and_or_walker, &env);
572
573         if (env.changed) {
574                 set_irg_outs_inconsistent(irg);
575                 set_irg_doms_inconsistent(irg);
576                 set_irg_extblk_inconsistent(irg);
577                 set_irg_loopinfo_inconsistent(irg);
578         }
579
580         ir_free_resources(irg, IR_RESOURCE_BLOCK_MARK | IR_RESOURCE_PHI_LIST);
581 }
582
583 /* Creates an ir_graph pass for opt_bool. */
584 ir_graph_pass_t *opt_bool_pass(const char *name)
585 {
586         return def_graph_pass(name ? name : "opt_bool", opt_bool);
587 }  /* opt_bool_pass */