Skip to content

Commit d82f0b7

Browse files
committed
[IndVars] Don't assume backedge value is instruction (PR64891)
In degenerate cases, the backedge value can be folded to poison. Fixes #64891.
1 parent 1de568d commit d82f0b7

File tree

2 files changed

+36
-7
lines changed

2 files changed

+36
-7
lines changed

llvm/lib/Transforms/Utils/SimplifyIndVar.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1944,13 +1944,15 @@ PHINode *WidenIV::createWideIV(SCEVExpander &Rewriter) {
19441944
// SCEVExpander. Henceforth, we produce 1-to-1 narrow to wide uses.
19451945
if (BasicBlock *LatchBlock = L->getLoopLatch()) {
19461946
WideInc =
1947-
cast<Instruction>(WidePhi->getIncomingValueForBlock(LatchBlock));
1948-
WideIncExpr = SE->getSCEV(WideInc);
1949-
// Propagate the debug location associated with the original loop increment
1950-
// to the new (widened) increment.
1951-
auto *OrigInc =
1952-
cast<Instruction>(OrigPhi->getIncomingValueForBlock(LatchBlock));
1953-
WideInc->setDebugLoc(OrigInc->getDebugLoc());
1947+
dyn_cast<Instruction>(WidePhi->getIncomingValueForBlock(LatchBlock));
1948+
if (WideInc) {
1949+
WideIncExpr = SE->getSCEV(WideInc);
1950+
// Propagate the debug location associated with the original loop
1951+
// increment to the new (widened) increment.
1952+
auto *OrigInc =
1953+
cast<Instruction>(OrigPhi->getIncomingValueForBlock(LatchBlock));
1954+
WideInc->setDebugLoc(OrigInc->getDebugLoc());
1955+
}
19541956
}
19551957

19561958
LLVM_DEBUG(dbgs() << "Wide IV: " << *WidePhi << "\n");
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 2
2+
; RUN: opt -S -passes=indvars < %s | FileCheck %s
3+
4+
target datalayout = "n8:16:32:64"
5+
6+
; Just make sure this doesn't crash.
7+
; SCEVExpander produces a degenerate phi node for the widened IV here,
8+
; where the "increment" instruction folds to a poison value.
9+
define i32 @main() {
10+
; CHECK-LABEL: define i32 @main() {
11+
; CHECK-NEXT: entry:
12+
; CHECK-NEXT: br label [[LOOP:%.*]]
13+
; CHECK: loop:
14+
; CHECK-NEXT: br label [[LOOP]]
15+
;
16+
entry:
17+
%div = sdiv i32 1, 0
18+
%trunc = trunc i32 %div to i16
19+
br label %loop
20+
21+
loop:
22+
%phi = phi i16 [ 0, %entry ], [ %or, %loop ]
23+
%or = or i16 %phi, %trunc
24+
%phi.ext = sext i16 %phi to i64
25+
%add.ptr = getelementptr i8, ptr null, i64 %phi.ext
26+
br label %loop
27+
}

0 commit comments

Comments
 (0)