From patchwork Mon Nov 7 11:42:38 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aldy Hernandez X-Patchwork-Id: 60081 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 235843858418 for ; Mon, 7 Nov 2022 11:43:14 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 235843858418 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1667821394; bh=NyfIpk2vKn20w3JvxJeQnuF4aIkzVSVea+7GyT7eEe0=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=oFxlAPrs6vVU/CanMGU/LcF1ia8yRFHUqVp8ewKDoyYTI2ypKFWYab6RiAAPXbItn t+FZwMJVWOVaJuNh704q4Pt06WadniMd5u+P5T4vClXNtMLVpoZA77BwB91AuR0Ipi 1zWC3Ul9RqYMGDEJepmAvCQ/2v3XaxTC5fnnauhM= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id F1F9F3858D20 for ; Mon, 7 Nov 2022 11:42:45 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org F1F9F3858D20 Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-272-e4Pe8CXZNk-FfktnVRh3vQ-1; Mon, 07 Nov 2022 06:42:44 -0500 X-MC-Unique: e4Pe8CXZNk-FfktnVRh3vQ-1 Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 4DA0E1C06907 for ; Mon, 7 Nov 2022 11:42:44 +0000 (UTC) Received: from abulafia.quesejoda.com (unknown [10.39.193.32]) by smtp.corp.redhat.com (Postfix) with ESMTPS id DAEEF492B05; Mon, 7 Nov 2022 11:42:43 +0000 (UTC) Received: from abulafia.quesejoda.com (localhost [127.0.0.1]) by abulafia.quesejoda.com (8.17.1/8.17.1) with ESMTPS id 2A7BgfR6663951 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Mon, 7 Nov 2022 12:42:41 +0100 Received: (from aldyh@localhost) by abulafia.quesejoda.com (8.17.1/8.17.1/Submit) id 2A7BgfHo663944; Mon, 7 Nov 2022 12:42:41 +0100 To: GCC patches Cc: Andrew MacLeod , Aldy Hernandez Subject: [COMMITTED] [range-op] Restrict division by power of 2 optimization to positive numbers. Date: Mon, 7 Nov 2022 12:42:38 +0100 Message-Id: <20221107114238.663927-1-aldyh@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.10 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.8 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Aldy Hernandez via Gcc-patches From: Aldy Hernandez Reply-To: Aldy Hernandez Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" 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; +}