@@ -128,6 +128,8 @@ class ConstantTerminatorFoldingImpl {
128128 // from any other block. So this variable set to true means that loop's latch
129129 // has become unreachable from loop header.
130130 bool DeleteCurrentLoop = false ;
131+ // Whether or not we enter the loop through an indirectbr.
132+ bool HasIndirectEntry = false ;
131133
132134 // The blocks of the original loop that will still be reachable from entry
133135 // after the constant folding.
@@ -216,6 +218,19 @@ class ConstantTerminatorFoldingImpl {
216218 return ;
217219 }
218220
221+ // We need a loop preheader to split in handleDeadExits(). If LoopSimplify
222+ // wasn't able to form one because the loop can be entered through an
223+ // indirectbr we cannot continue.
224+ if (!L.getLoopPreheader ()) {
225+ assert (any_of (predecessors (L.getHeader ()),
226+ [&](BasicBlock *Pred) {
227+ return isa<IndirectBrInst>(Pred->getTerminator ());
228+ }) &&
229+ " Loop should have preheader if it is not entered indirectly" );
230+ HasIndirectEntry = true ;
231+ return ;
232+ }
233+
219234 // Collect live and dead loop blocks and exits.
220235 LiveLoopBlocks.insert (L.getHeader ());
221236 for (auto I = DFS.beginRPO (), E = DFS.endRPO (); I != E; ++I) {
@@ -546,6 +561,12 @@ class ConstantTerminatorFoldingImpl {
546561 return false ;
547562 }
548563
564+ if (HasIndirectEntry) {
565+ LLVM_DEBUG (dbgs () << " Loops which can be entered indirectly are not"
566+ " supported!\n " );
567+ return false ;
568+ }
569+
549570 // Nothing to constant-fold.
550571 if (FoldCandidates.empty ()) {
551572 LLVM_DEBUG (
0 commit comments