summaryrefslogtreecommitdiffstats
path: root/src/temp/ivy/ivyObj.c
blob: 5d063525502e175ab9448ba36367c7a5d4923662 (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
319
320
321
322
323
324
325
/**CFile****************************************************************

  FileName    [ivyObj.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [And-Inverter Graph package.]

  Synopsis    [Adding/removing objects.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

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

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

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

#include "ivy.h"

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

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

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

  Synopsis    [Create the new node assuming it does not exist.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Ivy_Obj_t * Ivy_ObjCreate( Ivy_Obj_t * pGhost )
{
    Ivy_Man_t * p = Ivy_ObjMan(pGhost);
    Ivy_Obj_t * pObj;
    assert( !Ivy_IsComplement(pGhost) );
    assert( Ivy_ObjIsGhost(pGhost) );

    assert( Ivy_TableLookup(pGhost) == NULL );

    // realloc the node array
    if ( p->ObjIdNext == p->nObjsAlloc )
    {
        Ivy_ManGrow( p );
        pGhost = Ivy_ManGhost( p );
    }
    // get memory for the new object
    if ( Vec_IntSize(p->vFree) > 0 )
        pObj = p->pObjs + Vec_IntPop(p->vFree);
    else
        pObj = p->pObjs + p->ObjIdNext++;
    assert( pObj->Id == pObj - p->pObjs );
    assert( Ivy_ObjIsNone(pObj) );
    // add basic info (fanins, compls, type, init)
    Ivy_ObjOverwrite( pObj, pGhost );
    // increment references of the fanins
    Ivy_ObjRefsInc( Ivy_ObjFanin0(pObj) );
    Ivy_ObjRefsInc( Ivy_ObjFanin1(pObj) );
    // add the node to the structural hash table
    Ivy_TableInsert( pObj );
    // compute level
    if ( Ivy_ObjIsNode(pObj) )
        pObj->Level = Ivy_ObjLevelNew(pObj);
    else if ( Ivy_ObjIsLatch(pObj) )
        pObj->Level = 0;
    else if ( Ivy_ObjIsOneFanin(pObj) )
        pObj->Level = Ivy_ObjFanin0(pObj)->Level;
    else if ( !Ivy_ObjIsPi(pObj) )
        assert( 0 );
    // mark the fanins in a special way if the node is EXOR
    if ( Ivy_ObjIsExor(pObj) )
    {
        Ivy_ObjFanin0(pObj)->fExFan = 1;
        Ivy_ObjFanin1(pObj)->fExFan = 1;
    }
    // add PIs/POs to the arrays
    if ( Ivy_ObjIsPi(pObj) )
        Vec_IntPush( p->vPis, pObj->Id );
    else if ( Ivy_ObjIsPo(pObj) )
        Vec_IntPush( p->vPos, pObj->Id );
    // update node counters of the manager
    p->nObjs[Ivy_ObjType(pObj)]++;
    p->nCreated++;
    return pObj;
}

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

  Synopsis    [Create the new node assuming it does not exist.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Ivy_Obj_t * Ivy_ObjCreateExt( Ivy_Man_t * p, Ivy_Type_t Type )
{
    Ivy_Obj_t * pObj;
    assert( Type == IVY_ANDM || Type == IVY_EXORM || Type == IVY_LUT ); 
    // realloc the node array
    if ( p->ObjIdNext == p->nObjsAlloc )
        Ivy_ManGrow( p );
    // create the new node
    pObj = p->pObjs + p->ObjIdNext;
    assert( pObj->Id == p->ObjIdNext );
    p->ObjIdNext++;
    pObj->Type = Type;
    // update node counters of the manager
    p->nObjs[Type]++;
    p->nCreated++;
    return pObj;
}

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

  Synopsis    [Connect the object to the fanin.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Ivy_ObjConnect( Ivy_Obj_t * pObj, Ivy_Obj_t * pFanin )
{
    assert( !Ivy_IsComplement(pObj) );
    assert( Ivy_ObjIsOneFanin(pObj) );
    assert( Ivy_ObjFaninId0(pObj) == 0 );
    // add the first fanin
    pObj->fComp0 = Ivy_IsComplement(pFanin);
    pObj->Fanin0 = Ivy_Regular(pFanin)->Id;
    // increment references of the fanins
    Ivy_ObjRefsInc( Ivy_ObjFanin0(pObj) );
    // add the node to the structural hash table
    Ivy_TableInsert( pObj );
}

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

  Synopsis    [Deletes the node.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Ivy_ObjDelete( Ivy_Obj_t * pObj, int fFreeTop )
{
    Ivy_Man_t * p;
    assert( !Ivy_IsComplement(pObj) );
    assert( Ivy_ObjRefs(pObj) == 0 );
    // remove connections
    Ivy_ObjRefsDec(Ivy_ObjFanin0(pObj));
    Ivy_ObjRefsDec(Ivy_ObjFanin1(pObj));
    // remove the node from the structural hash table
    Ivy_TableDelete( pObj );
    // update node counters of the manager
    p = Ivy_ObjMan(pObj);
    p->nObjs[pObj->Type]--;
    p->nDeleted++;
    // remove PIs/POs from the arrays
    if ( Ivy_ObjIsPi(pObj) )
        Vec_IntRemove( p->vPis, pObj->Id );
    else if ( Ivy_ObjIsPo(pObj) )
        Vec_IntRemove( p->vPos, pObj->Id );
    // recorde the deleted node
    if ( p->fRecording )
        Ivy_ManUndoRecord( p, pObj );
    // clean the node's memory
    Ivy_ObjClean( pObj );
    // remember the entry
    if ( fFreeTop )
        Vec_IntPush( p->vFree, pObj->Id );
}

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

  Synopsis    [Deletes the MFFC of the node.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Ivy_ObjDelete_rec( Ivy_Obj_t * pObj, int fFreeTop )
{
    Ivy_Obj_t * pFanin0, * pFanin1;
    assert( !Ivy_IsComplement(pObj) );
    assert( !Ivy_ObjIsPo(pObj) && !Ivy_ObjIsNone(pObj) );
    if ( Ivy_ObjIsConst1(pObj) || Ivy_ObjIsPi(pObj) )
        return;
    pFanin0 = Ivy_ObjFanin0(pObj);
    pFanin1 = Ivy_ObjFanin1(pObj);
    Ivy_ObjDelete( pObj, fFreeTop );
    if ( !Ivy_ObjIsNone(pFanin0) && Ivy_ObjRefs(pFanin0) == 0 )
        Ivy_ObjDelete_rec( pFanin0, 1 );
    if ( !Ivy_ObjIsNone(pFanin1) && Ivy_ObjRefs(pFanin1) == 0 )
        Ivy_ObjDelete_rec( pFanin1, 1 );
}

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

  Synopsis    [Replaces one object by another.]

  Description [Both objects are currently in the manager. The new object
  (pObjNew) should be used instead of the old object (pObjOld). If the 
  new object is complemented or used, the buffer is added.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Ivy_ObjReplace( Ivy_Obj_t * pObjOld, Ivy_Obj_t * pObjNew, int fDeleteOld, int fFreeTop )
{
    int nRefsOld;
    // the object to be replaced cannot be complemented
    assert( !Ivy_IsComplement(pObjOld) );
    // the object to be replaced cannot be a terminal
    assert( Ivy_ObjIsNone(pObjOld) || !Ivy_ObjIsTerm(pObjOld) );
    // the object to be used cannot be a PO or assert
    assert( !Ivy_ObjIsPo(Ivy_Regular(pObjNew)) );
    // the object cannot be the same
    assert( pObjOld != Ivy_Regular(pObjNew) );
    // if the new object is complemented or already used, add the buffer
    if ( Ivy_IsComplement(pObjNew) || Ivy_ObjRefs(pObjNew) > 0 || Ivy_ObjIsPi(pObjNew) || Ivy_ObjIsConst1(pObjNew) )
        pObjNew = Ivy_ObjCreate( Ivy_ObjCreateGhost(pObjNew, Ivy_ObjConst1(pObjOld), IVY_BUF, IVY_INIT_NONE) );
    assert( !Ivy_IsComplement(pObjNew) );
    // remember the reference counter
    nRefsOld = pObjOld->nRefs;  
    pObjOld->nRefs = 0;
    // delete the old object
    if ( fDeleteOld )
        Ivy_ObjDelete_rec( pObjOld, fFreeTop );
    // transfer the old object
    assert( Ivy_ObjRefs(pObjNew) == 0 );
    Ivy_ObjOverwrite( pObjOld, pObjNew );
    pObjOld->nRefs = nRefsOld;
    // update the hash table
    Ivy_TableUpdate( pObjNew, pObjOld->Id );
    // create the object that was taken over by pObjOld
    Ivy_ObjClean( pObjNew );
    // remember the entry
    Vec_IntPush( Ivy_ObjMan(pObjOld)->vFree, pObjNew->Id );
}

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

  Synopsis    [Returns the first real fanins (not a buffer/inverter).]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Ivy_Obj_t * Ivy_NodeRealFanin_rec( Ivy_Obj_t * pNode, int iFanin )
{
    if ( iFanin == 0 )
    {
        if ( Ivy_ObjIsBuf(Ivy_ObjFanin0(pNode)) )
            return Ivy_NotCond( Ivy_NodeRealFanin_rec(Ivy_ObjFanin0(pNode), 0), Ivy_ObjFaninC0(pNode) );
        else
            return Ivy_ObjChild0(pNode);
    }
    else
    {
        if ( Ivy_ObjIsBuf(Ivy_ObjFanin1(pNode)) )
            return Ivy_NotCond( Ivy_NodeRealFanin_rec(Ivy_ObjFanin1(pNode), 0), Ivy_ObjFaninC1(pNode) );
        else
            return Ivy_ObjChild1(pNode);
    }
}

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

  Synopsis    [Fixes buffer fanins.]

  Description [This situation happens because NodeReplace is a lazy
  procedure, which does not propagate the change to the fanouts but
  instead records the change in the form of a buf/inv node.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Ivy_NodeFixBufferFanins( Ivy_Obj_t * pNode )
{
    Ivy_Obj_t * pFanReal0, * pFanReal1, * pResult;
    assert( Ivy_ObjIsNode(pNode) );
    if ( !Ivy_ObjIsBuf(Ivy_ObjFanin0(pNode)) && !Ivy_ObjIsBuf(Ivy_ObjFanin1(pNode)) )
        return;
    // get the real fanins
    pFanReal0 = Ivy_NodeRealFanin_rec( pNode, 0 );
    pFanReal1 = Ivy_NodeRealFanin_rec( pNode, 1 );
    // get the new node
    pResult = Ivy_Oper( pFanReal0, pFanReal1, Ivy_ObjType(pNode) );
    // perform the replacement
    Ivy_ObjReplace( pNode, pResult, 1, 0 ); 
}

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