summaryrefslogtreecommitdiffstats
path: root/src/aig/ivy/ivyCutTrav.c
blob: 74212b1062ed8bb872353ed5c4122bef29aeaa4d (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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
/**CFile****************************************************************

  FileName    [ivyCutTrav.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 - May 11, 2006.]

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

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

#include "ivy.h"

ABC_NAMESPACE_IMPL_START


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

static unsigned * Ivy_NodeCutElementary( Vec_Int_t * vStore, int nWords, int NodeId );
static void Ivy_NodeComputeVolume( Ivy_Obj_t * pObj, int nNodeLimit, Vec_Ptr_t * vNodes, Vec_Ptr_t * vFront );
static void Ivy_NodeFindCutsMerge( Vec_Ptr_t * vCuts0, Vec_Ptr_t * vCuts1, Vec_Ptr_t * vCuts, int nLeaves, int nWords, Vec_Int_t * vStore );

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

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

  Synopsis    [Computes cuts for one node.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Ivy_Store_t * Ivy_NodeFindCutsTravAll( Ivy_Man_t * p, Ivy_Obj_t * pObj, int nLeaves, int nNodeLimit, 
                                      Vec_Ptr_t * vNodes, Vec_Ptr_t * vFront, Vec_Int_t * vStore, Vec_Vec_t * vBitCuts )
{
    static Ivy_Store_t CutStore, * pCutStore = &CutStore;
    Vec_Ptr_t * vCuts, * vCuts0, * vCuts1;
    unsigned * pBitCut;
    Ivy_Obj_t * pLeaf;
    Ivy_Cut_t * pCut;
    int i, k, nWords, nNodes;

    assert( nLeaves <= IVY_CUT_INPUT );

    // find the given number of nodes in the TFI
    Ivy_NodeComputeVolume( pObj, nNodeLimit - 1, vNodes, vFront );
    nNodes = Vec_PtrSize(vNodes);
//    assert( nNodes <= nNodeLimit );

    // make sure vBitCuts has enough room
    Vec_VecExpand( vBitCuts, nNodes-1 );
    Vec_VecClear( vBitCuts );

    // prepare the memory manager
    Vec_IntClear( vStore );
    Vec_IntGrow( vStore, 64000 );

    // set elementary cuts for the leaves
    nWords = Extra_BitWordNum( nNodes );
    Vec_PtrForEachEntry( Ivy_Obj_t *, vFront, pLeaf, i )
    {
        assert( Ivy_ObjTravId(pLeaf) < nNodes );
        // get the new bitcut
        pBitCut = Ivy_NodeCutElementary( vStore, nWords, Ivy_ObjTravId(pLeaf) );
        // set it as the cut of this leaf
        Vec_VecPush( vBitCuts, Ivy_ObjTravId(pLeaf), pBitCut );
    }

    // compute the cuts for each node
    Vec_PtrForEachEntry( Ivy_Obj_t *, vNodes, pLeaf, i )
    {
        // skip the leaves
        vCuts = Vec_VecEntry( vBitCuts, Ivy_ObjTravId(pLeaf) );
        if ( Vec_PtrSize(vCuts) > 0 )
            continue;
        // add elementary cut
        pBitCut = Ivy_NodeCutElementary( vStore, nWords, Ivy_ObjTravId(pLeaf) );
        // set it as the cut of this leaf
        Vec_VecPush( vBitCuts, Ivy_ObjTravId(pLeaf), pBitCut );
        // get the fanin cuts
        vCuts0 = Vec_VecEntry( vBitCuts, Ivy_ObjTravId( Ivy_ObjFanin0(pLeaf) ) );
        vCuts1 = Vec_VecEntry( vBitCuts, Ivy_ObjTravId( Ivy_ObjFanin1(pLeaf) ) );
        assert( Vec_PtrSize(vCuts0) > 0 );
        assert( Vec_PtrSize(vCuts1) > 0 );
        // merge the cuts
        Ivy_NodeFindCutsMerge( vCuts0, vCuts1, vCuts, nLeaves, nWords, vStore );
    }

    // start the structure
    pCutStore->nCuts = 0;
    pCutStore->nCutsMax = IVY_CUT_LIMIT;
    // collect the cuts of the root node
    vCuts = Vec_VecEntry( vBitCuts, Ivy_ObjTravId(pObj) );
    Vec_PtrForEachEntry( unsigned *, vCuts, pBitCut, i )
    {
        pCut = pCutStore->pCuts + pCutStore->nCuts++;
        pCut->nSize = 0;
        pCut->nSizeMax = nLeaves;
        pCut->uHash = 0;
        for ( k = 0; k < nNodes; k++ )
            if ( Extra_TruthHasBit(pBitCut, k) )
                pCut->pArray[ pCut->nSize++ ] = Ivy_ObjId( (Ivy_Obj_t *)Vec_PtrEntry(vNodes, k) );
        assert( pCut->nSize <= nLeaves );
        if ( pCutStore->nCuts == pCutStore->nCutsMax )
            break;
    }

    // clean the travIds
    Vec_PtrForEachEntry( Ivy_Obj_t *, vNodes, pLeaf, i )
        pLeaf->TravId = 0;
    return pCutStore;
}

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

  Synopsis    [Creates elementary bit-cut.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
unsigned * Ivy_NodeCutElementary( Vec_Int_t * vStore, int nWords, int NodeId )
{
    unsigned * pBitCut;
    pBitCut = Vec_IntFetch( vStore, nWords );
    memset( pBitCut, 0, (size_t)(4 * nWords) );
    Extra_TruthSetBit( pBitCut, NodeId );
    return pBitCut;
}

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

  Synopsis    [Compares the node by level.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Ivy_CompareNodesByLevel( Ivy_Obj_t ** ppObj1, Ivy_Obj_t ** ppObj2 )
{
    Ivy_Obj_t * pObj1 = *ppObj1;
    Ivy_Obj_t * pObj2 = *ppObj2;
    if ( pObj1->Level < pObj2->Level )
        return -1;
    if ( pObj1->Level > pObj2->Level )
        return 1;
    return 0;
}

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

  Synopsis    [Mark all nodes up to the given depth.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Ivy_NodeComputeVolumeTrav1_rec( Ivy_Obj_t * pObj, int Depth )
{
    if ( Ivy_ObjIsCi(pObj) || Depth == 0 )
        return;
    Ivy_NodeComputeVolumeTrav1_rec( Ivy_ObjFanin0(pObj), Depth - 1 );
    Ivy_NodeComputeVolumeTrav1_rec( Ivy_ObjFanin1(pObj), Depth - 1 );
    pObj->fMarkA = 1;
}

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

  Synopsis    [Collect the marked nodes.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Ivy_NodeComputeVolumeTrav2_rec( Ivy_Obj_t * pObj, Vec_Ptr_t * vNodes )
{
    if ( !pObj->fMarkA )
        return;
    Ivy_NodeComputeVolumeTrav2_rec( Ivy_ObjFanin0(pObj), vNodes );
    Ivy_NodeComputeVolumeTrav2_rec( Ivy_ObjFanin1(pObj), vNodes );
    Vec_PtrPush( vNodes, pObj );
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Ivy_NodeComputeVolume( Ivy_Obj_t * pObj, int nNodeLimit, Vec_Ptr_t * vNodes, Vec_Ptr_t * vFront )
{
    Ivy_Obj_t * pTemp, * pFanin;
    int i, nNodes;
    // mark nodes up to the given depth
    Ivy_NodeComputeVolumeTrav1_rec( pObj, 6 );
    // collect the marked nodes
    Vec_PtrClear( vFront );
    Ivy_NodeComputeVolumeTrav2_rec( pObj, vFront );
    // find the fanins that are not marked
    Vec_PtrClear( vNodes );
    Vec_PtrForEachEntry( Ivy_Obj_t *, vFront, pTemp, i )
    {
        pFanin = Ivy_ObjFanin0(pTemp);
        if ( !pFanin->fMarkA )
        {
            pFanin->fMarkA = 1;
            Vec_PtrPush( vNodes, pFanin );
        }
        pFanin = Ivy_ObjFanin1(pTemp);
        if ( !pFanin->fMarkA )
        {
            pFanin->fMarkA = 1;
            Vec_PtrPush( vNodes, pFanin );
        }
    }
    // remember the number of nodes in the frontier
    nNodes = Vec_PtrSize( vNodes );
    // add the remaining nodes
    Vec_PtrForEachEntry( Ivy_Obj_t *, vFront, pTemp, i )
        Vec_PtrPush( vNodes, pTemp );
    // unmark the nodes
    Vec_PtrForEachEntry( Ivy_Obj_t *, vNodes, pTemp, i )
    {
        pTemp->fMarkA = 0;
        pTemp->TravId = i;
    }
    // collect the frontier nodes
    Vec_PtrClear( vFront );
    Vec_PtrForEachEntryStop( Ivy_Obj_t *, vNodes, pTemp, i, nNodes )
        Vec_PtrPush( vFront, pTemp );
//    printf( "%d ", Vec_PtrSize(vNodes) );
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Ivy_NodeComputeVolume2( Ivy_Obj_t * pObj, int nNodeLimit, Vec_Ptr_t * vNodes, Vec_Ptr_t * vFront )
{
    Ivy_Obj_t * pLeaf, * pPivot, * pFanin;
    int LevelMax, i;
    assert( Ivy_ObjIsNode(pObj) );
    // clear arrays
    Vec_PtrClear( vNodes );
    Vec_PtrClear( vFront );
    // add the root
    pObj->fMarkA = 1;
    Vec_PtrPush( vNodes, pObj );
    Vec_PtrPush( vFront, pObj );
    // expand node with maximum level
    LevelMax = pObj->Level;
    do {
        // get the node to expand
        pPivot = NULL;
        Vec_PtrForEachEntryReverse( Ivy_Obj_t *, vFront, pLeaf, i )
        {
            if ( (int)pLeaf->Level == LevelMax )
            {
                pPivot = pLeaf;
                break;
            }
        }
        // decrease level if we did not find the node
        if ( pPivot == NULL )
        {
            if ( --LevelMax == 0 )
                break;
            continue;
        }
        // the node to expand is found
        // remove it from frontier
        Vec_PtrRemove( vFront, pPivot );
        // add fanins
        pFanin = Ivy_ObjFanin0(pPivot); 
        if ( !pFanin->fMarkA )
        {
            pFanin->fMarkA = 1;
            Vec_PtrPush( vNodes, pFanin );
            Vec_PtrPush( vFront, pFanin );
        }
        pFanin = Ivy_ObjFanin1(pPivot); 
        if ( pFanin && !pFanin->fMarkA )
        {
            pFanin->fMarkA = 1;
            Vec_PtrPush( vNodes, pFanin );
            Vec_PtrPush( vFront, pFanin );
        }
        // quit if we collected enough nodes
    } while ( Vec_PtrSize(vNodes) < nNodeLimit );

    // sort nodes by level
    Vec_PtrSort( vNodes, (int (*)(void))Ivy_CompareNodesByLevel );
    // make sure the nodes are ordered in the increasing number of levels
    pFanin = (Ivy_Obj_t *)Vec_PtrEntry( vNodes, 0 );
    pPivot = (Ivy_Obj_t *)Vec_PtrEntryLast( vNodes );
    assert( pFanin->Level <= pPivot->Level );

    // clean the marks and remember node numbers in the TravId
    Vec_PtrForEachEntry( Ivy_Obj_t *, vNodes, pFanin, i )
    {
        pFanin->fMarkA = 0;
        pFanin->TravId = i;
    }
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline void Extra_TruthOrWords( unsigned * pOut, unsigned * pIn0, unsigned * pIn1, int nWords )
{
    int w;
    for ( w = nWords-1; w >= 0; w-- )
        pOut[w] = pIn0[w] | pIn1[w];
}
static inline int Extra_TruthIsImplyWords( unsigned * pIn1, unsigned * pIn2, int nWords )
{
    int w;
    for ( w = nWords-1; w >= 0; w-- )
        if ( pIn1[w] & ~pIn2[w] )
            return 0;
    return 1;
}

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

  Synopsis    [Merges two sets of bit-cuts at a node.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Ivy_NodeFindCutsMerge( Vec_Ptr_t * vCuts0, Vec_Ptr_t * vCuts1, Vec_Ptr_t * vCuts, 
                           int nLeaves, int nWords, Vec_Int_t * vStore )
{
    unsigned * pBitCut, * pBitCut0, * pBitCut1, * pBitCutTest;
    int i, k, c, w, Counter;
    // iterate through the cut pairs
    Vec_PtrForEachEntry( unsigned *, vCuts0, pBitCut0, i )
    Vec_PtrForEachEntry( unsigned *, vCuts1, pBitCut1, k )
    {
        // skip infeasible cuts
        Counter = 0;
        for ( w = 0; w < nWords; w++ )
        {
            Counter += Extra_WordCountOnes( pBitCut0[w] | pBitCut1[w] );
            if ( Counter > nLeaves )
                break;
        }
        if ( Counter > nLeaves )
            continue;
        // the new cut is feasible - create it
        pBitCutTest = Vec_IntFetch( vStore, nWords );
        Extra_TruthOrWords( pBitCutTest, pBitCut0, pBitCut1, nWords );
        // filter contained cuts; try to find containing cut
        w = 0;
        Vec_PtrForEachEntry( unsigned *, vCuts, pBitCut, c )
        {
            if ( Extra_TruthIsImplyWords( pBitCut, pBitCutTest, nWords ) )
                break;
            if ( Extra_TruthIsImplyWords( pBitCutTest, pBitCut, nWords ) )
                continue;
            Vec_PtrWriteEntry( vCuts, w++, pBitCut );
        }
        if ( c != Vec_PtrSize(vCuts) )
            continue;
        Vec_PtrShrink( vCuts, w );
        // add the cut
        Vec_PtrPush( vCuts, pBitCutTest );
    }
}
 
/**Function*************************************************************

  Synopsis    [Compute the set of all cuts.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Ivy_ManTestCutsTravAll( Ivy_Man_t * p )
{
    Ivy_Store_t * pStore;
    Ivy_Obj_t * pObj;
    Vec_Ptr_t * vNodes, * vFront;
    Vec_Int_t * vStore;
    Vec_Vec_t * vBitCuts;
    int i, nCutsCut, nCutsTotal, nNodeTotal, nNodeOver;
    abctime clk = Abc_Clock();

    vNodes = Vec_PtrAlloc( 100 );
    vFront = Vec_PtrAlloc( 100 );
    vStore = Vec_IntAlloc( 100 );
    vBitCuts = Vec_VecAlloc( 100 );

    nNodeTotal = nNodeOver = 0;
    nCutsTotal = -Ivy_ManNodeNum(p);
    Ivy_ManForEachObj( p, pObj, i )
    {
        if ( !Ivy_ObjIsNode(pObj) )
            continue;
        pStore = Ivy_NodeFindCutsTravAll( p, pObj, 4, 60, vNodes, vFront, vStore, vBitCuts );
        nCutsCut    = pStore->nCuts;
        nCutsTotal += nCutsCut;
        nNodeOver  += (nCutsCut == IVY_CUT_LIMIT);
        nNodeTotal++;
    }
    printf( "Total cuts = %6d. Trivial = %6d.   Nodes = %6d. Satur = %6d.  ", 
        nCutsTotal, Ivy_ManPiNum(p) + Ivy_ManNodeNum(p), nNodeTotal, nNodeOver );
    ABC_PRT( "Time", Abc_Clock() - clk );

    Vec_PtrFree( vNodes );
    Vec_PtrFree( vFront );
    Vec_IntFree( vStore );
    Vec_VecFree( vBitCuts );

}

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


ABC_NAMESPACE_IMPL_END