]> git.unchartedbackwaters.co.uk Git - francis/stp.git/commitdiff
Changes to inactive by default code.
authortrevor_hansen <trevor_hansen@e59a4935-1847-0410-ae03-e826735625c1>
Sat, 28 Aug 2010 02:52:36 +0000 (02:52 +0000)
committertrevor_hansen <trevor_hansen@e59a4935-1847-0410-ae03-e826735625c1>
Sat, 28 Aug 2010 02:52:36 +0000 (02:52 +0000)
This patch makes the term-level simplifier, the bit-blaster and the constant bit propagation co-operate during simplification. If the cbitp or the bitblaster discover a new constant. Then the term-level simplifier will be re-ran.

I haven't experimented whether this is useful yet.

git-svn-id: https://stp-fast-prover.svn.sourceforge.net/svnroot/stp-fast-prover/trunk/stp@1002 e59a4935-1847-0410-ae03-e826735625c1

src/AST/NodeFactory/NodeFactory.cpp
src/AST/NodeFactory/NodeFactory.h
src/simplifier/constantBitP/ConstantBitPropagation.h
src/simplifier/constantBitP/FixedBits.h
src/simplifier/constantBitP/WorkList.h
src/to-sat/AIG/BBNodeManagerAIG.cpp
src/to-sat/AIG/ToSATAIG.cpp
src/to-sat/BitBlaster.cpp
src/to-sat/BitBlaster.h
src/to-sat/ToSAT.cpp

index ff7af9b2acef160da7258fa5c8a99bd103b161e3..3e4b8053bb99583da6ce500b29bf1ece05484883 100644 (file)
@@ -104,6 +104,9 @@ BEEV::ASTNode NodeFactory::CreateArrayTerm(Kind kind, unsigned int index,
        return result;
 }
 
+BEEV::ASTNode NodeFactory::getTrue() {return bm.ASTTrue;}
+BEEV::ASTNode NodeFactory::getFalse(){return bm.ASTFalse;}
+
 
 ASTNode NodeFactory::CreateSymbol(const char * const name, unsigned indexWidth, unsigned valueWidth)
 {
@@ -112,3 +115,8 @@ ASTNode NodeFactory::CreateSymbol(const char * const name, unsigned indexWidth,
   n.SetValueWidth(valueWidth);
   return n;
 }
+
+ASTNode NodeFactory::CreateConstant(BEEV::CBV  cbv, unsigned width)
+{
+  return bm.CreateBVConst(cbv,width);
+}
index e09934d4e85163d395413e282a91caa08e602774..a32e3a060529307bdeb5d09d3d2085c298e4babb 100644 (file)
@@ -11,6 +11,7 @@ class ASTNode;
 typedef std::vector<ASTNode> ASTVec;
 extern ASTVec _empty_ASTVec;
 class STPMgr;
+typedef unsigned int * CBV;
 }
 
 using BEEV::ASTNode;
@@ -60,6 +61,11 @@ public:
                        const ASTNode& child1, const ASTNode& child2,
                        const ASTVec &children = _empty_ASTVec);
 
+       ASTNode getTrue();
+       ASTNode getFalse();
+
+       ASTNode CreateConstant(BEEV::CBV cbv, unsigned width);
+
 };
 
 #endif
index 7b8bdd3e8f62feee591b41c297f6914057f1d9cc..454c79902b0027bbf5f6203a87e7c7340d157505 100644 (file)
@@ -105,6 +105,12 @@ public:
 
       ASTNodeMap
       getAllFixed();
+
+      void initWorkList(const ASTNode n)
+      {
+        workList->initWorkList(n);
+      }
+
     };
   }
 }
index 6ded3d74895891d5fd20fcb83fae08eda317ac14..859a7154b5f763acd6bc832d6edf9caf749f72c5 100644 (file)
@@ -279,6 +279,21 @@ namespace simplifier
       void
       getUnsignedMinMax(unsigned &minShift, unsigned &maxShift) const;
 
+      void
+      mergeIn(const FixedBits& a)
+      {
+        assert(a.getWidth() == getWidth());
+        for (int i= 0; i < width;i++)
+          {
+          if (a.isFixed(i) && !isFixed(i))
+            {
+            setFixed(i,true);
+            setValue(i,a.getValue(i));
+            }
+          }
+      }
+
+
       static FixedBits
       meet(const FixedBits& a, const FixedBits& b);
 
index 4900ed524786a55dedff5856534b35d107ed7b58..df957fe21c3535c88bce552e3dd07887e8c84ed8 100644 (file)
@@ -48,11 +48,18 @@ namespace simplifier
       // Add to the worklist any node that immediately depends on a constant.
 
       WorkList(const ASTNode& top)
+      {
+        initWorkList(top);
+      }
+
+      void
+      initWorkList(const ASTNode&n)
       {
         ASTNodeSet visited;
-        addToWorklist(top, visited);
+        addToWorklist(n, visited);
       }
 
+
       void
       push(const BEEV::ASTNode& n)
       {
index 088349dfb209b1a166cc7d78aa3fc90d21a3d31c..f12473719253e00e30053d0dbfe1c9236b2ff125 100644 (file)
@@ -18,12 +18,13 @@ namespace BEEV
     // Rewriting is sometimes very slow. Can it be configured to be faster?
     // What about refactoring???
 
-    if (uf.enable_AIG_rewrites_flag)
-      {
-          int nodeCount = aigMgr->nObjs[AIG_OBJ_AND];
-          if (uf.stats_flag)
-            cerr << "Nodes before AIG rewrite:" << nodeCount <<endl;
+    int nodeCount = aigMgr->nObjs[AIG_OBJ_AND];
+    if (uf.stats_flag)
+      cerr << "Nodes before AIG rewrite:" << nodeCount <<endl;
+
 
+    if (uf.enable_AIG_rewrites_flag && aigMgr->nObjs[AIG_OBJ_AND] < 5000)
+      {
           Dar_LibStart();
           Aig_Man_t * pTemp;
           Dar_RwrPar_t Pars, * pPars = &Pars;
@@ -38,12 +39,6 @@ namespace BEEV
           // With nCutsMax =2, CNF generation takes 16 seconds, solving 10 seconds.
           // The rewriting doesn't remove as many nodes of course..
           int iterations = 3;
-          if (nodeCount > 1000000)
-          {
-            iterations =1;
-            pPars->nCutsMax=2;
-          }
-
 
           for (int i=0; i < iterations;i++)
           {
index f1980f0746789f59709c20fa5b70f498428ec063..7774d6a5bf25aa9dcc562d7a494b5dd28b6dd3b4 100644 (file)
@@ -1,5 +1,6 @@
 #include "ToSATAIG.h"
 #include "../../simplifier/constantBitP/ConstantBitPropagation.h"
+#include "../../simplifier/simplifier.h"
 
 namespace BEEV
 {
@@ -12,14 +13,20 @@ namespace BEEV
         return false;
 
       BBNodeManagerAIG mgr;
-      BitBlaster<BBNodeAIG, BBNodeManagerAIG> bb(&mgr,cb);
+      Simplifier simp(bm);
+      BitBlaster<BBNodeAIG, BBNodeManagerAIG> bb(&mgr,cb,&simp,bm->defaultNodeFactory,&bm->UserFlags);
 
       bm->GetRunTimes()->start(RunTimes::BitBlasting);
       BBNodeAIG BBFormula = bb.BBForm(input);
       bm->GetRunTimes()->stop(RunTimes::BitBlasting);
+      bb.ClearAllTables();
 
       assert(satSolver.nVars() ==0);
 
+      // Oddly the substitution map, which is necessary to output a model is kept in the simplifier.
+      // The bitblaster should never enter anything into the solver map.
+      //assert(simp.Return_SolverMap()->size() ==0);
+
       Cnf_Dat_t* cnfData = NULL;
 
       bm->GetRunTimes()->start(RunTimes::CNFConversion);
@@ -30,17 +37,6 @@ namespace BEEV
       BBFormula = BBNodeAIG(); // null node
       mgr.stop();
 
-      // make the result true, see if a contradiction arises.
-      /*
-      if (cb != NULL)
-        {
-        cb->setNodeToTrue(input);
-        cb->propagate();
-        if (cb->isUnsatisfiable())
-          return false;
-        }
-      */
-
       //Clear out all the constant bit stuff before sending the SAT.
       if (cb != NULL)
          cb->clearTables();
index ee2b0dd5037b770c9ec1a235eb1e71f6a3843187..46cc4cf8f9476951d81097910eea8fa5c7be9ed0 100644 (file)
@@ -15,6 +15,7 @@
 #include "../simplifier/constantBitP/FixedBits.h"
 #include "../simplifier/constantBitP/ConstantBitPropagation.h"
 #include "../simplifier/constantBitP/NodeToFixedBitsMap.h"
+#include "../simplifier/simplifier.h"
 
 namespace BEEV {
 
@@ -136,6 +137,9 @@ void BitBlaster<BBNode,BBNodeManagerT>::updateTerm(const ASTNode&n, BBNodeVec& b
         if (cb == NULL)
                 return;
 
+        if (cb->isUnsatisfiable())
+          return;
+
         if (n.isConstant())
           {
               simplifier::constantBitP::NodeToFixedBitsMap::NodeToFixedBitsMapType::const_iterator it;
@@ -212,18 +216,169 @@ void BitBlaster<BBNode,BBNodeManagerT>::updateTerm(const ASTNode&n, BBNodeVec& b
           }
 }
 
+template <class BBNode, class BBNodeManagerT>
+bool BitBlaster<BBNode,BBNodeManagerT>::isConstant(const BBNodeVec& v)
+{
+  for (int i =0; i < v.size();i++)
+    {
+        if (v[i] != nf->getTrue() && v[i] != nf->getFalse())
+          return false;
+    }
 
+  return true;
+}
 
 
 template <class BBNode, class BBNodeManagerT>
-const BBNodeVec BitBlaster<BBNode,BBNodeManagerT>::BBTerm(const ASTNode& term, BBNodeSet& support) {
-        typename BBNodeVecMap::iterator it = BBTermMemo.find(term);
+ASTNode BitBlaster<BBNode,BBNodeManagerT>::getConstant(const BBNodeVec& v, const ASTNode&n )
+{
+  if (n.GetType() == BOOLEAN_TYPE)
+    {
+      if (v[0] == nf->getTrue())
+        return ASTNF->getTrue();
+      else
+        return ASTNF->getFalse();
+    }
+
+  CBV bv = CONSTANTBV::BitVector_Create(v.size(),true);
+
+  for (int i =0; i < v.size();i++)
+      if (v[i] == nf->getTrue())
+        CONSTANTBV::BitVector_Bit_On(bv,i);
+
+  return ASTNF->CreateConstant(bv,v.size());
+
+}
+
+template <class BBNode, class BBNodeManagerT>
+const BBNodeVec BitBlaster<BBNode,BBNodeManagerT>::BBTerm(const ASTNode& _term, BBNodeSet& support) {
+      ASTNode term = _term; // mutable local copy.
+
+      typename BBNodeVecMap::iterator it = BBTermMemo.find(term);
        if (it != BBTermMemo.end()) {
                 // Constant bit propagation may have updated something.
                 updateTerm(term,it->second,support);
                 return it->second;
        }
 
+       // This block checks if the bitblasting/fixed bits have discovered
+       // any new constants. If they've discovered a new constant, then
+       // the simplification function is called on a new term with the constant
+       // value replacing what used to be a variable child. For instance, if
+       // the term is ite(x,y,z), and we now know that x is true. Then we will
+       // call SimplifyTerm on ite(true,y,z), which will do the expected simplification.
+       // Then the term that we bitblast will by "y".
+
+       if (uf !=NULL && uf->optimize_flag)
+       {
+          const int numberOfChildren = term.Degree();
+          vector<BBNodeVec> ch;
+          ch.reserve(numberOfChildren);
+
+          for (int i = 0; i < numberOfChildren; i++)
+            {
+              if (term[i].GetType() == BITVECTOR_TYPE)
+                {
+                  ch.push_back(BBTerm(term[i],support));
+                }
+              else if (term[i].GetType() == BOOLEAN_TYPE)
+                {
+                  BBNodeVec t;
+                  t.push_back(BBForm(term[i],support));
+                  ch.push_back(t);
+                }
+              else
+                throw "sdfssfa";
+            }
+
+          bool newConst = false;
+          for (int i = 0; i < numberOfChildren; i++)
+            {
+                if (term[i].isConstant())
+                    continue;
+
+                if (isConstant(ch[i]))
+                    {
+                      // it's only interesting if the child isn't a constant,
+                      // but the bitblasted version is.
+                      newConst = true;
+                      break;
+                    }
+            }
+
+          // Something is now constant that didn't used to be.
+          if (newConst)
+            {
+              ASTVec new_ch;
+              new_ch.reserve(numberOfChildren);
+              for (int i = 0; i < numberOfChildren; i++)
+                {
+                  if (!term[i].isConstant() && isConstant(ch[i]))
+                    new_ch.push_back(getConstant(ch[i], term[i]));
+                  else
+                    new_ch.push_back(term[i]);
+                }
+
+
+              ASTNode n_term = simp->SimplifyTerm(ASTNF->CreateTerm(term.GetKind(),term.GetValueWidth(),new_ch));
+              assert(BVTypeCheck(n_term));
+              // n_term is the potentially simplified version of term.
+
+              if (cb!= NULL)
+                {
+                  // Add all the nodes to the worklist that have a constant as a child.
+                  cb->initWorkList(n_term);
+
+                  simplifier::constantBitP::NodeToFixedBitsMap::NodeToFixedBitsMapType::iterator it;
+                  it = cb->fixedMap->map->find(n_term) ;
+                  FixedBits* nBits;
+                  if (it == cb->fixedMap->map->end())
+                  {
+                      nBits = new FixedBits(std::max((unsigned)1,n_term.GetValueWidth()), term.GetType() == BOOLEAN_TYPE);
+                      cb->fixedMap->map->insert(pair<ASTNode, FixedBits*> (n_term, nBits));
+                  }else
+                      nBits = it->second;
+
+                  if (n_term.isConstant())
+                    {
+                      // It's assumed elsewhere that constants map to themselves in the fixed map.
+                      // That doesn't happen here unless it's added explicitly.
+                      *nBits = FixedBits::concreteToAbstract(n_term);
+                    }
+
+                   it = cb->fixedMap->map->find(term);
+                   if (it != cb->fixedMap->map->end())
+                   {
+                     // Copy over to the (potentially) new node. Everything we know about the old node.
+                     nBits->mergeIn(*(it->second));
+
+                   }
+                   cb->propagate();
+
+                   it = cb->fixedMap->map->find(term);
+                   if (it != cb->fixedMap->map->end())
+                   {
+                     // Copy to the old node, all we know about the new node. This means that
+                     // all the parents of the old node get the (potentially) updated fixings.
+                     it->second->mergeIn(*nBits);
+                   }
+                   // Propagate through all the parents of term.
+                   cb->scheduleUp(term);
+                   cb->propagate();
+                   // Now we've propagated.
+                }
+              term = n_term;
+
+              // check if we've already done the simplified one.
+                 it = BBTermMemo.find(term);
+                 if (it != BBTermMemo.end()) {
+                          // Constant bit propagation may have updated something.
+                          updateTerm(term,it->second,support);
+                          return it->second;
+                  }
+            }
+        }
+
        BBNodeVec result;
 
        const Kind k = term.GetKind();
@@ -1305,6 +1460,7 @@ const bool multiplication_variant3 = true; // multiplication with booth recoding
 const bool multiplication_variant4 = false;  // multiplication via sorting networks.
 const bool multiplication_variant5 = false;  // Using bounds.
 
+
 // Multiply two bitblasted numbers
 template <class BBNode, class BBNodeManagerT>
 BBNodeVec BitBlaster<BBNode,BBNodeManagerT>::BBMult(const BBNodeVec& _x, const BBNodeVec& _y,
@@ -1323,23 +1479,27 @@ BBNodeVec BitBlaster<BBNode,BBNodeManagerT>::BBMult(const BBNodeVec& _x, const B
       const int bitWidth = x.size();
 
       if (multiplication_variant1) {
+              //cout << "v1";
               return mult_normal(x, y, support);
       }
 
       stack<BBNode> products[bitWidth];
 
       if (multiplication_variant2) {
+              //cout << "v2";
               mult_allPairs(x, y, support,products);
               return buildAdditionNetworkResult(products,support, bitWidth);
       }
 
       if (multiplication_variant3) {
+              //cout << "v3";
               mult_Booth(_x, _y, support,n[0],n[1],products);
               return buildAdditionNetworkResult(products,support,bitWidth);
       }
 
       if (multiplication_variant4)
       {
+        //cout << "v4";
         mult_Booth(_x, _y, support,n[0],n[1],products);
         for (int i = 0; i < bitWidth; i++)
           {
@@ -1354,7 +1514,7 @@ BBNodeVec BitBlaster<BBNode,BBNodeManagerT>::BBMult(const BBNodeVec& _x, const B
 
       if (multiplication_variant5)
         {
-
+        //cout << "v5";
           if (!statsFound(n))
             {
               mult_Booth(_x, _y, support, n[0], n[1], products);
index e73f3e31af28fbaaacad36da3a006939248b224b..a78d716760f19115645b550e322c3c81d57761e2 100644 (file)
@@ -14,6 +14,7 @@
 #include <cassert>
 #include <map>
 #include "../STPManager/STPManager.h"
+//#include "../STPManager/UserDefinedFlags.h"
 
 namespace simplifier
 {
@@ -26,6 +27,7 @@ namespace simplifier
 
 namespace BEEV {
 
+class Simplifier;
 class ASTNode;
 typedef std::vector<ASTNode> ASTVec;
 
@@ -150,12 +152,19 @@ class BitBlaster {
 
         const BBNode BBForm(const ASTNode& form, set<BBNode>& support);
 
+        bool isConstant(const vector<BBNode>& v);
+        ASTNode getConstant(const vector<BBNode>& v, const ASTNode&n );
+
         // Nodes in this set can be replaced by their constant values, without being
         // conjoined to the top..
         ASTNodeSet fixedFromBottom;
 
+        UserDefinedFlags *uf;
+        NodeFactory* ASTNF;
+        Simplifier* simp;
+        BBNodeManagerT* nf;
+
 public:
-       BBNodeManagerT* nf;
 
        // Bit blast a bitvector term.  The term must have a kind for a
        // bitvector term.  Result is a ref to a vector of formula nodes
@@ -166,28 +175,39 @@ public:
         * Public Member Functions                                      *
         ****************************************************************/
 
-        BitBlaster(BBNodeManagerT* bnm  , simplifier::constantBitP::ConstantBitPropagation *cb_)
+        BitBlaster(BBNodeManagerT* bnm  , simplifier::constantBitP::ConstantBitPropagation *cb_, Simplifier* _simp, NodeFactory *astNodeF, UserDefinedFlags *_uf)
                 {
           nf = bnm;
           cb = cb_;
           BBTrue = nf->getTrue();
           BBFalse = nf->getFalse();
+          simp = _simp;
+          ASTNF = astNodeF;
+          uf = _uf;
         }
 
 
 
-        BitBlaster(BBNodeManagerT* bnm)
+        BitBlaster(BBNodeManagerT* bnm, Simplifier* _simp, NodeFactory *astNodeF)
                {
           nf = bnm;
           BBTrue = nf->getTrue();
           BBFalse = nf->getFalse();
           cb = NULL;
+          simp = _simp;
+          ASTNF = astNodeF;
+          uf = NULL;
        }
 
 
+        void ClearAllTables()
+        {
+          BBTermMemo.clear();
+          BBFormMemo.clear();
+        }
+
        ~BitBlaster() {
-               BBTermMemo.clear();
-               BBFormMemo.clear();
+         ClearAllTables();
        }
 
        //Bitblast a formula
index 3cae17509fe3f0d5730990fb5cb35d218a1ba0c6..9954dc3ad5a8222c8798fb4e34de6ed642574bfc 100644 (file)
@@ -337,7 +337,8 @@ namespace BEEV
     ASTNode BBFormula;
     {
         BBNodeManagerASTNode nm(bm);
-        BitBlaster<ASTNode,BBNodeManagerASTNode> BB(&nm);
+        Simplifier simp(bm);
+        BitBlaster<ASTNode,BBNodeManagerASTNode> BB(&nm,&simp, bm->defaultNodeFactory);
        BBFormula = BB.BBForm(input);
     }