From patchwork Mon Oct 23 17:16:10 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Sandiford X-Patchwork-Id: 116782 Delivered-To: patch@linaro.org Received: by 10.140.22.164 with SMTP id 33csp4881958qgn; Mon, 23 Oct 2017 10:16:32 -0700 (PDT) X-Received: by 10.98.103.72 with SMTP id b69mr13626558pfc.251.1508778992098; Mon, 23 Oct 2017 10:16:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1508778992; cv=none; d=google.com; s=arc-20160816; b=BR/wDvHPYVZhujud8dA4nWBQOm3Iu48uwDafKWJGNLHtPiZf+rXhWsIqrCu6IvfPaN t/K45QIhKByGFdeza9ibVVME2DQ6LxMZyyWuBTD6bdRGJ6tAuhV5D8aROPo18k4cUZeH DxQ0NKny0s4+EVsvdCe0stNulEDqy95DedhpweHDSi8qjJptsuEp1aOKbnL+PUbE7J1Z fuUuIJ9hh2U95ZV5OpNwTPFNbA/7LH1aTMhMJd/92URYazDO3bJg0UrqtVrnPKSmV37s BOxZ/S0hEoKch/9lK/cSRrt6u2fYll06EE0w6spOWq0CIQVdeJEpuP1n3M7FSHhQG9Cn 4fAQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=mime-version:user-agent:message-id:in-reply-to:date:references :subject:mail-followup-to:to:from:delivered-to:sender:list-help :list-post:list-archive:list-unsubscribe:list-id:precedence :mailing-list:dkim-signature:domainkey-signature :arc-authentication-results; bh=/R9p0EPAVZRutMGavwECPCAzD0LKssBG4vC76XLLjH4=; b=LOBZ8cbys4tjinBGnUGjURW9pEIweU4N2dnxUSvVEC+KFWTTLj4pteJv6P0gN4eEVM wIsCGSHfg4Lc6FYxKu4sr+e+evzo5P7+vEyOIupplLTrJDuwiKFrxrHBBe1p28NX327j WUWZJ8Yu6CgJWCxzT06R43+THG5eVIdSgCaUHvBcTnCZ7J6Th6I/lzmRV+Vm37ZXgL7q wRMScUA9QFeQ7lLZX4tYE/oa+77JpYpQPTCi0OjY6vN/+OqbYLBJ0HV0Wv/cJ8Zs0c5p Pa1HMGQ24ErjFDmSDtmr6EUo0ViWvB86qBBPOMaMNwj0NIduk0vgs/gy+yDSxG915Men GJJA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=RMkg+G8b; spf=pass (google.com: domain of gcc-patches-return-464814-patch=linaro.org@gcc.gnu.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=gcc-patches-return-464814-patch=linaro.org@gcc.gnu.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from sourceware.org (server1.sourceware.org. [209.132.180.131]) by mx.google.com with ESMTPS id a6si4283960pll.474.2017.10.23.10.16.31 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 23 Oct 2017 10:16:32 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-return-464814-patch=linaro.org@gcc.gnu.org designates 209.132.180.131 as permitted sender) client-ip=209.132.180.131; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=RMkg+G8b; spf=pass (google.com: domain of gcc-patches-return-464814-patch=linaro.org@gcc.gnu.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=gcc-patches-return-464814-patch=linaro.org@gcc.gnu.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:from :to:subject:references:date:in-reply-to:message-id:mime-version :content-type; q=dns; s=default; b=cSHAKFSCzoDHAdHzE1xxPiPEVo/8n fg4cg3nACU155KEl4XkZaSusGwH/bo2s0gRIWo98efgs/41hFM8oW82c1Zq1eIo+ C75dZqQ09TaEKmwQqVp52g9QG+cIGy3mOnUYsr/ZzMXCFyKyEx4f/mZf+BWbce3k kho2Beqqs0kxlY= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:from :to:subject:references:date:in-reply-to:message-id:mime-version :content-type; s=default; bh=8vuM9c/Khh7l2vhqDJOmU6LUrXo=; b=RMk g+G8b36o1a9tqC2uyeICKyptWoBTLIVCIl8t41nnoZNx8SR9+FNgOv8Yhtq7/uAR VM/vxOvTJwHCu401O+bE1kbyCXctCsKk0sEGg52qCcqTW7JeAhlcPP5B250nGQlJ VBcwqEcUdPNLrzsxMJG09NLgvFBF0MRepGsWVjqc= Received: (qmail 34508 invoked by alias); 23 Oct 2017 17:16:16 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 34489 invoked by uid 89); 23 Oct 2017 17:16:16 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-11.3 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, RCVD_IN_SORBS_SPAM, SPF_PASS autolearn=ham version=3.3.2 spammy= X-HELO: mail-wr0-f174.google.com Received: from mail-wr0-f174.google.com (HELO mail-wr0-f174.google.com) (209.85.128.174) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Mon, 23 Oct 2017 17:16:14 +0000 Received: by mail-wr0-f174.google.com with SMTP id g90so18135616wrd.6 for ; Mon, 23 Oct 2017 10:16:14 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:mail-followup-to:subject:references:date :in-reply-to:message-id:user-agent:mime-version; bh=/R9p0EPAVZRutMGavwECPCAzD0LKssBG4vC76XLLjH4=; b=sDne4wyi4wNXq/H1baS6/jCMzTcNYt7NaMo33al/+ekF7el5V5aW55R+l7ZPSM919B bJ2kobcNcsTSCE/FEOYhp4bdPpn/y0aAXVIuQ2eYcLkymIyprGWoOI7QEr7xHumC5UR0 KChR5anay2n80h24iI17CHLVlFV7JUEP3CMrb5bnX2nzJZfsRoeexE0joK3a0q2IsYtm PcpmBZusyy0B4YbkOzJrIgzr5uT8ZRjqg3ErX7tVfu7aThWp6laj/albOszvVR0D0Qc1 67HXWm7JyleHh+7suzDBf/xElF81/kvrhqN0+EOLH+1BQUxE/XQFoFrIfWYPSdCOXhQS OfQA== X-Gm-Message-State: AMCzsaUBKbrKnkxs8O/S+c03fLAMLFML2VxA/vUqPwAcbc0TvRTC0+VS foYZihkt7WHRCW/2l/iX3UEKknMQnWE= X-Google-Smtp-Source: ABhQp+Qo+4nkKy8OjreMoKviy443rBI/bxlTAhFNXot0EjR6E7cF0WoUWVzRVWSRgkf40WuGNc2ttw== X-Received: by 10.223.173.104 with SMTP id p95mr11049434wrc.209.1508778972260; Mon, 23 Oct 2017 10:16:12 -0700 (PDT) Received: from localhost ([2.26.27.199]) by smtp.gmail.com with ESMTPSA id v78sm3091602wmv.48.2017.10.23.10.16.11 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Mon, 23 Oct 2017 10:16:11 -0700 (PDT) From: Richard Sandiford To: gcc-patches@gcc.gnu.org Mail-Followup-To: gcc-patches@gcc.gnu.org, richard.sandiford@linaro.org Subject: [038/nnn] poly_int: fold_comparison References: <871sltvm7r.fsf@linaro.org> Date: Mon, 23 Oct 2017 18:16:10 +0100 In-Reply-To: <871sltvm7r.fsf@linaro.org> (Richard Sandiford's message of "Mon, 23 Oct 2017 17:54:32 +0100") Message-ID: <87bmkxokdh.fsf@linaro.org> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.2 (gnu/linux) MIME-Version: 1.0 This patch makes fold_comparison track polynomial offsets when folding address comparisons. 2017-10-23 Richard Sandiford Alan Hayward David Sherwood gcc/ * fold-const.c (fold_comparison): Track sizes and offsets as poly_int64s rather than HOST_WIDE_INTs when folding address comparisons. Index: gcc/fold-const.c =================================================================== --- gcc/fold-const.c 2017-10-23 17:18:43.843393046 +0100 +++ gcc/fold-const.c 2017-10-23 17:18:44.902299961 +0100 @@ -8522,7 +8522,7 @@ fold_comparison (location_t loc, enum tr || TREE_CODE (arg1) == POINTER_PLUS_EXPR)) { tree base0, base1, offset0 = NULL_TREE, offset1 = NULL_TREE; - HOST_WIDE_INT bitsize, bitpos0 = 0, bitpos1 = 0; + poly_int64 bitsize, bitpos0 = 0, bitpos1 = 0; machine_mode mode; int volatilep, reversep, unsignedp; bool indirect_base0 = false, indirect_base1 = false; @@ -8563,17 +8563,14 @@ fold_comparison (location_t loc, enum tr else offset0 = size_binop (PLUS_EXPR, offset0, TREE_OPERAND (arg0, 1)); - if (TREE_CODE (offset0) == INTEGER_CST) + if (poly_int_tree_p (offset0)) { - offset_int tem = wi::sext (wi::to_offset (offset0), - TYPE_PRECISION (sizetype)); + poly_offset_int tem = wi::sext (wi::to_poly_offset (offset0), + TYPE_PRECISION (sizetype)); tem <<= LOG2_BITS_PER_UNIT; tem += bitpos0; - if (wi::fits_shwi_p (tem)) - { - bitpos0 = tem.to_shwi (); - offset0 = NULL_TREE; - } + if (tem.to_shwi (&bitpos0)) + offset0 = NULL_TREE; } } @@ -8609,17 +8606,14 @@ fold_comparison (location_t loc, enum tr else offset1 = size_binop (PLUS_EXPR, offset1, TREE_OPERAND (arg1, 1)); - if (TREE_CODE (offset1) == INTEGER_CST) + if (poly_int_tree_p (offset1)) { - offset_int tem = wi::sext (wi::to_offset (offset1), - TYPE_PRECISION (sizetype)); + poly_offset_int tem = wi::sext (wi::to_poly_offset (offset1), + TYPE_PRECISION (sizetype)); tem <<= LOG2_BITS_PER_UNIT; tem += bitpos1; - if (wi::fits_shwi_p (tem)) - { - bitpos1 = tem.to_shwi (); - offset1 = NULL_TREE; - } + if (tem.to_shwi (&bitpos1)) + offset1 = NULL_TREE; } } @@ -8635,7 +8629,7 @@ fold_comparison (location_t loc, enum tr && operand_equal_p (offset0, offset1, 0))) { if (!equality_code - && bitpos0 != bitpos1 + && may_ne (bitpos0, bitpos1) && (pointer_may_wrap_p (base0, offset0, bitpos0) || pointer_may_wrap_p (base1, offset1, bitpos1))) fold_overflow_warning (("assuming pointer wraparound does not " @@ -8646,17 +8640,41 @@ fold_comparison (location_t loc, enum tr switch (code) { case EQ_EXPR: - return constant_boolean_node (bitpos0 == bitpos1, type); + if (must_eq (bitpos0, bitpos1)) + return boolean_true_node; + if (must_ne (bitpos0, bitpos1)) + return boolean_false_node; + break; case NE_EXPR: - return constant_boolean_node (bitpos0 != bitpos1, type); + if (must_ne (bitpos0, bitpos1)) + return boolean_true_node; + if (must_eq (bitpos0, bitpos1)) + return boolean_false_node; + break; case LT_EXPR: - return constant_boolean_node (bitpos0 < bitpos1, type); + if (must_lt (bitpos0, bitpos1)) + return boolean_true_node; + if (must_ge (bitpos0, bitpos1)) + return boolean_false_node; + break; case LE_EXPR: - return constant_boolean_node (bitpos0 <= bitpos1, type); + if (must_le (bitpos0, bitpos1)) + return boolean_true_node; + if (must_gt (bitpos0, bitpos1)) + return boolean_false_node; + break; case GE_EXPR: - return constant_boolean_node (bitpos0 >= bitpos1, type); + if (must_ge (bitpos0, bitpos1)) + return boolean_true_node; + if (must_lt (bitpos0, bitpos1)) + return boolean_false_node; + break; case GT_EXPR: - return constant_boolean_node (bitpos0 > bitpos1, type); + if (must_gt (bitpos0, bitpos1)) + return boolean_true_node; + if (must_le (bitpos0, bitpos1)) + return boolean_false_node; + break; default:; } } @@ -8667,7 +8685,7 @@ fold_comparison (location_t loc, enum tr because pointer arithmetic is restricted to retain within an object and overflow on pointer differences is undefined as of 6.5.6/8 and /9 with respect to the signed ptrdiff_t. */ - else if (bitpos0 == bitpos1) + else if (must_eq (bitpos0, bitpos1)) { /* By converting to signed sizetype we cover middle-end pointer arithmetic which operates on unsigned pointer types of size @@ -8696,7 +8714,7 @@ fold_comparison (location_t loc, enum tr } /* For equal offsets we can simplify to a comparison of the base addresses. */ - else if (bitpos0 == bitpos1 + else if (must_eq (bitpos0, bitpos1) && (indirect_base0 ? base0 != TREE_OPERAND (arg0, 0) : base0 != arg0) && (indirect_base1 @@ -8725,7 +8743,7 @@ fold_comparison (location_t loc, enum tr eliminated. When ptr is null, although the -> expression is strictly speaking invalid, GCC retains it as a matter of QoI. See PR c/44555. */ - && (offset0 == NULL_TREE && bitpos0 != 0)) + && (offset0 == NULL_TREE && known_nonzero (bitpos0))) || CONSTANT_CLASS_P (base0)) && indirect_base0 /* The caller guarantees that when one of the arguments is