BranchProbabilityInfo.cpp 37 KB

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