1 //===- ADCE.cpp - Code to perform aggressive dead code elimination --------===//
3 // This file implements "aggressive" dead code elimination. ADCE is DCe where
4 // values are assumed to be dead until proven otherwise. This is similar to
5 // SCCP, except applied to the liveness of values.
7 //===----------------------------------------------------------------------===//
9 #include "llvm/Transforms/Scalar.h"
10 #include "llvm/Transforms/Utils/Local.h"
11 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
12 #include "llvm/Type.h"
13 #include "llvm/Analysis/PostDominators.h"
14 #include "llvm/iTerminators.h"
15 #include "llvm/iPHINode.h"
16 #include "llvm/Constant.h"
17 #include "llvm/Support/CFG.h"
18 #include "Support/Debug.h"
19 #include "Support/DepthFirstIterator.h"
20 #include "Support/Statistic.h"
21 #include "Support/STLExtras.h"
25 Statistic<> NumBlockRemoved("adce", "Number of basic blocks removed");
26 Statistic<> NumInstRemoved ("adce", "Number of instructions removed");
28 //===----------------------------------------------------------------------===//
31 // This class does all of the work of Aggressive Dead Code Elimination.
32 // It's public interface consists of a constructor and a doADCE() method.
34 class ADCE : public FunctionPass {
35 Function *Func; // The function that we are working on
36 std::vector<Instruction*> WorkList; // Instructions that just became live
37 std::set<Instruction*> LiveSet; // The set of live instructions
39 //===--------------------------------------------------------------------===//
40 // The public interface for this class
43 // Execute the Aggressive Dead Code Elimination Algorithm
45 virtual bool runOnFunction(Function &F) {
47 bool Changed = doADCE();
48 assert(WorkList.empty());
52 // getAnalysisUsage - We require post dominance frontiers (aka Control
54 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
55 AU.addRequired<PostDominatorTree>();
56 AU.addRequired<PostDominanceFrontier>();
60 //===--------------------------------------------------------------------===//
61 // The implementation of this class
64 // doADCE() - Run the Aggressive Dead Code Elimination algorithm, returning
65 // true if the function was modified.
69 void markBlockAlive(BasicBlock *BB);
72 // dropReferencesOfDeadInstructionsInLiveBlock - Loop over all of the
73 // instructions in the specified basic block, dropping references on
74 // instructions that are dead according to LiveSet.
75 bool dropReferencesOfDeadInstructionsInLiveBlock(BasicBlock *BB);
77 TerminatorInst *convertToUnconditionalBranch(TerminatorInst *TI);
79 inline void markInstructionLive(Instruction *I) {
80 if (LiveSet.count(I)) return;
81 DEBUG(std::cerr << "Insn Live: " << I);
83 WorkList.push_back(I);
86 inline void markTerminatorLive(const BasicBlock *BB) {
87 DEBUG(std::cerr << "Terminator Live: " << BB->getTerminator());
88 markInstructionLive(const_cast<TerminatorInst*>(BB->getTerminator()));
92 RegisterOpt<ADCE> X("adce", "Aggressive Dead Code Elimination");
93 } // End of anonymous namespace
95 Pass *createAggressiveDCEPass() { return new ADCE(); }
97 void ADCE::markBlockAlive(BasicBlock *BB) {
98 // Mark the basic block as being newly ALIVE... and mark all branches that
99 // this block is control dependent on as being alive also...
101 PostDominanceFrontier &CDG = getAnalysis<PostDominanceFrontier>();
103 PostDominanceFrontier::const_iterator It = CDG.find(BB);
104 if (It != CDG.end()) {
105 // Get the blocks that this node is control dependent on...
106 const PostDominanceFrontier::DomSetType &CDB = It->second;
107 for_each(CDB.begin(), CDB.end(), // Mark all their terminators as live
108 bind_obj(this, &ADCE::markTerminatorLive));
111 // If this basic block is live, and it ends in an unconditional branch, then
112 // the branch is alive as well...
113 if (BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator()))
114 if (BI->isUnconditional())
115 markTerminatorLive(BB);
118 // dropReferencesOfDeadInstructionsInLiveBlock - Loop over all of the
119 // instructions in the specified basic block, dropping references on
120 // instructions that are dead according to LiveSet.
121 bool ADCE::dropReferencesOfDeadInstructionsInLiveBlock(BasicBlock *BB) {
122 bool Changed = false;
123 for (BasicBlock::iterator I = BB->begin(), E = --BB->end(); I != E; )
124 if (!LiveSet.count(I)) { // Is this instruction alive?
125 I->dropAllReferences(); // Nope, drop references...
126 if (PHINode *PN = dyn_cast<PHINode>(I)) {
127 // We don't want to leave PHI nodes in the program that have
128 // #arguments != #predecessors, so we remove them now.
130 PN->replaceAllUsesWith(Constant::getNullValue(PN->getType()));
132 // Delete the instruction...
133 I = BB->getInstList().erase(I);
145 /// convertToUnconditionalBranch - Transform this conditional terminator
146 /// instruction into an unconditional branch because we don't care which of the
147 /// successors it goes to. This eliminate a use of the condition as well.
149 TerminatorInst *ADCE::convertToUnconditionalBranch(TerminatorInst *TI) {
150 BranchInst *NB = new BranchInst(TI->getSuccessor(0), TI);
151 BasicBlock *BB = TI->getParent();
153 // Remove entries from PHI nodes to avoid confusing ourself later...
154 for (unsigned i = 1, e = TI->getNumSuccessors(); i != e; ++i)
155 TI->getSuccessor(i)->removePredecessor(BB);
157 // Delete the old branch itself...
158 BB->getInstList().erase(TI);
163 // doADCE() - Run the Aggressive Dead Code Elimination algorithm, returning
164 // true if the function was modified.
166 bool ADCE::doADCE() {
167 bool MadeChanges = false;
169 // Iterate over all of the instructions in the function, eliminating trivially
170 // dead instructions, and marking instructions live that are known to be
171 // needed. Perform the walk in depth first order so that we avoid marking any
172 // instructions live in basic blocks that are unreachable. These blocks will
173 // be eliminated later, along with the instructions inside.
175 for (df_iterator<Function*> BBI = df_begin(Func), BBE = df_end(Func);
177 BasicBlock *BB = *BBI;
178 for (BasicBlock::iterator II = BB->begin(), EI = BB->end(); II != EI; ) {
179 if (II->mayWriteToMemory() || isa<ReturnInst>(II) || isa<UnwindInst>(II)){
180 markInstructionLive(II);
181 ++II; // Increment the inst iterator if the inst wasn't deleted
182 } else if (isInstructionTriviallyDead(II)) {
183 // Remove the instruction from it's basic block...
184 II = BB->getInstList().erase(II);
188 ++II; // Increment the inst iterator if the inst wasn't deleted
193 // Check to ensure we have an exit node for this CFG. If we don't, we won't
194 // have any post-dominance information, thus we cannot perform our
195 // transformations safely.
197 PostDominatorTree &DT = getAnalysis<PostDominatorTree>();
198 if (DT[&Func->getEntryBlock()] == 0) {
203 DEBUG(std::cerr << "Processing work list\n");
205 // AliveBlocks - Set of basic blocks that we know have instructions that are
208 std::set<BasicBlock*> AliveBlocks;
210 // Process the work list of instructions that just became live... if they
211 // became live, then that means that all of their operands are necessary as
212 // well... make them live as well.
214 while (!WorkList.empty()) {
215 Instruction *I = WorkList.back(); // Get an instruction that became live...
218 BasicBlock *BB = I->getParent();
219 if (!AliveBlocks.count(BB)) { // Basic block not alive yet...
220 AliveBlocks.insert(BB); // Block is now ALIVE!
221 markBlockAlive(BB); // Make it so now!
224 // PHI nodes are a special case, because the incoming values are actually
225 // defined in the predecessor nodes of this block, meaning that the PHI
226 // makes the predecessors alive.
228 if (PHINode *PN = dyn_cast<PHINode>(I))
229 for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB); PI != PE; ++PI)
230 if (!AliveBlocks.count(*PI)) {
231 AliveBlocks.insert(BB); // Block is now ALIVE!
235 // Loop over all of the operands of the live instruction, making sure that
236 // they are known to be alive as well...
238 for (unsigned op = 0, End = I->getNumOperands(); op != End; ++op)
239 if (Instruction *Operand = dyn_cast<Instruction>(I->getOperand(op)))
240 markInstructionLive(Operand);
244 std::cerr << "Current Function: X = Live\n";
245 for (Function::iterator I = Func->begin(), E = Func->end(); I != E; ++I){
246 std::cerr << I->getName() << ":\t"
247 << (AliveBlocks.count(I) ? "LIVE\n" : "DEAD\n");
248 for (BasicBlock::iterator BI = I->begin(), BE = I->end(); BI != BE; ++BI){
249 if (LiveSet.count(BI)) std::cerr << "X ";
254 // Find the first postdominator of the entry node that is alive. Make it the
257 if (AliveBlocks.size() == Func->size()) { // No dead blocks?
258 for (Function::iterator I = Func->begin(), E = Func->end(); I != E; ++I) {
259 // Loop over all of the instructions in the function, telling dead
260 // instructions to drop their references. This is so that the next sweep
261 // over the program can safely delete dead instructions without other dead
262 // instructions still refering to them.
264 dropReferencesOfDeadInstructionsInLiveBlock(I);
266 // Check to make sure the terminator instruction is live. If it isn't,
267 // this means that the condition that it branches on (we know it is not an
268 // unconditional branch), is not needed to make the decision of where to
269 // go to, because all outgoing edges go to the same place. We must remove
270 // the use of the condition (because it's probably dead), so we convert
271 // the terminator to a conditional branch.
273 TerminatorInst *TI = I->getTerminator();
274 if (!LiveSet.count(TI))
275 convertToUnconditionalBranch(TI);
278 } else { // If there are some blocks dead...
279 // If the entry node is dead, insert a new entry node to eliminate the entry
280 // node as a special case.
282 if (!AliveBlocks.count(&Func->front())) {
283 BasicBlock *NewEntry = new BasicBlock();
284 NewEntry->getInstList().push_back(new BranchInst(&Func->front()));
285 Func->getBasicBlockList().push_front(NewEntry);
286 AliveBlocks.insert(NewEntry); // This block is always alive!
287 LiveSet.insert(NewEntry->getTerminator()); // The branch is live
290 // Loop over all of the alive blocks in the function. If any successor
291 // blocks are not alive, we adjust the outgoing branches to branch to the
292 // first live postdominator of the live block, adjusting any PHI nodes in
293 // the block to reflect this.
295 for (Function::iterator I = Func->begin(), E = Func->end(); I != E; ++I)
296 if (AliveBlocks.count(I)) {
298 TerminatorInst *TI = BB->getTerminator();
300 // If the terminator instruction is alive, but the block it is contained
301 // in IS alive, this means that this terminator is a conditional branch
302 // on a condition that doesn't matter. Make it an unconditional branch
303 // to ONE of the successors. This has the side effect of dropping a use
304 // of the conditional value, which may also be dead.
305 if (!LiveSet.count(TI))
306 TI = convertToUnconditionalBranch(TI);
308 // Loop over all of the successors, looking for ones that are not alive.
309 // We cannot save the number of successors in the terminator instruction
310 // here because we may remove them if we don't have a postdominator...
312 for (unsigned i = 0; i != TI->getNumSuccessors(); ++i)
313 if (!AliveBlocks.count(TI->getSuccessor(i))) {
314 // Scan up the postdominator tree, looking for the first
315 // postdominator that is alive, and the last postdominator that is
318 PostDominatorTree::Node *LastNode = DT[TI->getSuccessor(i)];
320 // There is a special case here... if there IS no post-dominator for
321 // the block we have no owhere to point our branch to. Instead,
322 // convert it to a return. This can only happen if the code
323 // branched into an infinite loop. Note that this may not be
324 // desirable, because we _are_ altering the behavior of the code.
325 // This is a well known drawback of ADCE, so in the future if we
326 // choose to revisit the decision, this is where it should be.
328 if (LastNode == 0) { // No postdominator!
329 // Call RemoveSuccessor to transmogrify the terminator instruction
330 // to not contain the outgoing branch, or to create a new
331 // terminator if the form fundementally changes (ie unconditional
332 // branch to return). Note that this will change a branch into an
333 // infinite loop into a return instruction!
335 RemoveSuccessor(TI, i);
337 // RemoveSuccessor may replace TI... make sure we have a fresh
338 // pointer... and e variable.
340 TI = BB->getTerminator();
342 // Rescan this successor...
345 PostDominatorTree::Node *NextNode = LastNode->getIDom();
347 while (!AliveBlocks.count(NextNode->getBlock())) {
349 NextNode = NextNode->getIDom();
352 // Get the basic blocks that we need...
353 BasicBlock *LastDead = LastNode->getBlock();
354 BasicBlock *NextAlive = NextNode->getBlock();
356 // Make the conditional branch now go to the next alive block...
357 TI->getSuccessor(i)->removePredecessor(BB);
358 TI->setSuccessor(i, NextAlive);
360 // If there are PHI nodes in NextAlive, we need to add entries to
361 // the PHI nodes for the new incoming edge. The incoming values
362 // should be identical to the incoming values for LastDead.
364 for (BasicBlock::iterator II = NextAlive->begin();
365 PHINode *PN = dyn_cast<PHINode>(II); ++II)
366 if (LiveSet.count(PN)) { // Only modify live phi nodes
367 // Get the incoming value for LastDead...
368 int OldIdx = PN->getBasicBlockIndex(LastDead);
369 assert(OldIdx != -1 &&"LastDead is not a pred of NextAlive!");
370 Value *InVal = PN->getIncomingValue(OldIdx);
372 // Add an incoming value for BB now...
373 PN->addIncoming(InVal, BB);
378 // Now loop over all of the instructions in the basic block, telling
379 // dead instructions to drop their references. This is so that the next
380 // sweep over the program can safely delete dead instructions without
381 // other dead instructions still refering to them.
383 dropReferencesOfDeadInstructionsInLiveBlock(BB);
387 // We make changes if there are any dead blocks in the function...
388 if (unsigned NumDeadBlocks = Func->size() - AliveBlocks.size()) {
390 NumBlockRemoved += NumDeadBlocks;
393 // Loop over all of the basic blocks in the function, removing control flow
394 // edges to live blocks (also eliminating any entries in PHI functions in
395 // referenced blocks).
397 for (Function::iterator BB = Func->begin(), E = Func->end(); BB != E; ++BB)
398 if (!AliveBlocks.count(BB)) {
399 // Remove all outgoing edges from this basic block and convert the
400 // terminator into a return instruction.
401 std::vector<BasicBlock*> Succs(succ_begin(BB), succ_end(BB));
403 if (!Succs.empty()) {
404 // Loop over all of the successors, removing this block from PHI node
405 // entries that might be in the block...
406 while (!Succs.empty()) {
407 Succs.back()->removePredecessor(BB);
411 // Delete the old terminator instruction...
412 BB->getInstList().pop_back();
413 const Type *RetTy = Func->getReturnType();
414 BB->getInstList().push_back(new ReturnInst(RetTy != Type::VoidTy ?
415 Constant::getNullValue(RetTy) : 0));
420 // Loop over all of the basic blocks in the function, dropping references of
421 // the dead basic blocks. We must do this after the previous step to avoid
422 // dropping references to PHIs which still have entries...
424 for (Function::iterator BB = Func->begin(), E = Func->end(); BB != E; ++BB)
425 if (!AliveBlocks.count(BB))
426 BB->dropAllReferences();
428 // Now loop through all of the blocks and delete the dead ones. We can safely
429 // do this now because we know that there are no references to dead blocks
430 // (because they have dropped all of their references... we also remove dead
431 // instructions from alive blocks.
433 for (Function::iterator BI = Func->begin(); BI != Func->end(); )
434 if (!AliveBlocks.count(BI)) { // Delete dead blocks...
435 BI = Func->getBasicBlockList().erase(BI);
436 } else { // Scan alive blocks...
437 for (BasicBlock::iterator II = BI->begin(); II != --BI->end(); )
438 if (!LiveSet.count(II)) { // Is this instruction alive?
439 // Nope... remove the instruction from it's basic block...
440 II = BI->getInstList().erase(II);
447 ++BI; // Increment iterator...