summaryrefslogtreecommitdiffstats
path: root/src/temp/ivy/ivyRewrite.c
blob: 031db9bc0c960358a9fa277a046e95875f8215f4 (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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/**CFile****************************************************************

  FileName    [ivyRewrite.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [And-Inverter Graph package.]

  Synopsis    [AIG rewriting.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

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

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

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

#include "ivy.h"

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

static unsigned    Ivy_ManSeqFindTruth( Ivy_Obj_t * pNode, Vec_Int_t * vFront );
static void        Ivy_ManSeqCollectCone( Ivy_Obj_t * pNode, Vec_Int_t * vCone );
static int         Ivy_ManSeqGetCost( Ivy_Man_t * p, Vec_Int_t * vCone );

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

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

  Synopsis    [Performs Boolean rewriting of sequential AIG.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Ivy_ManSeqRewrite( Ivy_Man_t * p, int fUpdateLevel, int fUseZeroCost )
{
    Vec_Int_t * vNodes, * vFront, * vInside, * vTree;
    Ivy_Obj_t * pNode, * pNodeNew, * pTemp;
    int i, k, nCostOld, nCostInt, nCostInt2, nCostNew, RetValue, Entry, nRefsSaved, nInputs;
    int clk, clkCut = 0, clkTru = 0, clkDsd = 0, clkUpd = 0, clkStart = clock();
    unsigned uTruth;

    nInputs  = 4;
    vTree    = Vec_IntAlloc( 8 );
    vFront   = Vec_IntAlloc( 8 );
    vInside  = Vec_IntAlloc( 50 );
    vNodes   = Ivy_ManDfs( p );
    Ivy_ManForEachNodeVec( p, vNodes, pNode, i )
    {
        if ( Ivy_ObjIsBuf(pNode) )
            continue;
        // fix the fanin buffer problem
        Ivy_NodeFixBufferFanins( pNode );
        if ( Ivy_ObjIsBuf(pNode) )
            continue;

        // find one sequential cut rooted at this node
clk = clock();
        Ivy_ManSeqFindCut( pNode, vFront, vInside, nInputs );
clkCut += clock() - clk;
        // compute the truth table of the cut
clk = clock();
        uTruth = Ivy_ManSeqFindTruth( pNode, vFront );
clkTru += clock() - clk;
        // decompose the truth table
clk = clock();
        RetValue = Ivy_TruthDsd( uTruth, vTree );
clkDsd += clock() - clk;
        if ( !RetValue )
            continue; // DSD does not exist
//        Ivy_TruthDsdPrint( stdout, vTree );

clk = clock();
        // remember the cost of the current network
        nCostOld = Ivy_ManGetCost(p);
        // increment references of the cut variables
        Vec_IntForEachEntry( vFront, Entry, k )
        {
            pTemp = Ivy_ManObj(p, Ivy_LeafId(Entry));
            Ivy_ObjRefsInc( pTemp );
        }
        // dereference and record undo
        nRefsSaved = pNode->nRefs; pNode->nRefs = 0;
        Ivy_ManUndoStart( p );
        Ivy_ObjDelete_rec( pNode, 0 );
        Ivy_ManUndoStop( p );
        pNode->nRefs = nRefsSaved;

        // get the intermediate cost
        nCostInt = Ivy_ManGetCost(p);

//        printf( "Removed by dereferencing = %d.\n", nCostOld - nCostInt );

        // construct the new logic cone
        pNodeNew = Ivy_ManDsdConstruct( p, vFront, vTree );
        // remember the cost
        nCostNew = Ivy_ManGetCost(p);

//        printf( "Added by dereferencing = %d.\n", nCostInt - nCostNew );
//        nCostNew = nCostNew;

/*
        if ( Ivy_Regular(pNodeNew)->nRefs == 0 )
            Ivy_ObjDelete_rec( Ivy_Regular(pNodeNew), 1 );
        // get the intermediate cost
        nCostInt2 = Ivy_ManGetCost(p);
        assert( nCostInt == nCostInt2 );

        Ivy_ManUndoPerform( p, pNode );
        pNode->nRefs = nRefsSaved;

        Vec_IntForEachEntry( vFront, Entry, k )
        {
//            pTemp = Ivy_ManObj(p, Ivy_LeafId(Entry));
            pTemp = Ivy_ManObj(p, Entry);
            Ivy_ObjRefsDec( pTemp );
        }
clkUpd += clock() - clk;
        continue;
*/

        // detect the case when they are exactly the same
//        if ( pNodeNew == pNode )
//            continue;

        // compare the costs
        if ( nCostOld > nCostNew || nCostOld == nCostNew && fUseZeroCost )
        { // accept the change
//            printf( "NODES GAINED = %d\n", nCostOld - nCostNew );

            Ivy_ObjReplace( pNode, pNodeNew, 0, 1 );
            pNode->nRefs = nRefsSaved;
        }
        else
        { // reject change
//            printf( "Rejected\n" );

            if ( Ivy_Regular(pNodeNew)->nRefs == 0 )
                Ivy_ObjDelete_rec( Ivy_Regular(pNodeNew), 1 );

            // get the intermediate cost
            nCostInt2 = Ivy_ManGetCost(p);
            assert( nCostInt == nCostInt2 );

            // reconstruct the old node
            Ivy_ManUndoPerform( p, pNode );
            pNode->nRefs = nRefsSaved;
        }
        // decrement references of the cut variables
        Vec_IntForEachEntry( vFront, Entry, k )
        {
//            pTemp = Ivy_ManObj(p, Ivy_LeafId(Entry));
            pTemp = Ivy_ManObj(p, Entry);
            if ( Ivy_ObjIsNone(pTemp) )
                continue;
            Ivy_ObjRefsDec( pTemp );
            if ( Ivy_ObjRefs(pTemp) == 0 )
                Ivy_ObjDelete_rec( pTemp, 1 );
        }

clkUpd += clock() - clk;
    }

PRT( "Cut    ", clkCut );
PRT( "Truth  ", clkTru );
PRT( "DSD    ", clkDsd );
PRT( "Update ", clkUpd );
PRT( "TOTAL  ", clock() - clkStart );

    Vec_IntFree( vTree  );
    Vec_IntFree( vFront );
    Vec_IntFree( vInside );
    Vec_IntFree( vNodes );

    if ( !Ivy_ManCheck(p) )
    {
        printf( "Ivy_ManSeqRewrite(): The check has failed.\n" );
    }
    return 1;
}

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

  Synopsis    [Computes the truth table of the sequential cut.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
unsigned Ivy_ManSeqFindTruth_rec( Ivy_Man_t * p, unsigned Node, Vec_Int_t * vFront )
{
    static unsigned uMasks[5] = { 0xAAAAAAAA, 0xCCCCCCCC, 0xF0F0F0F0, 0xFF00FF00, 0xFFFF0000 };
    unsigned uTruth0, uTruth1;
    Ivy_Obj_t * pNode;
    int nLatches, Number;
    // consider the case of a constant
    if ( Node == 0 )
        return ~((unsigned)0);
    // try to find this node in the frontier
    Number = Vec_IntFind( vFront, Node );
    if ( Number >= 0 )
        return uMasks[Number];
    // get the node
    pNode = Ivy_ManObj( p, Ivy_LeafId(Node) );
    assert( !Ivy_ObjIsPi(pNode) && !Ivy_ObjIsConst1(pNode) );
    // get the number of latches
    nLatches = Ivy_LeafLat(Node) + Ivy_ObjIsLatch(pNode);
    // expand the first fanin
    uTruth0 = Ivy_ManSeqFindTruth_rec( p, Ivy_LeafCreate(Ivy_ObjFaninId0(pNode), nLatches), vFront );
    if ( Ivy_ObjFaninC0(pNode) )
        uTruth0 = ~uTruth0;
    // quit if this is the one fanin node
    if ( Ivy_ObjIsLatch(pNode) || Ivy_ObjIsBuf(pNode) )
        return uTruth0;
    assert( Ivy_ObjIsNode(pNode) );
    // expand the second fanin
    uTruth1 = Ivy_ManSeqFindTruth_rec( p, Ivy_LeafCreate(Ivy_ObjFaninId1(pNode), nLatches), vFront );
    if ( Ivy_ObjFaninC1(pNode) )
        uTruth1 = ~uTruth1;
    // return the truth table
    return Ivy_ObjIsAnd(pNode)? uTruth0 & uTruth1 : uTruth0 ^ uTruth1;
}

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

  Synopsis    [Computes the truth table of the sequential cut.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
unsigned Ivy_ManSeqFindTruth( Ivy_Obj_t * pNode, Vec_Int_t * vFront )
{
    assert( Ivy_ObjIsNode(pNode) );
    return Ivy_ManSeqFindTruth_rec( Ivy_ObjMan(pNode), Ivy_LeafCreate(pNode->Id, 0), vFront );
}


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

  Synopsis    [Recursively dereferences the node.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Ivy_ManSeqDeref_rec( Ivy_Obj_t * pNode, Vec_Int_t * vCone )
{
    Ivy_Obj_t * pFan;
    assert( !Ivy_IsComplement(pNode) );
    assert( !Ivy_ObjIsNone(pNode) );
    if ( Ivy_ObjIsPi(pNode) )
        return;
    // deref the first fanin
    pFan = Ivy_ObjFanin0(pNode);
    Ivy_ObjRefsDec( pFan );
    if ( Ivy_ObjRefs( pFan ) == 0 )
        Ivy_ManSeqDeref_rec( pFan, vCone );
    // deref the second fanin
    pFan = Ivy_ObjFanin1(pNode);
    Ivy_ObjRefsDec( pFan );
    if ( Ivy_ObjRefs( pFan ) == 0 )
        Ivy_ManSeqDeref_rec( pFan, vCone );
    // save the node
    Vec_IntPush( vCone, pNode->Id );
}

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

  Synopsis    [Recursively references the node.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Ivy_ManSeqRef_rec( Ivy_Obj_t * pNode )
{
    Ivy_Obj_t * pFan;
    assert( !Ivy_IsComplement(pNode) );
    assert( !Ivy_ObjIsNone(pNode) );
    if ( Ivy_ObjIsPi(pNode) )
        return;
    // ref the first fanin
    pFan = Ivy_ObjFanin0(pNode);
    if ( Ivy_ObjRefs( pFan ) == 0 )
        Ivy_ManSeqRef_rec( pFan );
    Ivy_ObjRefsInc( pFan );
    // ref the second fanin
    pFan = Ivy_ObjFanin1(pNode);
    if ( Ivy_ObjRefs( pFan ) == 0 )
        Ivy_ManSeqRef_rec( pFan );
    Ivy_ObjRefsInc( pFan );
}

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

  Synopsis    [Collect MFFC of the node.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Ivy_ManSeqCollectCone( Ivy_Obj_t * pNode, Vec_Int_t * vCone )
{
    Vec_IntClear( vCone );
    Ivy_ManSeqDeref_rec( pNode, vCone );
    Ivy_ManSeqRef_rec( pNode );
}

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

  Synopsis    [Computes the cost of the logic cone.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Ivy_ManSeqGetCost( Ivy_Man_t * p, Vec_Int_t * vCone )
{
    Ivy_Obj_t * pObj;
    int i, Cost = 0;
    Ivy_ManForEachNodeVec( p, vCone, pObj, i )
    {
        if ( Ivy_ObjIsAnd(pObj) )
            Cost += 1;
        else if ( Ivy_ObjIsExor(pObj) )
            Cost += 2;
    }
    return 0;
}

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