dchSat.c 4.95 KB
Newer Older
Alan Mishchenko committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/**CFile****************************************************************

  FileName    [dchSat.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [Choice computation for tech-mapping.]

  Synopsis    [Calls to the SAT solver.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

  Date        [Ver. 1.0. Started - June 29, 2008.]

  Revision    [$Id: dchSat.c,v 1.00 2008/07/29 00:00:00 alanmi Exp $]

***********************************************************************/

#include "dchInt.h"

23 24 25
ABC_NAMESPACE_IMPL_START


Alan Mishchenko committed
26 27 28 29 30 31 32 33 34 35
////////////////////////////////////////////////////////////////////////
///                        DECLARATIONS                              ///
////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////
///                     FUNCTION DEFINITIONS                         ///
////////////////////////////////////////////////////////////////////////

/**Function*************************************************************

Alan Mishchenko committed
36
  Synopsis    [Runs equivalence test for the two nodes.]
Alan Mishchenko committed
37 38 39 40 41 42 43 44

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
45 46 47
int Dch_NodesAreEquiv( Dch_Man_t * p, Aig_Obj_t * pOld, Aig_Obj_t * pNew )
{
    int nBTLimit = p->pPars->nBTLimit;
48
    int pLits[2], RetValue, RetValue1, status;
49
    abctime clk;
Alan Mishchenko committed
50 51 52 53 54 55
    p->nSatCalls++;

    // sanity checks
    assert( !Aig_IsComplement(pNew) );
    assert( !Aig_IsComplement(pOld) );
    assert( pNew != pOld );
Alan Mishchenko committed
56

Alan Mishchenko committed
57
    p->nCallsSince++;  // experiment with this!!!
Alan Mishchenko committed
58 59
    
    // check if SAT solver needs recycling
Alan Mishchenko committed
60 61 62
    if ( p->pSat == NULL || 
        (p->pPars->nSatVarMax && 
         p->nSatVars > p->pPars->nSatVarMax && 
Alan Mishchenko committed
63
         p->nCallsSince > p->pPars->nCallsRecycle) )
Alan Mishchenko committed
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
        Dch_ManSatSolverRecycle( p );

    // if the nodes do not have SAT variables, allocate them
    Dch_CnfNodeAddToSolver( p, pOld );
    Dch_CnfNodeAddToSolver( p, pNew );

    // propage unit clauses
    if ( p->pSat->qtail != p->pSat->qhead )
    {
        status = sat_solver_simplify(p->pSat);
        assert( status != 0 );
        assert( p->pSat->qtail == p->pSat->qhead );
    }

    // solve under assumptions
    // A = 1; B = 0     OR     A = 1; B = 1 
    pLits[0] = toLitCond( Dch_ObjSatNum(p,pOld), 0 );
    pLits[1] = toLitCond( Dch_ObjSatNum(p,pNew), pOld->fPhase == pNew->fPhase );
Alan Mishchenko committed
82 83 84 85 86
    if ( p->pPars->fPolarFlip )
    {
        if ( pOld->fPhase )  pLits[0] = lit_neg( pLits[0] );
        if ( pNew->fPhase )  pLits[1] = lit_neg( pLits[1] );
    }
Alan Mishchenko committed
87
//Sat_SolverWriteDimacs( p->pSat, "temp.cnf", pLits, pLits + 2, 1 );
88
clk = Abc_Clock();
Alan Mishchenko committed
89
    RetValue1 = sat_solver_solve( p->pSat, pLits, pLits + 2, 
Alan Mishchenko committed
90
        (ABC_INT64_T)nBTLimit, (ABC_INT64_T)0, (ABC_INT64_T)0, (ABC_INT64_T)0 );
91
p->timeSat += Abc_Clock() - clk;
Alan Mishchenko committed
92 93
    if ( RetValue1 == l_False )
    {
94
p->timeSatUnsat += Abc_Clock() - clk;
Alan Mishchenko committed
95 96 97 98 99 100 101 102
        pLits[0] = lit_neg( pLits[0] );
        pLits[1] = lit_neg( pLits[1] );
        RetValue = sat_solver_addclause( p->pSat, pLits, pLits + 2 );
        assert( RetValue );
        p->nSatCallsUnsat++;
    }
    else if ( RetValue1 == l_True )
    {
103
p->timeSatSat += Abc_Clock() - clk;
Alan Mishchenko committed
104 105 106 107 108
        p->nSatCallsSat++;
        return 0;
    }
    else // if ( RetValue1 == l_Undef )
    {
109
p->timeSatUndec += Abc_Clock() - clk;
Alan Mishchenko committed
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
        p->nSatFailsReal++;
        return -1;
    }

    // if the old node was constant 0, we already know the answer
    if ( pOld == Aig_ManConst1(p->pAigFraig) )
    {
        p->nSatProof++;
        return 1;
    }

    // solve under assumptions
    // A = 0; B = 1     OR     A = 0; B = 0 
    pLits[0] = toLitCond( Dch_ObjSatNum(p,pOld), 1 );
    pLits[1] = toLitCond( Dch_ObjSatNum(p,pNew), pOld->fPhase ^ pNew->fPhase );
Alan Mishchenko committed
125 126 127 128 129
    if ( p->pPars->fPolarFlip )
    {
        if ( pOld->fPhase )  pLits[0] = lit_neg( pLits[0] );
        if ( pNew->fPhase )  pLits[1] = lit_neg( pLits[1] );
    }
130
clk = Abc_Clock();
Alan Mishchenko committed
131
    RetValue1 = sat_solver_solve( p->pSat, pLits, pLits + 2, 
Alan Mishchenko committed
132
        (ABC_INT64_T)nBTLimit, (ABC_INT64_T)0, (ABC_INT64_T)0, (ABC_INT64_T)0 );
133
p->timeSat += Abc_Clock() - clk;
Alan Mishchenko committed
134 135
    if ( RetValue1 == l_False )
    {
136
p->timeSatUnsat += Abc_Clock() - clk;
Alan Mishchenko committed
137 138 139 140 141 142 143 144
        pLits[0] = lit_neg( pLits[0] );
        pLits[1] = lit_neg( pLits[1] );
        RetValue = sat_solver_addclause( p->pSat, pLits, pLits + 2 );
        assert( RetValue );
        p->nSatCallsUnsat++;
    }
    else if ( RetValue1 == l_True )
    {
145
p->timeSatSat += Abc_Clock() - clk;
Alan Mishchenko committed
146 147 148 149 150
        p->nSatCallsSat++;
        return 0;
    }
    else // if ( RetValue1 == l_Undef )
    {
151
p->timeSatUndec += Abc_Clock() - clk;
Alan Mishchenko committed
152 153 154 155 156 157 158 159
        p->nSatFailsReal++;
        return -1;
    }
    // return SAT proof
    p->nSatProof++;
    return 1;
}

Alan Mishchenko committed
160 161 162 163 164 165

////////////////////////////////////////////////////////////////////////
///                       END OF FILE                                ///
////////////////////////////////////////////////////////////////////////


166 167
ABC_NAMESPACE_IMPL_END