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