FunctionPropertiesAnalysis.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //=- FunctionPropertiesAnalysis.h - Function Properties Analysis --*- C++ -*-=//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This file defines the FunctionPropertiesInfo and FunctionPropertiesAnalysis
  15. // classes used to extract function properties.
  16. //
  17. //===----------------------------------------------------------------------===//
  18. #ifndef LLVM_ANALYSIS_FUNCTIONPROPERTIESANALYSIS_H
  19. #define LLVM_ANALYSIS_FUNCTIONPROPERTIESANALYSIS_H
  20. #include "llvm/ADT/SmallPtrSet.h"
  21. #include "llvm/ADT/iterator_range.h"
  22. #include "llvm/IR/InstrTypes.h"
  23. #include "llvm/IR/PassManager.h"
  24. namespace llvm {
  25. class Function;
  26. class LoopInfo;
  27. class FunctionPropertiesInfo {
  28. friend class FunctionPropertiesUpdater;
  29. void updateForBB(const BasicBlock &BB, int64_t Direction);
  30. void updateAggregateStats(const Function &F, const LoopInfo &LI);
  31. void reIncludeBB(const BasicBlock &BB);
  32. public:
  33. static FunctionPropertiesInfo
  34. getFunctionPropertiesInfo(const Function &F, FunctionAnalysisManager &FAM);
  35. bool operator==(const FunctionPropertiesInfo &FPI) const {
  36. return std::memcmp(this, &FPI, sizeof(FunctionPropertiesInfo)) == 0;
  37. }
  38. bool operator!=(const FunctionPropertiesInfo &FPI) const {
  39. return !(*this == FPI);
  40. }
  41. void print(raw_ostream &OS) const;
  42. /// Number of basic blocks
  43. int64_t BasicBlockCount = 0;
  44. /// Number of blocks reached from a conditional instruction, or that are
  45. /// 'cases' of a SwitchInstr.
  46. // FIXME: We may want to replace this with a more meaningful metric, like
  47. // number of conditionally executed blocks:
  48. // 'if (a) s();' would be counted here as 2 blocks, just like
  49. // 'if (a) s(); else s2(); s3();' would.
  50. int64_t BlocksReachedFromConditionalInstruction = 0;
  51. /// Number of uses of this function, plus 1 if the function is callable
  52. /// outside the module.
  53. int64_t Uses = 0;
  54. /// Number of direct calls made from this function to other functions
  55. /// defined in this module.
  56. int64_t DirectCallsToDefinedFunctions = 0;
  57. // Load Instruction Count
  58. int64_t LoadInstCount = 0;
  59. // Store Instruction Count
  60. int64_t StoreInstCount = 0;
  61. // Maximum Loop Depth in the Function
  62. int64_t MaxLoopDepth = 0;
  63. // Number of Top Level Loops in the Function
  64. int64_t TopLevelLoopCount = 0;
  65. // All non-debug instructions
  66. int64_t TotalInstructionCount = 0;
  67. };
  68. // Analysis pass
  69. class FunctionPropertiesAnalysis
  70. : public AnalysisInfoMixin<FunctionPropertiesAnalysis> {
  71. public:
  72. static AnalysisKey Key;
  73. using Result = const FunctionPropertiesInfo;
  74. FunctionPropertiesInfo run(Function &F, FunctionAnalysisManager &FAM);
  75. };
  76. /// Printer pass for the FunctionPropertiesAnalysis results.
  77. class FunctionPropertiesPrinterPass
  78. : public PassInfoMixin<FunctionPropertiesPrinterPass> {
  79. raw_ostream &OS;
  80. public:
  81. explicit FunctionPropertiesPrinterPass(raw_ostream &OS) : OS(OS) {}
  82. PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
  83. };
  84. /// Correctly update FunctionPropertiesInfo post-inlining. A
  85. /// FunctionPropertiesUpdater keeps the state necessary for tracking the changes
  86. /// llvm::InlineFunction makes. The idea is that inlining will at most modify
  87. /// a few BBs of the Caller (maybe the entry BB and definitely the callsite BB)
  88. /// and potentially affect exception handling BBs in the case of invoke
  89. /// inlining.
  90. class FunctionPropertiesUpdater {
  91. public:
  92. FunctionPropertiesUpdater(FunctionPropertiesInfo &FPI, const CallBase &CB);
  93. void finish(FunctionAnalysisManager &FAM) const;
  94. private:
  95. FunctionPropertiesInfo &FPI;
  96. const BasicBlock &CallSiteBB;
  97. const Function &Caller;
  98. DenseSet<const BasicBlock *> Successors;
  99. };
  100. } // namespace llvm
  101. #endif // LLVM_ANALYSIS_FUNCTIONPROPERTIESANALYSIS_H
  102. #ifdef __GNUC__
  103. #pragma GCC diagnostic pop
  104. #endif