ABC: A System for Sequential Synthesis and Verification
 
Loading...
Searching...
No Matches
giaPf.c
Go to the documentation of this file.
1
20
21#include <float.h>
22#include "gia.h"
23#include "misc/st/st.h"
24#include "map/mio/mio.h"
25#include "misc/util/utilTruth.h"
26#include "misc/extra/extra.h"
27#include "base/main/main.h"
28#include "misc/vec/vecMem.h"
29#include "misc/vec/vecWec.h"
30#include "opt/dau/dau.h"
31
33
37
38#define PF_LEAF_MAX 6
39#define PF_CUT_MAX 32
40#define PF_NO_LEAF 31
41#define PF_NO_FUNC 0x3FFFFFF
42#define PF_INFINITY FLT_MAX
43
44typedef struct Pf_Cut_t_ Pf_Cut_t;
46{
47 word Sign; // signature
48 int Delay; // delay
49 float Flow; // flow
50 unsigned iFunc : 26; // function (PF_NO_FUNC)
51 unsigned Useless : 1; // function
52 unsigned nLeaves : 5; // leaf number (PF_NO_LEAF)
53 int pLeaves[PF_LEAF_MAX+1]; // leaves
54};
55typedef struct Pf_Mat_t_ Pf_Mat_t;
57{
58 unsigned fCompl : 8; // complemented
59 unsigned Phase : 6; // match phase
60 unsigned Perm : 18; // match permutation
61};
62typedef struct Pf_Obj_t_ Pf_Obj_t;
64{
65 float Area;
66 unsigned Gate : 7; // gate
67 unsigned nLeaves : 3; // fanin count
68 unsigned nRefs : 22; // ref count
69 int pLeaves[6]; // leaf literals
70};
71typedef struct Pf_Man_t_ Pf_Man_t;
73{
74 // user data
75 Gia_Man_t * pGia; // derived manager
76 Jf_Par_t * pPars; // parameters
77 // matching
78 Vec_Mem_t * vTtMem; // truth tables
79 Vec_Wec_t * vTt2Match; // matches for truth tables
80 Mio_Cell_t * pCells; // library gates
81 int nCells; // library gate count
82 // cut data
83 Pf_Obj_t * pPfObjs; // best cuts
84 Vec_Ptr_t vPages; // cut memory
85 Vec_Int_t vCutSets; // cut offsets
86 Vec_Flt_t vCutFlows; // temporary cut area
87 Vec_Int_t vCutDelays; // temporary cut delay
88 int iCur; // current position
89 int Iter; // mapping iterations
90 int fUseEla; // use exact area
91 int nInvs; // the inverter count
92 float InvDelay; // inverter delay
93 float InvArea; // inverter area
94 // statistics
95 abctime clkStart; // starting time
96 double CutCount[6]; // cut counts
97 int nCutUseAll; // objects with useful cuts
98};
99
100static inline int Pf_Mat2Int( Pf_Mat_t Mat ) { union { int x; Pf_Mat_t y; } v; v.y = Mat; return v.x; }
101static inline Pf_Mat_t Pf_Int2Mat( int Int ) { union { int x; Pf_Mat_t y; } v; v.x = Int; return v.y; }
102
103static inline Pf_Obj_t * Pf_ManObj( Pf_Man_t * p, int i ) { return p->pPfObjs + i; }
104static inline Mio_Cell_t* Pf_ManCell( Pf_Man_t * p, int i ) { return p->pCells + i; }
105static inline int * Pf_ManCutSet( Pf_Man_t * p, int i ) { return (int *)Vec_PtrEntry(&p->vPages, i >> 16) + (i & 0xFFFF); }
106static inline int Pf_ObjCutSetId( Pf_Man_t * p, int i ) { return Vec_IntEntry( &p->vCutSets, i ); }
107static inline int * Pf_ObjCutSet( Pf_Man_t * p, int i ) { return Pf_ManCutSet(p, Pf_ObjCutSetId(p, i)); }
108static inline int Pf_ObjHasCuts( Pf_Man_t * p, int i ) { return (int)(Vec_IntEntry(&p->vCutSets, i) > 0); }
109static inline int Pf_ObjCutUseless( Pf_Man_t * p, int TruthId ) { return (int)(TruthId >= Vec_WecSize(p->vTt2Match)); }
110
111static inline float Pf_ObjCutFlow( Pf_Man_t * p, int i ) { return Vec_FltEntry(&p->vCutFlows, i); }
112static inline int Pf_ObjCutDelay( Pf_Man_t * p, int i ) { return Vec_IntEntry(&p->vCutDelays, i); }
113static inline void Pf_ObjSetCutFlow( Pf_Man_t * p, int i, float a ) { Vec_FltWriteEntry(&p->vCutFlows, i, a); }
114static inline void Pf_ObjSetCutDelay( Pf_Man_t * p, int i, int d ) { Vec_IntWriteEntry(&p->vCutDelays, i, d); }
115
116static inline int Pf_CutSize( int * pCut ) { return pCut[0] & PF_NO_LEAF; }
117static inline int Pf_CutFunc( int * pCut ) { return ((unsigned)pCut[0] >> 5); }
118static inline int * Pf_CutLeaves( int * pCut ) { return pCut + 1; }
119static inline int Pf_CutSetBoth( int n, int f ) { return n | (f << 5); }
120static inline int Pf_CutIsTriv( int * pCut, int i ) { return Pf_CutSize(pCut) == 1 && pCut[1] == i; }
121static inline int Pf_CutHandle( int * pCutSet, int * pCut ) { assert( pCut > pCutSet ); return pCut - pCutSet; }
122static inline int * Pf_CutFromHandle( int * pCutSet, int h ) { assert( h > 0 ); return pCutSet + h; }
123static inline int Pf_CutConfLit( int Conf, int i ) { return 15 & (Conf >> (i << 2)); }
124static inline int Pf_CutConfVar( int Conf, int i ) { return Abc_Lit2Var( Pf_CutConfLit(Conf, i) ); }
125static inline int Pf_CutConfC( int Conf, int i ) { return Abc_LitIsCompl( Pf_CutConfLit(Conf, i) ); }
126
127#define Pf_SetForEachCut( pList, pCut, i ) for ( i = 0, pCut = pList + 1; i < pList[0]; i++, pCut += Pf_CutSize(pCut) + 1 )
128#define Pf_ObjForEachCut( pCuts, i, nCuts ) for ( i = 0, i < nCuts; i++ )
129#define Pf_CutForEachLit( pCut, Conf, iLit, i ) for ( i = 0; i < Pf_CutSize(pCut) && (iLit = Abc_Lit2LitV(Pf_CutLeaves(pCut), Pf_CutConfLit(Conf, i))); i++ )
130#define Pf_CutForEachVar( pCut, Conf, iVar, c, i ) for ( i = 0; i < Pf_CutSize(pCut) && (iVar = Pf_CutLeaves(pCut)[Pf_CutConfVar(Conf, i)]) && ((c = Pf_CutConfC(Conf, i)), 1); i++ )
131
135
147void Pf_StoCreateGateAdd( Pf_Man_t * pMan, word uTruth, int * pFans, int nFans, int CellId )
148{
149 Vec_Int_t * vArray;
150 Pf_Mat_t Mat = Pf_Int2Mat(0);
151 int i, GateId, Entry, fCompl = (int)(uTruth & 1);
152 word uFunc = fCompl ? ~uTruth : uTruth;
153 int iFunc = Vec_MemHashInsert( pMan->vTtMem, &uFunc );
154 if ( iFunc == Vec_WecSize(pMan->vTt2Match) )
155 Vec_WecPushLevel( pMan->vTt2Match );
156 vArray = Vec_WecEntry( pMan->vTt2Match, iFunc );
157 Mat.fCompl = fCompl;
158 assert( nFans < 7 );
159 for ( i = 0; i < nFans; i++ )
160 {
161 Mat.Perm |= (unsigned)(Abc_Lit2Var(pFans[i]) << (3*i));
162 Mat.Phase |= (unsigned)(Abc_LitIsCompl(pFans[i]) << i);
163 }
164 // check if the same one exists
165 Vec_IntForEachEntryDouble( vArray, GateId, Entry, i )
166 if ( GateId == CellId && Pf_Int2Mat(Entry).Phase == Mat.Phase )
167 break;
168 if ( i == Vec_IntSize(vArray) )
169 {
170 Vec_IntPush( vArray, CellId );
171 Vec_IntPush( vArray, Pf_Mat2Int(Mat) );
172 }
173}
174void Pf_StoCreateGate( Pf_Man_t * pMan, Mio_Cell_t * pCell, int ** pComp, int ** pPerm, int * pnPerms )
175{
176 int Perm[PF_LEAF_MAX], * Perm1, * Perm2;
177 int nPerms = pnPerms[pCell->nFanins];
178 int nMints = (1 << pCell->nFanins);
179 word tCur, tTemp1, tTemp2;
180 int i, p, c;
181 for ( i = 0; i < (int)pCell->nFanins; i++ )
182 Perm[i] = Abc_Var2Lit( i, 0 );
183 tCur = tTemp1 = pCell->uTruth;
184 for ( p = 0; p < nPerms; p++ )
185 {
186 tTemp2 = tCur;
187 for ( c = 0; c < nMints; c++ )
188 {
189 Pf_StoCreateGateAdd( pMan, tCur, Perm, pCell->nFanins, pCell->Id );
190 // update
191 tCur = Abc_Tt6Flip( tCur, pComp[pCell->nFanins][c] );
192 Perm1 = Perm + pComp[pCell->nFanins][c];
193 *Perm1 = Abc_LitNot( *Perm1 );
194 }
195 assert( tTemp2 == tCur );
196 // update
197 tCur = Abc_Tt6SwapAdjacent( tCur, pPerm[pCell->nFanins][p] );
198 Perm1 = Perm + pPerm[pCell->nFanins][p];
199 Perm2 = Perm1 + 1;
200 ABC_SWAP( int, *Perm1, *Perm2 );
201 }
202 assert( tTemp1 == tCur );
203}
204void Pf_StoDeriveMatches( Pf_Man_t * p, int fVerbose )
205{
206// abctime clk = Abc_Clock();
207 int * pComp[7];
208 int * pPerm[7];
209 int nPerms[7], i;
210 for ( i = 2; i <= 6; i++ )
211 pComp[i] = Extra_GreyCodeSchedule( i );
212 for ( i = 2; i <= 6; i++ )
213 pPerm[i] = Extra_PermSchedule( i );
214 for ( i = 2; i <= 6; i++ )
215 nPerms[i] = Extra_Factorial( i );
216 p->pCells = Mio_CollectRootsNewDefault( 6, &p->nCells, fVerbose );
217 for ( i = 4; i < p->nCells; i++ )
218 Pf_StoCreateGate( p, p->pCells + i, pComp, pPerm, nPerms );
219 for ( i = 2; i <= 6; i++ )
220 ABC_FREE( pComp[i] );
221 for ( i = 2; i <= 6; i++ )
222 ABC_FREE( pPerm[i] );
223// Abc_PrintTime( 1, "Time", Abc_Clock() - clk );
224}
225void Pf_StoPrintOne( Pf_Man_t * p, int Count, int t, int i, int GateId, Pf_Mat_t Mat )
226{
227 Mio_Cell_t * pC = p->pCells + GateId;
228 word * pTruth = Vec_MemReadEntry(p->vTtMem, t);
229 int k, nSuppSize = Abc_TtSupportSize(pTruth, 6);
230 printf( "%6d : ", Count );
231 printf( "%6d : ", t );
232 printf( "%6d : ", i );
233 printf( "Gate %16s ", pC->pName );
234 printf( "Area =%8.2f ", pC->Area );
235 printf( "In = %d ", pC->nFanins );
236 if ( Mat.fCompl )
237 printf( " compl " );
238 else
239 printf( " " );
240 for ( k = 0; k < (int)pC->nFanins; k++ )
241 {
242 int fComplF = (Mat.Phase >> k) & 1;
243 int iFanin = (Mat.Perm >> (3*k)) & 7;
244 printf( "%c", 'a' + iFanin - fComplF * ('a' - 'A') );
245 }
246 printf( " " );
247 Dau_DsdPrintFromTruth( pTruth, nSuppSize );
248}
249void Pf_StoPrint( Pf_Man_t * p, int fVerbose )
250{
251 int t, i, GateId, Entry, Count = 0;
252 for ( t = 2; t < Vec_WecSize(p->vTt2Match); t++ )
253 {
254 Vec_Int_t * vArr = Vec_WecEntry( p->vTt2Match, t );
255 Vec_IntForEachEntryDouble( vArr, GateId, Entry, i )
256 {
257 Count++;
258 if ( !fVerbose )
259 continue;
260 if ( t < 10 )
261 Pf_StoPrintOne( p, Count, t, i/2, GateId, Pf_Int2Mat(Entry) );
262 }
263 }
264 printf( "Gates = %d. Truths = %d. Matches = %d.\n",
265 p->nCells, Vec_MemEntryNum(p->vTtMem), Count );
266}
267/*
268void Pf_ManPrepareLibraryTest()
269{
270 int fVerbose = 0;
271 abctime clk = Abc_Clock();
272 Pf_Man_t * p;
273 p = Pf_StoCreate( NULL, NULL, fVerbose );
274 Pf_StoPrint( p, fVerbose );
275 Pf_StoDelete(p);
276 Abc_PrintTime( 1, "Time", Abc_Clock() - clk );
277}
278*/
279
280
281
294{
295 extern void Mf_ManSetFlowRefs( Gia_Man_t * p, Vec_Int_t * vRefs );
296 Pf_Man_t * p;
297 Vec_Int_t * vFlowRefs;
298 assert( pPars->nCutNum > 1 && pPars->nCutNum <= PF_CUT_MAX );
299 assert( pPars->nLutSize > 1 && pPars->nLutSize <= PF_LEAF_MAX );
300 ABC_FREE( pGia->pRefs );
301 Vec_IntFreeP( &pGia->vCellMapping );
302 if ( Gia_ManHasChoices(pGia) )
303 Gia_ManSetPhase(pGia);
304 // create references
305 ABC_FREE( pGia->pRefs );
306 vFlowRefs = Vec_IntAlloc(0);
307 Mf_ManSetFlowRefs( pGia, vFlowRefs );
308 pGia->pRefs= Vec_IntReleaseArray(vFlowRefs);
309 Vec_IntFree(vFlowRefs);
310 // create
311 p = ABC_CALLOC( Pf_Man_t, 1 );
312 p->clkStart = Abc_Clock();
313 p->pGia = pGia;
314 p->pPars = pPars;
315 p->pPfObjs = ABC_CALLOC( Pf_Obj_t, Gia_ManObjNum(pGia) );
316 p->iCur = 2;
317 // other
318 Vec_PtrGrow( &p->vPages, 256 ); // cut memory
319 Vec_IntFill( &p->vCutSets, Gia_ManObjNum(pGia), 0 ); // cut offsets
320 Vec_FltFill( &p->vCutFlows, Gia_ManObjNum(pGia), 0 ); // cut area
321 Vec_IntFill( &p->vCutDelays,Gia_ManObjNum(pGia), 0 ); // cut delay
322 // matching
323 p->vTtMem = Vec_MemAllocForTT( 6, 0 );
324 p->vTt2Match = Vec_WecAlloc( 1000 );
325 Vec_WecPushLevel( p->vTt2Match );
326 Vec_WecPushLevel( p->vTt2Match );
327 assert( Vec_WecSize(p->vTt2Match) == Vec_MemEntryNum(p->vTtMem) );
328 Pf_StoDeriveMatches( p, 0 );//pPars->fVerbose );
329 p->InvDelay = p->pCells[3].Delays[0];
330 p->InvArea = p->pCells[3].Area;
331 //Pf_ObjMatchD(p, 0, 0)->Gate = 0;
332 //Pf_ObjMatchD(p, 0, 1)->Gate = 1;
333 // prepare cuts
334 return p;
335}
337{
338 Vec_PtrFreeData( &p->vPages );
339 ABC_FREE( p->vPages.pArray );
340 ABC_FREE( p->vCutSets.pArray );
341 ABC_FREE( p->vCutFlows.pArray );
342 ABC_FREE( p->vCutDelays.pArray );
343 ABC_FREE( p->pPfObjs );
344 // matching
345 Vec_WecFree( p->vTt2Match );
346 Vec_MemHashFree( p->vTtMem );
347 Vec_MemFree( p->vTtMem );
348 ABC_FREE( p->pCells );
349 ABC_FREE( p );
350}
351
352
353
354
366static inline int Pf_CutComputeTruth6( Pf_Man_t * p, Pf_Cut_t * pCut0, Pf_Cut_t * pCut1, int fCompl0, int fCompl1, Pf_Cut_t * pCutR, int fIsXor )
367{
368// extern int Pf_ManTruthCanonicize( word * t, int nVars );
369 int nOldSupp = pCutR->nLeaves, truthId, fCompl; word t;
370 word t0 = *Vec_MemReadEntry(p->vTtMem, Abc_Lit2Var(pCut0->iFunc));
371 word t1 = *Vec_MemReadEntry(p->vTtMem, Abc_Lit2Var(pCut1->iFunc));
372 if ( Abc_LitIsCompl(pCut0->iFunc) ^ fCompl0 ) t0 = ~t0;
373 if ( Abc_LitIsCompl(pCut1->iFunc) ^ fCompl1 ) t1 = ~t1;
374 t0 = Abc_Tt6Expand( t0, pCut0->pLeaves, pCut0->nLeaves, pCutR->pLeaves, pCutR->nLeaves );
375 t1 = Abc_Tt6Expand( t1, pCut1->pLeaves, pCut1->nLeaves, pCutR->pLeaves, pCutR->nLeaves );
376 t = fIsXor ? t0 ^ t1 : t0 & t1;
377 if ( (fCompl = (int)(t & 1)) ) t = ~t;
378 pCutR->nLeaves = Abc_Tt6MinBase( &t, pCutR->pLeaves, pCutR->nLeaves );
379 assert( (int)(t & 1) == 0 );
380 truthId = Vec_MemHashInsert(p->vTtMem, &t);
381 pCutR->iFunc = Abc_Var2Lit( truthId, fCompl );
382 pCutR->Useless = Pf_ObjCutUseless( p, truthId );
383 assert( (int)pCutR->nLeaves <= nOldSupp );
384 return (int)pCutR->nLeaves < nOldSupp;
385}
386static inline int Pf_CutComputeTruthMux6( Pf_Man_t * p, Pf_Cut_t * pCut0, Pf_Cut_t * pCut1, Pf_Cut_t * pCutC, int fCompl0, int fCompl1, int fComplC, Pf_Cut_t * pCutR )
387{
388 int nOldSupp = pCutR->nLeaves, truthId, fCompl; word t;
389 word t0 = *Vec_MemReadEntry(p->vTtMem, Abc_Lit2Var(pCut0->iFunc));
390 word t1 = *Vec_MemReadEntry(p->vTtMem, Abc_Lit2Var(pCut1->iFunc));
391 word tC = *Vec_MemReadEntry(p->vTtMem, Abc_Lit2Var(pCutC->iFunc));
392 if ( Abc_LitIsCompl(pCut0->iFunc) ^ fCompl0 ) t0 = ~t0;
393 if ( Abc_LitIsCompl(pCut1->iFunc) ^ fCompl1 ) t1 = ~t1;
394 if ( Abc_LitIsCompl(pCutC->iFunc) ^ fComplC ) tC = ~tC;
395 t0 = Abc_Tt6Expand( t0, pCut0->pLeaves, pCut0->nLeaves, pCutR->pLeaves, pCutR->nLeaves );
396 t1 = Abc_Tt6Expand( t1, pCut1->pLeaves, pCut1->nLeaves, pCutR->pLeaves, pCutR->nLeaves );
397 tC = Abc_Tt6Expand( tC, pCutC->pLeaves, pCutC->nLeaves, pCutR->pLeaves, pCutR->nLeaves );
398 t = (tC & t1) | (~tC & t0);
399 if ( (fCompl = (int)(t & 1)) ) t = ~t;
400 pCutR->nLeaves = Abc_Tt6MinBase( &t, pCutR->pLeaves, pCutR->nLeaves );
401 assert( (int)(t & 1) == 0 );
402 truthId = Vec_MemHashInsert(p->vTtMem, &t);
403 pCutR->iFunc = Abc_Var2Lit( truthId, fCompl );
404 pCutR->Useless = Pf_ObjCutUseless( p, truthId );
405 assert( (int)pCutR->nLeaves <= nOldSupp );
406 return (int)pCutR->nLeaves < nOldSupp;
407}
408
409
421static inline int Pf_CutCountBits( word i )
422{
423 i = i - ((i >> 1) & 0x5555555555555555);
424 i = (i & 0x3333333333333333) + ((i >> 2) & 0x3333333333333333);
425 i = ((i + (i >> 4)) & 0x0F0F0F0F0F0F0F0F);
426 return (i*(0x0101010101010101))>>56;
427}
428static inline word Pf_CutGetSign( int * pLeaves, int nLeaves )
429{
430 word Sign = 0; int i;
431 for ( i = 0; i < nLeaves; i++ )
432 Sign |= ((word)1) << (pLeaves[i] & 0x3F);
433 return Sign;
434}
435static inline int Pf_CutCreateUnit( Pf_Cut_t * p, int i )
436{
437 p->Delay = 0;
438 p->Flow = 0;
439 p->iFunc = 2;
440 p->nLeaves = 1;
441 p->pLeaves[0] = i;
442 p->Sign = ((word)1) << (i & 0x3F);
443 return 1;
444}
445static inline void Pf_Cutprintf( Pf_Man_t * p, Pf_Cut_t * pCut )
446{
447 int i, nDigits = Abc_Base10Log(Gia_ManObjNum(p->pGia));
448 printf( "%d {", pCut->nLeaves );
449 for ( i = 0; i < (int)pCut->nLeaves; i++ )
450 printf( " %*d", nDigits, pCut->pLeaves[i] );
451 for ( ; i < (int)p->pPars->nLutSize; i++ )
452 printf( " %*s", nDigits, " " );
453 printf( " } Useless = %d. D = %4d A = %9.4f F = %6d ",
454 pCut->Useless, pCut->Delay, pCut->Flow, pCut->iFunc );
455 if ( p->vTtMem )
456 Dau_DsdPrintFromTruth( Vec_MemReadEntry(p->vTtMem, Abc_Lit2Var(pCut->iFunc)), pCut->nLeaves );
457 else
458 printf( "\n" );
459}
460static inline int Pf_ManPrepareCuts( Pf_Cut_t * pCuts, Pf_Man_t * p, int iObj, int fAddUnit )
461{
462 if ( Pf_ObjHasCuts(p, iObj) )
463 {
464 Pf_Cut_t * pMfCut = pCuts;
465 int i, * pCut, * pList = Pf_ObjCutSet(p, iObj);
466 Pf_SetForEachCut( pList, pCut, i )
467 {
468 pMfCut->Delay = 0;
469 pMfCut->Flow = 0;
470 pMfCut->iFunc = Pf_CutFunc( pCut );
471 pMfCut->nLeaves = Pf_CutSize( pCut );
472 pMfCut->Sign = Pf_CutGetSign( pCut+1, Pf_CutSize(pCut) );
473 pMfCut->Useless = Pf_ObjCutUseless( p, Abc_Lit2Var(pMfCut->iFunc) );
474 memcpy( pMfCut->pLeaves, pCut+1, sizeof(int) * Pf_CutSize(pCut) );
475 pMfCut++;
476 }
477 if ( fAddUnit && pCuts->nLeaves > 1 )
478 return pList[0] + Pf_CutCreateUnit( pMfCut, iObj );
479 return pList[0];
480 }
481 return Pf_CutCreateUnit( pCuts, iObj );
482}
483static inline int Pf_ManSaveCuts( Pf_Man_t * p, Pf_Cut_t ** pCuts, int nCuts, int fUseful )
484{
485 int i, * pPlace, iCur, nInts = 1, nCutsNew = 0;
486 for ( i = 0; i < nCuts; i++ )
487 if ( !fUseful || !pCuts[i]->Useless )
488 nInts += pCuts[i]->nLeaves + 1, nCutsNew++;
489 if ( (p->iCur & 0xFFFF) + nInts > 0xFFFF )
490 p->iCur = ((p->iCur >> 16) + 1) << 16;
491 if ( Vec_PtrSize(&p->vPages) == (p->iCur >> 16) )
492 Vec_PtrPush( &p->vPages, ABC_ALLOC(int, (1<<16)) );
493 iCur = p->iCur; p->iCur += nInts;
494 pPlace = Pf_ManCutSet( p, iCur );
495 *pPlace++ = nCutsNew;
496 for ( i = 0; i < nCuts; i++ )
497 if ( !fUseful || !pCuts[i]->Useless )
498 {
499 *pPlace++ = Pf_CutSetBoth( pCuts[i]->nLeaves, pCuts[i]->iFunc );
500 memcpy( pPlace, pCuts[i]->pLeaves, sizeof(int) * pCuts[i]->nLeaves );
501 pPlace += pCuts[i]->nLeaves;
502 }
503 return iCur;
504}
505static inline int Pf_ManCountUseful( Pf_Cut_t ** pCuts, int nCuts )
506{
507 int i, Count = 0;
508 for ( i = 0; i < nCuts; i++ )
509 Count += !pCuts[i]->Useless;
510 return Count;
511}
512static inline int Pf_ManCountMatches( Pf_Man_t * p, Pf_Cut_t ** pCuts, int nCuts )
513{
514 int i, Count = 0;
515 for ( i = 0; i < nCuts; i++ )
516 if ( !pCuts[i]->Useless )
517 Count += Vec_IntSize(Vec_WecEntry(p->vTt2Match, Abc_Lit2Var(pCuts[i]->iFunc))) / 2;
518 return Count;
519}
520
532static inline int Pf_CutCheck( Pf_Cut_t * pBase, Pf_Cut_t * pCut ) // check if pCut is contained in pBase
533{
534 int nSizeB = pBase->nLeaves;
535 int nSizeC = pCut->nLeaves;
536 int i, * pB = pBase->pLeaves;
537 int k, * pC = pCut->pLeaves;
538 for ( i = 0; i < nSizeC; i++ )
539 {
540 for ( k = 0; k < nSizeB; k++ )
541 if ( pC[i] == pB[k] )
542 break;
543 if ( k == nSizeB )
544 return 0;
545 }
546 return 1;
547}
548static inline int Pf_SetCheckArray( Pf_Cut_t ** ppCuts, int nCuts )
549{
550 Pf_Cut_t * pCut0, * pCut1;
551 int i, k, m, n, Value;
552 assert( nCuts > 0 );
553 for ( i = 0; i < nCuts; i++ )
554 {
555 pCut0 = ppCuts[i];
556 assert( pCut0->nLeaves <= PF_LEAF_MAX );
557 assert( pCut0->Sign == Pf_CutGetSign(pCut0->pLeaves, pCut0->nLeaves) );
558 // check duplicates
559 for ( m = 0; m < (int)pCut0->nLeaves; m++ )
560 for ( n = m + 1; n < (int)pCut0->nLeaves; n++ )
561 assert( pCut0->pLeaves[m] < pCut0->pLeaves[n] );
562 // check pairs
563 for ( k = 0; k < nCuts; k++ )
564 {
565 pCut1 = ppCuts[k];
566 if ( pCut0 == pCut1 )
567 continue;
568 // check containments
569 Value = Pf_CutCheck( pCut0, pCut1 );
570 assert( Value == 0 );
571 }
572 }
573 return 1;
574}
575
576
588static inline int Pf_CutMergeOrder( Pf_Cut_t * pCut0, Pf_Cut_t * pCut1, Pf_Cut_t * pCut, int nLutSize )
589{
590 int nSize0 = pCut0->nLeaves;
591 int nSize1 = pCut1->nLeaves;
592 int i, * pC0 = pCut0->pLeaves;
593 int k, * pC1 = pCut1->pLeaves;
594 int c, * pC = pCut->pLeaves;
595 // the case of the largest cut sizes
596 if ( nSize0 == nLutSize && nSize1 == nLutSize )
597 {
598 for ( i = 0; i < nSize0; i++ )
599 {
600 if ( pC0[i] != pC1[i] ) return 0;
601 pC[i] = pC0[i];
602 }
603 pCut->nLeaves = nLutSize;
604 pCut->iFunc = PF_NO_FUNC;
605 pCut->Sign = pCut0->Sign | pCut1->Sign;
606 return 1;
607 }
608 // compare two cuts with different numbers
609 i = k = c = 0;
610 if ( nSize0 == 0 ) goto FlushCut1;
611 if ( nSize1 == 0 ) goto FlushCut0;
612 while ( 1 )
613 {
614 if ( c == nLutSize ) return 0;
615 if ( pC0[i] < pC1[k] )
616 {
617 pC[c++] = pC0[i++];
618 if ( i >= nSize0 ) goto FlushCut1;
619 }
620 else if ( pC0[i] > pC1[k] )
621 {
622 pC[c++] = pC1[k++];
623 if ( k >= nSize1 ) goto FlushCut0;
624 }
625 else
626 {
627 pC[c++] = pC0[i++]; k++;
628 if ( i >= nSize0 ) goto FlushCut1;
629 if ( k >= nSize1 ) goto FlushCut0;
630 }
631 }
632
633FlushCut0:
634 if ( c + nSize0 > nLutSize + i ) return 0;
635 while ( i < nSize0 )
636 pC[c++] = pC0[i++];
637 pCut->nLeaves = c;
638 pCut->iFunc = PF_NO_FUNC;
639 pCut->Sign = pCut0->Sign | pCut1->Sign;
640 return 1;
641
642FlushCut1:
643 if ( c + nSize1 > nLutSize + k ) return 0;
644 while ( k < nSize1 )
645 pC[c++] = pC1[k++];
646 pCut->nLeaves = c;
647 pCut->iFunc = PF_NO_FUNC;
648 pCut->Sign = pCut0->Sign | pCut1->Sign;
649 return 1;
650}
651static inline int Pf_CutMergeOrderMux( Pf_Cut_t * pCut0, Pf_Cut_t * pCut1, Pf_Cut_t * pCut2, Pf_Cut_t * pCut, int nLutSize )
652{
653 int x0, i0 = 0, nSize0 = pCut0->nLeaves, * pC0 = pCut0->pLeaves;
654 int x1, i1 = 0, nSize1 = pCut1->nLeaves, * pC1 = pCut1->pLeaves;
655 int x2, i2 = 0, nSize2 = pCut2->nLeaves, * pC2 = pCut2->pLeaves;
656 int xMin, c = 0, * pC = pCut->pLeaves;
657 while ( 1 )
658 {
659 x0 = (i0 == nSize0) ? ABC_INFINITY : pC0[i0];
660 x1 = (i1 == nSize1) ? ABC_INFINITY : pC1[i1];
661 x2 = (i2 == nSize2) ? ABC_INFINITY : pC2[i2];
662 xMin = Abc_MinInt( Abc_MinInt(x0, x1), x2 );
663 if ( xMin == ABC_INFINITY ) break;
664 if ( c == nLutSize ) return 0;
665 pC[c++] = xMin;
666 if (x0 == xMin) i0++;
667 if (x1 == xMin) i1++;
668 if (x2 == xMin) i2++;
669 }
670 pCut->nLeaves = c;
671 pCut->iFunc = PF_NO_FUNC;
672 pCut->Sign = pCut0->Sign | pCut1->Sign | pCut2->Sign;
673 return 1;
674}
675static inline int Pf_SetCutIsContainedOrder( Pf_Cut_t * pBase, Pf_Cut_t * pCut ) // check if pCut is contained in pBase
676{
677 int i, nSizeB = pBase->nLeaves;
678 int k, nSizeC = pCut->nLeaves;
679 if ( nSizeB == nSizeC )
680 {
681 for ( i = 0; i < nSizeB; i++ )
682 if ( pBase->pLeaves[i] != pCut->pLeaves[i] )
683 return 0;
684 return 1;
685 }
686 assert( nSizeB > nSizeC );
687 if ( nSizeC == 0 )
688 return 1;
689 for ( i = k = 0; i < nSizeB; i++ )
690 {
691 if ( pBase->pLeaves[i] > pCut->pLeaves[k] )
692 return 0;
693 if ( pBase->pLeaves[i] == pCut->pLeaves[k] )
694 {
695 if ( ++k == nSizeC )
696 return 1;
697 }
698 }
699 return 0;
700}
701static inline int Pf_SetLastCutIsContained( Pf_Cut_t ** pCuts, int nCuts )
702{
703 int i;
704 for ( i = 0; i < nCuts; i++ )
705 if ( pCuts[i]->nLeaves <= pCuts[nCuts]->nLeaves && (pCuts[i]->Sign & pCuts[nCuts]->Sign) == pCuts[i]->Sign && Pf_SetCutIsContainedOrder(pCuts[nCuts], pCuts[i]) )
706 return 1;
707 return 0;
708}
709static inline int Pf_SetLastCutContainsArea( Pf_Cut_t ** pCuts, int nCuts )
710{
711 int i, k, fChanges = 0;
712 for ( i = 0; i < nCuts; i++ )
713 if ( pCuts[nCuts]->nLeaves < pCuts[i]->nLeaves && (pCuts[nCuts]->Sign & pCuts[i]->Sign) == pCuts[nCuts]->Sign && Pf_SetCutIsContainedOrder(pCuts[i], pCuts[nCuts]) )
714 pCuts[i]->nLeaves = PF_NO_LEAF, fChanges = 1;
715 if ( !fChanges )
716 return nCuts;
717 for ( i = k = 0; i <= nCuts; i++ )
718 {
719 if ( pCuts[i]->nLeaves == PF_NO_LEAF )
720 continue;
721 if ( k < i )
722 ABC_SWAP( Pf_Cut_t *, pCuts[k], pCuts[i] );
723 k++;
724 }
725 return k - 1;
726}
727static inline int Pf_CutCompareArea( Pf_Cut_t * pCut0, Pf_Cut_t * pCut1 )
728{
729 if ( pCut0->Useless < pCut1->Useless ) return -1;
730 if ( pCut0->Useless > pCut1->Useless ) return 1;
731 if ( pCut0->Flow < pCut1->Flow ) return -1;
732 if ( pCut0->Flow > pCut1->Flow ) return 1;
733 if ( pCut0->Delay < pCut1->Delay ) return -1;
734 if ( pCut0->Delay > pCut1->Delay ) return 1;
735 if ( pCut0->nLeaves < pCut1->nLeaves ) return -1;
736 if ( pCut0->nLeaves > pCut1->nLeaves ) return 1;
737 return 0;
738}
739static inline void Pf_SetSortByArea( Pf_Cut_t ** pCuts, int nCuts )
740{
741 int i;
742 for ( i = nCuts; i > 0; i-- )
743 {
744 if ( Pf_CutCompareArea(pCuts[i - 1], pCuts[i]) < 0 )
745 return;
746 ABC_SWAP( Pf_Cut_t *, pCuts[i - 1], pCuts[i] );
747 }
748}
749static inline int Pf_SetAddCut( Pf_Cut_t ** pCuts, int nCuts, int nCutNum )
750{
751 if ( nCuts == 0 )
752 return 1;
753 nCuts = Pf_SetLastCutContainsArea(pCuts, nCuts);
754 Pf_SetSortByArea( pCuts, nCuts );
755 return Abc_MinInt( nCuts + 1, nCutNum - 1 );
756}
757static inline int Pf_CutArea( Pf_Man_t * p, int nLeaves )
758{
759 if ( nLeaves < 2 )
760 return 0;
761 return nLeaves + p->pPars->nAreaTuner;
762}
763static inline void Pf_CutParams( Pf_Man_t * p, Pf_Cut_t * pCut, int nGiaRefs )
764{
765 int i, nLeaves = pCut->nLeaves;
766 assert( nLeaves <= p->pPars->nLutSize );
767 pCut->Delay = 0;
768 pCut->Flow = 0;
769 for ( i = 0; i < nLeaves; i++ )
770 {
771 pCut->Delay = Abc_MaxInt( pCut->Delay, Pf_ObjCutDelay(p, pCut->pLeaves[i]) );
772 pCut->Flow += Pf_ObjCutFlow(p, pCut->pLeaves[i]);
773 }
774 pCut->Delay += (int)(nLeaves > 1);
775 pCut->Flow = (pCut->Flow + Pf_CutArea(p, nLeaves)) / (nGiaRefs ? nGiaRefs : 1);
776}
777void Pf_ObjMergeOrder( Pf_Man_t * p, int iObj )
778{
779 Pf_Cut_t pCuts0[PF_CUT_MAX], pCuts1[PF_CUT_MAX], pCuts[PF_CUT_MAX], * pCutsR[PF_CUT_MAX];
780 Gia_Obj_t * pObj = Gia_ManObj(p->pGia, iObj);
781 int nGiaRefs = 2*Gia_ObjRefNumId(p->pGia, iObj);
782 int nLutSize = p->pPars->nLutSize;
783 int nCutNum = p->pPars->nCutNum;
784 int nCuts0 = Pf_ManPrepareCuts(pCuts0, p, Gia_ObjFaninId0(pObj, iObj), 1);
785 int nCuts1 = Pf_ManPrepareCuts(pCuts1, p, Gia_ObjFaninId1(pObj, iObj), 1);
786 int fComp0 = Gia_ObjFaninC0(pObj);
787 int fComp1 = Gia_ObjFaninC1(pObj);
788 int iSibl = Gia_ObjSibl(p->pGia, iObj);
789 Pf_Cut_t * pCut0, * pCut1, * pCut0Lim = pCuts0 + nCuts0, * pCut1Lim = pCuts1 + nCuts1;
790 int i, nCutsUse, nCutsR = 0;
791 assert( !Gia_ObjIsBuf(pObj) );
792 for ( i = 0; i < nCutNum; i++ )
793 pCutsR[i] = pCuts + i;
794 if ( iSibl )
795 {
796 Pf_Cut_t pCuts2[PF_CUT_MAX];
797 Gia_Obj_t * pObjE = Gia_ObjSiblObj(p->pGia, iObj);
798 int fCompE = Gia_ObjPhase(pObj) ^ Gia_ObjPhase(pObjE);
799 int nCuts2 = Pf_ManPrepareCuts(pCuts2, p, iSibl, 0);
800 Pf_Cut_t * pCut2, * pCut2Lim = pCuts2 + nCuts2;
801 for ( pCut2 = pCuts2; pCut2 < pCut2Lim; pCut2++ )
802 {
803 *pCutsR[nCutsR] = *pCut2;
804 pCutsR[nCutsR]->iFunc = Abc_LitNotCond( pCutsR[nCutsR]->iFunc, fCompE );
805 Pf_CutParams( p, pCutsR[nCutsR], nGiaRefs );
806 nCutsR = Pf_SetAddCut( pCutsR, nCutsR, nCutNum );
807 }
808 }
809 if ( Gia_ObjIsMuxId(p->pGia, iObj) )
810 {
811 Pf_Cut_t pCuts2[PF_CUT_MAX];
812 int nCuts2 = Pf_ManPrepareCuts(pCuts2, p, Gia_ObjFaninId2(p->pGia, iObj), 1);
813 int fComp2 = Gia_ObjFaninC2(p->pGia, pObj);
814 Pf_Cut_t * pCut2, * pCut2Lim = pCuts2 + nCuts2;
815 p->CutCount[0] += nCuts0 * nCuts1 * nCuts2;
816 for ( pCut0 = pCuts0; pCut0 < pCut0Lim; pCut0++ )
817 for ( pCut1 = pCuts1; pCut1 < pCut1Lim; pCut1++ )
818 for ( pCut2 = pCuts2; pCut2 < pCut2Lim; pCut2++ )
819 {
820 if ( Pf_CutCountBits(pCut0->Sign | pCut1->Sign | pCut2->Sign) > nLutSize )
821 continue;
822 p->CutCount[1]++;
823 if ( !Pf_CutMergeOrderMux(pCut0, pCut1, pCut2, pCutsR[nCutsR], nLutSize) )
824 continue;
825 if ( Pf_SetLastCutIsContained(pCutsR, nCutsR) )
826 continue;
827 p->CutCount[2]++;
828 if ( Pf_CutComputeTruthMux6(p, pCut0, pCut1, pCut2, fComp0, fComp1, fComp2, pCutsR[nCutsR]) )
829 pCutsR[nCutsR]->Sign = Pf_CutGetSign(pCutsR[nCutsR]->pLeaves, pCutsR[nCutsR]->nLeaves);
830 Pf_CutParams( p, pCutsR[nCutsR], nGiaRefs );
831 nCutsR = Pf_SetAddCut( pCutsR, nCutsR, nCutNum );
832 }
833 }
834 else
835 {
836 int fIsXor = Gia_ObjIsXor(pObj);
837 p->CutCount[0] += nCuts0 * nCuts1;
838 for ( pCut0 = pCuts0; pCut0 < pCut0Lim; pCut0++ )
839 for ( pCut1 = pCuts1; pCut1 < pCut1Lim; pCut1++ )
840 {
841 if ( (int)(pCut0->nLeaves + pCut1->nLeaves) > nLutSize && Pf_CutCountBits(pCut0->Sign | pCut1->Sign) > nLutSize )
842 continue;
843 p->CutCount[1]++;
844 if ( !Pf_CutMergeOrder(pCut0, pCut1, pCutsR[nCutsR], nLutSize) )
845 continue;
846 if ( Pf_SetLastCutIsContained(pCutsR, nCutsR) )
847 continue;
848 p->CutCount[2]++;
849 if ( Pf_CutComputeTruth6(p, pCut0, pCut1, fComp0, fComp1, pCutsR[nCutsR], fIsXor) )
850 pCutsR[nCutsR]->Sign = Pf_CutGetSign(pCutsR[nCutsR]->pLeaves, pCutsR[nCutsR]->nLeaves);
851 Pf_CutParams( p, pCutsR[nCutsR], nGiaRefs );
852 nCutsR = Pf_SetAddCut( pCutsR, nCutsR, nCutNum );
853 }
854 }
855 // debug printout
856 if ( 0 )
857// if ( iObj % 10000 == 0 )
858// if ( iObj == 1090 )
859 {
860 printf( "*** Obj = %d Useful = %d\n", iObj, Pf_ManCountUseful(pCutsR, nCutsR) );
861 for ( i = 0; i < nCutsR; i++ )
862 Pf_Cutprintf( p, pCutsR[i] );
863 printf( "\n" );
864 }
865 // verify
866 assert( nCutsR > 0 && nCutsR < nCutNum );
867// assert( Pf_SetCheckArray(pCutsR, nCutsR) );
868 // store the cutset
869 Pf_ObjSetCutFlow( p, iObj, pCutsR[0]->Flow );
870 Pf_ObjSetCutDelay( p, iObj, pCutsR[0]->Delay );
871 *Vec_IntEntryP(&p->vCutSets, iObj) = Pf_ManSaveCuts(p, pCutsR, nCutsR, 0);
872 p->CutCount[3] += nCutsR;
873 nCutsUse = Pf_ManCountUseful(pCutsR, nCutsR);
874 p->CutCount[4] += nCutsUse;
875 p->nCutUseAll += nCutsUse == nCutsR;
876 p->CutCount[5] += Pf_ManCountMatches(p, pCutsR, nCutsR);
877}
879{
880 Gia_Obj_t * pObj; int i, iFanin;
881 Gia_ManForEachAnd( p->pGia, pObj, i )
882 if ( Gia_ObjIsBuf(pObj) )
883 {
884 iFanin = Gia_ObjFaninId0(pObj, i);
885 Pf_ObjSetCutFlow( p, i, Pf_ObjCutFlow(p, iFanin) );
886 Pf_ObjSetCutDelay( p, i, Pf_ObjCutDelay(p, iFanin) );
887 }
888 else
889 Pf_ObjMergeOrder( p, i );
890}
891
892
893
894
906void Pf_ManPrintStats( Pf_Man_t * p, char * pTitle )
907{
908 if ( !p->pPars->fVerbose )
909 return;
910 printf( "%s : ", pTitle );
911 printf( "Delay =%8.2f ", (float)p->pPars->MapDelay );
912 printf( "Area =%12.2f ", p->pPars->MapArea );
913 printf( "Gate =%6d ", (int)p->pPars->Area );
914 printf( "Inv =%6d ", (int)p->nInvs );
915 printf( "Edge =%7d ", (int)p->pPars->Edge );
916 Abc_PrintTime( 1, "Time", Abc_Clock() - p->clkStart );
917 fflush( stdout );
918}
920{
921 int nChoices;
922 if ( !p->pPars->fVerbose )
923 return;
924 printf( "LutSize = %d ", p->pPars->nLutSize );
925 printf( "CutNum = %d ", p->pPars->nCutNum );
926 printf( "Iter = %d ", p->pPars->nRounds + p->pPars->nRoundsEla );
927 printf( "Coarse = %d ", p->pPars->fCoarsen );
928 printf( "Cells = %d ", p->nCells );
929 printf( "Funcs = %d ", Vec_MemEntryNum(p->vTtMem) );
930 printf( "Matches = %d ", Vec_WecSizeSize(p->vTt2Match)/2 );
931 nChoices = Gia_ManChoiceNum( p->pGia );
932 if ( nChoices )
933 printf( "Choices = %d ", nChoices );
934 printf( "\n" );
935 printf( "Computing cuts...\r" );
936 fflush( stdout );
937}
939{
940 float MemGia = Gia_ManMemory(p->pGia) / (1<<20);
941 float MemMan =(1.0 * sizeof(Pf_Obj_t) + 3.0 * sizeof(int)) * Gia_ManObjNum(p->pGia) / (1<<20);
942 float MemCuts = 1.0 * sizeof(int) * (1 << 16) * Vec_PtrSize(&p->vPages) / (1<<20);
943 float MemTt = p->vTtMem ? Vec_MemMemory(p->vTtMem) / (1<<20) : 0;
944 if ( p->CutCount[0] == 0 )
945 p->CutCount[0] = 1;
946 if ( !p->pPars->fVerbose )
947 return;
948 printf( "CutPair = %.0f ", p->CutCount[0] );
949 printf( "Merge = %.0f (%.1f) ", p->CutCount[1], 1.0*p->CutCount[1]/Gia_ManAndNum(p->pGia) );
950 printf( "Eval = %.0f (%.1f) ", p->CutCount[2], 1.0*p->CutCount[2]/Gia_ManAndNum(p->pGia) );
951 printf( "Cut = %.0f (%.1f) ", p->CutCount[3], 1.0*p->CutCount[3]/Gia_ManAndNum(p->pGia) );
952 printf( "Use = %.0f (%.1f) ", p->CutCount[4], 1.0*p->CutCount[4]/Gia_ManAndNum(p->pGia) );
953 printf( "Mat = %.0f (%.1f) ", p->CutCount[5], 1.0*p->CutCount[5]/Gia_ManAndNum(p->pGia) );
954// printf( "Equ = %d (%.2f %%) ", p->nCutUseAll, 100.0*p->nCutUseAll /p->CutCount[0] );
955 printf( "\n" );
956 printf( "Gia = %.2f MB ", MemGia );
957 printf( "Man = %.2f MB ", MemMan );
958 printf( "Cut = %.2f MB ", MemCuts );
959 printf( "TT = %.2f MB ", MemTt );
960 printf( "Total = %.2f MB ", MemGia + MemMan + MemCuts + MemTt );
961// printf( "\n" );
962 Abc_PrintTime( 1, "Time", Abc_Clock() - p->clkStart );
963 fflush( stdout );
964}
965
966
978/*
979void Pf_ManSetMapRefsGate( Pf_Man_t * p, int iObj, float Required, Pf_Mat_t * pM )
980{
981 int k, iVar, fCompl;
982 Mio_Cell_t * pCell = Pf_ManCell( p, pM->Gate );
983 int * pCut = Pf_CutFromHandle( Pf_ObjCutSet(p, iObj), pM->CutH );
984 Pf_CutForEachVar( pCut, pM->Conf, iVar, fCompl, k )
985 {
986 Pf_ObjMapRefInc( p, iVar, fCompl );
987 Pf_ObjUpdateRequired( p, iVar, fCompl, Required - pCell->Delays[k] );
988 }
989 assert( Pf_CutSize(pCut) == (int)pCell->nFanins );
990 // update global stats
991 p->pPars->MapArea += pCell->Area;
992 p->pPars->Edge += Pf_CutSize(pCut);
993 p->pPars->Area++;
994 // update status of the gate
995 assert( pM->fBest == 0 );
996 pM->fBest = 1;
997}
998int Pf_ManSetMapRefs( Pf_Man_t * p )
999{
1000 float Coef = 1.0 / (1.0 + (p->Iter + 1) * (p->Iter + 1));
1001 float * pFlowRefs = Vec_FltArray( &p->vFlowRefs );
1002 int * pMapRefs = Vec_IntArray( &p->vMapRefs );
1003 float Epsilon = p->pPars->Epsilon;
1004 int nLits = 2*Gia_ManObjNum(p->pGia);
1005 int i, c, Id, nRefs[2];
1006 Pf_Mat_t * pD, * pA, * pM;
1007 Pf_Mat_t * pDs[2], * pAs[2], * pMs[2];
1008 Gia_Obj_t * pObj;
1009 float Required = 0, Requireds[2];
1010 // check references
1011 assert( !p->fUseEla );
1012 memset( pMapRefs, 0, sizeof(int) * nLits );
1013 Vec_FltFill( &p->vRequired, nLits, PF_INFINITY );
1014// for ( i = 0; i < Gia_ManObjNum(p->pGia); i++ )
1015// assert( !Pf_ObjMapRefNum(p, i, 0) && !Pf_ObjMapRefNum(p, i, 1) );
1016 // compute delay
1017 p->pPars->MapDelay = 0;
1018 Gia_ManForEachCo( p->pGia, pObj, i )
1019 {
1020 Required = Pf_ObjMatchD( p, Gia_ObjFaninId0p(p->pGia, pObj), Gia_ObjFaninC0(pObj) )->D;
1021 if ( Required == PF_INFINITY )
1022 {
1023 Pf_ManCutMatchprintf( p, Gia_ObjFaninId0p(p->pGia, pObj), Gia_ObjFaninC0(pObj), Pf_ObjMatchD( p, Gia_ObjFaninId0p(p->pGia, pObj), Gia_ObjFaninC0(pObj) ) );
1024 }
1025 p->pPars->MapDelay = Abc_MaxFloat( p->pPars->MapDelay, Required );
1026 }
1027 // check delay target
1028 if ( p->pPars->MapDelayTarget == -1 && p->pPars->nRelaxRatio )
1029 p->pPars->MapDelayTarget = (int)((float)p->pPars->MapDelay * (100.0 + p->pPars->nRelaxRatio) / 100.0);
1030 if ( p->pPars->MapDelayTarget != -1 )
1031 {
1032 if ( p->pPars->MapDelay < p->pPars->MapDelayTarget + Epsilon )
1033 p->pPars->MapDelay = p->pPars->MapDelayTarget;
1034 else if ( p->pPars->nRelaxRatio == 0 )
1035 Abc_Print( 0, "Relaxing user-specified delay target from %.2f to %.2f.\n", p->pPars->MapDelayTarget, p->pPars->MapDelay );
1036 }
1037 // set required times
1038 Gia_ManForEachCo( p->pGia, pObj, i )
1039 {
1040 Required = Pf_ObjMatchD( p, Gia_ObjFaninId0p(p->pGia, pObj), Gia_ObjFaninC0(pObj) )->D;
1041 Required = p->pPars->fDoAverage ? Required * (100.0 + p->pPars->nRelaxRatio) / 100.0 : p->pPars->MapDelay;
1042 Pf_ObjUpdateRequired( p, Gia_ObjFaninId0p(p->pGia, pObj), Gia_ObjFaninC0(pObj), Required );
1043 Pf_ObjMapRefInc( p, Gia_ObjFaninId0p(p->pGia, pObj), Gia_ObjFaninC0(pObj));
1044 }
1045 // compute area and edges
1046 p->nInvs = 0;
1047 p->pPars->MapArea = 0;
1048 p->pPars->Area = p->pPars->Edge = 0;
1049 Gia_ManForEachAndReverse( p->pGia, pObj, i )
1050 {
1051 if ( Gia_ObjIsBuf(pObj) )
1052 {
1053 if ( Pf_ObjMapRefNum(p, i, 1) )
1054 {
1055 Pf_ObjMapRefInc( p, i, 0 );
1056 Pf_ObjUpdateRequired( p, i, 0, Pf_ObjRequired(p, i, 1) - p->InvDelay );
1057 p->pPars->MapArea += p->InvArea;
1058 p->pPars->Edge++;
1059 p->pPars->Area++;
1060 p->nInvs++;
1061 }
1062 Pf_ObjUpdateRequired( p, Gia_ObjFaninId0(pObj, i), Gia_ObjFaninC0(pObj), Pf_ObjRequired(p, i, 0) );
1063 Pf_ObjMapRefInc( p, Gia_ObjFaninId0(pObj, i), Gia_ObjFaninC0(pObj));
1064 continue;
1065 }
1066 // skip if this node is not used
1067 for ( c = 0; c < 2; c++ )
1068 {
1069 nRefs[c] = Pf_ObjMapRefNum(p, i, c);
1070
1071 //if ( Pf_ObjMatchD( p, i, c )->fCompl )
1072 // printf( "Match D of node %d has inv in phase %d.\n", i, c );
1073 //if ( Pf_ObjMatchA( p, i, c )->fCompl )
1074 // printf( "Match A of node %d has inv in phase %d.\n", i, c );
1075 }
1076 if ( !nRefs[0] && !nRefs[1] )
1077 continue;
1078
1079 // consider two cases
1080 if ( nRefs[0] && nRefs[1] )
1081 {
1082 // find best matches for both phases
1083 for ( c = 0; c < 2; c++ )
1084 {
1085 Requireds[c] = Pf_ObjRequired( p, i, c );
1086 //assert( Requireds[c] < PF_INFINITY );
1087 pDs[c] = Pf_ObjMatchD( p, i, c );
1088 pAs[c] = Pf_ObjMatchA( p, i, c );
1089 pMs[c] = (pAs[c]->D < Requireds[c] + Epsilon) ? pAs[c] : pDs[c];
1090 }
1091 // swap complemented matches
1092 if ( pMs[0]->fCompl && pMs[1]->fCompl )
1093 {
1094 pMs[0]->fCompl = pMs[1]->fCompl = 0;
1095 ABC_SWAP( Pf_Mat_t *, pMs[0], pMs[1] );
1096 }
1097 // check if intervers are involved
1098 if ( !pMs[0]->fCompl && !pMs[1]->fCompl )
1099 {
1100 // no inverters
1101 for ( c = 0; c < 2; c++ )
1102 Pf_ManSetMapRefsGate( p, i, Requireds[c], pMs[c] );
1103 }
1104 else
1105 {
1106 // one interver
1107 assert( !pMs[0]->fCompl || !pMs[1]->fCompl );
1108 c = pMs[1]->fCompl;
1109 assert( pMs[c]->fCompl && !pMs[!c]->fCompl );
1110 //printf( "Using inverter at node %d in phase %d\n", i, c );
1111
1112 // update this phase phase
1113 pM = pMs[c];
1114 pM->fBest = 1;
1115 Required = Requireds[c];
1116
1117 // update opposite phase
1118 Pf_ObjMapRefInc( p, i, !c );
1119 Pf_ObjUpdateRequired( p, i, !c, Required - p->InvDelay );
1120
1121 // select oppositve phase
1122 Required = Pf_ObjRequired( p, i, !c );
1123 //assert( Required < PF_INFINITY );
1124 pD = Pf_ObjMatchD( p, i, !c );
1125 pA = Pf_ObjMatchA( p, i, !c );
1126 pM = (pA->D < Required + Epsilon) ? pA : pD;
1127 assert( !pM->fCompl );
1128
1129 // account for the inverter
1130 p->pPars->MapArea += p->InvArea;
1131 p->pPars->Edge++;
1132 p->pPars->Area++;
1133 p->nInvs++;
1134
1135 // create gate
1136 Pf_ManSetMapRefsGate( p, i, Required, pM );
1137 }
1138 }
1139 else
1140 {
1141 c = (int)(nRefs[1] > 0);
1142 assert( nRefs[c] && !nRefs[!c] );
1143 // consider this phase
1144 Required = Pf_ObjRequired( p, i, c );
1145 //assert( Required < PF_INFINITY );
1146 pD = Pf_ObjMatchD( p, i, c );
1147 pA = Pf_ObjMatchA( p, i, c );
1148 pM = (pA->D < Required + Epsilon) ? pA : pD;
1149
1150 if ( pM->fCompl ) // use inverter
1151 {
1152 p->nInvs++;
1153 //printf( "Using inverter at node %d in phase %d\n", i, c );
1154 pM->fBest = 1;
1155 // update opposite phase
1156 Pf_ObjMapRefInc( p, i, !c );
1157 Pf_ObjUpdateRequired( p, i, !c, Required - p->InvDelay );
1158 // select oppositve phase
1159 Required = Pf_ObjRequired( p, i, !c );
1160 //assert( Required < PF_INFINITY );
1161 pD = Pf_ObjMatchD( p, i, !c );
1162 pA = Pf_ObjMatchA( p, i, !c );
1163 pM = (pA->D < Required + Epsilon) ? pA : pD;
1164 assert( !pM->fCompl );
1165
1166 // account for the inverter
1167 p->pPars->MapArea += p->InvArea;
1168 p->pPars->Edge++;
1169 p->pPars->Area++;
1170 }
1171
1172 // create gate
1173 Pf_ManSetMapRefsGate( p, i, Required, pM );
1174 }
1175
1176
1177 // the result of this:
1178 // - only one phase can be implemented as inverter of the other phase
1179 // - required times are propagated correctly
1180 // - references are set correctly
1181 }
1182 Gia_ManForEachCiId( p->pGia, Id, i )
1183 if ( Pf_ObjMapRefNum(p, Id, 1) )
1184 {
1185 Pf_ObjMapRefInc( p, Id, 0 );
1186 Pf_ObjUpdateRequired( p, Id, 0, Required - p->InvDelay );
1187 p->pPars->MapArea += p->InvArea;
1188 p->pPars->Edge++;
1189 p->pPars->Area++;
1190 p->nInvs++;
1191 }
1192 // blend references
1193 for ( i = 0; i < nLits; i++ )
1194// pFlowRefs[i] = Abc_MaxFloat(1.0, pMapRefs[i]);
1195 pFlowRefs[i] = Abc_MaxFloat(1.0, Coef * pFlowRefs[i] + (1.0 - Coef) * Abc_MaxFloat(1, pMapRefs[i]));
1196// pFlowRefs[i] = 0.2 * pFlowRefs[i] + 0.8 * Abc_MaxFloat(1, pMapRefs[i]);
1197// memset( pMapRefs, 0, sizeof(int) * nLits );
1198 return p->pPars->Area;
1199}
1200Gia_Man_t * Pf_ManDeriveMapping( Pf_Man_t * p )
1201{
1202 Vec_Int_t * vMapping;
1203 Pf_Mat_t * pM;
1204 int i, k, c, Id, iLit, * pCut;
1205 assert( p->pGia->vCellMapping == NULL );
1206 vMapping = Vec_IntAlloc( 2*Gia_ManObjNum(p->pGia) + (int)p->pPars->Edge + (int)p->pPars->Area * 2 );
1207 Vec_IntFill( vMapping, 2*Gia_ManObjNum(p->pGia), 0 );
1208 // create CI inverters
1209 Gia_ManForEachCiId( p->pGia, Id, i )
1210 if ( Pf_ObjMapRefNum(p, Id, 1) )
1211 Vec_IntWriteEntry( vMapping, Abc_Var2Lit(Id, 1), -1 );
1212 // create internal nodes
1213 Gia_ManForEachAndId( p->pGia, i )
1214 {
1215 Gia_Obj_t * pObj = Gia_ManObj(p->pGia, i);
1216 if ( Gia_ObjIsBuf(pObj) )
1217 {
1218 if ( Pf_ObjMapRefNum(p, i, 1) )
1219 Vec_IntWriteEntry( vMapping, Abc_Var2Lit(i, 1), -1 );
1220 Vec_IntWriteEntry( vMapping, Abc_Var2Lit(i, 0), -2 );
1221 continue;
1222 }
1223 for ( c = 0; c < 2; c++ )
1224 if ( Pf_ObjMapRefNum(p, i, c) )
1225 {
1226 // printf( "Using %d %d\n", i, c );
1227 pM = Pf_ObjMatchBest( p, i, c );
1228 // remember inverter
1229 if ( pM->fCompl )
1230 {
1231 Vec_IntWriteEntry( vMapping, Abc_Var2Lit(i, c), -1 );
1232 continue;
1233 }
1234 // Pf_ManCutMatchprintf( p, i, c, pM );
1235 pCut = Pf_CutFromHandle( Pf_ObjCutSet(p, i), pM->CutH );
1236 // create mapping
1237 Vec_IntWriteEntry( vMapping, Abc_Var2Lit(i, c), Vec_IntSize(vMapping) );
1238 Vec_IntPush( vMapping, Pf_CutSize(pCut) );
1239 Pf_CutForEachLit( pCut, pM->Conf, iLit, k )
1240 Vec_IntPush( vMapping, iLit );
1241 Vec_IntPush( vMapping, pM->Gate );
1242 }
1243 }
1244// assert( Vec_IntCap(vMapping) == 16 || Vec_IntSize(vMapping) == Vec_IntCap(vMapping) );
1245 p->pGia->vCellMapping = vMapping;
1246 return p->pGia;
1247}
1248*/
1249
1250
1263{
1264}
1265
1278{
1279 memset( pPars, 0, sizeof(Jf_Par_t) );
1280 pPars->nLutSize = 6;
1281 pPars->nCutNum = 16;
1282 pPars->nProcNum = 0;
1283 pPars->nRounds = 3;
1284 pPars->nRoundsEla = 0;
1285 pPars->nRelaxRatio = 0;
1286 pPars->nCoarseLimit = 3;
1287 pPars->nAreaTuner = 1;
1288 pPars->nVerbLimit = 5;
1289 pPars->DelayTarget = -1;
1290 pPars->fAreaOnly = 0;
1291 pPars->fOptEdge = 1;
1292 pPars->fCoarsen = 0;
1293 pPars->fCutMin = 1;
1294 pPars->fGenCnf = 0;
1295 pPars->fPureAig = 0;
1296 pPars->fVerbose = 0;
1297 pPars->fVeryVerbose = 0;
1298 pPars->nLutSizeMax = PF_LEAF_MAX;
1299 pPars->nCutNumMax = PF_CUT_MAX;
1300 pPars->MapDelayTarget = -1;
1301 pPars->Epsilon = (float)0.01;
1302}
1304{
1305 Gia_Man_t * pNew = NULL, * pCls;
1306 Pf_Man_t * p;
1307 if ( Gia_ManHasChoices(pGia) )
1308 pPars->fCoarsen = 0;
1309 pCls = pPars->fCoarsen ? Gia_ManDupMuxes(pGia, pPars->nCoarseLimit) : pGia;
1310 p = Pf_StoCreate( pCls, pPars );
1311// if ( pPars->fVeryVerbose )
1312 Pf_StoPrint( p, 1 );
1313 if ( pPars->fVerbose && pPars->fCoarsen )
1314 {
1315 printf( "Initial " ); Gia_ManPrintMuxStats( pGia ); printf( "\n" );
1316 printf( "Derived " ); Gia_ManPrintMuxStats( pCls ); printf( "\n" );
1317 }
1318 Pf_ManPrintInit( p );
1320 Pf_ManPrintQuit( p );
1321/*
1322 Gia_ManForEachCiId( p->pGia, Id, i )
1323 Pf_ObjPrepareCi( p, Id );
1324 for ( p->Iter = 0; p->Iter < p->pPars->nRounds; p->Iter++ )
1325 {
1326 Pf_ManComputeMapping( p );
1327 //Pf_ManSetMapRefs( p );
1328 Pf_ManPrintStats( p, p->Iter ? "Area " : "Delay" );
1329 }
1330 p->fUseEla = 1;
1331 for ( ; p->Iter < p->pPars->nRounds + pPars->nRoundsEla; p->Iter++ )
1332 {
1333 Pf_ManComputeMapping( p );
1334 //Pf_ManUpdateStats( p );
1335 Pf_ManPrintStats( p, "Ela " );
1336 }
1337*/
1338 pNew = NULL; //Pf_ManDeriveMapping( p );
1339// Gia_ManMappingVerify( pNew );
1340 Pf_StoDelete( p );
1341 if ( pCls != pGia )
1342 Gia_ManStop( pCls );
1343 if ( pNew == NULL )
1344 return Gia_ManDup( pGia );
1345 return pNew;
1346}
1347
1351
1352
1354
#define ABC_SWAP(Type, a, b)
Definition abc_global.h:253
ABC_INT64_T abctime
Definition abc_global.h:332
#define ABC_ALLOC(type, num)
Definition abc_global.h:264
#define ABC_INFINITY
MACRO DEFINITIONS ///.
Definition abc_global.h:250
#define ABC_CALLOC(type, num)
Definition abc_global.h:265
#define ABC_FREE(obj)
Definition abc_global.h:267
#define ABC_NAMESPACE_IMPL_START
#define ABC_NAMESPACE_IMPL_END
typedefABC_NAMESPACE_IMPL_START struct Vec_Int_t_ Vec_Int_t
DECLARATIONS ///.
Definition bblif.c:37
void Dau_DsdPrintFromTruth(word *pTruth, int nVarsInit)
Definition dauDsd.c:1968
Cube * p
Definition exorList.c:222
int * Extra_PermSchedule(int n)
int Extra_Factorial(int n)
int * Extra_GreyCodeSchedule(int n)
void Mf_ManSetFlowRefs(Gia_Man_t *p, Vec_Int_t *vRefs)
Definition giaMf.c:1137
void Pf_ManPrintInit(Pf_Man_t *p)
Definition giaPf.c:919
#define PF_NO_FUNC
Definition giaPf.c:41
void Pf_ObjMergeOrder(Pf_Man_t *p, int iObj)
Definition giaPf.c:777
void Pf_StoPrint(Pf_Man_t *p, int fVerbose)
Definition giaPf.c:249
void Pf_StoDelete(Pf_Man_t *p)
Definition giaPf.c:336
void Pf_ManSetDefaultPars(Jf_Par_t *pPars)
Definition giaPf.c:1277
Pf_Man_t * Pf_StoCreate(Gia_Man_t *pGia, Jf_Par_t *pPars)
Definition giaPf.c:293
void Pf_StoDeriveMatches(Pf_Man_t *p, int fVerbose)
Definition giaPf.c:204
struct Pf_Cut_t_ Pf_Cut_t
Definition giaPf.c:44
#define Pf_SetForEachCut(pList, pCut, i)
Definition giaPf.c:127
void Pf_StoPrintOne(Pf_Man_t *p, int Count, int t, int i, int GateId, Pf_Mat_t Mat)
Definition giaPf.c:225
#define PF_CUT_MAX
Definition giaPf.c:39
void Pf_StoCreateGate(Pf_Man_t *pMan, Mio_Cell_t *pCell, int **pComp, int **pPerm, int *pnPerms)
Definition giaPf.c:174
void Pf_ManComputeMapping(Pf_Man_t *p)
Definition giaPf.c:1262
void Pf_ManPrintStats(Pf_Man_t *p, char *pTitle)
Definition giaPf.c:906
struct Pf_Man_t_ Pf_Man_t
Definition giaPf.c:71
Gia_Man_t * Pf_ManPerformMapping(Gia_Man_t *pGia, Jf_Par_t *pPars)
Definition giaPf.c:1303
#define PF_NO_LEAF
Definition giaPf.c:40
struct Pf_Obj_t_ Pf_Obj_t
Definition giaPf.c:62
struct Pf_Mat_t_ Pf_Mat_t
Definition giaPf.c:55
void Pf_StoCreateGateAdd(Pf_Man_t *pMan, word uTruth, int *pFans, int nFans, int CellId)
FUNCTION DEFINITIONS ///.
Definition giaPf.c:147
#define PF_LEAF_MAX
DECLARATIONS ///.
Definition giaPf.c:38
void Pf_ManComputeCuts(Pf_Man_t *p)
Definition giaPf.c:878
void Pf_ManPrintQuit(Pf_Man_t *p)
Definition giaPf.c:938
void Gia_ManStop(Gia_Man_t *p)
Definition giaMan.c:82
Gia_Man_t * Gia_ManDup(Gia_Man_t *p)
Definition giaDup.c:720
Gia_Man_t * Gia_ManDupMuxes(Gia_Man_t *p, int Limit)
Definition giaMuxes.c:98
double Gia_ManMemory(Gia_Man_t *p)
Definition giaMan.c:194
#define Gia_ManForEachAnd(p, pObj, i)
Definition gia.h:1214
struct Gia_Obj_t_ Gia_Obj_t
Definition gia.h:76
struct Gia_Man_t_ Gia_Man_t
Definition gia.h:96
void Gia_ManPrintMuxStats(Gia_Man_t *p)
Definition giaMuxes.c:63
struct Jf_Par_t_ Jf_Par_t
Definition gia.h:333
void Gia_ManSetPhase(Gia_Man_t *p)
Definition giaUtil.c:420
unsigned __int64 word
DECLARATIONS ///.
Definition kitPerm.c:36
Mio_Cell_t * Mio_CollectRootsNewDefault(int nInputs, int *pnGates, int fVerbose)
Definition mioUtils.c:715
struct Mio_Cell_t_ Mio_Cell_t
Definition mio.h:46
Vec_Int_t * vCellMapping
Definition gia.h:139
int * pRefs
Definition gia.h:118
float MapDelayTarget
Definition gia.h:387
int nRounds
Definition gia.h:339
int fGenCnf
Definition gia.h:360
int nProcNum
Definition gia.h:338
int nRelaxRatio
Definition gia.h:341
int nCutNum
Definition gia.h:337
int nRoundsEla
Definition gia.h:340
int fOptEdge
Definition gia.h:354
int fAreaOnly
Definition gia.h:350
int nCutNumMax
Definition gia.h:373
int fCoarsen
Definition gia.h:357
int nLutSizeMax
Definition gia.h:372
int nLutSize
Definition gia.h:336
int nAreaTuner
Definition gia.h:343
int fVeryVerbose
Definition gia.h:371
int fCutMin
Definition gia.h:358
int fPureAig
Definition gia.h:365
float Epsilon
Definition gia.h:388
int nCoarseLimit
Definition gia.h:342
int fVerbose
Definition gia.h:370
int nVerbLimit
Definition gia.h:345
int DelayTarget
Definition gia.h:349
unsigned nFanins
Definition mio.h:51
float Area
Definition mio.h:52
unsigned Id
Definition mio.h:50
word uTruth
Definition mio.h:53
char * pName
Definition mio.h:49
word Sign
Definition giaPf.c:47
unsigned iFunc
Definition giaPf.c:50
int Delay
Definition giaPf.c:48
unsigned nLeaves
Definition giaPf.c:52
int pLeaves[PF_LEAF_MAX+1]
Definition giaPf.c:53
unsigned Useless
Definition giaPf.c:51
float Flow
Definition giaPf.c:49
Mio_Cell_t * pCells
Definition giaPf.c:80
Jf_Par_t * pPars
Definition giaPf.c:76
int nCutUseAll
Definition giaPf.c:97
Gia_Man_t * pGia
Definition giaPf.c:75
abctime clkStart
Definition giaPf.c:95
int Iter
Definition giaPf.c:89
Vec_Int_t vCutDelays
Definition giaPf.c:87
Vec_Wec_t * vTt2Match
Definition giaPf.c:79
int nInvs
Definition giaPf.c:91
int nCells
Definition giaPf.c:81
Vec_Mem_t * vTtMem
Definition giaPf.c:78
Vec_Int_t vCutSets
Definition giaPf.c:85
Pf_Obj_t * pPfObjs
Definition giaPf.c:83
float InvDelay
Definition giaPf.c:92
float InvArea
Definition giaPf.c:93
double CutCount[6]
Definition giaPf.c:96
Vec_Ptr_t vPages
Definition giaPf.c:84
int fUseEla
Definition giaPf.c:90
int iCur
Definition giaPf.c:88
Vec_Flt_t vCutFlows
Definition giaPf.c:86
unsigned fCompl
Definition giaPf.c:58
unsigned Phase
Definition giaPf.c:59
unsigned Perm
Definition giaPf.c:60
unsigned Gate
Definition giaPf.c:66
unsigned nRefs
Definition giaPf.c:68
unsigned nLeaves
Definition giaPf.c:67
int pLeaves[6]
Definition giaPf.c:69
float Area
Definition giaPf.c:65
typedefABC_NAMESPACE_IMPL_START struct Vec_Mem_t_ Vec_Mem_t
DECLARATIONS ///.
Definition utilMem.c:35
#define assert(ex)
Definition util_old.h:213
char * memcpy()
char * memset()
typedefABC_NAMESPACE_HEADER_START struct Vec_Flt_t_ Vec_Flt_t
INCLUDES ///.
Definition vecFlt.h:42
#define Vec_IntForEachEntryDouble(vVec, Entry1, Entry2, i)
Definition vecInt.h:72
typedefABC_NAMESPACE_HEADER_START struct Vec_Ptr_t_ Vec_Ptr_t
INCLUDES ///.
Definition vecPtr.h:42
typedefABC_NAMESPACE_HEADER_START struct Vec_Wec_t_ Vec_Wec_t
INCLUDES ///.
Definition vecWec.h:42