abc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
retDelay.c
Go to the documentation of this file.
1 /**CFile****************************************************************
2 
3  FileName [retDelay.c]
4 
5  SystemName [ABC: Logic synthesis and verification system.]
6 
7  PackageName [Retiming package.]
8 
9  Synopsis [Incremental retiming for optimum delay.]
10 
11  Author [Alan Mishchenko]
12 
13  Affiliation [UC Berkeley]
14 
15  Date [Ver. 1.0. Started - Oct 31, 2006.]
16 
17  Revision [$Id: retDelay.c,v 1.00 2006/10/31 00:00:00 alanmi Exp $]
18 
19 ***********************************************************************/
20 
21 #include "retInt.h"
22 
24 
25 
26 ////////////////////////////////////////////////////////////////////////
27 /// DECLARATIONS ///
28 ////////////////////////////////////////////////////////////////////////
29 
30 static int Abc_NtkRetimeMinDelayTry( Abc_Ntk_t * pNtk, int nDelayLim, int fForward, int fInitial, int nIterLimit, int * pIterBest, int fVerbose );
31 static int Abc_NtkRetimeTiming( Abc_Ntk_t * pNtk, int fForward, Vec_Ptr_t * vCritical );
32 static int Abc_NtkRetimeTiming_rec( Abc_Obj_t * pObj, int fForward );
33 
34 ////////////////////////////////////////////////////////////////////////
35 /// FUNCTION DEFINITIONS ///
36 ////////////////////////////////////////////////////////////////////////
37 
38 /**Function*************************************************************
39 
40  Synopsis [Retimes incrementally for minimum delay.]
41 
42  Description [This procedure cannot be called in the application code
43  because it assumes that the network is preprocessed by removing LIs/LOs.]
44 
45  SideEffects []
46 
47  SeeAlso []
48 
49 ***********************************************************************/
50 int Abc_NtkRetimeMinDelay( Abc_Ntk_t * pNtk, Abc_Ntk_t * pNtkCopy, int nDelayLim, int nIterLimit, int fForward, int fVerbose )
51 {
52  int IterBest, DelayBest;
53  int IterBest2, DelayBest2;
54  // try to find the best delay iteration on a copy
55  DelayBest = Abc_NtkRetimeMinDelayTry( pNtkCopy, nDelayLim, fForward, 0, nIterLimit, &IterBest, fVerbose );
56  if ( IterBest == 0 )
57  return 1;
58  // perform the given number of iterations on the original network
59  DelayBest2 = Abc_NtkRetimeMinDelayTry( pNtk, nDelayLim, fForward, 1, IterBest, &IterBest2, fVerbose );
60  assert( DelayBest == DelayBest2 );
61  assert( IterBest == IterBest2 );
62  return 1;
63 }
64 
65 /**Function*************************************************************
66 
67  Synopsis [Returns the best delay and the number of best iteration.]
68 
69  Description []
70 
71  SideEffects []
72 
73  SeeAlso []
74 
75 ***********************************************************************/
76 int Abc_NtkRetimeMinDelayTry( Abc_Ntk_t * pNtk, int nDelayLim, int fForward, int fInitial, int nIterLimit, int * pIterBest, int fVerbose )
77 {
78  Abc_Ntk_t * pNtkNew = NULL;
79  Vec_Ptr_t * vCritical;
80  Vec_Int_t * vValues = NULL; // Suppress "might be used uninitialized"
81  Abc_Obj_t * pObj;
82  int i, k, IterBest, DelayCur, DelayBest;
83  int DelayStart = -1; // Suppress "might be used uninitialized"
84  int LatchesBest;
85  // transfer intitial values
86  if ( fInitial )
87  {
88  if ( fForward )
90  else
91  {
92  // save initial value of the latches
93  vValues = Abc_NtkRetimeCollectLatchValues( pNtk );
94  // start the network for initial value computation
95  pNtkNew = Abc_NtkRetimeBackwardInitialStart( pNtk );
96  }
97  }
98 
99 if ( fVerbose && !fInitial )
100  printf( "Performing analysis:\n" );
101  // find the best iteration
102  DelayBest = ABC_INFINITY; IterBest = 0; LatchesBest = Abc_NtkLatchNum(pNtk);
103  vCritical = Vec_PtrAlloc( 100 );
104  for ( i = 0; ; i++ )
105  {
106  // perform moves for the timing-critical nodes
107  DelayCur = Abc_NtkRetimeTiming( pNtk, fForward, vCritical );
108  if ( i == 0 )
109  DelayStart = DelayCur;
110  // record this position if it has the best delay
111  if ( DelayBest > DelayCur )
112  {
113 if ( fVerbose && !fInitial )
114  printf( "%s Iter = %3d. Delay = %3d. Latches = %5d. Delta = %6.2f. Ratio = %4.2f %%\n",
115  fForward ? "Fwd": "Bwd", i, DelayCur, Abc_NtkLatchNum(pNtk),
116  1.0*(Abc_NtkLatchNum(pNtk)-LatchesBest)/(DelayBest-DelayCur),
117  100.0*(Abc_NtkLatchNum(pNtk)-LatchesBest)/Abc_NtkLatchNum(pNtk)/(DelayBest-DelayCur) );
118 
119  DelayBest = DelayCur;
120  IterBest = i;
121  LatchesBest = Abc_NtkLatchNum(pNtk);
122  }
123  // quit after timing analysis
124  if ( i == nIterLimit )
125  break;
126  // skip if 10 interations did not give improvement
127  if ( i - IterBest > 20 )
128  break;
129  // skip if delay limit is reached
130  if ( nDelayLim > 0 && DelayCur <= nDelayLim )
131  break;
132  // try retiming to improve the delay
133  Vec_PtrForEachEntry( Abc_Obj_t *, vCritical, pObj, k )
134  if ( Abc_NtkRetimeNodeIsEnabled(pObj, fForward) )
135  Abc_NtkRetimeNode( pObj, fForward, fInitial );
136  // share latches
137  if ( !fForward )
138  Abc_NtkRetimeShareLatches( pNtk, fInitial );
139  }
140  Vec_PtrFree( vCritical );
141  // transfer the initial state back to the latches
142  if ( fInitial )
143  {
144  if ( fForward )
146  else
147  {
148  Abc_NtkRetimeBackwardInitialFinish( pNtk, pNtkNew, vValues, fVerbose );
149  Abc_NtkDelete( pNtkNew );
150  Vec_IntFree( vValues );
151  }
152  }
153 if ( fVerbose && !fInitial )
154  printf( "%s : Starting delay = %3d. Final delay = %3d. IterBest = %2d (out of %2d).\n",
155  fForward? "Forward " : "Backward", DelayStart, DelayBest, IterBest, nIterLimit );
156  *pIterBest = (nIterLimit == 1) ? 1 : IterBest;
157  return DelayBest;
158 }
159 
160 /**Function*************************************************************
161 
162  Synopsis [Returns the set of timing-critical nodes.]
163 
164  Description [Performs static timing analysis on the network. Uses
165  unit-delay model.]
166 
167  SideEffects []
168 
169  SeeAlso []
170 
171 ***********************************************************************/
172 int Abc_NtkRetimeTiming( Abc_Ntk_t * pNtk, int fForward, Vec_Ptr_t * vCritical )
173 {
174  Vec_Ptr_t * vLatches;
175  Abc_Obj_t * pObj, * pNext;
176  int i, k, LevelCur, LevelMax = 0;
177  // mark all objects except nodes
179  vLatches = Vec_PtrAlloc( Abc_NtkLatchNum(pNtk) );
180  Abc_NtkForEachObj( pNtk, pObj, i )
181  {
182  if ( Abc_ObjIsLatch(pObj) )
183  Vec_PtrPush( vLatches, pObj );
184  if ( Abc_ObjIsNode(pObj) )
185  continue;
186  pObj->Level = 0;
187  Abc_NodeSetTravIdCurrent( pObj );
188  }
189  // perform analysis from CIs/COs
190  if ( fForward )
191  {
192  Vec_PtrForEachEntry( Abc_Obj_t *, vLatches, pObj, i )
193  {
194  Abc_ObjForEachFanout( pObj, pNext, k )
195  {
196  LevelCur = Abc_NtkRetimeTiming_rec( pNext, fForward );
197  if ( LevelMax < LevelCur )
198  LevelMax = LevelCur;
199  }
200  }
201  Abc_NtkForEachPi( pNtk, pObj, i )
202  {
203  Abc_ObjForEachFanout( pObj, pNext, k )
204  {
205  LevelCur = Abc_NtkRetimeTiming_rec( pNext, fForward );
206  if ( LevelMax < LevelCur )
207  LevelMax = LevelCur;
208  }
209  }
210  }
211  else
212  {
213  Vec_PtrForEachEntry( Abc_Obj_t *, vLatches, pObj, i )
214  {
215  LevelCur = Abc_NtkRetimeTiming_rec( Abc_ObjFanin0(pObj), fForward );
216  if ( LevelMax < LevelCur )
217  LevelMax = LevelCur;
218  }
219  Abc_NtkForEachPo( pNtk, pObj, i )
220  {
221  LevelCur = Abc_NtkRetimeTiming_rec( Abc_ObjFanin0(pObj), fForward );
222  if ( LevelMax < LevelCur )
223  LevelMax = LevelCur;
224  }
225  }
226  // collect timing critical nodes, which should be retimed forward/backward
227  Vec_PtrClear( vCritical );
229  if ( fForward )
230  {
231  Vec_PtrForEachEntry( Abc_Obj_t *, vLatches, pObj, i )
232  {
233  Abc_ObjForEachFanout( pObj, pNext, k )
234  {
235  if ( Abc_NodeIsTravIdCurrent(pNext) )
236  continue;
237  if ( LevelMax != (int)pNext->Level )
238  continue;
239  // new critical node
240  Vec_PtrPush( vCritical, pNext );
241  Abc_NodeSetTravIdCurrent( pNext );
242  }
243  }
244  }
245  else
246  {
247  Vec_PtrForEachEntry( Abc_Obj_t *, vLatches, pObj, i )
248  {
249  Abc_ObjForEachFanin( pObj, pNext, k )
250  {
251  if ( Abc_NodeIsTravIdCurrent(pNext) )
252  continue;
253  if ( LevelMax != (int)pNext->Level )
254  continue;
255  // new critical node
256  Vec_PtrPush( vCritical, pNext );
257  Abc_NodeSetTravIdCurrent( pNext );
258  }
259  }
260  }
261  Vec_PtrFree( vLatches );
262  return LevelMax;
263 }
264 
265 /**Function*************************************************************
266 
267  Synopsis [Recursively performs timing analysis.]
268 
269  Description [Performs static timing analysis on the network. Uses
270  unit-delay model.]
271 
272  SideEffects []
273 
274  SeeAlso []
275 
276 ***********************************************************************/
277 int Abc_NtkRetimeTiming_rec( Abc_Obj_t * pObj, int fForward )
278 {
279  Abc_Obj_t * pNext;
280  int i, LevelCur, LevelMax = 0;
281  // skip visited nodes
282  if ( Abc_NodeIsTravIdCurrent(pObj) )
283  return pObj->Level;
285  // visit the next nodes
286  if ( fForward )
287  {
288  Abc_ObjForEachFanout( pObj, pNext, i )
289  {
290  LevelCur = Abc_NtkRetimeTiming_rec( pNext, fForward );
291  if ( LevelMax < LevelCur )
292  LevelMax = LevelCur;
293  }
294  }
295  else
296  {
297  Abc_ObjForEachFanin( pObj, pNext, i )
298  {
299  LevelCur = Abc_NtkRetimeTiming_rec( pNext, fForward );
300  if ( LevelMax < LevelCur )
301  LevelMax = LevelCur;
302  }
303  }
304 // printf( "Node %3d -> Level %3d.\n", pObj->Id, LevelMax + 1 );
305  pObj->Level = LevelMax + 1;
306  return pObj->Level;
307 }
308 
309 ////////////////////////////////////////////////////////////////////////
310 /// END OF FILE ///
311 ////////////////////////////////////////////////////////////////////////
312 
313 
315 
typedefABC_NAMESPACE_HEADER_START struct Vec_Ptr_t_ Vec_Ptr_t
INCLUDES ///.
Definition: vecPtr.h:42
typedefABC_NAMESPACE_IMPL_START struct Vec_Int_t_ Vec_Int_t
DECLARATIONS ///.
Definition: bblif.c:37
static int Abc_ObjIsLatch(Abc_Obj_t *pObj)
Definition: abc.h:356
int Abc_NtkRetimeMinDelay(Abc_Ntk_t *pNtk, Abc_Ntk_t *pNtkCopy, int nDelayLim, int nIterLimit, int fForward, int fVerbose)
FUNCTION DEFINITIONS ///.
Definition: retDelay.c:50
static int Abc_NtkLatchNum(Abc_Ntk_t *pNtk)
Definition: abc.h:294
static void Vec_PtrPush(Vec_Ptr_t *p, void *Entry)
Definition: vecPtr.h:606
static Abc_Obj_t * Abc_ObjFanin0(Abc_Obj_t *pObj)
Definition: abc.h:373
ABC_DLL void Abc_NtkDelete(Abc_Ntk_t *pNtk)
Definition: abcNtk.c:1233
unsigned Level
Definition: abc.h:142
void Abc_NtkRetimeShareLatches(Abc_Ntk_t *pNtk, int fInitial)
Definition: retIncrem.c:426
static int Abc_ObjIsNode(Abc_Obj_t *pObj)
Definition: abc.h:355
Abc_Ntk_t * Abc_NtkRetimeBackwardInitialStart(Abc_Ntk_t *pNtk)
Definition: retInit.c:254
static ABC_NAMESPACE_IMPL_START int Abc_NtkRetimeMinDelayTry(Abc_Ntk_t *pNtk, int nDelayLim, int fForward, int fInitial, int nIterLimit, int *pIterBest, int fVerbose)
DECLARATIONS ///.
Definition: retDelay.c:76
void Abc_NtkRetimeTranferToCopy(Abc_Ntk_t *pNtk)
Definition: retInit.c:168
Vec_Int_t * Abc_NtkRetimeCollectLatchValues(Abc_Ntk_t *pNtk)
Definition: retInit.c:208
void Abc_NtkRetimeNode(Abc_Obj_t *pObj, int fForward, int fInitial)
Definition: retIncrem.c:315
#define ABC_NAMESPACE_IMPL_END
Definition: abc_global.h:108
int Abc_NtkRetimeNodeIsEnabled(Abc_Obj_t *pObj, int fForward)
Definition: retIncrem.c:284
#define ABC_NAMESPACE_IMPL_START
Definition: abc_global.h:107
static int Abc_NodeIsTravIdCurrent(Abc_Obj_t *p)
Definition: abc.h:411
#define Abc_ObjForEachFanout(pObj, pFanout, i)
Definition: abc.h:526
static int Abc_NtkRetimeTiming_rec(Abc_Obj_t *pObj, int fForward)
Definition: retDelay.c:277
static Vec_Ptr_t * Vec_PtrAlloc(int nCap)
FUNCTION DEFINITIONS ///.
Definition: vecPtr.h:83
#define Abc_ObjForEachFanin(pObj, pFanin, i)
Definition: abc.h:524
void Abc_NtkRetimeBackwardInitialFinish(Abc_Ntk_t *pNtk, Abc_Ntk_t *pNtkNew, Vec_Int_t *vValuesOld, int fVerbose)
Definition: retInit.c:279
static void Abc_NtkIncrementTravId(Abc_Ntk_t *p)
Definition: abc.h:406
#define ABC_INFINITY
MACRO DEFINITIONS ///.
Definition: abc_global.h:216
static int Abc_NtkRetimeTiming(Abc_Ntk_t *pNtk, int fForward, Vec_Ptr_t *vCritical)
Definition: retDelay.c:172
#define assert(ex)
Definition: util_old.h:213
static void Vec_PtrClear(Vec_Ptr_t *p)
Definition: vecPtr.h:545
void Abc_NtkRetimeTranferFromCopy(Abc_Ntk_t *pNtk)
Definition: retInit.c:188
#define Abc_NtkForEachPo(pNtk, pPo, i)
Definition: abc.h:517
static void Vec_IntFree(Vec_Int_t *p)
Definition: bblif.c:235
#define Vec_PtrForEachEntry(Type, vVec, pEntry, i)
MACRO DEFINITIONS ///.
Definition: vecPtr.h:55
#define Abc_NtkForEachObj(pNtk, pObj, i)
ITERATORS ///.
Definition: abc.h:446
#define Abc_NtkForEachPi(pNtk, pPi, i)
Definition: abc.h:513
static void Abc_NodeSetTravIdCurrent(Abc_Obj_t *p)
Definition: abc.h:409
static void Vec_PtrFree(Vec_Ptr_t *p)
Definition: vecPtr.h:223