abcTiming.c 45.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
/**CFile****************************************************************

  FileName    [abcTiming.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [Network and node package.]

  Synopsis    [Computation of timing info for mapped circuits.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

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

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

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

21 22
#include <math.h>

23 24 25
#include "base/abc/abc.h"
#include "base/main/main.h"
#include "map/mio/mio.h"
Alan Mishchenko committed
26

27 28 29
ABC_NAMESPACE_IMPL_START


Alan Mishchenko committed
30 31 32 33 34 35 36 37 38 39
////////////////////////////////////////////////////////////////////////
///                        DECLARATIONS                              ///
////////////////////////////////////////////////////////////////////////

struct Abc_ManTime_t_
{
    Abc_Time_t     tArrDef;
    Abc_Time_t     tReqDef;
    Vec_Ptr_t  *   vArrs;
    Vec_Ptr_t  *   vReqs;
40 41 42 43
    Abc_Time_t     tInDriveDef;
    Abc_Time_t     tOutLoadDef;
    Abc_Time_t *   tInDrive;
    Abc_Time_t *   tOutLoad;
Alan Mishchenko committed
44 45
};

46 47 48 49
#define TOLERANCE 0.001

static inline int          Abc_FloatEqual( float x, float y )     { return fabs(x-y) < TOLERANCE; }

Alan Mishchenko committed
50
// static functions
51
static Abc_ManTime_t *     Abc_ManTimeStart( Abc_Ntk_t * pNtk );
Alan Mishchenko committed
52 53 54
static void                Abc_ManTimeExpand( Abc_ManTime_t * p, int nSize, int fProgressive );

// accessing the arrival and required times of a node
55 56
static inline Abc_Time_t * Abc_NodeArrival( Abc_Obj_t * pNode )  {  return (Abc_Time_t *)pNode->pNtk->pManTime->vArrs->pArray[pNode->Id];  }
static inline Abc_Time_t * Abc_NodeRequired( Abc_Obj_t * pNode ) {  return (Abc_Time_t *)pNode->pNtk->pManTime->vReqs->pArray[pNode->Id];  }
Alan Mishchenko committed
57 58

////////////////////////////////////////////////////////////////////////
Alan Mishchenko committed
59
///                     FUNCTION DEFINITIONS                         ///
Alan Mishchenko committed
60 61 62 63
////////////////////////////////////////////////////////////////////////

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

64
  Synopsis    [Reads the arrival.required time of the node.]
Alan Mishchenko committed
65 66 67 68 69 70 71 72

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
73 74 75 76 77 78 79 80 81 82
Abc_Time_t * Abc_NtkReadDefaultArrival( Abc_Ntk_t * pNtk )
{
    assert( pNtk->pManTime );
    return &pNtk->pManTime->tArrDef;
}
Abc_Time_t * Abc_NtkReadDefaultRequired( Abc_Ntk_t * pNtk )
{
    assert( pNtk->pManTime );
    return &pNtk->pManTime->tReqDef;
}
Alan Mishchenko committed
83 84 85 86 87 88 89 90 91 92
Abc_Time_t * Abc_NodeReadArrival( Abc_Obj_t * pNode )
{
    assert( pNode->pNtk->pManTime );
    return Abc_NodeArrival(pNode);
}
Abc_Time_t * Abc_NodeReadRequired( Abc_Obj_t * pNode )
{
    assert( pNode->pNtk->pManTime );
    return Abc_NodeRequired(pNode);
}
93 94 95 96 97 98 99 100
float Abc_NtkReadDefaultArrivalWorst( Abc_Ntk_t * pNtk )
{
    return 0.5 * pNtk->pManTime->tArrDef.Rise + 0.5 * pNtk->pManTime->tArrDef.Fall;
}
float Abc_NtkReadDefaultRequiredWorst( Abc_Ntk_t * pNtk )
{
    return 0.5 * pNtk->pManTime->tReqDef.Rise + 0.5 * pNtk->pManTime->tReqDef.Fall;
}
101 102 103 104 105 106 107 108 109 110 111 112 113 114
float Abc_NodeReadArrivalAve( Abc_Obj_t * pNode )
{
    return 0.5 * Abc_NodeArrival(pNode)->Rise + 0.5 * Abc_NodeArrival(pNode)->Fall;
}
float Abc_NodeReadRequiredAve( Abc_Obj_t * pNode )
{
    return 0.5 * Abc_NodeReadRequired(pNode)->Rise + 0.5 * Abc_NodeReadRequired(pNode)->Fall;
}
float Abc_NodeReadArrivalWorst( Abc_Obj_t * pNode )
{
    return Abc_MaxFloat( Abc_NodeArrival(pNode)->Rise, Abc_NodeArrival(pNode)->Fall );
}
float Abc_NodeReadRequiredWorst( Abc_Obj_t * pNode )
{
115
    return Abc_MinFloat( Abc_NodeReadRequired(pNode)->Rise, Abc_NodeReadRequired(pNode)->Fall );
116
}
Alan Mishchenko committed
117 118 119

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

120
  Synopsis    [Reads the input drive / output load of the node.]
Alan Mishchenko committed
121 122 123 124 125 126 127 128

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
129
Abc_Time_t * Abc_NtkReadDefaultInputDrive( Abc_Ntk_t * pNtk )
Alan Mishchenko committed
130 131
{
    assert( pNtk->pManTime );
132
    return &pNtk->pManTime->tInDriveDef;
Alan Mishchenko committed
133
}
134
Abc_Time_t * Abc_NtkReadDefaultOutputLoad( Abc_Ntk_t * pNtk )
Alan Mishchenko committed
135 136
{
    assert( pNtk->pManTime );
137
    return &pNtk->pManTime->tOutLoadDef;
Alan Mishchenko committed
138
}
139
Abc_Time_t * Abc_NodeReadInputDrive( Abc_Ntk_t * pNtk, int iPi )
140
{
141
    assert( pNtk->pManTime );
142
    return pNtk->pManTime->tInDrive ? pNtk->pManTime->tInDrive + iPi : NULL;
143
}
144
Abc_Time_t * Abc_NodeReadOutputLoad( Abc_Ntk_t * pNtk, int iPo )
145
{
146
    assert( pNtk->pManTime );
147
    return pNtk->pManTime->tOutLoad ? pNtk->pManTime->tOutLoad + iPo : NULL;
148 149 150 151 152 153 154 155
}
float Abc_NodeReadInputDriveWorst( Abc_Ntk_t * pNtk, int iPi )
{
    return Abc_MaxFloat( Abc_NodeReadInputDrive(pNtk, iPi)->Rise, Abc_NodeReadInputDrive(pNtk, iPi)->Fall );
}
float Abc_NodeReadOutputLoadWorst( Abc_Ntk_t * pNtk, int iPo )
{
    return Abc_MaxFloat( Abc_NodeReadOutputLoad(pNtk, iPo)->Rise, Abc_NodeReadOutputLoad(pNtk, iPo)->Fall );
156 157 158 159
}

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

Alan Mishchenko committed
160 161
  Synopsis    [Sets the default arrival time for the network.]

162 163 164
  Description [Please note that .default_input_arrival and
  .default_output_required should precede .input_arrival and 
  .output required. Otherwise, an overwrite may happen.]
Alan Mishchenko committed
165 166 167 168 169 170 171 172
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Abc_NtkTimeSetDefaultArrival( Abc_Ntk_t * pNtk, float Rise, float Fall )
{
173
    Abc_Obj_t * pObj; int i;
Alan Mishchenko committed
174
    if ( pNtk->pManTime == NULL )
175
        pNtk->pManTime = Abc_ManTimeStart(pNtk);
Alan Mishchenko committed
176 177
    pNtk->pManTime->tArrDef.Rise  = Rise;
    pNtk->pManTime->tArrDef.Fall  = Fall;
178
    // set the arrival times for each input
179 180
    Abc_NtkForEachCi( pNtk, pObj, i )
        Abc_NtkTimeSetArrival( pNtk, Abc_ObjId(pObj), Rise, Fall );    
Alan Mishchenko committed
181 182 183
}
void Abc_NtkTimeSetDefaultRequired( Abc_Ntk_t * pNtk, float Rise, float Fall )
{
184
    Abc_Obj_t * pObj; int i;
Alan Mishchenko committed
185
    if ( pNtk->pManTime == NULL )
186
        pNtk->pManTime = Abc_ManTimeStart(pNtk);
Alan Mishchenko committed
187
    pNtk->pManTime->tReqDef.Rise  = Rise;
Alan Mishchenko committed
188
    pNtk->pManTime->tReqDef.Fall  = Fall;
189
    // set the required times for each output
190 191
    Abc_NtkForEachCo( pNtk, pObj, i )
        Abc_NtkTimeSetRequired( pNtk, Abc_ObjId(pObj), Rise, Fall );        
Alan Mishchenko committed
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
}

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

  Synopsis    [Sets the arrival time for an object.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Abc_NtkTimeSetArrival( Abc_Ntk_t * pNtk, int ObjId, float Rise, float Fall )
{
    Vec_Ptr_t * vTimes;
    Abc_Time_t * pTime;
    if ( pNtk->pManTime == NULL )
210
        pNtk->pManTime = Abc_ManTimeStart(pNtk);
Alan Mishchenko committed
211 212 213
    Abc_ManTimeExpand( pNtk->pManTime, ObjId + 1, 1 );
    // set the arrival time
    vTimes = pNtk->pManTime->vArrs;
214
    pTime = (Abc_Time_t *)vTimes->pArray[ObjId];
Alan Mishchenko committed
215
    pTime->Rise  = Rise;
Alan Mishchenko committed
216
    pTime->Fall  = Fall;
217 218 219 220 221 222
}
void Abc_NtkTimeSetRequired( Abc_Ntk_t * pNtk, int ObjId, float Rise, float Fall )
{
    Vec_Ptr_t * vTimes;
    Abc_Time_t * pTime;
    if ( pNtk->pManTime == NULL )
223
        pNtk->pManTime = Abc_ManTimeStart(pNtk);
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
    Abc_ManTimeExpand( pNtk->pManTime, ObjId + 1, 1 );
    // set the required time
    vTimes = pNtk->pManTime->vReqs;
    pTime = (Abc_Time_t *)vTimes->pArray[ObjId];
    pTime->Rise  = Rise;
    pTime->Fall  = Fall;
}

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

  Synopsis    [Sets the default arrival time for the network.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Abc_NtkTimeSetDefaultInputDrive( Abc_Ntk_t * pNtk, float Rise, float Fall )
{
245 246
//    if ( Rise == 0.0 && Fall == 0.0 )
//        return;
247
    if ( pNtk->pManTime == NULL )
248
        pNtk->pManTime = Abc_ManTimeStart(pNtk);
249 250
    pNtk->pManTime->tInDriveDef.Rise  = Rise;
    pNtk->pManTime->tInDriveDef.Fall  = Fall;
251 252 253 254 255 256 257
    if ( pNtk->pManTime->tInDrive != NULL )
    {
        int i;
        for ( i = 0; i < Abc_NtkCiNum(pNtk); i++ )
            if ( pNtk->pManTime->tInDrive[i].Rise == 0 && pNtk->pManTime->tInDrive[i].Fall == 0 )
                pNtk->pManTime->tInDrive[i] = pNtk->pManTime->tInDriveDef;
    }
258 259 260
}
void Abc_NtkTimeSetDefaultOutputLoad( Abc_Ntk_t * pNtk, float Rise, float Fall )
{
261 262
//    if ( Rise == 0.0 && Fall == 0.0 )
//        return;
263
    if ( pNtk->pManTime == NULL )
264
        pNtk->pManTime = Abc_ManTimeStart(pNtk);
265 266
    pNtk->pManTime->tOutLoadDef.Rise  = Rise;
    pNtk->pManTime->tOutLoadDef.Fall  = Fall;
267 268 269 270 271 272 273
    if ( pNtk->pManTime->tOutLoad != NULL )
    {
        int i;
        for ( i = 0; i < Abc_NtkCoNum(pNtk); i++ )
            if ( pNtk->pManTime->tOutLoad[i].Rise == 0 && pNtk->pManTime->tOutLoad[i].Fall == 0 )
                pNtk->pManTime->tOutLoad[i] = pNtk->pManTime->tOutLoadDef;
    }
Alan Mishchenko committed
274 275 276 277 278 279 280 281 282 283 284 285 286
}

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

  Synopsis    [Sets the arrival time for an object.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
287
void Abc_NtkTimeSetInputDrive( Abc_Ntk_t * pNtk, int PiNum, float Rise, float Fall )
Alan Mishchenko committed
288 289
{
    Abc_Time_t * pTime;
290
    assert( PiNum >= 0 && PiNum < Abc_NtkCiNum(pNtk) );
Alan Mishchenko committed
291
    if ( pNtk->pManTime == NULL )
292
        pNtk->pManTime = Abc_ManTimeStart(pNtk);
293
    if ( pNtk->pManTime->tInDriveDef.Rise == Rise && pNtk->pManTime->tInDriveDef.Fall == Fall )
Alan Mishchenko committed
294
        return;
295
    if ( pNtk->pManTime->tInDrive == NULL )
296 297
    {
        int i;
298
        pNtk->pManTime->tInDrive = ABC_CALLOC( Abc_Time_t, Abc_NtkCiNum(pNtk) );
299 300 301
        for ( i = 0; i < Abc_NtkCiNum(pNtk); i++ )
            pNtk->pManTime->tInDrive[i] = pNtk->pManTime->tInDriveDef;
    }
302 303 304 305 306 307 308 309 310
    pTime = pNtk->pManTime->tInDrive + PiNum;
    pTime->Rise  = Rise;
    pTime->Fall  = Fall;
}
void Abc_NtkTimeSetOutputLoad( Abc_Ntk_t * pNtk, int PoNum, float Rise, float Fall )
{
    Abc_Time_t * pTime;
    assert( PoNum >= 0 && PoNum < Abc_NtkCoNum(pNtk) );
    if ( pNtk->pManTime == NULL )
311
        pNtk->pManTime = Abc_ManTimeStart(pNtk);
312 313 314
    if ( pNtk->pManTime->tOutLoadDef.Rise == Rise && pNtk->pManTime->tOutLoadDef.Fall == Fall )
        return;
    if ( pNtk->pManTime->tOutLoad == NULL )
315 316
    {
        int i;
317
        pNtk->pManTime->tOutLoad = ABC_CALLOC( Abc_Time_t, Abc_NtkCoNum(pNtk) );
318 319 320
        for ( i = 0; i < Abc_NtkCoNum(pNtk); i++ )
            pNtk->pManTime->tOutLoad[i] = pNtk->pManTime->tOutLoadDef;
    }
321
    pTime = pNtk->pManTime->tOutLoad + PoNum;
Alan Mishchenko committed
322
    pTime->Rise  = Rise;
Alan Mishchenko committed
323
    pTime->Fall  = Fall;
Alan Mishchenko committed
324 325 326 327 328 329 330 331 332 333 334 335 336
}

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

  Synopsis    [Finalizes the timing manager after setting arr/req times.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
337
void Abc_NtkTimeInitialize( Abc_Ntk_t * pNtk, Abc_Ntk_t * pNtkOld )
Alan Mishchenko committed
338 339
{
    Abc_Obj_t * pObj;
340
    Abc_Time_t ** ppTimes;
Alan Mishchenko committed
341
    int i;
342
    assert( pNtkOld == NULL || pNtkOld->pManTime != NULL );
343 344
    assert( pNtkOld == NULL || Abc_NtkCiNum(pNtk) == Abc_NtkCiNum(pNtkOld) );
    assert( pNtkOld == NULL || Abc_NtkCoNum(pNtk) == Abc_NtkCoNum(pNtkOld) );
Alan Mishchenko committed
345 346
    if ( pNtk->pManTime == NULL )
        return;
347
    // create timing manager with default values
Alan Mishchenko committed
348
    Abc_ManTimeExpand( pNtk->pManTime, Abc_NtkObjNumMax(pNtk), 0 );
349
    // set global defaults from pNtkOld
350 351 352 353
    if ( pNtkOld )
    {
        pNtk->pManTime->tArrDef = pNtkOld->pManTime->tArrDef;
        pNtk->pManTime->tReqDef = pNtkOld->pManTime->tReqDef;
354
        pNtk->AndGateDelay = pNtkOld->AndGateDelay;
355
    }
356
    // set the default timing for CI and COs
Alan Mishchenko committed
357
    ppTimes = (Abc_Time_t **)pNtk->pManTime->vArrs->pArray;
358
    Abc_NtkForEachCi( pNtk, pObj, i )
359
        *ppTimes[pObj->Id] = pNtkOld ? *Abc_NodeReadArrival(Abc_NtkCi(pNtkOld, i)) : pNtk->pManTime->tArrDef;
Alan Mishchenko committed
360
    ppTimes = (Abc_Time_t **)pNtk->pManTime->vReqs->pArray;
361
    Abc_NtkForEachCo( pNtk, pObj, i )
362
        *ppTimes[pObj->Id] = pNtkOld ? *Abc_NodeReadRequired(Abc_NtkCo(pNtkOld, i)) : pNtk->pManTime->tReqDef;
Alan Mishchenko committed
363 364 365 366
}

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

367
  Synopsis    [This procedure scales user timing by multiplicative factor.]
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Abc_NtkTimeScale( Abc_Ntk_t * pNtk, float Scale )
{
    Abc_Obj_t * pObj;
    Abc_Time_t ** ppTimes, * pTime;
    int i;
    if ( pNtk->pManTime == NULL )
        return;
    // arrival
    pNtk->pManTime->tArrDef.Fall *= Scale;
    pNtk->pManTime->tArrDef.Rise *= Scale;
    // departure
387 388
    pNtk->pManTime->tReqDef.Fall *= Scale;
    pNtk->pManTime->tReqDef.Rise *= Scale;
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
    // set the default timing
    ppTimes = (Abc_Time_t **)pNtk->pManTime->vArrs->pArray;
    Abc_NtkForEachCi( pNtk, pObj, i )
    {
        pTime = ppTimes[pObj->Id];
        pTime->Fall *= Scale;
        pTime->Rise *= Scale;
    }
    // set the default timing
    ppTimes = (Abc_Time_t **)pNtk->pManTime->vReqs->pArray;
    Abc_NtkForEachCo( pNtk, pObj, i )
    {
        pTime = ppTimes[pObj->Id];
        pTime->Fall *= Scale;
        pTime->Rise *= Scale;
    }
}

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

Alan Mishchenko committed
409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
  Synopsis    [Prepares the timing manager for delay trace.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Abc_NtkTimePrepare( Abc_Ntk_t * pNtk )
{
    Abc_Obj_t * pObj;
    Abc_Time_t ** ppTimes, * pTime;
    int i;
    // if there is no timing manager, allocate and initialize
    if ( pNtk->pManTime == NULL )
    {
426
        pNtk->pManTime = Abc_ManTimeStart(pNtk);
427
        Abc_NtkTimeInitialize( pNtk, NULL );
Alan Mishchenko committed
428 429
        return;
    }
Alan Mishchenko committed
430
    // if timing manager is given, expand it if necessary
Alan Mishchenko committed
431
    Abc_ManTimeExpand( pNtk->pManTime, Abc_NtkObjNumMax(pNtk), 0 );
Alan Mishchenko committed
432
    // clean arrivals except for PIs
Alan Mishchenko committed
433 434 435 436
    ppTimes = (Abc_Time_t **)pNtk->pManTime->vArrs->pArray;
    Abc_NtkForEachNode( pNtk, pObj, i )
    {
        pTime = ppTimes[pObj->Id];
437
        pTime->Fall = pTime->Rise = Abc_ObjFaninNum(pObj) ? -ABC_INFINITY : 0; // set contant node arrivals to zero
Alan Mishchenko committed
438
    }
439
    Abc_NtkForEachCo( pNtk, pObj, i )
Alan Mishchenko committed
440 441
    {
        pTime = ppTimes[pObj->Id];
442
        pTime->Fall = pTime->Rise = -ABC_INFINITY;
Alan Mishchenko committed
443
    }
Alan Mishchenko committed
444
    // clean required except for POs
445 446 447 448
    ppTimes = (Abc_Time_t **)pNtk->pManTime->vReqs->pArray;
    Abc_NtkForEachNode( pNtk, pObj, i )
    {
        pTime = ppTimes[pObj->Id];
449
        pTime->Fall = pTime->Rise = ABC_INFINITY;
450
    }
451
    Abc_NtkForEachCi( pNtk, pObj, i )
452 453
    {
        pTime = ppTimes[pObj->Id];
454
        pTime->Fall = pTime->Rise = ABC_INFINITY;
455
    }
Alan Mishchenko committed
456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471
}




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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
472
Abc_ManTime_t * Abc_ManTimeStart( Abc_Ntk_t * pNtk )
Alan Mishchenko committed
473
{
474
    int fUseZeroDefaultOutputRequired = 1;
Alan Mishchenko committed
475
    Abc_ManTime_t * p;
476 477
    Abc_Obj_t * pObj; int i;
    p = pNtk->pManTime = ABC_ALLOC( Abc_ManTime_t, 1 );
Alan Mishchenko committed
478 479 480
    memset( p, 0, sizeof(Abc_ManTime_t) );
    p->vArrs = Vec_PtrAlloc( 0 );
    p->vReqs = Vec_PtrAlloc( 0 );
481 482 483 484 485
    // set default default input=arrivals (assumed to be 0)
    // set default default output-requireds (can be either 0 or +infinity, based on the flag)
    p->tReqDef.Rise = fUseZeroDefaultOutputRequired ? 0 : ABC_INFINITY;
    p->tReqDef.Fall = fUseZeroDefaultOutputRequired ? 0 : ABC_INFINITY;
    // extend manager
486
    Abc_ManTimeExpand( p, Abc_NtkObjNumMax(pNtk) + 1, 0 );
487
    // set the default timing for CIs
488
    Abc_NtkForEachCi( pNtk, pObj, i )
489 490
        Abc_NtkTimeSetArrival( pNtk, Abc_ObjId(pObj), p->tArrDef.Rise, p->tArrDef.Rise );   
    // set the default timing for COs
491
    Abc_NtkForEachCo( pNtk, pObj, i )
492
        Abc_NtkTimeSetRequired( pNtk, Abc_ObjId(pObj), p->tReqDef.Rise, p->tReqDef.Rise );        
Alan Mishchenko committed
493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
    return p;
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Abc_ManTimeStop( Abc_ManTime_t * p )
{
509 510 511 512
    if ( p->tInDrive )
        ABC_FREE( p->tInDrive );
    if ( p->tOutLoad )
        ABC_FREE( p->tOutLoad );
Alan Mishchenko committed
513
    if ( Vec_PtrSize(p->vArrs) > 0 )
Alan Mishchenko committed
514
        ABC_FREE( p->vArrs->pArray[0] );
Alan Mishchenko committed
515 516
    Vec_PtrFree( p->vArrs );
    if ( Vec_PtrSize(p->vReqs) > 0 )
Alan Mishchenko committed
517
        ABC_FREE( p->vReqs->pArray[0] );
Alan Mishchenko committed
518
    Vec_PtrFree( p->vReqs );
Alan Mishchenko committed
519
    ABC_FREE( p );
Alan Mishchenko committed
520 521 522 523 524 525 526 527 528 529 530 531 532 533 534
}

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

  Synopsis    [Duplicates the timing manager with the PI/PO timing info.]

  Description [The PIs/POs of the new network should be allocated.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Abc_ManTimeDup( Abc_Ntk_t * pNtkOld, Abc_Ntk_t * pNtkNew )
{
535 536
    extern void Abc_NtkTimePrint( Abc_Ntk_t * pNtk );

Alan Mishchenko committed
537 538 539 540 541
    Abc_Obj_t * pObj;
    Abc_Time_t ** ppTimesOld, ** ppTimesNew;
    int i;
    if ( pNtkOld->pManTime == NULL )
        return;
542 543
    assert( Abc_NtkCiNum(pNtkOld) == Abc_NtkCiNum(pNtkNew) );
    assert( Abc_NtkCoNum(pNtkOld) == Abc_NtkCoNum(pNtkNew) );
Alan Mishchenko committed
544 545
    assert( Abc_NtkLatchNum(pNtkOld) == Abc_NtkLatchNum(pNtkNew) );
    // create the new timing manager
546
    pNtkNew->pManTime = Abc_ManTimeStart(pNtkNew);
Alan Mishchenko committed
547
    Abc_ManTimeExpand( pNtkNew->pManTime, Abc_NtkObjNumMax(pNtkNew), 0 );
Alan Mishchenko committed
548 549 550
    // set the default timing
    pNtkNew->pManTime->tArrDef = pNtkOld->pManTime->tArrDef;
    pNtkNew->pManTime->tReqDef = pNtkOld->pManTime->tReqDef;
Alan Mishchenko committed
551
    // set the CI timing
Alan Mishchenko committed
552 553
    ppTimesOld = (Abc_Time_t **)pNtkOld->pManTime->vArrs->pArray;
    ppTimesNew = (Abc_Time_t **)pNtkNew->pManTime->vArrs->pArray;
Alan Mishchenko committed
554 555 556
    Abc_NtkForEachCi( pNtkOld, pObj, i )
        *ppTimesNew[ Abc_NtkCi(pNtkNew,i)->Id ] = *ppTimesOld[ pObj->Id ];
    // set the CO timing
Alan Mishchenko committed
557 558
    ppTimesOld = (Abc_Time_t **)pNtkOld->pManTime->vReqs->pArray;
    ppTimesNew = (Abc_Time_t **)pNtkNew->pManTime->vReqs->pArray;
Alan Mishchenko committed
559 560
    Abc_NtkForEachCo( pNtkOld, pObj, i )
        *ppTimesNew[ Abc_NtkCo(pNtkNew,i)->Id ] = *ppTimesOld[ pObj->Id ];
561 562 563 564 565 566 567 568 569 570 571 572 573
    // duplicate input drive
    pNtkNew->pManTime->tInDriveDef = pNtkOld->pManTime->tInDriveDef; 
    pNtkNew->pManTime->tOutLoadDef = pNtkOld->pManTime->tOutLoadDef; 
    if ( pNtkOld->pManTime->tInDrive )
    {
        pNtkNew->pManTime->tInDrive = ABC_ALLOC( Abc_Time_t, Abc_NtkCiNum(pNtkOld) );
        memcpy( pNtkNew->pManTime->tInDrive, pNtkOld->pManTime->tInDrive, sizeof(Abc_Time_t) * Abc_NtkCiNum(pNtkOld) );
    }
    if ( pNtkOld->pManTime->tOutLoad )
    {
        pNtkNew->pManTime->tOutLoad = ABC_ALLOC( Abc_Time_t, Abc_NtkCiNum(pNtkOld) );
        memcpy( pNtkNew->pManTime->tOutLoad, pNtkOld->pManTime->tOutLoad, sizeof(Abc_Time_t) * Abc_NtkCoNum(pNtkOld) );
    }
Alan Mishchenko committed
574 575 576 577
}

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

578
  Synopsis    [Prints out the timing manager.]
579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Abc_NtkTimePrint( Abc_Ntk_t * pNtk )
{
    if ( pNtk->pManTime == NULL )
        printf( "There is no timing manager\n" );
    else
    {
        Abc_Obj_t * pObj; int i;
        printf( "Default arrival = %8f\n", pNtk->pManTime->tArrDef.Fall );
        printf( "Default required = %8f\n", pNtk->pManTime->tReqDef.Fall );
        printf( "Inputs (%d):\n", Abc_NtkCiNum(pNtk) );
        Abc_NtkForEachCi( pNtk, pObj, i )
            printf( "%20s   arrival = %8f   required = %8f\n", 
                Abc_ObjName(pObj), 
                Abc_NodeReadArrivalWorst(pObj), 
                Abc_NodeReadRequiredWorst(pObj) );
        printf( "Outputs (%d):\n", Abc_NtkCoNum(pNtk) );
        Abc_NtkForEachCo( pNtk, pObj, i )
            printf( "%20s   arrival = %8f   required = %8f\n", 
                Abc_ObjName(pObj), 
                Abc_NodeReadArrivalWorst(pObj), 
                Abc_NodeReadRequiredWorst(pObj) );
    }
}

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

Alan Mishchenko committed
613
  Synopsis    [Expends the storage for timing information.]
Alan Mishchenko committed
614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Abc_ManTimeExpand( Abc_ManTime_t * p, int nSize, int fProgressive )
{
    Vec_Ptr_t * vTimes;
    Abc_Time_t * ppTimes, * ppTimesOld, * pTime;
    int nSizeOld, nSizeNew, i;

    nSizeOld = p->vArrs->nSize;
    if ( nSizeOld >= nSize )
        return;
    nSizeNew = fProgressive? 2 * nSize : nSize;
    if ( nSizeNew < 100 )
        nSizeNew = 100;

    vTimes = p->vArrs;
    Vec_PtrGrow( vTimes, nSizeNew );
    vTimes->nSize = nSizeNew;
638
    ppTimesOld = ( nSizeOld == 0 )? NULL : (Abc_Time_t *)vTimes->pArray[0];
Alan Mishchenko committed
639
    ppTimes = ABC_REALLOC( Abc_Time_t, ppTimesOld, nSizeNew );
Alan Mishchenko committed
640 641 642 643
    for ( i = 0; i < nSizeNew; i++ )
        vTimes->pArray[i] = ppTimes + i;
    for ( i = nSizeOld; i < nSizeNew; i++ )
    {
644
        pTime = (Abc_Time_t *)vTimes->pArray[i];
Alan Mishchenko committed
645 646 647 648 649 650 651
        pTime->Rise  = -ABC_INFINITY;
        pTime->Fall  = -ABC_INFINITY;
    }

    vTimes = p->vReqs;
    Vec_PtrGrow( vTimes, nSizeNew );
    vTimes->nSize = nSizeNew;
652
    ppTimesOld = ( nSizeOld == 0 )? NULL : (Abc_Time_t *)vTimes->pArray[0];
Alan Mishchenko committed
653
    ppTimes = ABC_REALLOC( Abc_Time_t, ppTimesOld, nSizeNew );
Alan Mishchenko committed
654 655 656 657
    for ( i = 0; i < nSizeNew; i++ )
        vTimes->pArray[i] = ppTimes + i;
    for ( i = nSizeOld; i < nSizeNew; i++ )
    {
658
        pTime = (Abc_Time_t *)vTimes->pArray[i];
659 660
        pTime->Rise  = ABC_INFINITY;
        pTime->Fall  = ABC_INFINITY;
Alan Mishchenko committed
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
    }
}






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

  Synopsis    [Sets the CI node levels according to the arrival info.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Abc_NtkSetNodeLevelsArrival( Abc_Ntk_t * pNtkOld )
{
    Abc_Obj_t * pNodeOld, * pNodeNew;
    float tAndDelay;
    int i;
    if ( pNtkOld->pManTime == NULL )
        return;
687
    if ( Abc_FrameReadLibGen() == NULL || Mio_LibraryReadNand2((Mio_Library_t *)Abc_FrameReadLibGen()) == NULL )
Alan Mishchenko committed
688
        return;
689
    tAndDelay = Mio_LibraryReadDelayNand2Max((Mio_Library_t *)Abc_FrameReadLibGen());
690
    Abc_NtkForEachCi( pNtkOld, pNodeOld, i )
Alan Mishchenko committed
691 692
    {
        pNodeNew = pNodeOld->pCopy;
693
        pNodeNew->Level = (int)(Abc_NodeReadArrivalWorst(pNodeOld) / tAndDelay);
Alan Mishchenko committed
694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712
    }
}

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

  Synopsis    [Sets the CI node levels according to the arrival info.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Abc_Time_t * Abc_NtkGetCiArrivalTimes( Abc_Ntk_t * pNtk )
{
    Abc_Time_t * p;
    Abc_Obj_t * pNode;
    int i;
713
    p = ABC_CALLOC( Abc_Time_t, Abc_NtkCiNum(pNtk) );
Alan Mishchenko committed
714 715 716
    if ( pNtk->pManTime == NULL )
        return p;
    // set the PI arrival times
717
    Abc_NtkForEachCi( pNtk, pNode, i )
Alan Mishchenko committed
718 719 720
        p[i] = *Abc_NodeArrival(pNode);
    return p;
}
721 722 723 724 725 726 727 728 729
Abc_Time_t * Abc_NtkGetCoRequiredTimes( Abc_Ntk_t * pNtk )
{
    Abc_Time_t * p;
    Abc_Obj_t * pNode;
    int i;
    p = ABC_CALLOC( Abc_Time_t, Abc_NtkCoNum(pNtk) );
    if ( pNtk->pManTime == NULL )
        return p;
    // set the PO required times
730
    Abc_NtkForEachCo( pNtk, pNode, i )
731 732 733 734
        p[i] = *Abc_NodeRequired(pNode);
    return p;
}

Alan Mishchenko committed
735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751

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

  Synopsis    [Sets the CI node levels according to the arrival info.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
float * Abc_NtkGetCiArrivalFloats( Abc_Ntk_t * pNtk )
{
    float * p;
    Abc_Obj_t * pNode;
    int i;
752
    p = ABC_CALLOC( float, Abc_NtkCiNum(pNtk) );
Alan Mishchenko committed
753 754 755
    if ( pNtk->pManTime == NULL )
        return p;
    // set the PI arrival times
756
    Abc_NtkForEachCi( pNtk, pNode, i )
757
        p[i] = Abc_NodeReadArrivalWorst(pNode);
Alan Mishchenko committed
758 759
    return p;
}
760 761 762 763 764 765 766 767 768
float * Abc_NtkGetCoRequiredFloats( Abc_Ntk_t * pNtk )
{
    float * p;
    Abc_Obj_t * pNode;
    int i;
    if ( pNtk->pManTime == NULL )
        return NULL;
    // set the PO required times
    p = ABC_CALLOC( float, Abc_NtkCoNum(pNtk) );
769
    Abc_NtkForEachCo( pNtk, pNode, i )
770
        p[i] = Abc_NodeReadRequiredWorst(pNode);
771 772
    return p;
}
Alan Mishchenko committed
773 774 775 776 777 778 779 780 781 782 783 784 785


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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
786
Vec_Int_t * Abc_NtkDelayTraceSlackStart( Abc_Ntk_t * pNtk )
Alan Mishchenko committed
787
{
788 789 790 791 792 793 794 795 796 797 798
    Vec_Int_t * vSlacks;
    Abc_Obj_t * pObj;
    int i, k;
    vSlacks = Vec_IntAlloc( Abc_NtkObjNumMax(pNtk) + Abc_NtkGetTotalFanins(pNtk) );
    Vec_IntFill( vSlacks, Abc_NtkObjNumMax(pNtk), -1 );
    Abc_NtkForEachNode( pNtk, pObj, i )
    {
        Vec_IntWriteEntry( vSlacks, i, Vec_IntSize(vSlacks) );
        for ( k = 0; k < Abc_ObjFaninNum(pObj); k++ )
            Vec_IntPush( vSlacks, -1 );
    }
799
//    assert( Abc_MaxInt(16, Vec_IntSize(vSlacks)) == Vec_IntCap(vSlacks) );
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
    return vSlacks;
}

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

  Synopsis    [Read/write edge slacks.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline float Abc_NtkDelayTraceSlack( Vec_Int_t * vSlacks, Abc_Obj_t * pObj, int iFanin )
{
    return Abc_Int2Float( Vec_IntEntry( vSlacks, Vec_IntEntry(vSlacks, Abc_ObjId(pObj)) + iFanin ) );
}
static inline void Abc_NtkDelayTraceSetSlack( Vec_Int_t * vSlacks, Abc_Obj_t * pObj, int iFanin, float Num )
{
    Vec_IntWriteEntry( vSlacks, Vec_IntEntry(vSlacks, Abc_ObjId(pObj)) + iFanin, Abc_Float2Int(Num) );
}

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

  Synopsis    [Find most-critical path (the path with smallest slacks).]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Abc_NtkDelayTraceCritPath_rec( Vec_Int_t * vSlacks, Abc_Obj_t * pNode, Abc_Obj_t * pLeaf, Vec_Int_t * vBest )
{
    Abc_Obj_t * pFanin, * pFaninBest = NULL;
837
    float SlackMin = ABC_INFINITY;  int i;
838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861
    // check primary inputs
    if ( Abc_ObjIsCi(pNode) )
        return (pLeaf == NULL || pLeaf == pNode);
    assert( Abc_ObjIsNode(pNode) );
    // check visited
    if ( Abc_NodeIsTravIdCurrent( pNode ) )
        return Vec_IntEntry(vBest, Abc_ObjId(pNode)) >= 0;
    Abc_NodeSetTravIdCurrent( pNode );
    // check the node
    assert( Abc_ObjIsNode(pNode) );
    Abc_ObjForEachFanin( pNode, pFanin, i )
    {
        if ( !Abc_NtkDelayTraceCritPath_rec( vSlacks, pFanin, pLeaf, vBest ) )
            continue;
        if ( pFaninBest == NULL || SlackMin > Abc_NtkDelayTraceSlack(vSlacks, pNode, i) )
        {
            pFaninBest = pFanin;
            SlackMin = Abc_NtkDelayTraceSlack(vSlacks, pNode, i);
        }
    }
    if ( pFaninBest != NULL )
        Vec_IntWriteEntry( vBest, Abc_ObjId(pNode), Abc_NodeFindFanin(pNode, pFaninBest) );
    return (pFaninBest != NULL);
}
Alan Mishchenko committed
862

863
/**Function*************************************************************
Alan Mishchenko committed
864

865
  Synopsis    [Find most-critical path (the path with smallest slacks).]
Alan Mishchenko committed
866

867 868 869 870 871 872 873 874 875 876 877
  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Abc_NtkDelayTraceCritPathCollect_rec( Vec_Int_t * vSlacks, Abc_Obj_t * pNode, Vec_Int_t * vBest, Vec_Ptr_t * vPath )
{
    assert( Abc_ObjIsCi(pNode) || Abc_ObjIsNode(pNode) );
    if ( Abc_ObjIsNode(pNode) )
Alan Mishchenko committed
878
    {
879 880 881
        int iFanin = Vec_IntEntry( vBest, Abc_ObjId(pNode) );
        assert( iFanin >= 0 );
        Abc_NtkDelayTraceCritPathCollect_rec( vSlacks, Abc_ObjFanin(pNode, iFanin), vBest, vPath );
Alan Mishchenko committed
882
    }
883
    Vec_PtrPush( vPath, pNode );
Alan Mishchenko committed
884 885 886 887 888 889 890 891 892 893 894 895 896
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
897
void Abc_NodeDelayTraceArrival( Abc_Obj_t * pNode, Vec_Int_t * vSlacks )
Alan Mishchenko committed
898 899 900 901 902 903 904 905 906 907
{
    Abc_Obj_t * pFanin;
    Abc_Time_t * pTimeIn, * pTimeOut;
    float tDelayBlockRise, tDelayBlockFall;
    Mio_PinPhase_t PinPhase;
    Mio_Pin_t * pPin;
    int i;

    // start the arrival time of the node
    pTimeOut = Abc_NodeArrival(pNode);
Alan Mishchenko committed
908
    pTimeOut->Rise = pTimeOut->Fall = -ABC_INFINITY; 
909 910 911 912 913 914 915
    // consider the buffer
    if ( Abc_ObjIsBarBuf(pNode) )
    {
        pTimeIn = Abc_NodeArrival(Abc_ObjFanin0(pNode));
        *pTimeOut = *pTimeIn;
        return;
    }
Alan Mishchenko committed
916
    // go through the pins of the gate
917
    pPin = Mio_GateReadPins((Mio_Gate_t *)pNode->pData);
Alan Mishchenko committed
918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941
    Abc_ObjForEachFanin( pNode, pFanin, i )
    {
        pTimeIn = Abc_NodeArrival(pFanin);
        // get the interesting parameters of this pin
        PinPhase = Mio_PinReadPhase(pPin);
        tDelayBlockRise = (float)Mio_PinReadDelayBlockRise( pPin );  
        tDelayBlockFall = (float)Mio_PinReadDelayBlockFall( pPin );  
        // compute the arrival times of the positive phase
        if ( PinPhase != MIO_PHASE_INV )  // NONINV phase is present
        {
            if ( pTimeOut->Rise < pTimeIn->Rise + tDelayBlockRise )
                pTimeOut->Rise = pTimeIn->Rise + tDelayBlockRise;
            if ( pTimeOut->Fall < pTimeIn->Fall + tDelayBlockFall )
                pTimeOut->Fall = pTimeIn->Fall + tDelayBlockFall;
        }
        if ( PinPhase != MIO_PHASE_NONINV )  // INV phase is present
        {
            if ( pTimeOut->Rise < pTimeIn->Fall + tDelayBlockRise )
                pTimeOut->Rise = pTimeIn->Fall + tDelayBlockRise;
            if ( pTimeOut->Fall < pTimeIn->Rise + tDelayBlockFall )
                pTimeOut->Fall = pTimeIn->Rise + tDelayBlockFall;
        }
        pPin = Mio_PinReadNext(pPin);
    }
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

    // compute edge slacks
    if ( vSlacks )
    {
        float Slack;
        // go through the pins of the gate
        pPin = Mio_GateReadPins((Mio_Gate_t *)pNode->pData);
        Abc_ObjForEachFanin( pNode, pFanin, i )
        {
            pTimeIn = Abc_NodeArrival(pFanin);
            // get the interesting parameters of this pin
            PinPhase = Mio_PinReadPhase(pPin);
            tDelayBlockRise = (float)Mio_PinReadDelayBlockRise( pPin );  
            tDelayBlockFall = (float)Mio_PinReadDelayBlockFall( pPin );  
            // compute the arrival times of the positive phase
            Slack = ABC_INFINITY;
            if ( PinPhase != MIO_PHASE_INV )  // NONINV phase is present
            {
                Slack = Abc_MinFloat( Slack, Abc_AbsFloat(pTimeIn->Rise + tDelayBlockRise - pTimeOut->Rise) );
                Slack = Abc_MinFloat( Slack, Abc_AbsFloat(pTimeIn->Fall + tDelayBlockFall - pTimeOut->Fall) );
            }
            if ( PinPhase != MIO_PHASE_NONINV )  // INV phase is present
            {
                Slack = Abc_MinFloat( Slack, Abc_AbsFloat(pTimeIn->Fall + tDelayBlockRise - pTimeOut->Rise) );
                Slack = Abc_MinFloat( Slack, Abc_AbsFloat(pTimeIn->Rise + tDelayBlockFall - pTimeOut->Fall) );
            }
            pPin = Mio_PinReadNext(pPin);
            Abc_NtkDelayTraceSetSlack( vSlacks, pNode, i, Slack );
        }
    }
Alan Mishchenko committed
972 973
}

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

  Synopsis    [Performs delay-trace of the network. If input (pIn) or 
  output (pOut) are given, finds the most-timing-critical path between 
  them and prints it to the standard output. If input and/or output are 
  not given, finds the most-critical path in the network and prints it.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
float Abc_NtkDelayTrace( Abc_Ntk_t * pNtk, Abc_Obj_t * pOut, Abc_Obj_t * pIn, int fPrint )
{
    Vec_Int_t * vSlacks = NULL;
    Abc_Obj_t * pNode, * pDriver;
    Vec_Ptr_t * vNodes;
    Abc_Time_t * pTime;
    float tArrivalMax;
    int i;

    assert( Abc_NtkIsMappedLogic(pNtk) );
    assert( pOut == NULL || Abc_ObjIsCo(pOut) );
    assert( pIn == NULL || Abc_ObjIsCi(pIn) );

    // create slacks (need slacks if printing is requested even if pIn/pOut are not given)
    if ( pOut || pIn || fPrint )
        vSlacks = Abc_NtkDelayTraceSlackStart( pNtk );

    // compute the timing
    Abc_NtkTimePrepare( pNtk );
    vNodes = Abc_NtkDfs( pNtk, 1 );
    Vec_PtrForEachEntry( Abc_Obj_t *, vNodes, pNode, i )
        Abc_NodeDelayTraceArrival( pNode, vSlacks );
    Vec_PtrFree( vNodes );

    // get the latest arrival times
    tArrivalMax = -ABC_INFINITY;
    Abc_NtkForEachCo( pNtk, pNode, i )
    {
        pDriver = Abc_ObjFanin0(pNode);
        pTime   = Abc_NodeArrival(pDriver);
1019 1020
        if ( tArrivalMax < Abc_MaxFloat(pTime->Fall, pTime->Rise) )
            tArrivalMax = Abc_MaxFloat(pTime->Fall, pTime->Rise);
1021 1022 1023 1024 1025 1026 1027 1028 1029
    }

    // determine the output to print
    if ( fPrint && pOut == NULL )
    {
        Abc_NtkForEachCo( pNtk, pNode, i )
        {
            pDriver = Abc_ObjFanin0(pNode);
            pTime   = Abc_NodeArrival(pDriver);
1030
            if ( tArrivalMax == Abc_MaxFloat(pTime->Fall, pTime->Rise) )
1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042
                pOut = pNode;
        }
        assert( pOut != NULL );
    }

    if ( fPrint )
    {
        Vec_Ptr_t * vPath = Vec_PtrAlloc( 100 );
        Vec_Int_t * vBest = Vec_IntStartFull( Abc_NtkObjNumMax(pNtk) );
        // traverse to determine the critical path
        Abc_NtkIncrementTravId( pNtk );
        if ( !Abc_NtkDelayTraceCritPath_rec( vSlacks, Abc_ObjFanin0(pOut), pIn, vBest ) )
1043 1044 1045 1046 1047 1048
        {
            if ( pIn == NULL )
                printf( "The logic cone of PO \"%s\" has no primary inputs.\n", Abc_ObjName(pOut) );
            else
                printf( "There is no combinational path between PI \"%s\" and PO \"%s\".\n", Abc_ObjName(pIn), Abc_ObjName(pOut) );
        }
1049 1050
        else
        {
1051
            float Slack = 0.0, SlackAdd;
1052 1053
            int k, iFanin, Length = 0;
            Abc_Obj_t * pFanin;
1054
            // check the additional slack
1055
            SlackAdd = Abc_NodeReadRequiredWorst(pOut) - Abc_NodeReadArrivalWorst(Abc_ObjFanin0(pOut));
1056 1057 1058 1059
            // collect the critical path
            Abc_NtkDelayTraceCritPathCollect_rec( vSlacks, Abc_ObjFanin0(pOut), vBest, vPath );
            if ( pIn == NULL )
                pIn = (Abc_Obj_t *)Vec_PtrEntry( vPath, 0 );
1060 1061 1062 1063
            // find the longest gate name
            Vec_PtrForEachEntry( Abc_Obj_t *, vPath, pNode, i )
                if ( Abc_ObjIsNode(pNode) )
                    Length = Abc_MaxInt( Length, strlen(Mio_GateReadName((Mio_Gate_t *)pNode->pData)) );
1064
            // print critical path
1065
            Abc_NtkLevel( pNtk );
1066 1067
            printf( "Critical path from PI \"%s\" to PO \"%s\":\n", Abc_ObjName(pIn), Abc_ObjName(pOut) ); 
            Vec_PtrForEachEntry( Abc_Obj_t *, vPath, pNode, i )
1068
            {
1069
                printf( "Level %3d : ", Abc_ObjLevel(pNode) );
1070
                if ( Abc_ObjIsCi(pNode) )
1071 1072
                {
                    printf( "Primary input \"%s\".   ", Abc_ObjName(pNode) );
1073
                    printf( "Arrival time =%6.1f. ", Abc_NodeReadArrivalWorst(pNode) );
1074 1075 1076 1077 1078 1079
                    printf( "\n" );
                    continue;
                }
                if ( Abc_ObjIsCo(pNode) )
                {
                    printf( "Primary output \"%s\".   ", Abc_ObjName(pNode) );
1080
                    printf( "Arrival =%6.1f. ", Abc_NodeReadArrivalWorst(pNode) );
1081
                }
1082 1083 1084 1085
                else
                {
                    assert( Abc_ObjIsNode(pNode) );
                    iFanin = Abc_NodeFindFanin( pNode, (Abc_Obj_t *)Vec_PtrEntry(vPath,i-1) );
1086 1087 1088 1089 1090 1091 1092
                    Slack = Abc_NtkDelayTraceSlack(vSlacks, pNode, iFanin);
                    printf( "%10s/", Abc_ObjName(pNode) );
                    printf( "%-4s", Mio_GateReadPinName((Mio_Gate_t *)pNode->pData, iFanin) );
                    printf( " (%s)", Mio_GateReadName((Mio_Gate_t *)pNode->pData) );
                    for ( k = strlen(Mio_GateReadName((Mio_Gate_t *)pNode->pData)); k < Length; k++ )
                        printf( " " );
                    printf( "   " );
1093
                    printf( "Arrival =%6.1f.   ", Abc_NodeReadArrivalWorst(pNode) );
1094
                    printf( "I/O times: (" );
1095
                    Abc_ObjForEachFanin( pNode, pFanin, k )
1096
                        printf( "%s%.1f", (k? ", ":""), Abc_NodeReadArrivalWorst(pFanin) );
1097
//                    printf( " -> %.1f)", Abc_NodeReadArrival(pNode)->Worst + Slack + SlackAdd );
1098
                    printf( " -> %.1f)", Abc_NodeReadArrivalWorst(pNode) );
1099 1100 1101
                }
                printf( "\n" );
            }
1102 1103
            printf( "Level %3d : ", Abc_ObjLevel(Abc_ObjFanin0(pOut)) + 1 );
            printf( "Primary output \"%s\".   ", Abc_ObjName(pOut) );
1104
            printf( "Required time = %6.1f.  ", Abc_NodeReadRequiredWorst(pOut) );
1105
            printf( "Path slack = %6.1f.\n", SlackAdd );
1106 1107 1108 1109 1110 1111 1112 1113 1114
        }
        Vec_PtrFree( vPath );
        Vec_IntFree( vBest );
    }

    Vec_IntFreeP( &vSlacks );
    return tArrivalMax;
}

Alan Mishchenko committed
1115 1116


Alan Mishchenko committed
1117 1118
/**Function*************************************************************

Alan Mishchenko committed
1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132
  Synopsis    [Computes the level of the node using its fanin levels.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Abc_ObjLevelNew( Abc_Obj_t * pObj )
{
    Abc_Obj_t * pFanin;
    int i, Level = 0;
    Abc_ObjForEachFanin( pObj, pFanin, i )
1133
        Level = Abc_MaxFloat( Level, Abc_ObjLevel(pFanin) );
1134
    return Level + (int)(Abc_ObjFaninNum(pObj) > 0);
Alan Mishchenko committed
1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154
}

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

  Synopsis    [Computes the reverse level of the node using its fanout levels.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Abc_ObjReverseLevelNew( Abc_Obj_t * pObj )
{
    Abc_Obj_t * pFanout;
    int i, LevelCur, Level = 0;
    Abc_ObjForEachFanout( pObj, pFanout, i )
    {
        LevelCur = Abc_ObjReverseLevel( pFanout );
1155
        Level = Abc_MaxFloat( Level, LevelCur );
Alan Mishchenko committed
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
    }
    return Level + 1;
}

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

  Synopsis    [Returns required level of the node.]

  Description [Converts the reverse levels of the node into its required 
  level as follows: ReqLevel(Node) = MaxLevels(Ntk) + 1 - LevelR(Node).]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Abc_ObjRequiredLevel( Abc_Obj_t * pObj )
{
    Abc_Ntk_t * pNtk = pObj->pNtk;
    assert( pNtk->vLevelsR );
    return pNtk->LevelMax + 1 - Abc_ObjReverseLevel(pObj);
}

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

  Synopsis    [Returns the reverse level of the node.]

  Description [The reverse level is the level of the node in reverse
  topological order, starting from the COs.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Abc_ObjReverseLevel( Abc_Obj_t * pObj )
{
    Abc_Ntk_t * pNtk = pObj->pNtk;
    assert( pNtk->vLevelsR );
    Vec_IntFillExtra( pNtk->vLevelsR, pObj->Id + 1, 0 );
    return Vec_IntEntry(pNtk->vLevelsR, pObj->Id);
}

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

  Synopsis    [Sets the reverse level of the node.]

  Description [The reverse level is the level of the node in reverse
  topological order, starting from the COs.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Abc_ObjSetReverseLevel( Abc_Obj_t * pObj, int LevelR )
{
    Abc_Ntk_t * pNtk = pObj->pNtk;
    assert( pNtk->vLevelsR );
    Vec_IntFillExtra( pNtk->vLevelsR, pObj->Id + 1, 0 );
    Vec_IntWriteEntry( pNtk->vLevelsR, pObj->Id, LevelR );
}

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

  Synopsis    [Prepares for the computation of required levels.]
Alan Mishchenko committed
1222

Alan Mishchenko committed
1223 1224
  Description [This procedure should be called before the required times
  are used. It starts internal data structures, which records the level 
Alan Mishchenko committed
1225
  from the COs of the network nodes in reverse topologogical order.]
Alan Mishchenko committed
1226 1227 1228 1229 1230 1231
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
1232
void Abc_NtkStartReverseLevels( Abc_Ntk_t * pNtk, int nMaxLevelIncrease )
Alan Mishchenko committed
1233 1234
{
    Vec_Ptr_t * vNodes;
Alan Mishchenko committed
1235 1236
    Abc_Obj_t * pObj;
    int i;
Alan Mishchenko committed
1237
    // remember the maximum number of direct levels
Alan Mishchenko committed
1238
    pNtk->LevelMax = Abc_NtkLevel(pNtk) + nMaxLevelIncrease;
Alan Mishchenko committed
1239 1240
    // start the reverse levels
    pNtk->vLevelsR = Vec_IntAlloc( 0 );
Alan Mishchenko committed
1241
    Vec_IntFill( pNtk->vLevelsR, 1 + Abc_NtkObjNumMax(pNtk), 0 );
Alan Mishchenko committed
1242 1243
    // compute levels in reverse topological order
    vNodes = Abc_NtkDfsReverse( pNtk );
1244
    Vec_PtrForEachEntry( Abc_Obj_t *, vNodes, pObj, i )
Alan Mishchenko committed
1245
        Abc_ObjSetReverseLevel( pObj, Abc_ObjReverseLevelNew(pObj) );
Alan Mishchenko committed
1246
    Vec_PtrFree( vNodes );
Alan Mishchenko committed
1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270
}

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

  Synopsis    [Cleans the data structures used to compute required levels.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Abc_NtkStopReverseLevels( Abc_Ntk_t * pNtk )
{
    assert( pNtk->vLevelsR );
    Vec_IntFree( pNtk->vLevelsR );
    pNtk->vLevelsR = NULL;
    pNtk->LevelMax = 0;

}

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

Alan Mishchenko committed
1271
  Synopsis    [Incrementally updates level of the nodes.]
Alan Mishchenko committed
1272

Alan Mishchenko committed
1273
  Description []
Alan Mishchenko committed
1274 1275 1276 1277 1278 1279
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
1280
void Abc_NtkUpdateLevel( Abc_Obj_t * pObjNew, Vec_Vec_t * vLevels )
Alan Mishchenko committed
1281
{
Alan Mishchenko committed
1282 1283
    Abc_Obj_t * pFanout, * pTemp;
    int LevelOld, Lev, k, m;
Alan Mishchenko committed
1284
//    int Counter = 0, CounterMax = 0;
Alan Mishchenko committed
1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295
    // check if level has changed
    LevelOld = Abc_ObjLevel(pObjNew);
    if ( LevelOld == Abc_ObjLevelNew(pObjNew) )
        return;
    // start the data structure for level update
    // we cannot fail to visit a node when using this structure because the 
    // nodes are stored by their _old_ levels, which are assumed to be correct
    Vec_VecClear( vLevels );
    Vec_VecPush( vLevels, LevelOld, pObjNew );
    pObjNew->fMarkA = 1;
    // recursively update level
1296
    Vec_VecForEachEntryStart( Abc_Obj_t *, vLevels, pTemp, Lev, k, LevelOld )
Alan Mishchenko committed
1297
    {
Alan Mishchenko committed
1298
//        Counter--;
Alan Mishchenko committed
1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311
        pTemp->fMarkA = 0;
        assert( Abc_ObjLevel(pTemp) == Lev );
        Abc_ObjSetLevel( pTemp, Abc_ObjLevelNew(pTemp) );
        // if the level did not change, no need to check the fanout levels
        if ( Abc_ObjLevel(pTemp) == Lev )
            continue;
        // schedule fanout for level update
        Abc_ObjForEachFanout( pTemp, pFanout, m )
        {
            if ( !Abc_ObjIsCo(pFanout) && !pFanout->fMarkA )
            {
                assert( Abc_ObjLevel(pFanout) >= Lev );
                Vec_VecPush( vLevels, Abc_ObjLevel(pFanout), pFanout );
Alan Mishchenko committed
1312
//                Counter++;
1313
//                CounterMax = Abc_MaxFloat( CounterMax, Counter );
Alan Mishchenko committed
1314 1315 1316 1317
                pFanout->fMarkA = 1;
            }
        }
    }
Alan Mishchenko committed
1318
//    printf( "%d ", CounterMax );
Alan Mishchenko committed
1319 1320 1321 1322
}

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

Alan Mishchenko committed
1323
  Synopsis    [Incrementally updates level of the nodes.]
Alan Mishchenko committed
1324

Alan Mishchenko committed
1325
  Description []
Alan Mishchenko committed
1326 1327 1328 1329 1330 1331
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
1332
void Abc_NtkUpdateReverseLevel( Abc_Obj_t * pObjNew, Vec_Vec_t * vLevels )
Alan Mishchenko committed
1333
{
Alan Mishchenko committed
1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346
    Abc_Obj_t * pFanin, * pTemp;
    int LevelOld, LevFanin, Lev, k, m;
    // check if level has changed
    LevelOld = Abc_ObjReverseLevel(pObjNew);
    if ( LevelOld == Abc_ObjReverseLevelNew(pObjNew) )
        return;
    // start the data structure for level update
    // we cannot fail to visit a node when using this structure because the 
    // nodes are stored by their _old_ levels, which are assumed to be correct
    Vec_VecClear( vLevels );
    Vec_VecPush( vLevels, LevelOld, pObjNew );
    pObjNew->fMarkA = 1;
    // recursively update level
1347
    Vec_VecForEachEntryStart( Abc_Obj_t *, vLevels, pTemp, Lev, k, LevelOld )
Alan Mishchenko committed
1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367
    {
        pTemp->fMarkA = 0;
        LevelOld = Abc_ObjReverseLevel(pTemp); 
        assert( LevelOld == Lev );
        Abc_ObjSetReverseLevel( pTemp, Abc_ObjReverseLevelNew(pTemp) );
        // if the level did not change, no need to check the fanout levels
        if ( Abc_ObjReverseLevel(pTemp) == Lev )
            continue;
        // schedule fanins for level update
        Abc_ObjForEachFanin( pTemp, pFanin, m )
        {
            if ( !Abc_ObjIsCi(pFanin) && !pFanin->fMarkA )
            {
                LevFanin = Abc_ObjReverseLevel( pFanin );
                assert( LevFanin >= Lev );
                Vec_VecPush( vLevels, LevFanin, pFanin );
                pFanin->fMarkA = 1;
            }
        }
    }
Alan Mishchenko committed
1368 1369 1370 1371
}

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

Alan Mishchenko committed
1372
  Synopsis    [Replaces the node and incrementally updates levels.]
Alan Mishchenko committed
1373

Alan Mishchenko committed
1374
  Description []
Alan Mishchenko committed
1375 1376 1377 1378 1379 1380
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
1381
void Abc_NtkUpdate( Abc_Obj_t * pObj, Abc_Obj_t * pObjNew, Vec_Vec_t * vLevels )
Alan Mishchenko committed
1382
{
Alan Mishchenko committed
1383 1384 1385 1386 1387 1388 1389
    // replace the old node by the new node
    pObjNew->Level = pObj->Level;
    Abc_ObjReplace( pObj, pObjNew );
    // update the level of the node
    Abc_NtkUpdateLevel( pObjNew, vLevels );
    Abc_ObjSetReverseLevel( pObjNew, 0 );
    Abc_NtkUpdateReverseLevel( pObjNew, vLevels );
Alan Mishchenko committed
1390 1391
}

Alan Mishchenko committed
1392 1393 1394 1395 1396
////////////////////////////////////////////////////////////////////////
///                       END OF FILE                                ///
////////////////////////////////////////////////////////////////////////


1397 1398
ABC_NAMESPACE_IMPL_END