00001
00021 #include "abc.h"
00022 #include "rwr.h"
00023 #include "dec.h"
00024
00025
00026
00027
00028
00029
00030
00034
00035 static Cut_Man_t * Abc_NtkStartCutManForRewrite( Abc_Ntk_t * pNtk );
00036 static void Abc_NodePrintCuts( Abc_Obj_t * pNode );
00037 static void Abc_ManShowCutCone( Abc_Obj_t * pNode, Vec_Ptr_t * vLeaves );
00038
00039 extern void Abc_PlaceBegin( Abc_Ntk_t * pNtk );
00040 extern void Abc_PlaceEnd( Abc_Ntk_t * pNtk );
00041 extern void Abc_PlaceUpdate( Vec_Ptr_t * vAddedCells, Vec_Ptr_t * vUpdatedNets );
00042
00046
00058 int Abc_NtkRewrite( Abc_Ntk_t * pNtk, int fUpdateLevel, int fUseZeros, int fVerbose, int fVeryVerbose, int fPlaceEnable )
00059 {
00060 ProgressBar * pProgress;
00061 Cut_Man_t * pManCut;
00062 Rwr_Man_t * pManRwr;
00063 Abc_Obj_t * pNode;
00064 Vec_Ptr_t * vAddedCells = NULL, * vUpdatedNets = NULL;
00065 Dec_Graph_t * pGraph;
00066 int i, nNodes, nGain, fCompl;
00067 int clk, clkStart = clock();
00068
00069 assert( Abc_NtkIsStrash(pNtk) );
00070
00071 Abc_AigCleanup(pNtk->pManFunc);
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088 pManRwr = Rwr_ManStart( 0 );
00089 if ( pManRwr == NULL )
00090 return 0;
00091
00092 if ( fUpdateLevel )
00093 Abc_NtkStartReverseLevels( pNtk, 0 );
00094
00095 clk = clock();
00096 pManCut = Abc_NtkStartCutManForRewrite( pNtk );
00097 Rwr_ManAddTimeCuts( pManRwr, clock() - clk );
00098 pNtk->pManCut = pManCut;
00099
00100 if ( fVeryVerbose )
00101 Rwr_ScoresClean( pManRwr );
00102
00103
00104 pManRwr->nNodesBeg = Abc_NtkNodeNum(pNtk);
00105 nNodes = Abc_NtkObjNumMax(pNtk);
00106 pProgress = Extra_ProgressBarStart( stdout, nNodes );
00107 Abc_NtkForEachNode( pNtk, pNode, i )
00108 {
00109 Extra_ProgressBarUpdate( pProgress, i, NULL );
00110
00111 if ( i >= nNodes )
00112 break;
00113
00114 if ( Abc_NodeIsPersistant(pNode) )
00115 continue;
00116
00117 if ( Abc_ObjFanoutNum(pNode) > 1000 )
00118 continue;
00119
00120
00121 nGain = Rwr_NodeRewrite( pManRwr, pManCut, pNode, fUpdateLevel, fUseZeros, fPlaceEnable );
00122 if ( !(nGain > 0 || nGain == 0 && fUseZeros) )
00123 continue;
00124
00125
00126
00127 pGraph = Rwr_ManReadDecs(pManRwr);
00128 fCompl = Rwr_ManReadCompl(pManRwr);
00129
00130
00131 if ( fPlaceEnable )
00132 Abc_AigUpdateReset( pNtk->pManFunc );
00133
00134
00135 if ( fCompl ) Dec_GraphComplement( pGraph );
00136 clk = clock();
00137 Dec_GraphUpdateNetwork( pNode, pGraph, fUpdateLevel, nGain );
00138 Rwr_ManAddTimeUpdate( pManRwr, clock() - clk );
00139 if ( fCompl ) Dec_GraphComplement( pGraph );
00140
00141
00142
00143
00144 }
00145 Extra_ProgressBarStop( pProgress );
00146 Rwr_ManAddTimeTotal( pManRwr, clock() - clkStart );
00147
00148 pManRwr->nNodesEnd = Abc_NtkNodeNum(pNtk);
00149 if ( fVerbose )
00150 Rwr_ManPrintStats( pManRwr );
00151
00152 if ( fVeryVerbose )
00153 Rwr_ScoresReport( pManRwr );
00154
00155 Rwr_ManStop( pManRwr );
00156 Cut_ManStop( pManCut );
00157 pNtk->pManCut = NULL;
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167 {
00168
00169 Abc_NtkReassignIds( pNtk );
00170
00171 }
00172
00173
00174 if ( fUpdateLevel )
00175 Abc_NtkStopReverseLevels( pNtk );
00176 else
00177 Abc_NtkLevel( pNtk );
00178
00179 if ( !Abc_NtkCheck( pNtk ) )
00180 {
00181 printf( "Abc_NtkRewrite: The network check has failed.\n" );
00182 return 0;
00183 }
00184 return 1;
00185 }
00186
00187
00199 Cut_Man_t * Abc_NtkStartCutManForRewrite( Abc_Ntk_t * pNtk )
00200 {
00201 static Cut_Params_t Params, * pParams = &Params;
00202 Cut_Man_t * pManCut;
00203 Abc_Obj_t * pObj;
00204 int i;
00205
00206 memset( pParams, 0, sizeof(Cut_Params_t) );
00207 pParams->nVarsMax = 4;
00208 pParams->nKeepMax = 250;
00209 pParams->fTruth = 1;
00210 pParams->fFilter = 1;
00211 pParams->fSeq = 0;
00212 pParams->fDrop = 0;
00213 pParams->fVerbose = 0;
00214 pParams->nIdsMax = Abc_NtkObjNumMax( pNtk );
00215 pManCut = Cut_ManStart( pParams );
00216 if ( pParams->fDrop )
00217 Cut_ManSetFanoutCounts( pManCut, Abc_NtkFanoutCounts(pNtk) );
00218
00219 Abc_NtkForEachCi( pNtk, pObj, i )
00220 if ( Abc_ObjFanoutNum(pObj) > 0 )
00221 Cut_NodeSetTriv( pManCut, pObj->Id );
00222 return pManCut;
00223 }
00224
00236 void Abc_NodePrintCuts( Abc_Obj_t * pNode )
00237 {
00238 Vec_Ptr_t * vCuts;
00239 Cut_Cut_t * pCut;
00240 int k;
00241
00242 printf( "\nNode %s\n", Abc_ObjName(pNode) );
00243 vCuts = (Vec_Ptr_t *)pNode->pCopy;
00244 Vec_PtrForEachEntry( vCuts, pCut, k )
00245 {
00246 Extra_PrintBinary( stdout, (unsigned *)&pCut->uSign, 16 );
00247 printf( " " );
00248 Cut_CutPrint( pCut, 0 );
00249 printf( "\n" );
00250 }
00251 }
00252
00253
00265 void Abc_ManRewritePrintDivs( Vec_Ptr_t * vDivs, int nLeaves )
00266 {
00267 Abc_Obj_t * pFanin, * pNode, * pRoot;
00268 int i, k;
00269 pRoot = Vec_PtrEntryLast(vDivs);
00270
00271 Vec_PtrForEachEntry( vDivs, pNode, i )
00272 {
00273 if ( i < nLeaves )
00274 {
00275 printf( "%6d : %c\n", pNode->Id, 'a'+i );
00276 continue;
00277 }
00278 printf( "%6d : %2d = ", pNode->Id, i );
00279
00280 Vec_PtrForEachEntry( vDivs, pFanin, k )
00281 if ( Abc_ObjFanin0(pNode) == pFanin )
00282 break;
00283 if ( k < nLeaves )
00284 printf( "%c", 'a' + k );
00285 else
00286 printf( "%d", k );
00287 printf( "%s ", Abc_ObjFaninC0(pNode)? "\'" : "" );
00288
00289 Vec_PtrForEachEntry( vDivs, pFanin, k )
00290 if ( Abc_ObjFanin1(pNode) == pFanin )
00291 break;
00292 if ( k < nLeaves )
00293 printf( "%c", 'a' + k );
00294 else
00295 printf( "%d", k );
00296 printf( "%s ", Abc_ObjFaninC1(pNode)? "\'" : "" );
00297 if ( pNode == pRoot )
00298 printf( " root" );
00299 printf( "\n" );
00300 }
00301 printf( "\n" );
00302 }
00303
00315 void Abc_ManShowCutCone_rec( Abc_Obj_t * pNode, Vec_Ptr_t * vDivs )
00316 {
00317 if ( Abc_NodeIsTravIdCurrent(pNode) )
00318 return;
00319 Abc_NodeSetTravIdCurrent(pNode);
00320 Abc_ManShowCutCone_rec( Abc_ObjFanin0(pNode), vDivs );
00321 Abc_ManShowCutCone_rec( Abc_ObjFanin1(pNode), vDivs );
00322 Vec_PtrPush( vDivs, pNode );
00323 }
00324
00336 void Abc_ManShowCutCone( Abc_Obj_t * pNode, Vec_Ptr_t * vLeaves )
00337 {
00338 Abc_Ntk_t * pNtk = pNode->pNtk;
00339 Abc_Obj_t * pObj;
00340 Vec_Ptr_t * vDivs;
00341 int i;
00342 vDivs = Vec_PtrAlloc( 100 );
00343 Abc_NtkIncrementTravId( pNtk );
00344 Vec_PtrForEachEntry( vLeaves, pObj, i )
00345 {
00346 Abc_NodeSetTravIdCurrent( Abc_ObjRegular(pObj) );
00347 Vec_PtrPush( vDivs, Abc_ObjRegular(pObj) );
00348 }
00349 Abc_ManShowCutCone_rec( pNode, vDivs );
00350 Abc_ManRewritePrintDivs( vDivs, Vec_PtrSize(vLeaves) );
00351 Vec_PtrFree( vDivs );
00352 }
00353
00354
00366 void Abc_RwrExpWithCut_rec( Abc_Obj_t * pNode, Vec_Ptr_t * vLeaves, int fUseA )
00367 {
00368 if ( Vec_PtrFind(vLeaves, pNode) >= 0 || Vec_PtrFind(vLeaves, Abc_ObjNot(pNode)) >= 0 )
00369 {
00370 if ( fUseA )
00371 Abc_ObjRegular(pNode)->fMarkA = 1;
00372 else
00373 Abc_ObjRegular(pNode)->fMarkB = 1;
00374 return;
00375 }
00376 assert( Abc_ObjIsNode(pNode) );
00377 Abc_RwrExpWithCut_rec( Abc_ObjFanin0(pNode), vLeaves, fUseA );
00378 Abc_RwrExpWithCut_rec( Abc_ObjFanin1(pNode), vLeaves, fUseA );
00379 }
00380
00392 void Abc_RwrExpWithCut( Abc_Obj_t * pNode, Vec_Ptr_t * vLeaves )
00393 {
00394 Abc_Obj_t * pObj;
00395 int i, CountA, CountB;
00396 Abc_RwrExpWithCut_rec( Abc_ObjFanin0(pNode), vLeaves, 1 );
00397 Abc_RwrExpWithCut_rec( Abc_ObjFanin1(pNode), vLeaves, 0 );
00398 CountA = CountB = 0;
00399 Vec_PtrForEachEntry( vLeaves, pObj, i )
00400 {
00401 CountA += Abc_ObjRegular(pObj)->fMarkA;
00402 CountB += Abc_ObjRegular(pObj)->fMarkB;
00403 Abc_ObjRegular(pObj)->fMarkA = 0;
00404 Abc_ObjRegular(pObj)->fMarkB = 0;
00405 }
00406 printf( "(%d,%d:%d) ", CountA, CountB, CountA+CountB-Vec_PtrSize(vLeaves) );
00407 }
00408
00409
00413
00414