abcHaig.c 20.8 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    [abcHaig.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [Network and node package.]

  Synopsis    [Implements history AIG for combinational rewriting.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

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

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

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

#include "abc.h"

23 24 25
ABC_NAMESPACE_IMPL_START


Alan Mishchenko committed
26 27 28 29 30 31 32 33
////////////////////////////////////////////////////////////////////////
///                        DECLARATIONS                              ///
////////////////////////////////////////////////////////////////////////

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

Alan Mishchenko committed
34 35
#if 0

Alan Mishchenko committed
36 37
/**Function*************************************************************

Alan Mishchenko committed
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
  Synopsis    [Start history AIG.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Abc_NtkHaigStart( Abc_Ntk_t * pNtk )
{
    Hop_Man_t * p;
    Abc_Obj_t * pObj, * pTemp;
    int i;
    assert( Abc_NtkIsStrash(pNtk) );
    // check if the package is already started
    if ( pNtk->pHaig )
    {
        Abc_NtkHaigStop( pNtk );
        assert( pNtk->pHaig == NULL );
        printf( "Warning: Previous history AIG was removed.\n" );
    }
    // make sure the data is clean
    Abc_NtkForEachObj( pNtk, pObj, i )
        assert( pObj->pEquiv == NULL );
    // start the HOP package
    p = Hop_ManStart();
    p->vObjs = Vec_PtrAlloc( 4096 );
    Vec_PtrPush( p->vObjs, Hop_ManConst1(p) );
    // map the constant node
    Abc_AigConst1(pNtk)->pEquiv = Hop_ManConst1(p);
    // map the CIs
    Abc_NtkForEachCi( pNtk, pObj, i )
        pObj->pEquiv = Hop_ObjCreatePi(p);
    // map the internal nodes
    Abc_NtkForEachNode( pNtk, pObj, i )
        pObj->pEquiv = Hop_And( p, Abc_ObjChild0Equiv(pObj), Abc_ObjChild1Equiv(pObj) );
    // map the choice nodes
    if ( Abc_NtkGetChoiceNum( pNtk ) )
    {
        // print warning about choice nodes
        printf( "Warning: The choice nodes in the original AIG are converted into HAIG.\n" );
        Abc_NtkForEachNode( pNtk, pObj, i )
        {
            if ( !Abc_AigNodeIsChoice( pObj ) )
                continue;
            for ( pTemp = pObj->pData; pTemp; pTemp = pTemp->pData )
                Hop_ObjCreateChoice( pObj->pEquiv, pTemp->pEquiv );
        }
    }
    // make sure everything is okay
    if ( !Hop_ManCheck(p) )
    {
        printf( "Abc_NtkHaigStart: Check for History AIG has failed.\n" );
        Hop_ManStop(p);
        return 0;
    }
    pNtk->pHaig = p;
    return 1;
}

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

  Synopsis    [Stops history AIG.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Abc_NtkHaigStop( Abc_Ntk_t * pNtk )
{
    Abc_Obj_t * pObj;
    int i;
    assert( Abc_NtkIsStrash(pNtk) );
    if ( pNtk->pHaig == NULL )
    {
        printf( "Warning: History AIG is not allocated.\n" );
        return 1;
    }
    Abc_NtkForEachObj( pNtk, pObj, i )
        pObj->pEquiv = NULL;
    Hop_ManStop( pNtk->pHaig );
    pNtk->pHaig = NULL;
    return 1;
}

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

  Synopsis    [Transfers the HAIG to the new network.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Abc_NtkHaigTranfer( Abc_Ntk_t * pNtkOld, Abc_Ntk_t * pNtkNew )
{
    Abc_Obj_t * pObj;
    int i;
    if ( pNtkOld->pHaig == NULL )
        return;
    // transfer the package
    assert( pNtkNew->pHaig == NULL );
    pNtkNew->pHaig = pNtkOld->pHaig;
    pNtkOld->pHaig = NULL;
    // transfer constant pointer
    Abc_AigConst1(pNtkOld)->pCopy->pEquiv = Abc_AigConst1(pNtkOld)->pEquiv;
    // transfer the CI pointers
    Abc_NtkForEachCi( pNtkOld, pObj, i )
        pObj->pCopy->pEquiv = pObj->pEquiv;
}


Alan Mishchenko committed
156
#endif
Alan Mishchenko committed
157 158 159

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

Alan Mishchenko committed
160 161 162 163 164 165 166 167 168 169 170
  Synopsis    [Collects the nodes in the classes.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Vec_Ptr_t * Abc_NtkHaigCollectMembers( Hop_Man_t * p )
{
Alan Mishchenko committed
171
    Vec_Ptr_t * vObjs;
Alan Mishchenko committed
172 173
    Hop_Obj_t * pObj;
    int i;
Alan Mishchenko committed
174
    vObjs = Vec_PtrAlloc( 4098 );
175
    Vec_PtrForEachEntry( Hop_Obj_t *, p->vObjs, pObj, i )
Alan Mishchenko committed
176 177 178 179
    {
        if ( pObj->pData == NULL )
            continue;
        pObj->pData = Hop_ObjRepr( pObj );
Alan Mishchenko committed
180
        Vec_PtrPush( vObjs, pObj );
Alan Mishchenko committed
181
    }
Alan Mishchenko committed
182
    return vObjs;
Alan Mishchenko committed
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
}

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

  Synopsis    [Creates classes.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Vec_Ptr_t * Abc_NtkHaigCreateClasses( Vec_Ptr_t * vMembers )
{
    Vec_Ptr_t * vClasses;
    Hop_Obj_t * pObj, * pRepr;
    int i;

    // count classes
    vClasses = Vec_PtrAlloc( 4098 );
204
    Vec_PtrForEachEntry( Hop_Obj_t *, vMembers, pObj, i )
Alan Mishchenko committed
205
    {
206
        pRepr = (Hop_Obj_t *)pObj->pData;
Alan Mishchenko committed
207 208 209 210 211 212 213 214 215
        assert( pRepr->pData == NULL );
        if ( pRepr->fMarkA == 0 ) // new
        {
            pRepr->fMarkA = 1;
            Vec_PtrPush( vClasses, pRepr );
        }
    }

    // set representatives as representatives
216
    Vec_PtrForEachEntry( Hop_Obj_t *, vClasses, pObj, i )
Alan Mishchenko committed
217 218 219 220 221 222
    {
        pObj->fMarkA = 0;
        pObj->pData = pObj;
    }

    // go through the members and update
223
    Vec_PtrForEachEntry( Hop_Obj_t *, vMembers, pObj, i )
Alan Mishchenko committed
224
    {
225
        pRepr = (Hop_Obj_t *)pObj->pData;
Alan Mishchenko committed
226 227 228 229 230
        if ( ((Hop_Obj_t *)pRepr->pData)->Id > pObj->Id )
            pRepr->pData = pObj;
    }

    // change representatives of the class
231
    Vec_PtrForEachEntry( Hop_Obj_t *, vMembers, pObj, i )
Alan Mishchenko committed
232
    {
233
        pRepr = (Hop_Obj_t *)pObj->pData;
Alan Mishchenko committed
234 235 236 237 238
        pObj->pData = pRepr->pData;
        assert( ((Hop_Obj_t *)pObj->pData)->Id <= pObj->Id );
    }

    // update classes
239
    Vec_PtrForEachEntry( Hop_Obj_t *, vClasses, pObj, i )
Alan Mishchenko committed
240
    {
241
        pRepr = (Hop_Obj_t *)pObj->pData;
Alan Mishchenko committed
242
        assert( pRepr->pData == pRepr );
Alan Mishchenko committed
243
//        pRepr->pData = NULL;
Alan Mishchenko committed
244 245 246
        Vec_PtrWriteEntry( vClasses, i, pRepr );
        Vec_PtrPush( vMembers, pObj );
    }
Alan Mishchenko committed
247

248
    Vec_PtrForEachEntry( Hop_Obj_t *, vMembers, pObj, i )
Alan Mishchenko committed
249 250 251
        if ( pObj->pData == pObj )
            pObj->pData = NULL;

Alan Mishchenko committed
252
/*
253
    Vec_PtrForEachEntry( Hop_Obj_t *, vMembers, pObj, i )
Alan Mishchenko committed
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
    {
        printf( "ObjId = %4d : ", pObj->Id );
        if ( pObj->pData == NULL )
        {
            printf( "NULL" );
        }
        else
        {
            printf( "%4d", ((Hop_Obj_t *)pObj->pData)->Id );
            assert( ((Hop_Obj_t *)pObj->pData)->Id <= pObj->Id );
        }
        printf( "\n" );
    }
*/
    return vClasses;
}

Alan Mishchenko committed
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
/**Function*************************************************************

  Synopsis    [Counts how many data members have non-trivial fanout.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Abc_NtkHaigCountFans( Hop_Man_t * p )
{
    Hop_Obj_t * pObj;
    int i, Counter = 0;
286
    Vec_PtrForEachEntry( Hop_Obj_t *, p->vObjs, pObj, i )
Alan Mishchenko committed
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
    {
        if ( pObj->pData == NULL )
            continue;
        if ( Hop_ObjRefs(pObj) > 0 )
            Counter++;
    }
    printf( "The number of class members with fanouts = %5d.\n", Counter );
    return Counter;
}


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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline Hop_Obj_t * Hop_ObjReprHop( Hop_Obj_t * pObj )
{
    Hop_Obj_t * pRepr;
    assert( pObj->pNext != NULL );
    if ( pObj->pData == NULL )
        return pObj->pNext;
315
    pRepr = (Hop_Obj_t *)pObj->pData;
Alan Mishchenko committed
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
    assert( pRepr->pData == pRepr );
    return Hop_NotCond( pRepr->pNext, pObj->fPhase ^ pRepr->fPhase );
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline Hop_Obj_t * Hop_ObjChild0Hop( Hop_Obj_t * pObj ) { return Hop_NotCond( Hop_ObjReprHop(Hop_ObjFanin0(pObj)), Hop_ObjFaninC0(pObj) ); }
static inline Hop_Obj_t * Hop_ObjChild1Hop( Hop_Obj_t * pObj ) { return Hop_NotCond( Hop_ObjReprHop(Hop_ObjFanin1(pObj)), Hop_ObjFaninC1(pObj) ); }

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

  Synopsis    [Stops history AIG.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Hop_Man_t * Abc_NtkHaigReconstruct( Hop_Man_t * p )
{ 
    Hop_Man_t * pNew;
    Hop_Obj_t * pObj;
    int i, Counter = 0;
350
    Vec_PtrForEachEntry( Hop_Obj_t *, p->vObjs, pObj, i )
Alan Mishchenko committed
351 352 353 354 355 356 357 358 359 360 361
        pObj->pNext = NULL;
    // start the HOP package
    pNew = Hop_ManStart();
    pNew->vObjs = Vec_PtrAlloc( p->nCreated );
    Vec_PtrPush( pNew->vObjs, Hop_ManConst1(pNew) );
    // map the constant node
    Hop_ManConst1(p)->pNext = Hop_ManConst1(pNew);
    // map the CIs
    Hop_ManForEachPi( p, pObj, i )
        pObj->pNext = Hop_ObjCreatePi(pNew);
    // map the internal nodes
362
    Vec_PtrForEachEntry( Hop_Obj_t *, p->vObjs, pObj, i )
Alan Mishchenko committed
363 364 365 366 367 368 369 370 371 372
    {
        if ( !Hop_ObjIsNode(pObj) )
            continue;
        pObj->pNext = Hop_And( pNew, Hop_ObjChild0Hop(pObj), Hop_ObjChild1Hop(pObj) );
//        assert( !Hop_IsComplement(pObj->pNext) );
        if ( Hop_ManConst1(pNew) == Hop_Regular(pObj->pNext) )
            Counter++;
        if ( pObj->pData ) // member of the class
            Hop_Regular(pObj->pNext)->pData = Hop_Regular(((Hop_Obj_t *)pObj->pData)->pNext);
    }
Alan Mishchenko committed
373
//    printf( " Counter = %d.\n", Counter );
Alan Mishchenko committed
374 375 376 377 378 379 380 381 382 383 384 385 386
    // transfer the POs
    Hop_ManForEachPo( p, pObj, i )
        Hop_ObjCreatePo( pNew, Hop_ObjChild0Hop(pObj) );
    // check the new manager
    if ( !Hop_ManCheck(pNew) )
    {
        printf( "Abc_NtkHaigReconstruct: Check for History AIG has failed.\n" );
        Hop_ManStop(pNew);
        return NULL;
    }
    return pNew;
}

Alan Mishchenko committed
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405

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

  Synopsis    [Returns 1 if pOld is in the TFI of pNew.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Abc_NtkHaigCheckTfi_rec( Abc_Obj_t * pNode, Abc_Obj_t * pOld )
{
    if ( pNode == NULL )
        return 0;
    if ( pNode == pOld )
        return 1;
    // check the trivial cases
Alan Mishchenko committed
406
    if ( Abc_ObjIsCi(pNode) )
Alan Mishchenko committed
407 408 409 410 411 412 413 414 415 416 417 418 419
        return 0;
    assert( Abc_ObjIsNode(pNode) );
    // if this node is already visited, skip
    if ( Abc_NodeIsTravIdCurrent( pNode ) )
        return 0;
    // mark the node as visited
    Abc_NodeSetTravIdCurrent( pNode );
    // check the children
    if ( Abc_NtkHaigCheckTfi_rec( Abc_ObjFanin0(pNode), pOld ) )
        return 1;
    if ( Abc_NtkHaigCheckTfi_rec( Abc_ObjFanin1(pNode), pOld ) )
        return 1;
    // check equivalent nodes
420
    return Abc_NtkHaigCheckTfi_rec( (Abc_Obj_t *)pNode->pData, pOld );
Alan Mishchenko committed
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
}

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

  Synopsis    [Returns 1 if pOld is in the TFI of pNew.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Abc_NtkHaigCheckTfi( Abc_Ntk_t * pNtk, Abc_Obj_t * pOld, Abc_Obj_t * pNew )
{
    assert( !Abc_ObjIsComplement(pOld) );
    assert( !Abc_ObjIsComplement(pNew) );
    Abc_NtkIncrementTravId(pNtk);
    return Abc_NtkHaigCheckTfi_rec( pNew, pOld );
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
453 454
static inline Abc_Obj_t * Hop_ObjChild0Next( Hop_Obj_t * pObj ) { return Abc_ObjNotCond( (Abc_Obj_t *)Hop_ObjFanin0(pObj)->pNext, Hop_ObjFaninC0(pObj) );  }
static inline Abc_Obj_t * Hop_ObjChild1Next( Hop_Obj_t * pObj ) { return Abc_ObjNotCond( (Abc_Obj_t *)Hop_ObjFanin1(pObj)->pNext, Hop_ObjFaninC1(pObj) );  }
Alan Mishchenko committed
455 456 457 458 459 460 461 462 463 464 465 466 467 468 469

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

  Synopsis    [Stops history AIG.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Abc_Ntk_t * Abc_NtkHaigRecreateAig( Abc_Ntk_t * pNtk, Hop_Man_t * p )
{
    Abc_Ntk_t * pNtkAig;
Alan Mishchenko committed
470
    Abc_Obj_t * pObjOld, * pObjAbcThis, * pObjAbcRepr;
Alan Mishchenko committed
471 472
    Hop_Obj_t * pObj;
    int i;
Alan Mishchenko committed
473
    assert( p->nCreated == Vec_PtrSize(p->vObjs) );
Alan Mishchenko committed
474 475

    // start the new network
Alan Mishchenko committed
476
    pNtkAig = Abc_NtkStartFrom( pNtk, ABC_NTK_STRASH, ABC_FUNC_AIG );
Alan Mishchenko committed
477 478

    // transfer new nodes to the PIs of HOP
Alan Mishchenko committed
479
    Hop_ManConst1(p)->pNext = (Hop_Obj_t *)Abc_AigConst1( pNtkAig );
Alan Mishchenko committed
480
    Hop_ManForEachPi( p, pObj, i )
Alan Mishchenko committed
481
        pObj->pNext = (Hop_Obj_t *)Abc_NtkCi( pNtkAig, i );
Alan Mishchenko committed
482 483

    // construct new nodes
484
    Vec_PtrForEachEntry( Hop_Obj_t *, p->vObjs, pObj, i )
Alan Mishchenko committed
485 486 487
    {
        if ( !Hop_ObjIsNode(pObj) )
            continue;
488
        pObj->pNext = (Hop_Obj_t *)Abc_AigAnd( (Abc_Aig_t *)pNtkAig->pManFunc, Hop_ObjChild0Next(pObj), Hop_ObjChild1Next(pObj) );
Alan Mishchenko committed
489 490
        assert( !Hop_IsComplement(pObj->pNext) );
    }
Alan Mishchenko committed
491 492 493

    // set the COs
    Abc_NtkForEachCo( pNtk, pObjOld, i )
Alan Mishchenko committed
494
        Abc_ObjAddFanin( pObjOld->pCopy, Hop_ObjChild0Next(Hop_ManPo(p,i)) );
Alan Mishchenko committed
495

Alan Mishchenko committed
496
    // construct choice nodes
497
    Vec_PtrForEachEntry( Hop_Obj_t *, p->vObjs, pObj, i )
Alan Mishchenko committed
498
    {
Alan Mishchenko committed
499
        // skip the node without choices
Alan Mishchenko committed
500 501
        if ( pObj->pData == NULL )
            continue;
Alan Mishchenko committed
502 503
        // skip the representative of the class
        if ( pObj->pData == pObj )
Alan Mishchenko committed
504
            continue;
Alan Mishchenko committed
505
        // do not create choices for constant 1 and PIs
506
        if ( !Hop_ObjIsNode((Hop_Obj_t *)pObj->pData) )
Alan Mishchenko committed
507
            continue;
Alan Mishchenko committed
508 509 510 511 512 513 514 515 516
        // get the corresponding new nodes
        pObjAbcThis = (Abc_Obj_t *)pObj->pNext;
        pObjAbcRepr = (Abc_Obj_t *)((Hop_Obj_t *)pObj->pData)->pNext;
        // the new node cannot be already in the class
        assert( pObjAbcThis->pData == NULL );
        // the new node cannot have fanouts
        assert( Abc_ObjFanoutNum(pObjAbcThis) == 0 );
        // these should be different nodes
        assert( pObjAbcRepr != pObjAbcThis );
Alan Mishchenko committed
517 518 519 520 521
        // do not create choices if there is a path from pObjAbcThis to pObjAbcRepr
        if ( !Abc_NtkHaigCheckTfi( pNtkAig, pObjAbcRepr, pObjAbcThis ) )
        {
            // find the last node in the class
            while ( pObjAbcRepr->pData )
522
                pObjAbcRepr = (Abc_Obj_t *)pObjAbcRepr->pData;
Alan Mishchenko committed
523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542
            // add the new node at the end of the list
            pObjAbcRepr->pData = pObjAbcThis;
        }
    }

    // finish the new network
//    Abc_NtkFinalize( pNtk, pNtkAig );
//    Abc_AigCleanup( pNtkAig->pManFunc );
    // check correctness of the network
    if ( !Abc_NtkCheck( pNtkAig ) )
    {
        printf( "Abc_NtkHaigUse: The network check has failed.\n" );
        Abc_NtkDelete( pNtkAig );
        return NULL;
    }
    return pNtkAig;
}

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

Alan Mishchenko committed
543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 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
  Synopsis    [Resets representatives.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Abc_NtkHaigResetReprsOld( Hop_Man_t * pMan )
{
    Vec_Ptr_t * vMembers, * vClasses;

    // collect members of the classes and make them point to reprs
    vMembers = Abc_NtkHaigCollectMembers( pMan );
    printf( "Collected %6d class members.\n", Vec_PtrSize(vMembers) );

    // create classes
    vClasses = Abc_NtkHaigCreateClasses( vMembers );
    printf( "Collected %6d classes. (Ave = %5.2f)\n", Vec_PtrSize(vClasses), 
        (float)(Vec_PtrSize(vMembers))/Vec_PtrSize(vClasses) );

    Vec_PtrFree( vMembers );
    Vec_PtrFree( vClasses );
}

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

  Synopsis    [Resets representatives.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Abc_NtkHaigResetReprs( Hop_Man_t * p )
{
    Hop_Obj_t * pObj, * pRepr;
    int i, nClasses, nMembers, nFanouts, nNormals;
    // clear self-classes
585
    Vec_PtrForEachEntry( Hop_Obj_t *, p->vObjs, pObj, i )
Alan Mishchenko committed
586 587
    {
        // fix the strange situation of double-loop
588
        pRepr = (Hop_Obj_t *)pObj->pData;
Alan Mishchenko committed
589 590 591 592 593 594 595
        if ( pRepr && pRepr->pData == pObj )
            pRepr->pData = pRepr;
        // remove self-loops
        if ( pObj->pData == pObj )
            pObj->pData = NULL;
    }
    // set representatives
596
    Vec_PtrForEachEntry( Hop_Obj_t *, p->vObjs, pObj, i )
Alan Mishchenko committed
597 598 599 600 601 602 603 604 605 606
    {
        if ( pObj->pData == NULL )
            continue;
        // get representative of the node
        pRepr = Hop_ObjRepr( pObj );
        pRepr->pData = pRepr;
        // set the representative
        pObj->pData = pRepr;
    }
    // make each class point to the smallest topological order
607
    Vec_PtrForEachEntry( Hop_Obj_t *, p->vObjs, pObj, i )
Alan Mishchenko committed
608 609 610 611 612 613 614 615 616 617 618 619 620 621
    {
        if ( pObj->pData == NULL )
            continue;
        pRepr = Hop_ObjRepr( pObj );
        if ( pRepr->Id > pObj->Id )
        {
            pRepr->pData = pObj;
            pObj->pData = pObj;
        }
        else
            pObj->pData = pRepr;
    }
    // count classes, members, and fanouts - and verify
    nMembers = nClasses = nFanouts = nNormals = 0;
622
    Vec_PtrForEachEntry( Hop_Obj_t *, p->vObjs, pObj, i )
Alan Mishchenko committed
623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644
    {
        if ( pObj->pData == NULL )
            continue;
        // count members
        nMembers++;
        // count the classes and fanouts
        if ( pObj->pData == pObj )
            nClasses++;
        else if ( Hop_ObjRefs(pObj) > 0 )
            nFanouts++;
        else
            nNormals++;
        // compare representatives
        pRepr = Hop_ObjRepr( pObj );
        assert( pObj->pData == pRepr );
        assert( pRepr->Id <= pObj->Id );
    }
//    printf( "Nodes = %7d.  Member = %7d.  Classes = %6d.  Fanouts = %6d.  Normals = %6d.\n", 
//        Hop_ManNodeNum(p), nMembers, nClasses, nFanouts, nNormals );
    return nFanouts;
}

Alan Mishchenko committed
645 646
#if 0

Alan Mishchenko committed
647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 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 694 695 696
/**Function*************************************************************

  Synopsis    [Stops history AIG.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Abc_Ntk_t * Abc_NtkHaigUse( Abc_Ntk_t * pNtk )
{
    Hop_Man_t * pMan, * pManTemp;
    Abc_Ntk_t * pNtkAig;
    Abc_Obj_t * pObj;
    int i;

    // check if HAIG is available
    assert( Abc_NtkIsStrash(pNtk) );
    if ( pNtk->pHaig == NULL )
    {
        printf( "Warning: History AIG is not available.\n" );
        return NULL;
    }
    // convert HOP package into AIG with choices
    // print HAIG stats
//    Hop_ManPrintStats( pMan ); // USES DATA!!!

    // add the POs
    Abc_NtkForEachCo( pNtk, pObj, i )
        Hop_ObjCreatePo( pNtk->pHaig, Abc_ObjChild0Equiv(pObj) );

    // clean the old network
    Abc_NtkForEachObj( pNtk, pObj, i )
        pObj->pEquiv = NULL;
    pMan = pNtk->pHaig; 
    pNtk->pHaig = 0;

    // iteratively reconstruct the HOP manager to create choice nodes
    while ( Abc_NtkHaigResetReprs( pMan ) )
    {
        pMan = Abc_NtkHaigReconstruct( pManTemp = pMan );
        Hop_ManStop( pManTemp );
    }

    // traverse in the topological order and create new AIG
    pNtkAig = Abc_NtkHaigRecreateAig( pNtk, pMan );
    Hop_ManStop( pMan );

Alan Mishchenko committed
697
    // free HAIG
Alan Mishchenko committed
698 699
    return pNtkAig;
}
Alan Mishchenko committed
700

Alan Mishchenko committed
701 702
#endif

Alan Mishchenko committed
703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731
/**Function*************************************************************

  Synopsis    [Transform HOP manager into the one without loops.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Abc_Ntk_t * Abc_NtkHopRemoveLoops( Abc_Ntk_t * pNtk, Hop_Man_t * pMan )
{
    Abc_Ntk_t * pNtkAig;
    Hop_Man_t * pManTemp;

    // iteratively reconstruct the HOP manager to create choice nodes
    while ( Abc_NtkHaigResetReprs( pMan ) )
    {
        pMan = Abc_NtkHaigReconstruct( pManTemp = pMan );
        Hop_ManStop( pManTemp );
    }

    // traverse in the topological order and create new AIG
    pNtkAig = Abc_NtkHaigRecreateAig( pNtk, pMan );
    Hop_ManStop( pMan );
    return pNtkAig;
}

Alan Mishchenko committed
732 733 734 735 736
////////////////////////////////////////////////////////////////////////
///                       END OF FILE                                ///
////////////////////////////////////////////////////////////////////////


737 738
ABC_NAMESPACE_IMPL_END