From patchwork Fri Oct 1 12:43:08 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aldy Hernandez X-Patchwork-Id: 45678 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 7C9C03858034 for ; Fri, 1 Oct 2021 12:45:49 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 7C9C03858034 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1633092349; bh=b9SueDTMMRWU2dpjZWfO7y3nyUbpeP6umUTB4CntCbY=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=oKH1GJvRO0W3eF83DGZ6BiR3PSoVNYPByWeX89ktYqndIKW6+b82BdEmrq4ZKwWm2 +/LAL7RDyuMAJpDozR7dRBRWf91JX8GI4CR5VxkoH1gAPdC/9knSZJ1r8ab/u/u7cv U9+ongJDMANDmpMXUlEaLDBpn6mNQb51m8VqyWZ8= 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 ESMTP id 97FCB3858422 for ; Fri, 1 Oct 2021 12:43:38 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 97FCB3858422 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-64-Z78yHRPdNq2ohCdqWFfCQA-1; Fri, 01 Oct 2021 08:43:36 -0400 X-MC-Unique: Z78yHRPdNq2ohCdqWFfCQA-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 5BD099126B for ; Fri, 1 Oct 2021 12:43:35 +0000 (UTC) Received: from abulafia.quesejoda.com (unknown [10.39.193.105]) by smtp.corp.redhat.com (Postfix) with ESMTPS id C3CB81972D; Fri, 1 Oct 2021 12:43:31 +0000 (UTC) Received: from abulafia.quesejoda.com (localhost [127.0.0.1]) by abulafia.quesejoda.com (8.16.1/8.15.2) with ESMTPS id 191ChSTJ1458287 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Fri, 1 Oct 2021 14:43:28 +0200 Received: (from aldyh@localhost) by abulafia.quesejoda.com (8.16.1/8.16.1/Submit) id 191ChSwT1458286; Fri, 1 Oct 2021 14:43:28 +0200 To: GCC patches Subject: [PATCH] Handle EQ_EXPR relation for operator_lshift. Date: Fri, 1 Oct 2021 14:43:08 +0200 Message-Id: <20211001124306.1458223-3-aldyh@redhat.com> In-Reply-To: <20211001124306.1458223-1-aldyh@redhat.com> References: <20211001124306.1458223-1-aldyh@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-13.1 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_LOW, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) 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" Knowing that X << X is non-zero means X is also non-zero. This patch teaches this this to range-ops. As usual, the big twiddling experts could come up with all sorts of fancy enhancements in this area, and we welcome all patches :). I will push this pending tests. gcc/ChangeLog: PR tree-optimization/102546 * range-op.cc (operator_lshift::op1_range): Handle EQ_EXPR relation. --- gcc/range-op.cc | 19 ++++++++++++++++--- gcc/testsuite/gcc.dg/tree-ssa/pr102546.c | 23 +++++++++++++++++++++++ 2 files changed, 39 insertions(+), 3 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr102546.c diff --git a/gcc/range-op.cc b/gcc/range-op.cc index 5e37133026d..53f3be4266e 100644 --- a/gcc/range-op.cc +++ b/gcc/range-op.cc @@ -2075,9 +2075,14 @@ operator_lshift::op1_range (irange &r, tree type, const irange &lhs, const irange &op2, - relation_kind rel ATTRIBUTE_UNUSED) const + relation_kind rel) const { tree shift_amount; + int_range<2> adjust (type); + + if (rel == EQ_EXPR && !lhs.contains_p (build_zero_cst (type))) + adjust.set_nonzero (type); + if (op2.singleton_p (&shift_amount)) { wide_int shift = wi::to_wide (shift_amount); @@ -2086,10 +2091,11 @@ operator_lshift::op1_range (irange &r, if (wi::ge_p (shift, wi::uhwi (TYPE_PRECISION (type), TYPE_PRECISION (op2.type ())), UNSIGNED)) - return false; + goto done; if (shift == 0) { r = lhs; + r.intersect (adjust); return true; } @@ -2126,9 +2132,16 @@ operator_lshift::op1_range (irange &r, if (utype != type) range_cast (r, type); + r.intersect (adjust); return true; } - return false; + + done: + if (adjust.varying_p ()) + return false; + + r = adjust; + return true; } bool diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr102546.c b/gcc/testsuite/gcc.dg/tree-ssa/pr102546.c new file mode 100644 index 00000000000..4bd98747732 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr102546.c @@ -0,0 +1,23 @@ +// { dg-do compile } +// { dg-options "-O3 -fdump-tree-optimized" } + +static int a; +static char b, c, d; +void bar(void); +void foo(void); + +int main() { + int f = 0; + for (; f <= 5; f++) { + bar(); + b = b && f; + d = f << f; + if (!(a >= d || f)) + foo(); + c = 1; + for (; c; c = 0) + ; + } +} + +// { dg-final { scan-tree-dump-not "foo" "optimized" } }