acbMfs.c 60.9 KB
Newer Older
1 2
/**CFile****************************************************************

3
  FileName    [abcMfs.c]
4 5 6 7 8

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [Hierarchical word-level netlist.]

9
  Synopsis    [Optimization with don't-cares.]
10 11 12 13 14 15 16

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

  Date        [Ver. 1.0. Started - July 21, 2015.]

17
  Revision    [$Id: abcMfs.c,v 1.00 2014/11/29 00:00:00 alanmi Exp $]
18 19 20 21

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

#include "acb.h"
22 23
#include "bool/kit/kit.h"
#include "sat/bsat/satSolver.h"
24 25 26
#include "sat/cnf/cnf.h"
#include "misc/util/utilTruth.h"
#include "acbPar.h"
27 28 29 30 31 32 33

ABC_NAMESPACE_IMPL_START

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

34 35 36
static inline int Acb_ObjIsDelayCriticalFanin( Acb_Ntk_t * p, int i, int f )  { return !Acb_ObjIsCi(p, f) && Acb_ObjLevelR(p, i) + Acb_ObjLevelD(p, f) == p->LevelMax; }
static inline int Acb_ObjIsAreaCritical( Acb_Ntk_t * p, int f )               { return !Acb_ObjIsCi(p, f) && Acb_ObjFanoutNum(p, f) == 1;                              }
static inline int Acb_ObjIsCritical( Acb_Ntk_t * p, int i, int f, int fDel )  { return fDel ? Acb_ObjIsDelayCriticalFanin(p, i, f) : Acb_ObjIsAreaCritical(p, f);      }
37

38 39 40 41 42 43
////////////////////////////////////////////////////////////////////////
///                     FUNCTION DEFINITIONS                         ///
////////////////////////////////////////////////////////////////////////

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

44
  Synopsis    [Derive CNF for nodes in the window.]
45 46 47 48 49 50 51 52

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
53
int Acb_DeriveCnfFromTruth( word Truth, int nVars, Vec_Int_t * vCover, Vec_Str_t * vCnf )
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
{
    Vec_StrClear( vCnf );
    if ( Truth == 0 || ~Truth == 0 )
    {
//        assert( nVars == 0 );
        Vec_StrPush( vCnf, (char)(Truth == 0) );
        Vec_StrPush( vCnf, (char)-1 );
        return 1;
    }
    else 
    {
        int i, k, c, RetValue, Literal, Cube, nCubes = 0;
        assert( nVars > 0 );
        for ( c = 0; c < 2; c ++ )
        {
            Truth = c ? ~Truth : Truth;
            RetValue = Kit_TruthIsop( (unsigned *)&Truth, nVars, vCover, 0 );
            assert( RetValue == 0 );
            nCubes += Vec_IntSize( vCover );
            Vec_IntForEachEntry( vCover, Cube, i )
            {
                for ( k = 0; k < nVars; k++ )
                {
                    Literal = 3 & (Cube >> (k << 1));
                    if ( Literal == 1 )      // '0'  -> pos lit
                        Vec_StrPush( vCnf, (char)Abc_Var2Lit(k, 0) );
                    else if ( Literal == 2 ) // '1'  -> neg lit
                        Vec_StrPush( vCnf, (char)Abc_Var2Lit(k, 1) );
                    else if ( Literal != 0 )
                        assert( 0 );
                }
                Vec_StrPush( vCnf, (char)Abc_Var2Lit(nVars, c) );
                Vec_StrPush( vCnf, (char)-1 );
            }
        }
        return nCubes;
    }
}
92 93 94 95 96 97 98 99 100

void Acb_DeriveCnfForWindowOne( Acb_Ntk_t * p, int iObj )
{
    Vec_Wec_t * vCnfs = &p->vCnfs;
    Vec_Str_t * vCnfBase = Acb_ObjCnfs( p, iObj );
    assert( Vec_StrSize(vCnfBase) == 0 ); // unassigned
    assert( Vec_WecSize(vCnfs) == Acb_NtkObjNumMax(p) );
    Acb_DeriveCnfFromTruth( Acb_ObjTruth(p, iObj), Acb_ObjFaninNum(p, iObj), &p->vCover, &p->vCnf );
    Vec_StrGrow( vCnfBase, Vec_StrSize(&p->vCnf) );
101
    memcpy( Vec_StrArray(vCnfBase), Vec_StrArray(&p->vCnf), (size_t)Vec_StrSize(&p->vCnf) );
102 103
    vCnfBase->nSize = Vec_StrSize(&p->vCnf);
}
104
Vec_Wec_t * Acb_DeriveCnfForWindow( Acb_Ntk_t * p, Vec_Int_t * vWin, int PivotVar )
105
{
106
    Vec_Wec_t * vCnfs = &p->vCnfs;
107
    Vec_Str_t * vCnfBase; int i, iObj;
108 109
    assert( Vec_WecSize(vCnfs) == Acb_NtkObjNumMax(p) );
    Vec_IntForEachEntry( vWin, iObj, i )
110
    {
111 112
        if ( Abc_LitIsCompl(iObj) && i < PivotVar )
            continue;
113 114
        iObj = Abc_Lit2Var(iObj);
        vCnfBase = Acb_ObjCnfs( p, iObj );
115
        if ( Vec_StrSize(vCnfBase) > 0 )
116
            continue;
117
        Acb_DeriveCnfForWindowOne( p, iObj );
118 119 120 121 122 123
    }
    return vCnfs;
}

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

124
  Synopsis    [Constructs CNF for the window.]
125 126 127 128 129 130 131 132 133 134 135 136 137 138

  Description [The window for the pivot node is represented as a DFS ordered array 
  of objects (vWinObjs) whose indexes are used as SAT variable IDs (stored in p->vCopies).
  PivotVar is the index of the pivot node in array vWinObjs.
  The nodes before (after) PivotVar are TFI (TFO) nodes.
  The leaf (root) nodes are labeled with Abc_LitIsCompl().
  If fQbf is 1, returns the instance meant for QBF solving. It uses the last 
  variable (LastVar) as the placeholder for the second copy of the pivot node.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
void Acb_TranslateCnf( Vec_Int_t * vClas, Vec_Int_t * vLits, Vec_Str_t * vCnf, Vec_Int_t * vSatVars, int iPivotVar )
{
    signed char Entry;
    int i, Lit;
    Vec_StrForEachEntry( vCnf, Entry, i )
    {
        if ( (int)Entry == -1 )
        {
            Vec_IntPush( vClas, Vec_IntSize(vLits) );
            continue;
        }
        Lit = Abc_Lit2LitV( Vec_IntArray(vSatVars), (int)Entry );
        Lit = Abc_LitNotCond( Lit, Abc_Lit2Var(Lit) == iPivotVar );
        Vec_IntPush( vLits, Lit );
    }
}
155 156 157 158 159 160 161
int Acb_NtkCountRoots( Vec_Int_t * vWinObjs, int PivotVar )
{
    int i, iObjLit, nRoots = 0;
    Vec_IntForEachEntryStart( vWinObjs, iObjLit, i, PivotVar + 1 )
        nRoots += Abc_LitIsCompl(iObjLit);
    return nRoots;
}
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
void Acb_DeriveCnfForNode( Acb_Ntk_t * p, int iObj, sat_solver * pSat, int OutVar )
{
    Vec_Wec_t * vCnfs = &p->vCnfs;
    Vec_Int_t * vFaninVars = &p->vCover;
    Vec_Int_t * vClas = Vec_IntAlloc( 100 );
    Vec_Int_t * vLits = Vec_IntAlloc( 100 );
    int k, iFanin, * pFanins, Prev, This;
    // collect SAT variables
    Vec_IntClear( vFaninVars );
    Acb_ObjForEachFaninFast( p, iObj, pFanins, iFanin, k )
    {
        assert( Acb_ObjFunc(p, iFanin) >= 0 );
        Vec_IntPush( vFaninVars, Acb_ObjFunc(p, iFanin) );
    }
    Vec_IntPush( vFaninVars, OutVar );
    // derive CNF for the node
    Acb_TranslateCnf( vClas, vLits, (Vec_Str_t *)Vec_WecEntry(vCnfs, iObj), vFaninVars, -1 );
    // add clauses
    Prev = 0;
    Vec_IntForEachEntry( vClas, This, k )
    {
        if ( !sat_solver_addclause( pSat, Vec_IntArray(vLits) + Prev, Vec_IntArray(vLits) + This ) )
            printf( "Error: SAT solver became UNSAT at a wrong place (while adding new CNF).\n" );
        Prev = This;
    }
    Vec_IntFree( vClas );
    Vec_IntFree( vLits );
}
190
Cnf_Dat_t * Acb_NtkWindow2Cnf( Acb_Ntk_t * p, Vec_Int_t * vWinObjs, int Pivot )
191
{
192
    Cnf_Dat_t * pCnf;
193 194 195 196 197
    Vec_Int_t * vFaninVars = Vec_IntAlloc( 8 );
    int PivotVar = Vec_IntFind(vWinObjs, Abc_Var2Lit(Pivot, 0));
    int nRoots   = Acb_NtkCountRoots(vWinObjs, PivotVar);
    int TfoStart = PivotVar + 1;
    int nTfoSize = Vec_IntSize(vWinObjs) - TfoStart;
198 199 200 201 202
    int nVarsAll = Vec_IntSize(vWinObjs) + nTfoSize + nRoots;
    int i, k, iObj, iObjLit, iFanin, * pFanins, Entry;
    Vec_Wec_t * vCnfs = Acb_DeriveCnfForWindow( p, vWinObjs, PivotVar );
    Vec_Int_t * vClas = Vec_IntAlloc( 100 );
    Vec_Int_t * vLits = Vec_IntAlloc( 1000 );
203 204
    // mark new SAT variables
    Vec_IntForEachEntry( vWinObjs, iObj, i )
205
        Acb_ObjSetFunc( p, Abc_Lit2Var(iObj), i );
206
    // add clauses for all nodes
207 208
    Vec_IntPush( vClas, Vec_IntSize(vLits) );
    Vec_IntForEachEntry( vWinObjs, iObjLit, i )
209
    {
210
        if ( Abc_LitIsCompl(iObjLit) && i < PivotVar )
211 212 213 214 215 216
            continue;
        iObj = Abc_Lit2Var(iObjLit);
        assert( !Acb_ObjIsCio(p, iObj) );
        // collect SAT variables
        Vec_IntClear( vFaninVars );
        Acb_ObjForEachFaninFast( p, iObj, pFanins, iFanin, k )
217 218
            Vec_IntPush( vFaninVars, Acb_ObjFunc(p, iFanin) );
        Vec_IntPush( vFaninVars, Acb_ObjFunc(p, iObj) );
219
        // derive CNF for the node
220
        Acb_TranslateCnf( vClas, vLits, (Vec_Str_t *)Vec_WecEntry(vCnfs, iObj), vFaninVars, -1 );
221 222 223 224 225 226 227 228 229
    }
    // add second clauses for the TFO
    Vec_IntForEachEntryStart( vWinObjs, iObjLit, i, TfoStart )
    {
        iObj = Abc_Lit2Var(iObjLit);
        assert( !Acb_ObjIsCio(p, iObj) );
        // collect SAT variables
        Vec_IntClear( vFaninVars );
        Acb_ObjForEachFaninFast( p, iObj, pFanins, iFanin, k )
230 231
            Vec_IntPush( vFaninVars, Acb_ObjFunc(p, iFanin) + (Acb_ObjFunc(p, iFanin) > PivotVar) * nTfoSize );
        Vec_IntPush( vFaninVars, Acb_ObjFunc(p, iObj) + nTfoSize );
232
        // derive CNF for the node
233
        Acb_TranslateCnf( vClas, vLits, (Vec_Str_t *)Vec_WecEntry(vCnfs, iObj), vFaninVars, PivotVar );
234 235 236 237 238 239 240 241 242 243
    }
    if ( nRoots > 0 )
    {
        // create XOR clauses for the roots
        int nVars = Vec_IntSize(vWinObjs) + nTfoSize;
        Vec_IntClear( vFaninVars );
        Vec_IntForEachEntryStart( vWinObjs, iObjLit, i, TfoStart )
        {
            if ( !Abc_LitIsCompl(iObjLit) )
                continue;
244 245
            iObj = Abc_Lit2Var(iObjLit);
            // add clauses
246
            Vec_IntPushThree( vLits, Abc_Var2Lit(Acb_ObjFunc(p, iObj), 1), Abc_Var2Lit(Acb_ObjFunc(p, iObj) + nTfoSize, 0), Abc_Var2Lit(nVars, 0) );
247
            Vec_IntPush( vClas, Vec_IntSize(vLits) );
248
            Vec_IntPushThree( vLits, Abc_Var2Lit(Acb_ObjFunc(p, iObj), 0), Abc_Var2Lit(Acb_ObjFunc(p, iObj) + nTfoSize, 1), Abc_Var2Lit(nVars, 0) );
249
            Vec_IntPush( vClas, Vec_IntSize(vLits) );
250
            Vec_IntPushThree( vLits, Abc_Var2Lit(Acb_ObjFunc(p, iObj), 0), Abc_Var2Lit(Acb_ObjFunc(p, iObj) + nTfoSize, 0), Abc_Var2Lit(nVars, 1) );
251
            Vec_IntPush( vClas, Vec_IntSize(vLits) );
252
            Vec_IntPushThree( vLits, Abc_Var2Lit(Acb_ObjFunc(p, iObj), 1), Abc_Var2Lit(Acb_ObjFunc(p, iObj) + nTfoSize, 1), Abc_Var2Lit(nVars, 1) );
253 254
            Vec_IntPush( vClas, Vec_IntSize(vLits) );
            Vec_IntPush( vFaninVars, Abc_Var2Lit(nVars++, 0) );
255
        }
256 257 258 259
        Vec_IntAppend( vLits, vFaninVars );
        Vec_IntPush( vClas, Vec_IntSize(vLits) );
        assert( nRoots == Vec_IntSize(vFaninVars) );
        assert( nVars == nVarsAll );
260 261
    }
    Vec_IntFree( vFaninVars );
262 263 264 265 266 267 268 269 270 271 272 273
    // create CNF structure
    pCnf = ABC_CALLOC( Cnf_Dat_t, 1 );
    pCnf->nVars     = nVarsAll;
    pCnf->nClauses  = Vec_IntSize(vClas)-1;
    pCnf->nLiterals = Vec_IntSize(vLits);
    pCnf->pClauses  = ABC_ALLOC( int *, Vec_IntSize(vClas) );
    pCnf->pClauses[0] = Vec_IntReleaseArray(vLits);
    Vec_IntForEachEntry( vClas, Entry, i )
        pCnf->pClauses[i] = pCnf->pClauses[0] + Entry;
    // cleanup
    Vec_IntFree( vClas );
    Vec_IntFree( vLits );
274
    //Cnf_DataPrint( pCnf, 1 );
275
    return pCnf;
276
}
277 278 279 280 281 282 283 284 285
void Acb_NtkWindowUndo( Acb_Ntk_t * p, Vec_Int_t * vWin )
{
    int i, iObj;
    Vec_IntForEachEntry( vWin, iObj, i )
    {
        assert( Vec_IntEntry(&p->vObjFunc, Abc_Lit2Var(iObj)) != -1 );
        Vec_IntWriteEntry( &p->vObjFunc, Abc_Lit2Var(iObj), -1 );
    }
}
286 287 288

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

289
  Synopsis    [Creates SAT solver containing several copies of the window.]
290 291 292 293 294 295 296 297

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
298
int Acb_NtkWindow2Solver( sat_solver * pSat, Cnf_Dat_t * pCnf, Vec_Int_t * vFlip, int PivotVar, int nDivs, int nTimes )
299
{
300 301 302 303
    int n, i, RetValue, Test = pCnf->pClauses[0][0];
    int nGroups = nTimes <= 2 ? nTimes-1 : 2;
    int nRounds = nTimes <= 2 ? nTimes-1 : nTimes;
    assert( sat_solver_nvars(pSat) == 0 );
304
    sat_solver_setnvars( pSat, nTimes * pCnf->nVars + nGroups * nDivs + 2 );
305 306
    assert( nTimes == 1 || nTimes == 2 || nTimes == 6 );
    for ( n = 0; n < nTimes; n++ )
307
    {
308
        if ( n & 1 )
309
            Cnf_DataLiftAndFlipLits( pCnf, -pCnf->nVars, vFlip );
310 311
        for ( i = 0; i < pCnf->nClauses; i++ )
            if ( !sat_solver_addclause( pSat, pCnf->pClauses[i], pCnf->pClauses[i+1] ) )
312
                printf( "Error: SAT solver became UNSAT at a wrong place.\n" );
313
        if ( n & 1 )
314
            Cnf_DataLiftAndFlipLits( pCnf, pCnf->nVars, vFlip );
315 316 317 318
        if ( n < nTimes - 1 )
            Cnf_DataLift( pCnf, pCnf->nVars );
        else if ( n ) // if ( n == nTimes - 1 )
            Cnf_DataLift( pCnf, -(nTimes - 1) * pCnf->nVars );
319
    }
320
    assert( Test == pCnf->pClauses[0][0] );
321 322
    // add conditional buffers
    for ( n = 0; n < nRounds; n++ )
323
    {
324 325
        int BaseA = n * pCnf->nVars;
        int BaseB = ((n + 1) % nTimes) * pCnf->nVars;
326
        int BaseC = nTimes * pCnf->nVars + (n & 1) * nDivs;
327 328
        for ( i = 0; i < nDivs; i++ )
            sat_solver_add_buffer_enable( pSat, BaseA + i, BaseB + i, BaseC + i, 0 );
329
    }
330 331
    // finalize
    RetValue = sat_solver_simplify( pSat );
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
    if ( !RetValue ) printf( "Error: SAT solver became UNSAT at a wrong place.\n" );
    return 1;
}

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

  Synopsis    [Computes function of the node]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
347
word Acb_ComputeFunction( sat_solver * pSat, int PivotVar, int FreeVar, Vec_Int_t * vDivVars, int fCompl )
348 349 350 351 352 353
{
    int fExpand = 0;
    word uCube, uTruth = 0;
    Vec_Int_t * vTempLits = Vec_IntAlloc( 100 );
    int status, i, iVar, iLit, nFinal, * pFinal, pLits[2];
    assert( FreeVar < sat_solver_nvars(pSat) );
354 355 356 357
//    if ( fCompl )
//        pLits[0] = Abc_Var2Lit( sat_solver_nvars(pSat)-2, 0 ); // F = 1
//    else
        pLits[0] = Abc_Var2Lit( PivotVar, fCompl ); // F = 1
358 359
    pLits[1] = Abc_Var2Lit( FreeVar, 0 );  // iNewLit
    while ( 1 ) 
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
        // find onset minterm
        status = sat_solver_solve( pSat, pLits, pLits + 2, 0, 0, 0, 0 );
        if ( status == l_False )
        {
            Vec_IntFree( vTempLits );
            return uTruth;
        }
        assert( status == l_True );
        if ( fExpand )
        {
            // collect divisor literals
            Vec_IntFill( vTempLits, 1, Abc_LitNot(pLits[0]) ); // F = 0
            Vec_IntForEachEntry( vDivVars, iVar, i )
                Vec_IntPush( vTempLits, sat_solver_var_literal(pSat, iVar) );
            // check against offset
            status = sat_solver_solve( pSat, Vec_IntArray(vTempLits), Vec_IntLimit(vTempLits), 0, 0, 0, 0 );
            if ( status != l_False )
                printf( "Failed internal check during function comptutation.\n" );
            assert( status == l_False );
            // compute cube and add clause
            nFinal = sat_solver_final( pSat, &pFinal );
            Vec_IntFill( vTempLits, 1, Abc_LitNot(pLits[1]) ); // NOT(iNewLit)
            for ( i = 0; i < nFinal; i++ )
                if ( pFinal[i] != pLits[0] )
                    Vec_IntPush( vTempLits, pFinal[i] );
        }
        else
        {
            // collect divisor literals
            Vec_IntFill( vTempLits, 1, Abc_LitNot(pLits[1]) );// NOT(iNewLit)
            Vec_IntForEachEntry( vDivVars, iVar, i )
                Vec_IntPush( vTempLits, Abc_LitNot(sat_solver_var_literal(pSat, iVar)) );
        }
        uCube = ~(word)0;
        Vec_IntForEachEntryStart( vTempLits, iLit, i, 1 )
        {
            iVar = Vec_IntFind( vDivVars, Abc_Lit2Var(iLit) );   assert( iVar >= 0 );
            uCube &= Abc_LitIsCompl(iLit) ? s_Truths6[iVar] : ~s_Truths6[iVar];
        }
        uTruth |= uCube;
        status = sat_solver_addclause( pSat, Vec_IntArray(vTempLits), Vec_IntLimit(vTempLits) );
        if ( status == 0 )
        {
            Vec_IntFree( vTempLits );
            return uTruth;
        }
407
    }
408
    Vec_IntFree( vTempLits );
409 410
    assert( 0 ); 
    return ~(word)0;
411 412
}

413

414 415


416

417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
/**Function*************************************************************

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Acb_NtkPrintVec( Acb_Ntk_t * p, Vec_Int_t * vVec, char * pName )
{
    int i;
    printf( "%s: ", pName );
    for ( i = 0; i < vVec->nSize; i++ )
433
        printf( "%d ", vVec->pArray[i] );
434 435
    printf( "\n" );
}
436 437 438 439 440 441 442 443
void Acb_NtkPrintVec2( Acb_Ntk_t * p, Vec_Int_t * vVec, char * pName )
{
    int i;
    printf( "%s: \n", pName );
    for ( i = 0; i < vVec->nSize; i++ )
        Acb_NtkPrintNode( p, vVec->pArray[i] );
    printf( "\n" );
}
444 445 446 447 448 449 450 451
void Acb_NtkPrintVecWin( Acb_Ntk_t * p, Vec_Int_t * vVec, char * pName )
{
    int i;
    printf( "%s: \n", pName );
    for ( i = 0; i < vVec->nSize; i++ )
        Acb_NtkPrintNode( p, Abc_Lit2Var(vVec->pArray[i]) );
    printf( "\n" );
}
452 453 454 455 456 457 458 459 460 461 462 463

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

  Synopsis    [Collects divisors in a non-topo order.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
464 465 466 467 468 469 470 471 472 473 474 475 476
void Acb_NtkDivisors_rec( Acb_Ntk_t * p, int iObj, int nTfiLevMin, Vec_Int_t * vDivs )
{
    int k, iFanin, * pFanins;
//    if ( !Acb_ObjIsCi(p, iObj) && Acb_ObjLevelD(p, iObj) < nTfiLevMin )
    if ( !Acb_ObjIsCi(p, iObj) && nTfiLevMin < 0 )
        return;
    if ( Acb_ObjSetTravIdCur(p, iObj) )
        return;
    Acb_ObjForEachFaninFast( p, iObj, pFanins, iFanin, k )
        Acb_NtkDivisors_rec( p, iFanin, nTfiLevMin-1, vDivs );
    Vec_IntPush( vDivs, iObj );
}
Vec_Int_t * Acb_NtkDivisors( Acb_Ntk_t * p, int Pivot, int nTfiLevMin, int fDelay )
477
{
478
    int k, iFanin, * pFanins;
479 480
    Vec_Int_t * vDivs = Vec_IntAlloc( 100 );
    Acb_NtkIncTravId( p );
481 482
//    if ( fDelay ) // delay-oriented
    if ( 0 ) // delay-oriented
483
    {
484 485 486
        // start from critical fanins
        assert( Acb_ObjLevelD( p, Pivot ) > 1 );
        Acb_ObjForEachFaninFast( p, Pivot, pFanins, iFanin, k )
487
            if ( Acb_ObjIsDelayCriticalFanin( p, Pivot, iFanin ) )
488 489 490
                Acb_NtkDivisors_rec( p, iFanin, nTfiLevMin, vDivs );
        // add non-critical fanins
        Acb_ObjForEachFaninFast( p, Pivot, pFanins, iFanin, k )
491
            if ( !Acb_ObjIsDelayCriticalFanin( p, Pivot, iFanin ) )
492 493
                if ( !Acb_ObjSetTravIdCur(p, iFanin) )
                    Vec_IntPush( vDivs, iFanin );
494
    }
495
    else
496
    {
497 498 499 500 501 502
        Acb_NtkDivisors_rec( p, Pivot, nTfiLevMin, vDivs );
        assert( Vec_IntEntryLast(vDivs) == Pivot );
        Vec_IntPop( vDivs );
        // add remaining fanins of the node
        Acb_ObjForEachFaninFast( p, Pivot, pFanins, iFanin, k )
            if ( !Acb_ObjSetTravIdCur(p, iFanin) )
503
                Vec_IntPush( vDivs, iFanin );
504 505 506 507 508 509 510 511 512 513 514 515
/*
        // start from critical fanins
        assert( Acb_ObjLevelD( p, Pivot ) > 1 );
        Acb_ObjForEachFaninFast( p, Pivot, pFanins, iFanin, k )
            if ( Acb_ObjIsAreaCritical( p, iFanin ) )
                Acb_NtkDivisors_rec( p, iFanin, nTfiLevMin, vDivs );
        // add non-critical fanins
        Acb_ObjForEachFaninFast( p, Pivot, pFanins, iFanin, k )
            if ( !Acb_ObjIsAreaCritical( p, iFanin ) )
                if ( !Acb_ObjSetTravIdCur(p, iFanin) )
                    Vec_IntPush( vDivs, iFanin );
*/
516 517 518 519
    }
    return vDivs;
}

520 521 522 523 524 525 526 527 528 529 530
/**Function*************************************************************

  Synopsis    [Marks TFO of divisors.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
531
void Acb_ObjMarkTfo_rec( Acb_Ntk_t * p, int iObj, int nTfoLevMax, int nFanMax, Vec_Int_t * vMarked )
532 533 534 535
{
    int iFanout, i;
    if ( Acb_ObjSetTravIdCur(p, iObj) )
        return;
536
    Vec_IntPush( vMarked, iObj );
537
    if ( Acb_ObjLevelD(p, iObj) > nTfoLevMax || Acb_ObjFanoutNum(p, iObj) > nFanMax )
538 539
        return;
    Acb_ObjForEachFanout( p, iObj, iFanout, i )
540
        Acb_ObjMarkTfo_rec( p, iFanout, nTfoLevMax, nFanMax, vMarked );
541
}
542
Vec_Int_t * Acb_ObjMarkTfo( Acb_Ntk_t * p, Vec_Int_t * vDivs, int Pivot, int nTfoLevMax, int nFanMax )
543
{
544
    Vec_Int_t * vMarked = Vec_IntAlloc( 1000 );
545 546
    int i, iObj;
    Acb_NtkIncTravId( p );
547
    Acb_ObjSetTravIdCur( p, Pivot );
548
    Vec_IntPush( vMarked, Pivot );
549
    Vec_IntForEachEntry( vDivs, iObj, i )
550 551 552 553 554 555 556 557 558
        Acb_ObjMarkTfo_rec( p, iObj, nTfoLevMax, nFanMax, vMarked );
    return vMarked;
}
void Acb_ObjMarkTfo2( Acb_Ntk_t * p, Vec_Int_t * vMarked )
{
    int i, Node;
    Acb_NtkIncTravId( p );
    Vec_IntForEachEntry( vMarked, Node, i )
        Acb_ObjSetTravIdCur( p, Node );
559 560 561 562 563 564 565 566 567 568 569 570 571
}

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

  Synopsis    [Labels TFO nodes with {none, root, inner} based on their type.]

  Description [Assuming TFO of TFI is marked with the current trav ID.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
572
int Acb_ObjLabelTfo_rec( Acb_Ntk_t * p, int iObj, int nTfoLevMax, int nFanMax, int fFirst )
573 574 575 576 577 578 579
{
    int iFanout, i, Diff, fHasNone = 0;
    if ( (Diff = Acb_ObjTravIdDiff(p, iObj)) <= 2 )
        return Diff;
    Acb_ObjSetTravIdDiff( p, iObj, 2 );
    if ( Acb_ObjIsCo(p, iObj) || Acb_ObjLevelD(p, iObj) > nTfoLevMax )
        return 2;
580
    if ( Acb_ObjLevelD(p, iObj) == nTfoLevMax || Acb_ObjFanoutNum(p, iObj) > nFanMax )
581
    {
582 583
        if ( Diff == 3 )  // belongs to TFO of TFI
            Acb_ObjSetTravIdDiff( p, iObj, 1 ); // root
584 585 586
        return Acb_ObjTravIdDiff(p, iObj);
    }
    Acb_ObjForEachFanout( p, iObj, iFanout, i )
587
        if ( !fFirst || Acb_ObjIsDelayCriticalFanin(p, iFanout, iObj) )
588
            fHasNone |= 2 == Acb_ObjLabelTfo_rec( p, iFanout, nTfoLevMax, nFanMax, 0 );
589
    if ( fHasNone && Diff == 3 )  // belongs to TFO of TFI
590
        Acb_ObjSetTravIdDiff( p, iObj, 1 ); // root
591
    else if ( !fHasNone )
592 593 594
        Acb_ObjSetTravIdDiff( p, iObj, 0 ); // inner
    return Acb_ObjTravIdDiff(p, iObj);
}
595
int Acb_ObjLabelTfo( Acb_Ntk_t * p, int Root, int nTfoLevMax, int nFanMax, int fDelay )
596 597 598 599
{
    Acb_NtkIncTravId( p ); // none  (2)    marked (3)  unmarked (4)
    Acb_NtkIncTravId( p ); // root  (1)
    Acb_NtkIncTravId( p ); // inner (0)
600
    assert( Acb_ObjTravIdDiff(p, Root) > 2 );
601
    return Acb_ObjLabelTfo_rec( p, Root, nTfoLevMax, nFanMax, fDelay );
602 603 604 605 606 607 608 609 610 611 612 613 614
}

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

  Synopsis    [Collects labeled TFO.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
615
void Acb_ObjDeriveTfo_rec( Acb_Ntk_t * p, int iObj, Vec_Int_t * vTfo, Vec_Int_t * vRoots, int fFirst )
616 617 618 619 620 621
{
    int iFanout, i, Diff = Acb_ObjTravIdDiff(p, iObj);
    if ( Acb_ObjSetTravIdCur(p, iObj) )
        return;
    if ( Diff == 2 ) // root
    {
622 623
        Vec_IntPush( vRoots, iObj );
        Vec_IntPush( vTfo, iObj );
624 625 626 627
        return;
    }
    assert( Diff == 1 );
    Acb_ObjForEachFanout( p, iObj, iFanout, i )
628
        if ( !fFirst || Acb_ObjIsDelayCriticalFanin(p, iFanout, iObj) )
629
            Acb_ObjDeriveTfo_rec( p, iFanout, vTfo, vRoots, 0 );
630
    Vec_IntPush( vTfo, iObj );
631
}
632
void Acb_ObjDeriveTfo( Acb_Ntk_t * p, int Pivot, int nTfoLevMax, int nFanMax, Vec_Int_t ** pvTfo, Vec_Int_t ** pvRoots, int fDelay )
633
{
634
    int Res = Acb_ObjLabelTfo( p, Pivot, nTfoLevMax, nFanMax, fDelay );
635 636
    Vec_Int_t * vTfo   = *pvTfo   = Vec_IntAlloc( 10 );
    Vec_Int_t * vRoots = *pvRoots = Vec_IntAlloc( 10 );
637 638 639
    if ( Res ) // none or root
        return;
    Acb_NtkIncTravId( p ); // root (2)   inner (1)  visited (0)
640
    Acb_ObjDeriveTfo_rec( p, Pivot, vTfo, vRoots, fDelay );
641
    assert( Vec_IntEntryLast(vTfo) == Pivot );
642
    Vec_IntPop( vTfo );
643
    assert( Vec_IntEntryLast(vRoots) != Pivot );
644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664
    Vec_IntReverseOrder( vTfo );
    Vec_IntReverseOrder( vRoots );
}


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

  Synopsis    [Collect side-inputs of the TFO, except the node.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Vec_Int_t * Acb_NtkCollectTfoSideInputs( Acb_Ntk_t * p, int Pivot, Vec_Int_t * vTfo )
{
    Vec_Int_t * vSide  = Vec_IntAlloc( 100 );
    int i, k, Node, iFanin, * pFanins;
    Acb_NtkIncTravId( p );
665
    Vec_IntPush( vTfo, Pivot );
666
    Vec_IntForEachEntry( vTfo, Node, i )
667
        Acb_ObjSetTravIdCur( p, Node );
668 669 670 671
    Vec_IntForEachEntry( vTfo, Node, i )
        Acb_ObjForEachFaninFast( p, Node, pFanins, iFanin, k )
            if ( !Acb_ObjSetTravIdCur(p, iFanin) && iFanin != Pivot )
                Vec_IntPush( vSide, iFanin );
672
    Vec_IntPop( vTfo );
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
    return vSide;
}

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

  Synopsis    [From side inputs, collect marked nodes and their unmarked fanins.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Acb_NtkCollectNewTfi1_rec( Acb_Ntk_t * p, int iObj, Vec_Int_t * vTfiNew )
{
    int i, iFanin, * pFanins;
    if ( !Acb_ObjIsTravIdPrev(p, iObj) )
        return;
    if ( Acb_ObjSetTravIdCur(p, iObj) )
        return;
    Acb_ObjForEachFaninFast( p, iObj, pFanins, iFanin, i )
        Acb_NtkCollectNewTfi1_rec( p, iFanin, vTfiNew );
    Vec_IntPush( vTfiNew, iObj );
}
void Acb_NtkCollectNewTfi2_rec( Acb_Ntk_t * p, int iObj, Vec_Int_t * vTfiNew )
{
    int i, iFanin, * pFanins;
    int fTravIdPrev = Acb_ObjIsTravIdPrev(p, iObj);
    if ( Acb_ObjSetTravIdCur(p, iObj) )
        return;
    if ( fTravIdPrev && !Acb_ObjIsCi(p, iObj) )
        Acb_ObjForEachFaninFast( p, iObj, pFanins, iFanin, i )
            Acb_NtkCollectNewTfi2_rec( p, iFanin, vTfiNew );
    Vec_IntPush( vTfiNew, iObj );
}
709
Vec_Int_t * Acb_NtkCollectNewTfi( Acb_Ntk_t * p, int Pivot, Vec_Int_t * vDivs, Vec_Int_t * vSide, int * pnDivs )
710 711 712 713
{
    Vec_Int_t * vTfiNew  = Vec_IntAlloc( 100 );
    int i, Node;
    Acb_NtkIncTravId( p );
714
    //Acb_NtkPrintVec( p, vDivs, "vDivs" );
715 716
    Vec_IntForEachEntry( vDivs, Node, i )
        Acb_NtkCollectNewTfi1_rec( p, Node, vTfiNew );
717
//Acb_NtkPrintVec( p, vTfiNew, "vTfiNew" );
718
    Acb_NtkCollectNewTfi1_rec( p, Pivot, vTfiNew );
719
//Acb_NtkPrintVec( p, vTfiNew, "vTfiNew" );
720 721
    assert( Vec_IntEntryLast(vTfiNew) == Pivot );
    Vec_IntPop( vTfiNew );
722 723 724 725 726 727 728 729
/*
    Vec_IntForEachEntry( vDivs, Node, i )
    {
        Acb_ObjSetTravIdCur( p, Node );
        Vec_IntPush( vTfiNew, Node );
    }
*/
    *pnDivs = Vec_IntSize(vTfiNew);
730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750
    Vec_IntForEachEntry( vSide, Node, i )
        Acb_NtkCollectNewTfi2_rec( p, Node, vTfiNew );
    Vec_IntPush( vTfiNew, Pivot );
    return vTfiNew;
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Vec_Int_t * Acb_NtkCollectWindow( Acb_Ntk_t * p, int Pivot, Vec_Int_t * vTfi, Vec_Int_t * vTfo, Vec_Int_t * vRoots )
{
    Vec_Int_t * vWin = Vec_IntAlloc( 100 );
    int i, k, iObj, iFanin, * pFanins;
751
    assert( Vec_IntEntryLast(vTfi) == Pivot );
752
    // mark nodes
753 754 755 756 757
    Acb_NtkIncTravId( p );
    Vec_IntForEachEntry( vTfi, iObj, i )
        Acb_ObjSetTravIdCur(p, iObj);
    // add TFI
    Vec_IntForEachEntry( vTfi, iObj, i )
758 759 760 761 762 763 764
    {
        int fIsTfiInput = 0;
        Acb_ObjForEachFaninFast( p, iObj, pFanins, iFanin, k )
            if ( !Acb_ObjIsTravIdCur(p, iFanin) ) // fanin is not in TFI
                fIsTfiInput = 1; // mark as leaf
        Vec_IntPush( vWin, Abc_Var2Lit(iObj, Acb_ObjIsCi(p, iObj) || fIsTfiInput) );
    }
765
    // mark roots
766
    Acb_NtkIncTravId( p );
767 768 769 770
    Vec_IntForEachEntry( vRoots, iObj, i )
        Acb_ObjSetTravIdCur(p, iObj);
    // add TFO
    Vec_IntForEachEntry( vTfo, iObj, i )
771
    {
772 773
        assert( !Acb_ObjIsCo(p, iObj) );
        Vec_IntPush( vWin, Abc_Var2Lit(iObj, Acb_ObjIsTravIdCur(p, iObj)) );
774
    }
775
    return vWin;
776 777
}

778 779 780 781 782 783 784 785 786 787 788
/**Function*************************************************************

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
789
Vec_Int_t * Acb_NtkWindow( Acb_Ntk_t * p, int Pivot, int nTfiLevs, int nTfoLevs, int nFanMax, int fDelay, int * pnDivs )
790
{
791
    int fVerbose = 0;
792
    //int nTfiLevMin = Acb_ObjLevelD(p, Pivot) - nTfiLevs;
793
    int nTfoLevMax = Acb_ObjLevelD(p, Pivot) + nTfoLevs;
794
    Vec_Int_t * vWin, * vDivs, * vMarked, * vTfo, * vRoots, * vSide, * vTfi;
795
    // collect divisors by traversing limited TFI
796
    vDivs = Acb_NtkDivisors( p, Pivot, nTfiLevs, fDelay );
797
    if ( fVerbose ) Acb_NtkPrintVec( p, vDivs, "vDivs" );
798
    // mark limited TFO of the divisors
799
    vMarked = Acb_ObjMarkTfo( p, vDivs, Pivot, nTfoLevMax, nFanMax );
800
    // collect TFO and roots
801
    Acb_ObjDeriveTfo( p, Pivot, nTfoLevMax, nFanMax, &vTfo, &vRoots, 0 );//fDelay );
802 803
    if ( fVerbose ) Acb_NtkPrintVec( p, vTfo, "vTfo" );
    if ( fVerbose ) Acb_NtkPrintVec( p, vRoots, "vRoots" );
804 805
    // collect side inputs of the TFO
    vSide = Acb_NtkCollectTfoSideInputs( p, Pivot, vTfo );
806
    if ( fVerbose ) Acb_NtkPrintVec( p, vSide, "vSide" );
807
    // mark limited TFO of the divisors
808 809 810
    //Acb_ObjMarkTfo( p, vDivs, Pivot, nTfoLevMax, nFanMax );
    Acb_ObjMarkTfo2( p, vMarked );
    Vec_IntFree( vMarked );
811
    // collect new TFI
812 813
    vTfi = Acb_NtkCollectNewTfi( p, Pivot, vDivs, vSide, pnDivs );
    if ( fVerbose ) Acb_NtkPrintVec( p, vTfi, "vTfi" );
814 815
    Vec_IntFree( vSide );
    Vec_IntFree( vDivs );
816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837
    // collect all nodes
    vWin = Acb_NtkCollectWindow( p, Pivot, vTfi, vTfo, vRoots );
    // cleanup
    Vec_IntFree( vTfi );
    Vec_IntFree( vTfo );
    Vec_IntFree( vRoots );
    return vWin;
}



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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
838 839 840 841 842 843
static inline void Vec_IntVars2Vars( Vec_Int_t * p, int Shift )
{
    int i;
    for ( i = 0; i < p->nSize; i++ )
        p->pArray[i] += Shift;
}
844 845 846 847 848 849 850 851 852 853
static inline void Vec_IntVars2Lits( Vec_Int_t * p, int Shift, int fCompl )
{
    int i;
    for ( i = 0; i < p->nSize; i++ )
        p->pArray[i] = Abc_Var2Lit( p->pArray[i] + Shift, fCompl );
}
static inline void Vec_IntLits2Vars( Vec_Int_t * p, int Shift )
{
    int i;
    for ( i = 0; i < p->nSize; i++ )
854
        p->pArray[i] = Abc_Lit2Var( p->pArray[i] ) + Shift;
855 856 857 858 859 860 861 862
}
static inline void Vec_IntRemap( Vec_Int_t * p, Vec_Int_t * vMap )
{
    int i;
    for ( i = 0; i < p->nSize; i++ )
        p->pArray[i] = Vec_IntEntry(vMap, p->pArray[i]);
}

863
static inline void Acb_WinPrint( Acb_Ntk_t * p, Vec_Int_t * vWin, int Pivot, int nDivs )
864 865
{
    int i, Node;
866
    printf( "Window for node %d with %d divisors:\n", Pivot, nDivs );
867 868 869 870 871
    Vec_IntForEachEntry( vWin, Node, i )
    {
        if ( i == nDivs )
            printf( " | " );
        if ( Abc_Lit2Var(Node) == Pivot )
872
            printf( "(%d) ", Pivot );
873
        else
874
            printf( "%s%d ", Abc_LitIsCompl(Node) ? "*":"", Abc_Lit2Var(Node) );
875 876 877 878
    }
    printf( "\n" );
}

879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902
static inline void Acb_NtkOrderByRefCount( Acb_Ntk_t * p, Vec_Int_t * vSupp )
{
    int i, j, best_i, nSize = Vec_IntSize(vSupp);
    int * pArray = Vec_IntArray(vSupp);
    for ( i = 0; i < nSize-1; i++ )
    {
        best_i = i;
        for ( j = i+1; j < nSize; j++ )
            if ( Acb_ObjFanoutNum(p, pArray[j]) > Acb_ObjFanoutNum(p, pArray[best_i]) )
                best_i = j;
        ABC_SWAP( int, pArray[i], pArray[best_i] );
    }
}

static inline void Acb_NtkRemapIntoSatVariables( Acb_Ntk_t * p, Vec_Int_t * vSupp )
{
    int k, iFanin;
    Vec_IntForEachEntry( vSupp, iFanin, k )
    {
        assert( Acb_ObjFunc(p, iFanin) >= 0 );
        Vec_IntWriteEntry( vSupp, k, Acb_ObjFunc(p, iFanin) );
    }
}

903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918
/**Function*************************************************************

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Acb_NtkFindSupp1( Acb_Ntk_t * p, int Pivot, sat_solver * pSat, int nVars, int nDivs, Vec_Int_t * vWin, Vec_Int_t * vSupp )
{
    int nSuppNew, status, k, iFanin, * pFanins;
    Vec_IntClear( vSupp );
    Acb_ObjForEachFaninFast( p, Pivot, pFanins, iFanin, k )
919 920 921
        Vec_IntPush( vSupp, iFanin );
    Acb_NtkOrderByRefCount( p, vSupp );
    Acb_NtkRemapIntoSatVariables( p, vSupp );
922
    Vec_IntVars2Lits( vSupp, 2*nVars, 0 );
923 924 925 926 927
    status = sat_solver_solve( pSat, Vec_IntArray(vSupp), Vec_IntLimit(vSupp), 0, 0, 0, 0 );
    if ( status != l_False )
        printf( "Failed internal check at node %d.\n", Pivot );
    assert( status == l_False );
    nSuppNew = sat_solver_minimize_assumptions( pSat, Vec_IntArray(vSupp), Vec_IntSize(vSupp), 0 );
928 929
    Vec_IntShrink( vSupp, nSuppNew );
    Vec_IntLits2Vars( vSupp, -2*nVars );
930
    return Vec_IntSize(vSupp) < Acb_ObjFaninNum(p, Pivot);
931 932
}

933 934 935 936 937 938 939 940 941 942
static int StrCount = 0;

int Acb_NtkFindSupp2( Acb_Ntk_t * p, int Pivot, sat_solver * pSat, int nVars, int nDivs, Vec_Int_t * vWin, Vec_Int_t * vSupp, int nLutSize, int fDelay )
{
    int nSuppNew, status, k, iFanin, * pFanins, k2, iFanin2, * pFanins2;
    Acb_ObjForEachFaninFast( p, Pivot, pFanins, iFanin, k )
        assert( Acb_ObjFunc(p, iFanin) >= 0 && Acb_ObjFunc(p, iFanin) < nDivs );
    if ( fDelay )
    {
        // add non-timing-critical fanins
943
        int nNonCrits, k2, iFanin2 = 0, * pFanins2;
944 945 946
        assert( Acb_ObjLevelD( p, Pivot ) > 1 );
        Vec_IntClear( vSupp );
        Acb_ObjForEachFaninFast( p, Pivot, pFanins, iFanin, k )
947
            if ( !Acb_ObjIsDelayCriticalFanin( p, Pivot, iFanin ) )
948 949 950 951
                Vec_IntPush( vSupp, iFanin );
        nNonCrits = Vec_IntSize(vSupp);
        // add fanins of timing critical fanins
        Acb_ObjForEachFaninFast( p, Pivot, pFanins, iFanin, k )
952
            if ( Acb_ObjIsDelayCriticalFanin( p, Pivot, iFanin ) )
953 954 955 956 957 958 959 960
                Acb_ObjForEachFaninFast( p, iFanin, pFanins2, iFanin2, k2 )
                    Vec_IntPushUnique( vSupp, iFanin2 );
        assert( nNonCrits < Vec_IntSize(vSupp) );
        // sort additional fanins by level
        Vec_IntSelectSortCost( Vec_IntArray(vSupp) + nNonCrits, Vec_IntSize(vSupp) - nNonCrits, &p->vLevelD );
        // translate to SAT vars
        Vec_IntForEachEntry( vSupp, iFanin, k )
        {
961
            assert( Acb_ObjFunc(p, iFanin) >= 0 );
962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977
            Vec_IntWriteEntry( vSupp, k, Acb_ObjFunc(p, iFanin) );
        }
        // solve for these fanins
        Vec_IntVars2Lits( vSupp, 2*nVars, 0 );
        status = sat_solver_solve( pSat, Vec_IntArray(vSupp), Vec_IntLimit(vSupp), 0, 0, 0, 0 );
        if ( status != l_False )
            printf( "Failed internal check at node %d.\n", Pivot );
        assert( status == l_False );
        nSuppNew = sat_solver_minimize_assumptions( pSat, Vec_IntArray(vSupp), Vec_IntSize(vSupp), 0 );
        Vec_IntShrink( vSupp, nSuppNew );
        Vec_IntLits2Vars( vSupp, -2*nVars );
        return Vec_IntSize(vSupp) <= nLutSize;
    }
    // iterate through different fanout free cones
    Acb_ObjForEachFaninFast( p, Pivot, pFanins, iFanin, k )
    {
978
        if ( !Acb_ObjIsAreaCritical(p, iFanin) )
979 980 981 982 983 984
            continue;
        // collect fanins of the root node
        Vec_IntClear( vSupp );
        Acb_ObjForEachFaninFast( p, Pivot, pFanins2, iFanin2, k2 )
            if ( iFanin != iFanin2 )
                Vec_IntPush( vSupp, iFanin2 );
985
        // collect fanins of the selected node
986 987 988 989
        Acb_ObjForEachFaninFast( p, iFanin, pFanins2, iFanin2, k2 )
            Vec_IntPushUnique( vSupp, iFanin2 );
        // sort fanins by level
        Vec_IntSelectSortCost( Vec_IntArray(vSupp), Vec_IntSize(vSupp), &p->vLevelD );
990 991
        //Acb_NtkOrderByRefCount( p, vSupp );
        Acb_NtkRemapIntoSatVariables( p, vSupp );
992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016
        // solve for these fanins
        Vec_IntVars2Lits( vSupp, 2*nVars, 0 );
        status = sat_solver_solve( pSat, Vec_IntArray(vSupp), Vec_IntLimit(vSupp), 0, 0, 0, 0 );
        if ( status != l_False )
            printf( "Failed internal check at node %d.\n", Pivot );
        assert( status == l_False );
        nSuppNew = sat_solver_minimize_assumptions( pSat, Vec_IntArray(vSupp), Vec_IntSize(vSupp), 0 );
        Vec_IntShrink( vSupp, nSuppNew );
        Vec_IntLits2Vars( vSupp, -2*nVars );
        if ( Vec_IntSize(vSupp) <= nLutSize )
            return 1;
    }
    return 0;
}

int Acb_NtkFindSupp3( Acb_Ntk_t * p, int Pivot, sat_solver * pSat, int nVars, int nDivs, Vec_Int_t * vWin, Vec_Int_t * vSupp, int nLutSize, int fDelay )
{
    int nSuppNew, status, k, iFanin, * pFanins, k2, iFanin2, * pFanins2, k3, iFanin3, * pFanins3, NodeMark;

    if ( fDelay )
        return 0;

    // iterate through pairs of fanins with one fanouts
    Acb_ObjForEachFaninFast( p, Pivot, pFanins, iFanin, k )
    {
1017
        if ( !Acb_ObjIsAreaCritical(p, iFanin) )
1018 1019 1020
            continue;
        Acb_ObjForEachFaninFast( p, Pivot, pFanins2, iFanin2, k2 )
        {
1021
            if ( !Acb_ObjIsAreaCritical(p, iFanin2) || k2 == k )
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
                continue;
            // iFanin and iFanin2 have 1 fanout
            assert( iFanin != iFanin2 );

            // collect fanins of the root node
            Vec_IntClear( vSupp );
            Acb_ObjForEachFaninFast( p, Pivot, pFanins3, iFanin3, k3 )
                if ( iFanin3 != iFanin && iFanin3 != iFanin2 )
                {
                    assert( Acb_ObjFunc(p, iFanin3) >= 0 );
                    Vec_IntPush( vSupp, Abc_Var2Lit(Acb_ObjFunc(p, iFanin3) + 6*nVars, 0) );
                }
            NodeMark = Vec_IntSize(vSupp);

            // collect fanins of the second node
            Acb_ObjForEachFaninFast( p, iFanin, pFanins3, iFanin3, k3 )
            {
                assert( Acb_ObjFunc(p, iFanin3) >= 0 );
                Vec_IntPush( vSupp, Abc_Var2Lit(Acb_ObjFunc(p, iFanin3) + 6*nVars + nDivs, 0) );
            }
            // collect fanins of the third node
            Acb_ObjForEachFaninFast( p, iFanin2, pFanins3, iFanin3, k3 )
            {
                assert( Acb_ObjFunc(p, iFanin3) >= 0 );
                Vec_IntPushUnique( vSupp, Abc_Var2Lit(Acb_ObjFunc(p, iFanin3) + 6*nVars + nDivs, 0) );
            }
            assert( Vec_IntCheckUniqueSmall(vSupp) );

            // sort fanins by level
            //Vec_IntSelectSortCost( Vec_IntArray(vSupp) + NodeMark, Vec_IntSize(vSupp) - NodeMark, &p->vLevelD );
            // solve for these fanins
            status = sat_solver_solve( pSat, Vec_IntArray(vSupp), Vec_IntLimit(vSupp), 0, 0, 0, 0 );
            if ( status != l_False )
                continue;
            assert( status == l_False );
            nSuppNew = sat_solver_minimize_assumptions( pSat, Vec_IntArray(vSupp), Vec_IntSize(vSupp), 0 );
            Vec_IntShrink( vSupp, nSuppNew );
            Vec_IntLits2Vars( vSupp, -6*nVars );
1060
            Vec_IntSort( vSupp, 1 );
1061 1062 1063
            // count how many belong to H; the rest belong to G
            NodeMark = 0;
            Vec_IntForEachEntry( vSupp, iFanin3, k3 )
1064
                if ( iFanin3 >= nDivs )
1065
                    Vec_IntWriteEntry( vSupp, k3, iFanin3 - nDivs );
1066 1067 1068 1069 1070 1071 1072 1073
                else 
                    NodeMark++;
            if ( NodeMark == 0 )
            {
                //printf( "Obj %d: Special case 1 (vars = %d)\n", Pivot, Vec_IntSize(vSupp) );
                continue;
            }
            assert( NodeMark > 0 );
1074 1075 1076 1077 1078 1079 1080 1081
            if ( Vec_IntSize(vSupp) - NodeMark <= nLutSize )
                return NodeMark;
        }
    }

    // iterate through fanins with one fanout and their fanins with one fanout
    Acb_ObjForEachFaninFast( p, Pivot, pFanins, iFanin, k )
    {
1082
        if ( !Acb_ObjIsAreaCritical(p, iFanin) )
1083 1084 1085
            continue;
        Acb_ObjForEachFaninFast( p, iFanin, pFanins2, iFanin2, k2 )
        {
1086
            if ( !Acb_ObjIsAreaCritical(p, iFanin2) )
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 1119 1120
                continue;
            // iFanin and iFanin2 have 1 fanout
            assert( iFanin != iFanin2 );

            // collect fanins of the root node
            Vec_IntClear( vSupp );
            Acb_ObjForEachFaninFast( p, Pivot, pFanins3, iFanin3, k3 )
                if ( iFanin3 != iFanin && iFanin3 != iFanin2 )
                    Vec_IntPush( vSupp, Abc_Var2Lit(Acb_ObjFunc(p, iFanin3) + 6*nVars, 0) );
            NodeMark = Vec_IntSize(vSupp);

            // collect fanins of the second node
            Acb_ObjForEachFaninFast( p, iFanin, pFanins3, iFanin3, k3 )
                if ( iFanin3 != iFanin2 )
                    Vec_IntPush( vSupp, Abc_Var2Lit(Acb_ObjFunc(p, iFanin3) + 6*nVars + nDivs, 0) );
            // collect fanins of the third node
            Acb_ObjForEachFaninFast( p, iFanin2, pFanins3, iFanin3, k3 )
            {
                assert( Acb_ObjFunc(p, iFanin3) >= 0 );
                Vec_IntPushUnique( vSupp, Abc_Var2Lit(Acb_ObjFunc(p, iFanin3) + 6*nVars + nDivs, 0) );
            }
            assert( Vec_IntCheckUniqueSmall(vSupp) );

            // sort fanins by level
            //Vec_IntSelectSortCost( Vec_IntArray(vSupp) + NodeMark, Vec_IntSize(vSupp) - NodeMark, &p->vLevelD );
            //Sat_SolverWriteDimacs( pSat, NULL, Vec_IntArray(vSupp), Vec_IntLimit(vSupp), 0 );
            // solve for these fanins
            status = sat_solver_solve( pSat, Vec_IntArray(vSupp), Vec_IntLimit(vSupp), 0, 0, 0, 0 );
            if ( status != l_False )
                printf( "Failed internal check at node %d.\n", Pivot );
            assert( status == l_False );
            nSuppNew = sat_solver_minimize_assumptions( pSat, Vec_IntArray(vSupp), Vec_IntSize(vSupp), 0 );
            Vec_IntShrink( vSupp, nSuppNew );
            Vec_IntLits2Vars( vSupp, -6*nVars );
1121
            Vec_IntSort( vSupp, 1 );
1122 1123 1124
            // count how many belong to H; the rest belong to G
            NodeMark = 0;
            Vec_IntForEachEntry( vSupp, iFanin3, k3 )
1125
                if ( iFanin3 >= nDivs )
1126
                    Vec_IntWriteEntry( vSupp, k3, iFanin3 - nDivs );
1127 1128 1129 1130 1131 1132 1133
                else 
                    NodeMark++;
            if ( NodeMark == 0 )
            {
                //printf( "Obj %d: Special case 2 (vars = %d)\n", Pivot, Vec_IntSize(vSupp) );
                continue;
            }
1134 1135 1136 1137 1138 1139 1140 1141 1142
            assert( NodeMark > 0 );
            if ( Vec_IntSize(vSupp) - NodeMark <= nLutSize )
                return NodeMark;
        }
    }

    return 0;
}

1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155
/**Function*************************************************************

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
typedef struct Acb_Mfs_t_ Acb_Mfs_t;
struct Acb_Mfs_t_
1156
{
1157 1158 1159
    Acb_Ntk_t *     pNtk;        // network
    Acb_Par_t *     pPars;       // parameters
    sat_solver *    pSat[3];     // SAT solvers
1160 1161
    Vec_Int_t *     vSupp;       // support
    Vec_Int_t *     vFlip;       // support
1162
    Vec_Int_t *     vValues;     // support
1163 1164 1165 1166
    int             nNodes;      // nodes
    int             nWins;       // windows
    int             nWinsAll;    // windows
    int             nDivsAll;    // windows
1167 1168
    int             nChanges[8]; // changes
    int             nOvers;      // overflows
1169
    int             nTwoNodes;   // two nodes
1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186
    abctime         timeTotal;
    abctime         timeCnf;
    abctime         timeSol;
    abctime         timeWin;
    abctime         timeSat;
    abctime         timeSatU;
    abctime         timeSatS;
};
Acb_Mfs_t * Acb_MfsStart( Acb_Ntk_t * pNtk, Acb_Par_t * pPars )
{
    Acb_Mfs_t * p = ABC_CALLOC( Acb_Mfs_t, 1 );
    p->pNtk       = pNtk;
    p->pPars      = pPars;
    p->timeTotal  = Abc_Clock();
    p->pSat[0]    = sat_solver_new();
    p->pSat[1]    = sat_solver_new();
    p->pSat[2]    = sat_solver_new();
1187 1188
    p->vSupp      = Vec_IntAlloc(100);
    p->vFlip      = Vec_IntAlloc(100);
1189
    p->vValues    = Vec_IntAlloc(100);
1190 1191 1192 1193
    return p;
}
void Acb_MfsStop( Acb_Mfs_t * p )
{
1194 1195
    Vec_IntFree( p->vFlip );
    Vec_IntFree( p->vSupp );
1196
    Vec_IntFree( p->vValues );
1197 1198 1199 1200 1201
    sat_solver_delete( p->pSat[0] );
    sat_solver_delete( p->pSat[1] );
    sat_solver_delete( p->pSat[2] );
    ABC_FREE( p );
}
1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 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
static inline int Acb_NtkObjMffcEstimate( Acb_Ntk_t * pNtk, int iObj )
{
    int k, iFanin, * pFanins, Count = 0, iFaninCrit = -1;
    Acb_ObjForEachFaninFast( pNtk, iObj, pFanins, iFanin, k )
        if ( Acb_ObjIsAreaCritical(pNtk, iFanin) )
            iFaninCrit = iFanin, Count++;
    if ( Count != 1 )
        return Count;
    Acb_ObjForEachFaninFast( pNtk, iFaninCrit, pFanins, iFanin, k )
        if ( Acb_ObjIsAreaCritical(pNtk, iFanin) )
            Count++;
    return Count;
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Acb_NtkOptNodeAnalyze( Acb_Mfs_t * p, int PivotVar, int nDivs, int nValues, int * pValues, Vec_Int_t * vSupp )
{
    word OnSet[64] = {0};
    word OffSet[64] = {0};
    word Diffs[64] = {0};
    int s, nScope = 1 + 2*nDivs, d, i;
    int f, nFrames = nValues / nScope;
    int start = nDivs < 64 ? 0 : nDivs - 64;
    int stop  = nDivs < 64 ? nDivs : 64;
    assert( nValues % nScope == 0 );
    assert( nFrames <= 16 );
    for ( f = 0; f < nFrames; f++ )
    {
        int * pStart  = pValues + f * nScope;
        int * pOnSet  = pStart + 1 + (pStart[0] ? 0 : nDivs);
        int * pOffSet = pStart + 1 + (pStart[0] ? nDivs : 0);

        printf( "%2d:", f );
        for ( s = start; s < stop; s++ )
            printf( "%d", pOnSet[s] );
        printf( "\n" );

        printf( "%2d:", f );
        for ( s = start; s < stop; s++ )
            printf( "%d", pOffSet[s] );
        printf( "\n" );

        for ( s = start; s < stop; s++ )
        {
            if ( pOnSet[s] )   OnSet[f]  |= (((word)1) << (s-start));
            if ( pOffSet[s] )  OffSet[f] |= (((word)1) << (s-start));
        }
    }
    d = 0;
    for ( f = 0; f < nFrames; f++ )
    for ( s = 0; s < nFrames; s++ )
    {
        for ( i = 0; i < d; i++ )
            if ( Diffs[i] == (OnSet[f] ^ OffSet[s]) )
                break;
        if ( i < d )
            continue;
        if ( d < 64 )
            Diffs[d++] = OnSet[f] ^ OffSet[s];
    }

    printf( "Divisors = %d.  Frames = %d.  Patterns = %d.\n", nDivs, nFrames, d );
    printf( "   " );
    for ( s = start; s < stop; s++ )
        printf( "%d", s / 10 );
    printf( "\n" );
    printf( "   " );
    for ( s = start; s < stop; s++ )
        printf( "%d", s % 10 );
    printf( "\n" );
    printf( "   " );
    for ( s = start; s < stop; s++ )
        printf( "%c", Vec_IntFind(vSupp, s) >= 0 ? 'a' + Vec_IntFind(vSupp, s) : ' ' );
    printf( "\n" );
    for ( s = 0; s < d; s++ )
    {
        printf( "%2d:", s );
        for ( f = 0; f < stop; f++ )
            printf( "%c", ((Diffs[s] >> f) & 1) ? '*' : ' ' );
        printf( "\n" );
    }
}

1295
int Acb_NtkOptNode( Acb_Mfs_t * p, int Pivot )
1296
{
1297 1298 1299
    Cnf_Dat_t * pCnf = NULL; abctime clk;
    Vec_Int_t * vWin = NULL; word uTruth;
    int Result, PivotVar, nDivs = 0, RetValue = 0, c;
1300
    assert( Acb_ObjFanoutNum(p->pNtk, Pivot) > 0 );
1301
    p->nWins++;
1302

1303
    // compute divisors and window for this target node with these taboo nodes
1304
    clk = Abc_Clock();
1305
    vWin = Acb_NtkWindow( p->pNtk, Pivot, p->pPars->nTfiLevMax, p->pPars->nTfoLevMax, p->pPars->nFanoutMax, !p->pPars->fArea, &nDivs );
1306 1307 1308
    p->nWinsAll += Vec_IntSize(vWin);
    p->nDivsAll += nDivs;
    p->timeWin  += Abc_Clock() - clk;
1309
    PivotVar = Vec_IntFind( vWin, Abc_Var2Lit(Pivot, 0) );
1310
    if ( p->pPars->fVerbose )
1311
    printf( "Node %d: Window contains %d objects and %d divisors.  ", Pivot, Vec_IntSize(vWin), nDivs );
1312 1313 1314
//    Acb_WinPrint( p->pNtk, vWin, Pivot, nDivs );
//    Acb_NtkPrintVecWin( p->pNtk, vWin, "Win" );
    if ( Vec_IntSize(vWin) > p->pPars->nWinNodeMax )
1315 1316 1317 1318
    {
        p->nOvers++;
        if ( p->pPars->fVerbose )
            printf( "Too many divisors.\n" );
1319
        goto cleanup;
1320
    }
1321

1322 1323
    // derive CNF 
    clk = Abc_Clock();
1324 1325 1326
    pCnf = Acb_NtkWindow2Cnf( p->pNtk, vWin, Pivot );
    assert( PivotVar == Acb_ObjFunc(p->pNtk, Pivot) );
    Cnf_DataCollectFlipLits( pCnf, PivotVar, p->vFlip );
1327
    p->timeCnf += Abc_Clock() - clk;
1328

1329 1330
    // derive SAT solver
    clk = Abc_Clock();
1331
    Acb_NtkWindow2Solver( p->pSat[0], pCnf, p->vFlip, PivotVar, nDivs, 1 );
1332
    p->timeSol += Abc_Clock() - clk;
1333 1334 1335 1336
    // check constants
    for ( c = 0; c < 2; c++ )
    {
        int Lit = Abc_Var2Lit( PivotVar, c );
1337
        int status = sat_solver_solve( p->pSat[0], &Lit, &Lit + 1, 0, 0, 0, 0 );
1338 1339
        if ( status == l_False )
        {
1340 1341
            p->nChanges[0]++;
            if ( p->pPars->fVerbose )
1342
            printf( "Found constant %d.\n", c );
1343 1344
            Acb_NtkUpdateNode( p->pNtk, Pivot, c ? ~(word)0 : 0, NULL );
            RetValue = 1;
1345 1346 1347 1348 1349
            goto cleanup;
        }
        assert( status == l_True );
    }

1350 1351
    // derive SAT solver
    clk = Abc_Clock();
1352
    Acb_NtkWindow2Solver( p->pSat[1], pCnf, p->vFlip, PivotVar, nDivs, 2 );
1353
    p->timeSol += Abc_Clock() - clk;
1354 1355 1356 1357

    // try to remove useless fanins
    if ( p->pPars->fArea )
    {
1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378
        int fEnableProfile = 0;
        if ( fEnableProfile )
        {
            // alloc
            if ( p->pSat[1]->user_values.cap == 0 )
                veci_new(&p->pSat[1]->user_values);
            else
                p->pSat[1]->user_values.size = 0;
            if ( p->pSat[1]->user_vars.cap == 0 )
                veci_new(&p->pSat[1]->user_vars);
            else
                p->pSat[1]->user_vars.size = 0;
            // set variables
            veci_push(&p->pSat[1]->user_vars, PivotVar);
            for ( c = 0; c < nDivs; c++ )
                veci_push(&p->pSat[1]->user_vars, c);
            for ( c = 0; c < nDivs; c++ )
                veci_push(&p->pSat[1]->user_vars, c+pCnf->nVars);
        }

        // perform solving
1379 1380 1381
        clk = Abc_Clock();
        Result = Acb_NtkFindSupp1( p->pNtk, Pivot, p->pSat[1], pCnf->nVars, nDivs, vWin, p->vSupp );
        p->timeSat += Abc_Clock() - clk;
1382 1383
        // undo variables
        p->pSat[1]->user_vars.size = 0;
1384 1385 1386 1387 1388 1389 1390 1391 1392
        if ( Result )
        {
            if ( Vec_IntSize(p->vSupp) == 0 )
                p->nChanges[0]++;
            else
                p->nChanges[1]++;
            assert( Vec_IntSize(p->vSupp) < p->pPars->nLutSize );
            if ( p->pPars->fVerbose )
            printf( "Found %d inputs: ", Vec_IntSize(p->vSupp) );
1393
            uTruth = Acb_ComputeFunction( p->pSat[0], PivotVar, sat_solver_nvars(p->pSat[0])-1, p->vSupp, 0 );
1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404
            if ( p->pPars->fVerbose )
            Extra_PrintHex( stdout, (unsigned *)&uTruth, Vec_IntSize(p->vSupp) ); 
            if ( p->pPars->fVerbose )
            printf( "\n" );
            // create support in terms of nodes
            Vec_IntRemap( p->vSupp, vWin );
            Vec_IntLits2Vars( p->vSupp, 0 );
            Acb_NtkUpdateNode( p->pNtk, Pivot, uTruth, p->vSupp );
            RetValue = 1;
            goto cleanup;
        }
1405 1406 1407 1408 1409 1410
        if ( fEnableProfile )
        {
            // analyze the resulting values
            Acb_NtkOptNodeAnalyze( p, PivotVar, nDivs, p->pSat[1]->user_values.size, p->pSat[1]->user_values.ptr, p->vSupp );
            p->pSat[1]->user_values.size = 0;
        }
1411 1412
    }

1413
    if ( Acb_NtkObjMffcEstimate(p->pNtk, Pivot) >= 1 )
1414
    {
1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436
        // check for one-node implementation
        clk = Abc_Clock();
        Result = Acb_NtkFindSupp2( p->pNtk, Pivot, p->pSat[1], pCnf->nVars, nDivs, vWin, p->vSupp, p->pPars->nLutSize, !p->pPars->fArea );
        p->timeSat += Abc_Clock() - clk;
        if ( Result )
        {
            p->nChanges[2]++;
            assert( Vec_IntSize(p->vSupp) <= p->pPars->nLutSize );
            if ( p->pPars->fVerbose )
            printf( "Found %d inputs: ", Vec_IntSize(p->vSupp) );
            uTruth = Acb_ComputeFunction( p->pSat[0], PivotVar, sat_solver_nvars(p->pSat[0])-1, p->vSupp, 0 );
            if ( p->pPars->fVerbose )
            Extra_PrintHex( stdout, (unsigned *)&uTruth, Vec_IntSize(p->vSupp) ); 
            if ( p->pPars->fVerbose )
            printf( "\n" );
            // create support in terms of nodes
            Vec_IntRemap( p->vSupp, vWin );
            Vec_IntLits2Vars( p->vSupp, 0 );
            Acb_NtkUpdateNode( p->pNtk, Pivot, uTruth, p->vSupp );
            RetValue = 1;
            goto cleanup;
        }
1437 1438
    }

1439
//#if 0
1440
    if ( p->pPars->fUseAshen && Acb_NtkObjMffcEstimate(p->pNtk, Pivot) >= 2 )// && Pivot != 70 )
1441
    {
1442 1443 1444 1445 1446 1447 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 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
        p->nTwoNodes++;
        // derive SAT solver
        clk = Abc_Clock();
        Acb_NtkWindow2Solver( p->pSat[2], pCnf, p->vFlip, PivotVar, nDivs, 6 );
        p->timeSol += Abc_Clock() - clk;

        // check for two-node implementation
        clk = Abc_Clock();
        Result = Acb_NtkFindSupp3( p->pNtk, Pivot, p->pSat[2], pCnf->nVars, nDivs, vWin, p->vSupp, p->pPars->nLutSize, !p->pPars->fArea );
        p->timeSat += Abc_Clock() - clk;
        if ( Result )
        {
            int fVerbose = 1;
            int i, k, Lit, Var, Var2, status, NodeNew, fBecameUnsat = 0, fCompl = 0;
            assert( Result                       <  p->pPars->nLutSize );
            assert( Vec_IntSize(p->vSupp)-Result <= p->pPars->nLutSize );
            if ( fVerbose || p->pPars->fVerbose )
            printf( "Obj %5d: Found %d Hvars and %d Gvars: ", Pivot, Result, Vec_IntSize(p->vSupp)-Result );
            // p->vSupp contains G variables (Vec_IntSize(p->vSupp)-Result) followed by H variables (Result)
            //sat_solver_restart( p->pSat[1] );
            //Acb_NtkWindow2Solver( p->pSat[1], pCnf, p->vFlip, PivotVar, nDivs, 2 );

            // constrain H-variables to be equal
            Vec_IntForEachEntryStart( p->vSupp, Var, i, Vec_IntSize(p->vSupp)-Result ) // H variables
            {
                assert( Var >= 0 && Var < nDivs );
                assert( Var + 2*pCnf->nVars < sat_solver_nvars(p->pSat[1]) );
                Lit = Abc_Var2Lit( Var + 2*pCnf->nVars, 0 ); // HVars are the same
                if ( !sat_solver_addclause( p->pSat[1], &Lit, &Lit + 1 ) )
                { if ( fVerbose || p->pPars->fVerbose ) printf( "Error: SAT solver became UNSAT at a wrong place (place 2).  " ); fBecameUnsat = 1; }
            }
            // find one satisfying assighment
            status = sat_solver_solve( p->pSat[1], NULL, NULL, 0, 0, 0, 0 );
            assert( status == l_True );
            // get assignment of the function
            fCompl = !sat_solver_var_value( p->pSat[1], PivotVar );
            // constrain second set of G-vars to have values equal to the assignment
            Vec_IntForEachEntryStop( p->vSupp, Var, i, Vec_IntSize(p->vSupp)-Result ) // G variables
            {
                // check if this is a C-var
                Vec_IntForEachEntryStart( p->vSupp, Var2, k, Vec_IntSize(p->vSupp)-Result ) // G variables
                    if ( Var == Var2 )
                        break;
                if ( k < Vec_IntSize(p->vSupp) ) // do not constrain a C-var
                {
                    if ( fVerbose || p->pPars->fVerbose )
                    printf( "Found C-var in object %d.  ", Pivot );
                    continue;
                }
                assert( Var >= 0 && Var < nDivs );
                Lit = sat_solver_var_literal( p->pSat[1], Var + pCnf->nVars );
                if ( !sat_solver_addclause( p->pSat[1], &Lit, &Lit + 1 ) )
                { if ( fVerbose || p->pPars->fVerbose ) printf( "Error: SAT solver became UNSAT at a wrong place (place 1).  " ); fBecameUnsat = 1; }
            }
            if ( fBecameUnsat )
            {
                StrCount++;
                if ( fVerbose || p->pPars->fVerbose )
                printf( " Quitting.\n" );
                goto cleanup;
            }
            // consider only G variables
            p->vSupp->nSize -= Result;
            // truth table
            uTruth = Acb_ComputeFunction( p->pSat[1], PivotVar, sat_solver_nvars(p->pSat[1])-1, p->vSupp, fCompl );
            if ( fVerbose || p->pPars->fVerbose )
            Extra_PrintHex( stdout, (unsigned *)&uTruth, Vec_IntSize(p->vSupp) ); 
            if ( uTruth == 0 || ~uTruth == 0 )
            {
                if ( fVerbose || p->pPars->fVerbose )
                printf( " Quitting.\n" );
                goto cleanup;
            }
            p->nChanges[3]++;
            // create new node
            Vec_IntRemap( p->vSupp, vWin );
            Vec_IntLits2Vars( p->vSupp, 0 );
            NodeNew = Acb_NtkCreateNode( p->pNtk, uTruth, p->vSupp );
            Acb_DeriveCnfForWindowOne( p->pNtk, NodeNew );
            Acb_DeriveCnfForNode( p->pNtk, NodeNew, p->pSat[0], sat_solver_nvars(p->pSat[0])-2 );
            p->vSupp->nSize += Result;
            // collect new variables
            Vec_IntForEachEntryStart( p->vSupp, Var, i, Vec_IntSize(p->vSupp)-Result )
                Vec_IntWriteEntry( p->vSupp, i-(Vec_IntSize(p->vSupp)-Result), Var );
            Vec_IntShrink( p->vSupp, Result );
            Vec_IntPush( p->vSupp, sat_solver_nvars(p->pSat[0])-2 );
            // truth table
            uTruth = Acb_ComputeFunction( p->pSat[0], PivotVar, sat_solver_nvars(p->pSat[0])-1, p->vSupp, 0 );
            // create new fanins of the node
            if ( fVerbose || p->pPars->fVerbose )
            printf( "  " );
            if ( fVerbose || p->pPars->fVerbose )
            Extra_PrintHex( stdout, (unsigned *)&uTruth, Vec_IntSize(p->vSupp) ); 
            if ( fVerbose || p->pPars->fVerbose )
            printf( "\n" );
            // create support in terms of nodes
            Vec_IntPop( p->vSupp );
            Vec_IntRemap( p->vSupp, vWin );
            Vec_IntLits2Vars( p->vSupp, 0 );
            Vec_IntPush( p->vSupp, NodeNew );
            Acb_NtkUpdateNode( p->pNtk, Pivot, uTruth, p->vSupp );
            RetValue = 2;
            goto cleanup;
        }
1546
    }
1547
//#endif
1548

1549
    if ( p->pPars->fVerbose )
1550
    printf( "\n" );
1551

1552
cleanup:
1553 1554 1555
    sat_solver_restart( p->pSat[0] );
    sat_solver_restart( p->pSat[1] );
    sat_solver_restart( p->pSat[2] );
1556
    if ( pCnf ) 
1557
    {
1558 1559
        Cnf_DataFree( pCnf );
        Acb_NtkWindowUndo( p->pNtk, vWin );
1560
    }
1561 1562
    Vec_IntFreeP( &vWin );
    return RetValue;
1563 1564
}

1565 1566 1567
/**Function*************************************************************

  Synopsis    []
1568

1569 1570 1571 1572 1573 1574 1575
  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
1576 1577 1578
void Acb_NtkOpt( Acb_Ntk_t * pNtk, Acb_Par_t * pPars )
{
    Acb_Mfs_t * pMan = Acb_MfsStart( pNtk, pPars );
1579
    if ( pPars->fVerbose )
1580 1581
        printf( "%s-optimization parameters: TfiLev(I) = %d  TfoLev(O) = %d  WinMax(W) = %d  LutSize = %d\n", 
            pMan->pPars->fArea ? "Area" : "Delay", pMan->pPars->nTfiLevMax, pMan->pPars->nTfoLevMax, pMan->pPars->nWinNodeMax, pMan->pPars->nLutSize );
1582 1583 1584
    Acb_NtkCreateFanout( pNtk );  // fanout data structure
    Acb_NtkCleanObjFuncs( pNtk ); // SAT variables
    Acb_NtkCleanObjCnfs( pNtk );  // CNF representations
1585
    if ( pMan->pPars->fArea )
1586
    {
1587 1588 1589 1590 1591 1592 1593 1594
        int n = 0, iObj, RetValue, nNodes = Acb_NtkObjNumMax(pNtk);
        Vec_Bit_t * vVisited = Vec_BitStart( Acb_NtkObjNumMax(pNtk) );
        Acb_NtkUpdateLevelD( pNtk, -1 ); // compute forward logic level
        for ( n = 2; n >= 0; n-- )
            Acb_NtkForEachNode( pNtk, iObj )
                if ( iObj < nNodes && !Vec_BitEntry(vVisited, iObj) && Acb_NtkObjMffcEstimate(pNtk, iObj) >= n )
                {
                    pMan->nNodes++;
1595 1596
                    //if ( iObj != 103 )
                    //    continue;
1597 1598 1599 1600 1601
                    //Acb_NtkOptNode( pMan, iObj );
                    while ( (RetValue = Acb_NtkOptNode(pMan, iObj)) && Acb_ObjFaninNum(pNtk, iObj) );                    
                    Vec_BitWriteEntry( vVisited, iObj, 1 );
                }
        Vec_BitFree( vVisited );
1602 1603 1604
    }
    else
    {
1605
        int Value;
1606
        Acb_NtkUpdateTiming( pNtk, -1 ); // compute delay information
1607
        while ( (Value = (int)Vec_QueTopPriority(pNtk->vQue)) > 0 )
1608
        {
1609
            int iObj = Vec_QuePop(pNtk->vQue);
1610
            if ( !Acb_ObjType(pNtk, iObj) )
1611
                continue;
1612 1613
            //if ( iObj != 103 )
            //    continue;
1614
            //printf( "Trying node %4d (%4d) ", iObj, Value );
1615
            Acb_NtkOptNode( pMan, iObj ); 
1616 1617
        }
    }
1618
    if ( pPars->fVerbose )
1619 1620
    {
        pMan->timeTotal = Abc_Clock() - pMan->timeTotal;
1621
        printf( "Node = %d  Win = %d (Ave = %d)  DivAve = %d   Change = %d  C = %d  N1 = %d  N2 = %d  N3 = %d   Over = %d  Str = %d  2Node = %d.\n", 
1622
            pMan->nNodes, pMan->nWins, pMan->nWinsAll/Abc_MaxInt(1, pMan->nWins), pMan->nDivsAll/Abc_MaxInt(1, pMan->nWins),
1623
            pMan->nChanges[0] + pMan->nChanges[1] + pMan->nChanges[2] + pMan->nChanges[3],
1624
            pMan->nChanges[0], pMan->nChanges[1], pMan->nChanges[2], pMan->nChanges[3], pMan->nOvers, StrCount, pMan->nTwoNodes );
1625 1626 1627 1628 1629 1630 1631 1632 1633 1634
        ABC_PRTP( "Windowing  ", pMan->timeWin,    pMan->timeTotal );
        ABC_PRTP( "CNF compute", pMan->timeCnf,    pMan->timeTotal );
        ABC_PRTP( "Make solver", pMan->timeSol,    pMan->timeTotal );
        ABC_PRTP( "SAT solving", pMan->timeSat,    pMan->timeTotal );
//        ABC_PRTP( "  unsat    ", pMan->timeSatU,   pMan->timeTotal );
//        ABC_PRTP( "  sat      ", pMan->timeSatS,   pMan->timeTotal );
        ABC_PRTP( "TOTAL      ", pMan->timeTotal,  pMan->timeTotal );
        fflush( stdout );
    }
    Acb_MfsStop( pMan );
1635
    StrCount = 0;
1636
}
1637 1638 1639 1640 1641 1642 1643 1644

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


ABC_NAMESPACE_IMPL_END