[indvars] Fix lftr crash when preheader is terminated by switch

This was found by oss-fuzz.  The switch will get canonicalized to a branch, but if it hasn't been when we run LFTR, we crashed on an unneeded assert.
This commit is contained in:
Philip Reames 2021-11-23 09:57:30 -08:00
parent c933c2eb33
commit 03d8bc184a
2 changed files with 19 additions and 2 deletions

View File

@ -1951,7 +1951,6 @@ bool IndVarSimplify::run(Loop *L) {
// using it.
if (!DisableLFTR) {
BasicBlock *PreHeader = L->getLoopPreheader();
BranchInst *PreHeaderBR = cast<BranchInst>(PreHeader->getTerminator());
SmallVector<BasicBlock*, 16> ExitingBlocks;
L->getExitingBlocks(ExitingBlocks);
@ -1987,7 +1986,7 @@ bool IndVarSimplify::run(Loop *L) {
// Avoid high cost expansions. Note: This heuristic is questionable in
// that our definition of "high cost" is not exactly principled.
if (Rewriter.isHighCostExpansion(ExitCount, L, SCEVCheapExpansionBudget,
TTI, PreHeaderBR))
TTI, PreHeader->getTerminator()))
continue;
// Check preconditions for proper SCEVExpander operation. SCEV does not

View File

@ -706,3 +706,21 @@ if.end:
declare i32 @llvm.loop.decrement.reg.i32(i32, i32)
; This used to crash, we're basically just checking that it doesn't.
define void @switch_preheader() {
; CHECK-LABEL: @switch_preheader(
; CHECK-NEXT: switch i32 -1, label [[X:%.*]] [
; CHECK-NEXT: ]
; CHECK: x:
; CHECK-NEXT: br i1 false, label [[X]], label [[BB:%.*]]
; CHECK: BB:
; CHECK-NEXT: ret void
;
switch i32 -1, label %x []
x: ; preds = %x, %0
br i1 false, label %x, label %BB
BB: ; preds = %x
ret void
}