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

  FileName    [giaMap.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [Scalable AIG package.]

  Synopsis    [Manipulation of mapping associated with the AIG.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

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

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

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

#include "gia.h"
22 23
#include "aig/aig/aig.h"
#include "map/if/if.h"
24
#include "bool/kit/kit.h"
25 26
#include "base/main/main.h"
#include "sat/bsat/satSolver.h"
27

28 29 30 31
#ifdef WIN32
#include <windows.h>
#endif

32 33
ABC_NAMESPACE_IMPL_START

Alan Mishchenko committed
34 35 36 37 38

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

39
extern int Kit_TruthToGia( Gia_Man_t * pMan, unsigned * pTruth, int nVars, Vec_Int_t * vMemory, Vec_Int_t * vLeaves, int fHash );
40
extern int Abc_RecToGia3( Gia_Man_t * pMan, If_Man_t * pIfMan, If_Cut_t * pCut, Vec_Int_t * vLeaves, int fHash );
41

42 43
extern void Gia_ManPrintGetMuxFanins( Gia_Man_t * p, Gia_Obj_t * pObj, int * pFanins );

Alan Mishchenko committed
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
////////////////////////////////////////////////////////////////////////
///                     FUNCTION DEFINITIONS                         ///
////////////////////////////////////////////////////////////////////////

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

  Synopsis    [Load the network into FPGA manager.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
59
void Gia_ManSetIfParsDefault( void * pp )
Alan Mishchenko committed
60
{
61
    If_Par_t * pPars = (If_Par_t *)pp;
Alan Mishchenko committed
62
//    extern void * Abc_FrameReadLibLut();
63
    If_Par_t * p = (If_Par_t *)pPars;
Alan Mishchenko committed
64
    // set defaults
65
    memset( p, 0, sizeof(If_Par_t) );
Alan Mishchenko committed
66
    // user-controlable paramters
67 68 69 70 71 72 73 74 75 76
    p->nLutSize    = -1;
//    p->nLutSize    =  6;
    p->nCutsMax    =  8;
    p->nFlowIters  =  1;
    p->nAreaIters  =  2;
    p->DelayTarget = -1;
    p->Epsilon     =  (float)0.005;
    p->fPreprocess =  1;
    p->fArea       =  0;
    p->fFancy      =  0;
77
    p->fExpRed     =  1; ////
78 79 80 81 82 83
    p->fLatchPaths =  0;
    p->fEdge       =  1;
    p->fPower      =  0;
    p->fCutMin     =  0;
    p->fVerbose    =  0;
    p->pLutStruct  =  NULL;
Alan Mishchenko committed
84
    // internal parameters
85 86 87 88 89 90 91
    p->fTruth      =  0;
    p->nLatchesCi  =  0;
    p->nLatchesCo  =  0;
    p->fLiftLeaves =  0;
    p->fUseCoAttrs =  1;   // use CO attributes
    p->pLutLib     =  NULL;
    p->pTimesArr   =  NULL; 
92
    p->pTimesReq   =  NULL;   
93
    p->pFuncCost   =  NULL;   
Alan Mishchenko committed
94 95
}

96

Alan Mishchenko committed
97 98
/**Function*************************************************************

99
  Synopsis    [Prints mapping statistics.]
Alan Mishchenko committed
100 101 102 103 104 105 106 107

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
108
int Gia_ManLutFaninCount( Gia_Man_t * p )
Alan Mishchenko committed
109
{
110 111 112 113
    int i, Counter = 0;
    Gia_ManForEachLut( p, i )
        Counter += Gia_ObjLutSize(p, i);
    return Counter;
Alan Mishchenko committed
114 115 116 117
}

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

118
  Synopsis    [Prints mapping statistics.]
Alan Mishchenko committed
119 120 121 122 123 124 125 126

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
127
int Gia_ManLutSizeMax( Gia_Man_t * p )
Alan Mishchenko committed
128
{
129 130 131 132
    int i, nSizeMax = -1;
    Gia_ManForEachLut( p, i )
        nSizeMax = Abc_MaxInt( nSizeMax, Gia_ObjLutSize(p, i) );
    return nSizeMax;
Alan Mishchenko committed
133 134 135 136
}

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

137
  Synopsis    [Prints mapping statistics.]
Alan Mishchenko committed
138 139 140 141 142 143 144 145

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
146
int Gia_ManLutNum( Gia_Man_t * p )
Alan Mishchenko committed
147
{
148 149 150 151
    int i, Counter = 0;
    Gia_ManForEachLut( p, i )
        Counter ++;
    return Counter;
Alan Mishchenko committed
152 153 154 155 156 157 158 159 160 161 162 163 164
}

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

  Synopsis    [Prints mapping statistics.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
165
int Gia_ManLutLevel( Gia_Man_t * p, int ** ppLevels )
Alan Mishchenko committed
166
{
167 168 169
    Gia_Obj_t * pObj;
    int i, k, iFan, Level;
    int * pLevels = ABC_CALLOC( int, Gia_ManObjNum(p) );
170
    Gia_ManForEachLut( p, i )
Alan Mishchenko committed
171
    {
172
        Level = 0;
173
        Gia_LutForEachFanin( p, i, iFan, k )
174 175 176
            if ( Level < pLevels[iFan] )
                Level = pLevels[iFan];
        pLevels[i] = Level + 1;
Alan Mishchenko committed
177
    }
178 179
    Level = 0;
    Gia_ManForEachCo( p, pObj, k )
180 181 182 183 184
    {
        int LevelFan = pLevels[Gia_ObjFaninId0p(p, pObj)];
        Level = Abc_MaxInt( Level, LevelFan );
        pLevels[Gia_ObjId(p, pObj)] = LevelFan;
    }
185 186 187 188
    if ( ppLevels )
        *ppLevels = pLevels;
    else
        ABC_FREE( pLevels );
189
    return Level;
190 191 192 193
}

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

194 195 196 197 198 199 200 201 202 203 204
  Synopsis    [Prints mapping statistics.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Gia_ManLutParams( Gia_Man_t * p, int * pnCurLuts, int * pnCurEdges, int * pnCurLevels )
{
205
    int fDisable2Lut = 1;
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
    if ( p->pManTime && Tim_ManBoxNum((Tim_Man_t *)p->pManTime) )
    {
        int i;
        *pnCurLuts = 0;
        *pnCurEdges = 0;
        Gia_ManForEachLut( p, i )
        {
            (*pnCurLuts)++;
            (*pnCurEdges) += Gia_ObjLutSize(p, i);
        }
        *pnCurLevels = Gia_ManLutLevelWithBoxes( p );
    }
    else
    {
        Gia_Obj_t * pObj;
        int i, k, iFan;
        int * pLevels = ABC_CALLOC( int, Gia_ManObjNum(p) );
        *pnCurLuts = 0;
        *pnCurEdges = 0;
225
        *pnCurLevels = 0;
226 227
        Gia_ManForEachLut( p, i )
        {
228
            if ( Gia_ObjLutIsMux(p, i) && !(fDisable2Lut && Gia_ObjLutSize(p, i) == 2) )
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
            {
                int pFanins[3];
                if ( Gia_ObjLutSize(p, i) == 3 )
                {
                    Gia_ManPrintGetMuxFanins( p, Gia_ManObj(p, i), pFanins );
                    pLevels[i] = Abc_MaxInt( pLevels[i], pLevels[pFanins[0]]+1 );
                    pLevels[i] = Abc_MaxInt( pLevels[i], pLevels[pFanins[1]] );
                    pLevels[i] = Abc_MaxInt( pLevels[i], pLevels[pFanins[2]] );
                }
                else if ( Gia_ObjLutSize(p, i) == 2 )
                {
                    pObj = Gia_ManObj( p, i );
                    pLevels[i] = Abc_MaxInt( pLevels[i], pLevels[Gia_ObjFaninId0(pObj, i)] );
                    pLevels[i] = Abc_MaxInt( pLevels[i], pLevels[Gia_ObjFaninId1(pObj, i)] );
                }
                *pnCurLevels = Abc_MaxInt( *pnCurLevels, pLevels[i] );
245
                (*pnCurEdges)++;
246 247 248
                //nMuxF++;
                continue;
            }
249 250 251
            (*pnCurLuts)++;
            (*pnCurEdges) += Gia_ObjLutSize(p, i);
            Gia_LutForEachFanin( p, i, iFan, k )
252 253 254
                pLevels[i] = Abc_MaxInt( pLevels[i], pLevels[iFan] );
            pLevels[i]++;
            *pnCurLevels = Abc_MaxInt( *pnCurLevels, pLevels[i] );
255 256 257 258 259 260 261
        }
        ABC_FREE( pLevels );
    }
}

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

262
  Synopsis    [Assigns levels.]
263 264 265 266 267 268 269 270

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
271
void Gia_ManSetRefsMapped( Gia_Man_t * p )  
272
{
273 274 275 276 277
    Gia_Obj_t * pObj;
    int i, k, iFan;
    ABC_FREE( p->pRefs );
    p->pRefs = ABC_CALLOC( int, Gia_ManObjNum(p) );
    Gia_ManForEachCo( p, pObj, i )
278
        Gia_ObjRefIncId( p, Gia_ObjFaninId0p(p, pObj) );
279
    Gia_ManForEachLut( p, i )
280
        Gia_LutForEachFanin( p, i, iFan, k )
281
            Gia_ObjRefIncId( p, iFan );
282 283 284 285
}

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

286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
  Synopsis    [Assigns levels.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Gia_ManSetLutRefs( Gia_Man_t * p )  
{
    Gia_Obj_t * pObj;
    int i, k, iFan;
    ABC_FREE( p->pLutRefs );
    p->pLutRefs = ABC_CALLOC( int, Gia_ManObjNum(p) );
    Gia_ManForEachCo( p, pObj, i )
302
        Gia_ObjLutRefIncId( p, Gia_ObjFaninId0p(p, pObj) );
303 304
    Gia_ManForEachLut( p, i )
        Gia_LutForEachFanin( p, i, iFan, k )
305
            Gia_ObjLutRefIncId( p, iFan );
306 307 308 309
}

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

310 311 312 313 314 315 316 317 318
  Synopsis    [Calculate mapping overlap.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
319
int Gia_ManComputeOverlap2One_rec( Gia_Man_t * p, int iObj, Vec_Str_t * vLabel, Vec_Int_t * vVisit )
320 321 322 323 324 325 326 327
{
    Gia_Obj_t * pObj;
    int Counter;
    if ( Vec_StrEntry(vLabel, iObj) )
        return 0;
    Vec_StrWriteEntry( vLabel, iObj, 1 );
    pObj = Gia_ManObj( p, iObj );
    assert( Gia_ObjIsAnd(pObj) );
328 329
    Counter  = Gia_ManComputeOverlap2One_rec( p, Gia_ObjFaninId0(pObj, iObj), vLabel, vVisit );
    Counter += Gia_ManComputeOverlap2One_rec( p, Gia_ObjFaninId1(pObj, iObj), vLabel, vVisit );
330 331 332
    Vec_IntPush( vVisit, iObj );
    return Counter + 1;
}
333
int Gia_ManComputeOverlap2One( Gia_Man_t * p, int iObj, Vec_Str_t * vLabel, Vec_Int_t * vVisit )
334 335 336 337 338
{
    int iFan, k, Counter;
    Vec_IntClear( vVisit );
    Gia_LutForEachFanin( p, iObj, iFan, k )
        Vec_StrWriteEntry( vLabel, iFan, 1 );
339
    Counter = Gia_ManComputeOverlap2One_rec( p, iObj, vLabel, vVisit );
340 341 342 343 344 345
    Gia_LutForEachFanin( p, iObj, iFan, k )
        Vec_StrWriteEntry( vLabel, iFan, 0 );
    Vec_IntForEachEntry( vVisit, iFan, k )
        Vec_StrWriteEntry( vLabel, iFan, 0 );
    return Counter;
}
346
int Gia_ManComputeOverlap2( Gia_Man_t * p )
347 348 349
{
    Vec_Int_t * vVisit;
    Vec_Str_t * vLabel;
350
    int i, Count = -Gia_ManAndNum(p);
351 352 353 354
    assert( Gia_ManHasMapping(p) );
    vVisit = Vec_IntAlloc( 100 );
    vLabel = Vec_StrStart( Gia_ManObjNum(p) );
    Gia_ManForEachLut( p, i )
355
        Count += Gia_ManComputeOverlap2One( p, i, vLabel, vVisit );
356 357 358 359 360 361 362
    Vec_StrFree( vLabel );
    Vec_IntFree( vVisit );
    return Count;
}

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

363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
  Synopsis    [Calculate mapping overlap.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Gia_ManComputeOverlapOne_rec( Gia_Man_t * p, int iObj )
{
    Gia_Obj_t * pObj;
    if ( Gia_ObjIsTravIdCurrentId(p, iObj) )
        return 0;
    Gia_ObjSetTravIdCurrentId( p, iObj );
    pObj = Gia_ManObj( p, iObj );
    assert( Gia_ObjIsAnd(pObj) );
    return 1 + Gia_ManComputeOverlapOne_rec( p, Gia_ObjFaninId0(pObj, iObj) )
          + Gia_ManComputeOverlapOne_rec( p, Gia_ObjFaninId1(pObj, iObj) );
}
int Gia_ManComputeOverlapOne( Gia_Man_t * p, int iObj )
{
    int iFan, k;
    Gia_ManIncrementTravId(p);
    Gia_LutForEachFanin( p, iObj, iFan, k )
        Gia_ObjSetTravIdCurrentId( p, iFan );
    return Gia_ManComputeOverlapOne_rec( p, iObj );
}
int Gia_ManComputeOverlap( Gia_Man_t * p )
{
    int i, Count = -Gia_ManAndNum(p);
    assert( Gia_ManHasMapping(p) );
    Gia_ManForEachLut( p, i )
        Count += Gia_ManComputeOverlapOne( p, i );
    return Count;
}

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

402 403 404 405 406 407 408 409 410
  Synopsis    [Prints mapping statistics.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
411 412 413 414 415 416 417 418
void Gia_ManPrintGetMuxFanins( Gia_Man_t * p, Gia_Obj_t * pObj, int * pFanins )
{
    Gia_Obj_t * pData0, * pData1;
    Gia_Obj_t * pCtrl = Gia_ObjRecognizeMux( pObj, &pData1, &pData0 );
    pFanins[0] = Gia_ObjId(p, Gia_Regular(pCtrl));
    pFanins[1] = Gia_ObjId(p, Gia_Regular(pData1));
    pFanins[2] = Gia_ObjId(p, Gia_Regular(pData0));
}
419
int Gia_ManCountDupLut( Gia_Man_t * p )
420
{
421 422
    Gia_Obj_t * pObj, * pFanin;
    int i, pFanins[3], nCountDup = 0, nCountPis = 0, nCountMux = 0;
423
    Gia_ManCleanMark01( p );
424
    Gia_ManForEachLut( p, i )
425
        if ( Gia_ObjLutIsMux(p, i) )
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
            pObj = Gia_ManObj( p, i );
            pObj->fMark1 = 1;
            if ( Gia_ObjLutSize(p, i) == 3 )
            {
                Gia_ManPrintGetMuxFanins( p, pObj, pFanins );

                pFanin = Gia_ManObj(p, pFanins[1]);
                nCountPis += Gia_ObjIsCi(pFanin);
                nCountDup += pFanin->fMark0;
                nCountMux += pFanin->fMark1;
                pFanin->fMark0 = 1;

                pFanin = Gia_ManObj(p, pFanins[2]);
                nCountPis += Gia_ObjIsCi(pFanin);
                nCountDup += pFanin->fMark0;
                nCountMux += pFanin->fMark1;
                pFanin->fMark0 = 1;
            }
            else if ( Gia_ObjLutSize(p, i) == 2 )
            {
                pFanin = Gia_ObjFanin0(pObj);
                if ( pFanin->fMark0 || pFanin->fMark1 )
                {
                    pFanin = Gia_ObjFanin1(pObj);
                    nCountPis += Gia_ObjIsCi(pFanin);
                    nCountDup += pFanin->fMark0;
                    nCountMux += pFanin->fMark1;
                    pFanin->fMark0 = 1;
                }
                else
                {
                    nCountPis += Gia_ObjIsCi(pFanin);
                    nCountDup += pFanin->fMark0;
                    nCountMux += pFanin->fMark1;
                    pFanin->fMark0 = 1;
                }
            }
            else assert( 0 );
465
        }
466 467 468 469
    Gia_ManCleanMark01( p );
    if ( nCountDup + nCountPis + nCountMux )
        printf( "Dup fanins = %d.  CI fanins = %d.  MUX fanins = %d.  Total = %d.  (%.2f %%)\n", 
            nCountDup, nCountPis, nCountMux, nCountDup + nCountPis, 100.0 * (nCountDup + nCountPis + nCountMux) / Gia_ManLutNum(p) );
470 471 472
    return nCountDup + nCountPis;
}

473
void Gia_ManPrintMappingStats( Gia_Man_t * p, char * pDumpFile )
474
{
475
    int fDisable2Lut = 1;
476
    Gia_Obj_t * pObj;
477
    int * pLevels;
478
    int i, k, iFan, nLutSize = 0, nLuts = 0, nFanins = 0, LevelMax = 0, Ave = 0, nMuxF = 0;
479
    if ( !Gia_ManHasMapping(p) )
480 481
        return;
    pLevels = ABC_CALLOC( int, Gia_ManObjNum(p) );
482
    Gia_ManForEachLut( p, i )
483
    {
484
        if ( Gia_ObjLutIsMux(p, i) && !(fDisable2Lut && Gia_ObjLutSize(p, i) == 2) )
485 486
        {
            int pFanins[3];
487 488 489 490 491 492 493 494 495 496 497 498 499
            if ( Gia_ObjLutSize(p, i) == 3 )
            {
                Gia_ManPrintGetMuxFanins( p, Gia_ManObj(p, i), pFanins );
                pLevels[i] = Abc_MaxInt( pLevels[i], pLevels[pFanins[0]]+1 );
                pLevels[i] = Abc_MaxInt( pLevels[i], pLevels[pFanins[1]] );
                pLevels[i] = Abc_MaxInt( pLevels[i], pLevels[pFanins[2]] );
            }
            else if ( Gia_ObjLutSize(p, i) == 2 )
            {
                pObj = Gia_ManObj( p, i );
                pLevels[i] = Abc_MaxInt( pLevels[i], pLevels[Gia_ObjFaninId0(pObj, i)] );
                pLevels[i] = Abc_MaxInt( pLevels[i], pLevels[Gia_ObjFaninId1(pObj, i)] );
            }
500
            LevelMax = Abc_MaxInt( LevelMax, pLevels[i] );
501
            nFanins++;
502
            nMuxF++;
503 504
            continue;
        }
505 506 507 508 509 510 511 512
        nLuts++;
        nFanins += Gia_ObjLutSize(p, i);
        nLutSize = Abc_MaxInt( nLutSize, Gia_ObjLutSize(p, i) );
        Gia_LutForEachFanin( p, i, iFan, k )
            pLevels[i] = Abc_MaxInt( pLevels[i], pLevels[iFan] );
        pLevels[i]++;
        LevelMax = Abc_MaxInt( LevelMax, pLevels[i] );
    }
513 514
    Gia_ManForEachCo( p, pObj, i )
        Ave += pLevels[Gia_ObjFaninId0p(p, pObj)];
515
    ABC_FREE( pLevels );
516 517 518 519

#ifdef WIN32
    {
    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
520
    Abc_Print( 1, "Mapping (K=%d)  :  ", nLutSize );
521 522
    SetConsoleTextAttribute( hConsole, 14 ); // yellow
    Abc_Print( 1, "lut =%7d  ",  nLuts );
523 524
    if ( nMuxF )
    Abc_Print( 1, "muxF =%7d  ",  nMuxF );
525
    SetConsoleTextAttribute( hConsole, 10 ); // green
526
    Abc_Print( 1, "edge =%8d  ", nFanins );
527
    SetConsoleTextAttribute( hConsole, 12 ); // red
528 529
    Abc_Print( 1, "lev =%5d ",   LevelMax );
    Abc_Print( 1, "(%.2f)  ",    (float)Ave / Gia_ManCoNum(p) );
530 531 532
//    Abc_Print( 1, "over =%5.1f %%  ", 100.0 * Gia_ManComputeOverlap(p) / Gia_ManAndNum(p) );
    if ( p->pManTime && Tim_ManBoxNum((Tim_Man_t *)p->pManTime) )
        Abc_Print( 1, "levB =%5d  ", Gia_ManLutLevelWithBoxes(p) );
533
    SetConsoleTextAttribute( hConsole, 7 );  // normal
534 535
    Abc_Print( 1, "mem =%5.2f MB", 4.0*(Gia_ManObjNum(p) + 2*nLuts + nFanins)/(1<<20) );
    Abc_Print( 1, "\n" );
536 537 538 539 540
    }
#else
    Abc_Print( 1, "Mapping (K=%d)  :  ", nLutSize );
    Abc_Print( 1, "%slut =%7d%s  ",  "\033[1;33m", nLuts,    "\033[0m" );  // yellow
    Abc_Print( 1, "%sedge =%8d%s  ", "\033[1;32m", nFanins,  "\033[0m" );  // green
541 542
    Abc_Print( 1, "%slev =%5d%s ",   "\033[1;31m", LevelMax, "\033[0m" );  // red
    Abc_Print( 1, "%s(%.2f)%s  ",    "\033[1;31m", (float)Ave / Gia_ManCoNum(p), "\033[0m" );
543 544
//    Abc_Print( 1, "over =%5.1f %%  ", 100.0 * Gia_ManComputeOverlap(p) / Gia_ManAndNum(p) );
    if ( p->pManTime && Tim_ManBoxNum((Tim_Man_t *)p->pManTime) )
545
        Abc_Print( 1, "%slevB =%5d%s  ", "\033[1;31m", Gia_ManLutLevelWithBoxes(p), "\033[0m" );
546 547 548 549
    Abc_Print( 1, "mem =%5.2f MB", 4.0*(Gia_ManObjNum(p) + 2*nLuts + nFanins)/(1<<20) );
    Abc_Print( 1, "\n" );
#endif

550 551
    if ( nMuxF )
        Gia_ManCountDupLut( p );
552

553
    //return;
554
    if ( pDumpFile )
555 556 557
    {
        static char FileNameOld[1000] = {0};
        static abctime clk = 0;
558
        FILE * pTable = fopen( pDumpFile, "a+" );
559 560 561 562
        if ( strcmp( FileNameOld, p->pName ) )
        {
            sprintf( FileNameOld, "%s", p->pName );
            fprintf( pTable, "\n" );
563
            fprintf( pTable, "%s ", p->pName );
564
            fprintf( pTable, " " );
565
            fprintf( pTable, "%d ", Gia_ManAndNum(p) );
566
            fprintf( pTable, "%d ", nLuts           );
567
            fprintf( pTable, "%d ", Gia_ManLutLevelWithBoxes(p) );
568 569 570
            //fprintf( pTable, "%d ", Gia_ManRegBoxNum(p) );
            //fprintf( pTable, "%d ", Gia_ManNonRegBoxNum(p) );
            //fprintf( pTable, "%.2f", 1.0*(Abc_Clock() - clk)/CLOCKS_PER_SEC );
571 572 573 574
            clk = Abc_Clock();
        }
        else
        {
575 576
            //printf( "This part of the code is currently not used.\n" );
            //assert( 0 );
577 578
            fprintf( pTable, " " );
            fprintf( pTable, "%d ", nLuts           );
579 580 581
            fprintf( pTable, "%d ", Gia_ManLutLevelWithBoxes(p) );
            //fprintf( pTable, "%d ", Gia_ManRegBoxNum(p) );
            //fprintf( pTable, "%d ", Gia_ManNonRegBoxNum(p) );
582
            fprintf( pTable, "%.2f", 1.0*(Abc_Clock() - clk)/CLOCKS_PER_SEC );
583
            clk = Abc_Clock();
584 585 586
        }
        fclose( pTable );
    }
587

588 589
}

590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605
/**Function*************************************************************

  Synopsis    [Prints mapping statistics.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Gia_ManPrintPackingStats( Gia_Man_t * p )
{
    int fVerbose = 0;
    int nObjToShow = 200;
    int nNumStr[5] = {0};
606
    int i, k, Entry, nEntries, nEntries2, MaxSize = -1, Count = 0;
607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629
    if ( p->vPacking == NULL )
        return;
    nEntries = Vec_IntEntry( p->vPacking, 0 );
    nEntries2 = 0;
    Vec_IntForEachEntryStart( p->vPacking, Entry, i, 1 )
    {
        assert( Entry > 0 && Entry < 4 );
        nNumStr[Entry]++;
        i++;
        if ( fVerbose && nEntries2 < nObjToShow ) Abc_Print( 1, "{ " );
        for ( k = 0; k < Entry; k++, i++ )
            if ( fVerbose && nEntries2 < nObjToShow ) Abc_Print( 1, "%d ", Vec_IntEntry(p->vPacking, i) );
        if ( fVerbose && nEntries2 < nObjToShow ) Abc_Print( 1, "}\n" );
        i--;
        nEntries2++;
    }
    assert( nEntries == nEntries2 );
    if ( nNumStr[3] > 0 )
        MaxSize = 3;
    else if ( nNumStr[2] > 0 )
        MaxSize = 2;
    else if ( nNumStr[1] > 0 )
        MaxSize = 1;
630
    Abc_Print( 1, "Packing (N=%d)  :  ", MaxSize );
631
    for ( i = 1; i <= MaxSize; i++ )
632
    {
633
        Abc_Print( 1, "%d x LUT = %d   ", i, nNumStr[i] );
634 635 636 637
        Count += i * nNumStr[i];
    }
    Abc_Print( 1, "Total = %d   ", nEntries2 );
    Abc_Print( 1, "Total LUT = %d", Count );
638 639 640
    Abc_Print( 1, "\n" );
}

641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677
/**Function*************************************************************

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Gia_ManPrintNodeProfile( int * pCounts, int nSizeMax )
{
    int i, SizeAll = 0, NodeAll = 0;
    for ( i = 0; i <= nSizeMax; i++ )
    {
        SizeAll += i * pCounts[i];
        NodeAll += pCounts[i];
    }
    Abc_Print( 1, "LUT = %d : ", NodeAll );
    for ( i = 2; i <= nSizeMax; i++ )
        Abc_Print( 1, "%d=%d %.1f %%  ", i, pCounts[i], 100.0*pCounts[i]/NodeAll );
    Abc_Print( 1, "Ave = %.2f\n", 1.0*SizeAll/(NodeAll ? NodeAll : 1) );
}
void Gia_ManPrintLutStats( Gia_Man_t * p )
{
    int i, nSizeMax, pCounts[33] = {0};
    nSizeMax = Gia_ManLutSizeMax( p );
    if ( nSizeMax > 32 )
    {
        Abc_Print( 1, "The max LUT size (%d) is too large.\n", nSizeMax );
        return;
    }
    Gia_ManForEachLut( p, i )
        pCounts[ Gia_ObjLutSize(p, i) ]++;
    Gia_ManPrintNodeProfile( pCounts, nSizeMax );
}
678

679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779
/**Function*************************************************************

  Synopsis    [Computes levels for AIG with choices and white boxes.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Gia_ManChoiceLevel_rec( Gia_Man_t * p, Gia_Obj_t * pObj )
{
    Tim_Man_t * pManTime = (Tim_Man_t *)p->pManTime;
    Gia_Obj_t * pNext;
    int i, iBox, iTerm1, nTerms, LevelMax = 0;
    if ( Gia_ObjIsTravIdCurrent( p, pObj ) )
        return;
    Gia_ObjSetTravIdCurrent( p, pObj );
    if ( Gia_ObjIsCi(pObj) )
    {
        if ( pManTime )
        {
            iBox = Tim_ManBoxForCi( pManTime, Gia_ObjCioId(pObj) );
            if ( iBox >= 0 ) // this is not a true PI
            {
                iTerm1 = Tim_ManBoxInputFirst( pManTime, iBox );
                nTerms = Tim_ManBoxInputNum( pManTime, iBox );
                for ( i = 0; i < nTerms; i++ )
                {
                    pNext = Gia_ManCo( p, iTerm1 + i );
                    Gia_ManChoiceLevel_rec( p, pNext );
                    if ( LevelMax < Gia_ObjLevel(p, pNext) )
                        LevelMax = Gia_ObjLevel(p, pNext);
                }
                LevelMax++;
            }
        }
//        Abc_Print( 1, "%d ", pObj->Level );
    }
    else if ( Gia_ObjIsCo(pObj) )
    {
        pNext = Gia_ObjFanin0(pObj);
        Gia_ManChoiceLevel_rec( p, pNext );
        if ( LevelMax < Gia_ObjLevel(p, pNext) )
            LevelMax = Gia_ObjLevel(p, pNext);
    }
    else if ( Gia_ObjIsAnd(pObj) )
    { 
        // get the maximum level of the two fanins
        pNext = Gia_ObjFanin0(pObj);
        Gia_ManChoiceLevel_rec( p, pNext );
        if ( LevelMax < Gia_ObjLevel(p, pNext) )
            LevelMax = Gia_ObjLevel(p, pNext);
        pNext = Gia_ObjFanin1(pObj);
        Gia_ManChoiceLevel_rec( p, pNext );
        if ( LevelMax < Gia_ObjLevel(p, pNext) )
            LevelMax = Gia_ObjLevel(p, pNext);
        LevelMax++;

        // get the level of the nodes in the choice node
        if ( (pNext = Gia_ObjSiblObj(p, Gia_ObjId(p, pObj))) )
        {
            Gia_ManChoiceLevel_rec( p, pNext );
            if ( LevelMax < Gia_ObjLevel(p, pNext) )
                LevelMax = Gia_ObjLevel(p, pNext);
        }
    }
    else if ( !Gia_ObjIsConst0(pObj) )
        assert( 0 );
    Gia_ObjSetLevel( p, pObj, LevelMax );
}
int Gia_ManChoiceLevel( Gia_Man_t * p )
{
    Gia_Obj_t * pObj;
    int i, LevelMax = 0;
//    assert( Gia_ManRegNum(p) == 0 );
    Gia_ManCleanLevels( p, Gia_ManObjNum(p) );
    Gia_ManIncrementTravId( p );
    Gia_ManForEachCo( p, pObj, i )
    {
        Gia_ManChoiceLevel_rec( p, pObj );
        if ( LevelMax < Gia_ObjLevel(p, pObj) )
            LevelMax = Gia_ObjLevel(p, pObj);
    }
    // account for dangling boxes
    Gia_ManForEachCi( p, pObj, i )
    {
        Gia_ManChoiceLevel_rec( p, pObj );
        if ( LevelMax < Gia_ObjLevel(p, pObj) )
            LevelMax = Gia_ObjLevel(p, pObj);
//        Abc_Print( 1, "%d ", Gia_ObjLevel(p, pObj) );
    }
//    Abc_Print( 1, "\n" );
    Gia_ManForEachAnd( p, pObj, i )
        assert( Gia_ObjLevel(p, pObj) > 0 );
//    printf( "Max level %d\n", LevelMax );
    return LevelMax;
} 


780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816
/**Function*************************************************************

  Synopsis    [Checks integrity of choice nodes.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void If_ManCheckChoices_rec( If_Man_t * pIfMan, If_Obj_t * pIfObj )
{
    if ( !pIfObj || pIfObj->Type != IF_AND || pIfObj->fDriver )
        return;
    pIfObj->fDriver = 1;
    If_ManCheckChoices_rec( pIfMan, If_ObjFanin0(pIfObj) );
    If_ManCheckChoices_rec( pIfMan, If_ObjFanin1(pIfObj) );
    If_ManCheckChoices_rec( pIfMan, pIfObj->pEquiv );
}
void If_ManCheckChoices( If_Man_t * pIfMan )
{
    If_Obj_t * pIfObj;
    int i, fFound = 0;
    If_ManForEachObj( pIfMan, pIfObj, i )
        pIfObj->fDriver = 0;
    If_ManForEachCo( pIfMan, pIfObj, i )
        If_ManCheckChoices_rec( pIfMan, If_ObjFanin0(pIfObj) );
    If_ManForEachNode( pIfMan, pIfObj, i )
        if ( !pIfObj->fDriver )
            printf( "Object %d is dangling.\n", i ), fFound = 1;
    if ( !fFound )
        printf( "There are no dangling objects.\n" );
    If_ManForEachObj( pIfMan, pIfObj, i )
        pIfObj->fDriver = 0;
}

817

818 819
/**Function*************************************************************

820
  Synopsis    [Converts GIA into IF manager.]
821 822 823 824 825 826 827 828

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
829 830 831
static inline If_Obj_t * If_ManFanin0Copy( If_Man_t * pIfMan, Gia_Obj_t * pObj ) { return If_NotCond( If_ManObj(pIfMan, Gia_ObjValue(Gia_ObjFanin0(pObj))), Gia_ObjFaninC0(pObj) ); }
static inline If_Obj_t * If_ManFanin1Copy( If_Man_t * pIfMan, Gia_Obj_t * pObj ) { return If_NotCond( If_ManObj(pIfMan, Gia_ObjValue(Gia_ObjFanin1(pObj))), Gia_ObjFaninC1(pObj) ); }
If_Man_t * Gia_ManToIf( Gia_Man_t * p, If_Par_t * pPars )
832
{
833
    If_Man_t * pIfMan;
Alan Mishchenko committed
834
    If_Obj_t * pIfObj = NULL;
835 836 837 838 839
    Gia_Obj_t * pObj;
    int i;
    // create levels with choices
    Gia_ManChoiceLevel( p );
    // mark representative nodes
840 841
    if ( Gia_ManHasChoices(p) )
        Gia_ManMarkFanoutDrivers( p );
842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876
    // start the mapping manager and set its parameters
    pIfMan = If_ManStart( pPars );
    pIfMan->pName = Abc_UtilStrsav( Gia_ManName(p) );
    // print warning about excessive memory usage
    if ( 1.0 * Gia_ManObjNum(p) * pIfMan->nObjBytes / (1<<30) > 1.0 )
        printf( "Warning: The mapper will allocate %.1f GB for to represent the subject graph with %d AIG nodes.\n", 
            1.0 * Gia_ManObjNum(p) * pIfMan->nObjBytes / (1<<30), Gia_ManObjNum(p) );
    // load the AIG into the mapper
    Gia_ManFillValue( p );
    Gia_ManConst0(p)->Value = If_ObjId( If_ManConst1(pIfMan) );
    Gia_ManForEachObj1( p, pObj, i )
    {
        if ( Gia_ObjIsAnd(pObj) )
            pIfObj = If_ManCreateAnd( pIfMan, If_ManFanin0Copy(pIfMan, pObj), If_ManFanin1Copy(pIfMan, pObj) );
        else if ( Gia_ObjIsCi(pObj) )
        {
            pIfObj = If_ManCreateCi( pIfMan );
            If_ObjSetLevel( pIfObj, Gia_ObjLevel(p, pObj) );
//            Abc_Print( 1, "pi%d=%d\n ", If_ObjId(pIfObj), If_ObjLevel(pIfObj) );
            if ( pIfMan->nLevelMax < (int)pIfObj->Level )
                pIfMan->nLevelMax = (int)pIfObj->Level;
        }
        else if ( Gia_ObjIsCo(pObj) )
        {
            pIfObj = If_ManCreateCo( pIfMan, If_NotCond( If_ManFanin0Copy(pIfMan, pObj), Gia_ObjIsConst0(Gia_ObjFanin0(pObj))) );
//            Abc_Print( 1, "po%d=%d\n ", If_ObjId(pIfObj), If_ObjLevel(pIfObj) );
        }
        else assert( 0 );
        assert( i == If_ObjId(pIfObj) );
        Gia_ObjSetValue( pObj, If_ObjId(pIfObj) );
        // set up the choice node
        if ( Gia_ObjSibl(p, i) && pObj->fMark0 )
        {
            Gia_Obj_t * pSibl, * pPrev;
            for ( pPrev = pObj, pSibl = Gia_ObjSiblObj(p, i); pSibl; pPrev = pSibl, pSibl = Gia_ObjSiblObj(p, Gia_ObjId(p, pSibl)) )
Alan Mishchenko committed
877
                If_ObjSetChoice( If_ManObj(pIfMan, Gia_ObjValue(pPrev)), If_ManObj(pIfMan, Gia_ObjValue(pSibl)) );
878
            If_ManCreateChoice( pIfMan, If_ManObj(pIfMan, Gia_ObjValue(pObj)) );
879
            pPars->fExpRed = 0;
880 881 882
        }
//        assert( If_ObjLevel(pIfObj) == Gia_ObjLevel(pNode) );
    }
883 884
    if ( Gia_ManHasChoices(p) )
        Gia_ManCleanMark0( p );
885
    //If_ManCheckChoices( pIfMan );
886
    return pIfMan;
887 888 889 890
}

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

891 892 893 894 895 896 897 898 899
  Synopsis    [Rebuilds GIA from mini AIG.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
900
int Gia_ManBuildFromMiniInt( Gia_Man_t * pNew, Vec_Int_t * vLeaves, Vec_Int_t * vAig, int fHash )
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
{
    assert( Vec_IntSize(vAig) > 0 );
    assert( Vec_IntEntryLast(vAig) < 2 );
    if ( Vec_IntSize(vAig) == 1 ) // const
        return Vec_IntEntry(vAig, 0);
    if ( Vec_IntSize(vAig) == 2 ) // variable
    {
        assert( Vec_IntEntry(vAig, 0) == 0 );
        assert( Vec_IntSize(vLeaves) == 1 );
        return Abc_LitNotCond( Vec_IntEntry(vLeaves, 0), Vec_IntEntry(vAig, 1) );
    }
    else
    {
        int nLeaves = Vec_IntSize(vLeaves);
        int i, iVar0, iVar1, iLit0, iLit1, iLit = 0;
        assert( Vec_IntSize(vAig) & 1 );
        Vec_IntForEachEntryDouble( vAig, iLit0, iLit1, i )
        {
            iVar0 = Abc_Lit2Var( iLit0 );
            iVar1 = Abc_Lit2Var( iLit1 );
            iLit0 = Abc_LitNotCond( iVar0 < nLeaves ? Vec_IntEntry(vLeaves, iVar0) : Vec_IntEntry(vAig, iVar0 - nLeaves), Abc_LitIsCompl(iLit0) );
            iLit1 = Abc_LitNotCond( iVar1 < nLeaves ? Vec_IntEntry(vLeaves, iVar1) : Vec_IntEntry(vAig, iVar1 - nLeaves), Abc_LitIsCompl(iLit1) );
            if ( fHash )
                iLit = Gia_ManHashAnd( pNew, iLit0, iLit1 );
            else if ( iLit0 == iLit1 )
                iLit = iLit0;
            else
                iLit = Gia_ManAppendAnd( pNew, iLit0, iLit1 );
            assert( (i & 1) == 0 );
            Vec_IntWriteEntry( vAig, Abc_Lit2Var(i), iLit );  // overwriting entries
        }
        assert( i == Vec_IntSize(vAig) - 1 );
        iLit = Abc_LitNotCond( iLit, Vec_IntEntry(vAig, i) );
        Vec_IntClear( vAig ); // useless
        return iLit;
    }
}
938 939 940
int Gia_ManBuildFromMini( Gia_Man_t * pNew, If_Man_t * pIfMan, If_Cut_t * pCut, Vec_Int_t * vLeaves, Vec_Int_t * vAig, int fHash, int fUseDsd )
{
    if ( fUseDsd )
941
        If_CutDsdBalanceEval( pIfMan, pCut, vAig );
942
    else
943
        If_CutSopBalanceEval( pIfMan, pCut, vAig );
944 945
    return Gia_ManBuildFromMiniInt( pNew, vLeaves, vAig, fHash );
}
946 947 948

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

949
  Synopsis    [Converts IF into GIA manager.]
950 951 952 953 954 955 956 957

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
958 959 960 961 962 963 964 965 966 967 968 969
int Gia_ManFromIfAig_rec( Gia_Man_t * pNew, If_Man_t * pIfMan, If_Obj_t * pIfObj )
{
    int iLit0, iLit1;
    if ( pIfObj->iCopy )
        return pIfObj->iCopy;
    iLit0 = Gia_ManFromIfAig_rec( pNew, pIfMan, pIfObj->pFanin0 );
    iLit1 = Gia_ManFromIfAig_rec( pNew, pIfMan, pIfObj->pFanin1 );
    iLit0 = Abc_LitNotCond( iLit0, pIfObj->fCompl0 );
    iLit1 = Abc_LitNotCond( iLit1, pIfObj->fCompl1 );
    pIfObj->iCopy = Gia_ManHashAnd( pNew, iLit0, iLit1 );
    return pIfObj->iCopy;
}
970
Gia_Man_t * Gia_ManFromIfAig( If_Man_t * pIfMan )
971
{
972
    int fHash = 0;
973
    Gia_Man_t * pNew, * pTemp;
974 975 976
    If_Obj_t * pIfObj, * pIfLeaf;
    If_Cut_t * pCutBest;
    Vec_Int_t * vLeaves;
977
    Vec_Int_t * vAig;
978
    int i, k;
979
    assert( pIfMan->pPars->pLutStruct == NULL );
980
    assert( pIfMan->pPars->fDelayOpt || pIfMan->pPars->fDsdBalance || pIfMan->pPars->fUserRecLib || pIfMan->pPars->fUserSesLib );
981 982 983 984
    // create new manager
    pNew = Gia_ManStart( If_ManObjNum(pIfMan) );
    Gia_ManHashAlloc( pNew );
    // iterate through nodes used in the mapping
985
    vAig = Vec_IntAlloc( 1 << 16 );
986
    vLeaves = Vec_IntAlloc( 16 );
987 988
//    If_ManForEachObj( pIfMan, pIfObj, i )
//        pIfObj->iCopy = 0;
989
    If_ManForEachObj( pIfMan, pIfObj, i )
990
    {
991
        if ( pIfObj->nRefs == 0 && !If_ObjIsTerm(pIfObj) )
992
            continue;
993 994 995
        if ( If_ObjIsAnd(pIfObj) )
        {
            pCutBest = If_ObjCutBest( pIfObj );
996 997 998 999 1000 1001
            // if the cut does not offer delay improvement
//            if ( (int)pIfObj->Level <= (int)pCutBest->Delay )
//            {
//                Gia_ManFromIfAig_rec( pNew, pIfMan, pIfObj );
//                continue;
//            }
1002 1003 1004 1005 1006
            // collect leaves of the best cut
            Vec_IntClear( vLeaves );
            If_CutForEachLeaf( pIfMan, pCutBest, pIfLeaf, k )
                Vec_IntPush( vLeaves, pIfLeaf->iCopy );
            // get the functionality
1007
            if ( pIfMan->pPars->fDelayOpt )
1008
                pIfObj->iCopy = Gia_ManBuildFromMini( pNew, pIfMan, pCutBest, vLeaves, vAig, fHash, 0 );
1009
            else if ( pIfMan->pPars->fDsdBalance )
1010
                pIfObj->iCopy = Gia_ManBuildFromMini( pNew, pIfMan, pCutBest, vLeaves, vAig, fHash, 1 );
1011
            else if ( pIfMan->pPars->fUserRecLib )
1012
                pIfObj->iCopy = Abc_RecToGia3( pNew, pIfMan, pCutBest, vLeaves, fHash );
1013
            else assert( 0 );
1014 1015 1016 1017 1018 1019 1020 1021
        }
        else if ( If_ObjIsCi(pIfObj) )
            pIfObj->iCopy = Gia_ManAppendCi(pNew);
        else if ( If_ObjIsCo(pIfObj) )
            pIfObj->iCopy = Gia_ManAppendCo( pNew, Abc_LitNotCond(If_ObjFanin0(pIfObj)->iCopy, If_ObjFaninC0(pIfObj)) );
        else if ( If_ObjIsConst1(pIfObj) )
            pIfObj->iCopy = 1;
        else assert( 0 );
1022
    }
1023
    Vec_IntFree( vAig );
1024
    Vec_IntFree( vLeaves );
1025 1026
    pNew = Gia_ManRehash( pTemp = pNew, 0 );
    Gia_ManStop( pTemp );
1027 1028 1029
    return pNew;
}

1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041

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

  Synopsis    [Write mapping for LUT with given fanins.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
1042
int Gia_ManFromIfLogicCreateLut( Gia_Man_t * pNew, word * pRes, Vec_Int_t * vLeaves, Vec_Int_t * vCover, Vec_Int_t * vMapping, Vec_Int_t * vMapping2 )
1043 1044 1045
{
    int i, iLit, iObjLit1;
    iObjLit1 = Kit_TruthToGia( pNew, (unsigned *)pRes, Vec_IntSize(vLeaves), vCover, vLeaves, 0 );
Alan Mishchenko committed
1046 1047 1048 1049 1050 1051
    // do not create LUT in the simple case
    if ( Abc_Lit2Var(iObjLit1) == 0 )
        return iObjLit1;
    Vec_IntForEachEntry( vLeaves, iLit, i )
        if ( Abc_Lit2Var(iObjLit1) == Abc_Lit2Var(iLit) )
            return iObjLit1;
1052 1053 1054
    // write mapping
    Vec_IntSetEntry( vMapping, Abc_Lit2Var(iObjLit1), Vec_IntSize(vMapping2) );
    Vec_IntPush( vMapping2, Vec_IntSize(vLeaves) );
1055 1056
//    Vec_IntForEachEntry( vLeaves, iLit, i )
//        assert( Abc_Lit2Var(iLit) < Abc_Lit2Var(iObjLit1) );
1057 1058 1059 1060 1061 1062
    Vec_IntForEachEntry( vLeaves, iLit, i )
        Vec_IntPush( vMapping2, Abc_Lit2Var(iLit)  );
    Vec_IntPush( vMapping2, Abc_Lit2Var(iObjLit1) );
    return iObjLit1;
}

1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083
/**Function*************************************************************

  Synopsis    [Write mapping for LUT with given fanins.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Gia_ManFromIfLogicCreateLutSpecial( Gia_Man_t * pNew, word * pRes, Vec_Int_t * vLeaves, Vec_Int_t * vLeavesTemp, Vec_Int_t * vCover, Vec_Int_t * vMapping, Vec_Int_t * vMapping2, Vec_Int_t * vPacking )
{
    word z = If_CutPerformDerive07( NULL, (unsigned *)pRes, Vec_IntSize(vLeaves), Vec_IntSize(vLeaves), NULL );
    word Truth;
    int i, iObjLit1, iObjLit2;
    // create first LUT
    Vec_IntClear( vLeavesTemp );
    for ( i = 0; i < 4; i++ )
    {
        int v = (int)((z >> (16+(i<<2))) & 7);
1084 1085
        if ( v == 6 && Vec_IntSize(vLeaves) == 5 )
            continue;
1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096
        Vec_IntPush( vLeavesTemp, Vec_IntEntry(vLeaves, v) );
    }
    Truth = (z & 0xffff);
    Truth |= (Truth << 16);
    Truth |= (Truth << 32);
    iObjLit1 = Gia_ManFromIfLogicCreateLut( pNew, &Truth, vLeavesTemp, vCover, vMapping, vMapping2 );
    // create second LUT
    Vec_IntClear( vLeavesTemp );
    for ( i = 0; i < 4; i++ )
    {
        int v =  (int)((z >> (48+(i<<2))) & 7);
1097 1098
        if ( v == 6 && Vec_IntSize(vLeaves) == 5 )
            continue;
1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114
        if ( v == 7 )
            Vec_IntPush( vLeavesTemp, iObjLit1 );
        else
            Vec_IntPush( vLeavesTemp, Vec_IntEntry(vLeaves, v) );
    }
    Truth = ((z >> 32) & 0xffff);
    Truth |= (Truth << 16);
    Truth |= (Truth << 32);
    iObjLit2 = Gia_ManFromIfLogicCreateLut( pNew, &Truth, vLeavesTemp, vCover, vMapping, vMapping2 );
    // write packing
    Vec_IntPush( vPacking, 2 );
    Vec_IntPush( vPacking, Abc_Lit2Var(iObjLit1) );
    Vec_IntPush( vPacking, Abc_Lit2Var(iObjLit2) );
    Vec_IntAddToEntry( vPacking, 0, 1 );
    return iObjLit2;
}
1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126

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

  Synopsis    [Write the node into a file.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
1127
int Gia_ManFromIfLogicNode( void * pIfMan, Gia_Man_t * pNew, int iObj, Vec_Int_t * vLeaves, Vec_Int_t * vLeavesTemp, 
1128
    word * pRes, char * pStr, Vec_Int_t * vCover, Vec_Int_t * vMapping, Vec_Int_t * vMapping2, Vec_Int_t * vPacking, int fCheck75, int fCheck44e )
1129 1130 1131
{
    int nLeaves = Vec_IntSize(vLeaves);
    int i, Length, nLutLeaf, nLutLeaf2, nLutRoot, iObjLit1, iObjLit2, iObjLit3;
1132
    // workaround for the special case
1133
    if ( fCheck75 )
1134
        pStr = "54";
1135 1136 1137 1138 1139 1140 1141 1142
    // perform special case matching for 44
    if ( fCheck44e )
    {
        if ( Vec_IntSize(vLeaves) <= 4 )
        {
            // create mapping
            iObjLit1 = Gia_ManFromIfLogicCreateLut( pNew, pRes, vLeaves, vCover, vMapping, vMapping2 );
            // write packing
1143
            if ( !Gia_ObjIsCi(Gia_ManObj(pNew, Abc_Lit2Var(iObjLit1))) && iObjLit1 > 1 )
1144 1145 1146 1147 1148 1149 1150 1151 1152
            {
                Vec_IntPush( vPacking, 1 );
                Vec_IntPush( vPacking, Abc_Lit2Var(iObjLit1) );
                Vec_IntAddToEntry( vPacking, 0, 1 );
            }
            return iObjLit1;
        }
        return Gia_ManFromIfLogicCreateLutSpecial( pNew, pRes, vLeaves, vLeavesTemp, vCover, vMapping, vMapping2, vPacking );
    }
1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180
    if ( ((If_Man_t *)pIfMan)->pPars->fLut6Filter && Vec_IntSize(vLeaves) == 6 )
    {
        extern word If_Dec6Perform( word t, int fDerive );
        extern void If_Dec6Verify( word t, word z );
        Vec_Int_t * vLeaves2 = Vec_IntAlloc( 4 );
        word t = pRes[0];
        word z = If_Dec6Perform( t, 1 );
        //If_DecPrintConfig( z );
        If_Dec6Verify( t, z );

        t = Abc_Tt6Stretch( z & 0xffff, 4 );
        Vec_IntClear( vLeaves2 );
        for ( i = 0; i < 4; i++ )
            Vec_IntPush( vLeaves2, Vec_IntEntry( vLeaves, (int)((z >> (16+i*4)) & 7) ) );
        iObjLit1 = Gia_ManFromIfLogicCreateLut( pNew, &t, vLeaves2, vCover, vMapping, vMapping2 );

        t = Abc_Tt6Stretch( (z >> 32) & 0xffff, 4 );
        Vec_IntClear( vLeaves2 );
        for ( i = 0; i < 4; i++ )
            if ( ((z >> (48+i*4)) & 7) == 7 )
                Vec_IntPush( vLeaves2, iObjLit1 );
            else
                Vec_IntPush( vLeaves2, Vec_IntEntry( vLeaves, (int)((z >> (48+i*4)) & 7) ) );
        iObjLit1 = Gia_ManFromIfLogicCreateLut( pNew, &t, vLeaves2, vCover, vMapping, vMapping2 );

        Vec_IntFree( vLeaves2 );
        return iObjLit1;
    }
1181 1182 1183
    // check if there is no LUT structures
    if ( pStr == NULL )
        return Gia_ManFromIfLogicCreateLut( pNew, pRes, vLeaves, vCover, vMapping, vMapping2 );
1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210
    // quit if parameters are wrong
    Length = strlen(pStr);
    if ( Length != 2 && Length != 3 )
    {
        printf( "Wrong LUT struct (%s)\n", pStr );
        return -1;
    }
    for ( i = 0; i < Length; i++ )
        if ( pStr[i] - '0' < 3 || pStr[i] - '0' > 6 )
        {
            printf( "The LUT size (%d) should belong to {3,4,5,6}.\n", pStr[i] - '0' );
            return -1;
        }

    nLutLeaf  =                   pStr[0] - '0';
    nLutLeaf2 = ( Length == 3 ) ? pStr[1] - '0' : 0;
    nLutRoot  =                   pStr[Length-1] - '0';
    if ( nLeaves > nLutLeaf - 1 + (nLutLeaf2 ? nLutLeaf2 - 1 : 0) + nLutRoot )
    {
        printf( "The node size (%d) is too large for the LUT structure %s.\n", nLeaves, pStr );
        return -1;
    }

    // consider easy case
    if ( nLeaves <= Abc_MaxInt( nLutLeaf2, Abc_MaxInt(nLutLeaf, nLutRoot) ) )
    {
        // create mapping
1211
        iObjLit1 = Gia_ManFromIfLogicCreateLut( pNew, pRes, vLeaves, vCover, vMapping, vMapping2 );
1212
        // write packing
1213
        if ( !Gia_ObjIsCi(Gia_ManObj(pNew, Abc_Lit2Var(iObjLit1))) && iObjLit1 > 1 )
Alan Mishchenko committed
1214 1215 1216 1217 1218
        {
            Vec_IntPush( vPacking, 1 );
            Vec_IntPush( vPacking, Abc_Lit2Var(iObjLit1) );
            Vec_IntAddToEntry( vPacking, 0, 1 );
        }
1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255
        return iObjLit1;
    }
    else
    {
        extern int If_CluMinimumBase( word * t, int * pSupp, int nVarsAll, int * pnVars );

        static word TruthStore[16][1<<10] = {{0}}, * pTruths[16];
        word Func0, Func1, Func2;
        char pLut0[32], pLut1[32], pLut2[32] = {0};

        if ( TruthStore[0][0] == 0 )
        {
            static word Truth6[6] = {
                0xAAAAAAAAAAAAAAAA,
                0xCCCCCCCCCCCCCCCC,
                0xF0F0F0F0F0F0F0F0,
                0xFF00FF00FF00FF00,
                0xFFFF0000FFFF0000,
                0xFFFFFFFF00000000
            };
            int nVarsMax = 16;
            int nWordsMax = (1 << 10);
            int i, k;
            assert( nVarsMax <= 16 );
            for ( i = 0; i < nVarsMax; i++ )
                pTruths[i] = TruthStore[i];
            for ( i = 0; i < 6; i++ )
                for ( k = 0; k < nWordsMax; k++ )
                    pTruths[i][k] = Truth6[i];
            for ( i = 6; i < nVarsMax; i++ )
                for ( k = 0; k < nWordsMax; k++ )
                    pTruths[i][k] = ((k >> (i-6)) & 1) ? ~(word)0 : 0;
        }
        // derive truth table
        if ( Kit_TruthIsConst0((unsigned *)pRes, nLeaves) || Kit_TruthIsConst1((unsigned *)pRes, nLeaves) )
        {
//            fprintf( pFile, ".names %s\n %d\n", Abc_ObjName(Abc_ObjFanout0(pObj)), Kit_TruthIsConst1((unsigned *)pRes, nLeaves) );
1256 1257 1258 1259 1260 1261 1262 1263 1264
            iObjLit1 = Abc_LitNotCond( 0, Kit_TruthIsConst1((unsigned *)pRes, nLeaves) );
            // write mapping
            if ( Vec_IntEntry(vMapping, 0) == 0 )
            {
                Vec_IntSetEntry( vMapping, 0, Vec_IntSize(vMapping2) );
                Vec_IntPush( vMapping2, 0 );
                Vec_IntPush( vMapping2, 0 );
            }
            return iObjLit1;
1265
        }
Alan Mishchenko committed
1266 1267 1268 1269 1270 1271 1272 1273
        // check for elementary truth table
        for ( i = 0; i < nLeaves; i++ )
        {
            if ( Kit_TruthIsEqual((unsigned *)pRes, (unsigned *)pTruths[i], nLeaves) )
                return Vec_IntEntry(vLeaves, i);
            if ( Kit_TruthIsOpposite((unsigned *)pRes, (unsigned *)pTruths[i], nLeaves) )
                return Abc_LitNot(Vec_IntEntry(vLeaves, i));
        }
1274 1275

        // perform decomposition
1276
        if ( fCheck75 ) 
1277
        {
1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296
//            if ( nLeaves < 8 && If_CutPerformCheck16( p, (unsigned *)pTruth, nVars, nLeaves, "44" ) )
            if ( nLeaves < 8 && If_CluCheckExt( NULL, pRes, nLeaves, 4, 4, pLut0, pLut1, &Func0, &Func1 ) )
            {
                nLutLeaf = 4;
                nLutRoot = 4;
            }
//            if ( If_CutPerformCheck45( p, (unsigned *)pTruth, nVars, nLeaves, pStr ) )
            else if ( If_CluCheckExt( NULL, pRes, nLeaves, 5, 4, pLut0, pLut1, &Func0, &Func1 ) )
            {
                nLutLeaf = 5;
                nLutRoot = 4;
            }
//            if ( If_CutPerformCheck54( p, (unsigned *)pTruth, nVars, nLeaves, pStr ) )
            else if ( If_CluCheckExt( NULL, pRes, nLeaves, 4, 5, pLut0, pLut1, &Func0, &Func1 ) )
            {
                nLutLeaf = 4;
                nLutRoot = 5;
            }
            else
1297 1298 1299 1300 1301 1302 1303 1304 1305
            {
                Extra_PrintHex( stdout, (unsigned *)pRes, nLeaves );  printf( "    " );
                Kit_DsdPrintFromTruth( (unsigned*)pRes, nLeaves );  printf( "\n" );
                printf( "Node %d is not decomposable. Deriving LUT structures has failed.\n", iObj );
                return -1;
            }
        }
        else
        {
1306
            if ( Length == 2 )
1307
            {
1308 1309 1310 1311 1312 1313 1314 1315 1316 1317
                if ( !If_CluCheckExt( NULL, pRes, nLeaves, nLutLeaf, nLutRoot, pLut0, pLut1, &Func0, &Func1 ) )
                {
                    Extra_PrintHex( stdout, (unsigned *)pRes, nLeaves );  printf( "    " );
                    Kit_DsdPrintFromTruth( (unsigned*)pRes, nLeaves );  printf( "\n" );
                    printf( "Node %d is not decomposable. Deriving LUT structures has failed.\n", iObj );
                    return -1;
                }
            }
            else
            {
1318
                if ( !If_CluCheckExt3( pIfMan, pRes, nLeaves, nLutLeaf, nLutLeaf2, nLutRoot, pLut0, pLut1, pLut2, &Func0, &Func1, &Func2 ) )
1319 1320 1321 1322 1323 1324
                {
                    Extra_PrintHex( stdout, (unsigned *)pRes, nLeaves );  printf( "    " );
                    Kit_DsdPrintFromTruth( (unsigned*)pRes, nLeaves );  printf( "\n" );
                    printf( "Node %d is not decomposable. Deriving LUT structures has failed.\n", iObj );
                    return -1;
                }
1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340
            }
        }

/*
        // write leaf node
        Id = Abc2_NtkAllocObj( pNew, pLut1[0], Abc2_ObjType(pObj) );
        iObjLit1 = Abc_Var2Lit( Id, 0 );
        pObjNew = Abc2_NtkObj( pNew, Id );
        for ( i = 0; i < pLut1[0]; i++ )
            Abc2_ObjSetFaninLit( pObjNew, i, Abc2_ObjFaninCopy(pObj, pLut1[2+i]) );
        Abc2_ObjSetTruth( pObjNew, Func1 );
*/
        // write leaf node
        Vec_IntClear( vLeavesTemp );
        for ( i = 0; i < pLut1[0]; i++ )
            Vec_IntPush( vLeavesTemp, Vec_IntEntry(vLeaves, pLut1[2+i]) );
1341
        iObjLit1 = Gia_ManFromIfLogicCreateLut( pNew, &Func1, vLeavesTemp, vCover, vMapping, vMapping2 );
1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363

        if ( Length == 3 && pLut2[0] > 0 )
        {
        /*
            Id = Abc2_NtkAllocObj( pNew, pLut2[0], Abc2_ObjType(pObj) );
            iObjLit2 = Abc_Var2Lit( Id, 0 );
            pObjNew = Abc2_NtkObj( pNew, Id );
            for ( i = 0; i < pLut2[0]; i++ )
                if ( pLut2[2+i] == nLeaves )
                    Abc2_ObjSetFaninLit( pObjNew, i, iObjLit1 );
                else
                    Abc2_ObjSetFaninLit( pObjNew, i, Abc2_ObjFaninCopy(pObj, pLut2[2+i]) );
            Abc2_ObjSetTruth( pObjNew, Func2 );
        */

            // write leaf node
            Vec_IntClear( vLeavesTemp );
            for ( i = 0; i < pLut2[0]; i++ )
                if ( pLut2[2+i] == nLeaves )
                    Vec_IntPush( vLeavesTemp, iObjLit1 );
                else
                    Vec_IntPush( vLeavesTemp, Vec_IntEntry(vLeaves, pLut2[2+i]) );
1364
            iObjLit2 = Gia_ManFromIfLogicCreateLut( pNew, &Func2, vLeavesTemp, vCover, vMapping, vMapping2 );
1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375

            // write packing
            Vec_IntPush( vPacking, 3 );
            Vec_IntPush( vPacking, Abc_Lit2Var(iObjLit1) );
            Vec_IntPush( vPacking, Abc_Lit2Var(iObjLit2) );
        }
        else
        {
            // write packing
            Vec_IntPush( vPacking, 2 );
            Vec_IntPush( vPacking, Abc_Lit2Var(iObjLit1) );
Alan Mishchenko committed
1376
            iObjLit2 = -1;
1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401
        }
/*
        // write root node
        Id = Abc2_NtkAllocObj( pNew, pLut0[0], Abc2_ObjType(pObj) );
        iObjLit3 = Abc_Var2Lit( Id, 0 );
        pObjNew = Abc2_NtkObj( pNew, Id );
        for ( i = 0; i < pLut0[0]; i++ )
            if ( pLut0[2+i] == nLeaves )
                Abc2_ObjSetFaninLit( pObjNew, i, iObjLit1 );
            else if ( pLut0[2+i] == nLeaves+1 )
                Abc2_ObjSetFaninLit( pObjNew, i, iObjLit2 );
            else
                Abc2_ObjSetFaninLit( pObjNew, i, Abc2_ObjFaninCopy(pObj, pLut0[2+i]) );
        Abc2_ObjSetTruth( pObjNew, Func0 );
        Abc2_ObjSetCopy( pObj, iObjLit3 );
*/
        // write root node
        Vec_IntClear( vLeavesTemp );
        for ( i = 0; i < pLut0[0]; i++ )
            if ( pLut0[2+i] == nLeaves )
                Vec_IntPush( vLeavesTemp, iObjLit1 );
            else if ( pLut0[2+i] == nLeaves+1 )
                Vec_IntPush( vLeavesTemp, iObjLit2 );
            else
                Vec_IntPush( vLeavesTemp, Vec_IntEntry(vLeaves, pLut0[2+i]) );
1402
        iObjLit3 = Gia_ManFromIfLogicCreateLut( pNew, &Func0, vLeavesTemp, vCover, vMapping, vMapping2 );
1403 1404 1405 1406 1407 1408 1409 1410

        // write packing
        Vec_IntPush( vPacking, Abc_Lit2Var(iObjLit3) );
        Vec_IntAddToEntry( vPacking, 0, 1 );
    }
    return iObjLit3;
}

1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485
/**Function*************************************************************

  Synopsis    [Recursively derives the local AIG for the cut.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Gia_ManNodeIfToGia_rec( Gia_Man_t * pNew, If_Man_t * pIfMan, If_Obj_t * pIfObj, Vec_Ptr_t * vVisited, int fHash )
{
    If_Cut_t * pCut;
    If_Obj_t * pTemp;
    int iFunc, iFunc0, iFunc1;
    // get the best cut
    pCut = If_ObjCutBest(pIfObj);
    // if the cut is visited, return the result
    if ( If_CutDataInt(pCut) )
        return If_CutDataInt(pCut);
    // mark the node as visited
    Vec_PtrPush( vVisited, pCut );
    // insert the worst case
    If_CutSetDataInt( pCut, ~0 );
    // skip in case of primary input
    if ( If_ObjIsCi(pIfObj) )
        return If_CutDataInt(pCut);
    // compute the functions of the children
    for ( pTemp = pIfObj; pTemp; pTemp = pTemp->pEquiv )
    {
        iFunc0 = Gia_ManNodeIfToGia_rec( pNew, pIfMan, pTemp->pFanin0, vVisited, fHash );
        if ( iFunc0 == ~0 )
            continue;
        iFunc1 = Gia_ManNodeIfToGia_rec( pNew, pIfMan, pTemp->pFanin1, vVisited, fHash );
        if ( iFunc1 == ~0 )
            continue;
        // both branches are solved
        if ( fHash )
            iFunc = Gia_ManHashAnd( pNew, Abc_LitNotCond(iFunc0, pTemp->fCompl0), Abc_LitNotCond(iFunc1, pTemp->fCompl1) ); 
        else
            iFunc = Gia_ManAppendAnd( pNew, Abc_LitNotCond(iFunc0, pTemp->fCompl0), Abc_LitNotCond(iFunc1, pTemp->fCompl1) ); 
        if ( pTemp->fPhase != pIfObj->fPhase )
            iFunc = Abc_LitNot(iFunc);
        If_CutSetDataInt( pCut, iFunc );
        break;
    }
    return If_CutDataInt(pCut);
}
int Gia_ManNodeIfToGia( Gia_Man_t * pNew, If_Man_t * pIfMan, If_Obj_t * pIfObj, Vec_Int_t * vLeaves, int fHash )
{
    If_Cut_t * pCut;
    If_Obj_t * pLeaf;
    int i, iRes;
    // get the best cut
    pCut = If_ObjCutBest(pIfObj);
    assert( pCut->nLeaves > 1 );
    // set the leaf variables
    If_CutForEachLeaf( pIfMan, pCut, pLeaf, i )
        If_CutSetDataInt( If_ObjCutBest(pLeaf), Vec_IntEntry(vLeaves, i) );
    // recursively compute the function while collecting visited cuts
    Vec_PtrClear( pIfMan->vTemp );
    iRes = Gia_ManNodeIfToGia_rec( pNew, pIfMan, pIfObj, pIfMan->vTemp, fHash ); 
    if ( iRes == ~0 )
    {
        Abc_Print( -1, "Gia_ManNodeIfToGia(): Computing local AIG has failed.\n" );
        return ~0;
    }
    // clean the cuts
    If_CutForEachLeaf( pIfMan, pCut, pLeaf, i )
        If_CutSetDataInt( If_ObjCutBest(pLeaf), 0 );
    Vec_PtrForEachEntry( If_Cut_t *, pIfMan->vTemp, pCut, i )
        If_CutSetDataInt( pCut, 0 );
    return iRes;
}
1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497

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

  Synopsis    [Converts IF into GIA manager.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
1498
int Gia_ManFromIfLogicFindLut( If_Man_t * pIfMan, Gia_Man_t * pNew, If_Cut_t * pCutBest, sat_solver * pSat, Vec_Int_t * vLeaves, Vec_Int_t * vLits, Vec_Int_t * vCover, Vec_Int_t * vMapping, Vec_Int_t * vMapping2, Vec_Int_t * vPacking )
1499
{
1500 1501 1502 1503 1504 1505 1506
    word uBound, uFree;
    int nLutSize = (int)(pIfMan->pPars->pLutStruct[0] - '0');
    int nVarsF = 0, pVarsF[IF_MAX_FUNC_LUTSIZE];
    int nVarsB = 0, pVarsB[IF_MAX_FUNC_LUTSIZE];
    int nVarsS = 0, pVarsS[IF_MAX_FUNC_LUTSIZE];
    unsigned uSetNew, uSetOld;
    int RetValue, RetValue2, k;
1507
    char * pPerm;
1508 1509 1510 1511
    if ( Vec_IntSize(vLeaves) <= nLutSize )
    {
        RetValue = Gia_ManFromIfLogicCreateLut( pNew, If_CutTruthW(pIfMan, pCutBest), vLeaves, vCover, vMapping, vMapping2 );
        // write packing
1512
        if ( !Gia_ObjIsCi(Gia_ManObj(pNew, Abc_Lit2Var(RetValue))) && RetValue > 1 )
1513 1514 1515 1516 1517 1518 1519
        {
            Vec_IntPush( vPacking, 1 );
            Vec_IntPush( vPacking, Abc_Lit2Var(RetValue) );
            Vec_IntAddToEntry( vPacking, 0, 1 );
        }
        return RetValue;
    }
1520
    assert( If_DsdManSuppSize(pIfMan->pIfDsdMan, If_CutDsdLit(pIfMan, pCutBest)) == (int)pCutBest->nLeaves );
1521
    // find the bound set
1522 1523 1524 1525
    if ( pIfMan->pPars->fDelayOptLut )
        uSetOld = pCutBest->uMaskFunc;
    else
        uSetOld = If_DsdManCheckXY( pIfMan->pIfDsdMan, If_CutDsdLit(pIfMan, pCutBest), nLutSize, 1, 0, 1, 0 );
1526 1527
    // remap bound set
    uSetNew = 0;
1528
    pPerm = If_CutDsdPerm( pIfMan, pCutBest );
1529 1530
    for ( k = 0; k < If_CutLeaveNum(pCutBest); k++ )
    {
1531
        int iVar = Abc_Lit2Var((int)pPerm[k]);
1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574
        int Value = ((uSetOld >> (k << 1)) & 3);
        if ( Value == 1 )
            uSetNew |= (1 << (2*iVar));
        else if ( Value == 3 )
            uSetNew |= (3 << (2*iVar));
        else assert( Value == 0 );
    }
    RetValue = If_ManSatCheckXY( pSat, nLutSize, If_CutTruthW(pIfMan, pCutBest), pCutBest->nLeaves, uSetNew, &uBound, &uFree, vLits );
    assert( RetValue );
    // collect variables
    for ( k = 0; k < If_CutLeaveNum(pCutBest); k++ )
    {
        int Value = ((uSetNew >> (k << 1)) & 3);
        if ( Value == 0 )
            pVarsF[nVarsF++] = k;
        else if ( Value == 1 )
            pVarsB[nVarsB++] = k;
        else if ( Value == 3 )
            pVarsS[nVarsS++] = k;
        else assert( Value == 0 );
    }
    // collect bound set variables
    Vec_IntClear( vLits );
    for ( k = 0; k < nVarsS; k++ )
        Vec_IntPush( vLits, Vec_IntEntry(vLeaves, pVarsS[k]) );
    for ( k = 0; k < nVarsB; k++ )
        Vec_IntPush( vLits, Vec_IntEntry(vLeaves, pVarsB[k]) );
    RetValue = Gia_ManFromIfLogicCreateLut( pNew, &uBound, vLits, vCover, vMapping, vMapping2 );
    // collecct free set variables
    Vec_IntClear( vLits );
    Vec_IntPush( vLits, RetValue );
    for ( k = 0; k < nVarsS; k++ )
        Vec_IntPush( vLits, Vec_IntEntry(vLeaves, pVarsS[k]) );
    for ( k = 0; k < nVarsF; k++ )
        Vec_IntPush( vLits, Vec_IntEntry(vLeaves, pVarsF[k]) );
    // add packing
    RetValue2 = Gia_ManFromIfLogicCreateLut( pNew, &uFree, vLits, vCover, vMapping, vMapping2 );
    // write packing
    Vec_IntPush( vPacking, 2 );
    Vec_IntPush( vPacking, Abc_Lit2Var(RetValue) );
    Vec_IntPush( vPacking, Abc_Lit2Var(RetValue2) );
    Vec_IntAddToEntry( vPacking, 0, 1 );
    return RetValue2;
1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587
}

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

  Synopsis    [Converts IF into GIA manager.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
1588
void Gia_ManFromIfGetConfig( Vec_Int_t * vConfigs, If_Man_t * pIfMan, If_Cut_t * pCutBest, int iLit, Vec_Str_t * vConfigsStr )
1589
{
Alan Mishchenko committed
1590
    If_Obj_t * pIfObj = NULL;
1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626
    word * pPerm = If_DsdManGetFuncConfig( pIfMan->pIfDsdMan, If_CutDsdLit(pIfMan, pCutBest) ); // cell input -> DSD input
    char * pCutPerm = If_CutDsdPerm( pIfMan, pCutBest ); // DSD input -> cut input
    word * pArray;  int v, i, Lit, Var;
    int nVarNum = If_DsdManVarNum(pIfMan->pIfDsdMan);
    int nTtBitNum = If_DsdManTtBitNum(pIfMan->pIfDsdMan);
    int nPermBitNum = If_DsdManPermBitNum(pIfMan->pIfDsdMan);
    int nPermBitOne = nPermBitNum / nVarNum;
    // prepare storage
    int nIntNum = Vec_IntEntry( vConfigs, 1 );
    for ( i = 0; i < nIntNum; i++ )
        Vec_IntPush( vConfigs, 0 );
    pArray = (word *)Vec_IntEntryP( vConfigs, Vec_IntSize(vConfigs) - nIntNum );
    assert( nPermBitNum % nVarNum == 0 );
    // set truth table bits
    for ( i = 0; i < nTtBitNum; i++ )
        if ( Abc_TtGetBit(pPerm + 1, i) )
            Abc_TtSetBit( pArray, i );
    // set permutation bits
    for ( v = 0; v < nVarNum; v++ )
    {
        // get DSD variable
        Var = ((pPerm[0] >> (v * 4)) & 0xF);
        assert( Var < (int)pCutBest->nLeaves );
        // get AIG literal
        Lit = (int)pCutPerm[Var];
        assert( Abc_Lit2Var(Lit) < (int)pCutBest->nLeaves );
        // complement if polarity has changed
        pIfObj = If_ManObj( pIfMan, pCutBest->pLeaves[Abc_Lit2Var(Lit)] );
        Lit = Abc_LitNotCond( Lit, Abc_LitIsCompl(pIfObj->iCopy) );
        // create config literal
        for ( i = 0; i < nPermBitOne; i++ )
            if ( (Lit >> i) & 1 )
                Abc_TtSetBit( pArray, nTtBitNum + v * nPermBitOne + i );
    }
    // remember complementation
    assert( nTtBitNum + nPermBitNum < 32 * nIntNum );
1627
    if ( Abc_LitIsCompl(If_CutDsdLit(pIfMan, pCutBest)) ^ pCutBest->fCompl ^ Abc_LitIsCompl(iLit) )
1628 1629 1630
        Abc_TtSetBit( pArray, nTtBitNum + nPermBitNum );
    // update count
    Vec_IntAddToEntry( vConfigs, 0, 1 );
1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653
    // write configs
    if ( vConfigsStr )
    {
        Vec_StrPrintF( vConfigsStr, "%d", Abc_Lit2Var(iLit) );
        Vec_StrPush( vConfigsStr, ' ' );
        for ( i = 0; i < nTtBitNum; i++ )
            Vec_StrPush( vConfigsStr, (char)(Abc_TtGetBit(pArray, i) ? '1' : '0') );
        Vec_StrPush( vConfigsStr, ' ' );
        Vec_StrPush( vConfigsStr, ' ' );
        for ( v = 0; v < nVarNum; v++ )
        {
            for ( i = 0; i < nPermBitOne; i++ )
            {
                Vec_StrPush( vConfigsStr, (char)(Abc_TtGetBit(pArray, nTtBitNum + v * nPermBitOne + i) ? '1' : '0') );
                if ( i == 0 ) 
                    Vec_StrPush( vConfigsStr, ' ' );
            }
            Vec_StrPush( vConfigsStr, ' ' );
            Vec_StrPush( vConfigsStr, ' ' );
        }
        Vec_StrPush( vConfigsStr, (char)(Abc_TtGetBit(pArray, nTtBitNum + nPermBitNum) ? '1' : '0') );
        Vec_StrPush( vConfigsStr, '\n' );
    }
1654 1655
}
int Gia_ManFromIfLogicFindCell( If_Man_t * pIfMan, Gia_Man_t * pNew, Gia_Man_t * pTemp, If_Cut_t * pCutBest, Ifn_Ntk_t * pNtkCell, int nLutMax, Vec_Int_t * vLeaves, Vec_Int_t * vLits, Vec_Int_t * vCover, Vec_Int_t * vMapping, Vec_Int_t * vMapping2, Vec_Int_t * vConfigs )
1656
{
1657
    int iLit;
1658
    assert( 0 );
1659 1660 1661 1662 1663
    if ( Vec_IntSize(vLeaves) <= nLutMax )
        iLit = Gia_ManFromIfLogicCreateLut( pNew, If_CutTruthW(pIfMan, pCutBest), vLeaves, vCover, vMapping, vMapping2 );
    else
    {
        Gia_Obj_t * pObj;
1664
        int i, Id, iLitTemp;
1665
        // extract variable permutation
Alan Mishchenko committed
1666
        //char * pCutPerm = If_CutDsdPerm( pIfMan, pCutBest ); // DSD input -> cut input
1667
        word * pPerm = If_DsdManGetFuncConfig( pIfMan->pIfDsdMan, If_CutDsdLit(pIfMan, pCutBest) ); // cell input -> DSD input
Alan Mishchenko committed
1668
        //int nBits = If_DsdManTtBitNum( pIfMan->pIfDsdMan );
1669
        // use config bits to generate the network
1670
        iLit = If_ManSatDeriveGiaFromBits( pTemp, pNtkCell, pPerm + 1, vLeaves, vCover );
1671 1672 1673
        // copy GIA back into the manager
        Vec_IntFillExtra( &pTemp->vCopies, Gia_ManObjNum(pTemp), -1 );
        Gia_ObjSetCopyArray( pTemp, 0, 0 );
1674 1675
        Vec_IntForEachEntry( vLeaves, iLitTemp, i )
            Gia_ObjSetCopyArray( pTemp, Gia_ManCiIdToId(pTemp, i), iLitTemp );
1676 1677 1678
        // collect nodes
        Gia_ManIncrementTravId( pTemp );
        Id = Abc_Lit2Var( iLit );
1679
        Gia_ManCollectAnds( pTemp, &Id, 1, vCover, NULL );
1680 1681 1682
        Vec_IntPrint( vCover );
        Gia_ManForEachObjVec( vCover, pTemp, pObj, i )
            Gia_ObjPrint( pTemp, pObj );
1683 1684 1685 1686 1687 1688 1689 1690 1691
        // copy GIA
        Gia_ManForEachObjVec( vCover, pTemp, pObj, i )
        {
            iLit = Gia_ManAppendAnd( pNew, Gia_ObjFanin0CopyArray(pTemp, pObj), Gia_ObjFanin1CopyArray(pTemp, pObj) );
            Gia_ObjSetCopyArray( pTemp, Gia_ObjId(pTemp, pObj), iLit );
        }
        iLit = Abc_LitNotCond( Gia_ObjCopyArray(pTemp, Id), Abc_LitIsCompl(iLit) );
    }
    // write packing
1692 1693
//    if ( vConfigs && !Gia_ObjIsCi(Gia_ManObj(pNew, Abc_Lit2Var(iLit))) && iLit > 1 )
//        Gia_ManFromIfGetConfig( vConfigs, pIfMan, pCutBest );
1694
    return iLit;
1695 1696
}

1697

1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708
/**Function*************************************************************

  Synopsis    [Converts IF into GIA manager.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840
int Gia_ManFromIfLogicCofVars( Gia_Man_t * pNew, If_Man_t * pIfMan, If_Cut_t * pCutBest, Vec_Int_t * vLeaves, Vec_Int_t * vLeaves2, Vec_Int_t * vCover, Vec_Int_t * vMapping, Vec_Int_t * vMapping2 )
{
    word pTruthCof[128], * pTruth = If_CutTruthW(pIfMan, pCutBest);
    int pVarsNew[16], nVarsNew, iLitCofs[3]; 
    int nLeaves = pCutBest->nLeaves;
    int nWords  = Abc_Truth6WordNum(nLeaves);
    int truthId = Abc_Lit2Var(pCutBest->iCutFunc);
    int c, iVar = Vec_StrEntry(pIfMan->vTtVars[nLeaves], truthId), iTemp, iTopLit;
    int k, RetValue = -1;
    assert( iVar >= 0 && iVar < nLeaves && pIfMan->pPars->nLutSize <= 13 );
    for ( c = 0; c < 2; c++ )
    {
        for ( k = 0; k < nLeaves; k++ )
            pVarsNew[k] = k;
        if ( c )
            Abc_TtCofactor1p( pTruthCof, pTruth, nWords, iVar );
        else
            Abc_TtCofactor0p( pTruthCof, pTruth, nWords, iVar );
        nVarsNew = Abc_TtMinBase( pTruthCof, pVarsNew, pCutBest->nLeaves, Abc_MaxInt(6, pCutBest->nLeaves) );
        // derive LUT
        Vec_IntClear( vLeaves2 );
        for ( k = 0; k < nVarsNew; k++ )
            Vec_IntPush( vLeaves2, Vec_IntEntry(vLeaves, pVarsNew[k]) );
        iLitCofs[c] = Kit_TruthToGia( pNew, (unsigned *)pTruthCof, nVarsNew, vCover, vLeaves2, 0 );
        if ( nVarsNew < 2 )
            continue;
        // create mapping
        assert( Gia_ObjIsAnd(Gia_ManObj(pNew, Abc_Lit2Var(iLitCofs[c]))) );
        Vec_IntSetEntry( vMapping, Abc_Lit2Var(iLitCofs[c]), Vec_IntSize(vMapping2) );
        Vec_IntPush( vMapping2, Vec_IntSize(vLeaves2) );
        Vec_IntForEachEntry( vLeaves2, iTemp, k )
            Vec_IntPush( vMapping2, Abc_Lit2Var(iTemp) );
        Vec_IntPush( vMapping2, Abc_Lit2Var(iLitCofs[c]) );
    }
    iLitCofs[2]  = Vec_IntEntry(vLeaves, iVar);
    // derive MUX
    if ( iLitCofs[0] > 1 && iLitCofs[1] > 1 )
    {
        pTruthCof[0] = ABC_CONST(0xCACACACACACACACA);
        Vec_IntClear( vLeaves2 );
        Vec_IntPush( vLeaves2, iLitCofs[0] );
        Vec_IntPush( vLeaves2, iLitCofs[1] );
        Vec_IntPush( vLeaves2, iLitCofs[2] );
        RetValue = Kit_TruthToGia( pNew, (unsigned *)pTruthCof, Vec_IntSize(vLeaves2), vCover, vLeaves2, 0 );
        iTopLit = RetValue;
    }
    else
    {
        assert( iLitCofs[0] > 1 || iLitCofs[1] > 1 );
        // collect leaves
        Vec_IntClear( vLeaves2 );
        for ( k = 0; k < 3; k++ )
            if ( iLitCofs[k] > 1 )
                Vec_IntPush( vLeaves2, iLitCofs[k] );
        assert( Vec_IntSize(vLeaves2) == 2 );
        // consider three possibilities
        if ( iLitCofs[0] == 0 )
            RetValue = Gia_ManAppendAnd( pNew, iLitCofs[2], iLitCofs[1] );
        else if ( iLitCofs[0] == 1 )
            RetValue = Gia_ManAppendOr( pNew, Abc_LitNot(iLitCofs[2]), iLitCofs[1] );
        else if ( iLitCofs[1] == 0 )
            RetValue = Gia_ManAppendAnd( pNew, Abc_LitNot(iLitCofs[2]), iLitCofs[0] );
        else if ( iLitCofs[1] == 1 )
            RetValue = Gia_ManAppendOr( pNew, iLitCofs[2], iLitCofs[0] );
        else assert( 0 );
        iTopLit = iLitCofs[2];
    }
    // create mapping
    Vec_IntSetEntry( vMapping, Abc_Lit2Var(RetValue), Vec_IntSize(vMapping2) );
    Vec_IntPush( vMapping2, Vec_IntSize(vLeaves2) );
    Vec_IntForEachEntry( vLeaves2, iTemp, k )
        Vec_IntPush( vMapping2, Abc_Lit2Var(iTemp) );
    Vec_IntPush( vMapping2, -Abc_Lit2Var(iTopLit) );
    RetValue = Abc_LitNotCond( RetValue, pCutBest->fCompl );
    return RetValue;
}
int Gia_ManFromIfLogicAndVars( Gia_Man_t * pNew, If_Man_t * pIfMan, If_Cut_t * pCutBest, Vec_Int_t * vLeaves, Vec_Int_t * vLeaves2, Vec_Int_t * vCover, Vec_Int_t * vMapping, Vec_Int_t * vMapping2 )
{
    word pFunc[64], uTruth[2];
    int nLeaves = pCutBest->nLeaves;
    int truthId = Abc_Lit2Var(pCutBest->iCutFunc);
    int c, k, Mask = Vec_IntEntry(pIfMan->vTtDecs[nLeaves], truthId);
    int MaskOne[2] = { Mask & 0xFFFF, (Mask >> 16) & 0x3FFF };
    int iLitCofs[2], iTemp, fOrDec = (Mask >> 30) & 1, RetValue = -1; 
    assert( Mask > 0 && nLeaves <= 2 * (pIfMan->pPars->nLutSize/2) && pIfMan->pPars->nLutSize <= 13 );
    Abc_TtCopy( pFunc, If_CutTruthWR(pIfMan, pCutBest), pIfMan->nTruth6Words[nLeaves], fOrDec );
    Abc_TtDeriveBiDec( pFunc, nLeaves, MaskOne[0], MaskOne[1], pIfMan->pPars->nLutSize/2, &uTruth[0], &uTruth[1] );
    uTruth[0] = fOrDec ? ~uTruth[0] : uTruth[0];
    uTruth[1] = fOrDec ? ~uTruth[1] : uTruth[1];
    for ( c = 0; c < 2; c++ )
    {
        Vec_IntClear( vLeaves2 );
        for ( k = 0; k < nLeaves; k++ )
            if ( (MaskOne[c] >> k) & 1 )
                Vec_IntPush( vLeaves2, Vec_IntEntry(vLeaves, k) );
        assert( Vec_IntSize(vLeaves2) >= 1 );
        iLitCofs[c] = Kit_TruthToGia( pNew, (unsigned *)&uTruth[c], Vec_IntSize(vLeaves2), vCover, vLeaves2, 0 );
        if ( Vec_IntSize(vLeaves2) == 1 )
            continue;
        // create mapping
        assert( Gia_ObjIsAnd(Gia_ManObj(pNew, Abc_Lit2Var(iLitCofs[c]))) );
        Vec_IntSetEntry( vMapping, Abc_Lit2Var(iLitCofs[c]), Vec_IntSize(vMapping2) );
        Vec_IntPush( vMapping2, Vec_IntSize(vLeaves2) );
        Vec_IntForEachEntry( vLeaves2, iTemp, k )
            Vec_IntPush( vMapping2, Abc_Lit2Var(iTemp) );
        Vec_IntPush( vMapping2, Abc_Lit2Var(iLitCofs[c]) );
    }
    iLitCofs[0] = Abc_LitNotCond( iLitCofs[0], fOrDec );
    iLitCofs[1] = Abc_LitNotCond( iLitCofs[1], fOrDec );
    RetValue = Gia_ManAppendAnd( pNew, iLitCofs[0], iLitCofs[1] );
    RetValue = Abc_LitNotCond( RetValue, fOrDec ^ Abc_LitIsCompl(pCutBest->iCutFunc) );
    // create mapping
    Vec_IntSetEntry( vMapping, Abc_Lit2Var(RetValue), Vec_IntSize(vMapping2) );
    Vec_IntPush( vMapping2, 2 );
    Vec_IntPush( vMapping2, Abc_Lit2Var(iLitCofs[0]) );
    Vec_IntPush( vMapping2, Abc_Lit2Var(iLitCofs[1]) );
    Vec_IntPush( vMapping2, -Abc_Lit2Var(RetValue) );
    RetValue = Abc_LitNotCond( RetValue, pCutBest->fCompl );
    return RetValue;
}

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

  Synopsis    [Converts IF into GIA manager.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
1841
Gia_Man_t * Gia_ManFromIfLogic( If_Man_t * pIfMan )
1842
{
1843
    int fWriteConfigs = 1;
1844
    Gia_Man_t * pNew, * pHashed = NULL;
1845 1846
    If_Cut_t * pCutBest;
    If_Obj_t * pIfObj, * pIfLeaf;
1847
    Vec_Int_t * vMapping, * vMapping2, * vPacking = NULL, * vConfigs = NULL;
1848
    Vec_Int_t * vLeaves, * vLeaves2, * vCover, * vLits;
1849
    Vec_Str_t * vConfigsStr = NULL;
1850
    Ifn_Ntk_t * pNtkCell = NULL;
1851
    sat_solver * pSat = NULL;
Alan Mishchenko committed
1852
    int i, k, Entry;
1853
    assert( !pIfMan->pPars->fDeriveLuts || pIfMan->pPars->fTruth );
1854 1855
//    if ( pIfMan->pPars->fEnableCheck07 )
//        pIfMan->pPars->fDeriveLuts = 0;
1856 1857 1858
    // start mapping and packing
    vMapping  = Vec_IntStart( If_ManObjNum(pIfMan) );
    vMapping2 = Vec_IntStart( 1 );
1859
    if ( pIfMan->pPars->fDeriveLuts && (pIfMan->pPars->pLutStruct || pIfMan->pPars->fEnableCheck75 || pIfMan->pPars->fEnableCheck75u || pIfMan->pPars->fEnableCheck07) )
1860 1861 1862 1863
    {
        vPacking = Vec_IntAlloc( 1000 );
        Vec_IntPush( vPacking, 0 );
    }
1864 1865 1866 1867 1868 1869 1870 1871
    if ( pIfMan->pPars->fUseDsdTune )
    {
        int nTtBitNum = If_DsdManTtBitNum(pIfMan->pIfDsdMan);
        int nPermBitNum = If_DsdManPermBitNum(pIfMan->pIfDsdMan);
        int nConfigInts = Abc_BitWordNum(nTtBitNum + nPermBitNum + 1);
        vConfigs = Vec_IntAlloc( 1000 );
        Vec_IntPush( vConfigs, 0 );
        Vec_IntPush( vConfigs, nConfigInts );
1872 1873
        if ( fWriteConfigs )
            vConfigsStr = Vec_StrAlloc( 1000 );
1874
    }
1875 1876 1877
    // create new manager
    pNew = Gia_ManStart( If_ManObjNum(pIfMan) );
    // iterate through nodes used in the mapping
1878
    vLits    = Vec_IntAlloc( 1000 );
1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889
    vCover   = Vec_IntAlloc( 1 << 16 );
    vLeaves  = Vec_IntAlloc( 16 );
    vLeaves2 = Vec_IntAlloc( 16 );
    If_ManCleanCutData( pIfMan );
    If_ManForEachObj( pIfMan, pIfObj, i )
    {
        if ( pIfObj->nRefs == 0 && !If_ObjIsTerm(pIfObj) )
            continue;
        if ( If_ObjIsAnd(pIfObj) )
        {
            pCutBest = If_ObjCutBest( pIfObj );
1890
            // perform sorting of cut leaves by delay, so that the slowest pin drives the fastest input of the LUT
1891
            if ( !pIfMan->pPars->fUseTtPerm && !pIfMan->pPars->fDelayOpt && !pIfMan->pPars->fDelayOptLut && !pIfMan->pPars->fDsdBalance && 
1892 1893
                 !pIfMan->pPars->pLutStruct && !pIfMan->pPars->fUserRecLib && !pIfMan->pPars->fUserSesLib && !pIfMan->pPars->nGateSize && 
                 !pIfMan->pPars->fEnableCheck75 && !pIfMan->pPars->fEnableCheck75u && !pIfMan->pPars->fEnableCheck07 && !pIfMan->pPars->fUseDsdTune && 
1894
                 !pIfMan->pPars->fUseCofVars && !pIfMan->pPars->fUseAndVars )
1895
                If_CutRotatePins( pIfMan, pCutBest );
1896 1897 1898 1899
            // collect leaves of the best cut
            Vec_IntClear( vLeaves );
            If_CutForEachLeaf( pIfMan, pCutBest, pIfLeaf, k )
                Vec_IntPush( vLeaves, pIfLeaf->iCopy );
1900
            // perform one of the two types of mapping: with and without structures
1901
            if ( pIfMan->pPars->fUseDsd && pIfMan->pPars->pLutStruct )
1902 1903 1904 1905 1906 1907 1908 1909 1910
            {
                if ( pSat == NULL )
                    pSat = (sat_solver *)If_ManSatBuildXY( (int)(pIfMan->pPars->pLutStruct[0] - '0') );
                if ( pIfMan->pPars->pLutStruct && pIfMan->pPars->fDeriveLuts )
                    pIfObj->iCopy = Gia_ManFromIfLogicFindLut( pIfMan, pNew, pCutBest, pSat, vLeaves, vLits, vCover, vMapping, vMapping2, vPacking );
                else
                    pIfObj->iCopy = Gia_ManFromIfLogicCreateLut( pNew, If_CutTruthW(pIfMan, pCutBest), vLeaves, vCover, vMapping, vMapping2 );
                pIfObj->iCopy = Abc_LitNotCond( pIfObj->iCopy, pCutBest->fCompl );
            }
1911
/*
1912 1913
            else if ( pIfMan->pPars->fUseDsd && pIfMan->pPars->fUseDsdTune && pIfMan->pPars->fDeriveLuts )
            {
1914
                if ( pNtkCell == NULL )
1915
                {
1916 1917
                    assert( If_DsdManGetCellStr(pIfMan->pIfDsdMan) != NULL );
                    pNtkCell = Ifn_NtkParse( If_DsdManGetCellStr(pIfMan->pIfDsdMan) );
1918 1919 1920 1921 1922 1923 1924
                    nLutMax = Ifn_NtkLutSizeMax( pNtkCell );
                    pHashed = Gia_ManStart( 10000 );
                    Vec_IntFillExtra( &pHashed->vCopies, 10000, -1 );
                    for ( k = 0; k < pIfMan->pPars->nLutSize; k++ )
                        Gia_ManAppendCi( pHashed );
                    Gia_ManHashAlloc( pHashed );
                }
1925
                pIfObj->iCopy = Gia_ManFromIfLogicFindCell( pIfMan, pNew, pHashed, pCutBest, pNtkCell, nLutMax, vLeaves, vLits, vCover, vMapping, vMapping2, vConfigs );
1926 1927
                pIfObj->iCopy = Abc_LitNotCond( pIfObj->iCopy, pCutBest->fCompl );
            }
1928
*/
1929
            else if ( pIfMan->pPars->fUseAndVars && pIfMan->pPars->fUseCofVars && pIfMan->pPars->fDeriveLuts && (int)pCutBest->nLeaves > pIfMan->pPars->nLutSize/2 )
1930
            {
1931
                int truthId = Abc_Lit2Var(pCutBest->iCutFunc);
1932 1933 1934
                int Mask = Vec_IntEntry(pIfMan->vTtDecs[pCutBest->nLeaves], truthId);
                if ( Mask )
                    pIfObj->iCopy = Gia_ManFromIfLogicAndVars( pNew, pIfMan, pCutBest, vLeaves, vLeaves2, vCover, vMapping, vMapping2 );
1935
                else
1936
                    pIfObj->iCopy = Gia_ManFromIfLogicCofVars( pNew, pIfMan, pCutBest, vLeaves, vLeaves2, vCover, vMapping, vMapping2 );
1937
            }
1938 1939
            else if ( pIfMan->pPars->fUseAndVars && pIfMan->pPars->fDeriveLuts && (int)pCutBest->nLeaves > pIfMan->pPars->nLutSize/2 )
            {
1940 1941 1942 1943 1944
                pIfObj->iCopy = Gia_ManFromIfLogicAndVars( pNew, pIfMan, pCutBest, vLeaves, vLeaves2, vCover, vMapping, vMapping2 );
            }
            else if ( pIfMan->pPars->fUseCofVars && pIfMan->pPars->fDeriveLuts && (int)pCutBest->nLeaves > pIfMan->pPars->nLutSize/2 )
            {
                pIfObj->iCopy = Gia_ManFromIfLogicCofVars( pNew, pIfMan, pCutBest, vLeaves, vLeaves2, vCover, vMapping, vMapping2 );
1945
            }
1946
            else if ( (pIfMan->pPars->fDeriveLuts && pIfMan->pPars->fTruth) || pIfMan->pPars->fUseDsd || pIfMan->pPars->fUseTtPerm || pIfMan->pPars->pFuncCell2 )
1947
            {
1948 1949 1950
                word * pTruth = If_CutTruthW(pIfMan, pCutBest);
                if ( pIfMan->pPars->fUseTtPerm )
                    for ( k = 0; k < (int)pCutBest->nLeaves; k++ )
1951
                        if ( If_CutLeafBit(pCutBest, k) )
1952
                            Abc_TtFlip( pTruth, Abc_TtWordNum(pCutBest->nLeaves), k );
1953
                // perform decomposition of the cut
1954
                pIfObj->iCopy = Gia_ManFromIfLogicNode( pIfMan, pNew, i, vLeaves, vLeaves2, pTruth, pIfMan->pPars->pLutStruct, vCover, vMapping, vMapping2, vPacking, (pIfMan->pPars->fEnableCheck75 || pIfMan->pPars->fEnableCheck75u), pIfMan->pPars->fEnableCheck07 );
1955
                pIfObj->iCopy = Abc_LitNotCond( pIfObj->iCopy, pCutBest->fCompl );
1956
                if ( vConfigs && Vec_IntSize(vLeaves) > 1 && !Gia_ObjIsCi(Gia_ManObj(pNew, Abc_Lit2Var(pIfObj->iCopy))) && pIfObj->iCopy > 1 )
1957
                    Gia_ManFromIfGetConfig( vConfigs, pIfMan, pCutBest, pIfObj->iCopy, vConfigsStr );
1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970
            }
            else
            {
                pIfObj->iCopy = Gia_ManNodeIfToGia( pNew, pIfMan, pIfObj, vLeaves, 0 );
                // write mapping
                Vec_IntSetEntry( vMapping, Abc_Lit2Var(pIfObj->iCopy), Vec_IntSize(vMapping2) );
                Vec_IntPush( vMapping2, Vec_IntSize(vLeaves) );
                Vec_IntForEachEntry( vLeaves, Entry, k )
                    assert( Abc_Lit2Var(Entry) < Abc_Lit2Var(pIfObj->iCopy) );
                Vec_IntForEachEntry( vLeaves, Entry, k )
                    Vec_IntPush( vMapping2, Abc_Lit2Var(Entry)  );
                Vec_IntPush( vMapping2, Abc_Lit2Var(pIfObj->iCopy) );
            }
1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985
        }
        else if ( If_ObjIsCi(pIfObj) )
            pIfObj->iCopy = Gia_ManAppendCi(pNew);
        else if ( If_ObjIsCo(pIfObj) )
            pIfObj->iCopy = Gia_ManAppendCo( pNew, Abc_LitNotCond(If_ObjFanin0(pIfObj)->iCopy, If_ObjFaninC0(pIfObj)) );
        else if ( If_ObjIsConst1(pIfObj) )
        {
            pIfObj->iCopy = 1;
            // create const LUT
            Vec_IntWriteEntry( vMapping, 0, Vec_IntSize(vMapping2) );
            Vec_IntPush( vMapping2, 0 );
            Vec_IntPush( vMapping2, 0 );
        }
        else assert( 0 );
    }
1986
    Vec_IntFree( vLits );
1987 1988 1989
    Vec_IntFree( vCover );
    Vec_IntFree( vLeaves );
    Vec_IntFree( vLeaves2 );
1990 1991
    if ( pNtkCell )
        ABC_FREE( pNtkCell );
1992
    if ( pSat )
1993
        sat_solver_delete(pSat);
1994 1995
    if ( pHashed )
        Gia_ManStop( pHashed );
1996 1997
//    printf( "Mapping array size:  IfMan = %d. Gia = %d. Increase = %.2f\n", 
//        If_ManObjNum(pIfMan), Gia_ManObjNum(pNew), 1.0 * Gia_ManObjNum(pNew) / If_ManObjNum(pIfMan) );
1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009
    // finish mapping 
    if ( Vec_IntSize(vMapping) > Gia_ManObjNum(pNew) )
        Vec_IntShrink( vMapping, Gia_ManObjNum(pNew) );
    else
        Vec_IntFillExtra( vMapping, Gia_ManObjNum(pNew), 0 );
    assert( Vec_IntSize(vMapping) == Gia_ManObjNum(pNew) );
    Vec_IntForEachEntry( vMapping, Entry, i )
        if ( Entry > 0 )
            Vec_IntAddToEntry( vMapping, i, Gia_ManObjNum(pNew) );
    Vec_IntAppend( vMapping, vMapping2 );
    Vec_IntFree( vMapping2 );
    // attach mapping and packing
2010 2011
    assert( pNew->vMapping == NULL );
    assert( pNew->vPacking == NULL );
2012 2013
    assert( pNew->vConfigs == NULL );
    assert( pNew->pCellStr == NULL );
2014
    pNew->vMapping = vMapping;
2015
    pNew->vPacking = vPacking;
2016 2017 2018
    pNew->vConfigs = vConfigs;
    pNew->pCellStr = vConfigs ? Abc_UtilStrsav( If_DsdManGetCellStr(pIfMan->pIfDsdMan) ) : NULL;
    assert( !vConfigs || Vec_IntSize(vConfigs) == 2 + Vec_IntEntry(vConfigs, 0) * Vec_IntEntry(vConfigs, 1) );
2019 2020 2021 2022
    // verify that COs have mapping
    {
        Gia_Obj_t * pObj;
        Gia_ManForEachCo( pNew, pObj, i )
2023
           assert( !Gia_ObjIsAnd(Gia_ObjFanin0(pObj)) || Gia_ObjIsLut(pNew, Gia_ObjFaninId0p(pNew, pObj)) );
2024
    }
2025 2026 2027 2028 2029 2030 2031
    // verify that internal nodes have mapping
    {
        Gia_Obj_t * pFanin;
        Gia_ManForEachLut( pNew, i )
            Gia_LutForEachFaninObj( pNew, i, pFanin, k )
                assert( !Gia_ObjIsAnd(pFanin) || Gia_ObjIsLut(pNew, Gia_ObjId(pNew, pFanin)) );
    }
2032 2033 2034 2035 2036 2037
    // verify that CIs have no mapping
    {
        Gia_Obj_t * pObj;
        Gia_ManForEachCi( pNew, pObj, i )
           assert( !Gia_ObjIsLut(pNew, Gia_ObjId(pNew, pObj)) );
    }
2038 2039 2040 2041 2042
    // dump configuration strings
    if ( vConfigsStr )
    {
        FILE * pFile; int status;
        char * pStr, Buffer[1000] = {0};
2043
        const char * pNameGen = pIfMan->pName? Extra_FileNameGeneric( pIfMan->pName ) : "nameless_";
2044 2045 2046 2047 2048
        sprintf( Buffer, "%s_configs.txt", pNameGen );
        ABC_FREE( pNameGen );
        pFile = fopen( Buffer, "wb" );
        if ( pFile == NULL )
        {
Alan Mishchenko committed
2049
            Vec_StrFree( vConfigsStr );
2050 2051 2052 2053 2054 2055 2056 2057 2058 2059
            printf( "Cannot open file \"%s\".\n", Buffer );
            return pNew;
        }
        Vec_StrPush( vConfigsStr, '\0' );
        pStr = Vec_StrArray(vConfigsStr);
        status = fwrite( pStr, strlen(pStr), 1, pFile );
        Vec_StrFree( vConfigsStr );
        fclose( pFile );
        printf( "Finished dumping configs into file \"%s\".\n", Buffer );
    }
2060 2061 2062 2063
    return pNew;
}


2064 2065
/**Function*************************************************************

2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080
  Synopsis    [Verifies mapping.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Gia_ManMappingVerify_rec( Gia_Man_t * p, Gia_Obj_t * pObj )
{
    int Id, iFan, k, Result = 1;
    if ( Gia_ObjIsTravIdCurrent(p, pObj) )
        return 1;
    Gia_ObjSetTravIdCurrent(p, pObj);
2081
    if ( !Gia_ObjIsAndNotBuf(pObj) )
2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097
        return 1;
    if ( !Gia_ObjIsLut(p, Gia_ObjId(p, pObj)) )
    {
        Abc_Print( -1, "Gia_ManMappingVerify: Internal node %d does not have mapping.\n", Gia_ObjId(p, pObj) );
        return 0;
    }
    Id = Gia_ObjId(p, pObj);
    Gia_LutForEachFanin( p, Id, iFan, k )
        if ( Result )
            Result &= Gia_ManMappingVerify_rec( p, Gia_ManObj(p, iFan) );
    return Result;
}
void Gia_ManMappingVerify( Gia_Man_t * p )
{
    Gia_Obj_t * pObj, * pFanin;
    int i, Result = 1;
2098
    assert( Gia_ManHasMapping(p) );
2099
    Gia_ManIncrementTravId( p );
2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112
    Gia_ManForEachBuf( p, pObj, i )
    {
        pFanin = Gia_ObjFanin0(pObj);
        if ( !Gia_ObjIsAndNotBuf(pFanin) )
            continue;
        if ( !Gia_ObjIsLut(p, Gia_ObjId(p, pFanin)) )
        {
            Abc_Print( -1, "Gia_ManMappingVerify: CO driver %d does not have mapping.\n", Gia_ObjId(p, pFanin) );
            Result = 0;
            continue;
        }
        Result &= Gia_ManMappingVerify_rec( p, pFanin );
    }
2113 2114 2115
    Gia_ManForEachCo( p, pObj, i )
    {
        pFanin = Gia_ObjFanin0(pObj);
2116
        if ( !Gia_ObjIsAndNotBuf(pFanin) )
2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141
            continue;
        if ( !Gia_ObjIsLut(p, Gia_ObjId(p, pFanin)) )
        {
            Abc_Print( -1, "Gia_ManMappingVerify: CO driver %d does not have mapping.\n", Gia_ObjId(p, pFanin) );
            Result = 0;
            continue;
        }
        Result &= Gia_ManMappingVerify_rec( p, pFanin );
    }
//    if ( Result && Gia_NtkIsRoot(p) )
//        Abc_Print( 1, "Mapping verified correctly.\n" );
}


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

  Synopsis    [Transfers mapping from hie GIA to normalized GIA.]

  Description [Hie GIA (pGia) points to normalized GIA (p).]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
2142
void Gia_ManTransferMapping( Gia_Man_t * p, Gia_Man_t * pGia )
2143 2144
{
    Gia_Obj_t * pObj;
2145
    int i, k, iFan, iPlace;
2146
    if ( !Gia_ManHasMapping(pGia) )
2147
        return;
2148 2149 2150 2151 2152 2153
    Gia_ManMappingVerify( pGia );
    Vec_IntFreeP( &p->vMapping );
    p->vMapping = Vec_IntAlloc( 2 * Gia_ManObjNum(p) );
    Vec_IntFill( p->vMapping, Gia_ManObjNum(p), 0 );
    Gia_ManForEachLut( pGia, i )
    {
2154 2155
        if ( Gia_ObjValue(Gia_ManObj(pGia, i)) == ~0 ) // handle dangling LUT
            continue;
2156 2157 2158
        assert( !Abc_LitIsCompl(Gia_ObjValue(Gia_ManObj(pGia, i))) );
        pObj = Gia_ManObj( p, Abc_Lit2Var(Gia_ObjValue(Gia_ManObj(pGia, i))) );
        Vec_IntWriteEntry( p->vMapping, Gia_ObjId(p, pObj), Vec_IntSize(p->vMapping) );
2159
        iPlace = Vec_IntSize( p->vMapping );
2160 2161
        Vec_IntPush( p->vMapping, Gia_ObjLutSize(pGia, i) );
        Gia_LutForEachFanin( pGia, i, iFan, k )
2162 2163 2164 2165 2166 2167
        {
            if ( Gia_ObjValue(Gia_ManObj(pGia, iFan)) == ~0 ) // handle dangling LUT fanin
                Vec_IntAddToEntry( p->vMapping, iPlace, -1 );
            else
                Vec_IntPush( p->vMapping, Abc_Lit2Var(Gia_ObjValue(Gia_ManObj(pGia, iFan))) );
        }
2168
        iFan = Abc_Lit2Var( Gia_ObjValue(Gia_ManObj(pGia, Abc_AbsInt(Gia_ObjLutMuxId(pGia, i)))) );
2169
        Vec_IntPush( p->vMapping, Gia_ObjLutIsMux(pGia, i) ? -iFan : iFan );
2170 2171 2172
    }
    Gia_ManMappingVerify( p );
}
2173
void Gia_ManTransferPacking( Gia_Man_t * p, Gia_Man_t * pGia )
2174 2175 2176
{
    Vec_Int_t * vPackingNew;
    Gia_Obj_t * pObj, * pObjNew;
Alan Mishchenko committed
2177
    int i, k, Entry, nEntries, nEntries2;
2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206
    if ( pGia->vPacking == NULL )
        return;
    nEntries = Vec_IntEntry( pGia->vPacking, 0 );
    nEntries2 = 0;
    // create new packing info
    vPackingNew = Vec_IntAlloc( Vec_IntSize(pGia->vPacking) );
    Vec_IntPush( vPackingNew, nEntries );
    Vec_IntForEachEntryStart( pGia->vPacking, Entry, i, 1 )
    {
        assert( Entry > 0 && Entry < 4 );
        Vec_IntPush( vPackingNew, Entry );
        i++;
        for ( k = 0; k < Entry; k++, i++ )
        {
            pObj = Gia_ManObj(pGia, Vec_IntEntry(pGia->vPacking, i));
            pObjNew = Gia_ManObj(p, Abc_Lit2Var(Gia_ObjValue(pObj)));
            assert( Gia_ObjIsLut(pGia, Gia_ObjId(pGia, pObj)) );
            assert( Gia_ObjIsLut(p, Gia_ObjId(p, pObjNew)) );
            Vec_IntPush( vPackingNew, Gia_ObjId(p, pObjNew) );
//            printf( "%d -> %d  ", Vec_IntEntry(pGia->vPacking, i), Gia_ObjId(p, pObjNew) );
        }
        i--;
        nEntries2++;
    }
    assert( nEntries == nEntries2 );
    // attach packing info
    assert( p->vPacking == NULL );
    p->vPacking = vPackingNew;
}
2207
void Gia_ManTransferTiming( Gia_Man_t * p, Gia_Man_t * pGia )
2208
{
2209
    if ( pGia->vCiArrs || pGia->vCoReqs || pGia->vCoArrs || pGia->vCoAttrs )
2210 2211 2212 2213
    {
        p->vCiArrs     = pGia->vCiArrs;     pGia->vCiArrs    = NULL;
        p->vCoReqs     = pGia->vCoReqs;     pGia->vCoReqs    = NULL;
        p->vCoArrs     = pGia->vCoArrs;     pGia->vCoArrs    = NULL;
2214
        p->vCoAttrs    = pGia->vCoAttrs;    pGia->vCoAttrs   = NULL;
2215 2216
        p->And2Delay   = pGia->And2Delay;
    }
2217 2218 2219 2220
    if ( pGia->vInArrs || pGia->vOutReqs )
    {
        p->vInArrs     = pGia->vInArrs;     pGia->vInArrs     = NULL;
        p->vOutReqs    = pGia->vOutReqs;    pGia->vOutReqs    = NULL;
2221 2222
        p->DefInArrs   = pGia->DefInArrs;
        p->DefOutReqs  = pGia->DefOutReqs;
2223
        p->And2Delay   = pGia->And2Delay;
2224
    }
Alan Mishchenko committed
2225
    if ( pGia->vNamesIn || pGia->vNamesOut || pGia->vNamesNode )
2226 2227 2228
    {
        p->vNamesIn     = pGia->vNamesIn;     pGia->vNamesIn     = NULL;
        p->vNamesOut    = pGia->vNamesOut;    pGia->vNamesOut    = NULL;
Alan Mishchenko committed
2229
        p->vNamesNode   = pGia->vNamesNode;   pGia->vNamesNode   = NULL;
2230
    }
2231 2232 2233 2234 2235
    if ( pGia->vConfigs || pGia->pCellStr )
    {
        p->vConfigs     = pGia->vConfigs;     pGia->vConfigs     = NULL;
        p->pCellStr     = pGia->pCellStr;     pGia->pCellStr     = NULL;
    }
2236
    if ( pGia->pManTime == NULL || p == pGia )
2237
        return;
2238 2239 2240
    p->pManTime    = pGia->pManTime;    pGia->pManTime    = NULL;
    p->pAigExtra   = pGia->pAigExtra;   pGia->pAigExtra   = NULL;
    p->vRegClasses = pGia->vRegClasses; pGia->vRegClasses = NULL;
2241
    p->vRegInits   = pGia->vRegInits;   pGia->vRegInits = NULL;
2242
    p->nAnd2Delay  = pGia->nAnd2Delay;  pGia->nAnd2Delay  = 0;
2243
}
2244

2245 2246
/**Function*************************************************************

2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310
  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Abc_FrameMiniAigSetCiArrivals( Abc_Frame_t * pAbc, int * pArrivals )
{
    Gia_Man_t * pGia;
    if ( pArrivals == NULL )
        { printf( "Arrival times are not given.\n" ); return; }
    if ( pAbc == NULL )
        { printf( "ABC framework is not initialized by calling Abc_Start().\n" ); return; }
    pGia = Abc_FrameReadGia( pAbc );
    if ( pGia == NULL )
        { printf( "Current network in ABC framework is not defined.\n" ); return; }
    Vec_IntFreeP( &pGia->vCiArrs );
    pGia->vCiArrs = Vec_IntAllocArrayCopy( pArrivals, Gia_ManCiNum(pGia) );
}
void Abc_FrameMiniAigSetCoRequireds( Abc_Frame_t * pAbc, int * pRequireds )
{
    Gia_Man_t * pGia;
    if ( pRequireds == NULL )
        { printf( "Required times are not given.\n" ); return; }
    if ( pAbc == NULL )
        { printf( "ABC framework is not initialized by calling Abc_Start().\n" ); return; }
    pGia = Abc_FrameReadGia( pAbc );
    if ( pGia == NULL )
        { printf( "Current network in ABC framework is not defined.\n" ); return; }
    Vec_IntFreeP( &pGia->vCoReqs );
    pGia->vCoReqs = Vec_IntAllocArrayCopy( pRequireds, Gia_ManCoNum(pGia) );
}
int * Abc_FrameMiniAigReadCoArrivals( Abc_Frame_t * pAbc )
{
    Vec_Int_t * vArrs; int * pArrs;
    Gia_Man_t * pGia;
    if ( pAbc == NULL )
        { printf( "ABC framework is not initialized by calling Abc_Start()\n" ); return NULL; }
    pGia = Abc_FrameReadGia( pAbc );
    if ( pGia == NULL )
        { printf( "Current network in ABC framework is not defined.\n" ); return NULL; }
    if ( pGia->vCoArrs == NULL )
        { printf( "Current network in ABC framework has no CO arrival times.\n" ); return NULL; }
    vArrs = Vec_IntDup( pGia->vCoArrs );
    pArrs = Vec_IntReleaseArray( vArrs );
    Vec_IntFree( vArrs );
    return pArrs;
}
void Abc_FrameMiniAigSetAndGateDelay( Abc_Frame_t * pAbc, int Delay )
{
    Gia_Man_t * pGia;
    if ( pAbc == NULL )
        printf( "ABC framework is not initialized by calling Abc_Start()\n" );
    pGia = Abc_FrameReadGia( pAbc );
    if ( pGia == NULL )
        printf( "Current network in ABC framework is not defined.\n" );
    pGia->And2Delay = Delay;
}

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

2311 2312 2313 2314 2315 2316
  Synopsis    [Interface of LUT mapping package.]

  Description []
               
  SideEffects []

2317
  SeeAlso     [] 
2318 2319

***********************************************************************/
2320
Gia_Man_t * Gia_ManPerformMappingInt( Gia_Man_t * p, If_Par_t * pPars )
2321
{
2322
    extern void Gia_ManIffTest( Gia_Man_t * pGia, If_LibLut_t * pLib, int fVerbose );
2323
    Gia_Man_t * pNew;
2324
    If_Man_t * pIfMan; int i, Entry;//, Id, EntryF;
2325 2326
    assert( pPars->pTimesArr == NULL );
    assert( pPars->pTimesReq == NULL );
2327 2328 2329 2330 2331 2332 2333
    if ( p->vCiArrs )
    {
        assert( Vec_IntSize(p->vCiArrs) == Gia_ManCiNum(p) );
        pPars->pTimesArr = ABC_CALLOC( float, Gia_ManCiNum(p) );
        Vec_IntForEachEntry( p->vCiArrs, Entry, i )
            pPars->pTimesArr[i] = (float)Entry;
    }
2334
/*  // uncommenting this leads to a mysterious memory corruption 
2335 2336 2337
    else if ( p->vInArrs )
    {
        assert( Vec_FltSize(p->vInArrs) == Gia_ManCiNum(p) );
2338
        pPars->pTimesArr = ABC_CALLOC( float, Gia_ManCiNum(p));
2339
        Gia_ManForEachCiId( p, Id, i )
2340
            pPars->pTimesArr[i] = Vec_FltEntry(p->vInArrs, i);
2341
    }
2342
*/
2343 2344 2345 2346 2347 2348 2349
    if ( p->vCoReqs )
    {
        assert( Vec_IntSize(p->vCoReqs) == Gia_ManCoNum(p) );
        pPars->pTimesReq = ABC_CALLOC( float, Gia_ManCoNum(p) );
        Vec_IntForEachEntry( p->vCoReqs, Entry, i )
            pPars->pTimesReq[i] = (float)Entry;
    }
2350
/*  // uncommenting this leads to a mysterious memory corruption 
2351 2352 2353 2354 2355 2356 2357
    else if ( p->vOutReqs )
    {
        assert( Vec_FltSize(p->vOutReqs) == Gia_ManCoNum(p) );
        pPars->pTimesReq = ABC_CALLOC( float, Gia_ManCoNum(p) );
        Vec_FltForEachEntry( p->vOutReqs, EntryF, i )
            pPars->pTimesReq[i] = EntryF;
    }
2358
*/
2359 2360
    ABC_FREE( p->pCellStr );
    Vec_IntFreeP( &p->vConfigs );
2361
    // disable cut minimization when GIA strucure is needed
2362
    if ( !pPars->fDelayOpt && !pPars->fDelayOptLut && !pPars->fDsdBalance && !pPars->fUserRecLib && !pPars->fUserSesLib && !pPars->fDeriveLuts && !pPars->fUseDsd && !pPars->fUseTtPerm && !pPars->pFuncCell2 )
2363
        pPars->fCutMin = 0;
2364 2365 2366 2367
    // translate into the mapper
    pIfMan = Gia_ManToIf( p, pPars );    
    if ( pIfMan == NULL )
        return NULL;
2368 2369 2370 2371 2372 2373 2374
    // create DSD manager
    if ( pPars->fUseDsd )
    {
        If_DsdMan_t * p = (If_DsdMan_t *)Abc_FrameReadManDsd();
        assert( pPars->nLutSize <= If_DsdManVarNum(p) );
        assert( (pPars->pLutStruct == NULL && If_DsdManLutSize(p) == 0) || (pPars->pLutStruct && pPars->pLutStruct[0] - '0' == If_DsdManLutSize(p)) );
        pIfMan->pIfDsdMan = (If_DsdMan_t *)Abc_FrameReadManDsd();
2375 2376
        if ( pPars->fDsdBalance )
            If_DsdManAllocIsops( pIfMan->pIfDsdMan, pPars->nLutSize );
2377
    }
2378 2379
    // compute switching for the IF objects
    if ( pPars->fPower )
2380 2381 2382 2383 2384 2385
    {
        if ( p->pManTime == NULL )
            If_ManComputeSwitching( pIfMan );
        else
            Abc_Print( 0, "Switching activity computation for designs with boxes is disabled.\n" );
    }
2386 2387
    if ( pPars->pReoMan )
        pIfMan->pUserMan = pPars->pReoMan;
2388
    if ( p->pManTime )
2389
        pIfMan->pManTim = Tim_ManDup( (Tim_Man_t *)p->pManTime, pPars->fDelayOpt || pPars->fDelayOptLut || pPars->fDsdBalance || pPars->fUserRecLib || pPars->fUserSesLib );
2390
//    Tim_ManPrint( pIfMan->pManTim );
2391 2392 2393 2394 2395 2396
    if ( p->vCoAttrs )
    {
        assert( If_ManCoNum(pIfMan) == Vec_IntSize(p->vCoAttrs) );
        Vec_IntForEachEntry( p->vCoAttrs, Entry, i )
            If_ObjFanin0( If_ManCo(pIfMan, i) )->fSpec = (Entry != 0);
    }
2397 2398 2399 2400 2401
    if ( !If_ManPerformMapping( pIfMan ) )
    {
        If_ManStop( pIfMan );
        return NULL;
    }
2402 2403
    if ( pPars->pFuncWrite )
        pPars->pFuncWrite( pIfMan );
2404
    // transform the result of mapping into the new network
2405
    if ( pIfMan->pPars->fDelayOpt || pIfMan->pPars->fDsdBalance || pIfMan->pPars->fUserRecLib || pIfMan->pPars->fUserSesLib )
2406
        pNew = Gia_ManFromIfAig( pIfMan );
2407
    else
2408
        pNew = Gia_ManFromIfLogic( pIfMan );
2409 2410
    if ( p->vCiArrs || p->vCoReqs )
    {
Alan Mishchenko committed
2411
        If_Obj_t * pIfObj = NULL;
2412 2413 2414 2415 2416
        Vec_IntFreeP( &p->vCoArrs );
        p->vCoArrs = Vec_IntAlloc( Gia_ManCoNum(p) );
        If_ManForEachCo( pIfMan, pIfObj, i )
            Vec_IntPush( p->vCoArrs, (int)If_ObjArrTime(If_ObjFanin0(pIfObj)) );
    }
2417 2418 2419 2420
    If_ManStop( pIfMan );
    // transfer name
    assert( pNew->pName == NULL );
    pNew->pName = Abc_UtilStrsav( p->pName );
Alan Mishchenko committed
2421
    ABC_FREE( pNew->pSpec );
2422 2423
    pNew->pSpec = Abc_UtilStrsav( p->pSpec );
    Gia_ManSetRegNum( pNew, Gia_ManRegNum(p) );
2424 2425 2426 2427 2428 2429 2430
    // print delay trace
    if ( pPars->fVerboseTrace )
    {
        pNew->pLutLib = pPars->pLutLib;
        Gia_ManDelayTraceLutPrint( pNew, 1 );
        pNew->pLutLib = NULL;
    }
2431
    return pNew;
Alan Mishchenko committed
2432
}
2433 2434 2435
Gia_Man_t * Gia_ManPerformMapping( Gia_Man_t * p, void * pp )
{
    Gia_Man_t * pNew;
2436
    if ( p->pManTime && Tim_ManBoxNum((Tim_Man_t*)p->pManTime) && Gia_ManIsNormalized(p) )
2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450
    {
        pNew = Gia_ManDupUnnormalize( p );
        if ( pNew == NULL )
            return NULL;
        Gia_ManTransferTiming( pNew, p );
        p = pNew;
        // mapping
        pNew = Gia_ManPerformMappingInt( p, (If_Par_t *)pp );
        if ( pNew != p )
        {
            Gia_ManTransferTiming( pNew, p );
            Gia_ManStop( p );
        }
        // normalize
2451
        pNew = Gia_ManDupNormalize( p = pNew, ((If_Par_t *)pp)->fHashMapping );
2452 2453 2454 2455 2456 2457 2458 2459 2460 2461
        Gia_ManTransferMapping( pNew, p );
        Gia_ManTransferPacking( pNew, p );
        Gia_ManTransferTiming( pNew, p );
        Gia_ManStop( p );
        assert( Gia_ManIsNormalized(pNew) );
    }
    else 
    {
        pNew = Gia_ManPerformMappingInt( p, (If_Par_t *)pp );
        Gia_ManTransferTiming( pNew, p );
2462
        if ( ((If_Par_t *)pp)->fHashMapping )
2463 2464
        {
            pNew = Gia_ManDupHashMapping( p = pNew );
2465
            Gia_ManTransferPacking( pNew, p );
2466 2467 2468
            Gia_ManTransferTiming( pNew, p );
            Gia_ManStop( p );
        }
2469
    }
2470 2471
    pNew->MappedDelay = (int)((If_Par_t *)pp)->FinalDelay;
    pNew->MappedArea  = (int)((If_Par_t *)pp)->FinalArea;
2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485
    return pNew;
}

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

  Synopsis    [Interface of other mapping-based procedures.]

  Description []
               
  SideEffects []

  SeeAlso     [] 

***********************************************************************/
2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504
Gia_Man_t * Gia_ManPerformSopBalance( Gia_Man_t * p, int nCutNum, int nRelaxRatio, int fVerbose )
{
    Gia_Man_t * pNew;
    If_Man_t * pIfMan;
    If_Par_t Pars, * pPars = &Pars;
    If_ManSetDefaultPars( pPars );
    pPars->nCutsMax    = nCutNum;
    pPars->nRelaxRatio = nRelaxRatio;
    pPars->fVerbose    = fVerbose;
    pPars->nLutSize    = 6;
    pPars->fDelayOpt   = 1;
    pPars->fCutMin     = 1;
    pPars->fTruth      = 1;
    pPars->fExpRed     = 0;
    // perform mapping
    pIfMan = Gia_ManToIf( p, pPars );
    If_ManPerformMapping( pIfMan );
    pNew = Gia_ManFromIfAig( pIfMan );
    If_ManStop( pIfMan );
2505
    Gia_ManTransferTiming( pNew, p );
2506 2507 2508
    // transfer name
    assert( pNew->pName == NULL );
    pNew->pName = Abc_UtilStrsav( p->pName );
2509 2510 2511 2512
    pNew->pSpec = Abc_UtilStrsav( p->pSpec );
    Gia_ManSetRegNum( pNew, Gia_ManRegNum(p) );
    return pNew;
}
2513
Gia_Man_t * Gia_ManPerformDsdBalance( Gia_Man_t * p, int nLutSize, int nCutNum, int nRelaxRatio, int fVerbose )
2514 2515 2516 2517 2518 2519 2520 2521
{
    Gia_Man_t * pNew;
    If_Man_t * pIfMan;
    If_Par_t Pars, * pPars = &Pars;
    If_ManSetDefaultPars( pPars );
    pPars->nCutsMax    = nCutNum;
    pPars->nRelaxRatio = nRelaxRatio;
    pPars->fVerbose    = fVerbose;
2522
    pPars->nLutSize    = nLutSize;
2523 2524 2525 2526 2527
    pPars->fDsdBalance = 1;
    pPars->fUseDsd     = 1;
    pPars->fCutMin     = 1;
    pPars->fTruth      = 1;
    pPars->fExpRed     = 0;
2528 2529
    if ( Abc_FrameReadManDsd2() == NULL )
        Abc_FrameSetManDsd2( If_DsdManAlloc(pPars->nLutSize, 0) );
2530 2531
    // perform mapping
    pIfMan = Gia_ManToIf( p, pPars );
2532
    pIfMan->pIfDsdMan = (If_DsdMan_t *)Abc_FrameReadManDsd2();
2533 2534 2535 2536 2537
    if ( pPars->fDsdBalance )
        If_DsdManAllocIsops( pIfMan->pIfDsdMan, pPars->nLutSize );
    If_ManPerformMapping( pIfMan );
    pNew = Gia_ManFromIfAig( pIfMan );
    If_ManStop( pIfMan );
2538
    Gia_ManTransferTiming( pNew, p );
2539 2540 2541
    // transfer name
    assert( pNew->pName == NULL );
    pNew->pName = Abc_UtilStrsav( p->pName );
2542 2543 2544 2545
    pNew->pSpec = Abc_UtilStrsav( p->pSpec );
    Gia_ManSetRegNum( pNew, Gia_ManRegNum(p) );
    return pNew;
}
Alan Mishchenko committed
2546

2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595
/**Function*************************************************************

  Synopsis    [Tests decomposition structures.]

  Description []
               
  SideEffects []

  SeeAlso     [] 

***********************************************************************/
void Gia_ManTestStruct( Gia_Man_t * p )
{
    int nCutMax = 7;
    int LutCount[8] = {0}, LutNDecomp[8] = {0};
    int i, k, iFan, nFanins, Status;
    Vec_Int_t * vLeaves;
    word * pTruth;

    vLeaves = Vec_IntAlloc( 100 );
    Gia_ObjComputeTruthTableStart( p, nCutMax );
    Gia_ManForEachLut( p, i )
    {
        nFanins = Gia_ObjLutSize(p, i);
        assert( nFanins <= 7 );
        LutCount[Abc_MaxInt(nFanins, 5)]++;
        if ( nFanins <= 5 )
            continue;
        Vec_IntClear( vLeaves );
        Gia_LutForEachFanin( p, i, iFan, k )
            Vec_IntPush( vLeaves, iFan );
        pTruth = Gia_ObjComputeTruthTableCut( p, Gia_ManObj(p, i), vLeaves );
        // check if it is decomposable
        Status = If_CutPerformCheck07( NULL, (unsigned *)pTruth, 7, nFanins, NULL );
        if ( Status == 1 )
            continue;
        LutNDecomp[nFanins]++;
        if ( LutNDecomp[nFanins] > 10 )
            continue;
        Kit_DsdPrintFromTruth( (unsigned *)pTruth, nFanins ); printf( "\n" );
    }
    Gia_ObjComputeTruthTableStop( p );

    printf( "LUT5 = %d    ", LutCount[5] );
    printf( "LUT6 = %d  NonDec = %d (%.2f %%)    ", LutCount[6], LutNDecomp[6], 100.0 * LutNDecomp[6]/Abc_MaxInt(LutCount[6], 1) );
    printf( "LUT7 = %d  NonDec = %d (%.2f %%)    ", LutCount[7], LutNDecomp[7], 100.0 * LutNDecomp[7]/Abc_MaxInt(LutCount[7], 1) );
    printf( "\n" );
}

2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644
/**Function*************************************************************

  Synopsis    [Performs hashing for a mapped AIG.]

  Description []
               
  SideEffects []

  SeeAlso     [] 

***********************************************************************/
Gia_Man_t * Gia_ManDupHashMapping( Gia_Man_t * p )
{
    Gia_Man_t * pNew; 
    Vec_Int_t * vMapping; 
    Gia_Obj_t * pObj, * pFanin;
    int i, k;
    assert( Gia_ManHasMapping(p) );
    // copy the old manager with hashing
    pNew = Gia_ManStart( Gia_ManObjNum(p) );
    pNew->pName = Abc_UtilStrsav( p->pName );
    pNew->pSpec = Abc_UtilStrsav( p->pSpec );
    Gia_ManHashAlloc( pNew );
    Gia_ManFillValue( p );
    Gia_ManConst0(p)->Value = 0;
    Gia_ManForEachCi( p, pObj, i )
        pObj->Value = Gia_ManAppendCi( pNew );
    Gia_ManForEachAnd( p, pObj, i )
        pObj->Value = Gia_ManHashAnd( pNew, Gia_ObjFanin0Copy(pObj), Gia_ObjFanin1Copy(pObj) );
    Gia_ManForEachCo( p, pObj, i )
        Gia_ManAppendCo( pNew, Gia_ObjFanin0Copy(pObj) );
    Gia_ManHashStop( pNew );
    Gia_ManSetRegNum( pNew, Gia_ManRegNum(p) );
    // recreate mapping
    vMapping = Vec_IntAlloc( Vec_IntSize(p->vMapping) );
    Vec_IntFill( vMapping, Gia_ManObjNum(p), 0 );
    Gia_ManForEachLut( p, i )
    {
        pObj = Gia_ManObj( p, i );
        Vec_IntWriteEntry( vMapping, Abc_Lit2Var(pObj->Value), Vec_IntSize(vMapping) );
        Vec_IntPush( vMapping, Gia_ObjLutSize(p, i) );
        Gia_LutForEachFaninObj( p, i, pFanin, k )
            Vec_IntPush( vMapping, Abc_Lit2Var(pFanin->Value)  );
        Vec_IntPush( vMapping, Abc_Lit2Var(pObj->Value) );
    }
    pNew->vMapping = vMapping;
    return pNew;
}

2645

Alan Mishchenko committed
2646 2647 2648 2649 2650
////////////////////////////////////////////////////////////////////////
///                       END OF FILE                                ///
////////////////////////////////////////////////////////////////////////


2651 2652
ABC_NAMESPACE_IMPL_END