cleanup and rewrite dumper interface
[libfirm] / ir / opt / boolopt.c
index 072d638..fbe8d45 100644 (file)
@@ -19,7 +19,7 @@
 
 /**
  * @file
- * @brief   boolean condition/controlflow optimisations
+ * @brief   boolean condition/control flow optimizations
  * @author  Matthias Braun, Christoph Mallon, Michael Beck
  * @version $Id: cfopt.c 22579 2008-10-07 14:54:04Z beck $
  */
 #include "irnode_t.h"
 #include "tv.h"
 #include "irpass.h"
+#include "debug.h"
 
 /** Describes a pair of relative conditions lo < hi, lo pnc_lo x, hi pnc_hi x */
 typedef struct cond_pair {
-       ir_node *cmp_lo;  /**< the lo compare node. */
-       ir_node *cmp_hi;  /**< the hi compare node. */
-       pn_Cmp   pnc_lo;  /**< the lo relation node. */
-       pn_Cmp   pnc_hi;  /**< the hi relation node. */
-       ir_node *proj_lo; /**< the mode_b result proj of cmp_lo */
-       ir_node *proj_hi; /**< the mode_b result proj of cmp_hi */
-       tarval  *tv_lo;   /**< the tarval of lo */
-       tarval  *tv_hi;   /**< the tarval of hi */
+       ir_node *cmp_lo;  /**< The lo compare node. */
+       ir_node *cmp_hi;  /**< The hi compare node. */
+       pn_Cmp   pnc_lo;  /**< The lo relation node. */
+       pn_Cmp   pnc_hi;  /**< The hi relation node. */
+       ir_node *proj_lo; /**< The mode_b result proj of cmp_lo. */
+       ir_node *proj_hi; /**< The mode_b result proj of cmp_hi. */
+       tarval  *tv_lo;   /**< The tarval of cmp_lo node. */
+       tarval  *tv_hi;   /**< The tarval of cmp_hi node. */
+       ir_mode *lo_mode; /**< The mode of the cmp_lo operands. */
 } cond_pair;
 
 /** Environment for all walker in boolopt. */
@@ -56,6 +58,8 @@ typedef struct {
        int changed;  /**< Set if the graph was changed. */
 } bool_opt_env_t;
 
+DEBUG_ONLY(static firm_dbg_module_t *dbg);
+
 /**
  * Check if tho given nodes, l and r, represent two compares with
  * ... . If yes, return non-zero and fill the res struct.
@@ -67,32 +71,38 @@ static int find_cond_pair(ir_node *const l, ir_node *const r, cond_pair *const r
                ir_node *const ro = get_Proj_pred(r);
 
                if (is_Cmp(lo) && is_Cmp(ro)) {
-                       ir_node *const lol = get_Cmp_left(lo);
-                       ir_node *const lor = get_Cmp_right(lo);
-                       ir_node *const rol = get_Cmp_left(ro);
-                       ir_node *const ror = get_Cmp_right(ro);
+                       ir_node *const lol   = get_Cmp_left(lo);
+                       ir_node *const lor   = get_Cmp_right(lo);
+                       ir_node *const rol   = get_Cmp_left(ro);
+                       ir_node *const ror   = get_Cmp_right(ro);
+                       pn_Cmp   const pnc_l = get_Proj_proj(l);
+                       pn_Cmp   const pnc_r = get_Proj_proj(r);
 
                        if (is_Const(lor) && is_Const_null(lor) &&
-                               is_Const(ror) && is_Const_null(ror) &&
-                               get_Proj_proj(l) == pn_Cmp_Lg &&
-                               get_Proj_proj(r) == pn_Cmp_Lg) {
-                               /* lo == (lol != NULL) && ro == (rol != NULL) */
+                           is_Const(ror) && is_Const_null(ror) &&
+                           pnc_l == pnc_r &&
+                           (pnc_l == pn_Cmp_Lg || pnc_l == pn_Cmp_Eq)) {
+                               /* lo == (lol !=|== NULL) && ro == (rol !=|== NULL) */
+                               res->cmp_lo  = lo;
+                               res->cmp_hi  = ro;
+                               res->pnc_lo  = pnc_l;
+                               res->pnc_hi  = pnc_l;
+                               res->proj_lo = l;
+                               res->proj_hi = r;
+                               res->tv_lo   = get_Const_tarval(lor);
+                               res->tv_hi   = get_Const_tarval(ror);
+                               res->lo_mode = get_irn_mode(lor);
 
-                               /* TODO: found <null null> */
+                               return 1;
                        }
 
-                       /* TODO float */
-                       /* The constants shall be unequal.  Local optimisations handle the
-                        * equal case */
-                       if (lol == rol && mode_is_int(get_irn_mode(lol)) && lor != ror && is_Const(lor) && is_Const(ror)) {
-                               /* lo == (x CMP c_l), ro == (x cmp c_r), c_l != c_r */
+                       if (lol == rol && lor != ror && is_Const(lor) && is_Const(ror)) {
+                               /* lo == (x CMP c_l), ro == (x cmp c_r) */
                                tarval *const tv_l  = get_Const_tarval(lor);
                                tarval *const tv_r  = get_Const_tarval(ror);
-                               pn_Cmp  const pnc_l = get_Proj_proj(l);
-                               pn_Cmp  const pnc_r = get_Proj_proj(r);
                                pn_Cmp  const rel   = tarval_cmp(tv_l, tv_r);
 
-                               assert(rel != pn_Cmp_Eq);
+                               res->lo_mode = get_irn_mode(lol);
 
                                if (rel == pn_Cmp_Lt) {
                                        /* c_l < c_r */
@@ -104,8 +114,7 @@ static int find_cond_pair(ir_node *const l, ir_node *const r, cond_pair *const r
                                        res->proj_hi = r;
                                        res->tv_lo   = tv_l;
                                        res->tv_hi   = tv_r;
-                               } else {
-                                       assert(rel == pn_Cmp_Gt);
+                               } else if (rel == pn_Cmp_Gt) {
                                        /* c_l > c_r */
                                        res->cmp_lo  = ro;
                                        res->cmp_hi  = lo;
@@ -115,6 +124,10 @@ static int find_cond_pair(ir_node *const l, ir_node *const r, cond_pair *const r
                                        res->proj_hi = l;
                                        res->tv_lo   = tv_r;
                                        res->tv_hi   = tv_l;
+                               } else {
+                                       /* The constants shall be unequal but comparable.
+                                        * Local optimizations handle the equal case. */
+                                       return 0;
                                }
                                return 1;
                        }
@@ -126,57 +139,129 @@ static int find_cond_pair(ir_node *const l, ir_node *const r, cond_pair *const r
 /**
  * Handle (lo pnc_lo x) AND (hi pnc_hi x)
  */
-static ir_node *bool_and(cond_pair* const cpair)
+static ir_node *bool_and(cond_pair* const cpair, ir_node *dst_block)
 {
        ir_node *const cmp_lo  = cpair->cmp_lo;
        ir_node *const cmp_hi  = cpair->cmp_hi;
-       pn_Cmp   const pnc_lo  = cpair->pnc_lo;
+       pn_Cmp         pnc_lo  = cpair->pnc_lo;
        pn_Cmp   const pnc_hi  = cpair->pnc_hi;
        ir_node *const proj_lo = cpair->proj_lo;
        ir_node *const proj_hi = cpair->proj_hi;
-       tarval  *const tv_lo   = cpair->tv_lo;
-       tarval  *const tv_hi   = cpair->tv_hi;
+       tarval  *      tv_lo   = cpair->tv_lo;
+       tarval  *      tv_hi   = cpair->tv_hi;
+       ir_mode *      mode    = cpair->lo_mode;
+
+       if (pnc_lo == pn_Cmp_Eq && pnc_hi == pn_Cmp_Eq &&
+           tarval_is_null(tv_lo) && tarval_is_null(tv_hi) &&
+           mode == get_tarval_mode(tv_hi)) {
+               /* p == NULL && q == NULL ==> (p&q) == NULL) */
+               ir_node *lol, *hil, *cmp, *c, *p;
+
+               if (mode_is_reference(mode)) {
+                       mode = find_unsigned_mode(mode);
+                       if (! mode)
+                               return NULL;
+                       tv_lo = tarval_convert_to(tv_lo, mode);
+                       if (tv_lo == tarval_bad)
+                               return NULL;
+               }
+               if (mode_is_int(mode)) {
+                       lol   = get_Cmp_left(cmp_lo);
+                       lol   = new_r_Conv(dst_block, lol, mode);
+                       hil   = get_Cmp_left(cmp_hi);
+                       hil   = new_r_Conv(dst_block, hil, mode);
+                       p     = new_r_And(dst_block, lol, hil, mode);
+                       c     = new_Const(tv_lo);
+                       cmp   = new_r_Cmp(dst_block, p, c);
+                       p     = new_r_Proj(cmp, mode_b, pn_Cmp_Eq);
+                       return p;
+               }
+       }
+
+       /* the following tests expect one common operand */
+       if (get_Cmp_left(cmp_lo) !=  get_Cmp_left(cmp_hi))
+               return 0;
+
+       /* TODO: for now reject float modes */
+       if (! mode_is_int(mode))
+               return 0;
 
        /* Beware of NaN's, we can only check for (ordered) != here (which is Lg, not Ne) */
        if ((pnc_lo == pn_Cmp_Lt || pnc_lo == pn_Cmp_Le || pnc_lo == pn_Cmp_Eq) &&
            (pnc_hi == pn_Cmp_Eq || pnc_hi == pn_Cmp_Ge || pnc_hi == pn_Cmp_Gt)) {
-               /* x <|<=|== lo | x ==|>=|> hi -> false */
+               /* x <|<=|== lo && x ==|>=|> hi ==> false */
                ir_node *const t = new_Const(tarval_b_false);
                return t;
        } else if ((pnc_lo == pn_Cmp_Lt || pnc_lo == pn_Cmp_Le || pnc_lo == pn_Cmp_Eq) &&
                   (pnc_hi == pn_Cmp_Lt || pnc_hi == pn_Cmp_Le || pnc_hi == pn_Cmp_Lg)) {
-               /* x <|<=|== lo && x <|<=|!= hi -> x <|<=|== lo */
+               /* x <|<=|== lo && x <|<=|!= hi ==> x <|<=|== lo */
                return proj_lo;
        } else if ((pnc_lo == pn_Cmp_Ge || pnc_lo == pn_Cmp_Gt || pnc_lo == pn_Cmp_Lg) &&
                   (pnc_hi == pn_Cmp_Eq || pnc_hi == pn_Cmp_Ge || pnc_hi == pn_Cmp_Gt)) {
-               /* x >=|>|!= lo || x ==|>=|> hi -> x ==|>=|> hi */
+               /* x >=|>|!= lo && x ==|>=|> hi ==> x ==|>=|> hi */
                return proj_hi;
        } else if (tarval_is_one(tarval_sub(tv_hi, tv_lo, NULL))) { /* lo + 1 == hi */
                if (pnc_lo == pn_Cmp_Ge && pnc_hi == pn_Cmp_Lt) {
-                       /* x >= c || x < c + 1 -> x == c */
-                       ir_node  *const block = get_nodes_block(cmp_lo);
-                       ir_node  *const p = new_r_Proj(block, cmp_lo, mode_b, pn_Cmp_Eq);
+                       /* x >= c && x < c + 1 ==> x == c */
+                       ir_node  *const p = new_r_Proj(cmp_lo, mode_b, pn_Cmp_Eq);
                        return p;
                } else if (pnc_lo == pn_Cmp_Gt) {
                        if (pnc_hi == pn_Cmp_Lg) {
-                               /* x > c || x != c + 1 -> x > c + 1 */
-                               ir_node  *const block = get_nodes_block(cmp_hi);
-                               ir_node  *const p = new_r_Proj(block, cmp_hi, mode_b, pn_Cmp_Gt);
+                               /* x > c && x != c + 1 ==> x > c + 1 */
+                               ir_node  *const p = new_r_Proj(cmp_hi, mode_b, pn_Cmp_Gt);
                                return p;
                        } else if (pnc_hi == pn_Cmp_Lt) {
-                               /* x > c || x < c + 1 -> false */
+                               /* x > c && x < c + 1 ==> false */
                                ir_node *const t = new_Const(tarval_b_false);
                                return t;
                        } else if (pnc_hi == pn_Cmp_Le) {
-                               /* x > c || x <= c + 1 -> x != c + 1 */
-                               ir_node  *const block = get_nodes_block(cmp_hi);
-                               ir_node  *const p = new_r_Proj(block, cmp_hi, mode_b, pn_Cmp_Eq);
+                               /* x > c && x <= c + 1 ==> x != c + 1 */
+                               ir_node  *const p = new_r_Proj(cmp_hi, mode_b, pn_Cmp_Eq);
                                return p;
                        }
                } else if (pnc_lo == pn_Cmp_Lg && pnc_hi == pn_Cmp_Lt) {
-                       /* x != c || c < c + 1 -> x < c */
-                       ir_node  *const block = get_nodes_block(cmp_lo);
-                       ir_node  *const p     = new_r_Proj(block, cmp_lo, mode_b, pn_Cmp_Lt);
+                       /* x != c && c < c + 1 ==> x < c */
+                       ir_node  *const p     = new_r_Proj(cmp_lo, mode_b, pn_Cmp_Lt);
+                       return p;
+               }
+       } else if ((pnc_lo == pn_Cmp_Gt || pnc_lo == pn_Cmp_Ge) &&
+                  (pnc_hi == pn_Cmp_Lt || pnc_lo == pn_Cmp_Le) &&
+                  get_mode_arithmetic(mode) == irma_twos_complement) {
+               /* works for two-complements only */
+               /* x >|\= lo && x <|<= hi ==> (x - lo) <u|<=u (hi-lo) */
+               if (pnc_lo == pn_Cmp_Gt) {
+                       /* must convert to >= */
+                       ir_mode *mode = get_tarval_mode(tv_lo);
+                       tarval *n = tarval_add(tv_lo, get_mode_one(mode));
+                       if (n != tarval_bad && tarval_cmp(n, tv_lo) == pn_Cmp_Gt) {
+                               /* no overflow */
+                               tv_lo = n;
+                               pnc_lo = pn_Cmp_Ge;
+                       }
+               }
+               if (pnc_lo == pn_Cmp_Ge) {
+                       /* all fine */
+                       ir_node *const block = get_nodes_block(cmp_hi);
+                       ir_node *      x     = get_Cmp_left(cmp_hi);
+                       ir_mode *      mode  = get_irn_mode(x);
+                       ir_node *sub, *cmp, *c, *subc, *p;
+
+                       if (mode_is_signed(mode)) {
+                               /* convert to unsigned */
+                               mode = find_unsigned_mode(mode);
+                               if (mode == NULL)
+                                       return NULL;
+                               x     = new_r_Conv(block, x, mode);
+                               tv_lo = tarval_convert_to(tv_lo, mode);
+                               tv_hi = tarval_convert_to(tv_hi, mode);
+                               if (tv_lo == tarval_bad || tv_hi == tarval_bad)
+                                       return NULL;
+                       }
+                       c    = new_Const(tv_lo);
+                       sub  = new_r_Sub(block, x, c, mode);
+                       subc = new_r_Sub(block, new_Const(tv_hi), c, mode);
+                       cmp  = new_r_Cmp(block, sub, subc);
+                       p    = new_r_Proj(cmp, mode_b, pnc_hi);
                        return p;
                }
        }
@@ -186,57 +271,129 @@ static ir_node *bool_and(cond_pair* const cpair)
 /**
  * Handle (lo pnc_lo x) OR (hi pnc_hi x)
  */
-static ir_node *bool_or(cond_pair *const cpair)
+static ir_node *bool_or(cond_pair *const cpair, ir_node *dst_block)
 {
        ir_node *const cmp_lo  = cpair->cmp_lo;
        ir_node *const cmp_hi  = cpair->cmp_hi;
-       pn_Cmp   const pnc_lo  = cpair->pnc_lo;
+       pn_Cmp         pnc_lo  = cpair->pnc_lo;
        pn_Cmp   const pnc_hi  = cpair->pnc_hi;
        ir_node *const proj_lo = cpair->proj_lo;
        ir_node *const proj_hi = cpair->proj_hi;
-       tarval  *const tv_lo   = cpair->tv_lo;
-       tarval  *const tv_hi   = cpair->tv_hi;
+       tarval  *      tv_lo   = cpair->tv_lo;
+       tarval  *      tv_hi   = cpair->tv_hi;
+       ir_mode *      mode    = cpair->lo_mode;
+
+       if (pnc_lo == pn_Cmp_Lg && pnc_hi == pn_Cmp_Lg &&
+               tarval_is_null(tv_lo) && tarval_is_null(tv_hi) &&
+               mode == get_tarval_mode(tv_hi)) {
+               /* p != NULL || q != NULL ==> (p|q) != NULL) */
+               ir_node *lol, *hil, *cmp, *c, *p;
+
+               if (mode_is_reference(mode)) {
+                       mode = find_unsigned_mode(mode);
+                       if (! mode)
+                               return NULL;
+                       tv_lo = tarval_convert_to(tv_lo, mode);
+                       if (tv_lo == tarval_bad)
+                               return NULL;
+               }
+               if (mode_is_int(mode)) {
+                       lol   = get_Cmp_left(cmp_lo);
+                       lol   = new_r_Conv(dst_block, lol, mode);
+                       hil   = get_Cmp_left(cmp_hi);
+                       hil   = new_r_Conv(dst_block, hil, mode);
+                       p     = new_r_Or(dst_block, lol, hil, mode);
+                       c     = new_Const(tv_lo);
+                       cmp   = new_r_Cmp(dst_block, p, c);
+                       p     = new_r_Proj(cmp, mode_b, pn_Cmp_Lg);
+                       return p;
+               }
+       }
+
+       /* the following tests expect one common operand */
+       if (get_Cmp_left(cmp_lo) !=  get_Cmp_left(cmp_hi))
+               return 0;
+
+       /* TODO: for now reject float modes */
+       if (! mode_is_int(mode))
+               return 0;
 
        /* Beware of NaN's, we can only check for (ordered) != here (which is Lg, not Ne) */
        if ((pnc_lo == pn_Cmp_Ge || pnc_lo == pn_Cmp_Gt || pnc_lo == pn_Cmp_Lg) &&
-                       (pnc_hi == pn_Cmp_Lt || pnc_hi == pn_Cmp_Le || pnc_hi == pn_Cmp_Lg)) {
-               /* x >=|>|!= lo | x <|<=|!= hi -> true */
+           (pnc_hi == pn_Cmp_Lt || pnc_hi == pn_Cmp_Le || pnc_hi == pn_Cmp_Lg)) {
+               /* x >=|>|!= lo | x <|<=|!= hi ==> true */
                ir_node *const t = new_Const(tarval_b_true);
                return t;
        } else if ((pnc_lo == pn_Cmp_Lt || pnc_lo == pn_Cmp_Le || pnc_lo == pn_Cmp_Eq) &&
-                                                (pnc_hi == pn_Cmp_Lt || pnc_hi == pn_Cmp_Le || pnc_hi == pn_Cmp_Lg)) {
-               /* x <|<=|== lo || x <|<=|!= hi -> x <|<=|!= hi */
+                  (pnc_hi == pn_Cmp_Lt || pnc_hi == pn_Cmp_Le || pnc_hi == pn_Cmp_Lg)) {
+               /* x <|<=|== lo || x <|<=|!= hi ==> x <|<=|!= hi */
                return proj_hi;
        } else if ((pnc_lo == pn_Cmp_Ge || pnc_lo == pn_Cmp_Gt || pnc_lo == pn_Cmp_Lg) &&
-                                                (pnc_hi == pn_Cmp_Eq || pnc_hi == pn_Cmp_Ge || pnc_hi == pn_Cmp_Gt)) {
-               /* x >=|>|!= lo || x ==|>=|> hi -> x >=|>|!= lo */
+                  (pnc_hi == pn_Cmp_Eq || pnc_hi == pn_Cmp_Ge || pnc_hi == pn_Cmp_Gt)) {
+               /* x >=|>|!= lo || x ==|>=|> hi ==> x >=|>|!= lo */
                return proj_lo;
        } else if (tarval_is_one(tarval_sub(tv_hi, tv_lo, NULL))) { /* lo + 1 == hi */
                if (pnc_lo == pn_Cmp_Lt && pnc_hi == pn_Cmp_Ge) {
-                       /* x < c || x >= c + 1 -> x != c */
-                       ir_node  *const block = get_nodes_block(cmp_lo);
-                       ir_node  *const p = new_r_Proj(block, cmp_lo, mode_b, pn_Cmp_Lg);
+                       /* x < c || x >= c + 1 ==> x != c */
+                       ir_node  *const p = new_r_Proj(cmp_lo, mode_b, pn_Cmp_Lg);
                        return p;
                } else if (pnc_lo == pn_Cmp_Le) {
                        if (pnc_hi == pn_Cmp_Eq) {
-                               /* x <= c || x == c + 1 -> x <= c + 1 */
-                               ir_node  *const block = get_nodes_block(cmp_hi);
-                               ir_node  *const p = new_r_Proj(block, cmp_hi, mode_b, pn_Cmp_Le);
+                               /* x <= c || x == c + 1 ==> x <= c + 1 */
+                               ir_node  *const p = new_r_Proj(cmp_hi, mode_b, pn_Cmp_Le);
                                return p;
                        } else if (pnc_hi == pn_Cmp_Ge) {
-                               /* x <= c || x >= c + 1 -> true */
+                               /* x <= c || x >= c + 1 ==> true */
                                ir_node *const t = new_Const(tarval_b_true);
                                return t;
                        } else if (pnc_hi == pn_Cmp_Gt) {
-                               /* x <= c || x > c + 1 -> x != c + 1 */
-                               ir_node  *const block = get_nodes_block(cmp_hi);
-                               ir_node  *const p = new_r_Proj(block, cmp_hi, mode_b, pn_Cmp_Lg);
+                               /* x <= c || x > c + 1 ==> x != c + 1 */
+                               ir_node  *const p = new_r_Proj(cmp_hi, mode_b, pn_Cmp_Lg);
                                return p;
                        }
                } else if (pnc_lo == pn_Cmp_Eq && pnc_hi == pn_Cmp_Ge) {
-                       /* x == c || x >= c + 1 -> x >= c */
-                       ir_node  *const block = get_nodes_block(cmp_lo);
-                       ir_node  *const p     = new_r_Proj(block, cmp_lo, mode_b, pn_Cmp_Ge);
+                       /* x == c || x >= c + 1 ==> x >= c */
+                       ir_node  *const p     = new_r_Proj(cmp_lo, mode_b, pn_Cmp_Ge);
+                       return p;
+               }
+       } else if ((pnc_lo == pn_Cmp_Lt || pnc_lo == pn_Cmp_Le) &&
+                  (pnc_hi == pn_Cmp_Gt || pnc_lo == pn_Cmp_Ge) &&
+                  get_mode_arithmetic(mode) == irma_twos_complement) {
+               /* works for two-complements only */
+               /* x <|<= lo  || x >|>= hi ==> (x - lo) >u|>=u (hi-lo) */
+               if (pnc_lo == pn_Cmp_Le) {
+                       /* must convert to < */
+                       ir_mode *mode = get_tarval_mode(tv_lo);
+                       tarval *n = tarval_add(tv_lo, get_mode_one(mode));
+                       if (n != tarval_bad && tarval_cmp(n, tv_lo) == pn_Cmp_Gt) {
+                               /* no overflow */
+                               tv_lo = n;
+                               pnc_lo = pn_Cmp_Lt;
+                       }
+               }
+               if (pnc_lo == pn_Cmp_Lt) {
+                       /* all fine */
+                       ir_node *const block = get_nodes_block(cmp_hi);
+                       ir_node *      x     = get_Cmp_left(cmp_hi);
+                       ir_mode *      mode  = get_irn_mode(x);
+                       ir_node *sub, *cmp, *c, *subc, *p;
+
+                       if (mode_is_signed(mode)) {
+                               /* convert to unsigned */
+                               mode = find_unsigned_mode(mode);
+                               if (mode == NULL)
+                                       return NULL;
+                               x     = new_r_Conv(block, x, mode);
+                               tv_lo = tarval_convert_to(tv_lo, mode);
+                               tv_hi = tarval_convert_to(tv_hi, mode);
+                               if (tv_lo == tarval_bad || tv_hi == tarval_bad)
+                                       return NULL;
+                       }
+                       c    = new_Const(tv_lo);
+                       sub  = new_r_Sub(block, x, c, mode);
+                       subc = new_r_Sub(block, new_Const(tv_hi), c, mode);
+                       cmp  = new_r_Cmp(block, sub, subc);
+                       p    = new_r_Proj(cmp, mode_b, pnc_hi);
                        return p;
                }
        }
@@ -260,7 +417,7 @@ static void bool_walk(ir_node *n, void *ctx)
                cond_pair      cpair;
                if (!find_cond_pair(l, r, &cpair))
                        return;
-               replacement = bool_and(&cpair);
+               replacement = bool_and(&cpair, get_nodes_block(n));
                if (replacement) {
                        exchange(n, replacement);
                        env->changed = 1;
@@ -272,7 +429,7 @@ static void bool_walk(ir_node *n, void *ctx)
                cond_pair      cpair;
                if (!find_cond_pair(l, r, &cpair))
                        return;
-               replacement = bool_or(&cpair);
+               replacement = bool_or(&cpair, get_nodes_block(n));
                if (replacement) {
                        exchange(n, replacement);
                        env->changed = 1;
@@ -344,7 +501,8 @@ static ir_node *skip_empty_blocks(ir_node *node)
  * This can be done, if block contains no Phi node that depends on
  * different inputs idx_i and idx_j.
  */
-static int can_fuse_block_inputs(const ir_node *block, int idx_i, int idx_j) {
+static int can_fuse_block_inputs(const ir_node *block, int idx_i, int idx_j)
+{
        const ir_node *phi;
 
        for (phi = get_Block_phis(block); phi != NULL; phi = get_Phi_next(phi)) {
@@ -393,13 +551,18 @@ static void remove_block_input(ir_node *block, int idx)
  * Under the preposition that we have a chain of blocks from
  * from_block to to_block, collapse them all into to_block.
  */
-static void move_nodes_to_block(ir_node *from_block, ir_node *to_block) {
+static void move_nodes_to_block(ir_node *jmp, ir_node *to_block)
+{
+       ir_node *new_jmp = NULL;
        ir_node *block, *next_block;
 
-       for (block = from_block; block != to_block; block = next_block) {
-               next_block = get_Block_cfgpred(block, 0);
+       for (block = get_nodes_block(jmp); block != to_block; block = next_block) {
+               new_jmp = get_Block_cfgpred(block, 0);
+               next_block = get_nodes_block(new_jmp);
                exchange(block, to_block);
        }
+       if (new_jmp)
+               exchange(jmp, new_jmp);
 }
 
 /**
@@ -422,13 +585,20 @@ static void move_nodes_to_block(ir_node *from_block, ir_node *to_block) {
 static void find_cf_and_or_walker(ir_node *block, void *ctx)
 {
        int low_idx, up_idx;
-       int n_cfgpreds = get_Block_n_cfgpreds(block);
+       int n_cfgpreds;
        bool_opt_env_t *env = ctx;
 
-       if (n_cfgpreds < 2)
+       /* because we modify the graph in regions we might not visited yet,
+        * Id nodes might arise here. Ignore them.
+        */
+       if (is_Id(block))
                return;
 
+       n_cfgpreds = get_Block_n_cfgpreds(block);
 restart:
+       if (n_cfgpreds < 2)
+               return;
+
        for (low_idx = 0; low_idx < n_cfgpreds; ++low_idx) {
                ir_node      *lower_block;
                ir_node      *lower_cf;
@@ -474,6 +644,8 @@ restart:
                        upper_block = get_nodes_block(upper_cf);
                        if (upper_block != lower_pred)
                                continue;
+                       if (!block_dominates(upper_block, block))
+                               continue;
 
                        assert(is_Proj(upper_cf));
                        upper_cond = get_Proj_pred(upper_cf);
@@ -496,40 +668,43 @@ restart:
                         * common block (ie. conjunctive normal form) */
                        if (get_Proj_proj(lower_cf) == pn_Cond_false) {
                                if (cpair.proj_lo == cond_selector) {
-                                       ir_mode *mode = get_tarval_mode(cpair.tv_lo);
-                                       cpair.pnc_lo  = get_negated_pnc(cpair.pnc_lo, mode);
-                                       cpair.proj_lo = new_r_Proj(lower_block,
-                                                       get_Proj_pred(cpair.proj_lo), mode_b, cpair.pnc_lo);
+                                       ir_mode *mode  = get_tarval_mode(cpair.tv_lo);
+                                       ir_node *cmp   = get_Proj_pred(cpair.proj_lo);
+                                       cpair.pnc_lo   = get_negated_pnc(cpair.pnc_lo, mode);
+                                       cpair.proj_lo  = new_r_Proj(cmp, mode_b, cpair.pnc_lo);
                                } else {
-                                       ir_mode *mode = get_tarval_mode(cpair.tv_hi);
+                                       ir_mode *mode  = get_tarval_mode(cpair.tv_hi);
+                                       ir_node *cmp   = get_Proj_pred(cpair.proj_hi);
                                        assert(cpair.proj_hi == cond_selector);
-                                       cpair.pnc_hi  = get_negated_pnc(cpair.pnc_hi, mode);
-                                       cpair.proj_hi = new_r_Proj(lower_block,
-                                                       get_Proj_pred(cpair.proj_hi), mode_b, cpair.pnc_hi);
+                                       cpair.pnc_hi   = get_negated_pnc(cpair.pnc_hi, mode);
+                                       cpair.proj_hi  = new_r_Proj(cmp, mode_b, cpair.pnc_hi);
                                }
                        }
                        if (get_Proj_proj(upper_cf) == pn_Cond_false) {
                                if (cpair.proj_lo == upper_cond_selector) {
-                                       ir_mode *mode = get_tarval_mode(cpair.tv_lo);
-                                       cpair.pnc_lo  = get_negated_pnc(cpair.pnc_lo, mode);
-                                       cpair.proj_lo = new_r_Proj(upper_block,
-                                                       get_Proj_pred(cpair.proj_lo), mode_b, cpair.pnc_lo);
+                                       ir_mode *mode  = get_tarval_mode(cpair.tv_lo);
+                                       ir_node *cmp   = get_Proj_pred(cpair.proj_lo);
+                                       cpair.pnc_lo   = get_negated_pnc(cpair.pnc_lo, mode);
+                                       cpair.proj_lo  = new_r_Proj(cmp, mode_b, cpair.pnc_lo);
                                } else {
-                                       ir_mode *mode = get_tarval_mode(cpair.tv_hi);
+                                       ir_mode *mode  = get_tarval_mode(cpair.tv_hi);
+                                       ir_node *cmp   = get_Proj_pred(cpair.proj_hi);
                                        assert(cpair.proj_hi == upper_cond_selector);
-                                       cpair.pnc_hi  = get_negated_pnc(cpair.pnc_hi, mode);
-                                       cpair.proj_hi = new_r_Proj(upper_block,
-                                                       get_Proj_pred(cpair.proj_hi), mode_b, cpair.pnc_hi);
+                                       cpair.pnc_hi   = get_negated_pnc(cpair.pnc_hi, mode);
+                                       cpair.proj_hi  = new_r_Proj(cmp, mode_b, cpair.pnc_hi);
                                }
                        }
 
                        /* can we optimize the case? */
-                       replacement = bool_or(&cpair);
+                       replacement = bool_or(&cpair, upper_block);
                        if (replacement == NULL)
                                continue;
 
                        env->changed = 1;
 
+                       DB((dbg, LEVEL_1, "boolopt: %+F: fusing (ub %+F lb %+F)\n",
+                               current_ir_graph, upper_block, lower_block));
+
                        /* move all expressions on the path to lower/upper block */
                        move_nodes_to_block(get_Block_cfgpred(block, up_idx), upper_block);
                        move_nodes_to_block(get_Block_cfgpred(block, low_idx), lower_block);
@@ -538,15 +713,15 @@ restart:
                        exchange(lower_block, upper_block);
 
                        remove_block_input(block, up_idx);
+                       --n_cfgpreds;
 
-                       /* the optimisations expected the true case to jump */
+                       /* the optimizations expected the true case to jump */
                        if (get_Proj_proj(lower_cf) == pn_Cond_false) {
                                ir_node *block = get_nodes_block(replacement);
                                replacement    = new_rd_Not(NULL, block, replacement, mode_b);
                        }
                        set_Cond_selector(cond, replacement);
 
-                       ir_fprintf(stderr, "replaced (ub %+F)\n", upper_block);
                        goto restart;
                }
        }
@@ -556,6 +731,9 @@ void opt_bool(ir_graph *const irg)
 {
        bool_opt_env_t env;
 
+       /* register a debug mask */
+       FIRM_DBG_REGISTER(dbg, "firm.opt.bool");
+
        /* works better with one return block only */
        normalize_one_return(irg);