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
|
/**CFile****************************************************************
FileName [nwkDfs.c]
SystemName [ABC: Logic synthesis and verification system.]
PackageName [Logic network representation.]
Synopsis [DFS traversals.]
Author [Alan Mishchenko]
Affiliation [UC Berkeley]
Date [Ver. 1.0. Started - June 20, 2005.]
Revision [$Id: nwkDfs.c,v 1.00 2005/06/20 00:00:00 alanmi Exp $]
***********************************************************************/
#include "nwk.h"
////////////////////////////////////////////////////////////////////////
/// DECLARATIONS ///
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
/// FUNCTION DEFINITIONS ///
////////////////////////////////////////////////////////////////////////
/**Function*************************************************************
Synopsis [Verifies that the objects are in a topo order.]
Description []
SideEffects []
SeeAlso []
***********************************************************************/
int Nwk_ManVerifyTopoOrder( Nwk_Man_t * pNtk )
{
Nwk_Obj_t * pObj, * pNext;
int i, k, iBox, iTerm1, nTerms;
Nwk_ManIncrementTravId( pNtk );
Nwk_ManForEachObj( pNtk, pObj, i )
{
if ( Nwk_ObjIsNode(pObj) || Nwk_ObjIsCo(pObj) )
{
Nwk_ObjForEachFanin( pObj, pNext, k )
{
if ( !Nwk_ObjIsTravIdCurrent(pNext) )
{
printf( "Node %d has fanin %d that is not in a topological order.\n", pObj->Id, pNext->Id );
return 0;
}
}
}
else if ( Nwk_ObjIsCi(pObj) )
{
if ( pNtk->pManTime )
{
iBox = Tim_ManBoxForCi( pNtk->pManTime, pObj->PioId );
if ( iBox >= 0 ) // this is not a true PI
{
iTerm1 = Tim_ManBoxInputFirst( pNtk->pManTime, iBox );
nTerms = Tim_ManBoxInputNum( pNtk->pManTime, iBox );
for ( k = 0; k < nTerms; k++ )
{
pNext = Nwk_ManCo( pNtk, iTerm1 + k );
if ( !Nwk_ObjIsTravIdCurrent(pNext) )
{
printf( "Box %d has input %d that is not in a topological order.\n", iBox, pNext->Id );
return 0;
}
}
}
}
}
else
assert( 0 );
Nwk_ObjSetTravIdCurrent( pObj );
}
return 1;
}
/**Function*************************************************************
Synopsis [Computes the number of logic levels not counting PIs/POs.]
Description [Assumes that white boxes have unit level.]
SideEffects []
SeeAlso []
***********************************************************************/
int Nwk_ManLevelBackup( Nwk_Man_t * pNtk )
{
Tim_Man_t * pManTimeUnit;
Nwk_Obj_t * pObj, * pFanin;
int i, k, LevelMax, Level;
assert( Nwk_ManVerifyTopoOrder(pNtk) );
// clean the levels
Nwk_ManForEachObj( pNtk, pObj, i )
Nwk_ObjSetLevel( pObj, 0 );
// perform level computation
LevelMax = 0;
pManTimeUnit = pNtk->pManTime ? Tim_ManDupUnit( pNtk->pManTime ) : NULL;
if ( pManTimeUnit )
Tim_ManIncrementTravId( pManTimeUnit );
Nwk_ManForEachObj( pNtk, pObj, i )
{
if ( Nwk_ObjIsCi(pObj) )
{
Level = pManTimeUnit? (int)Tim_ManGetCiArrival( pManTimeUnit, pObj->PioId ) : 0;
Nwk_ObjSetLevel( pObj, Level );
}
else if ( Nwk_ObjIsCo(pObj) )
{
Level = Nwk_ObjLevel( Nwk_ObjFanin0(pObj) );
if ( pManTimeUnit )
Tim_ManSetCoArrival( pManTimeUnit, pObj->PioId, (float)Level );
Nwk_ObjSetLevel( pObj, Level );
if ( LevelMax < Nwk_ObjLevel(pObj) )
LevelMax = Nwk_ObjLevel(pObj);
}
else if ( Nwk_ObjIsNode(pObj) )
{
Level = 0;
Nwk_ObjForEachFanin( pObj, pFanin, k )
if ( Level < Nwk_ObjLevel(pFanin) )
Level = Nwk_ObjLevel(pFanin);
Nwk_ObjSetLevel( pObj, Level + 1 );
}
else
assert( 0 );
}
// set the old timing manager
if ( pManTimeUnit )
Tim_ManStop( pManTimeUnit );
return LevelMax;
}
/**Function*************************************************************
Synopsis [Computes the number of logic levels not counting PIs/POs.]
Description []
SideEffects []
SeeAlso []
***********************************************************************/
void Nwk_ManLevel_rec( Nwk_Obj_t * pObj )
{
Tim_Man_t * pManTime = pObj->pMan->pManTime;
Nwk_Obj_t * pNext;
int i, iBox, iTerm1, nTerms, LevelMax = 0;
if ( Nwk_ObjIsTravIdCurrent( pObj ) )
return;
Nwk_ObjSetTravIdCurrent( pObj );
if ( Nwk_ObjIsCi(pObj) )
{
if ( pManTime )
{
iBox = Tim_ManBoxForCi( pManTime, pObj->PioId );
if ( iBox >= 0 ) // this is not a true PI
{
iTerm1 = Tim_ManBoxInputFirst( pManTime, iBox );
nTerms = Tim_ManBoxInputNum( pManTime, iBox );
for ( i = 0; i < nTerms; i++ )
{
pNext = Nwk_ManCo(pObj->pMan, iTerm1 + i);
Nwk_ManLevel_rec( pNext );
if ( LevelMax < Nwk_ObjLevel(pNext) )
LevelMax = Nwk_ObjLevel(pNext);
}
LevelMax++;
}
}
}
else if ( Nwk_ObjIsNode(pObj) || Nwk_ObjIsCo(pObj) )
{
Nwk_ObjForEachFanin( pObj, pNext, i )
{
Nwk_ManLevel_rec( pNext );
if ( LevelMax < Nwk_ObjLevel(pNext) )
LevelMax = Nwk_ObjLevel(pNext);
}
if ( Nwk_ObjIsNode(pObj) && Nwk_ObjFaninNum(pObj) > 0 )
LevelMax++;
}
else
assert( 0 );
Nwk_ObjSetLevel( pObj, LevelMax );
}
/**Function*************************************************************
Synopsis [Computes the number of logic levels not counting PIs/POs.]
Description [Does not assume that the objects are in a topo order.]
SideEffects []
SeeAlso []
***********************************************************************/
int Nwk_ManLevel( Nwk_Man_t * pNtk )
{
Nwk_Obj_t * pObj;
int i, LevelMax = 0;
Nwk_ManForEachObj( pNtk, pObj, i )
Nwk_ObjSetLevel( pObj, 0 );
Nwk_ManIncrementTravId( pNtk );
Nwk_ManForEachPo( pNtk, pObj, i )
{
Nwk_ManLevel_rec( pObj );
if ( LevelMax < Nwk_ObjLevel(pObj) )
LevelMax = Nwk_ObjLevel(pObj);
}
Nwk_ManForEachCi( pNtk, pObj, i )
{
Nwk_ManLevel_rec( pObj );
if ( LevelMax < Nwk_ObjLevel(pObj) )
LevelMax = Nwk_ObjLevel(pObj);
}
return LevelMax;
}
/**Function*************************************************************
Synopsis [Computes the number of logic levels not counting PIs/POs.]
Description [Does not assume that the objects are in a topo order.]
SideEffects []
SeeAlso []
***********************************************************************/
int Nwk_ManLevelMax( Nwk_Man_t * pNtk )
{
Nwk_Obj_t * pObj;
int i, LevelMax = 0;
Nwk_ManForEachPo( pNtk, pObj, i )
if ( LevelMax < Nwk_ObjLevel(pObj) )
LevelMax = Nwk_ObjLevel(pObj);
return LevelMax;
}
/**Function*************************************************************
Synopsis [Returns the array of objects in the AIG manager ordered by level.]
Description []
SideEffects []
SeeAlso []
***********************************************************************/
Vec_Vec_t * Nwk_ManLevelize( Nwk_Man_t * pNtk )
{
Nwk_Obj_t * pObj;
Vec_Vec_t * vLevels;
int nLevels, i;
assert( Nwk_ManVerifyLevel(pNtk) );
nLevels = Nwk_ManLevelMax( pNtk );
vLevels = Vec_VecStart( nLevels + 1 );
Nwk_ManForEachNode( pNtk, pObj, i )
{
assert( Nwk_ObjLevel(pObj) <= nLevels );
Vec_VecPush( vLevels, Nwk_ObjLevel(pObj), pObj );
}
return vLevels;
}
/**Function*************************************************************
Synopsis [Performs DFS for one node.]
Description []
SideEffects []
SeeAlso []
***********************************************************************/
void Nwk_ManDfs_rec( Nwk_Obj_t * pObj, Vec_Ptr_t * vNodes )
{
Nwk_Obj_t * pNext;
int i;
if ( Nwk_ObjIsTravIdCurrent( pObj ) )
return;
Nwk_ObjSetTravIdCurrent( pObj );
Nwk_ObjForEachFanin( pObj, pNext, i )
Nwk_ManDfs_rec( pNext, vNodes );
Vec_PtrPush( vNodes, pObj );
}
/**Function*************************************************************
Synopsis [Returns the DFS ordered array of all objects except latches.]
Description []
SideEffects []
SeeAlso []
***********************************************************************/
Vec_Ptr_t * Nwk_ManDfs( Nwk_Man_t * pNtk )
{
Vec_Ptr_t * vNodes;
Nwk_Obj_t * pObj;
int i;
Nwk_ManIncrementTravId( pNtk );
vNodes = Vec_PtrAlloc( 100 );
Nwk_ManForEachObj( pNtk, pObj, i )
{
if ( Nwk_ObjIsCi(pObj) )
{
Nwk_ObjSetTravIdCurrent( pObj );
Vec_PtrPush( vNodes, pObj );
}
else if ( Nwk_ObjIsCo(pObj) )
Nwk_ManDfs_rec( pObj, vNodes );
}
return vNodes;
}
/**Function*************************************************************
Synopsis [Performs DFS for one node.]
Description []
SideEffects []
SeeAlso []
***********************************************************************/
void Nwk_ManDfsNodes_rec( Nwk_Obj_t * pObj, Vec_Ptr_t * vNodes )
{
Nwk_Obj_t * pNext;
int i;
if ( Nwk_ObjIsTravIdCurrent( pObj ) )
return;
Nwk_ObjSetTravIdCurrent( pObj );
if ( Nwk_ObjIsCi(pObj) )
return;
assert( Nwk_ObjIsNode(pObj) );
Nwk_ObjForEachFanin( pObj, pNext, i )
Nwk_ManDfsNodes_rec( pNext, vNodes );
Vec_PtrPush( vNodes, pObj );
}
/**Function*************************************************************
Synopsis [Returns the set of internal nodes rooted in the given nodes.]
Description []
SideEffects []
SeeAlso []
***********************************************************************/
Vec_Ptr_t * Nwk_ManDfsNodes( Nwk_Man_t * pNtk, Nwk_Obj_t ** ppNodes, int nNodes )
{
Vec_Ptr_t * vNodes;
int i;
// set the traversal ID
Nwk_ManIncrementTravId( pNtk );
// start the array of nodes
vNodes = Vec_PtrAlloc( 100 );
// go through the PO nodes and call for each of them
for ( i = 0; i < nNodes; i++ )
if ( Nwk_ObjIsCo(ppNodes[i]) )
Nwk_ManDfsNodes_rec( Nwk_ObjFanin0(ppNodes[i]), vNodes );
else
Nwk_ManDfsNodes_rec( ppNodes[i], vNodes );
return vNodes;
}
/**Function*************************************************************
Synopsis [Performs DFS for one node.]
Description []
SideEffects []
SeeAlso []
***********************************************************************/
void Nwk_ManDfsReverse_rec( Nwk_Obj_t * pObj, Vec_Ptr_t * vNodes )
{
Nwk_Obj_t * pNext;
int i, iBox, iTerm1, nTerms;
if ( Nwk_ObjIsTravIdCurrent( pObj ) )
return;
Nwk_ObjSetTravIdCurrent( pObj );
if ( Nwk_ObjIsCo(pObj) )
{
if ( pObj->pMan->pManTime )
{
iBox = Tim_ManBoxForCo( pObj->pMan->pManTime, pObj->PioId );
if ( iBox >= 0 ) // this is not a true PO
{
iTerm1 = Tim_ManBoxOutputFirst( pObj->pMan->pManTime, iBox );
nTerms = Tim_ManBoxOutputNum( pObj->pMan->pManTime, iBox );
for ( i = 0; i < nTerms; i++ )
{
pNext = Nwk_ManCi(pObj->pMan, iTerm1 + i);
Nwk_ManDfsReverse_rec( pNext, vNodes );
}
}
}
}
else if ( Nwk_ObjIsNode(pObj) || Nwk_ObjIsCi(pObj) )
{
Nwk_ObjForEachFanout( pObj, pNext, i )
Nwk_ManDfsReverse_rec( pNext, vNodes );
}
else
assert( 0 );
Vec_PtrPush( vNodes, pObj );
}
/**Function*************************************************************
Synopsis [Returns the DFS ordered array of all objects except latches.]
Description []
SideEffects []
SeeAlso []
***********************************************************************/
Vec_Ptr_t * Nwk_ManDfsReverse( Nwk_Man_t * pNtk )
{
Vec_Ptr_t * vNodes;
Nwk_Obj_t * pObj;
int i;
Nwk_ManIncrementTravId( pNtk );
vNodes = Vec_PtrAlloc( 100 );
Nwk_ManForEachPi( pNtk, pObj, i )
Nwk_ManDfsReverse_rec( pObj, vNodes );
// add nodes without fanins
Nwk_ManForEachNode( pNtk, pObj, i )
if ( Nwk_ObjFaninNum(pObj) == 0 && !Nwk_ObjIsTravIdCurrent(pObj) )
Vec_PtrPush( vNodes, pObj );
return vNodes;
}
/**Function*************************************************************
Synopsis [Performs DFS for one node.]
Description []
SideEffects []
SeeAlso []
***********************************************************************/
void Nwk_ManSupportNodes_rec( Nwk_Obj_t * pNode, Vec_Ptr_t * vNodes )
{
Nwk_Obj_t * pFanin;
int i;
// if this node is already visited, skip
if ( Nwk_ObjIsTravIdCurrent( pNode ) )
return;
// mark the node as visited
Nwk_ObjSetTravIdCurrent( pNode );
// collect the CI
if ( Nwk_ObjIsCi(pNode) )
{
Vec_PtrPush( vNodes, pNode );
return;
}
assert( Nwk_ObjIsNode( pNode ) );
// visit the transitive fanin of the node
Nwk_ObjForEachFanin( pNode, pFanin, i )
Nwk_ManSupportNodes_rec( pFanin, vNodes );
}
/**Function*************************************************************
Synopsis [Returns the set of CI nodes in the support of the given nodes.]
Description []
SideEffects []
SeeAlso []
***********************************************************************/
Vec_Ptr_t * Nwk_ManSupportNodes( Nwk_Man_t * pNtk, Nwk_Obj_t ** ppNodes, int nNodes )
{
Vec_Ptr_t * vNodes;
int i;
// set the traversal ID
Nwk_ManIncrementTravId( pNtk );
// start the array of nodes
vNodes = Vec_PtrAlloc( 100 );
// go through the PO nodes and call for each of them
for ( i = 0; i < nNodes; i++ )
if ( Nwk_ObjIsCo(ppNodes[i]) )
Nwk_ManSupportNodes_rec( Nwk_ObjFanin0(ppNodes[i]), vNodes );
else
Nwk_ManSupportNodes_rec( ppNodes[i], vNodes );
return vNodes;
}
/**Function*************************************************************
Synopsis [Computes the sum total of supports of all outputs.]
Description []
SideEffects []
SeeAlso []
***********************************************************************/
void Nwk_ManSupportSum( Nwk_Man_t * pNtk )
{
Vec_Ptr_t * vSupp;
Nwk_Obj_t * pObj;
int i, nTotalSupps = 0;
Nwk_ManForEachCo( pNtk, pObj, i )
{
vSupp = Nwk_ManSupportNodes( pNtk, &pObj, 1 );
nTotalSupps += Vec_PtrSize( vSupp );
Vec_PtrFree( vSupp );
}
printf( "Total supports = %d.\n", nTotalSupps );
}
/**Function*************************************************************
Synopsis [Dereferences the node's MFFC.]
Description []
SideEffects []
SeeAlso []
***********************************************************************/
int Nwk_ObjDeref_rec( Nwk_Obj_t * pNode )
{
Nwk_Obj_t * pFanin;
int i, Counter = 1;
if ( Nwk_ObjIsCi(pNode) )
return 0;
Nwk_ObjForEachFanin( pNode, pFanin, i )
{
assert( pFanin->nFanouts > 0 );
if ( --pFanin->nFanouts == 0 )
Counter += Nwk_ObjDeref_rec( pFanin );
}
return Counter;
}
/**Function*************************************************************
Synopsis [References the node's MFFC.]
Description []
SideEffects []
SeeAlso []
***********************************************************************/
int Nwk_ObjRef_rec( Nwk_Obj_t * pNode )
{
Nwk_Obj_t * pFanin;
int i, Counter = 1;
if ( Nwk_ObjIsCi(pNode) )
return 0;
Nwk_ObjForEachFanin( pNode, pFanin, i )
{
if ( pFanin->nFanouts++ == 0 )
Counter += Nwk_ObjRef_rec( pFanin );
}
return Counter;
}
/**Function*************************************************************
Synopsis [Collects the internal and boundary nodes in the derefed MFFC.]
Description []
SideEffects []
SeeAlso []
***********************************************************************/
void Nwk_ObjMffcLabel_rec( Nwk_Obj_t * pNode, int fTopmost )
{
Nwk_Obj_t * pFanin;
int i;
// add to the new support nodes
if ( !fTopmost && (Nwk_ObjIsCi(pNode) || pNode->nFanouts > 0) )
return;
// skip visited nodes
if ( Nwk_ObjIsTravIdCurrent(pNode) )
return;
Nwk_ObjSetTravIdCurrent(pNode);
// recur on the children
Nwk_ObjForEachFanin( pNode, pFanin, i )
Nwk_ObjMffcLabel_rec( pFanin, 0 );
// collect the internal node
// printf( "%d ", pNode->Id );
}
/**Function*************************************************************
Synopsis [Collects the internal nodes of the MFFC limited by cut.]
Description []
SideEffects [Increments the trav ID and marks visited nodes.]
SeeAlso []
***********************************************************************/
int Nwk_ObjMffcLabel( Nwk_Obj_t * pNode )
{
int Count1, Count2;
// dereference the node
Count1 = Nwk_ObjDeref_rec( pNode );
// collect the nodes inside the MFFC
Nwk_ManIncrementTravId( pNode->pMan );
Nwk_ObjMffcLabel_rec( pNode, 1 );
// reference it back
Count2 = Nwk_ObjRef_rec( pNode );
assert( Count1 == Count2 );
return Count1;
}
////////////////////////////////////////////////////////////////////////
/// END OF FILE ///
////////////////////////////////////////////////////////////////////////
|