Static Value-Flow Analysis
Loading...
Searching...
No Matches
BasicBlockG.h
Go to the documentation of this file.
1//===- BasicBlockG.h -- BasicBlock node------------------------------------------------//
2//
3// SVF: Static Value-Flow Analysis
4//
5// Copyright (C) <2013-2025> <Yulei Sui>
6//
7
8// This program is free software: you can redistribute it and/or modify
9// it under the terms of the GNU Affero General Public License as published by
10// the Free Software Foundation, either version 3 of the License, or
11// (at your option) any later version.
12
13// This program is distributed in the hope that it will be useful,
14// but WITHOUT ANY WARRANTY; without even the implied warranty of
15// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16// GNU Affero General Public License for more details.
17
18// You should have received a copy of the GNU Affero General Public License
19// along with this program. If not, see <http://www.gnu.org/licenses/>.
20//
21//===----------------------------------------------------------------------===//
22
23/*
24 * ICFGNode.h
25 *
26 * Created on: 23 Jan, 2025
27 * Author: Jiawei, Xiao
28 */
29
30#ifndef BASICBLOCKGRAPH_H_
31#define BASICBLOCKGRAPH_H_
32#include "GenericGraph.h"
33#include <sstream>
34#include <algorithm>
35
36
37namespace SVF
38{
39class SVFBasicBlock;
40class BasicBlockEdge;
41class ICFGNode;
42class FunObjVar;
43
46{
47
48public:
55
57
59 {
60 o << edge.toString();
61 return o;
62 }
64
65 virtual const std::string toString() const;
66};
67
68
71{
72 friend class LLVMModuleSet;
73 friend class SVFIRBuilder;
74 friend class FunObjVar;
75 friend class ICFGBuilder;
76 friend class ICFG;
77 friend class GraphDBClient;
78
79public:
80 typedef std::vector<const ICFGNode*>::const_iterator const_iterator;
81 std::vector<const SVFBasicBlock*> succBBs;
82 std::vector<const SVFBasicBlock*> predBBs;
83
84private:
85 std::vector<const ICFGNode*> allICFGNodes;
86 const FunObjVar* fun;
87
88
89
90protected:
92
93 inline void addICFGNode(const ICFGNode* icfgNode)
94 {
96 icfgNode) == getICFGNodeList().end() && "duplicated icfgnode");
97 allICFGNodes.push_back(icfgNode);
98 }
99
101
102 // Getters of predecessor and successor basic blocks, used for writing bb to DB
103 const std::vector<const SVFBasicBlock*> getSuccBBs() const
104 {
105 return succBBs;
106 }
107
108 const std::vector<const SVFBasicBlock*> getPredBBs() const
109 {
110 return predBBs;
111 }
112
113public:
119 SVFBasicBlock() = delete;
121 {
122
123 }
124
125 static inline bool classof(const SVFValue* node)
126 {
127 return node->getNodeKind() == SVFValue::BasicBlockKd;
128 }
129
130 static inline bool classof(const SVFBasicBlock* node)
131 {
132 return true;
133 }
134
137 {
138 o << node.toString();
139 return o;
140 }
142
143
144 inline const std::vector<const ICFGNode*>& getICFGNodeList() const
145 {
146 return allICFGNodes;
147 }
148
149 inline const_iterator begin() const
150 {
151 return allICFGNodes.begin();
152 }
153
154 inline const_iterator end() const
155 {
156 return allICFGNodes.end();
157 }
158
159
160 inline void setFun(const FunObjVar* f)
161 {
162 fun = f;
163 }
164
166 {
167 // check if the edge already exists
168 for (auto edge: this->getOutEdges())
169 {
170 if (edge->getDstNode() == succ2)
171 return;
172 }
173
174 SVFBasicBlock* succ = const_cast<SVFBasicBlock*>(succ2);
176 this->addOutgoingEdge(edge);
177 succ->addIncomingEdge(edge);
178 this->succBBs.push_back(succ);
179 succ->predBBs.push_back(this);
180 }
181
183 {
184 // check if the edge already exists
185 for (auto edge: this->getInEdges())
186 {
187 if (edge->getSrcNode() == pred2)
188 return;
189 }
190 SVFBasicBlock* pred = const_cast<SVFBasicBlock*>(pred2);
192 this->addIncomingEdge(edge);
193 pred->addOutgoingEdge(edge);
194 this->predBBs.push_back(pred);
195 pred->succBBs.push_back(this);
196 }
197
198 inline const FunObjVar* getParent() const
199 {
200 assert(fun && "Function is null?");
201 return fun;
202 }
203
204 inline const FunObjVar* getFunction() const
205 {
206 assert(fun && "Function is null?");
207 return fun;
208 }
209
210 inline const ICFGNode* front() const
211 {
212 assert(!allICFGNodes.empty() && "bb empty?");
213 return allICFGNodes.front();
214 }
215
216 inline const ICFGNode* back() const
217 {
218 assert(!allICFGNodes.empty() && "bb empty?");
219 return allICFGNodes.back();
220 }
221
222 inline std::vector<const SVFBasicBlock*> getSuccessors() const
223 {
224 std::vector<const SVFBasicBlock*> res;
225 for (auto edge : this->getOutEdges())
226 {
227 res.push_back(edge->getDstNode());
228 }
229 return res;
230 }
231
232 inline std::vector<const SVFBasicBlock*> getPredecessors() const
233 {
234 std::vector<const SVFBasicBlock*> res;
235 for (auto edge : this->getInEdges())
236 {
237 res.push_back(edge->getSrcNode());
238 }
239 return res;
240 }
242 {
243 return this->getOutEdges().size();
244 }
246 {
247 u32_t i = 0;
248 for (const SVFBasicBlock* SuccBB: succBBs)
249 {
250 if (SuccBB == Succ)
251 return i;
252 i++;
253 }
254 assert(false && "Didn't find successor edge?");
255 return 0;
256 }
258 {
259 u32_t i = 0;
260 for (const SVFBasicBlock* SuccBB: succBBs)
261 {
262 if (SuccBB == Succ)
263 return i;
264 i++;
265 }
266 assert(false && "Didn't find successor edge?");
267 return 0;
268
269 }
271 {
272 u32_t pos = 0;
273 for (const SVFBasicBlock* PredBB : succbb->getPredecessors())
274 {
275 if(PredBB == this)
276 return pos;
277 ++pos;
278 }
279 assert(false && "Didn't find predecessor edge?");
280 return pos;
281 }
283 {
284 u32_t pos = 0;
285 for (const SVFBasicBlock* PredBB : succbb->getPredecessors())
286 {
287 if(PredBB == this)
288 return pos;
289 ++pos;
290 }
291 assert(false && "Didn't find predecessor edge?");
292 return pos;
293 }
294
295 const std::string toString() const;
296
297};
298
299
300
303{
304public:
305 NodeID id{0};
311
312
313 SVFBasicBlock* addBasicBlock(const std::string& bbname)
314 {
315 id++;
316 SVFBasicBlock* bb = new SVFBasicBlock(id, nullptr);
317 bb->setName(bbname);
318 addBasicBlock(bb);
319 return bb;
320 }
321
323
324};
325}
326
327#endif
friend OutStream & operator<<(OutStream &o, const BasicBlockEdge &edge)
Overloading operator << for dumping ICFG node ID.
Definition BasicBlockG.h:58
BasicBlockEdge(SVFBasicBlock *s, SVFBasicBlock *d)
Constructor.
Definition BasicBlockG.h:50
virtual const std::string toString() const
~BasicBlockEdge()
Destructor.
Definition BasicBlockG.h:54
SVFBasicBlock * addBasicBlock(const std::string &bbname)
BasicBlockGraph()
Constructor.
const GEdgeSetTy & getOutEdges() const
const GEdgeSetTy & getInEdges() const
bool addIncomingEdge(EdgeType *inEdge)
Add incoming and outgoing edges.
bool addOutgoingEdge(EdgeType *outEdge)
static bool classof(const SVFValue *node)
const FunObjVar * fun
Definition BasicBlockG.h:86
u32_t getBBSuccessorPos(const SVFBasicBlock *Succ) const
const std::vector< const SVFBasicBlock * > getSuccBBs() const
const FunObjVar * getParent() const
SVFBasicBlock(NodeID id, const FunObjVar *f)
Constructor without name.
void setFun(const FunObjVar *f)
void addPredBasicBlock(const SVFBasicBlock *pred2)
std::vector< const SVFBasicBlock * > getPredecessors() const
const std::vector< const ICFGNode * > & getICFGNodeList() const
const FunObjVar * getFunction() const
const std::string toString() const
void addSuccBasicBlock(const SVFBasicBlock *succ2)
void addICFGNode(const ICFGNode *icfgNode)
Function where this BasicBlock is.
Definition BasicBlockG.h:93
friend OutStream & operator<<(OutStream &o, const SVFBasicBlock &node)
const ICFGNode * front() const
std::vector< const SVFBasicBlock * > succBBs
Definition BasicBlockG.h:81
u32_t getBBPredecessorPos(const SVFBasicBlock *succbb)
u32_t getBBPredecessorPos(const SVFBasicBlock *succbb) const
std::vector< constICFGNode * >::const_iterator const_iterator
Definition BasicBlockG.h:80
std::vector< const ICFGNode * > allICFGNodes
all ICFGNodes in this BasicBlock
Definition BasicBlockG.h:85
friend class GraphDBClient
Definition BasicBlockG.h:77
static bool classof(const SVFBasicBlock *node)
const_iterator end() const
const std::vector< const SVFBasicBlock * > getPredBBs() const
const_iterator begin() const
u32_t getBBSuccessorPos(const SVFBasicBlock *Succ)
SVFBasicBlock()=delete
u32_t getNumSuccessors() const
std::vector< const SVFBasicBlock * > predBBs
Definition BasicBlockG.h:82
const ICFGNode * back() const
std::vector< const SVFBasicBlock * > getSuccessors() const
GNodeK getNodeKind() const
Get node kind.
Definition SVFValue.h:166
NodeID id
Node ID.
Definition SVFValue.h:206
virtual void setName(const std::string &nameInfo)
Definition SVFValue.h:176
for isBitcode
Definition BasicTypes.h:68
u32_t NodeID
Definition GeneralType.h:56
std::ostream OutStream
Definition GeneralType.h:46
GenericNode< SVFBasicBlock, BasicBlockEdge > GenericBasicBlockNodeTy
Definition BasicBlockG.h:69
llvm::IRBuilder IRBuilder
Definition BasicTypes.h:74
GenericEdge< SVFBasicBlock > GenericBasicBlockEdgeTy
Definition BasicBlockG.h:44
unsigned u32_t
Definition GeneralType.h:47
GenericGraph< SVFBasicBlock, BasicBlockEdge > GenericBasicBlockGraphTy