lpkCore.c 23.1 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    [lpkCore.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [Fast Boolean matching for LUT structures.]

  Synopsis    []

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

  Date        [Ver. 1.0. Started - April 28, 2007.]

  Revision    [$Id: lpkCore.c,v 1.00 2007/04/28 00:00:00 alanmi Exp $]

***********************************************************************/
 
#include "lpkInt.h"
22 23
#include "bool/kit/cloud.h"
#include "base/main/main.h"
Alan Mishchenko committed
24

25 26 27
ABC_NAMESPACE_IMPL_START


Alan Mishchenko committed
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
////////////////////////////////////////////////////////////////////////
///                        DECLARATIONS                              ///
////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////
///                     FUNCTION DEFINITIONS                         ///
////////////////////////////////////////////////////////////////////////

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

  Synopsis    [Prepares the mapping manager.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Lpk_IfManStart( Lpk_Man_t * p )
{
    If_Par_t * pPars;
    assert( p->pIfMan == NULL );
    // set defaults
Alan Mishchenko committed
52
    pPars = ABC_ALLOC( If_Par_t, 1 );
Alan Mishchenko committed
53 54 55 56 57 58 59
    memset( pPars, 0, sizeof(If_Par_t) );
    // user-controlable paramters
    pPars->nLutSize    =  p->pPars->nLutSize;
    pPars->nCutsMax    = 16;
    pPars->nFlowIters  =  0; // 1
    pPars->nAreaIters  =  0; // 1 
    pPars->DelayTarget = -1;
Alan Mishchenko committed
60
    pPars->Epsilon     =  (float)0.005;
Alan Mishchenko committed
61 62 63 64 65 66 67 68 69
    pPars->fPreprocess =  0;
    pPars->fArea       =  1;
    pPars->fFancy      =  0;
    pPars->fExpRed     =  0; //
    pPars->fLatchPaths =  0;
    pPars->fVerbose    =  0;
    // internal parameters
    pPars->fTruth      =  1;
    pPars->fUsePerm    =  0; 
70 71
    pPars->nLatchesCi  =  0;
    pPars->nLatchesCo  =  0;
Alan Mishchenko committed
72 73 74 75 76 77 78 79 80 81
    pPars->pLutLib     =  NULL; // Abc_FrameReadLibLut();
    pPars->pTimesArr   =  NULL; 
    pPars->pTimesArr   =  NULL;   
    pPars->fUseBdds    =  0;
    pPars->fUseSops    =  0;
    pPars->fUseCnfs    =  0;
    pPars->fUseMv      =  0;
    // start the mapping manager and set its parameters
    p->pIfMan = If_ManStart( pPars );
    If_ManSetupSetAll( p->pIfMan, 1000 );
Alan Mishchenko committed
82
    p->pIfMan->pPars->pTimesArr = ABC_ALLOC( float, 32 );
Alan Mishchenko committed
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
}

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

  Synopsis    [Returns 1 if at least one entry has changed.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Lpk_NodeHasChanged( Lpk_Man_t * p, int iNode )
{
    Vec_Ptr_t * vNodes;
    Abc_Obj_t * pTemp;
    int i;
101
    vNodes = Vec_VecEntry( p->vVisited, iNode );
Alan Mishchenko committed
102 103
    if ( Vec_PtrSize(vNodes) == 0 )
        return 1;
104
    Vec_PtrForEachEntry( Abc_Obj_t *, vNodes, pTemp, i )
Alan Mishchenko committed
105 106
    {
        // check if the node has changed
Alan Mishchenko committed
107
        pTemp = Abc_NtkObj( p->pNtk, (int)(ABC_PTRUINT_T)pTemp );
Alan Mishchenko committed
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
        if ( pTemp == NULL )
            return 1;
        // check if the number of fanouts has changed
//        if ( Abc_ObjFanoutNum(pTemp) != (int)Vec_PtrEntry(vNodes, i+1) )
//            return 1;
        i++;
    }
    return 0;
}

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

  Synopsis    [Prepares the mapping manager.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Lpk_ExploreCut( Lpk_Man_t * p, Lpk_Cut_t * pCut, Kit_DsdNtk_t * pNtk )
{
    extern Abc_Obj_t * Abc_NodeFromIf_rec( Abc_Ntk_t * pNtkNew, If_Man_t * pIfMan, If_Obj_t * pIfObj, Vec_Int_t * vCover );
    Kit_DsdObj_t * pRoot;
    If_Obj_t * pDriver, * ppLeaves[16];
    Abc_Obj_t * pLeaf, * pObjNew;
135
    int nGain, i;
136
    abctime clk;
Alan Mishchenko committed
137 138 139 140 141 142 143
    int nNodesBef;
//    int nOldShared;

    // check special cases
    pRoot = Kit_DsdNtkRoot( pNtk );
    if ( pRoot->Type == KIT_DSD_CONST1 )
    {
144
        if ( Abc_LitIsCompl(pNtk->Root) )
Alan Mishchenko committed
145 146 147 148 149 150 151 152 153
            pObjNew = Abc_NtkCreateNodeConst0( p->pNtk );
        else
            pObjNew = Abc_NtkCreateNodeConst1( p->pNtk );
        Abc_NtkUpdate( p->pObj, pObjNew, p->vLevels );
        p->nGainTotal += pCut->nNodes - pCut->nNodesDup;
        return 1;
    }
    if ( pRoot->Type == KIT_DSD_VAR )
    {
154 155
        pObjNew = Abc_NtkObj( p->pNtk, pCut->pLeaves[ Abc_Lit2Var(pRoot->pFans[0]) ] );
        if ( Abc_LitIsCompl(pNtk->Root) ^ Abc_LitIsCompl(pRoot->pFans[0]) )
Alan Mishchenko committed
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
            pObjNew = Abc_NtkCreateNodeInv( p->pNtk, pObjNew );
        Abc_NtkUpdate( p->pObj, pObjNew, p->vLevels );
        p->nGainTotal += pCut->nNodes - pCut->nNodesDup;
        return 1;
    }
    assert( pRoot->Type == KIT_DSD_AND || pRoot->Type == KIT_DSD_XOR || pRoot->Type == KIT_DSD_PRIME );

    // start the mapping manager
    if ( p->pIfMan == NULL )
        Lpk_IfManStart( p );

    // prepare the mapping manager
    If_ManRestart( p->pIfMan );
    // create the PI variables
    for ( i = 0; i < p->pPars->nVarsMax; i++ )
        ppLeaves[i] = If_ManCreateCi( p->pIfMan );
    // set the arrival times
    Lpk_CutForEachLeaf( p->pNtk, pCut, pLeaf, i )
        p->pIfMan->pPars->pTimesArr[i] = (float)pLeaf->Level;
    // prepare the PI cuts
    If_ManSetupCiCutSets( p->pIfMan );
    // create the internal nodes
    p->fCalledOnce = 0;
    p->nCalledSRed = 0;
    pDriver = Lpk_MapTree_rec( p, pNtk, ppLeaves, pNtk->Root, NULL );
    if ( pDriver == NULL )
        return 0;
    // create the PO node
    If_ManCreateCo( p->pIfMan, If_Regular(pDriver) );

    // perform mapping
    p->pIfMan->pPars->fAreaOnly = 1;
188
clk = Abc_Clock();
Alan Mishchenko committed
189
    If_ManPerformMappingComb( p->pIfMan );
190
p->timeMap += Abc_Clock() - clk;
Alan Mishchenko committed
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220

    // compute the gain in area
    nGain = pCut->nNodes - pCut->nNodesDup - (int)p->pIfMan->AreaGlo;
    if ( p->pPars->fVeryVerbose )
        printf( "       Mffc = %2d. Mapped = %2d. Gain = %3d. Depth increase = %d. SReds = %d.\n", 
            pCut->nNodes - pCut->nNodesDup, (int)p->pIfMan->AreaGlo, nGain, (int)p->pIfMan->RequiredGlo - (int)p->pObj->Level, p->nCalledSRed );

    // quit if there is no gain
    if ( !(nGain > 0 || (p->pPars->fZeroCost && nGain == 0)) )
        return 0;

    // quit if depth increases too much
    if ( (int)p->pIfMan->RequiredGlo > Abc_ObjRequiredLevel(p->pObj) )
        return 0;

    // perform replacement
    p->nGainTotal += nGain;
    p->nChanges++;
    if ( p->nCalledSRed )
        p->nBenefited++;

    nNodesBef = Abc_NtkNodeNum(p->pNtk);
    // prepare the mapping manager
    If_ManCleanNodeCopy( p->pIfMan );
    If_ManCleanCutData( p->pIfMan );
    // set the PIs of the cut
    Lpk_CutForEachLeaf( p->pNtk, pCut, pLeaf, i )
        If_ObjSetCopy( If_ManCi(p->pIfMan, i), pLeaf );
    // get the area of mapping
    pObjNew = Abc_NodeFromIf_rec( p->pNtk, p->pIfMan, If_Regular(pDriver), p->vCover );
221
    pObjNew->pData = Hop_NotCond( (Hop_Obj_t *)pObjNew->pData, If_IsComplement(pDriver) );
Alan Mishchenko committed
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
    // perform replacement
    Abc_NtkUpdate( p->pObj, pObjNew, p->vLevels );
//printf( "%3d : %d-%d=%d(%d) \n", p->nChanges, nNodesBef, Abc_NtkNodeNum(p->pNtk), nNodesBef-Abc_NtkNodeNum(p->pNtk), nGain );
    return 1;
}

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

  Synopsis    [Performs resynthesis for one node.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Lpk_ResynthesizeNode( Lpk_Man_t * p )
{
Alan Mishchenko committed
241
//    static int Count = 0;
Alan Mishchenko committed
242 243 244
    Kit_DsdNtk_t * pDsdNtk;
    Lpk_Cut_t * pCut;
    unsigned * pTruth;
245
    int i, k, nSuppSize, nCutNodes, RetValue;
246
    abctime clk;
Alan Mishchenko committed
247 248

    // compute the cuts
249
clk = Abc_Clock();
Alan Mishchenko committed
250 251
    if ( !Lpk_NodeCuts( p ) )
    {
252
p->timeCuts += Abc_Clock() - clk;
Alan Mishchenko committed
253 254
        return 0;
    }
255
p->timeCuts += Abc_Clock() - clk;
Alan Mishchenko committed
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284

//return 0;

    if ( p->pPars->fVeryVerbose )
    printf( "Node %5d : Mffc size = %5d. Cuts = %5d.\n", p->pObj->Id, p->nMffc, p->nEvals );
    // try the good cuts
    p->nCutsTotal  += p->nCuts;
    p->nCutsUseful += p->nEvals;
    for ( i = 0; i < p->nEvals; i++ )
    {
        // get the cut
        pCut = p->pCuts + p->pEvals[i];
        if ( p->pPars->fFirst && i == 1 )
            break;

        // skip bad cuts        
//        printf( "Mffc size = %d.  ", Abc_NodeMffcLabel(p->pObj) );
        for ( k = 0; k < (int)pCut->nLeaves; k++ )
            Abc_NtkObj(p->pNtk, pCut->pLeaves[k])->vFanouts.nSize++;
        nCutNodes = Abc_NodeMffcLabel(p->pObj);
//        printf( "Mffc with cut = %d.  ", nCutNodes );
        for ( k = 0; k < (int)pCut->nLeaves; k++ )
            Abc_NtkObj(p->pNtk, pCut->pLeaves[k])->vFanouts.nSize--;
//        printf( "Mffc cut = %d.  ", (int)pCut->nNodes - (int)pCut->nNodesDup );
//        printf( "\n" );
        if ( nCutNodes != (int)pCut->nNodes - (int)pCut->nNodesDup )
            continue;

        // compute the truth table
285
clk = Abc_Clock();
Alan Mishchenko committed
286 287
        pTruth = Lpk_CutTruth( p, pCut, 0 );
        nSuppSize = Extra_TruthSupportSize(pTruth, pCut->nLeaves);
288
p->timeTruth += Abc_Clock() - clk;
Alan Mishchenko committed
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320

        pDsdNtk = Kit_DsdDecompose( pTruth, pCut->nLeaves ); 
//        Kit_DsdVerify( pDsdNtk, pTruth, pCut->nLeaves ); 
        // skip 16-input non-DSD because ISOP will not work
        if ( Kit_DsdNtkRoot(pDsdNtk)->nFans == 16 ) 
        {
            Kit_DsdNtkFree( pDsdNtk );
            continue;
        }

        // if DSD has nodes that require splitting to fit them into LUTs
        // we can skip those cuts that cannot lead to improvement
        // (a full DSD network requires  V = Nmin * (K-1) + 1 for improvement)
        if ( Kit_DsdNonDsdSizeMax(pDsdNtk) > p->pPars->nLutSize && 
             nSuppSize >= ((int)pCut->nNodes - (int)pCut->nNodesDup - 1) * (p->pPars->nLutSize - 1) + 1 )
        {
            Kit_DsdNtkFree( pDsdNtk );
            continue;
        }

        if ( p->pPars->fVeryVerbose )
        {
//            char * pFileName;
            printf( "  C%02d: L= %2d/%2d  V= %2d/%d  N= %d  W= %4.2f  ", 
                i, pCut->nLeaves, nSuppSize, pCut->nNodes, pCut->nNodesDup, pCut->nLuts, pCut->Weight );
            Kit_DsdPrint( stdout, pDsdNtk );
            Kit_DsdPrintFromTruth( pTruth, pCut->nLeaves );
//            pFileName = Kit_TruthDumpToFile( pTruth, pCut->nLeaves, Count++ );
//            printf( "Saved truth table in file \"%s\".\n", pFileName );
        }

        // update the network
321
clk = Abc_Clock();
Alan Mishchenko committed
322
        RetValue = Lpk_ExploreCut( p, pCut, pDsdNtk );
323
p->timeEval += Abc_Clock() - clk;
Alan Mishchenko committed
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
        Kit_DsdNtkFree( pDsdNtk );
        if ( RetValue )
            break;
    }
    return 1;
}


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

  Synopsis    [Computes supports of the cofactors of the function.]

  Description [This procedure should be called after Lpk_CutTruth(p,pCut,0)]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Lpk_ComputeSupports( Lpk_Man_t * p, Lpk_Cut_t * pCut, unsigned * pTruth )
{
    unsigned * pTruthInv;
    int RetValue1, RetValue2;
    pTruthInv = Lpk_CutTruth( p, pCut, 1 );
    RetValue1 = Kit_CreateCloudFromTruth( p->pDsdMan->dd, pTruth, pCut->nLeaves, p->vBddDir );
    RetValue2 = Kit_CreateCloudFromTruth( p->pDsdMan->dd, pTruthInv, pCut->nLeaves, p->vBddInv );
Alan Mishchenko committed
350
    if ( RetValue1 && RetValue2 && Vec_IntSize(p->vBddDir) > 1 && Vec_IntSize(p->vBddInv) > 1 )
Alan Mishchenko committed
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
        Kit_TruthCofSupports( p->vBddDir, p->vBddInv, pCut->nLeaves, p->vMemory, p->puSupps ); 
    else
        p->puSupps[0] = p->puSupps[1] = 0;
}


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

  Synopsis    [Performs resynthesis for one node.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Lpk_ResynthesizeNodeNew( Lpk_Man_t * p )
{
Alan Mishchenko committed
370
//    static int Count = 0;
Alan Mishchenko committed
371 372 373 374
    Abc_Obj_t * pObjNew, * pLeaf;
    Lpk_Cut_t * pCut;
    unsigned * pTruth;
    int nNodesBef, nNodesAft, nCutNodes;
375
    int i, k;
376
    abctime clk;
Alan Mishchenko committed
377 378 379 380
    int Required = Abc_ObjRequiredLevel(p->pObj);
//    CloudNode * pFun2;//, * pFun1;

    // compute the cuts
381
clk = Abc_Clock();
Alan Mishchenko committed
382 383
    if ( !Lpk_NodeCuts( p ) )
    {
384
p->timeCuts += Abc_Clock() - clk;
Alan Mishchenko committed
385 386
        return 0;
    }
387
p->timeCuts += Abc_Clock() - clk;
Alan Mishchenko committed
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422

    if ( p->pPars->fVeryVerbose )
        printf( "Node %5d : Mffc size = %5d. Cuts = %5d.  Level = %2d. Req = %2d.\n", 
            p->pObj->Id, p->nMffc, p->nEvals, p->pObj->Level, Required );
    // try the good cuts
    p->nCutsTotal  += p->nCuts;
    p->nCutsUseful += p->nEvals;
    for ( i = 0; i < p->nEvals; i++ )
    {
        // get the cut
        pCut = p->pCuts + p->pEvals[i];
        if ( p->pPars->fFirst && i == 1 )
            break;
//        if ( pCut->Weight < 1.05 )
//            continue;

        // skip bad cuts        
//        printf( "Mffc size = %d.  ", Abc_NodeMffcLabel(p->pObj) );
        for ( k = 0; k < (int)pCut->nLeaves; k++ )
            Abc_NtkObj(p->pNtk, pCut->pLeaves[k])->vFanouts.nSize++;
        nCutNodes = Abc_NodeMffcLabel(p->pObj);
//        printf( "Mffc with cut = %d.  ", nCutNodes );
        for ( k = 0; k < (int)pCut->nLeaves; k++ )
            Abc_NtkObj(p->pNtk, pCut->pLeaves[k])->vFanouts.nSize--;
//        printf( "Mffc cut = %d.  ", (int)pCut->nNodes - (int)pCut->nNodesDup );
//        printf( "\n" );
        if ( nCutNodes != (int)pCut->nNodes - (int)pCut->nNodesDup )
            continue;

        // collect nodes into the array
        Vec_PtrClear( p->vLeaves );
        for ( k = 0; k < (int)pCut->nLeaves; k++ )
            Vec_PtrPush( p->vLeaves, Abc_NtkObj(p->pNtk, pCut->pLeaves[k]) );

        // compute the truth table
423
clk = Abc_Clock();
Alan Mishchenko committed
424
        pTruth = Lpk_CutTruth( p, pCut, 0 );
425 426
p->timeTruth += Abc_Clock() - clk;
clk = Abc_Clock();
Alan Mishchenko committed
427
        Lpk_ComputeSupports( p, pCut, pTruth );        
428 429
p->timeSupps += Abc_Clock() - clk;
//clk = Abc_Clock();
Alan Mishchenko committed
430
//        pFun1 = Lpk_CutTruthBdd( p, pCut );
431
//p->timeTruth2 += Abc_Clock() - clk;
Alan Mishchenko committed
432
/*
433
clk = Abc_Clock();
Alan Mishchenko committed
434 435 436
        Cloud_Restart( p->pDsdMan->dd );
        pFun2 = Kit_TruthToCloud( p->pDsdMan->dd, pTruth, pCut->nLeaves );
        RetValue = Kit_CreateCloud( p->pDsdMan->dd, pFun2, p->vBddNodes );
437
p->timeTruth3 += Abc_Clock() - clk;
Alan Mishchenko committed
438 439 440 441 442 443 444 445 446 447 448 449
*/
//        if ( pFun1 != pFun2 )
//            printf( "Truth tables do not agree!\n" );
//        else
//            printf( "Fine!\n" );

        if ( p->pPars->fVeryVerbose )
        {
//            char * pFileName;
            int nSuppSize = Extra_TruthSupportSize( pTruth, pCut->nLeaves );
            printf( "  C%02d: L= %2d/%2d  V= %2d/%d  N= %d  W= %4.2f  ", 
                i, pCut->nLeaves, nSuppSize, pCut->nNodes, pCut->nNodesDup, pCut->nLuts, pCut->Weight );
450
            Vec_PtrForEachEntry( Abc_Obj_t *, p->vLeaves, pLeaf, k )
Alan Mishchenko committed
451 452 453 454 455 456 457 458 459
                printf( "%c=%d ", 'a'+k, Abc_ObjLevel(pLeaf) );
            printf( "\n" );
            Kit_DsdPrintFromTruth( pTruth, pCut->nLeaves );
//            pFileName = Kit_TruthDumpToFile( pTruth, pCut->nLeaves, Count++ );
//            printf( "Saved truth table in file \"%s\".\n", pFileName );
        }

        // update the network
        nNodesBef = Abc_NtkNodeNum(p->pNtk);
460
clk = Abc_Clock();
Alan Mishchenko committed
461 462
        pObjNew = Lpk_Decompose( p, p->pNtk, p->vLeaves, pTruth, p->puSupps, p->pPars->nLutSize,
            (int)pCut->nNodes - (int)pCut->nNodesDup - 1 + (int)(p->pPars->fZeroCost > 0), Required );
463
p->timeEval += Abc_Clock() - clk;
Alan Mishchenko committed
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506
        nNodesAft = Abc_NtkNodeNum(p->pNtk);

        // perform replacement
        if ( pObjNew )
        {
            int nGain = (int)pCut->nNodes - (int)pCut->nNodesDup - (nNodesAft - nNodesBef);
            assert( nGain >= 1 - p->pPars->fZeroCost );
            assert( Abc_ObjLevel(pObjNew) <= Required );
/*
            if ( nGain <= 0 )
            {
                int x = 0;
            }
            if ( Abc_ObjLevel(pObjNew) > Required )
            {
                int x = 0;
            }
*/
            p->nGainTotal += nGain;
            p->nChanges++;
            if ( p->pPars->fVeryVerbose )
                printf( "Performed resynthesis: Gain = %2d. Level = %2d. Req = %2d.\n", nGain, Abc_ObjLevel(pObjNew), Required );
            Abc_NtkUpdate( p->pObj, pObjNew, p->vLevels );
//printf( "%3d : %d-%d=%d(%d) \n", p->nChanges, nNodesBef, Abc_NtkNodeNum(p->pNtk), nNodesBef-Abc_NtkNodeNum(p->pNtk), nGain );
            break;
        }
    }
    return 1;
}

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

  Synopsis    [Performs resynthesis for one network.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Lpk_Resynthesize( Abc_Ntk_t * pNtk, Lpk_Par_t * pPars )
{
Alan Mishchenko committed
507
    ProgressBar * pProgress = NULL; // Suppress "might be used uninitialized"
Alan Mishchenko committed
508 509 510
    Lpk_Man_t * p;
    Abc_Obj_t * pObj;
    double Delta;
Alan Mishchenko committed
511
//    int * pnFanouts, nObjMax;
512
    int i, Iter, nNodes, nNodesPrev;
513
    abctime clk = Abc_Clock();
Alan Mishchenko committed
514 515 516 517 518 519
    assert( Abc_NtkIsLogic(pNtk) );
 
    // sweep dangling nodes as a preprocessing step
    Abc_NtkSweep( pNtk, 0 );

    // get the number of inputs
520
    if ( Abc_FrameReadLibLut() )
521
        pPars->nLutSize = ((If_LibLut_t *)Abc_FrameReadLibLut())->LutMax;
522 523
    else
        pPars->nLutSize = Abc_NtkGetFaninMax( pNtk );
Alan Mishchenko committed
524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569
    if ( pPars->nLutSize > 6 )
        pPars->nLutSize = 6;
    if ( pPars->nLutSize < 3 )
        pPars->nLutSize = 3;
    // adjust the number of crossbars based on LUT size
    if ( pPars->nVarsShared > pPars->nLutSize - 2 )
        pPars->nVarsShared = pPars->nLutSize - 2;
    // get the max number of LUTs tried
    pPars->nVarsMax = pPars->nLutsMax * (pPars->nLutSize - 1) + 1; // V = N * (K-1) + 1
    while ( pPars->nVarsMax > 16 )
    {
        pPars->nLutsMax--;
        pPars->nVarsMax = pPars->nLutsMax * (pPars->nLutSize - 1) + 1;

    }
    if ( pPars->fVerbose )
    {
        printf( "Resynthesis for %d %d-LUTs with %d non-MFFC LUTs, %d crossbars, and %d-input cuts.\n",
            pPars->nLutsMax, pPars->nLutSize, pPars->nLutsOver, pPars->nVarsShared, pPars->nVarsMax );
    }
 

    // convert into the AIG
    if ( !Abc_NtkToAig(pNtk) )
    {
        fprintf( stdout, "Converting to BDD has failed.\n" );
        return 0;
    }
    assert( Abc_NtkHasAig(pNtk) );

    // set the number of levels
    Abc_NtkLevel( pNtk );
    Abc_NtkStartReverseLevels( pNtk, pPars->nGrowthLevel );

    // start the manager
    p = Lpk_ManStart( pPars );
    p->pNtk = pNtk;
    p->nNodesTotal = Abc_NtkNodeNum(pNtk);
    p->vLevels = Vec_VecStart( pNtk->LevelMax ); 
    if ( p->pPars->fSatur )
        p->vVisited = Vec_VecStart( 0 );
    if ( pPars->fVerbose )
    {
        p->nTotalNets = Abc_NtkGetTotalFanins(pNtk);
        p->nTotalNodes = Abc_NtkNodeNum(pNtk);
    }
Alan Mishchenko committed
570 571 572
/*
    // save the number of fanouts of all objects
    nObjMax = Abc_NtkObjNumMax( pNtk );
Alan Mishchenko committed
573
    pnFanouts = ABC_ALLOC( int, nObjMax );
Alan Mishchenko committed
574 575 576 577
    memset( pnFanouts, 0, sizeof(int) * nObjMax );
    Abc_NtkForEachObj( pNtk, pObj, i )
        pnFanouts[pObj->Id] = Abc_ObjFanoutNum(pObj);
*/
Alan Mishchenko committed
578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627

    // iterate over the network
    nNodesPrev = p->nNodesTotal;
    for ( Iter = 1; ; Iter++ )
    {
        // expand storage for changed nodes
        if ( p->pPars->fSatur )
            Vec_VecExpand( p->vVisited, Abc_NtkObjNumMax(pNtk) + 1 );

        // consider all nodes
        nNodes = Abc_NtkObjNumMax(pNtk);
        if ( !pPars->fVeryVerbose )
            pProgress = Extra_ProgressBarStart( stdout, nNodes );
        Abc_NtkForEachNode( pNtk, pObj, i )
        {
            // skip all except the final node
            if ( pPars->fFirst )
            {
                if ( !Abc_ObjIsCo(Abc_ObjFanout0(pObj)) )
                    continue;
            }
            if ( i >= nNodes )
                break;
            if ( !pPars->fVeryVerbose )
                Extra_ProgressBarUpdate( pProgress, i, NULL );
            // skip the nodes that did not change
            if ( p->pPars->fSatur && !Lpk_NodeHasChanged(p, pObj->Id) )
                continue;
            // resynthesize
            p->pObj = pObj;
            if ( p->pPars->fOldAlgo )
                Lpk_ResynthesizeNode( p );
            else
                Lpk_ResynthesizeNodeNew( p );
        }
        if ( !pPars->fVeryVerbose )
            Extra_ProgressBarStop( pProgress );

        // check the increase
        Delta = 100.00 * (nNodesPrev - Abc_NtkNodeNum(pNtk)) / p->nNodesTotal;
        if ( Delta < 0.05 )
            break;
        nNodesPrev = Abc_NtkNodeNum(pNtk);
        if ( !p->pPars->fSatur )
            break;

        if ( pPars->fFirst )
            break;
    }
    Abc_NtkStopReverseLevels( pNtk );
Alan Mishchenko committed
628 629 630 631 632 633 634 635 636 637 638 639
/*
    // report the fanout changes
    Abc_NtkForEachObj( pNtk, pObj, i )
    {
        if ( i >= nObjMax )
            continue;
        if ( Abc_ObjFanoutNum(pObj) - pnFanouts[pObj->Id] == 0 )
            continue;
        printf( "%d ", Abc_ObjFanoutNum(pObj) - pnFanouts[pObj->Id] );
    }
    printf( "\n" );
*/
Alan Mishchenko committed
640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660

    if ( pPars->fVerbose )
    {
//        Cloud_PrintInfo( p->pDsdMan->dd );
        p->nTotalNets2 = Abc_NtkGetTotalFanins(pNtk);
        p->nTotalNodes2 = Abc_NtkNodeNum(pNtk);
        printf( "Node gain = %5d. (%.2f %%)  ", 
            p->nTotalNodes-p->nTotalNodes2, 100.0*(p->nTotalNodes-p->nTotalNodes2)/p->nTotalNodes );
        printf( "Edge gain = %5d. (%.2f %%)  ", 
            p->nTotalNets-p->nTotalNets2, 100.0*(p->nTotalNets-p->nTotalNets2)/p->nTotalNets );
        printf( "Muxes = %4d. Dsds = %4d.", p->nMuxes, p->nDsds );
        printf( "\n" );
        printf( "Nodes = %5d (%3d)  Cuts = %5d (%4d)  Changes = %5d  Iter = %2d  Benefit = %d.\n", 
            p->nNodesTotal, p->nNodesOver, p->nCutsTotal, p->nCutsUseful, p->nChanges, Iter, p->nBenefited );

        printf( "Non-DSD:" );
        for ( i = 3; i <= pPars->nVarsMax; i++ )
            if ( p->nBlocks[i] )
                printf( " %d=%d", i, p->nBlocks[i] );
        printf( "\n" );

661
        p->timeTotal = Abc_Clock() - clk;
Alan Mishchenko committed
662 663
        p->timeEval  = p->timeEval  - p->timeMap;
        p->timeOther = p->timeTotal - p->timeCuts - p->timeTruth - p->timeEval - p->timeMap;
Alan Mishchenko committed
664 665 666 667 668 669 670 671 672 673 674 675
        ABC_PRTP( "Cuts  ", p->timeCuts,  p->timeTotal );
        ABC_PRTP( "Truth ", p->timeTruth, p->timeTotal );
        ABC_PRTP( "CSupps", p->timeSupps, p->timeTotal );
        ABC_PRTP( "Eval  ", p->timeEval,  p->timeTotal );
        ABC_PRTP( " MuxAn", p->timeEvalMuxAn, p->timeEval );
        ABC_PRTP( " MuxSp", p->timeEvalMuxSp, p->timeEval );
        ABC_PRTP( " DsdAn", p->timeEvalDsdAn, p->timeEval );
        ABC_PRTP( " DsdSp", p->timeEvalDsdSp, p->timeEval );
        ABC_PRTP( " Other", p->timeEval-p->timeEvalMuxAn-p->timeEvalMuxSp-p->timeEvalDsdAn-p->timeEvalDsdSp, p->timeEval );
        ABC_PRTP( "Map   ", p->timeMap,   p->timeTotal );
        ABC_PRTP( "Other ", p->timeOther, p->timeTotal );
        ABC_PRTP( "TOTAL ", p->timeTotal, p->timeTotal );
Alan Mishchenko committed
676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692
    }

    Lpk_ManStop( p );
    // check the resulting network
    if ( !Abc_NtkCheck( pNtk ) )
    {
        printf( "Lpk_Resynthesize: The network check has failed.\n" );
        return 0;
    }
    return 1;
}

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


693 694
ABC_NAMESPACE_IMPL_END