From patchwork Mon Oct 22 19:35:16 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 193266 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) by ozlabs.org (Postfix) with SMTP id 005142C0089 for ; Tue, 23 Oct 2012 06:35:44 +1100 (EST) Comment: DKIM? See http://www.dkim.org DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d=gcc.gnu.org; s=default; x=1351539345; h=Comment: DomainKey-Signature:Received:Received:Received:Received:Received: Received:Received:Date:From:To:Subject:Message-ID:Reply-To: MIME-Version:Content-Type:Content-Disposition:User-Agent: Mailing-List:Precedence:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:Sender:Delivered-To; bh=InT1+OihwjbMNYWeDUA/ V8e73is=; b=FkZfv8sJ56mdupL1rocU8byWE7s1xsEyvR5KHde81TuvmpGTEa7J L8rkejQmxifJAV5dn8Rx65O6HWeM9x4uLzpO7ZbVODO7S75DJy51kiuGaE1FjRqm QjWn4V3YQ16HoiBYxzlYGF33Z5Aer0k/YOJH9BYUwqr2tE3/BtPVA0U= Comment: DomainKeys? See http://antispam.yahoo.com/domainkeys DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=default; d=gcc.gnu.org; h=Received:Received:X-SWARE-Spam-Status:X-Spam-Check-By:Received:Received:Received:Received:Received:Date:From:To:Subject:Message-ID:Reply-To:MIME-Version:Content-Type:Content-Disposition:User-Agent:X-IsSubscribed:Mailing-List:Precedence:List-Id:List-Unsubscribe:List-Archive:List-Post:List-Help:Sender:Delivered-To; b=XC8l7igoY32eK21bRuj1ir1mbNowpcREOhNl80vw1rOmxHoUprXNqHnqOwUkzN +GutgSqNeib5Fp+DsJg0zOwReEVPVoUIkoTc29okVnqKXQT1XED5n90doSLJevXd s61mOQP4K35bt3XZspEXvrEF4v0mB+jVNN34Pnmep4R24=; Received: (qmail 1502 invoked by alias); 22 Oct 2012 19:35:33 -0000 Received: (qmail 1434 invoked by uid 22791); 22 Oct 2012 19:35:31 -0000 X-SWARE-Spam-Status: No, hits=-6.7 required=5.0 tests=AWL, BAYES_00, KHOP_RCVD_UNTRUST, RCVD_IN_DNSWL_HI, RCVD_IN_HOSTKARMA_W, RP_MATCHES_RCVD, SPF_HELO_PASS, TW_TM X-Spam-Check-By: sourceware.org Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Mon, 22 Oct 2012 19:35:19 +0000 Received: from int-mx11.intmail.prod.int.phx2.redhat.com (int-mx11.intmail.prod.int.phx2.redhat.com [10.5.11.24]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id q9MJZJX5016661 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Mon, 22 Oct 2012 15:35:19 -0400 Received: from zalov.redhat.com (vpn1-6-20.ams2.redhat.com [10.36.6.20]) by int-mx11.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id q9MJZHp7010066 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO) for ; Mon, 22 Oct 2012 15:35:18 -0400 Received: from zalov.cz (localhost [127.0.0.1]) by zalov.redhat.com (8.14.5/8.14.5) with ESMTP id q9MJZGRa029148 for ; Mon, 22 Oct 2012 21:35:16 +0200 Received: (from jakub@localhost) by zalov.cz (8.14.5/8.14.5/Submit) id q9MJZGsx029147 for gcc-patches@gcc.gnu.org; Mon, 22 Oct 2012 21:35:16 +0200 Date: Mon, 22 Oct 2012 21:35:16 +0200 From: Jakub Jelinek To: gcc-patches@gcc.gnu.org Subject: [PATCH] Fix CDDCE miscompilation (PR tree-optimization/55018) Message-ID: <20121022193516.GG1752@tucnak.redhat.com> Reply-To: Jakub Jelinek MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.5.21 (2010-09-15) X-IsSubscribed: yes Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Hi! On the following testcase we have two endless loops before cddce2: Sender_signal (int Connect) { int State; unsigned int occurrence; : if (Connect_6(D) != 0) goto ; else goto ; : # occurrence_8 = PHI <0(7), occurrence_12(4)> occurrence_12 = occurrence_8 + 1; __builtin_printf ("Sender_Signal occurrence %u\n", occurrence_12); : goto ; : : goto ; : goto ; : goto ; } The problem are the two empty bbs on the path from the conditional at the end of bb2 and the endless loops (i.e. bb7 and bb8). In presence of infinite loops dominance.c adds fake edges to exit pretty arbitrarily (it uses FOR_EACH_BB_REVERSE and for unconnected bbs computes post-dominance and adds fake edges to exit), so with the above testcases both bb7 and bb8 have exit block as immediate post-dominator, so find_control_dependence stops at those bb's when starting from the 2->7 resp. 2->8 edges. bb7/bb8 don't have a control stmt at the end, so mark_last_stmt_necessary doesn't mark any stmt as necessary in them and thus if (Connect_6(D) != 0) GIMPLE_COND is never marked as necessary and the whole endless loop with printfs in it is removed. The following patch fixes it by detecting such problematic blocks and recursing on them in mark_control_dependence_edges_necessary. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2012-10-22 Jakub Jelinek PR tree-optimization/55018 * tree-ssa-dce.c (mark_last_stmt_necessary): Return bool whether mark_stmt_necessary was called. (mark_control_dependence_edges_necessary): Recurse on cd_bb if mark_last_stmt_necessary hasn't marked a control stmt, cd_bb has exit block as immediate dominator and a single succ edge. * gcc.dg/torture/pr55018.c: New test. Jakub --- gcc/tree-ssa-dce.c.jj 2012-08-15 10:55:33.000000000 +0200 +++ gcc/tree-ssa-dce.c 2012-10-22 16:50:03.011497546 +0200 @@ -381,7 +381,7 @@ mark_stmt_if_obviously_necessary (gimple /* Mark the last statement of BB as necessary. */ -static void +static bool mark_last_stmt_necessary (basic_block bb) { gimple stmt = last_stmt (bb); @@ -391,7 +391,11 @@ mark_last_stmt_necessary (basic_block bb /* We actually mark the statement only if it is a control statement. */ if (stmt && is_ctrl_stmt (stmt)) - mark_stmt_necessary (stmt, true); + { + mark_stmt_necessary (stmt, true); + return true; + } + return false; } @@ -423,8 +427,18 @@ mark_control_dependent_edges_necessary ( continue; } - if (!TEST_BIT (last_stmt_necessary, cd_bb->index)) - mark_last_stmt_necessary (cd_bb); + if (!TEST_BIT (last_stmt_necessary, cd_bb->index) + && !mark_last_stmt_necessary (cd_bb)) + { + /* In presence of infinite loops, some bbs on a path + to an infinite loop might not end with a control stmt, + but due to a fake edge to exit stop find_control_dependence. + Recurse for those. */ + if (get_immediate_dominator (CDI_POST_DOMINATORS, cd_bb) + == EXIT_BLOCK_PTR + && single_succ_p (cd_bb)) + mark_control_dependent_edges_necessary (cd_bb, el, false); + } } if (!skipped) --- gcc/testsuite/gcc.dg/torture/pr55018.c.jj 2012-10-22 16:53:56.623083723 +0200 +++ gcc/testsuite/gcc.dg/torture/pr55018.c 2012-10-22 16:54:21.278934668 +0200 @@ -0,0 +1,22 @@ +/* PR tree-optimization/55018 */ +/* { dg-do compile } */ +/* { dg-options "-fdump-tree-optimized" } */ + +void +foo (int x) +{ + unsigned int a = 0; + int b = 3; + if (x) + b = 0; +lab: + if (x) + goto lab; + a++; + if (b != 2) + __builtin_printf ("%u", a); + goto lab; +} + +/* { dg-final { scan-tree-dump "printf" "optimized" } } */ +/* { dg-final { cleanup-tree-dump "optimized" } } */