abc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
giaRetime.c
Go to the documentation of this file.
1 /**CFile****************************************************************
2 
3  FileName [giaRetime.c]
4 
5  SystemName [ABC: Logic synthesis and verification system.]
6 
7  PackageName [Scalable AIG package.]
8 
9  Synopsis [Performs most-forward retiming for AIG with flop classes.]
10 
11  Author [Alan Mishchenko]
12 
13  Affiliation [UC Berkeley]
14 
15  Date [Ver. 1.0. Started - June 20, 2005.]
16 
17  Revision [$Id: giaRetime.c,v 1.00 2005/06/20 00:00:00 alanmi Exp $]
18 
19 ***********************************************************************/
20 
21 #include "gia.h"
22 
24 
25 
26 ////////////////////////////////////////////////////////////////////////
27 /// DECLARATIONS ///
28 ////////////////////////////////////////////////////////////////////////
29 
30 ////////////////////////////////////////////////////////////////////////
31 /// FUNCTION DEFINITIONS ///
32 ////////////////////////////////////////////////////////////////////////
33 
34 /**Function*************************************************************
35 
36  Synopsis [Marks objects reachables from Const0 and PIs/
37 
38  Description []
39 
40  SideEffects []
41 
42  SeeAlso []
43 
44 ***********************************************************************/
46 {
47  if ( Gia_ObjIsTravIdCurrent(p, pObj) )
48  return pObj->fMark0;
49  Gia_ObjSetTravIdCurrent(p, pObj);
50  assert( pObj->fMark0 == 0 );
51  if ( Gia_ObjIsPi(p, pObj) || Gia_ObjIsConst0(pObj) )
52  return pObj->fMark0 = 1;
53  if ( Gia_ObjIsCo(pObj) )
54  return pObj->fMark0 = Gia_ManMarkAutonomous_rec( p, Gia_ObjFanin0(pObj) );
55  if ( Gia_ObjIsCi(pObj) )
56  return pObj->fMark0 = Gia_ManMarkAutonomous_rec( p, Gia_ObjRoToRi(p, pObj) );
57  assert( Gia_ObjIsAnd(pObj) );
58  if ( Gia_ManMarkAutonomous_rec( p, Gia_ObjFanin0(pObj) ) )
59  return pObj->fMark0 = 1;
60  return pObj->fMark0 = Gia_ManMarkAutonomous_rec( p, Gia_ObjFanin1(pObj) );
61 }
62 
63 /**Function*************************************************************
64 
65  Synopsis [Marks with current trav ROs reachable from Const0 and PIs.]
66 
67  Description []
68 
69  SideEffects []
70 
71  SeeAlso []
72 
73 ***********************************************************************/
75 {
76  Gia_Obj_t * pObj;
77  int i;
78  Gia_ManCleanMark0( p );
80  Gia_ManForEachRo( p, pObj, i )
81  Gia_ManMarkAutonomous_rec( p, pObj );
83  Gia_ManForEachRo( p, pObj, i )
84  if ( pObj->fMark0 )
85  Gia_ObjSetTravIdCurrent( p, pObj );
86  Gia_ManCleanMark0( p );
87 }
88 
89 /**Function*************************************************************
90 
91  Synopsis [Duplicates the AIG recursively.]
92 
93  Description []
94 
95  SideEffects []
96 
97  SeeAlso []
98 
99 ***********************************************************************/
101 {
102  if ( ~pObj->Value )
103  return;
104  assert( Gia_ObjIsAnd(pObj) );
105  Gia_ManRetimeDup_rec( pNew, Gia_ObjFanin0(pObj) );
106  Gia_ManRetimeDup_rec( pNew, Gia_ObjFanin1(pObj) );
107  pObj->Value = Gia_ManHashAnd( pNew, Gia_ObjFanin0Copy(pObj), Gia_ObjFanin1Copy(pObj) );
108 }
109 
110 /**Function*************************************************************
111 
112  Synopsis [Duplicates the AIG while retiming the registers to the cut.]
113 
114  Description []
115 
116  SideEffects []
117 
118  SeeAlso []
119 
120 ***********************************************************************/
122 {
123  Gia_Man_t * pNew, * pTemp;
124  Gia_Obj_t * pObj, * pObjRi, * pObjRo;
125  int i;
126  // create the new manager
127  pNew = Gia_ManStart( Gia_ManObjNum(p) );
128  pNew->pName = Abc_UtilStrsav( p->pName );
129  pNew->pSpec = Abc_UtilStrsav( p->pSpec );
130  Gia_ManHashAlloc( pNew );
131  // create the true PIs
132  Gia_ManFillValue( p );
133  Gia_ManSetPhase( p );
134  Gia_ManConst0(p)->Value = 0;
135  Gia_ManForEachPi( p, pObj, i )
136  pObj->Value = Gia_ManAppendCi( pNew );
137  // create the registers
138  Vec_PtrForEachEntry( Gia_Obj_t *, vCut, pObj, i )
139  pObj->Value = Abc_LitNotCond( Gia_ManAppendCi(pNew), pObj->fPhase );
140  // duplicate logic above the cut
141  Gia_ManForEachCo( p, pObj, i )
142  Gia_ManRetimeDup_rec( pNew, Gia_ObjFanin0(pObj) );
143  // create the true POs
144  Gia_ManForEachPo( p, pObj, i )
145  Gia_ManAppendCo( pNew, Gia_ObjFanin0Copy(pObj) );
146  // remember value in LI
147  Gia_ManForEachRi( p, pObj, i )
148  pObj->Value = Gia_ObjFanin0Copy(pObj);
149  // transfer values from the LIs to the LOs
150  Gia_ManForEachRiRo( p, pObjRi, pObjRo, i )
151  pObjRo->Value = pObjRi->Value;
152  // erase the data values on the internal nodes of the cut
153  Vec_PtrForEachEntry( Gia_Obj_t *, vCut, pObj, i )
154  if ( Gia_ObjIsAnd(pObj) )
155  pObj->Value = ~0;
156  // duplicate logic below the cut
157  Vec_PtrForEachEntry( Gia_Obj_t *, vCut, pObj, i )
158  {
159  Gia_ManRetimeDup_rec( pNew, pObj );
160  Gia_ManAppendCo( pNew, Abc_LitNotCond( pObj->Value, pObj->fPhase ) );
161  }
162  Gia_ManHashStop( pNew );
163  Gia_ManSetRegNum( pNew, Vec_PtrSize(vCut) );
164  pNew = Gia_ManCleanup( pTemp = pNew );
165  Gia_ManStop( pTemp );
166  return pNew;
167 }
168 
169 /**Function*************************************************************
170 
171  Synopsis [Derives the cut for forward retiming.]
172 
173  Description [Assumes topological ordering of the nodes.]
174 
175  SideEffects []
176 
177  SeeAlso []
178 
179 ***********************************************************************/
180 Gia_Man_t * Gia_ManRetimeForwardOne( Gia_Man_t * p, int * pnRegFixed, int * pnRegMoves )
181 {
182  Vec_Int_t * vFlopClasses = NULL;
183  Vec_Int_t * vObjClasses = NULL;
184  Gia_Man_t * pNew;
185  Vec_Ptr_t * vCut;
186  Gia_Obj_t * pObj;
187  int i;
188  if ( p->vFlopClasses )
189  {
190 // printf( "Performing retiming with register classes.\n" );
191  vObjClasses = Vec_IntAlloc( Gia_ManObjNum(p) );
192  for ( i = 0; i < Gia_ManObjNum(p); i++ )
193  Vec_IntPush( vObjClasses, -1 );
194  Gia_ManForEachRo( p, pObj, i )
195  Vec_IntWriteEntry( vObjClasses, Gia_ObjId(p, pObj), Vec_IntEntry(p->vFlopClasses, i) );
196  vFlopClasses = Vec_IntAlloc( Gia_ManRegNum(p) );
197  }
198  // mark the retimable nodes
201  // mark the retimable registers with the fresh trav ID
203  *pnRegFixed = 0;
204  Gia_ManForEachRo( p, pObj, i )
205  if ( Gia_ObjIsTravIdPrevious(p, pObj) )
206  Gia_ObjSetTravIdCurrent(p, pObj);
207  else
208  (*pnRegFixed)++;
209  // mark all the nodes that can be retimed forward
210  *pnRegMoves = 0;
211  Gia_ManForEachAnd( p, pObj, i )
213  {
214  if ( vObjClasses && Vec_IntEntry(vObjClasses, Gia_ObjFaninId0(pObj, i)) != Vec_IntEntry(vObjClasses, Gia_ObjFaninId1(pObj, i)) )
215  continue;
216  if ( vObjClasses )
217  Vec_IntWriteEntry( vObjClasses, Gia_ObjId(p, pObj), Vec_IntEntry(vObjClasses, Gia_ObjFaninId0(pObj, i)) );
218  Gia_ObjSetTravIdCurrent(p, pObj);
219  (*pnRegMoves)++;
220  }
221  // mark the remaining registers
222  Gia_ManForEachRo( p, pObj, i )
223  Gia_ObjSetTravIdCurrent(p, pObj);
224  // find the cut (all such marked objects that fanout into unmarked nodes)
225  vCut = Vec_PtrAlloc( 1000 );
227  Gia_ManForEachObj( p, pObj, i )
228  {
229  if ( Gia_ObjIsTravIdPrevious(p, pObj) )
230  continue;
231  if ( (Gia_ObjIsCo(pObj) || Gia_ObjIsAnd(pObj)) && Gia_ObjIsTravIdPrevious(p, Gia_ObjFanin0(pObj)) )
232  {
233  if ( vFlopClasses )
234  Vec_IntPush( vFlopClasses, Vec_IntEntry(vObjClasses, Gia_ObjFaninId0(pObj, i)) );
235  Vec_PtrPush( vCut, Gia_ObjFanin0(pObj) );
237  }
238  if ( Gia_ObjIsAnd(pObj) && Gia_ObjIsTravIdPrevious(p, Gia_ObjFanin1(pObj)) )
239  {
240  if ( vFlopClasses )
241  Vec_IntPush( vFlopClasses, Vec_IntEntry(vObjClasses, Gia_ObjFaninId1(pObj, i)) );
242  Vec_PtrPush( vCut, Gia_ObjFanin1(pObj) );
244  }
245  }
246  assert( vFlopClasses == NULL || Vec_IntSize(vFlopClasses) == Vec_PtrSize(vCut) );
247  // finally derive the new manager
248  pNew = Gia_ManRetimeDupForward( p, vCut );
249  Vec_PtrFree( vCut );
250  if ( vObjClasses )
251  Vec_IntFree( vObjClasses );
252  pNew->vFlopClasses = vFlopClasses;
253  return pNew;
254 }
255 
256 /**Function*************************************************************
257 
258  Synopsis [Derives the cut for forward retiming.]
259 
260  Description [Assumes topological ordering of the nodes.]
261 
262  SideEffects []
263 
264  SeeAlso []
265 
266 ***********************************************************************/
267 Gia_Man_t * Gia_ManRetimeForward( Gia_Man_t * p, int nMaxIters, int fVerbose )
268 {
269  Gia_Man_t * pNew, * pTemp;
270  int i, nRegFixed, nRegMoves = 1;
271  abctime clk;
272  pNew = p;
273  for ( i = 0; i < nMaxIters && nRegMoves > 0; i++ )
274  {
275  clk = Abc_Clock();
276  pNew = Gia_ManRetimeForwardOne( pTemp = pNew, &nRegFixed, &nRegMoves );
277  if ( fVerbose )
278  {
279  printf( "%2d : And = %6d. Reg = %5d. Unret = %5d. Move = %6d. ",
280  i + 1, Gia_ManAndNum(pTemp), Gia_ManRegNum(pTemp), nRegFixed, nRegMoves );
281  ABC_PRT( "Time", Abc_Clock() - clk );
282  }
283  if ( pTemp != p )
284  Gia_ManStop( pTemp );
285  }
286 /*
287  clk = Abc_Clock();
288  pNew = Gia_ManReduceLaches( pNew, fVerbose );
289  if ( fVerbose )
290  {
291  ABC_PRT( "Register sharing time", Abc_Clock() - clk );
292  }
293 */
294  return pNew;
295 }
296 
297 
298 ////////////////////////////////////////////////////////////////////////
299 /// END OF FILE ///
300 ////////////////////////////////////////////////////////////////////////
301 
302 
304 
typedefABC_NAMESPACE_HEADER_START struct Vec_Ptr_t_ Vec_Ptr_t
INCLUDES ///.
Definition: vecPtr.h:42
static int Gia_ObjIsTravIdPrevious(Gia_Man_t *p, Gia_Obj_t *pObj)
Definition: gia.h:534
Gia_Man_t * Gia_ManRetimeForwardOne(Gia_Man_t *p, int *pnRegFixed, int *pnRegMoves)
Definition: giaRetime.c:180
void Gia_ManStop(Gia_Man_t *p)
Definition: giaMan.c:77
#define Gia_ManForEachCo(p, pObj, i)
Definition: gia.h:1022
static int Gia_ManAppendCo(Gia_Man_t *p, int iLit0)
Definition: gia.h:703
static Llb_Mgr_t * p
Definition: llb3Image.c:950
typedefABC_NAMESPACE_IMPL_START struct Vec_Int_t_ Vec_Int_t
DECLARATIONS ///.
Definition: bblif.c:37
static int Gia_ObjIsTravIdCurrent(Gia_Man_t *p, Gia_Obj_t *pObj)
Definition: gia.h:533
void Gia_ManRetimeDup_rec(Gia_Man_t *pNew, Gia_Obj_t *pObj)
Definition: giaRetime.c:100
static int Gia_ManAppendCi(Gia_Man_t *p)
Definition: gia.h:583
void Gia_ManCleanMark0(Gia_Man_t *p)
Definition: giaUtil.c:215
static int Gia_ObjIsConst0(Gia_Obj_t *pObj)
Definition: gia.h:430
Gia_Man_t * Gia_ManRetimeForward(Gia_Man_t *p, int nMaxIters, int fVerbose)
Definition: giaRetime.c:267
Vec_Int_t * vFlopClasses
Definition: gia.h:140
static void Vec_PtrPush(Vec_Ptr_t *p, void *Entry)
Definition: vecPtr.h:606
void Gia_ManSetRegNum(Gia_Man_t *p, int nRegs)
Definition: giaMan.c:628
static abctime Abc_Clock()
Definition: abc_global.h:279
static int Vec_PtrSize(Vec_Ptr_t *p)
Definition: vecPtr.h:295
static int Abc_LitNotCond(int Lit, int c)
Definition: abc_global.h:267
Definition: gia.h:75
Gia_Man_t * Gia_ManRetimeDupForward(Gia_Man_t *p, Vec_Ptr_t *vCut)
Definition: giaRetime.c:121
#define Gia_ManForEachRiRo(p, pObjRi, pObjRo, i)
Definition: gia.h:1042
static void Vec_IntWriteEntry(Vec_Int_t *p, int i, int Entry)
Definition: bblif.c:285
static int Gia_ManAndNum(Gia_Man_t *p)
Definition: gia.h:389
static Gia_Obj_t * Gia_ObjFanin0(Gia_Obj_t *pObj)
Definition: gia.h:454
char * pName
Definition: gia.h:97
static Vec_Int_t * Vec_IntAlloc(int nCap)
FUNCTION DEFINITIONS ///.
Definition: bblif.c:149
static int Gia_ObjFanin1Copy(Gia_Obj_t *pObj)
Definition: gia.h:482
static Gia_Obj_t * Gia_ObjRoToRi(Gia_Man_t *p, Gia_Obj_t *pObj)
Definition: gia.h:446
static int Vec_IntEntry(Vec_Int_t *p, int i)
Definition: bblif.c:268
static void Gia_ObjSetTravIdCurrent(Gia_Man_t *p, Gia_Obj_t *pObj)
Definition: gia.h:531
#define ABC_NAMESPACE_IMPL_END
Definition: abc_global.h:108
char * pSpec
Definition: gia.h:98
Gia_Man_t * Gia_ManStart(int nObjsMax)
DECLARATIONS ///.
Definition: giaMan.c:52
#define Gia_ManForEachAnd(p, pObj, i)
Definition: gia.h:1002
static void Vec_IntPush(Vec_Int_t *p, int Entry)
Definition: bblif.c:468
static int Gia_ObjId(Gia_Man_t *p, Gia_Obj_t *pObj)
Definition: gia.h:410
void Gia_ManFillValue(Gia_Man_t *p)
Definition: giaUtil.c:328
#define Gia_ManForEachPi(p, pObj, i)
Definition: gia.h:1034
static int Gia_ObjFanin0Copy(Gia_Obj_t *pObj)
Definition: gia.h:481
static int Gia_ObjIsCo(Gia_Obj_t *pObj)
Definition: gia.h:421
#define ABC_NAMESPACE_IMPL_START
Definition: abc_global.h:107
void Gia_ManMarkAutonomous(Gia_Man_t *p)
Definition: giaRetime.c:74
unsigned fPhase
Definition: gia.h:85
static int Vec_IntSize(Vec_Int_t *p)
Definition: bblif.c:252
static Vec_Ptr_t * Vec_PtrAlloc(int nCap)
FUNCTION DEFINITIONS ///.
Definition: vecPtr.h:83
unsigned fMark0
Definition: gia.h:79
Definition: gia.h:95
#define ABC_PRT(a, t)
Definition: abc_global.h:220
static int Gia_ObjIsAnd(Gia_Obj_t *pObj)
Definition: gia.h:422
#define Gia_ManForEachObj(p, pObj, i)
MACRO DEFINITIONS ///.
Definition: gia.h:984
static Gia_Obj_t * Gia_ManConst0(Gia_Man_t *p)
Definition: gia.h:400
static Gia_Obj_t * Gia_ObjFanin1(Gia_Obj_t *pObj)
Definition: gia.h:455
ABC_NAMESPACE_IMPL_START int Gia_ManMarkAutonomous_rec(Gia_Man_t *p, Gia_Obj_t *pObj)
DECLARATIONS ///.
Definition: giaRetime.c:45
#define Gia_ManForEachRo(p, pObj, i)
Definition: gia.h:1038
#define assert(ex)
Definition: util_old.h:213
void Gia_ManSetPhase(Gia_Man_t *p)
Definition: giaUtil.c:379
unsigned Value
Definition: gia.h:87
#define Gia_ManForEachRi(p, pObj, i)
Definition: gia.h:1040
void Gia_ManHashAlloc(Gia_Man_t *p)
Definition: giaHash.c:99
void Gia_ManIncrementTravId(Gia_Man_t *p)
Definition: giaUtil.c:149
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
ABC_INT64_T abctime
Definition: abc_global.h:278
static int Gia_ObjIsCi(Gia_Obj_t *pObj)
Definition: gia.h:420
Gia_Man_t * Gia_ManCleanup(Gia_Man_t *p)
Definition: giaScl.c:84
char * Abc_UtilStrsav(char *s)
Definition: starter.c:47
#define Gia_ManForEachPo(p, pObj, i)
Definition: gia.h:1036
static int Gia_ObjFaninId1(Gia_Obj_t *pObj, int ObjId)
Definition: gia.h:461
int Gia_ManHashAnd(Gia_Man_t *p, int iLit0, int iLit1)
Definition: giaHash.c:572
static int Gia_ManObjNum(Gia_Man_t *p)
Definition: gia.h:388
static void Vec_PtrFree(Vec_Ptr_t *p)
Definition: vecPtr.h:223
static int Gia_ObjFaninId0(Gia_Obj_t *pObj, int ObjId)
Definition: gia.h:460
void Gia_ManHashStop(Gia_Man_t *p)
Definition: giaHash.c:142
static int Gia_ObjIsPi(Gia_Man_t *p, Gia_Obj_t *pObj)
Definition: gia.h:441
static int Gia_ManRegNum(Gia_Man_t *p)
Definition: gia.h:387