summaryrefslogtreecommitdiffstats
path: root/src/aig/ntl/ntlFraig.c
blob: 9470df3ed6462dc81d10b9ac443fa1b5ccf8d3af (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
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
/**CFile****************************************************************

  FileName    [ntlFraig.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [Netlist representation.]

  Synopsis    [Performing fraiging with white-boxes.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

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

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

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

#include "ntl.h"
#include "fra.h"
#include "ssw.h"
#include "dch.h"

ABC_NAMESPACE_IMPL_START


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

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

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

  Synopsis    [Remaps representatives of the equivalence classes.]

  Description [For each equivalence class, if the current representative 
  of the class cannot be used because its corresponding net has no-merge 
  attribute, find the topologically-shallowest node, which can be used 
  as a representative.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Ntl_ManUpdateNoMergeReprs( Aig_Man_t * pAig, Aig_Obj_t ** pReprs )
{
    Aig_Obj_t ** pReprsNew = NULL;
    Aig_Obj_t * pObj, * pRepres, * pRepresNew;
    Ntl_Net_t * pNet, * pNetObj;
    int i;

    // allocate room for the new representative
    pReprsNew = ABC_ALLOC( Aig_Obj_t *, Aig_ManObjNumMax(pAig) );
    memset( pReprsNew, 0, sizeof(Aig_Obj_t *) * Aig_ManObjNumMax(pAig) );
    Aig_ManForEachObj( pAig, pObj, i )
    {
        // get the old representative node
        pRepres = pReprs[pObj->Id];
        if ( pRepres == NULL )
            continue;
        // if this representative node is already remapped, skip it
        pRepresNew = pReprsNew[ pRepres->Id ];
        if ( pRepresNew != NULL )
            continue;
        // get the net of the representative node
        pNet = (Ntl_Net_t *)pRepres->pData;
        assert( pRepres->pData != NULL );
        if ( Ntl_ObjIsBox(pNet->pDriver) && pNet->pDriver->pImplem->attrNoMerge )
        {
            // the net belongs to the no-merge box
            pNetObj = (Ntl_Net_t *)pObj->pData;
            if ( Ntl_ObjIsBox(pNetObj->pDriver) && pNetObj->pDriver->pImplem->attrNoMerge )
                continue;
            // the object's net does not belong to the no-merge box
            // pObj can be used instead of pRepres
            pReprsNew[ pRepres->Id ] = pObj;
        }
        else
        {
            // otherwise, it is fine to use pRepres
            pReprsNew[ pRepres->Id ] = pRepres;
        }
    }
    // update the representatives
    Aig_ManForEachObj( pAig, pObj, i )
    {
        // get the representative node
        pRepres = pReprs[ pObj->Id ];
        if ( pRepres == NULL )
            continue;
        // if the representative has no mapping, undo the mapping of the node
        pRepresNew = pReprsNew[ pRepres->Id ];
        if ( pRepresNew == NULL || pRepresNew == pObj )
        {
            pReprs[ pObj->Id ] = NULL;
            continue;
        }
        // remap the representative
//        assert( pObj->Id > pRepresNew->Id );        
//        pReprs[ pObj->Id ] = pRepresNew;
        if ( pObj->Id > pRepresNew->Id )
            pReprs[ pObj->Id ] = pRepresNew;
        else
            pReprs[ pObj->Id ] = NULL;
    }
    ABC_FREE( pReprsNew );
}

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

  Synopsis    [Transfers equivalence class info from pAigCol to pAig.]

  Description [pAig points to the nodes of netlist (pNew) derived using it.
  pNew points to the nodes of the collapsed AIG (pAigCol) derived using it.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Aig_Obj_t ** Ntl_ManFraigDeriveClasses( Aig_Man_t * pAig, Ntl_Man_t * pNew, Aig_Man_t * pAigCol )
{
    Ntl_Net_t * pNet;
    Aig_Obj_t ** pReprs = NULL, ** pMapBack = NULL;
    Aig_Obj_t * pObj, * pObjCol, * pObjColRepr, * pCorresp;
    int i;

    // remember pointers to the nets of pNew
    Aig_ManForEachObj( pAig, pObj, i )
        pObj->pNext = (Aig_Obj_t *)pObj->pData;

    // map the AIG managers
    Aig_ManForEachObj( pAig, pObj, i )
    {
        if ( Aig_ObjIsConst1(pObj) )
            pObj->pData = Aig_ManConst1(pAigCol);
        else if ( !Aig_ObjIsPo(pObj) )
        {
            pNet = (Ntl_Net_t *)pObj->pData;
            pObjCol = Aig_Regular((Aig_Obj_t *)pNet->pCopy);
            pObj->pData = pObjCol;
        }
    }

    // create mapping from the collapsed manager into the original manager
    // (each node in the collapsed manager may have more than one equivalent node 
    // in the original manager; this procedure finds the first node in the original 
    // manager that is equivalent to the given node in the collapsed manager) 
    pMapBack = ABC_ALLOC( Aig_Obj_t *, Aig_ManObjNumMax(pAigCol) );
    memset( pMapBack, 0, sizeof(Aig_Obj_t *) * Aig_ManObjNumMax(pAigCol) );
    Aig_ManForEachObj( pAig, pObj, i )
    {
        if ( Aig_ObjIsPo(pObj) )
            continue;
        pObjCol = (Aig_Obj_t *)pObj->pData;
        if ( pObjCol == NULL )
            continue;
        if ( pMapBack[pObjCol->Id] == NULL )
            pMapBack[pObjCol->Id] = pObj;
    }

    // create the equivalence classes for the original manager
    pReprs = ABC_ALLOC( Aig_Obj_t *, Aig_ManObjNumMax(pAig) );
    memset( pReprs, 0, sizeof(Aig_Obj_t *) * Aig_ManObjNumMax(pAig) );
    Aig_ManForEachObj( pAig, pObj, i )
    {
        if ( Aig_ObjIsPo(pObj) )
            continue;
        // get the collapsed node
        pObjCol = (Aig_Obj_t *)pObj->pData;
        if ( pObjCol == NULL )
            continue;
        // get the representative of the collapsed node
        pObjColRepr = pAigCol->pReprs[pObjCol->Id];
        if ( pObjColRepr == NULL )
            pObjColRepr = pObjCol;
        // get the corresponding original node
        pCorresp = pMapBack[pObjColRepr->Id];
        if ( pCorresp == NULL || pCorresp == pObj )
            continue;
        // set the representative
        if ( pCorresp->Id < pObj->Id )
            pReprs[pObj->Id] = pCorresp;
        else
            pReprs[pCorresp->Id] = pObj;
    }
    ABC_FREE( pMapBack );

    // recall pointers to the nets of pNew
    Aig_ManForEachObj( pAig, pObj, i )
        pObj->pData = pObj->pNext, pObj->pNext = NULL;

    // remap no-merge representatives to point to 
    // the shallowest nodes in the class without no-merge
    Ntl_ManUpdateNoMergeReprs( pAig, pReprs );
    return pReprs;
}

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

  Synopsis    [Uses equivalences in the AIG to reduce the design.]

  Description [The AIG (pAig) was extracted from the netlist and still 
  points to it (pObj->pData is the pointer to the nets in the netlist).
  Equivalences have been computed for the collapsed AIG and transfered
  to this AIG (pAig). This procedure reduces the corresponding nets.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Ntl_ManReduce( Ntl_Man_t * p, Aig_Man_t * pAig )
{
    Aig_Obj_t * pObj, * pObjRepr;
    Ntl_Net_t * pNet, * pNetRepr, * pNetNew;
    Ntl_Mod_t * pRoot;
    Ntl_Obj_t * pNode, * pNodeOld;
    int i, fCompl, Counter = 0;
    char * pNameNew;
//    int Lenght;
    assert( pAig->pReprs );
    pRoot = Ntl_ManRootModel( p );
    Aig_ManForEachObj( pAig, pObj, i )
    {
        pObjRepr = Aig_ObjRepr( pAig, pObj );
        if ( pObjRepr == NULL )
            continue;
        assert( pObj != pObjRepr );
        pNet = (Ntl_Net_t *)pObj->pData;
        pNetRepr = (Ntl_Net_t *)pObjRepr->pData;
        // consider special cases, when the net should not be reduced
        if ( Ntl_ObjIsBox(pNet->pDriver) )
        {
            // do not reduce the net if it is driven by a multi-output box
            if ( Ntl_ObjFanoutNum(pNet->pDriver) > 1 )
                continue;
            // do not reduce the net if it has no-merge attribute
            if ( pNet->pDriver->pImplem->attrNoMerge )
                continue;
            // do not reduce the net if the replacement net has no-merge attribute
            if ( pNetRepr != NULL && Ntl_ObjIsBox(pNetRepr->pDriver) && 
                 pNetRepr->pDriver->pImplem->attrNoMerge )
                continue;
        }
        if ( pNetRepr == NULL )
        {
            // this is the constant node
            assert( Aig_ObjIsConst1(pObjRepr) );
            pNode = Ntl_ModelCreateNode( pRoot, 0 );
            pNode->pSop = Ntl_ManStoreSop( p->pMemSops, " 1\n" );
            if ( (pNetRepr = Ntl_ModelFindNet( pRoot, "Const1" )) )
            {
                printf( "Ntl_ManReduce(): Internal error: Intermediate net name is not unique.\n" );
                return;
            }
            pNetRepr = Ntl_ModelFindOrCreateNet( pRoot, "Const1" );
            if ( !Ntl_ModelSetNetDriver( pNode, pNetRepr ) )
            {
                printf( "Ntl_ManReduce(): Internal error: Net has more than one fanin.\n" );
                return;
            }
            pObjRepr->pData = pNetRepr;
            pNetRepr->pCopy = Aig_ManConst1(pAig);
        }
        // get the complemented attributes of the nets
        fCompl = Aig_IsComplement((Aig_Obj_t *)pNet->pCopy) ^ Aig_Regular((Aig_Obj_t *)pNet->pCopy)->fPhase ^
                 Aig_IsComplement((Aig_Obj_t *)pNetRepr->pCopy) ^ Aig_Regular((Aig_Obj_t *)pNetRepr->pCopy)->fPhase;
        // create interter/buffer driven by the representative net
        pNode = Ntl_ModelCreateNode( pRoot, 1 );
        pNode->pSop = fCompl? Ntl_ManStoreSop( p->pMemSops, "0 1\n" ) : Ntl_ManStoreSop( p->pMemSops, "1 1\n" );
        Ntl_ObjSetFanin( pNode, pNetRepr, 0 );
        // make the new node drive the equivalent net (pNet)
        pNodeOld = pNet->pDriver;
        if ( !Ntl_ModelClearNetDriver( pNet->pDriver, pNet ) )
            printf( "Ntl_ManReduce(): Internal error! Net already has no driver.\n" );
        if ( !Ntl_ModelSetNetDriver( pNode, pNet ) )
            printf( "Ntl_ManReduce(): Internal error! Net already has a driver.\n" );
/*
        // remove this net from the hash table (but do not remove from the array)
        Ntl_ModelDeleteNet( pRoot, pNet );
        // create new net with the same name
        pNetNew = Ntl_ModelFindOrCreateNet( pRoot, pNet->pName );
        // clean the name
        pNet->pName[0] = 0;
*/
        // create new net with a new name
        pNameNew = Ntl_ModelCreateNetName( pRoot, "noname", (int)(ABC_PTRINT_T)pNet );
        pNetNew = Ntl_ModelFindOrCreateNet( pRoot, pNameNew );

        // make the old node drive the new net without fanouts
        if ( !Ntl_ModelSetNetDriver( pNodeOld, pNetNew ) )
            printf( "Ntl_ManReduce(): Internal error! Net already has a driver.\n" );

        Counter++;
    }
//    printf( "Nets without names = %d.\n", Counter );
}

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

  Synopsis    [Resets complemented attributes of the collapsed AIG.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Ntl_ManResetComplemented( Ntl_Man_t * p, Aig_Man_t * pAigCol )
{
    Ntl_Mod_t * pRoot;
    Ntl_Obj_t * pObj;
    Aig_Obj_t * pObjCol;
    int i;
    pRoot = Ntl_ManRootModel(p);
    Ntl_ModelForEachLatch( pRoot, pObj, i )
    {
        if ( Ntl_ObjIsInit1( pObj ) )
        {
            pObjCol = (Aig_Obj_t *)Ntl_ObjFanout0(pObj)->pCopy;
            assert( pObjCol->fPhase == 0 );
            pObjCol->fPhase = 1;
        }
    }
}
 
/**Function*************************************************************

  Synopsis    [Finalizes the transformation.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Ntl_Man_t * Ntl_ManFinalize( Ntl_Man_t * pNew, Aig_Man_t * pAig, Aig_Man_t * pAigCol, int fVerbose )
{
    int fUseExtraSweep = 1;
    Ntl_Man_t * pSwept;
    Aig_Man_t * pTemp;
    assert( pAig->pReprs == NULL );
    assert( pAigCol->pReprs != NULL );

    // transfer equivalence classes to the original AIG
    pAig->pReprs = Ntl_ManFraigDeriveClasses( pAig, pNew, pAigCol );
    pAig->nReprsAlloc = Aig_ManObjNumMax(pAig);
if ( fVerbose )
    printf( "Equivalences:  Collapsed = %5d.  Extracted = %5d.\n", Aig_ManCountReprs(pAigCol), Aig_ManCountReprs(pAig) );
/*
{
    Aig_Obj_t * pObj;
    int i;
    Aig_ManForEachObj( pAig, pObj, i )
        if ( pAig->pReprs[i] != NULL )
            printf( "%s%d->%s%d  ", 
                (Aig_ObjIsPi(pObj)? "pi": ""), 
                pObj->Id, 
                (Aig_ObjIsPi(pAig->pReprs[i])? "pi": ""),
                pAig->pReprs[i]->Id ); 
    printf( "\n" );
}
*/
    // implement equivalence classes and remove dangling nodes
    Ntl_ManReduce( pNew, pAig );
    Ntl_ManSweep( pNew, fVerbose );

    // perform one more sweep
    if ( fUseExtraSweep )
    {
        pTemp = Ntl_ManExtract( pNew );
        pSwept = Ntl_ManInsertAig( pNew, pTemp );
        Aig_ManStop( pTemp );
        Ntl_ManSweep( pSwept, fVerbose );
        return pSwept;
    }
    return Ntl_ManDup(pNew);
}

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

  Synopsis    [Returns AIG with WB after fraiging.]

  Description [Consumes the input NTL to save memory.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Ntl_Man_t * Ntl_ManFraig( Ntl_Man_t * p, int nPartSize, int nConfLimit, int nLevelMax, int fUseCSat, int fVerbose )
{
    Ntl_Man_t * pNew, * pAux;
    Aig_Man_t * pAig, * pAigCol, * pTemp;

    if ( Ntl_ModelNodeNum(Ntl_ManRootModel(p)) == 0 )
        return p;

    // collapse the AIG
    pAig = Ntl_ManExtract( p );
    pNew = Ntl_ManInsertAig( p, pAig );
    Ntl_ManFree( p );
    pAigCol = Ntl_ManCollapseComb( pNew );
    if ( pAigCol == NULL )
    {
        Aig_ManStop( pAig );
        return pNew;
    }

    // perform fraiging for the given design
    if ( fUseCSat )
    {
//        extern Aig_Man_t * Cec_FraigCombinational( Aig_Man_t * pAig, int nConfs, int fVerbose );
//        pTemp = Cec_FraigCombinational( pAigCol, nConfLimit, fVerbose );
//        Aig_ManStop( pTemp );
        extern void Dch_ComputeEquivalences( Aig_Man_t * pAig, Dch_Pars_t * pPars );
        Dch_Pars_t Pars, * pPars = &Pars;
        Dch_ManSetDefaultParams( pPars );
        pPars->nBTLimit = nConfLimit;
        pPars->fVerbose = fVerbose;
        Dch_ComputeEquivalences( pAigCol, pPars );
    }
    else
    {
        nPartSize = nPartSize? nPartSize : Aig_ManPoNum(pAigCol);
        pTemp = Aig_ManFraigPartitioned( pAigCol, nPartSize, nConfLimit, nLevelMax, fVerbose );
        Aig_ManStop( pTemp );
    }

    // finalize the transformation
    pNew = Ntl_ManFinalize( pAux = pNew, pAig, pAigCol, fVerbose );
    Ntl_ManFree( pAux );
    Aig_ManStop( pAig );
    Aig_ManStop( pAigCol );
    return pNew;
}

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

  Synopsis    [Counts the number of resets.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Ntl_ManAigCountResets( Ntl_Man_t * pNtl )
{
/*
    Ntl_Mod_t * pModel = Ntl_ManRootModel(pNtl);
    Ntl_Obj_t * pBox;
    int i, Counter = -1;
    Ntl_ModelForEachObj( pModel, pBox, i )
        Counter = ABC_MAX( Counter, pBox->Reset );
    return Counter + 1;
*/
    return -1;
}

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

  Synopsis    [Transforms sequential AIG to allow for async reset.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Aig_Man_t * Ntl_ManAigToRst( Ntl_Man_t * pNtl, Aig_Man_t * p )
{
    Ntl_Mod_t * pModel = Ntl_ManRootModel(pNtl);
    Aig_Man_t * pNew;
    Aig_Obj_t * pObj;
    int i, iRegNum, iRstNum, Counter = 0;
    int nResets = Ntl_ManAigCountResets( pNtl );
    assert( pNtl->pNal != NULL );
    assert( Aig_ManRegNum(p) > 0 );
    assert( Vec_IntSize(pNtl->vRstClasses) == Aig_ManRegNum(p) );
//printf( "Number of resets before synthesis = %d.\n", nResets );
    // create the PIs
    Aig_ManCleanData( p );
    Aig_ManSetPioNumbers( p );
    // create the new manager
    pNew = Aig_ManStart( Aig_ManObjNumMax(p) );
    pNew->pName = Aig_UtilStrsav( p->pName );
    pNew->pSpec = Aig_UtilStrsav( p->pSpec );
    // create special PIs
    for ( i = 0; i < nResets; i++ )
        Aig_ObjCreatePi( pNew );
    // duplicate internal nodes
    Aig_ManForEachPi( p, pObj, i )
        pObj->pData = Aig_ObjCreatePi( pNew );
    Aig_ManForEachObj( p, pObj, i )
    {
        if ( Aig_ObjIsNode(pObj) )
            pObj->pData = Aig_And( pNew, Aig_ObjChild0Copy(pObj), Aig_ObjChild1Copy(pObj) );
        else if ( Aig_ObjIsPo(pObj) )
            pObj->pData = Aig_ObjCreatePo( pNew, Aig_ObjChild0Copy(pObj) );
        else if ( Aig_ObjIsPi(pObj) )
        {
//            pObj->pData = Aig_ObjCreatePi( pNew );
            iRegNum = Aig_ObjPioNum(pObj) - (Aig_ManPiNum(p) - Aig_ManRegNum(p));
            if ( iRegNum < 0 )
                continue;
            iRstNum = Vec_IntEntry(pNtl->vRstClasses, iRegNum);
            if ( iRstNum < 0 )
                continue;
            assert( iRstNum < nResets );
            pObj->pData = Aig_And( pNew, (Aig_Obj_t *)pObj->pData, Aig_ManPi(pNew, iRstNum) ); // could be NOT(pi)
            Counter++;
        }
        else if ( Aig_ObjIsConst1(pObj) )
            pObj->pData = Aig_ManConst1(pNew);
        else
            assert( 0 );
    }
    assert( Aig_ManNodeNum(p) + Counter == Aig_ManNodeNum(pNew) );
    if ( (Counter = Aig_ManCleanup( pNew )) )
        printf( "Aig_ManDupOrdered(): Cleanup after AIG duplication removed %d nodes.\n", Counter );
    Aig_ManSetRegNum( pNew, Aig_ManRegNum(p) );
    return pNew;
}

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

  Synopsis    [Remaps equivalence classes from the new nodes to the old ones.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Ntl_ManRemapClassesLcorr( Ntl_Man_t * pNtl, Aig_Man_t * p, Aig_Man_t * pNew )
{
    Ntl_Mod_t * pModel = Ntl_ManRootModel(pNtl);
    Aig_Obj_t * pObj, * pObjRepr, * pObjNew, * pObjNewRepr;
    int i, nResets = Ntl_ManAigCountResets( pNtl );
    int nTruePis = Aig_ManPiNum(p) - Aig_ManRegNum(p);
    assert( pNew->pReprs != NULL );
    assert( nResets == Aig_ManPiNum(pNew) - Aig_ManPiNum(p) );
    Aig_ManReprStart( p, Aig_ManObjNumMax(p) );
    Aig_ManForEachLoSeq( pNew, pObjNew, i )
    {
        pObj = Aig_ManPi( p, i - nResets );
        pObjNewRepr = pNew->pReprs[pObjNew->Id];
        if ( pObjNewRepr == NULL )
            continue;
        if ( pObjNewRepr == Aig_ManConst1(pNew) )
        {
            Aig_ObjCreateRepr( p, Aig_ManConst1(p), pObj );
            continue;
        }
        assert( Aig_ObjIsPi(pObjNewRepr) );
        // find the corresponding representative node
        pObjRepr = Aig_ManPi( p, Aig_ObjPioNum(pObjNewRepr) - nResets );
        // if they belong to different domains, quit
        if ( Vec_IntEntry( pNtl->vRstClasses, Aig_ObjPioNum(pObj) - nTruePis ) != 
             Vec_IntEntry( pNtl->vRstClasses, Aig_ObjPioNum(pObjRepr) - nTruePis ) )
            continue;
        Aig_ObjCreateRepr( p, pObjRepr, pObj );
    }
} 

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

  Synopsis    [Remaps equivalence classes from the new nodes to the old ones.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Ntl_ManRemapClassesScorr( Ntl_Man_t * pNtl, Aig_Man_t * p, Aig_Man_t * pNew )
{
    Aig_Obj_t * pObj, * pObjRepr, * pObjNew, * pObjNewRepr;
    int i;
    // map things back
    Aig_ManForEachObj( p, pObj, i )
    {
        pObjNew = (Aig_Obj_t *)pObj->pData;
        assert( pObjNew != NULL && !Aig_IsComplement(pObjNew) );
        pObjNew->pData = pObj;
    }
    // remap the classes
    Aig_ManForEachObj( pNew, pObjNew, i )
    {
        pObjNewRepr = pNew->pReprs[pObjNew->Id];
        if ( pObjNewRepr == NULL )
            continue;
        pObj = (Aig_Obj_t *)pObjNew->pData;
        pObjRepr = (Aig_Obj_t *)pObjNewRepr->pData;    
        assert( Aig_ObjId(pObjRepr) < Aig_ObjId(pObj) );
        Aig_ObjCreateRepr( p, pObjRepr, pObj );
    }
}


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

  Synopsis    [Performs sequential cleanup.]

  Description [Consumes the input NTL to save memory.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Ntl_Man_t * Ntl_ManScl( Ntl_Man_t * p, int fLatchConst, int fLatchEqual, int fVerbose )
{
    Ntl_Man_t * pNew, * pAux;
    Aig_Man_t * pAig, * pAigCol, * pTemp;

    // collapse the AIG
    pAig = Ntl_ManExtract( p );
//Ntl_ManPrintStats( p );
//Aig_ManPrintStats( pAig );
    pNew = Ntl_ManInsertAig( p, pAig );
    Ntl_ManFree( p );
    pAigCol = Ntl_ManCollapseSeq( pNew, 0, fVerbose );
    if ( pAigCol == NULL )
    {
        Aig_ManStop( pAig );
        return pNew;
    }
//Ntl_ManPrintStats( pNew );
//Aig_ManPrintStats( pAigCol );

    // perform SCL
    if ( pNew->pNal )
    {
        Aig_Man_t * pAigRst;
        pAigRst = Ntl_ManAigToRst( pNew, pAigCol );
        pTemp = Aig_ManScl( pAigRst, fLatchConst, fLatchEqual, 0, -1, -1, fVerbose, 0 );
        Aig_ManStop( pTemp );
        Ntl_ManRemapClassesLcorr( pNew, pAigCol, pAigRst );
        Aig_ManStop( pAigRst );
    }
    else
    {
        pTemp = Aig_ManScl( pAigCol, fLatchConst, fLatchEqual, 0, -1, -1, fVerbose, 0 );
        Aig_ManStop( pTemp );
    }

    // finalize the transformation
    pNew = Ntl_ManFinalize( pAux = pNew, pAig, pAigCol, fVerbose );
    Ntl_ManFree( pAux );
    Aig_ManStop( pAig );
    Aig_ManStop( pAigCol );
    return pNew;
}

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

  Synopsis    [Returns AIG with WB after fraiging.]

  Description [Consumes the input NTL to save memory.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Ntl_Man_t * Ntl_ManLcorr( Ntl_Man_t * p, int nConfMax, int fScorrGia, int fUseCSat, int fVerbose )
{
    Ntl_Man_t * pNew, * pAux;
    Aig_Man_t * pAig, * pAigCol, * pTemp;
    Ssw_Pars_t Pars, * pPars = &Pars;
    Ssw_ManSetDefaultParamsLcorr( pPars );
    pPars->nBTLimit = nConfMax;
    pPars->fVerbose = fVerbose;

    // collapse the AIG
    pAig = Ntl_ManExtract( p );
    pNew = Ntl_ManInsertAig( p, pAig );
    Ntl_ManFree( p );
    pAigCol = Ntl_ManCollapseSeq( pNew, pPars->nMinDomSize, pPars->fVerbose );
    if ( pAigCol == NULL )
    {
        Aig_ManStop( pAig );
        return pNew;
    }

    // perform LCORR
    pPars->fScorrGia = fScorrGia;
    pPars->fUseCSat  = fUseCSat;
    if ( pNew->pNal )
    {
        Aig_Man_t * pAigRst;
        pAigRst = Ntl_ManAigToRst( pNew, pAigCol );
        pTemp = Ssw_LatchCorrespondence( pAigRst, pPars );
        Aig_ManStop( pTemp );
        Ntl_ManRemapClassesLcorr( pNew, pAigCol, pAigRst );
        Aig_ManStop( pAigRst );
    }
    else
    {
        pTemp = Ssw_LatchCorrespondence( pAigCol, pPars );
        Aig_ManStop( pTemp );
    }

    // finalize the transformation
    pNew = Ntl_ManFinalize( pAux = pNew, pAig, pAigCol, fVerbose );
    Ntl_ManFree( pAux );
    Aig_ManStop( pAig );
    Aig_ManStop( pAigCol );
    return pNew;
}

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

  Synopsis    [Returns AIG with WB after fraiging.]

  Description [Consumes the input NTL to save memory.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Ntl_Man_t * Ntl_ManSsw( Ntl_Man_t * p, Fra_Ssw_t * pPars )
{
    Ntl_Man_t * pNew, * pAux;
    Aig_Man_t * pAig, * pAigCol, * pTemp;
    assert( 0 ); // not updated for nal

    // collapse the AIG
    pAig = Ntl_ManExtract( p );
    pNew = Ntl_ManInsertAig( p, pAig );
    Ntl_ManFree( p );
    pAigCol = Ntl_ManCollapseSeq( pNew, pPars->nMinDomSize, pPars->fVerbose );
    if ( pAigCol == NULL )
    {
        Aig_ManStop( pAig );
        return pNew;
    }

    // perform SCL for the given design
    pTemp = Fra_FraigInduction( pAigCol, pPars );
    Aig_ManStop( pTemp );

    // finalize the transformation
    pNew = Ntl_ManFinalize( pAux = pNew, pAig, pAigCol, pPars->fVerbose );
    Ntl_ManFree( pAux );
    Aig_ManStop( pAig );
    Aig_ManStop( pAigCol );
    return pNew;
}

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

  Synopsis    [Returns AIG with WB after fraiging.]

  Description [Consumes the input NTL to save memory.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Ntl_Man_t * Ntl_ManScorr( Ntl_Man_t * p, Ssw_Pars_t * pPars )
{
    Ntl_Man_t * pNew, * pAux;
    Aig_Man_t * pAig, * pAigCol, * pTemp;

    // collapse the AIG
    pAig = Ntl_ManExtract( p );
    pNew = Ntl_ManInsertAig( p, pAig );
    Ntl_ManFree( p );
    pAigCol = Ntl_ManCollapseSeq( pNew, pPars->nMinDomSize, pPars->fVerbose );
    if ( pAigCol == NULL )
    {
        Aig_ManStop( pAig );
        return pNew;
    }

    // perform SCL
    if ( pNew->pNal )
    {
        Aig_Man_t * pAigRst;
        pAigRst = Ntl_ManAigToRst( pNew, pAigCol );
        pTemp = Ssw_SignalCorrespondence( pAigRst, pPars );
        Aig_ManStop( pTemp );
        Ntl_ManRemapClassesLcorr( pNew, pAigCol, pAigRst );
        Aig_ManStop( pAigRst );
    }
    else
    {
        pPars->fVerbose = 1;

        pTemp = Ssw_SignalCorrespondence( pAigCol, pPars );
        Aig_ManStop( pTemp );
    }

    // finalize the transformation
    pNew = Ntl_ManFinalize( pAux = pNew, pAig, pAigCol, pPars->fVerbose );
    Ntl_ManFree( pAux );
    Aig_ManStop( pAig );
    Aig_ManStop( pAigCol );
    return pNew;
}


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

  Synopsis    [Transfers the copy field into the second copy field.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Ntl_ManTransferCopy( Ntl_Man_t * p )
{
    Ntl_Net_t * pNet;
    Ntl_Mod_t * pRoot;
    int i;
    pRoot = Ntl_ManRootModel( p );
    Ntl_ModelForEachNet( pRoot, pNet, i )
    {
        pNet->pCopy2 = pNet->pCopy;
        pNet->pCopy = NULL;
    }
}

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

  Synopsis    [Reattaches one white-box.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Ntl_ManAttachWhiteBox( Ntl_Man_t * p, Aig_Man_t * pAigCol, Aig_Man_t * pAigRed, Ntl_Man_t * pNew, Ntl_Obj_t * pBox )
{
}

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

  Synopsis    [Reattaches white-boxes after reducing the netlist.]

  Description [The following parameters are given:
  Original netlist (p) whose nets point to the nodes of collapsed AIG.
  Collapsed AIG (pAigCol) whose objects point to those of reduced AIG.
  Reduced AIG (pAigRed) whose objects point to the nets of the new netlist.
  The new netlist is changed by this procedure to have those white-boxes
  from the original AIG (p) those outputs are preserved after reduction.
  Note that if outputs are preserved, the inputs are also preserved.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Ntl_ManAttachWhiteBoxes( Ntl_Man_t * p, Aig_Man_t * pAigCol, Aig_Man_t * pAigRed, Ntl_Man_t * pNew, int fVerbose )
{
    Ntl_Mod_t * pRoot;
    Ntl_Obj_t * pBox;
    Ntl_Net_t * pNet;
    int i, k, Counter = 0;
    // go through the white-boxes and check if they are preserved
    pRoot = Ntl_ManRootModel( p );
    Ntl_ModelForEachBox( pRoot, pBox, i )
    {
        Ntl_ObjForEachFanout( pBox, pNet, k )
        {
            // skip dangling outputs of the box
            if ( pNet->pCopy == NULL )
                continue;
            // skip the outputs that are not preserved after merging equivalence
            if ( Aig_Regular((Aig_Obj_t *)pNet->pCopy2)->pData == NULL )
                continue;
            break;
        }
        if ( k == Ntl_ObjFanoutNum(pBox) )
            continue;
        // the box is preserved
        Ntl_ManAttachWhiteBox( p, pAigCol, pAigRed, pNew, pBox );
        Counter++;
    }
    if ( fVerbose )
        printf( "Attached %d boxed (out of %d).\n", Counter, Ntl_ModelBoxNum(pRoot) );
}

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

  Synopsis    [Flip complemented edges.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Ntl_ManFlipEdges( Ntl_Man_t * p, Aig_Man_t * pAigCol )
{
    Ntl_Mod_t * pRoot;
    Ntl_Obj_t * pObj;
    Aig_Obj_t * pObjCol, * pFanin;
    int i, iLatch;
    pRoot = Ntl_ManRootModel(p);
    iLatch = 0;
    Ntl_ModelForEachLatch( pRoot, pObj, i )
    {
        if ( Ntl_ObjIsInit1( pObj ) )
        {
            pObjCol = Aig_ManPi( pAigCol, Ntl_ModelPiNum(pRoot) + iLatch );
            assert( pObjCol->fMarkA == 0 );
            pObjCol->fMarkA = 1;
        }
        iLatch++;
    }
    // flip pointers to the complemented edges
    Aig_ManForEachObj( pAigCol, pObjCol, i )
    {
        pFanin = Aig_ObjFanin0(pObjCol);
        if ( pFanin && pFanin->fMarkA )
            pObjCol->pFanin0 = Aig_Not(pObjCol->pFanin0);
        pFanin = Aig_ObjFanin1(pObjCol);
        if ( pFanin && pFanin->fMarkA )
            pObjCol->pFanin1 = Aig_Not(pObjCol->pFanin1);
    }
    // flip complemented latch derivers and undo the marks
    iLatch = 0;
    Ntl_ModelForEachLatch( pRoot, pObj, i )
    {
        if ( Ntl_ObjIsInit1( pObj ) )
        {
            // flip the latch input
            pObjCol = Aig_ManPo( pAigCol, Ntl_ModelPoNum(pRoot) + iLatch );
            pObjCol->pFanin0 = Aig_Not(pObjCol->pFanin0);
            // unmark the latch output
            pObjCol = Aig_ManPi( pAigCol, Ntl_ModelPiNum(pRoot) + iLatch );
            assert( pObjCol->fMarkA == 1 );
            pObjCol->fMarkA = 0;
        }
        iLatch++;
    }
}

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

  Synopsis    [Returns AIG with WB after sequential SAT sweeping.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Ntl_Man_t * Ntl_ManSsw2( Ntl_Man_t * p, Fra_Ssw_t * pPars )
{
    Ntl_Man_t * pNew;
    Aig_Man_t * pAigRed, * pAigCol;
    // collapse the AIG
    pAigCol = Ntl_ManCollapseSeq( p, pPars->nMinDomSize, pPars->fVerbose );
    // transform the collapsed AIG
    pAigRed = Fra_FraigInduction( pAigCol, pPars );
    Aig_ManStop( pAigRed );
    pAigRed = Aig_ManDupReprBasic( pAigCol );
    // insert the result back
    Ntl_ManFlipEdges( p, pAigRed );
    Ntl_ManTransferCopy( p );
    pNew = Ntl_ManInsertAig( p, pAigRed );
    // attach the white-boxes
    Ntl_ManAttachWhiteBoxes( p, pAigCol, pAigRed, pNew, pPars->fVerbose );
    Ntl_ManSweep( pNew, pPars->fVerbose );
    // cleanup
    Aig_ManStop( pAigRed );
    Aig_ManStop( pAigCol );
    return pNew;
}

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


ABC_NAMESPACE_IMPL_END