From: lianah Date: Thu, 23 Oct 2014 18:05:21 +0000 (-0700) Subject: Fixed inefficiency in bit-vector rewrite rule. X-Git-Tag: cvc5-1.0.0~6545 X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=1c8d1d7c5831baebc0a59a7dcf36f942504e5556;p=cvc5.git Fixed inefficiency in bit-vector rewrite rule. --- diff --git a/src/theory/bv/theory_bv_rewrite_rules.h b/src/theory/bv/theory_bv_rewrite_rules.h index bc9095f03..3cc1c323c 100644 --- a/src/theory/bv/theory_bv_rewrite_rules.h +++ b/src/theory/bv/theory_bv_rewrite_rules.h @@ -152,6 +152,7 @@ enum RewriteRuleId { NotOr, // not sure why this would help (not done) NotXor, // not sure why this would help (not done) FlattenAssocCommut, + FlattenAssocCommutNoDuplicates, PlusCombineLikeTerms, MultSimplify, MultDistribConst, @@ -270,7 +271,8 @@ inline std::ostream& operator << (std::ostream& out, RewriteRuleId ruleId) { case SignExtendEliminate : out << "SignExtendEliminate"; return out; case NotIdemp : out << "NotIdemp"; return out; case UleSelf: out << "UleSelf"; return out; - case FlattenAssocCommut: out << "FlattenAssocCommut"; return out; + case FlattenAssocCommut: out << "FlattenAssocCommut"; return out; + case FlattenAssocCommutNoDuplicates: out << "FlattenAssocCommutNoDuplicates"; return out; case PlusCombineLikeTerms: out << "PlusCombineLikeTerms"; return out; case MultSimplify: out << "MultSimplify"; return out; case MultDistribConst: out << "MultDistribConst"; return out; @@ -485,8 +487,9 @@ struct AllRewriteRules { RewriteRule rule82; RewriteRule rule83; RewriteRule rule84; - RewriteRule rule87; RewriteRule rule85; + RewriteRule rule86; + RewriteRule rule87; RewriteRule rule88; RewriteRule rule91; RewriteRule rule92; diff --git a/src/theory/bv/theory_bv_rewrite_rules_normalization.h b/src/theory/bv/theory_bv_rewrite_rules_normalization.h index 21b59fa8c..bc1b92dce 100644 --- a/src/theory/bv/theory_bv_rewrite_rules_normalization.h +++ b/src/theory/bv/theory_bv_rewrite_rules_normalization.h @@ -995,6 +995,51 @@ Node RewriteRule::apply(TNode node) { return utils::mkSortedNode(kind::BITVECTOR_AND, children); } +template<> inline +bool RewriteRule::applies(TNode node) { + Kind kind = node.getKind(); + if (kind != kind::BITVECTOR_OR && + kind != kind::BITVECTOR_AND) + return false; + TNode::iterator child_it = node.begin(); + for(; child_it != node.end(); ++child_it) { + if ((*child_it).getKind() == kind) { + return true; + } + } + return false; +} + +template<> inline +Node RewriteRule::apply(TNode node) { + Debug("bv-rewrite") << "RewriteRule(" << node << ")" << std::endl; + std::vector processingStack; + processingStack.push_back(node); + __gnu_cxx::hash_set processed; + std::vector children; + Kind kind = node.getKind(); + + while (! processingStack.empty()) { + TNode current = processingStack.back(); + processingStack.pop_back(); + if (processed.count(current)) + continue; + + processed.insert(current); + + // flatten expression + if (current.getKind() == kind) { + for (unsigned i = 0; i < current.getNumChildren(); ++i) { + processingStack.push_back(current[i]); + } + } else { + children.push_back(current); + } + } + return utils::mkSortedNode(kind, children); +} + + template<> inline bool RewriteRule::applies(TNode node) { return (node.getKind() == kind::BITVECTOR_OR); diff --git a/src/theory/bv/theory_bv_rewriter.cpp b/src/theory/bv/theory_bv_rewriter.cpp index 306a3bd97..dc91c338b 100644 --- a/src/theory/bv/theory_bv_rewriter.cpp +++ b/src/theory/bv/theory_bv_rewriter.cpp @@ -214,7 +214,7 @@ RewriteResponse TheoryBVRewriter::RewriteConcat(TNode node, bool prerewrite) { RewriteResponse TheoryBVRewriter::RewriteAnd(TNode node, bool prerewrite) { Node resultNode = node; resultNode = LinearRewriteStrategy - < RewriteRule, + < RewriteRule, RewriteRule >::apply(node); @@ -234,7 +234,7 @@ RewriteResponse TheoryBVRewriter::RewriteAnd(TNode node, bool prerewrite) { RewriteResponse TheoryBVRewriter::RewriteOr(TNode node, bool prerewrite){ Node resultNode = node; resultNode = LinearRewriteStrategy - < RewriteRule, + < RewriteRule, RewriteRule >::apply(node);