ivyFastMap.c 47.5 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    [ivyFastMap.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [And-Inverter Graph package.]

  Synopsis    [Fast FPGA mapping.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

  Date        [Ver. 1.0. Started - May 11, 2006.]

  Revision    [$Id: ivyFastMap.c,v 1.00 2006/05/11 00:00:00 alanmi Exp $]

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

#include "ivy.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 57 58 59 60 61 62 63 64 65 66 67 68 69
////////////////////////////////////////////////////////////////////////
///                        DECLARATIONS                              ///
////////////////////////////////////////////////////////////////////////

#define IVY_INFINITY   10000

typedef struct Ivy_SuppMan_t_ Ivy_SuppMan_t;
struct Ivy_SuppMan_t_ 
{
    int         nLimit;    // the limit on the number of inputs
    int         nObjs;     // the number of entries
    int         nSize;     // size of each entry in bytes
    char *      pMem;      // memory allocated
    Vec_Vec_t * vLuts;     // the array of nodes used in the mapping
};

typedef struct Ivy_Supp_t_ Ivy_Supp_t;
struct Ivy_Supp_t_ 
{
    char        nSize;     // the number of support nodes
    char        fMark;     // multipurpose mask
    char        fMark2;    // multipurpose mask 
    char        fMark3;    // multipurpose mask 
    int         nRefs;     // the number of references
    short       Delay;     // the delay of the node
    short       DelayR;    // the reverse delay of the node
    int         pArray[0]; // the support nodes
};

static inline Ivy_Supp_t * Ivy_ObjSupp( Ivy_Man_t * pAig, Ivy_Obj_t * pObj )      
{ 
    return (Ivy_Supp_t *)(((Ivy_SuppMan_t*)pAig->pData)->pMem + pObj->Id * ((Ivy_SuppMan_t*)pAig->pData)->nSize); 
}
static inline Ivy_Supp_t * Ivy_ObjSuppStart( Ivy_Man_t * pAig, Ivy_Obj_t * pObj ) 
{ 
    Ivy_Supp_t * pSupp;
    pSupp = Ivy_ObjSupp( pAig, pObj );
    pSupp->fMark = 0;
    pSupp->Delay = 0;
    pSupp->nSize = 1;
    pSupp->pArray[0] = pObj->Id;
    return pSupp;
}

70
static void Ivy_FastMapPrint( Ivy_Man_t * pAig, int Delay, int Area, abctime Time, char * pStr );
Alan Mishchenko committed
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
static int  Ivy_FastMapDelay( Ivy_Man_t * pAig );
static int  Ivy_FastMapArea( Ivy_Man_t * pAig );
static void Ivy_FastMapNode( Ivy_Man_t * pAig, Ivy_Obj_t * pObj, int nLimit );
static void Ivy_FastMapNodeArea( Ivy_Man_t * pAig, Ivy_Obj_t * pObj, int nLimit );
static int  Ivy_FastMapMerge( Ivy_Supp_t * pSupp0, Ivy_Supp_t * pSupp1, Ivy_Supp_t * pSupp, int nLimit );
static void Ivy_FastMapRequired( Ivy_Man_t * pAig, int Delay, int fSetInter );
static void Ivy_FastMapRecover( Ivy_Man_t * pAig, int nLimit );
static int  Ivy_FastMapNodeDelay( Ivy_Man_t * pAig, Ivy_Obj_t * pObj );
static int  Ivy_FastMapNodeAreaRefed( Ivy_Man_t * pAig, Ivy_Obj_t * pObj );
static int  Ivy_FastMapNodeAreaDerefed( Ivy_Man_t * pAig, Ivy_Obj_t * pObj );
static void Ivy_FastMapNodeRecover( Ivy_Man_t * pAig, Ivy_Obj_t * pObj, int nLimit, Vec_Ptr_t * vFront, Vec_Ptr_t * vFrontOld );
static int  Ivy_FastMapNodeRef( Ivy_Man_t * pAig, Ivy_Obj_t * pObj );
static int  Ivy_FastMapNodeDeref( Ivy_Man_t * pAig, Ivy_Obj_t * pObj );


86
extern abctime s_MappingTime;
87
extern int s_MappingMem;
Alan Mishchenko committed
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108


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

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

  Synopsis    [Performs fast K-LUT mapping of the AIG.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Ivy_FastMapPerform( Ivy_Man_t * pAig, int nLimit, int fRecovery, int fVerbose )
{
    Ivy_SuppMan_t * pMan;
    Ivy_Obj_t * pObj;
109
    int i, Delay, Area;
110
    abctime clk, clkTotal = Abc_Clock();
Alan Mishchenko committed
111
    // start the memory for supports
Alan Mishchenko committed
112
    pMan = ABC_ALLOC( Ivy_SuppMan_t, 1 );
Alan Mishchenko committed
113 114 115 116
    memset( pMan, 0, sizeof(Ivy_SuppMan_t) );
    pMan->nLimit = nLimit;
    pMan->nObjs  = Ivy_ManObjIdMax(pAig) + 1;
    pMan->nSize  = sizeof(Ivy_Supp_t) + nLimit * sizeof(int);
Alan Mishchenko committed
117
    pMan->pMem   = (char *)ABC_ALLOC( char, pMan->nObjs * pMan->nSize );
Alan Mishchenko committed
118 119 120
    memset( pMan->pMem, 0, pMan->nObjs * pMan->nSize );
    pMan->vLuts  = Vec_VecAlloc( 100 );
    pAig->pData  = pMan;
121
clk = Abc_Clock();
Alan Mishchenko committed
122 123 124 125 126 127 128 129 130 131 132
    // set the PI mapping
    Ivy_ObjSuppStart( pAig, Ivy_ManConst1(pAig) );
    Ivy_ManForEachPi( pAig, pObj, i )
        Ivy_ObjSuppStart( pAig, pObj );
    // iterate through all nodes in the topological order
    Ivy_ManForEachNode( pAig, pObj, i )
        Ivy_FastMapNode( pAig, pObj, nLimit );
    // find the best arrival time and area
    Delay = Ivy_FastMapDelay( pAig );
    Area = Ivy_FastMapArea(pAig);
    if ( fVerbose )
133
        Ivy_FastMapPrint( pAig, Delay, Area, Abc_Clock() - clk, "Delay oriented mapping: " );
Alan Mishchenko committed
134 135 136 137 138

// 2-1-2 (doing 2-1-2-1-2 improves 0.5%)

    if ( fRecovery )
    {
139
clk = Abc_Clock();
Alan Mishchenko committed
140 141 142 143 144 145
    Ivy_FastMapRequired( pAig, Delay, 0 );
    // remap the nodes
    Ivy_FastMapRecover( pAig, nLimit );
    Delay = Ivy_FastMapDelay( pAig );
    Area = Ivy_FastMapArea(pAig);
    if ( fVerbose )
146
        Ivy_FastMapPrint( pAig, Delay, Area, Abc_Clock() - clk, "Area recovery 2       : " );
Alan Mishchenko committed
147

148
clk = Abc_Clock();
Alan Mishchenko committed
149 150 151 152 153 154 155
    Ivy_FastMapRequired( pAig, Delay, 0 );
    // iterate through all nodes in the topological order
    Ivy_ManForEachNode( pAig, pObj, i )
        Ivy_FastMapNodeArea( pAig, pObj, nLimit );
    Delay = Ivy_FastMapDelay( pAig );
    Area = Ivy_FastMapArea(pAig);
    if ( fVerbose )
156
        Ivy_FastMapPrint( pAig, Delay, Area, Abc_Clock() - clk, "Area recovery 1       : " );
Alan Mishchenko committed
157

158
clk = Abc_Clock();
Alan Mishchenko committed
159 160 161 162 163 164
    Ivy_FastMapRequired( pAig, Delay, 0 );
    // remap the nodes
    Ivy_FastMapRecover( pAig, nLimit );
    Delay = Ivy_FastMapDelay( pAig );
    Area = Ivy_FastMapArea(pAig);
    if ( fVerbose )
165
        Ivy_FastMapPrint( pAig, Delay, Area, Abc_Clock() - clk, "Area recovery 2       : " );
Alan Mishchenko committed
166 167 168
    }


169
    s_MappingTime = Abc_Clock() - clkTotal;
Alan Mishchenko committed
170 171 172 173 174
    s_MappingMem = pMan->nObjs * pMan->nSize;
/*
    {
        Vec_Ptr_t * vNodes;
        vNodes = Vec_PtrAlloc( 100 );
175
        Vec_VecForEachEntry( Ivy_Obj_t *, pMan->vLuts, pObj, i, k )
Alan Mishchenko committed
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
            Vec_PtrPush( vNodes, pObj );
        Ivy_ManShow( pAig, 0, vNodes );
        Vec_PtrFree( vNodes );
    }
*/
}

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

  Synopsis    [Cleans memory used for decomposition.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Ivy_FastMapStop( Ivy_Man_t * pAig )
{
196
    Ivy_SuppMan_t * p = (Ivy_SuppMan_t *)pAig->pData;
Alan Mishchenko committed
197
    Vec_VecFree( p->vLuts );
Alan Mishchenko committed
198 199
    ABC_FREE( p->pMem );
    ABC_FREE( p );
Alan Mishchenko committed
200 201 202 203 204 205 206 207 208 209 210 211 212 213
    pAig->pData = NULL;
}

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

  Synopsis    [Prints statistics.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
214
void Ivy_FastMapPrint( Ivy_Man_t * pAig, int Delay, int Area, abctime Time, char * pStr )
Alan Mishchenko committed
215 216
{
    printf( "%s : Delay = %3d. Area = %6d. ", pStr, Delay, Area );
Alan Mishchenko committed
217
    ABC_PRT( "Time", Time );
Alan Mishchenko committed
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 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 315
}

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

  Synopsis    [Computes delay after LUT mapping.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Ivy_FastMapDelay( Ivy_Man_t * pAig )
{
    Ivy_Supp_t * pSupp;
    Ivy_Obj_t * pObj;
    int i, DelayMax = 0;
    Ivy_ManForEachPo( pAig, pObj, i )
    {
        pObj = Ivy_ObjFanin0(pObj);
        if ( !Ivy_ObjIsNode(pObj) )
            continue;
        pSupp = Ivy_ObjSupp( pAig, pObj );
        if ( DelayMax < pSupp->Delay )
            DelayMax = pSupp->Delay;
    }
    return DelayMax;
}

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

  Synopsis    [Computes area after mapping.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Ivy_FastMapArea_rec( Ivy_Man_t * pAig, Ivy_Obj_t * pObj, Vec_Vec_t * vLuts )
{
    Ivy_Supp_t * pSupp;
    int i, Counter;
    pSupp = Ivy_ObjSupp( pAig, pObj );
    // skip visited nodes and PIs
    if ( pSupp->fMark || pSupp->nSize == 1 )
        return 0;
    pSupp->fMark = 1;
    // compute the area of this node
    Counter = 0;
    for ( i = 0; i < pSupp->nSize; i++ )
        Counter += Ivy_FastMapArea_rec( pAig, Ivy_ManObj(pAig, pSupp->pArray[i]), vLuts );
    // add the node to the array of LUTs
    Vec_VecPush( vLuts, pSupp->Delay, pObj );
    return 1 + Counter;
}

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

  Synopsis    [Computes area after mapping.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Ivy_FastMapArea( Ivy_Man_t * pAig )
{
    Vec_Vec_t * vLuts;
    Ivy_Obj_t * pObj;
    int i, Counter = 0;
    // get the array to store the nodes
    vLuts = ((Ivy_SuppMan_t *)pAig->pData)->vLuts;
    Vec_VecClear( vLuts );
    // explore starting from each node
    Ivy_ManForEachPo( pAig, pObj, i )
        Counter += Ivy_FastMapArea_rec( pAig, Ivy_ObjFanin0(pObj), vLuts );
    // clean the marks
    Ivy_ManForEachNode( pAig, pObj, i )
        Ivy_ObjSupp( pAig, pObj )->fMark = 0;
    return Counter;
}

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

  Synopsis    [Performs fast mapping for one node.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
316
static inline int Ivy_ObjIsNodeInt1( Ivy_Obj_t * pObj )
Alan Mishchenko committed
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
{
    return Ivy_ObjIsNode(pObj) && Ivy_ObjRefs(pObj) == 1;
}

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

  Synopsis    [Performs fast mapping for one node.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
332
static inline int Ivy_ObjIsNodeInt2( Ivy_Obj_t * pObj )
Alan Mishchenko committed
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 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 403 404 405 406 407 408 409 410 411 412 413 414 415 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 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 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 585 586 587 588 589 590 591 592
{
    return Ivy_ObjIsNode(pObj) && Ivy_ObjRefs(pObj) <= 2;
}

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

  Synopsis    [Performs fast mapping for one node.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline int Vec_IntRemoveDup( int * pArray, int nSize )
{
    int i, k;
    if ( nSize < 2 )
        return nSize;
    for ( i = k = 1; i < nSize; i++ )
        if ( pArray[i] != pArray[i-1] )
            pArray[k++] = pArray[i];
    return k;
}

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

  Synopsis    [Performs fast mapping for one node.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Ivy_FastMapNodeArea2( Ivy_Man_t * pAig, Ivy_Obj_t * pObj, int nLimit )
{
    static int Store[32], StoreSize;
    static char Supp0[16], Supp1[16];
    static Ivy_Supp_t * pTemp0 = (Ivy_Supp_t *)Supp0;
    static Ivy_Supp_t * pTemp1 = (Ivy_Supp_t *)Supp1;
    Ivy_Obj_t * pFanin0, * pFanin1;
    Ivy_Supp_t * pSupp0, * pSupp1, * pSupp;
    int RetValue, DelayOld;
    assert( nLimit <= 32 );
    assert( Ivy_ObjIsNode(pObj) );
    // get the fanins
    pFanin0 = Ivy_ObjFanin0(pObj);
    pFanin1 = Ivy_ObjFanin1(pObj);
    // get the supports
    pSupp0 = Ivy_ObjSupp( pAig, pFanin0 );
    pSupp1 = Ivy_ObjSupp( pAig, pFanin1 );
    pSupp  = Ivy_ObjSupp( pAig, pObj );
    assert( pSupp->fMark == 0 );
    // get the old delay of the node
    DelayOld = Ivy_FastMapNodeDelay(pAig, pObj);
    assert( DelayOld <= pSupp->DelayR );
    // copy the current cut
    memcpy( Store, pSupp->pArray, sizeof(int) * pSupp->nSize );
    StoreSize = pSupp->nSize;
    // get the fanin support
    if ( Ivy_ObjRefs(pFanin0) > 1 && pSupp0->Delay < pSupp->DelayR )
    {
        pSupp0 = pTemp0;
        pSupp0->nSize = 1;
        pSupp0->pArray[0] = Ivy_ObjFaninId0(pObj);
    }
    // get the fanin support
    if ( Ivy_ObjRefs(pFanin1) > 1 && pSupp1->Delay < pSupp->DelayR )
    {
        pSupp1 = pTemp1;
        pSupp1->nSize = 1;
        pSupp1->pArray[0] = Ivy_ObjFaninId1(pObj);
    }
    // merge the cuts
    if ( pSupp0->nSize < pSupp1->nSize )
        RetValue = Ivy_FastMapMerge( pSupp1, pSupp0, pSupp, nLimit );
    else
        RetValue = Ivy_FastMapMerge( pSupp0, pSupp1, pSupp, nLimit );
    if ( !RetValue )
    {
        pSupp->nSize = 2;
        pSupp->pArray[0] = Ivy_ObjFaninId0(pObj);
        pSupp->pArray[1] = Ivy_ObjFaninId1(pObj);
    }
    // check the resulting delay
    pSupp->Delay = Ivy_FastMapNodeDelay(pAig, pObj);
    if ( pSupp->Delay > pSupp->DelayR )
    {
        pSupp->nSize = StoreSize;
        memcpy( pSupp->pArray, Store, sizeof(int) * pSupp->nSize );
        pSupp->Delay = DelayOld;
    }
}

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

  Synopsis    [Performs fast mapping for one node.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Ivy_FastMapNodeArea( Ivy_Man_t * pAig, Ivy_Obj_t * pObj, int nLimit )
{
    static int Store[32], StoreSize;
    static char Supp0[16], Supp1[16];
    static Ivy_Supp_t * pTemp0 = (Ivy_Supp_t *)Supp0;
    static Ivy_Supp_t * pTemp1 = (Ivy_Supp_t *)Supp1;
    Ivy_Obj_t * pFanin0, * pFanin1;
    Ivy_Supp_t * pSupp0, * pSupp1, * pSupp;
    int RetValue, DelayOld, RefsOld;
    int AreaBef, AreaAft;
    assert( nLimit <= 32 );
    assert( Ivy_ObjIsNode(pObj) );
    // get the fanins
    pFanin0 = Ivy_ObjFanin0(pObj);
    pFanin1 = Ivy_ObjFanin1(pObj);
    // get the supports
    pSupp0 = Ivy_ObjSupp( pAig, pFanin0 );
    pSupp1 = Ivy_ObjSupp( pAig, pFanin1 );
    pSupp  = Ivy_ObjSupp( pAig, pObj );
    assert( pSupp->fMark == 0 );

    // get the area
    if ( pSupp->nRefs == 0 )
        AreaBef = Ivy_FastMapNodeAreaDerefed( pAig, pObj );
    else
        AreaBef = Ivy_FastMapNodeAreaRefed( pAig, pObj );
//    if ( AreaBef == 1 )
//        return;

    // deref the cut if the node is refed
    if ( pSupp->nRefs != 0 )
        Ivy_FastMapNodeDeref( pAig, pObj );

    // get the old delay of the node
    DelayOld = Ivy_FastMapNodeDelay(pAig, pObj);
    assert( DelayOld <= pSupp->DelayR );
    // copy the current cut
    memcpy( Store, pSupp->pArray, sizeof(int) * pSupp->nSize );
    StoreSize = pSupp->nSize;
    // get the fanin support
    if ( Ivy_ObjRefs(pFanin0) > 2 && pSupp0->Delay < pSupp->DelayR )
//    if ( pSupp0->nRefs > 0 && pSupp0->Delay < pSupp->DelayR ) // this leads to 2% worse results
    {
        pSupp0 = pTemp0;
        pSupp0->nSize = 1;
        pSupp0->pArray[0] = Ivy_ObjFaninId0(pObj);
    }
    // get the fanin support
    if ( Ivy_ObjRefs(pFanin1) > 2 && pSupp1->Delay < pSupp->DelayR )
//    if ( pSupp1->nRefs > 0 && pSupp1->Delay < pSupp->DelayR )
    {
        pSupp1 = pTemp1;
        pSupp1->nSize = 1;
        pSupp1->pArray[0] = Ivy_ObjFaninId1(pObj);
    }
    // merge the cuts
    if ( pSupp0->nSize < pSupp1->nSize )
        RetValue = Ivy_FastMapMerge( pSupp1, pSupp0, pSupp, nLimit );
    else
        RetValue = Ivy_FastMapMerge( pSupp0, pSupp1, pSupp, nLimit );
    if ( !RetValue )
    {
        pSupp->nSize = 2;
        pSupp->pArray[0] = Ivy_ObjFaninId0(pObj);
        pSupp->pArray[1] = Ivy_ObjFaninId1(pObj);
    }

    // check the resulting delay
    pSupp->Delay = Ivy_FastMapNodeDelay(pAig, pObj);

    RefsOld = pSupp->nRefs; pSupp->nRefs = 0;
    AreaAft = Ivy_FastMapNodeAreaDerefed( pAig, pObj );
    pSupp->nRefs = RefsOld;

    if ( AreaAft > AreaBef || pSupp->Delay > pSupp->DelayR )
//    if ( pSupp->Delay > pSupp->DelayR )
    {
        pSupp->nSize = StoreSize;
        memcpy( pSupp->pArray, Store, sizeof(int) * pSupp->nSize );
        pSupp->Delay = DelayOld;
//        printf( "-" );
    }
//    else
//        printf( "+" );

    if ( pSupp->nRefs != 0 )
        Ivy_FastMapNodeRef( pAig, pObj );
}

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

  Synopsis    [Performs fast mapping for one node.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Ivy_FastMapNode( Ivy_Man_t * pAig, Ivy_Obj_t * pObj, int nLimit )
{
    Ivy_Supp_t * pSupp0, * pSupp1, * pSupp;
    int fFaninParam = 2;
    int RetValue;
    assert( Ivy_ObjIsNode(pObj) );
    // get the supports
    pSupp0 = Ivy_ObjSupp( pAig, Ivy_ObjFanin0(pObj) );
    pSupp1 = Ivy_ObjSupp( pAig, Ivy_ObjFanin1(pObj) );
    pSupp  = Ivy_ObjSupp( pAig, pObj );
    pSupp->fMark = 0;
    // get the delays
    if ( pSupp0->Delay == pSupp1->Delay )
        pSupp->Delay = (pSupp0->Delay == 0) ? pSupp0->Delay + 1: pSupp0->Delay;
    else if ( pSupp0->Delay > pSupp1->Delay )
    {
        pSupp->Delay = pSupp0->Delay;
        pSupp1 = Ivy_ObjSupp( pAig, Ivy_ManConst1(pAig) );
        pSupp1->pArray[0] = Ivy_ObjFaninId1(pObj);
    }
    else // if ( pSupp0->Delay < pSupp1->Delay )
    {
        pSupp->Delay = pSupp1->Delay;
        pSupp0 = Ivy_ObjSupp( pAig, Ivy_ManConst1(pAig) );
        pSupp0->pArray[0] = Ivy_ObjFaninId0(pObj);
    }
    // merge the cuts
    if ( pSupp0->nSize < pSupp1->nSize )
        RetValue = Ivy_FastMapMerge( pSupp1, pSupp0, pSupp, nLimit );
    else
        RetValue = Ivy_FastMapMerge( pSupp0, pSupp1, pSupp, nLimit );
    if ( !RetValue )
    {
        pSupp->Delay++;
        if ( fFaninParam == 2 )
        {
            pSupp->nSize = 2;
            pSupp->pArray[0] = Ivy_ObjFaninId0(pObj);
            pSupp->pArray[1] = Ivy_ObjFaninId1(pObj);
        }
        else if ( fFaninParam == 3 )
        {
            Ivy_Obj_t * pFanin0, * pFanin1, * pFaninA, * pFaninB;
            pFanin0 = Ivy_ObjFanin0(pObj);
            pFanin1 = Ivy_ObjFanin1(pObj);
            pSupp->nSize = 0;
            // process the first fanin
            if ( Ivy_ObjIsNodeInt1(pFanin0) )
            {
                pFaninA = Ivy_ObjFanin0(pFanin0);
                pFaninB = Ivy_ObjFanin1(pFanin0);
                if ( Ivy_ObjIsNodeInt1(pFaninA) && Ivy_ObjIsNodeInt1(pFaninB) )
Alan Mishchenko committed
593
                    pSupp->pArray[(int)(pSupp->nSize++)] = Ivy_ObjId(pFanin0);
Alan Mishchenko committed
594 595
                else
                {
Alan Mishchenko committed
596 597
                    pSupp->pArray[(int)(pSupp->nSize++)] = Ivy_ObjId(pFaninA);
                    pSupp->pArray[(int)(pSupp->nSize++)] = Ivy_ObjId(pFaninB);
Alan Mishchenko committed
598 599 600
                }
            }
            else
Alan Mishchenko committed
601
                pSupp->pArray[(int)(pSupp->nSize++)] = Ivy_ObjId(pFanin0);
Alan Mishchenko committed
602 603 604 605 606 607
            // process the second fanin
            if ( Ivy_ObjIsNodeInt1(pFanin1) )
            {
                pFaninA = Ivy_ObjFanin0(pFanin1);
                pFaninB = Ivy_ObjFanin1(pFanin1);
                if ( Ivy_ObjIsNodeInt1(pFaninA) && Ivy_ObjIsNodeInt1(pFaninB) )
Alan Mishchenko committed
608
                    pSupp->pArray[(int)(pSupp->nSize++)] = Ivy_ObjId(pFanin1);
Alan Mishchenko committed
609 610
                else
                {
Alan Mishchenko committed
611 612
                    pSupp->pArray[(int)(pSupp->nSize++)] = Ivy_ObjId(pFaninA);
                    pSupp->pArray[(int)(pSupp->nSize++)] = Ivy_ObjId(pFaninB);
Alan Mishchenko committed
613 614 615
                }
            }
            else
Alan Mishchenko committed
616
                pSupp->pArray[(int)(pSupp->nSize++)] = Ivy_ObjId(pFanin1);
Alan Mishchenko committed
617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661
            // sort the fanins
            Vec_IntSelectSort( pSupp->pArray, pSupp->nSize );
            pSupp->nSize = Vec_IntRemoveDup( pSupp->pArray, pSupp->nSize );
            assert( pSupp->pArray[0] < pSupp->pArray[1] );
        }
        else if ( fFaninParam == 4 )
        {
            Ivy_Obj_t * pFanin0, * pFanin1, * pFaninA, * pFaninB;
            pFanin0 = Ivy_ObjFanin0(pObj);
            pFanin1 = Ivy_ObjFanin1(pObj);
            pSupp->nSize = 0;
            // consider the case when exactly one of them is internal
            if ( Ivy_ObjIsNodeInt1(pFanin0) ^ Ivy_ObjIsNodeInt1(pFanin1) )
            {
                pSupp0 = Ivy_ObjSupp( pAig, Ivy_ObjFanin0(pObj) );
                pSupp1 = Ivy_ObjSupp( pAig, Ivy_ObjFanin1(pObj) );
                if ( Ivy_ObjIsNodeInt1(pFanin0) && pSupp0->nSize < nLimit )
                {
                    pSupp->Delay = IVY_MAX( pSupp0->Delay, pSupp1->Delay + 1 );
                    pSupp1 = Ivy_ObjSupp( pAig, Ivy_ManConst1(pAig) );
                    pSupp1->pArray[0] = Ivy_ObjId(pFanin1);
                    // merge the cuts
                    RetValue = Ivy_FastMapMerge( pSupp0, pSupp1, pSupp, nLimit );
                    assert( RetValue );
                    assert( pSupp->nSize > 1 );
                    return;
                }
                if ( Ivy_ObjIsNodeInt1(pFanin1) && pSupp1->nSize < nLimit )
                {
                    pSupp->Delay = IVY_MAX( pSupp1->Delay, pSupp0->Delay + 1 );
                    pSupp0 = Ivy_ObjSupp( pAig, Ivy_ManConst1(pAig) );
                    pSupp0->pArray[0] = Ivy_ObjId(pFanin0);
                    // merge the cuts
                    RetValue = Ivy_FastMapMerge( pSupp1, pSupp0, pSupp, nLimit );
                    assert( RetValue );
                    assert( pSupp->nSize > 1 );
                    return;
                }
            }
            // process the first fanin
            if ( Ivy_ObjIsNodeInt1(pFanin0) )
            {
                pFaninA = Ivy_ObjFanin0(pFanin0);
                pFaninB = Ivy_ObjFanin1(pFanin0);
                if ( Ivy_ObjIsNodeInt1(pFaninA) && Ivy_ObjIsNodeInt1(pFaninB) )
Alan Mishchenko committed
662
                    pSupp->pArray[(int)(pSupp->nSize++)] = Ivy_ObjId(pFanin0);
Alan Mishchenko committed
663 664
                else
                {
Alan Mishchenko committed
665 666
                    pSupp->pArray[(int)(pSupp->nSize++)] = Ivy_ObjId(pFaninA);
                    pSupp->pArray[(int)(pSupp->nSize++)] = Ivy_ObjId(pFaninB);
Alan Mishchenko committed
667 668 669
                }
            }
            else
Alan Mishchenko committed
670
                pSupp->pArray[(int)(pSupp->nSize++)] = Ivy_ObjId(pFanin0);
Alan Mishchenko committed
671 672 673 674 675 676
            // process the second fanin
            if ( Ivy_ObjIsNodeInt1(pFanin1) )
            {
                pFaninA = Ivy_ObjFanin0(pFanin1);
                pFaninB = Ivy_ObjFanin1(pFanin1);
                if ( Ivy_ObjIsNodeInt1(pFaninA) && Ivy_ObjIsNodeInt1(pFaninB) )
Alan Mishchenko committed
677
                    pSupp->pArray[(int)(pSupp->nSize++)] = Ivy_ObjId(pFanin1);
Alan Mishchenko committed
678 679
                else
                {
Alan Mishchenko committed
680 681
                    pSupp->pArray[(int)(pSupp->nSize++)] = Ivy_ObjId(pFaninA);
                    pSupp->pArray[(int)(pSupp->nSize++)] = Ivy_ObjId(pFaninB);
Alan Mishchenko committed
682 683 684
                }
            }
            else
Alan Mishchenko committed
685
                pSupp->pArray[(int)(pSupp->nSize++)] = Ivy_ObjId(pFanin1);
Alan Mishchenko committed
686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 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 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 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
            // sort the fanins
            Vec_IntSelectSort( pSupp->pArray, pSupp->nSize );
            pSupp->nSize = Vec_IntRemoveDup( pSupp->pArray, pSupp->nSize );
            assert( pSupp->pArray[0] < pSupp->pArray[1] );
            assert( pSupp->nSize > 1 );
        }
    }
    assert( pSupp->Delay > 0 );
}

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

  Synopsis    [Merges two supports]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Ivy_FastMapMerge( Ivy_Supp_t * pSupp0, Ivy_Supp_t * pSupp1, Ivy_Supp_t * pSupp, int nLimit )
{ 
    int i, k, c;
    assert( pSupp0->nSize >= pSupp1->nSize );
    // the case of the largest cut sizes
    if ( pSupp0->nSize == nLimit && pSupp1->nSize == nLimit )
    {
        for ( i = 0; i < pSupp0->nSize; i++ )
            if ( pSupp0->pArray[i] != pSupp1->pArray[i] )
                return 0;
        for ( i = 0; i < pSupp0->nSize; i++ )
            pSupp->pArray[i] = pSupp0->pArray[i];
        pSupp->nSize = pSupp0->nSize;
        return 1;
    }
    // the case when one of the cuts is the largest
    if ( pSupp0->nSize == nLimit )
    {
        for ( i = 0; i < pSupp1->nSize; i++ )
        {
            for ( k = pSupp0->nSize - 1; k >= 0; k-- )
                if ( pSupp0->pArray[k] == pSupp1->pArray[i] )
                    break;
            if ( k == -1 ) // did not find
                return 0;
        }
        for ( i = 0; i < pSupp0->nSize; i++ )
            pSupp->pArray[i] = pSupp0->pArray[i];
        pSupp->nSize = pSupp0->nSize;
        return 1;
    }

    // compare two cuts with different numbers
    i = k = 0;
    for ( c = 0; c < nLimit; c++ )
    {
        if ( k == pSupp1->nSize )
        {
            if ( i == pSupp0->nSize )
            {
                pSupp->nSize = c;
                return 1;
            }
            pSupp->pArray[c] = pSupp0->pArray[i++];
            continue;
        }
        if ( i == pSupp0->nSize )
        {
            if ( k == pSupp1->nSize )
            {
                pSupp->nSize = c;
                return 1;
            }
            pSupp->pArray[c] = pSupp1->pArray[k++];
            continue;
        }
        if ( pSupp0->pArray[i] < pSupp1->pArray[k] )
        {
            pSupp->pArray[c] = pSupp0->pArray[i++];
            continue;
        }
        if ( pSupp0->pArray[i] > pSupp1->pArray[k] )
        {
            pSupp->pArray[c] = pSupp1->pArray[k++];
            continue;
        }
        pSupp->pArray[c] = pSupp0->pArray[i++]; 
        k++;
    }
    if ( i < pSupp0->nSize || k < pSupp1->nSize )
        return 0;
    pSupp->nSize = c;
    return 1;
}

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

  Synopsis    [Creates integer vector with the support of the node.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Ivy_FastMapReadSupp( Ivy_Man_t * pAig, Ivy_Obj_t * pObj, Vec_Int_t * vLeaves )
{
    Ivy_Supp_t * pSupp;
    pSupp = Ivy_ObjSupp( pAig, pObj );
    vLeaves->nCap   = 8;
    vLeaves->nSize  = pSupp->nSize;
    vLeaves->pArray = pSupp->pArray;
}

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

  Synopsis    [Sets the required times of the intermediate nodes.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Ivy_FastMapRequired_rec( Ivy_Man_t * pAig, Ivy_Obj_t * pObj, Ivy_Obj_t * pRoot, int DelayR )
{
    Ivy_Supp_t * pSupp;
    pSupp = Ivy_ObjSupp( pAig, pObj );
    if ( pObj != pRoot && (pSupp->nRefs > 0 || Ivy_ObjIsCi(pObj)) )
        return;
    Ivy_FastMapRequired_rec( pAig, Ivy_ObjFanin0(pObj), pRoot, DelayR );
    Ivy_FastMapRequired_rec( pAig, Ivy_ObjFanin1(pObj), pRoot, DelayR );
//    assert( pObj == pRoot || pSupp->DelayR == IVY_INFINITY );
    pSupp->DelayR = DelayR;
}

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

  Synopsis    [Computes the required times for each node.]

  Description [Sets reference counters for each node.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Ivy_FastMapRequired( Ivy_Man_t * pAig, int Delay, int fSetInter )
{
    Vec_Vec_t * vLuts;
    Vec_Ptr_t * vNodes;
    Ivy_Obj_t * pObj;
    Ivy_Supp_t * pSupp, * pSuppF;
    int i, k, c;
    // clean the required times
    Ivy_ManForEachPi( pAig, pObj, i )
    {
        pSupp = Ivy_ObjSupp( pAig, pObj );
        pSupp->DelayR = IVY_INFINITY;
        pSupp->nRefs = 0;
    }
    Ivy_ManForEachNode( pAig, pObj, i )
    {
        pSupp = Ivy_ObjSupp( pAig, pObj );
        pSupp->DelayR = IVY_INFINITY;
        pSupp->nRefs = 0;
    }
    // set the required times of the POs
    Ivy_ManForEachPo( pAig, pObj, i )
    {
        pSupp = Ivy_ObjSupp( pAig, Ivy_ObjFanin0(pObj) );
        pSupp->DelayR = Delay;
        pSupp->nRefs++;
    }
    // get the levelized nodes used in the mapping
    vLuts = ((Ivy_SuppMan_t *)pAig->pData)->vLuts;
    // propagate the required times
    Vec_VecForEachLevelReverse( vLuts, vNodes, i )
867
    Vec_PtrForEachEntry( Ivy_Obj_t *, vNodes, pObj, k )
Alan Mishchenko committed
868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891
    {
        pSupp = Ivy_ObjSupp( pAig, pObj );
        assert( pSupp->nRefs > 0 );
        for ( c = 0; c < pSupp->nSize; c++ )
        {
            pSuppF = Ivy_ObjSupp( pAig, Ivy_ManObj(pAig, pSupp->pArray[c]) );
            pSuppF->DelayR = IVY_MIN( pSuppF->DelayR, pSupp->DelayR - 1 );
            pSuppF->nRefs++;
        }
    }
/*
    // print out some of the required times
    Ivy_ManForEachPi( pAig, pObj, i )
    {
        pSupp = Ivy_ObjSupp( pAig, pObj );
        printf( "%d ", pSupp->DelayR );
    }
    printf( "\n" );    
*/

    if ( fSetInter )
    {
        // set the required times of the intermediate nodes
        Vec_VecForEachLevelReverse( vLuts, vNodes, i )
892
        Vec_PtrForEachEntry( Ivy_Obj_t *, vNodes, pObj, k )
Alan Mishchenko committed
893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097
        {
            pSupp = Ivy_ObjSupp( pAig, pObj );
            Ivy_FastMapRequired_rec( pAig, pObj, pObj, pSupp->DelayR ); 
        }
        // make sure that all required times are assigned
        Ivy_ManForEachNode( pAig, pObj, i )
        {
            pSupp = Ivy_ObjSupp( pAig, pObj );
            assert( pSupp->DelayR < IVY_INFINITY );
        }
    }
}

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

  Synopsis    [Performs area recovery for each node.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Ivy_FastMapRecover( Ivy_Man_t * pAig, int nLimit )
{
    Vec_Ptr_t * vFront, * vFrontOld;
    Ivy_Obj_t * pObj;
    int i;
    vFront = Vec_PtrAlloc( nLimit );
    vFrontOld = Vec_PtrAlloc( nLimit );
    Ivy_ManCleanTravId( pAig );
    // iterate through all nodes in the topological order
    Ivy_ManForEachNode( pAig, pObj, i )
        Ivy_FastMapNodeRecover( pAig, pObj, nLimit, vFront, vFrontOld );
    Vec_PtrFree( vFrontOld );
    Vec_PtrFree( vFront );
}

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

  Synopsis    [Computes the delay of the cut rooted at this node.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Ivy_FastMapNodeDelay( Ivy_Man_t * pAig, Ivy_Obj_t * pObj )
{
    Ivy_Supp_t * pSupp, * pSuppF;
    int c, Delay = 0;
    pSupp = Ivy_ObjSupp( pAig, pObj );
    for ( c = 0; c < pSupp->nSize; c++ )
    {
        pSuppF = Ivy_ObjSupp( pAig, Ivy_ManObj(pAig, pSupp->pArray[c]) );
        Delay = IVY_MAX( Delay, pSuppF->Delay );
    }
    return 1 + Delay;
}


/**function*************************************************************

  synopsis    [References the cut.]

  description [This procedure is similar to the procedure NodeReclaim.]
               
  sideeffects []

  seealso     []

***********************************************************************/
int Ivy_FastMapNodeRef( Ivy_Man_t * pAig, Ivy_Obj_t * pObj )
{
    Ivy_Supp_t * pSupp, * pSuppF;
    Ivy_Obj_t * pNodeChild;
    int aArea, i;
    // start the area of this cut
    aArea = 1;
    // go through the children
    pSupp = Ivy_ObjSupp( pAig, pObj );
    assert( pSupp->nSize > 1 );
    for ( i = 0; i < pSupp->nSize; i++ )
    {
        pNodeChild = Ivy_ManObj(pAig, pSupp->pArray[i]);
        pSuppF = Ivy_ObjSupp( pAig, pNodeChild );
        assert( pSuppF->nRefs >= 0 );
        if ( pSuppF->nRefs++ > 0 )  
            continue;
        if ( pSuppF->nSize == 1 ) 
            continue;
        aArea += Ivy_FastMapNodeRef( pAig, pNodeChild );
    }
    return aArea;
}

/**function*************************************************************

  synopsis    [Dereferences the cut.]

  description [This procedure is similar to the procedure NodeRecusiveDeref.]
               
  sideeffects []

  seealso     []

***********************************************************************/
int Ivy_FastMapNodeDeref( Ivy_Man_t * pAig, Ivy_Obj_t * pObj )
{
    Ivy_Supp_t * pSupp, * pSuppF;
    Ivy_Obj_t * pNodeChild;
    int aArea, i;
    // start the area of this cut
    aArea = 1;
    // go through the children
    pSupp = Ivy_ObjSupp( pAig, pObj );
    assert( pSupp->nSize > 1 );
    for ( i = 0; i < pSupp->nSize; i++ )
    {
        pNodeChild = Ivy_ManObj(pAig, pSupp->pArray[i]);
        pSuppF = Ivy_ObjSupp( pAig, pNodeChild );
        assert( pSuppF->nRefs > 0 );
        if ( --pSuppF->nRefs > 0 )  
            continue;
        if ( pSuppF->nSize == 1 ) 
            continue;
        aArea += Ivy_FastMapNodeDeref( pAig, pNodeChild );
    }
    return aArea;
}

/**function*************************************************************

  synopsis    [Computes the exact area associated with the cut.]

  description []
               
  sideeffects []

  seealso     []

***********************************************************************/
int Ivy_FastMapNodeAreaRefed( Ivy_Man_t * pAig, Ivy_Obj_t * pObj )
{
    Ivy_Supp_t * pSupp;
    int aResult, aResult2;
    if ( Ivy_ObjIsCi(pObj) )
        return 0;
    assert( Ivy_ObjIsNode(pObj) );
    pSupp = Ivy_ObjSupp( pAig, pObj );
    assert( pSupp->nRefs > 0 );
    aResult  = Ivy_FastMapNodeDeref( pAig, pObj );
    aResult2 = Ivy_FastMapNodeRef( pAig, pObj );
    assert( aResult == aResult2 );
    return aResult;
}

/**function*************************************************************

  synopsis    [Computes the exact area associated with the cut.]

  description []
               
  sideeffects []

  seealso     []

***********************************************************************/
int Ivy_FastMapNodeAreaDerefed( Ivy_Man_t * pAig, Ivy_Obj_t * pObj )
{
    Ivy_Supp_t * pSupp;
    int aResult, aResult2;
    if ( Ivy_ObjIsCi(pObj) )
        return 0;
    assert( Ivy_ObjIsNode(pObj) );
    pSupp = Ivy_ObjSupp( pAig, pObj );
    assert( pSupp->nRefs == 0 );
    aResult2 = Ivy_FastMapNodeRef( pAig, pObj );
    aResult  = Ivy_FastMapNodeDeref( pAig, pObj );
    assert( aResult == aResult2 );
    return aResult;
}




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

  Synopsis    [Counts the number of nodes with no external fanout.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Ivy_FastMapCutCost( Ivy_Man_t * pAig, Vec_Ptr_t * vFront )
{
    Ivy_Supp_t * pSuppF;
    Ivy_Obj_t * pFanin;
    int i, Counter = 0;
1098
    Vec_PtrForEachEntry( Ivy_Obj_t *, vFront, pFanin, i )
Alan Mishchenko committed
1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226
    {
        pSuppF = Ivy_ObjSupp( pAig, pFanin );
        if ( pSuppF->nRefs == 0 )
            Counter++;
    }
    return Counter;
}

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

  Synopsis    [Performs area recovery for each node.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Ivy_FastMapMark_rec( Ivy_Man_t * pAig, Ivy_Obj_t * pObj )
{
    if ( Ivy_ObjIsTravIdCurrent(pAig, pObj) )
        return;
    assert( Ivy_ObjIsNode(pObj) );
    Ivy_FastMapMark_rec( pAig, Ivy_ObjFanin0(pObj) );
    Ivy_FastMapMark_rec( pAig, Ivy_ObjFanin1(pObj) );
    Ivy_ObjSetTravIdCurrent(pAig, pObj);
}

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

  Synopsis    [Returns 1 if the number of fanins will grow.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Ivy_FastMapNodeWillGrow( Ivy_Man_t * pAig, Ivy_Obj_t * pObj )
{
    Ivy_Obj_t * pFanin0, * pFanin1;
    assert( Ivy_ObjIsNode(pObj) );
    pFanin0 = Ivy_ObjFanin0(pObj);
    pFanin1 = Ivy_ObjFanin1(pObj);
    return !Ivy_ObjIsTravIdCurrent(pAig, pFanin0) && !Ivy_ObjIsTravIdCurrent(pAig, pFanin1);
}

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

  Synopsis    [Returns the increase in the number of fanins with no external refs.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Ivy_FastMapNodeFaninCost( Ivy_Man_t * pAig, Ivy_Obj_t * pObj )
{
    Ivy_Supp_t * pSuppF;
    Ivy_Obj_t * pFanin;
    int Counter = 0;
    assert( Ivy_ObjIsNode(pObj) );
    // check if the node has external refs
    pSuppF = Ivy_ObjSupp( pAig, pObj );
    if ( pSuppF->nRefs == 0 )
        Counter--;
    // increment the number of fanins without external refs
    pFanin = Ivy_ObjFanin0(pObj);
    pSuppF = Ivy_ObjSupp( pAig, pFanin );
    if ( !Ivy_ObjIsTravIdCurrent(pAig, pFanin) && pSuppF->nRefs == 0 )
        Counter++;
    // increment the number of fanins without external refs
    pFanin = Ivy_ObjFanin1(pObj);
    pSuppF = Ivy_ObjSupp( pAig, pFanin );
    if ( !Ivy_ObjIsTravIdCurrent(pAig, pFanin) && pSuppF->nRefs == 0 )
        Counter++;
    return Counter;
}

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

  Synopsis    [Updates the frontier.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Ivy_FastMapNodeFaninUpdate( Ivy_Man_t * pAig, Ivy_Obj_t * pObj, Vec_Ptr_t * vFront )
{
    Ivy_Obj_t * pFanin;
    assert( Ivy_ObjIsNode(pObj) );
    Vec_PtrRemove( vFront, pObj );
    pFanin = Ivy_ObjFanin0(pObj);
    if ( !Ivy_ObjIsTravIdCurrent(pAig, pFanin) )
    {
        Ivy_ObjSetTravIdCurrent(pAig, pFanin);
        Vec_PtrPush( vFront, pFanin );
    }
    pFanin = Ivy_ObjFanin1(pObj);
    if ( !Ivy_ObjIsTravIdCurrent(pAig, pFanin) )
    {
        Ivy_ObjSetTravIdCurrent(pAig, pFanin);
        Vec_PtrPush( vFront, pFanin );
    }
}

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

  Synopsis    [Compacts the number of external refs.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Ivy_FastMapNodeFaninCompact0( Ivy_Man_t * pAig, Ivy_Obj_t * pObj, int nLimit, Vec_Ptr_t * vFront )
{
    Ivy_Obj_t * pFanin;
    int i;
1227
    Vec_PtrForEachEntry( Ivy_Obj_t *, vFront, pFanin, i )
Alan Mishchenko committed
1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256
    {
        if ( Ivy_ObjIsCi(pFanin) )
            continue;
        if ( Ivy_FastMapNodeWillGrow(pAig, pFanin) )
            continue;
        if ( Ivy_FastMapNodeFaninCost(pAig, pFanin) <= 0 )
        {
            Ivy_FastMapNodeFaninUpdate( pAig, pFanin, vFront );
            return 1;
        }
    }
    return 0;
}

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

  Synopsis    [Compacts the number of external refs.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Ivy_FastMapNodeFaninCompact1( Ivy_Man_t * pAig, Ivy_Obj_t * pObj, int nLimit, Vec_Ptr_t * vFront )
{
    Ivy_Obj_t * pFanin;
    int i;
1257
    Vec_PtrForEachEntry( Ivy_Obj_t *, vFront, pFanin, i )
Alan Mishchenko committed
1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284
    {
        if ( Ivy_ObjIsCi(pFanin) )
            continue;
        if ( Ivy_FastMapNodeFaninCost(pAig, pFanin) < 0 )
        {
            Ivy_FastMapNodeFaninUpdate( pAig, pFanin, vFront );
            return 1;
        }
    }
    return 0;
}

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

  Synopsis    [Compacts the number of external refs.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Ivy_FastMapNodeFaninCompact2( Ivy_Man_t * pAig, Ivy_Obj_t * pObj, int nLimit, Vec_Ptr_t * vFront )
{
    Ivy_Obj_t * pFanin;
    int i;
1285
    Vec_PtrForEachEntry( Ivy_Obj_t *, vFront, pFanin, i )
Alan Mishchenko committed
1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389
    {
        if ( Ivy_ObjIsCi(pFanin) )
            continue;
        if ( Ivy_FastMapNodeFaninCost(pAig, pFanin) <= 0 )
        {
            Ivy_FastMapNodeFaninUpdate( pAig, pFanin, vFront );
            return 1;
        }
    }
    return 0;
}

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

  Synopsis    [Compacts the number of external refs.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Ivy_FastMapNodeFaninCompact_int( Ivy_Man_t * pAig, Ivy_Obj_t * pObj, int nLimit, Vec_Ptr_t * vFront )
{
    if ( Ivy_FastMapNodeFaninCompact0(pAig, pObj, nLimit, vFront) )
        return 1;
    if (  Vec_PtrSize(vFront) < nLimit && Ivy_FastMapNodeFaninCompact1(pAig, pObj, nLimit, vFront) )
        return 1;
    if ( Vec_PtrSize(vFront) < nLimit && Ivy_FastMapNodeFaninCompact2(pAig, pObj, nLimit, vFront) )
        return 1;
    assert( Vec_PtrSize(vFront) <= nLimit );
    return 0;
}

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

  Synopsis    [Compacts the number of external refs.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Ivy_FastMapNodeFaninCompact( Ivy_Man_t * pAig, Ivy_Obj_t * pObj, int nLimit, Vec_Ptr_t * vFront )
{
    while ( Ivy_FastMapNodeFaninCompact_int( pAig, pObj, nLimit, vFront ) );
}

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

  Synopsis    [Prepares node mapping.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Ivy_FastMapNodePrepare( Ivy_Man_t * pAig, Ivy_Obj_t * pObj, int nLimit, Vec_Ptr_t * vFront, Vec_Ptr_t * vFrontOld )
{
    Ivy_Supp_t * pSupp;
    Ivy_Obj_t * pFanin;
    int i;
    pSupp = Ivy_ObjSupp( pAig, pObj );
    // expand the cut downwards from the given place
    Vec_PtrClear( vFront );
    Vec_PtrClear( vFrontOld );
    Ivy_ManIncrementTravId( pAig );
    for ( i = 0; i < pSupp->nSize; i++ )
    {
        pFanin = Ivy_ManObj(pAig, pSupp->pArray[i]);
        Vec_PtrPush( vFront, pFanin );
        Vec_PtrPush( vFrontOld, pFanin );
        Ivy_ObjSetTravIdCurrent( pAig, pFanin );
    }
    // mark the nodes in the cone
    Ivy_FastMapMark_rec( pAig, pObj );
}

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

  Synopsis    [Updates the frontier.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Ivy_FastMapNodeUpdate( Ivy_Man_t * pAig, Ivy_Obj_t * pObj, Vec_Ptr_t * vFront )
{
    Ivy_Supp_t * pSupp;
    Ivy_Obj_t * pFanin;
    int i;
    pSupp = Ivy_ObjSupp( pAig, pObj );
    // deref node's cut
    Ivy_FastMapNodeDeref( pAig, pObj );
    // update the node's cut
    pSupp->nSize = Vec_PtrSize(vFront);
1390
    Vec_PtrForEachEntry( Ivy_Obj_t *, vFront, pFanin, i )
Alan Mishchenko committed
1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567
        pSupp->pArray[i] = pFanin->Id;
    // ref the new cut
    Ivy_FastMapNodeRef( pAig, pObj );
}

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

  Synopsis    [Performs area recovery for each node.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Ivy_FastMapNodeRecover2( Ivy_Man_t * pAig, Ivy_Obj_t * pObj, int nLimit, Vec_Ptr_t * vFront, Vec_Ptr_t * vFrontOld )
{
    Ivy_Supp_t * pSupp;
    int CostBef, CostAft;
    int AreaBef, AreaAft;
    pSupp = Ivy_ObjSupp( pAig, pObj );
//    if ( pSupp->nRefs == 0 )
//        return;
    if ( pSupp->nRefs == 0 )
        AreaBef = Ivy_FastMapNodeAreaDerefed( pAig, pObj );
    else
        AreaBef = Ivy_FastMapNodeAreaRefed( pAig, pObj );
    // get the area
    if ( AreaBef == 1 )
        return;

    if ( pSupp->nRefs == 0 )
    {
        pSupp->nRefs = 1000000;
        Ivy_FastMapNodeRef( pAig, pObj );
    }
    // the cut is non-trivial
    Ivy_FastMapNodePrepare( pAig, pObj, nLimit, vFront, vFrontOld );
    // iteratively modify the cut
    CostBef = Ivy_FastMapCutCost( pAig, vFront );
    Ivy_FastMapNodeFaninCompact( pAig, pObj, nLimit, vFront );
    CostAft = Ivy_FastMapCutCost( pAig, vFront );
    assert( CostBef >= CostAft );
    // update the node
    Ivy_FastMapNodeUpdate( pAig, pObj, vFront );
    // get the new area
    AreaAft = Ivy_FastMapNodeAreaRefed( pAig, pObj );
    if ( AreaAft > AreaBef )
    {
        Ivy_FastMapNodeUpdate( pAig, pObj, vFrontOld );
        AreaAft = Ivy_FastMapNodeAreaRefed( pAig, pObj );
        assert( AreaAft == AreaBef );
    }
    if ( pSupp->nRefs == 1000000 )
    {
        pSupp->nRefs = 0;
        Ivy_FastMapNodeDeref( pAig, pObj );
    }
}

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

  Synopsis    [Performs area recovery for each node.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Ivy_FastMapNodeRecover( Ivy_Man_t * pAig, Ivy_Obj_t * pObj, int nLimit, Vec_Ptr_t * vFront, Vec_Ptr_t * vFrontOld )
{
    Ivy_Supp_t * pSupp;
    int CostBef, CostAft;
    int AreaBef, AreaAft;
    int DelayOld;
    pSupp = Ivy_ObjSupp( pAig, pObj );
    DelayOld = pSupp->Delay = Ivy_FastMapNodeDelay( pAig, pObj );
    assert( pSupp->Delay <= pSupp->DelayR );
    if ( pSupp->nRefs == 0 )
        return;
    // get the area
    AreaBef = Ivy_FastMapNodeAreaRefed( pAig, pObj );
//    if ( AreaBef == 1 )
//        return;
    // the cut is non-trivial
    Ivy_FastMapNodePrepare( pAig, pObj, nLimit, vFront, vFrontOld );
    // iteratively modify the cut
    Ivy_FastMapNodeDeref( pAig, pObj );
    CostBef = Ivy_FastMapCutCost( pAig, vFront );
    Ivy_FastMapNodeFaninCompact( pAig, pObj, nLimit, vFront );
    CostAft = Ivy_FastMapCutCost( pAig, vFront );
    Ivy_FastMapNodeRef( pAig, pObj );
    assert( CostBef >= CostAft );
    // update the node
    Ivy_FastMapNodeUpdate( pAig, pObj, vFront );
    pSupp->Delay = Ivy_FastMapNodeDelay( pAig, pObj );
    // get the new area
    AreaAft = Ivy_FastMapNodeAreaRefed( pAig, pObj );
    if ( AreaAft > AreaBef || pSupp->Delay > pSupp->DelayR )
    {
        Ivy_FastMapNodeUpdate( pAig, pObj, vFrontOld );
        AreaAft = Ivy_FastMapNodeAreaRefed( pAig, pObj );
        assert( AreaAft == AreaBef );
        pSupp->Delay = DelayOld;
    }
}

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

  Synopsis    [Performs area recovery for each node.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Ivy_FastMapNodeRecover4( Ivy_Man_t * pAig, Ivy_Obj_t * pObj, int nLimit, Vec_Ptr_t * vFront, Vec_Ptr_t * vFrontOld )
{
    Ivy_Supp_t * pSupp;
    int CostBef, CostAft;
    int AreaBef, AreaAft;
    int DelayOld;
    pSupp = Ivy_ObjSupp( pAig, pObj );
    DelayOld = pSupp->Delay = Ivy_FastMapNodeDelay( pAig, pObj );
    assert( pSupp->Delay <= pSupp->DelayR );
//    if ( pSupp->nRefs == 0 )
//        return;
//    AreaBef = Ivy_FastMapNodeAreaRefed( pAig, pObj );
    // get the area
    if ( pSupp->nRefs == 0 )
        AreaBef = Ivy_FastMapNodeAreaDerefed( pAig, pObj );
    else
        AreaBef = Ivy_FastMapNodeAreaRefed( pAig, pObj );
    if ( AreaBef == 1 )
        return;

    if ( pSupp->nRefs == 0 )
    {
        pSupp->nRefs = 1000000;
        Ivy_FastMapNodeRef( pAig, pObj );
    }
    // the cut is non-trivial
    Ivy_FastMapNodePrepare( pAig, pObj, nLimit, vFront, vFrontOld );
    // iteratively modify the cut
    CostBef = Ivy_FastMapCutCost( pAig, vFront );
    Ivy_FastMapNodeFaninCompact( pAig, pObj, nLimit, vFront );
    CostAft = Ivy_FastMapCutCost( pAig, vFront );
    assert( CostBef >= CostAft );
    // update the node
    Ivy_FastMapNodeUpdate( pAig, pObj, vFront );
    pSupp->Delay = Ivy_FastMapNodeDelay( pAig, pObj );
    // get the new area
    AreaAft = Ivy_FastMapNodeAreaRefed( pAig, pObj );
    if ( AreaAft > AreaBef || pSupp->Delay > pSupp->DelayR )
    {
        Ivy_FastMapNodeUpdate( pAig, pObj, vFrontOld );
        AreaAft = Ivy_FastMapNodeAreaRefed( pAig, pObj );
        assert( AreaAft == AreaBef );
        pSupp->Delay = DelayOld;
    }
    if ( pSupp->nRefs == 1000000 )
    {
        pSupp->nRefs = 0;
        Ivy_FastMapNodeDeref( pAig, pObj );
    }
}

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


1568 1569
ABC_NAMESPACE_IMPL_END