/
CGProfile.cpp
110 lines (91 loc) · 3.33 KB
/
CGProfile.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
//===-- CGProfile.cpp -----------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "llvm/ADT/MapVector.h"
#include "llvm/Analysis/BlockFrequencyInfo.h"
#include "llvm/Analysis/BranchProbabilityInfo.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/MDBuilder.h"
#include "llvm/IR/PassManager.h"
#include "llvm/Transforms/Instrumentation.h"
#include <array>
using namespace llvm;
class CGProfilePass : public ModulePass {
public:
static char ID;
CGProfilePass() : ModulePass(ID) {
initializeCGProfilePassPass(*PassRegistry::getPassRegistry());
}
StringRef getPassName() const override { return "CGProfilePass"; }
private:
bool runOnModule(Module &M) override;
bool addModuleFlags(
Module &M,
MapVector<std::pair<Function *, Function *>, uint64_t> &Counts) const;
void getAnalysisUsage(AnalysisUsage &AU) const override {
AU.addRequired<BlockFrequencyInfoWrapperPass>();
AU.addRequired<BranchProbabilityInfoWrapperPass>();
}
};
bool CGProfilePass::runOnModule(Module &M) {
if (skipModule(M))
return false;
MapVector<std::pair<Function *, Function *>, uint64_t> Counts;
for (auto &F : M) {
if (F.isDeclaration())
continue;
getAnalysis<BranchProbabilityInfoWrapperPass>(F).getBPI();
auto &BFI = getAnalysis<BlockFrequencyInfoWrapperPass>(F).getBFI();
for (const auto &BB : F) {
Optional<uint64_t> BBCount = BFI.getBlockProfileCount(&BB);
if (!BBCount)
continue;
for (const auto &I : BB) {
auto *CI = dyn_cast<CallInst>(&I);
if (!CI)
continue;
Function *CalledF = CI->getCalledFunction();
if (!CalledF || CalledF->isIntrinsic())
continue;
uint64_t &Count = Counts[std::make_pair(&F, CalledF)];
Count = SaturatingAdd(Count, *BBCount);
}
}
}
return addModuleFlags(M, Counts);
}
bool CGProfilePass::addModuleFlags(
Module &M,
MapVector<std::pair<Function *, Function *>, uint64_t> &Counts) const {
if (Counts.empty())
return false;
LLVMContext &Context = M.getContext();
MDBuilder MDB(Context);
std::vector<Metadata *> Nodes;
for (auto E : Counts) {
SmallVector<Metadata *, 3> Vals;
Vals.push_back(ValueAsMetadata::get(E.first.first));
Vals.push_back(ValueAsMetadata::get(E.first.second));
Vals.push_back(MDB.createConstant(
ConstantInt::get(Type::getInt64Ty(Context), E.second)));
Nodes.push_back(MDNode::get(Context, Vals));
}
M.addModuleFlag(Module::Append, "CG Profile", MDNode::get(Context, Nodes));
return true;
}
char CGProfilePass::ID = 0;
INITIALIZE_PASS_BEGIN(CGProfilePass, "cg-profile",
"Generate profile information from the call graph.",
false, false)
INITIALIZE_PASS_DEPENDENCY(BlockFrequencyInfoWrapperPass)
INITIALIZE_PASS_DEPENDENCY(BranchProbabilityInfoWrapperPass)
INITIALIZE_PASS_END(CGProfilePass, "cg-profile",
"Generate profile information from the call graph.", false,
false)
ModulePass *llvm::createCGProfilePass() { return new CGProfilePass(); }