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"
20 #include "llvm/ProfileData/InstrProf.h"
21 #include "llvm/Transforms/Instrumentation.h"
22 #include <cstdint>
23 #include <cstring>
24 #include <vector>
25 
26 namespace llvm {
27 
28 class TargetLibraryInfo;
29 using LoadStorePair = std::pair<Instruction *, Instruction *>;
30 
31 /// Instrumentation based profiling lowering pass. This pass lowers
32 /// the profile instrumented code generated by FE or the IR based
33 /// instrumentation pass.
34 class InstrProfiling : public PassInfoMixin<InstrProfiling> {
35 public:
36   InstrProfiling() : IsCS(false) {}
37   InstrProfiling(const InstrProfOptions &Options, bool IsCS = false)
38       : Options(Options), IsCS(IsCS) {}
39 
40   PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
41   bool run(Module &M,
42            std::function<const TargetLibraryInfo &(Function &F)> GetTLI);
43 
44 private:
45   InstrProfOptions Options;
46   Module *M;
47   Triple TT;
48   std::function<const TargetLibraryInfo &(Function &F)> GetTLI;
49   struct PerFunctionProfileData {
50     uint32_t NumValueSites[IPVK_Last + 1];
51     GlobalVariable *RegionCounters = nullptr;
52     GlobalVariable *DataVar = nullptr;
53 
54     PerFunctionProfileData() {
55       memset(NumValueSites, 0, sizeof(uint32_t) * (IPVK_Last + 1));
56     }
57   };
58   DenseMap<GlobalVariable *, PerFunctionProfileData> ProfileDataMap;
59   /// If runtime relocation is enabled, this maps functions to the load
60   /// instruction that produces the profile relocation bias.
61   DenseMap<const Function *, LoadInst *> FunctionToProfileBiasMap;
62   std::vector<GlobalValue *> CompilerUsedVars;
63   std::vector<GlobalValue *> UsedVars;
64   std::vector<GlobalVariable *> ReferencedNames;
65   GlobalVariable *NamesVar;
66   size_t NamesSize;
67 
68   // Is this lowering for the context-sensitive instrumentation.
69   bool IsCS;
70 
71   // vector of counter load/store pairs to be register promoted.
72   std::vector<LoadStorePair> PromotionCandidates;
73 
74   int64_t TotalCountersPromoted = 0;
75 
76   /// Lower instrumentation intrinsics in the function. Returns true if there
77   /// any lowering.
78   bool lowerIntrinsics(Function *F);
79 
80   /// Register-promote counter loads and stores in loops.
81   void promoteCounterLoadStores(Function *F);
82 
83   /// Returns true if relocating counters at runtime is enabled.
84   bool isRuntimeCounterRelocationEnabled() const;
85 
86   /// Returns true if profile counter update register promotion is enabled.
87   bool isCounterPromotionEnabled() const;
88 
89   /// Count the number of instrumented value sites for the function.
90   void computeNumValueSiteCounts(InstrProfValueProfileInst *Ins);
91 
92   /// Replace instrprof.value.profile with a call to runtime library.
93   void lowerValueProfileInst(InstrProfValueProfileInst *Ins);
94 
95   /// Replace instrprof.cover with a store instruction to the coverage byte.
96   void lowerCover(InstrProfCoverInst *Inc);
97 
98   /// Replace instrprof.timestamp with a call to
99   /// INSTR_PROF_PROFILE_SET_TIMESTAMP.
100   void lowerTimestamp(InstrProfTimestampInst *TimestampInstruction);
101 
102   /// Replace instrprof.increment with an increment of the appropriate value.
103   void lowerIncrement(InstrProfIncrementInst *Inc);
104 
105   /// Force emitting of name vars for unused functions.
106   void lowerCoverageData(GlobalVariable *CoverageNamesVar);
107 
108   /// Compute the address of the counter value that this profiling instruction
109   /// acts on.
110   Value *getCounterAddress(InstrProfInstBase *I);
111 
112   /// Get the region counters for an increment, creating them if necessary.
113   ///
114   /// If the counter array doesn't yet exist, the profile data variables
115   /// referring to them will also be created.
116   GlobalVariable *getOrCreateRegionCounters(InstrProfInstBase *Inc);
117 
118   /// Create the region counters.
119   GlobalVariable *createRegionCounters(InstrProfInstBase *Inc, StringRef Name,
120                                        GlobalValue::LinkageTypes Linkage);
121 
122   /// Emit the section with compressed function names.
123   void emitNameData();
124 
125   /// Emit value nodes section for value profiling.
126   void emitVNodes();
127 
128   /// Emit runtime registration functions for each profile data variable.
129   void emitRegistration();
130 
131   /// Emit the necessary plumbing to pull in the runtime initialization.
132   /// Returns true if a change was made.
133   bool emitRuntimeHook();
134 
135   /// Add uses of our data variables and runtime hook.
136   void emitUses();
137 
138   /// Create a static initializer for our data, on platforms that need it,
139   /// and for any profile output file that was specified.
140   void emitInitialization();
141 };
142 
143 } // end namespace llvm
144 
145 #endif // LLVM_TRANSFORMS_INSTRUMENTATION_INSTRPROFILING_H
146