abc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
sfmSat.c
Go to the documentation of this file.
1 /**CFile****************************************************************
2 
3  FileName [sfmSat.c]
4 
5  SystemName [ABC: Logic synthesis and verification system.]
6 
7  PackageName [SAT-based optimization using internal don't-cares.]
8 
9  Synopsis [SAT-based procedures.]
10 
11  Author [Alan Mishchenko]
12 
13  Affiliation [UC Berkeley]
14 
15  Date [Ver. 1.0. Started - June 20, 2005.]
16 
17  Revision [$Id: sfmSat.c,v 1.00 2005/06/20 00:00:00 alanmi Exp $]
18 
19 ***********************************************************************/
20 
21 #include "sfmInt.h"
22 
24 
25 
26 ////////////////////////////////////////////////////////////////////////
27 /// DECLARATIONS ///
28 ////////////////////////////////////////////////////////////////////////
29 
30 static word s_Truths6[6] = {
31  ABC_CONST(0xAAAAAAAAAAAAAAAA),
32  ABC_CONST(0xCCCCCCCCCCCCCCCC),
33  ABC_CONST(0xF0F0F0F0F0F0F0F0),
34  ABC_CONST(0xFF00FF00FF00FF00),
35  ABC_CONST(0xFFFF0000FFFF0000),
36  ABC_CONST(0xFFFFFFFF00000000)
37 };
38 
39 ////////////////////////////////////////////////////////////////////////
40 /// FUNCTION DEFINITIONS ///
41 ////////////////////////////////////////////////////////////////////////
42 
43 /**Function*************************************************************
44 
45  Synopsis [Converts a window into a SAT solver.]
46 
47  Description []
48 
49  SideEffects []
50 
51  SeeAlso []
52 
53 ***********************************************************************/
55 {
56  // p->vOrder contains all variables in the window in a good order
57  // p->vDivs is a subset of nodes in p->vOrder used as divisor candidates
58  // p->vTfo contains TFO of the node (does not include node)
59  // p->vRoots contains roots of the TFO of the node (may include node)
60  Vec_Int_t * vClause;
61  int RetValue, iNode = -1, iFanin, i, k;
62  abctime clk = Abc_Clock();
63 // if ( p->pSat )
64 // printf( "%d ", p->pSat->stats.learnts );
65  sat_solver_restart( p->pSat );
66  sat_solver_setnvars( p->pSat, 1 + Vec_IntSize(p->vOrder) + Vec_IntSize(p->vTfo) + Vec_IntSize(p->vRoots) + 10 );
67  // create SAT variables
68  Sfm_NtkCleanVars( p );
69  p->nSatVars = 1;
70  Vec_IntForEachEntry( p->vOrder, iNode, i )
71  Sfm_ObjSetSatVar( p, iNode, p->nSatVars++ );
72  // collect divisor variables
73  Vec_IntClear( p->vDivVars );
74  Vec_IntForEachEntry( p->vDivs, iNode, i )
75  Vec_IntPush( p->vDivVars, Sfm_ObjSatVar(p, iNode) );
76  // add CNF clauses for the TFI
77  Vec_IntForEachEntry( p->vOrder, iNode, i )
78  {
79  if ( Sfm_ObjIsPi(p, iNode) )
80  continue;
81  // collect fanin variables
82  Vec_IntClear( p->vFaninMap );
83  Sfm_ObjForEachFanin( p, iNode, iFanin, k )
84  Vec_IntPush( p->vFaninMap, Sfm_ObjSatVar(p, iFanin) );
85  Vec_IntPush( p->vFaninMap, Sfm_ObjSatVar(p, iNode) );
86  // generate CNF
87  Sfm_TranslateCnf( p->vClauses, (Vec_Str_t *)Vec_WecEntry(p->vCnfs, iNode), p->vFaninMap, -1 );
88  // add clauses
89  Vec_WecForEachLevel( p->vClauses, vClause, k )
90  {
91  if ( Vec_IntSize(vClause) == 0 )
92  break;
93  RetValue = sat_solver_addclause( p->pSat, Vec_IntArray(vClause), Vec_IntArray(vClause) + Vec_IntSize(vClause) );
94  if ( RetValue == 0 )
95  return 0;
96  }
97  }
98  if ( Vec_IntSize(p->vTfo) > 0 )
99  {
100  assert( p->pPars->nTfoLevMax > 0 );
101  assert( Vec_IntSize(p->vRoots) > 0 );
102  assert( Vec_IntEntry(p->vTfo, 0) != p->iPivotNode );
103  // collect variables of root nodes
104  Vec_IntClear( p->vLits );
105  Vec_IntForEachEntry( p->vRoots, iNode, i )
106  Vec_IntPush( p->vLits, Sfm_ObjSatVar(p, iNode) );
107  // assign new variables to the TFO nodes
108  Vec_IntForEachEntry( p->vTfo, iNode, i )
109  {
110  Sfm_ObjCleanSatVar( p, Sfm_ObjSatVar(p, iNode) );
111  Sfm_ObjSetSatVar( p, iNode, p->nSatVars++ );
112  }
113  // add CNF clauses for the TFO
114  Vec_IntForEachEntry( p->vTfo, iNode, i )
115  {
116  assert( Sfm_ObjIsNode(p, iNode) );
117  // collect fanin variables
118  Vec_IntClear( p->vFaninMap );
119  Sfm_ObjForEachFanin( p, iNode, iFanin, k )
120  Vec_IntPush( p->vFaninMap, Sfm_ObjSatVar(p, iFanin) );
121  Vec_IntPush( p->vFaninMap, Sfm_ObjSatVar(p, iNode) );
122  // generate CNF
123  Sfm_TranslateCnf( p->vClauses, (Vec_Str_t *)Vec_WecEntry(p->vCnfs, iNode), p->vFaninMap, Sfm_ObjSatVar(p, p->iPivotNode) );
124  // add clauses
125  Vec_WecForEachLevel( p->vClauses, vClause, k )
126  {
127  if ( Vec_IntSize(vClause) == 0 )
128  break;
129  RetValue = sat_solver_addclause( p->pSat, Vec_IntArray(vClause), Vec_IntArray(vClause) + Vec_IntSize(vClause) );
130  if ( RetValue == 0 )
131  return 0;
132  }
133  }
134  // create XOR clauses for the roots
135  Vec_IntForEachEntry( p->vRoots, iNode, i )
136  {
137  sat_solver_add_xor( p->pSat, Vec_IntEntry(p->vLits, i), Sfm_ObjSatVar(p, iNode), p->nSatVars++, 0 );
138  Vec_IntWriteEntry( p->vLits, i, Abc_Var2Lit(p->nSatVars-1, 0) );
139  }
140  // make OR clause for the last nRoots variables
141  RetValue = sat_solver_addclause( p->pSat, Vec_IntArray(p->vLits), Vec_IntArray(p->vLits) + Vec_IntSize(p->vLits) );
142  if ( RetValue == 0 )
143  return 0;
144  }
145  // finalize
146  RetValue = sat_solver_simplify( p->pSat );
147  p->timeCnf += Abc_Clock() - clk;
148  return RetValue;
149 }
150 
151 /**Function*************************************************************
152 
153  Synopsis [Takes SAT solver and returns interpolant.]
154 
155  Description [If interpolant does not exist, records difference variables.]
156 
157  SideEffects []
158 
159  SeeAlso []
160 
161 ***********************************************************************/
163 {
164  word * pSign, uCube, uTruth = 0;
165  int status, i, Div, iVar, nFinal, * pFinal, nIter = 0;
166  int pLits[2], nVars = sat_solver_nvars( p->pSat );
167  sat_solver_setnvars( p->pSat, nVars + 1 );
168  pLits[0] = Abc_Var2Lit( Sfm_ObjSatVar(p, p->iPivotNode), 0 ); // F = 1
169  pLits[1] = Abc_Var2Lit( nVars, 0 ); // iNewLit
170  while ( 1 )
171  {
172  // find onset minterm
173  p->nSatCalls++;
174  status = sat_solver_solve( p->pSat, pLits, pLits + 2, p->pPars->nBTLimit, 0, 0, 0 );
175  if ( status == l_Undef )
176  return SFM_SAT_UNDEC;
177  if ( status == l_False )
178  return uTruth;
179  assert( status == l_True );
180  // remember variable values
181  Vec_IntClear( p->vValues );
182  Vec_IntForEachEntry( p->vDivVars, iVar, i )
183  Vec_IntPush( p->vValues, sat_solver_var_value(p->pSat, iVar) );
184  // collect divisor literals
185  Vec_IntClear( p->vLits );
186  Vec_IntPush( p->vLits, Abc_LitNot(pLits[0]) ); // F = 0
187  Vec_IntForEachEntry( p->vDivIds, Div, i )
188  Vec_IntPush( p->vLits, sat_solver_var_literal(p->pSat, Div) );
189  // check against offset
190  p->nSatCalls++;
191  status = sat_solver_solve( p->pSat, Vec_IntArray(p->vLits), Vec_IntArray(p->vLits) + Vec_IntSize(p->vLits), p->pPars->nBTLimit, 0, 0, 0 );
192  if ( status == l_Undef )
193  return SFM_SAT_UNDEC;
194  if ( status == l_True )
195  break;
196  assert( status == l_False );
197  // compute cube and add clause
198  nFinal = sat_solver_final( p->pSat, &pFinal );
199  uCube = ~(word)0;
200  Vec_IntClear( p->vLits );
201  Vec_IntPush( p->vLits, Abc_LitNot(pLits[1]) ); // NOT(iNewLit)
202  for ( i = 0; i < nFinal; i++ )
203  {
204  if ( pFinal[i] == pLits[0] )
205  continue;
206  Vec_IntPush( p->vLits, pFinal[i] );
207  iVar = Vec_IntFind( p->vDivIds, Abc_Lit2Var(pFinal[i]) ); assert( iVar >= 0 );
208  uCube &= Abc_LitIsCompl(pFinal[i]) ? s_Truths6[iVar] : ~s_Truths6[iVar];
209  }
210  uTruth |= uCube;
211  status = sat_solver_addclause( p->pSat, Vec_IntArray(p->vLits), Vec_IntArray(p->vLits) + Vec_IntSize(p->vLits) );
212  assert( status );
213  nIter++;
214  }
215  assert( status == l_True );
216  // store the counter-example
217  Vec_IntForEachEntry( p->vDivVars, iVar, i )
218  if ( Vec_IntEntry(p->vValues, i) ^ sat_solver_var_value(p->pSat, iVar) ) // insert 1
219  {
220  pSign = Vec_WrdEntryP( p->vDivCexes, i );
221  assert( !Abc_InfoHasBit( (unsigned *)pSign, p->nCexes) );
222  Abc_InfoXorBit( (unsigned *)pSign, p->nCexes );
223  }
224  p->nCexes++;
225  return SFM_SAT_SAT;
226 }
227 
228 /**Function*************************************************************
229 
230  Synopsis [Checks resubstitution.]
231 
232  Description []
233 
234  SideEffects []
235 
236  SeeAlso []
237 
238 ***********************************************************************/
240 {
241  int iNode = 3;
242  int iDiv0 = 6;
243  int iDiv1 = 7;
244  word uTruth;
245 // int i;
246 // Sfm_NtkForEachNode( p, i )
247  {
248  Sfm_NtkCreateWindow( p, iNode, 1 );
250 
251  // collect SAT variables of divisors
252  Vec_IntClear( p->vDivIds );
253  Vec_IntPush( p->vDivIds, Sfm_ObjSatVar(p, iDiv0) );
254  Vec_IntPush( p->vDivIds, Sfm_ObjSatVar(p, iDiv1) );
255 
256  uTruth = Sfm_ComputeInterpolant( p );
257 
258  if ( uTruth == SFM_SAT_SAT )
259  printf( "The problem is SAT.\n" );
260  else if ( uTruth == SFM_SAT_UNDEC )
261  printf( "The problem is UNDEC.\n" );
262  else
263  Kit_DsdPrintFromTruth( (unsigned *)&uTruth, 2 ); printf( "\n" );
264  }
265 }
266 
267 ////////////////////////////////////////////////////////////////////////
268 /// END OF FILE ///
269 ////////////////////////////////////////////////////////////////////////
270 
271 
273 
static int * Vec_IntArray(Vec_Int_t *p)
Definition: vecInt.h:328
#define Sfm_ObjForEachFanin(p, Node, Fan, i)
Definition: sfmInt.h:172
static ABC_NAMESPACE_IMPL_START word s_Truths6[6]
DECLARATIONS ///.
Definition: sfmSat.c:30
static Llb_Mgr_t * p
Definition: llb3Image.c:950
int sat_solver_addclause(sat_solver *s, lit *begin, lit *end)
Definition: satSolver.c:1492
typedefABC_NAMESPACE_IMPL_START struct Vec_Int_t_ Vec_Int_t
DECLARATIONS ///.
Definition: bblif.c:37
static int Abc_InfoHasBit(unsigned *p, int i)
Definition: abc_global.h:258
#define l_Undef
Definition: SolverTypes.h:86
int sat_solver_solve(sat_solver *s, lit *begin, lit *end, ABC_INT64_T nConfLimit, ABC_INT64_T nInsLimit, ABC_INT64_T nConfLimitGlobal, ABC_INT64_T nInsLimitGlobal)
Definition: satSolver.c:1700
static void Abc_InfoXorBit(unsigned *p, int i)
Definition: abc_global.h:260
static int Abc_Var2Lit(int Var, int fCompl)
Definition: abc_global.h:263
static int Vec_IntFind(Vec_Int_t *p, int Entry)
Definition: vecInt.h:895
static void Sfm_NtkCleanVars(Sfm_Ntk_t *p)
Definition: sfmInt.h:151
#define l_True
Definition: SolverTypes.h:84
static int sat_solver_add_xor(sat_solver *pSat, int iVarA, int iVarB, int iVarC, int fCompl)
Definition: satSolver.h:346
typedefABC_NAMESPACE_HEADER_START struct Sfm_Ntk_t_ Sfm_Ntk_t
INCLUDES ///.
Definition: sfm.h:41
static void Sfm_ObjCleanSatVar(Sfm_Ntk_t *p, int Num)
Definition: sfmInt.h:150
void Kit_DsdPrintFromTruth(unsigned *pTruth, int nVars)
Definition: kitDsd.c:490
static abctime Abc_Clock()
Definition: abc_global.h:279
static int sat_solver_var_value(sat_solver *s, int v)
Definition: satSolver.h:200
static int Sfm_ObjIsNode(Sfm_Ntk_t *p, int i)
Definition: sfmInt.h:131
static int sat_solver_final(sat_solver *s, int **ppArray)
Definition: satSolver.h:227
int Sfm_NtkCreateWindow(Sfm_Ntk_t *p, int iNode, int fVerbose)
Definition: sfmWin.c:334
static void Vec_IntWriteEntry(Vec_Int_t *p, int i, int Entry)
Definition: bblif.c:285
static int Abc_LitIsCompl(int Lit)
Definition: abc_global.h:265
static int Sfm_ObjIsPi(Sfm_Ntk_t *p, int i)
Definition: sfmInt.h:129
#define Vec_WecForEachLevel(vGlob, vVec, i)
MACRO DEFINITIONS ///.
Definition: vecWec.h:55
static int Sfm_ObjSatVar(Sfm_Ntk_t *p, int iObj)
Definition: sfmInt.h:148
void sat_solver_setnvars(sat_solver *s, int n)
Definition: satSolver.c:1072
static int Vec_IntEntry(Vec_Int_t *p, int i)
Definition: bblif.c:268
unsigned __int64 word
DECLARATIONS ///.
Definition: kitPerm.c:36
int sat_solver_nvars(sat_solver *s)
Definition: satSolver.c:1896
#define ABC_NAMESPACE_IMPL_END
Definition: abc_global.h:108
#define SFM_SAT_SAT
Definition: sfmInt.h:46
static void Vec_IntPush(Vec_Int_t *p, int Entry)
Definition: bblif.c:468
#define ABC_NAMESPACE_IMPL_START
Definition: abc_global.h:107
word Sfm_ComputeInterpolant(Sfm_Ntk_t *p)
Definition: sfmSat.c:162
static Vec_Int_t * Vec_WecEntry(Vec_Wec_t *p, int i)
Definition: vecWec.h:142
static int Abc_LitNot(int Lit)
Definition: abc_global.h:266
int sat_solver_simplify(sat_solver *s)
Definition: satSolver.c:1276
static int Vec_IntSize(Vec_Int_t *p)
Definition: bblif.c:252
void Sfm_ComputeInterpolantCheck(Sfm_Ntk_t *p)
Definition: sfmSat.c:239
static void Sfm_ObjSetSatVar(Sfm_Ntk_t *p, int iObj, int Num)
Definition: sfmInt.h:149
#define ABC_CONST(number)
PARAMETERS ///.
Definition: abc_global.h:206
static int sat_solver_var_literal(sat_solver *s, int v)
Definition: satSolver.h:205
void Sfm_TranslateCnf(Vec_Wec_t *vRes, Vec_Str_t *vCnf, Vec_Int_t *vFaninMap, int iPivotVar)
Definition: sfmCnf.c:153
static int Abc_Lit2Var(int Lit)
Definition: abc_global.h:264
#define l_False
Definition: SolverTypes.h:85
int Sfm_NtkWindowToSolver(Sfm_Ntk_t *p)
FUNCTION DEFINITIONS ///.
Definition: sfmSat.c:54
#define assert(ex)
Definition: util_old.h:213
static void Vec_IntClear(Vec_Int_t *p)
Definition: bblif.c:452
ABC_INT64_T abctime
Definition: abc_global.h:278
#define SFM_SAT_UNDEC
Definition: sfmInt.h:45
void sat_solver_restart(sat_solver *s)
Definition: satSolver.c:1186
#define Vec_IntForEachEntry(vVec, Entry, i)
MACRO DEFINITIONS ///.
Definition: vecInt.h:54
static word * Vec_WrdEntryP(Vec_Wrd_t *p, int i)
Definition: vecWrd.h:401