FunctionPropertiesAnalysis.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. //===- FunctionPropertiesAnalysis.cpp - Function Properties 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. // This file defines the FunctionPropertiesInfo and FunctionPropertiesAnalysis
  10. // classes used to extract function properties.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/Analysis/FunctionPropertiesAnalysis.h"
  14. #include "llvm/ADT/STLExtras.h"
  15. #include "llvm/ADT/SetVector.h"
  16. #include "llvm/Analysis/LoopInfo.h"
  17. #include "llvm/IR/CFG.h"
  18. #include "llvm/IR/Dominators.h"
  19. #include "llvm/IR/Instructions.h"
  20. #include <deque>
  21. using namespace llvm;
  22. namespace {
  23. int64_t getNrBlocksFromCond(const BasicBlock &BB) {
  24. int64_t Ret = 0;
  25. if (const auto *BI = dyn_cast<BranchInst>(BB.getTerminator())) {
  26. if (BI->isConditional())
  27. Ret += BI->getNumSuccessors();
  28. } else if (const auto *SI = dyn_cast<SwitchInst>(BB.getTerminator())) {
  29. Ret += (SI->getNumCases() + (nullptr != SI->getDefaultDest()));
  30. }
  31. return Ret;
  32. }
  33. int64_t getUses(const Function &F) {
  34. return ((!F.hasLocalLinkage()) ? 1 : 0) + F.getNumUses();
  35. }
  36. } // namespace
  37. void FunctionPropertiesInfo::reIncludeBB(const BasicBlock &BB) {
  38. updateForBB(BB, +1);
  39. }
  40. void FunctionPropertiesInfo::updateForBB(const BasicBlock &BB,
  41. int64_t Direction) {
  42. assert(Direction == 1 || Direction == -1);
  43. BasicBlockCount += Direction;
  44. BlocksReachedFromConditionalInstruction +=
  45. (Direction * getNrBlocksFromCond(BB));
  46. for (const auto &I : BB) {
  47. if (auto *CS = dyn_cast<CallBase>(&I)) {
  48. const auto *Callee = CS->getCalledFunction();
  49. if (Callee && !Callee->isIntrinsic() && !Callee->isDeclaration())
  50. DirectCallsToDefinedFunctions += Direction;
  51. }
  52. if (I.getOpcode() == Instruction::Load) {
  53. LoadInstCount += Direction;
  54. } else if (I.getOpcode() == Instruction::Store) {
  55. StoreInstCount += Direction;
  56. }
  57. }
  58. TotalInstructionCount += Direction * BB.sizeWithoutDebug();
  59. }
  60. void FunctionPropertiesInfo::updateAggregateStats(const Function &F,
  61. const LoopInfo &LI) {
  62. Uses = getUses(F);
  63. TopLevelLoopCount = llvm::size(LI);
  64. MaxLoopDepth = 0;
  65. std::deque<const Loop *> Worklist;
  66. llvm::append_range(Worklist, LI);
  67. while (!Worklist.empty()) {
  68. const auto *L = Worklist.front();
  69. MaxLoopDepth =
  70. std::max(MaxLoopDepth, static_cast<int64_t>(L->getLoopDepth()));
  71. Worklist.pop_front();
  72. llvm::append_range(Worklist, L->getSubLoops());
  73. }
  74. }
  75. FunctionPropertiesInfo FunctionPropertiesInfo::getFunctionPropertiesInfo(
  76. const Function &F, FunctionAnalysisManager &FAM) {
  77. FunctionPropertiesInfo FPI;
  78. // The const casts are due to the getResult API - there's no mutation of F.
  79. const auto &LI = FAM.getResult<LoopAnalysis>(const_cast<Function &>(F));
  80. const auto &DT =
  81. FAM.getResult<DominatorTreeAnalysis>(const_cast<Function &>(F));
  82. for (const auto &BB : F)
  83. if (DT.isReachableFromEntry(&BB))
  84. FPI.reIncludeBB(BB);
  85. FPI.updateAggregateStats(F, LI);
  86. return FPI;
  87. }
  88. void FunctionPropertiesInfo::print(raw_ostream &OS) const {
  89. OS << "BasicBlockCount: " << BasicBlockCount << "\n"
  90. << "BlocksReachedFromConditionalInstruction: "
  91. << BlocksReachedFromConditionalInstruction << "\n"
  92. << "Uses: " << Uses << "\n"
  93. << "DirectCallsToDefinedFunctions: " << DirectCallsToDefinedFunctions
  94. << "\n"
  95. << "LoadInstCount: " << LoadInstCount << "\n"
  96. << "StoreInstCount: " << StoreInstCount << "\n"
  97. << "MaxLoopDepth: " << MaxLoopDepth << "\n"
  98. << "TopLevelLoopCount: " << TopLevelLoopCount << "\n"
  99. << "TotalInstructionCount: " << TotalInstructionCount << "\n\n";
  100. }
  101. AnalysisKey FunctionPropertiesAnalysis::Key;
  102. FunctionPropertiesInfo
  103. FunctionPropertiesAnalysis::run(Function &F, FunctionAnalysisManager &FAM) {
  104. return FunctionPropertiesInfo::getFunctionPropertiesInfo(F, FAM);
  105. }
  106. PreservedAnalyses
  107. FunctionPropertiesPrinterPass::run(Function &F, FunctionAnalysisManager &AM) {
  108. OS << "Printing analysis results of CFA for function "
  109. << "'" << F.getName() << "':"
  110. << "\n";
  111. AM.getResult<FunctionPropertiesAnalysis>(F).print(OS);
  112. return PreservedAnalyses::all();
  113. }
  114. FunctionPropertiesUpdater::FunctionPropertiesUpdater(
  115. FunctionPropertiesInfo &FPI, const CallBase &CB)
  116. : FPI(FPI), CallSiteBB(*CB.getParent()), Caller(*CallSiteBB.getParent()) {
  117. assert(isa<CallInst>(CB) || isa<InvokeInst>(CB));
  118. // For BBs that are likely to change, we subtract from feature totals their
  119. // contribution. Some features, like max loop counts or depths, are left
  120. // invalid, as they will be updated post-inlining.
  121. SmallPtrSet<const BasicBlock *, 4> LikelyToChangeBBs;
  122. // The CB BB will change - it'll either be split or the callee's body (single
  123. // BB) will be pasted in.
  124. LikelyToChangeBBs.insert(&CallSiteBB);
  125. // The caller's entry BB may change due to new alloca instructions.
  126. LikelyToChangeBBs.insert(&*Caller.begin());
  127. // The successors may become unreachable in the case of `invoke` inlining.
  128. // We track successors separately, too, because they form a boundary, together
  129. // with the CB BB ('Entry') between which the inlined callee will be pasted.
  130. Successors.insert(succ_begin(&CallSiteBB), succ_end(&CallSiteBB));
  131. // Inlining only handles invoke and calls. If this is an invoke, and inlining
  132. // it pulls another invoke, the original landing pad may get split, so as to
  133. // share its content with other potential users. So the edge up to which we
  134. // need to invalidate and then re-account BB data is the successors of the
  135. // current landing pad. We can leave the current lp, too - if it doesn't get
  136. // split, then it will be the place traversal stops. Either way, the
  137. // discounted BBs will be checked if reachable and re-added.
  138. if (const auto *II = dyn_cast<InvokeInst>(&CB)) {
  139. const auto *UnwindDest = II->getUnwindDest();
  140. Successors.insert(succ_begin(UnwindDest), succ_end(UnwindDest));
  141. }
  142. // Exclude the CallSiteBB, if it happens to be its own successor (1-BB loop).
  143. // We are only interested in BBs the graph moves past the callsite BB to
  144. // define the frontier past which we don't want to re-process BBs. Including
  145. // the callsite BB in this case would prematurely stop the traversal in
  146. // finish().
  147. Successors.erase(&CallSiteBB);
  148. for (const auto *BB : Successors)
  149. LikelyToChangeBBs.insert(BB);
  150. // Commit the change. While some of the BBs accounted for above may play dual
  151. // role - e.g. caller's entry BB may be the same as the callsite BB - set
  152. // insertion semantics make sure we account them once. This needs to be
  153. // followed in `finish`, too.
  154. for (const auto *BB : LikelyToChangeBBs)
  155. FPI.updateForBB(*BB, -1);
  156. }
  157. void FunctionPropertiesUpdater::finish(FunctionAnalysisManager &FAM) const {
  158. // Update feature values from the BBs that were copied from the callee, or
  159. // might have been modified because of inlining. The latter have been
  160. // subtracted in the FunctionPropertiesUpdater ctor.
  161. // There could be successors that were reached before but now are only
  162. // reachable from elsewhere in the CFG.
  163. // One example is the following diamond CFG (lines are arrows pointing down):
  164. // A
  165. // / \
  166. // B C
  167. // | |
  168. // | D
  169. // | |
  170. // | E
  171. // \ /
  172. // F
  173. // There's a call site in C that is inlined. Upon doing that, it turns out
  174. // it expands to
  175. // call void @llvm.trap()
  176. // unreachable
  177. // F isn't reachable from C anymore, but we did discount it when we set up
  178. // FunctionPropertiesUpdater, so we need to re-include it here.
  179. // At the same time, D and E were reachable before, but now are not anymore,
  180. // so we need to leave D out (we discounted it at setup), and explicitly
  181. // remove E.
  182. SetVector<const BasicBlock *> Reinclude;
  183. SetVector<const BasicBlock *> Unreachable;
  184. const auto &DT =
  185. FAM.getResult<DominatorTreeAnalysis>(const_cast<Function &>(Caller));
  186. if (&CallSiteBB != &*Caller.begin())
  187. Reinclude.insert(&*Caller.begin());
  188. // Distribute the successors to the 2 buckets.
  189. for (const auto *Succ : Successors)
  190. if (DT.isReachableFromEntry(Succ))
  191. Reinclude.insert(Succ);
  192. else
  193. Unreachable.insert(Succ);
  194. // For reinclusion, we want to stop at the reachable successors, who are at
  195. // the beginning of the worklist; but, starting from the callsite bb and
  196. // ending at those successors, we also want to perform a traversal.
  197. // IncludeSuccessorsMark is the index after which we include successors.
  198. const auto IncludeSuccessorsMark = Reinclude.size();
  199. bool CSInsertion = Reinclude.insert(&CallSiteBB);
  200. (void)CSInsertion;
  201. assert(CSInsertion);
  202. for (size_t I = 0; I < Reinclude.size(); ++I) {
  203. const auto *BB = Reinclude[I];
  204. FPI.reIncludeBB(*BB);
  205. if (I >= IncludeSuccessorsMark)
  206. Reinclude.insert(succ_begin(BB), succ_end(BB));
  207. }
  208. // For exclusion, we don't need to exclude the set of BBs that were successors
  209. // before and are now unreachable, because we already did that at setup. For
  210. // the rest, as long as a successor is unreachable, we want to explicitly
  211. // exclude it.
  212. const auto AlreadyExcludedMark = Unreachable.size();
  213. for (size_t I = 0; I < Unreachable.size(); ++I) {
  214. const auto *U = Unreachable[I];
  215. if (I >= AlreadyExcludedMark)
  216. FPI.updateForBB(*U, -1);
  217. for (const auto *Succ : successors(U))
  218. if (!DT.isReachableFromEntry(Succ))
  219. Unreachable.insert(Succ);
  220. }
  221. const auto &LI = FAM.getResult<LoopAnalysis>(const_cast<Function &>(Caller));
  222. FPI.updateAggregateStats(Caller, LI);
  223. assert(FPI == FunctionPropertiesInfo::getFunctionPropertiesInfo(Caller, FAM));
  224. }