-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCallFunctionGraph.cpp
90 lines (74 loc) · 2.63 KB
/
CallFunctionGraph.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
#include "CallFunctionGraph.h"
CallFunctionGraph::CallFunctionGraph(Function *EntryFunction)
: m_EntryFunction(EntryFunction) {}
void CallFunctionGraph::createCallGraph() {
m_CallGraph.clear();
m_UsedFunctions.clear();
createCallGraph(m_EntryFunction);
}
void CallFunctionGraph::createCallGraph(Function *F) {
m_UsedFunctions.insert(F);
for (BasicBlock &BB : *F) {
for (Instruction &I : BB) {
if (CallInst *CallInstr = dyn_cast<CallInst>(&I)) {
Function *Callee = CallInstr->getCalledFunction();
m_CallGraph[F].push_back(Callee);
if (m_UsedFunctions.find(Callee) == m_UsedFunctions.end()) {
createCallGraph(Callee);
}
}
}
}
}
void CallFunctionGraph::findRecursiveCalls(Function *F, std::unordered_set<Function *> &Visited,
std::vector<Function *> &Path,
std::unordered_set<Function *> &InPath) {
Visited.insert(F);
Path.push_back(F);
InPath.insert(F);
for (Function *Callee : m_CallGraph[F]) {
if (InPath.find(Callee) != InPath.end()) {
for (size_t i = Path.size() - 1; Path[i] != Callee; i--) {
m_RecursiveFunctions.insert(Path[i]);
}
m_RecursiveFunctions.insert(Callee);
}
else if (Visited.find(Callee) == Visited.end()) {
findRecursiveCalls(Callee, Visited, Path, InPath);
}
}
Path.pop_back();
InPath.erase(F);
}
void CallFunctionGraph::findRecursiveCalls() {
std::vector<Function *> Path;
std::unordered_set<Function *> Visited, InPath;
findRecursiveCalls(m_EntryFunction, Visited, Path, InPath);
}
bool CallFunctionGraph::isRecursive(Function *F) {
return m_RecursiveFunctions.find(F) != m_RecursiveFunctions.end();
}
bool CallFunctionGraph::removeUnusedFunctions() {
std::vector<Function *> UnusedFunctions;
for (Function &F : *m_EntryFunction->getParent()) {
if (m_UsedFunctions.find(&F) == m_UsedFunctions.end()) {
UnusedFunctions.push_back(&F);
}
}
for (Function *F : UnusedFunctions) {
F->eraseFromParent();
}
return !UnusedFunctions.empty();
}
void CallFunctionGraph::print() {
for (auto &MapPair : m_CallGraph) {
errs() << MapPair.first->getName() << ": \n";
for (Function *F : MapPair.second) {
errs() << "\t" << F->getName() << "\n";
}
}
errs() << "Recursive functions: \n";
for (Function *F : m_RecursiveFunctions) {
errs() << "\t" << F->getName() << "\n";
}
}