[llvm] faa3273 - SimplifyLibCalls: Pass AssumptionCache to isKnownNeverInfinity

Matt Arsenault via llvm-commits llvm-commits at lists.llvm.org
Thu May 18 11:45:12 PDT 2023


Author: Matt Arsenault
Date: 2023-05-18T19:44:56+01:00
New Revision: faa32734bf9a55fa3f91d91f6fdf0f8a951a9c0e

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

LOG: SimplifyLibCalls: Pass AssumptionCache to isKnownNeverInfinity

Let's assumes work for determining no infinities.

Added: 
    

Modified: 
    llvm/include/llvm/Transforms/Utils/SimplifyLibCalls.h
    llvm/lib/Analysis/InstructionSimplify.cpp
    llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
    llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
    llvm/test/Transforms/InstCombine/pow-1.ll

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/Transforms/Utils/SimplifyLibCalls.h b/llvm/include/llvm/Transforms/Utils/SimplifyLibCalls.h
index 9269216d06789..eb10545ee149e 100644
--- a/llvm/include/llvm/Transforms/Utils/SimplifyLibCalls.h
+++ b/llvm/include/llvm/Transforms/Utils/SimplifyLibCalls.h
@@ -18,6 +18,7 @@
 #include "llvm/Analysis/TargetLibraryInfo.h"
 
 namespace llvm {
+class AssumptionCache;
 class StringRef;
 class Value;
 class CallInst;
@@ -102,6 +103,7 @@ class LibCallSimplifier {
   FortifiedLibCallSimplifier FortifiedSimplifier;
   const DataLayout &DL;
   const TargetLibraryInfo *TLI;
+  AssumptionCache *AC;
   OptimizationRemarkEmitter &ORE;
   BlockFrequencyInfo *BFI;
   ProfileSummaryInfo *PSI;
@@ -134,9 +136,9 @@ class LibCallSimplifier {
 
 public:
   LibCallSimplifier(
-      const DataLayout &DL, const TargetLibraryInfo *TLI,
-      OptimizationRemarkEmitter &ORE,
-      BlockFrequencyInfo *BFI, ProfileSummaryInfo *PSI,
+      const DataLayout &DL, const TargetLibraryInfo *TLI, AssumptionCache *AC,
+      OptimizationRemarkEmitter &ORE, BlockFrequencyInfo *BFI,
+      ProfileSummaryInfo *PSI,
       function_ref<void(Instruction *, Value *)> Replacer =
           &replaceAllUsesWithDefault,
       function_ref<void(Instruction *)> Eraser = &eraseFromParentDefault);

diff  --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp
index 3b951b9c80766..3d4e96937cd68 100644
--- a/llvm/lib/Analysis/InstructionSimplify.cpp
+++ b/llvm/lib/Analysis/InstructionSimplify.cpp
@@ -4090,7 +4090,7 @@ static Value *simplifyFCmpInst(unsigned Predicate, Value *LHS, Value *RHS,
         return getFalse(RetTy);
       // LHS != Inf && LHS != NaN
       if (Pred == FCmpInst::FCMP_ONE &&
-          isKnownNeverInfinity(LHS, Q.DL, Q.TLI) &&
+          isKnownNeverInfinity(LHS, Q.DL, Q.TLI) && // xxxx fixme
           isKnownNeverNaN(LHS, Q.DL, Q.TLI))
         return getTrue(RetTy);
     }

diff  --git a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
index 2992d163344d9..1d7bf48414a13 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
@@ -3239,7 +3239,7 @@ Instruction *InstCombinerImpl::tryOptimizeCall(CallInst *CI) {
   auto InstCombineErase = [this](Instruction *I) {
     eraseInstFromFunction(*I);
   };
-  LibCallSimplifier Simplifier(DL, &TLI, ORE, BFI, PSI, InstCombineRAUW,
+  LibCallSimplifier Simplifier(DL, &TLI, &AC, ORE, BFI, PSI, InstCombineRAUW,
                                InstCombineErase);
   if (Value *With = Simplifier.optimizeCall(CI, Builder)) {
     ++NumSimplified;

diff  --git a/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp b/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
index 190b17f177a9a..220f0b271d81b 100644
--- a/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
@@ -2179,7 +2179,7 @@ Value *LibCallSimplifier::replacePowWithSqrt(CallInst *Pow, IRBuilderBase &B) {
   // pow(-Inf, 0.5) is optionally required to have a result of +Inf (not setting
   // errno), but sqrt(-Inf) is required by various standards to set errno.
   if (!Pow->doesNotAccessMemory() && !Pow->hasNoInfs() &&
-      !isKnownNeverInfinity(Base, DL, TLI))
+      !isKnownNeverInfinity(Base, DL, TLI, 0, AC, Pow, /*DT=*/nullptr, &ORE))
     return nullptr;
 
   Sqrt = getSqrtCall(Base, AttributeList(), Pow->doesNotAccessMemory(), Mod, B,
@@ -3837,13 +3837,13 @@ Value *LibCallSimplifier::optimizeCall(CallInst *CI, IRBuilderBase &Builder) {
 }
 
 LibCallSimplifier::LibCallSimplifier(
-    const DataLayout &DL, const TargetLibraryInfo *TLI,
-    OptimizationRemarkEmitter &ORE,
-    BlockFrequencyInfo *BFI, ProfileSummaryInfo *PSI,
+    const DataLayout &DL, const TargetLibraryInfo *TLI, AssumptionCache *AC,
+    OptimizationRemarkEmitter &ORE, BlockFrequencyInfo *BFI,
+    ProfileSummaryInfo *PSI,
     function_ref<void(Instruction *, Value *)> Replacer,
     function_ref<void(Instruction *)> Eraser)
-    : FortifiedSimplifier(TLI), DL(DL), TLI(TLI), ORE(ORE), BFI(BFI), PSI(PSI),
-      Replacer(Replacer), Eraser(Eraser) {}
+    : FortifiedSimplifier(TLI), DL(DL), TLI(TLI), AC(AC), ORE(ORE), BFI(BFI),
+      PSI(PSI), Replacer(Replacer), Eraser(Eraser) {}
 
 void LibCallSimplifier::replaceAllUsesWith(Instruction *I, Value *With) {
   // Indirect through the replacer used in this instance.

diff  --git a/llvm/test/Transforms/InstCombine/pow-1.ll b/llvm/test/Transforms/InstCombine/pow-1.ll
index f93d3586d95d2..d1f587f7ce089 100644
--- a/llvm/test/Transforms/InstCombine/pow-1.ll
+++ b/llvm/test/Transforms/InstCombine/pow-1.ll
@@ -20,10 +20,12 @@
 
 declare float @powf(float, float) nounwind readonly
 declare float @llvm.pow.f32(float, float)
+declare float @llvm.fabs.f32(float)
 declare double @pow(double, double) nounwind readonly
 declare double @llvm.pow.f64(double, double)
 declare <2 x float> @llvm.pow.v2f32(<2 x float>, <2 x float>) nounwind readonly
 declare <2 x double> @llvm.pow.v2f64(<2 x double>, <2 x double>) nounwind readonly
+declare void @llvm.assume(i1 noundef)
 
 ; Check pow(1.0, x) -> 1.0.
 
@@ -270,6 +272,72 @@ define float @powf_libcall_half_ninf(float %x) {
   ret float %retval
 }
 
+; Make sure assume works when inferring no infinities
+define float @powf_libcall_half_assume_ninf(float %x) {
+; ANY-LABEL: define float @powf_libcall_half_assume_ninf
+; ANY-SAME: (float [[X:%.*]]) {
+; ANY-NEXT:    [[FABS:%.*]] = call float @llvm.fabs.f32(float [[X]])
+; ANY-NEXT:    [[NOT_INF:%.*]] = fcmp one float [[FABS]], 0x7FF0000000000000
+; ANY-NEXT:    call void @llvm.assume(i1 [[NOT_INF]])
+; ANY-NEXT:    [[SQRTF:%.*]] = call float @sqrtf(float [[X]])
+; ANY-NEXT:    [[ABS:%.*]] = call float @llvm.fabs.f32(float [[SQRTF]])
+; ANY-NEXT:    [[ISINF:%.*]] = fcmp oeq float [[X]], 0xFFF0000000000000
+; ANY-NEXT:    [[RETVAL:%.*]] = select i1 [[ISINF]], float 0x7FF0000000000000, float [[ABS]]
+; ANY-NEXT:    ret float [[RETVAL]]
+;
+; VC32-LABEL: define float @powf_libcall_half_assume_ninf
+; VC32-SAME: (float [[X:%.*]]) {
+; VC32-NEXT:    [[FABS:%.*]] = call float @llvm.fabs.f32(float [[X]])
+; VC32-NEXT:    [[NOT_INF:%.*]] = fcmp one float [[FABS]], 0x7FF0000000000000
+; VC32-NEXT:    call void @llvm.assume(i1 [[NOT_INF]])
+; VC32-NEXT:    [[RETVAL:%.*]] = call float @powf(float [[X]], float 5.000000e-01)
+; VC32-NEXT:    ret float [[RETVAL]]
+;
+; VC51-LABEL: define float @powf_libcall_half_assume_ninf
+; VC51-SAME: (float [[X:%.*]]) {
+; VC51-NEXT:    [[FABS:%.*]] = call float @llvm.fabs.f32(float [[X]])
+; VC51-NEXT:    [[NOT_INF:%.*]] = fcmp one float [[FABS]], 0x7FF0000000000000
+; VC51-NEXT:    call void @llvm.assume(i1 [[NOT_INF]])
+; VC51-NEXT:    [[RETVAL:%.*]] = call float @powf(float [[X]], float 5.000000e-01)
+; VC51-NEXT:    ret float [[RETVAL]]
+;
+; VC64-LABEL: define float @powf_libcall_half_assume_ninf
+; VC64-SAME: (float [[X:%.*]]) {
+; VC64-NEXT:    [[FABS:%.*]] = call float @llvm.fabs.f32(float [[X]])
+; VC64-NEXT:    [[NOT_INF:%.*]] = fcmp one float [[FABS]], 0x7FF0000000000000
+; VC64-NEXT:    call void @llvm.assume(i1 [[NOT_INF]])
+; VC64-NEXT:    [[SQRTF:%.*]] = call float @sqrtf(float [[X]])
+; VC64-NEXT:    [[ABS:%.*]] = call float @llvm.fabs.f32(float [[SQRTF]])
+; VC64-NEXT:    [[ISINF:%.*]] = fcmp oeq float [[X]], 0xFFF0000000000000
+; VC64-NEXT:    [[RETVAL:%.*]] = select i1 [[ISINF]], float 0x7FF0000000000000, float [[ABS]]
+; VC64-NEXT:    ret float [[RETVAL]]
+;
+; VC83-LABEL: define float @powf_libcall_half_assume_ninf
+; VC83-SAME: (float [[X:%.*]]) {
+; VC83-NEXT:    [[FABS:%.*]] = call float @llvm.fabs.f32(float [[X]])
+; VC83-NEXT:    [[NOT_INF:%.*]] = fcmp one float [[FABS]], 0x7FF0000000000000
+; VC83-NEXT:    call void @llvm.assume(i1 [[NOT_INF]])
+; VC83-NEXT:    [[SQRTF:%.*]] = call float @sqrtf(float [[X]])
+; VC83-NEXT:    [[ABS:%.*]] = call float @llvm.fabs.f32(float [[SQRTF]])
+; VC83-NEXT:    [[ISINF:%.*]] = fcmp oeq float [[X]], 0xFFF0000000000000
+; VC83-NEXT:    [[RETVAL:%.*]] = select i1 [[ISINF]], float 0x7FF0000000000000, float [[ABS]]
+; VC83-NEXT:    ret float [[RETVAL]]
+;
+; NOLIB-LABEL: define float @powf_libcall_half_assume_ninf
+; NOLIB-SAME: (float [[X:%.*]]) {
+; NOLIB-NEXT:    [[FABS:%.*]] = call float @llvm.fabs.f32(float [[X]])
+; NOLIB-NEXT:    [[NOT_INF:%.*]] = fcmp one float [[FABS]], 0x7FF0000000000000
+; NOLIB-NEXT:    call void @llvm.assume(i1 [[NOT_INF]])
+; NOLIB-NEXT:    [[RETVAL:%.*]] = call float @powf(float [[X]], float 5.000000e-01)
+; NOLIB-NEXT:    ret float [[RETVAL]]
+;
+  %fabs = call float @llvm.fabs.f32(float %x)
+  %not.inf = fcmp one float %fabs, 0x7FF0000000000000
+  call void @llvm.assume(i1 %not.inf)
+  %retval = call float @powf(float %x, float 0.5)
+  ret float %retval
+}
+
 define float @powf_libcall_half_ninf_tail(float %x) {
 ; CHECK-LABEL: @powf_libcall_half_ninf_tail(
 ; ANY-NEXT:      %sqrtf = call ninf float @sqrtf(float %x)


        


More information about the llvm-commits mailing list