summaryrefslogtreecommitdiffstats
path: root/src/aig/ivy/ivyHaig.c
blob: 8702160078fb6c3fa02efb340b88732e7377b2ac (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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
/**CFile****************************************************************

  FileName    [ivyHaig.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [And-Inverter Graph package.]

  Synopsis    [HAIG management procedures.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

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

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

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

#include "ivy.h"

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

/* 
    HAIGing rules in working AIG:
    - Each node in the working AIG has a pointer to the corresponding node in HAIG 
      (this node is not necessarily the representative of the equivalence class of HAIG nodes)
    - This pointer is complemented if the AIG node and its corresponding HAIG node have different phase

    Choice node rules in HAIG:
    - Equivalent nodes are linked into a ring
    - Exactly one node in the ring has fanouts (this node is called the representative)
    - The pointer going from a node to the next node in the ring is complemented 
      if the first node is complemented, compared to the representative node of the equivalence class
    - (consequence of the above) The representative node always has non-complemented pointer to the next node
    - New nodes are inserted into the ring immediately after the representative node
*/

// returns the representative node of the given HAIG node
static inline Ivy_Obj_t * Ivy_HaigObjRepr( Ivy_Obj_t * pObj )
{
    Ivy_Obj_t * pTemp;
    assert( !Ivy_IsComplement(pObj) );
    // if the node has no equivalent node or has fanout, it is representative
    if ( pObj->pEquiv == NULL || Ivy_ObjRefs(pObj) > 0 )
        return pObj;
    // the node belongs to a class and is not a representative
    // complemented edge (pObj->pEquiv) tells if it is complemented w.r.t. the repr
    for ( pTemp = Ivy_Regular(pObj->pEquiv); pTemp != pObj; pTemp = Ivy_Regular(pTemp->pEquiv) )
        if ( Ivy_ObjRefs(pTemp) > 0 )
            break;
    // return the representative node
    assert( Ivy_ObjRefs(pTemp) > 0 );
    return Ivy_NotCond( pTemp, Ivy_IsComplement(pObj->pEquiv) );
}

// counts the number of nodes in the equivalence class
static inline int Ivy_HaigObjCountClass( Ivy_Obj_t * pObj )
{
    Ivy_Obj_t * pTemp;
    int Counter;
    assert( !Ivy_IsComplement(pObj) );
    assert( Ivy_ObjRefs(pObj) > 0 );
    if ( pObj->pEquiv == NULL )
        return 1;
    assert( !Ivy_IsComplement(pObj->pEquiv) );
    Counter = 1;
    for ( pTemp = pObj->pEquiv; pTemp != pObj; pTemp = Ivy_Regular(pTemp->pEquiv) )
        Counter++;
    return Counter;
}

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

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

  Synopsis    [Starts HAIG for the manager.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Ivy_ManHaigStart( Ivy_Man_t * p, int fVerbose )
{
    Vec_Int_t * vLatches;
    Ivy_Obj_t * pObj;
    int i;
    assert( p->pHaig == NULL );
    p->pHaig = Ivy_ManDup( p );

    if ( fVerbose )
    {
        printf( "Starting : " );
        Ivy_ManPrintStats( p->pHaig );
    }

    // collect latches of design D and set their values to be DC
    vLatches = Vec_IntAlloc( 100 );
    Ivy_ManForEachLatch( p->pHaig, pObj, i )
    {
        pObj->Init = IVY_INIT_DC;
        Vec_IntPush( vLatches, pObj->Id );
    }
    p->pHaig->pData = vLatches;
/*
    {
        int x;
        Ivy_ManShow( p, 0, NULL );
        Ivy_ManShow( p->pHaig, 1, NULL );
        x = 0;
    }
*/
}

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

  Synopsis    [Transfers the HAIG to the newly created manager.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Ivy_ManHaigTrasfer( Ivy_Man_t * p, Ivy_Man_t * pNew )
{
    Ivy_Obj_t * pObj;
    int i;
    assert( p->pHaig != NULL );
    Ivy_ManConst1(pNew)->pEquiv = Ivy_ManConst1(p)->pEquiv;
    Ivy_ManForEachPi( pNew, pObj, i )
        pObj->pEquiv = Ivy_ManPi( p, i )->pEquiv;
    pNew->pHaig = p->pHaig;
}

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

  Synopsis    [Stops HAIG for the manager.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Ivy_ManHaigStop( Ivy_Man_t * p )
{
    Ivy_Obj_t * pObj;
    int i;
    assert( p->pHaig != NULL );
    Vec_IntFree( p->pHaig->pData );
    Ivy_ManStop( p->pHaig );
    p->pHaig = NULL;
    // remove dangling pointers to the HAIG objects
    Ivy_ManForEachObj( p, pObj, i )
        pObj->pEquiv = NULL;
}

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

  Synopsis    [Creates a new node in HAIG.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Ivy_ManHaigCreateObj( Ivy_Man_t * p, Ivy_Obj_t * pObj )
{
    Ivy_Obj_t * pEquiv0, * pEquiv1;
    assert( p->pHaig != NULL );
    assert( !Ivy_IsComplement(pObj) );
    if ( Ivy_ObjType(pObj) == IVY_BUF )
        pObj->pEquiv = Ivy_ObjChild0Equiv(pObj);
    else if ( Ivy_ObjType(pObj) == IVY_LATCH )
    {
//        pObj->pEquiv = Ivy_Latch( p->pHaig, Ivy_ObjChild0Equiv(pObj), pObj->Init );
        pEquiv0 = Ivy_ObjChild0Equiv(pObj);
        pEquiv0 = Ivy_NotCond( Ivy_HaigObjRepr(Ivy_Regular(pEquiv0)), Ivy_IsComplement(pEquiv0) );
        pObj->pEquiv = Ivy_Latch( p->pHaig, pEquiv0, pObj->Init );
    }
    else if ( Ivy_ObjType(pObj) == IVY_AND )
    {
//        pObj->pEquiv = Ivy_And( p->pHaig, Ivy_ObjChild0Equiv(pObj), Ivy_ObjChild1Equiv(pObj) );
        pEquiv0 = Ivy_ObjChild0Equiv(pObj);
        pEquiv0 = Ivy_NotCond( Ivy_HaigObjRepr(Ivy_Regular(pEquiv0)), Ivy_IsComplement(pEquiv0) );
        pEquiv1 = Ivy_ObjChild1Equiv(pObj);
        pEquiv1 = Ivy_NotCond( Ivy_HaigObjRepr(Ivy_Regular(pEquiv1)), Ivy_IsComplement(pEquiv1) );
        pObj->pEquiv = Ivy_And( p->pHaig, pEquiv0, pEquiv1 );
    }
    else assert( 0 );
    // make sure the node points to the representative
//    pObj->pEquiv = Ivy_NotCond( Ivy_HaigObjRepr(Ivy_Regular(pObj->pEquiv)), Ivy_IsComplement(pObj->pEquiv) );
}

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

  Synopsis    [Checks if the old node is in the TFI of the new node.]

  Description [] 
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Ivy_ObjIsInTfi_rec( Ivy_Obj_t * pObjNew, Ivy_Obj_t * pObjOld, int Levels )
{
    if ( pObjNew == pObjOld )
        return 1;
    if ( Levels == 0 || Ivy_ObjIsCi(pObjNew) || Ivy_ObjIsConst1(pObjNew) )
        return 0;
    if ( Ivy_ObjIsInTfi_rec( Ivy_ObjFanin0(pObjNew), pObjOld, Levels - 1 ) )
        return 1;
    if ( Ivy_ObjIsNode(pObjNew) && Ivy_ObjIsInTfi_rec( Ivy_ObjFanin1(pObjNew), pObjOld, Levels - 1 ) )
        return 1;
    return 0;
}

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

  Synopsis    [Sets the pair of equivalent nodes in HAIG.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Ivy_ManHaigCreateChoice( Ivy_Man_t * p, Ivy_Obj_t * pObjOld, Ivy_Obj_t * pObjNew )
{
    Ivy_Obj_t * pObjOldHaig, * pObjNewHaig;
    Ivy_Obj_t * pObjOldHaigR, * pObjNewHaigR;
    int fCompl;
//printf( "\nCreating choice for %d and %d in AIG\n", pObjOld->Id, Ivy_Regular(pObjNew)->Id );

    assert( p->pHaig != NULL );
    assert( !Ivy_IsComplement(pObjOld) );
    // get pointers to the representatives of pObjOld and pObjNew
    pObjOldHaig = pObjOld->pEquiv;
    pObjNewHaig = Ivy_NotCond( Ivy_Regular(pObjNew)->pEquiv, Ivy_IsComplement(pObjNew) );
    // get the classes
    pObjOldHaig = Ivy_NotCond( Ivy_HaigObjRepr(Ivy_Regular(pObjOldHaig)), Ivy_IsComplement(pObjOldHaig) );
    pObjNewHaig = Ivy_NotCond( Ivy_HaigObjRepr(Ivy_Regular(pObjNewHaig)), Ivy_IsComplement(pObjNewHaig) );
    // get regular pointers
    pObjOldHaigR = Ivy_Regular(pObjOldHaig);
    pObjNewHaigR = Ivy_Regular(pObjNewHaig);
    // check if there is phase difference between them
    fCompl = (Ivy_IsComplement(pObjOldHaig) != Ivy_IsComplement(pObjNewHaig));
    // if the class is the same, nothing to do
    if ( pObjOldHaigR == pObjNewHaigR )
        return;
    // if the second node belongs to a class, do not merge classes (for the time being)
    if ( Ivy_ObjRefs(pObjOldHaigR) == 0 || pObjNewHaigR->pEquiv != NULL || 
        Ivy_ObjRefs(pObjNewHaigR) > 0 ) //|| Ivy_ObjIsInTfi_rec(pObjNewHaigR, pObjOldHaigR, 10) )
    {
/*
        if ( pObjNewHaigR->pEquiv != NULL )
            printf( "c" );
        if ( Ivy_ObjRefs(pObjNewHaigR) > 0 )
            printf( "f" );
        printf( " " );
*/
        p->pHaig->nClassesSkip++;
        return;
    }

    // add this node to the class of pObjOldHaig
    assert( Ivy_ObjRefs(pObjOldHaigR) > 0 );
    assert( !Ivy_IsComplement(pObjOldHaigR->pEquiv) );
    if ( pObjOldHaigR->pEquiv == NULL )
        pObjNewHaigR->pEquiv = Ivy_NotCond( pObjOldHaigR, fCompl );
    else
        pObjNewHaigR->pEquiv = Ivy_NotCond( pObjOldHaigR->pEquiv, fCompl );
    pObjOldHaigR->pEquiv = pObjNewHaigR;
//printf( "Setting choice node %d -> %d.\n", pObjOldHaigR->Id, pObjNewHaigR->Id );
    // update the class of the new node
//    Ivy_Regular(pObjNew)->pEquiv = Ivy_NotCond( pObjOldHaigR, fCompl ^ Ivy_IsComplement(pObjNew) );
//printf( "Creating choice for %d and %d in HAIG\n", pObjOldHaigR->Id, pObjNewHaigR->Id );

//    if ( pObjOldHaigR->Id == 13 )
//    {
//        Ivy_ManShow( p, 0 );
//        Ivy_ManShow( p->pHaig, 1 );
//    }
//    if ( !Ivy_ManIsAcyclic( p->pHaig ) )
//        printf( "HAIG contains a cycle\n" );
}

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

  Synopsis    [Count the number of choices and choice nodes in HAIG.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Ivy_ManHaigCountChoices( Ivy_Man_t * p, int * pnChoices )
{
    Ivy_Obj_t * pObj;
    int nChoices, nChoiceNodes, Counter, i;
    assert( p->pHaig != NULL );
    nChoices = nChoiceNodes = 0;
    Ivy_ManForEachObj( p->pHaig, pObj, i )
    {
        if ( Ivy_ObjIsTerm(pObj) || i == 0 )
            continue;
        if ( Ivy_ObjRefs(pObj) == 0 )
            continue;
        Counter = Ivy_HaigObjCountClass( pObj );
        nChoiceNodes += (int)(Counter > 1);
        nChoices += Counter - 1;
//        if ( Counter > 1 )
//            printf( "Choice node %d %s\n", pObj->Id, Ivy_ObjIsLatch(pObj)? "(latch)": "" );
    }
    *pnChoices = nChoices;
    return nChoiceNodes;
} 

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

  Synopsis    [Prints statistics of the HAIG.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Ivy_ManHaigPostprocess( Ivy_Man_t * p, int fVerbose )
{
    int nChoices, nChoiceNodes;

    assert( p->pHaig != NULL );

    if ( fVerbose )
    {
        printf( "Final    : " );
        Ivy_ManPrintStats( p );
        printf( "HAIG     : " );
        Ivy_ManPrintStats( p->pHaig );

        // print choice node stats
        nChoiceNodes = Ivy_ManHaigCountChoices( p, &nChoices );
        printf( "Total choice nodes = %d. Total choices = %d. Skipped classes = %d.\n", 
            nChoiceNodes, nChoices, p->pHaig->nClassesSkip ); 
    }

    if ( Ivy_ManIsAcyclic( p->pHaig ) )
    {
        if ( fVerbose )
            printf( "HAIG is acyclic\n" );
    }
    else
        printf( "HAIG contains a cycle\n" );

//    if ( fVerbose )
//        Ivy_ManHaigSimulate( p );
}


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

  Synopsis    [Applies the simulation rules.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline Ivy_Init_t Ivy_ManHaigSimulateAnd( Ivy_Init_t In0, Ivy_Init_t In1 )
{
    assert( In0 != IVY_INIT_NONE && In1 != IVY_INIT_NONE );
    if ( In0 == IVY_INIT_DC || In1 == IVY_INIT_DC )
        return IVY_INIT_DC;
    if ( In0 == IVY_INIT_1 && In1 == IVY_INIT_1 )
        return IVY_INIT_1;
    return IVY_INIT_0;
}

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

  Synopsis    [Applies the simulation rules.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline Ivy_Init_t Ivy_ManHaigSimulateChoice( Ivy_Init_t In0, Ivy_Init_t In1 )
{
    assert( In0 != IVY_INIT_NONE && In1 != IVY_INIT_NONE );
    if ( (In0 == IVY_INIT_0 && In1 == IVY_INIT_1) || (In0 == IVY_INIT_1 && In1 == IVY_INIT_0) )
    {
         printf( "Compatibility fails.\n" );
         return IVY_INIT_0;
    }
    if ( In0 == IVY_INIT_DC && In1 == IVY_INIT_DC )
        return IVY_INIT_DC;
    if ( In0 != IVY_INIT_DC )
        return In0;
    return In1;
}

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

  Synopsis    [Simulate HAIG using modified 3-valued simulation.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Ivy_ManHaigSimulate( Ivy_Man_t * p )
{
    Vec_Int_t * vNodes, * vLatches, * vLatchesD;
    Ivy_Obj_t * pObj, * pTemp;
    Ivy_Init_t In0, In1;
    int i, k, Counter;
    int fVerbose = 0;

    // check choices
    Ivy_ManCheckChoices( p );

    // switch to HAIG
    assert( p->pHaig != NULL );
    p = p->pHaig;

if ( fVerbose )
Ivy_ManForEachPi( p, pObj, i )
printf( "Setting PI %d\n", pObj->Id );

    // collect latches and nodes in the DFS order
    vNodes = Ivy_ManDfsSeq( p, &vLatches );

if ( fVerbose )
Ivy_ManForEachNodeVec( p, vNodes, pObj, i )
printf( "Collected node %d with fanins %d and %d\n", pObj->Id, Ivy_ObjFanin0(pObj)->Id, Ivy_ObjFanin1(pObj)->Id );

    // set the PI values
    Ivy_ManConst1(p)->Init = IVY_INIT_1;
    Ivy_ManForEachPi( p, pObj, i )
        pObj->Init = IVY_INIT_0;

    // set the latch values
    Ivy_ManForEachNodeVec( p, vLatches, pObj, i )
        pObj->Init = IVY_INIT_DC;
    // set the latches of D to be determinate
    vLatchesD = p->pData;
    Ivy_ManForEachNodeVec( p, vLatchesD, pObj, i )
        pObj->Init = IVY_INIT_0;

    // perform several rounds of simulation
    for ( k = 0; k < 10; k++ )
    {
        // count the number of non-determinate values
        Counter = 0;
        Ivy_ManForEachNodeVec( p, vLatches, pObj, i )
            Counter += ( pObj->Init == IVY_INIT_DC );
        printf( "Iter %d : Non-determinate = %d\n", k, Counter );    
        
        // simulate the internal nodes
        Ivy_ManForEachNodeVec( p, vNodes, pObj, i )
        {
if ( fVerbose )
printf( "Processing node %d with fanins %d and %d\n", pObj->Id, Ivy_ObjFanin0(pObj)->Id, Ivy_ObjFanin1(pObj)->Id );
            In0 = Ivy_InitNotCond( Ivy_ObjFanin0(pObj)->Init, Ivy_ObjFaninC0(pObj) );
            In1 = Ivy_InitNotCond( Ivy_ObjFanin1(pObj)->Init, Ivy_ObjFaninC1(pObj) );
            pObj->Init = Ivy_ManHaigSimulateAnd( In0, In1 );
            // simulate the equivalence class if the node is a representative
            if ( pObj->pEquiv && Ivy_ObjRefs(pObj) > 0 )
            {
if ( fVerbose )
printf( "Processing choice node %d\n", pObj->Id );
                In0 = pObj->Init;
                assert( !Ivy_IsComplement(pObj->pEquiv) );
                for ( pTemp = pObj->pEquiv; pTemp != pObj; pTemp = Ivy_Regular(pTemp->pEquiv) )
                {
if ( fVerbose )
printf( "Processing secondary node %d\n", pTemp->Id );
                    In1 = Ivy_InitNotCond( pTemp->Init, Ivy_IsComplement(pTemp->pEquiv) );
                    In0 = Ivy_ManHaigSimulateChoice( In0, In1 );
                }
                pObj->Init = In0;
            }
        }

        // simulate the latches
        Ivy_ManForEachNodeVec( p, vLatches, pObj, i )
        {
            pObj->Level = Ivy_ObjFanin0(pObj)->Init;
if ( fVerbose )
printf( "Using latch %d with fanin %d\n", pObj->Id, Ivy_ObjFanin0(pObj)->Id );
        }
        Ivy_ManForEachNodeVec( p, vLatches, pObj, i )
            pObj->Init = pObj->Level, pObj->Level = 0;
    }
    // free arrays
    Vec_IntFree( vNodes );
    Vec_IntFree( vLatches );
}

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