int2Bmc.c 12 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
/**CFile****************************************************************

  FileName    [int2Bmc.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [Interpolation engine.]

  Synopsis    [BMC used inside IMC.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

  Date        [Ver. 1.0. Started - Dec 1, 2013.]

  Revision    [$Id: int2Bmc.c,v 1.00 2013/12/01 00:00:00 alanmi Exp $]

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

#include "int2Int.h"

ABC_NAMESPACE_IMPL_START


////////////////////////////////////////////////////////////////////////
///                        DECLARATIONS                              ///
////////////////////////////////////////////////////////////////////////

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


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

  Synopsis    [Trasnforms AIG to transition into the init state.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Gia_Man_t * Int2_ManDupInit( Gia_Man_t * p, int fVerbose )
{
    Gia_Man_t * pNew, * pTemp;
    Gia_Obj_t * pObj, * pObjRi, * pObjRo;
    int i, iCtrl;
    assert( Gia_ManRegNum(p) > 0 );
    pNew = Gia_ManStart( 10000 );
    pNew->pName = Abc_UtilStrsav( p->pName );
    pNew->pSpec = Abc_UtilStrsav( p->pSpec );
    Gia_ManConst0(p)->Value = 0;
    Gia_ManForEachCi( p, pObj, i )
    {
        if ( i == Gia_ManPiNum(p) )
            iCtrl = Gia_ManAppendCi( pNew );
        pObj->Value = Gia_ManAppendCi( pNew );
    }
    Gia_ManHashAlloc( pNew );
    Gia_ManForEachAnd( p, pObj, i )
        pObj->Value = Gia_ManHashAnd( pNew, Gia_ObjFanin0Copy(pObj), Gia_ObjFanin1Copy(pObj) );
    Gia_ManForEachPo( p, pObj, i )
        pObj->Value = Gia_ManAppendCo( pNew, Gia_ObjFanin0Copy(pObj) );
    Gia_ManForEachRiRo( p, pObjRi, pObjRo, i )
        Gia_ManAppendCo( pNew, Gia_ManHashMux( pNew, iCtrl, pObjRo->Value, Gia_ObjFanin0Copy(pObjRi) ) );
    Gia_ManHashStop( pNew );
    Gia_ManSetRegNum( pNew, Gia_ManRegNum(p) );
    // remove dangling
    pNew = Gia_ManCleanup( pTemp = pNew );
    if ( fVerbose )
        printf( "Before cleanup = %d nodes. After cleanup = %d nodes.\n", 
            Gia_ManAndNum(pTemp), Gia_ManAndNum(pNew) );
    Gia_ManStop( pTemp );
    return pNew;
}


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

  Synopsis    [Returns 1 if AIG has transition into init state.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Int2_ManCheckInit( Gia_Man_t * p )
{
    sat_solver * pSat;
    Cnf_Dat_t * pCnf;
    Gia_Man_t * pNew;
    Gia_Obj_t * pObj;
    Vec_Int_t * vLits;
    int i, Lit, RetValue = 0;
    assert( Gia_ManRegNum(p) > 0 );
    pNew = Jf_ManDeriveCnf( p, 0 );  
    pCnf = (Cnf_Dat_t *)pNew->pData; pNew->pData = NULL;
    pSat = (sat_solver *)Cnf_DataWriteIntoSolver( pCnf, 1, 0 );
    if ( pSat != NULL )
    {
        vLits = Vec_IntAlloc( Gia_ManRegNum(p) );
        Gia_ManForEachRi( pNew, pObj, i )
        {
            Lit = pCnf->pVarNums[ Gia_ObjId(pNew, Gia_ObjFanin0(pObj)) ];
            Lit = Abc_Var2Lit( Lit, Gia_ObjFaninC0(pObj) );
            Vec_IntPush( vLits, Abc_LitNot(Lit) );
        }
        if ( sat_solver_solve( pSat, Vec_IntArray(vLits), Vec_IntArray(vLits) + Vec_IntSize(vLits), 0, 0, 0, 0 ) == l_True )
            RetValue = 1;
        Vec_IntFree( vLits );
        sat_solver_delete( pSat );
    }
    Cnf_DataFree( pCnf );
    Gia_ManStop( pNew );
    return RetValue;
}


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

  Synopsis    [Creates the BMC instance in the SAT solver.]

  Description [The PIs are mapped in the natural order. The flop inputs
  are the last Gia_ManRegNum(p) variables of resulting SAT solver.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Gia_Man_t * Int2_ManFrameInit( Gia_Man_t * p, int nFrames, int fVerbose )
{
    Gia_Man_t * pFrames, * pTemp;
    Gia_Obj_t * pObj;
    int i, f;
    pFrames = Gia_ManStart( 10000 );
    pFrames->pName = Abc_UtilStrsav( p->pName );
    pFrames->pSpec = Abc_UtilStrsav( p->pSpec );
    Gia_ManConst0(p)->Value = 0;
    // perform structural hashing
    Gia_ManHashAlloc( pFrames );
    for ( f = 0; f < nFrames; f++ )
    {
        Gia_ManForEachPi( p, pObj, i )
            pObj->Value = Gia_ManAppendCi( pFrames );
        Gia_ManForEachRo( p, pObj, i )
            pObj->Value = f ? Gia_ObjRoToRi(p, pObj)->Value : 0;
        Gia_ManForEachAnd( p, pObj, i )
            pObj->Value = Gia_ManHashAnd( pFrames, Gia_ObjFanin0Copy(pObj), Gia_ObjFanin1Copy(pObj) );
        Gia_ManForEachRi( p, pObj, i )
            pObj->Value = Gia_ObjFanin0Copy(pObj);
    }
    Gia_ManHashStop( pFrames );
    // create flop inputs
    Gia_ManForEachRi( p, pObj, i )
        pObj->Value = Gia_ManAppendCo( pFrames, Gia_ObjFanin0Copy(pObj) );
    // remove dangling
    pFrames = Gia_ManCleanup( pTemp = pFrames );
    if ( fVerbose )
        printf( "Before cleanup = %d nodes. After cleanup = %d nodes.\n", 
            Gia_ManAndNum(pTemp), Gia_ManAndNum(pFrames) );
    Gia_ManStop( pTemp );
    return pFrames;
}
sat_solver * Int2_ManSetupBmcSolver( Gia_Man_t * p, int nFrames )
{
    Gia_Man_t * pFrames, * pTemp;
    Cnf_Dat_t * pCnf;
    sat_solver * pSat;
    // unfold for the given number of timeframes
    pFrames = Int2_ManFrameInit( p, nFrames, 1 );
    assert( Gia_ManRegNum(pFrames) == 0 );
    // derive CNF for the timeframes
    pFrames = Jf_ManDeriveCnf( pTemp = pFrames, 0 );  Gia_ManStop( pTemp );
    pCnf = (Cnf_Dat_t *)pFrames->pData; pFrames->pData = NULL;
    // create SAT solver
    pSat = (sat_solver *)Cnf_DataWriteIntoSolver( pCnf, 1, 0 );
    if ( pSat != NULL )
    {
        Gia_Obj_t * pObj;
        int i, nVars = sat_solver_nvars( pSat );
        sat_solver_setnvars( pSat, nVars + Gia_ManPoNum(pFrames) );
        // add clauses for the POs
        Gia_ManForEachCo( pFrames, pObj, i )
            sat_solver_add_buffer( pSat, nVars + i, pCnf->pVarNums[Gia_ObjId(pFrames, Gia_ObjFanin0(pObj))], Gia_ObjFaninC0(pObj) );
    }
    Cnf_DataFree( pCnf );
    Gia_ManStop( pFrames );
    return pSat;
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline int Int2_ManCheckFrames( Int2_Man_t * p, int iFrame, int iObj )
{
    Vec_Int_t * vMapFrame = (Vec_Int_t *)Vec_PtrEntry(p->vMapFrames, iFrame);
    return Vec_IntEntry(vMapFrame, iObj);
}
static inline void Int2_ManWriteFrames( Int2_Man_t * p, int iFrame, int iObj, int iRes )
{
    Vec_Int_t * vMapFrame = (Vec_Int_t *)Vec_PtrEntry(p->vMapFrames, iFrame);
    assert( Vec_IntEntry(vMapFrame, iObj) == -1 );
    Vec_IntWriteEntry( vMapFrame, iObj, iRes );
}
void Int2_ManCreateFrames( Int2_Man_t * p, int iFrame, Vec_Int_t * vPrefCos )
{
    Gia_Obj_t * pObj;
    int i, Entry, iLit;
    // create storage room for unfolded IDs
    for ( i = Vec_PtrSize(p->vMapFrames); i <= iFrame; i++ )
        Vec_PtrPush( p->vMapFrames, Vec_IntStartFull( Gia_ManObjNum(p->pGia) ) );
    assert( Vec_PtrSize(p->vMapFrames) == iFrame + 1 );
    // create constant 0 node
    if ( f == 0 )
    {
        iLit = 1;
        Int2_ManWriteFrames( p, iFrame, iObj, 0 );
        sat_solver_addclause( p->pGiaPref, &iLit, &iLit + 1 );
    }
    // start the stack
    Vec_IntClear( p->vStack );
    Vec_IntForEachEntry( vPrefCos, Entry, i )
    {
        pObj = Gia_ManCo( p->pGia, Entry );
        Vec_IntPush( p->vStack, iFrame );
        Vec_IntPush( p->vStack, Gia_ObjId(p->pGia, pObj) );
    }
    // construct unfolded AIG
    while ( Vec_IntSize(p->vStack) > 0 )
    {
        int iObj   = Vec_IntPop(p->vStack);
        int iFrame = Vec_IntPop(p->vStack);
        if ( Int2_ManCheckFrames(p, iFrame, iObj) >= 0 )
            continue;
        pObj = Gia_ManObj( p->pGia, iObj );
        if ( Gia_ObjIsPi(p->pGia, pObj) )
            Int2_ManWriteFrames( p, iFrame, iObj, Gia_ManAppendCi(p->pFrames) );
        else if ( iFrame == 0 && Gia_ObjIsRo(p->pGia, iObj) )
            Int2_ManWriteFrames( p, iFrame, iObj, 0 );
        else if ( Gia_ObjIsRo(p->pGia, iObj) )
        {
            int iObjF = Gia_ObjId( p->pGia, Gia_ObjRoToRi(p->pGia, pObj) );
            int iLit = Int2_ManCheckFrames( p, iFrame-1, iObjF );
            if ( iLit >= 0 )
                Int2_ManWriteFrames( p, iFrame, iObj, iLit );
            else
            {
                Vec_IntPush( p->vStack, iFrame );
                Vec_IntPush( p->vStack, iObj );
                Vec_IntPush( p->vStack, iFrame-1 );
                Vec_IntPush( p->vStack, iObjF );
            }
        }
        else if ( Gia_ObjIsCo(pObj) )
        {
            int iObjF = Gia_ObjFaninId0(p->pGia, iObj) );
            int iLit = Int2_ManCheckFrames( p, iFrame, iObjF );
            if ( iLit >= 0 )
                Int2_ManWriteFrames( p, iFrame, iObj, Abc_LitNotCond(iLit, Gia_ObjFaninC0(pObj)) );
            else
            {
                Vec_IntPush( p->vStack, iFrame );
                Vec_IntPush( p->vStack, iObj );
                Vec_IntPush( p->vStack, iFrame );
                Vec_IntPush( p->vStack, iObjF );
            }
        }
        else if ( Gia_ObjIsAnd(pObj) )
        {
            int iObjF0 = Gia_ObjFaninId0(p->pGia, iObj) );
            int iLit0 = Int2_ManCheckFrames( p, iFrame, iObjF0 );
            int iObjF1 = Gia_ObjFaninId1(p->pGia, iObj) );
            int iLit1 = Int2_ManCheckFrames( p, iFrame, iObjF1 );
            if ( iLit0 >= 0 && iLit1 >= 0 )
            {
                Entry = Gia_ManObjNum(pFrames);
                iLit = Gia_ManHashAnd(pFrames, iLit0, iLit1);
                Int2_ManWriteFrames( p, iFrame, iObj, iLit );
                if ( Entry < Gia_ManObjNum(pFrames) )
                {
                    assert( !Abc_LitIsCompl(iLit) );
296
                    sat_solver_add_and( p->pGiaPref, Abc_Lit2Var(iLit), Abc_Lit2Var(iLit0), Abc_Lit2Var(iLit1), Abc_LitIsCompl(iLit0), Abc_LitIsCompl(iLit1), 0 ); 
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
                }
            }
            else
            {
                Vec_IntPush( p->vStack, iFrame );
                Vec_IntPush( p->vStack, iObj );
                if ( iLit0 < 0 )
                {
                    Vec_IntPush( p->vStack, iFrame );
                    Vec_IntPush( p->vStack, iObjF0 );
                }
                if ( iLit1 < 0 )
                {
                    Vec_IntPush( p->vStack, iFrame );
                    Vec_IntPush( p->vStack, iObjF1 );
                }
            }
        }
        else assert( 0 );
    }
}
int Int2_ManCheckBmc( Int2_Man_t * p, Vec_Int_t * vCube )
{
    int status;
    if ( vCube == NULL )
    {
        Gia_Obj_t * pObj;
        int i, iLit;
        Gia_ManForEachPo( p->pGia, pObj, i )
        {
            iLit = Int2_ManCheckFrames( p, 0, Gia_ObjId(p->pGia, pObj) );
            if ( iLit == 0 )
                continue;
            if ( iLit == 1 )
                return 0;
            status = sat_solver_solve( p->pSatPref, &iLit, &iLit + 1, 0, 0, 0, 0 );
            if ( status == l_False )
                continue;
            if ( status == l_True )
                return 0;
            return -1;
        }
        return 1;
    }
    status = sat_solver_solve( p->pSatPref, Vec_IntArray(vCube), Vec_IntArray(vCube) + Vec_IntSize(vCube), 0, 0, 0, 0 );
    if ( status == l_False )
        return 1;
    if ( status == l_True )
        return 0;
    return -1;
}

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


ABC_NAMESPACE_IMPL_END