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