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

  FileName    [abcMfs.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [Network and node package.]

  Synopsis    [Optimization with don't-cares.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

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

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

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

#include "base/abc/abc.h"
Alan Mishchenko committed
22
#include "bool/kit/kit.h"
Alan Mishchenko committed
23 24 25 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
#include "opt/sfm/sfm.h"

ABC_NAMESPACE_IMPL_START


////////////////////////////////////////////////////////////////////////
///                        DECLARATIONS                              ///
////////////////////////////////////////////////////////////////////////
 
////////////////////////////////////////////////////////////////////////
///                     FUNCTION DEFINITIONS                         ///
////////////////////////////////////////////////////////////////////////

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Vec_Ptr_t * Abc_NtkAssignIDs( Abc_Ntk_t * pNtk )
{
    Vec_Ptr_t * vNodes;
    Abc_Obj_t * pObj;
    int i;
    vNodes = Abc_NtkDfs( pNtk, 0 );
    Abc_NtkCleanCopy( pNtk );
Alan Mishchenko committed
54
    Abc_NtkForEachCi( pNtk, pObj, i )
Alan Mishchenko committed
55 56
        pObj->iTemp = i;
    Vec_PtrForEachEntry( Abc_Obj_t *, vNodes, pObj, i )
Alan Mishchenko committed
57
    {
Alan Mishchenko committed
58
        pObj->iTemp = Abc_NtkCiNum(pNtk) + i;
Alan Mishchenko committed
59 60 61
//printf( "%d->%d ", pObj->Id, pObj->iTemp );
    }
//printf( "\n" );
Alan Mishchenko committed
62 63
    Abc_NtkForEachCo( pNtk, pObj, i )
        pObj->iTemp = Abc_NtkCiNum(pNtk) + Vec_PtrSize(vNodes) + i;
Alan Mishchenko committed
64 65
    return vNodes;
}
66 67 68 69 70 71
Vec_Ptr_t * Abc_NtkAssignIDs2( Abc_Ntk_t * pNtk )
{
    Vec_Ptr_t * vNodes;
    Abc_Obj_t * pObj;
    int i;
    Abc_NtkCleanCopy( pNtk );
Alan Mishchenko committed
72
    Abc_NtkForEachCi( pNtk, pObj, i )
73 74 75 76
        pObj->iTemp = i;
    vNodes = Vec_PtrAlloc( Abc_NtkNodeNum(pNtk) );
    Abc_NtkForEachNode( pNtk, pObj, i )
    {
77
        pObj->iTemp = Abc_NtkCiNum(pNtk) + Vec_PtrSize(vNodes);
78 79
        Vec_PtrPush( vNodes, pObj );
    }
80
    assert( Vec_PtrSize(vNodes) == Abc_NtkNodeNum(pNtk) );
Alan Mishchenko committed
81 82
    Abc_NtkForEachCo( pNtk, pObj, i )
        pObj->iTemp = Abc_NtkCiNum(pNtk) + Vec_PtrSize(vNodes) + i;
83 84
    return vNodes;
}
Alan Mishchenko committed
85 86 87 88 89 90 91 92 93 94 95 96

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

  Synopsis    [Extracts information about the network.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
97
Sfm_Ntk_t * Abc_NtkExtractMfs( Abc_Ntk_t * pNtk, int nFirstFixed )
Alan Mishchenko committed
98 99 100 101 102 103 104 105
{
    Vec_Ptr_t * vNodes;
    Vec_Wec_t * vFanins;
    Vec_Str_t * vFixed;
    Vec_Wrd_t * vTruths;
    Vec_Int_t * vArray;
    Abc_Obj_t * pObj, * pFanin;
    int i, k, nObjs;
106
    vNodes  = nFirstFixed ? Abc_NtkAssignIDs2(pNtk) : Abc_NtkAssignIDs(pNtk);
Alan Mishchenko committed
107
    nObjs   = Abc_NtkCiNum(pNtk) + Vec_PtrSize(vNodes) + Abc_NtkCoNum(pNtk);
Alan Mishchenko committed
108 109 110 111 112
    vFanins = Vec_WecStart( nObjs );
    vFixed  = Vec_StrStart( nObjs );
    vTruths = Vec_WrdStart( nObjs );
    Vec_PtrForEachEntry( Abc_Obj_t *, vNodes, pObj, i )
    {
Alan Mishchenko committed
113 114 115 116
        word uTruth = Abc_SopToTruth((char *)pObj->pData, Abc_ObjFaninNum(pObj));
        Vec_WrdWriteEntry( vTruths, pObj->iTemp, uTruth );
        if ( uTruth == 0 || ~uTruth == 0 )
            continue;
Alan Mishchenko committed
117
        vArray = Vec_WecEntry( vFanins, pObj->iTemp );
Alan Mishchenko committed
118 119 120 121
        Vec_IntGrow( vArray, Abc_ObjFaninNum(pObj) );
        Abc_ObjForEachFanin( pObj, pFanin, k )
            Vec_IntPush( vArray, pFanin->iTemp );
    }
Alan Mishchenko committed
122
    Abc_NtkForEachCo( pNtk, pObj, i )
Alan Mishchenko committed
123 124 125 126 127 128 129
    {
        vArray = Vec_WecEntry( vFanins, pObj->iTemp );
        Vec_IntGrow( vArray, Abc_ObjFaninNum(pObj) );
        Abc_ObjForEachFanin( pObj, pFanin, k )
            Vec_IntPush( vArray, pFanin->iTemp );
    }
    Vec_PtrFree( vNodes );
Alan Mishchenko committed
130
    for ( i = Abc_NtkCiNum(pNtk); i < Abc_NtkCiNum(pNtk) + nFirstFixed; i++ )
131
        Vec_StrWriteEntry( vFixed, i, (char)1 );
Alan Mishchenko committed
132 133 134 135 136
    // update fixed
    assert( nFirstFixed >= 0 && nFirstFixed < Abc_NtkNodeNum(pNtk) );
//    for ( i = Abc_NtkCiNum(pNtk); i + Abc_NtkCoNum(pNtk) < Abc_NtkObjNum(pNtk); i++ )
//        if ( rand() % 10 == 0 )
//            Vec_StrWriteEntry( vFixed, i, (char)1 );
137
    return Sfm_NtkConstruct( vFanins, Abc_NtkCiNum(pNtk), Abc_NtkCoNum(pNtk), vFixed, NULL, vTruths );
Alan Mishchenko committed
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Abc_NtkInsertMfs( Abc_Ntk_t * pNtk, Sfm_Ntk_t * p )
{
Alan Mishchenko committed
153
    Vec_Int_t * vCover, * vMap, * vArray;
Alan Mishchenko committed
154
    Abc_Obj_t * pNode;
Alan Mishchenko committed
155
    word * pTruth;
Alan Mishchenko committed
156
    int i, k, Fanin;
Alan Mishchenko committed
157 158
    // map new IDs into old nodes
    vMap = Vec_IntStart( Abc_NtkObjNumMax(pNtk) );
Alan Mishchenko committed
159
    Abc_NtkForEachCi( pNtk, pNode, i )
Alan Mishchenko committed
160 161 162 163 164 165
        Vec_IntWriteEntry( vMap, pNode->iTemp, Abc_ObjId(pNode) );
    Abc_NtkForEachNode( pNtk, pNode, i )
        if ( pNode->iTemp > 0 )
            Vec_IntWriteEntry( vMap, pNode->iTemp, Abc_ObjId(pNode) );
    // remove old fanins
    Abc_NtkForEachNode( pNtk, pNode, i )
Alan Mishchenko committed
166 167
        if ( !Sfm_NodeReadFixed(p, pNode->iTemp) )
            Abc_ObjRemoveFanins( pNode );
Alan Mishchenko committed
168
    // create new fanins
Alan Mishchenko committed
169
    vCover = Vec_IntAlloc( 1 << 16 );
Alan Mishchenko committed
170
    Abc_NtkForEachNode( pNtk, pNode, i )
Alan Mishchenko committed
171 172 173 174
    {
        if ( pNode->iTemp == 0 || Sfm_NodeReadFixed(p, pNode->iTemp) )
            continue;
        if ( !Sfm_NodeReadUsed(p, pNode->iTemp) )
Alan Mishchenko committed
175
        {
Alan Mishchenko committed
176 177
            Abc_NtkDeleteObj( pNode );
            continue;
Alan Mishchenko committed
178
        }
Alan Mishchenko committed
179 180 181 182 183 184 185 186 187 188 189
        // update fanins
        vArray = Sfm_NodeReadFanins( p, pNode->iTemp );
        Vec_IntForEachEntry( vArray, Fanin, k )
            Abc_ObjAddFanin( pNode, Abc_NtkObj(pNtk, Vec_IntEntry(vMap, Fanin)) );
        // update function
        pTruth = Sfm_NodeReadTruth( p, pNode->iTemp );
        if ( pTruth[0] == 0 )
            pNode->pData = Abc_SopRegister( (Mem_Flex_t *)pNtk->pManFunc, " 0\n" );
        else if ( ~pTruth[0] == 0 )
            pNode->pData = Abc_SopRegister( (Mem_Flex_t *)pNtk->pManFunc, " 1\n" );
        else
Alan Mishchenko committed
190 191
        {
            int RetValue = Kit_TruthIsop( (unsigned *)pTruth, Vec_IntSize(vArray), vCover, 1 );
Alan Mishchenko committed
192
            assert( Vec_IntSize(vArray) > 0 );
Alan Mishchenko committed
193 194 195 196 197
            assert( RetValue == 0 || RetValue == 1 );
            pNode->pData = Abc_SopCreateFromIsop( (Mem_Flex_t *)pNtk->pManFunc, Vec_IntSize(vArray), vCover );
            if ( RetValue )
                Abc_SopComplement( (char *)pNode->pData );
        }
Alan Mishchenko committed
198
        assert( Abc_SopGetVarNum((char *)pNode->pData) == Vec_IntSize(vArray) );
Alan Mishchenko committed
199
    }
Alan Mishchenko committed
200
    Vec_IntFree( vCover );
Alan Mishchenko committed
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
    Vec_IntFree( vMap );
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Abc_NtkPerformMfs( Abc_Ntk_t * pNtk, Sfm_Par_t * pPars )
{
    Sfm_Ntk_t * p;
    int nFaninMax, nNodes;
Alan Mishchenko committed
219
    assert( Abc_NtkIsLogic(pNtk) );
Alan Mishchenko committed
220 221 222 223 224 225 226
    // count fanouts
    nFaninMax = Abc_NtkGetFaninMax( pNtk );
    if ( nFaninMax > 6 )
    {
        Abc_Print( 1, "Currently \"mfs\" cannot process the network containing nodes with more than 6 fanins.\n" );
        return 0;
    }
Alan Mishchenko committed
227 228
    if ( !Abc_NtkHasSop(pNtk) )
        Abc_NtkToSop( pNtk, 0 );
Alan Mishchenko committed
229
    // collect information
230
    p = Abc_NtkExtractMfs( pNtk, pPars->nFirstFixed );
Alan Mishchenko committed
231 232 233 234
    // perform optimization
    nNodes = Sfm_NtkPerform( p, pPars );
    // call the fast extract procedure
    if ( nNodes == 0 )
Alan Mishchenko committed
235 236 237
    {
//        Abc_Print( 1, "The network is not changed by \"mfs\".\n" );
    }
Alan Mishchenko committed
238 239 240
    else
    {
        Abc_NtkInsertMfs( pNtk, p );
Alan Mishchenko committed
241 242
        if( pPars->fVerbose )
            Abc_Print( 1, "The network has %d nodes changed by \"mfs\".\n", nNodes );
Alan Mishchenko committed
243 244 245 246 247 248 249 250 251 252 253 254
    }
    Sfm_NtkFree( p );
    return 1;
}

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


ABC_NAMESPACE_IMPL_END