abcRenode.c 9.78 KB
Newer Older
Alan Mishchenko committed
1 2 3 4 5 6 7 8
/**CFile****************************************************************

  FileName    [abcRenode.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [Network and node package.]

Alan Mishchenko committed
9
  Synopsis    []
Alan Mishchenko committed
10 11 12 13 14 15 16 17 18 19 20

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

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

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

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

21 22 23 24 25
#include "base/abc/abc.h"
#include "bdd/reo/reo.h"
#include "map/if/if.h"
#include "bool/kit/kit.h"
#include "misc/extra/extraBdd.h"
Alan Mishchenko committed
26

27 28 29
ABC_NAMESPACE_IMPL_START


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

34 35 36 37 38
static int Abc_NtkRenodeEvalAig( If_Man_t * p, If_Cut_t * pCut );
static int Abc_NtkRenodeEvalBdd( If_Man_t * p, If_Cut_t * pCut );
static int Abc_NtkRenodeEvalSop( If_Man_t * p, If_Cut_t * pCut );
static int Abc_NtkRenodeEvalCnf( If_Man_t * p, If_Cut_t * pCut );
static int Abc_NtkRenodeEvalMv( If_Man_t * p, If_Cut_t * pCut );
Alan Mishchenko committed
39

Alan Mishchenko committed
40 41 42 43
static reo_man * s_pReo       = NULL;
static DdManager * s_pDd      = NULL;
static Vec_Int_t * s_vMemory  = NULL;
static Vec_Int_t * s_vMemory2 = NULL;
Alan Mishchenko committed
44

Alan Mishchenko committed
45
static int nDsdCounter = 0;
Alan Mishchenko committed
46

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

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

Alan Mishchenko committed
53
  Synopsis    [Performs renoding as technology mapping.]
Alan Mishchenko committed
54

Alan Mishchenko committed
55
  Description []
Alan Mishchenko committed
56 57 58 59 60 61
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
62
Abc_Ntk_t * Abc_NtkRenode( Abc_Ntk_t * pNtk, int nFaninMax, int nCubeMax, int nFlowIters, int nAreaIters, int fArea, int fUseBdds, int fUseSops, int fUseCnfs, int fUseMv, int fVerbose )
Alan Mishchenko committed
63
{
Alan Mishchenko committed
64 65
    extern Abc_Ntk_t * Abc_NtkIf( Abc_Ntk_t * pNtk, If_Par_t * pPars );
    If_Par_t Pars, * pPars = &Pars;
Alan Mishchenko committed
66 67
    Abc_Ntk_t * pNtkNew;

Alan Mishchenko committed
68
    if ( Abc_NtkGetChoiceNum( pNtk ) )
Alan Mishchenko committed
69 70 71 72 73 74 75 76 77 78 79 80
        printf( "Performing renoding with choices.\n" );

    nDsdCounter = 0;

    // set defaults
    memset( pPars, 0, sizeof(If_Par_t) );
    // user-controlable paramters
    pPars->nLutSize    =  nFaninMax;
    pPars->nCutsMax    =  nCubeMax;
    pPars->nFlowIters  =  nFlowIters;
    pPars->nAreaIters  =  nAreaIters;
    pPars->DelayTarget = -1;
Alan Mishchenko committed
81
    pPars->Epsilon     =  (float)0.005;
Alan Mishchenko committed
82 83 84 85 86 87 88 89 90
    pPars->fPreprocess =  1;
    pPars->fArea       =  fArea;
    pPars->fFancy      =  0;
    pPars->fExpRed     =  0; //
    pPars->fLatchPaths =  0;
    pPars->fVerbose    =  fVerbose;
    // internal parameters
    pPars->fTruth      =  1;
    pPars->fUsePerm    =  1; 
91 92
    pPars->nLatchesCi  =  0;
    pPars->nLatchesCo  =  0;
Alan Mishchenko committed
93 94 95 96 97 98 99 100 101 102 103 104
    pPars->pLutLib     =  NULL; // Abc_FrameReadLibLut();
    pPars->pTimesArr   =  NULL; 
    pPars->pTimesArr   =  NULL;   
    pPars->fUseBdds    =  fUseBdds;
    pPars->fUseSops    =  fUseSops;
    pPars->fUseCnfs    =  fUseCnfs;
    pPars->fUseMv      =  fUseMv;
    if ( fUseBdds )
        pPars->pFuncCost = Abc_NtkRenodeEvalBdd;
    else if ( fUseSops )
        pPars->pFuncCost = Abc_NtkRenodeEvalSop;
    else if ( fUseCnfs )
Alan Mishchenko committed
105
    {
Alan Mishchenko committed
106 107
        pPars->fArea = 1;
        pPars->pFuncCost = Abc_NtkRenodeEvalCnf;
Alan Mishchenko committed
108
    }
Alan Mishchenko committed
109 110 111 112
    else if ( fUseMv )
        pPars->pFuncCost = Abc_NtkRenodeEvalMv;
    else
        pPars->pFuncCost = Abc_NtkRenodeEvalAig;
Alan Mishchenko committed
113

Alan Mishchenko committed
114 115
    // start the manager
    if ( fUseBdds )
Alan Mishchenko committed
116
    {
Alan Mishchenko committed
117 118 119 120
        assert( s_pReo == NULL );
        s_pDd  = Cudd_Init( nFaninMax, 0, CUDD_UNIQUE_SLOTS, CUDD_CACHE_SLOTS, 0 );
        s_pReo = Extra_ReorderInit( nFaninMax, 100 );
        pPars->pReoMan  = s_pReo;
Alan Mishchenko committed
121
    }
Alan Mishchenko committed
122
    else
Alan Mishchenko committed
123
    {
Alan Mishchenko committed
124 125 126
        assert( s_vMemory == NULL );
        s_vMemory  = Vec_IntAlloc( 1 << 16 );
        s_vMemory2 = Vec_IntAlloc( 1 << 16 );
Alan Mishchenko committed
127
    }
Alan Mishchenko committed
128

Alan Mishchenko committed
129 130
    // perform mapping/renoding
    pNtkNew = Abc_NtkIf( pNtk, pPars );
Alan Mishchenko committed
131

Alan Mishchenko committed
132 133
    // start the manager
    if ( fUseBdds )
Alan Mishchenko committed
134
    {
Alan Mishchenko committed
135 136 137 138
        Extra_StopManager( s_pDd );
        Extra_ReorderQuit( s_pReo );
        s_pReo = NULL;
        s_pDd  = NULL;
Alan Mishchenko committed
139
    }
Alan Mishchenko committed
140
    else
Alan Mishchenko committed
141
    {
Alan Mishchenko committed
142 143 144 145
        Vec_IntFree( s_vMemory );
        Vec_IntFree( s_vMemory2 );
        s_vMemory = NULL;
        s_vMemory2 = NULL;
Alan Mishchenko committed
146 147
    }

Alan Mishchenko committed
148
//    printf( "Decomposed %d functions.\n", nDsdCounter );
Alan Mishchenko committed
149

Alan Mishchenko committed
150
    return pNtkNew;
Alan Mishchenko committed
151
}
Alan Mishchenko committed
152 153 154

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

Alan Mishchenko committed
155
  Synopsis    [Computes the cost based on the factored form.]
Alan Mishchenko committed
156

Alan Mishchenko committed
157
  Description []
Alan Mishchenko committed
158 159 160 161 162 163
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
164
int Abc_NtkRenodeEvalAig( If_Man_t * p, If_Cut_t * pCut )
Alan Mishchenko committed
165
{
166
    char * pPerm = If_CutPerm( pCut );
Alan Mishchenko committed
167 168
    Kit_Graph_t * pGraph;
    int i, nNodes;
169
    pGraph = Kit_TruthToGraph( If_CutTruth(p, pCut), If_CutLeaveNum(pCut), s_vMemory );
Alan Mishchenko committed
170
    if ( pGraph == NULL )
Alan Mishchenko committed
171
    {
Alan Mishchenko committed
172
        for ( i = 0; i < If_CutLeaveNum(pCut); i++ )
173
            pPerm[i] = 100;
Alan Mishchenko committed
174
        return IF_COST_MAX;
Alan Mishchenko committed
175
    }
Alan Mishchenko committed
176 177
    nNodes = Kit_GraphNodeNum( pGraph );
    for ( i = 0; i < If_CutLeaveNum(pCut); i++ )
178
        pPerm[i] = Kit_GraphLeafDepth_rec( pGraph, Kit_GraphNodeLast(pGraph), Kit_GraphNode(pGraph, i) );
Alan Mishchenko committed
179 180 181
    Kit_GraphFree( pGraph );
    return nNodes;
}
Alan Mishchenko committed
182 183 184

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

Alan Mishchenko committed
185
  Synopsis    [Computes the cost based on the BDD size after reordering.]
Alan Mishchenko committed
186 187 188 189 190 191 192 193

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
194
int Abc_NtkRenodeEvalBdd( If_Man_t * p, If_Cut_t * pCut )
Alan Mishchenko committed
195
{
196
    char * pPerm = If_CutPerm( pCut );
Alan Mishchenko committed
197 198 199 200
    int pOrder[IF_MAX_LUTSIZE];
    DdNode * bFunc, * bFuncNew;
    int i, k, nNodes;
    for ( i = 0; i < If_CutLeaveNum(pCut); i++ )
201
        pPerm[i] = pOrder[i] = -100;
202
    bFunc = Kit_TruthToBdd( s_pDd, If_CutTruth(p, pCut), If_CutLeaveNum(pCut), 0 );  Cudd_Ref( bFunc );
Alan Mishchenko committed
203 204 205
    bFuncNew = Extra_Reorder( s_pReo, s_pDd, bFunc, pOrder );                     Cudd_Ref( bFuncNew );
    for ( i = k = 0; i < If_CutLeaveNum(pCut); i++ )
        if ( pOrder[i] >= 0 )
206
            pPerm[pOrder[i]] = ++k; // double-check this!
Alan Mishchenko committed
207 208 209 210
    nNodes = -1 + Cudd_DagSize( bFuncNew );
    Cudd_RecursiveDeref( s_pDd, bFuncNew );
    Cudd_RecursiveDeref( s_pDd, bFunc );
    return nNodes; 
Alan Mishchenko committed
211 212 213 214
}

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

Alan Mishchenko committed
215
  Synopsis    [Computes the cost based on ISOP.]
Alan Mishchenko committed
216 217 218 219 220 221 222 223

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
224
int Abc_NtkRenodeEvalSop( If_Man_t * p, If_Cut_t * pCut )
Alan Mishchenko committed
225
{
226
    char * pPerm = If_CutPerm( pCut );
Alan Mishchenko committed
227 228
    int i, RetValue;
    for ( i = 0; i < If_CutLeaveNum(pCut); i++ )
229
        pPerm[i] = 1;
230
    RetValue = Kit_TruthIsop( If_CutTruth(p, pCut), If_CutLeaveNum(pCut), s_vMemory, 1 );
Alan Mishchenko committed
231 232 233 234
    if ( RetValue == -1 )
        return IF_COST_MAX;
    assert( RetValue == 0 || RetValue == 1 );
    return Vec_IntSize( s_vMemory );
Alan Mishchenko committed
235 236 237
}

/**Function*************************************************************
Alan Mishchenko committed
238

Alan Mishchenko committed
239
  Synopsis    [Computes the cost based on two ISOPs.]
Alan Mishchenko committed
240 241 242 243 244 245 246 247

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
248
int Abc_NtkRenodeEvalCnf( If_Man_t * p, If_Cut_t * pCut )
Alan Mishchenko committed
249
{
250
    char * pPerm = If_CutPerm( pCut );
Alan Mishchenko committed
251 252 253
    int i, RetValue, nClauses;
    // set internal mapper parameters
    for ( i = 0; i < If_CutLeaveNum(pCut); i++ )
254
        pPerm[i] = 1;
Alan Mishchenko committed
255
    // compute ISOP for the positive phase
256
    RetValue = Kit_TruthIsop( If_CutTruth(p, pCut), If_CutLeaveNum(pCut), s_vMemory, 0 );
Alan Mishchenko committed
257 258 259 260 261
    if ( RetValue == -1 )
        return IF_COST_MAX;
    assert( RetValue == 0 || RetValue == 1 );
    nClauses = Vec_IntSize( s_vMemory );
    // compute ISOP for the negative phase
262 263 264
    Kit_TruthNot( If_CutTruth(p, pCut), If_CutTruth(p, pCut), If_CutLeaveNum(pCut) );
    RetValue = Kit_TruthIsop( If_CutTruth(p, pCut), If_CutLeaveNum(pCut), s_vMemory, 0 );
    Kit_TruthNot( If_CutTruth(p, pCut), If_CutTruth(p, pCut), If_CutLeaveNum(pCut) );
Alan Mishchenko committed
265 266 267 268 269
    if ( RetValue == -1 )
        return IF_COST_MAX;
    assert( RetValue == 0 || RetValue == 1 );
    nClauses += Vec_IntSize( s_vMemory );
    return nClauses;
Alan Mishchenko committed
270 271 272
}

/**Function*************************************************************
Alan Mishchenko committed
273

Alan Mishchenko committed
274
  Synopsis    [Computes the cost of MV-SOP of the cut function.]
Alan Mishchenko committed
275 276 277 278 279 280 281 282

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
283
int Abc_NtkRenodeEvalMv( If_Man_t * p, If_Cut_t * pCut )
Alan Mishchenko committed
284
{
285
    char * pPerm = If_CutPerm( pCut );
Alan Mishchenko committed
286 287 288
    int i, RetValue;
    // set internal mapper parameters
    for ( i = 0; i < If_CutLeaveNum(pCut); i++ )
289
        pPerm[i] = 1;
Alan Mishchenko committed
290
    // compute ISOP for the positive phase
291
    RetValue = Kit_TruthIsop( If_CutTruth(p, pCut), If_CutLeaveNum(pCut), s_vMemory, 0 );
Alan Mishchenko committed
292 293 294 295
    if ( RetValue == -1 )
        return IF_COST_MAX;
    assert( RetValue == 0 || RetValue == 1 );
    // compute ISOP for the negative phase
296 297 298
    Kit_TruthNot( If_CutTruth(p, pCut), If_CutTruth(p, pCut), If_CutLeaveNum(pCut) );
    RetValue = Kit_TruthIsop( If_CutTruth(p, pCut), If_CutLeaveNum(pCut), s_vMemory2, 0 );
    Kit_TruthNot( If_CutTruth(p, pCut), If_CutTruth(p, pCut), If_CutLeaveNum(pCut) );
Alan Mishchenko committed
299 300 301 302 303 304 305 306
    if ( RetValue == -1 )
        return IF_COST_MAX;
    assert( RetValue == 0 || RetValue == 1 );
    // return the cost of the cut 
    RetValue = Abc_NodeEvalMvCost( If_CutLeaveNum(pCut), s_vMemory, s_vMemory2 );
    if ( RetValue >= IF_COST_MAX )
        return IF_COST_MAX;
    return RetValue;
Alan Mishchenko committed
307 308 309 310 311 312 313
}

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


314 315
ABC_NAMESPACE_IMPL_END