abcRenode.c 9.84 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 91
    pPars->fPreprocess =  1;
    pPars->fArea       =  fArea;
    pPars->fFancy      =  0;
    pPars->fExpRed     =  0; //
    pPars->fLatchPaths =  0;
    pPars->fSeqMap     =  0;
    pPars->fVerbose    =  fVerbose;
    // internal parameters
    pPars->fTruth      =  1;
    pPars->fUsePerm    =  1; 
92 93
    pPars->nLatchesCi  =  0;
    pPars->nLatchesCo  =  0;
Alan Mishchenko committed
94 95 96 97 98 99 100 101 102 103 104 105
    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
106
    {
Alan Mishchenko committed
107 108
        pPars->fArea = 1;
        pPars->pFuncCost = Abc_NtkRenodeEvalCnf;
Alan Mishchenko committed
109
    }
Alan Mishchenko committed
110 111 112 113
    else if ( fUseMv )
        pPars->pFuncCost = Abc_NtkRenodeEvalMv;
    else
        pPars->pFuncCost = Abc_NtkRenodeEvalAig;
Alan Mishchenko committed
114

Alan Mishchenko committed
115 116
    // start the manager
    if ( fUseBdds )
Alan Mishchenko committed
117
    {
Alan Mishchenko committed
118 119 120 121
        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
122
    }
Alan Mishchenko committed
123
    else
Alan Mishchenko committed
124
    {
Alan Mishchenko committed
125 126 127
        assert( s_vMemory == NULL );
        s_vMemory  = Vec_IntAlloc( 1 << 16 );
        s_vMemory2 = Vec_IntAlloc( 1 << 16 );
Alan Mishchenko committed
128
    }
Alan Mishchenko committed
129

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

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

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

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

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

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

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

  SeeAlso     []

***********************************************************************/
165
int Abc_NtkRenodeEvalAig( If_Man_t * p, If_Cut_t * pCut )
Alan Mishchenko committed
166
{
Alan Mishchenko committed
167 168 169 170 171
    Kit_Graph_t * pGraph;
    int i, nNodes;
/*
extern void Kit_DsdTest( unsigned * pTruth, int nVars );
if ( If_CutLeaveNum(pCut) == 8 )
Alan Mishchenko committed
172
{
Alan Mishchenko committed
173
    nDsdCounter++;
174
    Kit_DsdTest( If_CutTruth(p, pCut), If_CutLeaveNum(pCut) );
Alan Mishchenko committed
175 176
}
*/
177
    pGraph = Kit_TruthToGraph( If_CutTruth(p, pCut), If_CutLeaveNum(pCut), s_vMemory );
Alan Mishchenko committed
178
    if ( pGraph == NULL )
Alan Mishchenko committed
179
    {
Alan Mishchenko committed
180 181 182
        for ( i = 0; i < If_CutLeaveNum(pCut); i++ )
            pCut->pPerm[i] = 100;
        return IF_COST_MAX;
Alan Mishchenko committed
183
    }
Alan Mishchenko committed
184 185 186 187 188 189
    nNodes = Kit_GraphNodeNum( pGraph );
    for ( i = 0; i < If_CutLeaveNum(pCut); i++ )
        pCut->pPerm[i] = Kit_GraphLeafDepth_rec( pGraph, Kit_GraphNodeLast(pGraph), Kit_GraphNode(pGraph, i) );
    Kit_GraphFree( pGraph );
    return nNodes;
}
Alan Mishchenko committed
190 191 192

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

Alan Mishchenko committed
193
  Synopsis    [Computes the cost based on the BDD size after reordering.]
Alan Mishchenko committed
194 195 196 197 198 199 200 201

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
202
int Abc_NtkRenodeEvalBdd( If_Man_t * p, If_Cut_t * pCut )
Alan Mishchenko committed
203
{
Alan Mishchenko committed
204 205 206 207 208
    int pOrder[IF_MAX_LUTSIZE];
    DdNode * bFunc, * bFuncNew;
    int i, k, nNodes;
    for ( i = 0; i < If_CutLeaveNum(pCut); i++ )
        pCut->pPerm[i] = pOrder[i] = -100;
209
    bFunc = Kit_TruthToBdd( s_pDd, If_CutTruth(p, pCut), If_CutLeaveNum(pCut), 0 );  Cudd_Ref( bFunc );
Alan Mishchenko committed
210 211 212 213 214 215 216 217
    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 )
            pCut->pPerm[pOrder[i]] = ++k; // double-check this!
    nNodes = -1 + Cudd_DagSize( bFuncNew );
    Cudd_RecursiveDeref( s_pDd, bFuncNew );
    Cudd_RecursiveDeref( s_pDd, bFunc );
    return nNodes; 
Alan Mishchenko committed
218 219 220 221
}

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

Alan Mishchenko committed
222
  Synopsis    [Computes the cost based on ISOP.]
Alan Mishchenko committed
223 224 225 226 227 228 229 230

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
231
int Abc_NtkRenodeEvalSop( If_Man_t * p, If_Cut_t * pCut )
Alan Mishchenko committed
232
{
Alan Mishchenko committed
233 234 235
    int i, RetValue;
    for ( i = 0; i < If_CutLeaveNum(pCut); i++ )
        pCut->pPerm[i] = 1;
236
    RetValue = Kit_TruthIsop( If_CutTruth(p, pCut), If_CutLeaveNum(pCut), s_vMemory, 1 );
Alan Mishchenko committed
237 238 239 240
    if ( RetValue == -1 )
        return IF_COST_MAX;
    assert( RetValue == 0 || RetValue == 1 );
    return Vec_IntSize( s_vMemory );
Alan Mishchenko committed
241 242 243
}

/**Function*************************************************************
Alan Mishchenko committed
244

Alan Mishchenko committed
245
  Synopsis    [Computes the cost based on two ISOPs.]
Alan Mishchenko committed
246 247 248 249 250 251 252 253

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
254
int Abc_NtkRenodeEvalCnf( If_Man_t * p, If_Cut_t * pCut )
Alan Mishchenko committed
255
{
Alan Mishchenko committed
256 257 258 259 260
    int i, RetValue, nClauses;
    // set internal mapper parameters
    for ( i = 0; i < If_CutLeaveNum(pCut); i++ )
        pCut->pPerm[i] = 1;
    // compute ISOP for the positive phase
261
    RetValue = Kit_TruthIsop( If_CutTruth(p, pCut), If_CutLeaveNum(pCut), s_vMemory, 0 );
Alan Mishchenko committed
262 263 264 265 266
    if ( RetValue == -1 )
        return IF_COST_MAX;
    assert( RetValue == 0 || RetValue == 1 );
    nClauses = Vec_IntSize( s_vMemory );
    // compute ISOP for the negative phase
267 268 269
    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
270 271 272 273 274
    if ( RetValue == -1 )
        return IF_COST_MAX;
    assert( RetValue == 0 || RetValue == 1 );
    nClauses += Vec_IntSize( s_vMemory );
    return nClauses;
Alan Mishchenko committed
275 276 277
}

/**Function*************************************************************
Alan Mishchenko committed
278

Alan Mishchenko committed
279
  Synopsis    [Computes the cost of MV-SOP of the cut function.]
Alan Mishchenko committed
280 281 282 283 284 285 286 287

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
288
int Abc_NtkRenodeEvalMv( If_Man_t * p, If_Cut_t * pCut )
Alan Mishchenko committed
289
{
Alan Mishchenko committed
290 291 292 293 294
    int i, RetValue;
    // set internal mapper parameters
    for ( i = 0; i < If_CutLeaveNum(pCut); i++ )
        pCut->pPerm[i] = 1;
    // compute ISOP for the positive phase
295
    RetValue = Kit_TruthIsop( If_CutTruth(p, pCut), If_CutLeaveNum(pCut), s_vMemory, 0 );
Alan Mishchenko committed
296 297 298 299
    if ( RetValue == -1 )
        return IF_COST_MAX;
    assert( RetValue == 0 || RetValue == 1 );
    // compute ISOP for the negative phase
300 301 302
    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
303 304 305 306 307 308 309 310
    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
311 312 313 314 315 316 317
}

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


318 319
ABC_NAMESPACE_IMPL_END