public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: "bin.cheng" <bin.cheng@linux.alibaba.com>
To: "GCC Patches" <gcc-patches@gcc.gnu.org>
Subject: [PATCH PR100740]Fix overflow check in simplifying exit cond comparing two IVs.
Date: Tue, 01 Jun 2021 19:15:00 +0800	[thread overview]
Message-ID: <b348cd70-f131-4efc-b980-95ae68bd4a8e.bin.cheng@linux.alibaba.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 180 bytes --]

Hi,
As described in patch summary, this fixes the wrong code issue by adding overflow-ness
check for iv1.step - iv2.step.

Bootstrap and test on x86_64.  Any comments?

Thanks,
bin

[-- Attachment #2: pr100740-20210525.txt --]
[-- Type: application/octet-stream, Size: 2700 bytes --]

commit 71454637909401501ec0fa268f83db0bc8ffbc03
Author: Bin Cheng <bin.cheng@linux.alibaba.com>
Date:   Fri May 28 16:49:54 2021 +0800

    Fix overflow check in simplifying exit cond comparing two IVs.
    
    We should also check that iv1.step - iv2.step doesn't overflow,
    in addition to overflowness of the two IVs.
    
    gcc:
            PR tree-optimization/100740
            * tree-ssa-loop-niter.c (number_of_iterations_cond): Check
              overflowness for subtract operation of the two IVs.
    
    gcc/testsuite:
            * gcc.c-torture/execute/pr100740.c

diff --git a/gcc/testsuite/gcc.c-torture/execute/pr100740.c b/gcc/testsuite/gcc.c-torture/execute/pr100740.c
new file mode 100644
index 00000000000..8fcdaffef3b
--- /dev/null
+++ b/gcc/testsuite/gcc.c-torture/execute/pr100740.c
@@ -0,0 +1,11 @@
+/* PR tree-optimization/100740 */
+
+unsigned a, b;
+int main() {
+  unsigned c = 0;
+  for (a = 0; a < 2; a++)
+    for (b = 0; b < 2; b++)
+      if (++c < a)
+        __builtin_abort ();
+  return 0;
+}
diff --git a/gcc/tree-ssa-loop-niter.c b/gcc/tree-ssa-loop-niter.c
index 325bd978609..45c8d99c43d 100644
--- a/gcc/tree-ssa-loop-niter.c
+++ b/gcc/tree-ssa-loop-niter.c
@@ -1782,7 +1782,9 @@ number_of_iterations_cond (class loop *loop,
      provided that either below condition is satisfied:
 
        a) the test is NE_EXPR;
-       b) iv0.step - iv1.step is integer and iv0/iv1 don't overflow.
+       b) no overlfow happens during simplification;
+	  - iv0 and iv1 don't overflow.
+	  - iv0.step - iv1.step is integer and doesn't overflow.
 
      This rarely occurs in practice, but it is simple enough to manage.  */
   if (!integer_zerop (iv0->step) && !integer_zerop (iv1->step))
@@ -1790,14 +1792,27 @@ number_of_iterations_cond (class loop *loop,
       tree step_type = POINTER_TYPE_P (type) ? sizetype : type;
       tree step = fold_binary_to_constant (MINUS_EXPR, step_type,
 					   iv0->step, iv1->step);
+      if (code != NE_EXPR)
+	{
+	  if (TREE_CODE (step) != INTEGER_CST)
+	    return false;
+
+	  if (!iv0->no_overflow || !iv1->no_overflow)
+	    return false;
+
+	  bool wrap_p = TYPE_OVERFLOW_WRAPS (step_type);
+	  if (wrap_p)
+	    {
+	      tree t = fold_binary_to_constant (GE_EXPR, step_type,
+						iv0->step, iv1->step);
+	      wrap_p = integer_zerop (t);
+	    }
+	  if (wrap_p)
+	    return false;
+	}
 
       /* No need to check sign of the new step since below code takes care
 	 of this well.  */
-      if (code != NE_EXPR
-	  && (TREE_CODE (step) != INTEGER_CST
-	      || !iv0->no_overflow || !iv1->no_overflow))
-	return false;
-
       iv0->step = step;
       if (!POINTER_TYPE_P (type))
 	iv0->no_overflow = false;

             reply	other threads:[~2021-06-01 11:15 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-06-01 11:15 bin.cheng [this message]
2021-06-02  7:27 ` Richard Biener
2021-06-06 10:01   ` Bin.Cheng
2021-06-07 14:35     ` Richard Biener
2021-07-01 12:19       ` Richard Biener
2021-07-02  1:37         ` Bin.Cheng
2021-07-02  7:57           ` Richard Biener

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=b348cd70-f131-4efc-b980-95ae68bd4a8e.bin.cheng@linux.alibaba.com \
    --to=bin.cheng@linux.alibaba.com \
    --cc=gcc-patches@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).