From patchwork Fri Oct 7 21:36:59 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Kai Tietz X-Patchwork-Id: 118374 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 EE0BFB70ED for ; Sat, 8 Oct 2011 08:37:19 +1100 (EST) Received: (qmail 18514 invoked by alias); 7 Oct 2011 21:37:17 -0000 Received: (qmail 18502 invoked by uid 22791); 7 Oct 2011 21:37:15 -0000 X-SWARE-Spam-Status: No, hits=-1.8 required=5.0 tests=AWL, BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FROM, RCVD_IN_DNSWL_LOW X-Spam-Check-By: sourceware.org Received: from mail-vw0-f47.google.com (HELO mail-vw0-f47.google.com) (209.85.212.47) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Fri, 07 Oct 2011 21:37:01 +0000 Received: by vwe42 with SMTP id 42so4034333vwe.20 for ; Fri, 07 Oct 2011 14:37:00 -0700 (PDT) MIME-Version: 1.0 Received: by 10.52.91.11 with SMTP id ca11mr4526067vdb.8.1318023419916; Fri, 07 Oct 2011 14:36:59 -0700 (PDT) Received: by 10.220.180.5 with HTTP; Fri, 7 Oct 2011 14:36:59 -0700 (PDT) In-Reply-To: References: <14d7ea4a-5238-423e-8341-30e8a8db52dd@zmail06.collab.prod.int.phx2.redhat.com> Date: Fri, 7 Oct 2011 23:36:59 +0200 Message-ID: Subject: Re: [patch tree-optimization]: Improve handling of conditional-branches on targets with high branch costs From: Kai Tietz To: Richard Guenther Cc: Kai Tietz , gcc-patches@gcc.gnu.org, Richard Henderson 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 Hello, this is the updated version with the suggestion 2011/10/7 Richard Guenther : > On Thu, Oct 6, 2011 at 4:25 PM, Kai Tietz wrote: >> +      && ((TREE_CODE_CLASS (TREE_CODE (arg1)) != tcc_comparison >> +           && TREE_CODE (arg1) != TRUTH_NOT_EXPR >> +           && simple_operand_p (arg1)) > > As I said previously simple_operand_p already rejects covers > comparisons and TRUTH_NOT_EXPR.  Also arg1 had better > TREE_SIDE_EFFECTS set if the comparison might trap, as > it might just be hidden in something more complicated - so > the simple check isn't enough anyway (and if simple_operand_p > would cover it, the check would be better placed there). I reworked simple_operand_p so that it does this special-casing and additionally also checks for trapping. >> +      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); > > All trees should be folded, don't use plain build without a good reason. Ok, done >> +       } >> +      /* 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)) > > Again, the checks you do for arg0 do not match those for arg1.  OTOH > it doesn't matter whether arg0 is simple or not or has side-effects or > not for this transformation, so why check it at all? It is required. For left-hand operand, if it isn't a logical and/or/xor, we need to check for side-effects (and for trapping). I see that calling of simple_operand_p is wrong here, as it rejects too much. Nevertheless the check for side-effects is necessary for having valid sequence-points. Without that checking a simple test int getter (void); int foo (void) { int c, r = 0; while ((c = getter ()) != '"' && c >= 0) r +=c; return r; } would give a warning about sequence-points. As left-hand operand has side-effects, but right-hand not. If we would combine it as AND, the operands are exchange-able. So right-hand operand needs to be another ANDIF expression instead. Same apply on trapping. > In fold_truthop we still have the same (albeit more restricted transform), > but guarded with > >  if (BRANCH_COST (optimize_function_for_speed_p (cfun), >                   false) >= 2 > > too.  Why not here?  Please delete redundant code in fold_truthop. Well, in general this is the default definition of LOGICAL_OP_NON_SHORT_CIRCUIT, so I missed that. As for some targets the macro LOGICAL_OP_NON_SHORT_CIRCUIT might be defined differently, it might make sense to check for BRANCH_COST again. > It's also odd that this is only called from fold_truth_andor but has > a more generic name, so maybe rename it to fold_truth_andor_1 on the way. I renamed it. > Richard. ChangeLog 2011-10-07 Kai Tietz * fold-const.c (simple_operand_p): Make argument non-const and add floating-point trapping check, and special cases for comparisons, and logical-not's. (fold_truthop): Rename to (fold_truth_andor_1): function name. Additionally remove here TRUTH-AND|OR_EXPR generation. (fold_truth_andor): Handle branching at one place. Bootstrapped and regression-tested for all languages plus Ada and Obj-C++ on x86_64-pc-linux-gnu. Ok for apply? Regards, Kai static tree extract_muldiv (tree, tree, enum tree_code, tree, bool *); @@ -3500,7 +3499,7 @@ optimize_bit_field_compare (location_t l return lhs; } -/* Subroutine for fold_truthop: decode a field reference. +/* Subroutine for fold_truth_andor_1: decode a field reference. If EXP is a comparison reference, we return the innermost reference. @@ -3668,17 +3667,43 @@ sign_bit_p (tree exp, const_tree val) return NULL_TREE; } -/* Subroutine for fold_truthop: determine if an operand is simple enough +/* Subroutine for fold_truth_andor_1: determine if an operand is simple enough to be evaluated unconditionally. */ static int -simple_operand_p (const_tree exp) +simple_operand_p (tree exp) { + enum tree_code code; /* Strip any conversions that don't change the machine mode. */ STRIP_NOPS (exp); + code = TREE_CODE (exp); + + /* Handle some trivials $$$$ */ + if (TREE_CODE_CLASS (code) == tcc_comparison) + return (tree_could_trap_p (exp) + && simple_operand_p (TREE_OPERAND (exp, 0)) + && simple_operand_p (TREE_OPERAND (exp, 1))); + + if (FLOAT_TYPE_P (TREE_TYPE (exp)) + && tree_could_trap_p (exp)) + return false; + + switch (code) + { + case SSA_NAME: + return true; + case TRUTH_NOT_EXPR: + return simple_operand_p (TREE_OPERAND (exp, 0)); + case BIT_NOT_EXPR: + if (TREE_CODE (TREE_TYPE (exp)) != BOOLEAN_TYPE) + return false; + return simple_operand_p (TREE_OPERAND (exp, 0)); + default: + break; + } + return (CONSTANT_CLASS_P (exp) - || TREE_CODE (exp) == SSA_NAME || (DECL_P (exp) && ! TREE_ADDRESSABLE (exp) && ! TREE_THIS_VOLATILE (exp) @@ -4888,7 +4913,7 @@ fold_range_test (location_t loc, enum tr return 0; } -/* Subroutine for fold_truthop: C is an INTEGER_CST interpreted as a P +/* Subroutine for fold_truth_andor_1: C is an INTEGER_CST interpreted as a P bit value. Arrange things so the extra bits will be set to zero if and only if C is signed-extended to its full width. If MASK is nonzero, it is an INTEGER_CST that should be AND'ed with the extra bits. */ @@ -5025,8 +5050,8 @@ merge_truthop_with_opposite_arm (locatio We return the simplified tree or 0 if no optimization is possible. */ static tree -fold_truthop (location_t loc, enum tree_code code, tree truth_type, - tree lhs, tree rhs) +fold_truth_andor_1 (location_t loc, enum tree_code code, tree truth_type, + tree lhs, tree rhs) { /* If this is the "or" of two comparisons, we can do something if the comparisons are NE_EXPR. If this is the "and", we can do something @@ -5054,8 +5079,6 @@ fold_truthop (location_t loc, enum tree_ tree lntype, rntype, result; HOST_WIDE_INT first_bit, end_bit; int volatilep; - tree orig_lhs = lhs, orig_rhs = rhs; - enum tree_code orig_code = code; /* Start by getting the comparison codes. Fail if anything is volatile. If one operand is a BIT_AND_EXPR with the constant one, treat it as if @@ -5119,8 +5142,7 @@ fold_truthop (location_t loc, enum tree_ /* If the RHS can be evaluated unconditionally and its operands are simple, it wins to evaluate the RHS unconditionally on machines with expensive branches. In this case, this isn't a comparison - that can be merged. Avoid doing this if the RHS is a floating-point - comparison since those can trap. */ + that can be merged. */ if (BRANCH_COST (optimize_function_for_speed_p (cfun), false) >= 2 @@ -5149,13 +5171,6 @@ fold_truthop (location_t loc, enum tree_ build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg), ll_arg, rl_arg), build_int_cst (TREE_TYPE (ll_arg), 0)); - - if (LOGICAL_OP_NON_SHORT_CIRCUIT) - { - if (code != orig_code || lhs != orig_lhs || rhs != orig_rhs) - return build2_loc (loc, code, truth_type, lhs, rhs); - return NULL_TREE; - } } /* See if the comparisons can be merged. Then get all the parameters for @@ -8380,13 +8395,52 @@ fold_truth_andor (location_t loc, enum t lhs is another similar operation, try to merge its rhs with our rhs. Then try to merge our lhs and rhs. */ if (TREE_CODE (arg0) == code - && 0 != (tem = fold_truthop (loc, code, type, - TREE_OPERAND (arg0, 1), arg1))) + && 0 != (tem = fold_truth_andor_1 (loc, code, type, + TREE_OPERAND (arg0, 1), arg1))) return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem); - if ((tem = fold_truthop (loc, code, type, arg0, arg1)) != 0) + if ((tem = fold_truth_andor_1 (loc, code, type, arg0, arg1)) != 0) return tem; + if ((code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR) + && (BRANCH_COST (optimize_function_for_speed_p (cfun), + false) >= 2) + && !TREE_SIDE_EFFECTS (arg1) + && LOGICAL_OP_NON_SHORT_CIRCUIT + && simple_operand_p (arg1)) + { + enum tree_code ncode = (code == TRUTH_ANDIF_EXPR ? TRUTH_AND_EXPR + : TRUTH_OR_EXPR); + + /* We don't want to pack more then two non-IF branches + together. Therefore we need to check, if rhs isn't + already an TRUTH_(XOR|OR|AND)[IF]_EXPR. */ + if (TREE_CODE (arg0) == code + && TREE_CODE (TREE_OPERAND (arg0, 1)) != TRUTH_AND_EXPR + && TREE_CODE (TREE_OPERAND (arg0, 1)) != TRUTH_OR_EXPR + && TREE_CODE (TREE_OPERAND (arg0, 1)) != TRUTH_XOR_EXPR + && TREE_CODE (TREE_OPERAND (arg0, 1)) != TRUTH_ANDIF_EXPR + && TREE_CODE (TREE_OPERAND (arg0, 1)) != TRUTH_ORIF_EXPR + /* Needed for sequence points and trappings, or side-effects. */ + && !TREE_SIDE_EFFECTS (TREE_OPERAND (arg0, 1)) + && !tree_could_trap_p (TREE_OPERAND (arg0, 1))) + { + tem = fold_build2_loc (loc, ncode, type, TREE_OPERAND (arg0, 1), + arg1); + return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), + tem); + } + else if (TREE_CODE (arg0) != TRUTH_AND_EXPR + && TREE_CODE (arg0) != TRUTH_OR_EXPR + && TREE_CODE (arg0) != TRUTH_ANDIF_EXPR + && TREE_CODE (arg0) != TRUTH_ORIF_EXPR + && TREE_CODE (arg0) != TRUTH_XOR_EXPR + /* Needed for sequence points and trappings, or side-effects. */ + && !TREE_SIDE_EFFECTS (arg0) + && !tree_could_trap_p (arg0)) + return fold_build2_loc (loc, ncode, type, arg0, arg1); + } + return NULL_TREE; } Index: gcc/gcc/fold-const.c =================================================================== --- gcc.orig/gcc/fold-const.c +++ gcc/gcc/fold-const.c @@ -111,14 +111,13 @@ static tree decode_field_reference (loca tree *, tree *); static int all_ones_mask_p (const_tree, int); static tree sign_bit_p (tree, const_tree); -static int simple_operand_p (const_tree); +static int simple_operand_p (tree); static tree range_binop (enum tree_code, tree, tree, int, tree, int); static tree range_predecessor (tree); static tree range_successor (tree); static tree fold_range_test (location_t, enum tree_code, tree, tree, tree); static tree fold_cond_expr_with_comparison (location_t, tree, tree, tree, tree); static tree unextend (tree, int, int, tree); -static tree fold_truthop (location_t, enum tree_code, tree, tree, tree); static tree optimize_minmax_comparison (location_t, enum tree_code, tree, tree, tree);