SVF
PathCondAllocator.h
Go to the documentation of this file.
1 //===- PathCondAllocator.h -- Path condition manipulation---------------------//
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  * PathAllocator.h
25  *
26  * Created on: Apr 3, 2014
27  * Author: Yulei Sui
28  */
29 
30 #ifndef PATHALLOCATOR_H_
31 #define PATHALLOCATOR_H_
32 
33 #include "Util/SVFModule.h"
34 #include "SVF-FE/DataFlowUtil.h"
35 #include "Util/Conditions.h"
36 #include "Util/WorkList.h"
37 
38 namespace SVF
39 {
40 
45 {
46 
47 public:
49 
50  typedef DdNode Condition;
52  typedef Map<const BasicBlock*, CondPosMap > BBCondMap; // map bb to a Condition
53  typedef Map<const Condition*, const Instruction* > CondToTermInstMap; // map a condition to its branch instruction
58 
60 
63  {
65  }
68  {
69  destroy();
70  }
71  static inline Condition* trueCond()
72  {
73  return getBddCondManager()->getTrueCond();
74  }
75 
76  static inline Condition* falseCond()
77  {
78  return getBddCondManager()->getFalseCond();
79  }
80 
82 
83  static inline u32_t getMemUsage()
84  {
86  }
87  static inline u32_t getCondNum()
88  {
89  return getBddCondManager()->getCondNumber();
90  }
91  static inline u32_t getMaxLiveCondNumber()
92  {
94  }
96 
98  void allocate(const SVFModule* module);
99 
101  inline const Instruction* getCondInst(const Condition* cond) const
102  {
103  CondToTermInstMap::const_iterator it = condToInstMap.find(cond);
104  assert(it!=condToInstMap.end() && "this should be a fresh condition");
105  return it->second;
106  }
107 
109  inline DominatorTree* getDT(const Function* fun)
110  {
111  return cfInfoBuilder.getDT(fun);
112  }
115  {
116  return cfInfoBuilder.getPostDT(fun);
117  }
119  inline LoopInfo* getLoopInfo(const Function* f)
120  {
121  return cfInfoBuilder.getLoopInfo(f);
122  }
123 
125 
126  inline Condition* condAnd(Condition* lhs, Condition* rhs)
127  {
128  return bddCondMgr->AND(lhs,rhs);
129  }
130  inline Condition* condOr(Condition* lhs, Condition* rhs)
131  {
132  return bddCondMgr->OR(lhs,rhs);
133  }
134  inline Condition* condNeg(Condition* cond)
135  {
136  return bddCondMgr->NEG(cond);
137  }
138  inline Condition* getTrueCond() const
139  {
140  return bddCondMgr->getTrueCond();
141  }
142  inline Condition* getFalseCond() const
143  {
144  return bddCondMgr->getFalseCond();
145  }
147  inline Condition* getCond(u32_t i) const
148  {
149  IndexToConditionMap::const_iterator it = indexToDDNodeMap.find(i);
150  assert(it!=indexToDDNodeMap.end() && "condition not found!");
151  return it->second;
152  }
154  inline NodeBS exactCondElem(Condition* cond)
155  {
156  NodeBS elems;
157  bddCondMgr->BddSupport(cond,elems);
158  return elems;
159  }
161  inline void markForRelease(Condition* cond)
162  {
163  bddCondMgr->markForRelease(cond);
164  }
166  inline void printDbg(Condition* cond)
167  {
168  bddCondMgr->printDbg(cond);
169  }
170  inline std::string dumpCond(Condition* cond) const
171  {
172  return bddCondMgr->dumpStr(cond);
173  }
175 
177 
178  virtual Condition* ComputeIntraVFGGuard(const BasicBlock* src, const BasicBlock* dst);
179  virtual Condition* ComputeInterCallVFGGuard(const BasicBlock* src, const BasicBlock* dst, const BasicBlock* callBB);
180  virtual Condition* ComputeInterRetVFGGuard(const BasicBlock* src, const BasicBlock* dst, const BasicBlock* retBB);
181 
184  virtual Condition* getPHIComplementCond(const BasicBlock* BB1, const BasicBlock* BB2, const BasicBlock* BB0);
185 
186  inline void clearCFCond()
187  {
188  bbToCondMap.clear();
189  }
191  inline void setCurEvalVal(const Value* val)
192  {
193  curEvalVal = val;
194  }
196  inline const Value* getCurEvalVal() const
197  {
198  return curEvalVal;
199  }
201 
203  void printPathCond();
204 
205 private:
206 
208  virtual void allocateForBB(const BasicBlock& bb);
209 
211 
212  void setBranchCond(const BasicBlock *bb, const BasicBlock *succ, Condition* cond);
215  Condition* getBranchCond(const BasicBlock * bb, const BasicBlock *succ) const;
217  inline Condition* getEvalBrCond(const BasicBlock * bb, const BasicBlock *succ)
218  {
219  if(const Value* val = getCurEvalVal())
220  return evaluateBranchCond(bb, succ, val);
221  else
222  return getBranchCond(bb,succ);
223  }
225 
227  Condition* evaluateBranchCond(const BasicBlock * bb, const BasicBlock *succ, const Value* val) ;
230  Condition* evaluateLoopExitBranch(const BasicBlock * bb, const BasicBlock *succ);
232  Condition* evaluateTestNullLikeExpr(const BranchInst* brInst, const BasicBlock *succ, const Value* val);
234  Condition* evaluateProgExit(const BranchInst* brInst, const BasicBlock *succ);
236  void collectBBCallingProgExit(const BasicBlock& bb);
237  bool isBBCallsProgExit(const BasicBlock* bb);
239 
241 
242  bool isEQCmp(const CmpInst* cmp) const;
245  bool isNECmp(const CmpInst* cmp) const;
247  bool isTestNullExpr(const Value* test,const Value* val) const;
249  bool isTestNotNullExpr(const Value* test,const Value* val) const;
251  bool isTestContainsNullAndTheValue(const CmpInst* cmp, const Value* val) const;
253 
254 
256 
257  inline bool setCFCond(const BasicBlock* bb, Condition* cond)
258  {
259  BBToCondMap::iterator it = bbToCondMap.find(bb);
260  if(it!=bbToCondMap.end() && it->second == cond)
261  return false;
262 
263  bbToCondMap[bb] = cond;
264  return true;
265  }
266  inline Condition* getCFCond(const BasicBlock* bb) const
267  {
268  BBToCondMap::const_iterator it = bbToCondMap.find(bb);
269  if(it==bbToCondMap.end())
270  {
271  return getFalseCond();
272  }
273  return it->second;
274  }
276 
278  inline Condition* createNewCond(u32_t i)
279  {
280  assert(indexToDDNodeMap.find(i)==indexToDDNodeMap.end() && "This should be fresh index to create new BDD");
281  Condition* d = bddCondMgr->Cudd_bdd(i);
282  indexToDDNodeMap[i] = d;
283  return d;
284  }
286  inline Condition* newCond(const Instruction* inst)
287  {
288  Condition* cond = createNewCond(totalCondNum++);
289  assert(condToInstMap.find(cond)==condToInstMap.end() && "this should be a fresh condition");
290  condToInstMap[cond] = inst;
291  return cond;
292  }
293 
296  {
297  if(bddCondMgr==nullptr)
298  bddCondMgr = new BddCondManager();
299  return bddCondMgr;
300  }
301 
303  void destroy();
304 
305  CondToTermInstMap condToInstMap;
307  FunToExitBBsMap funToExitBBsMap;
308  BBToCondMap bbToCondMap;
309  const Value* curEvalVal;
310 
311 protected:
313  BBCondMap bbConds;
314  IndexToConditionMap indexToDDNodeMap;
315 
316 };
317 
318 } // End namespace SVF
319 
320 #endif /* PATHALLOCATOR_H_ */
DdNode * AND(DdNode *lhs, DdNode *rhs)
Operations on conditions.
Definition: Conditions.cpp:40
bool isTestNullExpr(const Value *test, const Value *val) const
Return true if this is a test null expression.
virtual void allocateForBB(const BasicBlock &bb)
Allocate path condition for every basic block.
Condition * getBranchCond(const BasicBlock *bb, const BasicBlock *succ) const
Get branch condition.
llvm::BranchInst BranchInst
Definition: BasicTypes.h:157
virtual Condition * ComputeInterCallVFGGuard(const BasicBlock *src, const BasicBlock *dst, const BasicBlock *callBB)
void printDbg(Condition *cond)
Print debug information for this condition.
llvm::BasicBlock BasicBlock
Definition: BasicTypes.h:77
Definition: cudd.h:270
Condition * evaluateBranchCond(const BasicBlock *bb, const BasicBlock *succ, const Value *val)
Evaluate branch conditions.
Condition * evaluateTestNullLikeExpr(const BranchInst *brInst, const BasicBlock *succ, const Value *val)
Return branch condition after evaluating test null like expression.
NodeBS exactCondElem(Condition *cond)
Iterator every element of the bdd.
Condition * getEvalBrCond(const BasicBlock *bb, const BasicBlock *succ)
Get a condition, evaluate the value for conditions if necessary (e.g., testNull like express) ...
const Value * getCurEvalVal() const
Get current value for branch condition evaluation.
#define assert(ex)
Definition: util.h:141
Set< const BasicBlock * > BasicBlockSet
Condition * getTrueCond() const
bool setCFCond(const BasicBlock *bb, Condition *cond)
Get/Set control-flow conditions.
void setCurEvalVal(const Value *val)
Set current value for branch condition evaluation.
PostDominatorTree * getPostDT(const Function *fun)
Get Postdominators.
Condition * evaluateLoopExitBranch(const BasicBlock *bb, const BasicBlock *succ)
Evaluate loop exit branch.
static u32_t getMaxLiveCondNumber()
llvm::LoopInfo LoopInfo
Definition: BasicTypes.h:89
void printPathCond()
Print out the path condition information.
DominatorTree * getDT(const Function *fun)
Get dominators.
void BddSupport(DdNode *f, NodeBS &support) const
Definition: Conditions.cpp:134
IndexToConditionMap indexToDDNodeMap
Condition * condOr(Condition *lhs, Condition *rhs)
Map< const Function *, BasicBlockSet > FunToExitBBsMap
map a function to all its basic blocks calling program exit
DdNode * getTrueCond() const
Definition: Conditions.h:68
const Value * curEvalVal
current llvm value to evaluate branch condition when computing guards
std::unordered_map< Key, Value, Hash, KeyEqual, Allocator > Map
Definition: SVFBasicTypes.h:98
static BddCondManager * bddCondMgr
bbd manager
unsigned u32_t
Definition: SVFBasicTypes.h:75
Condition * newCond(const Instruction *inst)
Allocate a new condition.
FunToExitBBsMap funToExitBBsMap
map a function to all its basic blocks calling program exit
Condition * getCond(u32_t i) const
Given an index, get its condition.
PostDominatorTree * getPostDT(const Function *f)
Get post dominator tree of a function.
llvm::PostDominatorTree PostDominatorTree
Definition: BasicTypes.h:194
void markForRelease(Condition *cond)
Decrease reference counting for the bdd.
bool isTestContainsNullAndTheValue(const CmpInst *cmp, const Value *val) const
Return true if two values on the predicate are what we want.
BBCondMap bbConds
map basic block to its successors/predecessors branch conditions
static Condition * trueCond()
virtual ~PathCondAllocator()
Destructor.
CondToTermInstMap condToInstMap
map a condition to its corresponding llvm instruction
DdNode * Cudd_bdd(u32_t i)
Definition: Conditions.h:59
Condition * getFalseCond() const
u32_t getBDDMemUsage()
Definition: Conditions.h:77
void setBranchCond(const BasicBlock *bb, const BasicBlock *succ, Condition *cond)
Get/Set a branch condition, and its terminator instruction.
std::unordered_set< Key, Hash, KeyEqual, Allocator > Set
Definition: SVFBasicTypes.h:93
llvm::Function Function
Definition: BasicTypes.h:76
LoopInfo * getLoopInfo(const Function *f)
Get loop info of a function.
u32_t getCondNumber()
Definition: Conditions.h:81
llvm::Instruction Instruction
Definition: BasicTypes.h:79
bool isNECmp(const CmpInst *cmp) const
Return true if the predicate of this compare instruction is not equal.
Condition * evaluateProgExit(const BranchInst *brInst, const BasicBlock *succ)
Return condition when there is a branch calls program exit.
bool isEQCmp(const CmpInst *cmp) const
Evaluate test null/not null like expressions.
LoopInfo * getLoopInfo(const Function *f)
Get LoopInfo.
PTACFInfoBuilder cfInfoBuilder
map a function to its loop info
virtual Condition * ComputeInterRetVFGGuard(const BasicBlock *src, const BasicBlock *dst, const BasicBlock *retBB)
BBToCondMap bbToCondMap
map a basic block to its path condition starting from root
llvm::DominatorTree DominatorTree
Definition: BasicTypes.h:193
static BddCondManager * getBddCondManager()
Used internally, not supposed to be exposed to other classes.
static u32_t getMemUsage()
Statistics.
u32_t getMaxLiveCondNumber()
Definition: Conditions.h:85
DominatorTree * getDT(const Function *f)
Get dominator tree of a function.
Condition * condAnd(Condition *lhs, Condition *rhs)
Condition operations.
FIFOWorkList< const BasicBlock * > CFWorkList
worklist for control-flow guard computation
DdNode * getFalseCond() const
Definition: Conditions.h:72
void allocate(const SVFModule *module)
Perform path allocation.
bool isTestNotNullExpr(const Value *test, const Value *val) const
Return true if this is a test not null expression.
DdNode * OR(DdNode *lhs, DdNode *rhs)
Definition: Conditions.cpp:68
Map< u32_t, Condition * > CondPosMap
map a branch to its Condition
PathCondAllocator()
Constructor.
Condition * condNeg(Condition *cond)
void destroy()
Release memory.
std::string dumpStr(DdNode *lhs) const
Definition: Conditions.cpp:163
Condition * getCFCond(const BasicBlock *bb) const
DdNode * NEG(DdNode *lhs)
Definition: Conditions.cpp:93
for isBitcode
Definition: ContextDDA.h:15
Map< const BasicBlock *, Condition * > BBToCondMap
map a basic block to its condition during control-flow guard computation
llvm::SparseBitVector NodeBS
Definition: SVFBasicTypes.h:87
Condition * createNewCond(u32_t i)
Create new BDD condition.
bool isBBCallsProgExit(const BasicBlock *bb)
static Condition * falseCond()
Map< u32_t, Condition * > IndexToConditionMap
std::string dumpCond(Condition *cond) const
void collectBBCallingProgExit(const BasicBlock &bb)
Collect basic block contains program exit function call.
const Instruction * getCondInst(const Condition *cond) const
Get llvm conditional expression.
virtual Condition * ComputeIntraVFGGuard(const BasicBlock *src, const BasicBlock *dst)
Guard Computation for a value-flow (between two basic blocks)
virtual Condition * getPHIComplementCond(const BasicBlock *BB1, const BasicBlock *BB2, const BasicBlock *BB0)
Map< const BasicBlock *, CondPosMap > BBCondMap
llvm::Value Value
Definition: BasicTypes.h:78
Map< const Condition *, const Instruction *> CondToTermInstMap
void printDbg(DdNode *d)
Definition: Conditions.h:114
void markForRelease(DdNode *cond)
Definition: Conditions.h:89
llvm::CmpInst CmpInst
Definition: BasicTypes.h:156