summaryrefslogtreecommitdiffstats
path: root/src/base/abc/abcReconv.c
blob: 82adc92c27db4cad94d9fcea6a7d9cbd800677ed (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
/**CFile****************************************************************

  FileName    [abcRes.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [Network and node package.]

  Synopsis    [Reconvergence=driven cut computation.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

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

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

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

#include "abc.h"
#include "ft.h"

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

struct Abc_ManCut_t_
{
    // user specified parameters
    int              nNodeSizeMax;  // the limit on the size of the supernode
    int              nConeSizeMax;  // the limit on the size of the containing cone
    // internal parameters
    Vec_Ptr_t *      vFaninsNode;   // fanins of the supernode
    Vec_Ptr_t *      vInsideNode;   // inside of the supernode
    Vec_Ptr_t *      vFaninsCone;   // fanins of the containing cone
    Vec_Ptr_t *      vInsideCone;   // inside of the containing cone
    Vec_Ptr_t *      vVisited;      // the visited nodes
};

static int           Abc_NodeFindCut_int( Vec_Ptr_t * vInside, Vec_Ptr_t * vFanins, int nSizeLimit );
static int           Abc_NodeGetFaninCost( Abc_Obj_t * pNode );
static void          Abc_NodeConeMarkCollect_rec( Abc_Obj_t * pNode, Vec_Ptr_t * vVisited );
static void          Abc_NodeConeMark( Vec_Ptr_t * vVisited );
static void          Abc_NodeConeUnmark( Vec_Ptr_t * vVisited );

////////////////////////////////////////////////////////////////////////
///                     FUNCTION DEFITIONS                           ///
////////////////////////////////////////////////////////////////////////

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

  Synopsis    [Finds a reconvergence-driven cut.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Vec_Ptr_t * Abc_NodeFindCut( Abc_ManCut_t * p, Abc_Obj_t * pRoot )
{
    Abc_Obj_t * pNode;
    int i;

    // mark TFI using fMarkA
    Vec_PtrClear( p->vVisited );
    Abc_NodeConeMarkCollect_rec( pRoot, p->vVisited );

    // start the reconvergence-driven node
    Vec_PtrClear( p->vInsideNode );
    Vec_PtrClear( p->vFaninsNode );
    Vec_PtrPush( p->vFaninsNode, pRoot );
    pRoot->fMarkB = 1;

    // compute reconvergence-driven node
    while ( Abc_NodeFindCut_int( p->vInsideNode, p->vFaninsNode, p->nNodeSizeMax ) );

    // compute reconvergence-driven cone
    Vec_PtrClear( p->vInsideCone );
    Vec_PtrClear( p->vFaninsCone );
    if ( p->nConeSizeMax > p->nNodeSizeMax )
    {
        // copy the node into the cone
        Vec_PtrForEachEntry( p->vInsideNode, pNode, i )
            Vec_PtrPush( p->vInsideCone, pNode );
        Vec_PtrForEachEntry( p->vFaninsNode, pNode, i )
            Vec_PtrPush( p->vFaninsCone, pNode );
        // compute reconvergence-driven cone
        while ( Abc_NodeFindCut_int( p->vInsideCone, p->vFaninsCone, p->nConeSizeMax ) );
        // unmark the nodes of the sets
        Vec_PtrForEachEntry( p->vInsideCone, pNode, i )
            pNode->fMarkB = 0;
        Vec_PtrForEachEntry( p->vFaninsCone, pNode, i )
            pNode->fMarkB = 0;
    }
    else
    {
        // unmark the nodes of the sets
        Vec_PtrForEachEntry( p->vInsideNode, pNode, i )
            pNode->fMarkB = 0;
        Vec_PtrForEachEntry( p->vFaninsNode, pNode, i )
            pNode->fMarkB = 0;
    }

    // unmark TFI using fMarkA
    Abc_NodeConeUnmark( p->vVisited );
    return p->vFaninsNode;
}

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

  Synopsis    [Finds a reconvergence-driven cut.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Abc_NodeFindCut_int( Vec_Ptr_t * vInside, Vec_Ptr_t * vFanins, int nSizeLimit )
{
    Abc_Obj_t * pNode, * pFaninBest, * pNext;
    int CostBest, CostCur, i;
    // find the best fanin
    CostBest   = 100;
    pFaninBest = NULL;
    Vec_PtrForEachEntry( vFanins, pNode, i )
    {
        CostCur = Abc_NodeGetFaninCost( pNode );
        if ( CostBest > CostCur )
        {
            CostBest   = CostCur;
            pFaninBest = pNode;
        }
    }
    if ( pFaninBest == NULL )
        return 0;
    assert( CostBest < 3 );
    if ( vFanins->nSize - 1 + CostBest > nSizeLimit )
        return 0;
    assert( Abc_ObjIsNode(pFaninBest) );
    // remove the node from the array
    Vec_PtrRemove( vFanins, pFaninBest );
    // add the node to the set
    Vec_PtrPush( vInside, pFaninBest );
    // add the left child to the fanins
    pNext = Abc_ObjFanin0(pFaninBest);
    if ( !pNext->fMarkB )
    {
        pNext->fMarkB = 1;
        Vec_PtrPush( vFanins, pNext );
    }
    // add the right child to the fanins
    pNext = Abc_ObjFanin1(pFaninBest);
    if ( !pNext->fMarkB )
    {
        pNext->fMarkB = 1;
        Vec_PtrPush( vFanins, pNext );
    }
    assert( vFanins->nSize <= nSizeLimit );
    // keep doing this
    return 1;
}

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

  Synopsis    [Evaluate the fanin cost.]

  Description [Returns the number of fanins that will be brought in.
  Returns large number if the node cannot be added.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Abc_NodeGetFaninCost( Abc_Obj_t * pNode )
{
    Abc_Obj_t * pFanout;
    int i;
    assert( pNode->fMarkA == 1 );  // this node is in the TFI
    assert( pNode->fMarkB == 1 );  // this node is in the constructed cone
    // check the PI node
    if ( !Abc_ObjIsNode(pNode) )
        return 999;
    // check the fanouts
    Abc_ObjForEachFanout( pNode, pFanout, i )
        if ( pFanout->fMarkA && pFanout->fMarkB == 0 ) // in the cone but not in the set
            return 999;
    // the fanouts are in the TFI and inside the constructed cone
    // return the number of fanins that will be on the boundary if this node is added
    return (!Abc_ObjFanin0(pNode)->fMarkB) + (!Abc_ObjFanin1(pNode)->fMarkB);
}

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

  Synopsis    [Marks the TFI cone.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Abc_NodeConeMarkCollect_rec( Abc_Obj_t * pNode, Vec_Ptr_t * vVisited )
{
    if ( pNode->fMarkA == 1 )
        return;
    // visit transitive fanin 
    if ( Abc_ObjIsNode(pNode) )
    {
        Abc_NodeConeMarkCollect_rec( Abc_ObjFanin0(pNode), vVisited );
        Abc_NodeConeMarkCollect_rec( Abc_ObjFanin1(pNode), vVisited );
    }
    assert( pNode->fMarkA == 0 );
    pNode->fMarkA = 1;
    Vec_PtrPush( vVisited, pNode );
}

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

  Synopsis    [Unmarks the TFI cone.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Abc_NodeConeMark( Vec_Ptr_t * vVisited )
{
    int i;
    for ( i = 0; i < vVisited->nSize; i++ )
        ((Abc_Obj_t *)vVisited->pArray)->fMarkA = 1;
}

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

  Synopsis    [Unmarks the TFI cone.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Abc_NodeConeUnmark( Vec_Ptr_t * vVisited )
{
    int i;
    for ( i = 0; i < vVisited->nSize; i++ )
        ((Abc_Obj_t *)vVisited->pArray)->fMarkA = 0;
}


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

  Synopsis    [Returns BDD representing the logic function of the cone.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
DdNode * Abc_NodeConeBdd( DdManager * dd, DdNode ** pbVars, Abc_Obj_t * pNode, Vec_Ptr_t * vFanins, Vec_Ptr_t * vVisited )
{
    DdNode * bFunc0, * bFunc1, * bFunc;
    int i;
    // mark the fanins of the cone
    Abc_NodeConeMark( vFanins );
    // collect the nodes in the DFS order
    Vec_PtrClear( vVisited );
    Abc_NodeConeMarkCollect_rec( pNode, vVisited );
    // unmark both sets
    Abc_NodeConeUnmark( vFanins );
    Abc_NodeConeUnmark( vVisited );
    // set the elementary BDDs
    Vec_PtrForEachEntry( vFanins, pNode, i )
        pNode->pCopy = (Abc_Obj_t *)pbVars[i];
    // compute the BDDs for the collected nodes
    Vec_PtrForEachEntry( vVisited, pNode, i )
    {
        bFunc0 = Cudd_NotCond( Abc_ObjFanin0(pNode)->pCopy, Abc_ObjFaninC0(pNode) );
        bFunc1 = Cudd_NotCond( Abc_ObjFanin1(pNode)->pCopy, Abc_ObjFaninC1(pNode) );
        bFunc  = Cudd_bddAnd( dd, bFunc0, bFunc1 );    Cudd_Ref( bFunc );
        pNode->pCopy = (Abc_Obj_t *)bFunc;
    }
    Cudd_Ref( bFunc );
    // dereference the intermediate ones
    Vec_PtrForEachEntry( vVisited, pNode, i )
        Cudd_RecursiveDeref( dd, (DdNode *)pNode->pCopy );
    Cudd_Deref( bFunc );
    return bFunc;
}

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

  Synopsis    [Starts the resynthesis manager.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Abc_ManCut_t * Abc_NtkManCutStart( int nNodeSizeMax, int nConeSizeMax )
{
    Abc_ManCut_t * p;
    p = ALLOC( Abc_ManCut_t, 1 );
    memset( p, 0, sizeof(Abc_ManCut_t) );
    p->vFaninsNode  = Vec_PtrAlloc( 100 );
    p->vInsideNode  = Vec_PtrAlloc( 100 );
    p->vFaninsCone  = Vec_PtrAlloc( 100 );
    p->vInsideCone  = Vec_PtrAlloc( 100 );
    p->vVisited     = Vec_PtrAlloc( 100 );
    p->nNodeSizeMax = nNodeSizeMax;
    p->nConeSizeMax = nConeSizeMax;
    return p;
}

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

  Synopsis    [Stops the resynthesis manager.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Abc_NtkManCutStop( Abc_ManCut_t * p )
{
    Vec_PtrFree( p->vFaninsNode );
    Vec_PtrFree( p->vInsideNode );
    Vec_PtrFree( p->vFaninsCone );
    Vec_PtrFree( p->vInsideCone );
    Vec_PtrFree( p->vVisited    );
    free( p );
}




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

  Synopsis    [Collects the TFO of the cut in the topological order.]

  Description [TFO of the cut is defined as a set of nodes, for which the cut
  is a cut, that is, every path from the collected nodes to the CIs goes through 
  a node in the cut. The nodes are collected if their level does not exceed
  the given number (LevelMax). The nodes are returned in the topological order.
  If the root node is given, its MFFC is marked, so that the collected nodes
  do not contain any nodes in the MFFC.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Abc_NodeCollectTfoCands( Abc_Ntk_t * pNtk, Abc_Obj_t * pRoot, 
    Vec_Ptr_t * vFanins, int LevelMax, Vec_Vec_t * vLevels, Vec_Ptr_t * vResult )
{
    Vec_Ptr_t * vVec;
    Abc_Obj_t * pNode, * pFanout;
    int i, k, v, LevelMin;
    assert( Abc_NtkIsAig(pNtk) );

    // assuming that the structure is clean
    Vec_VecForEachLevel( vLevels, vVec, i )
        assert( vVec->nSize == 0 );

    // put fanins into the structure while labeling them
    Abc_NtkIncrementTravId( pNtk );
    LevelMin = ABC_INFINITY;
    Vec_PtrForEachEntry( vFanins, pNode, i )
    {
        if ( pNode->Level > (unsigned)LevelMax )
            continue;
        Abc_NodeSetTravIdCurrent( pNode );
        Vec_VecPush( vLevels, pNode->Level, pNode );
        if ( LevelMin < (int)pNode->Level )
            LevelMin = pNode->Level;
    }
    assert( LevelMin >= 0 );

    // mark MFFC 
    if ( pRoot )
        Abc_NodeMffcLabel( pRoot );

    // go through the levels up
    Vec_PtrClear( vResult );
    Vec_VecForEachEntryStartStop( vLevels, pNode, i, k, LevelMin, LevelMax )
    {
        // if the node is not marked, it is not a fanin
        if ( !Abc_NodeIsTravIdCurrent(pNode) )
        {
            // check if it belongs to the TFO
            if ( !Abc_NodeIsTravIdCurrent(Abc_ObjFanin0(pNode)) || 
                 !Abc_NodeIsTravIdCurrent(Abc_ObjFanin1(pNode)) )
                 continue;
            // save the node in the TFO and label it
            Vec_PtrPush( vResult, pNode );
            Abc_NodeSetTravIdCurrent( pNode );
        }
        // go through the fanouts and add them to the structure if they meet the conditions
        Abc_ObjForEachFanout( pNode, pFanout, v )
        {
            // skip if fanout is a CO or its level exceeds
            if ( Abc_ObjIsCo(pFanout) || pFanout->Level > (unsigned)LevelMax )
                continue;
            // skip if it is already added or if it is in MFFC
            if ( Abc_NodeIsTravIdCurrent(pFanout) )
                continue;
            // add it to the structure but do not mark it (until tested later)
            Vec_VecPush( vLevels, pFanout->Level, pFanout );
        }
    }

    // clear the levelized structure
    Vec_VecForEachLevelStartStop( vLevels, vVec, i, LevelMin, LevelMax )
        Vec_PtrClear( vVec );
}

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