Static Value-Flow Analysis
Loading...
Searching...
No Matches
WorkList.h
Go to the documentation of this file.
1//===- WorkList.h -- Internal worklist used in SVF---------------------------//
2//
3// SVF: Static Value-Flow Analysis
4//
5// Copyright (C) <2013-2017> <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 * @file: WorkList.h
25 * @author: yesen
26 * @date: 06/12/2013
27 * @version: 1.0
28 *
29 * @section LICENSE
30 *
31 * @section DESCRIPTION
32 *
33 */
34
35
36#ifndef WORKLIST_H_
37#define WORKLIST_H_
38
39#include "SVFIR/SVFValue.h"
40
41#include <assert.h>
42#include <cstdlib>
43#include <vector>
44#include <deque>
45#include <list>
46#include <set>
47
48namespace SVF
49{
50
56template<class Data>
57class List
58{
60 {
61 public:
62 ListNode(const Data &d)
63 {
64 data = d;
65 next = nullptr;
66 }
67
69
72 };
73
75 typedef ListNode Node;
76
77public:
79 {
80 head = nullptr;
81 tail = nullptr;
82 }
83
84 ~List() {}
85
86 inline bool empty() const
87 {
88 return (head == nullptr);
89 }
90
91 inline bool find(const Data &data) const
92 {
93 return nodeSet.find(data) != nodeSet.end();
94 }
95
96 void push(const Data &data)
97 {
98 if (nodeSet.find(data) == nodeSet.end())
99 {
100 Node *new_node = new Node(data);
101 if (head == nullptr)
102 head = new_node;// the list is empty
103 else
104 tail->next = new_node;
105 tail = new_node;
106 }
107 }
108
110 {
111 assert(head != nullptr && "list is empty");
114
116 head = head->next;
117 if (head == nullptr)
118 tail = nullptr;
119
120 Data data = head_node->data;
121 nodeSet.erase(data);
122 delete head_node;
123 return data;
124 }
125
126private:
130};
131
137template<class Data>
139{
141 typedef std::deque<Data> DataDeque;
142public:
144
146 explicit FIFOWorkList(const std::vector<Data>& vec)
147 {
148 for (const Data& d : vec)
149 push(d);
150 }
151
153 explicit FIFOWorkList(const std::list<Data>& lst)
154 {
155 for (const Data& d : lst)
156 push(d);
157 }
158
160
161 inline bool empty() const
162 {
163 return data_list.empty();
164 }
165
166 inline u32_t size() const
167 {
168 assert(data_list.size() == data_set.size() && "list and set must be the same size!");
169 return data_list.size();
170 }
171
172 inline bool find(const Data &data) const
173 {
174 return data_set.find(data) != data_set.end();
175 }
176
180 inline bool push(const Data &data)
181 {
182 if (!find(data))
183 {
184 data_list.push_back(data);
185 data_set.insert(data);
186 return true;
187 }
188 else
189 return false;
190 }
191
195 inline void removeFront()
196 {
197 assert(!empty() && "work list is empty");
198 data_set.erase(front());
199 data_list.pop_front();
200 }
201
205 inline Data &front()
206 {
207 assert(!empty() && "work list is empty");
208 Data &data = data_list.front();
209 return data;
210 }
211
215 inline Data pop()
216 {
217 assert(!empty() && "work list is empty");
218 Data data = data_list.front();
219 data_list.pop_front();
220 data_set.erase(data);
221 return data;
222 }
223
227 inline void clear()
228 {
229 data_list.clear();
230 data_set.clear();
231 }
232
233private:
236};
237
243template<class Data>
245{
247 typedef std::vector<Data> DataVector;
248public:
250
252 explicit FILOWorkList(const std::vector<Data>& vec)
253 {
254 for (const Data& d : vec)
255 push(d);
256 }
257
259 explicit FILOWorkList(const std::list<Data>& lst)
260 {
261 for (const Data& d : lst)
262 push(d);
263 }
264
266
267 inline bool empty() const
268 {
269 return data_list.empty();
270 }
271
272 inline u32_t size() const
273 {
274 assert(data_list.size() == data_set.size() && "list and set must be the same size!");
275 return data_list.size();
276 }
277
278 inline bool find(const Data &data) const
279 {
280 return data_set.find(data) != data_set.end();;
281 }
282
286 inline bool push(const Data &data)
287 {
288 if (!find(data))
289 {
290 data_list.push_back(data);
291 data_set.insert(data);
292 return true;
293 }
294 else
295 return false;
296 }
297
301 inline Data pop()
302 {
303 assert(!empty() && "work list is empty");
304 Data data = data_list.back();
305 data_list.pop_back();
306 data_set.erase(data);
307 return data;
308 }
309
313 inline void removeBack()
314 {
315 assert(!empty() && "work list is empty");
316 data_set.erase(back());
317 data_list.pop_back();
318 }
319
323 inline Data &back()
324 {
325 assert(!empty() && "work list is empty");
326 Data &data = data_list.back();
327 return data;
328 }
329
333 inline void clear()
334 {
335 data_list.clear();
336 data_set.clear();
337 }
338
339private:
342};
343
344} // End namespace SVF
345
346#endif /* WORKLIST_H_ */
bool push(const Data &data)
Definition WorkList.h:180
FIFOWorkList(const std::vector< Data > &vec)
Construct from a vector, pushing all elements in order.
Definition WorkList.h:146
DataDeque data_list
work list using std::vector.
Definition WorkList.h:235
std::deque< Data > DataDeque
Definition WorkList.h:141
Set< Data > DataSet
Definition WorkList.h:140
DataSet data_set
store all data in the work list.
Definition WorkList.h:234
bool empty() const
Definition WorkList.h:161
FIFOWorkList(const std::list< Data > &lst)
Construct from a list, pushing all elements in order.
Definition WorkList.h:153
bool find(const Data &data) const
Definition WorkList.h:172
u32_t size() const
Definition WorkList.h:166
bool empty() const
Definition WorkList.h:267
std::vector< Data > DataVector
Definition WorkList.h:247
DataSet data_set
store all data in the work list.
Definition WorkList.h:340
FILOWorkList(const std::list< Data > &lst)
Construct from a list, pushing all elements in order.
Definition WorkList.h:259
Set< Data > DataSet
Definition WorkList.h:246
bool find(const Data &data) const
Definition WorkList.h:278
DataVector data_list
work list using std::vector.
Definition WorkList.h:341
bool push(const Data &data)
Definition WorkList.h:286
FILOWorkList(const std::vector< Data > &vec)
Construct from a vector, pushing all elements in order.
Definition WorkList.h:252
u32_t size() const
Definition WorkList.h:272
ListNode * next
Definition WorkList.h:71
ListNode(const Data &d)
Definition WorkList.h:62
bool empty() const
Definition WorkList.h:86
Node * head
Definition WorkList.h:128
ListNode Node
Definition WorkList.h:75
bool find(const Data &data) const
Definition WorkList.h:91
Data pop()
Definition WorkList.h:109
Node * tail
Definition WorkList.h:129
DataSet nodeSet
Definition WorkList.h:127
Set< Data > DataSet
Definition WorkList.h:74
void push(const Data &data)
Definition WorkList.h:96
for isBitcode
Definition BasicTypes.h:68
llvm::IRBuilder IRBuilder
Definition BasicTypes.h:74
unsigned u32_t
Definition GeneralType.h:47