From patchwork Mon Jan 30 19:46:33 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Andrew MacLeod X-Patchwork-Id: 1734353 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=Ry24Ia6v; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4P5Jbw0nk5z23hS for ; Tue, 31 Jan 2023 06:47:05 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id B2D483858C2C for ; Mon, 30 Jan 2023 19:47:01 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B2D483858C2C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1675108021; bh=sfM2vO61aiq8ySchdK/FOWb6gO71S6hBBfvbWCSscSs=; h=Date:To:Cc:Subject:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=Ry24Ia6vRf82wr+IwG1ImuVSCWtAWp1+BQRPK+8c1yqCdQm/gUIymLvKuTl4qxjiR vIvJeDOtr71/1+HJzc0lTF2adhzgisfRGAAnv0W2LnKtOKL+SVZW1VsPgA4tzaKsJH tPuAM86qllD+vAuBgafoPOvoI9A6fCJw4kSBu5Dg= 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.129.124]) by sourceware.org (Postfix) with ESMTPS id 588533858D1E for ; Mon, 30 Jan 2023 19:46:40 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 588533858D1E Received: from mail-qt1-f197.google.com (mail-qt1-f197.google.com [209.85.160.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-563-YEjyV3mJMUK9VzQ93KgTIA-1; Mon, 30 Jan 2023 14:46:38 -0500 X-MC-Unique: YEjyV3mJMUK9VzQ93KgTIA-1 Received: by mail-qt1-f197.google.com with SMTP id o16-20020ac841d0000000b003b689580725so5517530qtm.22 for ; Mon, 30 Jan 2023 11:46:38 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=subject:from:cc:to:content-language:user-agent:mime-version:date :message-id:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=WTt8B7Kf7hmypwgc1HDVcuvKspnVx3dVQT2+3MCBzGQ=; b=dOSuQkFX7t2A0Es1t6q04M0gEMqnQwsMPzNhUssRYy0+3flIjh/n8en2Ttcmc9xBGm Jl7+y60URTbM4JYJskQu9aZ3XB11gjsCuX1YVm01OcEIrAZpaoxU/5gAPr3VayBhCT+B JwZWLLqHscWzOVSMO3PaMwM5zIUyXwMWPwZ8J+/TYpNGuUyFd+/JjO83TLHweXo0kbIo hCB8gL3VEP7qLzuG4Bhm/yD47UUteCOv+zOU2/VSD41T6VCrnIr/u5kaEpWxevNpURm0 tyU1YHWAPtvT5yiU5/Ita+9sHHuE0wuLJK0CfMy+OSRafpY/ARPN8Ubx1IzOXKh9sBcX L78g== X-Gm-Message-State: AFqh2kpHeJE1dUmbzl4bGeSZ/mNGGCx7JzNmp7cJ1I+nClh4pzH4AuCI wnzGASwheuShYYxXyiX1UxvEWuTvRbex9MDLct1s9wYN1485LtlebHgoaeINSRaLwCKr7WrCprG O1rgAjONtBBLHdmgAREcKZZhLi4d6EQUlLKqK0UN/pX6O5GIrzWCXaG/ENTu+7dCXj6W8nA== X-Received: by 2002:a05:622a:4805:b0:3b2:365c:9a9e with SMTP id fb5-20020a05622a480500b003b2365c9a9emr72725195qtb.61.1675107997346; Mon, 30 Jan 2023 11:46:37 -0800 (PST) X-Google-Smtp-Source: AMrXdXs8JS3mu5awBWw4wOZaODljHKZumesk4a+bEaYNovuCoGjIaMiIIPAhO2vKQSh1oP1Lu3Gtmw== X-Received: by 2002:a05:622a:4805:b0:3b2:365c:9a9e with SMTP id fb5-20020a05622a480500b003b2365c9a9emr72725166qtb.61.1675107996970; Mon, 30 Jan 2023 11:46:36 -0800 (PST) Received: from ?IPV6:2607:fea8:a263:f600::fa90? ([2607:fea8:a263:f600::fa90]) by smtp.gmail.com with ESMTPSA id a12-20020a05620a438c00b00706b09b16fasm2544180qkp.11.2023.01.30.11.46.35 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 30 Jan 2023 11:46:36 -0800 (PST) Message-ID: <64ec68a2-7a9f-4c20-0abe-7d36d7707ee4@redhat.com> Date: Mon, 30 Jan 2023 14:46:33 -0500 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.6.0 To: gcc-patches Cc: "hernandez, aldy" Subject: [PATCH 3/3] tree-optimization/108385 - Add op2_range to pointer_plus. X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US X-Spam-Status: No, score=-12.0 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: Andrew MacLeod via Gcc-patches From: Andrew MacLeod Reply-To: Andrew MacLeod Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" Implement op2_range for pointer_plus to determine the offset (operand 2) is zero or non-zero based on equality/inequality between the LHS and op1. Fairly trivial fix for the PR, dependent on the first patch in the set as it uses an accurate relation_trio in GORI to determine if LHS == OP1. There was one tweak to GORI in that we use to stop calculating when the LHS was varying.  THis PR also exposed a case where the LHS is varying, but a relation between the operands can still cause us to find a useful result...  ie VARYING = VARYING + OFFSET when LHS and OP1 are equal can produce a non-zero OFFSET calculation. Bootstraps on x86_64-pc-linux-gnu with no regressions. OK for trunk? Andrew From 0730d9a6b856f6887bfffc4ce45d4164563a476e Mon Sep 17 00:00:00 2001 From: Andrew MacLeod Date: Tue, 17 Jan 2023 11:39:47 -0500 Subject: [PATCH 3/3] Add op2_range to pointer_plus. Implement op2_range for pointer_plus to determine the offset (operand 2) is zero or non-zero based on equality/inequality between the LHS and op1. Also allow GORI computations to continue if the LHS is VARYING and there is also a relation. PR tree-optimization/108385 gcc/ * gimple-range-gori.cc (gori_compute::compute_operand_range): Allow VARYING computations to continue if there is a relation. * range-op.cc (pointer_plus_operator::op2_range): New. gcc/testsuite/ * gcc.dg/pr108385.c: New. --- gcc/gimple-range-gori.cc | 13 +++++++---- gcc/range-op.cc | 23 +++++++++++++++++++ gcc/testsuite/gcc.dg/pr108385.c | 39 +++++++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+), 4 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/pr108385.c diff --git a/gcc/gimple-range-gori.cc b/gcc/gimple-range-gori.cc index 3dc4576ff13..beb1c0064b9 100644 --- a/gcc/gimple-range-gori.cc +++ b/gcc/gimple-range-gori.cc @@ -607,10 +607,6 @@ gori_compute::compute_operand_range (vrange &r, gimple *stmt, { value_relation vrel; value_relation *vrel_ptr = rel; - // If the lhs doesn't tell us anything, neither will unwinding further. - if (lhs.varying_p ()) - return false; - // Empty ranges are viral as they are on an unexecutable path. if (lhs.undefined_p ()) { @@ -657,10 +653,19 @@ gori_compute::compute_operand_range (vrange &r, gimple *stmt, if (!op1_in_chain && !op2_in_chain) return false; + // If the lhs doesn't tell us anything and there are no relations, there + // is nothing to be learned. + if (lhs.varying_p () && !vrel_ptr) + return false; + bool res; // Process logicals as they have special handling. if (is_gimple_logical_p (stmt)) { + // If the lhs doesn't tell us anything, neither will combining operands. + if (lhs.varying_p ()) + return false; + unsigned idx; if ((idx = tracer.header ("compute_operand "))) { diff --git a/gcc/range-op.cc b/gcc/range-op.cc index f7c1e84e0bd..136b709385c 100644 --- a/gcc/range-op.cc +++ b/gcc/range-op.cc @@ -4212,6 +4212,10 @@ public: const wide_int &lh_ub, const wide_int &rh_lb, const wide_int &rh_ub) const; + virtual bool op2_range (irange &r, tree type, + const irange &lhs, + const irange &op1, + relation_trio = TRIO_VARYING) const; } op_pointer_plus; void @@ -4258,6 +4262,25 @@ pointer_plus_operator::wi_fold (irange &r, tree type, r.set_varying (type); } +bool +pointer_plus_operator::op2_range (irange &r, tree type, + const irange &lhs ATTRIBUTE_UNUSED, + const irange &op1 ATTRIBUTE_UNUSED, + relation_trio trio) const +{ + relation_kind rel = trio.lhs_op1 (); + r.set_varying (type); + + // If the LHS and OP1 are equal, the op2 must be zero. + if (rel == VREL_EQ) + r.set_zero (type); + // If the LHS and OP1 are not equal, the offset must be non-zero. + else if (rel == VREL_NE) + r.set_nonzero (type); + else + return false; + return true; +} class pointer_min_max_operator : public range_operator { diff --git a/gcc/testsuite/gcc.dg/pr108385.c b/gcc/testsuite/gcc.dg/pr108385.c new file mode 100644 index 00000000000..13babf06d9a --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr108385.c @@ -0,0 +1,39 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-evrp" } */ + +void bar(char *); + +/* Ensure that PTR1 = PTR2 + OFF properly picks up the zero and non-zero + properties if PTR1 and PTR2 are known equal or non-equal. */ + +void foo1 (char *p, char *pp, int off) +{ + char *q = p + off; + if (q != p) + { + if (off == 0) + bar (q); + } + else + { + if (off != 0) + bar (p); + } +} + +void foo2 (char *p, char *pp, int off) +{ + char *q = p + off; + if (q == p) + { + if (off != 0) + bar (p); + } + else + { + if (off == 0) + bar (q); + } +} + +/* { dg-final { scan-tree-dump-not "bar" "evrp" } } */ -- 2.39.0