summaryrefslogtreecommitdiffstats
path: root/src/base/abc/abcDfs.c
blob: 5c75a94c5593bc2179239fccf132f19dc0567b07 (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
/**CFile****************************************************************

  FileName    [abcDfs.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [Network and node package.]

  Synopsis    [Procedures that use depth-first search.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

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

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

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

#include "abc.h"

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

static void Abc_NtkDfs_rec( Abc_Obj_t * pNode, Vec_Ptr_t * vNodes );
static void Abc_AigDfs_rec( Abc_Obj_t * pNode, Vec_Ptr_t * vNodes );
static int  Abc_NtkGetLevelNum_rec( Abc_Obj_t * pNode );
static bool Abc_NtkIsAcyclic_rec( Abc_Obj_t * pNode );

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

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

  Synopsis    [Returns the DFS ordered array of logic nodes.]

  Description [Collects only the internal nodes, leaving out PIs, POs and latches.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Vec_Ptr_t * Abc_NtkDfs( Abc_Ntk_t * pNtk )
{
    Vec_Ptr_t * vNodes;
    Abc_Obj_t * pNet, * pNode;
    int i, fMadeComb;
    // set the traversal ID
    Abc_NtkIncrementTravId( pNtk );
    // start the array of nodes
    vNodes = Vec_PtrAlloc( 100 );
    // go through the PO nodes and call for each of them
    if ( Abc_NtkIsNetlist(pNtk) )
    {
        fMadeComb = Abc_NtkMakeComb( pNtk );
        Abc_NtkForEachPo( pNtk, pNet, i )
        {
            if ( Abc_ObjIsCi(pNet) )
                continue;
            Abc_NtkDfs_rec( Abc_ObjFanin0(pNet), vNodes );
        }
        if ( fMadeComb )  Abc_NtkMakeSeq( pNtk );
    }
    else
    {
        Abc_NtkForEachCo( pNtk, pNode, i )
            Abc_NtkDfs_rec( Abc_ObjFanin0(pNode), vNodes );
    }
    return vNodes;
}

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

  Synopsis    [Performs DFS for one node.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Abc_NtkDfs_rec( Abc_Obj_t * pNode, Vec_Ptr_t * vNodes )
{
    Abc_Obj_t * pFanin;
    int i;
    assert( !Abc_ObjIsComplement( pNode ) );
    // skip the PI
    if ( Abc_ObjIsPi(pNode) || Abc_ObjIsLatch(pNode) )
        return;
    assert( Abc_ObjIsNode( pNode ) );

    // if this node is already visited, skip
    if ( Abc_NodeIsTravIdCurrent( pNode ) )
        return;
    // mark the node as visited
    Abc_NodeSetTravIdCurrent( pNode );

    // visit the transitive fanin of the node
    if ( Abc_NtkIsNetlist(pNode->pNtk) )
    {
        Abc_ObjForEachFanin( pNode, pFanin, i )
        {
            if ( Abc_ObjIsCi(pFanin) )
                continue;
            pFanin = Abc_ObjFanin0(pFanin);
            Abc_NtkDfs_rec( pFanin, vNodes );
        }
    }
    else
    {
        Abc_ObjForEachFanin( pNode, pFanin, i )
            Abc_NtkDfs_rec( pFanin, vNodes );
    }
    // add the node after the fanins have been added
    Vec_PtrPush( vNodes, pNode );
}


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

  Synopsis    [Returns the DFS ordered array of logic nodes.]

  Description [Collects only the internal nodes, leaving out PIs, POs and latches.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Vec_Ptr_t * Abc_AigDfs( Abc_Ntk_t * pNtk )
{
    Vec_Ptr_t * vNodes;
    Abc_Obj_t * pNode;
    int i;
    assert( Abc_NtkIsAig(pNtk) );
    // set the traversal ID
    Abc_NtkIncrementTravId( pNtk );
    // start the array of nodes
    vNodes = Vec_PtrAlloc( 100 );
    // go through the PO nodes and call for each of them
    Abc_NtkForEachCo( pNtk, pNode, i )
        Abc_AigDfs_rec( Abc_ObjFanin0(pNode), vNodes );
    return vNodes;
}

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

  Synopsis    [Performs DFS for one node.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Abc_AigDfs_rec( Abc_Obj_t * pNode, Vec_Ptr_t * vNodes )
{
    Abc_Obj_t * pFanin;
    int i;
    assert( !Abc_ObjIsComplement( pNode ) );
    // skip the PI
    if ( Abc_ObjIsPi(pNode) || Abc_ObjIsLatch(pNode) )
        return;
    assert( Abc_ObjIsNode( pNode ) );
    // if this node is already visited, skip
    if ( Abc_NodeIsTravIdCurrent( pNode ) )
        return;
    // mark the node as visited
    Abc_NodeSetTravIdCurrent( pNode );
    // visit the transitive fanin of the node
    Abc_ObjForEachFanin( pNode, pFanin, i )
        Abc_AigDfs_rec( pFanin, vNodes );
    // visit the equivalent nodes
    if ( Abc_NodeIsChoice( pNode ) )
        for ( pFanin = pNode->pData; pFanin; pFanin = pFanin->pData )
            Abc_AigDfs_rec( pFanin, vNodes );
    // add the node after the fanins have been added
    Vec_PtrPush( vNodes, pNode );
}



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

  Synopsis    [Computes the number of logic levels not counting PIs/POs.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Abc_NtkGetLevelNum( Abc_Ntk_t * pNtk )
{
    Abc_Obj_t * pNet, * pNode, * pDriver;
    unsigned LevelsMax;
    int i, fMadeComb;
    
    // set the traversal ID for this traversal
    Abc_NtkIncrementTravId( pNtk );
    // perform the traversal
    LevelsMax = 0;
    if ( Abc_NtkIsNetlist(pNtk) )
    {
        fMadeComb = Abc_NtkMakeComb( pNtk );
        Abc_NtkForEachPo( pNtk, pNet, i )
        {
            if ( Abc_ObjIsCi(pNet) )
                continue;
            pDriver = Abc_ObjFanin0( pNet );
            Abc_NtkGetLevelNum_rec( pDriver );
            if ( LevelsMax < pDriver->Level )
                LevelsMax = pDriver->Level;
        }
        if ( fMadeComb )  Abc_NtkMakeSeq( pNtk );
    }
    else
    {
//        Abc_NtkForEachCo( pNtk, pNode, i )
        Abc_NtkForEachNode( pNtk, pNode, i )
        {
//            pDriver = Abc_ObjFanin0( pNode );
            pDriver = pNode;

            Abc_NtkGetLevelNum_rec( pDriver );
            if ( LevelsMax < pDriver->Level )
                LevelsMax = pDriver->Level;
        }
    }
    return LevelsMax;
}

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

  Synopsis    [Recursively counts the number of logic levels of one node.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Abc_NtkGetLevelNum_rec( Abc_Obj_t * pNode )
{
    Abc_Obj_t * pFanin;
    int i;
    assert( !Abc_ObjIsComplement( pNode ) );
    // skip the PI
    if ( Abc_ObjIsPi(pNode) || Abc_ObjIsLatch(pNode) )
        return 0;
    assert( Abc_ObjIsNode( pNode ) );

    // if this node is already visited, return
    if ( Abc_NodeIsTravIdCurrent( pNode ) )
        return pNode->Level;
    // mark the node as visited
    Abc_NodeSetTravIdCurrent( pNode );

    // visit the transitive fanin
    pNode->Level = 0;
    if ( Abc_NtkIsNetlist(pNode->pNtk) )
    {
        Abc_ObjForEachFanin( pNode, pFanin, i )
        {
            if ( Abc_ObjIsCi(pFanin) )
                continue;
            pFanin = Abc_ObjFanin0(pFanin);
            Abc_NtkGetLevelNum_rec( pFanin );
            if ( pNode->Level < pFanin->Level )
                pNode->Level = pFanin->Level;
        }
    }
    else
    {
        Abc_ObjForEachFanin( pNode, pFanin, i )
        {
            Abc_NtkGetLevelNum_rec( pFanin );
            if ( pNode->Level < pFanin->Level )
                pNode->Level = pFanin->Level;
        }
    }
    pNode->Level++;
    return pNode->Level;
}


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

  Synopsis    [Detects combinational loops.]

  Description [This procedure is based on the idea suggested by Donald Chai. 
  As we traverse the network and visit the nodes, we need to distinquish 
  three types of nodes: (1) those that are visited for the first time, 
  (2) those that have been visited in this traversal but are currently not 
  on the traversal path, (3) those that have been visited and are currently 
  on the travesal path. When the node of type (3) is encountered, it means 
  that there is a combinational loop. To mark the three types of nodes, 
  two new values of the traversal IDs are used.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
bool Abc_NtkIsAcyclic( Abc_Ntk_t * pNtk )
{
    Abc_Obj_t * pNet, * pNode;
    int fAcyclic, fMadeComb, i;
    // set the traversal ID for this DFS ordering
    Abc_NtkIncrementTravId( pNtk );   
    Abc_NtkIncrementTravId( pNtk );   
    // pNode->TravId == pNet->nTravIds      means "pNode is on the path"
    // pNode->TravId == pNet->nTravIds - 1  means "pNode is visited but is not on the path"
    // pNode->TravId < pNet->nTravIds - 1   means "pNode is not visited"
    // traverse the network to detect cycles
    fAcyclic = 1;
    if ( Abc_NtkIsNetlist(pNtk) )
    {
        fMadeComb = Abc_NtkMakeComb( pNtk );
        Abc_NtkForEachPo( pNtk, pNet, i )
        {
            if ( Abc_ObjIsCi(pNet) )
                continue;
            pNode = Abc_ObjFanin0( pNet );
            if ( Abc_NodeIsTravIdPrevious(pNode) )
                continue;
            // traverse the output logic cone to detect the combinational loops
            if ( (fAcyclic = Abc_NtkIsAcyclic_rec( pNode )) == 0 ) 
            { // stop as soon as the first loop is detected
                fprintf( stdout, " (the output node)\n" );
                break;
            }
        }
        if ( fMadeComb )  Abc_NtkMakeSeq( pNtk );
    }
    else
    {
        Abc_NtkForEachCo( pNtk, pNode, i )
        {
            pNode = Abc_ObjFanin0( pNode );
            if ( Abc_NodeIsTravIdPrevious(pNode) )
                continue;
            // traverse the output logic cone to detect the combinational loops
            if ( (fAcyclic = Abc_NtkIsAcyclic_rec( pNode )) == 0 ) 
            { // stop as soon as the first loop is detected
                fprintf( stdout, " (the output node)\n" );
                break;
            }
        }
    }
    return fAcyclic;
}

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

  Synopsis    [Recursively detects combinational loops.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
bool Abc_NtkIsAcyclic_rec( Abc_Obj_t * pNode )
{
    Abc_Ntk_t * pNtk = pNode->pNtk;
    Abc_Obj_t * pFanin;
    int fAcyclic, i;

    assert( !Abc_ObjIsComplement( pNode ) );
    // skip the PI
    if ( Abc_ObjIsPi(pNode) || Abc_ObjIsLatch(pNode) )
        return 1;
    assert( Abc_ObjIsNode( pNode ) );

    // make sure the node is not visited
    assert( !Abc_NodeIsTravIdPrevious(pNode) );
    // check if the node is part of the combinational loop
    if ( Abc_NodeIsTravIdCurrent(pNode) )
    {
        fprintf( stdout, "Network \"%s\" contains combinational loop!\n", pNtk->pName );
        fprintf( stdout, "Node \"%s\" is encountered twice on the following path:\n", Abc_ObjName(pNode) );
        fprintf( stdout, " %s", Abc_ObjName(pNode) );
        return 0;
    }
    // mark this node as a node on the current path
    Abc_NodeSetTravIdCurrent( pNode );

    // visit the transitive fanin
    if ( Abc_NtkIsNetlist(pNode->pNtk) )
    {
        Abc_ObjForEachFanin( pNode, pFanin, i )
        {
            if ( Abc_ObjIsCi(pFanin) )
                continue;
            pFanin = Abc_ObjFanin0(pFanin);
            // make sure there is no mixing of networks
            assert( pFanin->pNtk == pNode->pNtk );
            // check if the fanin is visited
            if ( Abc_NodeIsTravIdPrevious(pFanin) ) 
                continue;
            // traverse searching for the loop
            fAcyclic = Abc_NtkIsAcyclic_rec( pFanin );
            // return as soon as the loop is detected
            if ( fAcyclic == 0 ) 
            {
                fprintf( stdout, "  <--  %s", Abc_ObjName(pNode) );
                return 0;
            }
        }
    }
    else
    {
        Abc_ObjForEachFanin( pNode, pFanin, i )
        {
            // make sure there is no mixing of networks
            assert( pFanin->pNtk == pNode->pNtk );
            // check if the fanin is visited
            if ( Abc_NodeIsTravIdPrevious(pFanin) ) 
                continue;
            // traverse searching for the loop
            fAcyclic = Abc_NtkIsAcyclic_rec( pFanin );
            // return as soon as the loop is detected
            if ( fAcyclic == 0 ) 
            {
                fprintf( stdout, "  <--  %s", Abc_ObjName(pNode) );
                return 0;
            }
        }
    }
    // mark this node as a visited node
    Abc_NodeSetTravIdPrevious( pNode );
    return 1;
}

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