abcExtract.c 24 KB
Newer Older
Alan Mishchenko committed
1 2
/**CFile****************************************************************

3
  FileName    [abcShare.c]
Alan Mishchenko committed
4 5 6

  SystemName  [ABC: Logic synthesis and verification system.]

Alan Mishchenko committed
7
  PackageName [Network and node package.]
Alan Mishchenko committed
8

9
  Synopsis    [Shared logic extraction.]
Alan Mishchenko committed
10 11 12 13 14 15 16

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

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

17
  Revision    [$Id: abcShare.c,v 1.00 2005/06/20 00:00:00 alanmi Exp $]
Alan Mishchenko committed
18 19 20

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

21
#include "base/abc/abc.h"
Alan Mishchenko committed
22

23 24
ABC_NAMESPACE_IMPL_START

Alan Mishchenko committed
25 26 27
////////////////////////////////////////////////////////////////////////
///                        DECLARATIONS                              ///
////////////////////////////////////////////////////////////////////////
Alan Mishchenko committed
28

29 30 31 32 33 34 35 36 37 38 39
#define SHARE_NUM 2

typedef struct Abc_ShaMan_t_ Abc_ShaMan_t;
struct Abc_ShaMan_t_ 
{
    int             nMultiSize;
    int             fVerbose;
    Abc_Ntk_t *     pNtk;
    Vec_Ptr_t *     vBuckets;
    Vec_Int_t *     vObj2Lit;
    int             nStartCols;
40 41
    int             nCountGates;
    int             nFoundGates;
42 43 44 45
};

static inline word  Abc_NtkSharePack( int Lev, int Id )  { return (((word)Lev) << 32) | Id; }
static inline int   Abc_NtkShareUnpackLev( word Num )    { return (Num >> 32);              }
46
static inline int   Abc_NtkShareUnpackId( word Num )     { return Num & 0xFFFFFFFF;         }
47 48


Alan Mishchenko committed
49
////////////////////////////////////////////////////////////////////////
Alan Mishchenko committed
50
///                     FUNCTION DEFINITIONS                         ///
Alan Mishchenko committed
51 52 53 54
////////////////////////////////////////////////////////////////////////

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

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
  Synopsis    [Working with the manager.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Abc_ShaMan_t * Abc_ShaManStart( Abc_Ntk_t * pNtk )
{
    Abc_ShaMan_t * p;
    p = ABC_CALLOC( Abc_ShaMan_t, 1 );
    p->pNtk      = pNtk;
    p->vObj2Lit  = Vec_IntAlloc( 1000 );
    return p;
}
void Abc_ShaManStop( Abc_ShaMan_t * p )
{
    Vec_Ptr_t * vBucket;
    int i;
    Vec_PtrForEachEntry( Vec_Ptr_t *, p->vBuckets, vBucket, i )
        Vec_VecFree( (Vec_Vec_t *)vBucket );
    Vec_PtrFreeP( &p->vBuckets );
    Vec_IntFreeP( &p->vObj2Lit );
    ABC_FREE( p );
}

83

84 85
/**Function*************************************************************

86
  Synopsis    [Collects one multi-input gate.]
87 88 89 90 91 92 93 94

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
95
Vec_Wrd_t * Abc_NtkShareSuperXor( Abc_Obj_t * pObj, int * pfCompl, int * pCounter )
96 97
{
    Abc_Ntk_t * pNtk = Abc_ObjNtk(pObj);
98
    Abc_Obj_t * pObjC, * pObj0, * pObj1, * pRoot = NULL;
99 100
    Vec_Wrd_t * vSuper;
    word Num, NumNext;
101 102
    int i, k, fCompl = 0;
    assert( !Abc_ObjIsComplement(pObj) );
103 104 105 106 107 108 109 110 111 112
    assert( Abc_NodeIsExorType(pObj) );
    // start iteration
    vSuper = Vec_WrdAlloc( 10 );
    Vec_WrdPush( vSuper, Abc_NtkSharePack(Abc_ObjLevel(pObj), Abc_ObjId(pObj)) );
    while ( Vec_WrdSize(vSuper) > 0 )
    {
        // make sure there are no duplicates
        Num = Vec_WrdEntry( vSuper, 0 );
        Vec_WrdForEachEntryStart( vSuper, NumNext, i, 1 )
        {
113
            assert( Num < NumNext );
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
            Num = NumNext;
        }
        // extract XOR gate decomposable on the topmost level
        Vec_WrdForEachEntryReverse( vSuper, Num, i )
        {
            pRoot = Abc_NtkObj( pNtk, Abc_NtkShareUnpackId(Num) );
            if ( Abc_NodeIsExorType(pRoot) )
            {
                Vec_WrdRemove( vSuper, Num );
                break;
            }
        }
        if ( i == -1 )
            break;
        // extract
        pObjC = Abc_NodeRecognizeMux( pRoot, &pObj1, &pObj0 );
        assert( pObj1 == Abc_ObjNot(pObj0) );
        fCompl ^= Abc_ObjIsComplement(pObjC);  pObjC = Abc_ObjRegular(pObjC);
        fCompl ^= Abc_ObjIsComplement(pObj0);  pObj0 = Abc_ObjRegular(pObj0);
        Vec_WrdPushOrder( vSuper, Abc_NtkSharePack(Abc_ObjLevel(pObjC), Abc_ObjId(pObjC)) );
        Vec_WrdPushOrder( vSuper, Abc_NtkSharePack(Abc_ObjLevel(pObj0), Abc_ObjId(pObj0)) );
        (*pCounter)++;
        // remove duplicates
        k = 0;
        Vec_WrdForEachEntry( vSuper, Num, i )
        {
            if ( i + 1 == Vec_WrdSize(vSuper) )
            {
                Vec_WrdWriteEntry( vSuper, k++, Num );
                break;
            }
            NumNext = Vec_WrdEntry( vSuper, i+1 );
            assert( Num <= NumNext );
            if ( Num == NumNext )
                i++;
            else
                Vec_WrdWriteEntry( vSuper, k++, Num );
        }
        Vec_WrdShrink( vSuper, k );
    }
    *pfCompl = fCompl;
    Vec_WrdForEachEntry( vSuper, Num, i )
        Vec_WrdWriteEntry( vSuper, i, Abc_NtkShareUnpackId(Num) );
    return vSuper;
}
159 160 161
Vec_Wrd_t * Abc_NtkShareSuperAnd( Abc_Obj_t * pObj, int * pCounter )
{
    Abc_Ntk_t * pNtk = Abc_ObjNtk(pObj);
162
    Abc_Obj_t * pObj0, * pObj1, * pRoot = NULL;
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
    Vec_Wrd_t * vSuper;
    word Num, NumNext;
    int i, k;
    assert( !Abc_ObjIsComplement(pObj) );
    // start iteration
    vSuper = Vec_WrdAlloc( 10 );
    Vec_WrdPush( vSuper, Abc_NtkSharePack(Abc_ObjLevel(pObj), Abc_ObjToLit(pObj)) );
    while ( Vec_WrdSize(vSuper) > 0 )
    {
        // make sure there are no duplicates
        Num = Vec_WrdEntry( vSuper, 0 );
        Vec_WrdForEachEntryStart( vSuper, NumNext, i, 1 )
        {
            assert( Num < NumNext );
            Num = NumNext;
        }
        // extract AND gate decomposable on the topmost level
        Vec_WrdForEachEntryReverse( vSuper, Num, i )
        {
            pRoot = Abc_ObjFromLit( pNtk, Abc_NtkShareUnpackId(Num) );
            if ( !Abc_ObjIsComplement(pRoot) && Abc_ObjIsNode(pRoot) )
            {
                Vec_WrdRemove( vSuper, Num );
                break;
            }
        }
        if ( i == -1 )
            break;
191
        assert( Abc_ObjIsNode(pRoot) );
192 193 194
        // extract
        pObj0 = Abc_ObjChild0(pRoot);
        pObj1 = Abc_ObjChild1(pRoot);
195 196
        assert( Abc_ObjIsNode(Abc_ObjRegular(pObj0)) || Abc_ObjIsCi(Abc_ObjRegular(pObj0)) );
        assert( Abc_ObjIsNode(Abc_ObjRegular(pObj1)) || Abc_ObjIsCi(Abc_ObjRegular(pObj1)) );
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
        Vec_WrdPushOrder( vSuper, Abc_NtkSharePack(Abc_ObjLevel(Abc_ObjRegular(pObj0)), Abc_ObjToLit(pObj0)) );
        Vec_WrdPushOrder( vSuper, Abc_NtkSharePack(Abc_ObjLevel(Abc_ObjRegular(pObj1)), Abc_ObjToLit(pObj1)) );
        (*pCounter)++;
        // remove duplicates
        k = 0;
        Vec_WrdForEachEntry( vSuper, Num, i )
        {
            if ( i + 1 == Vec_WrdSize(vSuper) )
            {
                Vec_WrdWriteEntry( vSuper, k++, Num );
                break;
            }
            NumNext = Vec_WrdEntry( vSuper, i+1 );
            assert( Num <= NumNext );
            if ( Num + 1 == NumNext && (NumNext & 1) ) // pos_lit & neg_lit = 0
            {
                Vec_WrdClear( vSuper );
                return vSuper;
            }
            if ( Num < NumNext )
                Vec_WrdWriteEntry( vSuper, k++, Num );
        }
        Vec_WrdShrink( vSuper, k );
    }
    Vec_WrdForEachEntry( vSuper, Num, i )
        Vec_WrdWriteEntry( vSuper, i, Abc_NtkShareUnpackId(Num) );
    return vSuper;
}

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

  Synopsis    [Creates multi-input XOR representation for the nodes.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Abc_NtkTraverseSupersXor_rec( Abc_ShaMan_t * p, Abc_Obj_t * pObj, Vec_Ptr_t * vInputs )
{
    if ( Abc_NodeIsTravIdCurrent( pObj ) )
        return;
    Abc_NodeSetTravIdCurrent( pObj );
    if ( Abc_ObjIsCi(pObj) )
        return;
    assert( Abc_ObjIsNode(pObj) );
    if ( Abc_NodeIsExorType(pObj) )
    {
        Vec_Wrd_t * vSuper;
        int k, fCompl;
        word Num;
        vSuper = Abc_NtkShareSuperXor( pObj, &fCompl, &p->nFoundGates );
251
        if ( Vec_WrdSize(vSuper) <= 1 || Vec_WrdSize(vSuper) >= p->nMultiSize )
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
        {
            Vec_WrdForEachEntry( vSuper, Num, k )
            {
                Vec_Int_t * vInput = (Vec_Int_t *)Vec_PtrEntry( vInputs, (int)Num );
                if ( vInput == NULL )
                {
                    vInput = Vec_IntAlloc( 10 );
                    Vec_IntPush( vInput, Abc_Var2Lit((int)Num, 0) );
                    Vec_IntPush( vInput, Abc_ObjLevel(Abc_NtkObj(p->pNtk, (int)Num)) );
                    assert( SHARE_NUM == Vec_IntSize(vInput) );
                    Vec_PtrWriteEntry( vInputs, (int)Num, vInput );
                }
                Vec_IntPush( vInput, Vec_IntSize(p->vObj2Lit) );
            }
            Vec_IntPush( p->vObj2Lit, Abc_Var2Lit(Abc_ObjId(pObj), fCompl) );
        }
        // call recursively
        Vec_WrdForEachEntry( vSuper, Num, k )
            Abc_NtkTraverseSupersXor_rec( p, Abc_NtkObj(p->pNtk, (int)Num), vInputs );
        Vec_WrdFree( vSuper );
    }
    else
    {
        Abc_NtkTraverseSupersXor_rec( p, Abc_ObjFanin0(pObj), vInputs );
        Abc_NtkTraverseSupersXor_rec( p, Abc_ObjFanin1(pObj), vInputs );
    }
}
void Abc_NtkTraverseSupersAnd_rec( Abc_ShaMan_t * p, Abc_Obj_t * pObj, Vec_Ptr_t * vInputs )
{
    Vec_Wrd_t * vSuper;
    word Num;
    int k;
    if ( Abc_NodeIsTravIdCurrent( pObj ) )
        return;
    Abc_NodeSetTravIdCurrent( pObj );
    if ( Abc_ObjIsCi(pObj) )
        return;
    assert( Abc_ObjIsNode(pObj) );
    vSuper = Abc_NtkShareSuperAnd( pObj, &p->nFoundGates );
291
    if ( Vec_WrdSize(vSuper) <= 1 || Vec_WrdSize(vSuper) >= p->nMultiSize )
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
    {
        Vec_WrdForEachEntry( vSuper, Num, k )
        {
            Vec_Int_t * vInput = (Vec_Int_t *)Vec_PtrEntry( vInputs, (int)Num );
            if ( vInput == NULL )
            {
                vInput = Vec_IntAlloc( 10 );
                Vec_IntPush( vInput, (int)Num );
                Vec_IntPush( vInput, Abc_ObjLevel(Abc_NtkObj(p->pNtk, Abc_Lit2Var((int)Num))) );
                assert( SHARE_NUM == Vec_IntSize(vInput) );
                Vec_PtrWriteEntry( vInputs, (int)Num, vInput );
            }
            Vec_IntPush( vInput, Vec_IntSize(p->vObj2Lit) );
        }
        Vec_IntPush( p->vObj2Lit, Abc_ObjToLit(pObj) );
    }
    // call recursively
    Vec_WrdForEachEntry( vSuper, Num, k )
        Abc_NtkTraverseSupersAnd_rec( p, Abc_NtkObj(p->pNtk, Abc_Lit2Var((int)Num)), vInputs );
    Vec_WrdFree( vSuper );
}
void Abc_NtkTraverseSupers( Abc_ShaMan_t * p, int fAnd )
{
    Vec_Ptr_t * vInputs;
    Vec_Int_t * vInput;
    Abc_Obj_t * pObj;
    int i, nOnesMax;

    // create mapping of nodes into their column vectors
321
    vInputs = Vec_PtrStart( Abc_NtkObjNumMax(p->pNtk) * (1 + fAnd) );
322 323 324 325
    Abc_NtkIncrementTravId( p->pNtk );
    if ( fAnd )
    {
        Abc_NtkForEachCo( p->pNtk, pObj, i )
326 327
            if ( Abc_ObjIsNode(Abc_ObjFanin0(pObj)) )
                Abc_NtkTraverseSupersAnd_rec( p, Abc_ObjFanin0(pObj), vInputs );
328 329 330 331
    }
    else
    {
        Abc_NtkForEachCo( p->pNtk, pObj, i )
332 333
            if ( Abc_ObjIsNode(Abc_ObjFanin0(pObj)) )
                Abc_NtkTraverseSupersXor_rec( p, Abc_ObjFanin0(pObj), vInputs );
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
    }
    p->nStartCols = Vec_IntSize(p->vObj2Lit);

    // find the largest number of 1s
    nOnesMax = 0;
    Vec_PtrForEachEntry( Vec_Int_t *, vInputs, vInput, i )
        if ( vInput )
            nOnesMax = Abc_MaxInt( nOnesMax, Vec_IntSize(vInput)-SHARE_NUM );

    // create buckets
    assert( p->vBuckets == NULL );
    p->vBuckets = Vec_PtrAlloc( nOnesMax + 1 );
    for ( i = 0; i <= nOnesMax; i++ )
        Vec_PtrPush( p->vBuckets, Vec_PtrAlloc(10) );

    // load vectors into buckets
    Vec_PtrForEachEntry( Vec_Int_t *, vInputs, vInput, i )
        if ( vInput )
            Vec_PtrPush( (Vec_Ptr_t *)Vec_PtrEntry(p->vBuckets, Vec_IntSize(vInput)-SHARE_NUM), vInput );
    Vec_PtrFree( vInputs );
}
355 356 357

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

Alan Mishchenko committed
358 359 360 361 362 363 364 365 366
  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
367
void Abc_NtkSharePrint( Abc_ShaMan_t * p )
Alan Mishchenko committed
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
    Vec_Ptr_t * vBucket;
    Vec_Int_t * vInput;
    int i, k, j, ObjId;
    char * pBuffer = ABC_ALLOC( char, Vec_IntSize(p->vObj2Lit) + 1 );
    int * pCounters = ABC_CALLOC( int, Vec_IntSize(p->vObj2Lit) + 1 );
    int nTotal = 0;
    Vec_PtrForEachEntry( Vec_Ptr_t *, p->vBuckets, vBucket, i )
    Vec_PtrForEachEntry( Vec_Int_t *, vBucket, vInput, j )
    {
        for ( k = 0; k < Vec_IntSize(p->vObj2Lit); k++ )
            pBuffer[k] = '0';
        pBuffer[k] = 0;

        Vec_IntForEachEntryStart( vInput, ObjId, k, SHARE_NUM )
        {
            assert( ObjId < Vec_IntSize(p->vObj2Lit) );
            pBuffer[ObjId] = '1';
            pCounters[ObjId]++;
        }
        printf( "%4d%3d: %s\n", Vec_IntEntry(vInput, 0), Vec_IntEntry(vInput, 1), pBuffer );
    }

    for ( i = 0; i < Vec_IntSize(p->vObj2Lit); i++ )
        if ( pCounters[i] > 0 )
            printf( "%d=%d ", i, pCounters[i] ); 
    printf( "\n" );

    nTotal = 0;
    for ( i = 0; i < p->nStartCols; i++ )
        nTotal += pCounters[i] - 1;
    printf( "Total = %d.  ", nTotal );
400
    printf( "Gates = %d.\n", Vec_IntSize(p->vObj2Lit) - p->nStartCols + nTotal );
401 402 403

    ABC_FREE( pCounters );
    ABC_FREE( pBuffer );
Alan Mishchenko committed
404

405 406 407 408
    printf( "Bucket contents: " );
    Vec_PtrForEachEntry( Vec_Ptr_t *, p->vBuckets, vBucket, i )
        printf( "%d ", Vec_PtrSize(vBucket) );
    printf( "\n" );
Alan Mishchenko committed
409
}
Alan Mishchenko committed
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
/**Function*************************************************************

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Abc_NtkDumpBlif( Abc_Ntk_t * p )
{
    FILE * pFile;
    Vec_Ptr_t * vSupp;
    Abc_Obj_t * pObj;
    int i, k;
    pFile = fopen( "multi_and.blif", "wb" );
    if ( pFile == NULL )
    {
        printf( "Cannot open output file.\n" );
        return;
    }
    fprintf( pFile, ".model %s\n", "multi_and" );
    fprintf( pFile, ".inputs" );
    for ( i = 0; i < Abc_NtkCiNum(p); i++ )
        fprintf( pFile, " i%d", i );
    fprintf( pFile, "\n" );
    fprintf( pFile, ".outputs" );
    for ( i = 0; i < Abc_NtkCoNum(p); i++ )
        fprintf( pFile, " o%d", i );
    fprintf( pFile, "\n" );
    Abc_NtkForEachCi( p, pObj, i )
        pObj->iTemp = i;
    for ( i = 0; i < Abc_NtkCoNum(p); i++ )
    {
        pObj = Abc_NtkCo( p, i );
        vSupp = Abc_NtkNodeSupport( p, &pObj, 1 );
        fprintf( pFile, ".names" );
        Vec_PtrForEachEntry( Abc_Obj_t *, vSupp, pObj, k )
            fprintf( pFile, " i%d", pObj->iTemp );
        fprintf( pFile, " o%d\n", i );
        Vec_PtrForEachEntry( Abc_Obj_t *, vSupp, pObj, k )
            fprintf( pFile, "1" );
        fprintf( pFile, " 1\n" );
        Vec_PtrFree( vSupp );
    }
    fprintf( pFile, ".end\n\n" );
    fclose( pFile );
}

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

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Abc_NtkShareFindBestMatch( Vec_Ptr_t * vBuckets, Vec_Int_t ** pvInput, Vec_Int_t ** pvInput2 )
{
    int nPoolSize = 40;
    Vec_Ptr_t * vPool = Vec_PtrAlloc( nPoolSize );
    Vec_Ptr_t * vBucket;
    Vec_Int_t * vInput, * vInput2, * vInputBest = NULL, * vInputBest2 = NULL;
    int i, k, Cost, CostBest = 0, Delay, DelayBest = 0;

    Vec_PtrForEachEntryReverse( Vec_Ptr_t *, vBuckets, vBucket, i )
        Vec_PtrForEachEntry( Vec_Int_t *, vBucket, vInput, k )
        {
            Vec_PtrPush( vPool, vInput );
            if ( Vec_PtrSize(vPool) == nPoolSize )
                goto outside;
        }
outside:

    Vec_PtrForEachEntryReverse( Vec_Int_t *, vPool, vInput, i )
    Vec_PtrForEachEntryReverse( Vec_Int_t *, vPool, vInput2, k )
    {
        if ( i == k )
            continue;

        vInput->pArray += SHARE_NUM;
        vInput2->pArray += SHARE_NUM;
        vInput->nSize -= SHARE_NUM;
        vInput2->nSize -= SHARE_NUM;

        Cost = Vec_IntTwoCountCommon(vInput, vInput2);

        vInput->pArray -= SHARE_NUM;
        vInput2->pArray -= SHARE_NUM;
        vInput->nSize += SHARE_NUM;
        vInput2->nSize += SHARE_NUM;

        if ( Cost < 2 )
            continue;

        Delay = Abc_MaxInt( Vec_IntEntry(vInput, 1), Vec_IntEntry(vInput2, 1) );

        if ( CostBest < Cost || (CostBest == Cost && (DelayBest > Delay)) )
        {
            CostBest = Cost;
            DelayBest = Delay;
            vInputBest = vInput;
            vInputBest2 = vInput2;
        }
    }
    Vec_PtrFree( vPool );

    *pvInput  = vInputBest;
    *pvInput2 = vInputBest2;

    if ( vInputBest == NULL )
        return;

    Vec_PtrRemove( (Vec_Ptr_t *)Vec_PtrEntry(vBuckets, Vec_IntSize(vInputBest)-SHARE_NUM),  (Vec_Int_t *)vInputBest ); 
    Vec_PtrRemove( (Vec_Ptr_t *)Vec_PtrEntry(vBuckets, Vec_IntSize(vInputBest2)-SHARE_NUM), (Vec_Int_t *)vInputBest2 ); 
}
533
void Abc_NtkShareOptimize( Abc_ShaMan_t * p, int fAnd )
534 535 536 537 538 539 540 541 542 543 544 545 546 547
{
    Abc_Obj_t * pObj, * pObj0, * pObj1;
    Vec_Int_t * vInput, * vInput2;
    Vec_Int_t * vNew, * vOld1, * vOld2;
    int i;
    for ( i = 0; ; i++ )
    {
        Abc_NtkShareFindBestMatch( p->vBuckets, &vInput, &vInput2 );
        if ( vInput == NULL )
            break;

        // create new node
        pObj0 = Abc_ObjFromLit( p->pNtk, Vec_IntEntry(vInput,  0) );
        pObj1 = Abc_ObjFromLit( p->pNtk, Vec_IntEntry(vInput2, 0) );
548 549 550 551 552
        if ( fAnd )
            pObj  = Abc_AigAnd( (Abc_Aig_t *)p->pNtk->pManFunc, pObj0, pObj1 );
        else
            pObj  = Abc_AigXor( (Abc_Aig_t *)p->pNtk->pManFunc, pObj0, pObj1 );
        p->nCountGates++;
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

        // save new node
        vOld1 = Vec_IntAlloc( 16 );  Vec_IntPush( vOld1, Vec_IntEntry(vInput,  0) );  Vec_IntPush( vOld1, Vec_IntEntry(vInput,  1) );
        vOld2 = Vec_IntAlloc( 16 );  Vec_IntPush( vOld2, Vec_IntEntry(vInput2, 0) );  Vec_IntPush( vOld2, Vec_IntEntry(vInput2, 1) );
        vNew  = Vec_IntAlloc( 16 );  Vec_IntPush( vNew,  Abc_ObjToLit(pObj) );        Vec_IntPush( vNew, Abc_ObjLevel(Abc_ObjRegular(pObj)) );

        // compute new arrays
        vInput->pArray += SHARE_NUM;
        vInput2->pArray += SHARE_NUM;
        vInput->nSize -= SHARE_NUM;
        vInput2->nSize -= SHARE_NUM;

        Vec_IntTwoSplit( vInput, vInput2, vNew, vOld1, vOld2 );

        vInput->pArray -= SHARE_NUM;
        vInput2->pArray -= SHARE_NUM;
        vInput->nSize += SHARE_NUM;
        vInput2->nSize += SHARE_NUM;

        // add to the old ones
        Vec_IntPush( vOld1, Vec_IntSize(p->vObj2Lit) );
        Vec_IntPush( vOld2, Vec_IntSize(p->vObj2Lit) );
        Vec_IntPush( p->vObj2Lit, Abc_ObjToLit(pObj) );

        Vec_PtrPush( (Vec_Ptr_t *)Vec_PtrEntry(p->vBuckets, Vec_IntSize(vOld1)-SHARE_NUM), vOld1 );
        Vec_PtrPush( (Vec_Ptr_t *)Vec_PtrEntry(p->vBuckets, Vec_IntSize(vOld2)-SHARE_NUM), vOld2 );
        Vec_PtrPush( (Vec_Ptr_t *)Vec_PtrEntry(p->vBuckets, Vec_IntSize(vNew)-SHARE_NUM), vNew );

        Vec_IntFree( vInput );
        Vec_IntFree( vInput2 );
    }
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
597
Abc_Ntk_t * Abc_NtkUpdateNetwork( Abc_ShaMan_t * p, int fAnd )
598 599 600 601 602 603
{
    Abc_Ntk_t * pNtk;
    Vec_Int_t * vInput, * vMap2Repl;
    Vec_Ptr_t * vOrig, * vRepl, * vBucket;
    Abc_Obj_t * pObj, * pNew;
    int i, j, k, ObjId, iLit;
604
    int iLitConst1 = Abc_ObjToLit( Abc_AigConst1(p->pNtk) );
605 606 607 608 609 610

    vOrig = Vec_PtrAlloc( p->nStartCols );
    vRepl = Vec_PtrAlloc( p->nStartCols );
    for ( i = 0; i < p->nStartCols; i++ )
    {
        iLit = Vec_IntEntry( p->vObj2Lit, i );
611
        assert( !fAnd || !Abc_LitIsCompl(iLit) );
612 613

        pObj = Abc_NtkObj( p->pNtk, Abc_Lit2Var(iLit) );
614 615 616 617 618

        if ( fAnd )
            pNew = Abc_AigConst1(p->pNtk);
        else
            pNew = Abc_ObjNotCond( Abc_AigConst1(p->pNtk), !Abc_LitIsCompl(iLit) );
619 620 621 622

        Vec_PtrPush( vOrig, pObj );
        Vec_PtrPush( vRepl, pNew );

623
        p->nCountGates--;
624 625 626 627 628 629 630 631 632 633 634 635 636 637 638
    }

    // go through the columns
    Vec_PtrForEachEntry( Vec_Ptr_t *, p->vBuckets, vBucket, i )
    Vec_PtrForEachEntry( Vec_Int_t *, vBucket, vInput, j )
    {
        Vec_IntForEachEntryStart( vInput, ObjId, k, SHARE_NUM )
        {
            assert( ObjId < Vec_IntSize(p->vObj2Lit) );
            if ( ObjId >= p->nStartCols )
                break;
            assert( ObjId < p->nStartCols );
            iLit = Vec_IntEntry( vInput, 0 );

            pNew = (Abc_Obj_t *)Vec_PtrEntry( vRepl, ObjId );
639 640 641 642
            if ( fAnd )
                pNew = Abc_AigAnd( (Abc_Aig_t *)p->pNtk->pManFunc, pNew, Abc_ObjFromLit(p->pNtk, iLit) );
            else
                pNew = Abc_AigXor( (Abc_Aig_t *)p->pNtk->pManFunc, pNew, Abc_ObjFromLit(p->pNtk, iLit) );
643
            Vec_PtrWriteEntry( vRepl, ObjId, pNew );
644
            p->nCountGates++;
645 646 647 648
        }
    }

    if ( p->fVerbose )
649
        printf( "Total gates collected = %d.  Total gates constructed = %d.\n", p->nFoundGates, p->nCountGates );
650 651 652 653

    // create map of originals
    vMap2Repl = Vec_IntStartFull( Abc_NtkObjNumMax(p->pNtk) );
    Vec_PtrForEachEntry( Abc_Obj_t *, vOrig, pObj, i )
654 655
    {
//        printf( "Replacing %d by %d.\n", Abc_ObjId(pObj), Abc_ObjToLit((Abc_Obj_t *)Vec_PtrEntry(vRepl, i)) );
656
        Vec_IntWriteEntry( vMap2Repl, Abc_ObjId(pObj), Abc_ObjToLit((Abc_Obj_t *)Vec_PtrEntry(vRepl, i)) );
657
    }
658 659 660 661 662 663 664 665 666 667 668
    Vec_PtrFree( vOrig );
    Vec_PtrFree( vRepl );

    // update fanin pointers
    Abc_NtkForEachObj( p->pNtk, pObj, i )
    {
        if ( Abc_ObjIsCo(pObj) || Abc_ObjIsNode(pObj) )
        {
            iLit = Vec_IntEntry( vMap2Repl, Abc_ObjFaninId0(pObj) );
            if ( iLit >= 0 )
            {
669 670
                if ( iLit == iLitConst1 && fAnd )
                {
671
                    pObj->fCompl0 ^= 1;
672 673 674 675 676 677 678
                    Vec_IntWriteEntry( &pObj->vFanins, 0, Abc_Lit2Var(iLitConst1) );
                }
                else
                {
                    pObj->fCompl0 ^= Abc_LitIsCompl(iLit);
                    Vec_IntWriteEntry( &pObj->vFanins, 0, Abc_Lit2Var(iLit) );
                }
679 680 681 682 683 684 685
            }
        }
        if ( Abc_ObjIsNode(pObj) )
        {
            iLit = Vec_IntEntry( vMap2Repl, Abc_ObjFaninId1(pObj) );
            if ( iLit >= 0 )
            {
686 687
                if ( iLit == iLitConst1 && fAnd )
                {
688
                    pObj->fCompl1 ^= 1;
689 690 691 692 693 694 695
                    Vec_IntWriteEntry( &pObj->vFanins, 1, Abc_Lit2Var(iLitConst1) );
                }
                else
                {
                    pObj->fCompl1 ^= Abc_LitIsCompl(iLit);
                    Vec_IntWriteEntry( &pObj->vFanins, 1, Abc_Lit2Var(iLit) );
                }
696 697 698 699 700 701
            }
        }
    }
    Vec_IntFree( vMap2Repl );

//    pNtk = Abc_NtkRestrash( p->pNtk, 1 );
702 703
    if ( fAnd )
        pNtk = Abc_NtkBalance( p->pNtk, 0, 0, 1 );
704
    else
705 706
        pNtk = Abc_NtkBalanceExor( p->pNtk, 1, 0 );
    return pNtk;
707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724
}

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

  Synopsis    [Extracts one multi-output XOR.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Abc_Ntk_t * Abc_NtkShareXor( Abc_Ntk_t * pNtk, int nMultiSize, int fAnd, int fVerbose )
{
    Abc_Ntk_t * pNtkNew;
    Abc_ShaMan_t * p;
    assert( Abc_NtkIsStrash(pNtk) );
725
//    Abc_NtkDumpBlif( pNtk );
726 727 728
    p = Abc_ShaManStart( pNtk );
    p->nMultiSize = nMultiSize;
    p->fVerbose   = fVerbose;
729
    Abc_NtkTraverseSupers( p, fAnd );
730 731 732 733 734 735 736
    if ( p->nStartCols < 2 )
    {
        Abc_ShaManStop( p );
        return Abc_NtkDup( pNtk );
    }
    if ( fVerbose )
        Abc_NtkSharePrint( p );
737
    Abc_NtkShareOptimize( p, fAnd );
738 739
    if ( fVerbose )
        Abc_NtkSharePrint( p );
740
    pNtkNew = Abc_NtkUpdateNetwork( p, fAnd );
741 742 743 744 745
    Abc_ShaManStop( p );
    return pNtkNew;
}


Alan Mishchenko committed
746 747 748 749 750
////////////////////////////////////////////////////////////////////////
///                       END OF FILE                                ///
////////////////////////////////////////////////////////////////////////


751 752
ABC_NAMESPACE_IMPL_END