36
36
#include " llvm/Transforms/Utils/SizeOpts.h"
37
37
38
38
#include < cmath>
39
+ #include < map>
39
40
40
41
using namespace llvm ;
41
42
using namespace PatternMatch ;
@@ -2607,13 +2608,16 @@ Value *LibCallSimplifier::optimizeSqrt(CallInst *CI, IRBuilderBase &B) {
2607
2608
return copyFlags (*CI, FabsCall);
2608
2609
}
2609
2610
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) {
2612
2613
Module *M = CI->getModule ();
2613
2614
Function *Callee = CI->getCalledFunction ();
2614
2615
Value *Ret = nullptr ;
2615
2616
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))
2617
2621
Ret = optimizeUnaryDoubleFP (CI, B, TLI, true );
2618
2622
2619
2623
Value *Op1 = CI->getArgOperand (0 );
@@ -2626,16 +2630,34 @@ Value *LibCallSimplifier::optimizeTan(CallInst *CI, IRBuilderBase &B) {
2626
2630
return Ret;
2627
2631
2628
2632
// 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
2631
2637
LibFunc Func;
2632
2638
Function *F = OpC->getCalledFunction ();
2633
2639
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
+ }
2639
2661
return Ret;
2640
2662
}
2641
2663
@@ -3628,7 +3650,19 @@ Value *LibCallSimplifier::optimizeFloatingPointLibCall(CallInst *CI,
3628
3650
case LibFunc_tan:
3629
3651
case LibFunc_tanf:
3630
3652
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);
3632
3666
case LibFunc_ceil:
3633
3667
return replaceUnaryCall (CI, Builder, Intrinsic::ceil );
3634
3668
case LibFunc_floor:
@@ -3646,17 +3680,13 @@ Value *LibCallSimplifier::optimizeFloatingPointLibCall(CallInst *CI,
3646
3680
case LibFunc_acos:
3647
3681
case LibFunc_acosh:
3648
3682
case LibFunc_asin:
3649
- case LibFunc_asinh:
3650
3683
case LibFunc_atan:
3651
- case LibFunc_atanh:
3652
3684
case LibFunc_cbrt:
3653
- case LibFunc_cosh:
3654
3685
case LibFunc_exp:
3655
3686
case LibFunc_exp10:
3656
3687
case LibFunc_expm1:
3657
3688
case LibFunc_cos:
3658
3689
case LibFunc_sin:
3659
- case LibFunc_sinh:
3660
3690
case LibFunc_tanh:
3661
3691
if (UnsafeFPShrink && hasFloatVersion (M, CI->getCalledFunction ()->getName ()))
3662
3692
return optimizeUnaryDoubleFP (CI, Builder, TLI, true );
0 commit comments