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