pdrInv.c 32.2 KB
Newer Older
1 2 3 4 5 6
/**CFile****************************************************************

  FileName    [pdrInv.c]

  SystemName  [ABC: Logic synthesis and verification system.]

7
  PackageName [Property driven reachability.]
8 9 10 11 12 13 14 15 16 17 18 19 20 21

  Synopsis    [Invariant computation, printing, verification.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

  Date        [Ver. 1.0. Started - November 20, 2010.]

  Revision    [$Id: pdrInv.c,v 1.00 2010/11/20 00:00:00 alanmi Exp $]

***********************************************************************/

#include "pdrInt.h"
22 23
#include "base/abc/abc.h"      // for Abc_NtkCollectCioNames()
#include "base/main/main.h"    // for Abc_FrameReadGlobalFrame()
Alan Mishchenko committed
24
#include "aig/ioa/ioa.h"
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47

ABC_NAMESPACE_IMPL_START


////////////////////////////////////////////////////////////////////////
///                        DECLARATIONS                              ///
////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////
///                     FUNCTION DEFINITIONS                         ///
////////////////////////////////////////////////////////////////////////

/**Function*************************************************************

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
48
void Pdr_ManPrintProgress( Pdr_Man_t * p, int fClose, abctime Time )
49 50 51 52
{
    Vec_Ptr_t * vVec;
    int i, ThisSize, Length, LengthStart;
    if ( Vec_PtrSize(p->vSolvers) < 2 )
53
    {
54 55 56 57 58 59 60
        Abc_Print(1, "Frame " );
        Abc_Print(1, "Clauses                                                     " );
        Abc_Print(1, "Max Queue " );
        Abc_Print(1, "Flops " );
        Abc_Print(1, "Cex      " );
        Abc_Print(1, "Time" );
        Abc_Print(1, "\n" );
61
        return;
62
    }
63 64
    if ( Abc_FrameIsBatchMode() && !fClose )
        return;
65 66 67
    // count the total length of the printout
    Length = 0;
    Vec_VecForEachLevel( p->vClauses, vVec, i )
68
        Length += 1 + Abc_Base10Log(Vec_PtrSize(vVec)+1);
69
    // determine the starting point
Alan Mishchenko committed
70
    LengthStart = Abc_MaxInt( 0, Length - 60 );
71
    Abc_Print( 1, "%3d :", Vec_PtrSize(p->vSolvers)-1 );
Alan Mishchenko committed
72
    ThisSize = 5;
73 74
    if ( LengthStart > 0 )
    {
75
        Abc_Print( 1, " ..." );
76 77 78 79 80 81 82
        ThisSize += 4;
    }
    Length = 0;
    Vec_VecForEachLevel( p->vClauses, vVec, i )
    {
        if ( Length < LengthStart )
        {
83
            Length += 1 + Abc_Base10Log(Vec_PtrSize(vVec)+1);
84 85
            continue;
        }
86
        Abc_Print( 1, " %d", Vec_PtrSize(vVec) );
87 88
        Length += 1 + Abc_Base10Log(Vec_PtrSize(vVec)+1);
        ThisSize += 1 + Abc_Base10Log(Vec_PtrSize(vVec)+1);
89
    }
Alan Mishchenko committed
90
    for ( i = ThisSize; i < 70; i++ )
91
        Abc_Print( 1, " " );
92
    Abc_Print( 1, "%5d", p->nQueMax );
93
    Abc_Print( 1, "%6d", p->vAbsFlops ? Vec_IntCountPositive(p->vAbsFlops) : p->nAbsFlops );
94
    if ( p->pPars->fUseAbs )
95
    Abc_Print( 1, "%4d", p->nCexes );
96
    Abc_Print( 1, "%10.2f sec", 1.0*Time/CLOCKS_PER_SEC );
97 98
    if ( p->pPars->fSolveAll )
        Abc_Print( 1, "  CEX =%4d", p->pPars->nFailOuts );
99 100
    if ( p->pPars->nTimeOutOne )
        Abc_Print( 1, "  T/O =%3d", p->pPars->nDropOuts );
101
    Abc_Print( 1, "%s", fClose ? "\n":"\r" );
102
    if ( fClose )
103
        p->nQueMax = 0, p->nCexes = 0;
104
    fflush( stdout );
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
}

/**Function*************************************************************

  Synopsis    [Counts how many times each flop appears in the set of cubes.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Vec_Int_t * Pdr_ManCountFlops( Pdr_Man_t * p, Vec_Ptr_t * vCubes )
{
    Vec_Int_t * vFlopCount;
    Pdr_Set_t * pCube;
    int i, n;
    vFlopCount = Vec_IntStart( Aig_ManRegNum(p->pAig) );
    Vec_PtrForEachEntry( Pdr_Set_t *, vCubes, pCube, i )
125 126 127
    {
        if ( pCube->nRefs == -1 )
            continue;
128 129 130 131 132
        for ( n = 0; n < pCube->nLits; n++ )
        {
            assert( pCube->Lits[n] >= 0 && pCube->Lits[n] < 2*Aig_ManRegNum(p->pAig) );
            Vec_IntAddToEntry( vFlopCount, pCube->Lits[n] >> 1, 1 );
        }
133
    }
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
    return vFlopCount;
}

/**Function*************************************************************

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Pdr_ManFindInvariantStart( Pdr_Man_t * p )
{
    Vec_Ptr_t * vArrayK;
    int k, kMax = Vec_PtrSize(p->vSolvers)-1;
152
    Vec_VecForEachLevelStartStop( p->vClauses, vArrayK, k, 1, kMax+1 )
153 154
        if ( Vec_PtrSize(vArrayK) == 0 )
            return k;
155 156
//    return -1;
    // if there is no starting point (as in case of SAT or undecided), return the last frame
157
//    Abc_Print( 1, "The last timeframe contains %d clauses.\n", Vec_PtrSize(Vec_VecEntry(p->vClauses, kMax)) );
158
    return kMax;
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
}

/**Function*************************************************************

  Synopsis    [Counts the number of variables used in the clauses.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Vec_Ptr_t * Pdr_ManCollectCubes( Pdr_Man_t * p, int kStart )
{
    Vec_Ptr_t * vResult;
    Vec_Ptr_t * vArrayK;
    Pdr_Set_t * pSet;
    int i, j;
    vResult = Vec_PtrAlloc( 100 );
    Vec_VecForEachLevelStart( p->vClauses, vArrayK, i, kStart )
        Vec_PtrForEachEntry( Pdr_Set_t *, vArrayK, pSet, j )
            Vec_PtrPush( vResult, pSet );
    return vResult;
}

/**Function*************************************************************
Alan Mishchenko committed
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205

  Synopsis    []

  Description []

  SideEffects []

  SeeAlso     []

***********************************************************************/
Vec_Int_t * Pdr_ManCountFlopsInv( Pdr_Man_t * p )
{
    int kStart = Pdr_ManFindInvariantStart(p);
    Vec_Ptr_t *vCubes = Pdr_ManCollectCubes(p, kStart);
    Vec_Int_t * vInv = Pdr_ManCountFlops( p, vCubes );
    Vec_PtrFree(vCubes);
    return vInv;
}

/**Function*************************************************************
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220

  Synopsis    [Counts the number of variables used in the clauses.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Pdr_ManCountVariables( Pdr_Man_t * p, int kStart )
{
    Vec_Int_t * vFlopCounts;
    Vec_Ptr_t * vCubes;
    int i, Entry, Counter = 0;
221 222 223 224
    if ( p->vInfCubes == NULL )
        vCubes = Pdr_ManCollectCubes( p, kStart );
    else
        vCubes = Vec_PtrDup( p->vInfCubes );
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
    vFlopCounts = Pdr_ManCountFlops( p, vCubes );
    Vec_IntForEachEntry( vFlopCounts, Entry, i )
        Counter += (Entry > 0);
    Vec_IntFreeP( &vFlopCounts );
    Vec_PtrFree( vCubes );
    return Counter;
}

/**Function*************************************************************

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Pdr_ManPrintClauses( Pdr_Man_t * p, int kStart )
{
    Vec_Ptr_t * vArrayK;
    Pdr_Set_t * pCube;
    int i, k, Counter = 0;
    Vec_VecForEachLevelStart( p->vClauses, vArrayK, k, kStart )
    {
251
        Vec_PtrSort( vArrayK, (int (*)(const void *, const void *))Pdr_SetCompare );
252 253
        Vec_PtrForEachEntry( Pdr_Set_t *, vArrayK, pCube, i )
        {
254
            Abc_Print( 1, "C=%4d. F=%4d ", Counter++, k );
255
            Pdr_SetPrint( stdout, pCube, Aig_ManRegNum(p->pAig), NULL );  
256
            Abc_Print( 1, "\n" ); 
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
        }
    }
}

/**Function*************************************************************

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     [] 

***********************************************************************/
272 273 274
void Pdr_SetPrintOne( Pdr_Set_t * p )
{
    int i;
275
    Abc_Print(1, "Clause: {" );
276
    for ( i = 0; i < p->nLits; i++ )
277 278
        Abc_Print(1, " %s%d", Abc_LitIsCompl(p->Lits[i])? "!":"", Abc_Lit2Var(p->Lits[i]) );
    Abc_Print(1, " }\n" );
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
}

/**Function*************************************************************

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     [] 

***********************************************************************/
Aig_Man_t * Pdr_ManDupAigWithClauses( Aig_Man_t * p, Vec_Ptr_t * vCubes )
{
    Aig_Man_t * pNew;
    Aig_Obj_t * pObj, * pObjNew, * pLit;
    Pdr_Set_t * pCube;
    int i, n;
    // create the new manager
    pNew = Aig_ManStart( Aig_ManObjNumMax(p) );
    pNew->pName = Abc_UtilStrsav( p->pName );
    pNew->pSpec = Abc_UtilStrsav( p->pSpec );
    // create the PIs
    Aig_ManCleanData( p );
    Aig_ManConst1(p)->pData = Aig_ManConst1(pNew);
    Aig_ManForEachCi( p, pObj, i )
        pObj->pData = Aig_ObjCreateCi( pNew );
    // create outputs for each cube
    Vec_PtrForEachEntry( Pdr_Set_t *, vCubes, pCube, i )
    {
//        Pdr_SetPrintOne( pCube );
        pObjNew = Aig_ManConst1(pNew);
        for ( n = 0; n < pCube->nLits; n++ )
        {
            assert( Abc_Lit2Var(pCube->Lits[n]) < Saig_ManRegNum(p) );
            pLit = Aig_NotCond( Aig_ManCi(pNew, Saig_ManPiNum(p) + Abc_Lit2Var(pCube->Lits[n])), Abc_LitIsCompl(pCube->Lits[n]) );
            pObjNew = Aig_And( pNew, pObjNew, pLit );
        }
        Aig_ObjCreateCo( pNew, pObjNew );
    }
    // duplicate internal nodes
    Aig_ManForEachNode( p, pObj, i )
        pObj->pData = Aig_And( pNew, Aig_ObjChild0Copy(pObj), Aig_ObjChild1Copy(pObj) );
    // add the POs
    Saig_ManForEachLi( p, pObj, i )
        Aig_ObjCreateCo( pNew, Aig_ObjChild0Copy(pObj) );
    Aig_ManCleanup( pNew );
    Aig_ManSetRegNum( pNew, Aig_ManRegNum(p) );
    // check the resulting network
    if ( !Aig_ManCheck(pNew) )
330
        Abc_Print(1, "Aig_ManDupSimple(): The check has failed.\n" );
331 332 333 334 335 336 337
    return pNew;
}
void Pdr_ManDumpAig( Aig_Man_t * p, Vec_Ptr_t * vCubes )
{
    Aig_Man_t * pNew = Pdr_ManDupAigWithClauses( p, vCubes );
    Ioa_WriteAiger( pNew, "aig_with_clauses.aig", 0, 0 );
    Aig_ManStop( pNew );
338
    Abc_Print(1, "Dumped modified AIG into file \"aig_with_clauses.aig\".\n" );
339 340 341 342 343 344 345 346 347 348 349 350 351
}

/**Function*************************************************************

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     [] 

***********************************************************************/
352
void Pdr_ManDumpClauses( Pdr_Man_t * p, char * pFileName, int fProved )
353 354 355 356 357
{
    FILE * pFile;
    Vec_Int_t * vFlopCounts;
    Vec_Ptr_t * vCubes;
    Pdr_Set_t * pCube;
358
    char ** pNamesCi;
359
    int i, kStart, Count = 0;
360 361 362 363
    // create file
    pFile = fopen( pFileName, "w" );
    if ( pFile == NULL )
    {
364
        Abc_Print( 1, "Cannot open file \"%s\" for writing invariant.\n", pFileName );
365 366 367 368
        return;
    } 
    // collect cubes
    kStart = Pdr_ManFindInvariantStart( p );
369 370 371 372
    if ( fProved )
        vCubes = Pdr_ManCollectCubes( p, kStart );
    else
        vCubes = Vec_PtrDup( p->vInfCubes );
373
    Vec_PtrSort( vCubes, (int (*)(const void *, const void *))Pdr_SetCompare );
374
//    Pdr_ManDumpAig( p->pAig, vCubes );
375 376 377 378 379 380 381 382
    // count cubes
    Count = 0;
    Vec_PtrForEachEntry( Pdr_Set_t *, vCubes, pCube, i )
    {
        if ( pCube->nRefs == -1 )
            continue;
        Count++;
    }
383
    // collect variable appearances
384
    vFlopCounts = p->pPars->fUseSupp ? Pdr_ManCountFlops( p, vCubes ) : NULL; 
385
    // output the header
386 387 388 389
    if ( fProved )
        fprintf( pFile, "# Inductive invariant for \"%s\"\n", p->pAig->pName );
    else
        fprintf( pFile, "# Clauses of the last timeframe for \"%s\"\n", p->pAig->pName );
390
    fprintf( pFile, "# generated by PDR in ABC on %s\n", Aig_TimeStamp() );
391
    fprintf( pFile, ".i %d\n", p->pPars->fUseSupp ? Pdr_ManCountVariables(p, kStart) : Aig_ManRegNum(p->pAig) );
392
    fprintf( pFile, ".o 1\n" );
393
    fprintf( pFile, ".p %d\n", Count );
394 395 396 397 398 399
    // output flop names
    pNamesCi = Abc_NtkCollectCioNames( Abc_FrameReadNtk( Abc_FrameReadGlobalFrame() ), 0 );
    if ( pNamesCi )
    {
        fprintf( pFile, ".ilb" );
        for ( i = 0; i < Aig_ManRegNum(p->pAig); i++ )
400
            if ( !p->pPars->fUseSupp || Vec_IntEntry( vFlopCounts, i ) )
401 402 403 404 405
                fprintf( pFile, " %s", pNamesCi[Saig_ManPiNum(p->pAig) + i] );
        fprintf( pFile, "\n" );
        ABC_FREE( pNamesCi );
        fprintf( pFile, ".ob inv\n" );
    }
406 407 408
    // output cubes
    Vec_PtrForEachEntry( Pdr_Set_t *, vCubes, pCube, i )
    {
409 410
        if ( pCube->nRefs == -1 )
            continue;
411
        Pdr_SetPrint( pFile, pCube, Aig_ManRegNum(p->pAig), vFlopCounts );  
412
        fprintf( pFile, " 1\n" ); 
413 414 415 416 417
    }
    fprintf( pFile, ".e\n\n" );
    fclose( pFile );
    Vec_IntFreeP( &vFlopCounts );
    Vec_PtrFree( vCubes );
418
    if ( fProved )
419
        Abc_Print( 1, "Inductive invariant was written into file \"%s\".\n", pFileName );
420
    else
421
        Abc_Print( 1, "Clauses of the last timeframe were written into file \"%s\".\n", pFileName );
422 423
}

424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
/**Function*************************************************************

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     [] 

***********************************************************************/
Vec_Str_t * Pdr_ManDumpString( Pdr_Man_t * p )
{
    Vec_Str_t * vStr;
    Vec_Int_t * vFlopCounts;
    Vec_Ptr_t * vCubes;
    Pdr_Set_t * pCube;
    int i, kStart;
    vStr = Vec_StrAlloc( 1000 );
    // collect cubes
    kStart = Pdr_ManFindInvariantStart( p );
    if ( p->vInfCubes == NULL )
        vCubes = Pdr_ManCollectCubes( p, kStart );
    else
        vCubes = Vec_PtrDup( p->vInfCubes );
449
    Vec_PtrSort( vCubes, (int (*)(const void *, const void *))Pdr_SetCompare );
450
    // collect variable appearances
451
    vFlopCounts = p->pPars->fUseSupp ? Pdr_ManCountFlops( p, vCubes ) : NULL; 
452 453 454 455 456 457 458 459 460 461 462 463 464
    // output cubes
    Vec_PtrForEachEntry( Pdr_Set_t *, vCubes, pCube, i )
    {
        if ( pCube->nRefs == -1 )
            continue;
        Pdr_SetPrintStr( vStr, pCube, Aig_ManRegNum(p->pAig), vFlopCounts );  
    }
    Vec_IntFreeP( &vFlopCounts );
    Vec_PtrFree( vCubes );
    Vec_StrPush( vStr, '\0' );
    return vStr;
}

465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481

/**Function*************************************************************

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Pdr_ManReportInvariant( Pdr_Man_t * p )
{
    Vec_Ptr_t * vCubes;
    int kStart = Pdr_ManFindInvariantStart( p );
    vCubes = Pdr_ManCollectCubes( p, kStart );
482 483
    Abc_Print( 1, "Invariant F[%d] : %d clauses with %d flops (out of %d) (cex = %d, ave = %.2f)\n", 
        kStart, Vec_PtrSize(vCubes), Pdr_ManCountVariables(p, kStart), Aig_ManRegNum(p->pAig), p->nCexesTotal, 1.0*p->nXsimLits/p->nXsimRuns );
484 485
//    Abc_Print( 1, "Invariant F[%d] : %d clauses with %d flops (out of %d)\n", 
//        kStart, Vec_PtrSize(vCubes), Pdr_ManCountVariables(p, kStart), Aig_ManRegNum(p->pAig) );
486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505
    Vec_PtrFree( vCubes );
}

/**Function*************************************************************

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Pdr_ManVerifyInvariant( Pdr_Man_t * p )
{
    sat_solver * pSat;
    Vec_Int_t * vLits;
    Vec_Ptr_t * vCubes;
    Pdr_Set_t * pCube;
506
    int i, kStart, kThis, RetValue, Counter = 0;
507
    abctime clk = Abc_Clock();
508 509 510 511 512 513 514
    // collect cubes used in the inductive invariant
    kStart = Pdr_ManFindInvariantStart( p );
    vCubes = Pdr_ManCollectCubes( p, kStart );
    // create solver with the cubes
    kThis = Vec_PtrSize(p->vSolvers);
    pSat  = Pdr_ManCreateSolver( p, kThis );
    // add the property output
515
//    Pdr_ManSetPropertyOutput( p, kThis );
516 517 518 519 520 521 522 523 524 525 526 527 528 529 530
    // add the clauses
    Vec_PtrForEachEntry( Pdr_Set_t *, vCubes, pCube, i )
    {
        vLits = Pdr_ManCubeToLits( p, kThis, pCube, 1, 0 );
        RetValue = sat_solver_addclause( pSat, Vec_IntArray(vLits), Vec_IntArray(vLits) + Vec_IntSize(vLits) );
        assert( RetValue );
        sat_solver_compress( pSat );
    }
    // check each clause
    Vec_PtrForEachEntry( Pdr_Set_t *, vCubes, pCube, i )
    {
        vLits = Pdr_ManCubeToLits( p, kThis, pCube, 0, 1 );
        RetValue = sat_solver_solve( pSat, Vec_IntArray(vLits), Vec_IntArray(vLits) + Vec_IntSize(vLits), 0, 0, 0, 0 );
        if ( RetValue != l_False )
        {
531
            Abc_Print( 1, "Verification of clause %d failed.\n", i );
532 533 534 535
            Counter++;
        }
    }
    if ( Counter )
536
        Abc_Print( 1, "Verification of %d clauses has failed.\n", Counter );
537 538
    else
    {
539
        Abc_Print( 1, "Verification of invariant with %d clauses was successful.  ", Vec_PtrSize(vCubes) );
540
        Abc_PrintTime( 1, "Time", Abc_Clock() - clk );
541 542 543 544 545
    }
//    sat_solver_delete( pSat );
    Vec_PtrFree( vCubes );
}

546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590
/**Function*************************************************************

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Pdr_ManDeriveMarkNonInductive( Pdr_Man_t * p, Vec_Ptr_t * vCubes )
{
    sat_solver * pSat;
    Vec_Int_t * vLits;
    Pdr_Set_t * pCube;
    int i, kThis, RetValue, fChanges = 0, Counter = 0;
    // create solver with the cubes
    kThis = Vec_PtrSize(p->vSolvers);
    pSat  = Pdr_ManCreateSolver( p, kThis );
    // add the clauses
    Vec_PtrForEachEntry( Pdr_Set_t *, vCubes, pCube, i )
    {
        if ( pCube->nRefs == -1 ) // skip non-inductive
            continue;
        vLits = Pdr_ManCubeToLits( p, kThis, pCube, 1, 0 );
        RetValue = sat_solver_addclause( pSat, Vec_IntArray(vLits), Vec_IntArray(vLits) + Vec_IntSize(vLits) );
        assert( RetValue );
        sat_solver_compress( pSat );
    }
    // check each clause
    Vec_PtrForEachEntry( Pdr_Set_t *, vCubes, pCube, i )
    {
        if ( pCube->nRefs == -1 ) // skip non-inductive
            continue;
        vLits = Pdr_ManCubeToLits( p, kThis, pCube, 0, 1 );
        RetValue = sat_solver_solve( pSat, Vec_IntArray(vLits), Vec_IntArray(vLits) + Vec_IntSize(vLits), 0, 0, 0, 0 );
        if ( RetValue != l_False ) // mark as non-inductive
        {
            pCube->nRefs = -1;
            fChanges = 1;
        }
        else
            Counter++;
    }
591
    //Abc_Print(1, "Clauses = %d.\n", Counter );
592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621
    //sat_solver_delete( pSat );
    return fChanges;
}
Vec_Int_t * Pdr_ManDeriveInfinityClauses( Pdr_Man_t * p, int fReduce )
{
    Vec_Int_t * vResult;
    Vec_Ptr_t * vCubes;
    Pdr_Set_t * pCube;
    int i, v, kStart;
    // collect cubes used in the inductive invariant
    kStart = Pdr_ManFindInvariantStart( p );
    vCubes = Pdr_ManCollectCubes( p, kStart );
    // refine as long as there are changes
    if ( fReduce )
        while ( Pdr_ManDeriveMarkNonInductive(p, vCubes) );
    // collect remaining clauses
    vResult = Vec_IntAlloc( 1000 );
    Vec_IntPush( vResult, 0 );
    Vec_PtrForEachEntry( Pdr_Set_t *, vCubes, pCube, i )
    {
        if ( pCube->nRefs == -1 ) // skip non-inductive
            continue;
        Vec_IntAddToEntry( vResult, 0, 1 );
        Vec_IntPush( vResult, pCube->nLits );
        for ( v = 0; v < pCube->nLits; v++ )
            Vec_IntPush( vResult, pCube->Lits[v] );
    }
    //Vec_PtrFree( vCubes );
    Vec_PtrFreeP( &p->vInfCubes );
    p->vInfCubes = vCubes;
622
    Vec_IntPush( vResult, Aig_ManRegNum(p->pAig) );
623 624 625
    return vResult;
}

626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672


/**Function*************************************************************

  Synopsis    [Remove clauses while maintaining the invariant.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
#define Pdr_ForEachCube( pList, pCut, i ) for ( i = 0, pCut = pList + 1; i < pList[0]; i++, pCut += pCut[0] + 1 )

Vec_Int_t * Pdr_InvMap( Vec_Int_t * vCounts )
{
    int i, k = 0, Count;
    Vec_Int_t * vMap = Vec_IntStart( Vec_IntSize(vCounts) );
    Vec_IntForEachEntry( vCounts, Count, i )
        if ( Count )
            Vec_IntWriteEntry( vMap, i, k++ );
    return vMap;
}
Vec_Int_t * Pdr_InvCounts( Vec_Int_t * vInv )
{
    int i, k, * pCube, * pList = Vec_IntArray(vInv);
    Vec_Int_t * vCounts = Vec_IntStart( Vec_IntEntryLast(vInv) );
    Pdr_ForEachCube( pList, pCube, i )
        for ( k = 0; k < pCube[0]; k++ )
            Vec_IntAddToEntry( vCounts, Abc_Lit2Var(pCube[k+1]), 1 );
    return vCounts;
}
int Pdr_InvUsedFlopNum( Vec_Int_t * vInv )
{
    Vec_Int_t * vCounts = Pdr_InvCounts( vInv );
    int nZeros = Vec_IntCountZero( vCounts );
    Vec_IntFree( vCounts );
    return Vec_IntEntryLast(vInv) - nZeros;
}

Vec_Str_t * Pdr_InvPrintStr( Vec_Int_t * vInv, Vec_Int_t * vCounts )
{
    Vec_Str_t * vStr = Vec_StrAlloc( 1000 );
    Vec_Int_t * vMap = Pdr_InvMap( vCounts );
    int nVars = Vec_IntSize(vCounts) - Vec_IntCountZero(vCounts);
    int i, k, * pCube, * pList = Vec_IntArray(vInv);
673
    char * pBuffer = ABC_ALLOC( char, (size_t)(unsigned)nVars );
674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692
    for ( i = 0; i < nVars; i++ )
        pBuffer[i] = '-';
    Pdr_ForEachCube( pList, pCube, i )
    {
        for ( k = 0; k < pCube[0]; k++ )
            pBuffer[Vec_IntEntry(vMap, Abc_Lit2Var(pCube[k+1]))] = '0' + !Abc_LitIsCompl(pCube[k+1]);
        for ( k = 0; k < nVars; k++ )
            Vec_StrPush( vStr, pBuffer[k] );
        Vec_StrPush( vStr, ' ' );
        Vec_StrPush( vStr, '1' );
        Vec_StrPush( vStr, '\n' );
        for ( k = 0; k < pCube[0]; k++ )
            pBuffer[Vec_IntEntry(vMap, Abc_Lit2Var(pCube[k+1]))] = '-';
    }
    Vec_StrPush( vStr, '\0' );
    ABC_FREE( pBuffer );
    Vec_IntFree( vMap );
    return vStr;
}
693
void Pdr_InvPrint( Vec_Int_t * vInv, int fVerbose )
694
{
695
    Abc_Print(1, "Invariant contains %d clauses with %d literals and %d flops (out of %d).\n", Vec_IntEntry(vInv, 0), Vec_IntSize(vInv)-Vec_IntEntry(vInv, 0)-2, Pdr_InvUsedFlopNum(vInv), Vec_IntEntryLast(vInv) );
696 697 698 699
    if ( fVerbose )
    {
        Vec_Int_t * vCounts = Pdr_InvCounts( vInv );
        Vec_Str_t * vStr = Pdr_InvPrintStr( vInv, vCounts );
700
        Abc_Print(1, "%s", Vec_StrArray( vStr ) );
701 702 703
        Vec_IntFree( vCounts );
        Vec_StrFree( vStr );
    }
704 705
}

706
int Pdr_InvCheck_int( Gia_Man_t * p, Vec_Int_t * vInv, int fVerbose, sat_solver * pSat, int fSkip )
707 708
{
    int nBTLimit = 0;
709 710
    int fCheckProperty = 1;
    int i, k, status, nFailed = 0, nFailedOuts = 0; 
711
    // collect cubes
712
    int * pCube, * pList = Vec_IntArray(vInv);
713 714
    // create variables
    Vec_Int_t * vLits = Vec_IntAlloc(100);
715
    int iFoVarBeg = sat_solver_nvars(pSat) - Gia_ManRegNum(p);
716 717 718 719 720 721 722
    int iFiVarBeg = 1 + Gia_ManPoNum(p);
    // add cubes
    Pdr_ForEachCube( pList, pCube, i )
    {
        // collect literals
        Vec_IntClear( vLits );
        for ( k = 0; k < pCube[0]; k++ )
723 724
            if ( pCube[k+1] != -1 )
                Vec_IntPush( vLits, Abc_Var2Lit(iFoVarBeg + Abc_Lit2Var(pCube[k+1]), !Abc_LitIsCompl(pCube[k+1])) );
725 726 727 728 729
        if ( Vec_IntSize(vLits) == 0 )
        {
            Vec_IntFree( vLits );
            return 1;
        }
730 731 732 733
        // add it to the solver
        status = sat_solver_addclause( pSat, Vec_IntArray(vLits), Vec_IntLimit(vLits) );
        assert( status == 1 );
    }
734 735 736 737 738 739 740 741 742 743 744 745
    // verify property output
    if ( fCheckProperty )
    {
        for ( i = 0; i < Gia_ManPoNum(p); i++ )
        {
            Vec_IntFill( vLits, 1, Abc_Var2Lit(1+i, 0) );
            status = sat_solver_solve( pSat, Vec_IntArray(vLits), Vec_IntLimit(vLits), nBTLimit, 0, 0, 0 );
            if ( status == l_Undef ) // timeout
                break;
            if ( status == l_True ) // sat - property fails
            {
                if ( fVerbose )
746
                    Abc_Print(1, "Coverage check failed for output %d.\n", i );
747
                nFailedOuts++;
748 749 750 751 752
                if ( fSkip )
                {
                    Vec_IntFree( vLits );
                    return 1;
                }
753 754 755 756 757
                continue;
            }
            assert( status == l_False ); // unsat - property holds
        }
    }
758 759 760 761 762 763
    // iterate through cubes in the direct order
    Pdr_ForEachCube( pList, pCube, i )
    {
        // collect cube
        Vec_IntClear( vLits );
        for ( k = 0; k < pCube[0]; k++ )
764 765
            if ( pCube[k+1] != -1 )
                Vec_IntPush( vLits, Abc_Var2Lit(iFiVarBeg + Abc_Lit2Var(pCube[k+1]), Abc_LitIsCompl(pCube[k+1])) );
766 767 768
        // check if this cube intersects with the complement of other cubes in the solver
        // if it does not intersect, then it is redundant and can be skipped
        status = sat_solver_solve( pSat, Vec_IntArray(vLits), Vec_IntLimit(vLits), nBTLimit, 0, 0, 0 );
769
        if ( status != l_True && fVerbose )
770
            Abc_Print(1, "Finished checking clause %d (out of %d)...\r", i, pList[0] );
771 772 773 774 775
        if ( status == l_Undef ) // timeout
            break;
        if ( status == l_False ) // unsat -- correct
            continue;
        assert( status == l_True );
776
        if ( fVerbose )
777
            Abc_Print(1, "Inductiveness check failed for clause %d.\n", i );
778
        nFailed++;
779 780 781 782 783
        if ( fSkip )
        {
            Vec_IntFree( vLits );
            return 1;
        }
784
    }
785 786 787 788 789 790 791
    Vec_IntFree( vLits );
    return nFailed + nFailedOuts;
}

int Pdr_InvCheck( Gia_Man_t * p, Vec_Int_t * vInv, int fVerbose )
{
    int RetValue;
792
    Cnf_Dat_t * pCnf = (Cnf_Dat_t *)Mf_ManGenerateCnf( p, 8, 0, 0, 0, 0 );
793 794
    sat_solver * pSat = (sat_solver *)Cnf_DataWriteIntoSolver( pCnf, 1, 0 );
    assert( sat_solver_nvars(pSat) == pCnf->nVars );
795
    Cnf_DataFree( pCnf );
796
    RetValue = Pdr_InvCheck_int( p, vInv, fVerbose, pSat, 0 );
797
    sat_solver_delete( pSat );
798
    return RetValue;
799 800
}

801
Vec_Int_t * Pdr_InvMinimize( Gia_Man_t * p, Vec_Int_t * vInv, int fVerbose )
802 803
{
    int nBTLimit = 0;
804 805
    int fCheckProperty = 1;
    abctime clk = Abc_Clock();
806 807 808
    int n, i, k, status, nLits, fFailed = 0, fCannot = 0, nRemoved = 0; 
    Vec_Int_t * vRes = NULL;
    // create SAT solver
809
    Cnf_Dat_t * pCnf = (Cnf_Dat_t *)Mf_ManGenerateCnf( p, 8, 0, 0, 0, 0 );
810 811 812 813 814 815 816 817 818
    sat_solver * pSat = (sat_solver *)Cnf_DataWriteIntoSolver( pCnf, 1, 0 );
    int * pCube, * pList = Vec_IntArray(vInv), nCubes = pList[0];
    // create variables
    Vec_Int_t * vLits = Vec_IntAlloc(100);
    Vec_Bit_t * vRemoved = Vec_BitStart( nCubes );
    int iFoVarBeg = pCnf->nVars - Gia_ManRegNum(p);
    int iFiVarBeg = 1 + Gia_ManPoNum(p);
    int iAuxVarBeg = sat_solver_nvars(pSat);
    // allocate auxiliary variables
819
    assert( sat_solver_nvars(pSat) == pCnf->nVars );
820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842
    sat_solver_setnvars( pSat, sat_solver_nvars(pSat) + nCubes );
    // add clauses
    Pdr_ForEachCube( pList, pCube, i )
    {
        // collect literals
        Vec_IntFill( vLits, 1, Abc_Var2Lit(iAuxVarBeg + i, 1) ); // neg aux literal
        for ( k = 0; k < pCube[0]; k++ )
            Vec_IntPush( vLits, Abc_Var2Lit(iFoVarBeg + Abc_Lit2Var(pCube[k+1]), !Abc_LitIsCompl(pCube[k+1])) );
        // add it to the solver
        status = sat_solver_addclause( pSat, Vec_IntArray(vLits), Vec_IntLimit(vLits) );
        assert( status == 1 );
    }
    // iterate through clauses 
    Pdr_ForEachCube( pList, pCube, i )
    {
        if ( Vec_BitEntry(vRemoved, i) )
            continue;
        // collect aux literals for remaining clauses
        Vec_IntClear( vLits );
        for ( k = 0; k < nCubes; k++ )
            if ( k != i && !Vec_BitEntry(vRemoved, k) ) // skip this cube and already removed cubes
                Vec_IntPush( vLits, Abc_Var2Lit(iAuxVarBeg + k, 0) ); // pos aux literal
        nLits = Vec_IntSize( vLits );
843 844 845
        // check if the property still holds
        if ( fCheckProperty )
        {
846
            for ( k = 0; k < Gia_ManPoNum(p); k++ )
847
            {
848 849 850 851 852 853 854 855 856 857 858
                Vec_IntShrink( vLits, nLits );
                Vec_IntPush( vLits, Abc_Var2Lit(1+k, 0) );
                status = sat_solver_solve( pSat, Vec_IntArray(vLits), Vec_IntLimit(vLits), nBTLimit, 0, 0, 0 );
                if ( status == l_Undef ) // timeout
                {
                    fFailed = 1;
                    break;
                }
                if ( status == l_True ) // sat - property fails
                    break;
                assert( status == l_False ); // unsat - property holds
859
            }
860 861 862
            if ( fFailed )
                break;
            if ( k < Gia_ManPoNum(p) )
863 864 865
                continue;
        }
        // check other clauses
866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893
        fCannot = 0;
        Pdr_ForEachCube( pList, pCube, n )
        {
            if ( Vec_BitEntry(vRemoved, n) || n == i )
                continue;
            // collect cube
            Vec_IntShrink( vLits, nLits );
            for ( k = 0; k < pCube[0]; k++ )
               Vec_IntPush( vLits, Abc_Var2Lit(iFiVarBeg + Abc_Lit2Var(pCube[k+1]), Abc_LitIsCompl(pCube[k+1])) );
            // check if this cube intersects with the complement of other cubes in the solver
            // if it does not intersect, then it is redundant and can be skipped
            status = sat_solver_solve( pSat, Vec_IntArray(vLits), Vec_IntLimit(vLits), nBTLimit, 0, 0, 0 );
            if ( status == l_Undef ) // timeout
            {
                fFailed = 1;
                break;
            }
            if ( status == l_False ) // unsat -- correct
                continue;
            assert( status == l_True );
            // cannot remove
            fCannot = 1;
            break;
        }
        if ( fFailed )
            break;
        if ( fCannot )
            continue;
894
        if ( fVerbose )
895
        Abc_Print(1, "Removing clause %d.\n", i );
896 897 898 899
        Vec_BitWriteEntry( vRemoved, i, 1 );
        nRemoved++;
    }
    if ( nRemoved )
900
        Abc_Print(1, "Invariant minimization reduced %d clauses (out of %d).  ", nRemoved, nCubes );
901
    else
902
        Abc_Print(1, "Invariant minimization did not change the invariant.  " ); 
903
    Abc_PrintTime( 1, "Time", Abc_Clock() - clk );
904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921
    // cleanup cover
    if ( !fFailed && nRemoved > 0 ) // finished without timeout and removed some cubes
    {
        vRes = Vec_IntAlloc( 1000 );
        Vec_IntPush( vRes, nCubes-nRemoved );
        Pdr_ForEachCube( pList, pCube, i )
            if ( !Vec_BitEntry(vRemoved, i) )
                for ( k = 0; k <= pCube[0]; k++ )
                    Vec_IntPush( vRes, pCube[k] );
        Vec_IntPush( vRes, Vec_IntEntryLast(vInv) );
    }
    Cnf_DataFree( pCnf );
    sat_solver_delete( pSat );
    Vec_BitFree( vRemoved );
    Vec_IntFree( vLits );
    return vRes;
}

922
Vec_Int_t * Pdr_InvMinimizeLits( Gia_Man_t * p, Vec_Int_t * vInv, int fVerbose )
923 924
{
    Vec_Int_t * vRes = NULL;
925
    abctime clk = Abc_Clock();
926
    int i, k, nLits = 0, * pCube, * pList = Vec_IntArray(vInv), nRemoved = 0;
927
    Cnf_Dat_t * pCnf = (Cnf_Dat_t *)Mf_ManGenerateCnf( p, 8, 0, 0, 0, 0 );
928 929
    sat_solver * pSat;
//    sat_solver * pSat = (sat_solver *)Cnf_DataWriteIntoSolver( pCnf, 1, 0 );
930 931 932 933 934 935 936
    Pdr_ForEachCube( pList, pCube, i )
    {
        nLits += pCube[0];
        for ( k = 0; k < pCube[0]; k++ )
        {
            int Save = pCube[k+1];
            pCube[k+1] = -1;
937 938
            //sat_solver_bookmark( pSat );
            pSat = (sat_solver *)Cnf_DataWriteIntoSolver( pCnf, 1, 0 );
939
            if ( Pdr_InvCheck_int(p, vInv, 0, pSat, 1) )
940
                pCube[k+1] = Save;
941 942 943
            else
            {
                if ( fVerbose )
944
                Abc_Print(1, "Removing lit %d from clause %d.\n", k, i );
945
                nRemoved++;
946
            }
947 948 949
            sat_solver_delete( pSat );
            //sat_solver_rollback( pSat );
            //sat_solver_bookmark( pSat );
950 951
        }
    }
952 953
    Cnf_DataFree( pCnf );
    //sat_solver_delete( pSat );
954
    if ( nRemoved )
955
        Abc_Print(1, "Invariant minimization reduced %d literals (out of %d).  ", nRemoved, nLits );
956
    else
957
        Abc_Print(1, "Invariant minimization did not change the invariant.  " ); 
958
    Abc_PrintTime( 1, "Time", Abc_Clock() - clk );
959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978
    if ( nRemoved > 0 ) // finished without timeout and removed some lits
    {
        vRes = Vec_IntAlloc( 1000 );
        Vec_IntPush( vRes, pList[0] );
        Pdr_ForEachCube( pList, pCube, i )
        {
            int nLits = 0;
            for ( k = 0; k < pCube[0]; k++ )
                if ( pCube[k+1] != -1 )
                    nLits++;
            Vec_IntPush( vRes, nLits );
            for ( k = 0; k < pCube[0]; k++ )
                if ( pCube[k+1] != -1 )
                    Vec_IntPush( vRes, pCube[k+1] );
        }
        Vec_IntPush( vRes, Vec_IntEntryLast(vInv) );
    }
    return vRes;
}

979 980 981 982 983 984 985
////////////////////////////////////////////////////////////////////////
///                       END OF FILE                                ///
////////////////////////////////////////////////////////////////////////


ABC_NAMESPACE_IMPL_END