Commit e19f6bde by Kaveh R. Ghazi Committed by Kaveh Ghazi

builtins.c (fold_builtin): Add new builtin optimizations for sqrt and/or cbrt.

	* builtins.c (fold_builtin): Add new builtin optimizations for
	sqrt and/or cbrt.
	* fold-const.c (fold): Likewise.

testsuite:
	* gcc.dg/torture/builtin-explog-1.c: Add new cases.
	* gcc.dg/torture/builtin-math-1.c: Likewise.
	* builtin-power-1.c: New test.

From-SVN: r79959
parent ec507f2d
2004-03-25 Kaveh R. Ghazi <ghazi@caip.rutgers.edu>
* builtins.c (fold_builtin): Add new builtin optimizations for
sqrt and/or cbrt.
* fold-const.c (fold): Likewise.
2004-03-25 David Edelsohn <edelsohn@gnu.org> 2004-03-25 David Edelsohn <edelsohn@gnu.org>
* config/rs6000/rs6000.c (rs6000_always_hint): New variable. * config/rs6000/rs6000.c (rs6000_always_hint): New variable.
......
...@@ -6690,6 +6690,29 @@ fold_builtin (tree exp) ...@@ -6690,6 +6690,29 @@ fold_builtin (tree exp)
return build_function_call_expr (expfn, arglist); return build_function_call_expr (expfn, arglist);
} }
/* Optimize sqrt(Nroot(x)) -> pow(x,1/(2*N)). */
if (flag_unsafe_math_optimizations && BUILTIN_ROOT_P (fcode))
{
tree powfn = mathfn_built_in (type, BUILT_IN_POW);
if (powfn)
{
tree arg0 = TREE_VALUE (TREE_OPERAND (arg, 1));
tree tree_root;
/* The inner root was either sqrt or cbrt. */
REAL_VALUE_TYPE dconstroot =
BUILTIN_SQRT_P (fcode) ? dconsthalf : dconstthird;
/* Adjust for the outer root. */
dconstroot.exp--;
dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
tree_root = build_real (type, dconstroot);
arglist = tree_cons (NULL_TREE, arg0,
build_tree_list (NULL_TREE, tree_root));
return build_function_call_expr (powfn, arglist);
}
}
/* Optimize sqrt(pow(x,y)) = pow(x,y*0.5). */ /* Optimize sqrt(pow(x,y)) = pow(x,y*0.5). */
if (flag_unsafe_math_optimizations if (flag_unsafe_math_optimizations
&& (fcode == BUILT_IN_POW && (fcode == BUILT_IN_POW
...@@ -6708,6 +6731,56 @@ fold_builtin (tree exp) ...@@ -6708,6 +6731,56 @@ fold_builtin (tree exp)
} }
break; break;
case BUILT_IN_CBRT:
case BUILT_IN_CBRTF:
case BUILT_IN_CBRTL:
if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
{
tree arg = TREE_VALUE (arglist);
const enum built_in_function fcode = builtin_mathfn_code (arg);
/* Optimize cbrt of constant value. */
if (real_zerop (arg) || real_onep (arg) || real_minus_onep (arg))
return arg;
/* Optimize cbrt(expN(x)) -> expN(x/3). */
if (flag_unsafe_math_optimizations && BUILTIN_EXPONENT_P (fcode))
{
tree expfn = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
const REAL_VALUE_TYPE third_trunc =
real_value_truncate (TYPE_MODE (type), dconstthird);
arg = fold (build (MULT_EXPR, type,
TREE_VALUE (TREE_OPERAND (arg, 1)),
build_real (type, third_trunc)));
arglist = build_tree_list (NULL_TREE, arg);
return build_function_call_expr (expfn, arglist);
}
/* Optimize cbrt(sqrt(x)) -> pow(x,1/6). */
/* We don't optimize cbrt(cbrt(x)) -> pow(x,1/9) because if
x is negative pow will error but cbrt won't. */
if (flag_unsafe_math_optimizations && BUILTIN_SQRT_P (fcode))
{
tree powfn = mathfn_built_in (type, BUILT_IN_POW);
if (powfn)
{
tree arg0 = TREE_VALUE (TREE_OPERAND (arg, 1));
tree tree_root;
REAL_VALUE_TYPE dconstroot = dconstthird;
dconstroot.exp--;
dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
tree_root = build_real (type, dconstroot);
arglist = tree_cons (NULL_TREE, arg0,
build_tree_list (NULL_TREE, tree_root));
return build_function_call_expr (powfn, arglist);
}
}
}
break;
case BUILT_IN_SIN: case BUILT_IN_SIN:
case BUILT_IN_SINF: case BUILT_IN_SINF:
case BUILT_IN_SINL: case BUILT_IN_SINL:
......
...@@ -6389,23 +6389,24 @@ fold (tree expr) ...@@ -6389,23 +6389,24 @@ fold (tree expr)
enum built_in_function fcode0 = builtin_mathfn_code (arg0); enum built_in_function fcode0 = builtin_mathfn_code (arg0);
enum built_in_function fcode1 = builtin_mathfn_code (arg1); enum built_in_function fcode1 = builtin_mathfn_code (arg1);
/* Optimizations of sqrt(...)*sqrt(...). */ /* Optimizations of root(...)*root(...). */
if (fcode0 == fcode1 && BUILTIN_SQRT_P (fcode0)) if (fcode0 == fcode1 && BUILTIN_ROOT_P (fcode0))
{ {
tree sqrtfn, arg, arglist; tree rootfn, arg, arglist;
tree arg00 = TREE_VALUE (TREE_OPERAND (arg0, 1)); tree arg00 = TREE_VALUE (TREE_OPERAND (arg0, 1));
tree arg10 = TREE_VALUE (TREE_OPERAND (arg1, 1)); tree arg10 = TREE_VALUE (TREE_OPERAND (arg1, 1));
/* Optimize sqrt(x)*sqrt(x) as x. */ /* Optimize sqrt(x)*sqrt(x) as x. */
if (operand_equal_p (arg00, arg10, 0) if (BUILTIN_SQRT_P (fcode0)
&& operand_equal_p (arg00, arg10, 0)
&& ! HONOR_SNANS (TYPE_MODE (type))) && ! HONOR_SNANS (TYPE_MODE (type)))
return arg00; return arg00;
/* Optimize sqrt(x)*sqrt(y) as sqrt(x*y). */ /* Optimize root(x)*root(y) as root(x*y). */
sqrtfn = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0); rootfn = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
arg = fold (build (MULT_EXPR, type, arg00, arg10)); arg = fold (build (MULT_EXPR, type, arg00, arg10));
arglist = build_tree_list (NULL_TREE, arg); arglist = build_tree_list (NULL_TREE, arg);
return build_function_call_expr (sqrtfn, arglist); return build_function_call_expr (rootfn, arglist);
} }
/* Optimize expN(x)*expN(y) as expN(x+y). */ /* Optimize expN(x)*expN(y) as expN(x+y). */
......
2004-03-25 Kaveh R. Ghazi <ghazi@caip.rutgers.edu>
* gcc.dg/torture/builtin-explog-1.c: Add new cases.
* gcc.dg/torture/builtin-math-1.c: Likewise.
* builtin-power-1.c: New test.
2004-03-24 Andreas Schwab <schwab@suse.de> 2004-03-24 Andreas Schwab <schwab@suse.de>
* lib/prune.exp (prune_gcc_output): Ignore errata warning from * lib/prune.exp (prune_gcc_output): Ignore errata warning from
......
/* Copyright (C) 2003 Free Software Foundation. /* Copyright (C) 2003, 2004 Free Software Foundation.
Verify that built-in math function constant folding of log & exp is Verify that built-in math function constant folding of log & exp is
correctly performed by the compiler. correctly performed by the compiler.
...@@ -119,6 +119,17 @@ void test(double d1, double d2, float f1, float f2, ...@@ -119,6 +119,17 @@ void test(double d1, double d2, float f1, float f2,
LOG_CBRT(log2); LOG_CBRT(log2);
LOG_CBRT(log10); LOG_CBRT(log10);
/* Test cbrt(expN(x)) -> expN(x/3). */
#define CBRT_EXP(EXP) \
extern void link_failure_cbrt_##EXP(void); \
if (cbrt(EXP(d1)) != EXP(d1/3.0) || cbrtf(EXP##f(f1)) != EXP##f(f1/3.0F) \
|| cbrtl(EXP##l(ld1)) != EXP##l(ld1/3.0L)) link_failure_cbrt_##EXP()
CBRT_EXP(exp);
CBRT_EXP(exp2);
CBRT_EXP(exp10);
CBRT_EXP(pow10);
/* Test logN(pow(x,y)) -> y*logN(x). */ /* Test logN(pow(x,y)) -> y*logN(x). */
#define LOG_POW(LOG, POW) \ #define LOG_POW(LOG, POW) \
extern void link_failure_##LOG##_##POW(void); \ extern void link_failure_##LOG##_##POW(void); \
......
/* Copyright (C) 2002, 2003 Free Software Foundation. /* Copyright (C) 2002, 2003, 2004 Free Software Foundation.
Verify that built-in math function constant folding of constant Verify that built-in math function constant folding of constant
arguments is correctly performed by the compiler. arguments is correctly performed by the compiler.
...@@ -18,6 +18,15 @@ void test (float f, double d, long double ld) ...@@ -18,6 +18,15 @@ void test (float f, double d, long double ld)
if (sqrt (1.0) != 1.0) if (sqrt (1.0) != 1.0)
link_error (); link_error ();
if (cbrt (0.0) != 0.0)
link_error ();
if (cbrt (1.0) != 1.0)
link_error ();
if (cbrt (-1.0) != -1.0)
link_error ();
if (exp (0.0) != 1.0) if (exp (0.0) != 1.0)
link_error (); link_error ();
...@@ -55,6 +64,15 @@ void test (float f, double d, long double ld) ...@@ -55,6 +64,15 @@ void test (float f, double d, long double ld)
if (sqrtf (1.0F) != 1.0F) if (sqrtf (1.0F) != 1.0F)
link_error (); link_error ();
if (cbrtf (0.0F) != 0.0F)
link_error ();
if (cbrtf (1.0F) != 1.0F)
link_error ();
if (cbrtf (-1.0F) != -1.0F)
link_error ();
if (expf (0.0F) != 1.0F) if (expf (0.0F) != 1.0F)
link_error (); link_error ();
...@@ -92,6 +110,15 @@ void test (float f, double d, long double ld) ...@@ -92,6 +110,15 @@ void test (float f, double d, long double ld)
if (sqrtl (1.0L) != 1.0L) if (sqrtl (1.0L) != 1.0L)
link_error (); link_error ();
if (cbrtl (0.0L) != 0.0L)
link_error ();
if (cbrtl (1.0L) != 1.0L)
link_error ();
if (cbrtl (-1.0L) != -1.0L)
link_error ();
if (expl (0.0L) != 1.0L) if (expl (0.0L) != 1.0L)
link_error (); link_error ();
......
/* Copyright (C) 2004 Free Software Foundation.
Verify that built-in folding of various math "power" functions is
correctly performed by the compiler.
Written by Kaveh Ghazi, 2004-03-11. */
/* { dg-do link } */
/* { dg-options "-ffast-math" } */
#include "../builtins-config.h"
#ifdef HAVE_C99_RUNTIME
#define C99CODE(CODE) CODE
#else
#define C99CODE(CODE) 0
#endif
#define PROTOTYPE(FN) extern double FN(double); extern float FN##f(float); \
extern long double FN##l(long double);
#define PROTOTYPE2(FN) extern double FN(double, double); \
extern float FN##f(float, float); \
extern long double FN##l(long double, long double);
PROTOTYPE(sqrt)
PROTOTYPE(cbrt)
PROTOTYPE2(pow)
void test(double d1, double d2, double d3,
float f1, float f2, float f3,
long double ld1, long double ld2, long double ld3)
{
/* Test N1root(N2root(x)) -> pow(x,1/(N1*N2)). */
/* E.g. sqrt(cbrt(x)) -> pow(x,1/6). */
#define ROOT_ROOT(FN1,N1,FN2,N2) \
extern void link_failure_##FN1##_##FN2(void); \
if (FN1(FN2(d1)) != pow(d1,1.0/(N1*N2)) \
|| C99CODE (FN1##f(FN2##f(f1)) != powf(f1,1.0F/(N1*N2))) \
|| C99CODE (FN1##l(FN2##l(ld1)) != powl(ld1,1.0L/(N1*N2)))) \
link_failure_##FN1##_##FN2()
ROOT_ROOT(sqrt,2,sqrt,2);
ROOT_ROOT(sqrt,2,cbrt,3);
ROOT_ROOT(cbrt,3,sqrt,2);
/*ROOT_ROOT(cbrt,3,cbrt,3); Intentionally not implemented. */
/* Test pow(Nroot(x),y) -> pow(x,y/N). */
#define POW_ROOT(FN,N) \
extern void link_failure_pow_##FN(void); \
if (pow(FN(d1), d2) != pow(d1,d2/N) \
|| powf(FN##f(f1),f2) != powf(f1,f2/N) \
|| powl(FN##l(ld1),ld2) != powl(ld1,ld2/N)) \
link_failure_pow_##FN()
POW_ROOT(sqrt,2);
/*POW_ROOT(cbrt,3); Intentionally not implemented. */
/* Test Nroot(pow(x,y)) -> pow(x,y/N). */
#define ROOT_POW(FN,N) \
extern void link_failure_##FN##_pow(void); \
if (FN(pow(d1, d2)) != pow(d1,d2/N) \
|| FN##f(powf(f1,f2)) != powf(f1,f2/N) \
|| FN##l(powl(ld1,ld2)) != powl(ld1,ld2/N)) \
link_failure_##FN##_pow()
ROOT_POW(sqrt,2);
/*ROOT_POW(cbrt,3); Intentionally not implemented. */
/* Test pow(pow(x,y),z) -> pow(x,y*z). */
#define POW_POW \
extern void link_failure_pow_pow(void); \
if (pow(pow(d1, d2), d3) != pow(d1,d2*d3) \
|| powf(powf(f1,f2),f3) != powf(f1,f2*f3) \
|| powl(powl(ld1,ld2),ld3) != powl(ld1,ld2*ld3)) \
link_failure_pow_pow()
POW_POW;
/* Test Nroot(x)*Nroot(y) -> Nroot(x*y). */
#define ROOT_X_ROOT(FN) \
extern void link_failure_root_x_root(void); \
if (FN(d1)*FN(d2) != FN(d1*d2) \
|| FN##f(f1)*FN##f(f2) != FN##f(f1*f2) \
|| FN##l(ld1)*FN##l(ld2) != FN##l(ld1*ld2)) \
link_failure_root_x_root()
ROOT_X_ROOT(sqrt);
ROOT_X_ROOT(cbrt);
/* Test pow(x,y)*pow(x,z) -> pow(x,y+z). */
#define POW_X_POW \
extern void link_failure_pow_x_pow(void); \
if (pow(d1,d2)*pow(d1,d3) != pow(d1,d2+d3) \
|| powf(f1,f2)*powf(f1,f3) != powf(f1,f2+f3) \
|| powl(ld1,ld2)*powl(ld1,ld3) != powl(ld1,ld2+ld3)) \
link_failure_pow_x_pow()
POW_X_POW;
}
int main (void)
{
return 0;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment