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: 50440 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp2363723wrn; Mon, 30 Jan 2023 11:47:27 -0800 (PST) X-Google-Smtp-Source: AK7set/lfLcDyMkrTwFfrTeYf4wxnAxC9hxOG87ZZWoH31EtTGyHr6eSvHW6ljSzG0H3CPtWZK4v X-Received: by 2002:a17:906:40d7:b0:877:6177:309c with SMTP id a23-20020a17090640d700b008776177309cmr682424ejk.31.1675108047008; Mon, 30 Jan 2023 11:47:27 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1675108046; cv=none; d=google.com; s=arc-20160816; b=Js5byMxIEQmko6FvOwaAysqkHGh7NDW9dEMGEDPcuZZidd5lrVX9xrtnHgYcRakGlE vM8UwQpvKkD0MCQXeOnyoNBbtVsFP/992pQ0dhTKuo1HsAL9+mazXE/9fn+pJ5vxPX0D msk6jKYCT/ouIPlFX8vMa9vFsdO4lKDVphgJT7kXqk6brMQ6z89EQg4OdmBWWKnka2GV BBdHf99GwA0NJ9YYOtS9PwFhFtHrct2Z2S4Exuc97g5dy560qGjr3tVO6ApQpL9JvSNX dmmzbAYCvjmKaOU0Htp1qvNU3QG1Tj4Gh1v7J9UjnSK4fIW9sgGlm8ecAh680qSPIFWl dOpQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence:content-language :subject:cc:to:user-agent:mime-version:date:message-id:dmarc-filter :delivered-to:dkim-signature:dkim-filter; bh=sfM2vO61aiq8ySchdK/FOWb6gO71S6hBBfvbWCSscSs=; b=aje1Ijl9G9rqpXf0JBGseIA1eS8poTOxvLz2oeJMbZ7ymO3AKUPvMCWx7VJDfUKbiS IFm1Y0/IbEIdTl683bQ5qKxh2ouLZ60jv3M+fji0+jlsXIUmTP5lB5CRrGKCTV0WxmU6 PwEEaBdrdt3azkfxXut5HphZKPv1T2By18Tuu8IhdZo1gYU2Lk2SI4VxNy6UNvAg3j+8 X4rg0fBMo/ohKPBg5uX6ddQ1unDO3QckfMrCmErj04t0/nMQ4CpEXjmL07wutl003OVS C7C1FIUf4+Uyhut6VoEjpn3mCf6bVdR2zKafpKZvEFoavLF17e4knk8QuJYonmATkf2F 46ig== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b="nnxDx/vQ"; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id ep22-20020a1709069b5600b00878734ccb99si15207111ejc.616.2023.01.30.11.47.26 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 Jan 2023 11:47:26 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b="nnxDx/vQ"; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id C9C523858D35 for ; Mon, 30 Jan 2023 19:47:25 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C9C523858D35 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1675108045; 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=nnxDx/vQfgjAGTjpkrtMKd+G+Ms16yfxPiJcCTcy9nSd/GhFOGfiooM8DSolm5VGZ vkQnYneX0Zg313AupevFqEFBJ/ePWrWnZsDFHel54sX3+FrNlDJpNUjuz+BIgcgSIi lHsMFPS4qYiB7qUH8MGLyHdKV30Kv+RXQlmhMizM= 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-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+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1756478095679934697?= X-GMAIL-MSGID: =?utf-8?q?1756478095679934697?= 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