From 86dc6c08cf72668b433ad207cf42cdd9a8fe822a Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Tue, 30 Nov 2010 21:18:46 +0000 Subject: [PATCH] move call handling in handleEndBlock up a bit, and simplify it. If the call might read all the allocas, stop scanning early. Convert a vector to smallvector, shrink SmallPtrSet to 16 instead of 64 to avoid crazy linear scans. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@120463 91177308-0d34-0410-b5e6-96231b3b80d8 --- .../Scalar/DeadStoreElimination.cpp | 77 ++++++++++--------- 1 file changed, 40 insertions(+), 37 deletions(-) diff --git a/lib/Transforms/Scalar/DeadStoreElimination.cpp b/lib/Transforms/Scalar/DeadStoreElimination.cpp index 5d55aa43e58..b03d092faef 100644 --- a/lib/Transforms/Scalar/DeadStoreElimination.cpp +++ b/lib/Transforms/Scalar/DeadStoreElimination.cpp @@ -66,9 +66,9 @@ namespace { bool handleEndBlock(BasicBlock &BB); bool RemoveUndeadPointers(Value *Ptr, uint64_t killPointerSize, BasicBlock::iterator &BBI, - SmallPtrSet &deadPointers); + SmallPtrSet &deadPointers); void DeleteDeadInstruction(Instruction *I, - SmallPtrSet *deadPointers = 0); + SmallPtrSet *deadPointers = 0); // getAnalysisUsage - We require post dominance frontiers (aka Control @@ -400,7 +400,7 @@ bool DSE::handleEndBlock(BasicBlock &BB) { bool MadeChange = false; // Pointers alloca'd in this function are dead in the end block - SmallPtrSet DeadPointers; + SmallPtrSet DeadPointers; // Find all of the alloca'd pointers in the entry block. BasicBlock *Entry = BB.getParent()->begin(); @@ -450,42 +450,26 @@ bool DSE::handleEndBlock(BasicBlock &BB) { continue; } - - Value *KillPointer = 0; - uint64_t KillPointerSize = AliasAnalysis::UnknownSize; - - // If we encounter a use of the pointer, it is no longer considered dead - if (LoadInst *L = dyn_cast(BBI)) { - KillPointer = L->getPointerOperand(); - } else if (VAArgInst *V = dyn_cast(BBI)) { - KillPointer = V->getOperand(0); - } else if (MemTransferInst *MTI = dyn_cast(BBI)) { - KillPointer = cast(BBI)->getSource(); - if (ConstantInt *Len = dyn_cast(MTI->getLength())) - KillPointerSize = Len->getZExtValue(); - } else if (CallSite CS = cast(BBI)) { + if (CallSite CS = cast(BBI)) { // If this call does not access memory, it can't be loading any of our // pointers. if (AA->doesNotAccessMemory(CS)) continue; - unsigned NumModRef = 0; - unsigned NumOther = 0; + unsigned NumModRef = 0, NumOther = 0; - // Remove any pointers made undead by the call from the dead set - std::vector dead; - for (SmallPtrSet::iterator I = DeadPointers.begin(), + // If the call might load from any of our allocas, then any store above + // the call is live. + SmallVector LiveAllocas; + for (SmallPtrSet::iterator I = DeadPointers.begin(), E = DeadPointers.end(); I != E; ++I) { - // HACK: if we detect that our AA is imprecise, it's not - // worth it to scan the rest of the deadPointers set. Just - // assume that the AA will return ModRef for everything, and - // go ahead and bail. - if (NumModRef >= 16 && NumOther == 0) { - DeadPointers.clear(); + // If we detect that our AA is imprecise, it's not worth it to scan the + // rest of the DeadPointers set. Just assume that the AA will return + // ModRef for everything, and go ahead and bail out. + if (NumModRef >= 16 && NumOther == 0) return MadeChange; - } - - // See if the call site touches it + + // See if the call site touches it. AliasAnalysis::ModRefResult A = AA->getModRefInfo(CS, *I, getPointerSize(*I, *AA)); @@ -495,14 +479,33 @@ bool DSE::handleEndBlock(BasicBlock &BB) { ++NumOther; if (A == AliasAnalysis::ModRef || A == AliasAnalysis::Ref) - dead.push_back(*I); + LiveAllocas.push_back(*I); } - - for (std::vector::iterator I = dead.begin(), E = dead.end(); - I != E; ++I) + + for (SmallVector::iterator I = LiveAllocas.begin(), + E = LiveAllocas.end(); I != E; ++I) DeadPointers.erase(*I); + // If all of the allocas were clobbered by the call then we're not going + // to find anything else to process. + if (DeadPointers.empty()) + return MadeChange; + continue; + } + + Value *KillPointer = 0; + uint64_t KillPointerSize = AliasAnalysis::UnknownSize; + + // If we encounter a use of the pointer, it is no longer considered dead + if (LoadInst *L = dyn_cast(BBI)) { + KillPointer = L->getPointerOperand(); + } else if (VAArgInst *V = dyn_cast(BBI)) { + KillPointer = V->getOperand(0); + } else if (MemTransferInst *MTI = dyn_cast(BBI)) { + KillPointer = cast(BBI)->getSource(); + if (ConstantInt *Len = dyn_cast(MTI->getLength())) + KillPointerSize = Len->getZExtValue(); } else { // Not a loading instruction. continue; @@ -522,7 +525,7 @@ bool DSE::handleEndBlock(BasicBlock &BB) { /// undead when scanning for dead stores to alloca's. bool DSE::RemoveUndeadPointers(Value *killPointer, uint64_t killPointerSize, BasicBlock::iterator &BBI, - SmallPtrSet &DeadPointers) { + SmallPtrSet &DeadPointers) { // If the kill pointer can be easily reduced to an alloca, // don't bother doing extraneous AA queries. if (DeadPointers.count(killPointer)) { @@ -575,7 +578,7 @@ bool DSE::RemoveUndeadPointers(Value *killPointer, uint64_t killPointerSize, /// If ValueSet is non-null, remove any deleted instructions from it as well. /// void DSE::DeleteDeadInstruction(Instruction *I, - SmallPtrSet *ValueSet) { + SmallPtrSet *ValueSet) { SmallVector NowDeadInsts; NowDeadInsts.push_back(I); -- 2.34.1