abc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
abcExtract.c
Go to the documentation of this file.
1 /**CFile****************************************************************
2 
3  FileName [abcShare.c]
4 
5  SystemName [ABC: Logic synthesis and verification system.]
6 
7  PackageName [Network and node package.]
8 
9  Synopsis [Shared logic extraction.]
10 
11  Author [Alan Mishchenko]
12 
13  Affiliation [UC Berkeley]
14 
15  Date [Ver. 1.0. Started - June 20, 2005.]
16 
17  Revision [$Id: abcShare.c,v 1.00 2005/06/20 00:00:00 alanmi Exp $]
18 
19 ***********************************************************************/
20 
21 #include "base/abc/abc.h"
22 
24 
25 ////////////////////////////////////////////////////////////////////////
26 /// DECLARATIONS ///
27 ////////////////////////////////////////////////////////////////////////
28 
29 #define SHARE_NUM 2
30 
31 typedef struct Abc_ShaMan_t_ Abc_ShaMan_t;
33 {
35  int fVerbose;
42 };
43 
44 static inline word Abc_NtkSharePack( int Lev, int Id ) { return (((word)Lev) << 32) | Id; }
45 static inline int Abc_NtkShareUnpackLev( word Num ) { return (Num >> 32); }
46 static inline int Abc_NtkShareUnpackId( word Num ) { return Num & 0xFFFFFFFF; }
47 
48 
49 ////////////////////////////////////////////////////////////////////////
50 /// FUNCTION DEFINITIONS ///
51 ////////////////////////////////////////////////////////////////////////
52 
53 /**Function*************************************************************
54 
55  Synopsis [Working with the manager.]
56 
57  Description []
58 
59  SideEffects []
60 
61  SeeAlso []
62 
63 ***********************************************************************/
65 {
66  Abc_ShaMan_t * p;
67  p = ABC_CALLOC( Abc_ShaMan_t, 1 );
68  p->pNtk = pNtk;
69  p->vObj2Lit = Vec_IntAlloc( 1000 );
70  return p;
71 }
73 {
74  Vec_Ptr_t * vBucket;
75  int i;
76  Vec_PtrForEachEntry( Vec_Ptr_t *, p->vBuckets, vBucket, i )
77  Vec_VecFree( (Vec_Vec_t *)vBucket );
78  Vec_PtrFreeP( &p->vBuckets );
79  Vec_IntFreeP( &p->vObj2Lit );
80  ABC_FREE( p );
81 }
82 
83 
84 /**Function*************************************************************
85 
86  Synopsis [Collects one multi-input gate.]
87 
88  Description []
89 
90  SideEffects []
91 
92  SeeAlso []
93 
94 ***********************************************************************/
95 Vec_Wrd_t * Abc_NtkShareSuperXor( Abc_Obj_t * pObj, int * pfCompl, int * pCounter )
96 {
97  Abc_Ntk_t * pNtk = Abc_ObjNtk(pObj);
98  Abc_Obj_t * pObjC, * pObj0, * pObj1, * pRoot = NULL;
99  Vec_Wrd_t * vSuper;
100  word Num, NumNext;
101  int i, k, fCompl = 0;
102  assert( !Abc_ObjIsComplement(pObj) );
103  assert( Abc_NodeIsExorType(pObj) );
104  // start iteration
105  vSuper = Vec_WrdAlloc( 10 );
106  Vec_WrdPush( vSuper, Abc_NtkSharePack(Abc_ObjLevel(pObj), Abc_ObjId(pObj)) );
107  while ( Vec_WrdSize(vSuper) > 0 )
108  {
109  // make sure there are no duplicates
110  Num = Vec_WrdEntry( vSuper, 0 );
111  Vec_WrdForEachEntryStart( vSuper, NumNext, i, 1 )
112  {
113  assert( Num < NumNext );
114  Num = NumNext;
115  }
116  // extract XOR gate decomposable on the topmost level
117  Vec_WrdForEachEntryReverse( vSuper, Num, i )
118  {
119  pRoot = Abc_NtkObj( pNtk, Abc_NtkShareUnpackId(Num) );
120  if ( Abc_NodeIsExorType(pRoot) )
121  {
122  Vec_WrdRemove( vSuper, Num );
123  break;
124  }
125  }
126  if ( i == -1 )
127  break;
128  // extract
129  pObjC = Abc_NodeRecognizeMux( pRoot, &pObj1, &pObj0 );
130  assert( pObj1 == Abc_ObjNot(pObj0) );
131  fCompl ^= Abc_ObjIsComplement(pObjC); pObjC = Abc_ObjRegular(pObjC);
132  fCompl ^= Abc_ObjIsComplement(pObj0); pObj0 = Abc_ObjRegular(pObj0);
133  Vec_WrdPushOrder( vSuper, Abc_NtkSharePack(Abc_ObjLevel(pObjC), Abc_ObjId(pObjC)) );
134  Vec_WrdPushOrder( vSuper, Abc_NtkSharePack(Abc_ObjLevel(pObj0), Abc_ObjId(pObj0)) );
135  (*pCounter)++;
136  // remove duplicates
137  k = 0;
138  Vec_WrdForEachEntry( vSuper, Num, i )
139  {
140  if ( i + 1 == Vec_WrdSize(vSuper) )
141  {
142  Vec_WrdWriteEntry( vSuper, k++, Num );
143  break;
144  }
145  NumNext = Vec_WrdEntry( vSuper, i+1 );
146  assert( Num <= NumNext );
147  if ( Num == NumNext )
148  i++;
149  else
150  Vec_WrdWriteEntry( vSuper, k++, Num );
151  }
152  Vec_WrdShrink( vSuper, k );
153  }
154  *pfCompl = fCompl;
155  Vec_WrdForEachEntry( vSuper, Num, i )
156  Vec_WrdWriteEntry( vSuper, i, Abc_NtkShareUnpackId(Num) );
157  return vSuper;
158 }
159 Vec_Wrd_t * Abc_NtkShareSuperAnd( Abc_Obj_t * pObj, int * pCounter )
160 {
161  Abc_Ntk_t * pNtk = Abc_ObjNtk(pObj);
162  Abc_Obj_t * pObj0, * pObj1, * pRoot = NULL;
163  Vec_Wrd_t * vSuper;
164  word Num, NumNext;
165  int i, k;
166  assert( !Abc_ObjIsComplement(pObj) );
167  // start iteration
168  vSuper = Vec_WrdAlloc( 10 );
169  Vec_WrdPush( vSuper, Abc_NtkSharePack(Abc_ObjLevel(pObj), Abc_ObjToLit(pObj)) );
170  while ( Vec_WrdSize(vSuper) > 0 )
171  {
172  // make sure there are no duplicates
173  Num = Vec_WrdEntry( vSuper, 0 );
174  Vec_WrdForEachEntryStart( vSuper, NumNext, i, 1 )
175  {
176  assert( Num < NumNext );
177  Num = NumNext;
178  }
179  // extract AND gate decomposable on the topmost level
180  Vec_WrdForEachEntryReverse( vSuper, Num, i )
181  {
182  pRoot = Abc_ObjFromLit( pNtk, Abc_NtkShareUnpackId(Num) );
183  if ( !Abc_ObjIsComplement(pRoot) && Abc_ObjIsNode(pRoot) )
184  {
185  Vec_WrdRemove( vSuper, Num );
186  break;
187  }
188  }
189  if ( i == -1 )
190  break;
191  assert( Abc_ObjIsNode(pRoot) );
192  // extract
193  pObj0 = Abc_ObjChild0(pRoot);
194  pObj1 = Abc_ObjChild1(pRoot);
199  (*pCounter)++;
200  // remove duplicates
201  k = 0;
202  Vec_WrdForEachEntry( vSuper, Num, i )
203  {
204  if ( i + 1 == Vec_WrdSize(vSuper) )
205  {
206  Vec_WrdWriteEntry( vSuper, k++, Num );
207  break;
208  }
209  NumNext = Vec_WrdEntry( vSuper, i+1 );
210  assert( Num <= NumNext );
211  if ( Num + 1 == NumNext && (NumNext & 1) ) // pos_lit & neg_lit = 0
212  {
213  Vec_WrdClear( vSuper );
214  return vSuper;
215  }
216  if ( Num < NumNext )
217  Vec_WrdWriteEntry( vSuper, k++, Num );
218  }
219  Vec_WrdShrink( vSuper, k );
220  }
221  Vec_WrdForEachEntry( vSuper, Num, i )
222  Vec_WrdWriteEntry( vSuper, i, Abc_NtkShareUnpackId(Num) );
223  return vSuper;
224 }
225 
226 /**Function*************************************************************
227 
228  Synopsis [Creates multi-input XOR representation for the nodes.]
229 
230  Description []
231 
232  SideEffects []
233 
234  SeeAlso []
235 
236 ***********************************************************************/
238 {
239  if ( Abc_NodeIsTravIdCurrent( pObj ) )
240  return;
241  Abc_NodeSetTravIdCurrent( pObj );
242  if ( Abc_ObjIsCi(pObj) )
243  return;
244  assert( Abc_ObjIsNode(pObj) );
245  if ( Abc_NodeIsExorType(pObj) )
246  {
247  Vec_Wrd_t * vSuper;
248  int k, fCompl;
249  word Num;
250  vSuper = Abc_NtkShareSuperXor( pObj, &fCompl, &p->nFoundGates );
251  if ( Vec_WrdSize(vSuper) <= 1 || Vec_WrdSize(vSuper) >= p->nMultiSize )
252  {
253  Vec_WrdForEachEntry( vSuper, Num, k )
254  {
255  Vec_Int_t * vInput = (Vec_Int_t *)Vec_PtrEntry( vInputs, (int)Num );
256  if ( vInput == NULL )
257  {
258  vInput = Vec_IntAlloc( 10 );
259  Vec_IntPush( vInput, Abc_Var2Lit((int)Num, 0) );
260  Vec_IntPush( vInput, Abc_ObjLevel(Abc_NtkObj(p->pNtk, (int)Num)) );
261  assert( SHARE_NUM == Vec_IntSize(vInput) );
262  Vec_PtrWriteEntry( vInputs, (int)Num, vInput );
263  }
264  Vec_IntPush( vInput, Vec_IntSize(p->vObj2Lit) );
265  }
266  Vec_IntPush( p->vObj2Lit, Abc_Var2Lit(Abc_ObjId(pObj), fCompl) );
267  }
268  // call recursively
269  Vec_WrdForEachEntry( vSuper, Num, k )
270  Abc_NtkTraverseSupersXor_rec( p, Abc_NtkObj(p->pNtk, (int)Num), vInputs );
271  Vec_WrdFree( vSuper );
272  }
273  else
274  {
275  Abc_NtkTraverseSupersXor_rec( p, Abc_ObjFanin0(pObj), vInputs );
276  Abc_NtkTraverseSupersXor_rec( p, Abc_ObjFanin1(pObj), vInputs );
277  }
278 }
280 {
281  Vec_Wrd_t * vSuper;
282  word Num;
283  int k;
284  if ( Abc_NodeIsTravIdCurrent( pObj ) )
285  return;
286  Abc_NodeSetTravIdCurrent( pObj );
287  if ( Abc_ObjIsCi(pObj) )
288  return;
289  assert( Abc_ObjIsNode(pObj) );
290  vSuper = Abc_NtkShareSuperAnd( pObj, &p->nFoundGates );
291  if ( Vec_WrdSize(vSuper) <= 1 || Vec_WrdSize(vSuper) >= p->nMultiSize )
292  {
293  Vec_WrdForEachEntry( vSuper, Num, k )
294  {
295  Vec_Int_t * vInput = (Vec_Int_t *)Vec_PtrEntry( vInputs, (int)Num );
296  if ( vInput == NULL )
297  {
298  vInput = Vec_IntAlloc( 10 );
299  Vec_IntPush( vInput, (int)Num );
300  Vec_IntPush( vInput, Abc_ObjLevel(Abc_NtkObj(p->pNtk, Abc_Lit2Var((int)Num))) );
301  assert( SHARE_NUM == Vec_IntSize(vInput) );
302  Vec_PtrWriteEntry( vInputs, (int)Num, vInput );
303  }
304  Vec_IntPush( vInput, Vec_IntSize(p->vObj2Lit) );
305  }
306  Vec_IntPush( p->vObj2Lit, Abc_ObjToLit(pObj) );
307  }
308  // call recursively
309  Vec_WrdForEachEntry( vSuper, Num, k )
310  Abc_NtkTraverseSupersAnd_rec( p, Abc_NtkObj(p->pNtk, Abc_Lit2Var((int)Num)), vInputs );
311  Vec_WrdFree( vSuper );
312 }
314 {
315  Vec_Ptr_t * vInputs;
316  Vec_Int_t * vInput;
317  Abc_Obj_t * pObj;
318  int i, nOnesMax;
319 
320  // create mapping of nodes into their column vectors
321  vInputs = Vec_PtrStart( Abc_NtkObjNumMax(p->pNtk) * (1 + fAnd) );
323  if ( fAnd )
324  {
325  Abc_NtkForEachCo( p->pNtk, pObj, i )
326  if ( Abc_ObjIsNode(Abc_ObjFanin0(pObj)) )
327  Abc_NtkTraverseSupersAnd_rec( p, Abc_ObjFanin0(pObj), vInputs );
328  }
329  else
330  {
331  Abc_NtkForEachCo( p->pNtk, pObj, i )
332  if ( Abc_ObjIsNode(Abc_ObjFanin0(pObj)) )
333  Abc_NtkTraverseSupersXor_rec( p, Abc_ObjFanin0(pObj), vInputs );
334  }
336 
337  // find the largest number of 1s
338  nOnesMax = 0;
339  Vec_PtrForEachEntry( Vec_Int_t *, vInputs, vInput, i )
340  if ( vInput )
341  nOnesMax = Abc_MaxInt( nOnesMax, Vec_IntSize(vInput)-SHARE_NUM );
342 
343  // create buckets
344  assert( p->vBuckets == NULL );
345  p->vBuckets = Vec_PtrAlloc( nOnesMax + 1 );
346  for ( i = 0; i <= nOnesMax; i++ )
347  Vec_PtrPush( p->vBuckets, Vec_PtrAlloc(10) );
348 
349  // load vectors into buckets
350  Vec_PtrForEachEntry( Vec_Int_t *, vInputs, vInput, i )
351  if ( vInput )
352  Vec_PtrPush( (Vec_Ptr_t *)Vec_PtrEntry(p->vBuckets, Vec_IntSize(vInput)-SHARE_NUM), vInput );
353  Vec_PtrFree( vInputs );
354 }
355 
356 /**Function*************************************************************
357 
358  Synopsis []
359 
360  Description []
361 
362  SideEffects []
363 
364  SeeAlso []
365 
366 ***********************************************************************/
368 {
369  Vec_Ptr_t * vBucket;
370  Vec_Int_t * vInput;
371  int i, k, j, ObjId;
372  char * pBuffer = ABC_ALLOC( char, Vec_IntSize(p->vObj2Lit) + 1 );
373  int * pCounters = ABC_CALLOC( int, Vec_IntSize(p->vObj2Lit) + 1 );
374  int nTotal = 0;
375  Vec_PtrForEachEntry( Vec_Ptr_t *, p->vBuckets, vBucket, i )
376  Vec_PtrForEachEntry( Vec_Int_t *, vBucket, vInput, j )
377  {
378  for ( k = 0; k < Vec_IntSize(p->vObj2Lit); k++ )
379  pBuffer[k] = '0';
380  pBuffer[k] = 0;
381 
382  Vec_IntForEachEntryStart( vInput, ObjId, k, SHARE_NUM )
383  {
384  assert( ObjId < Vec_IntSize(p->vObj2Lit) );
385  pBuffer[ObjId] = '1';
386  pCounters[ObjId]++;
387  }
388  printf( "%4d%3d: %s\n", Vec_IntEntry(vInput, 0), Vec_IntEntry(vInput, 1), pBuffer );
389  }
390 
391  for ( i = 0; i < Vec_IntSize(p->vObj2Lit); i++ )
392  if ( pCounters[i] > 0 )
393  printf( "%d=%d ", i, pCounters[i] );
394  printf( "\n" );
395 
396  nTotal = 0;
397  for ( i = 0; i < p->nStartCols; i++ )
398  nTotal += pCounters[i] - 1;
399  printf( "Total = %d. ", nTotal );
400  printf( "Gates = %d.\n", Vec_IntSize(p->vObj2Lit) - p->nStartCols + nTotal );
401 
402  ABC_FREE( pCounters );
403  ABC_FREE( pBuffer );
404 
405  printf( "Bucket contents: " );
406  Vec_PtrForEachEntry( Vec_Ptr_t *, p->vBuckets, vBucket, i )
407  printf( "%d ", Vec_PtrSize(vBucket) );
408  printf( "\n" );
409 }
410 
411 /**Function*************************************************************
412 
413  Synopsis []
414 
415  Description []
416 
417  SideEffects []
418 
419  SeeAlso []
420 
421 ***********************************************************************/
423 {
424  FILE * pFile;
425  Vec_Ptr_t * vSupp;
426  Abc_Obj_t * pObj;
427  int i, k;
428  pFile = fopen( "multi_and.blif", "wb" );
429  if ( pFile == NULL )
430  {
431  printf( "Cannot open output file.\n" );
432  return;
433  }
434  fprintf( pFile, ".model %s\n", "multi_and" );
435  fprintf( pFile, ".inputs" );
436  for ( i = 0; i < Abc_NtkCiNum(p); i++ )
437  fprintf( pFile, " i%d", i );
438  fprintf( pFile, "\n" );
439  fprintf( pFile, ".outputs" );
440  for ( i = 0; i < Abc_NtkCoNum(p); i++ )
441  fprintf( pFile, " o%d", i );
442  fprintf( pFile, "\n" );
443  Abc_NtkForEachCi( p, pObj, i )
444  pObj->iTemp = i;
445  for ( i = 0; i < Abc_NtkCoNum(p); i++ )
446  {
447  pObj = Abc_NtkCo( p, i );
448  vSupp = Abc_NtkNodeSupport( p, &pObj, 1 );
449  fprintf( pFile, ".names" );
450  Vec_PtrForEachEntry( Abc_Obj_t *, vSupp, pObj, k )
451  fprintf( pFile, " i%d", pObj->iTemp );
452  fprintf( pFile, " o%d\n", i );
453  Vec_PtrForEachEntry( Abc_Obj_t *, vSupp, pObj, k )
454  fprintf( pFile, "1" );
455  fprintf( pFile, " 1\n" );
456  Vec_PtrFree( vSupp );
457  }
458  fprintf( pFile, ".end\n\n" );
459  fclose( pFile );
460 }
461 
462 
463 /**Function*************************************************************
464 
465  Synopsis []
466 
467  Description []
468 
469  SideEffects []
470 
471  SeeAlso []
472 
473 ***********************************************************************/
474 void Abc_NtkShareFindBestMatch( Vec_Ptr_t * vBuckets, Vec_Int_t ** pvInput, Vec_Int_t ** pvInput2 )
475 {
476  int nPoolSize = 40;
477  Vec_Ptr_t * vPool = Vec_PtrAlloc( nPoolSize );
478  Vec_Ptr_t * vBucket;
479  Vec_Int_t * vInput, * vInput2, * vInputBest = NULL, * vInputBest2 = NULL;
480  int i, k, Cost, CostBest = 0, Delay, DelayBest = 0;
481 
482  Vec_PtrForEachEntryReverse( Vec_Ptr_t *, vBuckets, vBucket, i )
483  Vec_PtrForEachEntry( Vec_Int_t *, vBucket, vInput, k )
484  {
485  Vec_PtrPush( vPool, vInput );
486  if ( Vec_PtrSize(vPool) == nPoolSize )
487  goto outside;
488  }
489 outside:
490 
491  Vec_PtrForEachEntryReverse( Vec_Int_t *, vPool, vInput, i )
492  Vec_PtrForEachEntryReverse( Vec_Int_t *, vPool, vInput2, k )
493  {
494  if ( i == k )
495  continue;
496 
497  vInput->pArray += SHARE_NUM;
498  vInput2->pArray += SHARE_NUM;
499  vInput->nSize -= SHARE_NUM;
500  vInput2->nSize -= SHARE_NUM;
501 
502  Cost = Vec_IntTwoCountCommon(vInput, vInput2);
503 
504  vInput->pArray -= SHARE_NUM;
505  vInput2->pArray -= SHARE_NUM;
506  vInput->nSize += SHARE_NUM;
507  vInput2->nSize += SHARE_NUM;
508 
509  if ( Cost < 2 )
510  continue;
511 
512  Delay = Abc_MaxInt( Vec_IntEntry(vInput, 1), Vec_IntEntry(vInput2, 1) );
513 
514  if ( CostBest < Cost || (CostBest == Cost && (DelayBest > Delay)) )
515  {
516  CostBest = Cost;
517  DelayBest = Delay;
518  vInputBest = vInput;
519  vInputBest2 = vInput2;
520  }
521  }
522  Vec_PtrFree( vPool );
523 
524  *pvInput = vInputBest;
525  *pvInput2 = vInputBest2;
526 
527  if ( vInputBest == NULL )
528  return;
529 
530  Vec_PtrRemove( (Vec_Ptr_t *)Vec_PtrEntry(vBuckets, Vec_IntSize(vInputBest)-SHARE_NUM), (Vec_Int_t *)vInputBest );
531  Vec_PtrRemove( (Vec_Ptr_t *)Vec_PtrEntry(vBuckets, Vec_IntSize(vInputBest2)-SHARE_NUM), (Vec_Int_t *)vInputBest2 );
532 }
534 {
535  Abc_Obj_t * pObj, * pObj0, * pObj1;
536  Vec_Int_t * vInput, * vInput2;
537  Vec_Int_t * vNew, * vOld1, * vOld2;
538  int i;
539  for ( i = 0; ; i++ )
540  {
541  Abc_NtkShareFindBestMatch( p->vBuckets, &vInput, &vInput2 );
542  if ( vInput == NULL )
543  break;
544 
545  // create new node
546  pObj0 = Abc_ObjFromLit( p->pNtk, Vec_IntEntry(vInput, 0) );
547  pObj1 = Abc_ObjFromLit( p->pNtk, Vec_IntEntry(vInput2, 0) );
548  if ( fAnd )
549  pObj = Abc_AigAnd( (Abc_Aig_t *)p->pNtk->pManFunc, pObj0, pObj1 );
550  else
551  pObj = Abc_AigXor( (Abc_Aig_t *)p->pNtk->pManFunc, pObj0, pObj1 );
552  p->nCountGates++;
553 
554  // save new node
555  vOld1 = Vec_IntAlloc( 16 ); Vec_IntPush( vOld1, Vec_IntEntry(vInput, 0) ); Vec_IntPush( vOld1, Vec_IntEntry(vInput, 1) );
556  vOld2 = Vec_IntAlloc( 16 ); Vec_IntPush( vOld2, Vec_IntEntry(vInput2, 0) ); Vec_IntPush( vOld2, Vec_IntEntry(vInput2, 1) );
557  vNew = Vec_IntAlloc( 16 ); Vec_IntPush( vNew, Abc_ObjToLit(pObj) ); Vec_IntPush( vNew, Abc_ObjLevel(Abc_ObjRegular(pObj)) );
558 
559  // compute new arrays
560  vInput->pArray += SHARE_NUM;
561  vInput2->pArray += SHARE_NUM;
562  vInput->nSize -= SHARE_NUM;
563  vInput2->nSize -= SHARE_NUM;
564 
565  Vec_IntTwoSplit( vInput, vInput2, vNew, vOld1, vOld2 );
566 
567  vInput->pArray -= SHARE_NUM;
568  vInput2->pArray -= SHARE_NUM;
569  vInput->nSize += SHARE_NUM;
570  vInput2->nSize += SHARE_NUM;
571 
572  // add to the old ones
573  Vec_IntPush( vOld1, Vec_IntSize(p->vObj2Lit) );
574  Vec_IntPush( vOld2, Vec_IntSize(p->vObj2Lit) );
575  Vec_IntPush( p->vObj2Lit, Abc_ObjToLit(pObj) );
576 
580 
581  Vec_IntFree( vInput );
582  Vec_IntFree( vInput2 );
583  }
584 }
585 
586 /**Function*************************************************************
587 
588  Synopsis []
589 
590  Description []
591 
592  SideEffects []
593 
594  SeeAlso []
595 
596 ***********************************************************************/
598 {
599  Abc_Ntk_t * pNtk;
600  Vec_Int_t * vInput, * vMap2Repl;
601  Vec_Ptr_t * vOrig, * vRepl, * vBucket;
602  Abc_Obj_t * pObj, * pNew;
603  int i, j, k, ObjId, iLit;
604  int iLitConst1 = Abc_ObjToLit( Abc_AigConst1(p->pNtk) );
605 
606  vOrig = Vec_PtrAlloc( p->nStartCols );
607  vRepl = Vec_PtrAlloc( p->nStartCols );
608  for ( i = 0; i < p->nStartCols; i++ )
609  {
610  iLit = Vec_IntEntry( p->vObj2Lit, i );
611  assert( !fAnd || !Abc_LitIsCompl(iLit) );
612 
613  pObj = Abc_NtkObj( p->pNtk, Abc_Lit2Var(iLit) );
614 
615  if ( fAnd )
616  pNew = Abc_AigConst1(p->pNtk);
617  else
618  pNew = Abc_ObjNotCond( Abc_AigConst1(p->pNtk), !Abc_LitIsCompl(iLit) );
619 
620  Vec_PtrPush( vOrig, pObj );
621  Vec_PtrPush( vRepl, pNew );
622 
623  p->nCountGates--;
624  }
625 
626  // go through the columns
627  Vec_PtrForEachEntry( Vec_Ptr_t *, p->vBuckets, vBucket, i )
628  Vec_PtrForEachEntry( Vec_Int_t *, vBucket, vInput, j )
629  {
630  Vec_IntForEachEntryStart( vInput, ObjId, k, SHARE_NUM )
631  {
632  assert( ObjId < Vec_IntSize(p->vObj2Lit) );
633  if ( ObjId >= p->nStartCols )
634  break;
635  assert( ObjId < p->nStartCols );
636  iLit = Vec_IntEntry( vInput, 0 );
637 
638  pNew = (Abc_Obj_t *)Vec_PtrEntry( vRepl, ObjId );
639  if ( fAnd )
640  pNew = Abc_AigAnd( (Abc_Aig_t *)p->pNtk->pManFunc, pNew, Abc_ObjFromLit(p->pNtk, iLit) );
641  else
642  pNew = Abc_AigXor( (Abc_Aig_t *)p->pNtk->pManFunc, pNew, Abc_ObjFromLit(p->pNtk, iLit) );
643  Vec_PtrWriteEntry( vRepl, ObjId, pNew );
644  p->nCountGates++;
645  }
646  }
647 
648  if ( p->fVerbose )
649  printf( "Total gates collected = %d. Total gates constructed = %d.\n", p->nFoundGates, p->nCountGates );
650 
651  // create map of originals
652  vMap2Repl = Vec_IntStartFull( Abc_NtkObjNumMax(p->pNtk) );
653  Vec_PtrForEachEntry( Abc_Obj_t *, vOrig, pObj, i )
654  {
655 // printf( "Replacing %d by %d.\n", Abc_ObjId(pObj), Abc_ObjToLit((Abc_Obj_t *)Vec_PtrEntry(vRepl, i)) );
656  Vec_IntWriteEntry( vMap2Repl, Abc_ObjId(pObj), Abc_ObjToLit((Abc_Obj_t *)Vec_PtrEntry(vRepl, i)) );
657  }
658  Vec_PtrFree( vOrig );
659  Vec_PtrFree( vRepl );
660 
661  // update fanin pointers
662  Abc_NtkForEachObj( p->pNtk, pObj, i )
663  {
664  if ( Abc_ObjIsCo(pObj) || Abc_ObjIsNode(pObj) )
665  {
666  iLit = Vec_IntEntry( vMap2Repl, Abc_ObjFaninId0(pObj) );
667  if ( iLit >= 0 )
668  {
669  if ( iLit == iLitConst1 && fAnd )
670  {
671  pObj->fCompl0 ^= 1;
672  Vec_IntWriteEntry( &pObj->vFanins, 0, Abc_Lit2Var(iLitConst1) );
673  }
674  else
675  {
676  pObj->fCompl0 ^= Abc_LitIsCompl(iLit);
677  Vec_IntWriteEntry( &pObj->vFanins, 0, Abc_Lit2Var(iLit) );
678  }
679  }
680  }
681  if ( Abc_ObjIsNode(pObj) )
682  {
683  iLit = Vec_IntEntry( vMap2Repl, Abc_ObjFaninId1(pObj) );
684  if ( iLit >= 0 )
685  {
686  if ( iLit == iLitConst1 && fAnd )
687  {
688  pObj->fCompl1 ^= 1;
689  Vec_IntWriteEntry( &pObj->vFanins, 1, Abc_Lit2Var(iLitConst1) );
690  }
691  else
692  {
693  pObj->fCompl1 ^= Abc_LitIsCompl(iLit);
694  Vec_IntWriteEntry( &pObj->vFanins, 1, Abc_Lit2Var(iLit) );
695  }
696  }
697  }
698  }
699  Vec_IntFree( vMap2Repl );
700 
701 // pNtk = Abc_NtkRestrash( p->pNtk, 1 );
702  if ( fAnd )
703  pNtk = Abc_NtkBalance( p->pNtk, 0, 0, 1 );
704  else
705  pNtk = Abc_NtkBalanceExor( p->pNtk, 1, 0 );
706  return pNtk;
707 }
708 
709 /**Function*************************************************************
710 
711  Synopsis [Extracts one multi-output XOR.]
712 
713  Description []
714 
715  SideEffects []
716 
717  SeeAlso []
718 
719 ***********************************************************************/
720 Abc_Ntk_t * Abc_NtkShareXor( Abc_Ntk_t * pNtk, int nMultiSize, int fAnd, int fVerbose )
721 {
722  Abc_Ntk_t * pNtkNew;
723  Abc_ShaMan_t * p;
724  assert( Abc_NtkIsStrash(pNtk) );
725 // Abc_NtkDumpBlif( pNtk );
726  p = Abc_ShaManStart( pNtk );
727  p->nMultiSize = nMultiSize;
728  p->fVerbose = fVerbose;
729  Abc_NtkTraverseSupers( p, fAnd );
730  if ( p->nStartCols < 2 )
731  {
732  Abc_ShaManStop( p );
733  return Abc_NtkDup( pNtk );
734  }
735  if ( fVerbose )
736  Abc_NtkSharePrint( p );
737  Abc_NtkShareOptimize( p, fAnd );
738  if ( fVerbose )
739  Abc_NtkSharePrint( p );
740  pNtkNew = Abc_NtkUpdateNetwork( p, fAnd );
741  Abc_ShaManStop( p );
742  return pNtkNew;
743 }
744 
745 
746 ////////////////////////////////////////////////////////////////////////
747 /// END OF FILE ///
748 ////////////////////////////////////////////////////////////////////////
749 
750 
752 
int iTemp
Definition: abc.h:149
static unsigned Abc_ObjId(Abc_Obj_t *pObj)
Definition: abc.h:329
Vec_Wrd_t * Abc_NtkShareSuperAnd(Abc_Obj_t *pObj, int *pCounter)
Definition: abcExtract.c:159
static Vec_Ptr_t * Vec_PtrStart(int nSize)
Definition: vecPtr.h:106
static int Abc_NtkIsStrash(Abc_Ntk_t *pNtk)
Definition: abc.h:251
static Abc_Obj_t * Abc_ObjFromLit(Abc_Ntk_t *p, int iLit)
Definition: abc.h:390
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
Abc_ShaMan_t * Abc_ShaManStart(Abc_Ntk_t *pNtk)
FUNCTION DEFINITIONS ///.
Definition: abcExtract.c:64
static int Abc_ObjIsCi(Abc_Obj_t *pObj)
Definition: abc.h:351
static int Abc_NtkObjNumMax(Abc_Ntk_t *pNtk)
Definition: abc.h:284
typedefABC_NAMESPACE_HEADER_START struct Vec_Vec_t_ Vec_Vec_t
INCLUDES ///.
Definition: vecVec.h:42
#define SHARE_NUM
DECLARATIONS ///.
Definition: abcExtract.c:29
ABC_DLL Abc_Obj_t * Abc_AigConst1(Abc_Ntk_t *pNtk)
Definition: abcAig.c:683
unsigned fCompl0
Definition: abc.h:140
static Llb_Mgr_t * p
Definition: llb3Image.c:950
void Abc_NtkSharePrint(Abc_ShaMan_t *p)
Definition: abcExtract.c:367
typedefABC_NAMESPACE_IMPL_START struct Vec_Int_t_ Vec_Int_t
DECLARATIONS ///.
Definition: bblif.c:37
#define Vec_PtrForEachEntryReverse(Type, vVec, pEntry, i)
Definition: vecPtr.h:63
void Abc_NtkTraverseSupersXor_rec(Abc_ShaMan_t *p, Abc_Obj_t *pObj, Vec_Ptr_t *vInputs)
Definition: abcExtract.c:237
static void Vec_WrdPush(Vec_Wrd_t *p, word Entry)
Definition: vecWrd.h:618
static int Abc_Var2Lit(int Var, int fCompl)
Definition: abc_global.h:263
static void Vec_WrdPushOrder(Vec_Wrd_t *p, word Entry)
Definition: vecWrd.h:668
Abc_Ntk_t * pNtk
Definition: abcExtract.c:36
ABC_DLL Abc_Ntk_t * Abc_NtkBalanceExor(Abc_Ntk_t *pNtk, int fUpdateLevel, int fVerbose)
Definition: abcDar.c:3935
ABC_DLL Abc_Ntk_t * Abc_NtkDup(Abc_Ntk_t *pNtk)
Definition: abcNtk.c:419
Vec_Int_t vFanins
Definition: abc.h:143
static void Vec_PtrPush(Vec_Ptr_t *p, void *Entry)
Definition: vecPtr.h:606
void Abc_NtkShareOptimize(Abc_ShaMan_t *p, int fAnd)
Definition: abcExtract.c:533
static int Abc_NtkCiNum(Abc_Ntk_t *pNtk)
Definition: abc.h:287
static int Abc_NtkShareUnpackId(word Num)
Definition: abcExtract.c:46
#define ABC_ALLOC(type, num)
Definition: abc_global.h:229
#define Abc_NtkForEachCo(pNtk, pCo, i)
Definition: abc.h:519
static int Abc_MaxInt(int a, int b)
Definition: abc_global.h:238
static Abc_Obj_t * Abc_NtkObj(Abc_Ntk_t *pNtk, int i)
Definition: abc.h:314
static int Vec_PtrSize(Vec_Ptr_t *p)
Definition: vecPtr.h:295
static void Vec_VecFree(Vec_Vec_t *p)
Definition: vecVec.h:347
static int Vec_WrdSize(Vec_Wrd_t *p)
Definition: vecWrd.h:336
#define Vec_WrdForEachEntry(vVec, Entry, i)
MACRO DEFINITIONS ///.
Definition: vecWrd.h:54
static Vec_Int_t * Vec_IntStartFull(int nSize)
Definition: vecInt.h:119
static int Vec_IntTwoCountCommon(Vec_Int_t *vArr1, Vec_Int_t *vArr2)
Definition: vecInt.h:1528
unsigned fCompl1
Definition: abc.h:141
static Abc_Obj_t * Abc_ObjFanin0(Abc_Obj_t *pObj)
Definition: abc.h:373
static int Abc_ObjFaninId0(Abc_Obj_t *pObj)
Definition: abc.h:367
static int Abc_NtkCoNum(Abc_Ntk_t *pNtk)
Definition: abc.h:288
DECLARATIONS ///.
Definition: abcAig.c:52
static void Vec_PtrRemove(Vec_Ptr_t *p, void *Entry)
Definition: vecPtr.h:714
static int Abc_ObjIsCo(Abc_Obj_t *pObj)
Definition: abc.h:352
static Abc_Obj_t * Abc_NtkCo(Abc_Ntk_t *pNtk, int i)
Definition: abc.h:318
ABC_DLL Abc_Obj_t * Abc_AigXor(Abc_Aig_t *pMan, Abc_Obj_t *p0, Abc_Obj_t *p1)
Definition: abcAig.c:735
static Abc_Obj_t * Abc_ObjChild0(Abc_Obj_t *pObj)
Definition: abc.h:383
static void Vec_IntWriteEntry(Vec_Int_t *p, int i, int Entry)
Definition: bblif.c:285
Vec_Wrd_t * Abc_NtkShareSuperXor(Abc_Obj_t *pObj, int *pfCompl, int *pCounter)
Definition: abcExtract.c:95
void * pManFunc
Definition: abc.h:191
static int Abc_ObjIsNode(Abc_Obj_t *pObj)
Definition: abc.h:355
ABC_DLL Abc_Obj_t * Abc_AigAnd(Abc_Aig_t *pMan, Abc_Obj_t *p0, Abc_Obj_t *p1)
Definition: abcAig.c:700
static int Abc_LitIsCompl(int Lit)
Definition: abc_global.h:265
static Vec_Int_t * Vec_IntAlloc(int nCap)
FUNCTION DEFINITIONS ///.
Definition: bblif.c:149
static void Vec_IntTwoSplit(Vec_Int_t *vArr1, Vec_Int_t *vArr2, Vec_Int_t *vArr, Vec_Int_t *vArr1n, Vec_Int_t *vArr2n)
Definition: vecInt.h:1707
static void Vec_WrdClear(Vec_Wrd_t *p)
Definition: vecWrd.h:602
static int Abc_ObjLevel(Abc_Obj_t *pObj)
Definition: abc.h:330
void Abc_NtkTraverseSupers(Abc_ShaMan_t *p, int fAnd)
Definition: abcExtract.c:313
static void Vec_WrdShrink(Vec_Wrd_t *p, int nSizeNew)
Definition: vecWrd.h:585
static void Vec_WrdWriteEntry(Vec_Wrd_t *p, int i, word Entry)
Definition: vecWrd.h:418
static int Vec_IntEntry(Vec_Int_t *p, int i)
Definition: bblif.c:268
unsigned __int64 word
DECLARATIONS ///.
Definition: kitPerm.c:36
#define ABC_NAMESPACE_IMPL_END
Definition: abc_global.h:108
static void Vec_WrdFree(Vec_Wrd_t *p)
Definition: vecWrd.h:260
ABC_DLL Vec_Ptr_t * Abc_NtkNodeSupport(Abc_Ntk_t *pNtk, Abc_Obj_t **ppNodes, int nNodes)
Definition: abcDfs.c:859
void Abc_NtkTraverseSupersAnd_rec(Abc_ShaMan_t *p, Abc_Obj_t *pObj, Vec_Ptr_t *vInputs)
Definition: abcExtract.c:279
static void Vec_IntPush(Vec_Int_t *p, int Entry)
Definition: bblif.c:468
#define Vec_IntForEachEntryStart(vVec, Entry, i, Start)
Definition: vecInt.h:56
static void Vec_IntFreeP(Vec_Int_t **p)
Definition: vecInt.h:289
void Abc_NtkDumpBlif(Abc_Ntk_t *p)
Definition: abcExtract.c:422
static void Vec_PtrWriteEntry(Vec_Ptr_t *p, int i, void *Entry)
Definition: vecPtr.h:396
static Vec_Wrd_t * Vec_WrdAlloc(int nCap)
FUNCTION DEFINITIONS ///.
Definition: vecWrd.h:80
#define ABC_NAMESPACE_IMPL_START
Definition: abc_global.h:107
Abc_Ntk_t * Abc_NtkShareXor(Abc_Ntk_t *pNtk, int nMultiSize, int fAnd, int fVerbose)
Definition: abcExtract.c:720
static void * Vec_PtrEntry(Vec_Ptr_t *p, int i)
Definition: vecPtr.h:362
static word Abc_NtkSharePack(int Lev, int Id)
Definition: abcExtract.c:44
static int Abc_NodeIsTravIdCurrent(Abc_Obj_t *p)
Definition: abc.h:411
static Abc_Obj_t * Abc_ObjRegular(Abc_Obj_t *p)
Definition: abc.h:323
static int Vec_IntSize(Vec_Int_t *p)
Definition: bblif.c:252
static int Abc_NtkShareUnpackLev(word Num)
Definition: abcExtract.c:45
#define Abc_NtkForEachCi(pNtk, pCi, i)
Definition: abc.h:515
static Vec_Ptr_t * Vec_PtrAlloc(int nCap)
FUNCTION DEFINITIONS ///.
Definition: vecPtr.h:83
void Abc_NtkShareFindBestMatch(Vec_Ptr_t *vBuckets, Vec_Int_t **pvInput, Vec_Int_t **pvInput2)
Definition: abcExtract.c:474
#define ABC_FREE(obj)
Definition: abc_global.h:232
static void Abc_NtkIncrementTravId(Abc_Ntk_t *p)
Definition: abc.h:406
ABC_DLL Abc_Ntk_t * Abc_NtkBalance(Abc_Ntk_t *pNtk, int fDuplicate, int fSelective, int fUpdateLevel)
FUNCTION DEFINITIONS ///.
Definition: abcBalance.c:53
static int Vec_WrdRemove(Vec_Wrd_t *p, word Entry)
Definition: vecWrd.h:802
static word Vec_WrdEntry(Vec_Wrd_t *p, int i)
Definition: vecWrd.h:384
static int Abc_Lit2Var(int Lit)
Definition: abc_global.h:264
#define ABC_CALLOC(type, num)
Definition: abc_global.h:230
static int Abc_ObjToLit(Abc_Obj_t *p)
Definition: abc.h:391
static void Vec_PtrFreeP(Vec_Ptr_t **p)
Definition: vecPtr.h:240
Vec_Int_t * vObj2Lit
Definition: abcExtract.c:38
static Abc_Obj_t * Abc_ObjNotCond(Abc_Obj_t *p, int c)
Definition: abc.h:325
#define assert(ex)
Definition: util_old.h:213
#define Vec_WrdForEachEntryStart(vVec, Entry, i, Start)
Definition: vecWrd.h:56
static Abc_Obj_t * Abc_ObjNot(Abc_Obj_t *p)
Definition: abc.h:324
void Abc_ShaManStop(Abc_ShaMan_t *p)
Definition: abcExtract.c:72
static Abc_Ntk_t * Abc_ObjNtk(Abc_Obj_t *pObj)
Definition: abc.h:334
static int Abc_ObjFaninId1(Abc_Obj_t *pObj)
Definition: abc.h:368
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
Abc_Ntk_t * Abc_NtkUpdateNetwork(Abc_ShaMan_t *p, int fAnd)
Definition: abcExtract.c:597
static int Abc_ObjIsComplement(Abc_Obj_t *p)
Definition: abc.h:322
#define Abc_NtkForEachObj(pNtk, pObj, i)
ITERATORS ///.
Definition: abc.h:446
static Abc_Obj_t * Abc_ObjChild1(Abc_Obj_t *pObj)
Definition: abc.h:384
typedefABC_NAMESPACE_HEADER_START struct Vec_Wrd_t_ Vec_Wrd_t
INCLUDES ///.
Definition: vecWrd.h:42
ABC_DLL int Abc_NodeIsExorType(Abc_Obj_t *pNode)
Definition: abcUtil.c:1259
ABC_DLL Abc_Obj_t * Abc_NodeRecognizeMux(Abc_Obj_t *pNode, Abc_Obj_t **ppNodeT, Abc_Obj_t **ppNodeE)
Definition: abcUtil.c:1389
Vec_Ptr_t * vBuckets
Definition: abcExtract.c:37
int nTotal
DECLARATIONS ///.
Definition: cutTruth.c:37
static void Abc_NodeSetTravIdCurrent(Abc_Obj_t *p)
Definition: abc.h:409
static void Vec_PtrFree(Vec_Ptr_t *p)
Definition: vecPtr.h:223
#define Vec_WrdForEachEntryReverse(vVec, pEntry, i)
Definition: vecWrd.h:62