LLVM API Documentation
00001 //===- ADCE.cpp - Code to perform aggressive dead code elimination --------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file was developed by the LLVM research group and is distributed under 00006 // the University of Illinois Open Source License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file implements "aggressive" dead code elimination. ADCE is DCe where 00011 // values are assumed to be dead until proven otherwise. This is similar to 00012 // SCCP, except applied to the liveness of values. 00013 // 00014 //===----------------------------------------------------------------------===// 00015 00016 #include "llvm/Transforms/Scalar.h" 00017 #include "llvm/Constants.h" 00018 #include "llvm/Instructions.h" 00019 #include "llvm/Analysis/AliasAnalysis.h" 00020 #include "llvm/Analysis/PostDominators.h" 00021 #include "llvm/Support/CFG.h" 00022 #include "llvm/Transforms/Utils/BasicBlockUtils.h" 00023 #include "llvm/Transforms/Utils/Local.h" 00024 #include "llvm/Transforms/Utils/UnifyFunctionExitNodes.h" 00025 #include "llvm/Support/Debug.h" 00026 #include "llvm/ADT/DepthFirstIterator.h" 00027 #include "llvm/ADT/Statistic.h" 00028 #include "llvm/ADT/STLExtras.h" 00029 #include <algorithm> 00030 #include <iostream> 00031 using namespace llvm; 00032 00033 static IncludeFile X((void*)createUnifyFunctionExitNodesPass); 00034 00035 namespace { 00036 Statistic<> NumBlockRemoved("adce", "Number of basic blocks removed"); 00037 Statistic<> NumInstRemoved ("adce", "Number of instructions removed"); 00038 Statistic<> NumCallRemoved ("adce", "Number of calls and invokes removed"); 00039 00040 //===----------------------------------------------------------------------===// 00041 // ADCE Class 00042 // 00043 // This class does all of the work of Aggressive Dead Code Elimination. 00044 // It's public interface consists of a constructor and a doADCE() method. 00045 // 00046 class ADCE : public FunctionPass { 00047 Function *Func; // The function that we are working on 00048 std::vector<Instruction*> WorkList; // Instructions that just became live 00049 std::set<Instruction*> LiveSet; // The set of live instructions 00050 00051 //===--------------------------------------------------------------------===// 00052 // The public interface for this class 00053 // 00054 public: 00055 // Execute the Aggressive Dead Code Elimination Algorithm 00056 // 00057 virtual bool runOnFunction(Function &F) { 00058 Func = &F; 00059 bool Changed = doADCE(); 00060 assert(WorkList.empty()); 00061 LiveSet.clear(); 00062 return Changed; 00063 } 00064 // getAnalysisUsage - We require post dominance frontiers (aka Control 00065 // Dependence Graph) 00066 virtual void getAnalysisUsage(AnalysisUsage &AU) const { 00067 // We require that all function nodes are unified, because otherwise code 00068 // can be marked live that wouldn't necessarily be otherwise. 00069 AU.addRequired<UnifyFunctionExitNodes>(); 00070 AU.addRequired<AliasAnalysis>(); 00071 AU.addRequired<PostDominatorTree>(); 00072 AU.addRequired<PostDominanceFrontier>(); 00073 } 00074 00075 00076 //===--------------------------------------------------------------------===// 00077 // The implementation of this class 00078 // 00079 private: 00080 // doADCE() - Run the Aggressive Dead Code Elimination algorithm, returning 00081 // true if the function was modified. 00082 // 00083 bool doADCE(); 00084 00085 void markBlockAlive(BasicBlock *BB); 00086 00087 00088 // deleteDeadInstructionsInLiveBlock - Loop over all of the instructions in 00089 // the specified basic block, deleting ones that are dead according to 00090 // LiveSet. 00091 bool deleteDeadInstructionsInLiveBlock(BasicBlock *BB); 00092 00093 TerminatorInst *convertToUnconditionalBranch(TerminatorInst *TI); 00094 00095 inline void markInstructionLive(Instruction *I) { 00096 if (!LiveSet.insert(I).second) return; 00097 DEBUG(std::cerr << "Insn Live: " << *I); 00098 WorkList.push_back(I); 00099 } 00100 00101 inline void markTerminatorLive(const BasicBlock *BB) { 00102 DEBUG(std::cerr << "Terminator Live: " << *BB->getTerminator()); 00103 markInstructionLive(const_cast<TerminatorInst*>(BB->getTerminator())); 00104 } 00105 }; 00106 00107 RegisterOpt<ADCE> X("adce", "Aggressive Dead Code Elimination"); 00108 } // End of anonymous namespace 00109 00110 FunctionPass *llvm::createAggressiveDCEPass() { return new ADCE(); } 00111 00112 void ADCE::markBlockAlive(BasicBlock *BB) { 00113 // Mark the basic block as being newly ALIVE... and mark all branches that 00114 // this block is control dependent on as being alive also... 00115 // 00116 PostDominanceFrontier &CDG = getAnalysis<PostDominanceFrontier>(); 00117 00118 PostDominanceFrontier::const_iterator It = CDG.find(BB); 00119 if (It != CDG.end()) { 00120 // Get the blocks that this node is control dependent on... 00121 const PostDominanceFrontier::DomSetType &CDB = It->second; 00122 for (PostDominanceFrontier::DomSetType::const_iterator I = 00123 CDB.begin(), E = CDB.end(); I != E; ++I) 00124 markTerminatorLive(*I); // Mark all their terminators as live 00125 } 00126 00127 // If this basic block is live, and it ends in an unconditional branch, then 00128 // the branch is alive as well... 00129 if (BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator())) 00130 if (BI->isUnconditional()) 00131 markTerminatorLive(BB); 00132 } 00133 00134 // deleteDeadInstructionsInLiveBlock - Loop over all of the instructions in the 00135 // specified basic block, deleting ones that are dead according to LiveSet. 00136 bool ADCE::deleteDeadInstructionsInLiveBlock(BasicBlock *BB) { 00137 bool Changed = false; 00138 for (BasicBlock::iterator II = BB->begin(), E = --BB->end(); II != E; ) { 00139 Instruction *I = II++; 00140 if (!LiveSet.count(I)) { // Is this instruction alive? 00141 if (!I->use_empty()) 00142 I->replaceAllUsesWith(UndefValue::get(I->getType())); 00143 00144 // Nope... remove the instruction from it's basic block... 00145 if (isa<CallInst>(I)) 00146 ++NumCallRemoved; 00147 else 00148 ++NumInstRemoved; 00149 BB->getInstList().erase(I); 00150 Changed = true; 00151 } 00152 } 00153 return Changed; 00154 } 00155 00156 00157 /// convertToUnconditionalBranch - Transform this conditional terminator 00158 /// instruction into an unconditional branch because we don't care which of the 00159 /// successors it goes to. This eliminate a use of the condition as well. 00160 /// 00161 TerminatorInst *ADCE::convertToUnconditionalBranch(TerminatorInst *TI) { 00162 BranchInst *NB = new BranchInst(TI->getSuccessor(0), TI); 00163 BasicBlock *BB = TI->getParent(); 00164 00165 // Remove entries from PHI nodes to avoid confusing ourself later... 00166 for (unsigned i = 1, e = TI->getNumSuccessors(); i != e; ++i) 00167 TI->getSuccessor(i)->removePredecessor(BB); 00168 00169 // Delete the old branch itself... 00170 BB->getInstList().erase(TI); 00171 return NB; 00172 } 00173 00174 00175 // doADCE() - Run the Aggressive Dead Code Elimination algorithm, returning 00176 // true if the function was modified. 00177 // 00178 bool ADCE::doADCE() { 00179 bool MadeChanges = false; 00180 00181 AliasAnalysis &AA = getAnalysis<AliasAnalysis>(); 00182 00183 00184 // Iterate over all invokes in the function, turning invokes into calls if 00185 // they cannot throw. 00186 for (Function::iterator BB = Func->begin(), E = Func->end(); BB != E; ++BB) 00187 if (InvokeInst *II = dyn_cast<InvokeInst>(BB->getTerminator())) 00188 if (Function *F = II->getCalledFunction()) 00189 if (AA.onlyReadsMemory(F)) { 00190 // The function cannot unwind. Convert it to a call with a branch 00191 // after it to the normal destination. 00192 std::vector<Value*> Args(II->op_begin()+3, II->op_end()); 00193 std::string Name = II->getName(); II->setName(""); 00194 CallInst *NewCall = new CallInst(F, Args, Name, II); 00195 NewCall->setCallingConv(II->getCallingConv()); 00196 II->replaceAllUsesWith(NewCall); 00197 new BranchInst(II->getNormalDest(), II); 00198 00199 // Update PHI nodes in the unwind destination 00200 II->getUnwindDest()->removePredecessor(BB); 00201 BB->getInstList().erase(II); 00202 00203 if (NewCall->use_empty()) { 00204 BB->getInstList().erase(NewCall); 00205 ++NumCallRemoved; 00206 } 00207 } 00208 00209 // Iterate over all of the instructions in the function, eliminating trivially 00210 // dead instructions, and marking instructions live that are known to be 00211 // needed. Perform the walk in depth first order so that we avoid marking any 00212 // instructions live in basic blocks that are unreachable. These blocks will 00213 // be eliminated later, along with the instructions inside. 00214 // 00215 std::set<BasicBlock*> ReachableBBs; 00216 for (df_ext_iterator<BasicBlock*> 00217 BBI = df_ext_begin(&Func->front(), ReachableBBs), 00218 BBE = df_ext_end(&Func->front(), ReachableBBs); BBI != BBE; ++BBI) { 00219 BasicBlock *BB = *BBI; 00220 for (BasicBlock::iterator II = BB->begin(), EI = BB->end(); II != EI; ) { 00221 Instruction *I = II++; 00222 if (CallInst *CI = dyn_cast<CallInst>(I)) { 00223 Function *F = CI->getCalledFunction(); 00224 if (F && AA.onlyReadsMemory(F)) { 00225 if (CI->use_empty()) { 00226 BB->getInstList().erase(CI); 00227 ++NumCallRemoved; 00228 } 00229 } else { 00230 markInstructionLive(I); 00231 } 00232 } else if (I->mayWriteToMemory() || isa<ReturnInst>(I) || 00233 isa<UnwindInst>(I) || isa<UnreachableInst>(I)) { 00234 // FIXME: Unreachable instructions should not be marked intrinsically 00235 // live here. 00236 markInstructionLive(I); 00237 } else if (isInstructionTriviallyDead(I)) { 00238 // Remove the instruction from it's basic block... 00239 BB->getInstList().erase(I); 00240 ++NumInstRemoved; 00241 } 00242 } 00243 } 00244 00245 // Check to ensure we have an exit node for this CFG. If we don't, we won't 00246 // have any post-dominance information, thus we cannot perform our 00247 // transformations safely. 00248 // 00249 PostDominatorTree &DT = getAnalysis<PostDominatorTree>(); 00250 if (DT[&Func->getEntryBlock()] == 0) { 00251 WorkList.clear(); 00252 return MadeChanges; 00253 } 00254 00255 // Scan the function marking blocks without post-dominance information as 00256 // live. Blocks without post-dominance information occur when there is an 00257 // infinite loop in the program. Because the infinite loop could contain a 00258 // function which unwinds, exits or has side-effects, we don't want to delete 00259 // the infinite loop or those blocks leading up to it. 00260 for (Function::iterator I = Func->begin(), E = Func->end(); I != E; ++I) 00261 if (DT[I] == 0 && ReachableBBs.count(I)) 00262 for (pred_iterator PI = pred_begin(I), E = pred_end(I); PI != E; ++PI) 00263 markInstructionLive((*PI)->getTerminator()); 00264 00265 DEBUG(std::cerr << "Processing work list\n"); 00266 00267 // AliveBlocks - Set of basic blocks that we know have instructions that are 00268 // alive in them... 00269 // 00270 std::set<BasicBlock*> AliveBlocks; 00271 00272 // Process the work list of instructions that just became live... if they 00273 // became live, then that means that all of their operands are necessary as 00274 // well... make them live as well. 00275 // 00276 while (!WorkList.empty()) { 00277 Instruction *I = WorkList.back(); // Get an instruction that became live... 00278 WorkList.pop_back(); 00279 00280 BasicBlock *BB = I->getParent(); 00281 if (!ReachableBBs.count(BB)) continue; 00282 if (AliveBlocks.insert(BB).second) // Basic block not alive yet. 00283 markBlockAlive(BB); // Make it so now! 00284 00285 // PHI nodes are a special case, because the incoming values are actually 00286 // defined in the predecessor nodes of this block, meaning that the PHI 00287 // makes the predecessors alive. 00288 // 00289 if (PHINode *PN = dyn_cast<PHINode>(I)) { 00290 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { 00291 // If the incoming edge is clearly dead, it won't have control 00292 // dependence information. Do not mark it live. 00293 BasicBlock *PredBB = PN->getIncomingBlock(i); 00294 if (ReachableBBs.count(PredBB)) { 00295 // FIXME: This should mark the control dependent edge as live, not 00296 // necessarily the predecessor itself! 00297 if (AliveBlocks.insert(PredBB).second) 00298 markBlockAlive(PN->getIncomingBlock(i)); // Block is newly ALIVE! 00299 if (Instruction *Op = dyn_cast<Instruction>(PN->getIncomingValue(i))) 00300 markInstructionLive(Op); 00301 } 00302 } 00303 } else { 00304 // Loop over all of the operands of the live instruction, making sure that 00305 // they are known to be alive as well. 00306 // 00307 for (unsigned op = 0, End = I->getNumOperands(); op != End; ++op) 00308 if (Instruction *Operand = dyn_cast<Instruction>(I->getOperand(op))) 00309 markInstructionLive(Operand); 00310 } 00311 } 00312 00313 DEBUG( 00314 std::cerr << "Current Function: X = Live\n"; 00315 for (Function::iterator I = Func->begin(), E = Func->end(); I != E; ++I){ 00316 std::cerr << I->getName() << ":\t" 00317 << (AliveBlocks.count(I) ? "LIVE\n" : "DEAD\n"); 00318 for (BasicBlock::iterator BI = I->begin(), BE = I->end(); BI != BE; ++BI){ 00319 if (LiveSet.count(BI)) std::cerr << "X "; 00320 std::cerr << *BI; 00321 } 00322 }); 00323 00324 // All blocks being live is a common case, handle it specially. 00325 if (AliveBlocks.size() == Func->size()) { // No dead blocks? 00326 for (Function::iterator I = Func->begin(), E = Func->end(); I != E; ++I) { 00327 // Loop over all of the instructions in the function deleting instructions 00328 // to drop their references. 00329 deleteDeadInstructionsInLiveBlock(I); 00330 00331 // Check to make sure the terminator instruction is live. If it isn't, 00332 // this means that the condition that it branches on (we know it is not an 00333 // unconditional branch), is not needed to make the decision of where to 00334 // go to, because all outgoing edges go to the same place. We must remove 00335 // the use of the condition (because it's probably dead), so we convert 00336 // the terminator to an unconditional branch. 00337 // 00338 TerminatorInst *TI = I->getTerminator(); 00339 if (!LiveSet.count(TI)) 00340 convertToUnconditionalBranch(TI); 00341 } 00342 00343 return MadeChanges; 00344 } 00345 00346 00347 // If the entry node is dead, insert a new entry node to eliminate the entry 00348 // node as a special case. 00349 // 00350 if (!AliveBlocks.count(&Func->front())) { 00351 BasicBlock *NewEntry = new BasicBlock(); 00352 new BranchInst(&Func->front(), NewEntry); 00353 Func->getBasicBlockList().push_front(NewEntry); 00354 AliveBlocks.insert(NewEntry); // This block is always alive! 00355 LiveSet.insert(NewEntry->getTerminator()); // The branch is live 00356 } 00357 00358 // Loop over all of the alive blocks in the function. If any successor 00359 // blocks are not alive, we adjust the outgoing branches to branch to the 00360 // first live postdominator of the live block, adjusting any PHI nodes in 00361 // the block to reflect this. 00362 // 00363 for (Function::iterator I = Func->begin(), E = Func->end(); I != E; ++I) 00364 if (AliveBlocks.count(I)) { 00365 BasicBlock *BB = I; 00366 TerminatorInst *TI = BB->getTerminator(); 00367 00368 // If the terminator instruction is alive, but the block it is contained 00369 // in IS alive, this means that this terminator is a conditional branch on 00370 // a condition that doesn't matter. Make it an unconditional branch to 00371 // ONE of the successors. This has the side effect of dropping a use of 00372 // the conditional value, which may also be dead. 00373 if (!LiveSet.count(TI)) 00374 TI = convertToUnconditionalBranch(TI); 00375 00376 // Loop over all of the successors, looking for ones that are not alive. 00377 // We cannot save the number of successors in the terminator instruction 00378 // here because we may remove them if we don't have a postdominator. 00379 // 00380 for (unsigned i = 0; i != TI->getNumSuccessors(); ++i) 00381 if (!AliveBlocks.count(TI->getSuccessor(i))) { 00382 // Scan up the postdominator tree, looking for the first 00383 // postdominator that is alive, and the last postdominator that is 00384 // dead... 00385 // 00386 PostDominatorTree::Node *LastNode = DT[TI->getSuccessor(i)]; 00387 PostDominatorTree::Node *NextNode = 0; 00388 00389 if (LastNode) { 00390 NextNode = LastNode->getIDom(); 00391 while (!AliveBlocks.count(NextNode->getBlock())) { 00392 LastNode = NextNode; 00393 NextNode = NextNode->getIDom(); 00394 if (NextNode == 0) { 00395 LastNode = 0; 00396 break; 00397 } 00398 } 00399 } 00400 00401 // There is a special case here... if there IS no post-dominator for 00402 // the block we have nowhere to point our branch to. Instead, convert 00403 // it to a return. This can only happen if the code branched into an 00404 // infinite loop. Note that this may not be desirable, because we 00405 // _are_ altering the behavior of the code. This is a well known 00406 // drawback of ADCE, so in the future if we choose to revisit the 00407 // decision, this is where it should be. 00408 // 00409 if (LastNode == 0) { // No postdominator! 00410 if (!isa<InvokeInst>(TI)) { 00411 // Call RemoveSuccessor to transmogrify the terminator instruction 00412 // to not contain the outgoing branch, or to create a new 00413 // terminator if the form fundamentally changes (i.e., 00414 // unconditional branch to return). Note that this will change a 00415 // branch into an infinite loop into a return instruction! 00416 // 00417 RemoveSuccessor(TI, i); 00418 00419 // RemoveSuccessor may replace TI... make sure we have a fresh 00420 // pointer. 00421 // 00422 TI = BB->getTerminator(); 00423 00424 // Rescan this successor... 00425 --i; 00426 } else { 00427 00428 } 00429 } else { 00430 // Get the basic blocks that we need... 00431 BasicBlock *LastDead = LastNode->getBlock(); 00432 BasicBlock *NextAlive = NextNode->getBlock(); 00433 00434 // Make the conditional branch now go to the next alive block... 00435 TI->getSuccessor(i)->removePredecessor(BB); 00436 TI->setSuccessor(i, NextAlive); 00437 00438 // If there are PHI nodes in NextAlive, we need to add entries to 00439 // the PHI nodes for the new incoming edge. The incoming values 00440 // should be identical to the incoming values for LastDead. 00441 // 00442 for (BasicBlock::iterator II = NextAlive->begin(); 00443 isa<PHINode>(II); ++II) { 00444 PHINode *PN = cast<PHINode>(II); 00445 if (LiveSet.count(PN)) { // Only modify live phi nodes 00446 // Get the incoming value for LastDead... 00447 int OldIdx = PN->getBasicBlockIndex(LastDead); 00448 assert(OldIdx != -1 &&"LastDead is not a pred of NextAlive!"); 00449 Value *InVal = PN->getIncomingValue(OldIdx); 00450 00451 // Add an incoming value for BB now... 00452 PN->addIncoming(InVal, BB); 00453 } 00454 } 00455 } 00456 } 00457 00458 // Now loop over all of the instructions in the basic block, deleting 00459 // dead instructions. This is so that the next sweep over the program 00460 // can safely delete dead instructions without other dead instructions 00461 // still referring to them. 00462 // 00463 deleteDeadInstructionsInLiveBlock(BB); 00464 } 00465 00466 // Loop over all of the basic blocks in the function, dropping references of 00467 // the dead basic blocks. We must do this after the previous step to avoid 00468 // dropping references to PHIs which still have entries... 00469 // 00470 std::vector<BasicBlock*> DeadBlocks; 00471 for (Function::iterator BB = Func->begin(), E = Func->end(); BB != E; ++BB) 00472 if (!AliveBlocks.count(BB)) { 00473 // Remove PHI node entries for this block in live successor blocks. 00474 for (succ_iterator SI = succ_begin(BB), E = succ_end(BB); SI != E; ++SI) 00475 if (!SI->empty() && isa<PHINode>(SI->front()) && AliveBlocks.count(*SI)) 00476 (*SI)->removePredecessor(BB); 00477 00478 BB->dropAllReferences(); 00479 MadeChanges = true; 00480 DeadBlocks.push_back(BB); 00481 } 00482 00483 NumBlockRemoved += DeadBlocks.size(); 00484 00485 // Now loop through all of the blocks and delete the dead ones. We can safely 00486 // do this now because we know that there are no references to dead blocks 00487 // (because they have dropped all of their references). 00488 for (std::vector<BasicBlock*>::iterator I = DeadBlocks.begin(), 00489 E = DeadBlocks.end(); I != E; ++I) 00490 Func->getBasicBlockList().erase(*I); 00491 00492 return MadeChanges; 00493 }