10
10
11
11
#include " llvm/ADT/MapVector.h"
12
12
#include " llvm/Analysis/BlockFrequencyInfo.h"
13
+ #include " llvm/Analysis/LazyBlockFrequencyInfo.h"
13
14
#include " llvm/Analysis/TargetTransformInfo.h"
14
15
#include " llvm/IR/Constants.h"
15
16
#include " llvm/IR/Instructions.h"
16
17
#include " llvm/IR/MDBuilder.h"
17
18
#include " llvm/IR/PassManager.h"
19
+ #include " llvm/InitializePasses.h"
18
20
#include " llvm/ProfileData/InstrProf.h"
21
+ #include " llvm/Transforms/IPO.h"
19
22
#include " llvm/Transforms/Instrumentation.h"
20
23
21
24
#include < array>
22
25
23
26
using namespace llvm ;
24
27
25
- PreservedAnalyses CGProfilePass::run (Module &M, ModuleAnalysisManager &MAM) {
28
+ static bool
29
+ addModuleFlags (Module &M,
30
+ MapVector<std::pair<Function *, Function *>, uint64_t > &Counts) {
31
+ if (Counts.empty ())
32
+ return false ;
33
+
34
+ LLVMContext &Context = M.getContext ();
35
+ MDBuilder MDB (Context);
36
+ std::vector<Metadata *> Nodes;
37
+
38
+ for (auto E : Counts) {
39
+ Metadata *Vals[] = {ValueAsMetadata::get (E.first .first ),
40
+ ValueAsMetadata::get (E.first .second ),
41
+ MDB.createConstant (ConstantInt::get (
42
+ Type::getInt64Ty (Context), E.second ))};
43
+ Nodes.push_back (MDNode::get (Context, Vals));
44
+ }
45
+
46
+ M.addModuleFlag (Module::Append, " CG Profile" , MDNode::get (Context, Nodes));
47
+ return true ;
48
+ }
49
+
50
+ static bool
51
+ runCGProfilePass (Module &M,
52
+ function_ref<BlockFrequencyInfo &(Function &)> GetBFI,
53
+ function_ref<TargetTransformInfo &(Function &)> GetTTI) {
26
54
MapVector<std::pair<Function *, Function *>, uint64_t > Counts;
27
- FunctionAnalysisManager &FAM =
28
- MAM.getResult <FunctionAnalysisManagerModuleProxy>(M).getManager ();
29
55
InstrProfSymtab Symtab;
30
56
auto UpdateCounts = [&](TargetTransformInfo &TTI, Function *F,
31
57
Function *CalledF, uint64_t NewCount) {
@@ -35,14 +61,14 @@ PreservedAnalyses CGProfilePass::run(Module &M, ModuleAnalysisManager &MAM) {
35
61
Count = SaturatingAdd (Count, NewCount);
36
62
};
37
63
// Ignore error here. Indirect calls are ignored if this fails.
38
- (void )(bool )Symtab.create (M);
64
+ (void )(bool ) Symtab.create (M);
39
65
for (auto &F : M) {
40
- if (F.isDeclaration ())
66
+ if (F.isDeclaration () || !F. getEntryCount () )
41
67
continue ;
42
- auto &BFI = FAM. getResult <BlockFrequencyAnalysis> (F);
68
+ auto &BFI = GetBFI (F);
43
69
if (BFI.getEntryFreq () == 0 )
44
70
continue ;
45
- TargetTransformInfo &TTI = FAM. getResult <TargetIRAnalysis> (F);
71
+ TargetTransformInfo &TTI = GetTTI (F);
46
72
for (auto &BB : F) {
47
73
Optional<uint64_t > BBCount = BFI.getBlockProfileCount (&BB);
48
74
if (!BBCount)
@@ -69,28 +95,56 @@ PreservedAnalyses CGProfilePass::run(Module &M, ModuleAnalysisManager &MAM) {
69
95
}
70
96
}
71
97
72
- addModuleFlags (M, Counts);
73
-
74
- return PreservedAnalyses::all ();
98
+ return addModuleFlags (M, Counts);
75
99
}
76
100
77
- void CGProfilePass::addModuleFlags (
78
- Module &M,
79
- MapVector<std::pair<Function *, Function *>, uint64_t > &Counts) const {
80
- if (Counts.empty ())
81
- return ;
101
+ namespace {
102
+ struct CGProfileLegacyPass final : public ModulePass {
103
+ static char ID;
104
+ CGProfileLegacyPass () : ModulePass(ID) {
105
+ initializeCGProfileLegacyPassPass (*PassRegistry::getPassRegistry ());
106
+ }
82
107
83
- LLVMContext &Context = M.getContext ();
84
- MDBuilder MDB (Context);
85
- std::vector<Metadata *> Nodes;
108
+ void getAnalysisUsage (AnalysisUsage &AU) const override {
109
+ AU.setPreservesCFG ();
110
+ AU.addRequired <LazyBlockFrequencyInfoPass>();
111
+ AU.addRequired <TargetTransformInfoWrapperPass>();
112
+ }
86
113
87
- for (auto E : Counts) {
88
- Metadata *Vals[] = {ValueAsMetadata::get (E.first .first ),
89
- ValueAsMetadata::get (E.first .second ),
90
- MDB.createConstant (ConstantInt::get (
91
- Type::getInt64Ty (Context), E.second ))};
92
- Nodes.push_back (MDNode::get (Context, Vals));
114
+ bool runOnModule (Module &M) override {
115
+ auto GetBFI = [this ](Function &F) -> BlockFrequencyInfo & {
116
+ return this ->getAnalysis <LazyBlockFrequencyInfoPass>(F).getBFI ();
117
+ };
118
+ auto GetTTI = [this ](Function &F) -> TargetTransformInfo & {
119
+ return this ->getAnalysis <TargetTransformInfoWrapperPass>().getTTI (F);
120
+ };
121
+
122
+ return runCGProfilePass (M, GetBFI, GetTTI);
93
123
}
124
+ };
94
125
95
- M.addModuleFlag (Module::Append, " CG Profile" , MDNode::get (Context, Nodes));
126
+ } // namespace
127
+
128
+ char CGProfileLegacyPass::ID = 0 ;
129
+
130
+ INITIALIZE_PASS (CGProfileLegacyPass, " cg-profile" , " Call Graph Profile" , false ,
131
+ false )
132
+
133
+ ModulePass *llvm::createCGProfileLegacyPass() {
134
+ return new CGProfileLegacyPass ();
135
+ }
136
+
137
+ PreservedAnalyses CGProfilePass::run (Module &M, ModuleAnalysisManager &MAM) {
138
+ FunctionAnalysisManager &FAM =
139
+ MAM.getResult <FunctionAnalysisManagerModuleProxy>(M).getManager ();
140
+ auto GetBFI = [&FAM](Function &F) -> BlockFrequencyInfo & {
141
+ return FAM.getResult <BlockFrequencyAnalysis>(F);
142
+ };
143
+ auto GetTTI = [&FAM](Function &F) -> TargetTransformInfo & {
144
+ return FAM.getResult <TargetIRAnalysis>(F);
145
+ };
146
+
147
+ runCGProfilePass (M, GetBFI, GetTTI);
148
+
149
+ return PreservedAnalyses::all ();
96
150
}
0 commit comments