Chris Lattner | 7a7bef4 | 2003-06-22 20:10:28 +0000 | [diff] [blame] | 1 | //===- TailDuplication.cpp - Simplify CFG through tail duplication --------===// |
| 2 | // |
| 3 | // This pass performs a limited form of tail duplication, intended to simplify |
| 4 | // CFGs by removing some unconditional branches. This pass is necessary to |
| 5 | // straighten out loops created by the C front-end, but also is capable of |
| 6 | // making other code nicer. After this pass is run, the CFG simplify pass |
| 7 | // should be run to clean up the mess. |
| 8 | // |
| 9 | // This pass could be enhanced in the future to use profile information to be |
| 10 | // more aggressive. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "llvm/Transforms/Scalar.h" |
| 15 | #include "llvm/Function.h" |
| 16 | #include "llvm/iPHINode.h" |
| 17 | #include "llvm/iTerminators.h" |
| 18 | #include "llvm/Pass.h" |
| 19 | #include "llvm/Type.h" |
| 20 | #include "llvm/Support/CFG.h" |
| 21 | #include "llvm/Transforms/Utils/Local.h" |
Chris Lattner | 6806f56 | 2003-08-01 22:15:03 +0000 | [diff] [blame^] | 22 | #include "Support/Debug.h" |
Chris Lattner | 7a7bef4 | 2003-06-22 20:10:28 +0000 | [diff] [blame] | 23 | #include "Support/Statistic.h" |
| 24 | |
| 25 | namespace { |
| 26 | Statistic<> NumEliminated("tailduplicate", |
| 27 | "Number of unconditional branches eliminated"); |
| 28 | Statistic<> NumPHINodes("tailduplicate", "Number of phi nodes inserted"); |
| 29 | |
| 30 | class TailDup : public FunctionPass { |
| 31 | bool runOnFunction(Function &F); |
| 32 | private: |
| 33 | inline bool shouldEliminateUnconditionalBranch(TerminatorInst *TI); |
| 34 | inline void eliminateUnconditionalBranch(BranchInst *BI); |
| 35 | inline void InsertPHINodesIfNecessary(Instruction *OrigInst, Value *NewInst, |
| 36 | BasicBlock *NewBlock); |
| 37 | inline Value *GetValueInBlock(BasicBlock *BB, Value *OrigVal, |
| 38 | std::map<BasicBlock*, Value*> &ValueMap, |
| 39 | std::map<BasicBlock*, Value*> &OutValueMap); |
| 40 | inline Value *GetValueOutBlock(BasicBlock *BB, Value *OrigVal, |
| 41 | std::map<BasicBlock*, Value*> &ValueMap, |
| 42 | std::map<BasicBlock*, Value*> &OutValueMap); |
| 43 | }; |
| 44 | RegisterOpt<TailDup> X("tailduplicate", "Tail Duplication"); |
| 45 | } |
| 46 | |
| 47 | Pass *createTailDuplicationPass() { return new TailDup(); } |
| 48 | |
| 49 | /// runOnFunction - Top level algorithm - Loop over each unconditional branch in |
| 50 | /// the function, eliminating it if it looks attractive enough. |
| 51 | /// |
| 52 | bool TailDup::runOnFunction(Function &F) { |
| 53 | bool Changed = false; |
| 54 | for (Function::iterator I = F.begin(), E = F.end(); I != E; ) |
| 55 | if (shouldEliminateUnconditionalBranch(I->getTerminator())) { |
| 56 | eliminateUnconditionalBranch(cast<BranchInst>(I->getTerminator())); |
| 57 | Changed = true; |
| 58 | } else { |
| 59 | ++I; |
| 60 | } |
| 61 | return Changed; |
| 62 | } |
| 63 | |
| 64 | /// shouldEliminateUnconditionalBranch - Return true if this branch looks |
| 65 | /// attractive to eliminate. We eliminate the branch if the destination basic |
| 66 | /// block has <= 5 instructions in it, not counting PHI nodes. In practice, |
| 67 | /// since one of these is a terminator instruction, this means that we will add |
| 68 | /// up to 4 instructions to the new block. |
| 69 | /// |
| 70 | /// We don't count PHI nodes in the count since they will be removed when the |
| 71 | /// contents of the block are copied over. |
| 72 | /// |
| 73 | bool TailDup::shouldEliminateUnconditionalBranch(TerminatorInst *TI) { |
| 74 | BranchInst *BI = dyn_cast<BranchInst>(TI); |
| 75 | if (!BI || !BI->isUnconditional()) return false; // Not an uncond branch! |
| 76 | |
| 77 | BasicBlock *Dest = BI->getSuccessor(0); |
| 78 | if (Dest == BI->getParent()) return false; // Do not loop infinitely! |
| 79 | |
Chris Lattner | 00f185f | 2003-07-23 03:32:41 +0000 | [diff] [blame] | 80 | // Do not inline a block if we will just get another branch to the same block! |
| 81 | if (BranchInst *DBI = dyn_cast<BranchInst>(Dest->getTerminator())) |
| 82 | if (DBI->isUnconditional() && DBI->getSuccessor(0) == Dest) |
| 83 | return false; // Do not loop infinitely! |
| 84 | |
Chris Lattner | 7a7bef4 | 2003-06-22 20:10:28 +0000 | [diff] [blame] | 85 | // Do not bother working on dead blocks... |
| 86 | pred_iterator PI = pred_begin(Dest), PE = pred_end(Dest); |
| 87 | if (PI == PE && Dest != Dest->getParent()->begin()) |
| 88 | return false; // It's just a dead block, ignore it... |
| 89 | |
| 90 | // Also, do not bother with blocks with only a single predecessor: simplify |
| 91 | // CFG will fold these two blocks together! |
| 92 | ++PI; |
| 93 | if (PI == PE) return false; // Exactly one predecessor! |
| 94 | |
| 95 | BasicBlock::iterator I = Dest->begin(); |
| 96 | while (isa<PHINode>(*I)) ++I; |
| 97 | |
| 98 | for (unsigned Size = 0; I != Dest->end(); ++Size, ++I) |
| 99 | if (Size == 6) return false; // The block is too large... |
| 100 | return true; |
| 101 | } |
| 102 | |
| 103 | |
| 104 | /// eliminateUnconditionalBranch - Clone the instructions from the destination |
| 105 | /// block into the source block, eliminating the specified unconditional branch. |
| 106 | /// If the destination block defines values used by successors of the dest |
| 107 | /// block, we may need to insert PHI nodes. |
| 108 | /// |
| 109 | void TailDup::eliminateUnconditionalBranch(BranchInst *Branch) { |
| 110 | BasicBlock *SourceBlock = Branch->getParent(); |
| 111 | BasicBlock *DestBlock = Branch->getSuccessor(0); |
| 112 | assert(SourceBlock != DestBlock && "Our predicate is broken!"); |
| 113 | |
| 114 | DEBUG(std::cerr << "TailDuplication[" << SourceBlock->getParent()->getName() |
| 115 | << "]: Eliminating branch: " << *Branch); |
| 116 | |
| 117 | // We are going to have to map operands from the original block B to the new |
| 118 | // copy of the block B'. If there are PHI nodes in the DestBlock, these PHI |
| 119 | // nodes also define part of this mapping. Loop over these PHI nodes, adding |
| 120 | // them to our mapping. |
Chris Lattner | ea635cd | 2003-06-22 20:25:27 +0000 | [diff] [blame] | 121 | // |
Chris Lattner | 7a7bef4 | 2003-06-22 20:10:28 +0000 | [diff] [blame] | 122 | std::map<Value*, Value*> ValueMapping; |
| 123 | |
| 124 | BasicBlock::iterator BI = DestBlock->begin(); |
| 125 | bool HadPHINodes = isa<PHINode>(BI); |
| 126 | for (; PHINode *PN = dyn_cast<PHINode>(BI); ++BI) |
| 127 | ValueMapping[PN] = PN->getIncomingValueForBlock(SourceBlock); |
| 128 | |
| 129 | // Clone the non-phi instructions of the dest block into the source block, |
| 130 | // keeping track of the mapping... |
| 131 | // |
| 132 | for (; BI != DestBlock->end(); ++BI) { |
| 133 | Instruction *New = BI->clone(); |
| 134 | New->setName(BI->getName()); |
| 135 | SourceBlock->getInstList().push_back(New); |
| 136 | ValueMapping[BI] = New; |
| 137 | } |
| 138 | |
| 139 | // Now that we have built the mapping information and cloned all of the |
| 140 | // instructions (giving us a new terminator, among other things), walk the new |
| 141 | // instructions, rewriting references of old instructions to use new |
| 142 | // instructions. |
| 143 | // |
| 144 | BI = Branch; ++BI; // Get an iterator to the first new instruction |
| 145 | for (; BI != SourceBlock->end(); ++BI) |
| 146 | for (unsigned i = 0, e = BI->getNumOperands(); i != e; ++i) |
| 147 | if (Value *Remapped = ValueMapping[BI->getOperand(i)]) |
| 148 | BI->setOperand(i, Remapped); |
| 149 | |
| 150 | // Next we check to see if any of the successors of DestBlock had PHI nodes. |
| 151 | // If so, we need to add entries to the PHI nodes for SourceBlock now. |
| 152 | for (succ_iterator SI = succ_begin(DestBlock), SE = succ_end(DestBlock); |
| 153 | SI != SE; ++SI) { |
| 154 | BasicBlock *Succ = *SI; |
| 155 | for (BasicBlock::iterator PNI = Succ->begin(); |
| 156 | PHINode *PN = dyn_cast<PHINode>(PNI); ++PNI) { |
| 157 | // Ok, we have a PHI node. Figure out what the incoming value was for the |
| 158 | // DestBlock. |
| 159 | Value *IV = PN->getIncomingValueForBlock(DestBlock); |
| 160 | |
| 161 | // Remap the value if necessary... |
| 162 | if (Value *MappedIV = ValueMapping[IV]) |
| 163 | IV = MappedIV; |
| 164 | PN->addIncoming(IV, SourceBlock); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | // Now that all of the instructions are correctly copied into the SourceBlock, |
| 169 | // we have one more minor problem: the successors of the original DestBB may |
| 170 | // use the values computed in DestBB either directly (if DestBB dominated the |
| 171 | // block), or through a PHI node. In either case, we need to insert PHI nodes |
| 172 | // into any successors of DestBB (which are now our successors) for each value |
| 173 | // that is computed in DestBB, but is used outside of it. All of these uses |
| 174 | // we have to rewrite with the new PHI node. |
| 175 | // |
| 176 | if (succ_begin(SourceBlock) != succ_end(SourceBlock)) // Avoid wasting time... |
| 177 | for (BI = DestBlock->begin(); BI != DestBlock->end(); ++BI) |
| 178 | if (BI->getType() != Type::VoidTy) |
| 179 | InsertPHINodesIfNecessary(BI, ValueMapping[BI], SourceBlock); |
| 180 | |
| 181 | // Final step: now that we have finished everything up, walk the cloned |
| 182 | // instructions one last time, constant propagating and DCE'ing them, because |
| 183 | // they may not be needed anymore. |
| 184 | // |
| 185 | BI = Branch; ++BI; // Get an iterator to the first new instruction |
| 186 | if (HadPHINodes) |
| 187 | while (BI != SourceBlock->end()) |
| 188 | if (!dceInstruction(BI) && !doConstantPropagation(BI)) |
| 189 | ++BI; |
| 190 | |
| 191 | DestBlock->removePredecessor(SourceBlock); // Remove entries in PHI nodes... |
| 192 | SourceBlock->getInstList().erase(Branch); // Destroy the uncond branch... |
| 193 | |
| 194 | ++NumEliminated; // We just killed a branch! |
| 195 | } |
| 196 | |
| 197 | /// InsertPHINodesIfNecessary - So at this point, we cloned the OrigInst |
| 198 | /// instruction into the NewBlock with the value of NewInst. If OrigInst was |
| 199 | /// used outside of its defining basic block, we need to insert a PHI nodes into |
| 200 | /// the successors. |
| 201 | /// |
| 202 | void TailDup::InsertPHINodesIfNecessary(Instruction *OrigInst, Value *NewInst, |
| 203 | BasicBlock *NewBlock) { |
| 204 | // Loop over all of the uses of OrigInst, rewriting them to be newly inserted |
| 205 | // PHI nodes, unless they are in the same basic block as OrigInst. |
| 206 | BasicBlock *OrigBlock = OrigInst->getParent(); |
| 207 | std::vector<Instruction*> Users; |
| 208 | Users.reserve(OrigInst->use_size()); |
| 209 | for (Value::use_iterator I = OrigInst->use_begin(), E = OrigInst->use_end(); |
| 210 | I != E; ++I) { |
| 211 | Instruction *In = cast<Instruction>(*I); |
Chris Lattner | fcd74e2 | 2003-06-24 19:48:06 +0000 | [diff] [blame] | 212 | if (In->getParent() != OrigBlock || // Don't modify uses in the orig block! |
| 213 | isa<PHINode>(In)) |
Chris Lattner | 7a7bef4 | 2003-06-22 20:10:28 +0000 | [diff] [blame] | 214 | Users.push_back(In); |
| 215 | } |
| 216 | |
| 217 | // The common case is that the instruction is only used within the block that |
| 218 | // defines it. If we have this case, quick exit. |
| 219 | // |
| 220 | if (Users.empty()) return; |
| 221 | |
| 222 | // Otherwise, we have a more complex case, handle it now. This requires the |
| 223 | // construction of a mapping between a basic block and the value to use when |
| 224 | // in the scope of that basic block. This map will map to the original and |
| 225 | // new values when in the original or new block, but will map to inserted PHI |
| 226 | // nodes when in other blocks. |
| 227 | // |
| 228 | std::map<BasicBlock*, Value*> ValueMap; |
| 229 | std::map<BasicBlock*, Value*> OutValueMap; // The outgoing value map |
| 230 | OutValueMap[OrigBlock] = OrigInst; |
| 231 | OutValueMap[NewBlock ] = NewInst; // Seed the initial values... |
| 232 | |
| 233 | DEBUG(std::cerr << " ** Inserting PHI nodes for " << OrigInst); |
| 234 | while (!Users.empty()) { |
| 235 | Instruction *User = Users.back(); Users.pop_back(); |
| 236 | |
| 237 | if (PHINode *PN = dyn_cast<PHINode>(User)) { |
| 238 | // PHI nodes must be handled specially here, because their operands are |
| 239 | // actually defined in predecessor basic blocks, NOT in the block that the |
| 240 | // PHI node lives in. Note that we have already added entries to PHI nods |
| 241 | // which are in blocks that are immediate successors of OrigBlock, so |
| 242 | // don't modify them again. |
| 243 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) |
| 244 | if (PN->getIncomingValue(i) == OrigInst && |
| 245 | PN->getIncomingBlock(i) != OrigBlock) { |
| 246 | Value *V = GetValueOutBlock(PN->getIncomingBlock(i), OrigInst, |
| 247 | ValueMap, OutValueMap); |
| 248 | PN->setIncomingValue(i, V); |
| 249 | } |
| 250 | |
| 251 | } else { |
| 252 | // Any other user of the instruction can just replace any uses with the |
| 253 | // new value defined in the block it resides in. |
| 254 | Value *V = GetValueInBlock(User->getParent(), OrigInst, ValueMap, |
| 255 | OutValueMap); |
| 256 | User->replaceUsesOfWith(OrigInst, V); |
| 257 | } |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | /// GetValueInBlock - This is a recursive method which inserts PHI nodes into |
| 262 | /// the function until there is a value available in basic block BB. |
| 263 | /// |
| 264 | Value *TailDup::GetValueInBlock(BasicBlock *BB, Value *OrigVal, |
| 265 | std::map<BasicBlock*, Value*> &ValueMap, |
| 266 | std::map<BasicBlock*, Value*> &OutValueMap) { |
| 267 | Value*& BBVal = ValueMap[BB]; |
| 268 | if (BBVal) return BBVal; // Value already computed for this block? |
| 269 | |
| 270 | assert(pred_begin(BB) != pred_end(BB) && |
| 271 | "Propagating PHI nodes to unreachable blocks?"); |
| 272 | |
| 273 | // If there is no value already available in this basic block, we need to |
| 274 | // either reuse a value from an incoming, dominating, basic block, or we need |
| 275 | // to create a new PHI node to merge in different incoming values. Because we |
| 276 | // don't know if we're part of a loop at this point or not, we create a PHI |
| 277 | // node, even if we will ultimately eliminate it. |
| 278 | PHINode *PN = new PHINode(OrigVal->getType(), OrigVal->getName()+".pn", |
| 279 | BB->begin()); |
| 280 | BBVal = PN; // Insert this into the BBVal slot in case of cycles... |
| 281 | |
| 282 | Value*& BBOutVal = OutValueMap[BB]; |
| 283 | if (BBOutVal == 0) BBOutVal = PN; |
| 284 | |
| 285 | // Now that we have created the PHI node, loop over all of the predecessors of |
| 286 | // this block, computing an incoming value for the predecessor. |
| 287 | std::vector<BasicBlock*> Preds(pred_begin(BB), pred_end(BB)); |
| 288 | for (unsigned i = 0, e = Preds.size(); i != e; ++i) |
| 289 | PN->addIncoming(GetValueOutBlock(Preds[i], OrigVal, ValueMap, OutValueMap), |
| 290 | Preds[i]); |
| 291 | |
| 292 | // The PHI node is complete. In many cases, however the PHI node was |
| 293 | // ultimately unnecessary: we could have just reused a dominating incoming |
| 294 | // value. If this is the case, nuke the PHI node and replace the map entry |
| 295 | // with the dominating value. |
| 296 | // |
| 297 | assert(PN->getNumIncomingValues() > 0 && "No predecessors?"); |
| 298 | |
| 299 | // Check to see if all of the elements in the PHI node are either the PHI node |
| 300 | // itself or ONE particular value. |
| 301 | unsigned i = 0; |
| 302 | Value *ReplVal = PN->getIncomingValue(i); |
| 303 | for (; ReplVal == PN && i != PN->getNumIncomingValues(); ++i) |
| 304 | ReplVal = PN->getIncomingValue(i); // Skip values equal to the PN |
| 305 | |
| 306 | for (; i != PN->getNumIncomingValues(); ++i) |
| 307 | if (PN->getIncomingValue(i) != PN && PN->getIncomingValue(i) != ReplVal) { |
| 308 | ReplVal = 0; |
| 309 | break; |
| 310 | } |
| 311 | |
| 312 | // Found a value to replace the PHI node with? |
Chris Lattner | 066ab6a | 2003-06-22 20:46:00 +0000 | [diff] [blame] | 313 | if (ReplVal && ReplVal != PN) { |
Chris Lattner | 7a7bef4 | 2003-06-22 20:10:28 +0000 | [diff] [blame] | 314 | PN->replaceAllUsesWith(ReplVal); |
| 315 | BBVal = ReplVal; |
| 316 | if (BBOutVal == PN) BBOutVal = ReplVal; |
| 317 | BB->getInstList().erase(PN); // Erase the PHI node... |
| 318 | } else { |
| 319 | ++NumPHINodes; |
| 320 | } |
| 321 | |
| 322 | return BBVal; |
| 323 | } |
| 324 | |
| 325 | Value *TailDup::GetValueOutBlock(BasicBlock *BB, Value *OrigVal, |
| 326 | std::map<BasicBlock*, Value*> &ValueMap, |
| 327 | std::map<BasicBlock*, Value*> &OutValueMap) { |
| 328 | Value*& BBVal = OutValueMap[BB]; |
| 329 | if (BBVal) return BBVal; // Value already computed for this block? |
| 330 | |
| 331 | return BBVal = GetValueInBlock(BB, OrigVal, ValueMap, OutValueMap); |
| 332 | } |