From patchwork Thu Oct 6 14:25:58 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kai Tietz X-Patchwork-Id: 118102 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 1DB18B70B7 for ; Fri, 7 Oct 2011 01:26:47 +1100 (EST) Received: (qmail 6597 invoked by alias); 6 Oct 2011 14:26:25 -0000 Received: (qmail 6524 invoked by uid 22791); 6 Oct 2011 14:26:21 -0000 X-SWARE-Spam-Status: No, hits=-7.2 required=5.0 tests=AWL, BAYES_00, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, SPF_HELO_PASS X-Spam-Check-By: sourceware.org Received: from mx4-phx2.redhat.com (HELO mx4-phx2.redhat.com) (209.132.183.25) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Thu, 06 Oct 2011 14:26:03 +0000 Received: from mail06.corp.redhat.com (zmail06.collab.prod.int.phx2.redhat.com [10.5.5.45]) by mx4-phx2.redhat.com (8.13.8/8.13.8) with ESMTP id p96EPwTB012827; Thu, 6 Oct 2011 10:25:58 -0400 Date: Thu, 06 Oct 2011 10:25:58 -0400 (EDT) From: Kai Tietz To: Richard Guenther Cc: gcc-patches@gcc.gnu.org, Richard Henderson Subject: Re: [patch tree-optimization]: Improve handling of conditional-branches on targets with high branch costs Message-ID: <14d7ea4a-5238-423e-8341-30e8a8db52dd@zmail06.collab.prod.int.phx2.redhat.com> In-Reply-To: MIME-Version: 1.0 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, I modified the patch so, that it always just converts two leafs of a TRUTH(AND|OR)IF chain into a TRUTH_(AND|OR) expression, if branch costs are high and leafs are simple without side-effects. Additionally I added some testcases for it. 2011-10-06 Kai Tietz * fold-const.c (fold_truth_andor): Convert TRUTH_(AND|OR)IF_EXPR to TRUTH_OR_EXPR, if suitable. 2011-10-06 Kai Tietz * gcc.dg/tree-ssa/ssa-ifbranch-1.c: New test. * gcc.dg/tree-ssa/ssa-ifbranch-2.c: New test. * gcc.dg/tree-ssa/ssa-ifbranch-3.c: New test. * gcc.dg/tree-ssa/ssa-ifbranch-4.c: New test. Bootstrapped and tested for all languages (including Ada and Obj-C++) on host x86_64-unknown-linux-gnu. Ok for apply? Regards, Kai Index: gcc-head/gcc/testsuite/gcc.dg/tree-ssa/ssa-ifbranch-1.c =================================================================== --- /dev/null +++ gcc-head/gcc/testsuite/gcc.dg/tree-ssa/ssa-ifbranch-1.c @@ -0,0 +1,18 @@ +/* Skip on MIPS, S/390, and AVR due LOGICAL_OP_NON_SHORT_CIRCUIT, and + lower values in BRANCH_COST. */ +/* { dg-do compile { target { ! "mips*-*-* s390*-*-* avr-*-* mn10300-*-*" } } } */ +/* { dg-options "-O2 -fdump-tree-gimple" } */ +/* { dg-options "-O2 -fdump-tree-gimple -march=i586" { target { i?86-*-* && ilp32 } } } */ + +extern int doo1 (void); +extern int doo2 (void); + +int bar (int a, int b, int c) +{ + if (a && b && c) + return doo1 (); + return doo2 (); +} + +/* { dg-final { scan-tree-dump-times "if " 2 "gimple" } } */ +/* { dg-final { cleanup-tree-dump "gimple" } } */ Index: gcc-head/gcc/fold-const.c =================================================================== --- gcc-head.orig/gcc/fold-const.c +++ gcc-head/gcc/fold-const.c @@ -8387,6 +8387,45 @@ fold_truth_andor (location_t loc, enum t if ((tem = fold_truthop (loc, code, type, arg0, arg1)) != 0) return tem; + if ((code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR) + && !TREE_SIDE_EFFECTS (arg1) + && LOGICAL_OP_NON_SHORT_CIRCUIT + /* floats might trap. */ + && !FLOAT_TYPE_P (TREE_TYPE (arg1)) + && ((TREE_CODE_CLASS (TREE_CODE (arg1)) != tcc_comparison + && TREE_CODE (arg1) != TRUTH_NOT_EXPR + && simple_operand_p (arg1)) + || ((TREE_CODE_CLASS (TREE_CODE (arg1)) == tcc_comparison + || TREE_CODE (arg1) == TRUTH_NOT_EXPR) + /* Float comparison might trap. */ + && !FLOAT_TYPE_P (TREE_TYPE (TREE_OPERAND (arg1, 0))) + && simple_operand_p (TREE_OPERAND (arg1, 0))))) + { + /* We want to combine truth-comparison for + ((W TRUTH-ANDOR X) TRUTH-ANDORIF Y) TRUTH-ANDORIF Z, + if Y and Z are simple operands and have no side-effect to + ((W TRUTH-ANDOR X) TRUTH-IF (Y TRUTH-ANDOR Z). */ + if (TREE_CODE (arg0) == code + && !TREE_SIDE_EFFECTS (TREE_OPERAND (arg0, 1)) + && simple_operand_p (TREE_OPERAND (arg0, 1))) + { + tem = build2_loc (loc, + (code == TRUTH_ANDIF_EXPR ? TRUTH_AND_EXPR + : TRUTH_OR_EXPR), + type, TREE_OPERAND (arg0, 1), arg1); + return build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), + tem); + } + /* Convert X TRUTH-ANDORIF Y to X TRUTH-ANDOR Y, if X and Y + are simple operands and have no side-effects. */ + if (simple_operand_p (arg0) + && !TREE_SIDE_EFFECTS (arg0)) + return build2_loc (loc, + (code == TRUTH_ANDIF_EXPR ? TRUTH_AND_EXPR + : TRUTH_OR_EXPR), + type, arg0, arg1); + } + return NULL_TREE; } Index: gcc-head/gcc/testsuite/gcc.dg/tree-ssa/ssa-ifbranch-2.c =================================================================== --- /dev/null +++ gcc-head/gcc/testsuite/gcc.dg/tree-ssa/ssa-ifbranch-2.c @@ -0,0 +1,18 @@ +/* Skip on MIPS, S/390, and AVR due LOGICAL_OP_NON_SHORT_CIRCUIT, and + lower values in BRANCH_COST. */ +/* { dg-do compile { target { ! "mips*-*-* s390*-*-* avr-*-* mn10300-*-*" } } } */ +/* { dg-options "-O2 -fdump-tree-gimple" } */ +/* { dg-options "-O2 -fdump-tree-gimple -march=i586" { target { i?86-*-* && ilp32 } } } */ + +extern int doo1 (void); +extern int doo2 (void); + +int bar (int a, int b, int c, int d) +{ + if (a && b && c && d) + return doo1 (); + return doo2 (); +} + +/* { dg-final { scan-tree-dump-times "if " 2 "gimple" } } */ +/* { dg-final { cleanup-tree-dump "gimple" } } */ Index: gcc-head/gcc/testsuite/gcc.dg/tree-ssa/ssa-ifbranch-3.c =================================================================== --- /dev/null +++ gcc-head/gcc/testsuite/gcc.dg/tree-ssa/ssa-ifbranch-3.c @@ -0,0 +1,18 @@ +/* Skip on MIPS, S/390, and AVR due LOGICAL_OP_NON_SHORT_CIRCUIT, and + lower values in BRANCH_COST. */ +/* { dg-do compile { target { ! "mips*-*-* s390*-*-* avr-*-* mn10300-*-*" } } } */ +/* { dg-options "-O2 -fdump-tree-gimple" } */ +/* { dg-options "-O2 -fdump-tree-gimple -march=i586" { target { i?86-*-* && ilp32 } } } */ + +extern int doo1 (void); +extern int doo2 (void); + +int bar (int a, int c) +{ + if (a && c) + return doo1 (); + return doo2 (); +} + +/* { dg-final { scan-tree-dump-times "if " 1 "gimple" } } */ +/* { dg-final { cleanup-tree-dump "gimple" } } */ Index: gcc-head/gcc/testsuite/gcc.dg/tree-ssa/ssa-ifbranch-4.c =================================================================== --- /dev/null +++ gcc-head/gcc/testsuite/gcc.dg/tree-ssa/ssa-ifbranch-4.c @@ -0,0 +1,18 @@ +/* Skip on MIPS, S/390, and AVR due LOGICAL_OP_NON_SHORT_CIRCUIT, and + lower values in BRANCH_COST. */ +/* { dg-do compile { target { ! "mips*-*-* s390*-*-* avr-*-* mn10300-*-*" } } } */ +/* { dg-options "-O2 -fdump-tree-gimple" } */ +/* { dg-options "-O2 -fdump-tree-gimple -march=i586" { target { i?86-*-* && ilp32 } } } */ + +extern int doo1 (void); +extern int doo2 (void); + +int bar (int a, int b, int c, int d) +{ + if ((a && b) && (c && d)) + return doo1 (); + return doo2 (); +} + +/* { dg-final { scan-tree-dump-times "if " 2 "gimple" } } */ +/* { dg-final { cleanup-tree-dump "gimple" } } */