summaryrefslogtreecommitdiffstats
path: root/src/temp/ivy/ivyFanout.c
blob: 2295516d0d3417358f020669e5bc74cf80fd01d5 (plain)
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
/**CFile****************************************************************

  FileName    [ivyFanout.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [And-Inverter Graph package.]

  Synopsis    [Representation of the fanouts.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

  Date        [Ver. 1.0. Started - May 11, 2006.]

  Revision    [$Id: ivyFanout.c,v 1.00 2006/05/11 00:00:00 alanmi Exp $]

***********************************************************************/

#include "ivy.h"

////////////////////////////////////////////////////////////////////////
///                        DECLARATIONS                              ///
////////////////////////////////////////////////////////////////////////

static inline int          Ivy_FanoutIsArray( void * p )    { return (int )(((unsigned)p) & 01);          }
static inline Vec_Ptr_t *  Ivy_FanoutGetArray( void * p )   { assert( Ivy_FanoutIsArray(p) );  return (Vec_Ptr_t *)((unsigned)(p) & ~01);  }
static inline Vec_Ptr_t *  Ivy_FanoutSetArray( void * p )   { assert( !Ivy_FanoutIsArray(p) ); return (Vec_Ptr_t *)((unsigned)(p) ^  01);  }

////////////////////////////////////////////////////////////////////////
///                     FUNCTION DEFINITIONS                         ///
////////////////////////////////////////////////////////////////////////

/**Function*************************************************************

  Synopsis    [Starts the fanout representation.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Ivy_ManStartFanout( Ivy_Man_t * p )
{
    Ivy_Obj_t * pObj;
    int i;
    assert( p->vFanouts == NULL );
    p->vFanouts = Vec_PtrStart( Ivy_ManObjIdMax(p) + 1000 );
    Ivy_ManForEachObj( p, pObj, i )
    {
        if ( Ivy_ObjFanin0(pObj) )
            Ivy_ObjAddFanout( p, Ivy_ObjFanin0(pObj), pObj );
        if ( Ivy_ObjFanin1(pObj) )
            Ivy_ObjAddFanout( p, Ivy_ObjFanin1(pObj), pObj );
    }
}

/**Function*************************************************************

  Synopsis    [Stops the fanout representation.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Ivy_ManStopFanout( Ivy_Man_t * p )
{
    void * pTemp;
    int i;
    assert( p->vFanouts != NULL );
    Vec_PtrForEachEntry( p->vFanouts, pTemp, i )
        if ( Ivy_FanoutIsArray(pTemp) )
            Vec_PtrFree( Ivy_FanoutGetArray(pTemp) );
    Vec_PtrFree( p->vFanouts );
    p->vFanouts = NULL;
}

/**Function*************************************************************

  Synopsis    [Add the fanout.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Ivy_ObjAddFanout( Ivy_Man_t * p, Ivy_Obj_t * pObj, Ivy_Obj_t * pFanout )
{
    Vec_Ptr_t * vNodes;
    void ** ppSpot;
    assert( p->vFanouts != NULL );
    assert( !Ivy_IsComplement(pObj) );
    // extend the fanout array if needed
    Vec_PtrFillExtra( p->vFanouts, pObj->Id + 1, NULL );
    // get the pointer to the place where fanouts are stored
    ppSpot = Vec_PtrEntryP( p->vFanouts, pObj->Id );
    // consider several cases
    if ( *ppSpot == NULL ) // no fanout - add one fanout
        *ppSpot = pFanout;
    else if ( Ivy_FanoutIsArray(*ppSpot) ) // array of fanouts - add one fanout
    {
        vNodes = Ivy_FanoutGetArray(*ppSpot);
        Vec_PtrPush( vNodes, pFanout );
    }
    else // only one fanout - create array and put both fanouts into the array
    {
        vNodes = Vec_PtrAlloc( 4 );
        Vec_PtrPush( vNodes, *ppSpot );
        Vec_PtrPush( vNodes, pFanout );
        *ppSpot = Ivy_FanoutSetArray( vNodes );
    }
}

/**Function*************************************************************

  Synopsis    [Removes the fanout.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Ivy_ObjDeleteFanout( Ivy_Man_t * p, Ivy_Obj_t * pObj, Ivy_Obj_t * pFanout )
{
    Vec_Ptr_t * vNodes;
    void ** ppSpot;
    assert( p->vFanouts != NULL );
    assert( !Ivy_IsComplement(pObj) );
    // extend the fanout array if needed
    Vec_PtrFillExtra( p->vFanouts, pObj->Id + 1, NULL );
    ppSpot = Vec_PtrEntryP( p->vFanouts, pObj->Id );
    if ( *ppSpot == NULL ) // no fanout - should not happen
    {
        assert( 0 );
    }
    else if ( Ivy_FanoutIsArray(*ppSpot) ) // the array of fanouts - delete the fanout
    {
        vNodes = Ivy_FanoutGetArray(*ppSpot);
        Vec_PtrRemove( vNodes, pFanout );
    }
    else // only one fanout - delete the fanout
    {
        assert( *ppSpot == pFanout );
        *ppSpot = NULL;
    }
//    printf( " %d", Ivy_ObjFanoutNum(p, pObj) );
}

/**Function*************************************************************

  Synopsis    [Replaces the fanout of pOld to be pFanoutNew.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Ivy_ObjPatchFanout( Ivy_Man_t * p, Ivy_Obj_t * pObj, Ivy_Obj_t * pFanoutOld, Ivy_Obj_t * pFanoutNew )
{
    Vec_Ptr_t * vNodes;
    void ** ppSpot;
    int Index;
    assert( p->vFanouts != NULL );
    assert( !Ivy_IsComplement(pObj) );
    // extend the fanout array if needed
    Vec_PtrFillExtra( p->vFanouts, pObj->Id + 1, NULL );
    ppSpot = Vec_PtrEntryP( p->vFanouts, pObj->Id );
    if ( *ppSpot == NULL ) // no fanout - should not happen
    {
        assert( 0 );
    }
    else if ( Ivy_FanoutIsArray(*ppSpot) ) // the array of fanouts - find and replace the fanout
    {
        vNodes = Ivy_FanoutGetArray(*ppSpot);
        Index = Vec_PtrFind( vNodes, pFanoutOld );
        assert( Index >= 0 );
        Vec_PtrWriteEntry( vNodes, Index, pFanoutNew );
    }
    else // only one fanout - delete the fanout
    {
        assert( *ppSpot == pFanoutOld );
        *ppSpot = pFanoutNew;
    }
}

/**Function*************************************************************

  Synopsis    [Starts iteration through the fanouts.]

  Description [Copies the currently available fanouts into the array.]
               
  SideEffects [Can be used while the fanouts are being removed.]

  SeeAlso     []

***********************************************************************/
void Ivy_ObjCollectFanouts( Ivy_Man_t * p, Ivy_Obj_t * pObj, Vec_Ptr_t * vArray )
{
    Vec_Ptr_t * vNodes;
    Ivy_Obj_t * pTemp;
    int i;
    assert( p->vFanouts != NULL );
    assert( !Ivy_IsComplement(pObj) );
    // extend the fanout array if needed
    Vec_PtrFillExtra( p->vFanouts, pObj->Id + 1, NULL );
    vNodes = Vec_PtrEntry( p->vFanouts, pObj->Id );
    // clean the resulting array
    Vec_PtrClear( vArray );
    if ( vNodes == NULL ) // no fanout - nothing to do
    {
    }
    else if ( Ivy_FanoutIsArray(vNodes) ) // the array of fanouts - copy fanouts
    {
        vNodes = Ivy_FanoutGetArray(vNodes);
        Vec_PtrForEachEntry( vNodes, pTemp, i )
            Vec_PtrPush( vArray, pTemp );
    }
    else // only one fanout - add the fanout
        Vec_PtrPush( vArray, vNodes );
}

/**Function*************************************************************

  Synopsis    [Reads one fanout.]

  Description [Returns fanout if there is only one fanout.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Ivy_Obj_t * Ivy_ObjReadOneFanout( Ivy_Man_t * p, Ivy_Obj_t * pObj )
{
    Vec_Ptr_t * vNodes;
    assert( p->vFanouts != NULL );
    assert( !Ivy_IsComplement(pObj) );
    // extend the fanout array if needed
    Vec_PtrFillExtra( p->vFanouts, pObj->Id + 1, NULL );
    vNodes = Vec_PtrEntry( p->vFanouts, pObj->Id );
    // clean the resulting array
    if ( vNodes && !Ivy_FanoutIsArray(vNodes) ) // only one fanout - return
        return (Ivy_Obj_t *)vNodes;
    return NULL;
}

/**Function*************************************************************

  Synopsis    [Reads one fanout.]

  Description [Returns fanout if there is only one fanout.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Ivy_Obj_t * Ivy_ObjReadFirstFanout( Ivy_Man_t * p, Ivy_Obj_t * pObj )
{
    Vec_Ptr_t * vNodes;
    assert( p->vFanouts != NULL );
    assert( !Ivy_IsComplement(pObj) );
    // extend the fanout array if needed
    Vec_PtrFillExtra( p->vFanouts, pObj->Id + 1, NULL );
    vNodes = Vec_PtrEntry( p->vFanouts, pObj->Id );
    // clean the resulting array
    if ( vNodes == NULL )
        return NULL;
    if ( !Ivy_FanoutIsArray(vNodes) ) // only one fanout - return
        return (Ivy_Obj_t *)vNodes;
    return Vec_PtrEntry( Ivy_FanoutGetArray(vNodes), 0 );
}

/**Function*************************************************************

  Synopsis    [Reads one fanout.]

  Description [Returns fanout if there is only one fanout.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Ivy_ObjFanoutNum( Ivy_Man_t * p, Ivy_Obj_t * pObj )
{
    Vec_Ptr_t * vNodes;
    assert( p->vFanouts != NULL );
    assert( !Ivy_IsComplement(pObj) );
    // extend the fanout array if needed
    Vec_PtrFillExtra( p->vFanouts, pObj->Id + 1, NULL );
    vNodes = Vec_PtrEntry( p->vFanouts, pObj->Id );
    // clean the resulting array
    if ( vNodes == NULL )
        return 0;
    if ( !Ivy_FanoutIsArray(vNodes) ) // only one fanout - return
        return 1;
    return Vec_PtrSize( Ivy_FanoutGetArray(vNodes) );
}


////////////////////////////////////////////////////////////////////////
///                       END OF FILE                                ///
////////////////////////////////////////////////////////////////////////