LLVM API Documentation
00001 //===- DeadStoreElimination.cpp - Fast Dead Store Elimination -------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file implements a trivial dead store elimination that only considers 00011 // basic-block local redundant stores. 00012 // 00013 // FIXME: This should eventually be extended to be a post-dominator tree 00014 // traversal. Doing so would be pretty trivial. 00015 // 00016 //===----------------------------------------------------------------------===// 00017 00018 #define DEBUG_TYPE "dse" 00019 #include "llvm/Transforms/Scalar.h" 00020 #include "llvm/Constants.h" 00021 #include "llvm/Function.h" 00022 #include "llvm/Instructions.h" 00023 #include "llvm/IntrinsicInst.h" 00024 #include "llvm/Pass.h" 00025 #include "llvm/ADT/SetVector.h" 00026 #include "llvm/ADT/SmallPtrSet.h" 00027 #include "llvm/ADT/Statistic.h" 00028 #include "llvm/Analysis/AliasAnalysis.h" 00029 #include "llvm/Analysis/Dominators.h" 00030 #include "llvm/Analysis/MemoryDependenceAnalysis.h" 00031 #include "llvm/Target/TargetData.h" 00032 #include "llvm/Transforms/Utils/Local.h" 00033 #include "llvm/Support/Compiler.h" 00034 using namespace llvm; 00035 00036 STATISTIC(NumFastStores, "Number of stores deleted"); 00037 STATISTIC(NumFastOther , "Number of other instrs removed"); 00038 00039 namespace { 00040 struct VISIBILITY_HIDDEN DSE : public FunctionPass { 00041 static char ID; // Pass identification, replacement for typeid 00042 DSE() : FunctionPass(&ID) {} 00043 00044 virtual bool runOnFunction(Function &F) { 00045 bool Changed = false; 00046 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) 00047 Changed |= runOnBasicBlock(*I); 00048 return Changed; 00049 } 00050 00051 bool runOnBasicBlock(BasicBlock &BB); 00052 bool handleFreeWithNonTrivialDependency(FreeInst* F, 00053 Instruction* dependency, 00054 SetVector<Instruction*>& possiblyDead); 00055 bool handleEndBlock(BasicBlock& BB, SetVector<Instruction*>& possiblyDead); 00056 bool RemoveUndeadPointers(Value* pointer, uint64_t killPointerSize, 00057 BasicBlock::iterator& BBI, 00058 SmallPtrSet<Value*, 64>& deadPointers, 00059 SetVector<Instruction*>& possiblyDead); 00060 void DeleteDeadInstructionChains(Instruction *I, 00061 SetVector<Instruction*> &DeadInsts); 00062 00063 00064 // getAnalysisUsage - We require post dominance frontiers (aka Control 00065 // Dependence Graph) 00066 virtual void getAnalysisUsage(AnalysisUsage &AU) const { 00067 AU.setPreservesCFG(); 00068 AU.addRequired<DominatorTree>(); 00069 AU.addRequired<TargetData>(); 00070 AU.addRequired<AliasAnalysis>(); 00071 AU.addRequired<MemoryDependenceAnalysis>(); 00072 AU.addPreserved<DominatorTree>(); 00073 AU.addPreserved<AliasAnalysis>(); 00074 AU.addPreserved<MemoryDependenceAnalysis>(); 00075 } 00076 }; 00077 } 00078 00079 char DSE::ID = 0; 00080 static RegisterPass<DSE> X("dse", "Dead Store Elimination"); 00081 00082 FunctionPass *llvm::createDeadStoreEliminationPass() { return new DSE(); } 00083 00084 bool DSE::runOnBasicBlock(BasicBlock &BB) { 00085 MemoryDependenceAnalysis& MD = getAnalysis<MemoryDependenceAnalysis>(); 00086 TargetData &TD = getAnalysis<TargetData>(); 00087 00088 // Record the last-seen store to this pointer 00089 DenseMap<Value*, StoreInst*> lastStore; 00090 // Record instructions possibly made dead by deleting a store 00091 SetVector<Instruction*> possiblyDead; 00092 00093 bool MadeChange = false; 00094 00095 // Do a top-down walk on the BB 00096 for (BasicBlock::iterator BBI = BB.begin(), BBE = BB.end(); 00097 BBI != BBE; ++BBI) { 00098 // If we find a store or a free... 00099 if (!isa<StoreInst>(BBI) && !isa<FreeInst>(BBI)) 00100 continue; 00101 00102 Value* pointer = 0; 00103 if (StoreInst* S = dyn_cast<StoreInst>(BBI)) { 00104 if (S->isVolatile()) 00105 continue; 00106 pointer = S->getPointerOperand(); 00107 } else { 00108 pointer = cast<FreeInst>(BBI)->getPointerOperand(); 00109 } 00110 00111 pointer = pointer->stripPointerCasts(); 00112 StoreInst*& last = lastStore[pointer]; 00113 bool deletedStore = false; 00114 00115 // ... to a pointer that has been stored to before... 00116 if (last) { 00117 Instruction* dep = MD.getDependency(BBI); 00118 00119 // ... and no other memory dependencies are between them.... 00120 while (dep != MemoryDependenceAnalysis::None && 00121 dep != MemoryDependenceAnalysis::NonLocal && 00122 isa<StoreInst>(dep)) { 00123 if (dep != last || 00124 TD.getTypeStoreSize(last->getOperand(0)->getType()) > 00125 TD.getTypeStoreSize(BBI->getOperand(0)->getType())) { 00126 dep = MD.getDependency(BBI, dep); 00127 continue; 00128 } 00129 00130 // Remove it! 00131 MD.removeInstruction(last); 00132 00133 // DCE instructions only used to calculate that store 00134 if (Instruction* D = dyn_cast<Instruction>(last->getOperand(0))) 00135 possiblyDead.insert(D); 00136 if (Instruction* D = dyn_cast<Instruction>(last->getOperand(1))) 00137 possiblyDead.insert(D); 00138 00139 last->eraseFromParent(); 00140 NumFastStores++; 00141 deletedStore = true; 00142 MadeChange = true; 00143 00144 break; 00145 } 00146 } 00147 00148 // Handle frees whose dependencies are non-trivial. 00149 if (FreeInst* F = dyn_cast<FreeInst>(BBI)) { 00150 if (!deletedStore) 00151 MadeChange |= handleFreeWithNonTrivialDependency(F, 00152 MD.getDependency(F), 00153 possiblyDead); 00154 // No known stores after the free 00155 last = 0; 00156 } else { 00157 StoreInst* S = cast<StoreInst>(BBI); 00158 00159 // If we're storing the same value back to a pointer that we just 00160 // loaded from, then the store can be removed; 00161 if (LoadInst* L = dyn_cast<LoadInst>(S->getOperand(0))) { 00162 Instruction* dep = MD.getDependency(S); 00163 DominatorTree& DT = getAnalysis<DominatorTree>(); 00164 00165 if (!S->isVolatile() && S->getParent() == L->getParent() && 00166 S->getPointerOperand() == L->getPointerOperand() && 00167 ( dep == MemoryDependenceAnalysis::None || 00168 dep == MemoryDependenceAnalysis::NonLocal || 00169 DT.dominates(dep, L))) { 00170 if (Instruction* D = dyn_cast<Instruction>(S->getOperand(0))) 00171 possiblyDead.insert(D); 00172 if (Instruction* D = dyn_cast<Instruction>(S->getOperand(1))) 00173 possiblyDead.insert(D); 00174 00175 // Avoid iterator invalidation. 00176 BBI--; 00177 00178 MD.removeInstruction(S); 00179 S->eraseFromParent(); 00180 NumFastStores++; 00181 MadeChange = true; 00182 } else 00183 // Update our most-recent-store map. 00184 last = S; 00185 } else 00186 // Update our most-recent-store map. 00187 last = S; 00188 } 00189 } 00190 00191 // If this block ends in a return, unwind, unreachable, and eventually 00192 // tailcall, then all allocas are dead at its end. 00193 if (BB.getTerminator()->getNumSuccessors() == 0) 00194 MadeChange |= handleEndBlock(BB, possiblyDead); 00195 00196 // Do a trivial DCE 00197 while (!possiblyDead.empty()) { 00198 Instruction *I = possiblyDead.back(); 00199 possiblyDead.pop_back(); 00200 DeleteDeadInstructionChains(I, possiblyDead); 00201 } 00202 00203 return MadeChange; 00204 } 00205 00206 /// handleFreeWithNonTrivialDependency - Handle frees of entire structures whose 00207 /// dependency is a store to a field of that structure 00208 bool DSE::handleFreeWithNonTrivialDependency(FreeInst* F, Instruction* dep, 00209 SetVector<Instruction*>& possiblyDead) { 00210 TargetData &TD = getAnalysis<TargetData>(); 00211 AliasAnalysis &AA = getAnalysis<AliasAnalysis>(); 00212 MemoryDependenceAnalysis& MD = getAnalysis<MemoryDependenceAnalysis>(); 00213 00214 if (dep == MemoryDependenceAnalysis::None || 00215 dep == MemoryDependenceAnalysis::NonLocal) 00216 return false; 00217 00218 StoreInst* dependency = dyn_cast<StoreInst>(dep); 00219 if (!dependency) 00220 return false; 00221 else if (dependency->isVolatile()) 00222 return false; 00223 00224 Value* depPointer = dependency->getPointerOperand(); 00225 const Type* depType = dependency->getOperand(0)->getType(); 00226 unsigned depPointerSize = TD.getTypeStoreSize(depType); 00227 00228 // Check for aliasing 00229 AliasAnalysis::AliasResult A = AA.alias(F->getPointerOperand(), ~0U, 00230 depPointer, depPointerSize); 00231 00232 if (A == AliasAnalysis::MustAlias) { 00233 // Remove it! 00234 MD.removeInstruction(dependency); 00235 00236 // DCE instructions only used to calculate that store 00237 if (Instruction* D = dyn_cast<Instruction>(dependency->getOperand(0))) 00238 possiblyDead.insert(D); 00239 if (Instruction* D = dyn_cast<Instruction>(dependency->getOperand(1))) 00240 possiblyDead.insert(D); 00241 00242 dependency->eraseFromParent(); 00243 NumFastStores++; 00244 return true; 00245 } 00246 00247 return false; 00248 } 00249 00250 /// handleEndBlock - Remove dead stores to stack-allocated locations in the 00251 /// function end block. Ex: 00252 /// %A = alloca i32 00253 /// ... 00254 /// store i32 1, i32* %A 00255 /// ret void 00256 bool DSE::handleEndBlock(BasicBlock& BB, 00257 SetVector<Instruction*>& possiblyDead) { 00258 TargetData &TD = getAnalysis<TargetData>(); 00259 AliasAnalysis &AA = getAnalysis<AliasAnalysis>(); 00260 MemoryDependenceAnalysis& MD = getAnalysis<MemoryDependenceAnalysis>(); 00261 00262 bool MadeChange = false; 00263 00264 // Pointers alloca'd in this function are dead in the end block 00265 SmallPtrSet<Value*, 64> deadPointers; 00266 00267 // Find all of the alloca'd pointers in the entry block 00268 BasicBlock *Entry = BB.getParent()->begin(); 00269 for (BasicBlock::iterator I = Entry->begin(), E = Entry->end(); I != E; ++I) 00270 if (AllocaInst *AI = dyn_cast<AllocaInst>(I)) 00271 deadPointers.insert(AI); 00272 for (Function::arg_iterator AI = BB.getParent()->arg_begin(), 00273 AE = BB.getParent()->arg_end(); AI != AE; ++AI) 00274 if (AI->hasByValAttr()) 00275 deadPointers.insert(AI); 00276 00277 // Scan the basic block backwards 00278 for (BasicBlock::iterator BBI = BB.end(); BBI != BB.begin(); ){ 00279 --BBI; 00280 00281 // If we find a store whose pointer is dead... 00282 if (StoreInst* S = dyn_cast<StoreInst>(BBI)) { 00283 if (!S->isVolatile()) { 00284 // See through pointer-to-pointer bitcasts 00285 Value* pointerOperand = S->getPointerOperand()->getUnderlyingObject(); 00286 00287 // Alloca'd pointers or byval arguments (which are functionally like 00288 // alloca's) are valid candidates for removal. 00289 if (deadPointers.count(pointerOperand)) { 00290 // Remove it! 00291 MD.removeInstruction(S); 00292 00293 // DCE instructions only used to calculate that store 00294 if (Instruction* D = dyn_cast<Instruction>(S->getOperand(0))) 00295 possiblyDead.insert(D); 00296 if (Instruction* D = dyn_cast<Instruction>(S->getOperand(1))) 00297 possiblyDead.insert(D); 00298 00299 BBI++; 00300 MD.removeInstruction(S); 00301 S->eraseFromParent(); 00302 NumFastStores++; 00303 MadeChange = true; 00304 } 00305 } 00306 00307 continue; 00308 00309 // We can also remove memcpy's to local variables at the end of a function 00310 } else if (MemCpyInst* M = dyn_cast<MemCpyInst>(BBI)) { 00311 Value* dest = M->getDest()->getUnderlyingObject(); 00312 00313 if (deadPointers.count(dest)) { 00314 MD.removeInstruction(M); 00315 00316 // DCE instructions only used to calculate that memcpy 00317 if (Instruction* D = dyn_cast<Instruction>(M->getRawSource())) 00318 possiblyDead.insert(D); 00319 if (Instruction* D = dyn_cast<Instruction>(M->getLength())) 00320 possiblyDead.insert(D); 00321 if (Instruction* D = dyn_cast<Instruction>(M->getRawDest())) 00322 possiblyDead.insert(D); 00323 00324 BBI++; 00325 M->eraseFromParent(); 00326 NumFastOther++; 00327 MadeChange = true; 00328 00329 continue; 00330 } 00331 00332 // Because a memcpy is also a load, we can't skip it if we didn't remove it 00333 } 00334 00335 Value* killPointer = 0; 00336 uint64_t killPointerSize = ~0UL; 00337 00338 // If we encounter a use of the pointer, it is no longer considered dead 00339 if (LoadInst* L = dyn_cast<LoadInst>(BBI)) { 00340 // However, if this load is unused and not volatile, we can go ahead and 00341 // remove it, and not have to worry about it making our pointer undead! 00342 if (L->use_empty() && !L->isVolatile()) { 00343 MD.removeInstruction(L); 00344 00345 // DCE instructions only used to calculate that load 00346 if (Instruction* D = dyn_cast<Instruction>(L->getPointerOperand())) 00347 possiblyDead.insert(D); 00348 00349 BBI++; 00350 L->eraseFromParent(); 00351 NumFastOther++; 00352 MadeChange = true; 00353 possiblyDead.remove(L); 00354 00355 continue; 00356 } 00357 00358 killPointer = L->getPointerOperand(); 00359 } else if (VAArgInst* V = dyn_cast<VAArgInst>(BBI)) { 00360 killPointer = V->getOperand(0); 00361 } else if (isa<MemCpyInst>(BBI) && 00362 isa<ConstantInt>(cast<MemCpyInst>(BBI)->getLength())) { 00363 killPointer = cast<MemCpyInst>(BBI)->getSource(); 00364 killPointerSize = cast<ConstantInt>( 00365 cast<MemCpyInst>(BBI)->getLength())->getZExtValue(); 00366 } else if (AllocaInst* A = dyn_cast<AllocaInst>(BBI)) { 00367 deadPointers.erase(A); 00368 00369 // Dead alloca's can be DCE'd when we reach them 00370 if (A->use_empty()) { 00371 MD.removeInstruction(A); 00372 00373 // DCE instructions only used to calculate that load 00374 if (Instruction* D = dyn_cast<Instruction>(A->getArraySize())) 00375 possiblyDead.insert(D); 00376 00377 BBI++; 00378 A->eraseFromParent(); 00379 NumFastOther++; 00380 MadeChange = true; 00381 possiblyDead.remove(A); 00382 } 00383 00384 continue; 00385 } else if (CallSite::get(BBI).getInstruction() != 0) { 00386 // If this call does not access memory, it can't 00387 // be undeadifying any of our pointers. 00388 CallSite CS = CallSite::get(BBI); 00389 if (AA.doesNotAccessMemory(CS)) 00390 continue; 00391 00392 unsigned modRef = 0; 00393 unsigned other = 0; 00394 00395 // Remove any pointers made undead by the call from the dead set 00396 std::vector<Value*> dead; 00397 for (SmallPtrSet<Value*, 64>::iterator I = deadPointers.begin(), 00398 E = deadPointers.end(); I != E; ++I) { 00399 // HACK: if we detect that our AA is imprecise, it's not 00400 // worth it to scan the rest of the deadPointers set. Just 00401 // assume that the AA will return ModRef for everything, and 00402 // go ahead and bail. 00403 if (modRef >= 16 && other == 0) { 00404 deadPointers.clear(); 00405 return MadeChange; 00406 } 00407 00408 // Get size information for the alloca 00409 unsigned pointerSize = ~0U; 00410 if (AllocaInst* A = dyn_cast<AllocaInst>(*I)) { 00411 if (ConstantInt* C = dyn_cast<ConstantInt>(A->getArraySize())) 00412 pointerSize = C->getZExtValue() * \ 00413 TD.getABITypeSize(A->getAllocatedType()); 00414 } else { 00415 const PointerType* PT = cast<PointerType>( 00416 cast<Argument>(*I)->getType()); 00417 pointerSize = TD.getABITypeSize(PT->getElementType()); 00418 } 00419 00420 // See if the call site touches it 00421 AliasAnalysis::ModRefResult A = AA.getModRefInfo(CS, *I, pointerSize); 00422 00423 if (A == AliasAnalysis::ModRef) 00424 modRef++; 00425 else 00426 other++; 00427 00428 if (A == AliasAnalysis::ModRef || A == AliasAnalysis::Ref) 00429 dead.push_back(*I); 00430 } 00431 00432 for (std::vector<Value*>::iterator I = dead.begin(), E = dead.end(); 00433 I != E; ++I) 00434 deadPointers.erase(*I); 00435 00436 continue; 00437 } else { 00438 // For any non-memory-affecting non-terminators, DCE them as we reach them 00439 Instruction *CI = BBI; 00440 if (!CI->isTerminator() && CI->use_empty() && !isa<FreeInst>(CI)) { 00441 00442 // DCE instructions only used to calculate that load 00443 for (Instruction::op_iterator OI = CI->op_begin(), OE = CI->op_end(); 00444 OI != OE; ++OI) 00445 if (Instruction* D = dyn_cast<Instruction>(OI)) 00446 possiblyDead.insert(D); 00447 00448 BBI++; 00449 CI->eraseFromParent(); 00450 NumFastOther++; 00451 MadeChange = true; 00452 possiblyDead.remove(CI); 00453 00454 continue; 00455 } 00456 } 00457 00458 if (!killPointer) 00459 continue; 00460 00461 killPointer = killPointer->getUnderlyingObject(); 00462 00463 // Deal with undead pointers 00464 MadeChange |= RemoveUndeadPointers(killPointer, killPointerSize, BBI, 00465 deadPointers, possiblyDead); 00466 } 00467 00468 return MadeChange; 00469 } 00470 00471 /// RemoveUndeadPointers - check for uses of a pointer that make it 00472 /// undead when scanning for dead stores to alloca's. 00473 bool DSE::RemoveUndeadPointers(Value* killPointer, uint64_t killPointerSize, 00474 BasicBlock::iterator& BBI, 00475 SmallPtrSet<Value*, 64>& deadPointers, 00476 SetVector<Instruction*>& possiblyDead) { 00477 TargetData &TD = getAnalysis<TargetData>(); 00478 AliasAnalysis &AA = getAnalysis<AliasAnalysis>(); 00479 MemoryDependenceAnalysis& MD = getAnalysis<MemoryDependenceAnalysis>(); 00480 00481 // If the kill pointer can be easily reduced to an alloca, 00482 // don't bother doing extraneous AA queries 00483 if (deadPointers.count(killPointer)) { 00484 deadPointers.erase(killPointer); 00485 return false; 00486 } else if (isa<GlobalValue>(killPointer)) { 00487 // A global can't be in the dead pointer set 00488 return false; 00489 } 00490 00491 bool MadeChange = false; 00492 00493 std::vector<Value*> undead; 00494 00495 for (SmallPtrSet<Value*, 64>::iterator I = deadPointers.begin(), 00496 E = deadPointers.end(); I != E; ++I) { 00497 // Get size information for the alloca 00498 unsigned pointerSize = ~0U; 00499 if (AllocaInst* A = dyn_cast<AllocaInst>(*I)) { 00500 if (ConstantInt* C = dyn_cast<ConstantInt>(A->getArraySize())) 00501 pointerSize = C->getZExtValue() * \ 00502 TD.getABITypeSize(A->getAllocatedType()); 00503 } else { 00504 const PointerType* PT = cast<PointerType>( 00505 cast<Argument>(*I)->getType()); 00506 pointerSize = TD.getABITypeSize(PT->getElementType()); 00507 } 00508 00509 // See if this pointer could alias it 00510 AliasAnalysis::AliasResult A = AA.alias(*I, pointerSize, 00511 killPointer, killPointerSize); 00512 00513 // If it must-alias and a store, we can delete it 00514 if (isa<StoreInst>(BBI) && A == AliasAnalysis::MustAlias) { 00515 StoreInst* S = cast<StoreInst>(BBI); 00516 00517 // Remove it! 00518 MD.removeInstruction(S); 00519 00520 // DCE instructions only used to calculate that store 00521 if (Instruction* D = dyn_cast<Instruction>(S->getOperand(0))) 00522 possiblyDead.insert(D); 00523 if (Instruction* D = dyn_cast<Instruction>(S->getOperand(1))) 00524 possiblyDead.insert(D); 00525 00526 BBI++; 00527 S->eraseFromParent(); 00528 NumFastStores++; 00529 MadeChange = true; 00530 00531 continue; 00532 00533 // Otherwise, it is undead 00534 } else if (A != AliasAnalysis::NoAlias) 00535 undead.push_back(*I); 00536 } 00537 00538 for (std::vector<Value*>::iterator I = undead.begin(), E = undead.end(); 00539 I != E; ++I) 00540 deadPointers.erase(*I); 00541 00542 return MadeChange; 00543 } 00544 00545 /// DeleteDeadInstructionChains - takes an instruction and a setvector of 00546 /// dead instructions. If I is dead, it is erased, and its operands are 00547 /// checked for deadness. If they are dead, they are added to the dead 00548 /// setvector. 00549 void DSE::DeleteDeadInstructionChains(Instruction *I, 00550 SetVector<Instruction*> &DeadInsts) { 00551 // Instruction must be dead. 00552 if (!I->use_empty() || !isInstructionTriviallyDead(I)) return; 00553 00554 // Let the memory dependence know 00555 getAnalysis<MemoryDependenceAnalysis>().removeInstruction(I); 00556 00557 // See if this made any operands dead. We do it this way in case the 00558 // instruction uses the same operand twice. We don't want to delete a 00559 // value then reference it. 00560 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) { 00561 if (I->getOperand(i)->hasOneUse()) 00562 if (Instruction* Op = dyn_cast<Instruction>(I->getOperand(i))) 00563 DeadInsts.insert(Op); // Attempt to nuke it later. 00564 00565 I->setOperand(i, 0); // Drop from the operand list. 00566 } 00567 00568 I->eraseFromParent(); 00569 ++NumFastOther; 00570 }