Index: lib/Transforms/Scalar/LoopIdiomRecognize.cpp =================================================================== --- lib/Transforms/Scalar/LoopIdiomRecognize.cpp +++ lib/Transforms/Scalar/LoopIdiomRecognize.cpp @@ -554,8 +554,7 @@ // Do a quadratic search on all of the given stores and find // all of the pairs of stores that follow each other. - SmallVector IndexQueue; - for (unsigned i = 0, e = SL.size(); i < e; ++i) { + for (int i = 0, e = SL.size(); i < e; ++i) { assert(SL[i]->isSimple() && "Expected only non-volatile stores."); Value *FirstStoredVal = SL[i]->getValueOperand(); @@ -582,19 +581,29 @@ assert((FirstSplatValue || FirstPatternValue) && "Expected either splat value or pattern value."); - IndexQueue.clear(); // If a store has multiple consecutive store candidates, search Stores - // array according to the sequence: from i+1 to e, then from i-1 to 0. + // array according to the sequence: from [i+1, e), then from [i-1, 0]. // This is because usually pairing with immediate succeeding or preceding // candidate create the best chance to find memset opportunity. - unsigned j = 0; - for (j = i + 1; j < e; ++j) - IndexQueue.push_back(j); - for (j = i; j > 0; --j) - IndexQueue.push_back(j - 1); + // k varies from [i+1,e), [i-1,0] + int delta = 1; + int it = 0; + int k = i+1; + if (k == e) { // [i+1, e) cannot be done. + k = i-1; + delta = -1; + } - for (auto &k : IndexQueue) { + for ( ; k >= 0; k = it) { assert(SL[k]->isSimple() && "Expected only non-volatile stores."); + it = k; + if (it+1 == e) { + it = i; + delta = -1; + } + + it += delta; + Value *SecondStorePtr = SL[k]->getPointerOperand(); const SCEVAddRecExpr *SecondStoreEv = cast(SE->getSCEV(SecondStorePtr));