From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2136) id 29EEF3858D20; Mon, 7 Nov 2022 11:42:00 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 29EEF3858D20 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1667821321; bh=/lZOSMwUvVjKHxKU7wkCdfGM//q1+7+aG4jAChCIDF8=; h=From:To:Subject:Date:From; b=khJaqYSSRizXhErJ2oZVQS5Sy2a1jQaXeOKmmTwP2VVU8vkVezZsjWKf+br5s2B+w pQScc3BJsFrnKH2WLxYifq6eLKEkOEITSS+LXwpdfDUqyNw0LqXW0SeCNH/9Jt713D XHv2OmotP7ib2lW2AnI1NgvlnJHyWGLd9WhdR6Yc= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Aldy Hernandez To: gcc-cvs@gcc.gnu.org Subject: [gcc r13-3752] [range-op] Restrict division by power of 2 optimization to positive numbers. X-Act-Checkin: gcc X-Git-Author: Aldy Hernandez X-Git-Refname: refs/heads/master X-Git-Oldrev: 071d00e0faabbd45449d2e83f207fca0f8e8ef68 X-Git-Newrev: 3bff15c1c9fb3eb0bb042717e072476ec2d6d88c Message-Id: <20221107114201.29EEF3858D20@sourceware.org> Date: Mon, 7 Nov 2022 11:42:00 +0000 (GMT) List-Id: https://gcc.gnu.org/g:3bff15c1c9fb3eb0bb042717e072476ec2d6d88c commit r13-3752-g3bff15c1c9fb3eb0bb042717e072476ec2d6d88c Author: Aldy Hernandez Date: Mon Nov 7 08:40:12 2022 +0100 [range-op] Restrict division by power of 2 optimization to positive numbers. 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. Diff: --- gcc/range-op.cc | 4 +++- gcc/testsuite/gcc.dg/tree-ssa/pr107541.c | 16 ++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) 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; +}