1 //===- Transforms/InstrProfiling.h - Instrumentation passes ---*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 /// This file provides the interface for LLVM's PGO Instrumentation lowering
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_TRANSFORMS_INSTRPROFILING_H
15 #define LLVM_TRANSFORMS_INSTRPROFILING_H
17 #include "llvm/IR/IntrinsicInst.h"
18 #include "llvm/IR/PassManager.h"
19 #include "llvm/ProfileData/InstrProf.h"
20 #include "llvm/Transforms/Instrumentation.h"
24 class TargetLibraryInfo;
26 /// Instrumentation based profiling lowering pass. This pass lowers
27 /// the profile instrumented code generated by FE or the IR based
28 /// instrumentation pass.
29 class InstrProfiling : public PassInfoMixin<InstrProfiling> {
32 InstrProfiling(const InstrProfOptions &Options) : Options(Options) {}
34 PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
35 bool run(Module &M, const TargetLibraryInfo &TLI);
38 InstrProfOptions Options;
40 const TargetLibraryInfo *TLI;
41 struct PerFunctionProfileData {
42 uint32_t NumValueSites[IPVK_Last + 1];
43 GlobalVariable *RegionCounters;
44 GlobalVariable *DataVar;
45 PerFunctionProfileData() : RegionCounters(nullptr), DataVar(nullptr) {
46 memset(NumValueSites, 0, sizeof(uint32_t) * (IPVK_Last + 1));
49 DenseMap<GlobalVariable *, PerFunctionProfileData> ProfileDataMap;
50 std::vector<GlobalValue *> UsedVars;
51 std::vector<GlobalVariable *> ReferencedNames;
52 GlobalVariable *NamesVar;
57 /// Get the section name for the counter variables.
58 StringRef getCountersSection() const;
60 /// Get the section name for the name variables.
61 StringRef getNameSection() const;
63 /// Get the section name for the profile data variables.
64 StringRef getDataSection() const;
66 /// Get the section name for the coverage mapping data.
67 StringRef getCoverageSection() const;
69 /// Count the number of instrumented value sites for the function.
70 void computeNumValueSiteCounts(InstrProfValueProfileInst *Ins);
72 /// Replace instrprof_value_profile with a call to runtime library.
73 void lowerValueProfileInst(InstrProfValueProfileInst *Ins);
75 /// Replace instrprof_increment with an increment of the appropriate value.
76 void lowerIncrement(InstrProfIncrementInst *Inc);
78 /// Force emitting of name vars for unused functions.
79 void lowerCoverageData(GlobalVariable *CoverageNamesVar);
81 /// Get the region counters for an increment, creating them if necessary.
83 /// If the counter array doesn't yet exist, the profile data variables
84 /// referring to them will also be created.
85 GlobalVariable *getOrCreateRegionCounters(InstrProfIncrementInst *Inc);
87 /// Emit the section with compressed function names.
90 /// Emit value nodes section for value profiling.
93 /// Emit runtime registration functions for each profile data variable.
94 void emitRegistration();
96 /// Emit the necessary plumbing to pull in the runtime initialization.
97 void emitRuntimeHook();
99 /// Add uses of our data variables and runtime hook.
102 /// Create a static initializer for our data, on platforms that need it,
103 /// and for any profile output file that was specified.
104 void emitInitialization();
107 } // End llvm namespace