1//===- Transforms/InstrProfiling.h - Instrumentation passes ---*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9/// \file
10/// This file provides the interface for LLVM's PGO Instrumentation lowering
11/// pass.
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_TRANSFORMS_INSTRPROFILING_H
15#define LLVM_TRANSFORMS_INSTRPROFILING_H
16
17#include "llvm/IR/IntrinsicInst.h"
18#include "llvm/IR/PassManager.h"
19#include "llvm/ProfileData/InstrProf.h"
20#include "llvm/Transforms/Instrumentation.h"
21
22namespace llvm {
23
24/// Instrumenation based profiling lowering pass. This pass lowers
25/// the profile instrumented code generated by FE or the IR based
26/// instrumentation pass.
27class InstrProfiling : public PassInfoMixin<InstrProfiling> {
28public:
29  InstrProfiling() {}
30  InstrProfiling(const InstrProfOptions &Options) : Options(Options) {}
31
32  PreservedAnalyses run(Module &M, AnalysisManager<Module> &AM);
33  bool run(Module &M);
34
35private:
36  InstrProfOptions Options;
37  Module *M;
38  struct PerFunctionProfileData {
39    uint32_t NumValueSites[IPVK_Last + 1];
40    GlobalVariable *RegionCounters;
41    GlobalVariable *DataVar;
42    PerFunctionProfileData() : RegionCounters(nullptr), DataVar(nullptr) {
43      memset(NumValueSites, 0, sizeof(uint32_t) * (IPVK_Last + 1));
44    }
45  };
46  DenseMap<GlobalVariable *, PerFunctionProfileData> ProfileDataMap;
47  std::vector<Value *> UsedVars;
48  std::vector<GlobalVariable *> ReferencedNames;
49  GlobalVariable *NamesVar;
50  size_t NamesSize;
51
52  bool isMachO() const;
53
54  /// Get the section name for the counter variables.
55  StringRef getCountersSection() const;
56
57  /// Get the section name for the name variables.
58  StringRef getNameSection() const;
59
60  /// Get the section name for the profile data variables.
61  StringRef getDataSection() const;
62
63  /// Get the section name for the coverage mapping data.
64  StringRef getCoverageSection() const;
65
66  /// Count the number of instrumented value sites for the function.
67  void computeNumValueSiteCounts(InstrProfValueProfileInst *Ins);
68
69  /// Replace instrprof_value_profile with a call to runtime library.
70  void lowerValueProfileInst(InstrProfValueProfileInst *Ins);
71
72  /// Replace instrprof_increment with an increment of the appropriate value.
73  void lowerIncrement(InstrProfIncrementInst *Inc);
74
75  /// Force emitting of name vars for unused functions.
76  void lowerCoverageData(GlobalVariable *CoverageNamesVar);
77
78  /// Get the region counters for an increment, creating them if necessary.
79  ///
80  /// If the counter array doesn't yet exist, the profile data variables
81  /// referring to them will also be created.
82  GlobalVariable *getOrCreateRegionCounters(InstrProfIncrementInst *Inc);
83
84  /// Emit the section with compressed function names.
85  void emitNameData();
86
87  /// Emit value nodes section for value profiling.
88  void emitVNodes();
89
90  /// Emit runtime registration functions for each profile data variable.
91  void emitRegistration();
92
93  /// Emit the necessary plumbing to pull in the runtime initialization.
94  void emitRuntimeHook();
95
96  /// Add uses of our data variables and runtime hook.
97  void emitUses();
98
99  /// Create a static initializer for our data, on platforms that need it,
100  /// and for any profile output file that was specified.
101  void emitInitialization();
102};
103
104} // End llvm namespace
105#endif
106