public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Jakub Jelinek <jakub@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc r11-5491] tree-ssanames: Allow non-SSA_NAME arguments to get_range_info
Date: Fri, 27 Nov 2020 10:39:51 +0000 (GMT)	[thread overview]
Message-ID: <20201127103951.F0124385780D@sourceware.org> (raw)

https://gcc.gnu.org/g:83325a9db8348c3ba5e9c871d106ee1b76d40526

commit r11-5491-g83325a9db8348c3ba5e9c871d106ee1b76d40526
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Fri Nov 27 11:38:31 2020 +0100

    tree-ssanames: Allow non-SSA_NAME arguments to get_range_info
    
    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.

Diff:
---
 gcc/match.pd                | 60 ++++++++++++++++++---------------------------
 gcc/tree-scalar-evolution.c | 18 +++-----------
 gcc/tree-ssa-strlen.c       | 35 +++++++++++---------------
 gcc/tree-ssanames.c         | 19 ++++++++++----
 4 files changed, 56 insertions(+), 76 deletions(-)

diff --git a/gcc/match.pd b/gcc/match.pd
index b6dfc24af88..1726c186daa 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -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
    ))))
 
diff --git a/gcc/tree-scalar-evolution.c b/gcc/tree-scalar-evolution.c
index edab778277b..2c7923d742b 100644
--- a/gcc/tree-scalar-evolution.c
+++ b/gcc/tree-scalar-evolution.c
@@ -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))
diff --git a/gcc/tree-ssa-strlen.c b/gcc/tree-ssa-strlen.c
index a5e78a89e65..f365c2d0c45 100644
--- a/gcc/tree-ssa-strlen.c
+++ b/gcc/tree-ssa-strlen.c
@@ -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;
diff --git a/gcc/tree-ssanames.c b/gcc/tree-ssanames.c
index ec4681f85cb..1b9dcececb4 100644
--- a/gcc/tree-ssanames.c
+++ b/gcc/tree-ssanames.c
@@ -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


                 reply	other threads:[~2020-11-27 10:39 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20201127103951.F0124385780D@sourceware.org \
    --to=jakub@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).