Skip to content

Commit a053b90

Browse files
committed
Don't keep simplified value unless recurison depth is 1.
Add a test for that case.
1 parent d9f2861 commit a053b90

File tree

2 files changed

+45
-4
lines changed

2 files changed

+45
-4
lines changed

llvm/lib/Analysis/InlineCost.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1741,10 +1741,13 @@ bool CallAnalyzer::simplifyCmpInstForRecCall(CmpInst &Cmp) {
17411741
cast<CmpInst>(&Cmp), {CallArg, Cmp.getOperand(1)}, SQ);
17421742
if (auto *ConstVal = dyn_cast_or_null<ConstantInt>(SimplifiedInstruction)) {
17431743
bool IsTrueSuccessor = CallBB == Br->getSuccessor(0);
1744-
SimplifiedValues[&Cmp] = ConstVal;
1745-
if (ConstVal->isOne())
1746-
return !IsTrueSuccessor;
1747-
return IsTrueSuccessor;
1744+
// Make sure that the BB of the recursive call is NOT the next successor
1745+
// of the icmp. In other words, make sure that the recursion depth is 1.
1746+
if ((ConstVal->isOne() && !IsTrueSuccessor) ||
1747+
(ConstVal->isZero() && IsTrueSuccessor)) {
1748+
SimplifiedValues[&Cmp] = ConstVal;
1749+
return true;
1750+
}
17481751
}
17491752
}
17501753
return false;

llvm/test/Transforms/Inline/inline-recursive-fn.ll

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,3 +153,41 @@ if.end: ; preds = %entry
153153
%mul = fmul float %x, %scale
154154
br label %common.ret18
155155
}
156+
157+
; Test when the icmp can be simplified but the recurison depth is NOT 1,
158+
; so the recursive call will not be inlined.
159+
define float @no_inline_rec_depth_not_1(float %x, float %scale) {
160+
; CHECK-LABEL: define float @no_inline_rec_depth_not_1(
161+
; CHECK-SAME: float [[X:%.*]], float [[SCALE:%.*]]) {
162+
; CHECK-NEXT: [[ENTRY:.*:]]
163+
; CHECK-NEXT: [[CMP:%.*]] = fcmp olt float [[X]], 0.000000e+00
164+
; CHECK-NEXT: br i1 [[CMP]], label %[[IF_THEN:.*]], label %[[IF_END:.*]]
165+
; CHECK: [[COMMON_RET18:.*]]:
166+
; CHECK-NEXT: [[COMMON_RET18_OP:%.*]] = phi float [ [[CALL:%.*]], %[[IF_THEN]] ], [ [[MUL:%.*]], %[[IF_END]] ]
167+
; CHECK-NEXT: ret float [[COMMON_RET18_OP]]
168+
; CHECK: [[IF_THEN]]:
169+
; CHECK-NEXT: [[CALL]] = tail call float @no_inline_rec_depth_not_1(float [[X]], float [[SCALE]])
170+
; CHECK-NEXT: br label %[[COMMON_RET18]]
171+
; CHECK: [[IF_END]]:
172+
; CHECK-NEXT: [[MUL]] = fmul float [[X]], [[SCALE]]
173+
; CHECK-NEXT: br label %[[COMMON_RET18]]
174+
;
175+
entry:
176+
%cmp = fcmp olt float %x, 0.000000e+00
177+
br i1 %cmp, label %if.then, label %if.end
178+
179+
common.ret18: ; preds = %if.then, %if.end
180+
%common.ret18.op = phi float [ %call, %if.then ], [ %mul, %if.end ]
181+
ret float %common.ret18.op
182+
183+
if.then: ; preds = %entry
184+
%fneg1 = fneg float %x
185+
%fneg = fneg float %fneg1
186+
%call = tail call float @no_inline_rec_depth_not_1(float %fneg, float %scale)
187+
br label %common.ret18
188+
189+
if.end: ; preds = %entry
190+
%mul = fmul float %x, %scale
191+
br label %common.ret18
192+
}
193+

0 commit comments

Comments
 (0)