FunctionPropertiesAnalysis.h 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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_FUNCTIONPROPERTIESANALYSIS_H_
  19. #define LLVM_FUNCTIONPROPERTIESANALYSIS_H_
  20. #include "llvm/Analysis/LoopInfo.h"
  21. #include "llvm/IR/PassManager.h"
  22. namespace llvm {
  23. class Function;
  24. class FunctionPropertiesInfo {
  25. public:
  26. static FunctionPropertiesInfo getFunctionPropertiesInfo(const Function &F,
  27. const LoopInfo &LI);
  28. void print(raw_ostream &OS) const;
  29. /// Number of basic blocks
  30. int64_t BasicBlockCount = 0;
  31. /// Number of blocks reached from a conditional instruction, or that are
  32. /// 'cases' of a SwitchInstr.
  33. // FIXME: We may want to replace this with a more meaningful metric, like
  34. // number of conditionally executed blocks:
  35. // 'if (a) s();' would be counted here as 2 blocks, just like
  36. // 'if (a) s(); else s2(); s3();' would.
  37. int64_t BlocksReachedFromConditionalInstruction = 0;
  38. /// Number of uses of this function, plus 1 if the function is callable
  39. /// outside the module.
  40. int64_t Uses = 0;
  41. /// Number of direct calls made from this function to other functions
  42. /// defined in this module.
  43. int64_t DirectCallsToDefinedFunctions = 0;
  44. // Load Instruction Count
  45. int64_t LoadInstCount = 0;
  46. // Store Instruction Count
  47. int64_t StoreInstCount = 0;
  48. // Maximum Loop Depth in the Function
  49. int64_t MaxLoopDepth = 0;
  50. // Number of Top Level Loops in the Function
  51. int64_t TopLevelLoopCount = 0;
  52. };
  53. // Analysis pass
  54. class FunctionPropertiesAnalysis
  55. : public AnalysisInfoMixin<FunctionPropertiesAnalysis> {
  56. public:
  57. static AnalysisKey Key;
  58. using Result = FunctionPropertiesInfo;
  59. Result run(Function &F, FunctionAnalysisManager &FAM);
  60. };
  61. /// Printer pass for the FunctionPropertiesAnalysis results.
  62. class FunctionPropertiesPrinterPass
  63. : public PassInfoMixin<FunctionPropertiesPrinterPass> {
  64. raw_ostream &OS;
  65. public:
  66. explicit FunctionPropertiesPrinterPass(raw_ostream &OS) : OS(OS) {}
  67. PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
  68. };
  69. } // namespace llvm
  70. #endif // LLVM_FUNCTIONPROPERTIESANALYSIS_H_
  71. #ifdef __GNUC__
  72. #pragma GCC diagnostic pop
  73. #endif