summaryrefslogtreecommitdiff
path: root/src/sat/bmc/bmcCexMin2.c
blob: b39b8eec1230e6daafc6e080657b052f2bd6d307 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/**CFile****************************************************************

  FileName    [bmcCexMin2.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [SAT-based bounded model checking.]

  Synopsis    [CEX minimization.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

  Date        [Ver. 1.0. Started - June 20, 2005.]

  Revision    [$Id: bmcCexMin2.c,v 1.00 2005/06/20 00:00:00 alanmi Exp $]

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

#include "aig/gia/gia.h"
#include "bmc.h"

ABC_NAMESPACE_IMPL_START


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

static inline int  Abc_InfoGet2Bits( Vec_Int_t * vData, int nWords, int iFrame, int iObj )
{
    unsigned * pInfo = (unsigned *)Vec_IntEntryP( vData, nWords * iFrame );
    return 3 & (pInfo[iObj >> 4] >> ((iObj & 15) << 1));
}
static inline void Abc_InfoSet2Bits( Vec_Int_t * vData, int nWords, int iFrame, int iObj, int Value )
{
    unsigned * pInfo = (unsigned *)Vec_IntEntryP( vData, nWords * iFrame );
    Value ^= (3 & (pInfo[iObj >> 4] >> ((iObj & 15) << 1)));
    pInfo[iObj >> 4] ^= (Value << ((iObj & 15) << 1));
}
static inline void Abc_InfoAdd2Bits( Vec_Int_t * vData, int nWords, int iFrame, int iObj, int Value )
{
    unsigned * pInfo = (unsigned *)Vec_IntEntryP( vData, nWords * iFrame );
    pInfo[iObj >> 4] |= (Value << ((iObj & 15) << 1));
}

static inline int  Gia_ManGetTwo( Gia_Man_t * p, int iFrame, Gia_Obj_t * pObj )              { return Abc_InfoGet2Bits( p->vTruths, p->nTtWords, iFrame, Gia_ObjId(p, pObj) ); }
static inline void Gia_ManSetTwo( Gia_Man_t * p, int iFrame, Gia_Obj_t * pObj, int Value )   { Abc_InfoSet2Bits( p->vTruths, p->nTtWords, iFrame, Gia_ObjId(p, pObj), Value ); }
static inline void Gia_ManAddTwo( Gia_Man_t * p, int iFrame, Gia_Obj_t * pObj, int Value )   { Abc_InfoAdd2Bits( p->vTruths, p->nTtWords, iFrame, Gia_ObjId(p, pObj), Value ); }

/*
    For CEX minimization, all terminals (const0, PI, RO in frame0) have equal priority.
    For abstraction refinement, all terminals, except PPis, have higher priority.
*/

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

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

  Synopsis    [Annotates the unrolling with CEX value/priority.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Gia_ManAnnotateUnrolling( Gia_Man_t * p, Abc_Cex_t * pCex, int fJustMax )
{
    Gia_Obj_t * pObj, * pObjRi, * pObjRo;
    int RetValue, f, k, Value, Value0, Value1, iBit;
    // start storage for internal info
    assert( p->vTruths == NULL );
    p->nTtWords = Abc_BitWordNum( 2 * Gia_ManObjNum(p) );
    p->vTruths  = Vec_IntStart( (pCex->iFrame + 1) * p->nTtWords );
    // assign values to all objects (const0 and RO in frame0 are assigned 0)
    Gia_ManCleanMark0(p);
    for ( f = 0, iBit = pCex->nRegs; f <= pCex->iFrame; f++ )
    {
        Gia_ManForEachPi( p, pObj, k )
            if ( (pObj->fMark0 = Abc_InfoHasBit(pCex->pData, iBit++)) )
                Gia_ManAddTwo( p, f, pObj, 1 );
        Gia_ManForEachAnd( p, pObj, k )
            if ( (pObj->fMark0 = (Gia_ObjFanin0(pObj)->fMark0 ^ Gia_ObjFaninC0(pObj)) & (Gia_ObjFanin1(pObj)->fMark0 ^ Gia_ObjFaninC1(pObj))) )
                Gia_ManAddTwo( p, f, pObj, 1 );
        Gia_ManForEachCo( p, pObj, k )
            if ( (pObj->fMark0 = Gia_ObjFanin0(pObj)->fMark0 ^ Gia_ObjFaninC0(pObj)) )
                Gia_ManAddTwo( p, f, pObj, 1 );
        if ( f == pCex->iFrame )
            break;
        Gia_ManForEachRiRo( p, pObjRi, pObjRo, k )
            if ( (pObjRo->fMark0 = pObjRi->fMark0) )
                Gia_ManAddTwo( p, f+1, pObjRo, 1 );
    }
    assert( iBit == pCex->nBits );
    // check the output value
    RetValue = Gia_ManPo(p, pCex->iPo)->fMark0;
    if ( RetValue != 1 )
        printf( "Counter-example is invalid.\n" );
    // assign justification to nodes
    Gia_ManCleanMark0(p);
    pObj = Gia_ManPo(p, pCex->iPo);
    pObj->fMark0 = 1;
    Gia_ManAddTwo( p, pCex->iFrame, pObj, 2 );
    for ( f = pCex->iFrame; f >= 0; f-- )
    {
        // transfer to CO drivers
        Gia_ManForEachCo( p, pObj, k )
            if ( (Gia_ObjFanin0(pObj)->fMark0 = pObj->fMark0) )
            {
                pObj->fMark0 = 0;
                Gia_ManAddTwo( p, f, Gia_ObjFanin0(pObj), 2 );
            }
        // compute justification
        Gia_ManForEachAndReverse( p, pObj, k )
        {
            if ( !pObj->fMark0 ) 
                continue;
            pObj->fMark0 = 0;
            Value = (1 & Gia_ManGetTwo(p, f, pObj));
            Value0 = (1 & Gia_ManGetTwo(p, f, Gia_ObjFanin0(pObj))) ^ Gia_ObjFaninC0(pObj);
            Value1 = (1 & Gia_ManGetTwo(p, f, Gia_ObjFanin1(pObj))) ^ Gia_ObjFaninC1(pObj);
            if ( Value0 == Value1 )
            {
                assert( Value == (Value0 & Value1) );
                if ( fJustMax || Value == 1 )
                {
                    Gia_ObjFanin0(pObj)->fMark0 = Gia_ObjFanin1(pObj)->fMark0 = 1;
                    Gia_ManAddTwo( p, f, Gia_ObjFanin0(pObj), 2 );
                    Gia_ManAddTwo( p, f, Gia_ObjFanin1(pObj), 2 );
                }
                else
                {
                    Gia_ObjFanin0(pObj)->fMark0 = 1;
                    Gia_ManAddTwo( p, f, Gia_ObjFanin0(pObj), 2 );
                }
            }
            else if ( Value0 == 0 )
            {
                assert( Value == 0 );
                Gia_ObjFanin0(pObj)->fMark0 = 1;
                Gia_ManAddTwo( p, f, Gia_ObjFanin0(pObj), 2 );
            }
            else if ( Value1 == 0 )
            {
                assert( Value == 0 );
                Gia_ObjFanin1(pObj)->fMark0 = 1;
                Gia_ManAddTwo( p, f, Gia_ObjFanin1(pObj), 2 );
            }
            else assert( 0 );
        }
        if ( f == 0 )
            break;
        // transfer to RIs
        Gia_ManForEachPi( p, pObj, k )
            pObj->fMark0 = 0;
        Gia_ManForEachRiRo( p, pObjRi, pObjRo, k )
            if ( (pObjRi->fMark0 = pObjRo->fMark0) )
            {
                pObjRo->fMark0 = 0;
                Gia_ManAddTwo( p, f-1, pObjRi, 2 );
            }
    }
    Gia_ManCleanMark0(p);
    return RetValue;
}

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

  Synopsis    [Computing AIG characterizing all justifying assignments.]

  Description [Used in CEX minimization.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Gia_Man_t * Gia_ManCreateUnate( Gia_Man_t * p, Abc_Cex_t * pCex, int iFrame, int nRealPis, int fUseAllObjects )
{
    Gia_Man_t * pNew, * pTemp;
    Gia_Obj_t * pObj, * pObjRo, * pObjRi;
    int f, k, Value;
    assert( iFrame >= 0 && iFrame <= pCex->iFrame );
    pNew = Gia_ManStart( 1000 );
    pNew->pName = Abc_UtilStrsav( "unate" );
    Gia_ManCleanValue( p );
    // set flop outputs
    if ( nRealPis < 0 ) // CEX min
    {
        Gia_ManForEachRo( p, pObj, k )
        {
            if ( fUseAllObjects )
            {
                int Value = Gia_ManAppendCi(pNew);
                if ( (Gia_ManGetTwo(p, iFrame, pObj) >> 1) ) // in the path
                    pObj->Value = Value;
            }
            else if ( (Gia_ManGetTwo(p, iFrame, pObj) >> 1) ) // in the path
                pObj->Value = Gia_ManAppendCi(pNew);
        }
    }
    else
    {
        Gia_ManForEachRo( p, pObj, k )
            pObj->Value = (Gia_ManGetTwo(p, iFrame, pObj) >> 1);
    }
    Gia_ManHashAlloc( pNew );
    for ( f = iFrame; f <= pCex->iFrame; f++ )
    {
/*
        printf( "  F%03d ", f );
        Gia_ManForEachRo( p, pObj, k )
            printf( "%d", pObj->Value > 0 );
        printf( "\n" );
*/
        // set const0 to const1 if present
        pObj = Gia_ManConst0(p);
        pObj->Value = (Gia_ManGetTwo(p, f, pObj) >> 1);
        // set primary inputs 
        if ( nRealPis < 0 ) // CEX min
        {
            Gia_ManForEachPi( p, pObj, k )
                pObj->Value = (Gia_ManGetTwo(p, f, pObj) >> 1);
        }
        else
        {
            Gia_ManForEachPi( p, pObj, k )
            {
                pObj->Value = (Gia_ManGetTwo(p, f, pObj) >> 1);
                if ( k >= nRealPis )
                {
                    if ( fUseAllObjects )
                    {
                        int Value = Gia_ManAppendCi(pNew);
                        if ( (Gia_ManGetTwo(p, f, pObj) >> 1) ) // in the path
                            pObj->Value = Value;
                    }
                    else if ( (Gia_ManGetTwo(p, f, pObj) >> 1) ) // in the path
                        pObj->Value = Gia_ManAppendCi(pNew);
                }
            }
        }
        // traverse internal nodes
        Gia_ManForEachAnd( p, pObj, k )
        { 
            pObj->Value = 0;
            Value = Gia_ManGetTwo(p, f, pObj);
            if ( !(Value >> 1) ) // not in the path
                continue;
            if ( Gia_ObjFanin0(pObj)->Value && Gia_ObjFanin1(pObj)->Value )
            {
                if ( 1 & Gia_ManGetTwo(p, f, pObj) ) // value 1
                {
                    if ( Gia_ObjFanin0(pObj)->Value > 1 && Gia_ObjFanin1(pObj)->Value > 1 )
                        pObj->Value = Gia_ManHashAnd( pNew, Gia_ObjFanin0(pObj)->Value, Gia_ObjFanin1(pObj)->Value );
                    else if ( Gia_ObjFanin0(pObj)->Value > 1 )
                        pObj->Value = Gia_ObjFanin0(pObj)->Value;
                    else if ( Gia_ObjFanin1(pObj)->Value > 1 )
                        pObj->Value = Gia_ObjFanin1(pObj)->Value;
                    else 
                        pObj->Value = 1;
                }
                else // value 0
                {
                    if ( Gia_ObjFanin0(pObj)->Value > 1 && Gia_ObjFanin1(pObj)->Value > 1 )
                        pObj->Value = Gia_ManHashOr( pNew, Gia_ObjFanin0(pObj)->Value, Gia_ObjFanin1(pObj)->Value );
                    else if ( Gia_ObjFanin0(pObj)->Value > 1 )
                        pObj->Value = Gia_ObjFanin0(pObj)->Value;
                    else if ( Gia_ObjFanin1(pObj)->Value > 1 )
                        pObj->Value = Gia_ObjFanin1(pObj)->Value;
                    else 
                        pObj->Value = 1;
                }
            }
            else if ( Gia_ObjFanin0(pObj)->Value )
                pObj->Value = Gia_ObjFanin0(pObj)->Value;
            else if ( Gia_ObjFanin1(pObj)->Value )
                pObj->Value = Gia_ObjFanin1(pObj)->Value;
            else assert( 0 );
        }
        Gia_ManForEachCo( p, pObj, k )
            pObj->Value = Gia_ObjFanin0(pObj)->Value;
        if ( f == pCex->iFrame )
            break;
        Gia_ManForEachRiRo( p, pObjRi, pObjRo, k )
            pObjRo->Value = pObjRi->Value;
    }
    Gia_ManHashStop( pNew );
    // create primary output
    pObj = Gia_ManPo(p, pCex->iPo);
    assert( (Gia_ManGetTwo(p, pCex->iFrame, pObj) >> 1) );
    assert( pObj->Value );
    Gia_ManAppendCo( pNew, pObj->Value );
    // cleanup
    pNew = Gia_ManCleanup( pTemp = pNew );
    Gia_ManStop( pTemp );
    return pNew;
}


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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Abc_Cex_t * Gia_ManCexMin( Gia_Man_t * p, Abc_Cex_t * pCex, int iFrameStart, int nRealPis, int fJustMax, int fUseAll, int fVerbose )
{
    Gia_Man_t * pNew;
    int f, RetValue;
    // check CEX
    assert( pCex->nPis == Gia_ManPiNum(p) );
//    assert( pCex->nRegs == Gia_ManRegNum(p) );
    assert( pCex->iPo < Gia_ManPoNum(p) );
    // check frames
    assert( iFrameStart >= 0 && iFrameStart <= pCex->iFrame );
    // check primary inputs
    assert( nRealPis < Gia_ManPiNum(p) );
    // prepare
    RetValue = Gia_ManAnnotateUnrolling( p, pCex, fJustMax );
    if ( nRealPis >= 0 ) // refinement
    {
        pNew = Gia_ManCreateUnate( p, pCex, iFrameStart, nRealPis, fUseAll );
        printf( "%3d : ", iFrameStart );
        Gia_ManPrintStats( pNew, NULL );
        if ( fVerbose )
            Gia_AigerWrite( pNew, "temp.aig", 0, 0, 0 );
        Gia_ManStop( pNew );
    }
    else // CEX min
    {
        for ( f = pCex->iFrame; f >= iFrameStart; f-- )
        {
            pNew = Gia_ManCreateUnate( p, pCex, f, -1, fUseAll );
            printf( "%3d : ", f );
            Gia_ManPrintStats( pNew, NULL );
            if ( fVerbose )
                Gia_AigerWrite( pNew, "temp.aig", 0, 0, 0 );
            Gia_ManStop( pNew );
        }
    }
    Vec_IntFreeP( &p->vTruths );
    p->nTtWords = 0;
    return NULL;
}

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


ABC_NAMESPACE_IMPL_END