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

  FileName    [giaSatLut.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: giaSatLut.c,v 1.00 2005/06/20 00:00:00 alanmi Exp $]

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

#include "gia.h"
22
#include "misc/tim/tim.h"
23 24 25
#include "sat/bsat/satStore.h"
#include "misc/util/utilNam.h"
#include "map/scl/sclCon.h"
26
#include "misc/vec/vecHsh.h"
27 28 29 30 31 32 33 34 35 36 37

ABC_NAMESPACE_IMPL_START


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

typedef struct Sbl_Man_t_ Sbl_Man_t;
struct Sbl_Man_t_
{
38 39 40 41 42 43
    sat_solver *   pSat;         // SAT solver
    Vec_Int_t *    vCardVars;    // candinality variables
    int            nVars;        // max vars
    int            LogN;         // base-2 log of max vars
    int            Power2;       // power-2 of LogN
    int            FirstVar;     // first variable to be used
44 45 46
    // statistics
    int            nTried;       // nodes tried
    int            nImproved;    // nodes improved
47
    int            nRuns;        // the number of runs
48
    int            nHashWins;    // the number of hashed windows
49 50
    int            nSmallWins;   // the number of small windows
    int            nLargeWins;   // the number of large windows
51
    int            nIterOuts;    // the number of iters exceeded
52
    // parameters
53
    int            LutSize;      // LUT size
54 55 56 57 58 59
    int            nBTLimit;     // conflicts
    int            DelayMax;     // external delay
    int            nEdges;       // the number of edges
    int            fDelay;       // delay mode
    int            fReverse;     // reverse windowing
    int            fVerbose;     // verbose
60 61
    int            fVeryVerbose; // verbose
    int            fVeryVeryVerbose; // verbose
62
    // window
63 64 65 66 67 68
    Gia_Man_t *    pGia;
    Vec_Int_t *    vLeaves;      // leaf nodes
    Vec_Int_t *    vAnds;        // AND-gates
    Vec_Int_t *    vNodes;       // internal LUTs
    Vec_Int_t *    vRoots;       // driver nodes (a subset of vAnds)
    Vec_Int_t *    vRootVars;    // driver nodes (as SAT variables)
69
    Hsh_VecMan_t * pHash;        // hash table for windows
70
    // timing 
71 72 73 74 75
    Vec_Int_t *    vArrs;        // arrival times  
    Vec_Int_t *    vReqs;        // required times  
    Vec_Wec_t *    vWindow;      // fanins of each node in the window
    Vec_Int_t *    vPath;        // critical path (as SAT variables)
    Vec_Int_t *    vEdges;       // fanin edges
76
    // cuts
77 78 79 80
    Vec_Wrd_t *    vCutsI1;      // input bit patterns
    Vec_Wrd_t *    vCutsI2;      // input bit patterns
    Vec_Wrd_t *    vCutsN1;      // node bit patterns
    Vec_Wrd_t *    vCutsN2;      // node bit patterns
81 82 83
    Vec_Int_t *    vCutsNum;     // cut counts for each obj
    Vec_Int_t *    vCutsStart;   // starting cuts for each obj
    Vec_Int_t *    vCutsObj;     // object to which this cut belongs
84 85 86 87
    Vec_Wrd_t *    vTempI1;      // temporary cuts
    Vec_Wrd_t *    vTempI2;      // temporary cuts
    Vec_Wrd_t *    vTempN1;      // temporary cuts
    Vec_Wrd_t *    vTempN2;      // temporary cuts
88 89 90
    Vec_Int_t *    vSolInit;     // initial solution
    Vec_Int_t *    vSolCur;      // current solution
    Vec_Int_t *    vSolBest;     // best solution
91
    // temporary
92 93 94 95 96 97 98 99 100 101 102 103 104 105
    Vec_Int_t *    vLits;        // literals
    Vec_Int_t *    vAssump;      // literals
    Vec_Int_t *    vPolar;       // variables polarity
    // statistics
    abctime        timeWin;      // windowing
    abctime        timeCut;      // cut computation
    abctime        timeSat;      // SAT runtime
    abctime        timeSatSat;   // satisfiable time
    abctime        timeSatUns;   // unsatisfiable time
    abctime        timeSatUnd;   // undecided time
    abctime        timeTime;     // timing time
    abctime        timeStart;    // starting time
    abctime        timeTotal;    // all runtime
    abctime        timeOther;    // other time
106 107
};

108 109
extern sat_solver * Sbm_AddCardinSolver( int LogN, Vec_Int_t ** pvVars );

110 111 112 113 114 115
////////////////////////////////////////////////////////////////////////
///                     FUNCTION DEFINITIONS                         ///
////////////////////////////////////////////////////////////////////////

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

116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Sbl_Man_t * Sbl_ManAlloc( Gia_Man_t * pGia, int Number )
{
    Sbl_Man_t * p = ABC_CALLOC( Sbl_Man_t, 1 );
    p->nVars      = Number;
    p->LogN       = Abc_Base2Log(Number);
    p->Power2     = 1 << p->LogN;
    p->pSat       = Sbm_AddCardinSolver( p->LogN, &p->vCardVars );
    p->FirstVar   = sat_solver_nvars( p->pSat );
    sat_solver_bookmark( p->pSat );
    // window
    p->pGia       = pGia;
    p->vLeaves    = Vec_IntAlloc( p->nVars );
    p->vAnds      = Vec_IntAlloc( p->nVars );
    p->vNodes     = Vec_IntAlloc( p->nVars );
    p->vRoots     = Vec_IntAlloc( p->nVars );
    p->vRootVars  = Vec_IntAlloc( p->nVars );
141
    p->pHash      = Hsh_VecManStart( 1000 );
142 143 144 145 146 147 148
    // timing
    p->vArrs      = Vec_IntAlloc( 0 );
    p->vReqs      = Vec_IntAlloc( 0 );
    p->vWindow    = Vec_WecAlloc( 128 );
    p->vPath      = Vec_IntAlloc( 32 );
    p->vEdges     = Vec_IntAlloc( 32 );
    // cuts
149 150 151 152
    p->vCutsI1    = Vec_WrdAlloc( 1000 );     // input bit patterns
    p->vCutsI2    = Vec_WrdAlloc( 1000 );     // input bit patterns
    p->vCutsN1    = Vec_WrdAlloc( 1000 );     // node bit patterns
    p->vCutsN2    = Vec_WrdAlloc( 1000 );     // node bit patterns
153 154 155 156 157 158
    p->vCutsNum   = Vec_IntAlloc( 64 );       // cut counts for each obj
    p->vCutsStart = Vec_IntAlloc( 64 );       // starting cuts for each obj
    p->vCutsObj   = Vec_IntAlloc( 1000 );
    p->vSolInit   = Vec_IntAlloc( 64 );
    p->vSolCur    = Vec_IntAlloc( 64 );
    p->vSolBest   = Vec_IntAlloc( 64 );
159 160 161 162
    p->vTempI1    = Vec_WrdAlloc( 32 ); 
    p->vTempI2    = Vec_WrdAlloc( 32 ); 
    p->vTempN1    = Vec_WrdAlloc( 32 ); 
    p->vTempN2    = Vec_WrdAlloc( 32 ); 
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
    // internal
    p->vLits      = Vec_IntAlloc( 64 );
    p->vAssump    = Vec_IntAlloc( 64 );
    p->vPolar     = Vec_IntAlloc( 1000 );
    // other
    Gia_ManFillValue( pGia );
    return p;
}
void Sbl_ManClean( Sbl_Man_t * p )
{
    p->timeStart = Abc_Clock();
    sat_solver_rollback( p->pSat );
    sat_solver_bookmark( p->pSat );
    // internal
    Vec_IntClear( p->vLeaves );
    Vec_IntClear( p->vAnds );
    Vec_IntClear( p->vNodes );
    Vec_IntClear( p->vRoots );
    Vec_IntClear( p->vRootVars );
    // timing
    Vec_IntClear( p->vArrs );
    Vec_IntClear( p->vReqs );
    Vec_WecClear( p->vWindow );
    Vec_IntClear( p->vPath );
    Vec_IntClear( p->vEdges );
    // cuts
189 190 191 192
    Vec_WrdClear( p->vCutsI1 );
    Vec_WrdClear( p->vCutsI2 );
    Vec_WrdClear( p->vCutsN1 );
    Vec_WrdClear( p->vCutsN2 );
193 194 195 196 197 198
    Vec_IntClear( p->vCutsNum );
    Vec_IntClear( p->vCutsStart );
    Vec_IntClear( p->vCutsObj );
    Vec_IntClear( p->vSolInit );
    Vec_IntClear( p->vSolCur );
    Vec_IntClear( p->vSolBest );
199 200 201 202
    Vec_WrdClear( p->vTempI1 );
    Vec_WrdClear( p->vTempI2 );
    Vec_WrdClear( p->vTempN1 );
    Vec_WrdClear( p->vTempN2 );
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
    // temporary
    Vec_IntClear( p->vLits );
    Vec_IntClear( p->vAssump );
    Vec_IntClear( p->vPolar );
    // other
    Gia_ManFillValue( p->pGia );
}
void Sbl_ManStop( Sbl_Man_t * p )
{
    sat_solver_delete( p->pSat );
    Vec_IntFree( p->vCardVars );
    // internal
    Vec_IntFree( p->vLeaves );
    Vec_IntFree( p->vAnds );
    Vec_IntFree( p->vNodes );
    Vec_IntFree( p->vRoots );
    Vec_IntFree( p->vRootVars );
220
    Hsh_VecManStop( p->pHash );
221 222 223 224 225 226 227
    // timing
    Vec_IntFree( p->vArrs );
    Vec_IntFree( p->vReqs );
    Vec_WecFree( p->vWindow );
    Vec_IntFree( p->vPath );
    Vec_IntFree( p->vEdges );
    // cuts
228 229 230 231
    Vec_WrdFree( p->vCutsI1 );
    Vec_WrdFree( p->vCutsI2 );
    Vec_WrdFree( p->vCutsN1 );
    Vec_WrdFree( p->vCutsN2 );
232 233 234 235 236 237
    Vec_IntFree( p->vCutsNum );
    Vec_IntFree( p->vCutsStart );
    Vec_IntFree( p->vCutsObj );
    Vec_IntFree( p->vSolInit );
    Vec_IntFree( p->vSolCur );
    Vec_IntFree( p->vSolBest );
238 239 240 241
    Vec_WrdFree( p->vTempI1 );
    Vec_WrdFree( p->vTempI2 );
    Vec_WrdFree( p->vTempN1 );
    Vec_WrdFree( p->vTempN2 );
242 243 244 245 246 247 248 249 250 251
    // temporary
    Vec_IntFree( p->vLits );
    Vec_IntFree( p->vAssump );
    Vec_IntFree( p->vPolar );
    // other
    ABC_FREE( p );
}

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

252
  Synopsis    [For each node in the window, create fanins.]
253 254 255 256 257 258 259 260

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
261
void Sbl_ManGetCurrentMapping( Sbl_Man_t * p )
262
{
263 264 265 266 267 268 269
    Vec_Int_t * vObj;
    word CutI1, CutI2, CutN1, CutN2;
    int i, c, b, iObj;
    Vec_WecClear( p->vWindow );
    Vec_WecInit( p->vWindow, Vec_IntSize(p->vAnds) );
    assert( Vec_IntSize(p->vSolCur) > 0 );
    Vec_IntForEachEntry( p->vSolCur, c, i )
270
    {
271 272 273 274 275 276 277
        CutI1 = Vec_WrdEntry( p->vCutsI1, c );
        CutI2 = Vec_WrdEntry( p->vCutsI2, c );
        CutN1 = Vec_WrdEntry( p->vCutsN1, c );
        CutN2 = Vec_WrdEntry( p->vCutsN2, c );
        iObj  = Vec_IntEntry( p->vCutsObj, c );
        //iObj  = Vec_IntEntry( p->vAnds, iObj );
        vObj  = Vec_WecEntry( p->vWindow, iObj );
278
        Vec_IntClear( vObj );
279 280 281 282 283 284 285 286 287 288 289 290 291
        assert( Vec_IntSize(vObj) == 0 );
        for ( b = 0; b < 64; b++ )
            if ( (CutI1 >> b) & 1 )
                Vec_IntPush( vObj, Vec_IntEntry(p->vLeaves, b) );
        for ( b = 0; b < 64; b++ )
            if ( (CutI2 >> b) & 1 )
                Vec_IntPush( vObj, Vec_IntEntry(p->vLeaves, 64+b) );
        for ( b = 0; b < 64; b++ )
            if ( (CutN1 >> b) & 1 )
                Vec_IntPush( vObj, Vec_IntEntry(p->vAnds, b) );
        for ( b = 0; b < 64; b++ )
            if ( (CutN2 >> b) & 1 )
                Vec_IntPush( vObj, Vec_IntEntry(p->vAnds, 64+b) );
292
    }
293
}
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314


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

  Synopsis    [Timing computation.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Sbl_ManComputeDelay( Sbl_Man_t * p, int iLut, Vec_Int_t * vFanins )
{
    int k, iFan, Delay = 0;
    Vec_IntForEachEntry( vFanins, iFan, k )
        Delay = Abc_MaxInt( Delay, Vec_IntEntry(p->vArrs, iFan) + 1 );
    return Delay;
}
int Sbl_ManCreateTiming( Sbl_Man_t * p, int DelayStart )
315 316
{
    Vec_Int_t * vFanins;
317
    int DelayMax = DelayStart, Delay, iLut, iFan, k;
318 319
    // compute arrival times
    Vec_IntFill( p->vArrs,  Gia_ManObjNum(p->pGia), 0 );
320
    if ( p->pGia->pManTime != NULL && Tim_ManBoxNum((Tim_Man_t*)p->pGia->pManTime) )
321
    {
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
        Gia_Obj_t * pObj; 
        Vec_Int_t * vNodes = Gia_ManOrderWithBoxes( p->pGia );
        Tim_ManIncrementTravId( (Tim_Man_t*)p->pGia->pManTime );
        Gia_ManForEachObjVec( vNodes, p->pGia, pObj, k )
        {
            iLut = Gia_ObjId( p->pGia, pObj );
            if ( Gia_ObjIsAnd(pObj) )
            {
                if ( Gia_ObjIsLut2(p->pGia, iLut) )
                {
                    vFanins = Gia_ObjLutFanins2(p->pGia, iLut);
                    Delay = Sbl_ManComputeDelay( p, iLut, vFanins );
                    Vec_IntWriteEntry( p->vArrs,  iLut, Delay );
                    DelayMax = Abc_MaxInt( DelayMax, Delay );
                }
            }
            else if ( Gia_ObjIsCi(pObj) )
            {
                int arrTime = Tim_ManGetCiArrival( (Tim_Man_t*)p->pGia->pManTime, Gia_ObjCioId(pObj) );
                Vec_IntWriteEntry( p->vArrs,  iLut, arrTime );
            }
            else if ( Gia_ObjIsCo(pObj) )
            {
                int arrTime = Vec_IntEntry( p->vArrs, Gia_ObjFaninId0(pObj, iLut) );
                Tim_ManSetCoArrival( (Tim_Man_t*)p->pGia->pManTime, Gia_ObjCioId(pObj), arrTime );
            }
            else if ( !Gia_ObjIsConst0(pObj) ) 
                assert( 0 );
        }
        Vec_IntFree( vNodes );
    }
    else
    {
        Gia_ManForEachLut2( p->pGia, iLut )
        {
            vFanins = Gia_ObjLutFanins2(p->pGia, iLut);
            Delay = Sbl_ManComputeDelay( p, iLut, vFanins );
            Vec_IntWriteEntry( p->vArrs,  iLut, Delay );
            DelayMax = Abc_MaxInt( DelayMax, Delay );
        }
362 363 364 365 366
    }
    // compute required times
    Vec_IntFill( p->vReqs, Gia_ManObjNum(p->pGia), ABC_INFINITY );
    Gia_ManForEachCoDriverId( p->pGia, iLut, k )
        Vec_IntDowndateEntry( p->vReqs, iLut, DelayMax );
367
    if ( p->pGia->pManTime != NULL && Tim_ManBoxNum((Tim_Man_t*)p->pGia->pManTime) )
368
    {
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
        Gia_Obj_t * pObj; 
        Vec_Int_t * vNodes = Gia_ManOrderWithBoxes( p->pGia );
        Tim_ManIncrementTravId( (Tim_Man_t*)p->pGia->pManTime );
        Tim_ManInitPoRequiredAll( (Tim_Man_t*)p->pGia->pManTime, DelayMax );
        Gia_ManForEachObjVecReverse( vNodes, p->pGia, pObj, k )
        {
            iLut = Gia_ObjId( p->pGia, pObj );
            if ( Gia_ObjIsAnd(pObj) )
            {
                if ( Gia_ObjIsLut2(p->pGia, iLut) )
                {
                    Delay = Vec_IntEntry(p->vReqs, iLut) - 1;
                    vFanins = Gia_ObjLutFanins2(p->pGia, iLut);
                    Vec_IntForEachEntry( vFanins, iFan, k )
                        Vec_IntDowndateEntry( p->vReqs, iFan, Delay );
                }
            }
            else if ( Gia_ObjIsCi(pObj) )
            {
                int reqTime = Vec_IntEntry( p->vReqs, iLut );
                Tim_ManSetCiRequired( (Tim_Man_t*)p->pGia->pManTime, Gia_ObjCioId(pObj), reqTime );
            }
            else if ( Gia_ObjIsCo(pObj) )
            {
                int reqTime = Tim_ManGetCoRequired( (Tim_Man_t*)p->pGia->pManTime, Gia_ObjCioId(pObj) );
                Vec_IntWriteEntry( p->vReqs, Gia_ObjFaninId0(pObj, iLut), reqTime );
            }
            else if ( !Gia_ObjIsConst0(pObj) ) 
                assert( 0 );
        }
        Vec_IntFree( vNodes );
    }
    else
    {
        Gia_ManForEachLut2Reverse( p->pGia, iLut )
        {
            Delay = Vec_IntEntry(p->vReqs, iLut) - 1;
            vFanins = Gia_ObjLutFanins2(p->pGia, iLut);
            Vec_IntForEachEntry( vFanins, iFan, k )
                Vec_IntDowndateEntry( p->vReqs, iFan, Delay );
        }
410
    }
411
    return DelayMax;
412 413
}

414

415 416
/**Function*************************************************************

417
  Synopsis    [Given mapping in p->vSolCur, check if mapping meets delay.]
418 419 420 421 422 423 424 425

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
426
int Sbl_ManEvaluateMappingEdge( Sbl_Man_t * p, int DelayGlo )
427
{
428 429 430 431 432 433 434
    abctime clk = Abc_Clock();
    Vec_Int_t * vArray; 
    int i, DelayMax;
    Vec_IntClear( p->vPath );
    // update new timing
    Sbl_ManGetCurrentMapping( p );
    // derive new timing
435
    DelayMax = Gia_ManEvalWindow( p->pGia, p->vLeaves, p->vAnds, p->vWindow, p->vPolar, 1 );
436 437 438 439 440 441
    p->timeTime += Abc_Clock() - clk;
    if ( DelayMax <= DelayGlo )
        return 1;
    // create critical path composed of all nodes
    Vec_WecForEachLevel( p->vWindow, vArray, i )
        if ( Vec_IntSize(vArray) > 0 )
442
            Vec_IntPush( p->vPath, Abc_Var2Lit(i, 1) );
443
    return 0;
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
}

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

  Synopsis    [Given mapping in p->vSolCur, check the critical path.]

  Description [Returns 1 if the mapping satisfies the timing. Returns 0, 
  if the critical path is detected.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Sbl_ManCriticalFanin( Sbl_Man_t * p, int iLut, Vec_Int_t * vFanins )
{
    int k, iFan, Delay = Vec_IntEntry(p->vArrs, iLut);
    Vec_IntForEachEntry( vFanins, iFan, k )
        if ( Vec_IntEntry(p->vArrs, iFan) + 1 == Delay )
            return iFan;
    return -1;
}
int Sbl_ManEvaluateMapping( Sbl_Man_t * p, int DelayGlo )
{
468
    abctime clk = Abc_Clock();
469
    Vec_Int_t * vFanins;
470
    int i, iLut = -1, iAnd, Delay, Required;
471 472
    if ( p->pGia->vEdge1 )
        return Sbl_ManEvaluateMappingEdge( p, DelayGlo );
473 474
    Vec_IntClear( p->vPath );
    // derive timing
475
    Sbl_ManCreateTiming( p, DelayGlo );
476 477 478 479 480
    // update new timing
    Sbl_ManGetCurrentMapping( p );
    Vec_IntForEachEntry( p->vAnds, iLut, i )
    {
        vFanins = Vec_WecEntry( p->vWindow, i );
481
        Delay   = Sbl_ManComputeDelay( p, iLut, vFanins );
482 483 484 485 486 487 488 489 490 491
        Vec_IntWriteEntry( p->vArrs, iLut, Delay );
    }
    // compare timing at the root nodes
    Vec_IntForEachEntry( p->vRoots, iLut, i )
    {
        Delay    = Vec_IntEntry( p->vArrs, iLut );
        Required = Vec_IntEntry( p->vReqs, iLut );
        if ( Delay > Required ) // updated timing exceeded original timing
            break;
    }
492
    p->timeTime += Abc_Clock() - clk;
493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520
    if ( i == Vec_IntSize(p->vRoots) )
        return 1;
    // derive the critical path

    // find SAT variable of the node whose GIA ID is "iLut"
    iAnd = Vec_IntFind( p->vAnds, iLut );
    assert( iAnd >= 0 );
    // critical path begins in node "iLut", which is i-th root of the window
    assert( iAnd == Vec_IntEntry(p->vRootVars, i) );
    while ( 1 )
    {
        Vec_IntPush( p->vPath, Abc_Var2Lit(iAnd, 1) );
        // find fanins of this node
        vFanins = Vec_WecEntry( p->vWindow, iAnd );
        // find critical fanin
        iLut = Sbl_ManCriticalFanin( p, iLut, vFanins );
        assert( iLut > 0  );
        // find SAT variable of the node whose GIA ID is "iLut"
        iAnd = Vec_IntFind( p->vAnds, iLut );
        if ( iAnd == -1 )
            break;
    }
    return 0;
}


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

521 522 523 524 525 526 527 528 529 530 531
  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Sbl_ManUpdateMapping( Sbl_Man_t * p )
{
532
//    Gia_Obj_t * pObj;
533
    Vec_Int_t * vObj;
534
    word CutI1, CutI2, CutN1, CutN2;
535
    int i, c, b, iObj, iTemp; 
536
    assert( Vec_IntSize(p->vSolBest) < Vec_IntSize(p->vSolInit) );
537 538 539 540 541 542 543
    Vec_IntForEachEntry( p->vAnds, iObj, i )
    {
        vObj = Vec_WecEntry(p->pGia->vMapping2, iObj);
        Vec_IntForEachEntry( vObj, iTemp, b )
            Gia_ObjLutRefDecId( p->pGia, iTemp );
        Vec_IntClear( vObj );
    }
544
    Vec_IntForEachEntry( p->vSolBest, c, i )
545
    {
546 547 548 549
        CutI1 = Vec_WrdEntry( p->vCutsI1, c );
        CutI2 = Vec_WrdEntry( p->vCutsI2, c );
        CutN1 = Vec_WrdEntry( p->vCutsN1, c );
        CutN2 = Vec_WrdEntry( p->vCutsN2, c );
550
        iObj = Vec_IntEntry( p->vCutsObj, c );
551 552
        iObj = Vec_IntEntry( p->vAnds, iObj );
        vObj = Vec_WecEntry( p->pGia->vMapping2, iObj );
553
        Vec_IntClear( vObj );
554 555
        assert( Vec_IntSize(vObj) == 0 );
        for ( b = 0; b < 64; b++ )
556
            if ( (CutI1 >> b) & 1 )
557 558
                Vec_IntPush( vObj, Vec_IntEntry(p->vLeaves, b) );
        for ( b = 0; b < 64; b++ )
559 560 561 562
            if ( (CutI2 >> b) & 1 )
                Vec_IntPush( vObj, Vec_IntEntry(p->vLeaves, 64+b) );
        for ( b = 0; b < 64; b++ )
            if ( (CutN1 >> b) & 1 )
563
                Vec_IntPush( vObj, Vec_IntEntry(p->vAnds, b) );
564 565 566
        for ( b = 0; b < 64; b++ )
            if ( (CutN2 >> b) & 1 )
                Vec_IntPush( vObj, Vec_IntEntry(p->vAnds, 64+b) );
567 568
        Vec_IntForEachEntry( vObj, iTemp, b )
            Gia_ObjLutRefIncId( p->pGia, iTemp );
569
    }
570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587
/*
    // verify
    Gia_ManForEachLut2Vec( p->pGia, vObj, i )
        Vec_IntForEachEntry( vObj, iTemp, b )
            Gia_ObjLutRefDecId( p->pGia, iTemp );
    Gia_ManForEachCo( p->pGia, pObj, i )
        Gia_ObjLutRefDecId( p->pGia, Gia_ObjFaninId0p(p->pGia, pObj) );

    for ( i = 0; i < Gia_ManObjNum(p->pGia); i++ )
        if ( p->pGia->pLutRefs[i] )
            printf( "Object %d has %d refs\n", i, p->pGia->pLutRefs[i] );

    Gia_ManForEachCo( p->pGia, pObj, i )
        Gia_ObjLutRefIncId( p->pGia, Gia_ObjFaninId0p(p->pGia, pObj) );
    Gia_ManForEachLut2Vec( p->pGia, vObj, i )
        Vec_IntForEachEntry( vObj, iTemp, b )
            Gia_ObjLutRefIncId( p->pGia, iTemp );
*/
588 589 590 591 592 593 594 595 596 597 598 599 600
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
601
static int Sbl_ManPrintCut( word CutI1, word CutI2, word CutN1, word CutN2 )
602 603 604 605
{
    int b, Count = 0; 
    printf( "{ " );
    for ( b = 0; b < 64; b++ )
606
        if ( (CutI1 >> b) & 1 )
607
            printf( "i%d ", b ), Count++;
608 609 610
    for ( b = 0; b < 64; b++ )
        if ( (CutI2 >> b) & 1 )
            printf( "i%d ", 64+b ), Count++;
611 612
    printf( " " );
    for ( b = 0; b < 64; b++ )
613
        if ( (CutN1 >> b) & 1 )
614
            printf( "n%d ", b ), Count++;
615 616 617
    for ( b = 0; b < 64; b++ )
        if ( (CutN2 >> b) & 1 )
            printf( "n%d ", 64+b ), Count++;
618 619 620 621 622
    printf( "};\n" );
    return Count;
}
static int Sbl_ManFindAndPrintCut( Sbl_Man_t * p, int c )
{
623
    return Sbl_ManPrintCut( Vec_WrdEntry(p->vCutsI1, c), Vec_WrdEntry(p->vCutsI2, c), Vec_WrdEntry(p->vCutsN1, c), Vec_WrdEntry(p->vCutsN2, c) );
624
}
625
static inline int Sbl_CutIsFeasible( word CutI1, word CutI2, word CutN1, word CutN2, int LutSize )
626
{
627
    int Count = (CutI1 != 0) + (CutI2 != 0) + (CutN1 != 0) + (CutN2 != 0);
628
    assert( LutSize <= 6 );
629 630 631 632
    CutI1 &= CutI1-1; CutI2 &= CutI2-1;   CutN1 &= CutN1-1; CutN2 &= CutN2-1;   Count += (CutI1 != 0) + (CutI2 != 0) + (CutN1 != 0) + (CutN2 != 0);
    CutI1 &= CutI1-1; CutI2 &= CutI2-1;   CutN1 &= CutN1-1; CutN2 &= CutN2-1;   Count += (CutI1 != 0) + (CutI2 != 0) + (CutN1 != 0) + (CutN2 != 0);
    CutI1 &= CutI1-1; CutI2 &= CutI2-1;   CutN1 &= CutN1-1; CutN2 &= CutN2-1;   Count += (CutI1 != 0) + (CutI2 != 0) + (CutN1 != 0) + (CutN2 != 0);  
    CutI1 &= CutI1-1; CutI2 &= CutI2-1;   CutN1 &= CutN1-1; CutN2 &= CutN2-1;   Count += (CutI1 != 0) + (CutI2 != 0) + (CutN1 != 0) + (CutN2 != 0);
633 634 635 636 637
    if ( LutSize <= 4 )
        return Count <= 4;
    CutI1 &= CutI1-1; CutI2 &= CutI2-1;   CutN1 &= CutN1-1; CutN2 &= CutN2-1;   Count += (CutI1 != 0) + (CutI2 != 0) + (CutN1 != 0) + (CutN2 != 0);  
    CutI1 &= CutI1-1; CutI2 &= CutI2-1;   CutN1 &= CutN1-1; CutN2 &= CutN2-1;   Count += (CutI1 != 0) + (CutI2 != 0) + (CutN1 != 0) + (CutN2 != 0);
    return Count <= 6;
638
}
639
static inline int Sbl_CutPushUncontained( Vec_Wrd_t * vCutsI1, Vec_Wrd_t * vCutsI2, Vec_Wrd_t * vCutsN1, Vec_Wrd_t * vCutsN2, word CutI1, word CutI2, word CutN1, word CutN2 )
640 641
{
    int i, k = 0;
642 643 644 645 646 647 648
    assert( vCutsI1->nSize == vCutsN1->nSize );
    assert( vCutsI2->nSize == vCutsN2->nSize );
    for ( i = 0; i < vCutsI1->nSize; i++ )
        if ( (vCutsI1->pArray[i] & CutI1) == vCutsI1->pArray[i] && 
             (vCutsI2->pArray[i] & CutI2) == vCutsI2->pArray[i] && 
             (vCutsN1->pArray[i] & CutN1) == vCutsN1->pArray[i] && 
             (vCutsN2->pArray[i] & CutN2) == vCutsN2->pArray[i]  )
649
            return 1;
650 651 652 653 654
    for ( i = 0; i < vCutsI1->nSize; i++ )
        if ( (vCutsI1->pArray[i] & CutI1) != CutI1 ||
             (vCutsI2->pArray[i] & CutI2) != CutI2 || 
             (vCutsN1->pArray[i] & CutN1) != CutN1 || 
             (vCutsN2->pArray[i] & CutN2) != CutN2 )
655
        {
656 657 658 659
            Vec_WrdWriteEntry( vCutsI1, k, vCutsI1->pArray[i] );
            Vec_WrdWriteEntry( vCutsI2, k, vCutsI2->pArray[i] );
            Vec_WrdWriteEntry( vCutsN1, k, vCutsN1->pArray[i] );
            Vec_WrdWriteEntry( vCutsN2, k, vCutsN2->pArray[i] );
660 661
            k++;
        }
662 663 664 665 666 667 668 669
    Vec_WrdShrink( vCutsI1, k );
    Vec_WrdShrink( vCutsI2, k );
    Vec_WrdShrink( vCutsN1, k );
    Vec_WrdShrink( vCutsN2, k );
    Vec_WrdPush( vCutsI1, CutI1 );
    Vec_WrdPush( vCutsI2, CutI2 );
    Vec_WrdPush( vCutsN1, CutN1 );
    Vec_WrdPush( vCutsN2, CutN2 );
670 671 672 673
    return 0;
}
static inline void Sbl_ManComputeCutsOne( Sbl_Man_t * p, int Fan0, int Fan1, int Obj )
{
674 675 676 677
    word * pCutsI1 = Vec_WrdArray(p->vCutsI1);
    word * pCutsI2 = Vec_WrdArray(p->vCutsI2);
    word * pCutsN1 = Vec_WrdArray(p->vCutsN1);
    word * pCutsN2 = Vec_WrdArray(p->vCutsN2);
678 679 680 681 682
    int Start0 = Vec_IntEntry( p->vCutsStart, Fan0 );
    int Start1 = Vec_IntEntry( p->vCutsStart, Fan1 );
    int Limit0 = Start0 + Vec_IntEntry( p->vCutsNum, Fan0 );
    int Limit1 = Start1 + Vec_IntEntry( p->vCutsNum, Fan1 );
    int i, k;
683 684 685 686 687
    assert( Obj >= 0 && Obj < 128 );
    Vec_WrdClear( p->vTempI1 );
    Vec_WrdClear( p->vTempI2 );
    Vec_WrdClear( p->vTempN1 );
    Vec_WrdClear( p->vTempN2 );
688 689
    for ( i = Start0; i < Limit0; i++ )
        for ( k = Start1; k < Limit1; k++ )
690
            if ( Sbl_CutIsFeasible(pCutsI1[i] | pCutsI1[k], pCutsI2[i] | pCutsI2[k], pCutsN1[i] | pCutsN1[k], pCutsN2[i] | pCutsN2[k], p->LutSize) )
691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710
                Sbl_CutPushUncontained( p->vTempI1, p->vTempI2, p->vTempN1, p->vTempN2, pCutsI1[i] | pCutsI1[k], pCutsI2[i] | pCutsI2[k], pCutsN1[i] | pCutsN1[k], pCutsN2[i] | pCutsN2[k] );
    Vec_IntPush( p->vCutsStart, Vec_WrdSize(p->vCutsI1) );
    Vec_IntPush( p->vCutsNum, Vec_WrdSize(p->vTempI1) + 1 );
    Vec_WrdAppend( p->vCutsI1, p->vTempI1 );
    Vec_WrdAppend( p->vCutsI2, p->vTempI2 );
    Vec_WrdAppend( p->vCutsN1, p->vTempN1 );
    Vec_WrdAppend( p->vCutsN2, p->vTempN2 );
    Vec_WrdPush( p->vCutsI1, 0 );
    Vec_WrdPush( p->vCutsI2, 0 );
    if ( Obj < 64 )
    {
        Vec_WrdPush( p->vCutsN1, (((word)1) << Obj) );
        Vec_WrdPush( p->vCutsN2, 0 );
    }
    else
    {
        Vec_WrdPush( p->vCutsN1, 0 );
        Vec_WrdPush( p->vCutsN2, (((word)1) << (Obj-64)) );
    }
    for ( i = 0; i <= Vec_WrdSize(p->vTempI1); i++ )
711 712
        Vec_IntPush( p->vCutsObj, Obj );
}
713
static inline int Sbl_ManFindCut( Sbl_Man_t * p, int Obj, word CutI1, word CutI2, word CutN1, word CutN2 )
714
{
715 716 717 718
    word * pCutsI1 = Vec_WrdArray(p->vCutsI1);
    word * pCutsI2 = Vec_WrdArray(p->vCutsI2);
    word * pCutsN1 = Vec_WrdArray(p->vCutsN1);
    word * pCutsN2 = Vec_WrdArray(p->vCutsN2);
719 720 721
    int Start0 = Vec_IntEntry( p->vCutsStart, Obj );
    int Limit0 = Start0 + Vec_IntEntry( p->vCutsNum, Obj );
    int i;
722
    //printf( "\nLooking for:\n" );
723
    //Sbl_ManPrintCut( CutI, CutN );
724
    //printf( "\n" );
725 726
    for ( i = Start0; i < Limit0; i++ )
    {
727
        //Sbl_ManPrintCut( pCutsI[i], pCutsN[i] );
728
        if ( pCutsI1[i] == CutI1 && pCutsI2[i] == CutI2 && pCutsN1[i] == CutN1 && pCutsN2[i] == CutN2 )
729 730 731 732 733 734
            return i;
    }
    return -1;
}
int Sbl_ManComputeCuts( Sbl_Man_t * p )
{
735
    abctime clk = Abc_Clock();
736 737
    Gia_Obj_t * pObj; Vec_Int_t * vFanins;
    int i, k, Index, Fanin, nObjs = Vec_IntSize(p->vLeaves) + Vec_IntSize(p->vAnds);
738
    assert( Vec_IntSize(p->vLeaves) <= 128 && Vec_IntSize(p->vAnds) <= p->nVars );
739 740 741 742
    // assign leaf cuts
    Vec_IntClear( p->vCutsStart );
    Vec_IntClear( p->vCutsObj );
    Vec_IntClear( p->vCutsNum );
743 744 745 746
    Vec_WrdClear( p->vCutsI1 );
    Vec_WrdClear( p->vCutsI2 );
    Vec_WrdClear( p->vCutsN1 );
    Vec_WrdClear( p->vCutsN2 );
747 748
    Gia_ManForEachObjVec( p->vLeaves, p->pGia, pObj, i )
    {
749
        Vec_IntPush( p->vCutsStart, Vec_WrdSize(p->vCutsI1) );
750 751
        Vec_IntPush( p->vCutsObj, -1 );
        Vec_IntPush( p->vCutsNum, 1 );
752 753 754 755 756 757 758 759 760 761 762 763
        if ( i < 64 )
        {
            Vec_WrdPush( p->vCutsI1, (((word)1) << i) );
            Vec_WrdPush( p->vCutsI2, 0 );
        }
        else
        {
            Vec_WrdPush( p->vCutsI1, 0 );
            Vec_WrdPush( p->vCutsI2, (((word)1) << (i-64)) );
        }
        Vec_WrdPush( p->vCutsN1, 0 );
        Vec_WrdPush( p->vCutsN2, 0 );
764 765 766
        pObj->Value = i;
    }
    // assign internal cuts
767
    Gia_ManForEachObjVec( p->vAnds, p->pGia, pObj, i )
768 769 770 771 772 773 774 775 776
    {
        assert( Gia_ObjIsAnd(pObj) );
        assert( ~Gia_ObjFanin0(pObj)->Value );
        assert( ~Gia_ObjFanin1(pObj)->Value );
        Sbl_ManComputeCutsOne( p, Gia_ObjFanin0(pObj)->Value, Gia_ObjFanin1(pObj)->Value, i );
        pObj->Value = Vec_IntSize(p->vLeaves) + i;
    }
    assert( Vec_IntSize(p->vCutsStart) == nObjs );
    assert( Vec_IntSize(p->vCutsNum)   == nObjs );
777 778 779
    assert( Vec_WrdSize(p->vCutsI1)    == Vec_WrdSize(p->vCutsN1) );
    assert( Vec_WrdSize(p->vCutsI2)    == Vec_WrdSize(p->vCutsN2) );
    assert( Vec_WrdSize(p->vCutsI1)    == Vec_IntSize(p->vCutsObj) );
780 781 782 783 784 785 786
    // check that roots are mapped nodes
    Vec_IntClear( p->vRootVars );
    Gia_ManForEachObjVec( p->vRoots, p->pGia, pObj, i )
    {
        int Obj = Gia_ObjId(p->pGia, pObj);
        if ( Gia_ObjIsCi(pObj) )
            continue;
787
        assert( Gia_ObjIsLut2(p->pGia, Obj) );
788 789 790 791 792
        assert( ~pObj->Value );
        Vec_IntPush( p->vRootVars, pObj->Value - Vec_IntSize(p->vLeaves) );
    }
    // create current solution
    Vec_IntClear( p->vPolar );
793
    Vec_IntClear( p->vSolInit );
794
    Gia_ManForEachObjVec( p->vAnds, p->pGia, pObj, i )
795
    {
796
        word CutI1 = 0, CutI2 = 0, CutN1 = 0, CutN2 = 0;
797
        int Obj = Gia_ObjId(p->pGia, pObj);
798
        if ( !Gia_ObjIsLut2(p->pGia, Obj) )
799 800 801 802
            continue;
        assert( (int)pObj->Value == Vec_IntSize(p->vLeaves) + i );
        // add node
        Vec_IntPush( p->vPolar, i );
803
        Vec_IntPush( p->vSolInit, i );
804
        // add its cut
805 806 807
        //Gia_LutForEachFaninObj( p->pGia, Obj, pFanin, k )
        vFanins = Gia_ObjLutFanins2( p->pGia, Obj );
        Vec_IntForEachEntry( vFanins, Fanin, k )
808
        {
809 810
            Gia_Obj_t * pFanin = Gia_ManObj( p->pGia, Fanin );
            assert( (int)pFanin->Value < Vec_IntSize(p->vLeaves) || Gia_ObjIsLut2(p->pGia, Fanin) );
811 812 813 814
//            if ( ~pFanin->Value == 0 )
//                Gia_ManPrintConeMulti( p->pGia, p->vAnds, p->vLeaves, p->vPath );
            if ( ~pFanin->Value == 0 ) 
                continue;
815 816
            assert( ~pFanin->Value );
            if ( (int)pFanin->Value < Vec_IntSize(p->vLeaves) )
817 818 819 820 821 822
            {
                if ( (int)pFanin->Value < 64 )
                    CutI1 |= ((word)1 << pFanin->Value);
                else
                    CutI2 |= ((word)1 << (pFanin->Value - 64));
            }
823
            else
824 825 826 827 828 829
            {
                if ( pFanin->Value - Vec_IntSize(p->vLeaves) < 64 )
                    CutN1 |= ((word)1 << (pFanin->Value - Vec_IntSize(p->vLeaves)));
                else
                    CutN2 |= ((word)1 << (pFanin->Value - Vec_IntSize(p->vLeaves) - 64));
            }
830 831
        }
        // find the new cut
832
        Index = Sbl_ManFindCut( p, Vec_IntSize(p->vLeaves) + i, CutI1, CutI2, CutN1, CutN2 );
833 834
        if ( Index < 0 )
        {
835
            //printf( "Cannot find the available cut.\n" );
836 837
            continue;
        }
838 839 840 841 842 843
        assert( Index >= 0 );
        Vec_IntPush( p->vPolar, p->FirstVar+Index );
    }
    // clean value
    Gia_ManForEachObjVec( p->vLeaves, p->pGia, pObj, i )
        pObj->Value = ~0;
844
    Gia_ManForEachObjVec( p->vAnds, p->pGia, pObj, i )
845
        pObj->Value = ~0;
846
    p->timeCut += Abc_Clock() - clk;
847
    return Vec_WrdSize(p->vCutsI1);
848 849 850 851
}
int Sbl_ManCreateCnf( Sbl_Man_t * p )
{
    int i, k, c, pLits[2], value;
852 853
    word * pCutsN1 = Vec_WrdArray(p->vCutsN1);
    word * pCutsN2 = Vec_WrdArray(p->vCutsN2);
854
    assert( p->FirstVar == sat_solver_nvars(p->pSat) );
855
    sat_solver_setnvars( p->pSat, sat_solver_nvars(p->pSat) + Vec_WrdSize(p->vCutsI1) );
856
    //printf( "\n" );
857
    for ( i = 0; i < Vec_IntSize(p->vAnds); i++ )
858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875
    {
        int Start0 = Vec_IntEntry( p->vCutsStart, Vec_IntSize(p->vLeaves) + i );
        int Limit0 = Start0 + Vec_IntEntry( p->vCutsNum, Vec_IntSize(p->vLeaves) + i ) - 1;
        // add main clause
        Vec_IntClear( p->vLits );
        Vec_IntPush( p->vLits, Abc_Var2Lit(i, 1) );
        //printf( "Node %d implies cuts: ", i );
        for ( k = Start0; k < Limit0; k++ )
        {
            Vec_IntPush( p->vLits, Abc_Var2Lit(p->FirstVar+k, 0) );
            //printf( "%d ", p->FirstVar+k );
        }
        //printf( "\n" );
        value = sat_solver_addclause( p->pSat, Vec_IntArray(p->vLits), Vec_IntLimit(p->vLits)  );
        assert( value );
        // binary clauses
        for ( k = Start0; k < Limit0; k++ )
        {
876 877
            word Cut1 = pCutsN1[k];
            word Cut2 = pCutsN2[k];
878 879 880 881 882 883 884
            //printf( "Cut %d implies root node %d.\n", p->FirstVar+k, i );
            // root clause
            pLits[0] = Abc_Var2Lit( p->FirstVar+k, 1 );
            pLits[1] = Abc_Var2Lit( i, 0 );
            value = sat_solver_addclause( p->pSat, pLits, pLits + 2 );
            assert( value );
            // fanin clauses
885
            for ( c = 0; c < 64 && Cut1; c++, Cut1 >>= 1 )
886
            {
887
                if ( (Cut1 & 1) == 0 )
888 889 890 891 892 893
                    continue;
                //printf( "Cut %d implies fanin %d.\n", p->FirstVar+k, c );
                pLits[1] = Abc_Var2Lit( c, 0 );
                value = sat_solver_addclause( p->pSat, pLits, pLits + 2 );
                assert( value );
            }
894 895 896 897 898 899 900 901 902
            for ( c = 0; c < 64 && Cut2; c++, Cut2 >>= 1 )
            {
                if ( (Cut2 & 1) == 0 )
                    continue;
                //printf( "Cut %d implies fanin %d.\n", p->FirstVar+k, c );
                pLits[1] = Abc_Var2Lit( c+64, 0 );
                value = sat_solver_addclause( p->pSat, pLits, pLits + 2 );
                assert( value );
            }
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
        }
    }
    sat_solver_set_polarity( p->pSat, Vec_IntArray(p->vPolar), Vec_IntSize(p->vPolar) );
    return 1;
}


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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

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

void Sbl_ManWindow( Sbl_Man_t * p )
{
    int i, ObjId;
    // collect leaves
    Vec_IntClear( p->vLeaves );
    Gia_ManForEachCiId( p->pGia, ObjId, i )
        Vec_IntPush( p->vLeaves, ObjId );
    // collect internal
930
    Vec_IntClear( p->vAnds );
931
    Gia_ManForEachAndId( p->pGia, ObjId )
932
        Vec_IntPush( p->vAnds, ObjId );
933 934 935 936 937 938
    // collect roots
    Vec_IntClear( p->vRoots );
    Gia_ManForEachCoDriverId( p->pGia, ObjId, i )
        Vec_IntPush( p->vRoots, ObjId );
}

939
int Sbl_ManWindow2( Sbl_Man_t * p, int iPivot )
940
{
941
    abctime clk = Abc_Clock();
942 943
    Vec_Int_t * vRoots, * vNodes, * vLeaves, * vAnds;
    int Count = Gia_ManComputeOneWin( p->pGia, iPivot, &vRoots, &vNodes, &vLeaves, &vAnds );
944
    p->timeWin += Abc_Clock() - clk;
945 946 947 948 949 950 951 952 953 954 955 956 957 958 959
    if ( Count == 0 )
        return 0;
    Vec_IntClear( p->vRoots );   Vec_IntAppend( p->vRoots,  vRoots );
    Vec_IntClear( p->vNodes );   Vec_IntAppend( p->vNodes,  vNodes );
    Vec_IntClear( p->vLeaves );  Vec_IntAppend( p->vLeaves, vLeaves );
    Vec_IntClear( p->vAnds );    Vec_IntAppend( p->vAnds,   vAnds );
//Vec_IntPrint( vRoots );
//Vec_IntPrint( vAnds );
//Vec_IntPrint( vLeaves );
    // recompute internal nodes
//    Gia_ManIncrementTravId( p->pGia );
//    Gia_ManCollectAnds( p->pGia, Vec_IntArray(p->vRoots), Vec_IntSize(p->vRoots), p->vAnds, p->vLeaves );
    return Count;
}

960
int Sbl_ManTestSat( Sbl_Man_t * p, int iPivot )
961 962
{
    int fKeepTrying = 1;
963
    abctime clk = Abc_Clock(), clk2;
964
    int i, status, Root, Count, StartSol, nConfTotal = 0, nIters = 0;
965
    int nEntries = Hsh_VecSize( p->pHash );
966
    p->nTried++;
967

968
    Sbl_ManClean( p );
969 970 971 972 973

    // compute one window
    Count = Sbl_ManWindow2( p, iPivot );
    if ( Count == 0 )
    {
974
        if ( p->fVeryVerbose )
975
        printf( "Obj %d: Window with less than %d nodes does not exist.\n", iPivot, p->nVars );
976
        p->nSmallWins++;
977 978
        return 0;
    }
979 980 981 982 983 984 985 986
    Hsh_VecManAdd( p->pHash, p->vAnds );
    if ( nEntries == Hsh_VecSize(p->pHash) )
    {
        if ( p->fVeryVerbose )
        printf( "Obj %d: This window was already tried.\n", iPivot );
        p->nHashWins++;
        return 0;
    }
987
    if ( p->fVeryVerbose )
988 989
    printf( "\nObj = %6d : Leaf = %2d.  AND = %2d.  Root = %2d.    LUT = %2d.\n", 
        iPivot, Vec_IntSize(p->vLeaves), Vec_IntSize(p->vAnds), Vec_IntSize(p->vRoots), Vec_IntSize(p->vNodes) ); 
990

991
    if ( Vec_IntSize(p->vLeaves) > 128 || Vec_IntSize(p->vAnds) > p->nVars )
992
    {
993
        if ( p->fVeryVerbose )
994
        printf( "Obj %d: Encountered window with %d inputs and %d internal nodes.\n", iPivot, Vec_IntSize(p->vLeaves), Vec_IntSize(p->vAnds) );
995
        p->nLargeWins++;
996 997
        return 0;
    }
998
    if ( Vec_IntSize(p->vAnds) < 10 )
999
    {
1000
        if ( p->fVeryVerbose )
1001 1002 1003
        printf( "Skipping.\n" );
        return 0;
    }
1004 1005 1006 1007 1008 1009

    // derive cuts
    Sbl_ManComputeCuts( p );
    // derive SAT instance
    Sbl_ManCreateCnf( p );

1010
    if ( p->fVeryVeryVerbose )
1011
    printf( "All clauses = %d.  Multi clauses = %d.  Binary clauses = %d.  Other clauses = %d.\n\n", 
1012 1013
        sat_solver_nclauses(p->pSat), Vec_IntSize(p->vAnds), Vec_WrdSize(p->vCutsI1) - Vec_IntSize(p->vAnds), 
        sat_solver_nclauses(p->pSat) - Vec_WrdSize(p->vCutsI1) );
1014 1015 1016 1017 1018 1019

    // create assumptions
    // cardinality
    Vec_IntClear( p->vAssump );
    Vec_IntPush( p->vAssump, -1 );
    // unused variables
1020
    for ( i = Vec_IntSize(p->vAnds); i < p->Power2; i++ )
1021 1022 1023 1024 1025 1026
        Vec_IntPush( p->vAssump, Abc_Var2Lit(i, 1) );
    // root variables
    Vec_IntForEachEntry( p->vRootVars, Root, i )
        Vec_IntPush( p->vAssump, Abc_Var2Lit(Root, 0) );
//    Vec_IntPrint( p->vAssump );

1027
    StartSol = Vec_IntSize(p->vSolInit) + 1;
1028 1029 1030
//    StartSol = 30;
    while ( fKeepTrying && StartSol-fKeepTrying > 0 )
    {
1031
        int Count = 0, LitCount = 0;
1032
        int nConfBef, nConfAft;
1033
        if ( p->fVeryVerbose )
1034
            printf( "Trying to find mapping with %d LUTs.\n", StartSol-fKeepTrying );
1035
    //    for ( i = Vec_IntSize(p->vSolInit)-5; i < nVars; i++ )
1036 1037 1038 1039
    //        Vec_IntPush( p->vAssump, Abc_Var2Lit(Vec_IntEntry(p->vCardVars, i), 1) );
        Vec_IntWriteEntry( p->vAssump, 0, Abc_Var2Lit(Vec_IntEntry(p->vCardVars, StartSol-fKeepTrying), 1) );
        // solve the problem
        clk2 = Abc_Clock();
1040
        nConfBef = (int)p->pSat->stats.conflicts;
1041 1042
        status = sat_solver_solve( p->pSat, Vec_IntArray(p->vAssump), Vec_IntLimit(p->vAssump), p->nBTLimit, 0, 0, 0 );
        p->timeSat += Abc_Clock() - clk2;
1043 1044
        nConfAft = (int)p->pSat->stats.conflicts;
        nConfTotal += nConfAft - nConfBef;
1045
        nIters++;
1046
        p->nRuns++;
1047 1048 1049 1050 1051 1052
        if ( status == l_True )
            p->timeSatSat += Abc_Clock() - clk2;
        else if ( status == l_False )
            p->timeSatUns += Abc_Clock() - clk2;
        else 
            p->timeSatUnd += Abc_Clock() - clk2;
1053 1054
        if ( status == l_Undef )
            break;
1055 1056
        if ( status == l_True )
        {
1057
            if ( p->fVeryVeryVerbose )
1058
            {
1059
                for ( i = 0; i < Vec_IntSize(p->vAnds); i++ )
1060 1061
                    printf( "%d", sat_solver_var_value(p->pSat, i) );
                printf( "\n" );
1062
                for ( i = 0; i < Vec_IntSize(p->vAnds); i++ )
1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073
                    if ( sat_solver_var_value(p->pSat, i) )
                    {
                        printf( "%d=%d ", i, sat_solver_var_value(p->pSat, i) );
                        Count++;
                    }
                printf( "Count = %d\n", Count );
            }
//            for ( i = p->FirstVar; i < sat_solver_nvars(p->pSat); i++ )
//                printf( "%d", sat_solver_var_value(p->pSat, i) );
//            printf( "\n" );
            Count = 1;
1074
            Vec_IntClear( p->vSolCur );
1075 1076 1077
            for ( i = p->FirstVar; i < sat_solver_nvars(p->pSat); i++ )
                if ( sat_solver_var_value(p->pSat, i) )
                {
1078
                    if ( p->fVeryVeryVerbose )
1079
                        printf( "Cut %3d : Node = %3d %6d  ", Count++, Vec_IntEntry(p->vCutsObj, i-p->FirstVar), Vec_IntEntry(p->vAnds, Vec_IntEntry(p->vCutsObj, i-p->FirstVar)) );
1080
                    if ( p->fVeryVeryVerbose )
1081
                        LitCount += Sbl_ManFindAndPrintCut( p, i-p->FirstVar );
1082
                    Vec_IntPush( p->vSolCur, i-p->FirstVar );
1083
                }
1084
            //Vec_IntPrint( p->vRootVars );
1085 1086 1087
            //Vec_IntPrint( p->vRoots );
            //Vec_IntPrint( p->vAnds );
            //Vec_IntPrint( p->vLeaves );
1088
        }
1089 1090 1091 1092 1093

//        fKeepTrying = status == l_True ? fKeepTrying + 1 : 0;
        // check the timing
        if ( status == l_True )
        {
1094
            if ( p->fDelay && !Sbl_ManEvaluateMapping(p, p->DelayMax) )
1095 1096
            {
                int iLit, value;
1097
                if ( p->fVeryVerbose )
1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116
                {
                    printf( "Critical path of length (%d) is detected:   ", Vec_IntSize(p->vPath) );
                    Vec_IntForEachEntry( p->vPath, iLit, i )
                        printf( "%d=%d ", i, Vec_IntEntry(p->vAnds, Abc_Lit2Var(iLit)) );
                    printf( "\n" );
                }
                // add the clause
                value = sat_solver_addclause( p->pSat, Vec_IntArray(p->vPath), Vec_IntLimit(p->vPath)  );
                assert( value );
            }
            else
            {
                Vec_IntClear( p->vSolBest );
                Vec_IntAppend( p->vSolBest, p->vSolCur );
                fKeepTrying++;
            }
        }
        else
            fKeepTrying = 0;
1117
        if ( p->fVeryVerbose )
1118 1119 1120 1121 1122 1123 1124
        {
            if ( status == l_False )
                printf( "UNSAT " );
            else if ( status == l_True )
                printf( "SAT   " );
            else 
                printf( "UNDEC " );
1125
            printf( "confl =%8d.    ", nConfAft - nConfBef );
1126
            Abc_PrintTime( 1, "Time", Abc_Clock() - clk2 );
1127 1128 1129 1130

            printf( "Total " );
            printf( "confl =%8d.    ", nConfTotal );
            Abc_PrintTime( 1, "Time", Abc_Clock() - clk );
1131
            if ( p->fVeryVeryVerbose && status == l_True )
1132
                printf( "LitCount = %d.\n", LitCount );
1133 1134
            printf( "\n" );
        }
1135
        if ( nIters == 10 )
1136
        {
1137 1138
            p->nIterOuts++;
            //printf( "Obj %d : Quitting after %d iterations.\n", iPivot, nIters );
1139 1140
            break;
        }
1141 1142 1143
    }

    // update solution
1144
    if ( Vec_IntSize(p->vSolBest) > 0 && Vec_IntSize(p->vSolBest) < Vec_IntSize(p->vSolInit) )
1145
    {
1146
        int nDelayCur, nEdgesCur = 0;
1147
        Sbl_ManUpdateMapping( p );
1148 1149 1150 1151 1152 1153 1154
        if ( p->pGia->vEdge1 )
        {
            nDelayCur = Gia_ManEvalEdgeDelay( p->pGia );
            nEdgesCur = Gia_ManEvalEdgeCount( p->pGia );
        }
        else
            nDelayCur = Sbl_ManCreateTiming( p, p->DelayMax );
1155
        if ( p->fVerbose )
1156
        printf( "Object %5d : Saved %2d nodes  (Conf =%8d)  Iter =%3d  Delay = %d  Edges = %4d\n", 
1157 1158
            iPivot, Vec_IntSize(p->vSolInit)-Vec_IntSize(p->vSolBest), nConfTotal, nIters, nDelayCur, nEdgesCur );
        p->timeTotal += Abc_Clock() - p->timeStart;
1159
        p->nImproved++;
1160 1161
        return 2;
    }
1162 1163 1164 1165
    else
    {
//        printf( "Object %5d : Saved %2d nodes  (Conf =%8d)  Iter =%3d\n", iPivot, 0, nConfTotal, nIters );
    }
1166
    p->timeTotal += Abc_Clock() - p->timeStart;
1167 1168
    return 1;
}
1169 1170
void Sbl_ManPrintRuntime( Sbl_Man_t * p )
{
1171
    printf( "Runtime breakdown:\n" );
1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182
    p->timeOther = p->timeTotal - p->timeWin - p->timeCut - p->timeSat - p->timeTime;
    ABC_PRTP( "Win   ", p->timeWin  ,   p->timeTotal );
    ABC_PRTP( "Cut   ", p->timeCut  ,   p->timeTotal );
    ABC_PRTP( "Sat   ", p->timeSat,     p->timeTotal );
    ABC_PRTP( " Sat  ", p->timeSatSat,  p->timeTotal );
    ABC_PRTP( " Unsat", p->timeSatUns,  p->timeTotal );
    ABC_PRTP( " Undec", p->timeSatUnd,  p->timeTotal );
    ABC_PRTP( "Timing", p->timeTime ,   p->timeTotal );
    ABC_PRTP( "Other ", p->timeOther,   p->timeTotal );
    ABC_PRTP( "ALL   ", p->timeTotal,   p->timeTotal );
}
1183
void Gia_ManLutSat( Gia_Man_t * pGia, int LutSize, int nNumber, int nImproves, int nBTLimit, int DelayMax, int nEdges, int fDelay, int fReverse, int fVerbose, int fVeryVerbose )
1184
{
1185 1186
    int iLut, nImproveCount = 0;
    Sbl_Man_t * p   = Sbl_ManAlloc( pGia, nNumber );
1187
    p->LutSize      = LutSize;      // LUT size
1188 1189 1190 1191 1192 1193
    p->nBTLimit     = nBTLimit;     // conflicts
    p->DelayMax     = DelayMax;     // external delay
    p->nEdges       = nEdges;       // the number of edges
    p->fDelay       = fDelay;       // delay mode
    p->fReverse     = fReverse;     // reverse windowing
    p->fVerbose     = fVerbose | fVeryVerbose;
1194 1195 1196
    p->fVeryVerbose = fVeryVerbose;
    if ( p->fVerbose )
    printf( "Parameters: WinSize = %d AIG nodes.  Conf = %d.  DelayMax = %d.\n", p->nVars, p->nBTLimit, p->DelayMax );
1197 1198 1199 1200
    // determine delay limit
    if ( fDelay && pGia->vEdge1 && p->DelayMax == 0 )
        p->DelayMax = Gia_ManEvalEdgeDelay( pGia );
    // iterate through the internal nodes
1201
    Gia_ManComputeOneWinStart( pGia, nNumber, fReverse );
1202 1203
    Gia_ManForEachLut2( pGia, iLut )
    {
1204
        if ( Sbl_ManTestSat( p, iLut ) != 2 )
1205
            continue;
1206 1207
        if ( ++nImproveCount == nImproves )
            break;
1208 1209
    }
    Gia_ManComputeOneWin( pGia, -1, NULL, NULL, NULL, NULL );
1210
    if ( p->fVerbose )
1211 1212
    printf( "Tried = %d. Used = %d. HashWin = %d. SmallWin = %d. LargeWin = %d. IterOut = %d.  SAT runs = %d.\n", 
        p->nTried, p->nImproved, p->nHashWins, p->nSmallWins, p->nLargeWins, p->nIterOuts, p->nRuns );
1213
    if ( p->fVerbose )
1214 1215
    Sbl_ManPrintRuntime( p );
    Sbl_ManStop( p );
1216
    Vec_IntFreeP( &pGia->vPacking );
1217 1218 1219 1220 1221 1222 1223 1224 1225
}

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


ABC_NAMESPACE_IMPL_END