1 //===- Transforms/Instrumentation/InstrProfiling.h --------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 /// This file provides the interface for LLVM's PGO Instrumentation lowering
11 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_TRANSFORMS_INSTRPROFILING_H
14 #define LLVM_TRANSFORMS_INSTRPROFILING_H
16 #include "llvm/ADT/DenseMap.h"
17 #include "llvm/ADT/StringRef.h"
18 #include "llvm/IR/IntrinsicInst.h"
19 #include "llvm/IR/PassManager.h"
20 #include "llvm/ProfileData/InstrProf.h"
21 #include "llvm/Transforms/Instrumentation.h"
29 class TargetLibraryInfo
;
30 using LoadStorePair
= std::pair
<Instruction
*, Instruction
*>;
32 /// Instrumentation based profiling lowering pass. This pass lowers
33 /// the profile instrumented code generated by FE or the IR based
34 /// instrumentation pass.
35 class InstrProfiling
: public PassInfoMixin
<InstrProfiling
> {
37 InstrProfiling() = default;
38 InstrProfiling(const InstrProfOptions
&Options
) : Options(Options
) {}
40 PreservedAnalyses
run(Module
&M
, ModuleAnalysisManager
&AM
);
41 bool run(Module
&M
, const TargetLibraryInfo
&TLI
);
44 InstrProfOptions Options
;
47 const TargetLibraryInfo
*TLI
;
48 struct PerFunctionProfileData
{
49 uint32_t NumValueSites
[IPVK_Last
+ 1];
50 GlobalVariable
*RegionCounters
= nullptr;
51 GlobalVariable
*DataVar
= nullptr;
53 PerFunctionProfileData() {
54 memset(NumValueSites
, 0, sizeof(uint32_t) * (IPVK_Last
+ 1));
57 DenseMap
<GlobalVariable
*, PerFunctionProfileData
> ProfileDataMap
;
58 std::vector
<GlobalValue
*> UsedVars
;
59 std::vector
<GlobalVariable
*> ReferencedNames
;
60 GlobalVariable
*NamesVar
;
63 // vector of counter load/store pairs to be register promoted.
64 std::vector
<LoadStorePair
> PromotionCandidates
;
66 // The start value of precise value profile range for memory intrinsic sizes.
67 int64_t MemOPSizeRangeStart
;
68 // The end value of precise value profile range for memory intrinsic sizes.
69 int64_t MemOPSizeRangeLast
;
71 int64_t TotalCountersPromoted
= 0;
73 /// Lower instrumentation intrinsics in the function. Returns true if there
75 bool lowerIntrinsics(Function
*F
);
77 /// Register-promote counter loads and stores in loops.
78 void promoteCounterLoadStores(Function
*F
);
80 /// Returns true if profile counter update register promotion is enabled.
81 bool isCounterPromotionEnabled() const;
83 /// Count the number of instrumented value sites for the function.
84 void computeNumValueSiteCounts(InstrProfValueProfileInst
*Ins
);
86 /// Replace instrprof_value_profile with a call to runtime library.
87 void lowerValueProfileInst(InstrProfValueProfileInst
*Ins
);
89 /// Replace instrprof_increment with an increment of the appropriate value.
90 void lowerIncrement(InstrProfIncrementInst
*Inc
);
92 /// Force emitting of name vars for unused functions.
93 void lowerCoverageData(GlobalVariable
*CoverageNamesVar
);
95 /// Get the region counters for an increment, creating them if necessary.
97 /// If the counter array doesn't yet exist, the profile data variables
98 /// referring to them will also be created.
99 GlobalVariable
*getOrCreateRegionCounters(InstrProfIncrementInst
*Inc
);
101 /// Emit the section with compressed function names.
104 /// Emit value nodes section for value profiling.
107 /// Emit runtime registration functions for each profile data variable.
108 void emitRegistration();
110 /// Emit the necessary plumbing to pull in the runtime initialization.
111 /// Returns true if a change was made.
112 bool emitRuntimeHook();
114 /// Add uses of our data variables and runtime hook.
117 /// Create a static initializer for our data, on platforms that need it,
118 /// and for any profile output file that was specified.
119 void emitInitialization();
122 } // end namespace llvm
124 #endif // LLVM_TRANSFORMS_INSTRPROFILING_H