From patchwork Fri Feb 3 08:50:43 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aldy Hernandez X-Patchwork-Id: 64235 Return-Path: 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 ; 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 ; 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 ; 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 Cc: Andrew MacLeod , GCC patches , Aldy Hernandez 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 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Aldy Hernandez via Gcc-patches From: Aldy Hernandez Reply-To: Aldy Hernandez Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" 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 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. */