[llvm] 00fdd2c - [LSR] Don't crash on non-branch terminator in -lsr-term-fold
Philip Reames via llvm-commits
llvm-commits at lists.llvm.org
Tue Mar 21 09:36:03 PDT 2023
Author: Philip Reames
Date: 2023-03-21T09:30:01-07:00
New Revision: 00fdd2cb6c55744ae21f18c377854e9359273454
URL: https://github.com/llvm/llvm-project/commit/00fdd2cb6c55744ae21f18c377854e9359273454
DIFF: https://github.com/llvm/llvm-project/commit/00fdd2cb6c55744ae21f18c377854e9359273454.diff
LOG: [LSR] Don't crash on non-branch terminator in -lsr-term-fold
Reported in https://reviews.llvm.org/D146415. I rewrote the patch and aded the test case. Per that report, spec2006.483.xalancbmk crashes without this fix.
Added:
Modified:
llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp
llvm/test/Transforms/LoopStrengthReduce/lsr-term-fold.ll
Removed:
################################################################################
diff --git a/llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp b/llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp
index 3e5e0987e10c..82312de71c72 100644
--- a/llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp
+++ b/llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp
@@ -6700,8 +6700,8 @@ canFoldTermCondOfLoop(Loop *L, ScalarEvolution &SE, DominatorTree &DT,
}
BasicBlock *LoopLatch = L->getLoopLatch();
- BranchInst *BI = cast<BranchInst>(LoopLatch->getTerminator());
- if (BI->isUnconditional())
+ BranchInst *BI = dyn_cast<BranchInst>(LoopLatch->getTerminator());
+ if (!BI || BI->isUnconditional())
return std::nullopt;
auto *TermCond = dyn_cast<ICmpInst>(BI->getCondition());
if (!TermCond) {
diff --git a/llvm/test/Transforms/LoopStrengthReduce/lsr-term-fold.ll b/llvm/test/Transforms/LoopStrengthReduce/lsr-term-fold.ll
index 16e85a94517b..c9b66896eac7 100644
--- a/llvm/test/Transforms/LoopStrengthReduce/lsr-term-fold.ll
+++ b/llvm/test/Transforms/LoopStrengthReduce/lsr-term-fold.ll
@@ -439,3 +439,37 @@ another.branch:
for.end: ; preds = %for.body
ret void
}
+
+
+define void @non_branch_terminator(ptr %a) {
+; CHECK-LABEL: @non_branch_terminator(
+; CHECK-NEXT: entry:
+; CHECK-NEXT: [[UGLYGEP:%.*]] = getelementptr i8, ptr [[A:%.*]], i64 84
+; CHECK-NEXT: br label [[FOR_BODY:%.*]]
+; CHECK: for.body:
+; CHECK-NEXT: [[LSR_IV2:%.*]] = phi i64 [ [[LSR_IV_NEXT3:%.*]], [[FOR_BODY]] ], [ 378, [[ENTRY:%.*]] ]
+; CHECK-NEXT: [[LSR_IV1:%.*]] = phi ptr [ [[UGLYGEP2:%.*]], [[FOR_BODY]] ], [ [[UGLYGEP]], [[ENTRY]] ]
+; CHECK-NEXT: store i32 1, ptr [[LSR_IV1]], align 4
+; CHECK-NEXT: [[UGLYGEP2]] = getelementptr i8, ptr [[LSR_IV1]], i64 4
+; CHECK-NEXT: [[LSR_IV_NEXT3]] = add nsw i64 [[LSR_IV2]], -1
+; CHECK-NEXT: switch i64 [[LSR_IV2]], label [[FOR_BODY]] [
+; CHECK-NEXT: i64 0, label [[FOR_END:%.*]]
+; CHECK-NEXT: ]
+; CHECK: for.end:
+; CHECK-NEXT: ret void
+;
+entry:
+ %uglygep = getelementptr i8, ptr %a, i64 84
+ br label %for.body
+
+for.body: ; preds = %for.body, %entry
+ %lsr.iv1 = phi ptr [ %uglygep2, %for.body ], [ %uglygep, %entry ]
+ %lsr.iv = phi i64 [ %lsr.iv.next, %for.body ], [ 379, %entry ]
+ store i32 1, ptr %lsr.iv1, align 4
+ %lsr.iv.next = add nsw i64 %lsr.iv, -1
+ %uglygep2 = getelementptr i8, ptr %lsr.iv1, i64 4
+ switch i64 %lsr.iv.next, label %for.body [i64 0, label %for.end]
+
+for.end: ; preds = %for.body
+ ret void
+}
More information about the llvm-commits
mailing list