diff --git a/llvm/lib/Analysis/IVDescriptors.cpp b/llvm/lib/Analysis/IVDescriptors.cpp index d13f2e139ee4a..a88dd6776aa96 100644 --- a/llvm/lib/Analysis/IVDescriptors.cpp +++ b/llvm/lib/Analysis/IVDescriptors.cpp @@ -222,7 +222,9 @@ bool RecurrenceDescriptor::AddReductionVar( // Obtain the reduction start value from the value that comes from the loop // preheader. - Value *RdxStart = Phi->getIncomingValueForBlock(TheLoop->getLoopPreheader()); + Value *RdxStart; + if (TheLoop->getLoopPreheader()) + RdxStart = Phi->getIncomingValueForBlock(TheLoop->getLoopPreheader()); // ExitInstruction is the single value which is used outside the loop. // We only allow for a single reduction value to be used outside the loop. @@ -587,6 +589,9 @@ bool RecurrenceDescriptor::AddReductionVar( collectCastInstrs(TheLoop, ExitInstruction, RecurrenceType, CastInsts, MinWidthCastToRecurrenceType); + if (!RdxStart) + return false; + // We found a reduction var if we have reached the original phi node and we // only have a single instruction with out-of-loop users. @@ -1536,6 +1541,11 @@ bool InductionDescriptor::isInductionPHI( return false; } + if (!AR->getLoop()->getLoopPreheader()) { + LLVM_DEBUG(dbgs() << "LV: Loop doesn't have a legal pre-header.\n"); + return false; + } + // This function assumes that InductionPhi is called only on Phi nodes // present inside loop headers. Check for the same, and throw an assert if // the current Phi is not present inside the loop header. diff --git a/llvm/test/Transforms/LoopVectorize/loop-legality-checks-remarks.ll b/llvm/test/Transforms/LoopVectorize/loop-legality-checks-remarks.ll new file mode 100644 index 0000000000000..a586e0b32593b --- /dev/null +++ b/llvm/test/Transforms/LoopVectorize/loop-legality-checks-remarks.ll @@ -0,0 +1,25 @@ +; RUN: opt < %s -passes=loop-vectorize -pass-remarks=loop-vectorize -debug -disable-output 2>&1 | FileCheck %s +; REQUIRES: asserts + +; Make sure LV legal bails out when the loop doesn't have a legal pre-header. +; CHECK-LABEL: 'not_exist_preheader' +; CHECK: LV: Not vectorizing: Loop doesn't have a legal pre-header. +define void @not_exist_preheader(ptr %currMB, i1 %arg, ptr %arg2) nounwind uwtable { + +start.exit: + indirectbr ptr %arg2, [label %0, label %for.bodyprime] + +0: + unreachable + +for.bodyprime: + %i.057375 = phi i32 [0, %start.exit], [%1, %for.bodyprime] + %arrayidx8prime = getelementptr inbounds i32, ptr %currMB, i32 %i.057375 + store i32 0, ptr %arrayidx8prime, align 4 + %1 = add i32 %i.057375, 1 + %cmp5prime = icmp slt i32 %1, 4 + br i1 %cmp5prime, label %for.bodyprime, label %exit + +exit: + ret void +}