SVF
Conditions.cpp
Go to the documentation of this file.
1 //===- Conditions.cpp -- Context/path conditions in the form of BDDs----------//
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 /*
25  * Conditions.cpp
26  *
27  * Created on: Sep 22, 2014
28  * Author: Yulei Sui
29  */
30 
31 #include "Util/Options.h"
32 #include "Util/Conditions.h"
33 #include "Util/SVFUtil.h"
34 
35 using namespace SVF;
36 
38 
41 {
42  if (lhs == getFalseCond() || rhs == getFalseCond())
43  return getFalseCond();
44  else if (lhs == getTrueCond())
45  return rhs;
46  else if (rhs == getTrueCond())
47  return lhs;
48  else
49  {
51  if(tmp==nullptr)
52  {
53  SVFUtil::writeWrnMsg("exceeds max bdd size \n");
55  return lhs;
56  }
57  else
58  {
59  Cudd_Ref(tmp);
60  return tmp;
61  }
62  }
63 }
64 
69 {
70  if (lhs == getTrueCond() || rhs == getTrueCond())
71  return getTrueCond();
72  else if (lhs == getFalseCond())
73  return rhs;
74  else if (rhs == getFalseCond())
75  return lhs;
76  else
77  {
79  if(tmp==nullptr)
80  {
81  SVFUtil::writeWrnMsg("exceeds max bdd size \n");
83  return getTrueCond();
84  }
85  else
86  {
87  Cudd_Ref(tmp);
88  return tmp;
89  }
90  }
91 }
92 
94 {
95  if (lhs == getTrueCond())
96  return getFalseCond();
97  else if (lhs == getFalseCond())
98  return getTrueCond();
99  else
100  return Cudd_Not(lhs);
101 }
103 
109 {
110  if (!Cudd_IsComplement(f->next))
111  return;
112  /* Clear visited flag. */
113  f->next = Cudd_Regular(f->next);
114  if (cuddIsConstant(f))
115  return;
116  ddClearFlag(cuddT(f));
118  return;
119 }
120 
122 {
123  if (cuddIsConstant(f) || Cudd_IsComplement(f->next))
124  return;
125 
126  support.set(f->index);
127 
128  BddSupportStep(cuddT(f), support);
129  BddSupportStep(Cudd_Regular(cuddE(f)), support);
130  /* Mark as visited. */
131  f->next = Cudd_Complement(f->next);
132 }
133 
134 void BddCondManager::BddSupport(DdNode * f, NodeBS &support) const
135 {
136  BddSupportStep( Cudd_Regular(f), support);
138 }
139 
144 {
145  if (lhs == getTrueCond())
146  O << "T";
147  else
148  {
149  NodeBS support;
150  BddSupport(lhs, support);
151  for (NodeBS::iterator iter = support.begin(); iter != support.end();
152  ++iter)
153  {
154  unsigned rid = *iter;
155  O << rid << " ";
156  }
157  }
158 }
159 
163 std::string BddCondManager::dumpStr(DdNode* lhs) const
164 {
165  std::string str;
166  if (lhs == getTrueCond())
167  str += "T";
168  else
169  {
170  NodeBS support;
171  BddSupport(lhs, support);
172  for (NodeBS::iterator iter = support.begin(); iter != support.end();
173  ++iter)
174  {
175  unsigned rid = *iter;
176  char int2str[16];
177  sprintf(int2str, "%d", rid);
178  str += int2str;
179  str += " ";
180  }
181  }
182  return str;
183 }
184 
DdNode * AND(DdNode *lhs, DdNode *rhs)
Operations on conditions.
Definition: Conditions.cpp:40
Definition: cudd.h:270
llvm::raw_ostream raw_ostream
LLVM outputs.
Definition: BasicTypes.h:99
#define Cudd_Not(node)
Definition: cudd.h:354
#define Cudd_Regular(node)
Definition: cudd.h:384
void BddSupport(DdNode *f, NodeBS &support) const
Definition: Conditions.cpp:134
void dump(DdNode *lhs, raw_ostream &O)
Definition: Conditions.cpp:143
DdNode * getTrueCond() const
Definition: Conditions.h:68
DdNode * Cudd_bddAndLimit(DdManager *dd, DdNode *f, DdNode *g, unsigned int limit)
Definition: cuddBddIte.c:382
DdNode * Cudd_bddOrLimit(DdManager *dd, DdNode *f, DdNode *g, unsigned int limit)
Definition: cuddBddIte.c:450
void writeWrnMsg(std::string msg)
Writes a message run through wrnMsg.
Definition: SVFUtil.cpp:67
void BddSupportStep(DdNode *f, NodeBS &support) const
Definition: Conditions.cpp:121
#define Cudd_IsComplement(node)
Definition: cudd.h:412
static const llvm::cl::opt< unsigned > MaxBddSize
Definition: Options.h:170
DdNode * next
Definition: cudd.h:273
#define cuddIsConstant(node)
Definition: cuddInt.h:593
DdManager * m_bdd_mgr
Definition: Conditions.h:128
#define Cudd_Complement(node)
Definition: cudd.h:398
#define cuddT(node)
Definition: cuddInt.h:609
DdNode * getFalseCond() const
Definition: Conditions.h:72
DdNode * OR(DdNode *lhs, DdNode *rhs)
Definition: Conditions.cpp:68
std::string dumpStr(DdNode *lhs) const
Definition: Conditions.cpp:163
DdHalfWord index
Definition: cudd.h:271
DdNode * NEG(DdNode *lhs)
Definition: Conditions.cpp:93
for isBitcode
Definition: ContextDDA.h:15
void ddClearFlag(DdNode *f) const
Definition: Conditions.cpp:108
llvm::SparseBitVector NodeBS
Definition: SVFBasicTypes.h:87
#define cuddE(node)
Definition: cuddInt.h:625
void Cudd_Ref(DdNode *n)
Definition: cuddRef.c:125