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