Eugene Zelenko | 38c02bc | 2017-07-21 21:37:46 +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" |
Geoff Berry | eed6531 | 2017-11-01 15:16:50 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/SCCIterator.h" |
Eugene Zelenko | 38c02bc | 2017-07-21 21:37:46 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/STLExtras.h" |
| 18 | #include "llvm/ADT/SmallVector.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 19 | #include "llvm/Analysis/LoopInfo.h" |
John Brawn | da4a68a | 2017-06-08 09:44:40 +0000 | [diff] [blame] | 20 | #include "llvm/Analysis/TargetLibraryInfo.h" |
Eugene Zelenko | 38c02bc | 2017-07-21 21:37:46 +0000 | [diff] [blame] | 21 | #include "llvm/IR/Attributes.h" |
| 22 | #include "llvm/IR/BasicBlock.h" |
Chandler Carruth | 1305dc3 | 2014-03-04 11:45:46 +0000 | [diff] [blame] | 23 | #include "llvm/IR/CFG.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 24 | #include "llvm/IR/Constants.h" |
| 25 | #include "llvm/IR/Function.h" |
Eugene Zelenko | 38c02bc | 2017-07-21 21:37:46 +0000 | [diff] [blame] | 26 | #include "llvm/IR/InstrTypes.h" |
| 27 | #include "llvm/IR/Instruction.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 28 | #include "llvm/IR/Instructions.h" |
| 29 | #include "llvm/IR/LLVMContext.h" |
| 30 | #include "llvm/IR/Metadata.h" |
Eugene Zelenko | 38c02bc | 2017-07-21 21:37:46 +0000 | [diff] [blame] | 31 | #include "llvm/IR/PassManager.h" |
| 32 | #include "llvm/IR/Type.h" |
| 33 | #include "llvm/IR/Value.h" |
| 34 | #include "llvm/Pass.h" |
| 35 | #include "llvm/Support/BranchProbability.h" |
| 36 | #include "llvm/Support/Casting.h" |
Andrew Trick | 3d4e64b | 2011-06-11 01:05:22 +0000 | [diff] [blame] | 37 | #include "llvm/Support/Debug.h" |
Benjamin Kramer | 16132e6 | 2015-03-23 18:07:13 +0000 | [diff] [blame] | 38 | #include "llvm/Support/raw_ostream.h" |
Eugene Zelenko | 38c02bc | 2017-07-21 21:37:46 +0000 | [diff] [blame] | 39 | #include <cassert> |
| 40 | #include <cstdint> |
| 41 | #include <iterator> |
| 42 | #include <utility> |
Andrew Trick | 49371f3 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 43 | |
| 44 | using namespace llvm; |
| 45 | |
Chandler Carruth | f1221bd | 2014-04-22 02:48:03 +0000 | [diff] [blame] | 46 | #define DEBUG_TYPE "branch-prob" |
| 47 | |
Hiroshi Yamauchi | 63e17eb | 2017-08-26 00:31:00 +0000 | [diff] [blame] | 48 | static cl::opt<bool> PrintBranchProb( |
| 49 | "print-bpi", cl::init(false), cl::Hidden, |
| 50 | cl::desc("Print the branch probability info.")); |
| 51 | |
| 52 | cl::opt<std::string> PrintBranchProbFuncName( |
| 53 | "print-bpi-func-name", cl::Hidden, |
| 54 | cl::desc("The option to specify the name of the function " |
| 55 | "whose branch probability info is printed.")); |
| 56 | |
Cong Hou | ab23bfb | 2015-07-15 22:48:29 +0000 | [diff] [blame] | 57 | INITIALIZE_PASS_BEGIN(BranchProbabilityInfoWrapperPass, "branch-prob", |
Andrew Trick | 49371f3 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 58 | "Branch Probability Analysis", false, true) |
Chandler Carruth | 4f8f307 | 2015-01-17 14:16:18 +0000 | [diff] [blame] | 59 | INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass) |
John Brawn | da4a68a | 2017-06-08 09:44:40 +0000 | [diff] [blame] | 60 | INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass) |
Cong Hou | ab23bfb | 2015-07-15 22:48:29 +0000 | [diff] [blame] | 61 | INITIALIZE_PASS_END(BranchProbabilityInfoWrapperPass, "branch-prob", |
Andrew Trick | 49371f3 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 62 | "Branch Probability Analysis", false, true) |
| 63 | |
Cong Hou | ab23bfb | 2015-07-15 22:48:29 +0000 | [diff] [blame] | 64 | char BranchProbabilityInfoWrapperPass::ID = 0; |
Andrew Trick | 49371f3 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 65 | |
Chandler Carruth | 7a0094a | 2011-10-24 01:40:45 +0000 | [diff] [blame] | 66 | // Weights are for internal use only. They are used by heuristics to help to |
| 67 | // estimate edges' probability. Example: |
| 68 | // |
| 69 | // Using "Loop Branch Heuristics" we predict weights of edges for the |
| 70 | // block BB2. |
| 71 | // ... |
| 72 | // | |
| 73 | // V |
| 74 | // BB1<-+ |
| 75 | // | | |
| 76 | // | | (Weight = 124) |
| 77 | // V | |
| 78 | // BB2--+ |
| 79 | // | |
| 80 | // | (Weight = 4) |
| 81 | // V |
| 82 | // BB3 |
| 83 | // |
| 84 | // Probability of the edge BB2->BB1 = 124 / (124 + 4) = 0.96875 |
| 85 | // Probability of the edge BB2->BB3 = 4 / (124 + 4) = 0.03125 |
| 86 | static const uint32_t LBH_TAKEN_WEIGHT = 124; |
| 87 | static const uint32_t LBH_NONTAKEN_WEIGHT = 4; |
John Brawn | 29bbed3 | 2018-02-23 17:17:31 +0000 | [diff] [blame] | 88 | // Unlikely edges within a loop are half as likely as other edges |
| 89 | static const uint32_t LBH_UNLIKELY_WEIGHT = 62; |
Andrew Trick | 49371f3 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 90 | |
Serguei Katkov | ba831f7 | 2017-05-18 06:11:56 +0000 | [diff] [blame] | 91 | /// \brief Unreachable-terminating branch taken probability. |
Chandler Carruth | 7111f45 | 2011-10-24 12:01:08 +0000 | [diff] [blame] | 92 | /// |
Serguei Katkov | ba831f7 | 2017-05-18 06:11:56 +0000 | [diff] [blame] | 93 | /// 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] | 94 | /// (eventually) in unreachable. These are predicted as unlikely as possible. |
Serguei Katkov | ba831f7 | 2017-05-18 06:11:56 +0000 | [diff] [blame] | 95 | /// All reachable probability will equally share the remaining part. |
| 96 | static const BranchProbability UR_TAKEN_PROB = BranchProbability::getRaw(1); |
Serguei Katkov | 2616bbb | 2017-04-17 04:33:04 +0000 | [diff] [blame] | 97 | |
Diego Novillo | c639953 | 2013-05-24 12:26:52 +0000 | [diff] [blame] | 98 | /// \brief Weight for a branch taken going into a cold block. |
| 99 | /// |
| 100 | /// This is the weight for a branch taken toward a block marked |
| 101 | /// cold. A block is marked cold if it's postdominated by a |
| 102 | /// block containing a call to a cold function. Cold functions |
| 103 | /// are those marked with attribute 'cold'. |
| 104 | static const uint32_t CC_TAKEN_WEIGHT = 4; |
| 105 | |
| 106 | /// \brief Weight for a branch not-taken into a cold block. |
| 107 | /// |
| 108 | /// This is the weight for a branch not taken toward a block marked |
| 109 | /// cold. |
| 110 | static const uint32_t CC_NONTAKEN_WEIGHT = 64; |
| 111 | |
Chandler Carruth | 7a0094a | 2011-10-24 01:40:45 +0000 | [diff] [blame] | 112 | static const uint32_t PH_TAKEN_WEIGHT = 20; |
| 113 | static const uint32_t PH_NONTAKEN_WEIGHT = 12; |
Andrew Trick | 49371f3 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 114 | |
Chandler Carruth | 7a0094a | 2011-10-24 01:40:45 +0000 | [diff] [blame] | 115 | static const uint32_t ZH_TAKEN_WEIGHT = 20; |
| 116 | static const uint32_t ZH_NONTAKEN_WEIGHT = 12; |
Andrew Trick | 49371f3 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 117 | |
Chandler Carruth | 7a0094a | 2011-10-24 01:40:45 +0000 | [diff] [blame] | 118 | static const uint32_t FPH_TAKEN_WEIGHT = 20; |
| 119 | static const uint32_t FPH_NONTAKEN_WEIGHT = 12; |
Andrew Trick | 49371f3 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 120 | |
Bill Wendling | e1c5426 | 2012-08-15 12:22:35 +0000 | [diff] [blame] | 121 | /// \brief Invoke-terminating normal branch taken weight |
| 122 | /// |
| 123 | /// This is the weight for branching to the normal destination of an invoke |
| 124 | /// instruction. We expect this to happen most of the time. Set the weight to an |
| 125 | /// absurdly high value so that nested loops subsume it. |
| 126 | static const uint32_t IH_TAKEN_WEIGHT = 1024 * 1024 - 1; |
| 127 | |
| 128 | /// \brief Invoke-terminating normal branch not-taken weight. |
| 129 | /// |
| 130 | /// This is the weight for branching to the unwind destination of an invoke |
| 131 | /// instruction. This is essentially never taken. |
| 132 | static const uint32_t IH_NONTAKEN_WEIGHT = 1; |
| 133 | |
Serguei Katkov | ecebc3d | 2017-04-12 05:42:14 +0000 | [diff] [blame] | 134 | /// \brief Add \p BB to PostDominatedByUnreachable set if applicable. |
| 135 | void |
| 136 | BranchProbabilityInfo::updatePostDominatedByUnreachable(const BasicBlock *BB) { |
Mehdi Amini | a797877 | 2016-04-07 21:59:28 +0000 | [diff] [blame] | 137 | const TerminatorInst *TI = BB->getTerminator(); |
Chandler Carruth | 7111f45 | 2011-10-24 12:01:08 +0000 | [diff] [blame] | 138 | if (TI->getNumSuccessors() == 0) { |
Sanjoy Das | 432c1c3 | 2016-04-18 19:01:28 +0000 | [diff] [blame] | 139 | if (isa<UnreachableInst>(TI) || |
| 140 | // If this block is terminated by a call to |
| 141 | // @llvm.experimental.deoptimize then treat it like an unreachable since |
| 142 | // the @llvm.experimental.deoptimize call is expected to practically |
| 143 | // never execute. |
| 144 | BB->getTerminatingDeoptimizeCall()) |
Chandler Carruth | 7111f45 | 2011-10-24 12:01:08 +0000 | [diff] [blame] | 145 | PostDominatedByUnreachable.insert(BB); |
Serguei Katkov | ecebc3d | 2017-04-12 05:42:14 +0000 | [diff] [blame] | 146 | return; |
Chandler Carruth | 7111f45 | 2011-10-24 12:01:08 +0000 | [diff] [blame] | 147 | } |
| 148 | |
Serguei Katkov | ecebc3d | 2017-04-12 05:42:14 +0000 | [diff] [blame] | 149 | // If the terminator is an InvokeInst, check only the normal destination block |
| 150 | // as the unwind edge of InvokeInst is also very unlikely taken. |
| 151 | if (auto *II = dyn_cast<InvokeInst>(TI)) { |
| 152 | if (PostDominatedByUnreachable.count(II->getNormalDest())) |
| 153 | PostDominatedByUnreachable.insert(BB); |
| 154 | return; |
| 155 | } |
| 156 | |
| 157 | for (auto *I : successors(BB)) |
| 158 | // If any of successor is not post dominated then BB is also not. |
| 159 | if (!PostDominatedByUnreachable.count(I)) |
| 160 | return; |
| 161 | |
| 162 | PostDominatedByUnreachable.insert(BB); |
| 163 | } |
| 164 | |
| 165 | /// \brief Add \p BB to PostDominatedByColdCall set if applicable. |
| 166 | void |
| 167 | BranchProbabilityInfo::updatePostDominatedByColdCall(const BasicBlock *BB) { |
| 168 | assert(!PostDominatedByColdCall.count(BB)); |
| 169 | const TerminatorInst *TI = BB->getTerminator(); |
| 170 | if (TI->getNumSuccessors() == 0) |
| 171 | return; |
| 172 | |
| 173 | // If all of successor are post dominated then BB is also done. |
| 174 | if (llvm::all_of(successors(BB), [&](const BasicBlock *SuccBB) { |
| 175 | return PostDominatedByColdCall.count(SuccBB); |
| 176 | })) { |
| 177 | PostDominatedByColdCall.insert(BB); |
| 178 | return; |
| 179 | } |
| 180 | |
| 181 | // If the terminator is an InvokeInst, check only the normal destination |
| 182 | // block as the unwind edge of InvokeInst is also very unlikely taken. |
| 183 | if (auto *II = dyn_cast<InvokeInst>(TI)) |
| 184 | if (PostDominatedByColdCall.count(II->getNormalDest())) { |
| 185 | PostDominatedByColdCall.insert(BB); |
| 186 | return; |
| 187 | } |
| 188 | |
| 189 | // Otherwise, if the block itself contains a cold function, add it to the |
| 190 | // set of blocks post-dominated by a cold call. |
| 191 | for (auto &I : *BB) |
| 192 | if (const CallInst *CI = dyn_cast<CallInst>(&I)) |
| 193 | if (CI->hasFnAttr(Attribute::Cold)) { |
| 194 | PostDominatedByColdCall.insert(BB); |
| 195 | return; |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | /// \brief Calculate edge weights for successors lead to unreachable. |
| 200 | /// |
| 201 | /// Predict that a successor which leads necessarily to an |
| 202 | /// unreachable-terminated block as extremely unlikely. |
| 203 | bool BranchProbabilityInfo::calcUnreachableHeuristics(const BasicBlock *BB) { |
| 204 | const TerminatorInst *TI = BB->getTerminator(); |
Serguei Katkov | 11d9c4f | 2017-04-17 06:39:47 +0000 | [diff] [blame] | 205 | assert(TI->getNumSuccessors() > 1 && "expected more than one successor!"); |
| 206 | |
| 207 | // Return false here so that edge weights for InvokeInst could be decided |
| 208 | // in calcInvokeHeuristics(). |
| 209 | if (isa<InvokeInst>(TI)) |
Serguei Katkov | ecebc3d | 2017-04-12 05:42:14 +0000 | [diff] [blame] | 210 | return false; |
| 211 | |
Manman Ren | cf10446 | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 212 | SmallVector<unsigned, 4> UnreachableEdges; |
| 213 | SmallVector<unsigned, 4> ReachableEdges; |
Chandler Carruth | 7111f45 | 2011-10-24 12:01:08 +0000 | [diff] [blame] | 214 | |
Serguei Katkov | ecebc3d | 2017-04-12 05:42:14 +0000 | [diff] [blame] | 215 | 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] | 216 | if (PostDominatedByUnreachable.count(*I)) |
Manman Ren | cf10446 | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 217 | UnreachableEdges.push_back(I.getSuccessorIndex()); |
Chandler Carruth | 7111f45 | 2011-10-24 12:01:08 +0000 | [diff] [blame] | 218 | else |
Manman Ren | cf10446 | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 219 | ReachableEdges.push_back(I.getSuccessorIndex()); |
Chandler Carruth | 7111f45 | 2011-10-24 12:01:08 +0000 | [diff] [blame] | 220 | |
Serguei Katkov | 11d9c4f | 2017-04-17 06:39:47 +0000 | [diff] [blame] | 221 | // Skip probabilities if all were reachable. |
| 222 | if (UnreachableEdges.empty()) |
Serguei Katkov | ecebc3d | 2017-04-12 05:42:14 +0000 | [diff] [blame] | 223 | return false; |
Jun Bum Lim | a23e5f7 | 2015-12-21 22:00:51 +0000 | [diff] [blame] | 224 | |
Cong Hou | e93b8e1 | 2015-12-22 18:56:14 +0000 | [diff] [blame] | 225 | if (ReachableEdges.empty()) { |
| 226 | BranchProbability Prob(1, UnreachableEdges.size()); |
| 227 | for (unsigned SuccIdx : UnreachableEdges) |
| 228 | setEdgeProbability(BB, SuccIdx, Prob); |
Chandler Carruth | 7111f45 | 2011-10-24 12:01:08 +0000 | [diff] [blame] | 229 | return true; |
Cong Hou | e93b8e1 | 2015-12-22 18:56:14 +0000 | [diff] [blame] | 230 | } |
| 231 | |
Serguei Katkov | ba831f7 | 2017-05-18 06:11:56 +0000 | [diff] [blame] | 232 | auto UnreachableProb = UR_TAKEN_PROB; |
| 233 | auto ReachableProb = |
| 234 | (BranchProbability::getOne() - UR_TAKEN_PROB * UnreachableEdges.size()) / |
| 235 | ReachableEdges.size(); |
Cong Hou | e93b8e1 | 2015-12-22 18:56:14 +0000 | [diff] [blame] | 236 | |
| 237 | for (unsigned SuccIdx : UnreachableEdges) |
| 238 | setEdgeProbability(BB, SuccIdx, UnreachableProb); |
| 239 | for (unsigned SuccIdx : ReachableEdges) |
| 240 | setEdgeProbability(BB, SuccIdx, ReachableProb); |
Chandler Carruth | 7111f45 | 2011-10-24 12:01:08 +0000 | [diff] [blame] | 241 | |
| 242 | return true; |
| 243 | } |
| 244 | |
Chandler Carruth | d27a7a9 | 2011-10-19 10:30:30 +0000 | [diff] [blame] | 245 | // Propagate existing explicit probabilities from either profile data or |
Serguei Katkov | 2616bbb | 2017-04-17 04:33:04 +0000 | [diff] [blame] | 246 | // 'expect' intrinsic processing. Examine metadata against unreachable |
| 247 | // heuristic. The probability of the edge coming to unreachable block is |
| 248 | // set to min of metadata and unreachable heuristic. |
Mehdi Amini | a797877 | 2016-04-07 21:59:28 +0000 | [diff] [blame] | 249 | bool BranchProbabilityInfo::calcMetadataWeights(const BasicBlock *BB) { |
| 250 | const TerminatorInst *TI = BB->getTerminator(); |
Serguei Katkov | 11d9c4f | 2017-04-17 06:39:47 +0000 | [diff] [blame] | 251 | assert(TI->getNumSuccessors() > 1 && "expected more than one successor!"); |
Rong Xu | 15848e5 | 2017-08-23 21:36:02 +0000 | [diff] [blame] | 252 | if (!(isa<BranchInst>(TI) || isa<SwitchInst>(TI) || isa<IndirectBrInst>(TI))) |
Chandler Carruth | d27a7a9 | 2011-10-19 10:30:30 +0000 | [diff] [blame] | 253 | return false; |
| 254 | |
Duncan P. N. Exon Smith | de36e80 | 2014-11-11 21:30:22 +0000 | [diff] [blame] | 255 | MDNode *WeightsNode = TI->getMetadata(LLVMContext::MD_prof); |
Chandler Carruth | deac50c | 2011-10-19 10:32:19 +0000 | [diff] [blame] | 256 | if (!WeightsNode) |
Chandler Carruth | d27a7a9 | 2011-10-19 10:30:30 +0000 | [diff] [blame] | 257 | return false; |
| 258 | |
Diego Novillo | de5b801 | 2015-05-07 17:22:06 +0000 | [diff] [blame] | 259 | // Check that the number of successors is manageable. |
| 260 | assert(TI->getNumSuccessors() < UINT32_MAX && "Too many successors"); |
| 261 | |
Chandler Carruth | deac50c | 2011-10-19 10:32:19 +0000 | [diff] [blame] | 262 | // Ensure there are weights for all of the successors. Note that the first |
| 263 | // operand to the metadata node is a name, not a weight. |
| 264 | if (WeightsNode->getNumOperands() != TI->getNumSuccessors() + 1) |
Chandler Carruth | d27a7a9 | 2011-10-19 10:30:30 +0000 | [diff] [blame] | 265 | return false; |
| 266 | |
Diego Novillo | de5b801 | 2015-05-07 17:22:06 +0000 | [diff] [blame] | 267 | // Build up the final weights that will be used in a temporary buffer. |
| 268 | // Compute the sum of all weights to later decide whether they need to |
| 269 | // be scaled to fit in 32 bits. |
| 270 | uint64_t WeightSum = 0; |
Chandler Carruth | deac50c | 2011-10-19 10:32:19 +0000 | [diff] [blame] | 271 | SmallVector<uint32_t, 2> Weights; |
Serguei Katkov | 2616bbb | 2017-04-17 04:33:04 +0000 | [diff] [blame] | 272 | SmallVector<unsigned, 2> UnreachableIdxs; |
| 273 | SmallVector<unsigned, 2> ReachableIdxs; |
Chandler Carruth | deac50c | 2011-10-19 10:32:19 +0000 | [diff] [blame] | 274 | Weights.reserve(TI->getNumSuccessors()); |
| 275 | 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] | 276 | ConstantInt *Weight = |
| 277 | mdconst::dyn_extract<ConstantInt>(WeightsNode->getOperand(i)); |
Chandler Carruth | deac50c | 2011-10-19 10:32:19 +0000 | [diff] [blame] | 278 | if (!Weight) |
| 279 | return false; |
Diego Novillo | de5b801 | 2015-05-07 17:22:06 +0000 | [diff] [blame] | 280 | assert(Weight->getValue().getActiveBits() <= 32 && |
| 281 | "Too many bits for uint32_t"); |
| 282 | Weights.push_back(Weight->getZExtValue()); |
| 283 | WeightSum += Weights.back(); |
Serguei Katkov | 2616bbb | 2017-04-17 04:33:04 +0000 | [diff] [blame] | 284 | if (PostDominatedByUnreachable.count(TI->getSuccessor(i - 1))) |
| 285 | UnreachableIdxs.push_back(i - 1); |
| 286 | else |
| 287 | ReachableIdxs.push_back(i - 1); |
Chandler Carruth | deac50c | 2011-10-19 10:32:19 +0000 | [diff] [blame] | 288 | } |
| 289 | assert(Weights.size() == TI->getNumSuccessors() && "Checked above"); |
Diego Novillo | de5b801 | 2015-05-07 17:22:06 +0000 | [diff] [blame] | 290 | |
| 291 | // If the sum of weights does not fit in 32 bits, scale every weight down |
| 292 | // accordingly. |
| 293 | uint64_t ScalingFactor = |
| 294 | (WeightSum > UINT32_MAX) ? WeightSum / UINT32_MAX + 1 : 1; |
| 295 | |
Serguei Katkov | 2616bbb | 2017-04-17 04:33:04 +0000 | [diff] [blame] | 296 | if (ScalingFactor > 1) { |
| 297 | WeightSum = 0; |
| 298 | for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i) { |
| 299 | Weights[i] /= ScalingFactor; |
| 300 | WeightSum += Weights[i]; |
| 301 | } |
Diego Novillo | de5b801 | 2015-05-07 17:22:06 +0000 | [diff] [blame] | 302 | } |
Serguei Katkov | 63c9c81 | 2017-05-12 07:50:06 +0000 | [diff] [blame] | 303 | assert(WeightSum <= UINT32_MAX && |
| 304 | "Expected weights to scale down to 32 bits"); |
Cong Hou | 6a2c71a | 2015-12-22 23:45:55 +0000 | [diff] [blame] | 305 | |
Serguei Katkov | 2616bbb | 2017-04-17 04:33:04 +0000 | [diff] [blame] | 306 | if (WeightSum == 0 || ReachableIdxs.size() == 0) { |
Cong Hou | 6a2c71a | 2015-12-22 23:45:55 +0000 | [diff] [blame] | 307 | for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i) |
Serguei Katkov | 2616bbb | 2017-04-17 04:33:04 +0000 | [diff] [blame] | 308 | Weights[i] = 1; |
| 309 | WeightSum = TI->getNumSuccessors(); |
Cong Hou | 6a2c71a | 2015-12-22 23:45:55 +0000 | [diff] [blame] | 310 | } |
Cong Hou | e93b8e1 | 2015-12-22 18:56:14 +0000 | [diff] [blame] | 311 | |
Serguei Katkov | 2616bbb | 2017-04-17 04:33:04 +0000 | [diff] [blame] | 312 | // Set the probability. |
| 313 | SmallVector<BranchProbability, 2> BP; |
| 314 | for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i) |
| 315 | BP.push_back({ Weights[i], static_cast<uint32_t>(WeightSum) }); |
| 316 | |
| 317 | // Examine the metadata against unreachable heuristic. |
| 318 | // If the unreachable heuristic is more strong then we use it for this edge. |
| 319 | if (UnreachableIdxs.size() > 0 && ReachableIdxs.size() > 0) { |
| 320 | auto ToDistribute = BranchProbability::getZero(); |
Serguei Katkov | ba831f7 | 2017-05-18 06:11:56 +0000 | [diff] [blame] | 321 | auto UnreachableProb = UR_TAKEN_PROB; |
Serguei Katkov | 2616bbb | 2017-04-17 04:33:04 +0000 | [diff] [blame] | 322 | for (auto i : UnreachableIdxs) |
| 323 | if (UnreachableProb < BP[i]) { |
| 324 | ToDistribute += BP[i] - UnreachableProb; |
| 325 | BP[i] = UnreachableProb; |
| 326 | } |
| 327 | |
| 328 | // If we modified the probability of some edges then we must distribute |
| 329 | // the difference between reachable blocks. |
| 330 | if (ToDistribute > BranchProbability::getZero()) { |
| 331 | BranchProbability PerEdge = ToDistribute / ReachableIdxs.size(); |
Serguei Katkov | 63c9c81 | 2017-05-12 07:50:06 +0000 | [diff] [blame] | 332 | for (auto i : ReachableIdxs) |
Serguei Katkov | 2616bbb | 2017-04-17 04:33:04 +0000 | [diff] [blame] | 333 | BP[i] += PerEdge; |
Serguei Katkov | 2616bbb | 2017-04-17 04:33:04 +0000 | [diff] [blame] | 334 | } |
| 335 | } |
| 336 | |
| 337 | for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i) |
| 338 | setEdgeProbability(BB, i, BP[i]); |
| 339 | |
Chandler Carruth | d27a7a9 | 2011-10-19 10:30:30 +0000 | [diff] [blame] | 340 | return true; |
| 341 | } |
| 342 | |
Diego Novillo | c639953 | 2013-05-24 12:26:52 +0000 | [diff] [blame] | 343 | /// \brief Calculate edge weights for edges leading to cold blocks. |
| 344 | /// |
| 345 | /// A cold block is one post-dominated by a block with a call to a |
| 346 | /// cold function. Those edges are unlikely to be taken, so we give |
| 347 | /// them relatively low weight. |
| 348 | /// |
| 349 | /// Return true if we could compute the weights for cold edges. |
| 350 | /// Return false, otherwise. |
Mehdi Amini | a797877 | 2016-04-07 21:59:28 +0000 | [diff] [blame] | 351 | bool BranchProbabilityInfo::calcColdCallHeuristics(const BasicBlock *BB) { |
| 352 | const TerminatorInst *TI = BB->getTerminator(); |
Serguei Katkov | 11d9c4f | 2017-04-17 06:39:47 +0000 | [diff] [blame] | 353 | assert(TI->getNumSuccessors() > 1 && "expected more than one successor!"); |
| 354 | |
| 355 | // Return false here so that edge weights for InvokeInst could be decided |
| 356 | // in calcInvokeHeuristics(). |
| 357 | if (isa<InvokeInst>(TI)) |
Diego Novillo | c639953 | 2013-05-24 12:26:52 +0000 | [diff] [blame] | 358 | return false; |
| 359 | |
| 360 | // Determine which successors are post-dominated by a cold block. |
| 361 | SmallVector<unsigned, 4> ColdEdges; |
Diego Novillo | c639953 | 2013-05-24 12:26:52 +0000 | [diff] [blame] | 362 | SmallVector<unsigned, 4> NormalEdges; |
Mehdi Amini | a797877 | 2016-04-07 21:59:28 +0000 | [diff] [blame] | 363 | 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] | 364 | if (PostDominatedByColdCall.count(*I)) |
| 365 | ColdEdges.push_back(I.getSuccessorIndex()); |
| 366 | else |
| 367 | NormalEdges.push_back(I.getSuccessorIndex()); |
| 368 | |
Serguei Katkov | 11d9c4f | 2017-04-17 06:39:47 +0000 | [diff] [blame] | 369 | // Skip probabilities if no cold edges. |
| 370 | if (ColdEdges.empty()) |
Diego Novillo | c639953 | 2013-05-24 12:26:52 +0000 | [diff] [blame] | 371 | return false; |
| 372 | |
Cong Hou | e93b8e1 | 2015-12-22 18:56:14 +0000 | [diff] [blame] | 373 | if (NormalEdges.empty()) { |
| 374 | BranchProbability Prob(1, ColdEdges.size()); |
| 375 | for (unsigned SuccIdx : ColdEdges) |
| 376 | setEdgeProbability(BB, SuccIdx, Prob); |
Diego Novillo | c639953 | 2013-05-24 12:26:52 +0000 | [diff] [blame] | 377 | return true; |
Cong Hou | e93b8e1 | 2015-12-22 18:56:14 +0000 | [diff] [blame] | 378 | } |
| 379 | |
Vedant Kumar | a4bd146 | 2016-12-17 01:02:08 +0000 | [diff] [blame] | 380 | auto ColdProb = BranchProbability::getBranchProbability( |
| 381 | CC_TAKEN_WEIGHT, |
| 382 | (CC_TAKEN_WEIGHT + CC_NONTAKEN_WEIGHT) * uint64_t(ColdEdges.size())); |
| 383 | auto NormalProb = BranchProbability::getBranchProbability( |
| 384 | CC_NONTAKEN_WEIGHT, |
| 385 | (CC_TAKEN_WEIGHT + CC_NONTAKEN_WEIGHT) * uint64_t(NormalEdges.size())); |
Cong Hou | e93b8e1 | 2015-12-22 18:56:14 +0000 | [diff] [blame] | 386 | |
| 387 | for (unsigned SuccIdx : ColdEdges) |
| 388 | setEdgeProbability(BB, SuccIdx, ColdProb); |
| 389 | for (unsigned SuccIdx : NormalEdges) |
| 390 | setEdgeProbability(BB, SuccIdx, NormalProb); |
Diego Novillo | c639953 | 2013-05-24 12:26:52 +0000 | [diff] [blame] | 391 | |
| 392 | return true; |
| 393 | } |
| 394 | |
Vedant Kumar | 1a8456d | 2018-03-02 18:57:02 +0000 | [diff] [blame^] | 395 | // Calculate Edge Weights using "Pointer Heuristics". Predict a comparison |
Andrew Trick | 49371f3 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 396 | // between two pointer or pointer and NULL will fail. |
Mehdi Amini | a797877 | 2016-04-07 21:59:28 +0000 | [diff] [blame] | 397 | bool BranchProbabilityInfo::calcPointerHeuristics(const BasicBlock *BB) { |
| 398 | const BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator()); |
Andrew Trick | 49371f3 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 399 | if (!BI || !BI->isConditional()) |
Jakub Staszak | d07b2e1 | 2011-07-28 21:45:07 +0000 | [diff] [blame] | 400 | return false; |
Andrew Trick | 49371f3 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 401 | |
| 402 | Value *Cond = BI->getCondition(); |
| 403 | ICmpInst *CI = dyn_cast<ICmpInst>(Cond); |
Jakub Staszak | abb236f | 2011-07-15 20:51:06 +0000 | [diff] [blame] | 404 | if (!CI || !CI->isEquality()) |
Jakub Staszak | d07b2e1 | 2011-07-28 21:45:07 +0000 | [diff] [blame] | 405 | return false; |
Andrew Trick | 49371f3 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 406 | |
| 407 | Value *LHS = CI->getOperand(0); |
Andrew Trick | 49371f3 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 408 | |
| 409 | if (!LHS->getType()->isPointerTy()) |
Jakub Staszak | d07b2e1 | 2011-07-28 21:45:07 +0000 | [diff] [blame] | 410 | return false; |
Andrew Trick | 49371f3 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 411 | |
Nick Lewycky | 75b2053 | 2011-06-04 02:07:10 +0000 | [diff] [blame] | 412 | assert(CI->getOperand(1)->getType()->isPointerTy()); |
Andrew Trick | 49371f3 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 413 | |
Andrew Trick | 49371f3 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 414 | // p != 0 -> isProb = true |
| 415 | // p == 0 -> isProb = false |
| 416 | // p != q -> isProb = true |
| 417 | // p == q -> isProb = false; |
Manman Ren | cf10446 | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 418 | unsigned TakenIdx = 0, NonTakenIdx = 1; |
Jakub Staszak | abb236f | 2011-07-15 20:51:06 +0000 | [diff] [blame] | 419 | bool isProb = CI->getPredicate() == ICmpInst::ICMP_NE; |
Andrew Trick | 49371f3 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 420 | if (!isProb) |
Manman Ren | cf10446 | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 421 | std::swap(TakenIdx, NonTakenIdx); |
Andrew Trick | 49371f3 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 422 | |
Cong Hou | e93b8e1 | 2015-12-22 18:56:14 +0000 | [diff] [blame] | 423 | BranchProbability TakenProb(PH_TAKEN_WEIGHT, |
| 424 | PH_TAKEN_WEIGHT + PH_NONTAKEN_WEIGHT); |
| 425 | setEdgeProbability(BB, TakenIdx, TakenProb); |
| 426 | setEdgeProbability(BB, NonTakenIdx, TakenProb.getCompl()); |
Jakub Staszak | d07b2e1 | 2011-07-28 21:45:07 +0000 | [diff] [blame] | 427 | return true; |
Andrew Trick | 49371f3 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 428 | } |
| 429 | |
Geoff Berry | eed6531 | 2017-11-01 15:16:50 +0000 | [diff] [blame] | 430 | static int getSCCNum(const BasicBlock *BB, |
| 431 | const BranchProbabilityInfo::SccInfo &SccI) { |
| 432 | auto SccIt = SccI.SccNums.find(BB); |
| 433 | if (SccIt == SccI.SccNums.end()) |
| 434 | return -1; |
| 435 | return SccIt->second; |
| 436 | } |
| 437 | |
| 438 | // Consider any block that is an entry point to the SCC as a header. |
| 439 | static bool isSCCHeader(const BasicBlock *BB, int SccNum, |
| 440 | BranchProbabilityInfo::SccInfo &SccI) { |
| 441 | assert(getSCCNum(BB, SccI) == SccNum); |
| 442 | |
| 443 | // Lazily compute the set of headers for a given SCC and cache the results |
| 444 | // in the SccHeaderMap. |
| 445 | if (SccI.SccHeaders.size() <= static_cast<unsigned>(SccNum)) |
| 446 | SccI.SccHeaders.resize(SccNum + 1); |
| 447 | auto &HeaderMap = SccI.SccHeaders[SccNum]; |
| 448 | bool Inserted; |
| 449 | BranchProbabilityInfo::SccHeaderMap::iterator HeaderMapIt; |
| 450 | std::tie(HeaderMapIt, Inserted) = HeaderMap.insert(std::make_pair(BB, false)); |
| 451 | if (Inserted) { |
| 452 | bool IsHeader = llvm::any_of(make_range(pred_begin(BB), pred_end(BB)), |
| 453 | [&](const BasicBlock *Pred) { |
| 454 | return getSCCNum(Pred, SccI) != SccNum; |
| 455 | }); |
| 456 | HeaderMapIt->second = IsHeader; |
| 457 | return IsHeader; |
| 458 | } else |
| 459 | return HeaderMapIt->second; |
| 460 | } |
| 461 | |
John Brawn | 29bbed3 | 2018-02-23 17:17:31 +0000 | [diff] [blame] | 462 | // Compute the unlikely successors to the block BB in the loop L, specifically |
| 463 | // those that are unlikely because this is a loop, and add them to the |
| 464 | // UnlikelyBlocks set. |
| 465 | static void |
| 466 | computeUnlikelySuccessors(const BasicBlock *BB, Loop *L, |
| 467 | SmallPtrSetImpl<const BasicBlock*> &UnlikelyBlocks) { |
| 468 | // Sometimes in a loop we have a branch whose condition is made false by |
| 469 | // taking it. This is typically something like |
| 470 | // int n = 0; |
| 471 | // while (...) { |
| 472 | // if (++n >= MAX) { |
| 473 | // n = 0; |
| 474 | // } |
| 475 | // } |
| 476 | // In this sort of situation taking the branch means that at the very least it |
| 477 | // won't be taken again in the next iteration of the loop, so we should |
| 478 | // consider it less likely than a typical branch. |
| 479 | // |
| 480 | // We detect this by looking back through the graph of PHI nodes that sets the |
| 481 | // value that the condition depends on, and seeing if we can reach a successor |
| 482 | // block which can be determined to make the condition false. |
| 483 | // |
| 484 | // FIXME: We currently consider unlikely blocks to be half as likely as other |
| 485 | // blocks, but if we consider the example above the likelyhood is actually |
| 486 | // 1/MAX. We could therefore be more precise in how unlikely we consider |
| 487 | // blocks to be, but it would require more careful examination of the form |
| 488 | // of the comparison expression. |
| 489 | const BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator()); |
| 490 | if (!BI || !BI->isConditional()) |
| 491 | return; |
| 492 | |
| 493 | // Check if the branch is based on an instruction compared with a constant |
| 494 | CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition()); |
| 495 | if (!CI || !isa<Instruction>(CI->getOperand(0)) || |
| 496 | !isa<Constant>(CI->getOperand(1))) |
| 497 | return; |
| 498 | |
| 499 | // Either the instruction must be a PHI, or a chain of operations involving |
| 500 | // constants that ends in a PHI which we can then collapse into a single value |
| 501 | // if the PHI value is known. |
| 502 | Instruction *CmpLHS = dyn_cast<Instruction>(CI->getOperand(0)); |
| 503 | PHINode *CmpPHI = dyn_cast<PHINode>(CmpLHS); |
| 504 | Constant *CmpConst = dyn_cast<Constant>(CI->getOperand(1)); |
| 505 | // Collect the instructions until we hit a PHI |
| 506 | std::list<BinaryOperator*> InstChain; |
| 507 | while (!CmpPHI && CmpLHS && isa<BinaryOperator>(CmpLHS) && |
| 508 | isa<Constant>(CmpLHS->getOperand(1))) { |
| 509 | // Stop if the chain extends outside of the loop |
| 510 | if (!L->contains(CmpLHS)) |
| 511 | return; |
| 512 | InstChain.push_front(dyn_cast<BinaryOperator>(CmpLHS)); |
| 513 | CmpLHS = dyn_cast<Instruction>(CmpLHS->getOperand(0)); |
| 514 | if (CmpLHS) |
| 515 | CmpPHI = dyn_cast<PHINode>(CmpLHS); |
| 516 | } |
| 517 | if (!CmpPHI || !L->contains(CmpPHI)) |
| 518 | return; |
| 519 | |
| 520 | // Trace the phi node to find all values that come from successors of BB |
| 521 | SmallPtrSet<PHINode*, 8> VisitedInsts; |
| 522 | SmallVector<PHINode*, 8> WorkList; |
| 523 | WorkList.push_back(CmpPHI); |
| 524 | VisitedInsts.insert(CmpPHI); |
| 525 | while (!WorkList.empty()) { |
| 526 | PHINode *P = WorkList.back(); |
| 527 | WorkList.pop_back(); |
| 528 | for (BasicBlock *B : P->blocks()) { |
| 529 | // Skip blocks that aren't part of the loop |
| 530 | if (!L->contains(B)) |
| 531 | continue; |
| 532 | Value *V = P->getIncomingValueForBlock(B); |
| 533 | // If the source is a PHI add it to the work list if we haven't |
| 534 | // already visited it. |
| 535 | if (PHINode *PN = dyn_cast<PHINode>(V)) { |
| 536 | if (VisitedInsts.insert(PN).second) |
| 537 | WorkList.push_back(PN); |
| 538 | continue; |
| 539 | } |
| 540 | // If this incoming value is a constant and B is a successor of BB, then |
| 541 | // we can constant-evaluate the compare to see if it makes the branch be |
| 542 | // taken or not. |
| 543 | Constant *CmpLHSConst = dyn_cast<Constant>(V); |
| 544 | if (!CmpLHSConst || |
| 545 | std::find(succ_begin(BB), succ_end(BB), B) == succ_end(BB)) |
| 546 | continue; |
| 547 | // First collapse InstChain |
| 548 | for (Instruction *I : InstChain) { |
| 549 | CmpLHSConst = ConstantExpr::get(I->getOpcode(), CmpLHSConst, |
| 550 | dyn_cast<Constant>(I->getOperand(1)), |
| 551 | true); |
| 552 | if (!CmpLHSConst) |
| 553 | break; |
| 554 | } |
| 555 | if (!CmpLHSConst) |
| 556 | continue; |
| 557 | // Now constant-evaluate the compare |
| 558 | Constant *Result = ConstantExpr::getCompare(CI->getPredicate(), |
| 559 | CmpLHSConst, CmpConst, true); |
| 560 | // If the result means we don't branch to the block then that block is |
| 561 | // unlikely. |
| 562 | if (Result && |
| 563 | ((Result->isZeroValue() && B == BI->getSuccessor(0)) || |
| 564 | (Result->isOneValue() && B == BI->getSuccessor(1)))) |
| 565 | UnlikelyBlocks.insert(B); |
| 566 | } |
| 567 | } |
| 568 | } |
| 569 | |
Andrew Trick | 49371f3 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 570 | // Calculate Edge Weights using "Loop Branch Heuristics". Predict backedges |
| 571 | // as taken, exiting edges as not-taken. |
Mehdi Amini | a797877 | 2016-04-07 21:59:28 +0000 | [diff] [blame] | 572 | bool BranchProbabilityInfo::calcLoopBranchHeuristics(const BasicBlock *BB, |
Geoff Berry | eed6531 | 2017-11-01 15:16:50 +0000 | [diff] [blame] | 573 | const LoopInfo &LI, |
| 574 | SccInfo &SccI) { |
| 575 | int SccNum; |
Cong Hou | ab23bfb | 2015-07-15 22:48:29 +0000 | [diff] [blame] | 576 | Loop *L = LI.getLoopFor(BB); |
Geoff Berry | eed6531 | 2017-11-01 15:16:50 +0000 | [diff] [blame] | 577 | if (!L) { |
| 578 | SccNum = getSCCNum(BB, SccI); |
| 579 | if (SccNum < 0) |
| 580 | return false; |
| 581 | } |
Andrew Trick | 49371f3 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 582 | |
John Brawn | 29bbed3 | 2018-02-23 17:17:31 +0000 | [diff] [blame] | 583 | SmallPtrSet<const BasicBlock*, 8> UnlikelyBlocks; |
| 584 | if (L) |
| 585 | computeUnlikelySuccessors(BB, L, UnlikelyBlocks); |
| 586 | |
Manman Ren | cf10446 | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 587 | SmallVector<unsigned, 8> BackEdges; |
| 588 | SmallVector<unsigned, 8> ExitingEdges; |
| 589 | SmallVector<unsigned, 8> InEdges; // Edges from header to the loop. |
John Brawn | 29bbed3 | 2018-02-23 17:17:31 +0000 | [diff] [blame] | 590 | SmallVector<unsigned, 8> UnlikelyEdges; |
Jakub Staszak | bcb3c65 | 2011-07-28 21:33:46 +0000 | [diff] [blame] | 591 | |
Mehdi Amini | a797877 | 2016-04-07 21:59:28 +0000 | [diff] [blame] | 592 | for (succ_const_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I) { |
Geoff Berry | eed6531 | 2017-11-01 15:16:50 +0000 | [diff] [blame] | 593 | // Use LoopInfo if we have it, otherwise fall-back to SCC info to catch |
| 594 | // irreducible loops. |
| 595 | if (L) { |
John Brawn | 29bbed3 | 2018-02-23 17:17:31 +0000 | [diff] [blame] | 596 | if (UnlikelyBlocks.count(*I) != 0) |
| 597 | UnlikelyEdges.push_back(I.getSuccessorIndex()); |
| 598 | else if (!L->contains(*I)) |
Geoff Berry | eed6531 | 2017-11-01 15:16:50 +0000 | [diff] [blame] | 599 | ExitingEdges.push_back(I.getSuccessorIndex()); |
| 600 | else if (L->getHeader() == *I) |
| 601 | BackEdges.push_back(I.getSuccessorIndex()); |
| 602 | else |
| 603 | InEdges.push_back(I.getSuccessorIndex()); |
| 604 | } else { |
| 605 | if (getSCCNum(*I, SccI) != SccNum) |
| 606 | ExitingEdges.push_back(I.getSuccessorIndex()); |
| 607 | else if (isSCCHeader(*I, SccNum, SccI)) |
| 608 | BackEdges.push_back(I.getSuccessorIndex()); |
| 609 | else |
| 610 | InEdges.push_back(I.getSuccessorIndex()); |
| 611 | } |
Andrew Trick | 49371f3 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 612 | } |
| 613 | |
John Brawn | 29bbed3 | 2018-02-23 17:17:31 +0000 | [diff] [blame] | 614 | if (BackEdges.empty() && ExitingEdges.empty() && UnlikelyEdges.empty()) |
Akira Hatanaka | 5638b89 | 2014-04-14 16:56:19 +0000 | [diff] [blame] | 615 | return false; |
| 616 | |
Cong Hou | e93b8e1 | 2015-12-22 18:56:14 +0000 | [diff] [blame] | 617 | // Collect the sum of probabilities of back-edges/in-edges/exiting-edges, and |
| 618 | // normalize them so that they sum up to one. |
Cong Hou | e93b8e1 | 2015-12-22 18:56:14 +0000 | [diff] [blame] | 619 | unsigned Denom = (BackEdges.empty() ? 0 : LBH_TAKEN_WEIGHT) + |
| 620 | (InEdges.empty() ? 0 : LBH_TAKEN_WEIGHT) + |
John Brawn | 29bbed3 | 2018-02-23 17:17:31 +0000 | [diff] [blame] | 621 | (UnlikelyEdges.empty() ? 0 : LBH_UNLIKELY_WEIGHT) + |
Cong Hou | e93b8e1 | 2015-12-22 18:56:14 +0000 | [diff] [blame] | 622 | (ExitingEdges.empty() ? 0 : LBH_NONTAKEN_WEIGHT); |
Andrew Trick | 49371f3 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 623 | |
Cong Hou | e93b8e1 | 2015-12-22 18:56:14 +0000 | [diff] [blame] | 624 | if (uint32_t numBackEdges = BackEdges.size()) { |
John Brawn | 29bbed3 | 2018-02-23 17:17:31 +0000 | [diff] [blame] | 625 | BranchProbability TakenProb = BranchProbability(LBH_TAKEN_WEIGHT, Denom); |
| 626 | auto Prob = TakenProb / numBackEdges; |
Cong Hou | e93b8e1 | 2015-12-22 18:56:14 +0000 | [diff] [blame] | 627 | for (unsigned SuccIdx : BackEdges) |
| 628 | setEdgeProbability(BB, SuccIdx, Prob); |
Andrew Trick | 49371f3 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 629 | } |
| 630 | |
Jakub Staszak | bcb3c65 | 2011-07-28 21:33:46 +0000 | [diff] [blame] | 631 | if (uint32_t numInEdges = InEdges.size()) { |
John Brawn | 29bbed3 | 2018-02-23 17:17:31 +0000 | [diff] [blame] | 632 | BranchProbability TakenProb = BranchProbability(LBH_TAKEN_WEIGHT, Denom); |
| 633 | auto Prob = TakenProb / numInEdges; |
Cong Hou | e93b8e1 | 2015-12-22 18:56:14 +0000 | [diff] [blame] | 634 | for (unsigned SuccIdx : InEdges) |
| 635 | setEdgeProbability(BB, SuccIdx, Prob); |
Jakub Staszak | bcb3c65 | 2011-07-28 21:33:46 +0000 | [diff] [blame] | 636 | } |
| 637 | |
Chandler Carruth | 32f46e7 | 2011-10-25 09:47:41 +0000 | [diff] [blame] | 638 | if (uint32_t numExitingEdges = ExitingEdges.size()) { |
John Brawn | 29bbed3 | 2018-02-23 17:17:31 +0000 | [diff] [blame] | 639 | BranchProbability NotTakenProb = BranchProbability(LBH_NONTAKEN_WEIGHT, |
| 640 | Denom); |
| 641 | auto Prob = NotTakenProb / numExitingEdges; |
Cong Hou | e93b8e1 | 2015-12-22 18:56:14 +0000 | [diff] [blame] | 642 | for (unsigned SuccIdx : ExitingEdges) |
| 643 | setEdgeProbability(BB, SuccIdx, Prob); |
Andrew Trick | 49371f3 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 644 | } |
Jakub Staszak | d07b2e1 | 2011-07-28 21:45:07 +0000 | [diff] [blame] | 645 | |
John Brawn | 29bbed3 | 2018-02-23 17:17:31 +0000 | [diff] [blame] | 646 | if (uint32_t numUnlikelyEdges = UnlikelyEdges.size()) { |
| 647 | BranchProbability UnlikelyProb = BranchProbability(LBH_UNLIKELY_WEIGHT, |
| 648 | Denom); |
| 649 | auto Prob = UnlikelyProb / numUnlikelyEdges; |
| 650 | for (unsigned SuccIdx : UnlikelyEdges) |
| 651 | setEdgeProbability(BB, SuccIdx, Prob); |
| 652 | } |
| 653 | |
Jakub Staszak | d07b2e1 | 2011-07-28 21:45:07 +0000 | [diff] [blame] | 654 | return true; |
Andrew Trick | 49371f3 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 655 | } |
| 656 | |
John Brawn | da4a68a | 2017-06-08 09:44:40 +0000 | [diff] [blame] | 657 | bool BranchProbabilityInfo::calcZeroHeuristics(const BasicBlock *BB, |
| 658 | const TargetLibraryInfo *TLI) { |
Mehdi Amini | a797877 | 2016-04-07 21:59:28 +0000 | [diff] [blame] | 659 | const BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator()); |
Jakub Staszak | 17af66a | 2011-07-31 03:27:24 +0000 | [diff] [blame] | 660 | if (!BI || !BI->isConditional()) |
| 661 | return false; |
| 662 | |
| 663 | Value *Cond = BI->getCondition(); |
| 664 | ICmpInst *CI = dyn_cast<ICmpInst>(Cond); |
| 665 | if (!CI) |
| 666 | return false; |
| 667 | |
Jakub Staszak | 17af66a | 2011-07-31 03:27:24 +0000 | [diff] [blame] | 668 | Value *RHS = CI->getOperand(1); |
Jakub Staszak | bfb1ae2 | 2011-07-31 04:47:20 +0000 | [diff] [blame] | 669 | ConstantInt *CV = dyn_cast<ConstantInt>(RHS); |
Benjamin Kramer | 0ca1ad0 | 2011-09-04 23:53:04 +0000 | [diff] [blame] | 670 | if (!CV) |
Jakub Staszak | 17af66a | 2011-07-31 03:27:24 +0000 | [diff] [blame] | 671 | return false; |
| 672 | |
Daniel Jasper | a73f3d5 | 2015-04-15 06:24:07 +0000 | [diff] [blame] | 673 | // If the LHS is the result of AND'ing a value with a single bit bitmask, |
| 674 | // we don't have information about probabilities. |
| 675 | if (Instruction *LHS = dyn_cast<Instruction>(CI->getOperand(0))) |
| 676 | if (LHS->getOpcode() == Instruction::And) |
| 677 | if (ConstantInt *AndRHS = dyn_cast<ConstantInt>(LHS->getOperand(1))) |
Craig Topper | 4e22ee6 | 2017-08-04 16:59:29 +0000 | [diff] [blame] | 678 | if (AndRHS->getValue().isPowerOf2()) |
Daniel Jasper | a73f3d5 | 2015-04-15 06:24:07 +0000 | [diff] [blame] | 679 | return false; |
| 680 | |
John Brawn | da4a68a | 2017-06-08 09:44:40 +0000 | [diff] [blame] | 681 | // Check if the LHS is the return value of a library function |
| 682 | LibFunc Func = NumLibFuncs; |
| 683 | if (TLI) |
| 684 | if (CallInst *Call = dyn_cast<CallInst>(CI->getOperand(0))) |
| 685 | if (Function *CalledFn = Call->getCalledFunction()) |
| 686 | TLI->getLibFunc(*CalledFn, Func); |
| 687 | |
Jakub Staszak | 17af66a | 2011-07-31 03:27:24 +0000 | [diff] [blame] | 688 | bool isProb; |
John Brawn | da4a68a | 2017-06-08 09:44:40 +0000 | [diff] [blame] | 689 | if (Func == LibFunc_strcasecmp || |
| 690 | Func == LibFunc_strcmp || |
| 691 | Func == LibFunc_strncasecmp || |
| 692 | Func == LibFunc_strncmp || |
| 693 | Func == LibFunc_memcmp) { |
| 694 | // strcmp and similar functions return zero, negative, or positive, if the |
| 695 | // first string is equal, less, or greater than the second. We consider it |
| 696 | // likely that the strings are not equal, so a comparison with zero is |
| 697 | // probably false, but also a comparison with any other number is also |
| 698 | // probably false given that what exactly is returned for nonzero values is |
| 699 | // not specified. Any kind of comparison other than equality we know |
| 700 | // nothing about. |
| 701 | switch (CI->getPredicate()) { |
| 702 | case CmpInst::ICMP_EQ: |
| 703 | isProb = false; |
| 704 | break; |
| 705 | case CmpInst::ICMP_NE: |
| 706 | isProb = true; |
| 707 | break; |
| 708 | default: |
| 709 | return false; |
| 710 | } |
| 711 | } else if (CV->isZero()) { |
Benjamin Kramer | 0ca1ad0 | 2011-09-04 23:53:04 +0000 | [diff] [blame] | 712 | switch (CI->getPredicate()) { |
| 713 | case CmpInst::ICMP_EQ: |
| 714 | // X == 0 -> Unlikely |
| 715 | isProb = false; |
| 716 | break; |
| 717 | case CmpInst::ICMP_NE: |
| 718 | // X != 0 -> Likely |
| 719 | isProb = true; |
| 720 | break; |
| 721 | case CmpInst::ICMP_SLT: |
| 722 | // X < 0 -> Unlikely |
| 723 | isProb = false; |
| 724 | break; |
| 725 | case CmpInst::ICMP_SGT: |
| 726 | // X > 0 -> Likely |
| 727 | isProb = true; |
| 728 | break; |
| 729 | default: |
| 730 | return false; |
| 731 | } |
| 732 | } else if (CV->isOne() && CI->getPredicate() == CmpInst::ICMP_SLT) { |
| 733 | // InstCombine canonicalizes X <= 0 into X < 1. |
| 734 | // X <= 0 -> Unlikely |
Jakub Staszak | 17af66a | 2011-07-31 03:27:24 +0000 | [diff] [blame] | 735 | isProb = false; |
Craig Topper | 79ab643 | 2017-07-06 18:39:47 +0000 | [diff] [blame] | 736 | } else if (CV->isMinusOne()) { |
Hal Finkel | 4d94930 | 2013-11-01 10:58:22 +0000 | [diff] [blame] | 737 | switch (CI->getPredicate()) { |
| 738 | case CmpInst::ICMP_EQ: |
| 739 | // X == -1 -> Unlikely |
| 740 | isProb = false; |
| 741 | break; |
| 742 | case CmpInst::ICMP_NE: |
| 743 | // X != -1 -> Likely |
| 744 | isProb = true; |
| 745 | break; |
| 746 | case CmpInst::ICMP_SGT: |
| 747 | // InstCombine canonicalizes X >= 0 into X > -1. |
| 748 | // X >= 0 -> Likely |
| 749 | isProb = true; |
| 750 | break; |
| 751 | default: |
| 752 | return false; |
| 753 | } |
Benjamin Kramer | 0ca1ad0 | 2011-09-04 23:53:04 +0000 | [diff] [blame] | 754 | } else { |
Jakub Staszak | 17af66a | 2011-07-31 03:27:24 +0000 | [diff] [blame] | 755 | return false; |
Benjamin Kramer | 0ca1ad0 | 2011-09-04 23:53:04 +0000 | [diff] [blame] | 756 | } |
Jakub Staszak | 17af66a | 2011-07-31 03:27:24 +0000 | [diff] [blame] | 757 | |
Manman Ren | cf10446 | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 758 | unsigned TakenIdx = 0, NonTakenIdx = 1; |
Jakub Staszak | 17af66a | 2011-07-31 03:27:24 +0000 | [diff] [blame] | 759 | |
| 760 | if (!isProb) |
Manman Ren | cf10446 | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 761 | std::swap(TakenIdx, NonTakenIdx); |
Jakub Staszak | 17af66a | 2011-07-31 03:27:24 +0000 | [diff] [blame] | 762 | |
Cong Hou | e93b8e1 | 2015-12-22 18:56:14 +0000 | [diff] [blame] | 763 | BranchProbability TakenProb(ZH_TAKEN_WEIGHT, |
| 764 | ZH_TAKEN_WEIGHT + ZH_NONTAKEN_WEIGHT); |
| 765 | setEdgeProbability(BB, TakenIdx, TakenProb); |
| 766 | setEdgeProbability(BB, NonTakenIdx, TakenProb.getCompl()); |
Jakub Staszak | 17af66a | 2011-07-31 03:27:24 +0000 | [diff] [blame] | 767 | return true; |
| 768 | } |
| 769 | |
Mehdi Amini | a797877 | 2016-04-07 21:59:28 +0000 | [diff] [blame] | 770 | bool BranchProbabilityInfo::calcFloatingPointHeuristics(const BasicBlock *BB) { |
| 771 | const BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator()); |
Benjamin Kramer | 1e731a1 | 2011-10-21 20:12:47 +0000 | [diff] [blame] | 772 | if (!BI || !BI->isConditional()) |
| 773 | return false; |
| 774 | |
| 775 | Value *Cond = BI->getCondition(); |
| 776 | FCmpInst *FCmp = dyn_cast<FCmpInst>(Cond); |
Benjamin Kramer | 606a50a | 2011-10-21 21:13:47 +0000 | [diff] [blame] | 777 | if (!FCmp) |
Benjamin Kramer | 1e731a1 | 2011-10-21 20:12:47 +0000 | [diff] [blame] | 778 | return false; |
| 779 | |
Benjamin Kramer | 606a50a | 2011-10-21 21:13:47 +0000 | [diff] [blame] | 780 | bool isProb; |
| 781 | if (FCmp->isEquality()) { |
| 782 | // f1 == f2 -> Unlikely |
| 783 | // f1 != f2 -> Likely |
| 784 | isProb = !FCmp->isTrueWhenEqual(); |
| 785 | } else if (FCmp->getPredicate() == FCmpInst::FCMP_ORD) { |
| 786 | // !isnan -> Likely |
| 787 | isProb = true; |
| 788 | } else if (FCmp->getPredicate() == FCmpInst::FCMP_UNO) { |
| 789 | // isnan -> Unlikely |
| 790 | isProb = false; |
| 791 | } else { |
| 792 | return false; |
| 793 | } |
| 794 | |
Manman Ren | cf10446 | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 795 | unsigned TakenIdx = 0, NonTakenIdx = 1; |
Benjamin Kramer | 1e731a1 | 2011-10-21 20:12:47 +0000 | [diff] [blame] | 796 | |
Benjamin Kramer | 606a50a | 2011-10-21 21:13:47 +0000 | [diff] [blame] | 797 | if (!isProb) |
Manman Ren | cf10446 | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 798 | std::swap(TakenIdx, NonTakenIdx); |
Benjamin Kramer | 1e731a1 | 2011-10-21 20:12:47 +0000 | [diff] [blame] | 799 | |
Cong Hou | e93b8e1 | 2015-12-22 18:56:14 +0000 | [diff] [blame] | 800 | BranchProbability TakenProb(FPH_TAKEN_WEIGHT, |
| 801 | FPH_TAKEN_WEIGHT + FPH_NONTAKEN_WEIGHT); |
| 802 | setEdgeProbability(BB, TakenIdx, TakenProb); |
| 803 | setEdgeProbability(BB, NonTakenIdx, TakenProb.getCompl()); |
Benjamin Kramer | 1e731a1 | 2011-10-21 20:12:47 +0000 | [diff] [blame] | 804 | return true; |
| 805 | } |
Jakub Staszak | 17af66a | 2011-07-31 03:27:24 +0000 | [diff] [blame] | 806 | |
Mehdi Amini | a797877 | 2016-04-07 21:59:28 +0000 | [diff] [blame] | 807 | bool BranchProbabilityInfo::calcInvokeHeuristics(const BasicBlock *BB) { |
| 808 | const InvokeInst *II = dyn_cast<InvokeInst>(BB->getTerminator()); |
Bill Wendling | e1c5426 | 2012-08-15 12:22:35 +0000 | [diff] [blame] | 809 | if (!II) |
| 810 | return false; |
| 811 | |
Cong Hou | e93b8e1 | 2015-12-22 18:56:14 +0000 | [diff] [blame] | 812 | BranchProbability TakenProb(IH_TAKEN_WEIGHT, |
| 813 | IH_TAKEN_WEIGHT + IH_NONTAKEN_WEIGHT); |
| 814 | setEdgeProbability(BB, 0 /*Index for Normal*/, TakenProb); |
| 815 | setEdgeProbability(BB, 1 /*Index for Unwind*/, TakenProb.getCompl()); |
Bill Wendling | e1c5426 | 2012-08-15 12:22:35 +0000 | [diff] [blame] | 816 | return true; |
| 817 | } |
| 818 | |
Pete Cooper | b9d2e34 | 2015-05-28 19:43:06 +0000 | [diff] [blame] | 819 | void BranchProbabilityInfo::releaseMemory() { |
Cong Hou | e93b8e1 | 2015-12-22 18:56:14 +0000 | [diff] [blame] | 820 | Probs.clear(); |
Pete Cooper | b9d2e34 | 2015-05-28 19:43:06 +0000 | [diff] [blame] | 821 | } |
| 822 | |
Cong Hou | ab23bfb | 2015-07-15 22:48:29 +0000 | [diff] [blame] | 823 | void BranchProbabilityInfo::print(raw_ostream &OS) const { |
Chandler Carruth | 1c8ace0 | 2011-10-23 21:21:50 +0000 | [diff] [blame] | 824 | OS << "---- Branch Probabilities ----\n"; |
| 825 | // We print the probabilities from the last function the analysis ran over, |
| 826 | // or the function it is currently running over. |
| 827 | 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] | 828 | for (const auto &BI : *LastF) { |
| 829 | for (succ_const_iterator SI = succ_begin(&BI), SE = succ_end(&BI); SI != SE; |
| 830 | ++SI) { |
| 831 | printEdgeProbability(OS << " ", &BI, *SI); |
Duncan P. N. Exon Smith | 6c99015 | 2014-07-21 17:06:51 +0000 | [diff] [blame] | 832 | } |
| 833 | } |
Chandler Carruth | 1c8ace0 | 2011-10-23 21:21:50 +0000 | [diff] [blame] | 834 | } |
| 835 | |
Jakub Staszak | efd94c8 | 2011-07-29 19:30:00 +0000 | [diff] [blame] | 836 | bool BranchProbabilityInfo:: |
| 837 | isEdgeHot(const BasicBlock *Src, const BasicBlock *Dst) const { |
Andrew Trick | 3d4e64b | 2011-06-11 01:05:22 +0000 | [diff] [blame] | 838 | // Hot probability is at least 4/5 = 80% |
Benjamin Kramer | 929f53f | 2011-10-23 11:19:14 +0000 | [diff] [blame] | 839 | // FIXME: Compare against a static "hot" BranchProbability. |
| 840 | return getEdgeProbability(Src, Dst) > BranchProbability(4, 5); |
Andrew Trick | 49371f3 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 841 | } |
| 842 | |
Mehdi Amini | a797877 | 2016-04-07 21:59:28 +0000 | [diff] [blame] | 843 | const BasicBlock * |
| 844 | BranchProbabilityInfo::getHotSucc(const BasicBlock *BB) const { |
Cong Hou | e93b8e1 | 2015-12-22 18:56:14 +0000 | [diff] [blame] | 845 | auto MaxProb = BranchProbability::getZero(); |
Mehdi Amini | a797877 | 2016-04-07 21:59:28 +0000 | [diff] [blame] | 846 | const BasicBlock *MaxSucc = nullptr; |
Andrew Trick | 49371f3 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 847 | |
Mehdi Amini | a797877 | 2016-04-07 21:59:28 +0000 | [diff] [blame] | 848 | for (succ_const_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I) { |
| 849 | const BasicBlock *Succ = *I; |
Cong Hou | e93b8e1 | 2015-12-22 18:56:14 +0000 | [diff] [blame] | 850 | auto Prob = getEdgeProbability(BB, Succ); |
| 851 | if (Prob > MaxProb) { |
| 852 | MaxProb = Prob; |
Andrew Trick | 49371f3 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 853 | MaxSucc = Succ; |
| 854 | } |
| 855 | } |
| 856 | |
Benjamin Kramer | 929f53f | 2011-10-23 11:19:14 +0000 | [diff] [blame] | 857 | // Hot probability is at least 4/5 = 80% |
Cong Hou | e93b8e1 | 2015-12-22 18:56:14 +0000 | [diff] [blame] | 858 | if (MaxProb > BranchProbability(4, 5)) |
Andrew Trick | 49371f3 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 859 | return MaxSucc; |
| 860 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 861 | return nullptr; |
Andrew Trick | 49371f3 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 862 | } |
| 863 | |
Cong Hou | e93b8e1 | 2015-12-22 18:56:14 +0000 | [diff] [blame] | 864 | /// Get the raw edge probability for the edge. If can't find it, return a |
| 865 | /// default probability 1/N where N is the number of successors. Here an edge is |
| 866 | /// specified using PredBlock and an |
| 867 | /// index to the successors. |
| 868 | BranchProbability |
| 869 | BranchProbabilityInfo::getEdgeProbability(const BasicBlock *Src, |
| 870 | unsigned IndexInSuccessors) const { |
| 871 | auto I = Probs.find(std::make_pair(Src, IndexInSuccessors)); |
Andrew Trick | 49371f3 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 872 | |
Cong Hou | e93b8e1 | 2015-12-22 18:56:14 +0000 | [diff] [blame] | 873 | if (I != Probs.end()) |
Andrew Trick | 49371f3 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 874 | return I->second; |
| 875 | |
Cong Hou | e93b8e1 | 2015-12-22 18:56:14 +0000 | [diff] [blame] | 876 | return {1, |
| 877 | static_cast<uint32_t>(std::distance(succ_begin(Src), succ_end(Src)))}; |
Andrew Trick | 49371f3 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 878 | } |
| 879 | |
Cong Hou | d97c100 | 2015-12-01 05:29:22 +0000 | [diff] [blame] | 880 | BranchProbability |
| 881 | BranchProbabilityInfo::getEdgeProbability(const BasicBlock *Src, |
| 882 | succ_const_iterator Dst) const { |
| 883 | return getEdgeProbability(Src, Dst.getSuccessorIndex()); |
| 884 | } |
| 885 | |
Cong Hou | e93b8e1 | 2015-12-22 18:56:14 +0000 | [diff] [blame] | 886 | /// Get the raw edge probability calculated for the block pair. This returns the |
| 887 | /// sum of all raw edge probabilities from Src to Dst. |
| 888 | BranchProbability |
| 889 | BranchProbabilityInfo::getEdgeProbability(const BasicBlock *Src, |
| 890 | const BasicBlock *Dst) const { |
| 891 | auto Prob = BranchProbability::getZero(); |
| 892 | bool FoundProb = false; |
| 893 | for (succ_const_iterator I = succ_begin(Src), E = succ_end(Src); I != E; ++I) |
| 894 | if (*I == Dst) { |
| 895 | auto MapI = Probs.find(std::make_pair(Src, I.getSuccessorIndex())); |
| 896 | if (MapI != Probs.end()) { |
| 897 | FoundProb = true; |
| 898 | Prob += MapI->second; |
| 899 | } |
| 900 | } |
| 901 | uint32_t succ_num = std::distance(succ_begin(Src), succ_end(Src)); |
| 902 | return FoundProb ? Prob : BranchProbability(1, succ_num); |
| 903 | } |
| 904 | |
| 905 | /// Set the edge probability for a given edge specified by PredBlock and an |
| 906 | /// index to the successors. |
| 907 | void BranchProbabilityInfo::setEdgeProbability(const BasicBlock *Src, |
| 908 | unsigned IndexInSuccessors, |
| 909 | BranchProbability Prob) { |
| 910 | Probs[std::make_pair(Src, IndexInSuccessors)] = Prob; |
Igor Laevsky | ee40d1e | 2016-07-15 14:31:16 +0000 | [diff] [blame] | 911 | Handles.insert(BasicBlockCallbackVH(Src, this)); |
Cong Hou | e93b8e1 | 2015-12-22 18:56:14 +0000 | [diff] [blame] | 912 | DEBUG(dbgs() << "set edge " << Src->getName() << " -> " << IndexInSuccessors |
| 913 | << " successor probability to " << Prob << "\n"); |
| 914 | } |
| 915 | |
Andrew Trick | 49371f3 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 916 | raw_ostream & |
Chandler Carruth | 1c8ace0 | 2011-10-23 21:21:50 +0000 | [diff] [blame] | 917 | BranchProbabilityInfo::printEdgeProbability(raw_ostream &OS, |
| 918 | const BasicBlock *Src, |
| 919 | const BasicBlock *Dst) const { |
Jakub Staszak | 12a43bd | 2011-06-16 20:22:37 +0000 | [diff] [blame] | 920 | const BranchProbability Prob = getEdgeProbability(Src, Dst); |
Benjamin Kramer | 1f97a5a | 2011-11-15 16:27:03 +0000 | [diff] [blame] | 921 | OS << "edge " << Src->getName() << " -> " << Dst->getName() |
Andrew Trick | 3d4e64b | 2011-06-11 01:05:22 +0000 | [diff] [blame] | 922 | << " probability is " << Prob |
| 923 | << (isEdgeHot(Src, Dst) ? " [HOT edge]\n" : "\n"); |
Andrew Trick | 49371f3 | 2011-06-04 01:16:30 +0000 | [diff] [blame] | 924 | |
| 925 | return OS; |
| 926 | } |
Cong Hou | ab23bfb | 2015-07-15 22:48:29 +0000 | [diff] [blame] | 927 | |
Igor Laevsky | ee40d1e | 2016-07-15 14:31:16 +0000 | [diff] [blame] | 928 | void BranchProbabilityInfo::eraseBlock(const BasicBlock *BB) { |
| 929 | for (auto I = Probs.begin(), E = Probs.end(); I != E; ++I) { |
| 930 | auto Key = I->first; |
| 931 | if (Key.first == BB) |
| 932 | Probs.erase(Key); |
| 933 | } |
| 934 | } |
| 935 | |
John Brawn | da4a68a | 2017-06-08 09:44:40 +0000 | [diff] [blame] | 936 | void BranchProbabilityInfo::calculate(const Function &F, const LoopInfo &LI, |
| 937 | const TargetLibraryInfo *TLI) { |
Cong Hou | ab23bfb | 2015-07-15 22:48:29 +0000 | [diff] [blame] | 938 | DEBUG(dbgs() << "---- Branch Probability Info : " << F.getName() |
| 939 | << " ----\n\n"); |
| 940 | LastF = &F; // Store the last function we ran on for printing. |
| 941 | assert(PostDominatedByUnreachable.empty()); |
| 942 | assert(PostDominatedByColdCall.empty()); |
| 943 | |
Geoff Berry | eed6531 | 2017-11-01 15:16:50 +0000 | [diff] [blame] | 944 | // Record SCC numbers of blocks in the CFG to identify irreducible loops. |
| 945 | // FIXME: We could only calculate this if the CFG is known to be irreducible |
| 946 | // (perhaps cache this info in LoopInfo if we can easily calculate it there?). |
| 947 | int SccNum = 0; |
| 948 | SccInfo SccI; |
| 949 | for (scc_iterator<const Function *> It = scc_begin(&F); !It.isAtEnd(); |
| 950 | ++It, ++SccNum) { |
| 951 | // Ignore single-block SCCs since they either aren't loops or LoopInfo will |
| 952 | // catch them. |
| 953 | const std::vector<const BasicBlock *> &Scc = *It; |
| 954 | if (Scc.size() == 1) |
| 955 | continue; |
| 956 | |
| 957 | DEBUG(dbgs() << "BPI: SCC " << SccNum << ":"); |
| 958 | for (auto *BB : Scc) { |
| 959 | DEBUG(dbgs() << " " << BB->getName()); |
| 960 | SccI.SccNums[BB] = SccNum; |
| 961 | } |
| 962 | DEBUG(dbgs() << "\n"); |
| 963 | } |
| 964 | |
Cong Hou | ab23bfb | 2015-07-15 22:48:29 +0000 | [diff] [blame] | 965 | // Walk the basic blocks in post-order so that we can build up state about |
| 966 | // the successors of a block iteratively. |
| 967 | for (auto BB : post_order(&F.getEntryBlock())) { |
| 968 | DEBUG(dbgs() << "Computing probabilities for " << BB->getName() << "\n"); |
Serguei Katkov | ecebc3d | 2017-04-12 05:42:14 +0000 | [diff] [blame] | 969 | updatePostDominatedByUnreachable(BB); |
| 970 | updatePostDominatedByColdCall(BB); |
Serguei Katkov | 11d9c4f | 2017-04-17 06:39:47 +0000 | [diff] [blame] | 971 | // If there is no at least two successors, no sense to set probability. |
| 972 | if (BB->getTerminator()->getNumSuccessors() < 2) |
| 973 | continue; |
Cong Hou | ab23bfb | 2015-07-15 22:48:29 +0000 | [diff] [blame] | 974 | if (calcMetadataWeights(BB)) |
| 975 | continue; |
Serguei Katkov | 2616bbb | 2017-04-17 04:33:04 +0000 | [diff] [blame] | 976 | if (calcUnreachableHeuristics(BB)) |
| 977 | continue; |
Cong Hou | ab23bfb | 2015-07-15 22:48:29 +0000 | [diff] [blame] | 978 | if (calcColdCallHeuristics(BB)) |
| 979 | continue; |
Geoff Berry | eed6531 | 2017-11-01 15:16:50 +0000 | [diff] [blame] | 980 | if (calcLoopBranchHeuristics(BB, LI, SccI)) |
Cong Hou | ab23bfb | 2015-07-15 22:48:29 +0000 | [diff] [blame] | 981 | continue; |
| 982 | if (calcPointerHeuristics(BB)) |
| 983 | continue; |
John Brawn | da4a68a | 2017-06-08 09:44:40 +0000 | [diff] [blame] | 984 | if (calcZeroHeuristics(BB, TLI)) |
Cong Hou | ab23bfb | 2015-07-15 22:48:29 +0000 | [diff] [blame] | 985 | continue; |
| 986 | if (calcFloatingPointHeuristics(BB)) |
| 987 | continue; |
| 988 | calcInvokeHeuristics(BB); |
| 989 | } |
| 990 | |
| 991 | PostDominatedByUnreachable.clear(); |
| 992 | PostDominatedByColdCall.clear(); |
Hiroshi Yamauchi | 63e17eb | 2017-08-26 00:31:00 +0000 | [diff] [blame] | 993 | |
| 994 | if (PrintBranchProb && |
| 995 | (PrintBranchProbFuncName.empty() || |
| 996 | F.getName().equals(PrintBranchProbFuncName))) { |
| 997 | print(dbgs()); |
| 998 | } |
Cong Hou | ab23bfb | 2015-07-15 22:48:29 +0000 | [diff] [blame] | 999 | } |
| 1000 | |
| 1001 | void BranchProbabilityInfoWrapperPass::getAnalysisUsage( |
| 1002 | AnalysisUsage &AU) const { |
| 1003 | AU.addRequired<LoopInfoWrapperPass>(); |
John Brawn | da4a68a | 2017-06-08 09:44:40 +0000 | [diff] [blame] | 1004 | AU.addRequired<TargetLibraryInfoWrapperPass>(); |
Cong Hou | ab23bfb | 2015-07-15 22:48:29 +0000 | [diff] [blame] | 1005 | AU.setPreservesAll(); |
| 1006 | } |
| 1007 | |
| 1008 | bool BranchProbabilityInfoWrapperPass::runOnFunction(Function &F) { |
| 1009 | const LoopInfo &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo(); |
John Brawn | da4a68a | 2017-06-08 09:44:40 +0000 | [diff] [blame] | 1010 | const TargetLibraryInfo &TLI = getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(); |
| 1011 | BPI.calculate(F, LI, &TLI); |
Cong Hou | ab23bfb | 2015-07-15 22:48:29 +0000 | [diff] [blame] | 1012 | return false; |
| 1013 | } |
| 1014 | |
| 1015 | void BranchProbabilityInfoWrapperPass::releaseMemory() { BPI.releaseMemory(); } |
| 1016 | |
| 1017 | void BranchProbabilityInfoWrapperPass::print(raw_ostream &OS, |
| 1018 | const Module *) const { |
| 1019 | BPI.print(OS); |
| 1020 | } |
Xinliang David Li | 6e5dd41 | 2016-05-05 02:59:57 +0000 | [diff] [blame] | 1021 | |
Chandler Carruth | dab4eae | 2016-11-23 17:53:26 +0000 | [diff] [blame] | 1022 | AnalysisKey BranchProbabilityAnalysis::Key; |
Xinliang David Li | 6e5dd41 | 2016-05-05 02:59:57 +0000 | [diff] [blame] | 1023 | BranchProbabilityInfo |
Sean Silva | 36e0d01 | 2016-08-09 00:28:15 +0000 | [diff] [blame] | 1024 | BranchProbabilityAnalysis::run(Function &F, FunctionAnalysisManager &AM) { |
Xinliang David Li | 6e5dd41 | 2016-05-05 02:59:57 +0000 | [diff] [blame] | 1025 | BranchProbabilityInfo BPI; |
John Brawn | da4a68a | 2017-06-08 09:44:40 +0000 | [diff] [blame] | 1026 | BPI.calculate(F, AM.getResult<LoopAnalysis>(F), &AM.getResult<TargetLibraryAnalysis>(F)); |
Xinliang David Li | 6e5dd41 | 2016-05-05 02:59:57 +0000 | [diff] [blame] | 1027 | return BPI; |
| 1028 | } |
| 1029 | |
| 1030 | PreservedAnalyses |
Sean Silva | 36e0d01 | 2016-08-09 00:28:15 +0000 | [diff] [blame] | 1031 | BranchProbabilityPrinterPass::run(Function &F, FunctionAnalysisManager &AM) { |
Xinliang David Li | 6e5dd41 | 2016-05-05 02:59:57 +0000 | [diff] [blame] | 1032 | OS << "Printing analysis results of BPI for function " |
| 1033 | << "'" << F.getName() << "':" |
| 1034 | << "\n"; |
| 1035 | AM.getResult<BranchProbabilityAnalysis>(F).print(OS); |
| 1036 | return PreservedAnalyses::all(); |
| 1037 | } |