abc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
ifSeq.c
Go to the documentation of this file.
1 /**CFile****************************************************************
2 
3  FileName [ifSeq.c]
4 
5  SystemName [ABC: Logic synthesis and verification system.]
6 
7  PackageName [FPGA mapping based on priority cuts.]
8 
9  Synopsis [Sequential mapping.]
10 
11  Author [Alan Mishchenko]
12 
13  Affiliation [UC Berkeley]
14 
15  Date [Ver. 1.0. Started - November 21, 2006.]
16 
17  Revision [$Id: ifSeq.c,v 1.00 2006/11/21 00:00:00 alanmi Exp $]
18 
19 ***********************************************************************/
20 
21 #include "if.h"
22 
24 
25 
26 ////////////////////////////////////////////////////////////////////////
27 /// DECLARATIONS ///
28 ////////////////////////////////////////////////////////////////////////
29 
30 extern abctime s_MappingTime;
31 
32 ////////////////////////////////////////////////////////////////////////
33 /// FUNCTION DEFINITIONS ///
34 ////////////////////////////////////////////////////////////////////////
35 
36 /**Function*************************************************************
37 
38  Synopsis [Prepares for sequential mapping by linking the latches.]
39 
40  Description []
41 
42  SideEffects []
43 
44  SeeAlso []
45 
46 ***********************************************************************/
48 {
49  If_Obj_t * pObjLi, * pObjLo;
50  int i;
51 
52  // link the latch outputs (CIs) directly to the drivers of latch inputs (COs)
53  for ( i = 0; i < p->pPars->nLatchesCi; i++ )
54  {
55  pObjLi = If_ManLi( p, i );
56  pObjLo = If_ManLo( p, i );
57  pObjLo->pFanin0 = If_ObjFanin0( pObjLi );
58  pObjLo->fCompl0 = If_ObjFaninC0( pObjLi );
59  }
60 }
61 
62 /**Function*************************************************************
63 
64  Synopsis [Collects latches in the topological order.]
65 
66  Description []
67 
68  SideEffects []
69 
70  SeeAlso []
71 
72 ***********************************************************************/
73 void If_ManCollectLatches_rec( If_Obj_t * pObj, Vec_Ptr_t * vLatches )
74 {
75  if ( !If_ObjIsLatch(pObj) )
76  return;
77  if ( pObj->fMark )
78  return;
79  pObj->fMark = 1;
80  If_ManCollectLatches_rec( pObj->pFanin0, vLatches );
81  Vec_PtrPush( vLatches, pObj );
82 }
83 
84 /**Function*************************************************************
85 
86  Synopsis [Collects latches in the topological order.]
87 
88  Description []
89 
90  SideEffects []
91 
92  SeeAlso []
93 
94 ***********************************************************************/
96 {
97  Vec_Ptr_t * vLatches;
98  If_Obj_t * pObj;
99  int i;
100  // collect latches
101  vLatches = Vec_PtrAlloc( p->pPars->nLatchesCi );
102  If_ManForEachLatchOutput( p, pObj, i )
103  If_ManCollectLatches_rec( pObj, vLatches );
104  // clean marks
105  Vec_PtrForEachEntry( If_Obj_t *, vLatches, pObj, i )
106  pObj->fMark = 0;
107  assert( Vec_PtrSize(vLatches) == p->pPars->nLatchesCi );
108  return vLatches;
109 }
110 
111 /**Function*************************************************************
112 
113  Synopsis [Performs one pass of l-value computation over all nodes.]
114 
115  Description [Experimentally it was found that checking POs changes
116  is not enough to detect the convergence of l-values in the network.]
117 
118  SideEffects []
119 
120  SeeAlso []
121 
122 ***********************************************************************/
124 {
125  If_Obj_t * pObj;
126  int i;
127  abctime clk = Abc_Clock();
128  int fVeryVerbose = 0;
129  int fChange = 0;
130 
131  if ( nIter == 1 )
132  {
133  // if some latches depend on PIs, update their values
134  Vec_PtrForEachEntry( If_Obj_t *, p->vLatchOrder, pObj, i )
135  {
136  If_ObjSetLValue( pObj, If_ObjLValue(If_ObjFanin0(pObj)) - p->Period );
137  If_ObjSetArrTime( pObj, If_ObjLValue(pObj) );
138  }
139  }
140 
141  // map the internal nodes
142  p->nCutsMerged = 0;
143  If_ManForEachNode( p, pObj, i )
144  {
145  If_ObjPerformMappingAnd( p, pObj, 0, 0, 0 );
146  if ( pObj->fRepr )
147  If_ObjPerformMappingChoice( p, pObj, 0, 0 );
148  }
149 
150  // postprocess the mapping
151 //Abc_Print( 1, "Itereation %d: \n", nIter );
152  If_ManForEachNode( p, pObj, i )
153  {
154  // update the LValues stored separately
155  if ( If_ObjLValue(pObj) < If_ObjCutBest(pObj)->Delay - p->fEpsilon )
156  {
157  If_ObjSetLValue( pObj, If_ObjCutBest(pObj)->Delay );
158  fChange = 1;
159  }
160 //Abc_Print( 1, "%d ", (int)If_ObjLValue(pObj) );
161  // reset the visit counters
162  assert( pObj->nVisits == 0 );
163  pObj->nVisits = pObj->nVisitsCopy;
164  }
165 //Abc_Print( 1, "\n" );
166 
167  // propagate LValues over the registers
168  Vec_PtrForEachEntry( If_Obj_t *, p->vLatchOrder, pObj, i )
169  {
170  If_ObjSetLValue( pObj, If_ObjLValue(If_ObjFanin0(pObj)) - p->Period );
171  If_ObjSetArrTime( pObj, If_ObjLValue(pObj) );
172  }
173 
174  // compute area and delay
175  If_ManMarkMapping( p );
176  if ( fVeryVerbose )
177  {
178  p->RequiredGlo = If_ManDelayMax( p, 1 );
179 // p->AreaGlo = If_ManScanMapping(p);
180  Abc_Print( 1, "S%d: Fi = %6.2f. Del = %6.2f. Area = %8.2f. Cuts = %8d. ",
181  nIter, (float)p->Period, p->RequiredGlo, p->AreaGlo, p->nCutsMerged );
182  Abc_PrintTime( 1, "T", Abc_Clock() - clk );
183  }
184  return fChange;
185 }
186 
187 /**Function*************************************************************
188 
189  Synopsis [Returns 1 if retiming with this clock period is feasible.]
190 
191  Description []
192 
193  SideEffects []
194 
195  SeeAlso []
196 
197 ***********************************************************************/
199 {
200  If_Obj_t * pObj;
201  int i, c, fConverged;
202 // int fResetRefs = 0;
203  p->nAttempts++;
204 
205  // reset initial LValues (PIs to 0; others to -inf)
206  If_ManForEachObj( p, pObj, i )
207  {
208  If_ObjSetLValue( pObj, (float)-IF_INFINITY );
209  If_ObjSetArrTime( pObj, (float)-IF_INFINITY );
210  // undo any previous mapping, except for CIs
211  if ( If_ObjIsAnd(pObj) )
212  If_ObjCutBest(pObj)->nLeaves = 0;
213  }
214  pObj = If_ManConst1( p );
215  If_ObjSetLValue( pObj, (float)0.0 );
216  If_ObjSetArrTime( pObj, (float)0.0 );
217  If_ManForEachPi( p, pObj, i )
218  {
219  pObj = If_ManCi( p, i );
220  If_ObjSetLValue( pObj, (float)0.0 );
221  If_ObjSetArrTime( pObj, (float)0.0 );
222  }
223 
224  // update all values iteratively
225  fConverged = 0;
226  for ( c = 1; c <= p->nMaxIters; c++ )
227  {
228  if ( !If_ManPerformMappingRoundSeq( p, c ) )
229  {
230  p->RequiredGlo = If_ManDelayMax( p, 1 );
231  fConverged = 1;
232  break;
233  }
234  p->RequiredGlo = If_ManDelayMax( p, 1 );
235 //Abc_Print( 1, "Global = %d \n", (int)p->RequiredGlo );
236  if ( p->RequiredGlo > p->Period + p->fEpsilon )
237  break;
238  }
239 
240  // report the results
241  If_ManMarkMapping( p );
242  if ( p->pPars->fVerbose )
243  {
244 // p->AreaGlo = If_ManScanMapping(p);
245  Abc_Print( 1, "Attempt = %2d. Iters = %3d. Area = %10.2f. Fi = %6.2f. ", p->nAttempts, c, p->AreaGlo, (float)p->Period );
246  if ( fConverged )
247  Abc_Print( 1, " Feasible" );
248  else if ( c > p->nMaxIters )
249  Abc_Print( 1, "Infeasible (timeout)" );
250  else
251  Abc_Print( 1, "Infeasible" );
252  Abc_Print( 1, "\n" );
253  }
254  return fConverged;
255 }
256 
257 
258 /**Function*************************************************************
259 
260  Synopsis [Performs binary search for the optimal clock period.]
261 
262  Description [Assumes that FiMin is infeasible while FiMax is feasible.]
263 
264  SideEffects []
265 
266  SeeAlso []
267 
268 ***********************************************************************/
269 int If_ManBinarySearch_rec( If_Man_t * p, int FiMin, int FiMax )
270 {
271  assert( FiMin < FiMax );
272  if ( FiMin + 1 == FiMax )
273  return FiMax;
274  // compute the median
275  p->Period = FiMin + (FiMax - FiMin)/2;
276  if ( If_ManBinarySearchPeriod( p ) )
277  return If_ManBinarySearch_rec( p, FiMin, p->Period ); // Median is feasible
278  else
279  return If_ManBinarySearch_rec( p, p->Period, FiMax ); // Median is infeasible
280 }
281 
282 /**Function*************************************************************
283 
284  Synopsis [Performs sequential mapping.]
285 
286  Description []
287 
288  SideEffects []
289 
290  SeeAlso []
291 
292 ***********************************************************************/
294 {
295  If_Obj_t * pObjLi, * pObjLo, * pObj;
296  int i;
297  assert( 0 );
298 
299  // set arrival times
300  assert( p->pPars->pTimesArr != NULL );
301  If_ManForEachLatchOutput( p, pObjLo, i )
302  p->pPars->pTimesArr[i] = If_ObjLValue(pObjLo);
303 
304  // set the required times
305  assert( p->pPars->pTimesReq == NULL );
306  p->pPars->pTimesReq = ABC_ALLOC( float, If_ManCoNum(p) );
307  If_ManForEachPo( p, pObj, i )
308  p->pPars->pTimesReq[i] = p->RequiredGlo2;
309  If_ManForEachLatchInput( p, pObjLi, i )
310  p->pPars->pTimesReq[i] = If_ObjLValue(If_ObjFanin0(pObjLi));
311 
312  // undo previous mapping
313  If_ManForEachObj( p, pObj, i )
314  if ( If_ObjIsAnd(pObj) )
315  If_ObjCutBest(pObj)->nLeaves = 0;
316 
317  // map again combinationally
318 // p->pPars->fSeqMap = 0;
320 // p->pPars->fSeqMap = 1;
321 }
322 
323 /**Function*************************************************************
324 
325  Synopsis [Performs sequential mapping.]
326 
327  Description []
328 
329  SideEffects []
330 
331  SeeAlso []
332 
333 ***********************************************************************/
335 {
336  abctime clkTotal = Abc_Clock();
337  int PeriodBest;
338 
339  p->SortMode = 0;
340 
341  // perform combinational mapping to get the upper bound on the clock period
342  If_ManPerformMappingRound( p, 1, 0, 0, 1, NULL );
343  p->RequiredGlo = If_ManDelayMax( p, 0 );
344  p->RequiredGlo2 = p->RequiredGlo;
345 
346  // set direct linking of latches with their inputs
348 
349  // collect latches
351 
352  // set parameters
353  p->nCutsUsed = p->pPars->nCutsMax;
354  p->nAttempts = 0;
355  p->nMaxIters = 50;
356  p->Period = (int)p->RequiredGlo;
357 
358  // make sure the clock period works
359  if ( !If_ManBinarySearchPeriod( p ) )
360  {
361  Abc_Print( 1, "If_ManPerformMappingSeq(): The upper bound on the clock period cannot be computed.\n" );
362  return 0;
363  }
364 
365  // perform binary search
366  PeriodBest = If_ManBinarySearch_rec( p, 0, p->Period );
367 
368  // recompute the best l-values
369  if ( p->Period != PeriodBest )
370  {
371  p->Period = PeriodBest;
372  if ( !If_ManBinarySearchPeriod( p ) )
373  {
374  Abc_Print( 1, "If_ManPerformMappingSeq(): The final clock period cannot be confirmed.\n" );
375  return 0;
376  }
377  }
378 // if ( p->pPars->fVerbose )
379  {
380  Abc_Print( 1, "The best clock period is %3d. ", p->Period );
381  Abc_PrintTime( 1, "Time", Abc_Clock() - clkTotal );
382  }
383  p->RequiredGlo = (float)(PeriodBest);
384 
385  // postprocess it using combinational mapping
387  s_MappingTime = Abc_Clock() - clkTotal;
388  return 1;
389 }
390 
391 ////////////////////////////////////////////////////////////////////////
392 /// END OF FILE ///
393 ////////////////////////////////////////////////////////////////////////
394 
395 
397 
If_Obj_t * pFanin0
Definition: if.h:321
#define IF_INFINITY
Definition: if.h:56
#define If_ManForEachPi(p, pObj, i)
Definition: if.h:451
unsigned nLeaves
Definition: if.h:289
static int If_ObjIsLatch(If_Obj_t *pObj)
Definition: if.h:376
typedefABC_NAMESPACE_HEADER_START struct Vec_Ptr_t_ Vec_Ptr_t
INCLUDES ///.
Definition: vecPtr.h:42
static void If_ObjSetLValue(If_Obj_t *pObj, float LValue)
Definition: if.h:409
float RequiredGlo2
Definition: if.h:197
int nCutsUsed
Definition: if.h:201
#define If_ManForEachNode(p, pObj, i)
Definition: if.h:468
int nMaxIters
Definition: if.h:223
static Llb_Mgr_t * p
Definition: llb3Image.c:950
Definition: if.h:303
static int If_ObjIsAnd(If_Obj_t *pObj)
Definition: if.h:377
#define If_ManForEachLatchInput(p, pObj, i)
Definition: if.h:457
unsigned fRepr
Definition: if.h:309
int fVerbose
Definition: if.h:140
int If_ManBinarySearch_rec(If_Man_t *p, int FiMin, int FiMax)
Definition: ifSeq.c:269
static int If_ObjFaninC0(If_Obj_t *pObj)
Definition: if.h:382
void If_ObjPerformMappingAnd(If_Man_t *p, If_Obj_t *pObj, int Mode, int fPreprocess, int fFirst)
Definition: ifMap.c:95
int nCutsMerged
Definition: if.h:202
static If_Obj_t * If_ObjFanin0(If_Obj_t *pObj)
Definition: if.h:380
static void Vec_PtrPush(Vec_Ptr_t *p, void *Entry)
Definition: vecPtr.h:606
#define ABC_ALLOC(type, num)
Definition: abc_global.h:229
static If_Cut_t * If_ObjCutBest(If_Obj_t *pObj)
Definition: if.h:401
int If_ManPerformMappingRound(If_Man_t *p, int nCutsUsed, int Mode, int fPreprocess, int fFirst, char *pLabel)
Definition: ifMap.c:491
static abctime Abc_Clock()
Definition: abc_global.h:279
static int Vec_PtrSize(Vec_Ptr_t *p)
Definition: vecPtr.h:295
#define If_ManForEachPo(p, pObj, i)
Definition: if.h:454
int If_ManPerformMappingRoundSeq(If_Man_t *p, int nIter)
Definition: ifSeq.c:123
int nLatchesCi
Definition: if.h:152
static void Abc_PrintTime(int level, const char *pStr, abctime time)
Definition: abc_global.h:367
#define If_ManForEachObj(p, pObj, i)
Definition: if.h:462
float AreaGlo
Definition: if.h:198
int If_ManBinarySearchPeriod(If_Man_t *p)
Definition: ifSeq.c:198
void If_ObjPerformMappingChoice(If_Man_t *p, If_Obj_t *pObj, int Mode, int fPreprocess)
Definition: ifMap.c:407
float RequiredGlo
Definition: if.h:196
Definition: if.h:180
int If_ManPerformMappingComb(If_Man_t *p)
Definition: ifCore.c:104
#define ABC_NAMESPACE_IMPL_END
Definition: abc_global.h:108
static If_Obj_t * If_ManConst1(If_Man_t *p)
Definition: if.h:365
if(last==0)
Definition: sparse_int.h:34
float * pTimesArr
Definition: if.h:161
static If_Obj_t * If_ManCi(If_Man_t *p, int i)
Definition: if.h:366
static void If_ObjSetArrTime(If_Obj_t *pObj, float ArrTime)
Definition: if.h:406
float * pTimesReq
Definition: if.h:162
Vec_Ptr_t * If_ManCollectLatches(If_Man_t *p)
Definition: ifSeq.c:95
static void Abc_Print(int level, const char *format,...)
Definition: abc_global.h:313
int nAttempts
Definition: if.h:222
float fEpsilon
Definition: if.h:195
int nVisitsCopy
Definition: if.h:320
#define ABC_NAMESPACE_IMPL_START
Definition: abc_global.h:107
void If_ManCollectLatches_rec(If_Obj_t *pObj, Vec_Ptr_t *vLatches)
Definition: ifSeq.c:73
float If_ManDelayMax(If_Man_t *p, int fSeq)
Definition: ifTime.c:250
void If_ManPrepareMappingSeq(If_Man_t *p)
FUNCTION DEFINITIONS ///.
Definition: ifSeq.c:47
#define If_ManForEachLatchOutput(p, pObj, i)
Definition: if.h:459
ABC_NAMESPACE_IMPL_START abctime s_MappingTime
DECLARATIONS ///.
Definition: abcPrint.c:44
If_Par_t * pPars
Definition: if.h:184
static Vec_Ptr_t * Vec_PtrAlloc(int nCap)
FUNCTION DEFINITIONS ///.
Definition: vecPtr.h:83
int Period
Definition: if.h:224
void If_ManPerformMappingSeqPost(If_Man_t *p)
Definition: ifSeq.c:293
int nVisits
Definition: if.h:319
static If_Obj_t * If_ManLo(If_Man_t *p, int i)
Definition: if.h:369
#define assert(ex)
Definition: util_old.h:213
int nCutsMax
Definition: if.h:104
static int If_ManCoNum(If_Man_t *p)
Definition: if.h:361
int SortMode
Definition: if.h:205
Vec_Ptr_t * vLatchOrder
Definition: if.h:220
static float If_ObjLValue(If_Obj_t *pObj)
Definition: if.h:408
static If_Obj_t * If_ManLi(If_Man_t *p, int i)
Definition: if.h:368
#define Vec_PtrForEachEntry(Type, vVec, pEntry, i)
MACRO DEFINITIONS ///.
Definition: vecPtr.h:55
ABC_INT64_T abctime
Definition: abc_global.h:278
unsigned fCompl0
Definition: if.h:306
void If_ManMarkMapping(If_Man_t *p)
Definition: ifUtil.c:434
unsigned fMark
Definition: if.h:310
int If_ManPerformMappingSeq(If_Man_t *p)
Definition: ifSeq.c:334