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