abcRefactor.c 14.3 KB
Newer Older
Alan Mishchenko committed
1 2
/**CFile****************************************************************

Alan Mishchenko committed
3
  FileName    [abcRefactor.c]
Alan Mishchenko committed
4 5 6 7 8

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [Network and node package.]

Alan Mishchenko committed
9
  Synopsis    [Resynthesis based on collapsing and refactoring.]
Alan Mishchenko committed
10 11 12 13 14 15 16

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

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

Alan Mishchenko committed
17
  Revision    [$Id: abcRefactor.c,v 1.00 2005/06/20 00:00:00 alanmi Exp $]
Alan Mishchenko committed
18 19 20

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

21 22
#include "base/abc/abc.h"
#include "bool/dec/dec.h"
23
#include "bool/kit/kit.h"
Alan Mishchenko committed
24

25 26 27
ABC_NAMESPACE_IMPL_START


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

Alan Mishchenko committed
32 33 34 35
typedef struct Abc_ManRef_t_   Abc_ManRef_t;
struct Abc_ManRef_t_
{
    // user specified parameters
Alan Mishchenko committed
36 37 38 39
    int              nNodeSizeMax;      // the limit on the size of the supernode
    int              nConeSizeMax;      // the limit on the size of the containing cone
    int              fVerbose;          // the verbosity flag
    // internal data structures
40 41 42
    Vec_Ptr_t *      vVars;             // truth tables
    Vec_Ptr_t *      vFuncs;            // functions
    Vec_Int_t *      vMemory;           // memory
Alan Mishchenko committed
43 44 45 46 47 48 49 50 51
    Vec_Str_t *      vCube;             // temporary
    Vec_Int_t *      vForm;             // temporary
    Vec_Ptr_t *      vVisited;          // temporary
    Vec_Ptr_t *      vLeaves;           // temporary
    // node statistics
    int              nLastGain;
    int              nNodesConsidered;
    int              nNodesRefactored;
    int              nNodesGained;
Alan Mishchenko committed
52 53
    int              nNodesBeg;
    int              nNodesEnd;
Alan Mishchenko committed
54
    // runtime statistics
55
    abctime          timeCut;
56
    abctime          timeTru;
57 58 59 60 61 62 63
    abctime          timeDcs;
    abctime          timeSop;
    abctime          timeFact;
    abctime          timeEval;
    abctime          timeRes;
    abctime          timeNtk;
    abctime          timeTotal;
Alan Mishchenko committed
64 65 66
};

////////////////////////////////////////////////////////////////////////
Alan Mishchenko committed
67
///                     FUNCTION DEFINITIONS                         ///
Alan Mishchenko committed
68 69 70 71
////////////////////////////////////////////////////////////////////////

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

72
  Synopsis    [Returns function of the cone.]
Alan Mishchenko committed
73

74
  Description []
Alan Mishchenko committed
75 76 77 78 79 80
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
81
word * Abc_NodeConeTruth( Vec_Ptr_t * vVars, Vec_Ptr_t * vFuncs, int nWordsMax, Abc_Obj_t * pRoot, Vec_Ptr_t * vLeaves, Vec_Ptr_t * vVisited )
Alan Mishchenko committed
82 83
{
    Abc_Obj_t * pNode;
84 85 86 87 88 89 90 91 92 93 94 95
    word * pTruth0, * pTruth1, * pTruth = NULL;
    int i, k, nWords = Abc_Truth6WordNum( Vec_PtrSize(vLeaves) );
    // get nodes in the cut without fanins in the DFS order
    Abc_NodeConeCollect( &pRoot, 1, vLeaves, vVisited, 0 );
    // set elementary functions
    Vec_PtrForEachEntry( Abc_Obj_t *, vLeaves, pNode, i )
        pNode->pCopy = (Abc_Obj_t *)Vec_PtrEntry( vVars, i );
    // prepare functions
    for ( i = Vec_PtrSize(vFuncs); i < Vec_PtrSize(vVisited); i++ )
        Vec_PtrPush( vFuncs, ABC_ALLOC(word, nWordsMax) );
    // compute functions for the collected nodes
    Vec_PtrForEachEntry( Abc_Obj_t *, vVisited, pNode, i )
Alan Mishchenko committed
96
    {
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
        assert( !Abc_ObjIsPi(pNode) );
        pTruth0 = (word *)Abc_ObjFanin0(pNode)->pCopy;
        pTruth1 = (word *)Abc_ObjFanin1(pNode)->pCopy;
        pTruth  = (word *)Vec_PtrEntry( vFuncs, i );
        if ( Abc_ObjFaninC0(pNode) )
        {
            if ( Abc_ObjFaninC1(pNode) )
                for ( k = 0; k < nWords; k++ )
                    pTruth[k] = ~pTruth0[k] & ~pTruth1[k];
            else
                for ( k = 0; k < nWords; k++ )
                    pTruth[k] = ~pTruth0[k] &  pTruth1[k];
        }
        else
        {
            if ( Abc_ObjFaninC1(pNode) )
                for ( k = 0; k < nWords; k++ )
                    pTruth[k] =  pTruth0[k] & ~pTruth1[k];
            else
                for ( k = 0; k < nWords; k++ )
                    pTruth[k] =  pTruth0[k] &  pTruth1[k];
        }
        pNode->pCopy = (Abc_Obj_t *)pTruth;
Alan Mishchenko committed
120
    }
121 122 123 124 125 126 127 128
    return pTruth;
}
int Abc_NodeConeIsConst0( word * pTruth, int nVars )
{
    int k, nWords = Abc_Truth6WordNum( nVars );
    for ( k = 0; k < nWords; k++ )
        if ( pTruth[k] )
            return 0;
Alan Mishchenko committed
129 130
    return 1;
}
131 132 133 134 135 136 137 138 139
int Abc_NodeConeIsConst1( word * pTruth, int nVars )
{
    int k, nWords = Abc_Truth6WordNum( nVars );
    for ( k = 0; k < nWords; k++ )
        if ( ~pTruth[k] )
            return 0;
    return 1;
}

Alan Mishchenko committed
140 141 142 143 144 145 146 147 148 149 150 151

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

  Synopsis    [Resynthesizes the node using refactoring.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
152
Dec_Graph_t * Abc_NodeRefactor( Abc_ManRef_t * p, Abc_Obj_t * pNode, Vec_Ptr_t * vFanins, int nMinSaved, int fUpdateLevel, int fUseZeros, int fUseDcs, int fVerbose )
Alan Mishchenko committed
153
{
154
    extern int    Dec_GraphToNetworkCount( Abc_Obj_t * pRoot, Dec_Graph_t * pGraph, int NodeMax, int LevelMax );
Alan Mishchenko committed
155
    int fVeryVerbose = 0;
156 157
    int nVars = Vec_PtrSize(vFanins);
    int nWordsMax = Abc_Truth6WordNum(p->nNodeSizeMax);
Alan Mishchenko committed
158
    Dec_Graph_t * pFForm;
159 160
    Abc_Obj_t * pFanin;
    word * pTruth;
161
    abctime clk;
162
    int i, nNodesSaved, nNodesAdded, Required;
163 164
    if ( fUseZeros )
        nMinSaved = 0;
Alan Mishchenko committed
165 166

    p->nNodesConsidered++;
Alan Mishchenko committed
167

168
    Required = fUpdateLevel? Abc_ObjRequiredLevel(pNode) : ABC_INFINITY;
Alan Mishchenko committed
169

170
    // get the function of the cut
171
clk = Abc_Clock();
172 173
    pTruth = Abc_NodeConeTruth( p->vVars, p->vFuncs, nWordsMax, pNode, vFanins, p->vVisited );
p->timeTru += Abc_Clock() - clk;
174 175
    if ( pTruth == NULL )
        return NULL;
Alan Mishchenko committed
176 177

    // always accept the case of constant node
178
    if ( Abc_NodeConeIsConst0(pTruth, nVars) || Abc_NodeConeIsConst1(pTruth, nVars) )
Alan Mishchenko committed
179
    {
Alan Mishchenko committed
180 181
        p->nLastGain = Abc_NodeMffcSize( pNode );
        p->nNodesGained += p->nLastGain;
Alan Mishchenko committed
182
        p->nNodesRefactored++;
183
        return Abc_NodeConeIsConst0(pTruth, nVars) ? Dec_GraphCreateConst0() : Dec_GraphCreateConst1();
Alan Mishchenko committed
184 185 186
    }

    // get the factored form
187
clk = Abc_Clock();
188
    pFForm = (Dec_Graph_t *)Kit_TruthToGraph( (unsigned *)pTruth, nVars, p->vMemory );
189
p->timeFact += Abc_Clock() - clk;
Alan Mishchenko committed
190

Alan Mishchenko committed
191
    // mark the fanin boundary 
Alan Mishchenko committed
192
    // (can mark only essential fanins, belonging to bNodeFunc!)
193
    Vec_PtrForEachEntry( Abc_Obj_t *, vFanins, pFanin, i )
Alan Mishchenko committed
194 195 196
        pFanin->vFanouts.nSize++;
    // label MFFC with current traversal ID
    Abc_NtkIncrementTravId( pNode->pNtk );
Alan Mishchenko committed
197
    nNodesSaved = Abc_NodeMffcLabelAig( pNode );
Alan Mishchenko committed
198
    // unmark the fanin boundary and set the fanins as leaves in the form
199
    Vec_PtrForEachEntry( Abc_Obj_t *, vFanins, pFanin, i )
Alan Mishchenko committed
200
    {
Alan Mishchenko committed
201
        pFanin->vFanouts.nSize--;
Alan Mishchenko committed
202 203
        Dec_GraphNode(pFForm, i)->pFunc = pFanin;
    }
Alan Mishchenko committed
204 205

    // detect how many new nodes will be added (while taking into account reused nodes)
206
clk = Abc_Clock();
Alan Mishchenko committed
207
    nNodesAdded = Dec_GraphToNetworkCount( pNode, pFForm, nNodesSaved, Required );
208
p->timeEval += Abc_Clock() - clk;
Alan Mishchenko committed
209
    // quit if there is no improvement
210 211
    //if ( nNodesAdded == -1 || (nNodesAdded == nNodesSaved && !fUseZeros) )
    if ( nNodesAdded == -1 || nNodesSaved - nNodesAdded < nMinSaved )
Alan Mishchenko committed
212
    {
Alan Mishchenko committed
213
        Dec_GraphFree( pFForm );
Alan Mishchenko committed
214 215 216 217 218 219 220 221 222 223 224 225 226
        return NULL;
    }

    // compute the total gain in the number of nodes
    p->nLastGain = nNodesSaved - nNodesAdded;
    p->nNodesGained += p->nLastGain;
    p->nNodesRefactored++;

    // report the progress
    if ( fVeryVerbose )
    {
        printf( "Node %6s : ",  Abc_ObjName(pNode) );
        printf( "Cone = %2d. ", vFanins->nSize );
Alan Mishchenko committed
227
        printf( "FF = %2d. ",   1 + Dec_GraphNodeNum(pFForm) );
Alan Mishchenko committed
228 229 230 231 232
        printf( "MFFC = %2d. ", nNodesSaved );
        printf( "Add = %2d. ",  nNodesAdded );
        printf( "GAIN = %2d. ", p->nLastGain );
        printf( "\n" );
    }
Alan Mishchenko committed
233
    return pFForm;
Alan Mishchenko committed
234 235
}

Alan Mishchenko committed
236

Alan Mishchenko committed
237 238 239 240 241 242 243 244 245 246 247
/**Function*************************************************************

  Synopsis    [Starts the resynthesis manager.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
248
Abc_ManRef_t * Abc_NtkManRefStart( int nNodeSizeMax, int nConeSizeMax, int fUseDcs, int fVerbose )
Alan Mishchenko committed
249 250
{
    Abc_ManRef_t * p;
Alan Mishchenko committed
251
    p = ABC_ALLOC( Abc_ManRef_t, 1 );
Alan Mishchenko committed
252
    memset( p, 0, sizeof(Abc_ManRef_t) );
Alan Mishchenko committed
253 254
    p->vCube        = Vec_StrAlloc( 100 );
    p->vVisited     = Vec_PtrAlloc( 100 );
Alan Mishchenko committed
255 256 257
    p->nNodeSizeMax = nNodeSizeMax;
    p->nConeSizeMax = nConeSizeMax;
    p->fVerbose     = fVerbose;
258 259 260
    p->vVars        = Vec_PtrAllocTruthTables( Abc_MaxInt(nNodeSizeMax, 6) );
    p->vFuncs       = Vec_PtrAlloc( 100 );
    p->vMemory      = Vec_IntAlloc( 1 << 16 );
Alan Mishchenko committed
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
    return p;
}

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

  Synopsis    [Stops the resynthesis manager.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Abc_NtkManRefStop( Abc_ManRef_t * p )
{
277 278 279
    Vec_PtrFreeFree( p->vFuncs );
    Vec_PtrFree( p->vVars );
    Vec_IntFree( p->vMemory );
Alan Mishchenko committed
280
    Vec_PtrFree( p->vVisited );
281
    Vec_StrFree( p->vCube );
Alan Mishchenko committed
282
    ABC_FREE( p );
Alan Mishchenko committed
283 284
}

Alan Mishchenko committed
285 286 287 288 289 290 291 292 293 294 295 296 297 298
/**Function*************************************************************

  Synopsis    [Stops the resynthesis manager.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Abc_NtkManRefPrintStats( Abc_ManRef_t * p )
{
    printf( "Refactoring statistics:\n" );
Alan Mishchenko committed
299 300
    printf( "Nodes considered  = %8d.\n", p->nNodesConsidered );
    printf( "Nodes refactored  = %8d.\n", p->nNodesRefactored );
Alan Mishchenko committed
301
    printf( "Gain              = %8d. (%6.2f %%).\n", p->nNodesBeg-p->nNodesEnd, 100.0*(p->nNodesBeg-p->nNodesEnd)/p->nNodesBeg );
Alan Mishchenko committed
302 303
    ABC_PRT( "Cuts       ", p->timeCut );
    ABC_PRT( "Resynthesis", p->timeRes );
304
    ABC_PRT( "    BDD    ", p->timeTru );
Alan Mishchenko committed
305 306 307 308 309 310
    ABC_PRT( "    DCs    ", p->timeDcs );
    ABC_PRT( "    SOP    ", p->timeSop );
    ABC_PRT( "    FF     ", p->timeFact );
    ABC_PRT( "    Eval   ", p->timeEval );
    ABC_PRT( "AIG update ", p->timeNtk );
    ABC_PRT( "TOTAL      ", p->timeTotal );
Alan Mishchenko committed
311 312
}

313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
/**Function*************************************************************

  Synopsis    [Performs incremental resynthesis of the AIG.]

  Description [Starting from each node, computes a reconvergence-driven cut, 
  derives BDD of the cut function, constructs ISOP, factors the ISOP, 
  and replaces the current implementation of the MFFC of the node by the 
  new factored form, if the number of AIG nodes is reduced and the total
  number of levels of the AIG network is not increated. Returns the
  number of AIG nodes saved.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
329
int Abc_NtkRefactor( Abc_Ntk_t * pNtk, int nNodeSizeMax, int nMinSaved, int nConeSizeMax, int fUpdateLevel, int fUseZeros, int fUseDcs, int fVerbose )
330
{
331
    extern int           Dec_GraphUpdateNetwork( Abc_Obj_t * pRoot, Dec_Graph_t * pGraph, int fUpdateLevel, int nGain );
332 333 334 335 336 337 338
    ProgressBar * pProgress;
    Abc_ManRef_t * pManRef;
    Abc_ManCut_t * pManCut;
    Dec_Graph_t * pFForm;
    Vec_Ptr_t * vFanins;
    Abc_Obj_t * pNode;
    abctime clk, clkStart = Abc_Clock();
339
    int i, nNodes, RetValue = 1;
340 341 342 343 344 345 346 347 348 349 350

    assert( Abc_NtkIsStrash(pNtk) );
    // cleanup the AIG
    Abc_AigCleanup((Abc_Aig_t *)pNtk->pManFunc);
    // start the managers
    pManCut = Abc_NtkManCutStart( nNodeSizeMax, nConeSizeMax, 2, 1000 );
    pManRef = Abc_NtkManRefStart( nNodeSizeMax, nConeSizeMax, fUseDcs, fVerbose );
    pManRef->vLeaves   = Abc_NtkManCutReadCutLarge( pManCut );
    // compute the reverse levels if level update is requested
    if ( fUpdateLevel )
        Abc_NtkStartReverseLevels( pNtk, 0 );
351

352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
    // resynthesize each node once
    pManRef->nNodesBeg = Abc_NtkNodeNum(pNtk);
    nNodes = Abc_NtkObjNumMax(pNtk);
    pProgress = Extra_ProgressBarStart( stdout, nNodes );
    Abc_NtkForEachNode( pNtk, pNode, i )
    {
        Extra_ProgressBarUpdate( pProgress, i, NULL );
        // skip the constant node
//        if ( Abc_NodeIsConst(pNode) )
//            continue;
        // skip persistant nodes
        if ( Abc_NodeIsPersistant(pNode) )
            continue;
        // skip the nodes with many fanouts
        if ( Abc_ObjFanoutNum(pNode) > 1000 )
            continue;
        // stop if all nodes have been tried once
        if ( i >= nNodes )
            break;
        // compute a reconvergence-driven cut
clk = Abc_Clock();
        vFanins = Abc_NodeFindCut( pManCut, pNode, fUseDcs );
pManRef->timeCut += Abc_Clock() - clk;
        // evaluate this cut
clk = Abc_Clock();
377
        pFForm = Abc_NodeRefactor( pManRef, pNode, vFanins, nMinSaved, fUpdateLevel, fUseZeros, fUseDcs, fVerbose );
378 379 380 381 382
pManRef->timeRes += Abc_Clock() - clk;
        if ( pFForm == NULL )
            continue;
        // acceptable replacement found, update the graph
clk = Abc_Clock();
383 384 385 386 387 388
        if ( !Dec_GraphUpdateNetwork( pNode, pFForm, fUpdateLevel, pManRef->nLastGain ) )
        {
            Dec_GraphFree( pFForm );
            RetValue = -1;
            break;
        }
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
pManRef->timeNtk += Abc_Clock() - clk;
        Dec_GraphFree( pFForm );
    }
    Extra_ProgressBarStop( pProgress );
pManRef->timeTotal = Abc_Clock() - clkStart;
    pManRef->nNodesEnd = Abc_NtkNodeNum(pNtk);

    // print statistics of the manager
    if ( fVerbose )
        Abc_NtkManRefPrintStats( pManRef );
    // delete the managers
    Abc_NtkManCutStop( pManCut );
    Abc_NtkManRefStop( pManRef );
    // put the nodes into the DFS order and reassign their IDs
    Abc_NtkReassignIds( pNtk );
//    Abc_AigCheckFaninOrder( pNtk->pManFunc );
405
    if ( RetValue != -1 )
406
    {
407 408 409 410 411 412 413 414 415 416 417
        // fix the levels
        if ( fUpdateLevel )
            Abc_NtkStopReverseLevels( pNtk );
        else
            Abc_NtkLevel( pNtk );
        // check
        if ( !Abc_NtkCheck( pNtk ) )
        {
            printf( "Abc_NtkRefactor: The network check has failed.\n" );
            return 0;
        }
418
    }
419
    return RetValue;
420
}
421

Alan Mishchenko committed
422

Alan Mishchenko committed
423 424 425 426 427
////////////////////////////////////////////////////////////////////////
///                       END OF FILE                                ///
////////////////////////////////////////////////////////////////////////


428 429
ABC_NAMESPACE_IMPL_END