summaryrefslogtreecommitdiffstats
path: root/src/aig/dar/darObj.c
blob: 85b142e64ddab3b28397b96a5800ee42b31179f7 (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
/**CFile****************************************************************

  FileName    [darObj.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [DAG-aware AIG rewriting.]

  Synopsis    [Adding/removing objects.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

  Date        [Ver. 1.0. Started - April 28, 2007.]

  Revision    [$Id: darObj.c,v 1.00 2007/04/28 00:00:00 alanmi Exp $]

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

#include "dar.h"

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

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

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

  Synopsis    [Creates primary input.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Dar_Obj_t * Dar_ObjCreatePi( Dar_Man_t * p )
{
    Dar_Obj_t * pObj;
    pObj = Dar_ManFetchMemory( p );
    pObj->Type = DAR_AIG_PI;
    Vec_PtrPush( p->vPis, pObj );
    p->nObjs[DAR_AIG_PI]++;
    return pObj;
}

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

  Synopsis    [Creates primary output with the given driver.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Dar_Obj_t * Dar_ObjCreatePo( Dar_Man_t * p, Dar_Obj_t * pDriver )
{
    Dar_Obj_t * pObj;
    pObj = Dar_ManFetchMemory( p );
    pObj->Type = DAR_AIG_PO;
    Vec_PtrPush( p->vPos, pObj );
    // add connections
    Dar_ObjConnect( p, pObj, pDriver, NULL );
    // update node counters of the manager
    p->nObjs[DAR_AIG_PO]++;
    return pObj;
}


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

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

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Dar_Obj_t * Dar_ObjCreate( Dar_Man_t * p, Dar_Obj_t * pGhost )
{
    Dar_Obj_t * pObj;
    assert( !Dar_IsComplement(pGhost) );
    assert( Dar_ObjIsHash(pGhost) );
    assert( pGhost == &p->Ghost );
    // get memory for the new object
    pObj = Dar_ManFetchMemory( p );
    pObj->Type = pGhost->Type;
    // add connections
    Dar_ObjConnect( p, pObj, pGhost->pFanin0, pGhost->pFanin1 );
    // update node counters of the manager
    p->nObjs[Dar_ObjType(pObj)]++;
    assert( pObj->pData == NULL );
    return pObj;
}

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

  Synopsis    [Connect the object to the fanin.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Dar_ObjConnect( Dar_Man_t * p, Dar_Obj_t * pObj, Dar_Obj_t * pFan0, Dar_Obj_t * pFan1 )
{
    assert( !Dar_IsComplement(pObj) );
    assert( !Dar_ObjIsPi(pObj) );
    // add the first fanin
    pObj->pFanin0 = pFan0;
    pObj->pFanin1 = pFan1;
    // increment references of the fanins and add their fanouts
    if ( pFan0 != NULL )
    {
        assert( Dar_ObjFanin0(pObj)->Type > 0 );
        Dar_ObjRef( Dar_ObjFanin0(pObj) );
    }
    if ( pFan1 != NULL )
    {
        assert( Dar_ObjFanin1(pObj)->Type > 0 );
        Dar_ObjRef( Dar_ObjFanin1(pObj) );
    }
    // set level and phase
    if ( pFan1 != NULL )
    {
        pObj->Level = Dar_ObjLevelNew( pObj );
        pObj->fPhase = Dar_ObjFaninPhase(pFan0) & Dar_ObjFaninPhase(pFan1);
    }
    else
    {
        pObj->Level = pFan0->Level;
        pObj->fPhase = Dar_ObjFaninPhase(pFan0);
    }
    // add the node to the structural hash table
    if ( Dar_ObjIsHash(pObj) )
        Dar_TableInsert( p, pObj );
}

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

  Synopsis    [Disconnects the object from the fanins.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Dar_ObjDisconnect( Dar_Man_t * p, Dar_Obj_t * pObj )
{
    assert( !Dar_IsComplement(pObj) );
    // remove connections
    if ( pObj->pFanin0 != NULL )
        Dar_ObjDeref(Dar_ObjFanin0(pObj));
    if ( pObj->pFanin1 != NULL )
        Dar_ObjDeref(Dar_ObjFanin1(pObj));
    // remove the node from the structural hash table
    if ( Dar_ObjIsHash(pObj) )
        Dar_TableDelete( p, pObj );
    // add the first fanin
    pObj->pFanin0 = NULL;
    pObj->pFanin1 = NULL;
}

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

  Synopsis    [Deletes the node.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Dar_ObjDelete( Dar_Man_t * p, Dar_Obj_t * pObj )
{
    assert( !Dar_IsComplement(pObj) );
    assert( !Dar_ObjIsTerm(pObj) );
    assert( Dar_ObjRefs(pObj) == 0 );
    p->nObjs[pObj->Type]--;
    Vec_PtrWriteEntry( p->vObjs, pObj->Id, NULL );
    Dar_ManRecycleMemory( p, pObj );
}

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

  Synopsis    [Deletes the MFFC of the node.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Dar_ObjDelete_rec( Dar_Man_t * p, Dar_Obj_t * pObj, int fFreeTop )
{
    Dar_Obj_t * pFanin0, * pFanin1;
    assert( !Dar_IsComplement(pObj) );
    if ( Dar_ObjIsConst1(pObj) || Dar_ObjIsPi(pObj) )
        return;
    assert( !Dar_ObjIsPo(pObj) );
    pFanin0 = Dar_ObjFanin0(pObj);
    pFanin1 = Dar_ObjFanin1(pObj);
    Dar_ObjDisconnect( p, pObj );
    if ( fFreeTop )
        Dar_ObjDelete( p, pObj );
    if ( pFanin0 && !Dar_ObjIsNone(pFanin0) && Dar_ObjRefs(pFanin0) == 0 )
        Dar_ObjDelete_rec( p, pFanin0, 1 );
    if ( pFanin1 && !Dar_ObjIsNone(pFanin1) && Dar_ObjRefs(pFanin1) == 0 )
        Dar_ObjDelete_rec( p, pFanin1, 1 );
}

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

  Synopsis    [Replaces the first fanin of the node by the new fanin.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Dar_ObjPatchFanin0( Dar_Man_t * p, Dar_Obj_t * pObj, Dar_Obj_t * pFaninNew )
{
    Dar_Obj_t * pFaninOld;
    assert( !Dar_IsComplement(pObj) );
    pFaninOld = Dar_ObjFanin0(pObj);
    // decrement ref and remove fanout
    Dar_ObjDeref( pFaninOld );
    // update the fanin
    pObj->pFanin0 = pFaninNew;
    // increment ref and add fanout
    Dar_ObjRef( Dar_Regular(pFaninNew) );
    // get rid of old fanin
    if ( !Dar_ObjIsPi(pFaninOld) && !Dar_ObjIsConst1(pFaninOld) && Dar_ObjRefs(pFaninOld) == 0 )
        Dar_ObjDelete_rec( p, pFaninOld, 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 Dar_ObjReplace( Dar_Man_t * p, Dar_Obj_t * pObjOld, Dar_Obj_t * pObjNew, int fNodesOnly )
{
    Dar_Obj_t * pObjNewR = Dar_Regular(pObjNew);
    // the object to be replaced cannot be complemented
    assert( !Dar_IsComplement(pObjOld) );
    // the object to be replaced cannot be a terminal
    assert( !Dar_ObjIsPi(pObjOld) && !Dar_ObjIsPo(pObjOld) );
    // the object to be used cannot be a buffer or a PO
    assert( !Dar_ObjIsBuf(pObjNewR) && !Dar_ObjIsPo(pObjNewR) );
    // the object cannot be the same
    assert( pObjOld != pObjNewR );
    // make sure object is not pointing to itself
    assert( pObjOld != Dar_ObjFanin0(pObjNewR) );
    assert( pObjOld != Dar_ObjFanin1(pObjNewR) );
    // recursively delete the old node - but leave the object there
    pObjNewR->nRefs++;
    Dar_ObjDelete_rec( p, pObjOld, 0 );
    pObjNewR->nRefs--;
    // if the new object is complemented or already used, create a buffer
    p->nObjs[pObjOld->Type]--;
    if ( Dar_IsComplement(pObjNew) || Dar_ObjRefs(pObjNew) > 0 || (fNodesOnly && !Dar_ObjIsNode(pObjNew)) )
    {
        pObjOld->Type = DAR_AIG_BUF;
        Dar_ObjConnect( p, pObjOld, pObjNew, NULL );
    }
    else
    {
        Dar_Obj_t * pFanin0 = pObjNew->pFanin0;
        Dar_Obj_t * pFanin1 = pObjNew->pFanin1;
        pObjOld->Type = pObjNew->Type;
        Dar_ObjDisconnect( p, pObjNew );
        Dar_ObjConnect( p, pObjOld, pFanin0, pFanin1 );
        Dar_ObjDelete( p, pObjNew );
    }
    p->nObjs[pObjOld->Type]++;
}

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