SVF
SVFBasicTypes.h
Go to the documentation of this file.
1 //===- SVFBasicTypes.h -- Basic types used in SVF-------------------------------//
2 //
3 // SVF: Static Value-Flow Analysis
4 //
5 // Copyright (C) <2013-> <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 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 General Public License for more details.
17 
18 // You should have received a copy of the GNU General Public License
19 // along with this program. If not, see <http://www.gnu.org/licenses/>.
20 //
21 //===----------------------------------------------------------------------===//
22 
23 /*
24  * BasicTypes.h
25  *
26  * Created on: Apr 1, 2014
27  * Author: Yulei Sui
28  */
29 
30 
31 #ifndef INCLUDE_UTIL_SVFBASICTYPES_H_
32 #define INCLUDE_UTIL_SVFBASICTYPES_H_
33 
34 #include <llvm/ADT/SparseBitVector.h> // for points-to
35 #include <llvm/Support/raw_ostream.h> // for output
36 #include <llvm/Support/CommandLine.h> // for command line options
37 #include <llvm/ADT/StringMap.h> // for StringMap
38 
39 #include <vector>
40 #include <list>
41 #include <set>
42 #include <unordered_set>
43 #include <map>
44 #include <unordered_map>
45 #include <stack>
46 #include <deque>
47 
48 namespace SVF
49 {
50 
52 template <class T> struct Hash;
53 
54 template <class S, class T> struct Hash<std::pair<S, T>> {
55  // Pairing function from: http://szudzik.com/ElegantPairing.pdf
56  static size_t szudzik(size_t a, size_t b)
57  {
58  return a > b ? b * b + a : a * a + a + b;
59  }
60 
61  size_t operator()(const std::pair<S, T> &t) const {
62  Hash<decltype(t.first)> first;
63  Hash<decltype(t.second)> second;
64  return szudzik(first(t.first), second(t.second));
65  }
66 };
67 
68 template <class T> struct Hash {
69  size_t operator()(const T &t) const {
70  std::hash<T> h;
71  return h(t);
72  }
73 };
74 
75 typedef unsigned u32_t;
76 typedef unsigned long long u64_t;
77 typedef signed s32_t;
78 typedef signed long Size_t;
79 
80 typedef u32_t NodeID;
81 typedef u32_t EdgeID;
82 typedef unsigned SymID;
83 typedef unsigned CallSiteID;
84 typedef unsigned ThreadID;
85 typedef unsigned Version;
86 
87 typedef llvm::SparseBitVector<> NodeBS;
88 typedef NodeBS PointsTo;
89 typedef PointsTo AliasSet;
90 
91 template <typename Key, typename Hash = Hash<Key>, typename KeyEqual = std::equal_to<Key>,
92  typename Allocator = std::allocator<Key>>
93 using Set = std::unordered_set<Key, Hash, KeyEqual, Allocator>;
94 
95 template<typename Key, typename Value, typename Hash = Hash<Key>,
96  typename KeyEqual = std::equal_to<Key>,
97  typename Allocator = std::allocator<std::pair<const Key, Value>>>
98 using Map = std::unordered_map<Key, Value, Hash, KeyEqual, Allocator>;
99 
100 template<typename Key, typename Compare = std::less<Key>, typename Allocator = std::allocator<Key>>
101 using OrderedSet = std::set<Key, Compare, Allocator>;
102 
103 template<typename Key, typename Value, typename Compare = std::less<Key>,
104  typename Allocator = std::allocator<std::pair<const Key, Value>>>
105 using OrderedMap = std::map<Key, Value, Compare, Allocator>;
106 
107 template <typename T, unsigned N>
108 using SmallVector = llvm::SmallVector<T, N>;
109 
110 typedef std::pair<NodeID, NodeID> NodePair;
111 typedef std::pair<NodeID, Version> VersionedVar;
116 typedef std::vector<NodeID> NodeVector;
117 typedef std::vector<EdgeID> EdgeVector;
118 typedef std::stack<NodeID> NodeStack;
119 typedef std::list<NodeID> NodeList;
120 typedef std::deque<NodeID> NodeDeque;
123 typedef NodeSet EdgeSet;
124 typedef SmallVector16 CallStrCxt;
125 typedef llvm::StringMap<u32_t> StringMap;
126 
128 #define DBOUT(TYPE, X) DEBUG_WITH_TYPE(TYPE, X)
129 #define DOSTAT(X) X
130 #define DOTIMESTAT(X) X
131 
133 #define DGENERAL "general"
134 
135 #define DPAGBuild "pag"
136 #define DMemModel "mm"
137 #define DMemModelCE "mmce"
138 #define DCOMModel "comm"
139 #define DDDA "dda"
140 #define DDumpPT "dumppt"
141 #define DRefinePT "sbpt"
142 #define DCache "cache"
143 #define DWPA "wpa"
144 #define DMSSA "mssa"
145 #define DInstrument "ins"
146 #define DAndersen "ander"
147 #define DSaber "saber"
148 #define DMTA "mta"
149 #define DCHA "cha"
150 
151 /*
152  * Number of clock ticks per second. A clock tick is the unit by which
153  * processor time is measured and is returned by 'clock'.
154  */
155 #define TIMEINTERVAL 1000
156 #define CLOCK_IN_MS() (clock() / (CLOCKS_PER_SEC / TIMEINTERVAL))
157 
158 class SVFValue
159 {
160 
161 public:
162  typedef s32_t GNodeK;
163 
165  {
171  };
172 
173 private:
174  const std::string value;
175  GNodeK kind;
176 public:
178  SVFValue(const llvm::StringRef& val, SVFValKind k): value(val), kind(k)
179  {
180  }
181 
183  inline GNodeK getKind() const
184  {
185  return kind;
186  }
187 
189  // and duplicated elements in the set are not inserted (binary tree comparison)
191  bool operator()(const SVFValue* lhs, const SVFValue* rhs) const
192  {
193  return lhs->value < rhs->value;
194  }
195 
196  inline bool operator==(SVFValue* rhs) const
197  {
198  return value == rhs->value;
199  }
200 
201  inline bool operator!=(SVFValue* rhs) const
202  {
203  return value != rhs->value;
204  }
206 
207  const llvm::StringRef getName() const
208  {
209  return value;
210  }
211 
212  const std::string& getValue() const
213  {
214  return value;
215  }
216 
218 
220  {
221  o << node.getName();
222  return o;
223  }
225 
226  static inline bool classof(const SVFValue *node)
227  {
228  return node->getKind() == SVFValue::SVFVal ||
229  node->getKind() == SVFValue::SVFFunc ||
230  node->getKind() == SVFValue::SVFGlob ||
231  node->getKind() == SVFValue::SVFBB ||
232  node->getKind() == SVFValue::SVFInst;
233  }
234 };
235 
236 } // End namespace SVF
237 
238 
240 template <typename T, unsigned N>
241 struct std::hash<SVF::SmallVector<T, N>>
242 {
243  size_t operator()(const SVF::SmallVector<T, N> &sv) const {
244  if (sv.empty()) return 0;
245  if (sv.size() == 1) return sv[0];
246 
247  // Iterate and accumulate the hash.
248  size_t hash = 0;
250  std::hash<T> ht;
251  for (const T &t : sv)
252  {
253  hash = hts(std::make_pair(ht(t), hash));
254  }
255 
256  return hash;
257  }
258 };
259 
260 #endif /* INCLUDE_UTIL_SVFBASICTYPES_H_ */
std::set< Key, Compare, Allocator > OrderedSet
Set< NodeID > NodeSet
SVFValue(const llvm::StringRef &val, SVFValKind k)
Constructor.
llvm::raw_ostream raw_ostream
LLVM outputs.
Definition: BasicTypes.h:99
u32_t NodeID
Definition: SVFBasicTypes.h:80
std::stack< NodeID > NodeStack
provide extra hash function for std::pair handling
Definition: SVFBasicTypes.h:52
signed s32_t
Definition: SVFBasicTypes.h:77
PointsTo AliasSet
Definition: SVFBasicTypes.h:89
const llvm::StringRef getName() const
Set< NodePair > NodePairSet
std::unordered_map< Key, Value, Hash, KeyEqual, Allocator > Map
Definition: SVFBasicTypes.h:98
unsigned Version
Definition: SVFBasicTypes.h:85
unsigned u32_t
Definition: SVFBasicTypes.h:75
static bool classof(const SVFValue *node)
std::list< NodeID > NodeList
size_t operator()(const T &t) const
Definition: SVFBasicTypes.h:69
llvm::SmallVector< T, N > SmallVector
GNodeK getKind() const
Get the type of this SVFValue.
static size_t szudzik(size_t a, size_t b)
Definition: SVFBasicTypes.h:56
bool operator!=(SVFValue *rhs) const
Map< NodePair, NodeID > NodePairMap
std::unordered_set< Key, Hash, KeyEqual, Allocator > Set
Definition: SVFBasicTypes.h:93
signed long Size_t
Definition: SVFBasicTypes.h:78
SmallVector16 CallStrCxt
unsigned SymID
Definition: SVFBasicTypes.h:82
NodeSet EdgeSet
unsigned CallSiteID
Definition: SVFBasicTypes.h:83
bool operator==(SVFValue *rhs) const
std::vector< NodeID > NodeVector
SmallVector< u32_t, 16 > SmallVector16
SmallVector< u32_t, 8 > SmallVector8
llvm::StringRef StringRef
Definition: BasicTypes.h:102
std::vector< EdgeID > EdgeVector
unsigned ThreadID
Definition: SVFBasicTypes.h:84
u32_t EdgeID
Definition: SVFBasicTypes.h:81
const std::string value
raw_ostream & operator<<(raw_ostream &o, const std::pair< F, S > &var)
Definition: BasicTypes.h:341
for isBitcode
Definition: ContextDDA.h:15
size_t operator()(const SVF::SmallVector< T, N > &sv) const
std::deque< NodeID > NodeDeque
const std::string & getValue() const
llvm::SparseBitVector NodeBS
Definition: SVFBasicTypes.h:87
OrderedSet< NodeID > OrderedNodeSet
std::pair< NodeID, Version > VersionedVar
std::pair< NodeID, NodeID > NodePair
GNodeK kind
Type of this SVFValue.
llvm::StringMap< u32_t > StringMap
NodeBS PointsTo
Definition: SVFBasicTypes.h:88
size_t operator()(const std::pair< S, T > &t) const
Definition: SVFBasicTypes.h:61
unsigned long long u64_t
Definition: SVFBasicTypes.h:76
bool operator()(const SVFValue *lhs, const SVFValue *rhs) const
Add the hash function for std::set (we also can overload operator< to implement this) ...
std::map< Key, Value, Compare, Allocator > OrderedMap