bmcChain.c 13.1 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
/**CFile****************************************************************

  FileName    [bmcChain.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [SAT-based bounded model checking.]

  Synopsis    [Implementation of chain BMC.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

  Date        [Ver. 1.0. Started - June 20, 2005.]

  Revision    [$Id: bmcChain.c,v 1.00 2005/06/20 00:00:00 alanmi Exp $]

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

#include "bmc.h"
#include "aig/gia/giaAig.h"
#include "sat/cnf/cnf.h"
#include "sat/bsat/satSolver.h"

ABC_NAMESPACE_IMPL_START


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

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

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

  Synopsis    [Find the first failure.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Abc_Cex_t * Bmc_ChainFailOneOutput( Gia_Man_t * p, int nFrameMax, int nConfMax, int fVerbose, int fVeryVerbose )
{
    int RetValue;
    Abc_Cex_t * pCex = NULL;
    Aig_Man_t * pAig = Gia_ManToAigSimple( p );
    Saig_ParBmc_t Pars, * pPars = &Pars;
    Saig_ParBmcSetDefaultParams( pPars );
    pPars->nFramesMax = nFrameMax;
    pPars->nConfLimit = nConfMax;
    pPars->fVerbose   = fVeryVerbose;
    RetValue = Saig_ManBmcScalable( pAig, pPars );
    if ( RetValue == 0 ) // SAT
    {
        pCex = pAig->pSeqModel, pAig->pSeqModel = NULL;
        if ( fVeryVerbose )
            Abc_Print( 1, "Output %d of miter \"%s\" was asserted in frame %d.\n", pCex->iPo, p->pName, pCex->iFrame );
    }
    else if ( fVeryVerbose )
        Abc_Print( 1, "No output asserted in %d frames. Resource limit reached.\n", pPars->iFrame+2 );
    Aig_ManStop( pAig );
    return pCex;
}

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

  Synopsis    [Move GIA into the failing state.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Gia_Man_t * Gia_ManDupWithInit( Gia_Man_t * p )
{
    Gia_Man_t * pNew;
    Gia_Obj_t * pObj;
    int i;
    pNew = Gia_ManStart( Gia_ManObjNum(p) );
    pNew->pName = Abc_UtilStrsav( p->pName );
    pNew->pSpec = Abc_UtilStrsav( p->pSpec );
    Gia_ManConst0(p)->Value = 0;
    Gia_ManForEachObj1( p, pObj, i )
    {
        if ( Gia_ObjIsAnd(pObj) )
            pObj->Value = Gia_ManAppendAnd( pNew, Gia_ObjFanin0Copy(pObj), Gia_ObjFanin1Copy(pObj) );
        else if ( Gia_ObjIsCi(pObj) )
        {
            pObj->Value = Gia_ManAppendCi( pNew );
            pObj->Value = Abc_LitNotCond( pObj->Value, pObj->fMark0 );
        }
        else if ( Gia_ObjIsCo(pObj) )
        {
            pObj->Value = Gia_ObjFanin0Copy(pObj);
            pObj->Value = Abc_LitNotCond( pObj->Value, pObj->fMark0 );
            pObj->Value = Gia_ManAppendCo( pNew, pObj->Value );
        }
    }
    Gia_ManSetRegNum( pNew, Gia_ManRegNum(p) );
    return pNew;
}
Gia_Man_t * Gia_ManVerifyCexAndMove( Gia_Man_t * pGia, Abc_Cex_t * p )
{
    Gia_Man_t * pNew;
    Gia_Obj_t * pObj, * pObjRi, * pObjRo;
    int RetValue, i, k, iBit = 0;
    Gia_ManCleanMark0(pGia);
    Gia_ManForEachRo( pGia, pObj, i )
        pObj->fMark0 = Abc_InfoHasBit(p->pData, iBit++);
    for ( i = 0; i <= p->iFrame; i++ )
    {
        Gia_ManForEachPi( pGia, pObj, k )
            pObj->fMark0 = Abc_InfoHasBit(p->pData, iBit++);
        Gia_ManForEachAnd( pGia, pObj, k )
            pObj->fMark0 = (Gia_ObjFanin0(pObj)->fMark0 ^ Gia_ObjFaninC0(pObj)) & 
                           (Gia_ObjFanin1(pObj)->fMark0 ^ Gia_ObjFaninC1(pObj));
        Gia_ManForEachCo( pGia, pObj, k )
            pObj->fMark0 = Gia_ObjFanin0(pObj)->fMark0 ^ Gia_ObjFaninC0(pObj);
        if ( i == p->iFrame )
            break;
        Gia_ManForEachRiRo( pGia, pObjRi, pObjRo, k )
            pObjRo->fMark0 = pObjRi->fMark0;
    }
    assert( iBit == p->nBits );
    RetValue = Gia_ManPo(pGia, p->iPo)->fMark0;
    assert( RetValue );
    // set PI/PO values to zero and transfer RO values to RI
    Gia_ManForEachPi( pGia, pObj, k )
        pObj->fMark0 = 0;
    Gia_ManForEachPo( pGia, pObj, k )
        pObj->fMark0 = 0;
    Gia_ManForEachRiRo( pGia, pObjRi, pObjRo, k )
        pObjRi->fMark0 = pObjRo->fMark0;
    // duplicate assuming CI/CO marks are set correctly
    pNew = Gia_ManDupWithInit( pGia );
    Gia_ManCleanMark0(pGia);
    return pNew;
}

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

  Synopsis    [Find what outputs fail in this state.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Gia_Man_t * Gia_ManDupPosAndPropagateInit( Gia_Man_t * p )
{
    Gia_Man_t * pNew, * pTemp;
    Gia_Obj_t * pObj;  int i;
    pNew = Gia_ManStart( Gia_ManObjNum(p) );
    pNew->pName = Abc_UtilStrsav( p->pName );
    pNew->pSpec = Abc_UtilStrsav( p->pSpec );
    Gia_ManConst0(p)->Value = 0;
    Gia_ManHashAlloc( pNew );
    Gia_ManForEachObj1( p, pObj, i )
    {
        if ( Gia_ObjIsAnd(pObj) )
            pObj->Value = Gia_ManHashAnd( pNew, Gia_ObjFanin0Copy(pObj), Gia_ObjFanin1Copy(pObj) );
        else if ( Gia_ObjIsPi(p, pObj) )
            pObj->Value = Gia_ManAppendCi( pNew );
        else if ( Gia_ObjIsCi(pObj) )
            pObj->Value = 0;
        else if ( Gia_ObjIsPo(p, pObj) )
            Gia_ManAppendCo( pNew, Gia_ObjFanin0Copy(pObj) );
    }
    Gia_ManHashStop( pNew );
    assert( Gia_ManPiNum(p) == Gia_ManPiNum(pNew) );
    assert( Gia_ManPoNum(p) == Gia_ManPoNum(pNew) );
    pNew = Gia_ManCleanup( pTemp = pNew );
    Gia_ManStop( pTemp );
    return pNew;
}
186
sat_solver * Gia_ManDeriveSatSolver( Gia_Man_t * p, Vec_Int_t * vSatIds )
187 188 189
{
    sat_solver * pSat;
    Aig_Man_t * pAig = Gia_ManToAigSimple( p );
190
//    Cnf_Dat_t * pCnf = (Cnf_Dat_t *)Mf_ManGenerateCnf( p, 8, 0, 0, 0, 0 );
191
    Cnf_Dat_t * pCnf = Cnf_Derive( pAig, Aig_ManCoNum(pAig) );
192 193 194 195 196 197 198 199 200
    // save SAT vars for the primary inputs
    if ( vSatIds )
    {
        Aig_Obj_t * pObj; int i;
        Vec_IntClear( vSatIds );
        Aig_ManForEachCi( pAig, pObj, i )
            Vec_IntPush( vSatIds, pCnf->pVarNums[ Aig_ObjId(pObj) ] );
        assert( Vec_IntSize(vSatIds) == Gia_ManPiNum(p) );
    }
201 202 203 204 205 206
    Aig_ManStop( pAig );
    pSat = (sat_solver *)Cnf_DataWriteIntoSolver( pCnf, 1, 0 );
    Cnf_DataFree( pCnf );
    assert( p->nRegs == 0 );
    return pSat;
}
207
Vec_Int_t * Bmc_ChainFindFailedOutputs( Gia_Man_t * p, Vec_Ptr_t * vCexes )
208 209
{
    Vec_Int_t * vOutputs;
210
    Vec_Int_t * vSatIds;
211 212 213
    Gia_Man_t * pInit;
    Gia_Obj_t * pObj;
    sat_solver * pSat;
214
    int i, j, Lit, status = 0;
215 216 217
    // derive output logic cones
    pInit = Gia_ManDupPosAndPropagateInit( p );
    // derive SAT solver and test
218 219
    vSatIds = Vec_IntAlloc( Gia_ManPiNum(p) );
    pSat = Gia_ManDeriveSatSolver( pInit, vSatIds );
220 221 222 223 224 225 226 227
    vOutputs = Vec_IntAlloc( 100 );
    Gia_ManForEachPo( pInit, pObj, i )
    {
        if ( Gia_ObjFaninLit0p(pInit, pObj) == 0 )
            continue;
        Lit = Abc_Var2Lit( i+1, 0 );
        status = sat_solver_solve( pSat, &Lit, &Lit + 1, 0, 0, 0, 0 );
        if ( status == l_True )
228 229
        {
            // save the index of solved output
230
            Vec_IntPush( vOutputs, i );
231 232 233 234 235 236 237 238 239 240 241 242
            // create CEX for this output
            if ( vCexes )
            {
                Abc_Cex_t * pCex = Abc_CexAlloc( Gia_ManRegNum(p), Gia_ManPiNum(p), 1 );
                pCex->iFrame = 0;
                pCex->iPo = i;
                for ( j = 0; j < Gia_ManPiNum(p); j++ )
                    if ( sat_solver_var_value( pSat, Vec_IntEntry(vSatIds, j) ) )
                        Abc_InfoSetBit( pCex->pData, Gia_ManRegNum(p) + j );
                Vec_PtrPush( vCexes, pCex );
            }
        }
243 244 245
    }
    Gia_ManStop( pInit );
    sat_solver_delete( pSat );
246
    Vec_IntFree( vSatIds );
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 296 297 298
    return vOutputs;
}

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

  Synopsis    [Cleanup AIG by removing COs replacing failed out by const0.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline void Gia_ObjMakePoConst0( Gia_Man_t * p, Gia_Obj_t * pObj )
{ 
    assert( Gia_ObjIsCo(pObj) );
    pObj->iDiff0 = Gia_ObjId( p, pObj );
    pObj->fCompl0 = 0;
}
int Gia_ManCountNonConst0( Gia_Man_t * p )
{ 
    Gia_Obj_t * pObj;
    int i, Count = 0;
    Gia_ManForEachPo( p, pObj, i )
        Count += (Gia_ObjFaninLit0p(p, pObj) != 0);
    return Count;
}
Gia_Man_t * Bmc_ChainCleanup( Gia_Man_t * p, Vec_Int_t * vOutputs )
{
    int i, iOut;
    Vec_IntForEachEntry( vOutputs, iOut, i )
    {
        Gia_Obj_t * pObj = Gia_ManPo( p, iOut );
        assert( Gia_ObjFaninLit0p(p, pObj) != 0 );
        Gia_ObjMakePoConst0( p, pObj );
        assert( Gia_ObjFaninLit0p(p, pObj) == 0 );
    }
    return Gia_ManCleanup( p );
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
299
int Bmc_ChainTest( Gia_Man_t * p, int nFrameMax, int nConfMax, int fVerbose, int fVeryVerbose, Vec_Ptr_t ** pvCexes )
300 301 302 303 304
{
    int Iter, IterMax = 10000;
    Gia_Man_t * pTemp, * pNew = Gia_ManDup(p);
    Abc_Cex_t * pCex = NULL;
    Vec_Int_t * vOutputs;
305 306 307 308 309 310
    abctime clk2, clk = Abc_Clock(); 
    abctime clkBmc = 0;
    abctime clkMov = 0;
    abctime clkSat = 0;
    abctime clkCln = 0;
    abctime clkSwp = 0;
311
    int DepthTotal = 0;
312 313
    if ( pvCexes )
        *pvCexes = Vec_PtrAlloc( 1000 );
314 315 316 317 318 319 320 321 322
    for ( Iter = 0; Iter < IterMax; Iter++ )
    {
        if ( Gia_ManPoNum(pNew) == 0 )
        {
            if ( fVerbose )
                printf( "Finished all POs.\n" );
            break;
        }
        // run BMC till the first failure
323
        clk2 = Abc_Clock(); 
324
        pCex = Bmc_ChainFailOneOutput( pNew, nFrameMax, nConfMax, fVerbose, fVeryVerbose );
325
        clkBmc += Abc_Clock() - clk2;
326 327 328 329 330 331 332 333
        if ( pCex == NULL )
        {
            if ( fVerbose )
                printf( "BMC could not detect a failed output in %d frames with %d conflicts.\n", nFrameMax, nConfMax );
            break;
        }
        assert( !Iter || pCex->iFrame > 0 );
        // move the AIG to the failure state
334
        clk2 = Abc_Clock(); 
335 336 337
        pNew = Gia_ManVerifyCexAndMove( pTemp = pNew, pCex );
        Gia_ManStop( pTemp );
        DepthTotal += pCex->iFrame;
338
        clkMov += Abc_Clock() - clk2;
339
        // find outputs that fail in this state
340
        clk2 = Abc_Clock(); 
341
        vOutputs = Bmc_ChainFindFailedOutputs( pNew, pvCexes ? *pvCexes : NULL );
342
        assert( Vec_IntFind(vOutputs, pCex->iPo) >= 0 );
343 344 345
        // save the counter-example
        if ( pvCexes )
            Vec_PtrPush( *pvCexes, pCex );
346 347
        else
            Abc_CexFree( pCex );
348
        clkSat += Abc_Clock() - clk2;
349
        // remove them from the AIG 
350
        clk2 = Abc_Clock(); 
351 352
        pNew = Bmc_ChainCleanup( pTemp = pNew, vOutputs );
        Gia_ManStop( pTemp );
353
        clkCln += Abc_Clock() - clk2;
354
        // perform sequential cleanup
355
        clk2 = Abc_Clock(); 
356 357
//        pNew = Gia_ManSeqStructSweep( pTemp = pNew, 0, 1, 0 );
//        Gia_ManStop( pTemp );
358
        clkSwp += Abc_Clock() - clk2;
359 360 361 362 363
        // printout
        if ( fVerbose )
        {
            int nNonConst = Gia_ManCountNonConst0(pNew);
            printf( "Iter %4d : ",    Iter+1 );
364 365 366 367 368
            printf( "Depth =%5d  ",   DepthTotal );
            printf( "FailPo =%5d  ",  Vec_IntSize(vOutputs) );
            printf( "UndecPo =%5d ",  nNonConst );
            printf( "(%6.2f %%)  ",   100.0 * nNonConst / Gia_ManPoNum(pNew) );
            printf( "AIG =%8d  ",     Gia_ManAndNum(pNew) );
369 370 371 372 373 374
            Abc_PrintTime( 1, "Time", Abc_Clock() - clk );
        }
        Vec_IntFree( vOutputs );
    }
    printf( "Completed a CEX chain with %d segments, %d frames, and %d failed POs (out of %d). ", 
        Iter, DepthTotal, Gia_ManPoNum(pNew) - Gia_ManCountNonConst0(pNew), Gia_ManPoNum(pNew) );
375 376
    if ( fVerbose )
    {
377
    Abc_PrintTime( 1, "Time", Abc_Clock() - clk );
378 379 380 381 382 383
    Abc_PrintTimeP( 1, "BMC  ", clkBmc, Abc_Clock() - clk );
    Abc_PrintTimeP( 1, "Init ", clkMov, Abc_Clock() - clk );
    Abc_PrintTimeP( 1, "SAT  ", clkSat, Abc_Clock() - clk );
    Abc_PrintTimeP( 1, "Clean", clkCln, Abc_Clock() - clk );
//    Abc_PrintTimeP( 1, "Sweep", clkSwp, Abc_Clock() - clk );
    }
384
    Gia_ManStop( pNew );
385 386
    if ( pvCexes )
        printf( "Total number of CEXes collected = %d.\n", Vec_PtrSize(*pvCexes) );
387 388 389 390 391 392 393 394 395 396
    return 0;
}

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


ABC_NAMESPACE_IMPL_END