diff --git a/llvm/lib/Transforms/Scalar/LoopInterchange.cpp b/llvm/lib/Transforms/Scalar/LoopInterchange.cpp --- a/llvm/lib/Transforms/Scalar/LoopInterchange.cpp +++ b/llvm/lib/Transforms/Scalar/LoopInterchange.cpp @@ -661,15 +661,12 @@ // Check V's users to see if it is involved in a reduction in L. static PHINode *findInnerReductionPhi(Loop *L, Value *V) { - for (Value *User : V->users()) { - if (PHINode *PHI = dyn_cast(User)) { - if (PHI->getNumIncomingValues() == 1) - continue; - RecurrenceDescriptor RD; - if (RecurrenceDescriptor::isReductionPHI(PHI, L, RD)) - return PHI; - return nullptr; - } + for (PHINode &PHI : L->getHeader()->phis()) { + if (PHI.getNumIncomingValues() == 1 || + !llvm::is_contained(PHI.incoming_values(), V)) + continue; + RecurrenceDescriptor RD; + return RecurrenceDescriptor::isReductionPHI(&PHI, L, RD) ? &PHI : nullptr; } return nullptr;