llb2Core.c 27.9 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
/**CFile****************************************************************

  FileName    [llb2Core.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [BDD based reachability.]

  Synopsis    [Core procedure.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

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

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

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

#include "llbInt.h"

ABC_NAMESPACE_IMPL_START


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

typedef struct Llb_Img_t_ Llb_Img_t;
struct Llb_Img_t_
{
    Aig_Man_t *     pInit;          // AIG manager
    Aig_Man_t *     pAig;           // AIG manager
    Gia_ParLlb_t *  pPars;          // parameters

    DdManager *     dd;             // BDD manager
    DdManager *     ddG;            // BDD manager
    DdManager *     ddR;            // BDD manager
    Vec_Ptr_t *     vDdMans;        // BDD managers for each partition
    Vec_Ptr_t *     vRings;         // onion rings in ddR

    Vec_Int_t *     vDriRefs;       // driver references
    Vec_Int_t *     vVarsCs;        // cur state variables
    Vec_Int_t *     vVarsNs;        // next state variables

    Vec_Int_t *     vCs2Glo;        // cur state variables into global variables
    Vec_Int_t *     vNs2Glo;        // next state variables into global variables
    Vec_Int_t *     vGlo2Cs;        // global variables into cur state variables
    Vec_Int_t *     vGlo2Ns;        // global variables into next state variables
};

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

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

  Synopsis    [Computes cube composed of given variables with given values.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
DdNode * Llb_CoreComputeCube( DdManager * dd, Vec_Int_t * vVars, int fUseVarIndex, char * pValues )
{
    DdNode * bRes, * bVar, * bTemp;
71
    int i, iVar, Index;
72
    abctime TimeStop;
73
    TimeStop = dd->TimeStop; dd->TimeStop = 0;
74 75 76 77 78 79 80 81 82
    bRes = Cudd_ReadOne( dd );   Cudd_Ref( bRes );
    Vec_IntForEachEntry( vVars, Index, i )
    {
        iVar  = fUseVarIndex ? Index : i;
        bVar  = Cudd_NotCond( Cudd_bddIthVar(dd, iVar), (int)(pValues == NULL || pValues[i] != 1) );
        bRes  = Cudd_bddAnd( dd, bTemp = bRes, bVar );  Cudd_Ref( bRes );
        Cudd_RecursiveDeref( dd, bTemp );
    }
    Cudd_Deref( bRes );
83
    dd->TimeStop = TimeStop;
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
    return bRes;
}

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

  Synopsis    [Derives counter-example by backward reachability.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Abc_Cex_t * Llb_CoreDeriveCex( Llb_Img_t * p )
{
    Abc_Cex_t * pCex;
    Aig_Obj_t * pObj;
    Vec_Ptr_t * vSupps, * vQuant0, * vQuant1;
103
    DdNode * bState = NULL, * bImage, * bOneCube, * bTemp, * bRing;
104 105 106 107
    int i, v, RetValue, nPiOffset;
    char * pValues = ABC_ALLOC( char, Cudd_ReadSize(p->ddR) );
    assert( Vec_PtrSize(p->vRings) > 0 );

108 109 110
    p->dd->TimeStop  = 0;
    p->ddR->TimeStop = 0;

111 112 113 114 115 116 117 118 119
    // get supports and quantified variables
    Vec_PtrReverseOrder( p->vDdMans );
    vSupps = Llb_ImgSupports( p->pAig, p->vDdMans, p->vVarsNs, p->vVarsCs, 1, 0 );
    Llb_ImgSchedule( vSupps, &vQuant0, &vQuant1, 0 );
    Vec_VecFree( (Vec_Vec_t *)vSupps );
    Llb_ImgQuantifyReset( p->vDdMans );
//    Llb_ImgQuantifyFirst( p->pAig, p->vDdMans, vQuant0 );

    // allocate room for the counter-example
120
    pCex = Abc_CexAlloc( Saig_ManRegNum(p->pAig), Saig_ManPiNum(p->pAig), Vec_PtrSize(p->vRings) );
121 122 123 124
    pCex->iFrame = Vec_PtrSize(p->vRings) - 1;
    pCex->iPo = -1;

    // get the last cube
125
    bOneCube = Cudd_bddIntersect( p->ddR, (DdNode *)Vec_PtrEntryLast(p->vRings), p->ddR->bFunc );  Cudd_Ref( bOneCube );
126 127 128 129 130 131 132 133
    RetValue = Cudd_bddPickOneCube( p->ddR, bOneCube, pValues );
    Cudd_RecursiveDeref( p->ddR, bOneCube );
    assert( RetValue );

    // write PIs of counter-example
    nPiOffset = Saig_ManRegNum(p->pAig) + Saig_ManPiNum(p->pAig) * (Vec_PtrSize(p->vRings) - 1);
    Saig_ManForEachPi( p->pAig, pObj, i )
        if ( pValues[Saig_ManRegNum(p->pAig)+i] == 1 )
134
            Abc_InfoSetBit( pCex->pData, nPiOffset + i );
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

    // write state in terms of NS variables
    if ( Vec_PtrSize(p->vRings) > 1 )
    {
        bState = Llb_CoreComputeCube( p->dd, p->vVarsNs, 1, pValues );   Cudd_Ref( bState );
    }
    // perform backward analysis
    Vec_PtrForEachEntryReverse( DdNode *, p->vRings, bRing, v )
    { 
        if ( v == Vec_PtrSize(p->vRings) - 1 )
            continue;
        // compute the next states
        bImage = Llb_ImgComputeImage( p->pAig, p->vDdMans, p->dd, bState, 
            vQuant0, vQuant1, p->vDriRefs, p->pPars->TimeTarget, 1, 0, 0 );
        assert( bImage != NULL );
        Cudd_Ref( bImage );
        Cudd_RecursiveDeref( p->dd, bState );
//Extra_bddPrintSupport( p->dd, bImage ); printf( "\n" );

        // move reached states into ring manager
        bImage = Extra_TransferPermute( p->dd, p->ddR, bTemp = bImage, Vec_IntArray(p->vCs2Glo) );    Cudd_Ref( bImage );
        Cudd_RecursiveDeref( p->dd, bTemp );

        // intersect with the previous set
        bOneCube = Cudd_bddIntersect( p->ddR, bImage, bRing );                Cudd_Ref( bOneCube );
        Cudd_RecursiveDeref( p->ddR, bImage );

        // find any assignment of the BDD
        RetValue = Cudd_bddPickOneCube( p->ddR, bOneCube, pValues );
        Cudd_RecursiveDeref( p->ddR, bOneCube );
        assert( RetValue );

        // write PIs of counter-example
        nPiOffset -= Saig_ManPiNum(p->pAig);
        Saig_ManForEachPi( p->pAig, pObj, i )
            if ( pValues[Saig_ManRegNum(p->pAig)+i] == 1 )
171
                Abc_InfoSetBit( pCex->pData, nPiOffset + i );
172 173 174 175 176 177 178 179 180 181 182 183 184 185

        // check that we get the init state
        if ( v == 0 )
        {
            Saig_ManForEachLo( p->pAig, pObj, i )
                assert( pValues[i] == 0 );
            break;
        }

        // write state in terms of NS variables
        bState = Llb_CoreComputeCube( p->dd, p->vVarsNs, 1, pValues );   Cudd_Ref( bState );
    }
    assert( nPiOffset == Saig_ManRegNum(p->pAig) );
    // update the output number
186
    RetValue = Saig_ManFindFailedPoCex( p->pInit, pCex );
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
    assert( RetValue >= 0 && RetValue < Saig_ManPoNum(p->pInit) ); // invalid CEX!!!
    pCex->iPo = RetValue;
    // cleanup
    ABC_FREE( pValues );
    Vec_VecFree( (Vec_Vec_t *)vQuant0 );
    Vec_VecFree( (Vec_Vec_t *)vQuant1 );
    return pCex;
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Llb_CoreReachability_int( Llb_Img_t * p, Vec_Ptr_t * vQuant0, Vec_Ptr_t * vQuant1 )
{
    int * pLoc2Glo  = p->pPars->fBackward? Vec_IntArray( p->vCs2Glo ) : Vec_IntArray( p->vNs2Glo );
    int * pLoc2GloR = p->pPars->fBackward? Vec_IntArray( p->vNs2Glo ) : Vec_IntArray( p->vCs2Glo );
    int * pGlo2Loc  = p->pPars->fBackward? Vec_IntArray( p->vGlo2Ns ) : Vec_IntArray( p->vGlo2Cs );
    DdNode * bCurrent, * bReached, * bNext, * bTemp;
213
    abctime clk2, clk = Abc_Clock();
214
    int nIters, nBddSize;//, iOutFail = -1;
215
/*
216 217
    // compute time to stop
    if ( p->pPars->TimeLimit )
218
        p->pPars->TimeTarget = Abc_Clock() + p->pPars->TimeLimit * CLOCKS_PER_SEC;
219 220
    else
        p->pPars->TimeTarget = 0;
221 222
*/

223
    if ( Abc_Clock() > p->pPars->TimeTarget )
224 225 226
    {
        if ( !p->pPars->fSilent )
            printf( "Reached timeout (%d seconds) before image computation.\n", p->pPars->TimeLimit );
227
        p->pPars->iFrame = -1;
228 229 230 231 232 233 234
        return -1;
    }

    // set the stop time parameter
    p->dd->TimeStop  = p->pPars->TimeTarget;
    p->ddG->TimeStop = p->pPars->TimeTarget;
    p->ddR->TimeStop = p->pPars->TimeTarget;
235 236 237 238

    // compute initial states
    if ( p->pPars->fBackward )
    {
239
        // create init state in the global manager
240
        bTemp = Llb_BddComputeBad( p->pInit, p->ddR, p->pPars->TimeTarget );   
241 242 243
        if ( bTemp == NULL )
        {
            if ( !p->pPars->fSilent )
244
                printf( "Reached timeout (%d seconds) while computing bad states.\n", p->pPars->TimeLimit );
245
            p->pPars->iFrame = -1;
246 247 248
            return -1;
        }
        Cudd_Ref( bTemp );
249 250 251 252 253 254 255
        // create bad state in the ring manager
        p->ddR->bFunc = Llb_CoreComputeCube( p->ddR, p->vVarsCs, 0, NULL );      Cudd_Ref( p->ddR->bFunc );
        bCurrent = Llb_BddQuantifyPis( p->pInit, p->ddR, bTemp );                Cudd_Ref( bCurrent );
        Cudd_RecursiveDeref( p->ddR, bTemp );
        bReached = Cudd_bddTransfer( p->ddR, p->ddG, bCurrent );                 Cudd_Ref( bReached );
        Cudd_RecursiveDeref( p->ddR, bCurrent );
        // move init state to the working manager
256 257 258 259 260 261
        bCurrent = Extra_TransferPermute( p->ddG, p->dd, bReached, pGlo2Loc );   
        if ( bCurrent == NULL )
        {
            Cudd_RecursiveDeref( p->ddG, bReached );
            if ( !p->pPars->fSilent )
                printf( "Reached timeout (%d seconds) during transfer 0.\n", p->pPars->TimeLimit );
262
            p->pPars->iFrame = -1;
263 264 265
            return -1;
        }
        Cudd_Ref( bCurrent );
266 267 268 269
    }
    else
    {
        // create bad state in the ring manager
270
        p->ddR->bFunc = Llb_BddComputeBad( p->pInit, p->ddR, p->pPars->TimeTarget );  
271 272 273
        if ( p->ddR->bFunc == NULL )
        {
            if ( !p->pPars->fSilent )
274
                printf( "Reached timeout (%d seconds) while computing bad states.\n", p->pPars->TimeLimit );
275
            p->pPars->iFrame = -1;
276 277 278
            return -1;
        }
        Cudd_Ref( p->ddR->bFunc );
279 280 281 282 283 284 285 286 287 288
        // create init state in the working and global manager
        bCurrent = Llb_CoreComputeCube( p->dd,  p->vVarsCs, 1, NULL );           Cudd_Ref( bCurrent );
        bReached = Llb_CoreComputeCube( p->ddG, p->vVarsCs, 0, NULL );           Cudd_Ref( bReached );
//Extra_bddPrint( p->dd, bCurrent );  printf( "\n" );
//Extra_bddPrint( p->ddG, bReached );  printf( "\n" );
    }

    // compute onion rings
    for ( nIters = 0; nIters < p->pPars->nIterMax; nIters++ )
    { 
289
        clk2 = Abc_Clock();
290
        // check the runtime limit
291
        if ( p->pPars->TimeLimit && Abc_Clock() > p->pPars->TimeTarget )
292 293
        {
            if ( !p->pPars->fSilent )
294
                printf( "Reached timeout (%d seconds) during image computation.\n",  p->pPars->TimeLimit );
295 296 297 298 299 300 301
            p->pPars->iFrame = nIters - 1;
            Cudd_RecursiveDeref( p->dd,  bCurrent );  bCurrent = NULL;
            Cudd_RecursiveDeref( p->ddG, bReached );  bReached = NULL;
            return -1;
        }

        // save the onion ring
302 303 304 305 306 307 308 309 310 311 312
        bTemp = Extra_TransferPermute( p->dd, p->ddR, bCurrent, pLoc2GloR );  
        if ( bTemp == NULL )
        {
            if ( !p->pPars->fSilent )
                printf( "Reached timeout (%d seconds) during image computation.\n",  p->pPars->TimeLimit );
            p->pPars->iFrame = nIters - 1;
            Cudd_RecursiveDeref( p->dd,  bCurrent );  bCurrent = NULL;
            Cudd_RecursiveDeref( p->ddG, bReached );  bReached = NULL;
            return -1;
        }
        Cudd_Ref( bTemp );
313 314 315 316 317 318 319 320 321 322 323 324 325
        Vec_PtrPush( p->vRings, bTemp );

        // check it for bad states
        if ( !p->pPars->fSkipOutCheck && !Cudd_bddLeq( p->ddR, bTemp, Cudd_Not(p->ddR->bFunc) ) ) 
        {
            assert( p->pInit->pSeqModel == NULL );
            if ( !p->pPars->fBackward )
                p->pInit->pSeqModel = Llb_CoreDeriveCex( p ); 
            Cudd_RecursiveDeref( p->dd,  bCurrent );  bCurrent = NULL;
            Cudd_RecursiveDeref( p->ddG, bReached );  bReached = NULL;
            if ( !p->pPars->fSilent )
            {
                if ( !p->pPars->fBackward )
326
                    Abc_Print( 1, "Output %d of miter \"%s\" was asserted in frame %d.  ", p->pInit->pSeqModel->iPo, p->pInit->pName, nIters );
327
                else
328
                    Abc_Print( 1, "Output ??? was asserted in frame %d (counter-example is not produced).  ", nIters );
329
                Abc_PrintTime( 1, "Time", Abc_Clock() - clk );
330
            }
331
            p->pPars->iFrame = nIters - 1;
332 333 334 335 336 337 338 339 340 341
            return 0;
        }

        // compute the next states
        bNext = Llb_ImgComputeImage( p->pAig, p->vDdMans, p->dd, bCurrent, 
            vQuant0, vQuant1, p->vDriRefs, p->pPars->TimeTarget, 
            p->pPars->fBackward, p->pPars->fReorder, p->pPars->fVeryVerbose );
        if ( bNext == NULL )
        {
            if ( !p->pPars->fSilent )
342
                printf( "Reached timeout (%d seconds) during image computation.\n",  p->pPars->TimeLimit );
343 344 345 346 347 348 349 350 351 352
            p->pPars->iFrame = nIters - 1;
            Cudd_RecursiveDeref( p->dd,  bCurrent );   bCurrent = NULL;
            Cudd_RecursiveDeref( p->ddG, bReached );   bReached = NULL;
            return -1;
        }
        Cudd_Ref( bNext );
        Cudd_RecursiveDeref( p->dd, bCurrent );        bCurrent = NULL;
//Extra_bddPrintSupport( p->dd, bNext ); printf( "\n" );

        // remap these states into the global manager
353 354
//        bNext = Extra_TransferPermute( p->dd, p->ddG, bTemp = bNext, pLoc2Glo );    Cudd_Ref( bNext );
//        Cudd_RecursiveDeref( p->dd, bTemp );
355

356 357
//        bNext = Extra_TransferPermuteTime( p->dd, p->ddG, bTemp = bNext, pLoc2Glo, p->pPars->TimeTarget );    
        bNext = Extra_TransferPermute( p->dd, p->ddG, bTemp = bNext, pLoc2Glo );    
358 359 360 361 362 363 364 365 366 367 368 369 370
        if ( bNext == NULL )
        {
            if ( !p->pPars->fSilent )
                printf( "Reached timeout (%d seconds) during image computation in transfer 1.\n",  p->pPars->TimeLimit );
            p->pPars->iFrame = nIters - 1;
            Cudd_RecursiveDeref( p->dd,  bTemp );  
            Cudd_RecursiveDeref( p->ddG, bReached );   bReached = NULL;
            return -1;
        }
        Cudd_Ref( bNext );
        Cudd_RecursiveDeref( p->dd, bTemp );  

        nBddSize = Cudd_DagSize(bNext);
371 372 373 374 375 376 377 378
        // check if there are any new states
        if ( Cudd_bddLeq( p->ddG, bNext, bReached ) ) // implication = no new states
        {
            Cudd_RecursiveDeref( p->ddG,  bNext );     bNext = NULL;
            break;
        }

        // get the new states
379 380 381 382 383 384 385 386 387 388 389
        bCurrent = Cudd_bddAnd( p->ddG, bNext, Cudd_Not(bReached) );                    
        if ( bCurrent == NULL )
        {
            if ( !p->pPars->fSilent )
                printf( "Reached timeout (%d seconds) during image computation in transfer 2.\n",  p->pPars->TimeLimit );
            p->pPars->iFrame = nIters - 1;
            Cudd_RecursiveDeref( p->ddG, bNext );  
            Cudd_RecursiveDeref( p->ddG, bReached );   bReached = NULL;
            return -1;
        }
        Cudd_Ref( bCurrent );
390

391
        // remap these states into the current state vars
392 393 394
//        bCurrent = Extra_TransferPermute( p->ddG, p->dd, bTemp = bCurrent, pGlo2Loc );   Cudd_Ref( bCurrent );
//        Cudd_RecursiveDeref( p->ddG, bTemp );

395 396
//        bCurrent = Extra_TransferPermuteTime( p->ddG, p->dd, bTemp = bCurrent, pGlo2Loc, p->pPars->TimeTarget );    
        bCurrent = Extra_TransferPermute( p->ddG, p->dd, bTemp = bCurrent, pGlo2Loc );    
397 398 399 400 401 402 403 404 405 406
        if ( bCurrent == NULL )
        {
            if ( !p->pPars->fSilent )
                printf( "Reached timeout (%d seconds) during image computation in transfer 2.\n",  p->pPars->TimeLimit );
            p->pPars->iFrame = nIters - 1;
            Cudd_RecursiveDeref( p->ddG, bTemp );  
            Cudd_RecursiveDeref( p->ddG, bReached );   bReached = NULL;
            return -1;
        }
        Cudd_Ref( bCurrent );
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
        Cudd_RecursiveDeref( p->ddG, bTemp );

        // add to the reached states
        bReached = Cudd_bddOr( p->ddG, bTemp = bReached, bNext );                       Cudd_Ref( bReached );
        Cudd_RecursiveDeref( p->ddG, bTemp );
        Cudd_RecursiveDeref( p->ddG, bNext );
        bNext = NULL;

        if ( p->pPars->fVeryVerbose )
        {
            double nMints = Cudd_CountMinterm(p->ddG, bReached, Saig_ManRegNum(p->pAig) );
//            Extra_bddPrint( p->ddG, bReached );printf( "\n" );
            fprintf( stdout, "        Reachable states = %.0f. (Ratio = %.4f %%)\n", nMints, 100.0*nMints/pow(2.0, Saig_ManRegNum(p->pAig)) );
            fflush( stdout ); 
        }
        if ( p->pPars->fVerbose )
        {
            fprintf( stdout, "F =%3d : ",    nIters );
            fprintf( stdout, "Image =%6d  ", nBddSize );
            fprintf( stdout, "(%4d%4d)  ", 
                Cudd_ReadReorderings(p->dd),  Cudd_ReadGarbageCollections(p->dd) );
            fprintf( stdout, "Reach =%6d  ", Cudd_DagSize(bReached) );
            fprintf( stdout, "(%4d%4d)  ", 
                Cudd_ReadReorderings(p->ddG), Cudd_ReadGarbageCollections(p->ddG) );
431
            Abc_PrintTime( 1, "Time", Abc_Clock() - clk2 );
432 433 434 435 436 437 438 439 440 441 442 443 444 445
        }

        // check timeframe limit
        if ( nIters == p->pPars->nIterMax - 1 )
        {
            if ( !p->pPars->fSilent )
                printf( "Reached limit on the number of timeframes (%d).\n",  p->pPars->nIterMax );
            p->pPars->iFrame = nIters;
            Cudd_RecursiveDeref( p->dd,  bCurrent );  bCurrent = NULL;
            Cudd_RecursiveDeref( p->ddG, bReached );  bReached = NULL;
            return -1;
        }
    }
    if ( bReached == NULL )
446 447
    {
        p->pPars->iFrame = nIters - 1;
448
        return 0; // reachable
449
    }
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
    if ( bCurrent )
        Cudd_RecursiveDeref( p->dd, bCurrent );
    // report the stats
    if ( p->pPars->fVerbose )
    {
        double nMints = Cudd_CountMinterm(p->ddG, bReached, Saig_ManRegNum(p->pAig) );
        if ( nIters >= p->pPars->nIterMax )
            fprintf( stdout, "Reachability analysis is stopped after %d frames.\n", nIters );
        else
            fprintf( stdout, "Reachability analysis completed after %d frames.\n", nIters );
        fprintf( stdout, "Reachable states = %.0f. (Ratio = %.4f %%)\n", nMints, 100.0*nMints/pow(2.0, Saig_ManRegNum(p->pAig)) );
        fflush( stdout ); 
    }
    if ( p->pPars->fDumpReached )
    {
        Llb_ManDumpReached( p->ddG, bReached, p->pAig->pName, "reached.blif" );
        printf( "Reached states with %d BDD nodes are dumpted into file \"reached.blif\".\n", Cudd_DagSize(bReached) );
    }
    Cudd_RecursiveDeref( p->ddG, bReached );
    if ( nIters >= p->pPars->nIterMax )
    {
        if ( !p->pPars->fSilent )
        {
            printf( "Verified only for states reachable in %d frames.  ", nIters );
474
            Abc_PrintTime( 1, "Time", Abc_Clock() - clk );
475
        }
476
        p->pPars->iFrame = p->pPars->nIterMax;
477 478 479 480 481
        return -1; // undecided
    }
    if ( !p->pPars->fSilent )
    {
        printf( "The miter is proved unreachable after %d iterations.  ", nIters );
482
        Abc_PrintTime( 1, "Time", Abc_Clock() - clk );
483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533
    }
    p->pPars->iFrame = nIters - 1;
    return 1; // unreachable
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Llb_CoreReachability( Llb_Img_t * p )
{
    Vec_Ptr_t * vSupps, * vQuant0, * vQuant1;
    int RetValue;
    // get supports and quantified variables
    if ( p->pPars->fBackward )
    {
        Vec_PtrReverseOrder( p->vDdMans );
        vSupps = Llb_ImgSupports( p->pAig, p->vDdMans, p->vVarsNs, p->vVarsCs, 0, p->pPars->fVeryVerbose );
    }
    else
        vSupps = Llb_ImgSupports( p->pAig, p->vDdMans, p->vVarsCs, p->vVarsNs, 0, p->pPars->fVeryVerbose );
    Llb_ImgSchedule( vSupps, &vQuant0, &vQuant1, p->pPars->fVeryVerbose );
    Vec_VecFree( (Vec_Vec_t *)vSupps );
    // remove variables
    Llb_ImgQuantifyFirst( p->pAig, p->vDdMans, vQuant0, p->pPars->fVeryVerbose );
    // perform reachability
    RetValue = Llb_CoreReachability_int( p, vQuant0, vQuant1 );
    Vec_VecFree( (Vec_Vec_t *)vQuant0 );
    Vec_VecFree( (Vec_Vec_t *)vQuant1 );
    return RetValue;
}


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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
534
Vec_Ptr_t * Llb_CoreConstructAll( Aig_Man_t * p, Vec_Ptr_t * vResult, Vec_Int_t * vVarsNs, abctime TimeTarget )
535 536 537
{
    DdManager * dd;
    Vec_Ptr_t * vDdMans;
538
    Vec_Ptr_t * vLower, * vUpper = NULL;
539 540 541 542 543
    int i;
    vDdMans = Vec_PtrStart( Vec_PtrSize(vResult) );
    Vec_PtrForEachEntryReverse( Vec_Ptr_t *, vResult, vLower, i )
    {
        if ( i < Vec_PtrSize(vResult) - 1 )
544
            dd = Llb_ImgPartition( p, vLower, vUpper, TimeTarget );
545
        else
546 547 548 549 550 551 552 553 554 555 556 557 558 559
            dd = Llb_DriverLastPartition( p, vVarsNs, TimeTarget );
        if ( dd == NULL )
        {
            Vec_PtrForEachEntry( DdManager *, vDdMans, dd, i )
            {
                if ( dd == NULL )
                    continue;
                if ( dd->bFunc )
                    Cudd_RecursiveDeref( dd, dd->bFunc );
                Extra_StopManager( dd );
            }
            Vec_PtrFree( vDdMans );
            return NULL;
        }
560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626
        Vec_PtrWriteEntry( vDdMans, i, dd );
        vUpper = vLower;
    }
    return vDdMans;
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Llb_CoreSetVarMaps( Llb_Img_t * p )
{
    Aig_Obj_t * pObj;
    int i, iVarCs, iVarNs;
    assert( p->vVarsCs != NULL );
    assert( p->vVarsNs != NULL );
    assert( p->vCs2Glo == NULL );
    assert( p->vNs2Glo == NULL );
    assert( p->vGlo2Cs == NULL );
    assert( p->vGlo2Ns == NULL );
    p->vCs2Glo = Vec_IntStartFull( Aig_ManObjNumMax(p->pAig) );
    p->vNs2Glo = Vec_IntStartFull( Aig_ManObjNumMax(p->pAig) );
    p->vGlo2Cs = Vec_IntStartFull( Aig_ManRegNum(p->pAig) );
    p->vGlo2Ns = Vec_IntStartFull( Aig_ManRegNum(p->pAig) );
    for ( i = 0; i < Aig_ManRegNum(p->pAig); i++ )
    {
        iVarCs = Vec_IntEntry( p->vVarsCs, i );
        iVarNs = Vec_IntEntry( p->vVarsNs, i );
        assert( iVarCs >= 0 && iVarCs < Aig_ManObjNumMax(p->pAig) );
        assert( iVarNs >= 0 && iVarNs < Aig_ManObjNumMax(p->pAig) );
        Vec_IntWriteEntry( p->vCs2Glo, iVarCs, i );
        Vec_IntWriteEntry( p->vNs2Glo, iVarNs, i );
        Vec_IntWriteEntry( p->vGlo2Cs, i, iVarCs );
        Vec_IntWriteEntry( p->vGlo2Ns, i, iVarNs );
    }
    // add mapping of the PIs
    Saig_ManForEachPi( p->pAig, pObj, i )
        Vec_IntWriteEntry( p->vCs2Glo, Aig_ObjId(pObj), Aig_ManRegNum(p->pAig)+i );
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Llb_Img_t * Llb_CoreStart( Aig_Man_t * pInit, Aig_Man_t * pAig, Gia_ParLlb_t *  pPars )
{
    Llb_Img_t * p;
    p = ABC_CALLOC( Llb_Img_t, 1 );
    p->pInit = pInit;
    p->pAig  = pAig;
    p->pPars = pPars;
    p->dd    = Cudd_Init( Aig_ManObjNumMax(pAig), 0, CUDD_UNIQUE_SLOTS, CUDD_CACHE_SLOTS, 0 );
    p->ddG   = Cudd_Init( Aig_ManRegNum(pAig),    0, CUDD_UNIQUE_SLOTS, CUDD_CACHE_SLOTS, 0 );
627
    p->ddR   = Cudd_Init( Aig_ManCiNum(pAig),     0, CUDD_UNIQUE_SLOTS, CUDD_CACHE_SLOTS, 0 );
628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654
    Cudd_AutodynEnable( p->dd,  CUDD_REORDER_SYMM_SIFT );
    Cudd_AutodynEnable( p->ddG, CUDD_REORDER_SYMM_SIFT );
    Cudd_AutodynEnable( p->ddR, CUDD_REORDER_SYMM_SIFT );
    p->vRings = Vec_PtrAlloc( 100 );
    p->vDriRefs = Llb_DriverCountRefs( pAig );
    p->vVarsCs  = Llb_DriverCollectCs( pAig );
    p->vVarsNs  = Llb_DriverCollectNs( pAig, p->vDriRefs );
    Llb_CoreSetVarMaps( p );
    return p;
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Llb_CoreStop( Llb_Img_t * p )
{
    DdManager * dd;
    DdNode * bTemp;
    int i;
655
    if ( p->vDdMans )
656 657 658 659 660 661 662 663
    Vec_PtrForEachEntry( DdManager *, p->vDdMans, dd, i )
    {
        if ( dd->bFunc )
            Cudd_RecursiveDeref( dd, dd->bFunc );
        if ( dd->bFunc2 )
            Cudd_RecursiveDeref( dd, dd->bFunc2 );
        Extra_StopManager( dd );
    }
664
    Vec_PtrFreeP( &p->vDdMans );
665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693
    if ( p->ddR->bFunc )
        Cudd_RecursiveDeref( p->ddR, p->ddR->bFunc );
    Vec_PtrForEachEntry( DdNode *, p->vRings, bTemp, i )
        Cudd_RecursiveDeref( p->ddR, bTemp );
    Vec_PtrFree( p->vRings );
    Extra_StopManager( p->dd );
    Extra_StopManager( p->ddG );
    Extra_StopManager( p->ddR );
    Vec_IntFreeP( &p->vDriRefs );
    Vec_IntFreeP( &p->vVarsCs );
    Vec_IntFreeP( &p->vVarsNs );
    Vec_IntFreeP( &p->vCs2Glo );
    Vec_IntFreeP( &p->vNs2Glo );
    Vec_IntFreeP( &p->vGlo2Cs );
    Vec_IntFreeP( &p->vGlo2Ns );
    ABC_FREE( p );
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
694
int Llb_CoreExperiment( Aig_Man_t * pInit, Aig_Man_t * pAig, Gia_ParLlb_t * pPars, Vec_Ptr_t * vResult, abctime TimeTarget )
695 696 697 698 699 700
{
    int RetValue;
    Llb_Img_t * p;
//    printf( "\n" );
//    pPars->fVerbose = 1;
    p = Llb_CoreStart( pInit, pAig, pPars );
701 702 703 704 705 706 707 708
    p->vDdMans = Llb_CoreConstructAll( pAig, vResult, p->vVarsNs, TimeTarget );
    if ( p->vDdMans == NULL )
    {
        if ( !pPars->fSilent )
            printf( "Reached timeout (%d seconds) while deriving the partitions.\n", pPars->TimeLimit );
        Llb_CoreStop( p );
        return -1;
    }
709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730
    RetValue = Llb_CoreReachability( p );
    Llb_CoreStop( p );
    return RetValue;
}

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

  Synopsis    [Finds balanced cut.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Llb_ManReachMinCut( Aig_Man_t * pAig, Gia_ParLlb_t * pPars )
{
    extern Vec_Ptr_t * Llb_ManComputeCuts( Aig_Man_t * p, int Num, int fVerbose, int fVeryVerbose );
    Vec_Ptr_t * vResult;
    Aig_Man_t * p;
    int RetValue = -1;
731
    abctime clk = Abc_Clock();
732 733

    // compute time to stop
734
    pPars->TimeTarget = pPars->TimeLimit ? pPars->TimeLimit * CLOCKS_PER_SEC + Abc_Clock(): 0;
735 736 737 738 739 740 741 742 743 744

    p = Aig_ManDupFlopsOnly( pAig );
//Aig_ManShow( p, 0, NULL );
    if ( pPars->fVerbose )
    Aig_ManPrintStats( pAig );
    if ( pPars->fVerbose )
    Aig_ManPrintStats( p );
    Aig_ManFanoutStart( p );

    vResult = Llb_ManComputeCuts( p, pPars->nPartValue, pPars->fVerbose, pPars->fVeryVerbose );
745

746
    if ( pPars->TimeLimit && Abc_Clock() > pPars->TimeTarget )
747 748 749 750 751 752 753 754 755 756 757
    {
        if ( !pPars->fSilent )
            printf( "Reached timeout (%d seconds) after partitioning.\n", pPars->TimeLimit );

        Vec_VecFree( (Vec_Vec_t *)vResult );
        Aig_ManFanoutStop( p );
        Aig_ManCleanMarkAB( p );
        Aig_ManStop( p );
        return RetValue;
    }

758
    if ( !pPars->fSkipReach )
759
        RetValue = Llb_CoreExperiment( pAig, p, pPars, vResult, pPars->TimeTarget );
760

761
    Vec_VecFree( (Vec_Vec_t *)vResult );
762 763 764
    Aig_ManFanoutStop( p );
    Aig_ManCleanMarkAB( p );
    Aig_ManStop( p );
765 766

    if ( RetValue == -1 )
767
        Abc_PrintTime( 1, "Total runtime of the min-cut-based reachability engine", Abc_Clock() - clk );
768 769 770 771 772 773 774 775 776 777
    return RetValue;
}

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


ABC_NAMESPACE_IMPL_END