ntlSweep.c 6.78 KB
Newer Older
Alan Mishchenko committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/**CFile****************************************************************

  FileName    [ntlSweep.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [Netlist representation.]

  Synopsis    [Performs structural sweep of the netlist.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

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

Alan Mishchenko committed
17
  Revision    [$Id: ntlSweep.c,v 1.1 2008/10/10 14:09:30 mjarvin Exp $]
Alan Mishchenko committed
18 19 20 21 22

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

#include "ntl.h"

23 24 25
ABC_NAMESPACE_IMPL_START


Alan Mishchenko committed
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 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
////////////////////////////////////////////////////////////////////////
///                        DECLARATIONS                              ///
////////////////////////////////////////////////////////////////////////

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

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

  Synopsis    [Detects logic that does not fanout into POs.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Ntl_ManSweepMark_rec( Ntl_Man_t * p, Ntl_Obj_t * pObj )
{
    Ntl_Net_t * pNet;
    int i;
    if ( pObj->fMark )
        return;
    pObj->fMark = 1;
    Ntl_ObjForEachFanin( pObj, pNet, i )
        Ntl_ManSweepMark_rec( p, pNet->pDriver );
}

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

  Synopsis    [Detects logic that does not fanout into POs.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Ntl_ManSweepMark( Ntl_Man_t * p )
{
    Ntl_Mod_t * pRoot;
    Ntl_Obj_t * pObj;
    int i;
    // get the root model
    pRoot = Ntl_ManRootModel( p );
    // clear net visited flags
    Ntl_ModelForEachObj( pRoot, pObj, i )
        assert( pObj->fMark == 0 );
    // label the primary inputs
    Ntl_ModelForEachPi( pRoot, pObj, i )
        pObj->fMark = 1;
    // start from the primary outputs
    Ntl_ModelForEachPo( pRoot, pObj, i )
        Ntl_ManSweepMark_rec( p, pObj );
Alan Mishchenko committed
83 84
    // start from the persistant boxes
    Ntl_ModelForEachBox( pRoot, pObj, i )
Alan Mishchenko committed
85
        if ( pObj->pImplem->attrKeep )
Alan Mishchenko committed
86
            Ntl_ManSweepMark_rec( p, pObj );
Alan Mishchenko committed
87 88 89 90
}

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

Alan Mishchenko committed
91
  Synopsis    [Removes logic that does not fanout into POs.]
Alan Mishchenko committed
92 93 94 95 96 97 98 99

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
100
int Ntl_ManSweep( Ntl_Man_t * p, int fVerbose )
Alan Mishchenko committed
101 102
{
    int nObjsOld[NTL_OBJ_VOID];
Alan Mishchenko committed
103
    Ntl_Mod_t * pRoot, * pMod;
Alan Mishchenko committed
104 105 106
    Ntl_Net_t * pNet;
    Ntl_Obj_t * pObj;
    int i, k, nNetsOld;
Alan Mishchenko committed
107
    int ModelCounter = 0, Counter = 0;
Alan Mishchenko committed
108 109

    // remember the number of objects
Alan Mishchenko committed
110
    pRoot = Ntl_ManRootModel( p );
Alan Mishchenko committed
111 112 113 114 115
    for ( i = 0; i < NTL_OBJ_VOID; i++ )
        nObjsOld[i] = pRoot->nObjs[i];
    nNetsOld = Ntl_ModelCountNets(pRoot);

    // mark the nets that do not fanout into POs
Alan Mishchenko committed
116
    Ntl_ManSweepMark( p );
Alan Mishchenko committed
117

Alan Mishchenko committed
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
    // count how many boxes of each type are swept away
    if ( fVerbose )
    {
    Ntl_ManForEachModel( p, pMod, i )
        pMod->nUsed = pMod->nRems = 0;
    Ntl_ModelForEachObj( pRoot, pObj, i )
        if ( Ntl_ObjIsBox(pObj) && pObj->pImplem )
        {
            pObj->pImplem->nUsed++;
            if ( !pObj->fMark )
            {
                if ( pObj->pImplem->nRems++ == 0 )
                    ModelCounter++;
            }
        }
    }

Alan Mishchenko committed
135 136 137 138 139 140 141 142 143 144
    // remove the useless objects and their nets
    Ntl_ModelForEachObj( pRoot, pObj, i )
    {
        if ( pObj->fMark )
        {
            pObj->fMark = 0;
            continue;
        }
        // remove the fanout nets
        Ntl_ObjForEachFanout( pObj, pNet, k )
Alan Mishchenko committed
145 146
            if ( pNet != NULL && pNet->pName[0] != 0 )
            {
Alan Mishchenko committed
147
                Ntl_ModelDeleteNet( pRoot, pNet );
Alan Mishchenko committed
148 149
                Vec_PtrWriteEntry( pRoot->vNets, pNet->NetId, NULL );
            }
Alan Mishchenko committed
150 151 152
        // remove the object
        if ( Ntl_ObjIsNode(pObj) && Ntl_ObjFaninNum(pObj) == 1 )
            pRoot->nObjs[NTL_OBJ_LUT1]--;
Alan Mishchenko committed
153
        else 
Alan Mishchenko committed
154 155
            pRoot->nObjs[pObj->Type]--;
        Vec_PtrWriteEntry( pRoot->vObjs, pObj->Id, NULL );
Alan Mishchenko committed
156 157
        pObj->Type = NTL_OBJ_NONE;
        Counter++;
Alan Mishchenko committed
158
    }
Alan Mishchenko committed
159
 
Alan Mishchenko committed
160 161


Alan Mishchenko committed
162
    // print detailed statistics of sweeping
Alan Mishchenko committed
163
    if ( fVerbose && Counter > 0)
Alan Mishchenko committed
164
    {
Alan Mishchenko committed
165 166
        int numLutBox = 0;

Alan Mishchenko committed
167
        printf( "Swept away:" );
Alan Mishchenko committed
168 169 170 171 172 173 174 175 176 177 178 179 180
        printf( "  Node = %d (%4.1f %%)", 
            nObjsOld[NTL_OBJ_NODE] - pRoot->nObjs[NTL_OBJ_NODE],
            !nObjsOld[NTL_OBJ_NODE]? 0.0: 100.0 * (nObjsOld[NTL_OBJ_NODE] - pRoot->nObjs[NTL_OBJ_NODE]) / nObjsOld[NTL_OBJ_NODE] );
        printf( "  Buf/Inv = %d (%4.1f %%)", 
            nObjsOld[NTL_OBJ_LUT1] - pRoot->nObjs[NTL_OBJ_LUT1],
            !nObjsOld[NTL_OBJ_LUT1]? 0.0: 100.0 * (nObjsOld[NTL_OBJ_LUT1] - pRoot->nObjs[NTL_OBJ_LUT1]) / nObjsOld[NTL_OBJ_LUT1] );
        printf( "  Lat = %d (%4.1f %%)", 
            nObjsOld[NTL_OBJ_LATCH] - pRoot->nObjs[NTL_OBJ_LATCH],
            !nObjsOld[NTL_OBJ_LATCH]? 0.0: 100.0 * (nObjsOld[NTL_OBJ_LATCH] - pRoot->nObjs[NTL_OBJ_LATCH]) / nObjsOld[NTL_OBJ_LATCH] );
        printf( "  Box = %d (%4.1f %%)", 
            nObjsOld[NTL_OBJ_BOX] - pRoot->nObjs[NTL_OBJ_BOX],
            !nObjsOld[NTL_OBJ_BOX]? 0.0: 100.0 * (nObjsOld[NTL_OBJ_BOX] - pRoot->nObjs[NTL_OBJ_BOX]) / nObjsOld[NTL_OBJ_BOX] );
        printf( "\n" );
Alan Mishchenko committed
181 182 183 184 185
        if ( ModelCounter )
        {
            printf( "Sweep removed %d boxed of %d types (out of %d types):\n", 
                nObjsOld[NTL_OBJ_BOX] - pRoot->nObjs[NTL_OBJ_BOX], ModelCounter, Vec_PtrSize(p->vModels)-1 );
            Ntl_ManForEachModel( p, pMod, i )
Alan Mishchenko committed
186 187 188 189 190 191 192 193 194 195 196 197 198 199
            {
                if ( i && (pMod->nRems + pMod->nUsed-pMod->nRems) > 0)
                {

                    if (strncmp(pMod->pName, "LUT", 3) != 0)
                    {
                        //printf( "( M%d: %s,  S=%d, L=%d ) ", i, pMod->pName, pMod->nRems, pMod->nUsed-pMod->nRems );
                        //printf( "Model %3d : %-40s  Swept = %5d. Left = %5d.\n", i, pMod->pName, pMod->nRems, pMod->nUsed-pMod->nRems );
                    } else
                    {
                        numLutBox++;
                    }
                }
            }
Alan Mishchenko committed
200
//            printf("\nLUTboxType =%d\n", numLutBox);
Alan Mishchenko committed
201
        }
Alan Mishchenko committed
202
    }
Alan Mishchenko committed
203 204 205
    if ( !Ntl_ManCheck( p ) )
        printf( "Ntl_ManSweep: The check has failed for design %s.\n", p->pName );
    return Counter;
Alan Mishchenko committed
206 207 208 209 210 211 212
}

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


213 214
ABC_NAMESPACE_IMPL_END