1 /**CFile****************************************************************
2 
3   FileName    [giaMap.c]
4 
5   SystemName  [ABC: Logic synthesis and verification system.]
6 
7   PackageName [Scalable AIG package.]
8 
9   Synopsis    [Manipulation of mapping associated with the AIG.]
10 
11   Author      [Alan Mishchenko]
12 
13   Affiliation [UC Berkeley]
14 
15   Date        [Ver. 1.0. Started - June 20, 2005.]
16 
17   Revision    [$Id: giaMap.c,v 1.00 2005/06/20 00:00:00 alanmi Exp $]
18 
19 ***********************************************************************/
20 
21 #include "gia.h"
22 #include "aig/aig/aig.h"
23 #include "map/if/if.h"
24 #include "bool/kit/kit.h"
25 #include "base/main/main.h"
26 #include "sat/bsat/satSolver.h"
27 
28 #ifdef WIN32
29 #include <windows.h>
30 #endif
31 
32 ABC_NAMESPACE_IMPL_START
33 
34 
35 ////////////////////////////////////////////////////////////////////////
36 ///                        DECLARATIONS                              ///
37 ////////////////////////////////////////////////////////////////////////
38 
39 extern int Kit_TruthToGia( Gia_Man_t * pMan, unsigned * pTruth, int nVars, Vec_Int_t * vMemory, Vec_Int_t * vLeaves, int fHash );
40 extern int Abc_RecToGia3( Gia_Man_t * pMan, If_Man_t * pIfMan, If_Cut_t * pCut, Vec_Int_t * vLeaves, int fHash );
41 
42 ////////////////////////////////////////////////////////////////////////
43 ///                     FUNCTION DEFINITIONS                         ///
44 ////////////////////////////////////////////////////////////////////////
45 
46 /**Function*************************************************************
47 
48   Synopsis    [Load the network into FPGA manager.]
49 
50   Description []
51 
52   SideEffects []
53 
54   SeeAlso     []
55 
56 ***********************************************************************/
Gia_ManSetIfParsDefault(void * pp)57 void Gia_ManSetIfParsDefault( void * pp )
58 {
59     If_Par_t * pPars = (If_Par_t *)pp;
60 //    extern void * Abc_FrameReadLibLut();
61     If_Par_t * p = (If_Par_t *)pPars;
62     // set defaults
63     memset( p, 0, sizeof(If_Par_t) );
64     // user-controlable paramters
65     p->nLutSize    = -1;
66 //    p->nLutSize    =  6;
67     p->nCutsMax    =  8;
68     p->nFlowIters  =  1;
69     p->nAreaIters  =  2;
70     p->DelayTarget = -1;
71     p->Epsilon     =  (float)0.005;
72     p->fPreprocess =  1;
73     p->fArea       =  0;
74     p->fFancy      =  0;
75     p->fExpRed     =  1; ////
76     p->fLatchPaths =  0;
77     p->fEdge       =  1;
78     p->fPower      =  0;
79     p->fCutMin     =  0;
80     p->fVerbose    =  0;
81     p->pLutStruct  =  NULL;
82     // internal parameters
83     p->fTruth      =  0;
84     p->nLatchesCi  =  0;
85     p->nLatchesCo  =  0;
86     p->fLiftLeaves =  0;
87     p->fUseCoAttrs =  1;   // use CO attributes
88     p->pLutLib     =  NULL;
89     p->pTimesArr   =  NULL;
90     p->pTimesReq   =  NULL;
91     p->pFuncCost   =  NULL;
92 }
93 
94 
95 /**Function*************************************************************
96 
97   Synopsis    [Prints mapping statistics.]
98 
99   Description []
100 
101   SideEffects []
102 
103   SeeAlso     []
104 
105 ***********************************************************************/
Gia_ManLutFaninCount(Gia_Man_t * p)106 int Gia_ManLutFaninCount( Gia_Man_t * p )
107 {
108     int i, Counter = 0;
109     Gia_ManForEachLut( p, i )
110         Counter += Gia_ObjLutSize(p, i);
111     return Counter;
112 }
113 
114 /**Function*************************************************************
115 
116   Synopsis    [Prints mapping statistics.]
117 
118   Description []
119 
120   SideEffects []
121 
122   SeeAlso     []
123 
124 ***********************************************************************/
Gia_ManLutSizeMax(Gia_Man_t * p)125 int Gia_ManLutSizeMax( Gia_Man_t * p )
126 {
127     int i, nSizeMax = -1;
128     Gia_ManForEachLut( p, i )
129         nSizeMax = Abc_MaxInt( nSizeMax, Gia_ObjLutSize(p, i) );
130     return nSizeMax;
131 }
132 
133 /**Function*************************************************************
134 
135   Synopsis    [Prints mapping statistics.]
136 
137   Description []
138 
139   SideEffects []
140 
141   SeeAlso     []
142 
143 ***********************************************************************/
Gia_ManLutNum(Gia_Man_t * p)144 int Gia_ManLutNum( Gia_Man_t * p )
145 {
146     int i, Counter = 0;
147     Gia_ManForEachLut( p, i )
148         Counter ++;
149     return Counter;
150 }
151 
152 /**Function*************************************************************
153 
154   Synopsis    [Prints mapping statistics.]
155 
156   Description []
157 
158   SideEffects []
159 
160   SeeAlso     []
161 
162 ***********************************************************************/
Gia_ManLutLevel(Gia_Man_t * p,int ** ppLevels)163 int Gia_ManLutLevel( Gia_Man_t * p, int ** ppLevels )
164 {
165     Gia_Obj_t * pObj;
166     int i, k, iFan, Level;
167     int * pLevels = ABC_CALLOC( int, Gia_ManObjNum(p) );
168     Gia_ManForEachLut( p, i )
169     {
170         Level = 0;
171         Gia_LutForEachFanin( p, i, iFan, k )
172             if ( Level < pLevels[iFan] )
173                 Level = pLevels[iFan];
174         pLevels[i] = Level + 1;
175     }
176     Level = 0;
177     Gia_ManForEachCo( p, pObj, k )
178     {
179         int LevelFan = pLevels[Gia_ObjFaninId0p(p, pObj)];
180         Level = Abc_MaxInt( Level, LevelFan );
181         pLevels[Gia_ObjId(p, pObj)] = LevelFan;
182     }
183     if ( ppLevels )
184         *ppLevels = pLevels;
185     else
186         ABC_FREE( pLevels );
187     return Level;
188 }
189 
190 /**Function*************************************************************
191 
192   Synopsis    [Prints mapping statistics.]
193 
194   Description []
195 
196   SideEffects []
197 
198   SeeAlso     []
199 
200 ***********************************************************************/
Gia_ManLutParams(Gia_Man_t * p,int * pnCurLuts,int * pnCurEdges,int * pnCurLevels)201 void Gia_ManLutParams( Gia_Man_t * p, int * pnCurLuts, int * pnCurEdges, int * pnCurLevels )
202 {
203     if ( p->pManTime && Tim_ManBoxNum((Tim_Man_t *)p->pManTime) )
204     {
205         int i;
206         *pnCurLuts = 0;
207         *pnCurEdges = 0;
208         Gia_ManForEachLut( p, i )
209         {
210             (*pnCurLuts)++;
211             (*pnCurEdges) += Gia_ObjLutSize(p, i);
212         }
213         *pnCurLevels = Gia_ManLutLevelWithBoxes( p );
214     }
215     else
216     {
217         Gia_Obj_t * pObj;
218         int i, k, iFan;
219         int * pLevels = ABC_CALLOC( int, Gia_ManObjNum(p) );
220         *pnCurLuts = 0;
221         *pnCurEdges = 0;
222         Gia_ManForEachLut( p, i )
223         {
224             int Level = 0;
225             (*pnCurLuts)++;
226             (*pnCurEdges) += Gia_ObjLutSize(p, i);
227             Gia_LutForEachFanin( p, i, iFan, k )
228                 if ( Level < pLevels[iFan] )
229                     Level = pLevels[iFan];
230             pLevels[i] = Level + 1;
231         }
232         *pnCurLevels = 0;
233         Gia_ManForEachCo( p, pObj, k )
234             if ( *pnCurLevels < pLevels[Gia_ObjFaninId0p(p, pObj)] )
235                 *pnCurLevels = pLevels[Gia_ObjFaninId0p(p, pObj)];
236         ABC_FREE( pLevels );
237     }
238 }
239 
240 /**Function*************************************************************
241 
242   Synopsis    [Assigns levels.]
243 
244   Description []
245 
246   SideEffects []
247 
248   SeeAlso     []
249 
250 ***********************************************************************/
Gia_ManSetRefsMapped(Gia_Man_t * p)251 void Gia_ManSetRefsMapped( Gia_Man_t * p )
252 {
253     Gia_Obj_t * pObj;
254     int i, k, iFan;
255     ABC_FREE( p->pRefs );
256     p->pRefs = ABC_CALLOC( int, Gia_ManObjNum(p) );
257     Gia_ManForEachCo( p, pObj, i )
258         Gia_ObjRefIncId( p, Gia_ObjFaninId0p(p, pObj) );
259     Gia_ManForEachLut( p, i )
260         Gia_LutForEachFanin( p, i, iFan, k )
261             Gia_ObjRefIncId( p, iFan );
262 }
263 
264 /**Function*************************************************************
265 
266   Synopsis    [Assigns levels.]
267 
268   Description []
269 
270   SideEffects []
271 
272   SeeAlso     []
273 
274 ***********************************************************************/
Gia_ManSetLutRefs(Gia_Man_t * p)275 void Gia_ManSetLutRefs( Gia_Man_t * p )
276 {
277     Gia_Obj_t * pObj;
278     int i, k, iFan;
279     ABC_FREE( p->pLutRefs );
280     p->pLutRefs = ABC_CALLOC( int, Gia_ManObjNum(p) );
281     Gia_ManForEachCo( p, pObj, i )
282         Gia_ObjLutRefIncId( p, Gia_ObjFaninId0p(p, pObj) );
283     Gia_ManForEachLut( p, i )
284         Gia_LutForEachFanin( p, i, iFan, k )
285             Gia_ObjLutRefIncId( p, iFan );
286 }
287 
288 /**Function*************************************************************
289 
290   Synopsis    [Calculate mapping overlap.]
291 
292   Description []
293 
294   SideEffects []
295 
296   SeeAlso     []
297 
298 ***********************************************************************/
Gia_ManComputeOverlap2One_rec(Gia_Man_t * p,int iObj,Vec_Str_t * vLabel,Vec_Int_t * vVisit)299 int Gia_ManComputeOverlap2One_rec( Gia_Man_t * p, int iObj, Vec_Str_t * vLabel, Vec_Int_t * vVisit )
300 {
301     Gia_Obj_t * pObj;
302     int Counter;
303     if ( Vec_StrEntry(vLabel, iObj) )
304         return 0;
305     Vec_StrWriteEntry( vLabel, iObj, 1 );
306     pObj = Gia_ManObj( p, iObj );
307     assert( Gia_ObjIsAnd(pObj) );
308     Counter  = Gia_ManComputeOverlap2One_rec( p, Gia_ObjFaninId0(pObj, iObj), vLabel, vVisit );
309     Counter += Gia_ManComputeOverlap2One_rec( p, Gia_ObjFaninId1(pObj, iObj), vLabel, vVisit );
310     Vec_IntPush( vVisit, iObj );
311     return Counter + 1;
312 }
Gia_ManComputeOverlap2One(Gia_Man_t * p,int iObj,Vec_Str_t * vLabel,Vec_Int_t * vVisit)313 int Gia_ManComputeOverlap2One( Gia_Man_t * p, int iObj, Vec_Str_t * vLabel, Vec_Int_t * vVisit )
314 {
315     int iFan, k, Counter;
316     Vec_IntClear( vVisit );
317     Gia_LutForEachFanin( p, iObj, iFan, k )
318         Vec_StrWriteEntry( vLabel, iFan, 1 );
319     Counter = Gia_ManComputeOverlap2One_rec( p, iObj, vLabel, vVisit );
320     Gia_LutForEachFanin( p, iObj, iFan, k )
321         Vec_StrWriteEntry( vLabel, iFan, 0 );
322     Vec_IntForEachEntry( vVisit, iFan, k )
323         Vec_StrWriteEntry( vLabel, iFan, 0 );
324     return Counter;
325 }
Gia_ManComputeOverlap2(Gia_Man_t * p)326 int Gia_ManComputeOverlap2( Gia_Man_t * p )
327 {
328     Vec_Int_t * vVisit;
329     Vec_Str_t * vLabel;
330     int i, Count = -Gia_ManAndNum(p);
331     assert( Gia_ManHasMapping(p) );
332     vVisit = Vec_IntAlloc( 100 );
333     vLabel = Vec_StrStart( Gia_ManObjNum(p) );
334     Gia_ManForEachLut( p, i )
335         Count += Gia_ManComputeOverlap2One( p, i, vLabel, vVisit );
336     Vec_StrFree( vLabel );
337     Vec_IntFree( vVisit );
338     return Count;
339 }
340 
341 /**Function*************************************************************
342 
343   Synopsis    [Calculate mapping overlap.]
344 
345   Description []
346 
347   SideEffects []
348 
349   SeeAlso     []
350 
351 ***********************************************************************/
Gia_ManComputeOverlapOne_rec(Gia_Man_t * p,int iObj)352 int Gia_ManComputeOverlapOne_rec( Gia_Man_t * p, int iObj )
353 {
354     Gia_Obj_t * pObj;
355     if ( Gia_ObjIsTravIdCurrentId(p, iObj) )
356         return 0;
357     Gia_ObjSetTravIdCurrentId( p, iObj );
358     pObj = Gia_ManObj( p, iObj );
359     assert( Gia_ObjIsAnd(pObj) );
360     return 1 + Gia_ManComputeOverlapOne_rec( p, Gia_ObjFaninId0(pObj, iObj) )
361           + Gia_ManComputeOverlapOne_rec( p, Gia_ObjFaninId1(pObj, iObj) );
362 }
Gia_ManComputeOverlapOne(Gia_Man_t * p,int iObj)363 int Gia_ManComputeOverlapOne( Gia_Man_t * p, int iObj )
364 {
365     int iFan, k;
366     Gia_ManIncrementTravId(p);
367     Gia_LutForEachFanin( p, iObj, iFan, k )
368         Gia_ObjSetTravIdCurrentId( p, iFan );
369     return Gia_ManComputeOverlapOne_rec( p, iObj );
370 }
Gia_ManComputeOverlap(Gia_Man_t * p)371 int Gia_ManComputeOverlap( Gia_Man_t * p )
372 {
373     int i, Count = -Gia_ManAndNum(p);
374     assert( Gia_ManHasMapping(p) );
375     Gia_ManForEachLut( p, i )
376         Count += Gia_ManComputeOverlapOne( p, i );
377     return Count;
378 }
379 
380 /**Function*************************************************************
381 
382   Synopsis    [Prints mapping statistics.]
383 
384   Description []
385 
386   SideEffects []
387 
388   SeeAlso     []
389 
390 ***********************************************************************/
Gia_ManPrintGetMuxFanins(Gia_Man_t * p,Gia_Obj_t * pObj,int * pFanins)391 void Gia_ManPrintGetMuxFanins( Gia_Man_t * p, Gia_Obj_t * pObj, int * pFanins )
392 {
393     Gia_Obj_t * pData0, * pData1;
394     Gia_Obj_t * pCtrl = Gia_ObjRecognizeMux( pObj, &pData1, &pData0 );
395     pFanins[0] = Gia_ObjId(p, Gia_Regular(pCtrl));
396     pFanins[1] = Gia_ObjId(p, Gia_Regular(pData1));
397     pFanins[2] = Gia_ObjId(p, Gia_Regular(pData0));
398 }
Gia_ManCountDupLut(Gia_Man_t * p)399 int Gia_ManCountDupLut( Gia_Man_t * p )
400 {
401     Gia_Obj_t * pObj, * pFanin;
402     int i, pFanins[3], nCountDup = 0, nCountPis = 0, nCountMux = 0;
403     Gia_ManCleanMark01( p );
404     Gia_ManForEachLut( p, i )
405         if ( Gia_ObjLutIsMux(p, i) )
406         {
407             pObj = Gia_ManObj( p, i );
408             pObj->fMark1 = 1;
409             if ( Gia_ObjLutSize(p, i) == 3 )
410             {
411                 Gia_ManPrintGetMuxFanins( p, pObj, pFanins );
412 
413                 pFanin = Gia_ManObj(p, pFanins[1]);
414                 nCountPis += Gia_ObjIsCi(pFanin);
415                 nCountDup += pFanin->fMark0;
416                 nCountMux += pFanin->fMark1;
417                 pFanin->fMark0 = 1;
418 
419                 pFanin = Gia_ManObj(p, pFanins[2]);
420                 nCountPis += Gia_ObjIsCi(pFanin);
421                 nCountDup += pFanin->fMark0;
422                 nCountMux += pFanin->fMark1;
423                 pFanin->fMark0 = 1;
424             }
425             else if ( Gia_ObjLutSize(p, i) == 2 )
426             {
427                 pFanin = Gia_ObjFanin0(pObj);
428                 if ( pFanin->fMark0 || pFanin->fMark1 )
429                 {
430                     pFanin = Gia_ObjFanin1(pObj);
431                     nCountPis += Gia_ObjIsCi(pFanin);
432                     nCountDup += pFanin->fMark0;
433                     nCountMux += pFanin->fMark1;
434                     pFanin->fMark0 = 1;
435                 }
436                 else
437                 {
438                     nCountPis += Gia_ObjIsCi(pFanin);
439                     nCountDup += pFanin->fMark0;
440                     nCountMux += pFanin->fMark1;
441                     pFanin->fMark0 = 1;
442                 }
443             }
444             else assert( 0 );
445         }
446     Gia_ManCleanMark01( p );
447     if ( nCountDup + nCountPis + nCountMux )
448         printf( "Dup fanins = %d.  CI fanins = %d.  MUX fanins = %d.  Total = %d.  (%.2f %%)\n",
449             nCountDup, nCountPis, nCountMux, nCountDup + nCountPis, 100.0 * (nCountDup + nCountPis + nCountMux) / Gia_ManLutNum(p) );
450     return nCountDup + nCountPis;
451 }
452 
Gia_ManPrintMappingStats(Gia_Man_t * p,char * pDumpFile)453 void Gia_ManPrintMappingStats( Gia_Man_t * p, char * pDumpFile )
454 {
455     Gia_Obj_t * pObj;
456     int * pLevels;
457     int i, k, iFan, nLutSize = 0, nLuts = 0, nFanins = 0, LevelMax = 0, Ave = 0, nMuxF = 0;
458     if ( !Gia_ManHasMapping(p) )
459         return;
460     pLevels = ABC_CALLOC( int, Gia_ManObjNum(p) );
461     Gia_ManForEachLut( p, i )
462     {
463         if ( Gia_ObjLutIsMux(p, i) )
464         {
465             int pFanins[3];
466             if ( Gia_ObjLutSize(p, i) == 3 )
467             {
468                 Gia_ManPrintGetMuxFanins( p, Gia_ManObj(p, i), pFanins );
469                 pLevels[i] = Abc_MaxInt( pLevels[i], pLevels[pFanins[0]]+1 );
470                 pLevels[i] = Abc_MaxInt( pLevels[i], pLevels[pFanins[1]] );
471                 pLevels[i] = Abc_MaxInt( pLevels[i], pLevels[pFanins[2]] );
472             }
473             else if ( Gia_ObjLutSize(p, i) == 2 )
474             {
475                 pObj = Gia_ManObj( p, i );
476                 pLevels[i] = Abc_MaxInt( pLevels[i], pLevels[Gia_ObjFaninId0(pObj, i)] );
477                 pLevels[i] = Abc_MaxInt( pLevels[i], pLevels[Gia_ObjFaninId1(pObj, i)] );
478             }
479             LevelMax = Abc_MaxInt( LevelMax, pLevels[i] );
480             nFanins++;
481             nMuxF++;
482             continue;
483         }
484         nLuts++;
485         nFanins += Gia_ObjLutSize(p, i);
486         nLutSize = Abc_MaxInt( nLutSize, Gia_ObjLutSize(p, i) );
487         Gia_LutForEachFanin( p, i, iFan, k )
488             pLevels[i] = Abc_MaxInt( pLevels[i], pLevels[iFan] );
489         pLevels[i]++;
490         LevelMax = Abc_MaxInt( LevelMax, pLevels[i] );
491     }
492     Gia_ManForEachCo( p, pObj, i )
493         Ave += pLevels[Gia_ObjFaninId0p(p, pObj)];
494     ABC_FREE( pLevels );
495 
496 #ifdef WIN32
497     {
498     HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
499     Abc_Print( 1, "Mapping (K=%d)  :  ", nLutSize );
500     SetConsoleTextAttribute( hConsole, 14 ); // yellow
501     Abc_Print( 1, "lut =%7d  ",  nLuts );
502     if ( nMuxF )
503     Abc_Print( 1, "muxF =%7d  ",  nMuxF );
504     SetConsoleTextAttribute( hConsole, 10 ); // green
505     Abc_Print( 1, "edge =%8d  ", nFanins );
506     SetConsoleTextAttribute( hConsole, 12 ); // red
507     Abc_Print( 1, "lev =%5d ",   LevelMax );
508     Abc_Print( 1, "(%.2f)  ",    (float)Ave / Gia_ManCoNum(p) );
509 //    Abc_Print( 1, "over =%5.1f %%  ", 100.0 * Gia_ManComputeOverlap(p) / Gia_ManAndNum(p) );
510     if ( p->pManTime && Tim_ManBoxNum((Tim_Man_t *)p->pManTime) )
511         Abc_Print( 1, "levB =%5d  ", Gia_ManLutLevelWithBoxes(p) );
512     SetConsoleTextAttribute( hConsole, 7 );  // normal
513     Abc_Print( 1, "mem =%5.2f MB", 4.0*(Gia_ManObjNum(p) + 2*nLuts + nFanins)/(1<<20) );
514     Abc_Print( 1, "\n" );
515     }
516 #else
517     Abc_Print( 1, "Mapping (K=%d)  :  ", nLutSize );
518     Abc_Print( 1, "%slut =%7d%s  ",  "\033[1;33m", nLuts,    "\033[0m" );  // yellow
519     Abc_Print( 1, "%sedge =%8d%s  ", "\033[1;32m", nFanins,  "\033[0m" );  // green
520     Abc_Print( 1, "%slev =%5d%s ",   "\033[1;31m", LevelMax, "\033[0m" );  // red
521     Abc_Print( 1, "%s(%.2f)%s  ",    "\033[1;31m", (float)Ave / Gia_ManCoNum(p), "\033[0m" );
522 //    Abc_Print( 1, "over =%5.1f %%  ", 100.0 * Gia_ManComputeOverlap(p) / Gia_ManAndNum(p) );
523     if ( p->pManTime && Tim_ManBoxNum((Tim_Man_t *)p->pManTime) )
524         Abc_Print( 1, "%slevB =%5d%s  ", "\033[1;31m", Gia_ManLutLevelWithBoxes(p), "\033[0m" );
525     Abc_Print( 1, "mem =%5.2f MB", 4.0*(Gia_ManObjNum(p) + 2*nLuts + nFanins)/(1<<20) );
526     Abc_Print( 1, "\n" );
527 #endif
528 
529     if ( nMuxF )
530         Gia_ManCountDupLut( p );
531 
532     //return;
533     if ( pDumpFile )
534     {
535         static char FileNameOld[1000] = {0};
536         static abctime clk = 0;
537         FILE * pTable = fopen( pDumpFile, "a+" );
538         if ( strcmp( FileNameOld, p->pName ) )
539         {
540             sprintf( FileNameOld, "%s", p->pName );
541             fprintf( pTable, "\n" );
542             fprintf( pTable, "%s ", p->pName );
543             fprintf( pTable, " " );
544             fprintf( pTable, "%d ", Gia_ManAndNum(p) );
545             fprintf( pTable, "%d ", nLuts           );
546             fprintf( pTable, "%d ", Gia_ManLutLevelWithBoxes(p) );
547             //fprintf( pTable, "%d ", Gia_ManRegBoxNum(p) );
548             //fprintf( pTable, "%d ", Gia_ManNonRegBoxNum(p) );
549             //fprintf( pTable, "%.2f", 1.0*(Abc_Clock() - clk)/CLOCKS_PER_SEC );
550             clk = Abc_Clock();
551         }
552         else
553         {
554             //printf( "This part of the code is currently not used.\n" );
555             //assert( 0 );
556             fprintf( pTable, " " );
557             fprintf( pTable, "%d ", nLuts           );
558             fprintf( pTable, "%d ", Gia_ManLutLevelWithBoxes(p) );
559             //fprintf( pTable, "%d ", Gia_ManRegBoxNum(p) );
560             //fprintf( pTable, "%d ", Gia_ManNonRegBoxNum(p) );
561             fprintf( pTable, "%.2f", 1.0*(Abc_Clock() - clk)/CLOCKS_PER_SEC );
562             clk = Abc_Clock();
563         }
564         fclose( pTable );
565     }
566 
567 }
568 
569 /**Function*************************************************************
570 
571   Synopsis    [Prints mapping statistics.]
572 
573   Description []
574 
575   SideEffects []
576 
577   SeeAlso     []
578 
579 ***********************************************************************/
Gia_ManPrintPackingStats(Gia_Man_t * p)580 void Gia_ManPrintPackingStats( Gia_Man_t * p )
581 {
582     int fVerbose = 0;
583     int nObjToShow = 200;
584     int nNumStr[5] = {0};
585     int i, k, Entry, nEntries, nEntries2, MaxSize = -1, Count = 0;
586     if ( p->vPacking == NULL )
587         return;
588     nEntries = Vec_IntEntry( p->vPacking, 0 );
589     nEntries2 = 0;
590     Vec_IntForEachEntryStart( p->vPacking, Entry, i, 1 )
591     {
592         assert( Entry > 0 && Entry < 4 );
593         nNumStr[Entry]++;
594         i++;
595         if ( fVerbose && nEntries2 < nObjToShow ) Abc_Print( 1, "{ " );
596         for ( k = 0; k < Entry; k++, i++ )
597             if ( fVerbose && nEntries2 < nObjToShow ) Abc_Print( 1, "%d ", Vec_IntEntry(p->vPacking, i) );
598         if ( fVerbose && nEntries2 < nObjToShow ) Abc_Print( 1, "}\n" );
599         i--;
600         nEntries2++;
601     }
602     assert( nEntries == nEntries2 );
603     if ( nNumStr[3] > 0 )
604         MaxSize = 3;
605     else if ( nNumStr[2] > 0 )
606         MaxSize = 2;
607     else if ( nNumStr[1] > 0 )
608         MaxSize = 1;
609     Abc_Print( 1, "Packing (N=%d)  :  ", MaxSize );
610     for ( i = 1; i <= MaxSize; i++ )
611     {
612         Abc_Print( 1, "%d x LUT = %d   ", i, nNumStr[i] );
613         Count += i * nNumStr[i];
614     }
615     Abc_Print( 1, "Total = %d   ", nEntries2 );
616     Abc_Print( 1, "Total LUT = %d", Count );
617     Abc_Print( 1, "\n" );
618 }
619 
620 /**Function*************************************************************
621 
622   Synopsis    []
623 
624   Description []
625 
626   SideEffects []
627 
628   SeeAlso     []
629 
630 ***********************************************************************/
Gia_ManPrintNodeProfile(int * pCounts,int nSizeMax)631 void Gia_ManPrintNodeProfile( int * pCounts, int nSizeMax )
632 {
633     int i, SizeAll = 0, NodeAll = 0;
634     for ( i = 0; i <= nSizeMax; i++ )
635     {
636         SizeAll += i * pCounts[i];
637         NodeAll += pCounts[i];
638     }
639     Abc_Print( 1, "LUT = %d : ", NodeAll );
640     for ( i = 2; i <= nSizeMax; i++ )
641         Abc_Print( 1, "%d=%d %.1f %%  ", i, pCounts[i], 100.0*pCounts[i]/NodeAll );
642     Abc_Print( 1, "Ave = %.2f\n", 1.0*SizeAll/(NodeAll ? NodeAll : 1) );
643 }
Gia_ManPrintLutStats(Gia_Man_t * p)644 void Gia_ManPrintLutStats( Gia_Man_t * p )
645 {
646     int i, nSizeMax, pCounts[33] = {0};
647     nSizeMax = Gia_ManLutSizeMax( p );
648     if ( nSizeMax > 32 )
649     {
650         Abc_Print( 1, "The max LUT size (%d) is too large.\n", nSizeMax );
651         return;
652     }
653     Gia_ManForEachLut( p, i )
654         pCounts[ Gia_ObjLutSize(p, i) ]++;
655     Gia_ManPrintNodeProfile( pCounts, nSizeMax );
656 }
657 
658 /**Function*************************************************************
659 
660   Synopsis    [Computes levels for AIG with choices and white boxes.]
661 
662   Description []
663 
664   SideEffects []
665 
666   SeeAlso     []
667 
668 ***********************************************************************/
Gia_ManChoiceLevel_rec(Gia_Man_t * p,Gia_Obj_t * pObj)669 void Gia_ManChoiceLevel_rec( Gia_Man_t * p, Gia_Obj_t * pObj )
670 {
671     Tim_Man_t * pManTime = (Tim_Man_t *)p->pManTime;
672     Gia_Obj_t * pNext;
673     int i, iBox, iTerm1, nTerms, LevelMax = 0;
674     if ( Gia_ObjIsTravIdCurrent( p, pObj ) )
675         return;
676     Gia_ObjSetTravIdCurrent( p, pObj );
677     if ( Gia_ObjIsCi(pObj) )
678     {
679         if ( pManTime )
680         {
681             iBox = Tim_ManBoxForCi( pManTime, Gia_ObjCioId(pObj) );
682             if ( iBox >= 0 ) // this is not a true PI
683             {
684                 iTerm1 = Tim_ManBoxInputFirst( pManTime, iBox );
685                 nTerms = Tim_ManBoxInputNum( pManTime, iBox );
686                 for ( i = 0; i < nTerms; i++ )
687                 {
688                     pNext = Gia_ManCo( p, iTerm1 + i );
689                     Gia_ManChoiceLevel_rec( p, pNext );
690                     if ( LevelMax < Gia_ObjLevel(p, pNext) )
691                         LevelMax = Gia_ObjLevel(p, pNext);
692                 }
693                 LevelMax++;
694             }
695         }
696 //        Abc_Print( 1, "%d ", pObj->Level );
697     }
698     else if ( Gia_ObjIsCo(pObj) )
699     {
700         pNext = Gia_ObjFanin0(pObj);
701         Gia_ManChoiceLevel_rec( p, pNext );
702         if ( LevelMax < Gia_ObjLevel(p, pNext) )
703             LevelMax = Gia_ObjLevel(p, pNext);
704     }
705     else if ( Gia_ObjIsAnd(pObj) )
706     {
707         // get the maximum level of the two fanins
708         pNext = Gia_ObjFanin0(pObj);
709         Gia_ManChoiceLevel_rec( p, pNext );
710         if ( LevelMax < Gia_ObjLevel(p, pNext) )
711             LevelMax = Gia_ObjLevel(p, pNext);
712         pNext = Gia_ObjFanin1(pObj);
713         Gia_ManChoiceLevel_rec( p, pNext );
714         if ( LevelMax < Gia_ObjLevel(p, pNext) )
715             LevelMax = Gia_ObjLevel(p, pNext);
716         LevelMax++;
717 
718         // get the level of the nodes in the choice node
719         if ( (pNext = Gia_ObjSiblObj(p, Gia_ObjId(p, pObj))) )
720         {
721             Gia_ManChoiceLevel_rec( p, pNext );
722             if ( LevelMax < Gia_ObjLevel(p, pNext) )
723                 LevelMax = Gia_ObjLevel(p, pNext);
724         }
725     }
726     else if ( !Gia_ObjIsConst0(pObj) )
727         assert( 0 );
728     Gia_ObjSetLevel( p, pObj, LevelMax );
729 }
Gia_ManChoiceLevel(Gia_Man_t * p)730 int Gia_ManChoiceLevel( Gia_Man_t * p )
731 {
732     Gia_Obj_t * pObj;
733     int i, LevelMax = 0;
734 //    assert( Gia_ManRegNum(p) == 0 );
735     Gia_ManCleanLevels( p, Gia_ManObjNum(p) );
736     Gia_ManIncrementTravId( p );
737     Gia_ManForEachCo( p, pObj, i )
738     {
739         Gia_ManChoiceLevel_rec( p, pObj );
740         if ( LevelMax < Gia_ObjLevel(p, pObj) )
741             LevelMax = Gia_ObjLevel(p, pObj);
742     }
743     // account for dangling boxes
744     Gia_ManForEachCi( p, pObj, i )
745     {
746         Gia_ManChoiceLevel_rec( p, pObj );
747         if ( LevelMax < Gia_ObjLevel(p, pObj) )
748             LevelMax = Gia_ObjLevel(p, pObj);
749 //        Abc_Print( 1, "%d ", Gia_ObjLevel(p, pObj) );
750     }
751 //    Abc_Print( 1, "\n" );
752     Gia_ManForEachAnd( p, pObj, i )
753         assert( Gia_ObjLevel(p, pObj) > 0 );
754 //    printf( "Max level %d\n", LevelMax );
755     return LevelMax;
756 }
757 
758 
759 
760 /**Function*************************************************************
761 
762   Synopsis    [Converts GIA into IF manager.]
763 
764   Description []
765 
766   SideEffects []
767 
768   SeeAlso     []
769 
770 ***********************************************************************/
If_ManFanin0Copy(If_Man_t * pIfMan,Gia_Obj_t * pObj)771 static inline If_Obj_t * If_ManFanin0Copy( If_Man_t * pIfMan, Gia_Obj_t * pObj ) { return If_NotCond( If_ManObj(pIfMan, Gia_ObjValue(Gia_ObjFanin0(pObj))), Gia_ObjFaninC0(pObj) ); }
If_ManFanin1Copy(If_Man_t * pIfMan,Gia_Obj_t * pObj)772 static inline If_Obj_t * If_ManFanin1Copy( If_Man_t * pIfMan, Gia_Obj_t * pObj ) { return If_NotCond( If_ManObj(pIfMan, Gia_ObjValue(Gia_ObjFanin1(pObj))), Gia_ObjFaninC1(pObj) ); }
Gia_ManToIf(Gia_Man_t * p,If_Par_t * pPars)773 If_Man_t * Gia_ManToIf( Gia_Man_t * p, If_Par_t * pPars )
774 {
775     If_Man_t * pIfMan;
776     If_Obj_t * pIfObj = NULL;
777     Gia_Obj_t * pObj;
778     int i;
779     // create levels with choices
780     Gia_ManChoiceLevel( p );
781     // mark representative nodes
782     if ( Gia_ManHasChoices(p) )
783         Gia_ManMarkFanoutDrivers( p );
784     // start the mapping manager and set its parameters
785     pIfMan = If_ManStart( pPars );
786     pIfMan->pName = Abc_UtilStrsav( Gia_ManName(p) );
787     // print warning about excessive memory usage
788     if ( 1.0 * Gia_ManObjNum(p) * pIfMan->nObjBytes / (1<<30) > 1.0 )
789         printf( "Warning: The mapper will allocate %.1f GB for to represent the subject graph with %d AIG nodes.\n",
790             1.0 * Gia_ManObjNum(p) * pIfMan->nObjBytes / (1<<30), Gia_ManObjNum(p) );
791     // load the AIG into the mapper
792     Gia_ManFillValue( p );
793     Gia_ManConst0(p)->Value = If_ObjId( If_ManConst1(pIfMan) );
794     Gia_ManForEachObj1( p, pObj, i )
795     {
796         if ( Gia_ObjIsAnd(pObj) )
797             pIfObj = If_ManCreateAnd( pIfMan, If_ManFanin0Copy(pIfMan, pObj), If_ManFanin1Copy(pIfMan, pObj) );
798         else if ( Gia_ObjIsCi(pObj) )
799         {
800             pIfObj = If_ManCreateCi( pIfMan );
801             If_ObjSetLevel( pIfObj, Gia_ObjLevel(p, pObj) );
802 //            Abc_Print( 1, "pi%d=%d\n ", If_ObjId(pIfObj), If_ObjLevel(pIfObj) );
803             if ( pIfMan->nLevelMax < (int)pIfObj->Level )
804                 pIfMan->nLevelMax = (int)pIfObj->Level;
805         }
806         else if ( Gia_ObjIsCo(pObj) )
807         {
808             pIfObj = If_ManCreateCo( pIfMan, If_NotCond( If_ManFanin0Copy(pIfMan, pObj), Gia_ObjIsConst0(Gia_ObjFanin0(pObj))) );
809 //            Abc_Print( 1, "po%d=%d\n ", If_ObjId(pIfObj), If_ObjLevel(pIfObj) );
810         }
811         else assert( 0 );
812         assert( i == If_ObjId(pIfObj) );
813         Gia_ObjSetValue( pObj, If_ObjId(pIfObj) );
814         // set up the choice node
815         if ( Gia_ObjSibl(p, i) && pObj->fMark0 )
816         {
817             Gia_Obj_t * pSibl, * pPrev;
818             for ( pPrev = pObj, pSibl = Gia_ObjSiblObj(p, i); pSibl; pPrev = pSibl, pSibl = Gia_ObjSiblObj(p, Gia_ObjId(p, pSibl)) )
819                 If_ObjSetChoice( If_ManObj(pIfMan, Gia_ObjValue(pPrev)), If_ManObj(pIfMan, Gia_ObjValue(pSibl)) );
820             If_ManCreateChoice( pIfMan, If_ManObj(pIfMan, Gia_ObjValue(pObj)) );
821             pPars->fExpRed = 0;
822         }
823 //        assert( If_ObjLevel(pIfObj) == Gia_ObjLevel(pNode) );
824     }
825     if ( Gia_ManHasChoices(p) )
826         Gia_ManCleanMark0( p );
827     return pIfMan;
828 }
829 
830 /**Function*************************************************************
831 
832   Synopsis    [Rebuilds GIA from mini AIG.]
833 
834   Description []
835 
836   SideEffects []
837 
838   SeeAlso     []
839 
840 ***********************************************************************/
Gia_ManBuildFromMiniInt(Gia_Man_t * pNew,Vec_Int_t * vLeaves,Vec_Int_t * vAig,int fHash)841 int Gia_ManBuildFromMiniInt( Gia_Man_t * pNew, Vec_Int_t * vLeaves, Vec_Int_t * vAig, int fHash )
842 {
843     assert( Vec_IntSize(vAig) > 0 );
844     assert( Vec_IntEntryLast(vAig) < 2 );
845     if ( Vec_IntSize(vAig) == 1 ) // const
846         return Vec_IntEntry(vAig, 0);
847     if ( Vec_IntSize(vAig) == 2 ) // variable
848     {
849         assert( Vec_IntEntry(vAig, 0) == 0 );
850         assert( Vec_IntSize(vLeaves) == 1 );
851         return Abc_LitNotCond( Vec_IntEntry(vLeaves, 0), Vec_IntEntry(vAig, 1) );
852     }
853     else
854     {
855         int nLeaves = Vec_IntSize(vLeaves);
856         int i, iVar0, iVar1, iLit0, iLit1, iLit = 0;
857         assert( Vec_IntSize(vAig) & 1 );
858         Vec_IntForEachEntryDouble( vAig, iLit0, iLit1, i )
859         {
860             iVar0 = Abc_Lit2Var( iLit0 );
861             iVar1 = Abc_Lit2Var( iLit1 );
862             iLit0 = Abc_LitNotCond( iVar0 < nLeaves ? Vec_IntEntry(vLeaves, iVar0) : Vec_IntEntry(vAig, iVar0 - nLeaves), Abc_LitIsCompl(iLit0) );
863             iLit1 = Abc_LitNotCond( iVar1 < nLeaves ? Vec_IntEntry(vLeaves, iVar1) : Vec_IntEntry(vAig, iVar1 - nLeaves), Abc_LitIsCompl(iLit1) );
864             if ( fHash )
865                 iLit = Gia_ManHashAnd( pNew, iLit0, iLit1 );
866             else if ( iLit0 == iLit1 )
867                 iLit = iLit0;
868             else
869                 iLit = Gia_ManAppendAnd( pNew, iLit0, iLit1 );
870             assert( (i & 1) == 0 );
871             Vec_IntWriteEntry( vAig, Abc_Lit2Var(i), iLit );  // overwriting entries
872         }
873         assert( i == Vec_IntSize(vAig) - 1 );
874         iLit = Abc_LitNotCond( iLit, Vec_IntEntry(vAig, i) );
875         Vec_IntClear( vAig ); // useless
876         return iLit;
877     }
878 }
Gia_ManBuildFromMini(Gia_Man_t * pNew,If_Man_t * pIfMan,If_Cut_t * pCut,Vec_Int_t * vLeaves,Vec_Int_t * vAig,int fHash,int fUseDsd)879 int Gia_ManBuildFromMini( Gia_Man_t * pNew, If_Man_t * pIfMan, If_Cut_t * pCut, Vec_Int_t * vLeaves, Vec_Int_t * vAig, int fHash, int fUseDsd )
880 {
881     if ( fUseDsd )
882         If_CutDsdBalanceEval( pIfMan, pCut, vAig );
883     else
884         If_CutSopBalanceEval( pIfMan, pCut, vAig );
885     return Gia_ManBuildFromMiniInt( pNew, vLeaves, vAig, fHash );
886 }
887 
888 /**Function*************************************************************
889 
890   Synopsis    [Converts IF into GIA manager.]
891 
892   Description []
893 
894   SideEffects []
895 
896   SeeAlso     []
897 
898 ***********************************************************************/
Gia_ManFromIfAig_rec(Gia_Man_t * pNew,If_Man_t * pIfMan,If_Obj_t * pIfObj)899 int Gia_ManFromIfAig_rec( Gia_Man_t * pNew, If_Man_t * pIfMan, If_Obj_t * pIfObj )
900 {
901     int iLit0, iLit1;
902     if ( pIfObj->iCopy )
903         return pIfObj->iCopy;
904     iLit0 = Gia_ManFromIfAig_rec( pNew, pIfMan, pIfObj->pFanin0 );
905     iLit1 = Gia_ManFromIfAig_rec( pNew, pIfMan, pIfObj->pFanin1 );
906     iLit0 = Abc_LitNotCond( iLit0, pIfObj->fCompl0 );
907     iLit1 = Abc_LitNotCond( iLit1, pIfObj->fCompl1 );
908     pIfObj->iCopy = Gia_ManHashAnd( pNew, iLit0, iLit1 );
909     return pIfObj->iCopy;
910 }
Gia_ManFromIfAig(If_Man_t * pIfMan)911 Gia_Man_t * Gia_ManFromIfAig( If_Man_t * pIfMan )
912 {
913     int fHash = 0;
914     Gia_Man_t * pNew, * pTemp;
915     If_Obj_t * pIfObj, * pIfLeaf;
916     If_Cut_t * pCutBest;
917     Vec_Int_t * vLeaves;
918     Vec_Int_t * vAig;
919     int i, k;
920     assert( pIfMan->pPars->pLutStruct == NULL );
921     assert( pIfMan->pPars->fDelayOpt || pIfMan->pPars->fDsdBalance || pIfMan->pPars->fUserRecLib || pIfMan->pPars->fUserSesLib );
922     // create new manager
923     pNew = Gia_ManStart( If_ManObjNum(pIfMan) );
924     Gia_ManHashAlloc( pNew );
925     // iterate through nodes used in the mapping
926     vAig = Vec_IntAlloc( 1 << 16 );
927     vLeaves = Vec_IntAlloc( 16 );
928 //    If_ManForEachObj( pIfMan, pIfObj, i )
929 //        pIfObj->iCopy = 0;
930     If_ManForEachObj( pIfMan, pIfObj, i )
931     {
932         if ( pIfObj->nRefs == 0 && !If_ObjIsTerm(pIfObj) )
933             continue;
934         if ( If_ObjIsAnd(pIfObj) )
935         {
936             pCutBest = If_ObjCutBest( pIfObj );
937             // if the cut does not offer delay improvement
938 //            if ( (int)pIfObj->Level <= (int)pCutBest->Delay )
939 //            {
940 //                Gia_ManFromIfAig_rec( pNew, pIfMan, pIfObj );
941 //                continue;
942 //            }
943             // collect leaves of the best cut
944             Vec_IntClear( vLeaves );
945             If_CutForEachLeaf( pIfMan, pCutBest, pIfLeaf, k )
946                 Vec_IntPush( vLeaves, pIfLeaf->iCopy );
947             // get the functionality
948             if ( pIfMan->pPars->fDelayOpt )
949                 pIfObj->iCopy = Gia_ManBuildFromMini( pNew, pIfMan, pCutBest, vLeaves, vAig, fHash, 0 );
950             else if ( pIfMan->pPars->fDsdBalance )
951                 pIfObj->iCopy = Gia_ManBuildFromMini( pNew, pIfMan, pCutBest, vLeaves, vAig, fHash, 1 );
952             else if ( pIfMan->pPars->fUserRecLib )
953                 pIfObj->iCopy = Abc_RecToGia3( pNew, pIfMan, pCutBest, vLeaves, fHash );
954             else assert( 0 );
955         }
956         else if ( If_ObjIsCi(pIfObj) )
957             pIfObj->iCopy = Gia_ManAppendCi(pNew);
958         else if ( If_ObjIsCo(pIfObj) )
959             pIfObj->iCopy = Gia_ManAppendCo( pNew, Abc_LitNotCond(If_ObjFanin0(pIfObj)->iCopy, If_ObjFaninC0(pIfObj)) );
960         else if ( If_ObjIsConst1(pIfObj) )
961             pIfObj->iCopy = 1;
962         else assert( 0 );
963     }
964     Vec_IntFree( vAig );
965     Vec_IntFree( vLeaves );
966     pNew = Gia_ManRehash( pTemp = pNew, 0 );
967     Gia_ManStop( pTemp );
968     return pNew;
969 }
970 
971 
972 /**Function*************************************************************
973 
974   Synopsis    [Write mapping for LUT with given fanins.]
975 
976   Description []
977 
978   SideEffects []
979 
980   SeeAlso     []
981 
982 ***********************************************************************/
Gia_ManFromIfLogicCreateLut(Gia_Man_t * pNew,word * pRes,Vec_Int_t * vLeaves,Vec_Int_t * vCover,Vec_Int_t * vMapping,Vec_Int_t * vMapping2)983 int Gia_ManFromIfLogicCreateLut( Gia_Man_t * pNew, word * pRes, Vec_Int_t * vLeaves, Vec_Int_t * vCover, Vec_Int_t * vMapping, Vec_Int_t * vMapping2 )
984 {
985     int i, iLit, iObjLit1;
986     iObjLit1 = Kit_TruthToGia( pNew, (unsigned *)pRes, Vec_IntSize(vLeaves), vCover, vLeaves, 0 );
987     // do not create LUT in the simple case
988     if ( Abc_Lit2Var(iObjLit1) == 0 )
989         return iObjLit1;
990     Vec_IntForEachEntry( vLeaves, iLit, i )
991         if ( Abc_Lit2Var(iObjLit1) == Abc_Lit2Var(iLit) )
992             return iObjLit1;
993     // write mapping
994     Vec_IntSetEntry( vMapping, Abc_Lit2Var(iObjLit1), Vec_IntSize(vMapping2) );
995     Vec_IntPush( vMapping2, Vec_IntSize(vLeaves) );
996 //    Vec_IntForEachEntry( vLeaves, iLit, i )
997 //        assert( Abc_Lit2Var(iLit) < Abc_Lit2Var(iObjLit1) );
998     Vec_IntForEachEntry( vLeaves, iLit, i )
999         Vec_IntPush( vMapping2, Abc_Lit2Var(iLit)  );
1000     Vec_IntPush( vMapping2, Abc_Lit2Var(iObjLit1) );
1001     return iObjLit1;
1002 }
1003 
1004 /**Function*************************************************************
1005 
1006   Synopsis    [Write mapping for LUT with given fanins.]
1007 
1008   Description []
1009 
1010   SideEffects []
1011 
1012   SeeAlso     []
1013 
1014 ***********************************************************************/
Gia_ManFromIfLogicCreateLutSpecial(Gia_Man_t * pNew,word * pRes,Vec_Int_t * vLeaves,Vec_Int_t * vLeavesTemp,Vec_Int_t * vCover,Vec_Int_t * vMapping,Vec_Int_t * vMapping2,Vec_Int_t * vPacking)1015 int Gia_ManFromIfLogicCreateLutSpecial( Gia_Man_t * pNew, word * pRes, Vec_Int_t * vLeaves, Vec_Int_t * vLeavesTemp, Vec_Int_t * vCover, Vec_Int_t * vMapping, Vec_Int_t * vMapping2, Vec_Int_t * vPacking )
1016 {
1017     word z = If_CutPerformDerive07( NULL, (unsigned *)pRes, Vec_IntSize(vLeaves), Vec_IntSize(vLeaves), NULL );
1018     word Truth;
1019     int i, iObjLit1, iObjLit2;
1020     // create first LUT
1021     Vec_IntClear( vLeavesTemp );
1022     for ( i = 0; i < 4; i++ )
1023     {
1024         int v = (int)((z >> (16+(i<<2))) & 7);
1025         if ( v == 6 && Vec_IntSize(vLeaves) == 5 )
1026             continue;
1027         Vec_IntPush( vLeavesTemp, Vec_IntEntry(vLeaves, v) );
1028     }
1029     Truth = (z & 0xffff);
1030     Truth |= (Truth << 16);
1031     Truth |= (Truth << 32);
1032     iObjLit1 = Gia_ManFromIfLogicCreateLut( pNew, &Truth, vLeavesTemp, vCover, vMapping, vMapping2 );
1033     // create second LUT
1034     Vec_IntClear( vLeavesTemp );
1035     for ( i = 0; i < 4; i++ )
1036     {
1037         int v =  (int)((z >> (48+(i<<2))) & 7);
1038         if ( v == 6 && Vec_IntSize(vLeaves) == 5 )
1039             continue;
1040         if ( v == 7 )
1041             Vec_IntPush( vLeavesTemp, iObjLit1 );
1042         else
1043             Vec_IntPush( vLeavesTemp, Vec_IntEntry(vLeaves, v) );
1044     }
1045     Truth = ((z >> 32) & 0xffff);
1046     Truth |= (Truth << 16);
1047     Truth |= (Truth << 32);
1048     iObjLit2 = Gia_ManFromIfLogicCreateLut( pNew, &Truth, vLeavesTemp, vCover, vMapping, vMapping2 );
1049     // write packing
1050     Vec_IntPush( vPacking, 2 );
1051     Vec_IntPush( vPacking, Abc_Lit2Var(iObjLit1) );
1052     Vec_IntPush( vPacking, Abc_Lit2Var(iObjLit2) );
1053     Vec_IntAddToEntry( vPacking, 0, 1 );
1054     return iObjLit2;
1055 }
1056 
1057 /**Function*************************************************************
1058 
1059   Synopsis    [Write the node into a file.]
1060 
1061   Description []
1062 
1063   SideEffects []
1064 
1065   SeeAlso     []
1066 
1067 ***********************************************************************/
Gia_ManFromIfLogicNode(void * pIfMan,Gia_Man_t * pNew,int iObj,Vec_Int_t * vLeaves,Vec_Int_t * vLeavesTemp,word * pRes,char * pStr,Vec_Int_t * vCover,Vec_Int_t * vMapping,Vec_Int_t * vMapping2,Vec_Int_t * vPacking,int fCheck75,int fCheck44e)1068 int Gia_ManFromIfLogicNode( void * pIfMan, Gia_Man_t * pNew, int iObj, Vec_Int_t * vLeaves, Vec_Int_t * vLeavesTemp,
1069     word * pRes, char * pStr, Vec_Int_t * vCover, Vec_Int_t * vMapping, Vec_Int_t * vMapping2, Vec_Int_t * vPacking, int fCheck75, int fCheck44e )
1070 {
1071     int nLeaves = Vec_IntSize(vLeaves);
1072     int i, Length, nLutLeaf, nLutLeaf2, nLutRoot, iObjLit1, iObjLit2, iObjLit3;
1073     // workaround for the special case
1074     if ( fCheck75 )
1075         pStr = "54";
1076     // perform special case matching for 44
1077     if ( fCheck44e )
1078     {
1079         if ( Vec_IntSize(vLeaves) <= 4 )
1080         {
1081             // create mapping
1082             iObjLit1 = Gia_ManFromIfLogicCreateLut( pNew, pRes, vLeaves, vCover, vMapping, vMapping2 );
1083             // write packing
1084             if ( !Gia_ObjIsCi(Gia_ManObj(pNew, Abc_Lit2Var(iObjLit1))) && iObjLit1 > 1 )
1085             {
1086                 Vec_IntPush( vPacking, 1 );
1087                 Vec_IntPush( vPacking, Abc_Lit2Var(iObjLit1) );
1088                 Vec_IntAddToEntry( vPacking, 0, 1 );
1089             }
1090             return iObjLit1;
1091         }
1092         return Gia_ManFromIfLogicCreateLutSpecial( pNew, pRes, vLeaves, vLeavesTemp, vCover, vMapping, vMapping2, vPacking );
1093     }
1094     if ( ((If_Man_t *)pIfMan)->pPars->fLut6Filter && Vec_IntSize(vLeaves) == 6 )
1095     {
1096         extern word If_Dec6Perform( word t, int fDerive );
1097         extern void If_Dec6Verify( word t, word z );
1098         Vec_Int_t * vLeaves2 = Vec_IntAlloc( 4 );
1099         word t = pRes[0];
1100         word z = If_Dec6Perform( t, 1 );
1101         //If_DecPrintConfig( z );
1102         If_Dec6Verify( t, z );
1103 
1104         t = Abc_Tt6Stretch( z & 0xffff, 4 );
1105         Vec_IntClear( vLeaves2 );
1106         for ( i = 0; i < 4; i++ )
1107             Vec_IntPush( vLeaves2, Vec_IntEntry( vLeaves, (int)((z >> (16+i*4)) & 7) ) );
1108         iObjLit1 = Gia_ManFromIfLogicCreateLut( pNew, &t, vLeaves2, vCover, vMapping, vMapping2 );
1109 
1110         t = Abc_Tt6Stretch( (z >> 32) & 0xffff, 4 );
1111         Vec_IntClear( vLeaves2 );
1112         for ( i = 0; i < 4; i++ )
1113             if ( ((z >> (48+i*4)) & 7) == 7 )
1114                 Vec_IntPush( vLeaves2, iObjLit1 );
1115             else
1116                 Vec_IntPush( vLeaves2, Vec_IntEntry( vLeaves, (int)((z >> (48+i*4)) & 7) ) );
1117         iObjLit1 = Gia_ManFromIfLogicCreateLut( pNew, &t, vLeaves2, vCover, vMapping, vMapping2 );
1118 
1119         Vec_IntFree( vLeaves2 );
1120         return iObjLit1;
1121     }
1122     // check if there is no LUT structures
1123     if ( pStr == NULL )
1124         return Gia_ManFromIfLogicCreateLut( pNew, pRes, vLeaves, vCover, vMapping, vMapping2 );
1125     // quit if parameters are wrong
1126     Length = strlen(pStr);
1127     if ( Length != 2 && Length != 3 )
1128     {
1129         printf( "Wrong LUT struct (%s)\n", pStr );
1130         return -1;
1131     }
1132     for ( i = 0; i < Length; i++ )
1133         if ( pStr[i] - '0' < 3 || pStr[i] - '0' > 6 )
1134         {
1135             printf( "The LUT size (%d) should belong to {3,4,5,6}.\n", pStr[i] - '0' );
1136             return -1;
1137         }
1138 
1139     nLutLeaf  =                   pStr[0] - '0';
1140     nLutLeaf2 = ( Length == 3 ) ? pStr[1] - '0' : 0;
1141     nLutRoot  =                   pStr[Length-1] - '0';
1142     if ( nLeaves > nLutLeaf - 1 + (nLutLeaf2 ? nLutLeaf2 - 1 : 0) + nLutRoot )
1143     {
1144         printf( "The node size (%d) is too large for the LUT structure %s.\n", nLeaves, pStr );
1145         return -1;
1146     }
1147 
1148     // consider easy case
1149     if ( nLeaves <= Abc_MaxInt( nLutLeaf2, Abc_MaxInt(nLutLeaf, nLutRoot) ) )
1150     {
1151         // create mapping
1152         iObjLit1 = Gia_ManFromIfLogicCreateLut( pNew, pRes, vLeaves, vCover, vMapping, vMapping2 );
1153         // write packing
1154         if ( !Gia_ObjIsCi(Gia_ManObj(pNew, Abc_Lit2Var(iObjLit1))) && iObjLit1 > 1 )
1155         {
1156             Vec_IntPush( vPacking, 1 );
1157             Vec_IntPush( vPacking, Abc_Lit2Var(iObjLit1) );
1158             Vec_IntAddToEntry( vPacking, 0, 1 );
1159         }
1160         return iObjLit1;
1161     }
1162     else
1163     {
1164         extern int If_CluMinimumBase( word * t, int * pSupp, int nVarsAll, int * pnVars );
1165 
1166         static word TruthStore[16][1<<10] = {{0}}, * pTruths[16];
1167         word Func0, Func1, Func2;
1168         char pLut0[32], pLut1[32], pLut2[32] = {0};
1169 
1170         if ( TruthStore[0][0] == 0 )
1171         {
1172             static word Truth6[6] = {
1173                 0xAAAAAAAAAAAAAAAA,
1174                 0xCCCCCCCCCCCCCCCC,
1175                 0xF0F0F0F0F0F0F0F0,
1176                 0xFF00FF00FF00FF00,
1177                 0xFFFF0000FFFF0000,
1178                 0xFFFFFFFF00000000
1179             };
1180             int nVarsMax = 16;
1181             int nWordsMax = (1 << 10);
1182             int i, k;
1183             assert( nVarsMax <= 16 );
1184             for ( i = 0; i < nVarsMax; i++ )
1185                 pTruths[i] = TruthStore[i];
1186             for ( i = 0; i < 6; i++ )
1187                 for ( k = 0; k < nWordsMax; k++ )
1188                     pTruths[i][k] = Truth6[i];
1189             for ( i = 6; i < nVarsMax; i++ )
1190                 for ( k = 0; k < nWordsMax; k++ )
1191                     pTruths[i][k] = ((k >> (i-6)) & 1) ? ~(word)0 : 0;
1192         }
1193         // derive truth table
1194         if ( Kit_TruthIsConst0((unsigned *)pRes, nLeaves) || Kit_TruthIsConst1((unsigned *)pRes, nLeaves) )
1195         {
1196 //            fprintf( pFile, ".names %s\n %d\n", Abc_ObjName(Abc_ObjFanout0(pObj)), Kit_TruthIsConst1((unsigned *)pRes, nLeaves) );
1197             iObjLit1 = Abc_LitNotCond( 0, Kit_TruthIsConst1((unsigned *)pRes, nLeaves) );
1198             // write mapping
1199             if ( Vec_IntEntry(vMapping, 0) == 0 )
1200             {
1201                 Vec_IntSetEntry( vMapping, 0, Vec_IntSize(vMapping2) );
1202                 Vec_IntPush( vMapping2, 0 );
1203                 Vec_IntPush( vMapping2, 0 );
1204             }
1205             return iObjLit1;
1206         }
1207         // check for elementary truth table
1208         for ( i = 0; i < nLeaves; i++ )
1209         {
1210             if ( Kit_TruthIsEqual((unsigned *)pRes, (unsigned *)pTruths[i], nLeaves) )
1211                 return Vec_IntEntry(vLeaves, i);
1212             if ( Kit_TruthIsOpposite((unsigned *)pRes, (unsigned *)pTruths[i], nLeaves) )
1213                 return Abc_LitNot(Vec_IntEntry(vLeaves, i));
1214         }
1215 
1216         // perform decomposition
1217         if ( fCheck75 )
1218         {
1219 //            if ( nLeaves < 8 && If_CutPerformCheck16( p, (unsigned *)pTruth, nVars, nLeaves, "44" ) )
1220             if ( nLeaves < 8 && If_CluCheckExt( NULL, pRes, nLeaves, 4, 4, pLut0, pLut1, &Func0, &Func1 ) )
1221             {
1222                 nLutLeaf = 4;
1223                 nLutRoot = 4;
1224             }
1225 //            if ( If_CutPerformCheck45( p, (unsigned *)pTruth, nVars, nLeaves, pStr ) )
1226             else if ( If_CluCheckExt( NULL, pRes, nLeaves, 5, 4, pLut0, pLut1, &Func0, &Func1 ) )
1227             {
1228                 nLutLeaf = 5;
1229                 nLutRoot = 4;
1230             }
1231 //            if ( If_CutPerformCheck54( p, (unsigned *)pTruth, nVars, nLeaves, pStr ) )
1232             else if ( If_CluCheckExt( NULL, pRes, nLeaves, 4, 5, pLut0, pLut1, &Func0, &Func1 ) )
1233             {
1234                 nLutLeaf = 4;
1235                 nLutRoot = 5;
1236             }
1237             else
1238             {
1239                 Extra_PrintHex( stdout, (unsigned *)pRes, nLeaves );  printf( "    " );
1240                 Kit_DsdPrintFromTruth( (unsigned*)pRes, nLeaves );  printf( "\n" );
1241                 printf( "Node %d is not decomposable. Deriving LUT structures has failed.\n", iObj );
1242                 return -1;
1243             }
1244         }
1245         else
1246         {
1247             if ( Length == 2 )
1248             {
1249                 if ( !If_CluCheckExt( NULL, pRes, nLeaves, nLutLeaf, nLutRoot, pLut0, pLut1, &Func0, &Func1 ) )
1250                 {
1251                     Extra_PrintHex( stdout, (unsigned *)pRes, nLeaves );  printf( "    " );
1252                     Kit_DsdPrintFromTruth( (unsigned*)pRes, nLeaves );  printf( "\n" );
1253                     printf( "Node %d is not decomposable. Deriving LUT structures has failed.\n", iObj );
1254                     return -1;
1255                 }
1256             }
1257             else
1258             {
1259                 if ( !If_CluCheckExt3( pIfMan, pRes, nLeaves, nLutLeaf, nLutLeaf2, nLutRoot, pLut0, pLut1, pLut2, &Func0, &Func1, &Func2 ) )
1260                 {
1261                     Extra_PrintHex( stdout, (unsigned *)pRes, nLeaves );  printf( "    " );
1262                     Kit_DsdPrintFromTruth( (unsigned*)pRes, nLeaves );  printf( "\n" );
1263                     printf( "Node %d is not decomposable. Deriving LUT structures has failed.\n", iObj );
1264                     return -1;
1265                 }
1266             }
1267         }
1268 
1269 /*
1270         // write leaf node
1271         Id = Abc2_NtkAllocObj( pNew, pLut1[0], Abc2_ObjType(pObj) );
1272         iObjLit1 = Abc_Var2Lit( Id, 0 );
1273         pObjNew = Abc2_NtkObj( pNew, Id );
1274         for ( i = 0; i < pLut1[0]; i++ )
1275             Abc2_ObjSetFaninLit( pObjNew, i, Abc2_ObjFaninCopy(pObj, pLut1[2+i]) );
1276         Abc2_ObjSetTruth( pObjNew, Func1 );
1277 */
1278         // write leaf node
1279         Vec_IntClear( vLeavesTemp );
1280         for ( i = 0; i < pLut1[0]; i++ )
1281             Vec_IntPush( vLeavesTemp, Vec_IntEntry(vLeaves, pLut1[2+i]) );
1282         iObjLit1 = Gia_ManFromIfLogicCreateLut( pNew, &Func1, vLeavesTemp, vCover, vMapping, vMapping2 );
1283 
1284         if ( Length == 3 && pLut2[0] > 0 )
1285         {
1286         /*
1287             Id = Abc2_NtkAllocObj( pNew, pLut2[0], Abc2_ObjType(pObj) );
1288             iObjLit2 = Abc_Var2Lit( Id, 0 );
1289             pObjNew = Abc2_NtkObj( pNew, Id );
1290             for ( i = 0; i < pLut2[0]; i++ )
1291                 if ( pLut2[2+i] == nLeaves )
1292                     Abc2_ObjSetFaninLit( pObjNew, i, iObjLit1 );
1293                 else
1294                     Abc2_ObjSetFaninLit( pObjNew, i, Abc2_ObjFaninCopy(pObj, pLut2[2+i]) );
1295             Abc2_ObjSetTruth( pObjNew, Func2 );
1296         */
1297 
1298             // write leaf node
1299             Vec_IntClear( vLeavesTemp );
1300             for ( i = 0; i < pLut2[0]; i++ )
1301                 if ( pLut2[2+i] == nLeaves )
1302                     Vec_IntPush( vLeavesTemp, iObjLit1 );
1303                 else
1304                     Vec_IntPush( vLeavesTemp, Vec_IntEntry(vLeaves, pLut2[2+i]) );
1305             iObjLit2 = Gia_ManFromIfLogicCreateLut( pNew, &Func2, vLeavesTemp, vCover, vMapping, vMapping2 );
1306 
1307             // write packing
1308             Vec_IntPush( vPacking, 3 );
1309             Vec_IntPush( vPacking, Abc_Lit2Var(iObjLit1) );
1310             Vec_IntPush( vPacking, Abc_Lit2Var(iObjLit2) );
1311         }
1312         else
1313         {
1314             // write packing
1315             Vec_IntPush( vPacking, 2 );
1316             Vec_IntPush( vPacking, Abc_Lit2Var(iObjLit1) );
1317             iObjLit2 = -1;
1318         }
1319 /*
1320         // write root node
1321         Id = Abc2_NtkAllocObj( pNew, pLut0[0], Abc2_ObjType(pObj) );
1322         iObjLit3 = Abc_Var2Lit( Id, 0 );
1323         pObjNew = Abc2_NtkObj( pNew, Id );
1324         for ( i = 0; i < pLut0[0]; i++ )
1325             if ( pLut0[2+i] == nLeaves )
1326                 Abc2_ObjSetFaninLit( pObjNew, i, iObjLit1 );
1327             else if ( pLut0[2+i] == nLeaves+1 )
1328                 Abc2_ObjSetFaninLit( pObjNew, i, iObjLit2 );
1329             else
1330                 Abc2_ObjSetFaninLit( pObjNew, i, Abc2_ObjFaninCopy(pObj, pLut0[2+i]) );
1331         Abc2_ObjSetTruth( pObjNew, Func0 );
1332         Abc2_ObjSetCopy( pObj, iObjLit3 );
1333 */
1334         // write root node
1335         Vec_IntClear( vLeavesTemp );
1336         for ( i = 0; i < pLut0[0]; i++ )
1337             if ( pLut0[2+i] == nLeaves )
1338                 Vec_IntPush( vLeavesTemp, iObjLit1 );
1339             else if ( pLut0[2+i] == nLeaves+1 )
1340                 Vec_IntPush( vLeavesTemp, iObjLit2 );
1341             else
1342                 Vec_IntPush( vLeavesTemp, Vec_IntEntry(vLeaves, pLut0[2+i]) );
1343         iObjLit3 = Gia_ManFromIfLogicCreateLut( pNew, &Func0, vLeavesTemp, vCover, vMapping, vMapping2 );
1344 
1345         // write packing
1346         Vec_IntPush( vPacking, Abc_Lit2Var(iObjLit3) );
1347         Vec_IntAddToEntry( vPacking, 0, 1 );
1348     }
1349     return iObjLit3;
1350 }
1351 
1352 /**Function*************************************************************
1353 
1354   Synopsis    [Recursively derives the local AIG for the cut.]
1355 
1356   Description []
1357 
1358   SideEffects []
1359 
1360   SeeAlso     []
1361 
1362 ***********************************************************************/
Gia_ManNodeIfToGia_rec(Gia_Man_t * pNew,If_Man_t * pIfMan,If_Obj_t * pIfObj,Vec_Ptr_t * vVisited,int fHash)1363 int Gia_ManNodeIfToGia_rec( Gia_Man_t * pNew, If_Man_t * pIfMan, If_Obj_t * pIfObj, Vec_Ptr_t * vVisited, int fHash )
1364 {
1365     If_Cut_t * pCut;
1366     If_Obj_t * pTemp;
1367     int iFunc, iFunc0, iFunc1;
1368     // get the best cut
1369     pCut = If_ObjCutBest(pIfObj);
1370     // if the cut is visited, return the result
1371     if ( If_CutDataInt(pCut) )
1372         return If_CutDataInt(pCut);
1373     // mark the node as visited
1374     Vec_PtrPush( vVisited, pCut );
1375     // insert the worst case
1376     If_CutSetDataInt( pCut, ~0 );
1377     // skip in case of primary input
1378     if ( If_ObjIsCi(pIfObj) )
1379         return If_CutDataInt(pCut);
1380     // compute the functions of the children
1381     for ( pTemp = pIfObj; pTemp; pTemp = pTemp->pEquiv )
1382     {
1383         iFunc0 = Gia_ManNodeIfToGia_rec( pNew, pIfMan, pTemp->pFanin0, vVisited, fHash );
1384         if ( iFunc0 == ~0 )
1385             continue;
1386         iFunc1 = Gia_ManNodeIfToGia_rec( pNew, pIfMan, pTemp->pFanin1, vVisited, fHash );
1387         if ( iFunc1 == ~0 )
1388             continue;
1389         // both branches are solved
1390         if ( fHash )
1391             iFunc = Gia_ManHashAnd( pNew, Abc_LitNotCond(iFunc0, pTemp->fCompl0), Abc_LitNotCond(iFunc1, pTemp->fCompl1) );
1392         else
1393             iFunc = Gia_ManAppendAnd( pNew, Abc_LitNotCond(iFunc0, pTemp->fCompl0), Abc_LitNotCond(iFunc1, pTemp->fCompl1) );
1394         if ( pTemp->fPhase != pIfObj->fPhase )
1395             iFunc = Abc_LitNot(iFunc);
1396         If_CutSetDataInt( pCut, iFunc );
1397         break;
1398     }
1399     return If_CutDataInt(pCut);
1400 }
Gia_ManNodeIfToGia(Gia_Man_t * pNew,If_Man_t * pIfMan,If_Obj_t * pIfObj,Vec_Int_t * vLeaves,int fHash)1401 int Gia_ManNodeIfToGia( Gia_Man_t * pNew, If_Man_t * pIfMan, If_Obj_t * pIfObj, Vec_Int_t * vLeaves, int fHash )
1402 {
1403     If_Cut_t * pCut;
1404     If_Obj_t * pLeaf;
1405     int i, iRes;
1406     // get the best cut
1407     pCut = If_ObjCutBest(pIfObj);
1408     assert( pCut->nLeaves > 1 );
1409     // set the leaf variables
1410     If_CutForEachLeaf( pIfMan, pCut, pLeaf, i )
1411         If_CutSetDataInt( If_ObjCutBest(pLeaf), Vec_IntEntry(vLeaves, i) );
1412     // recursively compute the function while collecting visited cuts
1413     Vec_PtrClear( pIfMan->vTemp );
1414     iRes = Gia_ManNodeIfToGia_rec( pNew, pIfMan, pIfObj, pIfMan->vTemp, fHash );
1415     if ( iRes == ~0 )
1416     {
1417         Abc_Print( -1, "Gia_ManNodeIfToGia(): Computing local AIG has failed.\n" );
1418         return ~0;
1419     }
1420     // clean the cuts
1421     If_CutForEachLeaf( pIfMan, pCut, pLeaf, i )
1422         If_CutSetDataInt( If_ObjCutBest(pLeaf), 0 );
1423     Vec_PtrForEachEntry( If_Cut_t *, pIfMan->vTemp, pCut, i )
1424         If_CutSetDataInt( pCut, 0 );
1425     return iRes;
1426 }
1427 
1428 /**Function*************************************************************
1429 
1430   Synopsis    [Converts IF into GIA manager.]
1431 
1432   Description []
1433 
1434   SideEffects []
1435 
1436   SeeAlso     []
1437 
1438 ***********************************************************************/
Gia_ManFromIfLogicFindLut(If_Man_t * pIfMan,Gia_Man_t * pNew,If_Cut_t * pCutBest,sat_solver * pSat,Vec_Int_t * vLeaves,Vec_Int_t * vLits,Vec_Int_t * vCover,Vec_Int_t * vMapping,Vec_Int_t * vMapping2,Vec_Int_t * vPacking)1439 int Gia_ManFromIfLogicFindLut( If_Man_t * pIfMan, Gia_Man_t * pNew, If_Cut_t * pCutBest, sat_solver * pSat, Vec_Int_t * vLeaves, Vec_Int_t * vLits, Vec_Int_t * vCover, Vec_Int_t * vMapping, Vec_Int_t * vMapping2, Vec_Int_t * vPacking )
1440 {
1441     word uBound, uFree;
1442     int nLutSize = (int)(pIfMan->pPars->pLutStruct[0] - '0');
1443     int nVarsF = 0, pVarsF[IF_MAX_FUNC_LUTSIZE];
1444     int nVarsB = 0, pVarsB[IF_MAX_FUNC_LUTSIZE];
1445     int nVarsS = 0, pVarsS[IF_MAX_FUNC_LUTSIZE];
1446     unsigned uSetNew, uSetOld;
1447     int RetValue, RetValue2, k;
1448     char * pPerm;
1449     if ( Vec_IntSize(vLeaves) <= nLutSize )
1450     {
1451         RetValue = Gia_ManFromIfLogicCreateLut( pNew, If_CutTruthW(pIfMan, pCutBest), vLeaves, vCover, vMapping, vMapping2 );
1452         // write packing
1453         if ( !Gia_ObjIsCi(Gia_ManObj(pNew, Abc_Lit2Var(RetValue))) && RetValue > 1 )
1454         {
1455             Vec_IntPush( vPacking, 1 );
1456             Vec_IntPush( vPacking, Abc_Lit2Var(RetValue) );
1457             Vec_IntAddToEntry( vPacking, 0, 1 );
1458         }
1459         return RetValue;
1460     }
1461     assert( If_DsdManSuppSize(pIfMan->pIfDsdMan, If_CutDsdLit(pIfMan, pCutBest)) == (int)pCutBest->nLeaves );
1462     // find the bound set
1463     if ( pIfMan->pPars->fDelayOptLut )
1464         uSetOld = pCutBest->uMaskFunc;
1465     else
1466         uSetOld = If_DsdManCheckXY( pIfMan->pIfDsdMan, If_CutDsdLit(pIfMan, pCutBest), nLutSize, 1, 0, 1, 0 );
1467     // remap bound set
1468     uSetNew = 0;
1469     pPerm = If_CutDsdPerm( pIfMan, pCutBest );
1470     for ( k = 0; k < If_CutLeaveNum(pCutBest); k++ )
1471     {
1472         int iVar = Abc_Lit2Var((int)pPerm[k]);
1473         int Value = ((uSetOld >> (k << 1)) & 3);
1474         if ( Value == 1 )
1475             uSetNew |= (1 << (2*iVar));
1476         else if ( Value == 3 )
1477             uSetNew |= (3 << (2*iVar));
1478         else assert( Value == 0 );
1479     }
1480     RetValue = If_ManSatCheckXY( pSat, nLutSize, If_CutTruthW(pIfMan, pCutBest), pCutBest->nLeaves, uSetNew, &uBound, &uFree, vLits );
1481     assert( RetValue );
1482     // collect variables
1483     for ( k = 0; k < If_CutLeaveNum(pCutBest); k++ )
1484     {
1485         int Value = ((uSetNew >> (k << 1)) & 3);
1486         if ( Value == 0 )
1487             pVarsF[nVarsF++] = k;
1488         else if ( Value == 1 )
1489             pVarsB[nVarsB++] = k;
1490         else if ( Value == 3 )
1491             pVarsS[nVarsS++] = k;
1492         else assert( Value == 0 );
1493     }
1494     // collect bound set variables
1495     Vec_IntClear( vLits );
1496     for ( k = 0; k < nVarsS; k++ )
1497         Vec_IntPush( vLits, Vec_IntEntry(vLeaves, pVarsS[k]) );
1498     for ( k = 0; k < nVarsB; k++ )
1499         Vec_IntPush( vLits, Vec_IntEntry(vLeaves, pVarsB[k]) );
1500     RetValue = Gia_ManFromIfLogicCreateLut( pNew, &uBound, vLits, vCover, vMapping, vMapping2 );
1501     // collecct free set variables
1502     Vec_IntClear( vLits );
1503     Vec_IntPush( vLits, RetValue );
1504     for ( k = 0; k < nVarsS; k++ )
1505         Vec_IntPush( vLits, Vec_IntEntry(vLeaves, pVarsS[k]) );
1506     for ( k = 0; k < nVarsF; k++ )
1507         Vec_IntPush( vLits, Vec_IntEntry(vLeaves, pVarsF[k]) );
1508     // add packing
1509     RetValue2 = Gia_ManFromIfLogicCreateLut( pNew, &uFree, vLits, vCover, vMapping, vMapping2 );
1510     // write packing
1511     Vec_IntPush( vPacking, 2 );
1512     Vec_IntPush( vPacking, Abc_Lit2Var(RetValue) );
1513     Vec_IntPush( vPacking, Abc_Lit2Var(RetValue2) );
1514     Vec_IntAddToEntry( vPacking, 0, 1 );
1515     return RetValue2;
1516 }
1517 
1518 /**Function*************************************************************
1519 
1520   Synopsis    [Converts IF into GIA manager.]
1521 
1522   Description []
1523 
1524   SideEffects []
1525 
1526   SeeAlso     []
1527 
1528 ***********************************************************************/
Gia_ManFromIfGetConfig(Vec_Int_t * vConfigs,If_Man_t * pIfMan,If_Cut_t * pCutBest,int iLit,Vec_Str_t * vConfigsStr)1529 void Gia_ManFromIfGetConfig( Vec_Int_t * vConfigs, If_Man_t * pIfMan, If_Cut_t * pCutBest, int iLit, Vec_Str_t * vConfigsStr )
1530 {
1531     If_Obj_t * pIfObj = NULL;
1532     word * pPerm = If_DsdManGetFuncConfig( pIfMan->pIfDsdMan, If_CutDsdLit(pIfMan, pCutBest) ); // cell input -> DSD input
1533     char * pCutPerm = If_CutDsdPerm( pIfMan, pCutBest ); // DSD input -> cut input
1534     word * pArray;  int v, i, Lit, Var;
1535     int nVarNum = If_DsdManVarNum(pIfMan->pIfDsdMan);
1536     int nTtBitNum = If_DsdManTtBitNum(pIfMan->pIfDsdMan);
1537     int nPermBitNum = If_DsdManPermBitNum(pIfMan->pIfDsdMan);
1538     int nPermBitOne = nPermBitNum / nVarNum;
1539     // prepare storage
1540     int nIntNum = Vec_IntEntry( vConfigs, 1 );
1541     for ( i = 0; i < nIntNum; i++ )
1542         Vec_IntPush( vConfigs, 0 );
1543     pArray = (word *)Vec_IntEntryP( vConfigs, Vec_IntSize(vConfigs) - nIntNum );
1544     assert( nPermBitNum % nVarNum == 0 );
1545     // set truth table bits
1546     for ( i = 0; i < nTtBitNum; i++ )
1547         if ( Abc_TtGetBit(pPerm + 1, i) )
1548             Abc_TtSetBit( pArray, i );
1549     // set permutation bits
1550     for ( v = 0; v < nVarNum; v++ )
1551     {
1552         // get DSD variable
1553         Var = ((pPerm[0] >> (v * 4)) & 0xF);
1554         assert( Var < (int)pCutBest->nLeaves );
1555         // get AIG literal
1556         Lit = (int)pCutPerm[Var];
1557         assert( Abc_Lit2Var(Lit) < (int)pCutBest->nLeaves );
1558         // complement if polarity has changed
1559         pIfObj = If_ManObj( pIfMan, pCutBest->pLeaves[Abc_Lit2Var(Lit)] );
1560         Lit = Abc_LitNotCond( Lit, Abc_LitIsCompl(pIfObj->iCopy) );
1561         // create config literal
1562         for ( i = 0; i < nPermBitOne; i++ )
1563             if ( (Lit >> i) & 1 )
1564                 Abc_TtSetBit( pArray, nTtBitNum + v * nPermBitOne + i );
1565     }
1566     // remember complementation
1567     assert( nTtBitNum + nPermBitNum < 32 * nIntNum );
1568     if ( Abc_LitIsCompl(If_CutDsdLit(pIfMan, pCutBest)) ^ pCutBest->fCompl ^ Abc_LitIsCompl(iLit) )
1569         Abc_TtSetBit( pArray, nTtBitNum + nPermBitNum );
1570     // update count
1571     Vec_IntAddToEntry( vConfigs, 0, 1 );
1572     // write configs
1573     if ( vConfigsStr )
1574     {
1575         Vec_StrPrintF( vConfigsStr, "%d", Abc_Lit2Var(iLit) );
1576         Vec_StrPush( vConfigsStr, ' ' );
1577         for ( i = 0; i < nTtBitNum; i++ )
1578             Vec_StrPush( vConfigsStr, (char)(Abc_TtGetBit(pArray, i) ? '1' : '0') );
1579         Vec_StrPush( vConfigsStr, ' ' );
1580         Vec_StrPush( vConfigsStr, ' ' );
1581         for ( v = 0; v < nVarNum; v++ )
1582         {
1583             for ( i = 0; i < nPermBitOne; i++ )
1584             {
1585                 Vec_StrPush( vConfigsStr, (char)(Abc_TtGetBit(pArray, nTtBitNum + v * nPermBitOne + i) ? '1' : '0') );
1586                 if ( i == 0 )
1587                     Vec_StrPush( vConfigsStr, ' ' );
1588             }
1589             Vec_StrPush( vConfigsStr, ' ' );
1590             Vec_StrPush( vConfigsStr, ' ' );
1591         }
1592         Vec_StrPush( vConfigsStr, (char)(Abc_TtGetBit(pArray, nTtBitNum + nPermBitNum) ? '1' : '0') );
1593         Vec_StrPush( vConfigsStr, '\n' );
1594     }
1595 }
Gia_ManFromIfLogicFindCell(If_Man_t * pIfMan,Gia_Man_t * pNew,Gia_Man_t * pTemp,If_Cut_t * pCutBest,Ifn_Ntk_t * pNtkCell,int nLutMax,Vec_Int_t * vLeaves,Vec_Int_t * vLits,Vec_Int_t * vCover,Vec_Int_t * vMapping,Vec_Int_t * vMapping2,Vec_Int_t * vConfigs)1596 int Gia_ManFromIfLogicFindCell( If_Man_t * pIfMan, Gia_Man_t * pNew, Gia_Man_t * pTemp, If_Cut_t * pCutBest, Ifn_Ntk_t * pNtkCell, int nLutMax, Vec_Int_t * vLeaves, Vec_Int_t * vLits, Vec_Int_t * vCover, Vec_Int_t * vMapping, Vec_Int_t * vMapping2, Vec_Int_t * vConfigs )
1597 {
1598     int iLit;
1599     assert( 0 );
1600     if ( Vec_IntSize(vLeaves) <= nLutMax )
1601         iLit = Gia_ManFromIfLogicCreateLut( pNew, If_CutTruthW(pIfMan, pCutBest), vLeaves, vCover, vMapping, vMapping2 );
1602     else
1603     {
1604         Gia_Obj_t * pObj;
1605         int i, Id, iLitTemp;
1606         // extract variable permutation
1607         //char * pCutPerm = If_CutDsdPerm( pIfMan, pCutBest ); // DSD input -> cut input
1608         word * pPerm = If_DsdManGetFuncConfig( pIfMan->pIfDsdMan, If_CutDsdLit(pIfMan, pCutBest) ); // cell input -> DSD input
1609         //int nBits = If_DsdManTtBitNum( pIfMan->pIfDsdMan );
1610         // use config bits to generate the network
1611         iLit = If_ManSatDeriveGiaFromBits( pTemp, pNtkCell, pPerm + 1, vLeaves, vCover );
1612         // copy GIA back into the manager
1613         Vec_IntFillExtra( &pTemp->vCopies, Gia_ManObjNum(pTemp), -1 );
1614         Gia_ObjSetCopyArray( pTemp, 0, 0 );
1615         Vec_IntForEachEntry( vLeaves, iLitTemp, i )
1616             Gia_ObjSetCopyArray( pTemp, Gia_ManCiIdToId(pTemp, i), iLitTemp );
1617         // collect nodes
1618         Gia_ManIncrementTravId( pTemp );
1619         Id = Abc_Lit2Var( iLit );
1620         Gia_ManCollectAnds( pTemp, &Id, 1, vCover, NULL );
1621         Vec_IntPrint( vCover );
1622         Gia_ManForEachObjVec( vCover, pTemp, pObj, i )
1623             Gia_ObjPrint( pTemp, pObj );
1624         // copy GIA
1625         Gia_ManForEachObjVec( vCover, pTemp, pObj, i )
1626         {
1627             iLit = Gia_ManAppendAnd( pNew, Gia_ObjFanin0CopyArray(pTemp, pObj), Gia_ObjFanin1CopyArray(pTemp, pObj) );
1628             Gia_ObjSetCopyArray( pTemp, Gia_ObjId(pTemp, pObj), iLit );
1629         }
1630         iLit = Abc_LitNotCond( Gia_ObjCopyArray(pTemp, Id), Abc_LitIsCompl(iLit) );
1631     }
1632     // write packing
1633 //    if ( vConfigs && !Gia_ObjIsCi(Gia_ManObj(pNew, Abc_Lit2Var(iLit))) && iLit > 1 )
1634 //        Gia_ManFromIfGetConfig( vConfigs, pIfMan, pCutBest );
1635     return iLit;
1636 }
1637 
1638 
1639 /**Function*************************************************************
1640 
1641   Synopsis    [Converts IF into GIA manager.]
1642 
1643   Description []
1644 
1645   SideEffects []
1646 
1647   SeeAlso     []
1648 
1649 ***********************************************************************/
Gia_ManFromIfLogicCofVars(Gia_Man_t * pNew,If_Man_t * pIfMan,If_Cut_t * pCutBest,Vec_Int_t * vLeaves,Vec_Int_t * vLeaves2,Vec_Int_t * vCover,Vec_Int_t * vMapping,Vec_Int_t * vMapping2)1650 int Gia_ManFromIfLogicCofVars( Gia_Man_t * pNew, If_Man_t * pIfMan, If_Cut_t * pCutBest, Vec_Int_t * vLeaves, Vec_Int_t * vLeaves2, Vec_Int_t * vCover, Vec_Int_t * vMapping, Vec_Int_t * vMapping2 )
1651 {
1652     word pTruthCof[128], * pTruth = If_CutTruthW(pIfMan, pCutBest);
1653     int pVarsNew[16], nVarsNew, iLitCofs[3];
1654     int nLeaves = pCutBest->nLeaves;
1655     int nWords  = Abc_Truth6WordNum(nLeaves);
1656     int truthId = Abc_Lit2Var(pCutBest->iCutFunc);
1657     int c, iVar = Vec_StrEntry(pIfMan->vTtVars[nLeaves], truthId), iTemp, iTopLit;
1658     int k, RetValue = -1;
1659     assert( iVar >= 0 && iVar < nLeaves && pIfMan->pPars->nLutSize <= 13 );
1660     for ( c = 0; c < 2; c++ )
1661     {
1662         for ( k = 0; k < nLeaves; k++ )
1663             pVarsNew[k] = k;
1664         if ( c )
1665             Abc_TtCofactor1p( pTruthCof, pTruth, nWords, iVar );
1666         else
1667             Abc_TtCofactor0p( pTruthCof, pTruth, nWords, iVar );
1668         nVarsNew = Abc_TtMinBase( pTruthCof, pVarsNew, pCutBest->nLeaves, Abc_MaxInt(6, pCutBest->nLeaves) );
1669         // derive LUT
1670         Vec_IntClear( vLeaves2 );
1671         for ( k = 0; k < nVarsNew; k++ )
1672             Vec_IntPush( vLeaves2, Vec_IntEntry(vLeaves, pVarsNew[k]) );
1673         iLitCofs[c] = Kit_TruthToGia( pNew, (unsigned *)pTruthCof, nVarsNew, vCover, vLeaves2, 0 );
1674         if ( nVarsNew < 2 )
1675             continue;
1676         // create mapping
1677         assert( Gia_ObjIsAnd(Gia_ManObj(pNew, Abc_Lit2Var(iLitCofs[c]))) );
1678         Vec_IntSetEntry( vMapping, Abc_Lit2Var(iLitCofs[c]), Vec_IntSize(vMapping2) );
1679         Vec_IntPush( vMapping2, Vec_IntSize(vLeaves2) );
1680         Vec_IntForEachEntry( vLeaves2, iTemp, k )
1681             Vec_IntPush( vMapping2, Abc_Lit2Var(iTemp) );
1682         Vec_IntPush( vMapping2, Abc_Lit2Var(iLitCofs[c]) );
1683     }
1684     iLitCofs[2]  = Vec_IntEntry(vLeaves, iVar);
1685     // derive MUX
1686     if ( iLitCofs[0] > 1 && iLitCofs[1] > 1 )
1687     {
1688         pTruthCof[0] = ABC_CONST(0xCACACACACACACACA);
1689         Vec_IntClear( vLeaves2 );
1690         Vec_IntPush( vLeaves2, iLitCofs[0] );
1691         Vec_IntPush( vLeaves2, iLitCofs[1] );
1692         Vec_IntPush( vLeaves2, iLitCofs[2] );
1693         RetValue = Kit_TruthToGia( pNew, (unsigned *)pTruthCof, Vec_IntSize(vLeaves2), vCover, vLeaves2, 0 );
1694         iTopLit = RetValue;
1695     }
1696     else
1697     {
1698         assert( iLitCofs[0] > 1 || iLitCofs[1] > 1 );
1699         // collect leaves
1700         Vec_IntClear( vLeaves2 );
1701         for ( k = 0; k < 3; k++ )
1702             if ( iLitCofs[k] > 1 )
1703                 Vec_IntPush( vLeaves2, iLitCofs[k] );
1704         assert( Vec_IntSize(vLeaves2) == 2 );
1705         // consider three possibilities
1706         if ( iLitCofs[0] == 0 )
1707             RetValue = Gia_ManAppendAnd( pNew, iLitCofs[2], iLitCofs[1] );
1708         else if ( iLitCofs[0] == 1 )
1709             RetValue = Gia_ManAppendOr( pNew, Abc_LitNot(iLitCofs[2]), iLitCofs[1] );
1710         else if ( iLitCofs[1] == 0 )
1711             RetValue = Gia_ManAppendAnd( pNew, Abc_LitNot(iLitCofs[2]), iLitCofs[0] );
1712         else if ( iLitCofs[1] == 1 )
1713             RetValue = Gia_ManAppendOr( pNew, iLitCofs[2], iLitCofs[0] );
1714         else assert( 0 );
1715         iTopLit = iLitCofs[2];
1716     }
1717     // create mapping
1718     Vec_IntSetEntry( vMapping, Abc_Lit2Var(RetValue), Vec_IntSize(vMapping2) );
1719     Vec_IntPush( vMapping2, Vec_IntSize(vLeaves2) );
1720     Vec_IntForEachEntry( vLeaves2, iTemp, k )
1721         Vec_IntPush( vMapping2, Abc_Lit2Var(iTemp) );
1722     Vec_IntPush( vMapping2, -Abc_Lit2Var(iTopLit) );
1723     RetValue = Abc_LitNotCond( RetValue, pCutBest->fCompl );
1724     return RetValue;
1725 }
Gia_ManFromIfLogicAndVars(Gia_Man_t * pNew,If_Man_t * pIfMan,If_Cut_t * pCutBest,Vec_Int_t * vLeaves,Vec_Int_t * vLeaves2,Vec_Int_t * vCover,Vec_Int_t * vMapping,Vec_Int_t * vMapping2)1726 int Gia_ManFromIfLogicAndVars( Gia_Man_t * pNew, If_Man_t * pIfMan, If_Cut_t * pCutBest, Vec_Int_t * vLeaves, Vec_Int_t * vLeaves2, Vec_Int_t * vCover, Vec_Int_t * vMapping, Vec_Int_t * vMapping2 )
1727 {
1728     word pFunc[64], uTruth[2];
1729     int nLeaves = pCutBest->nLeaves;
1730     int truthId = Abc_Lit2Var(pCutBest->iCutFunc);
1731     int c, k, Mask = Vec_IntEntry(pIfMan->vTtDecs[nLeaves], truthId);
1732     int MaskOne[2] = { Mask & 0xFFFF, (Mask >> 16) & 0x3FFF };
1733     int iLitCofs[2], iTemp, fOrDec = (Mask >> 30) & 1, RetValue = -1;
1734     assert( Mask > 0 && nLeaves <= 2 * (pIfMan->pPars->nLutSize/2) && pIfMan->pPars->nLutSize <= 13 );
1735     Abc_TtCopy( pFunc, If_CutTruthWR(pIfMan, pCutBest), pIfMan->nTruth6Words[nLeaves], fOrDec );
1736     Abc_TtDeriveBiDec( pFunc, nLeaves, MaskOne[0], MaskOne[1], pIfMan->pPars->nLutSize/2, &uTruth[0], &uTruth[1] );
1737     uTruth[0] = fOrDec ? ~uTruth[0] : uTruth[0];
1738     uTruth[1] = fOrDec ? ~uTruth[1] : uTruth[1];
1739     for ( c = 0; c < 2; c++ )
1740     {
1741         Vec_IntClear( vLeaves2 );
1742         for ( k = 0; k < nLeaves; k++ )
1743             if ( (MaskOne[c] >> k) & 1 )
1744                 Vec_IntPush( vLeaves2, Vec_IntEntry(vLeaves, k) );
1745         assert( Vec_IntSize(vLeaves2) >= 1 );
1746         iLitCofs[c] = Kit_TruthToGia( pNew, (unsigned *)&uTruth[c], Vec_IntSize(vLeaves2), vCover, vLeaves2, 0 );
1747         if ( Vec_IntSize(vLeaves2) == 1 )
1748             continue;
1749         // create mapping
1750         assert( Gia_ObjIsAnd(Gia_ManObj(pNew, Abc_Lit2Var(iLitCofs[c]))) );
1751         Vec_IntSetEntry( vMapping, Abc_Lit2Var(iLitCofs[c]), Vec_IntSize(vMapping2) );
1752         Vec_IntPush( vMapping2, Vec_IntSize(vLeaves2) );
1753         Vec_IntForEachEntry( vLeaves2, iTemp, k )
1754             Vec_IntPush( vMapping2, Abc_Lit2Var(iTemp) );
1755         Vec_IntPush( vMapping2, Abc_Lit2Var(iLitCofs[c]) );
1756     }
1757     iLitCofs[0] = Abc_LitNotCond( iLitCofs[0], fOrDec );
1758     iLitCofs[1] = Abc_LitNotCond( iLitCofs[1], fOrDec );
1759     RetValue = Gia_ManAppendAnd( pNew, iLitCofs[0], iLitCofs[1] );
1760     RetValue = Abc_LitNotCond( RetValue, fOrDec ^ Abc_LitIsCompl(pCutBest->iCutFunc) );
1761     // create mapping
1762     Vec_IntSetEntry( vMapping, Abc_Lit2Var(RetValue), Vec_IntSize(vMapping2) );
1763     Vec_IntPush( vMapping2, 2 );
1764     Vec_IntPush( vMapping2, Abc_Lit2Var(iLitCofs[0]) );
1765     Vec_IntPush( vMapping2, Abc_Lit2Var(iLitCofs[1]) );
1766     Vec_IntPush( vMapping2, -Abc_Lit2Var(RetValue) );
1767     RetValue = Abc_LitNotCond( RetValue, pCutBest->fCompl );
1768     return RetValue;
1769 }
1770 
1771 /**Function*************************************************************
1772 
1773   Synopsis    [Converts IF into GIA manager.]
1774 
1775   Description []
1776 
1777   SideEffects []
1778 
1779   SeeAlso     []
1780 
1781 ***********************************************************************/
Gia_ManFromIfLogic(If_Man_t * pIfMan)1782 Gia_Man_t * Gia_ManFromIfLogic( If_Man_t * pIfMan )
1783 {
1784     int fWriteConfigs = 1;
1785     Gia_Man_t * pNew, * pHashed = NULL;
1786     If_Cut_t * pCutBest;
1787     If_Obj_t * pIfObj, * pIfLeaf;
1788     Vec_Int_t * vMapping, * vMapping2, * vPacking = NULL, * vConfigs = NULL;
1789     Vec_Int_t * vLeaves, * vLeaves2, * vCover, * vLits;
1790     Vec_Str_t * vConfigsStr = NULL;
1791     Ifn_Ntk_t * pNtkCell = NULL;
1792     sat_solver * pSat = NULL;
1793     int i, k, Entry;
1794     assert( !pIfMan->pPars->fDeriveLuts || pIfMan->pPars->fTruth );
1795 //    if ( pIfMan->pPars->fEnableCheck07 )
1796 //        pIfMan->pPars->fDeriveLuts = 0;
1797     // start mapping and packing
1798     vMapping  = Vec_IntStart( If_ManObjNum(pIfMan) );
1799     vMapping2 = Vec_IntStart( 1 );
1800     if ( pIfMan->pPars->fDeriveLuts && (pIfMan->pPars->pLutStruct || pIfMan->pPars->fEnableCheck75 || pIfMan->pPars->fEnableCheck75u || pIfMan->pPars->fEnableCheck07) )
1801     {
1802         vPacking = Vec_IntAlloc( 1000 );
1803         Vec_IntPush( vPacking, 0 );
1804     }
1805     if ( pIfMan->pPars->fUseDsdTune )
1806     {
1807         int nTtBitNum = If_DsdManTtBitNum(pIfMan->pIfDsdMan);
1808         int nPermBitNum = If_DsdManPermBitNum(pIfMan->pIfDsdMan);
1809         int nConfigInts = Abc_BitWordNum(nTtBitNum + nPermBitNum + 1);
1810         vConfigs = Vec_IntAlloc( 1000 );
1811         Vec_IntPush( vConfigs, 0 );
1812         Vec_IntPush( vConfigs, nConfigInts );
1813         if ( fWriteConfigs )
1814             vConfigsStr = Vec_StrAlloc( 1000 );
1815     }
1816     // create new manager
1817     pNew = Gia_ManStart( If_ManObjNum(pIfMan) );
1818     // iterate through nodes used in the mapping
1819     vLits    = Vec_IntAlloc( 1000 );
1820     vCover   = Vec_IntAlloc( 1 << 16 );
1821     vLeaves  = Vec_IntAlloc( 16 );
1822     vLeaves2 = Vec_IntAlloc( 16 );
1823     If_ManCleanCutData( pIfMan );
1824     If_ManForEachObj( pIfMan, pIfObj, i )
1825     {
1826         if ( pIfObj->nRefs == 0 && !If_ObjIsTerm(pIfObj) )
1827             continue;
1828         if ( If_ObjIsAnd(pIfObj) )
1829         {
1830             pCutBest = If_ObjCutBest( pIfObj );
1831             // perform sorting of cut leaves by delay, so that the slowest pin drives the fastest input of the LUT
1832             if ( !pIfMan->pPars->fUseTtPerm && !pIfMan->pPars->fDelayOpt && !pIfMan->pPars->fDelayOptLut && !pIfMan->pPars->fDsdBalance &&
1833                  !pIfMan->pPars->pLutStruct && !pIfMan->pPars->fUserRecLib && !pIfMan->pPars->fUserSesLib && !pIfMan->pPars->nGateSize &&
1834                  !pIfMan->pPars->fEnableCheck75 && !pIfMan->pPars->fEnableCheck75u && !pIfMan->pPars->fEnableCheck07 && !pIfMan->pPars->fUseDsdTune &&
1835                  !pIfMan->pPars->fUseCofVars && !pIfMan->pPars->fUseAndVars )
1836                 If_CutRotatePins( pIfMan, pCutBest );
1837             // collect leaves of the best cut
1838             Vec_IntClear( vLeaves );
1839             If_CutForEachLeaf( pIfMan, pCutBest, pIfLeaf, k )
1840                 Vec_IntPush( vLeaves, pIfLeaf->iCopy );
1841             // perform one of the two types of mapping: with and without structures
1842             if ( pIfMan->pPars->fUseDsd && pIfMan->pPars->pLutStruct )
1843             {
1844                 if ( pSat == NULL )
1845                     pSat = (sat_solver *)If_ManSatBuildXY( (int)(pIfMan->pPars->pLutStruct[0] - '0') );
1846                 if ( pIfMan->pPars->pLutStruct && pIfMan->pPars->fDeriveLuts )
1847                     pIfObj->iCopy = Gia_ManFromIfLogicFindLut( pIfMan, pNew, pCutBest, pSat, vLeaves, vLits, vCover, vMapping, vMapping2, vPacking );
1848                 else
1849                     pIfObj->iCopy = Gia_ManFromIfLogicCreateLut( pNew, If_CutTruthW(pIfMan, pCutBest), vLeaves, vCover, vMapping, vMapping2 );
1850                 pIfObj->iCopy = Abc_LitNotCond( pIfObj->iCopy, pCutBest->fCompl );
1851             }
1852 /*
1853             else if ( pIfMan->pPars->fUseDsd && pIfMan->pPars->fUseDsdTune && pIfMan->pPars->fDeriveLuts )
1854             {
1855                 if ( pNtkCell == NULL )
1856                 {
1857                     assert( If_DsdManGetCellStr(pIfMan->pIfDsdMan) != NULL );
1858                     pNtkCell = Ifn_NtkParse( If_DsdManGetCellStr(pIfMan->pIfDsdMan) );
1859                     nLutMax = Ifn_NtkLutSizeMax( pNtkCell );
1860                     pHashed = Gia_ManStart( 10000 );
1861                     Vec_IntFillExtra( &pHashed->vCopies, 10000, -1 );
1862                     for ( k = 0; k < pIfMan->pPars->nLutSize; k++ )
1863                         Gia_ManAppendCi( pHashed );
1864                     Gia_ManHashAlloc( pHashed );
1865                 }
1866                 pIfObj->iCopy = Gia_ManFromIfLogicFindCell( pIfMan, pNew, pHashed, pCutBest, pNtkCell, nLutMax, vLeaves, vLits, vCover, vMapping, vMapping2, vConfigs );
1867                 pIfObj->iCopy = Abc_LitNotCond( pIfObj->iCopy, pCutBest->fCompl );
1868             }
1869 */
1870             else if ( pIfMan->pPars->fUseAndVars && pIfMan->pPars->fUseCofVars && pIfMan->pPars->fDeriveLuts && (int)pCutBest->nLeaves > pIfMan->pPars->nLutSize/2 )
1871             {
1872                 int truthId = Abc_Lit2Var(pCutBest->iCutFunc);
1873                 int Mask = Vec_IntEntry(pIfMan->vTtDecs[pCutBest->nLeaves], truthId);
1874                 if ( Mask )
1875                     pIfObj->iCopy = Gia_ManFromIfLogicAndVars( pNew, pIfMan, pCutBest, vLeaves, vLeaves2, vCover, vMapping, vMapping2 );
1876                 else
1877                     pIfObj->iCopy = Gia_ManFromIfLogicCofVars( pNew, pIfMan, pCutBest, vLeaves, vLeaves2, vCover, vMapping, vMapping2 );
1878             }
1879             else if ( pIfMan->pPars->fUseAndVars && pIfMan->pPars->fDeriveLuts && (int)pCutBest->nLeaves > pIfMan->pPars->nLutSize/2 )
1880             {
1881                 pIfObj->iCopy = Gia_ManFromIfLogicAndVars( pNew, pIfMan, pCutBest, vLeaves, vLeaves2, vCover, vMapping, vMapping2 );
1882             }
1883             else if ( pIfMan->pPars->fUseCofVars && pIfMan->pPars->fDeriveLuts && (int)pCutBest->nLeaves > pIfMan->pPars->nLutSize/2 )
1884             {
1885                 pIfObj->iCopy = Gia_ManFromIfLogicCofVars( pNew, pIfMan, pCutBest, vLeaves, vLeaves2, vCover, vMapping, vMapping2 );
1886             }
1887             else if ( (pIfMan->pPars->fDeriveLuts && pIfMan->pPars->fTruth) || pIfMan->pPars->fUseDsd || pIfMan->pPars->fUseTtPerm || pIfMan->pPars->pFuncCell2 )
1888             {
1889                 word * pTruth = If_CutTruthW(pIfMan, pCutBest);
1890                 if ( pIfMan->pPars->fUseTtPerm )
1891                     for ( k = 0; k < (int)pCutBest->nLeaves; k++ )
1892                         if ( If_CutLeafBit(pCutBest, k) )
1893                             Abc_TtFlip( pTruth, Abc_TtWordNum(pCutBest->nLeaves), k );
1894                 // perform decomposition of the cut
1895                 pIfObj->iCopy = Gia_ManFromIfLogicNode( pIfMan, pNew, i, vLeaves, vLeaves2, pTruth, pIfMan->pPars->pLutStruct, vCover, vMapping, vMapping2, vPacking, (pIfMan->pPars->fEnableCheck75 || pIfMan->pPars->fEnableCheck75u), pIfMan->pPars->fEnableCheck07 );
1896                 pIfObj->iCopy = Abc_LitNotCond( pIfObj->iCopy, pCutBest->fCompl );
1897                 if ( vConfigs && Vec_IntSize(vLeaves) > 1 && !Gia_ObjIsCi(Gia_ManObj(pNew, Abc_Lit2Var(pIfObj->iCopy))) && pIfObj->iCopy > 1 )
1898                     Gia_ManFromIfGetConfig( vConfigs, pIfMan, pCutBest, pIfObj->iCopy, vConfigsStr );
1899             }
1900             else
1901             {
1902                 pIfObj->iCopy = Gia_ManNodeIfToGia( pNew, pIfMan, pIfObj, vLeaves, 0 );
1903                 // write mapping
1904                 Vec_IntSetEntry( vMapping, Abc_Lit2Var(pIfObj->iCopy), Vec_IntSize(vMapping2) );
1905                 Vec_IntPush( vMapping2, Vec_IntSize(vLeaves) );
1906                 Vec_IntForEachEntry( vLeaves, Entry, k )
1907                     assert( Abc_Lit2Var(Entry) < Abc_Lit2Var(pIfObj->iCopy) );
1908                 Vec_IntForEachEntry( vLeaves, Entry, k )
1909                     Vec_IntPush( vMapping2, Abc_Lit2Var(Entry)  );
1910                 Vec_IntPush( vMapping2, Abc_Lit2Var(pIfObj->iCopy) );
1911             }
1912         }
1913         else if ( If_ObjIsCi(pIfObj) )
1914             pIfObj->iCopy = Gia_ManAppendCi(pNew);
1915         else if ( If_ObjIsCo(pIfObj) )
1916             pIfObj->iCopy = Gia_ManAppendCo( pNew, Abc_LitNotCond(If_ObjFanin0(pIfObj)->iCopy, If_ObjFaninC0(pIfObj)) );
1917         else if ( If_ObjIsConst1(pIfObj) )
1918         {
1919             pIfObj->iCopy = 1;
1920             // create const LUT
1921             Vec_IntWriteEntry( vMapping, 0, Vec_IntSize(vMapping2) );
1922             Vec_IntPush( vMapping2, 0 );
1923             Vec_IntPush( vMapping2, 0 );
1924         }
1925         else assert( 0 );
1926     }
1927     Vec_IntFree( vLits );
1928     Vec_IntFree( vCover );
1929     Vec_IntFree( vLeaves );
1930     Vec_IntFree( vLeaves2 );
1931     if ( pNtkCell )
1932         ABC_FREE( pNtkCell );
1933     if ( pSat )
1934         sat_solver_delete(pSat);
1935     if ( pHashed )
1936         Gia_ManStop( pHashed );
1937 //    printf( "Mapping array size:  IfMan = %d. Gia = %d. Increase = %.2f\n",
1938 //        If_ManObjNum(pIfMan), Gia_ManObjNum(pNew), 1.0 * Gia_ManObjNum(pNew) / If_ManObjNum(pIfMan) );
1939     // finish mapping
1940     if ( Vec_IntSize(vMapping) > Gia_ManObjNum(pNew) )
1941         Vec_IntShrink( vMapping, Gia_ManObjNum(pNew) );
1942     else
1943         Vec_IntFillExtra( vMapping, Gia_ManObjNum(pNew), 0 );
1944     assert( Vec_IntSize(vMapping) == Gia_ManObjNum(pNew) );
1945     Vec_IntForEachEntry( vMapping, Entry, i )
1946         if ( Entry > 0 )
1947             Vec_IntAddToEntry( vMapping, i, Gia_ManObjNum(pNew) );
1948     Vec_IntAppend( vMapping, vMapping2 );
1949     Vec_IntFree( vMapping2 );
1950     // attach mapping and packing
1951     assert( pNew->vMapping == NULL );
1952     assert( pNew->vPacking == NULL );
1953     assert( pNew->vConfigs == NULL );
1954     assert( pNew->pCellStr == NULL );
1955     pNew->vMapping = vMapping;
1956     pNew->vPacking = vPacking;
1957     pNew->vConfigs = vConfigs;
1958     pNew->pCellStr = vConfigs ? Abc_UtilStrsav( If_DsdManGetCellStr(pIfMan->pIfDsdMan) ) : NULL;
1959     assert( !vConfigs || Vec_IntSize(vConfigs) == 2 + Vec_IntEntry(vConfigs, 0) * Vec_IntEntry(vConfigs, 1) );
1960     // verify that COs have mapping
1961     {
1962         Gia_Obj_t * pObj;
1963         Gia_ManForEachCo( pNew, pObj, i )
1964            assert( !Gia_ObjIsAnd(Gia_ObjFanin0(pObj)) || Gia_ObjIsLut(pNew, Gia_ObjFaninId0p(pNew, pObj)) );
1965     }
1966     // verify that internal nodes have mapping
1967     {
1968         Gia_Obj_t * pFanin;
1969         Gia_ManForEachLut( pNew, i )
1970             Gia_LutForEachFaninObj( pNew, i, pFanin, k )
1971                 assert( !Gia_ObjIsAnd(pFanin) || Gia_ObjIsLut(pNew, Gia_ObjId(pNew, pFanin)) );
1972     }
1973     // verify that CIs have no mapping
1974     {
1975         Gia_Obj_t * pObj;
1976         Gia_ManForEachCi( pNew, pObj, i )
1977            assert( !Gia_ObjIsLut(pNew, Gia_ObjId(pNew, pObj)) );
1978     }
1979     // dump configuration strings
1980     if ( vConfigsStr )
1981     {
1982         FILE * pFile; int status;
1983         char * pStr, Buffer[1000] = {0};
1984         const char * pNameGen = pIfMan->pName? Extra_FileNameGeneric( pIfMan->pName ) : "nameless_";
1985         sprintf( Buffer, "%s_configs.txt", pNameGen );
1986         ABC_FREE( pNameGen );
1987         pFile = fopen( Buffer, "wb" );
1988         if ( pFile == NULL )
1989         {
1990             printf( "Cannot open file \"%s\".\n", Buffer );
1991             return pNew;
1992         }
1993         Vec_StrPush( vConfigsStr, '\0' );
1994         pStr = Vec_StrArray(vConfigsStr);
1995         status = fwrite( pStr, strlen(pStr), 1, pFile );
1996         Vec_StrFree( vConfigsStr );
1997         fclose( pFile );
1998         printf( "Finished dumping configs into file \"%s\".\n", Buffer );
1999     }
2000     return pNew;
2001 }
2002 
2003 
2004 /**Function*************************************************************
2005 
2006   Synopsis    [Verifies mapping.]
2007 
2008   Description []
2009 
2010   SideEffects []
2011 
2012   SeeAlso     []
2013 
2014 ***********************************************************************/
Gia_ManMappingVerify_rec(Gia_Man_t * p,Gia_Obj_t * pObj)2015 int Gia_ManMappingVerify_rec( Gia_Man_t * p, Gia_Obj_t * pObj )
2016 {
2017     int Id, iFan, k, Result = 1;
2018     if ( Gia_ObjIsTravIdCurrent(p, pObj) )
2019         return 1;
2020     Gia_ObjSetTravIdCurrent(p, pObj);
2021     if ( !Gia_ObjIsAndNotBuf(pObj) )
2022         return 1;
2023     if ( !Gia_ObjIsLut(p, Gia_ObjId(p, pObj)) )
2024     {
2025         Abc_Print( -1, "Gia_ManMappingVerify: Internal node %d does not have mapping.\n", Gia_ObjId(p, pObj) );
2026         return 0;
2027     }
2028     Id = Gia_ObjId(p, pObj);
2029     Gia_LutForEachFanin( p, Id, iFan, k )
2030         if ( Result )
2031             Result &= Gia_ManMappingVerify_rec( p, Gia_ManObj(p, iFan) );
2032     return Result;
2033 }
Gia_ManMappingVerify(Gia_Man_t * p)2034 void Gia_ManMappingVerify( Gia_Man_t * p )
2035 {
2036     Gia_Obj_t * pObj, * pFanin;
2037     int i, Result = 1;
2038     assert( Gia_ManHasMapping(p) );
2039     Gia_ManIncrementTravId( p );
2040     Gia_ManForEachBuf( p, pObj, i )
2041     {
2042         pFanin = Gia_ObjFanin0(pObj);
2043         if ( !Gia_ObjIsAndNotBuf(pFanin) )
2044             continue;
2045         if ( !Gia_ObjIsLut(p, Gia_ObjId(p, pFanin)) )
2046         {
2047             Abc_Print( -1, "Gia_ManMappingVerify: CO driver %d does not have mapping.\n", Gia_ObjId(p, pFanin) );
2048             Result = 0;
2049             continue;
2050         }
2051         Result &= Gia_ManMappingVerify_rec( p, pFanin );
2052     }
2053     Gia_ManForEachCo( p, pObj, i )
2054     {
2055         pFanin = Gia_ObjFanin0(pObj);
2056         if ( !Gia_ObjIsAndNotBuf(pFanin) )
2057             continue;
2058         if ( !Gia_ObjIsLut(p, Gia_ObjId(p, pFanin)) )
2059         {
2060             Abc_Print( -1, "Gia_ManMappingVerify: CO driver %d does not have mapping.\n", Gia_ObjId(p, pFanin) );
2061             Result = 0;
2062             continue;
2063         }
2064         Result &= Gia_ManMappingVerify_rec( p, pFanin );
2065     }
2066 //    if ( Result && Gia_NtkIsRoot(p) )
2067 //        Abc_Print( 1, "Mapping verified correctly.\n" );
2068 }
2069 
2070 
2071 /**Function*************************************************************
2072 
2073   Synopsis    [Transfers mapping from hie GIA to normalized GIA.]
2074 
2075   Description [Hie GIA (pGia) points to normalized GIA (p).]
2076 
2077   SideEffects []
2078 
2079   SeeAlso     []
2080 
2081 ***********************************************************************/
Gia_ManTransferMapping(Gia_Man_t * p,Gia_Man_t * pGia)2082 void Gia_ManTransferMapping( Gia_Man_t * p, Gia_Man_t * pGia )
2083 {
2084     Gia_Obj_t * pObj;
2085     int i, k, iFan, iPlace;
2086     if ( !Gia_ManHasMapping(pGia) )
2087         return;
2088     Gia_ManMappingVerify( pGia );
2089     Vec_IntFreeP( &p->vMapping );
2090     p->vMapping = Vec_IntAlloc( 2 * Gia_ManObjNum(p) );
2091     Vec_IntFill( p->vMapping, Gia_ManObjNum(p), 0 );
2092     Gia_ManForEachLut( pGia, i )
2093     {
2094         if ( Gia_ObjValue(Gia_ManObj(pGia, i)) == ~0 ) // handle dangling LUT
2095             continue;
2096         assert( !Abc_LitIsCompl(Gia_ObjValue(Gia_ManObj(pGia, i))) );
2097         pObj = Gia_ManObj( p, Abc_Lit2Var(Gia_ObjValue(Gia_ManObj(pGia, i))) );
2098         Vec_IntWriteEntry( p->vMapping, Gia_ObjId(p, pObj), Vec_IntSize(p->vMapping) );
2099         iPlace = Vec_IntSize( p->vMapping );
2100         Vec_IntPush( p->vMapping, Gia_ObjLutSize(pGia, i) );
2101         Gia_LutForEachFanin( pGia, i, iFan, k )
2102         {
2103             if ( Gia_ObjValue(Gia_ManObj(pGia, iFan)) == ~0 ) // handle dangling LUT fanin
2104                 Vec_IntAddToEntry( p->vMapping, iPlace, -1 );
2105             else
2106                 Vec_IntPush( p->vMapping, Abc_Lit2Var(Gia_ObjValue(Gia_ManObj(pGia, iFan))) );
2107         }
2108         iFan = Abc_Lit2Var( Gia_ObjValue(Gia_ManObj(pGia, Abc_AbsInt(Gia_ObjLutMuxId(pGia, i)))) );
2109         Vec_IntPush( p->vMapping, Gia_ObjLutIsMux(pGia, i) ? -iFan : iFan );
2110     }
2111     Gia_ManMappingVerify( p );
2112 }
Gia_ManTransferPacking(Gia_Man_t * p,Gia_Man_t * pGia)2113 void Gia_ManTransferPacking( Gia_Man_t * p, Gia_Man_t * pGia )
2114 {
2115     Vec_Int_t * vPackingNew;
2116     Gia_Obj_t * pObj, * pObjNew;
2117     int i, k, Entry, nEntries, nEntries2;
2118     if ( pGia->vPacking == NULL )
2119         return;
2120     nEntries = Vec_IntEntry( pGia->vPacking, 0 );
2121     nEntries2 = 0;
2122     // create new packing info
2123     vPackingNew = Vec_IntAlloc( Vec_IntSize(pGia->vPacking) );
2124     Vec_IntPush( vPackingNew, nEntries );
2125     Vec_IntForEachEntryStart( pGia->vPacking, Entry, i, 1 )
2126     {
2127         assert( Entry > 0 && Entry < 4 );
2128         Vec_IntPush( vPackingNew, Entry );
2129         i++;
2130         for ( k = 0; k < Entry; k++, i++ )
2131         {
2132             pObj = Gia_ManObj(pGia, Vec_IntEntry(pGia->vPacking, i));
2133             pObjNew = Gia_ManObj(p, Abc_Lit2Var(Gia_ObjValue(pObj)));
2134             assert( Gia_ObjIsLut(pGia, Gia_ObjId(pGia, pObj)) );
2135             assert( Gia_ObjIsLut(p, Gia_ObjId(p, pObjNew)) );
2136             Vec_IntPush( vPackingNew, Gia_ObjId(p, pObjNew) );
2137 //            printf( "%d -> %d  ", Vec_IntEntry(pGia->vPacking, i), Gia_ObjId(p, pObjNew) );
2138         }
2139         i--;
2140         nEntries2++;
2141     }
2142     assert( nEntries == nEntries2 );
2143     // attach packing info
2144     assert( p->vPacking == NULL );
2145     p->vPacking = vPackingNew;
2146 }
Gia_ManTransferTiming(Gia_Man_t * p,Gia_Man_t * pGia)2147 void Gia_ManTransferTiming( Gia_Man_t * p, Gia_Man_t * pGia )
2148 {
2149     if ( pGia->vCiArrs || pGia->vCoReqs || pGia->vCoArrs || pGia->vCoAttrs )
2150     {
2151         p->vCiArrs     = pGia->vCiArrs;     pGia->vCiArrs    = NULL;
2152         p->vCoReqs     = pGia->vCoReqs;     pGia->vCoReqs    = NULL;
2153         p->vCoArrs     = pGia->vCoArrs;     pGia->vCoArrs    = NULL;
2154         p->vCoAttrs    = pGia->vCoAttrs;    pGia->vCoAttrs   = NULL;
2155         p->And2Delay   = pGia->And2Delay;
2156     }
2157     if ( pGia->vInArrs || pGia->vOutReqs )
2158     {
2159         p->vInArrs     = pGia->vInArrs;     pGia->vInArrs     = NULL;
2160         p->vOutReqs    = pGia->vOutReqs;    pGia->vOutReqs    = NULL;
2161         p->DefInArrs   = pGia->DefInArrs;
2162         p->DefOutReqs  = pGia->DefOutReqs;
2163         p->And2Delay   = pGia->And2Delay;
2164     }
2165     if ( pGia->vNamesIn || pGia->vNamesOut )
2166     {
2167         p->vNamesIn     = pGia->vNamesIn;     pGia->vNamesIn     = NULL;
2168         p->vNamesOut    = pGia->vNamesOut;    pGia->vNamesOut    = NULL;
2169     }
2170     if ( pGia->vConfigs || pGia->pCellStr )
2171     {
2172         p->vConfigs     = pGia->vConfigs;     pGia->vConfigs     = NULL;
2173         p->pCellStr     = pGia->pCellStr;     pGia->pCellStr     = NULL;
2174     }
2175     if ( pGia->pManTime == NULL || p == pGia )
2176         return;
2177     p->pManTime    = pGia->pManTime;    pGia->pManTime    = NULL;
2178     p->pAigExtra   = pGia->pAigExtra;   pGia->pAigExtra   = NULL;
2179     p->vRegClasses = pGia->vRegClasses; pGia->vRegClasses = NULL;
2180     p->vRegInits   = pGia->vRegInits;   pGia->vRegInits = NULL;
2181     p->nAnd2Delay  = pGia->nAnd2Delay;  pGia->nAnd2Delay  = 0;
2182 }
2183 
2184 /**Function*************************************************************
2185 
2186   Synopsis    []
2187 
2188   Description []
2189 
2190   SideEffects []
2191 
2192   SeeAlso     []
2193 
2194 ***********************************************************************/
Abc_FrameMiniAigSetCiArrivals(Abc_Frame_t * pAbc,int * pArrivals)2195 void Abc_FrameMiniAigSetCiArrivals( Abc_Frame_t * pAbc, int * pArrivals )
2196 {
2197     Gia_Man_t * pGia;
2198     if ( pArrivals == NULL )
2199         { printf( "Arrival times are not given.\n" ); return; }
2200     if ( pAbc == NULL )
2201         { printf( "ABC framework is not initialized by calling Abc_Start().\n" ); return; }
2202     pGia = Abc_FrameReadGia( pAbc );
2203     if ( pGia == NULL )
2204         { printf( "Current network in ABC framework is not defined.\n" ); return; }
2205     Vec_IntFreeP( &pGia->vCiArrs );
2206     pGia->vCiArrs = Vec_IntAllocArrayCopy( pArrivals, Gia_ManCiNum(pGia) );
2207 }
Abc_FrameMiniAigSetCoRequireds(Abc_Frame_t * pAbc,int * pRequireds)2208 void Abc_FrameMiniAigSetCoRequireds( Abc_Frame_t * pAbc, int * pRequireds )
2209 {
2210     Gia_Man_t * pGia;
2211     if ( pRequireds == NULL )
2212         { printf( "Required times are not given.\n" ); return; }
2213     if ( pAbc == NULL )
2214         { printf( "ABC framework is not initialized by calling Abc_Start().\n" ); return; }
2215     pGia = Abc_FrameReadGia( pAbc );
2216     if ( pGia == NULL )
2217         { printf( "Current network in ABC framework is not defined.\n" ); return; }
2218     Vec_IntFreeP( &pGia->vCoReqs );
2219     pGia->vCoReqs = Vec_IntAllocArrayCopy( pRequireds, Gia_ManCoNum(pGia) );
2220 }
Abc_FrameMiniAigReadCoArrivals(Abc_Frame_t * pAbc)2221 int * Abc_FrameMiniAigReadCoArrivals( Abc_Frame_t * pAbc )
2222 {
2223     Vec_Int_t * vArrs; int * pArrs;
2224     Gia_Man_t * pGia;
2225     if ( pAbc == NULL )
2226         { printf( "ABC framework is not initialized by calling Abc_Start()\n" ); return NULL; }
2227     pGia = Abc_FrameReadGia( pAbc );
2228     if ( pGia == NULL )
2229         { printf( "Current network in ABC framework is not defined.\n" ); return NULL; }
2230     if ( pGia->vCoArrs == NULL )
2231         { printf( "Current network in ABC framework has no CO arrival times.\n" ); return NULL; }
2232     vArrs = Vec_IntDup( pGia->vCoArrs );
2233     pArrs = Vec_IntReleaseArray( vArrs );
2234     Vec_IntFree( vArrs );
2235     return pArrs;
2236 }
Abc_FrameMiniAigSetAndGateDelay(Abc_Frame_t * pAbc,int Delay)2237 void Abc_FrameMiniAigSetAndGateDelay( Abc_Frame_t * pAbc, int Delay )
2238 {
2239     Gia_Man_t * pGia;
2240     if ( pAbc == NULL )
2241         printf( "ABC framework is not initialized by calling Abc_Start()\n" );
2242     pGia = Abc_FrameReadGia( pAbc );
2243     if ( pGia == NULL )
2244         printf( "Current network in ABC framework is not defined.\n" );
2245     pGia->And2Delay = Delay;
2246 }
2247 
2248 /**Function*************************************************************
2249 
2250   Synopsis    [Interface of LUT mapping package.]
2251 
2252   Description []
2253 
2254   SideEffects []
2255 
2256   SeeAlso     []
2257 
2258 ***********************************************************************/
Gia_ManPerformMappingInt(Gia_Man_t * p,If_Par_t * pPars)2259 Gia_Man_t * Gia_ManPerformMappingInt( Gia_Man_t * p, If_Par_t * pPars )
2260 {
2261     extern void Gia_ManIffTest( Gia_Man_t * pGia, If_LibLut_t * pLib, int fVerbose );
2262     Gia_Man_t * pNew;
2263     If_Man_t * pIfMan; int i, Entry;//, Id, EntryF;
2264     assert( pPars->pTimesArr == NULL );
2265     assert( pPars->pTimesReq == NULL );
2266     if ( p->vCiArrs )
2267     {
2268         assert( Vec_IntSize(p->vCiArrs) == Gia_ManCiNum(p) );
2269         pPars->pTimesArr = ABC_CALLOC( float, Gia_ManCiNum(p) );
2270         Vec_IntForEachEntry( p->vCiArrs, Entry, i )
2271             pPars->pTimesArr[i] = (float)Entry;
2272     }
2273 /*  // uncommenting this leads to a mysterious memory corruption
2274     else if ( p->vInArrs )
2275     {
2276         assert( Vec_FltSize(p->vInArrs) == Gia_ManCiNum(p) );
2277         pPars->pTimesArr = ABC_CALLOC( float, Gia_ManCiNum(p));
2278         Gia_ManForEachCiId( p, Id, i )
2279             pPars->pTimesArr[i] = Vec_FltEntry(p->vInArrs, i);
2280     }
2281 */
2282     if ( p->vCoReqs )
2283     {
2284         assert( Vec_IntSize(p->vCoReqs) == Gia_ManCoNum(p) );
2285         pPars->pTimesReq = ABC_CALLOC( float, Gia_ManCoNum(p) );
2286         Vec_IntForEachEntry( p->vCoReqs, Entry, i )
2287             pPars->pTimesReq[i] = (float)Entry;
2288     }
2289 /*  // uncommenting this leads to a mysterious memory corruption
2290     else if ( p->vOutReqs )
2291     {
2292         assert( Vec_FltSize(p->vOutReqs) == Gia_ManCoNum(p) );
2293         pPars->pTimesReq = ABC_CALLOC( float, Gia_ManCoNum(p) );
2294         Vec_FltForEachEntry( p->vOutReqs, EntryF, i )
2295             pPars->pTimesReq[i] = EntryF;
2296     }
2297 */
2298     ABC_FREE( p->pCellStr );
2299     Vec_IntFreeP( &p->vConfigs );
2300     // disable cut minimization when GIA strucure is needed
2301     if ( !pPars->fDelayOpt && !pPars->fDelayOptLut && !pPars->fDsdBalance && !pPars->fUserRecLib && !pPars->fUserSesLib && !pPars->fDeriveLuts && !pPars->fUseDsd && !pPars->fUseTtPerm && !pPars->pFuncCell2 )
2302         pPars->fCutMin = 0;
2303     // translate into the mapper
2304     pIfMan = Gia_ManToIf( p, pPars );
2305     if ( pIfMan == NULL )
2306         return NULL;
2307     // create DSD manager
2308     if ( pPars->fUseDsd )
2309     {
2310         If_DsdMan_t * p = (If_DsdMan_t *)Abc_FrameReadManDsd();
2311         assert( pPars->nLutSize <= If_DsdManVarNum(p) );
2312         assert( (pPars->pLutStruct == NULL && If_DsdManLutSize(p) == 0) || (pPars->pLutStruct && pPars->pLutStruct[0] - '0' == If_DsdManLutSize(p)) );
2313         pIfMan->pIfDsdMan = (If_DsdMan_t *)Abc_FrameReadManDsd();
2314         if ( pPars->fDsdBalance )
2315             If_DsdManAllocIsops( pIfMan->pIfDsdMan, pPars->nLutSize );
2316     }
2317     // compute switching for the IF objects
2318     if ( pPars->fPower )
2319     {
2320         if ( p->pManTime == NULL )
2321             If_ManComputeSwitching( pIfMan );
2322         else
2323             Abc_Print( 0, "Switching activity computation for designs with boxes is disabled.\n" );
2324     }
2325     if ( pPars->pReoMan )
2326         pIfMan->pUserMan = pPars->pReoMan;
2327     if ( p->pManTime )
2328         pIfMan->pManTim = Tim_ManDup( (Tim_Man_t *)p->pManTime, pPars->fDelayOpt || pPars->fDelayOptLut || pPars->fDsdBalance || pPars->fUserRecLib || pPars->fUserSesLib );
2329 //    Tim_ManPrint( pIfMan->pManTim );
2330     if ( p->vCoAttrs )
2331     {
2332         assert( If_ManCoNum(pIfMan) == Vec_IntSize(p->vCoAttrs) );
2333         Vec_IntForEachEntry( p->vCoAttrs, Entry, i )
2334             If_ObjFanin0( If_ManCo(pIfMan, i) )->fSpec = (Entry != 0);
2335     }
2336     if ( !If_ManPerformMapping( pIfMan ) )
2337     {
2338         If_ManStop( pIfMan );
2339         return NULL;
2340     }
2341     if ( pPars->pFuncWrite )
2342         pPars->pFuncWrite( pIfMan );
2343     // transform the result of mapping into the new network
2344     if ( pIfMan->pPars->fDelayOpt || pIfMan->pPars->fDsdBalance || pIfMan->pPars->fUserRecLib || pIfMan->pPars->fUserSesLib )
2345         pNew = Gia_ManFromIfAig( pIfMan );
2346     else
2347         pNew = Gia_ManFromIfLogic( pIfMan );
2348     if ( p->vCiArrs || p->vCoReqs )
2349     {
2350         If_Obj_t * pIfObj = NULL;
2351         Vec_IntFreeP( &p->vCoArrs );
2352         p->vCoArrs = Vec_IntAlloc( Gia_ManCoNum(p) );
2353         If_ManForEachCo( pIfMan, pIfObj, i )
2354             Vec_IntPush( p->vCoArrs, (int)If_ObjArrTime(If_ObjFanin0(pIfObj)) );
2355     }
2356     If_ManStop( pIfMan );
2357     // transfer name
2358     assert( pNew->pName == NULL );
2359     pNew->pName = Abc_UtilStrsav( p->pName );
2360     pNew->pSpec = Abc_UtilStrsav( p->pSpec );
2361     Gia_ManSetRegNum( pNew, Gia_ManRegNum(p) );
2362     // print delay trace
2363     if ( pPars->fVerboseTrace )
2364     {
2365         pNew->pLutLib = pPars->pLutLib;
2366         Gia_ManDelayTraceLutPrint( pNew, 1 );
2367         pNew->pLutLib = NULL;
2368     }
2369     return pNew;
2370 }
Gia_ManPerformMapping(Gia_Man_t * p,void * pp)2371 Gia_Man_t * Gia_ManPerformMapping( Gia_Man_t * p, void * pp )
2372 {
2373     Gia_Man_t * pNew;
2374     if ( p->pManTime && Tim_ManBoxNum((Tim_Man_t*)p->pManTime) && Gia_ManIsNormalized(p) )
2375     {
2376         pNew = Gia_ManDupUnnormalize( p );
2377         if ( pNew == NULL )
2378             return NULL;
2379         Gia_ManTransferTiming( pNew, p );
2380         p = pNew;
2381         // mapping
2382         pNew = Gia_ManPerformMappingInt( p, (If_Par_t *)pp );
2383         if ( pNew != p )
2384         {
2385             Gia_ManTransferTiming( pNew, p );
2386             Gia_ManStop( p );
2387         }
2388         // normalize
2389         pNew = Gia_ManDupNormalize( p = pNew, ((If_Par_t *)pp)->fHashMapping );
2390         Gia_ManTransferMapping( pNew, p );
2391         Gia_ManTransferPacking( pNew, p );
2392         Gia_ManTransferTiming( pNew, p );
2393         Gia_ManStop( p );
2394         assert( Gia_ManIsNormalized(pNew) );
2395     }
2396     else
2397     {
2398         pNew = Gia_ManPerformMappingInt( p, (If_Par_t *)pp );
2399         Gia_ManTransferTiming( pNew, p );
2400         if ( ((If_Par_t *)pp)->fHashMapping )
2401         {
2402             pNew = Gia_ManDupHashMapping( p = pNew );
2403             Gia_ManTransferPacking( pNew, p );
2404             Gia_ManTransferTiming( pNew, p );
2405             Gia_ManStop( p );
2406         }
2407     }
2408     pNew->MappedDelay = (int)((If_Par_t *)pp)->FinalDelay;
2409     pNew->MappedArea  = (int)((If_Par_t *)pp)->FinalArea;
2410     return pNew;
2411 }
2412 
2413 /**Function*************************************************************
2414 
2415   Synopsis    [Interface of other mapping-based procedures.]
2416 
2417   Description []
2418 
2419   SideEffects []
2420 
2421   SeeAlso     []
2422 
2423 ***********************************************************************/
Gia_ManPerformSopBalance(Gia_Man_t * p,int nCutNum,int nRelaxRatio,int fVerbose)2424 Gia_Man_t * Gia_ManPerformSopBalance( Gia_Man_t * p, int nCutNum, int nRelaxRatio, int fVerbose )
2425 {
2426     Gia_Man_t * pNew;
2427     If_Man_t * pIfMan;
2428     If_Par_t Pars, * pPars = &Pars;
2429     If_ManSetDefaultPars( pPars );
2430     pPars->nCutsMax    = nCutNum;
2431     pPars->nRelaxRatio = nRelaxRatio;
2432     pPars->fVerbose    = fVerbose;
2433     pPars->nLutSize    = 6;
2434     pPars->fDelayOpt   = 1;
2435     pPars->fCutMin     = 1;
2436     pPars->fTruth      = 1;
2437     pPars->fExpRed     = 0;
2438     // perform mapping
2439     pIfMan = Gia_ManToIf( p, pPars );
2440     If_ManPerformMapping( pIfMan );
2441     pNew = Gia_ManFromIfAig( pIfMan );
2442     If_ManStop( pIfMan );
2443     Gia_ManTransferTiming( pNew, p );
2444     // transfer name
2445     assert( pNew->pName == NULL );
2446     pNew->pName = Abc_UtilStrsav( p->pName );
2447     pNew->pSpec = Abc_UtilStrsav( p->pSpec );
2448     Gia_ManSetRegNum( pNew, Gia_ManRegNum(p) );
2449     return pNew;
2450 }
Gia_ManPerformDsdBalance(Gia_Man_t * p,int nLutSize,int nCutNum,int nRelaxRatio,int fVerbose)2451 Gia_Man_t * Gia_ManPerformDsdBalance( Gia_Man_t * p, int nLutSize, int nCutNum, int nRelaxRatio, int fVerbose )
2452 {
2453     Gia_Man_t * pNew;
2454     If_Man_t * pIfMan;
2455     If_Par_t Pars, * pPars = &Pars;
2456     If_ManSetDefaultPars( pPars );
2457     pPars->nCutsMax    = nCutNum;
2458     pPars->nRelaxRatio = nRelaxRatio;
2459     pPars->fVerbose    = fVerbose;
2460     pPars->nLutSize    = nLutSize;
2461     pPars->fDsdBalance = 1;
2462     pPars->fUseDsd     = 1;
2463     pPars->fCutMin     = 1;
2464     pPars->fTruth      = 1;
2465     pPars->fExpRed     = 0;
2466     if ( Abc_FrameReadManDsd2() == NULL )
2467         Abc_FrameSetManDsd2( If_DsdManAlloc(pPars->nLutSize, 0) );
2468     // perform mapping
2469     pIfMan = Gia_ManToIf( p, pPars );
2470     pIfMan->pIfDsdMan = (If_DsdMan_t *)Abc_FrameReadManDsd2();
2471     if ( pPars->fDsdBalance )
2472         If_DsdManAllocIsops( pIfMan->pIfDsdMan, pPars->nLutSize );
2473     If_ManPerformMapping( pIfMan );
2474     pNew = Gia_ManFromIfAig( pIfMan );
2475     If_ManStop( pIfMan );
2476     Gia_ManTransferTiming( pNew, p );
2477     // transfer name
2478     assert( pNew->pName == NULL );
2479     pNew->pName = Abc_UtilStrsav( p->pName );
2480     pNew->pSpec = Abc_UtilStrsav( p->pSpec );
2481     Gia_ManSetRegNum( pNew, Gia_ManRegNum(p) );
2482     return pNew;
2483 }
2484 
2485 /**Function*************************************************************
2486 
2487   Synopsis    [Tests decomposition structures.]
2488 
2489   Description []
2490 
2491   SideEffects []
2492 
2493   SeeAlso     []
2494 
2495 ***********************************************************************/
Gia_ManTestStruct(Gia_Man_t * p)2496 void Gia_ManTestStruct( Gia_Man_t * p )
2497 {
2498     int nCutMax = 7;
2499     int LutCount[8] = {0}, LutNDecomp[8] = {0};
2500     int i, k, iFan, nFanins, Status;
2501     Vec_Int_t * vLeaves;
2502     word * pTruth;
2503 
2504     vLeaves = Vec_IntAlloc( 100 );
2505     Gia_ObjComputeTruthTableStart( p, nCutMax );
2506     Gia_ManForEachLut( p, i )
2507     {
2508         nFanins = Gia_ObjLutSize(p, i);
2509         assert( nFanins <= 7 );
2510         LutCount[Abc_MaxInt(nFanins, 5)]++;
2511         if ( nFanins <= 5 )
2512             continue;
2513         Vec_IntClear( vLeaves );
2514         Gia_LutForEachFanin( p, i, iFan, k )
2515             Vec_IntPush( vLeaves, iFan );
2516         pTruth = Gia_ObjComputeTruthTableCut( p, Gia_ManObj(p, i), vLeaves );
2517         // check if it is decomposable
2518         Status = If_CutPerformCheck07( NULL, (unsigned *)pTruth, 7, nFanins, NULL );
2519         if ( Status == 1 )
2520             continue;
2521         LutNDecomp[nFanins]++;
2522         if ( LutNDecomp[nFanins] > 10 )
2523             continue;
2524         Kit_DsdPrintFromTruth( (unsigned *)pTruth, nFanins ); printf( "\n" );
2525     }
2526     Gia_ObjComputeTruthTableStop( p );
2527 
2528     printf( "LUT5 = %d    ", LutCount[5] );
2529     printf( "LUT6 = %d  NonDec = %d (%.2f %%)    ", LutCount[6], LutNDecomp[6], 100.0 * LutNDecomp[6]/Abc_MaxInt(LutCount[6], 1) );
2530     printf( "LUT7 = %d  NonDec = %d (%.2f %%)    ", LutCount[7], LutNDecomp[7], 100.0 * LutNDecomp[7]/Abc_MaxInt(LutCount[7], 1) );
2531     printf( "\n" );
2532 }
2533 
2534 /**Function*************************************************************
2535 
2536   Synopsis    [Performs hashing for a mapped AIG.]
2537 
2538   Description []
2539 
2540   SideEffects []
2541 
2542   SeeAlso     []
2543 
2544 ***********************************************************************/
Gia_ManDupHashMapping(Gia_Man_t * p)2545 Gia_Man_t * Gia_ManDupHashMapping( Gia_Man_t * p )
2546 {
2547     Gia_Man_t * pNew;
2548     Vec_Int_t * vMapping;
2549     Gia_Obj_t * pObj, * pFanin;
2550     int i, k;
2551     assert( Gia_ManHasMapping(p) );
2552     // copy the old manager with hashing
2553     pNew = Gia_ManStart( Gia_ManObjNum(p) );
2554     pNew->pName = Abc_UtilStrsav( p->pName );
2555     pNew->pSpec = Abc_UtilStrsav( p->pSpec );
2556     Gia_ManHashAlloc( pNew );
2557     Gia_ManFillValue( p );
2558     Gia_ManConst0(p)->Value = 0;
2559     Gia_ManForEachCi( p, pObj, i )
2560         pObj->Value = Gia_ManAppendCi( pNew );
2561     Gia_ManForEachAnd( p, pObj, i )
2562         pObj->Value = Gia_ManHashAnd( pNew, Gia_ObjFanin0Copy(pObj), Gia_ObjFanin1Copy(pObj) );
2563     Gia_ManForEachCo( p, pObj, i )
2564         Gia_ManAppendCo( pNew, Gia_ObjFanin0Copy(pObj) );
2565     Gia_ManHashStop( pNew );
2566     Gia_ManSetRegNum( pNew, Gia_ManRegNum(p) );
2567     // recreate mapping
2568     vMapping = Vec_IntAlloc( Vec_IntSize(p->vMapping) );
2569     Vec_IntFill( vMapping, Gia_ManObjNum(p), 0 );
2570     Gia_ManForEachLut( p, i )
2571     {
2572         pObj = Gia_ManObj( p, i );
2573         Vec_IntWriteEntry( vMapping, Abc_Lit2Var(pObj->Value), Vec_IntSize(vMapping) );
2574         Vec_IntPush( vMapping, Gia_ObjLutSize(p, i) );
2575         Gia_LutForEachFaninObj( p, i, pFanin, k )
2576             Vec_IntPush( vMapping, Abc_Lit2Var(pFanin->Value)  );
2577         Vec_IntPush( vMapping, Abc_Lit2Var(pObj->Value) );
2578     }
2579     pNew->vMapping = vMapping;
2580     return pNew;
2581 }
2582 
2583 
2584 ////////////////////////////////////////////////////////////////////////
2585 ///                       END OF FILE                                ///
2586 ////////////////////////////////////////////////////////////////////////
2587 
2588 
2589 ABC_NAMESPACE_IMPL_END
2590 
2591