From 591ba11b938cfdef5407b6b75fe54423839a8fdf Mon Sep 17 00:00:00 2001 From: Matt Arsenault Date: Fri, 19 May 2023 10:25:38 +0100 Subject: [PATCH] Reapply "SimplifyLibCalls: Pass AssumptionCache to isKnownNeverInfinity" This reverts commit b357f379c81811409348dd0e0273a248b055bb7a. --- .../llvm/Transforms/Utils/SimplifyLibCalls.h | 8 ++- .../Transforms/InstCombine/InstCombineCalls.cpp | 2 +- llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp | 12 ++-- llvm/test/Transforms/InstCombine/pow-1.ll | 68 ++++++++++++++++++++++ 4 files changed, 80 insertions(+), 10 deletions(-) diff --git a/llvm/include/llvm/Transforms/Utils/SimplifyLibCalls.h b/llvm/include/llvm/Transforms/Utils/SimplifyLibCalls.h index 9269216..eb10545 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 @@ private: FortifiedLibCallSimplifier FortifiedSimplifier; const DataLayout &DL; const TargetLibraryInfo *TLI; + AssumptionCache *AC; OptimizationRemarkEmitter &ORE; BlockFrequencyInfo *BFI; ProfileSummaryInfo *PSI; @@ -134,9 +136,9 @@ private: 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 Replacer = &replaceAllUsesWithDefault, function_ref Eraser = &eraseFromParentDefault); diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp index 2992d16..1d7bf48 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 190b17f..220f0b2 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 Replacer, function_ref 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 f93d358..d1f587f 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) -- 2.7.4