giaNf.c 91.4 KB
Newer Older
1 2 3 4 5 6 7 8
/**CFile****************************************************************

  FileName    [giaNf.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [Scalable AIG package.]

9
  Synopsis    [Standard-cell mapper.]
10 11 12 13 14 15 16 17 18 19 20

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

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

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

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

21
#include <float.h>
22
#include "gia.h"
23 24 25 26 27 28 29 30
#include "misc/st/st.h"
#include "map/mio/mio.h"
#include "misc/util/utilTruth.h"
#include "misc/extra/extra.h"
#include "base/main/main.h"
#include "misc/vec/vecMem.h"
#include "misc/vec/vecWec.h"
#include "opt/dau/dau.h"
31 32
#include "misc/util/utilNam.h"
#include "map/scl/sclCon.h"
33 34 35 36 37 38 39

ABC_NAMESPACE_IMPL_START

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

40 41 42
#define NF_LEAF_MAX  6
#define NF_CUT_MAX  32
#define NF_NO_LEAF  31
Alan Mishchenko committed
43
#define NF_NO_FUNC  0x3FFFFFF
44
#define NF_EPSILON  0.001
45 46 47 48 49 50 51

typedef struct Nf_Cut_t_ Nf_Cut_t; 
struct Nf_Cut_t_
{
    word            Sign;           // signature
    int             Delay;          // delay
    float           Flow;           // flow
Alan Mishchenko committed
52
    unsigned        iFunc   : 26;   // function (NF_NO_FUNC)
53 54 55 56
    unsigned        Useless :  1;   // function
    unsigned        nLeaves :  5;   // leaf number (NF_NO_LEAF)
    int             pLeaves[NF_LEAF_MAX+1]; // leaves
};
57 58
typedef struct Nf_Cfg_t_ Nf_Cfg_t; 
struct Nf_Cfg_t_
59
{
60 61 62
    unsigned        fCompl  :  1;   // complemented
    unsigned        Phase   :  7;   // match phase
    unsigned        Perm    : 24;   // match permutation
63
};
64 65 66 67 68 69 70
typedef struct Nf_Mat_t_ Nf_Mat_t; 
struct Nf_Mat_t_
{
    unsigned        Gate    : 20;   // gate
    unsigned        CutH    : 10;   // cut handle
    unsigned        fCompl  :  1;   // complemented
    unsigned        fBest   :  1;   // best cut
71
    Nf_Cfg_t        Cfg;            // input literals
72
    int             D;              // delay
73
    float           F;              // area 
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
};
typedef struct Nf_Obj_t_ Nf_Obj_t; 
struct Nf_Obj_t_
{
    Nf_Mat_t        M[2][2];         // del/area (2x)
};
typedef struct Nf_Man_t_ Nf_Man_t; 
struct Nf_Man_t_
{
    // user data
    Gia_Man_t *     pGia;           // derived manager
    Jf_Par_t *      pPars;          // parameters
    // matching
    Vec_Mem_t *     vTtMem;         // truth tables
    Vec_Wec_t *     vTt2Match;      // matches for truth tables
89
    Mio_Cell2_t *   pCells;         // library gates
90 91 92 93 94 95 96
    int             nCells;         // library gate count
    // cut data
    Nf_Obj_t *      pNfObjs;        // best cuts
    Vec_Ptr_t       vPages;         // cut memory
    Vec_Int_t       vCutSets;       // cut offsets
    Vec_Int_t       vMapRefs;       // mapping refs   (2x)
    Vec_Flt_t       vFlowRefs;      // flow refs      (2x)
97
    Vec_Int_t       vRequired;      // required times (2x)
98 99 100 101 102 103 104
    Vec_Flt_t       vCutFlows;      // temporary cut area
    Vec_Int_t       vCutDelays;     // temporary cut delay
    Vec_Int_t       vBackup;        // backup literals
    int             iCur;           // current position
    int             Iter;           // mapping iterations
    int             fUseEla;        // use exact area
    int             nInvs;          // the inverter count
105
    int             InvDelayI;      // inverter delay
106 107
    word            InvAreaW;       // inverter delay
    float           InvAreaF;       // inverter area 
108 109 110 111 112 113
    // statistics
    abctime         clkStart;       // starting time
    double          CutCount[6];    // cut counts
    int             nCutUseAll;     // objects with useful cuts
};

114 115
static inline int          Nf_Cfg2Int( Nf_Cfg_t Mat )                                { union { int x; Nf_Cfg_t y; } v; v.y = Mat; return v.x;           }
static inline Nf_Cfg_t     Nf_Int2Cfg( int Int )                                     { union { int x; Nf_Cfg_t y; } v; v.x = Int; return v.y;           }
116

117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
static inline Nf_Obj_t *   Nf_ManObj( Nf_Man_t * p, int i )                          { return p->pNfObjs + i;                                           }
static inline Mio_Cell2_t* Nf_ManCell( Nf_Man_t * p, int i )                         { return p->pCells + i;                                            }
static inline int *        Nf_ManCutSet( Nf_Man_t * p, int i )                       { return (int *)Vec_PtrEntry(&p->vPages, i >> 16) + (i & 0xFFFF);  }
static inline int          Nf_ObjCutSetId( Nf_Man_t * p, int i )                     { return Vec_IntEntry( &p->vCutSets, i );                          }
static inline int *        Nf_ObjCutSet( Nf_Man_t * p, int i )                       { return Nf_ManCutSet(p, Nf_ObjCutSetId(p, i));                    }
static inline int          Nf_ObjHasCuts( Nf_Man_t * p, int i )                      { return (int)(Vec_IntEntry(&p->vCutSets, i) > 0);                 }
static inline int *        Nf_ObjCutBest( Nf_Man_t * p, int i )                      { return NULL;                                                     } 
static inline int          Nf_ObjCutUseless( Nf_Man_t * p, int TruthId )             { return (int)(TruthId >= Vec_WecSize(p->vTt2Match));              } 

static inline float        Nf_ObjCutFlow( Nf_Man_t * p, int i )                      { return Vec_FltEntry(&p->vCutFlows, i);                           } 
static inline int          Nf_ObjCutDelay( Nf_Man_t * p, int i )                     { return Vec_IntEntry(&p->vCutDelays, i);                          } 
static inline void         Nf_ObjSetCutFlow( Nf_Man_t * p, int i, float a )          { Vec_FltWriteEntry(&p->vCutFlows, i, a);                          } 
static inline void         Nf_ObjSetCutDelay( Nf_Man_t * p, int i, int d )           { Vec_IntWriteEntry(&p->vCutDelays, i, d);                         } 

static inline int          Nf_ObjMapRefNum( Nf_Man_t * p, int i, int c )             { return Vec_IntEntry(&p->vMapRefs, Abc_Var2Lit(i,c));             }
static inline int          Nf_ObjMapRefInc( Nf_Man_t * p, int i, int c )             { return (*Vec_IntEntryP(&p->vMapRefs, Abc_Var2Lit(i,c)))++;       }
static inline int          Nf_ObjMapRefDec( Nf_Man_t * p, int i, int c )             { return --(*Vec_IntEntryP(&p->vMapRefs, Abc_Var2Lit(i,c)));       }
static inline float        Nf_ObjFlowRefs( Nf_Man_t * p, int i, int c )              { return Vec_FltEntry(&p->vFlowRefs, Abc_Var2Lit(i,c));            }
135 136 137
static inline int          Nf_ObjRequired( Nf_Man_t * p, int i, int c )              { return Vec_IntEntry(&p->vRequired, Abc_Var2Lit(i,c));            }
static inline void         Nf_ObjSetRequired( Nf_Man_t * p,int i, int c, int f )     { Vec_IntWriteEntry(&p->vRequired, Abc_Var2Lit(i,c), f);           }
static inline void         Nf_ObjUpdateRequired( Nf_Man_t * p,int i, int c, int f )  { if (Nf_ObjRequired(p, i, c) > f) Nf_ObjSetRequired(p, i, c, f);  }
138 139 140 141

static inline Nf_Mat_t *   Nf_ObjMatchD( Nf_Man_t * p, int i, int c )                { return &Nf_ManObj(p, i)->M[c][0];                                }
static inline Nf_Mat_t *   Nf_ObjMatchA( Nf_Man_t * p, int i, int c )                { return &Nf_ManObj(p, i)->M[c][1];                                }

142 143 144 145 146 147 148
static inline int          Nf_CutSize( int * pCut )                                  { return pCut[0] & NF_NO_LEAF;                                     }
static inline int          Nf_CutFunc( int * pCut )                                  { return ((unsigned)pCut[0] >> 5);                                 }
static inline int *        Nf_CutLeaves( int * pCut )                                { return pCut + 1;                                                 }
static inline int          Nf_CutSetBoth( int n, int f )                             { return n | (f << 5);                                             }
static inline int          Nf_CutIsTriv( int * pCut, int i )                         { return Nf_CutSize(pCut) == 1 && pCut[1] == i;                    } 
static inline int          Nf_CutHandle( int * pCutSet, int * pCut )                 { assert( pCut > pCutSet ); return pCut - pCutSet;                 } 
static inline int *        Nf_CutFromHandle( int * pCutSet, int h )                  { assert( h > 0 ); return pCutSet + h;                             }
149

150 151
static inline int          Nf_CfgVar( Nf_Cfg_t Cfg, int i )                          { return (Cfg.Perm >> (i<<2)) & 15;                                }
static inline int          Nf_CfgCompl( Nf_Cfg_t Cfg, int i )                        { return (Cfg.Phase >> i) & 1;                                     }
152

153 154 155
#define Nf_SetForEachCut( pList, pCut, i )                   for ( i = 0, pCut = pList + 1; i < pList[0]; i++, pCut += Nf_CutSize(pCut) + 1 )
#define Nf_CutForEachVarCompl( pCut, Cfg, iVar, fCompl, i )  for ( i = 0; i < Nf_CutSize(pCut) && (iVar = Nf_CutLeaves(pCut)[Nf_CfgVar(Cfg, i)]) && ((fCompl = Nf_CfgCompl(Cfg, i)), 1); i++ )
#define Nf_CfgForEachVarCompl( Cfg, Size, iVar, fCompl, i )  for ( i = 0; i < Size && ((iVar = Nf_CfgVar(Cfg, i)), 1) && ((fCompl = Nf_CfgCompl(Cfg, i)), 1); i++ )
156

157 158 159 160 161 162
////////////////////////////////////////////////////////////////////////
///                     FUNCTION DEFINITIONS                         ///
////////////////////////////////////////////////////////////////////////

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

163
  Synopsis    []
164 165 166 167 168 169 170 171

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
172
int Nf_StoCellIsDominated( Mio_Cell2_t * pCell, int * pFans, int * pProf )
173
{
174
    int k;
175
    if ( pCell->AreaF + NF_EPSILON < Abc_Int2Float(pProf[0]) )
176 177
        return 0;
    for ( k = 0; k < (int)pCell->nFanins; k++ )
178
        if ( pCell->iDelays[Abc_Lit2Var(pFans[k])] < pProf[k+1] )
179 180 181
            return 0;
    return 1; // pCell is dominated
}
182
void Nf_StoCreateGateAdd( Vec_Mem_t * vTtMem, Vec_Wec_t * vTt2Match, Mio_Cell2_t * pCell, word uTruth, int * pFans, int nFans, Vec_Wec_t * vProfs, Vec_Int_t * vStore, int fPinFilter, int fPinPerm, int fPinQuick )
183 184 185
{
    Vec_Int_t * vArray, * vArrayProfs = NULL;
    int i, k, GateId, Entry, fCompl = (int)(uTruth & 1);
186
    word uFunc = fCompl ? ~uTruth : uTruth;
187
    int iFunc = Vec_MemHashInsert( vTtMem, &uFunc );
188 189
    Nf_Cfg_t Mat = Nf_Int2Cfg(0);
    // get match array
190 191 192
    if ( iFunc == Vec_WecSize(vTt2Match) )
        Vec_WecPushLevel( vTt2Match );
    vArray = Vec_WecEntry( vTt2Match, iFunc );
193
    // create match
194
    Mat.fCompl = fCompl;
195
    assert( nFans == (int)pCell->nFanins );
196 197
    for ( i = 0; i < nFans; i++ )
    {
198 199
        Mat.Perm  |= (unsigned)(i << (Abc_Lit2Var(pFans[i]) << 2));
        Mat.Phase |= (unsigned)(Abc_LitIsCompl(pFans[i]) << Abc_Lit2Var(pFans[i]));
200
    }
201
    // check other profiles
202
    if ( fPinFilter )
203
    {
204
        // get profile array
205
        assert( Vec_WecSize(vTt2Match) == Vec_WecSize(vProfs) );
206 207 208 209 210 211 212 213 214
        if ( iFunc == Vec_WecSize(vProfs) )
            Vec_WecPushLevel( vProfs );
        vArrayProfs = Vec_WecEntry( vProfs, iFunc );
        assert( Vec_IntSize(vArray) == 2 * Vec_IntSize(vArrayProfs) );
        // skip dominated matches
        Vec_IntForEachEntryDouble( vArray, GateId, Entry, i )
            if ( Nf_Int2Cfg(Entry).Phase == Mat.Phase && Nf_Int2Cfg(Entry).fCompl == Mat.fCompl )
            {
                int Offset = Vec_IntEntry(vArrayProfs, i/2);
215
                int * pProf = Vec_IntEntryP(vStore, Offset);
216 217 218 219 220
                if ( Nf_StoCellIsDominated(pCell, pFans, pProf) )
                    return;
            }
    }
    // check pin permutation
221
    if ( !fPinPerm ) // do not use  pin-permutation (improves delay when pin-delays differ)
222
    {
223
        if ( fPinQuick ) // reduce the number of matches agressively
224 225
        {
            Vec_IntForEachEntryDouble( vArray, GateId, Entry, i )
226
                if ( GateId == (int)pCell->Id && Abc_TtBitCount8[Nf_Int2Cfg(Entry).Phase] == Abc_TtBitCount8[Mat.Phase] )
227 228 229 230 231
                    return;
        }
        else // reduce the number of matches less agressively
        {
            Vec_IntForEachEntryDouble( vArray, GateId, Entry, i )
232
                if ( GateId == (int)pCell->Id && Nf_Int2Cfg(Entry).Phase == Mat.Phase )
233 234
                    return;
        }
235
    }
236
    // save data and profile
237
    Vec_IntPush( vArray, pCell->Id );
238 239
    Vec_IntPush( vArray, Nf_Cfg2Int(Mat) );
    // add delay profile
240
    if ( fPinFilter )
241
    {
242 243
        Vec_IntPush( vArrayProfs, Vec_IntSize(vStore) );
        Vec_IntPush( vStore, Abc_Float2Int(pCell->AreaF) );
244
        for ( k = 0; k < nFans; k++ )
245
            Vec_IntPush( vStore, pCell->iDelays[Abc_Lit2Var(pFans[k])] );
246 247
    }
}
248
void Nf_StoCreateGateMaches( Vec_Mem_t * vTtMem, Vec_Wec_t * vTt2Match, Mio_Cell2_t * pCell, int ** pComp, int ** pPerm, int * pnPerms, Vec_Wec_t * vProfs, Vec_Int_t * vStore, int fPinFilter, int fPinPerm, int fPinQuick )
249 250 251 252 253 254
{
    int Perm[NF_LEAF_MAX], * Perm1, * Perm2;
    int nPerms = pnPerms[pCell->nFanins];
    int nMints = (1 << pCell->nFanins);
    word tCur, tTemp1, tTemp2;
    int i, p, c;
255
    assert( pCell->nFanins <= 6 );
256 257 258 259 260 261 262 263
    for ( i = 0; i < (int)pCell->nFanins; i++ )
        Perm[i] = Abc_Var2Lit( i, 0 );
    tCur = tTemp1 = pCell->uTruth;
    for ( p = 0; p < nPerms; p++ )
    {
        tTemp2 = tCur;
        for ( c = 0; c < nMints; c++ )
        {
264
            Nf_StoCreateGateAdd( vTtMem, vTt2Match, pCell, tCur, Perm, pCell->nFanins, vProfs, vStore, fPinFilter, fPinPerm, fPinQuick );
265 266 267 268 269 270
            // update
            tCur  = Abc_Tt6Flip( tCur, pComp[pCell->nFanins][c] );
            Perm1 = Perm + pComp[pCell->nFanins][c];
            *Perm1 = Abc_LitNot( *Perm1 );
        }
        assert( tTemp2 == tCur );
Alan Mishchenko committed
271 272
        if ( nPerms == 1 )
            continue;
273 274 275 276 277 278 279 280
        // update
        tCur = Abc_Tt6SwapAdjacent( tCur, pPerm[pCell->nFanins][p] );
        Perm1 = Perm + pPerm[pCell->nFanins][p];
        Perm2 = Perm1 + 1;
        ABC_SWAP( int, *Perm1, *Perm2 );
    }
    assert( tTemp1 == tCur );
}
281
Mio_Cell2_t * Nf_StoDeriveMatches( Vec_Mem_t * vTtMem, Vec_Wec_t * vTt2Match, int * pnCells, int fPinFilter, int fPinPerm, int fPinQuick )
282
{
283 284
    int fVerbose = 0;
    //abctime clk = Abc_Clock();
285
    Vec_Wec_t * vProfs = Vec_WecAlloc( 1000 );
286
    Vec_Int_t * vStore = Vec_IntAlloc( 10000 );
287
    int * pComp[7], * pPerm[7], nPerms[7], i;
288
    Mio_Cell2_t * pCells;
289 290
    Vec_WecPushLevel( vProfs );
    Vec_WecPushLevel( vProfs );
Alan Mishchenko committed
291
    for ( i = 1; i <= 6; i++ )
292
        pComp[i] = Extra_GreyCodeSchedule( i );
Alan Mishchenko committed
293
    for ( i = 1; i <= 6; i++ )
294
        pPerm[i] = Extra_PermSchedule( i );
Alan Mishchenko committed
295
    for ( i = 1; i <= 6; i++ )
296
        nPerms[i] = Extra_Factorial( i );
297
    pCells = Mio_CollectRootsNewDefault2( 6, pnCells, fVerbose );
298
    if ( pCells != NULL )
299 300
    for ( i = 2; i < *pnCells; i++ )
        Nf_StoCreateGateMaches( vTtMem, vTt2Match, pCells+i, pComp, pPerm, nPerms, vProfs, vStore, fPinFilter, fPinPerm, fPinQuick );
Alan Mishchenko committed
301
    for ( i = 1; i <= 6; i++ )
302
        ABC_FREE( pComp[i] );
Alan Mishchenko committed
303
    for ( i = 1; i <= 6; i++ )
304
        ABC_FREE( pPerm[i] );
305
    Vec_WecFree( vProfs );
306
    Vec_IntFree( vStore );
307 308
    //Abc_PrintTime( 1, "Time", Abc_Clock() - clk );
    return pCells;
309
}
310
void Nf_StoPrintOne( Nf_Man_t * p, int Count, int t, int i, int GateId, Nf_Cfg_t Mat )
311
{
312
    Mio_Cell2_t * pC = p->pCells + GateId;
313 314
    word * pTruth = Vec_MemReadEntry(p->vTtMem, t);
    int k, nSuppSize = Abc_TtSupportSize(pTruth, 6);
315
    printf( "%6d : ", Count );
316
    printf( "%6d : ", t );
317 318
    printf( "%6d : ", i );
    printf( "Gate %16s  ",   pC->pName );
319
    printf( "Area =%8.2f  ", pC->AreaF );
320 321
    printf( "In = %d   ",    pC->nFanins );
    if ( Mat.fCompl )
322 323 324
        printf( " compl " );
    else
        printf( "       " );
325
    for ( k = 0; k < (int)pC->nFanins; k++ )
326
    {
327 328 329
        int fComplF = (Mat.Phase >> k) & 1;
        int iFanin  = (Mat.Perm >> (3*k)) & 7;
        printf( "%c", 'a' + iFanin - fComplF * ('a' - 'A') );
330 331 332 333 334 335
    }
    printf( "  " );
    Dau_DsdPrintFromTruth( pTruth, nSuppSize );
}
void Nf_StoPrint( Nf_Man_t * p, int fVerbose )
{
336
    int t, i, GateId, Entry, Count = 0;
337 338 339
    for ( t = 2; t < Vec_WecSize(p->vTt2Match); t++ )
    {
        Vec_Int_t * vArr = Vec_WecEntry( p->vTt2Match, t );
340
        Vec_IntForEachEntryDouble( vArr, GateId, Entry, i )
341
        {
342
            Count++;
343 344
            if ( !fVerbose )
                continue;
345 346
            //if ( t < 10 )
            //    Nf_StoPrintOne( p, Count, t, i/2, GateId, Pf_Int2Mat(Entry) );
347 348
        }
    }
349 350
    printf( "Gates = %d.  Truths = %d.  Matches = %d.\n", 
        p->nCells, Vec_MemEntryNum(p->vTtMem), Count );
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
}



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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Nf_Man_t * Nf_StoCreate( Gia_Man_t * pGia, Jf_Par_t * pPars )
{
    extern void Mf_ManSetFlowRefs( Gia_Man_t * p, Vec_Int_t * vRefs );
    Vec_Int_t * vFlowRefs;
    Nf_Man_t * p;
    int i, Entry;
    assert( pPars->nCutNum > 1  && pPars->nCutNum <= NF_CUT_MAX );
    assert( pPars->nLutSize > 1 && pPars->nLutSize <= NF_LEAF_MAX );
    ABC_FREE( pGia->pRefs );
    Vec_IntFreeP( &pGia->vCellMapping );
    if ( Gia_ManHasChoices(pGia) )
        Gia_ManSetPhase(pGia);
    // create
    p = ABC_CALLOC( Nf_Man_t, 1 );
    p->clkStart = Abc_Clock();
    p->pGia     = pGia;
    p->pPars    = pPars;
    p->pNfObjs  = ABC_CALLOC( Nf_Obj_t, Gia_ManObjNum(pGia) );
    p->iCur     = 2;
    // other
    Vec_PtrGrow( &p->vPages, 256 );                                    // cut memory
    Vec_IntFill( &p->vMapRefs,  2*Gia_ManObjNum(pGia), 0 );            // mapping refs   (2x)
    Vec_FltFill( &p->vFlowRefs, 2*Gia_ManObjNum(pGia), 0 );            // flow refs      (2x)
389
    Vec_IntFill( &p->vRequired, 2*Gia_ManObjNum(pGia), SCL_INFINITY ); // required times (2x)
390 391 392 393 394 395 396 397 398 399 400 401 402 403
    Vec_IntFill( &p->vCutSets,  Gia_ManObjNum(pGia), 0 );              // cut offsets
    Vec_FltFill( &p->vCutFlows, Gia_ManObjNum(pGia), 0 );              // cut area
    Vec_IntFill( &p->vCutDelays,Gia_ManObjNum(pGia), 0 );              // cut delay
    Vec_IntGrow( &p->vBackup, 1000 );
    // references
    vFlowRefs = Vec_IntAlloc(0);
    Mf_ManSetFlowRefs( pGia, vFlowRefs );
    Vec_IntForEachEntry( vFlowRefs, Entry, i )
    {
        Vec_FltWriteEntry( &p->vFlowRefs, 2*i,   /*0.5* */Entry );
        Vec_FltWriteEntry( &p->vFlowRefs, 2*i+1, /*0.5* */Entry );
    }
    Vec_IntFree(vFlowRefs);
    // matching
404
    Mio_LibraryMatchesFetch( (Mio_Library_t *)Abc_FrameReadLibGen(), &p->vTtMem, &p->vTt2Match, &p->pCells, &p->nCells, p->pPars->fPinFilter, p->pPars->fPinPerm, p->pPars->fPinQuick );
405 406
    if ( p->pCells == NULL )
        return NULL;
407 408 409
    p->InvDelayI = p->pCells[3].iDelays[0];
    p->InvAreaW  = p->pCells[3].AreaW;
    p->InvAreaF  = p->pCells[3].AreaF;
410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522
    Nf_ObjMatchD(p, 0, 0)->Gate = 0;
    Nf_ObjMatchD(p, 0, 1)->Gate = 1;
    // prepare cuts
    return p;
}
void Nf_StoDelete( Nf_Man_t * p )
{
    Vec_PtrFreeData( &p->vPages );
    ABC_FREE( p->vPages.pArray );
    ABC_FREE( p->vMapRefs.pArray );
    ABC_FREE( p->vFlowRefs.pArray );
    ABC_FREE( p->vRequired.pArray );
    ABC_FREE( p->vCutSets.pArray );
    ABC_FREE( p->vCutFlows.pArray );
    ABC_FREE( p->vCutDelays.pArray );
    ABC_FREE( p->vBackup.pArray );
    ABC_FREE( p->pNfObjs );
    ABC_FREE( p );
}




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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline int Nf_CutComputeTruth6( Nf_Man_t * p, Nf_Cut_t * pCut0, Nf_Cut_t * pCut1, int fCompl0, int fCompl1, Nf_Cut_t * pCutR, int fIsXor )
{
//    extern int Nf_ManTruthCanonicize( word * t, int nVars );
    int nOldSupp = pCutR->nLeaves, truthId, fCompl; word t;
    word t0 = *Vec_MemReadEntry(p->vTtMem, Abc_Lit2Var(pCut0->iFunc));
    word t1 = *Vec_MemReadEntry(p->vTtMem, Abc_Lit2Var(pCut1->iFunc));
    if ( Abc_LitIsCompl(pCut0->iFunc) ^ fCompl0 ) t0 = ~t0;
    if ( Abc_LitIsCompl(pCut1->iFunc) ^ fCompl1 ) t1 = ~t1;
    t0 = Abc_Tt6Expand( t0, pCut0->pLeaves, pCut0->nLeaves, pCutR->pLeaves, pCutR->nLeaves );
    t1 = Abc_Tt6Expand( t1, pCut1->pLeaves, pCut1->nLeaves, pCutR->pLeaves, pCutR->nLeaves );
    t =  fIsXor ? t0 ^ t1 : t0 & t1;
    if ( (fCompl = (int)(t & 1)) ) t = ~t;
    pCutR->nLeaves = Abc_Tt6MinBase( &t, pCutR->pLeaves, pCutR->nLeaves );
    assert( (int)(t & 1) == 0 );
    truthId        = Vec_MemHashInsert(p->vTtMem, &t);
    pCutR->iFunc   = Abc_Var2Lit( truthId, fCompl );
    pCutR->Useless = Nf_ObjCutUseless( p, truthId );
    assert( (int)pCutR->nLeaves <= nOldSupp );
    return (int)pCutR->nLeaves < nOldSupp;
}
static inline int Nf_CutComputeTruthMux6( Nf_Man_t * p, Nf_Cut_t * pCut0, Nf_Cut_t * pCut1, Nf_Cut_t * pCutC, int fCompl0, int fCompl1, int fComplC, Nf_Cut_t * pCutR )
{
    int nOldSupp = pCutR->nLeaves, truthId, fCompl; word t;
    word t0 = *Vec_MemReadEntry(p->vTtMem, Abc_Lit2Var(pCut0->iFunc));
    word t1 = *Vec_MemReadEntry(p->vTtMem, Abc_Lit2Var(pCut1->iFunc));
    word tC = *Vec_MemReadEntry(p->vTtMem, Abc_Lit2Var(pCutC->iFunc));
    if ( Abc_LitIsCompl(pCut0->iFunc) ^ fCompl0 ) t0 = ~t0;
    if ( Abc_LitIsCompl(pCut1->iFunc) ^ fCompl1 ) t1 = ~t1;
    if ( Abc_LitIsCompl(pCutC->iFunc) ^ fComplC ) tC = ~tC;
    t0 = Abc_Tt6Expand( t0, pCut0->pLeaves, pCut0->nLeaves, pCutR->pLeaves, pCutR->nLeaves );
    t1 = Abc_Tt6Expand( t1, pCut1->pLeaves, pCut1->nLeaves, pCutR->pLeaves, pCutR->nLeaves );
    tC = Abc_Tt6Expand( tC, pCutC->pLeaves, pCutC->nLeaves, pCutR->pLeaves, pCutR->nLeaves );
    t = (tC & t1) | (~tC & t0);
    if ( (fCompl = (int)(t & 1)) ) t = ~t;
    pCutR->nLeaves = Abc_Tt6MinBase( &t, pCutR->pLeaves, pCutR->nLeaves );
    assert( (int)(t & 1) == 0 );
    truthId        = Vec_MemHashInsert(p->vTtMem, &t);
    pCutR->iFunc   = Abc_Var2Lit( truthId, fCompl );
    pCutR->Useless = Nf_ObjCutUseless( p, truthId );
    assert( (int)pCutR->nLeaves <= nOldSupp );
    return (int)pCutR->nLeaves < nOldSupp;
}


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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline int Nf_CutCountBits( word i )
{
    i = i - ((i >> 1) & 0x5555555555555555);
    i = (i & 0x3333333333333333) + ((i >> 2) & 0x3333333333333333);
    i = ((i + (i >> 4)) & 0x0F0F0F0F0F0F0F0F);
    return (i*(0x0101010101010101))>>56;
}
static inline word Nf_CutGetSign( int * pLeaves, int nLeaves )
{
    word Sign = 0; int i; 
    for ( i = 0; i < nLeaves; i++ )
        Sign |= ((word)1) << (pLeaves[i] & 0x3F);
    return Sign;
}
static inline int Nf_CutCreateUnit( Nf_Cut_t * p, int i )
{
    p->Delay      = 0;
    p->Flow       = 0;
    p->iFunc      = 2;
    p->nLeaves    = 1;
    p->pLeaves[0] = i;
    p->Sign       = ((word)1) << (i & 0x3F);
    return 1;
}
523
static inline void Nf_CutPrint( Nf_Man_t * p, Nf_Cut_t * pCut )
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
{
    int i, nDigits = Abc_Base10Log(Gia_ManObjNum(p->pGia)); 
    printf( "%d  {", pCut->nLeaves );
    for ( i = 0; i < (int)pCut->nLeaves; i++ )
        printf( " %*d", nDigits, pCut->pLeaves[i] );
    for ( ; i < (int)p->pPars->nLutSize; i++ )
        printf( " %*s", nDigits, " " );
    printf( "  }   Useless = %d. D = %4d  A = %9.4f  F = %6d  ", 
        pCut->Useless, pCut->Delay, pCut->Flow, pCut->iFunc );
    if ( p->vTtMem )
        Dau_DsdPrintFromTruth( Vec_MemReadEntry(p->vTtMem, Abc_Lit2Var(pCut->iFunc)), pCut->nLeaves );
    else
        printf( "\n" );
}
static inline int Nf_ManPrepareCuts( Nf_Cut_t * pCuts, Nf_Man_t * p, int iObj, int fAddUnit )
{
    if ( Nf_ObjHasCuts(p, iObj) )
    {
        Nf_Cut_t * pMfCut = pCuts;
        int i, * pCut, * pList = Nf_ObjCutSet(p, iObj);
        Nf_SetForEachCut( pList, pCut, i )
        {
            pMfCut->Delay   = 0;
            pMfCut->Flow    = 0;
            pMfCut->iFunc   = Nf_CutFunc( pCut );
            pMfCut->nLeaves = Nf_CutSize( pCut );
            pMfCut->Sign    = Nf_CutGetSign( pCut+1, Nf_CutSize(pCut) );
Alan Mishchenko committed
551
            pMfCut->Useless = Nf_ObjCutUseless( p, Abc_Lit2Var(pMfCut->iFunc) );
552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656
            memcpy( pMfCut->pLeaves, pCut+1, sizeof(int) * Nf_CutSize(pCut) );
            pMfCut++;
        }
        if ( fAddUnit && pCuts->nLeaves > 1 )
            return pList[0] + Nf_CutCreateUnit( pMfCut, iObj );
        return pList[0];
    }
    return Nf_CutCreateUnit( pCuts, iObj );
}
static inline int Nf_ManSaveCuts( Nf_Man_t * p, Nf_Cut_t ** pCuts, int nCuts, int fUseful )
{
    int i, * pPlace, iCur, nInts = 1, nCutsNew = 0;
    for ( i = 0; i < nCuts; i++ )
        if ( !fUseful || !pCuts[i]->Useless )
            nInts += pCuts[i]->nLeaves + 1, nCutsNew++;
    if ( (p->iCur & 0xFFFF) + nInts > 0xFFFF )
        p->iCur = ((p->iCur >> 16) + 1) << 16;
    if ( Vec_PtrSize(&p->vPages) == (p->iCur >> 16) )
        Vec_PtrPush( &p->vPages, ABC_ALLOC(int, (1<<16)) );
    iCur = p->iCur; p->iCur += nInts;
    pPlace = Nf_ManCutSet( p, iCur );
    *pPlace++ = nCutsNew;
    for ( i = 0; i < nCuts; i++ )
        if ( !fUseful || !pCuts[i]->Useless )
        {
            *pPlace++ = Nf_CutSetBoth( pCuts[i]->nLeaves, pCuts[i]->iFunc );
            memcpy( pPlace, pCuts[i]->pLeaves, sizeof(int) * pCuts[i]->nLeaves );
            pPlace += pCuts[i]->nLeaves;
        }
    return iCur;
}
static inline int Nf_ManCountUseful( Nf_Cut_t ** pCuts, int nCuts )
{
    int i, Count = 0;
    for ( i = 0; i < nCuts; i++ )
        Count += !pCuts[i]->Useless;
    return Count;
}
static inline int Nf_ManCountMatches( Nf_Man_t * p, Nf_Cut_t ** pCuts, int nCuts )
{
    int i, Count = 0;
    for ( i = 0; i < nCuts; i++ )
        if ( !pCuts[i]->Useless )
            Count += Vec_IntSize(Vec_WecEntry(p->vTt2Match, Abc_Lit2Var(pCuts[i]->iFunc))) / 2;
    return Count;
}

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

  Synopsis    [Check correctness of cuts.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline int Nf_CutCheck( Nf_Cut_t * pBase, Nf_Cut_t * pCut ) // check if pCut is contained in pBase
{
    int nSizeB = pBase->nLeaves;
    int nSizeC = pCut->nLeaves;
    int i, * pB = pBase->pLeaves;
    int k, * pC = pCut->pLeaves;
    for ( i = 0; i < nSizeC; i++ )
    {
        for ( k = 0; k < nSizeB; k++ )
            if ( pC[i] == pB[k] )
                break;
        if ( k == nSizeB )
            return 0;
    }
    return 1;
}
static inline int Nf_SetCheckArray( Nf_Cut_t ** ppCuts, int nCuts )
{
    Nf_Cut_t * pCut0, * pCut1; 
    int i, k, m, n, Value;
    assert( nCuts > 0 );
    for ( i = 0; i < nCuts; i++ )
    {
        pCut0 = ppCuts[i];
        assert( pCut0->nLeaves <= NF_LEAF_MAX );
        assert( pCut0->Sign == Nf_CutGetSign(pCut0->pLeaves, pCut0->nLeaves) );
        // check duplicates
        for ( m = 0; m < (int)pCut0->nLeaves; m++ )
        for ( n = m + 1; n < (int)pCut0->nLeaves; n++ )
            assert( pCut0->pLeaves[m] < pCut0->pLeaves[n] );
        // check pairs
        for ( k = 0; k < nCuts; k++ )
        {
            pCut1 = ppCuts[k];
            if ( pCut0 == pCut1 )
                continue;
            // check containments
            Value = Nf_CutCheck( pCut0, pCut1 );
            assert( Value == 0 );
        }
    }
    return 1;
}


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

657 658 659 660 661 662 663 664 665
  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681
static inline int Nf_CutMergeOrder( Nf_Cut_t * pCut0, Nf_Cut_t * pCut1, Nf_Cut_t * pCut, int nLutSize )
{ 
    int nSize0   = pCut0->nLeaves;
    int nSize1   = pCut1->nLeaves;
    int i, * pC0 = pCut0->pLeaves;
    int k, * pC1 = pCut1->pLeaves;
    int c, * pC  = pCut->pLeaves;
    // the case of the largest cut sizes
    if ( nSize0 == nLutSize && nSize1 == nLutSize )
    {
        for ( i = 0; i < nSize0; i++ )
        {
            if ( pC0[i] != pC1[i] )  return 0;
            pC[i] = pC0[i];
        }
        pCut->nLeaves = nLutSize;
Alan Mishchenko committed
682
        pCut->iFunc = NF_NO_FUNC;
683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715
        pCut->Sign = pCut0->Sign | pCut1->Sign;
        return 1;
    }
    // compare two cuts with different numbers
    i = k = c = 0;
    if ( nSize0 == 0 ) goto FlushCut1;
    if ( nSize1 == 0 ) goto FlushCut0;
    while ( 1 )
    {
        if ( c == nLutSize ) return 0;
        if ( pC0[i] < pC1[k] )
        {
            pC[c++] = pC0[i++];
            if ( i >= nSize0 ) goto FlushCut1;
        }
        else if ( pC0[i] > pC1[k] )
        {
            pC[c++] = pC1[k++];
            if ( k >= nSize1 ) goto FlushCut0;
        }
        else
        {
            pC[c++] = pC0[i++]; k++;
            if ( i >= nSize0 ) goto FlushCut1;
            if ( k >= nSize1 ) goto FlushCut0;
        }
    }

FlushCut0:
    if ( c + nSize0 > nLutSize + i ) return 0;
    while ( i < nSize0 )
        pC[c++] = pC0[i++];
    pCut->nLeaves = c;
Alan Mishchenko committed
716
    pCut->iFunc = NF_NO_FUNC;
717 718 719 720 721 722 723 724
    pCut->Sign = pCut0->Sign | pCut1->Sign;
    return 1;

FlushCut1:
    if ( c + nSize1 > nLutSize + k ) return 0;
    while ( k < nSize1 )
        pC[c++] = pC1[k++];
    pCut->nLeaves = c;
Alan Mishchenko committed
725
    pCut->iFunc = NF_NO_FUNC;
726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748
    pCut->Sign = pCut0->Sign | pCut1->Sign;
    return 1;
}
static inline int Nf_CutMergeOrderMux( Nf_Cut_t * pCut0, Nf_Cut_t * pCut1, Nf_Cut_t * pCut2, Nf_Cut_t * pCut, int nLutSize )
{ 
    int x0, i0 = 0, nSize0 = pCut0->nLeaves, * pC0 = pCut0->pLeaves;
    int x1, i1 = 0, nSize1 = pCut1->nLeaves, * pC1 = pCut1->pLeaves;
    int x2, i2 = 0, nSize2 = pCut2->nLeaves, * pC2 = pCut2->pLeaves;
    int xMin, c = 0, * pC  = pCut->pLeaves;
    while ( 1 )
    {
        x0 = (i0 == nSize0) ? ABC_INFINITY : pC0[i0];
        x1 = (i1 == nSize1) ? ABC_INFINITY : pC1[i1];
        x2 = (i2 == nSize2) ? ABC_INFINITY : pC2[i2];
        xMin = Abc_MinInt( Abc_MinInt(x0, x1), x2 );
        if ( xMin == ABC_INFINITY ) break;
        if ( c == nLutSize ) return 0;
        pC[c++] = xMin;
        if (x0 == xMin) i0++;
        if (x1 == xMin) i1++;
        if (x2 == xMin) i2++;
    }
    pCut->nLeaves = c;
Alan Mishchenko committed
749
    pCut->iFunc = NF_NO_FUNC;
750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808
    pCut->Sign = pCut0->Sign | pCut1->Sign | pCut2->Sign;
    return 1;
}
static inline int Nf_SetCutIsContainedOrder( Nf_Cut_t * pBase, Nf_Cut_t * pCut ) // check if pCut is contained in pBase
{
    int i, nSizeB = pBase->nLeaves;
    int k, nSizeC = pCut->nLeaves;
    if ( nSizeB == nSizeC )
    {
        for ( i = 0; i < nSizeB; i++ )
            if ( pBase->pLeaves[i] != pCut->pLeaves[i] )
                return 0;
        return 1;
    }
    assert( nSizeB > nSizeC ); 
    if ( nSizeC == 0 )
        return 1;
    for ( i = k = 0; i < nSizeB; i++ )
    {
        if ( pBase->pLeaves[i] > pCut->pLeaves[k] )
            return 0;
        if ( pBase->pLeaves[i] == pCut->pLeaves[k] )
        {
            if ( ++k == nSizeC )
                return 1;
        }
    }
    return 0;
}
static inline int Nf_SetLastCutIsContained( Nf_Cut_t ** pCuts, int nCuts )
{
    int i;
    for ( i = 0; i < nCuts; i++ )
        if ( pCuts[i]->nLeaves <= pCuts[nCuts]->nLeaves && (pCuts[i]->Sign & pCuts[nCuts]->Sign) == pCuts[i]->Sign && Nf_SetCutIsContainedOrder(pCuts[nCuts], pCuts[i]) )
            return 1;
    return 0;
}
static inline int Nf_SetLastCutContainsArea( Nf_Cut_t ** pCuts, int nCuts )
{
    int i, k, fChanges = 0;
    for ( i = 0; i < nCuts; i++ )
        if ( pCuts[nCuts]->nLeaves < pCuts[i]->nLeaves && (pCuts[nCuts]->Sign & pCuts[i]->Sign) == pCuts[nCuts]->Sign && Nf_SetCutIsContainedOrder(pCuts[i], pCuts[nCuts]) )
            pCuts[i]->nLeaves = NF_NO_LEAF, fChanges = 1;
    if ( !fChanges )
        return nCuts;
    for ( i = k = 0; i <= nCuts; i++ )
    {
        if ( pCuts[i]->nLeaves == NF_NO_LEAF )
            continue;
        if ( k < i )
            ABC_SWAP( Nf_Cut_t *, pCuts[k], pCuts[i] );
        k++;
    }
    return k - 1;
}
static inline int Nf_CutCompareArea( Nf_Cut_t * pCut0, Nf_Cut_t * pCut1 )
{
    if ( pCut0->Useless < pCut1->Useless )  return -1;
    if ( pCut0->Useless > pCut1->Useless )  return  1;
809 810
    if ( pCut0->Flow    < pCut1->Flow - NF_EPSILON )  return -1;
    if ( pCut0->Flow    > pCut1->Flow + NF_EPSILON )  return  1;
811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858
    if ( pCut0->Delay   < pCut1->Delay   )  return -1;
    if ( pCut0->Delay   > pCut1->Delay   )  return  1;
    if ( pCut0->nLeaves < pCut1->nLeaves )  return -1;
    if ( pCut0->nLeaves > pCut1->nLeaves )  return  1;
    return 0;
}
static inline void Nf_SetSortByArea( Nf_Cut_t ** pCuts, int nCuts )
{
    int i;
    for ( i = nCuts; i > 0; i-- )
    {
        if ( Nf_CutCompareArea(pCuts[i - 1], pCuts[i]) < 0 )//!= 1 )
            return;
        ABC_SWAP( Nf_Cut_t *, pCuts[i - 1], pCuts[i] );
    }
}
static inline int Nf_SetAddCut( Nf_Cut_t ** pCuts, int nCuts, int nCutNum )
{
    if ( nCuts == 0 )
        return 1;
    nCuts = Nf_SetLastCutContainsArea(pCuts, nCuts);
    Nf_SetSortByArea( pCuts, nCuts );
    return Abc_MinInt( nCuts + 1, nCutNum - 1 );
}
static inline int Nf_CutArea( Nf_Man_t * p, int nLeaves )
{
    if ( nLeaves < 2 )
        return 0;
    return nLeaves + p->pPars->nAreaTuner;
}
static inline void Nf_CutParams( Nf_Man_t * p, Nf_Cut_t * pCut, float FlowRefs )
{
    int i, nLeaves = pCut->nLeaves; 
    assert( nLeaves <= p->pPars->nLutSize );
    pCut->Delay = 0;
    pCut->Flow  = 0;
    for ( i = 0; i < nLeaves; i++ )
    {
        pCut->Delay = Abc_MaxInt( pCut->Delay, Nf_ObjCutDelay(p, pCut->pLeaves[i]) );
        pCut->Flow += Nf_ObjCutFlow(p, pCut->pLeaves[i]);
    }
    pCut->Delay += (int)(nLeaves > 1);
    pCut->Flow = (pCut->Flow + Nf_CutArea(p, nLeaves)) / FlowRefs;
}
void Nf_ObjMergeOrder( Nf_Man_t * p, int iObj )
{
    Nf_Cut_t pCuts0[NF_CUT_MAX], pCuts1[NF_CUT_MAX], pCuts[NF_CUT_MAX], * pCutsR[NF_CUT_MAX];
    Gia_Obj_t * pObj = Gia_ManObj(p->pGia, iObj);
Alan Mishchenko committed
859
    //Nf_Obj_t * pBest = Nf_ManObj(p, iObj);
860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940
    float dFlowRefs  = Nf_ObjFlowRefs(p, iObj, 0) + Nf_ObjFlowRefs(p, iObj, 1);
    int nLutSize = p->pPars->nLutSize;
    int nCutNum  = p->pPars->nCutNum;
    int nCuts0   = Nf_ManPrepareCuts(pCuts0, p, Gia_ObjFaninId0(pObj, iObj), 1);
    int nCuts1   = Nf_ManPrepareCuts(pCuts1, p, Gia_ObjFaninId1(pObj, iObj), 1);
    int fComp0   = Gia_ObjFaninC0(pObj);
    int fComp1   = Gia_ObjFaninC1(pObj);
    int iSibl    = Gia_ObjSibl(p->pGia, iObj);
    Nf_Cut_t * pCut0, * pCut1, * pCut0Lim = pCuts0 + nCuts0, * pCut1Lim = pCuts1 + nCuts1;
    int i, nCutsUse, nCutsR = 0;
    assert( !Gia_ObjIsBuf(pObj) );
    for ( i = 0; i < nCutNum; i++ )
        pCutsR[i] = pCuts + i;
    if ( iSibl )
    {
        Nf_Cut_t pCuts2[NF_CUT_MAX];
        Gia_Obj_t * pObjE = Gia_ObjSiblObj(p->pGia, iObj);
        int fCompE = Gia_ObjPhase(pObj) ^ Gia_ObjPhase(pObjE);
        int nCuts2 = Nf_ManPrepareCuts(pCuts2, p, iSibl, 0);
        Nf_Cut_t * pCut2, * pCut2Lim = pCuts2 + nCuts2;
        for ( pCut2 = pCuts2; pCut2 < pCut2Lim; pCut2++ )
        {
            *pCutsR[nCutsR] = *pCut2;
            pCutsR[nCutsR]->iFunc = Abc_LitNotCond( pCutsR[nCutsR]->iFunc, fCompE );
            Nf_CutParams( p, pCutsR[nCutsR], dFlowRefs );
            nCutsR = Nf_SetAddCut( pCutsR, nCutsR, nCutNum );
        }
    }
    if ( Gia_ObjIsMuxId(p->pGia, iObj) )
    {
        Nf_Cut_t pCuts2[NF_CUT_MAX];
        int nCuts2  = Nf_ManPrepareCuts(pCuts2, p, Gia_ObjFaninId2(p->pGia, iObj), 1);
        int fComp2  = Gia_ObjFaninC2(p->pGia, pObj);
        Nf_Cut_t * pCut2, * pCut2Lim = pCuts2 + nCuts2;
        p->CutCount[0] += nCuts0 * nCuts1 * nCuts2;
        for ( pCut0 = pCuts0; pCut0 < pCut0Lim; pCut0++ )
        for ( pCut1 = pCuts1; pCut1 < pCut1Lim; pCut1++ )
        for ( pCut2 = pCuts2; pCut2 < pCut2Lim; pCut2++ )
        {
            if ( Nf_CutCountBits(pCut0->Sign | pCut1->Sign | pCut2->Sign) > nLutSize )
                continue;
            p->CutCount[1]++; 
            if ( !Nf_CutMergeOrderMux(pCut0, pCut1, pCut2, pCutsR[nCutsR], nLutSize) )
                continue;
            if ( Nf_SetLastCutIsContained(pCutsR, nCutsR) )
                continue;
            p->CutCount[2]++;
            if ( Nf_CutComputeTruthMux6(p, pCut0, pCut1, pCut2, fComp0, fComp1, fComp2, pCutsR[nCutsR]) )
                pCutsR[nCutsR]->Sign = Nf_CutGetSign(pCutsR[nCutsR]->pLeaves, pCutsR[nCutsR]->nLeaves);
            Nf_CutParams( p, pCutsR[nCutsR], dFlowRefs );
            nCutsR = Nf_SetAddCut( pCutsR, nCutsR, nCutNum );
        }
    }
    else
    {
        int fIsXor = Gia_ObjIsXor(pObj);
        p->CutCount[0] += nCuts0 * nCuts1;
        for ( pCut0 = pCuts0; pCut0 < pCut0Lim; pCut0++ )
        for ( pCut1 = pCuts1; pCut1 < pCut1Lim; pCut1++ )
        {
            if ( (int)(pCut0->nLeaves + pCut1->nLeaves) > nLutSize && Nf_CutCountBits(pCut0->Sign | pCut1->Sign) > nLutSize )
                continue;
            p->CutCount[1]++; 
            if ( !Nf_CutMergeOrder(pCut0, pCut1, pCutsR[nCutsR], nLutSize) )
                continue;
            if ( Nf_SetLastCutIsContained(pCutsR, nCutsR) )
                continue;
            p->CutCount[2]++;
            if ( Nf_CutComputeTruth6(p, pCut0, pCut1, fComp0, fComp1, pCutsR[nCutsR], fIsXor) )
                pCutsR[nCutsR]->Sign = Nf_CutGetSign(pCutsR[nCutsR]->pLeaves, pCutsR[nCutsR]->nLeaves);
            Nf_CutParams( p, pCutsR[nCutsR], dFlowRefs );
            nCutsR = Nf_SetAddCut( pCutsR, nCutsR, nCutNum );
        }
    }
    // debug printout
    if ( 0 )
//    if ( iObj % 10000 == 0 )
//    if ( iObj == 1090 )
    {
        printf( "*** Obj = %d  Useful = %d\n", iObj, Nf_ManCountUseful(pCutsR, nCutsR) );
        for ( i = 0; i < nCutsR; i++ )
941
            Nf_CutPrint( p, pCutsR[i] );
942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989
        printf( "\n" );
    } 
    // verify
    assert( nCutsR > 0 && nCutsR < nCutNum );
//    assert( Nf_SetCheckArray(pCutsR, nCutsR) );
    // store the cutset
    Nf_ObjSetCutFlow( p, iObj, pCutsR[0]->Flow );
    Nf_ObjSetCutDelay( p, iObj, pCutsR[0]->Delay );
    *Vec_IntEntryP(&p->vCutSets, iObj) = Nf_ManSaveCuts(p, pCutsR, nCutsR, 0);
    p->CutCount[3] += nCutsR;
    nCutsUse = Nf_ManCountUseful(pCutsR, nCutsR);
    p->CutCount[4] += nCutsUse;
    p->nCutUseAll  += nCutsUse == nCutsR;
    p->CutCount[5] += Nf_ManCountMatches(p, pCutsR, nCutsR);
}
void Nf_ManComputeCuts( Nf_Man_t * p )
{
    Gia_Obj_t * pObj; int i, iFanin;
    Gia_ManForEachAnd( p->pGia, pObj, i )
        if ( Gia_ObjIsBuf(pObj) )
        {
            iFanin = Gia_ObjFaninId0(pObj, i);
            Nf_ObjSetCutFlow( p, i,  Nf_ObjCutFlow(p, iFanin) );
            Nf_ObjSetCutDelay( p, i, Nf_ObjCutDelay(p, iFanin) );
        }
        else
            Nf_ObjMergeOrder( p, i );
}




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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Nf_ManPrintStats( Nf_Man_t * p, char * pTitle )
{
    if ( !p->pPars->fVerbose )
        return;
    printf( "%s :  ", pTitle );
990
    printf( "Delay =%8.2f  ",  Scl_Int2Flt(p->pPars->MapDelay) );
991
    printf( "Area =%12.2f  ",  p->pPars->MapAreaF );
992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004
    printf( "Gate =%6d  ",    (int)p->pPars->Area );
    printf( "Inv =%6d  ",     (int)p->nInvs );
    printf( "Edge =%7d  ",    (int)p->pPars->Edge );
    Abc_PrintTime( 1, "Time", Abc_Clock() - p->clkStart );
    fflush( stdout );
}
void Nf_ManPrintInit( Nf_Man_t * p )
{
    int nChoices;
    if ( !p->pPars->fVerbose )
        return;
    printf( "LutSize = %d  ", p->pPars->nLutSize );
    printf( "CutNum = %d  ",  p->pPars->nCutNum );
1005
    printf( "Iter = %d  ",    p->pPars->nRounds );//+ p->pPars->nRoundsEla );
1006 1007 1008 1009
    printf( "Coarse = %d   ", p->pPars->fCoarsen );
    printf( "Cells = %d  ",   p->nCells );
    printf( "Funcs = %d  ",   Vec_MemEntryNum(p->vTtMem) );
    printf( "Matches = %d  ", Vec_WecSizeSize(p->vTt2Match)/2 );
1010
    printf( "And = %d  ",     Gia_ManAndNum(p->pGia) );
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057
    nChoices = Gia_ManChoiceNum( p->pGia );
    if ( nChoices )
    printf( "Choices = %d  ", nChoices );
    printf( "\n" );
    printf( "Computing cuts...\r" );
    fflush( stdout );
}
void Nf_ManPrintQuit( Nf_Man_t * p )
{
    float MemGia   = Gia_ManMemory(p->pGia) / (1<<20);
    float MemMan   =(1.0 * sizeof(Nf_Obj_t) + 8.0 * sizeof(int)) * Gia_ManObjNum(p->pGia) / (1<<20);
    float MemCuts  = 1.0 * sizeof(int) * (1 << 16) * Vec_PtrSize(&p->vPages) / (1<<20);
    float MemTt    = p->vTtMem ? Vec_MemMemory(p->vTtMem) / (1<<20) : 0;
    if ( p->CutCount[0] == 0 )
        p->CutCount[0] = 1;
    if ( !p->pPars->fVerbose )
        return;
    printf( "CutPair = %.0f  ",         p->CutCount[0] );
    printf( "Merge = %.0f (%.1f)  ",    p->CutCount[1], 1.0*p->CutCount[1]/Gia_ManAndNum(p->pGia) );
    printf( "Eval = %.0f (%.1f)  ",     p->CutCount[2], 1.0*p->CutCount[2]/Gia_ManAndNum(p->pGia) );
    printf( "Cut = %.0f (%.1f)  ",      p->CutCount[3], 1.0*p->CutCount[3]/Gia_ManAndNum(p->pGia) );
    printf( "Use = %.0f (%.1f)  ",      p->CutCount[4], 1.0*p->CutCount[4]/Gia_ManAndNum(p->pGia) );
    printf( "Mat = %.0f (%.1f)  ",      p->CutCount[5], 1.0*p->CutCount[5]/Gia_ManAndNum(p->pGia) );
//    printf( "Equ = %d (%.2f %%)  ",     p->nCutUseAll,  100.0*p->nCutUseAll /p->CutCount[0] );
    printf( "\n" );
    printf( "Gia = %.2f MB  ",          MemGia );
    printf( "Man = %.2f MB  ",          MemMan ); 
    printf( "Cut = %.2f MB   ",         MemCuts );
    printf( "TT = %.2f MB  ",           MemTt ); 
    printf( "Total = %.2f MB   ",       MemGia + MemMan + MemCuts + MemTt ); 
//    printf( "\n" );
    Abc_PrintTime( 1, "Time", Abc_Clock() - p->clkStart );
    fflush( stdout );
}


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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
1058
void Nf_ManCutMatchPrint( Nf_Man_t * p, int iObj, char * pStr, Nf_Mat_t * pM )
1059
{
1060
    Mio_Cell2_t * pCell;
1061
    int i, * pCut;
1062
    printf( "%5d %s : ", iObj, pStr );
1063 1064 1065 1066 1067 1068 1069
    if ( pM->CutH == 0 )
    {
        printf( "Unassigned\n" );
        return;
    }
    pCell = Nf_ManCell( p, pM->Gate );
    pCut = Nf_CutFromHandle( Nf_ObjCutSet(p, iObj), pM->CutH );
1070
    printf( "D =%6.2f  ", Scl_Int2Flt(pM->D) );
1071
    printf( "A =%6.2f  ", pM->F );
1072 1073 1074 1075 1076
    printf( "C = %d ", pM->fCompl );
//    printf( "B = %d ", pM->fBest );
    printf( "  " );
    printf( "Cut = {" );
    for ( i = 0; i < (int)pCell->nFanins; i++ )
1077
        printf( "%4d ", Nf_CutLeaves(pCut)[i] );
1078
    for ( ; i < 6; i++ )
1079
        printf( "     " );
1080
    printf( "}  " );
1081
    printf( "%10s ", pCell->pName );
1082 1083 1084
    printf( "%d  ", pCell->nFanins );
    printf( "{" );
    for ( i = 0; i < (int)pCell->nFanins; i++ )
1085
        printf( "%6.2f ", Scl_Int2Flt(pCell->iDelays[i]) );
1086
    for ( ; i < 6; i++ )
1087 1088
        printf( "       " );
    printf( " } " );
1089
    for ( i = 0; i < (int)pCell->nFanins; i++ )
1090
        printf( "%s%d ", Nf_CfgCompl(pM->Cfg, i) ? "!":" ", Nf_CfgVar(pM->Cfg, i) );
1091
    for ( ; i < 6; i++ )
1092
        printf( "  " );
1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103
    Dau_DsdPrintFromTruth( &pCell->uTruth, pCell->nFanins );
}
void Nf_ManCutMatchOne( Nf_Man_t * p, int iObj, int * pCut, int * pCutSet )
{
    Nf_Obj_t * pBest = Nf_ManObj(p, iObj);
    int * pFans      = Nf_CutLeaves(pCut);
    int nFans        = Nf_CutSize(pCut);
    int iFuncLit     = Nf_CutFunc(pCut);
    int fComplExt    = Abc_LitIsCompl(iFuncLit);
    Vec_Int_t * vArr = Vec_WecEntry( p->vTt2Match, Abc_Lit2Var(iFuncLit) );
    int i, k, c, Info, Offset, iFanin, fComplF;
1104
    int ArrivalD, ArrivalA;
1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119
    Nf_Mat_t * pD, * pA;
    // assign fanins matches
    Nf_Obj_t * pBestF[NF_LEAF_MAX];
    for ( i = 0; i < nFans; i++ )
        pBestF[i] = Nf_ManObj( p, pFans[i] );
    // special cases
    if ( nFans == 0 )
    {
        int Const = (iFuncLit == 1);
        assert( iFuncLit == 0 || iFuncLit == 1 );
        for ( c = 0; c < 2; c++ )
        { 
            pD = Nf_ObjMatchD( p, iObj, c );
            pA = Nf_ObjMatchA( p, iObj, c );
            pD->D = pA->D = 0;
1120
            pD->F = pA->F = p->pCells[c ^ Const].AreaF;
1121 1122
            pD->CutH = pA->CutH = Nf_CutHandle(pCutSet, pCut);
            pD->Gate = pA->Gate = c ^ Const;
1123 1124
//            pD->Conf = pA->Conf = 0;
            pD->Cfg = pA->Cfg = Nf_Int2Cfg(0);
1125 1126 1127 1128 1129 1130
        }
        return;
    }
    // consider matches of this function
    Vec_IntForEachEntryDouble( vArr, Info, Offset, i )
    {
1131
        Nf_Cfg_t Cfg   = Nf_Int2Cfg(Offset);
1132
        Mio_Cell2_t*pC = Nf_ManCell( p, Info );
1133
        int fCompl     = Cfg.fCompl ^ fComplExt;
1134
        int Required   = Nf_ObjRequired( p, iObj, fCompl ), Delay = 0;
1135 1136
        Nf_Mat_t * pD  = &pBest->M[fCompl][0];
        Nf_Mat_t * pA  = &pBest->M[fCompl][1];
1137
        float AreaF    = pC->AreaF;
1138
        assert( nFans == (int)pC->nFanins );
1139
        Nf_CfgForEachVarCompl( Cfg, nFans, iFanin, fComplF, k )
1140
        {
1141 1142
            ArrivalD  = pBestF[iFanin]->M[fComplF][0].D;
            ArrivalA  = pBestF[iFanin]->M[fComplF][1].D;
1143
            if ( ArrivalA + pC->iDelays[k] <= Required && Required != SCL_INFINITY )
1144
            {
1145
                Delay = Abc_MaxInt( Delay, ArrivalA + pC->iDelays[k] );
1146
                AreaF += pBestF[iFanin]->M[fComplF][1].F;
1147
            }
1148
            else 
1149
            {
1150
                if ( pD->D < SCL_INFINITY && pA->D < SCL_INFINITY && ArrivalD + pC->iDelays[k] > Required )
1151
                    break;
1152
                Delay = Abc_MaxInt( Delay, ArrivalD + pC->iDelays[k] );
1153 1154 1155 1156 1157
                //AreaF += pBestF[iFanin]->M[fComplF][0].F;
                if ( AreaF >= (float)1e32 || pBestF[iFanin]->M[fComplF][0].F >= (float)1e32 )
                    AreaF = (float)1e32;
                else 
                    AreaF += pBestF[iFanin]->M[fComplF][0].F;
1158 1159
            }
        }
1160 1161
        if ( k < nFans )
            continue;
1162
        // select best Cfgch
1163
        if ( pD->D > Delay )
1164
        {
1165
            pD->D = Delay;
1166
            pD->F = AreaF;
1167 1168
            pD->CutH = Nf_CutHandle(pCutSet, pCut);
            pD->Gate = pC->Id;
1169 1170
            pD->Cfg = Cfg;
            pD->Cfg.fCompl = 0;
1171
        }
1172

1173
        if ( pA->F > AreaF + NF_EPSILON )
1174
        {
1175
            pA->D = Delay;
1176
            pA->F = AreaF;
1177 1178
            pA->CutH = Nf_CutHandle(pCutSet, pCut);
            pA->Gate = pC->Id;
1179 1180
            pA->Cfg = Cfg;
            pA->Cfg.fCompl = 0;
1181 1182 1183
        }
    }
}
1184
static inline void Nf_ObjPrepareCi( Nf_Man_t * p, int iObj, int Time )
1185
{
1186 1187
    Nf_Mat_t * pD0 = Nf_ObjMatchD( p, iObj, 0 );
    Nf_Mat_t * pA0 = Nf_ObjMatchA( p, iObj, 0 );
1188 1189
    Nf_Mat_t * pD = Nf_ObjMatchD( p, iObj, 1 );
    Nf_Mat_t * pA = Nf_ObjMatchA( p, iObj, 1 );
1190
    pD0->D = pA0->D = pD->D = pA->D = Time;
1191
    pD->fCompl = 1;
1192
    pD->D += p->InvDelayI;
1193
    pD->F = p->InvAreaF;
1194
    pA->fCompl = 1;
1195
    pA->D += p->InvDelayI;
1196
    pA->F = p->InvAreaF;
1197 1198 1199 1200 1201 1202 1203 1204 1205
    Nf_ObjMatchD( p, iObj, 0 )->fBest = 1;
    Nf_ObjMatchD( p, iObj, 1 )->fBest = 1;
}
static inline void Nf_ObjPrepareBuf( Nf_Man_t * p, Gia_Obj_t * pObj )
{
    // get fanin info
    int iObj = Gia_ObjId( p->pGia, pObj );
    int iFanin = Gia_ObjFaninId0( pObj, iObj );
    Nf_Mat_t * pDf = Nf_ObjMatchD( p, iFanin, Gia_ObjFaninC0(pObj) );
Alan Mishchenko committed
1206
    //Nf_Mat_t * pAf = Nf_ObjMatchA( p, iFanin, Gia_ObjFaninC0(pObj) );
1207 1208 1209 1210 1211 1212 1213 1214 1215
    // set the direct phase
    Nf_Mat_t * pDp = Nf_ObjMatchD( p, iObj, 0 );
    Nf_Mat_t * pAp = Nf_ObjMatchA( p, iObj, 0 );
    Nf_Mat_t * pDn = Nf_ObjMatchD( p, iObj, 1 );
    Nf_Mat_t * pAn = Nf_ObjMatchA( p, iObj, 1 );
    assert( Gia_ObjIsBuf(pObj) );
    memset( Nf_ManObj(p, iObj), 0, sizeof(Nf_Obj_t) );
    // set the direct phase
    pDp->D = pAp->D = pDf->D;
1216
    pDp->F = pAp->F = pDf->F; // do not pass flow???
1217 1218
    pDp->fBest = 1;
    // set the inverted phase
1219
    pDn->D = pAn->D = pDf->D + p->InvDelayI;
1220
    pDn->F = pAn->F = pDf->F + p->InvAreaF;
1221 1222 1223
    pDn->fCompl = pAn->fCompl = 1;
    pDn->fBest = 1;
}
1224
static inline int Nf_CutRequired( Nf_Man_t * p, Nf_Mat_t * pM, int * pCutSet )
1225
{
1226
    Mio_Cell2_t * pCell = Nf_ManCell( p, pM->Gate );
1227 1228
    int * pCut = Nf_CutFromHandle( pCutSet, pM->CutH );
    int i, iVar, fCompl;
1229
    int Arr, Req, Arrival = 0, Required = 0;
1230
    Nf_CutForEachVarCompl( pCut, pM->Cfg, iVar, fCompl, i )
1231
    {
1232
        Arr     = Nf_ManObj(p, iVar)->M[fCompl][0].D + pCell->iDelays[i];
1233
        Req     = Nf_ObjRequired(p, iVar, fCompl);
1234 1235 1236
        Arrival = Abc_MaxInt( Arrival, Arr );
        if ( Req < SCL_INFINITY )
            Required = Abc_MaxInt( Required, Req + pCell->iDelays[i] );
1237
    }
1238
    return Abc_MaxInt( Required + p->pPars->nReqTimeFlex*p->InvDelayI, Arrival ); 
1239 1240 1241 1242 1243 1244
}
static inline void Nf_ObjComputeRequired( Nf_Man_t * p, int iObj )
{
    Nf_Obj_t * pBest = Nf_ManObj(p, iObj);
    int c, * pCutSet = Nf_ObjCutSet( p, iObj );
    for ( c = 0; c < 2; c++ )
1245
        if ( Nf_ObjRequired(p, iObj, c) == SCL_INFINITY )
1246 1247 1248 1249 1250 1251 1252 1253 1254
            Nf_ObjSetRequired( p, iObj, c, Nf_CutRequired(p, &pBest->M[c][0], pCutSet) );
}
void Nf_ManCutMatch( Nf_Man_t * p, int iObj )
{
    Nf_Obj_t * pBest = Nf_ManObj(p, iObj);
    Nf_Mat_t * pDp = &pBest->M[0][0];
    Nf_Mat_t * pDn = &pBest->M[1][0];
    Nf_Mat_t * pAp = &pBest->M[0][1];
    Nf_Mat_t * pAn = &pBest->M[1][1];
1255 1256
    float FlowRefPf  = Nf_ObjFlowRefs(p, iObj, 0);
    float FlowRefNf  = Nf_ObjFlowRefs(p, iObj, 1);
1257
    int i, * pCut, * pCutSet = Nf_ObjCutSet( p, iObj );
1258
    int Required[2] = {0};
1259 1260 1261 1262 1263 1264 1265
    if ( p->Iter )
    {
        Nf_ObjComputeRequired( p, iObj );
        Required[0] = Nf_ObjRequired( p, iObj, 0 );
        Required[1] = Nf_ObjRequired( p, iObj, 1 );
    }
    memset( pBest, 0, sizeof(Nf_Obj_t) );
1266 1267 1268 1269
    pDp->D = SCL_INFINITY; pDp->F = FLT_MAX;
    pDn->D = SCL_INFINITY; pDn->F = FLT_MAX;
    pAp->D = SCL_INFINITY; pAp->F = FLT_MAX;
    pAn->D = SCL_INFINITY; pAn->F = FLT_MAX;
1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280
    Nf_SetForEachCut( pCutSet, pCut, i )
    {
        if ( Abc_Lit2Var(Nf_CutFunc(pCut)) >= Vec_WecSize(p->vTt2Match) )
            continue;
        assert( !Nf_CutIsTriv(pCut, iObj) );
        assert( Nf_CutSize(pCut) <= p->pPars->nLutSize );
        assert( Abc_Lit2Var(Nf_CutFunc(pCut)) < Vec_WecSize(p->vTt2Match) );
        Nf_ManCutMatchOne( p, iObj, pCut, pCutSet );
    }

/*
1281
    if ( 461 == iObj && p->Iter == 0 )
1282
    {
1283
        printf( "\nObj %6d (%.2f %.2f):\n", iObj, Scl_Int2Flt(Required[0]), Scl_Int2Flt(Required[1]) );
1284 1285 1286 1287
        Nf_ManCutMatchPrint( p, iObj, "Dp", &pBest->M[0][0] );
        Nf_ManCutMatchPrint( p, iObj, "Dn", &pBest->M[1][0] );
        Nf_ManCutMatchPrint( p, iObj, "Ap", &pBest->M[0][1] );
        Nf_ManCutMatchPrint( p, iObj, "An", &pBest->M[1][1] );
1288 1289 1290 1291
        printf( "\n" );
    }
*/
    // divide by ref count
1292 1293 1294 1295
    pDp->F = pDp->F / FlowRefPf;
    pAp->F = pAp->F / FlowRefPf;
    pDn->F = pDn->F / FlowRefNf;
    pAn->F = pAn->F / FlowRefNf;
1296 1297

    // add the inverters
1298 1299
    assert( pDp->D < SCL_INFINITY || pDn->D < SCL_INFINITY );
    if ( pDp->D > pDn->D + p->InvDelayI )
1300 1301
    {
        *pDp = *pDn;
1302
        pDp->D += p->InvDelayI;
1303
        pDp->F += p->InvAreaF;
1304
        pDp->fCompl = 1;
1305
        if ( pAp->D == SCL_INFINITY )
1306 1307 1308
            *pAp = *pDp;
        //printf( "Using inverter to improve delay at node %d in phase %d.\n", iObj, 1 );
    }
1309
    else if ( pDn->D > pDp->D + p->InvDelayI )
1310 1311
    {
        *pDn = *pDp;
1312
        pDn->D += p->InvDelayI;
1313
        pDn->F += p->InvAreaF;
1314
        pDn->fCompl = 1;
1315
        if ( pAn->D == SCL_INFINITY )
1316 1317 1318
            *pAn = *pDn;
        //printf( "Using inverter to improve delay at node %d in phase %d.\n", iObj, 0 );
    }
1319
    //assert( pAp->F < FLT_MAX || pAn->F < FLT_MAX );
1320
    // try replacing pos with neg
1321
    if ( pAp->D == SCL_INFINITY || (pAp->F > pAn->F + p->InvAreaF + NF_EPSILON && pAn->D + p->InvDelayI <= Required[0]) )
1322 1323 1324
    {
        assert( p->Iter > 0 );
        *pAp = *pAn;
1325
        pAp->D += p->InvDelayI;
1326
        pAp->F += p->InvAreaF;
1327
        pAp->fCompl = 1;
1328
        if ( pDp->D == SCL_INFINITY )
1329 1330 1331 1332
            *pDp = *pAp;
        //printf( "Using inverter to improve area at node %d in phase %d.\n", iObj, 1 );
    }
    // try replacing neg with pos
1333
    else if ( pAn->D == SCL_INFINITY || (pAn->F > pAp->F + p->InvAreaF + NF_EPSILON && pAp->D + p->InvDelayI <= Required[1]) )
1334 1335 1336
    {
        assert( p->Iter > 0 );
        *pAn = *pAp;
1337
        pAn->D += p->InvDelayI;
1338
        pAn->F += p->InvAreaF;
1339
        pAn->fCompl = 1;
1340
        if ( pDn->D == SCL_INFINITY )
1341 1342 1343 1344
            *pDn = *pAn;
        //printf( "Using inverter to improve area at node %d in phase %d.\n", iObj, 0 );
    }

1345
    if ( pDp->D == SCL_INFINITY )
1346
        printf( "Object %d has pDp unassigned.\n", iObj );
1347
    if ( pDn->D == SCL_INFINITY )
1348
        printf( "Object %d has pDn unassigned.\n", iObj );
1349
    if ( pAp->D == SCL_INFINITY )
1350
        printf( "Object %d has pAp unassigned.\n", iObj );
1351
    if ( pAn->D == SCL_INFINITY )
1352
        printf( "Object %d has pAn unassigned.\n", iObj );
1353
/*
1354 1355 1356 1357
    pDp->F = Abc_MinFloat( pDp->F, FLT_MAX/SCL_NUM );
    pDn->F = Abc_MinFloat( pDn->F, FLT_MAX/SCL_NUM );
    pAp->F = Abc_MinFloat( pAp->F, FLT_MAX/SCL_NUM );  
    pAn->F = Abc_MinFloat( pAn->F, FLT_MAX/SCL_NUM );
1358
*/    
1359 1360 1361 1362
    assert( pDp->D < SCL_INFINITY );
    assert( pDn->D < SCL_INFINITY );
    assert( pAp->D < SCL_INFINITY );
    assert( pAn->D < SCL_INFINITY );
1363

1364 1365 1366 1367
    assert( pDp->F < FLT_MAX );
    assert( pDn->F < FLT_MAX );
    assert( pAp->F < FLT_MAX );
    assert( pAn->F < FLT_MAX );
1368 1369

/*
Alan Mishchenko committed
1370
    if ( p->Iter && (pDp->D > Required[0] || pDn->D > Required[1]) )
1371 1372
    {
        printf( "%5d : ", iObj );
1373 1374
        printf( "Dp = %6.2f  ", Scl_Int2Flt(pDp->D) );
        printf( "Dn = %6.2f  ", Scl_Int2Flt(pDn->D) );
1375
        printf( "  " );
1376 1377
        printf( "Ap = %6.2f  ", Scl_Int2Flt(pAp->D) );
        printf( "An = %6.2f  ", Scl_Int2Flt(pAn->D) );
1378
        printf( "  " );
1379 1380
        printf( "Rp = %6.2f  ", Scl_Int2Flt(Required[0]) );
        printf( "Rn = %6.2f  ", Scl_Int2Flt(Required[1]) );
1381 1382 1383
        printf( "\n" );
    }
*/
1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406
}
void Nf_ManComputeMapping( Nf_Man_t * p )
{
    Gia_Obj_t * pObj; int i;
    Gia_ManForEachAnd( p->pGia, pObj, i )
        if ( Gia_ObjIsBuf(pObj) )
            Nf_ObjPrepareBuf( p, pObj );
        else
            Nf_ManCutMatch( p, i );
}


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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418
static inline Nf_Mat_t * Nf_ObjMatchBest( Nf_Man_t * p, int i, int c )             
{
    Nf_Mat_t * pD = Nf_ObjMatchD(p, i, c);
    Nf_Mat_t * pA = Nf_ObjMatchA(p, i, c);
    assert( pD->fBest != pA->fBest );
    //assert( Nf_ObjMapRefNum(p, i, c) > 0 );
    if ( pA->fBest )
        return pA;
    if ( pD->fBest )
        return pD;
    return NULL;
}
Alan Mishchenko committed
1419
void Nf_ManSetOutputRequireds( Nf_Man_t * p, int fPropCompl )
1420 1421
{
    Gia_Obj_t * pObj;
1422
    int Required = 0, MapDelayOld = p->pPars->MapDelay;
1423
    int fUseConMan = Scl_ConIsRunning() && Scl_ConHasOutReqs();
Alan Mishchenko committed
1424
    int i, iObj, fCompl, nLits = 2*Gia_ManObjNum(p->pGia);
1425
    Vec_IntFill( &p->vRequired, nLits, SCL_INFINITY );
1426
    // compute delay
1427
    p->pPars->MapDelay = 0;
1428 1429 1430
    Gia_ManForEachCo( p->pGia, pObj, i )
    {
        Required = Nf_ObjMatchD( p, Gia_ObjFaninId0p(p->pGia, pObj), Gia_ObjFaninC0(pObj) )->D;
1431
        p->pPars->MapDelay = Abc_MaxInt( p->pPars->MapDelay, Required );
1432
    }
1433
    if ( p->Iter && MapDelayOld < p->pPars->MapDelay && p->pGia->vOutReqs == NULL )
1434
        printf( "******** Critical delay violation %.2f -> %.2f ********\n", Scl_Int2Flt(MapDelayOld), Scl_Int2Flt(p->pPars->MapDelay) ); 
1435
    p->pPars->MapDelay = Abc_MaxInt( p->pPars->MapDelay, MapDelayOld );
1436
    // check delay target
1437 1438 1439
    if ( p->pPars->MapDelayTarget == 0 && p->pPars->nRelaxRatio )
        p->pPars->MapDelayTarget = p->pPars->MapDelay * (100 + p->pPars->nRelaxRatio) / 100;
    if ( p->pPars->MapDelayTarget > 0 )
1440
    {
1441 1442
        if ( p->pPars->MapDelay < p->pPars->MapDelayTarget )
            p->pPars->MapDelay = p->pPars->MapDelayTarget;
1443
        else if ( p->pPars->nRelaxRatio == 0 )
1444
            Abc_Print( 0, "Relaxing user-specified delay target from %.2f to %.2f.\n", Scl_Int2Flt(p->pPars->MapDelayTarget), Scl_Int2Flt(p->pPars->MapDelay) );
1445
    }
1446
    //assert( p->pPars->MapDelayTarget == 0 );
1447 1448 1449
    // set required times
    Gia_ManForEachCo( p->pGia, pObj, i )
    {
Alan Mishchenko committed
1450 1451 1452
        iObj     = Gia_ObjFaninId0p(p->pGia, pObj);
        fCompl   = Gia_ObjFaninC0(pObj);
        Required = Nf_ObjMatchD(p, iObj, fCompl)->D;
1453
        Required = p->pPars->fDoAverage ? Required * (100 + p->pPars->nRelaxRatio) / 100 : p->pPars->MapDelay;
1454
        // if external required time can be achieved, use it
1455 1456 1457 1458 1459
        if ( fUseConMan )
        {
            if ( Scl_ConGetOutReq(i) > 0 && Required <= Scl_ConGetOutReq(i) )
                Required = Scl_ConGetOutReq(i);
        }
1460
        else if ( p->pGia->vOutReqs )
1461
        {
1462
            int NewRequired = Scl_Flt2Int(Vec_FltEntry(p->pGia->vOutReqs, i));
1463
            if ( NewRequired > 0 && Required <= NewRequired )
1464
                Required = Abc_MinInt( 2*Required, NewRequired );
1465
        }
1466 1467 1468 1469
        // if external required cannot be achieved, set the earliest possible arrival time
//        else if ( p->pGia->vOutReqs && Vec_FltEntry(p->pGia->vOutReqs, i) > 0 && Required > Vec_FltEntry(p->pGia->vOutReqs, i) )
//            ptTime->Rise = ptTime->Fall = ptTime->Worst = Required;
        // otherwise, set the global required time
Alan Mishchenko committed
1470 1471
        Nf_ObjUpdateRequired( p, iObj, fCompl, Required );
        if ( fPropCompl && iObj > 0 && Nf_ObjMatchBest(p, iObj, fCompl)->fCompl )
1472
            Nf_ObjUpdateRequired( p, iObj, !fCompl, Required - p->InvDelayI );
1473 1474 1475
        //Nf_ObjMapRefInc( p, Gia_ObjFaninId0p(p->pGia, pObj), Gia_ObjFaninC0(pObj));
    }
}
1476
void Nf_ManSetMapRefsGate( Nf_Man_t * p, int iObj, int Required, Nf_Mat_t * pM )
1477 1478
{
    int k, iVar, fCompl;
1479
    Mio_Cell2_t * pCell = Nf_ManCell( p, pM->Gate );
1480
    int * pCut = Nf_CutFromHandle( Nf_ObjCutSet(p, iObj), pM->CutH );
1481
    Nf_CutForEachVarCompl( pCut, pM->Cfg, iVar, fCompl, k )
1482 1483
    {
        Nf_ObjMapRefInc( p, iVar, fCompl );
1484
        Nf_ObjUpdateRequired( p, iVar, fCompl, Required - pCell->iDelays[k] );
1485 1486 1487
    }
    assert( Nf_CutSize(pCut) == (int)pCell->nFanins );
    // update global stats
1488
    p->pPars->MapAreaF += pCell->AreaF;
1489 1490 1491 1492 1493 1494
    p->pPars->Edge += Nf_CutSize(pCut);
    p->pPars->Area++;
    // update status of the gate
    assert( pM->fBest == 0 );
    pM->fBest = 1;
}
Alan Mishchenko committed
1495
void Nf_ManPrintMatches( Nf_Man_t * p )
1496
{
Alan Mishchenko committed
1497
    Gia_Obj_t * pObj; int i;
1498 1499 1500 1501 1502 1503 1504 1505
    Gia_ManForEachAnd( p->pGia, pObj, i )
    {
        Nf_Mat_t * pDp = Nf_ObjMatchD( p, i, 0 );
        Nf_Mat_t * pAp = Nf_ObjMatchA( p, i, 0 );
        Nf_Mat_t * pDn = Nf_ObjMatchD( p, i, 1 );
        Nf_Mat_t * pAn = Nf_ObjMatchA( p, i, 1 );

        printf( "%5d : ", i );
1506 1507
        printf( "Dp = %6.2f  ", Scl_Int2Flt(pDp->D) );
        printf( "Dn = %6.2f  ", Scl_Int2Flt(pDn->D) );
1508
        printf( "  " );
1509 1510
        printf( "Ap = %6.2f  ", Scl_Int2Flt(pAp->D) );
        printf( "An = %6.2f  ", Scl_Int2Flt(pAn->D) );
1511 1512 1513 1514 1515 1516 1517 1518
        printf( "  " );
        printf( "Dp = %8s ", Nf_ManCell(p, pDp->Gate)->pName );
        printf( "Dn = %8s ", Nf_ManCell(p, pDn->Gate)->pName );
        printf( "Ap = %8s ", Nf_ManCell(p, pAp->Gate)->pName );
        printf( "An = %8s ", Nf_ManCell(p, pAn->Gate)->pName );
        printf( "\n" );

    }
Alan Mishchenko committed
1519 1520 1521
}
int Nf_ManSetMapRefs( Nf_Man_t * p )
{
1522
    float Coef = 1.0 / (1.0 + (p->Iter + 1) * (p->Iter + 1));
Alan Mishchenko committed
1523 1524 1525 1526 1527 1528 1529
    float * pFlowRefs = Vec_FltArray( &p->vFlowRefs );
    int * pMapRefs = Vec_IntArray( &p->vMapRefs );
    int nLits = 2*Gia_ManObjNum(p->pGia);
    int i, c, Id, nRefs[2];
    Gia_Obj_t * pObj;
    Nf_Mat_t * pD, * pA, * pM;
    Nf_Mat_t * pDs[2], * pAs[2], * pMs[2];
1530
    int Required = 0, Requireds[2];
Alan Mishchenko committed
1531 1532 1533 1534
    assert( !p->fUseEla );
//    if ( p->Iter == 0 )
//        Nf_ManPrintMatches( p );
    Nf_ManSetOutputRequireds( p, 0 );
1535
    // set output references
1536
    memset( pMapRefs, 0, sizeof(int) * nLits );
1537 1538
    Gia_ManForEachCo( p->pGia, pObj, i )
        Nf_ObjMapRefInc( p, Gia_ObjFaninId0p(p->pGia, pObj), Gia_ObjFaninC0(pObj));
1539

1540 1541
    // compute area and edges
    p->nInvs = 0;
1542
    p->pPars->MapAreaF = 0; 
1543 1544 1545 1546 1547 1548 1549 1550
    p->pPars->Area = p->pPars->Edge = 0;
    Gia_ManForEachAndReverse( p->pGia, pObj, i )
    {
        if ( Gia_ObjIsBuf(pObj) )
        {
            if ( Nf_ObjMapRefNum(p, i, 1) )
            {
                Nf_ObjMapRefInc( p, i, 0 );
1551
                Nf_ObjUpdateRequired( p, i, 0, Nf_ObjRequired(p, i, 1) - p->InvDelayI );
1552
                p->pPars->MapAreaF += p->InvAreaF;
1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573
                p->pPars->Edge++;
                p->pPars->Area++;
                p->nInvs++;
            }
            Nf_ObjUpdateRequired( p, Gia_ObjFaninId0(pObj, i), Gia_ObjFaninC0(pObj), Nf_ObjRequired(p, i, 0) );
            Nf_ObjMapRefInc( p, Gia_ObjFaninId0(pObj, i), Gia_ObjFaninC0(pObj));
            continue;
        }
        // skip if this node is not used
        for ( c = 0; c < 2; c++ )
            nRefs[c] = Nf_ObjMapRefNum(p, i, c);
        if ( !nRefs[0] && !nRefs[1] )
            continue;

        // consider two cases
        if ( nRefs[0] && nRefs[1] )
        {
            // find best matches for both phases
            for ( c = 0; c < 2; c++ )
            {
                Requireds[c] = Nf_ObjRequired( p, i, c );
1574
                //assert( Requireds[c] < SCL_INFINITY );
1575 1576
                pDs[c] = Nf_ObjMatchD( p, i, c );
                pAs[c] = Nf_ObjMatchA( p, i, c );
1577
                pMs[c] = (pAs[c]->D <= Requireds[c]) ? pAs[c] : pDs[c];
1578 1579 1580 1581
            }
            // swap complemented matches
            if ( pMs[0]->fCompl && pMs[1]->fCompl )
            {
1582 1583 1584 1585 1586 1587
//                pMs[0]->fCompl = pMs[1]->fCompl = 0;
//                ABC_SWAP( Nf_Mat_t *, pMs[0], pMs[1] );
                // find best matches for both phases
                pMs[0] = Nf_ObjMatchD( p, i, 0 );
                pMs[1] = Nf_ObjMatchD( p, i, 1 );
                assert( !pMs[0]->fCompl || !pMs[1]->fCompl );
1588 1589
            }
            // check if intervers are involved
Alan Mishchenko committed
1590
            if ( !pMs[0]->fCompl && !pMs[1]->fCompl ) // no inverters
1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601
            {
                for ( c = 0; c < 2; c++ )
                    Nf_ManSetMapRefsGate( p, i, Requireds[c], pMs[c] );
            }
            else 
            {
                // one interver
                assert( !pMs[0]->fCompl || !pMs[1]->fCompl );
                c = pMs[1]->fCompl;
                assert( pMs[c]->fCompl && !pMs[!c]->fCompl );
                //printf( "Using inverter at node %d in phase %d\n", i, c );
1602
                // update this phase
1603 1604 1605 1606 1607
                pM = pMs[c];
                pM->fBest = 1;
                Required = Requireds[c];
                // update opposite phase
                Nf_ObjMapRefInc( p, i, !c );
1608
                Nf_ObjUpdateRequired( p, i, !c, Required - p->InvDelayI );
1609
                // select opposite phase
1610
                Required = Nf_ObjRequired( p, i, !c );
1611
                //assert( Required < SCL_INFINITY );
1612 1613
                pD = Nf_ObjMatchD( p, i, !c );
                pA = Nf_ObjMatchA( p, i, !c );
1614
                pM = (pA->D <= Required) ? pA : pD;
1615
                assert( !pM->fCompl );
Alan Mishchenko committed
1616 1617
                // create gate
                Nf_ManSetMapRefsGate( p, i, Required, pM );
1618
                // account for the inverter
1619
                p->pPars->MapAreaF += p->InvAreaF;
1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630
                p->pPars->Edge++;
                p->pPars->Area++;
                p->nInvs++;
            }
        }
        else
        {
            c = (int)(nRefs[1] > 0);
            assert( nRefs[c] && !nRefs[!c] );
            // consider this phase
            Required = Nf_ObjRequired( p, i, c );
1631
            //assert( Required < SCL_INFINITY );
1632 1633
            pD = Nf_ObjMatchD( p, i, c );
            pA = Nf_ObjMatchA( p, i, c );
1634
            pM = (pA->D <= Required) ? pA : pD;
1635 1636 1637 1638 1639 1640 1641
            if ( pM->fCompl ) // use inverter
            {
                p->nInvs++;
                //printf( "Using inverter at node %d in phase %d\n", i, c );
                pM->fBest = 1;
                // update opposite phase
                Nf_ObjMapRefInc( p, i, !c );
1642
                Nf_ObjUpdateRequired( p, i, !c, Required - p->InvDelayI );
1643
                // select opposite phase
1644
                Required = Nf_ObjRequired( p, i, !c );
1645
                //assert( Required < SCL_INFINITY );
1646 1647
                pD = Nf_ObjMatchD( p, i, !c );
                pA = Nf_ObjMatchA( p, i, !c );
1648
                pM = (pA->D <= Required) ? pA : pD;
1649 1650
                assert( !pM->fCompl );
                // account for the inverter
1651
                p->pPars->MapAreaF += p->InvAreaF;
1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666
                p->pPars->Edge++;
                p->pPars->Area++;
            }
            // create gate
            Nf_ManSetMapRefsGate( p, i, Required, pM );
        }
        // the result of this:
        // - only one phase can be implemented as inverter of the other phase
        // - required times are propagated correctly
        // - references are set correctly
    }
    Gia_ManForEachCiId( p->pGia, Id, i )
        if ( Nf_ObjMapRefNum(p, Id, 1) )
        {
            Nf_ObjMapRefInc( p, Id, 0 );
1667
            Nf_ObjUpdateRequired( p, Id, 0, Required - p->InvDelayI );
1668
            p->pPars->MapAreaF += p->InvAreaF;
1669 1670 1671 1672 1673 1674 1675 1676 1677 1678
            p->pPars->Edge++;
            p->pPars->Area++;
            p->nInvs++;
        }
    // blend references
    for ( i = 0; i < nLits; i++ )
        pFlowRefs[i] = Abc_MaxFloat(1.0, Coef * pFlowRefs[i] + (1.0 - Coef) * Abc_MaxFloat(1, pMapRefs[i]));
//        pFlowRefs[i] = 0.2 * pFlowRefs[i] + 0.8 * Abc_MaxFloat(1, pMapRefs[i]);
    return p->pPars->Area;
}
1679 1680


Alan Mishchenko committed
1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702

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

  Synopsis    [Area recovery.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
word Nf_MatchDeref_rec( Nf_Man_t * p, int i, int c, Nf_Mat_t * pM )
{
    word Area = 0;
    int k, iVar, fCompl, * pCut;
    assert( pM->fBest );
    if ( pM->fCompl )
    {
        assert( Nf_ObjMapRefNum(p, i, !c) > 0 );
        if ( !Nf_ObjMapRefDec(p, i, !c) )
            Area += Nf_MatchDeref_rec( p, i, !c, Nf_ObjMatchD(p, i, !c) );
1703
        return Area + p->InvAreaW;
Alan Mishchenko committed
1704 1705 1706 1707
    }
    if ( Nf_ObjCutSetId(p, i) == 0 )
        return 0;
    pCut = Nf_CutFromHandle( Nf_ObjCutSet(p, i), pM->CutH );
1708
    Nf_CutForEachVarCompl( pCut, pM->Cfg, iVar, fCompl, k )
Alan Mishchenko committed
1709 1710 1711 1712 1713
    {
        assert( Nf_ObjMapRefNum(p, iVar, fCompl) > 0 );
        if ( !Nf_ObjMapRefDec(p, iVar, fCompl) )
            Area += Nf_MatchDeref_rec( p, iVar, fCompl, Nf_ObjMatchD(p, iVar, fCompl) );
    }
1714
    return Area + Nf_ManCell(p, pM->Gate)->AreaW;
Alan Mishchenko committed
1715
}
1716
word Nf_MatchRef_rec( Nf_Man_t * p, int i, int c, Nf_Mat_t * pM, int Required, Vec_Int_t * vBackup )
Alan Mishchenko committed
1717
{
1718 1719
    word Area = 0;
    int ReqFanin;
Alan Mishchenko committed
1720 1721 1722 1723 1724
    int k, iVar, fCompl, * pCut;
    assert( pM->fBest );
    assert( pM->D <= Required );
    if ( pM->fCompl )
    {
1725
        ReqFanin = Required - p->InvDelayI;
Alan Mishchenko committed
1726 1727 1728 1729 1730
        if ( vBackup )
            Vec_IntPush( vBackup, Abc_Var2Lit(i, !c) );
        assert( Nf_ObjMapRefNum(p, i, !c) >= 0 );
        if ( !Nf_ObjMapRefInc(p, i, !c) )
            Area += Nf_MatchRef_rec( p, i, !c, Nf_ObjMatchD(p, i, !c), ReqFanin, vBackup );
1731
        return Area + p->InvAreaW;
Alan Mishchenko committed
1732 1733 1734 1735
    }
    if ( Nf_ObjCutSetId(p, i) == 0 )
        return 0;
    pCut = Nf_CutFromHandle( Nf_ObjCutSet(p, i), pM->CutH );
1736
    Nf_CutForEachVarCompl( pCut, pM->Cfg, iVar, fCompl, k )
Alan Mishchenko committed
1737
    {
1738
        ReqFanin = Required - Nf_ManCell(p, pM->Gate)->iDelays[k];
Alan Mishchenko committed
1739 1740 1741 1742 1743 1744
        if ( vBackup )
            Vec_IntPush( vBackup, Abc_Var2Lit(iVar, fCompl) );
        assert( Nf_ObjMapRefNum(p, iVar, fCompl) >= 0 );
        if ( !Nf_ObjMapRefInc(p, iVar, fCompl) )
            Area += Nf_MatchRef_rec( p, iVar, fCompl, Nf_ObjMatchD(p, iVar, fCompl), ReqFanin, vBackup );
    }
1745
    return Area + Nf_ManCell(p, pM->Gate)->AreaW;
Alan Mishchenko committed
1746
}
1747
word Nf_MatchRefArea( Nf_Man_t * p, int i, int c, Nf_Mat_t * pM, int Required )
Alan Mishchenko committed
1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758
{
    word Area;  int iLit, k; 
    Vec_IntClear( &p->vBackup );
    Area = Nf_MatchRef_rec( p, i, c, pM, Required, &p->vBackup );
    Vec_IntForEachEntry( &p->vBackup, iLit, k )
    {
        assert( Nf_ObjMapRefNum(p, Abc_Lit2Var(iLit), Abc_LitIsCompl(iLit)) > 0 );
        Nf_ObjMapRefDec( p, Abc_Lit2Var(iLit), Abc_LitIsCompl(iLit) );
    }
    return Area;
}
1759
void Nf_ManElaBestMatchOne( Nf_Man_t * p, int iObj, int c, int * pCut, int * pCutSet, Nf_Mat_t * pRes, int Required )
Alan Mishchenko committed
1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773
{
    Nf_Mat_t Mb,*pMb = &Mb, * pMd;
    int * pFans      = Nf_CutLeaves(pCut);
    int nFans        = Nf_CutSize(pCut);
    int iFuncLit     = Nf_CutFunc(pCut);
    int fComplExt    = Abc_LitIsCompl(iFuncLit);
    Vec_Int_t * vArr = Vec_WecEntry( p->vTt2Match, Abc_Lit2Var(iFuncLit) );
    int i, k, Info, Offset, iFanin, fComplF;
    // assign fanins matches
    Nf_Obj_t * pBestF[NF_LEAF_MAX];
    for ( i = 0; i < nFans; i++ )
        pBestF[i] = Nf_ManObj( p, pFans[i] );
    // consider matches of this function
    memset( pMb, 0, sizeof(Nf_Mat_t) );
1774
    pMb->D = SCL_INFINITY; pMb->F = FLT_MAX;
Alan Mishchenko committed
1775 1776 1777 1778 1779 1780 1781
    // special cases
    if ( nFans == 0 )
    {
        int Const = (iFuncLit == 1);
        //printf( "Node %d(%d) is const\n", iObj, c );
        assert( iFuncLit == 0 || iFuncLit == 1 );
        pMb->D = 0;
1782
        pMb->F = p->pCells[c ^ Const].AreaF;
Alan Mishchenko committed
1783 1784
        pMb->CutH = Nf_CutHandle(pCutSet, pCut);
        pMb->Gate = c ^ Const;
1785 1786
//        pMb->Conf = 0;
        pMb->Cfg = Nf_Int2Cfg(0);
Alan Mishchenko committed
1787 1788
        pMb->fBest = 1;
        // compare
1789
        if ( pRes->F > pMb->F + NF_EPSILON || (pRes->F > pMb->F - NF_EPSILON && pRes->D > pMb->D) )
Alan Mishchenko committed
1790 1791 1792 1793 1794 1795
            *pRes = *pMb;
        return;
    }
    // consider matches of this function
    Vec_IntForEachEntryDouble( vArr, Info, Offset, i )
    {
1796
        Nf_Cfg_t Cfg   = Nf_Int2Cfg(Offset);
Alan Mishchenko committed
1797
        Mio_Cell2_t*pC = Nf_ManCell( p, Info );
1798
        int fCompl     = Cfg.fCompl ^ fComplExt;
1799
        int Delay      = 0;
Alan Mishchenko committed
1800 1801 1802
        assert( nFans == (int)pC->nFanins );
        if ( fCompl != c )
            continue;
1803
        Nf_CfgForEachVarCompl( Cfg, nFans, iFanin, fComplF, k )
Alan Mishchenko committed
1804
        {
1805
            pMd     = &pBestF[iFanin]->M[fComplF][0];
Alan Mishchenko committed
1806
            assert( pMd->fBest );
1807
            Delay = Abc_MaxInt( Delay, pMd->D + pC->iDelays[k] );
Alan Mishchenko committed
1808 1809 1810 1811 1812
            if ( Delay > Required )
                break;
        }
        if ( k < nFans )
            continue;
1813
        // create match
Alan Mishchenko committed
1814
        pMb->D = Delay;
1815
        pMb->F = FLT_MAX;
Alan Mishchenko committed
1816 1817 1818 1819
        pMb->fBest = 1;
        pMb->fCompl = 0;
        pMb->CutH = Nf_CutHandle(pCutSet, pCut);
        pMb->Gate = pC->Id;
1820 1821
        pMb->Cfg = Cfg;
        pMb->Cfg.fCompl = 0;
Alan Mishchenko committed
1822
        // compute area
1823
        pMb->F = Scl_Int2Flt((int)Nf_MatchRefArea(p, iObj, c, pMb, Required));
Alan Mishchenko committed
1824
        // compare
1825
        if ( pRes->F > pMb->F + NF_EPSILON || (pRes->F > pMb->F - NF_EPSILON && pRes->D > pMb->D) )
Alan Mishchenko committed
1826 1827 1828
            *pRes = *pMb;
    }
}
1829
void Nf_ManElaBestMatch( Nf_Man_t * p, int iObj, int c, Nf_Mat_t * pRes, int Required )
Alan Mishchenko committed
1830 1831 1832
{
    int k, * pCut, * pCutSet = Nf_ObjCutSet( p, iObj );
    memset( pRes, 0, sizeof(Nf_Mat_t) );
1833
    pRes->D = SCL_INFINITY; pRes->F = FLT_MAX;
Alan Mishchenko committed
1834 1835 1836 1837 1838 1839 1840
    Nf_SetForEachCut( pCutSet, pCut, k )
    {
        if ( Abc_Lit2Var(Nf_CutFunc(pCut)) >= Vec_WecSize(p->vTt2Match) )
            continue;
        Nf_ManElaBestMatchOne( p, iObj, c, pCut, pCutSet, pRes, Required );
    }
}
1841
int Nf_ManComputeArrival( Nf_Man_t * p, Nf_Mat_t * pM, int * pCutSet )
Alan Mishchenko committed
1842
{
1843
    int Delay = 0; 
Alan Mishchenko committed
1844 1845 1846 1847 1848
    Nf_Mat_t * pMfan;
    int iVar, fCompl, k;
    Mio_Cell2_t * pCell = Nf_ManCell( p, pM->Gate );
    int * pCut = Nf_CutFromHandle( pCutSet, pM->CutH );
    assert( !pM->fCompl );
1849
    Nf_CutForEachVarCompl( pCut, pM->Cfg, iVar, fCompl, k )
Alan Mishchenko committed
1850 1851
    {
        pMfan = Nf_ObjMatchBest( p, iVar, fCompl );
1852
        Delay = Abc_MaxInt( Delay, pMfan->D + pCell->iDelays[k] );
Alan Mishchenko committed
1853
    }
1854
    //if ( pM->fCompl ) Delay += p->InvDelayI;
Alan Mishchenko committed
1855 1856 1857 1858
    return Delay;
}
void Nf_ManResetMatches( Nf_Man_t * p, int Round )
{
Alan Mishchenko committed
1859 1860
    Gia_Obj_t * pObj;
    Nf_Mat_t * pDc, * pAc, * pMfan, * pM[2]; 
1861
    int i, c, Arrival;
Alan Mishchenko committed
1862
    // go through matches in the topo order
Alan Mishchenko committed
1863
    Gia_ManForEachAnd( p->pGia, pObj, i )
Alan Mishchenko committed
1864
    {
Alan Mishchenko committed
1865 1866 1867 1868 1869 1870 1871
        if ( Gia_ObjIsBuf(pObj) )
        {
            pMfan = Nf_ObjMatchBest( p, Gia_ObjFaninId0(pObj, i), Gia_ObjFaninC0(pObj) );
            for ( c = 0; c < 2; c++ )
            {
                pDc = Nf_ObjMatchD( p, i, c );
                pAc = Nf_ObjMatchA( p, i, c );
1872
                pDc->F = pAc->F = 0;
1873
                pDc->D = pMfan->D + (c ? p->InvDelayI : 0); 
Alan Mishchenko committed
1874 1875 1876 1877 1878 1879
                assert( pDc->fBest );
                assert( !pAc->fBest );
                assert( c==0 || pDc->fCompl );
            }
            continue;
        }
Alan Mishchenko committed
1880 1881 1882 1883 1884
        // select the best match for each phase
        for ( c = 0; c < 2; c++ )
        {
            pDc = Nf_ObjMatchD( p, i, c );
            pAc = Nf_ObjMatchA( p, i, c );
1885
            pDc->F = pAc->F = 0;
Alan Mishchenko committed
1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896
            if ( Nf_ObjMapRefNum(p, i, c) )
            {
                assert( pDc->fBest != pAc->fBest );
                if ( pAc->fBest )
                    ABC_SWAP( Nf_Mat_t, *pDc, *pAc );
                assert( pDc->fBest );
                assert( !pAc->fBest );
            }
            else
            {
                assert( Round > 0 || (!pDc->fBest && !pAc->fBest) );
1897 1898
//                if ( (p->pPars->fAreaOnly || (Round & 1)) && !pAc->fCompl )
                if ( (Round & 1) && !pAc->fCompl )
Alan Mishchenko committed
1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910
                    ABC_SWAP( Nf_Mat_t, *pDc, *pAc );
                pDc->fBest = 1;
                pAc->fBest = 0;
            }
        }
        // consider best matches of both phases
        pM[0] = Nf_ObjMatchD( p, i, 0 );
        pM[1] = Nf_ObjMatchD( p, i, 1 );
        assert( pM[0]->fBest && pM[1]->fBest );
        // swap complemented matches
        if ( pM[0]->fCompl && pM[1]->fCompl )
        {
1911 1912 1913
//            pM[0]->fCompl = pM[1]->fCompl = 0;
//            ABC_SWAP( Nf_Mat_t *, pM[0], pM[1] );
            assert( 0 );
Alan Mishchenko committed
1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937
        }
        if ( !pM[0]->fCompl && !pM[1]->fCompl )
        {
            for ( c = 0; c < 2; c++ )
            {
                Arrival = Nf_ManComputeArrival( p, pM[c], Nf_ObjCutSet(p, i) );
                //if ( Nf_ObjMapRefNum(p, i, c) )
                //    assert( Round || Arrival <= pM[c]->D );
                pM[c]->D = Arrival;
            }
        }
        else 
        {
            // consider non-complemented match
            c = !pM[1]->fCompl;
            assert( !pM[c]->fCompl );
            assert( pM[!c]->fCompl );
            Arrival = Nf_ManComputeArrival( p, pM[c], Nf_ObjCutSet(p, i) );
            //if ( Nf_ObjMapRefNum(p, i, c) )
            //    assert( Round || Arrival <= pM[c]->D );
            pM[c]->D = Arrival;
            // consider complemented match
            Arrival = pM[!c]->D;
            *pM[!c] = *pM[c];
1938
            pM[!c]->D += p->InvDelayI;
Alan Mishchenko committed
1939 1940 1941 1942 1943 1944 1945 1946
            pM[!c]->fCompl = 1;
            //if ( Nf_ObjMapRefNum(p, i, !c) )
            //    assert( Round || pM[!c]->D <= Arrival );
        }
    }
}
void Nf_ManComputeMappingEla( Nf_Man_t * p )
{
Alan Mishchenko committed
1947 1948
    int fVerbose = 0;
    Gia_Obj_t * pObj;
Alan Mishchenko committed
1949 1950
    Mio_Cell2_t * pCell;
    Nf_Mat_t Mb, * pMb = &Mb, * pM;
1951
    word AreaBef, AreaAft, Gain = 0;
Alan Mishchenko committed
1952
    int i, c, iVar, Id, fCompl, k, * pCut;
1953
    int Required;
Alan Mishchenko committed
1954
    Nf_ManSetOutputRequireds( p, 1 );
Alan Mishchenko committed
1955
    Nf_ManResetMatches( p, p->Iter - p->pPars->nRounds );
Alan Mishchenko committed
1956
    Gia_ManForEachAndReverse( p->pGia, pObj, i )
Alan Mishchenko committed
1957
    {
Alan Mishchenko committed
1958
        if ( Gia_ObjIsBuf(pObj) )
Alan Mishchenko committed
1959
        {
Alan Mishchenko committed
1960
            if ( Nf_ObjMapRefNum(p, i, 1) )
1961
                Nf_ObjUpdateRequired( p, i, 0, Nf_ObjRequired(p, i, 1) - p->InvDelayI );
Alan Mishchenko committed
1962 1963
            Nf_ObjUpdateRequired( p, Gia_ObjFaninId0(pObj, i), Gia_ObjFaninC0(pObj), Nf_ObjRequired(p, i, 0) );
            continue;
Alan Mishchenko committed
1964
        }
Alan Mishchenko committed
1965 1966
        for ( c = 0; c < 2; c++ )
        if ( Nf_ObjMapRefNum(p, i, c) )
Alan Mishchenko committed
1967
        {
Alan Mishchenko committed
1968 1969 1970
            pM = Nf_ObjMatchBest( p, i, c );
            Required = Nf_ObjRequired( p, i, c );
            assert( pM->D <= Required );
Alan Mishchenko committed
1971
            if ( pM->fCompl )
Alan Mishchenko committed
1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985
                continue;
            // search for a better match
            assert( !pM->fCompl );
            AreaBef = Nf_MatchDeref_rec( p, i, c, pM );
            assert( pM->fBest );
            Nf_ManElaBestMatch( p, i, c, pMb, Required );
            AreaAft = Nf_MatchRef_rec( p, i, c, pMb, Required, NULL );
            Gain += AreaBef - AreaAft;
            // print area recover progress
            if ( fVerbose && Nf_ManCell(p, pM->Gate)->pName != Nf_ManCell(p, pMb->Gate)->pName )
            {
                printf( "%4d (%d)  ", i, c );
                printf( "%8s ->%8s  ",         Nf_ManCell(p, pM->Gate)->pName, Nf_ManCell(p, pMb->Gate)->pName );
                printf( "%d -> %d  ",          Nf_ManCell(p, pM->Gate)->nFanins, Nf_ManCell(p, pMb->Gate)->nFanins );
1986 1987
                printf( "D: %7.2f -> %7.2f  ", Scl_Int2Flt(pM->D), Scl_Int2Flt(pMb->D) );
                printf( "R: %7.2f  ",          Required == SCL_INFINITY ? 9999.99 : Scl_Int2Flt(Required) );
1988 1989
                printf( "A: %7.2f -> %7.2f  ", Scl_Int2Flt((int)AreaBef), Scl_Int2Flt((int)AreaAft) );
                printf( "G: %7.2f (%7.2f) ",   Scl_Int2Flt((int)AreaBef - (int)AreaAft), Scl_Int2Flt((int)Gain) );
Alan Mishchenko committed
1990 1991 1992 1993 1994
                printf( "\n" );
            }
            // set best match
            assert( pMb->fBest );
            assert( pMb->D <= Required );
1995
            //assert( Scl_Flt2Int(pMb->F) == (int)AreaAft );
1996
            //assert( AreaBef >= AreaAft );
Alan Mishchenko committed
1997 1998 1999 2000
            *pM = *pMb;
            // update timing
            pCell = Nf_ManCell( p, pMb->Gate );
            pCut = Nf_CutFromHandle( Nf_ObjCutSet(p, i), pMb->CutH );
2001
            Nf_CutForEachVarCompl( pCut, pMb->Cfg, iVar, fCompl, k )
Alan Mishchenko committed
2002
            {
Alan Mishchenko committed
2003
                pM = Nf_ObjMatchBest( p, iVar, fCompl );
2004 2005
                assert( pM->D <= Required - pCell->iDelays[k] );
                Nf_ObjUpdateRequired( p, iVar, fCompl, Required - pCell->iDelays[k] );
Alan Mishchenko committed
2006 2007 2008
                if ( pM->fCompl )
                {
                    pM = Nf_ObjMatchBest( p, iVar, !fCompl );
2009 2010
                    assert( pM->D <= Required - pCell->iDelays[k] - p->InvDelayI );
                    Nf_ObjUpdateRequired( p, iVar, !fCompl, Required - pCell->iDelays[k] - p->InvDelayI );
Alan Mishchenko committed
2011
                }
Alan Mishchenko committed
2012 2013 2014 2015 2016 2017 2018
            }
        }
    }
    Gia_ManForEachCiId( p->pGia, Id, i )
        if ( Nf_ObjMapRefNum(p, Id, 1) )
        {
            Required = Nf_ObjRequired( p, i, 1 );
2019
            Nf_ObjUpdateRequired( p, Id, 0, Required - p->InvDelayI );
Alan Mishchenko committed
2020 2021
        }
}
2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042
void Nf_ManFixPoDrivers( Nf_Man_t * p )
{
    Gia_Obj_t * pObj;
    Nf_Mat_t * pM, * pMc;
    int i, iDriver, Count = 0;
    Gia_ManForEachCo( p->pGia, pObj, i )
    {
        iDriver = Gia_ObjFaninId0p(p->pGia, pObj);
        if ( !Gia_ObjIsAnd(Gia_ManObj(p->pGia, iDriver)) )
            continue;
        // skip unless both are used
        if ( !Nf_ObjMapRefNum(p, iDriver, 0) || !Nf_ObjMapRefNum(p, iDriver, 1) )
            continue;
        pM  = Nf_ObjMatchD( p, iDriver,  Gia_ObjFaninC0(pObj) );
        pMc = Nf_ObjMatchD( p, iDriver, !Gia_ObjFaninC0(pObj) );
        // skip unless both are non-complemented
        if ( pM->fCompl || pMc->fCompl )
            continue;
        // skip if arrival time exceeds the required time
        if ( pMc->D + p->InvDelayI > p->pPars->MapDelay )
            continue;
2043 2044 2045
        // update references
        Nf_MatchDeref_rec( p, iDriver, Gia_ObjFaninC0(pObj), pM );
        Nf_ObjMapRefInc( p, iDriver, !Gia_ObjFaninC0(pObj) );
2046 2047 2048 2049
        // add inverter
        *pM = *pMc;
        pM->D += p->InvDelayI;
        pM->fCompl = 1;
2050 2051
        pM->fBest = 1;
        pMc->fBest = 1;
2052 2053 2054 2055
        Count++;
    }
    //printf( "Fixed %d PO drivers.\n", Count );
}
2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067

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

  Synopsis    [Deriving mapping.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
2068 2069 2070 2071
Gia_Man_t * Nf_ManDeriveMapping( Nf_Man_t * p )
{
    Vec_Int_t * vMapping;
    Nf_Mat_t * pM;
2072
    int i, k, c, Id, iVar, fCompl, * pCut;
2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100
    assert( p->pGia->vCellMapping == NULL );
    vMapping = Vec_IntAlloc( 2*Gia_ManObjNum(p->pGia) + (int)p->pPars->Edge + (int)p->pPars->Area * 2 );
    Vec_IntFill( vMapping, 2*Gia_ManObjNum(p->pGia), 0 );
    // create CI inverters
    Gia_ManForEachCiId( p->pGia, Id, i )
    if ( Nf_ObjMapRefNum(p, Id, 1) )
        Vec_IntWriteEntry( vMapping, Abc_Var2Lit(Id, 1), -1 );
    // create internal nodes
    Gia_ManForEachAndId( p->pGia, i )
    {
        Gia_Obj_t * pObj = Gia_ManObj(p->pGia, i);
        if ( Gia_ObjIsBuf(pObj) )
        {
            if ( Nf_ObjMapRefNum(p, i, 1) )
                Vec_IntWriteEntry( vMapping, Abc_Var2Lit(i, 1), -1 );
            Vec_IntWriteEntry( vMapping, Abc_Var2Lit(i, 0), -2 );
            continue;
        }
        for ( c = 0; c < 2; c++ )
        if ( Nf_ObjMapRefNum(p, i, c) )
        {
            pM = Nf_ObjMatchBest( p, i, c );
            // remember inverter
            if ( pM->fCompl )
            {
                Vec_IntWriteEntry( vMapping, Abc_Var2Lit(i, c), -1 );
                continue;
            }
2101
    //        Nf_ManCutMatchPrint( p, i, c, pM );
2102 2103 2104
            pCut = Nf_CutFromHandle( Nf_ObjCutSet(p, i), pM->CutH );
            Vec_IntWriteEntry( vMapping, Abc_Var2Lit(i, c), Vec_IntSize(vMapping) );
            Vec_IntPush( vMapping, Nf_CutSize(pCut) );
2105 2106
            Nf_CutForEachVarCompl( pCut, pM->Cfg, iVar, fCompl, k )
                Vec_IntPush( vMapping, Abc_Var2Lit(iVar, fCompl) );
2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117
            Vec_IntPush( vMapping, pM->Gate );
        }
    }
//    assert( Vec_IntCap(vMapping) == 16 || Vec_IntSize(vMapping) == Vec_IntCap(vMapping) );
    p->pGia->vCellMapping = vMapping;
    return p->pGia;
}
void Nf_ManUpdateStats( Nf_Man_t * p )
{
    Nf_Mat_t * pM;
    Gia_Obj_t * pObj;
2118
    Mio_Cell2_t * pCell;
2119
    int i, c, Id, * pCut;
2120
    p->pPars->MapAreaF = 0; p->nInvs = 0;
2121
    p->pPars->Area = p->pPars->Edge = 0;
Alan Mishchenko committed
2122
    Gia_ManForEachAndReverse( p->pGia, pObj, i )
2123
    {
Alan Mishchenko committed
2124
        if ( Gia_ObjIsBuf(pObj) )
2125
        {
Alan Mishchenko committed
2126 2127
            if ( Nf_ObjMapRefNum(p, i, 1) )
            {
2128
                p->pPars->MapAreaF += p->InvAreaF;
Alan Mishchenko committed
2129 2130 2131 2132
                p->pPars->Edge++;
                p->pPars->Area++;
                p->nInvs++;
            }
2133 2134
            continue;
        }
Alan Mishchenko committed
2135 2136 2137 2138 2139 2140
        for ( c = 0; c < 2; c++ )
        if ( Nf_ObjMapRefNum(p, i, c) )
        {
            pM = Nf_ObjMatchBest( p, i, c );
            if ( pM->fCompl )
            {
2141
                p->pPars->MapAreaF += p->InvAreaF;
Alan Mishchenko committed
2142 2143 2144 2145 2146 2147 2148 2149
                p->pPars->Edge++;
                p->pPars->Area++;
                p->nInvs++;
                continue;
            }
            pCut = Nf_CutFromHandle( Nf_ObjCutSet(p, i), pM->CutH );
            pCell = Nf_ManCell( p, pM->Gate );
            assert( Nf_CutSize(pCut) == (int)pCell->nFanins );
2150
            p->pPars->MapAreaF += pCell->AreaF;
Alan Mishchenko committed
2151 2152 2153 2154
            p->pPars->Edge += Nf_CutSize(pCut);
            p->pPars->Area++;
            //printf( "%5d (%d) : Gate = %7s \n", i, c, pCell->pName );
        }
2155 2156 2157 2158
    }
    Gia_ManForEachCiId( p->pGia, Id, i )
        if ( Nf_ObjMapRefNum(p, Id, 1) )
        {
2159
            p->pPars->MapAreaF += p->InvAreaF;
2160 2161
            p->pPars->Edge++;
            p->pPars->Area++;
Alan Mishchenko committed
2162
            p->nInvs++;
2163 2164 2165 2166 2167
        }
}

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

2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187
  Synopsis    [Extract window.]

  Description []
               
  SideEffects []

  SeeAlso     []

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

/*
    int              nInputs;   // the number of inputs
    int              nObjs;     // number of all objects
    Vec_Int_t *      vRoots;    // output drivers to be mapped (root -> obj lit)
    Vec_Wec_t *      vCuts;     // cuts (cut -> obj lit + fanin lits)
    Vec_Wec_t *      vObjCuts;  // cuts (obj lit -> obj lit + cut lits)
    Vec_Int_t *      vSolCuts;  // current solution (index -> cut)
    Vec_Int_t *      vCutGates; // gates (cut -> gate)
*/

2188
int Nf_ManExtractWindow( void * pMan, Vec_Int_t * vRoots, Vec_Wec_t * vCuts, Vec_Wec_t * vObjCuts, Vec_Int_t * vSolCuts, Vec_Int_t * vCutGates, Vec_Wrd_t * vCutAreas, word * pInvArea, int StartVar, int nVars )
2189 2190 2191
{
    Nf_Man_t * p = (Nf_Man_t *)pMan;
    int nInputs = Gia_ManCiNum(p->pGia);
2192
    int LitShift = 2*nInputs+2;
2193 2194
    Gia_Obj_t * pObj;
    int c, iObj;
2195 2196 2197 2198 2199 2200
    if ( 2*Gia_ManAndNum(p->pGia) + Gia_ManCiNum(p->pGia) > nVars )
    {
        printf( "The number of variables is too large: 2*%d + %d = %d > %d.\n", Gia_ManAndNum(p->pGia), Gia_ManCiNum(p->pGia), 2*Gia_ManAndNum(p->pGia) + Gia_ManCiNum(p->pGia), nVars );
        return 0;
    }
    *pInvArea = p->InvAreaW;
2201 2202 2203
    // save roots
    Vec_IntClear( vRoots );
    Gia_ManForEachCo( p->pGia, pObj, c )
2204 2205 2206 2207
    {
        assert( !Gia_ObjIsCi(Gia_ObjFanin0(pObj)) );
        Vec_IntPush( vRoots, Gia_ObjFaninLit0p(p->pGia, pObj)-LitShift );
    }
2208 2209 2210 2211 2212
    // prepare
    Vec_WecClear( vCuts );
    Vec_WecClear( vObjCuts );
    Vec_IntClear( vSolCuts );
    Vec_IntClear( vCutGates );
2213
    Vec_WrdClear( vCutAreas );
2214 2215 2216
    // collect cuts for each node
    Gia_ManForEachAndId( p->pGia, iObj )
    {
2217
        Vec_Int_t * vObj[2], * vCutOne;
2218 2219 2220
        int iCut, * pCut, * pCutSet;
        int iCutInv[2] = {-1, -1};
        // get matches
2221
        Nf_Mat_t * pM[2] = {NULL, NULL};
2222
        for ( c = 0; c < 2; c++ )
2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234
        {
            if ( Nf_ObjMapRefNum(p, iObj, c) == 0 )
                continue;
            if ( Nf_ObjMatchBest(p, iObj, c)->fCompl )
            {
                assert( iCutInv[c] == -1 );
                iCutInv[c] = Vec_IntSize(vSolCuts);
                Vec_IntPush( vSolCuts, -1 );
                continue;
            }
            pM[c] = Nf_ObjMatchBest(p, iObj, c);
        }
2235
        // start collecting cuts of pos-obj and neg-obj
2236
        assert( Vec_WecSize(vObjCuts) == 2*iObj-LitShift );
2237 2238 2239
        for ( c = 0; c < 2; c++ )
        {
            vObj[c] = Vec_WecPushLevel( vObjCuts );
2240
            Vec_IntPush( vObj[c], Abc_Var2Lit(Abc_Var2Lit(iObj, c)-LitShift, 1) );
2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262
        }
        // enumerate cuts
        pCutSet = Nf_ObjCutSet( p, iObj );
        Nf_SetForEachCut( pCutSet, pCut, iCut )
        {
            assert( !Nf_CutIsTriv(pCut, iObj) );
            assert( Nf_CutSize(pCut) <= p->pPars->nLutSize );
            if ( Abc_Lit2Var(Nf_CutFunc(pCut)) < Vec_WecSize(p->vTt2Match) )
            {
                int * pFans      = Nf_CutLeaves(pCut);
                int nFans        = Nf_CutSize(pCut);
                int iFuncLit     = Nf_CutFunc(pCut);
                int fComplExt    = Abc_LitIsCompl(iFuncLit);
                Vec_Int_t * vArr = Vec_WecEntry( p->vTt2Match, Abc_Lit2Var(iFuncLit) );
                int i, k, c, Info, Offset, iFanin, fComplF, iCutLit;
                Vec_IntForEachEntryDouble( vArr, Info, Offset, i )
                {
                    Nf_Cfg_t Cfg   = Nf_Int2Cfg(Offset);
                    int fCompl     = Cfg.fCompl ^ fComplExt;
                    Mio_Cell2_t*pC = Nf_ManCell( p, Info );
                    assert( nFans == (int)pC->nFanins );
                    Vec_IntPush( vCutGates, Info );
2263
                    Vec_WrdPush( vCutAreas, pC->AreaW );
2264 2265 2266 2267 2268 2269 2270
                    // to make comparison possible
                    Cfg.fCompl = 0;
                    // add solution cut
                    for ( c = 0; c < 2; c++ )
                    {
                        if ( pM[c] == NULL )
                            continue;
2271
                        if ( (int)pM[c]->CutH == Nf_CutHandle(pCutSet, pCut) && (int)pM[c]->Gate == Info && Nf_Cfg2Int(pM[c]->Cfg) == Nf_Cfg2Int(Cfg) )
2272 2273
                        {
                            Vec_IntPush( vSolCuts, Vec_WecSize(vCuts) );
2274
                            //printf( "adding solution for %d\n", Abc_Var2Lit(iObj, c)-LitShift );
2275 2276 2277 2278
                        }
                    }
                    // add new cut
                    iCutLit = Abc_Var2Lit( StartVar + Vec_WecSize(vCuts), 0 );
2279
                    vCutOne = Vec_WecPushLevel( vCuts );
2280
                    // add literals
2281
                    Vec_IntPush( vCutOne, Abc_Var2Lit(iObj, fCompl) );
2282 2283
                    Vec_IntPush( vObj[fCompl], iCutLit );
                    Nf_CfgForEachVarCompl( Cfg, nFans, iFanin, fComplF, k )
2284
                        if ( pFans[iFanin] >= nInputs + 1 ) // internal node
2285
                        {
2286 2287
                            Vec_IntPush( vCutOne, Abc_Var2Lit(pFans[iFanin], fComplF) );
                            //Vec_IntPush( Vec_WecEntry(vObjCuts, Abc_Var2Lit(pFans[iFanin], fComplF)-LitShift), iCutLit );
2288
                        }
2289 2290
                        else if ( fComplF ) // complemented primary input
                            Vec_IntPush( vCutOne, Abc_Var2Lit(pFans[iFanin], 1) );
2291 2292 2293 2294 2295 2296 2297 2298 2299
                } 
            }
        }
        assert( iCutInv[0] == -1 || iCutInv[1] == -1 );
        // add inverter cut
        for ( c = 0; c < 2; c++ )
        {
            if ( iCutInv[c] != -1 )
                Vec_IntWriteEntry( vSolCuts, iCutInv[c], Vec_WecSize(vCuts) );
2300 2301 2302 2303 2304 2305
            // the obj-lit implies its cut
            Vec_IntPush( Vec_WecEntry(vObjCuts, Abc_Var2Lit(iObj, c)-LitShift), Abc_Var2Lit(StartVar + Vec_WecSize(vCuts), 0) );
            // the cut includes both literals
            vCutOne = Vec_WecPushLevel( vCuts );
            Vec_IntPush( vCutOne, Abc_Var2Lit(iObj, c) );
            Vec_IntPush( vCutOne, Abc_Var2Lit(iObj, !c) );
2306
            Vec_IntPush( vCutGates, 3 );
2307
            Vec_WrdPush( vCutAreas, p->InvAreaW );
2308 2309
        }
    }
2310 2311 2312 2313 2314 2315 2316
//    for ( c = 0; c < p->nCells; c++ )
//        printf( "%d=%s ", c, p->pCells[c].pName );
//    printf( "\n" );
    // add complemented inputs
    Gia_ManForEachCiId( p->pGia, iObj, c )
        if ( Nf_ObjMapRefNum(p, iObj, 1) )
            Vec_IntPush( vSolCuts, -(2*Gia_ManAndNum(p->pGia)+c) );
2317
    assert( Vec_WecSize(vCuts) == Vec_IntSize(vCutGates) );
2318
    assert( Vec_WecSize(vCuts) == Vec_WrdSize(vCutAreas) );
2319
    assert( Vec_WecSize(vObjCuts) == 2*Gia_ManAndNum(p->pGia) );
2320
    return nInputs;
2321 2322 2323 2324
}

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

2325 2326 2327 2328 2329 2330 2331 2332 2333
  Synopsis    [Technology mappping.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
2334 2335
void Nf_ManSetDefaultPars( Jf_Par_t * pPars )
{
2336 2337 2338 2339
    memset( pPars, 0, sizeof(Jf_Par_t) );
    pPars->nLutSize     =  6;
    pPars->nCutNum      = 16;
    pPars->nProcNum     =  0;
Alan Mishchenko committed
2340 2341
    pPars->nRounds      =  4;
    pPars->nRoundsEla   =  2;
2342 2343
    pPars->nRelaxRatio  =  0;
    pPars->nCoarseLimit =  3;
Alan Mishchenko committed
2344
    pPars->nAreaTuner   =  0;
2345
    pPars->nReqTimeFlex =  0;
2346 2347 2348
    pPars->nVerbLimit   =  5;
    pPars->DelayTarget  = -1;
    pPars->fAreaOnly    =  0;
2349
    pPars->fPinPerm     =  0;
2350 2351
    pPars->fPinQuick    =  0;
    pPars->fPinFilter   =  0;
2352 2353 2354 2355 2356 2357 2358 2359 2360
    pPars->fOptEdge     =  1; 
    pPars->fCoarsen     =  0;
    pPars->fCutMin      =  1;
    pPars->fGenCnf      =  0;
    pPars->fPureAig     =  0;
    pPars->fVerbose     =  0;
    pPars->fVeryVerbose =  0;
    pPars->nLutSizeMax  =  NF_LEAF_MAX;
    pPars->nCutNumMax   =  NF_CUT_MAX;
2361
    pPars->MapDelayTarget = 0;
2362 2363 2364
}
Gia_Man_t * Nf_ManPerformMapping( Gia_Man_t * pGia, Jf_Par_t * pPars )
{
2365 2366 2367 2368 2369 2370
    Gia_Man_t * pNew = NULL, * pCls;
    Nf_Man_t * p; int i, Id;
    if ( Gia_ManHasChoices(pGia) )
        pPars->fCoarsen = 0; 
    pCls = pPars->fCoarsen ? Gia_ManDupMuxes(pGia, pPars->nCoarseLimit) : pGia;
    p = Nf_StoCreate( pCls, pPars );
2371 2372
    if ( p == NULL )
        return NULL;
2373 2374 2375 2376 2377 2378 2379 2380 2381 2382
//    if ( pPars->fVeryVerbose )
//        Nf_StoPrint( p, pPars->fVeryVerbose );
    if ( pPars->fVerbose && pPars->fCoarsen )
    {
        printf( "Initial " );  Gia_ManPrintMuxStats( pGia );  printf( "\n" );
        printf( "Derived " );  Gia_ManPrintMuxStats( pCls );  printf( "\n" );
    }
    Nf_ManPrintInit( p );
    Nf_ManComputeCuts( p );
    Nf_ManPrintQuit( p );
2383 2384 2385 2386 2387 2388 2389 2390
    if ( Scl_ConIsRunning() )
    {
        Gia_ManForEachCiId( p->pGia, Id, i )
            Nf_ObjPrepareCi( p, Id, Scl_ConGetInArr(i) );
    }
    else
    {
        Gia_ManForEachCiId( p->pGia, Id, i )
2391 2392
//            Nf_ObjPrepareCi( p, Id, Scl_Flt2Int(p->pGia->vInArrs ? Abc_MaxFloat(0.0, Vec_FltEntry(p->pGia->vInArrs, i)) : 0.0) );
            Nf_ObjPrepareCi( p, Id, Scl_Flt2Int(p->pGia->vInArrs ? Vec_FltEntry(p->pGia->vInArrs, i) : 0.0) );
2393
    }
2394 2395 2396 2397
    for ( p->Iter = 0; p->Iter < p->pPars->nRounds; p->Iter++ )
    {
        Nf_ManComputeMapping( p );
        Nf_ManSetMapRefs( p );
2398
        Nf_ManPrintStats( p, (char *)(p->Iter ? "Area " : "Delay") );
2399
    }
2400

2401 2402 2403
    p->fUseEla = 1;
    for ( ; p->Iter < p->pPars->nRounds + pPars->nRoundsEla; p->Iter++ )
    {
2404
        Nf_ManComputeMappingEla( p );
2405 2406 2407
        Nf_ManUpdateStats( p );
        Nf_ManPrintStats( p, "Ela  " );
    }
2408
    Nf_ManFixPoDrivers( p );
2409
    pNew = Nf_ManDeriveMapping( p );
2410
/*
2411
    if ( pPars->fAreaOnly )
2412
    {
2413 2414
        int Sbm_ManTestSat( void * pMan );
        Sbm_ManTestSat( p );
2415
    }
2416
*/
2417 2418
    Nf_StoDelete( p );
    return pNew;
2419 2420 2421 2422 2423 2424 2425 2426 2427
}

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


ABC_NAMESPACE_IMPL_END