Skip to content

Commit c0e308b

Browse files
authored
[InstCombine] Pass DomTree and DomTreeCacheto LibCallSimplifier (#108446)
This allows any combines to pick up Known states from dominating conditions.
1 parent 520ddf2 commit c0e308b

File tree

4 files changed

+37
-20
lines changed

4 files changed

+37
-20
lines changed

llvm/include/llvm/Transforms/Utils/SimplifyLibCalls.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ class AssumptionCache;
2222
class StringRef;
2323
class Value;
2424
class CallInst;
25+
class DominatorTree;
26+
class DomConditionCache;
2527
class DataLayout;
2628
class Instruction;
2729
class IRBuilderBase;
@@ -103,6 +105,8 @@ class LibCallSimplifier {
103105
FortifiedLibCallSimplifier FortifiedSimplifier;
104106
const DataLayout &DL;
105107
const TargetLibraryInfo *TLI;
108+
DominatorTree *DT;
109+
DomConditionCache *DC;
106110
AssumptionCache *AC;
107111
OptimizationRemarkEmitter &ORE;
108112
BlockFrequencyInfo *BFI;
@@ -136,7 +140,8 @@ class LibCallSimplifier {
136140

137141
public:
138142
LibCallSimplifier(
139-
const DataLayout &DL, const TargetLibraryInfo *TLI, AssumptionCache *AC,
143+
const DataLayout &DL, const TargetLibraryInfo *TLI, DominatorTree *DT,
144+
DomConditionCache *DC, AssumptionCache *AC,
140145
OptimizationRemarkEmitter &ORE, BlockFrequencyInfo *BFI,
141146
ProfileSummaryInfo *PSI,
142147
function_ref<void(Instruction *, Value *)> Replacer =

llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3711,8 +3711,8 @@ Instruction *InstCombinerImpl::tryOptimizeCall(CallInst *CI) {
37113711
auto InstCombineErase = [this](Instruction *I) {
37123712
eraseInstFromFunction(*I);
37133713
};
3714-
LibCallSimplifier Simplifier(DL, &TLI, &AC, ORE, BFI, PSI, InstCombineRAUW,
3715-
InstCombineErase);
3714+
LibCallSimplifier Simplifier(DL, &TLI, &DT, &DC, &AC, ORE, BFI, PSI,
3715+
InstCombineRAUW, InstCombineErase);
37163716
if (Value *With = Simplifier.optimizeCall(CI, Builder)) {
37173717
++NumSimplified;
37183718
return CI->use_empty() ? CI : replaceInstUsesWith(*CI, With);

llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2278,8 +2278,8 @@ Value *LibCallSimplifier::replacePowWithSqrt(CallInst *Pow, IRBuilderBase &B) {
22782278
// pow(-Inf, 0.5) is optionally required to have a result of +Inf (not setting
22792279
// errno), but sqrt(-Inf) is required by various standards to set errno.
22802280
if (!Pow->doesNotAccessMemory() && !Pow->hasNoInfs() &&
2281-
!isKnownNeverInfinity(Base, 0,
2282-
SimplifyQuery(DL, TLI, /*DT=*/nullptr, AC, Pow)))
2281+
!isKnownNeverInfinity(
2282+
Base, 0, SimplifyQuery(DL, TLI, DT, AC, Pow, true, true, DC)))
22832283
return nullptr;
22842284

22852285
Sqrt = getSqrtCall(Base, AttributeList(), Pow->doesNotAccessMemory(), Mod, B,
@@ -4162,13 +4162,13 @@ Value *LibCallSimplifier::optimizeCall(CallInst *CI, IRBuilderBase &Builder) {
41624162
}
41634163

41644164
LibCallSimplifier::LibCallSimplifier(
4165-
const DataLayout &DL, const TargetLibraryInfo *TLI, AssumptionCache *AC,
4166-
OptimizationRemarkEmitter &ORE, BlockFrequencyInfo *BFI,
4167-
ProfileSummaryInfo *PSI,
4165+
const DataLayout &DL, const TargetLibraryInfo *TLI, DominatorTree *DT,
4166+
DomConditionCache *DC, AssumptionCache *AC, OptimizationRemarkEmitter &ORE,
4167+
BlockFrequencyInfo *BFI, ProfileSummaryInfo *PSI,
41684168
function_ref<void(Instruction *, Value *)> Replacer,
41694169
function_ref<void(Instruction *)> Eraser)
4170-
: FortifiedSimplifier(TLI), DL(DL), TLI(TLI), AC(AC), ORE(ORE), BFI(BFI),
4171-
PSI(PSI), Replacer(Replacer), Eraser(Eraser) {}
4170+
: FortifiedSimplifier(TLI), DL(DL), TLI(TLI), DT(DT), DC(DC), AC(AC),
4171+
ORE(ORE), BFI(BFI), PSI(PSI), Replacer(Replacer), Eraser(Eraser) {}
41724172

41734173
void LibCallSimplifier::replaceAllUsesWith(Instruction *I, Value *With) {
41744174
// Indirect through the replacer used in this instance.

llvm/test/Transforms/InstCombine/pow-1.ll

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -863,16 +863,28 @@ define double @pow_libcall_half_no_FMF(double %x) {
863863
}
864864

865865
define double @pow_libcall_half_fromdomcondition(double %x) {
866-
; CHECK-LABEL: define double @pow_libcall_half_fromdomcondition(
867-
; CHECK-SAME: double [[X:%.*]]) {
868-
; CHECK-NEXT: [[A:%.*]] = call double @llvm.fabs.f64(double [[X]])
869-
; CHECK-NEXT: [[C:%.*]] = fcmp oeq double [[A]], 0x7FF0000000000000
870-
; CHECK-NEXT: br i1 [[C]], label [[THEN:%.*]], label [[ELSE:%.*]]
871-
; CHECK: then:
872-
; CHECK-NEXT: ret double 0.000000e+00
873-
; CHECK: else:
874-
; CHECK-NEXT: [[RETVAL:%.*]] = call double @pow(double [[X]], double 5.000000e-01)
875-
; CHECK-NEXT: ret double [[RETVAL]]
866+
; LIB-LABEL: define double @pow_libcall_half_fromdomcondition(
867+
; LIB-SAME: double [[X:%.*]]) {
868+
; LIB-NEXT: [[A:%.*]] = call double @llvm.fabs.f64(double [[X]])
869+
; LIB-NEXT: [[C:%.*]] = fcmp oeq double [[A]], 0x7FF0000000000000
870+
; LIB-NEXT: br i1 [[C]], label [[THEN:%.*]], label [[ELSE:%.*]]
871+
; LIB: then:
872+
; LIB-NEXT: ret double 0.000000e+00
873+
; LIB: else:
874+
; LIB-NEXT: [[SQRT:%.*]] = call double @sqrt(double [[X]])
875+
; LIB-NEXT: [[ABS:%.*]] = call double @llvm.fabs.f64(double [[SQRT]])
876+
; LIB-NEXT: ret double [[ABS]]
877+
;
878+
; NOLIB-LABEL: define double @pow_libcall_half_fromdomcondition(
879+
; NOLIB-SAME: double [[X:%.*]]) {
880+
; NOLIB-NEXT: [[A:%.*]] = call double @llvm.fabs.f64(double [[X]])
881+
; NOLIB-NEXT: [[C:%.*]] = fcmp oeq double [[A]], 0x7FF0000000000000
882+
; NOLIB-NEXT: br i1 [[C]], label [[THEN:%.*]], label [[ELSE:%.*]]
883+
; NOLIB: then:
884+
; NOLIB-NEXT: ret double 0.000000e+00
885+
; NOLIB: else:
886+
; NOLIB-NEXT: [[RETVAL:%.*]] = call double @pow(double [[X]], double 5.000000e-01)
887+
; NOLIB-NEXT: ret double [[RETVAL]]
876888
;
877889
%a = call double @llvm.fabs.f64(double %x)
878890
%c = fcmp oeq double %a, 0x7FF0000000000000

0 commit comments

Comments
 (0)