remove #ifdef HAVE_CONFIG_Hs
[libfirm] / ir / opt / opt_confirms.c
index b9c21c4..3fd45f7 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 1995-2007 University of Karlsruhe.  All right reserved.
+ * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
  *
  * This file is part of libFirm.
  *
@@ -23,9 +23,7 @@
  * @author  Michael Beck
  * @version $Id$
  */
-#ifdef HAVE_CONFIG_H
-# include "config.h"
-#endif
+#include "config.h"
 
 #undef DEBUG_CONFIRM
 
@@ -97,34 +95,24 @@ static tarval *compare_iv_dbg(const interval_t *l_iv, const interval_t *r_iv, pn
  * This is a often needed case, so we handle here Confirm
  * nodes too.
  */
-int value_not_zero(ir_node *blk, ir_node *n, ir_node **confirm) {
+int value_not_zero(const ir_node *n, ir_node_cnst_ptr *confirm) {
 #define RET_ON(x)  if (x) { *confirm = n; return 1; }; break
 
        tarval *tv;
        ir_mode *mode = get_irn_mode(n);
        pn_Cmp pnc;
 
-       if (get_irg_pinned(get_irn_irg(blk)) != op_pin_state_pinned)
-               blk = NULL;
-
        *confirm = NULL;
 
        /* there might be several Confirms one after other that form an interval */
-       for (; is_Confirm(n); n = get_Confirm_value(n)) {
-               unsigned long region = get_Confirm_region(n);
-
-               /* check if it's legal to use this confirm. */
-               if (region != 0) {
-                       ir_node *curr_blk = get_irn_n(n, -1);
-
-                       /* this confirm is bound to a region. */
-                       if (! blk)
-                               continue;
-
-                       if (get_Block_exc_region(curr_blk) != region)
-                               continue;
-                       /* all went fine, the current Confirm belongs to the same region */
+       for (;;) {
+               if (is_Minus(n) || is_Abs(n)) {
+                       /* we can safely skip Minus and Abs when checking for != 0 */
+                       n = get_unop_op(n);
+                       continue;
                }
+               if (! is_Confirm(n))
+                       break;
 
                /*
                 * Note: A Confirm is never after a Const. So,
@@ -161,6 +149,7 @@ int value_not_zero(ir_node *blk, ir_node *n, ir_node **confirm) {
                default:
                        break;
                }
+               n = get_Confirm_value(n);
        }
        tv = value_of(n);
 
@@ -184,51 +173,40 @@ int value_not_zero(ir_node *blk, ir_node *n, ir_node **confirm) {
  * - A SymConst(entity) is NEVER a NULL pointer
  * - Confirms are evaluated
  */
-int value_not_null(ir_node *blk, ir_node *n, ir_node **confirm) {
-       ir_op *op;
+int value_not_null(const ir_node *n, ir_node_cnst_ptr *confirm) {
+       tarval *tv;
 
-       if (get_irg_pinned(get_irn_irg(blk)) != op_pin_state_pinned)
-               blk = NULL;
        *confirm = NULL;
-       n  = skip_Cast(n);
+       n  = skip_Cast_const(n);
+
+       tv = value_of(n);
+       if (tarval_is_constant(tv) && ! tarval_is_null(tv))
+               return 1;
 
-       op = get_irn_op(n);
        assert(mode_is_reference(get_irn_mode(n)));
        if (get_opt_sel_based_null_check_elim()) {
                /* skip all Sel nodes and Cast's */
-               while (op == op_Sel) {
+               while (is_Sel(n)) {
                        n = skip_Cast(get_Sel_ptr(n));
-                       op = get_irn_op(n);
                }
        }
-       if (op == op_SymConst && get_SymConst_kind(n) == symconst_addr_ent)
+       if (is_Global(n)) {
+               /* global references are never NULL */
+               return 1;
+       } else if (n == get_irg_frame(current_ir_graph)) {
+               /* local references are never NULL */
                return 1;
-       if (op == op_Const) {
-               tarval *tv = get_Const_tarval(n);
-
-               if (tv != tarval_bad && classify_tarval(tv) != TV_CLASSIFY_NULL)
-                       return 1;
        } else {
+               /* check for more Confirms */
                for (; is_Confirm(n); n = skip_Cast(get_Confirm_value(n))) {
-                       unsigned long region = get_Confirm_region(n);
+                       if (get_Confirm_cmp(n) == pn_Cmp_Lg) {
+                               ir_node *bound = get_Confirm_bound(n);
+                               tarval  *tv    = value_of(bound);
 
-                       /* check if it's legal to use this confirm. */
-                       if (region != 0) {
-                               ir_node *curr_blk = get_irn_n(n, -1);
-
-                               /* this confirm is bound to a region. */
-                               if (! blk)
-                                       continue;
-
-                               if (get_Block_exc_region(curr_blk) != region)
-                                       continue;
-                               /* all went fine, the current Confirm belongs to the same region */
-                       }
-
-                       if (get_Confirm_cmp(n) == pn_Cmp_Lg &&
-                               classify_Const(get_Confirm_bound(n)) == CNST_NULL) {
+                               if (tarval_is_null(tv)) {
                                        *confirm = n;
                                        return 1;
+                               }
                        }
                }
        }
@@ -244,8 +222,24 @@ value_classify_sign classify_value_sign(ir_node *n) {
        tarval *tv, *c;
        ir_mode *mode;
        pn_Cmp cmp, ncmp;
+       int negate = 1;
 
-       if (get_irn_op(n) != op_Confirm)
+       for (;;) {
+               ir_opcode code = get_irn_opcode(n);
+
+               switch (code) {
+               case iro_Minus:
+                       negate *= -1;
+                       n = get_Minus_op(n);
+                       continue;
+               case iro_Confirm:
+                       break;
+               default:
+                       return value_classified_unknown;
+               }
+               break;
+       }
+       if (!is_Confirm(n))
                return value_classified_unknown;
 
        tv  = value_of(get_Confirm_bound(n));
@@ -287,7 +281,7 @@ value_classify_sign classify_value_sign(ir_node *n) {
                        return value_classified_unknown;
 
                /* yep, negative */
-               return value_classified_negative;
+               return value_classified_negative * negate;
 
        case pn_Cmp_Ge:
                /*
@@ -318,7 +312,7 @@ value_classify_sign classify_value_sign(ir_node *n) {
                }
 
                /* yep, positive */
-               return value_classified_positive;
+               return value_classified_positive * negate;
 
        default:
                return value_classified_unknown;
@@ -608,26 +602,6 @@ static int is_transitive(pn_Cmp pnc) {
        return (pn_Cmp_False < pnc && pnc < pn_Cmp_Lg);
 }  /* is_transitive */
 
-/**
- * returns a confirm node starting from n that is allowed to be used in the exception
- * region.
- *
- * @param region  the exception region
- * @param n       the node
- */
-static ir_node *get_allowed_confirm(unsigned long region, ir_node *n) {
-       for (; is_Confirm(n); n = get_Confirm_value(n)) {
-               unsigned long reg = get_Confirm_region(n);
-
-               if (reg == 0 || reg == region) {
-                       /* found an allowed Confirm */
-                       return n;
-               }
-
-       }
-       return NULL;
-}  /* get_allowed_confirm */
-
 /**
  * Return the value of a Cmp if one or both predecessors
  * are Confirm nodes.
@@ -638,27 +612,23 @@ static ir_node *get_allowed_confirm(unsigned long region, ir_node *n) {
  * @param pnc    the compare relation
  */
 tarval *computed_value_Cmp_Confirm(ir_node *cmp, ir_node *left, ir_node *right, pn_Cmp pnc) {
-       ir_node       *l_bound, *confirm;
-       pn_Cmp        l_pnc, res_pnc, neg_pnc;
-       interval_t    l_iv, r_iv;
-       tarval        *tv;
-       ir_mode       *mode;
-       unsigned long region;
-
-       /* Cmp is pinned, so this always gives the right exception region. */
-       region = get_Block_exc_region(get_nodes_block(cmp));
+       ir_node         *l_bound;
+       pn_Cmp          l_pnc, res_pnc, neg_pnc;
+       interval_t      l_iv, r_iv;
+       tarval          *tv;
+       ir_mode         *mode;
 
-       if ((confirm = get_allowed_confirm(region, right)) != NULL) {
+       if (is_Confirm(right)) {
                /* we want the Confirm on the left side */
+               ir_node *t = right;
                right = left;
-               left  = confirm;
+               left  = t;
 
                pnc = get_inversed_pnc(pnc);
-       } else if ((confirm = get_allowed_confirm(region, left)) != NULL) {
-               left = confirm;
-       } else {
-               /* no Confirm on either one side, finish */
-               return tarval_bad;
+       } else if (! is_Confirm(left)) {
+               /* nothing more found */
+               tv = tarval_bad;
+               goto check_null_case;
        }
 
        /* ok, here at least left is a Confirm, right might be */
@@ -791,6 +761,24 @@ tarval *computed_value_Cmp_Confirm(ir_node *cmp, ir_node *left, ir_node *right,
                        get_interval(&l_iv, l_bound, l_pnc),
                        get_interval_from_tv(&r_iv, tv),
                        pnc);
+       } else {
+check_null_case:
+               /* check some other cases */
+               if ((pnc == pn_Cmp_Eq || pnc == pn_Cmp_Lg) &&
+                       is_Const(right) && is_Const_null(right)) {
+                       /* for == 0 or != 0 we have some special tools */
+                       ir_mode       *mode = get_irn_mode(left);
+                       const ir_node *dummy;
+                       if (mode_is_reference(mode)) {
+                               if (value_not_null(left, &dummy)) {
+                                       tv = pnc == pn_Cmp_Eq ? tarval_b_false : tarval_b_true;
+                               }
+                       } else {
+                               if (value_not_zero(left, &dummy)) {
+                                       tv = pnc == pn_Cmp_Eq ? tarval_b_false : tarval_b_true;
+                               }
+                       }
+               }
        }
 
        if (tv != tarval_bad)