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

  FileName    [giaTsim.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [Scalable AIG package.]

  Synopsis    [Ternary simulation.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

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

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

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

#include "gia.h"

23 24 25
ABC_NAMESPACE_IMPL_START


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

static inline int Gia_ManTerSimInfoGet( unsigned * pInfo, int i )
{
    return 3 & (pInfo[i >> 4] >> ((i & 15) << 1));
}
static inline void Gia_ManTerSimInfoSet( unsigned * pInfo, int i, int Value )
{
    assert( Value >= GIA_ZER && Value <= GIA_UND );
    Value ^= Gia_ManTerSimInfoGet( pInfo, i );
    pInfo[i >> 4] ^= (Value << ((i & 15) << 1));
}

static inline unsigned * Gia_ManTerStateNext( unsigned * pState, int nWords )                      { return *((unsigned **)(pState + nWords));  }
static inline void       Gia_ManTerStateSetNext( unsigned * pState, int nWords, unsigned * pNext ) { *((unsigned **)(pState + nWords)) = pNext; }

// ternary simulation manager
typedef struct Gia_ManTer_t_ Gia_ManTer_t;
struct Gia_ManTer_t_
{
    Gia_Man_t *    pAig;
    int            nIters;
    int            nStateWords;
    Vec_Ptr_t *    vStates;
    Vec_Ptr_t *    vFlops;
Alan Mishchenko committed
53 54
    Vec_Int_t *    vRetired;     // retired registers
    char *         pRetired;     // retired registers
Alan Mishchenko committed
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
    int *          pCount0;
    int *          pCountX;
    // hash table for states
    int            nBins;
    unsigned **    pBins;
    // simulation information
    unsigned *     pDataSim;     // simulation data
    unsigned *     pDataSimCis;  // simulation data for CIs
    unsigned *     pDataSimCos;  // simulation data for COs
};

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

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

  Synopsis    [Creates fast simulation manager.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Gia_ManTer_t * Gia_ManTerCreate( Gia_Man_t * pAig )
{
    Gia_ManTer_t * p;
    p = ABC_CALLOC( Gia_ManTer_t, 1 );
    p->pAig   = Gia_ManFront( pAig );
    p->nIters = 300;
87 88 89
    p->pDataSim    = ABC_ALLOC( unsigned, Abc_BitWordNum(2*p->pAig->nFront) );
    p->pDataSimCis = ABC_ALLOC( unsigned, Abc_BitWordNum(2*Gia_ManCiNum(p->pAig)) );
    p->pDataSimCos = ABC_ALLOC( unsigned, Abc_BitWordNum(2*Gia_ManCoNum(p->pAig)) );
Alan Mishchenko committed
90
    // allocate storage for terminary states
91
    p->nStateWords = Abc_BitWordNum( 2*Gia_ManRegNum(pAig) );
Alan Mishchenko committed
92 93 94
    p->vStates  = Vec_PtrAlloc( 1000 );
    p->pCount0  = ABC_CALLOC( int, Gia_ManRegNum(pAig) );
    p->pCountX  = ABC_CALLOC( int, Gia_ManRegNum(pAig) );
95
    p->nBins    = Abc_PrimeCudd( 500 );
Alan Mishchenko committed
96 97 98
    p->pBins    = ABC_CALLOC( unsigned *, p->nBins );
    p->vRetired = Vec_IntAlloc( 100 );
    p->pRetired = ABC_CALLOC( char, Gia_ManRegNum(pAig) );
Alan Mishchenko committed
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
    return p;
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Gia_ManTerStatesFree( Vec_Ptr_t * vStates )
{
    unsigned * pTemp;
    int i;
117
    Vec_PtrForEachEntry( unsigned *, vStates, pTemp, i )
Alan Mishchenko committed
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
        ABC_FREE( pTemp );
    Vec_PtrFree( vStates );
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Gia_ManTerDelete( Gia_ManTer_t * p )
{
    if ( p->vStates ) 
        Gia_ManTerStatesFree( p->vStates );
    if ( p->vFlops ) 
        Gia_ManTerStatesFree( p->vFlops );
    Gia_ManStop( p->pAig );
Alan Mishchenko committed
140 141
    Vec_IntFree( p->vRetired );
    ABC_FREE( p->pRetired );
Alan Mishchenko committed
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 172 173 174 175 176 177 178 179 180 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 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 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 345 346 347 348 349 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 379
    ABC_FREE( p->pCount0 );
    ABC_FREE( p->pCountX );
    ABC_FREE( p->pBins );
    ABC_FREE( p->pDataSim );
    ABC_FREE( p->pDataSimCis );
    ABC_FREE( p->pDataSimCos );
    ABC_FREE( p );
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline void Gia_ManTerSimulateCi( Gia_ManTer_t * p, Gia_Obj_t * pObj, int iCi )
{
    Gia_ManTerSimInfoSet( p->pDataSim, Gia_ObjValue(pObj), Gia_ManTerSimInfoGet(p->pDataSimCis, iCi) );
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline void Gia_ManTerSimulateCo( Gia_ManTer_t * p, int iCo, Gia_Obj_t * pObj )
{
    int Value = Gia_ManTerSimInfoGet( p->pDataSim, Gia_ObjDiff0(pObj) );
    Gia_ManTerSimInfoSet( p->pDataSimCos, iCo, Gia_XsimNotCond( Value, Gia_ObjFaninC0(pObj) ) );
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline void Gia_ManTerSimulateNode( Gia_ManTer_t * p, Gia_Obj_t * pObj )
{
    int Value0 = Gia_ManTerSimInfoGet( p->pDataSim, Gia_ObjDiff0(pObj) );
    int Value1 = Gia_ManTerSimInfoGet( p->pDataSim, Gia_ObjDiff1(pObj) );
    Gia_ManTerSimInfoSet( p->pDataSim, Gia_ObjValue(pObj), Gia_XsimAndCond( Value0, Gia_ObjFaninC0(pObj), Value1, Gia_ObjFaninC1(pObj) ) );
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline void Gia_ManTerSimInfoInit( Gia_ManTer_t * p )
{
    int i = 0;
    for ( ; i < Gia_ManPiNum(p->pAig); i++ )
        Gia_ManTerSimInfoSet( p->pDataSimCis, i, GIA_UND );
    for ( ; i < Gia_ManCiNum(p->pAig); i++ )
        Gia_ManTerSimInfoSet( p->pDataSimCis, i, GIA_ZER );
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline void Gia_ManTerSimInfoTransfer( Gia_ManTer_t * p )
{
    int i = 0;
    for ( ; i < Gia_ManPiNum(p->pAig); i++ )
        Gia_ManTerSimInfoSet( p->pDataSimCis, i, GIA_UND );
    for ( ; i < Gia_ManCiNum(p->pAig); i++ )
        Gia_ManTerSimInfoSet( p->pDataSimCis, i, Gia_ManTerSimInfoGet( p->pDataSimCos, Gia_ManCoNum(p->pAig)-Gia_ManCiNum(p->pAig)+i ) );
}


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

  Synopsis    [Computes hash value of the node using its simulation info.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Gia_ManTerStateHash( unsigned * pState, int nWords, int nTableSize )
{
    static int s_FPrimes[128] = { 
        1009, 1049, 1093, 1151, 1201, 1249, 1297, 1361, 1427, 1459, 
        1499, 1559, 1607, 1657, 1709, 1759, 1823, 1877, 1933, 1997, 
        2039, 2089, 2141, 2213, 2269, 2311, 2371, 2411, 2467, 2543, 
        2609, 2663, 2699, 2741, 2797, 2851, 2909, 2969, 3037, 3089, 
        3169, 3221, 3299, 3331, 3389, 3461, 3517, 3557, 3613, 3671, 
        3719, 3779, 3847, 3907, 3943, 4013, 4073, 4129, 4201, 4243, 
        4289, 4363, 4441, 4493, 4549, 4621, 4663, 4729, 4793, 4871, 
        4933, 4973, 5021, 5087, 5153, 5227, 5281, 5351, 5417, 5471, 
        5519, 5573, 5651, 5693, 5749, 5821, 5861, 5923, 6011, 6073, 
        6131, 6199, 6257, 6301, 6353, 6397, 6481, 6563, 6619, 6689, 
        6737, 6803, 6863, 6917, 6977, 7027, 7109, 7187, 7237, 7309, 
        7393, 7477, 7523, 7561, 7607, 7681, 7727, 7817, 7877, 7933, 
        8011, 8039, 8059, 8081, 8093, 8111, 8123, 8147
    };
    unsigned uHash;
    int i;
    uHash = 0;
    for ( i = 0; i < nWords; i++ )
        uHash ^= pState[i] * s_FPrimes[i & 0x7F];
    return uHash % nTableSize;
}

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

  Synopsis    [Inserts value into the table.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
unsigned * Gia_ManTerStateLookup( unsigned * pState, int nWords, unsigned ** pBins, int nBins )
{
    unsigned * pEntry;
    int Hash = Gia_ManTerStateHash( pState, nWords, nBins );
    for ( pEntry = pBins[Hash]; pEntry; pEntry = Gia_ManTerStateNext(pEntry, nWords) )
        if ( !memcmp( pEntry, pState, sizeof(unsigned) * nWords ) )
            return pEntry;
    return NULL;
}

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

  Synopsis    [Inserts value into the table.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Gia_ManTerStateInsert( unsigned * pState, int nWords, unsigned ** pBins, int nBins )
{
    int Hash = Gia_ManTerStateHash( pState, nWords, nBins );
    assert( !Gia_ManTerStateLookup( pState, nWords, pBins, nBins ) );
    Gia_ManTerStateSetNext( pState, nWords, pBins[Hash] );
    pBins[Hash] = pState;    
}

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

  Synopsis    [Allocs new ternary state.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
unsigned * Gia_ManTerStateAlloc( int nWords )
{
    return (unsigned *)ABC_CALLOC( char, sizeof(unsigned) * nWords + sizeof(unsigned *) );
}

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

  Synopsis    [Creates new ternary state.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
unsigned * Gia_ManTerStateCreate( Gia_ManTer_t * p )
{
    int i, Value, nPis = Gia_ManPiNum(p->pAig);
    unsigned * pRes = Gia_ManTerStateAlloc( p->nStateWords );
    for ( i = nPis; i < Gia_ManCiNum(p->pAig); i++ )
    {
        Value = Gia_ManTerSimInfoGet( p->pDataSimCis, i );
        Gia_ManTerSimInfoSet( pRes, i-nPis, Value );
        if ( Value == GIA_ZER )
            p->pCount0[i-nPis]++;
        if ( Value == GIA_UND )
            p->pCountX[i-nPis]++;
    }
    Vec_PtrPush( p->vStates, pRes );
    return pRes;    
}

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

  Synopsis    [Performs one round of ternary simulation.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline void Gia_ManTerSimulateRound( Gia_ManTer_t * p )
{
    Gia_Obj_t * pObj;
    int i, iCis = 0, iCos = 0;
    assert( p->pAig->nFront > 0 );
    assert( Gia_ManConst0(p->pAig)->Value == 0 );
Alan Mishchenko committed
380
    Gia_ManTerSimInfoSet( p->pDataSim, 0, GIA_ZER );
Alan Mishchenko committed
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
    Gia_ManForEachObj1( p->pAig, pObj, i )
    {
        if ( Gia_ObjIsAndOrConst0(pObj) )
        {
            assert( Gia_ObjValue(pObj) < p->pAig->nFront );
            Gia_ManTerSimulateNode( p, pObj );
        }
        else if ( Gia_ObjIsCi(pObj) )
        {
            assert( Gia_ObjValue(pObj) < p->pAig->nFront );
            Gia_ManTerSimulateCi( p, pObj, iCis++ );
        }
        else // if ( Gia_ObjIsCo(pObj) )
        {
            assert( Gia_ObjValue(pObj) == GIA_NONE );
            Gia_ManTerSimulateCo( p, iCos++, pObj );
        }
    }
    assert( Gia_ManCiNum(p->pAig) == iCis );
    assert( Gia_ManCoNum(p->pAig) == iCos );
}

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

  Synopsis    [Retires a set of registers to speed up convergence.]

  Description [Retire all non-ternary registers which has max number 
  of ternary values so far.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
415
int Gia_ManTerRetire2( Gia_ManTer_t * p, unsigned * pState )
Alan Mishchenko committed
416
{
417
    int i, Entry, iMaxTerValue = -1;
Alan Mishchenko committed
418
    // find non-retired register with this value
Alan Mishchenko committed
419
    for ( i = 0; i < Gia_ManRegNum(p->pAig); i++ )
Alan Mishchenko committed
420
        if ( Gia_ManTerSimInfoGet( pState, i ) != GIA_UND && !p->pRetired[i] && iMaxTerValue < p->pCountX[i] )
Alan Mishchenko committed
421
            iMaxTerValue = p->pCountX[i];
Alan Mishchenko committed
422 423
    assert( iMaxTerValue >= 0 );
    // retire the first registers with this value
Alan Mishchenko committed
424
    for ( i = 0; i < Gia_ManRegNum(p->pAig); i++ )
Alan Mishchenko committed
425
        if ( Gia_ManTerSimInfoGet( pState, i ) != GIA_UND && !p->pRetired[i] && iMaxTerValue == p->pCountX[i] )
Alan Mishchenko committed
426
        {
Alan Mishchenko committed
427 428 429 430 431
            assert( p->pRetired[i] == 0 );
            p->pRetired[i] = 1;
            Vec_IntPush( p->vRetired, i );
            if ( iMaxTerValue == 0 )
                break;
Alan Mishchenko committed
432
        }
Alan Mishchenko committed
433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
    // update all the retired registers
    Vec_IntForEachEntry( p->vRetired, Entry, i )
        Gia_ManTerSimInfoSet( p->pDataSimCis, Gia_ManPiNum(p->pAig)+Entry, GIA_UND );
    return Vec_IntSize(p->vRetired);
}

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

  Synopsis    [Retires a set of registers to speed up convergence.]

  Description [Retire all non-ternary registers which has max number 
  of ternary values so far.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Gia_ManTerRetire( Gia_ManTer_t * p, unsigned * pThis, unsigned * pPrev )
{
    int i, Entry;
    // find registers whose value has changed
    Vec_IntClear( p->vRetired );
    for ( i = 0; i < Gia_ManRegNum(p->pAig); i++ )
        if ( Gia_ManTerSimInfoGet( pThis, i ) != Gia_ManTerSimInfoGet( pPrev, i ) )
            Vec_IntPush( p->vRetired, i );
    // set all of them to zero
    Vec_IntForEachEntry( p->vRetired, Entry, i )
        Gia_ManTerSimInfoSet( p->pDataSimCis, Gia_ManPiNum(p->pAig)+Entry, GIA_UND );
    return Vec_IntSize(p->vRetired);
Alan Mishchenko committed
463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506
}

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

  Synopsis    [Inserts value into the table.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Gia_ManTerStatePrint( unsigned * pState, int nRegs, int iNum )
{
    int i, nZeros = 0, nOnes = 0, nDcs = 0;
    printf( " %4d : ", iNum );
    for ( i = 0; i < nRegs; i++ )
    {
        if ( Gia_ManTerSimInfoGet(pState, i) == GIA_ZER )
            printf( "0" ), nZeros++;
        else if ( Gia_ManTerSimInfoGet(pState, i) == GIA_ONE )
            printf( "1" ), nOnes++;
        else if ( Gia_ManTerSimInfoGet(pState, i) == GIA_UND )
            printf( "x" ), nDcs++;
        else
            assert( 0 );
    }
    printf( " (0=%4d, 1=%4d, x=%4d)\n", nZeros, nOnes, nDcs );
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Gia_ManTerAnalyze2( Vec_Ptr_t * vStates, int nRegs )
{
507
    unsigned * pTemp, * pStates = (unsigned *)Vec_PtrPop( vStates );
Alan Mishchenko committed
508 509
    int i, w, nZeros, nConsts, nStateWords;
    // detect constant zero registers
510
    nStateWords = Abc_BitWordNum( 2*nRegs );
Alan Mishchenko committed
511
    memset( pStates, 0, sizeof(int) * nStateWords );
512
    Vec_PtrForEachEntry( unsigned *, vStates, pTemp, i )
Alan Mishchenko committed
513 514 515 516 517 518 519 520 521 522
        for ( w = 0; w < nStateWords; w++ )
            pStates[w] |= pTemp[w];
    // count the number of zeros
    nZeros = 0;
    for ( i = 0; i < nRegs; i++ )
        if ( Gia_ManTerSimInfoGet(pStates, i) == GIA_ZER )
            nZeros++;
    printf( "Found %d constant registers.\n", nZeros );
    // detect non-ternary registers
    memset( pStates, 0, sizeof(int) * nStateWords );
523
    Vec_PtrForEachEntry( unsigned *, vStates, pTemp, i )
Alan Mishchenko committed
524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554
        for ( w = 0; w < nStateWords; w++ )
            pStates[w] |= (~(pTemp[w] ^ (pTemp[w] >> 1)) & 0x55555555);
    // count the nonternary registers
    nConsts = 0;
    for ( i = 0; i < nRegs; i++ )
        if ( Gia_ManTerSimInfoGet(pStates, i) == 0 )
            nConsts++;
    printf( "Found %d non-ternary registers.\n", nConsts );
    // return the state back
    Vec_PtrPush( vStates, pStates );
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Gia_ManTerAnalyze( Gia_ManTer_t * p )
{
    int i, nZeros = 0, nConsts = 0;
    for ( i = 0; i < Gia_ManRegNum(p->pAig); i++ )
        if ( p->pCount0[i] == Vec_PtrSize(p->vStates) )
            nZeros++;
        else if ( p->pCountX[i] == 0 )
            nConsts++;
Alan Mishchenko committed
555 556
//    printf( "Found %d constant registers.\n", nZeros );
//    printf( "Found %d non-ternary registers.\n", nConsts );
Alan Mishchenko committed
557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577
}



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

  Synopsis    [Transposes state vector for non-ternary registers.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Vec_Ptr_t * Gia_ManTerTranspose( Gia_ManTer_t * p )
{
    Vec_Ptr_t * vFlops;
    unsigned * pState, * pFlop;
    int i, k, nFlopWords;
    vFlops = Vec_PtrAlloc( 100 );
578
    nFlopWords = Abc_BitWordNum( 2*Vec_PtrSize(p->vStates) );
Alan Mishchenko committed
579 580 581 582 583 584 585 586
    for ( i = 0; i < Gia_ManRegNum(p->pAig); i++ )
    {
        if ( p->pCount0[i] == Vec_PtrSize(p->vStates) ) 
            continue;
        if ( p->pCountX[i] > 0 )
            continue;
        pFlop = Gia_ManTerStateAlloc( nFlopWords );
        Vec_PtrPush( vFlops, pFlop );
587
        Vec_PtrForEachEntry( unsigned *, p->vStates, pState, k )
Alan Mishchenko committed
588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608
            Gia_ManTerSimInfoSet( pFlop, k, Gia_ManTerSimInfoGet(pState, i) );
//Gia_ManTerStatePrint( pFlop, Vec_PtrSize(p->vStates), i );
    }
    return vFlops;
}

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

  Synopsis    [Transposes state vector for non-ternary registers.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Gia_ManFindEqualFlop( Vec_Ptr_t * vFlops, int iFlop, int nFlopWords )
{
    unsigned * pFlop, * pTemp;
    int i;
609 610
    pFlop = (unsigned *)Vec_PtrEntry( vFlops, iFlop );
    Vec_PtrForEachEntryStop( unsigned *, vFlops, pTemp, i, iFlop )
Alan Mishchenko committed
611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626
        if ( !memcmp( pTemp, pFlop, sizeof(unsigned) * nFlopWords ) )
            return i;
    return -1;
}

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

  Synopsis    [Creates map of registers to replace.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
627
int * Gia_ManTerCreateMap( Gia_ManTer_t * p, int fVerbose )
Alan Mishchenko committed
628 629 630 631 632
{
    int * pCi2Lit;
    Gia_Obj_t * pObj;
    Vec_Int_t * vMapKtoI;
    int i, iRepr, nFlopWords, Counter0 = 0, CounterE = 0;
633
    nFlopWords = Abc_BitWordNum( 2*Vec_PtrSize(p->vStates) );
Alan Mishchenko committed
634
    p->vFlops = Gia_ManTerTranspose( p );
635
    pCi2Lit = ABC_FALLOC( int, Gia_ManCiNum(p->pAig) );
Alan Mishchenko committed
636 637 638 639 640 641 642 643 644 645 646
    vMapKtoI = Vec_IntAlloc( 100 );
    for ( i = 0; i < Gia_ManRegNum(p->pAig); i++ )
        if ( p->pCount0[i] == Vec_PtrSize(p->vStates) )
            pCi2Lit[Gia_ManPiNum(p->pAig)+i] = 0, Counter0++;
        else if ( p->pCountX[i] == 0 )
        {
            iRepr = Gia_ManFindEqualFlop( p->vFlops, Vec_IntSize(vMapKtoI), nFlopWords );
            Vec_IntPush( vMapKtoI, i );
            if ( iRepr < 0 )
                continue;
            pObj = Gia_ManCi( p->pAig, Gia_ManPiNum(p->pAig)+Vec_IntEntry(vMapKtoI, iRepr) );
647
            pCi2Lit[Gia_ManPiNum(p->pAig)+i] = Abc_Var2Lit( Gia_ObjId( p->pAig, pObj ), 0 );
Alan Mishchenko committed
648 649 650
            CounterE++;
        }
    Vec_IntFree( vMapKtoI );
Alan Mishchenko committed
651 652
    if ( fVerbose )
        printf( "Transforming %d const and %d equiv registers.\n", Counter0, CounterE );
Alan Mishchenko committed
653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670
    return pCi2Lit;
}


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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Gia_ManTer_t * Gia_ManTerSimulate( Gia_Man_t * pAig, int fVerbose )
{
    Gia_ManTer_t * p;
Alan Mishchenko committed
671
    unsigned * pState, * pPrev, * pLoop;
672
    int i, Counter;
673
    abctime clk, clkTotal = Abc_Clock();
Alan Mishchenko committed
674 675
    assert( Gia_ManRegNum(pAig) > 0 );
    // create manager
676
    clk = Abc_Clock();
Alan Mishchenko committed
677
    p = Gia_ManTerCreate( pAig );
Alan Mishchenko committed
678
    if ( 0 )
Alan Mishchenko committed
679 680
    {
        printf( "Obj = %8d (%8d). F = %6d. ", 
681
            pAig->nObjs, Gia_ManCiNum(pAig) + Gia_ManAndNum(pAig), p->pAig->nFront );
682
        printf( "AIG = %7.2f MB. F-mem = %7.2f MB. Other = %7.2f MB.  ", 
Alan Mishchenko committed
683
            12.0*Gia_ManObjNum(p->pAig)/(1<<20), 
684 685
            4.0*Abc_BitWordNum(2 * p->pAig->nFront)/(1<<20), 
            4.0*Abc_BitWordNum(2 * (Gia_ManCiNum(pAig) + Gia_ManCoNum(pAig)))/(1<<20) );
686
        ABC_PRT( "Time", Abc_Clock() - clk );
Alan Mishchenko committed
687 688 689 690 691 692 693 694
    }
    // perform simulation
    Gia_ManTerSimInfoInit( p );
    // hash the first state
    pState = Gia_ManTerStateCreate( p );
    Gia_ManTerStateInsert( pState, p->nStateWords, p->pBins, p->nBins );
//Gia_ManTerStatePrint( pState, Gia_ManRegNum(pAig), 0 );
    // perform simuluation till convergence
Alan Mishchenko committed
695
    pPrev = NULL;
Alan Mishchenko committed
696 697 698 699 700 701 702 703 704 705 706 707 708 709 710
    for ( i = 0; ; i++ )
    {
        Gia_ManTerSimulateRound( p );
        Gia_ManTerSimInfoTransfer( p );
        pState = Gia_ManTerStateCreate( p );
//Gia_ManTerStatePrint( pState, Gia_ManRegNum(pAig), i+1 );
        if ( (pLoop = Gia_ManTerStateLookup(pState, p->nStateWords, p->pBins, p->nBins)) )
        {
            pAig->nTerStates = Vec_PtrSize( p->vStates );
            pAig->nTerLoop = Vec_PtrFind( p->vStates, pLoop );
            break;
        }
        Gia_ManTerStateInsert( pState, p->nStateWords, p->pBins, p->nBins );
        if ( i >= p->nIters && i % 10 == 0 )
        {
Alan Mishchenko committed
711 712 713 714
            Counter = Gia_ManTerRetire( p, pState, pPrev );
//            Counter = Gia_ManTerRetire2( p, pState );
//            if ( fVerbose )
//                printf( "Retired %d registers.\n", Counter );
Alan Mishchenko committed
715
        }
Alan Mishchenko committed
716
        pPrev = pState;
Alan Mishchenko committed
717 718 719
    }
    if ( fVerbose )
    {
Alan Mishchenko committed
720
        printf( "Ternary simulation saturated after %d iterations. ", i+1 );
721
        ABC_PRT( "Time", Abc_Clock() - clkTotal );
Alan Mishchenko committed
722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743
    }
    return p;
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Gia_Man_t * Gia_ManReduceConst( Gia_Man_t * pAig, int fVerbose )
{
    Gia_ManTer_t * p;
    Gia_Man_t * pNew = NULL;
    int * pCi2Lit;
    p = Gia_ManTerSimulate( pAig, fVerbose );
    Gia_ManTerAnalyze( p );
Alan Mishchenko committed
744
    pCi2Lit = Gia_ManTerCreateMap( p, fVerbose );
Alan Mishchenko committed
745 746 747 748 749 750 751 752 753 754 755
    Gia_ManTerDelete( p );
    pNew = Gia_ManDupDfsCiMap( pAig, pCi2Lit, NULL );
    ABC_FREE( pCi2Lit );
    return pNew;
}

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


756 757
ABC_NAMESPACE_IMPL_END