00001
00021 #include "abc.h"
00022 #include "main.h"
00023 #include "mio.h"
00024 #include "mapper.h"
00025
00029
00030 static Map_Man_t * Abc_NtkToMap( Abc_Ntk_t * pNtk, double DelayTarget, int fRecovery, float * pSwitching, int fVerbose );
00031 static Abc_Ntk_t * Abc_NtkFromMap( Map_Man_t * pMan, Abc_Ntk_t * pNtk );
00032 static Abc_Obj_t * Abc_NodeFromMap_rec( Abc_Ntk_t * pNtkNew, Map_Node_t * pNodeMap, int fPhase );
00033 static Abc_Obj_t * Abc_NodeFromMapPhase_rec( Abc_Ntk_t * pNtkNew, Map_Node_t * pNodeMap, int fPhase );
00034 static Abc_Obj_t * Abc_NodeFromMapSuper_rec( Abc_Ntk_t * pNtkNew, Map_Node_t * pNodeMap, Map_Super_t * pSuper, Abc_Obj_t * pNodePis[], int nNodePis );
00035
00036 static Abc_Ntk_t * Abc_NtkFromMapSuperChoice( Map_Man_t * pMan, Abc_Ntk_t * pNtk );
00037 static void Abc_NodeSuperChoice( Abc_Ntk_t * pNtkNew, Abc_Obj_t * pNode );
00038 static void Abc_NodeFromMapCutPhase( Abc_Ntk_t * pNtkNew, Map_Cut_t * pCut, int fPhase );
00039 static Abc_Obj_t * Abc_NodeFromMapSuperChoice_rec( Abc_Ntk_t * pNtkNew, Map_Super_t * pSuper, Abc_Obj_t * pNodePis[], int nNodePis );
00040
00041
00045
00057 Abc_Ntk_t * Abc_NtkMap( Abc_Ntk_t * pNtk, double DelayTarget, int fRecovery, int fSwitching, int fVerbose )
00058 {
00059 int fShowSwitching = 1;
00060 Abc_Ntk_t * pNtkNew;
00061 Map_Man_t * pMan;
00062 Vec_Int_t * vSwitching;
00063 float * pSwitching = NULL;
00064 int clk;
00065
00066 assert( Abc_NtkIsStrash(pNtk) );
00067
00068
00069 if ( Abc_FrameReadLibGen() == NULL )
00070 {
00071 printf( "The current library is not available.\n" );
00072 return 0;
00073 }
00074
00075
00076 if ( Abc_FrameReadLibSuper() == NULL && Abc_FrameReadLibGen() )
00077 {
00078 printf( "A simple supergate library is derived from gate library \"%s\".\n",
00079 Mio_LibraryReadName(Abc_FrameReadLibGen()) );
00080 Map_SuperLibDeriveFromGenlib( Abc_FrameReadLibGen() );
00081 }
00082
00083
00084 if ( Abc_NtkGetChoiceNum( pNtk ) )
00085 printf( "Performing mapping with choices.\n" );
00086
00087
00088 fShowSwitching |= fSwitching;
00089 if ( fShowSwitching )
00090 {
00091 extern Vec_Int_t * Sim_NtkComputeSwitching( Abc_Ntk_t * pNtk, int nPatterns );
00092 vSwitching = Sim_NtkComputeSwitching( pNtk, 4096 );
00093 pSwitching = (float *)vSwitching->pArray;
00094 }
00095
00096
00097 pMan = Abc_NtkToMap( pNtk, DelayTarget, fRecovery, pSwitching, fVerbose );
00098 if ( pSwitching ) Vec_IntFree( vSwitching );
00099 if ( pMan == NULL )
00100 return NULL;
00101 clk = clock();
00102 Map_ManSetSwitching( pMan, fSwitching );
00103 if ( !Map_Mapping( pMan ) )
00104 {
00105 Map_ManFree( pMan );
00106 return NULL;
00107 }
00108
00109
00110
00111 pNtkNew = Abc_NtkFromMap( pMan, pNtk );
00112 if ( pNtkNew == NULL )
00113 return NULL;
00114 Map_ManFree( pMan );
00115
00116 if ( pNtk->pExdc )
00117 pNtkNew->pExdc = Abc_NtkDup( pNtk->pExdc );
00118
00119
00120 if ( !Abc_NtkCheck( pNtkNew ) )
00121 {
00122 printf( "Abc_NtkMap: The network check has failed.\n" );
00123 Abc_NtkDelete( pNtkNew );
00124 return NULL;
00125 }
00126 return pNtkNew;
00127 }
00128
00140 Map_Man_t * Abc_NtkToMap( Abc_Ntk_t * pNtk, double DelayTarget, int fRecovery, float * pSwitching, int fVerbose )
00141 {
00142 Map_Man_t * pMan;
00143 ProgressBar * pProgress;
00144 Map_Node_t * pNodeMap;
00145 Vec_Ptr_t * vNodes;
00146 Abc_Obj_t * pNode, * pFanin, * pPrev;
00147 int i;
00148
00149 assert( Abc_NtkIsStrash(pNtk) );
00150
00151
00152 pMan = Map_ManCreate( Abc_NtkPiNum(pNtk) + Abc_NtkLatchNum(pNtk), Abc_NtkPoNum(pNtk) + Abc_NtkLatchNum(pNtk), fVerbose );
00153 if ( pMan == NULL )
00154 return NULL;
00155 Map_ManSetAreaRecovery( pMan, fRecovery );
00156 Map_ManSetOutputNames( pMan, Abc_NtkCollectCioNames(pNtk, 1) );
00157 Map_ManSetDelayTarget( pMan, (float)DelayTarget );
00158 Map_ManSetInputArrivals( pMan, (Map_Time_t *)Abc_NtkGetCiArrivalTimes(pNtk) );
00159
00160
00161 Abc_NtkCleanCopy( pNtk );
00162 Abc_AigConst1(pNtk)->pCopy = (Abc_Obj_t *)Map_ManReadConst1(pMan);
00163 Abc_NtkForEachCi( pNtk, pNode, i )
00164 {
00165 pNodeMap = Map_ManReadInputs(pMan)[i];
00166 pNode->pCopy = (Abc_Obj_t *)pNodeMap;
00167 if ( pSwitching )
00168 Map_NodeSetSwitching( pNodeMap, pSwitching[pNode->Id] );
00169 }
00170
00171
00172 vNodes = Abc_AigDfs( pNtk, 0, 0 );
00173 pProgress = Extra_ProgressBarStart( stdout, vNodes->nSize );
00174 Vec_PtrForEachEntry( vNodes, pNode, i )
00175 {
00176 Extra_ProgressBarUpdate( pProgress, i, NULL );
00177
00178 pNodeMap = Map_NodeAnd( pMan,
00179 Map_NotCond( Abc_ObjFanin0(pNode)->pCopy, Abc_ObjFaninC0(pNode) ),
00180 Map_NotCond( Abc_ObjFanin1(pNode)->pCopy, Abc_ObjFaninC1(pNode) ) );
00181 assert( pNode->pCopy == NULL );
00182
00183 pNode->pCopy = (Abc_Obj_t *)pNodeMap;
00184 if ( pSwitching )
00185 Map_NodeSetSwitching( pNodeMap, pSwitching[pNode->Id] );
00186
00187 if ( Abc_AigNodeIsChoice( pNode ) )
00188 for ( pPrev = pNode, pFanin = pNode->pData; pFanin; pPrev = pFanin, pFanin = pFanin->pData )
00189 {
00190 Map_NodeSetNextE( (Map_Node_t *)pPrev->pCopy, (Map_Node_t *)pFanin->pCopy );
00191 Map_NodeSetRepr( (Map_Node_t *)pFanin->pCopy, (Map_Node_t *)pNode->pCopy );
00192 }
00193 }
00194 Extra_ProgressBarStop( pProgress );
00195 Vec_PtrFree( vNodes );
00196
00197
00198 Abc_NtkForEachCo( pNtk, pNode, i )
00199 Map_ManReadOutputs(pMan)[i] = Map_NotCond( (Map_Node_t *)Abc_ObjFanin0(pNode)->pCopy, Abc_ObjFaninC0(pNode) );
00200 return pMan;
00201 }
00202
00214 Abc_Ntk_t * Abc_NtkFromMap( Map_Man_t * pMan, Abc_Ntk_t * pNtk )
00215 {
00216 ProgressBar * pProgress;
00217 Abc_Ntk_t * pNtkNew;
00218 Map_Node_t * pNodeMap;
00219 Abc_Obj_t * pNode, * pNodeNew;
00220 int i, nDupGates;
00221
00222 pNtkNew = Abc_NtkStartFrom( pNtk, ABC_NTK_LOGIC, ABC_FUNC_MAP );
00223
00224 Map_ManCleanData( pMan );
00225 Abc_NtkForEachCi( pNtk, pNode, i )
00226 Map_NodeSetData( Map_ManReadInputs(pMan)[i], 1, (char *)pNode->pCopy );
00227
00228 pProgress = Extra_ProgressBarStart( stdout, Abc_NtkCoNum(pNtk) );
00229 Abc_NtkForEachCo( pNtk, pNode, i )
00230 {
00231 Extra_ProgressBarUpdate( pProgress, i, NULL );
00232 pNodeMap = Map_ManReadOutputs(pMan)[i];
00233 pNodeNew = Abc_NodeFromMap_rec( pNtkNew, Map_Regular(pNodeMap), !Map_IsComplement(pNodeMap) );
00234 assert( !Abc_ObjIsComplement(pNodeNew) );
00235 Abc_ObjAddFanin( pNode->pCopy, pNodeNew );
00236 }
00237 Extra_ProgressBarStop( pProgress );
00238
00239 nDupGates = Abc_NtkLogicMakeSimpleCos( pNtkNew, 1 );
00240
00241
00242 return pNtkNew;
00243 }
00244
00256 Abc_Obj_t * Abc_NodeFromMap_rec( Abc_Ntk_t * pNtkNew, Map_Node_t * pNodeMap, int fPhase )
00257 {
00258 Abc_Obj_t * pNodeNew, * pNodeInv;
00259
00260
00261 if ( Map_NodeIsConst(pNodeMap) )
00262 return fPhase? Abc_NtkCreateNodeConst1(pNtkNew) : Abc_NtkCreateNodeConst0(pNtkNew);
00263
00264
00265 pNodeNew = (Abc_Obj_t *)Map_NodeReadData( pNodeMap, fPhase );
00266 if ( pNodeNew )
00267 return pNodeNew;
00268
00269
00270 if ( Map_NodeReadCutBest(pNodeMap, fPhase) != NULL )
00271 return Abc_NodeFromMapPhase_rec( pNtkNew, pNodeMap, fPhase );
00272
00273
00274 assert( Map_NodeReadCutBest(pNodeMap, !fPhase) != NULL || Map_NodeIsConst(pNodeMap) );
00275 pNodeNew = Abc_NodeFromMapPhase_rec( pNtkNew, pNodeMap, !fPhase );
00276
00277
00278 pNodeInv = Abc_NtkCreateNode( pNtkNew );
00279 Abc_ObjAddFanin( pNodeInv, pNodeNew );
00280 pNodeInv->pData = Mio_LibraryReadInv(Map_ManReadGenLib(Map_NodeReadMan(pNodeMap)));
00281
00282
00283 Map_NodeSetData( pNodeMap, fPhase, (char *)pNodeInv );
00284 return pNodeInv;
00285 }
00286
00298 Abc_Obj_t * Abc_NodeFromMapPhase_rec( Abc_Ntk_t * pNtkNew, Map_Node_t * pNodeMap, int fPhase )
00299 {
00300 Abc_Obj_t * pNodePIs[10];
00301 Abc_Obj_t * pNodeNew;
00302 Map_Node_t ** ppLeaves;
00303 Map_Cut_t * pCutBest;
00304 Map_Super_t * pSuperBest;
00305 unsigned uPhaseBest;
00306 int i, fInvPin, nLeaves;
00307
00308
00309 assert( Map_NodeReadCutBest(pNodeMap, fPhase) != NULL || Map_NodeIsConst(pNodeMap) );
00310
00311 pNodeNew = (Abc_Obj_t *)Map_NodeReadData( pNodeMap, fPhase );
00312 if ( pNodeNew )
00313 return pNodeNew;
00314
00315
00316 pCutBest = Map_NodeReadCutBest( pNodeMap, fPhase );
00317 pSuperBest = Map_CutReadSuperBest( pCutBest, fPhase );
00318 uPhaseBest = Map_CutReadPhaseBest( pCutBest, fPhase );
00319 nLeaves = Map_CutReadLeavesNum( pCutBest );
00320 ppLeaves = Map_CutReadLeaves( pCutBest );
00321
00322
00323 for ( i = 0; i < nLeaves; i++ )
00324 {
00325 fInvPin = ((uPhaseBest & (1 << i)) > 0);
00326 pNodePIs[i] = Abc_NodeFromMap_rec( pNtkNew, ppLeaves[i], !fInvPin );
00327 assert( pNodePIs[i] != NULL );
00328 }
00329
00330
00331 pNodeNew = Abc_NodeFromMapSuper_rec( pNtkNew, pNodeMap, pSuperBest, pNodePIs, nLeaves );
00332 Map_NodeSetData( pNodeMap, fPhase, (char *)pNodeNew );
00333 return pNodeNew;
00334 }
00335
00347 Abc_Obj_t * Abc_NodeFromMapSuper_rec( Abc_Ntk_t * pNtkNew, Map_Node_t * pNodeMap, Map_Super_t * pSuper, Abc_Obj_t * pNodePis[], int nNodePis )
00348 {
00349 Mio_Gate_t * pRoot;
00350 Map_Super_t ** ppFanins;
00351 Abc_Obj_t * pNodeNew, * pNodeFanin;
00352 int nFanins, Number, i;
00353
00354
00355 pRoot = Map_SuperReadRoot(pSuper);
00356 if ( pRoot == NULL )
00357 {
00358 Number = Map_SuperReadNum(pSuper);
00359 if ( Number < nNodePis )
00360 {
00361 return pNodePis[Number];
00362 }
00363 else
00364 {
00365
00366
00367
00368
00369
00370
00371
00372 return Abc_NtkCreateNodeConst0(pNtkNew);
00373 }
00374 }
00375
00376
00377 nFanins = Map_SuperReadFaninNum( pSuper );
00378 ppFanins = Map_SuperReadFanins( pSuper );
00379
00380 pNodeNew = Abc_NtkCreateNode( pNtkNew );
00381 for ( i = 0; i < nFanins; i++ )
00382 {
00383 pNodeFanin = Abc_NodeFromMapSuper_rec( pNtkNew, pNodeMap, ppFanins[i], pNodePis, nNodePis );
00384 Abc_ObjAddFanin( pNodeNew, pNodeFanin );
00385 }
00386 pNodeNew->pData = pRoot;
00387 return pNodeNew;
00388 }
00389
00390
00391
00392
00393
00405 Abc_Ntk_t * Abc_NtkSuperChoice( Abc_Ntk_t * pNtk )
00406 {
00407 Abc_Ntk_t * pNtkNew;
00408
00409 Map_Man_t * pMan;
00410
00411 assert( Abc_NtkIsStrash(pNtk) );
00412
00413
00414 if ( Abc_FrameReadLibGen() == NULL )
00415 {
00416 printf( "The current library is not available.\n" );
00417 return 0;
00418 }
00419
00420
00421 if ( Abc_FrameReadLibSuper() == NULL && Abc_FrameReadLibGen() )
00422 {
00423 printf( "A simple supergate library is derived from gate library \"%s\".\n",
00424 Mio_LibraryReadName(Abc_FrameReadLibGen()) );
00425 Map_SuperLibDeriveFromGenlib( Abc_FrameReadLibGen() );
00426 }
00427
00428
00429 if ( Abc_NtkGetChoiceNum( pNtk ) )
00430 printf( "Performing mapping with choices.\n" );
00431
00432
00433 pMan = Abc_NtkToMap( pNtk, -1, 1, NULL, 0 );
00434 if ( pMan == NULL )
00435 return NULL;
00436 if ( !Map_Mapping( pMan ) )
00437 {
00438 Map_ManFree( pMan );
00439 return NULL;
00440 }
00441
00442
00443 pNtkNew = Abc_NtkFromMapSuperChoice( pMan, pNtk );
00444 if ( pNtkNew == NULL )
00445 return NULL;
00446 Map_ManFree( pMan );
00447
00448
00449 if ( !Abc_NtkCheck( pNtkNew ) )
00450 {
00451 printf( "Abc_NtkMap: The network check has failed.\n" );
00452 Abc_NtkDelete( pNtkNew );
00453 return NULL;
00454 }
00455 return pNtkNew;
00456 }
00457
00458
00470 Abc_Ntk_t * Abc_NtkFromMapSuperChoice( Map_Man_t * pMan, Abc_Ntk_t * pNtk )
00471 {
00472 extern Abc_Ntk_t * Abc_NtkMulti( Abc_Ntk_t * pNtk, int nThresh, int nFaninMax, int fCnf, int fMulti, int fSimple, int fFactor );
00473 ProgressBar * pProgress;
00474 Abc_Ntk_t * pNtkNew, * pNtkNew2;
00475 Abc_Obj_t * pNode;
00476 int i;
00477
00478
00479 Abc_NtkForEachCi( pNtk, pNode, i )
00480 pNode->pNext = pNode->pCopy;
00481 Abc_NtkForEachPo( pNtk, pNode, i )
00482 pNode->pNext = pNode->pCopy;
00483 Abc_NtkForEachNode( pNtk, pNode, i )
00484 pNode->pNext = pNode->pCopy;
00485
00486
00487 pNtkNew2 = Abc_NtkDup( pNtk );
00488 pNtkNew = Abc_NtkMulti( pNtkNew2, 0, 20, 0, 0, 1, 0 );
00489 if ( !Abc_NtkBddToSop( pNtkNew, 0 ) )
00490 {
00491 printf( "Abc_NtkFromMapSuperChoice(): Converting to SOPs has failed.\n" );
00492 return NULL;
00493 }
00494
00495
00496 Abc_NtkForEachCi( pNtk, pNode, i )
00497 pNode->pCopy = pNode->pCopy->pCopy;
00498 Abc_NtkForEachPo( pNtk, pNode, i )
00499 pNode->pCopy = pNode->pCopy->pCopy;
00500 Abc_NtkForEachNode( pNtk, pNode, i )
00501 pNode->pCopy = pNode->pCopy->pCopy;
00502 Abc_NtkDelete( pNtkNew2 );
00503
00504
00505 Abc_NtkForEachCi( pNtk, pNode, i )
00506 {
00507 Map_NodeSetData( Map_ManReadInputs(pMan)[i], 0, (char *)Abc_NtkCreateNodeInv(pNtkNew,pNode->pCopy) );
00508 Map_NodeSetData( Map_ManReadInputs(pMan)[i], 1, (char *)pNode->pCopy );
00509 }
00510 Abc_NtkForEachNode( pNtk, pNode, i )
00511 {
00512
00513
00514 Map_NodeSetData( (Map_Node_t *)pNode->pNext, 0, (char *)Abc_NtkCreateNodeInv(pNtkNew,pNode->pCopy) );
00515 Map_NodeSetData( (Map_Node_t *)pNode->pNext, 1, (char *)pNode->pCopy );
00516 }
00517
00518
00519 pProgress = Extra_ProgressBarStart( stdout, Abc_NtkObjNumMax(pNtk) );
00520 Abc_NtkForEachNode( pNtk, pNode, i )
00521 {
00522 Extra_ProgressBarUpdate( pProgress, i, NULL );
00523
00524
00525 Abc_NodeSuperChoice( pNtkNew, pNode );
00526 }
00527 Extra_ProgressBarStop( pProgress );
00528 return pNtkNew;
00529 }
00530
00531
00543 void Abc_NodeSuperChoice( Abc_Ntk_t * pNtkNew, Abc_Obj_t * pNode )
00544 {
00545 Map_Node_t * pMapNode = (Map_Node_t *)pNode->pNext;
00546 Map_Cut_t * pCuts, * pTemp;
00547
00548 pCuts = Map_NodeReadCuts(pMapNode);
00549 for ( pTemp = Map_CutReadNext(pCuts); pTemp; pTemp = Map_CutReadNext(pTemp) )
00550 {
00551 Abc_NodeFromMapCutPhase( pNtkNew, pTemp, 0 );
00552 Abc_NodeFromMapCutPhase( pNtkNew, pTemp, 1 );
00553 }
00554 }
00555
00556
00568 void Abc_NodeFromMapCutPhase( Abc_Ntk_t * pNtkNew, Map_Cut_t * pCut, int fPhase )
00569 {
00570 Abc_Obj_t * pNodePIs[10];
00571 Map_Node_t ** ppLeaves;
00572 Map_Super_t * pSuperBest;
00573 unsigned uPhaseBest;
00574 int i, fInvPin, nLeaves;
00575
00576 pSuperBest = Map_CutReadSuperBest( pCut, fPhase );
00577 if ( pSuperBest == NULL )
00578 return;
00579
00580
00581 uPhaseBest = Map_CutReadPhaseBest( pCut, fPhase );
00582 nLeaves = Map_CutReadLeavesNum( pCut );
00583 ppLeaves = Map_CutReadLeaves( pCut );
00584
00585
00586 for ( i = 0; i < nLeaves; i++ )
00587 {
00588 fInvPin = ((uPhaseBest & (1 << i)) > 0);
00589 pNodePIs[i] = (Abc_Obj_t *)Map_NodeReadData( ppLeaves[i], !fInvPin );
00590 assert( pNodePIs[i] != NULL );
00591 }
00592
00593
00594 Abc_NodeFromMapSuperChoice_rec( pNtkNew, pSuperBest, pNodePIs, nLeaves );
00595 }
00596
00597
00609 Abc_Obj_t * Abc_NodeFromMapSuperChoice_rec( Abc_Ntk_t * pNtkNew, Map_Super_t * pSuper, Abc_Obj_t * pNodePis[], int nNodePis )
00610 {
00611 Mio_Gate_t * pRoot;
00612 Map_Super_t ** ppFanins;
00613 Abc_Obj_t * pNodeNew, * pNodeFanin;
00614 int nFanins, Number, i;
00615
00616
00617 pRoot = Map_SuperReadRoot(pSuper);
00618 if ( pRoot == NULL )
00619 {
00620 Number = Map_SuperReadNum(pSuper);
00621 if ( Number < nNodePis )
00622 {
00623 return pNodePis[Number];
00624 }
00625 else
00626 {
00627
00628
00629
00630
00631
00632
00633
00634 return Abc_NtkCreateNodeConst0(pNtkNew);
00635 }
00636 }
00637
00638
00639 nFanins = Map_SuperReadFaninNum( pSuper );
00640 ppFanins = Map_SuperReadFanins( pSuper );
00641
00642 pNodeNew = Abc_NtkCreateNode( pNtkNew );
00643 for ( i = 0; i < nFanins; i++ )
00644 {
00645 pNodeFanin = Abc_NodeFromMapSuperChoice_rec( pNtkNew, ppFanins[i], pNodePis, nNodePis );
00646 Abc_ObjAddFanin( pNodeNew, pNodeFanin );
00647 }
00648 pNodeNew->pData = Abc_SopRegister( pNtkNew->pManFunc, Mio_GateReadSop(pRoot) );
00649 return pNodeNew;
00650 }
00651
00652
00656
00657