From patchwork Thu May 19 14:04:01 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bill Schmidt X-Patchwork-Id: 96383 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 3C5F0B71B0 for ; Fri, 20 May 2011 00:11:04 +1000 (EST) Received: (qmail 27385 invoked by alias); 19 May 2011 14:04:22 -0000 Received: (qmail 27361 invoked by uid 22791); 19 May 2011 14:04:19 -0000 X-SWARE-Spam-Status: No, hits=-0.3 required=5.0 tests=AWL, BAYES_50, TW_CF, TW_TM, T_RP_MATCHES_RCVD X-Spam-Check-By: sourceware.org Received: from e4.ny.us.ibm.com (HELO e4.ny.us.ibm.com) (32.97.182.144) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Thu, 19 May 2011 14:04:04 +0000 Received: from d01relay03.pok.ibm.com (d01relay03.pok.ibm.com [9.56.227.235]) by e4.ny.us.ibm.com (8.14.4/8.13.1) with ESMTP id p4JDh8wc021095 for ; Thu, 19 May 2011 09:43:08 -0400 Received: from d01av02.pok.ibm.com (d01av02.pok.ibm.com [9.56.224.216]) by d01relay03.pok.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id p4JE400T093896 for ; Thu, 19 May 2011 10:04:00 -0400 Received: from d01av02.pok.ibm.com (loopback [127.0.0.1]) by d01av02.pok.ibm.com (8.14.4/8.13.1/NCO v10.0 AVout) with ESMTP id p4JE3xXf024696 for ; Thu, 19 May 2011 11:03:59 -0300 Received: from [9.65.114.98] (sig-9-65-114-98.mts.ibm.com [9.65.114.98]) by d01av02.pok.ibm.com (8.14.4/8.13.1/NCO v10.0 AVin) with ESMTP id p4JE3vGr024528 for ; Thu, 19 May 2011 11:03:58 -0300 Subject: [PATCH] Add powi-to-multiply expansion to cse_sincos pass From: "William J. Schmidt" To: gcc-patches@gcc.gnu.org Date: Thu, 19 May 2011 09:04:01 -0500 Message-ID: <1305813841.29239.3.camel@gnopaine> Mime-Version: 1.0 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 As Richard Guenther suggested, I'm submitting a separate patch to add powi expansion to multiplies during the cse_sincos pass. This was originally submitted as part of a larger patch to fix PR46728. Richard noted that tree_expand_builtin_powi, powi_as_mults, and powi_as_mults_1 more properly belong in tree-ssa-math-opts.c. I've moved those here. These were originally in builtins.c because they require some #defines, a static table of values, and a couple of static functions in builtins.c. Those items will also belong in tree-ssa-math-opts.c once the similar RTL logic is removed. I chose to duplicate the required items into tree-ssa-math-opts.c for now. They will be removed from builtins.c in a future patch. This seemed more reasonable than exposing everything in a .h temporarily, and then removing the .h code later. But I can change to go that route if you prefer. Bootstrap/regtest complete on powerpc target. Seeking approval for mainline -- thanks! Bill 2011-05-19 Bill Schmidt * tree-ssa-math-opts.c (POWI_MAX_MULTS): Copied from builtins.c; will be deleted from builtins.c in a later patch. (POWI_TABLE_SIZE): Likewise. (POWI_WINDOW_SIZE): Likewise. (powi_table): Likewise. (powi_lookup_cost): Likewise. (powi_cost): Likewise. (powi_as_mults_1): New. (powi_as_mults): New. (tree_expand_builtin_powi): New. (execute_cse_sincos): Add switch case for BUILT_IN_POWI. (gate_cse_sincos): Remove sincos/cexp restriction. Index: gcc/tree-ssa-math-opts.c =================================================================== --- gcc/tree-ssa-math-opts.c (revision 173908) +++ gcc/tree-ssa-math-opts.c (working copy) @@ -795,8 +795,239 @@ execute_cse_sincos_1 (tree name) return cfg_changed; } +/* To evaluate powi(x,n), the floating point value x raised to the + constant integer exponent n, we use a hybrid algorithm that + combines the "window method" with look-up tables. For an + introduction to exponentiation algorithms and "addition chains", + see section 4.6.3, "Evaluation of Powers" of Donald E. Knuth, + "Seminumerical Algorithms", Vol. 2, "The Art of Computer Programming", + 3rd Edition, 1998, and Daniel M. Gordon, "A Survey of Fast Exponentiation + Methods", Journal of Algorithms, Vol. 27, pp. 129-146, 1998. */ + +/* Provide a default value for POWI_MAX_MULTS, the maximum number of + multiplications to inline before calling the system library's pow + function. powi(x,n) requires at worst 2*bits(n)-2 multiplications, + so this default never requires calling pow, powf or powl. */ + +#ifndef POWI_MAX_MULTS +#define POWI_MAX_MULTS (2*HOST_BITS_PER_WIDE_INT-2) +#endif + +/* The size of the "optimal power tree" lookup table. All + exponents less than this value are simply looked up in the + powi_table below. This threshold is also used to size the + cache of pseudo registers that hold intermediate results. */ +#define POWI_TABLE_SIZE 256 + +/* The size, in bits of the window, used in the "window method" + exponentiation algorithm. This is equivalent to a radix of + (1<= POWI_TABLE_SIZE) + { + if (val & 1) + { + digit = val & ((1 << POWI_WINDOW_SIZE) - 1); + result += powi_lookup_cost (digit, cache) + + POWI_WINDOW_SIZE + 1; + val >>= POWI_WINDOW_SIZE; + } + else + { + val >>= 1; + result++; + } + } + + return result + powi_lookup_cost (val, cache); +} + +/* Recursive subroutine of powi_as_mults. This function takes the + array, CACHE, of already calculated exponents and an exponent N and + returns a tree that corresponds to CACHE[1]**N, with type TYPE. */ + +static tree +powi_as_mults_1 (gimple_stmt_iterator *gsi, location_t loc, tree type, + HOST_WIDE_INT n, tree *cache) +{ + tree op0, op1, target; + unsigned HOST_WIDE_INT digit; + gimple mult_stmt; + + if (n < POWI_TABLE_SIZE) + { + if (cache[n]) + return cache[n]; + + target = create_tmp_var (type, "powmult"); + add_referenced_var (target); + target = make_ssa_name (target, NULL); + cache[n] = target; + + op0 = powi_as_mults_1 (gsi, loc, type, n - powi_table[n], cache); + op1 = powi_as_mults_1 (gsi, loc, type, powi_table[n], cache); + } + else if (n & 1) + { + target = create_tmp_var (type, "powmult"); + add_referenced_var (target); + target = make_ssa_name (target, NULL); + digit = n & ((1 << POWI_WINDOW_SIZE) - 1); + op0 = powi_as_mults_1 (gsi, loc, type, n - digit, cache); + op1 = powi_as_mults_1 (gsi, loc, type, digit, cache); + } + else + { + target = create_tmp_var (type, "powmult"); + add_referenced_var (target); + target = make_ssa_name (target, NULL); + op0 = powi_as_mults_1 (gsi, loc, type, n >> 1, cache); + op1 = op0; + } + + mult_stmt = gimple_build_assign_with_ops (MULT_EXPR, target, op0, op1); + SSA_NAME_DEF_STMT (target) = mult_stmt; + gsi_insert_before (gsi, mult_stmt, GSI_SAME_STMT); + + return target; +} + +/* Convert ARG0**N to a tree of multiplications of ARG0 with itself. + This function needs to be kept in sync with powi_cost in builtins.c. */ + +static tree +powi_as_mults (gimple_stmt_iterator *gsi, location_t loc, + tree arg0, HOST_WIDE_INT n) +{ + tree cache[POWI_TABLE_SIZE], result, type = TREE_TYPE (arg0); + + if (n == 0) + return omit_one_operand_loc (loc, type, build_real (type, dconst1), arg0); + + memset (cache, 0, sizeof (cache)); + cache[1] = arg0; + + result = powi_as_mults_1 (gsi, loc, type, (n < 0) ? -n : n, cache); + + /* If the original exponent was negative, reciprocate the result. */ + if (n < 0) + result = build2_loc (loc, RDIV_EXPR, type, + build_real (type, dconst1), result); + return result; +} + +/* ARGS are the two arguments to a powi builtin in GSI with location info + LOC. If the arguments are appropriate, create an equivalent set of + statements prior to GSI using an optimal number of multiplications, + and return an expession holding the result. */ + +static tree +tree_expand_builtin_powi (gimple_stmt_iterator *gsi, location_t loc, tree *args) +{ + HOST_WIDE_INT n = TREE_INT_CST_LOW (args[1]); + HOST_WIDE_INT n_hi = TREE_INT_CST_HIGH (args[1]); + + if ((n_hi == 0 || n_hi == -1) + /* Avoid largest negative number. */ + && (n != -n) + && ((n >= -1 && n <= 2) + || (optimize_function_for_speed_p (cfun) + && powi_cost (n) <= POWI_MAX_MULTS))) + return powi_as_mults (gsi, loc, args[0], n); + + return NULL_TREE; +} + /* Go through all calls to sin, cos and cexpi and call execute_cse_sincos_1 - on the SSA_NAME argument of each of them. */ + on the SSA_NAME argument of each of them. Also expand powi(x,n) into + an optimal number of multiplies, when n is a constant. */ static unsigned int execute_cse_sincos (void) @@ -821,7 +1052,7 @@ execute_cse_sincos (void) && (fndecl = gimple_call_fndecl (stmt)) && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL) { - tree arg; + tree arg, *args; switch (DECL_FUNCTION_CODE (fndecl)) { @@ -833,6 +1064,23 @@ execute_cse_sincos (void) cfg_changed |= execute_cse_sincos_1 (arg); break; + CASE_FLT_FN (BUILT_IN_POWI): + args = gimple_call_arg_ptr (stmt, 0); + if (host_integerp (args[1], 0) && !TREE_OVERFLOW (args[1])) + { + tree result = NULL_TREE; + location_t loc = gimple_location (stmt); + result = tree_expand_builtin_powi (&gsi, loc, args); + if (result) + { + tree lhs = gimple_get_lhs (stmt); + gimple new_stmt = gimple_build_assign (lhs, result); + gimple_set_location (new_stmt, loc); + gsi_replace (&gsi, new_stmt, true); + } + } + break; + default:; } } @@ -849,10 +1097,9 @@ execute_cse_sincos (void) static bool gate_cse_sincos (void) { - /* Make sure we have either sincos or cexp. */ - return (TARGET_HAS_SINCOS - || TARGET_C99_FUNCTIONS) - && optimize; + /* We no longer require either sincos or cexp, since powi expansion + piggybacks on this pass. */ + return optimize; } struct gimple_opt_pass pass_cse_sincos =