Skip to content

Commit 8fab9f8

Browse files
committed
[IndVars] Sharpen context in eliminateIVComparison
When eliminating comparisons, we can use common dominator of all its users as context. This gives better results when ICMP is not computed right before the branch that uses it. Differential Revision: https://reviews.llvm.org/D98924 Reviewed By: lebedev.ri
1 parent fc36a51 commit 8fab9f8

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
lines changed

llvm/lib/Transforms/Utils/SimplifyIndVar.cpp

+5-2
Original file line numberDiff line numberDiff line change
@@ -281,8 +281,11 @@ void SimplifyIndvar::eliminateIVComparison(ICmpInst *ICmp, Value *IVOperand) {
281281

282282
// If the condition is always true or always false in the given context,
283283
// replace it with a constant value.
284-
// TODO: We can sharpen the context to common dominator of all ICmp's users.
285-
if (auto Ev = SE->evaluatePredicateAt(Pred, S, X, ICmp)) {
284+
SmallVector<Instruction *, 4> Users;
285+
for (auto *U : ICmp->users())
286+
Users.push_back(cast<Instruction>(U));
287+
const Instruction *CtxI = findCommonDominator(Users, *DT);
288+
if (auto Ev = SE->evaluatePredicateAt(Pred, S, X, CtxI)) {
286289
ICmp->replaceAllUsesWith(ConstantInt::getBool(ICmp->getContext(), *Ev));
287290
DeadInsts.emplace_back(ICmp);
288291
LLVM_DEBUG(dbgs() << "INDVARS: Eliminated comparison: " << *ICmp << '\n');

llvm/test/Transforms/IndVarSimplify/eliminate-comparison.ll

+2-4
Original file line numberDiff line numberDiff line change
@@ -1101,13 +1101,11 @@ define i32 @func_28(i32 %start) {
11011101
; CHECK: loop:
11021102
; CHECK-NEXT: [[IV:%.*]] = phi i32 [ [[START:%.*]], [[ENTRY:%.*]] ], [ [[IV_NEXT:%.*]], [[BACKEDGE:%.*]] ]
11031103
; CHECK-NEXT: [[C1:%.*]] = icmp ne i32 [[IV]], 0
1104-
; CHECK-NEXT: [[C2:%.*]] = icmp ne i32 [[IV]], 0
1105-
; CHECK-NEXT: [[C3:%.*]] = icmp ne i32 [[IV]], 0
11061104
; CHECK-NEXT: br i1 [[C1]], label [[CHECKED_1:%.*]], label [[FAIL:%.*]]
11071105
; CHECK: checked.1:
1108-
; CHECK-NEXT: br i1 [[C2]], label [[CHECKED_2:%.*]], label [[FAIL]]
1106+
; CHECK-NEXT: br i1 true, label [[CHECKED_2:%.*]], label [[FAIL]]
11091107
; CHECK: checked.2:
1110-
; CHECK-NEXT: br i1 [[C3]], label [[BACKEDGE]], label [[FAIL]]
1108+
; CHECK-NEXT: br i1 true, label [[BACKEDGE]], label [[FAIL]]
11111109
; CHECK: backedge:
11121110
; CHECK-NEXT: [[IV_NEXT]] = add i32 [[IV]], 758394
11131111
; CHECK-NEXT: [[LOOP_COND:%.*]] = call i1 @cond_func()

0 commit comments

Comments
 (0)