llvm.org GIT mirror llvm / 6be977a
Port InstrProfiling pass to the new pass manager Differential Revision: http://reviews.llvm.org/D18126 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@266637 91177308-0d34-0410-b5e6-96231b3b80d8 Xinliang David Li 4 years ago
14 changed file(s) with 60 addition(s) and 96 deletion(s). Raw diff Collapse all Expand all
123123 void initializeGCOVProfilerPass(PassRegistry&);
124124 void initializePGOInstrumentationGenPass(PassRegistry&);
125125 void initializePGOInstrumentationUsePass(PassRegistry&);
126 void initializeInstrProfilingPass(PassRegistry&);
126 void initializeInstrProfilingLegacyPassPass(PassRegistry &);
127127 void initializeAddressSanitizerPass(PassRegistry&);
128128 void initializeAddressSanitizerModulePass(PassRegistry&);
129129 void initializeMemorySanitizerPass(PassRegistry&);
9090 (void) llvm::createGCOVProfilerPass();
9191 (void) llvm::createPGOInstrumentationGenPass();
9292 (void) llvm::createPGOInstrumentationUsePass();
93 (void) llvm::createInstrProfilingPass();
93 (void) llvm::createInstrProfilingLegacyPass();
9494 (void) llvm::createFunctionImportPass();
9595 (void) llvm::createFunctionInliningPass();
9696 (void) llvm::createAlwaysInlinerPass();
9595 };
9696
9797 /// Insert frontend instrumentation based profiling.
98 ModulePass *createInstrProfilingPass(
98 ModulePass *createInstrProfilingLegacyPass(
9999 const InstrProfOptions &Options = InstrProfOptions());
100100
101101 // Insert AddressSanitizer (address sanity checking) instrumentation
4848 #include "llvm/Transforms/IPO/InferFunctionAttrs.h"
4949 #include "llvm/Transforms/IPO/StripDeadPrototypes.h"
5050 #include "llvm/Transforms/InstCombine/InstCombine.h"
51 #include "llvm/Transforms/InstrProfiling.h"
5152 #include "llvm/Transforms/Scalar/ADCE.h"
5253 #include "llvm/Transforms/Scalar/EarlyCSE.h"
5354 #include "llvm/Transforms/Scalar/LowerExpectIntrinsic.h"
3636 #endif
3737 MODULE_PASS("forceattrs", ForceFunctionAttrsPass())
3838 MODULE_PASS("inferattrs", InferFunctionAttrsPass())
39 MODULE_PASS("instrprof", InstrProfiling())
3940 MODULE_PASS("invalidate", InvalidateAllAnalysesPass())
4041 MODULE_PASS("no-op-module", NoOpModulePass())
4142 MODULE_PASS("print", PrintModulePass(dbgs()))
219219 // Add the profile lowering pass.
220220 InstrProfOptions Options;
221221 Options.InstrProfileOutput = PGOInstrGen;
222 MPM.add(createInstrProfilingPass(Options));
222 MPM.add(createInstrProfilingLegacyPass(Options));
223223 }
224224 if (!PGOInstrUse.empty())
225225 MPM.add(createPGOInstrumentationUsePass(PGOInstrUse));
1717 #include "llvm/IR/IntrinsicInst.h"
1818 #include "llvm/IR/Module.h"
1919 #include "llvm/ProfileData/InstrProf.h"
20 #include "llvm/Transforms/Instrumentation.h"
20 #include "llvm/Transforms/InstrProfiling.h"
2121 #include "llvm/Transforms/Utils/ModuleUtils.h"
2222
2323 using namespace llvm;
3030 cl::desc("Enable name string compression"),
3131 cl::init(true));
3232
33 class InstrProfiling : public ModulePass {
33 class InstrProfilingLegacyPass : public ModulePass {
34 InstrProfiling InstrProf;
35
3436 public:
3537 static char ID;
36
37 InstrProfiling() : ModulePass(ID) {}
38
39 InstrProfiling(const InstrProfOptions &Options)
40 : ModulePass(ID), Options(Options) {}
41
38 InstrProfilingLegacyPass() : ModulePass(ID), InstrProf() {}
39 InstrProfilingLegacyPass(const InstrProfOptions &Options)
40 : ModulePass(ID), InstrProf(Options) {}
4241 const char *getPassName() const override {
4342 return "Frontend instrumentation-based coverage lowering";
4443 }
4544
46 bool runOnModule(Module &M) override;
45 bool runOnModule(Module &M) override { return InstrProf.run(M); }
4746
4847 void getAnalysisUsage(AnalysisUsage &AU) const override {
4948 AU.setPreservesCFG();
5049 }
51
52 private:
53 InstrProfOptions Options;
54 Module *M;
55 typedef struct PerFunctionProfileData {
56 uint32_t NumValueSites[IPVK_Last+1];
57 GlobalVariable* RegionCounters;
58 GlobalVariable* DataVar;
59 PerFunctionProfileData() : RegionCounters(nullptr), DataVar(nullptr) {
60 memset(NumValueSites, 0, sizeof(uint32_t) * (IPVK_Last+1));
61 }
62 } PerFunctionProfileData;
63 DenseMap ProfileDataMap;
64 std::vector UsedVars;
65 std::vector ReferencedNames;
66 GlobalVariable *NamesVar;
67 size_t NamesSize;
68
69 bool isMachO() const {
70 return Triple(M->getTargetTriple()).isOSBinFormatMachO();
71 }
72
73 /// Get the section name for the counter variables.
74 StringRef getCountersSection() const {
75 return getInstrProfCountersSectionName(isMachO());
76 }
77
78 /// Get the section name for the name variables.
79 StringRef getNameSection() const {
80 return getInstrProfNameSectionName(isMachO());
81 }
82
83 /// Get the section name for the profile data variables.
84 StringRef getDataSection() const {
85 return getInstrProfDataSectionName(isMachO());
86 }
87
88 /// Get the section name for the coverage mapping data.
89 StringRef getCoverageSection() const {
90 return getInstrProfCoverageSectionName(isMachO());
91 }
92
93 /// Count the number of instrumented value sites for the function.
94 void computeNumValueSiteCounts(InstrProfValueProfileInst *Ins);
95
96 /// Replace instrprof_value_profile with a call to runtime library.
97 void lowerValueProfileInst(InstrProfValueProfileInst *Ins);
98
99 /// Replace instrprof_increment with an increment of the appropriate value.
100 void lowerIncrement(InstrProfIncrementInst *Inc);
101
102 /// Force emitting of name vars for unused functions.
103 void lowerCoverageData(GlobalVariable *CoverageNamesVar);
104
105 /// Get the region counters for an increment, creating them if necessary.
106 ///
107 /// If the counter array doesn't yet exist, the profile data variables
108 /// referring to them will also be created.
109 GlobalVariable *getOrCreateRegionCounters(InstrProfIncrementInst *Inc);
110
111 /// Emit the section with compressed function names.
112 void emitNameData();
113
114 /// Emit runtime registration functions for each profile data variable.
115 void emitRegistration();
116
117 /// Emit the necessary plumbing to pull in the runtime initialization.
118 void emitRuntimeHook();
119
120 /// Add uses of our data variables and runtime hook.
121 void emitUses();
122
123 /// Create a static initializer for our data, on platforms that need it,
124 /// and for any profile output file that was specified.
125 void emitInitialization();
12650 };
12751
12852 } // anonymous namespace
12953
130 char InstrProfiling::ID = 0;
131 INITIALIZE_PASS(InstrProfiling, "instrprof",
54 PreservedAnalyses InstrProfiling::run(Module &M, AnalysisManager &AM) {
55 if (!run(M))
56 return PreservedAnalyses::all();
57
58 return PreservedAnalyses::none();
59 }
60
61 char InstrProfilingLegacyPass::ID = 0;
62 INITIALIZE_PASS(InstrProfilingLegacyPass, "instrprof",
13263 "Frontend instrumentation-based coverage lowering.", false,
13364 false)
13465
135 ModulePass *llvm::createInstrProfilingPass(const InstrProfOptions &Options) {
136 return new InstrProfiling(Options);
137 }
138
139 bool InstrProfiling::runOnModule(Module &M) {
66 ModulePass *llvm::createInstrProfilingLegacyPass(const InstrProfOptions &Options) {
67 return new InstrProfilingLegacyPass(Options);
68 }
69
70 bool InstrProfiling::isMachO() const {
71 return Triple(M->getTargetTriple()).isOSBinFormatMachO();
72 }
73
74 /// Get the section name for the counter variables.
75 StringRef InstrProfiling::getCountersSection() const {
76 return getInstrProfCountersSectionName(isMachO());
77 }
78
79 /// Get the section name for the name variables.
80 StringRef InstrProfiling::getNameSection() const {
81 return getInstrProfNameSectionName(isMachO());
82 }
83
84 /// Get the section name for the profile data variables.
85 StringRef InstrProfiling::getDataSection() const {
86 return getInstrProfDataSectionName(isMachO());
87 }
88
89 /// Get the section name for the coverage mapping data.
90 StringRef InstrProfiling::getCoverageSection() const {
91 return getInstrProfCoverageSectionName(isMachO());
92 }
93
94 bool InstrProfiling::run(Module &M) {
14095 bool MadeChange = false;
14196
14297 this->M = &M;
6161 initializeGCOVProfilerPass(Registry);
6262 initializePGOInstrumentationGenPass(Registry);
6363 initializePGOInstrumentationUsePass(Registry);
64 initializeInstrProfilingPass(Registry);
64 initializeInstrProfilingLegacyPassPass(Registry);
6565 initializeMemorySanitizerPass(Registry);
6666 initializeThreadSanitizerPass(Registry);
6767 initializeSanitizerCoverageModulePass(Registry);
33
44 ; RUN: opt < %s -mtriple=x86_64-apple-macosx10.10.0 -instrprof -S | FileCheck %s
55 ; RUN: opt < %s -mtriple=x86_64-unknown-linux -instrprof -S | FileCheck %s
6 ; RUN: opt < %s -mtriple=x86_64-unknown-linux -passes=instrprof -S | FileCheck %s
67 ; RUN: opt < %s -mtriple=x86_64-pc-win32-coff -instrprof -S | FileCheck %s --check-prefix=COFF
78
89 $_Z3barIvEvv = comdat any
11
22 ; RUN: opt < %s -mtriple=x86_64-apple-macosx10.10.0 -instrprof -S | FileCheck %s --check-prefix=OTHER --check-prefix=COMMON
33 ; RUN: opt < %s -mtriple=x86_64-unknown-linux -instrprof -S | FileCheck %s --check-prefix=LINUX --check-prefix=COMMON
4 ; RUN: opt < %s -mtriple=x86_64-apple-macosx10.10.0 -passes=instrprof -S | FileCheck %s --check-prefix=OTHER --check-prefix=COMMON
5 ; RUN: opt < %s -mtriple=x86_64-unknown-linux -passes=instrprof -S | FileCheck %s --check-prefix=LINUX --check-prefix=COMMON
46
57 @__profn_foo = hidden constant [3 x i8] c"foo"
68 @__profn_foo_weak = weak hidden constant [8 x i8] c"foo_weak"
0 ;; No instrumentation should be emitted if there are no counter increments.
11
22 ; RUN: opt < %s -instrprof -S | FileCheck %s
3 ; RUN: opt < %s -passes=instrprof -S | FileCheck %s
34 ; CHECK-NOT: @__profc
45 ; CHECK-NOT: @__profd
56 ; CHECK-NOT: @__llvm_profile_runtime
0 ;; Check that we don't emit the runtime hooks if the user provided them.
11
22 ; RUN: opt < %s -instrprof -S | FileCheck %s
3 ; RUN: opt < %s -passes=instrprof -S | FileCheck %s
34 ; CHECK-NOT: define {{.*}} @__llvm_profile_runtime_user()
45 ; CHECK-NOT: load i32, i32* @__llvm_profile_runtime
56
11
22 ; RUN: opt < %s -mtriple=x86_64-apple-macosx10.10.0 -instrprof -S | FileCheck %s -check-prefix=MACHO
33 ; RUN: opt < %s -mtriple=x86_64-unknown-linux -instrprof -S | FileCheck %s -check-prefix=LINUX
4 ; RUN: opt < %s -mtriple=x86_64-unknown-linux -passes=instrprof -S | FileCheck %s -check-prefix=LINUX
45 ; RUN: opt < %s -mtriple=x86_64-unknown-freebsd -instrprof -S | FileCheck %s -check-prefix=FREEBSD
56 ; RUN: opt < %s -mtriple=x86_64-scei-ps4 -instrprof -S | FileCheck %s -check-prefix=PS4
67 ; RUN: opt < %s -mtriple=x86_64-pc-solaris -instrprof -S | FileCheck %s -check-prefix=SOLARIS
0 ; RUN: opt < %s -instrprof -S | FileCheck %s
1 ; RUN: opt < %s -passes=instrprof -S | FileCheck %s
12
23 target triple = "x86_64-apple-macosx10.10.0"
34