From patchwork Thu Feb 3 11:05:48 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Roger Sayle X-Patchwork-Id: 50706 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 3F0DB38515D2 for ; Thu, 3 Feb 2022 11:06:12 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from server.nextmovesoftware.com (server.nextmovesoftware.com [162.254.253.69]) by sourceware.org (Postfix) with ESMTPS id E0FA0385E037 for ; Thu, 3 Feb 2022 11:05:51 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org E0FA0385E037 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=nextmovesoftware.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=nextmovesoftware.com DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=nextmovesoftware.com; s=default; h=Content-Type:MIME-Version:Message-ID: Date:Subject:To:From:Sender:Reply-To:Cc:Content-Transfer-Encoding:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:In-Reply-To:References:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=jCBDUXvPCKchq/CcIju2Dm3i69xIYnLcIV4HqIHflSw=; b=MKjpfl1iNsFqJfKH0wIBtqI4HO OmCncpUVSb7GOACQPvME8WIFy6awWNair9uoWX27keds3zyFJbAW7NiOc22aXYLfEnhNSfff/hN2x BY+Z6iTcodXSVuOwsNkXgyiZ6JBdzXtoGCFiccE6F0Lg427svHnDjxGOaabHtoTRgAK+3D1Cq19GW U1vywJZcjbQhtpAGdiBuxufi5+QU2zWH+jxxG8pbBWX7ebKh54uN+I9QBN1Hnt6TY1WO8HoHNH4Yd 2kenvgodbZtVzm919OcxQ0OyBSQxi92UKqEWU+KFlEBgkOsvCv/igxaTP5hdJpWVR2jmGPkMF6sH7 hXOOFUKw==; Received: from host86-160-23-130.range86-160.btcentralplus.com ([86.160.23.130]:57457 helo=Dell) by server.nextmovesoftware.com with esmtpsa (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1nFZvz-00051U-7F for gcc-patches@gcc.gnu.org; Thu, 03 Feb 2022 06:05:51 -0500 From: "Roger Sayle" To: "'GCC Patches'" Subject: [PATCH] PR rtl-optimization/101885: Prevent combine from clobbering flags Date: Thu, 3 Feb 2022 11:05:48 -0000 Message-ID: <011001d818ee$01004e70$0300eb50$@nextmovesoftware.com> MIME-Version: 1.0 X-Mailer: Microsoft Outlook 16.0 Thread-Index: AdgY7UvIRcykIoT6RWWQV4DjdFzbLA== Content-Language: en-gb X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - server.nextmovesoftware.com X-AntiAbuse: Original Domain - gcc.gnu.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - nextmovesoftware.com X-Get-Message-Sender-Via: server.nextmovesoftware.com: authenticated_id: roger@nextmovesoftware.com X-Authenticated-Sender: server.nextmovesoftware.com: roger@nextmovesoftware.com X-Source: X-Source-Args: X-Source-Dir: X-Spam-Status: No, score=-10.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_BARRACUDACENTRAL, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE 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: , Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" This patch addresses PR rtl-optimization/101885 which is a P2 wrong code regression. In combine, if the resulting fused instruction is a parallel of two sets which fails to be recognized by the backend, combine tries to emit these as two sequential set instructions (known as split_i2i3). As each set is recognized the backend may add any necessary "clobbers". The code currently checks that any clobbers added to the first "set" don't interfere with the second set, but doesn't currently handle the case that clobbers added to the second set may interfere/kill the destination of the first set (which must be live at this point). The solution is to cut'n'paste the "clobber" logic from just a few lines earlier, suitably adjusted for the second instruction. One minor nit that may confuse a reviewer is that at this point in the code we've lost track of which set was first and which was second (combine chooses dynamically, and the recog processes that adds the clobbers may have obfuscated the original SET_DEST) so the actual test below is to confirm that any newly added clobbers (on the second set instruction) don't overlap either set0's or set1's destination. This patch has been tested on x86_64-pc-linux-gnu with make bootstrap and make -k check with no new failures. Ok for mainline? 2022-02-03 Roger Sayle gcc/ChangeLog PR rtl-optimization/101885 * combine.c (try_combine): When splitting a parallel into two sequential sets, check not only that the first doesn't clobber the second but also that the second doesn't clobber the first. gcc/testsuite/ChangeLog PR rtl-optimization/101885 * gcc.dg/pr101885.c: New test case. Thanks in advance, Roger diff --git a/gcc/combine.cc b/gcc/combine.cc index 2d40635..38e0369 100644 --- a/gcc/combine.cc +++ b/gcc/combine.cc @@ -4017,6 +4017,23 @@ try_combine (rtx_insn *i3, rtx_insn *i2, rtx_insn *i1, rtx_insn *i0, insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes); + /* Likewise, recog_for_combine might have added clobbers to NEWPAT. + Make sure NEWI2PAT2's original SET_DEST isn't clobbered. */ + if (insn_code_number >= 0 && GET_CODE (newpat) == PARALLEL) + { + for (i = XVECLEN (newpat, 0) - 1; i >= 0; i--) + if (GET_CODE (XVECEXP (newpat, 0, i)) == CLOBBER) + { + rtx reg = XEXP (XVECEXP (newpat, 0, i), 0); + if (reg_overlap_mentioned_p (reg, SET_DEST (set0)) + || reg_overlap_mentioned_p (reg, SET_DEST (set1))) + { + undo_all (); + return 0; + } + } + } + if (insn_code_number >= 0) split_i2i3 = 1; } diff --git a/gcc/testsuite/gcc.dg/pr101885.c b/gcc/testsuite/gcc.dg/pr101885.c new file mode 100644 index 0000000..05fd0ed --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr101885.c @@ -0,0 +1,31 @@ +/* { dg-do run } */ +/* { dg-options "-O3" } */ +int a = 3, c; +short b = 5, d, f; +volatile short e; + +__attribute__((noipa)) void +foo (void) +{ +} + +int +main () +{ + for (f = 0; f != 2; f++) + { + int g = a; + if (b) + if (a) + for (a = b = 0; b <= 3; b++) + ; + for (c = 0; c != 16; ++c) + e; + } + b = d; + foo (); + if (a != 0) + __builtin_abort (); + return 0; +} +