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