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