blob: 358b8e284af2810359311a71e61670d71e67fc3e [file] [log] [blame]
Chris Lattneree99d152008-04-20 20:35:01 +00001//===- JumpThreading.cpp - Thread control through conditional blocks ------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Chris Lattner6c2a5502008-04-20 21:13:06 +000010// This file implements the Jump Threading pass.
Chris Lattneree99d152008-04-20 20:35:01 +000011//
12//===----------------------------------------------------------------------===//
13
14#define DEBUG_TYPE "jump-threading"
15#include "llvm/Transforms/Scalar.h"
Chris Lattner6c2a5502008-04-20 21:13:06 +000016#include "llvm/IntrinsicInst.h"
Chris Lattneree99d152008-04-20 20:35:01 +000017#include "llvm/Pass.h"
Chris Lattneree23b832008-04-20 22:39:42 +000018#include "llvm/ADT/DenseMap.h"
Chris Lattneree99d152008-04-20 20:35:01 +000019#include "llvm/ADT/Statistic.h"
Chris Lattneree23b832008-04-20 22:39:42 +000020#include "llvm/Transforms/Utils/Local.h"
Chris Lattneree99d152008-04-20 20:35:01 +000021#include "llvm/Support/CommandLine.h"
22#include "llvm/Support/Compiler.h"
Chris Lattner6c2a5502008-04-20 21:13:06 +000023#include "llvm/Support/Debug.h"
Chris Lattneree99d152008-04-20 20:35:01 +000024using namespace llvm;
25
Chris Lattneree23b832008-04-20 22:39:42 +000026STATISTIC(NumThreads, "Number of jumps threaded");
27STATISTIC(NumFolds, "Number of terminators folded");
Chris Lattneree99d152008-04-20 20:35:01 +000028
Chris Lattner6c2a5502008-04-20 21:13:06 +000029static cl::opt<unsigned>
30Threshold("jump-threading-threshold",
31 cl::desc("Max block size to duplicate for jump threading"),
32 cl::init(6), cl::Hidden);
33
Chris Lattneree99d152008-04-20 20:35:01 +000034namespace {
Chris Lattner6c2a5502008-04-20 21:13:06 +000035 /// This pass performs 'jump threading', which looks at blocks that have
36 /// multiple predecessors and multiple successors. If one or more of the
37 /// predecessors of the block can be proven to always jump to one of the
38 /// successors, we forward the edge from the predecessor to the successor by
39 /// duplicating the contents of this block.
40 ///
41 /// An example of when this can occur is code like this:
42 ///
43 /// if () { ...
44 /// X = 4;
45 /// }
46 /// if (X < 3) {
47 ///
48 /// In this case, the unconditional branch at the end of the first if can be
49 /// revectored to the false side of the second if.
50 ///
Chris Lattneree99d152008-04-20 20:35:01 +000051 class VISIBILITY_HIDDEN JumpThreading : public FunctionPass {
52 public:
53 static char ID; // Pass identification
54 JumpThreading() : FunctionPass((intptr_t)&ID) {}
55
56 bool runOnFunction(Function &F);
Chris Lattneree23b832008-04-20 22:39:42 +000057 bool ThreadBlock(BasicBlock *BB);
58 void ThreadEdge(BasicBlock *BB, BasicBlock *PredBB, BasicBlock *SuccBB);
Chris Lattneree99d152008-04-20 20:35:01 +000059 };
60 char JumpThreading::ID = 0;
61 RegisterPass<JumpThreading> X("jump-threading", "Jump Threading");
62}
63
64// Public interface to the Jump Threading pass
65FunctionPass *llvm::createJumpThreadingPass() { return new JumpThreading(); }
66
67/// runOnFunction - Top level algorithm.
68///
69bool JumpThreading::runOnFunction(Function &F) {
Chris Lattner6c2a5502008-04-20 21:13:06 +000070 DOUT << "Jump threading on function '" << F.getNameStart() << "'\n";
Chris Lattneree23b832008-04-20 22:39:42 +000071
72 bool AnotherIteration = true, EverChanged = false;
73 while (AnotherIteration) {
74 AnotherIteration = false;
75 bool Changed = false;
76 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
77 while (ThreadBlock(I))
78 Changed = true;
79 AnotherIteration = Changed;
80 EverChanged |= Changed;
81 }
82 return EverChanged;
Chris Lattneree99d152008-04-20 20:35:01 +000083}
Chris Lattner6c2a5502008-04-20 21:13:06 +000084
85/// getJumpThreadDuplicationCost - Return the cost of duplicating this block to
86/// thread across it.
Chris Lattneree23b832008-04-20 22:39:42 +000087static unsigned getJumpThreadDuplicationCost(const BasicBlock *BB) {
88 BasicBlock::const_iterator I = BB->begin();
Chris Lattner6c2a5502008-04-20 21:13:06 +000089 /// Ignore PHI nodes, these will be flattened when duplication happens.
90 while (isa<PHINode>(*I)) ++I;
91
92 // Sum up the cost of each instruction until we get to the terminator. Don't
93 // include the terminator because the copy won't include it.
94 unsigned Size = 0;
95 for (; !isa<TerminatorInst>(I); ++I) {
96 // Debugger intrinsics don't incur code size.
97 if (isa<DbgInfoIntrinsic>(I)) continue;
98
99 // If this is a pointer->pointer bitcast, it is free.
100 if (isa<BitCastInst>(I) && isa<PointerType>(I->getType()))
101 continue;
102
103 // All other instructions count for at least one unit.
104 ++Size;
105
106 // Calls are more expensive. If they are non-intrinsic calls, we model them
107 // as having cost of 4. If they are a non-vector intrinsic, we model them
108 // as having cost of 2 total, and if they are a vector intrinsic, we model
109 // them as having cost 1.
110 if (const CallInst *CI = dyn_cast<CallInst>(I)) {
111 if (!isa<IntrinsicInst>(CI))
112 Size += 3;
113 else if (isa<VectorType>(CI->getType()))
114 Size += 1;
115 }
116 }
117
118 // Threading through a switch statement is particularly profitable. If this
119 // block ends in a switch, decrease its cost to make it more likely to happen.
120 if (isa<SwitchInst>(I))
121 Size = Size > 6 ? Size-6 : 0;
122
123 return Size;
124}
125
126
127/// ThreadBlock - If there are any predecessors whose control can be threaded
128/// through to a successor, transform them now.
Chris Lattneree23b832008-04-20 22:39:42 +0000129bool JumpThreading::ThreadBlock(BasicBlock *BB) {
Chris Lattner6c2a5502008-04-20 21:13:06 +0000130 // See if this block ends with a branch of switch. If so, see if the
131 // condition is a phi node. If so, and if an entry of the phi node is a
132 // constant, we can thread the block.
133 Value *Condition;
Chris Lattneree23b832008-04-20 22:39:42 +0000134 if (BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator())) {
135 // Can't thread an unconditional jump.
136 if (BI->isUnconditional()) return false;
Chris Lattner6c2a5502008-04-20 21:13:06 +0000137 Condition = BI->getCondition();
Chris Lattneree23b832008-04-20 22:39:42 +0000138 } else if (SwitchInst *SI = dyn_cast<SwitchInst>(BB->getTerminator()))
Chris Lattner6c2a5502008-04-20 21:13:06 +0000139 Condition = SI->getCondition();
140 else
141 return false; // Must be an invoke.
Chris Lattneree23b832008-04-20 22:39:42 +0000142
143 // If the terminator of this block is branching on a constant, simplify the
144 // terminator to an unconditional branch. This can occur do to threading in
145 // other blocks.
146 if (isa<ConstantInt>(Condition)) {
147 DOUT << " In block '" << BB->getNameStart()
148 << "' folding terminator: " << *BB->getTerminator();
149 ++NumFolds;
150 ConstantFoldTerminator(BB);
151 return true;
152 }
153
154 // If there is only a single predecessor of this block, nothing to fold.
155 if (BB->getSinglePredecessor())
156 return false;
Chris Lattner6c2a5502008-04-20 21:13:06 +0000157
158 // See if this is a phi node in the current block.
159 PHINode *PN = dyn_cast<PHINode>(Condition);
Chris Lattneree23b832008-04-20 22:39:42 +0000160 if (!PN || PN->getParent() != BB) return false;
Chris Lattner6c2a5502008-04-20 21:13:06 +0000161
Chris Lattner0add3212008-04-20 21:18:09 +0000162 // See if the phi node has any constant values. If so, we can determine where
163 // the corresponding predecessor will branch.
164 unsigned PredNo = ~0U;
Chris Lattneree23b832008-04-20 22:39:42 +0000165 ConstantInt *PredCst = 0;
Chris Lattner0add3212008-04-20 21:18:09 +0000166 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
Chris Lattneree23b832008-04-20 22:39:42 +0000167 if ((PredCst = dyn_cast<ConstantInt>(PN->getIncomingValue(i)))) {
Chris Lattner0add3212008-04-20 21:18:09 +0000168 PredNo = i;
169 break;
170 }
171 }
172
173 // If no incoming value has a constant, we don't know the destination of any
174 // predecessors.
175 if (PredNo == ~0U)
176 return false;
177
Chris Lattner6c2a5502008-04-20 21:13:06 +0000178 // See if the cost of duplicating this block is low enough.
179 unsigned JumpThreadCost = getJumpThreadDuplicationCost(BB);
180 if (JumpThreadCost > Threshold) {
Chris Lattneree23b832008-04-20 22:39:42 +0000181 DOUT << " Not threading BB '" << BB->getNameStart()
Chris Lattner0add3212008-04-20 21:18:09 +0000182 << "' - Cost is too high: " << JumpThreadCost << "\n";
Chris Lattner6c2a5502008-04-20 21:13:06 +0000183 return false;
184 }
Chris Lattner6c2a5502008-04-20 21:13:06 +0000185
Chris Lattneree23b832008-04-20 22:39:42 +0000186 // If so, we can actually do this threading. Figure out which predecessor and
187 // which successor we are threading for.
188 BasicBlock *PredBB = PN->getIncomingBlock(PredNo);
189 BasicBlock *SuccBB;
190 if (BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator()))
191 SuccBB = BI->getSuccessor(PredCst == ConstantInt::getFalse());
192 else {
193 SwitchInst *SI = cast<SwitchInst>(BB->getTerminator());
194 SuccBB = SI->getSuccessor(SI->findCaseValue(PredCst));
195 }
196
197 // TODO: If there are multiple preds with the same incoming value for the PHI,
198 // factor them together so we get one thread block for the whole group. This
199 // is important for things like "phi i1 [true, true, false, true, x]" where
200 // we only need to clone the block for the true blocks once.
201
202 DOUT << " Threading edge from '" << PredBB->getNameStart() << "' to '"
203 << SuccBB->getNameStart() << "' with cost: " << JumpThreadCost
204 << ", across block:\n "
205 << *BB;
206
207 ThreadEdge(BB, PredBB, SuccBB);
208 ++NumThreads;
209 return true;
210}
211
212/// ThreadEdge - We have decided that it is safe and profitable to thread an
213/// edge from PredBB to SuccBB across BB. Transform the IR to reflect this
214/// change.
215void JumpThreading::ThreadEdge(BasicBlock *BB, BasicBlock *PredBB,
216 BasicBlock *SuccBB) {
217
218 // Jump Threading can not update SSA properties correctly if the values
219 // defined in the duplicated block are used outside of the block itself. For
220 // this reason, we spill all values that are used outside of BB to the stack.
221 for (BasicBlock::iterator I = BB->begin(); I != BB->end(); ++I)
222 if (I->isUsedOutsideOfBlock(BB)) {
223 // We found a use of I outside of BB. Create a new stack slot to
224 // break this inter-block usage pattern.
225 DemoteRegToStack(*I);
226 }
227
228 // We are going to have to map operands from the original BB block to the new
229 // copy of the block 'NewBB'. If there are PHI nodes in BB, evaluate them to
230 // account for entry from PredBB.
231 DenseMap<Instruction*, Value*> ValueMapping;
232
233 BasicBlock *NewBB =
234 BasicBlock::Create(BB->getName()+".thread", BB->getParent(), BB);
235 NewBB->moveAfter(PredBB);
236
237 BasicBlock::iterator BI = BB->begin();
238 for (; PHINode *PN = dyn_cast<PHINode>(BI); ++BI)
239 ValueMapping[PN] = PN->getIncomingValueForBlock(PredBB);
240
241 // Clone the non-phi instructions of BB into NewBB, keeping track of the
242 // mapping and using it to remap operands in the cloned instructions.
243 for (; !isa<TerminatorInst>(BI); ++BI) {
244 Instruction *New = BI->clone();
245 New->setName(BI->getNameStart());
246 NewBB->getInstList().push_back(New);
247 ValueMapping[BI] = New;
248
249 // Remap operands to patch up intra-block references.
250 for (unsigned i = 0, e = New->getNumOperands(); i != e; ++i)
251 if (Instruction *Inst = dyn_cast<Instruction>(New->getOperand(i)))
252 if (Value *Remapped = ValueMapping[Inst])
253 New->setOperand(i, Remapped);
254 }
255
256 // We didn't copy the terminator from BB over to NewBB, because there is now
257 // an unconditional jump to SuccBB. Insert the unconditional jump.
258 BranchInst::Create(SuccBB, NewBB);
259
260 // Check to see if SuccBB has PHI nodes. If so, we need to add entries to the
261 // PHI nodes for NewBB now.
262 for (BasicBlock::iterator PNI = SuccBB->begin(); isa<PHINode>(PNI); ++PNI) {
263 PHINode *PN = cast<PHINode>(PNI);
264 // Ok, we have a PHI node. Figure out what the incoming value was for the
265 // DestBlock.
266 Value *IV = PN->getIncomingValueForBlock(BB);
267
268 // Remap the value if necessary.
269 if (Instruction *Inst = dyn_cast<Instruction>(IV))
270 if (Value *MappedIV = ValueMapping[Inst])
271 IV = MappedIV;
272 PN->addIncoming(IV, NewBB);
273 }
274
275 // Finally, NewBB is good to go. Update the terminator of PredBB to jump to
276 // NewBB instead of BB. This eliminates predecessors from BB, which requires
277 // us to simplify any PHI nodes in BB.
278 TerminatorInst *PredTerm = PredBB->getTerminator();
279 for (unsigned i = 0, e = PredTerm->getNumSuccessors(); i != e; ++i)
280 if (PredTerm->getSuccessor(i) == BB) {
281 BB->removePredecessor(PredBB);
282 PredTerm->setSuccessor(i, NewBB);
283 }
Chris Lattner6c2a5502008-04-20 21:13:06 +0000284}