10
10
11
11
#include " llvm/ADT/MapVector.h"
12
12
#include " llvm/Analysis/BlockFrequencyInfo.h"
13
- #include " llvm/Analysis/LazyBlockFrequencyInfo.h"
14
13
#include " llvm/Analysis/TargetTransformInfo.h"
15
14
#include " llvm/IR/Constants.h"
16
15
#include " llvm/IR/Instructions.h"
17
16
#include " llvm/IR/MDBuilder.h"
18
17
#include " llvm/IR/PassManager.h"
19
- #include " llvm/InitializePasses.h"
20
18
#include " llvm/ProfileData/InstrProf.h"
21
- #include " llvm/Transforms/IPO.h"
22
19
#include " llvm/Transforms/Instrumentation.h"
23
20
24
21
#include < array>
25
22
26
23
using namespace llvm ;
27
24
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) {
25
+ PreservedAnalyses CGProfilePass::run (Module &M, ModuleAnalysisManager &MAM) {
54
26
MapVector<std::pair<Function *, Function *>, uint64_t > Counts;
27
+ FunctionAnalysisManager &FAM =
28
+ MAM.getResult <FunctionAnalysisManagerModuleProxy>(M).getManager ();
55
29
InstrProfSymtab Symtab;
56
30
auto UpdateCounts = [&](TargetTransformInfo &TTI, Function *F,
57
31
Function *CalledF, uint64_t NewCount) {
@@ -61,14 +35,14 @@ runCGProfilePass(Module &M,
61
35
Count = SaturatingAdd (Count, NewCount);
62
36
};
63
37
// Ignore error here. Indirect calls are ignored if this fails.
64
- (void )(bool ) Symtab.create (M);
38
+ (void )(bool )Symtab.create (M);
65
39
for (auto &F : M) {
66
- if (F.isDeclaration () || !F. getEntryCount () )
40
+ if (F.isDeclaration ())
67
41
continue ;
68
- auto &BFI = GetBFI (F);
42
+ auto &BFI = FAM. getResult <BlockFrequencyAnalysis> (F);
69
43
if (BFI.getEntryFreq () == 0 )
70
44
continue ;
71
- TargetTransformInfo &TTI = GetTTI (F);
45
+ TargetTransformInfo &TTI = FAM. getResult <TargetIRAnalysis> (F);
72
46
for (auto &BB : F) {
73
47
Optional<uint64_t > BBCount = BFI.getBlockProfileCount (&BB);
74
48
if (!BBCount)
@@ -95,56 +69,28 @@ runCGProfilePass(Module &M,
95
69
}
96
70
}
97
71
98
- return addModuleFlags (M, Counts);
99
- }
72
+ addModuleFlags (M, Counts);
100
73
101
- namespace {
102
- struct CGProfileLegacyPass final : public ModulePass {
103
- static char ID;
104
- CGProfileLegacyPass () : ModulePass(ID) {
105
- initializeCGProfileLegacyPassPass (*PassRegistry::getPassRegistry ());
106
- }
74
+ return PreservedAnalyses::all ();
75
+ }
107
76
108
- void getAnalysisUsage (AnalysisUsage &AU) const override {
109
- AU. setPreservesCFG ();
110
- AU. addRequired <LazyBlockFrequencyInfoPass>();
111
- AU. addRequired <TargetTransformInfoWrapperPass>();
112
- }
77
+ void CGProfilePass::addModuleFlags (
78
+ Module &M,
79
+ MapVector<std::pair<Function *, Function *>, uint64_t > &Counts) const {
80
+ if (Counts. empty ())
81
+ return ;
113
82
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
- };
83
+ LLVMContext &Context = M.getContext ();
84
+ MDBuilder MDB (Context);
85
+ std::vector<Metadata *> Nodes;
121
86
122
- return runCGProfilePass (M, GetBFI, GetTTI);
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));
123
93
}
124
- };
125
-
126
- } // namespace
127
-
128
- char CGProfileLegacyPass::ID = 0 ;
129
94
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 ();
95
+ M.addModuleFlag (Module::Append, " CG Profile" , MDNode::get (Context, Nodes));
150
96
}
0 commit comments