InstrProfiling.h revision 360784
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_INSTRPROFILING_H
14#define LLVM_TRANSFORMS_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 <cstddef>
23#include <cstdint>
24#include <cstring>
25#include <vector>
26
27namespace llvm {
28
29class TargetLibraryInfo;
30using 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.
35class InstrProfiling : public PassInfoMixin<InstrProfiling> {
36public:
37  InstrProfiling() : IsCS(false) {}
38  InstrProfiling(const InstrProfOptions &Options, bool IsCS = false)
39      : Options(Options), IsCS(IsCS) {}
40
41  PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
42  bool run(Module &M,
43           std::function<const TargetLibraryInfo &(Function &F)> GetTLI);
44
45private:
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 *> UsedVars;
61  std::vector<GlobalVariable *> ReferencedNames;
62  GlobalVariable *NamesVar;
63  size_t NamesSize;
64
65  // Is this lowering for the context-sensitive instrumentation.
66  bool IsCS;
67
68  // vector of counter load/store pairs to be register promoted.
69  std::vector<LoadStorePair> PromotionCandidates;
70
71  // The start value of precise value profile range for memory intrinsic sizes.
72  int64_t MemOPSizeRangeStart;
73  // The end value of precise value profile range for memory intrinsic sizes.
74  int64_t MemOPSizeRangeLast;
75
76  int64_t TotalCountersPromoted = 0;
77
78  /// Lower instrumentation intrinsics in the function. Returns true if there
79  /// any lowering.
80  bool lowerIntrinsics(Function *F);
81
82  /// Register-promote counter loads and stores in loops.
83  void promoteCounterLoadStores(Function *F);
84
85  /// Returns true if profile counter update register promotion is enabled.
86  bool isCounterPromotionEnabled() const;
87
88  /// Count the number of instrumented value sites for the function.
89  void computeNumValueSiteCounts(InstrProfValueProfileInst *Ins);
90
91  /// Replace instrprof_value_profile with a call to runtime library.
92  void lowerValueProfileInst(InstrProfValueProfileInst *Ins);
93
94  /// Replace instrprof_increment with an increment of the appropriate value.
95  void lowerIncrement(InstrProfIncrementInst *Inc);
96
97  /// Force emitting of name vars for unused functions.
98  void lowerCoverageData(GlobalVariable *CoverageNamesVar);
99
100  /// Get the region counters for an increment, creating them if necessary.
101  ///
102  /// If the counter array doesn't yet exist, the profile data variables
103  /// referring to them will also be created.
104  GlobalVariable *getOrCreateRegionCounters(InstrProfIncrementInst *Inc);
105
106  /// Emit the section with compressed function names.
107  void emitNameData();
108
109  /// Emit value nodes section for value profiling.
110  void emitVNodes();
111
112  /// Emit runtime registration functions for each profile data variable.
113  void emitRegistration();
114
115  /// Emit the necessary plumbing to pull in the runtime initialization.
116  /// Returns true if a change was made.
117  bool emitRuntimeHook();
118
119  /// Add uses of our data variables and runtime hook.
120  void emitUses();
121
122  /// Create a static initializer for our data, on platforms that need it,
123  /// and for any profile output file that was specified.
124  void emitInitialization();
125};
126
127} // end namespace llvm
128
129#endif // LLVM_TRANSFORMS_INSTRPROFILING_H
130