Static Value-Flow Analysis
SVFValue.cpp
Go to the documentation of this file.
1 #include "SVFIR/SVFValue.h"
2 #include "Util/SVFUtil.h"
3 #include "Graphs/GenericGraph.h"
4 
5 using namespace SVF;
6 using namespace SVFUtil;
7 
9 void StInfo::addFldWithType(u32_t fldIdx, const SVFType* type, u32_t elemIdx)
10 {
11  fldIdxVec.push_back(fldIdx);
12  elemIdxVec.push_back(elemIdx);
13  fldIdx2TypeMap[fldIdx] = type;
14 }
15 
19 //{@
21 {
22  Map<u32_t, const SVFType*>::const_iterator it = fldIdx2TypeMap.find(fldIdx);
23  if(it!=fldIdx2TypeMap.end())
24  return it->second;
25  return nullptr;
26 }
27 
29 {
30  assert(hasLoopInfo(bb) && "loopinfo does not exist (bb not in a loop)");
31  Map<const SVFBasicBlock*, LoopBBs>::const_iterator mapIter = bb2LoopMap.find(bb);
32  return mapIter->second;
33 }
34 
36 {
37  if (hasLoopInfo(bb))
38  {
39  const LoopBBs blocks = getLoopInfo(bb);
40  if (!blocks.empty())
41  {
42  for (const SVFBasicBlock* block : blocks)
43  {
44  for (const SVFBasicBlock* succ : block->getSuccessors())
45  {
46  if ((std::find(blocks.begin(), blocks.end(), succ)==blocks.end()))
47  exitbbs.push_back(succ);
48  }
49  }
50  }
51  }
52 }
53 
54 bool SVFLoopAndDomInfo::dominate(const SVFBasicBlock* bbKey, const SVFBasicBlock* bbValue) const
55 {
56  if (bbKey == bbValue)
57  return true;
58 
59  // An unreachable node is dominated by anything.
60  if (isUnreachable(bbValue))
61  {
62  return true;
63  }
64 
65  // And dominates nothing.
66  if (isUnreachable(bbKey))
67  {
68  return false;
69  }
70 
71  const Map<const SVFBasicBlock*,BBSet>& dtBBsMap = getDomTreeMap();
72  Map<const SVFBasicBlock*,BBSet>::const_iterator mapIter = dtBBsMap.find(bbKey);
73  if (mapIter != dtBBsMap.end())
74  {
75  const BBSet & dtBBs = mapIter->second;
76  if (dtBBs.find(bbValue) != dtBBs.end())
77  {
78  return true;
79  }
80  }
81 
82  return false;
83 }
84 
85 bool SVFLoopAndDomInfo::postDominate(const SVFBasicBlock* bbKey, const SVFBasicBlock* bbValue) const
86 {
87  if (bbKey == bbValue)
88  return true;
89 
90  // An unreachable node is dominated by anything.
91  if (isUnreachable(bbValue))
92  {
93  return true;
94  }
95 
96  // And dominates nothing.
97  if (isUnreachable(bbKey))
98  {
99  return false;
100  }
101 
102  const Map<const SVFBasicBlock*,BBSet>& dtBBsMap = getPostDomTreeMap();
103  Map<const SVFBasicBlock*,BBSet>::const_iterator mapIter = dtBBsMap.find(bbKey);
104  if (mapIter != dtBBsMap.end())
105  {
106  const BBSet & dtBBs = mapIter->second;
107  if (dtBBs.find(bbValue) != dtBBs.end())
108  {
109  return true;
110  }
111  }
112  return false;
113 }
114 
116 {
117  assert(A && B && "Pointers are not valid");
118  assert(A->getParent() == B->getParent() &&
119  "Two blocks are not in same function");
120 
121  // Use level information to go up the tree until the levels match. Then
122  // continue going up til we arrive at the same node.
123  while (A != B)
124  {
125  // no common PDominator
126  if(A == NULL) return NULL;
127  const auto lvA = getBBPDomLevel().find(A);
128  const auto lvB = getBBPDomLevel().find(B);
129  assert(lvA != getBBPDomLevel().end() && lvB != getBBPDomLevel().end());
130 
131  if (lvA->second < lvB->second) std::swap(A, B);
132 
133  const auto lvAIdom = getBB2PIdom().find(A);
134  assert(lvAIdom != getBB2PIdom().end());
135  A = lvAIdom->second;
136  }
137 
138  return A;
139 }
140 
142 {
143  if (hasLoopInfo(bb))
144  {
145  const LoopBBs& blocks = getLoopInfo(bb);
146  assert(!blocks.empty() && "no available loop info?");
147  return blocks.front() == bb;
148  }
149  return false;
150 }
151 
153  bool declare, bool intrinsic, bool adt, bool varg,
154  SVFLoopAndDomInfo* ld)
155  : SVFValue(ty, SVFValue::SVFFunc), isDecl(declare), intrinsic(intrinsic),
156  addrTaken(adt), isUncalled(false), isNotRet(false), varArg(varg),
157  funcType(ft), loopAndDom(ld), realDefFun(nullptr), exitBlock(nullptr)
158 {
159 }
160 
162 {
163  for(const SVFBasicBlock* bb : allBBs)
164  delete bb;
165  for(const SVFArgument* arg : allArgs)
166  delete arg;
167  delete loopAndDom;
168 }
169 
171 {
172  return allArgs.size();
173 }
174 
176 {
177  assert (idx < allArgs.size() && "getArg() out of range!");
178  return allArgs[idx];
179 }
180 
182 {
183  return varArg;
184 }
185 
187 {
188  assert(hasBasicBlock() && "function does not have any Basicblock, external function?");
189  assert(exitBlock && "must have an exitBlock");
190  return exitBlock;
191 }
192 
194 {
195  assert(!exitBlock && "have already set exit Basicblock!");
196  exitBlock = bb;
197 }
198 
200  : SVFValue(ty, SVFValue::SVFBB), fun(f)
201 {
202 }
203 
205 {
206 
207 }
208 
213 {
214  u32_t i = 0;
215  for (const SVFBasicBlock* SuccBB: succBBs)
216  {
217  if (SuccBB == Succ)
218  return i;
219  i++;
220  }
221  assert(false && "Didn't find successor edge?");
222  return 0;
223 }
224 
226 {
227  u32_t i = 0;
228  for (const SVFBasicBlock* SuccBB: succBBs)
229  {
230  if (SuccBB == Succ)
231  return i;
232  i++;
233  }
234  assert(false && "Didn't find successor edge?");
235  return 0;
236 }
237 
242 {
243  u32_t pos = 0;
244  for (const SVFBasicBlock* PredBB : succbb->getPredecessors())
245  {
246  if(PredBB == this)
247  return pos;
248  ++pos;
249  }
250  assert(false && "Didn't find predecessor edge?");
251  return pos;
252 }
254 {
255  u32_t pos = 0;
256  for (const SVFBasicBlock* PredBB : succbb->getPredecessors())
257  {
258  if(PredBB == this)
259  return pos;
260  ++pos;
261  }
262  assert(false && "Didn't find predecessor edge?");
263  return pos;
264 }
265 
267  bool tm, bool isRet, SVFValKind k)
268  : SVFValue(ty, k), bb(b), terminator(tm), ret(isRet)
269 {
270 }
271 
272 __attribute__((weak))
274 {
275  assert("SVFValue::toString should be implemented or supported by fronted" && false);
276  abort();
277 }
278 
279 __attribute__((weak))
281 {
282  assert("SVFBaseNode::valueOnlyToString should be implemented or supported by fronted" && false);
283  abort();
284 }
return NULL
Definition: cJSON.cpp:1173
newitem type
Definition: cJSON.cpp:2739
#define false
Definition: cJSON.cpp:70
const cJSON *const b
Definition: cJSON.h:255
const char *const string
Definition: cJSON.h:172
const std::string valueOnlyToString() const
Definition: LLVMUtil.cpp:688
u32_t getBBPredecessorPos(const SVFBasicBlock *succbb)
Definition: SVFValue.cpp:241
const std::vector< const SVFBasicBlock * > & getSuccessors() const
Definition: SVFValue.h:606
std::vector< const SVFBasicBlock * > succBBs
all successor BasicBlocks of this BasicBlock
Definition: SVFValue.h:533
u32_t getBBSuccessorPos(const SVFBasicBlock *succbb)
Definition: SVFValue.cpp:212
~SVFBasicBlock() override
Definition: SVFValue.cpp:204
const std::vector< const SVFBasicBlock * > & getPredecessors() const
Definition: SVFValue.h:611
SVFBasicBlock()=delete
const SVFFunction * getParent() const
Definition: SVFValue.h:584
const SVFArgument * getArg(u32_t idx) const
Definition: SVFValue.cpp:175
bool varArg
return true if this function never returns
Definition: SVFValue.h:316
SVFBasicBlock * exitBlock
all formal arguments of this function
Definition: SVFValue.h:322
virtual ~SVFFunction()
Definition: SVFValue.cpp:161
std::vector< const SVFArgument * > allArgs
all BasicBlocks of this function
Definition: SVFValue.h:321
SVFFunction(void)=delete
bool hasBasicBlock() const
Definition: SVFValue.h:404
std::vector< const SVFBasicBlock * > allBBs
the definition of a function across multiple modules
Definition: SVFValue.h:320
u32_t arg_size() const
Definition: SVFValue.cpp:170
void setExitBlock(SVFBasicBlock *bb)
Definition: SVFValue.cpp:193
const SVFBasicBlock * getExitBB() const
Definition: SVFValue.cpp:186
bool isVarArg() const
Definition: SVFValue.cpp:181
SVFLoopAndDomInfo * loopAndDom
FunctionType, which is different from the type (PointerType) of this SVFFunction.
Definition: SVFValue.h:318
SVFInstruction(void)=delete
const LoopBBs & getLoopInfo(const SVFBasicBlock *bb) const
Definition: SVFValue.cpp:28
std::vector< const SVFBasicBlock * > BBList
Definition: SVFValue.h:56
bool dominate(const SVFBasicBlock *bbKey, const SVFBasicBlock *bbValue) const
Definition: SVFValue.cpp:54
bool isLoopHeader(const SVFBasicBlock *bb) const
Definition: SVFValue.cpp:141
void getExitBlocksOfLoop(const SVFBasicBlock *bb, BBList &exitbbs) const
Definition: SVFValue.cpp:35
const SVFBasicBlock * findNearestCommonPDominator(const SVFBasicBlock *A, const SVFBasicBlock *B) const
find nearest common post dominator of two basic blocks
Definition: SVFValue.cpp:115
Set< const SVFBasicBlock * > BBSet
Definition: SVFValue.h:55
bool postDominate(const SVFBasicBlock *bbKey, const SVFBasicBlock *bbValue) const
Definition: SVFValue.cpp:85
std::string toString() const
Needs to be implemented by a SVF front end.
Definition: LLVMUtil.cpp:663
const SVFType * getOriginalElemType(u32_t fldIdx) const
Definition: SVFValue.cpp:20
void addFldWithType(u32_t fldIdx, const SVFType *type, u32_t elemIdx)
Add field index and element index and their corresponding type.
Definition: SVFValue.cpp:9
for isBitcode
Definition: BasicTypes.h:68
__attribute__((weak)) std
Definition: SVFType.cpp:10
std::unordered_map< Key, Value, Hash, KeyEqual, Allocator > Map
Definition: GeneralType.h:101
unsigned u32_t
Definition: GeneralType.h:46