SVF
PTAType.h
Go to the documentation of this file.
1 //===- PTAType.h -- PTAType class---------------------------------------------//
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 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  * PTAType.h
25  *
26  * Created on: Oct 06, 2016
27  * Author: Xiaokang Fan
28  */
29 
30 #ifndef PTATYPE_H_
31 #define PTATYPE_H_
32 
33 #include <set>
34 #include <map>
35 #include "Util/BasicTypes.h"
36 
37 namespace SVF
38 {
39 
40 class PAGNode;
41 class PAG;
42 
43 class PTAType
44 {
45 public:
47  PTAType(const Type *ty): type(ty) {}
48 
50  inline const Type *getLLVMType() const
51  {
52  return type;
53  }
54 
56  inline void dump() const
57  {
58  type->dump();
59  }
60 
62 
63  inline bool operator==(const PTAType &ty) const
64  {
65  return type == ty.getLLVMType();
66  }
67 
68  inline bool operator!=(const PTAType &ty) const
69  {
70  return type != ty.getLLVMType();
71  }
72 
73  inline bool operator<(const PTAType &ty) const
74  {
75  return type < ty.getLLVMType();
76  }
77 
78  inline bool operator>(const PTAType &ty) const
79  {
80  return type > ty.getLLVMType();
81  }
83 
84 private:
85  const Type *type;
86 };
87 
88 class TypeSet
89 {
90 public:
91 
93 
94  typedef TypeSetTy::iterator iterator;
95  typedef TypeSetTy::const_iterator const_iterator;
96 
97  // Iterators
99  inline iterator begin()
100  {
101  return typeSet.begin();
102  }
103  inline iterator end()
104  {
105  return typeSet.end();
106  }
107  inline const_iterator begin() const
108  {
109  return typeSet.begin();
110  }
111  inline const_iterator end() const
112  {
113  return typeSet.end();
114  }
116 
118  inline u32_t size() const
119  {
120  return typeSet.size();
121  }
122 
124  inline bool addType(const PTAType &type)
125  {
126  std::pair<iterator, bool> ret = typeSet.insert(type);
127  return ret.second;
128  }
129 
131  inline bool containType(const PTAType &type) const
132  {
133  return typeSet.find(type) != typeSet.end();
134  }
135 
137  // Algorithm set_intersection
138  // Complexity: 2 * (N1 + N2) - 1
139  // N1, N2: number of element in the two typeset
140  inline bool intersect(const TypeSet *typeset) const
141  {
142  if (size() == 1)
143  {
144  const_iterator first = begin();
145  return typeset->containType(*first);
146  }
147  else if (typeset->size() == 1)
148  {
149  const_iterator first = typeset->begin();
150  return containType(*first);
151  }
152  else
153  {
154  const_iterator first1 = typeset->begin(), first2 = begin(),
155  last1 = typeset->end(), last2 = end();
156  const_iterator largest1 = last1, largest2 = last2;
157  largest1--;
158  largest2--;
159  if (*largest1 < *first2 || *largest2 < *first1)
160  return false;
161 
162  while (first1 != last1 && first2 != last2)
163  {
164  if (*first1 < *first2)
165  first1++;
166  else if (*first2 < *first1)
167  first2++;
168  else
169  return true;
170  }
171  return false;
172  }
173  }
174 
176  inline void dumpTypes() const
177  {
178  for (const_iterator it = begin(), eit = end(); it != eit; ++it)
179  {
180  const PTAType &type = *it;
181  type.dump();
182  }
183  }
184 
185 private:
186  TypeSetTy typeSet;
187 };
188 
190 {
191 public:
192 
194 
196 
197  typedef typename VarToTypeSetMapTy::iterator iterator;
198  typedef typename VarToTypeSetMapTy::const_iterator const_iterator;
199 
201 
202  inline iterator begin()
203  {
204  return VarToTypeSetMap.begin();
205  }
206  inline iterator end()
207  {
208  return VarToTypeSetMap.end();
209  }
210  inline const_iterator begin() const
211  {
212  return VarToTypeSetMap.begin();
213  }
214  inline const_iterator end() const
215  {
216  return VarToTypeSetMap.end();
217  }
218  //}@
219 
221  TypeSystem(const PAG *pag)
222  {
223  translateLLVMTypeToPTAType(pag);
224  }
225 
227  inline bool hasTypeSet(NodeID var) const
228  {
229  const_iterator it = VarToTypeSetMap.find(var);
230  return it != VarToTypeSetMap.end();
231  }
232 
234  inline const TypeSet *getTypeSet(NodeID var) const
235  {
236  const_iterator it = VarToTypeSetMap.find(var);
237  assert(it != VarToTypeSetMap.end() && "Can not find typeset for var");
238  return it->second;
239  }
240 
243  inline bool addTypeForVar(NodeID var, const PTAType &type)
244  {
245  iterator it = VarToTypeSetMap.find(var);
246  if (it != VarToTypeSetMap.end())
247  {
248  TypeSet *typeSet = it->second;
249  return typeSet->addType(type);
250  }
251  else
252  {
253  TypeSet *typeSet = new TypeSet;
254  typeSet->addType(type);
255  VarToTypeSetMap[var] = typeSet;
256  return true;
257  }
258  }
259 
262  inline bool addTypeForVar(NodeID var, const Type *type)
263  {
264  PTAType ptaTy(type);
265  return addTypeForVar(var, ptaTy);
266  }
267 
268  void addVarForType(NodeID var, const PTAType &type)
269  {
270  TypeToVarsMapTy::iterator it = typeToVarsMap.find(type);
271  if (it == typeToVarsMap.end())
272  {
273  NodeBS nodes;
274  nodes.set(var);
275  typeToVarsMap[type] = nodes;
276  }
277  else
278  {
279  NodeBS &nodes = it->second;
280  nodes.set(var);
281  }
282  }
283 
284  void addVarForType(NodeID var, const Type *type)
285  {
286  PTAType ptaTy(type);
287  return addVarForType(var, ptaTy);
288  }
289 
290  inline bool hasVarsForType(const PTAType &type) const
291  {
292  TypeToVarsMapTy::const_iterator it = typeToVarsMap.find(type);
293  return it != typeToVarsMap.end();
294  }
295 
297  {
298  TypeToVarsMapTy::iterator it = typeToVarsMap.find(type);
299  assert(it != typeToVarsMap.end() && "Can not find vars for type");
300  return it->second;
301  }
302 
304 
305  void printTypeSystem() const
307  {
308  for (const_iterator it = VarToTypeSetMap.begin(),
309  eit = VarToTypeSetMap.end(); it != eit; ++it)
310  {
311  SVFUtil::errs() << "Var: " << it->first << '\n';
312  SVFUtil::errs() << "types:\n";
313  const TypeSet *typeSet = it->second;
314  typeSet->dumpTypes();
315  SVFUtil::errs() << '\n';
316  }
317  }
319 
320 private:
321  /*
322  * Translate llvm type into ptatype and build the pagnode to ptatype map
323  *
324  * Kinds of PAGNode:
325  * ValPN: GepValPN, DummyValPN
326  * ObjPN: GepObjPN, FIObjPN, DummyObjPN
327  * RetPN
328  * VarArgPN
329  */
331  {
332  for (PAG::const_iterator it = pag->begin(); it != pag->end(); ++it)
333  {
334  const PAGNode *pagNode = it->second;
335  if (pagNode->hasValue() == false)
336  continue;
337 
338  const Value *value = pagNode->getValue();
339  const Type *valType = value->getType();
340 
341  const Type *nodeType = valType;
342 
343  if (const GepValPN *gepvalnode = SVFUtil::dyn_cast<GepValPN>(pagNode))
344  {
345  nodeType = gepvalnode->getType();
346  }
347  else if (SVFUtil::isa<RetPN>(pagNode))
348  {
349  const llvm::PointerType *ptrTy = SVFUtil::dyn_cast<llvm::PointerType>(valType);
350  const llvm::FunctionType *funTy = SVFUtil::dyn_cast<llvm::FunctionType>(ptrTy->getElementType());
351  nodeType = funTy->getReturnType();
352  }
353 
354  PTAType ptaType(nodeType);
355  if (addTypeForVar(pagNode->getId(), ptaType))
356  addVarForType(pagNode->getId(), ptaType);
357  }
358  }
359 
360 private:
361  VarToTypeSetMapTy VarToTypeSetMap;
363  TypeToVarsMapTy typeToVarsMap;
364 };
365 
366 } // End namespace SVF
367 
368 #endif /* PTATYPE_H_ */
iterator begin()
Iterators.
Definition: GenericGraph.h:365
VarToTypeSetMapTy::const_iterator const_iterator
Definition: PTAType.h:198
Map< NodeID, TypeSet * > VarToTypeSetMapTy
Definition: PTAType.h:193
TypeToVarsMapTy typeToVarsMap
Definition: PTAType.h:363
bool addTypeForVar(NodeID var, const PTAType &type)
Definition: PTAType.h:243
void addVarForType(NodeID var, const Type *type)
Definition: PTAType.h:284
void translateLLVMTypeToPTAType(const PAG *pag)
Definition: PTAType.h:330
llvm::FunctionType FunctionType
Definition: BasicTypes.h:109
void dump() const
Dump the type.
Definition: PTAType.h:56
std::set< Key, Compare, Allocator > OrderedSet
bool operator<(const PTAType &ty) const
Definition: PTAType.h:73
iterator begin()
Iterators.
Definition: PTAType.h:202
u32_t NodeID
Definition: SVFBasicTypes.h:80
bool containType(const PTAType &type) const
Contain a ptatype or not.
Definition: PTAType.h:131
llvm::Type Type
Definition: BasicTypes.h:75
#define assert(ex)
Definition: util.h:141
llvm::PointerType PointerType
Definition: BasicTypes.h:108
raw_ostream & errs()
Overwrite llvm::errs()
Definition: SVFUtil.h:53
bool operator!=(const PTAType &ty) const
Definition: PTAType.h:68
bool addTypeForVar(NodeID var, const Type *type)
Definition: PTAType.h:262
Definition: PAG.h:47
bool operator==(const PTAType &ty) const
Operator overloading.
Definition: PTAType.h:63
const Value * getValue() const
Get/has methods of the components.
Definition: PAGNode.h:93
std::unordered_map< Key, Value, Hash, KeyEqual, Allocator > Map
Definition: SVFBasicTypes.h:98
IDToNodeMapTy::const_iterator const_iterator
Definition: GenericGraph.h:338
unsigned u32_t
Definition: SVFBasicTypes.h:75
bool intersect(const TypeSet *typeset) const
Intersect with another typeset or not.
Definition: PTAType.h:140
const_iterator end() const
Definition: PTAType.h:111
void dumpTypes() const
Dump all types in the typeset.
Definition: PTAType.h:176
void addVarForType(NodeID var, const PTAType &type)
Definition: PTAType.h:268
bool operator>(const PTAType &ty) const
Definition: PTAType.h:78
iterator begin()
Definition: PTAType.h:99
const Type * getLLVMType() const
Get the contained llvm type.
Definition: PTAType.h:50
TypeSetTy::const_iterator const_iterator
Definition: PTAType.h:95
const_iterator end() const
Definition: PTAType.h:214
OrderedSet< PTAType > allPTATypes
Definition: PTAType.h:362
bool hasValue() const
Definition: PAGNode.h:109
VarToTypeSetMapTy::iterator iterator
Definition: PTAType.h:197
const TypeSet * getTypeSet(NodeID var) const
Get a var&#39;s typeset.
Definition: PTAType.h:234
TypeSetTy typeSet
Definition: PTAType.h:186
TypeSetTy::iterator iterator
Definition: PTAType.h:94
const_iterator begin() const
Definition: PTAType.h:107
bool hasTypeSet(NodeID var) const
Has typeset or not.
Definition: PTAType.h:227
iterator end()
Definition: PTAType.h:103
OrderedMap< PTAType, NodeBS > TypeToVarsMapTy
Definition: PTAType.h:195
PTAType(const Type *ty)
Constructor.
Definition: PTAType.h:47
for isBitcode
Definition: ContextDDA.h:15
NodeID getId() const
Get ID.
Definition: GenericGraph.h:164
llvm::SparseBitVector NodeBS
Definition: SVFBasicTypes.h:87
iterator end()
Definition: PTAType.h:206
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:343
const_iterator begin() const
Definition: PTAType.h:210
VarToTypeSetMapTy VarToTypeSetMap
Definition: PTAType.h:361
u32_t size() const
Number of types contained.
Definition: PTAType.h:118
OrderedSet< PTAType > TypeSetTy
Definition: PTAType.h:92
const Type * type
Definition: PTAType.h:85
TypeSystem(const PAG *pag)
Constructor.
Definition: PTAType.h:221
llvm::Value Value
Definition: BasicTypes.h:78
NodeBS & getVarsForType(const PTAType &type)
Definition: PTAType.h:296
bool hasVarsForType(const PTAType &type) const
Definition: PTAType.h:290
bool addType(const PTAType &type)
Add a ptatype.
Definition: PTAType.h:124
std::map< Key, Value, Compare, Allocator > OrderedMap