updated Header
[libfirm] / ir / opt / opt_confirms.c
index 65054e1..847e320 100644 (file)
@@ -1,3 +1,22 @@
+/*
+ * Copyright (C) 1995-2007 University of Karlsruhe.  All right reserved.
+ *
+ * This file is part of libFirm.
+ *
+ * This file may be distributed and/or modified under the terms of the
+ * GNU General Public License version 2 as published by the Free Software
+ * Foundation and appearing in the file LICENSE.GPL included in the
+ * packaging of this file.
+ *
+ * Licensees holding valid libFirm Professional Edition licenses may use
+ * this file in accordance with the libFirm Commercial License.
+ * Agreement provided with the Software.
+ *
+ * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
+ * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE.
+ */
+
 /*
  * Project:     libFIRM
  * File name:   ir/opt/opt_confirms.c
  * Created:
  * CVS-ID:      $Id$
  * Copyright:   (c) 1998-2005 Universität Karlsruhe
- * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
  */
 
 #ifdef HAVE_CONFIG_H
 # include "config.h"
 #endif
 
+#undef DEBUG_CONFIRM
+
 #include "tv_t.h"
 #include "iropt_t.h"
 #include "iropt_dbg.h"
 #include "opt_confirms.h"
+#include "irflag_t.h"
+#include "irprintf.h"
 
 enum range_tags {
   MIN_INCLUDED = 0x00,  /**< [min, ... */
@@ -40,31 +62,65 @@ typedef struct _interval_t {
   unsigned char flags;  /**< border flags */
 } interval_t;
 
-/**
+#ifdef DEBUG_CONFIRM
+
+#define compare_iv(l_iv, r_iv, pnc)            compare_iv_dbg(l_iv, r_iv, pnc)
+
+/* forward */
+static tarval *compare_iv_dbg(const interval_t *l_iv, const interval_t *r_iv, pn_Cmp pnc);
+
+/* triangle */
+#define DBG_OUT_TR(l_pnc, l_bound, r_pnc, r_bound, pnc, v) \
+  ir_printf("In %e:\na %= %n && b %= %n  ==>  a %= b == %s\n", \
+    get_irg_entity(current_ir_graph), \
+    l_pnc, l_bound, r_pnc, r_bound, pnc, v);
+
+/* right side */
+#define DBG_OUT_R(r_pnc, r_bound, left, pnc, right, v) \
+  ir_printf("In %e:\na %= %n ==>  %n %= %n == %s\n", \
+    get_irg_entity(current_ir_graph), \
+    r_pnc, r_bound, left, pnc, right, v);
+
+/* left side */
+#define DBG_OUT_L(l_pnc, l_bound, left, pnc, right, v) \
+  ir_printf("In %e:\na %= %n ==>  %n %= %n == %s\n", \
+    get_irg_entity(current_ir_graph), \
+    l_pnc, l_bound, left, pnc, right, v);
+
+#else
+
+#define DBG_OUT_TR(l_pnc, l_bound, r_pnc, r_bound, pnc, v)
+#define DBG_OUT_R(r_pnc, r_bound, left, pnc, right, v)
+#define DBG_OUT_L(l_pnc, l_bound, left, pnc, right, v)
+
+#endif /* DEBUG_CONFIRM */
+
+/*
  * Check, if the value of a node is != 0.
  *
  * This is a often needed case, so we handle here Confirm
  * nodes too.
  */
-int value_not_zero(ir_node *n)
+int value_not_zero(ir_node *n, ir_node **confirm)
 {
-#define RET_ON(x)  if (x) return 1; break
+#define RET_ON(x)  if (x) { *confirm = n; return 1; }; break
 
   tarval *tv;
   ir_mode *mode = get_irn_mode(n);
   pn_Cmp pnc;
 
+  *confirm = NULL;
   while (get_irn_op(n) == op_Confirm) {
     /*
      * Note: A Confirm is never after a Const. So,
-     * we simply can check the bound for beeing a Const
+     * we simply can check the bound for being a Const
      * without the fear that is might be hidden by a further Confirm.
      */
     tv = value_of(get_Confirm_bound(n));
     if (tv == tarval_bad)
       return 0;
 
-    pnc  = tarval_cmp(tv, get_mode_null(mode));
+    pnc = tarval_cmp(tv, get_mode_null(mode));
 
     /*
      * Beware: C might by a NaN. It is not clear, what we should do
@@ -105,25 +161,67 @@ int value_not_zero(ir_node *n)
   return (pnc != pn_Cmp_Eq) && (pnc != pn_Cmp_Uo);
 
 #undef RET_ON
-}
+}  /* value_not_zero */
+
+/*
+ * Check, if the value of a node cannot represent a NULL pointer.
+ *
+ * - Casts are skipped
+ * - If sel_based_null_check_elim is enabled, all
+ *   Sel nodes can be skipped.
+ * - A SymConst(entity) is NEVER a NULL pointer
+ * - Confirms are evaluated
+ */
+int value_not_null(ir_node *n, ir_node **confirm)
+{
+  ir_op *op;
+
+  *confirm = NULL;
+  n  = skip_Cast(n);
+  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) {
+      n = skip_Cast(get_Sel_ptr(n));
+      op = get_irn_op(n);
+    }
+  }
+  if (op == op_SymConst && get_SymConst_kind(n) == symconst_addr_ent)
+    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 if (op == op_Confirm) {
+    if (get_Confirm_cmp(n) == pn_Cmp_Lg &&
+        classify_Const(get_Confirm_bound(n)) == CNST_NULL) {
+      *confirm = n;
+      return 1;
+    }
+  }
+  return 0;
+}  /* value_not_null */
 
 /*
  * Check, if the value of a node can be confirmed >= 0 or <= 0,
  * If the mode of the value did not honor signed zeros, else
  * check for >= 0 or < 0.
  */
-value_classify classify_value_sign(ir_node *n)
+value_classify_sign classify_value_sign(ir_node *n)
 {
   tarval *tv, *c;
   ir_mode *mode;
   pn_Cmp cmp, ncmp;
 
   if (get_irn_op(n) != op_Confirm)
-    return VALUE_UNKNOWN;
+    return value_classified_unknown;
 
   tv  = value_of(get_Confirm_bound(n));
   if (tv == tarval_bad)
-    return VALUE_UNKNOWN;
+    return value_classified_unknown;
 
   mode = get_irn_mode(n);
 
@@ -157,10 +255,10 @@ value_classify classify_value_sign(ir_node *n)
       ncmp = pn_Cmp_Le;
 
     if (cmp != (ncmp ^ pn_Cmp_Eq))
-      return VALUE_UNKNOWN;
+      return value_classified_unknown;
 
     /* yep, negative */
-    return VALUE_NEGATIVE;
+    return value_classified_negative;
 
   case pn_Cmp_Ge:
     /*
@@ -180,7 +278,7 @@ value_classify classify_value_sign(ir_node *n)
         ncmp = pn_Cmp_Ge;
 
       if (cmp != (ncmp ^ pn_Cmp_Eq))
-        return VALUE_UNKNOWN;
+        return value_classified_unknown;
     }
     else {
       c = get_mode_minus_one(mode);
@@ -188,16 +286,16 @@ value_classify classify_value_sign(ir_node *n)
       ncmp = tarval_cmp(tv, c);
 
       if (ncmp != pn_Cmp_Eq && ncmp != pn_Cmp_Gt)
-        return VALUE_UNKNOWN;
+        return value_classified_unknown;
     }
 
     /* yep, positive */
-    return VALUE_POSITIVE;
+    return value_classified_positive;
 
   default:
-    return VALUE_UNKNOWN;
+    return value_classified_unknown;
   }
-}
+}  /* classify_value_sign */
 
 /**
  * construct an interval from a value
@@ -242,14 +340,14 @@ static interval_t *get_interval_from_tv(interval_t *iv, tarval *tv)
   iv->flags = MIN_INCLUDED | MAX_INCLUDED;
 
   return iv;
-}
+}  /* get_interval_from_tv */
 
 /**
  * construct an interval from a Confirm
  *
  * @param iv     an empty interval, will be filled
  * @param bound  the bound value
- * @param pnc    the Confirm pnc relation
+ * @param pnc    the Confirm compare relation
  *
  * @return the filled interval or NULL if no interval
  *         can be created (happens only on floating point
@@ -344,7 +442,7 @@ static interval_t *get_interval(interval_t *iv, ir_node *bound, pn_Cmp pnc)
   if (iv->min != tarval_bad && iv->max != tarval_bad)
     return iv;
   return NULL;
-}
+}  /* get_interval */
 
 /**
  * Try to evaluate l_iv pnc r_iv.
@@ -357,7 +455,7 @@ static interval_t *get_interval(interval_t *iv, ir_node *bound, pn_Cmp pnc)
  *   tarval_b_true or tarval_b_false it it can be evaluated,
  *   tarval_bad else
  */
-static tarval *compare_iv(const interval_t *l_iv, const interval_t *r_iv, pn_Cmp pnc)
+static tarval *(compare_iv)(const interval_t *l_iv, const interval_t *r_iv, pn_Cmp pnc)
 {
   pn_Cmp res;
   unsigned flags;
@@ -393,6 +491,29 @@ static tarval *compare_iv(const interval_t *l_iv, const interval_t *r_iv, pn_Cmp
     /* two intervals can be compared for equality only if they are a single value */
     if (l_iv->min == l_iv->max && r_iv->min == r_iv->max)
       return tarval_cmp(l_iv->min, r_iv->min) == pn_Cmp_Eq ? tv_true : tv_false;
+
+    /* if both intervals do not intersect, it is never equal */
+    res = tarval_cmp(l_iv->max, r_iv->min);
+
+    /* b < c ==> [a,b] != [c,d] */
+    if (res == pn_Cmp_Lt)
+      return tv_false;
+
+    /* b <= c ==> [a,b) != [c,d]  AND [a,b] != (c,d] */
+    if ((l_iv->flags & MAX_EXCLUDED || r_iv->flags & MIN_EXCLUDED)
+        && (res == pn_Cmp_Eq))
+      return tv_false;
+
+    res = tarval_cmp(r_iv->max, l_iv->min);
+
+    /* d < a ==> [c,d] != [a,b] */
+    if (res == pn_Cmp_Lt)
+      return tv_false;
+
+    /* d <= a ==> [c,d) != [a,b]  AND [c,d] != (a,b] */
+    if ((r_iv->flags & MAX_EXCLUDED || l_iv->flags & MIN_EXCLUDED)
+        && (res == pn_Cmp_Eq))
+      return tv_false;
     break;
 
   case pn_Cmp_Lg:
@@ -454,21 +575,32 @@ static tarval *compare_iv(const interval_t *l_iv, const interval_t *r_iv, pn_Cmp
     return tarval_bad;
   }
   return tarval_bad;
-}
+}  /* compare_iv */
+
+/**
+ * Returns non-zero, if a given relation is transitive.
+ */
+static int is_transitive(pn_Cmp pnc) {
+  return (pn_Cmp_False < pnc && pnc < pn_Cmp_Lg);
+}  /* is_transitive */
+
 
 /**
  * Return the value of a Cmp if one or both predecessors
  * are Confirm nodes.
  *
+ * @param cmp    the Cmp node
  * @param left   the left operand of the Cmp
  * @param right  the right operand of the Cmp
+ * @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;
-  pn_Cmp     l_pnc;
+  pn_Cmp     l_pnc, res_pnc, neg_pnc;
   interval_t l_iv, r_iv;
   tarval     *tv;
+  ir_mode    *mode;
 
   if (get_irn_op(right) == op_Confirm) {
     ir_node *t;
@@ -497,30 +629,67 @@ tarval *computed_value_Cmp_Confirm(ir_node *cmp, ir_node *left, ir_node *right,
     pn_Cmp  r_pnc = get_Confirm_cmp(right);
 
     /*
-     * check for == or != can sometime be made WITHOUT constant bounds
-     * Beware of NaN's.
+     * some check can be made WITHOUT constant bounds
      */
-    if (! mode_is_float(get_irn_mode(left)) &&
-        (pnc == pn_Cmp_Eq || pnc == pn_Cmp_Lg)) {
-      /* l == r if bound(l) == bound(r) AND pnc(l) == pnc(r) == '=' */
-      if (r_bound == l_bound && r_pnc == l_pnc && r_pnc == pn_Cmp_Eq) {
-        DBG_EVAL_CONFIRM(cmp);
-        return pnc == pn_Cmp_Eq ? tarval_b_true : tarval_b_false;
+    if (r_bound == l_bound) {
+      if (is_transitive(l_pnc)) {
+        pn_Cmp r_inc_pnc = get_inversed_pnc(r_pnc);
+
+        /*
+         * triangle inequality:
+         *
+         * a CMP B && B CMP b => a CMP b, !(a ~CMP b)
+         *
+         * We handle correctly cases with some <=/>= here
+         */
+        if ((l_pnc & ~pn_Cmp_Eq) == (r_inc_pnc & ~pn_Cmp_Eq)) {
+          res_pnc = (l_pnc & ~pn_Cmp_Eq) | (l_pnc & r_inc_pnc & pn_Cmp_Eq);
+
+          if ((pnc == res_pnc) || ((pnc & ~pn_Cmp_Eq) == res_pnc)) {
+            DBG_OUT_TR(l_pnc, l_bound, r_pnc, r_bound, pnc, "true");
+            DBG_EVAL_CONFIRM(cmp);
+            return tarval_b_true;
+          }
+          else {
+            pn_Cmp neg_pnc = get_negated_pnc(pnc, get_irn_mode(left));
+
+            if ((neg_pnc == res_pnc) || ((neg_pnc & ~pn_Cmp_Eq) == res_pnc)) {
+              DBG_OUT_TR(l_pnc, l_bound, r_pnc, r_bound, pnc, "false");
+              DBG_EVAL_CONFIRM(cmp);
+              return tarval_b_false;
+            }
+          }
+        }
       }
+    }
+
+    /*
+     * Here, we check only the right Confirm, as the left Confirms are
+     * checked later anyway.
+     */
 
+    if (left == r_bound) {
       /*
-       * Here, we check only the right Confirm, as the left Confirms are
-       * checked later anyway.
+       * l == bound(r) AND pnc(r) == pnc:
+       *
+       * We know that a CMP b and check for that
        */
+      if ((r_pnc == pnc) || (r_pnc == (pnc & ~pn_Cmp_Eq))) {
+        DBG_OUT_R(r_pnc, r_bound, left, pnc, right, "true");
+        DBG_EVAL_CONFIRM(cmp);
+        return tarval_b_true;
+      }
+      /*
+       * l == bound(r) AND pnc(r) != pnc:
+       *
+       * We know that a CMP b and check for a ~CMP b
+       */
+      else {
+        mode    = get_irn_mode(left);
+        neg_pnc = get_negated_pnc(pnc, mode);
 
-      if (left == r_bound && (r_pnc == pn_Cmp_Eq || r_pnc == pn_Cmp_Lg)) {
-        /* l == bound(r) AND pnc(r) == pnc */
-        if (r_pnc == pnc) {
-          DBG_EVAL_CONFIRM(cmp);
-          return tarval_b_true;
-        }
-        /* l == bound(r) AND pnc(r) != pnc */
-        else {
+        if ((r_pnc == neg_pnc) || (r_pnc == (neg_pnc & ~pn_Cmp_Eq))) {
+          DBG_OUT_R(r_pnc, r_bound, left, pnc, right, "false");
           DBG_EVAL_CONFIRM(cmp);
           return tarval_b_false;
         }
@@ -542,19 +711,30 @@ tarval *computed_value_Cmp_Confirm(ir_node *cmp, ir_node *left, ir_node *right,
   /* from Here, check only left Confirm */
 
   /*
-   * checks for == or != can sometime be made WITHOUT constant bounds
-   * Beware of NaN's.
+   * some checks can be made WITHOUT constant bounds
    */
-  if (! mode_is_float(get_irn_mode(left)) &&
-    (pnc == pn_Cmp_Eq || pnc == pn_Cmp_Lg)) {
-    if (right == l_bound && (l_pnc == pn_Cmp_Eq || l_pnc == pn_Cmp_Lg)) {
-      /* r == bound(l) AND pnc(l) == pnc */
-      if (l_pnc == pnc) {
-        DBG_EVAL_CONFIRM(cmp);
-        return tarval_b_true;
-      }
-      /* r == bound(l) AND pnc(l) != pnc */
-      else {
+  if (right == l_bound) {
+    /*
+     * r == bound(l) AND pnc(l) == pnc:
+     *
+     * We know that a CMP b and check for that
+     */
+    if ((l_pnc == pnc) || (l_pnc == (pnc & ~pn_Cmp_Eq))) {
+      DBG_OUT_L(l_pnc, l_bound, left, pnc, right, "true");
+      DBG_EVAL_CONFIRM(cmp);
+      return tarval_b_true;
+    }
+    /*
+     * r == bound(l) AND pnc(l) is Not(pnc):
+     *
+     * We know that a CMP b and check for a ~CMP b
+     */
+    else {
+      mode = get_irn_mode(left);
+      neg_pnc = get_negated_pnc(pnc, mode);
+
+      if ((l_pnc == neg_pnc) || (l_pnc == (neg_pnc & ~pn_Cmp_Eq))) {
+        DBG_OUT_L(l_pnc, l_bound, left, pnc, right, "false");
         DBG_EVAL_CONFIRM(cmp);
         return tarval_b_false;
       }
@@ -575,18 +755,72 @@ tarval *computed_value_Cmp_Confirm(ir_node *cmp, ir_node *left, ir_node *right,
     DBG_EVAL_CONFIRM(cmp);
 
   return tv;
-}
+}  /* computed_value_Cmp_Confirm */
 
-/** For debugging. Prints an interval */
-static void print_iv(const interval_t *iv) {
+#ifdef DEBUG_CONFIRM
+/**
+ * For debugging. Prints an interval into a string.
+ *
+ * @param buf   address of a string buffer
+ * @param len   length of the string buffer
+ * @param iv    the interval
+ */
+static int iv_snprintf(char *buf, size_t len, const interval_t *iv) {
   char smin[64], smax[64];
 
-  tarval_snprintf(smin, sizeof(smin), iv->min);
-  tarval_snprintf(smax, sizeof(smax), iv->max);
+  if (iv) {
+    tarval_snprintf(smin, sizeof(smin), iv->min);
+
+    if (iv->min != iv->max || (iv->flags & (MIN_EXCLUDED|MAX_EXCLUDED))) {
+      tarval_snprintf(smax, sizeof(smax), iv->max);
+
+      return snprintf(buf, len, "%c%s, %s%c",
+              iv->flags & MIN_EXCLUDED ? '(' : '[',
+              smin, smax,
+              iv->flags & MAX_EXCLUDED ? ')' : ']'
+      );
+    }
+    else
+      return snprintf(buf, len, "%s", smin);
+  }
+  return snprintf(buf, len, "<UNKNOWN>");
+}  /* iv_snprintf */
+
+/**
+ * For debugging. Prints an interval compare.
+ *
+ * @param l_iv  the left interval
+ * @param r_iv  the right interval
+ * @param pnc   the compare relation
+ */
+static void print_iv_cmp(const interval_t *l_iv, const interval_t *r_iv, pn_Cmp pnc)
+{
+  char sl[128], sr[128];
+
+  iv_snprintf(sl, sizeof(sl), l_iv);
+  iv_snprintf(sr, sizeof(sr), r_iv);
+
+  ir_printf("%s %= %s", sl, pnc, sr);
+}  /* print_iv_cmp */
+
+/**
+ * For debugging. call *compare_iv() and prints inputs and result.
+ *
+ * @param l_iv  the left interval
+ * @param r_iv  the right interval
+ * @param pnc   the compare relation
+ */
+static tarval *compare_iv_dbg(const interval_t *l_iv, const interval_t *r_iv, pn_Cmp pnc)
+{
+  tarval *tv = (compare_iv)(l_iv, r_iv, pnc);
+
+  if (tv == tarval_bad)
+    return tv;
+
+  ir_printf("In %e:\n", get_irg_entity(current_ir_graph));
+  print_iv_cmp(l_iv, r_iv, pnc);
+  ir_printf(" = %T\n", tv);
+  return tv;
+}  /* compare_iv_dbg */
 
-  printf("%c%s, %s%c\n",
-    iv->flags & MIN_EXCLUDED ? '(' : '[',
-    smin, smax,
-    iv->flags & MAX_EXCLUDED ? ')' : ']'
-  );
-}
+#endif /* DEBUG_CONFIRM */