Skip to content

Commit c63a5ba

Browse files
committed
[Transforms] Expand optimizeTan to fold more inverse trig pairs
optimizeTan has been renamed to optimizeTrigInversionPairs as a result. Sadly, this is not mathematically true that all inverse pairs fold to x. For example, asin(sin(x)) does not fold to x if x is over 2pi.
1 parent 13af5b2 commit c63a5ba

File tree

2 files changed

+46
-16
lines changed

2 files changed

+46
-16
lines changed

llvm/include/llvm/Transforms/Utils/SimplifyLibCalls.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ class LibCallSimplifier {
202202
Value *optimizeLog(CallInst *CI, IRBuilderBase &B);
203203
Value *optimizeSqrt(CallInst *CI, IRBuilderBase &B);
204204
Value *optimizeSinCosPi(CallInst *CI, bool IsSin, IRBuilderBase &B);
205-
Value *optimizeTan(CallInst *CI, IRBuilderBase &B);
205+
Value *optimizeTrigInversionPairs(CallInst *CI, IRBuilderBase &B);
206206
// Wrapper for all floating point library call optimizations
207207
Value *optimizeFloatingPointLibCall(CallInst *CI, LibFunc Func,
208208
IRBuilderBase &B);

llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp

Lines changed: 45 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include "llvm/Transforms/Utils/SizeOpts.h"
3737

3838
#include <cmath>
39+
#include <map>
3940

4041
using namespace llvm;
4142
using namespace PatternMatch;
@@ -2607,13 +2608,16 @@ Value *LibCallSimplifier::optimizeSqrt(CallInst *CI, IRBuilderBase &B) {
26072608
return copyFlags(*CI, FabsCall);
26082609
}
26092610

2610-
// TODO: Generalize to handle any trig function and its inverse.
2611-
Value *LibCallSimplifier::optimizeTan(CallInst *CI, IRBuilderBase &B) {
2611+
Value *LibCallSimplifier::optimizeTrigInversionPairs(CallInst *CI,
2612+
IRBuilderBase &B) {
26122613
Module *M = CI->getModule();
26132614
Function *Callee = CI->getCalledFunction();
26142615
Value *Ret = nullptr;
26152616
StringRef Name = Callee->getName();
2616-
if (UnsafeFPShrink && Name == "tan" && hasFloatVersion(M, Name))
2617+
if (UnsafeFPShrink &&
2618+
(Name == "tan" || Name == "atanh" || Name == "sinh" || Name == "cosh" ||
2619+
Name == "asinh") &&
2620+
hasFloatVersion(M, Name))
26172621
Ret = optimizeUnaryDoubleFP(CI, B, TLI, true);
26182622

26192623
Value *Op1 = CI->getArgOperand(0);
@@ -2626,16 +2630,34 @@ Value *LibCallSimplifier::optimizeTan(CallInst *CI, IRBuilderBase &B) {
26262630
return Ret;
26272631

26282632
// tan(atan(x)) -> x
2629-
// tanf(atanf(x)) -> x
2630-
// tanl(atanl(x)) -> x
2633+
// atanh(tanh(x)) -> x
2634+
// sinh(asinh(x)) -> x
2635+
// asinh(sinh(x)) -> x
2636+
// cosh(acosh(x)) -> x
26312637
LibFunc Func;
26322638
Function *F = OpC->getCalledFunction();
26332639
if (F && TLI->getLibFunc(F->getName(), Func) &&
2634-
isLibFuncEmittable(M, TLI, Func) &&
2635-
((Func == LibFunc_atan && Callee->getName() == "tan") ||
2636-
(Func == LibFunc_atanf && Callee->getName() == "tanf") ||
2637-
(Func == LibFunc_atanl && Callee->getName() == "tanl")))
2638-
Ret = OpC->getArgOperand(0);
2640+
isLibFuncEmittable(M, TLI, Func)) {
2641+
LibFunc inverseFunc = llvm::StringSwitch<LibFunc>(Callee->getName())
2642+
.Case("tan", LibFunc_atan)
2643+
.Case("atanh", LibFunc_tanh)
2644+
.Case("sinh", LibFunc_asinh)
2645+
.Case("cosh", LibFunc_acosh)
2646+
.Case("tanf", LibFunc_atanf)
2647+
.Case("atanhf", LibFunc_tanhf)
2648+
.Case("sinhf", LibFunc_asinhf)
2649+
.Case("coshf", LibFunc_acoshf)
2650+
.Case("tanl", LibFunc_atanl)
2651+
.Case("atanhl", LibFunc_tanhl)
2652+
.Case("sinhl", LibFunc_asinhl)
2653+
.Case("coshl", LibFunc_acoshl)
2654+
.Case("asinh", LibFunc_sinh)
2655+
.Case("asinhf", LibFunc_sinhf)
2656+
.Case("asinhl", LibFunc_sinhl)
2657+
.Default(NumLibFuncs); // Used as error value
2658+
if (Func == inverseFunc)
2659+
Ret = OpC->getArgOperand(0);
2660+
}
26392661
return Ret;
26402662
}
26412663

@@ -3628,7 +3650,19 @@ Value *LibCallSimplifier::optimizeFloatingPointLibCall(CallInst *CI,
36283650
case LibFunc_tan:
36293651
case LibFunc_tanf:
36303652
case LibFunc_tanl:
3631-
return optimizeTan(CI, Builder);
3653+
case LibFunc_sinh:
3654+
case LibFunc_sinhf:
3655+
case LibFunc_sinhl:
3656+
case LibFunc_asinh:
3657+
case LibFunc_asinhf:
3658+
case LibFunc_asinhl:
3659+
case LibFunc_cosh:
3660+
case LibFunc_coshf:
3661+
case LibFunc_coshl:
3662+
case LibFunc_atanh:
3663+
case LibFunc_atanhf:
3664+
case LibFunc_atanhl:
3665+
return optimizeTrigInversionPairs(CI, Builder);
36323666
case LibFunc_ceil:
36333667
return replaceUnaryCall(CI, Builder, Intrinsic::ceil);
36343668
case LibFunc_floor:
@@ -3646,17 +3680,13 @@ Value *LibCallSimplifier::optimizeFloatingPointLibCall(CallInst *CI,
36463680
case LibFunc_acos:
36473681
case LibFunc_acosh:
36483682
case LibFunc_asin:
3649-
case LibFunc_asinh:
36503683
case LibFunc_atan:
3651-
case LibFunc_atanh:
36523684
case LibFunc_cbrt:
3653-
case LibFunc_cosh:
36543685
case LibFunc_exp:
36553686
case LibFunc_exp10:
36563687
case LibFunc_expm1:
36573688
case LibFunc_cos:
36583689
case LibFunc_sin:
3659-
case LibFunc_sinh:
36603690
case LibFunc_tanh:
36613691
if (UnsafeFPShrink && hasFloatVersion(M, CI->getCalledFunction()->getName()))
36623692
return optimizeUnaryDoubleFP(CI, Builder, TLI, true);

0 commit comments

Comments
 (0)