abcRenode.c 9.56 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 21

  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 $]

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

#include "abc.h"
Alan Mishchenko committed
22 23 24
#include "reo.h"
#include "if.h"
#include "kit.h"
Alan Mishchenko committed
25

26 27 28
ABC_NAMESPACE_IMPL_START


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

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

Alan Mishchenko committed
39 40 41 42
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
43

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

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

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

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

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

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
61
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
62
{
Alan Mishchenko committed
63 64
    extern Abc_Ntk_t * Abc_NtkIf( Abc_Ntk_t * pNtk, If_Par_t * pPars );
    If_Par_t Pars, * pPars = &Pars;
Alan Mishchenko committed
65 66
    Abc_Ntk_t * pNtkNew;

Alan Mishchenko committed
67
    if ( Abc_NtkGetChoiceNum( pNtk ) )
Alan Mishchenko committed
68 69 70 71 72 73 74 75 76 77 78 79
        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
80
    pPars->Epsilon     =  (float)0.005;
Alan Mishchenko committed
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
    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; 
    pPars->nLatches    =  0;
    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
104
    {
Alan Mishchenko committed
105 106
        pPars->fArea = 1;
        pPars->pFuncCost = Abc_NtkRenodeEvalCnf;
Alan Mishchenko committed
107
    }
Alan Mishchenko committed
108 109 110 111
    else if ( fUseMv )
        pPars->pFuncCost = Abc_NtkRenodeEvalMv;
    else
        pPars->pFuncCost = Abc_NtkRenodeEvalAig;
Alan Mishchenko committed
112

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

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

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

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

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

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

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

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

  SeeAlso     []

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

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

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

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
200
int Abc_NtkRenodeEvalBdd( If_Cut_t * pCut )
Alan Mishchenko committed
201
{
Alan Mishchenko committed
202 203 204 205 206 207 208 209 210 211 212 213 214 215
    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;
    bFunc = Kit_TruthToBdd( s_pDd, If_CutTruth(pCut), If_CutLeaveNum(pCut), 0 );  Cudd_Ref( bFunc );
    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
216 217 218 219
}

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

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

  Description []
               
  SideEffects []

  SeeAlso     []

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

/**Function*************************************************************
Alan Mishchenko committed
242

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

  Description []
               
  SideEffects []

  SeeAlso     []

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

/**Function*************************************************************
Alan Mishchenko committed
276

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

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
286
int Abc_NtkRenodeEvalMv( If_Cut_t * pCut )
Alan Mishchenko committed
287
{
Alan Mishchenko committed
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
    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
    RetValue = Kit_TruthIsop( If_CutTruth(pCut), If_CutLeaveNum(pCut), s_vMemory, 0 );
    if ( RetValue == -1 )
        return IF_COST_MAX;
    assert( RetValue == 0 || RetValue == 1 );
    // compute ISOP for the negative phase
    Kit_TruthNot( If_CutTruth(pCut), If_CutTruth(pCut), If_CutLeaveNum(pCut) );
    RetValue = Kit_TruthIsop( If_CutTruth(pCut), If_CutLeaveNum(pCut), s_vMemory2, 0 );
    Kit_TruthNot( If_CutTruth(pCut), If_CutTruth(pCut), If_CutLeaveNum(pCut) );
    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
309 310 311 312 313 314 315
}

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


316 317
ABC_NAMESPACE_IMPL_END