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

  FileName    [fraLcorr.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [New FRAIG package.]

  Synopsis    [Latch correspondence computation.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

  Date        [Ver. 1.0. Started - June 30, 2007.]

  Revision    [$Id: fraLcorr.c,v 1.00 2007/06/30 00:00:00 alanmi Exp $]

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

#include "fra.h"

23 24 25
ABC_NAMESPACE_IMPL_START


Alan Mishchenko committed
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
////////////////////////////////////////////////////////////////////////
///                        DECLARATIONS                              ///
////////////////////////////////////////////////////////////////////////

typedef struct Fra_Lcr_t_    Fra_Lcr_t;
struct Fra_Lcr_t_
{
    // original AIG
    Aig_Man_t *      pAig;
    // equivalence class representation
    Fra_Cla_t *      pCla;       
    // partitioning information
    Vec_Ptr_t *      vParts;        // output partitions
    int *            pInToOutPart;  // mapping of PI num into PO partition num
    int *            pInToOutNum;   // mapping of PI num into the num of this PO in the partition
    // AIGs for the partitions
    Vec_Ptr_t *      vFraigs;
    // other variables
    int              fRefining; 
    // parameters
    int              nFramesP;
    int              fVerbose;
    // statistics
    int              nIters;
    int              nLitsBeg;
    int              nLitsEnd;
    int              nNodesBeg;
    int              nNodesEnd;
    int              nRegsBeg;
    int              nRegsEnd;
    // runtime
57 58 59 60 61 62
    abctime          timeSim;
    abctime          timePart;
    abctime          timeTrav;
    abctime          timeFraig;
    abctime          timeUpdate;
    abctime          timeTotal;
Alan Mishchenko committed
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
};

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

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

  Synopsis    [Allocates the retiming manager.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Fra_Lcr_t * Lcr_ManAlloc( Aig_Man_t * pAig )
{
    Fra_Lcr_t * p;
Alan Mishchenko committed
83
    p = ABC_ALLOC( Fra_Lcr_t, 1 );
Alan Mishchenko committed
84 85
    memset( p, 0, sizeof(Fra_Lcr_t) );
    p->pAig = pAig;
86 87 88 89
    p->pInToOutPart = ABC_ALLOC( int, Aig_ManCiNum(pAig) );
    memset( p->pInToOutPart, 0, sizeof(int) * Aig_ManCiNum(pAig) );
    p->pInToOutNum = ABC_ALLOC( int, Aig_ManCiNum(pAig) );
    memset( p->pInToOutNum, 0, sizeof(int) * Aig_ManCiNum(pAig) );
Alan Mishchenko committed
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
    p->vFraigs = Vec_PtrAlloc( 1000 );
    return p;
}

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

  Synopsis    [Prints stats for the fraiging manager.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Lcr_ManPrint( Fra_Lcr_t * p )
{
    printf( "Iterations = %d.  LitBeg = %d.  LitEnd = %d. (%6.2f %%).\n", 
        p->nIters, p->nLitsBeg, p->nLitsEnd, 100.0*p->nLitsEnd/p->nLitsBeg );
    printf( "NBeg = %d. NEnd = %d. (Gain = %6.2f %%).  RBeg = %d. REnd = %d. (Gain = %6.2f %%).\n", 
        p->nNodesBeg, p->nNodesEnd, 100.0*(p->nNodesBeg-p->nNodesEnd)/p->nNodesBeg, 
        p->nRegsBeg, p->nRegsEnd, 100.0*(p->nRegsBeg-p->nRegsEnd)/p->nRegsBeg );
Alan Mishchenko committed
112 113 114 115 116 117
    ABC_PRT( "AIG simulation  ", p->timeSim  );
    ABC_PRT( "AIG partitioning", p->timePart );
    ABC_PRT( "AIG rebuiding   ", p->timeTrav );
    ABC_PRT( "FRAIGing        ", p->timeFraig );
    ABC_PRT( "AIG updating    ", p->timeUpdate );
    ABC_PRT( "TOTAL RUNTIME   ", p->timeTotal );
Alan Mishchenko committed
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
}

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

  Synopsis    [Deallocates the retiming manager.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Lcr_ManFree( Fra_Lcr_t * p )
{
    Aig_Obj_t * pObj;
    int i;
    if ( p->fVerbose )
        Lcr_ManPrint( p );
137
    Aig_ManForEachCi( p->pAig, pObj, i )
Alan Mishchenko committed
138 139 140 141
        pObj->pNext = NULL;
    Vec_PtrFree( p->vFraigs );
    if ( p->pCla  )     Fra_ClassesStop( p->pCla );
    if ( p->vParts  )   Vec_VecFree( (Vec_Vec_t *)p->vParts );
Alan Mishchenko committed
142 143 144
    ABC_FREE( p->pInToOutPart );
    ABC_FREE( p->pInToOutNum );
    ABC_FREE( p );
Alan Mishchenko committed
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
}

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

  Synopsis    [Prepare the AIG for class computation.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Fra_Man_t * Fra_LcrAigPrepare( Aig_Man_t * pAig )
{
    Fra_Man_t * p;
    Aig_Obj_t * pObj;
    int i;
Alan Mishchenko committed
163
    p = ABC_ALLOC( Fra_Man_t, 1 );
Alan Mishchenko committed
164
    memset( p, 0, sizeof(Fra_Man_t) );
165
//    Aig_ManForEachCi( pAig, pObj, i )
Alan Mishchenko committed
166
    Aig_ManForEachObj( pAig, pObj, i )
Alan Mishchenko committed
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
        pObj->pData = p;
    return p;
}

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

  Synopsis    [Prepare the AIG for class computation.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Fra_LcrAigPrepareTwo( Aig_Man_t * pAig, Fra_Man_t * p )
{
    Aig_Obj_t * pObj;
    int i;
186
    Aig_ManForEachCi( pAig, pObj, i )
Alan Mishchenko committed
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
        pObj->pData = p;
}

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

  Synopsis    [Compares two nodes for equivalence after partitioned fraiging.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Fra_LcrNodesAreEqual( Aig_Obj_t * pObj0, Aig_Obj_t * pObj1 )
{
203
    Fra_Man_t * pTemp = (Fra_Man_t *)pObj0->pData;
Alan Mishchenko committed
204 205 206 207
    Fra_Lcr_t * pLcr = (Fra_Lcr_t *)pTemp->pBmc;
    Aig_Man_t * pFraig;
    Aig_Obj_t * pOut0, * pOut1;
    int nPart0, nPart1;
208 209
    assert( Aig_ObjIsCi(pObj0) );
    assert( Aig_ObjIsCi(pObj1) );
Alan Mishchenko committed
210
    // find the partition to which these nodes belong
Alan Mishchenko committed
211 212
    nPart0 = pLcr->pInToOutPart[(long)pObj0->pNext];
    nPart1 = pLcr->pInToOutPart[(long)pObj1->pNext];
Alan Mishchenko committed
213 214 215 216 217 218 219 220
    // if this is the result of refinement of the class created const-1 nodes
    // the nodes may end up in different partions - we assume them equivalent
    if ( nPart0 != nPart1 )
    {
        assert( 0 );
        return 1;
    }
    assert( nPart0 == nPart1 );
221
    pFraig = (Aig_Man_t *)Vec_PtrEntry( pLcr->vFraigs, nPart0 );
Alan Mishchenko committed
222
    // get the fraig outputs
223 224
    pOut0 = Aig_ManCo( pFraig, pLcr->pInToOutNum[(long)pObj0->pNext] );
    pOut1 = Aig_ManCo( pFraig, pLcr->pInToOutNum[(long)pObj1->pNext] );
Alan Mishchenko committed
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
    return Aig_ObjFanin0(pOut0) == Aig_ObjFanin0(pOut1);
}

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

  Synopsis    [Compares the node with a constant after partioned fraiging.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Fra_LcrNodeIsConst( Aig_Obj_t * pObj )
{
241
    Fra_Man_t * pTemp = (Fra_Man_t *)pObj->pData;
Alan Mishchenko committed
242 243 244 245
    Fra_Lcr_t * pLcr = (Fra_Lcr_t *)pTemp->pBmc;
    Aig_Man_t * pFraig;
    Aig_Obj_t * pOut;
    int nPart;
246
    assert( Aig_ObjIsCi(pObj) );
Alan Mishchenko committed
247
    // find the partition to which these nodes belong
Alan Mishchenko committed
248
    nPart = pLcr->pInToOutPart[(long)pObj->pNext];
249
    pFraig = (Aig_Man_t *)Vec_PtrEntry( pLcr->vFraigs, nPart );
Alan Mishchenko committed
250
    // get the fraig outputs
251
    pOut = Aig_ManCo( pFraig, pLcr->pInToOutNum[(long)pObj->pNext] );
Alan Mishchenko committed
252 253 254 255 256
    return Aig_ObjFanin0(pOut) == Aig_ManConst1(pFraig);
}

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

Alan Mishchenko committed
257 258 259 260 261 262 263 264 265 266 267 268 269
  Synopsis    [Duplicates the AIG manager recursively.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Aig_Obj_t * Fra_LcrManDup_rec( Aig_Man_t * pNew, Aig_Man_t * p, Aig_Obj_t * pObj )
{
    Aig_Obj_t * pObjNew;
    if ( pObj->pData )
270
        return (Aig_Obj_t *)pObj->pData;
Alan Mishchenko committed
271 272
    Fra_LcrManDup_rec( pNew, p, Aig_ObjFanin0(pObj) );
    if ( Aig_ObjIsBuf(pObj) )
273
        return (Aig_Obj_t *)(pObj->pData = Aig_ObjChild0Copy(pObj));
Alan Mishchenko committed
274 275
    Fra_LcrManDup_rec( pNew, p, Aig_ObjFanin1(pObj) );
    pObjNew = Aig_Oper( pNew, Aig_ObjChild0Copy(pObj), Aig_ObjChild1Copy(pObj), Aig_ObjType(pObj) );
276
    return (Aig_Obj_t *)(pObj->pData = pObjNew);
Alan Mishchenko committed
277 278 279 280
}

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

Alan Mishchenko committed
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
  Synopsis    [Give the AIG and classes, reduces AIG for partitioning.]

  Description [Ignores registers that are not in the classes. 
  Places candidate equivalent classes of registers into single outputs 
  (for ease of partitioning). The resulting combinational AIG contains 
  outputs in the same order as equivalence classes of registers, 
  followed by constant-1 registers. Preserves the set of all inputs.
  Complemented attributes of the outputs do not matter because we need 
  then only for collecting the structural info.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Aig_Man_t * Fra_LcrDeriveAigForPartitioning( Fra_Lcr_t * pLcr )
{
    Aig_Man_t * pNew;
    Aig_Obj_t * pObj, * pObjPo, * pObjNew, ** ppClass, * pMiter;
    int i, c, Offset;
    // remember the numbers of the inputs of the original AIG
302
    Aig_ManForEachCi( pLcr->pAig, pObj, i )
Alan Mishchenko committed
303 304
    {
        pObj->pData = pLcr;
Alan Mishchenko committed
305
        pObj->pNext = (Aig_Obj_t *)(long)i;
Alan Mishchenko committed
306 307
    }
    // compute the LO/LI offset
308
    Offset = Aig_ManCoNum(pLcr->pAig) - Aig_ManCiNum(pLcr->pAig);
Alan Mishchenko committed
309 310 311 312
    // create the PIs
    Aig_ManCleanData( pLcr->pAig );
    pNew = Aig_ManStartFrom( pLcr->pAig );
    // go over the equivalence classes
313
    Vec_PtrForEachEntry( Aig_Obj_t **, pLcr->pCla->vClasses, ppClass, i )
Alan Mishchenko committed
314 315 316 317
    {
        pMiter = Aig_ManConst0(pNew);
        for ( c = 0; ppClass[c]; c++ )
        {
318 319
            assert( Aig_ObjIsCi(ppClass[c]) );
            pObjPo  = Aig_ManCo( pLcr->pAig, Offset+(long)ppClass[c]->pNext );
Alan Mishchenko committed
320
            pObjNew = Fra_LcrManDup_rec( pNew, pLcr->pAig, Aig_ObjFanin0(pObjPo) );
Alan Mishchenko committed
321 322
            pMiter  = Aig_Exor( pNew, pMiter, pObjNew );
        }
323
        Aig_ObjCreateCo( pNew, pMiter );
Alan Mishchenko committed
324 325
    }
    // go over the constant candidates
326
    Vec_PtrForEachEntry( Aig_Obj_t *, pLcr->pCla->vClasses1, pObj, i )
Alan Mishchenko committed
327
    {
328 329
        assert( Aig_ObjIsCi(pObj) );
        pObjPo = Aig_ManCo( pLcr->pAig, Offset+(long)pObj->pNext );
Alan Mishchenko committed
330
        pMiter = Fra_LcrManDup_rec( pNew, pLcr->pAig, Aig_ObjFanin0(pObjPo) );
331
        Aig_ObjCreateCo( pNew, pMiter );
Alan Mishchenko committed
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
    }
    return pNew;
}

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

  Synopsis    [Remaps partitions into the inputs of original AIG.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Fra_LcrRemapPartitions( Vec_Ptr_t * vParts, Fra_Cla_t * pCla, int * pInToOutPart, int * pInToOutNum )
{
    Vec_Int_t * vOne, * vOneNew;
    Aig_Obj_t ** ppClass, * pObjPi;
    int Out, Offset, i, k, c;
    // compute the LO/LI offset
353
    Offset = Aig_ManCoNum(pCla->pAig) - Aig_ManCiNum(pCla->pAig);
354
    Vec_PtrForEachEntry( Vec_Int_t *, vParts, vOne, i )
Alan Mishchenko committed
355 356 357 358 359 360
    {
        vOneNew = Vec_IntAlloc( Vec_IntSize(vOne) );
        Vec_IntForEachEntry( vOne, Out, k )
        {
            if ( Out < Vec_PtrSize(pCla->vClasses) )
            {
361
                ppClass = (Aig_Obj_t **)Vec_PtrEntry( pCla->vClasses, Out );
Alan Mishchenko committed
362 363
                for ( c = 0; ppClass[c]; c++ )
                {
Alan Mishchenko committed
364 365 366
                    pInToOutPart[(long)ppClass[c]->pNext] = i;
                    pInToOutNum[(long)ppClass[c]->pNext] = Vec_IntSize(vOneNew);
                    Vec_IntPush( vOneNew, Offset+(long)ppClass[c]->pNext );
Alan Mishchenko committed
367 368 369 370
                }
            }
            else
            {
371
                pObjPi = (Aig_Obj_t *)Vec_PtrEntry( pCla->vClasses1, Out - Vec_PtrSize(pCla->vClasses) );
Alan Mishchenko committed
372 373 374
                pInToOutPart[(long)pObjPi->pNext] = i;
                pInToOutNum[(long)pObjPi->pNext] = Vec_IntSize(vOneNew);
                Vec_IntPush( vOneNew, Offset+(long)pObjPi->pNext );
Alan Mishchenko committed
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
            }
        }
        // replace the class
        Vec_PtrWriteEntry( vParts, i, vOneNew );
        Vec_IntFree( vOne );
    }
}

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

  Synopsis    [Creates AIG of one partition with speculative reduction.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Aig_Obj_t * Fra_LcrCreatePart_rec( Fra_Cla_t * pCla, Aig_Man_t * pNew, Aig_Man_t * p, Aig_Obj_t * pObj )
{
    assert( !Aig_IsComplement(pObj) );
    if ( Aig_ObjIsTravIdCurrent(p, pObj) )
398
        return (Aig_Obj_t *)pObj->pData;
Alan Mishchenko committed
399
    Aig_ObjSetTravIdCurrent(p, pObj);
400
    if ( Aig_ObjIsCi(pObj) )
Alan Mishchenko committed
401 402 403 404
    {
//        Aig_Obj_t * pRepr = Fra_ClassObjRepr(pObj);
        Aig_Obj_t * pRepr = pCla->pMemRepr[pObj->Id];
        if ( pRepr == NULL )
405
            pObj->pData = Aig_ObjCreateCi( pNew );
Alan Mishchenko committed
406 407 408
        else
        {
            pObj->pData = Fra_LcrCreatePart_rec( pCla, pNew, p, pRepr );
409
            pObj->pData = Aig_NotCond( (Aig_Obj_t *)pObj->pData, pRepr->fPhase ^ pObj->fPhase );
Alan Mishchenko committed
410
        }
411
        return (Aig_Obj_t *)pObj->pData;
Alan Mishchenko committed
412 413 414
    }
    Fra_LcrCreatePart_rec( pCla, pNew, p, Aig_ObjFanin0(pObj) );
    Fra_LcrCreatePart_rec( pCla, pNew, p, Aig_ObjFanin1(pObj) );
415
    return (Aig_Obj_t *)(pObj->pData = Aig_And( pNew, Aig_ObjChild0Copy(pObj), Aig_ObjChild1Copy(pObj) ));
Alan Mishchenko committed
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
}

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

  Synopsis    [Creates AIG of one partition with speculative reduction.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Aig_Man_t * Fra_LcrCreatePart( Fra_Lcr_t * p, Vec_Int_t * vPart )
{
    Aig_Man_t * pNew;
    Aig_Obj_t * pObj, * pObjNew;
    int Out, i;
    // create new AIG for this partition
    pNew = Aig_ManStartFrom( p->pAig );
    Aig_ManIncrementTravId( p->pAig );
    Aig_ObjSetTravIdCurrent( p->pAig, Aig_ManConst1(p->pAig) );
    Aig_ManConst1(p->pAig)->pData = Aig_ManConst1(pNew);
    Vec_IntForEachEntry( vPart, Out, i )
    {
441
        pObj = Aig_ManCo( p->pAig, Out );
Alan Mishchenko committed
442 443 444 445 446 447 448
        if ( pObj->fMarkA )
        {
            pObjNew = Fra_LcrCreatePart_rec( p->pCla, pNew, p->pAig, Aig_ObjFanin0(pObj) );
            pObjNew = Aig_NotCond( pObjNew, Aig_ObjFaninC0(pObj) );
        }
        else
            pObjNew = Aig_ManConst1( pNew );
449
        Aig_ObjCreateCo( pNew, pObjNew ); 
Alan Mishchenko committed
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469
    }
    return pNew;
}

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

  Synopsis    [Marks the nodes belonging to the equivalence classes.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Fra_ClassNodesMark( Fra_Lcr_t * p )
{
    Aig_Obj_t * pObj, ** ppClass;
    int i, c, Offset;
    // compute the LO/LI offset
470
    Offset = Aig_ManCoNum(p->pCla->pAig) - Aig_ManCiNum(p->pCla->pAig);
Alan Mishchenko committed
471
    // mark the nodes remaining in the classes
472
    Vec_PtrForEachEntry( Aig_Obj_t *, p->pCla->vClasses1, pObj, i )
Alan Mishchenko committed
473
    {
474
        pObj = Aig_ManCo( p->pCla->pAig, Offset+(long)pObj->pNext );
Alan Mishchenko committed
475 476
        pObj->fMarkA = 1;
    }
477
    Vec_PtrForEachEntry( Aig_Obj_t **, p->pCla->vClasses, ppClass, i )
Alan Mishchenko committed
478 479 480
    {
        for ( c = 0; ppClass[c]; c++ )
        {
481
            pObj = Aig_ManCo( p->pCla->pAig, Offset+(long)ppClass[c]->pNext );
Alan Mishchenko committed
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502
            pObj->fMarkA = 1;
        }
    }
}

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

  Synopsis    [Unmarks the nodes belonging to the equivalence classes.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Fra_ClassNodesUnmark( Fra_Lcr_t * p )
{
    Aig_Obj_t * pObj, ** ppClass;
    int i, c, Offset;
    // compute the LO/LI offset
503
    Offset = Aig_ManCoNum(p->pCla->pAig) - Aig_ManCiNum(p->pCla->pAig);
Alan Mishchenko committed
504
    // mark the nodes remaining in the classes
505
    Vec_PtrForEachEntry( Aig_Obj_t *, p->pCla->vClasses1, pObj, i )
Alan Mishchenko committed
506
    {
507
        pObj = Aig_ManCo( p->pCla->pAig, Offset+(long)pObj->pNext );
Alan Mishchenko committed
508 509
        pObj->fMarkA = 0;
    }
510
    Vec_PtrForEachEntry( Aig_Obj_t **, p->pCla->vClasses, ppClass, i )
Alan Mishchenko committed
511 512 513
    {
        for ( c = 0; ppClass[c]; c++ )
        {
514
            pObj = Aig_ManCo( p->pCla->pAig, Offset+(long)ppClass[c]->pNext );
Alan Mishchenko committed
515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530
            pObj->fMarkA = 0;
        }
    }
}

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

  Synopsis    [Performs choicing of the AIG.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
531
Aig_Man_t * Fra_FraigLatchCorrespondence( Aig_Man_t * pAig, int nFramesP, int nConfMax, int fProve, int fVerbose, int * pnIter, float TimeLimit )
Alan Mishchenko committed
532 533 534 535
{
    int nPartSize    = 200;
    int fReprSelect  = 0;
    Fra_Lcr_t * p;
Alan Mishchenko committed
536
    Fra_Sml_t * pSml;
Alan Mishchenko committed
537
    Fra_Man_t * pTemp;
Alan Mishchenko committed
538
    Aig_Man_t * pAigPart, * pAigTemp, * pAigNew = NULL;
Alan Mishchenko committed
539
    Vec_Int_t * vPart;
540
    int i, nIter;
541 542
    abctime timeSim, clk2, clk3, clk = Abc_Clock();
    abctime TimeToStop = TimeLimit ? TimeLimit * CLOCKS_PER_SEC + Abc_Clock() : 0;
Alan Mishchenko committed
543 544 545
    if ( Aig_ManNodeNum(pAig) == 0 )
    {
        if ( pnIter ) *pnIter = 0;
Alan Mishchenko committed
546 547
        // Ntl_ManFinalize() requires the following to satisfy an assertion.
        Aig_ManReprStart(pAig,Aig_ManObjNumMax(pAig));
Alan Mishchenko committed
548
        return Aig_ManDupOrdered(pAig);
Alan Mishchenko committed
549
    }
Alan Mishchenko committed
550
    assert( Aig_ManRegNum(pAig) > 0 ); 
Alan Mishchenko committed
551 552

    // simulate the AIG 
553
clk2 = Abc_Clock();
Alan Mishchenko committed
554 555
if ( fVerbose )
printf( "Simulating AIG with %d nodes for %d cycles ...  ", Aig_ManNodeNum(pAig), nFramesP + 32 );
Alan Mishchenko committed
556
    pSml = Fra_SmlSimulateSeq( pAig, nFramesP, 32, 1, 1  ); 
Alan Mishchenko committed
557 558
if ( fVerbose ) 
{
559
ABC_PRT( "Time", Abc_Clock() - clk2 );
Alan Mishchenko committed
560
}
561
timeSim = Abc_Clock() - clk2;
Alan Mishchenko committed
562

Alan Mishchenko committed
563 564 565
    // check if simulation discovered non-constant-0 POs
    if ( fProve && pSml->fNonConstOut )
    {
Alan Mishchenko committed
566
        pAig->pSeqModel = Fra_SmlGetCounterExample( pSml );
Alan Mishchenko committed
567 568 569 570 571 572 573 574 575 576 577 578 579 580
        Fra_SmlStop( pSml );
        return NULL;
    }

    // start the manager
    p = Lcr_ManAlloc( pAig );
    p->nFramesP = nFramesP;
    p->fVerbose = fVerbose;
    p->timeSim += timeSim;

    pTemp = Fra_LcrAigPrepare( pAig );
    pTemp->pBmc = (Fra_Bmc_t *)p;
    pTemp->pSml = pSml;

Alan Mishchenko committed
581 582
    // get preliminary info about equivalence classes
    pTemp->pCla = p->pCla = Fra_ClassesStart( p->pAig );
Alan Mishchenko committed
583
    Fra_ClassesPrepare( p->pCla, 1, 0 );
Alan Mishchenko committed
584 585 586 587 588
    p->pCla->pFuncNodeIsConst   = Fra_LcrNodeIsConst;
    p->pCla->pFuncNodesAreEqual = Fra_LcrNodesAreEqual;
    Fra_SmlStop( pTemp->pSml );

    // partition the AIG for latch correspondence computation
589
clk2 = Abc_Clock();
Alan Mishchenko committed
590 591 592 593 594 595 596 597
if ( fVerbose )
printf( "Partitioning AIG ...  " );
    pAigPart = Fra_LcrDeriveAigForPartitioning( p );
    p->vParts = (Vec_Ptr_t *)Aig_ManPartitionSmart( pAigPart, nPartSize, 0, NULL );
    Fra_LcrRemapPartitions( p->vParts, p->pCla, p->pInToOutPart, p->pInToOutNum );
    Aig_ManStop( pAigPart );
if ( fVerbose ) 
{
598 599
ABC_PRT( "Time", Abc_Clock() - clk2 );
p->timePart += Abc_Clock() - clk2;
Alan Mishchenko committed
600 601 602 603 604 605 606 607 608 609 610 611
}

    // get the initial stats
    p->nLitsBeg  = Fra_ClassesCountLits( p->pCla );
    p->nNodesBeg = Aig_ManNodeNum(p->pAig);
    p->nRegsBeg  = Aig_ManRegNum(p->pAig);

    // perforn interative reduction of the partitions
    p->fRefining = 1;
    for ( nIter = 0; p->fRefining; nIter++ )
    {
        p->fRefining = 0;
612
        clk3 = Abc_Clock();
Alan Mishchenko committed
613 614 615
        // derive AIGs for each partition
        Fra_ClassNodesMark( p );
        Vec_PtrClear( p->vFraigs );
616
        Vec_PtrForEachEntry( Vec_Int_t *, p->vParts, vPart, i )
Alan Mishchenko committed
617
        {
618
            if ( TimeLimit != 0.0 && Abc_Clock() > TimeToStop )
Alan Mishchenko committed
619
            {
620
                Vec_PtrForEachEntry( Aig_Man_t *, p->vFraigs, pAigPart, i )
Alan Mishchenko committed
621 622 623 624 625 626
                    Aig_ManStop( pAigPart );
                Aig_ManCleanMarkA( pAig );
                Aig_ManCleanMarkB( pAig );
                printf( "Fra_FraigLatchCorrespondence(): Runtime limit exceeded.\n" );
                goto finish;
            }
627
clk2 = Abc_Clock();
Alan Mishchenko committed
628
            pAigPart = Fra_LcrCreatePart( p, vPart );
629 630
p->timeTrav += Abc_Clock() - clk2;
clk2 = Abc_Clock();
Alan Mishchenko committed
631
            pAigTemp  = Fra_FraigEquivence( pAigPart, nConfMax, 0 );
632
p->timeFraig += Abc_Clock() - clk2;
Alan Mishchenko committed
633
            Vec_PtrPush( p->vFraigs, pAigTemp );
Alan Mishchenko committed
634
/*
Alan Mishchenko committed
635 636 637 638 639 640
            {
                char Name[1000];
                sprintf( Name, "part%04d.blif", i );
                Aig_ManDumpBlif( pAigPart, Name, NULL, NULL );
            }
printf( "Finished part %4d (out of %4d).  ", i, Vec_PtrSize(p->vParts) );
641
ABC_PRT( "Time", Abc_Clock() - clk3 );
Alan Mishchenko committed
642
*/
Alan Mishchenko committed
643

Alan Mishchenko committed
644
            Aig_ManStop( pAigPart );
Alan Mishchenko committed
645 646 647 648 649 650 651 652
        }
        Fra_ClassNodesUnmark( p );
        // report the intermediate results
        if ( fVerbose )
        {
            printf( "%3d : Const = %6d. Class = %6d.  L = %6d. Part = %3d.  ", 
                nIter, Vec_PtrSize(p->pCla->vClasses1), Vec_PtrSize(p->pCla->vClasses), 
                Fra_ClassesCountLits(p->pCla), Vec_PtrSize(p->vParts) );
653
            ABC_PRT( "T", Abc_Clock() - clk3 );
Alan Mishchenko committed
654 655 656 657 658 659 660 661
        }
        // refine the classes
        Fra_LcrAigPrepareTwo( p->pAig, pTemp );
        if ( Fra_ClassesRefine( p->pCla ) )
            p->fRefining = 1;
        if ( Fra_ClassesRefine1( p->pCla, 0, NULL ) )
            p->fRefining = 1;
        // clean the fraigs
662
        Vec_PtrForEachEntry( Aig_Man_t *, p->vFraigs, pAigPart, i )
Alan Mishchenko committed
663 664 665 666 667
            Aig_ManStop( pAigPart );

        // repartition if needed
        if ( 1 )
        {
668
clk2 = Abc_Clock();
Alan Mishchenko committed
669 670 671 672 673
            Vec_VecFree( (Vec_Vec_t *)p->vParts );
            pAigPart = Fra_LcrDeriveAigForPartitioning( p );
            p->vParts = (Vec_Ptr_t *)Aig_ManPartitionSmart( pAigPart, nPartSize, 0, NULL );
            Fra_LcrRemapPartitions( p->vParts, p->pCla, p->pInToOutPart, p->pInToOutNum );
            Aig_ManStop( pAigPart );
674
p->timePart += Abc_Clock() - clk2;
Alan Mishchenko committed
675 676 677 678 679
        }
    }
    p->nIters = nIter;

    // move the classes into representatives and reduce AIG
680
clk2 = Abc_Clock();
Alan Mishchenko committed
681 682 683 684
//    Fra_ClassesPrint( p->pCla, 1 );
    if ( fReprSelect )
        Fra_ClassesSelectRepr( p->pCla );
    Fra_ClassesCopyReprs( p->pCla, NULL );
Alan Mishchenko committed
685
    pAigNew = Aig_ManDupRepr( p->pAig, 0 );
Alan Mishchenko committed
686 687
    Aig_ManSeqCleanup( pAigNew );
//    Aig_ManCountMergeRegs( pAigNew );
688 689
p->timeUpdate += Abc_Clock() - clk2;
p->timeTotal = Abc_Clock() - clk;
Alan Mishchenko committed
690 691 692 693
    // get the final stats
    p->nLitsEnd  = Fra_ClassesCountLits( p->pCla );
    p->nNodesEnd = Aig_ManNodeNum(pAigNew);
    p->nRegsEnd  = Aig_ManRegNum(pAigNew);
Alan Mishchenko committed
694
finish:
Alan Mishchenko committed
695
    ABC_FREE( pTemp );
Alan Mishchenko committed
696 697 698 699 700 701 702 703 704 705 706
    Lcr_ManFree( p );
    if ( pnIter ) *pnIter = nIter;
    return pAigNew;
}


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


707 708
ABC_NAMESPACE_IMPL_END