abcNtbdd.c 21.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
/**CFile****************************************************************

  FileName    [abcNtbdd.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [Network and node package.]

  Synopsis    [Procedures to translate between the BDD and the network.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

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

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

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

21
#include "base/abc/abc.h"
22 23 24
#include "aig/saig/saig.h"

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

28 29 30
ABC_NAMESPACE_IMPL_START


Alan Mishchenko committed
31 32 33 34
////////////////////////////////////////////////////////////////////////
///                        DECLARATIONS                              ///
////////////////////////////////////////////////////////////////////////

35 36
#ifdef ABC_USE_CUDD

Alan Mishchenko committed
37
static void        Abc_NtkBddToMuxesPerform( Abc_Ntk_t * pNtk, Abc_Ntk_t * pNtkNew );
Alan Mishchenko committed
38
static Abc_Obj_t * Abc_NodeBddToMuxes( Abc_Obj_t * pNodeOld, Abc_Ntk_t * pNtkNew );
39
static Abc_Obj_t * Abc_NodeBddToMuxes_rec( DdManager * dd, DdNode * bFunc, Abc_Ntk_t * pNtkNew, st__table * tBdd2Node );
Alan Mishchenko committed
40
static DdNode *    Abc_NodeGlobalBdds_rec( DdManager * dd, Abc_Obj_t * pNode, int nBddSizeMax, int fDropInternal, ProgressBar * pProgress, int * pCounter, int fVerbose );
Alan Mishchenko committed
41 42

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

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

  Synopsis    [Constructs the network isomorphic to the given BDD.]

  Description [Assumes that the BDD depends on the variables whose indexes
  correspond to the names in the array (pNamesPi). Otherwise, returns NULL.
  The resulting network comes with one node, whose functionality is
  equal to the given BDD. To decompose this BDD into the network of
  multiplexers use Abc_NtkBddToMuxes(). To decompose this BDD into
  an And-Inverter Graph, use Abc_NtkStrash().]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
62
Abc_Ntk_t * Abc_NtkDeriveFromBdd( void * dd0, void * bFunc, char * pNamePo, Vec_Ptr_t * vNamesPi )
Alan Mishchenko committed
63
{
64
    DdManager * dd = (DdManager *)dd0;
Alan Mishchenko committed
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
    Abc_Ntk_t * pNtk; 
    Vec_Ptr_t * vNamesPiFake = NULL;
    Abc_Obj_t * pNode, * pNodePi, * pNodePo;
    DdNode * bSupp, * bTemp;
    char * pName;
    int i;

    // supply fake names if real names are not given
    if ( pNamePo == NULL )
        pNamePo = "F";
    if ( vNamesPi == NULL )
    {
        vNamesPiFake = Abc_NodeGetFakeNames( dd->size );
        vNamesPi = vNamesPiFake;
    }

    // make sure BDD depends on the variables whose index 
    // does not exceed the size of the array with PI names
83
    bSupp = Cudd_Support( dd, (DdNode *)bFunc );   Cudd_Ref( bSupp );
Alan Mishchenko committed
84 85 86 87 88 89 90 91
    for ( bTemp = bSupp; bTemp != Cudd_ReadOne(dd); bTemp = cuddT(bTemp) )
        if ( (int)Cudd_NodeReadIndex(bTemp) >= Vec_PtrSize(vNamesPi) )
            break;
    Cudd_RecursiveDeref( dd, bSupp );
    if ( bTemp != Cudd_ReadOne(dd) )
        return NULL;

    // start the network
Alan Mishchenko committed
92 93
    pNtk = Abc_NtkAlloc( ABC_NTK_LOGIC, ABC_FUNC_BDD, 1 );
    pNtk->pName = Extra_UtilStrsav(pNamePo);
Alan Mishchenko committed
94
    // make sure the new manager has enough inputs
95
    Cudd_bddIthVar( (DdManager *)pNtk->pManFunc, Vec_PtrSize(vNamesPi) );
Alan Mishchenko committed
96
    // add the PIs corresponding to the names
97
    Vec_PtrForEachEntry( char *, vNamesPi, pName, i )
Alan Mishchenko committed
98
        Abc_ObjAssignName( Abc_NtkCreatePi(pNtk), pName, NULL );
Alan Mishchenko committed
99 100
    // create the node
    pNode = Abc_NtkCreateNode( pNtk );
101
    pNode->pData = (DdNode *)Cudd_bddTransfer( dd, (DdManager *)pNtk->pManFunc, (DdNode *)bFunc ); Cudd_Ref((DdNode *)pNode->pData);
Alan Mishchenko committed
102 103 104 105 106
    Abc_NtkForEachPi( pNtk, pNodePi, i )
        Abc_ObjAddFanin( pNode, pNodePi );
    // create the only PO
    pNodePo = Abc_NtkCreatePo( pNtk );
    Abc_ObjAddFanin( pNodePo, pNode );
Alan Mishchenko committed
107
    Abc_ObjAssignName( pNodePo, pNamePo, NULL );
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
    // make the network minimum base
    Abc_NtkMinimumBase( pNtk );
    if ( vNamesPiFake )
        Abc_NodeFreeNames( vNamesPiFake );
    if ( !Abc_NtkCheck( pNtk ) )
        fprintf( stdout, "Abc_NtkDeriveFromBdd(): Network check has failed.\n" );
    return pNtk;
}



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

  Synopsis    [Creates the network isomorphic to the union of local BDDs of the nodes.]

  Description [The nodes of the local BDDs are converted into the network nodes 
  with logic functions equal to the MUX.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Abc_Ntk_t * Abc_NtkBddToMuxes( Abc_Ntk_t * pNtk )
{
    Abc_Ntk_t * pNtkNew;
    assert( Abc_NtkIsBddLogic(pNtk) );
Alan Mishchenko committed
135
    pNtkNew = Abc_NtkStartFrom( pNtk, ABC_NTK_LOGIC, ABC_FUNC_SOP );
Alan Mishchenko committed
136 137 138
    Abc_NtkBddToMuxesPerform( pNtk, pNtkNew );
    Abc_NtkFinalize( pNtk, pNtkNew );
    // make sure everything is okay
Alan Mishchenko committed
139
    if ( !Abc_NtkCheck( pNtkNew ) )
Alan Mishchenko committed
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
    {
        printf( "Abc_NtkBddToMuxes: The network check has failed.\n" );
        Abc_NtkDelete( pNtkNew );
        return NULL;
    }
    return pNtkNew;
}

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

  Synopsis    [Converts the network to MUXes.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Abc_NtkBddToMuxesPerform( Abc_Ntk_t * pNtk, Abc_Ntk_t * pNtkNew )
{
    ProgressBar * pProgress;
Alan Mishchenko committed
162
    Abc_Obj_t * pNode, * pNodeNew;
Alan Mishchenko committed
163 164 165 166 167
    Vec_Ptr_t * vNodes;
    int i;
    // perform conversion in the topological order
    vNodes = Abc_NtkDfs( pNtk, 0 );
    pProgress = Extra_ProgressBarStart( stdout, vNodes->nSize );
168
    Vec_PtrForEachEntry( Abc_Obj_t *, vNodes, pNode, i )
Alan Mishchenko committed
169 170 171 172
    {
        Extra_ProgressBarUpdate( pProgress, i, NULL );
        // convert one node
        assert( Abc_ObjIsNode(pNode) );
Alan Mishchenko committed
173
        pNodeNew = Abc_NodeBddToMuxes( pNode, pNtkNew );
Alan Mishchenko committed
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
        // mark the old node with the new one
        assert( pNode->pCopy == NULL );
        pNode->pCopy = pNodeNew;
    }
    Vec_PtrFree( vNodes );
    Extra_ProgressBarStop( pProgress );
}

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

  Synopsis    [Converts the node to MUXes.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
193
Abc_Obj_t * Abc_NodeBddToMuxes( Abc_Obj_t * pNodeOld, Abc_Ntk_t * pNtkNew )
Alan Mishchenko committed
194
{
195 196
    DdManager * dd = (DdManager *)pNodeOld->pNtk->pManFunc;
    DdNode * bFunc = (DdNode *)pNodeOld->pData;
Alan Mishchenko committed
197
    Abc_Obj_t * pFaninOld, * pNodeNew;
198
    st__table * tBdd2Node;
Alan Mishchenko committed
199 200
    int i;
    // create the table mapping BDD nodes into the ABC nodes
201
    tBdd2Node = st__init_table( st__ptrcmp, st__ptrhash );
Alan Mishchenko committed
202 203
    // add the constant and the elementary vars
    Abc_ObjForEachFanin( pNodeOld, pFaninOld, i )
204
        st__insert( tBdd2Node, (char *)Cudd_bddIthVar(dd, i), (char *)pFaninOld->pCopy );
Alan Mishchenko committed
205 206
    // create the new nodes recursively
    pNodeNew = Abc_NodeBddToMuxes_rec( dd, Cudd_Regular(bFunc), pNtkNew, tBdd2Node );
207
    st__free_table( tBdd2Node );
Alan Mishchenko committed
208
    if ( Cudd_IsComplement(bFunc) )
Alan Mishchenko committed
209
        pNodeNew = Abc_NtkCreateNodeInv( pNtkNew, pNodeNew );
Alan Mishchenko committed
210 211 212 213 214 215 216 217 218 219 220 221 222 223
    return pNodeNew;
}

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

  Synopsis    [Converts the node to MUXes.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
224
Abc_Obj_t * Abc_NodeBddToMuxes_rec( DdManager * dd, DdNode * bFunc, Abc_Ntk_t * pNtkNew, st__table * tBdd2Node )
Alan Mishchenko committed
225 226 227
{
    Abc_Obj_t * pNodeNew, * pNodeNew0, * pNodeNew1, * pNodeNewC;
    assert( !Cudd_IsComplement(bFunc) );
Alan Mishchenko committed
228 229
    if ( bFunc == b1 )
        return Abc_NtkCreateNodeConst1(pNtkNew);
230
    if ( st__lookup( tBdd2Node, (char *)bFunc, (char **)&pNodeNew ) )
Alan Mishchenko committed
231 232 233 234
        return pNodeNew;
    // solve for the children nodes
    pNodeNew0 = Abc_NodeBddToMuxes_rec( dd, Cudd_Regular(cuddE(bFunc)), pNtkNew, tBdd2Node );
    if ( Cudd_IsComplement(cuddE(bFunc)) )
Alan Mishchenko committed
235
        pNodeNew0 = Abc_NtkCreateNodeInv( pNtkNew, pNodeNew0 );
Alan Mishchenko committed
236
    pNodeNew1 = Abc_NodeBddToMuxes_rec( dd, cuddT(bFunc), pNtkNew, tBdd2Node );
237
    if ( ! st__lookup( tBdd2Node, (char *)Cudd_bddIthVar(dd, bFunc->index), (char **)&pNodeNewC ) )
Alan Mishchenko committed
238 239
        assert( 0 );
    // create the MUX node
Alan Mishchenko committed
240
    pNodeNew = Abc_NtkCreateNodeMux( pNtkNew, pNodeNewC, pNodeNew1, pNodeNew0 );
241
    st__insert( tBdd2Node, (char *)bFunc, (char *)pNodeNew );
Alan Mishchenko committed
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
    return pNodeNew;
}


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

  Synopsis    [Derives global BDDs for the COs of the network.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
257
void * Abc_NtkBuildGlobalBdds( Abc_Ntk_t * pNtk, int nBddSizeMax, int fDropInternal, int fReorder, int fVerbose )
Alan Mishchenko committed
258 259
{
    ProgressBar * pProgress;
Alan Mishchenko committed
260 261
    Abc_Obj_t * pObj, * pFanin;
    Vec_Att_t * pAttMan;
Alan Mishchenko committed
262
    DdManager * dd;
Alan Mishchenko committed
263 264 265 266
    DdNode * bFunc;
    int i, k, Counter;

    // remove dangling nodes
267
    Abc_AigCleanup( (Abc_Aig_t *)pNtk->pManFunc );
Alan Mishchenko committed
268 269

    // start the manager
Alan Mishchenko committed
270
    assert( Abc_NtkGlobalBdd(pNtk) == NULL );
Alan Mishchenko committed
271
    dd = Cudd_Init( Abc_NtkCiNum(pNtk), 0, CUDD_UNIQUE_SLOTS, CUDD_CACHE_SLOTS, 0 );
Alan Mishchenko committed
272
    pAttMan = Vec_AttAlloc( Abc_NtkObjNumMax(pNtk) + 1, dd, (void (*)(void*))Extra_StopManager, NULL, (void (*)(void*,void*))Cudd_RecursiveDeref );
Alan Mishchenko committed
273 274 275
    Vec_PtrWriteEntry( pNtk->vAttrs, VEC_ATTR_GLOBAL_BDD, pAttMan );

    // set reordering
Alan Mishchenko committed
276 277 278 279
    if ( fReorder )
        Cudd_AutodynEnable( dd, CUDD_REORDER_SYMM_SIFT );

    // assign the constant node BDD
Alan Mishchenko committed
280 281
    pObj = Abc_AigConst1(pNtk);
    if ( Abc_ObjFanoutNum(pObj) > 0 )
Alan Mishchenko committed
282
    {
Alan Mishchenko committed
283 284 285 286 287 288
        bFunc = dd->one;
        Abc_ObjSetGlobalBdd( pObj, bFunc );   Cudd_Ref( bFunc );
    }
    // set the elementary variables
    Abc_NtkForEachCi( pNtk, pObj, i )
        if ( Abc_ObjFanoutNum(pObj) > 0 )
Alan Mishchenko committed
289
        {
Alan Mishchenko committed
290 291 292
            bFunc = dd->vars[i];
//            bFunc = dd->vars[Abc_NtkCiNum(pNtk) - 1 - i];
            Abc_ObjSetGlobalBdd( pObj, bFunc );  Cudd_Ref( bFunc );
Alan Mishchenko committed
293
        }
Alan Mishchenko committed
294 295 296 297 298 299

    // collect the global functions of the COs
    Counter = 0;
    // construct the BDDs
    pProgress = Extra_ProgressBarStart( stdout, Abc_NtkNodeNum(pNtk) );
    Abc_NtkForEachCo( pNtk, pObj, i )
Alan Mishchenko committed
300
    {
Alan Mishchenko committed
301 302
        bFunc = Abc_NodeGlobalBdds_rec( dd, Abc_ObjFanin0(pObj), nBddSizeMax, fDropInternal, pProgress, &Counter, fVerbose );
        if ( bFunc == NULL )
Alan Mishchenko committed
303
        {
Alan Mishchenko committed
304 305 306
            if ( fVerbose )
            printf( "Constructing global BDDs is aborted.\n" );
            Abc_NtkFreeGlobalBdds( pNtk, 0 );
Alan Mishchenko committed
307 308 309 310 311 312 313 314 315 316
            Cudd_Quit( dd ); 

            // reset references
            Abc_NtkForEachObj( pNtk, pObj, i )
                if ( !Abc_ObjIsBox(pObj) && !Abc_ObjIsBi(pObj) )
                    pObj->vFanouts.nSize = 0;
            Abc_NtkForEachObj( pNtk, pObj, i )
                if ( !Abc_ObjIsBox(pObj) && !Abc_ObjIsBo(pObj) )
                    Abc_ObjForEachFanin( pObj, pFanin, k )
                        pFanin->vFanouts.nSize++;
Alan Mishchenko committed
317
            return NULL;
Alan Mishchenko committed
318
        }
319
        bFunc = Cudd_NotCond( bFunc, (int)Abc_ObjFaninC0(pObj) );  Cudd_Ref( bFunc ); 
Alan Mishchenko committed
320
        Abc_ObjSetGlobalBdd( pObj, bFunc );
Alan Mishchenko committed
321
    }
Alan Mishchenko committed
322
    Extra_ProgressBarStop( pProgress );
Alan Mishchenko committed
323

Alan Mishchenko committed
324
/*
Alan Mishchenko committed
325
    // derefence the intermediate BDDs
Alan Mishchenko committed
326 327
    Abc_NtkForEachNode( pNtk, pObj, i )
        if ( pObj->pCopy ) 
Alan Mishchenko committed
328
        {
Alan Mishchenko committed
329 330
            Cudd_RecursiveDeref( dd, (DdNode *)pObj->pCopy );
            pObj->pCopy = NULL;
Alan Mishchenko committed
331
        }
Alan Mishchenko committed
332 333 334 335 336 337 338 339 340 341 342 343 344
*/
/*
    // make sure all nodes are derefed
    Abc_NtkForEachObj( pNtk, pObj, i )
    {
        if ( pObj->pCopy != NULL )
            printf( "Abc_NtkBuildGlobalBdds() error: Node %d has BDD assigned\n", pObj->Id );
        if ( pObj->vFanouts.nSize > 0 )
            printf( "Abc_NtkBuildGlobalBdds() error: Node %d has refs assigned\n", pObj->Id );
    }
*/
    // reset references
    Abc_NtkForEachObj( pNtk, pObj, i )
Alan Mishchenko committed
345 346 347
        if ( !Abc_ObjIsBox(pObj) && !Abc_ObjIsBi(pObj) )
            pObj->vFanouts.nSize = 0;
    Abc_NtkForEachObj( pNtk, pObj, i )
Alan Mishchenko committed
348 349 350 351
        if ( !Abc_ObjIsBox(pObj) && !Abc_ObjIsBo(pObj) )
            Abc_ObjForEachFanin( pObj, pFanin, k )
                pFanin->vFanouts.nSize++;

Alan Mishchenko committed
352 353 354 355 356 357
    // reorder one more time
    if ( fReorder )
    {
        Cudd_ReduceHeap( dd, CUDD_REORDER_SYMM_SIFT, 1 );
        Cudd_AutodynDisable( dd );
    }
Alan Mishchenko committed
358
//    Cudd_PrintInfo( dd, stdout );
Alan Mishchenko committed
359 360 361 362 363 364 365 366 367 368 369 370 371 372
    return dd;
}

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

  Synopsis    [Derives the global BDD for one AIG node.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
373
DdNode * Abc_NodeGlobalBdds_rec( DdManager * dd, Abc_Obj_t * pNode, int nBddSizeMax, int fDropInternal, ProgressBar * pProgress, int * pCounter, int fVerbose )
Alan Mishchenko committed
374
{
Alan Mishchenko committed
375
    DdNode * bFunc, * bFunc0, * bFunc1, * bFuncC;
376
    int fDetectMuxes = 0;
Alan Mishchenko committed
377
    assert( !Abc_ObjIsComplement(pNode) );
Alan Mishchenko committed
378 379 380 381 382 383
    if ( Cudd_ReadKeys(dd)-Cudd_ReadDead(dd) > (unsigned)nBddSizeMax )
    {
        Extra_ProgressBarStop( pProgress );
        if ( fVerbose )
        printf( "The number of live nodes reached %d.\n", nBddSizeMax );
        fflush( stdout );
Alan Mishchenko committed
384
        return NULL;
Alan Mishchenko committed
385
    }
Alan Mishchenko committed
386
    // if the result is available return
Alan Mishchenko committed
387 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 ( Abc_ObjGlobalBdd(pNode) == NULL )
    {
        Abc_Obj_t * pNodeC, * pNode0, * pNode1;
        pNode0 = Abc_ObjFanin0(pNode);
        pNode1 = Abc_ObjFanin1(pNode);
        // check for the special case when it is MUX/EXOR
        if ( fDetectMuxes && 
             Abc_ObjGlobalBdd(pNode0) == NULL && Abc_ObjGlobalBdd(pNode1) == NULL &&
             Abc_ObjIsNode(pNode0) && Abc_ObjFanoutNum(pNode0) == 1 && 
             Abc_ObjIsNode(pNode1) && Abc_ObjFanoutNum(pNode1) == 1 && 
             Abc_NodeIsMuxType(pNode) )
        {
            // deref the fanins
            pNode0->vFanouts.nSize--;
            pNode1->vFanouts.nSize--;
            // recognize the MUX
            pNodeC = Abc_NodeRecognizeMux( pNode, &pNode1, &pNode0 );
            assert( Abc_ObjFanoutNum(pNodeC) > 1 );
            // dereference the control once (the second time it will be derefed when BDDs are computed)
            pNodeC->vFanouts.nSize--;

            // compute the result for all branches
            bFuncC = Abc_NodeGlobalBdds_rec( dd, pNodeC, nBddSizeMax, fDropInternal, pProgress, pCounter, fVerbose ); 
            if ( bFuncC == NULL )
                return NULL;
            Cudd_Ref( bFuncC );
            bFunc0 = Abc_NodeGlobalBdds_rec( dd, Abc_ObjRegular(pNode0), nBddSizeMax, fDropInternal, pProgress, pCounter, fVerbose ); 
            if ( bFunc0 == NULL )
                return NULL;
            Cudd_Ref( bFunc0 );
            bFunc1 = Abc_NodeGlobalBdds_rec( dd, Abc_ObjRegular(pNode1), nBddSizeMax, fDropInternal, pProgress, pCounter, fVerbose ); 
            if ( bFunc1 == NULL )
                return NULL;
            Cudd_Ref( bFunc1 );

            // complement the branch BDDs
423 424
            bFunc0 = Cudd_NotCond( bFunc0, (int)Abc_ObjIsComplement(pNode0) );
            bFunc1 = Cudd_NotCond( bFunc1, (int)Abc_ObjIsComplement(pNode1) );
Alan Mishchenko committed
425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
            // get the final result
            bFunc = Cudd_bddIte( dd, bFuncC, bFunc1, bFunc0 );   Cudd_Ref( bFunc );
            Cudd_RecursiveDeref( dd, bFunc0 );
            Cudd_RecursiveDeref( dd, bFunc1 );
            Cudd_RecursiveDeref( dd, bFuncC );
            // add the number of used nodes
            (*pCounter) += 3;
        }
        else
        {
            // compute the result for both branches
            bFunc0 = Abc_NodeGlobalBdds_rec( dd, Abc_ObjFanin(pNode,0), nBddSizeMax, fDropInternal, pProgress, pCounter, fVerbose ); 
            if ( bFunc0 == NULL )
                return NULL;
            Cudd_Ref( bFunc0 );
            bFunc1 = Abc_NodeGlobalBdds_rec( dd, Abc_ObjFanin(pNode,1), nBddSizeMax, fDropInternal, pProgress, pCounter, fVerbose ); 
            if ( bFunc1 == NULL )
                return NULL;
            Cudd_Ref( bFunc1 );
444 445
            bFunc0 = Cudd_NotCond( bFunc0, (int)Abc_ObjFaninC0(pNode) );
            bFunc1 = Cudd_NotCond( bFunc1, (int)Abc_ObjFaninC1(pNode) );
Alan Mishchenko committed
446
            // get the final result
447 448 449 450
            bFunc = Cudd_bddAndLimit( dd, bFunc0, bFunc1, nBddSizeMax );
            if ( bFunc == NULL )
                return NULL;
            Cudd_Ref( bFunc );
Alan Mishchenko committed
451 452 453 454 455 456 457 458 459 460 461 462 463
            Cudd_RecursiveDeref( dd, bFunc0 );
            Cudd_RecursiveDeref( dd, bFunc1 );
            // add the number of used nodes
            (*pCounter)++;
        }
        // set the result
        assert( Abc_ObjGlobalBdd(pNode) == NULL );
        Abc_ObjSetGlobalBdd( pNode, bFunc );
        // increment the progress bar
        if ( pProgress )
            Extra_ProgressBarUpdate( pProgress, *pCounter, NULL );
    }
    // prepare the return value
464
    bFunc = (DdNode *)Abc_ObjGlobalBdd(pNode);
Alan Mishchenko committed
465 466 467 468 469 470
    // dereference BDD at the node
    if ( --pNode->vFanouts.nSize == 0 && fDropInternal )
    {
        Cudd_Deref( bFunc );
        Abc_ObjSetGlobalBdd( pNode, NULL );
    }
Alan Mishchenko committed
471 472 473 474 475
    return bFunc;
}

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

Alan Mishchenko committed
476
  Synopsis    [Frees the global BDDs of the network.]
Alan Mishchenko committed
477 478 479 480 481 482 483 484

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
485
void * Abc_NtkFreeGlobalBdds( Abc_Ntk_t * pNtk, int fFreeMan ) 
Alan Mishchenko committed
486
{ 
487
    return Abc_NtkAttrFree( pNtk, VEC_ATTR_GLOBAL_BDD, fFreeMan ); 
Alan Mishchenko committed
488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526
}

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

  Synopsis    [Returns the shared size of global BDDs of the COs.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Abc_NtkSizeOfGlobalBdds( Abc_Ntk_t * pNtk ) 
{
    Vec_Ptr_t * vFuncsGlob;
    Abc_Obj_t * pObj;
    int RetValue, i;
    // complement the global functions
    vFuncsGlob = Vec_PtrAlloc( Abc_NtkCoNum(pNtk) );
    Abc_NtkForEachCo( pNtk, pObj, i )
        Vec_PtrPush( vFuncsGlob, Abc_ObjGlobalBdd(pObj) );
    RetValue = Cudd_SharingSize( (DdNode **)Vec_PtrArray(vFuncsGlob), Vec_PtrSize(vFuncsGlob) );
    Vec_PtrFree( vFuncsGlob );
    return RetValue;
}

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

  Synopsis    [Computes the BDD of the logic cone of the node.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
double Abc_NtkSpacePercentage( Abc_Obj_t * pNode )
Alan Mishchenko committed
527
{
Alan Mishchenko committed
528 529 530 531
    /*
    Vec_Ptr_t * vNodes;
    Abc_Obj_t * pObj, * pNodeR;
    DdManager * dd;
Alan Mishchenko committed
532
    DdNode * bFunc;
Alan Mishchenko committed
533
    double Result;
Alan Mishchenko committed
534
    int i;
Alan Mishchenko committed
535 536 537 538 539 540 541 542 543
    pNodeR = Abc_ObjRegular(pNode);
    assert( Abc_NtkIsStrash(pNodeR->pNtk) );
    Abc_NtkCleanCopy( pNodeR->pNtk );
    // get the CIs in the support of the node
    vNodes = Abc_NtkNodeSupport( pNodeR->pNtk, &pNodeR, 1 );
    // start the manager
    dd = Cudd_Init( Vec_PtrSize(vNodes), 0, CUDD_UNIQUE_SLOTS, CUDD_CACHE_SLOTS, 0 );
    Cudd_AutodynEnable( dd, CUDD_REORDER_SYMM_SIFT );
    // assign elementary BDDs for the CIs
544
    Vec_PtrForEachEntry( Abc_Obj_t *, vNodes, pObj, i )
Alan Mishchenko committed
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 571 572 573 574 575 576 577 578 579 580 581 582
        pObj->pCopy = (Abc_Obj_t *)dd->vars[i];
    // build the BDD of the cone
    bFunc = Abc_NodeGlobalBdds_rec( dd, pNodeR, 10000000, 1, NULL, NULL, 1 );  Cudd_Ref( bFunc );
    bFunc = Cudd_NotCond( bFunc, pNode != pNodeR );
    // count minterms
    Result = Cudd_CountMinterm( dd, bFunc, dd->size );
    // get the percentagle
    Result *= 100.0;
    for ( i = 0; i < dd->size; i++ )
        Result /= 2;
    // clean up
    Cudd_Quit( dd );
    Vec_PtrFree( vNodes );
    return Result;
    */
    return 0.0;
}




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

  Synopsis    [Experiment with BDD-based representation of implications.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Abc_NtkBddImplicationTest()
{
    DdManager * dd;
    DdNode * bImp, * bSum, * bTemp;
    int nVars = 200;
    int nImps = 200;
583
    int i;
584 585
    abctime clk;
clk = Abc_Clock();
Alan Mishchenko committed
586 587 588 589 590 591 592 593 594 595 596 597 598 599
    dd = Cudd_Init( nVars, 0, CUDD_UNIQUE_SLOTS, CUDD_CACHE_SLOTS, 0 );
    Cudd_AutodynEnable( dd, CUDD_REORDER_SIFT );
    bSum = b0;   Cudd_Ref( bSum );
    for ( i = 0; i < nImps; i++ )
    {
        printf( "." );
        bImp = Cudd_bddAnd( dd, dd->vars[rand()%nVars], dd->vars[rand()%nVars] );  Cudd_Ref( bImp );
        bSum = Cudd_bddOr( dd, bTemp = bSum, bImp );     Cudd_Ref( bSum );
        Cudd_RecursiveDeref( dd, bTemp );
        Cudd_RecursiveDeref( dd, bImp );
    }
    printf( "The BDD before = %d.\n", Cudd_DagSize(bSum) );
    Cudd_ReduceHeap( dd, CUDD_REORDER_SIFT, 1 );
    printf( "The BDD after  = %d.\n", Cudd_DagSize(bSum) );
600
ABC_PRT( "Time", Abc_Clock() - clk );
Alan Mishchenko committed
601 602
    Cudd_RecursiveDeref( dd, bSum );
    Cudd_Quit( dd );
Alan Mishchenko committed
603 604
}

605 606 607 608 609 610 611 612
#else

double Abc_NtkSpacePercentage( Abc_Obj_t * pNode ) { return 0.0; }
Abc_Ntk_t * Abc_NtkBddToMuxes( Abc_Ntk_t * pNtk ) { return NULL; }


#endif

Alan Mishchenko committed
613 614 615 616 617
////////////////////////////////////////////////////////////////////////
///                       END OF FILE                                ///
////////////////////////////////////////////////////////////////////////


618 619
ABC_NAMESPACE_IMPL_END