abcDsd.c 18 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    [abcDsd.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [Network and node package.]

  Synopsis    [Decomposes the network using disjoint-support decomposition.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

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

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

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

21 22 23
#include "base/abc/abc.h"
#include "misc/extra/extraBdd.h"
#include "bdd/dsd/dsd.h"
Alan Mishchenko committed
24

25 26 27
ABC_NAMESPACE_IMPL_START


Alan Mishchenko committed
28 29 30 31
////////////////////////////////////////////////////////////////////////
///                        DECLARATIONS                              ///
////////////////////////////////////////////////////////////////////////
 
32
static Abc_Ntk_t *     Abc_NtkDsdInternal( Abc_Ntk_t * pNtk, int fVerbose, int fPrint, int fShort );
Alan Mishchenko committed
33
static void            Abc_NtkDsdConstruct( Dsd_Manager_t * pManDsd, Abc_Ntk_t * pNtk, Abc_Ntk_t * pNtkNew );
Alan Mishchenko committed
34
static Abc_Obj_t *     Abc_NtkDsdConstructNode( Dsd_Manager_t * pManDsd, Dsd_Node_t * pNodeDsd, Abc_Ntk_t * pNtkNew, int * pCounters );
Alan Mishchenko committed
35 36

static Vec_Ptr_t *     Abc_NtkCollectNodesForDsd( Abc_Ntk_t * pNtk );
37 38
static void            Abc_NodeDecompDsdAndMux( Abc_Obj_t * pNode, Vec_Ptr_t * vNodes, Dsd_Manager_t * pManDsd, int fRecursive, int * pCounters );
static int            Abc_NodeIsForDsd( Abc_Obj_t * pNode );
Alan Mishchenko committed
39 40
static int             Abc_NodeFindMuxVar( DdManager * dd, DdNode * bFunc, int nVars );

Alan Mishchenko committed
41
////////////////////////////////////////////////////////////////////////
Alan Mishchenko committed
42
///                     FUNCTION DEFINITIONS                         ///
Alan Mishchenko committed
43 44 45 46
////////////////////////////////////////////////////////////////////////

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

Alan Mishchenko committed
47 48 49 50 51 52 53 54 55 56 57 58 59
  Synopsis    [Derives the DSD network.]

  Description [Takes the strashed network (pNtk), derives global BDDs for
  the combinational outputs of this network, and decomposes these BDDs using
  disjoint support decomposition. Finally, constructs and return a new 
  network, which is topologically equivalent to the decomposition tree.
  Allocates and frees a new BDD manager and a new DSD manager.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
60
Abc_Ntk_t * Abc_NtkDsdGlobal( Abc_Ntk_t * pNtk, int fVerbose, int fPrint, int fShort )
Alan Mishchenko committed
61
{
Alan Mishchenko committed
62
    DdManager * dd;
Alan Mishchenko committed
63
    Abc_Ntk_t * pNtkNew;
Alan Mishchenko committed
64
    assert( Abc_NtkIsStrash(pNtk) );
65
    dd = (DdManager *)Abc_NtkBuildGlobalBdds( pNtk, 10000000, 1, 1, fVerbose );
Alan Mishchenko committed
66
    if ( dd == NULL )
Alan Mishchenko committed
67 68
        return NULL;
    if ( fVerbose )
Alan Mishchenko committed
69
        printf( "Shared BDD size = %6d nodes.\n", Cudd_ReadKeys(dd) - Cudd_ReadDead(dd) );
Alan Mishchenko committed
70
    // transform the result of mapping into a BDD network
Alan Mishchenko committed
71
    pNtkNew = Abc_NtkDsdInternal( pNtk, fVerbose, fPrint, fShort );
Alan Mishchenko committed
72
    Extra_StopManager( dd );
Alan Mishchenko committed
73 74
    if ( pNtkNew == NULL )
        return NULL;
Alan Mishchenko committed
75 76 77
    // copy EXDC network
    if ( pNtk->pExdc )
        pNtkNew->pExdc = Abc_NtkDup( pNtk->pExdc );
Alan Mishchenko committed
78
    if ( !Abc_NtkCheck( pNtkNew ) )
Alan Mishchenko committed
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
    {
        printf( "Abc_NtkDsdGlobal: The network check has failed.\n" );
        Abc_NtkDelete( pNtkNew );
        return NULL;
    }
    return pNtkNew;
}

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

  Synopsis    [Constructs the decomposed network.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
98
Abc_Ntk_t * Abc_NtkDsdInternal( Abc_Ntk_t * pNtk, int fVerbose, int fPrint, int fShort )
Alan Mishchenko committed
99
{
Alan Mishchenko committed
100 101
    char ** ppNamesCi, ** ppNamesCo;
    Vec_Ptr_t * vFuncsGlob;
Alan Mishchenko committed
102 103
    Dsd_Manager_t * pManDsd;
    Abc_Ntk_t * pNtkNew;
Alan Mishchenko committed
104
    DdManager * dd;
Alan Mishchenko committed
105 106 107 108
    Abc_Obj_t * pObj;
    int i;

    // complement the global functions
Alan Mishchenko committed
109
    vFuncsGlob = Vec_PtrAlloc( Abc_NtkCoNum(pNtk) );
Alan Mishchenko committed
110
    Abc_NtkForEachCo( pNtk, pObj, i )
Alan Mishchenko committed
111
        Vec_PtrPush( vFuncsGlob, Cudd_NotCond(Abc_ObjGlobalBdd(pObj), Abc_ObjFaninC0(pObj)) );
Alan Mishchenko committed
112 113

    // perform the decomposition
114
    dd = (DdManager *)Abc_NtkGlobalBddMan(pNtk);
Alan Mishchenko committed
115
    pManDsd = Dsd_ManagerStart( dd, Abc_NtkCiNum(pNtk), fVerbose );
Alan Mishchenko committed
116
    if ( pManDsd == NULL )
Alan Mishchenko committed
117
    {
118
        Vec_PtrFree( vFuncsGlob );
Alan Mishchenko committed
119
        Cudd_Quit( dd );
Alan Mishchenko committed
120
        return NULL;
Alan Mishchenko committed
121
    }
122 123 124
    Dsd_Decompose( pManDsd, (DdNode **)vFuncsGlob->pArray, Abc_NtkCoNum(pNtk) );
    Vec_PtrFree( vFuncsGlob );
    Abc_NtkFreeGlobalBdds( pNtk, 0 );
Alan Mishchenko committed
125 126

    // start the new network
Alan Mishchenko committed
127
    pNtkNew = Abc_NtkStartFrom( pNtk, ABC_NTK_LOGIC, ABC_FUNC_BDD );
Alan Mishchenko committed
128
    // make sure the new manager has enough inputs
129
    Cudd_bddIthVar( (DdManager *)pNtkNew->pManFunc, dd->size-1 );
Alan Mishchenko committed
130 131 132 133
    // put the results into the new network (save new CO drivers in old CO drivers)
    Abc_NtkDsdConstruct( pManDsd, pNtk, pNtkNew );
    // finalize the new network
    Abc_NtkFinalize( pNtk, pNtkNew );
Alan Mishchenko committed
134 135
    // fix the problem with complemented and duplicated CO edges
    Abc_NtkLogicMakeSimpleCos( pNtkNew, 0 );
Alan Mishchenko committed
136
    if ( fPrint )
Alan Mishchenko committed
137 138 139 140
    {
        ppNamesCi = Abc_NtkCollectCioNames( pNtk, 0 );
        ppNamesCo = Abc_NtkCollectCioNames( pNtk, 1 );
        Dsd_TreePrint( stdout, pManDsd, ppNamesCi, ppNamesCo, fShort, -1 );
Alan Mishchenko committed
141 142
        ABC_FREE( ppNamesCi );
        ABC_FREE( ppNamesCo );
Alan Mishchenko committed
143
    }
Alan Mishchenko committed
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164

    // stop the DSD manager
    Dsd_ManagerStop( pManDsd );
    return pNtkNew;
}

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

  Synopsis    [Constructs the decomposed network.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Abc_NtkDsdConstruct( Dsd_Manager_t * pManDsd, Abc_Ntk_t * pNtk, Abc_Ntk_t * pNtkNew )
{
    Dsd_Node_t ** ppNodesDsd;
    Dsd_Node_t * pNodeDsd;
Alan Mishchenko committed
165
    Abc_Obj_t * pNode, * pNodeNew, * pDriver;
Alan Mishchenko committed
166 167 168
    int i, nNodesDsd;

    // save the CI nodes in the DSD nodes
Alan Mishchenko committed
169 170
    Abc_AigConst1(pNtk)->pCopy = pNodeNew = Abc_NtkCreateNodeConst1(pNtkNew);
    Dsd_NodeSetMark( Dsd_ManagerReadConst1(pManDsd), (int)(ABC_PTRINT_T)pNodeNew );
Alan Mishchenko committed
171 172 173
    Abc_NtkForEachCi( pNtk, pNode, i )
    {
        pNodeDsd = Dsd_ManagerReadInput( pManDsd, i );
Alan Mishchenko committed
174
        Dsd_NodeSetMark( pNodeDsd, (int)(ABC_PTRINT_T)pNode->pCopy );
Alan Mishchenko committed
175 176 177 178 179
    }

    // collect DSD nodes in DFS order (leaves and const1 are not collected)
    ppNodesDsd = Dsd_TreeCollectNodesDfs( pManDsd, &nNodesDsd );
    for ( i = 0; i < nNodesDsd; i++ )
Alan Mishchenko committed
180
        Abc_NtkDsdConstructNode( pManDsd, ppNodesDsd[i], pNtkNew, NULL );
Alan Mishchenko committed
181
    ABC_FREE( ppNodesDsd );
Alan Mishchenko committed
182 183 184 185 186 187 188

    // set the pointers to the CO drivers
    Abc_NtkForEachCo( pNtk, pNode, i )
    {
        pDriver = Abc_ObjFanin0( pNode );
        if ( !Abc_ObjIsNode(pDriver) )
            continue;
Alan Mishchenko committed
189
        if ( !Abc_AigNodeIsAnd(pDriver) )
Alan Mishchenko committed
190
            continue;
Alan Mishchenko committed
191
        pNodeDsd = Dsd_ManagerReadRoot( pManDsd, i );
Alan Mishchenko committed
192
        pNodeNew = (Abc_Obj_t *)(ABC_PTRINT_T)Dsd_NodeReadMark( Dsd_Regular(pNodeDsd) );
Alan Mishchenko committed
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
        assert( !Abc_ObjIsComplement(pNodeNew) );
        pDriver->pCopy = Abc_ObjNotCond( pNodeNew, Dsd_IsComplement(pNodeDsd) );
    }
}

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

  Synopsis    [Performs DSD using the manager.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
209
Abc_Obj_t * Abc_NtkDsdConstructNode( Dsd_Manager_t * pManDsd, Dsd_Node_t * pNodeDsd, Abc_Ntk_t * pNtkNew, int * pCounters )
Alan Mishchenko committed
210 211
{
    DdManager * ddDsd = Dsd_ManagerReadDd( pManDsd );
212
    DdManager * ddNew = (DdManager *)pNtkNew->pManFunc;
Alan Mishchenko committed
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
    Dsd_Node_t * pFaninDsd;
    Abc_Obj_t * pNodeNew, * pFanin;
    DdNode * bLocal, * bTemp, * bVar;
    Dsd_Type_t Type;
    int i, nDecs;

    // create the new node
    pNodeNew = Abc_NtkCreateNode( pNtkNew );
    // add the fanins
    Type  = Dsd_NodeReadType( pNodeDsd );
    nDecs = Dsd_NodeReadDecsNum( pNodeDsd );
    assert( nDecs > 1 );
    for ( i = 0; i < nDecs; i++ )
    {
        pFaninDsd  = Dsd_NodeReadDec( pNodeDsd, i );
Alan Mishchenko committed
228
        pFanin     = (Abc_Obj_t *)(ABC_PTRINT_T)Dsd_NodeReadMark(Dsd_Regular(pFaninDsd));
Alan Mishchenko committed
229 230 231 232 233
        Abc_ObjAddFanin( pNodeNew, pFanin );
        assert( Type == DSD_NODE_OR || !Dsd_IsComplement(pFaninDsd) );
    }

    // create the local function depending on the type of the node
234
    ddNew = (DdManager *)pNtkNew->pManFunc;
Alan Mishchenko committed
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
    switch ( Type )
    {
        case DSD_NODE_CONST1:
        {
            bLocal = ddNew->one; Cudd_Ref( bLocal );
            break;
        }
        case DSD_NODE_OR:
        {
            bLocal = Cudd_Not(ddNew->one); Cudd_Ref( bLocal );
            for ( i = 0; i < nDecs; i++ )
            {
                pFaninDsd  = Dsd_NodeReadDec( pNodeDsd, i );
                bVar   = Cudd_NotCond( ddNew->vars[i], Dsd_IsComplement(pFaninDsd) );
                bLocal = Cudd_bddOr( ddNew, bTemp = bLocal, bVar );               Cudd_Ref( bLocal );
                Cudd_RecursiveDeref( ddNew, bTemp );
            }
            break;
        }
        case DSD_NODE_EXOR:
        {
            bLocal = Cudd_Not(ddNew->one); Cudd_Ref( bLocal );
            for ( i = 0; i < nDecs; i++ )
            {
                bLocal = Cudd_bddXor( ddNew, bTemp = bLocal, ddNew->vars[i] );    Cudd_Ref( bLocal );
                Cudd_RecursiveDeref( ddNew, bTemp );
            }
            break;
        }
        case DSD_NODE_PRIME:
        {
Alan Mishchenko committed
266 267 268 269 270 271 272
            if ( pCounters )
            {
                if ( nDecs < 10 )
                    pCounters[nDecs]++;
                else
                    pCounters[10]++;
            }
Alan Mishchenko committed
273 274
            bLocal = Dsd_TreeGetPrimeFunction( ddDsd, pNodeDsd );                Cudd_Ref( bLocal );
            bLocal = Extra_TransferLevelByLevel( ddDsd, ddNew, bTemp = bLocal ); Cudd_Ref( bLocal );
Alan Mishchenko committed
275 276 277 278 279 280 281
/*
if ( nDecs == 3 )
{
Extra_bddPrint( ddDsd, bTemp );
printf( "\n" );
}
*/
Alan Mishchenko committed
282 283 284 285 286 287 288 289 290 291 292
            Cudd_RecursiveDeref( ddDsd, bTemp );
            // bLocal is now in the new BDD manager
            break;
        }
        default:
        {
            assert( 0 );
            break;
        }
    }
    pNodeNew->pData = bLocal;
Alan Mishchenko committed
293
    Dsd_NodeSetMark( pNodeDsd, (int)(ABC_PTRINT_T)pNodeNew );
Alan Mishchenko committed
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
    return pNodeNew;
}






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

  Synopsis    [Recursively decomposes internal nodes.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
313
int Abc_NtkDsdLocal( Abc_Ntk_t * pNtk, int fVerbose, int fRecursive )
Alan Mishchenko committed
314 315
{
    Dsd_Manager_t * pManDsd;
316
    DdManager * dd = (DdManager *)pNtk->pManFunc;
Alan Mishchenko committed
317 318
    Vec_Ptr_t * vNodes;
    int i;
Alan Mishchenko committed
319
    int pCounters[11] = {0};
Alan Mishchenko committed
320

Alan Mishchenko committed
321
    assert( Abc_NtkIsBddLogic(pNtk) );
Alan Mishchenko committed
322 323 324 325 326 327 328 329 330 331

    // make the network minimum base
    Abc_NtkMinimumBase( pNtk );

    // start the DSD manager
    pManDsd = Dsd_ManagerStart( dd, dd->size, 0 );

    // collect nodes for decomposition
    vNodes = Abc_NtkCollectNodesForDsd( pNtk );
    for ( i = 0; i < vNodes->nSize; i++ )
332
        Abc_NodeDecompDsdAndMux( (Abc_Obj_t *)vNodes->pArray[i], vNodes, pManDsd, fRecursive, pCounters );
Alan Mishchenko committed
333 334
    Vec_PtrFree( vNodes );

335 336 337 338 339 340 341
    if ( fVerbose )
    {
        printf( "Number of non-decomposable functions:\n" );
        for ( i = 3; i < 10; i++ )
            printf( "Inputs = %d.  Functions = %6d.\n", i, pCounters[i] );
        printf( "Inputs > %d.  Functions = %6d.\n", 9, pCounters[10] );
    }
Alan Mishchenko committed
342

Alan Mishchenko committed
343 344 345 346
    // stop the DSD manager
    Dsd_ManagerStop( pManDsd );

    // make sure everything is okay
Alan Mishchenko committed
347
    if ( !Abc_NtkCheck( pNtk ) )
Alan Mishchenko committed
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
    {
        printf( "Abc_NtkDsdRecursive: The network check has failed.\n" );
        return 0;
    }
    return 1;
}

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

  Synopsis    [Collects the nodes that may need decomposition.]

  Description [The nodes that do not need decomposition are those
  whose BDD has more internal nodes than the support size.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Vec_Ptr_t * Abc_NtkCollectNodesForDsd( Abc_Ntk_t * pNtk )
{
    Vec_Ptr_t * vNodes;
    Abc_Obj_t * pNode;
    int i;
    vNodes = Vec_PtrAlloc( 100 );
    Abc_NtkForEachNode( pNtk, pNode, i )
    {
        if ( Abc_NodeIsForDsd(pNode) )
            Vec_PtrPush( vNodes, pNode );
    }
    return vNodes;
}

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

  Synopsis    [Performs decomposition of one node.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
392
void Abc_NodeDecompDsdAndMux( Abc_Obj_t * pNode, Vec_Ptr_t * vNodes, Dsd_Manager_t * pManDsd, int fRecursive, int * pCounters )
Alan Mishchenko committed
393
{
394
    DdManager * dd = (DdManager *)pNode->pNtk->pManFunc;
Alan Mishchenko committed
395
    Abc_Obj_t * pRoot = NULL, * pFanin, * pNode1, * pNode2, * pNodeC;
Alan Mishchenko committed
396 397 398 399
    Dsd_Node_t ** ppNodesDsd, * pNodeDsd, * pFaninDsd;
    int i, nNodesDsd, iVar, fCompl;

    // try disjoint support decomposition
400
    pNodeDsd = Dsd_DecomposeOne( pManDsd, (DdNode *)pNode->pData );
Alan Mishchenko committed
401 402 403 404
    fCompl   = Dsd_IsComplement( pNodeDsd );
    pNodeDsd = Dsd_Regular( pNodeDsd );

    // determine what decomposition to use   
Alan Mishchenko committed
405
    if ( !fRecursive || Dsd_NodeReadDecsNum(pNodeDsd) != Abc_ObjFaninNum(pNode) )
Alan Mishchenko committed
406 407 408 409 410 411
    { // perform DSD

        // set the inputs
        Abc_ObjForEachFanin( pNode, pFanin, i )
        {
            pFaninDsd = Dsd_ManagerReadInput( pManDsd, i );
Alan Mishchenko committed
412
            Dsd_NodeSetMark( pFaninDsd, (int)(ABC_PTRINT_T)pFanin );
Alan Mishchenko committed
413 414 415 416 417 418
        }

        // construct the intermediate nodes
        ppNodesDsd = Dsd_TreeCollectNodesDfsOne( pManDsd, pNodeDsd, &nNodesDsd );
        for ( i = 0; i < nNodesDsd; i++ )
        {
Alan Mishchenko committed
419
            pRoot = Abc_NtkDsdConstructNode( pManDsd, ppNodesDsd[i], pNode->pNtk, pCounters );
Alan Mishchenko committed
420
            if ( Abc_NodeIsForDsd(pRoot) && fRecursive )
Alan Mishchenko committed
421 422
                Vec_PtrPush( vNodes, pRoot );
        }
Alan Mishchenko committed
423
        ABC_FREE( ppNodesDsd );
Alan Mishchenko committed
424
        assert(pRoot);
Alan Mishchenko committed
425 426 427 428 429 430

        // remove the current fanins
        Abc_ObjRemoveFanins( pNode );
        // add fanin to the root
        Abc_ObjAddFanin( pNode, pRoot );
        // update the function to be that of buffer
431 432
        Cudd_RecursiveDeref( dd, (DdNode *)pNode->pData );
        pNode->pData = Cudd_NotCond( (DdNode *)dd->vars[0], fCompl ); Cudd_Ref( (DdNode *)pNode->pData );
Alan Mishchenko committed
433 434 435 436
    }
    else // perform MUX-decomposition
    {
        // get the cofactoring variable
437
        iVar = Abc_NodeFindMuxVar( dd, (DdNode *)pNode->pData, Abc_ObjFaninNum(pNode) );
Alan Mishchenko committed
438 439 440
        pNodeC = Abc_ObjFanin( pNode, iVar );

        // get the negative cofactor
Alan Mishchenko committed
441
        pNode1 = Abc_NtkCloneObj( pNode );
442
        pNode1->pData = Cudd_Cofactor( dd, (DdNode *)pNode->pData, Cudd_Not(dd->vars[iVar]) );  Cudd_Ref( (DdNode *)pNode1->pData );
Alan Mishchenko committed
443 444 445 446 447
        Abc_NodeMinimumBase( pNode1 );
        if ( Abc_NodeIsForDsd(pNode1) )
            Vec_PtrPush( vNodes, pNode1 );

        // get the positive cofactor
Alan Mishchenko committed
448
        pNode2 = Abc_NtkCloneObj( pNode );
449
        pNode2->pData = Cudd_Cofactor( dd, (DdNode *)pNode->pData, dd->vars[iVar] );            Cudd_Ref( (DdNode *)pNode2->pData );
Alan Mishchenko committed
450 451 452 453 454 455 456 457 458 459 460
        Abc_NodeMinimumBase( pNode2 );
        if ( Abc_NodeIsForDsd(pNode2) )
            Vec_PtrPush( vNodes, pNode2 );

        // remove the current fanins
        Abc_ObjRemoveFanins( pNode );
        // add new fanins
        Abc_ObjAddFanin( pNode, pNodeC );
        Abc_ObjAddFanin( pNode, pNode2 );
        Abc_ObjAddFanin( pNode, pNode1 );
        // update the function to be that of MUX
461 462
        Cudd_RecursiveDeref( dd, (DdNode *)pNode->pData );
        pNode->pData = Cudd_bddIte( dd, dd->vars[0], dd->vars[1], dd->vars[2] );    Cudd_Ref( (DdNode *)pNode->pData );
Alan Mishchenko committed
463 464 465 466 467
    }
}

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

Alan Mishchenko committed
468
  Synopsis    [Checks if the node should be decomposed by DSD.]
Alan Mishchenko committed
469 470 471 472 473 474 475 476

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
477
int Abc_NodeIsForDsd( Abc_Obj_t * pNode )
Alan Mishchenko committed
478
{
Alan Mishchenko committed
479
//    DdManager * dd = pNode->pNtk->pManFunc;
Alan Mishchenko committed
480
//    DdNode * bFunc, * bFunc0, * bFunc1;
Alan Mishchenko committed
481
    assert( Abc_ObjIsNode(pNode) );
Alan Mishchenko committed
482 483 484 485
//    if ( Cudd_DagSize(pNode->pData)-1 > Abc_ObjFaninNum(pNode) )
//        return 1;
//    return 0;

Alan Mishchenko committed
486 487
/*
    // this does not catch things like a(b+c), which should be decomposed
Alan Mishchenko committed
488 489 490 491 492 493 494 495 496 497 498
    for ( bFunc = Cudd_Regular(pNode->pData); !cuddIsConstant(bFunc); )
    {
        bFunc0 = Cudd_Regular( cuddE(bFunc) );
        bFunc1 = cuddT(bFunc);
        if ( bFunc0 == b1 )
            bFunc = bFunc1;
        else if ( bFunc1 == b1 || bFunc0 == bFunc1 )
            bFunc = bFunc0;
        else
            return 1;
    }
Alan Mishchenko committed
499 500 501
*/
    if ( Abc_ObjFaninNum(pNode) > 2 )
        return 1;
Alan Mishchenko committed
502 503 504 505 506 507 508 509 510 511 512 513 514 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
    return 0;
}

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

  Synopsis    [Determines a cofactoring variable.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Abc_NodeFindMuxVar( DdManager * dd, DdNode * bFunc, int nVars )
{
    DdNode * bVar, * bCof0, * bCof1;
    int SuppSumMin = 1000000;
    int i, nSSD, nSSQ, iVar;

//    printf( "\n\nCofactors:\n\n" );
    iVar = -1;
    for ( i = 0; i < nVars; i++ )
    {
        bVar = dd->vars[i];

        bCof0 = Cudd_Cofactor( dd, bFunc, Cudd_Not(bVar) );  Cudd_Ref( bCof0 );
        bCof1 = Cudd_Cofactor( dd, bFunc,          bVar  );  Cudd_Ref( bCof1 );

//        nodD = Cudd_DagSize(bCof0);
//        nodQ = Cudd_DagSize(bCof1);
//        printf( "+%02d: D=%2d. Q=%2d.  ", i, nodD, nodQ );
//        printf( "S=%2d. D=%2d.  ", nodD + nodQ, abs(nodD-nodQ) );

        nSSD = Cudd_SupportSize( dd, bCof0 );
        nSSQ = Cudd_SupportSize( dd, bCof1 );

//        printf( "SD=%2d. SQ=%2d.  ", nSSD, nSSQ );
//        printf( "S=%2d. D=%2d.  ", nSSD + nSSQ, abs(nSSD - nSSQ) );
//        printf( "Cost=%3d. ", Cost(nodD,nodQ,nSSD,nSSQ) );
//        printf( "\n" );

        Cudd_RecursiveDeref( dd, bCof0 );
        Cudd_RecursiveDeref( dd, bCof1 );
Alan Mishchenko committed
546

Alan Mishchenko committed
547 548 549 550 551 552 553 554
        if ( SuppSumMin > nSSD + nSSQ )
        {
             SuppSumMin = nSSD + nSSQ;
             iVar = i;
        }
    }
    return iVar;
}
Alan Mishchenko committed
555

Alan Mishchenko committed
556

Alan Mishchenko committed
557 558 559 560 561
////////////////////////////////////////////////////////////////////////
///                       END OF FILE                                ///
////////////////////////////////////////////////////////////////////////


562 563
ABC_NAMESPACE_IMPL_END