summaryrefslogtreecommitdiffstats
path: root/src/sat/aig/aigNode.c
blob: 991cc7e5042755604d6089fe62e67102e2a5a948 (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
/**CFile****************************************************************

  FileName    [aigNode.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [And-Inverter Graph package.]

  Synopsis    []

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

  Date        [Ver. 1.0. Started - June 20, 2005.]

  Revision    [$Id: aigNode.c,v 1.00 2005/06/20 00:00:00 alanmi Exp $]

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

#include "aig.h"

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

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

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

  Synopsis    [Creates the node.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline Aig_Node_t * Aig_NodeCreate( Aig_Man_t * p )
{
    Aig_Node_t * pNode;
    // create the node
    pNode = (Aig_Node_t *)Aig_MemFixedEntryFetch( p->mmNodes );
    memset( pNode, 0, sizeof(Aig_Node_t) );
    // assign the number and add to the array of nodes
    pNode->pMan = p;
    pNode->Id = p->vNodes->nSize;
    Vec_PtrPush( p->vNodes, pNode );
    return pNode;
}

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

  Synopsis    [Creates the constant 1 node.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Aig_Node_t * Aig_NodeCreateConst( Aig_Man_t * p )
{
    Aig_Node_t * pNode;
    pNode = Aig_NodeCreate( p );
    pNode->Type   = AIG_NONE;
    pNode->fPhase = 1; // sim value for 000... pattern
    return pNode;
}

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

  Synopsis    [Creates a primary input node.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Aig_Node_t * Aig_NodeCreatePi( Aig_Man_t * p )
{
    Aig_Node_t * pNode;
    pNode = Aig_NodeCreate( p );
    Vec_PtrPush( p->vPis, pNode );
    pNode->Type   = AIG_PI;
    pNode->fPhase =  0;  // sim value for 000... pattern
    return pNode;
}

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

  Synopsis    [Creates a primary output node.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Aig_Node_t * Aig_NodeCreatePo( Aig_Man_t * p, Aig_Node_t * pFanin )
{
    Aig_Node_t * pNode;
    pNode = Aig_NodeCreate( p );
    pNode->Type = AIG_PO;
    Vec_PtrPush( p->vPos, pNode );
    // connect to the fanin
    pNode->Fans[0].fComp = Aig_IsComplement(pFanin);
    pNode->Fans[0].iNode = Aig_Regular(pFanin)->Id;
    pNode->fPhase = pNode->Fans[0].fComp ^ Aig_Regular(pFanin)->fPhase;  // sim value for 000... pattern
    pNode->Level = Aig_Regular(pFanin)->Level;
    Aig_Regular(pFanin)->nRefs++;
    if ( pNode->pMan->vFanPivots )  Aig_NodeAddFaninFanout( pFanin, pNode );
    // update global level if needed
    if ( p->nLevelMax < (int)pNode->Level )
        p->nLevelMax = pNode->Level;
    return pNode;
}

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

  Synopsis    [Creates a new node.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Aig_Node_t * Aig_NodeCreateAnd( Aig_Man_t * p, Aig_Node_t * pFanin0, Aig_Node_t * pFanin1 )
{
    Aig_Node_t * pNode;
    pNode = Aig_NodeCreate( p );
    pNode->Type = AIG_AND;
    Aig_NodeConnectAnd( pFanin0, pFanin1, pNode );
    return pNode;
}

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

  Synopsis    [Connects the nodes to the AIG.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Aig_NodeConnectAnd( Aig_Node_t * pFanin0, Aig_Node_t * pFanin1, Aig_Node_t * pNode )
{
    assert( !Aig_IsComplement(pNode) );
    assert( Aig_NodeIsAnd(pNode) );
    // add the fanins
    pNode->Fans[0].fComp = Aig_IsComplement(pFanin0);
    pNode->Fans[0].iNode = Aig_Regular(pFanin0)->Id;
    pNode->Fans[1].fComp = Aig_IsComplement(pFanin1);
    pNode->Fans[1].iNode = Aig_Regular(pFanin1)->Id;
    // compute the phase (sim value for 000... pattern)
    pNode->fPhase = (pNode->Fans[0].fComp ^ Aig_Regular(pFanin0)->fPhase) & 
                    (pNode->Fans[1].fComp ^ Aig_Regular(pFanin1)->fPhase);
    pNode->Level = Aig_NodeGetLevelNew(pNode);
    // reference the fanins
    Aig_Regular(pFanin0)->nRefs++;
    Aig_Regular(pFanin1)->nRefs++;
    // add the fanouts
    if ( pNode->pMan->vFanPivots )  Aig_NodeAddFaninFanout( pFanin0, pNode );
    if ( pNode->pMan->vFanPivots )  Aig_NodeAddFaninFanout( pFanin1, pNode );
    // add the node to the structural hash table
    Aig_TableInsertNode( pNode->pMan, pFanin0, pFanin1, pNode );
}

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

  Synopsis    [Connects the nodes to the AIG.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Aig_NodeDisconnectAnd( Aig_Node_t * pNode )
{
    Aig_Node_t * pFanin0, * pFanin1;
    assert( !Aig_IsComplement(pNode) );
    assert( Aig_NodeIsAnd(pNode) );
    // get the fanins
    pFanin0 = Aig_NodeFanin0(pNode);
    pFanin1 = Aig_NodeFanin1(pNode);
    // dereference the fanins
    pFanin0->nRefs--;
    pFanin0->nRefs--;
    // remove the fanouts
    if ( pNode->pMan->vFanPivots )  Aig_NodeRemoveFaninFanout( pFanin0, pNode );
    if ( pNode->pMan->vFanPivots )  Aig_NodeRemoveFaninFanout( pFanin1, pNode );
    // remove the node from the structural hash table
    Aig_TableDeleteNode( pNode->pMan, pNode );
}

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

  Synopsis    [Performs internal deletion step.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Aig_NodeDeleteAnd_rec( Aig_Man_t * pMan, Aig_Node_t * pRoot )
{
    Aig_Node_t * pNode0, * pNode1;
    // make sure the node is regular and dangling
    assert( !Aig_IsComplement(pRoot) );
    assert( pRoot->nRefs == 0 );
    assert( Aig_NodeIsAnd(pRoot) );
    // save the children
    pNode0 = Aig_NodeFanin0(pRoot);
    pNode1 = Aig_NodeFanin1(pRoot);
    // disconnect the node
    Aig_NodeDisconnectAnd( pRoot );
    // recycle the node
    Vec_PtrWriteEntry( pMan->vNodes, pRoot->Id, NULL );
    Aig_MemFixedEntryRecycle( pMan->mmNodes, (char *)pRoot );
    // call recursively
    if ( Aig_NodeIsAnd(pNode0) && pNode0->nRefs == 0 )
        Aig_NodeDeleteAnd_rec( pMan, pNode0 );
    if ( Aig_NodeIsAnd(pNode1) && pNode1->nRefs == 0 )
        Aig_NodeDeleteAnd_rec( pMan, pNode1 );
}

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

  Synopsis    [Prints the AIG node for debugging purposes.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Aig_NodePrint( Aig_Node_t * pNode )
{
    Aig_Node_t * pNodeR = Aig_Regular(pNode);
    if ( Aig_NodeIsPi(pNode) )
    {
        printf( "PI %4s%s.\n", Aig_NodeName(pNode), Aig_IsComplement(pNode)? "\'" : "" );
        return;
    }
    if ( Aig_NodeIsConst(pNode) )
    {
        printf( "Constant 1 %s.\n", Aig_IsComplement(pNode)? "(complemented)" : ""  );
        return;
    }
    // print the node's function
    printf( "%7s%s", Aig_NodeName(pNodeR),                 Aig_IsComplement(pNode)? "\'" : "" );
    printf( " = " );
    printf( "%7s%s", Aig_NodeName(Aig_NodeFanin0(pNodeR)), Aig_NodeFaninC0(pNodeR)? "\'" : "" );
    printf( " * " );
    printf( "%7s%s", Aig_NodeName(Aig_NodeFanin1(pNodeR)), Aig_NodeFaninC1(pNodeR)? "\'" : "" );
    printf( "\n" );
}

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

  Synopsis    [Returns the name of the node.]

  Description [The name should be used before this procedure is called again.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
char * Aig_NodeName( Aig_Node_t * pNode )
{
    static char Buffer[100];
    sprintf( Buffer, "%d", Aig_Regular(pNode)->Id );
    return Buffer;
}

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