[llvm] e079bf6 - [AggressiveInstCombine] check sqrt operand to allow more libcall->intrinsic transforms

Sanjay Patel via llvm-commits llvm-commits at lists.llvm.org
Wed Jul 27 08:36:22 PDT 2022


Author: Sanjay Patel
Date: 2022-07-27T11:36:13-04:00
New Revision: e079bf655832b2261faf6883984400a8c37b1baa

URL: https://github.com/llvm/llvm-project/commit/e079bf655832b2261faf6883984400a8c37b1baa
DIFF: https://github.com/llvm/llvm-project/commit/e079bf655832b2261faf6883984400a8c37b1baa.diff

LOG: [AggressiveInstCombine] check sqrt operand to allow more libcall->intrinsic transforms

This should fix issue #56383 (at least when compiled with -O3 because this pass is only
run at -O3 currently).

Added: 
    

Modified: 
    llvm/lib/Transforms/AggressiveInstCombine/AggressiveInstCombine.cpp
    llvm/test/Transforms/AggressiveInstCombine/X86/sqrt.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/AggressiveInstCombine/AggressiveInstCombine.cpp b/llvm/lib/Transforms/AggressiveInstCombine/AggressiveInstCombine.cpp
index 35adaa3bde658..c3541b5ec58a6 100644
--- a/llvm/lib/Transforms/AggressiveInstCombine/AggressiveInstCombine.cpp
+++ b/llvm/lib/Transforms/AggressiveInstCombine/AggressiveInstCombine.cpp
@@ -446,21 +446,22 @@ foldSqrt(Instruction &I, TargetTransformInfo &TTI, TargetLibraryInfo &TLI) {
   if (Func != LibFunc_sqrt && Func != LibFunc_sqrtf && Func != LibFunc_sqrtl)
     return false;
 
-  // If (1) this is a sqrt libcall, (2) we can assume that NAN is not created,
-  // and (3) we would not end up lowering to a libcall anyway (which could
-  // change the value of errno), then:
-  // (1) the operand arg must not be less than -0.0.
-  // (2) errno won't be set.
-  // (3) it is safe to convert this to an intrinsic call.
-  // TODO: Check if the arg is known non-negative.
+  // If (1) this is a sqrt libcall, (2) we can assume that NAN is not created
+  // (because NNAN or the operand arg must not be less than -0.0) and (2) we
+  // would not end up lowering to a libcall anyway (which could change the value
+  // of errno), then:
+  // (1) errno won't be set.
+  // (2) it is safe to convert this to an intrinsic call.
   Type *Ty = Call->getType();
-  if (TTI.haveFastSqrt(Ty) && Call->hasNoNaNs()) {
+  Value *Arg = Call->getArgOperand(0);
+  if (TTI.haveFastSqrt(Ty) &&
+      (Call->hasNoNaNs() || CannotBeOrderedLessThanZero(Arg, &TLI))) {
     IRBuilder<> Builder(&I);
     IRBuilderBase::FastMathFlagGuard Guard(Builder);
     Builder.setFastMathFlags(Call->getFastMathFlags());
 
     Function *Sqrt = Intrinsic::getDeclaration(M, Intrinsic::sqrt, Ty);
-    Value *NewSqrt = Builder.CreateCall(Sqrt, Call->getArgOperand(0), "sqrt");
+    Value *NewSqrt = Builder.CreateCall(Sqrt, Arg, "sqrt");
     I.replaceAllUsesWith(NewSqrt);
 
     // Explicitly erase the old call because a call with side effects is not

diff  --git a/llvm/test/Transforms/AggressiveInstCombine/X86/sqrt.ll b/llvm/test/Transforms/AggressiveInstCombine/X86/sqrt.ll
index 42d7f5870ce4c..665f6de96d932 100644
--- a/llvm/test/Transforms/AggressiveInstCombine/X86/sqrt.ll
+++ b/llvm/test/Transforms/AggressiveInstCombine/X86/sqrt.ll
@@ -56,8 +56,8 @@ define float @sqrt_call_nnan_f32_nobuiltin(float %x) {
 define float @sqrt_call_f32_squared(float %x) {
 ; CHECK-LABEL: @sqrt_call_f32_squared(
 ; CHECK-NEXT:    [[X2:%.*]] = fmul float [[X:%.*]], [[X]]
-; CHECK-NEXT:    [[SQRT:%.*]] = call float @sqrtf(float [[X2]])
-; CHECK-NEXT:    ret float [[SQRT]]
+; CHECK-NEXT:    [[SQRT1:%.*]] = call float @llvm.sqrt.f32(float [[X2]])
+; CHECK-NEXT:    ret float [[SQRT1]]
 ;
   %x2 = fmul float %x, %x
   %sqrt = call float @sqrtf(float %x2)
@@ -67,8 +67,8 @@ define float @sqrt_call_f32_squared(float %x) {
 define float @sqrt_call_f32_fabs(float %x) {
 ; CHECK-LABEL: @sqrt_call_f32_fabs(
 ; CHECK-NEXT:    [[A:%.*]] = call float @llvm.fabs.f32(float [[X:%.*]])
-; CHECK-NEXT:    [[SQRT:%.*]] = call float @sqrtf(float [[A]])
-; CHECK-NEXT:    ret float [[SQRT]]
+; CHECK-NEXT:    [[SQRT1:%.*]] = call float @llvm.sqrt.f32(float [[A]])
+; CHECK-NEXT:    ret float [[SQRT1]]
 ;
   %a = call float @llvm.fabs.f32(float %x)
   %sqrt = call float @sqrtf(float %a)


        


More information about the llvm-commits mailing list