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

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

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

#include "gia.h"
#include "misc/vec/vecSet.h"
23
#include "misc/vec/vecMem.h"
24
#include "misc/extra/extra.h"
25
#include "bool/kit/kit.h"
26
#include "misc/util/utilTruth.h"
27
#include "opt/dau/dau.h"
28
#include "sat/cnf/cnf.h"
29 30 31 32 33 34 35

ABC_NAMESPACE_IMPL_START

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

36
#define JF_LEAF_MAX   8
37
#define JF_WORD_MAX  ((JF_LEAF_MAX > 6) ? 1 << (JF_LEAF_MAX-6) : 1)
38
#define JF_CUT_MAX   16
39
#define JF_EPSILON 0.005
40 41 42 43

typedef struct Jf_Cut_t_ Jf_Cut_t; 
struct Jf_Cut_t_
{
44 45 46 47
    word             Sign;        // signature
    float            Flow;        // flow
    int              Time;        // arrival time
    int              iFunc;       // function 
48 49
    int              Cost;        // cut cost
    int              pCut[JF_LEAF_MAX+2]; // cut
50 51 52 53 54 55 56
};

typedef struct Jf_Man_t_ Jf_Man_t; 
struct Jf_Man_t_
{
    Gia_Man_t *      pGia;        // user's manager
    Jf_Par_t *       pPars;       // users parameter
57
    Sdm_Man_t *      pDsd;        // extern DSD manager
58
    Vec_Int_t *      vCnfs;       // costs of elementary CNFs
59
    Vec_Mem_t *      vTtMem;      // truth table memory and hash table
60 61 62 63 64 65 66 67 68 69
    Vec_Int_t        vCuts;       // cuts for each node
    Vec_Int_t        vArr;        // arrival time
    Vec_Int_t        vDep;        // departure time
    Vec_Flt_t        vFlow;       // area flow
    Vec_Flt_t        vRefs;       // ref counters
    Vec_Set_t        pMem;        // cut storage
    Vec_Int_t *      vTemp;       // temporary
    float (*pCutCmp) (Jf_Cut_t *, Jf_Cut_t *);// procedure to compare cuts
    abctime          clkStart;    // starting time
    word             CutCount[4]; // statistics
70
    int              nCoarse;     // coarse nodes
71 72
};

73 74 75 76 77 78 79 80 81 82 83 84 85 86
static inline int    Jf_ObjIsUnit( Gia_Obj_t * p )          { return !p->fMark0;                                       }
static inline void   Jf_ObjCleanUnit( Gia_Obj_t * p )       { assert(Jf_ObjIsUnit(p)); p->fMark0 = 1;                  }
static inline void   Jf_ObjSetUnit( Gia_Obj_t * p )         { p->fMark0 = 0;                                           }

static inline int    Jf_ObjCutH( Jf_Man_t * p, int i )      { return Vec_IntEntry(&p->vCuts, i);                       }
static inline int *  Jf_ObjCuts( Jf_Man_t * p, int i )      { return (int *)Vec_SetEntry(&p->pMem, Jf_ObjCutH(p, i));  }
static inline int *  Jf_ObjCutBest( Jf_Man_t * p, int i )   { return Jf_ObjCuts(p, i) + 1;                             }
static inline int    Jf_ObjArr( Jf_Man_t * p, int i )       { return Vec_IntEntry(&p->vArr, i);                        }
static inline int    Jf_ObjDep( Jf_Man_t * p, int i )       { return Vec_IntEntry(&p->vDep, i);                        }
static inline float  Jf_ObjFlow( Jf_Man_t * p, int i )      { return Vec_FltEntry(&p->vFlow, i);                       }
static inline float  Jf_ObjRefs( Jf_Man_t * p, int i )      { return Vec_FltEntry(&p->vRefs, i);                       }
//static inline int    Jf_ObjLit( int i, int c )            { return i;                                                }
static inline int    Jf_ObjLit( int i, int c )              { return Abc_Var2Lit( i, c );                              }

87
static inline int    Jf_CutSize( int * pCut )               { return pCut[0] & 0xF;                                    }  //  4 bits
88 89 90
static inline int    Jf_CutCost( int * pCut )               { return (pCut[0] >> 4) & 0xF;                             }  //  4 bits
static inline int    Jf_CutFunc( int * pCut )               { return ((unsigned)pCut[0] >> 8);                         }  // 24 bits
static inline int    Jf_CutSetAll( int f, int c, int s )    { return (f << 8) | (c << 4) | s;                          }
91
static inline void   Jf_CutSetSize( int * pCut, int s )     { assert(s>=0 && s<16); pCut[0] ^= (Jf_CutSize(pCut) ^ s); }
92 93
static inline void   Jf_CutSetCost( int * pCut, int c )     { assert(c>=0 && c<16); pCut[0] ^=((Jf_CutCost(pCut) ^ c) << 4); }
static inline void   Jf_CutSetFunc( int * pCut, int f )     { assert(f>=0); pCut[0] ^=((Jf_CutFunc(pCut) ^ f) << 8);   }
94

95 96 97 98 99 100 101
static inline int    Jf_CutFuncClass( int * pCut )          { return Abc_Lit2Var(Jf_CutFunc(pCut));                    }
static inline int    Jf_CutFuncCompl( int * pCut )          { return Abc_LitIsCompl(Jf_CutFunc(pCut));                 }
static inline int *  Jf_CutLits( int * pCut )               { return pCut + 1;                                         }
static inline int    Jf_CutLit( int * pCut, int i )         { assert(i);return pCut[i];                                }
//static inline int    Jf_CutVar( int * pCut, int i )         { assert(i); return pCut[i];                               }
static inline int    Jf_CutVar( int * pCut, int i )         {  assert(i);return Abc_Lit2Var(pCut[i]);                  }
static inline int    Jf_CutIsTriv( int * pCut, int i )      { return Jf_CutSize(pCut) == 1 && Jf_CutVar(pCut, 1) == i; } 
102 103
static inline int    Jf_CutCnfSizeF( Jf_Man_t * p, int f )  { return Vec_IntEntry( p->vCnfs, f );                      }
static inline int    Jf_CutCnfSize( Jf_Man_t * p, int * c ) { return Jf_CutCnfSizeF( p, Jf_CutFuncClass(c) );          }
104

105 106
static inline int    Jf_ObjFunc0( Gia_Obj_t * p, int * c )  { return Abc_LitNotCond(Jf_CutFunc(c), Gia_ObjFaninC0(p)); } 
static inline int    Jf_ObjFunc1( Gia_Obj_t * p, int * c )  { return Abc_LitNotCond(Jf_CutFunc(c), Gia_ObjFaninC1(p)); } 
107 108

#define Jf_ObjForEachCut( pList, pCut, i )   for ( i = 0, pCut = pList + 1; i < pList[0]; i++, pCut += Jf_CutSize(pCut) + 1 )
109
#define Jf_CutForEachLit( pCut, Lit, i )     for ( i = 1; i <= Jf_CutSize(pCut) && (Lit = Jf_CutLit(pCut, i)); i++ )
110
#define Jf_CutForEachVar( pCut, Var, i )     for ( i = 1; i <= Jf_CutSize(pCut) && (Var = Jf_CutVar(pCut, i)); i++ )
111

112 113
extern int Kit_TruthToGia( Gia_Man_t * pMan, unsigned * pTruth, int nVars, Vec_Int_t * vMemory, Vec_Int_t * vLeaves, int fHash );

114 115 116 117 118 119
////////////////////////////////////////////////////////////////////////
///                     FUNCTION DEFINITIONS                         ///
////////////////////////////////////////////////////////////////////////

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

120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
  Synopsis    [Derives CNF for the mapped GIA.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Jf_ManGenCnf( word uTruth, int iLitOut, Vec_Int_t * vLeaves, Vec_Int_t * vLits, Vec_Int_t * vClas, Vec_Int_t * vCover )
{
    if ( uTruth == 0 || ~uTruth == 0 )
    {
        Vec_IntPush( vClas, Vec_IntSize(vLits) );
        Vec_IntPush( vLits, Abc_LitNotCond(iLitOut, (uTruth == 0)) );
    }
    else 
    {
        int i, k, c, Literal, Cube;
        assert( Vec_IntSize(vLeaves) > 0 );
        for ( c = 0; c < 2; c ++ )
        {
            int RetValue = Kit_TruthIsop( (unsigned *)&uTruth, Vec_IntSize(vLeaves), vCover, 0 );
            assert( RetValue == 0 );
            Vec_IntForEachEntry( vCover, Cube, i )
            {
                Vec_IntPush( vClas, Vec_IntSize(vLits) );
                Vec_IntPush( vLits, Abc_LitNotCond(iLitOut, c) );
                for ( k = 0; k < Vec_IntSize(vLeaves); k++ )
                {
                    Literal = 3 & (Cube >> (k << 1));
                    if ( Literal == 1 )      // '0'  -> pos lit
                        Vec_IntPush( vLits, Abc_LitNotCond(Vec_IntEntry(vLeaves, k), 0) );
                    else if ( Literal == 2 ) // '1'  -> neg lit
                        Vec_IntPush( vLits, Abc_LitNotCond(Vec_IntEntry(vLeaves, k), 1) );
                    else if ( Literal != 0 )
                        assert( 0 );
                }
            }
            uTruth = ~uTruth;
        }
    }
}
163
Cnf_Dat_t * Jf_ManCreateCnfRemap( Gia_Man_t * p, Vec_Int_t * vLits, Vec_Int_t * vClas, int fAddOrCla )
164 165 166 167
{
    Cnf_Dat_t * pCnf; 
    Gia_Obj_t * pObj;
    int i, Entry, * pMap, nVars = 0;
168 169 170 171 172 173
    if ( fAddOrCla )
    {
        Vec_IntPush( vClas, Vec_IntSize(vLits) );
        Gia_ManForEachPo( p, pObj, i )
            Vec_IntPush( vLits, Abc_Var2Lit(Gia_ObjId(p, pObj), 0) );
    }
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
    // label nodes present in the mapping
    Vec_IntForEachEntry( vLits, Entry, i )
        Gia_ManObj(p, Abc_Lit2Var(Entry))->fMark0 = 1;
    // create variable map
    pMap = ABC_FALLOC( int, Gia_ManObjNum(p) );
    Gia_ManForEachObjReverse( p, pObj, i )
        if ( pObj->fMark0 )
            pObj->fMark0 = 0, pMap[i] = nVars++;
    // relabel literals
    Vec_IntForEachEntry( vLits, Entry, i )
        Vec_IntWriteEntry( vLits, i, Abc_Lit2LitV(pMap, Entry) );
    // generate CNF
    pCnf = ABC_CALLOC( Cnf_Dat_t, 1 );
    pCnf->pMan = (Aig_Man_t *)p;
    pCnf->nVars = nVars;
    pCnf->nLiterals = Vec_IntSize(vLits);
    pCnf->nClauses = Vec_IntSize(vClas);
    pCnf->pClauses = ABC_ALLOC( int *, pCnf->nClauses+1 );
    pCnf->pClauses[0] = Vec_IntReleaseArray(vLits);
    Vec_IntForEachEntry( vClas, Entry, i )
        pCnf->pClauses[i] = pCnf->pClauses[0] + Entry;
    pCnf->pClauses[i] = pCnf->pClauses[0] + pCnf->nLiterals;
    pCnf->pVarNums = pMap;
    return pCnf;
}
Alan Mishchenko committed
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
Cnf_Dat_t * Jf_ManCreateCnf( Gia_Man_t * p, Vec_Int_t * vLits, Vec_Int_t * vClas )
{
    Cnf_Dat_t * pCnf; 
    int i, Entry, iOut;
    // generate CNF
    pCnf = ABC_CALLOC( Cnf_Dat_t, 1 );
    pCnf->pMan = (Aig_Man_t *)p;
    pCnf->nVars = Gia_ManObjNum(p);
    pCnf->nLiterals = Vec_IntSize(vLits);
    pCnf->nClauses = Vec_IntSize(vClas);
    pCnf->pClauses = ABC_ALLOC( int *, pCnf->nClauses+1 );
    pCnf->pClauses[0] = Vec_IntReleaseArray(vLits);
    Vec_IntForEachEntry( vClas, Entry, i )
        pCnf->pClauses[i] = pCnf->pClauses[0] + Entry;
    pCnf->pClauses[i] = pCnf->pClauses[0] + pCnf->nLiterals;
    // create mapping of objects into their clauses
    pCnf->pObj2Clause = ABC_FALLOC( int, Gia_ManObjNum(p) );
    pCnf->pObj2Count  = ABC_FALLOC( int, Gia_ManObjNum(p) );
    for ( i = 0; i < pCnf->nClauses; i++ )
    {
        iOut = Abc_Lit2Var(pCnf->pClauses[i][0]);
        if ( pCnf->pObj2Clause[iOut] == -1 )
        {
            pCnf->pObj2Clause[iOut] = i;
            pCnf->pObj2Count[iOut] = 1;
        }
        else
        {
            assert( pCnf->pObj2Count[iOut] > 0 );
            pCnf->pObj2Count[iOut]++;
        }
    }
    return pCnf;
}
233 234 235

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

236 237 238 239 240 241 242 243 244
  Synopsis    [Computing references while discounting XOR/MUX.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
245
float * Jf_ManInitRefs( Jf_Man_t * pMan )
246
{
247
    Gia_Man_t * p = pMan->pGia;
248 249 250 251 252 253 254
    Gia_Obj_t * pObj, * pCtrl, * pData0, * pData1;
    float * pRes; int i;
    assert( p->pRefs == NULL );
    p->pRefs = ABC_CALLOC( int, Gia_ManObjNum(p) );
    Gia_ManForEachAnd( p, pObj, i )
    {
        Gia_ObjRefFanin0Inc( p, pObj );
255
        if ( Gia_ObjIsBuf(pObj) )
256 257 258 259 260 261 262 263 264 265 266 267
            continue;
        Gia_ObjRefFanin1Inc( p, pObj );
        if ( !Gia_ObjIsMuxType(pObj) )
            continue;
        // discount XOR/MUX
        pCtrl = Gia_ObjRecognizeMux( pObj, &pData1, &pData0 );
        Gia_ObjRefDec( p, Gia_Regular(pCtrl) );
        if ( Gia_Regular(pData1) == Gia_Regular(pData0) )
            Gia_ObjRefDec( p, Gia_Regular(pData1) );
    }
    Gia_ManForEachCo( p, pObj, i )
        Gia_ObjRefFanin0Inc( p, pObj );
268 269 270 271 272 273
    // mark XOR/MUX internal nodes, which are not used elsewhere
    if ( pMan->pPars->fCoarsen )
    {
        pMan->nCoarse = 0;
        Gia_ManForEachAnd( p, pObj, i )
        {
274
            if ( !Gia_ObjIsMuxType(pObj) )
275
                continue;
276 277 278 279
            if ( Gia_ObjRefNum(p, Gia_ObjFanin0(pObj)) == 1 )
            {
                Jf_ObjSetUnit(Gia_ObjFanin0(Gia_ObjFanin0(pObj)));
                Jf_ObjSetUnit(Gia_ObjFanin0(Gia_ObjFanin1(pObj)));
280
                Jf_ObjCleanUnit(Gia_ObjFanin0(pObj)), pMan->nCoarse++;
281 282 283 284 285
            }
            if ( Gia_ObjRefNum(p, Gia_ObjFanin1(pObj)) == 1 )
            {
                Jf_ObjSetUnit(Gia_ObjFanin1(Gia_ObjFanin0(pObj)));
                Jf_ObjSetUnit(Gia_ObjFanin1(Gia_ObjFanin1(pObj)));
286
                Jf_ObjCleanUnit(Gia_ObjFanin1(pObj)), pMan->nCoarse++;
287
            }
288 289
        }
    }
290 291 292 293 294 295 296 297 298
    // multiply by factor
    pRes = ABC_ALLOC( float, Gia_ManObjNum(p) );
    for ( i = 0; i < Gia_ManObjNum(p); i++ )
        pRes[i] = Abc_MaxInt( 1, p->pRefs[i] );
    return pRes;
}

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

299 300 301 302 303 304 305 306 307 308 309 310
  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Jf_ManProfileClasses( Jf_Man_t * p )
{
    Gia_Obj_t * pObj; 
311 312
    int Counts[595] = {0}, Costs[595] = {0};
    int i, iFunc, Total = 0, CostTotal = 0, Other = 0, CostOther = 0;
313
    printf( "DSD classes that appear in more than %.1f %% of mapped nodes:\n", 0.1 * p->pPars->nVerbLimit );
314
    Gia_ManForEachAnd( p->pGia, pObj, i )
315
        if ( !Gia_ObjIsBuf(pObj) && Gia_ObjRefNumId(p->pGia, i) )
316
        {
317
            iFunc = Jf_CutFuncClass( Jf_ObjCutBest(p, i) );
318
            assert( iFunc < 595 );
319 320 321 322 323
            if ( p->pPars->fGenCnf )
            {
                Costs[iFunc] += Jf_CutCnfSizeF(p, iFunc);
                CostTotal += Jf_CutCnfSizeF(p, iFunc);
            }
324 325 326
            Counts[iFunc]++;
            Total++;
        }
327 328
    CostTotal = Abc_MaxInt(CostTotal, 1);
    Total = Abc_MaxInt(Total, 1);
329
    for ( i = 0; i < 595; i++ )
330
        if ( Counts[i] && 100.0 * Counts[i] / Total >= 0.1 * p->pPars->nVerbLimit )
331 332 333 334
        {
            printf( "%5d  :  ", i );
            printf( "%-20s   ", Sdm_ManReadDsdStr(p->pDsd, i) );
            printf( "%8d  ",    Counts[i] );
335 336 337
            printf( "%5.1f %%   ", 100.0 * Counts[i] / Total );
            printf( "%8d  ",    Costs[i] );
            printf( "%5.1f %%", 100.0 * Costs[i] / CostTotal );
338 339 340
            printf( "\n" );
        }
        else
341
        {
342
            Other += Counts[i];
343 344
            CostOther += Costs[i];
        }
345 346 347
    printf( "Other  :  " );
    printf( "%-20s   ",   "" );
    printf( "%8d  ",    Other );
348 349 350
    printf( "%5.1f %%   ", 100.0 * Other / Total );
    printf( "%8d  ",    CostOther );
    printf( "%5.1f %%", 100.0 * CostOther / CostTotal );
351 352 353 354 355
    printf( "\n" );
}

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

356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
  Synopsis    [Manager manipulation.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Jf_Man_t * Jf_ManAlloc( Gia_Man_t * pGia, Jf_Par_t * pPars )
{
    Jf_Man_t * p;
    assert( pPars->nLutSize <= JF_LEAF_MAX );
    assert( pPars->nCutNum <= JF_CUT_MAX );
    Vec_IntFreeP( &pGia->vMapping );
    p = ABC_CALLOC( Jf_Man_t, 1 );
    p->pGia      = pGia;
    p->pPars     = pPars;
374
    if ( pPars->fCutMin && !pPars->fFuncDsd )
375
        p->vTtMem = Vec_MemAllocForTT( pPars->nLutSize, 0 );
376
    else if ( pPars->fCutMin && pPars->fFuncDsd )
377 378 379 380 381 382 383 384
    {
        p->pDsd = Sdm_ManRead();
        if ( pPars->fGenCnf )
        {
            p->vCnfs = Vec_IntStart( 595 );
            Sdm_ManReadCnfCosts( p->pDsd, Vec_IntArray(p->vCnfs), Vec_IntSize(p->vCnfs) );
        }
    }
385 386 387 388 389
    Vec_IntFill( &p->vCuts, Gia_ManObjNum(pGia), 0 );
    Vec_IntFill( &p->vArr,  Gia_ManObjNum(pGia), 0 );
    Vec_IntFill( &p->vDep,  Gia_ManObjNum(pGia), 0 );
    Vec_FltFill( &p->vFlow, Gia_ManObjNum(pGia), 0 );
    p->vRefs.nCap = p->vRefs.nSize = Gia_ManObjNum(pGia);
390
    p->vRefs.pArray = Jf_ManInitRefs( p );
391 392 393 394 395 396 397
    Vec_SetAlloc_( &p->pMem, 20 );
    p->vTemp     = Vec_IntAlloc( 1000 );
    p->clkStart  = Abc_Clock();
    return p;
}
void Jf_ManFree( Jf_Man_t * p )
{
398
    if ( p->pPars->fVerbose && p->pDsd )
399
        Sdm_ManPrintDsdStats( p->pDsd, 0 );
400
    if ( p->pPars->fVerbose && p->vTtMem )
401 402 403 404
    {
        printf( "Unique truth tables = %d. Memory = %.2f MB   ", Vec_MemEntryNum(p->vTtMem), Vec_MemMemory(p->vTtMem) / (1<<20) ); 
        Abc_PrintTime( 1, "Time", Abc_Clock() - p->clkStart );
    }
405 406
    if ( p->pPars->fVeryVerbose && p->pPars->fCutMin && p->pPars->fFuncDsd )
        Jf_ManProfileClasses( p );
407 408
    if ( p->pPars->fCoarsen )
        Gia_ManCleanMark0( p->pGia );
409 410 411 412 413 414
    ABC_FREE( p->pGia->pRefs );
    ABC_FREE( p->vCuts.pArray );
    ABC_FREE( p->vArr.pArray );
    ABC_FREE( p->vDep.pArray );
    ABC_FREE( p->vFlow.pArray );
    ABC_FREE( p->vRefs.pArray );
415
    if ( p->pPars->fCutMin && !p->pPars->fFuncDsd )
416 417 418 419
    {
        Vec_MemHashFree( p->vTtMem );
        Vec_MemFree( p->vTtMem );
    }
420
    Vec_IntFreeP( &p->vCnfs );
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
    Vec_SetFree_( &p->pMem );
    Vec_IntFreeP( &p->vTemp );
    ABC_FREE( p );
}

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

  Synopsis    [Cut functions.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline void Jf_CutPrint( int * pCut )
{
    int i; 
440 441 442
    printf( "%d {", Jf_CutSize(pCut) );
    for ( i = 1; i <= Jf_CutSize(pCut); i++ )
        printf( " %d", Jf_CutLit(pCut, i) );
443
    printf( " } Func = %d\n", Jf_CutFunc(pCut) );
444 445 446 447 448 449 450 451
}
static inline void Jf_ObjCutPrint( int * pCuts )
{
    int i, * pCut; 
    Jf_ObjForEachCut( pCuts, pCut, i )
        Jf_CutPrint( pCut );
    printf( "\n" );
}
452 453 454 455 456
static inline void Jf_ObjBestCutConePrint( Jf_Man_t * p, Gia_Obj_t * pObj )
{
    int * pCut = Jf_ObjCutBest( p, Gia_ObjId(p->pGia, pObj) );
    printf( "Best cut of node %d : ", Gia_ObjId(p->pGia, pObj) );
    Jf_CutPrint( pCut );
457 458 459 460 461 462 463 464
    Gia_ManPrintCone( p->pGia, pObj, Jf_CutLits(pCut), Jf_CutSize(pCut), p->vTemp );
}
static inline void Jf_CutCheck( int * pCut )
{
    int i, k;
    for ( i = 2; i <= Jf_CutSize(pCut); i++ )
        for ( k = 1; k < i; k++ )
            assert( Jf_CutLit(pCut, i) != Jf_CutLit(pCut, k) );
465
}
466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487
static inline int Jf_CountBitsSimple( unsigned n )
{
    int i, Count = 0;
    for ( i = 0; i < 32; i++ )
        Count += ((n >> i) & 1);
    return Count;
}
static inline int Jf_CountBits32( unsigned i )
{
    i = i - ((i >> 1) & 0x55555555);
    i = (i & 0x33333333) + ((i >> 2) & 0x33333333);
    i = ((i + (i >> 4)) & 0x0F0F0F0F);
    return (i*(0x01010101))>>24;
}
static inline int Jf_CountBits( word i )
{
    i = i - ((i >> 1) & 0x5555555555555555);
    i = (i & 0x3333333333333333) + ((i >> 2) & 0x3333333333333333);
    i = ((i + (i >> 4)) & 0x0F0F0F0F0F0F0F0F);
    return (i*(0x0101010101010101))>>56;
}
static inline unsigned Jf_CutGetSign32( int * pCut )
488 489
{
    unsigned Sign = 0; int i; 
490 491
    for ( i = 1; i <= Jf_CutSize(pCut); i++ )
        Sign |= 1 << (Jf_CutVar(pCut, i) & 0x1F);
492 493
    return Sign;
}
494
static inline word Jf_CutGetSign( int * pCut )
495
{
496 497 498 499
    word Sign = 0; int i; 
    for ( i = 1; i <= Jf_CutSize(pCut); i++ )
        Sign |= ((word)1) << (Jf_CutVar(pCut, i) & 0x3F);
    return Sign;
500
}
501
static inline int Jf_CutArr( Jf_Man_t * p, int * pCut )
502
{
503 504 505 506
    int i, Time = 0;
    for ( i = 1; i <= Jf_CutSize(pCut); i++ )
        Time = Abc_MaxInt( Time, Jf_ObjArr(p, Jf_CutVar(pCut, i)) );
    return Time + 1; 
507
}
508

509
static inline void Jf_ObjSetBestCut( int * pCuts, int * pCut, Vec_Int_t * vTemp )
510
{
511 512
    assert( pCuts < pCut );
    if ( ++pCuts < pCut )
513
    {
514 515 516 517 518 519
        int nBlock = pCut - pCuts;
        int nSize = Jf_CutSize(pCut) + 1;
        Vec_IntGrow( vTemp, nBlock );
        memmove( Vec_IntArray(vTemp), pCuts, sizeof(int) * nBlock );
        memmove( pCuts, pCut, sizeof(int) * nSize );
        memmove( pCuts + nSize, Vec_IntArray(vTemp), sizeof(int) * nBlock );
520
    }
521
}
522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553
static inline void Jf_CutRef( Jf_Man_t * p, int * pCut )
{
    int i;
    for ( i = 1; i <= Jf_CutSize(pCut); i++ )
        Gia_ObjRefIncId( p->pGia, Jf_CutVar(pCut, i) );
}
static inline void Jf_CutDeref( Jf_Man_t * p, int * pCut )
{
    int i;
    for ( i = 1; i <= Jf_CutSize(pCut); i++ )
        Gia_ObjRefDecId( p->pGia, Jf_CutVar(pCut, i) );
}
static inline float Jf_CutFlow( Jf_Man_t * p, int * pCut )
{
    float Flow = 0; int i; 
    for ( i = 1; i <= Jf_CutSize(pCut); i++ )
        Flow += Jf_ObjFlow( p, Jf_CutVar(pCut, i) );
    assert( Flow >= 0 );
    return Flow; 
}

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

  Synopsis    [Cut merging.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578
static inline int Jf_CutIsContainedOrder( int * pBase, int * pCut ) // check if pCut is contained pBase
{
    int nSizeB = Jf_CutSize(pBase);
    int nSizeC = Jf_CutSize(pCut);
    int i, k;
    if ( nSizeB == nSizeC )
    {
        for ( i = 1; i <= nSizeB; i++ )
            if ( pBase[i] != pCut[i] )
                return 0;
        return 1;
    }
    assert( nSizeB > nSizeC ); 
    for ( i = k = 1; i <= nSizeB; i++ )
    {
        if ( pBase[i] > pCut[k] )
            return 0;
        if ( pBase[i] == pCut[k] )
        {
            if ( k++ == nSizeC )
                return 1;
        }
    }
    return 0;
}
579
static inline int Jf_CutMergeOrder( int * pCut0, int * pCut1, int * pCut, int LutSize )
580
{ 
581 582
    int nSize0 = Jf_CutSize(pCut0);
    int nSize1 = Jf_CutSize(pCut1);
583 584 585 586 587
    int * pC0 = pCut0 + 1;
    int * pC1 = pCut1 + 1;
    int * pC = pCut + 1;
    int i, k, c, s;
    // the case of the largest cut sizes
588
    if ( nSize0 == LutSize && nSize1 == LutSize )
589
    {
590
        for ( i = 0; i < nSize0; i++ )
591 592 593 594 595 596 597 598 599 600
        {
            if ( pC0[i] != pC1[i] )
                return 0;
            pC[i] = pC0[i];
        }
        pCut[0] = LutSize;
        return 1;
    }
    // compare two cuts with different numbers
    i = k = c = s = 0;
601 602
    if ( nSize0 == 0 ) goto FlushCut1;
    if ( nSize1 == 0 ) goto FlushCut0;
603 604 605 606 607 608
    while ( 1 )
    {
        if ( c == LutSize ) return 0;
        if ( pC0[i] < pC1[k] )
        {
            pC[c++] = pC0[i++];
609
            if ( i >= nSize0 ) goto FlushCut1;
610 611 612 613
        }
        else if ( pC0[i] > pC1[k] )
        {
            pC[c++] = pC1[k++];
614
            if ( k >= nSize1 ) goto FlushCut0;
615 616 617 618
        }
        else
        {
            pC[c++] = pC0[i++]; k++;
619 620
            if ( i >= nSize0 ) goto FlushCut1;
            if ( k >= nSize1 ) goto FlushCut0;
621 622 623 624
        }
    }

FlushCut0:
625 626
    if ( c + nSize0 > LutSize + i ) return 0;
    while ( i < nSize0 )
627 628 629 630 631
        pC[c++] = pC0[i++];
    pCut[0] = c;
    return 1;

FlushCut1:
632 633
    if ( c + nSize1 > LutSize + k ) return 0;
    while ( k < nSize1 )
634 635 636
        pC[c++] = pC1[k++];
    pCut[0] = c;
    return 1;
637
}
638 639 640 641 642 643 644 645 646 647 648 649 650

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

  Synopsis    [Cut merging.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline int Jf_CutFindLeaf0( int * pCut, int iObj )
651
{
652 653
    int i, nLits = Jf_CutSize(pCut);
    for ( i = 1; i <= nLits; i++ )
654 655 656
        if ( pCut[i] == iObj )
            return i;
    return i;
657
}
658
static inline int Jf_CutIsContained0( int * pBase, int * pCut ) // check if pCut is contained pBase
659
{
660 661
    int i, nLits = Jf_CutSize(pCut);
    for ( i = 1; i <= nLits; i++ )
662 663 664
        if ( Jf_CutFindLeaf0(pBase, pCut[i]) > pBase[0] )
            return 0;
    return 1;
665
}
666
static inline int Jf_CutMerge0( int * pCut0, int * pCut1, int * pCut, int LutSize )
667
{
668 669 670 671 672 673 674 675 676 677 678
    int nSize0 = Jf_CutSize(pCut0);
    int nSize1 = Jf_CutSize(pCut1), i;
    pCut[0] = nSize0;
    for ( i = 1; i <= nSize1; i++ )
        if ( Jf_CutFindLeaf0(pCut0, pCut1[i]) > nSize0 )
        {
            if ( pCut[0] == LutSize )
                return 0;
            pCut[++pCut[0]] = pCut1[i];
        }
    memcpy( pCut + 1, pCut0 + 1, sizeof(int) * nSize0 );
679
    return 1;
680
}
681 682 683 684 685 686 687 688 689 690 691 692

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

  Synopsis    [Cut merging.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
693
static inline int Jf_CutFindLeaf1( int * pCut, int iLit )
694
{
695 696
    int i, nLits = Jf_CutSize(pCut);
    for ( i = 1; i <= nLits; i++ )
697 698 699
        if ( Abc_Lit2Var(pCut[i]) == iLit )
            return i;
    return i;
700
}
701
static inline int Jf_CutIsContained1( int * pBase, int * pCut ) // check if pCut is contained pBase
702
{
703 704
    int i, nLits = Jf_CutSize(pCut);
    for ( i = 1; i <= nLits; i++ )
705
        if ( Jf_CutFindLeaf1(pBase, Abc_Lit2Var(pCut[i])) > pBase[0] )
706 707
            return 0;
    return 1;
708
}
709
static inline int Jf_CutMerge1( int * pCut0, int * pCut1, int * pCut, int LutSize )
710
{
711 712 713 714
    int nSize0 = Jf_CutSize(pCut0);
    int nSize1 = Jf_CutSize(pCut1), i;
    pCut[0] = nSize0;
    for ( i = 1; i <= nSize1; i++ )
715
        if ( Jf_CutFindLeaf1(pCut0, Abc_Lit2Var(pCut1[i])) > nSize0 )
716 717 718 719 720
        {
            if ( pCut[0] == LutSize )
                return 0;
            pCut[++pCut[0]] = pCut1[i];
        }
721
    memcpy( pCut + 1, pCut0 + 1, sizeof(int) * nSize0 );
722 723
    return 1;
}
724
static inline int Jf_CutMerge2( int * pCut0, int * pCut1, int * pCut, int LutSize )
725 726
{
    int ConfigMask = 0x3FFFF; // 18 bits
727 728
    int nSize0 = Jf_CutSize(pCut0);
    int nSize1 = Jf_CutSize(pCut1);
729
    int i, iPlace;
730 731
    pCut[0] = nSize0;
    for ( i = 1; i <= nSize1; i++ )
732
    {
733
        iPlace = Jf_CutFindLeaf1(pCut0, Abc_Lit2Var(pCut1[i]));
734
        if ( iPlace > nSize0 )
735 736 737
        {
            if ( pCut[0] == LutSize )
                return 0;
738
            pCut[(iPlace = ++pCut[0])] = pCut1[i];
739
        }
740
        else if ( pCut0[iPlace] != pCut1[i] )
741
            ConfigMask |= (1 << (iPlace+17));
742
        ConfigMask ^= (((i-1) ^ 7) << (3*(iPlace-1)));
743
    }
744 745
    memcpy( pCut + 1, pCut0 + 1, sizeof(int) * nSize0 );
    return ConfigMask;
746 747 748 749 750 751 752 753 754 755 756 757 758 759 760
}


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

  Synopsis    [Cut filtering.]

  Description [Returns the number of cuts after filtering and the last
  cut in the last entry.  If the cut is filtered, its size is set to -1.]
               
  SideEffects [This was found to be 15% slower.]

  SeeAlso     []

***********************************************************************/
761
int Jf_ObjCutFilterBoth( Jf_Man_t * p, Jf_Cut_t ** pSto, int c )
762 763 764 765 766 767
{
    int k, last;
    // filter this cut using other cuts
    for ( k = 0; k < c; k++ )
        if ( pSto[c]->pCut[0] >= pSto[k]->pCut[0] && 
            (pSto[c]->Sign & pSto[k]->Sign) == pSto[k]->Sign && 
768
             Jf_CutIsContained1(pSto[c]->pCut, pSto[k]->pCut) )
769 770 771 772 773 774 775 776
        {
                pSto[c]->pCut[0] = -1;
                return c;
        }
    // filter other cuts using this cut
    for ( k = last = 0; k < c; k++ )
        if ( !(pSto[c]->pCut[0] < pSto[k]->pCut[0] && 
              (pSto[c]->Sign & pSto[k]->Sign) == pSto[c]->Sign && 
777
               Jf_CutIsContained1(pSto[k]->pCut, pSto[c]->pCut)) )
778 779 780 781 782 783 784 785 786 787
        {
            if ( last++ == k )
                continue;
            ABC_SWAP( Jf_Cut_t *, pSto[last-1], pSto[k] );
        }
    assert( last <= c );
    if ( last < c )
        ABC_SWAP( Jf_Cut_t *, pSto[last], pSto[c] );
    return last;
}
788 789 790 791 792 793 794 795
int Jf_ObjCutFilter( Jf_Man_t * p, Jf_Cut_t ** pSto, int c )
{
    int k;
    if ( p->pPars->fCutMin )
    {
        for ( k = 0; k < c; k++ )
            if ( pSto[c]->pCut[0] >= pSto[k]->pCut[0] && 
                (pSto[c]->Sign & pSto[k]->Sign) == pSto[k]->Sign && 
796
                 Jf_CutIsContained1(pSto[c]->pCut, pSto[k]->pCut) )
797 798 799 800 801 802 803 804 805 806 807 808
                 return 0;
    }
    else
    {
        for ( k = 0; k < c; k++ )
            if ( pSto[c]->pCut[0] >= pSto[k]->pCut[0] && 
                (pSto[c]->Sign & pSto[k]->Sign) == pSto[k]->Sign && 
                 Jf_CutIsContainedOrder(pSto[c]->pCut, pSto[k]->pCut) )
                 return 0;
    }
    return 1;
}
809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831

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

  Synopsis    [Sorting cuts by size.]

  Description []
               
  SideEffects [Did not really help.]

  SeeAlso     []

***********************************************************************/
static inline void Jf_ObjSortCuts( Jf_Cut_t ** pSto, int nSize )
{
    int i, j, best_i;
    for ( i = 0; i < nSize-1; i++ )
    {
        best_i = i;
        for ( j = i+1; j < nSize; j++ )
            if ( pSto[j]->pCut[0] < pSto[best_i]->pCut[0] )
                best_i = j;
        ABC_SWAP( Jf_Cut_t *, pSto[i], pSto[best_i] );
    }
832 833 834 835 836 837 838 839 840 841 842 843 844
}

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

  Synopsis    [Reference counting.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
845
int Jf_CutRef_rec( Jf_Man_t * p, int * pCut )
846
{
847
    int i, Var, Count = Jf_CutCost(pCut);
848
    Jf_CutForEachVar( pCut, Var, i )
849 850 851
        if ( !Gia_ObjRefIncId(p->pGia, Var) && !Jf_CutIsTriv(Jf_ObjCutBest(p, Var), Var) )
            Count += Jf_CutRef_rec( p, Jf_ObjCutBest(p, Var) );
    return Count;
852
}
853
int Jf_CutDeref_rec( Jf_Man_t * p, int * pCut )
854
{
855
    int i, Var, Count = Jf_CutCost(pCut);
856
    Jf_CutForEachVar( pCut, Var, i )
857 858 859
        if ( !Gia_ObjRefDecId(p->pGia, Var) && !Jf_CutIsTriv(Jf_ObjCutBest(p, Var), Var) )
            Count += Jf_CutDeref_rec( p, Jf_ObjCutBest(p, Var) );
    return Count;
860
}
861
static inline int Jf_CutAreaOld( Jf_Man_t * p, int * pCut )
862
{
863 864 865
    int Ela1, Ela2;
    Ela1 = Jf_CutRef_rec( p, pCut );
    Ela2 = Jf_CutDeref_rec( p, pCut );
866 867 868
    assert( Ela1 == Ela2 );
    return Ela1;
}
869

870
int Jf_CutAreaRef_rec( Jf_Man_t * p, int * pCut )
871 872 873 874
{
    int i, Var, Count = Jf_CutCost(pCut);
    Jf_CutForEachVar( pCut, Var, i )
    {
875 876
        if ( !Gia_ObjRefIncId(p->pGia, Var) && !Jf_CutIsTriv(Jf_ObjCutBest(p, Var), Var) )
            Count += Jf_CutAreaRef_rec( p, Jf_ObjCutBest(p, Var) );
877 878 879 880
        Vec_IntPush( p->vTemp, Var );
    }
    return Count;
}
881
int Jf_CutAreaRefEdge_rec( Jf_Man_t * p, int * pCut )
882
{
883
    int i, Var, Count = (Jf_CutCost(pCut) << 4) | Jf_CutSize(pCut);
884
    Jf_CutForEachVar( pCut, Var, i )
885
    {
886 887
        if ( !Gia_ObjRefIncId(p->pGia, Var) && !Jf_CutIsTriv(Jf_ObjCutBest(p, Var), Var) )
            Count += Jf_CutAreaRefEdge_rec( p, Jf_ObjCutBest(p, Var) );
888
        Vec_IntPush( p->vTemp, Var );
889
    }
890
    return Count;
891
}
892
static inline int Jf_CutArea( Jf_Man_t * p, int * pCut, int fEdge )
893 894 895
{
    int Ela, Entry, i;
    Vec_IntClear( p->vTemp );
896
    if ( fEdge )
897
        Ela = Jf_CutAreaRefEdge_rec( p, pCut );
898
    else
899
        Ela = Jf_CutAreaRef_rec( p, pCut );
900 901 902 903
    Vec_IntForEachEntry( p->vTemp, Entry, i )
        Gia_ObjRefDecId( p->pGia, Entry );
    return Ela;
}
904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927
// returns 1 if MFFC size is less than limit
int Jf_CutCheckMffc_rec( Jf_Man_t * p, int * pCut, int Limit )
{
    int i, Var;
    Jf_CutForEachVar( pCut, Var, i )
    {
        int fRecur = (!Gia_ObjRefDecId(p->pGia, Var) && !Jf_CutIsTriv(Jf_ObjCutBest(p, Var), Var));
        Vec_IntPush( p->vTemp, Var );
        if ( Vec_IntSize(p->vTemp) >= Limit )
            return 0;
        if ( fRecur && !Jf_CutCheckMffc_rec( p, Jf_ObjCutBest(p, Var), Limit ) )
            return 0;
    }
    return 1;
}
static inline int Jf_CutCheckMffc( Jf_Man_t * p, int * pCut, int Limit )
{
    int RetValue, Entry, i;
    Vec_IntClear( p->vTemp );
    RetValue = Jf_CutCheckMffc_rec( p, pCut, Limit );
    Vec_IntForEachEntry( p->vTemp, Entry, i )
        Gia_ObjRefIncId( p->pGia, Entry );
    return RetValue;
}
928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943

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

  Synopsis    [Comparison procedures.]

  Description [Return positive value if the new cut is better than the old cut.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
float Jf_CutCompareDelay( Jf_Cut_t * pOld, Jf_Cut_t * pNew )
{
    if ( pOld->Time    != pNew->Time    ) return pOld->Time    - pNew->Time;
    if ( pOld->pCut[0] != pNew->pCut[0] ) return pOld->pCut[0] - pNew->pCut[0];
944 945 946
//    if ( pOld->Flow    != pNew->Flow    ) return pOld->Flow    - pNew->Flow;
    if ( pOld->Flow < pNew->Flow - JF_EPSILON ) return -1;
    if ( pOld->Flow > pNew->Flow + JF_EPSILON ) return 1;
947 948 949 950
    return 0;
}
float Jf_CutCompareArea( Jf_Cut_t * pOld, Jf_Cut_t * pNew )
{
951 952 953
//    if ( pOld->Flow    != pNew->Flow    ) return pOld->Flow    - pNew->Flow;
    if ( pOld->Flow < pNew->Flow - JF_EPSILON ) return -1;
    if ( pOld->Flow > pNew->Flow + JF_EPSILON ) return 1;
954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973
    if ( pOld->pCut[0] != pNew->pCut[0] ) return pOld->pCut[0] - pNew->pCut[0];
    if ( pOld->Time    != pNew->Time    ) return pOld->Time    - pNew->Time;
    return 0;
}
static inline int Jf_ObjAddCutToStore( Jf_Man_t * p, Jf_Cut_t ** pSto, int c, int cMax )
{
    Jf_Cut_t * pTemp;
    int k, last, iPivot;
    // if the store is empty, add anything
    if ( c == 0 )
        return 1;
    // special case when the cut store is full and last cut is better than new cut
    if ( c == cMax && p->pCutCmp(pSto[c-1], pSto[c]) <= 0 )
        return c;
    // find place of the given cut in the store
    assert( c <= cMax );
    for ( iPivot = c-1; iPivot >= 0; iPivot-- )
        if ( p->pCutCmp(pSto[iPivot], pSto[c]) < 0 ) // iPivot-th cut is better than new cut
            break;
    // filter this cut using other cuts
974 975 976 977 978
    if ( p->pPars->fCutMin )
    {
        for ( k = 0; k <= iPivot; k++ )
            if ( pSto[c]->pCut[0] >= pSto[k]->pCut[0] && 
                (pSto[c]->Sign & pSto[k]->Sign) == pSto[k]->Sign && 
979
                 Jf_CutIsContained1(pSto[c]->pCut, pSto[k]->pCut) )
980 981 982 983 984 985 986 987 988 989
                    return c;
    }
    else
    {
        for ( k = 0; k <= iPivot; k++ )
            if ( pSto[c]->pCut[0] >= pSto[k]->pCut[0] && 
                (pSto[c]->Sign & pSto[k]->Sign) == pSto[k]->Sign && 
                 Jf_CutIsContainedOrder(pSto[c]->pCut, pSto[k]->pCut) )
                    return c;
    }
990 991 992 993 994 995
    // insert this cut after iPivot
    pTemp = pSto[c];
    for ( ++iPivot, k = c++; k > iPivot; k-- )
        pSto[k] = pSto[k-1];
    pSto[iPivot] = pTemp;
    // filter other cuts using this cut
996 997 998 999 1000
    if ( p->pPars->fCutMin )
    {
        for ( k = last = iPivot+1; k < c; k++ )
            if ( !(pSto[iPivot]->pCut[0] <= pSto[k]->pCut[0] && 
                  (pSto[iPivot]->Sign & pSto[k]->Sign) == pSto[iPivot]->Sign && 
1001
                   Jf_CutIsContained1(pSto[k]->pCut, pSto[iPivot]->pCut)) )
1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019
            {
                if ( last++ == k )
                    continue;
                ABC_SWAP( Jf_Cut_t *, pSto[last-1], pSto[k] );
            }
    }
    else
    {
        for ( k = last = iPivot+1; k < c; k++ )
            if ( !(pSto[iPivot]->pCut[0] <= pSto[k]->pCut[0] && 
                  (pSto[iPivot]->Sign & pSto[k]->Sign) == pSto[iPivot]->Sign && 
                   Jf_CutIsContainedOrder(pSto[k]->pCut, pSto[iPivot]->pCut)) )
            {
                if ( last++ == k )
                    continue;
                ABC_SWAP( Jf_Cut_t *, pSto[last-1], pSto[k] );
            }
    }
1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030
    c = last;
    // remove the last cut if too many
    if ( c == cMax + 1 )
        return c - 1;
    return c;
}
static inline void Jf_ObjPrintStore( Jf_Man_t * p, Jf_Cut_t ** pSto, int c )
{
    int i;
    for ( i = 0; i < c; i++ )
    {
1031
        printf( "Flow =%9.5f  ", pSto[i]->Flow );
1032
        printf( "Time = %5d   ", pSto[i]->Time );
1033
        printf( "Func = %5d   ", pSto[i]->iFunc );
1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053
        printf( "  " );
        Jf_CutPrint( pSto[i]->pCut );
    }
    printf( "\n" );
}
static inline void Jf_ObjCheckPtrs( Jf_Cut_t ** pSto, int c )
{
    int i, k;
    for ( i = 1; i < c; i++ )
        for ( k = 0; k < i; k++ )
            assert( pSto[k] != pSto[i] );
}
static inline void Jf_ObjCheckStore( Jf_Man_t * p, Jf_Cut_t ** pSto, int c, int iObj )
{
    int i, k;
    for ( i = 1; i < c; i++ )
        assert( p->pCutCmp(pSto[i-1], pSto[i]) <= 0 );
    for ( i = 1; i < c; i++ )
        for ( k = 0; k < i; k++ )
        {
1054 1055
            assert( !Jf_CutIsContained1(pSto[k]->pCut, pSto[i]->pCut) );
            assert( !Jf_CutIsContained1(pSto[i]->pCut, pSto[k]->pCut) );
1056 1057 1058 1059 1060
        }
}

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

1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071
  Synopsis    [Cut minimization.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Jf_TtComputeForCut( Jf_Man_t * p, int iFuncLit0, int iFuncLit1, int * pCut0, int * pCut1, int * pCutOut )
{
1072 1073
    word uTruth[JF_WORD_MAX], uTruth0[JF_WORD_MAX], uTruth1[JF_WORD_MAX];
    int fCompl, truthId;
1074
    int LutSize    = p->pPars->nLutSize;
1075
    int nWords     = Abc_Truth6WordNum(p->pPars->nLutSize);
1076 1077
    word * pTruth0 = Vec_MemReadEntry(p->vTtMem, Abc_Lit2Var(iFuncLit0));
    word * pTruth1 = Vec_MemReadEntry(p->vTtMem, Abc_Lit2Var(iFuncLit1));
1078 1079
    Abc_TtCopy( uTruth0, pTruth0, nWords, Abc_LitIsCompl(iFuncLit0) );
    Abc_TtCopy( uTruth1, pTruth1, nWords, Abc_LitIsCompl(iFuncLit1) );
1080 1081
    Abc_TtExpand( uTruth0, LutSize, pCut0 + 1, Jf_CutSize(pCut0), pCutOut + 1, Jf_CutSize(pCutOut) );
    Abc_TtExpand( uTruth1, LutSize, pCut1 + 1, Jf_CutSize(pCut1), pCutOut + 1, Jf_CutSize(pCutOut) );
1082 1083 1084 1085 1086
    fCompl         = (int)(uTruth0[0] & uTruth1[0] & 1);
    Abc_TtAnd( uTruth, uTruth0, uTruth1, nWords, fCompl );
    pCutOut[0]     = Abc_TtMinBase( uTruth, pCutOut + 1, pCutOut[0], LutSize );
    assert( (uTruth[0] & 1) == 0 );
    truthId        = Vec_MemHashInsert(p->vTtMem, uTruth);
1087 1088 1089 1090 1091
    return Abc_Var2Lit( truthId, fCompl );
}

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

1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103
  Synopsis    [Cut enumeration.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline void Jf_ObjAssignCut( Jf_Man_t * p, Gia_Obj_t * pObj )
{
    int iObj = Gia_ObjId(p->pGia, pObj);
1104
    int pClause[3] = { 1, Jf_CutSetAll(2, 0, 1), Jf_ObjLit(iObj, 0) }; // set function
1105
    assert( Gia_ObjIsCi(pObj) || Gia_ObjIsBuf(pObj) );
1106
    Vec_IntWriteEntry( &p->vCuts, iObj, Vec_SetAppend( &p->pMem, pClause, 3 ) );
1107 1108 1109 1110 1111
}
static inline void Jf_ObjPropagateBuf( Jf_Man_t * p, Gia_Obj_t * pObj, int fReverse )
{
    int iObj = Gia_ObjId( p->pGia, pObj );
    int iFanin = Gia_ObjFaninId0( pObj, iObj );
1112
    assert( 0 );
1113
    assert( Gia_ObjIsBuf(pObj) );
1114 1115 1116 1117 1118
    if ( fReverse )
        ABC_SWAP( int, iObj, iFanin );
    Vec_IntWriteEntry( &p->vArr,  iObj, Jf_ObjArr(p, iFanin) );
    Vec_FltWriteEntry( &p->vFlow, iObj, Jf_ObjFlow(p, iFanin) );
}
1119
static inline int Jf_ObjHasCutWithSize( Jf_Cut_t ** pSto, int c, int nSize )
1120
{
1121 1122 1123 1124 1125
    int i;
    for ( i = 0; i < c; i++ )
        if ( pSto[i]->pCut[0] <= nSize )
            return 1;
    return 0;
1126
}
1127
void Jf_ObjComputeCuts( Jf_Man_t * p, Gia_Obj_t * pObj, int fEdge )
1128 1129 1130 1131
{
    int        LutSize = p->pPars->nLutSize;
    int        CutNum = p->pPars->nCutNum;
    int        iObj = Gia_ObjId(p->pGia, pObj);
1132 1133 1134 1135
    word       Sign0[JF_CUT_MAX+2]; // signatures of the first cut
    word       Sign1[JF_CUT_MAX+2]; // signatures of the second cut
    Jf_Cut_t   Sto[JF_CUT_MAX+2];   // cut storage
    Jf_Cut_t * pSto[JF_CUT_MAX+2];  // pointers to cut storage
1136
    int *      pCut0, * pCut1, * pCuts0, * pCuts1;
1137
    int        nOldSupp, Config, i, k, c = 0;
1138
    // prepare cuts
1139
    for ( i = 0; i <= CutNum+1; i++ )
1140
        pSto[i] = Sto + i, pSto[i]->Cost = 0, pSto[i]->iFunc = ~0;
1141 1142 1143 1144 1145 1146 1147 1148 1149
    // compute signatures
    pCuts0 = Jf_ObjCuts( p, Gia_ObjFaninId0(pObj, iObj) );
    Jf_ObjForEachCut( pCuts0, pCut0, i )
        Sign0[i] = Jf_CutGetSign( pCut0 );
    // compute signatures
    pCuts1 = Jf_ObjCuts( p, Gia_ObjFaninId1(pObj, iObj) );
    Jf_ObjForEachCut( pCuts1, pCut1, i )
        Sign1[i] = Jf_CutGetSign( pCut1 );
    // merge cuts
1150
    p->CutCount[0] += pCuts0[0] * pCuts1[0];
1151 1152 1153 1154 1155
    Jf_ObjForEachCut( pCuts0, pCut0, i )
    Jf_ObjForEachCut( pCuts1, pCut1, k )
    {
        if ( Jf_CountBits(Sign0[i] | Sign1[k]) > LutSize )
            continue;
1156
        p->CutCount[1]++;        
1157
        if ( !p->pPars->fCutMin )
1158
        {
1159
            if ( !Jf_CutMergeOrder(pCut0, pCut1, pSto[c]->pCut, LutSize) )
1160
                continue;
1161
            pSto[c]->Sign = Sign0[i] | Sign1[k];
1162
        }
1163
        else if ( p->pPars->fFuncDsd )
1164
        {
1165
            if ( !(Config = Jf_CutMerge2(pCut0, pCut1, pSto[c]->pCut, LutSize)) )
1166
                continue;
1167 1168
            pSto[c]->Sign = Sign0[i] | Sign1[k];
            nOldSupp = pSto[c]->pCut[0];
1169 1170 1171 1172 1173
            pSto[c]->iFunc = Sdm_ManComputeFunc( p->pDsd, Jf_ObjFunc0(pObj, pCut0), Jf_ObjFunc1(pObj, pCut1), pSto[c]->pCut, Config, 0 );
            if ( pSto[c]->iFunc == -1 )
                continue;
            if ( p->pPars->fGenCnf && Jf_CutCnfSizeF(p, Abc_Lit2Var(pSto[c]->iFunc)) >= 12 ) // no more than 15
                continue;
1174 1175 1176
            assert( pSto[c]->pCut[0] <= nOldSupp );
            if ( pSto[c]->pCut[0] < nOldSupp )
                pSto[c]->Sign = Jf_CutGetSign( pSto[c]->pCut );
1177
        }
1178 1179
        else
        {
1180
            if ( !Jf_CutMergeOrder(pCut0, pCut1, pSto[c]->pCut, LutSize) )
1181
                continue;
1182
            pSto[c]->Sign = Sign0[i] | Sign1[k];
1183
            nOldSupp = pSto[c]->pCut[0];
1184
            pSto[c]->iFunc = Jf_TtComputeForCut( p, Jf_ObjFunc0(pObj, pCut0), Jf_ObjFunc1(pObj, pCut1), pCut0, pCut1, pSto[c]->pCut );
1185 1186 1187
            assert( pSto[c]->pCut[0] <= nOldSupp );
            if ( pSto[c]->pCut[0] < nOldSupp )
                pSto[c]->Sign = Jf_CutGetSign( pSto[c]->pCut );
1188 1189
            if ( pSto[c]->iFunc >= (1 << 24) )
                printf( "Hard limit on the number of different Boolean functions (2^23) is reached. Quitting...\n" ), exit(1);
1190
        }
1191 1192 1193 1194 1195 1196
        p->CutCount[2]++;
        pSto[c]->Time = p->pPars->fAreaOnly ? 0 : Jf_CutArr(p, pSto[c]->pCut);
        pSto[c]->Flow = Jf_CutFlow(p, pSto[c]->pCut);
        c = Jf_ObjAddCutToStore( p, pSto, c, CutNum );
        assert( c <= CutNum );
    }
1197
//    Jf_ObjPrintStore( p, pSto, c );
1198
//    Jf_ObjCheckStore( p, pSto, c, iObj );
1199
    // add two variable cut
1200
    if ( !Jf_ObjIsUnit(pObj) && !Jf_ObjHasCutWithSize(pSto, c, 2) )
1201 1202
    {
        assert( Jf_ObjIsUnit(Gia_ObjFanin0(pObj)) && Jf_ObjIsUnit(Gia_ObjFanin1(pObj)) );
1203
        if ( p->pPars->fCutMin ) pSto[c]->iFunc = 4;  // set function (DSD only!)
1204 1205 1206 1207 1208
        pSto[c]->pCut[0] = 2; 
        pSto[c]->pCut[1] = Jf_ObjLit(Gia_ObjFaninId0(pObj, iObj), Gia_ObjFaninC0(pObj)); 
        pSto[c]->pCut[2] = Jf_ObjLit(Gia_ObjFaninId1(pObj, iObj), Gia_ObjFaninC1(pObj)); 
        c++;
    }
1209
    // add elementary cut
1210
    if ( Jf_ObjIsUnit(pObj) && !(p->pPars->fCutMin && Jf_ObjHasCutWithSize(pSto, c, 1)) )
1211
    {
1212
        if ( p->pPars->fCutMin ) pSto[c]->iFunc = 2;  // set function
1213 1214 1215 1216
        pSto[c]->pCut[0] = 1;
        pSto[c]->pCut[1] = Jf_ObjLit(iObj, 0);
        c++;
    }
1217 1218
    // reorder cuts
//    Jf_ObjSortCuts( pSto + 1, c - 1 );
1219
//    Jf_ObjCheckPtrs( pSto, CutNum );
1220 1221 1222
    // find cost of the best cut
    pSto[0]->Cost = p->pPars->fGenCnf ? Jf_CutCnfSizeF(p, Abc_Lit2Var(pSto[0]->iFunc)) : 1;
    assert( pSto[0]->Cost >= 0 );
1223
    // save best info
1224
    assert( pSto[0]->Flow >= 0 );
1225
    Vec_IntWriteEntry( &p->vArr,  iObj, pSto[0]->Time );
1226
    Vec_FltWriteEntry( &p->vFlow, iObj, (pSto[0]->Flow + (fEdge ? pSto[0]->pCut[0] : pSto[0]->Cost)) / Jf_ObjRefs(p, iObj) );
1227
    // add cuts to storage cuts
1228
    Vec_IntClear( p->vTemp );
1229
    Vec_IntPush( p->vTemp, c );
1230
    for ( i = 0; i < c; i++ )
1231
    {
1232 1233
        pSto[i]->Cost = p->pPars->fGenCnf ? Jf_CutCnfSizeF(p, Abc_Lit2Var(pSto[i]->iFunc)) : 1;
        Vec_IntPush( p->vTemp, Jf_CutSetAll(pSto[i]->iFunc, pSto[i]->Cost, pSto[i]->pCut[0]) );
1234
        for ( k = 1; k <= pSto[i]->pCut[0]; k++ )
1235
            Vec_IntPush( p->vTemp, pSto[i]->pCut[k] );
1236
    }
1237
    Vec_IntWriteEntry( &p->vCuts, iObj, Vec_SetAppend(&p->pMem, Vec_IntArray(p->vTemp), Vec_IntSize(p->vTemp)) );
1238
    p->CutCount[3] += c;
1239
}
1240
void Jf_ManComputeCuts( Jf_Man_t * p, int fEdge )
1241 1242
{
    Gia_Obj_t * pObj; int i;
1243 1244 1245 1246 1247
    if ( p->pPars->fVerbose )
    {
        printf( "Aig: CI = %d  CO = %d  AND = %d    ", Gia_ManCiNum(p->pGia), Gia_ManCoNum(p->pGia), Gia_ManAndNum(p->pGia) );
        printf( "LutSize = %d  CutMax = %d  Rounds = %d\n", p->pPars->nLutSize, p->pPars->nCutNum, p->pPars->nRounds );
        printf( "Computing cuts...\r" );
1248
        fflush( stdout );
1249
    }
1250 1251
    Gia_ManForEachObj( p->pGia, pObj, i )
    {
1252
        if ( Gia_ObjIsCi(pObj) || Gia_ObjIsBuf(pObj) )
1253
            Jf_ObjAssignCut( p, pObj );
1254
        if ( Gia_ObjIsBuf(pObj) )
1255 1256
            Jf_ObjPropagateBuf( p, pObj, 0 );
        else if ( Gia_ObjIsAnd(pObj) )
1257
            Jf_ObjComputeCuts( p, pObj, fEdge );
1258 1259 1260
    }
    if ( p->pPars->fVerbose )
    {
Alan Mishchenko committed
1261 1262 1263 1264
        printf( "CutPair = %lu  ", (long)p->CutCount[0] );
        printf( "Merge = %lu  ",   (long)p->CutCount[1] );
        printf( "Eval = %lu  ",    (long)p->CutCount[2] );
        printf( "Cut = %lu  ",     (long)p->CutCount[3] );
1265 1266 1267 1268 1269
        Abc_PrintTime( 1, "Time",  Abc_Clock() - p->clkStart );
        printf( "Memory:  " );
        printf( "Gia = %.2f MB  ", Gia_ManMemory(p->pGia) / (1<<20) );
        printf( "Man = %.2f MB  ", 6.0 * sizeof(int) * Gia_ManObjNum(p->pGia) / (1<<20) );
        printf( "Cuts = %.2f MB",  Vec_ReportMemory(&p->pMem) / (1<<20) );
1270 1271
        if ( p->nCoarse )
        printf( "   Coarse = %d (%.1f %%)",  p->nCoarse, 100.0 * p->nCoarse / Gia_ManObjNum(p->pGia) );
1272
        printf( "\n" );
1273
        fflush( stdout );
1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295
    }
}


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

  Synopsis    [Computing delay/area.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Jf_ManComputeDelay( Jf_Man_t * p, int fEval )
{
    Gia_Obj_t * pObj; 
    int i, Delay = 0;
    if ( fEval )
    {
        Gia_ManForEachObj( p->pGia, pObj, i )
1296
            if ( Gia_ObjIsBuf(pObj) )
1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310
                Jf_ObjPropagateBuf( p, pObj, 0 );
            else if ( Gia_ObjIsAnd(pObj) && Gia_ObjRefNum(p->pGia, pObj) > 0 )
                Vec_IntWriteEntry( &p->vArr, i, Jf_CutArr(p, Jf_ObjCutBest(p, i)) );
    }
    Gia_ManForEachCoDriver( p->pGia, pObj, i )
    {
        assert( Gia_ObjRefNum(p->pGia, pObj) > 0 );
        Delay = Abc_MaxInt( Delay, Jf_ObjArr(p, Gia_ObjId(p->pGia, pObj)) );
    }
    return Delay;
}
int Jf_ManComputeRefs( Jf_Man_t * p )
{
    Gia_Obj_t * pObj; 
1311
    float nRefsNew; int i, * pCut;
1312 1313 1314 1315 1316 1317 1318
    float * pRefs = Vec_FltArray(&p->vRefs);
    float * pFlow = Vec_FltArray(&p->vFlow);
    assert( p->pGia->pRefs != NULL );
    memset( p->pGia->pRefs, 0, sizeof(int) * Gia_ManObjNum(p->pGia) );
    p->pPars->Area = p->pPars->Edge = 0;
    Gia_ManForEachObjReverse( p->pGia, pObj, i )
    {
1319
        if ( Gia_ObjIsCo(pObj) || Gia_ObjIsBuf(pObj) )
1320 1321 1322
            Gia_ObjRefInc( p->pGia, Gia_ObjFanin0(pObj) );
        else if ( Gia_ObjIsAnd(pObj) && Gia_ObjRefNum(p->pGia, pObj) > 0 )
        {
1323
            assert( Jf_ObjIsUnit(pObj) );
1324 1325 1326 1327 1328
            pCut = Jf_ObjCutBest(p, i);
            Jf_CutRef( p, pCut );
            if ( p->pPars->fGenCnf )
            p->pPars->Clause += Jf_CutCnfSize(p, pCut);
            p->pPars->Edge += Jf_CutSize(pCut);
1329 1330
            p->pPars->Area++;
        }
Alan Mishchenko committed
1331 1332
    }
    // blend references and normalize flow
Alan Mishchenko committed
1333
    for ( i = 0; i < Gia_ManObjNum(p->pGia); i++ )
Alan Mishchenko committed
1334
    {
1335 1336 1337 1338
        if ( p->pPars->fOptEdge )
            nRefsNew = Abc_MaxFloat( 1, 0.8 * pRefs[i] + 0.2 * p->pGia->pRefs[i] );
        else
            nRefsNew = Abc_MaxFloat( 1, 0.2 * pRefs[i] + 0.8 * p->pGia->pRefs[i] );
1339 1340
        pFlow[i] = pFlow[i] * pRefs[i] / nRefsNew;
        pRefs[i] = nRefsNew;
1341
        assert( pFlow[i] >= 0 );
1342
    }
Alan Mishchenko committed
1343
    // compute delay
1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367
    p->pPars->Delay = Jf_ManComputeDelay( p, 1 );
    return p->pPars->Area;
}

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

  Synopsis    [Mapping rounds.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Jf_ObjComputeBestCut( Jf_Man_t * p, Gia_Obj_t * pObj, int fEdge, int fEla )
{
    int i, iObj = Gia_ObjId( p->pGia, pObj );
    int * pCuts = Jf_ObjCuts( p, iObj );
    int * pCut, * pCutBest = NULL;
    int Time = ABC_INFINITY, TimeBest = ABC_INFINITY;
    float Area, AreaBest = ABC_INFINITY;
    Jf_ObjForEachCut( pCuts, pCut, i )
    {
1368
        if ( Jf_CutIsTriv(pCut, iObj) ) continue;
1369 1370 1371
        if ( fEdge && !fEla ) 
            Jf_CutSetCost(pCut, Jf_CutSize(pCut));
        Area = fEla ? Jf_CutArea(p, pCut, fEdge) : Jf_CutFlow(p, pCut) + Jf_CutCost(pCut);
1372
        if ( pCutBest == NULL || AreaBest > Area + JF_EPSILON || (AreaBest > Area - JF_EPSILON && TimeBest > (Time = Jf_CutArr(p, pCut))) )
1373 1374 1375 1376
            pCutBest = pCut, AreaBest = Area, TimeBest = Time;
    }
    Vec_IntWriteEntry( &p->vArr,  iObj, Jf_CutArr(p, pCutBest) );
    if ( !fEla )
1377
    Vec_FltWriteEntry( &p->vFlow, iObj, AreaBest / Jf_ObjRefs(p, iObj) );
1378 1379 1380 1381 1382 1383 1384 1385
    Jf_ObjSetBestCut( pCuts, pCutBest, p->vTemp );
//    Jf_CutPrint( Jf_ObjCutBest(p, iObj) ); printf( "\n" );
}
void Jf_ManPropagateFlow( Jf_Man_t * p, int fEdge )
{
    Gia_Obj_t * pObj; 
    int i;
    Gia_ManForEachObj( p->pGia, pObj, i )
1386
        if ( Gia_ObjIsBuf(pObj) )
1387
            Jf_ObjPropagateBuf( p, pObj, 0 );
1388
        else if ( Gia_ObjIsAnd(pObj) && Jf_ObjIsUnit(pObj) )
1389 1390 1391 1392 1393 1394 1395
            Jf_ObjComputeBestCut( p, pObj, fEdge, 0 );
    Jf_ManComputeRefs( p );
}
void Jf_ManPropagateEla( Jf_Man_t * p, int fEdge )
{
    Gia_Obj_t * pObj; 
    int i, CostBef, CostAft;
1396
    p->pPars->Area = p->pPars->Edge = p->pPars->Clause = 0;
1397
    Gia_ManForEachObjReverse( p->pGia, pObj, i )
1398
        if ( Gia_ObjIsBuf(pObj) )
1399 1400 1401
            Jf_ObjPropagateBuf( p, pObj, 1 );
        else if ( Gia_ObjIsAnd(pObj) && Gia_ObjRefNum(p->pGia, pObj) > 0 )
        {
1402
            assert( Jf_ObjIsUnit(pObj) );
1403 1404 1405 1406 1407 1408 1409 1410
            if ( Jf_CutCheckMffc(p, Jf_ObjCutBest(p, i), 50) )
            {
                CostBef = Jf_CutDeref_rec( p, Jf_ObjCutBest(p, i) );
                Jf_ObjComputeBestCut( p, pObj, fEdge, 1 );
                CostAft = Jf_CutRef_rec( p, Jf_ObjCutBest(p, i) );
    //            if ( CostBef != CostAft )  printf( "%d -> %d   ", CostBef, CostAft );
                assert( CostBef >= CostAft ); // does not hold because of JF_EDGE_LIM
            }
1411 1412
            if ( p->pPars->fGenCnf )
            p->pPars->Clause += Jf_CutCnfSize(p, Jf_ObjCutBest(p, i));
1413
            p->pPars->Edge += Jf_CutSize(Jf_ObjCutBest(p, i));
1414 1415 1416
            p->pPars->Area++;
        }
    p->pPars->Delay = Jf_ManComputeDelay( p, 1 );
1417
//    printf( "\n" );
1418
}
1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434

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

  Synopsis    [Derives the result of mapping.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Gia_Man_t * Jf_ManDeriveMappingGia( Jf_Man_t * p )
{
    Gia_Man_t * pNew;
    Gia_Obj_t * pObj; 
1435
    Vec_Int_t * vCopies   = Vec_IntStartFull( Gia_ManObjNum(p->pGia) );
Alan Mishchenko committed
1436 1437
    Vec_Int_t * vMapping  = Vec_IntStart( 2 * Gia_ManObjNum(p->pGia) + (int)p->pPars->Edge + 2 * (int)p->pPars->Area );
    Vec_Int_t * vMapping2 = Vec_IntStart( (int)p->pPars->Edge + 2 * (int)p->pPars->Area + 1000 );
1438 1439
    Vec_Int_t * vCover    = Vec_IntAlloc( 1 << 16 );
    Vec_Int_t * vLeaves   = Vec_IntAlloc( 16 );
1440
    Vec_Int_t * vLits = NULL, * vClas = NULL;
1441
    int i, k, iLit, Class, * pCut;
1442
    word uTruth = 0, * pTruth = &uTruth;
1443
    assert( p->pPars->fCutMin );
1444 1445 1446 1447
    if ( p->pPars->fGenCnf )
    {
        vLits = Vec_IntAlloc( 1000 );
        vClas = Vec_IntAlloc( 1000 );
Alan Mishchenko committed
1448 1449
        Vec_IntPush( vClas, Vec_IntSize(vLits) );
        Vec_IntPush( vLits, 1 );
1450
    }
1451 1452 1453 1454 1455
    // create new manager
    pNew = Gia_ManStart( Gia_ManObjNum(p->pGia) );
    pNew->pName = Abc_UtilStrsav( p->pGia->pName );
    pNew->pSpec = Abc_UtilStrsav( p->pGia->pSpec );
    // map primary inputs
1456
    Vec_IntWriteEntry( vCopies, 0, 0 );
1457 1458 1459 1460 1461
    Gia_ManForEachCi( p->pGia, pObj, i )
        Vec_IntWriteEntry( vCopies, Gia_ObjId(p->pGia, pObj), Gia_ManAppendCi(pNew) );
    // iterate through nodes used in the mapping
    Gia_ManForEachAnd( p->pGia, pObj, i )
    {
1462
       if ( Gia_ObjIsBuf(pObj) || Gia_ObjRefNum(p->pGia, pObj) == 0 )
1463 1464
            continue;
        pCut = Jf_ObjCutBest( p, i );
1465
//        printf( "Best cut of node %d:  ", i );  Jf_CutPrint(pCut);
1466
        Class = Jf_CutFuncClass( pCut );
1467 1468
        if ( Jf_CutSize(pCut) == 0 )
        {
1469
            assert( Class == 0 );
1470 1471 1472 1473 1474
            Vec_IntWriteEntry( vCopies, i, Jf_CutFunc(pCut) );
            continue;
        }
        if ( Jf_CutSize(pCut) == 1 )
        {
1475 1476 1477
            assert( Class == 1 );
            iLit = Abc_LitNotCond( Jf_CutLit(pCut, 1) , Jf_CutFuncCompl(pCut) );
            iLit = Abc_Lit2LitL( Vec_IntArray(vCopies), iLit );
1478 1479 1480
            Vec_IntWriteEntry( vCopies, i, iLit );
            continue;
        }
1481 1482 1483 1484 1485
        if ( p->pPars->fFuncDsd )
            uTruth = Sdm_ManReadDsdTruth(p->pDsd, Class);
        else
            pTruth = Vec_MemReadEntry(p->vTtMem, Class);
        assert( p->pDsd == NULL || Sdm_ManReadDsdVarNum(p->pDsd, Class) == Jf_CutSize(pCut) );
1486 1487 1488 1489 1490
        // collect leaves
        Vec_IntClear( vLeaves );
        Jf_CutForEachLit( pCut, iLit, k )
            Vec_IntPush( vLeaves, Abc_Lit2LitL(Vec_IntArray(vCopies), iLit) );
        // create GIA
1491
        iLit = Kit_TruthToGia( pNew, (unsigned *)pTruth, Vec_IntSize(vLeaves), vCover, vLeaves, 0 );
1492 1493
        if ( p->pPars->fGenCnf )
            Jf_ManGenCnf( uTruth, iLit, vLeaves, vLits, vClas, vCover );
Alan Mishchenko committed
1494 1495
        iLit = Abc_LitNotCond( iLit, Jf_CutFuncCompl(pCut) );
        Vec_IntWriteEntry( vCopies, i, iLit );
1496 1497 1498 1499 1500 1501 1502 1503 1504
        // create mapping
        Vec_IntSetEntry( vMapping, Abc_Lit2Var(iLit), Vec_IntSize(vMapping2) );
        Vec_IntPush( vMapping2, Vec_IntSize(vLeaves) );
        Vec_IntForEachEntry( vLeaves, iLit, k )
            Vec_IntPush( vMapping2, Abc_Lit2Var(iLit) );
        Vec_IntPush( vMapping2, Abc_Lit2Var(Vec_IntEntry(vCopies, i)) );
    }
    Gia_ManForEachCo( p->pGia, pObj, i )
    {
1505 1506
        if ( p->pPars->fGenCnf )
            Vec_IntClear( vLeaves );
1507
        iLit = Vec_IntEntry( vCopies, Gia_ObjFaninId0p(p->pGia, pObj) );
1508 1509 1510 1511 1512
        if ( p->pPars->fGenCnf )
            Vec_IntPush( vLeaves, Abc_LitNotCond(iLit, Gia_ObjFaninC0(pObj)) );
        iLit = Gia_ManAppendCo( pNew, Abc_LitNotCond(iLit, Gia_ObjFaninC0(pObj)) );
        if ( p->pPars->fGenCnf )
            Jf_ManGenCnf( ABC_CONST(0xAAAAAAAAAAAAAAAA), iLit, vLeaves, vLits, vClas, vCover );
1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531
    }
    Vec_IntFree( vCopies );
    Vec_IntFree( vCover );
    Vec_IntFree( vLeaves );
    // finish mapping 
    if ( Vec_IntSize(vMapping) > Gia_ManObjNum(pNew) )
        Vec_IntShrink( vMapping, Gia_ManObjNum(pNew) );
    else
        Vec_IntFillExtra( vMapping, Gia_ManObjNum(pNew), 0 );
    assert( Vec_IntSize(vMapping) == Gia_ManObjNum(pNew) );
    Vec_IntForEachEntry( vMapping, iLit, i )
        if ( iLit > 0 )
            Vec_IntAddToEntry( vMapping, i, Gia_ManObjNum(pNew) );
    Vec_IntAppend( vMapping, vMapping2 );
    Vec_IntFree( vMapping2 );
    // attach mapping and packing
    assert( pNew->vMapping == NULL );
    pNew->vMapping = vMapping;
    Gia_ManSetRegNum( pNew, Gia_ManRegNum(p->pGia) );
1532 1533
    // derive CNF
    if ( p->pPars->fGenCnf )
Alan Mishchenko committed
1534
    {
1535 1536 1537
        if ( p->pPars->fCnfObjIds )
            pNew->pData = Jf_ManCreateCnf( pNew, vLits, vClas );
        else
1538
            pNew->pData = Jf_ManCreateCnfRemap( pNew, vLits, vClas, p->pPars->fAddOrCla );
Alan Mishchenko committed
1539
    }
1540 1541
    Vec_IntFreeP( &vLits );
    Vec_IntFreeP( &vClas );
1542 1543
    return pNew;
}
1544 1545 1546 1547 1548
void Jf_ManDeriveMapping( Jf_Man_t * p )
{
    Vec_Int_t * vMapping;
    Gia_Obj_t * pObj; 
    int i, k, * pCut;
1549
    assert( !p->pPars->fCutMin );
1550
    vMapping = Vec_IntAlloc( Gia_ManObjNum(p->pGia) + (int)p->pPars->Edge + (int)p->pPars->Area * 2 );
1551
    Vec_IntFill( vMapping, Gia_ManObjNum(p->pGia), 0 );
1552 1553
    Gia_ManForEachAnd( p->pGia, pObj, i )
    {
1554
        if ( Gia_ObjIsBuf(pObj) || Gia_ObjRefNum(p->pGia, pObj) == 0 )
1555 1556 1557
            continue;
        pCut = Jf_ObjCutBest( p, i );
        Vec_IntWriteEntry( vMapping, i, Vec_IntSize(vMapping) );
1558
        assert( !p->pPars->fCutMin || Jf_CutSize(pCut) <= 6 );
1559 1560 1561
        Vec_IntPush( vMapping, Jf_CutSize(pCut) );
        for ( k = 1; k <= Jf_CutSize(pCut); k++ )
            Vec_IntPush( vMapping, Jf_CutVar(pCut, k) );
1562 1563
        Vec_IntPush( vMapping, i );
    }
1564
    assert( Vec_IntCap(vMapping) == 16 || Vec_IntSize(vMapping) == Vec_IntCap(vMapping) );
1565 1566 1567 1568 1569 1570
    p->pGia->vMapping = vMapping;
//    Gia_ManMappingVerify( p->pGia );
}

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

1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581
  Synopsis    [Derive GIA without mapping.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Gia_Man_t * Jf_ManDeriveGia( Jf_Man_t * p )
{
1582
    Gia_Man_t * pNew, * pTemp;
1583 1584 1585 1586 1587
    Gia_Obj_t * pObj; 
    Vec_Int_t * vCopies   = Vec_IntStartFull( Gia_ManObjNum(p->pGia) );
    Vec_Int_t * vCover    = Vec_IntAlloc( 1 << 16 );
    Vec_Int_t * vLeaves   = Vec_IntAlloc( 16 );
    int i, k, iLit, Class, * pCut;
1588 1589
    int nWords = Abc_Truth6WordNum(p->pPars->nLutSize);
    word uTruth = 0, * pTruth = &uTruth, Truth[JF_WORD_MAX];
1590 1591 1592 1593
    // create new manager
    pNew = Gia_ManStart( Gia_ManObjNum(p->pGia) );
    pNew->pName = Abc_UtilStrsav( p->pGia->pName );
    pNew->pSpec = Abc_UtilStrsav( p->pGia->pSpec );
1594
    pNew->vLevels = Vec_IntStart( 6*Gia_ManObjNum(p->pGia)/5 + 100 );
1595 1596 1597 1598 1599
    // map primary inputs
    Vec_IntWriteEntry( vCopies, 0, 0 );
    Gia_ManForEachCi( p->pGia, pObj, i )
        Vec_IntWriteEntry( vCopies, Gia_ObjId(p->pGia, pObj), Gia_ManAppendCi(pNew) );
    // iterate through nodes used in the mapping
1600 1601
    if ( !p->pPars->fCutMin )
        Gia_ObjComputeTruthTableStart( p->pGia, p->pPars->nLutSize );
1602 1603 1604
    Gia_ManHashStart( pNew );
    Gia_ManForEachAnd( p->pGia, pObj, i )
    {
1605
       if ( Gia_ObjIsBuf(pObj) || Gia_ObjRefNum(p->pGia, pObj) == 0 )
1606 1607 1608
            continue;
        pCut = Jf_ObjCutBest( p, i );
//        printf( "Best cut of node %d:  ", i );  Jf_CutPrint(pCut);
1609 1610
        // get the truth table
        if ( p->pPars->fCutMin )
1611
        {
1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626
            Class = Jf_CutFuncClass( pCut );
            if ( Jf_CutSize(pCut) == 0 )
            {
                assert( Class == 0 );
                Vec_IntWriteEntry( vCopies, i, Jf_CutFunc(pCut) );
                continue;
            }
            if ( Jf_CutSize(pCut) == 1 )
            {
                assert( Class == 1 );
                iLit = Abc_LitNotCond( Jf_CutLit(pCut, 1) , Jf_CutFuncCompl(pCut) );
                iLit = Abc_Lit2LitL( Vec_IntArray(vCopies), iLit );
                Vec_IntWriteEntry( vCopies, i, iLit );
                continue;
            }
1627 1628 1629 1630 1631
            if ( p->pPars->fFuncDsd )
                uTruth = Sdm_ManReadDsdTruth(p->pDsd, Class);
            else
                Abc_TtCopy( (pTruth = Truth), Vec_MemReadEntry(p->vTtMem, Class), nWords, 0 );
            assert( p->pDsd == NULL || Sdm_ManReadDsdVarNum(p->pDsd, Class) == Jf_CutSize(pCut) );
1632
        }
1633
        else
1634
        {
1635 1636 1637 1638
            Vec_IntClear( vLeaves );
            Jf_CutForEachLit( pCut, iLit, k )
                Vec_IntPush( vLeaves, Abc_Lit2Var(iLit) );
            pTruth = Gia_ObjComputeTruthTableCut( p->pGia, pObj, vLeaves );
1639
        }
1640
        // collect incoming literals
1641 1642 1643 1644
        Vec_IntClear( vLeaves );
        Jf_CutForEachLit( pCut, iLit, k )
            Vec_IntPush( vLeaves, Abc_Lit2LitL(Vec_IntArray(vCopies), iLit) );
        // create GIA
1645
        iLit = Dsm_ManTruthToGia( pNew, pTruth, vLeaves, vCover );
1646
        iLit = Abc_LitNotCond( iLit, (p->pPars->fCutMin && Jf_CutFuncCompl(pCut)) );
1647 1648 1649 1650 1651 1652 1653
        Vec_IntWriteEntry( vCopies, i, iLit );
    }
    Gia_ManForEachCo( p->pGia, pObj, i )
    {
        iLit = Vec_IntEntry( vCopies, Gia_ObjFaninId0p(p->pGia, pObj) );
        Gia_ManAppendCo( pNew, Abc_LitNotCond(iLit, Gia_ObjFaninC0(pObj)) );
    }
1654 1655
    if ( !p->pPars->fCutMin )
        Gia_ObjComputeTruthTableStop( p->pGia );
1656 1657 1658 1659 1660
    Vec_IntFree( vCopies );
    Vec_IntFree( vLeaves );
    Vec_IntFree( vCover );
    Gia_ManHashStop( pNew );
    Gia_ManSetRegNum( pNew, Gia_ManRegNum(p->pGia) );
1661
//    Dsm_ManReportStats();
1662
    // perform cleanup
1663 1664 1665 1666 1667
    if ( !p->pPars->fCutMin )
    {
        pNew = Gia_ManCleanup( pTemp = pNew );
        Gia_ManStop( pTemp );
    }
1668 1669 1670 1671 1672
    return pNew;
}

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

1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687
  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Jf_ManSetDefaultPars( Jf_Par_t * pPars )
{
    memset( pPars, 0, sizeof(Jf_Par_t) );
    pPars->nLutSize     =  6;
    pPars->nCutNum      =  8;
    pPars->nRounds      =  1;
1688
    pPars->nVerbLimit   =  5;
1689 1690
    pPars->DelayTarget  = -1;
    pPars->fAreaOnly    =  1;
1691
    pPars->fOptEdge     =  1; 
1692
    pPars->fCoarsen     =  0;
1693
    pPars->fCutMin      =  0;
1694
    pPars->fFuncDsd     =  0;
1695
    pPars->fGenCnf      =  0;
1696
    pPars->fPureAig     =  0;
1697 1698 1699 1700 1701 1702 1703 1704 1705 1706
    pPars->fVerbose     =  0;
    pPars->fVeryVerbose =  0;
    pPars->nLutSizeMax  =  JF_LEAF_MAX;
    pPars->nCutNumMax   =  JF_CUT_MAX;
}
void Jf_ManPrintStats( Jf_Man_t * p, char * pTitle )
{
    if ( !p->pPars->fVerbose )
        return;
    printf( "%s :  ", pTitle );
Alan Mishchenko committed
1707 1708 1709
    printf( "Level =%6lu   ", (long)p->pPars->Delay );
    printf( "Area =%9lu   ",  (long)p->pPars->Area );
    printf( "Edge =%9lu   ",  (long)p->pPars->Edge );
1710
    if ( p->pPars->fGenCnf )
Alan Mishchenko committed
1711
    printf( "Cnf =%9lu   ",   (long)p->pPars->Clause );
1712
    Abc_PrintTime( 1, "Time", Abc_Clock() - p->clkStart );
1713
    fflush( stdout );
1714 1715 1716
}
Gia_Man_t * Jf_ManPerformMapping( Gia_Man_t * pGia, Jf_Par_t * pPars )
{
1717
    Gia_Man_t * pNew = pGia;
1718
    Jf_Man_t * p; int i;
1719
    assert( !Gia_ManBufNum(pGia) );
1720
    assert( !pPars->fCutMin || !pPars->fFuncDsd || pPars->nLutSize <= 6 );
1721
    if ( pPars->fGenCnf )
1722 1723
        pPars->fCutMin = 1, pPars->fFuncDsd = 1, pPars->fOptEdge = 0;
    if ( pPars->fCutMin && !pPars->fFuncDsd )
1724
        pPars->fCoarsen = 0;
1725 1726
    p = Jf_ManAlloc( pGia, pPars );
    p->pCutCmp = pPars->fAreaOnly ? Jf_CutCompareArea : Jf_CutCompareDelay;
1727
    Jf_ManComputeCuts( p, 0 );
1728
    Jf_ManComputeRefs( p );                         Jf_ManPrintStats( p, "Start" );
1729 1730
    for ( i = 0; i < pPars->nRounds; i++ )
    {
1731 1732
        if ( !p->pPars->fGenCnf )
        {
1733
        Jf_ManPropagateFlow( p, pPars->fOptEdge );  Jf_ManPrintStats( p, "Flow " );
1734
        }
1735 1736
        Jf_ManPropagateEla( p, 0 );                 Jf_ManPrintStats( p, "Area " );
        Jf_ManPropagateEla( p, 1 );                 Jf_ManPrintStats( p, "Edge " );
1737
    }
1738
    if ( p->pPars->fVeryVerbose && p->pPars->fCutMin && !p->pPars->fFuncDsd )
1739
        Vec_MemDumpTruthTables( p->vTtMem, Gia_ManName(p->pGia), p->pPars->nLutSize );
1740 1741 1742
    if ( p->pPars->fPureAig )
        pNew = Jf_ManDeriveGia(p);
    else if ( p->pPars->fCutMin )
1743 1744 1745
        pNew = Jf_ManDeriveMappingGia(p);
    else
        Jf_ManDeriveMapping(p);
1746
    Jf_ManFree( p );
1747
    return pNew;
1748
}
1749
Gia_Man_t * Jf_ManDeriveCnf( Gia_Man_t * p, int fCnfObjIds )
1750 1751 1752 1753
{
    Jf_Par_t Pars, * pPars = &Pars;
    Jf_ManSetDefaultPars( pPars );
    pPars->fGenCnf = 1;
1754
    pPars->fCnfObjIds = fCnfObjIds;
Alan Mishchenko committed
1755
    return Jf_ManPerformMapping( p, pPars );
Alan Mishchenko committed
1756
}
1757
Gia_Man_t * Jf_ManDeriveCnfMiter( Gia_Man_t * p, int fVerbose )
1758 1759 1760 1761 1762 1763
{
    Jf_Par_t Pars, * pPars = &Pars;
    Jf_ManSetDefaultPars( pPars );
    pPars->fGenCnf = 1;
    pPars->fCnfObjIds = 0;
    pPars->fAddOrCla = 1;
1764
    pPars->fVerbose = fVerbose;
1765 1766
    return Jf_ManPerformMapping( p, pPars );
}
1767
void Jf_ManDumpCnf( Gia_Man_t * p, char * pFileName, int fVerbose )
1768
{
1769
    abctime clk = Abc_Clock();
1770 1771
    Gia_Man_t * pNew;
    Cnf_Dat_t * pCnf;
1772
    pNew = Jf_ManDeriveCnfMiter( p, fVerbose );
1773 1774 1775
    pCnf = (Cnf_Dat_t *)pNew->pData; pNew->pData = NULL;
    Cnf_DataWriteIntoFile( pCnf, pFileName, 0, NULL, NULL );
    Gia_ManStop( pNew );
1776 1777 1778 1779 1780
//    if ( fVerbose )
    {
        printf( "CNF stats: Vars = %6d. Clauses = %7d. Literals = %8d. ", pCnf->nVars, pCnf->nClauses, pCnf->nLiterals );
        Abc_PrintTime( 1, "Time", Abc_Clock() - clk );
    }
1781 1782 1783
    Cnf_DataFree(pCnf);
}

Alan Mishchenko committed
1784 1785
void Jf_ManTestCnf( Gia_Man_t * p )
{
Alan Mishchenko committed
1786 1787 1788 1789
    Gia_Man_t * pNew;
    Cnf_Dat_t * pCnf;
    int i;
//    Cnf_Dat_t * pCnf = Cnf_DeriveGia( p );
1790
    pNew = Jf_ManDeriveCnf( p, 1 );
Alan Mishchenko committed
1791
    pCnf = (Cnf_Dat_t *)pNew->pData; pNew->pData = NULL;
1792
    Cnf_DataWriteIntoFile( pCnf, "test.cnf", 0, NULL, NULL );
Alan Mishchenko committed
1793 1794 1795
    for ( i = 0; i < pCnf->nVars; i++ )
        printf( "%d : %d %d\n", i, pCnf->pObj2Count[i], pCnf->pObj2Clause[i] );
    Gia_ManStop( pNew );
1796 1797
    Cnf_DataFree(pCnf);
}
1798 1799 1800 1801 1802 1803 1804 1805

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


ABC_NAMESPACE_IMPL_END