abcDsd.c 23.3 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
#include "base/abc/abc.h"
22 23

#ifdef ABC_USE_CUDD
24
#include "bdd/extrab/extraBdd.h"
25
#include "bdd/dsd/dsd.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

37
static Abc_Ntk_t *     Abc_NtkDsdInternal( Abc_Ntk_t * pNtk, int fVerbose, int fPrint, int fShort );
Alan Mishchenko committed
38
static void            Abc_NtkDsdConstruct( Dsd_Manager_t * pManDsd, Abc_Ntk_t * pNtk, Abc_Ntk_t * pNtkNew );
Alan Mishchenko committed
39
static Abc_Obj_t *     Abc_NtkDsdConstructNode( Dsd_Manager_t * pManDsd, Dsd_Node_t * pNodeDsd, Abc_Ntk_t * pNtkNew, int * pCounters );
Alan Mishchenko committed
40 41

static Vec_Ptr_t *     Abc_NtkCollectNodesForDsd( Abc_Ntk_t * pNtk );
42 43
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
44 45
static int             Abc_NodeFindMuxVar( DdManager * dd, DdNode * bFunc, int nVars );

Alan Mishchenko committed
46
////////////////////////////////////////////////////////////////////////
Alan Mishchenko committed
47
///                     FUNCTION DEFINITIONS                         ///
Alan Mishchenko committed
48 49 50 51
////////////////////////////////////////////////////////////////////////

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

Alan Mishchenko committed
52 53 54 55 56 57 58 59 60 61 62 63 64
  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     []

***********************************************************************/
65
Abc_Ntk_t * Abc_NtkDsdGlobal( Abc_Ntk_t * pNtk, int fVerbose, int fPrint, int fShort )
Alan Mishchenko committed
66
{
Alan Mishchenko committed
67
    DdManager * dd;
Alan Mishchenko committed
68
    Abc_Ntk_t * pNtkNew;
Alan Mishchenko committed
69
    assert( Abc_NtkIsStrash(pNtk) );
70
    dd = (DdManager *)Abc_NtkBuildGlobalBdds( pNtk, 10000000, 1, 1, fVerbose );
Alan Mishchenko committed
71
    if ( dd == NULL )
Alan Mishchenko committed
72 73
        return NULL;
    if ( fVerbose )
Alan Mishchenko committed
74
        printf( "Shared BDD size = %6d nodes.\n", Cudd_ReadKeys(dd) - Cudd_ReadDead(dd) );
Alan Mishchenko committed
75
    // transform the result of mapping into a BDD network
Alan Mishchenko committed
76
    pNtkNew = Abc_NtkDsdInternal( pNtk, fVerbose, fPrint, fShort );
Alan Mishchenko committed
77
    Extra_StopManager( dd );
Alan Mishchenko committed
78 79
    if ( pNtkNew == NULL )
        return NULL;
Alan Mishchenko committed
80 81 82
    // copy EXDC network
    if ( pNtk->pExdc )
        pNtkNew->pExdc = Abc_NtkDup( pNtk->pExdc );
Alan Mishchenko committed
83
    if ( !Abc_NtkCheck( pNtkNew ) )
Alan Mishchenko committed
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
    {
        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     []

***********************************************************************/
103
Abc_Ntk_t * Abc_NtkDsdInternal( Abc_Ntk_t * pNtk, int fVerbose, int fPrint, int fShort )
Alan Mishchenko committed
104
{
Alan Mishchenko committed
105 106
    char ** ppNamesCi, ** ppNamesCo;
    Vec_Ptr_t * vFuncsGlob;
Alan Mishchenko committed
107 108
    Dsd_Manager_t * pManDsd;
    Abc_Ntk_t * pNtkNew;
Alan Mishchenko committed
109
    DdManager * dd;
Alan Mishchenko committed
110 111 112 113
    Abc_Obj_t * pObj;
    int i;

    // complement the global functions
Alan Mishchenko committed
114
    vFuncsGlob = Vec_PtrAlloc( Abc_NtkCoNum(pNtk) );
Alan Mishchenko committed
115
    Abc_NtkForEachCo( pNtk, pObj, i )
Alan Mishchenko committed
116
        Vec_PtrPush( vFuncsGlob, Cudd_NotCond(Abc_ObjGlobalBdd(pObj), Abc_ObjFaninC0(pObj)) );
Alan Mishchenko committed
117 118

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

    // start the new network
Alan Mishchenko committed
132
    pNtkNew = Abc_NtkStartFrom( pNtk, ABC_NTK_LOGIC, ABC_FUNC_BDD );
Alan Mishchenko committed
133
    // make sure the new manager has enough inputs
134
    Cudd_bddIthVar( (DdManager *)pNtkNew->pManFunc, dd->size-1 );
Alan Mishchenko committed
135 136 137 138
    // 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
139 140
    // fix the problem with complemented and duplicated CO edges
    Abc_NtkLogicMakeSimpleCos( pNtkNew, 0 );
Alan Mishchenko committed
141
    if ( fPrint )
Alan Mishchenko committed
142 143 144
    {
        ppNamesCi = Abc_NtkCollectCioNames( pNtk, 0 );
        ppNamesCo = Abc_NtkCollectCioNames( pNtk, 1 );
145 146 147 148
        if ( fVerbose )
            Dsd_TreePrint( stdout, pManDsd, ppNamesCi, ppNamesCo, fShort, -1 );
        else
            Dsd_TreePrint2( stdout, pManDsd, ppNamesCi, ppNamesCo, -1 );
Alan Mishchenko committed
149 150
        ABC_FREE( ppNamesCi );
        ABC_FREE( ppNamesCo );
Alan Mishchenko committed
151
    }
Alan Mishchenko committed
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172

    // 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
173
    Abc_Obj_t * pNode, * pNodeNew, * pDriver;
Alan Mishchenko committed
174 175 176
    int i, nNodesDsd;

    // save the CI nodes in the DSD nodes
Alan Mishchenko committed
177 178
    Abc_AigConst1(pNtk)->pCopy = pNodeNew = Abc_NtkCreateNodeConst1(pNtkNew);
    Dsd_NodeSetMark( Dsd_ManagerReadConst1(pManDsd), (int)(ABC_PTRINT_T)pNodeNew );
Alan Mishchenko committed
179 180 181
    Abc_NtkForEachCi( pNtk, pNode, i )
    {
        pNodeDsd = Dsd_ManagerReadInput( pManDsd, i );
Alan Mishchenko committed
182
        Dsd_NodeSetMark( pNodeDsd, (int)(ABC_PTRINT_T)pNode->pCopy );
Alan Mishchenko committed
183 184 185 186 187
    }

    // 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
188
        Abc_NtkDsdConstructNode( pManDsd, ppNodesDsd[i], pNtkNew, NULL );
Alan Mishchenko committed
189
    ABC_FREE( ppNodesDsd );
Alan Mishchenko committed
190 191 192 193 194 195 196

    // set the pointers to the CO drivers
    Abc_NtkForEachCo( pNtk, pNode, i )
    {
        pDriver = Abc_ObjFanin0( pNode );
        if ( !Abc_ObjIsNode(pDriver) )
            continue;
Alan Mishchenko committed
197
        if ( !Abc_AigNodeIsAnd(pDriver) )
Alan Mishchenko committed
198
            continue;
Alan Mishchenko committed
199
        pNodeDsd = Dsd_ManagerReadRoot( pManDsd, i );
Alan Mishchenko committed
200
        pNodeNew = (Abc_Obj_t *)(ABC_PTRINT_T)Dsd_NodeReadMark( Dsd_Regular(pNodeDsd) );
Alan Mishchenko committed
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
        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
217
Abc_Obj_t * Abc_NtkDsdConstructNode( Dsd_Manager_t * pManDsd, Dsd_Node_t * pNodeDsd, Abc_Ntk_t * pNtkNew, int * pCounters )
Alan Mishchenko committed
218 219
{
    DdManager * ddDsd = Dsd_ManagerReadDd( pManDsd );
220
    DdManager * ddNew = (DdManager *)pNtkNew->pManFunc;
Alan Mishchenko committed
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
    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
236
        pFanin     = (Abc_Obj_t *)(ABC_PTRINT_T)Dsd_NodeReadMark(Dsd_Regular(pFaninDsd));
Alan Mishchenko committed
237 238 239 240 241
        Abc_ObjAddFanin( pNodeNew, pFanin );
        assert( Type == DSD_NODE_OR || !Dsd_IsComplement(pFaninDsd) );
    }

    // create the local function depending on the type of the node
242
    ddNew = (DdManager *)pNtkNew->pManFunc;
Alan Mishchenko committed
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
    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
274 275 276 277 278 279 280
            if ( pCounters )
            {
                if ( nDecs < 10 )
                    pCounters[nDecs]++;
                else
                    pCounters[10]++;
            }
Alan Mishchenko committed
281 282
            bLocal = Dsd_TreeGetPrimeFunction( ddDsd, pNodeDsd );                Cudd_Ref( bLocal );
            bLocal = Extra_TransferLevelByLevel( ddDsd, ddNew, bTemp = bLocal ); Cudd_Ref( bLocal );
Alan Mishchenko committed
283 284 285 286 287 288 289
/*
if ( nDecs == 3 )
{
Extra_bddPrint( ddDsd, bTemp );
printf( "\n" );
}
*/
Alan Mishchenko committed
290 291 292 293 294 295 296 297 298 299 300
            Cudd_RecursiveDeref( ddDsd, bTemp );
            // bLocal is now in the new BDD manager
            break;
        }
        default:
        {
            assert( 0 );
            break;
        }
    }
    pNodeNew->pData = bLocal;
Alan Mishchenko committed
301
    Dsd_NodeSetMark( pNodeDsd, (int)(ABC_PTRINT_T)pNodeNew );
Alan Mishchenko committed
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
    return pNodeNew;
}






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

  Synopsis    [Recursively decomposes internal nodes.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
321
int Abc_NtkDsdLocal( Abc_Ntk_t * pNtk, int fVerbose, int fRecursive )
Alan Mishchenko committed
322 323
{
    Dsd_Manager_t * pManDsd;
324
    DdManager * dd = (DdManager *)pNtk->pManFunc;
Alan Mishchenko committed
325 326
    Vec_Ptr_t * vNodes;
    int i;
Alan Mishchenko committed
327
    int pCounters[11] = {0};
Alan Mishchenko committed
328

Alan Mishchenko committed
329
    assert( Abc_NtkIsBddLogic(pNtk) );
Alan Mishchenko committed
330 331 332 333 334 335 336 337 338 339

    // 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++ )
340
        Abc_NodeDecompDsdAndMux( (Abc_Obj_t *)vNodes->pArray[i], vNodes, pManDsd, fRecursive, pCounters );
Alan Mishchenko committed
341 342
    Vec_PtrFree( vNodes );

343 344 345 346 347 348 349
    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
350

Alan Mishchenko committed
351 352 353 354
    // stop the DSD manager
    Dsd_ManagerStop( pManDsd );

    // make sure everything is okay
Alan Mishchenko committed
355
    if ( !Abc_NtkCheck( pNtk ) )
Alan Mishchenko committed
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 392 393 394 395 396 397 398 399
    {
        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     []

***********************************************************************/
400
void Abc_NodeDecompDsdAndMux( Abc_Obj_t * pNode, Vec_Ptr_t * vNodes, Dsd_Manager_t * pManDsd, int fRecursive, int * pCounters )
Alan Mishchenko committed
401
{
402
    DdManager * dd = (DdManager *)pNode->pNtk->pManFunc;
Alan Mishchenko committed
403
    Abc_Obj_t * pRoot = NULL, * pFanin, * pNode1, * pNode2, * pNodeC;
Alan Mishchenko committed
404 405 406 407
    Dsd_Node_t ** ppNodesDsd, * pNodeDsd, * pFaninDsd;
    int i, nNodesDsd, iVar, fCompl;

    // try disjoint support decomposition
408
    pNodeDsd = Dsd_DecomposeOne( pManDsd, (DdNode *)pNode->pData );
Alan Mishchenko committed
409 410 411 412
    fCompl   = Dsd_IsComplement( pNodeDsd );
    pNodeDsd = Dsd_Regular( pNodeDsd );

    // determine what decomposition to use   
Alan Mishchenko committed
413
    if ( !fRecursive || Dsd_NodeReadDecsNum(pNodeDsd) != Abc_ObjFaninNum(pNode) )
Alan Mishchenko committed
414 415 416 417 418 419
    { // perform DSD

        // set the inputs
        Abc_ObjForEachFanin( pNode, pFanin, i )
        {
            pFaninDsd = Dsd_ManagerReadInput( pManDsd, i );
Alan Mishchenko committed
420
            Dsd_NodeSetMark( pFaninDsd, (int)(ABC_PTRINT_T)pFanin );
Alan Mishchenko committed
421 422 423 424 425 426
        }

        // construct the intermediate nodes
        ppNodesDsd = Dsd_TreeCollectNodesDfsOne( pManDsd, pNodeDsd, &nNodesDsd );
        for ( i = 0; i < nNodesDsd; i++ )
        {
Alan Mishchenko committed
427
            pRoot = Abc_NtkDsdConstructNode( pManDsd, ppNodesDsd[i], pNode->pNtk, pCounters );
Alan Mishchenko committed
428
            if ( Abc_NodeIsForDsd(pRoot) && fRecursive )
Alan Mishchenko committed
429 430
                Vec_PtrPush( vNodes, pRoot );
        }
Alan Mishchenko committed
431
        ABC_FREE( ppNodesDsd );
Alan Mishchenko committed
432
        assert(pRoot);
Alan Mishchenko committed
433 434 435 436 437 438

        // remove the current fanins
        Abc_ObjRemoveFanins( pNode );
        // add fanin to the root
        Abc_ObjAddFanin( pNode, pRoot );
        // update the function to be that of buffer
439 440
        Cudd_RecursiveDeref( dd, (DdNode *)pNode->pData );
        pNode->pData = Cudd_NotCond( (DdNode *)dd->vars[0], fCompl ); Cudd_Ref( (DdNode *)pNode->pData );
Alan Mishchenko committed
441 442 443 444
    }
    else // perform MUX-decomposition
    {
        // get the cofactoring variable
445
        iVar = Abc_NodeFindMuxVar( dd, (DdNode *)pNode->pData, Abc_ObjFaninNum(pNode) );
Alan Mishchenko committed
446 447 448
        pNodeC = Abc_ObjFanin( pNode, iVar );

        // get the negative cofactor
Alan Mishchenko committed
449
        pNode1 = Abc_NtkCloneObj( pNode );
450
        pNode1->pData = Cudd_Cofactor( dd, (DdNode *)pNode->pData, Cudd_Not(dd->vars[iVar]) );  Cudd_Ref( (DdNode *)pNode1->pData );
Alan Mishchenko committed
451 452 453 454 455
        Abc_NodeMinimumBase( pNode1 );
        if ( Abc_NodeIsForDsd(pNode1) )
            Vec_PtrPush( vNodes, pNode1 );

        // get the positive cofactor
Alan Mishchenko committed
456
        pNode2 = Abc_NtkCloneObj( pNode );
457
        pNode2->pData = Cudd_Cofactor( dd, (DdNode *)pNode->pData, dd->vars[iVar] );            Cudd_Ref( (DdNode *)pNode2->pData );
Alan Mishchenko committed
458 459 460 461 462 463 464 465 466 467 468
        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
469 470
        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
471 472 473 474 475
    }
}

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

Alan Mishchenko committed
476
  Synopsis    [Checks if the node should be decomposed by DSD.]
Alan Mishchenko committed
477 478 479 480 481 482 483 484

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
485
int Abc_NodeIsForDsd( Abc_Obj_t * pNode )
Alan Mishchenko committed
486
{
Alan Mishchenko committed
487
//    DdManager * dd = pNode->pNtk->pManFunc;
Alan Mishchenko committed
488
//    DdNode * bFunc, * bFunc0, * bFunc1;
Alan Mishchenko committed
489
    assert( Abc_ObjIsNode(pNode) );
Alan Mishchenko committed
490 491 492 493
//    if ( Cudd_DagSize(pNode->pData)-1 > Abc_ObjFaninNum(pNode) )
//        return 1;
//    return 0;

Alan Mishchenko committed
494 495
/*
    // this does not catch things like a(b+c), which should be decomposed
Alan Mishchenko committed
496 497 498 499 500 501 502 503 504 505 506
    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
507 508 509
*/
    if ( Abc_ObjFaninNum(pNode) > 2 )
        return 1;
Alan Mishchenko committed
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 546 547 548 549 550 551 552 553
    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
554

Alan Mishchenko committed
555 556 557 558 559 560 561 562
        if ( SuppSumMin > nSSD + nSSQ )
        {
             SuppSumMin = nSSD + nSSQ;
             iVar = i;
        }
    }
    return iVar;
}
Alan Mishchenko committed
563

Alan Mishchenko committed
564

565 566 567 568 569 570 571 572 573 574 575 576 577 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 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662
/**Function********************************************************************

  Synopsis    [Computes the positive polarty cube composed of the first vars in the array.]

  Description []

  SideEffects []

  SeeAlso     []

******************************************************************************/
DdNode * Extra_bddComputeSum( DdManager * dd, DdNode ** pbCubes, int nCubes )
{
    DdNode * bRes, * bTemp;
    int i;
    bRes = b0; Cudd_Ref( bRes );
    for ( i = 0; i < nCubes; i++ )
    {
        bRes = Cudd_bddOr( dd, bTemp = bRes, pbCubes[i] );  Cudd_Ref( bRes );
        Cudd_RecursiveDeref( dd, bTemp );
    }
    Cudd_Deref( bRes );
    return bRes;
}

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

  Synopsis    [Derives network with the given percentage of on-set and off-set minterms.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
DdNode * Abc_NtkSparsifyInternalOne( DdManager * ddNew, DdNode * bFunc, int nFanins, int nPerc )
{
    int nSpace = (int)Cudd_CountMinterm( ddNew, bFunc, nFanins );
    int i, nMints = Abc_MaxInt( 1, (int)(0.01 * nPerc * nSpace) );
    DdNode ** pbMints = Cudd_bddPickArbitraryMinterms( ddNew, bFunc, ddNew->vars, nFanins, nMints );
    DdNode * bRes;
    for ( i = 0; i < nMints; i++ )
        Cudd_Ref( pbMints[i] );
    bRes = Extra_bddComputeSum( ddNew, pbMints, nMints ); Cudd_Ref( bRes );
    for ( i = 0; i < nMints; i++ )
        Cudd_RecursiveDeref( ddNew, pbMints[i] );
    Cudd_Deref( bRes );
    ABC_FREE( pbMints );
    return bRes;
}
Abc_Ntk_t * Abc_NtkSparsifyInternal( Abc_Ntk_t * pNtk, int nPerc, int fVerbose )
{
    Abc_Ntk_t * pNtkNew;
    Abc_Obj_t * pObj, * pDriver, * pFanin;
    DdNode * bFunc, * bFuncOld;
    DdManager * ddNew;
    int i, k, c;
    // start the new network
    pNtkNew = Abc_NtkAlloc( ABC_NTK_LOGIC, ABC_FUNC_BDD, 1 );
    Abc_NtkForEachCi( pNtk, pObj, i )
        Abc_NtkDupObj( pNtkNew, pObj, 1 );
    // duplicate the name and the spec
    pNtkNew->pName = Extra_UtilStrsav(pNtk->pName);
    pNtkNew->pSpec = Extra_UtilStrsav(pNtk->pSpec);
    // make sure the new manager has enough inputs
    ddNew = (DdManager *)pNtkNew->pManFunc;
    Cudd_bddIthVar( ddNew, Abc_NtkCiNum(pNtk)-1 );
    // go through the outputs
    Abc_NtkForEachCo( pNtk, pObj, i )
    {
        pDriver = Abc_ObjFanin0( pObj );
        if ( Abc_ObjIsCi(pDriver) )
        {
            Abc_NtkDupObj( pNtkNew, pObj, 0 );
            Abc_ObjAddFanin( pObj->pCopy, Abc_ObjNotCond(pDriver->pCopy, Abc_ObjFaninC0(pObj)) );
            Abc_ObjAssignName( pObj->pCopy, Abc_ObjName(pObj), "_on" );

            Abc_NtkDupObj( pNtkNew, pObj, 0 );
            Abc_ObjAddFanin( pObj->pCopy, Abc_ObjNotCond(pDriver->pCopy, !Abc_ObjFaninC0(pObj)) );
            Abc_ObjAssignName( pObj->pCopy, Abc_ObjName(pObj), "_off" );
            continue;
        }
        if ( Abc_ObjFaninNum(pDriver) == 0 )
        {
            Abc_NtkDupObj( pNtkNew, pObj, 0 );
            Abc_ObjAddFanin( pObj->pCopy, Abc_ObjFaninC0(pObj) ? Abc_NtkCreateNodeConst0(pNtkNew) : Abc_NtkCreateNodeConst1(pNtkNew) );
            Abc_ObjAssignName( pObj->pCopy, Abc_ObjName(pObj), "_on" );

            Abc_NtkDupObj( pNtkNew, pObj, 0 );
            Abc_ObjAddFanin( pObj->pCopy, Abc_ObjFaninC0(pObj) ? Abc_NtkCreateNodeConst1(pNtkNew) : Abc_NtkCreateNodeConst0(pNtkNew) );
            Abc_ObjAssignName( pObj->pCopy, Abc_ObjName(pObj), "_off" );
            continue;
        }
        assert( Abc_ObjFaninNum(pObj) > 0 );
        // onset/offset
        for ( c = 0; c < 2; c++ )
        {
663
            Cudd_Srandom( 0 );
664 665 666 667 668 669 670 671 672
            Abc_NtkDupObj( pNtkNew, pDriver, 0 );
            Abc_ObjForEachFanin( pDriver, pFanin, k )
                Abc_ObjAddFanin( pDriver->pCopy, pFanin->pCopy );
            bFuncOld = Cudd_NotCond( (DdNode *)pDriver->pCopy->pData, c );
            bFunc = Abc_NtkSparsifyInternalOne( ddNew, bFuncOld, Abc_ObjFaninNum(pDriver), nPerc );  Cudd_Ref( bFunc );
            Cudd_RecursiveDeref( ddNew, bFuncOld );
            pDriver->pCopy->pData = bFunc;
            Abc_NtkDupObj( pNtkNew, pObj, 0 );
            Abc_ObjAddFanin( pObj->pCopy, pDriver->pCopy );
673
            Abc_ObjAssignName( pObj->pCopy, Abc_ObjName(pObj), (char*)(c ? "_off" : "_on") );
674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695
        }
    }
    Abc_NtkLogicMakeSimpleCos( pNtkNew, 0 );
    return pNtkNew;
}
Abc_Ntk_t * Abc_NtkSparsify( Abc_Ntk_t * pNtk, int nPerc, int fVerbose )
{
    Abc_Ntk_t * pNtkNew;
    assert( Abc_NtkIsComb(pNtk) );
    assert( Abc_NtkIsBddLogic(pNtk) );
    pNtkNew = Abc_NtkSparsifyInternal( pNtk, nPerc, fVerbose );
    if ( pNtkNew == NULL )
        return NULL;
    if ( !Abc_NtkCheck( pNtkNew ) )
    {
        printf( "Abc_NtkSparsify: The network check has failed.\n" );
        Abc_NtkDelete( pNtkNew );
        return NULL;
    }
    return pNtkNew;
}

696 697 698 699 700 701 702 703
#else

Abc_Ntk_t * Abc_NtkSparsify( Abc_Ntk_t * pNtk, int nPerc, int fVerbose )                { return NULL; }
Abc_Ntk_t * Abc_NtkDsdGlobal( Abc_Ntk_t * pNtk, int fVerbose, int fPrint, int fShort )  { return NULL; }
int         Abc_NtkDsdLocal( Abc_Ntk_t * pNtk, int fVerbose, int fRecursive )           { return 0;    }

#endif

Alan Mishchenko committed
704 705 706 707 708
////////////////////////////////////////////////////////////////////////
///                       END OF FILE                                ///
////////////////////////////////////////////////////////////////////////


709 710
ABC_NAMESPACE_IMPL_END