sclSize.c 28.9 KB
Newer Older
1 2
/**CFile****************************************************************

3
  FileName    [sclSize.c]
4 5 6

  SystemName  [ABC: Logic synthesis and verification system.]

7 8
  PackageName [Standard-cell library representation.]

9
  Synopsis    [Core timing analysis used in gate-sizing.]
10 11 12 13 14 15 16

  Author      [Alan Mishchenko, Niklas Een]
  
  Affiliation [UC Berkeley]

  Date        [Ver. 1.0. Started - August 24, 2012.]

17
  Revision    [$Id: sclSize.c,v 1.0 2012/08/24 00:00:00 alanmi Exp $]
18 19 20

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

21
#include "sclSize.h"
22
#include "map/mio/mio.h"
23
#include "misc/vec/vecWec.h"
24
#include "base/main/main.h"
25 26 27 28 29 30 31 32 33 34 35 36 37 38

ABC_NAMESPACE_IMPL_START


////////////////////////////////////////////////////////////////////////
///                        DECLARATIONS                              ///
////////////////////////////////////////////////////////////////////////

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

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

39
  Synopsis    [Finding most critical objects.]
40 41 42 43 44 45 46 47

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
48
Abc_Obj_t * Abc_SclFindCriticalCo( SC_Man * p, int * pfRise )
49
{
50 51 52
    Abc_Obj_t * pObj, * pPivot = NULL;
    float fMaxArr = 0;
    int i;
53
    assert( Abc_NtkPoNum(p->pNtk) > 0 );
54 55 56 57 58 59
    Abc_NtkForEachCo( p->pNtk, pObj, i )
    {
        SC_Pair * pArr = Abc_SclObjTime( p, pObj );
        if ( fMaxArr < pArr->rise )  fMaxArr = pArr->rise, *pfRise = 1, pPivot = pObj;
        if ( fMaxArr < pArr->fall )  fMaxArr = pArr->fall, *pfRise = 0, pPivot = pObj;
    }
60 61
    if ( fMaxArr == 0 )
        pPivot = Abc_NtkPo(p->pNtk, 0);
62 63
    assert( pPivot != NULL );
    return pPivot;
64
}
65 66
// assumes that slacks are not available (uses arrival times)
Abc_Obj_t * Abc_SclFindMostCriticalFanin2( SC_Man * p, int * pfRise, Abc_Obj_t * pNode )
67
{
68
    Abc_Obj_t * pFanin, * pPivot = NULL;
69 70
    float fMaxArr = 0;
    int i;
71
    Abc_ObjForEachFanin( pNode, pFanin, i )
72
    {
73 74 75
        SC_Pair * pArr = Abc_SclObjTime( p, pFanin );
        if ( fMaxArr < pArr->rise )  fMaxArr = pArr->rise, *pfRise = 1, pPivot = pFanin;
        if ( fMaxArr < pArr->fall )  fMaxArr = pArr->fall, *pfRise = 0, pPivot = pFanin;
76 77
    }
    return pPivot;
78
}
79 80 81 82 83 84 85 86 87 88
// assumes that slack are available
Abc_Obj_t * Abc_SclFindMostCriticalFanin( SC_Man * p, int * pfRise, Abc_Obj_t * pNode )
{
    Abc_Obj_t * pFanin, * pPivot = NULL;
    float fMinSlack = ABC_INFINITY;
    SC_Pair * pArr;
    int i;
    *pfRise = 0;
    // find min-slack node
    Abc_ObjForEachFanin( pNode, pFanin, i )
89
        if ( fMinSlack > Abc_SclObjGetSlack( p, pFanin, p->MaxDelay0 ) )
90
        {
91
            fMinSlack = Abc_SclObjGetSlack( p, pFanin, p->MaxDelay0 );
92 93 94 95 96 97 98 99 100
            pPivot = pFanin;
        }
    if ( pPivot == NULL )
        return NULL;
    // find its leading phase
    pArr = Abc_SclObjTime( p, pPivot );
    *pfRise = (pArr->rise >= pArr->fall);
    return pPivot;
}
101 102 103

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

104
  Synopsis    [Printing timing information for the node/network.]
105 106 107 108 109 110 111 112

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
113
static inline void Abc_SclTimeNodePrint( SC_Man * p, Abc_Obj_t * pObj, int fRise, int Length, float maxDelay )
114
{
115
    SC_Cell * pCell = Abc_ObjIsNode(pObj) ? Abc_SclObjCell(pObj) : NULL;
116
    printf( "%8d : ",           Abc_ObjId(pObj) );
117
    printf( "%d ",              Abc_ObjFaninNum(pObj) );
118
    printf( "%4d ",             Abc_ObjFanoutNum(pObj) );
119 120 121
    printf( "%-*s ",            Length, pCell ? pCell->pName : "pi" );
    printf( "A =%7.2f  ",       pCell ? pCell->area : 0.0 );
    printf( "D%s =",            fRise ? "r" : "f" );
122 123 124
    printf( "%6.1f",            Abc_SclObjTimeMax(p, pObj) );
    printf( "%7.1f ps  ",       -Abc_AbsFloat(Abc_SclObjTimeOne(p, pObj, 0) - Abc_SclObjTimeOne(p, pObj, 1)) );
    printf( "S =%6.1f ps  ",    Abc_SclObjSlewMax(p, pObj) );
125
    printf( "Cin =%5.1f ff  ",  pCell ? SC_CellPinCapAve(pCell) : 0.0 );
126
    printf( "Cout =%6.1f ff  ", Abc_SclObjLoadMax(p, pObj) );
127
    printf( "Cmax =%6.1f ff  ", pCell ? SC_CellPin(pCell, pCell->n_inputs)->max_out_cap : 0.0 );
128
    printf( "G =%5d  ",         pCell ? (int)(100.0 * Abc_SclObjLoadAve(p, pObj) / SC_CellPinCapAve(pCell)) : 0 );
129
//    printf( "SL =%6.1f ps",     Abc_SclObjSlackMax(p, pObj, p->MaxDelay0) );
130
    printf( "\n" );
131
}
132
void Abc_SclTimeNtkPrint( SC_Man * p, int fShowAll, int fPrintPath )
133
{
134
    int fReversePath = 1;
135
    int i, nLength = 0, fRise = 0;
136
    Abc_Obj_t * pObj, * pPivot = Abc_SclFindCriticalCo( p, &fRise ); 
137
    float maxDelay = Abc_SclObjTimeOne( p, pPivot, fRise );
138
    p->ReportDelay = maxDelay;
139

140 141 142
    printf( "WireLoad = \"%s\"  ", p->pWLoadUsed ? p->pWLoadUsed->pName : "none" );
    printf( "Gates =%7d ",         Abc_NtkNodeNum(p->pNtk) );
    printf( "(%5.1f %%)   ",       100.0 * Abc_SclGetBufInvCount(p->pNtk) / Abc_NtkNodeNum(p->pNtk) );
143
    printf( "Cap =%5.1f ff ",      p->EstLoadAve );
144 145 146 147 148
    printf( "(%5.1f %%)   ",       Abc_SclGetAverageSize(p->pNtk) );
    printf( "Area =%12.2f ",       Abc_SclGetTotalArea(p->pNtk) );
    printf( "(%5.1f %%)   ",       100.0 * Abc_SclCountMinSize(p->pLib, p->pNtk, 0) / Abc_NtkNodeNum(p->pNtk) );
    printf( "Delay =%9.2f ps  ",   maxDelay );
    printf( "(%5.1f %%)   ",       100.0 * Abc_SclCountNearCriticalNodes(p) / Abc_NtkNodeNum(p->pNtk) );
149
    printf( "            \n" );
150
    if ( fShowAll )
151
    {
152
//        printf( "Timing information for all nodes: \n" );
153
        // find the longest cell name
154
        Abc_NtkForEachNodeReverse( p->pNtk, pObj, i )
155
            if ( Abc_ObjFaninNum(pObj) > 0 )
156
                nLength = Abc_MaxInt( nLength, strlen(Abc_SclObjCell(pObj)->pName) );
157 158 159
        // print timing
        Abc_NtkForEachNodeReverse( p->pNtk, pObj, i )
            if ( Abc_ObjFaninNum(pObj) > 0 )
160
                Abc_SclTimeNodePrint( p, pObj, -1, nLength, maxDelay );
161
    }
162
    if ( fPrintPath )
163
    {
164 165 166 167
        Abc_Obj_t * pTemp, * pPrev = NULL;
        int iStart = -1, iEnd = -1;
        Vec_Ptr_t * vPath;
//        printf( "Critical path: \n" );        
168 169
        // find the longest cell name
        pObj = Abc_ObjFanin0(pPivot);
170
        i = 0;
171 172
        while ( pObj && Abc_ObjIsNode(pObj) )
        {
173
            i++;
174
            nLength = Abc_MaxInt( nLength, strlen(Abc_SclObjCell(pObj)->pName) );
175 176
            pObj = Abc_SclFindMostCriticalFanin( p, &fRise, pObj );
        }
177

178
        // print timing
179
        if ( !fReversePath )
180
        {
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
            // print timing
            pObj = Abc_ObjFanin0(pPivot);
            while ( pObj )//&& Abc_ObjIsNode(pObj) )
            {
                printf( "Path%3d --", i-- );
                Abc_SclTimeNodePrint( p, pObj, fRise, nLength, maxDelay );
                pPrev = pObj;
                pObj = Abc_SclFindMostCriticalFanin( p, &fRise, pObj );
            }
        }
        else
        {
            // collect path nodes
            vPath = Vec_PtrAlloc( 100 );
            Vec_PtrPush( vPath, pPivot );
            pObj = Abc_ObjFanin0(pPivot);
            while ( pObj )//&& Abc_ObjIsNode(pObj) )
            {
                Vec_PtrPush( vPath, pObj );
                pPrev = pObj;
                pObj = Abc_SclFindMostCriticalFanin( p, &fRise, pObj );
            }
            Vec_PtrForEachEntryReverse( Abc_Obj_t *, vPath, pObj, i )
            {
                printf( "Path%3d --", Vec_PtrSize(vPath)-1-i );
                Abc_SclTimeNodePrint( p, pObj, fRise, nLength, maxDelay );
                if ( i == 1 )
                    break;
            }
            Vec_PtrFree( vPath );
211
        }
212 213 214 215 216 217 218 219 220 221
        // print start-point and end-point
        Abc_NtkForEachPi( p->pNtk, pTemp, iStart )
            if ( pTemp == pPrev )
                break;
        Abc_NtkForEachPo( p->pNtk, pTemp, iEnd )
            if ( pTemp == pPivot )
                break;
        printf( "Start-point = pi%0*d.  End-point = po%0*d.\n", 
            Abc_Base10Log( Abc_NtkPiNum(p->pNtk) ), iStart, 
            Abc_Base10Log( Abc_NtkPoNum(p->pNtk) ), iEnd );
222 223 224 225 226
    }
}

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

227
  Synopsis    [Timing computation for pin/gate/cone/network.]
228 229 230 231 232 233 234 235

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
236
static inline void Abc_SclTimeFanin( SC_Man * p, SC_Timing * pTime, Abc_Obj_t * pObj, Abc_Obj_t * pFanin )
237
{
238
    SC_Pair * pArrIn   = Abc_SclObjTime( p, pFanin );
239
    SC_Pair * pSlewIn  = Abc_SclObjSlew( p, pFanin );
240
    SC_Pair * pLoad    = Abc_SclObjLoad( p, pObj );
241 242
    SC_Pair * pArrOut  = Abc_SclObjTime( p, pObj );   // modified
    SC_Pair * pSlewOut = Abc_SclObjSlew( p, pObj );   // modified
243
    Scl_LibPinArrival( pTime, pArrIn, pSlewIn, pLoad, pArrOut, pSlewOut );
244
}
245
static inline void Abc_SclDeptFanin( SC_Man * p, SC_Timing * pTime, Abc_Obj_t * pObj, Abc_Obj_t * pFanin )
246 247 248 249 250
{
    SC_Pair * pDepIn   = Abc_SclObjDept( p, pFanin );   // modified
    SC_Pair * pSlewIn  = Abc_SclObjSlew( p, pFanin );
    SC_Pair * pLoad    = Abc_SclObjLoad( p, pObj );
    SC_Pair * pDepOut  = Abc_SclObjDept( p, pObj );
251
    Scl_LibPinDeparture( pTime, pDepIn, pSlewIn, pLoad, pDepOut );
252
}
253 254 255 256 257
static inline void Abc_SclDeptObj( SC_Man * p, Abc_Obj_t * pObj )
{
    SC_Timing * pTime;
    Abc_Obj_t * pFanout;
    int i;
258
    SC_PairClean( Abc_SclObjDept(p, pObj) );
259 260
    Abc_ObjForEachFanout( pObj, pFanout, i )
    {
261
        if ( Abc_ObjIsCo(pFanout) || Abc_ObjIsLatch(pFanout) )
262 263
            continue;
        pTime = Scl_CellPinTime( Abc_SclObjCell(pFanout), Abc_NodeFindFanin(pFanout, pObj) );
264 265 266
        Abc_SclDeptFanin( p, pTime, pFanout, pObj );
    }
}
267 268 269
static inline float Abc_SclObjLoadValue( SC_Man * p, Abc_Obj_t * pObj )
{
//    float Value = Abc_MaxFloat(pLoad->fall, pLoad->rise) / (p->EstLoadAve * p->EstLoadMax);
270
    return (0.5 * Abc_SclObjLoad(p, pObj)->fall + 0.5 * Abc_SclObjLoad(p, pObj)->rise) / (p->EstLoadAve * p->EstLoadMax);
271
}
272 273 274 275 276 277 278 279 280 281
static inline void Abc_SclTimeCi( SC_Man * p, Abc_Obj_t * pObj )
{
    if ( p->pPiDrive != NULL )
    {
        SC_Pair * pLoad = Abc_SclObjLoad( p, pObj );
        SC_Pair * pTime = Abc_SclObjTime( p, pObj );
        SC_Pair * pSlew = Abc_SclObjSlew( p, pObj );
        Scl_LibHandleInputDriver( p->pPiDrive, pLoad, pTime, pSlew );
    }
}
282
void Abc_SclTimeNode( SC_Man * p, Abc_Obj_t * pObj, int fDept )
283 284 285 286
{
    SC_Timing * pTime;
    SC_Cell * pCell;
    int k;
287 288 289 290 291
    SC_Pair * pLoad = Abc_SclObjLoad( p, pObj );
    float LoadRise = pLoad->rise;
    float LoadFall = pLoad->fall;
    float DeptRise = 0;
    float DeptFall = 0;
292
    float Value = p->EstLoadMax ? Abc_SclObjLoadValue( p, pObj ) : 0;
293
    Abc_Obj_t * pFanin;
294 295 296 297 298 299
    if ( Abc_ObjIsCi(pObj) )
    {
        assert( !fDept );
        Abc_SclTimeCi( p, pObj );
        return;
    }
300 301
    if ( Abc_ObjIsCo(pObj) )
    {
302
        if ( !fDept )
303
        {
304
            Abc_SclObjDupFanin( p, pObj );
305 306 307
            Vec_FltWriteEntry( p->vTimesOut, pObj->iData, Abc_SclObjTimeMax(p, pObj) );
            Vec_QueUpdate( p->vQue, pObj->iData );
        }
308 309 310
        return;
    }
    assert( Abc_ObjIsNode(pObj) );
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
//    if ( !(Abc_ObjFaninNum(pObj) == 1 && Abc_ObjIsPi(Abc_ObjFanin0(pObj))) && p->EstLoadMax && Value > 1 )
    if ( p->EstLoadMax && Value > 1 )
    {
        pLoad->rise = p->EstLoadAve * p->EstLoadMax;
        pLoad->fall = p->EstLoadAve * p->EstLoadMax;
        if ( fDept )
        {
            SC_Pair * pDepOut  = Abc_SclObjDept( p, pObj );
            float EstDelta = p->EstLinear * log( Value );
            DeptRise = pDepOut->rise;
            DeptFall = pDepOut->fall;
            pDepOut->rise += EstDelta;
            pDepOut->fall += EstDelta;
        }
        p->nEstNodes++;
    }
327
    // get the library cell
328
    pCell = Abc_SclObjCell( pObj );
329 330
    // compute for each fanin
    Abc_ObjForEachFanin( pObj, pFanin, k )
331
    {
332
        pTime = Scl_CellPinTime( pCell, k );
333
        if ( fDept )
334
            Abc_SclDeptFanin( p, pTime, pObj, pFanin );
335
        else
336
            Abc_SclTimeFanin( p, pTime, pObj, pFanin );
337
    }
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
    if ( p->EstLoadMax && Value > 1 )
    {
        pLoad->rise = LoadRise;
        pLoad->fall = LoadFall;
        if ( fDept )
        {
            SC_Pair * pDepOut  = Abc_SclObjDept( p, pObj );
            pDepOut->rise = DeptRise;
            pDepOut->fall = DeptFall;
        }
        else
        {
            SC_Pair * pArrOut  = Abc_SclObjTime( p, pObj );
            float EstDelta = p->EstLinear * log( Value );
            pArrOut->rise += EstDelta;
            pArrOut->fall += EstDelta;
        }
    }
356 357 358
}
void Abc_SclTimeCone( SC_Man * p, Vec_Int_t * vCone )
{
359
    int fVerbose = 0;
360 361
    Abc_Obj_t * pObj;
    int i;
362
    Abc_SclConeClean( p, vCone );
363
    Abc_NtkForEachObjVec( vCone, p->pNtk, pObj, i )
364
    {
365
        if ( fVerbose && Abc_ObjIsNode(pObj) )
366
        printf( "  Updating node %d with gate %s\n", Abc_ObjId(pObj), Abc_SclObjCell(pObj)->pName );
367
        if ( fVerbose && Abc_ObjIsNode(pObj) )
368
        printf( "    before (%6.1f ps  %6.1f ps)   ", Abc_SclObjTimeOne(p, pObj, 1), Abc_SclObjTimeOne(p, pObj, 0) );
369
        Abc_SclTimeNode( p, pObj, 0 );
370
        if ( fVerbose && Abc_ObjIsNode(pObj) )
371
        printf( "after (%6.1f ps  %6.1f ps)\n", Abc_SclObjTimeOne(p, pObj, 1), Abc_SclObjTimeOne(p, pObj, 0) );
372 373
    }
}
374
void Abc_SclTimeNtkRecompute( SC_Man * p, float * pArea, float * pDelay, int fReverse, float DUser )
375 376
{
    Abc_Obj_t * pObj;
377
    float D;
378
    int i;
379 380
    Abc_SclComputeLoad( p );
    Abc_SclManCleanTime( p );
381
    p->nEstNodes = 0;
382 383
    Abc_NtkForEachCi( p->pNtk, pObj, i )
        Abc_SclTimeNode( p, pObj, 0 );
384
    Abc_NtkForEachNode1( p->pNtk, pObj, i )
385
        Abc_SclTimeNode( p, pObj, 0 );
386
    Abc_NtkForEachCo( p->pNtk, pObj, i )
387
        Abc_SclTimeNode( p, pObj, 0 );
388
    D = Abc_SclReadMaxDelay( p );
389 390
    if ( fReverse && DUser > 0 && D < DUser )
        D = DUser;
391
    if ( pArea )
392
        *pArea = Abc_SclGetTotalArea(p->pNtk);
393
    if ( pDelay )
394 395
        *pDelay = D;
    if ( fReverse )
396
    {
397
        p->nEstNodes = 0;
398
        Abc_NtkForEachNodeReverse1( p->pNtk, pObj, i )
399
            Abc_SclTimeNode( p, pObj, 1 );
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
    }
}

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

  Synopsis    [Incremental timing update.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline void Abc_SclTimeIncUpdateClean( SC_Man * p )
{
    Vec_Int_t * vLevel;
    Abc_Obj_t * pObj;
    int i, k;
    Vec_WecForEachLevel( p->vLevels, vLevel, i )
    {
        Abc_NtkForEachObjVec( vLevel, p->pNtk, pObj, k )
        {
            assert( pObj->fMarkC == 1 );
            pObj->fMarkC = 0;
        }
        Vec_IntClear( vLevel );
    }
}
static inline void Abc_SclTimeIncAddNode( SC_Man * p, Abc_Obj_t * pObj )
{
431
    assert( !Abc_ObjIsLatch(pObj) );
432 433 434 435 436 437 438 439 440 441
    assert( pObj->fMarkC == 0 );
    pObj->fMarkC = 1;
    Vec_IntPush( Vec_WecEntry(p->vLevels, Abc_ObjLevel(pObj)), Abc_ObjId(pObj) );
    p->nIncUpdates++;
}
static inline void Abc_SclTimeIncAddFanins( SC_Man * p, Abc_Obj_t * pObj )
{
    Abc_Obj_t * pFanin;
    int i;
    Abc_ObjForEachFanin( pObj, pFanin, i )
442
//        if ( !pFanin->fMarkC && Abc_ObjIsNode(pFanin) )
443
        if ( !pFanin->fMarkC && !Abc_ObjIsLatch(pFanin) )
444 445 446 447 448 449 450
            Abc_SclTimeIncAddNode( p, pFanin );
}
static inline void Abc_SclTimeIncAddFanouts( SC_Man * p, Abc_Obj_t * pObj )
{
    Abc_Obj_t * pFanout;
    int i;
    Abc_ObjForEachFanout( pObj, pFanout, i )
451
        if ( !pFanout->fMarkC && !Abc_ObjIsLatch(pFanout) )
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
            Abc_SclTimeIncAddNode( p, pFanout );
}
static inline void Abc_SclTimeIncUpdateArrival( SC_Man * p )
{
    Vec_Int_t * vLevel;
    SC_Pair ArrOut, SlewOut;
    SC_Pair * pArrOut, *pSlewOut;
    Abc_Obj_t * pObj;
    float E = (float)0.1;
    int i, k;
    Vec_WecForEachLevel( p->vLevels, vLevel, i )
    {
        Abc_NtkForEachObjVec( vLevel, p->pNtk, pObj, k )
        {
            if ( Abc_ObjIsCo(pObj) )
            {
                Abc_SclObjDupFanin( p, pObj );
                Vec_FltWriteEntry( p->vTimesOut, pObj->iData, Abc_SclObjTimeMax(p, pObj) );
                Vec_QueUpdate( p->vQue, pObj->iData );
                continue;
            }
            pArrOut  = Abc_SclObjTime( p, pObj );
            pSlewOut = Abc_SclObjSlew( p, pObj );
            SC_PairMove( &ArrOut,  pArrOut  );
            SC_PairMove( &SlewOut, pSlewOut );
            Abc_SclTimeNode( p, pObj, 0 );
//            if ( !SC_PairEqual(&ArrOut, pArrOut) || !SC_PairEqual(&SlewOut, pSlewOut) )
            if ( !SC_PairEqualE(&ArrOut, pArrOut, E) || !SC_PairEqualE(&SlewOut, pSlewOut, E) )
                Abc_SclTimeIncAddFanouts( p, pObj );
        }
    }
    p->MaxDelay = Abc_SclReadMaxDelay( p );
}
static inline void Abc_SclTimeIncUpdateDeparture( SC_Man * p )
{
    Vec_Int_t * vLevel;
    SC_Pair DepOut, * pDepOut;
    Abc_Obj_t * pObj;
    float E = (float)0.1;
    int i, k;
    Vec_WecForEachLevelReverse( p->vLevels, vLevel, i )
    {
        Abc_NtkForEachObjVec( vLevel, p->pNtk, pObj, k )
495
        {
496 497 498 499 500 501
            pDepOut = Abc_SclObjDept( p, pObj );
            SC_PairMove( &DepOut, pDepOut );
            Abc_SclDeptObj( p, pObj );
//            if ( !SC_PairEqual(&DepOut, pDepOut) )
            if ( !SC_PairEqualE(&DepOut, pDepOut, E) )
                Abc_SclTimeIncAddFanins( p, pObj );
502
        }
503
    } 
504 505
    p->MaxDelay = Abc_SclReadMaxDelay( p );
}
506 507 508 509 510 511 512 513
void Abc_SclTimeIncCheckLevel( Abc_Ntk_t * pNtk )
{
    Abc_Obj_t * pObj;
    int i;
    Abc_NtkForEachObj( pNtk, pObj, i )
        if ( (int)pObj->Level != Abc_ObjLevelNew(pObj) )
            printf( "Level of node %d is out of date!\n", i );
}
514
int Abc_SclTimeIncUpdate( SC_Man * p )
515
{
516
    Abc_Obj_t * pObj;
517
    int i, RetValue;
518
    if ( Vec_IntSize(p->vChanged) == 0 )
519
        return 0;
520 521 522
//    Abc_SclTimeIncCheckLevel( p->pNtk );
    Abc_NtkForEachObjVec( p->vChanged, p->pNtk, pObj, i )
    {
523
        Abc_SclTimeIncAddFanins( p, pObj );
524 525 526 527 528
        if ( pObj->fMarkC )
            continue;
        Abc_SclTimeIncAddNode( p, pObj );
    }
    Vec_IntClear( p->vChanged );
529 530 531
    Abc_SclTimeIncUpdateArrival( p );
    Abc_SclTimeIncUpdateDeparture( p );
    Abc_SclTimeIncUpdateClean( p );
532
    RetValue = p->nIncUpdates;
533
    p->nIncUpdates = 0;
534
    return RetValue;
535 536 537
}
void Abc_SclTimeIncInsert( SC_Man * p, Abc_Obj_t * pObj )
{
538
    Vec_IntPush( p->vChanged, Abc_ObjId(pObj) );
539
}
540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555
void Abc_SclTimeIncUpdateLevel_rec( Abc_Obj_t * pObj )
{
    Abc_Obj_t * pFanout;
    int i, LevelNew = Abc_ObjLevelNew(pObj);
    if ( LevelNew == (int)pObj->Level )
        return;
    pObj->Level = LevelNew;
    Abc_ObjForEachFanout( pObj, pFanout, i )
        Abc_SclTimeIncUpdateLevel_rec( pFanout );
}
void Abc_SclTimeIncUpdateLevel( Abc_Obj_t * pObj )
{
    Abc_SclTimeIncUpdateLevel_rec( pObj );
}


556

557 558
/**Function*************************************************************

559 560 561 562 563 564 565 566 567 568 569
  Synopsis    [Read input slew and output load.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Abc_SclManReadSlewAndLoad( SC_Man * p, Abc_Ntk_t * pNtk )
{
570 571
    if ( Abc_FrameReadMaxLoad() )
    {
572
        Abc_Obj_t * pObj;  int i;
573
        float MaxLoad = Abc_FrameReadMaxLoad();
574
//        printf( "Default output load is specified (%.2f ff).\n", MaxLoad );
575 576 577
        Abc_NtkForEachPo( pNtk, pObj, i )
        {
            SC_Pair * pLoad = Abc_SclObjLoad( p, pObj );
578
            pLoad->rise = pLoad->fall = MaxLoad;
579 580 581 582 583 584 585 586 587 588 589 590
        }
    }
    if ( Abc_FrameReadDrivingCell() )
    {
        int iCell = Abc_SclCellFind( p->pLib, Abc_FrameReadDrivingCell() );
        if ( iCell == -1 )
            printf( "Cannot find the default PI driving cell (%s) in the library.\n", Abc_FrameReadDrivingCell() );
        else
        {
//            printf( "Default PI driving cell is specified (%s).\n", Abc_FrameReadDrivingCell() );
            p->pPiDrive = SC_LibCell( p->pLib, iCell );
            assert( p->pPiDrive != NULL );
591
            assert( p->pPiDrive->n_inputs == 1 );
592 593
        }
    }
594
}
595
 
596 597
/**Function*************************************************************

598
  Synopsis    [Prepare timing manager.]
599 600 601 602 603 604 605 606

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
607
SC_Man * Abc_SclManStart( SC_Lib * pLib, Abc_Ntk_t * pNtk, int fUseWireLoads, int fDept, float DUser, int nTreeCRatio )
608 609
{
    SC_Man * p = Abc_SclManAlloc( pLib, pNtk );
610 611 612 613 614
    if ( nTreeCRatio )
    {
        p->EstLoadMax = 0.01 * nTreeCRatio;  // max ratio of Cout/Cave when the estimation is used
        p->EstLinear  = 100;                  // linear coefficient
    }
615
    Abc_SclMioGates2SclGates( pLib, pNtk );
616
    Abc_SclManReadSlewAndLoad( p, pNtk );
617
    if ( fUseWireLoads )
618 619 620
    {
        if ( pNtk->pWLoadUsed == NULL )
        {            
621
            p->pWLoadUsed = Abc_SclFindWireLoadModel( pLib, Abc_SclGetTotalArea(p->pNtk) );
622
            if ( p->pWLoadUsed )
623 624 625 626 627
            pNtk->pWLoadUsed = Abc_UtilStrsav( p->pWLoadUsed->pName );
        }
        else
            p->pWLoadUsed = Abc_SclFetchWireLoadModel( pLib, pNtk->pWLoadUsed );
    }
628
    Abc_SclTimeNtkRecompute( p, &p->SumArea0, &p->MaxDelay0, fDept, DUser );
629 630
    p->SumArea  = p->SumArea0;
    p->MaxDelay = p->MaxDelay0;
631 632
    return p;
}
633

634 635
/**Function*************************************************************

636
  Synopsis    [Printing out timing information for the network.]
637 638 639 640 641 642 643 644

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
645
void Abc_SclTimePerform( SC_Lib * pLib, Abc_Ntk_t * pNtk, int nTreeCRatio, int fUseWireLoads, int fShowAll, int fPrintPath, int fDumpStats )
646 647
{
    SC_Man * p;
648
    p = Abc_SclManStart( pLib, pNtk, fUseWireLoads, 1, 0, nTreeCRatio );
649
    Abc_SclTimeNtkPrint( p, fShowAll, fPrintPath );
650 651
    if ( fDumpStats )
        Abc_SclDumpStats( p, "stats.txt", 0 );
652 653 654
    Abc_SclManFree( p );
}

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 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711

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

  Synopsis    [Printing out fanin information.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Abc_SclCheckCommonInputs( Abc_Obj_t * pObj, Abc_Obj_t * pFanin )
{
    Abc_Obj_t * pTemp;
    int i;
    Abc_ObjForEachFanin( pObj, pTemp, i )
        if ( Abc_NodeFindFanin( pFanin, pTemp ) >= 0 )
        {
            printf( "Node %d and its fanin %d have common fanin %d.\n", Abc_ObjId(pObj), Abc_ObjId(pFanin), Abc_ObjId(pTemp) );

            printf( "%-16s : ", Mio_GateReadName((Mio_Gate_t *)pObj->pData) ); 
            Abc_ObjPrint( stdout, pObj );

            printf( "%-16s : ", Mio_GateReadName((Mio_Gate_t *)pFanin->pData) ); 
            Abc_ObjPrint( stdout, pFanin );

            if ( pTemp->pData )
            printf( "%-16s : ", Mio_GateReadName((Mio_Gate_t *)pTemp->pData) ); 
            Abc_ObjPrint( stdout, pTemp );
            return 1;
        }
    return 0;
}
void Abc_SclPrintFaninPairs( SC_Man * p, Abc_Ntk_t * pNtk )
{
    Abc_Obj_t * pObj, * pFanin;
    int i, k;
    Abc_NtkForEachNode( pNtk, pObj, i )
        Abc_ObjForEachFanin( pObj, pFanin, k )
            if ( Abc_ObjIsNode(pFanin) && Abc_ObjFanoutNum(pFanin) == 1 )
                Abc_SclCheckCommonInputs( pObj, pFanin );
}

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

  Synopsis    [Printing out buffer information.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline int Abc_ObjIsBuffer( Abc_Obj_t * pObj ) { return Abc_ObjIsNode(pObj) && Abc_ObjFaninNum(pObj) == 1; }
712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734
int Abc_SclHasBufferFanout( Abc_Obj_t * pObj )
{
    Abc_Obj_t * pFanout;
    int i;
    Abc_ObjForEachFanout( pObj, pFanout, i )
        if ( Abc_ObjIsBuffer(pFanout) )
            return 1;
    return 0;
}
int Abc_SclCountBufferFanoutsInt( Abc_Obj_t * pObj )
{
    Abc_Obj_t * pFanout;
    int i, Counter = 0;
    Abc_ObjForEachFanout( pObj, pFanout, i )
        if ( Abc_ObjIsBuffer(pFanout) )
            Counter += Abc_SclCountBufferFanoutsInt( pFanout );
    return Counter + Abc_ObjIsBuffer(pObj);
}
int Abc_SclCountBufferFanouts( Abc_Obj_t * pObj )
{
    return Abc_SclCountBufferFanoutsInt(pObj) - Abc_ObjIsBuffer(pObj);
}
int Abc_SclCountNonBufferFanoutsInt( Abc_Obj_t * pObj )
735 736 737 738 739 740
{
    Abc_Obj_t * pFanout;
    int i, Counter = 0;
    if ( !Abc_ObjIsBuffer(pObj) )
        return 1;
    Abc_ObjForEachFanout( pObj, pFanout, i )
741
        Counter += Abc_SclCountNonBufferFanoutsInt( pFanout );
742 743
    return Counter;
}
744
int Abc_SclCountNonBufferFanouts( Abc_Obj_t * pObj )
745 746
{
    Abc_Obj_t * pFanout;
747
    int i, Counter = 0;
748
    Abc_ObjForEachFanout( pObj, pFanout, i )
749 750
        Counter += Abc_SclCountNonBufferFanoutsInt( pFanout );
    return Counter;
751
}
752 753 754 755 756 757
float Abc_SclCountNonBufferDelayInt( SC_Man * p, Abc_Obj_t * pObj )
{
    Abc_Obj_t * pFanout;
    float Delay = 0;
    int i; 
    if ( !Abc_ObjIsBuffer(pObj) )
758
        return Abc_SclObjTimeMax(p, pObj);
759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781
    Abc_ObjForEachFanout( pObj, pFanout, i )
        Delay += Abc_SclCountNonBufferDelayInt( p, pFanout );
    return Delay;
}
float Abc_SclCountNonBufferDelay( SC_Man * p, Abc_Obj_t * pObj )
{
    Abc_Obj_t * pFanout;
    float Delay = 0;
    int i; 
    Abc_ObjForEachFanout( pObj, pFanout, i )
        Delay += Abc_SclCountNonBufferDelayInt( p, pFanout );
    return Delay;
}
float Abc_SclCountNonBufferLoadInt( SC_Man * p, Abc_Obj_t * pObj )
{
    Abc_Obj_t * pFanout;
    float Load = 0;
    int i; 
    if ( !Abc_ObjIsBuffer(pObj) )
        return 0;
    Abc_ObjForEachFanout( pObj, pFanout, i )
        Load += Abc_SclCountNonBufferLoadInt( p, pFanout );
    Load += 0.5 * Abc_SclObjLoad(p, pObj)->rise + 0.5 * Abc_SclObjLoad(p, pObj)->fall;
782
    Load -= 0.5 * SC_CellPin(Abc_SclObjCell(pObj), 0)->rise_cap + 0.5 * SC_CellPin(Abc_SclObjCell(pObj), 0)->fall_cap;
783 784 785
    return Load;
}
float Abc_SclCountNonBufferLoad( SC_Man * p, Abc_Obj_t * pObj )
786 787
{
    Abc_Obj_t * pFanout;
788 789 790 791 792 793 794 795 796
    float Load = 0;
    int i; 
    Abc_ObjForEachFanout( pObj, pFanout, i )
        Load += Abc_SclCountNonBufferLoadInt( p, pFanout );
    Load += 0.5 * Abc_SclObjLoad(p, pObj)->rise + 0.5 * Abc_SclObjLoad(p, pObj)->fall;
    return Load;
}
void Abc_SclPrintBuffersOne( SC_Man * p, Abc_Obj_t * pObj, int nOffset )
{
797 798 799
    int i;
    for ( i = 0; i < nOffset; i++ )
        printf( "    " );
800 801 802 803 804 805
    printf( "%6d: %-16s (%2d:%3d:%3d)  ", 
        Abc_ObjId(pObj), 
        Abc_ObjIsPi(pObj) ? "pi" : Mio_GateReadName((Mio_Gate_t *)pObj->pData), 
        Abc_ObjFanoutNum(pObj), 
        Abc_SclCountBufferFanouts(pObj), 
        Abc_SclCountNonBufferFanouts(pObj) );
806 807
    for ( ; i < 4; i++ )
        printf( "    " );
808
    printf( "a =%5.2f  ",      Abc_ObjIsPi(pObj) ? 0 : Abc_SclObjCell(pObj)->area );
809
    printf( "d = (" );
810 811 812 813 814
    printf( "%6.0f ps; ",      Abc_SclObjTimeOne(p, pObj, 1) );
    printf( "%6.0f ps)  ",     Abc_SclObjTimeOne(p, pObj, 0) );
    printf( "l =%5.0f ff  ",   Abc_SclObjLoadMax(p, pObj) );
    printf( "s =%5.0f ps   ",  Abc_SclObjSlewMax(p, pObj) );
    printf( "sl =%5.0f ps   ", Abc_SclObjSlackMax(p, pObj, p->MaxDelay0) );
815 816
    if ( nOffset == 0 )
    {
817
    printf( "L =%5.0f ff   ",  Abc_SclCountNonBufferLoad(p, pObj) );
818
    printf( "Lx =%5.0f ff  ",  100.0*Abc_SclCountNonBufferLoad(p, pObj)/p->EstLoadAve );
819 820
    printf( "Dx =%5.0f ps  ",  Abc_SclCountNonBufferDelay(p, pObj)/Abc_SclCountNonBufferFanouts(pObj) - Abc_SclObjTimeOne(p, pObj, 1) );
    printf( "Cx =%5.0f ps",    (Abc_SclCountNonBufferDelay(p, pObj)/Abc_SclCountNonBufferFanouts(pObj) - Abc_SclObjTimeOne(p, pObj, 1))/log(Abc_SclCountNonBufferLoad(p, pObj)/p->EstLoadAve) );
821
    }
822
    printf( "\n" );
823 824 825 826 827 828 829
}
void Abc_SclPrintBuffersInt( SC_Man * p, Abc_Obj_t * pObj, int nOffset )
{
    Abc_Obj_t * pFanout;
    int i;
    Abc_SclPrintBuffersOne( p, pObj, nOffset );
    assert( Abc_ObjIsBuffer(pObj) );
830 831
    Abc_ObjForEachFanout( pObj, pFanout, i )
        if ( Abc_ObjIsBuffer(pFanout) )
832
            Abc_SclPrintBuffersInt( p, pFanout, nOffset + 1 );
833 834 835
}
void Abc_SclPrintBufferTrees( SC_Man * p, Abc_Ntk_t * pNtk )
{
836 837
    Abc_Obj_t * pObj, * pFanout;
    int i, k;
838
    Abc_NtkForEachObj( pNtk, pObj, i )
839 840 841 842 843 844 845 846 847 848
    {
        if ( !Abc_ObjIsBuffer(pObj) && Abc_SclCountBufferFanouts(pObj) > 3 )
        {           
            Abc_SclPrintBuffersOne( p, pObj, 0 );
            Abc_ObjForEachFanout( pObj, pFanout, k )
                if ( Abc_ObjIsBuffer(pFanout) )
                    Abc_SclPrintBuffersInt( p, pFanout, 1 );
            printf( "\n" );
        }
    }
849 850 851 852 853 854
}
void Abc_SclPrintBuffers( SC_Lib * pLib, Abc_Ntk_t * pNtk, int fVerbose )
{
    int fUseWireLoads = 0;
    SC_Man * p;
    assert( Abc_NtkIsMappedLogic(pNtk) );
855
    p = Abc_SclManStart( pLib, pNtk, fUseWireLoads, 1, 0, 10000 ); 
856 857 858 859 860 861
    Abc_SclPrintBufferTrees( p, pNtk ); 
//    Abc_SclPrintFaninPairs( p, pNtk );
    Abc_SclManFree( p );
}


862 863 864 865 866 867 868
////////////////////////////////////////////////////////////////////////
///                       END OF FILE                                ///
////////////////////////////////////////////////////////////////////////


ABC_NAMESPACE_IMPL_END