1 /**CFile****************************************************************
2 
3   FileName    [giaLf.c]
4 
5   SystemName  [ABC: Logic synthesis and verification system.]
6 
7   PackageName [Scalable AIG package.]
8 
9   Synopsis    [Cut computation.]
10 
11   Author      [Alan Mishchenko]`
12 
13   Affiliation [UC Berkeley]
14 
15   Date        [Ver. 1.0. Started - June 20, 2005.]
16 
17   Revision    [$Id: giaLf.c,v 1.00 2005/06/20 00:00:00 alanmi Exp $]
18 
19 ***********************************************************************/
20 
21 #include "gia.h"
22 #include "misc/tim/tim.h"
23 #include "misc/vec/vecSet.h"
24 #include "misc/vec/vecMem.h"
25 #include "misc/util/utilTruth.h"
26 
27 ABC_NAMESPACE_IMPL_START
28 
29 ////////////////////////////////////////////////////////////////////////
30 ///                        DECLARATIONS                              ///
31 ////////////////////////////////////////////////////////////////////////
32 
33 #define LF_LEAF_MAX   13
34 #define LF_CUT_MAX    32
35 #define LF_LOG_PAGE   12
36 #define LF_NO_LEAF   255
37 #define LF_CUT_WORDS (4+LF_LEAF_MAX/2)
38 #define LF_TT_WORDS  ((LF_LEAF_MAX > 6) ? 1 << (LF_LEAF_MAX-6) : 1)
39 #define LF_EPSILON 0.005
40 
41 typedef struct Lf_Cut_t_ Lf_Cut_t;
42 struct Lf_Cut_t_
43 {
44     word            Sign;            // signature
45     int             Delay;           // delay
46     float           Flow;            // flow
47     int             iFunc;           // functionality
48     unsigned        Cost      : 22;  // misc cut cost
49     unsigned        fLate     :  1;  // fails timing
50     unsigned        fMux7     :  1;  // specialized cut
51     unsigned        nLeaves   :  8;  // the number of leaves
52     int             pLeaves[0];      // leaves
53 };
54 typedef struct Lf_Plc_t_ Lf_Plc_t;
55 struct Lf_Plc_t_
56 {
57     unsigned        fUsed    :  1;   // the cut is used
58     unsigned        Handle   : 31;   // the cut handle
59 };
60 typedef struct Lf_Bst_t_ Lf_Bst_t;
61 struct Lf_Bst_t_
62 {
63     int             Delay[3];        // delay
64     float           Flow[3];         // flow
65     Lf_Plc_t        Cut[2];          // cut info
66 };
67 typedef struct Lf_Mem_t_ Lf_Mem_t;
68 struct Lf_Mem_t_
69 {
70     int             LogPage;         // log size of memory page
71     int             MaskPage;        // page mask
72     int             nCutWords;       // cut size in words
73     int             iCur;            // writing position
74     Vec_Ptr_t       vPages;          // memory pages
75     Vec_Ptr_t *     vFree;           // free pages
76 };
77 typedef struct Lf_Man_t_ Lf_Man_t;
78 struct Lf_Man_t_
79 {
80     // user data
81     Gia_Man_t *     pGia;            // manager
82     Jf_Par_t *      pPars;           // parameters
83     // cut data
84     int             nCutWords;       // cut size in words
85     int             nSetWords;       // set size in words
86     Lf_Bst_t *      pObjBests;       // best cuts
87     Vec_Ptr_t       vMemSets;        // memory for cutsets
88     Vec_Int_t       vFreeSets;       // free cutsets
89     Vec_Mem_t *     vTtMem;          // truth tables
90     Vec_Ptr_t       vFreePages;      // free memory pages
91     Lf_Mem_t        vStoreOld;       // previous cuts
92     Lf_Mem_t        vStoreNew;       // current cuts
93     // mapper data
94     Vec_Int_t       vOffsets;        // offsets
95     Vec_Int_t       vRequired;       // required times
96     Vec_Int_t       vCutSets;        // cutsets (pObj->Value stores cut refs)
97     Vec_Flt_t       vFlowRefs;       // flow refs
98     Vec_Int_t       vMapRefs;        // mapping refs
99     Vec_Flt_t       vSwitches;       // switching activity
100     Vec_Int_t       vCiArrivals;     // arrival times of the CIs
101     // statistics
102     abctime         clkStart;        // starting time
103     double          CutCount[4];     // cut counts
104     double          Switches;        // switching activity
105     int             nFrontMax;       // frontier
106     int             nCoDrivers;      // CO drivers
107     int             nInverters;      // inverters
108     int             nTimeFails;      // timing fails
109     int             Iter;            // mapping iteration
110     int             fUseEla;         // use exact local area
111     int             nCutMux;         // non-trivial MUX cuts
112     int             nCutEqual;       // equal two cuts
113     int             nCutCounts[LF_LEAF_MAX+1];
114 };
115 
Lf_CutCopy(Lf_Cut_t * p,Lf_Cut_t * q,int n)116 static inline void        Lf_CutCopy( Lf_Cut_t * p, Lf_Cut_t * q, int n ) { memcpy(p, q, sizeof(word) * n);                                         }
Lf_CutNext(Lf_Cut_t * p,int n)117 static inline Lf_Cut_t *  Lf_CutNext( Lf_Cut_t * p, int n )               { return (Lf_Cut_t *)((word *)p + n);                                     }
Lf_CutTruth(Lf_Man_t * p,Lf_Cut_t * pCut)118 static inline word *      Lf_CutTruth( Lf_Man_t * p, Lf_Cut_t * pCut )    { return Vec_MemReadEntry(p->vTtMem, Abc_Lit2Var(pCut->iFunc));           }
119 
Lf_ObjOff(Lf_Man_t * p,int i)120 static inline int         Lf_ObjOff( Lf_Man_t * p, int i )                { return Vec_IntEntry(&p->vOffsets, i);                                   }
Lf_ObjRequired(Lf_Man_t * p,int i)121 static inline int         Lf_ObjRequired( Lf_Man_t * p, int i )           { return Vec_IntEntry(&p->vRequired, i);                                  }
Lf_ObjSetRequired(Lf_Man_t * p,int i,int t)122 static inline void        Lf_ObjSetRequired( Lf_Man_t * p, int i, int t ) { Vec_IntDowndateEntry(&p->vRequired, i, t);                              }
Lf_ObjReadBest(Lf_Man_t * p,int i)123 static inline Lf_Bst_t *  Lf_ObjReadBest( Lf_Man_t * p, int i )           { return p->pObjBests + Lf_ObjOff(p,i);                                   }
Lf_ObjFlowRefs(Lf_Man_t * p,int i)124 static inline float       Lf_ObjFlowRefs( Lf_Man_t * p, int i )           { return Vec_FltEntry(&p->vFlowRefs, Lf_ObjOff(p,i));                     }
Lf_ObjMapRefNum(Lf_Man_t * p,int i)125 static inline int         Lf_ObjMapRefNum( Lf_Man_t * p, int i )          { return Vec_IntEntry(&p->vMapRefs, Lf_ObjOff(p,i));                      }
Lf_ObjMapRefInc(Lf_Man_t * p,int i)126 static inline int         Lf_ObjMapRefInc( Lf_Man_t * p, int i )          { return (*Vec_IntEntryP(&p->vMapRefs, Lf_ObjOff(p,i)))++;                }
Lf_ObjMapRefDec(Lf_Man_t * p,int i)127 static inline int         Lf_ObjMapRefDec( Lf_Man_t * p, int i )          { return --(*Vec_IntEntryP(&p->vMapRefs, Lf_ObjOff(p,i)));                }
Lf_ObjSwitches(Lf_Man_t * p,int i)128 static inline float       Lf_ObjSwitches( Lf_Man_t * p, int i )           { return Vec_FltEntry(&p->vSwitches, i);                                  }
Lf_BestDiffCuts(Lf_Bst_t * p)129 static inline int         Lf_BestDiffCuts( Lf_Bst_t * p )                 { return p->Cut[0].Handle != p->Cut[1].Handle;                            }
Lf_BestIsMapped(Lf_Bst_t * p)130 static inline int         Lf_BestIsMapped( Lf_Bst_t * p )                 { return (int)(p->Cut[0].fUsed ^ p->Cut[1].fUsed);                        }
Lf_BestIndex(Lf_Bst_t * p)131 static inline int         Lf_BestIndex( Lf_Bst_t * p )                    { return p->Cut[1].fUsed;                                                 }
Lf_BestCutIndex(Lf_Bst_t * p)132 static inline int         Lf_BestCutIndex( Lf_Bst_t * p )                 { if (p->Cut[0].fUsed) return 0; if (p->Cut[1].fUsed) return 1; return 2; }
133 
134 #define Lf_CutSetForEachCut( nWords, pCutSet, pCut, i, nCuts )  for ( i = 0, pCut = pCutSet; i < nCuts; pCut = Lf_CutNext(pCut, nWords), i++ )
135 #define Lf_CutForEachVar( pCut, Var, i )                        for ( i = 0; i < (int)pCut->nLeaves && (Var = pCut->pLeaves[i]); i++ ) if ( Lf_ObjOff(p, Var) < 0 ) {} else
136 
137 extern int Kit_TruthToGia( Gia_Man_t * pMan, unsigned * pTruth, int nVars, Vec_Int_t * vMemory, Vec_Int_t * vLeaves, int fHash );
138 
139 ////////////////////////////////////////////////////////////////////////
140 ///                     FUNCTION DEFINITIONS                         ///
141 ////////////////////////////////////////////////////////////////////////
142 
143 /**Function*************************************************************
144 
145   Synopsis    []
146 
147   Description []
148 
149   SideEffects []
150 
151   SeeAlso     []
152 
153 ***********************************************************************/
Lf_ObjSetCiArrival(Lf_Man_t * p,int iCi,int Time)154 static inline void Lf_ObjSetCiArrival( Lf_Man_t * p, int iCi, int Time )
155 {
156     Vec_IntWriteEntry( &p->vCiArrivals, iCi, Time );
157 }
Lf_ObjCiArrival(Lf_Man_t * p,int iCi)158 static inline int Lf_ObjCiArrival( Lf_Man_t * p, int iCi )
159 {
160     return Vec_IntEntry( &p->vCiArrivals, iCi );
161 }
Lf_ObjArrival_rec(Lf_Man_t * p,Gia_Obj_t * pDriver)162 int Lf_ObjArrival_rec( Lf_Man_t * p, Gia_Obj_t * pDriver )
163 {
164     if ( Gia_ObjIsBuf(pDriver) )
165         return Lf_ObjArrival_rec( p, Gia_ObjFanin0(pDriver) );
166     if ( Gia_ObjIsAnd(pDriver) )
167         return Lf_ObjReadBest(p, Gia_ObjId(p->pGia, pDriver))->Delay[0];
168     if ( Gia_ObjIsCi(pDriver) )
169         return Lf_ObjCiArrival(p, Gia_ObjCioId(pDriver));
170     return 0;
171 }
Lf_ObjCoArrival(Lf_Man_t * p,int iCo)172 static inline int Lf_ObjCoArrival( Lf_Man_t * p, int iCo )
173 {
174     Gia_Obj_t * pObj = Gia_ManCo(p->pGia, iCo);
175     Gia_Obj_t * pDriver = Gia_ObjFanin0(pObj);
176     return Lf_ObjArrival_rec( p, pDriver );
177 //    if ( Gia_ObjIsAnd(pDriver) )
178 //        return Lf_ObjReadBest(p, Gia_ObjId(p->pGia, pDriver))->Delay[0];
179 //    if ( Gia_ObjIsCi(pDriver) )
180 //        return Lf_ObjCiArrival(p, Gia_ObjCioId(pDriver));
181 //    return 0;
182 }
Lf_ObjCoArrival2_rec(Lf_Man_t * p,Gia_Obj_t * pDriver)183 int Lf_ObjCoArrival2_rec( Lf_Man_t * p, Gia_Obj_t * pDriver )
184 {
185     if ( Gia_ObjIsBuf(pDriver) )
186         return Lf_ObjCoArrival2_rec( p, Gia_ObjFanin0(pDriver) );
187     if ( Gia_ObjIsAnd(pDriver) )
188     {
189         Lf_Bst_t * pBest = Lf_ObjReadBest(p, Gia_ObjId(p->pGia, pDriver));
190         int Index = Lf_BestCutIndex( pBest );
191         assert( Index < 2 || Gia_ObjIsMux(p->pGia, pDriver) );
192         return pBest->Delay[Index];
193     }
194     if ( Gia_ObjIsCi(pDriver) )
195         return Lf_ObjCiArrival(p, Gia_ObjCioId(pDriver));
196     return 0;
197 }
Lf_ObjCoArrival2(Lf_Man_t * p,int iCo)198 static inline int Lf_ObjCoArrival2( Lf_Man_t * p, int iCo )
199 {
200     Gia_Obj_t * pObj = Gia_ManCo(p->pGia, iCo);
201     Gia_Obj_t * pDriver = Gia_ObjFanin0(pObj);
202     return Lf_ObjCoArrival2_rec( p, pDriver );
203 //    if ( Gia_ObjIsAnd(pDriver) )
204 //    {
205 //        Lf_Bst_t * pBest = Lf_ObjReadBest(p, Gia_ObjId(p->pGia, pDriver));
206 //        int Index = Lf_BestCutIndex( pBest );
207 //        assert( Index < 2 || Gia_ObjIsMux(p->pGia, pDriver) );
208 //        return pBest->Delay[Index];
209 //    }
210 //    if ( Gia_ObjIsCi(pDriver) )
211 //        return Lf_ObjCiArrival(p, Gia_ObjCioId(pDriver));
212 //    return 0;
213 }
214 
215 /**Function*************************************************************
216 
217   Synopsis    []
218 
219   Description []
220 
221   SideEffects []
222 
223   SeeAlso     []
224 
225 ***********************************************************************/
Lf_ManComputeCrossCut(Gia_Man_t * p)226 int Lf_ManComputeCrossCut( Gia_Man_t * p )
227 {
228     Gia_Obj_t * pObj;
229     int i, nCutMax = 0, nCutCur = 0;
230     assert( p->pMuxes == NULL );
231     Gia_ManForEachObj( p, pObj, i )
232         pObj->Value = 0;
233     Gia_ManForEachAnd( p, pObj, i )
234     {
235         if ( Gia_ObjIsAnd(Gia_ObjFanin0(pObj)) )
236             Gia_ObjFanin0(pObj)->Value++;
237         if ( Gia_ObjIsAnd(Gia_ObjFanin1(pObj)) )
238             Gia_ObjFanin1(pObj)->Value++;
239     }
240     Gia_ManForEachAnd( p, pObj, i )
241     {
242         if ( pObj->Value )
243             nCutCur++;
244         if ( nCutMax < nCutCur )
245             nCutMax = nCutCur;
246         if ( Gia_ObjIsAnd(Gia_ObjFanin0(pObj)) && --Gia_ObjFanin0(pObj)->Value == 0 )
247             nCutCur--;
248         if ( Gia_ObjIsAnd(Gia_ObjFanin1(pObj)) && --Gia_ObjFanin1(pObj)->Value == 0 )
249             nCutCur--;
250     }
251     assert( nCutCur == 0 );
252     if ( nCutCur )
253         printf( "Cutset is not 0\n" );
254     Gia_ManForEachObj( p, pObj, i )
255         assert( pObj->Value == 0 );
256     printf( "CutMax = %d\n", nCutMax );
257     return nCutMax;
258 }
259 
260 /**Function*************************************************************
261 
262   Synopsis    [Detect MUX truth tables.]
263 
264   Description []
265 
266   SideEffects []
267 
268   SeeAlso     []
269 
270 ***********************************************************************/
Lf_ManTtIsMux(word t)271 int Lf_ManTtIsMux( word t )
272 {
273     static unsigned s_Muxes[24] = {
274         (~0xAAAAAAAA & ~0xCCCCCCCC) | ( 0xAAAAAAAA & ~0xF0F0F0F0),
275         (~0xAAAAAAAA & ~0xCCCCCCCC) | ( 0xAAAAAAAA &  0xF0F0F0F0),
276         (~0xAAAAAAAA &  0xCCCCCCCC) | ( 0xAAAAAAAA & ~0xF0F0F0F0),
277         (~0xAAAAAAAA &  0xCCCCCCCC) | ( 0xAAAAAAAA &  0xF0F0F0F0),
278         ( 0xAAAAAAAA & ~0xCCCCCCCC) | (~0xAAAAAAAA & ~0xF0F0F0F0),
279         ( 0xAAAAAAAA & ~0xCCCCCCCC) | (~0xAAAAAAAA &  0xF0F0F0F0),
280         ( 0xAAAAAAAA &  0xCCCCCCCC) | (~0xAAAAAAAA & ~0xF0F0F0F0),
281         ( 0xAAAAAAAA &  0xCCCCCCCC) | (~0xAAAAAAAA &  0xF0F0F0F0),
282 
283         (~0xCCCCCCCC & ~0xAAAAAAAA) | ( 0xCCCCCCCC & ~0xF0F0F0F0),
284         (~0xCCCCCCCC & ~0xAAAAAAAA) | ( 0xCCCCCCCC &  0xF0F0F0F0),
285         (~0xCCCCCCCC &  0xAAAAAAAA) | ( 0xCCCCCCCC & ~0xF0F0F0F0),
286         (~0xCCCCCCCC &  0xAAAAAAAA) | ( 0xCCCCCCCC &  0xF0F0F0F0),
287         ( 0xCCCCCCCC & ~0xAAAAAAAA) | (~0xCCCCCCCC & ~0xF0F0F0F0),
288         ( 0xCCCCCCCC & ~0xAAAAAAAA) | (~0xCCCCCCCC &  0xF0F0F0F0),
289         ( 0xCCCCCCCC &  0xAAAAAAAA) | (~0xCCCCCCCC & ~0xF0F0F0F0),
290         ( 0xCCCCCCCC &  0xAAAAAAAA) | (~0xCCCCCCCC &  0xF0F0F0F0),
291 
292         (~0xF0F0F0F0 & ~0xCCCCCCCC) | ( 0xF0F0F0F0 & ~0xAAAAAAAA),
293         (~0xF0F0F0F0 & ~0xCCCCCCCC) | ( 0xF0F0F0F0 &  0xAAAAAAAA),
294         (~0xF0F0F0F0 &  0xCCCCCCCC) | ( 0xF0F0F0F0 & ~0xAAAAAAAA),
295         (~0xF0F0F0F0 &  0xCCCCCCCC) | ( 0xF0F0F0F0 &  0xAAAAAAAA),
296         ( 0xF0F0F0F0 & ~0xCCCCCCCC) | (~0xF0F0F0F0 & ~0xAAAAAAAA),
297         ( 0xF0F0F0F0 & ~0xCCCCCCCC) | (~0xF0F0F0F0 &  0xAAAAAAAA),
298         ( 0xF0F0F0F0 &  0xCCCCCCCC) | (~0xF0F0F0F0 & ~0xAAAAAAAA),
299         ( 0xF0F0F0F0 &  0xCCCCCCCC) | (~0xF0F0F0F0 &  0xAAAAAAAA)
300     };
301     int i;
302     for ( i = 0; i < 24; i++ )
303         if ( ((unsigned)t) == s_Muxes[i] )
304             return 1;
305     return 0;
306 }
307 
308 /**Function*************************************************************
309 
310   Synopsis    [Count the number of unique drivers and invertors.]
311 
312   Description []
313 
314   SideEffects []
315 
316   SeeAlso     []
317 
318 ***********************************************************************/
Lf_ManAnalyzeCoDrivers(Gia_Man_t * p,int * pnDrivers,int * pnInverts)319 void Lf_ManAnalyzeCoDrivers( Gia_Man_t * p, int * pnDrivers, int * pnInverts )
320 {
321     Gia_Obj_t * pObj;
322     int i, Entry, nDrivers, nInverts;
323     Vec_Int_t * vMarks = Vec_IntStart( Gia_ManObjNum(p) );
324     nDrivers = nInverts = 0;
325     Gia_ManForEachCo( p, pObj, i )
326         *Vec_IntEntryP( vMarks, Gia_ObjFaninId0p(p, pObj) ) |= Gia_ObjFaninC0(pObj) ? 2 : 1;
327     Vec_IntForEachEntry( vMarks, Entry, i )
328         nDrivers += (int)(Entry != 0), nInverts += (int)(Entry == 3);
329     Vec_IntFree( vMarks );
330     *pnDrivers = nDrivers;
331     *pnInverts = nInverts;
332 }
Lf_ManComputeSwitching(Gia_Man_t * p,Vec_Flt_t * vSwitches)333 void Lf_ManComputeSwitching( Gia_Man_t * p, Vec_Flt_t * vSwitches )
334 {
335 //    abctime clk = Abc_Clock();
336     Vec_Flt_t * vSwitching = (Vec_Flt_t *)Gia_ManComputeSwitchProbs( p, 48, 16, 0 );
337     assert( Vec_FltCap(vSwitches) == 0 );
338     *vSwitches = *vSwitching;
339     ABC_FREE( vSwitching );
340 //    Abc_PrintTime( 1, "Computing switching activity", Abc_Clock() - clk );
341 }
342 
343 /**Function*************************************************************
344 
345   Synopsis    []
346 
347   Description []
348 
349   SideEffects []
350 
351   SeeAlso     []
352 
353 ***********************************************************************/
Lf_CutCreateUnit(Lf_Cut_t * p,int i)354 static inline int Lf_CutCreateUnit( Lf_Cut_t * p, int i )
355 {
356     p->fLate = 0;
357     p->fMux7 = 0;
358     p->iFunc = 2;
359     p->nLeaves = 1;
360     p->pLeaves[0] = i;
361     p->Sign = ((word)1) << (i & 0x3F);
362     return 1;
363 }
Lf_ManFetchSet(Lf_Man_t * p,int i)364 static inline Lf_Cut_t * Lf_ManFetchSet( Lf_Man_t * p, int i )
365 {
366     int uMaskPage = (1 << LF_LOG_PAGE) - 1;
367     Gia_Obj_t * pObj = Gia_ManObj( p->pGia, i );
368     int iOffSet = Vec_IntEntry( &p->vOffsets, i );
369     int Entry = Vec_IntEntry( &p->vCutSets, iOffSet );
370     assert( Gia_ObjIsAndNotBuf(pObj) );
371     assert( pObj->Value > 0 );
372     if ( Entry == -1 ) // first visit
373     {
374         if ( Vec_IntSize(&p->vFreeSets) == 0 ) // add new
375         {
376             Lf_Cut_t * pCut = (Lf_Cut_t *)ABC_CALLOC( word, p->nSetWords * (1 << LF_LOG_PAGE) );
377             int uMaskShift = Vec_PtrSize(&p->vMemSets) << LF_LOG_PAGE;
378             Vec_PtrPush( &p->vMemSets, pCut );
379             for ( Entry = uMaskPage; Entry >= 0; Entry-- )
380             {
381                 Vec_IntPush( &p->vFreeSets, uMaskShift | Entry );
382                 pCut[Entry].nLeaves   = LF_NO_LEAF;
383             }
384         }
385         Entry = Vec_IntPop( &p->vFreeSets );
386         Vec_IntWriteEntry( &p->vCutSets, iOffSet, Entry );
387         p->nFrontMax = Abc_MaxInt( p->nFrontMax, Entry + 1 );
388     }
389     else if ( --pObj->Value == 0 )
390     {
391         Vec_IntPush( &p->vFreeSets, Entry );
392         Vec_IntWriteEntry( &p->vCutSets, iOffSet, -1 );
393     }
394     return (Lf_Cut_t *)((word *)Vec_PtrEntry(&p->vMemSets, Entry >> LF_LOG_PAGE) + p->nSetWords * (Entry & uMaskPage));
395 }
Lf_ManPrepareSet(Lf_Man_t * p,int iObj,int Index,Lf_Cut_t ** ppCutSet)396 static inline int Lf_ManPrepareSet( Lf_Man_t * p, int iObj, int Index, Lf_Cut_t ** ppCutSet )
397 {
398     static word CutTemp[3][LF_CUT_WORDS];
399     if ( Vec_IntEntry(&p->vOffsets, iObj) == -1 )
400         return Lf_CutCreateUnit( (*ppCutSet = (Lf_Cut_t *)CutTemp[Index]), iObj );
401     {
402         Lf_Cut_t * pCut;
403         int i, nCutNum = p->pPars->nCutNum;
404         *ppCutSet = Lf_ManFetchSet(p, iObj);
405         Lf_CutSetForEachCut( p->nCutWords, *ppCutSet, pCut, i, nCutNum )
406             if ( pCut->nLeaves == LF_NO_LEAF )
407                 return i;
408         return i;
409     }
410 }
411 
412 /**Function*************************************************************
413 
414   Synopsis    [Cut manipulation.]
415 
416   Description []
417 
418   SideEffects []
419 
420   SeeAlso     []
421 
422 ***********************************************************************/
Lf_CutGetSign(Lf_Cut_t * pCut)423 static inline word Lf_CutGetSign( Lf_Cut_t * pCut )
424 {
425     word Sign = 0; int i;
426     for ( i = 0; i < (int)pCut->nLeaves; i++ )
427         Sign |= ((word)1) << (pCut->pLeaves[i] & 0x3F);
428     return Sign;
429 }
Lf_CutCountBits(word i)430 static inline int Lf_CutCountBits( word i )
431 {
432     i = i - ((i >> 1) & 0x5555555555555555);
433     i = (i & 0x3333333333333333) + ((i >> 2) & 0x3333333333333333);
434     i = ((i + (i >> 4)) & 0x0F0F0F0F0F0F0F0F);
435     return (i*(0x0101010101010101))>>56;
436 }
Lf_CutEqual(Lf_Cut_t * pCut0,Lf_Cut_t * pCut1)437 static inline int Lf_CutEqual( Lf_Cut_t * pCut0, Lf_Cut_t * pCut1 )
438 {
439     int i;
440     if ( pCut0->iFunc != pCut1->iFunc )
441         return 0;
442     if ( pCut0->nLeaves != pCut1->nLeaves )
443         return 0;
444     for ( i = 0; i < (int)pCut0->nLeaves; i++ )
445         if ( pCut0->pLeaves[i] != pCut1->pLeaves[i] )
446             return 0;
447     return 1;
448 }
Lf_CutSwitches(Lf_Man_t * p,Lf_Cut_t * pCut)449 static inline float Lf_CutSwitches( Lf_Man_t * p, Lf_Cut_t * pCut )
450 {
451     float Switches = 0; int i;
452     for ( i = 0; i < (int)pCut->nLeaves; i++ )
453         Switches += Lf_ObjSwitches(p, pCut->pLeaves[i]);
454 //printf( "%.2f ", Switches );
455     return Switches;
456 }
Lf_CutPrint(Lf_Man_t * p,Lf_Cut_t * pCut)457 static inline void Lf_CutPrint( Lf_Man_t * p, Lf_Cut_t * pCut )
458 {
459     int i, nDigits = Abc_Base10Log(Gia_ManObjNum(p->pGia));
460     printf( "%d  {", pCut->nLeaves );
461     for ( i = 0; i < (int)pCut->nLeaves; i++ )
462         printf( " %*d", nDigits, pCut->pLeaves[i] );
463     for ( ; i < (int)p->pPars->nLutSize; i++ )
464         printf( " %*s", nDigits, " " );
465     printf( "  }   Late = %d  D = %4d  A = %9.4f  F = %6d\n",
466         pCut->fLate, pCut->Delay, pCut->Flow, pCut->iFunc );
467 }
Lf_CutArea(Lf_Man_t * p,Lf_Cut_t * pCut)468 static inline float Lf_CutArea( Lf_Man_t * p, Lf_Cut_t * pCut )
469 {
470     if ( pCut->nLeaves < 2 || pCut->fMux7 )
471         return 0;
472     if ( p->pPars->fPower )
473         return 1.0 * pCut->nLeaves + Lf_CutSwitches( p, pCut );
474     if ( p->pPars->fOptEdge )
475         return (pCut->nLeaves + p->pPars->nAreaTuner) * (1 + (p->pPars->fCutGroup && (int)pCut->nLeaves > p->pPars->nLutSize/2));
476     return 1 + (p->pPars->fCutGroup && (int)pCut->nLeaves > p->pPars->nLutSize/2);
477 }
Lf_CutIsMux(Lf_Man_t * p,Lf_Cut_t * pCut,Gia_Obj_t * pMux)478 static inline int Lf_CutIsMux( Lf_Man_t * p, Lf_Cut_t * pCut, Gia_Obj_t * pMux )
479 {
480     int i, Id;
481     if ( pCut->nLeaves != 3 )
482         return 0;
483     assert( Gia_ObjIsMux(p->pGia, pMux) );
484     if ( Gia_ObjIsCi(Gia_ObjFanin0(pMux)) || Gia_ObjIsCi(Gia_ObjFanin1(pMux)) )
485         return 0;
486     Id = Gia_ObjFaninId0p( p->pGia, pMux );
487     for ( i = 0; i < 3; i++ )
488         if ( pCut->pLeaves[i] == Id )
489             break;
490     if ( i == 3 )
491         return 0;
492     Id = Gia_ObjFaninId1p( p->pGia, pMux );
493     for ( i = 0; i < 3; i++ )
494         if ( pCut->pLeaves[i] == Id )
495             break;
496     if ( i == 3 )
497         return 0;
498     Id = Gia_ObjFaninId2p( p->pGia, pMux );
499     for ( i = 0; i < 3; i++ )
500         if ( pCut->pLeaves[i] == Id )
501             break;
502     if ( i == 3 )
503         return 0;
504     return 1;
505 }
506 
507 /**Function*************************************************************
508 
509   Synopsis    [Cut packing.]
510 
511   Description []
512 
513   SideEffects []
514 
515   SeeAlso     []
516 
517 ***********************************************************************/
Lf_MemAlloc(Lf_Mem_t * p,int LogPage,Vec_Ptr_t * vFree,int nCutWords)518 static inline void Lf_MemAlloc( Lf_Mem_t * p, int LogPage, Vec_Ptr_t * vFree, int nCutWords )
519 {
520     memset( p, 0, sizeof(Lf_Mem_t) );
521     p->LogPage   = LogPage;
522     p->MaskPage  = (1 << LogPage) - 1;
523     p->nCutWords = nCutWords;
524     p->vFree     = vFree;
525 }
Lf_MemSaveCut(Lf_Mem_t * p,Lf_Cut_t * pCut,int iObj)526 static inline int Lf_MemSaveCut( Lf_Mem_t * p, Lf_Cut_t * pCut, int iObj )
527 {
528     unsigned char * pPlace;
529     int i, iPlace, Prev = iObj, iCur = p->iCur;
530     assert( !pCut->fMux7 );
531     if ( Vec_PtrSize(&p->vPages) == (p->iCur >> p->LogPage) )
532         Vec_PtrPush( &p->vPages, Vec_PtrSize(p->vFree) ? Vec_PtrPop(p->vFree) : ABC_ALLOC(char,p->MaskPage+1) );
533     assert( p->MaskPage - (p->iCur & p->MaskPage) >= 4 * (LF_LEAF_MAX + 2) );
534     iPlace = iCur & p->MaskPage;
535     pPlace = (unsigned char *)Vec_PtrEntry(&p->vPages, p->iCur >> p->LogPage);
536     iPlace = Gia_AigerWriteUnsignedBuffer( pPlace, iPlace, pCut->nLeaves );
537     for ( i = pCut->nLeaves - 1; i >= 0; i-- )
538         iPlace = Gia_AigerWriteUnsignedBuffer( pPlace, iPlace, Prev - pCut->pLeaves[i] ), Prev = pCut->pLeaves[i];
539     assert( pCut->nLeaves >= 2 || pCut->iFunc <= 3 );
540     if ( pCut->iFunc >= 0 )
541         iPlace = Gia_AigerWriteUnsignedBuffer( pPlace, iPlace, pCut->iFunc );
542     if ( p->MaskPage - (iPlace & p->MaskPage) < 4 * (LF_LEAF_MAX + 2) )
543         p->iCur = ((p->iCur >> p->LogPage) + 1) << p->LogPage;
544     else
545         p->iCur = (p->iCur & ~p->MaskPage) | iPlace;
546     return iCur;
547 }
Lf_MemLoadCut(Lf_Mem_t * p,int iCur,int iObj,Lf_Cut_t * pCut,int fTruth,int fRecycle)548 static inline Lf_Cut_t * Lf_MemLoadCut( Lf_Mem_t * p, int iCur, int iObj, Lf_Cut_t * pCut, int fTruth, int fRecycle )
549 {
550     unsigned char * pPlace;
551     int i, Prev = iObj, Page = iCur >> p->LogPage;
552     assert( Page < Vec_PtrSize(&p->vPages) );
553     pPlace = (unsigned char *)Vec_PtrEntry(&p->vPages, Page) + (iCur & p->MaskPage);
554     pCut->nLeaves = Gia_AigerReadUnsigned(&pPlace);
555     assert( pCut->nLeaves <= LF_LEAF_MAX );
556     for ( i = pCut->nLeaves - 1; i >= 0; i-- )
557         pCut->pLeaves[i] = Prev - Gia_AigerReadUnsigned(&pPlace), Prev = pCut->pLeaves[i];
558     pCut->iFunc = fTruth ? Gia_AigerReadUnsigned(&pPlace) : -1;
559     assert( pCut->nLeaves >= 2 || pCut->iFunc <= 3 );
560     if ( fRecycle && Page && Vec_PtrEntry(&p->vPages, Page-1) )
561     {
562         Vec_PtrPush( p->vFree, Vec_PtrEntry(&p->vPages, Page-1) );
563         Vec_PtrWriteEntry( &p->vPages, Page-1, NULL );
564     }
565     pCut->Sign = fRecycle ? Lf_CutGetSign(pCut) : 0;
566     pCut->fMux7 = 0;
567     return pCut;
568 }
Lf_MemRecycle(Lf_Mem_t * p)569 static inline void Lf_MemRecycle( Lf_Mem_t * p )
570 {
571     void * pPlace; int i;
572     Vec_PtrForEachEntry( void *, &p->vPages, pPlace, i )
573         if ( pPlace )
574             Vec_PtrPush( p->vFree, pPlace );
575     Vec_PtrClear( &p->vPages );
576     p->iCur = 0;
577 }
Lf_MemLoadMuxCut(Lf_Man_t * p,int iObj,Lf_Cut_t * pCut)578 static inline Lf_Cut_t * Lf_MemLoadMuxCut( Lf_Man_t * p, int iObj, Lf_Cut_t * pCut )
579 {
580     Gia_Obj_t * pMux = Gia_ManObj( p->pGia, iObj );
581     assert( Gia_ObjIsMux(p->pGia, pMux) );
582     pCut->iFunc = p->pPars->fCutMin ? 4 : -1;
583     pCut->pLeaves[0] = Gia_ObjFaninId0( pMux, iObj );
584     pCut->pLeaves[1] = Gia_ObjFaninId1( pMux, iObj );
585     pCut->pLeaves[2] = Gia_ObjFaninId2( p->pGia, iObj );
586     pCut->nLeaves = 3;
587     pCut->fMux7 = 1;
588     return pCut;
589 }
Lf_ObjCutMux(Lf_Man_t * p,int i)590 static inline Lf_Cut_t * Lf_ObjCutMux( Lf_Man_t * p, int i )
591 {
592     static word CutSet[LF_CUT_WORDS];
593     return Lf_MemLoadMuxCut( p, i, (Lf_Cut_t *)CutSet );
594 }
Lf_ObjCutBest(Lf_Man_t * p,int i)595 static inline Lf_Cut_t * Lf_ObjCutBest( Lf_Man_t * p, int i )
596 {
597     static word CutSet[LF_CUT_WORDS];
598     Lf_Bst_t * pBest = Lf_ObjReadBest( p, i );
599     Lf_Cut_t * pCut = (Lf_Cut_t *)CutSet;
600     int Index = Lf_BestCutIndex( pBest );
601     pCut->Delay = pBest->Delay[Index];
602     pCut->Flow  = pBest->Flow[Index];
603     if ( Index == 2 )
604         return Lf_MemLoadMuxCut( p, i, pCut );
605     return Lf_MemLoadCut( &p->vStoreOld, pBest->Cut[Index].Handle, i, pCut, p->pPars->fCutMin, 0 );
606 }
Lf_ObjCutBestNew(Lf_Man_t * p,int i,Lf_Cut_t * pCut)607 static inline Lf_Cut_t * Lf_ObjCutBestNew( Lf_Man_t * p, int i, Lf_Cut_t * pCut )
608 {
609     Lf_Bst_t * pBest = Lf_ObjReadBest( p, i );
610     int Index = Lf_BestCutIndex( pBest );
611     pCut->Delay = pBest->Delay[Index];
612     pCut->Flow  = pBest->Flow[Index];
613     if ( Index == 2 )
614         return Lf_MemLoadMuxCut( p, i, pCut );
615     return Lf_MemLoadCut( &p->vStoreNew, pBest->Cut[Index].Handle, i, pCut, 0, 0 );
616 }
617 
618 /**Function*************************************************************
619 
620   Synopsis    [Check correctness of cuts.]
621 
622   Description []
623 
624   SideEffects []
625 
626   SeeAlso     []
627 
628 ***********************************************************************/
Lf_CutCheck(Lf_Cut_t * pBase,Lf_Cut_t * pCut)629 static inline int Lf_CutCheck( Lf_Cut_t * pBase, Lf_Cut_t * pCut ) // check if pCut is contained in pBase
630 {
631     int nSizeB = pBase->nLeaves;
632     int nSizeC = pCut->nLeaves;
633     int i, * pB = pBase->pLeaves;
634     int k, * pC = pCut->pLeaves;
635     for ( i = 0; i < nSizeC; i++ )
636     {
637         for ( k = 0; k < nSizeB; k++ )
638             if ( pC[i] == pB[k] )
639                 break;
640         if ( k == nSizeB )
641             return 0;
642     }
643     return 1;
644 }
Lf_SetCheckArray(Lf_Cut_t ** ppCuts,int nCuts)645 static inline int Lf_SetCheckArray( Lf_Cut_t ** ppCuts, int nCuts )
646 {
647     Lf_Cut_t * pCut0, * pCut1;
648     int i, k, m, n, Value;
649     assert( nCuts > 0 );
650     for ( i = 0; i < nCuts; i++ )
651     {
652         pCut0 = ppCuts[i];
653         assert( !pCut0->fMux7 );
654         assert( pCut0->nLeaves < LF_LEAF_MAX );
655         assert( pCut0->Sign == Lf_CutGetSign(pCut0) );
656         // check duplicates
657         for ( m = 0; m < (int)pCut0->nLeaves; m++ )
658         for ( n = m + 1; n < (int)pCut0->nLeaves; n++ )
659             assert( pCut0->pLeaves[m] < pCut0->pLeaves[n] );
660         // check pairs
661         for ( k = 0; k < nCuts; k++ )
662         {
663             pCut1 = ppCuts[k];
664             if ( pCut0 == pCut1 )
665                 continue;
666             // check containments
667             Value = Lf_CutCheck( pCut0, pCut1 );
668             assert( Value == 0 );
669         }
670     }
671     return 1;
672 }
673 
674 /**Function*************************************************************
675 
676   Synopsis    []
677 
678   Description []
679 
680   SideEffects []
681 
682   SeeAlso     []
683 
684 ***********************************************************************/
Lf_CutMergeOrder(Lf_Cut_t * pCut0,Lf_Cut_t * pCut1,Lf_Cut_t * pCut,int nLutSize)685 static inline int Lf_CutMergeOrder( Lf_Cut_t * pCut0, Lf_Cut_t * pCut1, Lf_Cut_t * pCut, int nLutSize )
686 {
687     int nSize0   = pCut0->nLeaves;
688     int nSize1   = pCut1->nLeaves;
689     int i, * pC0 = pCut0->pLeaves;
690     int k, * pC1 = pCut1->pLeaves;
691     int c, * pC  = pCut->pLeaves;
692     // the case of the largest cut sizes
693     if ( nSize0 == nLutSize && nSize1 == nLutSize )
694     {
695         for ( i = 0; i < nSize0; i++ )
696         {
697             if ( pC0[i] != pC1[i] )  return 0;
698             pC[i] = pC0[i];
699         }
700         pCut->nLeaves = nLutSize;
701         pCut->iFunc = -1;
702         pCut->Sign = pCut0->Sign | pCut1->Sign;
703         return 1;
704     }
705     // compare two cuts with different numbers
706     i = k = c = 0;
707     if ( nSize0 == 0 ) goto FlushCut1;
708     if ( nSize1 == 0 ) goto FlushCut0;
709     while ( 1 )
710     {
711         if ( c == nLutSize ) return 0;
712         if ( pC0[i] < pC1[k] )
713         {
714             pC[c++] = pC0[i++];
715             if ( i >= nSize0 ) goto FlushCut1;
716         }
717         else if ( pC0[i] > pC1[k] )
718         {
719             pC[c++] = pC1[k++];
720             if ( k >= nSize1 ) goto FlushCut0;
721         }
722         else
723         {
724             pC[c++] = pC0[i++]; k++;
725             if ( i >= nSize0 ) goto FlushCut1;
726             if ( k >= nSize1 ) goto FlushCut0;
727         }
728     }
729 
730 FlushCut0:
731     if ( c + nSize0 > nLutSize + i ) return 0;
732     while ( i < nSize0 )
733         pC[c++] = pC0[i++];
734     pCut->nLeaves = c;
735     pCut->iFunc = -1;
736     pCut->fMux7 = 0;
737     pCut->Sign = pCut0->Sign | pCut1->Sign;
738     return 1;
739 
740 FlushCut1:
741     if ( c + nSize1 > nLutSize + k ) return 0;
742     while ( k < nSize1 )
743         pC[c++] = pC1[k++];
744     pCut->nLeaves = c;
745     pCut->iFunc = -1;
746     pCut->fMux7 = 0;
747     pCut->Sign = pCut0->Sign | pCut1->Sign;
748     return 1;
749 }
Lf_CutMergeOrder2(Lf_Cut_t * pCut0,Lf_Cut_t * pCut1,Lf_Cut_t * pCut,int nLutSize)750 static inline int Lf_CutMergeOrder2( Lf_Cut_t * pCut0, Lf_Cut_t * pCut1, Lf_Cut_t * pCut, int nLutSize )
751 {
752     int x0, i0 = 0, nSize0 = pCut0->nLeaves, * pC0 = pCut0->pLeaves;
753     int x1, i1 = 0, nSize1 = pCut1->nLeaves, * pC1 = pCut1->pLeaves;
754     int xMin, c = 0, * pC  = pCut->pLeaves;
755     while ( 1 )
756     {
757         x0 = (i0 == nSize0) ? ABC_INFINITY : pC0[i0];
758         x1 = (i1 == nSize1) ? ABC_INFINITY : pC1[i1];
759         xMin = Abc_MinInt(x0, x1);
760         if ( xMin == ABC_INFINITY ) break;
761         if ( c == nLutSize ) return 0;
762         pC[c++] = xMin;
763         if (x0 == xMin) i0++;
764         if (x1 == xMin) i1++;
765     }
766     pCut->nLeaves = c;
767     pCut->iFunc = -1;
768     pCut->fMux7 = 0;
769     pCut->Sign = pCut0->Sign | pCut1->Sign;
770     return 1;
771 }
Lf_CutMergeOrderMux(Lf_Cut_t * pCut0,Lf_Cut_t * pCut1,Lf_Cut_t * pCut2,Lf_Cut_t * pCut,int nLutSize)772 static inline int Lf_CutMergeOrderMux( Lf_Cut_t * pCut0, Lf_Cut_t * pCut1, Lf_Cut_t * pCut2, Lf_Cut_t * pCut, int nLutSize )
773 {
774     int x0, i0 = 0, nSize0 = pCut0->nLeaves, * pC0 = pCut0->pLeaves;
775     int x1, i1 = 0, nSize1 = pCut1->nLeaves, * pC1 = pCut1->pLeaves;
776     int x2, i2 = 0, nSize2 = pCut2->nLeaves, * pC2 = pCut2->pLeaves;
777     int xMin, c = 0, * pC  = pCut->pLeaves;
778     while ( 1 )
779     {
780         x0 = (i0 == nSize0) ? ABC_INFINITY : pC0[i0];
781         x1 = (i1 == nSize1) ? ABC_INFINITY : pC1[i1];
782         x2 = (i2 == nSize2) ? ABC_INFINITY : pC2[i2];
783         xMin = Abc_MinInt( Abc_MinInt(x0, x1), x2 );
784         if ( xMin == ABC_INFINITY ) break;
785         if ( c == nLutSize ) return 0;
786         pC[c++] = xMin;
787         if (x0 == xMin) i0++;
788         if (x1 == xMin) i1++;
789         if (x2 == xMin) i2++;
790     }
791     pCut->nLeaves = c;
792     pCut->iFunc = -1;
793     pCut->fMux7 = 0;
794     pCut->Sign = pCut0->Sign | pCut1->Sign | pCut2->Sign;
795     return 1;
796 }
797 
Lf_SetCutIsContainedOrder(Lf_Cut_t * pBase,Lf_Cut_t * pCut)798 static inline int Lf_SetCutIsContainedOrder( Lf_Cut_t * pBase, Lf_Cut_t * pCut ) // check if pCut is contained in pBase
799 {
800     int i, nSizeB = pBase->nLeaves;
801     int k, nSizeC = pCut->nLeaves;
802     if ( nSizeB == nSizeC )
803     {
804         for ( i = 0; i < nSizeB; i++ )
805             if ( pBase->pLeaves[i] != pCut->pLeaves[i] )
806                 return 0;
807         return 1;
808     }
809     assert( nSizeB > nSizeC );
810     if ( nSizeC == 0 )
811         return 1;
812     for ( i = k = 0; i < nSizeB; i++ )
813     {
814         if ( pBase->pLeaves[i] > pCut->pLeaves[k] )
815             return 0;
816         if ( pBase->pLeaves[i] == pCut->pLeaves[k] )
817         {
818             if ( ++k == nSizeC )
819                 return 1;
820         }
821     }
822     return 0;
823 }
Lf_SetLastCutIsContained(Lf_Cut_t ** pCuts,int nCuts)824 static inline int Lf_SetLastCutIsContained( Lf_Cut_t ** pCuts, int nCuts )
825 {
826     int i;
827     for ( i = 0; i < nCuts; i++ )
828         if ( pCuts[i]->nLeaves <= pCuts[nCuts]->nLeaves && (pCuts[i]->Sign & pCuts[nCuts]->Sign) == pCuts[i]->Sign && Lf_SetCutIsContainedOrder(pCuts[nCuts], pCuts[i]) )
829             return 1;
830     return 0;
831 }
832 
833 
834 /**Function*************************************************************
835 
836   Synopsis    []
837 
838   Description []
839 
840   SideEffects []
841 
842   SeeAlso     []
843 
844 ***********************************************************************/
Lf_CutCompareDelay(Lf_Cut_t * pCut0,Lf_Cut_t * pCut1)845 static inline int Lf_CutCompareDelay( Lf_Cut_t * pCut0, Lf_Cut_t * pCut1 )
846 {
847     if ( pCut0->Delay   < pCut1->Delay   )  return -1;
848     if ( pCut0->Delay   > pCut1->Delay   )  return  1;
849     if ( pCut0->nLeaves < pCut1->nLeaves )  return -1;
850     if ( pCut0->nLeaves > pCut1->nLeaves )  return  1;
851     if ( pCut0->Flow    < pCut1->Flow - LF_EPSILON )  return -1;
852     if ( pCut0->Flow    > pCut1->Flow + LF_EPSILON )  return  1;
853     return 0;
854 }
Lf_CutCompareArea(Lf_Cut_t * pCut0,Lf_Cut_t * pCut1)855 static inline int Lf_CutCompareArea( Lf_Cut_t * pCut0, Lf_Cut_t * pCut1 )
856 {
857     if ( pCut0->fLate   < pCut1->fLate   )  return -1;
858     if ( pCut0->fLate   > pCut1->fLate   )  return  1;
859     if ( pCut0->Flow    < pCut1->Flow - LF_EPSILON )  return -1;
860     if ( pCut0->Flow    > pCut1->Flow + LF_EPSILON )  return  1;
861     if ( pCut0->Delay   < pCut1->Delay   )  return -1;
862     if ( pCut0->Delay   > pCut1->Delay   )  return  1;
863     if ( pCut0->nLeaves < pCut1->nLeaves )  return -1;
864     if ( pCut0->nLeaves > pCut1->nLeaves )  return  1;
865     return 0;
866 }
Lf_SetLastCutContainsArea(Lf_Cut_t ** pCuts,int nCuts)867 static inline int Lf_SetLastCutContainsArea( Lf_Cut_t ** pCuts, int nCuts )
868 {
869     int i, k, fChanges = 0;
870     for ( i = 1; i < nCuts; i++ )
871         if ( pCuts[nCuts]->nLeaves < pCuts[i]->nLeaves && (pCuts[nCuts]->Sign & pCuts[i]->Sign) == pCuts[nCuts]->Sign && Lf_SetCutIsContainedOrder(pCuts[i], pCuts[nCuts]) )
872             pCuts[i]->nLeaves = LF_NO_LEAF, fChanges = 1;
873     if ( !fChanges )
874         return nCuts;
875     for ( i = k = 1; i <= nCuts; i++ )
876     {
877         if ( pCuts[i]->nLeaves == LF_NO_LEAF )
878             continue;
879         if ( k < i )
880             ABC_SWAP( Lf_Cut_t *, pCuts[k], pCuts[i] );
881         k++;
882     }
883     return k - 1;
884 }
Lf_SetSortByArea(Lf_Cut_t ** pCuts,int nCuts)885 static inline void Lf_SetSortByArea( Lf_Cut_t ** pCuts, int nCuts )
886 {
887     int i;
888     for ( i = nCuts; i > 1; i-- )
889     {
890         if ( Lf_CutCompareArea(pCuts[i - 1], pCuts[i]) < 0 )//!= 1 )
891             return;
892         ABC_SWAP( Lf_Cut_t *, pCuts[i - 1], pCuts[i] );
893     }
894 }
Lf_SetAddCut(Lf_Cut_t ** pCuts,int nCuts,int nCutNum)895 static inline int Lf_SetAddCut( Lf_Cut_t ** pCuts, int nCuts, int nCutNum )
896 {
897     if ( nCuts == 0 )
898         return 1;
899     nCuts = Lf_SetLastCutContainsArea(pCuts, nCuts);
900     assert( nCuts >= 1 );
901     if ( Lf_CutCompareDelay(pCuts[0], pCuts[nCuts]) == 1 ) // new cut is better for delay
902     {
903         ABC_SWAP( Lf_Cut_t *, pCuts[0], pCuts[nCuts] );
904         // if old cut (now cut number nCuts) is contained - remove it
905         if ( pCuts[0]->nLeaves < pCuts[nCuts]->nLeaves && (pCuts[0]->Sign & pCuts[nCuts]->Sign) == pCuts[0]->Sign && Lf_SetCutIsContainedOrder(pCuts[nCuts], pCuts[0]) )
906             return nCuts;
907     }
908     // sort area cuts by area
909     Lf_SetSortByArea( pCuts, nCuts );
910     // add new cut if there is room
911     return Abc_MinInt( nCuts + 1, nCutNum - 1 );
912 }
Lf_SetSortBySize(Lf_Cut_t ** pCutsR,int nCutsR)913 static inline void Lf_SetSortBySize( Lf_Cut_t ** pCutsR, int nCutsR )
914 {
915     int i, j, best_i;
916     for ( i = 1; i < nCutsR-1; i++ )
917     {
918         best_i = i;
919         for ( j = i+1; j < nCutsR; j++ )
920             if ( pCutsR[j]->nLeaves > pCutsR[best_i]->nLeaves )
921                 best_i = j;
922         ABC_SWAP( Lf_Cut_t *, pCutsR[i], pCutsR[best_i] );
923     }
924 }
925 
926 /**Function*************************************************************
927 
928   Synopsis    [Check if truth table has non-const-cof cofactoring variable.]
929 
930   Description []
931 
932   SideEffects []
933 
934   SeeAlso     []
935 
936 ***********************************************************************/
Lf_ManFindCofVar(word * pTruth,int nWords,int nVars)937 static inline int Lf_ManFindCofVar( word * pTruth, int nWords, int nVars )
938 {
939     word uTruthCof[LF_TT_WORDS]; int iVar;
940     for ( iVar = 0; iVar < nVars; iVar++ )
941     {
942         Abc_TtCofactor0p( uTruthCof, pTruth, nWords, iVar );
943         if ( Abc_TtSupportSize(uTruthCof, nVars) < 2 )
944             continue;
945         Abc_TtCofactor1p( uTruthCof, pTruth, nWords, iVar );
946         if ( Abc_TtSupportSize(uTruthCof, nVars) < 2 )
947             continue;
948         return iVar;
949     }
950     return -1;
951 }
952 
953 /**Function*************************************************************
954 
955   Synopsis    []
956 
957   Description []
958 
959   SideEffects []
960 
961   SeeAlso     []
962 
963 ***********************************************************************/
Lf_CutComputeTruth6(Lf_Man_t * p,Lf_Cut_t * pCut0,Lf_Cut_t * pCut1,int fCompl0,int fCompl1,Lf_Cut_t * pCutR,int fIsXor)964 static inline int Lf_CutComputeTruth6( Lf_Man_t * p, Lf_Cut_t * pCut0, Lf_Cut_t * pCut1, int fCompl0, int fCompl1, Lf_Cut_t * pCutR, int fIsXor )
965 {
966 //    extern int Mf_ManTruthCanonicize( word * t, int nVars );
967     int nOldSupp = pCutR->nLeaves, truthId, fCompl; word t;
968     word t0 = *Lf_CutTruth(p, pCut0);
969     word t1 = *Lf_CutTruth(p, pCut1);
970     if ( Abc_LitIsCompl(pCut0->iFunc) ^ fCompl0 ) t0 = ~t0;
971     if ( Abc_LitIsCompl(pCut1->iFunc) ^ fCompl1 ) t1 = ~t1;
972     t0 = Abc_Tt6Expand( t0, pCut0->pLeaves, pCut0->nLeaves, pCutR->pLeaves, pCutR->nLeaves );
973     t1 = Abc_Tt6Expand( t1, pCut1->pLeaves, pCut1->nLeaves, pCutR->pLeaves, pCutR->nLeaves );
974     t =  fIsXor ? t0 ^ t1 : t0 & t1;
975     if ( (fCompl = (int)(t & 1)) ) t = ~t;
976     pCutR->nLeaves = Abc_Tt6MinBase( &t, pCutR->pLeaves, pCutR->nLeaves );
977     assert( (int)(t & 1) == 0 );
978     truthId        = Vec_MemHashInsert(p->vTtMem, &t);
979     pCutR->iFunc   = Abc_Var2Lit( truthId, fCompl );
980 //    p->nCutMux += Lf_ManTtIsMux( t );
981     assert( (int)pCutR->nLeaves <= nOldSupp );
982 //    Mf_ManTruthCanonicize( &t, pCutR->nLeaves );
983     return (int)pCutR->nLeaves < nOldSupp;
984 }
Lf_CutComputeTruth(Lf_Man_t * p,Lf_Cut_t * pCut0,Lf_Cut_t * pCut1,int fCompl0,int fCompl1,Lf_Cut_t * pCutR,int fIsXor)985 static inline int Lf_CutComputeTruth( Lf_Man_t * p, Lf_Cut_t * pCut0, Lf_Cut_t * pCut1, int fCompl0, int fCompl1, Lf_Cut_t * pCutR, int fIsXor )
986 {
987     if ( p->pPars->nLutSize <= 6 )
988         return Lf_CutComputeTruth6( p, pCut0, pCut1, fCompl0, fCompl1, pCutR, fIsXor );
989     {
990     word uTruth[LF_TT_WORDS], uTruth0[LF_TT_WORDS], uTruth1[LF_TT_WORDS];
991     int nOldSupp   = pCutR->nLeaves, truthId;
992     int LutSize    = p->pPars->nLutSize, fCompl;
993     int nWords     = Abc_Truth6WordNum(LutSize);
994     word * pTruth0 = Lf_CutTruth(p, pCut0);
995     word * pTruth1 = Lf_CutTruth(p, pCut1);
996     Abc_TtCopy( uTruth0, pTruth0, nWords, Abc_LitIsCompl(pCut0->iFunc) ^ fCompl0 );
997     Abc_TtCopy( uTruth1, pTruth1, nWords, Abc_LitIsCompl(pCut1->iFunc) ^ fCompl1 );
998     Abc_TtExpand( uTruth0, LutSize, pCut0->pLeaves, pCut0->nLeaves, pCutR->pLeaves, pCutR->nLeaves );
999     Abc_TtExpand( uTruth1, LutSize, pCut1->pLeaves, pCut1->nLeaves, pCutR->pLeaves, pCutR->nLeaves );
1000     if ( fIsXor )
1001         Abc_TtXor( uTruth, uTruth0, uTruth1, nWords, (fCompl = (int)((uTruth0[0] ^ uTruth1[0]) & 1)) );
1002     else
1003         Abc_TtAnd( uTruth, uTruth0, uTruth1, nWords, (fCompl = (int)((uTruth0[0] & uTruth1[0]) & 1)) );
1004     pCutR->nLeaves = Abc_TtMinBase( uTruth, pCutR->pLeaves, pCutR->nLeaves, LutSize );
1005     assert( (uTruth[0] & 1) == 0 );
1006 //Kit_DsdPrintFromTruth( uTruth, pCutR->nLeaves ), printf("\n" ), printf("\n" );
1007     truthId        = Vec_MemHashInsert(p->vTtMem, uTruth);
1008     pCutR->iFunc   = Abc_Var2Lit( truthId, fCompl );
1009     assert( (int)pCutR->nLeaves <= nOldSupp );
1010     return (int)pCutR->nLeaves < nOldSupp;
1011     }
1012 }
Lf_CutComputeTruthMux6(Lf_Man_t * p,Lf_Cut_t * pCut0,Lf_Cut_t * pCut1,Lf_Cut_t * pCutC,int fCompl0,int fCompl1,int fComplC,Lf_Cut_t * pCutR)1013 static inline int Lf_CutComputeTruthMux6( Lf_Man_t * p, Lf_Cut_t * pCut0, Lf_Cut_t * pCut1, Lf_Cut_t * pCutC, int fCompl0, int fCompl1, int fComplC, Lf_Cut_t * pCutR )
1014 {
1015     int nOldSupp = pCutR->nLeaves, truthId, fCompl; word t;
1016     word t0 = *Lf_CutTruth(p, pCut0);
1017     word t1 = *Lf_CutTruth(p, pCut1);
1018     word tC = *Lf_CutTruth(p, pCutC);
1019     if ( Abc_LitIsCompl(pCut0->iFunc) ^ fCompl0 ) t0 = ~t0;
1020     if ( Abc_LitIsCompl(pCut1->iFunc) ^ fCompl1 ) t1 = ~t1;
1021     if ( Abc_LitIsCompl(pCutC->iFunc) ^ fComplC ) tC = ~tC;
1022     t0 = Abc_Tt6Expand( t0, pCut0->pLeaves, pCut0->nLeaves, pCutR->pLeaves, pCutR->nLeaves );
1023     t1 = Abc_Tt6Expand( t1, pCut1->pLeaves, pCut1->nLeaves, pCutR->pLeaves, pCutR->nLeaves );
1024     tC = Abc_Tt6Expand( tC, pCutC->pLeaves, pCutC->nLeaves, pCutR->pLeaves, pCutR->nLeaves );
1025     t = (tC & t1) | (~tC & t0);
1026     if ( (fCompl = (int)(t & 1)) ) t = ~t;
1027     pCutR->nLeaves = Abc_Tt6MinBase( &t, pCutR->pLeaves, pCutR->nLeaves );
1028     assert( (int)(t & 1) == 0 );
1029     truthId        = Vec_MemHashInsert(p->vTtMem, &t);
1030     pCutR->iFunc   = Abc_Var2Lit( truthId, fCompl );
1031     assert( (int)pCutR->nLeaves <= nOldSupp );
1032     return (int)pCutR->nLeaves < nOldSupp;
1033 }
Lf_CutComputeTruthMux(Lf_Man_t * p,Lf_Cut_t * pCut0,Lf_Cut_t * pCut1,Lf_Cut_t * pCutC,int fCompl0,int fCompl1,int fComplC,Lf_Cut_t * pCutR)1034 static inline int Lf_CutComputeTruthMux( Lf_Man_t * p, Lf_Cut_t * pCut0, Lf_Cut_t * pCut1, Lf_Cut_t * pCutC, int fCompl0, int fCompl1, int fComplC, Lf_Cut_t * pCutR )
1035 {
1036     if ( p->pPars->nLutSize <= 6 )
1037         return Lf_CutComputeTruthMux6( p, pCut0, pCut1, pCutC, fCompl0, fCompl1, fComplC, pCutR );
1038     {
1039     word uTruth[LF_TT_WORDS], uTruth0[LF_TT_WORDS], uTruth1[LF_TT_WORDS], uTruthC[LF_TT_WORDS];
1040     int nOldSupp   = pCutR->nLeaves, truthId;
1041     int LutSize    = p->pPars->nLutSize, fCompl;
1042     int nWords     = Abc_Truth6WordNum(LutSize);
1043     word * pTruth0 = Lf_CutTruth(p, pCut0);
1044     word * pTruth1 = Lf_CutTruth(p, pCut1);
1045     word * pTruthC = Lf_CutTruth(p, pCutC);
1046     Abc_TtCopy( uTruth0, pTruth0, nWords, Abc_LitIsCompl(pCut0->iFunc) ^ fCompl0 );
1047     Abc_TtCopy( uTruth1, pTruth1, nWords, Abc_LitIsCompl(pCut1->iFunc) ^ fCompl1 );
1048     Abc_TtCopy( uTruthC, pTruthC, nWords, Abc_LitIsCompl(pCutC->iFunc) ^ fComplC );
1049     Abc_TtExpand( uTruth0, LutSize, pCut0->pLeaves, pCut0->nLeaves, pCutR->pLeaves, pCutR->nLeaves );
1050     Abc_TtExpand( uTruth1, LutSize, pCut1->pLeaves, pCut1->nLeaves, pCutR->pLeaves, pCutR->nLeaves );
1051     Abc_TtExpand( uTruthC, LutSize, pCutC->pLeaves, pCutC->nLeaves, pCutR->pLeaves, pCutR->nLeaves );
1052     Abc_TtMux( uTruth, uTruthC, uTruth1, uTruth0, nWords );
1053     fCompl         = (int)(uTruth[0] & 1);
1054     if ( fCompl ) Abc_TtNot( uTruth, nWords );
1055     pCutR->nLeaves = Abc_TtMinBase( uTruth, pCutR->pLeaves, pCutR->nLeaves, LutSize );
1056     assert( (uTruth[0] & 1) == 0 );
1057     truthId        = Vec_MemHashInsert(p->vTtMem, uTruth);
1058     pCutR->iFunc   = Abc_Var2Lit( truthId, fCompl );
1059     assert( (int)pCutR->nLeaves <= nOldSupp );
1060     return (int)pCutR->nLeaves < nOldSupp;
1061     }
1062 }
1063 
1064 /**Function*************************************************************
1065 
1066   Synopsis    [Exact local area.]
1067 
1068   Description []
1069 
1070   SideEffects []
1071 
1072   SeeAlso     []
1073 
1074 ***********************************************************************/
Lf_CutRef_rec(Lf_Man_t * p,Lf_Cut_t * pCut)1075 float Lf_CutRef_rec( Lf_Man_t * p, Lf_Cut_t * pCut )
1076 {
1077     word CutTemp[LF_CUT_WORDS] = {0};
1078     float Count = Lf_CutArea(p, pCut);
1079     int i, Var;
1080     Lf_CutForEachVar( pCut, Var, i )
1081         if ( !Lf_ObjMapRefInc(p, Var) )
1082             Count += Lf_CutRef_rec( p, Lf_ObjCutBestNew(p, Var, (Lf_Cut_t *)CutTemp) );
1083     return Count;
1084 }
Lf_CutDeref_rec(Lf_Man_t * p,Lf_Cut_t * pCut)1085 float Lf_CutDeref_rec( Lf_Man_t * p, Lf_Cut_t * pCut )
1086 {
1087     word CutTemp[LF_CUT_WORDS] = {0};
1088     float Count = Lf_CutArea(p, pCut);
1089     int i, Var;
1090     Lf_CutForEachVar( pCut, Var, i )
1091         if ( !Lf_ObjMapRefDec(p, Var) )
1092             Count += Lf_CutDeref_rec( p, Lf_ObjCutBestNew(p, Var, (Lf_Cut_t *)CutTemp) );
1093     return Count;
1094 }
Lf_CutAreaDerefed(Lf_Man_t * p,Lf_Cut_t * pCut)1095 static inline float Lf_CutAreaDerefed( Lf_Man_t * p, Lf_Cut_t * pCut )
1096 {
1097     float Ela1 = Lf_CutRef_rec( p, pCut );
1098     Lf_CutDeref_rec( p, pCut );
1099 //    float Ela2 = Lf_CutDeref_rec( p, pCut );
1100 //    assert( Ela1 == Ela2 );
1101     return Ela1;
1102 }
1103 
1104 
1105 /**Function*************************************************************
1106 
1107   Synopsis    []
1108 
1109   Description []
1110 
1111   SideEffects []
1112 
1113   SeeAlso     []
1114 
1115 ***********************************************************************/
Lf_CutRequired(Lf_Man_t * p,Lf_Cut_t * pCut)1116 static inline int  Lf_CutRequired( Lf_Man_t * p, Lf_Cut_t * pCut )
1117 {
1118     int i, Arr, Req, Arrival = 0, Required = 0;
1119     for ( i = 0; i < (int)pCut->nLeaves; i++ )
1120     {
1121         if ( Lf_ObjOff(p, pCut->pLeaves[i]) < 0 )
1122 //            Arr = Lf_ObjCiArrival( p, Gia_ObjCioId(Gia_ManObj(p->pGia, pCut->pLeaves[i])) );
1123             Arr = Lf_ObjArrival_rec( p, Gia_ManObj(p->pGia, pCut->pLeaves[i]) );
1124         else
1125             Arr = Lf_ObjReadBest(p, pCut->pLeaves[i])->Delay[0];
1126         Arrival = Abc_MaxInt( Arrival, Arr );
1127         Req = Lf_ObjRequired(p, pCut->pLeaves[i]);
1128         if ( Req < ABC_INFINITY )
1129             Required = Abc_MaxInt( Required, Req );
1130     }
1131     return Abc_MaxInt( Required + 2, Arrival + 1 );
1132 }
Lf_CutParams(Lf_Man_t * p,Lf_Cut_t * pCut,int Required,float FlowRefs,Gia_Obj_t * pMux)1133 static inline void Lf_CutParams( Lf_Man_t * p, Lf_Cut_t * pCut, int Required, float FlowRefs, Gia_Obj_t * pMux )
1134 {
1135     Lf_Bst_t * pBest;
1136     int i, Index, Delay;
1137     assert( !pCut->fMux7 || Gia_ObjIsMux(p->pGia, pMux) );
1138     pCut->fLate = 0;
1139     pCut->Delay = 0;
1140     pCut->Flow  = 0;
1141     assert( pCut->nLeaves < LF_NO_LEAF );
1142     for ( i = 0; i < (int)pCut->nLeaves; i++ )
1143     {
1144         if ( Lf_ObjOff(p, pCut->pLeaves[i]) < 0 )
1145 //            Delay = Lf_ObjCiArrival( p, Gia_ObjCioId(Gia_ManObj(p->pGia, pCut->pLeaves[i])) );
1146             Delay = Lf_ObjArrival_rec( p, Gia_ManObj(p->pGia, pCut->pLeaves[i]) );
1147         else
1148         {
1149             pBest = Lf_ObjReadBest(p, pCut->pLeaves[i]);
1150             assert( pBest->Delay[0] <= pBest->Delay[1] );
1151             assert( pBest->Flow[0]  >= pBest->Flow[1]  );
1152             if ( p->fUseEla )
1153                 Index = Lf_BestIndex(pBest);
1154             else
1155             {
1156                 Index = (int)(pBest->Delay[1] + 1 <= Required && Required != ABC_INFINITY);
1157                 //pCut->Flow += pBest->Flow[Index];
1158                 if ( pCut->Flow >= (float)1e32 || pBest->Flow[Index] >= (float)1e32 )
1159                     pCut->Flow = (float)1e32;
1160                 else
1161                 {
1162                     pCut->Flow += pBest->Flow[Index];
1163                     if ( pCut->Flow > (float)1e32 )
1164                          pCut->Flow = (float)1e32;
1165                 }
1166             }
1167             Delay = pBest->Delay[Index];
1168         }
1169 //        if ( pCut->fMux7 && pCut->pLeaves[i] == Gia_ObjFaninId2p(p->pGia, pMux) )
1170 //            Delay += 1;
1171         pCut->Delay = Abc_MaxInt( pCut->Delay, Delay );
1172     }
1173     pCut->Delay += (int)(pCut->nLeaves > 1);// && !pCut->fMux7;
1174     if ( pCut->Delay > Required )
1175         pCut->fLate = 1;
1176     if ( p->fUseEla )
1177         pCut->Flow = Lf_CutAreaDerefed(p, pCut) / FlowRefs;
1178     else
1179     {
1180         pCut->Flow = (pCut->Flow + Lf_CutArea(p, pCut)) / FlowRefs;
1181         if ( pCut->Flow > (float)1e32 )
1182              pCut->Flow = (float)1e32;
1183     }
1184 }
1185 
Lf_ObjMergeOrder(Lf_Man_t * p,int iObj)1186 void Lf_ObjMergeOrder( Lf_Man_t * p, int iObj )
1187 {
1188     word CutSet[LF_CUT_MAX][LF_CUT_WORDS] = {{0}};
1189     Lf_Cut_t * pCutSet0, * pCutSet1, * pCutSet2, * pCut0, * pCut1, * pCut2;
1190     Lf_Cut_t * pCutSet = (Lf_Cut_t *)CutSet, * pCutsR[LF_CUT_MAX];
1191     Gia_Obj_t * pObj = Gia_ManObj(p->pGia, iObj);
1192     Lf_Bst_t * pBest = Lf_ObjReadBest(p, iObj);
1193     float FlowRefs = Lf_ObjFlowRefs(p, iObj);
1194     int Required   = Lf_ObjRequired(p, iObj);
1195     int nLutSize   = p->pPars->fCutGroup ? p->pPars->nLutSize/2 : p->pPars->nLutSize;
1196     int nCutNum    = p->pPars->nCutNum;
1197     int nCutWords  = p->nCutWords;
1198     int fComp0     = Gia_ObjFaninC0(pObj);
1199     int fComp1     = Gia_ObjFaninC1(pObj);
1200     int nCuts0     = Lf_ManPrepareSet( p, Gia_ObjFaninId0(pObj, iObj), 0, &pCutSet0 );
1201     int nCuts1     = Lf_ManPrepareSet( p, Gia_ObjFaninId1(pObj, iObj), 1, &pCutSet1 );
1202     int iSibl      = Gia_ObjSibl(p->pGia, iObj);
1203     int i, k, n, iCutUsed, nCutsR = 0;
1204     float Value1 = -1, Value2 = -1;
1205     assert( !Gia_ObjIsBuf(pObj) );
1206     Lf_CutSetForEachCut( nCutWords, pCutSet, pCut0, i, nCutNum )
1207         pCutsR[i] = pCut0;
1208     if ( p->Iter )
1209     {
1210         assert( nCutsR == 0 );
1211         // load cuts
1212         Lf_MemLoadCut( &p->vStoreOld, pBest->Cut[0].Handle, iObj, pCutsR[0], p->pPars->fCutMin, 1 );
1213         if ( Lf_BestDiffCuts(pBest) )
1214             Lf_MemLoadCut( &p->vStoreOld, pBest->Cut[1].Handle, iObj, pCutsR[1], p->pPars->fCutMin, 1 );
1215         // deref the cut
1216         if ( p->fUseEla && Lf_ObjMapRefNum(p, iObj) > 0 )
1217             Value1 = Lf_CutDeref_rec( p, pCutsR[Lf_BestIndex(pBest)] );
1218         // update required times
1219         if ( Required == ABC_INFINITY )//&& !p->fUseEla )
1220             Required = Lf_CutRequired( p, pCutsR[0] );
1221         // compute parameters
1222         Lf_CutParams( p, pCutsR[nCutsR++], Required, FlowRefs, pObj );
1223         if ( Lf_BestDiffCuts(pBest) )
1224         {
1225             assert( nCutsR == 1 );
1226             Lf_CutParams( p, pCutsR[nCutsR], Required, FlowRefs, pObj );
1227             nCutsR = Lf_SetAddCut( pCutsR, nCutsR, nCutNum );
1228         }
1229         if ( pCutsR[0]->fLate )
1230             p->nTimeFails++;
1231     }
1232     if ( iSibl )
1233     {
1234         Gia_Obj_t * pObjE = Gia_ObjSiblObj(p->pGia, iObj);
1235         int fCompE = Gia_ObjPhase(pObj) ^ Gia_ObjPhase(pObjE);
1236         int nCutsE = Lf_ManPrepareSet( p, iSibl, 2, &pCutSet2 );
1237         Lf_CutSetForEachCut( nCutWords, pCutSet2, pCut2, n, nCutsE )
1238         {
1239             if ( pCut2->pLeaves[0] == iSibl )
1240                 continue;
1241             Lf_CutCopy( pCutsR[nCutsR], pCut2, nCutWords );
1242             if ( pCutsR[nCutsR]->iFunc >= 0 )
1243                 pCutsR[nCutsR]->iFunc = Abc_LitNotCond( pCutsR[nCutsR]->iFunc, fCompE );
1244             Lf_CutParams( p, pCutsR[nCutsR], Required, FlowRefs, pObj );
1245             nCutsR = Lf_SetAddCut( pCutsR, nCutsR, nCutNum );
1246         }
1247     }
1248     if ( Gia_ObjIsMuxId(p->pGia, iObj) )
1249     {
1250         Lf_Cut_t * pCutSave = NULL;
1251         int fComp2 = Gia_ObjFaninC2(p->pGia, pObj);
1252         int nCuts2 = Lf_ManPrepareSet( p, Gia_ObjFaninId2(p->pGia, iObj), 2, &pCutSet2 );
1253         p->CutCount[0] += nCuts0 * nCuts1 * nCuts2;
1254         Lf_CutSetForEachCut( nCutWords, pCutSet0, pCut0, i, nCuts0 ) if ( (int)pCut0->nLeaves <= nLutSize )
1255         Lf_CutSetForEachCut( nCutWords, pCutSet1, pCut1, k, nCuts1 ) if ( (int)pCut1->nLeaves <= nLutSize )
1256         Lf_CutSetForEachCut( nCutWords, pCutSet2, pCut2, n, nCuts2 ) if ( (int)pCut2->nLeaves <= nLutSize )
1257         {
1258             pCutSave = pCut2;
1259             if ( Lf_CutCountBits(pCut0->Sign | pCut1->Sign | pCut2->Sign) > nLutSize )
1260                 continue;
1261             p->CutCount[1]++;
1262             if ( !Lf_CutMergeOrderMux(pCut0, pCut1, pCut2, pCutsR[nCutsR], nLutSize) )
1263                 continue;
1264             if ( Lf_SetLastCutIsContained(pCutsR, nCutsR) )
1265                 continue;
1266             p->CutCount[2]++;
1267             if ( p->pPars->fCutMin && Lf_CutComputeTruthMux(p, pCut0, pCut1, pCut2, fComp0, fComp1, fComp2, pCutsR[nCutsR]) )
1268                 pCutsR[nCutsR]->Sign = Lf_CutGetSign(pCutsR[nCutsR]);
1269             if ( p->pPars->nLutSizeMux && p->pPars->nLutSizeMux == (int)pCutsR[nCutsR]->nLeaves &&
1270                 Lf_ManFindCofVar(Lf_CutTruth(p,pCutsR[nCutsR]), Abc_Truth6WordNum(nLutSize), pCutsR[nCutsR]->nLeaves) == -1 )
1271                 continue;
1272             Lf_CutParams( p, pCutsR[nCutsR], Required, FlowRefs, pObj );
1273             nCutsR = Lf_SetAddCut( pCutsR, nCutsR, nCutNum );
1274         }
1275         if ( p->pPars->fCutGroup )
1276         {
1277             assert( pCutSave->nLeaves == 1 );
1278             assert( pCutSave->pLeaves[0] == Gia_ObjFaninId2(p->pGia, iObj) );
1279             Lf_CutSetForEachCut( nCutWords, pCutSet0, pCut0, i, nCuts0 ) if ( (int)pCut0->nLeaves <= nLutSize )
1280             Lf_CutSetForEachCut( nCutWords, pCutSet1, pCut1, k, nCuts1 ) if ( (int)pCut1->nLeaves <= nLutSize )
1281             {
1282                 assert( (int)pCut0->nLeaves + (int)pCut1->nLeaves + 1 <= p->pPars->nLutSize );
1283     //            if ( Lf_CutCountBits(pCut0->Sign | pCut1->Sign | pCutSave->Sign) > p->pPars->nLutSize )
1284     //                continue;
1285                 p->CutCount[1]++;
1286                 if ( !Lf_CutMergeOrderMux(pCut0, pCut1, pCutSave, pCutsR[nCutsR], p->pPars->nLutSize) )
1287                     continue;
1288                 if ( Lf_SetLastCutIsContained(pCutsR, nCutsR) )
1289                     continue;
1290                 p->CutCount[2]++;
1291                 if ( p->pPars->fCutMin && Lf_CutComputeTruthMux(p, pCut0, pCut1, pCutSave, fComp0, fComp1, fComp2, pCutsR[nCutsR]) )
1292                     pCutsR[nCutsR]->Sign = Lf_CutGetSign(pCutsR[nCutsR]);
1293     //            if ( p->pPars->nLutSizeMux && p->pPars->nLutSizeMux == (int)pCutsR[nCutsR]->nLeaves &&
1294     //                Lf_ManFindCofVar(Lf_CutTruth(p,pCutsR[nCutsR]), Abc_Truth6WordNum(nLutSize), pCutsR[nCutsR]->nLeaves) == -1 )
1295     //                continue;
1296                 Lf_CutParams( p, pCutsR[nCutsR], Required, FlowRefs, pObj );
1297                 nCutsR = Lf_SetAddCut( pCutsR, nCutsR, nCutNum );
1298             }
1299         }
1300     }
1301     else
1302     {
1303         int fIsXor = Gia_ObjIsXor(pObj);
1304         p->CutCount[0] += nCuts0 * nCuts1;
1305         Lf_CutSetForEachCut( nCutWords, pCutSet0, pCut0, i, nCuts0 ) if ( (int)pCut0->nLeaves <= nLutSize )
1306         Lf_CutSetForEachCut( nCutWords, pCutSet1, pCut1, k, nCuts1 ) if ( (int)pCut1->nLeaves <= nLutSize )
1307         {
1308             if ( (int)(pCut0->nLeaves + pCut1->nLeaves) > nLutSize && Lf_CutCountBits(pCut0->Sign | pCut1->Sign) > nLutSize )
1309                 continue;
1310             p->CutCount[1]++;
1311             if ( !Lf_CutMergeOrder(pCut0, pCut1, pCutsR[nCutsR], nLutSize) )
1312                 continue;
1313             if ( Lf_SetLastCutIsContained(pCutsR, nCutsR) )
1314                 continue;
1315             p->CutCount[2]++;
1316             if ( p->pPars->fCutMin && Lf_CutComputeTruth(p, pCut0, pCut1, fComp0, fComp1, pCutsR[nCutsR], fIsXor) )
1317                 pCutsR[nCutsR]->Sign = Lf_CutGetSign(pCutsR[nCutsR]);
1318             if ( p->pPars->nLutSizeMux && p->pPars->nLutSizeMux == (int)pCutsR[nCutsR]->nLeaves &&
1319                 Lf_ManFindCofVar(Lf_CutTruth(p,pCutsR[nCutsR]), Abc_Truth6WordNum(nLutSize), pCutsR[nCutsR]->nLeaves) == -1 )
1320                 continue;
1321             Lf_CutParams( p, pCutsR[nCutsR], Required, FlowRefs, pObj );
1322             nCutsR = Lf_SetAddCut( pCutsR, nCutsR, nCutNum );
1323         }
1324     }
1325     // debug printout
1326     if ( 0 )
1327     {
1328         printf( "*** Obj = %d  FlowRefs = %.2f  MapRefs = %2d  Required = %2d\n", iObj, FlowRefs, Lf_ObjMapRefNum(p, iObj), Required );
1329         for ( i = 0; i < nCutsR; i++ )
1330             Lf_CutPrint( p, pCutsR[i] );
1331         printf( "\n" );
1332     }
1333     // verify
1334     assert( nCutsR > 0 && nCutsR < nCutNum );
1335 //    assert( Lf_SetCheckArray(pCutsR, nCutsR) );
1336     // delay cut
1337     assert( nCutsR == 1 || pCutsR[0]->Delay <= pCutsR[1]->Delay );
1338     pBest->Cut[0].fUsed = pBest->Cut[1].fUsed = 0;
1339     pBest->Cut[0].Handle = pBest->Cut[1].Handle = Lf_MemSaveCut(&p->vStoreNew, pCutsR[0], iObj);
1340     pBest->Delay[0] = pBest->Delay[1] = pCutsR[0]->Delay;
1341     pBest->Flow[0] = pBest->Flow[1] = pCutsR[0]->Flow;
1342     p->nCutCounts[pCutsR[0]->nLeaves]++;
1343     p->CutCount[3] += nCutsR;
1344     p->nCutEqual++;
1345     // area cut
1346     iCutUsed = 0;
1347     if ( nCutsR > 1 && pCutsR[0]->Flow > pCutsR[1]->Flow + LF_EPSILON )//&& !pCutsR[1]->fLate ) // can remove !fLate
1348     {
1349         pBest->Cut[1].Handle = Lf_MemSaveCut(&p->vStoreNew, pCutsR[1], iObj);
1350         pBest->Delay[1] = pCutsR[1]->Delay;
1351         pBest->Flow[1] = pCutsR[1]->Flow;
1352         p->nCutCounts[pCutsR[1]->nLeaves]++;
1353         p->nCutEqual--;
1354         if ( !pCutsR[1]->fLate )
1355             iCutUsed = 1;
1356     }
1357     // mux cut
1358     if ( p->pPars->fUseMux7 && Gia_ObjIsMuxId(p->pGia, iObj) )
1359     {
1360         pCut2 = Lf_ObjCutMux( p, iObj );
1361         Lf_CutParams( p, pCut2, Required, FlowRefs, pObj );
1362         pBest->Delay[2] = pCut2->Delay;
1363         pBest->Flow[2] = pCut2->Flow;
1364         // update area value of the best area cut
1365 //        if ( !pCut2->fLate )
1366 //            pBest->Flow[1] = Abc_MinFloat( pBest->Flow[1], pBest->Flow[2] );
1367     }
1368     // reference resulting cut
1369     if ( p->fUseEla )
1370     {
1371         pBest->Cut[iCutUsed].fUsed = 1;
1372         if ( Lf_ObjMapRefNum(p, iObj) > 0 )
1373             Value2 = Lf_CutRef_rec( p, pCutsR[iCutUsed] );
1374 //        if ( Value1 < Value2 )
1375 //            printf( "ELA degradated cost at node %d from %d to %d.\n", iObj, Value1, Value2 ), fflush(stdout);
1376 //        assert( Value1 >= Value2 );
1377 //        if ( Value1 != -1 )
1378 //            printf( "%.2f -> %.2f    ", Value1, Value2 );
1379     }
1380     if ( pObj->Value == 0 )
1381         return;
1382     // store the cutset
1383     pCutSet = Lf_ManFetchSet(p, iObj);
1384     Lf_CutSetForEachCut( nCutWords, pCutSet, pCut0, i, nCutNum )
1385     {
1386         assert( !pCut0->fMux7 );
1387         if ( i < nCutsR )
1388             Lf_CutCopy( pCut0, pCutsR[i], nCutWords );
1389         else if ( i == nCutsR && pCutsR[0]->nLeaves > 1 && (nCutsR == 1 || pCutsR[1]->nLeaves > 1) )
1390             Lf_CutCreateUnit( pCut0, iObj );
1391         else
1392             pCut0->nLeaves = LF_NO_LEAF;
1393     }
1394 }
1395 
1396 /**Function*************************************************************
1397 
1398   Synopsis    [Computing delay/area.]
1399 
1400   Description []
1401 
1402   SideEffects []
1403 
1404   SeeAlso     []
1405 
1406 ***********************************************************************/
Lf_ManSetFlowRefInc(Gia_Man_t * p,Vec_Flt_t * vRefs,Vec_Int_t * vOffsets,int i)1407 static inline void Lf_ManSetFlowRefInc( Gia_Man_t * p, Vec_Flt_t * vRefs, Vec_Int_t * vOffsets, int i )
1408 {
1409     if ( Gia_ObjIsAndNotBuf(Gia_ManObj(p, i)) )
1410         Vec_FltAddToEntry( vRefs, Vec_IntEntry(vOffsets, i), 1 );
1411 }
Lf_ManSetFlowRefs(Gia_Man_t * p,Vec_Flt_t * vRefs,Vec_Int_t * vOffsets)1412 void Lf_ManSetFlowRefs( Gia_Man_t * p, Vec_Flt_t * vRefs, Vec_Int_t * vOffsets )
1413 {
1414     int fDiscount = 1;
1415     Gia_Obj_t * pObj, * pCtrl, * pData0, * pData1;
1416     int i, Id;
1417     Vec_FltFill( vRefs, Gia_ManAndNotBufNum(p), 0 );
1418     Gia_ManForEachAnd( p, pObj, i )
1419     {
1420         if ( Gia_ObjIsAndNotBuf(Gia_ObjFanin0(pObj)) )
1421             Vec_FltAddToEntry( vRefs, Vec_IntEntry(vOffsets, Gia_ObjFaninId0(pObj, i)), 1 );
1422         if ( Gia_ObjIsBuf(pObj) )
1423             continue;
1424         if ( Gia_ObjIsAndNotBuf(Gia_ObjFanin1(pObj)) )
1425             Vec_FltAddToEntry( vRefs, Vec_IntEntry(vOffsets, Gia_ObjFaninId1(pObj, i)), 1 );
1426         if ( p->pMuxes )
1427         {
1428             if ( Gia_ObjIsMuxId(p, i) && Gia_ObjIsAndNotBuf(Gia_ObjFanin2(p, pObj)) )
1429                 Vec_FltAddToEntry( vRefs, Vec_IntEntry(vOffsets, Gia_ObjFaninId2(p, i)), 1 );
1430         }
1431         else if ( fDiscount && Gia_ObjIsMuxType(pObj) ) // discount XOR/MUX
1432         {
1433             pCtrl  = Gia_Regular(Gia_ObjRecognizeMux(pObj, &pData1, &pData0));
1434             pData0 = Gia_Regular(pData0);
1435             pData1 = Gia_Regular(pData1);
1436             if ( Gia_ObjIsAndNotBuf(pCtrl) )
1437                 Vec_FltAddToEntry( vRefs, Vec_IntEntry(vOffsets, Gia_ObjId(p, pCtrl)), -1 );
1438             if ( pData0 == pData1 && Gia_ObjIsAndNotBuf(pData0) )
1439                 Vec_FltAddToEntry( vRefs, Vec_IntEntry(vOffsets, Gia_ObjId(p, pData0)), -1 );
1440         }
1441     }
1442     Gia_ManForEachCoDriverId( p, Id, i )
1443         if ( Gia_ObjIsAndNotBuf(Gia_ManObj(p, Id)) )
1444             Vec_FltAddToEntry( vRefs, Vec_IntEntry(vOffsets, Id), 1 );
1445     for ( i = 0; i < Vec_FltSize(vRefs); i++ )
1446         Vec_FltUpdateEntry( vRefs, i, 1 );
1447 }
Lf_ManSetCutRefs(Lf_Man_t * p)1448 void Lf_ManSetCutRefs( Lf_Man_t * p )
1449 {
1450     Gia_Obj_t * pObj; int i;
1451     if ( Vec_PtrSize(&p->vMemSets) * (1 << LF_LOG_PAGE) != Vec_IntSize(&p->vFreeSets) )
1452         printf( "The number of used cutsets = %d.\n", Vec_PtrSize(&p->vMemSets) * (1 << LF_LOG_PAGE) - Vec_IntSize(&p->vFreeSets) );
1453     Gia_ManForEachAnd( p->pGia, pObj, i )
1454     {
1455         assert( pObj->Value == 0 );
1456         if ( Gia_ObjIsBuf(pObj) )
1457             continue;
1458         if ( Gia_ObjIsAndNotBuf(Gia_ObjFanin0(pObj)) )
1459             Gia_ObjFanin0(pObj)->Value++;
1460         if ( Gia_ObjIsAndNotBuf(Gia_ObjFanin1(pObj)) )
1461             Gia_ObjFanin1(pObj)->Value++;
1462         if ( Gia_ObjIsMuxId(p->pGia, i) && Gia_ObjIsAndNotBuf(Gia_ObjFanin2(p->pGia, pObj)) )
1463             Gia_ObjFanin2(p->pGia, pObj)->Value++;
1464         if ( Gia_ObjSibl(p->pGia, i) && Gia_ObjIsAndNotBuf(Gia_ObjSiblObj(p->pGia, i)) )
1465             Gia_ObjSiblObj(p->pGia, i)->Value++;
1466     }
1467 }
1468 
Lf_ManSetMuxCut(Lf_Man_t * p,Lf_Bst_t * pBest,int iObj,int Required)1469 static inline int Lf_ManSetMuxCut( Lf_Man_t * p, Lf_Bst_t * pBest, int iObj, int Required )
1470 {
1471     Gia_Obj_t * pMux;
1472     if ( !Gia_ObjIsMuxId(p->pGia, iObj) )
1473         return 0;
1474     if ( pBest->Delay[2] > Required )
1475         return 0;
1476     if ( pBest->Flow[2] > 1.1 * pBest->Flow[1] )
1477         return 0;
1478     pMux = Gia_ManObj(p->pGia, iObj);
1479     if ( pMux->fMark0 || Gia_ObjFanin0(pMux)->fMark0 || Gia_ObjFanin1(pMux)->fMark0 )
1480         return 0;
1481     Gia_ObjFanin0(pMux)->fMark0 = 1;
1482     Gia_ObjFanin1(pMux)->fMark0 = 1;
1483     return 1;
1484 }
Lf_ManSetMapRefsOne(Lf_Man_t * p,int iObj)1485 void Lf_ManSetMapRefsOne( Lf_Man_t * p, int iObj )
1486 {
1487     Lf_Cut_t * pCut;
1488     Lf_Bst_t * pBest = Lf_ObjReadBest( p, iObj );
1489     int k, Index, Required = Lf_ObjRequired( p, iObj );
1490     assert( Lf_ObjMapRefNum(p, iObj) > 0 );
1491     assert( !pBest->Cut[0].fUsed && !pBest->Cut[1].fUsed );
1492     if ( !p->pPars->fUseMux7 || !Lf_ManSetMuxCut(p, pBest, iObj, Required) )
1493     {
1494         Index = (int)(Lf_BestDiffCuts(pBest) && pBest->Delay[1] <= Required);
1495         pBest->Cut[Index].fUsed = 1;
1496     }
1497     pCut = Lf_ObjCutBest( p, iObj );
1498     assert( !pCut->fMux7 || pCut->nLeaves == 3 );
1499 //    assert( pCut->Delay <= Required );
1500     for ( k = 0; k < (int)pCut->nLeaves; k++ )
1501     {
1502 //        if ( pCut->fMux7 && pCut->pLeaves[k] != Gia_ObjFaninId2(p->pGia, iObj) )
1503 //            Lf_ObjSetRequired( p, pCut->pLeaves[k], Required );
1504 //        else
1505             Lf_ObjSetRequired( p, pCut->pLeaves[k], Required - 1 );
1506         if ( Gia_ObjIsAndNotBuf(Gia_ManObj(p->pGia, pCut->pLeaves[k])) )
1507             Lf_ObjMapRefInc( p, pCut->pLeaves[k] );
1508     }
1509     if ( pCut->fMux7 )
1510     {
1511         p->pPars->Mux7++;
1512         p->pPars->Edge++;
1513         return;
1514     }
1515     if ( Vec_FltSize(&p->vSwitches) )
1516         p->Switches += Lf_CutSwitches(p, pCut);
1517     p->pPars->Edge += pCut->nLeaves;
1518     p->pPars->Area++;
1519 }
Lf_ManSetMapRefs(Lf_Man_t * p)1520 int Lf_ManSetMapRefs( Lf_Man_t * p )
1521 {
1522     float Coef = 1.0 / (1.0 + (p->Iter + 1) * (p->Iter + 1));
1523     float * pFlowRefs;
1524     int * pMapRefs, i;
1525     Gia_Obj_t * pObj;
1526     // compute delay
1527     int Delay = 0;
1528     for ( i = 0; i < Gia_ManCoNum(p->pGia); i++ )
1529         Delay = Abc_MaxInt( Delay, Lf_ObjCoArrival(p, i) );
1530     // check delay target
1531     if ( p->pPars->DelayTarget == -1 && p->pPars->nRelaxRatio )
1532         p->pPars->DelayTarget = (int)((float)Delay * (100.0 + p->pPars->nRelaxRatio) / 100.0);
1533     if ( p->pPars->DelayTarget != -1 )
1534     {
1535         if ( Delay < p->pPars->DelayTarget + 0.01 )
1536             Delay = p->pPars->DelayTarget;
1537         else if ( p->pPars->nRelaxRatio == 0 )
1538             Abc_Print( 0, "Relaxing user-specified delay target from %d to %d.\n", p->pPars->DelayTarget, Delay );
1539     }
1540     p->pPars->Delay = Delay;
1541     // compute area/edges/required
1542     p->pPars->Mux7 = p->pPars->Area = p->pPars->Edge = p->Switches = 0;
1543     Vec_IntFill( &p->vMapRefs, Gia_ManAndNotBufNum(p->pGia), 0 );
1544     Vec_IntFill( &p->vRequired, Gia_ManObjNum(p->pGia), ABC_INFINITY );
1545     if ( p->pPars->fUseMux7 )
1546     {
1547         Gia_ManCleanMark0(p->pGia);
1548         Gia_ManForEachCi( p->pGia, pObj, i )
1549             pObj->fMark0 = 1;
1550     }
1551     if ( p->pGia->pManTime != NULL )
1552     {
1553         assert( !Gia_ManBufNum(p->pGia) );
1554         Tim_ManIncrementTravId( (Tim_Man_t*)p->pGia->pManTime );
1555         if ( p->pPars->fDoAverage )
1556             for ( i = 0; i < Gia_ManCoNum(p->pGia); i++ )
1557                Tim_ManSetCoRequired( (Tim_Man_t*)p->pGia->pManTime, i, (int)(Lf_ObjCoArrival(p, i) * (100.0 + p->pPars->nRelaxRatio) / 100.0) );
1558         else
1559             Tim_ManInitPoRequiredAll( (Tim_Man_t*)p->pGia->pManTime, Delay );
1560         Gia_ManForEachObjReverse1( p->pGia, pObj, i )
1561         {
1562             if ( Gia_ObjIsBuf(pObj) )
1563                 Lf_ObjSetRequired( p, Gia_ObjFaninId0(pObj, i), Lf_ObjRequired(p, i) );
1564             else if ( Gia_ObjIsAnd(pObj) )
1565             {
1566                 if ( Lf_ObjMapRefNum(p, i) )
1567                     Lf_ManSetMapRefsOne( p, i );
1568             }
1569             else if ( Gia_ObjIsCi(pObj) )
1570                 Tim_ManSetCiRequired( (Tim_Man_t*)p->pGia->pManTime, Gia_ObjCioId(pObj), Lf_ObjRequired(p, i) );
1571             else if ( Gia_ObjIsCo(pObj) )
1572             {
1573                 int iDriverId = Gia_ObjFaninId0(pObj, i);
1574                 int reqTime = Tim_ManGetCoRequired( (Tim_Man_t*)p->pGia->pManTime, Gia_ObjCioId(pObj) );
1575                 Lf_ObjSetRequired( p, iDriverId, reqTime );
1576                 if ( Gia_ObjIsAndNotBuf(Gia_ObjFanin0(pObj)) )
1577                     Lf_ObjMapRefInc( p, iDriverId );
1578             }
1579             else assert( 0 );
1580         }
1581     }
1582     else
1583     {
1584         Gia_ManForEachCo( p->pGia, pObj, i )
1585         {
1586             int iDriverId = Gia_ObjFaninId0p(p->pGia, pObj);
1587             int reqTime = p->pPars->fDoAverage ? (int)(Lf_ObjCoArrival(p, i) * (100.0 + p->pPars->nRelaxRatio) / 100.0) : Delay;
1588             Lf_ObjSetRequired( p, iDriverId, reqTime );
1589             if ( Gia_ObjIsAndNotBuf(Gia_ObjFanin0(pObj)) )
1590                 Lf_ObjMapRefInc( p, iDriverId );
1591         }
1592         Gia_ManForEachAndReverse( p->pGia, pObj, i )
1593         {
1594             if ( Gia_ObjIsBuf(pObj) )
1595             {
1596                 Lf_ObjSetRequired( p, Gia_ObjFaninId0(pObj, i), Lf_ObjRequired(p, i) );
1597                 if ( Gia_ObjIsAndNotBuf(Gia_ObjFanin0(pObj)) )
1598                     Lf_ObjMapRefInc( p, Gia_ObjFaninId0(pObj, i) );
1599             }
1600             else if ( Lf_ObjMapRefNum(p, i) )
1601                 Lf_ManSetMapRefsOne( p, i );
1602         }
1603     }
1604     if ( p->pPars->fUseMux7 )
1605         Gia_ManCleanMark0(p->pGia);
1606     // blend references
1607     assert( Vec_IntSize(&p->vMapRefs)  == Gia_ManAndNotBufNum(p->pGia) );
1608     assert( Vec_FltSize(&p->vFlowRefs) == Gia_ManAndNotBufNum(p->pGia) );
1609     pMapRefs  = Vec_IntArray(&p->vMapRefs);
1610     pFlowRefs = Vec_FltArray(&p->vFlowRefs);
1611     for ( i = 0; i < Vec_IntSize(&p->vMapRefs); i++ )
1612         pFlowRefs[i] = Coef * pFlowRefs[i] + (1.0 - Coef) * Abc_MaxFloat(1, pMapRefs[i]);
1613 //        pFlowRefs[i] = 0.2 * pFlowRefs[i] + 0.8 * Abc_MaxFloat(1, pMapRefs[i]);
1614     return p->pPars->Area;
1615 }
1616 
Lf_ManCountMapRefsOne(Lf_Man_t * p,int iObj)1617 void Lf_ManCountMapRefsOne( Lf_Man_t * p, int iObj )
1618 {
1619     Lf_Bst_t * pBest = Lf_ObjReadBest( p, iObj );
1620     Lf_Cut_t * pCut = Lf_ObjCutBest( p, iObj );
1621     int k ,Required = Lf_ObjRequired( p, iObj );
1622     assert( Lf_ObjMapRefNum(p, iObj) > 0 );
1623     assert( Lf_BestIsMapped(pBest) );
1624     assert( !pCut->fMux7 );
1625 //    assert( pCut->Delay <= Required );
1626     for ( k = 0; k < (int)pCut->nLeaves; k++ )
1627             Lf_ObjSetRequired( p, pCut->pLeaves[k], Required - 1 );
1628     if ( Vec_FltSize(&p->vSwitches) )
1629         p->Switches += Lf_CutSwitches(p, pCut);
1630     p->pPars->Edge += pCut->nLeaves;
1631     p->pPars->Area++;
1632 }
Lf_ManCountMapRefs(Lf_Man_t * p)1633 void Lf_ManCountMapRefs( Lf_Man_t * p )
1634 {
1635     // compute delay
1636     Gia_Obj_t * pObj;
1637     int i, Id, Delay = 0;
1638     for ( i = 0; i < Gia_ManCoNum(p->pGia); i++ )
1639         Delay = Abc_MaxInt( Delay, Lf_ObjCoArrival2(p, i) );
1640     // check delay target
1641     if ( p->pPars->DelayTarget == -1 && p->pPars->nRelaxRatio )
1642         p->pPars->DelayTarget = (int)((float)Delay * (100.0 + p->pPars->nRelaxRatio) / 100.0);
1643     if ( p->pPars->DelayTarget != -1 )
1644     {
1645         if ( Delay < p->pPars->DelayTarget + 0.01 )
1646             Delay = p->pPars->DelayTarget;
1647         else if ( p->pPars->nRelaxRatio == 0 )
1648             Abc_Print( 0, "Relaxing user-specified delay target from %d to %d.\n", p->pPars->DelayTarget, Delay );
1649     }
1650     p->pPars->Delay = Delay;
1651     // compute area/edges/required
1652     p->pPars->Mux7 = p->pPars->Area = p->pPars->Edge = p->Switches = 0;
1653     Vec_IntFill( &p->vRequired, Gia_ManObjNum(p->pGia), ABC_INFINITY );
1654     if ( p->pPars->fUseMux7 )
1655         Gia_ManCleanMark0(p->pGia);
1656     if ( p->pGia->pManTime != NULL )
1657     {
1658         Tim_ManIncrementTravId( (Tim_Man_t*)p->pGia->pManTime );
1659         if ( p->pPars->fDoAverage )
1660             for ( i = 0; i < Gia_ManCoNum(p->pGia); i++ )
1661                Tim_ManSetCoRequired( (Tim_Man_t*)p->pGia->pManTime, i, (int)(Lf_ObjCoArrival(p, i) * (100.0 + p->pPars->nRelaxRatio) / 100.0) );
1662         else
1663             Tim_ManInitPoRequiredAll( (Tim_Man_t*)p->pGia->pManTime, Delay );
1664         Gia_ManForEachObjReverse1( p->pGia, pObj, i )
1665         {
1666             if ( Gia_ObjIsBuf(pObj) )
1667                 Lf_ObjSetRequired( p, Gia_ObjFaninId0(pObj, i), Lf_ObjRequired(p, i) );
1668             else if ( Gia_ObjIsAnd(pObj) )
1669             {
1670                 if ( Lf_ObjMapRefNum(p, i) )
1671                     Lf_ManCountMapRefsOne( p, i );
1672             }
1673             else if ( Gia_ObjIsCi(pObj) )
1674                 Tim_ManSetCiRequired( (Tim_Man_t*)p->pGia->pManTime, Gia_ObjCioId(pObj), Lf_ObjRequired(p, i) );
1675             else if ( Gia_ObjIsCo(pObj) )
1676             {
1677                 int reqTime = Tim_ManGetCoRequired( (Tim_Man_t*)p->pGia->pManTime, Gia_ObjCioId(pObj) );
1678                 Lf_ObjSetRequired( p, Gia_ObjFaninId0(pObj, i), reqTime );
1679             }
1680             else assert( 0 );
1681         }
1682     }
1683     else
1684     {
1685         Gia_ManForEachCoDriverId( p->pGia, Id, i )
1686             Lf_ObjSetRequired( p, Id, p->pPars->fDoAverage ? (int)(Lf_ObjCoArrival(p, i) * (100.0 + p->pPars->nRelaxRatio) / 100.0) : Delay );
1687         Gia_ManForEachAndReverse( p->pGia, pObj, i )
1688             if ( Gia_ObjIsBuf(pObj) )
1689                 Lf_ObjSetRequired( p, Gia_ObjFaninId0(pObj, i), Lf_ObjRequired(p, i) );
1690             else if ( Lf_ObjMapRefNum(p, i) )
1691                 Lf_ManCountMapRefsOne( p, i );
1692     }
1693     if ( p->pPars->fUseMux7 )
1694         Gia_ManCleanMark0(p->pGia);
1695 }
1696 
1697 
1698 /**Function*************************************************************
1699 
1700   Synopsis    []
1701 
1702   Description []
1703 
1704   SideEffects []
1705 
1706   SeeAlso     []
1707 
1708 ***********************************************************************/
Lf_ManDeriveMapping(Lf_Man_t * p)1709 Gia_Man_t * Lf_ManDeriveMapping( Lf_Man_t * p )
1710 {
1711     Vec_Int_t * vMapping;
1712     Lf_Cut_t * pCut;
1713     int i, k;
1714     assert( !p->pPars->fCutMin && p->pGia->vMapping == NULL );
1715     vMapping = Vec_IntAlloc( Gia_ManObjNum(p->pGia) + (int)p->pPars->Edge + (int)p->pPars->Area * 2 );
1716     Vec_IntFill( vMapping, Gia_ManObjNum(p->pGia), 0 );
1717     Gia_ManForEachAndId( p->pGia, i )
1718     {
1719         if ( !Lf_ObjMapRefNum(p, i) )
1720             continue;
1721         assert( !Gia_ObjIsBuf(Gia_ManObj(p->pGia,i)) );
1722         pCut = Lf_ObjCutBest( p, i );
1723         assert( !pCut->fMux7 );
1724         Vec_IntWriteEntry( vMapping, i, Vec_IntSize(vMapping) );
1725         Vec_IntPush( vMapping, pCut->nLeaves );
1726         for ( k = 0; k < (int)pCut->nLeaves; k++ )
1727             Vec_IntPush( vMapping, pCut->pLeaves[k] );
1728         Vec_IntPush( vMapping, i );
1729     }
1730     assert( Vec_IntCap(vMapping) == 16 || Vec_IntSize(vMapping) == Vec_IntCap(vMapping) );
1731     p->pGia->vMapping = vMapping;
1732     return p->pGia;
1733 }
Lf_ManDeriveMappingCoarse(Lf_Man_t * p)1734 Gia_Man_t * Lf_ManDeriveMappingCoarse( Lf_Man_t * p )
1735 {
1736     Gia_Man_t * pNew, * pGia = p->pGia;
1737     Gia_Obj_t * pObj;
1738     Lf_Cut_t * pCut;
1739     int i, k;
1740     assert( !p->pPars->fCutMin && pGia->pMuxes );
1741     // create new manager
1742     pNew = Gia_ManStart( Gia_ManObjNum(pGia) );
1743     pNew->pName = Abc_UtilStrsav( pGia->pName );
1744     pNew->pSpec = Abc_UtilStrsav( pGia->pSpec );
1745     // start mapping
1746     pNew->vMapping = Vec_IntAlloc( Gia_ManObjNum(pGia) + 2*Gia_ManXorNum(pGia) + 2*Gia_ManMuxNum(pGia) + (int)p->pPars->Edge + 2*(int)p->pPars->Area + 4*(int)p->pPars->Mux7 );
1747     Vec_IntFill( pNew->vMapping, Gia_ManObjNum(pGia) + 2*Gia_ManXorNum(pGia) + 2*Gia_ManMuxNum(pGia), 0 );
1748     // process objects
1749     Gia_ManConst0(pGia)->Value = 0;
1750     Gia_ManForEachObj1( pGia, pObj, i )
1751     {
1752         if ( Gia_ObjIsCi(pObj) )
1753             { pObj->Value = Gia_ManAppendCi( pNew ); continue; }
1754         if ( Gia_ObjIsCo(pObj) )
1755             { pObj->Value = Gia_ManAppendCo( pNew, Gia_ObjFanin0Copy(pObj) ); continue; }
1756         if ( Gia_ObjIsBuf(pObj) )
1757             { pObj->Value = Gia_ManAppendBuf( pNew, Gia_ObjFanin0Copy(pObj) ); continue; }
1758         if ( Gia_ObjIsMuxId(pGia, i) )
1759             pObj->Value = Gia_ManAppendMux( pNew, Gia_ObjFanin2Copy(pGia, pObj), Gia_ObjFanin1Copy(pObj), Gia_ObjFanin0Copy(pObj) );
1760         else if ( Gia_ObjIsXor(pObj) )
1761             pObj->Value = Gia_ManAppendXor( pNew, Gia_ObjFanin0Copy(pObj), Gia_ObjFanin1Copy(pObj) );
1762         else
1763             pObj->Value = Gia_ManAppendAnd( pNew, Gia_ObjFanin0Copy(pObj), Gia_ObjFanin1Copy(pObj) );
1764         if ( !Lf_ObjMapRefNum(p, i) )
1765             continue;
1766         pCut = Lf_ObjCutBest( p, i );
1767         Vec_IntWriteEntry( pNew->vMapping, Abc_Lit2Var(pObj->Value), Vec_IntSize(pNew->vMapping) );
1768         Vec_IntPush( pNew->vMapping, pCut->nLeaves );
1769         for ( k = 0; k < (int)pCut->nLeaves; k++ )
1770             Vec_IntPush( pNew->vMapping, Abc_Lit2Var(Gia_ManObj(pGia, pCut->pLeaves[k])->Value) );
1771         Vec_IntPush( pNew->vMapping, pCut->fMux7 ? -Abc_Lit2Var(pObj->Value) : Abc_Lit2Var(pObj->Value) );
1772     }
1773     Gia_ManSetRegNum( pNew, Gia_ManRegNum(pGia) );
1774     assert( Vec_IntCap(pNew->vMapping) == 16 || Vec_IntSize(pNew->vMapping) == Vec_IntCap(pNew->vMapping) );
1775     return pNew;
1776 }
Lf_ManDerivePart(Lf_Man_t * p,Gia_Man_t * pNew,Vec_Int_t * vMapping,Vec_Int_t * vMapping2,Vec_Int_t * vCopies,Lf_Cut_t * pCut,Vec_Int_t * vLeaves,Vec_Int_t * vCover,Gia_Obj_t * pObj)1777 static inline int Lf_ManDerivePart( Lf_Man_t * p, Gia_Man_t * pNew, Vec_Int_t * vMapping, Vec_Int_t * vMapping2, Vec_Int_t * vCopies, Lf_Cut_t * pCut, Vec_Int_t * vLeaves, Vec_Int_t * vCover, Gia_Obj_t * pObj )
1778 {
1779     word * pTruth;
1780     int k, iLit, iTemp;
1781     if ( p->pPars->nLutSizeMux && p->pPars->nLutSizeMux == (int)pCut->nLeaves )
1782     {
1783         word pTruthCof[LF_TT_WORDS], * pTruth = Lf_CutTruth( p, pCut );
1784         int pVarsNew[LF_LEAF_MAX], nVarsNew, iLitCofs[2];
1785         int LutSize   = p->pPars->nLutSize;
1786         int nWords    = Abc_Truth6WordNum(LutSize);
1787         int c, iVar   = Lf_ManFindCofVar( pTruth, nWords, pCut->nLeaves );
1788         assert( iVar >= 0 && iVar < (int)pCut->nLeaves );
1789         for ( c = 0; c < 2; c++ )
1790         {
1791             for ( k = 0; k < (int)pCut->nLeaves; k++ )
1792                 pVarsNew[k] = k;
1793             if ( c )
1794                 Abc_TtCofactor1p( pTruthCof, pTruth, nWords, iVar );
1795             else
1796                 Abc_TtCofactor0p( pTruthCof, pTruth, nWords, iVar );
1797             nVarsNew = Abc_TtMinBase( pTruthCof, pVarsNew, pCut->nLeaves, LutSize );
1798             assert( nVarsNew > 0 );
1799             // derive LUT
1800             Vec_IntClear( vLeaves );
1801             for ( k = 0; k < nVarsNew; k++ )
1802                 Vec_IntPush( vLeaves, Vec_IntEntry(vCopies, pCut->pLeaves[pVarsNew[k]]) );
1803             iLitCofs[c] = Kit_TruthToGia( pNew, (unsigned *)pTruthCof, nVarsNew, vCover, vLeaves, 0 );
1804             // create mapping
1805             Vec_IntSetEntry( vMapping, Abc_Lit2Var(iLitCofs[c]), Vec_IntSize(vMapping2) );
1806             Vec_IntPush( vMapping2, Vec_IntSize(vLeaves) );
1807             Vec_IntForEachEntry( vLeaves, iTemp, k )
1808                 Vec_IntPush( vMapping2, Abc_Lit2Var(iTemp) );
1809             Vec_IntPush( vMapping2, Abc_Lit2Var(iLitCofs[c]) );
1810         }
1811         // derive MUX
1812         pTruthCof[0] = ABC_CONST(0xCACACACACACACACA);
1813         Vec_IntClear( vLeaves );
1814         Vec_IntPush( vLeaves, iLitCofs[0] );
1815         Vec_IntPush( vLeaves, iLitCofs[1] );
1816         Vec_IntPush( vLeaves, Vec_IntEntry(vCopies, pCut->pLeaves[iVar]) );
1817         iLit = Kit_TruthToGia( pNew, (unsigned *)pTruthCof, Vec_IntSize(vLeaves), vCover, vLeaves, 0 );
1818         // create mapping
1819         Vec_IntSetEntry( vMapping, Abc_Lit2Var(iLit), Vec_IntSize(vMapping2) );
1820         Vec_IntPush( vMapping2, Vec_IntSize(vLeaves) );
1821         Vec_IntForEachEntry( vLeaves, iTemp, k )
1822             Vec_IntPush( vMapping2, Abc_Lit2Var(iTemp) );
1823         Vec_IntPush( vMapping2, -Abc_Lit2Var(iLit) );
1824         return iLit;
1825     }
1826     Vec_IntClear( vLeaves );
1827     if ( pCut->fMux7 )
1828     {
1829         assert( pCut->nLeaves == 3 );
1830         Vec_IntPush( vLeaves, Abc_LitNotCond(Vec_IntEntry(vCopies, pCut->pLeaves[0]), Gia_ObjFaninC0(pObj)) );
1831         Vec_IntPush( vLeaves, Abc_LitNotCond(Vec_IntEntry(vCopies, pCut->pLeaves[1]), Gia_ObjFaninC1(pObj)) );
1832         Vec_IntPush( vLeaves, Abc_LitNotCond(Vec_IntEntry(vCopies, pCut->pLeaves[2]), Gia_ObjFaninC2(p->pGia,pObj)) );
1833     }
1834     else
1835     {
1836         for ( k = 0; k < (int)pCut->nLeaves; k++ )
1837             Vec_IntPush( vLeaves, Vec_IntEntry(vCopies, pCut->pLeaves[k]) );
1838     }
1839     pTruth = Lf_CutTruth( p, pCut );
1840     iLit = Kit_TruthToGia( pNew, (unsigned *)pTruth, Vec_IntSize(vLeaves), vCover, vLeaves, 0 );
1841     // create mapping
1842     Vec_IntSetEntry( vMapping, Abc_Lit2Var(iLit), Vec_IntSize(vMapping2) );
1843     Vec_IntPush( vMapping2, Vec_IntSize(vLeaves) );
1844     Vec_IntForEachEntry( vLeaves, iTemp, k )
1845         Vec_IntPush( vMapping2, Abc_Lit2Var(iTemp) );
1846     Vec_IntPush( vMapping2, pCut->fMux7 ? -Abc_Lit2Var(iLit) : Abc_Lit2Var(iLit) );
1847     return iLit;
1848 }
Lf_ManDeriveMappingGia(Lf_Man_t * p)1849 Gia_Man_t * Lf_ManDeriveMappingGia( Lf_Man_t * p )
1850 {
1851     Gia_Man_t * pNew;
1852     Gia_Obj_t * pObj;
1853     Vec_Int_t * vCopies   = Vec_IntStartFull( Gia_ManObjNum(p->pGia) );
1854     Vec_Int_t * vMapping  = Vec_IntStart( 2*Gia_ManObjNum(p->pGia) + (int)p->pPars->Edge + 2*(int)p->pPars->Area + 4*(int)p->pPars->Mux7 );
1855     Vec_Int_t * vMapping2 = Vec_IntStart( (int)p->pPars->Edge + 2*(int)p->pPars->Area + 1000 );
1856     Vec_Int_t * vCover    = Vec_IntAlloc( 1 << 16 );
1857     Vec_Int_t * vLeaves   = Vec_IntAlloc( 16 );
1858     Lf_Cut_t * pCut;
1859     int i, iLit;
1860     assert( p->pPars->fCutMin );
1861     // create new manager
1862     pNew = Gia_ManStart( Gia_ManObjNum(p->pGia) );
1863     pNew->pName = Abc_UtilStrsav( p->pGia->pName );
1864     pNew->pSpec = Abc_UtilStrsav( p->pGia->pSpec );
1865     Vec_IntWriteEntry( vCopies, 0, 0 );
1866     Gia_ManForEachObj1( p->pGia, pObj, i )
1867     {
1868         if ( Gia_ObjIsCi(pObj) )
1869         {
1870             Vec_IntWriteEntry( vCopies, i, Gia_ManAppendCi(pNew) );
1871             continue;
1872         }
1873         if ( Gia_ObjIsCo(pObj) )
1874         {
1875             iLit = Vec_IntEntry( vCopies, Gia_ObjFaninId0p(p->pGia, pObj) );
1876             iLit = Gia_ManAppendCo( pNew, Abc_LitNotCond(iLit, Gia_ObjFaninC0(pObj)) );
1877             continue;
1878         }
1879         if ( Gia_ObjIsBuf(pObj) )
1880         {
1881             iLit = Vec_IntEntry( vCopies, Gia_ObjFaninId0p(p->pGia, pObj) );
1882             iLit = Gia_ManAppendBuf( pNew, Abc_LitNotCond(iLit, Gia_ObjFaninC0(pObj)) );
1883             Vec_IntWriteEntry( vCopies, i, iLit );
1884             continue;
1885         }
1886         if ( !Lf_ObjMapRefNum(p, i) )
1887             continue;
1888         pCut = Lf_ObjCutBest( p, i );
1889         assert( pCut->iFunc >= 0 );
1890         if ( pCut->nLeaves == 0 )
1891         {
1892             assert( Abc_Lit2Var(pCut->iFunc) == 0 );
1893             Vec_IntWriteEntry( vCopies, i, pCut->iFunc );
1894             continue;
1895         }
1896         if ( pCut->nLeaves == 1 )
1897         {
1898             assert( Abc_Lit2Var(pCut->iFunc) == 1 );
1899             iLit = Vec_IntEntry( vCopies, pCut->pLeaves[0] );
1900             Vec_IntWriteEntry( vCopies, i, Abc_LitNotCond(iLit, Abc_LitIsCompl(pCut->iFunc)) );
1901             continue;
1902         }
1903         iLit = Lf_ManDerivePart( p, pNew, vMapping, vMapping2, vCopies, pCut, vLeaves, vCover, pObj );
1904         Vec_IntWriteEntry( vCopies, i, Abc_LitNotCond(iLit, Abc_LitIsCompl(pCut->iFunc)) );
1905     }
1906     Vec_IntFree( vCopies );
1907     Vec_IntFree( vCover );
1908     Vec_IntFree( vLeaves );
1909     // finish mapping
1910     if ( Vec_IntSize(vMapping) > Gia_ManObjNum(pNew) )
1911         Vec_IntShrink( vMapping, Gia_ManObjNum(pNew) );
1912     else
1913         Vec_IntFillExtra( vMapping, Gia_ManObjNum(pNew), 0 );
1914     assert( Vec_IntSize(vMapping) == Gia_ManObjNum(pNew) );
1915     Vec_IntForEachEntry( vMapping, iLit, i )
1916         if ( iLit > 0 )
1917             Vec_IntAddToEntry( vMapping, i, Gia_ManObjNum(pNew) );
1918     Vec_IntAppend( vMapping, vMapping2 );
1919     Vec_IntFree( vMapping2 );
1920     // attach mapping and packing
1921     assert( pNew->vMapping == NULL );
1922     pNew->vMapping = vMapping;
1923     Gia_ManSetRegNum( pNew, Gia_ManRegNum(p->pGia) );
1924     return pNew;
1925 }
1926 
1927 /**Function*************************************************************
1928 
1929   Synopsis    []
1930 
1931   Description []
1932 
1933   SideEffects []
1934 
1935   SeeAlso     []
1936 
1937 ***********************************************************************/
Lf_ManAlloc(Gia_Man_t * pGia,Jf_Par_t * pPars)1938 Lf_Man_t * Lf_ManAlloc( Gia_Man_t * pGia, Jf_Par_t * pPars )
1939 {
1940     Lf_Man_t * p; int i, k = 0;
1941     assert( pPars->nCutNum > 1  && pPars->nCutNum <= LF_CUT_MAX );
1942     assert( pPars->nLutSize > 1 && pPars->nLutSize <= LF_LEAF_MAX );
1943     ABC_FREE( pGia->pRefs );
1944     Vec_IntFreeP( &pGia->vMapping );
1945     Gia_ManCleanValue( pGia );
1946     if ( Gia_ManHasChoices(pGia) )
1947         Gia_ManSetPhase(pGia);
1948     p = ABC_CALLOC( Lf_Man_t, 1 );
1949     Lf_ManAnalyzeCoDrivers( pGia, &p->nCoDrivers, &p->nInverters );
1950     if ( pPars->fPower )
1951         Lf_ManComputeSwitching( pGia, &p->vSwitches );
1952     p->clkStart  = Abc_Clock();
1953     p->pGia      = pGia;
1954     p->pPars     = pPars;
1955     p->nCutWords = (sizeof(Lf_Cut_t)/sizeof(int) + pPars->nLutSize + 1) >> 1;
1956     p->nSetWords = p->nCutWords * pPars->nCutNum;
1957     p->vTtMem    = pPars->fCutMin ? Vec_MemAllocForTT( pPars->nLutSize, 0 ) : NULL;
1958     if ( pPars->fCutMin && pPars->fUseMux7 )
1959         Vec_MemAddMuxTT( p->vTtMem, pPars->nLutSize );
1960     p->pObjBests = ABC_CALLOC( Lf_Bst_t, Gia_ManAndNotBufNum(pGia) );
1961     Vec_IntGrow( &p->vFreeSets, (1<<14) );
1962     Vec_PtrGrow( &p->vFreePages, 256 );
1963     Lf_MemAlloc( &p->vStoreOld, 16, &p->vFreePages, p->nCutWords );
1964     Lf_MemAlloc( &p->vStoreNew, 16, &p->vFreePages, p->nCutWords );
1965     Vec_IntFill( &p->vOffsets,  Gia_ManObjNum(pGia), -1 );
1966     Vec_IntFill( &p->vRequired, Gia_ManObjNum(pGia), ABC_INFINITY );
1967     Vec_IntFill( &p->vCutSets,  Gia_ManAndNotBufNum(pGia), -1 );
1968     Vec_FltFill( &p->vFlowRefs, Gia_ManAndNotBufNum(pGia), 0 );
1969     Vec_IntFill( &p->vMapRefs,  Gia_ManAndNotBufNum(pGia), 0 );
1970     Vec_IntFill( &p->vCiArrivals, Gia_ManCiNum(pGia), 0 );
1971     Gia_ManForEachAndId( pGia, i )
1972         if ( !Gia_ObjIsBuf(Gia_ManObj(pGia, i)) )
1973             Vec_IntWriteEntry( &p->vOffsets, i, k++ );
1974     assert( k == Gia_ManAndNotBufNum(pGia) );
1975     Lf_ManSetFlowRefs( pGia, &p->vFlowRefs, &p->vOffsets );
1976     if ( pPars->pTimesArr )
1977         for ( i = 0; i < Gia_ManPiNum(pGia); i++ )
1978             Vec_IntWriteEntry( &p->vCiArrivals, i, pPars->pTimesArr[i] );
1979     return p;
1980 }
Lf_ManFree(Lf_Man_t * p)1981 void Lf_ManFree( Lf_Man_t * p )
1982 {
1983     ABC_FREE( p->pPars->pTimesArr );
1984     ABC_FREE( p->pPars->pTimesReq );
1985     if ( p->pPars->fCutMin )
1986         Vec_MemHashFree( p->vTtMem );
1987     if ( p->pPars->fCutMin )
1988         Vec_MemFree( p->vTtMem );
1989     Vec_PtrFreeData( &p->vMemSets );
1990     Vec_PtrFreeData( &p->vFreePages );
1991     Vec_PtrFreeData( &p->vStoreOld.vPages );
1992     Vec_PtrFreeData( &p->vStoreNew.vPages );
1993     ABC_FREE( p->vMemSets.pArray );
1994     ABC_FREE( p->vFreePages.pArray );
1995     ABC_FREE( p->vStoreOld.vPages.pArray );
1996     ABC_FREE( p->vStoreNew.vPages.pArray );
1997     ABC_FREE( p->vFreePages.pArray );
1998     ABC_FREE( p->vFreeSets.pArray );
1999     ABC_FREE( p->vOffsets.pArray );
2000     ABC_FREE( p->vRequired.pArray );
2001     ABC_FREE( p->vCutSets.pArray );
2002     ABC_FREE( p->vFlowRefs.pArray );
2003     ABC_FREE( p->vMapRefs.pArray );
2004     ABC_FREE( p->vSwitches.pArray );
2005     ABC_FREE( p->vCiArrivals.pArray );
2006     ABC_FREE( p->pObjBests );
2007     ABC_FREE( p );
2008 }
2009 
2010 /**Function*************************************************************
2011 
2012   Synopsis    []
2013 
2014   Description []
2015 
2016   SideEffects []
2017 
2018   SeeAlso     []
2019 
2020 ***********************************************************************/
Lf_ManSetDefaultPars(Jf_Par_t * pPars)2021 void Lf_ManSetDefaultPars( Jf_Par_t * pPars )
2022 {
2023     memset( pPars, 0, sizeof(Jf_Par_t) );
2024     pPars->nLutSize     =  6;
2025     pPars->nCutNum      =  8;
2026     pPars->nProcNum     =  0;
2027     pPars->nRounds      =  4;
2028     pPars->nRoundsEla   =  1;
2029     pPars->nRelaxRatio  =  0;
2030     pPars->nCoarseLimit =  3;
2031     pPars->nAreaTuner   =  1;
2032     pPars->nVerbLimit   =  5;
2033     pPars->DelayTarget  = -1;
2034     pPars->fAreaOnly    =  0;
2035     pPars->fOptEdge     =  1;
2036     pPars->fUseMux7     =  0;
2037     pPars->fPower       =  0;
2038     pPars->fCoarsen     =  1;
2039     pPars->fCutMin      =  0;
2040     pPars->fFuncDsd     =  0;
2041     pPars->fGenCnf      =  0;
2042     pPars->fPureAig     =  0;
2043     pPars->fCutHashing  =  0;
2044     pPars->fCutSimple   =  0;
2045     pPars->fVerbose     =  0;
2046     pPars->fVeryVerbose =  0;
2047     pPars->nLutSizeMax  =  LF_LEAF_MAX;
2048     pPars->nCutNumMax   =  LF_CUT_MAX;
2049 }
Lf_ManPrintStats(Lf_Man_t * p,char * pTitle)2050 void Lf_ManPrintStats( Lf_Man_t * p, char * pTitle )
2051 {
2052     if ( !p->pPars->fVerbose )
2053         return;
2054     printf( "%s :  ", pTitle );
2055     printf( "Level =%6lu   ",   (long)p->pPars->Delay );
2056     printf( "Area =%9lu   ",    (long)p->pPars->Area );
2057     printf( "Edge =%9lu   ",    (long)p->pPars->Edge );
2058     printf( "LUT =%9lu  ",      (long)p->pPars->Area+p->nInverters );
2059     if ( Vec_FltSize(&p->vSwitches) )
2060         printf( "Swt =%8.1f  ", p->Switches );
2061     if ( p->pPars->fUseMux7 )
2062         printf( "Mux7 =%7lu  ", (long)p->pPars->Mux7 );
2063     Abc_PrintTime( 1, "Time", Abc_Clock() - p->clkStart );
2064     fflush( stdout );
2065 }
Lf_ManPrintInit(Lf_Man_t * p)2066 void Lf_ManPrintInit( Lf_Man_t * p )
2067 {
2068     if ( !p->pPars->fVerbose )
2069         return;
2070     printf( "LutSize = %d  ", p->pPars->nLutSize );
2071     printf( "CutNum = %d  ",  p->pPars->nCutNum );
2072     printf( "Iter = %d  ",    p->pPars->nRounds + p->pPars->nRoundsEla );
2073     if ( p->pPars->nRelaxRatio )
2074     printf( "Ratio = %d  ",   p->pPars->nRelaxRatio );
2075     printf( "Edge = %d  ",    p->pPars->fOptEdge );
2076     if ( p->pPars->DelayTarget != -1 )
2077     printf( "Delay = %d  ",   p->pPars->DelayTarget );
2078     printf( "CutMin = %d  ",  p->pPars->fCutMin );
2079     printf( "Coarse = %d  ",  p->pPars->fCoarsen );
2080     printf( "Cut/Set = %d/%d Bytes", 8*p->nCutWords, 8*p->nSetWords );
2081     printf( "\n" );
2082     printf( "Computing cuts...\r" );
2083     fflush( stdout );
2084 }
Lf_ManPrintQuit(Lf_Man_t * p,Gia_Man_t * pNew)2085 void Lf_ManPrintQuit( Lf_Man_t * p, Gia_Man_t * pNew )
2086 {
2087     float MemGia   = Gia_ManMemory(p->pGia) / (1<<20);
2088     float MemMan   = 1.0 * sizeof(int) * (2 * Gia_ManObjNum(p->pGia) + 3 * Gia_ManAndNotBufNum(p->pGia)) / (1<<20); // offset, required, cutsets, maprefs, flowrefs
2089     float MemCutsB = 1.0 * (p->vStoreOld.MaskPage + 1) * (Vec_PtrSize(&p->vFreePages) + Vec_PtrSize(&p->vStoreOld.vPages)) / (1<<20) + 1.0 * sizeof(Lf_Bst_t) * Gia_ManAndNotBufNum(p->pGia) / (1<<20);
2090     float MemCutsF = 1.0 * sizeof(word) * p->nSetWords * (1<<LF_LOG_PAGE) * Vec_PtrSize(&p->vMemSets) / (1<<20);
2091     float MemTt    = p->vTtMem ? Vec_MemMemory(p->vTtMem) / (1<<20) : 0;
2092     float MemMap   = Vec_IntMemory(pNew->vMapping) / (1<<20);
2093     if ( p->CutCount[0] == 0 )
2094         p->CutCount[0] = 1;
2095     if ( !p->pPars->fVerbose )
2096     {
2097         int i, CountOver[2] = {0};
2098         int nLutSize = p->pPars->fCutGroup ? p->pPars->nLutSize/2 : p->pPars->nLutSize;
2099         Gia_ManForEachLut( pNew, i )
2100             CountOver[Gia_ObjLutSize(pNew, i) > nLutSize]++;
2101         if ( p->pPars->fCutGroup )
2102             printf( "Created %d regular %d-LUTs and %d dual %d-LUTs. The total of %d %d-LUTs.\n",
2103             CountOver[0], nLutSize, CountOver[1], nLutSize, CountOver[0] + 2*CountOver[1], nLutSize );
2104         return;
2105     }
2106     printf( "CutPair = %.0f  ",         p->CutCount[0] );
2107     printf( "Merge = %.0f (%.2f %%)  ", p->CutCount[1], 100.0*p->CutCount[1]/p->CutCount[0] );
2108     printf( "Eval = %.0f (%.2f %%)  ",  p->CutCount[2], 100.0*p->CutCount[2]/p->CutCount[0] );
2109     printf( "Cut = %.0f (%.2f %%)  ",   p->CutCount[3], 100.0*p->CutCount[3]/p->CutCount[0] );
2110     printf( "\n" );
2111     printf( "Gia = %.2f MB  ",          MemGia );
2112     printf( "Man = %.2f MB  ",          MemMan );
2113     printf( "Best = %.2f MB  ",         MemCutsB );
2114     printf( "Front = %.2f MB   ",       MemCutsF );
2115     printf( "Map = %.2f MB  ",          MemMap );
2116     printf( "TT = %.2f MB  ",           MemTt );
2117     printf( "Total = %.2f MB",          MemGia + MemMan + MemCutsB + MemCutsF + MemMap + MemTt );
2118     printf( "\n" );
2119     if ( 1 )
2120     {
2121         int i;
2122         for ( i = 0; i <= p->pPars->nLutSize; i++ )
2123             printf( "%d:%d  ", i, p->nCutCounts[i] );
2124         printf( "Equal = %d (%.0f %%) ", p->nCutEqual, 100.0 * p->nCutEqual / p->Iter / Gia_ManAndNotBufNum(p->pGia) );
2125         if ( p->vTtMem )
2126             printf( "TT = %d (%.2f %%)  ", Vec_MemEntryNum(p->vTtMem), 100.0 * Vec_MemEntryNum(p->vTtMem) / p->CutCount[2] );
2127         if ( p->pGia->pMuxes && p->nCutMux )
2128             printf( "MuxTT = %d (%.0f %%) ", p->nCutMux, 100.0 * p->nCutMux / p->Iter / Gia_ManMuxNum(p->pGia) );
2129         printf( "\n" );
2130     }
2131     printf( "CoDrvs = %d (%.2f %%)  ",  p->nCoDrivers, 100.0*p->nCoDrivers/Gia_ManCoNum(p->pGia) );
2132     printf( "CoInvs = %d (%.2f %%)  ",  p->nInverters, 100.0*p->nInverters/Gia_ManCoNum(p->pGia) );
2133     printf( "Front = %d (%.2f %%)  ",   p->nFrontMax,  100.0*p->nFrontMax/Gia_ManAndNum(p->pGia) );
2134     printf( "TimeFails = %d   ",        p->nTimeFails );
2135     Abc_PrintTime( 1, "Time",    Abc_Clock() - p->clkStart );
2136     fflush( stdout );
2137 }
Lf_ManComputeMapping(Lf_Man_t * p)2138 void Lf_ManComputeMapping( Lf_Man_t * p )
2139 {
2140     Gia_Obj_t * pObj;
2141     int i, arrTime;
2142     assert( p->vStoreNew.iCur == 0 );
2143     Lf_ManSetCutRefs( p );
2144     if ( p->pGia->pManTime != NULL )
2145     {
2146         assert( !Gia_ManBufNum(p->pGia) );
2147         Tim_ManIncrementTravId( (Tim_Man_t*)p->pGia->pManTime );
2148         Gia_ManForEachObj1( p->pGia, pObj, i )
2149         {
2150             if ( Gia_ObjIsBuf(pObj) )
2151                 continue;
2152             if ( Gia_ObjIsAnd(pObj) )
2153                 Lf_ObjMergeOrder( p, i );
2154             else if ( Gia_ObjIsCi(pObj) )
2155             {
2156                 arrTime = Tim_ManGetCiArrival( (Tim_Man_t*)p->pGia->pManTime, Gia_ObjCioId(pObj) );
2157                 Lf_ObjSetCiArrival( p, Gia_ObjCioId(pObj), arrTime );
2158             }
2159             else if ( Gia_ObjIsCo(pObj) )
2160             {
2161                 arrTime = Lf_ObjCoArrival( p, Gia_ObjCioId(pObj) );
2162                 Tim_ManSetCoArrival( (Tim_Man_t*)p->pGia->pManTime, Gia_ObjCioId(pObj), arrTime );
2163             }
2164             else assert( 0 );
2165         }
2166 //        Tim_ManPrint( p->pGia->pManTime );
2167     }
2168     else
2169     {
2170         Gia_ManForEachAnd( p->pGia, pObj, i )
2171             if ( !Gia_ObjIsBuf(pObj) )
2172                 Lf_ObjMergeOrder( p, i );
2173     }
2174     Lf_MemRecycle( &p->vStoreOld );
2175     ABC_SWAP( Lf_Mem_t, p->vStoreOld, p->vStoreNew );
2176     if ( p->fUseEla )
2177         Lf_ManCountMapRefs( p );
2178     else
2179         Lf_ManSetMapRefs( p );
2180     Lf_ManPrintStats( p, (char *)(p->fUseEla ? "Ela  " : (p->Iter ? "Area " : "Delay")) );
2181 }
Lf_ManPerformMappingInt(Gia_Man_t * pGia,Jf_Par_t * pPars)2182 Gia_Man_t * Lf_ManPerformMappingInt( Gia_Man_t * pGia, Jf_Par_t * pPars )
2183 {
2184     int fUsePowerMode = 0;
2185     Lf_Man_t * p;
2186     Gia_Man_t * pNew, * pCls;
2187     if ( pPars->fUseMux7 )
2188         pPars->fCoarsen = 1, pPars->nRoundsEla = 0;
2189     if ( Gia_ManHasChoices(pGia) || pPars->nLutSizeMux )
2190         pPars->fCutMin = 1;
2191     if ( pPars->fCoarsen )
2192     {
2193         pCls = Gia_ManDupMuxes(pGia, pPars->nCoarseLimit);
2194         pCls->pManTime = pGia->pManTime; pGia->pManTime = NULL;
2195     }
2196     else pCls = pGia;
2197     p = Lf_ManAlloc( pCls, pPars );
2198     if ( pPars->fVerbose && pPars->fCoarsen )
2199     {
2200         printf( "Initial " );  Gia_ManPrintMuxStats( pGia );  printf( "\n" );
2201         printf( "Derived " );  Gia_ManPrintMuxStats( pCls );  printf( "\n" );
2202     }
2203     Lf_ManPrintInit( p );
2204 
2205     // power mode
2206     if ( fUsePowerMode && Vec_FltSize(&p->vSwitches) )
2207         pPars->fPower = 0;
2208 
2209     // perform mapping
2210     for ( p->Iter = 0; p->Iter < p->pPars->nRounds; p->Iter++ )
2211         Lf_ManComputeMapping( p );
2212     p->fUseEla = 1;
2213     for ( ; p->Iter < p->pPars->nRounds + pPars->nRoundsEla; p->Iter++ )
2214         Lf_ManComputeMapping( p );
2215 
2216     // power mode
2217     if ( fUsePowerMode && Vec_FltSize(&p->vSwitches) )
2218     {
2219         pPars->fPower = 1;
2220         for ( ; p->Iter < p->pPars->nRounds + pPars->nRoundsEla + 2; p->Iter++ )
2221             Lf_ManComputeMapping( p );
2222     }
2223 
2224     if ( pPars->fVeryVerbose && pPars->fCutMin )
2225         Vec_MemDumpTruthTables( p->vTtMem, Gia_ManName(p->pGia), pPars->nLutSize );
2226     if ( pPars->fCutMin )
2227         pNew = Lf_ManDeriveMappingGia( p );
2228     else if ( pPars->fCoarsen )
2229         pNew = Lf_ManDeriveMappingCoarse( p );
2230     else
2231         pNew = Lf_ManDeriveMapping( p );
2232     Gia_ManMappingVerify( pNew );
2233     Lf_ManPrintQuit( p, pNew );
2234     Lf_ManFree( p );
2235     if ( pCls != pGia )
2236     {
2237         pGia->pManTime = pCls->pManTime; pCls->pManTime = NULL;
2238         Gia_ManStop( pCls );
2239     }
2240     return pNew;
2241 }
Lf_ManPerformMapping(Gia_Man_t * p,Jf_Par_t * pPars)2242 Gia_Man_t * Lf_ManPerformMapping( Gia_Man_t * p, Jf_Par_t * pPars )
2243 {
2244     Gia_Man_t * pNew;
2245     if ( p->pManTime && Tim_ManBoxNum((Tim_Man_t*)p->pManTime) && Gia_ManIsNormalized(p) )
2246     {
2247         Tim_Man_t * pTimOld = (Tim_Man_t *)p->pManTime;
2248         p->pManTime = Tim_ManDup( pTimOld, 1 );
2249         pNew = Gia_ManDupUnnormalize( p );
2250         if ( pNew == NULL )
2251             return NULL;
2252         Gia_ManTransferTiming( pNew, p );
2253         p = pNew;
2254         // mapping
2255         pNew = Lf_ManPerformMappingInt( p, pPars );
2256         if ( pNew != p )
2257         {
2258             Gia_ManTransferTiming( pNew, p );
2259             Gia_ManStop( p );
2260         }
2261         // normalize
2262         pNew = Gia_ManDupNormalize( p = pNew, 0 );
2263         Gia_ManTransferMapping( pNew, p );
2264 //        Gia_ManTransferPacking( pNew, p );
2265         Gia_ManTransferTiming( pNew, p );
2266         Gia_ManStop( p ); // do not delete if the original one!
2267         // cleanup
2268         Tim_ManStop( (Tim_Man_t *)pNew->pManTime );
2269         pNew->pManTime = pTimOld;
2270         assert( Gia_ManIsNormalized(pNew) );
2271     }
2272     else
2273     {
2274         // mapping
2275         pNew = Lf_ManPerformMappingInt( p, pPars );
2276         Gia_ManTransferTiming( pNew, p );
2277     }
2278     return pNew;
2279 }
2280 
2281 /**Function*************************************************************
2282 
2283   Synopsis    [Interface of LUT mapping package.]
2284 
2285   Description []
2286 
2287   SideEffects []
2288 
2289   SeeAlso     []
2290 
2291 ***********************************************************************/
Gia_ManPerformLfMapping(Gia_Man_t * p,Jf_Par_t * pPars,int fNormalized)2292 Gia_Man_t * Gia_ManPerformLfMapping( Gia_Man_t * p, Jf_Par_t * pPars, int fNormalized )
2293 {
2294     Gia_Man_t * pNew;
2295     assert( !pPars->fCutGroup || pPars->nLutSize == 9 || pPars->nLutSize == 11 || pPars->nLutSize == 13 );
2296     // reconstruct GIA according to the hierarchy manager
2297     assert( pPars->pTimesArr == NULL );
2298     assert( pPars->pTimesReq == NULL );
2299     if ( p->pManTime )
2300     {
2301         if ( fNormalized )
2302         {
2303             pNew = Gia_ManDupUnnormalize( p );
2304             if ( pNew == NULL )
2305                 return NULL;
2306             Gia_ManTransferTiming( pNew, p );
2307             p = pNew;
2308             // set arrival and required times
2309             pPars->pTimesArr = Tim_ManGetArrTimes( (Tim_Man_t *)p->pManTime );
2310             pPars->pTimesReq = Tim_ManGetReqTimes( (Tim_Man_t *)p->pManTime );
2311         }
2312         else
2313             p = Gia_ManDup( p );
2314     }
2315     else
2316         p = Gia_ManDup( p );
2317     // perform mapping
2318     pNew = Lf_ManPerformMappingInt( p, pPars );
2319     if ( pNew != p )
2320     {
2321         // transfer name
2322         ABC_FREE( pNew->pName );
2323         ABC_FREE( pNew->pSpec );
2324         pNew->pName = Abc_UtilStrsav( p->pName );
2325         pNew->pSpec = Abc_UtilStrsav( p->pSpec );
2326         Gia_ManSetRegNum( pNew, Gia_ManRegNum(p) );
2327         // return the original (unmodified by the mapper) timing manager
2328         Gia_ManTransferTiming( pNew, p );
2329         Gia_ManStop( p );
2330     }
2331     // normalize and transfer mapping
2332     pNew = Gia_ManDupNormalize( p = pNew, 0 );
2333     Gia_ManTransferMapping( pNew, p );
2334 //    Gia_ManTransferPacking( pNew, p );
2335     Gia_ManTransferTiming( pNew, p );
2336     Gia_ManStop( p );
2337     return pNew;
2338 }
2339 
2340 ////////////////////////////////////////////////////////////////////////
2341 ///                       END OF FILE                                ///
2342 ////////////////////////////////////////////////////////////////////////
2343 
2344 
2345 ABC_NAMESPACE_IMPL_END
2346 
2347