예제 #1
0
  ASTNode
  SubstitutionMap::replace(const ASTNode& n, ASTNodeMap& fromTo, ASTNodeMap& cache, NodeFactory * nf, bool stopAtArrays,
      bool preventInfinite)
  {
    const Kind k = n.GetKind();
    if (k == BVCONST || k == TRUE || k == FALSE)
      return n;

    ASTNodeMap::const_iterator it;

    if ((it = cache.find(n)) != cache.end())
      return it->second;

    if ((it = fromTo.find(n)) != fromTo.end())
      {
      const ASTNode& r = it->second;
      assert(r.GetIndexWidth() == n.GetIndexWidth());

      if (preventInfinite)
        cache.insert(make_pair(n, r));

      ASTNode replaced = replace(r, fromTo, cache, nf, stopAtArrays, preventInfinite);
      if (replaced != r)
        {
          fromTo.erase(n);
          fromTo[n] = replaced;
        }

      if (preventInfinite)
        cache.erase(n);

      cache.insert(make_pair(n, replaced));
      return replaced;
      }

    // These can't be created like regular nodes are
    if (k == SYMBOL)
      return n;

    const unsigned int indexWidth = n.GetIndexWidth();
    if (stopAtArrays && indexWidth > 0) // is an array.
      {
      return n;
      }

    const ASTVec& children = n.GetChildren();
    assert(children.size() > 0);
    // Should have no leaves left here.

    ASTVec new_children;
    new_children.reserve(children.size());

    for (ASTVec::const_iterator it = children.begin(); it != children.end(); it++)
      {
      new_children.push_back(replace(*it, fromTo, cache, nf, stopAtArrays, preventInfinite));
      }

    assert(new_children.size() == children.size());

    // This code short-cuts if the children are the same. Nodes with the same children,
    // won't have necessarily given the same node if the simplifyingNodeFactory is enabled
    // now, but wasn't enabled when the node was created. Shortcutting saves lots of time.
    if (new_children == children)
      {
      cache.insert(make_pair(n, n));
      return n;
      }

    ASTNode result;
    const unsigned int valueWidth = n.GetValueWidth();

    if (valueWidth == 0) // n.GetType() == BOOLEAN_TYPE
      {
      result = nf->CreateNode(k, new_children);
      }
    else
      {
      // If the index and value width aren't saved, they are reset sometimes (??)
      result = nf->CreateArrayTerm(k, indexWidth, valueWidth, new_children);
      }

    // We may have created something that should be mapped. For instance,
    // if n is READ(A, x), and the fromTo is: {x==0, READ(A,0) == 1}, then
    // by here the result will be READ(A,0). Which needs to be mapped again..
    // I hope that this makes it idempotent.

    if (fromTo.find(result) != fromTo.end())
      {
      // map n->result, if running replace() on result gives us 'n', it will not infinite loop.
      // This is only currently required for the bitblast equivalence stuff.
      if (preventInfinite)
        cache.insert(make_pair(n, result));

      result = replace(result, fromTo, cache, nf, stopAtArrays, preventInfinite);
      }

    assert(result.GetValueWidth() == valueWidth);
    assert(result.GetIndexWidth() == indexWidth);

    // If there is already an "n" element in the cache, the maps semantics are to ignore the next insertion.
    if (preventInfinite)
      cache.erase(n);

    cache.insert(make_pair(n, result));
    return result;
  }
예제 #2
0
    // NB: This expects that the constructor was called with teh same node. Sorry.
    ASTNode
    ConstantBitPropagation::topLevelBothWays(const ASTNode& top)
    {
      assert(top.GetSTPMgr()->UserFlags.bitConstantProp_flag);
      assert (BOOLEAN_TYPE == top.GetType());

      propagate();
      status = NO_CHANGE;

      //Determine what must always be true.
      ASTNodeMap fromTo = getAllFixed();

      if (debug_cBitProp_messages)
        {
          cerr << "Number removed by bottom UP:" << fromTo.size() << endl;
        }

      setNodeToTrue(top);

      if (debug_cBitProp_messages)
        {
          cerr << "starting propagation" << endl;
          printNodeWithFixings();
          cerr << "Initial Tree:" << endl;
          cerr << top;
        }

      propagate();

      if (debug_cBitProp_messages)
        {
          cerr << "status:" << status <<endl;
          cerr << "ended propagation" << endl;
          printNodeWithFixings();
        }

      // propagate may have stopped with a conflict.
      if (CONFLICT == status)
          return top.GetSTPMgr()->CreateNode(FALSE);

      ASTVec toConjoin;

      // go through the fixedBits. If a node is entirely fixed.
      // "and" it onto the top. Creates redundancy. Check that the
      // node doesn't already depend on "top" directly.

      for (NodeToFixedBitsMap::NodeToFixedBitsMapType::iterator it = fixedMap->map->begin(); it != fixedMap->map->end(); it++) // iterates through all the pairs of node->fixedBits.
        {
          const FixedBits& bits = *it->second;

          if (!bits.isTotallyFixed())
            continue;

          const ASTNode& node = (it->first);

          // Don't constrain nodes we already know all about.
          if (node.isConstant())
            continue;

          // other nodes will contain the same information (the extract doesn't change the fixings).
          if (BVEXTRACT == node.GetKind() || BVCONCAT == node.GetKind())
            continue;

          // toAssign: conjoin it with the top level.
          // toReplace: replace all references to it (except the one conjoined to the top) with this.
          ASTNode propositionToAssert;
          ASTNode constantToReplaceWith;
          // skip the assigning and replacing.
          bool doAssign = true;

            {
              // If it is already contained in the fromTo map, then it's one of the values
              // that have fully been determined (previously). Not conjoined.
              if (fromTo.find(node) != fromTo.end())
                continue;

              ASTNode constNode = bitsToNode(node,bits);

              if (node.GetType() == BOOLEAN_TYPE)
                {
                  if (SYMBOL == node.GetKind())
                    {
                      bool r = simplifier->UpdateSubstitutionMap(node, constNode);
                      assert(r);
                      doAssign = false;
                    }
                  else if (bits.getValue(0))
                    {
                      propositionToAssert = node;
                      constantToReplaceWith = constNode;
                    }
                  else
                    {
                      propositionToAssert = nf->CreateNode(NOT, node);
                      constantToReplaceWith = constNode;
                    }
                }
              else if (node.GetType() == BITVECTOR_TYPE)
                {
                  assert(((unsigned)bits.getWidth()) == node.GetValueWidth());
                  if (SYMBOL == node.GetKind())
                    {
                      bool r = simplifier->UpdateSubstitutionMap(node, constNode);
                      assert(r);
                      doAssign = false;
                    }
                  else
                    {
                      propositionToAssert = nf->CreateNode(EQ, node, constNode);
                      constantToReplaceWith = constNode;
                    }
                }
              else
                FatalError("sadf234s");
            }

          if (doAssign && top != propositionToAssert
              && !dependents->nodeDependsOn(top, propositionToAssert))
            {
              assert(!constantToReplaceWith.IsNull());
              assert(constantToReplaceWith.isConstant());
              assert(propositionToAssert.GetType() == BOOLEAN_TYPE);
              assert(node.GetValueWidth() == constantToReplaceWith.GetValueWidth());

              fromTo.insert(make_pair(node, constantToReplaceWith));
              toConjoin.push_back(propositionToAssert);
            }
        }

     // Write the constants into the main graph.
      ASTNodeMap cache;
      ASTNode result = SubstitutionMap::replace(top, fromTo, cache,nf);

      if (0 != toConjoin.size())
        {
          // It doesn't happen very often. But the "toConjoin" might contain a variable
          // that was added to the substitution map (because the value was determined just now
          // during propagation.
          ASTNode conjunct = (1 == toConjoin.size())? toConjoin[0]: nf->CreateNode(AND,toConjoin);
          conjunct = simplifier->applySubstitutionMap(conjunct);

          result = nf->CreateNode(AND, result, conjunct); // conjoin the new conditions.
        }


  	if (debug_print_graph_after)
		{
			ofstream file;
			file.open("afterCbitp.gdl");
			PrintingHackfixedMap = fixedMap;
			printer::GDL_Print(file,top,&toString);
			file.close();
		}


      assert(BVTypeCheck(result));
      assert(status != CONFLICT); // conflict should have been seen earlier.
      return result;
    }