Skip to content

Commit 471217c

Browse files
committed
Revert "Revert "[IndVars] Replace PHIs if loop exits on 1st iteration""
This reverts commit 6fec655. The patch was reverted on incorrect claim that this patch may break LCSSA form when the loop is not in a simplify form. All IndVars' transform insure that the loop is in simplify and LCSSA form, so if it wasn't broken before this transform, it will also not be broken after it.
1 parent 74670e7 commit 471217c

File tree

4 files changed

+21
-15
lines changed

4 files changed

+21
-15
lines changed

llvm/lib/Transforms/Scalar/IndVarSimplify.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1309,6 +1309,17 @@ static void foldExit(const Loop *L, BasicBlock *ExitingBB, bool IsTaken,
13091309
replaceExitCond(BI, NewCond, DeadInsts);
13101310
}
13111311

1312+
static void replaceLoopPHINodesWithPreheaderValues(
1313+
Loop *L, SmallVectorImpl<WeakTrackingVH> &DeadInsts) {
1314+
auto *LoopPreheader = L->getLoopPreheader();
1315+
auto *LoopHeader = L->getHeader();
1316+
for (auto &PN : LoopHeader->phis()) {
1317+
auto *PreheaderIncoming = PN.getIncomingValueForBlock(LoopPreheader);
1318+
PN.replaceAllUsesWith(PreheaderIncoming);
1319+
DeadInsts.emplace_back(&PN);
1320+
}
1321+
}
1322+
13121323
static void replaceWithInvariantCond(
13131324
const Loop *L, BasicBlock *ExitingBB, ICmpInst::Predicate InvariantPred,
13141325
const SCEV *InvariantLHS, const SCEV *InvariantRHS, SCEVExpander &Rewriter,
@@ -1499,10 +1510,11 @@ bool IndVarSimplify::optimizeLoopExits(Loop *L, SCEVExpander &Rewriter) {
14991510
// If we know we'd exit on the first iteration, rewrite the exit to
15001511
// reflect this. This does not imply the loop must exit through this
15011512
// exit; there may be an earlier one taken on the first iteration.
1502-
// TODO: Given we know the backedge can't be taken, we should go ahead
1503-
// and break it. Or at least, kill all the header phis and simplify.
1513+
// We know that the backedge can't be taken, so we replace all
1514+
// the header PHIs with values coming from the preheader.
15041515
if (ExitCount->isZero()) {
15051516
foldExit(L, ExitingBB, true, DeadInsts);
1517+
replaceLoopPHINodesWithPreheaderValues(L, DeadInsts);
15061518
Changed = true;
15071519
continue;
15081520
}

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

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,13 @@ define i1 @kill_backedge_and_phis(i8* align 1 %lhs, i8* align 1 %rhs, i32 %len)
1515
; CHECK: loop_preheader:
1616
; CHECK-NEXT: br label %loop
1717
; CHECK: loop:
18-
; CHECK-NEXT: %iv = phi i32 [ 0, %loop_preheader ], [ %iv.next, %exiting_3 ]
19-
; CHECK-NEXT: %iv.wide = phi i64 [ 0, %loop_preheader ], [ %iv.wide.next, %exiting_3 ]
20-
; CHECK-NEXT: %iv.next = add nuw nsw i32 %iv, 1
21-
; CHECK-NEXT: %iv.wide.next = add nuw nsw i64 %iv.wide, 1
22-
; CHECK-NEXT: %left_ptr = getelementptr inbounds i8, i8* %lhs, i32 %iv
23-
; CHECK-NEXT: %right_ptr = getelementptr inbounds i8, i8* %rhs, i32 %iv
18+
; CHECK-NEXT: %iv.next = add nuw nsw i32 0, 1
19+
; CHECK-NEXT: %left_ptr = getelementptr inbounds i8, i8* %lhs, i32 0
20+
; CHECK-NEXT: %right_ptr = getelementptr inbounds i8, i8* %rhs, i32 0
2421
; CHECK-NEXT: %result = call i1 @foo(i8* %left_ptr, i8* %right_ptr)
2522
; CHECK-NEXT: br i1 %result, label %exiting_1, label %exit.loopexit
2623
; CHECK: exiting_1:
27-
; CHECK-NEXT: %iv.wide.is_not_zero = icmp ne i64 %iv.wide, 0
24+
; CHECK-NEXT: %iv.wide.is_not_zero = icmp ne i64 0, 0
2825
; CHECK-NEXT: br i1 false, label %exiting_2, label %exit.loopexit
2926
; CHECK: exiting_2:
3027
; CHECK-NEXT: %bar_ret = call i1 @bar()

llvm/test/Transforms/IndVarSimplify/eliminate-exit-no-dl.ll

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,9 @@ define void @foo() {
1414
; CHECK-NEXT: bb:
1515
; CHECK-NEXT: br label [[BB3:%.*]]
1616
; CHECK: bb3:
17-
; CHECK-NEXT: [[TMP:%.*]] = phi i8* [ [[TMP4:%.*]], [[BB7:%.*]] ], [ getelementptr inbounds ([0 x i8], [0 x i8]* @global, i64 0, i64 2), [[BB:%.*]] ]
18-
; CHECK-NEXT: [[TMP4]] = getelementptr inbounds i8, i8* [[TMP]], i64 -1
17+
; CHECK-NEXT: [[TMP4:%.*]] = getelementptr inbounds i8, i8* getelementptr inbounds ([0 x i8], [0 x i8]* @global, i64 0, i64 2), i64 -1
1918
; CHECK-NEXT: [[TMP6:%.*]] = load i8, i8* [[TMP4]], align 1
20-
; CHECK-NEXT: br i1 false, label [[BB7]], label [[BB11:%.*]]
19+
; CHECK-NEXT: br i1 false, label [[BB7:%.*]], label [[BB11:%.*]]
2120
; CHECK: bb7:
2221
; CHECK-NEXT: [[TMP8:%.*]] = zext i8 [[TMP6]] to i64
2322
; CHECK-NEXT: br i1 true, label [[BB11]], label [[BB3]]

llvm/test/Transforms/IndVarSimplify/floating-point-iv.ll

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,7 @@ define void @test3() nounwind {
6565
; CHECK-NEXT: entry:
6666
; CHECK-NEXT: br label [[BB:%.*]]
6767
; CHECK: bb:
68-
; CHECK-NEXT: [[IV:%.*]] = phi double [ 0.000000e+00, [[ENTRY:%.*]] ], [ [[TMP1:%.*]], [[BB]] ]
69-
; CHECK-NEXT: [[TMP0:%.*]] = tail call i32 @foo(double [[IV]]) #[[ATTR0]]
70-
; CHECK-NEXT: [[TMP1]] = fadd double [[IV]], 1.000000e+00
68+
; CHECK-NEXT: [[TMP0:%.*]] = tail call i32 @foo(double 0.000000e+00) #[[ATTR0]]
7169
; CHECK-NEXT: br i1 false, label [[BB]], label [[RETURN:%.*]]
7270
; CHECK: return:
7371
; CHECK-NEXT: ret void

0 commit comments

Comments
 (0)