From patchwork Tue Jan 31 20:10:39 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Andrew MacLeod X-Patchwork-Id: 64044 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 F2BD63858414 for ; Tue, 31 Jan 2023 20:11:12 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org F2BD63858414 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1675195873; bh=FbYXeIgCkzDFFCFfV1tVOnh4NJMzoX8a1nKW+UQaVnE=; h=Date:To:Cc:Subject:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=ELOvaYyPrWVb4DR0xG9/SvdAsCHAlbcpr1wIWSEAmlO1T+kI9Pc0PLRNLXB0mvRVi w/dh6Iwq2NmeMzPGaLayoh27a7T+Z2KFAxFA2Wx4TYSxCXzR52Zo+aVjWvVzvEvX5S OLNCNC2JX4IYgmxRsVFleCh25EOgISgiUGu9ZWvM= 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.133.124]) by sourceware.org (Postfix) with ESMTPS id 8B67E3858D33 for ; Tue, 31 Jan 2023 20:10:44 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 8B67E3858D33 Received: from mail-qk1-f199.google.com (mail-qk1-f199.google.com [209.85.222.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-620-qP_KdCCMNOKhlTxWQbnKdw-1; Tue, 31 Jan 2023 15:10:42 -0500 X-MC-Unique: qP_KdCCMNOKhlTxWQbnKdw-1 Received: by mail-qk1-f199.google.com with SMTP id g6-20020ae9e106000000b00720f9e6e3e2so3533989qkm.13 for ; Tue, 31 Jan 2023 12:10:42 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=subject:cc:to:from:content-language:user-agent:mime-version:date :message-id:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=7xVF6YqKn/S21Yut1VWx95sX9Tod3dypcdgrKMpG0j4=; b=E/lttoo1H6hxW/aodapetUWQ3OsxDmK9H7HfM4Rr9A1GiIQKUsDdPNABbW7kG0OF/E JWp0u93slpmx/Ia+Dzdx+ATJSOGBImD8rLHepvcLmsDm7cstbeYSWboYXHLC2eus6aEN jUG9s+iIdYUcN8yjb1ClVFkIWiN83WkSbMlt6V57euE9T0SGiEK3eolMloVQYGYS4V2V 5QuEjc6UWpDpA/VJpY8oI1x34jO/11bBI4sykRn3LD44Fg/zXSxcvw3krSvLOdEP4y0P SDsZQHl7MIp5RwelHw4oLQVM5x7QoaDsJKn4dgXYN6EdwDcWRsGX6XuCpte4u7zUUCy6 rejQ== X-Gm-Message-State: AO0yUKX7ZHxJxUvvwOHGB9it3NMZWyMd3S4lo2PbY4Wfb/+Wp3r1MFc6 NLqBHz6ADLggOXMJxCmTRmQc2BZrzOYNMRiQLrzi7CdDXVaHpWghuqNDVlupM8vVjr4XoeFiyff DLvWXUPlBKdT7aiXLEQeFmy6jAj+h/Zv6nqbDnk9shq2eLjc2e9rwlUKIQ6ksf8EaD9pTHA== X-Received: by 2002:ac8:7d94:0:b0:3b8:5057:377b with SMTP id c20-20020ac87d94000000b003b85057377bmr19919567qtd.65.1675195841932; Tue, 31 Jan 2023 12:10:41 -0800 (PST) X-Google-Smtp-Source: AK7set/UHS4DKezaJEwiS2oDFQYeBihyZqyGz9e4y4+EG8JuJlMSS7llhfLhF6DBg6duIFdJCivTkQ== X-Received: by 2002:ac8:7d94:0:b0:3b8:5057:377b with SMTP id c20-20020ac87d94000000b003b85057377bmr19919531qtd.65.1675195841595; Tue, 31 Jan 2023 12:10:41 -0800 (PST) Received: from ?IPV6:2607:fea8:a263:f600::fa90? ([2607:fea8:a263:f600::fa90]) by smtp.gmail.com with ESMTPSA id u9-20020ae9c009000000b0072526a43ef7sm2225362qkk.120.2023.01.31.12.10.40 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 31 Jan 2023 12:10:40 -0800 (PST) Message-ID: <4edb00e2-4691-bdd4-1b4d-12e6b9983ad7@redhat.com> Date: Tue, 31 Jan 2023 15:10:39 -0500 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.6.0 To: gcc-patches Cc: "hernandez, aldy" Subject: [PATCH] PR tree-optimization/108356 - Ranger cache - always use range_from_dom when updating. X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US X-Spam-Status: No, score=-12.1 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: Andrew MacLeod via Gcc-patches From: Andrew MacLeod Reply-To: Andrew MacLeod Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" This turned out to be a more interesting problem than I wanted. the situation boils down to: # g_5 = PHI <0(2), 2(8)>   if (g_5 <= 1)     goto ; [INV] :   if (g_5 != 0)     goto ; [INV]   else     goto ; [INV]   :   c = 0;   :     goto ; [INV]  We globally know that g_5 is [0,0][2,2] we also know that 10->4 , g_5 will be [0,0] Which means we also know that that the branch in bb_4 will never be taken, and will always go to bb 8. THis is all processed in EVRP, the branch is changed, and the next time VRP is called, we blow the block with c = 0 like we want... Unfortunately it doesnt happen within EVRP because when this updated range for g_5 is propagated in the cache, it was tripping over a shotcut which tried to avoid using lookups when it thinks it didnt matter, and would occasionally drop back to the global range. In particular, the cache had originally propagated [0,0][2,2] as the on entry range to bb8. when we rewrite the branch, we mark 4->7 and 7->8  as unexecutable edges, then propagate the new range for g_5..  This requires recalculating the existing range on entry to bb8. It properly picked up [0,0] from 4->8, but when the cache query checked the range from 7->8, it discovered that no value was yet set, so instead of looking it up, it fell back to the global range of [0,0][2,2].  If it properly calculates that edge instead, it comes up with UNDEFINED (as it is unexecutable) and results in [0,0]... and EVRP then also removes the block is c = 0 in. By picking up the global value, it still thought 2 was a possibility later on, and a single VRP pass couldn't eliminate the branch ultimately leading to the store... it required a second one with the adjusted CFG to catch it. This patch tells the cache to always do a read-only scan of the dominator tree to find the nearest actual value and use that instead.  This may solve other lingering weird propagation issues. I also ran a performance run on this change. It does slow VRP by down about 1%, but the overall change is nominal at around 0.05%. Bootstraps on x86_64-pc-linux-gnu with no regressions.  OK? Andrew From ddb9df254ed2bc5f11bdde213e1485d7971a25d9 Mon Sep 17 00:00:00 2001 From: Andrew MacLeod Date: Tue, 10 Jan 2023 13:40:56 -0500 Subject: [PATCH] Ranger cache - always use range_from_dom when updating. When updating an existing range, if we dont query the dom tree, we can get the global range instead of a proper range on some incoming edges which cause the range to not be refined properly. PR tree-optimization/108356 gcc/ * gimple-range-cache.cc (ranger_cache::range_on_edge): Always do a search of the DOM tree for a range. gcc/testsuite/ * gcc.dg/pr108356.c: New. --- gcc/gimple-range-cache.cc | 2 +- gcc/testsuite/gcc.dg/pr108356.c | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/gcc.dg/pr108356.c diff --git a/gcc/gimple-range-cache.cc b/gcc/gimple-range-cache.cc index a8202f0e895..20c444bc4f4 100644 --- a/gcc/gimple-range-cache.cc +++ b/gcc/gimple-range-cache.cc @@ -998,7 +998,7 @@ bool ranger_cache::range_on_edge (vrange &r, edge e, tree expr) { if (gimple_range_ssa_p (expr)) - return edge_range (r, e, expr, RFD_NONE); + return edge_range (r, e, expr, RFD_READ_ONLY); return get_tree_range (r, expr, NULL); } diff --git a/gcc/testsuite/gcc.dg/pr108356.c b/gcc/testsuite/gcc.dg/pr108356.c new file mode 100644 index 00000000000..1df6b278e45 --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr108356.c @@ -0,0 +1,23 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-optimized" } */ + +char a; +static char c = 3; +char d; +void foo(); +short(b)(short e, short f) { return e + f; } +int main() { + unsigned g = 0; + if (c) + ; + else + foo(); + for (; g < 2; g = b(g, 2)) { + d = g ? 0 : a; + if (g) + c = 0; + } +} + + +/* { dg-final { scan-tree-dump-not "foo" "optimized" } } */ -- 2.39.0