blob: e39cd221b5a70a02fde274833d11a8fd1465fad8 [file] [log] [blame]
Andrew Trick9e764222011-06-04 01:16:30 +00001//===-- BranchProbabilityInfo.cpp - Branch Probability Analysis -*- C++ -*-===//
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//
10// Loops should be simplified before this analysis.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/Instructions.h"
15#include "llvm/Analysis/BranchProbabilityInfo.h"
Jakub Staszak12af93a2011-07-16 20:31:15 +000016#include "llvm/Analysis/LoopInfo.h"
Andrew Trickf289df22011-06-11 01:05:22 +000017#include "llvm/Support/Debug.h"
Andrew Trick9e764222011-06-04 01:16:30 +000018
19using namespace llvm;
20
21INITIALIZE_PASS_BEGIN(BranchProbabilityInfo, "branch-prob",
22 "Branch Probability Analysis", false, true)
23INITIALIZE_PASS_DEPENDENCY(LoopInfo)
24INITIALIZE_PASS_END(BranchProbabilityInfo, "branch-prob",
25 "Branch Probability Analysis", false, true)
26
27char BranchProbabilityInfo::ID = 0;
28
Benjamin Kramerafa88ea2011-06-13 18:38:56 +000029namespace {
Andrew Trick9e764222011-06-04 01:16:30 +000030// Please note that BranchProbabilityAnalysis is not a FunctionPass.
31// It is created by BranchProbabilityInfo (which is a FunctionPass), which
32// provides a clear interface. Thanks to that, all heuristics and other
33// private methods are hidden in the .cpp file.
34class BranchProbabilityAnalysis {
35
36 typedef std::pair<BasicBlock *, BasicBlock *> Edge;
37
Andrew Trickf289df22011-06-11 01:05:22 +000038 DenseMap<Edge, uint32_t> *Weights;
Andrew Trick9e764222011-06-04 01:16:30 +000039
40 BranchProbabilityInfo *BP;
41
42 LoopInfo *LI;
43
44
45 // Weights are for internal use only. They are used by heuristics to help to
46 // estimate edges' probability. Example:
47 //
48 // Using "Loop Branch Heuristics" we predict weights of edges for the
49 // block BB2.
50 // ...
51 // |
52 // V
53 // BB1<-+
54 // | |
55 // | | (Weight = 128)
56 // V |
57 // BB2--+
58 // |
59 // | (Weight = 4)
60 // V
61 // BB3
62 //
63 // Probability of the edge BB2->BB1 = 128 / (128 + 4) = 0.9696..
64 // Probability of the edge BB2->BB3 = 4 / (128 + 4) = 0.0303..
65
Andrew Trickf289df22011-06-11 01:05:22 +000066 static const uint32_t LBH_TAKEN_WEIGHT = 128;
67 static const uint32_t LBH_NONTAKEN_WEIGHT = 4;
Andrew Trick9e764222011-06-04 01:16:30 +000068
69 // Standard weight value. Used when none of the heuristics set weight for
70 // the edge.
Andrew Trickf289df22011-06-11 01:05:22 +000071 static const uint32_t NORMAL_WEIGHT = 16;
Andrew Trick9e764222011-06-04 01:16:30 +000072
73 // Minimum weight of an edge. Please note, that weight is NEVER 0.
Andrew Trickf289df22011-06-11 01:05:22 +000074 static const uint32_t MIN_WEIGHT = 1;
Andrew Trick9e764222011-06-04 01:16:30 +000075
76 // Return TRUE if BB leads directly to a Return Instruction.
77 static bool isReturningBlock(BasicBlock *BB) {
78 SmallPtrSet<BasicBlock *, 8> Visited;
79
80 while (true) {
81 TerminatorInst *TI = BB->getTerminator();
82 if (isa<ReturnInst>(TI))
83 return true;
84
85 if (TI->getNumSuccessors() > 1)
86 break;
87
88 // It is unreachable block which we can consider as a return instruction.
89 if (TI->getNumSuccessors() == 0)
90 return true;
91
92 Visited.insert(BB);
93 BB = TI->getSuccessor(0);
94
95 // Stop if cycle is detected.
96 if (Visited.count(BB))
97 return false;
98 }
99
100 return false;
101 }
102
103 // Multiply Edge Weight by two.
104 void incEdgeWeight(BasicBlock *Src, BasicBlock *Dst) {
Andrew Trickf289df22011-06-11 01:05:22 +0000105 uint32_t Weight = BP->getEdgeWeight(Src, Dst);
106 uint32_t MaxWeight = getMaxWeightFor(Src);
Andrew Trick9e764222011-06-04 01:16:30 +0000107
108 if (Weight * 2 > MaxWeight)
109 BP->setEdgeWeight(Src, Dst, MaxWeight);
110 else
111 BP->setEdgeWeight(Src, Dst, Weight * 2);
112 }
113
114 // Divide Edge Weight by two.
115 void decEdgeWeight(BasicBlock *Src, BasicBlock *Dst) {
Andrew Trickf289df22011-06-11 01:05:22 +0000116 uint32_t Weight = BP->getEdgeWeight(Src, Dst);
Andrew Trick9e764222011-06-04 01:16:30 +0000117
118 assert(Weight > 0);
119 if (Weight / 2 < MIN_WEIGHT)
120 BP->setEdgeWeight(Src, Dst, MIN_WEIGHT);
121 else
122 BP->setEdgeWeight(Src, Dst, Weight / 2);
123 }
124
125
Andrew Trickf289df22011-06-11 01:05:22 +0000126 uint32_t getMaxWeightFor(BasicBlock *BB) const {
127 return UINT32_MAX / BB->getTerminator()->getNumSuccessors();
Andrew Trick9e764222011-06-04 01:16:30 +0000128 }
129
130public:
Andrew Trickf289df22011-06-11 01:05:22 +0000131 BranchProbabilityAnalysis(DenseMap<Edge, uint32_t> *W,
Andrew Trick9e764222011-06-04 01:16:30 +0000132 BranchProbabilityInfo *BP, LoopInfo *LI)
133 : Weights(W), BP(BP), LI(LI) {
134 }
135
136 // Return Heuristics
137 void calcReturnHeuristics(BasicBlock *BB);
138
139 // Pointer Heuristics
140 void calcPointerHeuristics(BasicBlock *BB);
141
142 // Loop Branch Heuristics
143 void calcLoopBranchHeuristics(BasicBlock *BB);
144
145 bool runOnFunction(Function &F);
146};
Benjamin Kramerafa88ea2011-06-13 18:38:56 +0000147} // end anonymous namespace
Andrew Trick9e764222011-06-04 01:16:30 +0000148
149// Calculate Edge Weights using "Return Heuristics". Predict a successor which
150// leads directly to Return Instruction will not be taken.
151void BranchProbabilityAnalysis::calcReturnHeuristics(BasicBlock *BB){
152 if (BB->getTerminator()->getNumSuccessors() == 1)
153 return;
154
155 for (succ_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I) {
156 BasicBlock *Succ = *I;
157 if (isReturningBlock(Succ)) {
158 decEdgeWeight(BB, Succ);
159 }
160 }
161}
162
163// Calculate Edge Weights using "Pointer Heuristics". Predict a comparsion
164// between two pointer or pointer and NULL will fail.
165void BranchProbabilityAnalysis::calcPointerHeuristics(BasicBlock *BB) {
166 BranchInst * BI = dyn_cast<BranchInst>(BB->getTerminator());
167 if (!BI || !BI->isConditional())
168 return;
169
170 Value *Cond = BI->getCondition();
171 ICmpInst *CI = dyn_cast<ICmpInst>(Cond);
Jakub Staszakd7932ca2011-07-15 20:51:06 +0000172 if (!CI || !CI->isEquality())
Andrew Trick9e764222011-06-04 01:16:30 +0000173 return;
174
175 Value *LHS = CI->getOperand(0);
Andrew Trick9e764222011-06-04 01:16:30 +0000176
177 if (!LHS->getType()->isPointerTy())
178 return;
179
Nick Lewycky404b53e2011-06-04 02:07:10 +0000180 assert(CI->getOperand(1)->getType()->isPointerTy());
Andrew Trick9e764222011-06-04 01:16:30 +0000181
182 BasicBlock *Taken = BI->getSuccessor(0);
183 BasicBlock *NonTaken = BI->getSuccessor(1);
184
185 // p != 0 -> isProb = true
186 // p == 0 -> isProb = false
187 // p != q -> isProb = true
188 // p == q -> isProb = false;
Jakub Staszakd7932ca2011-07-15 20:51:06 +0000189 bool isProb = CI->getPredicate() == ICmpInst::ICMP_NE;
Andrew Trick9e764222011-06-04 01:16:30 +0000190 if (!isProb)
191 std::swap(Taken, NonTaken);
192
193 incEdgeWeight(BB, Taken);
194 decEdgeWeight(BB, NonTaken);
195}
196
197// Calculate Edge Weights using "Loop Branch Heuristics". Predict backedges
198// as taken, exiting edges as not-taken.
199void BranchProbabilityAnalysis::calcLoopBranchHeuristics(BasicBlock *BB) {
Andrew Trickf289df22011-06-11 01:05:22 +0000200 uint32_t numSuccs = BB->getTerminator()->getNumSuccessors();
Andrew Trick9e764222011-06-04 01:16:30 +0000201
202 Loop *L = LI->getLoopFor(BB);
203 if (!L)
204 return;
205
206 SmallVector<BasicBlock *, 8> BackEdges;
207 SmallVector<BasicBlock *, 8> ExitingEdges;
208
209 for (succ_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I) {
210 BasicBlock *Succ = *I;
211 Loop *SuccL = LI->getLoopFor(Succ);
212 if (SuccL != L)
213 ExitingEdges.push_back(Succ);
214 else if (Succ == L->getHeader())
215 BackEdges.push_back(Succ);
216 }
217
Andrew Trickf289df22011-06-11 01:05:22 +0000218 if (uint32_t numBackEdges = BackEdges.size()) {
219 uint32_t backWeight = LBH_TAKEN_WEIGHT / numBackEdges;
Andrew Trick9e764222011-06-04 01:16:30 +0000220 if (backWeight < NORMAL_WEIGHT)
221 backWeight = NORMAL_WEIGHT;
222
223 for (SmallVector<BasicBlock *, 8>::iterator EI = BackEdges.begin(),
224 EE = BackEdges.end(); EI != EE; ++EI) {
225 BasicBlock *Back = *EI;
226 BP->setEdgeWeight(BB, Back, backWeight);
227 }
228 }
229
Andrew Trickf289df22011-06-11 01:05:22 +0000230 uint32_t numExitingEdges = ExitingEdges.size();
231 if (uint32_t numNonExitingEdges = numSuccs - numExitingEdges) {
232 uint32_t exitWeight = LBH_NONTAKEN_WEIGHT / numNonExitingEdges;
Andrew Trick9e764222011-06-04 01:16:30 +0000233 if (exitWeight < MIN_WEIGHT)
234 exitWeight = MIN_WEIGHT;
235
236 for (SmallVector<BasicBlock *, 8>::iterator EI = ExitingEdges.begin(),
237 EE = ExitingEdges.end(); EI != EE; ++EI) {
238 BasicBlock *Exiting = *EI;
239 BP->setEdgeWeight(BB, Exiting, exitWeight);
240 }
241 }
242}
243
244bool BranchProbabilityAnalysis::runOnFunction(Function &F) {
245
246 for (Function::iterator I = F.begin(), E = F.end(); I != E; ) {
247 BasicBlock *BB = I++;
248
249 // Only LBH uses setEdgeWeight method.
250 calcLoopBranchHeuristics(BB);
251
252 // PH and RH use only incEdgeWeight and decEwdgeWeight methods to
253 // not efface LBH results.
254 calcPointerHeuristics(BB);
255 calcReturnHeuristics(BB);
256 }
257
258 return false;
259}
260
Jakub Staszak12af93a2011-07-16 20:31:15 +0000261void BranchProbabilityInfo::getAnalysisUsage(AnalysisUsage &AU) const {
262 AU.addRequired<LoopInfo>();
263 AU.setPreservesAll();
264}
Andrew Trick9e764222011-06-04 01:16:30 +0000265
266bool BranchProbabilityInfo::runOnFunction(Function &F) {
267 LoopInfo &LI = getAnalysis<LoopInfo>();
268 BranchProbabilityAnalysis BPA(&Weights, this, &LI);
Andrew Trickf289df22011-06-11 01:05:22 +0000269 return BPA.runOnFunction(F);
Andrew Trick9e764222011-06-04 01:16:30 +0000270}
271
Andrew Trickf289df22011-06-11 01:05:22 +0000272uint32_t BranchProbabilityInfo::getSumForBlock(BasicBlock *BB) const {
273 uint32_t Sum = 0;
274
275 for (succ_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I) {
Andrew Trick9e764222011-06-04 01:16:30 +0000276 BasicBlock *Succ = *I;
Andrew Trickf289df22011-06-11 01:05:22 +0000277 uint32_t Weight = getEdgeWeight(BB, Succ);
278 uint32_t PrevSum = Sum;
Andrew Trick9e764222011-06-04 01:16:30 +0000279
280 Sum += Weight;
281 assert(Sum > PrevSum); (void) PrevSum;
282 }
283
Andrew Trickf289df22011-06-11 01:05:22 +0000284 return Sum;
285}
286
287bool BranchProbabilityInfo::isEdgeHot(BasicBlock *Src, BasicBlock *Dst) const {
288 // Hot probability is at least 4/5 = 80%
289 uint32_t Weight = getEdgeWeight(Src, Dst);
290 uint32_t Sum = getSumForBlock(Src);
291
292 // FIXME: Implement BranchProbability::compare then change this code to
293 // compare this BranchProbability against a static "hot" BranchProbability.
294 return (uint64_t)Weight * 5 > (uint64_t)Sum * 4;
Andrew Trick9e764222011-06-04 01:16:30 +0000295}
296
297BasicBlock *BranchProbabilityInfo::getHotSucc(BasicBlock *BB) const {
Andrew Trickf289df22011-06-11 01:05:22 +0000298 uint32_t Sum = 0;
299 uint32_t MaxWeight = 0;
Andrew Trick9e764222011-06-04 01:16:30 +0000300 BasicBlock *MaxSucc = 0;
301
302 for (succ_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I) {
303 BasicBlock *Succ = *I;
Andrew Trickf289df22011-06-11 01:05:22 +0000304 uint32_t Weight = getEdgeWeight(BB, Succ);
305 uint32_t PrevSum = Sum;
Andrew Trick9e764222011-06-04 01:16:30 +0000306
307 Sum += Weight;
308 assert(Sum > PrevSum); (void) PrevSum;
309
310 if (Weight > MaxWeight) {
311 MaxWeight = Weight;
312 MaxSucc = Succ;
313 }
314 }
315
Andrew Trickf289df22011-06-11 01:05:22 +0000316 // FIXME: Use BranchProbability::compare.
317 if ((uint64_t)MaxWeight * 5 > (uint64_t)Sum * 4)
Andrew Trick9e764222011-06-04 01:16:30 +0000318 return MaxSucc;
319
320 return 0;
321}
322
323// Return edge's weight. If can't find it, return DEFAULT_WEIGHT value.
Andrew Trickf289df22011-06-11 01:05:22 +0000324uint32_t
Andrew Trick9e764222011-06-04 01:16:30 +0000325BranchProbabilityInfo::getEdgeWeight(BasicBlock *Src, BasicBlock *Dst) const {
326 Edge E(Src, Dst);
Andrew Trickf289df22011-06-11 01:05:22 +0000327 DenseMap<Edge, uint32_t>::const_iterator I = Weights.find(E);
Andrew Trick9e764222011-06-04 01:16:30 +0000328
329 if (I != Weights.end())
330 return I->second;
331
332 return DEFAULT_WEIGHT;
333}
334
335void BranchProbabilityInfo::setEdgeWeight(BasicBlock *Src, BasicBlock *Dst,
Andrew Trickf289df22011-06-11 01:05:22 +0000336 uint32_t Weight) {
Andrew Trick9e764222011-06-04 01:16:30 +0000337 Weights[std::make_pair(Src, Dst)] = Weight;
Andrew Trickf289df22011-06-11 01:05:22 +0000338 DEBUG(dbgs() << "set edge " << Src->getNameStr() << " -> "
339 << Dst->getNameStr() << " weight to " << Weight
340 << (isEdgeHot(Src, Dst) ? " [is HOT now]\n" : "\n"));
341}
342
343
344BranchProbability BranchProbabilityInfo::
345getEdgeProbability(BasicBlock *Src, BasicBlock *Dst) const {
346
347 uint32_t N = getEdgeWeight(Src, Dst);
348 uint32_t D = getSumForBlock(Src);
349
350 return BranchProbability(N, D);
Andrew Trick9e764222011-06-04 01:16:30 +0000351}
352
353raw_ostream &
354BranchProbabilityInfo::printEdgeProbability(raw_ostream &OS, BasicBlock *Src,
Andrew Trickf289df22011-06-11 01:05:22 +0000355 BasicBlock *Dst) const {
Andrew Trick9e764222011-06-04 01:16:30 +0000356
Jakub Staszak7cc2b072011-06-16 20:22:37 +0000357 const BranchProbability Prob = getEdgeProbability(Src, Dst);
Andrew Trickf289df22011-06-11 01:05:22 +0000358 OS << "edge " << Src->getNameStr() << " -> " << Dst->getNameStr()
359 << " probability is " << Prob
360 << (isEdgeHot(Src, Dst) ? " [HOT edge]\n" : "\n");
Andrew Trick9e764222011-06-04 01:16:30 +0000361
362 return OS;
363}