diff --git a/lib/Transforms/Vectorize/LoopVectorize.cpp b/lib/Transforms/Vectorize/LoopVectorize.cpp index 7f77784d282..fe73cd99062 100644 --- a/lib/Transforms/Vectorize/LoopVectorize.cpp +++ b/lib/Transforms/Vectorize/LoopVectorize.cpp @@ -2055,6 +2055,41 @@ Value *createMinMaxOp(IRBuilder<> &Builder, return Select; } +///\brief Perform cse of induction variable instructions. +static void cse(BasicBlock *BB) { + // Perform simple cse. + SmallPtrSet Visited; + SmallVector ToRemove; + for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) { + Instruction *In = I; + + if (!isa(In) && !isa(In) && + !isa(In) && !isa(In)) + continue; + + // Check if we can replace this instruction with any of the + // visited instructions. + for (SmallPtrSet::iterator v = Visited.begin(), + ve = Visited.end(); v != ve; ++v) { + if (In->isIdenticalTo(*v)) { + In->replaceAllUsesWith(*v); + ToRemove.push_back(In); + In = 0; + break; + } + } + if (In) + Visited.insert(In); + + } + // Erase all of the instructions that we RAUWed. + for (SmallVectorImpl::iterator v = ToRemove.begin(), + ve = ToRemove.end(); v != ve; ++v) { + assert((*v)->getNumUses() == 0 && "Can't remove instructions with uses"); + (*v)->eraseFromParent(); + } +} + void InnerLoopVectorizer::vectorizeLoop(LoopVectorizationLegality *Legal) { //===------------------------------------------------===// @@ -2275,38 +2310,8 @@ InnerLoopVectorizer::vectorizeLoop(LoopVectorizationLegality *Legal) { fixLCSSAPHIs(); - // Perform simple cse. - SmallPtrSet Visited; - SmallVector ToRemove; - for (BasicBlock::iterator I = LoopVectorBody->begin(), - E = LoopVectorBody->end(); I != E; ++I) { - Instruction *In = I; - - if (!isa(In) && !isa(In) && - !isa(In) && !isa(In)) - continue; - - // Check if we can replace this instruction with any of the - // visited instructions. - for (SmallPtrSet::iterator v = Visited.begin(), - ve = Visited.end(); v != ve; ++v) { - if (In->isIdenticalTo(*v)) { - In->replaceAllUsesWith(*v); - ToRemove.push_back(In); - In = 0; - break; - } - } - if (In) - Visited.insert(In); - - } - // Erase all of the instructions that we RAUWed. - for (SmallVectorImpl::iterator v = ToRemove.begin(), - ve = ToRemove.end(); v != ve; ++v) { - assert((*v)->getNumUses() == 0 && "Can't remove instructions with uses"); - (*v)->eraseFromParent(); - } + // Remove redundant induction instructions. + cse(LoopVectorBody); } void InnerLoopVectorizer::fixLCSSAPHIs() {