From patchwork Tue Jun 22 13:18:27 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Andrew MacLeod X-Patchwork-Id: 1495681 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: 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: 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=mwGfp5A9; 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 RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4G8RrW5Kvzz9sS8 for ; Tue, 22 Jun 2021 23:22:03 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 0861C389364E for ; Tue, 22 Jun 2021 13:22:01 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 0861C389364E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1624368121; bh=CiE0EWbPkbH+aQU1nWbqQEys7Wwo5YyYywZRPxvnZuk=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=mwGfp5A98elNovaW88rufq/PbJi4zi2QFn6dYBHVs4NATh/icuwQvB4rdGEEZIWwP 3zUDzDDW5NeywUt6RwSmJ6utwABkKmtP7NNrT3GFH+E9nEbEXizvaXR5CESiYePlQs AnAfq+pKMivZrNIiPrXPn0E6fIH3poHUFku4na8Q= 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 DE28F383D023 for ; Tue, 22 Jun 2021 13:18:39 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org DE28F383D023 Received: from mail-qv1-f70.google.com (mail-qv1-f70.google.com [209.85.219.70]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-500-t3YZdIf3PjqBAb1SkNMGWQ-1; Tue, 22 Jun 2021 09:18:37 -0400 X-MC-Unique: t3YZdIf3PjqBAb1SkNMGWQ-1 Received: by mail-qv1-f70.google.com with SMTP id x13-20020a0cfe0d0000b0290264540cb5d3so1343359qvr.17 for ; Tue, 22 Jun 2021 06:18:36 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:to:from:subject:message-id:date:user-agent :mime-version:content-language; bh=CiE0EWbPkbH+aQU1nWbqQEys7Wwo5YyYywZRPxvnZuk=; b=aU9kpvJRLdAjgybMEbaoSQyREIt42d0ysrpwSnVvcyv0sQ/+gZWoLG2Mqdt9qorwZC bxb0OjepmKcbMWYri/FvhDu2L3exfC1rxBQS0TXrn/zRrP6Sk0Qg37ikcr+g2Q/Ok99l nK0U/iSj8aapj8R1OCDnqiTdCZ1RvVsQuEYgE/rFmhLAavl9fX5dflEXaOpdtrqVNO7z 6PV8hEo9WxGHo6Q8mdzy488Pn+m8C+qUkfqRfbSI3XgDwEtNpPfXRjAIFLjQUqtYqXvq eeIezwqgQ/KSLWqzjxzRBs2VKyDn5ynfOspojRradNpCH+HICIXbg7ge6HNOMEtrWDi0 SCrw== X-Gm-Message-State: AOAM533DNM2tn66jRpvgJL+x7507FmSIB6RtSM/7bCA2DmutIt7PVOwB vbEjx0nqxVSqBimYvkzoJllg9QKN8S79PBgJWW63e/lE/IrowiJ9iDtf9WjIVIaxE353gU6SZJx gjZoIzA9k+/TLXam61GCY7OUl4Vp9SefCg+dD+ORQqikMnT1qYS/sDANFtozszlS9ZYpYrg== X-Received: by 2002:a37:b42:: with SMTP id 63mr4284224qkl.325.1624367913530; Tue, 22 Jun 2021 06:18:33 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwSK342EAn7NmGfWbQHm0mNMFzSqpMD2oyg6zEZhp5d3Bt7QwY5+YupSCmC36hZ4oUVcbfWAA== X-Received: by 2002:a37:b42:: with SMTP id 63mr4284188qkl.325.1624367913132; Tue, 22 Jun 2021 06:18:33 -0700 (PDT) Received: from ?IPv6:2607:fea8:a241:4600:84d5:145a:ba5d:a5af? ([2607:fea8:a241:4600:84d5:145a:ba5d:a5af]) by smtp.gmail.com with ESMTPSA id i67sm12996986qkd.90.2021.06.22.06.18.28 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 22 Jun 2021 06:18:28 -0700 (PDT) To: gcc-patches Subject: [COMMITTED 4/7] Add relations between LHS and op1/op2 for PLUS_EXPR. Message-ID: <9103ab49-d653-fc55-0ef6-6465bde4913c@redhat.com> Date: Tue, 22 Jun 2021 09:18:27 -0400 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.10.0 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-CA X-Spam-Status: No, score=-12.5 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_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) 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" This patch demonstrates how to add relation generation between the LHS of an expression and one of the operands in range-ops, using PLUS_EXPR. a_2 = b_3 + c_1 if c_1 == [0, 0], we know a_2 == b_3 if c_1 > 0, and there is no overflow/wrapping, we know a_2 > b_3 likewise, if c1 < 0 we know a_2 < b_3 if c_1 does not contain zero, we also know a_2 != b_3. plus is symmetrical, so we can draw similar conclusions about the relation between a_2 and c_1 based on what we know about b_3's range. Bootstraps on x86_64-pc-linux-gnu with no regressions.  Pushed. Andrew From c526de3f432a037bdbdd44eb6fa43af4f3b22694 Mon Sep 17 00:00:00 2001 From: Andrew MacLeod Date: Thu, 17 Jun 2021 13:35:10 -0400 Subject: [PATCH 4/7] Add relations between LHS and op1/op2 for PLUS_EXPR. * range-op.cc (operator_plus::lhs_op1_relation): New. (operator_plus::lhs_op2_relation): New. --- gcc/range-op.cc | 80 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) diff --git a/gcc/range-op.cc b/gcc/range-op.cc index d807693900a..a7698f21b0d 100644 --- a/gcc/range-op.cc +++ b/gcc/range-op.cc @@ -1150,8 +1150,88 @@ public: const wide_int &lh_ub, const wide_int &rh_lb, const wide_int &rh_ub) const; + virtual enum tree_code lhs_op1_relation (const irange &lhs, const irange &op1, + const irange &op2) const; + virtual enum tree_code lhs_op2_relation (const irange &lhs, const irange &op1, + const irange &op2) const; } op_plus; +// Check to see if the range of OP2 indicates anything about the relation +// between LHS and OP1. + +enum tree_code +operator_plus::lhs_op1_relation (const irange &lhs, + const irange &op1, + const irange &op2) const +{ + if (lhs.undefined_p () || op1.undefined_p () || op2.undefined_p ()) + return VREL_NONE; + + tree type = lhs.type (); + unsigned prec = TYPE_PRECISION (type); + wi::overflow_type ovf1, ovf2; + signop sign = TYPE_SIGN (type); + + // LHS = OP1 + 0 indicates LHS == OP1. + if (op2.zero_p ()) + return EQ_EXPR; + + if (TYPE_OVERFLOW_WRAPS (type)) + { + wi::add (op1.lower_bound (), op2.lower_bound (), sign, &ovf1); + wi::add (op1.upper_bound (), op2.upper_bound (), sign, &ovf2); + } + else + ovf1 = ovf2 = wi::OVF_NONE; + + // Never wrapping additions. + if (!ovf1 && !ovf2) + { + // Positive op2 means lhs > op1. + if (wi::gt_p (op2.lower_bound (), wi::zero (prec), sign)) + return GT_EXPR; + if (wi::ge_p (op2.lower_bound (), wi::zero (prec), sign)) + return GE_EXPR; + + // Negative op2 means lhs < op1. + if (wi::lt_p (op2.upper_bound (), wi::zero (prec), sign)) + return LT_EXPR; + if (wi::le_p (op2.upper_bound (), wi::zero (prec), sign)) + return LE_EXPR; + } + // Always wrapping additions. + else if (ovf1 && ovf1 == ovf2) + { + // Positive op2 means lhs < op1. + if (wi::gt_p (op2.lower_bound (), wi::zero (prec), sign)) + return LT_EXPR; + if (wi::ge_p (op2.lower_bound (), wi::zero (prec), sign)) + return LE_EXPR; + + // Negative op2 means lhs > op1. + if (wi::lt_p (op2.upper_bound (), wi::zero (prec), sign)) + return GT_EXPR; + if (wi::le_p (op2.upper_bound (), wi::zero (prec), sign)) + return GE_EXPR; + } + + // If op2 does not contain 0, then LHS and OP1 can never be equal. + if (!range_includes_zero_p (&op2)) + return NE_EXPR; + + return VREL_NONE; +} + +// PLUS is symmetrical, so we can simply call lhs_op1_relation with reversed +// operands. + +enum tree_code +operator_plus::lhs_op2_relation (const irange &lhs, const irange &op1, + const irange &op2) const +{ + return lhs_op1_relation (lhs, op2, op1); +} + void operator_plus::wi_fold (irange &r, tree type, const wide_int &lh_lb, const wide_int &lh_ub, -- 2.17.2