abcCollapse.c 7.21 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    [abcCollapse.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [Network and node package.]

  Synopsis    [Collapsing the network into two-levels.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

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

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

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

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

24 25 26
ABC_NAMESPACE_IMPL_START


Alan Mishchenko committed
27 28 29 30
////////////////////////////////////////////////////////////////////////
///                        DECLARATIONS                              ///
////////////////////////////////////////////////////////////////////////

Alan Mishchenko committed
31
static Abc_Ntk_t * Abc_NtkFromGlobalBdds( Abc_Ntk_t * pNtk );
Alan Mishchenko committed
32 33 34
static Abc_Obj_t * Abc_NodeFromGlobalBdds( Abc_Ntk_t * pNtkNew, DdManager * dd, DdNode * bFunc );

////////////////////////////////////////////////////////////////////////
Alan Mishchenko committed
35
///                     FUNCTION DEFINITIONS                         ///
Alan Mishchenko committed
36 37 38 39 40 41 42 43 44 45 46 47 48
////////////////////////////////////////////////////////////////////////

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

  Synopsis    [Collapses the network.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
49
Abc_Ntk_t * Abc_NtkCollapse( Abc_Ntk_t * pNtk, int fBddSizeMax, int fDualRail, int fReorder, int fVerbose )
Alan Mishchenko committed
50 51
{
    Abc_Ntk_t * pNtkNew;
52
    abctime clk = Abc_Clock();
Alan Mishchenko committed
53

Alan Mishchenko committed
54
    assert( Abc_NtkIsStrash(pNtk) );
Alan Mishchenko committed
55
    // compute the global BDDs
Alan Mishchenko committed
56
    if ( Abc_NtkBuildGlobalBdds(pNtk, fBddSizeMax, 1, fReorder, fVerbose) == NULL )
Alan Mishchenko committed
57 58
        return NULL;
    if ( fVerbose )
Alan Mishchenko committed
59
    {
60
        DdManager * dd = (DdManager *)Abc_NtkGlobalBddMan( pNtk );
Alan Mishchenko committed
61
        printf( "Shared BDD size = %6d nodes.  ", Cudd_ReadKeys(dd) - Cudd_ReadDead(dd) );
62
        ABC_PRT( "BDD construction time", Abc_Clock() - clk );
Alan Mishchenko committed
63
    }
Alan Mishchenko committed
64

Alan Mishchenko committed
65
    // create the new network
Alan Mishchenko committed
66
    pNtkNew = Abc_NtkFromGlobalBdds( pNtk );
Alan Mishchenko committed
67 68
//    Abc_NtkFreeGlobalBdds( pNtk );
    Abc_NtkFreeGlobalBdds( pNtk, 1 );
Alan Mishchenko committed
69 70
    if ( pNtkNew == NULL )
    {
Alan Mishchenko committed
71 72
//        Cudd_Quit( pNtk->pManGlob );
//        pNtk->pManGlob = NULL;
Alan Mishchenko committed
73 74
        return NULL;
    }
Alan Mishchenko committed
75 76
//    Extra_StopManager( pNtk->pManGlob );
//    pNtk->pManGlob = NULL;
Alan Mishchenko committed
77 78 79 80

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

Alan Mishchenko committed
81 82 83
    if ( pNtk->pExdc )
        pNtkNew->pExdc = Abc_NtkDup( pNtk->pExdc );

Alan Mishchenko committed
84
    // make sure that everything is okay
Alan Mishchenko committed
85
    if ( !Abc_NtkCheck( pNtkNew ) )
Alan Mishchenko committed
86 87 88 89 90 91 92 93
    {
        printf( "Abc_NtkCollapse: The network check has failed.\n" );
        Abc_NtkDelete( pNtkNew );
        return NULL;
    }
    return pNtkNew;
}

Alan Mishchenko committed
94 95 96

//int runtime1, runtime2;

Alan Mishchenko committed
97 98 99 100 101 102 103 104 105 106 107
/**Function*************************************************************

  Synopsis    [Derives the network with the given global BDD.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
108
Abc_Ntk_t * Abc_NtkFromGlobalBdds( Abc_Ntk_t * pNtk )
Alan Mishchenko committed
109
{
Alan Mishchenko committed
110 111 112
//    extern void Extra_ShuffleTest( reo_man * p, DdManager * dd, DdNode * Func );
//    reo_man * pReo;

Alan Mishchenko committed
113 114
    ProgressBar * pProgress;
    Abc_Ntk_t * pNtkNew;
Alan Mishchenko committed
115 116
    Abc_Obj_t * pNode, * pDriver, * pNodeNew;
//    DdManager * dd = pNtk->pManGlob;
117
    DdManager * dd = (DdManager *)Abc_NtkGlobalBddMan( pNtk );
Alan Mishchenko committed
118
    int i;
Alan Mishchenko committed
119

120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
    // extract don't-care and compute ISOP
    if ( pNtk->pExdc )
    {
        DdManager * ddExdc = NULL;
        DdNode * bBddMin, * bBddDc, * bBddL, * bBddU;
        assert( Abc_NtkIsStrash(pNtk->pExdc) );
        assert( Abc_NtkCoNum(pNtk->pExdc) == 1 );
        // compute the global BDDs
        if ( Abc_NtkBuildGlobalBdds(pNtk->pExdc, 10000000, 1, 1, 0) == NULL )
            return NULL;
        // transfer tot the same manager
        ddExdc = (DdManager *)Abc_NtkGlobalBddMan( pNtk->pExdc );
        bBddDc = (DdNode *)Abc_ObjGlobalBdd(Abc_NtkCo(pNtk->pExdc, 0));
        bBddDc = Cudd_bddTransfer( ddExdc, dd, bBddDc );  Cudd_Ref( bBddDc );
        Abc_NtkFreeGlobalBdds( pNtk->pExdc, 1 );
        // minimize the output
        Abc_NtkForEachCo( pNtk, pNode, i )
        {
            bBddMin = (DdNode *)Abc_ObjGlobalBdd(pNode);
            // derive lower and uppwer bound
            bBddL = Cudd_bddAnd( dd, bBddMin,           Cudd_Not(bBddDc) );  Cudd_Ref( bBddL );
            bBddU = Cudd_bddAnd( dd, Cudd_Not(bBddMin), Cudd_Not(bBddDc) );  Cudd_Ref( bBddU );
            Cudd_RecursiveDeref( dd, bBddMin );
            // compute new one
            bBddMin = Cudd_bddIsop( dd, bBddL, Cudd_Not(bBddU) );            Cudd_Ref( bBddMin );
            Cudd_RecursiveDeref( dd, bBddL );
            Cudd_RecursiveDeref( dd, bBddU );
            // update global BDD
            Abc_ObjSetGlobalBdd( pNode, bBddMin );
            //Extra_bddPrint( dd, bBddMin ); printf( "\n" );
        }
        Cudd_RecursiveDeref( dd, bBddDc );
    }

Alan Mishchenko committed
154 155 156
//    pReo = Extra_ReorderInit( Abc_NtkCiNum(pNtk), 1000 );
//    runtime1 = runtime2 = 0;

Alan Mishchenko committed
157
    // start the new network
Alan Mishchenko committed
158
    pNtkNew = Abc_NtkStartFrom( pNtk, ABC_NTK_LOGIC, ABC_FUNC_BDD );
Alan Mishchenko committed
159
    // make sure the new manager has the same number of inputs
160
    Cudd_bddIthVar( (DdManager *)pNtkNew->pManFunc, dd->size-1 );
Alan Mishchenko committed
161 162 163 164 165
    // process the POs
    pProgress = Extra_ProgressBarStart( stdout, Abc_NtkCoNum(pNtk) );
    Abc_NtkForEachCo( pNtk, pNode, i )
    {
        Extra_ProgressBarUpdate( pProgress, i, NULL );
Alan Mishchenko committed
166 167 168 169 170 171 172
        pDriver = Abc_ObjFanin0(pNode);
        if ( Abc_ObjIsCi(pDriver) && !strcmp(Abc_ObjName(pNode), Abc_ObjName(pDriver)) )
        {
            Abc_ObjAddFanin( pNode->pCopy, pDriver->pCopy );
            continue;
        }
//        pNodeNew = Abc_NodeFromGlobalBdds( pNtkNew, dd, Vec_PtrEntry(pNtk->vFuncsGlob, i) );
173
        pNodeNew = Abc_NodeFromGlobalBdds( pNtkNew, dd, (DdNode *)Abc_ObjGlobalBdd(pNode) );
Alan Mishchenko committed
174
        Abc_ObjAddFanin( pNode->pCopy, pNodeNew );
Alan Mishchenko committed
175 176 177

//        Extra_ShuffleTest( pReo, dd, Abc_ObjGlobalBdd(pNode) );

Alan Mishchenko committed
178 179
    }
    Extra_ProgressBarStop( pProgress );
Alan Mishchenko committed
180 181

//    Extra_ReorderQuit( pReo );
Alan Mishchenko committed
182 183
//ABC_PRT( "Reo ", runtime1 );
//ABC_PRT( "Cudd", runtime2 );
Alan Mishchenko committed
184

Alan Mishchenko committed
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
    return pNtkNew;
}

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

  Synopsis    [Derives the network with the given global BDD.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Abc_Obj_t * Abc_NodeFromGlobalBdds( Abc_Ntk_t * pNtkNew, DdManager * dd, DdNode * bFunc )
{
    Abc_Obj_t * pNodeNew, * pTemp;
    int i;
    // create a new node
    pNodeNew = Abc_NtkCreateNode( pNtkNew );
    // add the fanins in the order, in which they appear in the reordered manager
    Abc_NtkForEachCi( pNtkNew, pTemp, i )
        Abc_ObjAddFanin( pNodeNew, Abc_NtkCi(pNtkNew, dd->invperm[i]) );
    // transfer the function
209
    pNodeNew->pData = Extra_TransferLevelByLevel( dd, (DdManager *)pNtkNew->pManFunc, bFunc );  Cudd_Ref( (DdNode *)pNodeNew->pData );
Alan Mishchenko committed
210 211 212 213 214 215 216 217
    return pNodeNew;
}

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


218 219
ABC_NAMESPACE_IMPL_END