wlcNtk.c 53.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/**CFile****************************************************************

  FileName    [wlcNtk.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [Verilog parser.]

  Synopsis    [Network data-structure.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

  Date        [Ver. 1.0. Started - August 22, 2014.]

  Revision    [$Id: wlcNtk.c,v 1.00 2014/09/12 00:00:00 alanmi Exp $]

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

21
#include <math.h>
22
#include "wlc.h"
23
#include "misc/vec/vecWec.h"
24 25 26 27 28 29 30 31

ABC_NAMESPACE_IMPL_START


////////////////////////////////////////////////////////////////////////
///                        DECLARATIONS                              ///
////////////////////////////////////////////////////////////////////////

Alan Mishchenko committed
32 33 34 35
// object types
static char * Wlc_Names[WLC_OBJ_NUMBER+1] = { 
    NULL,                  // 00: unknown
    "pi",                  // 01: primary input 
36 37 38 39
    "po",                  // 02: primary output (unused)
    "ff",                  // 03: flop output
    "bi",                  // 04: flop input (unused)
    "ff",                  // 05: flop (unused)
Alan Mishchenko committed
40 41 42 43 44 45 46
    "const",               // 06: constant
    "buf",                 // 07: buffer
    "mux",                 // 08: multiplexer
    ">>",                  // 09: shift right
    ">>>",                 // 10: shift right (arithmetic)
    "<<",                  // 11: shift left
    "<<<",                 // 12: shift left (arithmetic)
47 48
    "rotR",                // 13: rotate right
    "rotL",                // 14: rotate left
49 50 51 52
    "~",                   // 15: bitwise NOT
    "&",                   // 16: bitwise AND
    "|",                   // 17: bitwise OR
    "^",                   // 18: bitwise XOR
53 54 55 56 57
    "~&",                  // 19: bitwise NAND
    "~|",                  // 20: bitwise NOR
    "~^",                  // 21: bitwise NXOR
    "[:]",                 // 22: bit selection
    "{,}",                 // 23: bit concatenation
58 59
    "zPad",                // 24: zero padding
    "sExt",                // 25: sign extension
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
    "!",                   // 26: logic NOT
    "=>",                  // 27: logic implication
    "&&",                  // 28: logic AND
    "||",                  // 29: logic OR
    "^^",                  // 30: logic XOR
    "==",                  // 31: compare equal
    "!=",                  // 32: compare not equal
    "<",                   // 33: compare less
    ">",                   // 34: compare more
    "<=",                  // 35: compare less or equal
    ">=",                  // 36: compare more or equal
    "&",                   // 37: reduction AND
    "|",                   // 38: reduction OR
    "^",                   // 39: reduction XOR
    "~&",                  // 40: reduction NAND
    "~|",                  // 41: reduction NOR
    "~^",                  // 42: reduction NXOR
    "+",                   // 43: arithmetic addition
    "-",                   // 44: arithmetic subtraction
    "*",                   // 45: arithmetic multiplier
    "/",                   // 46: arithmetic division
81 82 83 84 85
    "%",                   // 47: arithmetic reminder
    "mod",                 // 48: arithmetic modulus
    "**",                  // 49: arithmetic power
    "-",                   // 50: arithmetic minus
    "sqrt",                // 51: integer square root
86
    "squar",               // 52: integer square
87
    "table",               // 53: bit table
Alan Mishchenko committed
88 89
    "READ",                // 54: mem read port
    "WRITE",               // 55: mem write port
90 91 92
    "addsub",              // 56: adder/subtractor
    "sel",                 // 57: selector
    "dec",                 // 58: decoder
93
    "LUT",                 // 59: lookup table
94
    NULL                   // 58: unused
Alan Mishchenko committed
95 96
};

97
char * Wlc_ObjTypeName( Wlc_Obj_t * p ) { return p ? (p->Type < WLC_OBJ_NUMBER ? Wlc_Names[p->Type] : (char *)"out_of_bound") : (char *)"no_obj"; }
98

99 100 101 102 103 104
////////////////////////////////////////////////////////////////////////
///                     FUNCTION DEFINITIONS                         ///
////////////////////////////////////////////////////////////////////////

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

105 106 107 108 109 110 111 112 113 114 115 116
  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Wlc_ManSetDefaultParams( Wlc_Par_t * pPars )
{
    memset( pPars, 0, sizeof(Wlc_Par_t) );
117 118 119 120 121
    pPars->nBitsAdd      = ABC_INFINITY;   // adder bit-width
    pPars->nBitsMul      = ABC_INFINITY;   // multiplier bit-widht 
    pPars->nBitsMux      = ABC_INFINITY;   // MUX bit-width
    pPars->nBitsFlop     = ABC_INFINITY;   // flop bit-width
    pPars->nIterMax      =         1000;   // the max number of iterations
122
    pPars->nLimit        = ABC_INFINITY;   // the max number of signals
123 124 125 126 127
    pPars->fXorOutput    =            1;   // XOR outputs of word-level miter
    pPars->fCheckClauses =            1;   // Check clauses in the reloaded trace                    
    pPars->fPushClauses  =            0;   // Push clauses in the reloaded trace                    
    pPars->fMFFC         =            1;   // Refine the entire MFFC of a PPI 
    pPars->fPdra         =            0;   // Use pdr -nct 
128
    pPars->fLoadTrace    =            1;   // Load previous PDR traces 
129
    pPars->fProofRefine  =            0;   // Use proof-based refinement
130
    pPars->fHybrid       =            1;   // Use a hybrid of CBR and PBR
131
    pPars->fCheckCombUnsat =          0;   // Check if ABS becomes comb. unsat
132
    pPars->fAbs2         =            0;   // Use UFAR style createAbs
133
    pPars->fProofUsePPI  =            0;   // Use PPI values in PBR
Yen-Sheng Ho committed
134
    pPars->fUseBmc3      =            0;   // Run BMC3 in parallel
135
    pPars->fShrinkAbs    =            0;   // Shrink Abs with BMC
136
    pPars->fShrinkScratch=            0;   // Restart pdr from scratch after shrinking
137 138
    pPars->fVerbose      =            0;   // verbose output`
    pPars->fPdrVerbose   =            0;   // show verbose PDR output
139 140 141 142
}

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

143 144 145 146 147 148 149 150 151 152 153 154 155
  Synopsis    [Working with models.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Wlc_Ntk_t * Wlc_NtkAlloc( char * pName, int nObjsAlloc )
{
    Wlc_Ntk_t * p;
    p = ABC_CALLOC( Wlc_Ntk_t, 1 );
156
    p->pName = pName ? Extra_FileNameGeneric( pName ) : NULL;
157 158 159 160 161 162 163 164 165 166 167
    Vec_IntGrow( &p->vPis, 111 );
    Vec_IntGrow( &p->vPos, 111 );
    Vec_IntGrow( &p->vCis, 111 );
    Vec_IntGrow( &p->vCos, 111 );
    Vec_IntGrow( &p->vFfs, 111 );
    p->pMemFanin = Mem_FlexStart();
    p->nObjsAlloc = nObjsAlloc;  
    p->pObjs = ABC_CALLOC( Wlc_Obj_t, p->nObjsAlloc );
    p->iObj = 1;
    return p;
}
168 169 170 171
void Wlc_ObjSetCi( Wlc_Ntk_t * p, Wlc_Obj_t * pObj )
{
    assert( Wlc_ObjIsCi(pObj) );
    assert( Wlc_ObjFaninNum(pObj) == 0 );
172 173 174 175 176 177 178 179 180 181 182 183 184
    if ( Wlc_NtkPiNum(p) == Wlc_NtkCiNum(p) || pObj->Type != WLC_OBJ_PI )
    {
        pObj->Fanins[1] = Vec_IntSize(&p->vCis);
        Vec_IntPush( &p->vCis, Wlc_ObjId(p, pObj) );
    }
    else // insert in the array of CI at the end of PIs
    {
        Wlc_Obj_t * pTemp; int i;
        Vec_IntInsert( &p->vCis, Wlc_NtkPiNum(p), Wlc_ObjId(p, pObj) );
        // other CI IDs are invalidated... naive fix!
        Wlc_NtkForEachCi( p, pTemp, i )
            pTemp->Fanins[1] = i;
    }
185 186 187 188 189 190 191 192 193 194 195 196 197 198
    if ( pObj->Type == WLC_OBJ_PI )
        Vec_IntPush( &p->vPis, Wlc_ObjId(p, pObj) );
}
void Wlc_ObjSetCo( Wlc_Ntk_t * p, Wlc_Obj_t * pObj, int fFlopInput )
{
//    pObj->Fanins[1] = Vec_IntSize(&p->vCos);
    Vec_IntPush( &p->vCos, Wlc_ObjId(p, pObj) );
    if ( !fFlopInput )
        Vec_IntPush( &p->vPos, Wlc_ObjId(p, pObj) );
    if ( fFlopInput )
        pObj->fIsFi = 1;
    else
        pObj->fIsPo = 1;
}
199 200 201
int Wlc_ObjAlloc( Wlc_Ntk_t * p, int Type, int Signed, int End, int Beg )
{
    Wlc_Obj_t * pObj;
202
    assert( Type != WLC_OBJ_PO && Type != WLC_OBJ_FI );
203 204 205 206 207 208 209 210 211 212 213
    if ( p->iObj == p->nObjsAlloc )
    {
        p->pObjs = ABC_REALLOC( Wlc_Obj_t, p->pObjs, 2 * p->nObjsAlloc );
        memset( p->pObjs + p->nObjsAlloc, 0, sizeof(Wlc_Obj_t) * p->nObjsAlloc );
        p->nObjsAlloc *= 2;
    }
    pObj = Wlc_NtkObj( p, p->iObj );
    pObj->Type   = Type;
    pObj->Signed = Signed;
    pObj->End    = End;
    pObj->Beg    = Beg;
214 215
    if ( Wlc_ObjIsCi(pObj) )
        Wlc_ObjSetCi( p, pObj );
216 217 218
    p->nObjs[Type]++;
    return p->iObj++;
}
219 220 221 222 223 224
int Wlc_ObjCreate( Wlc_Ntk_t * p, int Type, int Signed, int End, int Beg, Vec_Int_t * vFanins )
{
    int iFaninNew = Wlc_ObjAlloc( p, Type, Signed, End, Beg );
    Wlc_ObjAddFanins( p, Wlc_NtkObj(p, iFaninNew), vFanins );
    return iFaninNew;
}
225 226 227 228 229 230 231 232 233 234
char * Wlc_ObjName( Wlc_Ntk_t * p, int iObj )
{
    static char Buffer[100];
    if ( Wlc_NtkHasNameId(p) && Wlc_ObjNameId(p, iObj) )
        return Abc_NamStr( p->pManName, Wlc_ObjNameId(p, iObj) );
    sprintf( Buffer, "n%d", iObj );
    return Buffer;
}                          
void Wlc_ObjUpdateType( Wlc_Ntk_t * p, Wlc_Obj_t * pObj, int Type )
{
235
    assert( pObj->Type == WLC_OBJ_NONE );
236 237 238 239 240 241 242 243
    p->nObjs[pObj->Type]--;
    pObj->Type = Type;
    p->nObjs[pObj->Type]++;
}
void Wlc_ObjAddFanins( Wlc_Ntk_t * p, Wlc_Obj_t * pObj, Vec_Int_t * vFanins )
{
    assert( pObj->nFanins == 0 );
    pObj->nFanins = Vec_IntSize(vFanins);
244
    // special treatment of CONST, SELECT and TABLE
245 246
    if ( pObj->Type == WLC_OBJ_CONST )
        pObj->nFanins = 0;
247
    else if ( pObj->Type == WLC_OBJ_BIT_SELECT || pObj->Type == WLC_OBJ_TABLE )
248
        pObj->nFanins = 1;
249 250 251
    if ( Wlc_ObjHasArray(pObj) )
        pObj->pFanins[0] = (int *)Mem_FlexEntryFetch( p->pMemFanin, Vec_IntSize(vFanins) * sizeof(int) );
    memcpy( Wlc_ObjFanins(pObj), Vec_IntArray(vFanins), sizeof(int) * Vec_IntSize(vFanins) );
252 253 254 255 256 257 258
}
void Wlc_NtkFree( Wlc_Ntk_t * p )
{
    if ( p->pManName )
        Abc_NamStop( p->pManName );
    if ( p->pMemFanin )
        Mem_FlexStop( p->pMemFanin, 0 );
259 260
    if ( p->pMemTable )
        Mem_FlexStop( p->pMemTable, 0 );
Alan Mishchenko committed
261
    ABC_FREE( p->vPoPairs.pArray );
262
    Vec_PtrFreeP( &p->vTables );
263
    Vec_WrdFreeP( &p->vLutTruths );
264 265 266 267 268
    ABC_FREE( p->vPis.pArray );
    ABC_FREE( p->vPos.pArray );
    ABC_FREE( p->vCis.pArray );
    ABC_FREE( p->vCos.pArray );
    ABC_FREE( p->vFfs.pArray );
269
    ABC_FREE( p->vFfs2.pArray );
270
    Vec_IntFreeP( &p->vInits );
271
    Vec_IntFreeP( &p->vArsts );
272 273
    ABC_FREE( p->vTravIds.pArray );
    ABC_FREE( p->vNameIds.pArray );
274
    ABC_FREE( p->vValues.pArray );
275
    ABC_FREE( p->vCopies.pArray );
276
    ABC_FREE( p->vBits.pArray );
277
    ABC_FREE( p->vLevels.pArray );
278
    ABC_FREE( p->vRefs.pArray );
279
    ABC_FREE( p->pInits );
280 281
    ABC_FREE( p->pObjs );
    ABC_FREE( p->pName );
282
    ABC_FREE( p->pSpec );
283 284 285 286 287 288 289 290 291 292
    ABC_FREE( p );
}
int Wlc_NtkMemUsage( Wlc_Ntk_t * p )
{
    int Mem = sizeof(Wlc_Ntk_t);
    Mem += 4 * p->vPis.nCap;
    Mem += 4 * p->vPos.nCap;
    Mem += 4 * p->vCis.nCap;
    Mem += 4 * p->vCos.nCap;
    Mem += 4 * p->vFfs.nCap;
293
    Mem += 4 * p->vFfs2.nCap;
294 295 296 297 298
    Mem += sizeof(Wlc_Obj_t) * p->nObjsAlloc;
    Mem += Abc_NamMemUsed(p->pManName);
    Mem += Mem_FlexReadMemUsage(p->pMemFanin);
    return Mem;
}
299 300 301

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

302 303 304 305 306 307 308 309 310
  Synopsis    [Assigns object levels.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
311
int Wlc_NtkCreateLevels_( Wlc_Ntk_t * p )
312 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
{
    Wlc_Obj_t * pObj; 
    int i, k, iFanin, Level, LevelMax = 0;
    Vec_IntFill( &p->vLevels, Wlc_NtkObjNumMax(p), 0 );
    Wlc_NtkForEachObj( p, pObj, i )
    {
        Level = 0;
        Wlc_ObjForEachFanin( pObj, iFanin, k )
            Level = Abc_MaxInt( Level, Wlc_ObjLevelId(p, iFanin) + 1 );
        Vec_IntWriteEntry( &p->vLevels, i, Level );
        LevelMax = Abc_MaxInt( LevelMax, Level );
    }
    return LevelMax;
}
int Wlc_NtkCreateLevelsRev( Wlc_Ntk_t * p )
{
    Wlc_Obj_t * pObj; 
    int i, k, iFanin, Level, LevelMax = 0;
    Vec_IntFill( &p->vLevels, Wlc_NtkObjNumMax(p), 0 );
    Wlc_NtkForEachObjReverse( p, pObj, i )
    {
        if ( Wlc_ObjIsCi(pObj) )
            continue;
        Level = Wlc_ObjLevel(p, pObj) + 1;
        Wlc_ObjForEachFanin( pObj, iFanin, k )
            Vec_IntUpdateEntry( &p->vLevels, iFanin, Level );
        LevelMax = Abc_MaxInt( LevelMax, Level );
    }
    // reverse the values
    Wlc_NtkForEachObj( p, pObj, i )
        Vec_IntWriteEntry( &p->vLevels, i, LevelMax - Wlc_ObjLevelId(p, i) );
    Wlc_NtkForEachCi( p, pObj, i )
        Vec_IntWriteEntry( &p->vLevels, Wlc_ObjId(p, pObj), 0 );
Alan Mishchenko committed
345 346
    //Wlc_NtkForEachObj( p, pObj, i )
    //    printf( "%d -> %d\n", i, Wlc_ObjLevelId(p, i) );
347 348 349 350 351
    return LevelMax;
}

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

352 353 354 355 356 357 358 359 360 361 362
  Synopsis    [Assigns object levels.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Wlc_NtkCreateLevels_rec( Wlc_Ntk_t * p, Wlc_Obj_t * pObj )
{
363 364
    int k, iFanin, Level = 0, iObj = Wlc_ObjId(p, pObj);
    if ( Wlc_ObjIsCi(pObj) || Wlc_ObjIsFf(p, iObj) || Wlc_ObjFaninNum(pObj) == 0 || Wlc_ObjLevel(p, pObj) > 0 )
365 366 367 368 369 370 371 372 373
        return;
    Wlc_ObjForEachFanin( pObj, iFanin, k ) if ( iFanin )
        Wlc_NtkCreateLevels_rec( p, Wlc_NtkObj(p, iFanin) );
    Wlc_ObjForEachFanin( pObj, iFanin, k ) if ( iFanin )
        Level = Abc_MaxInt( Level, Wlc_ObjLevelId(p, iFanin) );
    Vec_IntWriteEntry( &p->vLevels, Wlc_ObjId(p, pObj), Level + 1 );
}
int Wlc_NtkCreateLevels( Wlc_Ntk_t * p )
{
374
    Wlc_Obj_t * pObj;  int i, LeveMax = 0;
375
    Vec_IntFill( &p->vLevels, Wlc_NtkObjNumMax(p), 0 );
376
    Wlc_NtkForEachObj( p, pObj, i )
377
        Wlc_NtkCreateLevels_rec( p, pObj );
378 379 380 381 382 383
    Wlc_NtkForEachObj( p, pObj, i )
        if ( !Wlc_ObjIsCi(pObj) && Wlc_ObjFaninNum(pObj) )
            Vec_IntAddToEntry( &p->vLevels, i, 1 );
    LeveMax = Vec_IntFindMax( &p->vLevels );
    Wlc_NtkForEachFf2( p, pObj, i )
        Vec_IntWriteEntry( &p->vLevels, Wlc_ObjId(p, pObj), LeveMax+1 );
384
    //Wlc_NtkPrintObjects( p ); 
385
    return LeveMax+1;
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
int Wlc_NtkRemapLevels( Wlc_Ntk_t * p, Vec_Int_t * vObjs, int nLevels )
{
    int i, k, iFanin, iObj, Entry, Level = 0, Res = nLevels;
    Vec_Int_t * vMap  = Vec_IntStart( nLevels+1 );
    Vec_Int_t * vUsed = Vec_IntStart( nLevels+1 );
    // mark used levels
    Vec_IntWriteEntry( vUsed, nLevels, 1 );
    Vec_IntForEachEntry( vObjs, iObj, i )
    {
        Vec_IntWriteEntry( vUsed, Wlc_ObjLevelId(p, iObj), 1 );
        Wlc_ObjForEachFanin( Wlc_NtkObj(p, iObj), iFanin, k ) if ( iFanin )
            Vec_IntWriteEntry( vUsed, Wlc_ObjLevelId(p, iFanin), 1 );
    }
    // create level map
    Vec_IntForEachEntry( vUsed, Entry, i )
        if ( Entry )
            Vec_IntWriteEntry( vMap, i, Level++ );
    //printf( "Total used levels %d -> %d\n", nLevels, Level );
    // remap levels
    Vec_IntForEachEntry( &p->vLevels, Level, i )
    {
        if ( Vec_IntEntry(vUsed, Level) )
            Vec_IntWriteEntry( &p->vLevels, i, Vec_IntEntry(vMap, Level) );
        else
            Vec_IntWriteEntry( &p->vLevels, i, -1 );
    }
    Res = Vec_IntEntry( vMap, nLevels );
    Vec_IntFree( vUsed );
    Vec_IntFree( vMap );
    return Res;
}
418 419 420

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

421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437
  Synopsis    [Collects statistics for each side of the miter.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Wlc_NtkCollectStats( Wlc_Ntk_t * p, int nObjs[2][WLC_OBJ_NUMBER] )
{
    Wlc_Obj_t * pObj;
    int n, i;
    if ( Wlc_NtkPoNum(p) != 2 )
        return;
    for ( n = 0; n < 2; n++ )
    {
438
        Wlc_NtkMarkCone( p, n, 1, 1, 0 );
439 440 441 442 443 444
        Wlc_NtkForEachObj( p, pObj, i )
            if ( pObj->Mark )
                nObjs[n][pObj->Type]++;
    }
    Wlc_NtkCleanMarks( p );
}
445 446 447 448
int Wlc_NtkCountRealPis( Wlc_Ntk_t * p )
{
    Wlc_Obj_t * pObj;
    int i, Count = 0;
449
    Wlc_NtkMarkCone( p, -1, -1, 1, 0 );
450 451 452 453 454
    Wlc_NtkForEachPi( p, pObj, i )
        Count += pObj->Mark;
    Wlc_NtkCleanMarks( p );
    return Count;
}
455 456 457

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

458 459 460 461 462 463 464 465 466
  Synopsis    [Prints distribution of operator types.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
467
static inline void Vec_WrdSelectSortCost2( word * pArray, int nSize, word * pCosts )
468
{
469 470 471 472 473 474 475 476 477 478 479 480 481 482
    int i, j, best_i;
    for ( i = 0; i < nSize-1; i++ )
    {
        best_i = i;
        for ( j = i+1; j < nSize; j++ )
            if ( pCosts[j] < pCosts[best_i] )
                best_i = j;
        ABC_SWAP( word, pArray[i], pArray[best_i] );
        ABC_SWAP( word, pCosts[i], pCosts[best_i] );
    }
}
static inline word Wlc_NtkPrintDistribMakeSign( int s, int s0, int s1 )
{
    return ((word)s1 << 42) | ((word)s0 << 21) | (word)s;
483
}
484
static inline void Wlc_NtkPrintDistribFromSign( word sss, int * s, int * s0, int * s1 )
485
{
486
    *s1 =  (int)(sss >> 42);  *s0 = (int)(sss >> 21) & 0x1FFFFF;  *s  =  (int)sss & 0x1FFFFF;
487
}
488
static inline void Wlc_NtkPrintDistribAddOne( Vec_Ptr_t * vTypes, Vec_Ptr_t * vOccurs, int Type, word Sign )
489
{
490 491
    Vec_Wrd_t * vType  = (Vec_Wrd_t *)Vec_PtrEntry( vTypes, Type );
    Vec_Wrd_t * vOccur = (Vec_Wrd_t *)Vec_PtrEntry( vOccurs, Type );
492
    word Entry; int i;
493
    Vec_WrdForEachEntry( vType, Entry, i )
494 495
        if ( Entry == Sign )
        {
496
            Vec_WrdAddToEntry( vOccur, i, 1 );
497 498
            return;
        }
499 500
    Vec_WrdPush( vType, Sign );
    Vec_WrdPush( vOccur, 1 );
501
}
502
void Wlc_NtkPrintDistribSortOne( Vec_Ptr_t * vTypes, Vec_Ptr_t * vOccurs, int Type )
503
{
504 505 506 507 508
    Vec_Wrd_t * vType  = (Vec_Wrd_t *)Vec_PtrEntry( vTypes, Type );
    Vec_Wrd_t * vOccur = (Vec_Wrd_t *)Vec_PtrEntry( vOccurs, Type );
    Vec_WrdSelectSortCost2( Vec_WrdArray(vType), Vec_WrdSize(vType), Vec_WrdArray(vOccur) );
    Vec_WrdReverseOrder( vType );
    Vec_WrdReverseOrder( vOccur );
509
}
510
void Wlc_NtkPrintDistrib( Wlc_Ntk_t * p, int fTwoSides, int fVerbose )
511
{
512
    int nObjs[2][WLC_OBJ_NUMBER] = {{0}}; // counter of objects of each type
513
    Wlc_Obj_t * pObj, * pObjRange = NULL; int nCountRange = 0;
514
    Vec_Ptr_t * vTypes, * vOccurs;
515
    Vec_Int_t * vAnds = Vec_IntStart( WLC_OBJ_NUMBER );
516
    word Sign = 0;
517
    int i, k, s, s0, s1;
518 519 520 521
    if ( Wlc_NtkPoNum(p) != 2 )
        fTwoSides = 0;
    if ( fTwoSides )
        Wlc_NtkCollectStats( p, nObjs );
522 523 524 525 526 527 528 529
    // allocate statistics arrays
    vTypes  = Vec_PtrStart( WLC_OBJ_NUMBER );
    vOccurs = Vec_PtrStart( WLC_OBJ_NUMBER );
    for ( i = 0; i < WLC_OBJ_NUMBER; i++ )
        Vec_PtrWriteEntry( vTypes, i, Vec_WrdAlloc(16) );
    for ( i = 0; i < WLC_OBJ_NUMBER; i++ )
        Vec_PtrWriteEntry( vOccurs, i, Vec_WrdAlloc(16) );
    // add nodes
530 531 532
    Wlc_NtkForEachObj( p, pObj, i )
    {
//        char * pName = Wlc_ObjName(p, i);
533 534 535 536
        if ( Wlc_ObjSign(pObj) > 0x1FFFFF )
            printf( "Object %6d has range %d, which is reduced to %d in the statistics.\n", 
                i, Wlc_ObjRange(pObj), Wlc_ObjRange(pObj) & 0xFFFFF );
        if ( pObj->Beg )
537 538 539 540 541 542
        {
            if ( pObjRange == NULL )
                pObjRange = pObj;
            nCountRange++;
        }
        // 0-input types
543
        if ( Wlc_ObjIsCi(pObj) || pObj->Type == WLC_OBJ_CONST || pObj->Type == WLC_OBJ_BIT_CONCAT )
544 545
            Sign = Wlc_NtkPrintDistribMakeSign( Wlc_ObjSign(pObj), 0, 0 );
        // 1-input types
546
        else if ( pObj->Type == WLC_OBJ_BUF    || pObj->Type == WLC_OBJ_BIT_SELECT  || pObj->Type == WLC_OBJ_TABLE ||
547
             pObj->Type == WLC_OBJ_BIT_ZEROPAD || pObj->Type == WLC_OBJ_BIT_SIGNEXT || 
548
             pObj->Type == WLC_OBJ_BIT_NOT     || pObj->Type == WLC_OBJ_LOGIC_NOT   || pObj->Type == WLC_OBJ_ARI_MINUS )
549 550
            Sign = Wlc_NtkPrintDistribMakeSign( Wlc_ObjSign(pObj), Wlc_ObjSign(Wlc_ObjFanin0(p, pObj)), 0 );
        // 2-input types (including MUX)
Alan Mishchenko committed
551 552
        else if ( Wlc_ObjFaninNum(pObj) == 0 )
            printf( "Object %d with name \"%s\" has type 0. Looks like it was declared by not defined...\n", i, Wlc_ObjName(p, i) );
553 554 555 556 557
        else if ( Wlc_ObjFaninNum(pObj) == 1 )
            Sign = Wlc_NtkPrintDistribMakeSign( Wlc_ObjSign(pObj), Wlc_ObjSign(Wlc_ObjFanin0(p, pObj)), 0 );
        else
        {
            assert( Wlc_ObjFaninNum(pObj) >= 2 );
558
            Sign = Wlc_NtkPrintDistribMakeSign( Wlc_ObjSign(pObj), Wlc_ObjFaninId(pObj, 0) ? Wlc_ObjSign(Wlc_ObjFanin0(p, pObj)) : 0, Wlc_ObjFaninId(pObj, 1) ? Wlc_ObjSign(Wlc_ObjFanin1(p, pObj)) : 0 );
559
        }
560 561
        // add to storage
        Wlc_NtkPrintDistribAddOne( vTypes, vOccurs, pObj->Type, Sign );
562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584
        // count the number of AIG nodes
        if ( pObj->Type == WLC_OBJ_MUX )
            Vec_IntAddToEntry( vAnds, WLC_OBJ_MUX,      3 * Wlc_ObjRange(pObj) * (Wlc_ObjFaninNum(pObj) - 2) );
        else if ( pObj->Type == WLC_OBJ_SHIFT_R )      
            Vec_IntAddToEntry( vAnds, WLC_OBJ_SHIFT_R,  Abc_MinInt(Wlc_ObjRange(Wlc_ObjFanin0(p, pObj)), Abc_Base2Log(Wlc_ObjRange(pObj))) * 3 );
        else if ( pObj->Type == WLC_OBJ_SHIFT_RA )     
            Vec_IntAddToEntry( vAnds, WLC_OBJ_SHIFT_RA, Wlc_ObjRange(pObj) * Abc_MinInt(Wlc_ObjRange(Wlc_ObjFanin0(p, pObj)), Abc_Base2Log(Wlc_ObjRange(pObj))) * 3 );
        else if ( pObj->Type == WLC_OBJ_SHIFT_L )      
            Vec_IntAddToEntry( vAnds, WLC_OBJ_SHIFT_L,  Wlc_ObjRange(pObj) * Abc_MinInt(Wlc_ObjRange(Wlc_ObjFanin0(p, pObj)), Abc_Base2Log(Wlc_ObjRange(pObj))) * 3 );
        else if ( pObj->Type == WLC_OBJ_SHIFT_LA )     
            Vec_IntAddToEntry( vAnds, WLC_OBJ_SHIFT_LA, Wlc_ObjRange(pObj) * Abc_MinInt(Wlc_ObjRange(Wlc_ObjFanin0(p, pObj)), Abc_Base2Log(Wlc_ObjRange(pObj))) * 3 );
        else if ( pObj->Type == WLC_OBJ_ROTATE_R )     
            Vec_IntAddToEntry( vAnds, WLC_OBJ_ROTATE_R, Wlc_ObjRange(pObj) * Abc_MinInt(Wlc_ObjRange(Wlc_ObjFanin0(p, pObj)), Abc_Base2Log(Wlc_ObjRange(pObj))) * 3 );
        else if ( pObj->Type == WLC_OBJ_ROTATE_L )     
            Vec_IntAddToEntry( vAnds, WLC_OBJ_ROTATE_L, Wlc_ObjRange(pObj) * Abc_MinInt(Wlc_ObjRange(Wlc_ObjFanin0(p, pObj)), Abc_Base2Log(Wlc_ObjRange(pObj))) * 3 );
        else if ( pObj->Type == WLC_OBJ_BIT_NOT )     
            Vec_IntAddToEntry( vAnds, WLC_OBJ_BIT_NOT, 0 );
        else if ( pObj->Type == WLC_OBJ_BIT_AND )      
            Vec_IntAddToEntry( vAnds, WLC_OBJ_BIT_AND,      Wlc_ObjRange(Wlc_ObjFanin0(p, pObj)) );
        else if ( pObj->Type == WLC_OBJ_BIT_OR )       
            Vec_IntAddToEntry( vAnds, WLC_OBJ_BIT_OR,       Wlc_ObjRange(Wlc_ObjFanin0(p, pObj)) );
        else if ( pObj->Type == WLC_OBJ_BIT_XOR )      
            Vec_IntAddToEntry( vAnds, WLC_OBJ_BIT_XOR,  3 * Wlc_ObjRange(Wlc_ObjFanin0(p, pObj)) );
585 586 587 588
        else if ( pObj->Type == WLC_OBJ_BIT_NAND )      
            Vec_IntAddToEntry( vAnds, WLC_OBJ_BIT_NAND,     Wlc_ObjRange(Wlc_ObjFanin0(p, pObj)) );
        else if ( pObj->Type == WLC_OBJ_BIT_NOR )       
            Vec_IntAddToEntry( vAnds, WLC_OBJ_BIT_NOR,      Wlc_ObjRange(Wlc_ObjFanin0(p, pObj)) );
589 590
        else if ( pObj->Type == WLC_OBJ_BIT_NXOR )      
            Vec_IntAddToEntry( vAnds, WLC_OBJ_BIT_NXOR, 3 * Wlc_ObjRange(Wlc_ObjFanin0(p, pObj)) );
591 592 593 594 595 596 597 598 599 600
        else if ( pObj->Type == WLC_OBJ_BIT_SELECT )   
            Vec_IntAddToEntry( vAnds, WLC_OBJ_BIT_SELECT, 0 );
        else if ( pObj->Type == WLC_OBJ_BIT_CONCAT )   
            Vec_IntAddToEntry( vAnds, WLC_OBJ_BIT_CONCAT, 0 );
        else if ( pObj->Type == WLC_OBJ_BIT_ZEROPAD )  
            Vec_IntAddToEntry( vAnds, WLC_OBJ_BIT_ZEROPAD, 0 );
        else if ( pObj->Type == WLC_OBJ_BIT_SIGNEXT )  
            Vec_IntAddToEntry( vAnds, WLC_OBJ_BIT_SIGNEXT, 0 );
        else if ( pObj->Type == WLC_OBJ_LOGIC_NOT )    
            Vec_IntAddToEntry( vAnds, WLC_OBJ_LOGIC_NOT,        Wlc_ObjRange(Wlc_ObjFanin0(p, pObj)) - 1 );
601 602
        else if ( pObj->Type == WLC_OBJ_LOGIC_IMPL )    
            Vec_IntAddToEntry( vAnds, WLC_OBJ_LOGIC_IMPL,       Wlc_ObjRange(Wlc_ObjFanin0(p, pObj)) + Wlc_ObjRange(Wlc_ObjFanin1(p, pObj)) - 1 );
603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626
        else if ( pObj->Type == WLC_OBJ_LOGIC_AND )    
            Vec_IntAddToEntry( vAnds, WLC_OBJ_LOGIC_AND,        Wlc_ObjRange(Wlc_ObjFanin0(p, pObj)) + Wlc_ObjRange(Wlc_ObjFanin1(p, pObj)) - 1 );
        else if ( pObj->Type == WLC_OBJ_LOGIC_OR )     
            Vec_IntAddToEntry( vAnds, WLC_OBJ_LOGIC_OR,         Wlc_ObjRange(Wlc_ObjFanin0(p, pObj)) + Wlc_ObjRange(Wlc_ObjFanin1(p, pObj)) - 1 );
        else if ( pObj->Type == WLC_OBJ_LOGIC_XOR )    
            Vec_IntAddToEntry( vAnds, WLC_OBJ_LOGIC_XOR,        Wlc_ObjRange(Wlc_ObjFanin0(p, pObj)) + Wlc_ObjRange(Wlc_ObjFanin1(p, pObj)) + 1 );
        else if ( pObj->Type == WLC_OBJ_COMP_EQU )     
            Vec_IntAddToEntry( vAnds, WLC_OBJ_COMP_EQU,     4 * Wlc_ObjRange(Wlc_ObjFanin0(p, pObj)) - 1 );
        else if ( pObj->Type == WLC_OBJ_COMP_NOTEQU )  
            Vec_IntAddToEntry( vAnds, WLC_OBJ_COMP_NOTEQU,  4 * Wlc_ObjRange(Wlc_ObjFanin0(p, pObj)) - 1 );
        else if ( pObj->Type == WLC_OBJ_COMP_LESS )    
            Vec_IntAddToEntry( vAnds, WLC_OBJ_COMP_LESS,    6 * Wlc_ObjRange(Wlc_ObjFanin0(p, pObj)) - 6 );
        else if ( pObj->Type == WLC_OBJ_COMP_MORE )    
            Vec_IntAddToEntry( vAnds, WLC_OBJ_COMP_MORE,    6 * Wlc_ObjRange(Wlc_ObjFanin0(p, pObj)) - 6 );
        else if ( pObj->Type == WLC_OBJ_COMP_LESSEQU ) 
            Vec_IntAddToEntry( vAnds, WLC_OBJ_COMP_LESSEQU, 6 * Wlc_ObjRange(Wlc_ObjFanin0(p, pObj)) - 6 );
        else if ( pObj->Type == WLC_OBJ_COMP_MOREEQU ) 
            Vec_IntAddToEntry( vAnds, WLC_OBJ_COMP_MOREEQU, 6 * Wlc_ObjRange(Wlc_ObjFanin0(p, pObj)) - 6 );
        else if ( pObj->Type == WLC_OBJ_REDUCT_AND )   
            Vec_IntAddToEntry( vAnds, WLC_OBJ_REDUCT_AND,       Wlc_ObjRange(Wlc_ObjFanin0(p, pObj)) - 1 );
        else if ( pObj->Type == WLC_OBJ_REDUCT_OR )    
            Vec_IntAddToEntry( vAnds, WLC_OBJ_REDUCT_OR,        Wlc_ObjRange(Wlc_ObjFanin0(p, pObj)) - 1 );
        else if ( pObj->Type == WLC_OBJ_REDUCT_XOR )   
            Vec_IntAddToEntry( vAnds, WLC_OBJ_REDUCT_XOR,   3 * Wlc_ObjRange(Wlc_ObjFanin0(p, pObj)) - 3 );
627
        else if ( pObj->Type == WLC_OBJ_REDUCT_NAND )   
628
            Vec_IntAddToEntry( vAnds, WLC_OBJ_REDUCT_NAND,      Wlc_ObjRange(Wlc_ObjFanin0(p, pObj)) - 1 );
629
        else if ( pObj->Type == WLC_OBJ_REDUCT_NOR )    
630
            Vec_IntAddToEntry( vAnds, WLC_OBJ_REDUCT_NOR,       Wlc_ObjRange(Wlc_ObjFanin0(p, pObj)) - 1 );
631
        else if ( pObj->Type == WLC_OBJ_REDUCT_NXOR )   
632
            Vec_IntAddToEntry( vAnds, WLC_OBJ_REDUCT_NXOR,  3 * Wlc_ObjRange(Wlc_ObjFanin0(p, pObj)) - 3 );
633 634 635 636 637 638 639 640
        else if ( pObj->Type == WLC_OBJ_ARI_ADD )       
            Vec_IntAddToEntry( vAnds, WLC_OBJ_ARI_ADD,      9 * Wlc_ObjRange(Wlc_ObjFanin0(p, pObj)) );
        else if ( pObj->Type == WLC_OBJ_ARI_SUB )       
            Vec_IntAddToEntry( vAnds, WLC_OBJ_ARI_SUB,      9 * Wlc_ObjRange(Wlc_ObjFanin0(p, pObj)) );
        else if ( pObj->Type == WLC_OBJ_ARI_MULTI )    
            Vec_IntAddToEntry( vAnds, WLC_OBJ_ARI_MULTI,    9 * Wlc_ObjRange(Wlc_ObjFanin0(p, pObj)) * Wlc_ObjRange(Wlc_ObjFanin1(p, pObj)) );
        else if ( pObj->Type == WLC_OBJ_ARI_DIVIDE )   
            Vec_IntAddToEntry( vAnds, WLC_OBJ_ARI_DIVIDE,  13 * Wlc_ObjRange(Wlc_ObjFanin0(p, pObj)) * Wlc_ObjRange(Wlc_ObjFanin0(p, pObj)) - 19 * Wlc_ObjRange(Wlc_ObjFanin0(p, pObj)) + 10 );
641 642
        else if ( pObj->Type == WLC_OBJ_ARI_REM )  
            Vec_IntAddToEntry( vAnds, WLC_OBJ_ARI_REM,     13 * Wlc_ObjRange(Wlc_ObjFanin0(p, pObj)) * Wlc_ObjRange(Wlc_ObjFanin0(p, pObj)) - 7 * Wlc_ObjRange(Wlc_ObjFanin0(p, pObj)) - 2  );
643 644 645
        else if ( pObj->Type == WLC_OBJ_ARI_MODULUS )  
            Vec_IntAddToEntry( vAnds, WLC_OBJ_ARI_MODULUS, 13 * Wlc_ObjRange(Wlc_ObjFanin0(p, pObj)) * Wlc_ObjRange(Wlc_ObjFanin0(p, pObj)) - 7 * Wlc_ObjRange(Wlc_ObjFanin0(p, pObj)) - 2  );
        else if ( pObj->Type == WLC_OBJ_ARI_POWER ) 
646
            Vec_IntAddToEntry( vAnds, WLC_OBJ_ARI_POWER,   10 * (int)pow((double)Wlc_ObjRange(Wlc_ObjFanin0(p, pObj)),(double)Wlc_ObjRange(Wlc_ObjFanin0(p, pObj))) );
647 648 649 650
        else if ( pObj->Type == WLC_OBJ_ARI_MINUS )   
            Vec_IntAddToEntry( vAnds, WLC_OBJ_ARI_MINUS,    4 * Wlc_ObjRange(Wlc_ObjFanin0(p, pObj)) );
        else if ( pObj->Type == WLC_OBJ_ARI_SQRT )    
            Vec_IntAddToEntry( vAnds, WLC_OBJ_ARI_SQRT,    11 * Wlc_ObjRange(Wlc_ObjFanin0(p, pObj)) * Wlc_ObjRange(Wlc_ObjFanin0(p, pObj)) / 8 + 5 * Wlc_ObjRange(Wlc_ObjFanin0(p, pObj)) / 2 - 5  );
651 652
        else if ( pObj->Type == WLC_OBJ_ARI_SQUARE )    
            Vec_IntAddToEntry( vAnds, WLC_OBJ_ARI_SQUARE,   5 * Wlc_ObjRange(Wlc_ObjFanin0(p, pObj)) * Wlc_ObjRange(Wlc_ObjFanin1(p, pObj))  );
653
    }
654
    if ( nCountRange && Vec_IntSize(&p->vNameIds) > 0 )
655 656 657 658
    {
        printf( "Warning: %d objects of the design have non-zero-based ranges.\n", nCountRange );
        printf( "In particular, object %6d with name \"%s\" has range %d=[%d:%d]\n", Wlc_ObjId(p, pObjRange), 
            Abc_NamStr(p->pManName, Wlc_ObjNameId(p, Wlc_ObjId(p, pObjRange))), Wlc_ObjRange(pObjRange), pObjRange->End, pObjRange->Beg );
659 660
    }
    // print by occurrence
661
    printf( "ID  :  name  occurrence%s    and2 (occurrence)<output_range>=<input_range>.<input_range> ...\n", fTwoSides ? "     Left Share Right":"" );
662 663
    for ( i = 0; i < WLC_OBJ_NUMBER; i++ )
    {
664 665
        Vec_Wrd_t * vType  = (Vec_Wrd_t *)Vec_PtrEntry( vTypes, i );
        Vec_Wrd_t * vOccur = (Vec_Wrd_t *)Vec_PtrEntry( vOccurs, i );
666 667
        if ( p->nObjs[i] == 0 )
            continue;
668
        printf( "%2d  :  %-8s  %6d", i, Wlc_Names[i], p->nObjs[i] );
669
        if ( fTwoSides )
670
        {
671
            int nTotal = i == WLC_OBJ_PI ? Wlc_NtkCountRealPis(p) : p->nObjs[i];
672 673
            printf( "   " );
            printf( "%6d", nObjs[0][i] );
674
            printf( "%6d", nObjs[0][i]+nObjs[1][i]-nTotal );
675 676 677
            printf( "%6d", nObjs[1][i] );
        }
        printf( "%8d ", Vec_IntEntry(vAnds, i) );
678 679
        // sort by occurence
        Wlc_NtkPrintDistribSortOne( vTypes, vOccurs, i );
680
        Vec_WrdForEachEntry( vType, Sign, k )
681 682
        {
            Wlc_NtkPrintDistribFromSign( Sign, &s, &s0, &s1 );
683
            if ( ((k % 6) == 5 && s1) || ((k % 8) == 7 && !s1) )
684
            {
685
                printf( "\n                                " );
686
                if ( fTwoSides )
687 688
                    printf( "                     " );
            }
689
            printf( "(%d)", (int)Vec_WrdEntry( vOccur, k ) );
690
            printf( "%s%d",      Abc_LitIsCompl(s)?"-":"",  Abc_Lit2Var(s) );
691
            if ( s0 )
692
                printf( "=%s%d", Abc_LitIsCompl(s0)?"-":"", Abc_Lit2Var(s0) );
693
            if ( s1 )
694
                printf( ".%s%d", Abc_LitIsCompl(s1)?"-":"", Abc_Lit2Var(s1) );
695 696 697 698
            printf( " " );
        }
        printf( "\n" );
    }
699 700
    Vec_VecFree( (Vec_Vec_t *)vTypes );
    Vec_VecFree( (Vec_Vec_t *)vOccurs );
701
    Vec_IntFree( vAnds );
702
}
703 704 705
void Wlc_NtkPrintNode( Wlc_Ntk_t * p, Wlc_Obj_t * pObj )
{
    printf( "%8d  :  ", Wlc_ObjId(p, pObj) );
706 707
    if ( Vec_IntSize(&p->vLevels) )
        printf( "Lev = %2d  ", Vec_IntEntry(&p->vLevels, Wlc_ObjId(p,pObj)) );
Alan Mishchenko committed
708
    printf( "%6d%s = ", Wlc_ObjRange(pObj), Wlc_ObjIsSigned(pObj) ? "s" : " " );
Alan Mishchenko committed
709 710
    if ( pObj->Type == WLC_OBJ_PI )
    {
711
        printf( "            PI                   :    %-12s\n", Wlc_ObjName(p, Wlc_ObjId(p, pObj)) );
Alan Mishchenko committed
712 713 714 715
        return;
    }
    if ( pObj->Type == WLC_OBJ_FO )
    {
716
        printf( "            FO                   :    %-12s = %-12s\n", Wlc_ObjName(p, Wlc_ObjId(p, pObj)), Wlc_ObjName(p, Wlc_ObjId(p, Wlc_ObjFo2Fi(p, pObj))) );
Alan Mishchenko committed
717 718
        return;
    }
719 720
    if ( pObj->Type != WLC_OBJ_CONST && Wlc_ObjFaninNum(pObj) == 0 )
    {
721
        printf( "Unknown object without fanins    :    %-12s\n", Wlc_ObjName(p, Wlc_ObjId(p, pObj)) );
722 723
        return;
    }
Alan Mishchenko committed
724
    if ( pObj->Type != WLC_OBJ_CONST )
725
    {
Alan Mishchenko committed
726
        printf( "%6d%s  %5s  ",  Wlc_ObjRange(Wlc_ObjFanin0(p, pObj)), Wlc_ObjIsSigned(Wlc_ObjFanin0(p, pObj)) ? "s" : " ", Wlc_Names[(int)pObj->Type] );
727
        if ( Wlc_ObjFaninNum(pObj) > 1 )
Alan Mishchenko committed
728
            printf( "%6d%s ",       Wlc_ObjRange(Wlc_ObjFanin1(p, pObj)), Wlc_ObjIsSigned(Wlc_ObjFanin1(p, pObj)) ? "s" : " " );
729
        else
Alan Mishchenko committed
730
            printf( "        " );
731
        if ( Wlc_ObjFaninNum(pObj) > 2 )
Alan Mishchenko committed
732
            printf( "%6d%s ",       Wlc_ObjRange(Wlc_ObjFanin2(p, pObj)), Wlc_ObjIsSigned(Wlc_ObjFanin2(p, pObj)) ? "s" : " " );
733
        else
Alan Mishchenko committed
734
            printf( "        " );
735
    }
Alan Mishchenko committed
736 737
    else
        printf( "                                " );
738 739 740 741
    printf( " :    " );
    printf( "%-12s",   Wlc_ObjName(p, Wlc_ObjId(p, pObj)) );
    if ( pObj->Type == WLC_OBJ_CONST )
    {
Alan Mishchenko committed
742
        printf( " = %d\'%sh", Wlc_ObjRange(pObj), Wlc_ObjIsSigned(pObj) ? "s":"" );
743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770
        if ( pObj->fXConst )
        {
            int k;
            for ( k = 0; k < (Wlc_ObjRange(pObj) + 3) / 4; k++ )
                printf( "x" );
        }
        else
            Abc_TtPrintHexArrayRev( stdout, (word *)Wlc_ObjConstValue(pObj), (Wlc_ObjRange(pObj) + 3) / 4 );
    }
    else
    {
        printf( " =  %-12s  %5s  ", Wlc_ObjName(p, Wlc_ObjFaninId0(pObj)), Wlc_Names[(int)pObj->Type] );
        if ( Wlc_ObjFaninNum(pObj) > 1 )
            printf( "%-12s ",       Wlc_ObjName(p, Wlc_ObjFaninId1(pObj)) );
        else
            printf( "             " );
        if ( Wlc_ObjFaninNum(pObj) > 2 )
            printf( "%-12s ",       Wlc_ObjName(p, Wlc_ObjFaninId2(pObj)) );
    }
    printf( "\n" );
}
void Wlc_NtkPrintNodeArray( Wlc_Ntk_t * p, Vec_Int_t * vArray )
{
    Wlc_Obj_t * pObj; 
    int i;
    Wlc_NtkForEachObjVec( vArray, p, pObj, i )
        Wlc_NtkPrintNode( p, pObj );
}
771 772 773 774 775 776 777 778 779
void Wlc_NtkPrintNodes( Wlc_Ntk_t * p, int Type )
{
    Wlc_Obj_t * pObj; 
    int i, Counter = 0;
    printf( "Operation %s\n", Wlc_Names[Type] );
    Wlc_NtkForEachObj( p, pObj, i )
    {
        if ( (int)pObj->Type != Type )
            continue;
780 781
        printf( "%8d  :", Counter++ );
        Wlc_NtkPrintNode( p, pObj );
782 783
    }
}
784
void Wlc_NtkPrintStats( Wlc_Ntk_t * p, int fDistrib, int fTwoSides, int fVerbose )
785 786 787
{
    int i;
    printf( "%-20s : ",        p->pName );
788
    printf( "PI = %4d  ",      Wlc_NtkCountRealPis(p) ); //Wlc_NtkPiNum(p) );
789 790
    printf( "PO = %4d  ",      Wlc_NtkPoNum(p) );
    printf( "FF = %4d  ",      Wlc_NtkFfNum(p) );
791
    printf( "Obj = %6d  ",     Wlc_NtkObjNum(p) - Wlc_NtkPiNum(p) - Wlc_NtkPoNum(p) - Wlc_NtkFfNum(p) );
792 793
    printf( "Mem = %.3f MB",   1.0*Wlc_NtkMemUsage(p)/(1<<20) );
    printf( "\n" );
794 795
    if ( fDistrib )
    {
796
        Wlc_NtkPrintDistrib( p, fTwoSides, fVerbose );
797 798
        return;
    }
799 800
    if ( !fVerbose )
        return;
801 802 803 804 805 806 807 808 809 810
    printf( "Node type statistics:\n" );
    for ( i = 1; i < WLC_OBJ_NUMBER; i++ )
    {
        if ( !p->nObjs[i] )
            continue;
        if ( p->nAnds[0] && p->nAnds[i] )
            printf( "%2d  :  %-8s  %6d  %7.2f %%\n", i, Wlc_Names[i], p->nObjs[i], 100.0*p->nAnds[i]/p->nAnds[0] );
        else
            printf( "%2d  :  %-8s  %6d\n", i, Wlc_Names[i], p->nObjs[i] );
    }
811
}
Alan Mishchenko committed
812 813 814 815 816 817
void Wlc_NtkPrintObjects( Wlc_Ntk_t * p )
{
    Wlc_Obj_t * pObj; int i;
    Wlc_NtkForEachObj( p, pObj, i )
        Wlc_NtkPrintNode( p, pObj );
}
818 819 820

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

821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837
  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Wlc_NtkTransferNames( Wlc_Ntk_t * pNew, Wlc_Ntk_t * p )
{
    int i;
    assert( !Wlc_NtkHasCopy(pNew)   && Wlc_NtkHasCopy(p)   );
    assert( !Wlc_NtkHasNameId(pNew) && Wlc_NtkHasNameId(p) );
    assert( pNew->pManName  == NULL && p->pManName != NULL );
    Wlc_NtkCleanNameId( pNew );
    for ( i = 0; i < p->nObjsAlloc; i++ )
838
        if ( Wlc_ObjCopy(p, i) > 0 && i < Vec_IntSize(&p->vNameIds) && Wlc_ObjNameId(p, i) )
839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855
            Wlc_ObjSetNameId( pNew, Wlc_ObjCopy(p, i), Wlc_ObjNameId(p, i) );
    pNew->pManName = p->pManName; 
    p->pManName = NULL;
    Vec_IntErase( &p->vNameIds );
    // transfer table
    pNew->pMemTable = p->pMemTable;  p->pMemTable = NULL;
    pNew->vTables = p->vTables;      p->vTables = NULL;
}
char * Wlc_NtkNewName( Wlc_Ntk_t * p, int iCoId, int fSeq )
{
    static char pBuffer[1000];
    sprintf( pBuffer, "%s_o%d_%s", p->pName, iCoId, fSeq ? "seq": "comb" );
    return pBuffer;
}

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

856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871
  Synopsis    [Reduce init vector.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Vec_Int_t * Wlc_ReduceMarkedInitVec( Wlc_Ntk_t * p, Vec_Int_t * vInit )
{
    Vec_Int_t * vInitNew = Vec_IntDup( vInit );
    Wlc_Obj_t * pObj; int i, k = 0;
    assert( Vec_IntSize(vInit) == Wlc_NtkCiNum(p) - Wlc_NtkPiNum(p) );
    Wlc_NtkForEachCi( p, pObj, i )
        if ( !Wlc_ObjIsPi(pObj) && pObj->Mark )
872
            Vec_IntWriteEntry( vInitNew, k++, Vec_IntEntry(vInit, i - Wlc_NtkPiNum(p)) );
873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894
    Vec_IntShrink( vInitNew, k );
    return vInitNew;
}
char * Wlc_ReduceMarkedInitStr( Wlc_Ntk_t * p, char * pInit )
{
    char * pInitNew = Abc_UtilStrsav( pInit );
    Wlc_Obj_t * pObj; int i, b, nBits = 0, k = 0;
    Wlc_NtkForEachCi( p, pObj, i )
    {
        if ( !Wlc_ObjIsPi(pObj) && pObj->Mark )
            for ( b = 0; b < Wlc_ObjRange(pObj); b++ )
                pInitNew[k++] = pInitNew[nBits+b];
        if ( !Wlc_ObjIsPi(pObj) )
            nBits += Wlc_ObjRange(pObj);
    }
    pInitNew[k] = '\0';
    assert( nBits == (int)strlen(pInit) );
    return pInitNew;
}

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

895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918
  Synopsis    [Duplicates the network in a topological order.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Wlc_ObjCollectCopyFanins( Wlc_Ntk_t * p, int iObj, Vec_Int_t * vFanins )
{
    int i, iFanin;
    Wlc_Obj_t * pObj = Wlc_NtkObj( p, iObj );
    Vec_IntClear( vFanins );
    Wlc_ObjForEachFanin( pObj, iFanin, i )
        Vec_IntPush( vFanins, Wlc_ObjCopy(p, iFanin) );
    // special treatment of CONST and SELECT
    if ( pObj->Type == WLC_OBJ_CONST )
    {
        int * pInts = Wlc_ObjConstValue( pObj );
        int nInts = Abc_BitWordNum( Wlc_ObjRange(pObj) );
        for ( i = 0; i < nInts; i++ )
            Vec_IntPush( vFanins, pInts[i] );
    }
919 920 921 922 923 924
    else if ( pObj->Type == WLC_OBJ_BIT_SELECT )
    {
        assert( Vec_IntSize(vFanins) == 1 );
        Vec_IntPushTwo( vFanins, Wlc_ObjRangeEnd(pObj), Wlc_ObjRangeBeg(pObj) );
    }
    else if ( pObj->Type == WLC_OBJ_TABLE )
925 926 927 928 929 930 931 932
    {
        assert( Vec_IntSize(vFanins) == 1 );
        Vec_IntPush( vFanins, pObj->Fanins[1] );
    }
}
int Wlc_ObjDup( Wlc_Ntk_t * pNew, Wlc_Ntk_t * p, int iObj, Vec_Int_t * vFanins )
{
    Wlc_Obj_t * pObj = Wlc_NtkObj( p, iObj );
933
    int iFaninNew = Wlc_ObjAlloc( pNew, pObj->Type, Wlc_ObjIsSigned(pObj), pObj->End, pObj->Beg );
934 935 936 937
    Wlc_Obj_t * pObjNew = Wlc_NtkObj(pNew, iFaninNew);
    Wlc_ObjCollectCopyFanins( p, iObj, vFanins );
    Wlc_ObjAddFanins( pNew, pObjNew, vFanins );
    Wlc_ObjSetCopy( p, iObj, iFaninNew );
938
    pObjNew->fXConst = pObj->fXConst;
939 940 941 942 943 944
    return iFaninNew;
}
void Wlc_NtkDupDfs_rec( Wlc_Ntk_t * pNew, Wlc_Ntk_t * p, int iObj, Vec_Int_t * vFanins )
{
    Wlc_Obj_t * pObj;
    int i, iFanin;
945 946
    if ( iObj == 0 )
        return;
947 948 949
    if ( Wlc_ObjCopy(p, iObj) )
        return;
    pObj = Wlc_NtkObj( p, iObj );
950
    //printf( "Visiting node %16s (ID %6d) of type %5s (type ID %2d)\n", Wlc_ObjName(p, iObj), iObj, Wlc_ObjTypeName(pObj), Wlc_NtkObj(p, iObj)->Type );
951
    assert( pObj->Type != WLC_OBJ_FF );
952 953 954 955
    Wlc_ObjForEachFanin( pObj, iFanin, i )
        Wlc_NtkDupDfs_rec( pNew, p, iFanin, vFanins );
    Wlc_ObjDup( pNew, p, iObj, vFanins );
}
956 957 958 959 960 961 962 963 964 965 966

Wlc_Ntk_t * Wlc_NtkDupDfsSimple( Wlc_Ntk_t * p )
{
    Wlc_Ntk_t * pNew;
    Wlc_Obj_t * pObj;
    Vec_Int_t * vFanins;
    int i;
    Wlc_NtkCleanCopy( p );
    vFanins = Vec_IntAlloc( 100 );
    pNew = Wlc_NtkAlloc( p->pName, p->nObjsAlloc );
    pNew->fSmtLib = p->fSmtLib;
967
    pNew->fAsyncRst = p->fAsyncRst;
968 969
    pNew->fMemPorts = p->fMemPorts;
    pNew->fEasyFfs = p->fEasyFfs;
970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985
    Wlc_NtkForEachCi( p, pObj, i )
        Wlc_ObjDup( pNew, p, Wlc_ObjId(p, pObj), vFanins );
    Wlc_NtkForEachCo( p, pObj, i )
        Wlc_NtkDupDfs_rec( pNew, p, Wlc_ObjId(p, pObj), vFanins );
    Wlc_NtkForEachCo( p, pObj, i )
        Wlc_ObjSetCo( pNew, Wlc_ObjCopyObj(pNew, p, pObj), pObj->fIsFi );
    if ( p->vInits )
    pNew->vInits = Vec_IntDup( p->vInits );
    if ( p->pInits )
    pNew->pInits = Abc_UtilStrsav( p->pInits );
    Vec_IntFree( vFanins );
    if ( p->pSpec )
    pNew->pSpec = Abc_UtilStrsav( p->pSpec );
    return pNew;
}

986
Wlc_Ntk_t * Wlc_NtkDupDfs( Wlc_Ntk_t * p, int fMarked, int fSeq )
987 988
{
    Wlc_Ntk_t * pNew;
989
    Wlc_Obj_t * pObj, * pObjNew;
990
    Vec_Int_t * vFanins;
991
    int i, k, iObj, iFanin;
992
    vFanins = Vec_IntAlloc( 100 );
993
    Wlc_NtkCleanCopy( p );
994
    pNew = Wlc_NtkAlloc( p->pName, p->nObjsAlloc );
995
    pNew->fSmtLib = p->fSmtLib;
996
    pNew->fAsyncRst = p->fAsyncRst;
997 998
    pNew->fMemPorts = p->fMemPorts;
    pNew->fEasyFfs = p->fEasyFfs;
999 1000
    Wlc_NtkForEachCi( p, pObj, i )
        if ( !fMarked || pObj->Mark )
1001 1002
        {
            unsigned Type = pObj->Type;
1003
            if ( !fSeq ) pObj->Type = WLC_OBJ_PI;
1004
            Wlc_ObjDup( pNew, p, Wlc_ObjId(p, pObj), vFanins );
1005 1006
            pObj->Type = Type;
        }
1007 1008 1009 1010 1011 1012
    Wlc_NtkForEachFf2( p, pObj, i )
    {
        int iObjNew = Wlc_ObjAlloc( pNew, pObj->Type, Wlc_ObjIsSigned(pObj), pObj->End, pObj->Beg );
        Wlc_ObjSetCopy( p, Wlc_ObjId(p, pObj), iObjNew );
        Vec_IntPush( &pNew->vFfs2, iObjNew );
    }
1013
    Wlc_NtkForEachCo( p, pObj, i )
1014 1015
        if ( !fMarked || pObj->Mark )
            Wlc_NtkDupDfs_rec( pNew, p, Wlc_ObjId(p, pObj), vFanins );
1016
    Wlc_NtkForEachCo( p, pObj, i )
1017
        if ( !fMarked || pObj->Mark )
1018
            Wlc_ObjSetCo( pNew, Wlc_ObjCopyObj(pNew, p, pObj), fSeq ? pObj->fIsFi : 0 );
1019 1020 1021 1022 1023 1024 1025 1026 1027 1028
    Wlc_NtkForEachFf2( p, pObj, i )
    {
        iObj = Wlc_ObjId(p, pObj);
        Wlc_ObjForEachFanin( pObj, iFanin, k )
            Wlc_NtkDupDfs_rec( pNew, p, iFanin, vFanins );
        Wlc_ObjCollectCopyFanins( p, iObj, vFanins );
        pObjNew = Wlc_NtkObj( pNew, Wlc_ObjCopy(p, iObj) );
        Wlc_ObjAddFanins( pNew, pObjNew, vFanins );
        pObjNew->fXConst = pObj->fXConst;
    }
1029
    Vec_IntFree( vFanins );
1030
    if ( fSeq && p->vInits )
1031
    {
1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045
        if ( fMarked )
        {
            if ( p->vInits )
                pNew->vInits = Wlc_ReduceMarkedInitVec( p, p->vInits );
            if ( p->pInits )
                pNew->pInits = Wlc_ReduceMarkedInitStr( p, p->pInits );
        }
        else
        {
            if ( p->vInits )
                pNew->vInits = Vec_IntDup( p->vInits );
            if ( p->pInits )
                pNew->pInits = Abc_UtilStrsav( p->pInits );
        }
1046
    }
1047
    if ( p->pSpec )
1048
        pNew->pSpec = Abc_UtilStrsav( p->pSpec );
Alan Mishchenko committed
1049 1050
    if ( Wlc_NtkHasNameId(p) )
        Wlc_NtkTransferNames( pNew, p );
Alan Mishchenko committed
1051 1052
    if ( Vec_IntSize(&p->vPoPairs) )
        Vec_IntAppend( &pNew->vPoPairs, &p->vPoPairs );
1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063
    return pNew;
}
Wlc_Ntk_t * Wlc_NtkDupDfsAbs( Wlc_Ntk_t * p, Vec_Int_t * vPisOld, Vec_Int_t * vPisNew, Vec_Int_t * vFlops )
{
    Wlc_Ntk_t * pNew;
    Wlc_Obj_t * pObj;
    Vec_Int_t * vFanins;
    int i;
    Wlc_NtkCleanCopy( p );
    pNew = Wlc_NtkAlloc( p->pName, p->nObjsAlloc );
    pNew->fSmtLib = p->fSmtLib;
1064
    pNew->fAsyncRst = p->fAsyncRst;
1065 1066
    pNew->fMemPorts = p->fMemPorts;
    pNew->fEasyFfs = p->fEasyFfs;
1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119

    // duplicate marked PIs
    vFanins = Vec_IntAlloc( 100 );
    Wlc_NtkForEachObjVec( vPisOld, p, pObj, i )
    {
        assert( Wlc_ObjIsPi(pObj) );
        Wlc_ObjDup( pNew, p, Wlc_ObjId(p, pObj), vFanins );
    }
    // duplicate additional PIs
    Wlc_NtkForEachObjVec( vPisNew, p, pObj, i )
    {
        unsigned Type = pObj->Type;
        int nFanins = Wlc_ObjFaninNum(pObj);
        assert( !Wlc_ObjIsPi(pObj) );
        pObj->Type = WLC_OBJ_PI;
        pObj->nFanins = 0;
        Wlc_ObjDup( pNew, p, Wlc_ObjId(p, pObj), vFanins );
        pObj->Type = Type;
        pObj->nFanins = (unsigned)nFanins;
    }
    // duplicate flop outputs
    Wlc_NtkForEachObjVec( vFlops, p, pObj, i )
    {
        assert( !Wlc_ObjIsPi(pObj) && Wlc_ObjIsCi(pObj) );
        Wlc_ObjDup( pNew, p, Wlc_ObjId(p, pObj), vFanins );
    }

    // duplicate logic cones of primary outputs
    Wlc_NtkForEachPo( p, pObj, i )
        Wlc_NtkDupDfs_rec( pNew, p, Wlc_ObjId(p, pObj), vFanins );
    // duplidate logic cone of flop inputs
    Wlc_NtkForEachObjVec( vFlops, p, pObj, i )
        Wlc_NtkDupDfs_rec( pNew, p, Wlc_ObjId(p, Wlc_ObjFo2Fi(p, pObj)), vFanins );

    // duplicate POs
    Wlc_NtkForEachPo( p, pObj, i )
        Wlc_ObjSetCo( pNew, Wlc_ObjCopyObj(pNew, p, pObj), 0 );
    // duplicate flop inputs 
    Wlc_NtkForEachObjVec( vFlops, p, pObj, i )
        Wlc_ObjSetCo( pNew, Wlc_ObjCopyObj(pNew, p, Wlc_ObjFo2Fi(p, pObj)), 1 );
    Vec_IntFree( vFanins );

    // mark flop outputs
    Wlc_NtkForEachObjVec( vFlops, p, pObj, i )
        pObj->Mark = 1;
    if ( p->vInits )
        pNew->vInits = Wlc_ReduceMarkedInitVec( p, p->vInits );
    if ( p->pInits )
        pNew->pInits = Wlc_ReduceMarkedInitStr( p, p->pInits );
    Wlc_NtkCleanMarks( p );

    if ( p->pSpec )
        pNew->pSpec = Abc_UtilStrsav( p->pSpec );
1120
    //Wlc_NtkTransferNames( pNew, p );
1121 1122 1123
    return pNew;
}

1124
/**Function*************************************************************
1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141

  Synopsis    [Select the cone of the given output.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Wlc_NtkCleanMarks( Wlc_Ntk_t * p )
{
    Wlc_Obj_t * pObj;
    int i;
    Wlc_NtkForEachObj( p, pObj, i )
        pObj->Mark = 0;
}
1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165
int Wlc_NtkCountMarked( Wlc_Ntk_t * p, int * pnPis, int * pnFos, int * pnAdders, int * pnMults )
{
    Wlc_Obj_t * pObj;
    int i, nNodes = 0;
    *pnPis = *pnFos = *pnAdders = *pnMults = 0;
    Wlc_NtkForEachObj( p, pObj, i )
    {
        if ( !pObj->Mark )
            continue;
        if ( Wlc_ObjIsPi(pObj) )
            (*pnPis)++;
        else if ( Wlc_ObjIsCi(pObj) )
            (*pnFos)++;
        else if ( pObj->Mark )
        {
            nNodes++;
            if ( pObj->Type == WLC_OBJ_ARI_ADD || pObj->Type == WLC_OBJ_ARI_SUB )
                (*pnAdders)++;
            else if ( pObj->Type == WLC_OBJ_ARI_MULTI )
                (*pnMults)++;
        }
    }
    return nNodes;
}
1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177
void Wlc_NtkMarkCone_rec( Wlc_Ntk_t * p, Wlc_Obj_t * pObj, Vec_Int_t * vFlops )
{
    int i, iFanin;
    if ( pObj->Mark )
        return;
    pObj->Mark = 1;
    if ( Wlc_ObjIsCi(pObj) )
    {
        if ( !Wlc_ObjIsPi(pObj) )
            Vec_IntPush( vFlops, Wlc_ObjCiId(pObj) );
        return;
    }
1178
    Wlc_ObjForEachFanin( pObj, iFanin, i ) if ( iFanin )
1179 1180
        Wlc_NtkMarkCone_rec( p, Wlc_NtkObj(p, iFanin), vFlops );
}
1181
void Wlc_NtkMarkCone( Wlc_Ntk_t * p, int iCoId, int Range, int fSeq, int fAllPis )
1182 1183 1184 1185 1186
{
    Vec_Int_t * vFlops;
    Wlc_Obj_t * pObj;
    int i, CiId, CoId;
    Wlc_NtkCleanMarks( p );
1187 1188 1189
    if ( fAllPis )
    Wlc_NtkForEachPi( p, pObj, i )
        pObj->Mark = 1;
1190
    vFlops = Vec_IntAlloc( 100 );
1191
    Wlc_NtkForEachCo( p, pObj, i )
1192
        if ( iCoId == -1 || (i >= iCoId && i < iCoId + Range) )
1193
            Wlc_NtkMarkCone_rec( p, pObj, vFlops );
1194
    if ( fSeq )
1195 1196 1197 1198 1199 1200 1201
    Vec_IntForEachEntry( vFlops, CiId, i )
    {
        CoId = Wlc_NtkPoNum(p) + CiId - Wlc_NtkPiNum(p);
        Wlc_NtkMarkCone_rec( p, Wlc_NtkCo(p, CoId), vFlops );
    }
    Vec_IntFree( vFlops );
}
1202 1203 1204 1205 1206 1207
void Wlc_NtkProfileCones( Wlc_Ntk_t * p )
{
    Wlc_Obj_t * pObj; 
    int i, nPis, nFos, nNodes, nAdders, nMults;
    Wlc_NtkForEachCo( p, pObj, i )
    {
1208
        Wlc_NtkMarkCone( p, i, 1, 0, 0 );
1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219
        nNodes = Wlc_NtkCountMarked( p, &nPis, &nFos, &nAdders, &nMults );
        printf( "Cone %5d : ", i );
        printf( "PI = %4d  ", nPis );
        printf( "FO = %4d  ", nFos );
        printf( "Node = %6d  ", nNodes );
        printf( "Add/Sub = %4d  ", nAdders );
        printf( "Mult = %4d  ", nMults );
        printf( "\n" );
    }
    Wlc_NtkCleanMarks( p );
}
1220 1221

/**Function*************************************************************
1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246

  Synopsis    [Duplicates the network by copying each node.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Wlc_Ntk_t * Wlc_NtkDupSingleNodes( Wlc_Ntk_t * p )
{
    Wlc_Ntk_t * pNew;
    Vec_Int_t * vFanins;
    Wlc_Obj_t * pObj, * pObjNew;
    Wlc_Obj_t * pFanin, * pFaninNew;
    int i, k, iFanin, iFaninNew, iObjNew, Count = 0;
    // count objects
    Wlc_NtkForEachObj( p, pObj, i )
        if ( !Wlc_ObjIsCi(pObj) )
            Count += 1 + Wlc_ObjFaninNum(pObj);
    // copy objects
    Wlc_NtkCleanCopy( p );
    vFanins = Vec_IntAlloc( 100 );
    pNew = Wlc_NtkAlloc( p->pName, p->nObjsAlloc );
1247
    pNew->fSmtLib = p->fSmtLib;
1248
    pNew->fAsyncRst = p->fAsyncRst;
1249 1250
    pNew->fMemPorts = p->fMemPorts;
    pNew->fEasyFfs = p->fEasyFfs;
1251 1252 1253 1254 1255 1256
    Wlc_NtkForEachObj( p, pObj, i )
    {
        if ( Wlc_ObjIsCi(pObj) )
            continue;
        if ( pObj->Type == WLC_OBJ_ARI_MULTI )
            continue;
1257 1258
        if ( pObj->Type == WLC_OBJ_MUX && Wlc_ObjFaninNum(pObj) > 3 )
            continue;
1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275
        // create CIs for the fanins
        Wlc_ObjForEachFanin( pObj, iFanin, k )
        {
            pFanin = Wlc_NtkObj(p, iFanin);
            iFaninNew = Wlc_ObjAlloc( pNew, WLC_OBJ_PI, pFanin->Signed, pFanin->End, pFanin->Beg );
            pFaninNew = Wlc_NtkObj(pNew, iFaninNew);
            Wlc_ObjSetCopy( p, iFanin, iFaninNew );
            //Wlc_ObjSetCi( pNew, pFaninNew );
        }
        // create object
        iObjNew = Wlc_ObjDup( pNew, p, i, vFanins );
        pObjNew = Wlc_NtkObj(pNew, iObjNew);
        pObjNew->fIsPo = 1;
        Vec_IntPush( &pNew->vPos, iObjNew );
    }
    Vec_IntFree( vFanins );
    Wlc_NtkTransferNames( pNew, p );
1276 1277
    if ( p->pSpec )
    pNew->pSpec = Abc_UtilStrsav( p->pSpec );
1278 1279 1280
    return pNew;
}

1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294
/**Function*************************************************************

  Synopsis    [Creates short names for all objects.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Wlc_NtkShortNames( Wlc_Ntk_t * p )
{
    Wlc_Obj_t * pObj;
Alan Mishchenko committed
1295
    char pBuffer[1000];
Alan Mishchenko committed
1296
    int NameId, fFound, i;
1297
    int nFlops = Wlc_NtkCoNum(p) - Wlc_NtkPoNum(p);
Alan Mishchenko committed
1298
    unsigned char nDigits = (unsigned char)Abc_Base10Log( nFlops );
1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338
    Wlc_NtkForEachCo( p, pObj, i )
    {
        if ( Wlc_ObjIsPo(pObj) )
            continue;
        sprintf( pBuffer, "%s%0*d", "fi", nDigits, i - Wlc_NtkPoNum(p) );
        NameId = Abc_NamStrFindOrAdd( p->pManName, pBuffer, &fFound );
        Wlc_ObjSetNameId( p, Wlc_ObjId(p, pObj), NameId );
    }
    Wlc_NtkForEachCi( p, pObj, i )
    {
        if ( Wlc_ObjIsPi(pObj) )
            continue;
        sprintf( pBuffer, "%s%0*d", "fo", nDigits, i - Wlc_NtkPiNum(p) );
        NameId = Abc_NamStrFindOrAdd( p->pManName, pBuffer, &fFound );
        Wlc_ObjSetNameId( p, Wlc_ObjId(p, pObj), NameId );
    }
    nDigits = Abc_Base10Log( Wlc_NtkPoNum(p) );
    Wlc_NtkForEachPo( p, pObj, i )
    {
        sprintf( pBuffer, "%s%0*d", "po", nDigits, i );
        NameId = Abc_NamStrFindOrAdd( p->pManName, pBuffer, &fFound );
        Wlc_ObjSetNameId( p, Wlc_ObjId(p, pObj), NameId );
    }
    nDigits = Abc_Base10Log( Wlc_NtkPiNum(p) );
    Wlc_NtkForEachPi( p, pObj, i )
    {
        sprintf( pBuffer, "%s%0*d", "pi", nDigits, i );
        NameId = Abc_NamStrFindOrAdd( p->pManName, pBuffer, &fFound );
        Wlc_ObjSetNameId( p, Wlc_ObjId(p, pObj), NameId );
    }
    nDigits = Abc_Base10Log( Wlc_NtkObjNum(p) );
    Wlc_NtkForEachObj( p, pObj, i )
    {
        if ( Wlc_ObjIsCi(pObj) || Wlc_ObjIsCo(pObj) )
            continue;
        sprintf( pBuffer, "%s%0*d", "n", nDigits, i );
        NameId = Abc_NamStrFindOrAdd( p->pManName, pBuffer, &fFound );
        Wlc_ObjSetNameId( p, Wlc_ObjId(p, pObj), NameId );
    }
}
1339

1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403
/**Function*************************************************************

  Synopsis    [Count the number of flops initialized to DC value.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Wlc_NtkDcFlopNum( Wlc_Ntk_t * p )
{
    int i, nFlops, Count = 0;
    if ( p->pInits == NULL )
        return 0;
    nFlops = strlen(p->pInits);
    for ( i = 0; i < nFlops; i++ )
        Count += (p->pInits[i] == 'x' || p->pInits[i] == 'X');
    return Count;
}

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

  Synopsis    [Create references.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Wlc_NtkSetRefs( Wlc_Ntk_t * p )
{
    Wlc_Obj_t * pObj; int i, k, Fanin;
    Vec_IntFill( &p->vRefs, Wlc_NtkObjNumMax(p), 0 );
    Wlc_NtkForEachObj( p, pObj, i )
        Wlc_ObjForEachFanin( pObj, Fanin, k )
            Vec_IntAddToEntry( &p->vRefs, Fanin, 1 );
    Wlc_NtkForEachCo( p, pObj, i )
        Vec_IntAddToEntry( &p->vRefs, Wlc_ObjId(p, pObj), 1 );
}

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

  Synopsis    [This procedure simply count the number of PPI bits.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Wlc_NtkCountObjBits( Wlc_Ntk_t * p, Vec_Int_t * vPisNew )
{
    Wlc_Obj_t * pObj; 
    int i, Count = 0;
    Wlc_NtkForEachObjVec( vPisNew, p, pObj, i )
        Count += Wlc_ObjRange(pObj);
    return Count;
}

1404 1405 1406 1407 1408 1409 1410
////////////////////////////////////////////////////////////////////////
///                       END OF FILE                                ///
////////////////////////////////////////////////////////////////////////


ABC_NAMESPACE_IMPL_END