Index: llvm/lib/Analysis/IVDescriptors.cpp =================================================================== --- llvm/lib/Analysis/IVDescriptors.cpp +++ llvm/lib/Analysis/IVDescriptors.cpp @@ -1505,6 +1505,13 @@ PHINode *Phi, const Loop *TheLoop, ScalarEvolution *SE, InductionDescriptor &D, const SCEV *Expr, SmallVectorImpl *CastsToIgnore) { + + if (!Phi) + return false; + + if (TheLoop->getHeader() != Phi->getParent()) + return false; + Type *PhiTy = Phi->getType(); // We only handle integer and pointer inductions variables. if (!PhiTy->isIntegerTy() && !PhiTy->isPointerTy()) @@ -1527,6 +1534,8 @@ return false; } + if (!(AR->getLoop()->getLoopPreheader())) + return false; Value *StartValue = Phi->getIncomingValueForBlock(AR->getLoop()->getLoopPreheader()); Index: llvm/lib/Transforms/Utils/LoopUtils.cpp =================================================================== --- llvm/lib/Transforms/Utils/LoopUtils.cpp +++ llvm/lib/Transforms/Utils/LoopUtils.cpp @@ -1243,20 +1243,6 @@ return true; } -/// Checks if it is safe to call InductionDescriptor::isInductionPHI for \p Phi, -/// and returns true if this Phi is an induction phi in the loop. When -/// isInductionPHI returns true, \p ID will be also be set by isInductionPHI. -static bool checkIsIndPhi(PHINode *Phi, Loop *L, ScalarEvolution *SE, - InductionDescriptor &ID) { - if (!Phi) - return false; - if (!L->getLoopPreheader()) - return false; - if (Phi->getParent() != L->getHeader()) - return false; - return InductionDescriptor::isInductionPHI(Phi, L, SE, ID); -} - int llvm::rewriteLoopExitValues(Loop *L, LoopInfo *LI, TargetLibraryInfo *TLI, ScalarEvolution *SE, const TargetTransformInfo *TTI, @@ -1317,7 +1303,7 @@ InductionDescriptor ID; PHINode *IndPhi = dyn_cast(Inst); if (IndPhi) { - if (!checkIsIndPhi(IndPhi, L, SE, ID)) + if (!InductionDescriptor::isInductionPHI(IndPhi, L, SE, ID)) continue; // This is an induction PHI. Check that the only users are PHI // nodes, and induction variable update binary operators. @@ -1338,7 +1324,7 @@ continue; if (llvm::any_of(Inst->users(), [&](User *U) { PHINode *Phi = dyn_cast(U); - if (Phi != PN && !checkIsIndPhi(Phi, L, SE, ID)) + if (Phi != PN && !InductionDescriptor::isInductionPHI(Phi, L, SE, ID)) return true; return false; }))