Chris Lattner | 466a049 | 2002-05-21 20:50:24 +0000 | [diff] [blame] | 1 | //===- SimplifyCFG.cpp - Code to perform CFG simplification ---------------===// |
| 2 | // |
| 3 | // SimplifyCFG - This function is used to do simplification of a CFG. For |
| 4 | // example, it adjusts branches to branches to eliminate the extra hop, it |
| 5 | // eliminates unreachable basic blocks, and does other "peephole" optimization |
| 6 | // of the CFG. It returns true if a modification was made, and returns an |
| 7 | // iterator that designates the first element remaining after the block that |
| 8 | // was deleted. |
| 9 | // |
| 10 | // WARNING: The entry node of a function may not be simplified. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "llvm/Transforms/Utils/Local.h" |
| 15 | #include "llvm/Constant.h" |
| 16 | #include "llvm/iPHINode.h" |
| 17 | #include "llvm/Support/CFG.h" |
| 18 | #include <algorithm> |
| 19 | #include <functional> |
| 20 | |
| 21 | // PropogatePredecessors - This gets "Succ" ready to have the predecessors from |
| 22 | // "BB". This is a little tricky because "Succ" has PHI nodes, which need to |
| 23 | // have extra slots added to them to hold the merge edges from BB's |
| 24 | // predecessors. This function returns true (failure) if the Succ BB already |
| 25 | // has a predecessor that is a predecessor of BB. |
| 26 | // |
| 27 | // Assumption: Succ is the single successor for BB. |
| 28 | // |
| 29 | static bool PropogatePredecessorsForPHIs(BasicBlock *BB, BasicBlock *Succ) { |
| 30 | assert(*succ_begin(BB) == Succ && "Succ is not successor of BB!"); |
| 31 | assert(isa<PHINode>(Succ->front()) && "Only works on PHId BBs!"); |
| 32 | |
| 33 | // If there is more than one predecessor, and there are PHI nodes in |
| 34 | // the successor, then we need to add incoming edges for the PHI nodes |
| 35 | // |
| 36 | const std::vector<BasicBlock*> BBPreds(pred_begin(BB), pred_end(BB)); |
| 37 | |
| 38 | // Check to see if one of the predecessors of BB is already a predecessor of |
| 39 | // Succ. If so, we cannot do the transformation! |
| 40 | // |
| 41 | for (pred_iterator PI = pred_begin(Succ), PE = pred_end(Succ); |
| 42 | PI != PE; ++PI) { |
| 43 | if (find(BBPreds.begin(), BBPreds.end(), *PI) != BBPreds.end()) |
| 44 | return true; |
| 45 | } |
| 46 | |
| 47 | // Loop over all of the PHI nodes in the successor BB |
| 48 | for (BasicBlock::iterator I = Succ->begin(); |
Chris Lattner | fda72b1 | 2002-06-25 16:12:52 +0000 | [diff] [blame^] | 49 | PHINode *PN = dyn_cast<PHINode>(&*I); ++I) { |
Chris Lattner | 466a049 | 2002-05-21 20:50:24 +0000 | [diff] [blame] | 50 | Value *OldVal = PN->removeIncomingValue(BB); |
| 51 | assert(OldVal && "No entry in PHI for Pred BB!"); |
| 52 | |
| 53 | for (std::vector<BasicBlock*>::const_iterator PredI = BBPreds.begin(), |
| 54 | End = BBPreds.end(); PredI != End; ++PredI) { |
| 55 | // Add an incoming value for each of the new incoming values... |
| 56 | PN->addIncoming(OldVal, *PredI); |
| 57 | } |
| 58 | } |
| 59 | return false; |
| 60 | } |
| 61 | |
| 62 | |
| 63 | // SimplifyCFG - This function is used to do simplification of a CFG. For |
| 64 | // example, it adjusts branches to branches to eliminate the extra hop, it |
| 65 | // eliminates unreachable basic blocks, and does other "peephole" optimization |
| 66 | // of the CFG. It returns true if a modification was made, and returns an |
| 67 | // iterator that designates the first element remaining after the block that |
| 68 | // was deleted. |
| 69 | // |
| 70 | // WARNING: The entry node of a function may not be simplified. |
| 71 | // |
Chris Lattner | fda72b1 | 2002-06-25 16:12:52 +0000 | [diff] [blame^] | 72 | bool SimplifyCFG(BasicBlock *BB) { |
Chris Lattner | 466a049 | 2002-05-21 20:50:24 +0000 | [diff] [blame] | 73 | Function *M = BB->getParent(); |
| 74 | |
| 75 | assert(BB && BB->getParent() && "Block not embedded in function!"); |
| 76 | assert(BB->getTerminator() && "Degenerate basic block encountered!"); |
Chris Lattner | fda72b1 | 2002-06-25 16:12:52 +0000 | [diff] [blame^] | 77 | assert(&BB->getParent()->front() != BB && "Can't Simplify entry block!"); |
Chris Lattner | 466a049 | 2002-05-21 20:50:24 +0000 | [diff] [blame] | 78 | |
| 79 | |
| 80 | // Remove basic blocks that have no predecessors... which are unreachable. |
| 81 | if (pred_begin(BB) == pred_end(BB) && |
| 82 | !BB->hasConstantReferences()) { |
| 83 | //cerr << "Removing BB: \n" << BB; |
| 84 | |
| 85 | // Loop through all of our successors and make sure they know that one |
| 86 | // of their predecessors is going away. |
| 87 | for_each(succ_begin(BB), succ_end(BB), |
| 88 | std::bind2nd(std::mem_fun(&BasicBlock::removePredecessor), BB)); |
| 89 | |
| 90 | while (!BB->empty()) { |
Chris Lattner | fda72b1 | 2002-06-25 16:12:52 +0000 | [diff] [blame^] | 91 | Instruction &I = BB->back(); |
Chris Lattner | 466a049 | 2002-05-21 20:50:24 +0000 | [diff] [blame] | 92 | // If this instruction is used, replace uses with an arbitrary |
| 93 | // constant value. Because control flow can't get here, we don't care |
| 94 | // what we replace the value with. Note that since this block is |
| 95 | // unreachable, and all values contained within it must dominate their |
| 96 | // uses, that all uses will eventually be removed. |
Chris Lattner | fda72b1 | 2002-06-25 16:12:52 +0000 | [diff] [blame^] | 97 | if (!I.use_empty()) |
Chris Lattner | 466a049 | 2002-05-21 20:50:24 +0000 | [diff] [blame] | 98 | // Make all users of this instruction reference the constant instead |
Chris Lattner | fda72b1 | 2002-06-25 16:12:52 +0000 | [diff] [blame^] | 99 | I.replaceAllUsesWith(Constant::getNullValue(I.getType())); |
Chris Lattner | 466a049 | 2002-05-21 20:50:24 +0000 | [diff] [blame] | 100 | |
| 101 | // Remove the instruction from the basic block |
Chris Lattner | fda72b1 | 2002-06-25 16:12:52 +0000 | [diff] [blame^] | 102 | BB->getInstList().pop_back(); |
Chris Lattner | 466a049 | 2002-05-21 20:50:24 +0000 | [diff] [blame] | 103 | } |
Chris Lattner | fda72b1 | 2002-06-25 16:12:52 +0000 | [diff] [blame^] | 104 | M->getBasicBlockList().erase(BB); |
Chris Lattner | 466a049 | 2002-05-21 20:50:24 +0000 | [diff] [blame] | 105 | return true; |
| 106 | } |
| 107 | |
| 108 | // Check to see if this block has no instructions and only a single |
| 109 | // successor. If so, replace block references with successor. |
| 110 | succ_iterator SI(succ_begin(BB)); |
| 111 | if (SI != succ_end(BB) && ++SI == succ_end(BB)) { // One succ? |
Chris Lattner | fda72b1 | 2002-06-25 16:12:52 +0000 | [diff] [blame^] | 112 | if (BB->front().isTerminator()) { // Terminator is the only instruction! |
Chris Lattner | 466a049 | 2002-05-21 20:50:24 +0000 | [diff] [blame] | 113 | BasicBlock *Succ = *succ_begin(BB); // There is exactly one successor |
| 114 | |
| 115 | if (Succ != BB) { // Arg, don't hurt infinite loops! |
| 116 | // If our successor has PHI nodes, then we need to update them to |
| 117 | // include entries for BB's predecessors, not for BB itself. |
| 118 | // Be careful though, if this transformation fails (returns true) then |
| 119 | // we cannot do this transformation! |
| 120 | // |
| 121 | if (!isa<PHINode>(Succ->front()) || |
| 122 | !PropogatePredecessorsForPHIs(BB, Succ)) { |
| 123 | |
| 124 | //cerr << "Killing Trivial BB: \n" << BB; |
| 125 | |
| 126 | BB->replaceAllUsesWith(Succ); |
Chris Lattner | fda72b1 | 2002-06-25 16:12:52 +0000 | [diff] [blame^] | 127 | std::string OldName = BB->getName(); |
| 128 | |
| 129 | // Delete the old basic block... |
| 130 | M->getBasicBlockList().erase(BB); |
Chris Lattner | 466a049 | 2002-05-21 20:50:24 +0000 | [diff] [blame] | 131 | |
Chris Lattner | fda72b1 | 2002-06-25 16:12:52 +0000 | [diff] [blame^] | 132 | if (!OldName.empty() && !Succ->hasName()) // Transfer name if we can |
| 133 | Succ->setName(OldName); |
Chris Lattner | 466a049 | 2002-05-21 20:50:24 +0000 | [diff] [blame] | 134 | |
| 135 | //cerr << "Function after removal: \n" << M; |
| 136 | return true; |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | // Merge basic blocks into their predecessor if there is only one distinct |
| 143 | // pred, and if there is only one distinct successor of the predecessor, and |
| 144 | // if there are no PHI nodes. |
| 145 | // |
| 146 | if (!isa<PHINode>(BB->front()) && !BB->hasConstantReferences()) { |
| 147 | pred_iterator PI(pred_begin(BB)), PE(pred_end(BB)); |
| 148 | BasicBlock *OnlyPred = *PI++; |
| 149 | for (; PI != PE; ++PI) // Search all predecessors, see if they are all same |
| 150 | if (*PI != OnlyPred) { |
| 151 | OnlyPred = 0; // There are multiple different predecessors... |
| 152 | break; |
| 153 | } |
| 154 | |
| 155 | BasicBlock *OnlySucc = 0; |
| 156 | if (OnlyPred && OnlyPred != BB) { // Don't break self loops |
| 157 | // Check to see if there is only one distinct successor... |
| 158 | succ_iterator SI(succ_begin(OnlyPred)), SE(succ_end(OnlyPred)); |
| 159 | OnlySucc = BB; |
| 160 | for (; SI != SE; ++SI) |
| 161 | if (*SI != OnlySucc) { |
| 162 | OnlySucc = 0; // There are multiple distinct successors! |
| 163 | break; |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | if (OnlySucc) { |
| 168 | //cerr << "Merging: " << BB << "into: " << OnlyPred; |
| 169 | TerminatorInst *Term = OnlyPred->getTerminator(); |
| 170 | |
| 171 | // Delete the unconditional branch from the predecessor... |
Chris Lattner | fda72b1 | 2002-06-25 16:12:52 +0000 | [diff] [blame^] | 172 | OnlyPred->getInstList().pop_back(); |
Chris Lattner | 466a049 | 2002-05-21 20:50:24 +0000 | [diff] [blame] | 173 | |
| 174 | // Move all definitions in the succecessor to the predecessor... |
Chris Lattner | fda72b1 | 2002-06-25 16:12:52 +0000 | [diff] [blame^] | 175 | OnlyPred->getInstList().splice(OnlyPred->end(), BB->getInstList()); |
| 176 | |
Chris Lattner | 466a049 | 2002-05-21 20:50:24 +0000 | [diff] [blame] | 177 | // Make all PHI nodes that refered to BB now refer to Pred as their |
| 178 | // source... |
| 179 | BB->replaceAllUsesWith(OnlyPred); |
Chris Lattner | fda72b1 | 2002-06-25 16:12:52 +0000 | [diff] [blame^] | 180 | |
| 181 | std::string OldName = BB->getName(); |
| 182 | |
| 183 | // Erase basic block from the function... |
| 184 | M->getBasicBlockList().erase(BB); |
| 185 | |
Chris Lattner | 466a049 | 2002-05-21 20:50:24 +0000 | [diff] [blame] | 186 | // Inherit predecessors name if it exists... |
Chris Lattner | fda72b1 | 2002-06-25 16:12:52 +0000 | [diff] [blame^] | 187 | if (!OldName.empty() && !OnlyPred->hasName()) |
| 188 | OnlyPred->setName(OldName); |
Chris Lattner | 466a049 | 2002-05-21 20:50:24 +0000 | [diff] [blame] | 189 | |
Chris Lattner | 466a049 | 2002-05-21 20:50:24 +0000 | [diff] [blame] | 190 | return true; |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | return false; |
| 195 | } |