BlockFrequencyInfo.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. //===- BlockFrequencyInfo.cpp - Block Frequency 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/BlockFrequencyInfo.h"
  13. #include "llvm/ADT/APInt.h"
  14. #include "llvm/ADT/None.h"
  15. #include "llvm/ADT/iterator.h"
  16. #include "llvm/Analysis/BlockFrequencyInfoImpl.h"
  17. #include "llvm/Analysis/BranchProbabilityInfo.h"
  18. #include "llvm/Analysis/LoopInfo.h"
  19. #include "llvm/IR/CFG.h"
  20. #include "llvm/IR/Function.h"
  21. #include "llvm/IR/PassManager.h"
  22. #include "llvm/InitializePasses.h"
  23. #include "llvm/Pass.h"
  24. #include "llvm/Support/CommandLine.h"
  25. #include "llvm/Support/GraphWriter.h"
  26. #include "llvm/Support/raw_ostream.h"
  27. #include <algorithm>
  28. #include <cassert>
  29. #include <string>
  30. using namespace llvm;
  31. #define DEBUG_TYPE "block-freq"
  32. static cl::opt<GVDAGType> ViewBlockFreqPropagationDAG(
  33. "view-block-freq-propagation-dags", cl::Hidden,
  34. cl::desc("Pop up a window to show a dag displaying how block "
  35. "frequencies propagation through the CFG."),
  36. cl::values(clEnumValN(GVDT_None, "none", "do not display graphs."),
  37. clEnumValN(GVDT_Fraction, "fraction",
  38. "display a graph using the "
  39. "fractional block frequency representation."),
  40. clEnumValN(GVDT_Integer, "integer",
  41. "display a graph using the raw "
  42. "integer fractional block frequency representation."),
  43. clEnumValN(GVDT_Count, "count", "display a graph using the real "
  44. "profile count if available.")));
  45. namespace llvm {
  46. cl::opt<std::string>
  47. ViewBlockFreqFuncName("view-bfi-func-name", cl::Hidden,
  48. cl::desc("The option to specify "
  49. "the name of the function "
  50. "whose CFG will be displayed."));
  51. cl::opt<unsigned>
  52. ViewHotFreqPercent("view-hot-freq-percent", cl::init(10), cl::Hidden,
  53. cl::desc("An integer in percent used to specify "
  54. "the hot blocks/edges to be displayed "
  55. "in red: a block or edge whose frequency "
  56. "is no less than the max frequency of the "
  57. "function multiplied by this percent."));
  58. // Command line option to turn on CFG dot or text dump after profile annotation.
  59. cl::opt<PGOViewCountsType> PGOViewCounts(
  60. "pgo-view-counts", cl::Hidden,
  61. cl::desc("A boolean option to show CFG dag or text with "
  62. "block profile counts and branch probabilities "
  63. "right after PGO profile annotation step. The "
  64. "profile counts are computed using branch "
  65. "probabilities from the runtime profile data and "
  66. "block frequency propagation algorithm. To view "
  67. "the raw counts from the profile, use option "
  68. "-pgo-view-raw-counts instead. To limit graph "
  69. "display to only one function, use filtering option "
  70. "-view-bfi-func-name."),
  71. cl::values(clEnumValN(PGOVCT_None, "none", "do not show."),
  72. clEnumValN(PGOVCT_Graph, "graph", "show a graph."),
  73. clEnumValN(PGOVCT_Text, "text", "show in text.")));
  74. static cl::opt<bool> PrintBlockFreq(
  75. "print-bfi", cl::init(false), cl::Hidden,
  76. cl::desc("Print the block frequency info."));
  77. cl::opt<std::string> PrintBlockFreqFuncName(
  78. "print-bfi-func-name", cl::Hidden,
  79. cl::desc("The option to specify the name of the function "
  80. "whose block frequency info is printed."));
  81. } // namespace llvm
  82. namespace llvm {
  83. static GVDAGType getGVDT() {
  84. if (PGOViewCounts == PGOVCT_Graph)
  85. return GVDT_Count;
  86. return ViewBlockFreqPropagationDAG;
  87. }
  88. template <>
  89. struct GraphTraits<BlockFrequencyInfo *> {
  90. using NodeRef = const BasicBlock *;
  91. using ChildIteratorType = const_succ_iterator;
  92. using nodes_iterator = pointer_iterator<Function::const_iterator>;
  93. static NodeRef getEntryNode(const BlockFrequencyInfo *G) {
  94. return &G->getFunction()->front();
  95. }
  96. static ChildIteratorType child_begin(const NodeRef N) {
  97. return succ_begin(N);
  98. }
  99. static ChildIteratorType child_end(const NodeRef N) { return succ_end(N); }
  100. static nodes_iterator nodes_begin(const BlockFrequencyInfo *G) {
  101. return nodes_iterator(G->getFunction()->begin());
  102. }
  103. static nodes_iterator nodes_end(const BlockFrequencyInfo *G) {
  104. return nodes_iterator(G->getFunction()->end());
  105. }
  106. };
  107. using BFIDOTGTraitsBase =
  108. BFIDOTGraphTraitsBase<BlockFrequencyInfo, BranchProbabilityInfo>;
  109. template <>
  110. struct DOTGraphTraits<BlockFrequencyInfo *> : public BFIDOTGTraitsBase {
  111. explicit DOTGraphTraits(bool isSimple = false)
  112. : BFIDOTGTraitsBase(isSimple) {}
  113. std::string getNodeLabel(const BasicBlock *Node,
  114. const BlockFrequencyInfo *Graph) {
  115. return BFIDOTGTraitsBase::getNodeLabel(Node, Graph, getGVDT());
  116. }
  117. std::string getNodeAttributes(const BasicBlock *Node,
  118. const BlockFrequencyInfo *Graph) {
  119. return BFIDOTGTraitsBase::getNodeAttributes(Node, Graph,
  120. ViewHotFreqPercent);
  121. }
  122. std::string getEdgeAttributes(const BasicBlock *Node, EdgeIter EI,
  123. const BlockFrequencyInfo *BFI) {
  124. return BFIDOTGTraitsBase::getEdgeAttributes(Node, EI, BFI, BFI->getBPI(),
  125. ViewHotFreqPercent);
  126. }
  127. };
  128. } // end namespace llvm
  129. BlockFrequencyInfo::BlockFrequencyInfo() = default;
  130. BlockFrequencyInfo::BlockFrequencyInfo(const Function &F,
  131. const BranchProbabilityInfo &BPI,
  132. const LoopInfo &LI) {
  133. calculate(F, BPI, LI);
  134. }
  135. BlockFrequencyInfo::BlockFrequencyInfo(BlockFrequencyInfo &&Arg)
  136. : BFI(std::move(Arg.BFI)) {}
  137. BlockFrequencyInfo &BlockFrequencyInfo::operator=(BlockFrequencyInfo &&RHS) {
  138. releaseMemory();
  139. BFI = std::move(RHS.BFI);
  140. return *this;
  141. }
  142. // Explicitly define the default constructor otherwise it would be implicitly
  143. // defined at the first ODR-use which is the BFI member in the
  144. // LazyBlockFrequencyInfo header. The dtor needs the BlockFrequencyInfoImpl
  145. // template instantiated which is not available in the header.
  146. BlockFrequencyInfo::~BlockFrequencyInfo() = default;
  147. bool BlockFrequencyInfo::invalidate(Function &F, const PreservedAnalyses &PA,
  148. FunctionAnalysisManager::Invalidator &) {
  149. // Check whether the analysis, all analyses on functions, or the function's
  150. // CFG have been preserved.
  151. auto PAC = PA.getChecker<BlockFrequencyAnalysis>();
  152. return !(PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Function>>() ||
  153. PAC.preservedSet<CFGAnalyses>());
  154. }
  155. void BlockFrequencyInfo::calculate(const Function &F,
  156. const BranchProbabilityInfo &BPI,
  157. const LoopInfo &LI) {
  158. if (!BFI)
  159. BFI.reset(new ImplType);
  160. BFI->calculate(F, BPI, LI);
  161. if (ViewBlockFreqPropagationDAG != GVDT_None &&
  162. (ViewBlockFreqFuncName.empty() ||
  163. F.getName().equals(ViewBlockFreqFuncName))) {
  164. view();
  165. }
  166. if (PrintBlockFreq &&
  167. (PrintBlockFreqFuncName.empty() ||
  168. F.getName().equals(PrintBlockFreqFuncName))) {
  169. print(dbgs());
  170. }
  171. }
  172. BlockFrequency BlockFrequencyInfo::getBlockFreq(const BasicBlock *BB) const {
  173. return BFI ? BFI->getBlockFreq(BB) : 0;
  174. }
  175. Optional<uint64_t>
  176. BlockFrequencyInfo::getBlockProfileCount(const BasicBlock *BB,
  177. bool AllowSynthetic) const {
  178. if (!BFI)
  179. return None;
  180. return BFI->getBlockProfileCount(*getFunction(), BB, AllowSynthetic);
  181. }
  182. Optional<uint64_t>
  183. BlockFrequencyInfo::getProfileCountFromFreq(uint64_t Freq) const {
  184. if (!BFI)
  185. return None;
  186. return BFI->getProfileCountFromFreq(*getFunction(), Freq);
  187. }
  188. bool BlockFrequencyInfo::isIrrLoopHeader(const BasicBlock *BB) {
  189. assert(BFI && "Expected analysis to be available");
  190. return BFI->isIrrLoopHeader(BB);
  191. }
  192. void BlockFrequencyInfo::setBlockFreq(const BasicBlock *BB, uint64_t Freq) {
  193. assert(BFI && "Expected analysis to be available");
  194. BFI->setBlockFreq(BB, Freq);
  195. }
  196. void BlockFrequencyInfo::setBlockFreqAndScale(
  197. const BasicBlock *ReferenceBB, uint64_t Freq,
  198. SmallPtrSetImpl<BasicBlock *> &BlocksToScale) {
  199. assert(BFI && "Expected analysis to be available");
  200. // Use 128 bits APInt to avoid overflow.
  201. APInt NewFreq(128, Freq);
  202. APInt OldFreq(128, BFI->getBlockFreq(ReferenceBB).getFrequency());
  203. APInt BBFreq(128, 0);
  204. for (auto *BB : BlocksToScale) {
  205. BBFreq = BFI->getBlockFreq(BB).getFrequency();
  206. // Multiply first by NewFreq and then divide by OldFreq
  207. // to minimize loss of precision.
  208. BBFreq *= NewFreq;
  209. // udiv is an expensive operation in the general case. If this ends up being
  210. // a hot spot, one of the options proposed in
  211. // https://reviews.llvm.org/D28535#650071 could be used to avoid this.
  212. BBFreq = BBFreq.udiv(OldFreq);
  213. BFI->setBlockFreq(BB, BBFreq.getLimitedValue());
  214. }
  215. BFI->setBlockFreq(ReferenceBB, Freq);
  216. }
  217. /// Pop up a ghostview window with the current block frequency propagation
  218. /// rendered using dot.
  219. void BlockFrequencyInfo::view(StringRef title) const {
  220. ViewGraph(const_cast<BlockFrequencyInfo *>(this), title);
  221. }
  222. const Function *BlockFrequencyInfo::getFunction() const {
  223. return BFI ? BFI->getFunction() : nullptr;
  224. }
  225. const BranchProbabilityInfo *BlockFrequencyInfo::getBPI() const {
  226. return BFI ? &BFI->getBPI() : nullptr;
  227. }
  228. raw_ostream &BlockFrequencyInfo::
  229. printBlockFreq(raw_ostream &OS, const BlockFrequency Freq) const {
  230. return BFI ? BFI->printBlockFreq(OS, Freq) : OS;
  231. }
  232. raw_ostream &
  233. BlockFrequencyInfo::printBlockFreq(raw_ostream &OS,
  234. const BasicBlock *BB) const {
  235. return BFI ? BFI->printBlockFreq(OS, BB) : OS;
  236. }
  237. uint64_t BlockFrequencyInfo::getEntryFreq() const {
  238. return BFI ? BFI->getEntryFreq() : 0;
  239. }
  240. void BlockFrequencyInfo::releaseMemory() { BFI.reset(); }
  241. void BlockFrequencyInfo::print(raw_ostream &OS) const {
  242. if (BFI)
  243. BFI->print(OS);
  244. }
  245. void BlockFrequencyInfo::verifyMatch(BlockFrequencyInfo &Other) const {
  246. if (BFI)
  247. BFI->verifyMatch(*Other.BFI);
  248. }
  249. INITIALIZE_PASS_BEGIN(BlockFrequencyInfoWrapperPass, "block-freq",
  250. "Block Frequency Analysis", true, true)
  251. INITIALIZE_PASS_DEPENDENCY(BranchProbabilityInfoWrapperPass)
  252. INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
  253. INITIALIZE_PASS_END(BlockFrequencyInfoWrapperPass, "block-freq",
  254. "Block Frequency Analysis", true, true)
  255. char BlockFrequencyInfoWrapperPass::ID = 0;
  256. BlockFrequencyInfoWrapperPass::BlockFrequencyInfoWrapperPass()
  257. : FunctionPass(ID) {
  258. initializeBlockFrequencyInfoWrapperPassPass(*PassRegistry::getPassRegistry());
  259. }
  260. BlockFrequencyInfoWrapperPass::~BlockFrequencyInfoWrapperPass() = default;
  261. void BlockFrequencyInfoWrapperPass::print(raw_ostream &OS,
  262. const Module *) const {
  263. BFI.print(OS);
  264. }
  265. void BlockFrequencyInfoWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
  266. AU.addRequired<BranchProbabilityInfoWrapperPass>();
  267. AU.addRequired<LoopInfoWrapperPass>();
  268. AU.setPreservesAll();
  269. }
  270. void BlockFrequencyInfoWrapperPass::releaseMemory() { BFI.releaseMemory(); }
  271. bool BlockFrequencyInfoWrapperPass::runOnFunction(Function &F) {
  272. BranchProbabilityInfo &BPI =
  273. getAnalysis<BranchProbabilityInfoWrapperPass>().getBPI();
  274. LoopInfo &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
  275. BFI.calculate(F, BPI, LI);
  276. return false;
  277. }
  278. AnalysisKey BlockFrequencyAnalysis::Key;
  279. BlockFrequencyInfo BlockFrequencyAnalysis::run(Function &F,
  280. FunctionAnalysisManager &AM) {
  281. BlockFrequencyInfo BFI;
  282. BFI.calculate(F, AM.getResult<BranchProbabilityAnalysis>(F),
  283. AM.getResult<LoopAnalysis>(F));
  284. return BFI;
  285. }
  286. PreservedAnalyses
  287. BlockFrequencyPrinterPass::run(Function &F, FunctionAnalysisManager &AM) {
  288. OS << "Printing analysis results of BFI for function "
  289. << "'" << F.getName() << "':"
  290. << "\n";
  291. AM.getResult<BlockFrequencyAnalysis>(F).print(OS);
  292. return PreservedAnalyses::all();
  293. }