reduce indentation in LICM::sink by using early exits, use

getUniqueExitBlocks instead of getExitBlocks and a manual
set to eliminate dupes.

llvm-svn: 112405
This commit is contained in:
Chris Lattner 2010-08-29 04:28:20 +00:00
parent 188cc5a0fc
commit cd96b4df56
1 changed files with 92 additions and 89 deletions

View File

@ -460,7 +460,7 @@ void LICM::sink(Instruction &I) {
DEBUG(dbgs() << "LICM sinking instruction: " << I << "\n");
SmallVector<BasicBlock*, 8> ExitBlocks;
CurLoop->getExitBlocks(ExitBlocks);
CurLoop->getUniqueExitBlocks(ExitBlocks);
if (isa<LoadInst>(I)) ++NumMovedLoads;
else if (isa<CallInst>(I)) ++NumMovedCalls;
@ -487,7 +487,10 @@ void LICM::sink(Instruction &I) {
BasicBlock::iterator InsertPt = ExitBlocks[0]->getFirstNonPHI();
ExitBlocks[0]->getInstList().insert(InsertPt, &I);
}
} else if (ExitBlocks.empty()) {
return;
}
if (ExitBlocks.empty()) {
// The instruction is actually dead if there ARE NO exit blocks.
CurAST->deleteValue(&I);
// If I has users in unreachable blocks, eliminate.
@ -496,7 +499,9 @@ void LICM::sink(Instruction &I) {
if (!I.getType()->isVoidTy())
I.replaceAllUsesWith(UndefValue::get(I.getType()));
I.eraseFromParent();
} else {
return;
}
// Otherwise, if we have multiple exits, use the PromoteMem2Reg function to
// do all of the hard work of inserting PHI nodes as necessary. We convert
// the value into a stack object to get it to do this.
@ -545,21 +550,18 @@ void LICM::sink(Instruction &I) {
// Thirdly, insert a copy of the instruction in each exit block of the loop
// that is dominated by the instruction, storing the result into the memory
// location. Be careful not to insert the instruction into any particular
// basic block more than once.
// location. Each exit block is known to only be in the ExitBlocks list once.
SmallPtrSet<BasicBlock*, 16> InsertedBlocks;
BasicBlock *InstOrigBB = I.getParent();
unsigned NumInserted = 0;
for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i) {
BasicBlock *ExitBlock = ExitBlocks[i];
if (!isExitBlockDominatedByBlockInLoop(ExitBlock, InstOrigBB))
continue;
// If we haven't already processed this exit block, do so now.
if (!InsertedBlocks.insert(ExitBlock))
continue;
// Insert the code after the last PHI node...
BasicBlock::iterator InsertPt = ExitBlock->getFirstNonPHI();
@ -567,7 +569,7 @@ void LICM::sink(Instruction &I) {
// instruction, otherwise clone the original instruction and insert
// the copy.
Instruction *New;
if (InsertedBlocks.size() == 1) {
if (NumInserted == 0) {
I.removeFromParent();
ExitBlock->getInstList().insert(InsertPt, &I);
New = &I;
@ -579,12 +581,14 @@ void LICM::sink(Instruction &I) {
ExitBlock->getInstList().insert(InsertPt, New);
}
++NumInserted;
// Now that we have inserted the instruction, store it into the alloca
if (AI) new StoreInst(New, AI, InsertPt);
}
// If the instruction doesn't dominate any exit blocks, it must be dead.
if (InsertedBlocks.empty()) {
if (NumInserted == 0) {
CurAST->deleteValue(&I);
I.eraseFromParent();
}
@ -596,7 +600,6 @@ void LICM::sink(Instruction &I) {
PromoteMemToReg(Allocas, *DT, *DF, CurAST);
}
}
}
/// hoist - When an instruction is found to only use loop invariant operands
/// that is safe to hoist, this instruction is called to do the dirty work.