From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1675414252; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=DwJCWEevHQQgYpIV8mI1Rk2robo2gjB4jkpEsEXjiGI=; b=hMjqjE6PYmsrgdQ5W8wzhMdhGod2PQqB5VsIhpyTwFfEnvso7nGeFRu52VxQ/HNsS5Cuod RW0oYDRJEM1fICk8BBHLc8jNzcBspmh1iPhYYXkVDS4z4sABq0DU7oMxWpwjP5nwN2P2JZ 77+ifLf1QUd57KwkrJn6X0U9R4n5UUA= 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 From: Aldy Hernandez 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 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 List-Id: 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. */ -- 2.39.1