abc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
abcBm.c
Go to the documentation of this file.
1 /**CFile****************************************************************
2 
3  FileName [bm.c]
4 
5  SystemName [ABC: Logic synthesis and verification system.]
6 
7  PackageName [Boolean Matching package.]
8 
9  Synopsis [Check P-equivalence and PP-equivalence of two circuits.]
10 
11  Author [Hadi Katebi]
12 
13  Affiliation [University of Michigan]
14 
15  Date [Ver. 1.0. Started - January, 2009.]
16 
17  Revision [No revisions so far]
18 
19  Comments [This is the cleaned up version of the code I used for DATE 2010 publication.]
20  [If you have any question or if you find a bug, contact me at hadik@umich.edu.]
21  [I don't guarantee that I can fix all the bugs, but I can definitely point you to
22  the right direction so you can fix the bugs yourself].
23 
24  Debugging [There are some part of the code that are commented out. Those parts mostly print
25  the contents of the data structures to the standard output. Un-comment them if you
26  find them useful for debugging.]
27 
28 ***********************************************************************/
29 
30 #include "base/abc/abc.h"
31 #include "opt/sim/sim.h"
32 #include "sat/bsat/satSolver.h"
33 #include "misc/extra/extraBdd.h"
34 
36 
37 
38 int match1by1(Abc_Ntk_t * pNtk1, Vec_Ptr_t ** nodesInLevel1, Vec_Int_t ** iMatch1, Vec_Int_t ** iDep1, Vec_Int_t * matchedInputs1, int * iGroup1, Vec_Int_t ** oMatch1, int * oGroup1,
39  Abc_Ntk_t * pNtk2, Vec_Ptr_t ** nodesInLevel2, Vec_Int_t ** iMatch2, Vec_Int_t ** iDep2, Vec_Int_t * matchedInputs2, int * iGroup2, Vec_Int_t ** oMatch2, int * oGroup2,
40  Vec_Int_t * matchedOutputs1, Vec_Int_t * matchedOutputs2, Vec_Int_t * oMatchedGroups, Vec_Int_t * iNonSingleton, int ii, int idx);
41 
42 int Abc_NtkBmSat( Abc_Ntk_t * pNtk1, Abc_Ntk_t * pNtk2, Vec_Ptr_t * iMatchPairs, Vec_Ptr_t * oMatchPairs, Vec_Int_t * mismatch, int mode);
43 
44 void getDependencies(Abc_Ntk_t *pNtk, Vec_Int_t** iDep, Vec_Int_t** oDep)
45 {
46  Vec_Ptr_t * vSuppFun;
47  int i, j;
48 
49  vSuppFun = Sim_ComputeFunSupp(pNtk, 0);
50  for(i = 0; i < Abc_NtkPoNum(pNtk); i++) {
51  char * seg = (char *)vSuppFun->pArray[i];
52 
53  for(j = 0; j < Abc_NtkPiNum(pNtk); j+=8) {
54  if(((*seg) & 0x01) == 0x01)
55  Vec_IntPushOrder(oDep[i], j);
56  if(((*seg) & 0x02) == 0x02)
57  Vec_IntPushOrder(oDep[i], j+1);
58  if(((*seg) & 0x04) == 0x04)
59  Vec_IntPushOrder(oDep[i], j+2);
60  if(((*seg) & 0x08) == 0x08)
61  Vec_IntPushOrder(oDep[i], j+3);
62  if(((*seg) & 0x10) == 0x10)
63  Vec_IntPushOrder(oDep[i], j+4);
64  if(((*seg) & 0x20) == 0x20)
65  Vec_IntPushOrder(oDep[i], j+5);
66  if(((*seg) & 0x40) == 0x40)
67  Vec_IntPushOrder(oDep[i], j+6);
68  if(((*seg) & 0x80) == 0x80)
69  Vec_IntPushOrder(oDep[i], j+7);
70 
71  seg++;
72  }
73  }
74 
75  for(i = 0; i < Abc_NtkPoNum(pNtk); i++)
76  for(j = 0; j < Vec_IntSize(oDep[i]); j++)
77  Vec_IntPush(iDep[Vec_IntEntry(oDep[i], j)], i);
78 
79 
80  /*for(i = 0; i < Abc_NtkPoNum(pNtk); i++)
81  {
82  printf("Output %d: ", i);
83  for(j = 0; j < Vec_IntSize(oDep[i]); j++)
84  printf("%d ", Vec_IntEntry(oDep[i], j));
85  printf("\n");
86  }
87 
88  printf("\n");
89 
90  for(i = 0; i < Abc_NtkPiNum(pNtk); i++)
91  {
92  printf("Input %d: ", i);
93  for(j = 0; j < Vec_IntSize(iDep[i]); j++)
94  printf("%d ", Vec_IntEntry(iDep[i], j));
95  printf("\n");
96  }
97 
98  printf("\n"); */
99 }
100 
101 void initMatchList(Abc_Ntk_t *pNtk, Vec_Int_t** iDep, Vec_Int_t** oDep, Vec_Int_t** iMatch, int* iLastItem, Vec_Int_t** oMatch, int* oLastItem, int* iGroup, int* oGroup, int p_equivalence)
102 {
103  int i, j, curr;
104  Vec_Int_t** temp;
105 
106  if(!p_equivalence) {
107  temp = ABC_ALLOC( Vec_Int_t*, Abc_NtkPiNum(pNtk)+1);
108 
109  for(i = 0; i < Abc_NtkPiNum(pNtk)+1; i++)
110  temp[i] = Vec_IntAlloc( 0 );
111 
112  for(i = 0; i < Abc_NtkPoNum(pNtk); i++)
113  Vec_IntPush(temp[Vec_IntSize(oDep[i])], i);
114 
115  curr = 0;
116  for(i = 0; i < Abc_NtkPiNum(pNtk)+1; i++)
117  {
118  if(Vec_IntSize(temp[i]) == 0)
119  Vec_IntFree( temp[i] );
120 
121  else
122  {
123  oMatch[curr] = temp[i];
124 
125  for(j = 0; j < Vec_IntSize(temp[i]); j++)
126  oGroup[Vec_IntEntry(oMatch[curr], j)] = curr;
127 
128  curr++;
129  }
130  }
131 
132  *oLastItem = curr;
133 
134  ABC_FREE( temp );
135  }
136  else {
137  // the else part fixes the outputs for P-equivalence checking
138  for(i = 0; i < Abc_NtkPoNum(pNtk); i++)
139  {
140  Vec_IntPush(oMatch[i], i);
141  oGroup[i] = i;
142  (*oLastItem) = Abc_NtkPoNum(pNtk);
143  }
144  }
145 
146  /*for(j = 0; j < *oLastItem; j++)
147  {
148  printf("oMatch %d: ", j);
149  for(i = 0; i < Vec_IntSize(oMatch[j]); i++)
150  printf("%d ", Vec_IntEntry(oMatch[j], i));
151  printf("\n");
152  }
153 
154  for(i = 0; i < Abc_NtkPoNum(pNtk); i++)
155  printf("%d: %d ", i, oGroup[i]);*/
156 
157  //////////////////////////////////////////////////////////////////////////////
158 
159  temp = ABC_ALLOC( Vec_Int_t*, Abc_NtkPoNum(pNtk)+1 );
160 
161  for(i = 0; i < Abc_NtkPoNum(pNtk)+1; i++)
162  temp[i] = Vec_IntAlloc( 0 );
163 
164  for(i = 0; i < Abc_NtkPiNum(pNtk); i++)
165  Vec_IntPush(temp[Vec_IntSize(iDep[i])], i);
166 
167  curr = 0;
168  for(i = 0; i < Abc_NtkPoNum(pNtk)+1; i++)
169  {
170  if(Vec_IntSize(temp[i]) == 0)
171  Vec_IntFree( temp[i] );
172  else
173  {
174  iMatch[curr] = temp[i];
175  for(j = 0; j < Vec_IntSize(iMatch[curr]); j++)
176  iGroup[Vec_IntEntry(iMatch[curr], j)] = curr;
177  curr++;
178  }
179  }
180 
181  *iLastItem = curr;
182 
183  ABC_FREE( temp );
184 
185  /*printf("\n");
186  for(j = 0; j < *iLastItem; j++)
187  {
188  printf("iMatch %d: ", j);
189  for(i = 0; i < Vec_IntSize(iMatch[j]); i++)
190  printf("%d ", Vec_IntEntry(iMatch[j], i));
191  printf("\n");
192  }
193 
194  for(i = 0; i < Abc_NtkPiNum(pNtk); i++)
195  printf("%d: %d ", i, iGroup[i]);
196  printf("\n");*/
197 }
198 
199 void iSortDependencies(Abc_Ntk_t *pNtk, Vec_Int_t** iDep, int* oGroup)
200 {
201  int i, j, k;
202  Vec_Int_t * temp;
203  Vec_Int_t * oGroupList;
204 
205  oGroupList = Vec_IntAlloc( 10 );
206 
207  for(i = 0; i < Abc_NtkPiNum(pNtk); i++)
208  {
209  if(Vec_IntSize(iDep[i]) == 1)
210  continue;
211 
212  temp = Vec_IntAlloc( Vec_IntSize(iDep[i]) );
213 
214  for(j = 0; j < Vec_IntSize(iDep[i]); j++)
215  Vec_IntPushUniqueOrder(oGroupList, oGroup[Vec_IntEntry(iDep[i], j)]);
216 
217  for(j = 0; j < Vec_IntSize(oGroupList); j++)
218  {
219  for(k = 0; k < Vec_IntSize(iDep[i]); k++)
220  if(oGroup[Vec_IntEntry(iDep[i], k)] == Vec_IntEntry(oGroupList, j))
221  {
222  Vec_IntPush( temp, Vec_IntEntry(iDep[i], k) );
223  Vec_IntRemove( iDep[i], Vec_IntEntry(iDep[i], k) );
224  k--;
225  }
226  }
227 
228  Vec_IntFree( iDep[i] );
229  iDep[i] = temp;
230  Vec_IntClear( oGroupList );
231 
232  /*printf("Input %d: ", i);
233  for(j = 0; j < Vec_IntSize(iDep[i]); j++)
234  printf("%d ", Vec_IntEntry(iDep[i], j));
235  printf("\n");*/
236  }
237 
238  Vec_IntFree( oGroupList );
239 }
240 
241 void oSortDependencies(Abc_Ntk_t *pNtk, Vec_Int_t** oDep, int* iGroup)
242 {
243  int i, j, k;
244  Vec_Int_t * temp;
245  Vec_Int_t * iGroupList;
246 
247  iGroupList = Vec_IntAlloc( 10 );
248 
249  for(i = 0; i < Abc_NtkPoNum(pNtk); i++)
250  {
251  if(Vec_IntSize(oDep[i]) == 1)
252  continue;
253 
254  temp = Vec_IntAlloc( Vec_IntSize(oDep[i]) );
255 
256  for(j = 0; j < Vec_IntSize(oDep[i]); j++)
257  Vec_IntPushUniqueOrder(iGroupList, iGroup[Vec_IntEntry(oDep[i], j)]);
258 
259  for(j = 0; j < Vec_IntSize(iGroupList); j++)
260  {
261  for(k = 0; k < Vec_IntSize(oDep[i]); k++)
262  if(iGroup[Vec_IntEntry(oDep[i], k)] == Vec_IntEntry(iGroupList, j))
263  {
264  Vec_IntPush( temp, Vec_IntEntry(oDep[i], k) );
265  Vec_IntRemove( oDep[i], Vec_IntEntry(oDep[i], k) );
266  k--;
267  }
268  }
269 
270  Vec_IntFree( oDep[i] );
271  oDep[i] = temp;
272  Vec_IntClear( iGroupList );
273 
274  /*printf("Output %d: ", i);
275  for(j = 0; j < Vec_IntSize(oDep[i]); j++)
276  printf("%d ", Vec_IntEntry(oDep[i], j));
277  printf("\n");*/
278  }
279 
280  Vec_IntFree( iGroupList );
281 }
282 
283 int oSplitByDep(Abc_Ntk_t *pNtk, Vec_Int_t** oDep, Vec_Int_t** oMatch, int* oGroup, int* oLastItem, int* iGroup)
284 {
285  int i, j, k;
286  int numOfItemsAdded;
287  Vec_Int_t * array, * sortedArray;
288 
289  numOfItemsAdded = 0;
290 
291  for(i = 0; i < *oLastItem; i++)
292  {
293  if(Vec_IntSize(oMatch[i]) == 1)
294  continue;
295 
296  array = Vec_IntAlloc( Vec_IntSize(oMatch[i]) );
297  sortedArray = Vec_IntAlloc( Vec_IntSize(oMatch[i]) );
298 
299  for(j = 0; j < Vec_IntSize(oMatch[i]); j++)
300  {
301  int factor, encode;
302 
303  encode = 0;
304  factor = 1;
305 
306  for(k = 0; k < Vec_IntSize(oDep[Vec_IntEntry(oMatch[i], j)]); k++)
307  encode += iGroup[Vec_IntEntry(oDep[Vec_IntEntry(oMatch[i], j)], k)] * factor;
308 
309  Vec_IntPush(array, encode);
310  Vec_IntPushUniqueOrder(sortedArray, encode);
311 
312  if( encode < 0)
313  printf("WARNING! Integer overflow!");
314 
315  //printf("%d ", Vec_IntEntry(array, j));
316  }
317 
318  while( Vec_IntSize(sortedArray) > 1 )
319  {
320  for(k = 0; k < Vec_IntSize(oMatch[i]); k++)
321  {
322  if(Vec_IntEntry(array, k) == Vec_IntEntryLast(sortedArray))
323  {
324  Vec_IntPush(oMatch[*oLastItem+numOfItemsAdded], Vec_IntEntry(oMatch[i], k));
325  oGroup[Vec_IntEntry(oMatch[i], k)] = *oLastItem+numOfItemsAdded;
326  Vec_IntRemove( oMatch[i], Vec_IntEntry(oMatch[i], k) );
327  Vec_IntRemove( array, Vec_IntEntry(array, k) );
328  k--;
329  }
330  }
331  numOfItemsAdded++;
332  Vec_IntPop(sortedArray);
333  }
334 
335  Vec_IntFree( array );
336  Vec_IntFree( sortedArray );
337  //printf("\n");
338  }
339 
340  *oLastItem += numOfItemsAdded;
341 
342  /*printf("\n");
343  for(j = 0; j < *oLastItem ; j++)
344  {
345  printf("oMatch %d: ", j);
346  for(i = 0; i < Vec_IntSize(oMatch[j]); i++)
347  printf("%d ", Vec_IntEntry(oMatch[j], i));
348  printf("\n");
349  }*/
350 
351  return numOfItemsAdded;
352 }
353 
354 int iSplitByDep(Abc_Ntk_t *pNtk, Vec_Int_t** iDep, Vec_Int_t** iMatch, int* iGroup, int* iLastItem, int* oGroup)
355 {
356  int i, j, k;
357  int numOfItemsAdded = 0;
358  Vec_Int_t * array, * sortedArray;
359 
360  for(i = 0; i < *iLastItem; i++)
361  {
362  if(Vec_IntSize(iMatch[i]) == 1)
363  continue;
364 
365  array = Vec_IntAlloc( Vec_IntSize(iMatch[i]) );
366  sortedArray = Vec_IntAlloc( Vec_IntSize(iMatch[i]) );
367 
368  for(j = 0; j < Vec_IntSize(iMatch[i]); j++)
369  {
370  int factor, encode;
371 
372  encode = 0;
373  factor = 1;
374 
375  for(k = 0; k < Vec_IntSize(iDep[Vec_IntEntry(iMatch[i], j)]); k++)
376  encode += oGroup[Vec_IntEntry(iDep[Vec_IntEntry(iMatch[i], j)], k)] * factor;
377 
378  Vec_IntPush(array, encode);
379  Vec_IntPushUniqueOrder(sortedArray, encode);
380 
381  //printf("%d ", Vec_IntEntry(array, j));
382  }
383 
384  while( Vec_IntSize(sortedArray) > 1 )
385  {
386  for(k = 0; k < Vec_IntSize(iMatch[i]); k++)
387  {
388  if(Vec_IntEntry(array, k) == Vec_IntEntryLast(sortedArray))
389  {
390  Vec_IntPush(iMatch[*iLastItem+numOfItemsAdded], Vec_IntEntry(iMatch[i], k));
391  iGroup[Vec_IntEntry(iMatch[i], k)] = *iLastItem+numOfItemsAdded;
392  Vec_IntRemove( iMatch[i], Vec_IntEntry(iMatch[i], k) );
393  Vec_IntRemove( array, Vec_IntEntry(array, k) );
394  k--;
395  }
396  }
397  numOfItemsAdded++;
398  Vec_IntPop(sortedArray);
399  }
400 
401  Vec_IntFree( array );
402  Vec_IntFree( sortedArray );
403  //printf("\n");
404  }
405 
406  *iLastItem += numOfItemsAdded;
407 
408  /*printf("\n");
409  for(j = 0; j < *iLastItem ; j++)
410  {
411  printf("iMatch %d: ", j);
412  for(i = 0; i < Vec_IntSize(iMatch[j]); i++)
413  printf("%d ", Vec_IntEntry(iMatch[j], i));
414  printf("\n");
415  }*/
416 
417  return numOfItemsAdded;
418 }
419 
421 {
422  Vec_Ptr_t ** vNodes;
423  Abc_Obj_t * pObj, * pFanout;
424  int i, k;
425 
426  extern void Abc_NtkDfsReverse_rec( Abc_Obj_t * pNode, Vec_Ptr_t * vNodes );
427 
428  // start the array of nodes
429  vNodes = ABC_ALLOC(Vec_Ptr_t *, Abc_NtkPiNum(pNtk));
430  for(i = 0; i < Abc_NtkPiNum(pNtk); i++)
431  vNodes[i] = Vec_PtrAlloc(50);
432 
433  Abc_NtkForEachCi( pNtk, pObj, i )
434  {
435  // set the traversal ID
436  Abc_NtkIncrementTravId( pNtk );
437  Abc_NodeSetTravIdCurrent( pObj );
438  pObj = Abc_ObjFanout0Ntk(pObj);
439  Abc_ObjForEachFanout( pObj, pFanout, k )
440  Abc_NtkDfsReverse_rec( pFanout, vNodes[i] );
441  }
442 
443  return vNodes;
444 }
445 
446 
447 int * Abc_NtkSimulateOneNode( Abc_Ntk_t * pNtk, int * pModel, int input, Vec_Ptr_t ** topOrder )
448 {
449  Abc_Obj_t * pNode;
450  Vec_Ptr_t * vNodes;
451  int * pValues, Value0, Value1, i;
452 
453  vNodes = Vec_PtrAlloc( 50 );
454 /*
455  printf( "Counter example: " );
456  Abc_NtkForEachCi( pNtk, pNode, i )
457  printf( " %d", pModel[i] );
458  printf( "\n" );
459 */
460  // increment the trav ID
461  Abc_NtkIncrementTravId( pNtk );
462  // set the CI values
463  Abc_AigConst1(pNtk)->pCopy = (Abc_Obj_t *)1;
464  pNode = Abc_NtkCi(pNtk, input);
465  pNode->iTemp = pModel[input];
466 
467  // simulate in the topological order
468  for(i = Vec_PtrSize(topOrder[input])-1; i >= 0; i--)
469  {
470  pNode = (Abc_Obj_t *)Vec_PtrEntry(topOrder[input], i);
471 
472  Value0 = ((int)(ABC_PTRUINT_T)Abc_ObjFanin0(pNode)->pCopy) ^ Abc_ObjFaninC0(pNode);
473  Value1 = ((int)(ABC_PTRUINT_T)Abc_ObjFanin1(pNode)->pCopy) ^ Abc_ObjFaninC1(pNode);
474 
475  if( pNode->iTemp != (Value0 & Value1))
476  {
477  pNode->iTemp = (Value0 & Value1);
478  Vec_PtrPush(vNodes, pNode);
479  }
480 
481  }
482  // fill the output values
483  pValues = ABC_ALLOC( int, Abc_NtkCoNum(pNtk) );
484  Abc_NtkForEachCo( pNtk, pNode, i )
485  pValues[i] = ((int)(ABC_PTRUINT_T)Abc_ObjFanin0(pNode)->pCopy) ^ Abc_ObjFaninC0(pNode);
486 
487  pNode = Abc_NtkCi(pNtk, input);
488  if(pNode->pCopy == (Abc_Obj_t *)1)
489  pNode->pCopy = (Abc_Obj_t *)0;
490  else
491  pNode->pCopy = (Abc_Obj_t *)1;
492 
493  for(i = 0; i < Vec_PtrSize(vNodes); i++)
494  {
495  pNode = (Abc_Obj_t *)Vec_PtrEntry(vNodes, i);
496 
497  if(pNode->pCopy == (Abc_Obj_t *)1)
498  pNode->pCopy = (Abc_Obj_t *)0;
499  else
500  pNode->pCopy = (Abc_Obj_t *)1;
501  }
502 
503  Vec_PtrFree( vNodes );
504 
505  return pValues;
506 }
507 
508 int refineIOBySimulation(Abc_Ntk_t *pNtk, Vec_Int_t** iMatch, int* iLastItem, int * iGroup, Vec_Int_t** iDep, Vec_Int_t** oMatch, int* oLastItem, int * oGroup, Vec_Int_t** oDep, char * vPiValues, int * observability, Vec_Ptr_t ** topOrder)
509 {
510  Abc_Obj_t * pObj;
511  int * pModel;//, ** pModel2;
512  int * output, * output2;
513  int lastItem;
514  int i, j, k;
515  Vec_Int_t * iComputedNum, * iComputedNumSorted;
516  Vec_Int_t * oComputedNum; // encoding the number of flips
517  int factor;
518  int isRefined = FALSE;
519 
520  pModel = ABC_ALLOC( int, Abc_NtkCiNum(pNtk) );
521 
522  Abc_NtkForEachPi( pNtk, pObj, i )
523  pModel[i] = vPiValues[i] - '0';
524  Abc_NtkForEachLatch( pNtk, pObj, i )
525  pModel[Abc_NtkPiNum(pNtk)+i] = pObj->iData - 1;
526 
527  output = Abc_NtkVerifySimulatePattern( pNtk, pModel );
528 
529  oComputedNum = Vec_IntAlloc( Abc_NtkPoNum(pNtk) );
530  for(i = 0; i < Abc_NtkPoNum(pNtk); i++)
531  Vec_IntPush(oComputedNum, 0);
532 
533  /****************************************************************************************/
534  /********** group outputs that produce 1 and outputs that produce 0 together ************/
535 
536  lastItem = *oLastItem;
537  for(i = 0; i < lastItem && (*oLastItem) != Abc_NtkPoNum(pNtk); i++)
538  {
539  int flag = FALSE;
540 
541  if(Vec_IntSize(oMatch[i]) == 1)
542  continue;
543 
544  for(j = 1; j < Vec_IntSize(oMatch[i]); j++)
545  if(output[Vec_IntEntry(oMatch[i], 0)] != output[Vec_IntEntry(oMatch[i], j)])
546  {
547  flag = TRUE;
548  break;
549  }
550 
551  if(flag)
552  {
553  for(j = 0; j < Vec_IntSize(oMatch[i]); j++)
554  if(output[Vec_IntEntry(oMatch[i], j)])
555  {
556  Vec_IntPush(oMatch[*oLastItem], Vec_IntEntry(oMatch[i], j));
557  oGroup[Vec_IntEntry(oMatch[i], j)] = *oLastItem;
558  Vec_IntRemove(oMatch[i], Vec_IntEntry(oMatch[i], j));
559  j--;
560  }
561 
562  (*oLastItem)++;
563  }
564  }
565 
566  if( (*oLastItem) > lastItem )
567  {
568  isRefined = TRUE;
569  iSortDependencies(pNtk, iDep, oGroup);
570  }
571 
572  /****************************************************************************************/
573  /************* group inputs that make the same number of flips in outpus ****************/
574 
575  lastItem = *iLastItem;
576  for(i = 0; i < lastItem && (*iLastItem) != Abc_NtkPiNum(pNtk); i++)
577  {
578  int num;
579 
580  if(Vec_IntSize(iMatch[i]) == 1)
581  continue;
582 
583  iComputedNum = Vec_IntAlloc( Vec_IntSize(iMatch[i]) );
584  iComputedNumSorted = Vec_IntAlloc( Vec_IntSize(iMatch[i]) );
585 
586  for(j = 0; j < Vec_IntSize(iMatch[i]); j++)
587  {
588  if( vPiValues[Vec_IntEntry(iMatch[i], j)] == '0' )
589  pModel[Vec_IntEntry(iMatch[i], j)] = 1;
590  else
591  pModel[Vec_IntEntry(iMatch[i], j)] = 0;
592 
593  //output2 = Abc_NtkVerifySimulatePattern( pNtk, pModel );
594  output2 = Abc_NtkSimulateOneNode( pNtk, pModel, Vec_IntEntry(iMatch[i], j), topOrder );
595 
596  num = 0;
597  factor = 1;
598  for(k = 0; k < Vec_IntSize(iDep[Vec_IntEntry(iMatch[i], j)]); k++)
599  {
600  int outputIndex = Vec_IntEntry(iDep[Vec_IntEntry(iMatch[i], j)], k);
601 
602  if(output2[outputIndex])
603  num += (oGroup[outputIndex] + 1) * factor;
604 
605  if(output[outputIndex] != output2[outputIndex])
606  {
607  int temp = Vec_IntEntry(oComputedNum, outputIndex) + i + 1;
608  Vec_IntWriteEntry(oComputedNum, outputIndex, temp);
609  observability[Vec_IntEntry(iMatch[i], j)]++;
610  }
611  }
612 
613  Vec_IntPush(iComputedNum, num);
614  Vec_IntPushUniqueOrder(iComputedNumSorted, num);
615 
616  pModel[Vec_IntEntry(iMatch[i], j)] = vPiValues[Vec_IntEntry(iMatch[i], j)] - '0';
617  ABC_FREE( output2 );
618  }
619 
620  while( Vec_IntSize( iComputedNumSorted ) > 1 )
621  {
622  for(k = 0; k < Vec_IntSize(iMatch[i]); k++)
623  {
624  if(Vec_IntEntry(iComputedNum, k) == Vec_IntEntryLast(iComputedNumSorted) )
625  {
626  Vec_IntPush(iMatch[*iLastItem], Vec_IntEntry(iMatch[i], k));
627  iGroup[Vec_IntEntry(iMatch[i], k)] = *iLastItem;
628  Vec_IntRemove( iMatch[i], Vec_IntEntry(iMatch[i], k) );
629  Vec_IntRemove( iComputedNum, Vec_IntEntry(iComputedNum, k) );
630  k--;
631  }
632  }
633  (*iLastItem)++;
634  Vec_IntPop( iComputedNumSorted );
635  }
636 
637  Vec_IntFree( iComputedNum );
638  Vec_IntFree( iComputedNumSorted );
639  }
640 
641  if( (*iLastItem) > lastItem )
642  {
643  isRefined = TRUE;
644  oSortDependencies(pNtk, oDep, iGroup);
645  }
646 
647  /****************************************************************************************/
648  /********** encode the number of flips in each output by flipping the outputs ***********/
649  /********** and group all the outputs that have the same encoding ***********/
650 
651  lastItem = *oLastItem;
652  for(i = 0; i < lastItem && (*oLastItem) != Abc_NtkPoNum(pNtk); i++)
653  {
654  Vec_Int_t * encode, * sortedEncode; // encoding the number of flips
655 
656  if(Vec_IntSize(oMatch[i]) == 1)
657  continue;
658 
659  encode = Vec_IntAlloc( Vec_IntSize(oMatch[i]) );
660  sortedEncode = Vec_IntAlloc( Vec_IntSize(oMatch[i]) );
661 
662  for(j = 0; j < Vec_IntSize(oMatch[i]); j++)
663  {
664  Vec_IntPush(encode, Vec_IntEntry(oComputedNum, Vec_IntEntry(oMatch[i], j)) );
665  Vec_IntPushUniqueOrder( sortedEncode, Vec_IntEntry(encode, j) );
666  }
667 
668  while( Vec_IntSize(sortedEncode) > 1 )
669  {
670  for(j = 0; j < Vec_IntSize(oMatch[i]); j++)
671  if(Vec_IntEntry(encode, j) == Vec_IntEntryLast(sortedEncode))
672  {
673  Vec_IntPush(oMatch[*oLastItem], Vec_IntEntry(oMatch[i], j));
674  oGroup[Vec_IntEntry(oMatch[i], j)] = *oLastItem;
675  Vec_IntRemove( oMatch[i], Vec_IntEntry(oMatch[i], j) );
676  Vec_IntRemove( encode, Vec_IntEntry(encode, j) );
677  j --;
678  }
679 
680  (*oLastItem)++;
681  Vec_IntPop( sortedEncode );
682  }
683 
684  Vec_IntFree( encode );
685  Vec_IntFree( sortedEncode );
686  }
687 
688  if( (*oLastItem) > lastItem )
689  isRefined = TRUE;
690 
691  ABC_FREE( pModel );
692  ABC_FREE( output );
693  Vec_IntFree( oComputedNum );
694 
695  return isRefined;
696 }
697 
698 Abc_Ntk_t * Abc_NtkMiterBm( Abc_Ntk_t * pNtk1, Abc_Ntk_t * pNtk2, Vec_Ptr_t * iCurrMatch, Vec_Ptr_t * oCurrMatch )
699 {
700  char Buffer[1000];
701  Abc_Ntk_t * pNtkMiter;
702 
703  pNtkMiter = Abc_NtkAlloc( ABC_NTK_STRASH, ABC_FUNC_AIG, 1 );
704  sprintf( Buffer, "%s_%s_miter", pNtk1->pName, pNtk2->pName );
705  pNtkMiter->pName = Extra_UtilStrsav(Buffer);
706 
707  //Abc_NtkMiterPrepare( pNtk1, pNtk2, pNtkMiter, fComb, nPartSize );
708  {
709  Abc_Obj_t * pObj, * pObjNew;
710  int i;
711 
712  Abc_AigConst1(pNtk1)->pCopy = Abc_AigConst1(pNtkMiter);
713  Abc_AigConst1(pNtk2)->pCopy = Abc_AigConst1(pNtkMiter);
714 
715  // create new PIs and remember them in the old PIs
716  if(iCurrMatch == NULL)
717  {
718  Abc_NtkForEachCi( pNtk1, pObj, i )
719  {
720  pObjNew = Abc_NtkCreatePi( pNtkMiter );
721  // remember this PI in the old PIs
722  pObj->pCopy = pObjNew;
723  pObj = Abc_NtkCi(pNtk2, i);
724  pObj->pCopy = pObjNew;
725  // add name
726  Abc_ObjAssignName( pObjNew, Abc_ObjName(pObj), NULL );
727  }
728  }
729  else
730  {
731  for(i = 0; i < Vec_PtrSize( iCurrMatch ); i += 2)
732  {
733  pObjNew = Abc_NtkCreatePi( pNtkMiter );
734  pObj = (Abc_Obj_t *)Vec_PtrEntry(iCurrMatch, i);
735  pObj->pCopy = pObjNew;
736  pObj = (Abc_Obj_t *)Vec_PtrEntry(iCurrMatch, i+1);
737  pObj->pCopy = pObjNew;
738  // add name
739  Abc_ObjAssignName( pObjNew, Abc_ObjName(pObj), NULL );
740  }
741  }
742 
743  // create the only PO
744  pObjNew = Abc_NtkCreatePo( pNtkMiter );
745  // add the PO name
746  Abc_ObjAssignName( pObjNew, "miter", NULL );
747  }
748 
749  // Abc_NtkMiterAddOne( pNtk1, pNtkMiter );
750  {
751  Abc_Obj_t * pNode;
752  int i;
753  assert( Abc_NtkIsDfsOrdered(pNtk1) );
754  Abc_AigForEachAnd( pNtk1, pNode, i )
755  pNode->pCopy = Abc_AigAnd( (Abc_Aig_t *)pNtkMiter->pManFunc, Abc_ObjChild0Copy(pNode), Abc_ObjChild1Copy(pNode) );
756  }
757 
758  // Abc_NtkMiterAddOne( pNtk2, pNtkMiter );
759  {
760  Abc_Obj_t * pNode;
761  int i;
762  assert( Abc_NtkIsDfsOrdered(pNtk2) );
763  Abc_AigForEachAnd( pNtk2, pNode, i )
764  pNode->pCopy = Abc_AigAnd( (Abc_Aig_t *)pNtkMiter->pManFunc, Abc_ObjChild0Copy(pNode), Abc_ObjChild1Copy(pNode) );
765  }
766 
767  // Abc_NtkMiterFinalize( pNtk1, pNtk2, pNtkMiter, fComb, nPartSize );
768  {
769  Vec_Ptr_t * vPairs;
770  Abc_Obj_t * pMiter;
771  int i;
772 
773  vPairs = Vec_PtrAlloc( 100 );
774 
775  // collect the CO nodes for the miter
776  if(oCurrMatch != NULL)
777  {
778  for(i = 0; i < Vec_PtrSize( oCurrMatch ); i += 2)
779  {
780  Vec_PtrPush( vPairs, Abc_ObjChild0Copy((Abc_Obj_t *)Vec_PtrEntry(oCurrMatch, i)) );
781  Vec_PtrPush( vPairs, Abc_ObjChild0Copy((Abc_Obj_t *)Vec_PtrEntry(oCurrMatch, i+1)) );
782  }
783  }
784  else
785  {
786  Abc_Obj_t * pNode;
787 
788  Abc_NtkForEachCo( pNtk1, pNode, i )
789  {
790  Vec_PtrPush( vPairs, Abc_ObjChild0Copy(pNode) );
791  pNode = Abc_NtkCo( pNtk2, i );
792  Vec_PtrPush( vPairs, Abc_ObjChild0Copy(pNode) );
793  }
794  }
795 
796  pMiter = Abc_AigMiter( (Abc_Aig_t *)pNtkMiter->pManFunc, vPairs, 0 );
797  Abc_ObjAddFanin( Abc_NtkPo(pNtkMiter,0), pMiter );
798  Vec_PtrFree(vPairs);
799  }
800 
801  //Abc_AigCleanup(pNtkMiter->pManFunc);
802 
803  return pNtkMiter;
804 }
805 
807 
808 void Abc_NtkVerifyReportError( Abc_Ntk_t * pNtk1, Abc_Ntk_t * pNtk2, int * pModel, Vec_Int_t * mismatch )
809 {
810  Vec_Ptr_t * vNodes;
811  Abc_Obj_t * pNode;
812  int nErrors, nPrinted, i, iNode = -1;
813 
814  assert( Abc_NtkCiNum(pNtk1) == Abc_NtkCiNum(pNtk2) );
815  assert( Abc_NtkCoNum(pNtk1) == Abc_NtkCoNum(pNtk2) );
816  // get the CO values under this model
817  pValues1__ = Abc_NtkVerifySimulatePattern( pNtk1, pModel );
818  pValues2__ = Abc_NtkVerifySimulatePattern( pNtk2, pModel );
819  // count the mismatches
820  nErrors = 0;
821  for ( i = 0; i < Abc_NtkCoNum(pNtk1); i++ )
822  nErrors += (int)( pValues1__[i] != pValues2__[i] );
823  //printf( "Verification failed for at least %d outputs: ", nErrors );
824  // print the first 3 outputs
825  nPrinted = 0;
826  for ( i = 0; i < Abc_NtkCoNum(pNtk1); i++ )
827  if ( pValues1__[i] != pValues2__[i] )
828  {
829  if ( iNode == -1 )
830  iNode = i;
831  //printf( " %s", Abc_ObjName(Abc_NtkCo(pNtk1,i)) );
832  if ( ++nPrinted == 3 )
833  break;
834  }
835  /*if ( nPrinted != nErrors )
836  printf( " ..." );
837  printf( "\n" );*/
838  // report mismatch for the first output
839  if ( iNode >= 0 )
840  {
841  /*printf( "Output %s: Value in Network1 = %d. Value in Network2 = %d.\n",
842  Abc_ObjName(Abc_NtkCo(pNtk1,iNode)), pValues1[iNode], pValues2[iNode] );
843  printf( "Input pattern: " );*/
844  // collect PIs in the cone
845  pNode = Abc_NtkCo(pNtk1,iNode);
846  vNodes = Abc_NtkNodeSupport( pNtk1, &pNode, 1 );
847  // set the PI numbers
848  Abc_NtkForEachCi( pNtk1, pNode, i )
849  pNode->iTemp = i;
850  // print the model
851  pNode = (Abc_Obj_t *)Vec_PtrEntry( vNodes, 0 );
852  if ( Abc_ObjIsCi(pNode) )
853  {
854  Vec_PtrForEachEntry( Abc_Obj_t *, vNodes, pNode, i )
855  {
856  assert( Abc_ObjIsCi(pNode) );
857  //printf( " %s=%d", Abc_ObjName(pNode), pModel[(int)pNode->pCopy] );
858  Vec_IntPush(mismatch, Abc_ObjId(pNode)-1);
859  Vec_IntPush(mismatch, pModel[(int)(size_t)pNode->pCopy]);
860  }
861  }
862  //printf( "\n" );
863  Vec_PtrFree( vNodes );
864  }
865  free( pValues1__ );
866  free( pValues2__ );
867 }
868 
869 int Abc_NtkMiterSatBm( Abc_Ntk_t * pNtk, ABC_INT64_T nConfLimit, ABC_INT64_T nInsLimit, int fVerbose, ABC_INT64_T * pNumConfs, ABC_INT64_T * pNumInspects)
870 {
871  static sat_solver * pSat = NULL;
872  lbool status;
873  int RetValue;
874  abctime clk;
875 
876  extern int Abc_NodeAddClausesTop( sat_solver * pSat, Abc_Obj_t * pNode, Vec_Int_t * vVars );
877  extern Vec_Int_t * Abc_NtkGetCiSatVarNums( Abc_Ntk_t * pNtk );
878 
879  if ( pNumConfs )
880  *pNumConfs = 0;
881  if ( pNumInspects )
882  *pNumInspects = 0;
883 
884  assert( Abc_NtkLatchNum(pNtk) == 0 );
885 
886 // if ( Abc_NtkPoNum(pNtk) > 1 )
887 // fprintf( stdout, "Warning: The miter has %d outputs. SAT will try to prove all of them.\n", Abc_NtkPoNum(pNtk) );
888 
889  // load clauses into the sat_solver
890  clk = Abc_Clock();
891 
892 
893 
894  pSat = (sat_solver *)Abc_NtkMiterSatCreate( pNtk, 0 );
895 
896  if ( pSat == NULL )
897  return 1;
898 //printf( "%d \n", pSat->clauses.size );
899 //sat_solver_delete( pSat );
900 //return 1;
901 
902 // printf( "Created SAT problem with %d variable and %d clauses. ", sat_solver_nvars(pSat), sat_solver_nclauses(pSat) );
903 // PRT( "Time", Abc_Clock() - clk );
904 
905  // simplify the problem
906  clk = Abc_Clock();
907  status = sat_solver_simplify(pSat);
908 // printf( "Simplified the problem to %d variables and %d clauses. ", sat_solver_nvars(pSat), sat_solver_nclauses(pSat) );
909 // PRT( "Time", Abc_Clock() - clk );
910  if ( status == 0 )
911  {
912  sat_solver_delete( pSat );
913 // printf( "The problem is UNSATISFIABLE after simplification.\n" );
914  return 1;
915  }
916 
917  // solve the miter
918  clk = Abc_Clock();
919  if ( fVerbose )
920  pSat->verbosity = 1;
921  status = sat_solver_solve( pSat, NULL, NULL, (ABC_INT64_T)nConfLimit, (ABC_INT64_T)nInsLimit, (ABC_INT64_T)0, (ABC_INT64_T)0 );
922  if ( status == l_Undef )
923  {
924 // printf( "The problem timed out.\n" );
925  RetValue = -1;
926  }
927  else if ( status == l_True )
928  {
929 // printf( "The problem is SATISFIABLE.\n" );
930  RetValue = 0;
931  }
932  else if ( status == l_False )
933  {
934 // printf( "The problem is UNSATISFIABLE.\n" );
935  RetValue = 1;
936  }
937  else
938  assert( 0 );
939 // PRT( "SAT sat_solver time", Abc_Clock() - clk );
940 // printf( "The number of conflicts = %d.\n", (int)pSat->sat_solver_stats.conflicts );
941 
942  // if the problem is SAT, get the counterexample
943  if ( status == l_True )
944  {
945 // Vec_Int_t * vCiIds = Abc_NtkGetCiIds( pNtk );
946  Vec_Int_t * vCiIds = Abc_NtkGetCiSatVarNums( pNtk );
947  pNtk->pModel = Sat_SolverGetModel( pSat, vCiIds->pArray, vCiIds->nSize );
948  Vec_IntFree( vCiIds );
949  }
950  // free the sat_solver
951  if ( fVerbose )
952  Sat_SolverPrintStats( stdout, pSat );
953 
954  if ( pNumConfs )
955  *pNumConfs = (int)pSat->stats.conflicts;
956  if ( pNumInspects )
957  *pNumInspects = (int)pSat->stats.inspects;
958 
959 //sat_solver_store_write( pSat, "trace.cnf" );
960  sat_solver_store_free( pSat );
961  sat_solver_delete( pSat );
962  return RetValue;
963 }
964 
965 int Abc_NtkBmSat( Abc_Ntk_t * pNtk1, Abc_Ntk_t * pNtk2, Vec_Ptr_t * iMatchPairs, Vec_Ptr_t * oMatchPairs, Vec_Int_t * mismatch, int mode)
966 {
967  extern Abc_Ntk_t * Abc_NtkMulti( Abc_Ntk_t * pNtk, int nThresh, int nFaninMax, int fCnf, int fMulti, int fSimple, int fFactor );
968 
969  Abc_Ntk_t * pMiter = NULL;
970  Abc_Ntk_t * pCnf;
971  int RetValue;
972 
973  // get the miter of the two networks
974  if( mode == 0 )
975  {
976  //Abc_NtkDelete( pMiter );
977  pMiter = Abc_NtkMiterBm( pNtk1, pNtk2, iMatchPairs, oMatchPairs );
978  }
979  else if( mode == 1 ) // add new outputs
980  {
981  int i;
982  Abc_Obj_t * pObj;
983  Vec_Ptr_t * vPairs;
984  Abc_Obj_t * pNtkMiter;
985 
986  vPairs = Vec_PtrAlloc( 100 );
987 
988  Abc_NtkForEachCo( pMiter, pObj, i )
989  Abc_ObjRemoveFanins( pObj );
990 
991  for(i = 0; i < Vec_PtrSize( oMatchPairs ); i += 2)
992  {
993  Vec_PtrPush( vPairs, Abc_ObjChild0Copy((Abc_Obj_t *)Vec_PtrEntry(oMatchPairs, i)) );
994  Vec_PtrPush( vPairs, Abc_ObjChild0Copy((Abc_Obj_t *)Vec_PtrEntry(oMatchPairs, i+1)) );
995  }
996  pNtkMiter = Abc_AigMiter( (Abc_Aig_t *)pMiter->pManFunc, vPairs, 0 );
997  Abc_ObjAddFanin( Abc_NtkPo(pMiter,0), pNtkMiter );
998  Vec_PtrFree( vPairs);
999  }
1000  else if( mode == 2 ) // add some outputs
1001  {
1002 
1003  }
1004  else if( mode == 3) // remove all outputs
1005  {
1006  }
1007 
1008  if ( pMiter == NULL )
1009  {
1010  printf("Miter computation has failed.");
1011  return -1;
1012  }
1013  RetValue = Abc_NtkMiterIsConstant( pMiter );
1014  if ( RetValue == 0)
1015  {
1016  /*printf("Networks are NOT EQUIVALENT after structural hashing."); */
1017  // report the error
1018  if(mismatch != NULL)
1019  {
1020  pMiter->pModel = Abc_NtkVerifyGetCleanModel( pMiter, 1 );
1021  Abc_NtkVerifyReportError( pNtk1, pNtk2, pMiter->pModel, mismatch );
1022  ABC_FREE( pMiter->pModel );
1023  }
1024  Abc_NtkDelete( pMiter );
1025  return RetValue;
1026  }
1027  if( RetValue == 1 )
1028  {
1029  /*printf("Networks are equivalent after structural hashing."); */
1030  Abc_NtkDelete( pMiter );
1031  return RetValue;
1032  }
1033 
1034  // convert the miter into a CNF
1035  //if(mode == 0)
1036  pCnf = Abc_NtkMulti( pMiter, 0, 100, 1, 0, 0, 0 );
1037  Abc_NtkDelete( pMiter );
1038  if ( pCnf == NULL )
1039  {
1040  printf("Renoding for CNF has failed.");
1041  return -1;
1042  }
1043 
1044  // solve the CNF using the SAT solver
1045  RetValue = Abc_NtkMiterSat( pCnf, (ABC_INT64_T)10000, (ABC_INT64_T)0, 0, NULL, NULL);
1046  /*if ( RetValue == -1 )
1047  printf("Networks are undecided (SAT solver timed out).");
1048  else if ( RetValue == 0 )
1049  printf("Networks are NOT EQUIVALENT after SAT.");
1050  else
1051  printf("Networks are equivalent after SAT."); */
1052  if ( mismatch != NULL && pCnf->pModel )
1053  Abc_NtkVerifyReportError( pNtk1, pNtk2, pCnf->pModel, mismatch );
1054 
1055  ABC_FREE( pCnf->pModel );
1056  Abc_NtkDelete( pCnf );
1057 
1058  return RetValue;
1059 }
1060 
1061 int checkEquivalence( Abc_Ntk_t * pNtk1, Vec_Int_t* matchedInputs1, Vec_Int_t * matchedOutputs1,
1062  Abc_Ntk_t * pNtk2, Vec_Int_t* matchedInputs2, Vec_Int_t * matchedOutputs2)
1063 {
1064  Vec_Ptr_t * iMatchPairs, * oMatchPairs;
1065  int i;
1066  int result;
1067 
1068  iMatchPairs = Vec_PtrAlloc( Abc_NtkPiNum( pNtk1 ) * 2);
1069  oMatchPairs = Vec_PtrAlloc( Abc_NtkPoNum( pNtk1 ) * 2);
1070 
1071  for(i = 0; i < Abc_NtkPiNum(pNtk1); i++)
1072  {
1073  Vec_PtrPush(iMatchPairs, Abc_NtkPi(pNtk2, Vec_IntEntry(matchedInputs2, i)));
1074  Vec_PtrPush(iMatchPairs, Abc_NtkPi(pNtk1, Vec_IntEntry(matchedInputs1, i)));
1075  }
1076 
1077 
1078  for(i = 0; i < Abc_NtkPoNum(pNtk1); i++)
1079  {
1080  Vec_PtrPush(oMatchPairs, Abc_NtkPo(pNtk2, Vec_IntEntry(matchedOutputs2, i)));
1081  Vec_PtrPush(oMatchPairs, Abc_NtkPo(pNtk1, Vec_IntEntry(matchedOutputs1, i)));
1082  }
1083 
1084  result = Abc_NtkBmSat(pNtk1, pNtk2, iMatchPairs, oMatchPairs, NULL, 0);
1085 
1086  if( result )
1087  printf("*** Circuits are equivalent ***\n");
1088  else
1089  printf("*** Circuits are NOT equivalent ***\n");
1090 
1091  Vec_PtrFree( iMatchPairs );
1092  Vec_PtrFree( oMatchPairs );
1093 
1094  return result;
1095 }
1096 
1097 Abc_Ntk_t * computeCofactor(Abc_Ntk_t * pNtk, Vec_Ptr_t ** nodesInLevel, int * bitVector, Vec_Int_t * currInputs)
1098 {
1099  Abc_Ntk_t * subNtk;
1100  Abc_Obj_t * pObj, * pObjNew;
1101  int i, j, numOfLevels;
1102 
1103  numOfLevels = Abc_AigLevel( pNtk ); // number of levels excludes PI/POs
1104 
1105  // start a new network
1106  subNtk = Abc_NtkAlloc( ABC_NTK_STRASH, ABC_FUNC_AIG, 1 );
1107  subNtk->pName = Extra_UtilStrsav("subNtk");
1108 
1109  Abc_AigConst1(pNtk)->pCopy = Abc_AigConst1(subNtk);
1110 
1111  // clean the node copy fields and mark the nodes that need to be copied to the new network
1112  Abc_NtkCleanCopy( pNtk );
1113 
1114  if(bitVector != NULL)
1115  {
1116  for(i = 0; i < Abc_NtkPiNum(pNtk); i++)
1117  if(bitVector[i])
1118  {
1119  pObj = Abc_NtkPi(pNtk, i);
1120  pObj->pCopy = (Abc_Obj_t *)(1);
1121  }
1122  }
1123 
1124  for(i = 0; i < Vec_IntSize(currInputs); i++)
1125  {
1126  pObj = Abc_NtkPi(pNtk, Vec_IntEntry(currInputs, i));
1127  pObjNew = Abc_NtkDupObj( subNtk, pObj, 1 );
1128  pObj->pCopy = pObjNew;
1129  }
1130 
1131 
1132  // i = 0 are the inputs and the inputs are not added to the 2d array ( nodesInLevel )
1133  for( i = 0; i <= numOfLevels; i++ )
1134  for( j = 0; j < Vec_PtrSize( nodesInLevel[i] ); j++)
1135  {
1136  pObj = (Abc_Obj_t *)Vec_PtrEntry( nodesInLevel[i], j );
1137 
1138  if(Abc_ObjChild0Copy(pObj) == NULL && Abc_ObjChild1Copy(pObj) == NULL)
1139  pObj->pCopy = NULL;
1140  else if(Abc_ObjChild0Copy(pObj) == NULL && Abc_ObjChild1Copy(pObj) == (void*)(1))
1141  pObj->pCopy = NULL;
1142  else if(Abc_ObjChild0Copy(pObj) == NULL && (Abc_ObjChild1Copy(pObj) != (NULL) && Abc_ObjChild1Copy(pObj) != (void*)(1)) )
1143  pObj->pCopy = NULL;
1144  else if(Abc_ObjChild0Copy(pObj) == (void*)(1) && Abc_ObjChild1Copy(pObj) == NULL)
1145  pObj->pCopy = NULL;
1146  else if(Abc_ObjChild0Copy(pObj) == (void*)(1) && Abc_ObjChild1Copy(pObj) == (void*)(1))
1147  pObj->pCopy = (Abc_Obj_t *)(1);
1148  else if(Abc_ObjChild0Copy(pObj) == (void*)(1) && (Abc_ObjChild1Copy(pObj) != (NULL) && Abc_ObjChild1Copy(pObj) != (void*)(1)) )
1149  pObj->pCopy = Abc_ObjChild1Copy(pObj);
1150  else if( (Abc_ObjChild0Copy(pObj) != (NULL) && Abc_ObjChild0Copy(pObj) != (void*)(1)) && Abc_ObjChild1Copy(pObj) == NULL )
1151  pObj->pCopy = NULL;
1152  else if( (Abc_ObjChild0Copy(pObj) != (NULL) && Abc_ObjChild0Copy(pObj) != (void*)(1)) && Abc_ObjChild1Copy(pObj) == (void*)(1) )
1153  pObj->pCopy = Abc_ObjChild0Copy(pObj);
1154  else if( (Abc_ObjChild0Copy(pObj) != (NULL) && Abc_ObjChild0Copy(pObj) != (void*)(1)) &&
1155  (Abc_ObjChild1Copy(pObj) != (NULL) && Abc_ObjChild1Copy(pObj) != (void*)(1)) )
1156  pObj->pCopy = Abc_AigAnd( (Abc_Aig_t *)subNtk->pManFunc, Abc_ObjChild0Copy(pObj), Abc_ObjChild1Copy(pObj) );
1157  }
1158 
1159  for(i = 0; i < Abc_NtkPoNum(pNtk); i++)
1160  {
1161  pObj = Abc_NtkPo(pNtk, i);
1162  pObjNew = Abc_NtkDupObj( subNtk, pObj, 1 );
1163 
1164  if( Abc_ObjChild0Copy(pObj) == NULL)
1165  {
1166  Abc_ObjAddFanin( pObjNew, Abc_AigConst1(subNtk));
1167  pObjNew->fCompl0 = 1;
1168  }
1169  else if( Abc_ObjChild0Copy(pObj) == (void*)(1) )
1170  {
1171  Abc_ObjAddFanin( pObjNew, Abc_AigConst1(subNtk));
1172  pObjNew->fCompl0 = 0;
1173  }
1174  else
1175  Abc_ObjAddFanin( pObjNew, Abc_ObjChild0Copy(pObj) );
1176  }
1177 
1178  return subNtk;
1179 }
1180 
1182 
1183 int matchNonSingletonOutputs(Abc_Ntk_t * pNtk1, Vec_Ptr_t ** nodesInLevel1, Vec_Int_t ** iMatch1, Vec_Int_t ** iDep1, Vec_Int_t * matchedInputs1, int * iGroup1, Vec_Int_t ** oMatch1, int * oGroup1,
1184  Abc_Ntk_t * pNtk2, Vec_Ptr_t ** nodesInLevel2, Vec_Int_t ** iMatch2, Vec_Int_t ** iDep2, Vec_Int_t * matchedInputs2, int * iGroup2, Vec_Int_t ** oMatch2, int * oGroup2,
1185  Vec_Int_t * matchedOutputs1, Vec_Int_t * matchedOutputs2, Vec_Int_t * oMatchedGroups, Vec_Int_t * iNonSingleton,
1186  Abc_Ntk_t * subNtk1, Abc_Ntk_t * subNtk2, Vec_Ptr_t * oMatchPairs,
1187  Vec_Int_t * oNonSingleton, int oI, int idx, int ii, int iidx)
1188 {
1189  static int MATCH_FOUND;
1190  int i;
1191  int j, temp;
1192  Vec_Int_t * mismatch;
1193  int * skipList;
1194  static int counter = 0;
1195 
1196  MATCH_FOUND = FALSE;
1197 
1198  if( oI == Vec_IntSize( oNonSingleton ) )
1199  {
1200  if( iNonSingleton != NULL)
1201  if( match1by1(pNtk1, nodesInLevel1, iMatch1, iDep1, matchedInputs1, iGroup1, oMatch1, oGroup1,
1202  pNtk2, nodesInLevel2, iMatch2, iDep2, matchedInputs2, iGroup2, oMatch2, oGroup2,
1203  matchedOutputs1, matchedOutputs2, oMatchedGroups, iNonSingleton, ii, iidx) )
1204  MATCH_FOUND = TRUE;
1205 
1206  if( iNonSingleton == NULL)
1207  MATCH_FOUND = TRUE;
1208 
1209  return MATCH_FOUND;
1210  }
1211 
1212  i = Vec_IntEntry(oNonSingleton, oI);
1213 
1214  mismatch = Vec_IntAlloc(10);
1215 
1216  skipList = ABC_ALLOC(int, Vec_IntSize(oMatch1[i]));
1217 
1218  for(j = 0; j < Vec_IntSize(oMatch1[i]); j++)
1219  skipList[j] = FALSE;
1220 
1221  Vec_PtrPush(oMatchPairs, Abc_NtkPo(subNtk1, Vec_IntEntry(oMatch1[i], idx)) );
1222  Vec_IntPush(matchedOutputs1, Vec_IntEntry(oMatch1[i], idx));
1223 
1224  for(j = 0; j < Vec_IntSize( oMatch2[i] ) && MATCH_FOUND == FALSE; j++)
1225  {
1226  if( Vec_IntEntry(oMatch2[i], j) == -1 || skipList[j] == TRUE)
1227  continue;
1228 
1229  Vec_PtrPush(oMatchPairs, Abc_NtkPo(subNtk2, Vec_IntEntry(oMatch2[i], j)));
1230  Vec_IntPush(matchedOutputs2, Vec_IntEntry(oMatch2[i], j));
1231 
1232  counter++;
1233  if( Abc_NtkBmSat( subNtk1, subNtk2, NULL, oMatchPairs, mismatch, 0) )
1234  {
1235  /*fprintf(matchFile, "%s matched to %s\n", Abc_ObjName(Abc_NtkPo(pNtk1, Vec_IntEntry(oMatch1[i], idx))),
1236  Abc_ObjName(Abc_NtkPo(pNtk2, Vec_IntEntry(oMatch2[i], j)))); */
1237 
1238  temp = Vec_IntEntry(oMatch2[i], j);
1239  Vec_IntWriteEntry(oMatch2[i], j, -1);
1240 
1241  if(idx != Vec_IntSize( oMatch1[i] ) - 1)
1242  // call the same function with idx+1
1243  matchNonSingletonOutputs(pNtk1, nodesInLevel1, iMatch1, iDep1, matchedInputs1, iGroup1, oMatch1, oGroup1,
1244  pNtk2, nodesInLevel2, iMatch2, iDep2, matchedInputs2, iGroup2, oMatch2, oGroup2,
1245  matchedOutputs1, matchedOutputs2, oMatchedGroups, iNonSingleton,
1246  subNtk1, subNtk2, oMatchPairs,
1247  oNonSingleton, oI, idx+1, ii, iidx);
1248  else
1249  // call the same function with idx = 0 and oI++
1250  matchNonSingletonOutputs(pNtk1, nodesInLevel1, iMatch1, iDep1, matchedInputs1, iGroup1, oMatch1, oGroup1,
1251  pNtk2, nodesInLevel2, iMatch2, iDep2, matchedInputs2, iGroup2, oMatch2, oGroup2,
1252  matchedOutputs1, matchedOutputs2, oMatchedGroups, iNonSingleton,
1253  subNtk1, subNtk2, oMatchPairs,
1254  oNonSingleton, oI+1, 0, ii, iidx);
1255 
1256  Vec_IntWriteEntry(oMatch2[i], j, temp);
1257  }
1258  else
1259  {
1260  int * output1, * output2;
1261  int k;
1262  Abc_Obj_t * pObj;
1263  int * pModel;
1264  char * vPiValues;
1265 
1266 
1267  vPiValues = ABC_ALLOC( char, Abc_NtkPiNum(subNtk1) + 1);
1268  vPiValues[Abc_NtkPiNum(subNtk1)] = '\0';
1269 
1270  for(k = 0; k < Abc_NtkPiNum(subNtk1); k++)
1271  vPiValues[k] = '0';
1272 
1273  for(k = 0; k < Vec_IntSize(mismatch); k += 2)
1274  vPiValues[Vec_IntEntry(mismatch, k)] = Vec_IntEntry(mismatch, k+1);
1275 
1276  pModel = ABC_ALLOC( int, Abc_NtkCiNum(subNtk1) );
1277 
1278  Abc_NtkForEachPi( subNtk1, pObj, k )
1279  pModel[k] = vPiValues[k] - '0';
1280  Abc_NtkForEachLatch( subNtk1, pObj, k )
1281  pModel[Abc_NtkPiNum(subNtk1)+k] = pObj->iData - 1;
1282 
1283  output1 = Abc_NtkVerifySimulatePattern( subNtk1, pModel );
1284 
1285  Abc_NtkForEachLatch( subNtk2, pObj, k )
1286  pModel[Abc_NtkPiNum(subNtk2)+k] = pObj->iData - 1;
1287 
1288  output2 = Abc_NtkVerifySimulatePattern( subNtk2, pModel );
1289 
1290 
1291  for(k = 0; k < Vec_IntSize( oMatch1[i] ); k++)
1292  if(output1[Vec_IntEntry(oMatch1[i], idx)] != output2[Vec_IntEntry(oMatch2[i], k)])
1293  {
1294  skipList[k] = TRUE;
1295  /*printf("Output is SKIPPED");*/
1296  }
1297 
1298  ABC_FREE( vPiValues );
1299  ABC_FREE( pModel );
1300  ABC_FREE( output1 );
1301  ABC_FREE( output2 );
1302  }
1303 
1304  if(MATCH_FOUND == FALSE )
1305  {
1306  Vec_PtrPop(oMatchPairs);
1307  Vec_IntPop(matchedOutputs2);
1308  }
1309  }
1310 
1311  if(MATCH_FOUND == FALSE )
1312  {
1313  Vec_PtrPop(oMatchPairs);
1314  Vec_IntPop(matchedOutputs1);
1315  }
1316 
1317  if(MATCH_FOUND && counter != 0)
1318  {
1319  /*printf("Number of OUTPUT SAT instances = %d", counter);*/
1320  counter = 0;
1321  }
1322 
1323  ABC_FREE( mismatch );
1324  ABC_FREE( skipList );
1325 
1326  return MATCH_FOUND;
1327 }
1328 
1329 int match1by1(Abc_Ntk_t * pNtk1, Vec_Ptr_t ** nodesInLevel1, Vec_Int_t ** iMatch1, Vec_Int_t ** iDep1, Vec_Int_t * matchedInputs1, int * iGroup1, Vec_Int_t ** oMatch1, int * oGroup1,
1330  Abc_Ntk_t * pNtk2, Vec_Ptr_t ** nodesInLevel2, Vec_Int_t ** iMatch2, Vec_Int_t ** iDep2, Vec_Int_t * matchedInputs2, int * iGroup2, Vec_Int_t ** oMatch2, int * oGroup2,
1331  Vec_Int_t * matchedOutputs1, Vec_Int_t * matchedOutputs2, Vec_Int_t * oMatchedGroups, Vec_Int_t * iNonSingleton, int ii, int idx)
1332 {
1333  static int MATCH_FOUND = FALSE;
1334  Abc_Ntk_t * subNtk1, * subNtk2;
1335  Vec_Int_t * oNonSingleton;
1336  Vec_Ptr_t * oMatchPairs;
1337  int * skipList;
1338  int j, m;
1339  int i;
1340  static int counter = 0;
1341 
1342  MATCH_FOUND = FALSE;
1343 
1344  if( ii == Vec_IntSize(iNonSingleton) )
1345  {
1346  MATCH_FOUND = TRUE;
1347  return TRUE;
1348  }
1349 
1350  i = Vec_IntEntry(iNonSingleton, ii);
1351 
1352  if( idx == Vec_IntSize(iMatch1[i]) )
1353  {
1354  // call again with the next element in iNonSingleton
1355  return match1by1(pNtk1, nodesInLevel1, iMatch1, iDep1, matchedInputs1, iGroup1, oMatch1, oGroup1,
1356  pNtk2, nodesInLevel2, iMatch2, iDep2, matchedInputs2, iGroup2, oMatch2, oGroup2,
1357  matchedOutputs1, matchedOutputs2, oMatchedGroups, iNonSingleton, ii+1, 0);
1358 
1359  }
1360 
1361  oNonSingleton = Vec_IntAlloc(10);
1362  oMatchPairs = Vec_PtrAlloc(100);
1363  skipList = ABC_ALLOC(int, Vec_IntSize(iMatch1[i]));
1364 
1365  for(j = 0; j < Vec_IntSize(iMatch1[i]); j++)
1366  skipList[j] = FALSE;
1367 
1368  Vec_IntPush(matchedInputs1, Vec_IntEntry(iMatch1[i], idx));
1369  idx++;
1370 
1371  if(idx == 1)
1372  {
1373  for(j = 0; j < Vec_IntSize(iDep1[Vec_IntEntryLast(iMatch1[i])]); j++)
1374  {
1375  if( Vec_IntSize(oMatch1[oGroup1[Vec_IntEntry(iDep1[Vec_IntEntryLast(iMatch1[i])], j)]]) == 1 )
1376  continue;
1377  if( Vec_IntFind( oMatchedGroups, oGroup1[Vec_IntEntry(iDep1[Vec_IntEntryLast(iMatch1[i])], j)]) != -1)
1378  continue;
1379 
1380  Vec_IntPushUnique(oNonSingleton, oGroup1[Vec_IntEntry(iDep1[Vec_IntEntryLast(iMatch1[i])], j)]);
1381  Vec_IntPushUnique(oMatchedGroups, oGroup1[Vec_IntEntry(iDep1[Vec_IntEntryLast(iMatch1[i])], j)]);
1382  }
1383  }
1384 
1385  subNtk1 = computeCofactor(pNtk1, nodesInLevel1, NULL, matchedInputs1);
1386 
1387  for(j = idx-1; j < Vec_IntSize(iMatch2[i]) && MATCH_FOUND == FALSE; j++)
1388  {
1389  int tempJ;
1390  Vec_Int_t * mismatch;
1391 
1392  if( skipList[j] )
1393  continue;
1394 
1395  mismatch = Vec_IntAlloc(10);
1396 
1397  Vec_IntPush(matchedInputs2, Vec_IntEntry(iMatch2[i], j));
1398 
1399  subNtk2 = computeCofactor(pNtk2, nodesInLevel2, NULL, matchedInputs2);
1400 
1401  for(m = 0; m < Vec_IntSize(matchedOutputs1); m++)
1402  {
1403  Vec_PtrPush(oMatchPairs, Abc_NtkPo(subNtk1, Vec_IntEntry(matchedOutputs1, m)));
1404  Vec_PtrPush(oMatchPairs, Abc_NtkPo(subNtk2, Vec_IntEntry(matchedOutputs2, m)));
1405  }
1406 
1407  counter++;
1408 
1409  if( Abc_NtkBmSat( subNtk2, subNtk1, NULL, oMatchPairs, mismatch, 0) )
1410  {
1411  if(idx-1 != j)
1412  {
1413  tempJ = Vec_IntEntry(iMatch2[i], idx-1);
1414  Vec_IntWriteEntry(iMatch2[i], idx-1, Vec_IntEntry(iMatch2[i], j));
1415  Vec_IntWriteEntry(iMatch2[i], j, tempJ);
1416  }
1417 
1418  /*fprintf(matchFile, "%s matched to %s\n", Abc_ObjName(Abc_NtkPi(pNtk1, Vec_IntEntry(iMatch1[i], idx-1))),
1419  Abc_ObjName(Abc_NtkPi(pNtk2, Vec_IntEntry(iMatch2[i], j))));*/
1420 
1421  // we look for a match for outputs in oNonSingleton
1422  matchNonSingletonOutputs(pNtk1, nodesInLevel1, iMatch1, iDep1, matchedInputs1, iGroup1, oMatch1, oGroup1,
1423  pNtk2, nodesInLevel2, iMatch2, iDep2, matchedInputs2, iGroup2, oMatch2, oGroup2,
1424  matchedOutputs1, matchedOutputs2, oMatchedGroups, iNonSingleton,
1425  subNtk1, subNtk2, oMatchPairs, oNonSingleton, 0, 0, ii, idx);
1426 
1427 
1428  if(idx-1 != j)
1429  {
1430  tempJ = Vec_IntEntry(iMatch2[i], idx-1);
1431  Vec_IntWriteEntry(iMatch2[i], idx-1, Vec_IntEntry(iMatch2[i], j));
1432  Vec_IntWriteEntry(iMatch2[i], j, tempJ);
1433  }
1434  }
1435  else
1436  {
1437  Abc_Ntk_t * FpNtk1, * FpNtk2;
1438  int * bitVector1, * bitVector2;
1439  Vec_Int_t * currInputs1, * currInputs2;
1440  Vec_Ptr_t * vSupp;
1441  Abc_Obj_t * pObj;
1442  int suppNum1 = 0;
1443  int * suppNum2;
1444 
1445  bitVector1 = ABC_ALLOC( int, Abc_NtkPiNum(pNtk1) );
1446  bitVector2 = ABC_ALLOC( int, Abc_NtkPiNum(pNtk2) );
1447 
1448  currInputs1 = Vec_IntAlloc(10);
1449  currInputs2 = Vec_IntAlloc(10);
1450 
1451  suppNum2 = ABC_ALLOC(int, Vec_IntSize(iMatch2[i])-idx+1);
1452 
1453  for(m = 0; m < Abc_NtkPiNum(pNtk1); m++)
1454  {
1455  bitVector1[m] = 0;
1456  bitVector2[m] = 0;
1457  }
1458 
1459  for(m = 0; m < Vec_IntSize(iMatch2[i])-idx+1; m++)
1460  suppNum2[m]= 0;
1461 
1462  // First of all set the value of the inputs that are already matched and are in mismatch
1463  for(m = 0; m < Vec_IntSize(mismatch); m += 2)
1464  {
1465  int n = Vec_IntEntry(mismatch, m);
1466 
1467  bitVector1[Vec_IntEntry(matchedInputs1, n)] = Vec_IntEntry(mismatch, m+1);
1468  bitVector2[Vec_IntEntry(matchedInputs2, n)] = Vec_IntEntry(mismatch, m+1);
1469 
1470  }
1471 
1472  for(m = idx-1; m < Vec_IntSize(iMatch1[i]); m++)
1473  {
1474  Vec_IntPush(currInputs1, Vec_IntEntry(iMatch1[i], m));
1475  Vec_IntPush(currInputs2, Vec_IntEntry(iMatch2[i], m));
1476  }
1477 
1478  // Then add all the inputs that are not yet matched to the currInputs
1479  for(m = 0; m < Abc_NtkPiNum(pNtk1); m++)
1480  {
1481  if(Vec_IntFind( matchedInputs1, m ) == -1)
1482  Vec_IntPushUnique(currInputs1, m);
1483 
1484  if(Vec_IntFind( matchedInputs2, m ) == -1)
1485  Vec_IntPushUnique(currInputs2, m);
1486  }
1487 
1488  FpNtk1 = computeCofactor(pNtk1, nodesInLevel1, bitVector1, currInputs1);
1489  FpNtk2 = computeCofactor(pNtk2, nodesInLevel2, bitVector2, currInputs2);
1490 
1491  Abc_NtkForEachPo( FpNtk1, pObj, m )
1492  {
1493  int n;
1494  vSupp = Abc_NtkNodeSupport( FpNtk1, &pObj, 1 );
1495 
1496  for(n = 0; n < vSupp->nSize; n++)
1497  if( Abc_ObjId((Abc_Obj_t *)vSupp->pArray[n]) == 1 )
1498  suppNum1 += Vec_IntFind( matchedOutputs1, m) + 1;
1499 
1500  Vec_PtrFree( vSupp );
1501  }
1502 
1503  Abc_NtkForEachPo( FpNtk2, pObj, m )
1504  {
1505  int n;
1506  vSupp = Abc_NtkNodeSupport( FpNtk2, &pObj, 1 );
1507 
1508  for(n = 0; n < vSupp->nSize; n++)
1509  if( (int)Abc_ObjId((Abc_Obj_t *)vSupp->pArray[n])-1 < (Vec_IntSize(iMatch2[i]))-idx+1 &&
1510  (int)Abc_ObjId((Abc_Obj_t *)vSupp->pArray[n])-1 >= 0)
1511  suppNum2[Abc_ObjId((Abc_Obj_t *)vSupp->pArray[n])-1] += Vec_IntFind( matchedOutputs2, m) + 1;
1512 
1513  Vec_PtrFree( vSupp );
1514  }
1515 
1516  /*if(suppNum1 != 0)
1517  printf("Ntk1 is trigged");
1518 
1519  if(suppNum2[0] != 0)
1520  printf("Ntk2 is trigged");*/
1521 
1522  for(m = 0; m < Vec_IntSize(iMatch2[i])-idx+1; m++)
1523  if(suppNum2[m] != suppNum1)
1524  {
1525  skipList[m+idx-1] = TRUE;
1526  /*printf("input is skipped");*/
1527  }
1528 
1529  Abc_NtkDelete( FpNtk1 );
1530  Abc_NtkDelete( FpNtk2 );
1531  ABC_FREE( bitVector1 );
1532  ABC_FREE( bitVector2 );
1533  Vec_IntFree( currInputs1 );
1534  Vec_IntFree( currInputs2 );
1535  ABC_FREE( suppNum2 );
1536  }
1537 
1538  Vec_PtrClear(oMatchPairs);
1539  Abc_NtkDelete( subNtk2 );
1540  Vec_IntFree(mismatch);
1541 
1542  //Vec_IntWriteEntry(iMatch2[i], j, tempJ);
1543 
1544  if( MATCH_FOUND == FALSE )
1545  Vec_IntPop(matchedInputs2);
1546  }
1547 
1548  if( MATCH_FOUND == FALSE )
1549  {
1550  Vec_IntPop(matchedInputs1);
1551 
1552  if(idx == 1)
1553  {
1554  for(m = 0; m < Vec_IntSize(oNonSingleton); m++)
1555  Vec_IntPop( oMatchedGroups );
1556  }
1557  }
1558 
1559  Vec_IntFree( oNonSingleton );
1560  Vec_PtrFree( oMatchPairs );
1561  ABC_FREE( skipList );
1562  Abc_NtkDelete( subNtk1 );
1563 
1564  if(MATCH_FOUND && counter != 0)
1565  {
1566  /*printf("Number of INPUT SAT instances = %d\n", counter);*/
1567 
1568  counter = 0;
1569  }
1570 
1571  return MATCH_FOUND;
1572 }
1573 
1574 float refineBySAT(Abc_Ntk_t * pNtk1, Vec_Int_t ** iMatch1, int * iGroup1, Vec_Int_t ** iDep1, int* iLastItem1, Vec_Int_t ** oMatch1, int * oGroup1, Vec_Int_t ** oDep1, int* oLastItem1, int * observability1,
1575  Abc_Ntk_t * pNtk2, Vec_Int_t ** iMatch2, int * iGroup2, Vec_Int_t ** iDep2, int* iLastItem2, Vec_Int_t ** oMatch2, int * oGroup2, Vec_Int_t ** oDep2, int* oLastItem2, int * observability2)
1576 {
1577  int i, j;
1578  Abc_Obj_t * pObj;
1579  Vec_Int_t * iNonSingleton;
1580  Vec_Int_t * matchedInputs1, * matchedInputs2;
1581  Vec_Int_t * matchedOutputs1, * matchedOutputs2;
1582  Vec_Ptr_t ** nodesInLevel1, ** nodesInLevel2;
1583  Vec_Int_t * oMatchedGroups;
1584  FILE *result;
1585  int matchFound;
1586  abctime clk = Abc_Clock();
1587  float satTime = 0.0;
1588 
1589  /*matchFile = fopen("satmatch.txt", "w");*/
1590 
1591  iNonSingleton = Vec_IntAlloc(10);
1592 
1593  matchedInputs1 = Vec_IntAlloc( Abc_NtkPiNum(pNtk1) );
1594  matchedInputs2 = Vec_IntAlloc( Abc_NtkPiNum(pNtk2) );
1595 
1596  matchedOutputs1 = Vec_IntAlloc( Abc_NtkPoNum(pNtk1) );
1597  matchedOutputs2 = Vec_IntAlloc( Abc_NtkPoNum(pNtk2) );
1598 
1599  nodesInLevel1 = ABC_ALLOC( Vec_Ptr_t *, Abc_AigLevel( pNtk1 ) + 1); // why numOfLevels+1? because the inputs are in level 0
1600  for(i = 0; i <= Abc_AigLevel( pNtk1 ); i++)
1601  nodesInLevel1[i] = Vec_PtrAlloc( 20 );
1602 
1603  // bucket sort the objects based on their levels
1604  Abc_AigForEachAnd( pNtk1, pObj, i )
1605  Vec_PtrPush(nodesInLevel1[Abc_ObjLevel(pObj)], pObj);
1606 
1607  nodesInLevel2 = ABC_ALLOC( Vec_Ptr_t *, Abc_AigLevel( pNtk2 ) + 1); // why numOfLevels+1? because the inputs are in level 0
1608  for(i = 0; i <= Abc_AigLevel( pNtk2 ); i++)
1609  nodesInLevel2[i] = Vec_PtrAlloc( 20 );
1610 
1611  // bucket sort the objects based on their levels
1612  Abc_AigForEachAnd( pNtk2, pObj, i )
1613  Vec_PtrPush(nodesInLevel2[Abc_ObjLevel(pObj)], pObj);
1614 
1615  oMatchedGroups = Vec_IntAlloc( 10 );
1616 
1617  for(i = 0; i < *iLastItem1; i++)
1618  {
1619  if(Vec_IntSize(iMatch1[i]) == 1)
1620  {
1621  Vec_IntPush(matchedInputs1, Vec_IntEntryLast(iMatch1[i]));
1622  Vec_IntPush(matchedInputs2, Vec_IntEntryLast(iMatch2[i]));
1623  }
1624  else
1625  Vec_IntPush(iNonSingleton, i);
1626  }
1627 
1628  for(i = 0; i < *oLastItem1; i++)
1629  {
1630  if(Vec_IntSize(oMatch1[i]) == 1)
1631  {
1632  Vec_IntPush(matchedOutputs1, Vec_IntEntryLast(oMatch1[i]));
1633  Vec_IntPush(matchedOutputs2, Vec_IntEntryLast(oMatch2[i]));
1634  }
1635  }
1636 
1637  for(i = 0; i < Vec_IntSize(iNonSingleton) - 1; i++)
1638  {
1639  for(j = i + 1; j < Vec_IntSize(iNonSingleton); j++)
1640  if( observability2[Vec_IntEntry(iMatch2[Vec_IntEntry(iNonSingleton, j)], 0)] >
1641  observability2[Vec_IntEntry(iMatch2[Vec_IntEntry(iNonSingleton, i)], 0)] )
1642  {
1643  int temp = Vec_IntEntry(iNonSingleton, i);
1644  Vec_IntWriteEntry( iNonSingleton, i, Vec_IntEntry(iNonSingleton, j) );
1645  Vec_IntWriteEntry( iNonSingleton, j, temp );
1646  }
1647  else if( observability2[Vec_IntEntry(iMatch2[Vec_IntEntry(iNonSingleton, j)], 0)] ==
1648  observability2[Vec_IntEntry(iMatch2[Vec_IntEntry(iNonSingleton, i)], 0)] )
1649  {
1650  if( Vec_IntSize(iMatch2[Vec_IntEntry(iNonSingleton, j)]) < Vec_IntSize(iMatch2[Vec_IntEntry(iNonSingleton, i)]) )
1651  {
1652  int temp = Vec_IntEntry(iNonSingleton, i);
1653  Vec_IntWriteEntry( iNonSingleton, i, Vec_IntEntry(iNonSingleton, j) );
1654  Vec_IntWriteEntry( iNonSingleton, j, temp );
1655  }
1656  }
1657  }
1658 
1659  /*for(i = 0; i < Vec_IntSize(iNonSingleton) - 1; i++)
1660  {
1661  for(j = i + 1; j < Vec_IntSize(iNonSingleton); j++)
1662  if( Vec_IntSize(oDep2[oGroup2[Vec_IntEntryLast(iMatch2[Vec_IntEntry(iNonSingleton, j)])]]) >
1663  Vec_IntSize(oDep2[oGroup2[Vec_IntEntryLast(iMatch2[Vec_IntEntry(iNonSingleton, i)])]]) )
1664  {
1665  int temp = Vec_IntEntry(iNonSingleton, i);
1666  Vec_IntWriteEntry( iNonSingleton, i, Vec_IntEntry(iNonSingleton, j) );
1667  Vec_IntWriteEntry( iNonSingleton, j, temp );
1668  }
1669  else if( Vec_IntSize(oDep2[oGroup2[Vec_IntEntryLast(iMatch2[Vec_IntEntry(iNonSingleton, j)])]]) ==
1670  Vec_IntSize(oDep2[oGroup2[Vec_IntEntryLast(iMatch2[Vec_IntEntry(iNonSingleton, i)])]]) )
1671  {
1672  if( observability2[Vec_IntEntry(iMatch2[Vec_IntEntry(iNonSingleton, j)], 0)] >
1673  observability2[Vec_IntEntry(iMatch2[Vec_IntEntry(iNonSingleton, i)], 0)] )
1674  {
1675  int temp = Vec_IntEntry(iNonSingleton, i);
1676  Vec_IntWriteEntry( iNonSingleton, i, Vec_IntEntry(iNonSingleton, j) );
1677  Vec_IntWriteEntry( iNonSingleton, j, temp );
1678  }
1679  }
1680  }*/
1681 
1682  matchFound = match1by1(pNtk1, nodesInLevel1, iMatch1, iDep1, matchedInputs1, iGroup1, oMatch1, oGroup1,
1683  pNtk2, nodesInLevel2, iMatch2, iDep2, matchedInputs2, iGroup2, oMatch2, oGroup2,
1684  matchedOutputs1, matchedOutputs2, oMatchedGroups, iNonSingleton, 0, 0);
1685 
1686  if( matchFound && Vec_IntSize(matchedOutputs1) != Abc_NtkPoNum(pNtk1) )
1687  {
1688  Vec_Int_t * oNonSingleton;
1689  Vec_Ptr_t * oMatchPairs;
1690  Abc_Ntk_t * subNtk1, * subNtk2;
1691 
1692  oNonSingleton = Vec_IntAlloc( 10 );
1693 
1694  oMatchPairs = Vec_PtrAlloc(Abc_NtkPoNum(pNtk1) * 2);
1695 
1696  for(i = 0; i < *oLastItem1; i++)
1697  if( Vec_IntSize(oMatch1[i]) > 1 && Vec_IntFind( oMatchedGroups, i) == -1 )
1698  Vec_IntPush(oNonSingleton, i);
1699 
1700  subNtk1 = computeCofactor(pNtk1, nodesInLevel1, NULL, matchedInputs1);
1701  subNtk2 = computeCofactor(pNtk2, nodesInLevel2, NULL, matchedInputs2);
1702 
1703  matchFound = matchNonSingletonOutputs(pNtk1, nodesInLevel1, iMatch1, iDep1, matchedInputs1, iGroup1, oMatch1, oGroup1,
1704  pNtk2, nodesInLevel2, iMatch2, iDep2, matchedInputs2, iGroup1, oMatch2, oGroup2,
1705  matchedOutputs1, matchedOutputs2, oMatchedGroups, NULL,
1706  subNtk1, subNtk2, oMatchPairs, oNonSingleton, 0, 0, 0, 0);
1707 
1708  Vec_IntFree( oNonSingleton );
1709  Vec_PtrFree( oMatchPairs );
1710 
1711  Abc_NtkDelete(subNtk1);
1712  Abc_NtkDelete(subNtk2);
1713  }
1714 
1715  satTime = (float)(Abc_Clock() - clk)/(float)(CLOCKS_PER_SEC);
1716 
1717  if( matchFound )
1718  {
1719  checkEquivalence( pNtk1, matchedInputs1, matchedOutputs1, pNtk2, matchedInputs2, matchedOutputs2);
1720 
1721  result = fopen("IOmatch.txt", "w");
1722 
1723  fprintf(result, "I/O = %d / %d \n\n", Abc_NtkPiNum(pNtk1), Abc_NtkPoNum(pNtk1));
1724 
1725  for(i = 0; i < Vec_IntSize(matchedInputs1) ; i++)
1726  fprintf(result, "{%s}\t{%s}\n", Abc_ObjName(Abc_NtkPi(pNtk1, Vec_IntEntry(matchedInputs1, i))), Abc_ObjName(Abc_NtkPi(pNtk2, Vec_IntEntry(matchedInputs2, i))) );
1727 
1728  fprintf(result, "\n-----------------------------------------\n");
1729 
1730  for(i = 0; i < Vec_IntSize(matchedOutputs1) ; i++)
1731  fprintf(result, "{%s}\t{%s}\n", Abc_ObjName(Abc_NtkPo(pNtk1, Vec_IntEntry(matchedOutputs1, i))), Abc_ObjName(Abc_NtkPo(pNtk2, Vec_IntEntry(matchedOutputs2, i))) );
1732 
1733  fclose( result );
1734  }
1735 
1736  Vec_IntFree( matchedInputs1 );
1737  Vec_IntFree( matchedInputs2 );
1738  Vec_IntFree( matchedOutputs1 );
1739  Vec_IntFree( matchedOutputs2 );
1740  Vec_IntFree( iNonSingleton );
1741  Vec_IntFree( oMatchedGroups );
1742 
1743  for(i = 0; i <= Abc_AigLevel( pNtk1 ); i++)
1744  Vec_PtrFree( nodesInLevel1[i] );
1745  for(i = 0; i <= Abc_AigLevel( pNtk2 ); i++)
1746  Vec_PtrFree( nodesInLevel2[i] );
1747 
1748 
1749  ABC_FREE( nodesInLevel1 );
1750  ABC_FREE( nodesInLevel2 );
1751  /*fclose(matchFile);*/
1752 
1753  return satTime;
1754 }
1755 
1756 int checkListConsistency(Vec_Int_t ** iMatch1, Vec_Int_t ** oMatch1, Vec_Int_t ** iMatch2, Vec_Int_t ** oMatch2, int iLastItem1, int oLastItem1, int iLastItem2, int oLastItem2)
1757 {
1758  //int i;
1759 
1760  if(iLastItem1 != iLastItem2 || oLastItem1 != oLastItem2)
1761  return FALSE;
1762 
1763  /*for(i = 0; i < iLastItem1; i++) {
1764  if(Vec_IntSize(iMatch1[i]) != Vec_IntSize(iMatch2[i]))
1765  return FALSE;
1766  }
1767 
1768  for(i = 0; i < oLastItem1; i++) {
1769  if(Vec_IntSize(oMatch1[i]) != Vec_IntSize(oMatch2[i]))
1770  return FALSE;
1771  }*/
1772 
1773  return TRUE;
1774 }
1775 
1776 
1777 void bmGateWay( Abc_Ntk_t * pNtk1, Abc_Ntk_t * pNtk2, int p_equivalence )
1778 {
1779  Vec_Int_t ** iDep1, ** oDep1;
1780  Vec_Int_t ** iDep2, ** oDep2;
1781  Vec_Int_t ** iMatch1, ** oMatch1;
1782  Vec_Int_t ** iMatch2, ** oMatch2;
1783  int * iGroup1, * oGroup1;
1784  int * iGroup2, * oGroup2;
1785  int iLastItem1, oLastItem1;
1786  int iLastItem2, oLastItem2;
1787  int i, j;
1788 
1789  char * vPiValues1, * vPiValues2;
1790  int * observability1, * observability2;
1791  abctime clk = Abc_Clock();
1792  float initTime;
1793  float simulTime;
1794  float satTime;
1795  Vec_Ptr_t ** topOrder1 = NULL, ** topOrder2 = NULL;
1796 
1797  extern void getDependencies(Abc_Ntk_t *pNtk, Vec_Int_t** iDep, Vec_Int_t** oDep);
1798  extern void initMatchList(Abc_Ntk_t *pNtk, Vec_Int_t** iDep, Vec_Int_t** oDep, Vec_Int_t** iMatch, int* iLastItem, Vec_Int_t** oMatch, int* oLastItem, int* iGroup, int* oGroup, int p_equivalence);
1799  extern void iSortDependencies(Abc_Ntk_t *pNtk, Vec_Int_t** iDep, int* oGroup);
1800  extern void oSortDependencies(Abc_Ntk_t *pNtk, Vec_Int_t** oDep, int* iGroup);
1801  extern int iSplitByDep(Abc_Ntk_t *pNtk, Vec_Int_t** iDep, Vec_Int_t** iMatch, int* iGroup, int* iLastItem, int* oGroup);
1802  extern int oSplitByDep(Abc_Ntk_t *pNtk, Vec_Int_t** oDep, Vec_Int_t** oMatch, int* oGroup, int* oLastItem, int* iGroup);
1803  extern Vec_Ptr_t ** findTopologicalOrder(Abc_Ntk_t * pNtk);
1804  extern int refineIOBySimulation(Abc_Ntk_t *pNtk, Vec_Int_t** iMatch, int* iLastItem, int * iGroup, Vec_Int_t** iDep, Vec_Int_t** oMatch, int* oLastItem, int * oGroup, Vec_Int_t** oDep, char * vPiValues, int * observability, Vec_Ptr_t ** topOrder);
1805  extern float refineBySAT(Abc_Ntk_t * pNtk1, Vec_Int_t ** iMatch1, int * iGroup1, Vec_Int_t ** iDep1, int* iLastItem1, Vec_Int_t ** oMatch1, int * oGroup1, Vec_Int_t ** oDep1, int* oLastItem1, int * observability1,
1806  Abc_Ntk_t * pNtk2, Vec_Int_t ** iMatch2, int * iGroup2, Vec_Int_t ** iDep2, int* iLastItem2, Vec_Int_t ** oMatch2, int * oGroup2, Vec_Int_t ** oDep2, int* oLastItem2, int * observability2);
1807  int checkListConsistency(Vec_Int_t ** iMatch1, Vec_Int_t ** oMatch1, Vec_Int_t ** iMatch2, Vec_Int_t ** oMatch2, int iLastItem1, int oLastItem1, int iLastItem2, int oLastItem2);
1808 
1809  iDep1 = ABC_ALLOC( Vec_Int_t*, (unsigned)Abc_NtkPiNum(pNtk1) );
1810  oDep1 = ABC_ALLOC( Vec_Int_t*, (unsigned)Abc_NtkPoNum(pNtk1) );
1811 
1812  iDep2 = ABC_ALLOC( Vec_Int_t*, (unsigned)Abc_NtkPiNum(pNtk2) );
1813  oDep2 = ABC_ALLOC( Vec_Int_t*, (unsigned)Abc_NtkPoNum(pNtk2) );
1814 
1815  iMatch1 = ABC_ALLOC( Vec_Int_t*, (unsigned)Abc_NtkPiNum(pNtk1) );
1816  oMatch1 = ABC_ALLOC( Vec_Int_t*, (unsigned)Abc_NtkPoNum(pNtk1) );
1817 
1818  iMatch2 = ABC_ALLOC( Vec_Int_t*, (unsigned)Abc_NtkPiNum(pNtk2) );
1819  oMatch2 = ABC_ALLOC( Vec_Int_t*, (unsigned)Abc_NtkPoNum(pNtk2) );
1820 
1821  iGroup1 = ABC_ALLOC( int, Abc_NtkPiNum(pNtk1) );
1822  oGroup1 = ABC_ALLOC( int, Abc_NtkPoNum(pNtk1) );
1823 
1824  iGroup2 = ABC_ALLOC( int, Abc_NtkPiNum(pNtk2) );
1825  oGroup2 = ABC_ALLOC( int, Abc_NtkPoNum(pNtk2) );
1826 
1827  vPiValues1 = ABC_ALLOC( char, Abc_NtkPiNum(pNtk1) + 1);
1828  vPiValues1[Abc_NtkPiNum(pNtk1)] = '\0';
1829 
1830  vPiValues2 = ABC_ALLOC( char, Abc_NtkPiNum(pNtk2) + 1);
1831  vPiValues2[Abc_NtkPiNum(pNtk2)] = '\0';
1832 
1833  observability1 = ABC_ALLOC(int, (unsigned)Abc_NtkPiNum(pNtk1));
1834  observability2 = ABC_ALLOC(int, (unsigned)Abc_NtkPiNum(pNtk2));
1835 
1836  for(i = 0; i < Abc_NtkPiNum(pNtk1); i++)
1837  {
1838  iDep1[i] = Vec_IntAlloc( 1 );
1839  iMatch1[i] = Vec_IntAlloc( 1 );
1840 
1841  iDep2[i] = Vec_IntAlloc( 1 );
1842  iMatch2[i] = Vec_IntAlloc( 1 );
1843 
1844  vPiValues1[i] = '0';
1845  vPiValues2[i] = '0';
1846 
1847  observability1[i] = 0;
1848  observability2[i] = 0;
1849  }
1850 
1851  for(i = 0; i < Abc_NtkPoNum(pNtk1); i++)
1852  {
1853  oDep1[i] = Vec_IntAlloc( 1 );
1854  oMatch1[i] = Vec_IntAlloc( 1 );
1855 
1856  oDep2[i] = Vec_IntAlloc( 1 );
1857  oMatch2[i] = Vec_IntAlloc( 1 );
1858  }
1859 
1860  /************* Strashing ************/
1861  pNtk1 = Abc_NtkStrash( pNtk1, 0, 0, 0 );
1862  pNtk2 = Abc_NtkStrash( pNtk2, 0, 0, 0 );
1863  printf("Network strashing is done!\n");
1864  /************************************/
1865 
1866  /******* Getting Dependencies *******/
1867  getDependencies(pNtk1, iDep1, oDep1);
1868  getDependencies(pNtk2, iDep2, oDep2);
1869  printf("Getting dependencies is done!\n");
1870  /************************************/
1871 
1872  /***** Intializing match lists ******/
1873  initMatchList(pNtk1, iDep1, oDep1, iMatch1, &iLastItem1, oMatch1, &oLastItem1, iGroup1, oGroup1, p_equivalence);
1874  initMatchList(pNtk2, iDep2, oDep2, iMatch2, &iLastItem2, oMatch2, &oLastItem2, iGroup2, oGroup2, p_equivalence);
1875  printf("Initializing match lists is done!\n");
1876  /************************************/
1877 
1878  if( !checkListConsistency(iMatch1, oMatch1, iMatch2, oMatch2, iLastItem1, oLastItem1, iLastItem2, oLastItem2) )
1879  {
1880  fprintf( stdout, "I/O dependencies of two circuits are different.\n");
1881  goto freeAndExit;
1882  }
1883 
1884  printf("Refining IOs by dependencies ...");
1885  // split match lists further by checking dependencies
1886  do
1887  {
1888  int iNumOfItemsAdded = 1, oNumOfItemsAdded = 1;
1889 
1890  do
1891  {
1892  if( oNumOfItemsAdded )
1893  {
1894  iSortDependencies(pNtk1, iDep1, oGroup1);
1895  iSortDependencies(pNtk2, iDep2, oGroup2);
1896  }
1897 
1898  if( iNumOfItemsAdded )
1899  {
1900  oSortDependencies(pNtk1, oDep1, iGroup1);
1901  oSortDependencies(pNtk2, oDep2, iGroup2);
1902  }
1903 
1904  if( iLastItem1 < Abc_NtkPiNum(pNtk1) )
1905  {
1906  iSplitByDep(pNtk1, iDep1, iMatch1, iGroup1, &iLastItem1, oGroup1);
1907  if( oLastItem1 < Abc_NtkPoNum(pNtk1) )
1908  oSplitByDep(pNtk1, oDep1, oMatch1, oGroup1, &oLastItem1, iGroup1);
1909  }
1910 
1911  if( iLastItem2 < Abc_NtkPiNum(pNtk2) )
1912  iNumOfItemsAdded = iSplitByDep(pNtk2, iDep2, iMatch2, iGroup2, &iLastItem2, oGroup2);
1913  else
1914  iNumOfItemsAdded = 0;
1915 
1916  if( oLastItem2 < Abc_NtkPoNum(pNtk2) )
1917  oNumOfItemsAdded = oSplitByDep(pNtk2, oDep2, oMatch2, oGroup2, &oLastItem2, iGroup2);
1918  else
1919  oNumOfItemsAdded = 0;
1920 
1921  if(!checkListConsistency(iMatch1, oMatch1, iMatch2, oMatch2, iLastItem1, oLastItem1, iLastItem2, oLastItem2))
1922  {
1923  fprintf( stdout, "I/O dependencies of two circuits are different.\n");
1924  goto freeAndExit;
1925  }
1926  }while(iNumOfItemsAdded != 0 || oNumOfItemsAdded != 0);
1927 
1928  }while(0);
1929 
1930  printf(" done!\n");
1931 
1932  initTime = ((float)(Abc_Clock() - clk)/(float)(CLOCKS_PER_SEC));
1933  clk = Abc_Clock();
1934 
1935  topOrder1 = findTopologicalOrder(pNtk1);
1936  topOrder2 = findTopologicalOrder(pNtk2);
1937 
1938  printf("Refining IOs by simulation ...");
1939 
1940  do
1941  {
1942  int counter = 0;
1943  int ioSuccess1, ioSuccess2;
1944 
1945  do
1946  {
1947  for(i = 0; i < iLastItem1; i++)
1948  {
1949  int temp = (int)(SIM_RANDOM_UNSIGNED % 2);
1950 
1951  if(Vec_IntSize(iMatch1[i]) != Vec_IntSize(iMatch2[i]))
1952  {
1953  fprintf( stdout, "Input refinement by simulation finds two circuits different.\n");
1954  goto freeAndExit;
1955  }
1956 
1957  for(j = 0; j < Vec_IntSize(iMatch1[i]); j++)
1958  {
1959  vPiValues1[Vec_IntEntry(iMatch1[i], j)] = temp + '0';
1960  vPiValues2[Vec_IntEntry(iMatch2[i], j)] = temp + '0';
1961  }
1962  }
1963 
1964  ioSuccess1 = refineIOBySimulation(pNtk1, iMatch1, &iLastItem1, iGroup1, iDep1, oMatch1, &oLastItem1, oGroup1, oDep1, vPiValues1, observability1, topOrder1);
1965  ioSuccess2 = refineIOBySimulation(pNtk2, iMatch2, &iLastItem2, iGroup2, iDep2, oMatch2, &oLastItem2, oGroup2, oDep2, vPiValues2, observability2, topOrder2);
1966 
1967  if(ioSuccess1 && ioSuccess2)
1968  counter = 0;
1969  else
1970  counter++;
1971 
1972  if(ioSuccess1 != ioSuccess2 ||
1973  !checkListConsistency(iMatch1, oMatch1, iMatch2, oMatch2, iLastItem1, oLastItem1, iLastItem2, oLastItem2))
1974  {
1975  fprintf( stdout, "Input refinement by simulation finds two circuits different.\n");
1976  goto freeAndExit;
1977  }
1978  }while(counter <= 200);
1979 
1980  }while(0);
1981 
1982  printf(" done!\n");
1983 
1984  simulTime = (float)(Abc_Clock() - clk)/(float)(CLOCKS_PER_SEC);
1985  printf("SAT-based search started ...\n");
1986 
1987  satTime = refineBySAT(pNtk1, iMatch1, iGroup1, iDep1, &iLastItem1, oMatch1, oGroup1, oDep1, &oLastItem1, observability1,
1988  pNtk2, iMatch2, iGroup2, iDep2, &iLastItem2, oMatch2, oGroup2, oDep2, &oLastItem2, observability2);
1989 
1990  printf( "Init Time = %4.2f\n", initTime );
1991  printf( "Simulation Time = %4.2f\n", simulTime );
1992  printf( "SAT Time = %4.2f\n", satTime );
1993  printf( "Overall Time = %4.2f\n", initTime + simulTime + satTime );
1994 
1995 freeAndExit:
1996 
1997  for(i = 0; i < iLastItem1 ; i++)
1998  {
1999 
2000  Vec_IntFree( iMatch1[i] );
2001  Vec_IntFree( iMatch2[i] );
2002  }
2003 
2004  for(i = 0; i < oLastItem1 ; i++)
2005  {
2006 
2007  Vec_IntFree( oMatch1[i] );
2008  Vec_IntFree( oMatch2[i] );
2009  }
2010 
2011  for(i = 0; i < Abc_NtkPiNum(pNtk1); i++)
2012  {
2013  Vec_IntFree( iDep1[i] );
2014  Vec_IntFree( iDep2[i] );
2015  if(topOrder1 != NULL) {
2016  Vec_PtrFree( topOrder1[i] );
2017  Vec_PtrFree( topOrder2[i] );
2018  }
2019  }
2020 
2021  for(i = 0; i < Abc_NtkPoNum(pNtk1); i++)
2022  {
2023  Vec_IntFree( oDep1[i] );
2024  Vec_IntFree( oDep2[i] );
2025  }
2026 
2027  ABC_FREE( iMatch1 );
2028  ABC_FREE( iMatch2 );
2029  ABC_FREE( oMatch1 );
2030  ABC_FREE( oMatch2 );
2031  ABC_FREE( iDep1 );
2032  ABC_FREE( iDep2 );
2033  ABC_FREE( oDep1 );
2034  ABC_FREE( oDep2 );
2035  ABC_FREE( iGroup1 );
2036  ABC_FREE( iGroup2 );
2037  ABC_FREE( oGroup1 );
2038  ABC_FREE( oGroup2 );
2039  ABC_FREE( vPiValues1 );
2040  ABC_FREE( vPiValues2 );
2041  ABC_FREE( observability1 );
2042  ABC_FREE( observability2 );
2043  if(topOrder1 != NULL) {
2044  ABC_FREE( topOrder1 );
2045  ABC_FREE( topOrder2 );
2046  }
2048 
int iTemp
Definition: abc.h:149
static unsigned Abc_ObjId(Abc_Obj_t *pObj)
Definition: abc.h:329
static void Vec_IntPushOrder(Vec_Int_t *p, int Entry)
Definition: vecInt.h:751
typedefABC_NAMESPACE_HEADER_START struct Vec_Ptr_t_ Vec_Ptr_t
INCLUDES ///.
Definition: vecPtr.h:42
static Abc_Obj_t * Abc_ObjFanin1(Abc_Obj_t *pObj)
Definition: abc.h:374
VOID_HACK free()
#define FALSE
Definition: cudd.h:91
static int Abc_ObjIsCi(Abc_Obj_t *pObj)
Definition: abc.h:351
char lbool
Definition: satVec.h:133
int iData
Definition: abc.h:146
ABC_DLL int * Abc_NtkVerifyGetCleanModel(Abc_Ntk_t *pNtk, int nFrames)
Definition: abcVerify.c:668
ABC_DLL Abc_Obj_t * Abc_AigConst1(Abc_Ntk_t *pNtk)
Definition: abcAig.c:683
unsigned fCompl0
Definition: abc.h:140
int Abc_NtkMiterSatBm(Abc_Ntk_t *pNtk, ABC_INT64_T nConfLimit, ABC_INT64_T nInsLimit, int fVerbose, ABC_INT64_T *pNumConfs, ABC_INT64_T *pNumInspects)
Definition: abcBm.c:869
static int Abc_ObjFaninC1(Abc_Obj_t *pObj)
Definition: abc.h:378
typedefABC_NAMESPACE_IMPL_START struct Vec_Int_t_ Vec_Int_t
DECLARATIONS ///.
Definition: bblif.c:37
#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
Abc_Ntk_t * Abc_NtkMulti(Abc_Ntk_t *pNtk, int nThresh, int nFaninMax, int fCnf, int fMulti, int fSimple, int fFactor)
FUNCTION DEFINITIONS ///.
Definition: abcMulti.c:59
FILE * matchFile
Definition: abcBm.c:1181
#define SIM_RANDOM_UNSIGNED
Definition: sim.h:158
ABC_DLL Abc_Ntk_t * Abc_NtkStrash(Abc_Ntk_t *pNtk, int fAllNodes, int fCleanup, int fRecord)
Definition: abcStrash.c:265
void sat_solver_delete(sat_solver *s)
Definition: satSolver.c:1141
static Abc_Obj_t * Abc_ObjChild1Copy(Abc_Obj_t *pObj)
Definition: abc.h:387
int * pValues1__
Definition: abcBm.c:806
float refineBySAT(Abc_Ntk_t *pNtk1, Vec_Int_t **iMatch1, int *iGroup1, Vec_Int_t **iDep1, int *iLastItem1, Vec_Int_t **oMatch1, int *oGroup1, Vec_Int_t **oDep1, int *oLastItem1, int *observability1, Abc_Ntk_t *pNtk2, Vec_Int_t **iMatch2, int *iGroup2, Vec_Int_t **iDep2, int *iLastItem2, Vec_Int_t **oMatch2, int *oGroup2, Vec_Int_t **oDep2, int *oLastItem2, int *observability2)
Definition: abcBm.c:1574
int checkListConsistency(Vec_Int_t **iMatch1, Vec_Int_t **oMatch1, Vec_Int_t **iMatch2, Vec_Int_t **oMatch2, int iLastItem1, int oLastItem1, int iLastItem2, int oLastItem2)
Definition: abcBm.c:1756
ABC_DLL Abc_Obj_t * Abc_NtkDupObj(Abc_Ntk_t *pNtkNew, Abc_Obj_t *pObj, int fCopyName)
Definition: abcObj.c:337
static int Abc_NtkLatchNum(Abc_Ntk_t *pNtk)
Definition: abc.h:294
static int Abc_ObjFaninC0(Abc_Obj_t *pObj)
Definition: abc.h:377
static int Vec_IntFind(Vec_Int_t *p, int Entry)
Definition: vecInt.h:895
void iSortDependencies(Abc_Ntk_t *pNtk, Vec_Int_t **iDep, int *oGroup)
Definition: abcBm.c:199
int oSplitByDep(Abc_Ntk_t *pNtk, Vec_Int_t **oDep, Vec_Int_t **oMatch, int *oGroup, int *oLastItem, int *iGroup)
Definition: abcBm.c:283
static void Vec_PtrPush(Vec_Ptr_t *p, void *Entry)
Definition: vecPtr.h:606
#define l_True
Definition: SolverTypes.h:84
static int Abc_NtkCiNum(Abc_Ntk_t *pNtk)
Definition: abc.h:287
#define ABC_ALLOC(type, num)
Definition: abc_global.h:229
#define Abc_NtkForEachCo(pNtk, pCo, i)
Definition: abc.h:519
static Abc_Obj_t * Abc_NtkCi(Abc_Ntk_t *pNtk, int i)
Definition: abc.h:317
ABC_DLL char * Abc_ObjAssignName(Abc_Obj_t *pObj, char *pName, char *pSuffix)
Definition: abcNames.c:68
static abctime Abc_Clock()
Definition: abc_global.h:279
static int Vec_PtrSize(Vec_Ptr_t *p)
Definition: vecPtr.h:295
stats_t stats
Definition: satSolver.h:156
static void * Vec_PtrPop(Vec_Ptr_t *p)
Definition: vecPtr.h:677
ABC_DLL void * Abc_NtkMiterSatCreate(Abc_Ntk_t *pNtk, int fAllPrimes)
Definition: abcSat.c:629
static Abc_Obj_t * Abc_ObjFanin0(Abc_Obj_t *pObj)
Definition: abc.h:373
static int Abc_NtkCoNum(Abc_Ntk_t *pNtk)
Definition: abc.h:288
DECLARATIONS ///.
Definition: abcAig.c:52
ABC_DLL void Abc_NtkDelete(Abc_Ntk_t *pNtk)
Definition: abcNtk.c:1233
char * Extra_UtilStrsav(const char *s)
static Abc_Obj_t * Abc_NtkCo(Abc_Ntk_t *pNtk, int i)
Definition: abc.h:318
void initMatchList(Abc_Ntk_t *pNtk, Vec_Int_t **iDep, Vec_Int_t **oDep, Vec_Int_t **iMatch, int *iLastItem, Vec_Int_t **oMatch, int *oLastItem, int *iGroup, int *oGroup, int p_equivalence)
Definition: abcBm.c:101
ABC_DLL void Abc_ObjAddFanin(Abc_Obj_t *pObj, Abc_Obj_t *pFanin)
Definition: abcFanio.c:84
int * pModel
Definition: abc.h:198
ABC_DLL Abc_Ntk_t * Abc_NtkAlloc(Abc_NtkType_t Type, Abc_NtkFunc_t Func, int fUseMemMan)
DECLARATIONS ///.
Definition: abcNtk.c:50
#define Abc_AigForEachAnd(pNtk, pNode, i)
Definition: abc.h:485
static void Vec_IntWriteEntry(Vec_Int_t *p, int i, int Entry)
Definition: bblif.c:285
void * pManFunc
Definition: abc.h:191
ABC_DLL Abc_Obj_t * Abc_AigAnd(Abc_Aig_t *pMan, Abc_Obj_t *p0, Abc_Obj_t *p1)
Definition: abcAig.c:700
static Abc_Obj_t * Abc_ObjChild0Copy(Abc_Obj_t *pObj)
Definition: abc.h:386
int * Abc_NtkSimulateOneNode(Abc_Ntk_t *pNtk, int *pModel, int input, Vec_Ptr_t **topOrder)
Definition: abcBm.c:447
Abc_Obj_t * pCopy
Definition: abc.h:148
static Vec_Int_t * Vec_IntAlloc(int nCap)
FUNCTION DEFINITIONS ///.
Definition: bblif.c:149
int matchNonSingletonOutputs(Abc_Ntk_t *pNtk1, Vec_Ptr_t **nodesInLevel1, Vec_Int_t **iMatch1, Vec_Int_t **iDep1, Vec_Int_t *matchedInputs1, int *iGroup1, Vec_Int_t **oMatch1, int *oGroup1, Abc_Ntk_t *pNtk2, Vec_Ptr_t **nodesInLevel2, Vec_Int_t **iMatch2, Vec_Int_t **iDep2, Vec_Int_t *matchedInputs2, int *iGroup2, Vec_Int_t **oMatch2, int *oGroup2, Vec_Int_t *matchedOutputs1, Vec_Int_t *matchedOutputs2, Vec_Int_t *oMatchedGroups, Vec_Int_t *iNonSingleton, Abc_Ntk_t *subNtk1, Abc_Ntk_t *subNtk2, Vec_Ptr_t *oMatchPairs, Vec_Int_t *oNonSingleton, int oI, int idx, int ii, int iidx)
Definition: abcBm.c:1183
void oSortDependencies(Abc_Ntk_t *pNtk, Vec_Int_t **oDep, int *iGroup)
Definition: abcBm.c:241
static int Abc_ObjLevel(Abc_Obj_t *pObj)
Definition: abc.h:330
Vec_Int_t * Abc_NtkGetCiSatVarNums(Abc_Ntk_t *pNtk)
Definition: abcSat.c:154
ABC_DLL int Abc_AigLevel(Abc_Ntk_t *pNtk)
Definition: abcAig.c:292
static int Vec_IntEntry(Vec_Int_t *p, int i)
Definition: bblif.c:268
ABC_DLL Abc_Obj_t * Abc_AigMiter(Abc_Aig_t *pMan, Vec_Ptr_t *vPairs, int fImplic)
Definition: abcAig.c:789
#define ABC_NAMESPACE_IMPL_END
Definition: abc_global.h:108
ABC_DLL Vec_Ptr_t * Abc_NtkNodeSupport(Abc_Ntk_t *pNtk, Abc_Obj_t **ppNodes, int nNodes)
Definition: abcDfs.c:859
if(last==0)
Definition: sparse_int.h:34
static int Vec_IntPop(Vec_Int_t *p)
ABC_DLL int Abc_NtkMiterSat(Abc_Ntk_t *pNtk, ABC_INT64_T nConfLimit, ABC_INT64_T nInsLimit, int fVerbose, ABC_INT64_T *pNumConfs, ABC_INT64_T *pNumInspects)
FUNCTION DEFINITIONS ///.
Definition: abcSat.c:53
static void Vec_IntPush(Vec_Int_t *p, int Entry)
Definition: bblif.c:468
char * sprintf()
int checkEquivalence(Abc_Ntk_t *pNtk1, Vec_Int_t *matchedInputs1, Vec_Int_t *matchedOutputs1, Abc_Ntk_t *pNtk2, Vec_Int_t *matchedInputs2, Vec_Int_t *matchedOutputs2)
Definition: abcBm.c:1061
#define Abc_NtkForEachLatch(pNtk, pObj, i)
Definition: abc.h:497
static Abc_Obj_t * Abc_NtkCreatePi(Abc_Ntk_t *pNtk)
Definition: abc.h:303
static Abc_Obj_t * Abc_ObjFanout0Ntk(Abc_Obj_t *pObj)
Definition: abc.h:376
int refineIOBySimulation(Abc_Ntk_t *pNtk, Vec_Int_t **iMatch, int *iLastItem, int *iGroup, Vec_Int_t **iDep, Vec_Int_t **oMatch, int *oLastItem, int *oGroup, Vec_Int_t **oDep, char *vPiValues, int *observability, Vec_Ptr_t **topOrder)
Definition: abcBm.c:508
int Abc_NtkBmSat(Abc_Ntk_t *pNtk1, Abc_Ntk_t *pNtk2, Vec_Ptr_t *iMatchPairs, Vec_Ptr_t *oMatchPairs, Vec_Int_t *mismatch, int mode)
Definition: abcBm.c:965
ABC_NAMESPACE_IMPL_START int match1by1(Abc_Ntk_t *pNtk1, Vec_Ptr_t **nodesInLevel1, Vec_Int_t **iMatch1, Vec_Int_t **iDep1, Vec_Int_t *matchedInputs1, int *iGroup1, Vec_Int_t **oMatch1, int *oGroup1, Abc_Ntk_t *pNtk2, Vec_Ptr_t **nodesInLevel2, Vec_Int_t **iMatch2, Vec_Int_t **iDep2, Vec_Int_t *matchedInputs2, int *iGroup2, Vec_Int_t **oMatch2, int *oGroup2, Vec_Int_t *matchedOutputs1, Vec_Int_t *matchedOutputs2, Vec_Int_t *oMatchedGroups, Vec_Int_t *iNonSingleton, int ii, int idx)
Definition: abcBm.c:1329
int iSplitByDep(Abc_Ntk_t *pNtk, Vec_Int_t **iDep, Vec_Int_t **iMatch, int *iGroup, int *iLastItem, int *oGroup)
Definition: abcBm.c:354
Abc_Ntk_t * computeCofactor(Abc_Ntk_t *pNtk, Vec_Ptr_t **nodesInLevel, int *bitVector, Vec_Int_t *currInputs)
Definition: abcBm.c:1097
static int Vec_IntRemove(Vec_Int_t *p, int Entry)
Definition: vecInt.h:915
ABC_DLL void Abc_ObjRemoveFanins(Abc_Obj_t *pObj)
Definition: abcFanio.c:141
ABC_INT64_T inspects
Definition: satVec.h:154
#define ABC_NAMESPACE_IMPL_START
Definition: abc_global.h:107
static void * Vec_PtrEntry(Vec_Ptr_t *p, int i)
Definition: vecPtr.h:362
void Abc_NtkVerifyReportError(Abc_Ntk_t *pNtk1, Abc_Ntk_t *pNtk2, int *pModel, Vec_Int_t *mismatch)
Definition: abcBm.c:808
#define TRUE
Definition: cudd.h:88
int * pValues2__
Definition: abcBm.c:806
ABC_INT64_T conflicts
Definition: satVec.h:154
static int Vec_IntEntryLast(Vec_Int_t *p)
Definition: bblif.c:319
int sat_solver_simplify(sat_solver *s)
Definition: satSolver.c:1276
static int Vec_IntSize(Vec_Int_t *p)
Definition: bblif.c:252
ABC_DLL int Abc_NtkMiterIsConstant(Abc_Ntk_t *pMiter)
Definition: abcMiter.c:679
#define Abc_ObjForEachFanout(pObj, pFanout, i)
Definition: abc.h:526
static Abc_Obj_t * Abc_NtkPo(Abc_Ntk_t *pNtk, int i)
Definition: abc.h:316
#define Abc_NtkForEachCi(pNtk, pCi, i)
Definition: abc.h:515
static int Abc_NtkPoNum(Abc_Ntk_t *pNtk)
Definition: abc.h:286
static Vec_Ptr_t * Vec_PtrAlloc(int nCap)
FUNCTION DEFINITIONS ///.
Definition: vecPtr.h:83
#define ABC_FREE(obj)
Definition: abc_global.h:232
static int Abc_NtkPiNum(Abc_Ntk_t *pNtk)
Definition: abc.h:285
ABC_DLL char * Abc_ObjName(Abc_Obj_t *pNode)
DECLARATIONS ///.
Definition: abcNames.c:48
void sat_solver_store_free(sat_solver *s)
Definition: satSolver.c:1927
static void Abc_NtkIncrementTravId(Abc_Ntk_t *p)
Definition: abc.h:406
#define l_False
Definition: SolverTypes.h:85
Abc_Ntk_t * Abc_NtkMiterBm(Abc_Ntk_t *pNtk1, Abc_Ntk_t *pNtk2, Vec_Ptr_t *iCurrMatch, Vec_Ptr_t *oCurrMatch)
Definition: abcBm.c:698
Vec_Ptr_t * Sim_ComputeFunSupp(Abc_Ntk_t *pNtk, int fVerbose)
Definition: simSupp.c:103
void Abc_NtkDfsReverse_rec(Abc_Obj_t *pNode, Vec_Ptr_t *vNodes)
Definition: abcDfs.c:156
ABC_DLL int Abc_NtkIsDfsOrdered(Abc_Ntk_t *pNtk)
Definition: abcDfs.c:667
static int Vec_IntPushUniqueOrder(Vec_Int_t *p, int Entry)
Definition: vecInt.h:811
ABC_DLL void Abc_NtkCleanCopy(Abc_Ntk_t *pNtk)
Definition: abcUtil.c:507
#define assert(ex)
Definition: util_old.h:213
static void Vec_PtrClear(Vec_Ptr_t *p)
Definition: vecPtr.h:545
Vec_Ptr_t ** findTopologicalOrder(Abc_Ntk_t *pNtk)
Definition: abcBm.c:420
static int result
Definition: cuddGenetic.c:125
static Abc_Obj_t * Abc_NtkPi(Abc_Ntk_t *pNtk, int i)
Definition: abc.h:315
void Sat_SolverPrintStats(FILE *pFile, sat_solver *p)
Definition: satUtil.c:188
ABC_DLL int * Abc_NtkVerifySimulatePattern(Abc_Ntk_t *pNtk, int *pModel)
Definition: abcVerify.c:686
#define Abc_NtkForEachPo(pNtk, pPo, i)
Definition: abc.h:517
static void Vec_IntFree(Vec_Int_t *p)
Definition: bblif.c:235
#define Vec_PtrForEachEntry(Type, vVec, pEntry, i)
MACRO DEFINITIONS ///.
Definition: vecPtr.h:55
static int Vec_IntPushUnique(Vec_Int_t *p, int Entry)
Definition: vecInt.h:832
static void Vec_IntClear(Vec_Int_t *p)
Definition: bblif.c:452
ABC_INT64_T abctime
Definition: abc_global.h:278
static Abc_Obj_t * Abc_NtkCreatePo(Abc_Ntk_t *pNtk)
Definition: abc.h:304
char * pName
Definition: abc.h:158
int * Sat_SolverGetModel(sat_solver *p, int *pVars, int nVars)
Definition: satUtil.c:266
void bmGateWay(Abc_Ntk_t *pNtk1, Abc_Ntk_t *pNtk2, int p_equivalence)
Definition: abcBm.c:1777
void getDependencies(Abc_Ntk_t *pNtk, Vec_Int_t **iDep, Vec_Int_t **oDep)
Definition: abcBm.c:44
#define Abc_NtkForEachPi(pNtk, pPi, i)
Definition: abc.h:513
int Abc_NodeAddClausesTop(sat_solver *pSat, Abc_Obj_t *pNode, Vec_Int_t *vVars)
Definition: abcSat.c:763
static void Abc_NodeSetTravIdCurrent(Abc_Obj_t *p)
Definition: abc.h:409
static void Vec_PtrFree(Vec_Ptr_t *p)
Definition: vecPtr.h:223