Chris Lattner | c1496bda | 2002-05-07 22:11:39 +0000 | [diff] [blame] | 1 | //===- ADCE.cpp - Code to perform aggressive dead code elimination --------===// |
Chris Lattner | b28986f | 2001-06-30 06:39:11 +0000 | [diff] [blame] | 2 | // |
Chris Lattner | c1496bda | 2002-05-07 22:11:39 +0000 | [diff] [blame] | 3 | // This file implements "aggressive" dead code elimination. ADCE is DCe where |
Chris Lattner | b28986f | 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 | b4cfa7f | 2002-05-07 20:03:00 +0000 | [diff] [blame] | 9 | #include "llvm/Transforms/Scalar.h" |
Chris Lattner | c1496bda | 2002-05-07 22:11:39 +0000 | [diff] [blame] | 10 | #include "llvm/Transforms/Utils/Local.h" |
Chris Lattner | b28986f | 2001-06-30 06:39:11 +0000 | [diff] [blame] | 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 | 559f0ee | 2002-05-22 21:32:16 +0000 | [diff] [blame] | 16 | #include "llvm/Constant.h" |
Chris Lattner | 60a6591 | 2002-02-12 21:07:25 +0000 | [diff] [blame] | 17 | #include "llvm/Support/CFG.h" |
Chris Lattner | 5de2204 | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 18 | #include "Support/STLExtras.h" |
| 19 | #include "Support/DepthFirstIterator.h" |
Chris Lattner | 71cbd42 | 2002-05-22 17:17:27 +0000 | [diff] [blame] | 20 | #include "Support/StatisticReporter.h" |
Chris Lattner | fb8ed0c | 2001-07-08 18:38:36 +0000 | [diff] [blame] | 21 | #include <algorithm> |
Chris Lattner | 7f74a56 | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 22 | #include <iostream> |
| 23 | using std::cerr; |
Chris Lattner | b28986f | 2001-06-30 06:39:11 +0000 | [diff] [blame] | 24 | |
Chris Lattner | 559f0ee | 2002-05-22 21:32:16 +0000 | [diff] [blame] | 25 | static Statistic<> NumBlockRemoved("adce\t\t- Number of basic blocks removed"); |
| 26 | static Statistic<> NumInstRemoved ("adce\t\t- Number of instructions removed"); |
| 27 | |
Chris Lattner | 019f364 | 2002-05-06 17:27:57 +0000 | [diff] [blame] | 28 | namespace { |
| 29 | |
Chris Lattner | b28986f | 2001-06-30 06:39:11 +0000 | [diff] [blame] | 30 | //===----------------------------------------------------------------------===// |
| 31 | // ADCE Class |
| 32 | // |
Chris Lattner | c1496bda | 2002-05-07 22:11:39 +0000 | [diff] [blame] | 33 | // This class does all of the work of Aggressive Dead Code Elimination. |
Chris Lattner | b28986f | 2001-06-30 06:39:11 +0000 | [diff] [blame] | 34 | // It's public interface consists of a constructor and a doADCE() method. |
| 35 | // |
Chris Lattner | 019f364 | 2002-05-06 17:27:57 +0000 | [diff] [blame] | 36 | class ADCE : public FunctionPass { |
| 37 | Function *Func; // The function that we are working on |
Chris Lattner | 7f74a56 | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 38 | std::vector<Instruction*> WorkList; // Instructions that just became live |
| 39 | std::set<Instruction*> LiveSet; // The set of live instructions |
Chris Lattner | b28986f | 2001-06-30 06:39:11 +0000 | [diff] [blame] | 40 | |
| 41 | //===--------------------------------------------------------------------===// |
| 42 | // The public interface for this class |
| 43 | // |
| 44 | public: |
Chris Lattner | 019f364 | 2002-05-06 17:27:57 +0000 | [diff] [blame] | 45 | const char *getPassName() const { return "Aggressive Dead Code Elimination"; } |
| 46 | |
Chris Lattner | 559f0ee | 2002-05-22 21:32:16 +0000 | [diff] [blame] | 47 | // Execute the Aggressive Dead Code Elimination Algorithm |
Chris Lattner | 019f364 | 2002-05-06 17:27:57 +0000 | [diff] [blame] | 48 | // |
| 49 | virtual bool runOnFunction(Function *F) { |
Chris Lattner | 559f0ee | 2002-05-22 21:32:16 +0000 | [diff] [blame] | 50 | Func = F; |
| 51 | bool Changed = doADCE(); |
Chris Lattner | 019f364 | 2002-05-06 17:27:57 +0000 | [diff] [blame] | 52 | assert(WorkList.empty()); |
| 53 | LiveSet.clear(); |
Chris Lattner | 559f0ee | 2002-05-22 21:32:16 +0000 | [diff] [blame] | 54 | return Changed; |
Chris Lattner | 019f364 | 2002-05-06 17:27:57 +0000 | [diff] [blame] | 55 | } |
| 56 | // getAnalysisUsage - We require post dominance frontiers (aka Control |
| 57 | // Dependence Graph) |
| 58 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
Chris Lattner | 559f0ee | 2002-05-22 21:32:16 +0000 | [diff] [blame] | 59 | AU.addRequired(DominatorTree::PostDomID); |
Chris Lattner | 019f364 | 2002-05-06 17:27:57 +0000 | [diff] [blame] | 60 | AU.addRequired(DominanceFrontier::PostDomID); |
| 61 | } |
Chris Lattner | b28986f | 2001-06-30 06:39:11 +0000 | [diff] [blame] | 62 | |
Chris Lattner | b28986f | 2001-06-30 06:39:11 +0000 | [diff] [blame] | 63 | |
| 64 | //===--------------------------------------------------------------------===// |
| 65 | // The implementation of this class |
| 66 | // |
| 67 | private: |
Chris Lattner | c1496bda | 2002-05-07 22:11:39 +0000 | [diff] [blame] | 68 | // doADCE() - Run the Aggressive Dead Code Elimination algorithm, returning |
Chris Lattner | 019f364 | 2002-05-06 17:27:57 +0000 | [diff] [blame] | 69 | // true if the function was modified. |
| 70 | // |
Chris Lattner | 559f0ee | 2002-05-22 21:32:16 +0000 | [diff] [blame] | 71 | bool doADCE(); |
| 72 | |
| 73 | void markBlockAlive(BasicBlock *BB); |
Chris Lattner | 019f364 | 2002-05-06 17:27:57 +0000 | [diff] [blame] | 74 | |
Chris Lattner | b28986f | 2001-06-30 06:39:11 +0000 | [diff] [blame] | 75 | inline void markInstructionLive(Instruction *I) { |
| 76 | if (LiveSet.count(I)) return; |
Chris Lattner | 71cbd42 | 2002-05-22 17:17:27 +0000 | [diff] [blame] | 77 | DEBUG(cerr << "Insn Live: " << I); |
Chris Lattner | b28986f | 2001-06-30 06:39:11 +0000 | [diff] [blame] | 78 | LiveSet.insert(I); |
| 79 | WorkList.push_back(I); |
| 80 | } |
| 81 | |
Chris Lattner | fb8ed0c | 2001-07-08 18:38:36 +0000 | [diff] [blame] | 82 | inline void markTerminatorLive(const BasicBlock *BB) { |
Chris Lattner | 71cbd42 | 2002-05-22 17:17:27 +0000 | [diff] [blame] | 83 | DEBUG(cerr << "Terminat Live: " << BB->getTerminator()); |
Chris Lattner | acfd27d | 2001-09-09 22:26:47 +0000 | [diff] [blame] | 84 | markInstructionLive((Instruction*)BB->getTerminator()); |
Chris Lattner | fb8ed0c | 2001-07-08 18:38:36 +0000 | [diff] [blame] | 85 | } |
Chris Lattner | b28986f | 2001-06-30 06:39:11 +0000 | [diff] [blame] | 86 | }; |
| 87 | |
Chris Lattner | 019f364 | 2002-05-06 17:27:57 +0000 | [diff] [blame] | 88 | } // End of anonymous namespace |
| 89 | |
Chris Lattner | 559f0ee | 2002-05-22 21:32:16 +0000 | [diff] [blame] | 90 | Pass *createAggressiveDCEPass() { return new ADCE(); } |
| 91 | |
| 92 | |
| 93 | void ADCE::markBlockAlive(BasicBlock *BB) { |
| 94 | // Mark the basic block as being newly ALIVE... and mark all branches that |
| 95 | // this block is control dependant on as being alive also... |
| 96 | // |
| 97 | DominanceFrontier &CDG = |
| 98 | getAnalysis<DominanceFrontier>(DominanceFrontier::PostDomID); |
| 99 | |
| 100 | DominanceFrontier::const_iterator It = CDG.find(BB); |
| 101 | if (It != CDG.end()) { |
| 102 | // Get the blocks that this node is control dependant on... |
| 103 | const DominanceFrontier::DomSetType &CDB = It->second; |
| 104 | for_each(CDB.begin(), CDB.end(), // Mark all their terminators as live |
| 105 | bind_obj(this, &ADCE::markTerminatorLive)); |
| 106 | } |
| 107 | |
| 108 | // If this basic block is live, then the terminator must be as well! |
| 109 | markTerminatorLive(BB); |
Chris Lattner | 019f364 | 2002-05-06 17:27:57 +0000 | [diff] [blame] | 110 | } |
Chris Lattner | b28986f | 2001-06-30 06:39:11 +0000 | [diff] [blame] | 111 | |
| 112 | |
Chris Lattner | c1496bda | 2002-05-07 22:11:39 +0000 | [diff] [blame] | 113 | // doADCE() - Run the Aggressive Dead Code Elimination algorithm, returning |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 114 | // true if the function was modified. |
Chris Lattner | b28986f | 2001-06-30 06:39:11 +0000 | [diff] [blame] | 115 | // |
Chris Lattner | 559f0ee | 2002-05-22 21:32:16 +0000 | [diff] [blame] | 116 | bool ADCE::doADCE() { |
| 117 | bool MadeChanges = false; |
Chris Lattner | acfd27d | 2001-09-09 22:26:47 +0000 | [diff] [blame] | 118 | |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 119 | // Iterate over all of the instructions in the function, eliminating trivially |
Chris Lattner | acfd27d | 2001-09-09 22:26:47 +0000 | [diff] [blame] | 120 | // dead instructions, and marking instructions live that are known to be |
| 121 | // needed. Perform the walk in depth first order so that we avoid marking any |
| 122 | // instructions live in basic blocks that are unreachable. These blocks will |
| 123 | // be eliminated later, along with the instructions inside. |
| 124 | // |
Chris Lattner | 019f364 | 2002-05-06 17:27:57 +0000 | [diff] [blame] | 125 | for (df_iterator<Function*> BBI = df_begin(Func), BBE = df_end(Func); |
Chris Lattner | acfd27d | 2001-09-09 22:26:47 +0000 | [diff] [blame] | 126 | BBI != BBE; ++BBI) { |
| 127 | BasicBlock *BB = *BBI; |
| 128 | for (BasicBlock::iterator II = BB->begin(), EI = BB->end(); II != EI; ) { |
| 129 | Instruction *I = *II; |
| 130 | |
| 131 | if (I->hasSideEffects() || I->getOpcode() == Instruction::Ret) { |
| 132 | markInstructionLive(I); |
Chris Lattner | c1496bda | 2002-05-07 22:11:39 +0000 | [diff] [blame] | 133 | ++II; // Increment the inst iterator if the inst wasn't deleted |
| 134 | } else if (isInstructionTriviallyDead(I)) { |
| 135 | // Remove the instruction from it's basic block... |
| 136 | delete BB->getInstList().remove(II); |
Chris Lattner | 559f0ee | 2002-05-22 21:32:16 +0000 | [diff] [blame] | 137 | ++NumInstRemoved; |
Chris Lattner | c1496bda | 2002-05-07 22:11:39 +0000 | [diff] [blame] | 138 | MadeChanges = true; |
Chris Lattner | acfd27d | 2001-09-09 22:26:47 +0000 | [diff] [blame] | 139 | } else { |
Chris Lattner | c1496bda | 2002-05-07 22:11:39 +0000 | [diff] [blame] | 140 | ++II; // Increment the inst iterator if the inst wasn't deleted |
Chris Lattner | acfd27d | 2001-09-09 22:26:47 +0000 | [diff] [blame] | 141 | } |
Chris Lattner | acfd27d | 2001-09-09 22:26:47 +0000 | [diff] [blame] | 142 | } |
| 143 | } |
| 144 | |
Chris Lattner | 71cbd42 | 2002-05-22 17:17:27 +0000 | [diff] [blame] | 145 | DEBUG(cerr << "Processing work list\n"); |
Chris Lattner | b28986f | 2001-06-30 06:39:11 +0000 | [diff] [blame] | 146 | |
Chris Lattner | fb8ed0c | 2001-07-08 18:38:36 +0000 | [diff] [blame] | 147 | // AliveBlocks - Set of basic blocks that we know have instructions that are |
| 148 | // alive in them... |
| 149 | // |
Chris Lattner | 7f74a56 | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 150 | std::set<BasicBlock*> AliveBlocks; |
Chris Lattner | fb8ed0c | 2001-07-08 18:38:36 +0000 | [diff] [blame] | 151 | |
Chris Lattner | b28986f | 2001-06-30 06:39:11 +0000 | [diff] [blame] | 152 | // Process the work list of instructions that just became live... if they |
| 153 | // became live, then that means that all of their operands are neccesary as |
| 154 | // well... make them live as well. |
| 155 | // |
| 156 | while (!WorkList.empty()) { |
Chris Lattner | fb8ed0c | 2001-07-08 18:38:36 +0000 | [diff] [blame] | 157 | Instruction *I = WorkList.back(); // Get an instruction that became live... |
Chris Lattner | b28986f | 2001-06-30 06:39:11 +0000 | [diff] [blame] | 158 | WorkList.pop_back(); |
| 159 | |
Chris Lattner | fb8ed0c | 2001-07-08 18:38:36 +0000 | [diff] [blame] | 160 | BasicBlock *BB = I->getParent(); |
Chris Lattner | 559f0ee | 2002-05-22 21:32:16 +0000 | [diff] [blame] | 161 | if (!AliveBlocks.count(BB)) { // Basic block not alive yet... |
| 162 | AliveBlocks.insert(BB); // Block is now ALIVE! |
| 163 | markBlockAlive(BB); // Make it so now! |
Chris Lattner | fb8ed0c | 2001-07-08 18:38:36 +0000 | [diff] [blame] | 164 | } |
| 165 | |
Chris Lattner | 559f0ee | 2002-05-22 21:32:16 +0000 | [diff] [blame] | 166 | // PHI nodes are a special case, because the incoming values are actually |
| 167 | // defined in the predecessor nodes of this block, meaning that the PHI |
| 168 | // makes the predecessors alive. |
| 169 | // |
| 170 | if (PHINode *PN = dyn_cast<PHINode>(I)) |
| 171 | for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB); PI != PE; ++PI) |
| 172 | if (!AliveBlocks.count(*PI)) { |
| 173 | AliveBlocks.insert(BB); // Block is now ALIVE! |
| 174 | markBlockAlive(*PI); |
| 175 | } |
| 176 | |
Chris Lattner | acfd27d | 2001-09-09 22:26:47 +0000 | [diff] [blame] | 177 | // Loop over all of the operands of the live instruction, making sure that |
| 178 | // they are known to be alive as well... |
| 179 | // |
Chris Lattner | c1496bda | 2002-05-07 22:11:39 +0000 | [diff] [blame] | 180 | for (unsigned op = 0, End = I->getNumOperands(); op != End; ++op) |
Chris Lattner | 4b717c0 | 2001-10-01 16:18:37 +0000 | [diff] [blame] | 181 | if (Instruction *Operand = dyn_cast<Instruction>(I->getOperand(op))) |
Chris Lattner | acfd27d | 2001-09-09 22:26:47 +0000 | [diff] [blame] | 182 | markInstructionLive(Operand); |
Chris Lattner | b28986f | 2001-06-30 06:39:11 +0000 | [diff] [blame] | 183 | } |
| 184 | |
Chris Lattner | 71cbd42 | 2002-05-22 17:17:27 +0000 | [diff] [blame] | 185 | if (DebugFlag) { |
| 186 | cerr << "Current Function: X = Live\n"; |
| 187 | for (Function::iterator I = Func->begin(), E = Func->end(); I != E; ++I) |
| 188 | for (BasicBlock::iterator BI = (*I)->begin(), BE = (*I)->end(); |
| 189 | BI != BE; ++BI) { |
| 190 | if (LiveSet.count(*BI)) cerr << "X "; |
| 191 | cerr << *BI; |
| 192 | } |
| 193 | } |
Chris Lattner | acfd27d | 2001-09-09 22:26:47 +0000 | [diff] [blame] | 194 | |
Chris Lattner | 559f0ee | 2002-05-22 21:32:16 +0000 | [diff] [blame] | 195 | // Find the first postdominator of the entry node that is alive. Make it the |
| 196 | // new entry node... |
Chris Lattner | b28986f | 2001-06-30 06:39:11 +0000 | [diff] [blame] | 197 | // |
Chris Lattner | 559f0ee | 2002-05-22 21:32:16 +0000 | [diff] [blame] | 198 | DominatorTree &DT = getAnalysis<DominatorTree>(DominatorTree::PostDomID); |
Chris Lattner | b28986f | 2001-06-30 06:39:11 +0000 | [diff] [blame] | 199 | |
Chris Lattner | 559f0ee | 2002-05-22 21:32:16 +0000 | [diff] [blame] | 200 | // If there are some blocks dead... |
| 201 | if (AliveBlocks.size() != Func->size()) { |
| 202 | // Insert a new entry node to eliminate the entry node as a special case. |
| 203 | BasicBlock *NewEntry = new BasicBlock(); |
| 204 | NewEntry->getInstList().push_back(new BranchInst(Func->front())); |
| 205 | Func->getBasicBlocks().push_front(NewEntry); |
| 206 | AliveBlocks.insert(NewEntry); // This block is always alive! |
| 207 | |
| 208 | // Loop over all of the alive blocks in the function. If any successor |
| 209 | // blocks are not alive, we adjust the outgoing branches to branch to the |
| 210 | // first live postdominator of the live block, adjusting any PHI nodes in |
| 211 | // the block to reflect this. |
| 212 | // |
| 213 | for (Function::iterator I = Func->begin(), E = Func->end(); I != E; ++I) |
| 214 | if (AliveBlocks.count(*I)) { |
| 215 | BasicBlock *BB = *I; |
| 216 | TerminatorInst *TI = BB->getTerminator(); |
| 217 | |
| 218 | // Loop over all of the successors, looking for ones that are not alive |
| 219 | for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i) |
| 220 | if (!AliveBlocks.count(TI->getSuccessor(i))) { |
| 221 | // Scan up the postdominator tree, looking for the first |
| 222 | // postdominator that is alive, and the last postdominator that is |
| 223 | // dead... |
| 224 | // |
| 225 | DominatorTree::Node *LastNode = DT[TI->getSuccessor(i)]; |
| 226 | DominatorTree::Node *NextNode = LastNode->getIDom(); |
| 227 | while (!AliveBlocks.count(NextNode->getNode())) { |
| 228 | LastNode = NextNode; |
| 229 | NextNode = NextNode->getIDom(); |
| 230 | } |
| 231 | |
| 232 | // Get the basic blocks that we need... |
| 233 | BasicBlock *LastDead = LastNode->getNode(); |
| 234 | BasicBlock *NextAlive = NextNode->getNode(); |
| 235 | |
| 236 | // Make the conditional branch now go to the next alive block... |
| 237 | TI->getSuccessor(i)->removePredecessor(BB); |
| 238 | TI->setSuccessor(i, NextAlive); |
| 239 | |
| 240 | // If there are PHI nodes in NextAlive, we need to add entries to |
| 241 | // the PHI nodes for the new incoming edge. The incoming values |
| 242 | // should be identical to the incoming values for LastDead. |
| 243 | // |
| 244 | for (BasicBlock::iterator II = NextAlive->begin(); |
| 245 | PHINode *PN = dyn_cast<PHINode>(*II); ++II) { |
| 246 | // Get the incoming value for LastDead... |
| 247 | int OldIdx = PN->getBasicBlockIndex(LastDead); |
| 248 | assert(OldIdx != -1 && "LastDead is not a pred of NextAlive!"); |
| 249 | Value *InVal = PN->getIncomingValue(OldIdx); |
| 250 | |
| 251 | // Add an incoming value for BB now... |
| 252 | PN->addIncoming(InVal, BB); |
| 253 | } |
| 254 | } |
Chris Lattner | bad1b4d | 2002-05-10 15:37:35 +0000 | [diff] [blame] | 255 | |
Chris Lattner | 559f0ee | 2002-05-22 21:32:16 +0000 | [diff] [blame] | 256 | // Now loop over all of the instructions in the basic block, telling |
| 257 | // dead instructions to drop their references. This is so that the next |
| 258 | // sweep over the program can safely delete dead instructions without |
| 259 | // other dead instructions still refering to them. |
| 260 | // |
| 261 | for (BasicBlock::iterator I = BB->begin(), E = BB->end()-1; I != E; ++I) |
| 262 | if (!LiveSet.count(*I)) // Is this instruction alive? |
| 263 | (*I)->dropAllReferences(); // Nope, drop references... |
| 264 | } |
Chris Lattner | acfd27d | 2001-09-09 22:26:47 +0000 | [diff] [blame] | 265 | } |
| 266 | |
Chris Lattner | 194138c | 2002-05-28 21:38:16 +0000 | [diff] [blame^] | 267 | // Loop over all of the basic blocks in the function, dropping references of |
| 268 | // the dead basic blocks |
Chris Lattner | 559f0ee | 2002-05-22 21:32:16 +0000 | [diff] [blame] | 269 | // |
| 270 | for (Function::iterator I = Func->begin(), E = Func->end(); I != E; ++I) { |
| 271 | BasicBlock *BB = *I; |
Chris Lattner | 194138c | 2002-05-28 21:38:16 +0000 | [diff] [blame^] | 272 | if (!AliveBlocks.count(BB)) { |
Chris Lattner | 559f0ee | 2002-05-22 21:32:16 +0000 | [diff] [blame] | 273 | // Remove all outgoing edges from this basic block and convert the |
| 274 | // terminator into a return instruction. |
| 275 | vector<BasicBlock*> Succs(succ_begin(BB), succ_end(BB)); |
| 276 | |
| 277 | if (!Succs.empty()) { |
| 278 | // Loop over all of the successors, removing this block from PHI node |
| 279 | // entries that might be in the block... |
| 280 | while (!Succs.empty()) { |
| 281 | Succs.back()->removePredecessor(BB); |
| 282 | Succs.pop_back(); |
| 283 | } |
| 284 | |
| 285 | // Delete the old terminator instruction... |
| 286 | delete BB->getInstList().remove(BB->end()-1); |
| 287 | const Type *RetTy = Func->getReturnType(); |
| 288 | Instruction *New = new ReturnInst(RetTy != Type::VoidTy ? |
| 289 | Constant::getNullValue(RetTy) : 0); |
| 290 | BB->getInstList().push_back(New); |
| 291 | } |
| 292 | |
| 293 | BB->dropAllReferences(); |
| 294 | ++NumBlockRemoved; |
| 295 | MadeChanges = true; |
| 296 | } |
| 297 | } |
Chris Lattner | bad1b4d | 2002-05-10 15:37:35 +0000 | [diff] [blame] | 298 | |
Chris Lattner | 194138c | 2002-05-28 21:38:16 +0000 | [diff] [blame^] | 299 | // Now loop through all of the blocks and delete the dead ones. We can safely |
| 300 | // do this now because we know that there are no references to dead blocks |
| 301 | // (because they have dropped all of their references... we also remove dead |
| 302 | // instructions from alive blocks. |
Chris Lattner | acfd27d | 2001-09-09 22:26:47 +0000 | [diff] [blame] | 303 | // |
Chris Lattner | 559f0ee | 2002-05-22 21:32:16 +0000 | [diff] [blame] | 304 | for (Function::iterator BI = Func->begin(); BI != Func->end(); ) |
| 305 | if (!AliveBlocks.count(*BI)) |
Chris Lattner | 019f364 | 2002-05-06 17:27:57 +0000 | [diff] [blame] | 306 | delete Func->getBasicBlocks().remove(BI); |
Chris Lattner | 194138c | 2002-05-28 21:38:16 +0000 | [diff] [blame^] | 307 | else { |
| 308 | BasicBlock *BB = *BI; |
| 309 | for (BasicBlock::iterator II = BB->begin(); II != BB->end()-1; ) |
| 310 | if (!LiveSet.count(*II)) { // Is this instruction alive? |
| 311 | // Nope... remove the instruction from it's basic block... |
| 312 | delete BB->getInstList().remove(II); |
| 313 | ++NumInstRemoved; |
| 314 | MadeChanges = true; |
| 315 | } else { |
| 316 | ++II; |
| 317 | } |
| 318 | |
Chris Lattner | 559f0ee | 2002-05-22 21:32:16 +0000 | [diff] [blame] | 319 | ++BI; // Increment iterator... |
Chris Lattner | 194138c | 2002-05-28 21:38:16 +0000 | [diff] [blame^] | 320 | } |
Chris Lattner | bad1b4d | 2002-05-10 15:37:35 +0000 | [diff] [blame] | 321 | |
Chris Lattner | 559f0ee | 2002-05-22 21:32:16 +0000 | [diff] [blame] | 322 | return MadeChanges; |
Chris Lattner | b28986f | 2001-06-30 06:39:11 +0000 | [diff] [blame] | 323 | } |