Skip to content

Commit 453fdeb

Browse files
committed
[indvars] Extend canonicalizeExitConditions to inverted operands
As discussed in the original reviews, but done in a follow on.
1 parent eb0fa8b commit 453fdeb

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

llvm/lib/Transforms/Scalar/IndVarSimplify.cpp

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1439,8 +1439,12 @@ bool IndVarSimplify::canonicalizeExitCondition(Loop *L) {
14391439
// For the range reasoning, avoid computing SCEVs in the loop to avoid
14401440
// poisoning cache with sub-optimal results. For the must-execute case,
14411441
// this is a neccessary precondition for correctness.
1442-
if (!L->isLoopInvariant(RHS))
1443-
continue;
1442+
if (!L->isLoopInvariant(RHS)) {
1443+
if (!L->isLoopInvariant(LHS))
1444+
continue;
1445+
// Same logic applies for the inverse case
1446+
std::swap(LHS, RHS);
1447+
}
14441448

14451449
// Match (icmp signed-cond zext, RHS)
14461450
Value *LHSOp = nullptr;
@@ -1475,11 +1479,19 @@ bool IndVarSimplify::canonicalizeExitCondition(Loop *L) {
14751479
if (!ICmp || !ICmp->hasOneUse() || !ICmp->isUnsigned())
14761480
continue;
14771481

1482+
bool Swapped = false;
14781483
auto *LHS = ICmp->getOperand(0);
14791484
auto *RHS = ICmp->getOperand(1);
1480-
if (L->isLoopInvariant(LHS) || !L->isLoopInvariant(RHS))
1485+
if (L->isLoopInvariant(LHS) == L->isLoopInvariant(RHS))
14811486
// Nothing to rotate
14821487
continue;
1488+
if (L->isLoopInvariant(LHS)) {
1489+
// Same logic applies for the inverse case until we actually pick
1490+
// which operand of the compare to update.
1491+
Swapped = true;
1492+
std::swap(LHS, RHS);
1493+
}
1494+
assert(!L->isLoopInvariant(LHS) && L->isLoopInvariant(RHS));
14831495

14841496
if (!LHS->hasOneUse())
14851497
// Can't rotate without increasing instruction count
@@ -1501,8 +1513,8 @@ bool IndVarSimplify::canonicalizeExitCondition(Loop *L) {
15011513
auto *NewRHS =
15021514
CastInst::Create(Instruction::Trunc, RHS, LHSOp->getType(), "",
15031515
L->getLoopPreheader()->getTerminator());
1504-
ICmp->setOperand(0, LHSOp);
1505-
ICmp->setOperand(1, NewRHS);
1516+
ICmp->setOperand(Swapped ? 1 : 0, LHSOp);
1517+
ICmp->setOperand(Swapped ? 0 : 1, NewRHS);
15061518
if (LHS->use_empty())
15071519
DeadInsts.push_back(LHS);
15081520
};

llvm/test/Transforms/IndVarSimplify/finite-exit-comparisons.ll

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -824,12 +824,12 @@ for.end: ; preds = %for.body, %entry
824824
define void @slt_constant_lhs(i16 %n.raw, i8 %start) mustprogress {
825825
; CHECK-LABEL: @slt_constant_lhs(
826826
; CHECK-NEXT: entry:
827+
; CHECK-NEXT: [[TMP0:%.*]] = trunc i16 254 to i8
827828
; CHECK-NEXT: br label [[FOR_BODY:%.*]]
828829
; CHECK: for.body:
829830
; CHECK-NEXT: [[IV:%.*]] = phi i8 [ [[IV_NEXT:%.*]], [[FOR_BODY]] ], [ [[START:%.*]], [[ENTRY:%.*]] ]
830831
; CHECK-NEXT: [[IV_NEXT]] = add i8 [[IV]], 1
831-
; CHECK-NEXT: [[ZEXT:%.*]] = zext i8 [[IV_NEXT]] to i16
832-
; CHECK-NEXT: [[CMP:%.*]] = icmp slt i16 254, [[ZEXT]]
832+
; CHECK-NEXT: [[CMP:%.*]] = icmp ult i8 [[TMP0]], [[IV_NEXT]]
833833
; CHECK-NEXT: br i1 [[CMP]], label [[FOR_BODY]], label [[FOR_END:%.*]]
834834
; CHECK: for.end:
835835
; CHECK-NEXT: ret void
@@ -878,12 +878,12 @@ for.end: ; preds = %for.body, %entry
878878
define void @ult_constant_lhs(i16 %n.raw, i8 %start) mustprogress {
879879
; CHECK-LABEL: @ult_constant_lhs(
880880
; CHECK-NEXT: entry:
881+
; CHECK-NEXT: [[TMP0:%.*]] = trunc i16 254 to i8
881882
; CHECK-NEXT: br label [[FOR_BODY:%.*]]
882883
; CHECK: for.body:
883884
; CHECK-NEXT: [[IV:%.*]] = phi i8 [ [[IV_NEXT:%.*]], [[FOR_BODY]] ], [ [[START:%.*]], [[ENTRY:%.*]] ]
884885
; CHECK-NEXT: [[IV_NEXT]] = add i8 [[IV]], 1
885-
; CHECK-NEXT: [[ZEXT:%.*]] = zext i8 [[IV_NEXT]] to i16
886-
; CHECK-NEXT: [[CMP:%.*]] = icmp ult i16 254, [[ZEXT]]
886+
; CHECK-NEXT: [[CMP:%.*]] = icmp ult i8 [[TMP0]], [[IV_NEXT]]
887887
; CHECK-NEXT: br i1 [[CMP]], label [[FOR_BODY]], label [[FOR_END:%.*]]
888888
; CHECK: for.end:
889889
; CHECK-NEXT: ret void

0 commit comments

Comments
 (0)