Andrew Trick | 9e76422 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 1 | //===-- 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 | |
Jakub Staszak | a5dd550 | 2011-07-31 03:27:24 +0000 | [diff] [blame] | 14 | #include "llvm/Constants.h" |
Chandler Carruth | 14edd31 | 2011-10-23 21:21:50 +0000 | [diff] [blame] | 15 | #include "llvm/Function.h" |
Andrew Trick | 9e76422 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 16 | #include "llvm/Instructions.h" |
Chandler Carruth | 99d01c5 | 2011-10-19 10:30:30 +0000 | [diff] [blame] | 17 | #include "llvm/LLVMContext.h" |
| 18 | #include "llvm/Metadata.h" |
Andrew Trick | 9e76422 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 19 | #include "llvm/Analysis/BranchProbabilityInfo.h" |
Jakub Staszak | 12af93a | 2011-07-16 20:31:15 +0000 | [diff] [blame] | 20 | #include "llvm/Analysis/LoopInfo.h" |
Chandler Carruth | 14edd31 | 2011-10-23 21:21:50 +0000 | [diff] [blame] | 21 | #include "llvm/Support/CFG.h" |
Andrew Trick | f289df2 | 2011-06-11 01:05:22 +0000 | [diff] [blame] | 22 | #include "llvm/Support/Debug.h" |
Andrew Trick | 9e76422 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 23 | |
| 24 | using namespace llvm; |
| 25 | |
| 26 | INITIALIZE_PASS_BEGIN(BranchProbabilityInfo, "branch-prob", |
| 27 | "Branch Probability Analysis", false, true) |
| 28 | INITIALIZE_PASS_DEPENDENCY(LoopInfo) |
| 29 | INITIALIZE_PASS_END(BranchProbabilityInfo, "branch-prob", |
| 30 | "Branch Probability Analysis", false, true) |
| 31 | |
| 32 | char BranchProbabilityInfo::ID = 0; |
| 33 | |
Chandler Carruth | b068bbba | 2011-10-24 01:40:45 +0000 | [diff] [blame^] | 34 | // Weights are for internal use only. They are used by heuristics to help to |
| 35 | // estimate edges' probability. Example: |
| 36 | // |
| 37 | // Using "Loop Branch Heuristics" we predict weights of edges for the |
| 38 | // block BB2. |
| 39 | // ... |
| 40 | // | |
| 41 | // V |
| 42 | // BB1<-+ |
| 43 | // | | |
| 44 | // | | (Weight = 124) |
| 45 | // V | |
| 46 | // BB2--+ |
| 47 | // | |
| 48 | // | (Weight = 4) |
| 49 | // V |
| 50 | // BB3 |
| 51 | // |
| 52 | // Probability of the edge BB2->BB1 = 124 / (124 + 4) = 0.96875 |
| 53 | // Probability of the edge BB2->BB3 = 4 / (124 + 4) = 0.03125 |
| 54 | static const uint32_t LBH_TAKEN_WEIGHT = 124; |
| 55 | static const uint32_t LBH_NONTAKEN_WEIGHT = 4; |
Andrew Trick | 9e76422 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 56 | |
Chandler Carruth | b068bbba | 2011-10-24 01:40:45 +0000 | [diff] [blame^] | 57 | static const uint32_t RH_TAKEN_WEIGHT = 24; |
| 58 | static const uint32_t RH_NONTAKEN_WEIGHT = 8; |
Andrew Trick | 9e76422 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 59 | |
Chandler Carruth | b068bbba | 2011-10-24 01:40:45 +0000 | [diff] [blame^] | 60 | static const uint32_t PH_TAKEN_WEIGHT = 20; |
| 61 | static const uint32_t PH_NONTAKEN_WEIGHT = 12; |
Andrew Trick | 9e76422 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 62 | |
Chandler Carruth | b068bbba | 2011-10-24 01:40:45 +0000 | [diff] [blame^] | 63 | static const uint32_t ZH_TAKEN_WEIGHT = 20; |
| 64 | static const uint32_t ZH_NONTAKEN_WEIGHT = 12; |
Andrew Trick | 9e76422 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 65 | |
Chandler Carruth | b068bbba | 2011-10-24 01:40:45 +0000 | [diff] [blame^] | 66 | static const uint32_t FPH_TAKEN_WEIGHT = 20; |
| 67 | static const uint32_t FPH_NONTAKEN_WEIGHT = 12; |
Andrew Trick | 9e76422 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 68 | |
Chandler Carruth | b068bbba | 2011-10-24 01:40:45 +0000 | [diff] [blame^] | 69 | // Standard weight value. Used when none of the heuristics set weight for |
| 70 | // the edge. |
| 71 | static const uint32_t NORMAL_WEIGHT = 16; |
Andrew Trick | 9e76422 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 72 | |
Chandler Carruth | b068bbba | 2011-10-24 01:40:45 +0000 | [diff] [blame^] | 73 | // Minimum weight of an edge. Please note, that weight is NEVER 0. |
| 74 | static const uint32_t MIN_WEIGHT = 1; |
Andrew Trick | 9e76422 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 75 | |
Chandler Carruth | b068bbba | 2011-10-24 01:40:45 +0000 | [diff] [blame^] | 76 | // Return TRUE if BB leads directly to a Return Instruction. |
| 77 | static bool isReturningBlock(BasicBlock *BB) { |
| 78 | SmallPtrSet<BasicBlock *, 8> Visited; |
Jakub Staszak | e0058b4 | 2011-07-29 02:36:53 +0000 | [diff] [blame] | 79 | |
Chandler Carruth | b068bbba | 2011-10-24 01:40:45 +0000 | [diff] [blame^] | 80 | while (true) { |
| 81 | TerminatorInst *TI = BB->getTerminator(); |
| 82 | if (isa<ReturnInst>(TI)) |
| 83 | return true; |
Jakub Staszak | e0058b4 | 2011-07-29 02:36:53 +0000 | [diff] [blame] | 84 | |
Chandler Carruth | b068bbba | 2011-10-24 01:40:45 +0000 | [diff] [blame^] | 85 | if (TI->getNumSuccessors() > 1) |
| 86 | break; |
Jakub Staszak | a5dd550 | 2011-07-31 03:27:24 +0000 | [diff] [blame] | 87 | |
Chandler Carruth | b068bbba | 2011-10-24 01:40:45 +0000 | [diff] [blame^] | 88 | // It is unreachable block which we can consider as a return instruction. |
| 89 | if (TI->getNumSuccessors() == 0) |
| 90 | return true; |
Benjamin Kramer | c888aa4 | 2011-10-21 20:12:47 +0000 | [diff] [blame] | 91 | |
Chandler Carruth | b068bbba | 2011-10-24 01:40:45 +0000 | [diff] [blame^] | 92 | Visited.insert(BB); |
| 93 | BB = TI->getSuccessor(0); |
Andrew Trick | 9e76422 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 94 | |
Chandler Carruth | b068bbba | 2011-10-24 01:40:45 +0000 | [diff] [blame^] | 95 | // Stop if cycle is detected. |
| 96 | if (Visited.count(BB)) |
| 97 | return false; |
Andrew Trick | 9e76422 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 98 | } |
| 99 | |
Chandler Carruth | b068bbba | 2011-10-24 01:40:45 +0000 | [diff] [blame^] | 100 | return false; |
| 101 | } |
Andrew Trick | 9e76422 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 102 | |
Chandler Carruth | b068bbba | 2011-10-24 01:40:45 +0000 | [diff] [blame^] | 103 | static uint32_t getMaxWeightFor(BasicBlock *BB) { |
| 104 | return UINT32_MAX / BB->getTerminator()->getNumSuccessors(); |
| 105 | } |
Andrew Trick | 9e76422 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 106 | |
Andrew Trick | 9e76422 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 107 | |
Chandler Carruth | 99d01c5 | 2011-10-19 10:30:30 +0000 | [diff] [blame] | 108 | // Propagate existing explicit probabilities from either profile data or |
| 109 | // 'expect' intrinsic processing. |
Chandler Carruth | b068bbba | 2011-10-24 01:40:45 +0000 | [diff] [blame^] | 110 | bool BranchProbabilityInfo::calcMetadataWeights(BasicBlock *BB) { |
Chandler Carruth | 941aa7b | 2011-10-19 10:32:19 +0000 | [diff] [blame] | 111 | TerminatorInst *TI = BB->getTerminator(); |
| 112 | if (TI->getNumSuccessors() == 1) |
| 113 | return false; |
| 114 | if (!isa<BranchInst>(TI) && !isa<SwitchInst>(TI)) |
Chandler Carruth | 99d01c5 | 2011-10-19 10:30:30 +0000 | [diff] [blame] | 115 | return false; |
| 116 | |
Chandler Carruth | 941aa7b | 2011-10-19 10:32:19 +0000 | [diff] [blame] | 117 | MDNode *WeightsNode = TI->getMetadata(LLVMContext::MD_prof); |
| 118 | if (!WeightsNode) |
Chandler Carruth | 99d01c5 | 2011-10-19 10:30:30 +0000 | [diff] [blame] | 119 | return false; |
| 120 | |
Chandler Carruth | 941aa7b | 2011-10-19 10:32:19 +0000 | [diff] [blame] | 121 | // Ensure there are weights for all of the successors. Note that the first |
| 122 | // operand to the metadata node is a name, not a weight. |
| 123 | if (WeightsNode->getNumOperands() != TI->getNumSuccessors() + 1) |
Chandler Carruth | 99d01c5 | 2011-10-19 10:30:30 +0000 | [diff] [blame] | 124 | return false; |
| 125 | |
Chandler Carruth | 941aa7b | 2011-10-19 10:32:19 +0000 | [diff] [blame] | 126 | // Build up the final weights that will be used in a temporary buffer, but |
| 127 | // don't add them until all weihts are present. Each weight value is clamped |
| 128 | // to [1, getMaxWeightFor(BB)]. |
Chandler Carruth | 99d01c5 | 2011-10-19 10:30:30 +0000 | [diff] [blame] | 129 | uint32_t WeightLimit = getMaxWeightFor(BB); |
Chandler Carruth | 941aa7b | 2011-10-19 10:32:19 +0000 | [diff] [blame] | 130 | SmallVector<uint32_t, 2> Weights; |
| 131 | Weights.reserve(TI->getNumSuccessors()); |
| 132 | for (unsigned i = 1, e = WeightsNode->getNumOperands(); i != e; ++i) { |
| 133 | ConstantInt *Weight = dyn_cast<ConstantInt>(WeightsNode->getOperand(i)); |
| 134 | if (!Weight) |
| 135 | return false; |
| 136 | Weights.push_back( |
| 137 | std::max<uint32_t>(1, Weight->getLimitedValue(WeightLimit))); |
| 138 | } |
| 139 | assert(Weights.size() == TI->getNumSuccessors() && "Checked above"); |
| 140 | for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i) |
Chandler Carruth | b068bbba | 2011-10-24 01:40:45 +0000 | [diff] [blame^] | 141 | setEdgeWeight(BB, TI->getSuccessor(i), Weights[i]); |
Chandler Carruth | 99d01c5 | 2011-10-19 10:30:30 +0000 | [diff] [blame] | 142 | |
| 143 | return true; |
| 144 | } |
| 145 | |
Andrew Trick | 9e76422 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 146 | // Calculate Edge Weights using "Return Heuristics". Predict a successor which |
| 147 | // leads directly to Return Instruction will not be taken. |
Chandler Carruth | b068bbba | 2011-10-24 01:40:45 +0000 | [diff] [blame^] | 148 | bool BranchProbabilityInfo::calcReturnHeuristics(BasicBlock *BB){ |
Andrew Trick | 9e76422 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 149 | if (BB->getTerminator()->getNumSuccessors() == 1) |
Jakub Staszak | 7241caf | 2011-07-28 21:45:07 +0000 | [diff] [blame] | 150 | return false; |
Andrew Trick | 9e76422 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 151 | |
Jakub Staszak | b137f16 | 2011-08-01 19:16:26 +0000 | [diff] [blame] | 152 | SmallPtrSet<BasicBlock *, 4> ReturningEdges; |
| 153 | SmallPtrSet<BasicBlock *, 4> StayEdges; |
Jakub Staszak | e0058b4 | 2011-07-29 02:36:53 +0000 | [diff] [blame] | 154 | |
Andrew Trick | 9e76422 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 155 | for (succ_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I) { |
| 156 | BasicBlock *Succ = *I; |
Jakub Staszak | e0058b4 | 2011-07-29 02:36:53 +0000 | [diff] [blame] | 157 | if (isReturningBlock(Succ)) |
Jakub Staszak | b137f16 | 2011-08-01 19:16:26 +0000 | [diff] [blame] | 158 | ReturningEdges.insert(Succ); |
Jakub Staszak | e0058b4 | 2011-07-29 02:36:53 +0000 | [diff] [blame] | 159 | else |
Jakub Staszak | b137f16 | 2011-08-01 19:16:26 +0000 | [diff] [blame] | 160 | StayEdges.insert(Succ); |
Jakub Staszak | e0058b4 | 2011-07-29 02:36:53 +0000 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | if (uint32_t numStayEdges = StayEdges.size()) { |
| 164 | uint32_t stayWeight = RH_TAKEN_WEIGHT / numStayEdges; |
| 165 | if (stayWeight < NORMAL_WEIGHT) |
| 166 | stayWeight = NORMAL_WEIGHT; |
| 167 | |
Jakub Staszak | b137f16 | 2011-08-01 19:16:26 +0000 | [diff] [blame] | 168 | for (SmallPtrSet<BasicBlock *, 4>::iterator I = StayEdges.begin(), |
Jakub Staszak | e0058b4 | 2011-07-29 02:36:53 +0000 | [diff] [blame] | 169 | E = StayEdges.end(); I != E; ++I) |
Chandler Carruth | b068bbba | 2011-10-24 01:40:45 +0000 | [diff] [blame^] | 170 | setEdgeWeight(BB, *I, stayWeight); |
Jakub Staszak | e0058b4 | 2011-07-29 02:36:53 +0000 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | if (uint32_t numRetEdges = ReturningEdges.size()) { |
| 174 | uint32_t retWeight = RH_NONTAKEN_WEIGHT / numRetEdges; |
| 175 | if (retWeight < MIN_WEIGHT) |
| 176 | retWeight = MIN_WEIGHT; |
Jakub Staszak | b137f16 | 2011-08-01 19:16:26 +0000 | [diff] [blame] | 177 | for (SmallPtrSet<BasicBlock *, 4>::iterator I = ReturningEdges.begin(), |
Jakub Staszak | e0058b4 | 2011-07-29 02:36:53 +0000 | [diff] [blame] | 178 | E = ReturningEdges.end(); I != E; ++I) { |
Chandler Carruth | b068bbba | 2011-10-24 01:40:45 +0000 | [diff] [blame^] | 179 | setEdgeWeight(BB, *I, retWeight); |
Andrew Trick | 9e76422 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 180 | } |
| 181 | } |
Jakub Staszak | 7241caf | 2011-07-28 21:45:07 +0000 | [diff] [blame] | 182 | |
Jakub Staszak | e0058b4 | 2011-07-29 02:36:53 +0000 | [diff] [blame] | 183 | return ReturningEdges.size() > 0; |
Andrew Trick | 9e76422 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | // Calculate Edge Weights using "Pointer Heuristics". Predict a comparsion |
| 187 | // between two pointer or pointer and NULL will fail. |
Chandler Carruth | b068bbba | 2011-10-24 01:40:45 +0000 | [diff] [blame^] | 188 | bool BranchProbabilityInfo::calcPointerHeuristics(BasicBlock *BB) { |
Andrew Trick | 9e76422 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 189 | BranchInst * BI = dyn_cast<BranchInst>(BB->getTerminator()); |
| 190 | if (!BI || !BI->isConditional()) |
Jakub Staszak | 7241caf | 2011-07-28 21:45:07 +0000 | [diff] [blame] | 191 | return false; |
Andrew Trick | 9e76422 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 192 | |
| 193 | Value *Cond = BI->getCondition(); |
| 194 | ICmpInst *CI = dyn_cast<ICmpInst>(Cond); |
Jakub Staszak | d7932ca | 2011-07-15 20:51:06 +0000 | [diff] [blame] | 195 | if (!CI || !CI->isEquality()) |
Jakub Staszak | 7241caf | 2011-07-28 21:45:07 +0000 | [diff] [blame] | 196 | return false; |
Andrew Trick | 9e76422 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 197 | |
| 198 | Value *LHS = CI->getOperand(0); |
Andrew Trick | 9e76422 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 199 | |
| 200 | if (!LHS->getType()->isPointerTy()) |
Jakub Staszak | 7241caf | 2011-07-28 21:45:07 +0000 | [diff] [blame] | 201 | return false; |
Andrew Trick | 9e76422 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 202 | |
Nick Lewycky | 404b53e | 2011-06-04 02:07:10 +0000 | [diff] [blame] | 203 | assert(CI->getOperand(1)->getType()->isPointerTy()); |
Andrew Trick | 9e76422 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 204 | |
| 205 | BasicBlock *Taken = BI->getSuccessor(0); |
| 206 | BasicBlock *NonTaken = BI->getSuccessor(1); |
| 207 | |
| 208 | // p != 0 -> isProb = true |
| 209 | // p == 0 -> isProb = false |
| 210 | // p != q -> isProb = true |
| 211 | // p == q -> isProb = false; |
Jakub Staszak | d7932ca | 2011-07-15 20:51:06 +0000 | [diff] [blame] | 212 | bool isProb = CI->getPredicate() == ICmpInst::ICMP_NE; |
Andrew Trick | 9e76422 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 213 | if (!isProb) |
| 214 | std::swap(Taken, NonTaken); |
| 215 | |
Chandler Carruth | b068bbba | 2011-10-24 01:40:45 +0000 | [diff] [blame^] | 216 | setEdgeWeight(BB, Taken, PH_TAKEN_WEIGHT); |
| 217 | setEdgeWeight(BB, NonTaken, PH_NONTAKEN_WEIGHT); |
Jakub Staszak | 7241caf | 2011-07-28 21:45:07 +0000 | [diff] [blame] | 218 | return true; |
Andrew Trick | 9e76422 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | // Calculate Edge Weights using "Loop Branch Heuristics". Predict backedges |
| 222 | // as taken, exiting edges as not-taken. |
Chandler Carruth | b068bbba | 2011-10-24 01:40:45 +0000 | [diff] [blame^] | 223 | bool BranchProbabilityInfo::calcLoopBranchHeuristics(BasicBlock *BB) { |
Andrew Trick | f289df2 | 2011-06-11 01:05:22 +0000 | [diff] [blame] | 224 | uint32_t numSuccs = BB->getTerminator()->getNumSuccessors(); |
Andrew Trick | 9e76422 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 225 | |
| 226 | Loop *L = LI->getLoopFor(BB); |
| 227 | if (!L) |
Jakub Staszak | 7241caf | 2011-07-28 21:45:07 +0000 | [diff] [blame] | 228 | return false; |
Andrew Trick | 9e76422 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 229 | |
Jakub Staszak | b137f16 | 2011-08-01 19:16:26 +0000 | [diff] [blame] | 230 | SmallPtrSet<BasicBlock *, 8> BackEdges; |
| 231 | SmallPtrSet<BasicBlock *, 8> ExitingEdges; |
| 232 | SmallPtrSet<BasicBlock *, 8> InEdges; // Edges from header to the loop. |
Jakub Staszak | fa44725 | 2011-07-28 21:33:46 +0000 | [diff] [blame] | 233 | |
| 234 | bool isHeader = BB == L->getHeader(); |
Andrew Trick | 9e76422 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 235 | |
| 236 | for (succ_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I) { |
| 237 | BasicBlock *Succ = *I; |
| 238 | Loop *SuccL = LI->getLoopFor(Succ); |
| 239 | if (SuccL != L) |
Jakub Staszak | b137f16 | 2011-08-01 19:16:26 +0000 | [diff] [blame] | 240 | ExitingEdges.insert(Succ); |
Andrew Trick | 9e76422 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 241 | else if (Succ == L->getHeader()) |
Jakub Staszak | b137f16 | 2011-08-01 19:16:26 +0000 | [diff] [blame] | 242 | BackEdges.insert(Succ); |
Jakub Staszak | fa44725 | 2011-07-28 21:33:46 +0000 | [diff] [blame] | 243 | else if (isHeader) |
Jakub Staszak | b137f16 | 2011-08-01 19:16:26 +0000 | [diff] [blame] | 244 | InEdges.insert(Succ); |
Andrew Trick | 9e76422 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 245 | } |
| 246 | |
Andrew Trick | f289df2 | 2011-06-11 01:05:22 +0000 | [diff] [blame] | 247 | if (uint32_t numBackEdges = BackEdges.size()) { |
| 248 | uint32_t backWeight = LBH_TAKEN_WEIGHT / numBackEdges; |
Andrew Trick | 9e76422 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 249 | if (backWeight < NORMAL_WEIGHT) |
| 250 | backWeight = NORMAL_WEIGHT; |
| 251 | |
Jakub Staszak | b137f16 | 2011-08-01 19:16:26 +0000 | [diff] [blame] | 252 | for (SmallPtrSet<BasicBlock *, 8>::iterator EI = BackEdges.begin(), |
Andrew Trick | 9e76422 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 253 | EE = BackEdges.end(); EI != EE; ++EI) { |
| 254 | BasicBlock *Back = *EI; |
Chandler Carruth | b068bbba | 2011-10-24 01:40:45 +0000 | [diff] [blame^] | 255 | setEdgeWeight(BB, Back, backWeight); |
Andrew Trick | 9e76422 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 256 | } |
| 257 | } |
| 258 | |
Jakub Staszak | fa44725 | 2011-07-28 21:33:46 +0000 | [diff] [blame] | 259 | if (uint32_t numInEdges = InEdges.size()) { |
| 260 | uint32_t inWeight = LBH_TAKEN_WEIGHT / numInEdges; |
| 261 | if (inWeight < NORMAL_WEIGHT) |
| 262 | inWeight = NORMAL_WEIGHT; |
| 263 | |
Jakub Staszak | b137f16 | 2011-08-01 19:16:26 +0000 | [diff] [blame] | 264 | for (SmallPtrSet<BasicBlock *, 8>::iterator EI = InEdges.begin(), |
Jakub Staszak | fa44725 | 2011-07-28 21:33:46 +0000 | [diff] [blame] | 265 | EE = InEdges.end(); EI != EE; ++EI) { |
| 266 | BasicBlock *Back = *EI; |
Chandler Carruth | b068bbba | 2011-10-24 01:40:45 +0000 | [diff] [blame^] | 267 | setEdgeWeight(BB, Back, inWeight); |
Jakub Staszak | fa44725 | 2011-07-28 21:33:46 +0000 | [diff] [blame] | 268 | } |
| 269 | } |
| 270 | |
Andrew Trick | f289df2 | 2011-06-11 01:05:22 +0000 | [diff] [blame] | 271 | uint32_t numExitingEdges = ExitingEdges.size(); |
| 272 | if (uint32_t numNonExitingEdges = numSuccs - numExitingEdges) { |
| 273 | uint32_t exitWeight = LBH_NONTAKEN_WEIGHT / numNonExitingEdges; |
Andrew Trick | 9e76422 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 274 | if (exitWeight < MIN_WEIGHT) |
| 275 | exitWeight = MIN_WEIGHT; |
| 276 | |
Jakub Staszak | b137f16 | 2011-08-01 19:16:26 +0000 | [diff] [blame] | 277 | for (SmallPtrSet<BasicBlock *, 8>::iterator EI = ExitingEdges.begin(), |
Andrew Trick | 9e76422 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 278 | EE = ExitingEdges.end(); EI != EE; ++EI) { |
| 279 | BasicBlock *Exiting = *EI; |
Chandler Carruth | b068bbba | 2011-10-24 01:40:45 +0000 | [diff] [blame^] | 280 | setEdgeWeight(BB, Exiting, exitWeight); |
Andrew Trick | 9e76422 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 281 | } |
| 282 | } |
Jakub Staszak | 7241caf | 2011-07-28 21:45:07 +0000 | [diff] [blame] | 283 | |
| 284 | return true; |
Andrew Trick | 9e76422 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 285 | } |
| 286 | |
Chandler Carruth | b068bbba | 2011-10-24 01:40:45 +0000 | [diff] [blame^] | 287 | bool BranchProbabilityInfo::calcZeroHeuristics(BasicBlock *BB) { |
Jakub Staszak | a5dd550 | 2011-07-31 03:27:24 +0000 | [diff] [blame] | 288 | BranchInst * BI = dyn_cast<BranchInst>(BB->getTerminator()); |
| 289 | if (!BI || !BI->isConditional()) |
| 290 | return false; |
| 291 | |
| 292 | Value *Cond = BI->getCondition(); |
| 293 | ICmpInst *CI = dyn_cast<ICmpInst>(Cond); |
| 294 | if (!CI) |
| 295 | return false; |
| 296 | |
Jakub Staszak | a5dd550 | 2011-07-31 03:27:24 +0000 | [diff] [blame] | 297 | Value *RHS = CI->getOperand(1); |
Jakub Staszak | a385c20 | 2011-07-31 04:47:20 +0000 | [diff] [blame] | 298 | ConstantInt *CV = dyn_cast<ConstantInt>(RHS); |
Benjamin Kramer | 26eb870 | 2011-09-04 23:53:04 +0000 | [diff] [blame] | 299 | if (!CV) |
Jakub Staszak | a5dd550 | 2011-07-31 03:27:24 +0000 | [diff] [blame] | 300 | return false; |
| 301 | |
| 302 | bool isProb; |
Benjamin Kramer | 26eb870 | 2011-09-04 23:53:04 +0000 | [diff] [blame] | 303 | if (CV->isZero()) { |
| 304 | switch (CI->getPredicate()) { |
| 305 | case CmpInst::ICMP_EQ: |
| 306 | // X == 0 -> Unlikely |
| 307 | isProb = false; |
| 308 | break; |
| 309 | case CmpInst::ICMP_NE: |
| 310 | // X != 0 -> Likely |
| 311 | isProb = true; |
| 312 | break; |
| 313 | case CmpInst::ICMP_SLT: |
| 314 | // X < 0 -> Unlikely |
| 315 | isProb = false; |
| 316 | break; |
| 317 | case CmpInst::ICMP_SGT: |
| 318 | // X > 0 -> Likely |
| 319 | isProb = true; |
| 320 | break; |
| 321 | default: |
| 322 | return false; |
| 323 | } |
| 324 | } else if (CV->isOne() && CI->getPredicate() == CmpInst::ICMP_SLT) { |
| 325 | // InstCombine canonicalizes X <= 0 into X < 1. |
| 326 | // X <= 0 -> Unlikely |
Jakub Staszak | a5dd550 | 2011-07-31 03:27:24 +0000 | [diff] [blame] | 327 | isProb = false; |
Benjamin Kramer | 26eb870 | 2011-09-04 23:53:04 +0000 | [diff] [blame] | 328 | } else if (CV->isAllOnesValue() && CI->getPredicate() == CmpInst::ICMP_SGT) { |
| 329 | // InstCombine canonicalizes X >= 0 into X > -1. |
| 330 | // X >= 0 -> Likely |
Jakub Staszak | a5dd550 | 2011-07-31 03:27:24 +0000 | [diff] [blame] | 331 | isProb = true; |
Benjamin Kramer | 26eb870 | 2011-09-04 23:53:04 +0000 | [diff] [blame] | 332 | } else { |
Jakub Staszak | a5dd550 | 2011-07-31 03:27:24 +0000 | [diff] [blame] | 333 | return false; |
Benjamin Kramer | 26eb870 | 2011-09-04 23:53:04 +0000 | [diff] [blame] | 334 | } |
Jakub Staszak | a5dd550 | 2011-07-31 03:27:24 +0000 | [diff] [blame] | 335 | |
| 336 | BasicBlock *Taken = BI->getSuccessor(0); |
| 337 | BasicBlock *NonTaken = BI->getSuccessor(1); |
| 338 | |
| 339 | if (!isProb) |
| 340 | std::swap(Taken, NonTaken); |
| 341 | |
Chandler Carruth | b068bbba | 2011-10-24 01:40:45 +0000 | [diff] [blame^] | 342 | setEdgeWeight(BB, Taken, ZH_TAKEN_WEIGHT); |
| 343 | setEdgeWeight(BB, NonTaken, ZH_NONTAKEN_WEIGHT); |
Jakub Staszak | a5dd550 | 2011-07-31 03:27:24 +0000 | [diff] [blame] | 344 | |
| 345 | return true; |
| 346 | } |
| 347 | |
Chandler Carruth | b068bbba | 2011-10-24 01:40:45 +0000 | [diff] [blame^] | 348 | bool BranchProbabilityInfo::calcFloatingPointHeuristics(BasicBlock *BB) { |
Benjamin Kramer | c888aa4 | 2011-10-21 20:12:47 +0000 | [diff] [blame] | 349 | BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator()); |
| 350 | if (!BI || !BI->isConditional()) |
| 351 | return false; |
| 352 | |
| 353 | Value *Cond = BI->getCondition(); |
| 354 | FCmpInst *FCmp = dyn_cast<FCmpInst>(Cond); |
Benjamin Kramer | 675c02b | 2011-10-21 21:13:47 +0000 | [diff] [blame] | 355 | if (!FCmp) |
Benjamin Kramer | c888aa4 | 2011-10-21 20:12:47 +0000 | [diff] [blame] | 356 | return false; |
| 357 | |
Benjamin Kramer | 675c02b | 2011-10-21 21:13:47 +0000 | [diff] [blame] | 358 | bool isProb; |
| 359 | if (FCmp->isEquality()) { |
| 360 | // f1 == f2 -> Unlikely |
| 361 | // f1 != f2 -> Likely |
| 362 | isProb = !FCmp->isTrueWhenEqual(); |
| 363 | } else if (FCmp->getPredicate() == FCmpInst::FCMP_ORD) { |
| 364 | // !isnan -> Likely |
| 365 | isProb = true; |
| 366 | } else if (FCmp->getPredicate() == FCmpInst::FCMP_UNO) { |
| 367 | // isnan -> Unlikely |
| 368 | isProb = false; |
| 369 | } else { |
| 370 | return false; |
| 371 | } |
| 372 | |
Benjamin Kramer | c888aa4 | 2011-10-21 20:12:47 +0000 | [diff] [blame] | 373 | BasicBlock *Taken = BI->getSuccessor(0); |
| 374 | BasicBlock *NonTaken = BI->getSuccessor(1); |
| 375 | |
Benjamin Kramer | 675c02b | 2011-10-21 21:13:47 +0000 | [diff] [blame] | 376 | if (!isProb) |
Benjamin Kramer | c888aa4 | 2011-10-21 20:12:47 +0000 | [diff] [blame] | 377 | std::swap(Taken, NonTaken); |
| 378 | |
Chandler Carruth | b068bbba | 2011-10-24 01:40:45 +0000 | [diff] [blame^] | 379 | setEdgeWeight(BB, Taken, FPH_TAKEN_WEIGHT); |
| 380 | setEdgeWeight(BB, NonTaken, FPH_NONTAKEN_WEIGHT); |
Benjamin Kramer | c888aa4 | 2011-10-21 20:12:47 +0000 | [diff] [blame] | 381 | |
| 382 | return true; |
| 383 | } |
Jakub Staszak | a5dd550 | 2011-07-31 03:27:24 +0000 | [diff] [blame] | 384 | |
Chandler Carruth | b068bbba | 2011-10-24 01:40:45 +0000 | [diff] [blame^] | 385 | void BranchProbabilityInfo::getAnalysisUsage(AnalysisUsage &AU) const { |
| 386 | AU.addRequired<LoopInfo>(); |
| 387 | AU.setPreservesAll(); |
| 388 | } |
| 389 | |
| 390 | bool BranchProbabilityInfo::runOnFunction(Function &F) { |
| 391 | LastF = &F; // Store the last function we ran on for printing. |
| 392 | LI = &getAnalysis<LoopInfo>(); |
| 393 | |
Chandler Carruth | 22c8946 | 2011-10-23 22:40:13 +0000 | [diff] [blame] | 394 | for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) { |
| 395 | if (calcMetadataWeights(I)) |
Chandler Carruth | 99d01c5 | 2011-10-19 10:30:30 +0000 | [diff] [blame] | 396 | continue; |
Chandler Carruth | 22c8946 | 2011-10-23 22:40:13 +0000 | [diff] [blame] | 397 | if (calcLoopBranchHeuristics(I)) |
Jakub Staszak | 7241caf | 2011-07-28 21:45:07 +0000 | [diff] [blame] | 398 | continue; |
Chandler Carruth | 22c8946 | 2011-10-23 22:40:13 +0000 | [diff] [blame] | 399 | if (calcReturnHeuristics(I)) |
Jakub Staszak | 7241caf | 2011-07-28 21:45:07 +0000 | [diff] [blame] | 400 | continue; |
Chandler Carruth | 22c8946 | 2011-10-23 22:40:13 +0000 | [diff] [blame] | 401 | if (calcPointerHeuristics(I)) |
Jakub Staszak | a5dd550 | 2011-07-31 03:27:24 +0000 | [diff] [blame] | 402 | continue; |
Chandler Carruth | 22c8946 | 2011-10-23 22:40:13 +0000 | [diff] [blame] | 403 | if (calcZeroHeuristics(I)) |
Benjamin Kramer | c888aa4 | 2011-10-21 20:12:47 +0000 | [diff] [blame] | 404 | continue; |
Chandler Carruth | 22c8946 | 2011-10-23 22:40:13 +0000 | [diff] [blame] | 405 | calcFloatingPointHeuristics(I); |
Andrew Trick | 9e76422 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 406 | } |
Andrew Trick | 9e76422 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 407 | return false; |
| 408 | } |
| 409 | |
Chandler Carruth | 14edd31 | 2011-10-23 21:21:50 +0000 | [diff] [blame] | 410 | void BranchProbabilityInfo::print(raw_ostream &OS, const Module *) const { |
| 411 | OS << "---- Branch Probabilities ----\n"; |
| 412 | // We print the probabilities from the last function the analysis ran over, |
| 413 | // or the function it is currently running over. |
| 414 | assert(LastF && "Cannot print prior to running over a function"); |
| 415 | for (Function::const_iterator BI = LastF->begin(), BE = LastF->end(); |
| 416 | BI != BE; ++BI) { |
| 417 | for (succ_const_iterator SI = succ_begin(BI), SE = succ_end(BI); |
| 418 | SI != SE; ++SI) { |
| 419 | printEdgeProbability(OS << " ", BI, *SI); |
| 420 | } |
| 421 | } |
| 422 | } |
| 423 | |
Jakub Staszak | 6f6baf1 | 2011-07-29 19:30:00 +0000 | [diff] [blame] | 424 | uint32_t BranchProbabilityInfo::getSumForBlock(const BasicBlock *BB) const { |
Andrew Trick | f289df2 | 2011-06-11 01:05:22 +0000 | [diff] [blame] | 425 | uint32_t Sum = 0; |
| 426 | |
Jakub Staszak | 6f6baf1 | 2011-07-29 19:30:00 +0000 | [diff] [blame] | 427 | for (succ_const_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I) { |
| 428 | const BasicBlock *Succ = *I; |
Andrew Trick | f289df2 | 2011-06-11 01:05:22 +0000 | [diff] [blame] | 429 | uint32_t Weight = getEdgeWeight(BB, Succ); |
| 430 | uint32_t PrevSum = Sum; |
Andrew Trick | 9e76422 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 431 | |
| 432 | Sum += Weight; |
| 433 | assert(Sum > PrevSum); (void) PrevSum; |
| 434 | } |
| 435 | |
Andrew Trick | f289df2 | 2011-06-11 01:05:22 +0000 | [diff] [blame] | 436 | return Sum; |
| 437 | } |
| 438 | |
Jakub Staszak | 6f6baf1 | 2011-07-29 19:30:00 +0000 | [diff] [blame] | 439 | bool BranchProbabilityInfo:: |
| 440 | isEdgeHot(const BasicBlock *Src, const BasicBlock *Dst) const { |
Andrew Trick | f289df2 | 2011-06-11 01:05:22 +0000 | [diff] [blame] | 441 | // Hot probability is at least 4/5 = 80% |
Benjamin Kramer | 341473c | 2011-10-23 11:19:14 +0000 | [diff] [blame] | 442 | // FIXME: Compare against a static "hot" BranchProbability. |
| 443 | return getEdgeProbability(Src, Dst) > BranchProbability(4, 5); |
Andrew Trick | 9e76422 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | BasicBlock *BranchProbabilityInfo::getHotSucc(BasicBlock *BB) const { |
Andrew Trick | f289df2 | 2011-06-11 01:05:22 +0000 | [diff] [blame] | 447 | uint32_t Sum = 0; |
| 448 | uint32_t MaxWeight = 0; |
Andrew Trick | 9e76422 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 449 | BasicBlock *MaxSucc = 0; |
| 450 | |
| 451 | for (succ_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I) { |
| 452 | BasicBlock *Succ = *I; |
Andrew Trick | f289df2 | 2011-06-11 01:05:22 +0000 | [diff] [blame] | 453 | uint32_t Weight = getEdgeWeight(BB, Succ); |
| 454 | uint32_t PrevSum = Sum; |
Andrew Trick | 9e76422 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 455 | |
| 456 | Sum += Weight; |
| 457 | assert(Sum > PrevSum); (void) PrevSum; |
| 458 | |
| 459 | if (Weight > MaxWeight) { |
| 460 | MaxWeight = Weight; |
| 461 | MaxSucc = Succ; |
| 462 | } |
| 463 | } |
| 464 | |
Benjamin Kramer | 341473c | 2011-10-23 11:19:14 +0000 | [diff] [blame] | 465 | // Hot probability is at least 4/5 = 80% |
| 466 | if (BranchProbability(MaxWeight, Sum) > BranchProbability(4, 5)) |
Andrew Trick | 9e76422 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 467 | return MaxSucc; |
| 468 | |
| 469 | return 0; |
| 470 | } |
| 471 | |
| 472 | // Return edge's weight. If can't find it, return DEFAULT_WEIGHT value. |
Jakub Staszak | 6f6baf1 | 2011-07-29 19:30:00 +0000 | [diff] [blame] | 473 | uint32_t BranchProbabilityInfo:: |
| 474 | getEdgeWeight(const BasicBlock *Src, const BasicBlock *Dst) const { |
Andrew Trick | 9e76422 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 475 | Edge E(Src, Dst); |
Andrew Trick | f289df2 | 2011-06-11 01:05:22 +0000 | [diff] [blame] | 476 | DenseMap<Edge, uint32_t>::const_iterator I = Weights.find(E); |
Andrew Trick | 9e76422 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 477 | |
| 478 | if (I != Weights.end()) |
| 479 | return I->second; |
| 480 | |
| 481 | return DEFAULT_WEIGHT; |
| 482 | } |
| 483 | |
Jakub Staszak | 6f6baf1 | 2011-07-29 19:30:00 +0000 | [diff] [blame] | 484 | void BranchProbabilityInfo:: |
| 485 | setEdgeWeight(const BasicBlock *Src, const BasicBlock *Dst, uint32_t Weight) { |
Andrew Trick | 9e76422 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 486 | Weights[std::make_pair(Src, Dst)] = Weight; |
Andrew Trick | f289df2 | 2011-06-11 01:05:22 +0000 | [diff] [blame] | 487 | DEBUG(dbgs() << "set edge " << Src->getNameStr() << " -> " |
| 488 | << Dst->getNameStr() << " weight to " << Weight |
| 489 | << (isEdgeHot(Src, Dst) ? " [is HOT now]\n" : "\n")); |
| 490 | } |
| 491 | |
| 492 | |
| 493 | BranchProbability BranchProbabilityInfo:: |
Jakub Staszak | 6f6baf1 | 2011-07-29 19:30:00 +0000 | [diff] [blame] | 494 | getEdgeProbability(const BasicBlock *Src, const BasicBlock *Dst) const { |
Andrew Trick | f289df2 | 2011-06-11 01:05:22 +0000 | [diff] [blame] | 495 | |
| 496 | uint32_t N = getEdgeWeight(Src, Dst); |
| 497 | uint32_t D = getSumForBlock(Src); |
| 498 | |
| 499 | return BranchProbability(N, D); |
Andrew Trick | 9e76422 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 500 | } |
| 501 | |
| 502 | raw_ostream & |
Chandler Carruth | 14edd31 | 2011-10-23 21:21:50 +0000 | [diff] [blame] | 503 | BranchProbabilityInfo::printEdgeProbability(raw_ostream &OS, |
| 504 | const BasicBlock *Src, |
| 505 | const BasicBlock *Dst) const { |
Andrew Trick | 9e76422 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 506 | |
Jakub Staszak | 7cc2b07 | 2011-06-16 20:22:37 +0000 | [diff] [blame] | 507 | const BranchProbability Prob = getEdgeProbability(Src, Dst); |
Andrew Trick | f289df2 | 2011-06-11 01:05:22 +0000 | [diff] [blame] | 508 | OS << "edge " << Src->getNameStr() << " -> " << Dst->getNameStr() |
| 509 | << " probability is " << Prob |
| 510 | << (isEdgeHot(Src, Dst) ? " [HOT edge]\n" : "\n"); |
Andrew Trick | 9e76422 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 511 | |
| 512 | return OS; |
| 513 | } |