fix irio not working correctly anymore after MacroBlocks were removed
[libfirm] / ir / opt / boolopt.c
index 88940f8..eaae5a3 100644 (file)
 
 /** 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 cmp_lo node. */
-       tarval  *tv_hi;   /**< The tarval of cmp_hi node. */
-       ir_mode *lo_mode; /**< The mode of the cmp_lo operands. */
+       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. */
+       ir_tarval *tv_lo;   /**< The tarval of cmp_lo node. */
+       ir_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. */
@@ -75,8 +75,8 @@ static int find_cond_pair(ir_node *const l, ir_node *const r, cond_pair *const r
                        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);
+                       pn_Cmp   const pnc_l = get_Proj_pn_cmp(l);
+                       pn_Cmp   const pnc_r = get_Proj_pn_cmp(r);
 
                        if (is_Const(lor) && is_Const_null(lor) &&
                            is_Const(ror) && is_Const_null(ror) &&
@@ -98,9 +98,9 @@ static int find_cond_pair(ir_node *const l, ir_node *const r, cond_pair *const 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 rel   = tarval_cmp(tv_l, tv_r);
+                               ir_tarval *const tv_l  = get_Const_tarval(lor);
+                               ir_tarval *const tv_r  = get_Const_tarval(ror);
+                               pn_Cmp     const rel   = tarval_cmp(tv_l, tv_r);
 
                                res->lo_mode = get_irn_mode(lol);
 
@@ -141,15 +141,16 @@ static int find_cond_pair(ir_node *const l, ir_node *const r, cond_pair *const r
  */
 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         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  *      tv_lo   = cpair->tv_lo;
-       tarval  *      tv_hi   = cpair->tv_hi;
-       ir_mode *      mode    = cpair->lo_mode;
+       ir_node    *const cmp_lo  = cpair->cmp_lo;
+       ir_node    *const cmp_hi  = cpair->cmp_hi;
+       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;
+       ir_tarval  *      tv_lo   = cpair->tv_lo;
+       ir_tarval  *      tv_hi   = cpair->tv_hi;
+       ir_mode    *      mode    = cpair->lo_mode;
+       ir_graph   *      irg     = get_irn_irg(cmp_lo);
 
        if (pnc_lo == pn_Cmp_Eq && pnc_hi == pn_Cmp_Eq &&
            tarval_is_null(tv_lo) && tarval_is_null(tv_hi) &&
@@ -171,7 +172,7 @@ static ir_node *bool_and(cond_pair* const cpair, ir_node *dst_block)
                        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);
+                       c     = new_r_Const(irg, tv_lo);
                        cmp   = new_r_Cmp(dst_block, p, c);
                        p     = new_r_Proj(cmp, mode_b, pn_Cmp_Eq);
                        return p;
@@ -190,7 +191,7 @@ static ir_node *bool_and(cond_pair* const cpair, ir_node *dst_block)
        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 */
-               ir_node *const t = new_Const(tarval_b_false);
+               ir_node *const t = new_r_Const(irg, 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)) {
@@ -212,7 +213,7 @@ static ir_node *bool_and(cond_pair* const cpair, ir_node *dst_block)
                                return p;
                        } else if (pnc_hi == pn_Cmp_Lt) {
                                /* x > c && x < c + 1 ==> false */
-                               ir_node *const t = new_Const(tarval_b_false);
+                               ir_node *const t = new_r_Const(irg, tarval_b_false);
                                return t;
                        } else if (pnc_hi == pn_Cmp_Le) {
                                /* x > c && x <= c + 1 ==> x != c + 1 */
@@ -231,8 +232,8 @@ static ir_node *bool_and(cond_pair* const cpair, ir_node *dst_block)
                /* 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));
+                       ir_mode   *mode = get_tarval_mode(tv_lo);
+                       ir_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;
@@ -257,9 +258,9 @@ static ir_node *bool_and(cond_pair* const cpair, ir_node *dst_block)
                                if (tv_lo == tarval_bad || tv_hi == tarval_bad)
                                        return NULL;
                        }
-                       c    = new_Const(tv_lo);
+                       c    = new_r_Const(irg, tv_lo);
                        sub  = new_r_Sub(block, x, c, mode);
-                       subc = new_r_Sub(block, new_Const(tv_hi), c, mode);
+                       subc = new_r_Sub(block, new_r_Const(irg, tv_hi), c, mode);
                        cmp  = new_r_Cmp(block, sub, subc);
                        p    = new_r_Proj(cmp, mode_b, pnc_hi);
                        return p;
@@ -273,15 +274,16 @@ static ir_node *bool_and(cond_pair* const cpair, ir_node *dst_block)
  */
 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         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  *      tv_lo   = cpair->tv_lo;
-       tarval  *      tv_hi   = cpair->tv_hi;
-       ir_mode *      mode    = cpair->lo_mode;
+       ir_node   *const cmp_lo  = cpair->cmp_lo;
+       ir_node   *const cmp_hi  = cpair->cmp_hi;
+       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;
+       ir_tarval *      tv_lo   = cpair->tv_lo;
+       ir_tarval *      tv_hi   = cpair->tv_hi;
+       ir_mode   *      mode    = cpair->lo_mode;
+       ir_graph  *      irg     = get_irn_irg(cmp_lo);
 
        if (pnc_lo == pn_Cmp_Lg && pnc_hi == pn_Cmp_Lg &&
                tarval_is_null(tv_lo) && tarval_is_null(tv_hi) &&
@@ -303,7 +305,7 @@ static ir_node *bool_or(cond_pair *const cpair, ir_node *dst_block)
                        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);
+                       c     = new_r_Const(irg, tv_lo);
                        cmp   = new_r_Cmp(dst_block, p, c);
                        p     = new_r_Proj(cmp, mode_b, pn_Cmp_Lg);
                        return p;
@@ -322,7 +324,7 @@ static ir_node *bool_or(cond_pair *const cpair, ir_node *dst_block)
        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 */
-               ir_node *const t = new_Const(tarval_b_true);
+               ir_node *const t = new_r_Const(irg, 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)) {
@@ -344,7 +346,7 @@ static ir_node *bool_or(cond_pair *const cpair, ir_node *dst_block)
                                return p;
                        } else if (pnc_hi == pn_Cmp_Ge) {
                                /* x <= c || x >= c + 1 ==> true */
-                               ir_node *const t = new_Const(tarval_b_true);
+                               ir_node *const t = new_r_Const(irg, tarval_b_true);
                                return t;
                        } else if (pnc_hi == pn_Cmp_Gt) {
                                /* x <= c || x > c + 1 ==> x != c + 1 */
@@ -363,8 +365,8 @@ static ir_node *bool_or(cond_pair *const cpair, ir_node *dst_block)
                /* 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));
+                       ir_mode   *mode = get_tarval_mode(tv_lo);
+                       ir_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;
@@ -389,9 +391,9 @@ static ir_node *bool_or(cond_pair *const cpair, ir_node *dst_block)
                                if (tv_lo == tarval_bad || tv_hi == tarval_bad)
                                        return NULL;
                        }
-                       c    = new_Const(tv_lo);
+                       c    = new_r_Const(irg, tv_lo);
                        sub  = new_r_Sub(block, x, c, mode);
-                       subc = new_r_Sub(block, new_Const(tv_hi), c, mode);
+                       subc = new_r_Sub(block, new_r_Const(irg, tv_hi), c, mode);
                        cmp  = new_r_Cmp(block, sub, subc);
                        p    = new_r_Proj(cmp, mode_b, pnc_hi);
                        return p;
@@ -405,7 +407,7 @@ static ir_node *bool_or(cond_pair *const cpair, ir_node *dst_block)
  */
 static void bool_walk(ir_node *n, void *ctx)
 {
-       bool_opt_env_t *env = ctx;
+       bool_opt_env_t *env = (bool_opt_env_t*)ctx;
 
        if (get_irn_mode(n) != mode_b)
                return;
@@ -584,10 +586,17 @@ static void move_nodes_to_block(ir_node *jmp, ir_node *to_block)
  */
 static void find_cf_and_or_walker(ir_node *block, void *ctx)
 {
+       bool_opt_env_t *env = (bool_opt_env_t*)ctx;
        int low_idx, up_idx;
-       int n_cfgpreds = get_Block_n_cfgpreds(block);
-       bool_opt_env_t *env = ctx;
+       int n_cfgpreds;
 
+       /* 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;
@@ -696,7 +705,7 @@ restart:
                        env->changed = 1;
 
                        DB((dbg, LEVEL_1, "boolopt: %+F: fusing (ub %+F lb %+F)\n",
-                               current_ir_graph, upper_block, lower_block));
+                               get_irn_irg(upper_block), 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);