From patchwork Fri May 20 13:12:42 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 96600 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 4F36AB71A1 for ; Fri, 20 May 2011 23:13:05 +1000 (EST) Received: (qmail 14973 invoked by alias); 20 May 2011 13:13:02 -0000 Received: (qmail 14932 invoked by uid 22791); 20 May 2011 13:13:01 -0000 X-SWARE-Spam-Status: No, hits=-6.4 required=5.0 tests=AWL, BAYES_00, RCVD_IN_DNSWL_HI, SPF_HELO_PASS, TW_TM, T_RP_MATCHES_RCVD 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; Fri, 20 May 2011 13:12:44 +0000 Received: from int-mx02.intmail.prod.int.phx2.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id p4KDChB9020723 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Fri, 20 May 2011 09:12:44 -0400 Received: from tyan-ft48-01.lab.bos.redhat.com (tyan-ft48-01.lab.bos.redhat.com [10.16.42.4]) by int-mx02.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id p4KDCh10007427 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO) for ; Fri, 20 May 2011 09:12:43 -0400 Received: from tyan-ft48-01.lab.bos.redhat.com (localhost.localdomain [127.0.0.1]) by tyan-ft48-01.lab.bos.redhat.com (8.14.4/8.14.4) with ESMTP id p4KDCg9r004133 for ; Fri, 20 May 2011 15:12:42 +0200 Received: (from jakub@localhost) by tyan-ft48-01.lab.bos.redhat.com (8.14.4/8.14.4/Submit) id p4KDCgeK004132 for gcc-patches@gcc.gnu.org; Fri, 20 May 2011 15:12:42 +0200 Date: Fri, 20 May 2011 15:12:42 +0200 From: Jakub Jelinek To: gcc-patches@gcc.gnu.org Subject: [PATCH] Fix {and,or}_comparisons_1 (PR tree-optimization/49073) Message-ID: <20110520131242.GP17079@tyan-ft48-01.lab.bos.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! As discussed in the PR, these routines happily look through PHI nodes into previous loop iteration. f is initialized in the previous loop iteration with f_10 = d_6 == 3; and in the current iteration there is f_2 != 0 && d_6 == 4 test. As we look through # f_2 = PHI <0(2), f_10(6)> PHI, this is evaluated as 0 != 0 && d_6 == 4 (which is correctly false) and (d_6 == 3) != 0 && d_6 == 4 (this one is false due to d_6 being from one iteration in one case and next one in another case). The following patch fixes it, bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk/4.6? 2011-05-20 Jakub Jelinek PR tree-optimization/49073 * gimple-fold.c (and_comparisons_1, or_comparisons_1): Return NULL if PHI argument is SSA_NAME, whose def_stmt is dominated by the PHI. * tree-ssa-ifcombine.c (tree_ssa_ifcombine): Calculate dominators. * gcc.c-torture/execute/pr49073.c: New test. Jakub --- gcc/gimple-fold.c.jj 2011-05-11 19:39:04.000000000 +0200 +++ gcc/gimple-fold.c 2011-05-20 11:05:11.000000000 +0200 @@ -1,5 +1,5 @@ /* Statement simplification on GIMPLE. - Copyright (C) 2010 Free Software Foundation, Inc. + Copyright (C) 2010, 2011 Free Software Foundation, Inc. Split out from tree-ssa-ccp.c. This file is part of GCC. @@ -2278,8 +2278,19 @@ and_comparisons_1 (enum tree_code code1, } else if (TREE_CODE (arg) == SSA_NAME) { - tree temp = and_var_with_comparison (arg, invert, - code2, op2a, op2b); + tree temp; + gimple def_stmt = SSA_NAME_DEF_STMT (arg); + /* In simple cases we can look through PHI nodes, + but we have to be careful with loops. + See PR49073. */ + if (! dom_info_available_p (CDI_DOMINATORS) + || gimple_bb (def_stmt) == gimple_bb (stmt) + || dominated_by_p (CDI_DOMINATORS, + gimple_bb (def_stmt), + gimple_bb (stmt))) + return NULL_TREE; + temp = and_var_with_comparison (arg, invert, code2, + op2a, op2b); if (!temp) return NULL_TREE; else if (!result) @@ -2728,8 +2739,19 @@ or_comparisons_1 (enum tree_code code1, } else if (TREE_CODE (arg) == SSA_NAME) { - tree temp = or_var_with_comparison (arg, invert, - code2, op2a, op2b); + tree temp; + gimple def_stmt = SSA_NAME_DEF_STMT (arg); + /* In simple cases we can look through PHI nodes, + but we have to be careful with loops. + See PR49073. */ + if (! dom_info_available_p (CDI_DOMINATORS) + || gimple_bb (def_stmt) == gimple_bb (stmt) + || dominated_by_p (CDI_DOMINATORS, + gimple_bb (def_stmt), + gimple_bb (stmt))) + return NULL_TREE; + temp = or_var_with_comparison (arg, invert, code2, + op2a, op2b); if (!temp) return NULL_TREE; else if (!result) --- gcc/tree-ssa-ifcombine.c.jj 2011-05-20 08:14:08.000000000 +0200 +++ gcc/tree-ssa-ifcombine.c 2011-05-20 11:37:24.000000000 +0200 @@ -1,5 +1,5 @@ /* Combining of if-expressions on trees. - Copyright (C) 2007, 2008, 2009, 2010 Free Software Foundation, Inc. + Copyright (C) 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc. Contributed by Richard Guenther This file is part of GCC. @@ -625,6 +625,7 @@ tree_ssa_ifcombine (void) int i; bbs = blocks_in_phiopt_order (); + calculate_dominance_info (CDI_DOMINATORS); for (i = 0; i < n_basic_blocks - NUM_FIXED_BLOCKS; ++i) { --- gcc/testsuite/gcc.c-torture/execute/pr49073.c.jj 2011-05-20 11:38:58.000000000 +0200 +++ gcc/testsuite/gcc.c-torture/execute/pr49073.c 2011-05-20 11:37:40.000000000 +0200 @@ -0,0 +1,26 @@ +/* PR tree-optimization/49073 */ + +extern void abort (void); +int a[] = { 1, 2, 3, 4, 5, 6, 7 }, c; + +int +main () +{ + int d = 1, i = 1; + _Bool f = 0; + do + { + d = a[i]; + if (f && d == 4) + { + ++c; + break; + } + i++; + f = (d == 3); + } + while (d < 7); + if (c != 1) + abort (); + return 0; +}