1 //===-- BranchProbabilityInfo.cpp - Branch Probability Analysis -*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Loops should be simplified before this analysis.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Instructions.h"
15 #include "llvm/Analysis/BranchProbabilityInfo.h"
16 #include "llvm/Analysis/LoopInfo.h"
17 #include "llvm/Support/Debug.h"
21 INITIALIZE_PASS_BEGIN(BranchProbabilityInfo, "branch-prob",
22 "Branch Probability Analysis", false, true)
23 INITIALIZE_PASS_DEPENDENCY(LoopInfo)
24 INITIALIZE_PASS_END(BranchProbabilityInfo, "branch-prob",
25 "Branch Probability Analysis", false, true)
27 char BranchProbabilityInfo::ID = 0;
30 // 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.
34 class BranchProbabilityAnalysis {
36 typedef std::pair<BasicBlock *, BasicBlock *> Edge;
38 DenseMap<Edge, uint32_t> *Weights;
40 BranchProbabilityInfo *BP;
45 // Weights are for internal use only. They are used by heuristics to help to
46 // estimate edges' probability. Example:
48 // Using "Loop Branch Heuristics" we predict weights of edges for the
63 // Probability of the edge BB2->BB1 = 124 / (124 + 4) = 0.96875
64 // Probability of the edge BB2->BB3 = 4 / (124 + 4) = 0.03125
66 static const uint32_t LBH_TAKEN_WEIGHT = 124;
67 static const uint32_t LBH_NONTAKEN_WEIGHT = 4;
69 static const uint32_t RH_TAKEN_WEIGHT = 24;
70 static const uint32_t RH_NONTAKEN_WEIGHT = 8;
72 static const uint32_t PH_TAKEN_WEIGHT = 20;
73 static const uint32_t PH_NONTAKEN_WEIGHT = 12;
75 // Standard weight value. Used when none of the heuristics set weight for
77 static const uint32_t NORMAL_WEIGHT = 16;
79 // Minimum weight of an edge. Please note, that weight is NEVER 0.
80 static const uint32_t MIN_WEIGHT = 1;
82 // Return TRUE if BB leads directly to a Return Instruction.
83 static bool isReturningBlock(BasicBlock *BB) {
84 SmallPtrSet<BasicBlock *, 8> Visited;
87 TerminatorInst *TI = BB->getTerminator();
88 if (isa<ReturnInst>(TI))
91 if (TI->getNumSuccessors() > 1)
94 // It is unreachable block which we can consider as a return instruction.
95 if (TI->getNumSuccessors() == 0)
99 BB = TI->getSuccessor(0);
101 // Stop if cycle is detected.
102 if (Visited.count(BB))
109 uint32_t getMaxWeightFor(BasicBlock *BB) const {
110 return UINT32_MAX / BB->getTerminator()->getNumSuccessors();
114 BranchProbabilityAnalysis(DenseMap<Edge, uint32_t> *W,
115 BranchProbabilityInfo *BP, LoopInfo *LI)
116 : Weights(W), BP(BP), LI(LI) {
120 bool calcReturnHeuristics(BasicBlock *BB);
122 // Pointer Heuristics
123 bool calcPointerHeuristics(BasicBlock *BB);
125 // Loop Branch Heuristics
126 bool calcLoopBranchHeuristics(BasicBlock *BB);
128 bool runOnFunction(Function &F);
130 } // end anonymous namespace
132 // Calculate Edge Weights using "Return Heuristics". Predict a successor which
133 // leads directly to Return Instruction will not be taken.
134 bool BranchProbabilityAnalysis::calcReturnHeuristics(BasicBlock *BB){
135 if (BB->getTerminator()->getNumSuccessors() == 1)
138 SmallVector<BasicBlock *, 4> ReturningEdges;
139 SmallVector<BasicBlock *, 4> StayEdges;
141 for (succ_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I) {
142 BasicBlock *Succ = *I;
143 if (isReturningBlock(Succ))
144 ReturningEdges.push_back(Succ);
146 StayEdges.push_back(Succ);
149 if (uint32_t numStayEdges = StayEdges.size()) {
150 uint32_t stayWeight = RH_TAKEN_WEIGHT / numStayEdges;
151 if (stayWeight < NORMAL_WEIGHT)
152 stayWeight = NORMAL_WEIGHT;
154 for (SmallVector<BasicBlock *, 4>::iterator I = StayEdges.begin(),
155 E = StayEdges.end(); I != E; ++I)
156 BP->setEdgeWeight(BB, *I, stayWeight);
159 if (uint32_t numRetEdges = ReturningEdges.size()) {
160 uint32_t retWeight = RH_NONTAKEN_WEIGHT / numRetEdges;
161 if (retWeight < MIN_WEIGHT)
162 retWeight = MIN_WEIGHT;
163 for (SmallVector<BasicBlock *, 4>::iterator I = ReturningEdges.begin(),
164 E = ReturningEdges.end(); I != E; ++I) {
165 BP->setEdgeWeight(BB, *I, retWeight);
169 return ReturningEdges.size() > 0;
172 // Calculate Edge Weights using "Pointer Heuristics". Predict a comparsion
173 // between two pointer or pointer and NULL will fail.
174 bool BranchProbabilityAnalysis::calcPointerHeuristics(BasicBlock *BB) {
175 BranchInst * BI = dyn_cast<BranchInst>(BB->getTerminator());
176 if (!BI || !BI->isConditional())
179 Value *Cond = BI->getCondition();
180 ICmpInst *CI = dyn_cast<ICmpInst>(Cond);
181 if (!CI || !CI->isEquality())
184 Value *LHS = CI->getOperand(0);
186 if (!LHS->getType()->isPointerTy())
189 assert(CI->getOperand(1)->getType()->isPointerTy());
191 BasicBlock *Taken = BI->getSuccessor(0);
192 BasicBlock *NonTaken = BI->getSuccessor(1);
194 // p != 0 -> isProb = true
195 // p == 0 -> isProb = false
196 // p != q -> isProb = true
197 // p == q -> isProb = false;
198 bool isProb = CI->getPredicate() == ICmpInst::ICMP_NE;
200 std::swap(Taken, NonTaken);
202 BP->setEdgeWeight(BB, Taken, PH_TAKEN_WEIGHT);
203 BP->setEdgeWeight(BB, NonTaken, PH_NONTAKEN_WEIGHT);
207 // Calculate Edge Weights using "Loop Branch Heuristics". Predict backedges
208 // as taken, exiting edges as not-taken.
209 bool BranchProbabilityAnalysis::calcLoopBranchHeuristics(BasicBlock *BB) {
210 uint32_t numSuccs = BB->getTerminator()->getNumSuccessors();
212 Loop *L = LI->getLoopFor(BB);
216 SmallVector<BasicBlock *, 8> BackEdges;
217 SmallVector<BasicBlock *, 8> ExitingEdges;
218 SmallVector<BasicBlock *, 8> InEdges; // Edges from header to the loop.
220 bool isHeader = BB == L->getHeader();
222 for (succ_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I) {
223 BasicBlock *Succ = *I;
224 Loop *SuccL = LI->getLoopFor(Succ);
226 ExitingEdges.push_back(Succ);
227 else if (Succ == L->getHeader())
228 BackEdges.push_back(Succ);
230 InEdges.push_back(Succ);
233 if (uint32_t numBackEdges = BackEdges.size()) {
234 uint32_t backWeight = LBH_TAKEN_WEIGHT / numBackEdges;
235 if (backWeight < NORMAL_WEIGHT)
236 backWeight = NORMAL_WEIGHT;
238 for (SmallVector<BasicBlock *, 8>::iterator EI = BackEdges.begin(),
239 EE = BackEdges.end(); EI != EE; ++EI) {
240 BasicBlock *Back = *EI;
241 BP->setEdgeWeight(BB, Back, backWeight);
245 if (uint32_t numInEdges = InEdges.size()) {
246 uint32_t inWeight = LBH_TAKEN_WEIGHT / numInEdges;
247 if (inWeight < NORMAL_WEIGHT)
248 inWeight = NORMAL_WEIGHT;
250 for (SmallVector<BasicBlock *, 8>::iterator EI = InEdges.begin(),
251 EE = InEdges.end(); EI != EE; ++EI) {
252 BasicBlock *Back = *EI;
253 BP->setEdgeWeight(BB, Back, inWeight);
257 uint32_t numExitingEdges = ExitingEdges.size();
258 if (uint32_t numNonExitingEdges = numSuccs - numExitingEdges) {
259 uint32_t exitWeight = LBH_NONTAKEN_WEIGHT / numNonExitingEdges;
260 if (exitWeight < MIN_WEIGHT)
261 exitWeight = MIN_WEIGHT;
263 for (SmallVector<BasicBlock *, 8>::iterator EI = ExitingEdges.begin(),
264 EE = ExitingEdges.end(); EI != EE; ++EI) {
265 BasicBlock *Exiting = *EI;
266 BP->setEdgeWeight(BB, Exiting, exitWeight);
273 bool BranchProbabilityAnalysis::runOnFunction(Function &F) {
275 for (Function::iterator I = F.begin(), E = F.end(); I != E; ) {
276 BasicBlock *BB = I++;
278 // Only LBH uses setEdgeWeight method.
279 if (calcLoopBranchHeuristics(BB))
282 // PH and RH use only incEdgeWeight and decEwdgeWeight methods to
283 // not efface LBH results.
284 if (calcReturnHeuristics(BB))
287 calcPointerHeuristics(BB);
293 void BranchProbabilityInfo::getAnalysisUsage(AnalysisUsage &AU) const {
294 AU.addRequired<LoopInfo>();
295 AU.setPreservesAll();
298 bool BranchProbabilityInfo::runOnFunction(Function &F) {
299 LoopInfo &LI = getAnalysis<LoopInfo>();
300 BranchProbabilityAnalysis BPA(&Weights, this, &LI);
301 return BPA.runOnFunction(F);
304 uint32_t BranchProbabilityInfo::getSumForBlock(BasicBlock *BB) const {
307 for (succ_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I) {
308 BasicBlock *Succ = *I;
309 uint32_t Weight = getEdgeWeight(BB, Succ);
310 uint32_t PrevSum = Sum;
313 assert(Sum > PrevSum); (void) PrevSum;
319 bool BranchProbabilityInfo::isEdgeHot(BasicBlock *Src, BasicBlock *Dst) const {
320 // Hot probability is at least 4/5 = 80%
321 uint32_t Weight = getEdgeWeight(Src, Dst);
322 uint32_t Sum = getSumForBlock(Src);
324 // FIXME: Implement BranchProbability::compare then change this code to
325 // compare this BranchProbability against a static "hot" BranchProbability.
326 return (uint64_t)Weight * 5 > (uint64_t)Sum * 4;
329 BasicBlock *BranchProbabilityInfo::getHotSucc(BasicBlock *BB) const {
331 uint32_t MaxWeight = 0;
332 BasicBlock *MaxSucc = 0;
334 for (succ_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I) {
335 BasicBlock *Succ = *I;
336 uint32_t Weight = getEdgeWeight(BB, Succ);
337 uint32_t PrevSum = Sum;
340 assert(Sum > PrevSum); (void) PrevSum;
342 if (Weight > MaxWeight) {
348 // FIXME: Use BranchProbability::compare.
349 if ((uint64_t)MaxWeight * 5 > (uint64_t)Sum * 4)
355 // Return edge's weight. If can't find it, return DEFAULT_WEIGHT value.
357 BranchProbabilityInfo::getEdgeWeight(BasicBlock *Src, BasicBlock *Dst) const {
359 DenseMap<Edge, uint32_t>::const_iterator I = Weights.find(E);
361 if (I != Weights.end())
364 return DEFAULT_WEIGHT;
367 void BranchProbabilityInfo::setEdgeWeight(BasicBlock *Src, BasicBlock *Dst,
369 Weights[std::make_pair(Src, Dst)] = Weight;
370 DEBUG(dbgs() << "set edge " << Src->getNameStr() << " -> "
371 << Dst->getNameStr() << " weight to " << Weight
372 << (isEdgeHot(Src, Dst) ? " [is HOT now]\n" : "\n"));
376 BranchProbability BranchProbabilityInfo::
377 getEdgeProbability(BasicBlock *Src, BasicBlock *Dst) const {
379 uint32_t N = getEdgeWeight(Src, Dst);
380 uint32_t D = getSumForBlock(Src);
382 return BranchProbability(N, D);
386 BranchProbabilityInfo::printEdgeProbability(raw_ostream &OS, BasicBlock *Src,
387 BasicBlock *Dst) const {
389 const BranchProbability Prob = getEdgeProbability(Src, Dst);
390 OS << "edge " << Src->getNameStr() << " -> " << Dst->getNameStr()
391 << " probability is " << Prob
392 << (isEdgeHot(Src, Dst) ? " [HOT edge]\n" : "\n");