Message ID | 20230203085043.157321-1-aldyh@redhat.com |
---|---|
State | Committed |
Commit | e261fcefb71e1270673f0457fcc73711f13d3079 |
Headers |
Return-Path: <gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org> X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 4A5B4385842D for <patchwork@sourceware.org>; Fri, 3 Feb 2023 08:51:23 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4A5B4385842D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1675414283; bh=DwJCWEevHQQgYpIV8mI1Rk2robo2gjB4jkpEsEXjiGI=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=MDD/nLxWHhY2h7P9sqnuI0V5u6kCRghB1Im1oCT2NZEZZ5R3hVkZdquKrmLC4ceIk 7uULD15M2wwV268GPAIOWLxECQLDUiI4wFBJ8yOvaqnrXmcqvXEOhREPHCeAw1M9yI ohZnSU5BrQPd9vfeRY62xURvb++5EoVhb68K3k+A= 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 F3CC33858C52 for <gcc-patches@gcc.gnu.org>; Fri, 3 Feb 2023 08:50:52 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org F3CC33858C52 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-607-u8E0vvtJNCmnWqG_3JD1Bg-1; Fri, 03 Feb 2023 03:50:51 -0500 X-MC-Unique: u8E0vvtJNCmnWqG_3JD1Bg-1 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 12E8E88646B for <gcc-patches@gcc.gnu.org>; Fri, 3 Feb 2023 08:50:51 +0000 (UTC) Received: from abulafia.quesejoda.com (unknown [10.39.193.28]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 9FDC5492C14; Fri, 3 Feb 2023 08:50:50 +0000 (UTC) Received: from abulafia.quesejoda.com (localhost [127.0.0.1]) by abulafia.quesejoda.com (8.17.1/8.17.1) with ESMTPS id 3138omWA157362 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Fri, 3 Feb 2023 09:50:48 +0100 Received: (from aldyh@localhost) by abulafia.quesejoda.com (8.17.1/8.17.1/Submit) id 3138omtK157361; Fri, 3 Feb 2023 09:50:48 +0100 To: Jakub Jelinek <jakub@redhat.com> Cc: Andrew MacLeod <amacleod@redhat.com>, GCC patches <gcc-patches@gcc.gnu.org>, Aldy Hernandez <aldyh@redhat.com> Subject: [PATCH] [PR tree-optimization/18639] Compare nonzero bits in irange with widest_int. Date: Fri, 3 Feb 2023 09:50:43 +0100 Message-Id: <20230203085043.157321-1-aldyh@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.9 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true X-Spam-Status: No, score=-11.3 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 <gcc-patches.gcc.gnu.org> List-Unsubscribe: <https://gcc.gnu.org/mailman/options/gcc-patches>, <mailto:gcc-patches-request@gcc.gnu.org?subject=unsubscribe> List-Archive: <https://gcc.gnu.org/pipermail/gcc-patches/> List-Post: <mailto:gcc-patches@gcc.gnu.org> List-Help: <mailto:gcc-patches-request@gcc.gnu.org?subject=help> List-Subscribe: <https://gcc.gnu.org/mailman/listinfo/gcc-patches>, <mailto:gcc-patches-request@gcc.gnu.org?subject=subscribe> From: Aldy Hernandez via Gcc-patches <gcc-patches@gcc.gnu.org> Reply-To: Aldy Hernandez <aldyh@redhat.com> Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" <gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org> |
Series |
[PR,tree-optimization/18639] Compare nonzero bits in irange with widest_int.
|
|
Commit Message
Aldy Hernandez
Feb. 3, 2023, 8:50 a.m. UTC
The problem here is we are trying to compare two ranges with different precisions and the == operator in wide_int is complaining. Interestingly, the problem is not the nonzero bits, but the fact that the entire ranges have different precisions. The reason we don't ICE when comparing the sub-ranges, is because the code in irange::operator== works on trees, and tree_int_cst_equal is promoting the comparison to a widest int: if (TREE_CODE (t1) == INTEGER_CST && TREE_CODE (t2) == INTEGER_CST && wi::to_widest (t1) == wi::to_widest (t2)) return 1; This is why we don't see the ICE until the nonzero bits comparison is done on wide ints. I think we should maintain the current equality behavior, and follow suit in the nonzero bit comparison. I have also fixed the legacy equality code, even though technically nonzero bits shouldn't appear in legacy. But better safe than sorry. PR 108639/tree-optimization Re-running tests with Jakub's testcases for both PR108638 and PR108639. OK pending tests? gcc/ChangeLog: * value-range.cc (irange::legacy_equal_p): Compare nonzero bits as widest_int. (irange::operator==): Same. --- gcc/testsuite/gcc.c-torture/compile/pr108638.c | 12 ++++++++++++ gcc/testsuite/gcc.c-torture/compile/pr108639.c | 11 +++++++++++ gcc/value-range.cc | 11 +++++++++-- 3 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 gcc/testsuite/gcc.c-torture/compile/pr108638.c create mode 100644 gcc/testsuite/gcc.c-torture/compile/pr108639.c
Comments
On Fri, Feb 03, 2023 at 09:50:43AM +0100, Aldy Hernandez wrote: > [PR tree-optimization/18639] Compare nonzero bits in irange with widest_int. 0 missing in the bug number in the subject line, though the current recommended formatting of the subject is I think: value-range: Compare nonzero bits in irange with widest_int [PR180639] PR 108639/tree-optimization Reversed component and number > --- a/gcc/value-range.cc > +++ b/gcc/value-range.cc > @@ -1259,7 +1259,10 @@ irange::legacy_equal_p (const irange &other) const > other.tree_lower_bound (0)) > && vrp_operand_equal_p (tree_upper_bound (0), > other.tree_upper_bound (0)) > - && get_nonzero_bits () == other.get_nonzero_bits ()); > + && (widest_int::from (get_nonzero_bits (), > + TYPE_SIGN (type ())) > + == widest_int::from (other.get_nonzero_bits (), > + TYPE_SIGN (other.type ())))); > } > > bool > @@ -1294,7 +1297,11 @@ irange::operator== (const irange &other) const > || !operand_equal_p (ub, ub_other, 0)) > return false; > } > - return get_nonzero_bits () == other.get_nonzero_bits (); > + widest_int nz1 = widest_int::from (get_nonzero_bits (), > + TYPE_SIGN (type ())); > + widest_int nz2 = widest_int::from (other.get_nonzero_bits (), > + TYPE_SIGN (other.type ())); > + return nz1 == nz2; > } While the above avoids the ICE (and would be certainly correct for the bounds, depending on the sign of their type sign or zero extended to widest int), but is the above what we want for non-zero bits to be considered equal? The wide_ints (which ought to have precision of the corresponding type) don't represent normal numbers but bitmasks, 0 - this bit is known to be zero, 1 - nothing is known about this bit). So, if there are different precisions and the narrower value has 0 in the MSB of the bitmask (so MSB is known to be zero), the above requires for equality that in the other range all upper bits are known to be zero too for both signed and unsigned. That is ok. Similarly for MSB set if TYPE_SIGN of the narrower is unsigned, the MSB value is unknown, but we require on the wider to have all the upper bits cleared. But for signed narrower type with MSB set, i.e. it is unknown if it is positive or negative, the above requires that all the above bits are unknown too. And that is the case I'm not sure about, whether in that case the upper bits of the wider wide_int should be checked at all. Though, perhaps from the POV of nonzero bits derived from the sign-extended values in the ranges sign bit copies (so all above bits 1) is what one would get, so maybe it is ok. Just food for thought. As for retesting, if you have done full bootstrap/regtest with the patch without the testcases in it, it should be more than enough to test just make check-gcc \ RUNTESTFLAGS='--target_board=unix\{-m32,-m64\} compile.exp=pr10863*.c' You don't really need to rerun all tests just for it. Jakub
On 2/3/23 04:16, Jakub Jelinek wrote: > On Fri, Feb 03, 2023 at 09:50:43AM +0100, Aldy Hernandez wrote: >> [PR tree-optimization/18639] Compare nonzero bits in irange with widest_int. > 0 missing in the bug number in the subject line, though the current > recommended formatting of the subject is I think: > value-range: Compare nonzero bits in irange with widest_int [PR180639] > > PR 108639/tree-optimization > > Reversed component and number > >> --- a/gcc/value-range.cc >> +++ b/gcc/value-range.cc >> @@ -1259,7 +1259,10 @@ irange::legacy_equal_p (const irange &other) const >> other.tree_lower_bound (0)) >> && vrp_operand_equal_p (tree_upper_bound (0), >> other.tree_upper_bound (0)) >> - && get_nonzero_bits () == other.get_nonzero_bits ()); >> + && (widest_int::from (get_nonzero_bits (), >> + TYPE_SIGN (type ())) >> + == widest_int::from (other.get_nonzero_bits (), >> + TYPE_SIGN (other.type ())))); >> } >> >> bool >> @@ -1294,7 +1297,11 @@ irange::operator== (const irange &other) const >> || !operand_equal_p (ub, ub_other, 0)) >> return false; >> } >> - return get_nonzero_bits () == other.get_nonzero_bits (); >> + widest_int nz1 = widest_int::from (get_nonzero_bits (), >> + TYPE_SIGN (type ())); >> + widest_int nz2 = widest_int::from (other.get_nonzero_bits (), >> + TYPE_SIGN (other.type ())); >> + return nz1 == nz2; >> } > While the above avoids the ICE (and would be certainly correct for > the bounds, depending on the sign of their type sign or zero extended > to widest int), but is the above what we want for non-zero bits > to be considered equal? The wide_ints (which ought to have precision > of the corresponding type) don't represent normal numbers but bitmasks, > 0 - this bit is known to be zero, 1 - nothing is known about this bit). > So, if there are different precisions and the narrower value has 0 > in the MSB of the bitmask (so MSB is known to be zero), the above requires > for equality that in the other range all upper bits are known to be zero > too for both signed and unsigned. That is ok. Similarly for MSB set > if TYPE_SIGN of the narrower is unsigned, the MSB value is unknown, but we > require on the wider to have all the upper bits cleared. But for signed > narrower type with MSB set, i.e. it is unknown if it is positive or > negative, the above requires that all the above bits are unknown too. > And that is the case I'm not sure about, whether in that case the > upper bits of the wider wide_int should be checked at all. > Though, perhaps from the POV of nonzero bits derived from the sign-extended > values in the ranges sign bit copies (so all above bits 1) is what one would > get, so maybe it is ok. Just food for thought. > if the bits match exactly along with everything else, then we can be sure the ranges are truly equal. If for some reason the numbers are all the same but the non-zero bits don't compare equal, then I can't think of what harm it could cause to compare unequal.. Worst case is we dont perform some optimization in this extremely rare scenario of differing precisions. And in fact they could actually be unequal... So I suspect this is fine... Andrew
On Fri, Feb 03, 2023 at 11:23:28AM -0500, Andrew MacLeod wrote: > > On 2/3/23 04:16, Jakub Jelinek wrote: > > On Fri, Feb 03, 2023 at 09:50:43AM +0100, Aldy Hernandez wrote: > > > [PR tree-optimization/18639] Compare nonzero bits in irange with widest_int. > > 0 missing in the bug number in the subject line, though the current > > recommended formatting of the subject is I think: > > value-range: Compare nonzero bits in irange with widest_int [PR180639] > > PR 108639/tree-optimization > > > > Reversed component and number > > > > > --- a/gcc/value-range.cc > > > +++ b/gcc/value-range.cc > > > @@ -1259,7 +1259,10 @@ irange::legacy_equal_p (const irange &other) const > > > other.tree_lower_bound (0)) > > > && vrp_operand_equal_p (tree_upper_bound (0), > > > other.tree_upper_bound (0)) > > > - && get_nonzero_bits () == other.get_nonzero_bits ()); > > > + && (widest_int::from (get_nonzero_bits (), > > > + TYPE_SIGN (type ())) > > > + == widest_int::from (other.get_nonzero_bits (), > > > + TYPE_SIGN (other.type ())))); > > > } > > > bool > > > @@ -1294,7 +1297,11 @@ irange::operator== (const irange &other) const > > > || !operand_equal_p (ub, ub_other, 0)) > > > return false; > > > } > > > - return get_nonzero_bits () == other.get_nonzero_bits (); > > > + widest_int nz1 = widest_int::from (get_nonzero_bits (), > > > + TYPE_SIGN (type ())); > > > + widest_int nz2 = widest_int::from (other.get_nonzero_bits (), > > > + TYPE_SIGN (other.type ())); > > > + return nz1 == nz2; > > > } > > While the above avoids the ICE (and would be certainly correct for > > the bounds, depending on the sign of their type sign or zero extended > > to widest int), but is the above what we want for non-zero bits > > to be considered equal? The wide_ints (which ought to have precision > > of the corresponding type) don't represent normal numbers but bitmasks, > > 0 - this bit is known to be zero, 1 - nothing is known about this bit). > > So, if there are different precisions and the narrower value has 0 > > in the MSB of the bitmask (so MSB is known to be zero), the above requires > > for equality that in the other range all upper bits are known to be zero > > too for both signed and unsigned. That is ok. Similarly for MSB set > > if TYPE_SIGN of the narrower is unsigned, the MSB value is unknown, but we > > require on the wider to have all the upper bits cleared. But for signed > > narrower type with MSB set, i.e. it is unknown if it is positive or > > negative, the above requires that all the above bits are unknown too. > > And that is the case I'm not sure about, whether in that case the > > upper bits of the wider wide_int should be checked at all. > > Though, perhaps from the POV of nonzero bits derived from the sign-extended > > values in the ranges sign bit copies (so all above bits 1) is what one would > > get, so maybe it is ok. Just food for thought. > > > if the bits match exactly along with everything else, then we can be sure > the ranges are truly equal. If for some reason the numbers are all the same > but the non-zero bits don't compare equal, then I can't think of what harm > it could cause to compare unequal.. Worst case is we dont perform some > optimization in this extremely rare scenario of differing precisions. And > in fact they could actually be unequal... > > So I suspect this is fine... Ok then. Jakub
diff --git a/gcc/testsuite/gcc.c-torture/compile/pr108638.c b/gcc/testsuite/gcc.c-torture/compile/pr108638.c new file mode 100644 index 00000000000..755c151a09a --- /dev/null +++ b/gcc/testsuite/gcc.c-torture/compile/pr108638.c @@ -0,0 +1,12 @@ +/* PR tree-optimization/108638 */ + +long long a; +int b; + +void +foo (void) +{ + for (a = 0; a < __SIZEOF_LONG_LONG__ * __CHAR_BIT__; a++) + if (b) + b |= a << a; +} diff --git a/gcc/testsuite/gcc.c-torture/compile/pr108639.c b/gcc/testsuite/gcc.c-torture/compile/pr108639.c new file mode 100644 index 00000000000..ed826cc2f5a --- /dev/null +++ b/gcc/testsuite/gcc.c-torture/compile/pr108639.c @@ -0,0 +1,11 @@ +/* PR tree-optimization/108639 */ + +long long a; + +int +main () +{ + a = a ? 0 || 0 % 0 : 0; + a = a << a; + return 0; +} diff --git a/gcc/value-range.cc b/gcc/value-range.cc index 26f6f26b01a..a535337c47a 100644 --- a/gcc/value-range.cc +++ b/gcc/value-range.cc @@ -1259,7 +1259,10 @@ irange::legacy_equal_p (const irange &other) const other.tree_lower_bound (0)) && vrp_operand_equal_p (tree_upper_bound (0), other.tree_upper_bound (0)) - && get_nonzero_bits () == other.get_nonzero_bits ()); + && (widest_int::from (get_nonzero_bits (), + TYPE_SIGN (type ())) + == widest_int::from (other.get_nonzero_bits (), + TYPE_SIGN (other.type ())))); } bool @@ -1294,7 +1297,11 @@ irange::operator== (const irange &other) const || !operand_equal_p (ub, ub_other, 0)) return false; } - return get_nonzero_bits () == other.get_nonzero_bits (); + widest_int nz1 = widest_int::from (get_nonzero_bits (), + TYPE_SIGN (type ())); + widest_int nz2 = widest_int::from (other.get_nonzero_bits (), + TYPE_SIGN (other.type ())); + return nz1 == nz2; } /* Return TRUE if this is a symbolic range. */