Index: llvm/lib/Transforms/Utils/LoopUtils.cpp =================================================================== --- llvm/lib/Transforms/Utils/LoopUtils.cpp +++ llvm/lib/Transforms/Utils/LoopUtils.cpp @@ -1246,6 +1246,19 @@ return true; } +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; + if (!InductionDescriptor::isInductionPHI(Phi, L, SE, ID)) + return false; + return true; +} + int llvm::rewriteLoopExitValues(Loop *L, LoopInfo *LI, TargetLibraryInfo *TLI, ScalarEvolution *SE, const TargetTransformInfo *TTI, @@ -1306,10 +1319,7 @@ InductionDescriptor ID; PHINode *IndPhi = dyn_cast(Inst); if (IndPhi) { - if (IndPhi->getParent() != L->getHeader()) - continue; - // Do not consider non induction phis. - if (!InductionDescriptor::isInductionPHI(IndPhi, L, SE, ID)) + if (!checkIsIndPhi(IndPhi, L, SE, ID)) continue; // This is an induction PHI. Check that the only users are PHI // nodes, and induction variable update binary operators. @@ -1330,12 +1340,8 @@ continue; if (llvm::any_of(Inst->users(), [&](User *U) { PHINode *Phi = dyn_cast(U); - if (!Phi) + if (Phi != PN && !checkIsIndPhi(Phi, L, SE, ID)) return true; - if (Phi->getParent() == L->getHeader()) { - if (!InductionDescriptor::isInductionPHI(Phi, L, SE, ID)) - return true; - } return false; })) continue;