summaryrefslogtreecommitdiffstats
path: root/src/aig/nwk/nwkTiming.c
blob: 0cbcb7f82faec7b8196110e4b340d82594c26510 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
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
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
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
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
/**CFile****************************************************************

  FileName    [nwkTiming.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [Logic network representation.]

  Synopsis    [Manipulation of timing information.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

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

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

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

#include "nwk.h"

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

static inline int   Nwk_ManTimeEqual( float f1, float f2, float Eps )  { return (f1 < f2 + Eps) && (f2 < f1 + Eps);  }
static inline int   Nwk_ManTimeLess( float f1, float f2, float Eps )   { return (f1 < f2 + Eps);                     }
static inline int   Nwk_ManTimeMore( float f1, float f2, float Eps )   { return (f1 + Eps > f2);                     }

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

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

  Synopsis    [Cleans timing information for all nodes.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Nwk_ManCleanTiming( Nwk_Man_t * pNtk )
{
    Nwk_Obj_t * pObj;
    int i;
    Nwk_ManForEachObj( pNtk, pObj, i )
    {
        pObj->tArrival = pObj->tSlack = 0.0;
        pObj->tRequired = AIG_INFINITY;
    }
}

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

  Synopsis    [Sorts the pins in the decreasing order of delays.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Nwk_ManDelayTraceSortPins( Nwk_Obj_t * pNode, int * pPinPerm, float * pPinDelays )
{
    Nwk_Obj_t * pFanin;
    int i, j, best_i, temp;
    // start the trivial permutation and collect pin delays
    Nwk_ObjForEachFanin( pNode, pFanin, i )
    {
        pPinPerm[i] = i;
        pPinDelays[i] = Nwk_ObjArrival(pFanin);
    }
    // selection sort the pins in the decreasible order of delays
    // this order will match the increasing order of LUT input pins
    for ( i = 0; i < Nwk_ObjFaninNum(pNode)-1; i++ )
    {
        best_i = i;
        for ( j = i+1; j < Nwk_ObjFaninNum(pNode); j++ )
            if ( pPinDelays[pPinPerm[j]] > pPinDelays[pPinPerm[best_i]] )
                best_i = j;
        if ( best_i == i )
            continue;
        temp = pPinPerm[i]; 
        pPinPerm[i] = pPinPerm[best_i]; 
        pPinPerm[best_i] = temp;
    }
    // verify
    assert( Nwk_ObjFaninNum(pNode) == 0 || pPinPerm[0] < Nwk_ObjFaninNum(pNode) );
    for ( i = 1; i < Nwk_ObjFaninNum(pNode); i++ )
    {
        assert( pPinPerm[i] < Nwk_ObjFaninNum(pNode) );
        assert( pPinDelays[pPinPerm[i-1]] >= pPinDelays[pPinPerm[i]] );
    }
}

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

  Synopsis    [Computes the arrival times for the given node.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
float Nwk_NodeComputeArrival( Nwk_Obj_t * pObj, If_Lib_t * pLutLib, int fUseSorting )
{
    int pPinPerm[32];
    float pPinDelays[32];
    Nwk_Obj_t * pFanin;
    float tArrival, * pDelays;
    int k;
    assert( Nwk_ObjIsNode(pObj) );
    tArrival = -AIG_INFINITY;
    if ( pLutLib == NULL )
    {
        Nwk_ObjForEachFanin( pObj, pFanin, k )
            if ( tArrival < Nwk_ObjArrival(pFanin) + 1.0 )
                tArrival = Nwk_ObjArrival(pFanin) + 1.0;
    }
    else if ( !pLutLib->fVarPinDelays )
    {
        pDelays = pLutLib->pLutDelays[Nwk_ObjFaninNum(pObj)];
        Nwk_ObjForEachFanin( pObj, pFanin, k )
            if ( tArrival < Nwk_ObjArrival(pFanin) + pDelays[0] )
                tArrival = Nwk_ObjArrival(pFanin) + pDelays[0];
    }
    else
    {
        pDelays = pLutLib->pLutDelays[Nwk_ObjFaninNum(pObj)];
        if ( fUseSorting )
        {
            Nwk_ManDelayTraceSortPins( pObj, pPinPerm, pPinDelays );
            Nwk_ObjForEachFanin( pObj, pFanin, k ) 
                if ( tArrival < Nwk_ObjArrival(Nwk_ObjFanin(pObj,pPinPerm[k])) + pDelays[k] )
                    tArrival = Nwk_ObjArrival(Nwk_ObjFanin(pObj,pPinPerm[k])) + pDelays[k];
        }
        else
        {
            Nwk_ObjForEachFanin( pObj, pFanin, k )
                if ( tArrival < Nwk_ObjArrival(pFanin) + pDelays[k] )
                    tArrival = Nwk_ObjArrival(pFanin) + pDelays[k];
        }
    }
    if ( Nwk_ObjFaninNum(pObj) == 0 )
        tArrival = 0.0;
    return tArrival;
}

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

  Synopsis    [Computes the required times for the given node.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
float Nwk_NodeComputeRequired( Nwk_Obj_t * pObj, If_Lib_t * pLutLib, int fUseSorting )
{
    int pPinPerm[32];
    float pPinDelays[32];
    Nwk_Obj_t * pFanout;
    float tRequired, * pDelays;
    int k;
    assert( Nwk_ObjIsNode(pObj) || Nwk_ObjIsCi(pObj) );
    tRequired = AIG_INFINITY;
    if ( pLutLib == NULL )
    {
        Nwk_ObjForEachFanout( pObj, pFanout, k )
            if ( tRequired > Nwk_ObjRequired(pFanout) - 1.0 )
                tRequired = Nwk_ObjRequired(pFanout) - 1.0;
    }
    else if ( !pLutLib->fVarPinDelays )
    {
        Nwk_ObjForEachFanout( pObj, pFanout, k )
        {
            pDelays = pLutLib->pLutDelays[Nwk_ObjFaninNum(pFanout)];
            if ( tRequired > Nwk_ObjRequired(pFanout) - pDelays[0] )
                tRequired = Nwk_ObjRequired(pFanout) - pDelays[0];
        }
    }
    else
    {
        if ( fUseSorting )
        {
            Nwk_ObjForEachFanout( pObj, pFanout, k ) 
            {
                pDelays = pLutLib->pLutDelays[Nwk_ObjFaninNum(pFanout)];
                Nwk_ManDelayTraceSortPins( pFanout, pPinPerm, pPinDelays );
                if ( tRequired > Nwk_ObjRequired(Nwk_ObjFanout(pObj,pPinPerm[k])) - pDelays[k] )
                    tRequired = Nwk_ObjRequired(Nwk_ObjFanout(pObj,pPinPerm[k])) - pDelays[k];
            }
        }
        else
        {
            Nwk_ObjForEachFanout( pObj, pFanout, k )
            {
                pDelays = pLutLib->pLutDelays[Nwk_ObjFaninNum(pFanout)];
                if ( tRequired > Nwk_ObjRequired(pFanout) - pDelays[k] )
                    tRequired = Nwk_ObjRequired(pFanout) - pDelays[k];
            }
        }
    }
    return tRequired;
}

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

  Synopsis    [Propagates the required times through the given node.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
float Nwk_NodePropagateRequired( Nwk_Obj_t * pObj, If_Lib_t * pLutLib, int fUseSorting )
{
    int pPinPerm[32];
    float pPinDelays[32];
    Nwk_Obj_t * pFanin;
    float tRequired, * pDelays;
    int k;
    assert( Nwk_ObjIsNode(pObj) );
    if ( pLutLib == NULL )
    {
        tRequired = Nwk_ObjRequired(pObj) - (float)1.0;
        Nwk_ObjForEachFanin( pObj, pFanin, k )
            if ( Nwk_ObjRequired(pFanin) > tRequired )
                Nwk_ObjSetRequired( pFanin, tRequired );
    }
    else if ( !pLutLib->fVarPinDelays )
    {
        pDelays = pLutLib->pLutDelays[Nwk_ObjFaninNum(pObj)];
        tRequired = Nwk_ObjRequired(pObj) - pDelays[0];
        Nwk_ObjForEachFanin( pObj, pFanin, k )
            if ( Nwk_ObjRequired(pFanin) > tRequired )
                Nwk_ObjSetRequired( pFanin, tRequired );
    }
    else 
    {
        pDelays = pLutLib->pLutDelays[Nwk_ObjFaninNum(pObj)];
        if ( fUseSorting )
        {
            Nwk_ManDelayTraceSortPins( pObj, pPinPerm, pPinDelays );
            Nwk_ObjForEachFanin( pObj, pFanin, k )
            {
                tRequired = Nwk_ObjRequired(pObj) - pDelays[k];
                if ( Nwk_ObjRequired(Nwk_ObjFanin(pObj,pPinPerm[k])) > tRequired )
                    Nwk_ObjSetRequired( Nwk_ObjFanin(pObj,pPinPerm[k]), tRequired );
            }
        }
        else
        {
            Nwk_ObjForEachFanin( pObj, pFanin, k )
            {
                tRequired = Nwk_ObjRequired(pObj) - pDelays[k];
                if ( Nwk_ObjRequired(pFanin) > tRequired )
                    Nwk_ObjSetRequired( pFanin, tRequired );
            }
        }
    }
    return tRequired;
}

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

  Synopsis    [Computes the delay trace of the given network.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
float Nwk_ManDelayTraceLut( Nwk_Man_t * pNtk, If_Lib_t * pLutLib )
{
    int fUseSorting = 1;
    Vec_Ptr_t * vNodes;
    Nwk_Obj_t * pObj;
    float tArrival, tRequired, tSlack;
    int i;

    // get the library
    if ( pLutLib && pLutLib->LutMax < Nwk_ManGetFaninMax(pNtk) )
    {
        printf( "The max LUT size (%d) is less than the max fanin count (%d).\n", 
            pLutLib->LutMax, Nwk_ManGetFaninMax(pNtk) );
        return -AIG_INFINITY;
    }

    // compute the reverse order of all objects
    vNodes = Nwk_ManDfsReverse( pNtk );

    // initialize the arrival times
    Nwk_ManCleanTiming( pNtk );

    // propagate arrival times
    if ( pNtk->pManTime )
        Tim_ManIncrementTravId( pNtk->pManTime );
    Nwk_ManForEachObj( pNtk, pObj, i )
    {
        if ( Nwk_ObjIsNode(pObj) )
        {
            tArrival = Nwk_NodeComputeArrival( pObj, pLutLib, fUseSorting );
        }
        else if ( Nwk_ObjIsCi(pObj) )
        {
            tArrival = pNtk->pManTime? Tim_ManGetPiArrival( pNtk->pManTime, pObj->PioId ) : (float)0.0;
        }
        else if ( Nwk_ObjIsCo(pObj) )
        {
            tArrival = Nwk_ObjArrival( Nwk_ObjFanin0(pObj) );
            if ( pNtk->pManTime )
                Tim_ManSetPoArrival( pNtk->pManTime, pObj->PioId, tArrival );
        }
        else
            assert( 0 );
        Nwk_ObjSetArrival( pObj, tArrival );
    }

    // get the latest arrival times
    tArrival = -AIG_INFINITY;
    Nwk_ManForEachPo( pNtk, pObj, i )
        if ( tArrival < Nwk_ObjArrival(pObj) )
            tArrival = Nwk_ObjArrival(pObj);

    // initialize the required times
    if ( pNtk->pManTime )
    {
        Tim_ManIncrementTravId( pNtk->pManTime );
        Tim_ManSetPoRequiredAll( pNtk->pManTime, tArrival );
    }
    else
        Nwk_ManForEachPo( pNtk, pObj, i )
            Nwk_ObjSetRequired( pObj, tArrival );

    // propagate the required times
    Vec_PtrForEachEntry( vNodes, pObj, i )
    {
        if ( Nwk_ObjIsNode(pObj) )
        {
            Nwk_NodePropagateRequired( pObj, pLutLib, fUseSorting );
        }
        else if ( Nwk_ObjIsCi(pObj) )
        {
            if ( pNtk->pManTime )
                Tim_ManSetPiRequired( pNtk->pManTime, pObj->PioId, Nwk_ObjRequired(pObj) );
        }
        else if ( Nwk_ObjIsCo(pObj) )
        {
            if ( pNtk->pManTime )
                tRequired = Tim_ManGetPoRequired( pNtk->pManTime, pObj->PioId );
            else
                tRequired = Nwk_ObjRequired(pObj);
            if ( Nwk_ObjRequired(Nwk_ObjFanin0(pObj)) > tRequired )
                Nwk_ObjSetRequired( Nwk_ObjFanin0(pObj), tRequired );
        }

        // set slack for this object
        tSlack = Nwk_ObjRequired(pObj) - Nwk_ObjArrival(pObj);
        assert( tSlack + 0.001 > 0.0 );
        Nwk_ObjSetSlack( pObj, tSlack < 0.0 ? 0.0 : tSlack );
    }
    Vec_PtrFree( vNodes );
    return tArrival;
}

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

  Synopsis    [Prints the delay trace for the given network.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Nwk_ManDelayTracePrint( Nwk_Man_t * pNtk, If_Lib_t * pLutLib )
{
    Nwk_Obj_t * pNode;
    int i, Nodes, * pCounters;
    float tArrival, tDelta, nSteps, Num;
    // get the library
    if ( pLutLib && pLutLib->LutMax < Nwk_ManGetFaninMax(pNtk) )
    {
        printf( "The max LUT size (%d) is less than the max fanin count (%d).\n", 
            pLutLib->LutMax, Nwk_ManGetFaninMax(pNtk) );
        return;
    }
    // decide how many steps
    nSteps = pLutLib ? 20 : Nwk_ManLevel(pNtk);
    pCounters = ALLOC( int, nSteps + 1 );
    memset( pCounters, 0, sizeof(int)*(nSteps + 1) );
    // perform delay trace
    tArrival = Nwk_ManDelayTraceLut( pNtk, pLutLib );
    tDelta = tArrival / nSteps;
    // count how many nodes have slack in the corresponding intervals
    Nwk_ManForEachNode( pNtk, pNode, i )
    {
        if ( Nwk_ObjFaninNum(pNode) == 0 )
            continue;
        Num = Nwk_ObjSlack(pNode) / tDelta;
        if ( Num > nSteps )
            continue;
        assert( Num >=0 && Num <= nSteps );
        pCounters[(int)Num]++;
    }
    // print the results
    printf( "Max delay = %6.2f. Delay trace using %s model:\n", tArrival, pLutLib? "LUT library" : "unit-delay" );
    Nodes = 0;
    for ( i = 0; i < nSteps; i++ )
    {
        Nodes += pCounters[i];
        printf( "%3d %s : %5d  (%6.2f %%)\n", pLutLib? 5*(i+1) : i+1, 
            pLutLib? "%":"lev", Nodes, 100.0*Nodes/Nwk_ManNodeNum(pNtk) );
    }
    free( pCounters );
}


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

  Synopsis    [Inserts node into the queue of nodes sorted by level.]

  Description [The inserted node should not go before the current position 
  given by iCurrent. If the arrival times are computed, the nodes are sorted
  in the increasing order of levels. If the required times are computed, 
  the nodes are sorted in the decreasing order of levels.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Nwk_NodeUpdateAddToQueue( Vec_Ptr_t * vQueue, Nwk_Obj_t * pObj, int iCurrent, int fArrival )
{
    Nwk_Obj_t * pTemp1, * pTemp2;
    int i;
    Vec_PtrPush( vQueue, pObj );
    for ( i = Vec_PtrSize(vQueue) - 1; i > iCurrent + 1; i-- )
    {
        pTemp1 = vQueue->pArray[i];
        pTemp2 = vQueue->pArray[i-1];
        if ( fArrival )
        {
            if ( Nwk_ObjLevel(pTemp2) <= Nwk_ObjLevel(pTemp1) )
                break;
        }
        else
        {
            if ( Nwk_ObjLevel(pTemp2) >= Nwk_ObjLevel(pTemp1) )
                break;
        }
//        assert( i-1 > iCurrent );
        vQueue->pArray[i-1] = pTemp1;
        vQueue->pArray[i]   = pTemp2;
    }
    // verification
    for ( i = iCurrent + 1; i < Vec_PtrSize(vQueue) - 1; i++ )
    {
        pTemp1 = vQueue->pArray[i];
        pTemp2 = vQueue->pArray[i+1];
        if ( fArrival )
            assert( Nwk_ObjLevel(pTemp1) <= Nwk_ObjLevel(pTemp2) );
        else
            assert( Nwk_ObjLevel(pTemp1) >= Nwk_ObjLevel(pTemp2) );
    }
}

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

  Synopsis    [Incrementally updates arrival times of the node.]

  Description [Supports variable-pin delay model and white-boxes.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Nwk_NodeUpdateArrival( Nwk_Obj_t * pObj, If_Lib_t * pLutLib )
{
    Tim_Man_t * pManTime = pObj->pMan->pManTime;
    Vec_Ptr_t * vQueue = pObj->pMan->vTemp;
    Nwk_Obj_t * pTemp, * pNext;
    float tArrival;
    int i, k;
    assert( Nwk_ObjIsNode(pObj) );
    // initialize the queue with the node
    Vec_PtrClear( vQueue );
    Vec_PtrPush( vQueue, pObj );
    pObj->MarkA = 1;
    // process objects
    Tim_ManTravIdDisable( pManTime );
    Vec_PtrForEachEntry( vQueue, pTemp, i )
    {
        pTemp->MarkA = 0;
        tArrival = Nwk_NodeComputeArrival( pTemp, pLutLib, 1 );
        if ( Nwk_ManTimeEqual( tArrival, Nwk_ObjArrival(pTemp), (float)0.001 ) )
            continue;
        Nwk_ObjSetArrival( pTemp, tArrival );
        // add the fanouts to the queue
        Nwk_ObjForEachFanout( pTemp, pNext, k )
        {
            if ( Nwk_ObjIsCo(pNext) )
            {
                Nwk_ObjSetArrival( pNext, tArrival );
                continue;
            }
            if ( pNext->MarkA )
                continue;
            Nwk_NodeUpdateAddToQueue( vQueue, pNext, i, 1 );
            pNext->MarkA = 1;
        }
    }
}

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

  Synopsis    [Incrementally updates required times of the node.]

  Description [Supports variable-pin delay model and white-boxes.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Nwk_NodeUpdateRequired( Nwk_Obj_t * pObj, If_Lib_t * pLutLib )
{
    Tim_Man_t * pManTime = pObj->pMan->pManTime;
    Vec_Ptr_t * vQueue = pObj->pMan->vTemp;
    Nwk_Obj_t * pTemp, * pNext;
    float tRequired;
    int i, k; 
    assert( Nwk_ObjIsNode(pObj) );
    // make sure the node's required time remained the same
    tRequired = Nwk_NodeComputeRequired( pObj, pLutLib, 1 );
    assert( Nwk_ManTimeEqual( tRequired, Nwk_ObjRequired(pObj), (float)0.001 ) );
    // initialize the queue with the node's fanins
    Vec_PtrClear( vQueue );
    Nwk_ObjForEachFanin( pObj, pNext, k )
    {
        if ( pNext->MarkA )
            continue;
        Nwk_NodeUpdateAddToQueue( vQueue, pNext, -1, 0 );
        pNext->MarkA = 1;
    }
    // process objects
    Tim_ManTravIdDisable( pManTime );
    Vec_PtrForEachEntry( vQueue, pTemp, i )
    {
        pTemp->MarkA = 0;
        tRequired = Nwk_NodeComputeRequired( pTemp, pLutLib, 1 );
        if ( Nwk_ManTimeEqual( tRequired, Nwk_ObjRequired(pTemp), (float)0.001 ) )
            continue;
        Nwk_ObjSetRequired( pTemp, tRequired );
        // schedule fanins of the node
        Nwk_ObjForEachFanin( pTemp, pNext, k )
        {
            if ( pNext->MarkA )
                continue;
            Nwk_NodeUpdateAddToQueue( vQueue, pNext, i, 0 );
            pNext->MarkA = 1;
        }
    }
}

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

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

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Nwk_ObjLevelNew( Nwk_Obj_t * pObj )
{
    Nwk_Obj_t * pFanin;
    int i, Level = 0;
    if ( Nwk_ObjIsCi(pObj) || Nwk_ObjIsLatch(pObj) )
        return 0;
    assert( Nwk_ObjIsNode(pObj) || Nwk_ObjIsCo(pObj) );
    Nwk_ObjForEachFanin( pObj, pFanin, i )
        Level = AIG_MAX( Level, Nwk_ObjLevel(pFanin) );
    return Level + (Nwk_ObjIsNode(pObj) && Nwk_ObjFaninNum(pObj) > 0);
}

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

  Synopsis    [Incrementally updates level of the nodes.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Nwk_ManUpdateLevel( Nwk_Obj_t * pObj )
{
    Vec_Ptr_t * vQueue = pObj->pMan->vTemp;
    Nwk_Obj_t * pTemp, * pNext;
    int LevelNew, i, k;
    assert( Nwk_ObjIsNode(pObj) );
    // initialize the queue with the node
    Vec_PtrClear( vQueue );
    Vec_PtrPush( vQueue, pObj );
    pObj->MarkA = 1;
    // process objects
    Vec_PtrForEachEntry( vQueue, pTemp, i )
    {
        pTemp->MarkA = 0;
        LevelNew = Nwk_ObjLevelNew( pTemp );
        if ( LevelNew == Nwk_ObjLevel(pTemp) )
            continue;
        Nwk_ObjSetLevel( pTemp, LevelNew );
        // add the fanouts to the queue
        Nwk_ObjForEachFanout( pTemp, pNext, k )
        {
            if ( Nwk_ObjIsCo(pNext) )
            {
                Nwk_ObjSetLevel( pNext, LevelNew );
                continue;
            }
            if ( pNext->MarkA )
                continue;
            Nwk_NodeUpdateAddToQueue( vQueue, pNext, i, 1 );
            pNext->MarkA = 1;
        }
    }
}

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

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

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Nwk_ManVerifyLevel( Nwk_Man_t * pNtk )
{
    Nwk_Obj_t * pObj;
    int LevelNew, i;
    Nwk_ManForEachObj( pNtk, pObj, i )
    {
        assert( pObj->MarkA == 0 );
        LevelNew = Nwk_ObjLevelNew( pObj );
        if ( Nwk_ObjLevel(pObj) != LevelNew )
        {
            printf( "Object %6d: Mismatch betweeh levels: Actual = %d. Correct = %d.\n", 
                i, Nwk_ObjLevel(pObj), LevelNew );
        }
    }
}

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

  Synopsis    [Replaces the node and incrementally updates levels.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Nwk_ManUpdate( Nwk_Obj_t * pObj, Nwk_Obj_t * pObjNew, Vec_Vec_t * vLevels )
{
    // transfer the timing information
    // (this is needed because updating level happens if the level has changed;
    // when we set the old level, it will be recomputed by the level updating
    // procedure, which will update level of other nodes if there is a difference)
    pObjNew->Level = pObj->Level;
    pObjNew->tArrival = pObj->tArrival;
    pObjNew->tRequired = pObj->tRequired;
    // replace the old node by the new node
    Nwk_ObjReplace( pObj, pObjNew );
    // update the level of the node
    Nwk_ManUpdateLevel( pObjNew );
//Nwk_ManVerifyLevel( pObjNew->pMan );
//    Nwk_NodeUpdateArrival( pObjNew, pObj->pMan->pLutLib );
//    Nwk_NodeUpdateRequired( pObjNew, pObj->pMan->pLutLib );
}


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