FunctionPropertiesAnalysis.h 2.6 KB

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