1 /********************* */
2 /*! \file theory_bv.cpp
4 ** Top contributors (to current version):
5 ** Liana Hadarean, Tim King, Dejan Jovanovic
6 ** This file is part of the CVC4 project.
7 ** Copyright (c) 2009-2016 by the authors listed in the file AUTHORS
8 ** in the top-level source directory) and their institutional affiliations.
9 ** All rights reserved. See the file COPYING in the top-level source
10 ** directory for licensing information.\endverbatim
12 ** [[ Add lengthier description here ]]
13 ** \todo document this file
16 #include "theory/bv/theory_bv.h"
18 #include "options/bv_options.h"
19 #include "options/smt_options.h"
20 #include "smt/smt_statistics_registry.h"
21 #include "theory/bv/abstraction.h"
22 #include "theory/bv/bv_eager_solver.h"
23 #include "theory/bv/bv_subtheory_algebraic.h"
24 #include "theory/bv/bv_subtheory_bitblast.h"
25 #include "theory/bv/bv_subtheory_core.h"
26 #include "theory/bv/bv_subtheory_inequality.h"
27 #include "theory/bv/slicer.h"
28 #include "theory/bv/theory_bv_rewrite_rules_normalization.h"
29 #include "theory/bv/theory_bv_rewrite_rules_simplification.h"
30 #include "theory/bv/theory_bv_rewriter.h"
31 #include "theory/bv/theory_bv_utils.h"
32 #include "theory/theory_model.h"
33 #include "proof/theory_proof.h"
34 #include "proof/proof_manager.h"
35 #include "theory/valuation.h"
37 using namespace CVC4::context
;
38 using namespace CVC4::theory::bv::utils
;
45 TheoryBV::TheoryBV(context::Context
* c
, context::UserContext
* u
,
46 OutputChannel
& out
, Valuation valuation
,
47 const LogicInfo
& logicInfo
, std::string name
)
48 : Theory(THEORY_BV
, c
, u
, out
, valuation
, logicInfo
, name
),
50 d_alreadyPropagatedSet(c
),
54 d_statistics(getFullInstanceName()),
60 d_lemmasAdded(c
, false),
62 d_invalidateModelCache(c
, true),
63 d_literalsToPropagate(c
),
64 d_literalsToPropagateIndex(c
, 0),
67 d_abstractionModule(new AbstractionModule(getFullInstanceName())),
68 d_isCoreTheory(false),
69 d_calledPreregister(false),
70 d_needsLastCallCheck(false),
71 d_extf_range_infer(u
),
72 d_extf_collapse_infer(c
)
74 d_extt
= new ExtTheory( this );
75 d_extt
->addFunctionKind( kind::BITVECTOR_TO_NAT
);
76 d_extt
->addFunctionKind( kind::INT_TO_BITVECTOR
);
78 if (options::bitblastMode() == theory::bv::BITBLAST_MODE_EAGER
) {
79 d_eagerSolver
= new EagerBitblastSolver(this);
83 if (options::bitvectorEqualitySolver()) {
84 SubtheorySolver
* core_solver
= new CoreSolver(c
, this);
85 d_subtheories
.push_back(core_solver
);
86 d_subtheoryMap
[SUB_CORE
] = core_solver
;
89 if (options::bitvectorInequalitySolver()) {
90 SubtheorySolver
* ineq_solver
= new InequalitySolver(c
, this);
91 d_subtheories
.push_back(ineq_solver
);
92 d_subtheoryMap
[SUB_INEQUALITY
] = ineq_solver
;
95 if (options::bitvectorAlgebraicSolver()) {
96 SubtheorySolver
* alg_solver
= new AlgebraicSolver(c
, this);
97 d_subtheories
.push_back(alg_solver
);
98 d_subtheoryMap
[SUB_ALGEBRAIC
] = alg_solver
;
101 BitblastSolver
* bb_solver
= new BitblastSolver(c
, this);
102 if (options::bvAbstraction()) {
103 bb_solver
->setAbstraction(d_abstractionModule
);
105 d_subtheories
.push_back(bb_solver
);
106 d_subtheoryMap
[SUB_BITBLAST
] = bb_solver
;
109 TheoryBV::~TheoryBV() {
111 delete d_eagerSolver
;
113 for (unsigned i
= 0; i
< d_subtheories
.size(); ++i
) {
114 delete d_subtheories
[i
];
116 delete d_abstractionModule
;
120 void TheoryBV::setMasterEqualityEngine(eq::EqualityEngine
* eq
) {
121 if (options::bitblastMode() == theory::bv::BITBLAST_MODE_EAGER
) {
124 if (options::bitvectorEqualitySolver()) {
125 dynamic_cast<CoreSolver
*>(d_subtheoryMap
[SUB_CORE
])->setMasterEqualityEngine(eq
);
129 void TheoryBV::spendResource(unsigned ammount
) throw(UnsafeInterruptException
) {
130 getOutputChannel().spendResource(ammount
);
133 TheoryBV::Statistics::Statistics(const std::string
&name
):
134 d_avgConflictSize(name
+ "theory::bv::AvgBVConflictSize"),
135 d_solveSubstitutions(name
+ "theory::bv::NumberOfSolveSubstitutions", 0),
136 d_solveTimer(name
+ "theory::bv::solveTimer"),
137 d_numCallsToCheckFullEffort(name
+ "theory::bv::NumberOfFullCheckCalls", 0),
138 d_numCallsToCheckStandardEffort(name
+ "theory::bv::NumberOfStandardCheckCalls", 0),
139 d_weightComputationTimer(name
+ "theory::bv::weightComputationTimer"),
140 d_numMultSlice(name
+ "theory::bv::NumMultSliceApplied", 0)
142 smtStatisticsRegistry()->registerStat(&d_avgConflictSize
);
143 smtStatisticsRegistry()->registerStat(&d_solveSubstitutions
);
144 smtStatisticsRegistry()->registerStat(&d_solveTimer
);
145 smtStatisticsRegistry()->registerStat(&d_numCallsToCheckFullEffort
);
146 smtStatisticsRegistry()->registerStat(&d_numCallsToCheckStandardEffort
);
147 smtStatisticsRegistry()->registerStat(&d_weightComputationTimer
);
148 smtStatisticsRegistry()->registerStat(&d_numMultSlice
);
151 TheoryBV::Statistics::~Statistics() {
152 smtStatisticsRegistry()->unregisterStat(&d_avgConflictSize
);
153 smtStatisticsRegistry()->unregisterStat(&d_solveSubstitutions
);
154 smtStatisticsRegistry()->unregisterStat(&d_solveTimer
);
155 smtStatisticsRegistry()->unregisterStat(&d_numCallsToCheckFullEffort
);
156 smtStatisticsRegistry()->unregisterStat(&d_numCallsToCheckStandardEffort
);
157 smtStatisticsRegistry()->unregisterStat(&d_weightComputationTimer
);
158 smtStatisticsRegistry()->unregisterStat(&d_numMultSlice
);
161 Node
TheoryBV::getBVDivByZero(Kind k
, unsigned width
) {
162 NodeManager
* nm
= NodeManager::currentNM();
163 if (k
== kind::BITVECTOR_UDIV
) {
164 if (d_BVDivByZero
.find(width
) == d_BVDivByZero
.end()) {
165 // lazily create the function symbols
167 os
<< "BVUDivByZero_" << width
;
168 Node divByZero
= nm
->mkSkolem(os
.str(),
169 nm
->mkFunctionType(nm
->mkBitVectorType(width
), nm
->mkBitVectorType(width
)),
170 "partial bvudiv", NodeManager::SKOLEM_EXACT_NAME
);
171 d_BVDivByZero
[width
] = divByZero
;
173 return d_BVDivByZero
[width
];
175 else if (k
== kind::BITVECTOR_UREM
) {
176 if (d_BVRemByZero
.find(width
) == d_BVRemByZero
.end()) {
178 os
<< "BVURemByZero_" << width
;
179 Node divByZero
= nm
->mkSkolem(os
.str(),
180 nm
->mkFunctionType(nm
->mkBitVectorType(width
), nm
->mkBitVectorType(width
)),
181 "partial bvurem", NodeManager::SKOLEM_EXACT_NAME
);
182 d_BVRemByZero
[width
] = divByZero
;
184 return d_BVRemByZero
[width
];
191 void TheoryBV::collectFunctionSymbols(TNode term
, TNodeSet
& seen
) {
192 if (seen
.find(term
) != seen
.end())
194 if (term
.getKind() == kind::APPLY_UF
) {
195 TNode func
= term
.getOperator();
196 storeFunction(func
, term
);
197 } else if (term
.getKind() == kind::SELECT
) {
198 TNode func
= term
[0];
199 storeFunction(func
, term
);
200 } else if (term
.getKind() == kind::STORE
) {
201 AlwaysAssert(false, "Cannot use eager bitblasting on QF_ABV formula with stores");
203 for (unsigned i
= 0; i
< term
.getNumChildren(); ++i
) {
204 collectFunctionSymbols(term
[i
], seen
);
209 void TheoryBV::storeFunction(TNode func
, TNode term
) {
210 if (d_funcToArgs
.find(func
) == d_funcToArgs
.end()) {
211 d_funcToArgs
.insert(make_pair(func
, NodeSet()));
213 NodeSet
& set
= d_funcToArgs
[func
];
214 if (set
.find(term
) == set
.end()) {
216 Node skolem
= utils::mkVar(utils::getSize(term
));
217 d_funcToSkolem
.addSubstitution(term
, skolem
);
221 void TheoryBV::mkAckermanizationAsssertions(std::vector
<Node
>& assertions
) {
222 Debug("bv-ackermanize") << "TheoryBV::mkAckermanizationAsssertions\n";
224 Assert(options::bitblastMode() == theory::bv::BITBLAST_MODE_EAGER
);
225 AlwaysAssert(!options::incrementalSolving());
227 for (unsigned i
= 0; i
< assertions
.size(); ++i
) {
228 collectFunctionSymbols(assertions
[i
], seen
);
231 FunctionToArgs::const_iterator it
= d_funcToArgs
.begin();
232 NodeManager
* nm
= NodeManager::currentNM();
233 for (; it
!= d_funcToArgs
.end(); ++it
) {
234 TNode func
= it
->first
;
235 const NodeSet
& args
= it
->second
;
236 NodeSet::const_iterator it1
= args
.begin();
237 for ( ; it1
!= args
.end(); ++it1
) {
238 for(NodeSet::const_iterator it2
= it1
; it2
!= args
.end(); ++it2
) {
243 if (args1
.getKind() == kind::APPLY_UF
) {
244 AlwaysAssert (args1
.getKind() == kind::APPLY_UF
&&
245 args1
.getOperator() == func
);
246 AlwaysAssert (args2
.getKind() == kind::APPLY_UF
&&
247 args2
.getOperator() == func
);
248 AlwaysAssert (args1
.getNumChildren() == args2
.getNumChildren());
250 std::vector
<Node
> eqs(args1
.getNumChildren());
252 for (unsigned i
= 0; i
< args1
.getNumChildren(); ++i
) {
253 eqs
[i
] = nm
->mkNode(kind::EQUAL
, args1
[i
], args2
[i
]);
255 args_eq
= eqs
.size() == 1 ? eqs
[0] : nm
->mkNode(kind::AND
, eqs
);
257 AlwaysAssert (args1
.getKind() == kind::SELECT
&&
259 AlwaysAssert (args2
.getKind() == kind::SELECT
&&
261 AlwaysAssert (args1
.getNumChildren() == 2);
262 AlwaysAssert (args2
.getNumChildren() == 2);
263 args_eq
= nm
->mkNode(kind::EQUAL
, args1
[1], args2
[1]);
265 Node func_eq
= nm
->mkNode(kind::EQUAL
, args1
, args2
);
266 Node lemma
= nm
->mkNode(kind::IMPLIES
, args_eq
, func_eq
);
267 assertions
.push_back(lemma
);
272 // replace applications of UF by skolems (FIXME for model building)
273 for(unsigned i
= 0; i
< assertions
.size(); ++i
) {
274 assertions
[i
] = d_funcToSkolem
.apply(assertions
[i
]);
278 Node
TheoryBV::expandDefinition(LogicRequest
&logicRequest
, Node node
) {
279 Debug("bitvector-expandDefinition") << "TheoryBV::expandDefinition(" << node
<< ")" << std::endl
;
281 switch (node
.getKind()) {
282 case kind::BITVECTOR_SDIV
:
283 case kind::BITVECTOR_SREM
:
284 case kind::BITVECTOR_SMOD
:
285 return TheoryBVRewriter::eliminateBVSDiv(node
);
288 case kind::BITVECTOR_UDIV
:
289 case kind::BITVECTOR_UREM
: {
290 NodeManager
* nm
= NodeManager::currentNM();
291 unsigned width
= node
.getType().getBitVectorSize();
293 if (options::bitvectorDivByZeroConst()) {
294 Kind kind
= node
.getKind() == kind::BITVECTOR_UDIV
? kind::BITVECTOR_UDIV_TOTAL
: kind::BITVECTOR_UREM_TOTAL
;
295 return nm
->mkNode(kind
, node
[0], node
[1]);
298 TNode num
= node
[0], den
= node
[1];
299 Node den_eq_0
= nm
->mkNode(kind::EQUAL
, den
, nm
->mkConst(BitVector(width
, Integer(0))));
300 Node divTotalNumDen
= nm
->mkNode(node
.getKind() == kind::BITVECTOR_UDIV
? kind::BITVECTOR_UDIV_TOTAL
:
301 kind::BITVECTOR_UREM_TOTAL
, num
, den
);
303 // if (options::bitblastMode() == theory::bv::BITBLAST_MODE_EAGER) {
304 // // Ackermanize UF if using eager bit-blasting
305 // Node ackerman_var = nm->mkNode(node.getKind() == kind::BITVECTOR_UDIV ? kind::BITVECTOR_ACKERMANIZE_UDIV : kind::BITVECTOR_ACKERMANIZE_UREM, num);
306 // node = nm->mkNode(kind::ITE, den_eq_0, ackerman_var, divTotalNumDen);
309 Node divByZero
= getBVDivByZero(node
.getKind(), width
);
310 Node divByZeroNum
= nm
->mkNode(kind::APPLY_UF
, divByZero
, num
);
311 node
= nm
->mkNode(kind::ITE
, den_eq_0
, divByZeroNum
, divTotalNumDen
);
312 logicRequest
.widenLogic(THEORY_UF
);
327 void TheoryBV::preRegisterTerm(TNode node
) {
328 d_calledPreregister
= true;
329 Debug("bitvector-preregister") << "TheoryBV::preRegister(" << node
<< ")" << std::endl
;
331 if (options::bitblastMode() == theory::bv::BITBLAST_MODE_EAGER
) {
332 // the aig bit-blaster option is set heuristically
333 // if bv abstraction is not used
334 if (!d_eagerSolver
->isInitialized()) {
335 d_eagerSolver
->initialize();
338 if (node
.getKind() == kind::BITVECTOR_EAGER_ATOM
) {
339 Node formula
= node
[0];
340 d_eagerSolver
->assertFormula(formula
);
342 // nothing to do for the other terms
346 for (unsigned i
= 0; i
< d_subtheories
.size(); ++i
) {
347 d_subtheories
[i
]->preRegister(node
);
350 // AJR : equality solver currently registers all terms to ExtTheory, if we want a lazy reduction without the bv equality solver, need to call this
351 //getExtTheory()->registerTermRec( node );
354 void TheoryBV::sendConflict() {
356 if (d_conflictNode
.isNull()) {
359 Debug("bitvector") << indent() << "TheoryBV::check(): conflict " << d_conflictNode
<< std::endl
;
360 d_out
->conflict(d_conflictNode
);
361 d_statistics
.d_avgConflictSize
.addEntry(d_conflictNode
.getNumChildren());
362 d_conflictNode
= Node::null();
366 void TheoryBV::checkForLemma(TNode fact
) {
367 if (fact
.getKind() == kind::EQUAL
) {
368 if (fact
[0].getKind() == kind::BITVECTOR_UREM_TOTAL
) {
369 TNode urem
= fact
[0];
370 TNode result
= fact
[1];
371 TNode divisor
= urem
[1];
372 Node result_ult_div
= mkNode(kind::BITVECTOR_ULT
, result
, divisor
);
373 Node divisor_eq_0
= mkNode(kind::EQUAL
,
375 mkConst(BitVector(getSize(divisor
), 0u)));
376 Node split
= utils::mkNode(kind::OR
, divisor_eq_0
, mkNode(kind::NOT
, fact
), result_ult_div
);
379 if (fact
[1].getKind() == kind::BITVECTOR_UREM_TOTAL
) {
380 TNode urem
= fact
[1];
381 TNode result
= fact
[0];
382 TNode divisor
= urem
[1];
383 Node result_ult_div
= mkNode(kind::BITVECTOR_ULT
, result
, divisor
);
384 Node divisor_eq_0
= mkNode(kind::EQUAL
,
386 mkConst(BitVector(getSize(divisor
), 0u)));
387 Node split
= utils::mkNode(kind::OR
, divisor_eq_0
, mkNode(kind::NOT
, fact
), result_ult_div
);
393 void TheoryBV::check(Effort e
)
395 if (done() && e
<Theory::EFFORT_FULL
) {
399 //last call : do reductions on extended bitvector functions
400 if( e
==Theory::EFFORT_LAST_CALL
){
401 std::vector
< Node
> nred
;
402 getExtTheory()->getActive( nred
);
403 doExtfReductions( nred
);
407 TimerStat::CodeTimer
checkTimer(d_checkTime
);
408 Debug("bitvector") << "TheoryBV::check(" << e
<< ")" << std::endl
;
409 TimerStat::CodeTimer
codeTimer(d_statistics
.d_solveTimer
);
410 // we may be getting new assertions so the model cache may not be sound
411 d_invalidateModelCache
.set(true);
412 // if we are using the eager solver
413 if (options::bitblastMode() == theory::bv::BITBLAST_MODE_EAGER
) {
414 // this can only happen on an empty benchmark
415 if (!d_eagerSolver
->isInitialized()) {
416 d_eagerSolver
->initialize();
418 if (!Theory::fullEffort(e
))
421 std::vector
<TNode
> assertions
;
423 TNode fact
= get().assertion
;
424 Assert (fact
.getKind() == kind::BITVECTOR_EAGER_ATOM
);
425 assertions
.push_back(fact
);
427 Assert (d_eagerSolver
->hasAssertions(assertions
));
429 bool ok
= d_eagerSolver
->checkSat();
431 if (assertions
.size() == 1) {
432 d_out
->conflict(assertions
[0]);
435 Node conflict
= NodeManager::currentNM()->mkNode(kind::AND
, assertions
);
436 d_out
->conflict(conflict
);
443 if (Theory::fullEffort(e
)) {
444 ++(d_statistics
.d_numCallsToCheckFullEffort
);
446 ++(d_statistics
.d_numCallsToCheckStandardEffort
);
448 // if we are already in conflict just return the conflict
455 TNode fact
= get().assertion
;
459 for (unsigned i
= 0; i
< d_subtheories
.size(); ++i
) {
460 d_subtheories
[i
]->assertFact(fact
);
465 bool complete
= false;
466 for (unsigned i
= 0; i
< d_subtheories
.size(); ++i
) {
467 Assert (!inConflict());
468 ok
= d_subtheories
[i
]->check(e
);
469 complete
= d_subtheories
[i
]->isComplete();
472 // if we are in a conflict no need to check with other theories
473 Assert (inConflict());
478 // if the last subtheory was complete we stop
483 //check extended functions
484 if (Theory::fullEffort(e
)) {
485 //do inferences (adds external lemmas) TODO: this can be improved to add internal inferences
486 std::vector
< Node
> nred
;
487 if( getExtTheory()->doInferences( 0, nred
) ){
490 d_needsLastCallCheck
= false;
492 //other inferences involving bv2nat, int2bv
493 if( options::bvAlgExtf() ){
494 if( doExtfInferences( nred
) ){
498 if( !options::bvLazyReduceExtf() ){
499 if( doExtfReductions( nred
) ){
503 d_needsLastCallCheck
= true;
509 bool TheoryBV::doExtfInferences( std::vector
< Node
>& terms
) {
510 bool sentLemma
= false;
511 eq::EqualityEngine
* ee
= getEqualityEngine();
512 std::map
< Node
, Node
> op_map
;
513 for( unsigned j
=0; j
<terms
.size(); j
++ ){
515 Assert( n
.getKind()==kind::BITVECTOR_TO_NAT
|| kind::INT_TO_BITVECTOR
);
516 if( n
.getKind()==kind::BITVECTOR_TO_NAT
){
518 if( d_extf_range_infer
.find( n
)==d_extf_range_infer
.end() ){
519 d_extf_range_infer
.insert( n
);
520 unsigned bvs
= n
[0].getType().getBitVectorSize();
521 Node min
= NodeManager::currentNM()->mkConst( Rational( 0 ) );
522 Node max
= NodeManager::currentNM()->mkConst( Rational( Integer(1).multiplyByPow2(bvs
) ) );
523 Node lem
= NodeManager::currentNM()->mkNode( kind::AND
, NodeManager::currentNM()->mkNode( kind::GEQ
, n
, min
), NodeManager::currentNM()->mkNode( kind::LT
, n
, max
) );
524 Trace("bv-extf-lemma") << "BV extf lemma (range) : " << lem
<< std::endl
;
529 Node r
= ( ee
&& ee
->hasTerm( n
[0] ) ) ? ee
->getRepresentative( n
[0] ) : n
[0];
532 for( unsigned j
=0; j
<terms
.size(); j
++ ){
534 Node r
= ( ee
&& ee
->hasTerm( n
[0] ) ) ? ee
->getRepresentative( n
) : n
;
535 std::map
< Node
, Node
>::iterator it
= op_map
.find( r
);
536 if( it
!=op_map
.end() ){
537 Node parent
= it
->second
;
538 Node cterm
= parent
[0]==n
? parent
: NodeManager::currentNM()->mkNode( parent
.getOperator(), n
);
539 Trace("bv-extf-lemma-debug") << "BV extf collapse based on : " << n
<< std::endl
;
540 if( d_extf_collapse_infer
.find( cterm
)==d_extf_collapse_infer
.end() ){
541 d_extf_collapse_infer
.insert( cterm
);
544 if( n
.getKind()==kind::INT_TO_BITVECTOR
){
545 Assert( t
.getType().isInteger() );
546 //congruent modulo 2^( bv width )
547 unsigned bvs
= n
.getType().getBitVectorSize();
548 Node coeff
= NodeManager::currentNM()->mkConst( Rational( Integer(1).multiplyByPow2(bvs
) ) );
549 Node k
= NodeManager::currentNM()->mkSkolem( "int_bv_cong", t
.getType(), "for int2bv/bv2nat congruence" );
550 t
= NodeManager::currentNM()->mkNode( kind::PLUS
, t
, NodeManager::currentNM()->mkNode( kind::MULT
, coeff
, k
) );
552 Node lem
= parent
.eqNode( t
);
555 Assert( ee
->areEqual( parent
[0], n
) );
556 lem
= NodeManager::currentNM()->mkNode( kind::IMPLIES
, parent
[0].eqNode( n
), lem
);
558 Trace("bv-extf-lemma") << "BV extf lemma (collapse) : " << lem
<< std::endl
;
567 bool TheoryBV::doExtfReductions( std::vector
< Node
>& terms
) {
568 std::vector
< Node
> nredr
;
569 if( getExtTheory()->doReductions( 0, terms
, nredr
) ){
572 Assert( nredr
.empty() );
576 bool TheoryBV::needsCheckLastEffort() {
577 return d_needsLastCallCheck
;
580 void TheoryBV::collectModelInfo( TheoryModel
* m
, bool fullModel
){
581 Assert(!inConflict());
582 if (options::bitblastMode() == theory::bv::BITBLAST_MODE_EAGER
) {
583 d_eagerSolver
->collectModelInfo(m
, fullModel
);
585 for (unsigned i
= 0; i
< d_subtheories
.size(); ++i
) {
586 if (d_subtheories
[i
]->isComplete()) {
587 d_subtheories
[i
]->collectModelInfo(m
, fullModel
);
593 Node
TheoryBV::getModelValue(TNode var
) {
594 Assert(!inConflict());
595 for (unsigned i
= 0; i
< d_subtheories
.size(); ++i
) {
596 if (d_subtheories
[i
]->isComplete()) {
597 return d_subtheories
[i
]->getModelValue(var
);
603 void TheoryBV::propagate(Effort e
) {
604 Debug("bitvector") << indent() << "TheoryBV::propagate()" << std::endl
;
605 if (options::bitblastMode() == theory::bv::BITBLAST_MODE_EAGER
) {
613 // go through stored propagations
615 for (; d_literalsToPropagateIndex
< d_literalsToPropagate
.size() && ok
; d_literalsToPropagateIndex
= d_literalsToPropagateIndex
+ 1) {
616 TNode literal
= d_literalsToPropagate
[d_literalsToPropagateIndex
];
617 // temporary fix for incremental bit-blasting
618 if (d_valuation
.isSatLiteral(literal
)) {
619 Debug("bitvector::propagate") << "TheoryBV:: propagating " << literal
<<"\n";
620 ok
= d_out
->propagate(literal
);
625 Debug("bitvector::propagate") << indent() << "TheoryBV::propagate(): conflict from theory engine" << std::endl
;
631 eq::EqualityEngine
* TheoryBV::getEqualityEngine() {
632 CoreSolver
* core
= (CoreSolver
*)d_subtheoryMap
[SUB_CORE
];
634 return core
->getEqualityEngine();
640 bool TheoryBV::getCurrentSubstitution( int effort
, std::vector
< Node
>& vars
, std::vector
< Node
>& subs
, std::map
< Node
, std::vector
< Node
> >& exp
) {
641 eq::EqualityEngine
* ee
= getEqualityEngine();
643 //get the constant equivalence classes
645 for( unsigned i
=0; i
<vars
.size(); i
++ ){
647 if( ee
->hasTerm( n
) ){
648 Node nr
= ee
->getRepresentative( n
);
650 subs
.push_back( nr
);
651 exp
[n
].push_back( n
.eqNode( nr
) );
660 //return true if the substitution is non-trivial
666 int TheoryBV::getReduction( int effort
, Node n
, Node
& nr
) {
667 Trace("bv-ext") << "TheoryBV::checkExt : non-reduced : " << n
<< std::endl
;
668 if( n
.getKind()==kind::BITVECTOR_TO_NAT
){
669 //taken from rewrite code
670 const unsigned size
= utils::getSize(n
[0]);
671 NodeManager
* const nm
= NodeManager::currentNM();
672 const Node z
= nm
->mkConst(Rational(0));
673 const Node bvone
= nm
->mkConst(BitVector(1u, 1u));
674 NodeBuilder
<> result(kind::PLUS
);
676 for(unsigned bit
= 0; bit
< size
; ++bit
, i
*= 2) {
677 Node cond
= nm
->mkNode(kind::EQUAL
, nm
->mkNode(nm
->mkConst(BitVectorExtract(bit
, bit
)), n
[0]), bvone
);
678 result
<< nm
->mkNode(kind::ITE
, cond
, nm
->mkConst(Rational(i
)), z
);
682 }else if( n
.getKind()==kind::INT_TO_BITVECTOR
){
683 //taken from rewrite code
684 const unsigned size
= n
.getOperator().getConst
<IntToBitVector
>().size
;
685 NodeManager
* const nm
= NodeManager::currentNM();
686 const Node bvzero
= nm
->mkConst(BitVector(1u, 0u));
687 const Node bvone
= nm
->mkConst(BitVector(1u, 1u));
690 while(v
.size() < size
) {
691 Node cond
= nm
->mkNode(kind::GEQ
, nm
->mkNode(kind::INTS_MODULUS_TOTAL
, n
[0], nm
->mkConst(Rational(i
))), nm
->mkConst(Rational(i
, 2)));
692 cond
= Rewriter::rewrite( cond
);
693 v
.push_back(nm
->mkNode(kind::ITE
, cond
, bvone
, bvzero
));
696 NodeBuilder
<> result(kind::BITVECTOR_CONCAT
);
697 result
.append(v
.rbegin(), v
.rend());
704 Theory::PPAssertStatus
TheoryBV::ppAssert(TNode in
, SubstitutionMap
& outSubstitutions
) {
705 switch(in
.getKind()) {
708 if (in
[0].isVar() && !in
[1].hasSubterm(in
[0])) {
709 ++(d_statistics
.d_solveSubstitutions
);
710 outSubstitutions
.addSubstitution(in
[0], in
[1]);
711 return PP_ASSERT_STATUS_SOLVED
;
713 if (in
[1].isVar() && !in
[0].hasSubterm(in
[1])) {
714 ++(d_statistics
.d_solveSubstitutions
);
715 outSubstitutions
.addSubstitution(in
[1], in
[0]);
716 return PP_ASSERT_STATUS_SOLVED
;
718 Node node
= Rewriter::rewrite(in
);
719 if ((node
[0].getKind() == kind::BITVECTOR_EXTRACT
&& node
[1].isConst()) ||
720 (node
[1].getKind() == kind::BITVECTOR_EXTRACT
&& node
[0].isConst())) {
721 Node extract
= node
[0].isConst() ? node
[1] : node
[0];
722 if (extract
[0].getKind() == kind::VARIABLE
) {
723 Node c
= node
[0].isConst() ? node
[0] : node
[1];
725 unsigned high
= utils::getExtractHigh(extract
);
726 unsigned low
= utils::getExtractLow(extract
);
727 unsigned var_bitwidth
= utils::getSize(extract
[0]);
728 std::vector
<Node
> children
;
731 Assert (high
!= var_bitwidth
- 1);
732 unsigned skolem_size
= var_bitwidth
- high
- 1;
733 Node skolem
= utils::mkVar(skolem_size
);
734 children
.push_back(skolem
);
735 children
.push_back(c
);
736 } else if (high
== var_bitwidth
- 1) {
737 unsigned skolem_size
= low
;
738 Node skolem
= utils::mkVar(skolem_size
);
739 children
.push_back(c
);
740 children
.push_back(skolem
);
742 unsigned skolem1_size
= low
;
743 unsigned skolem2_size
= var_bitwidth
- high
- 1;
744 Node skolem1
= utils::mkVar(skolem1_size
);
745 Node skolem2
= utils::mkVar(skolem2_size
);
746 children
.push_back(skolem2
);
747 children
.push_back(c
);
748 children
.push_back(skolem1
);
750 Node concat
= utils::mkNode(kind::BITVECTOR_CONCAT
, children
);
751 Assert (utils::getSize(concat
) == utils::getSize(extract
[0]));
752 outSubstitutions
.addSubstitution(extract
[0], concat
);
753 return PP_ASSERT_STATUS_SOLVED
;
758 case kind::BITVECTOR_ULT
:
759 case kind::BITVECTOR_SLT
:
760 case kind::BITVECTOR_ULE
:
761 case kind::BITVECTOR_SLE
:
764 // TODO other predicates
767 return PP_ASSERT_STATUS_UNSOLVED
;
770 Node
TheoryBV::ppRewrite(TNode t
)
772 Debug("bv-pp-rewrite") << "TheoryBV::ppRewrite " << t
<< "\n";
774 if (RewriteRule
<BitwiseEq
>::applies(t
)) {
775 Node result
= RewriteRule
<BitwiseEq
>::run
<false>(t
);
776 res
= Rewriter::rewrite(result
);
777 } else if (d_isCoreTheory
&& t
.getKind() == kind::EQUAL
) {
778 std::vector
<Node
> equalities
;
779 Slicer::splitEqualities(t
, equalities
);
780 res
= utils::mkAnd(equalities
);
781 } else if (RewriteRule
<UltPlusOne
>::applies(t
)) {
782 Node result
= RewriteRule
<UltPlusOne
>::run
<false>(t
);
783 res
= Rewriter::rewrite(result
);
784 } else if( res
.getKind() == kind::EQUAL
&&
785 ((res
[0].getKind() == kind::BITVECTOR_PLUS
&&
786 RewriteRule
<ConcatToMult
>::applies(res
[1])) ||
787 (res
[1].getKind() == kind::BITVECTOR_PLUS
&&
788 RewriteRule
<ConcatToMult
>::applies(res
[0])))) {
789 Node mult
= RewriteRule
<ConcatToMult
>::applies(res
[0])?
790 RewriteRule
<ConcatToMult
>::run
<false>(res
[0]) :
791 RewriteRule
<ConcatToMult
>::run
<true>(res
[1]);
792 Node factor
= mult
[0];
793 Node sum
= RewriteRule
<ConcatToMult
>::applies(res
[0])? res
[1] : res
[0];
794 Node new_eq
=utils::mkNode(kind::EQUAL
, sum
, mult
);
795 Node rewr_eq
= RewriteRule
<SolveEq
>::run
<true>(new_eq
);
796 if (rewr_eq
[0].isVar() || rewr_eq
[1].isVar()){
797 res
= Rewriter::rewrite(rewr_eq
);
804 // if(t.getKind() == kind::EQUAL &&
805 // ((t[0].getKind() == kind::BITVECTOR_MULT && t[1].getKind() == kind::BITVECTOR_PLUS) ||
806 // (t[1].getKind() == kind::BITVECTOR_MULT && t[0].getKind() == kind::BITVECTOR_PLUS))) {
807 // // if we have an equality between a multiplication and addition
808 // // try to express multiplication in terms of addition
809 // Node mult = t[0].getKind() == kind::BITVECTOR_MULT? t[0] : t[1];
810 // Node add = t[0].getKind() == kind::BITVECTOR_PLUS? t[0] : t[1];
811 // if (RewriteRule<MultSlice>::applies(mult)) {
812 // Node new_mult = RewriteRule<MultSlice>::run<false>(mult);
813 // Node new_eq = Rewriter::rewrite(utils::mkNode(kind::EQUAL, new_mult, add));
815 // // the simplification can cause the formula to blow up
816 // // only apply if formula reduced
817 // if (d_subtheoryMap.find(SUB_BITBLAST) != d_subtheoryMap.end()) {
818 // BitblastSolver* bv = (BitblastSolver*)d_subtheoryMap[SUB_BITBLAST];
819 // uint64_t old_size = bv->computeAtomWeight(t);
820 // Assert (old_size);
821 // uint64_t new_size = bv->computeAtomWeight(new_eq);
822 // double ratio = ((double)new_size)/old_size;
823 // if (ratio <= 0.4) {
824 // ++(d_statistics.d_numMultSlice);
829 // if (new_eq.getKind() == kind::CONST_BOOLEAN) {
830 // ++(d_statistics.d_numMultSlice);
836 if (options::bvAbstraction() && t
.getType().isBoolean()) {
837 d_abstractionModule
->addInputAtom(res
);
839 Debug("bv-pp-rewrite") << "to " << res
<< "\n";
843 void TheoryBV::presolve() {
844 Debug("bitvector") << "TheoryBV::presolve" << endl
;
847 static int prop_count
= 0;
849 bool TheoryBV::storePropagation(TNode literal
, SubTheory subtheory
)
851 Debug("bitvector::propagate") << indent() << getSatContext()->getLevel() << " " << "TheoryBV::storePropagation(" << literal
<< ", " << subtheory
<< ")" << std::endl
;
854 // If already in conflict, no more propagation
856 Debug("bitvector::propagate") << indent() << "TheoryBV::storePropagation(" << literal
<< ", " << subtheory
<< "): already in conflict" << std::endl
;
860 // If propagated already, just skip
861 PropagatedMap::const_iterator find
= d_propagatedBy
.find(literal
);
862 if (find
!= d_propagatedBy
.end()) {
865 bool polarity
= literal
.getKind() != kind::NOT
;
866 Node negatedLiteral
= polarity
? literal
.notNode() : (Node
) literal
[0];
867 find
= d_propagatedBy
.find(negatedLiteral
);
868 if (find
!= d_propagatedBy
.end() && (*find
).second
!= subtheory
) {
869 // Safe to ignore this one, subtheory should produce a conflict
873 d_propagatedBy
[literal
] = subtheory
;
876 // Propagate differs depending on the subtheory
877 // * bitblaster needs to be left alone until it's done, otherwise it doesn't know how to explain
878 // * equality engine can propagate eagerly
880 if (subtheory
== SUB_CORE
) {
881 d_out
->propagate(literal
);
886 d_literalsToPropagate
.push_back(literal
);
890 }/* TheoryBV::propagate(TNode) */
893 void TheoryBV::explain(TNode literal
, std::vector
<TNode
>& assumptions
) {
894 Assert (wasPropagatedBySubtheory(literal
));
895 SubTheory sub
= getPropagatingSubtheory(literal
);
896 d_subtheoryMap
[sub
]->explain(literal
, assumptions
);
900 Node
TheoryBV::explain(TNode node
) {
901 Debug("bitvector::explain") << "TheoryBV::explain(" << node
<< ")" << std::endl
;
902 std::vector
<TNode
> assumptions
;
904 // Ask for the explanation
905 explain(node
, assumptions
);
906 // this means that it is something true at level 0
907 if (assumptions
.size() == 0) {
908 return utils::mkTrue();
910 // return the explanation
911 Node explanation
= utils::mkAnd(assumptions
);
912 Debug("bitvector::explain") << "TheoryBV::explain(" << node
<< ") => " << explanation
<< std::endl
;
913 Debug("bitvector::explain") << "TheoryBV::explain done. \n";
918 void TheoryBV::addSharedTerm(TNode t
) {
919 Debug("bitvector::sharing") << indent() << "TheoryBV::addSharedTerm(" << t
<< ")" << std::endl
;
920 d_sharedTermsSet
.insert(t
);
921 if (options::bitvectorEqualitySolver()) {
922 for (unsigned i
= 0; i
< d_subtheories
.size(); ++i
) {
923 d_subtheories
[i
]->addSharedTerm(t
);
929 EqualityStatus
TheoryBV::getEqualityStatus(TNode a
, TNode b
)
931 if (options::bitblastMode() == theory::bv::BITBLAST_MODE_EAGER
)
932 return EQUALITY_UNKNOWN
;
933 Assert (options::bitblastMode() == theory::bv::BITBLAST_MODE_LAZY
);
934 for (unsigned i
= 0; i
< d_subtheories
.size(); ++i
) {
935 EqualityStatus status
= d_subtheories
[i
]->getEqualityStatus(a
, b
);
936 if (status
!= EQUALITY_UNKNOWN
) {
940 return EQUALITY_UNKNOWN
; ;
944 void TheoryBV::enableCoreTheorySlicer() {
945 Assert (!d_calledPreregister
);
946 d_isCoreTheory
= true;
947 if (d_subtheoryMap
.find(SUB_CORE
) != d_subtheoryMap
.end()) {
948 CoreSolver
* core
= (CoreSolver
*)d_subtheoryMap
[SUB_CORE
];
949 core
->enableSlicer();
954 void TheoryBV::ppStaticLearn(TNode in
, NodeBuilder
<>& learned
) {
955 if(d_staticLearnCache
.find(in
) != d_staticLearnCache
.end()){
958 d_staticLearnCache
.insert(in
);
960 if (in
.getKind() == kind::EQUAL
) {
961 if((in
[0].getKind() == kind::BITVECTOR_PLUS
&& in
[1].getKind() == kind::BITVECTOR_SHL
) ||
962 (in
[1].getKind() == kind::BITVECTOR_PLUS
&& in
[0].getKind() == kind::BITVECTOR_SHL
)) {
963 TNode p
= in
[0].getKind() == kind::BITVECTOR_PLUS
? in
[0] : in
[1];
964 TNode s
= in
[0].getKind() == kind::BITVECTOR_PLUS
? in
[1] : in
[0];
966 if(p
.getNumChildren() == 2
967 && p
[0].getKind() == kind::BITVECTOR_SHL
968 && p
[1].getKind() == kind::BITVECTOR_SHL
){
969 unsigned size
= utils::getSize(s
);
970 Node one
= utils::mkConst(size
, 1u);
971 if(s
[0] == one
&& p
[0][0] == one
&& p
[1][0] == one
){
972 Node zero
= utils::mkConst(size
, 0u);
975 // (s : 1 << S) = (b : 1 << B) + (c : 1 << C)
976 Node b_eq_0
= b
.eqNode(zero
);
977 Node c_eq_0
= c
.eqNode(zero
);
978 Node b_eq_c
= b
.eqNode(c
);
980 Node dis
= utils::mkNode(kind::OR
, b_eq_0
, c_eq_0
, b_eq_c
);
981 Node imp
= in
.impNode(dis
);
986 }else if(in
.getKind() == kind::AND
){
987 for(size_t i
= 0, N
= in
.getNumChildren(); i
< N
; ++i
){
988 ppStaticLearn(in
[i
], learned
);
993 bool TheoryBV::applyAbstraction(const std::vector
<Node
>& assertions
, std::vector
<Node
>& new_assertions
) {
994 bool changed
= d_abstractionModule
->applyAbstraction(assertions
, new_assertions
);
996 options::bitblastMode() == theory::bv::BITBLAST_MODE_EAGER
&&
997 options::bitvectorAig()) {
999 AlwaysAssert (!d_eagerSolver
->isInitialized());
1000 d_eagerSolver
->turnOffAig();
1001 d_eagerSolver
->initialize();
1006 void TheoryBV::setProofLog( BitVectorProof
* bvp
) {
1007 if( options::bitblastMode() == theory::bv::BITBLAST_MODE_EAGER
){
1008 d_eagerSolver
->setProofLog( bvp
);
1010 for( unsigned i
=0; i
< d_subtheories
.size(); i
++ ){
1011 d_subtheories
[i
]->setProofLog( bvp
);
1016 void TheoryBV::setConflict(Node conflict
) {
1017 if (options::bvAbstraction()) {
1018 Node new_conflict
= d_abstractionModule
->simplifyConflict(conflict
);
1020 std::vector
<Node
> lemmas
;
1021 lemmas
.push_back(new_conflict
);
1022 d_abstractionModule
->generalizeConflict(new_conflict
, lemmas
);
1023 for (unsigned i
= 0; i
< lemmas
.size(); ++i
) {
1024 lemma(utils::mkNode(kind::NOT
, lemmas
[i
]));
1028 d_conflictNode
= conflict
;
1031 } /* namespace CVC4::theory::bv */
1032 } /* namespace CVC4::theory */
1033 } /* namespace CVC4 */