From patchwork Mon Oct 25 08:27:48 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Biener X-Patchwork-Id: 46596 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 E3EC53858400 for ; Mon, 25 Oct 2021 08:28:41 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E3EC53858400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1635150521; bh=Z9tecLqt7HjpOb+XU+3hD2dEaVxNcTu1f8trXx9S4M0=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=AomgJENCR4GXLZdT5AU3MIn3oDoRcdxS5rQbQjuMuUBqlwyf2moYjqQ0v/qpYJAWe bdmGmaOcHhIUXoLODFzwRIkO9ooTpG826fUFeFfGSUbpey5c2whd95HhOjfKUk25of ZKIDCDxOu+1s1UGemqeXpJ26NBseudyBuqGgtGdg= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.220.28]) by sourceware.org (Postfix) with ESMTPS id 57C4A3858439 for ; Mon, 25 Oct 2021 08:27:50 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 57C4A3858439 Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id 4A0B72191E for ; Mon, 25 Oct 2021 08:27:49 +0000 (UTC) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 386AD13216 for ; Mon, 25 Oct 2021 08:27:49 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id kUatDIVqdmFdKQAAMHmgww (envelope-from ) for ; Mon, 25 Oct 2021 08:27:49 +0000 Date: Mon, 25 Oct 2021 10:27:48 +0200 (CEST) To: gcc-patches@gcc.gnu.org Subject: [PATCH] tree-optimization/102920 - fix PHI VN with undefined args Message-ID: MIME-Version: 1.0 X-Spam-Status: No, score=-11.8 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) 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: Richard Biener via Gcc-patches From: Richard Biener Reply-To: Richard Biener Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" This fixes a latent issue exposed by now allowing VN_TOP in PHI arguments. We may only use optimistic equality when merging values on different edges, not when merging values on the same edge - in particular we may not choose the undef value on any edge when there's a not undef value as well. Bootstrapped and tested on x86_64-unknown-linux-gnu, pushed. 2021-10-25 Richard Biener PR tree-optimization/102920 * tree-ssa-sccvn.h (expressions_equal_p): Add argument controlling VN_TOP matching behavior. * tree-ssa-sccvn.c (expressions_equal_p): Likewise. (vn_phi_eq): Do not optimistically match VN_TOP. * gcc.dg/torture/pr102920.c: New testcase. --- gcc/testsuite/gcc.dg/torture/pr102920.c | 25 +++++++++++++++++++++++++ gcc/tree-ssa-sccvn.c | 21 ++++++++++++++------- gcc/tree-ssa-sccvn.h | 2 +- 3 files changed, 40 insertions(+), 8 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/torture/pr102920.c diff --git a/gcc/testsuite/gcc.dg/torture/pr102920.c b/gcc/testsuite/gcc.dg/torture/pr102920.c new file mode 100644 index 00000000000..aa27ac5f6ca --- /dev/null +++ b/gcc/testsuite/gcc.dg/torture/pr102920.c @@ -0,0 +1,25 @@ +/* { dg-do run } */ +/* { dg-additional-options "-funswitch-loops" } */ + +unsigned short a = 42; +unsigned short b = 1; +long int c = 1; +unsigned char var_120; +unsigned char var_123; + +void __attribute__((noipa)) test(unsigned short a, unsigned short b, long c) +{ + for (char i = 0; i < (char)c; i += 5) + if (!b) + var_120 = a; + else + var_123 = a; +} + +int main() +{ + test(a, b, c); + if (var_123 != 42) + __builtin_abort (); + return 0; +} diff --git a/gcc/tree-ssa-sccvn.c b/gcc/tree-ssa-sccvn.c index 893b1d0ddaa..d5242597684 100644 --- a/gcc/tree-ssa-sccvn.c +++ b/gcc/tree-ssa-sccvn.c @@ -4441,11 +4441,15 @@ vn_phi_eq (const_vn_phi_t const vp1, const_vn_phi_t const vp2) if (inverted_p) std::swap (te2, fe2); - /* ??? Handle VN_TOP specially. */ + /* Since we do not know which edge will be executed we have + to be careful when matching VN_TOP. Be conservative and + only match VN_TOP == VN_TOP for now, we could allow + VN_TOP on the not prevailing PHI though. See for example + PR102920. */ if (! expressions_equal_p (vp1->phiargs[te1->dest_idx], - vp2->phiargs[te2->dest_idx]) + vp2->phiargs[te2->dest_idx], false) || ! expressions_equal_p (vp1->phiargs[fe1->dest_idx], - vp2->phiargs[fe2->dest_idx])) + vp2->phiargs[fe2->dest_idx], false)) return false; return true; @@ -4470,7 +4474,7 @@ vn_phi_eq (const_vn_phi_t const vp1, const_vn_phi_t const vp2) tree phi2op = vp2->phiargs[i]; if (phi1op == phi2op) continue; - if (!expressions_equal_p (phi1op, phi2op)) + if (!expressions_equal_p (phi1op, phi2op, false)) return false; } @@ -5816,17 +5820,20 @@ get_next_constant_value_id (void) } -/* Compare two expressions E1 and E2 and return true if they are equal. */ +/* Compare two expressions E1 and E2 and return true if they are equal. + If match_vn_top_optimistically is true then VN_TOP is equal to anything, + otherwise VN_TOP only matches VN_TOP. */ bool -expressions_equal_p (tree e1, tree e2) +expressions_equal_p (tree e1, tree e2, bool match_vn_top_optimistically) { /* The obvious case. */ if (e1 == e2) return true; /* If either one is VN_TOP consider them equal. */ - if (e1 == VN_TOP || e2 == VN_TOP) + if (match_vn_top_optimistically + && (e1 == VN_TOP || e2 == VN_TOP)) return true; /* SSA_NAME compare pointer equal. */ diff --git a/gcc/tree-ssa-sccvn.h b/gcc/tree-ssa-sccvn.h index 8a1b649c726..7d53ab5e39f 100644 --- a/gcc/tree-ssa-sccvn.h +++ b/gcc/tree-ssa-sccvn.h @@ -22,7 +22,7 @@ #define TREE_SSA_SCCVN_H /* In tree-ssa-sccvn.c */ -bool expressions_equal_p (tree, tree); +bool expressions_equal_p (tree, tree, bool = true); /* TOP of the VN lattice. */