tree-ssanames: Allow non-SSA_NAME arguments to get_range_info
authorJakub Jelinek <jakub@redhat.com>
Fri, 27 Nov 2020 10:38:31 +0000 (11:38 +0100)
committerJakub Jelinek <jakub@redhat.com>
Fri, 27 Nov 2020 10:38:31 +0000 (11:38 +0100)
My recent match.pd change required quite a lot of code due to the separate
need to handle INTEGER_CSTs and SSA_NAMEs, and after all, I didn't even
handle one case there, when in x * y / y the x is INTEGER_CST and y is
SSA_NAME.
The following patch allows to simplify it, by allowing non-SSA_NAME argument
to get_range_info, for INTEGER_CSTs it will return VR_RANGE with *min == *max
equal to the constnat, and for non-INTEGER_CST/SSA_NAMEs it will just return
VR_VARYING.

This allows not to simplify just the match.pd, but some other spots too.

2020-11-27  Jakub Jelinek  <jakub@redhat.com>

* tree-ssanames.c (get_range_info): Handle INTEGER_CST by returning
VR_RANGE with both *min and *max set to the wide_int value of the
INTEGER_CST.  Return VR_VARYING for non-SSA_NAMEs.
* match.pd ((t * 2) / 2) -> t): Handle also @0 being INTEGER_CST.
Simplify by calling get_range_info on everything.
* tree-ssa-strlen.c (maybe_diag_stxncpy_trunc): Simplify by calling
get_range_info on everything.
* tree-scalar-evolution.c (iv_can_overflow_p): Likewise.

gcc/match.pd
gcc/tree-scalar-evolution.c
gcc/tree-ssa-strlen.c
gcc/tree-ssanames.c

index b6dfc24..1726c18 100644 (file)
@@ -654,42 +654,30 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
    (if (TYPE_OVERFLOW_UNDEFINED (type))
     @0
 #if GIMPLE
-    (if (TREE_CODE (@0) == SSA_NAME
-        && (TREE_CODE (@1) == SSA_NAME || TREE_CODE (@1) == INTEGER_CST))
-     (with
-      {
-       bool overflowed = true;
-       wide_int wmin0, wmax0;
-       if (get_range_info (@0, &wmin0, &wmax0) == VR_RANGE)
-         {
-           /* If the multiplication can't overflow/wrap around, then
-              it can be optimized too.  */
-           wide_int wmin1, wmax1;
-           wi::overflow_type min_ovf, max_ovf;
-           if (TREE_CODE (@1) == INTEGER_CST)
-             {
-               wmin1 = wi::to_wide (@1);
-               wi::mul (wmin0, wmin1, TYPE_SIGN (type), &min_ovf);
-               wi::mul (wmax0, wmin1, TYPE_SIGN (type), &max_ovf);
-               if (min_ovf == wi::OVF_NONE && max_ovf == wi::OVF_NONE)
-                 overflowed = false;
-             }
-           else if (get_range_info (@1, &wmin1, &wmax1) == VR_RANGE)
-             {
-               wi::mul (wmin0, wmin1, TYPE_SIGN (type), &min_ovf);
-               wi::mul (wmax0, wmax1, TYPE_SIGN (type), &max_ovf);
-               if (min_ovf == wi::OVF_NONE && max_ovf == wi::OVF_NONE)
-                 {
-                   wi::mul (wmin0, wmax1, TYPE_SIGN (type), &min_ovf);
-                   wi::mul (wmax0, wmin1, TYPE_SIGN (type), &max_ovf);
-                   if (min_ovf == wi::OVF_NONE && max_ovf == wi::OVF_NONE)
-                     overflowed = false;
-                 }
-             }
-         }
-      }
-     (if (!overflowed)
-      @0)))
+    (with
+     {
+       bool overflowed = true;
+       wide_int wmin0, wmax0, wmin1, wmax1;
+       if (INTEGRAL_TYPE_P (type)
+          && get_range_info (@0, &wmin0, &wmax0) == VR_RANGE
+          && get_range_info (@1, &wmin1, &wmax1) == VR_RANGE)
+        {
+          /* If the multiplication can't overflow/wrap around, then
+             it can be optimized too.  */
+          wi::overflow_type min_ovf, max_ovf;
+          wi::mul (wmin0, wmin1, TYPE_SIGN (type), &min_ovf);
+          wi::mul (wmax0, wmax1, TYPE_SIGN (type), &max_ovf);
+          if (min_ovf == wi::OVF_NONE && max_ovf == wi::OVF_NONE)
+            {
+              wi::mul (wmin0, wmax1, TYPE_SIGN (type), &min_ovf);
+              wi::mul (wmax0, wmin1, TYPE_SIGN (type), &max_ovf);
+              if (min_ovf == wi::OVF_NONE && max_ovf == wi::OVF_NONE)
+                overflowed = false;
+            }
+        }
+     }
+    (if (!overflowed)
+     @0))
 #endif
    ))))
 
index edab778..2c7923d 100644 (file)
@@ -3043,22 +3043,12 @@ iv_can_overflow_p (class loop *loop, tree type, tree base, tree step)
   if (integer_zerop (step))
     return false;
 
-  if (TREE_CODE (base) == INTEGER_CST)
-    base_min = base_max = wi::to_wide (base);
-  else if (TREE_CODE (base) == SSA_NAME
-          && INTEGRAL_TYPE_P (TREE_TYPE (base))
-          && get_range_info (base, &base_min, &base_max) == VR_RANGE)
-    ;
-  else
+  if (!INTEGRAL_TYPE_P (TREE_TYPE (base))
+      || get_range_info (base, &base_min, &base_max) != VR_RANGE)
     return true;
 
-  if (TREE_CODE (step) == INTEGER_CST)
-    step_min = step_max = wi::to_wide (step);
-  else if (TREE_CODE (step) == SSA_NAME
-          && INTEGRAL_TYPE_P (TREE_TYPE (step))
-          && get_range_info (step, &step_min, &step_max) == VR_RANGE)
-    ;
-  else
+  if (!INTEGRAL_TYPE_P (TREE_TYPE (step))
+      || get_range_info (step, &step_min, &step_max) != VR_RANGE)
     return true;
 
   if (!get_max_loop_iterations (loop, &nit))
index a5e78a8..f365c2d 100644 (file)
@@ -3038,31 +3038,24 @@ maybe_diag_stxncpy_trunc (gimple_stmt_iterator gsi, tree src, tree cnt)
 
   wide_int cntrange[2];
 
-  if (TREE_CODE (cnt) == INTEGER_CST)
-    cntrange[0] = cntrange[1] = wi::to_wide (cnt);
-  else if (TREE_CODE (cnt) == SSA_NAME)
+  // FIXME: Use range_query instead of global ranges.
+  enum value_range_kind rng = get_range_info (cnt, cntrange, cntrange + 1);
+  if (rng == VR_RANGE)
+    ;
+  else if (rng == VR_ANTI_RANGE)
     {
-      // FIXME: Use range_query instead of global ranges.
-      enum value_range_kind rng = get_range_info (cnt, cntrange, cntrange + 1);
-      if (rng == VR_RANGE)
-       ;
-      else if (rng == VR_ANTI_RANGE)
-       {
-         wide_int maxobjsize = wi::to_wide (TYPE_MAX_VALUE (ptrdiff_type_node));
+      wide_int maxobjsize = wi::to_wide (TYPE_MAX_VALUE (ptrdiff_type_node));
 
-         if (wi::ltu_p (cntrange[1], maxobjsize))
-           {
-             cntrange[0] = cntrange[1] + 1;
-             cntrange[1] = maxobjsize;
-           }
-         else
-           {
-             cntrange[1] = cntrange[0] - 1;
-             cntrange[0] = wi::zero (TYPE_PRECISION (TREE_TYPE (cnt)));
-           }
+      if (wi::ltu_p (cntrange[1], maxobjsize))
+       {
+         cntrange[0] = cntrange[1] + 1;
+         cntrange[1] = maxobjsize;
        }
       else
-       return false;
+       {
+         cntrange[1] = cntrange[0] - 1;
+         cntrange[0] = wi::zero (TYPE_PRECISION (TREE_TYPE (cnt)));
+       }
     }
   else
     return false;
index ec4681f..1b9dcec 100644 (file)
@@ -420,21 +420,30 @@ set_range_info (tree name, const value_range &vr)
    is used to determine if MIN and MAX are valid values.  */
 
 enum value_range_kind
-get_range_info (const_tree name, wide_int *min, wide_int *max)
+get_range_info (const_tree expr, wide_int *min, wide_int *max)
 {
-  gcc_assert (!POINTER_TYPE_P (TREE_TYPE (name)));
+  gcc_assert (!POINTER_TYPE_P (TREE_TYPE (expr)));
   gcc_assert (min && max);
-  range_info_def *ri = SSA_NAME_RANGE_INFO (name);
+  if (TREE_CODE (expr) == INTEGER_CST)
+    {
+      *min = wi::to_wide (expr);
+      *max = *min;
+      return VR_RANGE;
+    }
+  if (TREE_CODE (expr) != SSA_NAME)
+    return VR_VARYING;
+
+  range_info_def *ri = SSA_NAME_RANGE_INFO (expr);
 
   /* Return VR_VARYING for SSA_NAMEs with NULL RANGE_INFO or SSA_NAMEs
      with integral types width > 2 * HOST_BITS_PER_WIDE_INT precision.  */
-  if (!ri || (GET_MODE_PRECISION (SCALAR_INT_TYPE_MODE (TREE_TYPE (name)))
+  if (!ri || (GET_MODE_PRECISION (SCALAR_INT_TYPE_MODE (TREE_TYPE (expr)))
              > 2 * HOST_BITS_PER_WIDE_INT))
     return VR_VARYING;
 
   *min = ri->get_min ();
   *max = ri->get_max ();
-  return SSA_NAME_RANGE_TYPE (name);
+  return SSA_NAME_RANGE_TYPE (expr);
 }
 
 /* Gets range information corresponding to ssa_name NAME and stores it