cecCorr.c 41.9 KB
Newer Older
Alan Mishchenko committed
1 2
/**CFile****************************************************************

Alan Mishchenko committed
3
  FileName    [cecCorr.c]
Alan Mishchenko committed
4 5 6

  SystemName  [ABC: Logic synthesis and verification system.]

Alan Mishchenko committed
7
  PackageName [Combinational equivalence checking.]
Alan Mishchenko committed
8

Alan Mishchenko committed
9
  Synopsis    [Latch/signal correspondence computation.]
Alan Mishchenko committed
10 11 12 13 14 15 16

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

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

Alan Mishchenko committed
17
  Revision    [$Id: cecCorr.c,v 1.00 2005/06/20 00:00:00 alanmi Exp $]
Alan Mishchenko committed
18 19 20 21 22

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

#include "cecInt.h"

23 24 25
ABC_NAMESPACE_IMPL_START


Alan Mishchenko committed
26 27 28 29
////////////////////////////////////////////////////////////////////////
///                        DECLARATIONS                              ///
////////////////////////////////////////////////////////////////////////

Alan Mishchenko committed
30
static void Gia_ManCorrSpecReduce_rec( Gia_Man_t * pNew, Gia_Man_t * p, Gia_Obj_t * pObj, int f, int nPrefix );
Alan Mishchenko committed
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46

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

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

  Synopsis    [Computes the real value of the literal w/o spec reduction.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
47
static inline int Gia_ManCorrSpecReal( Gia_Man_t * pNew, Gia_Man_t * p, Gia_Obj_t * pObj, int f, int nPrefix )
Alan Mishchenko committed
48 49 50
{
    if ( Gia_ObjIsAnd(pObj) )
    {
Alan Mishchenko committed
51 52
        Gia_ManCorrSpecReduce_rec( pNew, p, Gia_ObjFanin0(pObj), f, nPrefix );
        Gia_ManCorrSpecReduce_rec( pNew, p, Gia_ObjFanin1(pObj), f, nPrefix );
Alan Mishchenko committed
53 54
        return Gia_ManHashAnd( pNew, Gia_ObjFanin0CopyF(p, f, pObj), Gia_ObjFanin1CopyF(p, f, pObj) );
    }
Alan Mishchenko committed
55 56 57 58 59
    if ( f == 0 )
    {
        assert( Gia_ObjIsRo(p, pObj) );
        return Gia_ObjCopyF(p, f, pObj);
    }
Alan Mishchenko committed
60 61
    assert( f && Gia_ObjIsRo(p, pObj) );
    pObj = Gia_ObjRoToRi( p, pObj );
Alan Mishchenko committed
62
    Gia_ManCorrSpecReduce_rec( pNew, p, Gia_ObjFanin0(pObj), f-1, nPrefix );
Alan Mishchenko committed
63 64 65 66 67 68 69 70 71 72 73 74 75 76
    return Gia_ObjFanin0CopyF( p, f-1, pObj );
}

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

  Synopsis    [Recursively performs speculative reduction for the object.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
77
void Gia_ManCorrSpecReduce_rec( Gia_Man_t * pNew, Gia_Man_t * p, Gia_Obj_t * pObj, int f, int nPrefix )
Alan Mishchenko committed
78 79 80 81 82
{
    Gia_Obj_t * pRepr;
    int iLitNew;
    if ( ~Gia_ObjCopyF(p, f, pObj) )
        return;
Alan Mishchenko committed
83
    if ( f >= nPrefix && (pRepr = Gia_ObjReprObj(p, Gia_ObjId(p, pObj))) )
Alan Mishchenko committed
84
    {
Alan Mishchenko committed
85
        Gia_ManCorrSpecReduce_rec( pNew, p, pRepr, f, nPrefix );
86
        iLitNew = Abc_LitNotCond( Gia_ObjCopyF(p, f, pRepr), Gia_ObjPhase(pRepr) ^ Gia_ObjPhase(pObj) );
Alan Mishchenko committed
87 88
        Gia_ObjSetCopyF( p, f, pObj, iLitNew );
        return;
Alan Mishchenko committed
89 90
    }
    assert( Gia_ObjIsCand(pObj) );
Alan Mishchenko committed
91
    iLitNew = Gia_ManCorrSpecReal( pNew, p, pObj, f, nPrefix );
Alan Mishchenko committed
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
    Gia_ObjSetCopyF( p, f, pObj, iLitNew );
}

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

  Synopsis    [Derives SRM for signal correspondence.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Gia_Man_t * Gia_ManCorrSpecReduce( Gia_Man_t * p, int nFrames, int fScorr, Vec_Int_t ** pvOutputs, int fRings )
{
    Gia_Man_t * pNew, * pTemp;
    Gia_Obj_t * pObj, * pRepr;
    Vec_Int_t * vXorLits;
    int f, i, iPrev, iObj, iPrevNew, iObjNew;
    assert( nFrames > 0 );
    assert( Gia_ManRegNum(p) > 0 );
    assert( p->pReprs != NULL );
    p->pCopies = ABC_FALLOC( int, (nFrames+fScorr)*Gia_ManObjNum(p) );
    Gia_ManSetPhase( p );
    pNew = Gia_ManStart( nFrames * Gia_ManObjNum(p) );
118
    pNew->pName = Abc_UtilStrsav( p->pName );
119
    pNew->pSpec = Abc_UtilStrsav( p->pSpec );
Alan Mishchenko committed
120 121 122 123 124 125
    Gia_ManHashAlloc( pNew );
    Gia_ObjSetCopyF( p, 0, Gia_ManConst0(p), 0 );
    Gia_ManForEachRo( p, pObj, i )
        Gia_ObjSetCopyF( p, 0, pObj, Gia_ManAppendCi(pNew) );
    Gia_ManForEachRo( p, pObj, i )
        if ( (pRepr = Gia_ObjReprObj(p, Gia_ObjId(p, pObj))) )
Alan Mishchenko committed
126
            Gia_ObjSetCopyF( p, 0, pObj, Gia_ObjCopyF(p, 0, pRepr) );
Alan Mishchenko committed
127 128 129 130 131 132 133 134 135 136 137 138 139 140
    for ( f = 0; f < nFrames+fScorr; f++ )
    { 
        Gia_ObjSetCopyF( p, f, Gia_ManConst0(p), 0 );
        Gia_ManForEachPi( p, pObj, i )
            Gia_ObjSetCopyF( p, f, pObj, Gia_ManAppendCi(pNew) );
    }
    *pvOutputs = Vec_IntAlloc( 1000 );
    vXorLits = Vec_IntAlloc( 1000 );
    if ( fRings )
    {
        Gia_ManForEachObj1( p, pObj, i )
        {
            if ( Gia_ObjIsConst( p, i ) )
            {
Alan Mishchenko committed
141
                iObjNew = Gia_ManCorrSpecReal( pNew, p, pObj, nFrames, 0 );
142
                iObjNew = Abc_LitNotCond( iObjNew, Gia_ObjPhase(pObj) );
Alan Mishchenko committed
143 144 145 146 147 148 149 150 151 152 153 154
                if ( iObjNew != 0 )
                {
                    Vec_IntPush( *pvOutputs, 0 );
                    Vec_IntPush( *pvOutputs, i );
                    Vec_IntPush( vXorLits, iObjNew );
                }
            }
            else if ( Gia_ObjIsHead( p, i ) )
            {
                iPrev = i;
                Gia_ClassForEachObj1( p, i, iObj )
                {
Alan Mishchenko committed
155 156
                    iPrevNew = Gia_ManCorrSpecReal( pNew, p, Gia_ManObj(p, iPrev), nFrames, 0 );
                    iObjNew  = Gia_ManCorrSpecReal( pNew, p, Gia_ManObj(p, iObj), nFrames, 0 );
157 158
                    iPrevNew = Abc_LitNotCond( iPrevNew, Gia_ObjPhase(pObj) ^ Gia_ObjPhase(Gia_ManObj(p, iPrev)) );
                    iObjNew  = Abc_LitNotCond( iObjNew,  Gia_ObjPhase(pObj) ^ Gia_ObjPhase(Gia_ManObj(p, iObj)) );
Alan Mishchenko committed
159 160 161 162
                    if ( iPrevNew != iObjNew && iPrevNew != 0 && iObjNew != 1 )
                    {
                        Vec_IntPush( *pvOutputs, iPrev );
                        Vec_IntPush( *pvOutputs, iObj );
163
                        Vec_IntPush( vXorLits, Gia_ManHashAnd(pNew, iPrevNew, Abc_LitNot(iObjNew)) );
Alan Mishchenko committed
164 165 166 167
                    }
                    iPrev = iObj;
                }
                iObj = i;
Alan Mishchenko committed
168 169
                iPrevNew = Gia_ManCorrSpecReal( pNew, p, Gia_ManObj(p, iPrev), nFrames, 0 );
                iObjNew  = Gia_ManCorrSpecReal( pNew, p, Gia_ManObj(p, iObj), nFrames, 0 );
170 171
                iPrevNew = Abc_LitNotCond( iPrevNew, Gia_ObjPhase(pObj) ^ Gia_ObjPhase(Gia_ManObj(p, iPrev)) );
                iObjNew  = Abc_LitNotCond( iObjNew,  Gia_ObjPhase(pObj) ^ Gia_ObjPhase(Gia_ManObj(p, iObj)) );
Alan Mishchenko committed
172 173 174 175
                if ( iPrevNew != iObjNew && iPrevNew != 0 && iObjNew != 1 )
                {
                    Vec_IntPush( *pvOutputs, iPrev );
                    Vec_IntPush( *pvOutputs, iObj );
176
                    Vec_IntPush( vXorLits, Gia_ManHashAnd(pNew, iPrevNew, Abc_LitNot(iObjNew)) );
Alan Mishchenko committed
177 178 179 180 181 182 183 184 185 186 187
                }
            }
        }
    }
    else
    {
        Gia_ManForEachObj1( p, pObj, i )
        {
            pRepr = Gia_ObjReprObj( p, Gia_ObjId(p,pObj) );
            if ( pRepr == NULL )
                continue;
Alan Mishchenko committed
188 189
            iPrevNew = Gia_ObjIsConst(p, i)? 0 : Gia_ManCorrSpecReal( pNew, p, pRepr, nFrames, 0 );
            iObjNew  = Gia_ManCorrSpecReal( pNew, p, pObj, nFrames, 0 );
190
            iObjNew  = Abc_LitNotCond( iObjNew, Gia_ObjPhase(pRepr) ^ Gia_ObjPhase(pObj) );
Alan Mishchenko committed
191 192 193 194 195 196 197 198 199 200 201 202 203
            if ( iPrevNew != iObjNew )
            {
                Vec_IntPush( *pvOutputs, Gia_ObjId(p, pRepr) );
                Vec_IntPush( *pvOutputs, Gia_ObjId(p, pObj) );
                Vec_IntPush( vXorLits, Gia_ManHashXor(pNew, iPrevNew, iObjNew) );
            }
        }
    }
    Vec_IntForEachEntry( vXorLits, iObjNew, i )
        Gia_ManAppendCo( pNew, iObjNew );
    Vec_IntFree( vXorLits );
    Gia_ManHashStop( pNew );
    ABC_FREE( p->pCopies );
204
//Abc_Print( 1, "Before sweeping = %d\n", Gia_ManAndNum(pNew) );
Alan Mishchenko committed
205
    pNew = Gia_ManCleanup( pTemp = pNew );
206
//Abc_Print( 1, "After sweeping = %d\n", Gia_ManAndNum(pNew) );
Alan Mishchenko committed
207 208 209 210
    Gia_ManStop( pTemp );
    return pNew;
}

Alan Mishchenko committed
211 212 213

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

Alan Mishchenko committed
214 215 216 217 218 219 220 221 222
  Synopsis    [Derives SRM for signal correspondence.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
223
Gia_Man_t * Gia_ManCorrSpecReduceInit( Gia_Man_t * p, int nFrames, int nPrefix, int fScorr, Vec_Int_t ** pvOutputs, int fRings )
Alan Mishchenko committed
224 225 226 227 228
{
    Gia_Man_t * pNew, * pTemp;
    Gia_Obj_t * pObj, * pRepr;
    Vec_Int_t * vXorLits;
    int f, i, iPrevNew, iObjNew;
Alan Mishchenko committed
229
    assert( (!fScorr && nFrames > 1) || (fScorr && nFrames > 0) || nPrefix );
Alan Mishchenko committed
230 231
    assert( Gia_ManRegNum(p) > 0 );
    assert( p->pReprs != NULL );
Alan Mishchenko committed
232
    p->pCopies = ABC_FALLOC( int, (nFrames+nPrefix+fScorr)*Gia_ManObjNum(p) );
Alan Mishchenko committed
233
    Gia_ManSetPhase( p );
Alan Mishchenko committed
234
    pNew = Gia_ManStart( (nFrames+nPrefix) * Gia_ManObjNum(p) );
235
    pNew->pName = Abc_UtilStrsav( p->pName );
236
    pNew->pSpec = Abc_UtilStrsav( p->pSpec );
Alan Mishchenko committed
237 238 239 240 241 242
    Gia_ManHashAlloc( pNew );
    Gia_ManForEachRo( p, pObj, i )
    {
        Gia_ManAppendCi(pNew);
        Gia_ObjSetCopyF( p, 0, pObj, 0 );
    }
Alan Mishchenko committed
243
    for ( f = 0; f < nFrames+nPrefix+fScorr; f++ )
Alan Mishchenko committed
244 245 246 247 248 249 250
    { 
        Gia_ObjSetCopyF( p, f, Gia_ManConst0(p), 0 );
        Gia_ManForEachPi( p, pObj, i )
            Gia_ObjSetCopyF( p, f, pObj, Gia_ManAppendCi(pNew) );
    }
    *pvOutputs = Vec_IntAlloc( 1000 );
    vXorLits = Vec_IntAlloc( 1000 );
Alan Mishchenko committed
251
    for ( f = nPrefix; f < nFrames+nPrefix; f++ )
Alan Mishchenko committed
252 253 254 255 256 257
    {
        Gia_ManForEachObj1( p, pObj, i )
        {
            pRepr = Gia_ObjReprObj( p, Gia_ObjId(p,pObj) );
            if ( pRepr == NULL )
                continue;
Alan Mishchenko committed
258 259
            iPrevNew = Gia_ObjIsConst(p, i)? 0 : Gia_ManCorrSpecReal( pNew, p, pRepr, f, nPrefix );
            iObjNew  = Gia_ManCorrSpecReal( pNew, p, pObj, f, nPrefix );
260
            iObjNew  = Abc_LitNotCond( iObjNew, Gia_ObjPhase(pRepr) ^ Gia_ObjPhase(pObj) );
Alan Mishchenko committed
261 262 263 264 265 266 267 268 269 270 271 272 273
            if ( iPrevNew != iObjNew )
            {
                Vec_IntPush( *pvOutputs, Gia_ObjId(p, pRepr) );
                Vec_IntPush( *pvOutputs, Gia_ObjId(p, pObj) );
                Vec_IntPush( vXorLits, Gia_ManHashXor(pNew, iPrevNew, iObjNew) );
            }
        }
    }
    Vec_IntForEachEntry( vXorLits, iObjNew, i )
        Gia_ManAppendCo( pNew, iObjNew );
    Vec_IntFree( vXorLits );
    Gia_ManHashStop( pNew );
    ABC_FREE( p->pCopies );
274
//Abc_Print( 1, "Before sweeping = %d\n", Gia_ManAndNum(pNew) );
Alan Mishchenko committed
275
    pNew = Gia_ManCleanup( pTemp = pNew );
276
//Abc_Print( 1, "After sweeping = %d\n", Gia_ManAndNum(pNew) );
Alan Mishchenko committed
277 278 279 280 281 282
    Gia_ManStop( pTemp );
    return pNew;
}

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

Alan Mishchenko committed
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
  Synopsis    [Initializes simulation info for lcorr/scorr counter-examples.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Cec_ManStartSimInfo( Vec_Ptr_t * vInfo, int nFlops )
{
    unsigned * pInfo;
    int k, w, nWords;
    nWords = Vec_PtrReadWordsSimInfo( vInfo );
    assert( nFlops <= Vec_PtrSize(vInfo) );
    for ( k = 0; k < nFlops; k++ )
    {
300
        pInfo = (unsigned *)Vec_PtrEntry( vInfo, k );
Alan Mishchenko committed
301 302 303 304 305
        for ( w = 0; w < nWords; w++ )
            pInfo[w] = 0;
    }
    for ( k = nFlops; k < Vec_PtrSize(vInfo); k++ )
    {
306
        pInfo = (unsigned *)Vec_PtrEntry( vInfo, k );
Alan Mishchenko committed
307
        for ( w = 0; w < nWords; w++ )
Alan Mishchenko committed
308
            pInfo[w] = Gia_ManRandom( 0 );
Alan Mishchenko committed
309 310 311
    }
}

Alan Mishchenko committed
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
/**Function*************************************************************

  Synopsis    [Remaps simulation info from SRM to the original AIG.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Gia_ManCorrRemapSimInfo( Gia_Man_t * p, Vec_Ptr_t * vInfo )
{
    Gia_Obj_t * pObj, * pRepr;
    unsigned * pInfoObj, * pInfoRepr;
    int i, w, nWords;
    nWords = Vec_PtrReadWordsSimInfo( vInfo );
    Gia_ManForEachRo( p, pObj, i )
    {
        // skip ROs without representatives
        pRepr = Gia_ObjReprObj( p, Gia_ObjId(p,pObj) );
        if ( pRepr == NULL || Gia_ObjFailed(p, Gia_ObjId(p,pObj)) )
            continue;
335
        pInfoObj = (unsigned *)Vec_PtrEntry( vInfo, i );
Alan Mishchenko committed
336 337 338 339 340 341
        for ( w = 0; w < nWords; w++ )
            assert( pInfoObj[w] == 0 );
        // skip ROs with constant representatives
        if ( Gia_ObjIsConst0(pRepr) )
            continue;
        assert( Gia_ObjIsRo(p, pRepr) );
342
//        Abc_Print( 1, "%d -> %d    ", i, Gia_ObjId(p, pRepr) );
Alan Mishchenko committed
343
        // transfer info from the representative
344
        pInfoRepr = (unsigned *)Vec_PtrEntry( vInfo, Gia_ObjCioId(pRepr) - Gia_ManPiNum(p) );
Alan Mishchenko committed
345 346 347
        for ( w = 0; w < nWords; w++ )
            pInfoObj[w] = pInfoRepr[w];
    }
348
//    Abc_Print( 1, "\n" );
Alan Mishchenko committed
349 350 351 352
}

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

Alan Mishchenko committed
353
  Synopsis    [Collects information about remapping.]
Alan Mishchenko committed
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Vec_Int_t * Gia_ManCorrCreateRemapping( Gia_Man_t * p )
{
    Vec_Int_t * vPairs;
    Gia_Obj_t * pObj, * pRepr;
    int i;
    vPairs = Vec_IntAlloc( 100 );
    Gia_ManForEachRo( p, pObj, i )
    {
        // skip ROs without representatives
        pRepr = Gia_ObjReprObj( p, Gia_ObjId(p,pObj) );
Alan Mishchenko committed
372 373
        if ( pRepr == NULL || Gia_ObjIsConst0(pRepr) || Gia_ObjFailed(p, Gia_ObjId(p,pObj)) )
//        if ( pRepr == NULL || Gia_ObjIsConst0(pRepr) || Gia_ObjIsFailedPair(p, Gia_ObjId(p, pRepr), Gia_ObjId(p, pObj)) )
Alan Mishchenko committed
374 375
            continue;
        assert( Gia_ObjIsRo(p, pRepr) );
376
//        Abc_Print( 1, "%d -> %d    ", Gia_ObjId(p,pObj), Gia_ObjId(p, pRepr) );
Alan Mishchenko committed
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
        // remember the pair
        Vec_IntPush( vPairs, Gia_ObjCioId(pRepr) - Gia_ManPiNum(p) );
        Vec_IntPush( vPairs, i );
    }
    return vPairs;
}

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

  Synopsis    [Remaps simulation info from SRM to the original AIG.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Gia_ManCorrPerformRemapping( Vec_Int_t * vPairs, Vec_Ptr_t * vInfo )
{
    unsigned * pInfoObj, * pInfoRepr;
    int w, i, iObj, iRepr, nWords;
    nWords = Vec_PtrReadWordsSimInfo( vInfo );
    Vec_IntForEachEntry( vPairs, iRepr, i )
    {
        iObj = Vec_IntEntry( vPairs, ++i );
403 404
        pInfoObj = (unsigned *)Vec_PtrEntry( vInfo, iObj );
        pInfoRepr = (unsigned *)Vec_PtrEntry( vInfo, iRepr );
Alan Mishchenko committed
405 406 407 408 409 410 411 412 413 414
        for ( w = 0; w < nWords; w++ )
        {
            assert( pInfoObj[w] == 0 );
            pInfoObj[w] = pInfoRepr[w];
        }
    }
}

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

Alan Mishchenko committed
415
  Synopsis    [Packs one counter-examples into the array of simulation info.]
Alan Mishchenko committed
416 417 418 419 420 421 422

  Description []
               
  SideEffects []

  SeeAlso     []

Alan Mishchenko committed
423 424
*************************************`**********************************/
int Cec_ManLoadCounterExamplesTry( Vec_Ptr_t * vInfo, Vec_Ptr_t * vPres, int iBit, int * pLits, int nLits )
Alan Mishchenko committed
425
{
Alan Mishchenko committed
426 427 428
    unsigned * pInfo, * pPres;
    int i;
    for ( i = 0; i < nLits; i++ )
Alan Mishchenko committed
429
    {
430 431 432 433
        pInfo = (unsigned *)Vec_PtrEntry(vInfo, Abc_Lit2Var(pLits[i]));
        pPres = (unsigned *)Vec_PtrEntry(vPres, Abc_Lit2Var(pLits[i]));
        if ( Abc_InfoHasBit( pPres, iBit ) && 
             Abc_InfoHasBit( pInfo, iBit ) == Abc_LitIsCompl(pLits[i]) )
Alan Mishchenko committed
434
             return 0;
Alan Mishchenko committed
435
    }
Alan Mishchenko committed
436
    for ( i = 0; i < nLits; i++ )
Alan Mishchenko committed
437
    {
438 439 440 441 442
        pInfo = (unsigned *)Vec_PtrEntry(vInfo, Abc_Lit2Var(pLits[i]));
        pPres = (unsigned *)Vec_PtrEntry(vPres, Abc_Lit2Var(pLits[i]));
        Abc_InfoSetBit( pPres, iBit );
        if ( Abc_InfoHasBit( pInfo, iBit ) == Abc_LitIsCompl(pLits[i]) )
            Abc_InfoXorBit( pInfo, iBit );
Alan Mishchenko committed
443
    }
Alan Mishchenko committed
444
    return 1;
Alan Mishchenko committed
445 446 447 448
}

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

Alan Mishchenko committed
449
  Synopsis    [Performs bitpacking of counter-examples.]
Alan Mishchenko committed
450 451 452 453 454 455 456 457

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
458 459 460 461 462 463
int Cec_ManLoadCounterExamples( Vec_Ptr_t * vInfo, Vec_Int_t * vCexStore, int iStart )
{ 
    Vec_Int_t * vPat;
    Vec_Ptr_t * vPres;
    int nWords = Vec_PtrReadWordsSimInfo(vInfo);
    int nBits = 32 * nWords; 
464
    int k, nSize, kMax = 0;//, iBit = 1;
Alan Mishchenko committed
465 466 467 468
    vPat  = Vec_IntAlloc( 100 );
    vPres = Vec_PtrAllocSimInfo( Vec_PtrSize(vInfo), nWords );
    Vec_PtrCleanSimInfo( vPres, 0, nWords );
    while ( iStart < Vec_IntSize(vCexStore) )
Alan Mishchenko committed
469
    {
Alan Mishchenko committed
470 471 472 473 474 475 476 477 478 479 480 481 482 483
        // skip the output number
        iStart++;
        // get the number of items
        nSize = Vec_IntEntry( vCexStore, iStart++ );
        if ( nSize <= 0 )
            continue;
        // extract pattern
        Vec_IntClear( vPat );
        for ( k = 0; k < nSize; k++ )
            Vec_IntPush( vPat, Vec_IntEntry( vCexStore, iStart++ ) );
        // add pattern to storage
        for ( k = 1; k < nBits; k++ )
            if ( Cec_ManLoadCounterExamplesTry( vInfo, vPres, k, (int *)Vec_IntArray(vPat), Vec_IntSize(vPat) ) )
                break;
484
        kMax = Abc_MaxInt( kMax, k );
Alan Mishchenko committed
485 486
        if ( k == nBits-1 )
            break;
Alan Mishchenko committed
487
    }
Alan Mishchenko committed
488 489 490
    Vec_PtrFree( vPres );
    Vec_IntFree( vPat );
    return iStart;
Alan Mishchenko committed
491 492 493 494
}

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

Alan Mishchenko committed
495
  Synopsis    [Performs bitpacking of counter-examples.]
Alan Mishchenko committed
496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Cec_ManLoadCounterExamples2( Vec_Ptr_t * vInfo, Vec_Int_t * vCexStore, int iStart )
{ 
    unsigned * pInfo;
    int nBits = 32 * Vec_PtrReadWordsSimInfo(vInfo); 
    int k, iLit, nLits, Out, iBit = 1;
    while ( iStart < Vec_IntSize(vCexStore) )
    {
        // skip the output number
//        iStart++;
        Out = Vec_IntEntry( vCexStore, iStart++ );
514
//        Abc_Print( 1, "iBit = %d. Out = %d.\n", iBit, Out );
Alan Mishchenko committed
515 516 517 518 519 520 521 522
        // get the number of items
        nLits = Vec_IntEntry( vCexStore, iStart++ );
        if ( nLits <= 0 )
            continue;
        // add pattern to storage
        for ( k = 0; k < nLits; k++ )
        {
            iLit = Vec_IntEntry( vCexStore, iStart++ );
523 524 525
            pInfo = (unsigned *)Vec_PtrEntry( vInfo, Abc_Lit2Var(iLit) );
            if ( Abc_InfoHasBit( pInfo, iBit ) == Abc_LitIsCompl(iLit) )
                Abc_InfoXorBit( pInfo, iBit );
Alan Mishchenko committed
526 527 528 529
        }
        if ( ++iBit == nBits )
            break;
    }
530
//    Abc_Print( 1, "added %d bits\n", iBit-1 );
Alan Mishchenko committed
531 532 533 534 535
    return iStart;
}

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

Alan Mishchenko committed
536
  Synopsis    [Resimulates counter-examples derived by the SAT solver.]
Alan Mishchenko committed
537 538 539 540 541 542 543

  Description []
               
  SideEffects []

  SeeAlso     []

Alan Mishchenko committed
544 545 546 547 548 549 550
***********************************************************************/
int Cec_ManResimulateCounterExamples( Cec_ManSim_t * pSim, Vec_Int_t * vCexStore, int nFrames )
{ 
    Vec_Int_t * vPairs;
    Vec_Ptr_t * vSimInfo; 
    int RetValue = 0, iStart = 0;
    vPairs = Gia_ManCorrCreateRemapping( pSim->pAig );
551
    Gia_ManCreateValueRefs( pSim->pAig );
Alan Mishchenko committed
552
//    pSim->pPars->nWords  = 63;
Alan Mishchenko committed
553
    pSim->pPars->nFrames = nFrames;
Alan Mishchenko committed
554 555
    vSimInfo = Vec_PtrAllocSimInfo( Gia_ManRegNum(pSim->pAig) + Gia_ManPiNum(pSim->pAig) * nFrames, pSim->pPars->nWords );
    while ( iStart < Vec_IntSize(vCexStore) )
Alan Mishchenko committed
556
    {
Alan Mishchenko committed
557 558 559 560 561 562 563
        Cec_ManStartSimInfo( vSimInfo, Gia_ManRegNum(pSim->pAig) );
        iStart = Cec_ManLoadCounterExamples( vSimInfo, vCexStore, iStart );
//        iStart = Cec_ManLoadCounterExamples2( vSimInfo, vCexStore, iStart );
//        Gia_ManCorrRemapSimInfo( pSim->pAig, vSimInfo );
        Gia_ManCorrPerformRemapping( vPairs, vSimInfo );
        RetValue |= Cec_ManSeqResimulate( pSim, vSimInfo );
//        Cec_ManSeqResimulateInfo( pSim->pAig, vSimInfo, NULL );
Alan Mishchenko committed
564
    }
Alan Mishchenko committed
565 566 567 568 569 570 571 572 573
//Gia_ManEquivPrintOne( pSim->pAig, 85, 0 );
    assert( iStart == Vec_IntSize(vCexStore) );
    Vec_PtrFree( vSimInfo );
    Vec_IntFree( vPairs );
    return RetValue;
}

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

Alan Mishchenko committed
574 575 576 577 578 579 580 581 582 583 584 585 586
  Synopsis    [Resimulates counter-examples derived by the SAT solver.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Cec_ManResimulateCounterExamplesComb( Cec_ManSim_t * pSim, Vec_Int_t * vCexStore )
{ 
    Vec_Ptr_t * vSimInfo; 
    int RetValue = 0, iStart = 0;
587
    Gia_ManCreateValueRefs( pSim->pAig );
Alan Mishchenko committed
588
    pSim->pPars->nFrames = 1;
Alan Mishchenko committed
589 590 591 592 593 594 595 596 597 598 599 600 601 602
    vSimInfo = Vec_PtrAllocSimInfo( Gia_ManCiNum(pSim->pAig), pSim->pPars->nWords );
    while ( iStart < Vec_IntSize(vCexStore) )
    {
        Cec_ManStartSimInfo( vSimInfo, 0 );
        iStart = Cec_ManLoadCounterExamples( vSimInfo, vCexStore, iStart );
        RetValue |= Cec_ManSeqResimulate( pSim, vSimInfo );
    }
    assert( iStart == Vec_IntSize(vCexStore) );
    Vec_PtrFree( vSimInfo );
    return RetValue;
}

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

Alan Mishchenko committed
603 604 605 606 607 608 609 610 611 612 613 614 615 616 617
  Synopsis    [Updates equivalence classes by marking those that timed out.]

  Description [Returns 1 if all ndoes are proved.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Gia_ManCheckRefinements( Gia_Man_t * p, Vec_Str_t * vStatus, Vec_Int_t * vOutputs, Cec_ManSim_t * pSim, int fRings )
{
    int i, status, iRepr, iObj;
    int Counter = 0;
    assert( 2 * Vec_StrSize(vStatus) == Vec_IntSize(vOutputs) );
    Vec_StrForEachEntry( vStatus, status, i )
Alan Mishchenko committed
618
    {
Alan Mishchenko committed
619 620 621 622 623 624 625 626 627
        iRepr = Vec_IntEntry( vOutputs, 2*i );
        iObj  = Vec_IntEntry( vOutputs, 2*i+1 );
        if ( status == 1 )
            continue;
        if ( status == 0 )
        {
            if ( Gia_ObjHasSameRepr(p, iRepr, iObj) )
                Counter++;
//            if ( Gia_ObjHasSameRepr(p, iRepr, iObj) )
628
//                Abc_Print( 1, "Gia_ManCheckRefinements(): Disproved equivalence (%d,%d) is not refined!\n", iRepr, iObj );
Alan Mishchenko committed
629 630 631 632 633 634 635
//            if ( Gia_ObjHasSameRepr(p, iRepr, iObj) )
//                Cec_ManSimClassRemoveOne( pSim, iObj );
            continue;
        }
        if ( status == -1 )
        {
//            if ( !Gia_ObjFailed( p, iObj ) )
636
//                Abc_Print( 1, "Gia_ManCheckRefinements(): Failed equivalence is not marked as failed!\n" );
Alan Mishchenko committed
637 638 639 640 641 642 643
//            Gia_ObjSetFailed( p, iRepr );
//            Gia_ObjSetFailed( p, iObj );        
//            if ( fRings )
//            Cec_ManSimClassRemoveOne( pSim, iRepr );
            Cec_ManSimClassRemoveOne( pSim, iObj );
            continue;
        }
Alan Mishchenko committed
644
    }
Alan Mishchenko committed
645
//    if ( Counter )
646
//    Abc_Print( 1, "Gia_ManCheckRefinements(): Could not refine %d nodes.\n", Counter );
Alan Mishchenko committed
647 648 649
    return 1;
}

Alan Mishchenko committed
650

Alan Mishchenko committed
651 652
/**Function*************************************************************

Alan Mishchenko committed
653
  Synopsis    [Duplicates the AIG in the DFS order.]
Alan Mishchenko committed
654 655 656 657 658 659 660 661

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
662
void Gia_ManCorrReduce_rec( Gia_Man_t * pNew, Gia_Man_t * p, Gia_Obj_t * pObj )
Alan Mishchenko committed
663
{
Alan Mishchenko committed
664 665
    Gia_Obj_t * pRepr;
    if ( (pRepr = Gia_ObjReprObj(p, Gia_ObjId(p, pObj))) )
Alan Mishchenko committed
666
    {
Alan Mishchenko committed
667
        Gia_ManCorrReduce_rec( pNew, p, pRepr );
668
        pObj->Value = Abc_LitNotCond( pRepr->Value, Gia_ObjPhaseReal(pRepr) ^ Gia_ObjPhaseReal(pObj) );
Alan Mishchenko committed
669
        return;
Alan Mishchenko committed
670
    }
Alan Mishchenko committed
671 672 673 674 675 676
    if ( ~pObj->Value )
        return;
    assert( Gia_ObjIsAnd(pObj) );
    Gia_ManCorrReduce_rec( pNew, p, Gia_ObjFanin0(pObj) );
    Gia_ManCorrReduce_rec( pNew, p, Gia_ObjFanin1(pObj) );
    pObj->Value = Gia_ManHashAnd( pNew, Gia_ObjFanin0Copy(pObj), Gia_ObjFanin1Copy(pObj) );
Alan Mishchenko committed
677 678 679 680
}

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

Alan Mishchenko committed
681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696
  Synopsis    [Reduces AIG using equivalence classes.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Gia_Man_t * Gia_ManCorrReduce( Gia_Man_t * p )
{
    Gia_Man_t * pNew;
    Gia_Obj_t * pObj;
    int i;
    Gia_ManSetPhase( p );
    pNew = Gia_ManStart( Gia_ManObjNum(p) );
697
    pNew->pName = Abc_UtilStrsav( p->pName );
698
    pNew->pSpec = Abc_UtilStrsav( p->pSpec );
Alan Mishchenko committed
699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715
    Gia_ManFillValue( p );
    Gia_ManConst0(p)->Value = 0;
    Gia_ManForEachCi( p, pObj, i )
        pObj->Value = Gia_ManAppendCi(pNew);
    Gia_ManHashAlloc( pNew );
    Gia_ManForEachCo( p, pObj, i )
        Gia_ManCorrReduce_rec( pNew, p, Gia_ObjFanin0(pObj) );
    Gia_ManForEachCo( p, pObj, i )
        Gia_ManAppendCo( pNew, Gia_ObjFanin0Copy(pObj) );
    Gia_ManHashStop( pNew );
    Gia_ManSetRegNum( pNew, Gia_ManRegNum(p) );
    return pNew;
}


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

Alan Mishchenko committed
716
  Synopsis    [Prints statistics during solving.]
Alan Mishchenko committed
717 718 719 720 721 722 723 724

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
725
void Cec_ManRefinedClassPrintStats( Gia_Man_t * p, Vec_Str_t * vStatus, int iIter, abctime Time )
Alan Mishchenko committed
726
{ 
Alan Mishchenko committed
727 728 729
    int nLits, CounterX = 0, Counter0 = 0, Counter = 0;
    int i, Entry, nProve = 0, nDispr = 0, nFail = 0;
    for ( i = 1; i < Gia_ManObjNum(p); i++ )
Alan Mishchenko committed
730
    {
Alan Mishchenko committed
731 732 733 734 735 736
        if ( Gia_ObjIsNone(p, i) )
            CounterX++;
        else if ( Gia_ObjIsConst(p, i) )
            Counter0++;
        else if ( Gia_ObjIsHead(p, i) )
            Counter++;
Alan Mishchenko committed
737
    }
Alan Mishchenko committed
738 739
    CounterX -= Gia_ManCoNum(p);
    nLits = Gia_ManCiNum(p) + Gia_ManAndNum(p) - Counter - CounterX;
Alan Mishchenko committed
740
    if ( iIter == -1 )
741
        Abc_Print( 1, "BMC : " );
Alan Mishchenko committed
742
    else
743 744
        Abc_Print( 1, "%3d : ", iIter );
    Abc_Print( 1, "c =%8d  cl =%7d  lit =%8d  ", Counter0, Counter, nLits );
Alan Mishchenko committed
745 746 747 748 749 750 751 752 753 754
    if ( vStatus )
    Vec_StrForEachEntry( vStatus, Entry, i )
    {
        if ( Entry == 1 )
            nProve++;
        else if ( Entry == 0 )
            nDispr++;
        else if ( Entry == -1 )
            nFail++;
    }
755
    Abc_Print( 1, "p =%6d  d =%6d  f =%6d  ", nProve, nDispr, nFail );
756
    Abc_Print( 1, "%c  ", Gia_ObjIsConst( p, Gia_ObjFaninId0p(p, Gia_ManPo(p, 0)) ) ? '+' : '-' );
757
    Abc_PrintTime( 1, "T", Time );
Alan Mishchenko committed
758 759 760 761
}

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

Alan Mishchenko committed
762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783
  Synopsis    [Runs BMC for the equivalence classes.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Cec_ManLSCorrespondenceBmc( Gia_Man_t * pAig, Cec_ParCor_t * pPars, int nPrefs )
{  
    Cec_ParSim_t ParsSim, * pParsSim = &ParsSim;
    Cec_ParSat_t ParsSat, * pParsSat = &ParsSat;
    Vec_Str_t * vStatus;
    Vec_Int_t * vOutputs;
    Vec_Int_t * vCexStore;
    Cec_ManSim_t * pSim;
    Gia_Man_t * pSrm;
    int fChanges, RetValue;
    // prepare simulation manager
    Cec_ManSimSetDefaultParams( pParsSim );
    pParsSim->nWords     = pPars->nWords;
Alan Mishchenko committed
784
    pParsSim->nFrames    = pPars->nRounds;
Alan Mishchenko committed
785 786 787 788 789 790 791 792 793 794 795
    pParsSim->fVerbose   = pPars->fVerbose;
    pParsSim->fLatchCorr = pPars->fLatchCorr;
    pParsSim->fSeqSimulate = 1;
    pSim = Cec_ManSimStart( pAig, pParsSim );
    // prepare SAT solving
    Cec_ManSatSetDefaultParams( pParsSat );
    pParsSat->nBTLimit = pPars->nBTLimit;
    pParsSat->fVerbose = pPars->fVerbose;
    fChanges = 1;
    while ( fChanges )
    {
796
        abctime clkBmc = Abc_Clock();
Alan Mishchenko committed
797 798 799 800 801 802 803 804 805 806
        fChanges = 0;
        pSrm = Gia_ManCorrSpecReduceInit( pAig, pPars->nFrames, nPrefs, !pPars->fLatchCorr, &vOutputs, pPars->fUseRings );
        if ( Gia_ManPoNum(pSrm) == 0 )
        {
            Gia_ManStop( pSrm );
            Vec_IntFree( vOutputs );
            break;
        } 
        pParsSat->nBTLimit *= 10;
        if ( pPars->fUseCSat )
Alan Mishchenko committed
807
            vCexStore = Tas_ManSolveMiterNc( pSrm, pPars->nBTLimit, &vStatus, 0 );
Alan Mishchenko committed
808 809 810 811 812 813 814 815 816 817
        else
            vCexStore = Cec_ManSatSolveMiter( pSrm, pParsSat, &vStatus );
        // refine classes with these counter-examples
        if ( Vec_IntSize(vCexStore) )
        {
            RetValue = Cec_ManResimulateCounterExamples( pSim, vCexStore, pPars->nFrames + 1 + nPrefs );
            Gia_ManCheckRefinements( pAig, vStatus, vOutputs, pSim, pPars->fUseRings );
            fChanges = 1;
        }
        if ( pPars->fVerbose )
818
            Cec_ManRefinedClassPrintStats( pAig, vStatus, -1, Abc_Clock() - clkBmc );
Alan Mishchenko committed
819 820 821 822 823 824 825 826 827 828 829
        // recycle
        Vec_IntFree( vCexStore );
        Vec_StrFree( vStatus );
        Gia_ManStop( pSrm );
        Vec_IntFree( vOutputs );
    }
    Cec_ManSimStop( pSim );
}

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

830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899
  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Cec_ManLSCorrAnalyzeDependence( Gia_Man_t * p, Vec_Int_t * vEquivs, Vec_Str_t * vStatus )
{
    Gia_Obj_t * pObj, * pObjRo;
    int i, Iter, iObj, iRepr, fPrev, Total, Count0, Count1;
    assert( Vec_StrSize(vStatus) * 2 == Vec_IntSize(vEquivs) );
    Total = 0;
    Gia_ManForEachObj( p, pObj, i )
    {
        assert( pObj->fMark1 == 0 );
        if ( Gia_ObjHasRepr(p, i) )
            Total++;
    }
    Count0 = 0;
    for ( i = 0; i < Vec_StrSize(vStatus); i++ )
    {
        iRepr = Vec_IntEntry(vEquivs, 2*i);
        iObj = Vec_IntEntry(vEquivs, 2*i+1);
        assert( iRepr == Gia_ObjRepr(p, iObj) );
        if ( Vec_StrEntry(vStatus, i) != 1 ) // disproved or undecided
        {
            Gia_ManObj(p, iObj)->fMark1 = 1;
            Count0++;
        }
    }
    for ( Iter = 0; Iter < 100; Iter++ )
    {
        int fChanges = 0;
        Gia_ManForEachObj1( p, pObj, i )
        {
            if ( Gia_ObjIsCi(pObj) )
                continue;
            assert( Gia_ObjIsAnd(pObj) || Gia_ObjIsCo(pObj) );
//            fPrev = pObj->fMark1;
            if ( Gia_ObjIsAnd(pObj) )
                pObj->fMark1 |= Gia_ObjFanin0(pObj)->fMark1 | Gia_ObjFanin1(pObj)->fMark1;
            else
                pObj->fMark1 |= Gia_ObjFanin0(pObj)->fMark1;
//            fChanges += fPrev ^ pObj->fMark1;
        }
        Gia_ManForEachRiRo( p, pObj, pObjRo, i )
        {
            fPrev = pObjRo->fMark1;
            pObjRo->fMark1 = pObj->fMark1;
            fChanges += fPrev ^ pObjRo->fMark1;
        }
        if ( fChanges == 0 )
            break;
    }
    Count1 = 0;
    Gia_ManForEachObj( p, pObj, i )
    {
        if ( pObj->fMark1 && Gia_ObjHasRepr(p, i) )
            Count1++;
        pObj->fMark1 = 0;
    }
    printf( "%5d -> %5d (%3d)  ", Count0, Count1, Iter );
    return 0;
}

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

Alan Mishchenko committed
900
  Synopsis    [Internal procedure for register correspondence.]
Alan Mishchenko committed
901 902 903 904 905 906 907 908

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
909
int Cec_ManLSCorrespondenceClasses( Gia_Man_t * pAig, Cec_ParCor_t * pPars )
Alan Mishchenko committed
910
{  
Alan Mishchenko committed
911 912
    int nIterMax     = 100000;
    int nAddFrames   = 1; // additional timeframes to simulate
913
    int fRunBmcFirst = 1;
Alan Mishchenko committed
914 915 916 917 918 919 920
    Vec_Str_t * vStatus;
    Vec_Int_t * vOutputs;
    Vec_Int_t * vCexStore;
    Cec_ParSim_t ParsSim, * pParsSim = &ParsSim;
    Cec_ParSat_t ParsSat, * pParsSat = &ParsSat;
    Cec_ManSim_t * pSim;
    Gia_Man_t * pSrm;
921
    int r, RetValue;
922 923 924
    abctime clkTotal = Abc_Clock();
    abctime clkSat = 0, clkSim = 0, clkSrm = 0;
    abctime clk2, clk = Abc_Clock();
Alan Mishchenko committed
925 926
    if ( Gia_ManRegNum(pAig) == 0 )
    {
927
        Abc_Print( 1, "Cec_ManLatchCorrespondence(): Not a sequential AIG.\n" );
Alan Mishchenko committed
928
        return 0;
Alan Mishchenko committed
929
    }
Alan Mishchenko committed
930
    Gia_ManRandom( 1 );
Alan Mishchenko committed
931 932 933
    // prepare simulation manager
    Cec_ManSimSetDefaultParams( pParsSim );
    pParsSim->nWords     = pPars->nWords;
Alan Mishchenko committed
934
    pParsSim->nFrames    = pPars->nFrames;
Alan Mishchenko committed
935 936
    pParsSim->fVerbose   = pPars->fVerbose;
    pParsSim->fLatchCorr = pPars->fLatchCorr;
937
    pParsSim->fConstCorr = pPars->fConstCorr;
Alan Mishchenko committed
938 939 940
    pParsSim->fSeqSimulate = 1;
    // create equivalence classes of registers
    pSim = Cec_ManSimStart( pAig, pParsSim );
Alan Mishchenko committed
941 942
    if ( pAig->pReprs == NULL )
    {
943
        Cec_ManSimClassesPrepare( pSim, pPars->nLevelMax );
Alan Mishchenko committed
944 945
        Cec_ManSimClassesRefine( pSim );
    }
Alan Mishchenko committed
946 947 948 949
    // prepare SAT solving
    Cec_ManSatSetDefaultParams( pParsSat );
    pParsSat->nBTLimit = pPars->nBTLimit;
    pParsSat->fVerbose = pPars->fVerbose;
950 951 952
    // limit the number of conflicts in the circuit-based solver
    if ( pPars->fUseCSat )
        pParsSat->nBTLimit = Abc_MinInt( pParsSat->nBTLimit, 1000 );
Alan Mishchenko committed
953 954
    if ( pPars->fVerbose )
    {
955
        Abc_Print( 1, "Obj = %7d. And = %7d. Conf = %5d. Fr = %d. Lcorr = %d. Ring = %d. CSat = %d.\n",
Alan Mishchenko committed
956 957
            Gia_ManObjNum(pAig), Gia_ManAndNum(pAig), 
            pPars->nBTLimit, pPars->nFrames, pPars->fLatchCorr, pPars->fUseRings, pPars->fUseCSat );
958
        Cec_ManRefinedClassPrintStats( pAig, NULL, 0, Abc_Clock() - clk );
Alan Mishchenko committed
959
    }
Alan Mishchenko committed
960
    // check the base case
Alan Mishchenko committed
961 962
    if ( fRunBmcFirst && (!pPars->fLatchCorr || pPars->nFrames > 1) )
        Cec_ManLSCorrespondenceBmc( pAig, pPars, 0 );
963 964 965 966 967 968 969 970 971 972 973
    if ( pPars->pFunc )
    {
        ((int (*)(void *))pPars->pFunc)( pPars->pData );
        ((int (*)(void *))pPars->pFunc)( pPars->pData );
    }
    if ( pPars->nStepsMax == 0 )
    {
        Abc_Print( 1, "Stopped signal correspondence after BMC.\n" );
        Cec_ManSimStop( pSim );
        return 1;
    }
Alan Mishchenko committed
974
    // perform refinement of equivalence classes
Alan Mishchenko committed
975
    for ( r = 0; r < nIterMax; r++ )
Alan Mishchenko committed
976
    { 
977 978 979 980 981 982
        if ( pPars->nStepsMax == r )
        {
            Cec_ManSimStop( pSim );
            Abc_Print( 1, "Stopped signal correspondence after %d refiment iterations.\n", r );
            return 1;
        }
983
        clk = Abc_Clock();
Alan Mishchenko committed
984
        // perform speculative reduction
985
        clk2 = Abc_Clock();
Alan Mishchenko committed
986 987
        pSrm = Gia_ManCorrSpecReduce( pAig, pPars->nFrames, !pPars->fLatchCorr, &vOutputs, pPars->fUseRings );
        assert( Gia_ManRegNum(pSrm) == 0 && Gia_ManPiNum(pSrm) == Gia_ManRegNum(pAig)+(pPars->nFrames+!pPars->fLatchCorr)*Gia_ManPiNum(pAig) );
988
        clkSrm += Abc_Clock() - clk2;
Alan Mishchenko committed
989 990 991 992 993 994 995 996
        if ( Gia_ManCoNum(pSrm) == 0 )
        {
            Vec_IntFree( vOutputs );
            Gia_ManStop( pSrm );            
            break;
        }
//Gia_DumpAiger( pSrm, "corrsrm", r, 2 );
        // found counter-examples to speculation
997
        clk2 = Abc_Clock();
Alan Mishchenko committed
998
        if ( pPars->fUseCSat )
Alan Mishchenko committed
999
            vCexStore = Cbs_ManSolveMiterNc( pSrm, pPars->nBTLimit, &vStatus, 0 );
Alan Mishchenko committed
1000 1001 1002
        else
            vCexStore = Cec_ManSatSolveMiter( pSrm, pParsSat, &vStatus );
        Gia_ManStop( pSrm );
1003
        clkSat += Abc_Clock() - clk2;
Alan Mishchenko committed
1004 1005 1006 1007 1008 1009 1010
        if ( Vec_IntSize(vCexStore) == 0 )
        {
            Vec_IntFree( vCexStore );
            Vec_StrFree( vStatus );
            Vec_IntFree( vOutputs );
            break;
        }
1011 1012
//        Cec_ManLSCorrAnalyzeDependence( pAig, vOutputs, vStatus );        

Alan Mishchenko committed
1013
        // refine classes with these counter-examples
1014
        clk2 = Abc_Clock();
Alan Mishchenko committed
1015 1016
        RetValue = Cec_ManResimulateCounterExamples( pSim, vCexStore, pPars->nFrames + 1 + nAddFrames );
        Vec_IntFree( vCexStore );
1017
        clkSim += Abc_Clock() - clk2;
Alan Mishchenko committed
1018 1019
        Gia_ManCheckRefinements( pAig, vStatus, vOutputs, pSim, pPars->fUseRings );
        if ( pPars->fVerbose )
1020
            Cec_ManRefinedClassPrintStats( pAig, vStatus, r+1, Abc_Clock() - clk );
Alan Mishchenko committed
1021 1022
        Vec_StrFree( vStatus );
        Vec_IntFree( vOutputs );
Alan Mishchenko committed
1023
//Gia_ManEquivPrintClasses( pAig, 1, 0 );
1024 1025
        if ( pPars->pFunc )
            ((int (*)(void *))pPars->pFunc)( pPars->pData );
1026 1027 1028 1029 1030 1031 1032 1033
        // quit if const is no longer there
        if ( pPars->fStopWhenGone && Gia_ManPoNum(pAig) == 1 && !Gia_ObjIsConst( pAig, Gia_ObjFaninId0p(pAig, Gia_ManPo(pAig, 0)) ) )
        {
            printf( "Iterative refinement is stopped after iteration %d\n", r );
            printf( "because the property output is no longer a candidate constant.\n" );
            Cec_ManSimStop( pSim );
            return 0;
        }
Alan Mishchenko committed
1034
    }
Alan Mishchenko committed
1035
    if ( pPars->fVerbose )
1036
        Cec_ManRefinedClassPrintStats( pAig, NULL, r+1, Abc_Clock() - clk );
Alan Mishchenko committed
1037
    // check the overflow
Alan Mishchenko committed
1038
    if ( r == nIterMax )
1039
        Abc_Print( 1, "The refinement was not finished. The result may be incorrect.\n" );
Alan Mishchenko committed
1040
    Cec_ManSimStop( pSim );
Alan Mishchenko committed
1041 1042 1043
    // check the base case
    if ( !fRunBmcFirst && (!pPars->fLatchCorr || pPars->nFrames > 1) )
        Cec_ManLSCorrespondenceBmc( pAig, pPars, 0 );
1044
    clkTotal = Abc_Clock() - clkTotal;
Alan Mishchenko committed
1045 1046 1047 1048 1049 1050 1051
    // report the results
    if ( pPars->fVerbose )
    {
        ABC_PRTP( "Srm  ", clkSrm,                        clkTotal );
        ABC_PRTP( "Sat  ", clkSat,                        clkTotal );
        ABC_PRTP( "Sim  ", clkSim,                        clkTotal );
        ABC_PRTP( "Other", clkTotal-clkSat-clkSrm-clkSim, clkTotal );
1052
        Abc_PrintTime( 1, "TOTAL",  clkTotal );
Alan Mishchenko committed
1053
    }
1054
    return 1;
Alan Mishchenko committed
1055 1056 1057 1058
}    

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

Alan Mishchenko committed
1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073
  Synopsis    [Computes new initial state.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
unsigned * Cec_ManComputeInitState( Gia_Man_t * pAig, int nFrames )
{  
    Gia_Obj_t * pObj, * pObjRo, * pObjRi;
    unsigned * pInitState;
    int i, f; 
    Gia_ManRandom( 1 );
1074
//    Abc_Print( 1, "Simulating %d timeframes.\n", nFrames );
Alan Mishchenko committed
1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089
    Gia_ManForEachRo( pAig, pObj, i )
        pObj->fMark1 = 0;
    for ( f = 0; f < nFrames; f++ )
    {
        Gia_ManConst0(pAig)->fMark1 = 0;
        Gia_ManForEachPi( pAig, pObj, i )
            pObj->fMark1 = Gia_ManRandom(0) & 1;
        Gia_ManForEachAnd( pAig, pObj, i )
            pObj->fMark1 = (Gia_ObjFanin0(pObj)->fMark1 ^ Gia_ObjFaninC0(pObj)) & 
                (Gia_ObjFanin1(pObj)->fMark1 ^ Gia_ObjFaninC1(pObj));
        Gia_ManForEachRi( pAig, pObj, i )
            pObj->fMark1 = (Gia_ObjFanin0(pObj)->fMark1 ^ Gia_ObjFaninC0(pObj));
        Gia_ManForEachRiRo( pAig, pObjRi, pObjRo, i )
            pObjRo->fMark1 = pObjRi->fMark1;
    }
1090
    pInitState = ABC_CALLOC( unsigned, Abc_BitWordNum(Gia_ManRegNum(pAig)) );
Alan Mishchenko committed
1091 1092 1093
    Gia_ManForEachRo( pAig, pObj, i )
    {
        if ( pObj->fMark1 )
1094
            Abc_InfoSetBit( pInitState, i );
1095
//        Abc_Print( 1, "%d", pObj->fMark1 );
Alan Mishchenko committed
1096
    }
1097
//    Abc_Print( 1, "\n" );
Alan Mishchenko committed
1098 1099 1100 1101 1102 1103
    Gia_ManCleanMark1( pAig );
    return pInitState;
}

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

Alan Mishchenko committed
1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120
  Synopsis    [Prints flop equivalences.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Cec_ManPrintFlopEquivs( Gia_Man_t * p )
{
    Gia_Obj_t * pObj, * pRepr;
    int i;
    assert( p->vNamesIn != NULL );
    Gia_ManForEachRo( p, pObj, i )
    {
        if ( Gia_ObjIsConst(p, Gia_ObjId(p, pObj)) )
1121
            Abc_Print( 1, "Original flop %s is proved equivalent to constant.\n", Vec_PtrEntry(p->vNamesIn, Gia_ObjCioId(pObj)) );
Alan Mishchenko committed
1122 1123 1124
        else if ( (pRepr = Gia_ObjReprObj(p, Gia_ObjId(p, pObj))) )
        {
            if ( Gia_ObjIsCi(pRepr) )
1125
                Abc_Print( 1, "Original flop %s is proved equivalent to flop %s.\n",
Alan Mishchenko committed
1126
                    Vec_PtrEntry( p->vNamesIn, Gia_ObjCioId(pObj)  ),
Alan Mishchenko committed
1127 1128
                    Vec_PtrEntry( p->vNamesIn, Gia_ObjCioId(pRepr) ) );
            else
1129
                Abc_Print( 1, "Original flop %s is proved equivalent to internal node %d.\n",
Alan Mishchenko committed
1130 1131 1132 1133 1134
                    Vec_PtrEntry( p->vNamesIn, Gia_ObjCioId(pObj) ), Gia_ObjId(p, pRepr) );
        }
    }
}

Alan Mishchenko committed
1135

Alan Mishchenko committed
1136 1137
/**Function*************************************************************

Alan Mishchenko committed
1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149
  Synopsis    [Top-level procedure for register correspondence.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Gia_Man_t * Cec_ManLSCorrespondence( Gia_Man_t * pAig, Cec_ParCor_t * pPars )
{  
    Gia_Man_t * pNew, * pTemp;
Alan Mishchenko committed
1150
    unsigned * pInitState;
Alan Mishchenko committed
1151
    int RetValue;
Alan Mishchenko committed
1152 1153 1154
    ABC_FREE( pAig->pReprs );
    ABC_FREE( pAig->pNexts );
    if ( pPars->nPrefix == 0 )
1155
    {
Alan Mishchenko committed
1156
        RetValue = Cec_ManLSCorrespondenceClasses( pAig, pPars );
1157 1158 1159
        if ( RetValue == 0 )
            return Gia_ManDup( pAig );
    }
Alan Mishchenko committed
1160 1161 1162 1163
    else
    {
        // compute the cycles AIG
        pInitState = Cec_ManComputeInitState( pAig, pPars->nPrefix );
1164
        pTemp = Gia_ManDupFlip( pAig, (int *)pInitState );
Alan Mishchenko committed
1165 1166 1167 1168 1169 1170 1171 1172
        ABC_FREE( pInitState );
        // compute classes of this AIG
        RetValue = Cec_ManLSCorrespondenceClasses( pTemp, pPars );
        // transfer the class info
        pAig->pReprs = pTemp->pReprs; pTemp->pReprs = NULL;
        pAig->pNexts = pTemp->pNexts; pTemp->pNexts = NULL;
        // perform additional BMC
        pPars->fUseCSat = 0;
1173
        pPars->nBTLimit = Abc_MaxInt( pPars->nBTLimit, 1000 );
Alan Mishchenko committed
1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186
        Cec_ManLSCorrespondenceBmc( pAig, pPars, pPars->nPrefix );
/*
        // transfer the class info back
        pTemp->pReprs = pAig->pReprs; pAig->pReprs = NULL;
        pTemp->pNexts = pAig->pNexts; pAig->pNexts = NULL;
        // continue refining
        RetValue = Cec_ManLSCorrespondenceClasses( pTemp, pPars );
        // transfer the class info
        pAig->pReprs = pTemp->pReprs; pTemp->pReprs = NULL;
        pAig->pNexts = pTemp->pNexts; pTemp->pNexts = NULL;
*/
        Gia_ManStop( pTemp );
    }
Alan Mishchenko committed
1187
    // derive reduced AIG
Alan Mishchenko committed
1188 1189 1190
    if ( pPars->fMakeChoices )
    {
        pNew = Gia_ManEquivToChoices( pAig, 1 );
1191
//        Gia_ManHasChoices_very_old( pNew );
Alan Mishchenko committed
1192 1193 1194
    }
    else
    {
Alan Mishchenko committed
1195
//        Gia_ManEquivImprove( pAig );
Alan Mishchenko committed
1196 1197 1198
        pNew = Gia_ManCorrReduce( pAig );
        pNew = Gia_ManSeqCleanup( pTemp = pNew );
        Gia_ManStop( pTemp );
1199
        //Gia_AigerWrite( pNew, "reduced.aig", 0, 0 );
Alan Mishchenko committed
1200
    }
Alan Mishchenko committed
1201
    // report the results
Alan Mishchenko committed
1202 1203
    if ( pPars->fVerbose )
    {
1204
        Abc_Print( 1, "NBeg = %d. NEnd = %d. (Gain = %6.2f %%).  RBeg = %d. REnd = %d. (Gain = %6.2f %%).\n", 
Alan Mishchenko committed
1205 1206 1207 1208
            Gia_ManAndNum(pAig), Gia_ManAndNum(pNew), 
            100.0*(Gia_ManAndNum(pAig)-Gia_ManAndNum(pNew))/(Gia_ManAndNum(pAig)?Gia_ManAndNum(pAig):1), 
            Gia_ManRegNum(pAig), Gia_ManRegNum(pNew), 
            100.0*(Gia_ManRegNum(pAig)-Gia_ManRegNum(pNew))/(Gia_ManRegNum(pAig)?Gia_ManRegNum(pAig):1) );
Alan Mishchenko committed
1209
    }
Alan Mishchenko committed
1210
    if ( pPars->nPrefix && (Gia_ManAndNum(pNew) < Gia_ManAndNum(pAig) || Gia_ManRegNum(pNew) < Gia_ManRegNum(pAig)) )
1211
        Abc_Print( 1, "The reduced AIG was produced using %d-th invariants and will not verify.\n", pPars->nPrefix );
Alan Mishchenko committed
1212 1213 1214 1215
    // print verbose info about equivalences
    if ( pPars->fVerboseFlops )
    {
        if ( pAig->vNamesIn == NULL )
1216
            Abc_Print( 1, "Flop output names are not available. Use command \"&get -n\".\n" );
Alan Mishchenko committed
1217 1218 1219
        else
            Cec_ManPrintFlopEquivs( pAig );
    }
Alan Mishchenko committed
1220 1221 1222 1223 1224 1225 1226 1227
    return pNew;
}

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


1228 1229
ABC_NAMESPACE_IMPL_END