LLVM  13.0.0git
InstrProfiling.h
Go to the documentation of this file.
1 //===- Transforms/Instrumentation/InstrProfiling.h --------------*- 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 /// \file
9 /// This file provides the interface for LLVM's PGO Instrumentation lowering
10 /// pass.
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_TRANSFORMS_INSTRUMENTATION_INSTRPROFILING_H
14 #define LLVM_TRANSFORMS_INSTRUMENTATION_INSTRPROFILING_H
15 
16 #include "llvm/ADT/DenseMap.h"
17 #include "llvm/ADT/StringRef.h"
18 #include "llvm/IR/IntrinsicInst.h"
19 #include "llvm/IR/PassManager.h"
22 #include <cstddef>
23 #include <cstdint>
24 #include <cstring>
25 #include <vector>
26 
27 namespace llvm {
28 
29 class TargetLibraryInfo;
30 using LoadStorePair = std::pair<Instruction *, Instruction *>;
31 
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> {
36 public:
37  InstrProfiling() : IsCS(false) {}
38  InstrProfiling(const InstrProfOptions &Options, bool IsCS = false)
39  : Options(Options), IsCS(IsCS) {}
40 
42  bool run(Module &M,
43  std::function<const TargetLibraryInfo &(Function &F)> GetTLI);
44 
45 private:
46  InstrProfOptions Options;
47  Module *M;
48  Triple TT;
49  std::function<const TargetLibraryInfo &(Function &F)> GetTLI;
50  struct PerFunctionProfileData {
51  uint32_t NumValueSites[IPVK_Last + 1];
52  GlobalVariable *RegionCounters = nullptr;
53  GlobalVariable *DataVar = nullptr;
54 
55  PerFunctionProfileData() {
56  memset(NumValueSites, 0, sizeof(uint32_t) * (IPVK_Last + 1));
57  }
58  };
59  DenseMap<GlobalVariable *, PerFunctionProfileData> ProfileDataMap;
60  std::vector<GlobalValue *> CompilerUsedVars;
61  std::vector<GlobalValue *> UsedVars;
62  std::vector<GlobalVariable *> ReferencedNames;
63  GlobalVariable *NamesVar;
64  size_t NamesSize;
65 
66  // Is this lowering for the context-sensitive instrumentation.
67  bool IsCS;
68 
69  // vector of counter load/store pairs to be register promoted.
70  std::vector<LoadStorePair> PromotionCandidates;
71 
72  int64_t TotalCountersPromoted = 0;
73 
74  /// Lower instrumentation intrinsics in the function. Returns true if there
75  /// any lowering.
76  bool lowerIntrinsics(Function *F);
77 
78  /// Register-promote counter loads and stores in loops.
79  void promoteCounterLoadStores(Function *F);
80 
81  /// Returns true if relocating counters at runtime is enabled.
82  bool isRuntimeCounterRelocationEnabled() const;
83 
84  /// Returns true if profile counter update register promotion is enabled.
85  bool isCounterPromotionEnabled() const;
86 
87  /// Count the number of instrumented value sites for the function.
88  void computeNumValueSiteCounts(InstrProfValueProfileInst *Ins);
89 
90  /// Replace instrprof_value_profile with a call to runtime library.
91  void lowerValueProfileInst(InstrProfValueProfileInst *Ins);
92 
93  /// Replace instrprof_increment with an increment of the appropriate value.
94  void lowerIncrement(InstrProfIncrementInst *Inc);
95 
96  /// Force emitting of name vars for unused functions.
97  void lowerCoverageData(GlobalVariable *CoverageNamesVar);
98 
99  /// Get the region counters for an increment, creating them if necessary.
100  ///
101  /// If the counter array doesn't yet exist, the profile data variables
102  /// referring to them will also be created.
103  GlobalVariable *getOrCreateRegionCounters(InstrProfIncrementInst *Inc);
104 
105  /// Emit the section with compressed function names.
106  void emitNameData();
107 
108  /// Emit value nodes section for value profiling.
109  void emitVNodes();
110 
111  /// Emit runtime registration functions for each profile data variable.
112  void emitRegistration();
113 
114  /// Emit the necessary plumbing to pull in the runtime initialization.
115  /// Returns true if a change was made.
116  bool emitRuntimeHook();
117 
118  /// Add uses of our data variables and runtime hook.
119  void emitUses();
120 
121  /// Create a static initializer for our data, on platforms that need it,
122  /// and for any profile output file that was specified.
123  void emitInitialization();
124 };
125 
126 } // end namespace llvm
127 
128 #endif // LLVM_TRANSFORMS_INSTRUMENTATION_INSTRPROFILING_H
llvm::PreservedAnalyses
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:155
Instrumentation.h
llvm
Definition: AllocatorList.h:23
M
We currently emits eax Perhaps this is what we really should generate is Is imull three or four cycles eax eax The current instruction priority is based on pattern complexity The former is more complex because it folds a load so the latter will not be emitted Perhaps we should use AddedComplexity to give LEA32r a higher priority We should always try to match LEA first since the LEA matching code does some estimate to determine whether the match is profitable if we care more about code then imull is better It s two bytes shorter than movl leal On a Pentium M
Definition: README.txt:252
IntrinsicInst.h
llvm::PassInfoMixin
A CRTP mix-in to automatically provide informational APIs needed for passes.
Definition: PassManager.h:374
llvm::Function
Definition: Function.h:61
StringRef.h
llvm::InstrProfiling::InstrProfiling
InstrProfiling()
Definition: InstrProfiling.h:37
llvm::GlobalVariable
Definition: GlobalVariable.h:40
llvm::Triple
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:45
DenseMap.h
llvm::InstrProfiling
Instrumentation based profiling lowering pass.
Definition: InstrProfiling.h:35
F
#define F(x, y, z)
Definition: MD5.cpp:56
false
Definition: StackSlotColoring.cpp:142
InstrProf.h
llvm::InstrProfOptions
Options for the frontend instrumentation based profiling pass.
Definition: Instrumentation.h:121
function
print Print MemDeps of function
Definition: MemDepPrinter.cpp:83
llvm::Module
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:67
llvm::LoadStorePair
std::pair< Instruction *, Instruction * > LoadStorePair
Definition: InstrProfiling.h:30
uint32_t
llvm::InstrProfiling::InstrProfiling
InstrProfiling(const InstrProfOptions &Options, bool IsCS=false)
Definition: InstrProfiling.h:38
PassManager.h
llvm::TargetLibraryInfo
Provides information about what library functions are available for the current target.
Definition: TargetLibraryInfo.h:208
llvm::InstrProfiling::run
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
Definition: InstrProfiling.cpp:411
llvm::MipsISD::Ins
@ Ins
Definition: MipsISelLowering.h:157
llvm::AnalysisManager
A container for analyses that lazily runs them and caches their results.
Definition: InstructionSimplify.h:44