1 //===- Transforms/Instrumentation/InstrProfiling.h --------------*- 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/ADT/DenseMap.h"
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/IR/IntrinsicInst.h"
20 #include "llvm/IR/PassManager.h"
21 #include "llvm/ProfileData/InstrProf.h"
22 #include "llvm/Transforms/Instrumentation.h"
30 class TargetLibraryInfo;
31 using LoadStorePair = std::pair<Instruction *, Instruction *>;
33 /// Instrumentation based profiling lowering pass. This pass lowers
34 /// the profile instrumented code generated by FE or the IR based
35 /// instrumentation pass.
36 class InstrProfiling : public PassInfoMixin<InstrProfiling> {
38 InstrProfiling() = default;
39 InstrProfiling(const InstrProfOptions &Options) : Options(Options) {}
41 PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
42 bool run(Module &M, const TargetLibraryInfo &TLI);
45 InstrProfOptions Options;
48 const TargetLibraryInfo *TLI;
49 struct PerFunctionProfileData {
50 uint32_t NumValueSites[IPVK_Last + 1];
51 GlobalVariable *RegionCounters = nullptr;
52 GlobalVariable *DataVar = nullptr;
54 PerFunctionProfileData() {
55 memset(NumValueSites, 0, sizeof(uint32_t) * (IPVK_Last + 1));
58 DenseMap<GlobalVariable *, PerFunctionProfileData> ProfileDataMap;
59 std::vector<GlobalValue *> UsedVars;
60 std::vector<GlobalVariable *> ReferencedNames;
61 GlobalVariable *NamesVar;
64 // vector of counter load/store pairs to be register promoted.
65 std::vector<LoadStorePair> PromotionCandidates;
67 // The start value of precise value profile range for memory intrinsic sizes.
68 int64_t MemOPSizeRangeStart;
69 // The end value of precise value profile range for memory intrinsic sizes.
70 int64_t MemOPSizeRangeLast;
72 int64_t TotalCountersPromoted = 0;
74 /// Lower instrumentation intrinsics in the function. Returns true if there
76 bool lowerIntrinsics(Function *F);
78 /// Register-promote counter loads and stores in loops.
79 void promoteCounterLoadStores(Function *F);
81 /// Returns true if profile counter update register promotion is enabled.
82 bool isCounterPromotionEnabled() const;
84 /// Count the number of instrumented value sites for the function.
85 void computeNumValueSiteCounts(InstrProfValueProfileInst *Ins);
87 /// Replace instrprof_value_profile with a call to runtime library.
88 void lowerValueProfileInst(InstrProfValueProfileInst *Ins);
90 /// Replace instrprof_increment with an increment of the appropriate value.
91 void lowerIncrement(InstrProfIncrementInst *Inc);
93 /// Force emitting of name vars for unused functions.
94 void lowerCoverageData(GlobalVariable *CoverageNamesVar);
96 /// Get the region counters for an increment, creating them if necessary.
98 /// If the counter array doesn't yet exist, the profile data variables
99 /// referring to them will also be created.
100 GlobalVariable *getOrCreateRegionCounters(InstrProfIncrementInst *Inc);
102 /// Emit the section with compressed function names.
105 /// Emit value nodes section for value profiling.
108 /// Emit runtime registration functions for each profile data variable.
109 void emitRegistration();
111 /// Emit the necessary plumbing to pull in the runtime initialization.
112 /// Returns true if a change was made.
113 bool emitRuntimeHook();
115 /// Add uses of our data variables and runtime hook.
118 /// Create a static initializer for our data, on platforms that need it,
119 /// and for any profile output file that was specified.
120 void emitInitialization();
123 } // end namespace llvm
125 #endif // LLVM_TRANSFORMS_INSTRPROFILING_H