summaryrefslogtreecommitdiffstats
path: root/src/aig/saig/saigWnd.c
blob: 949a2728ec81bb790bb0dbebbb30d2f65a8ca4d7 (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
/**CFile****************************************************************

  FileName    [saigWnd.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [Sequential AIG package.]

  Synopsis    [Sequential windowing.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

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

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

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

#include "saig.h"

ABC_NAMESPACE_IMPL_START


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

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

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

  Synopsis    [Returns the array of PI/internal nodes.]

  Description [Marks all the visited nodes with the current ID.
  Does not collect constant node and PO/LI nodes.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Saig_ManWindowOutline_rec( Aig_Man_t * p, Aig_Obj_t * pObj, int nDist, Vec_Ptr_t * vNodes, int * pDists )
{
    Aig_Obj_t * pMatch, * pFanout;
    int fCollected, iFanout = -1, i;
    if ( nDist == 0 )
        return;
    if ( pDists[pObj->Id] >= nDist )
        return;
    pDists[pObj->Id] = nDist;
    fCollected = Aig_ObjIsTravIdCurrent( p, pObj );
    Aig_ObjSetTravIdCurrent( p, pObj );    
    if ( Aig_ObjIsConst1(pObj) )
        return;
    if ( Saig_ObjIsPo(p, pObj) )
        return;
    if ( Saig_ObjIsLi(p, pObj) )
    {
        pMatch = Saig_ObjLiToLo( p, pObj );
        if ( !Aig_ObjIsTravIdCurrent( p, pMatch ) )
            Saig_ManWindowOutline_rec( p, pMatch, nDist, vNodes, pDists );
        Saig_ManWindowOutline_rec( p, Aig_ObjFanin0(pObj), nDist-1, vNodes, pDists );
        return;
    }
    if ( !fCollected )
        Vec_PtrPush( vNodes, pObj );
    if ( Saig_ObjIsPi(p, pObj) )
        return;
    if ( Saig_ObjIsLo(p, pObj) )
    {
        pMatch = Saig_ObjLoToLi( p, pObj );
        if ( !Aig_ObjIsTravIdCurrent( p, pMatch ) )
            Saig_ManWindowOutline_rec( p, pMatch, nDist, vNodes, pDists );
        Aig_ObjForEachFanout( p, pObj, pFanout, iFanout, i )
            Saig_ManWindowOutline_rec( p, pFanout, nDist-1, vNodes, pDists );
        return;
    }
    assert( Aig_ObjIsNode(pObj) );
    Saig_ManWindowOutline_rec( p, Aig_ObjFanin0(pObj), nDist-1, vNodes, pDists );
    Saig_ManWindowOutline_rec( p, Aig_ObjFanin1(pObj), nDist-1, vNodes, pDists );
    Aig_ObjForEachFanout( p, pObj, pFanout, iFanout, i )
        Saig_ManWindowOutline_rec( p, pFanout, nDist-1, vNodes, pDists );
}

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

  Synopsis    [Returns the array of PI/internal nodes.]

  Description [Marks all the visited nodes with the current ID.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Vec_Ptr_t * Saig_ManWindowOutline( Aig_Man_t * p, Aig_Obj_t * pObj, int nDist )
{
    Vec_Ptr_t * vNodes;
    Aig_Obj_t * pObjLi, * pObjLo;
    int * pDists, i;
    pDists = ABC_CALLOC( int, Aig_ManObjNumMax(p) );
    vNodes = Vec_PtrAlloc( 1000 );
    Aig_ManIncrementTravId( p );
    Saig_ManWindowOutline_rec( p, pObj, nDist, vNodes, pDists );
    Vec_PtrSort( vNodes, (int (*)(const void *, const void *))Aig_ObjCompareIdIncrease );
    // make sure LI/LO are labeled/unlabeled mutually
    Saig_ManForEachLiLo( p, pObjLi, pObjLo, i )
        assert( Aig_ObjIsTravIdCurrent(p, pObjLi) == 
                Aig_ObjIsTravIdCurrent(p, pObjLo) );
    ABC_FREE( pDists );
    return vNodes;
}

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

  Synopsis    [Returns 1 if the node has unlabeled fanout.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Aig_Obj_t * Saig_ObjHasUnlabeledFanout( Aig_Man_t * p, Aig_Obj_t * pObj )
{
    Aig_Obj_t * pFanout;
    int iFanout = -1, i;
    Aig_ObjForEachFanout( p, pObj, pFanout, iFanout, i )
        if ( Saig_ObjIsPo(p, pFanout) || !Aig_ObjIsTravIdCurrent(p, pFanout) )
            return pFanout;
    return NULL;
}

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

  Synopsis    [Collects primary inputs.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Vec_Ptr_t * Saig_ManWindowCollectPis( Aig_Man_t * p, Vec_Ptr_t * vNodes )
{
    Vec_Ptr_t * vNodesPi;
    Aig_Obj_t * pObj, * pMatch, * pFanin;
    int i;
    vNodesPi = Vec_PtrAlloc( 1000 );
    Vec_PtrForEachEntry( Aig_Obj_t *, vNodes, pObj, i )
    {
        if ( Saig_ObjIsPi(p, pObj) )
        {
            assert( pObj->pData == NULL );
            Vec_PtrPush( vNodesPi, pObj );
        }
        else if ( Saig_ObjIsLo(p, pObj) )
        {
            pMatch = Saig_ObjLoToLi( p, pObj );
            pFanin = Aig_ObjFanin0(pMatch);
            if ( !Aig_ObjIsTravIdCurrent(p, pFanin) && pFanin->pData == NULL )
                Vec_PtrPush( vNodesPi, pFanin );
        }
        else
        {
            assert( Aig_ObjIsNode(pObj) );
            pFanin = Aig_ObjFanin0(pObj);
            if ( !Aig_ObjIsTravIdCurrent(p, pFanin) && pFanin->pData == NULL )
                Vec_PtrPush( vNodesPi, pFanin );
            pFanin = Aig_ObjFanin1(pObj);
            if ( !Aig_ObjIsTravIdCurrent(p, pFanin) && pFanin->pData == NULL )
                Vec_PtrPush( vNodesPi, pFanin );
        }
    }
    return vNodesPi;
}

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

  Synopsis    [Collects primary outputs.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Vec_Ptr_t * Saig_ManWindowCollectPos( Aig_Man_t * p, Vec_Ptr_t * vNodes, Vec_Ptr_t ** pvPointers )
{
    Vec_Ptr_t * vNodesPo;
    Aig_Obj_t * pObj, * pPointer;
    int i;
    vNodesPo = Vec_PtrAlloc( 1000 );
    if ( pvPointers )
        *pvPointers = Vec_PtrAlloc( 1000 );
    Vec_PtrForEachEntry( Aig_Obj_t *, vNodes, pObj, i )
    {
        if ( (pPointer = Saig_ObjHasUnlabeledFanout(p, pObj)) )
        {
            Vec_PtrPush( vNodesPo, pObj );
            if ( pvPointers )
                Vec_PtrPush( *pvPointers, pPointer );
        }
    }
    return vNodesPo;
}

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

  Synopsis    [Extracts the window AIG from the AIG manager.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Aig_Man_t * Saig_ManWindowExtractNodes( Aig_Man_t * p, Vec_Ptr_t * vNodes )
{
    Aig_Man_t * pNew;
    Aig_Obj_t * pObj, * pMatch;
    Vec_Ptr_t * vNodesPi, * vNodesPo;
    int i, nRegCount;
    Aig_ManCleanData( p ); 
    // create the new manager
    pNew = Aig_ManStart( Vec_PtrSize(vNodes) );
    pNew->pName = Abc_UtilStrsav( "wnd" );
    pNew->pSpec = NULL;
    // map constant nodes
    pObj = Aig_ManConst1( p );
    pObj->pData = Aig_ManConst1( pNew );
    // create real PIs
    vNodesPi = Saig_ManWindowCollectPis( p, vNodes );
    Vec_PtrForEachEntry( Aig_Obj_t *, vNodesPi, pObj, i )
        pObj->pData = Aig_ObjCreateCi(pNew);
    Vec_PtrFree( vNodesPi );
    // create register outputs
    Vec_PtrForEachEntry( Aig_Obj_t *, vNodes, pObj, i )
    {
        if ( Saig_ObjIsLo(p, pObj) )
            pObj->pData = Aig_ObjCreateCi(pNew);
    }
    // create internal nodes
    Vec_PtrForEachEntry( Aig_Obj_t *, vNodes, pObj, i )
    {
        if ( Aig_ObjIsNode(pObj) )
            pObj->pData = Aig_And( pNew, Aig_ObjChild0Copy(pObj), Aig_ObjChild1Copy(pObj) );
    }
    // create POs
    vNodesPo = Saig_ManWindowCollectPos( p, vNodes, NULL );
    Vec_PtrForEachEntry( Aig_Obj_t *, vNodesPo, pObj, i )
        Aig_ObjCreateCo( pNew, (Aig_Obj_t *)pObj->pData );
    Vec_PtrFree( vNodesPo );
    // create register inputs
    nRegCount = 0;
    Vec_PtrForEachEntry( Aig_Obj_t *, vNodes, pObj, i )
    {
        if ( Saig_ObjIsLo(p, pObj) )
        {
            pMatch = Saig_ObjLoToLi( p, pObj );
            Aig_ObjCreateCo( pNew, Aig_ObjChild0Copy(pMatch) );
            nRegCount++;
        }
    }
    Aig_ManSetRegNum( pNew, nRegCount );
    Aig_ManCleanup( pNew );
    return pNew;
}

static void Saig_ManWindowInsertSmall_rec( Aig_Man_t * pNew, Aig_Obj_t * pObjSmall, 
     Vec_Ptr_t * vBigNode2SmallPo, Vec_Ptr_t * vSmallPi2BigNode );

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

  Synopsis    [Adds nodes for the big manager.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Saig_ManWindowInsertBig_rec( Aig_Man_t * pNew, Aig_Obj_t * pObjBig, 
         Vec_Ptr_t * vBigNode2SmallPo, Vec_Ptr_t * vSmallPi2BigNode )
{
    Aig_Obj_t * pMatch;
    if ( pObjBig->pData )
        return;
    if ( (pMatch = (Aig_Obj_t *)Vec_PtrEntry( vBigNode2SmallPo, pObjBig->Id )) )
    {
        Saig_ManWindowInsertSmall_rec( pNew, Aig_ObjFanin0(pMatch), vBigNode2SmallPo, vSmallPi2BigNode );
        pObjBig->pData = Aig_ObjChild0Copy(pMatch);
        return;        
    }
    assert( Aig_ObjIsNode(pObjBig) );
    Saig_ManWindowInsertBig_rec( pNew, Aig_ObjFanin0(pObjBig), vBigNode2SmallPo, vSmallPi2BigNode );
    Saig_ManWindowInsertBig_rec( pNew, Aig_ObjFanin1(pObjBig), vBigNode2SmallPo, vSmallPi2BigNode );
    pObjBig->pData = Aig_And( pNew, Aig_ObjChild0Copy(pObjBig), Aig_ObjChild1Copy(pObjBig) );
}

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

  Synopsis    [Adds nodes for the small manager.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Saig_ManWindowInsertSmall_rec( Aig_Man_t * pNew, Aig_Obj_t * pObjSmall, 
         Vec_Ptr_t * vBigNode2SmallPo, Vec_Ptr_t * vSmallPi2BigNode )
{
    Aig_Obj_t * pMatch;
    if ( pObjSmall->pData )
        return;
    if ( (pMatch = (Aig_Obj_t *)Vec_PtrEntry( vSmallPi2BigNode, pObjSmall->Id )) )
    {
        Saig_ManWindowInsertBig_rec( pNew, pMatch, vBigNode2SmallPo, vSmallPi2BigNode );
        pObjSmall->pData = pMatch->pData;
        return;        
    }
    assert( Aig_ObjIsNode(pObjSmall) );
    Saig_ManWindowInsertSmall_rec( pNew, Aig_ObjFanin0(pObjSmall), vBigNode2SmallPo, vSmallPi2BigNode );
    Saig_ManWindowInsertSmall_rec( pNew, Aig_ObjFanin1(pObjSmall), vBigNode2SmallPo, vSmallPi2BigNode );
    pObjSmall->pData = Aig_And( pNew, Aig_ObjChild0Copy(pObjSmall), Aig_ObjChild1Copy(pObjSmall) );
}

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

  Synopsis    [Extracts the network from the AIG manager.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Aig_Man_t * Saig_ManWindowInsertNodes( Aig_Man_t * p, Vec_Ptr_t * vNodes, Aig_Man_t * pWnd )
{
    Aig_Man_t * pNew;
    Vec_Ptr_t * vBigNode2SmallPo, * vSmallPi2BigNode;
    Vec_Ptr_t * vNodesPi, * vNodesPo;
    Aig_Obj_t * pObj;
    int i;

    // set mapping of small PIs into big nodes
    vSmallPi2BigNode = Vec_PtrStart( Aig_ManObjNumMax(pWnd) ); 
    vNodesPi = Saig_ManWindowCollectPis( p, vNodes );
    Vec_PtrForEachEntry( Aig_Obj_t *, vNodesPi, pObj, i )
        Vec_PtrWriteEntry( vSmallPi2BigNode, Aig_ManCi(pWnd, i)->Id, pObj );
    assert( i == Saig_ManPiNum(pWnd) );
    Vec_PtrFree( vNodesPi );

    // set mapping of big nodes into small POs
    vBigNode2SmallPo = Vec_PtrStart( Aig_ManObjNumMax(p) ); 
    vNodesPo = Saig_ManWindowCollectPos( p, vNodes, NULL );
    Vec_PtrForEachEntry( Aig_Obj_t *, vNodesPo, pObj, i )
        Vec_PtrWriteEntry( vBigNode2SmallPo, pObj->Id, Aig_ManCo(pWnd, i) );
    assert( i == Saig_ManPoNum(pWnd) );
    Vec_PtrFree( vNodesPo );

    // create the new manager
    Aig_ManCleanData( p ); 
    Aig_ManCleanData( pWnd ); 
    pNew = Aig_ManStart( Aig_ManObjNumMax(p) );
    pNew->pName = Abc_UtilStrsav( p->pName );
    pNew->pSpec = Abc_UtilStrsav( p->pSpec );
    // map constant nodes
    pObj = Aig_ManConst1( p );
    pObj->pData = Aig_ManConst1( pNew );
    pObj = Aig_ManConst1( pWnd );
    pObj->pData = Aig_ManConst1( pNew );

    // create real PIs
    Aig_ManForEachCi( p, pObj, i )
        if ( Saig_ObjIsPi(p, pObj) || !Aig_ObjIsTravIdCurrent(p, pObj) )
            pObj->pData = Aig_ObjCreateCi(pNew);
    // create additional latch outputs
    Saig_ManForEachLo( pWnd, pObj, i )
        pObj->pData = Aig_ObjCreateCi(pNew);

    // create internal nodes starting from the big
    Aig_ManForEachCo( p, pObj, i )
        if ( Saig_ObjIsPo(p, pObj) || !Aig_ObjIsTravIdCurrent(p, pObj) )
        {
            Saig_ManWindowInsertBig_rec( pNew, Aig_ObjFanin0(pObj), vBigNode2SmallPo, vSmallPi2BigNode );
            pObj->pData = Aig_ObjCreateCo( pNew, Aig_ObjChild0Copy(pObj) );
        }
    // create internal nodes starting from the small
    Saig_ManForEachLi( pWnd, pObj, i )
    {
        Saig_ManWindowInsertSmall_rec( pNew, Aig_ObjFanin0(pObj), vBigNode2SmallPo, vSmallPi2BigNode );
        pObj->pData = Aig_ObjCreateCo( pNew, Aig_ObjChild0Copy(pObj) );
    }
    Vec_PtrFree( vBigNode2SmallPo );
    Vec_PtrFree( vSmallPi2BigNode );
    // set the new number of registers
    assert( Aig_ManCiNum(pNew) - Aig_ManCiNum(p) == Aig_ManCoNum(pNew) - Aig_ManCoNum(p) );
    Aig_ManSetRegNum( pNew, Aig_ManRegNum(p) + (Aig_ManCiNum(pNew) - Aig_ManCiNum(p)) );
    Aig_ManCleanup( pNew );
    return pNew;
}

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

  Synopsis    [Find a good object.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Aig_Obj_t * Saig_ManFindPivot( Aig_Man_t * p )
{
    Aig_Obj_t * pObj;
    int i, Counter;
    if ( Aig_ManRegNum(p) > 0 )
    {
        if ( Aig_ManRegNum(p) == 1 )
            return Saig_ManLo( p, 0 );
        Saig_ManForEachLo( p, pObj, i )
        {
            if ( i == Aig_ManRegNum(p)/2 )
                return pObj;
        }
    }
    else
    {
        Counter = 0;
        assert( Aig_ManNodeNum(p) > 1 );
        Aig_ManForEachNode( p, pObj, i )
        {
            if ( Counter++ == Aig_ManNodeNum(p)/2 )
                return pObj;
        }
    }
    return NULL;
}

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

  Synopsis    [Computes sequential window of the node.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Aig_Man_t * Saig_ManWindowExtract( Aig_Man_t * p, Aig_Obj_t * pObj, int nDist )
{
    Aig_Man_t * pWnd;
    Vec_Ptr_t * vNodes;
    Aig_ManFanoutStart( p );
    vNodes = Saig_ManWindowOutline( p, pObj, nDist );
    pWnd = Saig_ManWindowExtractNodes( p, vNodes );
    Vec_PtrFree( vNodes );
    Aig_ManFanoutStop( p );
    return pWnd;
}

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

  Synopsis    [Computes sequential window of the node.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Aig_Man_t * Saig_ManWindowInsert( Aig_Man_t * p, Aig_Obj_t * pObj, int nDist, Aig_Man_t * pWnd )
{
    Aig_Man_t * pNew, * pWndTest;
    Vec_Ptr_t * vNodes;
    Aig_ManFanoutStart( p );

    vNodes = Saig_ManWindowOutline( p, pObj, nDist );
    pWndTest = Saig_ManWindowExtractNodes( p, vNodes );
    if ( Saig_ManPiNum(pWndTest) != Saig_ManPiNum(pWnd) ||
         Saig_ManPoNum(pWndTest) != Saig_ManPoNum(pWnd) )
    {
        printf( "The window cannot be reinserted because PI/PO counts do not match.\n" );
        Aig_ManStop( pWndTest );
        Vec_PtrFree( vNodes );
        Aig_ManFanoutStop( p );
        return NULL;
    }
    Aig_ManStop( pWndTest );
    Vec_PtrFree( vNodes );

    // insert the nodes
    Aig_ManCleanData( p ); 
    vNodes = Saig_ManWindowOutline( p, pObj, nDist );
    pNew = Saig_ManWindowInsertNodes( p, vNodes, pWnd );
    Vec_PtrFree( vNodes );
    Aig_ManFanoutStop( p );
    return pNew;
}

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

  Synopsis    [Tests the above computation.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Aig_Man_t * Saig_ManWindowTest( Aig_Man_t * p )
{
    int nDist = 3;
    Aig_Man_t * pWnd, * pNew;
    Aig_Obj_t * pPivot;
    pPivot = Saig_ManFindPivot( p );
    assert( pPivot != NULL );
    pWnd = Saig_ManWindowExtract( p, pPivot, nDist );
    pNew = Saig_ManWindowInsert( p, pPivot, nDist, pWnd );
    Aig_ManStop( pWnd );
    return pNew;
}

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

  Synopsis    [Collects the nodes that are not linked to each other.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Vec_Ptr_t * Saig_ManCollectedDiffNodes( Aig_Man_t * p0, Aig_Man_t * p1 )
{
    Vec_Ptr_t * vNodes;
    Aig_Obj_t * pObj0, * pObj1;
    int i;
    // collect nodes that are not linked
    Aig_ManIncrementTravId( p0 );
    vNodes = Vec_PtrAlloc( 1000 );
    Aig_ManForEachObj( p0, pObj0, i )
    {
        pObj1 = Aig_ObjRepr( p0, pObj0 );
        if ( pObj1 != NULL )
        {
            assert( pObj0 == Aig_ObjRepr( p1, pObj1 ) );   
            continue;
        }
        // mark and collect unmatched objects
        Aig_ObjSetTravIdCurrent( p0, pObj0 ); 
        if ( Aig_ObjIsNode(pObj0) || Aig_ObjIsCi(pObj0) )
            Vec_PtrPush( vNodes, pObj0 );
    }
    // make sure LI/LO are labeled/unlabeled mutually
    Saig_ManForEachLiLo( p0, pObj0, pObj1, i )
        assert( Aig_ObjIsTravIdCurrent(p0, pObj0) == 
                Aig_ObjIsTravIdCurrent(p0, pObj1) );
    return vNodes;
}
         
/**Function*************************************************************

  Synopsis    [Creates PIs of the miter.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Saig_ManWindowCreatePis( Aig_Man_t * pNew, Aig_Man_t * p0, Aig_Man_t * p1, Vec_Ptr_t * vNodes0 )
{
    Aig_Obj_t * pObj, * pMatch, * pFanin;
    int i, Counter = 0;
    Vec_PtrForEachEntry( Aig_Obj_t *, vNodes0, pObj, i )
    {
        if ( Saig_ObjIsLo(p0, pObj) )
        {
            pMatch = Saig_ObjLoToLi( p0, pObj );
            pFanin = Aig_ObjFanin0(pMatch);
            if ( !Aig_ObjIsTravIdCurrent(p0, pFanin) && pFanin->pData == NULL )
            {
                pFanin->pData = Aig_ObjCreateCi(pNew);
                pMatch = Aig_ObjRepr( p0, pFanin );
                assert( pFanin == Aig_ObjRepr( p1, pMatch ) );
                assert( pMatch != NULL );
                pMatch->pData = pFanin->pData;
                Counter++;
            }
        }
        else
        {
            assert( Aig_ObjIsNode(pObj) );
            pFanin = Aig_ObjFanin0(pObj);
            if ( !Aig_ObjIsTravIdCurrent(p0, pFanin) && pFanin->pData == NULL )
            {
                pFanin->pData = Aig_ObjCreateCi(pNew);
                pMatch = Aig_ObjRepr( p0, pFanin );
                assert( pFanin == Aig_ObjRepr( p1, pMatch ) );
                assert( pMatch != NULL );
                pMatch->pData = pFanin->pData;
                Counter++;
            }
            pFanin = Aig_ObjFanin1(pObj);
            if ( !Aig_ObjIsTravIdCurrent(p0, pFanin) && pFanin->pData == NULL )
            {
                pFanin->pData = Aig_ObjCreateCi(pNew);
                pMatch = Aig_ObjRepr( p0, pFanin );
                assert( pFanin == Aig_ObjRepr( p1, pMatch ) );
                assert( pMatch != NULL );
                pMatch->pData = pFanin->pData;
                Counter++;
            }
        }
    }
//    printf( "Added %d primary inputs.\n", Counter );
}
        
/**Function*************************************************************

  Synopsis    [Creates POs of the miter.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Saig_ManWindowCreatePos( Aig_Man_t * pNew, Aig_Man_t * p0, Aig_Man_t * p1 )
{
    Aig_Obj_t * pObj0, * pObj1, * pMiter;
    Aig_Obj_t * pFanin0, * pFanin1;
    int i;
    Aig_ManForEachObj( p0, pObj0, i )
    {
        if ( Aig_ObjIsTravIdCurrent(p0, pObj0) )
            continue;
        if ( Aig_ObjIsConst1(pObj0) )
            continue;
        if ( Aig_ObjIsCi(pObj0) )
            continue;
        pObj1 = Aig_ObjRepr( p0, pObj0 );
        assert( pObj0 == Aig_ObjRepr( p1, pObj1 ) );
        if ( Aig_ObjIsCo(pObj0) )
        {
            pFanin0 = Aig_ObjFanin0(pObj0);
            pFanin1 = Aig_ObjFanin0(pObj1);
            assert( Aig_ObjIsTravIdCurrent(p0, pFanin0) ==
                    Aig_ObjIsTravIdCurrent(p1, pFanin1) );
            if ( Aig_ObjIsTravIdCurrent(p0, pFanin0) )
            {
                pMiter = Aig_Exor( pNew, (Aig_Obj_t *)pFanin0->pData, (Aig_Obj_t *)pFanin1->pData );
                Aig_ObjCreateCo( pNew, pMiter );
            }
        }
        else
        {
            assert( Aig_ObjIsNode(pObj0) );

            pFanin0 = Aig_ObjFanin0(pObj0);
            pFanin1 = Aig_ObjFanin0(pObj1);
            assert( Aig_ObjIsTravIdCurrent(p0, pFanin0) ==
                    Aig_ObjIsTravIdCurrent(p1, pFanin1) );
            if ( Aig_ObjIsTravIdCurrent(p0, pFanin0) )
            {
                pMiter = Aig_Exor( pNew, (Aig_Obj_t *)pFanin0->pData, (Aig_Obj_t *)pFanin1->pData );
                Aig_ObjCreateCo( pNew, pMiter );
            }

            pFanin0 = Aig_ObjFanin1(pObj0);
            pFanin1 = Aig_ObjFanin1(pObj1);
            assert( Aig_ObjIsTravIdCurrent(p0, pFanin0) ==
                    Aig_ObjIsTravIdCurrent(p1, pFanin1) );
            if ( Aig_ObjIsTravIdCurrent(p0, pFanin0) )
            {
                pMiter = Aig_Exor( pNew, (Aig_Obj_t *)pFanin0->pData, (Aig_Obj_t *)pFanin1->pData );
                Aig_ObjCreateCo( pNew, pMiter );
            }
        }
    }
}


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

  Synopsis    [Extracts the window AIG from the AIG manager.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Aig_Man_t * Saig_ManWindowExtractMiter( Aig_Man_t * p0, Aig_Man_t * p1 )
{
    Aig_Man_t * pNew;
    Aig_Obj_t * pObj0, * pObj1, * pMatch0, * pMatch1;
    Vec_Ptr_t * vNodes0, * vNodes1;
    int i, nRegCount;
    // add matching of POs and LIs
    Saig_ManForEachPo( p0, pObj0, i )
    {
        pObj1 = Aig_ManCo( p1, i );
        Aig_ObjSetRepr( p0, pObj0, pObj1 );
        Aig_ObjSetRepr( p1, pObj1, pObj0 );
    }
    Saig_ManForEachLi( p0, pObj0, i )
    {
        pMatch0 = Saig_ObjLiToLo( p0, pObj0 );
        pMatch1 = Aig_ObjRepr( p0, pMatch0 );
        if ( pMatch1 == NULL )
            continue;
        assert( pMatch0 == Aig_ObjRepr( p1, pMatch1 ) );
        pObj1 = Saig_ObjLoToLi( p1, pMatch1 );
        Aig_ObjSetRepr( p0, pObj0, pObj1 );
        Aig_ObjSetRepr( p1, pObj1, pObj0 );
    }
    // clean the markings
    Aig_ManCleanData( p0 ); 
    Aig_ManCleanData( p1 ); 
    // collect nodes that are not linked
    vNodes0 = Saig_ManCollectedDiffNodes( p0, p1 );
    vNodes1 = Saig_ManCollectedDiffNodes( p1, p0 );
    // create the new manager
    pNew = Aig_ManStart( Vec_PtrSize(vNodes0) + Vec_PtrSize(vNodes1) );
    pNew->pName = Abc_UtilStrsav( "wnd" );
    pNew->pSpec = NULL;
    // map constant nodes
    pObj0 = Aig_ManConst1( p0 );
    pObj0->pData = Aig_ManConst1( pNew );
    pObj1 = Aig_ManConst1( p1 );
    pObj1->pData = Aig_ManConst1( pNew );
    // create real PIs
    Saig_ManWindowCreatePis( pNew, p0, p1, vNodes0 );
    Saig_ManWindowCreatePis( pNew, p1, p0, vNodes1 );
    // create register outputs
    Vec_PtrForEachEntry( Aig_Obj_t *, vNodes0, pObj0, i )
    {
        if ( Saig_ObjIsLo(p0, pObj0) )
            pObj0->pData = Aig_ObjCreateCi(pNew);
    }
    Vec_PtrForEachEntry( Aig_Obj_t *, vNodes1, pObj1, i )
    {
        if ( Saig_ObjIsLo(p1, pObj1) )
            pObj1->pData = Aig_ObjCreateCi(pNew);
    }
    // create internal nodes
    Vec_PtrForEachEntry( Aig_Obj_t *, vNodes0, pObj0, i )
    {
        if ( Aig_ObjIsNode(pObj0) )
            pObj0->pData = Aig_And( pNew, Aig_ObjChild0Copy(pObj0), Aig_ObjChild1Copy(pObj0) );
    }
    Vec_PtrForEachEntry( Aig_Obj_t *, vNodes1, pObj1, i )
    {
        if ( Aig_ObjIsNode(pObj1) )
            pObj1->pData = Aig_And( pNew, Aig_ObjChild0Copy(pObj1), Aig_ObjChild1Copy(pObj1) );
    }
    // create POs
    Saig_ManWindowCreatePos( pNew, p0, p1 );
//    Saig_ManWindowCreatePos( pNew, p1, p0 );
    // create register inputs
    nRegCount = 0;
    Vec_PtrForEachEntry( Aig_Obj_t *, vNodes0, pObj0, i )
    {
        if ( Saig_ObjIsLo(p0, pObj0) )
        {
            pMatch0 = Saig_ObjLoToLi( p0, pObj0 );
            Aig_ObjCreateCo( pNew, Aig_ObjChild0Copy(pMatch0) );
            nRegCount++;
        }
    }
    Vec_PtrForEachEntry( Aig_Obj_t *, vNodes1, pObj1, i )
    {
        if ( Saig_ObjIsLo(p1, pObj1) )
        {
            pMatch1 = Saig_ObjLoToLi( p1, pObj1 );
            Aig_ObjCreateCo( pNew, Aig_ObjChild0Copy(pMatch1) );
            nRegCount++;
        }
    }
    Aig_ManSetRegNum( pNew, nRegCount );
    Aig_ManCleanup( pNew );
    Vec_PtrFree( vNodes0 );
    Vec_PtrFree( vNodes1 );
    return pNew;
}

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


ABC_NAMESPACE_IMPL_END