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
|
/**CFile****************************************************************
FileName [nwkFlow.c]
SystemName [ABC: Logic synthesis and verification system.]
PackageName [Netlist representation.]
Synopsis [Max-flow/min-cut computation.]
Author [Alan Mishchenko]
Affiliation [UC Berkeley]
Date [Ver. 1.0. Started - June 20, 2005.]
Revision [$Id: nwkFlow.c,v 1.00 2005/06/20 00:00:00 alanmi Exp $]
***********************************************************************/
#include "nwk.h"
ABC_NAMESPACE_IMPL_START
/*
This code is based on the papers:
A. Hurst, A. Mishchenko, and R. Brayton, "Fast minimum-register retiming
via binary maximum-flow", Proc. FMCAD '07, pp. 181-187.
A. Hurst, A. Mishchenko, and R. Brayton, "Scalable min-area retiming
under simultaneous delay and initial state constraints". Proc. DAC'08.
*/
////////////////////////////////////////////////////////////////////////
/// DECLARATIONS ///
////////////////////////////////////////////////////////////////////////
// predecessors
static inline Nwk_Obj_t * Nwk_ObjPred( Nwk_Obj_t * pObj ) { return (Nwk_Obj_t *)pObj->pCopy; }
static inline int Nwk_ObjSetPred( Nwk_Obj_t * pObj, Nwk_Obj_t * p ) { pObj->pCopy = p; return 1; }
// sink
static inline int Nwk_ObjIsSink( Nwk_Obj_t * pObj ) { return pObj->MarkA; }
static inline void Nwk_ObjSetSink( Nwk_Obj_t * pObj ) { pObj->MarkA = 1; }
// flow
static inline int Nwk_ObjHasFlow( Nwk_Obj_t * pObj ) { return pObj->MarkB; }
static inline void Nwk_ObjSetFlow( Nwk_Obj_t * pObj ) { pObj->MarkB = 1; }
static inline void Nwk_ObjClearFlow( Nwk_Obj_t * pObj ) { pObj->MarkB = 0; }
// representation of visited nodes
// pObj->TravId < pNtk->nTravIds-2 --- not visited
// pObj->TravId == pNtk->nTravIds-2 --- visited bot only
// pObj->TravId == pNtk->nTravIds-1 --- visited top only
// pObj->TravId == pNtk->nTravIds --- visited bot and top
static inline int Nwk_ObjVisitedBotOnly( Nwk_Obj_t * pObj )
{
return pObj->TravId == pObj->pMan->nTravIds - 2;
}
static inline int Nwk_ObjVisitedBot( Nwk_Obj_t * pObj )
{
return pObj->TravId == pObj->pMan->nTravIds - 2 || pObj->TravId == pObj->pMan->nTravIds;
}
static inline int Nwk_ObjVisitedTop( Nwk_Obj_t * pObj )
{
return pObj->TravId == pObj->pMan->nTravIds - 1 || pObj->TravId == pObj->pMan->nTravIds;
}
static inline void Nwk_ObjSetVisitedBot( Nwk_Obj_t * pObj )
{
if ( pObj->TravId < pObj->pMan->nTravIds - 2 )
pObj->TravId = pObj->pMan->nTravIds - 2;
else if ( pObj->TravId == pObj->pMan->nTravIds - 1 )
pObj->TravId = pObj->pMan->nTravIds;
else
assert( 0 );
}
static inline void Nwk_ObjSetVisitedTop( Nwk_Obj_t * pObj )
{
if ( pObj->TravId < pObj->pMan->nTravIds - 2 )
pObj->TravId = pObj->pMan->nTravIds - 1;
else if ( pObj->TravId == pObj->pMan->nTravIds - 2 )
pObj->TravId = pObj->pMan->nTravIds;
else
assert( 0 );
}
static inline void Nwk_ManIncrementTravIdFlow( Nwk_Man_t * pMan )
{
Nwk_ManIncrementTravId( pMan );
Nwk_ManIncrementTravId( pMan );
Nwk_ManIncrementTravId( pMan );
}
static int Nwk_ManPushForwardTop_rec( Nwk_Obj_t * pObj, Nwk_Obj_t * pPred );
static int Nwk_ManPushForwardBot_rec( Nwk_Obj_t * pObj, Nwk_Obj_t * pPred );
static int Nwk_ManPushBackwardTop_rec( Nwk_Obj_t * pObj, Nwk_Obj_t * pPred );
static int Nwk_ManPushBackwardBot_rec( Nwk_Obj_t * pObj, Nwk_Obj_t * pPred );
////////////////////////////////////////////////////////////////////////
/// FUNCTION DEFINITIONS ///
////////////////////////////////////////////////////////////////////////
/**Function*************************************************************
Synopsis [Marks TFI of the node.]
Description []
SideEffects []
SeeAlso []
***********************************************************************/
void Nwk_ManMarkTfiCone_rec( Nwk_Obj_t * pObj )
{
Nwk_Obj_t * pNext;
int i;
if ( pObj->MarkA )
return;
pObj->MarkA = 1;
Nwk_ObjForEachFanin( pObj, pNext, i )
Nwk_ManMarkTfiCone_rec( pNext );
}
/**Function*************************************************************
Synopsis [Marks TFO of the node.]
Description []
SideEffects []
SeeAlso []
***********************************************************************/
void Nwk_ManMarkTfoCone_rec( Nwk_Obj_t * pObj )
{
Nwk_Obj_t * pNext;
int i;
if ( pObj->MarkA )
return;
pObj->MarkA = 1;
Nwk_ObjForEachFanout( pObj, pNext, i )
Nwk_ManMarkTfoCone_rec( pNext );
}
/**Function*************************************************************
Synopsis [Fast forward flow pushing.]
Description []
SideEffects []
SeeAlso []
***********************************************************************/
int Nwk_ManPushForwardFast_rec( Nwk_Obj_t * pObj, Nwk_Obj_t * pPred )
{
Nwk_Obj_t * pNext;
int i;
if ( Nwk_ObjIsTravIdCurrent( pObj ) )
return 0;
Nwk_ObjSetTravIdCurrent( pObj );
if ( Nwk_ObjHasFlow(pObj) )
return 0;
if ( Nwk_ObjIsSink(pObj) )
{
Nwk_ObjSetFlow(pObj);
return Nwk_ObjSetPred( pObj, pPred );
}
Nwk_ObjForEachFanout( pObj, pNext, i )
if ( Nwk_ManPushForwardFast_rec( pNext, pObj ) )
{
Nwk_ObjSetFlow(pObj);
return Nwk_ObjSetPred( pObj, pPred );
}
return 0;
}
/**Function*************************************************************
Synopsis [Fast backward flow pushing.]
Description []
SideEffects []
SeeAlso []
***********************************************************************/
int Nwk_ManPushBackwardFast_rec( Nwk_Obj_t * pObj, Nwk_Obj_t * pPred )
{
Nwk_Obj_t * pNext;
int i;
if ( Nwk_ObjIsTravIdCurrent( pObj ) )
return 0;
Nwk_ObjSetTravIdCurrent( pObj );
if ( Nwk_ObjHasFlow(pObj) )
return 0;
if ( Nwk_ObjIsSink(pObj) )
{
Nwk_ObjSetFlow(pObj);
return Nwk_ObjSetPred( pObj, pPred );
}
Nwk_ObjForEachFanin( pObj, pNext, i )
if ( Nwk_ManPushBackwardFast_rec( pNext, pObj ) )
{
Nwk_ObjSetFlow(pObj);
return Nwk_ObjSetPred( pObj, pPred );
}
return 0;
}
/**Function*************************************************************
Synopsis [Pushing the flow through the bottom part of the node.]
Description []
SideEffects []
SeeAlso []
***********************************************************************/
int Nwk_ManPushForwardBot_rec( Nwk_Obj_t * pObj, Nwk_Obj_t * pPred )
{
Nwk_Obj_t * pNext;
int i;
if ( Nwk_ObjVisitedBot(pObj) )
return 0;
Nwk_ObjSetVisitedBot(pObj);
// propagate through the internal edge
if ( Nwk_ObjHasFlow(pObj) )
{
if ( Nwk_ObjPred(pObj) )
if ( Nwk_ManPushForwardTop_rec( Nwk_ObjPred(pObj), Nwk_ObjPred(pObj) ) )
return Nwk_ObjSetPred( pObj, pPred );
}
else if ( Nwk_ManPushForwardTop_rec(pObj, pObj) )
{
Nwk_ObjSetFlow( pObj );
return Nwk_ObjSetPred( pObj, pPred );
}
// try to push through the fanins
Nwk_ObjForEachFanin( pObj, pNext, i )
if ( Nwk_ManPushForwardBot_rec( pNext, pPred ) )
return 1;
return 0;
}
/**Function*************************************************************
Synopsis [Pushing the flow through the top part of the node.]
Description []
SideEffects []
SeeAlso []
***********************************************************************/
int Nwk_ManPushForwardTop_rec( Nwk_Obj_t * pObj, Nwk_Obj_t * pPred )
{
Nwk_Obj_t * pNext;
int i;
if ( Nwk_ObjVisitedTop(pObj) )
return 0;
Nwk_ObjSetVisitedTop(pObj);
// check if this is the sink
if ( Nwk_ObjIsSink(pObj) )
return 1;
// try to push through the fanouts
Nwk_ObjForEachFanout( pObj, pNext, i )
if ( Nwk_ManPushForwardBot_rec( pNext, pPred ) )
return 1;
// redirect the flow
if ( Nwk_ObjHasFlow(pObj) && !Nwk_ObjIsCi(pObj) )
if ( Nwk_ManPushForwardBot_rec( pObj, Nwk_ObjPred(pObj) ) )
{
Nwk_ObjClearFlow( pObj );
return Nwk_ObjSetPred( pObj, NULL );
}
return 0;
}
/**Function*************************************************************
Synopsis [Pushing the flow through the bottom part of the node.]
Description []
SideEffects []
SeeAlso []
***********************************************************************/
int Nwk_ManPushBackwardBot_rec( Nwk_Obj_t * pObj, Nwk_Obj_t * pPred )
{
if ( Nwk_ObjVisitedBot(pObj) )
return 0;
Nwk_ObjSetVisitedBot(pObj);
// propagate through the internal edge
if ( Nwk_ObjHasFlow(pObj) )
{
if ( Nwk_ObjPred(pObj) )
if ( Nwk_ManPushBackwardTop_rec( Nwk_ObjPred(pObj), Nwk_ObjPred(pObj) ) )
return Nwk_ObjSetPred( pObj, pPred );
}
else if ( Nwk_ManPushBackwardTop_rec(pObj, pObj) )
{
Nwk_ObjSetFlow( pObj );
return Nwk_ObjSetPred( pObj, pPred );
}
return 0;
}
/**Function*************************************************************
Synopsis [Pushing the flow through the top part of the node.]
Description []
SideEffects []
SeeAlso []
***********************************************************************/
int Nwk_ManPushBackwardTop_rec( Nwk_Obj_t * pObj, Nwk_Obj_t * pPred )
{
Nwk_Obj_t * pNext;
int i;
if ( Nwk_ObjVisitedTop(pObj) )
return 0;
Nwk_ObjSetVisitedTop(pObj);
// check if this is the sink
if ( Nwk_ObjIsSink(pObj) )
return 1;
// try to push through the fanins
Nwk_ObjForEachFanin( pObj, pNext, i )
if ( Nwk_ManPushBackwardBot_rec( pNext, pPred ) )
return 1;
// try to push through the fanouts
Nwk_ObjForEachFanout( pObj, pNext, i )
if ( !Nwk_ObjIsCo(pObj) && Nwk_ManPushBackwardTop_rec( pNext, pPred ) )
return 1;
// redirect the flow
if ( Nwk_ObjHasFlow(pObj) )
if ( Nwk_ObjPred(pObj) && Nwk_ManPushBackwardBot_rec( pObj, Nwk_ObjPred(pObj) ) )
{
Nwk_ObjClearFlow( pObj );
return Nwk_ObjSetPred( pObj, NULL );
}
return 0;
}
/**Function*************************************************************
Synopsis [Returns 0 if there is an unmarked path to a CI.]
Description []
SideEffects []
SeeAlso []
***********************************************************************/
int Nwk_ManVerifyCut_rec( Nwk_Obj_t * pObj )
{
Nwk_Obj_t * pNext;
int i;
if ( pObj->MarkA )
return 1;
if ( Nwk_ObjIsLo(pObj) )
return 0;
if ( Nwk_ObjIsTravIdCurrent( pObj ) )
return 1;
Nwk_ObjSetTravIdCurrent( pObj );
Nwk_ObjForEachFanin( pObj, pNext, i )
if ( !Nwk_ManVerifyCut_rec( pNext ) )
return 0;
return 1;
}
/**Function*************************************************************
Synopsis [Verifies the forward cut.]
Description []
SideEffects []
SeeAlso []
***********************************************************************/
int Nwk_ManRetimeVerifyCutForward( Nwk_Man_t * pMan, Vec_Ptr_t * vNodes )
{
Nwk_Obj_t * pObj;
int i;
// mark the nodes
Vec_PtrForEachEntry( Nwk_Obj_t *, vNodes, pObj, i )
{
assert( pObj->MarkA == 0 );
pObj->MarkA = 1;
}
// traverse from the COs
Nwk_ManIncrementTravId( pMan );
Nwk_ManForEachCo( pMan, pObj, i )
if ( !Nwk_ManVerifyCut_rec( pObj ) )
printf( "Nwk_ManRetimeVerifyCutForward(): Internal cut verification failed.\n" );
// unmark the nodes
Vec_PtrForEachEntry( Nwk_Obj_t *, vNodes, pObj, i )
pObj->MarkA = 0;
return 1;
}
/**Function*************************************************************
Synopsis [Verifies the forward cut.]
Description []
SideEffects []
SeeAlso []
***********************************************************************/
int Nwk_ManRetimeVerifyCutBackward( Nwk_Man_t * pMan, Vec_Ptr_t * vNodes )
{
return 1;
}
/**Function*************************************************************
Synopsis [Computes minimum cut for forward retiming.]
Description []
SideEffects []
SeeAlso []
***********************************************************************/
Vec_Ptr_t * Nwk_ManRetimeCutForward( Nwk_Man_t * pMan, int nLatches, int fVerbose )
{
Vec_Ptr_t * vNodes;
Nwk_Obj_t * pObj;
int i, RetValue, Counter = 0, Counter2 = 0;
clock_t clk = clock();
// set the sequential parameters
pMan->nLatches = nLatches;
pMan->nTruePis = Nwk_ManCiNum(pMan) - nLatches;
pMan->nTruePos = Nwk_ManCoNum(pMan) - nLatches;
// mark the COs and the TFO of PIs
Nwk_ManForEachCo( pMan, pObj, i )
pObj->MarkA = 1;
Nwk_ManForEachPiSeq( pMan, pObj, i )
Nwk_ManMarkTfoCone_rec( pObj );
// start flow computation from each LO
Nwk_ManIncrementTravIdFlow( pMan );
Nwk_ManForEachLoSeq( pMan, pObj, i )
{
if ( !Nwk_ManPushForwardFast_rec( pObj, NULL ) )
continue;
Nwk_ManIncrementTravIdFlow( pMan );
Counter++;
}
if ( fVerbose )
printf( "Forward: Max-flow = %4d -> ", Counter );
// continue flow computation from each LO
Nwk_ManIncrementTravIdFlow( pMan );
Nwk_ManForEachLoSeq( pMan, pObj, i )
{
if ( !Nwk_ManPushForwardBot_rec( pObj, NULL ) )
continue;
Nwk_ManIncrementTravIdFlow( pMan );
Counter2++;
}
if ( fVerbose )
printf( "%4d. ", Counter+Counter2 );
// repeat flow computation from each LO
if ( Counter2 > 0 )
{
Nwk_ManIncrementTravIdFlow( pMan );
Nwk_ManForEachLoSeq( pMan, pObj, i )
{
RetValue = Nwk_ManPushForwardBot_rec( pObj, NULL );
assert( !RetValue );
}
}
// cut is a set of nodes whose bottom is visited but top is not visited
vNodes = Vec_PtrAlloc( Counter+Counter2 );
Counter = 0;
Nwk_ManForEachObj( pMan, pObj, i )
{
if ( Nwk_ObjVisitedBotOnly(pObj) )
{
assert( Nwk_ObjHasFlow(pObj) );
assert( !Nwk_ObjIsCo(pObj) );
Vec_PtrPush( vNodes, pObj );
Counter += Nwk_ObjIsCi(pObj);
}
}
Nwk_ManCleanMarks( pMan );
// assert( Nwk_ManRetimeVerifyCutForward(pMan, vNodes) );
if ( fVerbose )
{
printf( "Min-cut = %4d. Unmoved = %4d. ", Vec_PtrSize(vNodes), Counter );
ABC_PRT( "Time", clock() - clk );
}
return vNodes;
}
/**Function*************************************************************
Synopsis [Computes minimum cut for backward retiming.]
Description []
SideEffects []
SeeAlso []
***********************************************************************/
Vec_Ptr_t * Nwk_ManRetimeCutBackward( Nwk_Man_t * pMan, int nLatches, int fVerbose )
{
Vec_Ptr_t * vNodes;
Nwk_Obj_t * pObj;
int i, RetValue, Counter = 0, Counter2 = 0;
clock_t clk = clock();
// set the sequential parameters
pMan->nLatches = nLatches;
pMan->nTruePis = Nwk_ManCiNum(pMan) - nLatches;
pMan->nTruePos = Nwk_ManCoNum(pMan) - nLatches;
// mark the CIs, the TFI of POs, and the constant nodes
Nwk_ManForEachCi( pMan, pObj, i )
pObj->MarkA = 1;
Nwk_ManForEachPoSeq( pMan, pObj, i )
Nwk_ManMarkTfiCone_rec( pObj );
Nwk_ManForEachNode( pMan, pObj, i )
if ( Nwk_ObjFaninNum(pObj) == 0 )
pObj->MarkA = 1;
// start flow computation from each LI driver
Nwk_ManIncrementTravIdFlow( pMan );
Nwk_ManForEachLiSeq( pMan, pObj, i )
{
if ( !Nwk_ManPushBackwardFast_rec( Nwk_ObjFanin0(pObj), NULL ) )
continue;
Nwk_ManIncrementTravIdFlow( pMan );
Counter++;
}
if ( fVerbose )
printf( "Backward: Max-flow = %4d -> ", Counter );
// continue flow computation from each LI driver
Nwk_ManIncrementTravIdFlow( pMan );
Nwk_ManForEachLiSeq( pMan, pObj, i )
{
if ( !Nwk_ManPushBackwardBot_rec( Nwk_ObjFanin0(pObj), NULL ) )
continue;
Nwk_ManIncrementTravIdFlow( pMan );
Counter2++;
}
if ( fVerbose )
printf( "%4d. ", Counter+Counter2 );
// repeat flow computation from each LI driver
if ( Counter2 > 0 )
{
Nwk_ManIncrementTravIdFlow( pMan );
Nwk_ManForEachLiSeq( pMan, pObj, i )
{
RetValue = Nwk_ManPushBackwardBot_rec( Nwk_ObjFanin0(pObj), NULL );
assert( !RetValue );
}
}
// cut is a set of nodes whose bottom is visited but top is not visited
vNodes = Vec_PtrAlloc( Counter+Counter2 );
Nwk_ManForEachObj( pMan, pObj, i )
{
if ( Nwk_ObjVisitedBotOnly(pObj) )
{
assert( Nwk_ObjHasFlow(pObj) );
assert( !Nwk_ObjIsCo(pObj) );
Vec_PtrPush( vNodes, pObj );
}
}
// count CO drivers
Counter = 0;
Nwk_ManForEachLiSeq( pMan, pObj, i )
if ( Nwk_ObjVisitedBotOnly( Nwk_ObjFanin0(pObj) ) )
Counter++;
Nwk_ManCleanMarks( pMan );
// assert( Nwk_ManRetimeVerifyCutBackward(pMan, vNodes) );
if ( fVerbose )
{
printf( "Min-cut = %4d. Unmoved = %4d. ", Vec_PtrSize(vNodes), Counter );
ABC_PRT( "Time", clock() - clk );
}
return vNodes;
}
////////////////////////////////////////////////////////////////////////
/// END OF FILE ///
////////////////////////////////////////////////////////////////////////
ABC_NAMESPACE_IMPL_END
|