xref: /aosp_15_r20/external/llvm/include/llvm/Transforms/InstrProfiling.h (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker //===- Transforms/InstrProfiling.h - Instrumentation passes ---*- C++ -*-===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker //                     The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker /// \file
10*9880d681SAndroid Build Coastguard Worker /// This file provides the interface for LLVM's PGO Instrumentation lowering
11*9880d681SAndroid Build Coastguard Worker /// pass.
12*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
13*9880d681SAndroid Build Coastguard Worker 
14*9880d681SAndroid Build Coastguard Worker #ifndef LLVM_TRANSFORMS_INSTRPROFILING_H
15*9880d681SAndroid Build Coastguard Worker #define LLVM_TRANSFORMS_INSTRPROFILING_H
16*9880d681SAndroid Build Coastguard Worker 
17*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/IntrinsicInst.h"
18*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/PassManager.h"
19*9880d681SAndroid Build Coastguard Worker #include "llvm/ProfileData/InstrProf.h"
20*9880d681SAndroid Build Coastguard Worker #include "llvm/Transforms/Instrumentation.h"
21*9880d681SAndroid Build Coastguard Worker 
22*9880d681SAndroid Build Coastguard Worker namespace llvm {
23*9880d681SAndroid Build Coastguard Worker 
24*9880d681SAndroid Build Coastguard Worker /// Instrumenation based profiling lowering pass. This pass lowers
25*9880d681SAndroid Build Coastguard Worker /// the profile instrumented code generated by FE or the IR based
26*9880d681SAndroid Build Coastguard Worker /// instrumentation pass.
27*9880d681SAndroid Build Coastguard Worker class InstrProfiling : public PassInfoMixin<InstrProfiling> {
28*9880d681SAndroid Build Coastguard Worker public:
InstrProfiling()29*9880d681SAndroid Build Coastguard Worker   InstrProfiling() {}
InstrProfiling(const InstrProfOptions & Options)30*9880d681SAndroid Build Coastguard Worker   InstrProfiling(const InstrProfOptions &Options) : Options(Options) {}
31*9880d681SAndroid Build Coastguard Worker 
32*9880d681SAndroid Build Coastguard Worker   PreservedAnalyses run(Module &M, AnalysisManager<Module> &AM);
33*9880d681SAndroid Build Coastguard Worker   bool run(Module &M);
34*9880d681SAndroid Build Coastguard Worker 
35*9880d681SAndroid Build Coastguard Worker private:
36*9880d681SAndroid Build Coastguard Worker   InstrProfOptions Options;
37*9880d681SAndroid Build Coastguard Worker   Module *M;
38*9880d681SAndroid Build Coastguard Worker   struct PerFunctionProfileData {
39*9880d681SAndroid Build Coastguard Worker     uint32_t NumValueSites[IPVK_Last + 1];
40*9880d681SAndroid Build Coastguard Worker     GlobalVariable *RegionCounters;
41*9880d681SAndroid Build Coastguard Worker     GlobalVariable *DataVar;
PerFunctionProfileDataPerFunctionProfileData42*9880d681SAndroid Build Coastguard Worker     PerFunctionProfileData() : RegionCounters(nullptr), DataVar(nullptr) {
43*9880d681SAndroid Build Coastguard Worker       memset(NumValueSites, 0, sizeof(uint32_t) * (IPVK_Last + 1));
44*9880d681SAndroid Build Coastguard Worker     }
45*9880d681SAndroid Build Coastguard Worker   };
46*9880d681SAndroid Build Coastguard Worker   DenseMap<GlobalVariable *, PerFunctionProfileData> ProfileDataMap;
47*9880d681SAndroid Build Coastguard Worker   std::vector<Value *> UsedVars;
48*9880d681SAndroid Build Coastguard Worker   std::vector<GlobalVariable *> ReferencedNames;
49*9880d681SAndroid Build Coastguard Worker   GlobalVariable *NamesVar;
50*9880d681SAndroid Build Coastguard Worker   size_t NamesSize;
51*9880d681SAndroid Build Coastguard Worker 
52*9880d681SAndroid Build Coastguard Worker   bool isMachO() const;
53*9880d681SAndroid Build Coastguard Worker 
54*9880d681SAndroid Build Coastguard Worker   /// Get the section name for the counter variables.
55*9880d681SAndroid Build Coastguard Worker   StringRef getCountersSection() const;
56*9880d681SAndroid Build Coastguard Worker 
57*9880d681SAndroid Build Coastguard Worker   /// Get the section name for the name variables.
58*9880d681SAndroid Build Coastguard Worker   StringRef getNameSection() const;
59*9880d681SAndroid Build Coastguard Worker 
60*9880d681SAndroid Build Coastguard Worker   /// Get the section name for the profile data variables.
61*9880d681SAndroid Build Coastguard Worker   StringRef getDataSection() const;
62*9880d681SAndroid Build Coastguard Worker 
63*9880d681SAndroid Build Coastguard Worker   /// Get the section name for the coverage mapping data.
64*9880d681SAndroid Build Coastguard Worker   StringRef getCoverageSection() const;
65*9880d681SAndroid Build Coastguard Worker 
66*9880d681SAndroid Build Coastguard Worker   /// Count the number of instrumented value sites for the function.
67*9880d681SAndroid Build Coastguard Worker   void computeNumValueSiteCounts(InstrProfValueProfileInst *Ins);
68*9880d681SAndroid Build Coastguard Worker 
69*9880d681SAndroid Build Coastguard Worker   /// Replace instrprof_value_profile with a call to runtime library.
70*9880d681SAndroid Build Coastguard Worker   void lowerValueProfileInst(InstrProfValueProfileInst *Ins);
71*9880d681SAndroid Build Coastguard Worker 
72*9880d681SAndroid Build Coastguard Worker   /// Replace instrprof_increment with an increment of the appropriate value.
73*9880d681SAndroid Build Coastguard Worker   void lowerIncrement(InstrProfIncrementInst *Inc);
74*9880d681SAndroid Build Coastguard Worker 
75*9880d681SAndroid Build Coastguard Worker   /// Force emitting of name vars for unused functions.
76*9880d681SAndroid Build Coastguard Worker   void lowerCoverageData(GlobalVariable *CoverageNamesVar);
77*9880d681SAndroid Build Coastguard Worker 
78*9880d681SAndroid Build Coastguard Worker   /// Get the region counters for an increment, creating them if necessary.
79*9880d681SAndroid Build Coastguard Worker   ///
80*9880d681SAndroid Build Coastguard Worker   /// If the counter array doesn't yet exist, the profile data variables
81*9880d681SAndroid Build Coastguard Worker   /// referring to them will also be created.
82*9880d681SAndroid Build Coastguard Worker   GlobalVariable *getOrCreateRegionCounters(InstrProfIncrementInst *Inc);
83*9880d681SAndroid Build Coastguard Worker 
84*9880d681SAndroid Build Coastguard Worker   /// Emit the section with compressed function names.
85*9880d681SAndroid Build Coastguard Worker   void emitNameData();
86*9880d681SAndroid Build Coastguard Worker 
87*9880d681SAndroid Build Coastguard Worker   /// Emit value nodes section for value profiling.
88*9880d681SAndroid Build Coastguard Worker   void emitVNodes();
89*9880d681SAndroid Build Coastguard Worker 
90*9880d681SAndroid Build Coastguard Worker   /// Emit runtime registration functions for each profile data variable.
91*9880d681SAndroid Build Coastguard Worker   void emitRegistration();
92*9880d681SAndroid Build Coastguard Worker 
93*9880d681SAndroid Build Coastguard Worker   /// Emit the necessary plumbing to pull in the runtime initialization.
94*9880d681SAndroid Build Coastguard Worker   void emitRuntimeHook();
95*9880d681SAndroid Build Coastguard Worker 
96*9880d681SAndroid Build Coastguard Worker   /// Add uses of our data variables and runtime hook.
97*9880d681SAndroid Build Coastguard Worker   void emitUses();
98*9880d681SAndroid Build Coastguard Worker 
99*9880d681SAndroid Build Coastguard Worker   /// Create a static initializer for our data, on platforms that need it,
100*9880d681SAndroid Build Coastguard Worker   /// and for any profile output file that was specified.
101*9880d681SAndroid Build Coastguard Worker   void emitInitialization();
102*9880d681SAndroid Build Coastguard Worker };
103*9880d681SAndroid Build Coastguard Worker 
104*9880d681SAndroid Build Coastguard Worker } // End llvm namespace
105*9880d681SAndroid Build Coastguard Worker #endif
106