10 #include "llvm/Config/config.h" 24 friend class Z3Context;
29 Z3Config() : Config(Z3_mk_config()) {
31 Z3_set_param_value(Config,
"model",
"true");
33 Z3_set_param_value(Config,
"proof",
"false");
35 Z3_set_param_value(Config,
"timeout",
"15000");
38 ~Z3Config() { Z3_del_config(Config); }
42 void Z3ErrorHandler(Z3_context
Context, Z3_error_code
Error) {
53 Context = Z3_mk_context_rc(Z3Config().Config);
56 Z3_set_error_handler(Context, Z3ErrorHandler);
59 virtual ~Z3Context() {
60 Z3_del_context(Context);
67 friend class Z3Solver;
75 Z3Sort(Z3Context &
C, Z3_sort ZS) : Context(C), Sort(ZS) {
76 Z3_inc_ref(Context.Context, reinterpret_cast<Z3_ast>(Sort));
80 Z3Sort(
const Z3Sort &
Other) : Context(Other.Context), Sort(Other.Sort) {
81 Z3_inc_ref(Context.Context, reinterpret_cast<Z3_ast>(Sort));
86 Z3Sort &operator=(
const Z3Sort &
Other) {
87 Z3_inc_ref(Context.Context, reinterpret_cast<Z3_ast>(Other.Sort));
88 Z3_dec_ref(Context.Context, reinterpret_cast<Z3_ast>(Sort));
93 Z3Sort(Z3Sort &&
Other) =
delete;
94 Z3Sort &operator=(Z3Sort &&
Other) =
delete;
98 Z3_dec_ref(Context.Context, reinterpret_cast<Z3_ast>(Sort));
103 Z3_get_ast_id(Context.Context, reinterpret_cast<Z3_ast>(Sort)));
106 bool isBitvectorSortImpl()
const override {
107 return (Z3_get_sort_kind(Context.Context, Sort) == Z3_BV_SORT);
110 bool isFloatSortImpl()
const override {
111 return (Z3_get_sort_kind(Context.Context, Sort) == Z3_FLOATING_POINT_SORT);
114 bool isBooleanSortImpl()
const override {
115 return (Z3_get_sort_kind(Context.Context, Sort) == Z3_BOOL_SORT);
118 unsigned getBitvectorSortSizeImpl()
const override {
119 return Z3_get_bv_sort_size(Context.Context, Sort);
122 unsigned getFloatSortSizeImpl()
const override {
123 return Z3_fpa_get_ebits(Context.Context, Sort) +
124 Z3_fpa_get_sbits(Context.Context, Sort);
128 return Z3_is_eq_sort(Context.Context, Sort,
129 static_cast<const Z3Sort &>(Other).Sort);
133 OS << Z3_sort_to_string(Context.Context, Sort);
137 static const Z3Sort &toZ3Sort(
const SMTSort &S) {
138 return static_cast<const Z3Sort &
>(S);
141 class Z3Expr :
public SMTExpr {
142 friend class Z3Solver;
149 Z3Expr(Z3Context &
C, Z3_ast ZA) :
SMTExpr(), Context(C), AST(ZA) {
150 Z3_inc_ref(Context.Context, AST);
154 Z3Expr(
const Z3Expr &Copy) :
SMTExpr(), Context(Copy.Context), AST(Copy.AST) {
155 Z3_inc_ref(Context.Context, AST);
160 Z3Expr &operator=(
const Z3Expr &
Other) {
161 Z3_inc_ref(Context.Context, Other.AST);
162 Z3_dec_ref(Context.Context, AST);
167 Z3Expr(Z3Expr &&
Other) =
delete;
168 Z3Expr &operator=(Z3Expr &&
Other) =
delete;
172 Z3_dec_ref(Context.Context, AST);
176 ID.
AddInteger(Z3_get_ast_id(Context.Context, AST));
181 assert(Z3_is_eq_sort(Context.Context, Z3_get_sort(Context.Context, AST),
182 Z3_get_sort(Context.Context,
183 static_cast<const Z3Expr &>(Other).AST)) &&
184 "AST's must have the same sort");
185 return Z3_is_eq_ast(Context.Context, AST,
186 static_cast<const Z3Expr &>(Other).AST);
190 OS << Z3_ast_to_string(Context.Context, AST);
194 static const Z3Expr &toZ3Expr(
const SMTExpr &
E) {
195 return static_cast<const Z3Expr &
>(
E);
199 friend class Z3Solver;
206 Z3Model(Z3Context &
C, Z3_model ZM) : Context(C), Model(ZM) {
207 Z3_model_inc_ref(Context.Context, Model);
210 Z3Model(
const Z3Model &Other) =
delete;
211 Z3Model(Z3Model &&Other) =
delete;
212 Z3Model &operator=(Z3Model &Other) =
delete;
213 Z3Model &operator=(Z3Model &&Other) =
delete;
217 Z3_model_dec_ref(Context.Context, Model);
221 OS << Z3_model_to_string(Context.Context, Model);
258 friend class Z3ConstraintManager;
265 std::set<Z3Sort> CachedSorts;
268 std::set<Z3Expr> CachedExprs;
271 Z3Solver() : Solver(Z3_mk_simple_solver(Context.Context)) {
272 Z3_solver_inc_ref(Context.Context, Solver);
275 Z3Solver(
const Z3Solver &Other) =
delete;
276 Z3Solver(Z3Solver &&Other) =
delete;
277 Z3Solver &operator=(Z3Solver &Other) =
delete;
278 Z3Solver &operator=(Z3Solver &&Other) =
delete;
282 Z3_solver_dec_ref(Context.Context, Solver);
285 void addConstraint(
const SMTExprRef &Exp)
const override {
286 Z3_solver_assert(Context.Context, Solver, toZ3Expr(*Exp).AST);
292 auto It = CachedSorts.insert(toZ3Sort(Sort));
299 auto It = CachedExprs.insert(toZ3Expr(Exp));
304 return newSortRef(Z3Sort(Context, Z3_mk_bool_sort(Context.Context)));
307 SMTSortRef getBitvectorSort(
unsigned BitWidth)
override {
309 Z3Sort(Context, Z3_mk_bv_sort(Context.Context, BitWidth)));
314 Z3Sort(Context, Z3_get_sort(Context.Context, toZ3Expr(*Exp).AST)));
318 return newSortRef(Z3Sort(Context, Z3_mk_fpa_sort_16(Context.Context)));
322 return newSortRef(Z3Sort(Context, Z3_mk_fpa_sort_32(Context.Context)));
326 return newSortRef(Z3Sort(Context, Z3_mk_fpa_sort_64(Context.Context)));
330 return newSortRef(Z3Sort(Context, Z3_mk_fpa_sort_128(Context.Context)));
335 Z3Expr(Context, Z3_mk_bvneg(Context.Context, toZ3Expr(*Exp).AST)));
340 Z3Expr(Context, Z3_mk_bvnot(Context.Context, toZ3Expr(*Exp).AST)));
345 Z3Expr(Context, Z3_mk_not(Context.Context, toZ3Expr(*Exp).AST)));
350 Z3Expr(Context, Z3_mk_bvadd(Context.Context, toZ3Expr(*LHS).AST,
351 toZ3Expr(*RHS).AST)));
356 Z3Expr(Context, Z3_mk_bvsub(Context.Context, toZ3Expr(*LHS).AST,
357 toZ3Expr(*RHS).AST)));
362 Z3Expr(Context, Z3_mk_bvmul(Context.Context, toZ3Expr(*LHS).AST,
363 toZ3Expr(*RHS).AST)));
368 Z3Expr(Context, Z3_mk_bvsrem(Context.Context, toZ3Expr(*LHS).AST,
369 toZ3Expr(*RHS).AST)));
374 Z3Expr(Context, Z3_mk_bvurem(Context.Context, toZ3Expr(*LHS).AST,
375 toZ3Expr(*RHS).AST)));
380 Z3Expr(Context, Z3_mk_bvsdiv(Context.Context, toZ3Expr(*LHS).AST,
381 toZ3Expr(*RHS).AST)));
386 Z3Expr(Context, Z3_mk_bvudiv(Context.Context, toZ3Expr(*LHS).AST,
387 toZ3Expr(*RHS).AST)));
392 Z3Expr(Context, Z3_mk_bvshl(Context.Context, toZ3Expr(*LHS).AST,
393 toZ3Expr(*RHS).AST)));
398 Z3Expr(Context, Z3_mk_bvashr(Context.Context, toZ3Expr(*LHS).AST,
399 toZ3Expr(*RHS).AST)));
404 Z3Expr(Context, Z3_mk_bvlshr(Context.Context, toZ3Expr(*LHS).AST,
405 toZ3Expr(*RHS).AST)));
410 Z3Expr(Context, Z3_mk_bvxor(Context.Context, toZ3Expr(*LHS).AST,
411 toZ3Expr(*RHS).AST)));
416 Z3Expr(Context, Z3_mk_bvor(Context.Context, toZ3Expr(*LHS).AST,
417 toZ3Expr(*RHS).AST)));
422 Z3Expr(Context, Z3_mk_bvand(Context.Context, toZ3Expr(*LHS).AST,
423 toZ3Expr(*RHS).AST)));
428 Z3Expr(Context, Z3_mk_bvult(Context.Context, toZ3Expr(*LHS).AST,
429 toZ3Expr(*RHS).AST)));
434 Z3Expr(Context, Z3_mk_bvslt(Context.Context, toZ3Expr(*LHS).AST,
435 toZ3Expr(*RHS).AST)));
440 Z3Expr(Context, Z3_mk_bvugt(Context.Context, toZ3Expr(*LHS).AST,
441 toZ3Expr(*RHS).AST)));
446 Z3Expr(Context, Z3_mk_bvsgt(Context.Context, toZ3Expr(*LHS).AST,
447 toZ3Expr(*RHS).AST)));
452 Z3Expr(Context, Z3_mk_bvule(Context.Context, toZ3Expr(*LHS).AST,
453 toZ3Expr(*RHS).AST)));
458 Z3Expr(Context, Z3_mk_bvsle(Context.Context, toZ3Expr(*LHS).AST,
459 toZ3Expr(*RHS).AST)));
464 Z3Expr(Context, Z3_mk_bvuge(Context.Context, toZ3Expr(*LHS).AST,
465 toZ3Expr(*RHS).AST)));
470 Z3Expr(Context, Z3_mk_bvsge(Context.Context, toZ3Expr(*LHS).AST,
471 toZ3Expr(*RHS).AST)));
475 Z3_ast
Args[2] = {toZ3Expr(*LHS).AST, toZ3Expr(*RHS).AST};
476 return newExprRef(Z3Expr(Context, Z3_mk_and(Context.Context, 2, Args)));
480 Z3_ast Args[2] = {toZ3Expr(*LHS).AST, toZ3Expr(*RHS).AST};
481 return newExprRef(Z3Expr(Context, Z3_mk_or(Context.Context, 2, Args)));
486 Z3Expr(Context, Z3_mk_eq(Context.Context, toZ3Expr(*LHS).AST,
487 toZ3Expr(*RHS).AST)));
492 Z3Expr(Context, Z3_mk_fpa_neg(Context.Context, toZ3Expr(*Exp).AST)));
496 return newExprRef(Z3Expr(
497 Context, Z3_mk_fpa_is_infinite(Context.Context, toZ3Expr(*Exp).AST)));
502 Z3Expr(Context, Z3_mk_fpa_is_nan(Context.Context, toZ3Expr(*Exp).AST)));
506 return newExprRef(Z3Expr(
507 Context, Z3_mk_fpa_is_normal(Context.Context, toZ3Expr(*Exp).AST)));
511 return newExprRef(Z3Expr(
512 Context, Z3_mk_fpa_is_zero(Context.Context, toZ3Expr(*Exp).AST)));
519 Z3_mk_fpa_mul(Context.Context, toZ3Expr(*LHS).AST,
520 toZ3Expr(*RHS).AST, toZ3Expr(*RoundingMode).AST)));
524 SMTExprRef RoundingMode = getFloatRoundingMode();
527 Z3_mk_fpa_div(Context.Context, toZ3Expr(*LHS).AST,
528 toZ3Expr(*RHS).AST, toZ3Expr(*RoundingMode).AST)));
533 Z3Expr(Context, Z3_mk_fpa_rem(Context.Context, toZ3Expr(*LHS).AST,
534 toZ3Expr(*RHS).AST)));
538 SMTExprRef RoundingMode = getFloatRoundingMode();
541 Z3_mk_fpa_add(Context.Context, toZ3Expr(*LHS).AST,
542 toZ3Expr(*RHS).AST, toZ3Expr(*RoundingMode).AST)));
546 SMTExprRef RoundingMode = getFloatRoundingMode();
549 Z3_mk_fpa_sub(Context.Context, toZ3Expr(*LHS).AST,
550 toZ3Expr(*RHS).AST, toZ3Expr(*RoundingMode).AST)));
555 Z3Expr(Context, Z3_mk_fpa_lt(Context.Context, toZ3Expr(*LHS).AST,
556 toZ3Expr(*RHS).AST)));
561 Z3Expr(Context, Z3_mk_fpa_gt(Context.Context, toZ3Expr(*LHS).AST,
562 toZ3Expr(*RHS).AST)));
567 Z3Expr(Context, Z3_mk_fpa_leq(Context.Context, toZ3Expr(*LHS).AST,
568 toZ3Expr(*RHS).AST)));
573 Z3Expr(Context, Z3_mk_fpa_geq(Context.Context, toZ3Expr(*LHS).AST,
574 toZ3Expr(*RHS).AST)));
579 Z3Expr(Context, Z3_mk_fpa_eq(Context.Context, toZ3Expr(*LHS).AST,
580 toZ3Expr(*RHS).AST)));
586 Z3Expr(Context, Z3_mk_ite(Context.Context, toZ3Expr(*Cond).AST,
587 toZ3Expr(*T).AST, toZ3Expr(*F).AST)));
591 return newExprRef(Z3Expr(
592 Context, Z3_mk_sign_ext(Context.Context, i, toZ3Expr(*Exp).AST)));
596 return newExprRef(Z3Expr(
597 Context, Z3_mk_zero_ext(Context.Context, i, toZ3Expr(*Exp).AST)));
602 return newExprRef(Z3Expr(Context, Z3_mk_extract(Context.Context, High, Low,
603 toZ3Expr(*Exp).AST)));
609 bool isSigned)
override {
610 return newExprRef(Z3Expr(
611 Context, Z3_mk_bvadd_no_overflow(Context.Context, toZ3Expr(*LHS).AST,
612 toZ3Expr(*RHS).AST, isSigned)));
619 return newExprRef(Z3Expr(
620 Context, Z3_mk_bvadd_no_underflow(Context.Context, toZ3Expr(*LHS).AST,
621 toZ3Expr(*RHS).AST)));
628 return newExprRef(Z3Expr(
629 Context, Z3_mk_bvsub_no_overflow(Context.Context, toZ3Expr(*LHS).AST,
630 toZ3Expr(*RHS).AST)));
636 bool isSigned)
override {
637 return newExprRef(Z3Expr(
638 Context, Z3_mk_bvsub_no_underflow(Context.Context, toZ3Expr(*LHS).AST,
639 toZ3Expr(*RHS).AST, isSigned)));
646 return newExprRef(Z3Expr(
647 Context, Z3_mk_bvsdiv_no_overflow(Context.Context, toZ3Expr(*LHS).AST,
648 toZ3Expr(*RHS).AST)));
654 return newExprRef(Z3Expr(
655 Context, Z3_mk_bvneg_no_overflow(Context.Context, toZ3Expr(*Exp).AST)));
661 bool isSigned)
override {
662 return newExprRef(Z3Expr(
663 Context, Z3_mk_bvmul_no_overflow(Context.Context, toZ3Expr(*LHS).AST,
664 toZ3Expr(*RHS).AST, isSigned)));
671 return newExprRef(Z3Expr(
672 Context, Z3_mk_bvmul_no_underflow(Context.Context, toZ3Expr(*LHS).AST,
673 toZ3Expr(*RHS).AST)));
678 Z3Expr(Context, Z3_mk_concat(Context.Context, toZ3Expr(*LHS).AST,
679 toZ3Expr(*RHS).AST)));
683 SMTExprRef RoundingMode = getFloatRoundingMode();
684 return newExprRef(Z3Expr(
686 Z3_mk_fpa_to_fp_float(Context.Context, toZ3Expr(*RoundingMode).AST,
687 toZ3Expr(*From).AST, toZ3Sort(*To).Sort)));
691 SMTExprRef RoundingMode = getFloatRoundingMode();
692 return newExprRef(Z3Expr(
694 Z3_mk_fpa_to_fp_signed(Context.Context, toZ3Expr(*RoundingMode).AST,
695 toZ3Expr(*From).AST, toZ3Sort(*To).Sort)));
699 SMTExprRef RoundingMode = getFloatRoundingMode();
700 return newExprRef(Z3Expr(
702 Z3_mk_fpa_to_fp_unsigned(Context.Context, toZ3Expr(*RoundingMode).AST,
703 toZ3Expr(*From).AST, toZ3Sort(*To).Sort)));
707 SMTExprRef RoundingMode = getFloatRoundingMode();
708 return newExprRef(Z3Expr(
709 Context, Z3_mk_fpa_to_sbv(Context.Context, toZ3Expr(*RoundingMode).AST,
710 toZ3Expr(*From).AST, ToWidth)));
714 SMTExprRef RoundingMode = getFloatRoundingMode();
715 return newExprRef(Z3Expr(
716 Context, Z3_mk_fpa_to_ubv(Context.Context, toZ3Expr(*RoundingMode).AST,
717 toZ3Expr(*From).AST, ToWidth)));
721 return newExprRef(Z3Expr(Context, b ? Z3_mk_true(Context.Context)
722 : Z3_mk_false(Context.Context)));
726 const SMTSortRef Sort = getBitvectorSort(BitWidth);
728 Z3Expr(Context, Z3_mk_numeral(Context.Context, Int.
toString(10).c_str(),
729 toZ3Sort(*Sort).Sort)));
738 return newExprRef(Z3Expr(
739 Context, Z3_mk_fpa_to_fp_bv(Context.Context, toZ3Expr(*Z3Int).AST,
740 toZ3Sort(*Sort).Sort)));
745 Z3Expr(Context, Z3_mk_const(Context.Context,
746 Z3_mk_string_symbol(Context.Context, Name),
747 toZ3Sort(*Sort).Sort)));
751 bool isUnsigned)
override {
754 Z3_get_numeral_string(Context.Context, toZ3Expr(*Exp).AST),
759 bool getBoolean(
const SMTExprRef &Exp)
override {
760 return Z3_get_bool_value(Context.Context, toZ3Expr(*Exp).AST) == Z3_L_TRUE;
765 return newExprRef(Z3Expr(Context, Z3_mk_fpa_rne(Context.Context)));
776 if (!toAPSInt(BVSort, AST, Int,
true)) {
780 if (useSemantics && !areEquivalent(Float.
getSemantics(), Semantics)) {
781 assert(
false &&
"Floating-point types don't match!");
793 assert(
false &&
"Bitvector types don't match!");
809 assert(
false &&
"Bitwidth not supported!");
815 assert(
false &&
"Boolean type doesn't match!");
828 Z3Model
Model(Context, Z3_solver_get_model(Context.Context, Solver));
829 Z3_func_decl
Func = Z3_get_app_decl(
830 Context.Context, Z3_to_app(Context.Context, toZ3Expr(*Exp).AST));
831 if (Z3_model_has_interp(Context.Context, Model.Model, Func) != Z3_L_TRUE)
836 Z3_model_get_const_interp(Context.Context, Model.Model, Func)));
838 return toAPSInt(Sort, Assign, Int,
true);
842 Z3Model
Model(Context, Z3_solver_get_model(Context.Context, Solver));
843 Z3_func_decl Func = Z3_get_app_decl(
844 Context.Context, Z3_to_app(Context.Context, toZ3Expr(*Exp).AST));
845 if (Z3_model_has_interp(Context.Context, Model.Model, Func) != Z3_L_TRUE)
850 Z3_model_get_const_interp(Context.Context, Model.Model, Func)));
852 return toAPFloat(Sort, Assign, Float,
true);
856 Z3_lbool res = Z3_solver_check(Context.Context, Solver);
857 if (res == Z3_L_TRUE)
860 if (res == Z3_L_FALSE)
866 void push()
override {
return Z3_solver_push(Context.Context, Solver); }
868 void pop(
unsigned NumStates = 1)
override {
869 assert(Z3_solver_get_num_scopes(Context.Context, Solver) >= NumStates);
870 return Z3_solver_pop(Context.Context, Solver, NumStates);
873 bool isFPSupported()
override {
return true; }
876 void reset()
override { Z3_solver_reset(Context.Context, Solver); }
879 OS << Z3_solver_to_string(Context.Context, Solver);
889 return std::make_unique<Z3Solver>();
892 "with -DLLVM_ENABLE_Z3_SOLVER=ON",
static const fltSemantics & IEEEquad() LLVM_READNONE
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
virtual unsigned getFloatSortSize() const
Returns the floating-point size, fails if the sort is not a floating-point Calls getFloatSortSizeImpl...
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
This class represents lattice values for constants.
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds...
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
RoundingMode
Rounding mode used for floating point operations.
const fltSemantics & getSemantics() const
LLVM_DUMP_METHOD void dump() const
Generic base class for SMT exprs.
unsigned getBitWidth() const
Return the number of bits in the APInt.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
void AddInteger(signed I)
static ExponentType semanticsMaxExponent(const fltSemantics &)
Generic base class for SMT Solvers.
static unsigned int semanticsSizeInBits(const fltSemantics &)
static const fltSemantics & IEEEdouble() LLVM_READNONE
static ExponentType semanticsMinExponent(const fltSemantics &)
FoldingSetNodeID - This class is used to gather all the unique data bits of a node.
virtual bool isBooleanSort() const
Returns true if the sort is a boolean, calls isBooleanSortImpl().
void dump(const SparseBitVector< ElementSize > &LHS, raw_ostream &out)
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static void print(raw_ostream &Out, object::Archive::Kind Kind, T Val)
LLVM_DUMP_METHOD void dump() const
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
static const fltSemantics & IEEEsingle() LLVM_READNONE
BlockVerifier::State From
static const fltSemantics & IEEEhalf() LLVM_READNONE
static unsigned int semanticsPrecision(const fltSemantics &)
Class for arbitrary precision integers.
virtual bool isBitvectorSort() const
Returns true if the sort is a bitvector, calls isBitvectorSortImpl().
virtual bool isFloatSort() const
Returns true if the sort is a floating-point, calls isFloatSortImpl().
LLVM_DUMP_METHOD void dump() const
virtual unsigned getBitvectorSortSize() const
Returns the bitvector size, fails if the sort is not a bitvector Calls getBitvectorSortSizeImpl().
Generic base class for SMT sorts.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
SMTSolverRef CreateZ3Solver()
Convenience method to create and Z3Solver object.
void toString(SmallVectorImpl< char > &Str, unsigned Radix=10) const
toString - Append this APSInt to the specified SmallString.
Lightweight error class with error context and mandatory checking.
This class implements an extremely fast bulk output stream that can only output to a stream...
APInt bitcastToAPInt() const
std::shared_ptr< SMTSolver > SMTSolverRef
Shared pointer for SMTSolvers.
constexpr char Args[]
Key for Kernel::Metadata::mArgs.