-
Notifications
You must be signed in to change notification settings - Fork 491
Expand file tree
/
Copy pathBasicBlockG.h
More file actions
328 lines (273 loc) · 7.99 KB
/
BasicBlockG.h
File metadata and controls
328 lines (273 loc) · 7.99 KB
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
//===- BasicBlockG.h -- BasicBlock node------------------------------------------------//
//
// SVF: Static Value-Flow Analysis
//
// Copyright (C) <2013-2025> <Yulei Sui>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
//===----------------------------------------------------------------------===//
/*
* ICFGNode.h
*
* Created on: 23 Jan, 2025
* Author: Jiawei, Xiao
*/
#ifndef BASICBLOCKGRAPH_H_
#define BASICBLOCKGRAPH_H_
#include "GenericGraph.h"
#include <sstream>
#include <algorithm>
namespace SVF
{
class SVFBasicBlock;
class BasicBlockEdge;
class ICFGNode;
class FunObjVar;
typedef GenericEdge<SVFBasicBlock> GenericBasicBlockEdgeTy;
class BasicBlockEdge: public GenericBasicBlockEdgeTy
{
public:
/// Constructor
BasicBlockEdge(SVFBasicBlock* s, SVFBasicBlock* d) : GenericBasicBlockEdgeTy(s, d, 0)
{
}
/// Destructor
~BasicBlockEdge() {}
/// Overloading operator << for dumping ICFG node ID
//@{
friend OutStream& operator<<(OutStream& o, const BasicBlockEdge& edge)
{
o << edge.toString();
return o;
}
//@}
virtual const std::string toString() const;
};
typedef GenericNode<SVFBasicBlock, BasicBlockEdge> GenericBasicBlockNodeTy;
class SVFBasicBlock : public GenericBasicBlockNodeTy
{
friend class LLVMModuleSet;
friend class SVFIRBuilder;
friend class FunObjVar;
friend class ICFGBuilder;
friend class ICFG;
friend class GraphDBClient;
public:
typedef std::vector<const ICFGNode*>::const_iterator const_iterator;
std::vector<const SVFBasicBlock*> succBBs;
std::vector<const SVFBasicBlock*> predBBs;
private:
std::vector<const ICFGNode*> allICFGNodes; ///< all ICFGNodes in this BasicBlock
const FunObjVar* fun; /// Function where this BasicBlock is
protected:
///@{ attributes to be set only through Module builders e.g., LLVMModule
inline void addICFGNode(const ICFGNode* icfgNode)
{
assert(std::find(getICFGNodeList().begin(), getICFGNodeList().end(),
icfgNode) == getICFGNodeList().end() && "duplicated icfgnode");
allICFGNodes.push_back(icfgNode);
}
/// @}
// Getters of predecessor and successor basic blocks, used for writing bb to DB
const std::vector<const SVFBasicBlock*> getSuccBBs() const
{
return succBBs;
}
const std::vector<const SVFBasicBlock*> getPredBBs() const
{
return predBBs;
}
public:
/// Constructor without name
SVFBasicBlock(NodeID id, const FunObjVar* f): GenericBasicBlockNodeTy(id, BasicBlockKd), fun(f)
{
}
SVFBasicBlock() = delete;
~SVFBasicBlock()
{
}
static inline bool classof(const SVFValue* node)
{
return node->getNodeKind() == SVFValue::BasicBlockKd;
}
static inline bool classof(const SVFBasicBlock* node)
{
return true;
}
//@{
friend OutStream &operator<<(OutStream &o, const SVFBasicBlock&node)
{
o << node.toString();
return o;
}
//@}
inline const std::vector<const ICFGNode*>& getICFGNodeList() const
{
return allICFGNodes;
}
inline const_iterator begin() const
{
return allICFGNodes.begin();
}
inline const_iterator end() const
{
return allICFGNodes.end();
}
inline void setFun(const FunObjVar* f)
{
fun = f;
}
inline void addSuccBasicBlock(const SVFBasicBlock* succ2)
{
// check if the edge already exists
for (auto edge: this->getOutEdges())
{
if (edge->getDstNode() == succ2)
return;
}
SVFBasicBlock* succ = const_cast<SVFBasicBlock*>(succ2);
BasicBlockEdge* edge = new BasicBlockEdge(this, succ);
this->addOutgoingEdge(edge);
succ->addIncomingEdge(edge);
this->succBBs.push_back(succ);
succ->predBBs.push_back(this);
}
inline void addPredBasicBlock(const SVFBasicBlock* pred2)
{
// check if the edge already exists
for (auto edge: this->getInEdges())
{
if (edge->getSrcNode() == pred2)
return;
}
SVFBasicBlock* pred = const_cast<SVFBasicBlock*>(pred2);
BasicBlockEdge* edge = new BasicBlockEdge(pred, this);
this->addIncomingEdge(edge);
pred->addOutgoingEdge(edge);
this->predBBs.push_back(pred);
pred->succBBs.push_back(this);
}
inline const FunObjVar* getParent() const
{
assert(fun && "Function is null?");
return fun;
}
inline const FunObjVar* getFunction() const
{
assert(fun && "Function is null?");
return fun;
}
inline const ICFGNode* front() const
{
assert(!allICFGNodes.empty() && "bb empty?");
return allICFGNodes.front();
}
inline const ICFGNode* back() const
{
assert(!allICFGNodes.empty() && "bb empty?");
return allICFGNodes.back();
}
inline std::vector<const SVFBasicBlock*> getSuccessors() const
{
std::vector<const SVFBasicBlock*> res;
for (auto edge : this->getOutEdges())
{
res.push_back(edge->getDstNode());
}
return res;
}
inline std::vector<const SVFBasicBlock*> getPredecessors() const
{
std::vector<const SVFBasicBlock*> res;
for (auto edge : this->getInEdges())
{
res.push_back(edge->getSrcNode());
}
return res;
}
u32_t getNumSuccessors() const
{
return this->getOutEdges().size();
}
u32_t getBBSuccessorPos(const SVFBasicBlock* Succ)
{
u32_t i = 0;
for (const SVFBasicBlock* SuccBB: succBBs)
{
if (SuccBB == Succ)
return i;
i++;
}
assert(false && "Didn't find successor edge?");
return 0;
}
u32_t getBBSuccessorPos(const SVFBasicBlock* Succ) const
{
u32_t i = 0;
for (const SVFBasicBlock* SuccBB: succBBs)
{
if (SuccBB == Succ)
return i;
i++;
}
assert(false && "Didn't find successor edge?");
return 0;
}
u32_t getBBPredecessorPos(const SVFBasicBlock* succbb)
{
u32_t pos = 0;
for (const SVFBasicBlock* PredBB : succbb->getPredecessors())
{
if(PredBB == this)
return pos;
++pos;
}
assert(false && "Didn't find predecessor edge?");
return pos;
}
u32_t getBBPredecessorPos(const SVFBasicBlock* succbb) const
{
u32_t pos = 0;
for (const SVFBasicBlock* PredBB : succbb->getPredecessors())
{
if(PredBB == this)
return pos;
++pos;
}
assert(false && "Didn't find predecessor edge?");
return pos;
}
const std::string toString() const;
};
typedef GenericGraph<SVFBasicBlock, BasicBlockEdge> GenericBasicBlockGraphTy;
class BasicBlockGraph: public GenericBasicBlockGraphTy
{
private:
NodeID id{0};
public:
/// Constructor
BasicBlockGraph(): GenericBasicBlockGraphTy()
{
}
SVFBasicBlock* addBasicBlock(const std::string& bbname)
{
id++;
SVFBasicBlock* bb = new SVFBasicBlock(id, nullptr);
bb->setName(bbname);
addBasicBlock(bb);
return bb;
}
void addBasicBlock(SVFBasicBlock* bb);
};
}
#endif