abc/src/base/abci/abcStrash.c

408 lines
13 KiB
C
Raw Normal View History

2005-07-29 17:01:00 +02:00
/**CFile****************************************************************
FileName [aigStrash.c]
SystemName [ABC: Logic synthesis and verification system.]
PackageName [Network and node package.]
Synopsis [Strashing of the current network.]
Author [Alan Mishchenko]
Affiliation [UC Berkeley]
Date [Ver. 1.0. Started - June 20, 2005.]
Revision [$Id: aigStrash.c,v 1.00 2005/06/20 00:00:00 alanmi Exp $]
***********************************************************************/
#include "abc.h"
#include "extra.h"
2005-09-02 17:01:00 +02:00
#include "dec.h"
2005-07-29 17:01:00 +02:00
////////////////////////////////////////////////////////////////////////
/// DECLARATIONS ///
////////////////////////////////////////////////////////////////////////
// static functions
2005-08-07 17:01:00 +02:00
static void Abc_NtkStrashPerform( Abc_Ntk_t * pNtk, Abc_Ntk_t * pNtkAig, bool fAllNodes );
2005-11-14 17:01:00 +01:00
static Abc_Obj_t * Abc_NodeStrashSop( Abc_Ntk_t * pNtkNew, Abc_Obj_t * pNode, char * pSop );
2005-12-22 17:01:00 +01:00
static Abc_Obj_t * Abc_NodeStrashExor( Abc_Ntk_t * pNtkNew, Abc_Obj_t * pNode, char * pSop );
2005-11-14 17:01:00 +01:00
static Abc_Obj_t * Abc_NodeStrashFactor( Abc_Ntk_t * pNtkNew, Abc_Obj_t * pNode, char * pSop );
2005-07-29 17:01:00 +02:00
extern char * Mio_GateReadSop( void * pGate );
////////////////////////////////////////////////////////////////////////
2005-10-12 17:01:00 +02:00
/// FUNCTION DEFINITIONS ///
2005-07-29 17:01:00 +02:00
////////////////////////////////////////////////////////////////////////
/**Function*************************************************************
Synopsis [Creates the strashed AIG network.]
2005-08-24 17:01:00 +02:00
Description [Converts the logic network or the AIG into a
structurally hashed AIG.]
2005-07-29 17:01:00 +02:00
SideEffects []
SeeAlso []
***********************************************************************/
2005-08-31 17:01:00 +02:00
Abc_Ntk_t * Abc_NtkStrash( Abc_Ntk_t * pNtk, bool fAllNodes, bool fCleanup )
2005-07-29 17:01:00 +02:00
{
Abc_Ntk_t * pNtkAig;
2005-08-14 17:01:00 +02:00
int nNodes;
2005-07-29 17:01:00 +02:00
assert( !Abc_NtkIsNetlist(pNtk) );
2005-11-14 17:01:00 +01:00
assert( !Abc_NtkIsSeq(pNtk) );
2005-08-29 17:01:00 +02:00
if ( Abc_NtkIsBddLogic(pNtk) )
2006-04-07 17:01:00 +02:00
{
if ( !Abc_NtkBddToSop(pNtk, 0) )
{
printf( "Converting to SOPs has failed.\n" );
return NULL;
}
}
2005-07-29 17:01:00 +02:00
// print warning about choice nodes
2005-09-02 17:01:00 +02:00
if ( Abc_NtkGetChoiceNum( pNtk ) )
2005-07-29 17:01:00 +02:00
printf( "Warning: The choice nodes in the initial AIG are removed by strashing.\n" );
// perform strashing
2005-09-13 17:01:00 +02:00
pNtkAig = Abc_NtkStartFrom( pNtk, ABC_NTK_STRASH, ABC_FUNC_AIG );
2005-11-14 17:01:00 +01:00
if ( Abc_NtkConst1(pNtk) )
Abc_NtkConst1(pNtk)->pCopy = NULL;
2005-08-07 17:01:00 +02:00
Abc_NtkStrashPerform( pNtk, pNtkAig, fAllNodes );
2005-07-29 17:01:00 +02:00
Abc_NtkFinalize( pNtk, pNtkAig );
// print warning about self-feed latches
2005-12-05 17:01:00 +01:00
// if ( Abc_NtkCountSelfFeedLatches(pNtkAig) )
// printf( "Warning: The network has %d self-feeding latches.\n", Abc_NtkCountSelfFeedLatches(pNtkAig) );
2005-08-31 17:01:00 +02:00
if ( fCleanup && (nNodes = Abc_AigCleanup(pNtkAig->pManFunc)) )
2006-04-07 17:01:00 +02:00
{
// printf( "Warning: AIG cleanup removed %d nodes (this is not a bug).\n", nNodes );
}
2005-07-29 17:01:00 +02:00
// duplicate EXDC
if ( pNtk->pExdc )
2005-10-05 17:01:00 +02:00
pNtkAig->pExdc = Abc_NtkDup( pNtk->pExdc );
2005-07-29 17:01:00 +02:00
// make sure everything is okay
2005-09-05 17:01:00 +02:00
if ( !Abc_NtkCheck( pNtkAig ) )
2005-07-29 17:01:00 +02:00
{
printf( "Abc_NtkStrash: The network check has failed.\n" );
Abc_NtkDelete( pNtkAig );
return NULL;
}
return pNtkAig;
}
2005-08-24 17:01:00 +02:00
/**Function*************************************************************
Synopsis [Appends the second network to the first.]
Description [Modifies the first network by adding the logic of the second.
Performs structural hashing while appending the networks. Does not add
the COs of the second. Does not change the second network. Returns 0
if the appending failed, 1 otherise.]
SideEffects []
SeeAlso []
***********************************************************************/
int Abc_NtkAppend( Abc_Ntk_t * pNtk1, Abc_Ntk_t * pNtk2 )
{
Abc_Obj_t * pObj;
int i;
// the first network should be an AIG
2005-08-29 17:01:00 +02:00
assert( Abc_NtkIsStrash(pNtk1) );
assert( Abc_NtkIsLogic(pNtk2) || Abc_NtkIsStrash(pNtk2) );
if ( Abc_NtkIsBddLogic(pNtk2) )
2006-04-07 17:01:00 +02:00
{
if ( !Abc_NtkBddToSop(pNtk2, 0) )
{
printf( "Converting to SOPs has failed.\n" );
return 0;
}
}
2005-08-24 17:01:00 +02:00
// check that the networks have the same PIs
// reorder PIs of pNtk2 according to pNtk1
if ( !Abc_NtkCompareSignals( pNtk1, pNtk2, 1 ) )
return 0;
// perform strashing
Abc_NtkCleanCopy( pNtk2 );
Abc_NtkForEachCi( pNtk2, pObj, i )
pObj->pCopy = Abc_NtkCi(pNtk1, i);
// add pNtk2 to pNtk1 while strashing
Abc_NtkStrashPerform( pNtk2, pNtk1, 1 );
// make sure that everything is okay
2005-09-05 17:01:00 +02:00
if ( !Abc_NtkCheck( pNtk1 ) )
2005-08-24 17:01:00 +02:00
{
printf( "Abc_NtkAppend: The network check has failed.\n" );
return 0;
}
return 1;
}
2005-07-29 17:01:00 +02:00
/**Function*************************************************************
Synopsis [Prepares the network for strashing.]
Description []
SideEffects []
SeeAlso []
***********************************************************************/
2005-08-07 17:01:00 +02:00
void Abc_NtkStrashPerform( Abc_Ntk_t * pNtk, Abc_Ntk_t * pNtkNew, bool fAllNodes )
2005-07-29 17:01:00 +02:00
{
ProgressBar * pProgress;
Vec_Ptr_t * vNodes;
Abc_Obj_t * pNode, * pNodeNew, * pObj;
int i;
// perform strashing
2005-08-14 17:01:00 +02:00
vNodes = Abc_NtkDfs( pNtk, fAllNodes );
2005-07-29 17:01:00 +02:00
pProgress = Extra_ProgressBarStart( stdout, vNodes->nSize );
2005-08-14 17:01:00 +02:00
Vec_PtrForEachEntry( vNodes, pNode, i )
2005-07-29 17:01:00 +02:00
{
Extra_ProgressBarUpdate( pProgress, i, NULL );
// get the node
assert( Abc_ObjIsNode(pNode) );
// strash the node
2005-11-14 17:01:00 +01:00
pNodeNew = Abc_NodeStrash( pNtkNew, pNode );
2005-07-29 17:01:00 +02:00
// get the old object
2005-08-24 17:01:00 +02:00
pObj = Abc_ObjFanout0Ntk( pNode );
2005-07-29 17:01:00 +02:00
// make sure the node is not yet strashed
assert( pObj->pCopy == NULL );
// mark the old object with the new AIG node
pObj->pCopy = pNodeNew;
2006-04-07 17:01:00 +02:00
Abc_HManAddProto( pObj->pCopy, pObj );
2005-07-29 17:01:00 +02:00
}
Vec_PtrFree( vNodes );
Extra_ProgressBarStop( pProgress );
}
/**Function*************************************************************
Synopsis [Strashes one logic node.]
Description []
SideEffects []
SeeAlso []
***********************************************************************/
2005-11-14 17:01:00 +01:00
Abc_Obj_t * Abc_NodeStrash( Abc_Ntk_t * pNtkNew, Abc_Obj_t * pNode )
2005-07-29 17:01:00 +02:00
{
int fUseFactor = 1;
char * pSop;
2005-12-22 17:01:00 +01:00
extern int Abc_SopIsExorType( char * pSop );
2005-07-29 17:01:00 +02:00
assert( Abc_ObjIsNode(pNode) );
// consider the case when the graph is an AIG
2005-08-29 17:01:00 +02:00
if ( Abc_NtkIsStrash(pNode->pNtk) )
2005-07-29 17:01:00 +02:00
{
if ( Abc_NodeIsConst(pNode) )
2005-11-14 17:01:00 +01:00
return Abc_NtkConst1(pNtkNew);
return Abc_AigAnd( pNtkNew->pManFunc, Abc_ObjChild0Copy(pNode), Abc_ObjChild1Copy(pNode) );
2005-07-29 17:01:00 +02:00
}
// get the SOP of the node
2005-08-29 17:01:00 +02:00
if ( Abc_NtkHasMapping(pNode->pNtk) )
2005-07-29 17:01:00 +02:00
pSop = Mio_GateReadSop(pNode->pData);
else
pSop = pNode->pData;
2005-09-02 17:01:00 +02:00
// consider the constant node
2005-07-29 17:01:00 +02:00
if ( Abc_NodeIsConst(pNode) )
2005-11-14 17:01:00 +01:00
return Abc_ObjNotCond( Abc_NtkConst1(pNtkNew), Abc_SopIsConst0(pSop) );
2005-07-29 17:01:00 +02:00
2005-12-22 17:01:00 +01:00
// consider the special case of EXOR function
if ( Abc_SopIsExorType(pSop) )
return Abc_NodeStrashExor( pNtkNew, pNode, pSop );
2005-07-29 17:01:00 +02:00
// decide when to use factoring
if ( fUseFactor && Abc_ObjFaninNum(pNode) > 2 && Abc_SopGetCubeNum(pSop) > 1 )
2005-11-14 17:01:00 +01:00
return Abc_NodeStrashFactor( pNtkNew, pNode, pSop );
return Abc_NodeStrashSop( pNtkNew, pNode, pSop );
2005-07-29 17:01:00 +02:00
}
/**Function*************************************************************
Synopsis [Strashes one logic node using its SOP.]
Description []
SideEffects []
SeeAlso []
***********************************************************************/
2005-11-14 17:01:00 +01:00
Abc_Obj_t * Abc_NodeStrashSop( Abc_Ntk_t * pNtkNew, Abc_Obj_t * pNode, char * pSop )
2005-07-29 17:01:00 +02:00
{
2005-11-14 17:01:00 +01:00
Abc_Aig_t * pMan = pNtkNew->pManFunc;
2005-07-29 17:01:00 +02:00
Abc_Obj_t * pFanin, * pAnd, * pSum;
char * pCube;
int i, nFanins;
// get the number of node's fanins
nFanins = Abc_ObjFaninNum( pNode );
assert( nFanins == Abc_SopGetVarNum(pSop) );
// go through the cubes of the node's SOP
2005-11-14 17:01:00 +01:00
pSum = Abc_ObjNot( Abc_NtkConst1(pNtkNew) );
2005-07-29 17:01:00 +02:00
Abc_SopForEachCube( pSop, nFanins, pCube )
{
// create the AND of literals
2005-11-14 17:01:00 +01:00
pAnd = Abc_NtkConst1(pNtkNew);
2005-07-29 17:01:00 +02:00
Abc_ObjForEachFanin( pNode, pFanin, i ) // pFanin can be a net
{
if ( pCube[i] == '1' )
pAnd = Abc_AigAnd( pMan, pAnd, pFanin->pCopy );
else if ( pCube[i] == '0' )
pAnd = Abc_AigAnd( pMan, pAnd, Abc_ObjNot(pFanin->pCopy) );
}
// add to the sum of cubes
pSum = Abc_AigOr( pMan, pSum, pAnd );
}
// decide whether to complement the result
2005-08-24 17:01:00 +02:00
if ( Abc_SopIsComplement(pSop) )
2005-07-29 17:01:00 +02:00
pSum = Abc_ObjNot(pSum);
2005-12-22 17:01:00 +01:00
return pSum;
}
/**Function*************************************************************
Synopsis [Strashed n-input XOR function.]
Description []
SideEffects []
SeeAlso []
***********************************************************************/
Abc_Obj_t * Abc_NodeStrashExor( Abc_Ntk_t * pNtkNew, Abc_Obj_t * pNode, char * pSop )
{
Abc_Aig_t * pMan = pNtkNew->pManFunc;
Abc_Obj_t * pFanin, * pSum;
int i, nFanins;
// get the number of node's fanins
nFanins = Abc_ObjFaninNum( pNode );
assert( nFanins == Abc_SopGetVarNum(pSop) );
// go through the cubes of the node's SOP
pSum = Abc_ObjNot( Abc_NtkConst1(pNtkNew) );
for ( i = 0; i < nFanins; i++ )
{
pFanin = Abc_ObjFanin( pNode, i );
pSum = Abc_AigXor( pMan, pSum, pFanin->pCopy );
}
if ( Abc_SopIsComplement(pSop) )
pSum = Abc_ObjNot(pSum);
2005-07-29 17:01:00 +02:00
return pSum;
}
/**Function*************************************************************
Synopsis [Strashes one logic node using its SOP.]
Description []
SideEffects []
SeeAlso []
***********************************************************************/
2005-11-14 17:01:00 +01:00
Abc_Obj_t * Abc_NodeStrashFactor( Abc_Ntk_t * pNtkNew, Abc_Obj_t * pRoot, char * pSop )
2005-08-17 17:01:00 +02:00
{
2005-09-02 17:01:00 +02:00
Dec_Graph_t * pFForm;
Dec_Node_t * pNode;
Abc_Obj_t * pAnd;
2005-08-17 17:01:00 +02:00
int i;
2005-09-02 17:01:00 +02:00
// perform factoring
pFForm = Dec_Factor( pSop );
2005-08-17 17:01:00 +02:00
// collect the fanins
2005-09-02 17:01:00 +02:00
Dec_GraphForEachLeaf( pFForm, pNode, i )
pNode->pFunc = Abc_ObjFanin(pRoot,i)->pCopy;
2005-08-17 17:01:00 +02:00
// perform strashing
2005-11-14 17:01:00 +01:00
pAnd = Dec_GraphToNetwork( pNtkNew, pFForm );
2005-09-02 17:01:00 +02:00
Dec_GraphFree( pFForm );
2005-08-17 17:01:00 +02:00
return pAnd;
}
2006-04-12 17:01:00 +02:00
/**Function*************************************************************
Synopsis [Copies the topmost levels of the network.]
Description []
SideEffects []
SeeAlso []
***********************************************************************/
Abc_Obj_t * Abc_NtkTopmost_rec( Abc_Ntk_t * pNtkNew, Abc_Obj_t * pNode, int LevelCut )
{
assert( Abc_ObjIsComplement(pNode) );
if ( pNode->pCopy )
return pNode->pCopy;
if ( pNode->Level <= (unsigned)LevelCut )
return pNode->pCopy = Abc_NtkCreatePi( pNtkNew );
Abc_NtkTopmost_rec( pNtkNew, Abc_ObjFanin0(pNode), LevelCut );
Abc_NtkTopmost_rec( pNtkNew, Abc_ObjFanin1(pNode), LevelCut );
return pNode->pCopy = Abc_AigAnd( pNtkNew->pManFunc, Abc_ObjChild0Copy(pNode), Abc_ObjChild1Copy(pNode) );
}
/**Function*************************************************************
Synopsis [Copies the topmost levels of the network.]
Description []
SideEffects []
SeeAlso []
***********************************************************************/
Abc_Ntk_t * Abc_NtkTopmost( Abc_Ntk_t * pNtk, int nLevels )
{
Abc_Ntk_t * pNtkNew;
Abc_Obj_t * pObjNew, * pPoNew;
int LevelCut;
assert( Abc_NtkIsStrash(pNtk) );
assert( Abc_NtkCoNum(pNtk) == 1 );
// get the cutoff level
LevelCut = ABC_MAX( 0, Abc_AigGetLevelNum(pNtk) - nLevels );
// start the network
pNtkNew = Abc_NtkAlloc( ABC_NTK_STRASH, ABC_FUNC_AIG );
pNtkNew->pName = Extra_UtilStrsav(pNtk->pName);
Abc_NtkConst1(pNtk)->pCopy = Abc_NtkConst1(pNtkNew);
// create PIs below the cut and nodes above the cut
Abc_NtkCleanCopy( pNtk );
pObjNew = Abc_NtkTopmost_rec( pNtkNew, Abc_ObjFanin0(Abc_NtkPo(pNtk, 0)), LevelCut );
// add the PO node and name
pPoNew = Abc_NtkCreatePo(pNtkNew);
Abc_ObjAddFanin( pPoNew, pObjNew );
Abc_NtkAddDummyPiNames( pNtkNew );
Abc_NtkLogicStoreName( pPoNew, Abc_ObjName(Abc_NtkPo(pNtk, 0)) );
// make sure everything is okay
if ( !Abc_NtkCheck( pNtkNew ) )
{
printf( "Abc_NtkTopmost: The network check has failed.\n" );
Abc_NtkDelete( pNtkNew );
return NULL;
}
return pNtkNew;
}
2005-07-29 17:01:00 +02:00
////////////////////////////////////////////////////////////////////////
/// END OF FILE ///
////////////////////////////////////////////////////////////////////////