giaMuxes.c 28.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
/**CFile****************************************************************

  FileName    [giaMuxes.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [Scalable AIG package.]

  Synopsis    [Multiplexer profiling algorithm.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

  Date        [Ver. 1.0. Started - June 20, 2005.]

  Revision    [$Id: giaMuxes.c,v 1.00 2005/06/20 00:00:00 alanmi Exp $]

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

#include "gia.h"
22 23
#include "misc/util/utilNam.h"
#include "misc/vec/vecWec.h"
24
#include "misc/vec/vecHsh.h"
Alan Mishchenko committed
25 26 27 28 29 30 31 32 33 34 35 36 37 38

ABC_NAMESPACE_IMPL_START


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

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

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

Alan Mishchenko committed
39
  Synopsis    [Counts XORs and MUXes.]
Alan Mishchenko committed
40 41 42 43 44 45 46 47

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
void Gia_ManCountMuxXor( Gia_Man_t * p, int * pnMuxes, int * pnXors )
{
    Gia_Obj_t * pObj, * pFan0, * pFan1; int i;
    *pnMuxes = *pnXors = 0;
    Gia_ManForEachAnd( p, pObj, i )
    {
        if ( !Gia_ObjIsMuxType(pObj) )
            continue;
        if ( Gia_ObjRecognizeExor(pObj, &pFan0, &pFan1) )
            (*pnXors)++;
        else
            (*pnMuxes)++;
    }
}
void Gia_ManPrintMuxStats( Gia_Man_t * p )
{
    int nAnds, nMuxes, nXors, nTotal;
    if ( p->pMuxes )
    {
67
        nAnds  = Gia_ManAndNotBufNum(p)-Gia_ManXorNum(p)-Gia_ManMuxNum(p);
Alan Mishchenko committed
68 69 70 71 72 73 74
        nXors  = Gia_ManXorNum(p);
        nMuxes = Gia_ManMuxNum(p);
        nTotal = nAnds + 3*nXors + 3*nMuxes;
    }
    else 
    {
        Gia_ManCountMuxXor( p, &nMuxes, &nXors );
75 76
        nAnds  = Gia_ManAndNotBufNum(p) - 3*nMuxes - 3*nXors;
        nTotal = Gia_ManAndNotBufNum(p);
Alan Mishchenko committed
77 78 79 80 81
    }
    Abc_Print( 1, "stats:  " );
    Abc_Print( 1, "xor =%8d %6.2f %%   ", nXors,  300.0*nXors/nTotal );
    Abc_Print( 1, "mux =%8d %6.2f %%   ", nMuxes, 300.0*nMuxes/nTotal );
    Abc_Print( 1, "and =%8d %6.2f %%   ", nAnds,  100.0*nAnds/nTotal );
82
    Abc_Print( 1, "obj =%8d  ", Gia_ManAndNotBufNum(p) );
Alan Mishchenko committed
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
    fflush( stdout );
}

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

  Synopsis    [Derives GIA with MUXes.]

  Description [Create MUX if the sum of fanin references does not exceed limit.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Gia_Man_t * Gia_ManDupMuxes( Gia_Man_t * p, int Limit )
Alan Mishchenko committed
98 99
{
    Gia_Man_t * pNew, * pTemp;
100
    Gia_Obj_t * pObj, * pFan0, * pFan1, * pFanC, * pSiblNew, * pObjNew;
Alan Mishchenko committed
101 102
    int i;
    assert( p->pMuxes == NULL );
Alan Mishchenko committed
103
    assert( Limit >= 2 );
104
    ABC_FREE( p->pRefs );
105
    Gia_ManCreateRefs( p ); 
Alan Mishchenko committed
106
    // start the new manager
Alan Mishchenko committed
107
    pNew = Gia_ManStart( Gia_ManObjNum(p) );
Alan Mishchenko committed
108 109 110
    pNew->pName = Abc_UtilStrsav( p->pName );
    pNew->pSpec = Abc_UtilStrsav( p->pSpec );
    pNew->pMuxes = ABC_CALLOC( unsigned, pNew->nObjsAlloc );
111 112
    if ( Gia_ManHasChoices(p) )
        pNew->pSibls = ABC_CALLOC( int, pNew->nObjsAlloc );
Alan Mishchenko committed
113 114
    Gia_ManConst0(p)->Value = 0;
    Gia_ManHashStart( pNew );
115
    Gia_ManForEachObj1( p, pObj, i )
Alan Mishchenko committed
116
    {
117 118 119 120
        if ( Gia_ObjIsCi(pObj) )
            pObj->Value = Gia_ManAppendCi( pNew );
        else if ( Gia_ObjIsCo(pObj) )
            pObj->Value = Gia_ManAppendCo( pNew, Gia_ObjFanin0Copy(pObj) );
121
        else if ( Gia_ObjIsBuf(pObj) )
122
            pObj->Value = Gia_ManAppendBuf( pNew, Gia_ObjFanin0Copy(pObj) );
123
        else if ( !Gia_ObjIsMuxType(pObj) || Gia_ObjSibl(p, Gia_ObjFaninId0(pObj, i)) || Gia_ObjSibl(p, Gia_ObjFaninId1(pObj, i)) )
Alan Mishchenko committed
124 125 126 127 128 129 130 131 132 133
            pObj->Value = Gia_ManHashAnd( pNew, Gia_ObjFanin0Copy(pObj), Gia_ObjFanin1Copy(pObj) );
        else if ( Gia_ObjRecognizeExor(pObj, &pFan0, &pFan1) )
            pObj->Value = Gia_ManHashXorReal( pNew, Gia_ObjLitCopy(p, Gia_ObjToLit(p, pFan0)), Gia_ObjLitCopy(p, Gia_ObjToLit(p, pFan1)) );
        else if ( Gia_ObjRefNum(p, Gia_ObjFanin0(pObj)) + Gia_ObjRefNum(p, Gia_ObjFanin1(pObj)) > Limit )
            pObj->Value = Gia_ManHashAnd( pNew, Gia_ObjFanin0Copy(pObj), Gia_ObjFanin1Copy(pObj) );
        else
        {
            pFanC = Gia_ObjRecognizeMux( pObj, &pFan1, &pFan0 );
            pObj->Value = Gia_ManHashMuxReal( pNew, Gia_ObjLitCopy(p, Gia_ObjToLit(p, pFanC)), Gia_ObjLitCopy(p, Gia_ObjToLit(p, pFan1)), Gia_ObjLitCopy(p, Gia_ObjToLit(p, pFan0)) );
        }
134 135 136 137 138 139
        if ( !Gia_ObjSibl(p, i) )
            continue;
        pObjNew  = Gia_ManObj( pNew, Abc_Lit2Var(pObj->Value) );
        pSiblNew = Gia_ManObj( pNew, Abc_Lit2Var(Gia_ObjSiblObj(p, i)->Value) );
        if ( Gia_ObjIsAnd(pObjNew) && Gia_ObjIsAnd(pSiblNew) && Gia_ObjId(pNew, pObjNew) > Gia_ObjId(pNew, pSiblNew) )
            pNew->pSibls[Gia_ObjId(pNew, pObjNew)] = Gia_ObjId(pNew, pSiblNew);
Alan Mishchenko committed
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
    }
    Gia_ManHashStop( pNew );
    Gia_ManSetRegNum( pNew, Gia_ManRegNum(p) );
    // perform cleanup
    pNew = Gia_ManCleanup( pTemp = pNew );
    Gia_ManStop( pTemp );
    return pNew;
}

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

  Synopsis    [Derives GIA without MUXes.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Gia_Man_t * Gia_ManDupNoMuxes( Gia_Man_t * p )
{
    Gia_Man_t * pNew, * pTemp;
    Gia_Obj_t * pObj;
    int i;
    assert( p->pMuxes != NULL );
    // start the new manager
    pNew = Gia_ManStart( 5000 );
    pNew->pName = Abc_UtilStrsav( p->pName );
    pNew->pSpec = Abc_UtilStrsav( p->pSpec );
    Gia_ManConst0(p)->Value = 0;
    Gia_ManHashStart( pNew );
172
    Gia_ManForEachObj1( p, pObj, i )
Alan Mishchenko committed
173
    {
174 175 176 177
        if ( Gia_ObjIsCi(pObj) )
            pObj->Value = Gia_ManAppendCi( pNew );
        else if ( Gia_ObjIsCo(pObj) )
            pObj->Value = Gia_ManAppendCo( pNew, Gia_ObjFanin0Copy(pObj) );
178
        else if ( Gia_ObjIsBuf(pObj) )
179
            pObj->Value = Gia_ManAppendBuf( pNew, Gia_ObjFanin0Copy(pObj) );
180
        else if ( Gia_ObjIsMuxId(p, i) )
Alan Mishchenko committed
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
            pObj->Value = Gia_ManHashMux( pNew, Gia_ObjFanin2Copy(p, pObj), Gia_ObjFanin1Copy(pObj), Gia_ObjFanin0Copy(pObj) );
        else if ( Gia_ObjIsXor(pObj) )
            pObj->Value = Gia_ManHashXor( pNew, Gia_ObjFanin0Copy(pObj), Gia_ObjFanin1Copy(pObj) );
        else 
            pObj->Value = Gia_ManHashAnd( pNew, Gia_ObjFanin0Copy(pObj), Gia_ObjFanin1Copy(pObj) );
    }
    Gia_ManHashStop( pNew );
    Gia_ManSetRegNum( pNew, Gia_ManRegNum(p) );
    // perform cleanup
    pNew = Gia_ManCleanup( pTemp = pNew );
    Gia_ManStop( pTemp );
    return pNew;
}

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

  Synopsis    [Test these procedures.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Gia_Man_t * Gia_ManDupMuxesTest( Gia_Man_t * p )
{
    Gia_Man_t * pNew, * pNew2;
Alan Mishchenko committed
209
    pNew = Gia_ManDupMuxes( p, 2 );
Alan Mishchenko committed
210
    pNew2 = Gia_ManDupNoMuxes( pNew );
211 212 213
    Gia_ManPrintStats( p, NULL );
    Gia_ManPrintStats( pNew, NULL );
    Gia_ManPrintStats( pNew2, NULL );
Alan Mishchenko committed
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
    Gia_ManStop( pNew );
//    Gia_ManStop( pNew2 );
    return pNew2;
}


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

  Synopsis    [Returns the size of MUX structure.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Gia_MuxRef_rec( Gia_Man_t * p, int iObj )
{
    Gia_Obj_t * pObj;
234
    if ( !Gia_ObjIsMuxId(p, iObj) )
Alan Mishchenko committed
235 236 237 238 239 240 241 242 243 244 245
        return 0;
    pObj = Gia_ManObj( p, iObj );
    if ( Gia_ObjRefInc(p, pObj) )
        return 0;
    return Gia_MuxRef_rec( p, Gia_ObjFaninId0p(p, pObj) ) + 
           Gia_MuxRef_rec( p, Gia_ObjFaninId1p(p, pObj) ) + 
           Gia_MuxRef_rec( p, Gia_ObjFaninId2p(p, pObj) ) + 1;
}
int Gia_MuxRef( Gia_Man_t * p, int iObj )
{
    Gia_Obj_t * pObj = Gia_ManObj( p, iObj );
246
    assert( Gia_ObjIsMuxId(p, iObj) );
Alan Mishchenko committed
247 248 249 250 251 252 253
    return Gia_MuxRef_rec( p, Gia_ObjFaninId0p(p, pObj) ) + 
           Gia_MuxRef_rec( p, Gia_ObjFaninId1p(p, pObj) ) + 
           Gia_MuxRef_rec( p, Gia_ObjFaninId2p(p, pObj) ) + 1;
}
int Gia_MuxDeref_rec( Gia_Man_t * p, int iObj )
{
    Gia_Obj_t * pObj;
254
    if ( !Gia_ObjIsMuxId(p, iObj) )
Alan Mishchenko committed
255 256 257 258 259 260 261 262 263 264 265
        return 0;
    pObj = Gia_ManObj( p, iObj );
    if ( Gia_ObjRefDec(p, pObj) )
        return 0;
    return Gia_MuxDeref_rec( p, Gia_ObjFaninId0p(p, pObj) ) + 
           Gia_MuxDeref_rec( p, Gia_ObjFaninId1p(p, pObj) ) + 
           Gia_MuxDeref_rec( p, Gia_ObjFaninId2p(p, pObj) ) + 1;
}
int Gia_MuxDeref( Gia_Man_t * p, int iObj )
{
    Gia_Obj_t * pObj = Gia_ManObj( p, iObj );
266
    assert( Gia_ObjIsMuxId(p, iObj) );
Alan Mishchenko committed
267 268 269 270 271 272 273
    return Gia_MuxDeref_rec( p, Gia_ObjFaninId0p(p, pObj) ) + 
           Gia_MuxDeref_rec( p, Gia_ObjFaninId1p(p, pObj) ) + 
           Gia_MuxDeref_rec( p, Gia_ObjFaninId2p(p, pObj) ) + 1;
}
int Gia_MuxMffcSize( Gia_Man_t * p, int iObj )
{
    int Count1, Count2;
274
    if ( !Gia_ObjIsMuxId(p, iObj) )
Alan Mishchenko committed
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
        return 0;
    Count1 = Gia_MuxDeref( p, iObj );
    Count2 = Gia_MuxRef( p, iObj );
    assert( Count1 == Count2 );
    return Count1;
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Gia_MuxStructPrint_rec( Gia_Man_t * p, int iObj, int fFirst )
{
    Gia_Obj_t * pObj = Gia_ManObj( p, iObj );
296
    int iCtrl;
297
    if ( !fFirst && (!Gia_ObjIsMuxId(p, iObj) || Gia_ObjRefNumId(p, iObj) > 0) )
298 299 300
    {
//        printf( "%d", iObj );
        printf( "<%02d>", Gia_ObjLevelId(p, iObj) );
Alan Mishchenko committed
301
        return;
302
    }
303 304 305 306
    iCtrl = Gia_ObjFaninId2p(p, pObj);
    printf( " [(" );
    if ( Gia_ObjIsMuxId(p, iCtrl) && Gia_ObjRefNumId(p, iCtrl) == 0 )
        Gia_MuxStructPrint_rec( p, iCtrl, 0 );
Alan Mishchenko committed
307
    else
308
    {
309
        printf( "%d", iCtrl );
310 311
        printf( "<%d>", Gia_ObjLevelId(p, iCtrl) );
    }
Alan Mishchenko committed
312
    printf( ")" );
313 314 315 316 317
    if ( Gia_ObjFaninC2(p, pObj) )
    {
        Gia_MuxStructPrint_rec( p, Gia_ObjFaninId0p(p, pObj), 0 );
        printf( "|" );
        Gia_MuxStructPrint_rec( p, Gia_ObjFaninId1p(p, pObj), 0 );
318
        printf( "]" );
319 320 321 322 323 324
    }
    else
    {
        Gia_MuxStructPrint_rec( p, Gia_ObjFaninId1p(p, pObj), 0 );
        printf( "|" );
        Gia_MuxStructPrint_rec( p, Gia_ObjFaninId0p(p, pObj), 0 );
325
        printf( "]" );
326
    }
Alan Mishchenko committed
327 328 329 330
}
void Gia_MuxStructPrint( Gia_Man_t * p, int iObj )
{
    int Count1, Count2;
331
    assert( Gia_ObjIsMuxId(p, iObj) );
Alan Mishchenko committed
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
    Count1 = Gia_MuxDeref( p, iObj );
    Gia_MuxStructPrint_rec( p, iObj, 1 );
    Count2 = Gia_MuxRef( p, iObj );
    assert( Count1 == Count2 );
    printf( "\n" );
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
void Gia_MuxStructDump_rec( Gia_Man_t * p, int iObj, int fFirst, Vec_Str_t * vStr, int nDigitsId )
{
    Gia_Obj_t * pObj = Gia_ManObj( p, iObj );
    int iCtrl;
    if ( !fFirst && (!Gia_ObjIsMuxId(p, iObj) || Gia_ObjRefNumId(p, iObj) > 0) )
        return;
    iCtrl = Gia_ObjFaninId2p(p, pObj);
    Vec_StrPush( vStr, '[' );
    Vec_StrPush( vStr, '(' );
    if ( Gia_ObjIsMuxId(p, iCtrl) && Gia_ObjRefNumId(p, iCtrl) == 0 )
        Gia_MuxStructDump_rec( p, iCtrl, 0, vStr, nDigitsId );
    else
        Vec_StrPrintNumStar( vStr, iCtrl, nDigitsId );
    Vec_StrPush( vStr, ')' );
    if ( Gia_ObjFaninC2(p, pObj) )
    {
        Gia_MuxStructDump_rec( p, Gia_ObjFaninId0p(p, pObj), 0, vStr, nDigitsId );
        Vec_StrPush( vStr, '|' );
        Gia_MuxStructDump_rec( p, Gia_ObjFaninId1p(p, pObj), 0, vStr, nDigitsId );
        Vec_StrPush( vStr, ']' );
    }
    else
    {
        Gia_MuxStructDump_rec( p, Gia_ObjFaninId1p(p, pObj), 0, vStr, nDigitsId );
        Vec_StrPush( vStr, '|' );
        Gia_MuxStructDump_rec( p, Gia_ObjFaninId0p(p, pObj), 0, vStr, nDigitsId );
        Vec_StrPush( vStr, ']' );
    }
}
379
int Gia_MuxStructDump( Gia_Man_t * p, int iObj, Vec_Str_t * vStr, int nDigitsNum, int nDigitsId )
380 381 382 383 384
{
    int Count1, Count2;
    assert( Gia_ObjIsMuxId(p, iObj) );
    Count1 = Gia_MuxDeref( p, iObj );
    Vec_StrClear( vStr );
385
    Vec_StrPrintNumStar( vStr, Count1, nDigitsNum );
386 387 388 389
    Gia_MuxStructDump_rec( p, iObj, 1, vStr, nDigitsId );
    Vec_StrPush( vStr, '\0' );
    Count2 = Gia_MuxRef( p, iObj );
    assert( Count1 == Count2 );
390
    return Count1;
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
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Gia_ManMuxCompare( char ** pp1, char ** pp2 )
{
    int Diff = strcmp( *pp1, *pp2 );
    if ( Diff < 0 )
        return 1;
    if ( Diff > 0) 
        return -1;
    return 0; 
}
int Gia_ManMuxCountOne( char * p )
{
    int Count = 0;
    for ( ; *p; p++ )
        Count += (*p == '[');
    return Count;
}
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

typedef struct Mux_Man_t_ Mux_Man_t; 
struct Mux_Man_t_
{
    Gia_Man_t *     pGia;      // manager
    Abc_Nam_t *     pNames;    // hashing name into ID
    Vec_Wec_t *     vTops;     // top nodes for each struct
};

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Mux_Man_t * Mux_ManAlloc( Gia_Man_t * pGia )
{
    Mux_Man_t * p;
    p = ABC_CALLOC( Mux_Man_t, 1 );
    p->pGia   = pGia;
    p->pNames = Abc_NamStart( 10000, 50 );
    p->vTops  = Vec_WecAlloc( 1000 );
    Vec_WecPushLevel( p->vTops );
    return p;
}
void Mux_ManFree( Mux_Man_t * p )
451
{
452 453 454
    Abc_NamStop( p->pNames );
    Vec_WecFree( p->vTops );
    ABC_FREE( p );
455 456
}

457 458 459 460 461 462 463 464 465 466 467
/**Function*************************************************************

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
468
int Gia_ManMuxProfile( Mux_Man_t * p, int fWidth )
469
{
Alan Mishchenko committed
470
    int i, Entry, Counter, Total;
471 472 473 474 475 476 477 478 479 480 481 482
    Vec_Int_t * vVec, * vCounts;
    vCounts = Vec_IntStart( 1000 );
    if ( fWidth )
    {
        Vec_WecForEachLevelStart( p->vTops, vVec, i, 1 )
            Vec_IntAddToEntry( vCounts, Abc_MinInt(Vec_IntSize(vVec), 999), 1 );
    }
    else
    {
        for ( i = 1; i < Vec_WecSize(p->vTops); i++ )
            Vec_IntAddToEntry( vCounts, Abc_MinInt(atoi(Abc_NamStr(p->pNames, i)), 999), 1 );
    }
Alan Mishchenko committed
483 484 485 486
    Total = Vec_IntCountPositive(vCounts);
    if ( Total == 0 )
        return 0;
    printf( "The distribution of MUX tree %s:\n", fWidth ? "widths" : "sizes"  );
487 488 489 490 491 492 493 494 495 496
    Counter = 0;
    Vec_IntForEachEntry( vCounts, Entry, i )
    {
        if ( !Entry ) continue;
        if ( ++Counter == 12 )
            printf( "\n" ), Counter = 0;
        printf( "  %d=%d", i, Entry );
    }
    printf( "\nSummary: " );
    printf( "Max = %d  ", Vec_IntFindMax(vCounts) );
Alan Mishchenko committed
497
    printf( "Ave = %.2f", 1.0*Vec_IntSum(vCounts)/Total );
498 499
    printf( "\n" );
    Vec_IntFree( vCounts );
Alan Mishchenko committed
500
    return 1;
501 502 503 504 505 506 507 508 509 510 511 512 513
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
514 515
void Gia_ManMuxProfiling( Gia_Man_t * p )
{
516
    Mux_Man_t * pMan;
Alan Mishchenko committed
517 518
    Gia_Man_t * pNew;
    Gia_Obj_t * pObj;
519 520 521
    Vec_Str_t * vStr;
    Vec_Int_t * vFans, * vVec;
    int i, Counter, fFound, iStructId, nDigitsId;
522
    abctime clk = Abc_Clock();
Alan Mishchenko committed
523

Alan Mishchenko committed
524
    pNew = Gia_ManDupMuxes( p, 2 );
525 526
    nDigitsId = Abc_Base10Log( Gia_ManObjNum(pNew) );

527 528 529
    pMan = Mux_ManAlloc( pNew );
     
    Gia_ManLevelNum( pNew );
Alan Mishchenko committed
530 531 532 533
    Gia_ManCreateRefs( pNew );
    Gia_ManForEachCo( pNew, pObj, i )
        Gia_ObjRefFanin0Inc( pNew, pObj );

534
    vStr = Vec_StrAlloc( 1000 );
Alan Mishchenko committed
535 536 537
    vFans = Gia_ManFirstFanouts( pNew );
    Gia_ManForEachMux( pNew, pObj, i )
    {
538 539 540 541
        // skip MUXes in the middle of the tree (which have only one MUX fanout)
        if ( Gia_ObjRefNumId(pNew, i) == 1 && Gia_ObjIsMuxId(pNew, Vec_IntEntry(vFans, i)) )
            continue;
        // this node is the root of the MUX structure - create hash key
542 543 544
        Counter = Gia_MuxStructDump( pNew, i, vStr, 3, nDigitsId );
        if ( Counter == 1 )
            continue;
545 546 547 548 549
        iStructId = Abc_NamStrFindOrAdd( pMan->pNames, Vec_StrArray(vStr), &fFound );
        if ( !fFound )
            Vec_WecPushLevel( pMan->vTops );
        assert( Abc_NamObjNumMax(pMan->pNames) == Vec_WecSize(pMan->vTops) );
        Vec_IntPush( Vec_WecEntry(pMan->vTops, iStructId), i );
Alan Mishchenko committed
550
    }
551 552
    Vec_StrFree( vStr );
    Vec_IntFree( vFans );
553

554 555
    printf( "MUX structure profile for AIG \"%s\":\n", p->pName );
    printf( "Total MUXes = %d.  Total trees = %d.  Unique trees = %d.  Memory = %.2f MB   ", 
556
        Gia_ManMuxNum(pNew), Vec_WecSizeSize(pMan->vTops), Vec_WecSize(pMan->vTops)-1, 
557 558
        1.0*Abc_NamMemUsed(pMan->pNames)/(1<<20) );
    Abc_PrintTime( 1, "Time", Abc_Clock() - clk );
Alan Mishchenko committed
559

Alan Mishchenko committed
560
    if ( Gia_ManMuxProfile(pMan, 0) )
561
    {
Alan Mishchenko committed
562
        Gia_ManMuxProfile( pMan, 1 );
563

Alan Mishchenko committed
564 565 566 567 568 569 570 571 572 573 574 575 576 577
        // short the first ones
        printf( "The first %d structures: \n", 10 );
        Vec_WecForEachLevelStartStop( pMan->vTops, vVec, i, 1, Abc_MinInt(Vec_WecSize(pMan->vTops), 10) )
        {
            char * pTemp = Abc_NamStr(pMan->pNames, i);
            printf( "%5d : ", i );
            printf( "Occur = %4d   ", Vec_IntSize(vVec) );
            printf( "Size = %4d   ", atoi(pTemp) );
            printf( "%s\n", pTemp );
        }

        // print trees for the first one
        Counter = 0;
        Vec_WecForEachLevelStart( pMan->vTops, vVec, i, 1 )
578
        {
Alan Mishchenko committed
579 580 581 582 583 584 585 586 587 588
            char * pTemp = Abc_NamStr(pMan->pNames, i);
            if ( Vec_IntSize(vVec) > 5 && atoi(pTemp) > 5 )
            {
                int k, Entry;
                printf( "For example, structure %d has %d MUXes and bit-width %d:\n", i, atoi(pTemp), Vec_IntSize(vVec) );
                Vec_IntForEachEntry( vVec, Entry, k )
                    Gia_MuxStructPrint( pNew, Entry );
                if ( ++Counter == 5 )
                    break;
            }
589 590
        }
    }
591

592
    Mux_ManFree( pMan );
Alan Mishchenko committed
593 594 595 596
    Gia_ManStop( pNew );
}


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
/**Function*************************************************************

  Synopsis    [Compute one-level TFI/TFO structural signatures.]

  Description []
               
  SideEffects []

  SeeAlso     []

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

// these are object/fanin/fanout attributes
// http://stackoverflow.com/questions/9907160/how-to-convert-enum-names-to-string-in-c
#define GIA_FOREACH_ITEM(ITEM) \
    ITEM(C0)    \
    ITEM(PO)    \
    ITEM(PI)    \
    ITEM(FF)    \
    ITEM(XOR)   \
    ITEM(MUX)   \
    ITEM(AND)   \
    ITEM(iC0)   \
    ITEM(iC1)   \
    ITEM(iPI)   \
    ITEM(iFF)   \
    ITEM(iXOR)  \
    ITEM(iMUX)  \
    ITEM(iAND)  \
    ITEM(iANDn) \
    ITEM(iANDp) \
    ITEM(oPO)   \
    ITEM(oFF)   \
    ITEM(oXOR)  \
    ITEM(oMUXc) \
    ITEM(oMUXd) \
633
    ITEM(oAND)  \
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 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687
    ITEM(oANDn) \
    ITEM(oANDp) \
    ITEM(GIA_END)

#define GENERATE_ENUM(ENUM) ENUM,
typedef enum { GIA_FOREACH_ITEM(GENERATE_ENUM) } Gia_ObjType_t;

#define GENERATE_STRING(STRING) #STRING,
static const char * GIA_TYPE_STRINGS[] = { GIA_FOREACH_ITEM(GENERATE_STRING) };

void Gia_ManProfileStructuresTest( Gia_Man_t * p )
{
    int i;
    for ( i = 0; i < GIA_END; i++ )
        printf( "%d = %s\n", i, GIA_TYPE_STRINGS[i] );
}



// find object code
int Gia_ManEncodeObj( Gia_Man_t * p, int i )
{
    Gia_Obj_t * pObj = Gia_ManObj( p, i );
    assert( !Gia_ObjIsRi(p, pObj) );
    if ( Gia_ObjIsConst0(pObj) )
        return C0;
    if ( Gia_ObjIsPo(p, pObj) )
        return PO;
    if ( Gia_ObjIsPi(p, pObj) )
        return PI;
    if ( Gia_ObjIsCi(pObj) )
        return FF;
    if ( Gia_ObjIsXor(pObj) )
        return XOR;
    if ( Gia_ObjIsMux(p, pObj) )
        return MUX;
    assert( Gia_ObjIsAnd(pObj) );
    return AND;
}
// find fanin code
int Gia_ManEncodeFanin( Gia_Man_t * p, int iLit )
{
    Gia_Obj_t * pObj = Gia_ManObj( p, Abc_Lit2Var(iLit) );
    if ( Gia_ObjIsConst0(pObj) )
        return iC0;
    if ( Gia_ObjIsPi(p, pObj) )
        return iPI;
    if ( Gia_ObjIsCi(pObj) )
        return iFF;
    if ( Gia_ObjIsXor(pObj) )
        return iXOR;
    if ( Gia_ObjIsMux(p, pObj) )
        return iMUX;
    assert( Gia_ObjIsAnd(pObj) );
688 689 690 691 692
    return iAND;
//    if ( Abc_LitIsCompl(iLit) )
//        return iANDn;
//    else
//        return iANDp;
693 694 695 696
}
// find fanout code
int Gia_ManEncodeFanout( Gia_Man_t * p, Gia_Obj_t * pObj, int i )
{
697
//    int iLit;
698 699 700 701 702 703 704 705 706
    if ( Gia_ObjIsPo(p, pObj) )
        return oPO;
    if ( Gia_ObjIsCo(pObj) )
        return oFF;
    if ( Gia_ObjIsXor(pObj) )
        return oXOR;
    if ( Gia_ObjIsMux(p, pObj) )
        return i == 2 ? oMUXc : oMUXd;
    assert( Gia_ObjIsAnd(pObj) );
707 708 709 710 711 712
    return oAND;
//    iLit = i ? Gia_ObjFaninLit1p(p, pObj) : Gia_ObjFaninLit0p(p, pObj);
//    if ( Abc_LitIsCompl(iLit) )
//        return oANDn;
//    else
//        return oANDp;
713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728
}

void Gia_ManProfileCollect( Gia_Man_t * p, int i, Vec_Int_t * vCode, Vec_Int_t * vCodeOffsets, Vec_Int_t * vArray )
{
    int k;
    Vec_IntClear( vArray );
    for ( k = Vec_IntEntry(vCodeOffsets, i); k < Vec_IntEntry(vCodeOffsets, i+1); k++ )
        Vec_IntPush( vArray, Vec_IntEntry(vCode, k) );
}

void Gia_ManProfilePrintOne( Gia_Man_t * p, int i, Vec_Int_t * vArray )
{
    Gia_Obj_t * pObj = Gia_ManObj( p, i );
    int k, nFanins, nFanouts;
    if ( Gia_ObjIsRi(p, pObj) )
        return;
729
    nFanins = Gia_ObjIsRo(p, pObj) ? 1 : Gia_ObjFaninNum(p, pObj);
730 731 732 733 734 735 736 737 738 739
    nFanouts = Gia_ObjFanoutNum(p, pObj);

    printf( "%6d : ", i );
    for ( k = 0; k < nFanins; k++ )
        printf( "  %5s", GIA_TYPE_STRINGS[Vec_IntEntry(vArray, k + 1)] );
    for (      ; k < 3; k++ )
        printf( "  %5s", "" );
    printf( "  ->" );
    printf( " %5s", GIA_TYPE_STRINGS[Vec_IntEntry(vArray, 0)] );
    printf( "  ->" );
740 741 742 743 744 745 746 747 748 749 750 751 752 753 754
    if ( nFanouts > 0 )
    {
        int Count = 1, Prev = Vec_IntEntry(vArray, 1 + nFanins);
        for ( k = 1; k < nFanouts; k++ )
        {
            if ( Prev != Vec_IntEntry(vArray, k + 1 + nFanins) )
            {
                printf( "  %d x %s", Count, GIA_TYPE_STRINGS[Prev] );
                Prev = Vec_IntEntry(vArray, k + 1 + nFanins);
                Count = 0;
            }
            Count++;
        }
        printf( "  %d x %s", Count, GIA_TYPE_STRINGS[Prev] );
    }
755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803
    printf( "\n" );
}

Vec_Int_t * Gia_ManProfileHash( Gia_Man_t * p, Vec_Int_t * vCode, Vec_Int_t * vCodeOffsets )
{
    Hsh_VecMan_t * pHash;
    Vec_Int_t * vRes, * vArray;
    Gia_Obj_t * pObj;
    int i;
    vRes = Vec_IntAlloc( Gia_ManObjNum(p) );
    pHash = Hsh_VecManStart( Gia_ManObjNum(p) );
    // add empty entry
    vArray = Vec_IntAlloc( 100 );
    Hsh_VecManAdd( pHash, vArray );
    // iterate through the entries
    Gia_ManForEachObj( p, pObj, i )
    {
        Gia_ManProfileCollect( p, i, vCode, vCodeOffsets, vArray );
        Vec_IntPush( vRes, Hsh_VecManAdd( pHash, vArray ) );
    }
    Hsh_VecManStop( pHash );
    Vec_IntFree( vArray );
    assert( Vec_IntSize(vRes) == Gia_ManObjNum(p) );
    return vRes;
}


void Gia_ManProfileStructuresInt( Gia_Man_t * p, int nLimit, int fVerbose )
{
    Vec_Int_t * vRes, * vCount, * vFirst;
    Vec_Int_t * vCode, * vCodeOffsets, * vArray;
    Gia_Obj_t * pObj, * pFanout;
    int i, k, nFanins, nFanouts, * pPerm, nClasses;
    assert( p->pMuxes );
    Gia_ManStaticFanoutStart( p );
    // create fanout codes
    vArray = Vec_IntAlloc( 100 );
    vCode = Vec_IntAlloc( 5 * Gia_ManObjNum(p) );
    vCodeOffsets = Vec_IntAlloc( Gia_ManObjNum(p) );
    Gia_ManForEachObj( p, pObj, i )
    {
        Vec_IntPush( vCodeOffsets, Vec_IntSize(vCode) );
        if ( Gia_ObjIsRi(p, pObj) )
            continue;
        nFanins = Gia_ObjFaninNum(p, pObj);
        nFanouts = Gia_ObjFanoutNum(p, pObj);
        Vec_IntPush( vCode, Gia_ManEncodeObj(p, i) );
        if ( nFanins == 3 )
        {
804 805 806 807 808 809 810 811 812 813 814 815
            int iLit = Gia_ObjFaninLit2p(p, pObj);
            Vec_IntPush( vCode, Gia_ManEncodeFanin(p, Abc_LitRegular(iLit)) );
            if ( Abc_LitIsCompl(iLit) )
            {
                Vec_IntPush( vCode, Gia_ManEncodeFanin(p, Gia_ObjFaninLit0p(p, pObj)) );
                Vec_IntPush( vCode, Gia_ManEncodeFanin(p, Gia_ObjFaninLit1p(p, pObj)) );
            }
            else
            {
                Vec_IntPush( vCode, Gia_ManEncodeFanin(p, Gia_ObjFaninLit1p(p, pObj)) );
                Vec_IntPush( vCode, Gia_ManEncodeFanin(p, Gia_ObjFaninLit0p(p, pObj)) );
            }
816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908
        }
        else if ( nFanins == 2 )
        {
            int Code0 = Gia_ManEncodeFanin(p, Gia_ObjFaninLit0p(p, pObj));
            int Code1 = Gia_ManEncodeFanin(p, Gia_ObjFaninLit1p(p, pObj));
            Vec_IntPush( vCode, Code0 < Code1 ? Code0 : Code1 );
            Vec_IntPush( vCode, Code0 < Code1 ? Code1 : Code0 );
        }
        else if ( nFanins == 1 )
            Vec_IntPush( vCode, Gia_ManEncodeFanin(p, Gia_ObjFaninLit0p(p, pObj)) );
        else if ( Gia_ObjIsRo(p, pObj) )
            Vec_IntPush( vCode, Gia_ManEncodeFanin(p, Gia_ObjFaninLit0p(p, Gia_ObjRoToRi(p, pObj))) );

        // add fanouts
        Vec_IntClear( vArray );
        Gia_ObjForEachFanoutStatic( p, pObj, pFanout, k )
        {
            int Index = Gia_ObjWhatFanin( p, pFanout, pObj );
            Gia_ObjType_t Type = Gia_ManEncodeFanout( p, pFanout, Index );
            Vec_IntPush( vArray, Type );
        }
        Vec_IntSort( vArray, 0 );
        Vec_IntAppend( vCode, vArray );
    }
    assert( Vec_IntSize(vCodeOffsets) == Gia_ManObjNum(p) );
    Vec_IntPush( vCodeOffsets, Vec_IntSize(vCode) );
    // print the results
    if ( fVerbose )
    {
        printf( "Showing TFI/node/TFO structures for all nodes:\n" );
        Gia_ManForEachObj( p, pObj, i )
        {
            Gia_ManProfileCollect( p, i, vCode, vCodeOffsets, vArray );
            Gia_ManProfilePrintOne( p, i, vArray );
        }
    }

    // collect statistics
    vRes = Gia_ManProfileHash( p, vCode, vCodeOffsets );
    //Vec_IntPrint( vRes );

    // count how many times each class appears
    nClasses = Vec_IntFindMax(vRes) + 1;
    vCount = Vec_IntStart( nClasses );
    vFirst = Vec_IntStart( nClasses );
    Gia_ManForEachObj( p, pObj, i )
    {
        int Entry = Vec_IntEntry( vRes, i );
        if ( Gia_ObjIsRi(p, pObj) )
            continue;
        if ( Vec_IntEntry(vCount, Entry) == 0 )
            Vec_IntWriteEntry( vFirst, Entry, i );
        Vec_IntAddToEntry( vCount, Entry, -1 );
    }
    // sort the counts
    pPerm = Abc_MergeSortCost( Vec_IntArray(vCount), Vec_IntSize(vCount) );
    printf( "Showing TFI/node/TFO structures that appear more than %d times.\n", nLimit );
    for ( i = 0; i < nClasses-1; i++ )
    {
        if ( nLimit > -Vec_IntEntry(vCount, pPerm[i]) )
            break;
        printf( "%6d : ", i );
        printf( "%6d : ", pPerm[i] );
        printf( "Weight =%6d  ",   -Vec_IntEntry(vCount, pPerm[i]) );
        printf( "First obj =" );
        // print the object
        Gia_ManProfileCollect( p, Vec_IntEntry(vFirst, pPerm[i]), vCode, vCodeOffsets, vArray );
        Gia_ManProfilePrintOne( p, Vec_IntEntry(vFirst, pPerm[i]), vArray );
    }

    // cleanup
    ABC_FREE( pPerm );
    Vec_IntFree( vRes );
    Vec_IntFree( vCount );
    Vec_IntFree( vFirst );

    Vec_IntFree( vArray );
    Vec_IntFree( vCode );
    Vec_IntFree( vCodeOffsets );
    Gia_ManStaticFanoutStop( p );
}
void Gia_ManProfileStructures( Gia_Man_t * p, int nLimit, int fVerbose )
{
    if ( p->pMuxes )
        Gia_ManProfileStructuresInt( p, nLimit, fVerbose );
    else
    {
        Gia_Man_t * pNew = Gia_ManDupMuxes( p, 2 );
        Gia_ManProfileStructuresInt( pNew, nLimit, fVerbose );
        Gia_ManStop( pNew );
    }
}

Alan Mishchenko committed
909 910 911 912 913 914 915
////////////////////////////////////////////////////////////////////////
///                       END OF FILE                                ///
////////////////////////////////////////////////////////////////////////


ABC_NAMESPACE_IMPL_END