From: Aldy Hernandez <aldyh@redhat.com>
To: GCC patches <gcc-patches@gcc.gnu.org>
Cc: Andrew MacLeod <amacleod@redhat.com>, Aldy Hernandez <aldyh@redhat.com>
Subject: [COMMITTED] [range-op] Restrict division by power of 2 optimization to positive numbers.
Date: Mon, 7 Nov 2022 12:42:38 +0100 [thread overview]
Message-ID: <20221107114238.663927-1-aldyh@redhat.com> (raw)
The problem here is that we are transforming a division by a power of
2 into a right shift, and using this to shift the maybe nonzero bits.
This gives the wrong result when the number being divided is negative.
In the testcase we are dividing this by 8:
[irange] int [-256, -255] NONZERO 0xffffff01
and coming up with:
[irange] int [-32, -31] NONZERO 0xffffffe0
The maybe nonzero bits are wrong as -31 has the LSB set (0xffffffe1)
whereas the bitmask says the lower 4 bits are off.
PR tree-optimization/107541
gcc/ChangeLog:
* range-op.cc (operator_div::fold_range): Restrict power of 2
optimization to positive numbers.
gcc/testsuite/ChangeLog:
* gcc.dg/tree-ssa/pr107541.c: New test.
---
gcc/range-op.cc | 4 +++-
gcc/testsuite/gcc.dg/tree-ssa/pr107541.c | 16 ++++++++++++++++
2 files changed, 19 insertions(+), 1 deletion(-)
create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr107541.c
diff --git a/gcc/range-op.cc b/gcc/range-op.cc
index 5e94c3d2282..2b5db0cac85 100644
--- a/gcc/range-op.cc
+++ b/gcc/range-op.cc
@@ -1953,7 +1953,9 @@ operator_div::fold_range (irange &r, tree type,
return true;
tree t;
- if (rh.singleton_p (&t))
+ if (code == TRUNC_DIV_EXPR
+ && rh.singleton_p (&t)
+ && !wi::neg_p (lh.lower_bound ()))
{
wide_int wi = wi::to_wide (t);
int shift = wi::exact_log2 (wi);
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr107541.c b/gcc/testsuite/gcc.dg/tree-ssa/pr107541.c
new file mode 100644
index 00000000000..475142186b5
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/pr107541.c
@@ -0,0 +1,16 @@
+// { dg-do run }
+// { dg-options "-O1" }
+
+unsigned char a = 1;
+char b, e;
+long c;
+short d;
+int main() {
+ a = ~(1 && a);
+ c = ~((~a / 8 | -2) & 11007578330939886389LLU);
+ e = -c;
+ d = ~c / e;
+ if (d < 2000)
+ __builtin_abort();
+ return 0;
+}
--
2.38.1
next reply other threads:[~2022-11-07 11:42 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-11-07 11:42 Aldy Hernandez [this message]
2022-11-08 11:50 ` [PATCH] testsuite: Fix up pr107541.c test Jakub Jelinek
2022-11-13 0:15 ` Jeff Law
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=20221107114238.663927-1-aldyh@redhat.com \
--to=aldyh@redhat.com \
--cc=amacleod@redhat.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).