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