-
Notifications
You must be signed in to change notification settings - Fork 0
/
rcg.cpp
48 lines (42 loc) · 1.39 KB
/
rcg.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
#include "rcg.hpp"
#include "cg.hpp"
ReverseCallGraph::~ReverseCallGraph() {
for (auto &El : FuncPcToNode) {
if (El.second->NumCallers)
delete[] El.second->Callers;
El.second->Callers = nullptr;
delete El.second;
}
FuncPcToNode.clear();
CallSitePcToNode.clear();
}
ReverseCallGraph::ReverseCallGraph(const CallGraph& RawCG) {
// Get the filtered target to callers mapping.
auto &TargetToCallers = RawCG.TargetsToCallers;
// Create function nodes.
for (const auto &El : TargetToCallers) {
uint64_t FuncPc = El.first;
FuncPcToNode[FuncPc] = new FunctionNode(FuncPc);
}
// Set callers.
for (const auto &El : TargetToCallers) {
uint64_t FuncPc = El.first;
FunctionNode *FuncNode = FuncPcToNode[FuncPc];
const std::vector<CallSite> &Callers = El.second;
uint64_t NumCallers = Callers.size();
// Allocate for callers.
FuncNode->NumCallers = NumCallers;
if (NumCallers)
FuncNode->Callers = new CallSiteNode[NumCallers];
// Set callers.
for (int I = 0; I < NumCallers; I++) {
const CallSite CS = Callers[I]; //< Get info from.
CallSiteNode &CSN = FuncNode->Callers[I]; //< Fill info to.
// Set caller.
CSN.CallSitePc = CS.CallSitePc;
CSN.Caller = FuncPcToNode[CS.CallerPc];
// Set CallSiteToPcNode mapping for reverse call graph.
CallSitePcToNode[CS.CallSitePc] = &CSN;
}
}
}