Chris Lattner | b28986f | 2001-06-30 06:39:11 +0000 | [diff] [blame] | 1 | //===- ADCE.cpp - Code to perform agressive dead code elimination ---------===// |
| 2 | // |
| 3 | // This file implements "agressive" 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. |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
Chris Lattner | ee965ab | 2002-01-21 23:17:48 +0000 | [diff] [blame] | 9 | #include "llvm/Transforms/Scalar/DCE.h" |
Chris Lattner | b28986f | 2001-06-30 06:39:11 +0000 | [diff] [blame] | 10 | #include "llvm/Instruction.h" |
| 11 | #include "llvm/Type.h" |
Chris Lattner | 8024bde | 2001-07-06 16:32:07 +0000 | [diff] [blame] | 12 | #include "llvm/Analysis/Dominators.h" |
Chris Lattner | 8024bde | 2001-07-06 16:32:07 +0000 | [diff] [blame] | 13 | #include "llvm/Analysis/Writer.h" |
Chris Lattner | acfd27d | 2001-09-09 22:26:47 +0000 | [diff] [blame] | 14 | #include "llvm/iTerminators.h" |
Chris Lattner | fb5ae02 | 2001-12-03 18:02:31 +0000 | [diff] [blame] | 15 | #include "llvm/iPHINode.h" |
Chris Lattner | 5de2204 | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 16 | #include "Support/STLExtras.h" |
| 17 | #include "Support/DepthFirstIterator.h" |
Chris Lattner | fb8ed0c | 2001-07-08 18:38:36 +0000 | [diff] [blame] | 18 | #include <algorithm> |
Chris Lattner | 7f74a56 | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 19 | #include <iostream> |
| 20 | using std::cerr; |
Chris Lattner | b28986f | 2001-06-30 06:39:11 +0000 | [diff] [blame] | 21 | |
Chris Lattner | b271be3 | 2001-09-28 00:06:42 +0000 | [diff] [blame] | 22 | #define DEBUG_ADCE 1 |
Chris Lattner | acfd27d | 2001-09-09 22:26:47 +0000 | [diff] [blame] | 23 | |
Chris Lattner | b28986f | 2001-06-30 06:39:11 +0000 | [diff] [blame] | 24 | //===----------------------------------------------------------------------===// |
| 25 | // ADCE Class |
| 26 | // |
| 27 | // This class does all of the work of Agressive Dead Code Elimination. |
| 28 | // It's public interface consists of a constructor and a doADCE() method. |
| 29 | // |
| 30 | class ADCE { |
| 31 | Method *M; // The method that we are working on... |
Chris Lattner | 7f74a56 | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 32 | std::vector<Instruction*> WorkList; // Instructions that just became live |
| 33 | std::set<Instruction*> LiveSet; // The set of live instructions |
Chris Lattner | acfd27d | 2001-09-09 22:26:47 +0000 | [diff] [blame] | 34 | bool MadeChanges; |
Chris Lattner | b28986f | 2001-06-30 06:39:11 +0000 | [diff] [blame] | 35 | |
| 36 | //===--------------------------------------------------------------------===// |
| 37 | // The public interface for this class |
| 38 | // |
| 39 | public: |
| 40 | // ADCE Ctor - Save the method to operate on... |
Chris Lattner | acfd27d | 2001-09-09 22:26:47 +0000 | [diff] [blame] | 41 | inline ADCE(Method *m) : M(m), MadeChanges(false) {} |
Chris Lattner | b28986f | 2001-06-30 06:39:11 +0000 | [diff] [blame] | 42 | |
| 43 | // doADCE() - Run the Agressive Dead Code Elimination algorithm, returning |
| 44 | // true if the method was modified. |
Chris Lattner | d5d5678 | 2002-01-31 00:45:11 +0000 | [diff] [blame] | 45 | bool doADCE(cfg::DominanceFrontier &CDG); |
Chris Lattner | b28986f | 2001-06-30 06:39:11 +0000 | [diff] [blame] | 46 | |
| 47 | //===--------------------------------------------------------------------===// |
| 48 | // The implementation of this class |
| 49 | // |
| 50 | private: |
| 51 | inline void markInstructionLive(Instruction *I) { |
| 52 | if (LiveSet.count(I)) return; |
Chris Lattner | acfd27d | 2001-09-09 22:26:47 +0000 | [diff] [blame] | 53 | #ifdef DEBUG_ADCE |
Chris Lattner | b28986f | 2001-06-30 06:39:11 +0000 | [diff] [blame] | 54 | cerr << "Insn Live: " << I; |
Chris Lattner | acfd27d | 2001-09-09 22:26:47 +0000 | [diff] [blame] | 55 | #endif |
Chris Lattner | b28986f | 2001-06-30 06:39:11 +0000 | [diff] [blame] | 56 | LiveSet.insert(I); |
| 57 | WorkList.push_back(I); |
| 58 | } |
| 59 | |
Chris Lattner | fb8ed0c | 2001-07-08 18:38:36 +0000 | [diff] [blame] | 60 | inline void markTerminatorLive(const BasicBlock *BB) { |
Chris Lattner | acfd27d | 2001-09-09 22:26:47 +0000 | [diff] [blame] | 61 | #ifdef DEBUG_ADCE |
| 62 | cerr << "Terminat Live: " << BB->getTerminator(); |
| 63 | #endif |
| 64 | markInstructionLive((Instruction*)BB->getTerminator()); |
Chris Lattner | fb8ed0c | 2001-07-08 18:38:36 +0000 | [diff] [blame] | 65 | } |
Chris Lattner | acfd27d | 2001-09-09 22:26:47 +0000 | [diff] [blame] | 66 | |
| 67 | // fixupCFG - Walk the CFG in depth first order, eliminating references to |
| 68 | // dead blocks. |
| 69 | // |
Chris Lattner | 7f74a56 | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 70 | BasicBlock *fixupCFG(BasicBlock *Head, std::set<BasicBlock*> &VisitedBlocks, |
| 71 | const std::set<BasicBlock*> &AliveBlocks); |
Chris Lattner | b28986f | 2001-06-30 06:39:11 +0000 | [diff] [blame] | 72 | }; |
| 73 | |
| 74 | |
| 75 | |
| 76 | // doADCE() - Run the Agressive Dead Code Elimination algorithm, returning |
| 77 | // true if the method was modified. |
| 78 | // |
Chris Lattner | d5d5678 | 2002-01-31 00:45:11 +0000 | [diff] [blame] | 79 | bool ADCE::doADCE(cfg::DominanceFrontier &CDG) { |
Chris Lattner | acfd27d | 2001-09-09 22:26:47 +0000 | [diff] [blame] | 80 | #ifdef DEBUG_ADCE |
| 81 | cerr << "Method: " << M; |
| 82 | #endif |
| 83 | |
| 84 | // Iterate over all of the instructions in the method, eliminating trivially |
| 85 | // dead instructions, and marking instructions live that are known to be |
| 86 | // needed. Perform the walk in depth first order so that we avoid marking any |
| 87 | // instructions live in basic blocks that are unreachable. These blocks will |
| 88 | // be eliminated later, along with the instructions inside. |
| 89 | // |
Chris Lattner | bb09a10 | 2001-09-28 22:56:31 +0000 | [diff] [blame] | 90 | for (df_iterator<Method*> BBI = df_begin(M), |
| 91 | BBE = df_end(M); |
Chris Lattner | acfd27d | 2001-09-09 22:26:47 +0000 | [diff] [blame] | 92 | BBI != BBE; ++BBI) { |
| 93 | BasicBlock *BB = *BBI; |
| 94 | for (BasicBlock::iterator II = BB->begin(), EI = BB->end(); II != EI; ) { |
| 95 | Instruction *I = *II; |
| 96 | |
| 97 | if (I->hasSideEffects() || I->getOpcode() == Instruction::Ret) { |
| 98 | markInstructionLive(I); |
| 99 | } else { |
| 100 | // Check to see if anything is trivially dead |
| 101 | if (I->use_size() == 0 && I->getType() != Type::VoidTy) { |
| 102 | // Remove the instruction from it's basic block... |
| 103 | delete BB->getInstList().remove(II); |
| 104 | MadeChanges = true; |
| 105 | continue; // Don't increment the iterator past the current slot |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | ++II; // Increment the inst iterator if the inst wasn't deleted |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | #ifdef DEBUG_ADCE |
Chris Lattner | b28986f | 2001-06-30 06:39:11 +0000 | [diff] [blame] | 114 | cerr << "Processing work list\n"; |
Chris Lattner | acfd27d | 2001-09-09 22:26:47 +0000 | [diff] [blame] | 115 | #endif |
Chris Lattner | b28986f | 2001-06-30 06:39:11 +0000 | [diff] [blame] | 116 | |
Chris Lattner | fb8ed0c | 2001-07-08 18:38:36 +0000 | [diff] [blame] | 117 | // AliveBlocks - Set of basic blocks that we know have instructions that are |
| 118 | // alive in them... |
| 119 | // |
Chris Lattner | 7f74a56 | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 120 | std::set<BasicBlock*> AliveBlocks; |
Chris Lattner | fb8ed0c | 2001-07-08 18:38:36 +0000 | [diff] [blame] | 121 | |
Chris Lattner | b28986f | 2001-06-30 06:39:11 +0000 | [diff] [blame] | 122 | // Process the work list of instructions that just became live... if they |
| 123 | // became live, then that means that all of their operands are neccesary as |
| 124 | // well... make them live as well. |
| 125 | // |
| 126 | while (!WorkList.empty()) { |
Chris Lattner | fb8ed0c | 2001-07-08 18:38:36 +0000 | [diff] [blame] | 127 | Instruction *I = WorkList.back(); // Get an instruction that became live... |
Chris Lattner | b28986f | 2001-06-30 06:39:11 +0000 | [diff] [blame] | 128 | WorkList.pop_back(); |
| 129 | |
Chris Lattner | fb8ed0c | 2001-07-08 18:38:36 +0000 | [diff] [blame] | 130 | BasicBlock *BB = I->getParent(); |
| 131 | if (AliveBlocks.count(BB) == 0) { // Basic block not alive yet... |
| 132 | // Mark the basic block as being newly ALIVE... and mark all branches that |
| 133 | // this block is control dependant on as being alive also... |
| 134 | // |
| 135 | AliveBlocks.insert(BB); // Block is now ALIVE! |
| 136 | cfg::DominanceFrontier::const_iterator It = CDG.find(BB); |
| 137 | if (It != CDG.end()) { |
| 138 | // Get the blocks that this node is control dependant on... |
| 139 | const cfg::DominanceFrontier::DomSetType &CDB = It->second; |
| 140 | for_each(CDB.begin(), CDB.end(), // Mark all their terminators as live |
| 141 | bind_obj(this, &ADCE::markTerminatorLive)); |
| 142 | } |
Chris Lattner | acfd27d | 2001-09-09 22:26:47 +0000 | [diff] [blame] | 143 | |
| 144 | // If this basic block is live, then the terminator must be as well! |
| 145 | markTerminatorLive(BB); |
Chris Lattner | fb8ed0c | 2001-07-08 18:38:36 +0000 | [diff] [blame] | 146 | } |
| 147 | |
Chris Lattner | acfd27d | 2001-09-09 22:26:47 +0000 | [diff] [blame] | 148 | // Loop over all of the operands of the live instruction, making sure that |
| 149 | // they are known to be alive as well... |
| 150 | // |
Chris Lattner | fb8ed0c | 2001-07-08 18:38:36 +0000 | [diff] [blame] | 151 | for (unsigned op = 0, End = I->getNumOperands(); op != End; ++op) { |
Chris Lattner | 4b717c0 | 2001-10-01 16:18:37 +0000 | [diff] [blame] | 152 | if (Instruction *Operand = dyn_cast<Instruction>(I->getOperand(op))) |
Chris Lattner | acfd27d | 2001-09-09 22:26:47 +0000 | [diff] [blame] | 153 | markInstructionLive(Operand); |
Chris Lattner | b28986f | 2001-06-30 06:39:11 +0000 | [diff] [blame] | 154 | } |
| 155 | } |
| 156 | |
Chris Lattner | acfd27d | 2001-09-09 22:26:47 +0000 | [diff] [blame] | 157 | #ifdef DEBUG_ADCE |
| 158 | cerr << "Current Method: X = Live\n"; |
| 159 | for (Method::inst_iterator IL = M->inst_begin(); IL != M->inst_end(); ++IL) { |
| 160 | if (LiveSet.count(*IL)) cerr << "X "; |
| 161 | cerr << *IL; |
| 162 | } |
| 163 | #endif |
| 164 | |
| 165 | // After the worklist is processed, recursively walk the CFG in depth first |
| 166 | // order, patching up references to dead blocks... |
Chris Lattner | b28986f | 2001-06-30 06:39:11 +0000 | [diff] [blame] | 167 | // |
Chris Lattner | 7f74a56 | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 168 | std::set<BasicBlock*> VisitedBlocks; |
Chris Lattner | acfd27d | 2001-09-09 22:26:47 +0000 | [diff] [blame] | 169 | BasicBlock *EntryBlock = fixupCFG(M->front(), VisitedBlocks, AliveBlocks); |
| 170 | if (EntryBlock && EntryBlock != M->front()) { |
Chris Lattner | da55810 | 2001-10-02 03:41:24 +0000 | [diff] [blame] | 171 | if (isa<PHINode>(EntryBlock->front())) { |
Chris Lattner | acfd27d | 2001-09-09 22:26:47 +0000 | [diff] [blame] | 172 | // Cannot make the first block be a block with a PHI node in it! Instead, |
| 173 | // strip the first basic block of the method to contain no instructions, |
| 174 | // then add a simple branch to the "real" entry node... |
| 175 | // |
| 176 | BasicBlock *E = M->front(); |
Chris Lattner | da55810 | 2001-10-02 03:41:24 +0000 | [diff] [blame] | 177 | if (!isa<TerminatorInst>(E->front()) || // Check for an actual change... |
| 178 | cast<TerminatorInst>(E->front())->getNumSuccessors() != 1 || |
| 179 | cast<TerminatorInst>(E->front())->getSuccessor(0) != EntryBlock) { |
Chris Lattner | acfd27d | 2001-09-09 22:26:47 +0000 | [diff] [blame] | 180 | E->getInstList().delete_all(); // Delete all instructions in block |
| 181 | E->getInstList().push_back(new BranchInst(EntryBlock)); |
| 182 | MadeChanges = true; |
| 183 | } |
| 184 | AliveBlocks.insert(E); |
Chris Lattner | b271be3 | 2001-09-28 00:06:42 +0000 | [diff] [blame] | 185 | |
| 186 | // Next we need to change any PHI nodes in the entry block to refer to the |
| 187 | // new predecessor node... |
| 188 | |
| 189 | |
Chris Lattner | acfd27d | 2001-09-09 22:26:47 +0000 | [diff] [blame] | 190 | } else { |
| 191 | // We need to move the new entry block to be the first bb of the method. |
| 192 | Method::iterator EBI = find(M->begin(), M->end(), EntryBlock); |
Chris Lattner | 7f74a56 | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 193 | std::swap(*EBI, *M->begin());// Exchange old location with start of method |
Chris Lattner | acfd27d | 2001-09-09 22:26:47 +0000 | [diff] [blame] | 194 | MadeChanges = true; |
Chris Lattner | b28986f | 2001-06-30 06:39:11 +0000 | [diff] [blame] | 195 | } |
Chris Lattner | b28986f | 2001-06-30 06:39:11 +0000 | [diff] [blame] | 196 | } |
| 197 | |
Chris Lattner | acfd27d | 2001-09-09 22:26:47 +0000 | [diff] [blame] | 198 | // Now go through and tell dead blocks to drop all of their references so they |
| 199 | // can be safely deleted. |
| 200 | // |
| 201 | for (Method::iterator BI = M->begin(), BE = M->end(); BI != BE; ++BI) { |
| 202 | BasicBlock *BB = *BI; |
| 203 | if (!AliveBlocks.count(BB)) { |
| 204 | BB->dropAllReferences(); |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | // Now loop through all of the blocks and delete them. We can safely do this |
| 209 | // now because we know that there are no references to dead blocks (because |
| 210 | // they have dropped all of their references... |
| 211 | // |
| 212 | for (Method::iterator BI = M->begin(); BI != M->end();) { |
| 213 | if (!AliveBlocks.count(*BI)) { |
| 214 | delete M->getBasicBlocks().remove(BI); |
| 215 | MadeChanges = true; |
| 216 | continue; // Don't increment iterator |
| 217 | } |
| 218 | ++BI; // Increment iterator... |
| 219 | } |
| 220 | |
| 221 | return MadeChanges; |
Chris Lattner | b28986f | 2001-06-30 06:39:11 +0000 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | |
Chris Lattner | acfd27d | 2001-09-09 22:26:47 +0000 | [diff] [blame] | 225 | // fixupCFG - Walk the CFG in depth first order, eliminating references to |
| 226 | // dead blocks: |
| 227 | // If the BB is alive (in AliveBlocks): |
| 228 | // 1. Eliminate all dead instructions in the BB |
| 229 | // 2. Recursively traverse all of the successors of the BB: |
| 230 | // - If the returned successor is non-null, update our terminator to |
| 231 | // reference the returned BB |
| 232 | // 3. Return 0 (no update needed) |
| 233 | // |
| 234 | // If the BB is dead (not in AliveBlocks): |
| 235 | // 1. Add the BB to the dead set |
| 236 | // 2. Recursively traverse all of the successors of the block: |
| 237 | // - Only one shall return a nonnull value (or else this block should have |
| 238 | // been in the alive set). |
| 239 | // 3. Return the nonnull child, or 0 if no non-null children. |
| 240 | // |
Chris Lattner | 7f74a56 | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 241 | BasicBlock *ADCE::fixupCFG(BasicBlock *BB, std::set<BasicBlock*> &VisitedBlocks, |
| 242 | const std::set<BasicBlock*> &AliveBlocks) { |
Chris Lattner | acfd27d | 2001-09-09 22:26:47 +0000 | [diff] [blame] | 243 | if (VisitedBlocks.count(BB)) return 0; // Revisiting a node? No update. |
| 244 | VisitedBlocks.insert(BB); // We have now visited this node! |
| 245 | |
| 246 | #ifdef DEBUG_ADCE |
| 247 | cerr << "Fixing up BB: " << BB; |
| 248 | #endif |
| 249 | |
| 250 | if (AliveBlocks.count(BB)) { // Is the block alive? |
| 251 | // Yes it's alive: loop through and eliminate all dead instructions in block |
| 252 | for (BasicBlock::iterator II = BB->begin(); II != BB->end()-1; ) { |
| 253 | Instruction *I = *II; |
| 254 | if (!LiveSet.count(I)) { // Is this instruction alive? |
| 255 | // Nope... remove the instruction from it's basic block... |
| 256 | delete BB->getInstList().remove(II); |
| 257 | MadeChanges = true; |
| 258 | continue; // Don't increment II |
| 259 | } |
| 260 | ++II; |
| 261 | } |
| 262 | |
| 263 | // Recursively traverse successors of this basic block. |
Chris Lattner | ba1c1f2 | 2001-10-01 13:19:53 +0000 | [diff] [blame] | 264 | BasicBlock::succ_iterator SI = BB->succ_begin(), SE = BB->succ_end(); |
Chris Lattner | acfd27d | 2001-09-09 22:26:47 +0000 | [diff] [blame] | 265 | for (; SI != SE; ++SI) { |
| 266 | BasicBlock *Succ = *SI; |
| 267 | BasicBlock *Repl = fixupCFG(Succ, VisitedBlocks, AliveBlocks); |
| 268 | if (Repl && Repl != Succ) { // We have to replace the successor |
| 269 | Succ->replaceAllUsesWith(Repl); |
| 270 | MadeChanges = true; |
| 271 | } |
| 272 | } |
| 273 | return BB; |
| 274 | } else { // Otherwise the block is dead... |
| 275 | BasicBlock *ReturnBB = 0; // Default to nothing live down here |
| 276 | |
| 277 | // Recursively traverse successors of this basic block. |
Chris Lattner | ba1c1f2 | 2001-10-01 13:19:53 +0000 | [diff] [blame] | 278 | BasicBlock::succ_iterator SI = BB->succ_begin(), SE = BB->succ_end(); |
Chris Lattner | acfd27d | 2001-09-09 22:26:47 +0000 | [diff] [blame] | 279 | for (; SI != SE; ++SI) { |
| 280 | BasicBlock *RetBB = fixupCFG(*SI, VisitedBlocks, AliveBlocks); |
| 281 | if (RetBB) { |
| 282 | assert(ReturnBB == 0 && "One one live child allowed!"); |
| 283 | ReturnBB = RetBB; |
| 284 | } |
| 285 | } |
| 286 | return ReturnBB; // Return the result of traversal |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | |
| 291 | |
Chris Lattner | 5398a6e | 2001-10-18 01:32:34 +0000 | [diff] [blame] | 292 | // doADCE - Execute the Agressive Dead Code Elimination Algorithm |
Chris Lattner | b28986f | 2001-06-30 06:39:11 +0000 | [diff] [blame] | 293 | // |
Chris Lattner | d5d5678 | 2002-01-31 00:45:11 +0000 | [diff] [blame] | 294 | bool AgressiveDCE::runOnMethod(Method *M) { |
Chris Lattner | 352151e | 2002-01-31 00:51:24 +0000 | [diff] [blame] | 295 | return ADCE(M).doADCE( |
Chris Lattner | d5d5678 | 2002-01-31 00:45:11 +0000 | [diff] [blame] | 296 | getAnalysis<cfg::DominanceFrontier>(cfg::DominanceFrontier::PostDomID)); |
| 297 | } |
| 298 | |
| 299 | |
| 300 | // getAnalysisUsageInfo - We require post dominance frontiers (aka Control |
| 301 | // Dependence Graph) |
| 302 | // |
| 303 | void AgressiveDCE::getAnalysisUsageInfo(Pass::AnalysisSet &Requires, |
| 304 | Pass::AnalysisSet &Destroyed, |
| 305 | Pass::AnalysisSet &Provided) { |
| 306 | Requires.push_back(cfg::DominanceFrontier::PostDomID); |
Chris Lattner | b28986f | 2001-06-30 06:39:11 +0000 | [diff] [blame] | 307 | } |