00001
00021 #include "fra.h"
00022 #include "cnf.h"
00023 #include "dar.h"
00024
00028
00032
00044 void Fra_FraigInductionRewrite( Fra_Man_t * p )
00045 {
00046 Aig_Man_t * pTemp;
00047 Aig_Obj_t * pObj, * pObjPo;
00048 int nTruePis, k, i, clk = clock();
00049
00050 pTemp = Aig_ManDup( p->pManFraig, 0 );
00051
00052 pTemp = Dar_ManRewriteDefault( pTemp );
00053
00054
00055
00056
00057
00058
00059 assert( p->pManFraig->nRegs == pTemp->nRegs );
00060 assert( p->pManFraig->nAsserts == pTemp->nAsserts );
00061 nTruePis = Aig_ManPiNum(p->pManAig) - Aig_ManRegNum(p->pManAig);
00062 memset( p->pMemFraig, 0, sizeof(Aig_Obj_t *) * p->nSizeAlloc * p->nFramesAll );
00063 Fra_ObjSetFraig( Aig_ManConst1(p->pManAig), p->pPars->nFramesK, Aig_ManConst1(pTemp) );
00064 Aig_ManForEachPiSeq( p->pManAig, pObj, i )
00065 Fra_ObjSetFraig( pObj, p->pPars->nFramesK, Aig_ManPi(pTemp,nTruePis*p->pPars->nFramesK+i) );
00066 k = 0;
00067 assert( Aig_ManRegNum(p->pManAig) == Aig_ManPoNum(pTemp) - pTemp->nAsserts );
00068 Aig_ManForEachLoSeq( p->pManAig, pObj, i )
00069 {
00070 pObjPo = Aig_ManPo(pTemp, pTemp->nAsserts + k++);
00071 Fra_ObjSetFraig( pObj, p->pPars->nFramesK, Aig_ObjChild0(pObjPo) );
00072 }
00073
00074 Aig_ManStop( p->pManFraig );
00075 p->pManFraig = pTemp;
00076 p->timeRwr += clock() - clk;
00077 }
00078
00090 static inline void Fra_FramesConstrainNode( Aig_Man_t * pManFraig, Aig_Obj_t * pObj, int iFrame )
00091 {
00092 Aig_Obj_t * pObjNew, * pObjNew2, * pObjRepr, * pObjReprNew, * pMiter;
00093
00094 if ( (pObjRepr = Fra_ClassObjRepr(pObj)) == NULL )
00095 return;
00096 assert( pObjRepr->Id < pObj->Id );
00097
00098 pObjNew = Fra_ObjFraig( pObj, iFrame );
00099
00100 pObjReprNew = Fra_ObjFraig( pObjRepr, iFrame );
00101
00102 if ( Aig_Regular(pObjNew) == Aig_Regular(pObjReprNew) )
00103 return;
00104
00105 pObjNew2 = Aig_NotCond( pObjReprNew, pObj->fPhase ^ pObjRepr->fPhase );
00106
00107 Fra_ObjSetFraig( pObj, iFrame, pObjNew2 );
00108
00109 pMiter = Aig_Exor( pManFraig, Aig_Regular(pObjNew), Aig_Regular(pObjReprNew) );
00110 pMiter = Aig_NotCond( pMiter, Aig_Regular(pMiter)->fPhase ^ Aig_IsComplement(pMiter) );
00111 pMiter = Aig_Not( pMiter );
00112 Aig_ObjCreatePo( pManFraig, pMiter );
00113 }
00114
00126 Aig_Man_t * Fra_FramesWithClasses( Fra_Man_t * p )
00127 {
00128 Aig_Man_t * pManFraig;
00129 Aig_Obj_t * pObj, * pObjLi, * pObjLo, * pObjNew;
00130 int i, k, f;
00131 assert( p->pManFraig == NULL );
00132 assert( Aig_ManRegNum(p->pManAig) > 0 );
00133 assert( Aig_ManRegNum(p->pManAig) < Aig_ManPiNum(p->pManAig) );
00134
00135
00136 pManFraig = Aig_ManStart( Aig_ManObjNumMax(p->pManAig) * p->nFramesAll );
00137 pManFraig->pName = Aig_UtilStrsav( p->pManAig->pName );
00138 pManFraig->nRegs = p->pManAig->nRegs;
00139
00140 for ( f = 0; f < p->nFramesAll; f++ )
00141 Fra_ObjSetFraig( Aig_ManConst1(p->pManAig), f, Aig_ManConst1(pManFraig) );
00142 for ( f = 0; f < p->nFramesAll; f++ )
00143 Aig_ManForEachPiSeq( p->pManAig, pObj, i )
00144 Fra_ObjSetFraig( pObj, f, Aig_ObjCreatePi(pManFraig) );
00145
00146 Aig_ManForEachLoSeq( p->pManAig, pObj, i )
00147 Fra_ObjSetFraig( pObj, 0, Aig_ObjCreatePi(pManFraig) );
00148
00149
00150
00151 for ( f = 0; f < p->nFramesAll - 1; f++ )
00152 {
00153
00154 Aig_ManForEachLoSeq( p->pManAig, pObj, i )
00155 Fra_FramesConstrainNode( pManFraig, pObj, f );
00156
00157 Aig_ManForEachNode( p->pManAig, pObj, i )
00158 {
00159 pObjNew = Aig_And( pManFraig, Fra_ObjChild0Fra(pObj,f), Fra_ObjChild1Fra(pObj,f) );
00160 Fra_ObjSetFraig( pObj, f, pObjNew );
00161 Fra_FramesConstrainNode( pManFraig, pObj, f );
00162 }
00163
00164 Aig_ManForEachLiLoSeq( p->pManAig, pObjLi, pObjLo, k )
00165 Fra_ObjSetFraig( pObjLo, f+1, Fra_ObjChild0Fra(pObjLi,f) );
00166 }
00167
00168
00169 pManFraig->nAsserts = Aig_ManPoNum(pManFraig);
00170
00171 Aig_ManForEachLoSeq( p->pManAig, pObj, i )
00172 Aig_ObjCreatePo( pManFraig, Fra_ObjFraig(pObj,p->nFramesAll-1) );
00173
00174
00175 Aig_ManCleanup( pManFraig );
00176
00177 assert( pManFraig->pData == NULL );
00178 return pManFraig;
00179 }
00180
00192 void Fra_FramesAddMore( Aig_Man_t * p, int nFrames )
00193 {
00194 Aig_Obj_t * pObj, ** pLatches;
00195 int i, k, f, nNodesOld;
00196
00197 Aig_ManForEachObj( p, pObj, i )
00198 pObj->pData = pObj;
00199
00200 nNodesOld = Aig_ManObjNumMax(p);
00201 pLatches = ALLOC( Aig_Obj_t *, Aig_ManRegNum(p) );
00202 for ( f = 0; f < nFrames; f++ )
00203 {
00204
00205 Aig_ManForEachLiSeq( p, pObj, i )
00206 pObj->pData = NULL;
00207 Aig_ManForEachLoSeq( p, pObj, i )
00208 pObj->pData = NULL;
00209
00210 k = 0;
00211 Aig_ManForEachLiSeq( p, pObj, i )
00212 {
00213 if ( Aig_ObjFanin0(pObj)->pData )
00214 pLatches[k++] = Aig_ObjChild0Copy(pObj);
00215 else
00216 pLatches[k++] = NULL;
00217 }
00218
00219 k = 0;
00220 Aig_ManForEachLoSeq( p, pObj, i )
00221 pObj->pData = pLatches[k++];
00222
00223 Aig_ManForEachNode( p, pObj, i )
00224 {
00225 if ( i > nNodesOld )
00226 break;
00227 if ( Aig_ObjFanin0(pObj)->pData && Aig_ObjFanin1(pObj)->pData )
00228 pObj->pData = Aig_And( p, Aig_ObjChild0Copy(pObj), Aig_ObjChild1Copy(pObj) );
00229 else
00230 pObj->pData = NULL;
00231 }
00232 }
00233 free( pLatches );
00234 }
00235
00247 Aig_Man_t * Fra_FraigInduction( Aig_Man_t * pManAig, int nFramesP, int nFramesK, int nMaxImps, int fRewrite, int fUseImps, int fLatchCorr, int fWriteImps, int fVerbose, int * pnIter )
00248 {
00249 int fUseSimpleCnf = 0;
00250 int fUseOldSimulation = 0;
00251
00252
00253
00254
00255
00256
00257 Fra_Man_t * p;
00258 Fra_Par_t Pars, * pPars = &Pars;
00259 Aig_Obj_t * pObj;
00260 Cnf_Dat_t * pCnf;
00261 Aig_Man_t * pManAigNew;
00262 int nNodesBeg, nRegsBeg;
00263 int nIter, i, clk = clock(), clk2;
00264
00265 if ( Aig_ManNodeNum(pManAig) == 0 )
00266 {
00267 if ( pnIter ) *pnIter = 0;
00268 return Aig_ManDup(pManAig, 1);
00269 }
00270 assert( Aig_ManLatchNum(pManAig) == 0 );
00271 assert( Aig_ManRegNum(pManAig) > 0 );
00272 assert( nFramesK > 0 );
00273
00274
00275 nNodesBeg = Aig_ManNodeNum(pManAig);
00276 nRegsBeg = Aig_ManRegNum(pManAig);
00277
00278
00279
00280
00281
00282 Fra_ParamsDefaultSeq( pPars );
00283 pPars->nFramesP = nFramesP;
00284 pPars->nFramesK = nFramesK;
00285 pPars->nMaxImps = nMaxImps;
00286 pPars->fVerbose = fVerbose;
00287 pPars->fRewrite = fRewrite;
00288 pPars->fLatchCorr = fLatchCorr;
00289 pPars->fUseImps = fUseImps;
00290 pPars->fWriteImps = fWriteImps;
00291
00292
00293 p = Fra_ManStart( pManAig, pPars );
00294
00295 if ( fUseOldSimulation )
00296 {
00297 if ( pPars->nFramesP > 0 )
00298 {
00299 pPars->nFramesP = 0;
00300 printf( "Fra_FraigInduction(): Prefix cannot be used.\n" );
00301 }
00302 p->pSml = Fra_SmlStart( pManAig, 0, pPars->nFramesK + 1, pPars->nSimWords );
00303 Fra_SmlSimulate( p, 1 );
00304 }
00305 else
00306 {
00307
00308
00309
00310 if ( fVerbose )
00311 printf( "Simulating %d AIG nodes for %d cycles ... ", Aig_ManNodeNum(pManAig), pPars->nFramesP + 32 );
00312 p->pSml = Fra_SmlSimulateSeq( pManAig, pPars->nFramesP, 32, 1 );
00313 if ( fVerbose )
00314 {
00315 PRT( "Time", clock() - clk );
00316 }
00317 Fra_ClassesPrepare( p->pCla, p->pPars->fLatchCorr );
00318
00319
00320 Fra_SmlStop( p->pSml );
00321 p->pSml = Fra_SmlStart( pManAig, 0, pPars->nFramesK + 1, pPars->nSimWords );
00322 }
00323
00324
00325 if ( pPars->fUseImps )
00326 p->pCla->vImps = Fra_ImpDerive( p, 5000000, pPars->nMaxImps, pPars->fLatchCorr );
00327
00328
00329 Fra_BmcPerform( p, pPars->nFramesP, pPars->nFramesK+1 );
00330
00331
00332
00333
00334 p->nLitsBeg = Fra_ClassesCountLits( p->pCla );
00335 p->nNodesBeg = nNodesBeg;
00336 p->nRegsBeg = nRegsBeg;
00337
00338
00339
00340
00341
00342
00343
00344
00345 p->pCla->fRefinement = 1;
00346 for ( nIter = 0; p->pCla->fRefinement; nIter++ )
00347 {
00348 int nLitsOld = Fra_ClassesCountLits(p->pCla);
00349 int nImpsOld = p->pCla->vImps? Vec_IntSize(p->pCla->vImps) : 0;
00350
00351 p->pCla->fRefinement = 0;
00352
00353 clk2 = clock();
00354 p->pManFraig = Fra_FramesWithClasses( p );
00355 p->timeTrav += clock() - clk2;
00356
00357
00358
00359 if ( p->pPars->fRewrite )
00360 Fra_FraigInductionRewrite( p );
00361
00362
00363 if ( fUseSimpleCnf || pPars->fUseImps )
00364 pCnf = Cnf_DeriveSimple( p->pManFraig, Aig_ManRegNum(p->pManFraig) );
00365 else
00366 pCnf = Cnf_Derive( p->pManFraig, Aig_ManRegNum(p->pManFraig) );
00367
00368
00369 p->pSat = Cnf_DataWriteIntoSolver( pCnf );
00370 p->nSatVars = pCnf->nVars;
00371 assert( p->pSat != NULL );
00372 if ( p->pSat == NULL )
00373 printf( "Fra_FraigInduction(): Computed CNF is not valid.\n" );
00374 if ( pPars->fUseImps )
00375 {
00376 Fra_ImpAddToSolver( p, p->pCla->vImps, pCnf->pVarNums );
00377 if ( p->pSat == NULL )
00378 printf( "Fra_FraigInduction(): Adding implicationsn to CNF led to a conflict.\n" );
00379 }
00380
00381
00382 Aig_ManForEachObj( p->pManFraig, pObj, i )
00383 pObj->pData = p;
00384
00385
00386 Fra_ManClean( p, Aig_ManObjNumMax(p->pManFraig) + Aig_ManNodeNum(p->pManAig) );
00387
00388
00389 Aig_ManForEachObj( p->pManFraig, pObj, i )
00390 {
00391 if ( pCnf->pVarNums[pObj->Id] == -1 )
00392 continue;
00393 Fra_ObjSetSatNum( pObj, pCnf->pVarNums[pObj->Id] );
00394 Fra_ObjSetFaninVec( pObj, (void *)1 );
00395 }
00396 Cnf_DataFree( pCnf );
00397
00398
00399 if ( fVerbose )
00400 {
00401 printf( "%3d : Const = %6d. Class = %6d. L = %6d. LR = %6d. ",
00402 nIter, Vec_PtrSize(p->pCla->vClasses1), Vec_PtrSize(p->pCla->vClasses),
00403 Fra_ClassesCountLits(p->pCla), p->pManFraig->nAsserts );
00404 if ( p->pCla->vImps )
00405 printf( "I = %6d. ", Vec_IntSize(p->pCla->vImps) );
00406 printf( "NR = %6d.\n", Aig_ManNodeNum(p->pManFraig) );
00407 }
00408
00409
00410 p->nSatCallsRecent = 0;
00411 p->nSatCallsSkipped = 0;
00412 Fra_FraigSweep( p );
00413
00414
00415
00416
00417 Aig_ManStop( p->pManFraig ); p->pManFraig = NULL;
00418 sat_solver_delete( p->pSat ); p->pSat = NULL;
00419 memset( p->pMemFraig, 0, sizeof(Aig_Obj_t *) * p->nSizeAlloc * p->nFramesAll );
00420
00421 assert( p->vTimeouts == NULL );
00422 if ( p->vTimeouts )
00423 printf( "Fra_FraigInduction(): SAT solver timed out!\n" );
00424
00425
00426 if ( p->pCla->fRefinement &&
00427 nLitsOld == Fra_ClassesCountLits(p->pCla) &&
00428 nImpsOld == (p->pCla->vImps? Vec_IntSize(p->pCla->vImps) : 0) )
00429 {
00430 printf( "Fra_FraigInduction(): Internal error. The result may not verify.\n" );
00431 break;
00432 }
00433 }
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447
00448 clk2 = clock();
00449
00450 Fra_ClassesSelectRepr( p->pCla );
00451 Fra_ClassesCopyReprs( p->pCla, p->vTimeouts );
00452 pManAigNew = Aig_ManDupRepr( pManAig, 0 );
00453
00454 if ( fWriteImps && p->pCla->vImps && Vec_IntSize(p->pCla->vImps) )
00455 Fra_ImpRecordInManager( p, pManAigNew );
00456
00457 Aig_ManSeqCleanup( pManAigNew );
00458
00459 p->timeTrav += clock() - clk2;
00460 p->timeTotal = clock() - clk;
00461
00462 p->nLitsEnd = Fra_ClassesCountLits( p->pCla );
00463 p->nNodesEnd = Aig_ManNodeNum(pManAigNew);
00464 p->nRegsEnd = Aig_ManRegNum(pManAigNew);
00465
00466 Fra_ManStop( p );
00467
00468
00469
00470
00471 if ( pnIter ) *pnIter = nIter;
00472 return pManAigNew;
00473 }
00474
00478
00479