-
Notifications
You must be signed in to change notification settings - Fork 3
/
TGCallGraph.cpp
182 lines (165 loc) · 5.22 KB
/
TGCallGraph.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include "llvm/Typegraph/TGCallGraph.h"
#include "scc_algorithms.h"
#include <sstream>
#ifndef WITHOUT_LLVM
#include <llvm/Support/raw_ostream.h>
#endif
namespace typegraph {
TGCallGraph::TGCallGraph() : TGCallGraphBase(std::make_shared<StringContainer>()) {}
TGCallGraph::TGCallGraph(std::shared_ptr<StringContainer> SymbolContainer) : TGCallGraphBase(SymbolContainer) {}
void TGCallGraph::joinSCCs(CGSCC S1, CGSCC S2) {
auto &N1 = (*this)[S1];
for (const std::string *C: (*this)[S2].Contexts) {
N1.Contexts.insert(C);
ContextToSCC[C] = S1;
}
(*this)[S2].Contexts.clear();
replace_vertex_uses(S2, S1);
remove_vertex(S2);
// remove self edges S1->S1
remove_edge({S1, S1, {}});
}
std::shared_ptr<TGCallGraph> TGCallGraph::buildStronglyConnectedComponents() {
basegraph::ComponentMap Map(*this);
long NumComponents = basegraph::strong_components(*this, Map);
auto NewGraph = std::make_shared<TGCallGraph>(SymbolContainer);
for (int I = 0; I < NumComponents; I++) {
NewGraph->add_vertex({});
}
for (auto V: vertex_set()) {
auto V2 = Map.get(V);
assert(V2 >= 0 && V2 < NumComponents);
(*NewGraph)[V2].Contexts.insert((*this)[V].Contexts.begin(), (*this)[V].Contexts.end());
for (const auto *C: (*this)[V].Contexts) {
NewGraph->ContextToSCC[C] = V2;
}
for (auto E: out_edges(V)) {
auto V3 = Map.get(E.target);
if (V2 != V3) NewGraph->add_edge(V2, V3, E.property);
}
}
return NewGraph;
}
void TGCallGraph::parserAddSCC(int V, const std::string &Repr) {
CGSCC NewV = add_vertex({});
std::stringstream Ss(Repr);
std::string S;
while (getline(Ss, S, '\t')) {
const auto *C = SymbolContainer->get(S);
auto V2 = getVertexOpt(C);
if (V2 == NO_VERTEX) {
(*this)[NewV].Contexts.insert(C);
ContextToSCC[C] = NewV;
} else if (V2 != NewV) {
joinSCCs(V2, NewV);
NewV = V2;
}
}
ParserMap[V] = NewV;
}
void TGCallGraph::parserAddEdge(int S, int T) {
add_edge(ParserMap[S], ParserMap[T], {});
}
void TGCallGraph::parserReset() {
ParserMap.clear();
}
void TGLayeredCallGraph::joinSCCs(CGSCC S1, CGSCC S2) {
auto &N1 = (*this)[S1];
for (const auto &C: (*this)[S2].Contexts) {
N1.Contexts.insert(C);
ContextToSCC[C] = S1;
}
(*this)[S2].Contexts.clear();
remove_vertex(S2);
// remove self edges S1->S1
remove_edge({S1, S1, {}});
}
std::shared_ptr<TGLayeredCallGraph> TGLayeredCallGraph::buildStronglyConnectedComponents() {
basegraph::ComponentMap Map(*this);
long NumComponents = basegraph::strong_components(*this, Map);
auto NewGraph = std::make_shared<TGLayeredCallGraph>(SymbolContainer);
for (int I = 0; I < NumComponents; I++) {
NewGraph->add_vertex({});
}
for (auto V: vertex_set()) {
auto V2 = Map.get(V);
assert(V2 >= 0 && V2 < NumComponents);
(*NewGraph)[V2].Contexts.insert((*this)[V].Contexts.begin(), (*this)[V].Contexts.end());
for (const auto &C: (*this)[V].Contexts) {
NewGraph->ContextToSCC[C] = V2;
}
for (auto E: out_edges(V)) {
auto V3 = Map.get(E.target);
if (V2 != V3) NewGraph->add_edge(V2, V3, E.property);
}
}
return NewGraph;
}
void TGLayeredCallGraph::importUnlayeredGraph(TGCallGraph &Graph) {
basegraph::AbstractMap<CGSCC> OldToNew(Graph);
for (auto V: Graph.vertex_set()) {
auto V2 = OldToNew[V] = add_vertex({});
for (const auto *C: Graph[V].Contexts) {
(*this)[V2].Contexts.insert({C, 0});
ContextToSCC[{C, 0}] = V2;
}
}
for (auto V: Graph.vertex_set()) {
for (auto E: Graph.out_edges(V)) {
add_edge(OldToNew[E.source], OldToNew[E.target], E.property);
}
}
}
std::vector<CGSCC> TGLayeredCallGraph::getOrderCalleeBeforeCaller() {
std::vector<CGSCC> SCCs;
// Store SCCs (callee before caller)
bottom_up_traversal(*this, [&SCCs](CGSCC V, TGLayeredCallGraph &Graph) {
SCCs.push_back(V);
});
return SCCs;
}
std::vector<CGSCC> TGLayeredCallGraph::getOrderCallerBeforeCallee() {
std::vector<CGSCC> SCCs;
// Store SCCs (caller before callee)
top_down_traversal(*this, [&SCCs](CGSCC V, TGLayeredCallGraph &Graph) {
SCCs.push_back(V);
});
return SCCs;
}
bool TGLayeredCallGraph::hasEdge(CGSCC Source, CGSCC Target) {
if (Source == NO_VERTEX || Target == NO_VERTEX) return false;
return out_edges(Source).contains({Source, Target, {}});
}
CGSCC TGLayeredCallGraph::getLayeredVertex(CGSCC V, int Layer) {
const std::string *FirstContext = (*this)[V].Contexts.begin()->first;
auto V2 = getVertexOpt({FirstContext, Layer});
if (V2 != NO_VERTEX)
return V2;
V2 = add_vertex({});
for (auto &C: (*this)[V].Contexts) {
(*this)[V2].Contexts.insert({C.first, Layer});
ContextToSCC[{C.first, Layer}] = V2;
}
return V2;
}
void TGLayeredCallGraph::optimizeGraph() {
// Detect 1:1 relations (and merge)
for (auto V: vertex_set()) {
if (out_edges(V).size() == 1) {
auto E = *out_edges(V).begin();
if (in_edges(E.target).size() == 1) {
joinSCCs(V, E.target);
}
}
}
}
void TGLayeredCallGraph::computeDepth() {
bottom_up_traversal(*this, [](CGSCC V, TGLayeredCallGraph &Graph) {
int Depth = 0;
for (auto &E: Graph.out_edges(V)) {
Depth = std::max(Depth, Graph[E.target].Depth + 1);
}
Graph[V].Depth = Depth;
});
}
} // namespace typegraph