Index: lib/Transforms/Utils/LoopUnroll.cpp =================================================================== --- lib/Transforms/Utils/LoopUnroll.cpp +++ lib/Transforms/Utils/LoopUnroll.cpp @@ -321,10 +321,16 @@ BasicBlock *Header = L->getHeader(); BranchInst *BI = dyn_cast(LatchBlock->getTerminator()); - if (!BI || BI->isUnconditional()) { - // The loop-rotate pass can be helpful to avoid this in many cases. - DEBUG(dbgs() << - " Can't unroll; loop not terminated by a conditional branch.\n"); + // If the loop is not rotated the latch is not one of the exiting blocks. + // Bail out on non-rotated loops. + SmallVector ExitingBlocks; + L->getExitingBlocks(ExitingBlocks); + bool NotRotated = + llvm::all_of(ExitingBlocks, [&LatchBlock](const BasicBlock *BB) { + return LatchBlock != BB; + }); + if (NotRotated) { + DEBUG(dbgs() << " Can't unroll; loop not rotated\n"); return false; } Index: test/Transforms/LoopUnroll/not-rotated.ll =================================================================== --- /dev/null +++ test/Transforms/LoopUnroll/not-rotated.ll @@ -0,0 +1,25 @@ +; PR28103 +; Don't unroll non-rotated loops. Those are not currently +; properly handled by LoopUnroll. + +; RUN: opt -loop-unroll -verify-dom-info %s +; REQUIRE: asserts + +define void @tinkywinky(i1 %patatino) { +entry: + br label %header1 +header1: + %indvars.iv = phi i64 [ 1, %body2 ], [ 0, %entry ] + %exitcond = icmp ne i64 %indvars.iv, 1 + br i1 %exitcond, label %body1, label %exit +body1: + br i1 %patatino, label %body2, label %sink +body2: + br i1 %patatino, label %header1, label %body3 +body3: + br label %sink +sink: + br label %body2 +exit: + ret void +}