@@ -2607,13 +2607,16 @@ Value *LibCallSimplifier::optimizeSqrt(CallInst *CI, IRBuilderBase &B) {
2607
2607
return copyFlags (*CI, FabsCall);
2608
2608
}
2609
2609
2610
- // TODO: Generalize to handle any trig function and its inverse.
2611
- Value * LibCallSimplifier::optimizeTan (CallInst *CI, IRBuilderBase &B) {
2610
+ Value * LibCallSimplifier::optimizeTrigInversionPairs (CallInst *CI,
2611
+ IRBuilderBase &B) {
2612
2612
Module *M = CI->getModule ();
2613
2613
Function *Callee = CI->getCalledFunction ();
2614
2614
Value *Ret = nullptr ;
2615
2615
StringRef Name = Callee->getName ();
2616
- if (UnsafeFPShrink && Name == " tan" && hasFloatVersion (M, Name))
2616
+ if (UnsafeFPShrink &&
2617
+ (Name == " tan" || Name == " atanh" || Name == " sinh" || Name == " cosh" ||
2618
+ Name == " asinh" ) &&
2619
+ hasFloatVersion (M, Name))
2617
2620
Ret = optimizeUnaryDoubleFP (CI, B, TLI, true );
2618
2621
2619
2622
Value *Op1 = CI->getArgOperand (0 );
@@ -2626,16 +2629,34 @@ Value *LibCallSimplifier::optimizeTan(CallInst *CI, IRBuilderBase &B) {
2626
2629
return Ret;
2627
2630
2628
2631
// tan(atan(x)) -> x
2629
- // tanf(atanf(x)) -> x
2630
- // tanl(atanl(x)) -> x
2632
+ // atanh(tanh(x)) -> x
2633
+ // sinh(asinh(x)) -> x
2634
+ // asinh(sinh(x)) -> x
2635
+ // cosh(acosh(x)) -> x
2631
2636
LibFunc Func;
2632
2637
Function *F = OpC->getCalledFunction ();
2633
2638
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 );
2639
+ isLibFuncEmittable (M, TLI, Func)) {
2640
+ LibFunc inverseFunc = llvm::StringSwitch<LibFunc>(Callee->getName ())
2641
+ .Case (" tan" , LibFunc_atan)
2642
+ .Case (" atanh" , LibFunc_tanh)
2643
+ .Case (" sinh" , LibFunc_asinh)
2644
+ .Case (" cosh" , LibFunc_acosh)
2645
+ .Case (" tanf" , LibFunc_atanf)
2646
+ .Case (" atanhf" , LibFunc_tanhf)
2647
+ .Case (" sinhf" , LibFunc_asinhf)
2648
+ .Case (" coshf" , LibFunc_acoshf)
2649
+ .Case (" tanl" , LibFunc_atanl)
2650
+ .Case (" atanhl" , LibFunc_tanhl)
2651
+ .Case (" sinhl" , LibFunc_asinhl)
2652
+ .Case (" coshl" , LibFunc_acoshl)
2653
+ .Case (" asinh" , LibFunc_sinh)
2654
+ .Case (" asinhf" , LibFunc_sinhf)
2655
+ .Case (" asinhl" , LibFunc_sinhl)
2656
+ .Default (NumLibFuncs); // Used as error value
2657
+ if (Func == inverseFunc)
2658
+ Ret = OpC->getArgOperand (0 );
2659
+ }
2639
2660
return Ret;
2640
2661
}
2641
2662
@@ -3628,7 +3649,19 @@ Value *LibCallSimplifier::optimizeFloatingPointLibCall(CallInst *CI,
3628
3649
case LibFunc_tan:
3629
3650
case LibFunc_tanf:
3630
3651
case LibFunc_tanl:
3631
- return optimizeTan (CI, Builder);
3652
+ case LibFunc_sinh:
3653
+ case LibFunc_sinhf:
3654
+ case LibFunc_sinhl:
3655
+ case LibFunc_asinh:
3656
+ case LibFunc_asinhf:
3657
+ case LibFunc_asinhl:
3658
+ case LibFunc_cosh:
3659
+ case LibFunc_coshf:
3660
+ case LibFunc_coshl:
3661
+ case LibFunc_atanh:
3662
+ case LibFunc_atanhf:
3663
+ case LibFunc_atanhl:
3664
+ return optimizeTrigInversionPairs (CI, Builder);
3632
3665
case LibFunc_ceil:
3633
3666
return replaceUnaryCall (CI, Builder, Intrinsic::ceil );
3634
3667
case LibFunc_floor:
@@ -3646,17 +3679,13 @@ Value *LibCallSimplifier::optimizeFloatingPointLibCall(CallInst *CI,
3646
3679
case LibFunc_acos:
3647
3680
case LibFunc_acosh:
3648
3681
case LibFunc_asin:
3649
- case LibFunc_asinh:
3650
3682
case LibFunc_atan:
3651
- case LibFunc_atanh:
3652
3683
case LibFunc_cbrt:
3653
- case LibFunc_cosh:
3654
3684
case LibFunc_exp:
3655
3685
case LibFunc_exp10:
3656
3686
case LibFunc_expm1:
3657
3687
case LibFunc_cos:
3658
3688
case LibFunc_sin:
3659
- case LibFunc_sinh:
3660
3689
case LibFunc_tanh:
3661
3690
if (UnsafeFPShrink && hasFloatVersion (M, CI->getCalledFunction ()->getName ()))
3662
3691
return optimizeUnaryDoubleFP (CI, Builder, TLI, true );
0 commit comments