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 | 6806f56 | 2003-08-01 22:15:03 +0000 | [diff] [blame] | 18 | #include "Support/Debug.h" |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 19 | #include "Support/DepthFirstIterator.h" |
Chris Lattner | a92f696 | 2002-10-01 22:38:41 +0000 | [diff] [blame] | 20 | #include "Support/Statistic.h" |
Chris Lattner | 6806f56 | 2003-08-01 22:15:03 +0000 | [diff] [blame] | 21 | #include "Support/STLExtras.h" |
Chris Lattner | 72f1e99 | 2001-07-08 18:38:36 +0000 | [diff] [blame] | 22 | #include <algorithm> |
Chris Lattner | 02e90d5 | 2001-06-30 06:39:11 +0000 | [diff] [blame] | 23 | |
Chris Lattner | dfe81ab | 2002-05-06 17:27:57 +0000 | [diff] [blame] | 24 | namespace { |
Chris Lattner | a92f696 | 2002-10-01 22:38:41 +0000 | [diff] [blame] | 25 | Statistic<> NumBlockRemoved("adce", "Number of basic blocks removed"); |
| 26 | Statistic<> NumInstRemoved ("adce", "Number of instructions removed"); |
Chris Lattner | dfe81ab | 2002-05-06 17:27:57 +0000 | [diff] [blame] | 27 | |
Chris Lattner | 02e90d5 | 2001-06-30 06:39:11 +0000 | [diff] [blame] | 28 | //===----------------------------------------------------------------------===// |
| 29 | // ADCE Class |
| 30 | // |
Chris Lattner | ea54ab9 | 2002-05-07 22:11:39 +0000 | [diff] [blame] | 31 | // This class does all of the work of Aggressive Dead Code Elimination. |
Chris Lattner | 02e90d5 | 2001-06-30 06:39:11 +0000 | [diff] [blame] | 32 | // It's public interface consists of a constructor and a doADCE() method. |
| 33 | // |
Chris Lattner | dfe81ab | 2002-05-06 17:27:57 +0000 | [diff] [blame] | 34 | class ADCE : public FunctionPass { |
| 35 | Function *Func; // The function that we are working on |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 36 | std::vector<Instruction*> WorkList; // Instructions that just became live |
| 37 | std::set<Instruction*> LiveSet; // The set of live instructions |
Chris Lattner | 02e90d5 | 2001-06-30 06:39:11 +0000 | [diff] [blame] | 38 | |
| 39 | //===--------------------------------------------------------------------===// |
| 40 | // The public interface for this class |
| 41 | // |
| 42 | public: |
Chris Lattner | d9036a1 | 2002-05-22 21:32:16 +0000 | [diff] [blame] | 43 | // Execute the Aggressive Dead Code Elimination Algorithm |
Chris Lattner | dfe81ab | 2002-05-06 17:27:57 +0000 | [diff] [blame] | 44 | // |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 45 | virtual bool runOnFunction(Function &F) { |
| 46 | Func = &F; |
Chris Lattner | d9036a1 | 2002-05-22 21:32:16 +0000 | [diff] [blame] | 47 | bool Changed = doADCE(); |
Chris Lattner | dfe81ab | 2002-05-06 17:27:57 +0000 | [diff] [blame] | 48 | assert(WorkList.empty()); |
| 49 | LiveSet.clear(); |
Chris Lattner | d9036a1 | 2002-05-22 21:32:16 +0000 | [diff] [blame] | 50 | return Changed; |
Chris Lattner | dfe81ab | 2002-05-06 17:27:57 +0000 | [diff] [blame] | 51 | } |
| 52 | // getAnalysisUsage - We require post dominance frontiers (aka Control |
| 53 | // Dependence Graph) |
| 54 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
Chris Lattner | 5f0eb8d | 2002-08-08 19:01:30 +0000 | [diff] [blame] | 55 | AU.addRequired<PostDominatorTree>(); |
| 56 | AU.addRequired<PostDominanceFrontier>(); |
Chris Lattner | dfe81ab | 2002-05-06 17:27:57 +0000 | [diff] [blame] | 57 | } |
Chris Lattner | 02e90d5 | 2001-06-30 06:39:11 +0000 | [diff] [blame] | 58 | |
Chris Lattner | 02e90d5 | 2001-06-30 06:39:11 +0000 | [diff] [blame] | 59 | |
| 60 | //===--------------------------------------------------------------------===// |
| 61 | // The implementation of this class |
| 62 | // |
| 63 | private: |
Chris Lattner | ea54ab9 | 2002-05-07 22:11:39 +0000 | [diff] [blame] | 64 | // doADCE() - Run the Aggressive Dead Code Elimination algorithm, returning |
Chris Lattner | dfe81ab | 2002-05-06 17:27:57 +0000 | [diff] [blame] | 65 | // true if the function was modified. |
| 66 | // |
Chris Lattner | d9036a1 | 2002-05-22 21:32:16 +0000 | [diff] [blame] | 67 | bool doADCE(); |
| 68 | |
| 69 | void markBlockAlive(BasicBlock *BB); |
Chris Lattner | dfe81ab | 2002-05-06 17:27:57 +0000 | [diff] [blame] | 70 | |
Chris Lattner | 446698b | 2002-07-30 00:22:34 +0000 | [diff] [blame] | 71 | |
| 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); |
| 76 | |
Chris Lattner | 837e42c | 2003-06-24 23:02:45 +0000 | [diff] [blame] | 77 | TerminatorInst *convertToUnconditionalBranch(TerminatorInst *TI); |
| 78 | |
Chris Lattner | 02e90d5 | 2001-06-30 06:39:11 +0000 | [diff] [blame] | 79 | inline void markInstructionLive(Instruction *I) { |
| 80 | if (LiveSet.count(I)) return; |
Chris Lattner | de579f1 | 2003-05-22 22:00:07 +0000 | [diff] [blame] | 81 | DEBUG(std::cerr << "Insn Live: " << I); |
Chris Lattner | 02e90d5 | 2001-06-30 06:39:11 +0000 | [diff] [blame] | 82 | LiveSet.insert(I); |
| 83 | WorkList.push_back(I); |
| 84 | } |
| 85 | |
Chris Lattner | 72f1e99 | 2001-07-08 18:38:36 +0000 | [diff] [blame] | 86 | inline void markTerminatorLive(const BasicBlock *BB) { |
Chris Lattner | de579f1 | 2003-05-22 22:00:07 +0000 | [diff] [blame] | 87 | DEBUG(std::cerr << "Terminat Live: " << BB->getTerminator()); |
Chris Lattner | b8259dd | 2001-09-09 22:26:47 +0000 | [diff] [blame] | 88 | markInstructionLive((Instruction*)BB->getTerminator()); |
Chris Lattner | 72f1e99 | 2001-07-08 18:38:36 +0000 | [diff] [blame] | 89 | } |
Chris Lattner | 02e90d5 | 2001-06-30 06:39:11 +0000 | [diff] [blame] | 90 | }; |
| 91 | |
Chris Lattner | a6275cc | 2002-07-26 21:12:46 +0000 | [diff] [blame] | 92 | RegisterOpt<ADCE> X("adce", "Aggressive Dead Code Elimination"); |
Chris Lattner | dfe81ab | 2002-05-06 17:27:57 +0000 | [diff] [blame] | 93 | } // End of anonymous namespace |
| 94 | |
Chris Lattner | d9036a1 | 2002-05-22 21:32:16 +0000 | [diff] [blame] | 95 | Pass *createAggressiveDCEPass() { return new ADCE(); } |
| 96 | |
Chris Lattner | d9036a1 | 2002-05-22 21:32:16 +0000 | [diff] [blame] | 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 dependant on as being alive also... |
| 100 | // |
Chris Lattner | ce6ef11 | 2002-07-26 18:40:14 +0000 | [diff] [blame] | 101 | PostDominanceFrontier &CDG = getAnalysis<PostDominanceFrontier>(); |
Chris Lattner | d9036a1 | 2002-05-22 21:32:16 +0000 | [diff] [blame] | 102 | |
Chris Lattner | ce6ef11 | 2002-07-26 18:40:14 +0000 | [diff] [blame] | 103 | PostDominanceFrontier::const_iterator It = CDG.find(BB); |
Chris Lattner | d9036a1 | 2002-05-22 21:32:16 +0000 | [diff] [blame] | 104 | if (It != CDG.end()) { |
| 105 | // Get the blocks that this node is control dependant on... |
Chris Lattner | ce6ef11 | 2002-07-26 18:40:14 +0000 | [diff] [blame] | 106 | const PostDominanceFrontier::DomSetType &CDB = It->second; |
Chris Lattner | d9036a1 | 2002-05-22 21:32:16 +0000 | [diff] [blame] | 107 | for_each(CDB.begin(), CDB.end(), // Mark all their terminators as live |
| 108 | bind_obj(this, &ADCE::markTerminatorLive)); |
| 109 | } |
| 110 | |
Chris Lattner | 99c91e0 | 2003-06-24 21:49:45 +0000 | [diff] [blame] | 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); |
Chris Lattner | dfe81ab | 2002-05-06 17:27:57 +0000 | [diff] [blame] | 116 | } |
Chris Lattner | 02e90d5 | 2001-06-30 06:39:11 +0000 | [diff] [blame] | 117 | |
Chris Lattner | 446698b | 2002-07-30 00:22:34 +0000 | [diff] [blame] | 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... |
Chris Lattner | e408e25 | 2003-04-23 16:37:45 +0000 | [diff] [blame] | 126 | if (PHINode *PN = dyn_cast<PHINode>(I)) { |
Chris Lattner | 446698b | 2002-07-30 00:22:34 +0000 | [diff] [blame] | 127 | // We don't want to leave PHI nodes in the program that have |
| 128 | // #arguments != #predecessors, so we remove them now. |
| 129 | // |
| 130 | PN->replaceAllUsesWith(Constant::getNullValue(PN->getType())); |
| 131 | |
| 132 | // Delete the instruction... |
| 133 | I = BB->getInstList().erase(I); |
| 134 | Changed = true; |
| 135 | } else { |
| 136 | ++I; |
| 137 | } |
| 138 | } else { |
| 139 | ++I; |
| 140 | } |
| 141 | return Changed; |
| 142 | } |
| 143 | |
Chris Lattner | 02e90d5 | 2001-06-30 06:39:11 +0000 | [diff] [blame] | 144 | |
Chris Lattner | 837e42c | 2003-06-24 23:02:45 +0000 | [diff] [blame] | 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. |
| 148 | /// |
| 149 | TerminatorInst *ADCE::convertToUnconditionalBranch(TerminatorInst *TI) { |
| 150 | BranchInst *NB = new BranchInst(TI->getSuccessor(0), TI); |
| 151 | BasicBlock *BB = TI->getParent(); |
| 152 | |
| 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); |
| 156 | |
| 157 | // Delete the old branch itself... |
| 158 | BB->getInstList().erase(TI); |
| 159 | return NB; |
| 160 | } |
| 161 | |
| 162 | |
Chris Lattner | ea54ab9 | 2002-05-07 22:11:39 +0000 | [diff] [blame] | 163 | // doADCE() - Run the Aggressive Dead Code Elimination algorithm, returning |
Chris Lattner | f57b845 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 164 | // true if the function was modified. |
Chris Lattner | 02e90d5 | 2001-06-30 06:39:11 +0000 | [diff] [blame] | 165 | // |
Chris Lattner | d9036a1 | 2002-05-22 21:32:16 +0000 | [diff] [blame] | 166 | bool ADCE::doADCE() { |
| 167 | bool MadeChanges = false; |
Chris Lattner | b8259dd | 2001-09-09 22:26:47 +0000 | [diff] [blame] | 168 | |
Chris Lattner | f57b845 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 169 | // Iterate over all of the instructions in the function, eliminating trivially |
Chris Lattner | b8259dd | 2001-09-09 22:26:47 +0000 | [diff] [blame] | 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. |
| 174 | // |
Chris Lattner | dfe81ab | 2002-05-06 17:27:57 +0000 | [diff] [blame] | 175 | for (df_iterator<Function*> BBI = df_begin(Func), BBE = df_end(Func); |
Chris Lattner | b8259dd | 2001-09-09 22:26:47 +0000 | [diff] [blame] | 176 | BBI != BBE; ++BBI) { |
| 177 | BasicBlock *BB = *BBI; |
| 178 | for (BasicBlock::iterator II = BB->begin(), EI = BB->end(); II != EI; ) { |
Chris Lattner | f0a93ed | 2003-02-24 20:48:32 +0000 | [diff] [blame] | 179 | if (II->mayWriteToMemory() || II->getOpcode() == Instruction::Ret) { |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 180 | markInstructionLive(II); |
Chris Lattner | ea54ab9 | 2002-05-07 22:11:39 +0000 | [diff] [blame] | 181 | ++II; // Increment the inst iterator if the inst wasn't deleted |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 182 | } else if (isInstructionTriviallyDead(II)) { |
Chris Lattner | ea54ab9 | 2002-05-07 22:11:39 +0000 | [diff] [blame] | 183 | // Remove the instruction from it's basic block... |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 184 | II = BB->getInstList().erase(II); |
Chris Lattner | d9036a1 | 2002-05-22 21:32:16 +0000 | [diff] [blame] | 185 | ++NumInstRemoved; |
Chris Lattner | ea54ab9 | 2002-05-07 22:11:39 +0000 | [diff] [blame] | 186 | MadeChanges = true; |
Chris Lattner | b8259dd | 2001-09-09 22:26:47 +0000 | [diff] [blame] | 187 | } else { |
Chris Lattner | ea54ab9 | 2002-05-07 22:11:39 +0000 | [diff] [blame] | 188 | ++II; // Increment the inst iterator if the inst wasn't deleted |
Chris Lattner | b8259dd | 2001-09-09 22:26:47 +0000 | [diff] [blame] | 189 | } |
Chris Lattner | b8259dd | 2001-09-09 22:26:47 +0000 | [diff] [blame] | 190 | } |
| 191 | } |
| 192 | |
Chris Lattner | 34e353e | 2003-06-16 12:10:45 +0000 | [diff] [blame] | 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. |
| 196 | // |
| 197 | PostDominatorTree &DT = getAnalysis<PostDominatorTree>(); |
| 198 | if (DT[&Func->getEntryNode()] == 0) { |
| 199 | WorkList.clear(); |
| 200 | return MadeChanges; |
| 201 | } |
| 202 | |
Chris Lattner | de579f1 | 2003-05-22 22:00:07 +0000 | [diff] [blame] | 203 | DEBUG(std::cerr << "Processing work list\n"); |
Chris Lattner | 02e90d5 | 2001-06-30 06:39:11 +0000 | [diff] [blame] | 204 | |
Chris Lattner | 72f1e99 | 2001-07-08 18:38:36 +0000 | [diff] [blame] | 205 | // AliveBlocks - Set of basic blocks that we know have instructions that are |
| 206 | // alive in them... |
| 207 | // |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 208 | std::set<BasicBlock*> AliveBlocks; |
Chris Lattner | 72f1e99 | 2001-07-08 18:38:36 +0000 | [diff] [blame] | 209 | |
Chris Lattner | 02e90d5 | 2001-06-30 06:39:11 +0000 | [diff] [blame] | 210 | // Process the work list of instructions that just became live... if they |
Misha Brukman | 5560c9d | 2003-08-18 14:43:39 +0000 | [diff] [blame] | 211 | // became live, then that means that all of their operands are necessary as |
Chris Lattner | 02e90d5 | 2001-06-30 06:39:11 +0000 | [diff] [blame] | 212 | // well... make them live as well. |
| 213 | // |
| 214 | while (!WorkList.empty()) { |
Chris Lattner | 72f1e99 | 2001-07-08 18:38:36 +0000 | [diff] [blame] | 215 | Instruction *I = WorkList.back(); // Get an instruction that became live... |
Chris Lattner | 02e90d5 | 2001-06-30 06:39:11 +0000 | [diff] [blame] | 216 | WorkList.pop_back(); |
| 217 | |
Chris Lattner | 72f1e99 | 2001-07-08 18:38:36 +0000 | [diff] [blame] | 218 | BasicBlock *BB = I->getParent(); |
Chris Lattner | d9036a1 | 2002-05-22 21:32:16 +0000 | [diff] [blame] | 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! |
Chris Lattner | 72f1e99 | 2001-07-08 18:38:36 +0000 | [diff] [blame] | 222 | } |
| 223 | |
Chris Lattner | d9036a1 | 2002-05-22 21:32:16 +0000 | [diff] [blame] | 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. |
| 227 | // |
| 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! |
| 232 | markBlockAlive(*PI); |
| 233 | } |
| 234 | |
Chris Lattner | b8259dd | 2001-09-09 22:26:47 +0000 | [diff] [blame] | 235 | // Loop over all of the operands of the live instruction, making sure that |
| 236 | // they are known to be alive as well... |
| 237 | // |
Chris Lattner | ea54ab9 | 2002-05-07 22:11:39 +0000 | [diff] [blame] | 238 | for (unsigned op = 0, End = I->getNumOperands(); op != End; ++op) |
Chris Lattner | 9636a91 | 2001-10-01 16:18:37 +0000 | [diff] [blame] | 239 | if (Instruction *Operand = dyn_cast<Instruction>(I->getOperand(op))) |
Chris Lattner | b8259dd | 2001-09-09 22:26:47 +0000 | [diff] [blame] | 240 | markInstructionLive(Operand); |
Chris Lattner | 02e90d5 | 2001-06-30 06:39:11 +0000 | [diff] [blame] | 241 | } |
| 242 | |
Chris Lattner | de579f1 | 2003-05-22 22:00:07 +0000 | [diff] [blame] | 243 | DEBUG( |
| 244 | std::cerr << "Current Function: X = Live\n"; |
Chris Lattner | 34e353e | 2003-06-16 12:10:45 +0000 | [diff] [blame] | 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"); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 248 | for (BasicBlock::iterator BI = I->begin(), BE = I->end(); BI != BE; ++BI){ |
Chris Lattner | de579f1 | 2003-05-22 22:00:07 +0000 | [diff] [blame] | 249 | if (LiveSet.count(BI)) std::cerr << "X "; |
| 250 | std::cerr << *BI; |
Chris Lattner | f016ea4 | 2002-05-22 17:17:27 +0000 | [diff] [blame] | 251 | } |
Chris Lattner | 34e353e | 2003-06-16 12:10:45 +0000 | [diff] [blame] | 252 | }); |
Chris Lattner | b8259dd | 2001-09-09 22:26:47 +0000 | [diff] [blame] | 253 | |
Chris Lattner | d9036a1 | 2002-05-22 21:32:16 +0000 | [diff] [blame] | 254 | // Find the first postdominator of the entry node that is alive. Make it the |
| 255 | // new entry node... |
Chris Lattner | 02e90d5 | 2001-06-30 06:39:11 +0000 | [diff] [blame] | 256 | // |
Chris Lattner | 446698b | 2002-07-30 00:22:34 +0000 | [diff] [blame] | 257 | if (AliveBlocks.size() == Func->size()) { // No dead blocks? |
Chris Lattner | 837e42c | 2003-06-24 23:02:45 +0000 | [diff] [blame] | 258 | for (Function::iterator I = Func->begin(), E = Func->end(); I != E; ++I) { |
Chris Lattner | 446698b | 2002-07-30 00:22:34 +0000 | [diff] [blame] | 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. |
| 263 | // |
| 264 | dropReferencesOfDeadInstructionsInLiveBlock(I); |
Chris Lattner | 837e42c | 2003-06-24 23:02:45 +0000 | [diff] [blame] | 265 | |
| 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. |
| 272 | // |
| 273 | TerminatorInst *TI = I->getTerminator(); |
| 274 | if (!LiveSet.count(TI)) |
| 275 | convertToUnconditionalBranch(TI); |
| 276 | } |
Chris Lattner | 446698b | 2002-07-30 00:22:34 +0000 | [diff] [blame] | 277 | |
| 278 | } else { // If there are some blocks dead... |
Chris Lattner | db6e4d6 | 2002-08-14 21:35:02 +0000 | [diff] [blame] | 279 | // If the entry node is dead, insert a new entry node to eliminate the entry |
| 280 | // node as a special case. |
| 281 | // |
| 282 | if (!AliveBlocks.count(&Func->front())) { |
| 283 | BasicBlock *NewEntry = new BasicBlock(); |
Chris Lattner | 9e6161c | 2002-09-10 23:46:10 +0000 | [diff] [blame] | 284 | NewEntry->getInstList().push_back(new BranchInst(&Func->front())); |
Chris Lattner | db6e4d6 | 2002-08-14 21:35:02 +0000 | [diff] [blame] | 285 | Func->getBasicBlockList().push_front(NewEntry); |
| 286 | AliveBlocks.insert(NewEntry); // This block is always alive! |
Chris Lattner | 99c91e0 | 2003-06-24 21:49:45 +0000 | [diff] [blame] | 287 | LiveSet.insert(NewEntry->getTerminator()); // The branch is live |
Chris Lattner | db6e4d6 | 2002-08-14 21:35:02 +0000 | [diff] [blame] | 288 | } |
Chris Lattner | d9036a1 | 2002-05-22 21:32:16 +0000 | [diff] [blame] | 289 | |
| 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. |
| 294 | // |
| 295 | for (Function::iterator I = Func->begin(), E = Func->end(); I != E; ++I) |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 296 | if (AliveBlocks.count(I)) { |
| 297 | BasicBlock *BB = I; |
Chris Lattner | d9036a1 | 2002-05-22 21:32:16 +0000 | [diff] [blame] | 298 | TerminatorInst *TI = BB->getTerminator(); |
| 299 | |
Chris Lattner | 99c91e0 | 2003-06-24 21:49:45 +0000 | [diff] [blame] | 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. |
Chris Lattner | 837e42c | 2003-06-24 23:02:45 +0000 | [diff] [blame] | 305 | if (!LiveSet.count(TI)) |
| 306 | TI = convertToUnconditionalBranch(TI); |
Chris Lattner | 99c91e0 | 2003-06-24 21:49:45 +0000 | [diff] [blame] | 307 | |
Chris Lattner | 011de07 | 2002-07-29 22:31:39 +0000 | [diff] [blame] | 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... |
| 311 | // |
| 312 | for (unsigned i = 0; i != TI->getNumSuccessors(); ++i) |
Chris Lattner | d9036a1 | 2002-05-22 21:32:16 +0000 | [diff] [blame] | 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 |
| 316 | // dead... |
| 317 | // |
Chris Lattner | ce6ef11 | 2002-07-26 18:40:14 +0000 | [diff] [blame] | 318 | PostDominatorTree::Node *LastNode = DT[TI->getSuccessor(i)]; |
Chris Lattner | 011de07 | 2002-07-29 22:31:39 +0000 | [diff] [blame] | 319 | |
| 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. |
Chris Lattner | d9036a1 | 2002-05-22 21:32:16 +0000 | [diff] [blame] | 327 | // |
Chris Lattner | 011de07 | 2002-07-29 22:31:39 +0000 | [diff] [blame] | 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! |
| 334 | // |
| 335 | RemoveSuccessor(TI, i); |
| 336 | |
| 337 | // RemoveSuccessor may replace TI... make sure we have a fresh |
| 338 | // pointer... and e variable. |
| 339 | // |
| 340 | TI = BB->getTerminator(); |
| 341 | |
| 342 | // Rescan this successor... |
| 343 | --i; |
| 344 | } else { |
| 345 | PostDominatorTree::Node *NextNode = LastNode->getIDom(); |
| 346 | |
| 347 | while (!AliveBlocks.count(NextNode->getNode())) { |
| 348 | LastNode = NextNode; |
| 349 | NextNode = NextNode->getIDom(); |
| 350 | } |
| 351 | |
| 352 | // Get the basic blocks that we need... |
| 353 | BasicBlock *LastDead = LastNode->getNode(); |
| 354 | BasicBlock *NextAlive = NextNode->getNode(); |
| 355 | |
| 356 | // Make the conditional branch now go to the next alive block... |
| 357 | TI->getSuccessor(i)->removePredecessor(BB); |
| 358 | TI->setSuccessor(i, NextAlive); |
| 359 | |
| 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. |
| 363 | // |
| 364 | for (BasicBlock::iterator II = NextAlive->begin(); |
Chris Lattner | 619f825 | 2003-04-25 22:53:27 +0000 | [diff] [blame] | 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); |
| 371 | |
| 372 | // Add an incoming value for BB now... |
| 373 | PN->addIncoming(InVal, BB); |
| 374 | } |
Chris Lattner | d9036a1 | 2002-05-22 21:32:16 +0000 | [diff] [blame] | 375 | } |
| 376 | } |
Chris Lattner | 5554727 | 2002-05-10 15:37:35 +0000 | [diff] [blame] | 377 | |
Chris Lattner | d9036a1 | 2002-05-22 21:32:16 +0000 | [diff] [blame] | 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. |
| 382 | // |
Chris Lattner | 446698b | 2002-07-30 00:22:34 +0000 | [diff] [blame] | 383 | dropReferencesOfDeadInstructionsInLiveBlock(BB); |
Chris Lattner | d9036a1 | 2002-05-22 21:32:16 +0000 | [diff] [blame] | 384 | } |
Chris Lattner | b8259dd | 2001-09-09 22:26:47 +0000 | [diff] [blame] | 385 | } |
| 386 | |
Chris Lattner | d7f268d | 2003-01-23 02:12:18 +0000 | [diff] [blame] | 387 | // We make changes if there are any dead blocks in the function... |
| 388 | if (unsigned NumDeadBlocks = Func->size() - AliveBlocks.size()) { |
| 389 | MadeChanges = true; |
| 390 | NumBlockRemoved += NumDeadBlocks; |
| 391 | } |
| 392 | |
| 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). |
Chris Lattner | d9036a1 | 2002-05-22 21:32:16 +0000 | [diff] [blame] | 396 | // |
Chris Lattner | d7f268d | 2003-01-23 02:12:18 +0000 | [diff] [blame] | 397 | for (Function::iterator BB = Func->begin(), E = Func->end(); BB != E; ++BB) |
Chris Lattner | 84369b3 | 2002-05-28 21:38:16 +0000 | [diff] [blame] | 398 | if (!AliveBlocks.count(BB)) { |
Chris Lattner | d9036a1 | 2002-05-22 21:32:16 +0000 | [diff] [blame] | 399 | // Remove all outgoing edges from this basic block and convert the |
| 400 | // terminator into a return instruction. |
Chris Lattner | de579f1 | 2003-05-22 22:00:07 +0000 | [diff] [blame] | 401 | std::vector<BasicBlock*> Succs(succ_begin(BB), succ_end(BB)); |
Chris Lattner | d9036a1 | 2002-05-22 21:32:16 +0000 | [diff] [blame] | 402 | |
| 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); |
| 408 | Succs.pop_back(); |
| 409 | } |
| 410 | |
| 411 | // Delete the old terminator instruction... |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 412 | BB->getInstList().pop_back(); |
Chris Lattner | d9036a1 | 2002-05-22 21:32:16 +0000 | [diff] [blame] | 413 | const Type *RetTy = Func->getReturnType(); |
Chris Lattner | 9e6161c | 2002-09-10 23:46:10 +0000 | [diff] [blame] | 414 | BB->getInstList().push_back(new ReturnInst(RetTy != Type::VoidTy ? |
| 415 | Constant::getNullValue(RetTy) : 0)); |
Chris Lattner | d9036a1 | 2002-05-22 21:32:16 +0000 | [diff] [blame] | 416 | } |
Chris Lattner | d9036a1 | 2002-05-22 21:32:16 +0000 | [diff] [blame] | 417 | } |
Chris Lattner | d7f268d | 2003-01-23 02:12:18 +0000 | [diff] [blame] | 418 | |
| 419 | |
| 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... |
| 423 | // |
| 424 | for (Function::iterator BB = Func->begin(), E = Func->end(); BB != E; ++BB) |
| 425 | if (!AliveBlocks.count(BB)) |
| 426 | BB->dropAllReferences(); |
Chris Lattner | 5554727 | 2002-05-10 15:37:35 +0000 | [diff] [blame] | 427 | |
Chris Lattner | 84369b3 | 2002-05-28 21:38:16 +0000 | [diff] [blame] | 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. |
Chris Lattner | b8259dd | 2001-09-09 22:26:47 +0000 | [diff] [blame] | 432 | // |
Chris Lattner | d9036a1 | 2002-05-22 21:32:16 +0000 | [diff] [blame] | 433 | for (Function::iterator BI = Func->begin(); BI != Func->end(); ) |
Chris Lattner | 446698b | 2002-07-30 00:22:34 +0000 | [diff] [blame] | 434 | if (!AliveBlocks.count(BI)) { // Delete dead blocks... |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 435 | BI = Func->getBasicBlockList().erase(BI); |
Chris Lattner | 446698b | 2002-07-30 00:22:34 +0000 | [diff] [blame] | 436 | } else { // Scan alive blocks... |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 437 | for (BasicBlock::iterator II = BI->begin(); II != --BI->end(); ) |
| 438 | if (!LiveSet.count(II)) { // Is this instruction alive? |
Chris Lattner | 84369b3 | 2002-05-28 21:38:16 +0000 | [diff] [blame] | 439 | // Nope... remove the instruction from it's basic block... |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 440 | II = BI->getInstList().erase(II); |
Chris Lattner | 84369b3 | 2002-05-28 21:38:16 +0000 | [diff] [blame] | 441 | ++NumInstRemoved; |
| 442 | MadeChanges = true; |
| 443 | } else { |
| 444 | ++II; |
| 445 | } |
| 446 | |
Chris Lattner | d9036a1 | 2002-05-22 21:32:16 +0000 | [diff] [blame] | 447 | ++BI; // Increment iterator... |
Chris Lattner | 84369b3 | 2002-05-28 21:38:16 +0000 | [diff] [blame] | 448 | } |
Chris Lattner | 5554727 | 2002-05-10 15:37:35 +0000 | [diff] [blame] | 449 | |
Chris Lattner | d9036a1 | 2002-05-22 21:32:16 +0000 | [diff] [blame] | 450 | return MadeChanges; |
Chris Lattner | 02e90d5 | 2001-06-30 06:39:11 +0000 | [diff] [blame] | 451 | } |