summaryrefslogtreecommitdiffstats
path: root/src/sat/aig/rwrMffc.c
blob: 663534b335393f2b7465104f1f32d0d8f4fe40f0 (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
/**CFile****************************************************************

  FileName    [rwrMffc.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [And-Inverter Graph package.]

  Synopsis    [Procedures working with Maximum Fanout-Free Cones.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

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

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

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

#include "aig.h"

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

extern int  Aig_NodeDeref_rec( Aig_Node_t * pNode );
extern int  Aig_NodeRef_rec( Aig_Node_t * pNode );
extern void Aig_NodeMffsConeSupp( Aig_Node_t * pNode, Vec_Ptr_t * vCone, Vec_Ptr_t * vSupp );
extern void Aig_NodeFactorConeSupp( Aig_Node_t * pNode, Vec_Ptr_t * vCone, Vec_Ptr_t * vSupp );


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

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Aig_MffcTest( Aig_Man_t * pMan )
{
    Aig_Node_t * pNode, * pNodeA, * pNodeB, * pNodeC, * pLeaf;
    Vec_Ptr_t * vCone, * vSupp;
    int i, k;//, nNodes1, nNodes2;
    int nSizes = 0;
    int nCones = 0;
    int nMuxes = 0;
    int nExors = 0;

    vCone = Vec_PtrAlloc( 100 );
    vSupp = Vec_PtrAlloc( 100 );

    // mark the multiple-fanout nodes
    Aig_ManForEachAnd( pMan, pNode, i )
        if ( pNode->nRefs > 1 )
            pNode->fMarkA = 1;
    // unmark the control inputs of MUXes and inputs of EXOR gates
    Aig_ManForEachAnd( pMan, pNode, i )
    {
        if ( !Aig_NodeIsMuxType(pNode) )
            continue;

        pNodeC = Aig_NodeRecognizeMux( pNode, &pNodeA, &pNodeB );
        // if real children are used, skip
        if ( Aig_NodeFanin0(pNode)->nRefs > 1 || Aig_NodeFanin1(pNode)->nRefs > 1 )
            continue;
/*

        if ( pNodeC->nRefs == 2 )
            pNodeC->fMarkA = 0;
        if ( Aig_Regular(pNodeA) == Aig_Regular(pNodeB) && Aig_Regular(pNodeA)->nRefs == 2 )
            Aig_Regular(pNodeA)->fMarkA = 0;
*/

        if ( Aig_Regular(pNodeA) == Aig_Regular(pNodeB) )
            nExors++;
        else
            nMuxes++;
    }
    // mark the PO drivers
    Aig_ManForEachPo( pMan, pNode, i )
    {
        Aig_NodeFanin0(pNode)->fMarkA = 1;
        Aig_NodeFanin0(pNode)->fMarkB = 1;
    }


    // print sizes of MFFCs
    Aig_ManForEachAnd( pMan, pNode, i )
    {
        if ( !pNode->fMarkA )
            continue;

//        nNodes1 = Aig_NodeDeref_rec( pNode );
//        Aig_NodeMffsConeSupp( pNode, vCone, vSupp );
//        nNodes2 = Aig_NodeRef_rec( pNode );
//        assert( nNodes1 == nNodes2 );

        Aig_NodeFactorConeSupp( pNode, vCone, vSupp );

        printf( "%6d : Fan = %4d. Co = %5d.  Su = %5d.  %s ", 
            pNode->Id, pNode->nRefs, Vec_PtrSize(vCone), Vec_PtrSize(vSupp), pNode->fMarkB? "po" : "  " );

        Vec_PtrForEachEntry( vSupp, pLeaf, k )
            printf( " %d", pLeaf->Id );

        printf( "\n" );

        nSizes += Vec_PtrSize(vCone);
        nCones++;
    }
    printf( "Nodes = %6d.  MFFC sizes = %6d.  Cones = %6d.  nExors = %6d.  Muxes = %6d.\n",
        Aig_ManAndNum(pMan), nSizes, nCones, nExors, nMuxes );

    // unmark the nodes
    Aig_ManForEachNode( pMan, pNode, i )
    {
        pNode->fMarkA = 0;
        pNode->fMarkB = 0;
        pNode->fMarkC = 0;
    }

    Vec_PtrFree( vCone );
    Vec_PtrFree( vSupp );
}

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

  Synopsis    [Dereferences the node's MFFC.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Aig_NodeDeref_rec( Aig_Node_t * pNode )
{
    Aig_Node_t * pNode0, * pNode1;
    int Counter = 1;
    if ( Aig_NodeIsPi(pNode) )
        return 0;
    pNode0 = Aig_NodeFanin0(pNode);
    pNode1 = Aig_NodeFanin1(pNode);
    assert( pNode0->nRefs > 0 );
    assert( pNode1->nRefs > 0 );
    if ( --pNode0->nRefs == 0 )
        Counter += Aig_NodeDeref_rec( pNode0 );
    if ( --pNode1->nRefs == 0 )
        Counter += Aig_NodeDeref_rec( pNode1 );
    return Counter;
}

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

  Synopsis    [References the node's MFFC.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Aig_NodeRef_rec( Aig_Node_t * pNode )
{
    Aig_Node_t * pNode0, * pNode1;
    int Counter = 1;
    if ( Aig_NodeIsPi(pNode) )
        return 0;
    pNode0 = Aig_NodeFanin0(pNode);
    pNode1 = Aig_NodeFanin1(pNode);
    if ( pNode0->nRefs++ == 0 )
        Counter += Aig_NodeRef_rec( pNode0 );
    if ( pNode1->nRefs++ == 0 )
        Counter += Aig_NodeRef_rec( pNode1 );
    return Counter;
}

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

  Synopsis    [Collects the internal and leaf nodes in the derefed MFFC.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Aig_NodeMffsConeSupp_rec( Aig_Node_t * pNode, Vec_Ptr_t * vCone, Vec_Ptr_t * vSupp, int fTopmost )
{
    // skip visited nodes
    if ( Aig_NodeIsTravIdCurrent(pNode) )
        return;
    Aig_NodeSetTravIdCurrent(pNode);
    // add to the new support nodes
    if ( !fTopmost && (Aig_NodeIsPi(pNode) || pNode->nRefs > 0) )
    {
        Vec_PtrPush( vSupp, pNode );
        return;
    }
    // recur on the children
    Aig_NodeMffsConeSupp_rec( Aig_NodeFanin0(pNode), vCone, vSupp, 0 );
    Aig_NodeMffsConeSupp_rec( Aig_NodeFanin1(pNode), vCone, vSupp, 0 );
    // collect the internal node
    Vec_PtrPush( vCone, pNode );
}

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

  Synopsis    [Collects the support of the derefed MFFC.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Aig_NodeMffsConeSupp( Aig_Node_t * pNode, Vec_Ptr_t * vCone, Vec_Ptr_t * vSupp )
{
    assert( Aig_NodeIsAnd(pNode) );
    assert( !Aig_IsComplement(pNode) );
    Vec_PtrClear( vCone );
    Vec_PtrClear( vSupp );
    Aig_ManIncrementTravId( pNode->pMan );
    Aig_NodeMffsConeSupp_rec( pNode, vCone, vSupp, 1 );
}





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

  Synopsis    [Collects the internal and leaf nodes of the factor-cut of the node.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Aig_NodeFactorConeSupp_rec( Aig_Node_t * pNode, Vec_Ptr_t * vCone, Vec_Ptr_t * vSupp, int fTopmost )
{
    // skip visited nodes
    if ( Aig_NodeIsTravIdCurrent(pNode) )
        return;
    Aig_NodeSetTravIdCurrent(pNode);
    // add to the new support nodes
    if ( !fTopmost && (Aig_NodeIsPi(pNode) || pNode->fMarkA) )
    {
        Vec_PtrPush( vSupp, pNode );
        return;
    }
    // recur on the children
    Aig_NodeFactorConeSupp_rec( Aig_NodeFanin0(pNode), vCone, vSupp, 0 );
    Aig_NodeFactorConeSupp_rec( Aig_NodeFanin1(pNode), vCone, vSupp, 0 );
    // collect the internal node
    assert( fTopmost || !pNode->fMarkA );
    Vec_PtrPush( vCone, pNode );

    assert( pNode->fMarkC == 0 );
    pNode->fMarkC = 1;
}

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

  Synopsis    [Collects the support of the derefed MFFC.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Aig_NodeFactorConeSupp( Aig_Node_t * pNode, Vec_Ptr_t * vCone, Vec_Ptr_t * vSupp )
{
    assert( Aig_NodeIsAnd(pNode) );
    assert( !Aig_IsComplement(pNode) );
    Vec_PtrClear( vCone );
    Vec_PtrClear( vSupp );
    Aig_ManIncrementTravId( pNode->pMan );
    Aig_NodeFactorConeSupp_rec( pNode, vCone, vSupp, 1 );
}

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