SVF
LocationSet.h
Go to the documentation of this file.
1 //===- LocationSet.h -- Location set of abstract object-----------------------//
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  * @file: LocationSet.h
25  * @author: yesen
26  * @date: 26 Sep 2014
27  *
28  * LICENSE
29  *
30  */
31 
32 
33 #ifndef LOCATIONSET_H_
34 #define LOCATIONSET_H_
35 
36 
37 #include "Util/BasicTypes.h"
38 
39 namespace SVF
40 {
41 
45 class FieldInfo
46 {
47 public:
48  typedef std::vector<NodePair > ElemNumStridePairVec;
49 
50 private:
53  const Type* elemTy;
54  ElemNumStridePairVec elemNumStridePair;
55 public:
56  FieldInfo(u32_t idx, u32_t byteOff, const Type* ty, ElemNumStridePairVec pa) :
57  fldIdx(idx), byteOffset(byteOff), elemTy(ty), elemNumStridePair(pa)
58  {
59  }
60  inline u32_t getFlattenFldIdx() const
61  {
62  return fldIdx;
63  }
64  inline u32_t getFlattenByteOffset() const
65  {
66  return byteOffset;
67  }
68  inline const Type* getFlattenElemTy() const
69  {
70  return elemTy;
71  }
72  inline const ElemNumStridePairVec& getElemNumStridePairVect() const
73  {
74  return elemNumStridePair;
75  }
76  inline ElemNumStridePairVec::const_iterator elemStridePairBegin() const
77  {
78  return elemNumStridePair.begin();
79  }
80  inline ElemNumStridePairVec::const_iterator elemStridePairEnd() const
81  {
82  return elemNumStridePair.end();
83  }
84 };
85 
86 
87 /*
88  * Location set represents a set of locations in a memory block with following offsets:
89  * { offset + \sum_{i=0}^N (stride_i * j_i) | 0 \leq j_i < M_i }
90  * where N is the size of number-stride pair vector, M_i (stride_i) is i-th number (stride)
91  * in the number-stride pair vector.
92  */
94 {
95  friend class SymbolTableInfo;
96  friend class LocSymTableInfo;
97 public:
99  {
100  NonOverlap, Overlap, Subset, Superset, Same
101  };
102 
104 
107  {}
108 
112  {
113  const ElemNumStridePairVec& vec = ls.getNumStridePair();
114  ElemNumStridePairVec::const_iterator it = vec.begin();
115  ElemNumStridePairVec::const_iterator eit = vec.end();
116  for (; it != eit; ++it)
117  addElemNumStridePair(*it);
118  }
119 
123  {
124  const ElemNumStridePairVec& vec = fi.getElemNumStridePairVect();
125  ElemNumStridePairVec::const_iterator it = vec.begin();
126  ElemNumStridePairVec::const_iterator eit = vec.end();
127  for (; it != eit; ++it)
128  addElemNumStridePair(*it);
129  }
130 
132 
133 
135 
136  inline LocationSet operator+ (const LocationSet& rhs) const
137  {
138  LocationSet ls(rhs);
139  ls.fldIdx += getOffset();
140  ls.byteOffset += getByteOffset();
141  ElemNumStridePairVec::const_iterator it = getNumStridePair().begin();
142  ElemNumStridePairVec::const_iterator eit = getNumStridePair().end();
143  for (; it != eit; ++it)
144  ls.addElemNumStridePair(*it);
145 
146  return ls;
147  }
148  inline const LocationSet& operator= (const LocationSet& rhs)
149  {
150  fldIdx = rhs.fldIdx;
151  byteOffset = rhs.byteOffset;
152  numStridePair = rhs.getNumStridePair();
153  return *this;
154  }
155  inline bool operator< (const LocationSet& rhs) const
156  {
157  if (fldIdx != rhs.fldIdx)
158  return (fldIdx < rhs.fldIdx);
159 // else if (byteOffset != rhs.byteOffset)
160 // return (byteOffset < rhs.byteOffset);
161  else
162  {
163  const ElemNumStridePairVec& pairVec = getNumStridePair();
164  const ElemNumStridePairVec& rhsPairVec = rhs.getNumStridePair();
165  if (pairVec.size() != rhsPairVec.size())
166  return (pairVec.size() < rhsPairVec.size());
167  else
168  {
169  ElemNumStridePairVec::const_iterator it = pairVec.begin();
170  ElemNumStridePairVec::const_iterator rhsIt = rhsPairVec.begin();
171  for (; it != pairVec.end() && rhsIt != rhsPairVec.end(); ++it, ++rhsIt)
172  {
173  if ((*it).first != (*rhsIt).first)
174  return ((*it).first < (*rhsIt).first);
175  else if ((*it).second != (*rhsIt).second)
176  return ((*it).second < (*rhsIt).second);
177  }
178 
179  return false;
180  }
181  }
182  }
183 
184  inline bool operator==(const LocationSet& rhs) const
185  {
186  return this->fldIdx == rhs.fldIdx
187  && this->byteOffset == rhs.byteOffset
188  && this->numStridePair == rhs.numStridePair;
189  }
191 
193 
194  inline Size_t getOffset() const
195  {
196  return fldIdx;
197  }
198  inline Size_t getByteOffset() const
199  {
200  return byteOffset;
201  }
202  inline void setFldIdx(Size_t idx)
203  {
204  fldIdx = idx;
205  }
206  inline void setByteOffset(Size_t os)
207  {
208  byteOffset = os;
209  }
210  inline const ElemNumStridePairVec& getNumStridePair() const
211  {
212  return numStridePair;
213  }
215 
216  void addElemNumStridePair(const NodePair& pair);
217 
219  inline bool isConstantOffset() const
220  {
221  return (numStridePair.size() == 0);
222  }
223 
225  inline bool intersects(const LocationSet& RHS) const
226  {
227  return computeAllLocations().intersects(RHS.computeAllLocations());
228  }
229 
231  static inline LSRelation checkRelation(const LocationSet& LHS, const LocationSet& RHS)
232  {
233  PointsTo lhsLocations = LHS.computeAllLocations();
234  PointsTo rhsLocations = RHS.computeAllLocations();
235  if (lhsLocations.intersects(rhsLocations))
236  {
237  if (lhsLocations == rhsLocations)
238  return Same;
239  else if (lhsLocations.contains(rhsLocations))
240  return Superset;
241  else if (rhsLocations.contains(lhsLocations))
242  return Subset;
243  else
244  return Overlap;
245  }
246  else
247  {
248  return NonOverlap;
249  }
250  }
251 
253  std::string dump() const
254  {
255  std::string str;
256  raw_string_ostream rawstr(str);
257 
258  rawstr << "LocationSet\tField_Index: " << getOffset();
259  rawstr << "\tOffset: " << getByteOffset()
260  << ",\tNum-Stride: {";
261  const ElemNumStridePairVec& vec = getNumStridePair();
262  ElemNumStridePairVec::const_iterator it = vec.begin();
263  ElemNumStridePairVec::const_iterator eit = vec.end();
264  for (; it != eit; ++it)
265  {
266  rawstr << " (" << it->first << "," << it->second << ")";
267  }
268  rawstr << " }\n";
269  return rawstr.str();
270  }
271 private:
273  bool increaseIfNotReachUpperBound(std::vector<NodeID>& indices, const ElemNumStridePairVec& pairVec) const;
274 
276  PointsTo computeAllLocations() const;
277 
279  inline unsigned gcd (unsigned n1, unsigned n2) const
280  {
281  return (n2 == 0) ? n1 : gcd (n2, n1 % n2);
282  }
283 
286  ElemNumStridePairVec numStridePair;
287 };
288 
289 } // End namespace SVF
290 
291 template <> struct std::hash<SVF::LocationSet> {
292  size_t operator()(const SVF::LocationSet &ls) const {
294  return h(std::make_pair(ls.getOffset(), ls.getByteOffset()));
295  }
296 };
297 
298 #endif /* LOCATIONSET_H_ */
const Type * getFlattenElemTy() const
Definition: LocationSet.h:68
PointsTo computeAllLocations() const
Compute all possible locations according to offset and number-stride pairs.
bool intersects(const LocationSet &RHS) const
Return TRUE if we share any location in common with RHS.
Definition: LocationSet.h:225
void setByteOffset(Size_t os)
Definition: LocationSet.h:206
size_t operator()(const SVF::LocationSet &ls) const
Definition: LocationSet.h:292
llvm::Type Type
Definition: BasicTypes.h:75
u32_t getFlattenByteOffset() const
Definition: LocationSet.h:64
Size_t getByteOffset() const
Definition: LocationSet.h:198
ElemNumStridePairVec::const_iterator elemStridePairBegin() const
Definition: LocationSet.h:76
provide extra hash function for std::pair handling
Definition: SVFBasicTypes.h:52
FieldInfo::ElemNumStridePairVec ElemNumStridePairVec
Definition: LocationSet.h:103
u32_t byteOffset
Definition: LocationSet.h:52
u32_t getFlattenFldIdx() const
Definition: LocationSet.h:60
std::string dump() const
Dump location set.
Definition: LocationSet.h:253
void setFldIdx(Size_t idx)
Definition: LocationSet.h:202
bool isConstantOffset() const
Return TRUE if this is a constant location set.
Definition: LocationSet.h:219
const Type * elemTy
Definition: LocationSet.h:53
unsigned u32_t
Definition: SVFBasicTypes.h:75
void addElemNumStridePair(const NodePair &pair)
Definition: LocationSet.cpp:42
LocationSet(const FieldInfo &fi)
Initialization from FieldInfo.
Definition: LocationSet.h:121
LocationSet(Size_t o=0)
Constructor.
Definition: LocationSet.h:106
Size_t byteOffset
offset relative to base
Definition: LocationSet.h:285
unsigned gcd(unsigned n1, unsigned n2) const
Return greatest common divisor.
Definition: LocationSet.h:279
signed long Size_t
Definition: SVFBasicTypes.h:78
llvm::raw_string_ostream raw_string_ostream
Definition: BasicTypes.h:100
Size_t fldIdx
offset relative to base
Definition: LocationSet.h:284
bool operator==(const LocationSet &rhs) const
Definition: LocationSet.h:184
FieldInfo(u32_t idx, u32_t byteOff, const Type *ty, ElemNumStridePairVec pa)
Definition: LocationSet.h:56
static LSRelation checkRelation(const LocationSet &LHS, const LocationSet &RHS)
Check relations of two location sets.
Definition: LocationSet.h:231
ElemNumStridePairVec elemNumStridePair
Definition: LocationSet.h:54
Size_t getOffset() const
Get methods.
Definition: LocationSet.h:194
for isBitcode
Definition: ContextDDA.h:15
LocationSet(const LocationSet &ls)
Copy Constructor.
Definition: LocationSet.h:110
const ElemNumStridePairVec & getElemNumStridePairVect() const
Definition: LocationSet.h:72
std::vector< NodePair > ElemNumStridePairVec
Definition: LocationSet.h:48
const ElemNumStridePairVec & getNumStridePair() const
Definition: LocationSet.h:210
std::pair< NodeID, NodeID > NodePair
ElemNumStridePairVec::const_iterator elemStridePairEnd() const
Definition: LocationSet.h:80
ElemNumStridePairVec numStridePair
element number and stride pair
Definition: LocationSet.h:286
NodeBS PointsTo
Definition: SVFBasicTypes.h:88