abcIf.c 43.7 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
/**CFile****************************************************************

  FileName    [abcIf.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [Network and node package.]

  Synopsis    [Interface with the FPGA mapping package.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

  Date        [Ver. 1.0. Started - November 21, 2006.]

  Revision    [$Id: abcIf.c,v 1.00 2006/11/21 00:00:00 alanmi Exp $]

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

21 22 23 24 25
#include "base/abc/abc.h"
#include "base/main/main.h"
#include "map/if/if.h"
#include "bool/kit/kit.h"
#include "aig/aig/aig.h"
Alan Mishchenko committed
26

27 28 29
ABC_NAMESPACE_IMPL_START


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

34
extern If_Man_t *  Abc_NtkToIf( Abc_Ntk_t * pNtk, If_Par_t * pPars );
Alan Mishchenko committed
35 36 37 38 39 40
static Abc_Ntk_t * Abc_NtkFromIf( If_Man_t * pIfMan, Abc_Ntk_t * pNtk );
extern Abc_Obj_t * Abc_NodeFromIf_rec( Abc_Ntk_t * pNtkNew, If_Man_t * pIfMan, If_Obj_t * pIfObj, Vec_Int_t * vCover );
static Hop_Obj_t * Abc_NodeIfToHop( Hop_Man_t * pHopMan, If_Man_t * pIfMan, If_Obj_t * pIfObj );
static Vec_Ptr_t * Abc_NtkFindGoodOrder( Abc_Ntk_t * pNtk );

extern void Abc_NtkBddReorder( Abc_Ntk_t * pNtk, int fVerbose );
Alan Mishchenko committed
41
extern void Abc_NtkBidecResyn( Abc_Ntk_t * pNtk, int fVerbose );
42 43

extern void Abc_NtkCollectPoDrivers( If_Man_t * p, Abc_Ntk_t * pNtk );
44
extern void Abc_NtkCreateChoiceDrivers( If_Man_t * p );
45
extern void Abc_NtkFreePoDrivers( If_Man_t * p, Abc_Ntk_t * pNtk );
46
extern void Abc_NtkRecreatePoDrivers( If_Man_t * p, Abc_Ntk_t * pNtkNew );
Alan Mishchenko committed
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
 
////////////////////////////////////////////////////////////////////////
///                     FUNCTION DEFINITIONS                         ///
////////////////////////////////////////////////////////////////////////

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

  Synopsis    [Interface with the FPGA mapping package.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
63 64 65 66 67 68 69 70 71 72
void Abc_NtkIfComputeSwitching( Abc_Ntk_t * pNtk, If_Man_t * pIfMan )
{
    extern Aig_Man_t * Abc_NtkToDar( Abc_Ntk_t * pNtk, int fExors, int fRegisters );
    extern Vec_Int_t * Saig_ManComputeSwitchProbs( Aig_Man_t * p, int nFrames, int nPref, int fProbOne );
    Vec_Int_t * vSwitching;
    float * pSwitching;
    Abc_Obj_t * pObjAbc;
    Aig_Obj_t * pObjAig;
    Aig_Man_t * pAig;
    If_Obj_t * pObjIf;
73 74
    int i;
    clock_t clk = clock();
Alan Mishchenko committed
75 76
    // map IF objects into old network
    Abc_NtkForEachObj( pNtk, pObjAbc, i )
77
        if ( (pObjIf = (If_Obj_t *)pObjAbc->pTemp) )
Alan Mishchenko committed
78 79 80 81 82 83
            pObjIf->pCopy = pObjAbc;
    // map network into an AIG
    pAig = Abc_NtkToDar( pNtk, 0, 0 );
    vSwitching = Saig_ManComputeSwitchProbs( pAig, 48, 16, 0 );
    pSwitching = (float *)vSwitching->pArray;
    Abc_NtkForEachObj( pNtk, pObjAbc, i )
84
        if ( (pObjAig = (Aig_Obj_t *)pObjAbc->pTemp) )
Alan Mishchenko committed
85 86
        {
            pObjAbc->dTemp = pSwitching[pObjAig->Id];
87
            // J. Anderson and F. N. Najm, Power-Aware Technology Mapping for LUT-Based FPGAs,
Alan Mishchenko committed
88 89 90 91 92 93 94 95 96 97
            // IEEE Intl. Conf. on Field-Programmable Technology, 2002.
//            pObjAbc->dTemp = (1.55 + 1.05 / (float) Abc_ObjFanoutNum(pObjAbc)) * pSwitching[pObjAig->Id];
        }
    Vec_IntFree( vSwitching );
    Aig_ManStop( pAig );
    // compute switching for the IF objects
    assert( pIfMan->vSwitching == NULL );
    pIfMan->vSwitching = Vec_IntStart( If_ManObjNum(pIfMan) );
    pSwitching = (float *)pIfMan->vSwitching->pArray;
    If_ManForEachObj( pIfMan, pObjIf, i )
98
        if ( (pObjAbc = (Abc_Obj_t *)pObjIf->pCopy) )
Alan Mishchenko committed
99 100 101
            pSwitching[i] = pObjAbc->dTemp;
if ( pIfMan->pPars->fVerbose )
{
Alan Mishchenko committed
102
    ABC_PRT( "Computing switching activity", clock() - clk );
Alan Mishchenko committed
103 104 105 106 107 108 109 110 111 112 113 114 115 116
}
}

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

  Synopsis    [Interface with the FPGA mapping package.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
Abc_Ntk_t * Abc_NtkIf( Abc_Ntk_t * pNtk, If_Par_t * pPars )
{
    Abc_Ntk_t * pNtkNew;
    If_Man_t * pIfMan;

    assert( Abc_NtkIsStrash(pNtk) );

    // get timing information
    pPars->pTimesArr = Abc_NtkGetCiArrivalFloats(pNtk);
    pPars->pTimesReq = NULL;

    // set the latch paths
    if ( pPars->fLatchPaths && pPars->pTimesArr )
    {
        int c;
        for ( c = 0; c < Abc_NtkPiNum(pNtk); c++ )
            pPars->pTimesArr[c] = -ABC_INFINITY;
    }

136
    // create FPGA mapper
Alan Mishchenko committed
137 138 139
    pIfMan = Abc_NtkToIf( pNtk, pPars );    
    if ( pIfMan == NULL )
        return NULL;
Alan Mishchenko committed
140 141
    if ( pPars->fPower )
        Abc_NtkIfComputeSwitching( pNtk, pIfMan );
142 143

    // perform FPGA mapping
144 145
    if ( pPars->fEnableRealPos )
        Abc_NtkCollectPoDrivers( pIfMan, pNtk );
146
    Abc_NtkCreateChoiceDrivers( pIfMan );
Alan Mishchenko committed
147 148
    if ( !If_ManPerformMapping( pIfMan ) )
    {
149
        Abc_NtkFreePoDrivers( pIfMan, pNtk );
Alan Mishchenko committed
150 151 152
        If_ManStop( pIfMan );
        return NULL;
    }
153
    Abc_NtkFreePoDrivers( pIfMan, pNtk );
Alan Mishchenko committed
154 155 156 157 158 159

    // transform the result of mapping into the new network
    pNtkNew = Abc_NtkFromIf( pIfMan, pNtk );
    if ( pNtkNew == NULL )
        return NULL;
    If_ManStop( pIfMan );
Alan Mishchenko committed
160
    if ( pPars->fBidec && pPars->nLutSize <= 8 )
Alan Mishchenko committed
161
        Abc_NtkBidecResyn( pNtkNew, 0 );
Alan Mishchenko committed
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200

    // duplicate EXDC
    if ( pNtk->pExdc )
        pNtkNew->pExdc = Abc_NtkDup( pNtk->pExdc );
    // make sure that everything is okay
    if ( !Abc_NtkCheck( pNtkNew ) )
    {
        printf( "Abc_NtkIf: The network check has failed.\n" );
        Abc_NtkDelete( pNtkNew );
        return NULL;
    }
    return pNtkNew;
}

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

  Synopsis    [Load the network into FPGA manager.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
If_Man_t * Abc_NtkToIf( Abc_Ntk_t * pNtk, If_Par_t * pPars )
{
    ProgressBar * pProgress;
    If_Man_t * pIfMan;
    Abc_Obj_t * pNode, * pFanin, * pPrev;
    Vec_Ptr_t * vNodes;
    int i;

    assert( Abc_NtkIsStrash(pNtk) );
//    vNodes = Abc_NtkFindGoodOrder( pNtk );
    vNodes = Abc_AigDfs( pNtk, 0, 0 );

    // start the mapping manager and set its parameters
    pIfMan = If_ManStart( pPars );
201
    pIfMan->pName = Abc_UtilStrsav( Abc_NtkName(pNtk) );
Alan Mishchenko committed
202 203 204

    // print warning about excessive memory usage
    if ( 1.0 * Abc_NtkObjNum(pNtk) * pIfMan->nObjBytes / (1<<30) > 1.0 )
205
        printf( "Warning: The mapper will allocate %.1f GB for to represent the subject graph with %d AIG nodes.\n", 
Alan Mishchenko committed
206 207 208
            1.0 * Abc_NtkObjNum(pNtk) * pIfMan->nObjBytes / (1<<30), Abc_NtkObjNum(pNtk) );

    // create PIs and remember them in the old nodes
Alan Mishchenko committed
209
    Abc_NtkCleanCopy( pNtk );
Alan Mishchenko committed
210 211 212 213
    Abc_AigConst1(pNtk)->pCopy = (Abc_Obj_t *)If_ManConst1( pIfMan );
    Abc_NtkForEachCi( pNtk, pNode, i )
    {
        pNode->pCopy = (Abc_Obj_t *)If_ManCreateCi( pIfMan );
214 215
        // transfer logic level information
        ((If_Obj_t *)pNode->pCopy)->Level = pNode->Level;
Alan Mishchenko committed
216 217 218 219 220
    }

    // load the AIG into the mapper
    pProgress = Extra_ProgressBarStart( stdout, Abc_NtkObjNumMax(pNtk) );
//    Abc_AigForEachAnd( pNtk, pNode, i )
221
    Vec_PtrForEachEntry( Abc_Obj_t *, vNodes, pNode, i )
Alan Mishchenko committed
222 223 224 225 226 227 228 229 230 231
    {
        Extra_ProgressBarUpdate( pProgress, i, "Initial" );
        // add the node to the mapper
        pNode->pCopy = (Abc_Obj_t *)If_ManCreateAnd( pIfMan, 
            If_NotCond( (If_Obj_t *)Abc_ObjFanin0(pNode)->pCopy, Abc_ObjFaninC0(pNode) ), 
            If_NotCond( (If_Obj_t *)Abc_ObjFanin1(pNode)->pCopy, Abc_ObjFaninC1(pNode) ) );
        // set up the choice node
        if ( Abc_AigNodeIsChoice( pNode ) )
        {
            pIfMan->nChoices++;
232
            for ( pPrev = pNode, pFanin = (Abc_Obj_t *)pNode->pData; pFanin; pPrev = pFanin, pFanin = (Abc_Obj_t *)pFanin->pData )
Alan Mishchenko committed
233 234 235 236 237 238 239 240 241 242
                If_ObjSetChoice( (If_Obj_t *)pPrev->pCopy, (If_Obj_t *)pFanin->pCopy );
            If_ManCreateChoice( pIfMan, (If_Obj_t *)pNode->pCopy );
        }
//printf( "AIG node %2d -> IF node %2d\n", pNode->Id, ((If_Obj_t *)pNode->pCopy)->Id );
    }
    Extra_ProgressBarStop( pProgress );
    Vec_PtrFree( vNodes );

    // set the primary outputs without copying the phase
    Abc_NtkForEachCo( pNtk, pNode, i )
Alan Mishchenko committed
243
        pNode->pCopy = (Abc_Obj_t *)If_ManCreateCo( pIfMan, If_NotCond( (If_Obj_t *)Abc_ObjFanin0(pNode)->pCopy, Abc_ObjFaninC0(pNode) ) );
Alan Mishchenko committed
244 245 246
    return pIfMan;
}

247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277

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

  Synopsis    [Box mapping procedures.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline void Abc_MapBoxSetPrevNext( Vec_Ptr_t * vDrivers, Vec_Int_t * vMapIn, Vec_Int_t * vMapOut, int Id )
{
    Abc_Obj_t * pNode;
    pNode = (Abc_Obj_t *)Vec_PtrEntry(vDrivers, Id+2);
    Vec_IntWriteEntry( vMapIn, Abc_ObjId(Abc_ObjFanin0(pNode)), Id );
    pNode = (Abc_Obj_t *)Vec_PtrEntry(vDrivers, Id+4);
    Vec_IntWriteEntry( vMapOut, Abc_ObjId(Abc_ObjFanin0(pNode)), Id );
}
static inline int Abc_MapBox2Next( Vec_Ptr_t * vDrivers, Vec_Int_t * vMapIn, Vec_Int_t * vMapOut, int Id )
{
    Abc_Obj_t * pNode = (Abc_Obj_t *)Vec_PtrEntry(vDrivers, Id+4);
    return Vec_IntEntry( vMapIn, Abc_ObjId(Abc_ObjFanin0(pNode)) );
}
static inline int Abc_MapBox2Prev( Vec_Ptr_t * vDrivers, Vec_Int_t * vMapIn, Vec_Int_t * vMapOut, int Id )
{
    Abc_Obj_t * pNode = (Abc_Obj_t *)Vec_PtrEntry(vDrivers, Id+2);
    return Vec_IntEntry( vMapOut, Abc_ObjId(Abc_ObjFanin0(pNode)) );
}

Alan Mishchenko committed
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
/**Function*************************************************************

  Synopsis    [Creates the mapped network.]

  Description [Assuming the copy field of the mapped nodes are NULL.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Abc_Ntk_t * Abc_NtkFromIf( If_Man_t * pIfMan, Abc_Ntk_t * pNtk )
{
    ProgressBar * pProgress;
    Abc_Ntk_t * pNtkNew;
293
    Abc_Obj_t * pNode, * pNodeNew;
Alan Mishchenko committed
294
    Vec_Int_t * vCover;
Alan Mishchenko committed
295
    int i, nDupGates;
Alan Mishchenko committed
296 297 298
    // create the new network
    if ( pIfMan->pPars->fUseBdds || pIfMan->pPars->fUseCnfs || pIfMan->pPars->fUseMv )
        pNtkNew = Abc_NtkStartFrom( pNtk, ABC_NTK_LOGIC, ABC_FUNC_BDD );
299
    else if ( pIfMan->pPars->fUseSops || pIfMan->pPars->nGateSize > 0 )
Alan Mishchenko committed
300 301 302 303 304 305 306 307 308 309
        pNtkNew = Abc_NtkStartFrom( pNtk, ABC_NTK_LOGIC, ABC_FUNC_SOP );
    else
        pNtkNew = Abc_NtkStartFrom( pNtk, ABC_NTK_LOGIC, ABC_FUNC_AIG );
    // prepare the mapping manager
    If_ManCleanNodeCopy( pIfMan );
    If_ManCleanCutData( pIfMan );
    // make the mapper point to the new network
    If_ObjSetCopy( If_ManConst1(pIfMan), Abc_NtkCreateNodeConst1(pNtkNew) );
    Abc_NtkForEachCi( pNtk, pNode, i )
        If_ObjSetCopy( If_ManCi(pIfMan, i), pNode->pCopy );
310

Alan Mishchenko committed
311 312 313
    // process the nodes in topological order
    vCover = Vec_IntAlloc( 1 << 16 );
    pProgress = Extra_ProgressBarStart( stdout, Abc_NtkCoNum(pNtk) );
314
    Abc_NtkForEachCo( pNtk, pNode, i )
Alan Mishchenko committed
315
    {
316 317 318 319
        Extra_ProgressBarUpdate( pProgress, i, "Final" );
        pNodeNew = Abc_NodeFromIf_rec( pNtkNew, pIfMan, If_ObjFanin0(If_ManCo(pIfMan, i)), vCover );
        pNodeNew = Abc_ObjNotCond( pNodeNew, If_ObjFaninC0(If_ManCo(pIfMan, i)) );
        Abc_ObjAddFanin( pNode->pCopy, pNodeNew );
Alan Mishchenko committed
320 321 322
    }
    Extra_ProgressBarStop( pProgress );
    Vec_IntFree( vCover );
323 324 325 326 327

    // update PO drivers
    if ( pIfMan->pPars->fEnableRealPos )
        Abc_NtkRecreatePoDrivers( pIfMan, pNtkNew );

Alan Mishchenko committed
328 329
    // remove the constant node if not used
    pNodeNew = (Abc_Obj_t *)If_ObjCopy( If_ManConst1(pIfMan) );
330
    if ( Abc_ObjFanoutNum(pNodeNew) == 0 && !Abc_ObjIsNone(pNodeNew) )
Alan Mishchenko committed
331 332 333 334 335 336 337
        Abc_NtkDeleteObj( pNodeNew );
    // minimize the node
    if ( pIfMan->pPars->fUseBdds || pIfMan->pPars->fUseCnfs || pIfMan->pPars->fUseMv )
        Abc_NtkSweep( pNtkNew, 0 );
    if ( pIfMan->pPars->fUseBdds )
        Abc_NtkBddReorder( pNtkNew, 0 );
    // decouple the PO driver nodes to reduce the number of levels
338
    nDupGates = Abc_NtkLogicMakeSimpleCos( pNtkNew, !pIfMan->pPars->fUseBuffs );
339
    if ( nDupGates && pIfMan->pPars->fVerbose && !Abc_FrameReadFlag("silentmode") )
340 341 342 343 344 345
    {
        if ( pIfMan->pPars->fUseBuffs )
            printf( "Added %d buffers/inverters to decouple the CO drivers.\n", nDupGates );
        else
            printf( "Duplicated %d gates to decouple the CO drivers.\n", nDupGates );
    }
Alan Mishchenko committed
346 347 348
    return pNtkNew;
}

349 350 351 352 353 354 355 356 357 358 359 360 361 362 363

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

  Synopsis    [Inserts the entry while sorting them by delay.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Hop_Obj_t * Abc_NodeTruthToHopInt( Hop_Man_t * pMan, Vec_Wrd_t * vAnds, int nVars )
{
    Vec_Ptr_t * vResults;
364
    Hop_Obj_t * pRes0, * pRes1, * pRes = NULL;
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
    If_And_t This;
    word Entry;
    int i;
    if ( Vec_WrdSize(vAnds) == 0 )
        return Hop_ManConst0(pMan);
    if ( Vec_WrdSize(vAnds) == 1 && Vec_WrdEntry(vAnds,0) == 0 )
        return Hop_ManConst1(pMan);
    vResults = Vec_PtrAlloc( Vec_WrdSize(vAnds) );
    for ( i = 0; i < nVars; i++ )
        Vec_PtrPush( vResults, Hop_IthVar(pMan, i) );
    Vec_WrdForEachEntryStart( vAnds, Entry, i, nVars )
    {
        This  = If_WrdToAnd( Entry );
        pRes0 = Hop_NotCond( (Hop_Obj_t *)Vec_PtrEntry(vResults, This.iFan0), This.fCompl0 ); 
        pRes1 = Hop_NotCond( (Hop_Obj_t *)Vec_PtrEntry(vResults, This.iFan1), This.fCompl1 ); 
        pRes  = Hop_And( pMan, pRes0, pRes1 );
        Vec_PtrPush( vResults, pRes );
/*
        printf( "fan0 = %c%d  fan1 = %c%d  Del = %d\n", 
            This.fCompl0? '-':'+', This.iFan0, 
            This.fCompl1? '-':'+', This.iFan1, 
            This.Delay );
*/
    }
    Vec_PtrFree( vResults );
    return Hop_NotCond( pRes, This.fCompl );
}

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

  Synopsis    [Creates the mapped network.]

  Description [Assuming the copy field of the mapped nodes are NULL.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Hop_Obj_t * Abc_NodeTruthToHop( Hop_Man_t * pMan, If_Man_t * p, If_Cut_t * pCut )
{
    Hop_Obj_t * pResult;
    Vec_Wrd_t * vArray;
    vArray  = If_CutDelaySopArray( p, pCut );
    pResult = Abc_NodeTruthToHopInt( pMan, vArray, If_CutLeaveNum(pCut) );
410
//    Vec_WrdFree( vArray );
411 412 413
    return pResult;
}

Alan Mishchenko committed
414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
/**Function*************************************************************

  Synopsis    [Derive one node after FPGA mapping.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Abc_Obj_t * Abc_NodeFromIf_rec( Abc_Ntk_t * pNtkNew, If_Man_t * pIfMan, If_Obj_t * pIfObj, Vec_Int_t * vCover )
{
    Abc_Obj_t * pNodeNew;
    If_Cut_t * pCutBest;
    If_Obj_t * pIfLeaf;
    int i;
    // return if the result if known
    pNodeNew = (Abc_Obj_t *)If_ObjCopy( pIfObj );
    if ( pNodeNew )
        return pNodeNew;
    assert( pIfObj->Type == IF_AND );
    // get the parameters of the best cut
    // create a new node 
    pNodeNew = Abc_NtkCreateNode( pNtkNew );
    pCutBest = If_ObjCutBest( pIfObj );
440
//    printf( "%d 0x%02X %d\n", pCutBest->nLeaves, 0xff & *If_CutTruth(pCutBest), pIfMan->pPars->pFuncCost(pCutBest) );
Alan Mishchenko committed
441
//    if ( pIfMan->pPars->pLutLib && pIfMan->pPars->pLutLib->fVarPinDelays )
442
    if ( !pIfMan->pPars->fDelayOpt && !pIfMan->pPars->pLutStruct && !pIfMan->pPars->fUserRecLib && !pIfMan->pPars->nGateSize )
443
        If_CutRotatePins( pIfMan, pCutBest );
Alan Mishchenko committed
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461
    if ( pIfMan->pPars->fUseCnfs || pIfMan->pPars->fUseMv )
    {
        If_CutForEachLeafReverse( pIfMan, pCutBest, pIfLeaf, i )
            Abc_ObjAddFanin( pNodeNew, Abc_NodeFromIf_rec(pNtkNew, pIfMan, pIfLeaf, vCover) );
    }
    else
    {
        If_CutForEachLeaf( pIfMan, pCutBest, pIfLeaf, i )
            Abc_ObjAddFanin( pNodeNew, Abc_NodeFromIf_rec(pNtkNew, pIfMan, pIfLeaf, vCover) );
    }
    // set the level of the new node
    pNodeNew->Level = Abc_ObjLevelNew( pNodeNew );
    // derive the function of this node
    if ( pIfMan->pPars->fTruth )
    {
        if ( pIfMan->pPars->fUseBdds )
        { 
            // transform truth table into the BDD 
462
            pNodeNew->pData = Kit_TruthToBdd( (DdManager *)pNtkNew->pManFunc, If_CutTruth(pCutBest), If_CutLeaveNum(pCutBest), 0 );  Cudd_Ref((DdNode *)pNodeNew->pData); 
Alan Mishchenko committed
463 464 465 466
        }
        else if ( pIfMan->pPars->fUseCnfs || pIfMan->pPars->fUseMv )
        { 
            // transform truth table into the BDD 
467
            pNodeNew->pData = Kit_TruthToBdd( (DdManager *)pNtkNew->pManFunc, If_CutTruth(pCutBest), If_CutLeaveNum(pCutBest), 1 );  Cudd_Ref((DdNode *)pNodeNew->pData); 
Alan Mishchenko committed
468
        }
469
        else if ( pIfMan->pPars->fUseSops || pIfMan->pPars->nGateSize > 0 ) 
Alan Mishchenko committed
470 471 472 473 474 475 476 477
        {
            // transform truth table into the SOP
            int RetValue = Kit_TruthIsop( If_CutTruth(pCutBest), If_CutLeaveNum(pCutBest), vCover, 1 );
            assert( RetValue == 0 || RetValue == 1 );
            // check the case of constant cover
            if ( Vec_IntSize(vCover) == 0 || (Vec_IntSize(vCover) == 1 && Vec_IntEntry(vCover,0) == 0) )
            {
                assert( RetValue == 0 );
478
                pNodeNew->pData = Abc_SopCreateAnd( (Mem_Flex_t *)pNtkNew->pManFunc, If_CutLeaveNum(pCutBest), NULL );
Alan Mishchenko committed
479 480 481 482 483
                pNodeNew = (Vec_IntSize(vCover) == 0) ? Abc_NtkCreateNodeConst0(pNtkNew) : Abc_NtkCreateNodeConst1(pNtkNew);
            }
            else
            {
                // derive the AIG for that tree
484
                pNodeNew->pData = Abc_SopCreateFromIsop( (Mem_Flex_t *)pNtkNew->pManFunc, If_CutLeaveNum(pCutBest), vCover );
Alan Mishchenko committed
485
                if ( RetValue )
486
                    Abc_SopComplement( (char *)pNodeNew->pData );
Alan Mishchenko committed
487 488
            }
        }
489 490 491 492 493
        else if ( pIfMan->pPars->fDelayOpt )
        {
            extern Hop_Obj_t * Abc_NodeTruthToHop( Hop_Man_t * pMan, If_Man_t * pIfMan, If_Cut_t * pCut );
            pNodeNew->pData = Abc_NodeTruthToHop( (Hop_Man_t *)pNtkNew->pManFunc, pIfMan, pCutBest );
        }
494 495
        else if ( pIfMan->pPars->fUserRecLib )
        {
496
            extern Hop_Obj_t * Abc_RecToHop( Hop_Man_t * pMan, If_Man_t * pIfMan, If_Cut_t * pCut, If_Obj_t * pIfObj );
497 498 499 500 501 502
            extern Hop_Obj_t * Abc_RecToHop2( Hop_Man_t * pMan, If_Man_t * pIfMan, If_Cut_t * pCut, If_Obj_t * pIfObj );
            if(Abc_NtkRecIsRunning())
                pNodeNew->pData = Abc_RecToHop( (Hop_Man_t *)pNtkNew->pManFunc, pIfMan, pCutBest, pIfObj); 
            else
                pNodeNew->pData = Abc_RecToHop2( (Hop_Man_t *)pNtkNew->pManFunc, pIfMan, pCutBest, pIfObj); 
            
503
        }
504
        else
Alan Mishchenko committed
505 506
        {
            extern Hop_Obj_t * Kit_TruthToHop( Hop_Man_t * pMan, unsigned * pTruth, int nVars, Vec_Int_t * vMemory );
507
            pNodeNew->pData = Kit_TruthToHop( (Hop_Man_t *)pNtkNew->pManFunc, If_CutTruth(pCutBest), If_CutLeaveNum(pCutBest), vCover );
Alan Mishchenko committed
508 509 510 511 512 513
        }
        // complement the node if the cut was complemented
        if ( pCutBest->fCompl )
            Abc_NodeComplement( pNodeNew );
    }
    else
Alan Mishchenko committed
514
    {
515
        pNodeNew->pData = Abc_NodeIfToHop( (Hop_Man_t *)pNtkNew->pManFunc, pIfMan, pIfObj );
Alan Mishchenko committed
516
    }
Alan Mishchenko committed
517
    If_ObjSetCopy( pIfObj, pNodeNew );
518 519 520 521 522 523 524 525 526
/*
printf( "%3d : Delay = %d  Cutsize = %d\n", pNodeNew->Id, (int)pCutBest->Delay, pCutBest->nLeaves );
{
    Abc_Obj_t * pFanin;
    int i;
Abc_ObjForEachFanin( pNodeNew, pFanin, i )
printf( "fanin%d = %2d\n", i, pFanin->Id );
}
*/
Alan Mishchenko committed
527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548
    return pNodeNew;
}

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

  Synopsis    [Recursively derives the truth table for the cut.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Hop_Obj_t * Abc_NodeIfToHop_rec( Hop_Man_t * pHopMan, If_Man_t * pIfMan, If_Obj_t * pIfObj, Vec_Ptr_t * vVisited )
{
    If_Cut_t * pCut;
    Hop_Obj_t * gFunc, * gFunc0, * gFunc1;
    // get the best cut
    pCut = If_ObjCutBest(pIfObj);
    // if the cut is visited, return the result
    if ( If_CutData(pCut) )
549
        return (Hop_Obj_t *)If_CutData(pCut);
Alan Mishchenko committed
550 551 552 553 554 555 556 557 558 559 560 561
    // compute the functions of the children
    gFunc0 = Abc_NodeIfToHop_rec( pHopMan, pIfMan, pIfObj->pFanin0, vVisited );
    gFunc1 = Abc_NodeIfToHop_rec( pHopMan, pIfMan, pIfObj->pFanin1, vVisited );
    // get the function of the cut
    gFunc  = Hop_And( pHopMan, Hop_NotCond(gFunc0, pIfObj->fCompl0), Hop_NotCond(gFunc1, pIfObj->fCompl1) );  
    assert( If_CutData(pCut) == NULL );
    If_CutSetData( pCut, gFunc );
    // add this cut to the visited list
    Vec_PtrPush( vVisited, pCut );
    return gFunc;
}

Alan Mishchenko committed
562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582

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

  Synopsis    [Recursively derives the truth table for the cut.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Hop_Obj_t * Abc_NodeIfToHop2_rec( Hop_Man_t * pHopMan, If_Man_t * pIfMan, If_Obj_t * pIfObj, Vec_Ptr_t * vVisited )
{
    If_Cut_t * pCut;
    If_Obj_t * pTemp;
    Hop_Obj_t * gFunc, * gFunc0, * gFunc1;
    // get the best cut
    pCut = If_ObjCutBest(pIfObj);
    // if the cut is visited, return the result
    if ( If_CutData(pCut) )
583
        return (Hop_Obj_t *)If_CutData(pCut);
Alan Mishchenko committed
584 585 586 587 588 589
    // mark the node as visited
    Vec_PtrPush( vVisited, pCut );
    // insert the worst case
    If_CutSetData( pCut, (void *)1 );
    // skip in case of primary input
    if ( If_ObjIsCi(pIfObj) )
590
        return (Hop_Obj_t *)If_CutData(pCut);
Alan Mishchenko committed
591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606
    // compute the functions of the children
    for ( pTemp = pIfObj; pTemp; pTemp = pTemp->pEquiv )
    {
        gFunc0 = Abc_NodeIfToHop2_rec( pHopMan, pIfMan, pTemp->pFanin0, vVisited );
        if ( gFunc0 == (void *)1 )
            continue;
        gFunc1 = Abc_NodeIfToHop2_rec( pHopMan, pIfMan, pTemp->pFanin1, vVisited );
        if ( gFunc1 == (void *)1 )
            continue;
        // both branches are solved
        gFunc = Hop_And( pHopMan, Hop_NotCond(gFunc0, pTemp->fCompl0), Hop_NotCond(gFunc1, pTemp->fCompl1) ); 
        if ( pTemp->fPhase != pIfObj->fPhase )
            gFunc = Hop_Not(gFunc);
        If_CutSetData( pCut, gFunc );
        break;
    }
607
    return (Hop_Obj_t *)If_CutData(pCut);
Alan Mishchenko committed
608 609
}

Alan Mishchenko committed
610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634
/**Function*************************************************************

  Synopsis    [Derives the truth table for one cut.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Hop_Obj_t * Abc_NodeIfToHop( Hop_Man_t * pHopMan, If_Man_t * pIfMan, If_Obj_t * pIfObj )
{
    If_Cut_t * pCut;
    Hop_Obj_t * gFunc;
    If_Obj_t * pLeaf;
    int i;
    // get the best cut
    pCut = If_ObjCutBest(pIfObj);
    assert( pCut->nLeaves > 1 );
    // set the leaf variables
    If_CutForEachLeaf( pIfMan, pCut, pLeaf, i )
        If_CutSetData( If_ObjCutBest(pLeaf), Hop_IthVar(pHopMan, i) );
    // recursively compute the function while collecting visited cuts
    Vec_PtrClear( pIfMan->vTemp );
Alan Mishchenko committed
635 636 637 638 639 640
    gFunc = Abc_NodeIfToHop2_rec( pHopMan, pIfMan, pIfObj, pIfMan->vTemp ); 
    if ( gFunc == (void *)1 )
    {
        printf( "Abc_NodeIfToHop(): Computing local AIG has failed.\n" );
        return NULL;
    }
Alan Mishchenko committed
641 642 643 644
//    printf( "%d ", Vec_PtrSize(p->vTemp) );
    // clean the cuts
    If_CutForEachLeaf( pIfMan, pCut, pLeaf, i )
        If_CutSetData( If_ObjCutBest(pLeaf), NULL );
645
    Vec_PtrForEachEntry( If_Cut_t *, pIfMan->vTemp, pCut, i )
Alan Mishchenko committed
646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663
        If_CutSetData( pCut, NULL );
    return gFunc;
}


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

  Synopsis    [Comparison for two nodes with the flow.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Abc_ObjCompareFlow( Abc_Obj_t ** ppNode0, Abc_Obj_t ** ppNode1 )
{
Alan Mishchenko committed
664 665
    float Flow0 = Abc_Int2Float((int)(ABC_PTRINT_T)(*ppNode0)->pCopy);
    float Flow1 = Abc_Int2Float((int)(ABC_PTRINT_T)(*ppNode1)->pCopy);
Alan Mishchenko committed
666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727
    if ( Flow0 > Flow1 )
        return -1;
    if ( Flow0 < Flow1 )
        return 1;
    return 0;
}

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

  Synopsis    [Orders AIG nodes so that nodes from larger cones go first.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Abc_NtkFindGoodOrder_rec( Abc_Obj_t * pNode, Vec_Ptr_t * vNodes )
{
    if ( !Abc_ObjIsNode(pNode) )
        return;
    assert( Abc_ObjIsNode( pNode ) );
    // if this node is already visited, skip
    if ( Abc_NodeIsTravIdCurrent( pNode ) )
        return;
    // mark the node as visited
    Abc_NodeSetTravIdCurrent( pNode );
    // visit the transitive fanin of the node
    Abc_NtkFindGoodOrder_rec( Abc_ObjFanin0(pNode), vNodes );
    Abc_NtkFindGoodOrder_rec( Abc_ObjFanin1(pNode), vNodes );
    // add the node after the fanins have been added
    Vec_PtrPush( vNodes, pNode );
}

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

  Synopsis    [Orders AIG nodes so that nodes from larger cones go first.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Vec_Ptr_t * Abc_NtkFindGoodOrder( Abc_Ntk_t * pNtk )
{
    Vec_Ptr_t * vNodes, * vCos;
    Abc_Obj_t * pNode, * pFanin0, * pFanin1;
    float Flow0, Flow1;
    int i;

    // initialize the flow
    Abc_AigConst1(pNtk)->pCopy = NULL;
    Abc_NtkForEachCi( pNtk, pNode, i )
        pNode->pCopy = NULL;
    // compute the flow
    Abc_AigForEachAnd( pNtk, pNode, i )
    {
        pFanin0 = Abc_ObjFanin0(pNode);
        pFanin1 = Abc_ObjFanin1(pNode);
Alan Mishchenko committed
728 729 730
        Flow0 = Abc_Int2Float((int)(ABC_PTRINT_T)pFanin0->pCopy)/Abc_ObjFanoutNum(pFanin0);
        Flow1 = Abc_Int2Float((int)(ABC_PTRINT_T)pFanin1->pCopy)/Abc_ObjFanoutNum(pFanin1);
        pNode->pCopy = (Abc_Obj_t *)(ABC_PTRINT_T)Abc_Float2Int(Flow0 + Flow1+(float)1.0);
Alan Mishchenko committed
731 732 733 734 735 736 737 738 739 740 741 742 743 744
    }
    // find the flow of the COs
    vCos = Vec_PtrAlloc( Abc_NtkCoNum(pNtk) );
    Abc_NtkForEachCo( pNtk, pNode, i )
    {
        pNode->pCopy = Abc_ObjFanin0(pNode)->pCopy;
//        pNode->pCopy = (Abc_Obj_t *)Abc_Float2Int((float)Abc_ObjFanin0(pNode)->Level);
        Vec_PtrPush( vCos, pNode );
    }

    // sort nodes in the increasing order of the flow
    qsort( (Abc_Obj_t **)Vec_PtrArray(vCos), Abc_NtkCoNum(pNtk), 
        sizeof(Abc_Obj_t *), (int (*)(const void *, const void *))Abc_ObjCompareFlow );
    // verify sorting
745 746
    pFanin0 = (Abc_Obj_t *)Vec_PtrEntry(vCos, 0);
    pFanin1 = (Abc_Obj_t *)Vec_PtrEntryLast(vCos);
Alan Mishchenko committed
747
    assert( Abc_Int2Float((int)(ABC_PTRINT_T)pFanin0->pCopy) >= Abc_Int2Float((int)(ABC_PTRINT_T)pFanin1->pCopy) );
Alan Mishchenko committed
748 749 750 751

    // collect the nodes in the topological order from the new array
    Abc_NtkIncrementTravId( pNtk );
    vNodes = Vec_PtrAlloc( 100 );
752
    Vec_PtrForEachEntry( Abc_Obj_t *, vCos, pNode, i )
Alan Mishchenko committed
753 754 755 756 757 758 759 760
    {
        Abc_NtkFindGoodOrder_rec( Abc_ObjFanin0(pNode), vNodes );
//        printf( "%.2f ", Abc_Int2Float((int)pNode->pCopy) );
    }
    Vec_PtrFree( vCos );
    return vNodes;
}

Alan Mishchenko committed
761

762 763 764 765 766 767 768 769 770 771 772
/**Function*************************************************************

  Synopsis    [Sets PO drivers.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792
void Abc_NtkMarkMux( Abc_Obj_t * pDriver, Abc_Obj_t ** ppNode1, Abc_Obj_t ** ppNode2 )
{
    Abc_Obj_t * pNodeC, * pNodeT, * pNodeE;
    If_Obj_t * pIfObj;

    *ppNode1 = NULL;
    *ppNode2 = NULL;
    if ( pDriver == NULL )
        return;
    if ( !Abc_NodeIsMuxType(pDriver) )
        return;

    pNodeC = Abc_NodeRecognizeMux( pDriver, &pNodeT, &pNodeE );

    pIfObj = If_Regular( (If_Obj_t *)Abc_ObjFanin0(pDriver)->pCopy );
    if ( If_ObjIsAnd(pIfObj) )
        pIfObj->fSkipCut = 1;
    pIfObj = If_Regular( (If_Obj_t *)Abc_ObjFanin1(pDriver)->pCopy );
    if ( If_ObjIsAnd(pIfObj) )
        pIfObj->fSkipCut = 1;
793

794 795 796
    pIfObj = If_Regular( (If_Obj_t *)Abc_ObjRegular(pNodeC)->pCopy );
    if ( If_ObjIsAnd(pIfObj) )
        pIfObj->fSkipCut = 1;
797 798

/*
799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820
    pIfObj = If_Regular( (If_Obj_t *)Abc_ObjRegular(pNodeT)->pCopy );
    if ( If_ObjIsAnd(pIfObj) )
        pIfObj->fSkipCut = 1;
    pIfObj = If_Regular( (If_Obj_t *)Abc_ObjRegular(pNodeE)->pCopy );
    if ( If_ObjIsAnd(pIfObj) )
        pIfObj->fSkipCut = 1;
*/
    *ppNode1 = Abc_ObjRegular(pNodeC);
    *ppNode2 = Abc_ObjRegular(pNodeT);
}

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

  Synopsis    [Sets PO drivers.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
821 822 823
void Abc_NtkCollectPoDrivers( If_Man_t * p, Abc_Ntk_t * pNtk )
{
    Vec_Int_t * vTemp;
824
    Abc_Obj_t * pObj, * pDriver;
825 826 827 828
    If_Obj_t * pIfObj;
    int i, g, nGroups;
    if ( pNtk->nRealPos == 0 )
    {
829 830
        if ( !Abc_FrameReadFlag("silentmode") )
            printf( "PO drivers are not defined.\n" );
831 832 833 834
        return;
    }
    if ( (Abc_NtkPoNum(pNtk) - pNtk->nRealPos) % 5 != 0 )
    {
835 836
        if ( !Abc_FrameReadFlag("silentmode") )
            printf( "PO drivers are not divisible by 5.\n" );
837 838 839
        return;
    }
    nGroups = (Abc_NtkPoNum(pNtk) - pNtk->nRealPos) / 5;
840 841
    if ( !Abc_FrameReadFlag("silentmode") )
        printf( "Processing %d groups of PO drivers.\n", nGroups );
842
    // mark the drivers (0 a   1 b   2 c   3 s   4 c)
843 844 845 846 847 848 849 850 851 852 853 854
    assert( p->pDriverCuts == NULL );
    p->pDriverCuts = ABC_CALLOC( Vec_Int_t *, If_ManObjNum(p) );
    for ( g = 0; g < nGroups; g++ )
    {
        // collect inputs
        vTemp = Vec_IntAlloc( 3 );
        for ( i = 0; i < 3; i++ )
        {
            pObj   = Abc_NtkPo( pNtk, pNtk->nRealPos + g * 5 + i );
            pIfObj = If_Regular( ((If_Obj_t *)pObj->pCopy)->pFanin0 );
            Vec_IntPush( vTemp, pIfObj->Id );
        }
855
//        Vec_IntSort( vTemp, 0 );
856 857 858 859 860 861 862 863 864 865 866 867
        // find output node
        pObj   = Abc_NtkPo( pNtk, pNtk->nRealPos + g * 5 + 3 );
        pIfObj = If_Regular( ((If_Obj_t *)pObj->pCopy)->pFanin0 );
        if ( !If_ObjIsConst1(pIfObj) && p->pDriverCuts[pIfObj->Id] == NULL )
            p->pDriverCuts[pIfObj->Id] = Vec_IntDup( vTemp );
        // find output node
        pObj   = Abc_NtkPo( pNtk, pNtk->nRealPos + g * 5 + 4 );
        pIfObj = If_Regular( ((If_Obj_t *)pObj->pCopy)->pFanin0 );
        if ( !If_ObjIsConst1(pIfObj) && p->pDriverCuts[pIfObj->Id] == NULL )
        {
            p->pDriverCuts[pIfObj->Id] = Vec_IntDup( vTemp );
            pIfObj->fDriver = 1;
868
//            printf( "%d ", pIfObj->Id );
869 870 871
        }
        Vec_IntFree( vTemp );
    }
872
//    printf( "\n" );
873 874 875 876 877 878 879 880 881 882 883
    return;

    // highlight inner logic
    for ( i = pNtk->nRealPos; i <  Abc_NtkPoNum(pNtk); i += 5 )
    {
        Abc_Obj_t * pNode1, * pNode2;

        pObj = Abc_NtkPo( pNtk, i + 4 );
        pDriver = Abc_ObjFanin0( pObj );
        Abc_NtkMarkMux( pDriver, &pNode1, &pNode2 );

884
/*
885 886 887 888 889 890 891 892 893
        pObj = Abc_NtkPo( pNtk, i + 3 );
        pDriver = Abc_ObjFanin0( pObj );
        Abc_NtkMarkMux( pDriver, &pNode1, &pNode2 );
        if ( pNode1 == NULL )
            continue;

        assert( Abc_ObjRegular(pNode1) != Abc_ObjRegular(pNode2) );
//        Abc_NtkMarkMux( pNode1, &pNode1, &pNode2 );
//        Abc_NtkMarkMux( pNode2, &pNode1, &pNode2 );
894
*/
895 896
    }

897
/*
898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939
    {
        Vec_Int_t * vInfo;
        int i, k, numPo;

        Vec_VecForEachLevelInt( pNtk->vRealPos, vInfo, i )
        {
            numPo = Vec_IntEntry( vInfo, 0 );
            pObj = Abc_NtkPo( pNtk, numPo+2 );
            pDriver = Abc_ObjFanin0( pObj );
            pIfObj = If_Regular( (If_Obj_t *)pDriver->pCopy );
            pIfObj->fSkipCut = 0;

            numPo = Vec_IntEntryLast( vInfo );
            pObj = Abc_NtkPo( pNtk, numPo+4 );
            pDriver = Abc_ObjFanin0( pObj );
            pIfObj = If_Regular( (If_Obj_t *)pDriver->pCopy );
            pIfObj->fSkipCut = 0;

            Vec_IntForEachEntry( vInfo, numPo, k )
            {
                pObj = Abc_NtkPo( pNtk, numPo+0 );
                pDriver = Abc_ObjFanin0( pObj );
                pIfObj = If_Regular( (If_Obj_t *)pDriver->pCopy );
                pIfObj->fSkipCut = 0;

                pObj = Abc_NtkPo( pNtk, numPo+1 );
                pDriver = Abc_ObjFanin0( pObj );
                pIfObj = If_Regular( (If_Obj_t *)pDriver->pCopy );
                pIfObj->fSkipCut = 0;

                pObj = Abc_NtkPo( pNtk, numPo+2 );
                pDriver = Abc_ObjFanin0( pObj );
                pIfObj = If_Regular( (If_Obj_t *)pDriver->pCopy );
                pIfObj->fSkipCut = 0;

                pObj = Abc_NtkPo( pNtk, numPo+3 );
                pDriver = Abc_ObjFanin0( pObj );
                pIfObj = If_Regular( (If_Obj_t *)pDriver->pCopy );
                pIfObj->fSkipCut = 0;
            }
        }
    }
940
*/
941 942
}

943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988

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

  Synopsis    [Frees PO drivers.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Abc_NtkCreateChoiceDrivers( If_Man_t * p )
{
    Vec_Int_t * vVec;
    If_Obj_t * pObj, * pTemp;
    int i, Counter = 0;
    if ( p->pDriverCuts == NULL )
        return;
    If_ManForEachNode( p, pObj, i )
    {
        // skip non-choice nodes
        if ( pObj->pEquiv == NULL || pObj->nRefs == 0 )
            continue;
        // find driver cut
        vVec = NULL;
        for ( pTemp = pObj; pTemp; pTemp = pTemp->pEquiv )
            if ( p->pDriverCuts[pTemp->Id] != NULL )
            {
                vVec = Vec_IntDup( p->pDriverCuts[pTemp->Id] );
                break;
            }
        if ( vVec == NULL )
            continue;
        // transfer driver cut to the root node
        for ( pTemp = pObj; pTemp; pTemp = pTemp->pEquiv )
        {
            Vec_IntFreeP( &p->pDriverCuts[pTemp->Id] );
            p->pDriverCuts[pTemp->Id] = Vec_IntDup( vVec );
        }
        Vec_IntFree( vVec );
        Counter++;
    }
//    printf( "Choice driver cut updates = %d.\n", Counter );
}

989 990 991 992 993 994 995 996 997 998 999
/**Function*************************************************************

  Synopsis    [Frees PO drivers.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
1000
void Abc_NtkFreePoDrivers( If_Man_t * p, Abc_Ntk_t * pNtk )
1001
{
1002 1003
    If_Obj_t * pObj;
    If_Cut_t * pCut;
1004 1005 1006
    int i;
    if ( p->pDriverCuts == NULL )
        return;
1007 1008 1009
    pNtk->nRealDelay = p->RequiredGlo;
    if ( !Abc_FrameReadFlag("silentmode") )
        printf( "Actual delay after mapping = %.2f\n", p->RequiredGlo );
1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021
    assert( Abc_NtkPoNum(pNtk) == If_ManCoNum(p) - Abc_NtkLatchNum(pNtk) );
    // print the cut sizes of the drivers
    for ( i = pNtk->nRealPos; i <  Abc_NtkPoNum(pNtk); i += 5 )
    {
        pObj = If_ManCo( p, i + 4 );
        pObj = If_Regular(pObj->pFanin0);
        if ( !pObj->fDriver )
            continue;
        pCut = If_ObjCutBest(pObj);
//        printf( "%d(%d) ", pObj->Id, pCut->nLeaves );
    }
//    printf( "\n" );
1022 1023 1024 1025
    for ( i = 0; i < If_ManObjNum(p); i++ )
        Vec_IntFreeP( &p->pDriverCuts[i] );
    ABC_FREE( p->pDriverCuts );
}
Alan Mishchenko committed
1026

1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081

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

  Synopsis    [Returns 1 if pOld is in the TFI of pNew.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Abc_NtkIfCheckTfi_rec( Abc_Obj_t * pNode, Abc_Obj_t * pOld )
{
    Abc_Obj_t * pFanin;
    int k;
    if ( pNode == NULL )
        return 0;
    if ( pNode == pOld )
        return 1;
    // check the trivial cases
    if ( Abc_ObjIsCi(pNode) )
        return 0; 
    assert( Abc_ObjIsNode(pNode) );
    // if this node is already visited, skip
    if ( Abc_NodeIsTravIdCurrent( pNode ) )
        return 0;
    // mark the node as visited
    Abc_NodeSetTravIdCurrent( pNode );
    // check the children
    Abc_ObjForEachFanin( pNode, pFanin, k )
        if ( Abc_NtkIfCheckTfi_rec( pFanin, pOld ) )
            return 1;
    return 0;
}

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

  Synopsis    [Returns 1 if pOld is in the TFI of pNew.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Abc_NtkIfCheckTfi( Abc_Ntk_t * pNtk, Abc_Obj_t * pOld, Abc_Obj_t * pNew )
{
    assert( !Abc_ObjIsComplement(pOld) );
    assert( !Abc_ObjIsComplement(pNew) );
    Abc_NtkIncrementTravId(pNtk);
    return Abc_NtkIfCheckTfi_rec( pNew, pOld );
}

1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Abc_NtkIfCheckRealNodes( Abc_Ntk_t * pNtk )
{
    Abc_Obj_t * pObj;
    int i, iNode;
    for ( i = pNtk->nRealPos; i < Abc_NtkPoNum(pNtk); i += 5 )
    {
        iNode = Vec_IntEntry( pNtk->vRealNodes, i+3 - pNtk->nRealPos );
        pObj = Abc_NtkObj( pNtk, iNode );
        assert( Abc_ObjFaninNum(pObj) == 2 );

        iNode = Vec_IntEntry( pNtk->vRealNodes, i+4 - pNtk->nRealPos );
        pObj = Abc_NtkObj( pNtk, iNode );
        assert( Abc_ObjFaninNum(pObj) == 3 );
    }
}

1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126
/**Function*************************************************************

  Synopsis    [Restores the structure.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Abc_NtkRecreatePoDrivers( If_Man_t * p, Abc_Ntk_t * pNtkNew )
{
    Abc_Obj_t * pNode, * pExor, * pObj, * pFanin, * pFaninNew;
    Vec_Ptr_t * vDrivers, * vDriversNew, * vFanins;
    Vec_Int_t * vInfo, * vNodeMap, * vDriverInvs;
    int i, k, numPo, nRealLuts, fCompl;
1127
    float RealLutArea;
1128 1129
    if ( pNtkNew->vRealPos == NULL )
    {
1130 1131
        if ( !Abc_FrameReadFlag("silentmode") )
            printf( "Missing key information.\n" );
1132 1133 1134
        return;
    }

1135
    assert( pNtkNew->vRealNodes == NULL );
1136 1137 1138
    // create drivers
    vDrivers = Vec_PtrStart( pNtkNew->nRealPos );
    vDriverInvs = Vec_IntStart( pNtkNew->nRealPos );
1139
    pNtkNew->vRealNodes = Vec_IntAlloc( Abc_NtkPoNum(pNtkNew) - pNtkNew->nRealPos );
1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150
    for ( i = pNtkNew->nRealPos; i < Abc_NtkPoNum(pNtkNew); i++ )
    {
        pObj = Abc_NtkPo( pNtkNew, i );
        if ( Abc_ObjFaninC0(pObj) )
            pNode = Abc_NtkCreateNodeInv( pNtkNew, Abc_ObjFanin0(pObj) );
        else
            pNode = Abc_NtkCreateNodeBuf( pNtkNew, Abc_ObjFanin0(pObj) );
//        if ( i % 5 == 4 )
//            printf( "%d", Abc_ObjFaninC0(pObj) );
        Vec_PtrPush( vDrivers, pNode );
        Vec_IntPush( vDriverInvs, Abc_ObjFaninC0(pObj) );
1151
        Vec_IntPush( pNtkNew->vRealNodes, Abc_ObjId(pNode) );
1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180
    }
    assert( Vec_PtrSize( vDrivers ) == Abc_NtkPoNum( pNtkNew ) );

    // create new logic
    vFanins = Vec_PtrAlloc( 2 );
    vDriversNew = Vec_PtrStart( Abc_NtkPoNum(pNtkNew) );
    Vec_VecForEachLevelInt( pNtkNew->vRealPos, vInfo, i )
    {
        // find complemented attribute
        numPo  = Vec_IntEntry( vInfo, 0 );
        fCompl = (strstr( Abc_ObjName(Abc_NtkPo(pNtkNew, numPo)), "SUB" ) != NULL);
        // consider parts
        Vec_IntForEachEntry( vInfo, numPo, k )
        {
            // update input
            if ( k > 0 )
                Vec_PtrWriteEntry( vDriversNew, numPo+2, pNode );
            // create first XOR
            Vec_PtrClear( vFanins );
            Vec_PtrPush( vFanins, (Abc_Obj_t *)Vec_PtrEntry(vDrivers, numPo+0) );
            Vec_PtrPush( vFanins, (Abc_Obj_t *)Vec_PtrEntry(vDrivers, numPo+1) );
            pExor = Abc_NtkCreateNodeExor( pNtkNew, vFanins );
            // update polarity
            pExor->pData = Hop_NotCond( (Hop_Obj_t *)pExor->pData, fCompl );
            // create second XOR
            Vec_PtrClear( vFanins );
            Vec_PtrPush( vFanins, pExor );
            Vec_PtrPush( vFanins, (Abc_Obj_t *)Vec_PtrEntry(vDrivers, numPo+2) ); 
            pNode = Abc_NtkCreateNodeExor( pNtkNew, vFanins );
1181
            // update pointers
1182
            Vec_PtrWriteEntry( vDriversNew, numPo+3, pNode );
1183
            Vec_IntWriteEntry( pNtkNew->vRealNodes, numPo+3 - pNtkNew->nRealPos, Abc_ObjId(pNode) );
1184 1185 1186 1187
            // create MUX
            pNode = Abc_NtkCreateNodeMux( pNtkNew, pExor, 
                (Abc_Obj_t *)Vec_PtrEntry(vDrivers, numPo+2), 
                (Abc_Obj_t *)Vec_PtrEntry(vDrivers, numPo+(fCompl ? 0 : 1)) );
1188
            // update pointers
1189
            Vec_PtrWriteEntry( vDriversNew, numPo+4, pNode );
1190
            Vec_IntWriteEntry( pNtkNew->vRealNodes, numPo+4 - pNtkNew->nRealPos, Abc_ObjId(pNode) );
1191 1192 1193 1194
        }
    }
    Vec_PtrFree( vFanins );

1195 1196
    Abc_NtkIfCheckRealNodes( pNtkNew );

1197 1198 1199 1200 1201 1202 1203
    // map internal nodes into PO numbers
    vNodeMap = Vec_IntStartFull( Abc_NtkObjNumMax(pNtkNew) );
    Vec_VecForEachLevelInt( pNtkNew->vRealPos, vInfo, i )
    Vec_IntForEachEntryReverse( vInfo, numPo, k )
    {
        pObj = Abc_NtkPo( pNtkNew, numPo+3 );
        Vec_IntWriteEntry( vNodeMap, Abc_ObjId( Abc_ObjFanin0(pObj) ), numPo+3 );
1204 1205 1206 1207 1208 1209

        // update the PO pointer
//        if ( Abc_ObjFaninC0(pObj) )
//            Abc_ObjXorFaninC( pObj, 0 );
//       Abc_ObjPatchFanin( pObj, Abc_ObjFanin0(pObj), Vec_PtrEntry(vDriversNew, numPo+3) );

1210 1211
        pObj = Abc_NtkPo( pNtkNew, numPo+4 );
        Vec_IntWriteEntry( vNodeMap, Abc_ObjId( Abc_ObjFanin0(pObj) ), numPo+4 );
1212 1213 1214 1215 1216 1217

        // update the PO pointer
//        if ( Abc_ObjFaninC0(pObj) )
//            Abc_ObjXorFaninC( pObj, 0 );
//        Abc_ObjPatchFanin( pObj, Abc_ObjFanin0(pObj), Vec_PtrEntry(vDriversNew, numPo+4) );

1218 1219 1220 1221 1222
    }

    // replace logic
    Abc_NtkForEachObj( pNtkNew, pObj, i )
    {
1223 1224
//        if ( Abc_ObjIsPo(pObj) )
//            continue;
1225 1226
        Abc_ObjForEachFanin( pObj, pFanin, k )
        {
1227 1228
            if ( !Abc_ObjIsNode(pFanin) || Abc_ObjFaninNum(pFanin) == 0 )
                continue;
1229 1230 1231 1232
            numPo = Vec_IntEntry( vNodeMap, Abc_ObjId(pFanin) );
            if ( numPo == ~0 )
                continue;
            // get the node and the complemented bit
1233
            pFaninNew = (Abc_Obj_t *)Vec_PtrEntry( vDriversNew, numPo );
1234 1235 1236
            fCompl    = Vec_IntEntry( vDriverInvs, numPo );
            if ( fCompl )
                pFaninNew = Abc_NtkCreateNodeInv( pNtkNew, pFaninNew );
1237 1238
//            else
//                pFaninNew = Abc_NtkCreateNodeBuf( pNtkNew, pFaninNew );
1239 1240 1241 1242 1243

            if ( !Abc_NtkIfCheckTfi( pNtkNew, pObj, pFaninNew ) )
                Abc_ObjPatchFanin( pObj, pFanin, pFaninNew );
        }
    }    
1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256

    // sweep
    Abc_NtkCleanupNodes( pNtkNew, vDriversNew, 0 );

    // make sure that all vDriversNew are still present
    {
        Abc_Obj_t * pObj;
        int i;
        Vec_PtrForEachEntry( Abc_Obj_t *, vDriversNew, pObj, i )
            if ( pObj && Abc_ObjIsNone(pObj) )
                assert( 0 );
    }

1257 1258 1259 1260 1261 1262
    Vec_PtrFree( vDrivers );
    Vec_PtrFree( vDriversNew );
    Vec_IntFree( vNodeMap );
    Vec_IntFree( vDriverInvs );

    // count non-trivial LUTs nodes
1263 1264
    nRealLuts   = -2 * Vec_VecSizeSize(pNtkNew->vRealPos);
    RealLutArea = -(p->pPars->pLutLib ? p->pPars->pLutLib->pLutAreas[2] + p->pPars->pLutLib->pLutAreas[3] : 2.0) * Vec_VecSizeSize(pNtkNew->vRealPos);
1265 1266
    Abc_NtkForEachNode( pNtkNew, pNode, i )
        if ( Abc_ObjFaninNum(pNode) > 1 )
1267
        {
1268
            nRealLuts++;
1269 1270
            RealLutArea += p->pPars->pLutLib->pLutAreas[Abc_ObjFaninNum(pNode)];
        }
1271 1272 1273 1274
    if ( !Abc_FrameReadFlag("silentmode") )
        printf( "The number of real LUTs = %d.  Real LUT area = %.2f.\n", nRealLuts, RealLutArea );
    pNtkNew->nRealLuts = nRealLuts;
    pNtkNew->nRealArea = RealLutArea;
1275 1276 1277

    Abc_NtkIfCheckRealNodes( pNtkNew );

1278 1279
}

Alan Mishchenko committed
1280 1281 1282 1283 1284
////////////////////////////////////////////////////////////////////////
///                       END OF FILE                                ///
////////////////////////////////////////////////////////////////////////


1285 1286
ABC_NAMESPACE_IMPL_END