abc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
nwkFanio.c
Go to the documentation of this file.
1 /**CFile****************************************************************
2 
3  FileName [nwkFanio.c]
4 
5  SystemName [ABC: Logic synthesis and verification system.]
6 
7  PackageName [Logic network representation.]
8 
9  Synopsis [Manipulation of fanins/fanouts.]
10 
11  Author [Alan Mishchenko]
12 
13  Affiliation [UC Berkeley]
14 
15  Date [Ver. 1.0. Started - June 20, 2005.]
16 
17  Revision [$Id: nwkFanio.c,v 1.00 2005/06/20 00:00:00 alanmi Exp $]
18 
19 ***********************************************************************/
20 
21 #include "nwk.h"
22 
24 
25 
26 ////////////////////////////////////////////////////////////////////////
27 /// DECLARATIONS ///
28 ////////////////////////////////////////////////////////////////////////
29 
30 ////////////////////////////////////////////////////////////////////////
31 /// FUNCTION DEFINITIONS ///
32 ////////////////////////////////////////////////////////////////////////
33 
34 /**Function*************************************************************
35 
36  Synopsis [Collects fanins of the node.]
37 
38  Description []
39 
40  SideEffects []
41 
42  SeeAlso []
43 
44 ***********************************************************************/
45 void Nwk_ObjCollectFanins( Nwk_Obj_t * pNode, Vec_Ptr_t * vNodes )
46 {
47  Nwk_Obj_t * pFanin;
48  int i;
49  Vec_PtrClear(vNodes);
50  Nwk_ObjForEachFanin( pNode, pFanin, i )
51  Vec_PtrPush( vNodes, pFanin );
52 }
53 
54 /**Function*************************************************************
55 
56  Synopsis [Collects fanouts of the node.]
57 
58  Description []
59 
60  SideEffects []
61 
62  SeeAlso []
63 
64 ***********************************************************************/
65 void Nwk_ObjCollectFanouts( Nwk_Obj_t * pNode, Vec_Ptr_t * vNodes )
66 {
67  Nwk_Obj_t * pFanout;
68  int i;
69  Vec_PtrClear(vNodes);
70  Nwk_ObjForEachFanout( pNode, pFanout, i )
71  Vec_PtrPush( vNodes, pFanout );
72 }
73 
74 /**Function*************************************************************
75 
76  Synopsis [Returns the number of the fanin of the node.]
77 
78  Description []
79 
80  SideEffects []
81 
82  SeeAlso []
83 
84 ***********************************************************************/
85 int Nwk_ObjFindFanin( Nwk_Obj_t * pObj, Nwk_Obj_t * pFanin )
86 {
87  Nwk_Obj_t * pTemp;
88  int i;
89  Nwk_ObjForEachFanin( pObj, pTemp, i )
90  if ( pTemp == pFanin )
91  return i;
92  return -1;
93 }
94 
95 /**Function*************************************************************
96 
97  Synopsis [Returns the number of the fanout of the node.]
98 
99  Description []
100 
101  SideEffects []
102 
103  SeeAlso []
104 
105 ***********************************************************************/
106 int Nwk_ObjFindFanout( Nwk_Obj_t * pObj, Nwk_Obj_t * pFanout )
107 {
108  Nwk_Obj_t * pTemp;
109  int i;
110  Nwk_ObjForEachFanout( pObj, pTemp, i )
111  if ( pTemp == pFanout )
112  return i;
113  return -1;
114 }
115 
116 /**Function*************************************************************
117 
118  Synopsis [Returns 1 if the node has to be reallocated.]
119 
120  Description []
121 
122  SideEffects []
123 
124  SeeAlso []
125 
126 ***********************************************************************/
127 static inline int Nwk_ObjReallocIsNeeded( Nwk_Obj_t * pObj )
128 {
129  return pObj->nFanins + pObj->nFanouts == pObj->nFanioAlloc;
130 }
131 
132 /**Function*************************************************************
133 
134  Synopsis [Reallocates the object.]
135 
136  Description []
137 
138  SideEffects []
139 
140  SeeAlso []
141 
142 ***********************************************************************/
144 {
145  Nwk_Obj_t ** pFanioOld = pObj->pFanio;
147  pObj->pFanio = (Nwk_Obj_t **)Aig_MmFlexEntryFetch( pObj->pMan->pMemObjs, 2 * pObj->nFanioAlloc * sizeof(Nwk_Obj_t *) );
148  memmove( pObj->pFanio, pFanioOld, pObj->nFanioAlloc * sizeof(Nwk_Obj_t *) );
149  pObj->nFanioAlloc *= 2;
150  pObj->pMan->nRealloced++;
151  return NULL;
152 }
153 
154 /**Function*************************************************************
155 
156  Synopsis [Creates fanout/fanin relationship between the nodes.]
157 
158  Description []
159 
160  SideEffects []
161 
162  SeeAlso []
163 
164 ***********************************************************************/
165 void Nwk_ObjAddFanin( Nwk_Obj_t * pObj, Nwk_Obj_t * pFanin )
166 {
167  int i;
168  assert( pObj->pMan == pFanin->pMan );
169  assert( pObj->Id >= 0 && pFanin->Id >= 0 );
170  if ( Nwk_ObjReallocIsNeeded(pObj) )
171  Nwk_ManReallocNode( pObj );
172  if ( Nwk_ObjReallocIsNeeded(pFanin) )
173  Nwk_ManReallocNode( pFanin );
174  for ( i = pObj->nFanins + pObj->nFanouts; i > pObj->nFanins; i-- )
175  pObj->pFanio[i] = pObj->pFanio[i-1];
176  pObj->pFanio[pObj->nFanins++] = pFanin;
177  pFanin->pFanio[pFanin->nFanins + pFanin->nFanouts++] = pObj;
178  pObj->Level = Abc_MaxInt( pObj->Level, pFanin->Level + Nwk_ObjIsNode(pObj) );
179 }
180 
181 /**Function*************************************************************
182 
183  Synopsis [Removes fanout/fanin relationship between the nodes.]
184 
185  Description []
186 
187  SideEffects []
188 
189  SeeAlso []
190 
191 ***********************************************************************/
192 void Nwk_ObjDeleteFanin( Nwk_Obj_t * pObj, Nwk_Obj_t * pFanin )
193 {
194  int i, k, Limit, fFound;
195  // remove pFanin from the fanin list of pObj
196  Limit = pObj->nFanins + pObj->nFanouts;
197  fFound = 0;
198  for ( k = i = 0; i < Limit; i++ )
199  if ( fFound || pObj->pFanio[i] != pFanin )
200  pObj->pFanio[k++] = pObj->pFanio[i];
201  else
202  fFound = 1;
203  assert( i == k + 1 ); // if it fails, likely because of duplicated fanin
204  pObj->nFanins--;
205  // remove pObj from the fanout list of pFanin
206  Limit = pFanin->nFanins + pFanin->nFanouts;
207  fFound = 0;
208  for ( k = i = pFanin->nFanins; i < Limit; i++ )
209  if ( fFound || pFanin->pFanio[i] != pObj )
210  pFanin->pFanio[k++] = pFanin->pFanio[i];
211  else
212  fFound = 1;
213  assert( i == k + 1 ); // if it fails, likely because of duplicated fanout
214  pFanin->nFanouts--;
215 }
216 
217 /**Function*************************************************************
218 
219  Synopsis [Replaces a fanin of the node.]
220 
221  Description [The node is pObj. An old fanin of this node (pFaninOld) has to be
222  replaced by a new fanin (pFaninNew). Assumes that the node and the old fanin
223  are not complemented. The new fanin can be complemented. In this case, the
224  polarity of the new fanin will change, compared to the polarity of the old fanin.]
225 
226  SideEffects []
227 
228  SeeAlso []
229 
230 ***********************************************************************/
231 void Nwk_ObjPatchFanin( Nwk_Obj_t * pObj, Nwk_Obj_t * pFaninOld, Nwk_Obj_t * pFaninNew )
232 {
233  int i, k, iFanin, Limit;
234  assert( pFaninOld != pFaninNew );
235  assert( pObj != pFaninOld );
236  assert( pObj != pFaninNew );
237  assert( pObj->pMan == pFaninOld->pMan );
238  assert( pObj->pMan == pFaninNew->pMan );
239  // update the fanin
240  iFanin = Nwk_ObjFindFanin( pObj, pFaninOld );
241  if ( iFanin == -1 )
242  {
243  printf( "Nwk_ObjPatchFanin(); Error! Node %d is not among", pFaninOld->Id );
244  printf( " the fanins of node %d...\n", pObj->Id );
245  return;
246  }
247  pObj->pFanio[iFanin] = pFaninNew;
248  // remove pObj from the fanout list of pFaninOld
249  Limit = pFaninOld->nFanins + pFaninOld->nFanouts;
250  for ( k = i = pFaninOld->nFanins; i < Limit; i++ )
251  if ( pFaninOld->pFanio[i] != pObj )
252  pFaninOld->pFanio[k++] = pFaninOld->pFanio[i];
253  pFaninOld->nFanouts--;
254  // add pObj to the fanout list of pFaninNew
255  if ( Nwk_ObjReallocIsNeeded(pFaninNew) )
256  Nwk_ManReallocNode( pFaninNew );
257  pFaninNew->pFanio[pFaninNew->nFanins + pFaninNew->nFanouts++] = pObj;
258 }
259 
260 
261 /**Function*************************************************************
262 
263  Synopsis [Transfers fanout from the old node to the new node.]
264 
265  Description []
266 
267  SideEffects []
268 
269  SeeAlso []
270 
271 ***********************************************************************/
272 void Nwk_ObjTransferFanout( Nwk_Obj_t * pNodeFrom, Nwk_Obj_t * pNodeTo )
273 {
274  Vec_Ptr_t * vFanouts = pNodeFrom->pMan->vTemp;
275  Nwk_Obj_t * pTemp;
276  int nFanoutsOld, i;
277  assert( !Nwk_ObjIsCo(pNodeFrom) && !Nwk_ObjIsCo(pNodeTo) );
278  assert( pNodeFrom->pMan == pNodeTo->pMan );
279  assert( pNodeFrom != pNodeTo );
280  assert( Nwk_ObjFanoutNum(pNodeFrom) > 0 );
281  // get the fanouts of the old node
282  nFanoutsOld = Nwk_ObjFanoutNum(pNodeTo);
283  Nwk_ObjCollectFanouts( pNodeFrom, vFanouts );
284  // patch the fanin of each of them
285  Vec_PtrForEachEntry( Nwk_Obj_t *, vFanouts, pTemp, i )
286  Nwk_ObjPatchFanin( pTemp, pNodeFrom, pNodeTo );
287  assert( Nwk_ObjFanoutNum(pNodeFrom) == 0 );
288  assert( Nwk_ObjFanoutNum(pNodeTo) == nFanoutsOld + Vec_PtrSize(vFanouts) );
289 }
290 
291 /**Function*************************************************************
292 
293  Synopsis [Replaces the node by a new node.]
294 
295  Description []
296 
297  SideEffects []
298 
299  SeeAlso []
300 
301 ***********************************************************************/
302 void Nwk_ObjReplace( Nwk_Obj_t * pNodeOld, Nwk_Obj_t * pNodeNew )
303 {
304  assert( pNodeOld->pMan == pNodeNew->pMan );
305  assert( pNodeOld != pNodeNew );
306  assert( Nwk_ObjFanoutNum(pNodeOld) > 0 );
307  // transfer the fanouts to the old node
308  Nwk_ObjTransferFanout( pNodeOld, pNodeNew );
309  // remove the old node
310  Nwk_ManDeleteNode_rec( pNodeOld );
311 }
312 
313 
314 ////////////////////////////////////////////////////////////////////////
315 /// END OF FILE ///
316 ////////////////////////////////////////////////////////////////////////
317 
318 
320 
static Nwk_Obj_t * Nwk_ManReallocNode(Nwk_Obj_t *pObj)
Definition: nwkFanio.c:143
void Nwk_ObjReplace(Nwk_Obj_t *pNodeOld, Nwk_Obj_t *pNodeNew)
Definition: nwkFanio.c:302
typedefABC_NAMESPACE_HEADER_START struct Vec_Ptr_t_ Vec_Ptr_t
INCLUDES ///.
Definition: vecPtr.h:42
ABC_DLL void Nwk_ManDeleteNode_rec(Nwk_Obj_t *pObj)
Definition: nwkObj.c:183
int Nwk_ObjFindFanin(Nwk_Obj_t *pObj, Nwk_Obj_t *pFanin)
Definition: nwkFanio.c:85
static int Nwk_ObjFanoutNum(Nwk_Obj_t *p)
Definition: nwk.h:138
typedefABC_NAMESPACE_HEADER_START struct Nwk_Obj_t_ Nwk_Obj_t
INCLUDES ///.
Definition: nwk.h:49
void Nwk_ObjAddFanin(Nwk_Obj_t *pObj, Nwk_Obj_t *pFanin)
Definition: nwkFanio.c:165
char * Aig_MmFlexEntryFetch(Aig_MmFlex_t *p, int nBytes)
Definition: aigMem.c:366
#define Nwk_ObjForEachFanout(pObj, pFanout, i)
Definition: nwk.h:201
static void Vec_PtrPush(Vec_Ptr_t *p, void *Entry)
Definition: vecPtr.h:606
static int Abc_MaxInt(int a, int b)
Definition: abc_global.h:238
static int Vec_PtrSize(Vec_Ptr_t *p)
Definition: vecPtr.h:295
char * memmove()
int Nwk_ObjFindFanout(Nwk_Obj_t *pObj, Nwk_Obj_t *pFanout)
Definition: nwkFanio.c:106
static int Nwk_ObjIsCo(Nwk_Obj_t *p)
Definition: nwk.h:147
ABC_NAMESPACE_IMPL_START void Nwk_ObjCollectFanins(Nwk_Obj_t *pNode, Vec_Ptr_t *vNodes)
DECLARATIONS ///.
Definition: nwkFanio.c:45
#define ABC_NAMESPACE_IMPL_END
Definition: abc_global.h:108
void Nwk_ObjCollectFanouts(Nwk_Obj_t *pNode, Vec_Ptr_t *vNodes)
Definition: nwkFanio.c:65
static int Nwk_ObjIsNode(Nwk_Obj_t *p)
Definition: nwk.h:148
#define ABC_NAMESPACE_IMPL_START
Definition: abc_global.h:107
void Nwk_ObjPatchFanin(Nwk_Obj_t *pObj, Nwk_Obj_t *pFaninOld, Nwk_Obj_t *pFaninNew)
Definition: nwkFanio.c:231
#define Nwk_ObjForEachFanin(pObj, pFanin, i)
Definition: nwk.h:199
void Nwk_ObjDeleteFanin(Nwk_Obj_t *pObj, Nwk_Obj_t *pFanin)
Definition: nwkFanio.c:192
#define assert(ex)
Definition: util_old.h:213
static void Vec_PtrClear(Vec_Ptr_t *p)
Definition: vecPtr.h:545
#define Vec_PtrForEachEntry(Type, vVec, pEntry, i)
MACRO DEFINITIONS ///.
Definition: vecPtr.h:55
void Nwk_ObjTransferFanout(Nwk_Obj_t *pNodeFrom, Nwk_Obj_t *pNodeTo)
Definition: nwkFanio.c:272
static int Nwk_ObjReallocIsNeeded(Nwk_Obj_t *pObj)
Definition: nwkFanio.c:127