Chris Lattner | ea54ab9 | 2002-05-07 22:11:39 +0000 | [diff] [blame] | 1 | //===- ADCE.cpp - Code to perform aggressive dead code elimination --------===// |
Chris Lattner | 02e90d5 | 2001-06-30 06:39:11 +0000 | [diff] [blame] | 2 | // |
Chris Lattner | ea54ab9 | 2002-05-07 22:11:39 +0000 | [diff] [blame] | 3 | // This file implements "aggressive" dead code elimination. ADCE is DCe where |
Chris Lattner | 02e90d5 | 2001-06-30 06:39:11 +0000 | [diff] [blame] | 4 | // values are assumed to be dead until proven otherwise. This is similar to |
| 5 | // SCCP, except applied to the liveness of values. |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
Chris Lattner | 022103b | 2002-05-07 20:03:00 +0000 | [diff] [blame] | 9 | #include "llvm/Transforms/Scalar.h" |
Chris Lattner | ea54ab9 | 2002-05-07 22:11:39 +0000 | [diff] [blame] | 10 | #include "llvm/Transforms/Utils/Local.h" |
Chris Lattner | 011de07 | 2002-07-29 22:31:39 +0000 | [diff] [blame] | 11 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
Chris Lattner | 02e90d5 | 2001-06-30 06:39:11 +0000 | [diff] [blame] | 12 | #include "llvm/Type.h" |
Chris Lattner | a69fd90 | 2002-08-21 23:43:50 +0000 | [diff] [blame] | 13 | #include "llvm/Analysis/PostDominators.h" |
Chris Lattner | b8259dd | 2001-09-09 22:26:47 +0000 | [diff] [blame] | 14 | #include "llvm/iTerminators.h" |
Chris Lattner | 7061dc5 | 2001-12-03 18:02:31 +0000 | [diff] [blame] | 15 | #include "llvm/iPHINode.h" |
Chris Lattner | d9036a1 | 2002-05-22 21:32:16 +0000 | [diff] [blame] | 16 | #include "llvm/Constant.h" |
Chris Lattner | 221d688 | 2002-02-12 21:07:25 +0000 | [diff] [blame] | 17 | #include "llvm/Support/CFG.h" |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 18 | #include "Support/STLExtras.h" |
| 19 | #include "Support/DepthFirstIterator.h" |
Chris Lattner | a92f696 | 2002-10-01 22:38:41 +0000 | [diff] [blame] | 20 | #include "Support/Statistic.h" |
Chris Lattner | 72f1e99 | 2001-07-08 18:38:36 +0000 | [diff] [blame] | 21 | #include <algorithm> |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 22 | using std::cerr; |
Anand Shukla | 5ba99bd | 2002-06-25 21:07:58 +0000 | [diff] [blame] | 23 | using std::vector; |
Chris Lattner | 02e90d5 | 2001-06-30 06:39:11 +0000 | [diff] [blame] | 24 | |
Chris Lattner | dfe81ab | 2002-05-06 17:27:57 +0000 | [diff] [blame] | 25 | namespace { |
Chris Lattner | a92f696 | 2002-10-01 22:38:41 +0000 | [diff] [blame] | 26 | Statistic<> NumBlockRemoved("adce", "Number of basic blocks removed"); |
| 27 | Statistic<> NumInstRemoved ("adce", "Number of instructions removed"); |
Chris Lattner | dfe81ab | 2002-05-06 17:27:57 +0000 | [diff] [blame] | 28 | |
Chris Lattner | 02e90d5 | 2001-06-30 06:39:11 +0000 | [diff] [blame] | 29 | //===----------------------------------------------------------------------===// |
| 30 | // ADCE Class |
| 31 | // |
Chris Lattner | ea54ab9 | 2002-05-07 22:11:39 +0000 | [diff] [blame] | 32 | // This class does all of the work of Aggressive Dead Code Elimination. |
Chris Lattner | 02e90d5 | 2001-06-30 06:39:11 +0000 | [diff] [blame] | 33 | // It's public interface consists of a constructor and a doADCE() method. |
| 34 | // |
Chris Lattner | dfe81ab | 2002-05-06 17:27:57 +0000 | [diff] [blame] | 35 | class ADCE : public FunctionPass { |
| 36 | Function *Func; // The function that we are working on |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 37 | std::vector<Instruction*> WorkList; // Instructions that just became live |
| 38 | std::set<Instruction*> LiveSet; // The set of live instructions |
Chris Lattner | 02e90d5 | 2001-06-30 06:39:11 +0000 | [diff] [blame] | 39 | |
| 40 | //===--------------------------------------------------------------------===// |
| 41 | // The public interface for this class |
| 42 | // |
| 43 | public: |
Chris Lattner | d9036a1 | 2002-05-22 21:32:16 +0000 | [diff] [blame] | 44 | // Execute the Aggressive Dead Code Elimination Algorithm |
Chris Lattner | dfe81ab | 2002-05-06 17:27:57 +0000 | [diff] [blame] | 45 | // |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 46 | virtual bool runOnFunction(Function &F) { |
| 47 | Func = &F; |
Chris Lattner | d9036a1 | 2002-05-22 21:32:16 +0000 | [diff] [blame] | 48 | bool Changed = doADCE(); |
Chris Lattner | dfe81ab | 2002-05-06 17:27:57 +0000 | [diff] [blame] | 49 | assert(WorkList.empty()); |
| 50 | LiveSet.clear(); |
Chris Lattner | d9036a1 | 2002-05-22 21:32:16 +0000 | [diff] [blame] | 51 | return Changed; |
Chris Lattner | dfe81ab | 2002-05-06 17:27:57 +0000 | [diff] [blame] | 52 | } |
| 53 | // getAnalysisUsage - We require post dominance frontiers (aka Control |
| 54 | // Dependence Graph) |
| 55 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
Chris Lattner | 5f0eb8d | 2002-08-08 19:01:30 +0000 | [diff] [blame] | 56 | AU.addRequired<PostDominatorTree>(); |
| 57 | AU.addRequired<PostDominanceFrontier>(); |
Chris Lattner | dfe81ab | 2002-05-06 17:27:57 +0000 | [diff] [blame] | 58 | } |
Chris Lattner | 02e90d5 | 2001-06-30 06:39:11 +0000 | [diff] [blame] | 59 | |
Chris Lattner | 02e90d5 | 2001-06-30 06:39:11 +0000 | [diff] [blame] | 60 | |
| 61 | //===--------------------------------------------------------------------===// |
| 62 | // The implementation of this class |
| 63 | // |
| 64 | private: |
Chris Lattner | ea54ab9 | 2002-05-07 22:11:39 +0000 | [diff] [blame] | 65 | // doADCE() - Run the Aggressive Dead Code Elimination algorithm, returning |
Chris Lattner | dfe81ab | 2002-05-06 17:27:57 +0000 | [diff] [blame] | 66 | // true if the function was modified. |
| 67 | // |
Chris Lattner | d9036a1 | 2002-05-22 21:32:16 +0000 | [diff] [blame] | 68 | bool doADCE(); |
| 69 | |
| 70 | void markBlockAlive(BasicBlock *BB); |
Chris Lattner | dfe81ab | 2002-05-06 17:27:57 +0000 | [diff] [blame] | 71 | |
Chris Lattner | 446698b | 2002-07-30 00:22:34 +0000 | [diff] [blame] | 72 | |
| 73 | // dropReferencesOfDeadInstructionsInLiveBlock - Loop over all of the |
| 74 | // instructions in the specified basic block, dropping references on |
| 75 | // instructions that are dead according to LiveSet. |
| 76 | bool dropReferencesOfDeadInstructionsInLiveBlock(BasicBlock *BB); |
| 77 | |
Chris Lattner | 02e90d5 | 2001-06-30 06:39:11 +0000 | [diff] [blame] | 78 | inline void markInstructionLive(Instruction *I) { |
| 79 | if (LiveSet.count(I)) return; |
Chris Lattner | f016ea4 | 2002-05-22 17:17:27 +0000 | [diff] [blame] | 80 | DEBUG(cerr << "Insn Live: " << I); |
Chris Lattner | 02e90d5 | 2001-06-30 06:39:11 +0000 | [diff] [blame] | 81 | LiveSet.insert(I); |
| 82 | WorkList.push_back(I); |
| 83 | } |
| 84 | |
Chris Lattner | 72f1e99 | 2001-07-08 18:38:36 +0000 | [diff] [blame] | 85 | inline void markTerminatorLive(const BasicBlock *BB) { |
Chris Lattner | f016ea4 | 2002-05-22 17:17:27 +0000 | [diff] [blame] | 86 | DEBUG(cerr << "Terminat Live: " << BB->getTerminator()); |
Chris Lattner | b8259dd | 2001-09-09 22:26:47 +0000 | [diff] [blame] | 87 | markInstructionLive((Instruction*)BB->getTerminator()); |
Chris Lattner | 72f1e99 | 2001-07-08 18:38:36 +0000 | [diff] [blame] | 88 | } |
Chris Lattner | 02e90d5 | 2001-06-30 06:39:11 +0000 | [diff] [blame] | 89 | }; |
| 90 | |
Chris Lattner | a6275cc | 2002-07-26 21:12:46 +0000 | [diff] [blame] | 91 | RegisterOpt<ADCE> X("adce", "Aggressive Dead Code Elimination"); |
Chris Lattner | dfe81ab | 2002-05-06 17:27:57 +0000 | [diff] [blame] | 92 | } // End of anonymous namespace |
| 93 | |
Chris Lattner | d9036a1 | 2002-05-22 21:32:16 +0000 | [diff] [blame] | 94 | Pass *createAggressiveDCEPass() { return new ADCE(); } |
| 95 | |
Chris Lattner | d9036a1 | 2002-05-22 21:32:16 +0000 | [diff] [blame] | 96 | void ADCE::markBlockAlive(BasicBlock *BB) { |
| 97 | // Mark the basic block as being newly ALIVE... and mark all branches that |
| 98 | // this block is control dependant on as being alive also... |
| 99 | // |
Chris Lattner | ce6ef11 | 2002-07-26 18:40:14 +0000 | [diff] [blame] | 100 | PostDominanceFrontier &CDG = getAnalysis<PostDominanceFrontier>(); |
Chris Lattner | d9036a1 | 2002-05-22 21:32:16 +0000 | [diff] [blame] | 101 | |
Chris Lattner | ce6ef11 | 2002-07-26 18:40:14 +0000 | [diff] [blame] | 102 | PostDominanceFrontier::const_iterator It = CDG.find(BB); |
Chris Lattner | d9036a1 | 2002-05-22 21:32:16 +0000 | [diff] [blame] | 103 | if (It != CDG.end()) { |
| 104 | // Get the blocks that this node is control dependant on... |
Chris Lattner | ce6ef11 | 2002-07-26 18:40:14 +0000 | [diff] [blame] | 105 | const PostDominanceFrontier::DomSetType &CDB = It->second; |
Chris Lattner | d9036a1 | 2002-05-22 21:32:16 +0000 | [diff] [blame] | 106 | for_each(CDB.begin(), CDB.end(), // Mark all their terminators as live |
| 107 | bind_obj(this, &ADCE::markTerminatorLive)); |
| 108 | } |
| 109 | |
| 110 | // If this basic block is live, then the terminator must be as well! |
| 111 | markTerminatorLive(BB); |
Chris Lattner | dfe81ab | 2002-05-06 17:27:57 +0000 | [diff] [blame] | 112 | } |
Chris Lattner | 02e90d5 | 2001-06-30 06:39:11 +0000 | [diff] [blame] | 113 | |
Chris Lattner | 446698b | 2002-07-30 00:22:34 +0000 | [diff] [blame] | 114 | // dropReferencesOfDeadInstructionsInLiveBlock - Loop over all of the |
| 115 | // instructions in the specified basic block, dropping references on |
| 116 | // instructions that are dead according to LiveSet. |
| 117 | bool ADCE::dropReferencesOfDeadInstructionsInLiveBlock(BasicBlock *BB) { |
| 118 | bool Changed = false; |
| 119 | for (BasicBlock::iterator I = BB->begin(), E = --BB->end(); I != E; ) |
| 120 | if (!LiveSet.count(I)) { // Is this instruction alive? |
| 121 | I->dropAllReferences(); // Nope, drop references... |
| 122 | if (PHINode *PN = dyn_cast<PHINode>(&*I)) { |
| 123 | // We don't want to leave PHI nodes in the program that have |
| 124 | // #arguments != #predecessors, so we remove them now. |
| 125 | // |
| 126 | PN->replaceAllUsesWith(Constant::getNullValue(PN->getType())); |
| 127 | |
| 128 | // Delete the instruction... |
| 129 | I = BB->getInstList().erase(I); |
| 130 | Changed = true; |
| 131 | } else { |
| 132 | ++I; |
| 133 | } |
| 134 | } else { |
| 135 | ++I; |
| 136 | } |
| 137 | return Changed; |
| 138 | } |
| 139 | |
Chris Lattner | 02e90d5 | 2001-06-30 06:39:11 +0000 | [diff] [blame] | 140 | |
Chris Lattner | ea54ab9 | 2002-05-07 22:11:39 +0000 | [diff] [blame] | 141 | // doADCE() - Run the Aggressive Dead Code Elimination algorithm, returning |
Chris Lattner | f57b845 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 142 | // true if the function was modified. |
Chris Lattner | 02e90d5 | 2001-06-30 06:39:11 +0000 | [diff] [blame] | 143 | // |
Chris Lattner | d9036a1 | 2002-05-22 21:32:16 +0000 | [diff] [blame] | 144 | bool ADCE::doADCE() { |
| 145 | bool MadeChanges = false; |
Chris Lattner | b8259dd | 2001-09-09 22:26:47 +0000 | [diff] [blame] | 146 | |
Chris Lattner | f57b845 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 147 | // Iterate over all of the instructions in the function, eliminating trivially |
Chris Lattner | b8259dd | 2001-09-09 22:26:47 +0000 | [diff] [blame] | 148 | // dead instructions, and marking instructions live that are known to be |
| 149 | // needed. Perform the walk in depth first order so that we avoid marking any |
| 150 | // instructions live in basic blocks that are unreachable. These blocks will |
| 151 | // be eliminated later, along with the instructions inside. |
| 152 | // |
Chris Lattner | dfe81ab | 2002-05-06 17:27:57 +0000 | [diff] [blame] | 153 | for (df_iterator<Function*> BBI = df_begin(Func), BBE = df_end(Func); |
Chris Lattner | b8259dd | 2001-09-09 22:26:47 +0000 | [diff] [blame] | 154 | BBI != BBE; ++BBI) { |
| 155 | BasicBlock *BB = *BBI; |
| 156 | for (BasicBlock::iterator II = BB->begin(), EI = BB->end(); II != EI; ) { |
Chris Lattner | f0a93ed | 2003-02-24 20:48:32 +0000 | [diff] [blame^] | 157 | if (II->mayWriteToMemory() || II->getOpcode() == Instruction::Ret) { |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 158 | markInstructionLive(II); |
Chris Lattner | ea54ab9 | 2002-05-07 22:11:39 +0000 | [diff] [blame] | 159 | ++II; // Increment the inst iterator if the inst wasn't deleted |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 160 | } else if (isInstructionTriviallyDead(II)) { |
Chris Lattner | ea54ab9 | 2002-05-07 22:11:39 +0000 | [diff] [blame] | 161 | // Remove the instruction from it's basic block... |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 162 | II = BB->getInstList().erase(II); |
Chris Lattner | d9036a1 | 2002-05-22 21:32:16 +0000 | [diff] [blame] | 163 | ++NumInstRemoved; |
Chris Lattner | ea54ab9 | 2002-05-07 22:11:39 +0000 | [diff] [blame] | 164 | MadeChanges = true; |
Chris Lattner | b8259dd | 2001-09-09 22:26:47 +0000 | [diff] [blame] | 165 | } else { |
Chris Lattner | ea54ab9 | 2002-05-07 22:11:39 +0000 | [diff] [blame] | 166 | ++II; // Increment the inst iterator if the inst wasn't deleted |
Chris Lattner | b8259dd | 2001-09-09 22:26:47 +0000 | [diff] [blame] | 167 | } |
Chris Lattner | b8259dd | 2001-09-09 22:26:47 +0000 | [diff] [blame] | 168 | } |
| 169 | } |
| 170 | |
Chris Lattner | f016ea4 | 2002-05-22 17:17:27 +0000 | [diff] [blame] | 171 | DEBUG(cerr << "Processing work list\n"); |
Chris Lattner | 02e90d5 | 2001-06-30 06:39:11 +0000 | [diff] [blame] | 172 | |
Chris Lattner | 72f1e99 | 2001-07-08 18:38:36 +0000 | [diff] [blame] | 173 | // AliveBlocks - Set of basic blocks that we know have instructions that are |
| 174 | // alive in them... |
| 175 | // |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 176 | std::set<BasicBlock*> AliveBlocks; |
Chris Lattner | 72f1e99 | 2001-07-08 18:38:36 +0000 | [diff] [blame] | 177 | |
Chris Lattner | 02e90d5 | 2001-06-30 06:39:11 +0000 | [diff] [blame] | 178 | // Process the work list of instructions that just became live... if they |
| 179 | // became live, then that means that all of their operands are neccesary as |
| 180 | // well... make them live as well. |
| 181 | // |
| 182 | while (!WorkList.empty()) { |
Chris Lattner | 72f1e99 | 2001-07-08 18:38:36 +0000 | [diff] [blame] | 183 | Instruction *I = WorkList.back(); // Get an instruction that became live... |
Chris Lattner | 02e90d5 | 2001-06-30 06:39:11 +0000 | [diff] [blame] | 184 | WorkList.pop_back(); |
| 185 | |
Chris Lattner | 72f1e99 | 2001-07-08 18:38:36 +0000 | [diff] [blame] | 186 | BasicBlock *BB = I->getParent(); |
Chris Lattner | d9036a1 | 2002-05-22 21:32:16 +0000 | [diff] [blame] | 187 | if (!AliveBlocks.count(BB)) { // Basic block not alive yet... |
| 188 | AliveBlocks.insert(BB); // Block is now ALIVE! |
| 189 | markBlockAlive(BB); // Make it so now! |
Chris Lattner | 72f1e99 | 2001-07-08 18:38:36 +0000 | [diff] [blame] | 190 | } |
| 191 | |
Chris Lattner | d9036a1 | 2002-05-22 21:32:16 +0000 | [diff] [blame] | 192 | // PHI nodes are a special case, because the incoming values are actually |
| 193 | // defined in the predecessor nodes of this block, meaning that the PHI |
| 194 | // makes the predecessors alive. |
| 195 | // |
| 196 | if (PHINode *PN = dyn_cast<PHINode>(I)) |
| 197 | for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB); PI != PE; ++PI) |
| 198 | if (!AliveBlocks.count(*PI)) { |
| 199 | AliveBlocks.insert(BB); // Block is now ALIVE! |
| 200 | markBlockAlive(*PI); |
| 201 | } |
| 202 | |
Chris Lattner | b8259dd | 2001-09-09 22:26:47 +0000 | [diff] [blame] | 203 | // Loop over all of the operands of the live instruction, making sure that |
| 204 | // they are known to be alive as well... |
| 205 | // |
Chris Lattner | ea54ab9 | 2002-05-07 22:11:39 +0000 | [diff] [blame] | 206 | for (unsigned op = 0, End = I->getNumOperands(); op != End; ++op) |
Chris Lattner | 9636a91 | 2001-10-01 16:18:37 +0000 | [diff] [blame] | 207 | if (Instruction *Operand = dyn_cast<Instruction>(I->getOperand(op))) |
Chris Lattner | b8259dd | 2001-09-09 22:26:47 +0000 | [diff] [blame] | 208 | markInstructionLive(Operand); |
Chris Lattner | 02e90d5 | 2001-06-30 06:39:11 +0000 | [diff] [blame] | 209 | } |
| 210 | |
Chris Lattner | f016ea4 | 2002-05-22 17:17:27 +0000 | [diff] [blame] | 211 | if (DebugFlag) { |
| 212 | cerr << "Current Function: X = Live\n"; |
| 213 | for (Function::iterator I = Func->begin(), E = Func->end(); I != E; ++I) |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 214 | for (BasicBlock::iterator BI = I->begin(), BE = I->end(); BI != BE; ++BI){ |
| 215 | if (LiveSet.count(BI)) cerr << "X "; |
Chris Lattner | f016ea4 | 2002-05-22 17:17:27 +0000 | [diff] [blame] | 216 | cerr << *BI; |
| 217 | } |
| 218 | } |
Chris Lattner | b8259dd | 2001-09-09 22:26:47 +0000 | [diff] [blame] | 219 | |
Chris Lattner | d9036a1 | 2002-05-22 21:32:16 +0000 | [diff] [blame] | 220 | // Find the first postdominator of the entry node that is alive. Make it the |
| 221 | // new entry node... |
Chris Lattner | 02e90d5 | 2001-06-30 06:39:11 +0000 | [diff] [blame] | 222 | // |
Chris Lattner | ce6ef11 | 2002-07-26 18:40:14 +0000 | [diff] [blame] | 223 | PostDominatorTree &DT = getAnalysis<PostDominatorTree>(); |
Chris Lattner | 02e90d5 | 2001-06-30 06:39:11 +0000 | [diff] [blame] | 224 | |
Chris Lattner | 446698b | 2002-07-30 00:22:34 +0000 | [diff] [blame] | 225 | |
| 226 | if (AliveBlocks.size() == Func->size()) { // No dead blocks? |
| 227 | for (Function::iterator I = Func->begin(), E = Func->end(); I != E; ++I) |
| 228 | // Loop over all of the instructions in the function, telling dead |
| 229 | // instructions to drop their references. This is so that the next sweep |
| 230 | // over the program can safely delete dead instructions without other dead |
| 231 | // instructions still refering to them. |
| 232 | // |
| 233 | dropReferencesOfDeadInstructionsInLiveBlock(I); |
| 234 | |
| 235 | } else { // If there are some blocks dead... |
Chris Lattner | db6e4d6 | 2002-08-14 21:35:02 +0000 | [diff] [blame] | 236 | // If the entry node is dead, insert a new entry node to eliminate the entry |
| 237 | // node as a special case. |
| 238 | // |
| 239 | if (!AliveBlocks.count(&Func->front())) { |
| 240 | BasicBlock *NewEntry = new BasicBlock(); |
Chris Lattner | 9e6161c | 2002-09-10 23:46:10 +0000 | [diff] [blame] | 241 | NewEntry->getInstList().push_back(new BranchInst(&Func->front())); |
Chris Lattner | db6e4d6 | 2002-08-14 21:35:02 +0000 | [diff] [blame] | 242 | Func->getBasicBlockList().push_front(NewEntry); |
| 243 | AliveBlocks.insert(NewEntry); // This block is always alive! |
| 244 | } |
Chris Lattner | d9036a1 | 2002-05-22 21:32:16 +0000 | [diff] [blame] | 245 | |
| 246 | // Loop over all of the alive blocks in the function. If any successor |
| 247 | // blocks are not alive, we adjust the outgoing branches to branch to the |
| 248 | // first live postdominator of the live block, adjusting any PHI nodes in |
| 249 | // the block to reflect this. |
| 250 | // |
| 251 | for (Function::iterator I = Func->begin(), E = Func->end(); I != E; ++I) |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 252 | if (AliveBlocks.count(I)) { |
| 253 | BasicBlock *BB = I; |
Chris Lattner | d9036a1 | 2002-05-22 21:32:16 +0000 | [diff] [blame] | 254 | TerminatorInst *TI = BB->getTerminator(); |
| 255 | |
Chris Lattner | 011de07 | 2002-07-29 22:31:39 +0000 | [diff] [blame] | 256 | // Loop over all of the successors, looking for ones that are not alive. |
| 257 | // We cannot save the number of successors in the terminator instruction |
| 258 | // here because we may remove them if we don't have a postdominator... |
| 259 | // |
| 260 | for (unsigned i = 0; i != TI->getNumSuccessors(); ++i) |
Chris Lattner | d9036a1 | 2002-05-22 21:32:16 +0000 | [diff] [blame] | 261 | if (!AliveBlocks.count(TI->getSuccessor(i))) { |
| 262 | // Scan up the postdominator tree, looking for the first |
| 263 | // postdominator that is alive, and the last postdominator that is |
| 264 | // dead... |
| 265 | // |
Chris Lattner | ce6ef11 | 2002-07-26 18:40:14 +0000 | [diff] [blame] | 266 | PostDominatorTree::Node *LastNode = DT[TI->getSuccessor(i)]; |
Chris Lattner | 011de07 | 2002-07-29 22:31:39 +0000 | [diff] [blame] | 267 | |
| 268 | // There is a special case here... if there IS no post-dominator for |
| 269 | // the block we have no owhere to point our branch to. Instead, |
| 270 | // convert it to a return. This can only happen if the code |
| 271 | // branched into an infinite loop. Note that this may not be |
| 272 | // desirable, because we _are_ altering the behavior of the code. |
| 273 | // This is a well known drawback of ADCE, so in the future if we |
| 274 | // choose to revisit the decision, this is where it should be. |
Chris Lattner | d9036a1 | 2002-05-22 21:32:16 +0000 | [diff] [blame] | 275 | // |
Chris Lattner | 011de07 | 2002-07-29 22:31:39 +0000 | [diff] [blame] | 276 | if (LastNode == 0) { // No postdominator! |
| 277 | // Call RemoveSuccessor to transmogrify the terminator instruction |
| 278 | // to not contain the outgoing branch, or to create a new |
| 279 | // terminator if the form fundementally changes (ie unconditional |
| 280 | // branch to return). Note that this will change a branch into an |
| 281 | // infinite loop into a return instruction! |
| 282 | // |
| 283 | RemoveSuccessor(TI, i); |
| 284 | |
| 285 | // RemoveSuccessor may replace TI... make sure we have a fresh |
| 286 | // pointer... and e variable. |
| 287 | // |
| 288 | TI = BB->getTerminator(); |
| 289 | |
| 290 | // Rescan this successor... |
| 291 | --i; |
| 292 | } else { |
| 293 | PostDominatorTree::Node *NextNode = LastNode->getIDom(); |
| 294 | |
| 295 | while (!AliveBlocks.count(NextNode->getNode())) { |
| 296 | LastNode = NextNode; |
| 297 | NextNode = NextNode->getIDom(); |
| 298 | } |
| 299 | |
| 300 | // Get the basic blocks that we need... |
| 301 | BasicBlock *LastDead = LastNode->getNode(); |
| 302 | BasicBlock *NextAlive = NextNode->getNode(); |
| 303 | |
| 304 | // Make the conditional branch now go to the next alive block... |
| 305 | TI->getSuccessor(i)->removePredecessor(BB); |
| 306 | TI->setSuccessor(i, NextAlive); |
| 307 | |
| 308 | // If there are PHI nodes in NextAlive, we need to add entries to |
| 309 | // the PHI nodes for the new incoming edge. The incoming values |
| 310 | // should be identical to the incoming values for LastDead. |
| 311 | // |
| 312 | for (BasicBlock::iterator II = NextAlive->begin(); |
| 313 | PHINode *PN = dyn_cast<PHINode>(&*II); ++II) { |
| 314 | // Get the incoming value for LastDead... |
| 315 | int OldIdx = PN->getBasicBlockIndex(LastDead); |
| 316 | assert(OldIdx != -1 && "LastDead is not a pred of NextAlive!"); |
| 317 | Value *InVal = PN->getIncomingValue(OldIdx); |
| 318 | |
| 319 | // Add an incoming value for BB now... |
| 320 | PN->addIncoming(InVal, BB); |
| 321 | } |
Chris Lattner | d9036a1 | 2002-05-22 21:32:16 +0000 | [diff] [blame] | 322 | } |
| 323 | } |
Chris Lattner | 5554727 | 2002-05-10 15:37:35 +0000 | [diff] [blame] | 324 | |
Chris Lattner | d9036a1 | 2002-05-22 21:32:16 +0000 | [diff] [blame] | 325 | // Now loop over all of the instructions in the basic block, telling |
| 326 | // dead instructions to drop their references. This is so that the next |
| 327 | // sweep over the program can safely delete dead instructions without |
| 328 | // other dead instructions still refering to them. |
| 329 | // |
Chris Lattner | 446698b | 2002-07-30 00:22:34 +0000 | [diff] [blame] | 330 | dropReferencesOfDeadInstructionsInLiveBlock(BB); |
Chris Lattner | d9036a1 | 2002-05-22 21:32:16 +0000 | [diff] [blame] | 331 | } |
Chris Lattner | b8259dd | 2001-09-09 22:26:47 +0000 | [diff] [blame] | 332 | } |
| 333 | |
Chris Lattner | d7f268d | 2003-01-23 02:12:18 +0000 | [diff] [blame] | 334 | // We make changes if there are any dead blocks in the function... |
| 335 | if (unsigned NumDeadBlocks = Func->size() - AliveBlocks.size()) { |
| 336 | MadeChanges = true; |
| 337 | NumBlockRemoved += NumDeadBlocks; |
| 338 | } |
| 339 | |
| 340 | // Loop over all of the basic blocks in the function, removing control flow |
| 341 | // edges to live blocks (also eliminating any entries in PHI functions in |
| 342 | // referenced blocks). |
Chris Lattner | d9036a1 | 2002-05-22 21:32:16 +0000 | [diff] [blame] | 343 | // |
Chris Lattner | d7f268d | 2003-01-23 02:12:18 +0000 | [diff] [blame] | 344 | for (Function::iterator BB = Func->begin(), E = Func->end(); BB != E; ++BB) |
Chris Lattner | 84369b3 | 2002-05-28 21:38:16 +0000 | [diff] [blame] | 345 | if (!AliveBlocks.count(BB)) { |
Chris Lattner | d9036a1 | 2002-05-22 21:32:16 +0000 | [diff] [blame] | 346 | // Remove all outgoing edges from this basic block and convert the |
| 347 | // terminator into a return instruction. |
| 348 | vector<BasicBlock*> Succs(succ_begin(BB), succ_end(BB)); |
| 349 | |
| 350 | if (!Succs.empty()) { |
| 351 | // Loop over all of the successors, removing this block from PHI node |
| 352 | // entries that might be in the block... |
| 353 | while (!Succs.empty()) { |
| 354 | Succs.back()->removePredecessor(BB); |
| 355 | Succs.pop_back(); |
| 356 | } |
| 357 | |
| 358 | // Delete the old terminator instruction... |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 359 | BB->getInstList().pop_back(); |
Chris Lattner | d9036a1 | 2002-05-22 21:32:16 +0000 | [diff] [blame] | 360 | const Type *RetTy = Func->getReturnType(); |
Chris Lattner | 9e6161c | 2002-09-10 23:46:10 +0000 | [diff] [blame] | 361 | BB->getInstList().push_back(new ReturnInst(RetTy != Type::VoidTy ? |
| 362 | Constant::getNullValue(RetTy) : 0)); |
Chris Lattner | d9036a1 | 2002-05-22 21:32:16 +0000 | [diff] [blame] | 363 | } |
Chris Lattner | d9036a1 | 2002-05-22 21:32:16 +0000 | [diff] [blame] | 364 | } |
Chris Lattner | d7f268d | 2003-01-23 02:12:18 +0000 | [diff] [blame] | 365 | |
| 366 | |
| 367 | // Loop over all of the basic blocks in the function, dropping references of |
| 368 | // the dead basic blocks. We must do this after the previous step to avoid |
| 369 | // dropping references to PHIs which still have entries... |
| 370 | // |
| 371 | for (Function::iterator BB = Func->begin(), E = Func->end(); BB != E; ++BB) |
| 372 | if (!AliveBlocks.count(BB)) |
| 373 | BB->dropAllReferences(); |
Chris Lattner | 5554727 | 2002-05-10 15:37:35 +0000 | [diff] [blame] | 374 | |
Chris Lattner | 84369b3 | 2002-05-28 21:38:16 +0000 | [diff] [blame] | 375 | // Now loop through all of the blocks and delete the dead ones. We can safely |
| 376 | // do this now because we know that there are no references to dead blocks |
| 377 | // (because they have dropped all of their references... we also remove dead |
| 378 | // instructions from alive blocks. |
Chris Lattner | b8259dd | 2001-09-09 22:26:47 +0000 | [diff] [blame] | 379 | // |
Chris Lattner | d9036a1 | 2002-05-22 21:32:16 +0000 | [diff] [blame] | 380 | for (Function::iterator BI = Func->begin(); BI != Func->end(); ) |
Chris Lattner | 446698b | 2002-07-30 00:22:34 +0000 | [diff] [blame] | 381 | if (!AliveBlocks.count(BI)) { // Delete dead blocks... |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 382 | BI = Func->getBasicBlockList().erase(BI); |
Chris Lattner | 446698b | 2002-07-30 00:22:34 +0000 | [diff] [blame] | 383 | } else { // Scan alive blocks... |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 384 | for (BasicBlock::iterator II = BI->begin(); II != --BI->end(); ) |
| 385 | if (!LiveSet.count(II)) { // Is this instruction alive? |
Chris Lattner | 84369b3 | 2002-05-28 21:38:16 +0000 | [diff] [blame] | 386 | // Nope... remove the instruction from it's basic block... |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 387 | II = BI->getInstList().erase(II); |
Chris Lattner | 84369b3 | 2002-05-28 21:38:16 +0000 | [diff] [blame] | 388 | ++NumInstRemoved; |
| 389 | MadeChanges = true; |
| 390 | } else { |
| 391 | ++II; |
| 392 | } |
| 393 | |
Chris Lattner | d9036a1 | 2002-05-22 21:32:16 +0000 | [diff] [blame] | 394 | ++BI; // Increment iterator... |
Chris Lattner | 84369b3 | 2002-05-28 21:38:16 +0000 | [diff] [blame] | 395 | } |
Chris Lattner | 5554727 | 2002-05-10 15:37:35 +0000 | [diff] [blame] | 396 | |
Chris Lattner | d9036a1 | 2002-05-22 21:32:16 +0000 | [diff] [blame] | 397 | return MadeChanges; |
Chris Lattner | 02e90d5 | 2001-06-30 06:39:11 +0000 | [diff] [blame] | 398 | } |