ifMan.c 24.1 KB
Newer Older
Alan Mishchenko committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/**CFile****************************************************************

  FileName    [ifMan.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [FPGA mapping based on priority cuts.]

  Synopsis    [Mapping manager.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

  Date        [Ver. 1.0. Started - November 21, 2006.]

  Revision    [$Id: ifMan.c,v 1.00 2006/11/21 00:00:00 alanmi Exp $]

***********************************************************************/

#include "if.h"

23 24 25
ABC_NAMESPACE_IMPL_START


Alan Mishchenko committed
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
////////////////////////////////////////////////////////////////////////
///                        DECLARATIONS                              ///
////////////////////////////////////////////////////////////////////////

static If_Obj_t * If_ManSetupObj( If_Man_t * p );

static void       If_ManCutSetRecycle( If_Man_t * p, If_Set_t * pSet ) { pSet->pNext = p->pFreeList; p->pFreeList = pSet;                            }
static If_Set_t * If_ManCutSetFetch( If_Man_t * p )                    { If_Set_t * pTemp = p->pFreeList; p->pFreeList = p->pFreeList->pNext; return pTemp; }

////////////////////////////////////////////////////////////////////////
///                     FUNCTION DEFINITIONS                         ///
////////////////////////////////////////////////////////////////////////

/**Function*************************************************************

  Synopsis    [Starts the AIG manager.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
If_Man_t * If_ManStart( If_Par_t * pPars )
{
52
    If_Man_t * p; int v;
53
    assert( !pPars->fUseDsd || !pPars->fUseTtPerm );
Alan Mishchenko committed
54
    // start the manager
Alan Mishchenko committed
55
    p = ABC_ALLOC( If_Man_t, 1 );
Alan Mishchenko committed
56
    memset( p, 0, sizeof(If_Man_t) );
57
    p->pPars    = pPars;
Alan Mishchenko committed
58
    p->fEpsilon = pPars->Epsilon;
Alan Mishchenko committed
59
    // allocate arrays for nodes
60 61 62 63
    p->vCis     = Vec_PtrAlloc( 100 );
    p->vCos     = Vec_PtrAlloc( 100 );
    p->vObjs    = Vec_PtrAlloc( 100 );
    p->vTemp    = Vec_PtrAlloc( 100 );
64
    p->vVisited = Vec_PtrAlloc( 100 );
Alan Mishchenko committed
65
    // prepare the memory manager
66 67 68 69
    if ( p->pPars->fTruth )
    {
        for ( v = 0; v <= p->pPars->nLutSize; v++ )
            p->nTruth6Words[v] = Abc_Truth6WordNum( v );
70
        for ( v = 6; v <= Abc_MaxInt(6,p->pPars->nLutSize); v++ )
71
            p->vTtMem[v] = Vec_MemAllocForTT( v, pPars->fUseTtPerm );
72 73
        for ( v = 0; v < 6; v++ )
            p->vTtMem[v] = p->vTtMem[6];
74
        if ( p->pPars->fDelayOpt || pPars->nGateSize > 0 )
75
        {
76
            for ( v = 6; v <= Abc_MaxInt(6,p->pPars->nLutSize); v++ )
77
                p->vTtIsops[v] = Vec_WecAlloc( 1000 );
78
            for ( v = 6; v <= Abc_MaxInt(6,p->pPars->nLutSize); v++ )
79 80 81 82
                Vec_WecInit( p->vTtIsops[v], 2 );
            for ( v = 0; v < 6; v++ )
                p->vTtIsops[v] = p->vTtIsops[6];
        }
Alan Mishchenko committed
83
        if ( pPars->fDelayOpt || pPars->nGateSize > 0 || pPars->fDsdBalance )
84 85 86 87
        {
            p->vCover = Vec_IntAlloc( 0 );
            p->vArray = Vec_IntAlloc( 1000 );
        }
88
    }
Alan Mishchenko committed
89
    p->nPermWords  = p->pPars->fUsePerm? If_CutPermWords( p->pPars->nLutSize ) : 0;
90 91
    p->nObjBytes   = sizeof(If_Obj_t) + sizeof(int) * (p->pPars->nLutSize + p->nPermWords);
    p->nCutBytes   = sizeof(If_Cut_t) + sizeof(int) * (p->pPars->nLutSize + p->nPermWords);
Alan Mishchenko committed
92 93 94 95
    p->nSetBytes   = sizeof(If_Set_t) + (sizeof(If_Cut_t *) + p->nCutBytes) * (p->pPars->nCutsMax + 1);
    p->pMemObj     = Mem_FixedStart( p->nObjBytes );
    // report expected memory usage
    if ( p->pPars->fVerbose )
Alan Mishchenko committed
96
        Abc_Print( 1, "K = %d. Memory (bytes): Truth = %4d. Cut = %4d. Obj = %4d. Set = %4d. CutMin = %s\n", 
97
            p->pPars->nLutSize, 8 * p->nTruth6Words[p->pPars->nLutSize], p->nCutBytes, p->nObjBytes, p->nSetBytes, p->pPars->fCutMin? "yes":"no" );
Alan Mishchenko committed
98
    // room for temporary truth tables
99 100 101 102 103
    p->puTemp[0] = p->pPars->fTruth? ABC_ALLOC( unsigned, 8 * p->nTruth6Words[p->pPars->nLutSize] ) : NULL;
    p->puTemp[1] = p->puTemp[0] + p->nTruth6Words[p->pPars->nLutSize]*2;
    p->puTemp[2] = p->puTemp[1] + p->nTruth6Words[p->pPars->nLutSize]*2;
    p->puTemp[3] = p->puTemp[2] + p->nTruth6Words[p->pPars->nLutSize]*2;
    p->puTempW   = p->pPars->fTruth? ABC_ALLOC( word, p->nTruth6Words[p->pPars->nLutSize] ) : NULL;
Alan Mishchenko committed
104
    if ( pPars->fUseDsd )
105
    {
106
        for ( v = 6; v <= Abc_MaxInt(6,p->pPars->nLutSize); v++ )
107 108 109 110 111 112 113 114 115 116 117 118 119
        {
            p->vTtDsds[v] = Vec_IntAlloc( 1000 );
            Vec_IntPush( p->vTtDsds[v], 0 );
            Vec_IntPush( p->vTtDsds[v], 2 );
            p->vTtPerms[v] = Vec_StrAlloc( 10000 );
            Vec_StrFill( p->vTtPerms[v], 2 * v, IF_BIG_CHAR );
            Vec_StrWriteEntry( p->vTtPerms[v], v, 0 );
        }
        for ( v = 0; v < 6; v++ )
        {
            p->vTtDsds[v]  = p->vTtDsds[6];
            p->vTtPerms[v] = p->vTtPerms[6];
        }
120
    }
121 122 123
    if ( pPars->fUseTtPerm )
    {
        p->vPairHash = Hash_IntManStart( 10000 );
124 125 126 127
        p->vPairPerms = Vec_StrAlloc( 10000 );
        Vec_StrFill( p->vPairPerms, p->pPars->nLutSize, 0 );
        p->vPairRes = Vec_IntAlloc( 1000 );
        Vec_IntPush( p->vPairRes, -1 );
128 129 130 131 132 133
        for ( v = 6; v <= Abc_MaxInt(6,p->pPars->nLutSize); v++ )
            p->vTtOccurs[v] = Vec_IntAlloc( 1000 );
        for ( v = 0; v < 6; v++ )
            p->vTtOccurs[v] = p->vTtOccurs[6];
        for ( v = 6; v <= Abc_MaxInt(6,p->pPars->nLutSize); v++ )
            Vec_IntPushTwo( p->vTtOccurs[v], 0, 0 );
134
    }
135 136 137 138 139 140 141 142 143 144 145
    if ( pPars->fUseCofVars )
    {
        for ( v = 6; v <= Abc_MaxInt(6,p->pPars->nLutSize); v++ )
        {
            p->vTtVars[v] = Vec_StrAlloc( 1000 );
            Vec_StrPush( p->vTtVars[v], 0 );
            Vec_StrPush( p->vTtVars[v], 0 );
        }
        for ( v = 0; v < 6; v++ )
            p->vTtVars[v]  = p->vTtVars[6];
    }
146 147 148 149 150 151 152 153 154 155 156
    if ( pPars->fUseAndVars )
    {
        for ( v = 6; v <= Abc_MaxInt(6,p->pPars->nLutSize); v++ )
        {
            p->vTtDecs[v] = Vec_IntAlloc( 1000 );
            Vec_IntPush( p->vTtDecs[v], 0 );
            Vec_IntPush( p->vTtDecs[v], 0 );
        }
        for ( v = 0; v < 6; v++ )
            p->vTtDecs[v]  = p->vTtDecs[6];
    }
157 158 159 160 161
    if ( pPars->fUseBat )
    {
//        abctime clk = Abc_Clock();
        extern int Bat_ManCellFuncLookup( void * pMan, unsigned * pTruth, int nVars, int nLeaves, char * pStr );
        extern void Bat_ManFuncSetupTable();
162
        pPars->pFuncCell = (int (*)  (If_Man_t *, unsigned *, int, int, char *))Bat_ManCellFuncLookup;
163 164 165
        Bat_ManFuncSetupTable();
//        Abc_PrintTime( 1, "Setup time", Abc_Clock() - clk );
    }
Alan Mishchenko committed
166
    // create the constant node
167
    p->pConst1   = If_ManSetupObj( p );
Alan Mishchenko committed
168
    p->pConst1->Type   = IF_CONST1;
Alan Mishchenko committed
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
    p->pConst1->fPhase = 1;
    p->nObjs[IF_CONST1]++;
    return p;
}

/**Function*************************************************************

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void If_ManRestart( If_Man_t * p )
{
Alan Mishchenko committed
187
    ABC_FREE( p->pMemCi );
Alan Mishchenko committed
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
    Vec_PtrClear( p->vCis );
    Vec_PtrClear( p->vCos );
    Vec_PtrClear( p->vObjs );
    Vec_PtrClear( p->vTemp );
    Mem_FixedRestart( p->pMemObj );
    // create the constant node
    p->pConst1 = If_ManSetupObj( p );
    p->pConst1->Type = IF_CONST1;
    p->pConst1->fPhase = 1;
    // reset the counter of other nodes
    p->nObjs[IF_CI] = p->nObjs[IF_CO] = p->nObjs[IF_AND] = 0;
}

/**Function*************************************************************

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void If_ManStop( If_Man_t * p )
{
214
    extern void If_ManCacheAnalize( If_Man_t * p );
215
    int i;
216 217
    if ( p->pPars->fVerbose && p->vCutData )
        If_ManCacheAnalize( p );
218 219 220
    if ( p->pPars->fVerbose && p->pPars->fTruth )
    {
        int nUnique = 0, nMemTotal = 0;
221
        for ( i = 6; i <= Abc_MaxInt(6,p->pPars->nLutSize); i++ )
222
            nUnique += Vec_MemEntryNum(p->vTtMem[i]);
223
        for ( i = 6; i <= Abc_MaxInt(6,p->pPars->nLutSize); i++ )
224
            nMemTotal += (int)Vec_MemMemory(p->vTtMem[i]);
225 226
        printf( "Unique truth tables = %d   Memory = %.2f MB   ", nUnique, 1.0 * nMemTotal / (1<<20) ); 
        Abc_PrintTime( 1, "Time", p->timeCache[4] );
227 228 229 230 231 232
        if ( p->nCacheMisses )
        {
            printf( "Cache hits = %d. Cache misses = %d  (%.2f %%)\n", p->nCacheHits, p->nCacheMisses, 100.0 * p->nCacheMisses / (p->nCacheHits + p->nCacheMisses) ); 
            Abc_PrintTime( 1, "Non-DSD   ", p->timeCache[0] );
            Abc_PrintTime( 1, "DSD hits  ", p->timeCache[1] );
            Abc_PrintTime( 1, "DSD misses", p->timeCache[2] );
233
            Abc_PrintTime( 1, "TOTAL     ", p->timeCache[0] + p->timeCache[1] + p->timeCache[2] );
234 235
            Abc_PrintTime( 1, "Canon     ", p->timeCache[3] );
        }
236
    }
237
    if ( p->pPars->fVerbose && p->nCutsUselessAll )
238 239 240
    {
        for ( i = 0; i <= 16; i++ )
            if ( p->nCutsUseless[i] )
241 242
                Abc_Print( 1, "Useless cuts %2d  = %9d  (out of %9d)  (%6.2f %%)\n", i, p->nCutsUseless[i], p->nCutsCount[i], 100.0*p->nCutsUseless[i]/Abc_MaxInt(p->nCutsCount[i],1) );
        Abc_Print( 1, "Useless cuts all = %9d  (out of %9d)  (%6.2f %%)\n", p->nCutsUselessAll, p->nCutsCountAll, 100.0*p->nCutsUselessAll/Abc_MaxInt(p->nCutsCountAll,1) );
243
    }
244 245 246 247 248
//    if ( p->pPars->fVerbose && p->nCuts5 )
//        Abc_Print( 1, "Statistics about 5-cuts: Total = %d  Non-decomposable = %d (%.2f %%)\n", p->nCuts5, p->nCuts5-p->nCuts5a, 100.0*(p->nCuts5-p->nCuts5a)/p->nCuts5 );
    if ( p->pIfDsdMan )
        p->pIfDsdMan = NULL;
    if ( p->pPars->fUseDsd && (p->nCountNonDec[0] || p->nCountNonDec[1]) )
249
        printf( "NonDec0 = %d.  NonDec1 = %d.\n", p->nCountNonDec[0], p->nCountNonDec[1] );
250
    Vec_IntFreeP( &p->vCoAttrs );
Alan Mishchenko committed
251 252 253 254
    Vec_PtrFree( p->vCis );
    Vec_PtrFree( p->vCos );
    Vec_PtrFree( p->vObjs );
    Vec_PtrFree( p->vTemp );
255
    Vec_IntFreeP( &p->vCover );
256
    Vec_IntFreeP( &p->vArray );
257 258 259
    Vec_WrdFreeP( &p->vAnds );
    Vec_WrdFreeP( &p->vAndGate );
    Vec_WrdFreeP( &p->vOrGate );
260 261 262
    Vec_PtrFreeP( &p->vObjsRev );
    Vec_PtrFreeP( &p->vLatchOrder );
    Vec_IntFreeP( &p->vLags );
263
    for ( i = 6; i <= Abc_MaxInt(6,p->pPars->nLutSize); i++ )
264
        Vec_IntFreeP( &p->vTtDsds[i] );
265
    for ( i = 6; i <= Abc_MaxInt(6,p->pPars->nLutSize); i++ )
266
        Vec_StrFreeP( &p->vTtPerms[i] );
267 268
    for ( i = 6; i <= Abc_MaxInt(6,p->pPars->nLutSize); i++ )
        Vec_StrFreeP( &p->vTtVars[i] );
269 270
    for ( i = 6; i <= Abc_MaxInt(6,p->pPars->nLutSize); i++ )
        Vec_IntFreeP( &p->vTtDecs[i] );
271
    Vec_IntFreeP( &p->vCutData );
272 273
    Vec_IntFreeP( &p->vPairRes );
    Vec_StrFreeP( &p->vPairPerms );
274
    Vec_PtrFreeP( &p->vVisited );
275 276
    if ( p->vPairHash )
        Hash_IntManStop( p->vPairHash );
277
    for ( i = 6; i <= Abc_MaxInt(6,p->pPars->nLutSize); i++ )
278
        Vec_MemHashFree( p->vTtMem[i] );
279
    for ( i = 6; i <= Abc_MaxInt(6,p->pPars->nLutSize); i++ )
280
        Vec_MemFreeP( &p->vTtMem[i] );
281
    for ( i = 6; i <= Abc_MaxInt(6,p->pPars->nLutSize); i++ )
282
        Vec_WecFreeP( &p->vTtIsops[i] );
283 284
    for ( i = 6; i <= Abc_MaxInt(6,p->pPars->nLutSize); i++ )
        Vec_IntFreeP( &p->vTtOccurs[i] );
Alan Mishchenko committed
285
    Mem_FixedStop( p->pMemObj, 0 );
Alan Mishchenko committed
286 287 288
    ABC_FREE( p->pMemCi );
    ABC_FREE( p->pMemAnd );
    ABC_FREE( p->puTemp[0] );
289
    ABC_FREE( p->puTempW );
Alan Mishchenko committed
290
    // free pars memory
291 292
    ABC_FREE( p->pPars->pTimesArr );
    ABC_FREE( p->pPars->pTimesReq );
Alan Mishchenko committed
293 294
    if ( p->pManTim )
        Tim_ManStop( p->pManTim );
Alan Mishchenko committed
295 296
    if ( p->vSwitching )
        Vec_IntFree( p->vSwitching );
297 298 299 300 301
    if ( p->pPars->fUseBat )
    {
        extern void Bat_ManFuncSetdownTable();
        Bat_ManFuncSetdownTable();
    }
302
    // hash table
303 304 305 306 307 308
//    if ( p->pPars->fVerbose && p->nTableEntries[0] )
//        printf( "Hash table 2:  Entries = %7d.  Size = %7d.\n", p->nTableEntries[0], p->nTableSize[0] );
//    if ( p->pPars->fVerbose && p->nTableEntries[1] )
//        printf( "Hash table 3:  Entries = %7d.  Size = %7d.\n", p->nTableEntries[1], p->nTableSize[1] );
    ABC_FREE( p->pHashTable[0] );
    ABC_FREE( p->pHashTable[1] );
309 310
    if ( p->pMemEntries )
        Mem_FixedStop( p->pMemEntries, 0 );
311
    ABC_FREE( p->pName );
Alan Mishchenko committed
312
    ABC_FREE( p );
Alan Mishchenko committed
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
}

/**Function*************************************************************

  Synopsis    [Creates primary input.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
If_Obj_t * If_ManCreateCi( If_Man_t * p )
{
    If_Obj_t * pObj;
    pObj = If_ManSetupObj( p );
    pObj->Type = IF_CI;
    pObj->IdPio = Vec_PtrSize( p->vCis );
    Vec_PtrPush( p->vCis, pObj );
    p->nObjs[IF_CI]++;
    return pObj;
}

/**Function*************************************************************

  Synopsis    [Creates primary output with the given driver.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
If_Obj_t * If_ManCreateCo( If_Man_t * p, If_Obj_t * pDriver )
{
    If_Obj_t * pObj;
    pObj = If_ManSetupObj( p );
    pObj->IdPio = Vec_PtrSize( p->vCos );
    Vec_PtrPush( p->vCos, pObj );
    pObj->Type = IF_CO;
    pObj->fCompl0 = If_IsComplement(pDriver); pDriver = If_Regular(pDriver);
    pObj->pFanin0 = pDriver; pDriver->nRefs++; 
357
    pObj->fPhase  = (pObj->fCompl0 ^ pDriver->fPhase);
Alan Mishchenko committed
358
    pObj->Level   = pDriver->Level;
Alan Mishchenko committed
359 360
    if ( p->nLevelMax < (int)pObj->Level )
        p->nLevelMax = (int)pObj->Level;
Alan Mishchenko committed
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
    p->nObjs[IF_CO]++;
    return pObj;
}

/**Function*************************************************************

  Synopsis    [Create the new node assuming it does not exist.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
If_Obj_t * If_ManCreateAnd( If_Man_t * p, If_Obj_t * pFan0, If_Obj_t * pFan1 )
{
    If_Obj_t * pObj;
    // perform constant propagation
    if ( pFan0 == pFan1 )
        return pFan0;
    if ( pFan0 == If_Not(pFan1) )
        return If_Not(p->pConst1);
    if ( If_Regular(pFan0) == p->pConst1 )
        return pFan0 == p->pConst1 ? pFan1 : If_Not(p->pConst1);
    if ( If_Regular(pFan1) == p->pConst1 )
        return pFan1 == p->pConst1 ? pFan0 : If_Not(p->pConst1);
    // get memory for the new object
    pObj = If_ManSetupObj( p );
    pObj->Type    = IF_AND;
    pObj->fCompl0 = If_IsComplement(pFan0); pFan0 = If_Regular(pFan0);
    pObj->fCompl1 = If_IsComplement(pFan1); pFan1 = If_Regular(pFan1);
    pObj->pFanin0 = pFan0; pFan0->nRefs++; pFan0->nVisits++; pFan0->nVisitsCopy++;
    pObj->pFanin1 = pFan1; pFan1->nRefs++; pFan1->nVisits++; pFan1->nVisitsCopy++;
    pObj->fPhase  = (pObj->fCompl0 ^ pFan0->fPhase) & (pObj->fCompl1 ^ pFan1->fPhase);
    pObj->Level   = 1 + IF_MAX( pFan0->Level, pFan1->Level );
    if ( p->nLevelMax < (int)pObj->Level )
        p->nLevelMax = (int)pObj->Level;
    p->nObjs[IF_AND]++;
    return pObj;
}

/**Function*************************************************************

  Synopsis    [Create the new node assuming it does not exist.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
If_Obj_t * If_ManCreateXor( If_Man_t * p, If_Obj_t * pFan0, If_Obj_t * pFan1 )
{
    If_Obj_t * pRes1, * pRes2;
    pRes1 = If_ManCreateAnd( p, If_Not(pFan0), pFan1 );
    pRes2 = If_ManCreateAnd( p, pFan0, If_Not(pFan1) );
    return If_Not( If_ManCreateAnd( p, If_Not(pRes1), If_Not(pRes2) ) );
}

/**Function*************************************************************

  Synopsis    [Create the new node assuming it does not exist.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
If_Obj_t * If_ManCreateMux( If_Man_t * p, If_Obj_t * pFan0, If_Obj_t * pFan1, If_Obj_t * pCtrl )
{
    If_Obj_t * pRes1, * pRes2;
    pRes1 = If_ManCreateAnd( p, pFan0, If_Not(pCtrl) );
    pRes2 = If_ManCreateAnd( p, pFan1, pCtrl );
    return If_Not( If_ManCreateAnd( p, If_Not(pRes1), If_Not(pRes2) ) );
}

/**Function*************************************************************

  Synopsis    [Creates the choice node.]

  Description [Should be called after the equivalence class nodes are linked.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void If_ManCreateChoice( If_Man_t * p, If_Obj_t * pObj )
{
    If_Obj_t * pTemp;
    // mark the node as a representative if its class
    assert( pObj->fRepr == 0 );
    pObj->fRepr = 1;
    // update the level of this node (needed for correct required time computation)
    for ( pTemp = pObj; pTemp; pTemp = pTemp->pEquiv )
    {
        pObj->Level = IF_MAX( pObj->Level, pTemp->Level );
        pTemp->nVisits++; pTemp->nVisitsCopy++;
    }
    // mark the largest level
    if ( p->nLevelMax < (int)pObj->Level )
        p->nLevelMax = (int)pObj->Level;
467
    p->nChoices++;
Alan Mishchenko committed
468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491
}

/**Function*************************************************************

  Synopsis    [Prepares memory for one cutset.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void If_ManSetupSet( If_Man_t * p, If_Set_t * pSet )
{
    char * pArray;
    int i;
    pSet->nCuts = 0;
    pSet->nCutsMax = p->pPars->nCutsMax;
    pSet->ppCuts = (If_Cut_t **)(pSet + 1);
    pArray = (char *)pSet->ppCuts + sizeof(If_Cut_t *) * (pSet->nCutsMax+1);
    for ( i = 0; i <= pSet->nCutsMax; i++ )
    {
        pSet->ppCuts[i] = (If_Cut_t *)(pArray + i * p->nCutBytes); 
492
        If_CutSetup( p, pSet->ppCuts[i] );
Alan Mishchenko committed
493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515
    }
//    pArray += (pSet->nCutsMax + 1) * p->nCutBytes;
//    assert( ((char *)pArray) - ((char *)pSet) == p->nSetBytes );
}

/**Function*************************************************************

  Synopsis    [Prepares memory for one cut.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void If_ManSetupCutTriv( If_Man_t * p, If_Cut_t * pCut, int ObjId )
{
    pCut->fCompl     = 0;
    pCut->nLimit     = p->pPars->nLutSize;
    pCut->nLeaves    = 1;
    pCut->pLeaves[0] = p->pPars->fLiftLeaves? (ObjId << 8) : ObjId;
    pCut->uSign      = If_ObjCutSign( pCut->pLeaves[0] );
516
    pCut->iCutFunc   = p->pPars->fUseTtPerm ? 3 : (p->pPars->fTruth ? 2: -1);
517
    pCut->uMaskFunc  = 0;
518
    assert( pCut->pLeaves[0] < p->vObjs->nSize );
Alan Mishchenko committed
519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537
}

/**Function*************************************************************

  Synopsis    [Prepares memory for the node with cuts.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
If_Obj_t * If_ManSetupObj( If_Man_t * p )
{
    If_Obj_t * pObj;
    // get memory for the object
    pObj = (If_Obj_t *)Mem_FixedEntryFetch( p->pMemObj );
    memset( pObj, 0, sizeof(If_Obj_t) );
538
    If_CutSetup( p, &pObj->CutBest );
Alan Mishchenko committed
539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566
    // assign ID and save 
    pObj->Id = Vec_PtrSize(p->vObjs);
    Vec_PtrPush( p->vObjs, pObj );
    // set the required times
    pObj->Required = IF_FLOAT_LARGE;
    return pObj;
}

/**Function*************************************************************

  Synopsis    [Prepares memory for one cut.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void If_ManSetupCiCutSets( If_Man_t * p )
{
    If_Obj_t * pObj;
    int i;
    assert( p->pMemCi == NULL );
    // create elementary cuts for the CIs
    If_ManForEachCi( p, pObj, i )
        If_ManSetupCutTriv( p, &pObj->CutBest, pObj->Id );
    // create elementary cutsets for the CIs
Alan Mishchenko committed
567
    p->pMemCi = (If_Set_t *)ABC_ALLOC( char, If_ManCiNum(p) * (sizeof(If_Set_t) + sizeof(void *)) );
Alan Mishchenko committed
568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663
    If_ManForEachCi( p, pObj, i )
    {
        pObj->pCutSet = (If_Set_t *)((char *)p->pMemCi + i * (sizeof(If_Set_t) + sizeof(void *)));
        pObj->pCutSet->nCuts = 1;
        pObj->pCutSet->nCutsMax = p->pPars->nCutsMax;
        pObj->pCutSet->ppCuts = (If_Cut_t **)(pObj->pCutSet + 1);
        pObj->pCutSet->ppCuts[0] = &pObj->CutBest;
    }
}

/**Function*************************************************************

  Synopsis    [Prepares cutset of the node.]

  Description [Elementary cutset will be added last.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
If_Set_t * If_ManSetupNodeCutSet( If_Man_t * p, If_Obj_t * pObj )
{
    assert( If_ObjIsAnd(pObj) );
    assert( pObj->pCutSet == NULL );
//    pObj->pCutSet = (If_Set_t *)Mem_FixedEntryFetch( p->pMemSet );
//    If_ManSetupSet( p, pObj->pCutSet );
    pObj->pCutSet = If_ManCutSetFetch( p );
    pObj->pCutSet->nCuts = 0;
    pObj->pCutSet->nCutsMax = p->pPars->nCutsMax;
    return pObj->pCutSet;
}

/**Function*************************************************************

  Synopsis    [Dereferences cutset of the node.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void If_ManDerefNodeCutSet( If_Man_t * p, If_Obj_t * pObj )
{
    If_Obj_t * pFanin;
    assert( If_ObjIsAnd(pObj) );
    // consider the node
    assert( pObj->nVisits >= 0 );
    if ( pObj->nVisits == 0 )
    {
//        Mem_FixedEntryRecycle( p->pMemSet, (char *)pObj->pCutSet );
        If_ManCutSetRecycle( p, pObj->pCutSet );
        pObj->pCutSet = NULL;
    }
    // consider the first fanin
    pFanin = If_ObjFanin0(pObj);
    assert( pFanin->nVisits > 0 );
    if ( !If_ObjIsCi(pFanin) && --pFanin->nVisits == 0 )
    {
//        Mem_FixedEntryRecycle( p->pMemSet, (char *)pFanin->pCutSet );
        If_ManCutSetRecycle( p, pFanin->pCutSet );
        pFanin->pCutSet = NULL;
    }
    // consider the second fanin
    pFanin = If_ObjFanin1(pObj);
    assert( pFanin->nVisits > 0 );
    if ( !If_ObjIsCi(pFanin) && --pFanin->nVisits == 0 )
    {
//        Mem_FixedEntryRecycle( p->pMemSet, (char *)pFanin->pCutSet );
        If_ManCutSetRecycle( p, pFanin->pCutSet );
        pFanin->pCutSet = NULL;
    }
}

/**Function*************************************************************

  Synopsis    [Dereferences cutset of the node.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void If_ManDerefChoiceCutSet( If_Man_t * p, If_Obj_t * pObj )
{
    If_Obj_t * pTemp;
    assert( If_ObjIsAnd(pObj) );
    assert( pObj->fRepr );
    assert( pObj->nVisits > 0 );
    // consider the nodes in the choice class
    for ( pTemp = pObj; pTemp; pTemp = pTemp->pEquiv )
    {
Alan Mishchenko committed
664
//        assert( pTemp == pObj || pTemp->nVisits == 1 );
Alan Mishchenko committed
665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689
        if ( --pTemp->nVisits == 0 )
        {
//            Mem_FixedEntryRecycle( p->pMemSet, (char *)pTemp->pCutSet );
            If_ManCutSetRecycle( p, pTemp->pCutSet );
            pTemp->pCutSet = NULL;
        }
    }
}

/**Function*************************************************************

  Synopsis    [Dereferences cutset of the node.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void If_ManSetupSetAll( If_Man_t * p, int nCrossCut )
{
    If_Set_t * pCutSet;
    int i, nCutSets;
    nCutSets = 128 + nCrossCut;
Alan Mishchenko committed
690
    p->pFreeList = p->pMemAnd = pCutSet = (If_Set_t *)ABC_ALLOC( char, nCutSets * p->nSetBytes );
Alan Mishchenko committed
691 692 693 694 695 696 697 698 699 700 701 702 703
    for ( i = 0; i < nCutSets; i++ )
    {
        If_ManSetupSet( p, pCutSet );
        if ( i == nCutSets - 1 )
            pCutSet->pNext = NULL;
        else
            pCutSet->pNext = (If_Set_t *)( (char *)pCutSet + p->nSetBytes );
        pCutSet = pCutSet->pNext;
    }
    assert( pCutSet == NULL );

    if ( p->pPars->fVerbose )
    {
704
        Abc_Print( 1, "Node = %7d.  Ch = %5d.  Total mem = %7.2f MB. Peak cut mem = %7.2f MB.\n", 
Alan Mishchenko committed
705 706 707 708
            If_ManAndNum(p), p->nChoices,
            1.0 * (p->nObjBytes + 2*sizeof(void *)) * If_ManObjNum(p) / (1<<20), 
            1.0 * p->nSetBytes * nCrossCut / (1<<20) );
    }
709
//    Abc_Print( 1, "Cross cut = %d.\n", nCrossCut );
Alan Mishchenko committed
710 711 712 713 714 715 716 717

}

////////////////////////////////////////////////////////////////////////
///                       END OF FILE                                ///
////////////////////////////////////////////////////////////////////////


718 719
ABC_NAMESPACE_IMPL_END