sswSweep.c 13.7 KB
Newer Older
Alan Mishchenko committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/**CFile****************************************************************

  FileName    [sswSweep.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [Inductive prover with constraints.]

  Synopsis    [One round of SAT sweeping.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

  Date        [Ver. 1.0. Started - September 1, 2008.]

  Revision    [$Id: sswSweep.c,v 1.00 2008/09/01 00:00:00 alanmi Exp $]

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

#include "sswInt.h"
22
#include "misc/bar/bar.h"
Alan Mishchenko committed
23

24 25 26
ABC_NAMESPACE_IMPL_START


Alan Mishchenko committed
27 28 29 30 31 32 33 34 35 36
////////////////////////////////////////////////////////////////////////
///                        DECLARATIONS                              ///
////////////////////////////////////////////////////////////////////////

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

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

Alan Mishchenko committed
37 38 39 40 41 42 43 44 45
  Synopsis    [Retrives value of the PI in the original AIG.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
46
int Ssw_ManGetSatVarValue( Ssw_Man_t * p, Aig_Obj_t * pObj, int f )
Alan Mishchenko committed
47
{
Alan Mishchenko committed
48
    int fUseNoBoundary = 0;
Alan Mishchenko committed
49
    Aig_Obj_t * pObjFraig;
Alan Mishchenko committed
50
    int Value;
51
//    assert( Aig_ObjIsCi(pObj) );
Alan Mishchenko committed
52
    pObjFraig = Ssw_ObjFrame( p, pObj, f );
Alan Mishchenko committed
53 54 55 56 57 58 59 60 61 62 63
    if ( fUseNoBoundary )
    {
        Value = Ssw_CnfGetNodeValue( p->pMSat, Aig_Regular(pObjFraig) );
        Value ^= Aig_IsComplement(pObjFraig);
    }
    else
    {
        int nVarNum = Ssw_ObjSatNum( p->pMSat, Aig_Regular(pObjFraig) );
        Value = (!nVarNum)? 0 : (Aig_IsComplement(pObjFraig) ^ sat_solver_var_value( p->pMSat->pSat, nVarNum ));
    }

Alan Mishchenko committed
64
//    Value = (Aig_IsComplement(pObjFraig) ^ ((!nVarNum)? 0 : sat_solver_var_value( p->pSat, nVarNum )));
Alan Mishchenko committed
65 66 67 68 69 70 71 72 73 74
//    Value = (!nVarNum)? Aig_ManRandom(0) & 1 : (Aig_IsComplement(pObjFraig) ^ sat_solver_var_value( p->pSat, nVarNum ));
    if ( p->pPars->fPolarFlip )
    {
        if ( Aig_Regular(pObjFraig)->fPhase )  Value ^= 1;
    }
    return Value;
}

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

Alan Mishchenko committed
75 76 77 78 79 80 81 82 83 84 85 86 87 88
  Synopsis    [Performs fraiging for the internal nodes.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Ssw_CheckConstraints( Ssw_Man_t * p )
{
    Aig_Obj_t * pObj, * pObj2;
    int nConstrPairs, i;
    int Counter = 0;
89
    nConstrPairs = Aig_ManCoNum(p->pFrames)-Aig_ManRegNum(p->pAig);
Alan Mishchenko committed
90 91 92
    assert( (nConstrPairs & 1) == 0 );
    for ( i = 0; i < nConstrPairs; i += 2 )
    {
93 94
        pObj  = Aig_ManCo( p->pFrames, i   );
        pObj2 = Aig_ManCo( p->pFrames, i+1 );
Alan Mishchenko committed
95 96 97 98 99 100
        if ( Ssw_NodesAreEquiv( p, Aig_ObjFanin0(pObj), Aig_ObjFanin0(pObj2) ) != 1 )
        {
            Ssw_NodesAreConstrained( p, Aig_ObjChild0(pObj), Aig_ObjChild0(pObj2) );
            Counter++;
        }
    }
101
    Abc_Print( 1, "Total constraints = %d. Added constraints = %d.\n", nConstrPairs/2, Counter );
Alan Mishchenko committed
102 103 104 105
}

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

Alan Mishchenko committed
106 107 108 109 110 111 112 113 114
  Synopsis    [Copy pattern from the solver into the internal storage.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
115
void Ssw_SmlSavePatternAigPhase( Ssw_Man_t * p, int f )
Alan Mishchenko committed
116 117 118 119
{
    Aig_Obj_t * pObj;
    int i;
    memset( p->pPatWords, 0, sizeof(unsigned) * p->nPatWords );
120
    Aig_ManForEachCi( p->pAig, pObj, i )
Alan Mishchenko committed
121
        if ( Aig_ObjPhaseReal( Ssw_ObjFrame(p, pObj, f) ) )
122
            Abc_InfoSetBit( p->pPatWords, i );
Alan Mishchenko committed
123 124 125 126 127 128 129 130 131 132 133 134 135
}

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

  Synopsis    [Copy pattern from the solver into the internal storage.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
136
void Ssw_SmlSavePatternAig( Ssw_Man_t * p, int f )
Alan Mishchenko committed
137 138 139 140
{
    Aig_Obj_t * pObj;
    int i;
    memset( p->pPatWords, 0, sizeof(unsigned) * p->nPatWords );
141
    Aig_ManForEachCi( p->pAig, pObj, i )
Alan Mishchenko committed
142
        if ( Ssw_ManGetSatVarValue( p, pObj, f ) )
143
            Abc_InfoSetBit( p->pPatWords, i );
Alan Mishchenko committed
144 145 146 147
}

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

Alan Mishchenko committed
148 149 150
  Synopsis    [Saves one counter-example into internal storage.]

  Description []
151

Alan Mishchenko committed
152 153 154 155 156 157 158 159 160 161 162
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Ssw_SmlAddPatternDyn( Ssw_Man_t * p )
{
    Aig_Obj_t * pObj;
    unsigned * pInfo;
    int i, nVarNum;
    // iterate through the PIs of the frames
163
    Vec_PtrForEachEntry( Aig_Obj_t *, p->pMSat->vUsedPis, pObj, i )
Alan Mishchenko committed
164
    {
165
        assert( Aig_ObjIsCi(pObj) );
Alan Mishchenko committed
166 167 168 169
        nVarNum = Ssw_ObjSatNum( p->pMSat, pObj );
        assert( nVarNum > 0 );
        if ( sat_solver_var_value( p->pMSat->pSat, nVarNum ) )
        {
170
            pInfo = (unsigned *)Vec_PtrEntry( p->vSimInfo, Aig_ObjCioId(pObj) );
171
            Abc_InfoSetBit( pInfo, p->nPatterns );
Alan Mishchenko committed
172 173 174 175 176 177
        }
    }
}

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

Alan Mishchenko committed
178 179 180
  Synopsis    [Performs fraiging for one node.]

  Description [Returns the fraiged node.]
181

Alan Mishchenko committed
182 183 184 185 186
  SideEffects []

  SeeAlso     []

***********************************************************************/
187
int Ssw_ManSweepNode( Ssw_Man_t * p, Aig_Obj_t * pObj, int f, int fBmc, Vec_Int_t * vPairs )
188
{
Alan Mishchenko committed
189
    Aig_Obj_t * pObjRepr, * pObjFraig, * pObjFraig2, * pObjReprFraig;
190
    int RetValue;
191
    abctime clk;
Alan Mishchenko committed
192 193 194
    // get representative of this class
    pObjRepr = Aig_ObjRepr( p->pAig, pObj );
    if ( pObjRepr == NULL )
Alan Mishchenko committed
195
        return 0;
Alan Mishchenko committed
196
    // get the fraiged node
Alan Mishchenko committed
197
    pObjFraig = Ssw_ObjFrame( p, pObj, f );
Alan Mishchenko committed
198
    // get the fraiged representative
Alan Mishchenko committed
199
    pObjReprFraig = Ssw_ObjFrame( p, pObjRepr, f );
Alan Mishchenko committed
200
    // check if constant 0 pattern distinquishes these nodes
Alan Mishchenko committed
201
    assert( pObjFraig != NULL && pObjReprFraig != NULL );
Alan Mishchenko committed
202 203 204
    assert( (pObj->fPhase == pObjRepr->fPhase) == (Aig_ObjPhaseReal(pObjFraig) == Aig_ObjPhaseReal(pObjReprFraig)) );
    // if the fraiged nodes are the same, return
    if ( Aig_Regular(pObjFraig) == Aig_Regular(pObjReprFraig) )
205
        return 0;
Alan Mishchenko committed
206 207
    // add constraints on demand
    if ( !fBmc && p->pPars->fDynamic )
Alan Mishchenko committed
208
    {
209
clk = Abc_Clock();
Alan Mishchenko committed
210 211
        Ssw_ManLoadSolver( p, pObjRepr, pObj );
        p->nRecycleCalls++;
212
p->timeMarkCones += Abc_Clock() - clk;
Alan Mishchenko committed
213
    }
Alan Mishchenko committed
214 215 216
    // call equivalence checking
    if ( Aig_Regular(pObjFraig) != Aig_ManConst1(p->pFrames) )
        RetValue = Ssw_NodesAreEquiv( p, Aig_Regular(pObjReprFraig), Aig_Regular(pObjFraig) );
Alan Mishchenko committed
217
    else
Alan Mishchenko committed
218 219 220 221 222 223
        RetValue = Ssw_NodesAreEquiv( p, Aig_Regular(pObjFraig), Aig_Regular(pObjReprFraig) );
    if ( RetValue == 1 )  // proved equivalent
    {
        pObjFraig2 = Aig_NotCond( pObjReprFraig, pObj->fPhase ^ pObjRepr->fPhase );
        Ssw_ObjSetFrame( p, pObj, f, pObjFraig2 );
        return 0;
Alan Mishchenko committed
224
    }
225 226 227 228 229
    if ( vPairs )
    {
        Vec_IntPush( vPairs, pObjRepr->Id );
        Vec_IntPush( vPairs, pObj->Id );
    }
Alan Mishchenko committed
230 231 232 233 234 235 236 237 238 239 240 241 242 243
    if ( RetValue == -1 ) // timed out
    {
        Ssw_ClassesRemoveNode( p->ppClasses, pObj );
        return 1;
    }
    // disproved the equivalence
    if ( !fBmc && p->pPars->fDynamic )
    {
        Ssw_SmlAddPatternDyn( p );
        p->nPatterns++;
        return 1;
    }
    else
        Ssw_SmlSavePatternAig( p, f );
244
    if ( !p->pPars->fConstrs )
Alan Mishchenko committed
245
        Ssw_ManResimulateWord( p, pObj, pObjRepr, f );
Alan Mishchenko committed
246
    else
Alan Mishchenko committed
247
        Ssw_ManResimulateBit( p, pObj, pObjRepr );
Alan Mishchenko committed
248
    assert( Aig_ObjRepr( p->pAig, pObj ) != pObjRepr );
Alan Mishchenko committed
249 250
    if ( Aig_ObjRepr( p->pAig, pObj ) == pObjRepr )
    {
251
        Abc_Print( 1, "Ssw_ManSweepNode(): Failed to refine representative.\n" );
Alan Mishchenko committed
252
    }
Alan Mishchenko committed
253
    return 1;
Alan Mishchenko committed
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
}

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

  Synopsis    [Performs fraiging for the internal nodes.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Ssw_ManSweepBmc( Ssw_Man_t * p )
{
    Bar_Progress_t * pProgress = NULL;
Alan Mishchenko committed
270
    Aig_Obj_t * pObj, * pObjNew, * pObjLi, * pObjLo;
271
    int i, f;
272 273
    abctime clk;
clk = Abc_Clock();
Alan Mishchenko committed
274 275 276 277

    // start initialized timeframes
    p->pFrames = Aig_ManStart( Aig_ManObjNumMax(p->pAig) * p->pPars->nFramesK );
    Saig_ManForEachLo( p->pAig, pObj, i )
Alan Mishchenko committed
278
        Ssw_ObjSetFrame( p, pObj, 0, Aig_ManConst0(p->pFrames) );
Alan Mishchenko committed
279 280 281

    // sweep internal nodes
    p->fRefined = 0;
Alan Mishchenko committed
282 283
    if ( p->pPars->fVerbose )
        pProgress = Bar_ProgressStart( stdout, Aig_ManObjNumMax(p->pAig) * p->pPars->nFramesK );
Alan Mishchenko committed
284 285 286
    for ( f = 0; f < p->pPars->nFramesK; f++ )
    {
        // map constants and PIs
Alan Mishchenko committed
287
        Ssw_ObjSetFrame( p, Aig_ManConst1(p->pAig), f, Aig_ManConst1(p->pFrames) );
Alan Mishchenko committed
288
        Saig_ManForEachPi( p->pAig, pObj, i )
289
            Ssw_ObjSetFrame( p, pObj, f, Aig_ObjCreateCi(p->pFrames) );
Alan Mishchenko committed
290 291 292
        // sweep internal nodes
        Aig_ManForEachNode( p->pAig, pObj, i )
        {
Alan Mishchenko committed
293 294
            if ( p->pPars->fVerbose )
                Bar_ProgressUpdate( pProgress, Aig_ManObjNumMax(p->pAig) * f + i, NULL );
Alan Mishchenko committed
295
            pObjNew = Aig_And( p->pFrames, Ssw_ObjChild0Fra(p, pObj, f), Ssw_ObjChild1Fra(p, pObj, f) );
Alan Mishchenko committed
296
            Ssw_ObjSetFrame( p, pObj, f, pObjNew );
297
            p->fRefined |= Ssw_ManSweepNode( p, pObj, f, 1, NULL );
Alan Mishchenko committed
298
        }
Alan Mishchenko committed
299 300 301
        // quit if this is the last timeframe
        if ( f == p->pPars->nFramesK - 1 )
            break;
302
        // transfer latch input to the latch outputs
303
        Aig_ManForEachCo( p->pAig, pObj, i )
304
            Ssw_ObjSetFrame( p, pObj, f, Ssw_ObjChild0Fra(p, pObj, f) );
Alan Mishchenko committed
305
        // build logic cones for register outputs
Alan Mishchenko committed
306
        Saig_ManForEachLiLo( p->pAig, pObjLi, pObjLo, i )
Alan Mishchenko committed
307
        {
308
            pObjNew = Ssw_ObjFrame( p, pObjLi, f );
Alan Mishchenko committed
309
            Ssw_ObjSetFrame( p, pObjLo, f+1, pObjNew );
Alan Mishchenko committed
310
            Ssw_CnfNodeAddToSolver( p->pMSat, Aig_Regular(pObjNew) );//
Alan Mishchenko committed
311
        }
Alan Mishchenko committed
312
    }
Alan Mishchenko committed
313 314
    if ( p->pPars->fVerbose )
        Bar_ProgressStop( pProgress );
Alan Mishchenko committed
315 316

    // cleanup
Alan Mishchenko committed
317
//    Ssw_ClassesCheck( p->ppClasses );
318
p->timeBmc += Abc_Clock() - clk;
Alan Mishchenko committed
319 320 321
    return p->fRefined;
}

322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342

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

  Synopsis    [Generates AIG with the following nodes put into seq miters.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Ssw_ManDumpEquivMiter( Aig_Man_t * p, Vec_Int_t * vPairs, int Num )
{
    FILE * pFile;
    char pBuffer[16];
    Aig_Man_t * pNew;
    sprintf( pBuffer, "equiv%03d.aig", Num );
    pFile = fopen( pBuffer, "w" );
    if ( pFile == NULL )
    {
343
        Abc_Print( 1, "Cannot open file %s for writing.\n", pBuffer );
344 345 346 347 348 349
        return;
    }
    fclose( pFile );
    pNew = Saig_ManCreateEquivMiter( p, vPairs );
    Ioa_WriteAiger( pNew, pBuffer, 0, 0 );
    Aig_ManStop( pNew );
350
    Abc_Print( 1, "AIG with %4d disproved equivs is dumped into file \"%s\".\n", Vec_IntSize(vPairs)/2, pBuffer );
351 352 353
}


Alan Mishchenko committed
354 355 356 357 358 359 360 361 362 363 364
/**Function*************************************************************

  Synopsis    [Performs fraiging for the internal nodes.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
365
int Ssw_ManSweep( Ssw_Man_t * p )
366
{
367
    static int Counter;
Alan Mishchenko committed
368 369
    Bar_Progress_t * pProgress = NULL;
    Aig_Obj_t * pObj, * pObj2, * pObjNew;
370
    int nConstrPairs, i, f;
371
    abctime clk;
372
    Vec_Int_t * vDisproved;
Alan Mishchenko committed
373 374

    // perform speculative reduction
375
clk = Abc_Clock();
Alan Mishchenko committed
376 377
    // create timeframes
    p->pFrames = Ssw_FramesWithClasses( p );
Alan Mishchenko committed
378
    // add constants
379
    nConstrPairs = Aig_ManCoNum(p->pFrames)-Aig_ManRegNum(p->pAig);
Alan Mishchenko committed
380 381 382
    assert( (nConstrPairs & 1) == 0 );
    for ( i = 0; i < nConstrPairs; i += 2 )
    {
383 384
        pObj  = Aig_ManCo( p->pFrames, i   );
        pObj2 = Aig_ManCo( p->pFrames, i+1 );
Alan Mishchenko committed
385
        Ssw_NodesAreConstrained( p, Aig_ObjChild0(pObj), Aig_ObjChild0(pObj2) );
Alan Mishchenko committed
386 387 388 389
    }
    // build logic cones for register inputs
    for ( i = 0; i < Aig_ManRegNum(p->pAig); i++ )
    {
390
        pObj  = Aig_ManCo( p->pFrames, nConstrPairs + i );
Alan Mishchenko committed
391
        Ssw_CnfNodeAddToSolver( p->pMSat, Aig_ObjFanin0(pObj) );//
Alan Mishchenko committed
392
    }
Alan Mishchenko committed
393
    sat_solver_simplify( p->pMSat->pSat );
Alan Mishchenko committed
394

Alan Mishchenko committed
395 396
    // map constants and PIs of the last frame
    f = p->pPars->nFramesK;
Alan Mishchenko committed
397
    Ssw_ObjSetFrame( p, Aig_ManConst1(p->pAig), f, Aig_ManConst1(p->pFrames) );
Alan Mishchenko committed
398
    Saig_ManForEachPi( p->pAig, pObj, i )
399
        Ssw_ObjSetFrame( p, pObj, f, Aig_ObjCreateCi(p->pFrames) );
400
p->timeReduce += Abc_Clock() - clk;
Alan Mishchenko committed
401

Alan Mishchenko committed
402 403
    // sweep internal nodes
    p->fRefined = 0;
Alan Mishchenko committed
404
    Ssw_ClassesClearRefined( p->ppClasses );
Alan Mishchenko committed
405 406
    if ( p->pPars->fVerbose )
        pProgress = Bar_ProgressStart( stdout, Aig_ManObjNumMax(p->pAig) );
407
    vDisproved = p->pPars->fEquivDump? Vec_IntAlloc(1000) : NULL;
Alan Mishchenko committed
408
    Aig_ManForEachObj( p->pAig, pObj, i )
Alan Mishchenko committed
409
    {
Alan Mishchenko committed
410 411 412
        if ( p->pPars->fVerbose )
            Bar_ProgressUpdate( pProgress, i, NULL );
        if ( Saig_ObjIsLo(p->pAig, pObj) )
413
            p->fRefined |= Ssw_ManSweepNode( p, pObj, f, 0, vDisproved );
Alan Mishchenko committed
414
        else if ( Aig_ObjIsNode(pObj) )
415
        {
Alan Mishchenko committed
416
            pObjNew = Aig_And( p->pFrames, Ssw_ObjChild0Fra(p, pObj, f), Ssw_ObjChild1Fra(p, pObj, f) );
Alan Mishchenko committed
417
            Ssw_ObjSetFrame( p, pObj, f, pObjNew );
418
            p->fRefined |= Ssw_ManSweepNode( p, pObj, f, 0, vDisproved );
Alan Mishchenko committed
419
        }
Alan Mishchenko committed
420
    }
Alan Mishchenko committed
421 422
    if ( p->pPars->fVerbose )
        Bar_ProgressStop( pProgress );
Alan Mishchenko committed
423 424

    // cleanup
Alan Mishchenko committed
425
//    Ssw_ClassesCheck( p->ppClasses );
426 427 428
    if ( p->pPars->fEquivDump )
        Ssw_ManDumpEquivMiter( p->pAig, vDisproved, Counter++ );
    Vec_IntFreeP( &vDisproved );
Alan Mishchenko committed
429 430 431 432 433 434 435 436
    return p->fRefined;
}

////////////////////////////////////////////////////////////////////////
///                       END OF FILE                                ///
////////////////////////////////////////////////////////////////////////


437
ABC_NAMESPACE_IMPL_END