00001
00021 #include "if.h"
00022
00026
00027 static If_Obj_t * If_ManSetupObj( If_Man_t * p );
00028
00029 static void If_ManCutSetRecycle( If_Man_t * p, If_Set_t * pSet ) { pSet->pNext = p->pFreeList; p->pFreeList = pSet; }
00030 static If_Set_t * If_ManCutSetFetch( If_Man_t * p ) { If_Set_t * pTemp = p->pFreeList; p->pFreeList = p->pFreeList->pNext; return pTemp; }
00031
00035
00047 If_Man_t * If_ManStart( If_Par_t * pPars )
00048 {
00049 If_Man_t * p;
00050
00051 p = ALLOC( If_Man_t, 1 );
00052 memset( p, 0, sizeof(If_Man_t) );
00053 p->pPars = pPars;
00054 p->fEpsilon = (float)0.001;
00055
00056 p->vCis = Vec_PtrAlloc( 100 );
00057 p->vCos = Vec_PtrAlloc( 100 );
00058 p->vObjs = Vec_PtrAlloc( 100 );
00059 p->vMapped = Vec_PtrAlloc( 100 );
00060 p->vTemp = Vec_PtrAlloc( 100 );
00061
00062 p->nTruthWords = p->pPars->fTruth? If_CutTruthWords( p->pPars->nLutSize ) : 0;
00063 p->nPermWords = p->pPars->fUsePerm? If_CutPermWords( p->pPars->nLutSize ) : 0;
00064 p->nObjBytes = sizeof(If_Obj_t) + sizeof(int) * (p->pPars->nLutSize + p->nPermWords + p->nTruthWords);
00065 p->nCutBytes = sizeof(If_Cut_t) + sizeof(int) * (p->pPars->nLutSize + p->nPermWords + p->nTruthWords);
00066 p->nSetBytes = sizeof(If_Set_t) + (sizeof(If_Cut_t *) + p->nCutBytes) * (p->pPars->nCutsMax + 1);
00067 p->pMemObj = Mem_FixedStart( p->nObjBytes );
00068
00069
00070 if ( p->pPars->fVerbose )
00071 printf( "Memory (bytes): Truth = %4d. Cut = %4d. Obj = %4d. Set = %4d.\n",
00072 4 * p->nTruthWords, p->nCutBytes, p->nObjBytes, p->nSetBytes );
00073
00074 p->puTemp[0] = p->pPars->fTruth? ALLOC( unsigned, 4 * p->nTruthWords ) : NULL;
00075 p->puTemp[1] = p->puTemp[0] + p->nTruthWords;
00076 p->puTemp[2] = p->puTemp[1] + p->nTruthWords;
00077 p->puTemp[3] = p->puTemp[2] + p->nTruthWords;
00078
00079 p->pConst1 = If_ManSetupObj( p );
00080 p->pConst1->Type = IF_CONST1;
00081 p->pConst1->fPhase = 1;
00082 p->nObjs[IF_CONST1]++;
00083 return p;
00084 }
00085
00097 void If_ManRestart( If_Man_t * p )
00098 {
00099 FREE( p->pMemCi );
00100 Vec_PtrClear( p->vCis );
00101 Vec_PtrClear( p->vCos );
00102 Vec_PtrClear( p->vObjs );
00103 Vec_PtrClear( p->vMapped );
00104 Vec_PtrClear( p->vTemp );
00105 Mem_FixedRestart( p->pMemObj );
00106
00107 p->pConst1 = If_ManSetupObj( p );
00108 p->pConst1->Type = IF_CONST1;
00109 p->pConst1->fPhase = 1;
00110
00111 p->nObjs[IF_CI] = p->nObjs[IF_CO] = p->nObjs[IF_AND] = 0;
00112 }
00113
00125 void If_ManStop( If_Man_t * p )
00126 {
00127 Vec_PtrFree( p->vCis );
00128 Vec_PtrFree( p->vCos );
00129 Vec_PtrFree( p->vObjs );
00130 Vec_PtrFree( p->vMapped );
00131 Vec_PtrFree( p->vTemp );
00132 if ( p->vLatchOrder ) Vec_PtrFree( p->vLatchOrder );
00133 if ( p->vLags ) Vec_IntFree( p->vLags );
00134 Mem_FixedStop( p->pMemObj, 0 );
00135 FREE( p->pMemCi );
00136 FREE( p->pMemAnd );
00137 FREE( p->puTemp[0] );
00138
00139 if ( p->pPars->pTimesArr )
00140 FREE( p->pPars->pTimesArr );
00141 if ( p->pPars->pTimesReq )
00142 FREE( p->pPars->pTimesReq );
00143 free( p );
00144 }
00145
00157 If_Obj_t * If_ManCreateCi( If_Man_t * p )
00158 {
00159 If_Obj_t * pObj;
00160 pObj = If_ManSetupObj( p );
00161 pObj->Type = IF_CI;
00162 Vec_PtrPush( p->vCis, pObj );
00163 p->nObjs[IF_CI]++;
00164 return pObj;
00165 }
00166
00178 If_Obj_t * If_ManCreateCo( If_Man_t * p, If_Obj_t * pDriver )
00179 {
00180 If_Obj_t * pObj;
00181 pObj = If_ManSetupObj( p );
00182 Vec_PtrPush( p->vCos, pObj );
00183 pObj->Type = IF_CO;
00184 pObj->fCompl0 = If_IsComplement(pDriver); pDriver = If_Regular(pDriver);
00185 pObj->pFanin0 = pDriver; pDriver->nRefs++;
00186 p->nObjs[IF_CO]++;
00187 return pObj;
00188 }
00189
00201 If_Obj_t * If_ManCreateAnd( If_Man_t * p, If_Obj_t * pFan0, If_Obj_t * pFan1 )
00202 {
00203 If_Obj_t * pObj;
00204
00205 if ( pFan0 == pFan1 )
00206 return pFan0;
00207 if ( pFan0 == If_Not(pFan1) )
00208 return If_Not(p->pConst1);
00209 if ( If_Regular(pFan0) == p->pConst1 )
00210 return pFan0 == p->pConst1 ? pFan1 : If_Not(p->pConst1);
00211 if ( If_Regular(pFan1) == p->pConst1 )
00212 return pFan1 == p->pConst1 ? pFan0 : If_Not(p->pConst1);
00213
00214 pObj = If_ManSetupObj( p );
00215 pObj->Type = IF_AND;
00216 pObj->fCompl0 = If_IsComplement(pFan0); pFan0 = If_Regular(pFan0);
00217 pObj->fCompl1 = If_IsComplement(pFan1); pFan1 = If_Regular(pFan1);
00218 pObj->pFanin0 = pFan0; pFan0->nRefs++; pFan0->nVisits++; pFan0->nVisitsCopy++;
00219 pObj->pFanin1 = pFan1; pFan1->nRefs++; pFan1->nVisits++; pFan1->nVisitsCopy++;
00220 pObj->fPhase = (pObj->fCompl0 ^ pFan0->fPhase) & (pObj->fCompl1 ^ pFan1->fPhase);
00221 pObj->Level = 1 + IF_MAX( pFan0->Level, pFan1->Level );
00222 if ( p->nLevelMax < (int)pObj->Level )
00223 p->nLevelMax = (int)pObj->Level;
00224 p->nObjs[IF_AND]++;
00225 return pObj;
00226 }
00227
00239 If_Obj_t * If_ManCreateXor( If_Man_t * p, If_Obj_t * pFan0, If_Obj_t * pFan1 )
00240 {
00241 If_Obj_t * pRes1, * pRes2;
00242 pRes1 = If_ManCreateAnd( p, If_Not(pFan0), pFan1 );
00243 pRes2 = If_ManCreateAnd( p, pFan0, If_Not(pFan1) );
00244 return If_Not( If_ManCreateAnd( p, If_Not(pRes1), If_Not(pRes2) ) );
00245 }
00246
00258 If_Obj_t * If_ManCreateMux( If_Man_t * p, If_Obj_t * pFan0, If_Obj_t * pFan1, If_Obj_t * pCtrl )
00259 {
00260 If_Obj_t * pRes1, * pRes2;
00261 pRes1 = If_ManCreateAnd( p, pFan0, If_Not(pCtrl) );
00262 pRes2 = If_ManCreateAnd( p, pFan1, pCtrl );
00263 return If_Not( If_ManCreateAnd( p, If_Not(pRes1), If_Not(pRes2) ) );
00264 }
00265
00277 void If_ManCreateChoice( If_Man_t * p, If_Obj_t * pObj )
00278 {
00279 If_Obj_t * pTemp;
00280
00281 assert( pObj->fRepr == 0 );
00282 pObj->fRepr = 1;
00283
00284 for ( pTemp = pObj; pTemp; pTemp = pTemp->pEquiv )
00285 {
00286 pObj->Level = IF_MAX( pObj->Level, pTemp->Level );
00287 pTemp->nVisits++; pTemp->nVisitsCopy++;
00288 }
00289
00290 if ( p->nLevelMax < (int)pObj->Level )
00291 p->nLevelMax = (int)pObj->Level;
00292 }
00293
00305 void If_ManSetupCut( If_Man_t * p, If_Cut_t * pCut )
00306 {
00307 memset( pCut, 0, sizeof(If_Cut_t) );
00308 pCut->nLimit = p->pPars->nLutSize;
00309 pCut->pLeaves = (int *)(pCut + 1);
00310 if ( p->pPars->fUsePerm )
00311 pCut->pPerm = (char *)(pCut->pLeaves + p->pPars->nLutSize);
00312 if ( p->pPars->fTruth )
00313 pCut->pTruth = pCut->pLeaves + p->pPars->nLutSize + p->nPermWords;
00314 }
00315
00327 void If_ManSetupSet( If_Man_t * p, If_Set_t * pSet )
00328 {
00329 char * pArray;
00330 int i;
00331 pSet->nCuts = 0;
00332 pSet->nCutsMax = p->pPars->nCutsMax;
00333 pSet->ppCuts = (If_Cut_t **)(pSet + 1);
00334 pArray = (char *)pSet->ppCuts + sizeof(If_Cut_t *) * (pSet->nCutsMax+1);
00335 for ( i = 0; i <= pSet->nCutsMax; i++ )
00336 {
00337 pSet->ppCuts[i] = (If_Cut_t *)(pArray + i * p->nCutBytes);
00338 If_ManSetupCut( p, pSet->ppCuts[i] );
00339 }
00340
00341
00342 }
00343
00355 void If_ManSetupCutTriv( If_Man_t * p, If_Cut_t * pCut, int ObjId )
00356 {
00357 pCut->fCompl = 0;
00358 pCut->nLimit = p->pPars->nLutSize;
00359 pCut->nLeaves = 1;
00360 pCut->pLeaves[0] = p->pPars->fLiftLeaves? (ObjId << 8) : ObjId;
00361 pCut->uSign = If_ObjCutSign( pCut->pLeaves[0] );
00362
00363 if ( p->pPars->fTruth )
00364 {
00365 int i, nTruthWords;
00366 nTruthWords = pCut->nLimit <= 5 ? 1 : (1 << (pCut->nLimit - 5));
00367 for ( i = 0; i < nTruthWords; i++ )
00368 If_CutTruth(pCut)[i] = 0xAAAAAAAA;
00369 }
00370 }
00371
00383 If_Obj_t * If_ManSetupObj( If_Man_t * p )
00384 {
00385 If_Obj_t * pObj;
00386
00387 pObj = (If_Obj_t *)Mem_FixedEntryFetch( p->pMemObj );
00388 memset( pObj, 0, sizeof(If_Obj_t) );
00389 If_ManSetupCut( p, &pObj->CutBest );
00390
00391 pObj->Id = Vec_PtrSize(p->vObjs);
00392 Vec_PtrPush( p->vObjs, pObj );
00393
00394 pObj->Required = IF_FLOAT_LARGE;
00395 return pObj;
00396 }
00397
00409 void If_ManSetupCiCutSets( If_Man_t * p )
00410 {
00411 If_Obj_t * pObj;
00412 int i;
00413 assert( p->pMemCi == NULL );
00414
00415 If_ManForEachCi( p, pObj, i )
00416 If_ManSetupCutTriv( p, &pObj->CutBest, pObj->Id );
00417
00418 p->pMemCi = (If_Set_t *)malloc( If_ManCiNum(p) * (sizeof(If_Set_t) + sizeof(void *)) );
00419 If_ManForEachCi( p, pObj, i )
00420 {
00421 pObj->pCutSet = (If_Set_t *)((char *)p->pMemCi + i * (sizeof(If_Set_t) + sizeof(void *)));
00422 pObj->pCutSet->nCuts = 1;
00423 pObj->pCutSet->nCutsMax = p->pPars->nCutsMax;
00424 pObj->pCutSet->ppCuts = (If_Cut_t **)(pObj->pCutSet + 1);
00425 pObj->pCutSet->ppCuts[0] = &pObj->CutBest;
00426 }
00427 }
00428
00440 If_Set_t * If_ManSetupNodeCutSet( If_Man_t * p, If_Obj_t * pObj )
00441 {
00442 assert( If_ObjIsAnd(pObj) );
00443 assert( pObj->pCutSet == NULL );
00444
00445
00446
00447 pObj->pCutSet = If_ManCutSetFetch( p );
00448 pObj->pCutSet->nCuts = 0;
00449 pObj->pCutSet->nCutsMax = p->pPars->nCutsMax;
00450
00451 return pObj->pCutSet;
00452 }
00453
00465 void If_ManDerefNodeCutSet( If_Man_t * p, If_Obj_t * pObj )
00466 {
00467 If_Obj_t * pFanin;
00468 assert( If_ObjIsAnd(pObj) );
00469
00470 assert( pObj->nVisits >= 0 );
00471 if ( pObj->nVisits == 0 )
00472 {
00473
00474 If_ManCutSetRecycle( p, pObj->pCutSet );
00475 pObj->pCutSet = NULL;
00476 }
00477
00478 pFanin = If_ObjFanin0(pObj);
00479 assert( pFanin->nVisits > 0 );
00480 if ( !If_ObjIsCi(pFanin) && --pFanin->nVisits == 0 )
00481 {
00482
00483 If_ManCutSetRecycle( p, pFanin->pCutSet );
00484 pFanin->pCutSet = NULL;
00485 }
00486
00487 pFanin = If_ObjFanin1(pObj);
00488 assert( pFanin->nVisits > 0 );
00489 if ( !If_ObjIsCi(pFanin) && --pFanin->nVisits == 0 )
00490 {
00491
00492 If_ManCutSetRecycle( p, pFanin->pCutSet );
00493 pFanin->pCutSet = NULL;
00494 }
00495 }
00496
00508 void If_ManDerefChoiceCutSet( If_Man_t * p, If_Obj_t * pObj )
00509 {
00510 If_Obj_t * pTemp;
00511 assert( If_ObjIsAnd(pObj) );
00512 assert( pObj->fRepr );
00513 assert( pObj->nVisits > 0 );
00514
00515 for ( pTemp = pObj; pTemp; pTemp = pTemp->pEquiv )
00516 {
00517 assert( pTemp == pObj || pTemp->nVisits == 1 );
00518 if ( --pTemp->nVisits == 0 )
00519 {
00520
00521 If_ManCutSetRecycle( p, pTemp->pCutSet );
00522 pTemp->pCutSet = NULL;
00523 }
00524 }
00525 }
00526
00538 void If_ManSetupSetAll( If_Man_t * p, int nCrossCut )
00539 {
00540 If_Set_t * pCutSet;
00541 int i, nCutSets;
00542 nCutSets = 128 + nCrossCut;
00543 p->pFreeList = p->pMemAnd = pCutSet = (If_Set_t *)malloc( nCutSets * p->nSetBytes );
00544 for ( i = 0; i < nCutSets; i++ )
00545 {
00546 If_ManSetupSet( p, pCutSet );
00547 if ( i == nCutSets - 1 )
00548 pCutSet->pNext = NULL;
00549 else
00550 pCutSet->pNext = (If_Set_t *)( (char *)pCutSet + p->nSetBytes );
00551 pCutSet = pCutSet->pNext;
00552 }
00553 assert( pCutSet == NULL );
00554
00555 if ( p->pPars->fVerbose )
00556 {
00557 printf( "Node = %7d. Ch = %5d. Total mem = %7.2f Mb. Peak cut mem = %7.2f Mb.\n",
00558 If_ManAndNum(p), p->nChoices,
00559 1.0 * (p->nObjBytes + 2*sizeof(void *)) * If_ManObjNum(p) / (1<<20),
00560 1.0 * p->nSetBytes * nCrossCut / (1<<20) );
00561 }
00562
00563
00564 }
00565
00569
00570