abcLutmin.c 26.4 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
/**CFile****************************************************************

  FileName    [abcLutmin.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [Network and node package.]

  Synopsis    [Minimization of the number of LUTs.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

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

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

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

21
#include "base/abc/abc.h"
22 23

#ifdef ABC_USE_CUDD
24
#include "bdd/extrab/extraBdd.h"
25
#endif
Alan Mishchenko committed
26

27 28 29
ABC_NAMESPACE_IMPL_START


Alan Mishchenko committed
30 31 32 33 34 35
/* 
    Implememented here is the algorithm for minimal-LUT decomposition
    described in the paper: T. Sasao et al. "On the number of LUTs 
    to implement logic functions", To appear in Proc. IWLS'09.
*/

Alan Mishchenko committed
36 37 38 39 40 41 42 43
////////////////////////////////////////////////////////////////////////
///                        DECLARATIONS                              ///
////////////////////////////////////////////////////////////////////////

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

44 45
#ifdef ABC_USE_CUDD

Alan Mishchenko committed
46 47
/**Function*************************************************************

48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
  Synopsis    [Check if a LUT can absort a fanin.]

  Description [The fanins are (c, d0, d1).]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Abc_ObjCheckAbsorb( Abc_Obj_t * pObj, Abc_Obj_t * pPivot, int nLutSize, Vec_Ptr_t * vFanins )
{
    Abc_Obj_t * pFanin;
    int i;
    assert( Abc_ObjIsNode(pObj) && Abc_ObjIsNode(pPivot) );
    // add fanins of the node
    Vec_PtrClear( vFanins );
    Abc_ObjForEachFanin( pObj, pFanin, i )
        if ( pFanin != pPivot )
            Vec_PtrPush( vFanins, pFanin );
    // add fanins of the fanin
    Abc_ObjForEachFanin( pPivot, pFanin, i )
    {
        Vec_PtrPushUnique( vFanins, pFanin );
        if ( Vec_PtrSize(vFanins) > nLutSize )
            return 0;
    }
    return 1;
} 

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

  Synopsis    [Check how many times a LUT can absorb a fanin.]

  Description [The fanins are (c, d0, d1).]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Abc_NtkCheckAbsorb( Abc_Ntk_t * pNtk, int nLutSize )
{
    Vec_Int_t * vCounts;
    Vec_Ptr_t * vFanins;
    Abc_Obj_t * pObj, * pFanin;
93
    int i, k, Counter = 0, Counter2 = 0;
94
    abctime clk = Abc_Clock();
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
    vCounts = Vec_IntStart( Abc_NtkObjNumMax(pNtk) );
    vFanins = Vec_PtrAlloc( 100 );
    Abc_NtkForEachNode( pNtk, pObj, i )
    Abc_ObjForEachFanin( pObj, pFanin, k )
        if ( Abc_ObjIsNode(pFanin) && Abc_ObjCheckAbsorb( pObj, pFanin, nLutSize, vFanins ) )
        {
            Vec_IntAddToEntry( vCounts, Abc_ObjId(pFanin), 1 );
            Counter++;
        }
    Vec_PtrFree( vFanins );
    Abc_NtkForEachNode( pNtk, pObj, i )
        if ( Vec_IntEntry(vCounts, Abc_ObjId(pObj)) == Abc_ObjFanoutNum(pObj) )
        {
//            printf( "%d ", Abc_ObjId(pObj) );
            Counter2++;
        }
    printf( "Absorted = %6d. (%6.2f %%)   Fully = %6d. (%6.2f %%)  ", 
        Counter,  100.0 * Counter  / Abc_NtkNodeNum(pNtk), 
        Counter2, 100.0 * Counter2 / Abc_NtkNodeNum(pNtk) );
114
    Abc_PrintTime( 1, "Time", Abc_Clock() - clk );
115 116 117 118
}

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

Alan Mishchenko committed
119 120 121 122 123 124 125 126 127 128 129
  Synopsis    [Implements 2:1 MUX using one 3-LUT.]

  Description [The fanins are (c, d0, d1).]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Abc_Obj_t * Abc_NtkBddMux21( Abc_Ntk_t * pNtkNew, Abc_Obj_t * pFanins[] )
{
130
    DdManager * dd = (DdManager *)pNtkNew->pManFunc;
Alan Mishchenko committed
131 132 133 134 135 136 137 138 139
    Abc_Obj_t * pNode;
    DdNode * bSpin, * bCof0, * bCof1;
    pNode = Abc_NtkCreateNode( pNtkNew );
    Abc_ObjAddFanin( pNode, pFanins[0] );
    Abc_ObjAddFanin( pNode, pFanins[1] );
    Abc_ObjAddFanin( pNode, pFanins[2] );
    bSpin = Cudd_bddIthVar(dd, 0);
    bCof0 = Cudd_bddIthVar(dd, 1); 
    bCof1 = Cudd_bddIthVar(dd, 2); 
140
    pNode->pData = Cudd_bddIte( dd, bSpin, bCof1, bCof0 );  Cudd_Ref( (DdNode *)pNode->pData ); 
Alan Mishchenko committed
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
    return pNode;
}

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

  Synopsis    [Implements 4:1 MUX using one 6-LUT.]

  Description [The fanins are (c0, c1, d00, d01, d10, d11).]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Abc_Obj_t * Abc_NtkBddMux411( Abc_Ntk_t * pNtkNew, Abc_Obj_t * pFanins[] )
{
157
    DdManager * dd = (DdManager *)pNtkNew->pManFunc;
Alan Mishchenko committed
158 159 160 161 162 163 164 165 166 167 168 169 170
    Abc_Obj_t * pNode;
    DdNode * bSpin, * bCof0, * bCof1;
    pNode = Abc_NtkCreateNode( pNtkNew );
    Abc_ObjAddFanin( pNode, pFanins[0] );
    Abc_ObjAddFanin( pNode, pFanins[1] );
    Abc_ObjAddFanin( pNode, pFanins[2] );
    Abc_ObjAddFanin( pNode, pFanins[3] );
    Abc_ObjAddFanin( pNode, pFanins[4] );
    Abc_ObjAddFanin( pNode, pFanins[5] );
    bSpin = Cudd_bddIthVar(dd, 1);
    bCof0 = Cudd_bddIte( dd, bSpin, Cudd_bddIthVar(dd, 3), Cudd_bddIthVar(dd, 2) ); Cudd_Ref( bCof0 );
    bCof1 = Cudd_bddIte( dd, bSpin, Cudd_bddIthVar(dd, 5), Cudd_bddIthVar(dd, 4) ); Cudd_Ref( bCof1 );
    bSpin = Cudd_bddIthVar(dd, 0);
171
    pNode->pData = Cudd_bddIte( dd, bSpin, bCof1, bCof0 );  Cudd_Ref( (DdNode *)pNode->pData ); 
Alan Mishchenko committed
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
    Cudd_RecursiveDeref( dd, bCof0 );
    Cudd_RecursiveDeref( dd, bCof1 );
    return pNode;
}

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

  Synopsis    [Implementes 4:1 MUX using two 4-LUTs.]

  Description [The fanins are (c0, c1, d00, d01, d10, d11).]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Abc_Obj_t * Abc_NtkBddMux412( Abc_Ntk_t * pNtkNew, Abc_Obj_t * pFanins[] )
{
190
    DdManager * dd = (DdManager *)pNtkNew->pManFunc;
Alan Mishchenko committed
191 192 193 194 195 196 197 198 199 200 201
    Abc_Obj_t * pNodeBot, * pNodeTop;
    DdNode * bSpin, * bCof0, * bCof1;
    // bottom node
    pNodeBot = Abc_NtkCreateNode( pNtkNew );
    Abc_ObjAddFanin( pNodeBot, pFanins[0] );
    Abc_ObjAddFanin( pNodeBot, pFanins[1] );
    Abc_ObjAddFanin( pNodeBot, pFanins[2] );
    Abc_ObjAddFanin( pNodeBot, pFanins[3] );
    bSpin = Cudd_bddIthVar(dd, 0);
    bCof0 = Cudd_bddIte( dd, Cudd_bddIthVar(dd, 1), Cudd_bddIthVar(dd, 3), Cudd_bddIthVar(dd, 2) ); Cudd_Ref( bCof0 );
    bCof1 = Cudd_bddIthVar(dd, 1);
202
    pNodeBot->pData = Cudd_bddIte( dd, bSpin, bCof1, bCof0 );  Cudd_Ref( (DdNode *)pNodeBot->pData ); 
Alan Mishchenko committed
203 204 205 206 207 208 209 210 211 212
    Cudd_RecursiveDeref( dd, bCof0 );
    // top node
    pNodeTop = Abc_NtkCreateNode( pNtkNew );
    Abc_ObjAddFanin( pNodeTop, pFanins[0] );
    Abc_ObjAddFanin( pNodeTop, pNodeBot   );
    Abc_ObjAddFanin( pNodeTop, pFanins[4] );
    Abc_ObjAddFanin( pNodeTop, pFanins[5] );
    bSpin = Cudd_bddIthVar(dd, 0);
    bCof0 = Cudd_bddIthVar(dd, 1);
    bCof1 = Cudd_bddIte( dd, Cudd_bddIthVar(dd, 1), Cudd_bddIthVar(dd, 3), Cudd_bddIthVar(dd, 2) ); Cudd_Ref( bCof1 );
213
    pNodeTop->pData = Cudd_bddIte( dd, bSpin, bCof1, bCof0 );  Cudd_Ref( (DdNode *)pNodeTop->pData ); 
Alan Mishchenko committed
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
    Cudd_RecursiveDeref( dd, bCof1 );
    return pNodeTop;
}

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

  Synopsis    [Implementes 4:1 MUX using two 4-LUTs.]

  Description [The fanins are (c0, c1, d00, d01, d10, d11).]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Abc_Obj_t * Abc_NtkBddMux412a( Abc_Ntk_t * pNtkNew, Abc_Obj_t * pFanins[] )
{
231
    DdManager * dd = (DdManager *)pNtkNew->pManFunc;
Alan Mishchenko committed
232 233 234 235 236 237 238 239 240 241
    Abc_Obj_t * pNodeBot, * pNodeTop;
    DdNode * bSpin, * bCof0, * bCof1;
    // bottom node
    pNodeBot = Abc_NtkCreateNode( pNtkNew );
    Abc_ObjAddFanin( pNodeBot, pFanins[1] );
    Abc_ObjAddFanin( pNodeBot, pFanins[2] );
    Abc_ObjAddFanin( pNodeBot, pFanins[3] );
    bSpin = Cudd_bddIthVar(dd, 0);
    bCof0 = Cudd_bddIthVar(dd, 1);
    bCof1 = Cudd_bddIthVar(dd, 2);
242
    pNodeBot->pData = Cudd_bddIte( dd, bSpin, bCof1, bCof0 );  Cudd_Ref( (DdNode *)pNodeBot->pData ); 
Alan Mishchenko committed
243 244 245 246 247 248 249 250 251 252
    // top node
    pNodeTop = Abc_NtkCreateNode( pNtkNew );
    Abc_ObjAddFanin( pNodeTop, pFanins[0] );
    Abc_ObjAddFanin( pNodeTop, pFanins[1] );
    Abc_ObjAddFanin( pNodeTop, pNodeBot   );
    Abc_ObjAddFanin( pNodeTop, pFanins[4] );
    Abc_ObjAddFanin( pNodeTop, pFanins[5] );
    bSpin = Cudd_bddIthVar(dd, 0);
    bCof0 = Cudd_bddIthVar(dd, 2);
    bCof1 = Cudd_bddIte( dd, Cudd_bddIthVar(dd, 1), Cudd_bddIthVar(dd, 4), Cudd_bddIthVar(dd, 3) ); Cudd_Ref( bCof1 );
253
    pNodeTop->pData = Cudd_bddIte( dd, bSpin, bCof1, bCof0 );  Cudd_Ref( (DdNode *)pNodeTop->pData ); 
Alan Mishchenko committed
254 255 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 285 286 287 288 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 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
    Cudd_RecursiveDeref( dd, bCof1 );
    return pNodeTop;
}

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

  Synopsis    [Implements 4:1 MUX using three 2:1 MUXes.]

  Description [The fanins are (c0, c1, d00, d01, d10, d11).]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Abc_Obj_t * Abc_NtkBddMux413( Abc_Ntk_t * pNtkNew, Abc_Obj_t * pFanins[] )
{
    Abc_Obj_t * pNodesBot[3], * pNodesTop[3];
    // left bottom
    pNodesBot[0] = pFanins[1];
    pNodesBot[1] = pFanins[2];
    pNodesBot[2] = pFanins[3];
    pNodesTop[1] = Abc_NtkBddMux21( pNtkNew, pNodesBot );
    // right bottom
    pNodesBot[0] = pFanins[1];
    pNodesBot[1] = pFanins[4];
    pNodesBot[2] = pFanins[5];
    pNodesTop[2] = Abc_NtkBddMux21( pNtkNew, pNodesBot );
    // top node
    pNodesTop[0] = pFanins[0];
    return Abc_NtkBddMux21( pNtkNew, pNodesTop );
}

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

  Synopsis    [Finds unique cofactors of the function on the given level.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
DdNode * Abc_NtkBddCofactors_rec( DdManager * dd, DdNode * bNode, int iCof, int iLevel, int nLevels )
{
    DdNode * bNode0, * bNode1;
    if ( Cudd_IsConstant(bNode) || iLevel == nLevels )
        return bNode;
    if ( Cudd_ReadPerm( dd, Cudd_NodeReadIndex(bNode) ) > iLevel )
    {
        bNode0 = bNode;
        bNode1 = bNode;
    }
    else if ( Cudd_IsComplement(bNode) )
    {
        bNode0 = Cudd_Not(cuddE(Cudd_Regular(bNode)));
        bNode1 = Cudd_Not(cuddT(Cudd_Regular(bNode)));
    }
    else
    {
        bNode0 = cuddE(bNode);
        bNode1 = cuddT(bNode);
    }
    if ( (iCof >> (nLevels-1-iLevel)) & 1 )
        return Abc_NtkBddCofactors_rec( dd, bNode1, iCof, iLevel + 1, nLevels );
    return Abc_NtkBddCofactors_rec( dd, bNode0, iCof, iLevel + 1, nLevels );
}

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

  Synopsis    [Finds unique cofactors of the function on the given level.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Vec_Ptr_t * Abc_NtkBddCofactors( DdManager * dd, DdNode * bNode, int Level )
{
    Vec_Ptr_t * vCofs;
    int i, nCofs = (1<<Level);
    assert( Level > 0 && Level < 10 );
    vCofs = Vec_PtrAlloc( 8 );
    for ( i = 0; i < nCofs; i++ )
        Vec_PtrPush( vCofs, Abc_NtkBddCofactors_rec( dd, bNode, i, 0, Level ) );
    return vCofs;
}

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

  Synopsis    [Comparison procedure for two integers.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static int Vec_PtrSortCompare( void ** pp1, void ** pp2 )
{
    if ( *pp1 < *pp2 )
        return -1;
    if ( *pp1 > *pp2 )
        return 1;
    return 0;
}

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

Alan Mishchenko committed
367 368 369 370 371 372 373 374 375
  Synopsis    [Converts the node to MUXes.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
376
Abc_Obj_t * Abc_NtkCreateCofLut( Abc_Ntk_t * pNtkNew, DdManager * dd, DdNode * bCof, Abc_Obj_t * pNode, int Level )
Alan Mishchenko committed
377
{
Alan Mishchenko committed
378
    int fVerbose = 0;
Alan Mishchenko committed
379 380
    DdNode * bFuncNew;
    Abc_Obj_t * pNodeNew;
Alan Mishchenko committed
381 382
    int i;
    assert( Abc_ObjFaninNum(pNode) > Level );
Alan Mishchenko committed
383 384 385
    // create a new node
    pNodeNew = Abc_NtkCreateNode( pNtkNew );
    // add the fanins in the order, in which they appear in the reordered manager
Alan Mishchenko committed
386
    for ( i = Level; i < Abc_ObjFaninNum(pNode); i++ )
Alan Mishchenko committed
387
        Abc_ObjAddFanin( pNodeNew, Abc_ObjFanin(pNode, i)->pCopy );
Alan Mishchenko committed
388 389 390 391 392 393
if ( fVerbose )
{
Extra_bddPrint( dd, bCof );
printf( "\n" );
printf( "\n" );
}
Alan Mishchenko committed
394
    // transfer the function
Alan Mishchenko committed
395 396 397 398 399 400 401
    bFuncNew = Extra_bddMove( dd, bCof, -Level );  Cudd_Ref( bFuncNew );
if ( fVerbose )
{
Extra_bddPrint( dd, bFuncNew );
printf( "\n" );
printf( "\n" );
}
402
    pNodeNew->pData = Extra_TransferLevelByLevel( dd, (DdManager *)pNtkNew->pManFunc, bFuncNew );  Cudd_Ref( (DdNode *)pNodeNew->pData );
Alan Mishchenko committed
403 404 405
//Extra_bddPrint( pNtkNew->pManFunc, pNodeNew->pData );
//printf( "\n" );
//printf( "\n" );
Alan Mishchenko committed
406 407 408 409 410 411
    Cudd_RecursiveDeref( dd, bFuncNew );
    return pNodeNew;
}

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

Alan Mishchenko committed
412
  Synopsis    [Performs one step of Ashenhurst-Curtis decomposition.]
Alan Mishchenko committed
413 414 415 416 417 418 419 420

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
421 422
Abc_Obj_t * Abc_NtkBddCurtis( Abc_Ntk_t * pNtkNew, Abc_Obj_t * pNode, Vec_Ptr_t * vCofs, Vec_Ptr_t * vUniq )
{
423 424
    DdManager * ddOld = (DdManager *)pNode->pNtk->pManFunc;
    DdManager * ddNew = (DdManager *)pNtkNew->pManFunc;
Alan Mishchenko committed
425 426
    DdNode * bCof, * bUniq, * bMint, * bTemp, * bFunc, * bBits[10], ** pbCodeVars;
    Abc_Obj_t * pNodeNew = NULL, * pNodeBS[10];
427 428
    int nLutSize = Abc_Base2Log( Vec_PtrSize(vCofs) );
    int nBits    = Abc_Base2Log( Vec_PtrSize(vUniq) );
Alan Mishchenko committed
429 430 431 432 433 434 435
    int b, c, u, i;
    assert( nBits + 2 <= nLutSize );
    assert( nLutSize < Abc_ObjFaninNum(pNode) );
    // start BDDs for the decompoosed blocks
    for ( b = 0; b < nBits; b++ )
        bBits[b] = Cudd_ReadLogicZero(ddNew), Cudd_Ref( bBits[b] );
    // add each bound set minterm to one of the blccks
436
    Vec_PtrForEachEntry( DdNode *, vCofs, bCof, c )
Alan Mishchenko committed
437
    {
438
        Vec_PtrForEachEntry( DdNode *, vUniq, bUniq, u )
Alan Mishchenko committed
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470
            if ( bUniq == bCof )
                break;
        assert( u < Vec_PtrSize(vUniq) );
        for ( b = 0; b < nBits; b++ )
        {
            if ( ((u >> b) & 1) == 0 )
                continue;
            bMint = Extra_bddBitsToCube( ddNew, c, nLutSize, ddNew->vars, 1 );  Cudd_Ref( bMint );
            bBits[b] = Cudd_bddOr( ddNew, bTemp = bBits[b], bMint );  Cudd_Ref( bBits[b] );
            Cudd_RecursiveDeref( ddNew, bTemp );
            Cudd_RecursiveDeref( ddNew, bMint );
        }
    }
    // create bound set nodes
    for ( b = 0; b < nBits; b++ )
    {
        pNodeBS[b] = Abc_NtkCreateNode( pNtkNew );
        for ( i = 0; i < nLutSize; i++ )
            Abc_ObjAddFanin( pNodeBS[b], Abc_ObjFanin(pNode, i)->pCopy );
        pNodeBS[b]->pData = bBits[b]; // takes ref
    }
    // create composition node
    pNodeNew = Abc_NtkCreateNode( pNtkNew );
    // add free set variables first
    for ( i = nLutSize; i < Abc_ObjFaninNum(pNode); i++ )
        Abc_ObjAddFanin( pNodeNew, Abc_ObjFanin(pNode, i)->pCopy );
    // add code bit variables next
    for ( b = 0; b < nBits; b++ )
        Abc_ObjAddFanin( pNodeNew, pNodeBS[b] );
    // derive function of the composition node
    bFunc = Cudd_ReadLogicZero(ddNew); Cudd_Ref( bFunc );
    pbCodeVars = ddNew->vars + Abc_ObjFaninNum(pNode) - nLutSize;
471
    Vec_PtrForEachEntry( DdNode *, vUniq, bUniq, u )
Alan Mishchenko committed
472 473 474 475 476 477 478 479 480 481 482 483 484 485 486
    {
        bUniq = Extra_bddMove( ddOld, bUniq, -nLutSize );                   Cudd_Ref( bUniq );
        bUniq = Extra_TransferLevelByLevel( ddOld, ddNew, bTemp = bUniq );  Cudd_Ref( bUniq );
        Cudd_RecursiveDeref( ddOld, bTemp );

        bMint = Extra_bddBitsToCube( ddNew, u, nBits, pbCodeVars, 0 );  Cudd_Ref( bMint );
        bMint = Cudd_bddAnd( ddNew, bTemp = bMint, bUniq );  Cudd_Ref( bMint );
        Cudd_RecursiveDeref( ddNew, bTemp );
        Cudd_RecursiveDeref( ddNew, bUniq );

        bFunc = Cudd_bddOr( ddNew, bTemp = bFunc, bMint );  Cudd_Ref( bFunc );
        Cudd_RecursiveDeref( ddNew, bTemp );
        Cudd_RecursiveDeref( ddNew, bMint );
    }
    pNodeNew->pData = bFunc; // takes ref
Alan Mishchenko committed
487 488 489 490 491
    return pNodeNew;
}

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

Alan Mishchenko committed
492 493 494 495 496 497 498 499 500 501 502 503
  Synopsis    [Tries to decompose using cofactoring into two LUTs.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Abc_Obj_t * Abc_NtkBddFindCofactor( Abc_Ntk_t * pNtkNew, Abc_Obj_t * pNode, int nLutSize )
{
    Abc_Obj_t * pNodeBot, * pNodeTop;
504 505
    DdManager * ddOld = (DdManager *)pNode->pNtk->pManFunc;
    DdManager * ddNew = (DdManager *)pNtkNew->pManFunc;
506
    DdNode * bCof0 = NULL, * bCof1 = NULL, * bSupp, * bTemp, * bVar;
Alan Mishchenko committed
507 508 509 510 511 512
    DdNode * bCof0n, * bCof1n;
    int i, iCof, iFreeVar, fCof1Smaller = -1;
    assert( Abc_ObjFaninNum(pNode) == nLutSize + 1 );
    for ( iCof = 0; iCof < Abc_ObjFaninNum(pNode); iCof++ )
    {
        bVar  = Cudd_bddIthVar( ddOld, iCof );
513 514
        bCof0 = Cudd_Cofactor( ddOld, (DdNode *)pNode->pData, Cudd_Not(bVar) );  Cudd_Ref( bCof0 );
        bCof1 = Cudd_Cofactor( ddOld, (DdNode *)pNode->pData, bVar  );           Cudd_Ref( bCof1 );
Alan Mishchenko committed
515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570
        if ( Cudd_SupportSize( ddOld, bCof0 ) <= nLutSize - 2 )
        {
            fCof1Smaller = 0;
            break;
        }
        if ( Cudd_SupportSize( ddOld, bCof1 ) <= nLutSize - 2 )
        {
            fCof1Smaller = 1;
            break;
        }
        Cudd_RecursiveDeref( ddOld, bCof0 );
        Cudd_RecursiveDeref( ddOld, bCof1 );
    }
    if ( iCof == Abc_ObjFaninNum(pNode) )
        return NULL;
    // find unused variable
    bSupp = Cudd_Support( ddOld, fCof1Smaller? bCof1 : bCof0 );   Cudd_Ref( bSupp );
    iFreeVar = -1;
    for ( i = 0; i < Abc_ObjFaninNum(pNode); i++ )
    {
        assert( i == Cudd_ReadPerm(ddOld, i) );
        if ( i == iCof )
            continue;
        for ( bTemp = bSupp; !Cudd_IsConstant(bTemp); bTemp = cuddT(bTemp) )
            if ( i == (int)Cudd_NodeReadIndex(bTemp) )
                break;
        if ( Cudd_IsConstant(bTemp) )
        {
            iFreeVar = i;
            break;
        }
    }
    assert( iFreeVar != iCof && iFreeVar < Abc_ObjFaninNum(pNode) );
    Cudd_RecursiveDeref( ddOld, bSupp );
    // transfer the cofactors
    bCof0n = Extra_TransferLevelByLevel( ddOld, ddNew, bCof0 ); Cudd_Ref( bCof0n );
    bCof1n = Extra_TransferLevelByLevel( ddOld, ddNew, bCof1 ); Cudd_Ref( bCof1n );
    Cudd_RecursiveDeref( ddOld, bCof0 );
    Cudd_RecursiveDeref( ddOld, bCof1 );
    // create bottom node
    pNodeBot = Abc_NtkCreateNode( pNtkNew );
    for ( i = 0; i < Abc_ObjFaninNum(pNode); i++ )
        Abc_ObjAddFanin( pNodeBot, Abc_ObjFanin(pNode, i)->pCopy );
    pNodeBot->pData = fCof1Smaller? bCof0n : bCof1n;
    // create top node
    pNodeTop = Abc_NtkCreateNode( pNtkNew );
    for ( i = 0; i < Abc_ObjFaninNum(pNode); i++ )
        if ( i == iFreeVar )           
            Abc_ObjAddFanin( pNodeTop, pNodeBot );
        else
            Abc_ObjAddFanin( pNodeTop, Abc_ObjFanin(pNode, i)->pCopy );
    // derive the new function
    pNodeTop->pData = Cudd_bddIte( ddNew, 
        Cudd_bddIthVar(ddNew, iCof), 
        fCof1Smaller? bCof1n : Cudd_bddIthVar(ddNew, iFreeVar), 
        fCof1Smaller? Cudd_bddIthVar(ddNew, iFreeVar) : bCof0n );
571
    Cudd_Ref( (DdNode *)pNodeTop->pData );
Alan Mishchenko committed
572 573 574 575 576 577 578
    Cudd_RecursiveDeref( ddNew, fCof1Smaller? bCof1n : bCof0n );
    return pNodeTop;
}

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

  Synopsis    [Decompose the function once.]
Alan Mishchenko committed
579 580 581 582 583 584 585 586

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
587
Abc_Obj_t * Abc_NtkBddDecompose( Abc_Ntk_t * pNtkNew, Abc_Obj_t * pNode, int nLutSize, int fVerbose )
Alan Mishchenko committed
588
{
Alan Mishchenko committed
589
    Vec_Ptr_t * vCofs, * vUniq;
590
    DdManager * dd = (DdManager *)pNode->pNtk->pManFunc;
Alan Mishchenko committed
591 592 593
    DdNode * bCof;
    Abc_Obj_t * pNodeNew = NULL;
    Abc_Obj_t * pCofs[20];
Alan Mishchenko committed
594
    int i;
Alan Mishchenko committed
595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610
    assert( Abc_ObjFaninNum(pNode) > nLutSize );
    // try to decompose with two LUTs (the best case for Supp = LutSize + 1)
    if ( Abc_ObjFaninNum(pNode) == nLutSize + 1 )
    {

        pNodeNew = Abc_NtkBddFindCofactor( pNtkNew, pNode, nLutSize );
        if ( pNodeNew != NULL )
        {
            if ( fVerbose )
            printf( "Decomposing %d-input node %d using MUX.\n",
                Abc_ObjFaninNum(pNode), Abc_ObjId(pNode) );
            return pNodeNew;
        }

    }
    // cofactor w.r.t. the bound set variables
611
    vCofs = Abc_NtkBddCofactors( dd, (DdNode *)pNode->pData, nLutSize );
Alan Mishchenko committed
612 613 614 615 616 617
    vUniq = Vec_PtrDup( vCofs );
    Vec_PtrUniqify( vUniq, (int (*)())Vec_PtrSortCompare );
    // only perform decomposition with it is support reduring with two less vars
    if( Vec_PtrSize(vUniq) > (1 << (nLutSize-2)) )
    {
        Vec_PtrFree( vCofs );
618
        vCofs = Abc_NtkBddCofactors( dd, (DdNode *)pNode->pData, 2 );
Alan Mishchenko committed
619 620 621 622 623 624
        if ( fVerbose )
        printf( "Decomposing %d-input node %d using cofactoring with %d cofactors.\n",
            Abc_ObjFaninNum(pNode), Abc_ObjId(pNode), Vec_PtrSize(vCofs) );
        // implement the cofactors
        pCofs[0] = Abc_ObjFanin(pNode, 0)->pCopy;
        pCofs[1] = Abc_ObjFanin(pNode, 1)->pCopy;
625
        Vec_PtrForEachEntry( DdNode *, vCofs, bCof, i )
Alan Mishchenko committed
626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644
            pCofs[2+i] = Abc_NtkCreateCofLut( pNtkNew, dd, bCof, pNode, 2 );
        if ( nLutSize == 4 )
            pNodeNew = Abc_NtkBddMux412( pNtkNew, pCofs );
        else if ( nLutSize == 5 )
            pNodeNew = Abc_NtkBddMux412a( pNtkNew, pCofs );
        else if ( nLutSize == 6 )
            pNodeNew = Abc_NtkBddMux411( pNtkNew, pCofs );
        else  assert( 0 );
    }
    // alternative decompose using MUX-decomposition
    else
    {
        if ( fVerbose )
        printf( "Decomposing %d-input node %d using Curtis with %d unique columns.\n",
            Abc_ObjFaninNum(pNode), Abc_ObjId(pNode), Vec_PtrSize(vUniq) );
        pNodeNew = Abc_NtkBddCurtis( pNtkNew, pNode, vCofs, vUniq );
    }
    Vec_PtrFree( vCofs );
    Vec_PtrFree( vUniq );
Alan Mishchenko committed
645 646 647 648 649 650 651 652 653 654 655 656 657 658
    return pNodeNew;
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
659
void Abc_NtkLutminConstruct( Abc_Ntk_t * pNtkClp, Abc_Ntk_t * pNtkDec, int nLutSize, int fVerbose )
Alan Mishchenko committed
660
{
Alan Mishchenko committed
661 662 663 664
    Vec_Ptr_t * vNodes;
    Abc_Obj_t * pNode, * pFanin;
    int i, k;
    vNodes = Abc_NtkDfs( pNtkClp, 0 );
665
    Vec_PtrForEachEntry( Abc_Obj_t *, vNodes, pNode, i )
Alan Mishchenko committed
666
    {
Alan Mishchenko committed
667 668 669 670 671 672
        if ( Abc_ObjFaninNum(pNode) <= nLutSize )
        {
            pNode->pCopy = Abc_NtkDupObj( pNtkDec, pNode, 0 );
            Abc_ObjForEachFanin( pNode, pFanin, k )
                Abc_ObjAddFanin( pNode->pCopy, pFanin->pCopy );
        }
Alan Mishchenko committed
673
        else
Alan Mishchenko committed
674
            pNode->pCopy = Abc_NtkBddDecompose( pNtkDec, pNode, nLutSize, fVerbose );
Alan Mishchenko committed
675
    }
Alan Mishchenko committed
676
    Vec_PtrFree( vNodes );
Alan Mishchenko committed
677 678 679 680 681 682 683 684 685 686 687 688 689
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
690
Abc_Ntk_t * Abc_NtkLutminInt( Abc_Ntk_t * pNtk, int nLutSize, int fVerbose )
Alan Mishchenko committed
691 692
{
    extern void Abc_NtkBddReorder( Abc_Ntk_t * pNtk, int fVerbose );
Alan Mishchenko committed
693 694 695 696
    Abc_Ntk_t * pNtkDec;
    // minimize BDDs
//    Abc_NtkBddReorder( pNtk, fVerbose );
    Abc_NtkBddReorder( pNtk, 0 );
Alan Mishchenko committed
697
    // decompose one output at a time
Alan Mishchenko committed
698
    pNtkDec = Abc_NtkStartFrom( pNtk, ABC_NTK_LOGIC, ABC_FUNC_BDD );
Alan Mishchenko committed
699
    // make sure the new manager has enough inputs
700
    Cudd_bddIthVar( (DdManager *)pNtkDec->pManFunc, Abc_NtkGetFaninMax(pNtk) );
Alan Mishchenko committed
701
    // put the results into the new network (save new CO drivers in old CO drivers)
Alan Mishchenko committed
702
    Abc_NtkLutminConstruct( pNtk, pNtkDec, nLutSize, fVerbose );
Alan Mishchenko committed
703
    // finalize the new network
Alan Mishchenko committed
704
    Abc_NtkFinalize( pNtk, pNtkDec );
Alan Mishchenko committed
705 706
    // make the network minimum base
    Abc_NtkMinimumBase( pNtkDec );
Alan Mishchenko committed
707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722
    return pNtkDec;
}

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

  Synopsis    [Performs minimum-LUT decomposition of the network.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Abc_Ntk_t * Abc_NtkLutmin( Abc_Ntk_t * pNtkInit, int nLutSize, int fVerbose )
{
723
    extern int Abc_NtkFraigSweep( Abc_Ntk_t * pNtk, int fUseInv, int fExdc, int fVerbose, int fVeryVerbose );
Alan Mishchenko committed
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
    Abc_Ntk_t * pNtkNew, * pTemp;
    int i;
    if ( nLutSize < 4 )
    {
        printf( "The LUT count (%d) should be at least 4.\n", nLutSize );
        return NULL;
    }
    if ( nLutSize > 6 )
    {
        printf( "The LUT count (%d) should not exceed 6.\n", nLutSize );
        return NULL;
    }
    // create internal representation
    if ( Abc_NtkIsStrash(pNtkInit) )
        pNtkNew = Abc_NtkDup( pNtkInit );
    else
        pNtkNew = Abc_NtkStrash( pNtkInit, 0, 1, 0 );
    // collapse the network 
    pNtkNew = Abc_NtkCollapse( pTemp = pNtkNew, 10000, 0, 1, 0 );
    Abc_NtkDelete( pTemp );
    if ( pNtkNew == NULL )
        return NULL;
    // convert it to BDD
    if ( !Abc_NtkIsBddLogic(pNtkNew) )
        Abc_NtkToBdd( pNtkNew );
    // iterate decomposition
    for ( i = 0; Abc_NtkGetFaninMax(pNtkNew) > nLutSize; i++ )
    {
        if ( fVerbose )
            printf( "*** Iteration %d:\n", i+1 );
        if ( fVerbose )
            printf( "Decomposing network with %d nodes and %d max fanin count for K = %d.\n", 
                Abc_NtkNodeNum(pNtkNew), Abc_NtkGetFaninMax(pNtkNew), nLutSize );
        pNtkNew = Abc_NtkLutminInt( pTemp = pNtkNew, nLutSize, fVerbose );
        Abc_NtkDelete( pTemp );
    }
Alan Mishchenko committed
760
    // fix the problem with complemented and duplicated CO edges
Alan Mishchenko committed
761 762 763
    Abc_NtkLogicMakeSimpleCos( pNtkNew, 0 );
    // merge functionally equivalent nodes
    Abc_NtkFraigSweep( pNtkNew, 1, 0, 0, 0 );
Alan Mishchenko committed
764
    // make sure everything is okay
Alan Mishchenko committed
765
    if ( !Abc_NtkCheck( pNtkNew ) )
Alan Mishchenko committed
766 767 768 769
    {
        printf( "Abc_NtkLutmin: The network check has failed.\n" );
        return 0;
    }
Alan Mishchenko committed
770
    return pNtkNew;
Alan Mishchenko committed
771 772
}

773 774 775 776 777 778
#else

Abc_Ntk_t * Abc_NtkLutmin( Abc_Ntk_t * pNtkInit, int nLutSize, int fVerbose ) { return NULL; }

#endif

Alan Mishchenko committed
779 780 781 782 783
////////////////////////////////////////////////////////////////////////
///                       END OF FILE                                ///
////////////////////////////////////////////////////////////////////////


784 785
ABC_NAMESPACE_IMPL_END