From patchwork Thu Oct 16 09:09:08 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Biener X-Patchwork-Id: 400237 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]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id BDEB81400AF for ; Thu, 16 Oct 2014 20:13:23 +1100 (AEDT) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:subject:message-id:mime-version:content-type; q=dns; s= default; b=uE2ONBGuy69rN4/wcusxVc+7IYujFkN9FR/F52/xuqBt0UGKoKjwH qBY2SYZmHW4kEWAqg309azMrlX+gI7BpYCkR9h5OsWim4rdCxMyTCws/3ghq8cNp e42gAlquyc0UZyTVMhdnxduu/qNtW/2jZP4BNrNFGNCepX8U4FyFSU= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:subject:message-id:mime-version:content-type; s= default; bh=9wOpO85Jaj+IQKnADjByzBAIXUM=; b=l/iS8luU60GrnqOOM3C+ uNWvEd3+OJL+mtOCSF6UWd6jzwl7wKKBJb6e8dQ2EiH2zevjsewx4caf/YT7aGzk S9RA/gVPT/ABU7aObC6ECGrneGJrv2qzC+7S1QZBm/JvBUNFqWu7WHgelJUEXRpX 8HhSG/8sRtfg5fV6NGnV/94= Received: (qmail 2228 invoked by alias); 16 Oct 2014 09:13:17 -0000 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 Received: (qmail 2217 invoked by uid 89); 16 Oct 2014 09:13:16 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-3.2 required=5.0 tests=AWL, BAYES_00, T_RP_MATCHES_RCVD autolearn=ham version=3.3.2 X-HELO: mx2.suse.de Received: from cantor2.suse.de (HELO mx2.suse.de) (195.135.220.15) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (CAMELLIA256-SHA encrypted) ESMTPS; Thu, 16 Oct 2014 09:13:09 +0000 Received: from relay1.suse.de (charybdis-ext.suse.de [195.135.220.254]) by mx2.suse.de (Postfix) with ESMTP id 11AA3ACED for ; Thu, 16 Oct 2014 09:13:07 +0000 (UTC) Date: Thu, 16 Oct 2014 11:09:08 +0200 (CEST) From: Richard Biener To: gcc-patches@gcc.gnu.org Subject: [PATCH] Fix PR63168 Message-ID: User-Agent: Alpine 2.11 (LSU 23 2013-08-11) MIME-Version: 1.0 We can fail to copy loop headers if the loop looks proper to us (empty latch) even when the latch really isn't empty and just contains a PHI node. In this case cfgcleanup should have merged it with the previous block but it fails to do that because of an overly strict check. Fixed by only "preserving" loop latches when required and the result wouldn't be a simple latch. Bootstrapped on x86_64-unknown-linux-gnu, testing in progress. Richard. 2014-10-16 Richard Biener PR tree-optimization/63168 * tree-cfg.c (gimple_can_merge_blocks_p): Only protect latches if after merging they are no longer simple. * cfghooks.c (merge_blocks): Handle merging a latch block into another block. * gcc.dg/tree-ssa/loop-40.c: New testcase. Index: gcc/tree-cfg.c =================================================================== --- gcc/tree-cfg.c (revision 216258) +++ gcc/tree-cfg.c (working copy) @@ -1674,8 +1678,12 @@ gimple_can_merge_blocks_p (basic_block a return false; } - /* Protect the loop latches. */ - if (current_loops && b->loop_father->latch == b) + /* Protect simple loop latches. We only want to avoid merging + the latch with the loop header in this case. */ + if (current_loops + && b->loop_father->latch == b + && loops_state_satisfies_p (LOOPS_HAVE_SIMPLE_LATCHES) + && b->loop_father->header == a) return false; /* It must be possible to eliminate all phi nodes in B. If ssa form Index: gcc/cfghooks.c =================================================================== --- gcc/cfghooks.c (revision 216258) +++ gcc/cfghooks.c (working copy) @@ -766,6 +766,11 @@ merge_blocks (basic_block a, basic_block add_bb_to_loop (a, b->loop_father); a->loop_father->header = a; } + /* If we merge a loop latch into its predecessor, update the loop + structure. */ + if (b->loop_father->latch + && b->loop_father->latch == b) + b->loop_father->latch = a; remove_bb_from_loops (b); } Index: gcc/testsuite/gcc.dg/tree-ssa/loop-40.c =================================================================== --- gcc/testsuite/gcc.dg/tree-ssa/loop-40.c (revision 0) +++ gcc/testsuite/gcc.dg/tree-ssa/loop-40.c (working copy) @@ -0,0 +1,14 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-ch-details" } */ + +int mymax2(int *it, int *end) +{ + int max = *it; + while (++it != end) + if (*it > max) + max = *it; + return max; +} + +/* { dg-final { scan-tree-dump "Duplicating header" "ch" } } */ +/* { dg-final { cleanup-tree-dump "ch" } } */