From patchwork Fri Aug 18 15:40:11 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aldy Hernandez X-Patchwork-Id: 1822991 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=WJYndGNR; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (ip-8-43-85-97.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4RS5gl1RDQz1yfn for ; Sat, 19 Aug 2023 01:41:03 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id C10FF3850400 for ; Fri, 18 Aug 2023 15:40:58 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C10FF3850400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1692373258; bh=LlvErZzIkaicV8zeBUPFLO0ygBOqTj1SxzsXpFzGx6w=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=WJYndGNRGLB6aSBpFXT8Ktir1WCHy1puWPvDyXhhlcquaII0ysoGReRHJoT+3LSdU fM20JcFSbMPZk/MTiM+eaLfpsLLAwQ16+MQY4jN6F5nxl7F5Gg9wO+5LEx4LhFK4P/ eNMEGYRpRhBQWNVOZw2fCqz7LYiDAv+gD3lPmvHY= 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 DEDC138515DB for ; Fri, 18 Aug 2023 15:40:38 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org DEDC138515DB 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-335-9QW6eMWXMLS5M_DUDq0AjQ-1; Fri, 18 Aug 2023 11:40:37 -0400 X-MC-Unique: 9QW6eMWXMLS5M_DUDq0AjQ-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id D9761101A528 for ; Fri, 18 Aug 2023 15:40:36 +0000 (UTC) Received: from abulafia.quesejoda.com (unknown [10.39.192.63]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 6F7A26403D; Fri, 18 Aug 2023 15:40:36 +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 37IFeXTM064021 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Fri, 18 Aug 2023 17:40:34 +0200 Received: (from aldyh@localhost) by abulafia.quesejoda.com (8.17.1/8.17.1/Submit) id 37IFeXW8064020; Fri, 18 Aug 2023 17:40:33 +0200 To: GCC patches Cc: Andrew MacLeod , Aldy Hernandez Subject: [COMMITTED] [irange] Return FALSE if updated bitmask is unchanged [PR110753] Date: Fri, 18 Aug 2023 17:40:11 +0200 Message-ID: <20230818154030.64004-1-aldyh@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.5 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.4 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_H4, RCVD_IN_MSPIKE_WL, 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+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" The mask/value pair we track in the irange is a bit fickle in that it can sometimes contradict the bitmask inherent in the range. This can happen when a series of calculations yield a combination such as: [3, 1000] MASK 0xfffffffe VALUE 0x0 The mask/value above implies that the lowest bit is a known 0, which would exclude the 3 in the range. At one time we tried keeping mask and ranges 100% consistent, but the performance penalty was too high (5% in VRP). Also, it's unclear whether the intersection of two incompatible known bits should make the whole range undefined, or just the contradicting bits. This is all documented in irange::get_bitmask(). We could revisit both of these assumptions in the future. In this testcase IPA ends up with a range where the lower 2 bits are expected to be 0, but the range is [1,1]. [irange] long int [1, 1] MASK 0xfffffffffffffffc VALUE 0x0 This causes irange::union_bitmask() to think an update occurred, when no semantic change happened, thus triggering an assert in IPA-cp. We could get rid of the assert, but it's cleaner to make irange::{union,intersect}_bitmask always tell the truth. Beside, the ranger's cache also depends on union being truthful. PR ipa/110753 gcc/ChangeLog: * value-range.cc (irange::union_bitmask): Return FALSE if updated bitmask is semantically equivalent to the original mask. (irange::intersect_bitmask): Same. (irange::get_bitmask): Add comment. gcc/testsuite/ChangeLog: * gcc.dg/tree-ssa/pr110753.c: New test. --- gcc/testsuite/gcc.dg/tree-ssa/pr110753.c | 15 +++++++++++++++ gcc/value-range.cc | 18 ++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr110753.c diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr110753.c b/gcc/testsuite/gcc.dg/tree-ssa/pr110753.c new file mode 100644 index 00000000000..aa02487e2a7 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr110753.c @@ -0,0 +1,15 @@ +// { dg-do compile } +// { dg-options "-O2" } + +int a, b, c; +static int d(long e, long f) { return f == 0 || e && f == 1 ?: f; } +int g(void) {static int t; return t;} +static void h(long e) { + b = e - 1; + a = d(b || d(e, 8), g()); +} +int tt; +void i(void) { + c = (__SIZE_TYPE__)&tt; + h(c); +} diff --git a/gcc/value-range.cc b/gcc/value-range.cc index 2abf57bcee8..76f88d91046 100644 --- a/gcc/value-range.cc +++ b/gcc/value-range.cc @@ -1876,6 +1876,8 @@ irange::get_bitmask () const // // 3 is in the range endpoints, but is excluded per the known 0 bits // in the mask. + // + // See also the note in irange_bitmask::intersect. irange_bitmask bm = get_bitmask_from_range (); if (!m_bitmask.unknown_p ()) bm.intersect (m_bitmask); @@ -1915,10 +1917,18 @@ irange::intersect_bitmask (const irange &r) return false; irange_bitmask bm = get_bitmask (); + irange_bitmask save = bm; if (!bm.intersect (r.get_bitmask ())) return false; m_bitmask = bm; + + // Updating m_bitmask may still yield a semantic bitmask (as + // returned by get_bitmask) which is functionally equivalent to what + // we originally had. In which case, there's still no change. + if (save == get_bitmask ()) + return false; + if (!set_range_from_bitmask ()) normalize_kind (); if (flag_checking) @@ -1938,10 +1948,18 @@ irange::union_bitmask (const irange &r) return false; irange_bitmask bm = get_bitmask (); + irange_bitmask save = bm; if (!bm.union_ (r.get_bitmask ())) return false; m_bitmask = bm; + + // Updating m_bitmask may still yield a semantic bitmask (as + // returned by get_bitmask) which is functionally equivalent to what + // we originally had. In which case, there's still no change. + if (save == get_bitmask ()) + return false; + // No need to call set_range_from_mask, because we'll never // narrow the range. Besides, it would cause endless recursion // because of the union_ in set_range_from_mask.