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