klee
ExprVisitor.cpp
Go to the documentation of this file.
1//===-- ExprVisitor.cpp ---------------------------------------------------===//
2//
3// The KLEE Symbolic Virtual Machine
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
11
12#include "klee/Expr/Expr.h"
13
14#include "llvm/Support/CommandLine.h"
15
16namespace {
17llvm::cl::opt<bool> UseVisitorHash(
18 "use-visitor-hash",
19 llvm::cl::desc(
20 "Use hash-consing during expression visitation (default=true)"),
21 llvm::cl::init(true), llvm::cl::cat(klee::ExprCat));
22}
23
24using namespace klee;
25
26ref<Expr> ExprVisitor::visit(const ref<Expr> &e) {
27 if (!UseVisitorHash || isa<ConstantExpr>(e)) {
28 return visitActual(e);
29 } else {
30 visited_ty::iterator it = visited.find(e);
31
32 if (it!=visited.end()) {
33 return it->second;
34 } else {
35 ref<Expr> res = visitActual(e);
36 visited.insert(std::make_pair(e, res));
37 return res;
38 }
39 }
40}
41
43 if (isa<ConstantExpr>(e)) {
44 return e;
45 } else {
46 Expr &ep = *e.get();
47
48 Action res = visitExpr(ep);
49 switch(res.kind) {
51 // continue with normal action
52 break;
54 return e;
56 return res.argument;
57 }
58
59 switch(ep.getKind()) {
60 case Expr::NotOptimized: res = visitNotOptimized(static_cast<NotOptimizedExpr&>(ep)); break;
61 case Expr::Read: res = visitRead(static_cast<ReadExpr&>(ep)); break;
62 case Expr::Select: res = visitSelect(static_cast<SelectExpr&>(ep)); break;
63 case Expr::Concat: res = visitConcat(static_cast<ConcatExpr&>(ep)); break;
64 case Expr::Extract: res = visitExtract(static_cast<ExtractExpr&>(ep)); break;
65 case Expr::ZExt: res = visitZExt(static_cast<ZExtExpr&>(ep)); break;
66 case Expr::SExt: res = visitSExt(static_cast<SExtExpr&>(ep)); break;
67 case Expr::Add: res = visitAdd(static_cast<AddExpr&>(ep)); break;
68 case Expr::Sub: res = visitSub(static_cast<SubExpr&>(ep)); break;
69 case Expr::Mul: res = visitMul(static_cast<MulExpr&>(ep)); break;
70 case Expr::UDiv: res = visitUDiv(static_cast<UDivExpr&>(ep)); break;
71 case Expr::SDiv: res = visitSDiv(static_cast<SDivExpr&>(ep)); break;
72 case Expr::URem: res = visitURem(static_cast<URemExpr&>(ep)); break;
73 case Expr::SRem: res = visitSRem(static_cast<SRemExpr&>(ep)); break;
74 case Expr::Not: res = visitNot(static_cast<NotExpr&>(ep)); break;
75 case Expr::And: res = visitAnd(static_cast<AndExpr&>(ep)); break;
76 case Expr::Or: res = visitOr(static_cast<OrExpr&>(ep)); break;
77 case Expr::Xor: res = visitXor(static_cast<XorExpr&>(ep)); break;
78 case Expr::Shl: res = visitShl(static_cast<ShlExpr&>(ep)); break;
79 case Expr::LShr: res = visitLShr(static_cast<LShrExpr&>(ep)); break;
80 case Expr::AShr: res = visitAShr(static_cast<AShrExpr&>(ep)); break;
81 case Expr::Eq: res = visitEq(static_cast<EqExpr&>(ep)); break;
82 case Expr::Ne: res = visitNe(static_cast<NeExpr&>(ep)); break;
83 case Expr::Ult: res = visitUlt(static_cast<UltExpr&>(ep)); break;
84 case Expr::Ule: res = visitUle(static_cast<UleExpr&>(ep)); break;
85 case Expr::Ugt: res = visitUgt(static_cast<UgtExpr&>(ep)); break;
86 case Expr::Uge: res = visitUge(static_cast<UgeExpr&>(ep)); break;
87 case Expr::Slt: res = visitSlt(static_cast<SltExpr&>(ep)); break;
88 case Expr::Sle: res = visitSle(static_cast<SleExpr&>(ep)); break;
89 case Expr::Sgt: res = visitSgt(static_cast<SgtExpr&>(ep)); break;
90 case Expr::Sge: res = visitSge(static_cast<SgeExpr&>(ep)); break;
91 case Expr::Constant:
92 default:
93 assert(0 && "invalid expression kind");
94 }
95
96 switch(res.kind) {
97 default:
98 assert(0 && "invalid kind");
99 case Action::DoChildren: {
100 bool rebuild = false;
101 ref<Expr> e(&ep), kids[8];
102 unsigned count = ep.getNumKids();
103 for (unsigned i=0; i<count; i++) {
104 ref<Expr> kid = ep.getKid(i);
105 kids[i] = visit(kid);
106 if (kids[i] != kid)
107 rebuild = true;
108 }
109 if (rebuild) {
110 e = ep.rebuild(kids);
111 if (recursive)
112 e = visit(e);
113 }
114 if (!isa<ConstantExpr>(e)) {
115 res = visitExprPost(*e.get());
116 if (res.kind==Action::ChangeTo)
117 e = res.argument;
118 }
119 return e;
120 }
122 return e;
123 case Action::ChangeTo:
124 return res.argument;
125 }
126 }
127}
128
130 return Action::doChildren();
131}
132
134 return Action::skipChildren();
135}
136
138 return Action::doChildren();
139}
140
142 return Action::doChildren();
143}
144
146 return Action::doChildren();
147}
148
150 return Action::doChildren();
151}
152
154 return Action::doChildren();
155}
156
158 return Action::doChildren();
159}
160
162 return Action::doChildren();
163}
164
166 return Action::doChildren();
167}
168
170 return Action::doChildren();
171}
172
174 return Action::doChildren();
175}
176
178 return Action::doChildren();
179}
180
182 return Action::doChildren();
183}
184
186 return Action::doChildren();
187}
188
190 return Action::doChildren();
191}
192
194 return Action::doChildren();
195}
196
198 return Action::doChildren();
199}
200
202 return Action::doChildren();
203}
204
206 return Action::doChildren();
207}
208
210 return Action::doChildren();
211}
212
214 return Action::doChildren();
215}
216
218 return Action::doChildren();
219}
220
222 return Action::doChildren();
223}
224
226 return Action::doChildren();
227}
228
230 return Action::doChildren();
231}
232
234 return Action::doChildren();
235}
236
238 return Action::doChildren();
239}
240
242 return Action::doChildren();
243}
244
246 return Action::doChildren();
247}
248
250 return Action::doChildren();
251}
252
254 return Action::doChildren();
255}
256
258 return Action::doChildren();
259}
260
static Action doChildren()
Definition: ExprVisitor.h:39
static Action skipChildren()
Definition: ExprVisitor.h:40
virtual Action visitUle(const UleExpr &)
virtual Action visitUge(const UgeExpr &)
virtual Action visitZExt(const ZExtExpr &)
ref< Expr > visit(const ref< Expr > &e)
Definition: ExprVisitor.cpp:26
virtual Action visitOr(const OrExpr &)
virtual Action visitSge(const SgeExpr &)
virtual Action visitNotOptimized(const NotOptimizedExpr &)
virtual Action visitSRem(const SRemExpr &)
virtual Action visitRead(const ReadExpr &)
virtual Action visitMul(const MulExpr &)
ref< Expr > visitActual(const ref< Expr > &e)
Definition: ExprVisitor.cpp:42
virtual Action visitSub(const SubExpr &)
virtual Action visitSExt(const SExtExpr &)
visited_ty visited
Definition: ExprVisitor.h:85
virtual Action visitURem(const URemExpr &)
virtual Action visitSlt(const SltExpr &)
virtual Action visitEq(const EqExpr &)
virtual Action visitSDiv(const SDivExpr &)
virtual Action visitAShr(const AShrExpr &)
virtual Action visitSgt(const SgtExpr &)
virtual Action visitAdd(const AddExpr &)
virtual Action visitXor(const XorExpr &)
virtual Action visitAnd(const AndExpr &)
virtual Action visitExprPost(const Expr &)
virtual Action visitExpr(const Expr &)
virtual Action visitExtract(const ExtractExpr &)
virtual Action visitLShr(const LShrExpr &)
virtual Action visitShl(const ShlExpr &)
virtual Action visitUlt(const UltExpr &)
virtual Action visitSle(const SleExpr &)
virtual Action visitConcat(const ConcatExpr &)
virtual Action visitNot(const NotExpr &)
virtual Action visitNe(const NeExpr &)
virtual Action visitUgt(const UgtExpr &)
virtual Action visitSelect(const SelectExpr &)
virtual Action visitUDiv(const UDivExpr &)
Class representing symbolic expressions.
Definition: Expr.h:91
virtual Kind getKind() const =0
virtual unsigned getNumKids() const =0
@ Xor
Definition: Expr.h:149
@ Not
Definition: Expr.h:133
@ Ule
Definition: Expr.h:158
@ LShr
Definition: Expr.h:151
@ Sge
Not used in canonical form.
Definition: Expr.h:164
@ Add
Definition: Expr.h:138
@ Select
Definition: Expr.h:124
@ Ne
Not used in canonical form.
Definition: Expr.h:156
@ And
Definition: Expr.h:147
@ ZExt
Definition: Expr.h:129
@ URem
Definition: Expr.h:143
@ SDiv
Definition: Expr.h:142
@ NotOptimized
Definition: Expr.h:120
@ Mul
Definition: Expr.h:140
@ AShr
Definition: Expr.h:152
@ Extract
Definition: Expr.h:126
@ Read
Definition: Expr.h:123
@ Constant
Definition: Expr.h:113
@ Ugt
Not used in canonical form.
Definition: Expr.h:159
@ Sub
Definition: Expr.h:139
@ Ult
Definition: Expr.h:157
@ Shl
Definition: Expr.h:150
@ SRem
Definition: Expr.h:144
@ Concat
Definition: Expr.h:125
@ Or
Definition: Expr.h:148
@ Sle
Definition: Expr.h:162
@ Sgt
Not used in canonical form.
Definition: Expr.h:163
@ Slt
Definition: Expr.h:161
@ Uge
Not used in canonical form.
Definition: Expr.h:160
@ SExt
Definition: Expr.h:130
@ UDiv
Definition: Expr.h:141
@ Eq
Definition: Expr.h:155
virtual ref< Expr > rebuild(ref< Expr > kids[]) const =0
virtual ref< Expr > getKid(unsigned i) const =0
Class representing a one byte read from an array.
Definition: Expr.h:565
Class representing an if-then-else expression.
Definition: Expr.h:610
T * get() const
Definition: Ref.h:129
Definition: main.cpp:291
llvm::cl::OptionCategory ExprCat