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