giaResub2.c 55.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/**CFile****************************************************************

  FileName    [giaResub2.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [Scalable AIG package.]

  Synopsis    []

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

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

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

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

#include "gia.h"
#include "misc/util/utilTruth.h"
23 24
#include "misc/vec/vecHsh.h"
#include "opt/dau/dau.h"
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

ABC_NAMESPACE_IMPL_START


////////////////////////////////////////////////////////////////////////
///                        DECLARATIONS                              ///
////////////////////////////////////////////////////////////////////////
 
typedef struct Gia_Rsb2Man_t_ Gia_Rsb2Man_t;
struct Gia_Rsb2Man_t_
{
    // hyper-parameters
    int            nDivsMax;
    int            nLevelIncrease;
    int            fUseXor;
    int            fUseZeroCost;
    int            fDebug;
    int            fVerbose;
    // input AIG
    int            nObjs;
    int            nPis;
    int            nNodes;
    int            nPos;
    int            iFirstPo;
    int            Level;
    int            nMffc;
    // intermediate data
    Vec_Int_t      vObjs;
    Vec_Wrd_t      vSims;
    Vec_Ptr_t      vpDivs;
    Vec_Int_t      vDivs;
    Vec_Int_t      vLevels;
    Vec_Int_t      vRefs;
    Vec_Int_t      vCopies;
59
    Vec_Int_t      vTried;
60 61 62 63 64
    word           Truth0;
    word           Truth1;
    word           CareSet;
};

65
extern void Abc_ResubPrepareManager( int nWords );
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
extern int Abc_ResubComputeFunction( void ** ppDivs, int nDivs, int nWords, int nLimit, int nDivsMax, int iChoice, int fUseXor, int fDebug, int fVerbose, int ** ppArray );

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

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Gia_Rsb2Man_t * Gia_Rsb2ManAlloc()
{
    Gia_Rsb2Man_t * p = ABC_CALLOC( Gia_Rsb2Man_t, 1 );
    return p;
}
void Gia_Rsb2ManFree( Gia_Rsb2Man_t * p )
{
    Vec_IntErase( &p->vObjs   );
    Vec_WrdErase( &p->vSims   );
    Vec_PtrErase( &p->vpDivs  );
    Vec_IntErase( &p->vDivs   );
    Vec_IntErase( &p->vLevels );
    Vec_IntErase( &p->vRefs   );
    Vec_IntErase( &p->vCopies );
97
    Vec_IntErase( &p->vTried );
98 99 100 101 102 103 104 105 106 107 108 109 110 111
    ABC_FREE( p );
}
void Gia_Rsb2ManStart( Gia_Rsb2Man_t * p, int * pObjs, int nObjs, int nDivsMax, int nLevelIncrease, int fUseXor, int fUseZeroCost, int fDebug, int fVerbose )
{
    int i;
    // hyper-parameters
    p->nDivsMax       = nDivsMax; 
    p->nLevelIncrease = nLevelIncrease; 
    p->fUseXor        = fUseXor; 
    p->fUseZeroCost   = fUseZeroCost; 
    p->fDebug         = fDebug; 
    p->fVerbose       = fVerbose; 
    // user data
    Vec_IntClear( &p->vObjs );
112
    Vec_IntPushArray( &p->vObjs, pObjs, 2*nObjs );
113 114 115 116 117 118 119 120 121
    assert( pObjs[0] == 0 );
    assert( pObjs[1] == 0 );
    p->nObjs    = nObjs;
    p->nPis     = 0;
    p->nNodes   = 0;
    p->nPos     = 0;
    p->iFirstPo = 0;
    for ( i = 1; i < nObjs; i++ )
    {
122
        if ( pObjs[2*i+0] == 0 && pObjs[2*i+1] == 0 )
123
            p->nPis++;
124
        else if ( pObjs[2*i+0] == pObjs[2*i+1] )
125 126 127 128 129 130 131 132 133 134 135 136
            p->nPos++;
        else
            p->nNodes++;
    }
    assert( nObjs == 1 + p->nPis + p->nNodes + p->nPos );
    p->iFirstPo = nObjs - p->nPos;
    Vec_WrdClear( &p->vSims );
    Vec_WrdGrow( &p->vSims, 2*nObjs );
    Vec_WrdPush( &p->vSims, 0 );
    Vec_WrdPush( &p->vSims, 0 );
    for ( i = 0; i < p->nPis; i++ )
    {
137
        Vec_WrdPush( &p->vSims,  s_Truths6[i] );
138 139
        Vec_WrdPush( &p->vSims, ~s_Truths6[i] );
    }
140
    p->vSims.nSize = 2*p->nObjs;
141 142 143 144
    Vec_IntClear( &p->vDivs   );
    Vec_IntClear( &p->vLevels );
    Vec_IntClear( &p->vRefs   );
    Vec_IntClear( &p->vCopies );
145
    Vec_IntClear( &p->vTried  );
146 147 148 149 150
    Vec_PtrClear( &p->vpDivs  );
    Vec_IntGrow( &p->vDivs,   nObjs );
    Vec_IntGrow( &p->vLevels, nObjs );
    Vec_IntGrow( &p->vRefs,   nObjs );
    Vec_IntGrow( &p->vCopies, nObjs );
151
    Vec_IntGrow( &p->vTried,  nObjs );
152 153 154 155 156 157 158 159 160 161 162 163 164 165
    Vec_PtrGrow( &p->vpDivs,  nObjs );
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
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
void Gia_Rsb2ManPrint( Gia_Rsb2Man_t * p )
{
    int i, * pObjs = Vec_IntArray( &p->vObjs );
    printf( "PI = %d.  PO = %d.  Obj = %d.\n", p->nPis, p->nPos, p->nObjs );
    for ( i = p->nPis + 1; i < p->iFirstPo; i++ )
        printf( "%2d = %c%2d & %c%2d;\n", i, 
            Abc_LitIsCompl(pObjs[2*i+0]) ? '!' : ' ', Abc_Lit2Var(pObjs[2*i+0]),
            Abc_LitIsCompl(pObjs[2*i+1]) ? '!' : ' ', Abc_Lit2Var(pObjs[2*i+1]) );
    for ( i = p->iFirstPo; i < p->nObjs; i++ )
        printf( "%2d = %c%2d;\n", i, 
            Abc_LitIsCompl(pObjs[2*i+0]) ? '!' : ' ', Abc_Lit2Var(pObjs[2*i+0]) );
}

int Gia_Rsb2ManLevel( Gia_Rsb2Man_t * p )
{
    int i, * pLevs, Level = 0;
    Vec_IntClear( &p->vLevels );
    Vec_IntGrow( &p->vLevels, p->nObjs );
    pLevs = Vec_IntArray( &p->vLevels );
    for ( i = p->nPis + 1; i < p->iFirstPo; i++ )
        pLevs[i] = 1 + Abc_MaxInt( pLevs[2*i+0]/2, pLevs[2*i+1]/2 );
    for ( i = p->iFirstPo; i < p->nObjs; i++ )
        Level = Abc_MaxInt( Level, pLevs[i] = pLevs[2*i+0]/2 );
    return Level;
}
191 192 193 194 195 196 197 198 199
word Gia_Rsb2ManOdcs( Gia_Rsb2Man_t * p, int iNode )
{
    int i; word Res = 0;
    int  * pObjs = Vec_IntArray( &p->vObjs );
    word * pSims = Vec_WrdArray( &p->vSims );
    for ( i = p->nPis + 1; i < p->iFirstPo; i++ )
    {
        if ( pObjs[2*i+0] < pObjs[2*i+1] )
            pSims[2*i+0] = pSims[pObjs[2*i+0]] & pSims[pObjs[2*i+1]];
200
        else if ( pObjs[2*i+0] > pObjs[2*i+1] )
201
            pSims[2*i+0] = pSims[pObjs[2*i+0]] ^ pSims[pObjs[2*i+1]];
202
        else assert( 0 );
203 204 205 206 207 208 209 210 211
        pSims[2*i+1] = ~pSims[2*i+0];        
    }
    for ( i = p->iFirstPo; i < p->nObjs; i++ )
        pSims[2*i+0] = pSims[pObjs[2*i+0]];
    ABC_SWAP( word, pSims[2*iNode+0], pSims[2*iNode+1] );
    for ( i = iNode + 1; i < p->iFirstPo; i++ )
    {
        if ( pObjs[2*i+0] < pObjs[2*i+1] )
            pSims[2*i+0] = pSims[pObjs[2*i+0]] & pSims[pObjs[2*i+1]];
212
        else if ( pObjs[2*i+0] < pObjs[2*i+1] )
213
            pSims[2*i+0] = pSims[pObjs[2*i+0]] ^ pSims[pObjs[2*i+1]];
214
        else assert( 0 );
215 216 217 218
        pSims[2*i+1] = ~pSims[2*i+0];        
    }
    for ( i = p->iFirstPo; i < p->nObjs; i++ )
        Res |= pSims[2*i+0] ^ pSims[pObjs[2*i+0]];
219
    ABC_SWAP( word, pSims[2*iNode+0], pSims[2*iNode+1] );
220 221 222
    return Res;
}
// marks MFFC and returns its size
223 224 225 226 227 228 229 230 231 232 233
int Gia_Rsb2ManDeref_rec( Gia_Rsb2Man_t * p, int * pObjs, int * pRefs, int iNode )
{
    int Counter = 1;
    if ( iNode <= p->nPis )
        return 0;
    if ( --pRefs[Abc_Lit2Var(pObjs[2*iNode+0])] == 0 )
        Counter += Gia_Rsb2ManDeref_rec( p, pObjs, pRefs, Abc_Lit2Var(pObjs[2*iNode+0]) );
    if ( --pRefs[Abc_Lit2Var(pObjs[2*iNode+1])] == 0 )
        Counter += Gia_Rsb2ManDeref_rec( p, pObjs, pRefs, Abc_Lit2Var(pObjs[2*iNode+1]) );
    return Counter;
}
234 235
int Gia_Rsb2ManMffc( Gia_Rsb2Man_t * p, int iNode )
{
236
    int i, * pRefs, * pObjs;
237 238 239
    Vec_IntFill( &p->vRefs, p->nObjs, 0 );
    pRefs = Vec_IntArray( &p->vRefs );
    pObjs = Vec_IntArray( &p->vObjs );
240 241 242 243 244 245
    assert( pObjs[2*iNode+0] != pObjs[2*iNode+1] );
    for ( i = p->nPis + 1; i < p->iFirstPo; i++ )
        pRefs[Abc_Lit2Var(pObjs[2*i+0])]++, 
        pRefs[Abc_Lit2Var(pObjs[2*i+1])]++;
    for ( i = p->iFirstPo; i < p->nObjs; i++ )
        pRefs[Abc_Lit2Var(pObjs[2*i+0])]++;
246
    for ( i = p->nPis + 1; i < p->iFirstPo; i++ )
247 248 249 250 251 252
        assert( pRefs[i] );
    pRefs[iNode] = 0;
    for ( i = iNode + 1; i < p->iFirstPo; i++ )
        if ( !pRefs[Abc_Lit2Var(pObjs[2*i+0])] || !pRefs[Abc_Lit2Var(pObjs[2*i+1])] )
            pRefs[i] = 0;
    return Gia_Rsb2ManDeref_rec( p, pObjs, pRefs, iNode );
253 254 255 256 257
}
// collects divisors and maps them into nodes
// assumes MFFC is already marked
int Gia_Rsb2ManDivs( Gia_Rsb2Man_t * p, int iNode )
{
258
    int i, iNodeLevel = 0;
259 260
    int * pRefs = Vec_IntArray( &p->vRefs );
    p->CareSet = Gia_Rsb2ManOdcs( p, iNode );
261
    p->Truth1 = p->CareSet & Vec_WrdEntry(&p->vSims, 2*iNode);
262 263 264 265 266 267 268 269
    p->Truth0 = p->CareSet & ~p->Truth1;
    Vec_PtrClear( &p->vpDivs );
    Vec_PtrPush( &p->vpDivs, &p->Truth0 );
    Vec_PtrPush( &p->vpDivs, &p->Truth1 );
    Vec_IntClear( &p->vDivs );
    Vec_IntPushTwo( &p->vDivs, -1, -1 );
    for ( i = 1; i <= p->nPis; i++ )
    {
270
        Vec_PtrPush( &p->vpDivs, Vec_WrdEntryP(&p->vSims, 2*i) );
271 272 273
        Vec_IntPush( &p->vDivs, i );
    }
    p->nMffc = Gia_Rsb2ManMffc( p, iNode );
274 275 276 277 278
    if ( p->nLevelIncrease >= 0 )
    {
        p->Level = Gia_Rsb2ManLevel(p);
        iNodeLevel = Vec_IntEntry(&p->vLevels, iNode);
    }
279 280
    for ( i = p->nPis + 1; i < p->iFirstPo; i++ )
    {
281
        if ( !pRefs[i] || (p->nLevelIncrease >= 0 && Vec_IntEntry(&p->vLevels, i) > iNodeLevel + p->nLevelIncrease) )
282
            continue;
283
        Vec_PtrPush( &p->vpDivs, Vec_WrdEntryP(&p->vSims, 2*i) );
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
        Vec_IntPush( &p->vDivs, i );
    }
    assert( Vec_IntSize(&p->vDivs) == Vec_PtrSize(&p->vpDivs) );
    return Vec_IntSize(&p->vDivs);
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Gia_Rsb2AddNode( Vec_Int_t * vRes, int iLit0, int iLit1, int iRes0, int iRes1 )
{
    int iLitMin = iRes0 < iRes1 ? Abc_LitNotCond(iRes0, Abc_LitIsCompl(iLit0)) : Abc_LitNotCond(iRes1, Abc_LitIsCompl(iLit1));
    int iLitMax = iRes0 < iRes1 ? Abc_LitNotCond(iRes1, Abc_LitIsCompl(iLit1)) : Abc_LitNotCond(iRes0, Abc_LitIsCompl(iLit0));
    int iLitRes = Vec_IntSize(vRes);
    if ( iLit0 < iLit1 ) // and
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
    {
        if ( iLitMin == 0 )
            return 0;
        if ( iLitMin == 1 )
            return iLitMax;
        if ( iLitMin == Abc_LitNot(iLitMax) )
            return 0;
    }
    else if ( iLit0 > iLit1 ) // xor
    {
        if ( iLitMin == 0 )
            return iLitMax;
        if ( iLitMin == 1 )
            return Abc_LitNot(iLitMax);
        if ( iLitMin == Abc_LitNot(iLitMax) )
            return 1;
    }
    else assert( 0 );
    assert( iLitMin >= 2 && iLitMax >= 2 );
    if ( iLit0 < iLit1 ) // and
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
        Vec_IntPushTwo( vRes, iLitMin, iLitMax );
    else if ( iLit0 > iLit1 ) // xor
    {
        assert( !Abc_LitIsCompl(iLit0) );
        assert( !Abc_LitIsCompl(iLit1) );
        Vec_IntPushTwo( vRes, iLitMax, iLitMin );
    }
    else assert( 0 );
    return iLitRes;
}
int Gia_Rsb2ManInsert_rec( Vec_Int_t * vRes, int nPis, Vec_Int_t * vObjs, int iNode, Vec_Int_t * vResub, Vec_Int_t * vDivs, Vec_Int_t * vCopies, int iObj )
{
    if ( Vec_IntEntry(vCopies, iObj) >= 0 )
        return Vec_IntEntry(vCopies, iObj);
    assert( iObj > nPis );
    if ( iObj == iNode )
    {
        int nVars = Vec_IntSize(vDivs);
345
        int iLitRes = -1, iTopLit = Vec_IntEntryLast( vResub );
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
        if ( Abc_Lit2Var(iTopLit) == 0 )
            iLitRes = 0;
        else if ( Abc_Lit2Var(iTopLit) < nVars )
            iLitRes = Gia_Rsb2ManInsert_rec( vRes, nPis, vObjs, -1, vResub, vDivs, vCopies, Vec_IntEntry(vDivs, Abc_Lit2Var(iTopLit)) );
        else
        {
            Vec_Int_t * vCopy = Vec_IntAlloc( 10 );
            int k, iLit, iLit0, iLit1;
            Vec_IntForEachEntryStop( vResub, iLit, k, Vec_IntSize(vResub)-1 )
                if ( Abc_Lit2Var(iLit) < nVars )
                    Gia_Rsb2ManInsert_rec( vRes, nPis, vObjs, -1, vResub, vDivs, vCopies, Vec_IntEntry(vDivs, Abc_Lit2Var(iLit)) );
            Vec_IntForEachEntryDouble( vResub, iLit0, iLit1, k )
            {
                int iVar0 = Abc_Lit2Var(iLit0);
                int iVar1 = Abc_Lit2Var(iLit1);
                int iRes0 = iVar0 < nVars ? Vec_IntEntry(vCopies, Vec_IntEntry(vDivs, iVar0)) : Vec_IntEntry(vCopy, iVar0 - nVars);
                int iRes1 = iVar1 < nVars ? Vec_IntEntry(vCopies, Vec_IntEntry(vDivs, iVar1)) : Vec_IntEntry(vCopy, iVar1 - nVars);
363
                iLitRes   = Gia_Rsb2AddNode( vRes, iLit0, iLit1, iRes0, iRes1 );
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
                Vec_IntPush( vCopy, iLitRes );
            }
            Vec_IntFree( vCopy );
        }
        iLitRes = Abc_LitNotCond( iLitRes, Abc_LitIsCompl(iTopLit) );
        Vec_IntWriteEntry( vCopies, iObj, iLitRes );
        return iLitRes;
    }
    else
    {
        int iLit0 = Vec_IntEntry( vObjs, 2*iObj+0 );
        int iLit1 = Vec_IntEntry( vObjs, 2*iObj+1 );
        int iRes0 = Gia_Rsb2ManInsert_rec( vRes, nPis, vObjs, iNode, vResub, vDivs, vCopies, Abc_Lit2Var(iLit0) );
        int iRes1 = Gia_Rsb2ManInsert_rec( vRes, nPis, vObjs, iNode, vResub, vDivs, vCopies, Abc_Lit2Var(iLit1) );
        int iLitRes = Gia_Rsb2AddNode( vRes, iLit0, iLit1, iRes0, iRes1 );
        Vec_IntWriteEntry( vCopies, iObj, iLitRes );
        return iLitRes;
    }
}
Vec_Int_t * Gia_Rsb2ManInsert( int nPis, int nPos, Vec_Int_t * vObjs, int iNode, Vec_Int_t * vResub, Vec_Int_t * vDivs, Vec_Int_t * vCopies )
{
385 386 387 388
    int i, nObjs = Vec_IntSize(vObjs)/2, iFirstPo = nObjs - nPos;
    Vec_Int_t * vRes = Vec_IntAlloc( Vec_IntSize(vObjs) );
//Vec_IntPrint( vDivs );
//Vec_IntPrint( vResub );
389 390 391
    Vec_IntFill( vCopies, Vec_IntSize(vObjs), -1 );
    Vec_IntFill( vRes, 2*(nPis + 1), 0 );
    for ( i = 0; i <= nPis; i++ )
392 393 394 395
        Vec_IntWriteEntry( vCopies, i, 2*i );
    for ( i = iFirstPo; i < nObjs; i++ )
        Gia_Rsb2ManInsert_rec( vRes, nPis, vObjs, iNode, vResub, vDivs, vCopies, Abc_Lit2Var( Vec_IntEntry(vObjs, 2*i) ) );
    for ( i = iFirstPo; i < nObjs; i++ )
396
    {
397
        int iLitNew = Abc_Lit2LitL( Vec_IntArray(vCopies), Vec_IntEntry(vObjs, 2*i) );
398 399 400 401 402
        Vec_IntPushTwo( vRes, iLitNew, iLitNew );
    }
    return vRes;
}

403 404 405 406 407 408 409 410 411 412 413 414

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
415 416 417 418 419 420 421
void Abc_ResubPrintDivs( void ** ppDivs, int nDivs )
{
    word ** pDivs = (word **)ppDivs;
    int i;
    for ( i = 0; i < nDivs; i++ )
    {
        printf( "Div %2d : ", i );
Alan Mishchenko committed
422
        Dau_DsdPrintFromTruth( pDivs[i], 6 );
423 424
    }
}
425 426 427 428 429 430 431 432 433
int Abc_ResubNodeToTry( Vec_Int_t * vTried, int iFirst, int iLast )
{
    int iNode;
    //for ( iNode = iFirst; iNode < iLast; iNode++ )
    for ( iNode = iLast - 1; iNode >= iFirst; iNode-- )
        if ( Vec_IntFind(vTried, iNode) == -1 )
            return iNode;
    return -1;
}
434
int Abc_ResubComputeWindow( int * pObjs, int nObjs, int nDivsMax, int nLevelIncrease, int fUseXor, int fUseZeroCost, int fDebug, int fVerbose, int ** ppArray, int * pnResubs )
435
{
436
    int iNode, nChanges = 0, RetValue = 0;
437 438 439 440 441 442
    Gia_Rsb2Man_t * p = Gia_Rsb2ManAlloc(); 
    Gia_Rsb2ManStart( p, pObjs, nObjs, nDivsMax, nLevelIncrease, fUseXor, fUseZeroCost, fDebug, fVerbose );
    *ppArray = NULL;
    while ( (iNode = Abc_ResubNodeToTry(&p->vTried, p->nPis+1, p->iFirstPo)) > 0 )
    {
        int nDivs = Gia_Rsb2ManDivs( p, iNode );
443
        int * pResub, nResub = Abc_ResubComputeFunction( Vec_PtrArray(&p->vpDivs), nDivs, 1, p->nMffc-1, nDivsMax, 0, fUseXor, fDebug, fVerbose, &pResub );
444 445 446 447 448 449 450
        if ( nResub == 0 )
            Vec_IntPush( &p->vTried, iNode );
        else
        {
            int i, k = 0, iTried;
            Vec_Int_t vResub = { nResub, nResub, pResub };
            Vec_Int_t * vRes = Gia_Rsb2ManInsert( p->nPis, p->nPos, &p->vObjs, iNode, &vResub, &p->vDivs, &p->vCopies );
451 452
            //printf( "\nResubing node %d:\n", iNode );
            //Gia_Rsb2ManPrint( p );
453 454 455 456 457 458
            p->nObjs    = Vec_IntSize(vRes)/2;
            p->iFirstPo = p->nObjs - p->nPos;
            Vec_IntClear( &p->vObjs );
            Vec_IntAppend( &p->vObjs, vRes );
            Vec_IntFree( vRes );
            Vec_IntForEachEntry( &p->vTried, iTried, i )
459 460
                if ( Vec_IntEntry(&p->vCopies, iTried) > Abc_Var2Lit(p->nPis, 0) ) // internal node
                    Vec_IntWriteEntry( &p->vTried, k++, Abc_Lit2Var(Vec_IntEntry(&p->vCopies, iTried)) );
461
            Vec_IntShrink( &p->vTried, k );
462 463
            nChanges++;
            //Gia_Rsb2ManPrint( p );
464 465
        }
    }
466
    if ( nChanges )
467 468 469 470 471 472
    {
        RetValue = p->nObjs;
        *ppArray = p->vObjs.pArray;
        Vec_IntZero( &p->vObjs );
    }
    Gia_Rsb2ManFree( p );
473 474
    if ( pnResubs )
        *pnResubs = nChanges;
475 476
    return RetValue;
}
477
int Abc_ResubComputeWindow2( int * pObjs, int nObjs, int nDivsMax, int nLevelIncrease, int fUseXor, int fUseZeroCost, int fDebug, int fVerbose, int ** ppArray, int * pnResubs )
478 479 480
{
    *ppArray = ABC_ALLOC( int, 2*nObjs );
    memmove( *ppArray, pObjs, 2*nObjs * sizeof(int) );
481 482
    if ( pnResubs )
        *pnResubs = 0;
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
    return nObjs;
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int * Gia_ManToResub( Gia_Man_t * p )
{
    Gia_Obj_t * pObj;
    int i, * pObjs = ABC_CALLOC( int, 2*Gia_ManObjNum(p) );
    assert( Gia_ManIsNormalized(p) );
    Gia_ManForEachObj1( p, pObj, i )
    {
        if ( Gia_ObjIsCi(pObj) )
            continue;
        pObjs[2*i+0] = Gia_ObjFaninLit0(Gia_ManObj(p, i), i);
        if ( Gia_ObjIsCo(pObj) )
            pObjs[2*i+1] = pObjs[2*i+0];
        else if ( Gia_ObjIsAnd(pObj) )
            pObjs[2*i+1] = Gia_ObjFaninLit1(Gia_ManObj(p, i), i);
        else assert( 0 );
    }
    return pObjs;
}
515
Gia_Man_t * Gia_ManFromResub( int * pObjs, int nObjs, int nIns )
516 517 518 519 520
{
    Gia_Man_t * pNew = Gia_ManStart( nObjs );
    int i;
    for ( i = 1; i < nObjs; i++ )
    {
521
        if ( pObjs[2*i] == 0 && i <= nIns ) // pi
522 523 524 525 526 527 528 529 530 531 532 533 534 535
            Gia_ManAppendCi( pNew );
        else if ( pObjs[2*i] == pObjs[2*i+1] ) // po
            Gia_ManAppendCo( pNew, pObjs[2*i] );
        else if ( pObjs[2*i] < pObjs[2*i+1] )
            Gia_ManAppendAnd( pNew, pObjs[2*i], pObjs[2*i+1] );
        else if ( pObjs[2*i] > pObjs[2*i+1] )
            Gia_ManAppendXor( pNew, pObjs[2*i], pObjs[2*i+1] );
        else assert( 0 );
    }
    return pNew;
}
Gia_Man_t * Gia_ManResub2Test( Gia_Man_t * p )
{
    Gia_Man_t * pNew;
536 537
    int nResubs, nObjsNew, * pObjsNew, * pObjs = Gia_ManToResub( p );
//Gia_ManPrint( p );
538
    Abc_ResubPrepareManager( 1 );
539
    nObjsNew = Abc_ResubComputeWindow( pObjs, Gia_ManObjNum(p), 1000, -1, 0, 0, 0, 0, &pObjsNew, &nResubs );
540
    //printf( "Performed resub %d times.  Reduced %d nodes.\n", nResubs, nObjsNew ? Gia_ManObjNum(p) - nObjsNew : 0 );
541
    Abc_ResubPrepareManager( 0 );
542
    if ( nObjsNew )
543
    {
544
        pNew = Gia_ManFromResub( pObjsNew, nObjsNew, Gia_ManCiNum(p) );
545 546
        pNew->pName = Abc_UtilStrsav( p->pName );
    }
547 548
    else 
        pNew = Gia_ManDup( p );
549 550 551 552 553
    ABC_FREE( pObjs );
    ABC_FREE( pObjsNew );
    return pNew;
}

554 555 556 557 558 559 560 561 562 563 564 565 566 567
/**Function*************************************************************

  Synopsis    [Creating a window with support composed of primary inputs.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
// returns the number of nodes added to the window when is iPivot is added
// the window nodes in vNodes are labeled with the current traversal ID
// the new node iPivot and its fanout are temporarily labeled and then unlabeled
568
int Gia_WinTryAddingNode( Gia_Man_t * p, int iPivot, int iPivot2, Vec_Wec_t * vLevels, Vec_Int_t * vNodes )
569 570 571 572 573 574 575 576 577 578 579
{
    Vec_Int_t * vLevel; 
    Gia_Obj_t * pObj, * pFanout;
    int k, i, f, Count = 0;
    // precondition:  the levelized structure is empty
    assert( Vec_WecSizeSize(vLevels) == 0 );
    // precondition:  the new object to be added (iPivot) is not in the window
    assert( !Gia_ObjIsTravIdCurrentId(p, iPivot) );
    // add the object to the window and to the levelized structure
    Gia_ObjSetTravIdCurrentId( p, iPivot );
    Vec_WecPush( vLevels, Gia_ObjLevelId(p, iPivot), iPivot );
580 581 582 583 584 585 586 587 588
    // the same about the second node if it is given
    if ( iPivot2 != -1 )
    {
        // precondition:  the new object to be added (iPivot2) is not in the window
        assert( !Gia_ObjIsTravIdCurrentId(p, iPivot2) );
        // add the object to the window and to the levelized structure
        Gia_ObjSetTravIdCurrentId( p, iPivot2 );
        Vec_WecPush( vLevels, Gia_ObjLevelId(p, iPivot2), iPivot2 );
    }
589 590 591
    // iterate through all objects and explore their fanouts
    Vec_WecForEachLevel( vLevels, vLevel, k )
        Gia_ManForEachObjVec( vLevel, p, pObj, i )
592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607
            Gia_ObjForEachFanoutStatic( p, pObj, pFanout, f )
            {
                if ( f == 5 ) // explore first 5 fanouts of the node
                    break;
                if ( Gia_ObjIsAnd(pFanout) && // internal node
                    !Gia_ObjIsTravIdCurrent(p, pFanout) && // not in the window
                     Gia_ObjIsTravIdCurrent(p, Gia_ObjFanin0(pFanout)) && // but fanins are
                     Gia_ObjIsTravIdCurrent(p, Gia_ObjFanin1(pFanout)) )  // in the window
                {
                    // add fanout to the window and to the levelized structure
                    Gia_ObjSetTravIdCurrent( p, pFanout );
                    Vec_WecPush( vLevels, Gia_ObjLevel(p, pFanout), Gia_ObjId(p, pFanout) );
                    // count the number of nodes in the structure
                    Count++;
                }
            }
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
    // iterate through the nodes in the levelized structure
    Vec_WecForEachLevel( vLevels, vLevel, k )
    {
        Gia_ManForEachObjVec( vLevel, p, pObj, i )
            if ( vNodes == NULL ) // it was a test run - unmark the node
                Gia_ObjSetTravIdPrevious( p, pObj );
            else // it was a real run - permanently add to the node to the window
                Vec_IntPush( vNodes, Gia_ObjId(p, pObj) );
        // clean the levelized structure
        Vec_IntClear( vLevel );
    }
    // return the number of nodes to be added
    return Count;
}
// find the first PI to add based on the fanout count
int Gia_WinAddCiWithMaxFanouts( Gia_Man_t * p )
{
    int i, Id, nMaxFan = -1, iMaxFan = -1;
    Gia_ManForEachCiId( p, Id, i )
        if ( nMaxFan < Gia_ObjFanoutNumId(p, Id) )
        {
            nMaxFan = Gia_ObjFanoutNumId(p, Id);
            iMaxFan = Id;
        }
    assert( iMaxFan >= 0 );
    return iMaxFan;
}
// find the next PI to add based on how many nodes will be added to the window
int Gia_WinAddCiWithMaxDivisors( Gia_Man_t * p, Vec_Wec_t * vLevels )
{
    int i, Id, nCurFan, nMaxFan = -1, iMaxFan = -1;
    Gia_ManForEachCiId( p, Id, i )
    {
        if ( Gia_ObjIsTravIdCurrentId( p, Id ) )
            continue;
643
        nCurFan = Gia_WinTryAddingNode( p, Id, -1, vLevels, NULL );
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
        if ( nMaxFan < nCurFan )
        {
            nMaxFan = nCurFan;
            iMaxFan = Id;
        }
    }
    assert( iMaxFan >= 0 );
    return iMaxFan;
}
// check if the node has unmarked fanouts
int Gia_WinNodeHasUnmarkedFanouts( Gia_Man_t * p, int iPivot )
{
    int f, iFan;
    Gia_ObjForEachFanoutStaticId( p, iPivot, iFan, f )
        if ( !Gia_ObjIsTravIdCurrentId(p, iFan) )
            return 1;
    return 0;
}
// this is a translation procedure, which converts the array of node IDs (vObjs) 
// into the internal represnetation for the resub engine, which is returned
// (this procedure is not needed when we simply construct a window)
Vec_Int_t * Gia_RsbCiTranslate( Gia_Man_t * p, Vec_Int_t * vObjs, Vec_Int_t * vMap )
{
    int i, iObj, Lit0, Lit1, Fan0, Fan1;
    Vec_Int_t * vNodes = Vec_IntAlloc( 100 );
    assert( Vec_IntSize(vMap) == Gia_ManObjNum(p) );
    Vec_IntPushTwo( vNodes, 0, 0 ); // const0 node
    Vec_IntForEachEntry( vObjs, iObj, i )
    {
        Gia_Obj_t * pObj = Gia_ManObj(p, iObj);
        assert( Gia_ObjIsTravIdCurrentId(p, iObj) );
        Fan0 = Gia_ObjIsCi(pObj) ? 0 : Vec_IntEntry(vMap, Gia_ObjFaninId0(pObj, iObj));
        Fan1 = Gia_ObjIsCi(pObj) ? 0 : Vec_IntEntry(vMap, Gia_ObjFaninId1(pObj, iObj));
        Lit0 = Gia_ObjIsCi(pObj) ? 0 : Abc_LitNotCond( Fan0, Gia_ObjFaninC0(pObj) );
        Lit1 = Gia_ObjIsCi(pObj) ? 0 : Abc_LitNotCond( Fan1, Gia_ObjFaninC1(pObj) );
        Vec_IntWriteEntry( vMap, iObj, Vec_IntSize(vNodes) );
        Vec_IntPushTwo( vNodes, Lit0, Lit1 );
    }
    Vec_IntForEachEntry( vObjs, iObj, i )
        if ( Gia_WinNodeHasUnmarkedFanouts( p, iObj ) )
            Vec_IntPushTwo( vNodes, Vec_IntEntry(vMap, iObj), Vec_IntEntry(vMap, iObj) );
    return vNodes;
}
// construct a high-volume window support by the given number (nPis) of primary inputs
Vec_Int_t * Gia_RsbCiWindow( Gia_Man_t * p, int nPis )
{
    Vec_Int_t * vRes;  int i, iMaxFan;
    Vec_Int_t * vNodes  = Vec_IntAlloc( 100 );
    Vec_Int_t * vMap    = Vec_IntStartFull( Gia_ManObjNum(p) );
    Vec_Wec_t * vLevels = Vec_WecStart( Gia_ManLevelNum(p)+1 );
    Gia_ManStaticFanoutStart( p );
    Gia_ManIncrementTravId(p);
    // add the first one
    iMaxFan = Gia_WinAddCiWithMaxFanouts( p );
    Gia_ObjSetTravIdCurrentId( p, iMaxFan );
    Vec_IntPush( vNodes, iMaxFan );
    // add remaining ones
    for ( i = 1; i < nPis; i++ )
    {
        iMaxFan = Gia_WinAddCiWithMaxDivisors( p, vLevels );
704
        Gia_WinTryAddingNode( p, iMaxFan, -1, vLevels, vNodes );
705 706 707 708 709 710
    }
    Vec_IntSort( vNodes, 0 );
    vRes = Gia_RsbCiTranslate( p, vNodes, vMap );
    Gia_ManStaticFanoutStop( p );
    Vec_WecFree( vLevels );
    Vec_IntFree( vMap );
711
//Vec_IntPrint( vNodes );
712 713 714 715 716 717 718 719 720 721
    Vec_IntFree( vNodes );
    return vRes;
}
void Gia_RsbCiWindowTest( Gia_Man_t * p )
{
    Vec_Int_t * vWin = Gia_RsbCiWindow( p, 6 );
    //Vec_IntPrint( vWin );
    Vec_IntFree( vWin );
}

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



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

  Synopsis    [Return initial window for the given node.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline int  Gia_ObjFaninId( Gia_Obj_t * pObj, int iObj, int n )          { return n ? Gia_ObjFaninId1(pObj, iObj) : Gia_ObjFaninId0(pObj, iObj);  }

static inline int  Gia_ObjTravIsTopTwo( Gia_Man_t * p, int iNodeA )            { return (p->pTravIds[iNodeA] >= p->nTravIds - 1);       }
static inline int  Gia_ObjTravIsSame( Gia_Man_t * p, int iNodeA, int iNodeB )  { return (p->pTravIds[iNodeA] == p->pTravIds[iNodeB]);   }
static inline void Gia_ObjTravSetSame( Gia_Man_t * p, int iNodeA, int iNodeB ) { p->pTravIds[iNodeA] = p->pTravIds[iNodeB];             }

// collect nodes on the path from the meeting point to the root node, excluding the meeting point
void Gia_RsbWindowGather( Gia_Man_t * p, Vec_Int_t * vPaths, int iNode, Vec_Int_t * vVisited )
{
    int iPrev;
    if ( iNode == 0 )
        return;
    Vec_IntPush( vVisited, iNode );
    iPrev = Vec_IntEntry( vPaths, iNode );
    if ( iPrev == 0 )
        return;
    assert( Gia_ObjTravIsSame(p, iPrev, iNode) );
    Gia_RsbWindowGather( p, vPaths, iPrev, vVisited );
}
// explore the frontier of nodes in the breadth-first traversal
int Gia_RsbWindowExplore( Gia_Man_t * p, Vec_Int_t * vVisited, int iStart, Vec_Int_t * vPaths, int * piMeet, int * piNode )
{
    int i, n, iObj, iLimit = Vec_IntSize( vVisited );
    *piMeet = *piNode = 0;
    Vec_IntForEachEntryStartStop( vVisited, iObj, i, iStart, iLimit )
    {
        Gia_Obj_t * pObj = Gia_ManObj( p, iObj );
        if ( !Gia_ObjIsAnd(pObj) )
            continue;
        for ( n = 0; n < 2; n++ )
        {
            int iFan = Gia_ObjFaninId( pObj, iObj, n );
            // if the node was visited on the paths to both fanins, collect it
            if ( Gia_ObjTravIsTopTwo(p, iObj) && Gia_ObjTravIsTopTwo(p, iFan) && !Gia_ObjTravIsSame(p, iObj, iFan) )
            {
                *piMeet = iFan;
                *piNode = iObj;
                return 1;
            }
            // if the node was visited already on this path, skip it
            if ( Gia_ObjTravIsTopTwo(p, iFan) )
            {
                assert( Gia_ObjTravIsSame(p, iObj, iFan) );
                continue;
            }
            // label the node as visited
            Gia_ObjTravSetSame( p, iFan, iObj );
            Vec_IntWriteEntry( vPaths, iFan, iObj ); 
            Vec_IntPush( vVisited, iFan );
        }
    }
    return 0;
}
Vec_Int_t * Gia_RsbWindowInit( Gia_Man_t * p, Vec_Int_t * vPaths, int iPivot, int nIter )
{
    Vec_Int_t * vVisited = Vec_IntAlloc( 100 );
    Gia_Obj_t * pPivot = Gia_ManObj( p, iPivot ); 
    int i, n, iStart = 0;
    assert( Gia_ObjIsAnd(pPivot) );
    // start paths for both fanins of the pivot node
    for ( n = 0; n < 2; n++ )
    {
        int iFan = Gia_ObjFaninId( pPivot, iPivot, n );
        Gia_ManIncrementTravId(p);
        Vec_IntPush( vVisited, iFan );
        Vec_IntWriteEntry( vPaths, iFan, 0 ); 
        Gia_ObjSetTravIdCurrentId( p, iFan );
    }
    // perform several iterations of breadth-first search
    for ( i = 0; i < nIter; i++ )
    {
        int iMeet, iNode, iNext = Vec_IntSize(vVisited);
        if ( Gia_RsbWindowExplore( p, vVisited, iStart, vPaths, &iMeet, &iNode ) )
        {
            // found the shared path
            if ( Gia_ObjIsTravIdCurrentId(p, iMeet) )
                assert( Gia_ObjIsTravIdPreviousId(p, iNode) );
            else if ( Gia_ObjIsTravIdPreviousId(p, iMeet) )
                assert( Gia_ObjIsTravIdCurrentId(p, iNode) );
            else assert( 0 );
            // collect the initial window
            Vec_IntClear( vVisited );
            Gia_RsbWindowGather( p, vPaths, Vec_IntEntry(vPaths, iMeet), vVisited );
            Gia_RsbWindowGather( p, vPaths, iNode, vVisited );
            Vec_IntPush( vVisited, iPivot );
            break;
        }
        iStart = iNext;
    }
    // if no meeting point is found, make sure to return NULL
    if ( i == nIter )
        Vec_IntFreeP( &vVisited );
    return vVisited;
}
Vec_Int_t * Gia_RsbCreateWindowInputs( Gia_Man_t * p, Vec_Int_t * vWin )
{
    Vec_Int_t * vInputs = Vec_IntAlloc(10);
    Gia_Obj_t * pObj; int i, n, iObj;
    Gia_ManIncrementTravId(p);
    Gia_ManForEachObjVec( vWin, p, pObj, i )
        Gia_ObjSetTravIdCurrent(p, pObj);
    Gia_ManForEachObjVec( vWin, p, pObj, i )
    {
        assert( Gia_ObjIsAnd(pObj) );
        for ( n = 0; n < 2; n++ )
        {
            int iFan = n ? Gia_ObjFaninId1p(p, pObj) : Gia_ObjFaninId0p(p, pObj);
            if ( !Gia_ObjIsTravIdCurrentId(p, iFan) )
                Vec_IntPushUnique( vInputs, iFan );
        }
    }
    Vec_IntForEachEntry( vInputs, iObj, i )
    {
        Gia_ObjSetTravIdCurrentId( p, iObj );
        Vec_IntPush( vWin, iObj );
    }
    return vInputs;
}

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

  Synopsis    [Grow window for the node.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Gia_RsbAddSideInputs( Gia_Man_t * p, Vec_Wec_t * vLevels, Vec_Int_t * vWin )
{
    Vec_Int_t * vLevel; 
    Gia_Obj_t * pObj, * pFanout;
    int k, i, f, iObj;
    // precondition: the levelized structure is empty
    assert( Vec_WecSizeSize(vLevels) == 0 );
    // precondition: window nodes are labeled with the current ID
    Vec_IntForEachEntry( vWin, iObj, i )
    {
        assert( Gia_ObjIsTravIdCurrentId(p, iObj) );
        Vec_WecPush( vLevels, Gia_ObjLevelId(p, iObj), iObj );
    }
    // iterate through all objects and explore their fanouts
    Vec_WecForEachLevel( vLevels, vLevel, k )
        Gia_ManForEachObjVec( vLevel, p, pObj, i )
            Gia_ObjForEachFanoutStatic( p, pObj, pFanout, f )
            {
                if ( f == 5 ) // explore first 5 fanouts of the node
                    break;
                if ( Gia_ObjIsAnd(pFanout) && // internal node
                    !Gia_ObjIsTravIdCurrent(p, pFanout) && // not in the window
                     Gia_ObjIsTravIdCurrent(p, Gia_ObjFanin0(pFanout)) && // but fanins are
                     Gia_ObjIsTravIdCurrent(p, Gia_ObjFanin1(pFanout)) )  // in the window
                {
                    // add fanout to the window and to the levelized structure
                    Gia_ObjSetTravIdCurrent( p, pFanout );
                    Vec_WecPush( vLevels, Gia_ObjLevel(p, pFanout), Gia_ObjId(p, pFanout) );
                    Vec_IntPush( vWin, Gia_ObjId(p, pFanout) );
                }
            }
    // iterate through the nodes in the levelized structure
    Vec_WecForEachLevel( vLevels, vLevel, k )
        Vec_IntClear( vLevel );
}
// expland inputs until saturation while adding the side-fanouts
void Gia_RsbExpandInputs( Gia_Man_t * p, Vec_Wec_t * vLevels, Vec_Int_t * vWin, Vec_Int_t * vInputs )
{
    Gia_Obj_t * pObj; 
    int i, n, iFans[2], fChange = 1;
    while ( fChange )
    {
        fChange = 0;
        Gia_ManForEachObjVec( vInputs, p, pObj, i )
        {
            if ( !Gia_ObjIsAnd(pObj) )
                continue;
            iFans[0] = Gia_ObjFaninId0p(p, pObj);
            iFans[1] = Gia_ObjFaninId1p(p, pObj);
            if ( !Gia_ObjIsTravIdCurrentId(p, iFans[0]) && !Gia_ObjIsTravIdCurrentId(p, iFans[1]) )
                continue;
            Vec_IntRemove( vInputs, Gia_ObjId(p, pObj) );
            assert( Vec_IntFind(vWin, Gia_ObjId(p, pObj)) >= 0 );
            for ( n = 0; n < 2; n++ )
            {
                if ( Gia_ObjIsTravIdCurrentId(p, iFans[n]) )
                    continue;
                assert( Vec_IntFind(vInputs, iFans[n]) == -1 );
                Vec_IntPush( vInputs, iFans[n] );
                Gia_WinTryAddingNode( p, iFans[n], -1, vLevels, vWin );
                assert( Gia_ObjIsTravIdCurrentId(p, iFans[n]) );
            }
            fChange = 1;
        }
    }
}
// select the best input to expand, based on its contribution to the window size
int Gia_RsbSelectOneInput( Gia_Man_t * p, Vec_Wec_t * vLevels, Vec_Int_t * vIns )
{
    int i, iNode = 0, WeightThis, WeightBest = -1;
    Gia_Obj_t * pObj; 
    Gia_ManForEachObjVec( vIns, p, pObj, i )
        if ( Gia_ObjIsAnd(pObj) )
        {
            int iFan0 = Gia_ObjFaninId0p(p, pObj);
            int iFan1 = Gia_ObjFaninId1p(p, pObj);
            assert( !Gia_ObjIsTravIdCurrentId(p, iFan0) && !Gia_ObjIsTravIdCurrentId(p, iFan1) );
            WeightThis = Gia_WinTryAddingNode( p, iFan0, iFan1, vLevels, NULL );
            if ( WeightBest < WeightThis )
            {
                WeightBest = WeightThis;
                iNode = Gia_ObjId(p, pObj);
            }
        }
    return iNode;
}
// grow the initial window as long as it fits the input count limit
void Gia_RsbWindowGrow( Gia_Man_t * p, Vec_Wec_t * vLevels, Vec_Int_t * vWin, Vec_Int_t * vIns, int nInputsMax )
{
    int iNode;
    Gia_RsbAddSideInputs( p, vLevels, vWin );
    Gia_RsbExpandInputs( p, vLevels, vWin, vIns );
    while ( Vec_IntSize(vIns) < nInputsMax && (iNode = Gia_RsbSelectOneInput(p, vLevels, vIns)) )
    {
        int iFan0 = Gia_ObjFaninId0p(p, Gia_ManObj(p, iNode));
        int iFan1 = Gia_ObjFaninId1p(p, Gia_ManObj(p, iNode));
        assert( !Gia_ObjIsTravIdCurrentId(p, iFan0) && !Gia_ObjIsTravIdCurrentId(p, iFan1) );
        Gia_WinTryAddingNode( p, iFan0, iFan1, vLevels, vWin );
        assert(  Gia_ObjIsTravIdCurrentId(p, iFan0) &&  Gia_ObjIsTravIdCurrentId(p, iFan1) );
        Vec_IntRemove( vIns, iNode );
        Vec_IntPushTwo( vIns, iFan0, iFan1 );
        Gia_RsbExpandInputs( p, vLevels, vWin, vIns );
    }
}

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

973 974 975 976 977 978 979 980 981
  Synopsis    [Grow window for the node.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
982 983 984 985 986 987 988 989 990 991 992 993
void Gia_WinCreateFromCut_rec( Gia_Man_t * p, int iObj, Vec_Int_t * vWin )
{
    Gia_Obj_t * pObj;
    if ( Gia_ObjIsTravIdCurrentId(p, iObj) )
        return;
    Gia_ObjSetTravIdCurrentId(p, iObj);
    pObj = Gia_ManObj( p, iObj );
    assert( Gia_ObjIsAnd(pObj) );
    Gia_WinCreateFromCut_rec( p, Gia_ObjFaninId0(pObj, iObj), vWin );
    Gia_WinCreateFromCut_rec( p, Gia_ObjFaninId1(pObj, iObj), vWin );
    Vec_IntPush( vWin, iObj );
}
994
// uses levelized structure (vLevels) to collect in array vWin divisors supported by the cut (vIn)
995
void Gia_WinCreateFromCut( Gia_Man_t * p, int iPivot, Vec_Int_t * vIn, Vec_Wec_t * vLevels, Vec_Int_t * vWin )
996 997 998 999 1000 1001 1002 1003 1004
{
    Vec_Int_t * vLevel; 
    Gia_Obj_t * pObj, * pFanout;
    int k, i, f, iObj, Level;
    Vec_Int_t * vUsed = Vec_IntAlloc( 100 );
    // precondition:  the levelized structure is empty
    assert( Vec_WecSizeSize(vLevels) == 0 );
    // clean the resulting array
    Vec_IntClear( vWin );
1005
    // collect leaves
1006 1007 1008 1009 1010
    Gia_ManIncrementTravId( p );
    Vec_IntForEachEntry( vIn, iObj, i )
    {
        Gia_ObjSetTravIdCurrentId( p, iObj );
        Vec_IntPush( vWin, iObj );
1011 1012 1013 1014 1015 1016 1017
    }
    // collect internal cone
    Gia_WinCreateFromCut_rec( p, iPivot, vWin );
    // add nodes to the levelized structure
    Vec_IntForEachEntry( vWin, iObj, i )
    {
        Vec_WecPush( vLevels, Gia_ObjLevelId(p, iObj), iObj );
1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118
        Vec_IntPushUniqueOrder( vUsed, Gia_ObjLevelId(p, iObj) );
    }
    // iterate through all objects and explore their fanouts
    //Vec_WecForEachLevel( vLevels, vLevel, k )
    Vec_IntForEachEntry( vUsed, Level, k )
    {
        vLevel = Vec_WecEntry( vLevels, Level );
        Gia_ManForEachObjVec( vLevel, p, pObj, i )
            Gia_ObjForEachFanoutStatic( p, pObj, pFanout, f )
            {
                if ( f == 5 ) // explore first 5 fanouts of the node
                    break;
                if ( Gia_ObjIsAnd(pFanout) && // internal node
                    !Gia_ObjIsTravIdCurrent(p, pFanout) && // not in the window
                     Gia_ObjIsTravIdCurrent(p, Gia_ObjFanin0(pFanout)) && // but fanins are
                     Gia_ObjIsTravIdCurrent(p, Gia_ObjFanin1(pFanout)) )  // in the window
                {
                    // add fanout to the window and to the levelized structure
                    Gia_ObjSetTravIdCurrent( p, pFanout );
                    Vec_WecPush( vLevels, Gia_ObjLevel(p, pFanout), Gia_ObjId(p, pFanout) );
                    Vec_IntPush( vWin, Gia_ObjId(p, pFanout) );
                    Vec_IntPushUniqueOrder( vUsed, Gia_ObjLevel(p, pFanout) );
                }
            }
        Vec_IntClear( vLevel );
    }
    Vec_IntSort( vWin, 0 );
    Vec_IntFree( vUsed );
}
// update the cut until both fanins of AND nodes are not in the cut
int Gia_RsbExpandCut( Gia_Man_t * p, Vec_Int_t * vIns )
{
    int fOnlyPis = 0, fChange = 1, nSize = Vec_IntSize(vIns);
    while ( fChange )
    {
        Gia_Obj_t * pObj;
        int i, iFan0, iFan1, fHave0, fHave1;
        fOnlyPis = 1;
        fChange = 0;
        // check if some nodes can be expanded without increasing cut size
        Gia_ManForEachObjVec( vIns, p, pObj, i )
        {
            assert( Gia_ObjIsTravIdCurrent(p, pObj) );
            if ( !Gia_ObjIsAnd(pObj) )
                continue;
            fOnlyPis = 0;
            iFan0 = Gia_ObjFaninId0p(p, pObj);
            iFan1 = Gia_ObjFaninId1p(p, pObj);
            fHave0 = Gia_ObjIsTravIdCurrentId(p, iFan0);
            fHave1 = Gia_ObjIsTravIdCurrentId(p, iFan1);
            if ( !fHave0 && !fHave1 )
                continue;
            // can expand because one of the fanins is already in the cut
            // remove current cut node
            Vec_IntDrop( vIns, i );
            // add missing fanin
            if ( !fHave0 )  
            {
                Vec_IntPush( vIns, iFan0 );
                Gia_ObjSetTravIdCurrentId( p, iFan0 );
            }
            if ( !fHave1 )  
            {
                Vec_IntPush( vIns, iFan1 );
                Gia_ObjSetTravIdCurrentId( p, iFan1 );
            }
            fChange = 1;
            break;
        }
    }
    assert( Vec_IntSize(vIns) <= nSize );
    return fOnlyPis;
}
int Gia_RsbFindFaninAdd( int iFan, int pFanins[32], int pFaninCounts[32], int nFanins )
{
    int i;
    for ( i = 0; i < nFanins; i++ )
        if ( pFanins[i] == iFan )
            break;
    pFanins[i] = iFan;
    pFaninCounts[i]++;
    return nFanins + (i == nFanins);
}                        
int Gia_RsbFindFaninToAddToCut( Gia_Man_t * p, Vec_Int_t * vIns )
{
    Gia_Obj_t * pObj;
    int nFanins = 0, pFanins[64] = {0}, pFaninCounts[64] = {0};
    int i, iFan0, iFan1, iFanMax = -1, CountMax = 0;
    Gia_ManForEachObjVec( vIns, p, pObj, i )
    {
        if ( !Gia_ObjIsAnd(pObj) )
            continue;
        iFan0 = Gia_ObjFaninId0p(p, pObj);
        iFan1 = Gia_ObjFaninId1p(p, pObj);
        assert( !Gia_ObjIsTravIdCurrentId(p, iFan0) );
        assert( !Gia_ObjIsTravIdCurrentId(p, iFan1) );
        nFanins = Gia_RsbFindFaninAdd( iFan0, pFanins, pFaninCounts, nFanins );
        nFanins = Gia_RsbFindFaninAdd( iFan1, pFanins, pFaninCounts, nFanins );
        assert( nFanins < 64 );
    }
    // find fanin with the highest count
1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136
    if ( p->vFanoutNums != NULL )
    {
        for ( i = 0; i < nFanins; i++ )
            if ( CountMax < pFaninCounts[i] || (CountMax == pFaninCounts[i] && (Gia_ObjFanoutNumId(p, iFanMax) < Gia_ObjFanoutNumId(p, pFanins[i]))) )
            {
                CountMax = pFaninCounts[i];
                iFanMax  = pFanins[i];
            }
    }
    else
    {
        for ( i = 0; i < nFanins; i++ )
            if ( CountMax < pFaninCounts[i] || (CountMax == pFaninCounts[i] && (Gia_ObjRefNumId(p, iFanMax) < Gia_ObjRefNumId(p, pFanins[i]))) )
            {
                CountMax = pFaninCounts[i];
                iFanMax  = pFanins[i];
            }
    }
1137 1138 1139
    return iFanMax;
}
// precondition: nodes in vWin and in vIns are marked with the current ID
1140
void Gia_RsbWindowGrow2( Gia_Man_t * p, int iObj, Vec_Wec_t * vLevels, Vec_Int_t * vWin, Vec_Int_t * vIns, int nInputsMax )
1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180
{
    // window will be recomputed later
    Vec_IntClear( vWin );
    // expand the cut without increasing its cost
    if ( !Gia_RsbExpandCut( p, vIns ) )
    {
        // save it as the best cut
        Vec_Int_t * vBest = Vec_IntSize(vIns) <= nInputsMax ? Vec_IntDup(vIns) : NULL;
        int fOnlyPis = 0, Iter = 0;
        // iterate expansion until  
        // (1) the cut cannot be expanded because all leaves are PIs
        // (2) the cut size exceeded the limit for 5 consecutive iterations
        while ( !fOnlyPis && (Vec_IntSize(vIns) <= nInputsMax || Iter < 5) )
        {
            int iFanBest = Gia_RsbFindFaninToAddToCut( p, vIns );
            Vec_IntPush( vIns, iFanBest );
            Gia_ObjSetTravIdCurrentId( p, iFanBest );
            fOnlyPis = Gia_RsbExpandCut( p, vIns );
            if ( Vec_IntSize(vIns) > nInputsMax )
                Iter++;
            else
                Iter = 0;                
            if ( Vec_IntSize(vIns) <= nInputsMax && (!vBest || Vec_IntSize(vBest) <= Vec_IntSize(vIns)) )
            {
                if ( vBest )
                    Vec_IntClear(vBest);
                else
                    vBest = Vec_IntAlloc( 10 );
                Vec_IntAppend( vBest, vIns );
            }
        }
        if ( vBest )
        {
            Vec_IntClear( vIns );
            Vec_IntAppend( vIns, vBest );
            Vec_IntFree( vBest );
        }
        else
            assert( Vec_IntSize(vIns) > nInputsMax );
    }
1181
    if ( vLevels && Vec_IntSize(vIns) <= nInputsMax )
1182 1183
    {
        Vec_IntSort( vIns, 0 );
1184
        Gia_WinCreateFromCut( p, iObj, vIns, vLevels, vWin );
1185 1186 1187 1188 1189
    }
}

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

1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209
  Synopsis    [Create window for the node.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Gia_RsbWindowCompute( Gia_Man_t * p, int iObj, int nInputsMax, int nLevelsMax, Vec_Wec_t * vLevels, Vec_Int_t * vPaths, Vec_Int_t ** pvWin, Vec_Int_t ** pvIns )
{
    Vec_Int_t * vWin, * vIns;
    *pvWin = *pvIns = NULL;
    vWin = Gia_RsbWindowInit( p, vPaths, iObj, nLevelsMax );
    if ( vWin == NULL )
        return 0;
    vIns = Gia_RsbCreateWindowInputs( p, vWin ); 
    // vWin and vIns are labeled with the current trav ID
    //Vec_IntPrint( vWin );    
    //Vec_IntPrint( vIns );    
1210
    if ( Vec_IntSize(vIns) <= nInputsMax + 3 ) // consider windows, which initially has a larger input space
1211
        Gia_RsbWindowGrow2( p, iObj, vLevels, vWin, vIns, nInputsMax );
1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229
    if ( Vec_IntSize(vIns) <= nInputsMax )
    {
        Vec_IntSort( vWin, 0 );
        Vec_IntSort( vIns, 0 );
        *pvWin = vWin;
        *pvIns = vIns;
        return 1;
    }
    else
    {
        Vec_IntFree( vWin );
        Vec_IntFree( vIns );
        return 0;
    }
}

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

1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242
  Synopsis    [Derive GIA from the window]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Vec_Int_t * Gia_RsbFindOutputs( Gia_Man_t * p, Vec_Int_t * vWin, Vec_Int_t * vIns, Vec_Int_t * vRefs )
{
    Vec_Int_t * vOuts = Vec_IntAlloc( 100 );
    Gia_Obj_t * pObj; int i;
1243 1244 1245
    Gia_ManIncrementTravId( p );
    Gia_ManForEachObjVec( vIns, p, pObj, i ) 
        Gia_ObjSetTravIdCurrent( p, pObj );
1246
    Gia_ManForEachObjVec( vWin, p, pObj, i ) 
1247
        if ( !Gia_ObjIsTravIdCurrent(p, pObj) && Gia_ObjIsAnd(pObj) )
1248 1249 1250 1251 1252
        {
            Vec_IntAddToEntry( vRefs, Gia_ObjFaninId0p(p, pObj), 1 );
            Vec_IntAddToEntry( vRefs, Gia_ObjFaninId1p(p, pObj), 1 );
        }
    Gia_ManForEachObjVec( vWin, p, pObj, i )
1253
        if ( !Gia_ObjIsTravIdCurrent(p, pObj) && Gia_ObjFanoutNum(p, pObj) != Vec_IntEntry(vRefs, Gia_ObjId(p, pObj)) )
1254 1255
            Vec_IntPush( vOuts, Gia_ObjId(p, pObj) );
    Gia_ManForEachObjVec( vWin, p, pObj, i )
1256
        if ( !Gia_ObjIsTravIdCurrent(p, pObj) && Gia_ObjIsAnd(pObj) )
1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312
        {
            Vec_IntAddToEntry( vRefs, Gia_ObjFaninId0p(p, pObj), -1 );
            Vec_IntAddToEntry( vRefs, Gia_ObjFaninId1p(p, pObj), -1 );
        }
    return vOuts;
}

Gia_Man_t * Gia_RsbDeriveGiaFromWindows( Gia_Man_t * p, Vec_Int_t * vWin, Vec_Int_t * vIns, Vec_Int_t * vOuts )
{
    Gia_Man_t * pNew; 
    Gia_Obj_t * pObj;
    int i;
    pNew = Gia_ManStart( Gia_ManObjNum(p) );
    pNew->pName = Abc_UtilStrsav( p->pName );
    pNew->pSpec = Abc_UtilStrsav( p->pSpec );
    Gia_ManHashAlloc( pNew );
    Gia_ManFillValue( p );
    Gia_ManConst0(p)->Value = 0;
    Gia_ManForEachObjVec( vIns, p, pObj, i )
        pObj->Value = Gia_ManAppendCi( pNew );
    Gia_ManForEachObjVec( vWin, p, pObj, i )
        if ( !~pObj->Value )
            pObj->Value = Gia_ManHashAnd( pNew, Gia_ObjFanin0Copy(pObj), Gia_ObjFanin1Copy(pObj) );
    Gia_ManForEachObjVec( vOuts, p, pObj, i )
        Gia_ManAppendCo( pNew, pObj->Value );
    Gia_ManHashStop( pNew );
    return pNew;
}

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

  Synopsis    [Naive truth-table-based verification.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
word Gia_LutComputeTruth66_rec( Gia_Man_t * p, Gia_Obj_t * pObj )
{
    word Truth0, Truth1;
    if ( Gia_ObjIsCi(pObj) )
        return s_Truths6[Gia_ObjCioId(pObj)];
    if ( Gia_ObjIsConst0(pObj) )
        return 0;
    assert( Gia_ObjIsAnd(pObj) );
    Truth0 = Gia_LutComputeTruth66_rec( p, Gia_ObjFanin0(pObj) );
    Truth1 = Gia_LutComputeTruth66_rec( p, Gia_ObjFanin1(pObj) );
    if ( Gia_ObjFaninC0(pObj) )
        Truth0 = ~Truth0;
    if ( Gia_ObjFaninC1(pObj) )
        Truth1 = ~Truth1;
    return Truth0 & Truth1;
}
1313
int Gia_ManVerifyTwoTruths( Gia_Man_t * p1, Gia_Man_t * p2 )
1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328
{
    int i, fFailed = 0;
    assert( Gia_ManCoNum(p1) == Gia_ManCoNum(p2) );
    for ( i = 0; i < Gia_ManCoNum(p1); i++ )
    {
        Gia_Obj_t * pPo1 = Gia_ManCo(p1, i);
        Gia_Obj_t * pPo2 = Gia_ManCo(p2, i);
        word word1 = Gia_LutComputeTruth66_rec( p1, Gia_ObjFanin0(pPo1) );
        word word2 = Gia_LutComputeTruth66_rec( p2, Gia_ObjFanin0(pPo2) );
        if ( Gia_ObjFaninC0(pPo1) )
            word1 = ~word1;
        if ( Gia_ObjFaninC0(pPo2) )
            word2 = ~word2;
        if ( word1 != word2 )
        {
1329 1330
            //Dau_DsdPrintFromTruth( &word1, 6 );
            //Dau_DsdPrintFromTruth( &word2, 6 );
1331 1332 1333 1334
            printf( "Verification failed for output %d (out of %d).\n", i, Gia_ManCoNum(p1) );
            fFailed = 1;
        }
    }
1335 1336 1337
//    if ( !fFailed )
//        printf( "Verification succeeded for %d outputs.\n", Gia_ManCoNum(p1) );
    return !fFailed;
1338 1339 1340 1341 1342 1343
}



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

1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355
  Synopsis    [Enumerate windows of the nodes.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Gia_RsbEnumerateWindows( Gia_Man_t * p, int nInputsMax, int nLevelsMax )
{
    int fVerbose = 0;
1356
    int fUseHash = 0;
1357
    int i, nWins = 0, nWinSize = 0, nInsSize = 0, nOutSize = 0, nNodeGain = 0;
1358 1359
    Vec_Wec_t * vLevels = Vec_WecStart( Gia_ManLevelNum(p)+1 );
    Vec_Int_t * vPaths = Vec_IntStart( Gia_ManObjNum(p) );
1360
    Vec_Int_t * vRefs = Vec_IntStart( Gia_ManObjNum(p) );
1361
    Hsh_VecMan_t * pHash = Hsh_VecManStart( 1000 );
1362
    Gia_Obj_t * pObj;
1363
    Gia_Man_t * pIn, * pOut;
1364 1365 1366 1367
    abctime clk = Abc_Clock();
    Gia_ManStaticFanoutStart( p );
    Gia_ManForEachAnd( p, pObj, i )
    {
1368
        Vec_Int_t * vWin, * vIns, * vOuts;
1369 1370
        if ( !Gia_RsbWindowCompute( p, i, nInputsMax, nLevelsMax, vLevels, vPaths, &vWin, &vIns ) )
            continue;
1371
        vOuts = Gia_RsbFindOutputs( p, vWin, vIns, vRefs );
1372 1373 1374
        nWins++;
        nWinSize += Vec_IntSize(vWin);
        nInsSize += Vec_IntSize(vIns);
1375
        nOutSize += Vec_IntSize(vOuts);
1376 1377
   

1378 1379
        if ( fVerbose )
        {
1380
            printf( "\n\nObj %d\n", i );
1381 1382
            Vec_IntPrint( vWin );    
            Vec_IntPrint( vIns );    
1383
            Vec_IntPrint( vOuts );    
1384 1385
            printf( "\n" );
        }
1386 1387
        else if ( Vec_IntSize(vWin) > 1000 )
            printf( "Obj %d.   Window: Ins = %d. Ands = %d. Outs = %d.\n", 
1388 1389
                i, Vec_IntSize(vIns), Vec_IntSize(vWin)-Vec_IntSize(vIns), Vec_IntSize(vOuts) );

1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402
        if ( fUseHash )
        {
            int nEntries = Hsh_VecSize(pHash);
            Hsh_VecManAdd( pHash, vWin );
            if ( nEntries == Hsh_VecSize(pHash) )
            {
                Vec_IntFree( vWin );
                Vec_IntFree( vIns );
                Vec_IntFree( vOuts );
                continue;
            }
        }

1403 1404
        pIn = Gia_RsbDeriveGiaFromWindows( p, vWin, vIns, vOuts );
        pOut = Gia_ManResub2Test( pIn );
1405 1406 1407 1408 1409 1410 1411
        //pOut = Gia_ManDup( pIn );
        if ( !Gia_ManVerifyTwoTruths( pIn, pOut ) )
        {
            Gia_ManPrint( pIn );
            Gia_ManPrint( pOut );
            pOut = pOut;
        }
1412

1413
        nNodeGain += Gia_ManAndNum(pIn) - Gia_ManAndNum(pOut);
1414 1415 1416
        Gia_ManStop( pIn );
        Gia_ManStop( pOut );

1417 1418
        Vec_IntFree( vWin );
        Vec_IntFree( vIns );
1419
        Vec_IntFree( vOuts );
1420 1421 1422 1423
    }
    Gia_ManStaticFanoutStop( p );
    Vec_WecFree( vLevels );
    Vec_IntFree( vPaths );
1424
    Vec_IntFree( vRefs );
1425 1426 1427
    printf( "Computed windows for %d nodes (out of %d). Unique = %d. Ave inputs = %.2f. Ave outputs = %.2f. Ave volume = %.2f.  Gain = %d. ", 
        nWins, Gia_ManAndNum(p), Hsh_VecSize(pHash), 1.0*nInsSize/Abc_MaxInt(1,nWins), 
        1.0*nOutSize/Abc_MaxInt(1,nWins), 1.0*nWinSize/Abc_MaxInt(1,nWins), nNodeGain );
1428
    Abc_PrintTime( 1, "Time", Abc_Clock() - clk );
1429
    Hsh_VecManStop( pHash );
1430 1431
}

1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447
/**Function*************************************************************

  Synopsis    [Apply k-resub to one AIG.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Gia_Man_t * Gia_RsbTryOneWindow( Gia_Man_t * p )
{
    return Gia_ManResub2Test( p );
}

1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511
/**Function*************************************************************

  Synopsis    [Apply k-resub to one AIG.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Gia_RsbTestArray()
{
    int Array[1000] = { 
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 6, 3, 7, 15, 17, 8, 19, 
        5, 20, 5, 12, 8, 24, 4, 12, 9, 28, 27, 31, 23, 32, 4, 13, 8, 36, 5, 
        13, 18, 40, 9, 18, 5, 44, 19, 36, 9, 48, 47, 51, 10, 18, 40, 54, 8, 
        56, 25, 37, 44, 61, 59, 63, 8, 28, 8, 18, 25, 68, 66, 70, 64, 73, 11, 
        19, 8, 13, 76, 78, 10, 19, 40, 82, 9, 84, 81, 87, 20, 61, 19, 28, 30, 
        92, 91, 95, 88, 96, 74, 98, 9, 40, 49, 103, 27, 104, 10, 107, 8, 40, 
        9, 24, 111, 113, 11, 115, 109, 117, 11, 66, 51, 121, 118, 122, 18, 36, 
        18, 110, 93, 127, 10, 131, 129, 133, 11, 38, 32, 137, 103, 138, 19, 141, 
        134, 143, 28, 76, 9, 146, 11, 110, 19, 150, 149, 153, 87, 95, 9, 19, 10, 
        159, 61, 160, 18, 30, 61, 158, 9, 12, 25, 169, 19, 171, 111, 173, 10, 175, 
        167, 177, 18, 102, 4, 20, 18, 171, 183, 185, 11, 187, 181, 189, 178, 190, 
        24, 44, 11, 194, 8, 54, 4, 198, 197, 201, 45, 49, 10, 39, 9, 126, 73, 209, 
        11, 211, 54, 168, 213, 215, 43, 167, 67, 218, 10, 221, 26, 54, 18, 18, 34, 
        34, 38, 38, 40, 40, 42, 42, 52, 52, 100, 100, 124, 124, 126, 126, 144, 144, 
        148, 148, 154, 154, 156, 156, 162, 162, 164, 164, 192, 192, 70, 70, 202, 
        202, 204, 204, 206, 206, 216, 216, 222, 222, 224, 224
    };
    int i, iFan0, iFan1, nResubs;
    int * pRes;
    // create the internal array
    Vec_Int_t * vArray = Vec_IntAlloc( 100 );
    for ( i = 0; i < 50 || Array[i] > 0; i++ )
        Vec_IntPush( vArray, Array[i] );
    Vec_IntPrint( vArray );
    // check the nodes
    printf( "Constant0 and primary inputs:\n" );
    Vec_IntForEachEntryDouble( vArray, iFan0, iFan1, i )
    {
        if ( iFan0 != iFan1 )
            break;
        printf( "%2d = %c%2d & %c%2d;\n", i, 
            Abc_LitIsCompl(iFan0) ? '!' : ' ', Abc_Lit2Var(iFan0),
            Abc_LitIsCompl(iFan1) ? '!' : ' ', Abc_Lit2Var(iFan1) );
    }
    printf( "Primary outputs:\n" );
    Vec_IntForEachEntryDoubleStart( vArray, iFan0, iFan1, i, 14 )
    {
        if ( iFan0 != iFan1 )
            continue;
        printf( "%2d = %c%2d & %c%2d;\n", i, 
            Abc_LitIsCompl(iFan0) ? '!' : ' ', Abc_Lit2Var(iFan0),
            Abc_LitIsCompl(iFan1) ? '!' : ' ', Abc_Lit2Var(iFan1) );
    }
    // run the resub
    Abc_ResubPrepareManager( 1 );
    Abc_ResubComputeWindow( Vec_IntArray(vArray), Vec_IntSize(vArray)/2, 10, -1, 0, 0, 1, 1, &pRes, &nResubs );
    Abc_ResubPrepareManager( 0 );
    Vec_IntFree( vArray );
}

1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552
/**Function*************************************************************

  Synopsis    [Computing cuts of the nodes.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Vec_Wec_t * Gia_ManExtractCuts2( Gia_Man_t * p, int nCutSize, int nCuts, int fVerbose )
{
    int c, nLevelMax = 8;
    abctime clk = Abc_Clock();
    Vec_Wec_t * vCuts = Vec_WecStart( nCuts );
    Vec_Int_t * vPaths = Vec_IntStart( Gia_ManObjNum(p) );
    srand( time(NULL) );
    for ( c = 0; c < nCuts; )
    {
        Vec_Int_t * vCut, * vWin = NULL;
        while ( vWin == NULL )
        {
            int iPivot = 1 + Gia_ManCiNum(p) + rand() % Gia_ManAndNum(p);
            assert( Gia_ObjIsAnd(Gia_ManObj(p, iPivot)) );
            vWin = Gia_RsbWindowInit( p, vPaths, iPivot, nLevelMax );
        }
        vCut = Gia_RsbCreateWindowInputs( p, vWin );
        if ( Vec_IntSize(vCut) >= nCutSize - 2 && Vec_IntSize(vCut) <= nCutSize )
        {
            Vec_IntPush( Vec_WecEntry(vCuts, c), Vec_IntSize(vCut) );
            Vec_IntAppend( Vec_WecEntry(vCuts, c++), vCut );
        }
        Vec_IntFree( vCut );
        Vec_IntFree( vWin );
    }
    Vec_IntFree( vPaths );
    Abc_PrintTime( 0, "Computing cuts  ", Abc_Clock() - clk );
    return vCuts;
}

1553 1554 1555 1556 1557 1558
////////////////////////////////////////////////////////////////////////
///                       END OF FILE                                ///
////////////////////////////////////////////////////////////////////////

ABC_NAMESPACE_IMPL_END