diff --git a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp index f2241be60ce05..a25bde75b465d 100644 --- a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp +++ b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp @@ -5260,6 +5260,8 @@ LoopVectorizationCostModel::calculateRegisterUsage(ArrayRef VFs) { return TTICapture.getRegUsageForType(VectorType::get(Ty, VF)); }; + collectInLoopReductions(); + for (unsigned int Idx = 0, Sz = IdxToInstr.size(); Idx < Sz; ++Idx) { Instruction *I = IdxToInstr[Idx]; @@ -5276,8 +5278,6 @@ LoopVectorizationCostModel::calculateRegisterUsage(ArrayRef VFs) { if (ValuesToIgnore.count(I)) continue; - collectInLoopReductions(); - // For each VF find the maximum usage of registers. for (unsigned J = 0, E = VFs.size(); J < E; ++J) { // Count the number of registers used, per register class, given all open @@ -7008,6 +7008,10 @@ void LoopVectorizationCostModel::collectValuesToIgnore() { } void LoopVectorizationCostModel::collectInLoopReductions() { + // Avoid duplicating work finding in-loop reductions. + if (!InLoopReductions.empty()) + return; + for (const auto &Reduction : Legal->getReductionVars()) { PHINode *Phi = Reduction.first; const RecurrenceDescriptor &RdxDesc = Reduction.second;