abcHaig.c 16.2 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
/**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 $]

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

21
#include "base/abc/abc.h"
Alan Mishchenko committed
22

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 36

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

Alan Mishchenko committed
37 38 39 40 41 42 43 44 45 46 47
  Synopsis    [Collects the nodes in the classes.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Vec_Ptr_t * Abc_NtkHaigCollectMembers( Hop_Man_t * p )
{
Alan Mishchenko committed
48
    Vec_Ptr_t * vObjs;
Alan Mishchenko committed
49 50
    Hop_Obj_t * pObj;
    int i;
Alan Mishchenko committed
51
    vObjs = Vec_PtrAlloc( 4098 );
52
    Vec_PtrForEachEntry( Hop_Obj_t *, p->vObjs, pObj, i )
Alan Mishchenko committed
53 54 55 56
    {
        if ( pObj->pData == NULL )
            continue;
        pObj->pData = Hop_ObjRepr( pObj );
Alan Mishchenko committed
57
        Vec_PtrPush( vObjs, pObj );
Alan Mishchenko committed
58
    }
Alan Mishchenko committed
59
    return vObjs;
Alan Mishchenko committed
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
}

/**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 );
81
    Vec_PtrForEachEntry( Hop_Obj_t *, vMembers, pObj, i )
Alan Mishchenko committed
82
    {
83
        pRepr = (Hop_Obj_t *)pObj->pData;
Alan Mishchenko committed
84 85 86 87 88 89 90 91 92
        assert( pRepr->pData == NULL );
        if ( pRepr->fMarkA == 0 ) // new
        {
            pRepr->fMarkA = 1;
            Vec_PtrPush( vClasses, pRepr );
        }
    }

    // set representatives as representatives
93
    Vec_PtrForEachEntry( Hop_Obj_t *, vClasses, pObj, i )
Alan Mishchenko committed
94 95 96 97 98 99
    {
        pObj->fMarkA = 0;
        pObj->pData = pObj;
    }

    // go through the members and update
100
    Vec_PtrForEachEntry( Hop_Obj_t *, vMembers, pObj, i )
Alan Mishchenko committed
101
    {
102
        pRepr = (Hop_Obj_t *)pObj->pData;
Alan Mishchenko committed
103 104 105 106 107
        if ( ((Hop_Obj_t *)pRepr->pData)->Id > pObj->Id )
            pRepr->pData = pObj;
    }

    // change representatives of the class
108
    Vec_PtrForEachEntry( Hop_Obj_t *, vMembers, pObj, i )
Alan Mishchenko committed
109
    {
110
        pRepr = (Hop_Obj_t *)pObj->pData;
Alan Mishchenko committed
111 112 113 114 115
        pObj->pData = pRepr->pData;
        assert( ((Hop_Obj_t *)pObj->pData)->Id <= pObj->Id );
    }

    // update classes
116
    Vec_PtrForEachEntry( Hop_Obj_t *, vClasses, pObj, i )
Alan Mishchenko committed
117
    {
118
        pRepr = (Hop_Obj_t *)pObj->pData;
Alan Mishchenko committed
119
        assert( pRepr->pData == pRepr );
Alan Mishchenko committed
120
//        pRepr->pData = NULL;
Alan Mishchenko committed
121 122 123
        Vec_PtrWriteEntry( vClasses, i, pRepr );
        Vec_PtrPush( vMembers, pObj );
    }
Alan Mishchenko committed
124

125
    Vec_PtrForEachEntry( Hop_Obj_t *, vMembers, pObj, i )
Alan Mishchenko committed
126 127 128
        if ( pObj->pData == pObj )
            pObj->pData = NULL;

Alan Mishchenko committed
129
/*
130
    Vec_PtrForEachEntry( Hop_Obj_t *, vMembers, pObj, i )
Alan Mishchenko committed
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
    {
        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
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
/**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;
163
    Vec_PtrForEachEntry( Hop_Obj_t *, p->vObjs, pObj, i )
Alan Mishchenko committed
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
    {
        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;
192
    pRepr = (Hop_Obj_t *)pObj->pData;
Alan Mishchenko committed
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
    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;
227
    Vec_PtrForEachEntry( Hop_Obj_t *, p->vObjs, pObj, i )
Alan Mishchenko committed
228 229 230 231 232 233 234 235 236 237 238
        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
239
    Vec_PtrForEachEntry( Hop_Obj_t *, p->vObjs, pObj, i )
Alan Mishchenko committed
240 241 242 243 244 245 246 247 248 249
    {
        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
250
//    printf( " Counter = %d.\n", Counter );
Alan Mishchenko committed
251 252 253 254 255 256 257 258 259 260 261 262 263
    // 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
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282

/**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
283
    if ( Abc_ObjIsCi(pNode) )
Alan Mishchenko committed
284 285 286 287 288 289 290 291 292 293 294 295 296
        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
297
    return Abc_NtkHaigCheckTfi_rec( (Abc_Obj_t *)pNode->pData, pOld );
Alan Mishchenko committed
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
}

/**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
330 331
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
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346

/**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
347
    Abc_Obj_t * pObjOld, * pObjAbcThis, * pObjAbcRepr;
Alan Mishchenko committed
348 349
    Hop_Obj_t * pObj;
    int i;
Alan Mishchenko committed
350
    assert( p->nCreated == Vec_PtrSize(p->vObjs) );
Alan Mishchenko committed
351 352

    // start the new network
Alan Mishchenko committed
353
    pNtkAig = Abc_NtkStartFrom( pNtk, ABC_NTK_STRASH, ABC_FUNC_AIG );
Alan Mishchenko committed
354 355

    // transfer new nodes to the PIs of HOP
Alan Mishchenko committed
356
    Hop_ManConst1(p)->pNext = (Hop_Obj_t *)Abc_AigConst1( pNtkAig );
Alan Mishchenko committed
357
    Hop_ManForEachPi( p, pObj, i )
Alan Mishchenko committed
358
        pObj->pNext = (Hop_Obj_t *)Abc_NtkCi( pNtkAig, i );
Alan Mishchenko committed
359 360

    // construct new nodes
361
    Vec_PtrForEachEntry( Hop_Obj_t *, p->vObjs, pObj, i )
Alan Mishchenko committed
362 363 364
    {
        if ( !Hop_ObjIsNode(pObj) )
            continue;
365
        pObj->pNext = (Hop_Obj_t *)Abc_AigAnd( (Abc_Aig_t *)pNtkAig->pManFunc, Hop_ObjChild0Next(pObj), Hop_ObjChild1Next(pObj) );
Alan Mishchenko committed
366 367
        assert( !Hop_IsComplement(pObj->pNext) );
    }
Alan Mishchenko committed
368 369 370

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

Alan Mishchenko committed
373
    // construct choice nodes
374
    Vec_PtrForEachEntry( Hop_Obj_t *, p->vObjs, pObj, i )
Alan Mishchenko committed
375
    {
Alan Mishchenko committed
376
        // skip the node without choices
Alan Mishchenko committed
377 378
        if ( pObj->pData == NULL )
            continue;
Alan Mishchenko committed
379 380
        // skip the representative of the class
        if ( pObj->pData == pObj )
Alan Mishchenko committed
381
            continue;
Alan Mishchenko committed
382
        // do not create choices for constant 1 and PIs
383
        if ( !Hop_ObjIsNode((Hop_Obj_t *)pObj->pData) )
Alan Mishchenko committed
384
            continue;
Alan Mishchenko committed
385 386 387 388 389 390 391 392 393
        // 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
394 395 396 397 398
        // 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 )
399
                pObjAbcRepr = (Abc_Obj_t *)pObjAbcRepr->pData;
Alan Mishchenko committed
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
            // 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
420 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 453 454 455 456 457 458 459 460 461
  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
462
    Vec_PtrForEachEntry( Hop_Obj_t *, p->vObjs, pObj, i )
Alan Mishchenko committed
463 464
    {
        // fix the strange situation of double-loop
465
        pRepr = (Hop_Obj_t *)pObj->pData;
Alan Mishchenko committed
466 467 468 469 470 471 472
        if ( pRepr && pRepr->pData == pObj )
            pRepr->pData = pRepr;
        // remove self-loops
        if ( pObj->pData == pObj )
            pObj->pData = NULL;
    }
    // set representatives
473
    Vec_PtrForEachEntry( Hop_Obj_t *, p->vObjs, pObj, i )
Alan Mishchenko committed
474 475 476 477 478 479 480 481 482 483
    {
        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
484
    Vec_PtrForEachEntry( Hop_Obj_t *, p->vObjs, pObj, i )
Alan Mishchenko committed
485 486 487 488 489 490 491 492 493 494 495 496 497 498
    {
        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;
499
    Vec_PtrForEachEntry( Hop_Obj_t *, p->vObjs, pObj, i )
Alan Mishchenko committed
500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521
    {
        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
522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550
/**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
551 552 553 554 555
////////////////////////////////////////////////////////////////////////
///                       END OF FILE                                ///
////////////////////////////////////////////////////////////////////////


556 557
ABC_NAMESPACE_IMPL_END