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 | |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 14 | #include "llvm/Analysis/BranchProbabilityInfo.h" |
| 15 | #include "llvm/ADT/PostOrderIterator.h" |
| 16 | #include "llvm/Analysis/LoopInfo.h" |
John Brawn | da4a68a | 2017-06-08 09:44:40 +0000 | [diff] [blame^] | 17 | #include "llvm/Analysis/TargetLibraryInfo.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" |
Benjamin Kramer | 16132e6 | 2015-03-23 18:07:13 +0000 | [diff] [blame] | 25 | #include "llvm/Support/raw_ostream.h" |
Andrew Trick | 49371f3 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 26 | |
| 27 | using namespace llvm; |
| 28 | |
Chandler Carruth | f1221bd | 2014-04-22 02:48:03 +0000 | [diff] [blame] | 29 | #define DEBUG_TYPE "branch-prob" |
| 30 | |
Cong Hou | ab23bfb | 2015-07-15 22:48:29 +0000 | [diff] [blame] | 31 | INITIALIZE_PASS_BEGIN(BranchProbabilityInfoWrapperPass, "branch-prob", |
Andrew Trick | 49371f3 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 32 | "Branch Probability Analysis", false, true) |
Chandler Carruth | 4f8f307 | 2015-01-17 14:16:18 +0000 | [diff] [blame] | 33 | INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass) |
John Brawn | da4a68a | 2017-06-08 09:44:40 +0000 | [diff] [blame^] | 34 | INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass) |
Cong Hou | ab23bfb | 2015-07-15 22:48:29 +0000 | [diff] [blame] | 35 | INITIALIZE_PASS_END(BranchProbabilityInfoWrapperPass, "branch-prob", |
Andrew Trick | 49371f3 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 36 | "Branch Probability Analysis", false, true) |
| 37 | |
Cong Hou | ab23bfb | 2015-07-15 22:48:29 +0000 | [diff] [blame] | 38 | char BranchProbabilityInfoWrapperPass::ID = 0; |
Andrew Trick | 49371f3 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 39 | |
Chandler Carruth | 7a0094a | 2011-10-24 01:40:45 +0000 | [diff] [blame] | 40 | // Weights are for internal use only. They are used by heuristics to help to |
| 41 | // estimate edges' probability. Example: |
| 42 | // |
| 43 | // Using "Loop Branch Heuristics" we predict weights of edges for the |
| 44 | // block BB2. |
| 45 | // ... |
| 46 | // | |
| 47 | // V |
| 48 | // BB1<-+ |
| 49 | // | | |
| 50 | // | | (Weight = 124) |
| 51 | // V | |
| 52 | // BB2--+ |
| 53 | // | |
| 54 | // | (Weight = 4) |
| 55 | // V |
| 56 | // BB3 |
| 57 | // |
| 58 | // Probability of the edge BB2->BB1 = 124 / (124 + 4) = 0.96875 |
| 59 | // Probability of the edge BB2->BB3 = 4 / (124 + 4) = 0.03125 |
| 60 | static const uint32_t LBH_TAKEN_WEIGHT = 124; |
| 61 | static const uint32_t LBH_NONTAKEN_WEIGHT = 4; |
Andrew Trick | 49371f3 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 62 | |
Serguei Katkov | ba831f7 | 2017-05-18 06:11:56 +0000 | [diff] [blame] | 63 | /// \brief Unreachable-terminating branch taken probability. |
Chandler Carruth | 7111f45 | 2011-10-24 12:01:08 +0000 | [diff] [blame] | 64 | /// |
Serguei Katkov | ba831f7 | 2017-05-18 06:11:56 +0000 | [diff] [blame] | 65 | /// This is the probability for a branch being taken to a block that terminates |
Chandler Carruth | 7111f45 | 2011-10-24 12:01:08 +0000 | [diff] [blame] | 66 | /// (eventually) in unreachable. These are predicted as unlikely as possible. |
Serguei Katkov | ba831f7 | 2017-05-18 06:11:56 +0000 | [diff] [blame] | 67 | /// All reachable probability will equally share the remaining part. |
| 68 | static const BranchProbability UR_TAKEN_PROB = BranchProbability::getRaw(1); |
Serguei Katkov | 2616bbb | 2017-04-17 04:33:04 +0000 | [diff] [blame] | 69 | |
Diego Novillo | c639953 | 2013-05-24 12:26:52 +0000 | [diff] [blame] | 70 | /// \brief Weight for a branch taken going into a cold block. |
| 71 | /// |
| 72 | /// This is the weight for a branch taken toward a block marked |
| 73 | /// cold. A block is marked cold if it's postdominated by a |
| 74 | /// block containing a call to a cold function. Cold functions |
| 75 | /// are those marked with attribute 'cold'. |
| 76 | static const uint32_t CC_TAKEN_WEIGHT = 4; |
| 77 | |
| 78 | /// \brief Weight for a branch not-taken into a cold block. |
| 79 | /// |
| 80 | /// This is the weight for a branch not taken toward a block marked |
| 81 | /// cold. |
| 82 | static const uint32_t CC_NONTAKEN_WEIGHT = 64; |
| 83 | |
Chandler Carruth | 7a0094a | 2011-10-24 01:40:45 +0000 | [diff] [blame] | 84 | static const uint32_t PH_TAKEN_WEIGHT = 20; |
| 85 | static const uint32_t PH_NONTAKEN_WEIGHT = 12; |
Andrew Trick | 49371f3 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 86 | |
Chandler Carruth | 7a0094a | 2011-10-24 01:40:45 +0000 | [diff] [blame] | 87 | static const uint32_t ZH_TAKEN_WEIGHT = 20; |
| 88 | static const uint32_t ZH_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 FPH_TAKEN_WEIGHT = 20; |
| 91 | static const uint32_t FPH_NONTAKEN_WEIGHT = 12; |
Andrew Trick | 49371f3 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 92 | |
Bill Wendling | e1c5426 | 2012-08-15 12:22:35 +0000 | [diff] [blame] | 93 | /// \brief Invoke-terminating normal branch taken weight |
| 94 | /// |
| 95 | /// This is the weight for branching to the normal destination of an invoke |
| 96 | /// instruction. We expect this to happen most of the time. Set the weight to an |
| 97 | /// absurdly high value so that nested loops subsume it. |
| 98 | static const uint32_t IH_TAKEN_WEIGHT = 1024 * 1024 - 1; |
| 99 | |
| 100 | /// \brief Invoke-terminating normal branch not-taken weight. |
| 101 | /// |
| 102 | /// This is the weight for branching to the unwind destination of an invoke |
| 103 | /// instruction. This is essentially never taken. |
| 104 | static const uint32_t IH_NONTAKEN_WEIGHT = 1; |
| 105 | |
Serguei Katkov | ecebc3d | 2017-04-12 05:42:14 +0000 | [diff] [blame] | 106 | /// \brief Add \p BB to PostDominatedByUnreachable set if applicable. |
| 107 | void |
| 108 | BranchProbabilityInfo::updatePostDominatedByUnreachable(const BasicBlock *BB) { |
Mehdi Amini | a797877 | 2016-04-07 21:59:28 +0000 | [diff] [blame] | 109 | const TerminatorInst *TI = BB->getTerminator(); |
Chandler Carruth | 7111f45 | 2011-10-24 12:01:08 +0000 | [diff] [blame] | 110 | if (TI->getNumSuccessors() == 0) { |
Sanjoy Das | 432c1c3 | 2016-04-18 19:01:28 +0000 | [diff] [blame] | 111 | if (isa<UnreachableInst>(TI) || |
| 112 | // If this block is terminated by a call to |
| 113 | // @llvm.experimental.deoptimize then treat it like an unreachable since |
| 114 | // the @llvm.experimental.deoptimize call is expected to practically |
| 115 | // never execute. |
| 116 | BB->getTerminatingDeoptimizeCall()) |
Chandler Carruth | 7111f45 | 2011-10-24 12:01:08 +0000 | [diff] [blame] | 117 | PostDominatedByUnreachable.insert(BB); |
Serguei Katkov | ecebc3d | 2017-04-12 05:42:14 +0000 | [diff] [blame] | 118 | return; |
Chandler Carruth | 7111f45 | 2011-10-24 12:01:08 +0000 | [diff] [blame] | 119 | } |
| 120 | |
Serguei Katkov | ecebc3d | 2017-04-12 05:42:14 +0000 | [diff] [blame] | 121 | // If the terminator is an InvokeInst, check only the normal destination block |
| 122 | // as the unwind edge of InvokeInst is also very unlikely taken. |
| 123 | if (auto *II = dyn_cast<InvokeInst>(TI)) { |
| 124 | if (PostDominatedByUnreachable.count(II->getNormalDest())) |
| 125 | PostDominatedByUnreachable.insert(BB); |
| 126 | return; |
| 127 | } |
| 128 | |
| 129 | for (auto *I : successors(BB)) |
| 130 | // If any of successor is not post dominated then BB is also not. |
| 131 | if (!PostDominatedByUnreachable.count(I)) |
| 132 | return; |
| 133 | |
| 134 | PostDominatedByUnreachable.insert(BB); |
| 135 | } |
| 136 | |
| 137 | /// \brief Add \p BB to PostDominatedByColdCall set if applicable. |
| 138 | void |
| 139 | BranchProbabilityInfo::updatePostDominatedByColdCall(const BasicBlock *BB) { |
| 140 | assert(!PostDominatedByColdCall.count(BB)); |
| 141 | const TerminatorInst *TI = BB->getTerminator(); |
| 142 | if (TI->getNumSuccessors() == 0) |
| 143 | return; |
| 144 | |
| 145 | // If all of successor are post dominated then BB is also done. |
| 146 | if (llvm::all_of(successors(BB), [&](const BasicBlock *SuccBB) { |
| 147 | return PostDominatedByColdCall.count(SuccBB); |
| 148 | })) { |
| 149 | PostDominatedByColdCall.insert(BB); |
| 150 | return; |
| 151 | } |
| 152 | |
| 153 | // If the terminator is an InvokeInst, check only the normal destination |
| 154 | // block as the unwind edge of InvokeInst is also very unlikely taken. |
| 155 | if (auto *II = dyn_cast<InvokeInst>(TI)) |
| 156 | if (PostDominatedByColdCall.count(II->getNormalDest())) { |
| 157 | PostDominatedByColdCall.insert(BB); |
| 158 | return; |
| 159 | } |
| 160 | |
| 161 | // Otherwise, if the block itself contains a cold function, add it to the |
| 162 | // set of blocks post-dominated by a cold call. |
| 163 | for (auto &I : *BB) |
| 164 | if (const CallInst *CI = dyn_cast<CallInst>(&I)) |
| 165 | if (CI->hasFnAttr(Attribute::Cold)) { |
| 166 | PostDominatedByColdCall.insert(BB); |
| 167 | return; |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | /// \brief Calculate edge weights for successors lead to unreachable. |
| 172 | /// |
| 173 | /// Predict that a successor which leads necessarily to an |
| 174 | /// unreachable-terminated block as extremely unlikely. |
| 175 | bool BranchProbabilityInfo::calcUnreachableHeuristics(const BasicBlock *BB) { |
| 176 | const TerminatorInst *TI = BB->getTerminator(); |
Serguei Katkov | 11d9c4f | 2017-04-17 06:39:47 +0000 | [diff] [blame] | 177 | assert(TI->getNumSuccessors() > 1 && "expected more than one successor!"); |
| 178 | |
| 179 | // Return false here so that edge weights for InvokeInst could be decided |
| 180 | // in calcInvokeHeuristics(). |
| 181 | if (isa<InvokeInst>(TI)) |
Serguei Katkov | ecebc3d | 2017-04-12 05:42:14 +0000 | [diff] [blame] | 182 | return false; |
| 183 | |
Manman Ren | cf10446 | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 184 | SmallVector<unsigned, 4> UnreachableEdges; |
| 185 | SmallVector<unsigned, 4> ReachableEdges; |
Chandler Carruth | 7111f45 | 2011-10-24 12:01:08 +0000 | [diff] [blame] | 186 | |
Serguei Katkov | ecebc3d | 2017-04-12 05:42:14 +0000 | [diff] [blame] | 187 | for (succ_const_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I) |
Chandler Carruth | 7111f45 | 2011-10-24 12:01:08 +0000 | [diff] [blame] | 188 | if (PostDominatedByUnreachable.count(*I)) |
Manman Ren | cf10446 | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 189 | UnreachableEdges.push_back(I.getSuccessorIndex()); |
Chandler Carruth | 7111f45 | 2011-10-24 12:01:08 +0000 | [diff] [blame] | 190 | else |
Manman Ren | cf10446 | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 191 | ReachableEdges.push_back(I.getSuccessorIndex()); |
Chandler Carruth | 7111f45 | 2011-10-24 12:01:08 +0000 | [diff] [blame] | 192 | |
Serguei Katkov | 11d9c4f | 2017-04-17 06:39:47 +0000 | [diff] [blame] | 193 | // Skip probabilities if all were reachable. |
| 194 | if (UnreachableEdges.empty()) |
Serguei Katkov | ecebc3d | 2017-04-12 05:42:14 +0000 | [diff] [blame] | 195 | return false; |
Jun Bum Lim | a23e5f7 | 2015-12-21 22:00:51 +0000 | [diff] [blame] | 196 | |
Cong Hou | e93b8e1 | 2015-12-22 18:56:14 +0000 | [diff] [blame] | 197 | if (ReachableEdges.empty()) { |
| 198 | BranchProbability Prob(1, UnreachableEdges.size()); |
| 199 | for (unsigned SuccIdx : UnreachableEdges) |
| 200 | setEdgeProbability(BB, SuccIdx, Prob); |
Chandler Carruth | 7111f45 | 2011-10-24 12:01:08 +0000 | [diff] [blame] | 201 | return true; |
Cong Hou | e93b8e1 | 2015-12-22 18:56:14 +0000 | [diff] [blame] | 202 | } |
| 203 | |
Serguei Katkov | ba831f7 | 2017-05-18 06:11:56 +0000 | [diff] [blame] | 204 | auto UnreachableProb = UR_TAKEN_PROB; |
| 205 | auto ReachableProb = |
| 206 | (BranchProbability::getOne() - UR_TAKEN_PROB * UnreachableEdges.size()) / |
| 207 | ReachableEdges.size(); |
Cong Hou | e93b8e1 | 2015-12-22 18:56:14 +0000 | [diff] [blame] | 208 | |
| 209 | for (unsigned SuccIdx : UnreachableEdges) |
| 210 | setEdgeProbability(BB, SuccIdx, UnreachableProb); |
| 211 | for (unsigned SuccIdx : ReachableEdges) |
| 212 | setEdgeProbability(BB, SuccIdx, ReachableProb); |
Chandler Carruth | 7111f45 | 2011-10-24 12:01:08 +0000 | [diff] [blame] | 213 | |
| 214 | return true; |
| 215 | } |
| 216 | |
Chandler Carruth | d27a7a9 | 2011-10-19 10:30:30 +0000 | [diff] [blame] | 217 | // Propagate existing explicit probabilities from either profile data or |
Serguei Katkov | 2616bbb | 2017-04-17 04:33:04 +0000 | [diff] [blame] | 218 | // 'expect' intrinsic processing. Examine metadata against unreachable |
| 219 | // heuristic. The probability of the edge coming to unreachable block is |
| 220 | // set to min of metadata and unreachable heuristic. |
Mehdi Amini | a797877 | 2016-04-07 21:59:28 +0000 | [diff] [blame] | 221 | bool BranchProbabilityInfo::calcMetadataWeights(const BasicBlock *BB) { |
| 222 | const TerminatorInst *TI = BB->getTerminator(); |
Serguei Katkov | 11d9c4f | 2017-04-17 06:39:47 +0000 | [diff] [blame] | 223 | assert(TI->getNumSuccessors() > 1 && "expected more than one successor!"); |
Chandler Carruth | deac50c | 2011-10-19 10:32:19 +0000 | [diff] [blame] | 224 | if (!isa<BranchInst>(TI) && !isa<SwitchInst>(TI)) |
Chandler Carruth | d27a7a9 | 2011-10-19 10:30:30 +0000 | [diff] [blame] | 225 | return false; |
| 226 | |
Duncan P. N. Exon Smith | de36e80 | 2014-11-11 21:30:22 +0000 | [diff] [blame] | 227 | MDNode *WeightsNode = TI->getMetadata(LLVMContext::MD_prof); |
Chandler Carruth | deac50c | 2011-10-19 10:32:19 +0000 | [diff] [blame] | 228 | if (!WeightsNode) |
Chandler Carruth | d27a7a9 | 2011-10-19 10:30:30 +0000 | [diff] [blame] | 229 | return false; |
| 230 | |
Diego Novillo | de5b801 | 2015-05-07 17:22:06 +0000 | [diff] [blame] | 231 | // Check that the number of successors is manageable. |
| 232 | assert(TI->getNumSuccessors() < UINT32_MAX && "Too many successors"); |
| 233 | |
Chandler Carruth | deac50c | 2011-10-19 10:32:19 +0000 | [diff] [blame] | 234 | // Ensure there are weights for all of the successors. Note that the first |
| 235 | // operand to the metadata node is a name, not a weight. |
| 236 | if (WeightsNode->getNumOperands() != TI->getNumSuccessors() + 1) |
Chandler Carruth | d27a7a9 | 2011-10-19 10:30:30 +0000 | [diff] [blame] | 237 | return false; |
| 238 | |
Diego Novillo | de5b801 | 2015-05-07 17:22:06 +0000 | [diff] [blame] | 239 | // Build up the final weights that will be used in a temporary buffer. |
| 240 | // Compute the sum of all weights to later decide whether they need to |
| 241 | // be scaled to fit in 32 bits. |
| 242 | uint64_t WeightSum = 0; |
Chandler Carruth | deac50c | 2011-10-19 10:32:19 +0000 | [diff] [blame] | 243 | SmallVector<uint32_t, 2> Weights; |
Serguei Katkov | 2616bbb | 2017-04-17 04:33:04 +0000 | [diff] [blame] | 244 | SmallVector<unsigned, 2> UnreachableIdxs; |
| 245 | SmallVector<unsigned, 2> ReachableIdxs; |
Chandler Carruth | deac50c | 2011-10-19 10:32:19 +0000 | [diff] [blame] | 246 | Weights.reserve(TI->getNumSuccessors()); |
| 247 | for (unsigned i = 1, e = WeightsNode->getNumOperands(); i != e; ++i) { |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 248 | ConstantInt *Weight = |
| 249 | mdconst::dyn_extract<ConstantInt>(WeightsNode->getOperand(i)); |
Chandler Carruth | deac50c | 2011-10-19 10:32:19 +0000 | [diff] [blame] | 250 | if (!Weight) |
| 251 | return false; |
Diego Novillo | de5b801 | 2015-05-07 17:22:06 +0000 | [diff] [blame] | 252 | assert(Weight->getValue().getActiveBits() <= 32 && |
| 253 | "Too many bits for uint32_t"); |
| 254 | Weights.push_back(Weight->getZExtValue()); |
| 255 | WeightSum += Weights.back(); |
Serguei Katkov | 2616bbb | 2017-04-17 04:33:04 +0000 | [diff] [blame] | 256 | if (PostDominatedByUnreachable.count(TI->getSuccessor(i - 1))) |
| 257 | UnreachableIdxs.push_back(i - 1); |
| 258 | else |
| 259 | ReachableIdxs.push_back(i - 1); |
Chandler Carruth | deac50c | 2011-10-19 10:32:19 +0000 | [diff] [blame] | 260 | } |
| 261 | assert(Weights.size() == TI->getNumSuccessors() && "Checked above"); |
Diego Novillo | de5b801 | 2015-05-07 17:22:06 +0000 | [diff] [blame] | 262 | |
| 263 | // If the sum of weights does not fit in 32 bits, scale every weight down |
| 264 | // accordingly. |
| 265 | uint64_t ScalingFactor = |
| 266 | (WeightSum > UINT32_MAX) ? WeightSum / UINT32_MAX + 1 : 1; |
| 267 | |
Serguei Katkov | 2616bbb | 2017-04-17 04:33:04 +0000 | [diff] [blame] | 268 | if (ScalingFactor > 1) { |
| 269 | WeightSum = 0; |
| 270 | for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i) { |
| 271 | Weights[i] /= ScalingFactor; |
| 272 | WeightSum += Weights[i]; |
| 273 | } |
Diego Novillo | de5b801 | 2015-05-07 17:22:06 +0000 | [diff] [blame] | 274 | } |
Serguei Katkov | 63c9c81 | 2017-05-12 07:50:06 +0000 | [diff] [blame] | 275 | assert(WeightSum <= UINT32_MAX && |
| 276 | "Expected weights to scale down to 32 bits"); |
Cong Hou | 6a2c71a | 2015-12-22 23:45:55 +0000 | [diff] [blame] | 277 | |
Serguei Katkov | 2616bbb | 2017-04-17 04:33:04 +0000 | [diff] [blame] | 278 | if (WeightSum == 0 || ReachableIdxs.size() == 0) { |
Cong Hou | 6a2c71a | 2015-12-22 23:45:55 +0000 | [diff] [blame] | 279 | for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i) |
Serguei Katkov | 2616bbb | 2017-04-17 04:33:04 +0000 | [diff] [blame] | 280 | Weights[i] = 1; |
| 281 | WeightSum = TI->getNumSuccessors(); |
Cong Hou | 6a2c71a | 2015-12-22 23:45:55 +0000 | [diff] [blame] | 282 | } |
Cong Hou | e93b8e1 | 2015-12-22 18:56:14 +0000 | [diff] [blame] | 283 | |
Serguei Katkov | 2616bbb | 2017-04-17 04:33:04 +0000 | [diff] [blame] | 284 | // Set the probability. |
| 285 | SmallVector<BranchProbability, 2> BP; |
| 286 | for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i) |
| 287 | BP.push_back({ Weights[i], static_cast<uint32_t>(WeightSum) }); |
| 288 | |
| 289 | // Examine the metadata against unreachable heuristic. |
| 290 | // If the unreachable heuristic is more strong then we use it for this edge. |
| 291 | if (UnreachableIdxs.size() > 0 && ReachableIdxs.size() > 0) { |
| 292 | auto ToDistribute = BranchProbability::getZero(); |
Serguei Katkov | ba831f7 | 2017-05-18 06:11:56 +0000 | [diff] [blame] | 293 | auto UnreachableProb = UR_TAKEN_PROB; |
Serguei Katkov | 2616bbb | 2017-04-17 04:33:04 +0000 | [diff] [blame] | 294 | for (auto i : UnreachableIdxs) |
| 295 | if (UnreachableProb < BP[i]) { |
| 296 | ToDistribute += BP[i] - UnreachableProb; |
| 297 | BP[i] = UnreachableProb; |
| 298 | } |
| 299 | |
| 300 | // If we modified the probability of some edges then we must distribute |
| 301 | // the difference between reachable blocks. |
| 302 | if (ToDistribute > BranchProbability::getZero()) { |
| 303 | BranchProbability PerEdge = ToDistribute / ReachableIdxs.size(); |
Serguei Katkov | 63c9c81 | 2017-05-12 07:50:06 +0000 | [diff] [blame] | 304 | for (auto i : ReachableIdxs) |
Serguei Katkov | 2616bbb | 2017-04-17 04:33:04 +0000 | [diff] [blame] | 305 | BP[i] += PerEdge; |
Serguei Katkov | 2616bbb | 2017-04-17 04:33:04 +0000 | [diff] [blame] | 306 | } |
| 307 | } |
| 308 | |
| 309 | for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i) |
| 310 | setEdgeProbability(BB, i, BP[i]); |
| 311 | |
Chandler Carruth | d27a7a9 | 2011-10-19 10:30:30 +0000 | [diff] [blame] | 312 | return true; |
| 313 | } |
| 314 | |
Diego Novillo | c639953 | 2013-05-24 12:26:52 +0000 | [diff] [blame] | 315 | /// \brief Calculate edge weights for edges leading to cold blocks. |
| 316 | /// |
| 317 | /// A cold block is one post-dominated by a block with a call to a |
| 318 | /// cold function. Those edges are unlikely to be taken, so we give |
| 319 | /// them relatively low weight. |
| 320 | /// |
| 321 | /// Return true if we could compute the weights for cold edges. |
| 322 | /// Return false, otherwise. |
Mehdi Amini | a797877 | 2016-04-07 21:59:28 +0000 | [diff] [blame] | 323 | bool BranchProbabilityInfo::calcColdCallHeuristics(const BasicBlock *BB) { |
| 324 | const TerminatorInst *TI = BB->getTerminator(); |
Serguei Katkov | 11d9c4f | 2017-04-17 06:39:47 +0000 | [diff] [blame] | 325 | assert(TI->getNumSuccessors() > 1 && "expected more than one successor!"); |
| 326 | |
| 327 | // Return false here so that edge weights for InvokeInst could be decided |
| 328 | // in calcInvokeHeuristics(). |
| 329 | if (isa<InvokeInst>(TI)) |
Diego Novillo | c639953 | 2013-05-24 12:26:52 +0000 | [diff] [blame] | 330 | return false; |
| 331 | |
| 332 | // Determine which successors are post-dominated by a cold block. |
| 333 | SmallVector<unsigned, 4> ColdEdges; |
Diego Novillo | c639953 | 2013-05-24 12:26:52 +0000 | [diff] [blame] | 334 | SmallVector<unsigned, 4> NormalEdges; |
Mehdi Amini | a797877 | 2016-04-07 21:59:28 +0000 | [diff] [blame] | 335 | for (succ_const_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I) |
Diego Novillo | c639953 | 2013-05-24 12:26:52 +0000 | [diff] [blame] | 336 | if (PostDominatedByColdCall.count(*I)) |
| 337 | ColdEdges.push_back(I.getSuccessorIndex()); |
| 338 | else |
| 339 | NormalEdges.push_back(I.getSuccessorIndex()); |
| 340 | |
Serguei Katkov | 11d9c4f | 2017-04-17 06:39:47 +0000 | [diff] [blame] | 341 | // Skip probabilities if no cold edges. |
| 342 | if (ColdEdges.empty()) |
Diego Novillo | c639953 | 2013-05-24 12:26:52 +0000 | [diff] [blame] | 343 | return false; |
| 344 | |
Cong Hou | e93b8e1 | 2015-12-22 18:56:14 +0000 | [diff] [blame] | 345 | if (NormalEdges.empty()) { |
| 346 | BranchProbability Prob(1, ColdEdges.size()); |
| 347 | for (unsigned SuccIdx : ColdEdges) |
| 348 | setEdgeProbability(BB, SuccIdx, Prob); |
Diego Novillo | c639953 | 2013-05-24 12:26:52 +0000 | [diff] [blame] | 349 | return true; |
Cong Hou | e93b8e1 | 2015-12-22 18:56:14 +0000 | [diff] [blame] | 350 | } |
| 351 | |
Vedant Kumar | a4bd146 | 2016-12-17 01:02:08 +0000 | [diff] [blame] | 352 | auto ColdProb = BranchProbability::getBranchProbability( |
| 353 | CC_TAKEN_WEIGHT, |
| 354 | (CC_TAKEN_WEIGHT + CC_NONTAKEN_WEIGHT) * uint64_t(ColdEdges.size())); |
| 355 | auto NormalProb = BranchProbability::getBranchProbability( |
| 356 | CC_NONTAKEN_WEIGHT, |
| 357 | (CC_TAKEN_WEIGHT + CC_NONTAKEN_WEIGHT) * uint64_t(NormalEdges.size())); |
Cong Hou | e93b8e1 | 2015-12-22 18:56:14 +0000 | [diff] [blame] | 358 | |
| 359 | for (unsigned SuccIdx : ColdEdges) |
| 360 | setEdgeProbability(BB, SuccIdx, ColdProb); |
| 361 | for (unsigned SuccIdx : NormalEdges) |
| 362 | setEdgeProbability(BB, SuccIdx, NormalProb); |
Diego Novillo | c639953 | 2013-05-24 12:26:52 +0000 | [diff] [blame] | 363 | |
| 364 | return true; |
| 365 | } |
| 366 | |
Andrew Trick | 49371f3 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 367 | // Calculate Edge Weights using "Pointer Heuristics". Predict a comparsion |
| 368 | // between two pointer or pointer and NULL will fail. |
Mehdi Amini | a797877 | 2016-04-07 21:59:28 +0000 | [diff] [blame] | 369 | bool BranchProbabilityInfo::calcPointerHeuristics(const BasicBlock *BB) { |
| 370 | const BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator()); |
Andrew Trick | 49371f3 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 371 | if (!BI || !BI->isConditional()) |
Jakub Staszak | d07b2e1 | 2011-07-28 21:45:07 +0000 | [diff] [blame] | 372 | return false; |
Andrew Trick | 49371f3 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 373 | |
| 374 | Value *Cond = BI->getCondition(); |
| 375 | ICmpInst *CI = dyn_cast<ICmpInst>(Cond); |
Jakub Staszak | abb236f | 2011-07-15 20:51:06 +0000 | [diff] [blame] | 376 | if (!CI || !CI->isEquality()) |
Jakub Staszak | d07b2e1 | 2011-07-28 21:45:07 +0000 | [diff] [blame] | 377 | return false; |
Andrew Trick | 49371f3 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 378 | |
| 379 | Value *LHS = CI->getOperand(0); |
Andrew Trick | 49371f3 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 380 | |
| 381 | if (!LHS->getType()->isPointerTy()) |
Jakub Staszak | d07b2e1 | 2011-07-28 21:45:07 +0000 | [diff] [blame] | 382 | return false; |
Andrew Trick | 49371f3 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 383 | |
Nick Lewycky | 75b2053 | 2011-06-04 02:07:10 +0000 | [diff] [blame] | 384 | assert(CI->getOperand(1)->getType()->isPointerTy()); |
Andrew Trick | 49371f3 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 385 | |
Andrew Trick | 49371f3 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 386 | // p != 0 -> isProb = true |
| 387 | // p == 0 -> isProb = false |
| 388 | // p != q -> isProb = true |
| 389 | // p == q -> isProb = false; |
Manman Ren | cf10446 | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 390 | unsigned TakenIdx = 0, NonTakenIdx = 1; |
Jakub Staszak | abb236f | 2011-07-15 20:51:06 +0000 | [diff] [blame] | 391 | bool isProb = CI->getPredicate() == ICmpInst::ICMP_NE; |
Andrew Trick | 49371f3 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 392 | if (!isProb) |
Manman Ren | cf10446 | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 393 | std::swap(TakenIdx, NonTakenIdx); |
Andrew Trick | 49371f3 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 394 | |
Cong Hou | e93b8e1 | 2015-12-22 18:56:14 +0000 | [diff] [blame] | 395 | BranchProbability TakenProb(PH_TAKEN_WEIGHT, |
| 396 | PH_TAKEN_WEIGHT + PH_NONTAKEN_WEIGHT); |
| 397 | setEdgeProbability(BB, TakenIdx, TakenProb); |
| 398 | setEdgeProbability(BB, NonTakenIdx, TakenProb.getCompl()); |
Jakub Staszak | d07b2e1 | 2011-07-28 21:45:07 +0000 | [diff] [blame] | 399 | return true; |
Andrew Trick | 49371f3 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | // Calculate Edge Weights using "Loop Branch Heuristics". Predict backedges |
| 403 | // as taken, exiting edges as not-taken. |
Mehdi Amini | a797877 | 2016-04-07 21:59:28 +0000 | [diff] [blame] | 404 | bool BranchProbabilityInfo::calcLoopBranchHeuristics(const BasicBlock *BB, |
Cong Hou | ab23bfb | 2015-07-15 22:48:29 +0000 | [diff] [blame] | 405 | const LoopInfo &LI) { |
| 406 | Loop *L = LI.getLoopFor(BB); |
Andrew Trick | 49371f3 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 407 | if (!L) |
Jakub Staszak | d07b2e1 | 2011-07-28 21:45:07 +0000 | [diff] [blame] | 408 | return false; |
Andrew Trick | 49371f3 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 409 | |
Manman Ren | cf10446 | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 410 | SmallVector<unsigned, 8> BackEdges; |
| 411 | SmallVector<unsigned, 8> ExitingEdges; |
| 412 | SmallVector<unsigned, 8> InEdges; // Edges from header to the loop. |
Jakub Staszak | bcb3c65 | 2011-07-28 21:33:46 +0000 | [diff] [blame] | 413 | |
Mehdi Amini | a797877 | 2016-04-07 21:59:28 +0000 | [diff] [blame] | 414 | for (succ_const_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I) { |
Chandler Carruth | 32f46e7 | 2011-10-25 09:47:41 +0000 | [diff] [blame] | 415 | if (!L->contains(*I)) |
Manman Ren | cf10446 | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 416 | ExitingEdges.push_back(I.getSuccessorIndex()); |
Chandler Carruth | 32f46e7 | 2011-10-25 09:47:41 +0000 | [diff] [blame] | 417 | else if (L->getHeader() == *I) |
Manman Ren | cf10446 | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 418 | BackEdges.push_back(I.getSuccessorIndex()); |
Chandler Carruth | 32f46e7 | 2011-10-25 09:47:41 +0000 | [diff] [blame] | 419 | else |
Manman Ren | cf10446 | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 420 | InEdges.push_back(I.getSuccessorIndex()); |
Andrew Trick | 49371f3 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 421 | } |
| 422 | |
Akira Hatanaka | 5638b89 | 2014-04-14 16:56:19 +0000 | [diff] [blame] | 423 | if (BackEdges.empty() && ExitingEdges.empty()) |
| 424 | return false; |
| 425 | |
Cong Hou | e93b8e1 | 2015-12-22 18:56:14 +0000 | [diff] [blame] | 426 | // Collect the sum of probabilities of back-edges/in-edges/exiting-edges, and |
| 427 | // normalize them so that they sum up to one. |
Benjamin Kramer | 1d67ac5 | 2016-06-17 13:15:10 +0000 | [diff] [blame] | 428 | BranchProbability Probs[] = {BranchProbability::getZero(), |
| 429 | BranchProbability::getZero(), |
| 430 | BranchProbability::getZero()}; |
Cong Hou | e93b8e1 | 2015-12-22 18:56:14 +0000 | [diff] [blame] | 431 | unsigned Denom = (BackEdges.empty() ? 0 : LBH_TAKEN_WEIGHT) + |
| 432 | (InEdges.empty() ? 0 : LBH_TAKEN_WEIGHT) + |
| 433 | (ExitingEdges.empty() ? 0 : LBH_NONTAKEN_WEIGHT); |
| 434 | if (!BackEdges.empty()) |
| 435 | Probs[0] = BranchProbability(LBH_TAKEN_WEIGHT, Denom); |
| 436 | if (!InEdges.empty()) |
| 437 | Probs[1] = BranchProbability(LBH_TAKEN_WEIGHT, Denom); |
| 438 | if (!ExitingEdges.empty()) |
| 439 | Probs[2] = BranchProbability(LBH_NONTAKEN_WEIGHT, Denom); |
Andrew Trick | 49371f3 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 440 | |
Cong Hou | e93b8e1 | 2015-12-22 18:56:14 +0000 | [diff] [blame] | 441 | if (uint32_t numBackEdges = BackEdges.size()) { |
| 442 | auto Prob = Probs[0] / numBackEdges; |
| 443 | for (unsigned SuccIdx : BackEdges) |
| 444 | setEdgeProbability(BB, SuccIdx, Prob); |
Andrew Trick | 49371f3 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 445 | } |
| 446 | |
Jakub Staszak | bcb3c65 | 2011-07-28 21:33:46 +0000 | [diff] [blame] | 447 | if (uint32_t numInEdges = InEdges.size()) { |
Cong Hou | e93b8e1 | 2015-12-22 18:56:14 +0000 | [diff] [blame] | 448 | auto Prob = Probs[1] / numInEdges; |
| 449 | for (unsigned SuccIdx : InEdges) |
| 450 | setEdgeProbability(BB, SuccIdx, Prob); |
Jakub Staszak | bcb3c65 | 2011-07-28 21:33:46 +0000 | [diff] [blame] | 451 | } |
| 452 | |
Chandler Carruth | 32f46e7 | 2011-10-25 09:47:41 +0000 | [diff] [blame] | 453 | if (uint32_t numExitingEdges = ExitingEdges.size()) { |
Cong Hou | e93b8e1 | 2015-12-22 18:56:14 +0000 | [diff] [blame] | 454 | auto Prob = Probs[2] / numExitingEdges; |
| 455 | for (unsigned SuccIdx : ExitingEdges) |
| 456 | setEdgeProbability(BB, SuccIdx, Prob); |
Andrew Trick | 49371f3 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 457 | } |
Jakub Staszak | d07b2e1 | 2011-07-28 21:45:07 +0000 | [diff] [blame] | 458 | |
| 459 | return true; |
Andrew Trick | 49371f3 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 460 | } |
| 461 | |
John Brawn | da4a68a | 2017-06-08 09:44:40 +0000 | [diff] [blame^] | 462 | bool BranchProbabilityInfo::calcZeroHeuristics(const BasicBlock *BB, |
| 463 | const TargetLibraryInfo *TLI) { |
Mehdi Amini | a797877 | 2016-04-07 21:59:28 +0000 | [diff] [blame] | 464 | const BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator()); |
Jakub Staszak | 17af66a | 2011-07-31 03:27:24 +0000 | [diff] [blame] | 465 | if (!BI || !BI->isConditional()) |
| 466 | return false; |
| 467 | |
| 468 | Value *Cond = BI->getCondition(); |
| 469 | ICmpInst *CI = dyn_cast<ICmpInst>(Cond); |
| 470 | if (!CI) |
| 471 | return false; |
| 472 | |
Jakub Staszak | 17af66a | 2011-07-31 03:27:24 +0000 | [diff] [blame] | 473 | Value *RHS = CI->getOperand(1); |
Jakub Staszak | bfb1ae2 | 2011-07-31 04:47:20 +0000 | [diff] [blame] | 474 | ConstantInt *CV = dyn_cast<ConstantInt>(RHS); |
Benjamin Kramer | 0ca1ad0 | 2011-09-04 23:53:04 +0000 | [diff] [blame] | 475 | if (!CV) |
Jakub Staszak | 17af66a | 2011-07-31 03:27:24 +0000 | [diff] [blame] | 476 | return false; |
| 477 | |
Daniel Jasper | a73f3d5 | 2015-04-15 06:24:07 +0000 | [diff] [blame] | 478 | // If the LHS is the result of AND'ing a value with a single bit bitmask, |
| 479 | // we don't have information about probabilities. |
| 480 | if (Instruction *LHS = dyn_cast<Instruction>(CI->getOperand(0))) |
| 481 | if (LHS->getOpcode() == Instruction::And) |
| 482 | if (ConstantInt *AndRHS = dyn_cast<ConstantInt>(LHS->getOperand(1))) |
| 483 | if (AndRHS->getUniqueInteger().isPowerOf2()) |
| 484 | return false; |
| 485 | |
John Brawn | da4a68a | 2017-06-08 09:44:40 +0000 | [diff] [blame^] | 486 | // Check if the LHS is the return value of a library function |
| 487 | LibFunc Func = NumLibFuncs; |
| 488 | if (TLI) |
| 489 | if (CallInst *Call = dyn_cast<CallInst>(CI->getOperand(0))) |
| 490 | if (Function *CalledFn = Call->getCalledFunction()) |
| 491 | TLI->getLibFunc(*CalledFn, Func); |
| 492 | |
Jakub Staszak | 17af66a | 2011-07-31 03:27:24 +0000 | [diff] [blame] | 493 | bool isProb; |
John Brawn | da4a68a | 2017-06-08 09:44:40 +0000 | [diff] [blame^] | 494 | if (Func == LibFunc_strcasecmp || |
| 495 | Func == LibFunc_strcmp || |
| 496 | Func == LibFunc_strncasecmp || |
| 497 | Func == LibFunc_strncmp || |
| 498 | Func == LibFunc_memcmp) { |
| 499 | // strcmp and similar functions return zero, negative, or positive, if the |
| 500 | // first string is equal, less, or greater than the second. We consider it |
| 501 | // likely that the strings are not equal, so a comparison with zero is |
| 502 | // probably false, but also a comparison with any other number is also |
| 503 | // probably false given that what exactly is returned for nonzero values is |
| 504 | // not specified. Any kind of comparison other than equality we know |
| 505 | // nothing about. |
| 506 | switch (CI->getPredicate()) { |
| 507 | case CmpInst::ICMP_EQ: |
| 508 | isProb = false; |
| 509 | break; |
| 510 | case CmpInst::ICMP_NE: |
| 511 | isProb = true; |
| 512 | break; |
| 513 | default: |
| 514 | return false; |
| 515 | } |
| 516 | } else if (CV->isZero()) { |
Benjamin Kramer | 0ca1ad0 | 2011-09-04 23:53:04 +0000 | [diff] [blame] | 517 | switch (CI->getPredicate()) { |
| 518 | case CmpInst::ICMP_EQ: |
| 519 | // X == 0 -> Unlikely |
| 520 | isProb = false; |
| 521 | break; |
| 522 | case CmpInst::ICMP_NE: |
| 523 | // X != 0 -> Likely |
| 524 | isProb = true; |
| 525 | break; |
| 526 | case CmpInst::ICMP_SLT: |
| 527 | // X < 0 -> Unlikely |
| 528 | isProb = false; |
| 529 | break; |
| 530 | case CmpInst::ICMP_SGT: |
| 531 | // X > 0 -> Likely |
| 532 | isProb = true; |
| 533 | break; |
| 534 | default: |
| 535 | return false; |
| 536 | } |
| 537 | } else if (CV->isOne() && CI->getPredicate() == CmpInst::ICMP_SLT) { |
| 538 | // InstCombine canonicalizes X <= 0 into X < 1. |
| 539 | // X <= 0 -> Unlikely |
Jakub Staszak | 17af66a | 2011-07-31 03:27:24 +0000 | [diff] [blame] | 540 | isProb = false; |
Hal Finkel | 4d94930 | 2013-11-01 10:58:22 +0000 | [diff] [blame] | 541 | } else if (CV->isAllOnesValue()) { |
| 542 | switch (CI->getPredicate()) { |
| 543 | case CmpInst::ICMP_EQ: |
| 544 | // X == -1 -> Unlikely |
| 545 | isProb = false; |
| 546 | break; |
| 547 | case CmpInst::ICMP_NE: |
| 548 | // X != -1 -> Likely |
| 549 | isProb = true; |
| 550 | break; |
| 551 | case CmpInst::ICMP_SGT: |
| 552 | // InstCombine canonicalizes X >= 0 into X > -1. |
| 553 | // X >= 0 -> Likely |
| 554 | isProb = true; |
| 555 | break; |
| 556 | default: |
| 557 | return false; |
| 558 | } |
Benjamin Kramer | 0ca1ad0 | 2011-09-04 23:53:04 +0000 | [diff] [blame] | 559 | } else { |
Jakub Staszak | 17af66a | 2011-07-31 03:27:24 +0000 | [diff] [blame] | 560 | return false; |
Benjamin Kramer | 0ca1ad0 | 2011-09-04 23:53:04 +0000 | [diff] [blame] | 561 | } |
Jakub Staszak | 17af66a | 2011-07-31 03:27:24 +0000 | [diff] [blame] | 562 | |
Manman Ren | cf10446 | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 563 | unsigned TakenIdx = 0, NonTakenIdx = 1; |
Jakub Staszak | 17af66a | 2011-07-31 03:27:24 +0000 | [diff] [blame] | 564 | |
| 565 | if (!isProb) |
Manman Ren | cf10446 | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 566 | std::swap(TakenIdx, NonTakenIdx); |
Jakub Staszak | 17af66a | 2011-07-31 03:27:24 +0000 | [diff] [blame] | 567 | |
Cong Hou | e93b8e1 | 2015-12-22 18:56:14 +0000 | [diff] [blame] | 568 | BranchProbability TakenProb(ZH_TAKEN_WEIGHT, |
| 569 | ZH_TAKEN_WEIGHT + ZH_NONTAKEN_WEIGHT); |
| 570 | setEdgeProbability(BB, TakenIdx, TakenProb); |
| 571 | setEdgeProbability(BB, NonTakenIdx, TakenProb.getCompl()); |
Jakub Staszak | 17af66a | 2011-07-31 03:27:24 +0000 | [diff] [blame] | 572 | return true; |
| 573 | } |
| 574 | |
Mehdi Amini | a797877 | 2016-04-07 21:59:28 +0000 | [diff] [blame] | 575 | bool BranchProbabilityInfo::calcFloatingPointHeuristics(const BasicBlock *BB) { |
| 576 | const BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator()); |
Benjamin Kramer | 1e731a1 | 2011-10-21 20:12:47 +0000 | [diff] [blame] | 577 | if (!BI || !BI->isConditional()) |
| 578 | return false; |
| 579 | |
| 580 | Value *Cond = BI->getCondition(); |
| 581 | FCmpInst *FCmp = dyn_cast<FCmpInst>(Cond); |
Benjamin Kramer | 606a50a | 2011-10-21 21:13:47 +0000 | [diff] [blame] | 582 | if (!FCmp) |
Benjamin Kramer | 1e731a1 | 2011-10-21 20:12:47 +0000 | [diff] [blame] | 583 | return false; |
| 584 | |
Benjamin Kramer | 606a50a | 2011-10-21 21:13:47 +0000 | [diff] [blame] | 585 | bool isProb; |
| 586 | if (FCmp->isEquality()) { |
| 587 | // f1 == f2 -> Unlikely |
| 588 | // f1 != f2 -> Likely |
| 589 | isProb = !FCmp->isTrueWhenEqual(); |
| 590 | } else if (FCmp->getPredicate() == FCmpInst::FCMP_ORD) { |
| 591 | // !isnan -> Likely |
| 592 | isProb = true; |
| 593 | } else if (FCmp->getPredicate() == FCmpInst::FCMP_UNO) { |
| 594 | // isnan -> Unlikely |
| 595 | isProb = false; |
| 596 | } else { |
| 597 | return false; |
| 598 | } |
| 599 | |
Manman Ren | cf10446 | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 600 | unsigned TakenIdx = 0, NonTakenIdx = 1; |
Benjamin Kramer | 1e731a1 | 2011-10-21 20:12:47 +0000 | [diff] [blame] | 601 | |
Benjamin Kramer | 606a50a | 2011-10-21 21:13:47 +0000 | [diff] [blame] | 602 | if (!isProb) |
Manman Ren | cf10446 | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 603 | std::swap(TakenIdx, NonTakenIdx); |
Benjamin Kramer | 1e731a1 | 2011-10-21 20:12:47 +0000 | [diff] [blame] | 604 | |
Cong Hou | e93b8e1 | 2015-12-22 18:56:14 +0000 | [diff] [blame] | 605 | BranchProbability TakenProb(FPH_TAKEN_WEIGHT, |
| 606 | FPH_TAKEN_WEIGHT + FPH_NONTAKEN_WEIGHT); |
| 607 | setEdgeProbability(BB, TakenIdx, TakenProb); |
| 608 | setEdgeProbability(BB, NonTakenIdx, TakenProb.getCompl()); |
Benjamin Kramer | 1e731a1 | 2011-10-21 20:12:47 +0000 | [diff] [blame] | 609 | return true; |
| 610 | } |
Jakub Staszak | 17af66a | 2011-07-31 03:27:24 +0000 | [diff] [blame] | 611 | |
Mehdi Amini | a797877 | 2016-04-07 21:59:28 +0000 | [diff] [blame] | 612 | bool BranchProbabilityInfo::calcInvokeHeuristics(const BasicBlock *BB) { |
| 613 | const InvokeInst *II = dyn_cast<InvokeInst>(BB->getTerminator()); |
Bill Wendling | e1c5426 | 2012-08-15 12:22:35 +0000 | [diff] [blame] | 614 | if (!II) |
| 615 | return false; |
| 616 | |
Cong Hou | e93b8e1 | 2015-12-22 18:56:14 +0000 | [diff] [blame] | 617 | BranchProbability TakenProb(IH_TAKEN_WEIGHT, |
| 618 | IH_TAKEN_WEIGHT + IH_NONTAKEN_WEIGHT); |
| 619 | setEdgeProbability(BB, 0 /*Index for Normal*/, TakenProb); |
| 620 | setEdgeProbability(BB, 1 /*Index for Unwind*/, TakenProb.getCompl()); |
Bill Wendling | e1c5426 | 2012-08-15 12:22:35 +0000 | [diff] [blame] | 621 | return true; |
| 622 | } |
| 623 | |
Pete Cooper | b9d2e34 | 2015-05-28 19:43:06 +0000 | [diff] [blame] | 624 | void BranchProbabilityInfo::releaseMemory() { |
Cong Hou | e93b8e1 | 2015-12-22 18:56:14 +0000 | [diff] [blame] | 625 | Probs.clear(); |
Pete Cooper | b9d2e34 | 2015-05-28 19:43:06 +0000 | [diff] [blame] | 626 | } |
| 627 | |
Cong Hou | ab23bfb | 2015-07-15 22:48:29 +0000 | [diff] [blame] | 628 | void BranchProbabilityInfo::print(raw_ostream &OS) const { |
Chandler Carruth | 1c8ace0 | 2011-10-23 21:21:50 +0000 | [diff] [blame] | 629 | OS << "---- Branch Probabilities ----\n"; |
| 630 | // We print the probabilities from the last function the analysis ran over, |
| 631 | // or the function it is currently running over. |
| 632 | assert(LastF && "Cannot print prior to running over a function"); |
Duncan P. N. Exon Smith | 5a82c91 | 2015-10-10 00:53:03 +0000 | [diff] [blame] | 633 | for (const auto &BI : *LastF) { |
| 634 | for (succ_const_iterator SI = succ_begin(&BI), SE = succ_end(&BI); SI != SE; |
| 635 | ++SI) { |
| 636 | printEdgeProbability(OS << " ", &BI, *SI); |
Duncan P. N. Exon Smith | 6c99015 | 2014-07-21 17:06:51 +0000 | [diff] [blame] | 637 | } |
| 638 | } |
Chandler Carruth | 1c8ace0 | 2011-10-23 21:21:50 +0000 | [diff] [blame] | 639 | } |
| 640 | |
Jakub Staszak | efd94c8 | 2011-07-29 19:30:00 +0000 | [diff] [blame] | 641 | bool BranchProbabilityInfo:: |
| 642 | isEdgeHot(const BasicBlock *Src, const BasicBlock *Dst) const { |
Andrew Trick | 3d4e64b | 2011-06-11 01:05:22 +0000 | [diff] [blame] | 643 | // Hot probability is at least 4/5 = 80% |
Benjamin Kramer | 929f53f | 2011-10-23 11:19:14 +0000 | [diff] [blame] | 644 | // FIXME: Compare against a static "hot" BranchProbability. |
| 645 | return getEdgeProbability(Src, Dst) > BranchProbability(4, 5); |
Andrew Trick | 49371f3 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 646 | } |
| 647 | |
Mehdi Amini | a797877 | 2016-04-07 21:59:28 +0000 | [diff] [blame] | 648 | const BasicBlock * |
| 649 | BranchProbabilityInfo::getHotSucc(const BasicBlock *BB) const { |
Cong Hou | e93b8e1 | 2015-12-22 18:56:14 +0000 | [diff] [blame] | 650 | auto MaxProb = BranchProbability::getZero(); |
Mehdi Amini | a797877 | 2016-04-07 21:59:28 +0000 | [diff] [blame] | 651 | const BasicBlock *MaxSucc = nullptr; |
Andrew Trick | 49371f3 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 652 | |
Mehdi Amini | a797877 | 2016-04-07 21:59:28 +0000 | [diff] [blame] | 653 | for (succ_const_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I) { |
| 654 | const BasicBlock *Succ = *I; |
Cong Hou | e93b8e1 | 2015-12-22 18:56:14 +0000 | [diff] [blame] | 655 | auto Prob = getEdgeProbability(BB, Succ); |
| 656 | if (Prob > MaxProb) { |
| 657 | MaxProb = Prob; |
Andrew Trick | 49371f3 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 658 | MaxSucc = Succ; |
| 659 | } |
| 660 | } |
| 661 | |
Benjamin Kramer | 929f53f | 2011-10-23 11:19:14 +0000 | [diff] [blame] | 662 | // Hot probability is at least 4/5 = 80% |
Cong Hou | e93b8e1 | 2015-12-22 18:56:14 +0000 | [diff] [blame] | 663 | if (MaxProb > BranchProbability(4, 5)) |
Andrew Trick | 49371f3 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 664 | return MaxSucc; |
| 665 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 666 | return nullptr; |
Andrew Trick | 49371f3 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 667 | } |
| 668 | |
Cong Hou | e93b8e1 | 2015-12-22 18:56:14 +0000 | [diff] [blame] | 669 | /// Get the raw edge probability for the edge. If can't find it, return a |
| 670 | /// default probability 1/N where N is the number of successors. Here an edge is |
| 671 | /// specified using PredBlock and an |
| 672 | /// index to the successors. |
| 673 | BranchProbability |
| 674 | BranchProbabilityInfo::getEdgeProbability(const BasicBlock *Src, |
| 675 | unsigned IndexInSuccessors) const { |
| 676 | auto I = Probs.find(std::make_pair(Src, IndexInSuccessors)); |
Andrew Trick | 49371f3 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 677 | |
Cong Hou | e93b8e1 | 2015-12-22 18:56:14 +0000 | [diff] [blame] | 678 | if (I != Probs.end()) |
Andrew Trick | 49371f3 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 679 | return I->second; |
| 680 | |
Cong Hou | e93b8e1 | 2015-12-22 18:56:14 +0000 | [diff] [blame] | 681 | return {1, |
| 682 | static_cast<uint32_t>(std::distance(succ_begin(Src), succ_end(Src)))}; |
Andrew Trick | 49371f3 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 683 | } |
| 684 | |
Cong Hou | d97c100 | 2015-12-01 05:29:22 +0000 | [diff] [blame] | 685 | BranchProbability |
| 686 | BranchProbabilityInfo::getEdgeProbability(const BasicBlock *Src, |
| 687 | succ_const_iterator Dst) const { |
| 688 | return getEdgeProbability(Src, Dst.getSuccessorIndex()); |
| 689 | } |
| 690 | |
Cong Hou | e93b8e1 | 2015-12-22 18:56:14 +0000 | [diff] [blame] | 691 | /// Get the raw edge probability calculated for the block pair. This returns the |
| 692 | /// sum of all raw edge probabilities from Src to Dst. |
| 693 | BranchProbability |
| 694 | BranchProbabilityInfo::getEdgeProbability(const BasicBlock *Src, |
| 695 | const BasicBlock *Dst) const { |
| 696 | auto Prob = BranchProbability::getZero(); |
| 697 | bool FoundProb = false; |
| 698 | for (succ_const_iterator I = succ_begin(Src), E = succ_end(Src); I != E; ++I) |
| 699 | if (*I == Dst) { |
| 700 | auto MapI = Probs.find(std::make_pair(Src, I.getSuccessorIndex())); |
| 701 | if (MapI != Probs.end()) { |
| 702 | FoundProb = true; |
| 703 | Prob += MapI->second; |
| 704 | } |
| 705 | } |
| 706 | uint32_t succ_num = std::distance(succ_begin(Src), succ_end(Src)); |
| 707 | return FoundProb ? Prob : BranchProbability(1, succ_num); |
| 708 | } |
| 709 | |
| 710 | /// Set the edge probability for a given edge specified by PredBlock and an |
| 711 | /// index to the successors. |
| 712 | void BranchProbabilityInfo::setEdgeProbability(const BasicBlock *Src, |
| 713 | unsigned IndexInSuccessors, |
| 714 | BranchProbability Prob) { |
| 715 | Probs[std::make_pair(Src, IndexInSuccessors)] = Prob; |
Igor Laevsky | ee40d1e | 2016-07-15 14:31:16 +0000 | [diff] [blame] | 716 | Handles.insert(BasicBlockCallbackVH(Src, this)); |
Cong Hou | e93b8e1 | 2015-12-22 18:56:14 +0000 | [diff] [blame] | 717 | DEBUG(dbgs() << "set edge " << Src->getName() << " -> " << IndexInSuccessors |
| 718 | << " successor probability to " << Prob << "\n"); |
| 719 | } |
| 720 | |
Andrew Trick | 49371f3 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 721 | raw_ostream & |
Chandler Carruth | 1c8ace0 | 2011-10-23 21:21:50 +0000 | [diff] [blame] | 722 | BranchProbabilityInfo::printEdgeProbability(raw_ostream &OS, |
| 723 | const BasicBlock *Src, |
| 724 | const BasicBlock *Dst) const { |
Andrew Trick | 49371f3 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 725 | |
Jakub Staszak | 12a43bd | 2011-06-16 20:22:37 +0000 | [diff] [blame] | 726 | const BranchProbability Prob = getEdgeProbability(Src, Dst); |
Benjamin Kramer | 1f97a5a | 2011-11-15 16:27:03 +0000 | [diff] [blame] | 727 | OS << "edge " << Src->getName() << " -> " << Dst->getName() |
Andrew Trick | 3d4e64b | 2011-06-11 01:05:22 +0000 | [diff] [blame] | 728 | << " probability is " << Prob |
| 729 | << (isEdgeHot(Src, Dst) ? " [HOT edge]\n" : "\n"); |
Andrew Trick | 49371f3 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 730 | |
| 731 | return OS; |
| 732 | } |
Cong Hou | ab23bfb | 2015-07-15 22:48:29 +0000 | [diff] [blame] | 733 | |
Igor Laevsky | ee40d1e | 2016-07-15 14:31:16 +0000 | [diff] [blame] | 734 | void BranchProbabilityInfo::eraseBlock(const BasicBlock *BB) { |
| 735 | for (auto I = Probs.begin(), E = Probs.end(); I != E; ++I) { |
| 736 | auto Key = I->first; |
| 737 | if (Key.first == BB) |
| 738 | Probs.erase(Key); |
| 739 | } |
| 740 | } |
| 741 | |
John Brawn | da4a68a | 2017-06-08 09:44:40 +0000 | [diff] [blame^] | 742 | void BranchProbabilityInfo::calculate(const Function &F, const LoopInfo &LI, |
| 743 | const TargetLibraryInfo *TLI) { |
Cong Hou | ab23bfb | 2015-07-15 22:48:29 +0000 | [diff] [blame] | 744 | DEBUG(dbgs() << "---- Branch Probability Info : " << F.getName() |
| 745 | << " ----\n\n"); |
| 746 | LastF = &F; // Store the last function we ran on for printing. |
| 747 | assert(PostDominatedByUnreachable.empty()); |
| 748 | assert(PostDominatedByColdCall.empty()); |
| 749 | |
| 750 | // Walk the basic blocks in post-order so that we can build up state about |
| 751 | // the successors of a block iteratively. |
| 752 | for (auto BB : post_order(&F.getEntryBlock())) { |
| 753 | DEBUG(dbgs() << "Computing probabilities for " << BB->getName() << "\n"); |
Serguei Katkov | ecebc3d | 2017-04-12 05:42:14 +0000 | [diff] [blame] | 754 | updatePostDominatedByUnreachable(BB); |
| 755 | updatePostDominatedByColdCall(BB); |
Serguei Katkov | 11d9c4f | 2017-04-17 06:39:47 +0000 | [diff] [blame] | 756 | // If there is no at least two successors, no sense to set probability. |
| 757 | if (BB->getTerminator()->getNumSuccessors() < 2) |
| 758 | continue; |
Cong Hou | ab23bfb | 2015-07-15 22:48:29 +0000 | [diff] [blame] | 759 | if (calcMetadataWeights(BB)) |
| 760 | continue; |
Serguei Katkov | 2616bbb | 2017-04-17 04:33:04 +0000 | [diff] [blame] | 761 | if (calcUnreachableHeuristics(BB)) |
| 762 | continue; |
Cong Hou | ab23bfb | 2015-07-15 22:48:29 +0000 | [diff] [blame] | 763 | if (calcColdCallHeuristics(BB)) |
| 764 | continue; |
| 765 | if (calcLoopBranchHeuristics(BB, LI)) |
| 766 | continue; |
| 767 | if (calcPointerHeuristics(BB)) |
| 768 | continue; |
John Brawn | da4a68a | 2017-06-08 09:44:40 +0000 | [diff] [blame^] | 769 | if (calcZeroHeuristics(BB, TLI)) |
Cong Hou | ab23bfb | 2015-07-15 22:48:29 +0000 | [diff] [blame] | 770 | continue; |
| 771 | if (calcFloatingPointHeuristics(BB)) |
| 772 | continue; |
| 773 | calcInvokeHeuristics(BB); |
| 774 | } |
| 775 | |
| 776 | PostDominatedByUnreachable.clear(); |
| 777 | PostDominatedByColdCall.clear(); |
| 778 | } |
| 779 | |
| 780 | void BranchProbabilityInfoWrapperPass::getAnalysisUsage( |
| 781 | AnalysisUsage &AU) const { |
| 782 | AU.addRequired<LoopInfoWrapperPass>(); |
John Brawn | da4a68a | 2017-06-08 09:44:40 +0000 | [diff] [blame^] | 783 | AU.addRequired<TargetLibraryInfoWrapperPass>(); |
Cong Hou | ab23bfb | 2015-07-15 22:48:29 +0000 | [diff] [blame] | 784 | AU.setPreservesAll(); |
| 785 | } |
| 786 | |
| 787 | bool BranchProbabilityInfoWrapperPass::runOnFunction(Function &F) { |
| 788 | const LoopInfo &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo(); |
John Brawn | da4a68a | 2017-06-08 09:44:40 +0000 | [diff] [blame^] | 789 | const TargetLibraryInfo &TLI = getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(); |
| 790 | BPI.calculate(F, LI, &TLI); |
Cong Hou | ab23bfb | 2015-07-15 22:48:29 +0000 | [diff] [blame] | 791 | return false; |
| 792 | } |
| 793 | |
| 794 | void BranchProbabilityInfoWrapperPass::releaseMemory() { BPI.releaseMemory(); } |
| 795 | |
| 796 | void BranchProbabilityInfoWrapperPass::print(raw_ostream &OS, |
| 797 | const Module *) const { |
| 798 | BPI.print(OS); |
| 799 | } |
Xinliang David Li | 6e5dd41 | 2016-05-05 02:59:57 +0000 | [diff] [blame] | 800 | |
Chandler Carruth | dab4eae | 2016-11-23 17:53:26 +0000 | [diff] [blame] | 801 | AnalysisKey BranchProbabilityAnalysis::Key; |
Xinliang David Li | 6e5dd41 | 2016-05-05 02:59:57 +0000 | [diff] [blame] | 802 | BranchProbabilityInfo |
Sean Silva | 36e0d01 | 2016-08-09 00:28:15 +0000 | [diff] [blame] | 803 | BranchProbabilityAnalysis::run(Function &F, FunctionAnalysisManager &AM) { |
Xinliang David Li | 6e5dd41 | 2016-05-05 02:59:57 +0000 | [diff] [blame] | 804 | BranchProbabilityInfo BPI; |
John Brawn | da4a68a | 2017-06-08 09:44:40 +0000 | [diff] [blame^] | 805 | BPI.calculate(F, AM.getResult<LoopAnalysis>(F), &AM.getResult<TargetLibraryAnalysis>(F)); |
Xinliang David Li | 6e5dd41 | 2016-05-05 02:59:57 +0000 | [diff] [blame] | 806 | return BPI; |
| 807 | } |
| 808 | |
| 809 | PreservedAnalyses |
Sean Silva | 36e0d01 | 2016-08-09 00:28:15 +0000 | [diff] [blame] | 810 | BranchProbabilityPrinterPass::run(Function &F, FunctionAnalysisManager &AM) { |
Xinliang David Li | 6e5dd41 | 2016-05-05 02:59:57 +0000 | [diff] [blame] | 811 | OS << "Printing analysis results of BPI for function " |
| 812 | << "'" << F.getName() << "':" |
| 813 | << "\n"; |
| 814 | AM.getResult<BranchProbabilityAnalysis>(F).print(OS); |
| 815 | return PreservedAnalyses::all(); |
| 816 | } |