public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [COMMITTED] [range-op] Restrict division by power of 2 optimization to positive numbers.
@ 2022-11-07 11:42 Aldy Hernandez
  2022-11-08 11:50 ` [PATCH] testsuite: Fix up pr107541.c test Jakub Jelinek
  0 siblings, 1 reply; 3+ messages in thread
From: Aldy Hernandez @ 2022-11-07 11:42 UTC (permalink / raw)
  To: GCC patches; +Cc: Andrew MacLeod, Aldy Hernandez

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


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH] testsuite: Fix up pr107541.c test
  2022-11-07 11:42 [COMMITTED] [range-op] Restrict division by power of 2 optimization to positive numbers Aldy Hernandez
@ 2022-11-08 11:50 ` Jakub Jelinek
  2022-11-13  0:15   ` Jeff Law
  0 siblings, 1 reply; 3+ messages in thread
From: Jakub Jelinek @ 2022-11-08 11:50 UTC (permalink / raw)
  To: Aldy Hernandez; +Cc: GCC patches, Andrew MacLeod

On Mon, Nov 07, 2022 at 12:42:38PM +0100, Aldy Hernandez via Gcc-patches wrote:
> 	* gcc.dg/tree-ssa/pr107541.c: New test.

The test fails when long is 32-bit rather than 64-bit (say x86_64 with
RUNTESTFLAGS='--target_board=unix\{-m32,-m64\} tree-ssa.exp=pr107541.c'
).
I've tweaked it to use long long so it passes even on the 32-bit
targets, and added an early out for weirdo targets because I think
the test assumes the usual 1/2/4/8 bytes sizes for char/short/int/long long.

Tested on x86_64-linux, ok for trunk?

2022-11-08  Jakub Jelinek  <jakub@redhat.com>

	PR tree-optimization/107541
	* gcc.dg/tree-ssa/pr107541.c (c): Use long long type rather than long.
	(main): Punt if sizeof short isn't 2, or int 4, or long long 8.

--- gcc/testsuite/gcc.dg/tree-ssa/pr107541.c.jj	2022-11-07 15:12:24.519022064 +0100
+++ gcc/testsuite/gcc.dg/tree-ssa/pr107541.c	2022-11-08 12:45:12.926718243 +0100
@@ -3,9 +3,11 @@
 
 unsigned char a = 1;
 char b, e;
-long c;
+long long c;
 short d;
 int main() {
+  if (sizeof (short) != 2 || sizeof (int) != 4 || sizeof (long long) != 8)
+    return 0;
   a = ~(1 && a);
   c = ~((~a / 8 | -2) & 11007578330939886389LLU);
   e = -c;


	Jakub


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] testsuite: Fix up pr107541.c test
  2022-11-08 11:50 ` [PATCH] testsuite: Fix up pr107541.c test Jakub Jelinek
@ 2022-11-13  0:15   ` Jeff Law
  0 siblings, 0 replies; 3+ messages in thread
From: Jeff Law @ 2022-11-13  0:15 UTC (permalink / raw)
  To: Jakub Jelinek, Aldy Hernandez; +Cc: GCC patches, Andrew MacLeod


On 11/8/22 04:50, Jakub Jelinek via Gcc-patches wrote:
> On Mon, Nov 07, 2022 at 12:42:38PM +0100, Aldy Hernandez via Gcc-patches wrote:
>> 	* gcc.dg/tree-ssa/pr107541.c: New test.
> The test fails when long is 32-bit rather than 64-bit (say x86_64 with
> RUNTESTFLAGS='--target_board=unix\{-m32,-m64\} tree-ssa.exp=pr107541.c'
> ).
> I've tweaked it to use long long so it passes even on the 32-bit
> targets, and added an early out for weirdo targets because I think
> the test assumes the usual 1/2/4/8 bytes sizes for char/short/int/long long.
>
> Tested on x86_64-linux, ok for trunk?
>
> 2022-11-08  Jakub Jelinek  <jakub@redhat.com>
>
> 	PR tree-optimization/107541
> 	* gcc.dg/tree-ssa/pr107541.c (c): Use long long type rather than long.
> 	(main): Punt if sizeof short isn't 2, or int 4, or long long 8.

OK

jeff



^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2022-11-13  0:15 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-11-07 11:42 [COMMITTED] [range-op] Restrict division by power of 2 optimization to positive numbers Aldy Hernandez
2022-11-08 11:50 ` [PATCH] testsuite: Fix up pr107541.c test Jakub Jelinek
2022-11-13  0:15   ` Jeff Law

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).