00001
00019 #include "mapperInt.h"
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00035
00036 static int Map_MatchNodePhase( Map_Man_t * p, Map_Node_t * pNode, int fPhase );
00037 static int Map_MatchNodeCut( Map_Man_t * p, Map_Node_t * pNode, Map_Cut_t * pCut, int fPhase, float fWorstLimit );
00038
00039 static void Map_MappingSetPiArrivalTimes( Map_Man_t * p );
00040 static void Map_NodeTryDroppingOnePhase( Map_Man_t * p, Map_Node_t * pNode );
00041 static void Map_NodeTransferArrivalTimes( Map_Man_t * p, Map_Node_t * pNode );
00042
00046
00062 int Map_MappingMatches( Map_Man_t * p )
00063 {
00064 ProgressBar * pProgress;
00065 Map_Node_t * pNode;
00066 int i;
00067
00068 assert( p->fMappingMode >= 0 && p->fMappingMode <= 4 );
00069
00070
00071 if ( p->fMappingMode == 0 )
00072 Map_MappingSetPiArrivalTimes( p );
00073
00074
00075 if ( p->fMappingMode == 0 )
00076 Map_MappingEstimateRefsInit( p );
00077 else if ( p->fMappingMode == 1 )
00078 Map_MappingEstimateRefs( p );
00079
00080
00081
00082 pProgress = Extra_ProgressBarStart( stdout, p->vAnds->nSize );
00083 for ( i = 0; i < p->vAnds->nSize; i++ )
00084 {
00085
00086 pNode = p->vAnds->pArray[i];
00087 if ( !Map_NodeIsAnd( pNode ) || pNode->pRepr )
00088 continue;
00089
00090
00091 if ( pNode->pCuts->pNext == NULL )
00092 {
00093 printf( "\nError: A node in the mapping graph does not have feasible cuts.\n" );
00094 return 0;
00095 }
00096
00097
00098 if ( !Map_MatchNodePhase( p, pNode, 0 ) )
00099 return 0;
00100
00101 if ( !Map_MatchNodePhase( p, pNode, 1 ) )
00102 return 0;
00103
00104
00105 if ( pNode->pCutBest[0] == NULL && pNode->pCutBest[1] == NULL )
00106 {
00107 printf( "\nError: Could not match both phases of AIG node %d.\n", pNode->Num );
00108 printf( "Please make sure that the supergate library has equivalents of AND2 or NAND2.\n" );
00109 printf( "If such supergates exist in the library, report a bug.\n" );
00110 return 0;
00111 }
00112
00113
00114 Map_NodeTryDroppingOnePhase( p, pNode );
00115
00116 Map_NodeTransferArrivalTimes( p, pNode );
00117
00118
00119 Extra_ProgressBarUpdate( pProgress, i, "Matches ..." );
00120 }
00121 Extra_ProgressBarStop( pProgress );
00122 return 1;
00123 }
00124
00136 int Map_MatchNodePhase( Map_Man_t * p, Map_Node_t * pNode, int fPhase )
00137 {
00138 Map_Match_t MatchBest, * pMatch;
00139 Map_Cut_t * pCut, * pCutBest;
00140 float Area1, Area2, fWorstLimit;
00141
00142
00143 pCutBest = pNode->pCutBest[fPhase];
00144 if ( p->fMappingMode != 0 && pCutBest == NULL )
00145 return 1;
00146
00147
00148
00149
00150 if ( p->fMappingMode != 0 )
00151 {
00152 Map_TimeCutComputeArrival( pNode, pCutBest, fPhase, MAP_FLOAT_LARGE );
00153
00154 assert( pCutBest->M[fPhase].tArrive.Rise < pNode->tRequired[fPhase].Rise + p->fEpsilon );
00155 assert( pCutBest->M[fPhase].tArrive.Fall < pNode->tRequired[fPhase].Fall + p->fEpsilon );
00156 }
00157
00158
00159
00160
00161 if ( p->fMappingMode == 2 || p->fMappingMode == 3 )
00162 {
00163 pMatch = pCutBest->M + fPhase;
00164 if ( pNode->nRefAct[fPhase] > 0 ||
00165 (pNode->pCutBest[!fPhase] == NULL && pNode->nRefAct[!fPhase] > 0) )
00166 pMatch->AreaFlow = Area1 = Map_CutDeref( pCutBest, fPhase );
00167 else
00168 pMatch->AreaFlow = Area1 = Map_CutGetAreaDerefed( pCutBest, fPhase );
00169 }
00170 else if ( p->fMappingMode == 4 )
00171 {
00172 pMatch = pCutBest->M + fPhase;
00173 if ( pNode->nRefAct[fPhase] > 0 ||
00174 (pNode->pCutBest[!fPhase] == NULL && pNode->nRefAct[!fPhase] > 0) )
00175 pMatch->AreaFlow = Area1 = Map_SwitchCutDeref( pNode, pCutBest, fPhase );
00176 else
00177 pMatch->AreaFlow = Area1 = Map_SwitchCutGetDerefed( pNode, pCutBest, fPhase );
00178 }
00179
00180
00181 if ( pCutBest )
00182 MatchBest = pCutBest->M[fPhase];
00183 else
00184 Map_MatchClean( &MatchBest );
00185
00186
00187 fWorstLimit = pNode->tRequired[fPhase].Worst;
00188 for ( pCut = pNode->pCuts->pNext; pCut; pCut = pCut->pNext )
00189 {
00190 pMatch = pCut->M + fPhase;
00191 if ( pMatch->pSupers == NULL )
00192 continue;
00193
00194
00195 Map_MatchNodeCut( p, pNode, pCut, fPhase, fWorstLimit );
00196 if ( pMatch->pSuperBest == NULL || pMatch->tArrive.Worst > fWorstLimit + p->fEpsilon )
00197 continue;
00198
00199
00200 if ( Map_MatchCompare( p, &MatchBest, pMatch, p->fMappingMode ) )
00201 {
00202 pCutBest = pCut;
00203 MatchBest = *pMatch;
00204
00205 if ( p->fMappingMode == 0 )
00206 fWorstLimit = MatchBest.tArrive.Worst;
00207 }
00208 }
00209
00210 if ( pCutBest == NULL )
00211 return 1;
00212
00213
00214 pNode->pCutBest[fPhase] = pCutBest;
00215 pCutBest->M[fPhase] = MatchBest;
00216
00217
00218 if ( p->fMappingMode >= 2 &&
00219 (pNode->nRefAct[fPhase] > 0 ||
00220 (pNode->pCutBest[!fPhase] == NULL && pNode->nRefAct[!fPhase] > 0)) )
00221 {
00222 if ( p->fMappingMode == 2 || p->fMappingMode == 3 )
00223 Area2 = Map_CutRef( pNode->pCutBest[fPhase], fPhase );
00224 else if ( p->fMappingMode == 4 )
00225 Area2 = Map_SwitchCutRef( pNode, pNode->pCutBest[fPhase], fPhase );
00226 else
00227 assert( 0 );
00228 assert( Area2 < Area1 + p->fEpsilon );
00229 }
00230
00231
00232 assert( MatchBest.tArrive.Rise < pNode->tRequired[fPhase].Rise + p->fEpsilon );
00233 assert( MatchBest.tArrive.Fall < pNode->tRequired[fPhase].Fall + p->fEpsilon );
00234 return 1;
00235 }
00236
00252 int Map_MatchNodeCut( Map_Man_t * p, Map_Node_t * pNode, Map_Cut_t * pCut, int fPhase, float fWorstLimit )
00253 {
00254 Map_Match_t MatchBest, * pMatch = pCut->M + fPhase;
00255 Map_Super_t * pSuper;
00256 int i, Counter;
00257
00258
00259 MatchBest = *pMatch;
00260
00261 for ( pSuper = pMatch->pSupers, Counter = 0; pSuper; pSuper = pSuper->pNext, Counter++ )
00262 {
00263 p->nMatches++;
00264
00265
00266
00267 if ( Counter == 30 )
00268 break;
00269
00270
00271 pMatch->pSuperBest = pSuper;
00272 for ( i = 0; i < (int)pSuper->nPhases; i++ )
00273 {
00274 p->nPhases++;
00275
00276 pMatch->uPhaseBest = pMatch->uPhase ^ pSuper->uPhases[i];
00277 if ( p->fMappingMode == 0 )
00278 {
00279
00280 Map_TimeCutComputeArrival( pNode, pCut, fPhase, fWorstLimit );
00281
00282 if ( pMatch->tArrive.Worst > fWorstLimit + p->fEpsilon )
00283 continue;
00284
00285 pMatch->AreaFlow = Map_CutGetAreaFlow( pCut, fPhase );
00286 }
00287 else
00288 {
00289
00290 if ( p->fMappingMode == 2 || p->fMappingMode == 3 )
00291 pMatch->AreaFlow = Map_CutGetAreaDerefed( pCut, fPhase );
00292 else if ( p->fMappingMode == 4 )
00293 pMatch->AreaFlow = Map_SwitchCutGetDerefed( pNode, pCut, fPhase );
00294 else
00295 pMatch->AreaFlow = Map_CutGetAreaFlow( pCut, fPhase );
00296
00297 if ( pMatch->AreaFlow > MatchBest.AreaFlow + p->fEpsilon )
00298 continue;
00299
00300 Map_TimeCutComputeArrival( pNode, pCut, fPhase, fWorstLimit );
00301
00302 if ( pMatch->tArrive.Worst > fWorstLimit + p->fEpsilon )
00303 continue;
00304 }
00305
00306
00307 if ( Map_MatchCompare( p, &MatchBest, pMatch, p->fMappingMode ) )
00308 {
00309 MatchBest = *pMatch;
00310
00311 if ( p->fMappingMode == 0 )
00312 fWorstLimit = MatchBest.tArrive.Worst;
00313 }
00314 }
00315 }
00316
00317 *pMatch = MatchBest;
00318
00319
00320 if ( pMatch->pSuperBest )
00321 {
00322 Map_TimeCutComputeArrival( pNode, pCut, fPhase, MAP_FLOAT_LARGE );
00323 if ( p->fMappingMode == 2 || p->fMappingMode == 3 )
00324 pMatch->AreaFlow = Map_CutGetAreaDerefed( pCut, fPhase );
00325 else if ( p->fMappingMode == 4 )
00326 pMatch->AreaFlow = Map_SwitchCutGetDerefed( pNode, pCut, fPhase );
00327 else
00328 pMatch->AreaFlow = Map_CutGetAreaFlow( pCut, fPhase );
00329 }
00330 return 1;
00331 }
00332
00344 void Map_MatchClean( Map_Match_t * pMatch )
00345 {
00346 memset( pMatch, 0, sizeof(Map_Match_t) );
00347 pMatch->AreaFlow = MAP_FLOAT_LARGE;
00348 pMatch->tArrive.Rise = MAP_FLOAT_LARGE;
00349 pMatch->tArrive.Fall = MAP_FLOAT_LARGE;
00350 pMatch->tArrive.Worst = MAP_FLOAT_LARGE;
00351 }
00352
00364 int Map_MatchCompare( Map_Man_t * pMan, Map_Match_t * pM1, Map_Match_t * pM2, int fDoingArea )
00365 {
00366 if ( !fDoingArea )
00367 {
00368
00369 if ( pM1->tArrive.Worst < pM2->tArrive.Worst - pMan->fEpsilon )
00370 return 0;
00371 if ( pM1->tArrive.Worst > pM2->tArrive.Worst + pMan->fEpsilon )
00372 return 1;
00373
00374 if ( pM1->AreaFlow < pM2->AreaFlow - pMan->fEpsilon )
00375 return 0;
00376 if ( pM1->AreaFlow > pM2->AreaFlow + pMan->fEpsilon )
00377 return 1;
00378
00379 if ( pM1->pSuperBest->nFanLimit > pM2->pSuperBest->nFanLimit )
00380 return 0;
00381 if ( pM1->pSuperBest->nFanLimit < pM2->pSuperBest->nFanLimit )
00382 return 1;
00383
00384 if ( pM1->pSuperBest->nFanins < pM2->pSuperBest->nFanins )
00385 return 0;
00386 if ( pM1->pSuperBest->nFanins > pM2->pSuperBest->nFanins )
00387 return 1;
00388
00389 return 0;
00390 }
00391 else
00392 {
00393
00394 if ( pM1->AreaFlow < pM2->AreaFlow - pMan->fEpsilon )
00395 return 0;
00396 if ( pM1->AreaFlow > pM2->AreaFlow + pMan->fEpsilon )
00397 return 1;
00398
00399 if ( pM1->tArrive.Worst < pM2->tArrive.Worst - pMan->fEpsilon )
00400 return 0;
00401 if ( pM1->tArrive.Worst > pM2->tArrive.Worst + pMan->fEpsilon )
00402 return 1;
00403
00404 if ( pM1->pSuperBest->nFanLimit > pM2->pSuperBest->nFanLimit )
00405 return 0;
00406 if ( pM1->pSuperBest->nFanLimit < pM2->pSuperBest->nFanLimit )
00407 return 1;
00408
00409 if ( pM1->pSuperBest->nFanins < pM2->pSuperBest->nFanins )
00410 return 0;
00411 if ( pM1->pSuperBest->nFanins > pM2->pSuperBest->nFanins )
00412 return 1;
00413
00414 return 0;
00415 }
00416 }
00417
00429 void Map_MappingSetPiArrivalTimes( Map_Man_t * p )
00430 {
00431 Map_Node_t * pNode;
00432 int i;
00433 for ( i = 0; i < p->nInputs; i++ )
00434 {
00435 pNode = p->pInputs[i];
00436
00437 pNode->tArrival[1] = p->pInputArrivals[i];
00438
00439 pNode->tArrival[0].Rise = pNode->tArrival[1].Fall + p->pSuperLib->tDelayInv.Rise;
00440 pNode->tArrival[0].Fall = pNode->tArrival[1].Rise + p->pSuperLib->tDelayInv.Fall;
00441 pNode->tArrival[0].Worst = MAP_MAX(pNode->tArrival[0].Rise, pNode->tArrival[0].Fall);
00442 }
00443 }
00444
00445
00457 void Map_NodeTryDroppingOnePhase( Map_Man_t * p, Map_Node_t * pNode )
00458 {
00459 Map_Match_t * pMatchBest0, * pMatchBest1;
00460 float tWorst0Using1, tWorst1Using0;
00461 int fUsePhase1, fUsePhase0;
00462
00463
00464 if ( pNode->pCutBest[0] == NULL || pNode->pCutBest[1] == NULL )
00465 return;
00466
00467
00468 if ( p->fMappingMode == 1 )
00469 return;
00470
00471
00472 pMatchBest0 = pNode->pCutBest[0]->M + 0;
00473 pMatchBest1 = pNode->pCutBest[1]->M + 1;
00474
00475
00476
00477 tWorst0Using1 = Map_TimeMatchWithInverter( p, pMatchBest1 );
00478 tWorst1Using0 = Map_TimeMatchWithInverter( p, pMatchBest0 );
00479
00480
00481 if ( p->fMappingMode == 0 )
00482 {
00483
00484
00485 if ( pMatchBest0->tArrive.Worst > tWorst0Using1 + p->fEpsilon )
00486 pNode->pCutBest[0] = NULL;
00487 else if ( pMatchBest1->tArrive.Worst > tWorst1Using0 + p->fEpsilon )
00488 pNode->pCutBest[1] = NULL;
00489 return;
00490 }
00491
00492
00493 if ( pNode->nRefAct[0] == 0 || pNode->nRefAct[1] == 0 )
00494 return;
00495
00496
00497 fUsePhase0 = fUsePhase1 = 0;
00498 if ( p->fMappingMode == 2 )
00499 {
00500 fUsePhase0 = (pNode->tRequired[1].Worst > tWorst1Using0 + 3*p->pSuperLib->tDelayInv.Worst + p->fEpsilon);
00501 fUsePhase1 = (pNode->tRequired[0].Worst > tWorst0Using1 + 3*p->pSuperLib->tDelayInv.Worst + p->fEpsilon);
00502 }
00503 else if ( p->fMappingMode == 3 || p->fMappingMode == 4 )
00504 {
00505 fUsePhase0 = (pNode->tRequired[1].Worst > tWorst1Using0 + p->fEpsilon);
00506 fUsePhase1 = (pNode->tRequired[0].Worst > tWorst0Using1 + p->fEpsilon);
00507 }
00508 if ( !fUsePhase0 && !fUsePhase1 )
00509 return;
00510
00511
00512 if ( fUsePhase0 && fUsePhase1 )
00513 {
00514 if ( pMatchBest0->AreaFlow < pMatchBest1->AreaFlow )
00515 fUsePhase1 = 0;
00516 else
00517 fUsePhase0 = 0;
00518 }
00519
00520 assert( fUsePhase0 ^ fUsePhase1 );
00521
00522
00523 if ( fUsePhase0 )
00524 {
00525
00526 if ( p->fMappingMode >= 2 && pNode->nRefAct[1] > 0 )
00527 Map_CutDeref( pNode->pCutBest[1], 1 );
00528
00529 pNode->pCutBest[1] = NULL;
00530
00531 if ( p->fMappingMode >= 2 && pNode->nRefAct[0] == 0 )
00532 Map_CutRef( pNode->pCutBest[0], 0 );
00533 }
00534 else
00535 {
00536
00537 if ( p->fMappingMode >= 2 && pNode->nRefAct[0] > 0 )
00538 Map_CutDeref( pNode->pCutBest[0], 0 );
00539
00540 pNode->pCutBest[0] = NULL;
00541
00542 if ( p->fMappingMode >= 2 && pNode->nRefAct[1] == 0 )
00543 Map_CutRef( pNode->pCutBest[1], 1 );
00544 }
00545 }
00546
00547
00559 void Map_NodeTransferArrivalTimes( Map_Man_t * p, Map_Node_t * pNode )
00560 {
00561
00562 if ( pNode->pCutBest[0] && pNode->pCutBest[1] )
00563 {
00564 pNode->tArrival[0] = pNode->pCutBest[0]->M[0].tArrive;
00565 pNode->tArrival[1] = pNode->pCutBest[1]->M[1].tArrive;
00566 }
00567
00568 else if ( pNode->pCutBest[0] )
00569 {
00570 pNode->tArrival[0] = pNode->pCutBest[0]->M[0].tArrive;
00571 pNode->tArrival[1].Rise = pNode->tArrival[0].Fall + p->pSuperLib->tDelayInv.Rise;
00572 pNode->tArrival[1].Fall = pNode->tArrival[0].Rise + p->pSuperLib->tDelayInv.Fall;
00573 pNode->tArrival[1].Worst = MAP_MAX(pNode->tArrival[1].Rise, pNode->tArrival[1].Fall);
00574 }
00575 else if ( pNode->pCutBest[1] )
00576 {
00577 pNode->tArrival[1] = pNode->pCutBest[1]->M[1].tArrive;
00578 pNode->tArrival[0].Rise = pNode->tArrival[1].Fall + p->pSuperLib->tDelayInv.Rise;
00579 pNode->tArrival[0].Fall = pNode->tArrival[1].Rise + p->pSuperLib->tDelayInv.Fall;
00580 pNode->tArrival[0].Worst = MAP_MAX(pNode->tArrival[0].Rise, pNode->tArrival[0].Fall);
00581 }
00582 else
00583 {
00584 assert( 0 );
00585 }
00586
00587 assert( pNode->tArrival[0].Rise < pNode->tRequired[0].Rise + p->fEpsilon );
00588 assert( pNode->tArrival[0].Fall < pNode->tRequired[0].Fall + p->fEpsilon );
00589
00590 assert( pNode->tArrival[1].Rise < pNode->tRequired[1].Rise + p->fEpsilon );
00591 assert( pNode->tArrival[1].Fall < pNode->tRequired[1].Fall + p->fEpsilon );
00592 }
00593