diff --git a/llvm/lib/Transforms/Scalar/LoopRotation.cpp b/llvm/lib/Transforms/Scalar/LoopRotation.cpp index 2606805400fd..4d1234905370 100644 --- a/llvm/lib/Transforms/Scalar/LoopRotation.cpp +++ b/llvm/lib/Transforms/Scalar/LoopRotation.cpp @@ -535,6 +535,8 @@ bool LoopRotate::rotateLoop(Loop *L, bool SimplifiedLatch) { Loop *PredLoop = LI->getLoopFor(*PI); if (!PredLoop || PredLoop->contains(Exit)) continue; + if (isa((*PI)->getTerminator())) + continue; SplitLatchEdge |= L->getLoopLatch() == *PI; BasicBlock *ExitSplit = SplitCriticalEdge( *PI, Exit, CriticalEdgeSplittingOptions(DT, LI).setPreserveLCSSA()); diff --git a/llvm/test/Transforms/LoopRotate/crash.ll b/llvm/test/Transforms/LoopRotate/crash.ll index fd922cb5569e..e95f9a1f350f 100644 --- a/llvm/test/Transforms/LoopRotate/crash.ll +++ b/llvm/test/Transforms/LoopRotate/crash.ll @@ -153,3 +153,21 @@ entry: "5": ; preds = %"3", %entry ret void } + +; PR21968 +define void @test8(i1 %C, i8* %P) #0 { +entry: + br label %for.cond + +for.cond: ; preds = %for.inc, %entry + br i1 %C, label %l_bad, label %for.body + +for.body: ; preds = %for.cond + indirectbr i8* %P, [label %for.inc, label %l_bad] + +for.inc: ; preds = %for.body + br label %for.cond + +l_bad: ; preds = %for.body, %for.cond + ret void +}