Skip to content

[InstCombine] Pass DomTree and DomTreeCacheto LibCallSimplifier #108446

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 13, 2024

Conversation

davemgreen
Copy link
Collaborator

This allows any combines to pick up Known states from dominating conditions.

This allows any combines to pick up Known states from dominating conditions.
@llvmbot
Copy link
Member

llvmbot commented Sep 12, 2024

@llvm/pr-subscribers-llvm-transforms

Author: David Green (davemgreen)

Changes

This allows any combines to pick up Known states from dominating conditions.


Full diff: https://github.com/llvm/llvm-project/pull/108446.diff

4 Files Affected:

  • (modified) llvm/include/llvm/Transforms/Utils/SimplifyLibCalls.h (+6-1)
  • (modified) llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp (+2-2)
  • (modified) llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp (+7-7)
  • (modified) llvm/test/Transforms/InstCombine/pow-1.ll (+22-10)
diff --git a/llvm/include/llvm/Transforms/Utils/SimplifyLibCalls.h b/llvm/include/llvm/Transforms/Utils/SimplifyLibCalls.h
index 43b5c9250a8908..2e7a0ec29ed999 100644
--- a/llvm/include/llvm/Transforms/Utils/SimplifyLibCalls.h
+++ b/llvm/include/llvm/Transforms/Utils/SimplifyLibCalls.h
@@ -22,6 +22,8 @@ class AssumptionCache;
 class StringRef;
 class Value;
 class CallInst;
+class DominatorTree;
+class DomConditionCache;
 class DataLayout;
 class Instruction;
 class IRBuilderBase;
@@ -103,6 +105,8 @@ class LibCallSimplifier {
   FortifiedLibCallSimplifier FortifiedSimplifier;
   const DataLayout &DL;
   const TargetLibraryInfo *TLI;
+  DominatorTree *DT;
+  DomConditionCache *DC;
   AssumptionCache *AC;
   OptimizationRemarkEmitter &ORE;
   BlockFrequencyInfo *BFI;
@@ -136,7 +140,8 @@ class LibCallSimplifier {
 
 public:
   LibCallSimplifier(
-      const DataLayout &DL, const TargetLibraryInfo *TLI, AssumptionCache *AC,
+      const DataLayout &DL, const TargetLibraryInfo *TLI, DominatorTree *DT,
+      DomConditionCache *DC, AssumptionCache *AC,
       OptimizationRemarkEmitter &ORE, BlockFrequencyInfo *BFI,
       ProfileSummaryInfo *PSI,
       function_ref<void(Instruction *, Value *)> Replacer =
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
index eb94e894b57b06..61011d55227e7b 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
@@ -3711,8 +3711,8 @@ Instruction *InstCombinerImpl::tryOptimizeCall(CallInst *CI) {
   auto InstCombineErase = [this](Instruction *I) {
     eraseInstFromFunction(*I);
   };
-  LibCallSimplifier Simplifier(DL, &TLI, &AC, ORE, BFI, PSI, InstCombineRAUW,
-                               InstCombineErase);
+  LibCallSimplifier Simplifier(DL, &TLI, &DT, &DC, &AC, ORE, BFI, PSI,
+                               InstCombineRAUW, InstCombineErase);
   if (Value *With = Simplifier.optimizeCall(CI, Builder)) {
     ++NumSimplified;
     return CI->use_empty() ? CI : replaceInstUsesWith(*CI, With);
diff --git a/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp b/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
index 1e6dc88ed93532..917f81863cf673 100644
--- a/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
@@ -2278,8 +2278,8 @@ 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, 0,
-                            SimplifyQuery(DL, TLI, /*DT=*/nullptr, AC, Pow)))
+      !isKnownNeverInfinity(
+          Base, 0, SimplifyQuery(DL, TLI, DT, AC, Pow, true, true, DC)))
     return nullptr;
 
   Sqrt = getSqrtCall(Base, AttributeList(), Pow->doesNotAccessMemory(), Mod, B,
@@ -4162,13 +4162,13 @@ Value *LibCallSimplifier::optimizeCall(CallInst *CI, IRBuilderBase &Builder) {
 }
 
 LibCallSimplifier::LibCallSimplifier(
-    const DataLayout &DL, const TargetLibraryInfo *TLI, AssumptionCache *AC,
-    OptimizationRemarkEmitter &ORE, BlockFrequencyInfo *BFI,
-    ProfileSummaryInfo *PSI,
+    const DataLayout &DL, const TargetLibraryInfo *TLI, DominatorTree *DT,
+    DomConditionCache *DC, 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), AC(AC), ORE(ORE), BFI(BFI),
-      PSI(PSI), Replacer(Replacer), Eraser(Eraser) {}
+    : FortifiedSimplifier(TLI), DL(DL), TLI(TLI), DT(DT), DC(DC), 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 f4bbd3eb46bf79..91e909aa25c7e3 100644
--- a/llvm/test/Transforms/InstCombine/pow-1.ll
+++ b/llvm/test/Transforms/InstCombine/pow-1.ll
@@ -863,16 +863,28 @@ define double @pow_libcall_half_no_FMF(double %x) {
 }
 
 define double @pow_libcall_half_fromdomcondition(double %x) {
-; CHECK-LABEL: define double @pow_libcall_half_fromdomcondition(
-; CHECK-SAME: double [[X:%.*]]) {
-; CHECK-NEXT:    [[A:%.*]] = call double @llvm.fabs.f64(double [[X]])
-; CHECK-NEXT:    [[C:%.*]] = fcmp oeq double [[A]], 0x7FF0000000000000
-; CHECK-NEXT:    br i1 [[C]], label [[THEN:%.*]], label [[ELSE:%.*]]
-; CHECK:       then:
-; CHECK-NEXT:    ret double 0.000000e+00
-; CHECK:       else:
-; CHECK-NEXT:    [[RETVAL:%.*]] = call double @pow(double [[X]], double 5.000000e-01)
-; CHECK-NEXT:    ret double [[RETVAL]]
+; LIB-LABEL: define double @pow_libcall_half_fromdomcondition(
+; LIB-SAME: double [[X:%.*]]) {
+; LIB-NEXT:    [[A:%.*]] = call double @llvm.fabs.f64(double [[X]])
+; LIB-NEXT:    [[C:%.*]] = fcmp oeq double [[A]], 0x7FF0000000000000
+; LIB-NEXT:    br i1 [[C]], label [[THEN:%.*]], label [[ELSE:%.*]]
+; LIB:       then:
+; LIB-NEXT:    ret double 0.000000e+00
+; LIB:       else:
+; LIB-NEXT:    [[SQRT:%.*]] = call double @sqrt(double [[X]])
+; LIB-NEXT:    [[ABS:%.*]] = call double @llvm.fabs.f64(double [[SQRT]])
+; LIB-NEXT:    ret double [[ABS]]
+;
+; NOLIB-LABEL: define double @pow_libcall_half_fromdomcondition(
+; NOLIB-SAME: double [[X:%.*]]) {
+; NOLIB-NEXT:    [[A:%.*]] = call double @llvm.fabs.f64(double [[X]])
+; NOLIB-NEXT:    [[C:%.*]] = fcmp oeq double [[A]], 0x7FF0000000000000
+; NOLIB-NEXT:    br i1 [[C]], label [[THEN:%.*]], label [[ELSE:%.*]]
+; NOLIB:       then:
+; NOLIB-NEXT:    ret double 0.000000e+00
+; NOLIB:       else:
+; NOLIB-NEXT:    [[RETVAL:%.*]] = call double @pow(double [[X]], double 5.000000e-01)
+; NOLIB-NEXT:    ret double [[RETVAL]]
 ;
   %a = call double @llvm.fabs.f64(double %x)
   %c = fcmp oeq double %a, 0x7FF0000000000000

@davemgreen davemgreen merged commit c0e308b into llvm:main Sep 13, 2024
10 checks passed
@davemgreen davemgreen deleted the gh-libcalldomtree branch September 13, 2024 07:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants