Bill Wendling | e1c5426 | 2012-08-15 12:22:35 +0000 | [diff] [blame] | 1 | //===-- BranchProbabilityInfo.cpp - Branch Probability Analysis -----------===// |
Andrew Trick | 49371f3 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 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 | |
Michael Gottesman | fb9164f | 2013-12-14 02:24:25 +0000 | [diff] [blame] | 14 | #define DEBUG_TYPE "branch-prob" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 15 | #include "llvm/Analysis/BranchProbabilityInfo.h" |
| 16 | #include "llvm/ADT/PostOrderIterator.h" |
| 17 | #include "llvm/Analysis/LoopInfo.h" |
Chandler Carruth | 1305dc3 | 2014-03-04 11:45:46 +0000 | [diff] [blame] | 18 | #include "llvm/IR/CFG.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 19 | #include "llvm/IR/Constants.h" |
| 20 | #include "llvm/IR/Function.h" |
| 21 | #include "llvm/IR/Instructions.h" |
| 22 | #include "llvm/IR/LLVMContext.h" |
| 23 | #include "llvm/IR/Metadata.h" |
Andrew Trick | 3d4e64b | 2011-06-11 01:05:22 +0000 | [diff] [blame] | 24 | #include "llvm/Support/Debug.h" |
Andrew Trick | 49371f3 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 25 | |
| 26 | using namespace llvm; |
| 27 | |
| 28 | INITIALIZE_PASS_BEGIN(BranchProbabilityInfo, "branch-prob", |
| 29 | "Branch Probability Analysis", false, true) |
| 30 | INITIALIZE_PASS_DEPENDENCY(LoopInfo) |
| 31 | INITIALIZE_PASS_END(BranchProbabilityInfo, "branch-prob", |
| 32 | "Branch Probability Analysis", false, true) |
| 33 | |
| 34 | char BranchProbabilityInfo::ID = 0; |
| 35 | |
Chandler Carruth | 7a0094a | 2011-10-24 01:40:45 +0000 | [diff] [blame] | 36 | // Weights are for internal use only. They are used by heuristics to help to |
| 37 | // estimate edges' probability. Example: |
| 38 | // |
| 39 | // Using "Loop Branch Heuristics" we predict weights of edges for the |
| 40 | // block BB2. |
| 41 | // ... |
| 42 | // | |
| 43 | // V |
| 44 | // BB1<-+ |
| 45 | // | | |
| 46 | // | | (Weight = 124) |
| 47 | // V | |
| 48 | // BB2--+ |
| 49 | // | |
| 50 | // | (Weight = 4) |
| 51 | // V |
| 52 | // BB3 |
| 53 | // |
| 54 | // Probability of the edge BB2->BB1 = 124 / (124 + 4) = 0.96875 |
| 55 | // Probability of the edge BB2->BB3 = 4 / (124 + 4) = 0.03125 |
| 56 | static const uint32_t LBH_TAKEN_WEIGHT = 124; |
| 57 | static const uint32_t LBH_NONTAKEN_WEIGHT = 4; |
Andrew Trick | 49371f3 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 58 | |
Chandler Carruth | 7111f45 | 2011-10-24 12:01:08 +0000 | [diff] [blame] | 59 | /// \brief Unreachable-terminating branch taken weight. |
| 60 | /// |
| 61 | /// This is the weight for a branch being taken to a block that terminates |
| 62 | /// (eventually) in unreachable. These are predicted as unlikely as possible. |
| 63 | static const uint32_t UR_TAKEN_WEIGHT = 1; |
| 64 | |
| 65 | /// \brief Unreachable-terminating branch not-taken weight. |
| 66 | /// |
| 67 | /// This is the weight for a branch not being taken toward a block that |
| 68 | /// terminates (eventually) in unreachable. Such a branch is essentially never |
Chandler Carruth | b024aa0 | 2011-12-22 09:26:37 +0000 | [diff] [blame] | 69 | /// taken. Set the weight to an absurdly high value so that nested loops don't |
| 70 | /// easily subsume it. |
| 71 | static const uint32_t UR_NONTAKEN_WEIGHT = 1024*1024 - 1; |
Andrew Trick | 49371f3 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 72 | |
Diego Novillo | c639953 | 2013-05-24 12:26:52 +0000 | [diff] [blame] | 73 | /// \brief Weight for a branch taken going into a cold block. |
| 74 | /// |
| 75 | /// This is the weight for a branch taken toward a block marked |
| 76 | /// cold. A block is marked cold if it's postdominated by a |
| 77 | /// block containing a call to a cold function. Cold functions |
| 78 | /// are those marked with attribute 'cold'. |
| 79 | static const uint32_t CC_TAKEN_WEIGHT = 4; |
| 80 | |
| 81 | /// \brief Weight for a branch not-taken into a cold block. |
| 82 | /// |
| 83 | /// This is the weight for a branch not taken toward a block marked |
| 84 | /// cold. |
| 85 | static const uint32_t CC_NONTAKEN_WEIGHT = 64; |
| 86 | |
Chandler Carruth | 7a0094a | 2011-10-24 01:40:45 +0000 | [diff] [blame] | 87 | static const uint32_t PH_TAKEN_WEIGHT = 20; |
| 88 | static const uint32_t PH_NONTAKEN_WEIGHT = 12; |
Andrew Trick | 49371f3 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 89 | |
Chandler Carruth | 7a0094a | 2011-10-24 01:40:45 +0000 | [diff] [blame] | 90 | static const uint32_t ZH_TAKEN_WEIGHT = 20; |
| 91 | static const uint32_t ZH_NONTAKEN_WEIGHT = 12; |
Andrew Trick | 49371f3 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 92 | |
Chandler Carruth | 7a0094a | 2011-10-24 01:40:45 +0000 | [diff] [blame] | 93 | static const uint32_t FPH_TAKEN_WEIGHT = 20; |
| 94 | static const uint32_t FPH_NONTAKEN_WEIGHT = 12; |
Andrew Trick | 49371f3 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 95 | |
Bill Wendling | e1c5426 | 2012-08-15 12:22:35 +0000 | [diff] [blame] | 96 | /// \brief Invoke-terminating normal branch taken weight |
| 97 | /// |
| 98 | /// This is the weight for branching to the normal destination of an invoke |
| 99 | /// instruction. We expect this to happen most of the time. Set the weight to an |
| 100 | /// absurdly high value so that nested loops subsume it. |
| 101 | static const uint32_t IH_TAKEN_WEIGHT = 1024 * 1024 - 1; |
| 102 | |
| 103 | /// \brief Invoke-terminating normal branch not-taken weight. |
| 104 | /// |
| 105 | /// This is the weight for branching to the unwind destination of an invoke |
| 106 | /// instruction. This is essentially never taken. |
| 107 | static const uint32_t IH_NONTAKEN_WEIGHT = 1; |
| 108 | |
Chandler Carruth | 7a0094a | 2011-10-24 01:40:45 +0000 | [diff] [blame] | 109 | // Standard weight value. Used when none of the heuristics set weight for |
| 110 | // the edge. |
| 111 | static const uint32_t NORMAL_WEIGHT = 16; |
Andrew Trick | 49371f3 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 112 | |
Chandler Carruth | 7a0094a | 2011-10-24 01:40:45 +0000 | [diff] [blame] | 113 | // Minimum weight of an edge. Please note, that weight is NEVER 0. |
| 114 | static const uint32_t MIN_WEIGHT = 1; |
Andrew Trick | 49371f3 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 115 | |
Chandler Carruth | 7a0094a | 2011-10-24 01:40:45 +0000 | [diff] [blame] | 116 | static uint32_t getMaxWeightFor(BasicBlock *BB) { |
| 117 | return UINT32_MAX / BB->getTerminator()->getNumSuccessors(); |
| 118 | } |
Andrew Trick | 49371f3 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 119 | |
Andrew Trick | 49371f3 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 120 | |
Chandler Carruth | 7111f45 | 2011-10-24 12:01:08 +0000 | [diff] [blame] | 121 | /// \brief Calculate edge weights for successors lead to unreachable. |
| 122 | /// |
| 123 | /// Predict that a successor which leads necessarily to an |
| 124 | /// unreachable-terminated block as extremely unlikely. |
| 125 | bool BranchProbabilityInfo::calcUnreachableHeuristics(BasicBlock *BB) { |
| 126 | TerminatorInst *TI = BB->getTerminator(); |
| 127 | if (TI->getNumSuccessors() == 0) { |
| 128 | if (isa<UnreachableInst>(TI)) |
| 129 | PostDominatedByUnreachable.insert(BB); |
| 130 | return false; |
| 131 | } |
| 132 | |
Manman Ren | cf10446 | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 133 | SmallVector<unsigned, 4> UnreachableEdges; |
| 134 | SmallVector<unsigned, 4> ReachableEdges; |
Chandler Carruth | 7111f45 | 2011-10-24 12:01:08 +0000 | [diff] [blame] | 135 | |
| 136 | for (succ_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I) { |
| 137 | if (PostDominatedByUnreachable.count(*I)) |
Manman Ren | cf10446 | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 138 | UnreachableEdges.push_back(I.getSuccessorIndex()); |
Chandler Carruth | 7111f45 | 2011-10-24 12:01:08 +0000 | [diff] [blame] | 139 | else |
Manman Ren | cf10446 | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 140 | ReachableEdges.push_back(I.getSuccessorIndex()); |
Chandler Carruth | 7111f45 | 2011-10-24 12:01:08 +0000 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | // If all successors are in the set of blocks post-dominated by unreachable, |
| 144 | // this block is too. |
| 145 | if (UnreachableEdges.size() == TI->getNumSuccessors()) |
| 146 | PostDominatedByUnreachable.insert(BB); |
| 147 | |
| 148 | // Skip probabilities if this block has a single successor or if all were |
| 149 | // reachable. |
| 150 | if (TI->getNumSuccessors() == 1 || UnreachableEdges.empty()) |
| 151 | return false; |
| 152 | |
| 153 | uint32_t UnreachableWeight = |
Manman Ren | cf10446 | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 154 | std::max(UR_TAKEN_WEIGHT / (unsigned)UnreachableEdges.size(), MIN_WEIGHT); |
Craig Topper | af0dea1 | 2013-07-04 01:31:24 +0000 | [diff] [blame] | 155 | for (SmallVectorImpl<unsigned>::iterator I = UnreachableEdges.begin(), |
| 156 | E = UnreachableEdges.end(); |
Chandler Carruth | 7111f45 | 2011-10-24 12:01:08 +0000 | [diff] [blame] | 157 | I != E; ++I) |
| 158 | setEdgeWeight(BB, *I, UnreachableWeight); |
| 159 | |
| 160 | if (ReachableEdges.empty()) |
| 161 | return true; |
| 162 | uint32_t ReachableWeight = |
Manman Ren | cf10446 | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 163 | std::max(UR_NONTAKEN_WEIGHT / (unsigned)ReachableEdges.size(), |
| 164 | NORMAL_WEIGHT); |
Craig Topper | af0dea1 | 2013-07-04 01:31:24 +0000 | [diff] [blame] | 165 | for (SmallVectorImpl<unsigned>::iterator I = ReachableEdges.begin(), |
| 166 | E = ReachableEdges.end(); |
Chandler Carruth | 7111f45 | 2011-10-24 12:01:08 +0000 | [diff] [blame] | 167 | I != E; ++I) |
| 168 | setEdgeWeight(BB, *I, ReachableWeight); |
| 169 | |
| 170 | return true; |
| 171 | } |
| 172 | |
Chandler Carruth | d27a7a9 | 2011-10-19 10:30:30 +0000 | [diff] [blame] | 173 | // Propagate existing explicit probabilities from either profile data or |
| 174 | // 'expect' intrinsic processing. |
Chandler Carruth | 7a0094a | 2011-10-24 01:40:45 +0000 | [diff] [blame] | 175 | bool BranchProbabilityInfo::calcMetadataWeights(BasicBlock *BB) { |
Chandler Carruth | deac50c | 2011-10-19 10:32:19 +0000 | [diff] [blame] | 176 | TerminatorInst *TI = BB->getTerminator(); |
| 177 | if (TI->getNumSuccessors() == 1) |
| 178 | return false; |
| 179 | if (!isa<BranchInst>(TI) && !isa<SwitchInst>(TI)) |
Chandler Carruth | d27a7a9 | 2011-10-19 10:30:30 +0000 | [diff] [blame] | 180 | return false; |
| 181 | |
Chandler Carruth | deac50c | 2011-10-19 10:32:19 +0000 | [diff] [blame] | 182 | MDNode *WeightsNode = TI->getMetadata(LLVMContext::MD_prof); |
| 183 | if (!WeightsNode) |
Chandler Carruth | d27a7a9 | 2011-10-19 10:30:30 +0000 | [diff] [blame] | 184 | return false; |
| 185 | |
Chandler Carruth | deac50c | 2011-10-19 10:32:19 +0000 | [diff] [blame] | 186 | // Ensure there are weights for all of the successors. Note that the first |
| 187 | // operand to the metadata node is a name, not a weight. |
| 188 | if (WeightsNode->getNumOperands() != TI->getNumSuccessors() + 1) |
Chandler Carruth | d27a7a9 | 2011-10-19 10:30:30 +0000 | [diff] [blame] | 189 | return false; |
| 190 | |
Chandler Carruth | deac50c | 2011-10-19 10:32:19 +0000 | [diff] [blame] | 191 | // Build up the final weights that will be used in a temporary buffer, but |
| 192 | // don't add them until all weihts are present. Each weight value is clamped |
| 193 | // to [1, getMaxWeightFor(BB)]. |
Chandler Carruth | d27a7a9 | 2011-10-19 10:30:30 +0000 | [diff] [blame] | 194 | uint32_t WeightLimit = getMaxWeightFor(BB); |
Chandler Carruth | deac50c | 2011-10-19 10:32:19 +0000 | [diff] [blame] | 195 | SmallVector<uint32_t, 2> Weights; |
| 196 | Weights.reserve(TI->getNumSuccessors()); |
| 197 | for (unsigned i = 1, e = WeightsNode->getNumOperands(); i != e; ++i) { |
| 198 | ConstantInt *Weight = dyn_cast<ConstantInt>(WeightsNode->getOperand(i)); |
| 199 | if (!Weight) |
| 200 | return false; |
| 201 | Weights.push_back( |
| 202 | std::max<uint32_t>(1, Weight->getLimitedValue(WeightLimit))); |
| 203 | } |
| 204 | assert(Weights.size() == TI->getNumSuccessors() && "Checked above"); |
| 205 | for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i) |
Manman Ren | cf10446 | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 206 | setEdgeWeight(BB, i, Weights[i]); |
Chandler Carruth | d27a7a9 | 2011-10-19 10:30:30 +0000 | [diff] [blame] | 207 | |
| 208 | return true; |
| 209 | } |
| 210 | |
Diego Novillo | c639953 | 2013-05-24 12:26:52 +0000 | [diff] [blame] | 211 | /// \brief Calculate edge weights for edges leading to cold blocks. |
| 212 | /// |
| 213 | /// A cold block is one post-dominated by a block with a call to a |
| 214 | /// cold function. Those edges are unlikely to be taken, so we give |
| 215 | /// them relatively low weight. |
| 216 | /// |
| 217 | /// Return true if we could compute the weights for cold edges. |
| 218 | /// Return false, otherwise. |
| 219 | bool BranchProbabilityInfo::calcColdCallHeuristics(BasicBlock *BB) { |
| 220 | TerminatorInst *TI = BB->getTerminator(); |
| 221 | if (TI->getNumSuccessors() == 0) |
| 222 | return false; |
| 223 | |
| 224 | // Determine which successors are post-dominated by a cold block. |
| 225 | SmallVector<unsigned, 4> ColdEdges; |
Diego Novillo | c639953 | 2013-05-24 12:26:52 +0000 | [diff] [blame] | 226 | SmallVector<unsigned, 4> NormalEdges; |
Diego Novillo | c639953 | 2013-05-24 12:26:52 +0000 | [diff] [blame] | 227 | for (succ_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I) |
| 228 | if (PostDominatedByColdCall.count(*I)) |
| 229 | ColdEdges.push_back(I.getSuccessorIndex()); |
| 230 | else |
| 231 | NormalEdges.push_back(I.getSuccessorIndex()); |
| 232 | |
| 233 | // If all successors are in the set of blocks post-dominated by cold calls, |
| 234 | // this block is in the set post-dominated by cold calls. |
| 235 | if (ColdEdges.size() == TI->getNumSuccessors()) |
| 236 | PostDominatedByColdCall.insert(BB); |
| 237 | else { |
| 238 | // Otherwise, if the block itself contains a cold function, add it to the |
| 239 | // set of blocks postdominated by a cold call. |
| 240 | assert(!PostDominatedByColdCall.count(BB)); |
| 241 | for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) |
| 242 | if (CallInst *CI = dyn_cast<CallInst>(I)) |
| 243 | if (CI->hasFnAttr(Attribute::Cold)) { |
| 244 | PostDominatedByColdCall.insert(BB); |
| 245 | break; |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | // Skip probabilities if this block has a single successor. |
| 250 | if (TI->getNumSuccessors() == 1 || ColdEdges.empty()) |
| 251 | return false; |
| 252 | |
| 253 | uint32_t ColdWeight = |
| 254 | std::max(CC_TAKEN_WEIGHT / (unsigned) ColdEdges.size(), MIN_WEIGHT); |
Craig Topper | af0dea1 | 2013-07-04 01:31:24 +0000 | [diff] [blame] | 255 | for (SmallVectorImpl<unsigned>::iterator I = ColdEdges.begin(), |
| 256 | E = ColdEdges.end(); |
Diego Novillo | c639953 | 2013-05-24 12:26:52 +0000 | [diff] [blame] | 257 | I != E; ++I) |
| 258 | setEdgeWeight(BB, *I, ColdWeight); |
| 259 | |
| 260 | if (NormalEdges.empty()) |
| 261 | return true; |
| 262 | uint32_t NormalWeight = std::max( |
| 263 | CC_NONTAKEN_WEIGHT / (unsigned) NormalEdges.size(), NORMAL_WEIGHT); |
Craig Topper | af0dea1 | 2013-07-04 01:31:24 +0000 | [diff] [blame] | 264 | for (SmallVectorImpl<unsigned>::iterator I = NormalEdges.begin(), |
| 265 | E = NormalEdges.end(); |
Diego Novillo | c639953 | 2013-05-24 12:26:52 +0000 | [diff] [blame] | 266 | I != E; ++I) |
| 267 | setEdgeWeight(BB, *I, NormalWeight); |
| 268 | |
| 269 | return true; |
| 270 | } |
| 271 | |
Andrew Trick | 49371f3 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 272 | // Calculate Edge Weights using "Pointer Heuristics". Predict a comparsion |
| 273 | // between two pointer or pointer and NULL will fail. |
Chandler Carruth | 7a0094a | 2011-10-24 01:40:45 +0000 | [diff] [blame] | 274 | bool BranchProbabilityInfo::calcPointerHeuristics(BasicBlock *BB) { |
Andrew Trick | 49371f3 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 275 | BranchInst * BI = dyn_cast<BranchInst>(BB->getTerminator()); |
| 276 | if (!BI || !BI->isConditional()) |
Jakub Staszak | d07b2e1 | 2011-07-28 21:45:07 +0000 | [diff] [blame] | 277 | return false; |
Andrew Trick | 49371f3 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 278 | |
| 279 | Value *Cond = BI->getCondition(); |
| 280 | ICmpInst *CI = dyn_cast<ICmpInst>(Cond); |
Jakub Staszak | abb236f | 2011-07-15 20:51:06 +0000 | [diff] [blame] | 281 | if (!CI || !CI->isEquality()) |
Jakub Staszak | d07b2e1 | 2011-07-28 21:45:07 +0000 | [diff] [blame] | 282 | return false; |
Andrew Trick | 49371f3 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 283 | |
| 284 | Value *LHS = CI->getOperand(0); |
Andrew Trick | 49371f3 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 285 | |
| 286 | if (!LHS->getType()->isPointerTy()) |
Jakub Staszak | d07b2e1 | 2011-07-28 21:45:07 +0000 | [diff] [blame] | 287 | return false; |
Andrew Trick | 49371f3 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 288 | |
Nick Lewycky | 75b2053 | 2011-06-04 02:07:10 +0000 | [diff] [blame] | 289 | assert(CI->getOperand(1)->getType()->isPointerTy()); |
Andrew Trick | 49371f3 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 290 | |
Andrew Trick | 49371f3 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 291 | // p != 0 -> isProb = true |
| 292 | // p == 0 -> isProb = false |
| 293 | // p != q -> isProb = true |
| 294 | // p == q -> isProb = false; |
Manman Ren | cf10446 | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 295 | unsigned TakenIdx = 0, NonTakenIdx = 1; |
Jakub Staszak | abb236f | 2011-07-15 20:51:06 +0000 | [diff] [blame] | 296 | bool isProb = CI->getPredicate() == ICmpInst::ICMP_NE; |
Andrew Trick | 49371f3 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 297 | if (!isProb) |
Manman Ren | cf10446 | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 298 | std::swap(TakenIdx, NonTakenIdx); |
Andrew Trick | 49371f3 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 299 | |
Manman Ren | cf10446 | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 300 | setEdgeWeight(BB, TakenIdx, PH_TAKEN_WEIGHT); |
| 301 | setEdgeWeight(BB, NonTakenIdx, PH_NONTAKEN_WEIGHT); |
Jakub Staszak | d07b2e1 | 2011-07-28 21:45:07 +0000 | [diff] [blame] | 302 | return true; |
Andrew Trick | 49371f3 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | // Calculate Edge Weights using "Loop Branch Heuristics". Predict backedges |
| 306 | // as taken, exiting edges as not-taken. |
Chandler Carruth | 7a0094a | 2011-10-24 01:40:45 +0000 | [diff] [blame] | 307 | bool BranchProbabilityInfo::calcLoopBranchHeuristics(BasicBlock *BB) { |
Andrew Trick | 49371f3 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 308 | Loop *L = LI->getLoopFor(BB); |
| 309 | if (!L) |
Jakub Staszak | d07b2e1 | 2011-07-28 21:45:07 +0000 | [diff] [blame] | 310 | return false; |
Andrew Trick | 49371f3 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 311 | |
Manman Ren | cf10446 | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 312 | SmallVector<unsigned, 8> BackEdges; |
| 313 | SmallVector<unsigned, 8> ExitingEdges; |
| 314 | SmallVector<unsigned, 8> InEdges; // Edges from header to the loop. |
Jakub Staszak | bcb3c65 | 2011-07-28 21:33:46 +0000 | [diff] [blame] | 315 | |
Andrew Trick | 49371f3 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 316 | for (succ_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I) { |
Chandler Carruth | 32f46e7 | 2011-10-25 09:47:41 +0000 | [diff] [blame] | 317 | if (!L->contains(*I)) |
Manman Ren | cf10446 | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 318 | ExitingEdges.push_back(I.getSuccessorIndex()); |
Chandler Carruth | 32f46e7 | 2011-10-25 09:47:41 +0000 | [diff] [blame] | 319 | else if (L->getHeader() == *I) |
Manman Ren | cf10446 | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 320 | BackEdges.push_back(I.getSuccessorIndex()); |
Chandler Carruth | 32f46e7 | 2011-10-25 09:47:41 +0000 | [diff] [blame] | 321 | else |
Manman Ren | cf10446 | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 322 | InEdges.push_back(I.getSuccessorIndex()); |
Andrew Trick | 49371f3 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 323 | } |
| 324 | |
Andrew Trick | 3d4e64b | 2011-06-11 01:05:22 +0000 | [diff] [blame] | 325 | if (uint32_t numBackEdges = BackEdges.size()) { |
| 326 | uint32_t backWeight = LBH_TAKEN_WEIGHT / numBackEdges; |
Andrew Trick | 49371f3 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 327 | if (backWeight < NORMAL_WEIGHT) |
| 328 | backWeight = NORMAL_WEIGHT; |
| 329 | |
Craig Topper | af0dea1 | 2013-07-04 01:31:24 +0000 | [diff] [blame] | 330 | for (SmallVectorImpl<unsigned>::iterator EI = BackEdges.begin(), |
Andrew Trick | 49371f3 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 331 | EE = BackEdges.end(); EI != EE; ++EI) { |
Manman Ren | cf10446 | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 332 | setEdgeWeight(BB, *EI, backWeight); |
Andrew Trick | 49371f3 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 333 | } |
| 334 | } |
| 335 | |
Jakub Staszak | bcb3c65 | 2011-07-28 21:33:46 +0000 | [diff] [blame] | 336 | if (uint32_t numInEdges = InEdges.size()) { |
| 337 | uint32_t inWeight = LBH_TAKEN_WEIGHT / numInEdges; |
| 338 | if (inWeight < NORMAL_WEIGHT) |
| 339 | inWeight = NORMAL_WEIGHT; |
| 340 | |
Craig Topper | af0dea1 | 2013-07-04 01:31:24 +0000 | [diff] [blame] | 341 | for (SmallVectorImpl<unsigned>::iterator EI = InEdges.begin(), |
Jakub Staszak | bcb3c65 | 2011-07-28 21:33:46 +0000 | [diff] [blame] | 342 | EE = InEdges.end(); EI != EE; ++EI) { |
Manman Ren | cf10446 | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 343 | setEdgeWeight(BB, *EI, inWeight); |
Jakub Staszak | bcb3c65 | 2011-07-28 21:33:46 +0000 | [diff] [blame] | 344 | } |
| 345 | } |
| 346 | |
Chandler Carruth | 32f46e7 | 2011-10-25 09:47:41 +0000 | [diff] [blame] | 347 | if (uint32_t numExitingEdges = ExitingEdges.size()) { |
| 348 | uint32_t exitWeight = LBH_NONTAKEN_WEIGHT / numExitingEdges; |
Andrew Trick | 49371f3 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 349 | if (exitWeight < MIN_WEIGHT) |
| 350 | exitWeight = MIN_WEIGHT; |
| 351 | |
Craig Topper | af0dea1 | 2013-07-04 01:31:24 +0000 | [diff] [blame] | 352 | for (SmallVectorImpl<unsigned>::iterator EI = ExitingEdges.begin(), |
Andrew Trick | 49371f3 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 353 | EE = ExitingEdges.end(); EI != EE; ++EI) { |
Manman Ren | cf10446 | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 354 | setEdgeWeight(BB, *EI, exitWeight); |
Andrew Trick | 49371f3 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 355 | } |
| 356 | } |
Jakub Staszak | d07b2e1 | 2011-07-28 21:45:07 +0000 | [diff] [blame] | 357 | |
| 358 | return true; |
Andrew Trick | 49371f3 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 359 | } |
| 360 | |
Chandler Carruth | 7a0094a | 2011-10-24 01:40:45 +0000 | [diff] [blame] | 361 | bool BranchProbabilityInfo::calcZeroHeuristics(BasicBlock *BB) { |
Jakub Staszak | 17af66a | 2011-07-31 03:27:24 +0000 | [diff] [blame] | 362 | BranchInst * BI = dyn_cast<BranchInst>(BB->getTerminator()); |
| 363 | if (!BI || !BI->isConditional()) |
| 364 | return false; |
| 365 | |
| 366 | Value *Cond = BI->getCondition(); |
| 367 | ICmpInst *CI = dyn_cast<ICmpInst>(Cond); |
| 368 | if (!CI) |
| 369 | return false; |
| 370 | |
Jakub Staszak | 17af66a | 2011-07-31 03:27:24 +0000 | [diff] [blame] | 371 | Value *RHS = CI->getOperand(1); |
Jakub Staszak | bfb1ae2 | 2011-07-31 04:47:20 +0000 | [diff] [blame] | 372 | ConstantInt *CV = dyn_cast<ConstantInt>(RHS); |
Benjamin Kramer | 0ca1ad0 | 2011-09-04 23:53:04 +0000 | [diff] [blame] | 373 | if (!CV) |
Jakub Staszak | 17af66a | 2011-07-31 03:27:24 +0000 | [diff] [blame] | 374 | return false; |
| 375 | |
| 376 | bool isProb; |
Benjamin Kramer | 0ca1ad0 | 2011-09-04 23:53:04 +0000 | [diff] [blame] | 377 | if (CV->isZero()) { |
| 378 | switch (CI->getPredicate()) { |
| 379 | case CmpInst::ICMP_EQ: |
| 380 | // X == 0 -> Unlikely |
| 381 | isProb = false; |
| 382 | break; |
| 383 | case CmpInst::ICMP_NE: |
| 384 | // X != 0 -> Likely |
| 385 | isProb = true; |
| 386 | break; |
| 387 | case CmpInst::ICMP_SLT: |
| 388 | // X < 0 -> Unlikely |
| 389 | isProb = false; |
| 390 | break; |
| 391 | case CmpInst::ICMP_SGT: |
| 392 | // X > 0 -> Likely |
| 393 | isProb = true; |
| 394 | break; |
| 395 | default: |
| 396 | return false; |
| 397 | } |
| 398 | } else if (CV->isOne() && CI->getPredicate() == CmpInst::ICMP_SLT) { |
| 399 | // InstCombine canonicalizes X <= 0 into X < 1. |
| 400 | // X <= 0 -> Unlikely |
Jakub Staszak | 17af66a | 2011-07-31 03:27:24 +0000 | [diff] [blame] | 401 | isProb = false; |
Hal Finkel | 4d94930 | 2013-11-01 10:58:22 +0000 | [diff] [blame] | 402 | } else if (CV->isAllOnesValue()) { |
| 403 | switch (CI->getPredicate()) { |
| 404 | case CmpInst::ICMP_EQ: |
| 405 | // X == -1 -> Unlikely |
| 406 | isProb = false; |
| 407 | break; |
| 408 | case CmpInst::ICMP_NE: |
| 409 | // X != -1 -> Likely |
| 410 | isProb = true; |
| 411 | break; |
| 412 | case CmpInst::ICMP_SGT: |
| 413 | // InstCombine canonicalizes X >= 0 into X > -1. |
| 414 | // X >= 0 -> Likely |
| 415 | isProb = true; |
| 416 | break; |
| 417 | default: |
| 418 | return false; |
| 419 | } |
Benjamin Kramer | 0ca1ad0 | 2011-09-04 23:53:04 +0000 | [diff] [blame] | 420 | } else { |
Jakub Staszak | 17af66a | 2011-07-31 03:27:24 +0000 | [diff] [blame] | 421 | return false; |
Benjamin Kramer | 0ca1ad0 | 2011-09-04 23:53:04 +0000 | [diff] [blame] | 422 | } |
Jakub Staszak | 17af66a | 2011-07-31 03:27:24 +0000 | [diff] [blame] | 423 | |
Manman Ren | cf10446 | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 424 | unsigned TakenIdx = 0, NonTakenIdx = 1; |
Jakub Staszak | 17af66a | 2011-07-31 03:27:24 +0000 | [diff] [blame] | 425 | |
| 426 | if (!isProb) |
Manman Ren | cf10446 | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 427 | std::swap(TakenIdx, NonTakenIdx); |
Jakub Staszak | 17af66a | 2011-07-31 03:27:24 +0000 | [diff] [blame] | 428 | |
Manman Ren | cf10446 | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 429 | setEdgeWeight(BB, TakenIdx, ZH_TAKEN_WEIGHT); |
| 430 | setEdgeWeight(BB, NonTakenIdx, ZH_NONTAKEN_WEIGHT); |
Jakub Staszak | 17af66a | 2011-07-31 03:27:24 +0000 | [diff] [blame] | 431 | |
| 432 | return true; |
| 433 | } |
| 434 | |
Chandler Carruth | 7a0094a | 2011-10-24 01:40:45 +0000 | [diff] [blame] | 435 | bool BranchProbabilityInfo::calcFloatingPointHeuristics(BasicBlock *BB) { |
Benjamin Kramer | 1e731a1 | 2011-10-21 20:12:47 +0000 | [diff] [blame] | 436 | BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator()); |
| 437 | if (!BI || !BI->isConditional()) |
| 438 | return false; |
| 439 | |
| 440 | Value *Cond = BI->getCondition(); |
| 441 | FCmpInst *FCmp = dyn_cast<FCmpInst>(Cond); |
Benjamin Kramer | 606a50a | 2011-10-21 21:13:47 +0000 | [diff] [blame] | 442 | if (!FCmp) |
Benjamin Kramer | 1e731a1 | 2011-10-21 20:12:47 +0000 | [diff] [blame] | 443 | return false; |
| 444 | |
Benjamin Kramer | 606a50a | 2011-10-21 21:13:47 +0000 | [diff] [blame] | 445 | bool isProb; |
| 446 | if (FCmp->isEquality()) { |
| 447 | // f1 == f2 -> Unlikely |
| 448 | // f1 != f2 -> Likely |
| 449 | isProb = !FCmp->isTrueWhenEqual(); |
| 450 | } else if (FCmp->getPredicate() == FCmpInst::FCMP_ORD) { |
| 451 | // !isnan -> Likely |
| 452 | isProb = true; |
| 453 | } else if (FCmp->getPredicate() == FCmpInst::FCMP_UNO) { |
| 454 | // isnan -> Unlikely |
| 455 | isProb = false; |
| 456 | } else { |
| 457 | return false; |
| 458 | } |
| 459 | |
Manman Ren | cf10446 | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 460 | unsigned TakenIdx = 0, NonTakenIdx = 1; |
Benjamin Kramer | 1e731a1 | 2011-10-21 20:12:47 +0000 | [diff] [blame] | 461 | |
Benjamin Kramer | 606a50a | 2011-10-21 21:13:47 +0000 | [diff] [blame] | 462 | if (!isProb) |
Manman Ren | cf10446 | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 463 | std::swap(TakenIdx, NonTakenIdx); |
Benjamin Kramer | 1e731a1 | 2011-10-21 20:12:47 +0000 | [diff] [blame] | 464 | |
Manman Ren | cf10446 | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 465 | setEdgeWeight(BB, TakenIdx, FPH_TAKEN_WEIGHT); |
| 466 | setEdgeWeight(BB, NonTakenIdx, FPH_NONTAKEN_WEIGHT); |
Benjamin Kramer | 1e731a1 | 2011-10-21 20:12:47 +0000 | [diff] [blame] | 467 | |
| 468 | return true; |
| 469 | } |
Jakub Staszak | 17af66a | 2011-07-31 03:27:24 +0000 | [diff] [blame] | 470 | |
Bill Wendling | e1c5426 | 2012-08-15 12:22:35 +0000 | [diff] [blame] | 471 | bool BranchProbabilityInfo::calcInvokeHeuristics(BasicBlock *BB) { |
| 472 | InvokeInst *II = dyn_cast<InvokeInst>(BB->getTerminator()); |
| 473 | if (!II) |
| 474 | return false; |
| 475 | |
Manman Ren | cf10446 | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 476 | setEdgeWeight(BB, 0/*Index for Normal*/, IH_TAKEN_WEIGHT); |
| 477 | setEdgeWeight(BB, 1/*Index for Unwind*/, IH_NONTAKEN_WEIGHT); |
Bill Wendling | e1c5426 | 2012-08-15 12:22:35 +0000 | [diff] [blame] | 478 | return true; |
| 479 | } |
| 480 | |
Chandler Carruth | 7a0094a | 2011-10-24 01:40:45 +0000 | [diff] [blame] | 481 | void BranchProbabilityInfo::getAnalysisUsage(AnalysisUsage &AU) const { |
| 482 | AU.addRequired<LoopInfo>(); |
| 483 | AU.setPreservesAll(); |
| 484 | } |
| 485 | |
| 486 | bool BranchProbabilityInfo::runOnFunction(Function &F) { |
Michael Gottesman | fb9164f | 2013-12-14 02:24:25 +0000 | [diff] [blame] | 487 | DEBUG(dbgs() << "---- Branch Probability Info : " << F.getName() |
| 488 | << " ----\n\n"); |
Chandler Carruth | 7a0094a | 2011-10-24 01:40:45 +0000 | [diff] [blame] | 489 | LastF = &F; // Store the last function we ran on for printing. |
| 490 | LI = &getAnalysis<LoopInfo>(); |
Chandler Carruth | 7111f45 | 2011-10-24 12:01:08 +0000 | [diff] [blame] | 491 | assert(PostDominatedByUnreachable.empty()); |
Diego Novillo | c639953 | 2013-05-24 12:26:52 +0000 | [diff] [blame] | 492 | assert(PostDominatedByColdCall.empty()); |
Chandler Carruth | 7a0094a | 2011-10-24 01:40:45 +0000 | [diff] [blame] | 493 | |
Chandler Carruth | 7111f45 | 2011-10-24 12:01:08 +0000 | [diff] [blame] | 494 | // Walk the basic blocks in post-order so that we can build up state about |
| 495 | // the successors of a block iteratively. |
| 496 | for (po_iterator<BasicBlock *> I = po_begin(&F.getEntryBlock()), |
| 497 | E = po_end(&F.getEntryBlock()); |
| 498 | I != E; ++I) { |
| 499 | DEBUG(dbgs() << "Computing probabilities for " << I->getName() << "\n"); |
| 500 | if (calcUnreachableHeuristics(*I)) |
Chandler Carruth | d27a7a9 | 2011-10-19 10:30:30 +0000 | [diff] [blame] | 501 | continue; |
Chandler Carruth | 7111f45 | 2011-10-24 12:01:08 +0000 | [diff] [blame] | 502 | if (calcMetadataWeights(*I)) |
Jakub Staszak | d07b2e1 | 2011-07-28 21:45:07 +0000 | [diff] [blame] | 503 | continue; |
Diego Novillo | c639953 | 2013-05-24 12:26:52 +0000 | [diff] [blame] | 504 | if (calcColdCallHeuristics(*I)) |
| 505 | continue; |
Chandler Carruth | 7111f45 | 2011-10-24 12:01:08 +0000 | [diff] [blame] | 506 | if (calcLoopBranchHeuristics(*I)) |
Jakub Staszak | d07b2e1 | 2011-07-28 21:45:07 +0000 | [diff] [blame] | 507 | continue; |
Chandler Carruth | 7111f45 | 2011-10-24 12:01:08 +0000 | [diff] [blame] | 508 | if (calcPointerHeuristics(*I)) |
Jakub Staszak | 17af66a | 2011-07-31 03:27:24 +0000 | [diff] [blame] | 509 | continue; |
Chandler Carruth | 7111f45 | 2011-10-24 12:01:08 +0000 | [diff] [blame] | 510 | if (calcZeroHeuristics(*I)) |
Benjamin Kramer | 1e731a1 | 2011-10-21 20:12:47 +0000 | [diff] [blame] | 511 | continue; |
Bill Wendling | e1c5426 | 2012-08-15 12:22:35 +0000 | [diff] [blame] | 512 | if (calcFloatingPointHeuristics(*I)) |
| 513 | continue; |
| 514 | calcInvokeHeuristics(*I); |
Andrew Trick | 49371f3 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 515 | } |
Chandler Carruth | 7111f45 | 2011-10-24 12:01:08 +0000 | [diff] [blame] | 516 | |
| 517 | PostDominatedByUnreachable.clear(); |
Diego Novillo | c639953 | 2013-05-24 12:26:52 +0000 | [diff] [blame] | 518 | PostDominatedByColdCall.clear(); |
Andrew Trick | 49371f3 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 519 | return false; |
| 520 | } |
| 521 | |
Chandler Carruth | 1c8ace0 | 2011-10-23 21:21:50 +0000 | [diff] [blame] | 522 | void BranchProbabilityInfo::print(raw_ostream &OS, const Module *) const { |
| 523 | OS << "---- Branch Probabilities ----\n"; |
| 524 | // We print the probabilities from the last function the analysis ran over, |
| 525 | // or the function it is currently running over. |
| 526 | assert(LastF && "Cannot print prior to running over a function"); |
| 527 | for (Function::const_iterator BI = LastF->begin(), BE = LastF->end(); |
| 528 | BI != BE; ++BI) { |
| 529 | for (succ_const_iterator SI = succ_begin(BI), SE = succ_end(BI); |
| 530 | SI != SE; ++SI) { |
| 531 | printEdgeProbability(OS << " ", BI, *SI); |
| 532 | } |
| 533 | } |
| 534 | } |
| 535 | |
Jakub Staszak | efd94c8 | 2011-07-29 19:30:00 +0000 | [diff] [blame] | 536 | uint32_t BranchProbabilityInfo::getSumForBlock(const BasicBlock *BB) const { |
Andrew Trick | 3d4e64b | 2011-06-11 01:05:22 +0000 | [diff] [blame] | 537 | uint32_t Sum = 0; |
| 538 | |
Jakub Staszak | efd94c8 | 2011-07-29 19:30:00 +0000 | [diff] [blame] | 539 | for (succ_const_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I) { |
Manman Ren | cf10446 | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 540 | uint32_t Weight = getEdgeWeight(BB, I.getSuccessorIndex()); |
Andrew Trick | 3d4e64b | 2011-06-11 01:05:22 +0000 | [diff] [blame] | 541 | uint32_t PrevSum = Sum; |
Andrew Trick | 49371f3 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 542 | |
| 543 | Sum += Weight; |
| 544 | assert(Sum > PrevSum); (void) PrevSum; |
| 545 | } |
| 546 | |
Andrew Trick | 3d4e64b | 2011-06-11 01:05:22 +0000 | [diff] [blame] | 547 | return Sum; |
| 548 | } |
| 549 | |
Jakub Staszak | efd94c8 | 2011-07-29 19:30:00 +0000 | [diff] [blame] | 550 | bool BranchProbabilityInfo:: |
| 551 | isEdgeHot(const BasicBlock *Src, const BasicBlock *Dst) const { |
Andrew Trick | 3d4e64b | 2011-06-11 01:05:22 +0000 | [diff] [blame] | 552 | // Hot probability is at least 4/5 = 80% |
Benjamin Kramer | 929f53f | 2011-10-23 11:19:14 +0000 | [diff] [blame] | 553 | // FIXME: Compare against a static "hot" BranchProbability. |
| 554 | return getEdgeProbability(Src, Dst) > BranchProbability(4, 5); |
Andrew Trick | 49371f3 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 555 | } |
| 556 | |
| 557 | BasicBlock *BranchProbabilityInfo::getHotSucc(BasicBlock *BB) const { |
Andrew Trick | 3d4e64b | 2011-06-11 01:05:22 +0000 | [diff] [blame] | 558 | uint32_t Sum = 0; |
| 559 | uint32_t MaxWeight = 0; |
Andrew Trick | 49371f3 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 560 | BasicBlock *MaxSucc = 0; |
| 561 | |
| 562 | for (succ_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I) { |
| 563 | BasicBlock *Succ = *I; |
Andrew Trick | 3d4e64b | 2011-06-11 01:05:22 +0000 | [diff] [blame] | 564 | uint32_t Weight = getEdgeWeight(BB, Succ); |
| 565 | uint32_t PrevSum = Sum; |
Andrew Trick | 49371f3 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 566 | |
| 567 | Sum += Weight; |
| 568 | assert(Sum > PrevSum); (void) PrevSum; |
| 569 | |
| 570 | if (Weight > MaxWeight) { |
| 571 | MaxWeight = Weight; |
| 572 | MaxSucc = Succ; |
| 573 | } |
| 574 | } |
| 575 | |
Benjamin Kramer | 929f53f | 2011-10-23 11:19:14 +0000 | [diff] [blame] | 576 | // Hot probability is at least 4/5 = 80% |
| 577 | if (BranchProbability(MaxWeight, Sum) > BranchProbability(4, 5)) |
Andrew Trick | 49371f3 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 578 | return MaxSucc; |
| 579 | |
| 580 | return 0; |
| 581 | } |
| 582 | |
Manman Ren | cf10446 | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 583 | /// Get the raw edge weight for the edge. If can't find it, return |
| 584 | /// DEFAULT_WEIGHT value. Here an edge is specified using PredBlock and an index |
| 585 | /// to the successors. |
Jakub Staszak | efd94c8 | 2011-07-29 19:30:00 +0000 | [diff] [blame] | 586 | uint32_t BranchProbabilityInfo:: |
Manman Ren | cf10446 | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 587 | getEdgeWeight(const BasicBlock *Src, unsigned IndexInSuccessors) const { |
| 588 | DenseMap<Edge, uint32_t>::const_iterator I = |
| 589 | Weights.find(std::make_pair(Src, IndexInSuccessors)); |
Andrew Trick | 49371f3 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 590 | |
| 591 | if (I != Weights.end()) |
| 592 | return I->second; |
| 593 | |
| 594 | return DEFAULT_WEIGHT; |
| 595 | } |
| 596 | |
Duncan P. N. Exon Smith | 37bd529 | 2014-04-11 23:20:52 +0000 | [diff] [blame^] | 597 | uint32_t BranchProbabilityInfo::getEdgeWeight(const BasicBlock *Src, |
| 598 | succ_const_iterator Dst) const { |
| 599 | return getEdgeWeight(Src, Dst.getSuccessorIndex()); |
Michael Gottesman | fb9164f | 2013-12-14 02:24:25 +0000 | [diff] [blame] | 600 | } |
| 601 | |
Manman Ren | cf10446 | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 602 | /// Get the raw edge weight calculated for the block pair. This returns the sum |
| 603 | /// of all raw edge weights from Src to Dst. |
| 604 | uint32_t BranchProbabilityInfo:: |
| 605 | getEdgeWeight(const BasicBlock *Src, const BasicBlock *Dst) const { |
| 606 | uint32_t Weight = 0; |
| 607 | DenseMap<Edge, uint32_t>::const_iterator MapI; |
| 608 | for (succ_const_iterator I = succ_begin(Src), E = succ_end(Src); I != E; ++I) |
| 609 | if (*I == Dst) { |
| 610 | MapI = Weights.find(std::make_pair(Src, I.getSuccessorIndex())); |
| 611 | if (MapI != Weights.end()) |
| 612 | Weight += MapI->second; |
| 613 | } |
| 614 | return (Weight == 0) ? DEFAULT_WEIGHT : Weight; |
Andrew Trick | 3d4e64b | 2011-06-11 01:05:22 +0000 | [diff] [blame] | 615 | } |
| 616 | |
Manman Ren | cf10446 | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 617 | /// Set the edge weight for a given edge specified by PredBlock and an index |
| 618 | /// to the successors. |
| 619 | void BranchProbabilityInfo:: |
| 620 | setEdgeWeight(const BasicBlock *Src, unsigned IndexInSuccessors, |
| 621 | uint32_t Weight) { |
| 622 | Weights[std::make_pair(Src, IndexInSuccessors)] = Weight; |
| 623 | DEBUG(dbgs() << "set edge " << Src->getName() << " -> " |
| 624 | << IndexInSuccessors << " successor weight to " |
| 625 | << Weight << "\n"); |
| 626 | } |
Andrew Trick | 3d4e64b | 2011-06-11 01:05:22 +0000 | [diff] [blame] | 627 | |
Manman Ren | cf10446 | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 628 | /// Get an edge's probability, relative to other out-edges from Src. |
| 629 | BranchProbability BranchProbabilityInfo:: |
| 630 | getEdgeProbability(const BasicBlock *Src, unsigned IndexInSuccessors) const { |
| 631 | uint32_t N = getEdgeWeight(Src, IndexInSuccessors); |
| 632 | uint32_t D = getSumForBlock(Src); |
| 633 | |
| 634 | return BranchProbability(N, D); |
| 635 | } |
| 636 | |
| 637 | /// Get the probability of going from Src to Dst. It returns the sum of all |
| 638 | /// probabilities for edges from Src to Dst. |
Andrew Trick | 3d4e64b | 2011-06-11 01:05:22 +0000 | [diff] [blame] | 639 | BranchProbability BranchProbabilityInfo:: |
Jakub Staszak | efd94c8 | 2011-07-29 19:30:00 +0000 | [diff] [blame] | 640 | getEdgeProbability(const BasicBlock *Src, const BasicBlock *Dst) const { |
Andrew Trick | 3d4e64b | 2011-06-11 01:05:22 +0000 | [diff] [blame] | 641 | |
| 642 | uint32_t N = getEdgeWeight(Src, Dst); |
| 643 | uint32_t D = getSumForBlock(Src); |
| 644 | |
| 645 | return BranchProbability(N, D); |
Andrew Trick | 49371f3 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 646 | } |
| 647 | |
| 648 | raw_ostream & |
Chandler Carruth | 1c8ace0 | 2011-10-23 21:21:50 +0000 | [diff] [blame] | 649 | BranchProbabilityInfo::printEdgeProbability(raw_ostream &OS, |
| 650 | const BasicBlock *Src, |
| 651 | const BasicBlock *Dst) const { |
Andrew Trick | 49371f3 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 652 | |
Jakub Staszak | 12a43bd | 2011-06-16 20:22:37 +0000 | [diff] [blame] | 653 | const BranchProbability Prob = getEdgeProbability(Src, Dst); |
Benjamin Kramer | 1f97a5a | 2011-11-15 16:27:03 +0000 | [diff] [blame] | 654 | OS << "edge " << Src->getName() << " -> " << Dst->getName() |
Andrew Trick | 3d4e64b | 2011-06-11 01:05:22 +0000 | [diff] [blame] | 655 | << " probability is " << Prob |
| 656 | << (isEdgeHot(Src, Dst) ? " [HOT edge]\n" : "\n"); |
Andrew Trick | 49371f3 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 657 | |
| 658 | return OS; |
| 659 | } |