From patchwork Tue Jul 18 14:05:43 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiufu Guo X-Patchwork-Id: 122060 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c923:0:b0:3e4:2afc:c1 with SMTP id j3csp1776478vqt; Tue, 18 Jul 2023 07:12:32 -0700 (PDT) X-Google-Smtp-Source: APBJJlGkNa0nX5nZJY7J0oyAz5EQFnrfIl9xt4deqfPW1pFJtDQa6+1YCASiY3BxM77ZYB1X3YUN X-Received: by 2002:a17:906:3f02:b0:997:decf:11e7 with SMTP id c2-20020a1709063f0200b00997decf11e7mr49663ejj.12.1689689551771; Tue, 18 Jul 2023 07:12:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689689551; cv=none; d=google.com; s=arc-20160816; b=y1+L18rpZvyjhZlaku6/4aGHOsD/ussRwT0EyXzDuGwvKeVZmvl18sqLBhTbJKKCtt bhXEuc6YHElxe5Om0S3nq7jdzmm7fCFi1ikAPf9ZyYxAV6jeigBIlXamQVGC9+zrR+tN pSLMHptWqfOivRhHYiludcnODkgparYaGf6xPxFv1/2ek6VsagECqXYnIAjgdD8R2nKv 2C31Mu2ZcBYj8VU7KTrg0fkCDwnBev67LRB9u2TgU35AVR9ztdyv37FYEIHPmHLgHLjg Y/ri5JHDtJOQHV1ZLP0WmgXfMUIdPEvaYdgBKpHgA4WR8bpbaEA/8t/k4OVQfL4hCL1i uTwA== 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:mime-version :content-transfer-encoding:message-id:date:subject:cc:to :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=F5NNeUj8FlAp8wAgqiSEiz00saaKGR7DkZXHb0iVBos=; fh=Pgr0hSRPz3D7/UbchajK0QYZ8gPh5gFm38BGLFvZMT0=; b=u1XguuSGKHt7L0fhoP6SwmSvqrt4TGuZ+7WNb0F0z8EVAsqbH7iiIgVBxu8Rff8iWu A6yPrULIV2ORDElAHrjjuxU5PZOx7b3P0SyraznHlOGkUwi2+WTTAP03jRNQxCvR7t2I gNw6rHabt+7Z5o3Lp4jPnaTuS9cM2mbxmNXC8SF8phrYdz1LTgtGCpL/NES0lEG1qLUy fldHtrvF9BqU/kK/AkUiKa6w15xbJUk88GqeN37L+Ap1dx290NP2UlELM3AknFiDm/BZ /6TbC1eGiDnalYDOdbSpN3+8FeyQlBHSYRZFksZaIroUnGRh9Qvd36hysB+uHClLrDir ozdg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=QeBp1mro; 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 (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id t3-20020a170906948300b00992fec0f917si1172222ejx.634.2023.07.18.07.12.31 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 18 Jul 2023 07:12:31 -0700 (PDT) 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=QeBp1mro; 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 9F5B13857351 for ; Tue, 18 Jul 2023 14:12:30 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 9F5B13857351 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1689689550; bh=F5NNeUj8FlAp8wAgqiSEiz00saaKGR7DkZXHb0iVBos=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=QeBp1mroQGrkDs6aiUBGcmU9qtdvXhsP8GzW5Af/zr+XW4FxtSAXlJPMowVGW6z79 sWHbUsGz3Qza3MP6Y58O2a5R/a1/dbLgY01+w6x03165+fbcTjm/u7aXeZRO9q3RCT FC8j+nYQtl78HAx5P+lKoBzUrmbnDEztB1YKctrY= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mx0a-001b2d01.pphosted.com (mx0a-001b2d01.pphosted.com [148.163.156.1]) by sourceware.org (Postfix) with ESMTPS id 8AF7D385773C; Tue, 18 Jul 2023 14:11:46 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 8AF7D385773C Received: from pps.filterd (m0353729.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 36IDvTci020107; Tue, 18 Jul 2023 14:11:43 GMT Received: from pps.reinject (localhost [127.0.0.1]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 3rwv2t0nqf-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 18 Jul 2023 14:11:41 +0000 Received: from m0353729.ppops.net (m0353729.ppops.net [127.0.0.1]) by pps.reinject (8.17.1.5/8.17.1.5) with ESMTP id 36IDxBnH026757; Tue, 18 Jul 2023 14:11:09 GMT Received: from ppma11.dal12v.mail.ibm.com (db.9e.1632.ip4.static.sl-reverse.com [50.22.158.219]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 3rwv2t0mtd-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 18 Jul 2023 14:11:09 +0000 Received: from pps.filterd (ppma11.dal12v.mail.ibm.com [127.0.0.1]) by ppma11.dal12v.mail.ibm.com (8.17.1.19/8.17.1.19) with ESMTP id 36I8BB9c004479; Tue, 18 Jul 2023 14:05:50 GMT Received: from smtprelay06.fra02v.mail.ibm.com ([9.218.2.230]) by ppma11.dal12v.mail.ibm.com (PPS) with ESMTPS id 3rv8g0wwnc-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 18 Jul 2023 14:05:50 +0000 Received: from smtpav04.fra02v.mail.ibm.com (smtpav04.fra02v.mail.ibm.com [10.20.54.103]) by smtprelay06.fra02v.mail.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 36IE5l0739125674 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 18 Jul 2023 14:05:47 GMT Received: from smtpav04.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 7D86E2004E; Tue, 18 Jul 2023 14:05:47 +0000 (GMT) Received: from smtpav04.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id A42422004B; Tue, 18 Jul 2023 14:05:45 +0000 (GMT) Received: from ltcden2-lp1.aus.stglabs.ibm.com (unknown [9.3.90.43]) by smtpav04.fra02v.mail.ibm.com (Postfix) with ESMTP; Tue, 18 Jul 2023 14:05:45 +0000 (GMT) To: gcc-patches@gcc.gnu.org Cc: amacleod@redhat.com, aldyh@redhat.com, rguenther@suse.de, jeffreyalaw@gmail.com, richard.sandiford@arm.com, segher@kernel.crashing.org, dje.gcc@gmail.com, linkw@gcc.gnu.org, bergner@linux.ibm.com, guojiufu@linux.ibm.com Subject: [PATCH V5 1/2] Add overflow API for plus minus mult on range Date: Tue, 18 Jul 2023 22:05:43 +0800 Message-Id: <20230718140544.3497370-1-guojiufu@linux.ibm.com> X-Mailer: git-send-email 2.39.3 X-TM-AS-GCONF: 00 X-Proofpoint-ORIG-GUID: nyw-a6hVlvHCmVeuxmf3BOmXZ0IvGrrA X-Proofpoint-GUID: q0XPFlshVUBzS8uR3oLPPobqBloqhwgR X-Proofpoint-UnRewURL: 0 URL was un-rewritten MIME-Version: 1.0 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.254,Aquarius:18.0.957,Hydra:6.0.591,FMLib:17.11.176.26 definitions=2023-07-18_10,2023-07-18_01,2023-05-22_02 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 priorityscore=1501 impostorscore=0 suspectscore=0 mlxscore=0 adultscore=0 bulkscore=0 phishscore=0 malwarescore=0 spamscore=0 clxscore=1015 mlxlogscore=999 lowpriorityscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2306200000 definitions=main-2307180129 X-Spam-Status: No, score=-10.8 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_MSPIKE_H5, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE 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: Jiufu Guo via Gcc-patches From: Jiufu Guo Reply-To: Jiufu Guo Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1771767911023640105 X-GMAIL-MSGID: 1771767911023640105 Hi, As discussed in previous reviews, adding overflow APIs to range-op would be useful. Those APIs could help to check if overflow happens when operating between two 'range's, like: plus, minus, and mult. Previous discussions are here: https://gcc.gnu.org/pipermail/gcc-patches/2023-July/624067.html https://gcc.gnu.org/pipermail/gcc-patches/2023-July/624701.html Bootstrap & regtest pass on ppc64{,le} and x86_64. Is this patch ok for trunk? BR, Jeff (Jiufu Guo) gcc/ChangeLog: * range-op-mixed.h (operator_plus::overflow_free_p): New declare. (operator_minus::overflow_free_p): New declare. (operator_mult::overflow_free_p): New declare. * range-op.cc (range_op_handler::overflow_free_p): New function. (range_operator::overflow_free_p): New default function. (operator_plus::overflow_free_p): New function. (operator_minus::overflow_free_p): New function. (operator_mult::overflow_free_p): New function. * range-op.h (range_op_handler::overflow_free_p): New declare. (range_operator::overflow_free_p): New declare. * value-range.cc (irange::nonnegative_p): New function. (irange::nonpositive_p): New function. * value-range.h (irange::nonnegative_p): New declare. (irange::nonpositive_p): New declare. --- gcc/range-op-mixed.h | 11 ++++ gcc/range-op.cc | 124 +++++++++++++++++++++++++++++++++++++++++++ gcc/range-op.h | 5 ++ gcc/value-range.cc | 12 +++++ gcc/value-range.h | 2 + 5 files changed, 154 insertions(+) diff --git a/gcc/range-op-mixed.h b/gcc/range-op-mixed.h index 6944742ecbc..42157ed9061 100644 --- a/gcc/range-op-mixed.h +++ b/gcc/range-op-mixed.h @@ -383,6 +383,10 @@ public: relation_kind rel) const final override; void update_bitmask (irange &r, const irange &lh, const irange &rh) const final override; + + virtual bool overflow_free_p (const irange &lh, const irange &rh, + relation_trio = TRIO_VARYING) const; + private: void wi_fold (irange &r, tree type, const wide_int &lh_lb, const wide_int &lh_ub, const wide_int &rh_lb, @@ -446,6 +450,10 @@ public: relation_kind rel) const final override; void update_bitmask (irange &r, const irange &lh, const irange &rh) const final override; + + virtual bool overflow_free_p (const irange &lh, const irange &rh, + relation_trio = TRIO_VARYING) const; + private: void wi_fold (irange &r, tree type, const wide_int &lh_lb, const wide_int &lh_ub, const wide_int &rh_lb, @@ -525,6 +533,9 @@ public: const REAL_VALUE_TYPE &lh_lb, const REAL_VALUE_TYPE &lh_ub, const REAL_VALUE_TYPE &rh_lb, const REAL_VALUE_TYPE &rh_ub, relation_kind kind) const final override; + virtual bool overflow_free_p (const irange &lh, const irange &rh, + relation_trio = TRIO_VARYING) const; + }; class operator_addr_expr : public range_operator diff --git a/gcc/range-op.cc b/gcc/range-op.cc index cb584314f4c..632b044331b 100644 --- a/gcc/range-op.cc +++ b/gcc/range-op.cc @@ -366,6 +366,22 @@ range_op_handler::op1_op2_relation (const vrange &lhs) const } } +bool +range_op_handler::overflow_free_p (const vrange &lh, + const vrange &rh, + relation_trio rel) const +{ + gcc_checking_assert (m_operator); + switch (dispatch_kind (lh, lh, rh)) + { + case RO_III: + return m_operator->overflow_free_p(as_a (lh), + as_a (rh), + rel); + default: + return false; + } +} // Convert irange bitmasks into a VALUE MASK pair suitable for calling CCP. @@ -688,6 +704,13 @@ range_operator::op1_op2_relation_effect (irange &lhs_range ATTRIBUTE_UNUSED, return false; } +bool +range_operator::overflow_free_p (const irange &, const irange &, + relation_trio) const +{ + return false; +} + // Apply any known bitmask updates based on this operator. void @@ -4311,6 +4334,107 @@ range_op_table::initialize_integral_ops () } +bool +operator_plus::overflow_free_p (const irange &lh, const irange &rh, + relation_trio) const +{ + if (lh.undefined_p () || rh.undefined_p ()) + return false; + + tree type = lh.type (); + if (TYPE_OVERFLOW_UNDEFINED (type)) + return true; + + wi::overflow_type ovf; + signop sgn = TYPE_SIGN (type); + wide_int wmax0 = lh.upper_bound (); + wide_int wmax1 = rh.upper_bound (); + wi::add (wmax0, wmax1, sgn, &ovf); + if (ovf != wi::OVF_NONE) + return false; + + if (TYPE_UNSIGNED (type)) + return true; + + wide_int wmin0 = lh.lower_bound (); + wide_int wmin1 = rh.lower_bound (); + wi::add (wmin0, wmin1, sgn, &ovf); + if (ovf != wi::OVF_NONE) + return false; + + return true; +} + +bool +operator_minus::overflow_free_p (const irange &lh, const irange &rh, + relation_trio) const +{ + if (lh.undefined_p () || rh.undefined_p ()) + return false; + + tree type = lh.type (); + if (TYPE_OVERFLOW_UNDEFINED (type)) + return true; + + wi::overflow_type ovf; + signop sgn = TYPE_SIGN (type); + wide_int wmin0 = lh.lower_bound (); + wide_int wmax1 = rh.upper_bound (); + wi::sub (wmin0, wmax1, sgn, &ovf); + if (ovf != wi::OVF_NONE) + return false; + + if (TYPE_UNSIGNED (type)) + return true; + + wide_int wmax0 = lh.upper_bound (); + wide_int wmin1 = rh.lower_bound (); + wi::sub (wmax0, wmin1, sgn, &ovf); + if (ovf != wi::OVF_NONE) + return false; + + return true; +} + +bool +operator_mult::overflow_free_p (const irange &lh, const irange &rh, + relation_trio) const +{ + if (lh.undefined_p () || rh.undefined_p ()) + return false; + + tree type = lh.type (); + if (TYPE_OVERFLOW_UNDEFINED (type)) + return true; + + wi::overflow_type ovf; + signop sgn = TYPE_SIGN (type); + wide_int wmax0 = lh.upper_bound (); + wide_int wmax1 = rh.upper_bound (); + wi::mul (wmax0, wmax1, sgn, &ovf); + if (ovf != wi::OVF_NONE) + return false; + + if (TYPE_UNSIGNED (type)) + return true; + + wide_int wmin0 = lh.lower_bound (); + wide_int wmin1 = rh.lower_bound (); + wi::mul (wmin0, wmin1, sgn, &ovf); + if (ovf != wi::OVF_NONE) + return false; + + wi::mul (wmin0, wmax1, sgn, &ovf); + if (ovf != wi::OVF_NONE) + return false; + + wi::mul (wmax0, wmin1, sgn, &ovf); + if (ovf != wi::OVF_NONE) + return false; + + return true; +} + #if CHECKING_P #include "selftest.h" diff --git a/gcc/range-op.h b/gcc/range-op.h index af94c2756a7..db3b03f28a5 100644 --- a/gcc/range-op.h +++ b/gcc/range-op.h @@ -147,6 +147,9 @@ public: virtual relation_kind op1_op2_relation (const irange &lhs) const; virtual relation_kind op1_op2_relation (const frange &lhs) const; + + virtual bool overflow_free_p (const irange &lh, const irange &rh, + relation_trio = TRIO_VARYING) const; protected: // Perform an integral operation between 2 sub-ranges and return it. virtual void wi_fold (irange &r, tree type, @@ -214,6 +217,8 @@ public: const vrange &op2, relation_kind = VREL_VARYING) const; relation_kind op1_op2_relation (const vrange &lhs) const; + bool overflow_free_p (const vrange &lh, const vrange &rh, + relation_trio = TRIO_VARYING) const; protected: unsigned dispatch_kind (const vrange &lhs, const vrange &op1, const vrange& op2) const; diff --git a/gcc/value-range.cc b/gcc/value-range.cc index 011bdbdeae6..5ae4e044194 100644 --- a/gcc/value-range.cc +++ b/gcc/value-range.cc @@ -313,6 +313,18 @@ add_vrange (const vrange &v, inchash::hash &hstate, } //namespace inchash +bool +irange::nonnegative_p () const +{ + return wi::ge_p (lower_bound (), 0, TYPE_SIGN (type ())); +} + +bool +irange::nonpositive_p () const +{ + return wi::le_p (upper_bound (), 0, TYPE_SIGN (type ())); +} + bool irange::supports_type_p (const_tree type) const { diff --git a/gcc/value-range.h b/gcc/value-range.h index 0170188201b..a12dea514e4 100644 --- a/gcc/value-range.h +++ b/gcc/value-range.h @@ -280,6 +280,8 @@ public: virtual bool singleton_p (tree *result = NULL) const override; bool singleton_p (wide_int &) const; bool contains_p (const wide_int &) const; + bool nonnegative_p () const; + bool nonpositive_p () const; // In-place operators. virtual bool union_ (const vrange &) override;