public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Richard Biener <rguenth@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc r14-9293] tree-optimization/114203 - wrong CLZ niter computation
Date: Mon,  4 Mar 2024 10:45:28 +0000 (GMT)	[thread overview]
Message-ID: <20240304104528.85B683858C32@sourceware.org> (raw)

https://gcc.gnu.org/g:cde50296a19b109909089b91d532d2c8455f5f10

commit r14-9293-gcde50296a19b109909089b91d532d2c8455f5f10
Author: Richard Biener <rguenther@suse.de>
Date:   Mon Mar 4 10:38:31 2024 +0100

    tree-optimization/114203 - wrong CLZ niter computation
    
    For precision less than int we apply the adjustment to make it defined
    at zero after the adjustment to make it compute CLZ rather than CTZ.
    That's wrong.
    
            PR tree-optimization/114203
            * tree-ssa-loop-niter.cc (build_cltz_expr): Apply CTZ->CLZ
            adjustment before making the result defined at zero.
    
            * gcc.dg/torture/pr114203.c: New testcase.

Diff:
---
 gcc/testsuite/gcc.dg/torture/pr114203.c | 21 +++++++++++++++++++++
 gcc/tree-ssa-loop-niter.cc              |  7 +++----
 2 files changed, 24 insertions(+), 4 deletions(-)

diff --git a/gcc/testsuite/gcc.dg/torture/pr114203.c b/gcc/testsuite/gcc.dg/torture/pr114203.c
new file mode 100644
index 00000000000..0ef6279942a
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/torture/pr114203.c
@@ -0,0 +1,21 @@
+/* { dg-do run } */
+
+int __attribute__((noipa))
+foo (unsigned char b)
+{
+  int c = 0;
+
+  while (b) {
+      b >>= 1;
+      c++;
+  }
+
+  return c;
+}
+
+int main()
+{
+  if (foo(0) != 0)
+    __builtin_abort ();
+  return 0;
+}
diff --git a/gcc/tree-ssa-loop-niter.cc b/gcc/tree-ssa-loop-niter.cc
index 038e4331661..c6d010f6d89 100644
--- a/gcc/tree-ssa-loop-niter.cc
+++ b/gcc/tree-ssa-loop-niter.cc
@@ -2288,6 +2288,9 @@ build_cltz_expr (tree src, bool leading, bool define_at_zero)
 	src = fold_convert (unsigned_type_node, src);
 
       call = build_call_expr (fn, 1, src);
+      if (leading && prec < i_prec)
+	call = fold_build2 (MINUS_EXPR, integer_type_node, call,
+			    build_int_cst (integer_type_node, i_prec - prec));
       if (define_at_zero)
 	{
 	  tree is_zero = fold_build2 (NE_EXPR, boolean_type_node, src,
@@ -2295,10 +2298,6 @@ build_cltz_expr (tree src, bool leading, bool define_at_zero)
 	  call = fold_build3 (COND_EXPR, integer_type_node, is_zero, call,
 			      build_int_cst (integer_type_node, prec));
 	}
-
-      if (leading && prec < i_prec)
-	call = fold_build2 (MINUS_EXPR, integer_type_node, call,
-			    build_int_cst (integer_type_node, i_prec - prec));
     }
 
   return call;

                 reply	other threads:[~2024-03-04 10:45 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=20240304104528.85B683858C32@sourceware.org \
    --to=rguenth@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).