giaFrames.c 31.8 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    [giaFrames.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [Scalable AIG package.]

  Synopsis    [Timeframe unrolling.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

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

  Revision    [$Id: giaFrames.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
////////////////////////////////////////////////////////////////////////
///                        DECLARATIONS                              ///
////////////////////////////////////////////////////////////////////////

typedef struct Gia_ManFra_t_ Gia_ManFra_t;
struct Gia_ManFra_t_
{
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
    Gia_ParFra_t *  pPars;    // parameters
    Gia_Man_t *     pAig;     // AIG to unroll
    Vec_Ptr_t *     vIns;     // inputs of each timeframe    
    Vec_Ptr_t *     vAnds;    // nodes of each timeframe    
    Vec_Ptr_t *     vOuts;    // outputs of each timeframe    
};


typedef struct Gia_ManUnr_t_ Gia_ManUnr_t;
struct Gia_ManUnr_t_
{
    Gia_ParFra_t *  pPars;    // parameters
    Gia_Man_t *     pAig;     // AIG to unroll (points to pOrder)
    // internal data 
    Gia_Man_t *     pOrder;   // AIG reordered (points to pAig)
    Vec_Int_t *     vLimit;   // limits of each timeframe
    // data for each ordered node
    Vec_Int_t *     vRank;    // rank of each node
    Vec_Int_t *     vDegree;  // degree of each node
    Vec_Int_t *     vDegDiff; // degree of each node
    Vec_Int_t *     vFirst;   // first entry in the store
    Vec_Int_t *     vStore;   // store for saved data
55 56 57
    // the resulting AIG
    Gia_Man_t *     pNew;     // the resulting AIG
    int             LastLit;  // the place to store the last literal
Alan Mishchenko committed
58 59 60 61 62 63 64 65
};

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

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

66 67 68 69 70 71 72 73 74
  Synopsis    [Duplicates AIG for unrolling.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
75
void Gia_ManUnrollDup_rec( Gia_Man_t * pNew, Gia_Obj_t * pObj, int Id ) 
76 77 78 79 80 81 82
{
    if ( ~pObj->Value )
        return;
    if ( Gia_ObjIsCi(pObj) )
        pObj->Value = Gia_ManAppendCi(pNew);
    else if ( Gia_ObjIsCo(pObj) )
    {
83
        Gia_ManUnrollDup_rec( pNew, Gia_ObjFanin0(pObj), Gia_ObjFaninId0(pObj, Id) );
84 85 86 87
        pObj->Value = Gia_ManAppendCo( pNew, Gia_ObjFanin0Copy(pObj) );
    }
    else if ( Gia_ObjIsAnd(pObj) )
    {
88 89
        Gia_ManUnrollDup_rec( pNew, Gia_ObjFanin0(pObj), Gia_ObjFaninId0(pObj, Id) );
        Gia_ManUnrollDup_rec( pNew, Gia_ObjFanin1(pObj), Gia_ObjFaninId1(pObj, Id) );
90 91 92
        pObj->Value = Gia_ManAppendAnd( pNew, Gia_ObjFanin0Copy(pObj), Gia_ObjFanin1Copy(pObj) );
    }
    else assert( 0 );
93
    Gia_ManObj(pNew, Abc_Lit2Var(pObj->Value))->Value = Id;
94 95 96 97 98 99 100 101 102 103 104 105 106
}

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

  Synopsis    [Duplicates AIG for unrolling.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
107
Gia_Man_t * Gia_ManUnrollDup( Gia_Man_t * p, Vec_Int_t * vLimit )  
108 109 110 111 112 113
{
    Gia_Man_t * pNew;
    Gia_Obj_t * pObj;
    int i;
    assert( Vec_IntSize(vLimit) == 0 );
    pNew = Gia_ManStart( Gia_ManObjNum(p) );
114
    pNew->pName = Abc_UtilStrsav( p->pName );
115
    pNew->pSpec = Abc_UtilStrsav( p->pSpec );
116 117 118 119 120 121 122 123

    // save constant class
    Gia_ManFillValue( p );
    Gia_ManConst0(p)->Value = 0;
    Vec_IntPush( vLimit, Gia_ManObjNum(pNew) );
 
    // create first class
    Gia_ManForEachPo( p, pObj, i )
124
        Gia_ManUnrollDup_rec( pNew, pObj, Gia_ObjId(p, pObj) );
125 126 127 128 129 130 131 132 133 134 135 136
    Vec_IntPush( vLimit, Gia_ManObjNum(pNew) );

    // create next classes
    for ( i = 1; i < Gia_ManObjNum(pNew); i++ )
    {
        if ( i == Vec_IntEntryLast(vLimit) )
            Vec_IntPush( vLimit, Gia_ManObjNum(pNew) );
        pObj = Gia_ManObj( p, Gia_ManObj(pNew, i)->Value );
        if ( Gia_ObjIsRo(p, pObj) )
        {
            pObj = Gia_ObjRoToRi(p, pObj);
            assert( !~pObj->Value );
137
            Gia_ManUnrollDup_rec( pNew, pObj, Gia_ObjId(p, pObj) );
138 139 140 141 142 143 144 145
        }
    }
    Gia_ManSetRegNum( pNew, 0 );
    return pNew;
}

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

146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
  Synopsis    [Duplicates AIG for unrolling.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Vec_Ptr_t * Gia_ManUnrollAbs( Gia_Man_t * p, int nFrames )  
{
    int fVerbose = 0;
    Vec_Ptr_t * vFrames;
    Vec_Int_t * vLimit, * vOne;
    Gia_Man_t * pNew;
    Gia_Obj_t * pObj;
    int nObjBits, nObjMask;
    int f, fMax, k, Entry, Prev, iStart, iStop, Size;
    // get the bitmasks
165
    nObjBits = Abc_Base2Log( Gia_ManObjNum(p) );
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
    nObjMask = (1 << nObjBits) - 1;
    assert( Gia_ManObjNum(p) <= nObjMask );
    // derive the tents
    vLimit = Vec_IntAlloc( 1000 );
    pNew = Gia_ManUnrollDup( p, vLimit );
    // debug printout
    if ( fVerbose )
    {
        Prev = 1;
        printf( "Tents: " );
        Vec_IntForEachEntryStart( vLimit, Entry, k, 1 )
            printf( "%d=%d ", k, Entry-Prev ), Prev = Entry;
        printf( "  Unused=%d", Gia_ManObjNum(p) - Gia_ManObjNum(pNew) );
        printf( "\n" );
    }
    // create abstraction
    vFrames = Vec_PtrAlloc( Vec_IntSize(vLimit) );
    for ( fMax = 0; fMax < nFrames; fMax++ )
    {
        Size = (fMax+1 < Vec_IntSize(vLimit)) ? Vec_IntEntry(vLimit, fMax+1) : Gia_ManObjNum(pNew);
        vOne = Vec_IntAlloc( Size );
        for ( f = 0; f <= fMax; f++ )
        {
            iStart = (f   < Vec_IntSize(vLimit)) ? Vec_IntEntry(vLimit, f  ) : 0;
            iStop  = (f+1 < Vec_IntSize(vLimit)) ? Vec_IntEntry(vLimit, f+1) : 0;
            for ( k = iStop - 1; k >= iStart; k-- )
            {
193 194 195
                assert( Gia_ManObj(pNew, k)->Value > 0 );
                pObj  = Gia_ManObj(p, Gia_ManObj(pNew, k)->Value);
                if ( Gia_ObjIsCo(pObj) || Gia_ObjIsPi(p, pObj) )
196
                    continue;
197 198
                assert( Gia_ObjIsRo(p, pObj) || Gia_ObjIsAnd(pObj) );
                Entry = ((fMax-f) << nObjBits) | Gia_ObjId(p, pObj);
199 200 201 202 203
                Vec_IntPush( vOne, Entry );
//                printf( "%d ", Gia_ManObj(pNew, k)->Value );
            }
//            printf( "\n" );
        }
204 205
        // add in reverse topological order
        Vec_IntSort( vOne, 1 );
206 207 208 209 210 211 212 213 214 215
        Vec_PtrPush( vFrames, vOne );
        assert( Vec_IntSize(vOne) <= Size - 1 );
    }
    Vec_IntFree( vLimit );
    Gia_ManStop( pNew );
    return vFrames;
}

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

216 217 218 219 220 221 222 223 224 225 226 227 228 229
  Synopsis    [Creates manager.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Gia_ManUnr_t * Gia_ManUnrStart( Gia_Man_t * pAig, Gia_ParFra_t * pPars )  
{ 
    Gia_ManUnr_t * p;
    Gia_Obj_t * pObj;
    int i, k, iRank, iFanin, Degree, Shift;
230
    clock_t clk = clock();
231

232 233 234 235 236
    p = ABC_CALLOC( Gia_ManUnr_t, 1 );
    p->pAig   = pAig;
    p->pPars  = pPars;
    // create order
    p->vLimit = Vec_IntAlloc( 0 );
237
    p->pOrder = Gia_ManUnrollDup( pAig, p->vLimit );
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
/*
    Vec_IntForEachEntryStart( p->vLimit, Shift, i, 1 )
        printf( "%d=%d ", i, Shift-Vec_IntEntry(p->vLimit, i-1) );
    printf( "\n" );
*/
    // assign rank
    p->vRank  = Vec_IntAlloc( Gia_ManObjNum(p->pOrder) );
    for ( iRank = i = 0; i < Gia_ManObjNum(p->pOrder); Vec_IntPush(p->vRank, iRank), i++ )
        if ( Vec_IntEntry(p->vLimit, iRank) == i )
            iRank++;
    assert( iRank == Vec_IntSize(p->vLimit)-1 );
    assert( Vec_IntSize(p->vRank) == Gia_ManObjNum(p->pOrder) );
    // assign degree
    p->vDegree = Vec_IntStart( Gia_ManObjNum(p->pOrder) );
    p->vDegDiff= Vec_IntStart( 2* Gia_ManObjNum(p->pOrder) );
    Gia_ManForEachAnd( p->pOrder, pObj, i )
    {
        for ( k = 0; k < 2; k++ )
        {
            iFanin = k ? Gia_ObjFaninId1(pObj, i) : Gia_ObjFaninId0(pObj, i);
            Degree = Vec_IntEntry(p->vRank, i) - Vec_IntEntry(p->vRank, iFanin);
            Vec_IntWriteEntry( p->vDegDiff, 2*i + k, Degree );
            if ( Vec_IntEntry(p->vDegree, iFanin) < Degree )
                Vec_IntWriteEntry( p->vDegree, iFanin, Degree );
        }
    }
    Gia_ManForEachCo( p->pOrder, pObj, k )
    {
        i      = Gia_ObjId( p->pOrder, pObj );
        iFanin = Gia_ObjFaninId0(pObj, i);
        Degree = Vec_IntEntry(p->vRank, i) - Vec_IntEntry(p->vRank, iFanin);
        Vec_IntWriteEntry( p->vDegDiff, 2*i, Degree );
        if ( Vec_IntEntry(p->vDegree, iFanin) < Degree )
            Vec_IntWriteEntry( p->vDegree, iFanin, Degree );
    }
    // assign first
    p->vFirst = Vec_IntAlloc( Gia_ManObjNum(p->pOrder) );
    p->vStore = Vec_IntStartFull( 2* Gia_ManObjNum(p->pOrder) + Vec_IntSum(p->vDegree) );
    for ( Shift = i = 0; i < Gia_ManObjNum(p->pOrder); i++ )
    {
        Vec_IntPush( p->vFirst, Shift );
        Vec_IntWriteEntry( p->vStore, Shift, 1 + Vec_IntEntry(p->vDegree, i) ); 
        Shift += 2 + Vec_IntEntry(p->vDegree, i);
    }
    assert( Shift == Vec_IntSize(p->vStore) );
    // cleanup
    Vec_IntFreeP( &p->vRank );
    Vec_IntFreeP( &p->vDegree );
286 287 288 289 290 291 292 293 294 295

    // print verbose output
    if ( pPars->fVerbose )
    {
        printf( "Convergence = %d.  Dangling objects = %d.  Average degree = %.3f   ", 
            Vec_IntSize(p->vLimit) - 1,
            Gia_ManObjNum(pAig) - Gia_ManObjNum(p->pOrder),
            1.0*Vec_IntSize(p->vStore)/Gia_ManObjNum(p->pOrder) - 1.0  );
        Abc_PrintTime( 1, "Time", clock() - clk );
    }
296 297 298 299 300 301 302 303 304 305 306 307 308 309
    return p;
}

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

  Synopsis    [Deletes manager.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
310
void Gia_ManUnrollStop( void * pMan )  
311
{
312
    Gia_ManUnr_t * p = (Gia_ManUnr_t *)pMan;
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
    Gia_ManStopP( &p->pOrder );
    Vec_IntFreeP( &p->vLimit );
    Vec_IntFreeP( &p->vRank );
    Vec_IntFreeP( &p->vDegree );
    Vec_IntFreeP( &p->vDegDiff );
    Vec_IntFreeP( &p->vFirst );
    Vec_IntFreeP( &p->vStore );
    ABC_FREE( p );
}


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

  Synopsis    [Reading/writing entry from storage.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline void Gia_ObjUnrWrite( Gia_ManUnr_t * p, int Id, int Entry )  
{
    int i, * pArray = Vec_IntEntryP( p->vStore, Vec_IntEntry(p->vFirst, Id) );
    for ( i = pArray[0]; i > 1; i-- )
        pArray[i] = pArray[i-1];
    pArray[1] = Entry;
}
static inline int Gia_ObjUnrRead( Gia_ManUnr_t * p, int Id, int Degree )  
{
    int * pArray = Vec_IntEntryP( p->vStore, Vec_IntEntry(p->vFirst, Id) );
    if ( Id == 0 )
        return 0;
    assert( Degree >= 0 && Degree < pArray[0] );
    if ( Degree )
        Degree--;
    return pArray[Degree + 1];
}
static inline int Gia_ObjUnrReadCopy0( Gia_ManUnr_t * p, Gia_Obj_t * pObj, int Id )  
{
    int Lit = Gia_ObjUnrRead(p, Gia_ObjFaninId0(pObj, Id), Vec_IntEntry(p->vDegDiff, 2*Id));
355
    return Abc_LitNotCond( Lit, Gia_ObjFaninC0(pObj) );
356 357 358 359
}
static inline int Gia_ObjUnrReadCopy1( Gia_ManUnr_t * p, Gia_Obj_t * pObj, int Id )  
{
    int Lit = Gia_ObjUnrRead(p, Gia_ObjFaninId1(pObj, Id), Vec_IntEntry(p->vDegDiff, 2*Id+1));
360
    return Abc_LitNotCond( Lit, Gia_ObjFaninC1(pObj) );
361 362 363 364 365 366 367 368
}
static inline int Gia_ObjUnrReadCi( Gia_ManUnr_t * p, int Id, int f, Gia_Man_t * pNew )  
{
    Gia_Obj_t * pObj = Gia_ManObj( p->pOrder, Id );
    Gia_Obj_t * pObjReal = Gia_ManObj( p->pAig, pObj->Value );
    assert( Gia_ObjIsCi(pObjReal) );
    if ( Gia_ObjIsPi(p->pAig, pObjReal) )
    {
369 370 371 372
        if ( !p->pPars->fSaveLastLit ) 
            pObj = Gia_ManPi( pNew, Gia_ManPiNum(p->pAig) * f + Gia_ObjCioId(pObjReal) );
        else
            pObj = Gia_ManPi( pNew, Gia_ManRegNum(p->pAig) + Gia_ManPiNum(p->pAig) * f + Gia_ObjCioId(pObjReal) );
373
        return Abc_Var2Lit( Gia_ObjId(pNew, pObj), 0 );
374 375 376 377 378 379
    }
    if ( f == 0 ) // initialize!
    {
        if ( p->pPars->fInit )
            return 0;
        assert( Gia_ObjCioId(pObjReal) >= Gia_ManPiNum(p->pAig) );
380 381 382 383
        if ( !p->pPars->fSaveLastLit ) 
            pObj = Gia_ManPi( pNew, Gia_ManPiNum(p->pAig) * p->pPars->nFrames + Gia_ObjCioId(pObjReal)-Gia_ManPiNum(p->pAig) );
        else
            pObj = Gia_ManPi( pNew, Gia_ObjCioId(pObjReal)-Gia_ManPiNum(p->pAig) );
384
        return Abc_Var2Lit( Gia_ObjId(pNew, pObj), 0 );
385
    }
386
    pObj = Gia_ManObj( p->pOrder, Abc_Lit2Var(Gia_ObjRoToRi(p->pAig, pObjReal)->Value) );
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
    assert( Gia_ObjIsCo(pObj) );
    return Gia_ObjUnrRead( p, Gia_ObjId(p->pOrder, pObj), 0 );
}

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

  Synopsis    [Computes init/non-init unrolling without flops.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
402 403 404 405 406 407 408 409 410
void * Gia_ManUnrollStart( Gia_Man_t * pAig, Gia_ParFra_t * pPars )
{
    Gia_ManUnr_t * p;
    int f, i;
    // start 
    p = Gia_ManUnrStart( pAig, pPars );
    // start timeframes
    assert( p->pNew == NULL );
    p->pNew = Gia_ManStart( 10000 );
411
    p->pNew->pName = Abc_UtilStrsav( p->pAig->pName );
412
    p->pNew->pSpec = Abc_UtilStrsav( p->pAig->pSpec );
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 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 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540
    Gia_ManHashAlloc( p->pNew );
    // create combinational inputs
    if ( !p->pPars->fSaveLastLit ) // only in the case when unrolling depth is known
        for ( f = 0; f < p->pPars->nFrames; f++ )
            for ( i = 0; i < Gia_ManPiNum(p->pAig); i++ )
                Gia_ManAppendCi(p->pNew);
    // create flop outputs
    if ( !p->pPars->fInit ) // only in the case when initialization is not performed
        for ( i = 0; i < Gia_ManRegNum(p->pAig); i++ )
            Gia_ManAppendCi(p->pNew);
    return p;
}

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

  Synopsis    [Computes init/non-init unrolling without flops.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void * Gia_ManUnrollAdd( void * pMan, int fMax )
{
    Gia_ManUnr_t * p = (Gia_ManUnr_t *)pMan;
    Gia_Obj_t * pObj;
    int f, i, Lit, Beg, End;
    // create PIs on demand
    if ( p->pPars->fSaveLastLit ) 
        for ( i = 0; i < Gia_ManPiNum(p->pAig); i++ )
            Gia_ManAppendCi(p->pNew);
    // unroll another timeframe
    for ( f = 0; f < fMax; f++ )
    {
        if ( Vec_IntSize(p->vLimit) <= fMax-f )
            continue;
        Beg = Vec_IntEntry( p->vLimit, fMax-f-1 );
        End = Vec_IntEntry( p->vLimit, fMax-f );
        for ( i = Beg; i < End; i++ )
        {
            pObj = Gia_ManObj( p->pOrder, i );
            if ( Gia_ObjIsAnd(pObj) )
                Lit = Gia_ManHashAnd( p->pNew, Gia_ObjUnrReadCopy0(p, pObj, i), Gia_ObjUnrReadCopy1(p, pObj, i) );
            else if ( Gia_ObjIsCo(pObj) )
            {
                Lit = Gia_ObjUnrReadCopy0(p, pObj, i);
                if ( f == fMax-1 )
                {
                    if ( p->pPars->fSaveLastLit )
                        p->LastLit = Lit;
                    else
                        Gia_ManAppendCo( p->pNew, Lit );
                }
            }
            else if ( Gia_ObjIsCi(pObj) )
                Lit = Gia_ObjUnrReadCi( p, i, f, p->pNew );
            else assert( 0 );
            assert( Lit >= 0 );
            Gia_ObjUnrWrite( p, i, Lit ); // should be exactly one call for each obj!
        }
    }
    return p->pNew;
}

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

  Synopsis    [Read the last literal.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Gia_ManUnrollLastLit( void * pMan )
{
    Gia_ManUnr_t * p = (Gia_ManUnr_t *)pMan;
    return p->LastLit;
}

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

  Synopsis    [Computes init/non-init unrolling without flops.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Gia_Man_t * Gia_ManUnroll( Gia_Man_t * pAig, Gia_ParFra_t * pPars )
{
    Gia_ManUnr_t * p;
    Gia_Man_t * pNew, * pTemp;
    int fMax;
    p = (Gia_ManUnr_t *)Gia_ManUnrollStart( pAig, pPars );
    for ( fMax = 1; fMax <= p->pPars->nFrames; fMax++ )
        Gia_ManUnrollAdd( p, fMax );
    assert( Gia_ManPoNum(p->pNew) == p->pPars->nFrames * Gia_ManPoNum(p->pAig) );
    Gia_ManHashStop( p->pNew );
    Gia_ManSetRegNum( p->pNew, 0 );
//    Gia_ManPrintStats( pNew, 0 );
    // cleanup
    p->pNew = Gia_ManCleanup( pTemp = p->pNew );
    Gia_ManStop( pTemp );
//    Gia_ManPrintStats( pNew, 0 );
    pNew = p->pNew;  p->pNew = NULL;
    Gia_ManUnrollStop( p );
    return pNew;
}


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

  Synopsis    [Computes init/non-init unrolling without flops.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
/*
541 542 543 544 545 546 547
Gia_Man_t * Gia_ManUnroll( Gia_ManUnr_t * p )
{
    Gia_Man_t * pNew, * pTemp;
    Gia_Obj_t * pObj;
    int fMax, f, i, Lit, Beg, End;
    // start timeframes
    pNew = Gia_ManStart( 10000 );
548
    pNew->pName = Abc_UtilStrsav( p->pAig->pName );
549
    pNew->pSpec = Abc_UtilStrsav( p->pAig->pSpec );
550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594
    Gia_ManHashAlloc( pNew );
    // create combinational inputs
    for ( f = 0; f < p->pPars->nFrames; f++ )
        for ( i = 0; i < Gia_ManPiNum(p->pAig); i++ )
            Gia_ManAppendCi(pNew);
    if ( !p->pPars->fInit )
        for ( i = 0; i < Gia_ManRegNum(p->pAig); i++ )
            Gia_ManAppendCi(pNew);
    // add nodes to each time-frame
//    Gia_ObjUnrWrite( p, 0, 0 );
    for ( fMax = 1; fMax <= p->pPars->nFrames; fMax++ )
        for ( f = 0; f < fMax; f++ )
        {
            if ( Vec_IntSize(p->vLimit) <= fMax-f )
                continue;
            Beg = Vec_IntEntry( p->vLimit, fMax-f-1 );
            End = Vec_IntEntry( p->vLimit, fMax-f );
            for ( i = Beg; i < End; i++ )
            {
                pObj = Gia_ManObj( p->pOrder, i );
                if ( Gia_ObjIsAnd(pObj) )
                    Lit = Gia_ManHashAnd( pNew, Gia_ObjUnrReadCopy0(p, pObj, i), Gia_ObjUnrReadCopy1(p, pObj, i) );
                else if ( Gia_ObjIsCo(pObj) )
                {
                    Lit = Gia_ObjUnrReadCopy0(p, pObj, i);
                    if ( f == fMax-1 )
                        Gia_ManAppendCo( pNew, Lit );
                }
                else if ( Gia_ObjIsCi(pObj) )
                    Lit = Gia_ObjUnrReadCi( p, i, f, pNew );
                else assert( 0 );
                assert( Lit >= 0 );
                Gia_ObjUnrWrite( p, i, Lit ); // should be exactly one call for each obj!
            }
        }
    assert( Gia_ManPoNum(pNew) == p->pPars->nFrames * Gia_ManPoNum(p->pAig) );
    Gia_ManHashStop( pNew );
    Gia_ManSetRegNum( pNew, 0 );
//    Gia_ManPrintStats( pNew, 0 );
    // cleanup
    pNew = Gia_ManCleanup( pTemp = pNew );
    Gia_ManStop( pTemp );
//    Gia_ManPrintStats( pNew, 0 );
    return pNew;
} 
595
*/
596 597 598 599 600 601 602 603 604 605 606 607 608 609

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Gia_Man_t * Gia_ManFrames2( Gia_Man_t * pAig, Gia_ParFra_t * pPars )
{
610
    Gia_Man_t * pNew;
611
    clock_t clk = clock();
612
    pNew = Gia_ManUnroll( pAig, pPars );
613 614 615 616 617 618 619 620 621
    if ( pPars->fVerbose )
        Abc_PrintTime( 1, "Time", clock() - clk );
    return pNew;
}



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

Alan Mishchenko committed
622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697
  Synopsis    [This procedure sets default parameters.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Gia_ManFraSetDefaultParams( Gia_ParFra_t * p )
{
    memset( p, 0, sizeof(Gia_ParFra_t) );
    p->nFrames      =  32;    // the number of frames to unroll
    p->fInit        =   0;    // initialize the timeframes
    p->fVerbose     =   0;    // enables verbose output
}

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

  Synopsis    [Creates manager.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Gia_ManFra_t * Gia_ManFraStart( Gia_Man_t * pAig, Gia_ParFra_t * pPars )  
{ 
    Gia_ManFra_t * p;
    p = ABC_ALLOC( Gia_ManFra_t, 1 );
    memset( p, 0, sizeof(Gia_ManFra_t) );
    p->pAig  = pAig;
    p->pPars = pPars;
    return p;
}

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

  Synopsis    [Deletes manager.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Gia_ManFraStop( Gia_ManFra_t * p )  
{
    Vec_VecFree( (Vec_Vec_t *)p->vIns );
    Vec_VecFree( (Vec_Vec_t *)p->vAnds );
    Vec_VecFree( (Vec_Vec_t *)p->vOuts );
    ABC_FREE( p );
}

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

  Synopsis    [Computes supports of all timeframes.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Gia_ManFraSupports( Gia_ManFra_t * p )
{
    Vec_Int_t * vIns = NULL, * vAnds, * vOuts;
    Gia_Obj_t * pObj;
    int f, i;
    p->vIns  = Vec_PtrStart( p->pPars->nFrames );
    p->vAnds = Vec_PtrStart( p->pPars->nFrames );
    p->vOuts = Vec_PtrStart( p->pPars->nFrames );
698
    Gia_ManIncrementTravId( p->pAig );
Alan Mishchenko committed
699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761
    for ( f = p->pPars->nFrames - 1; f >= 0; f-- )
    {
        vOuts = Gia_ManCollectPoIds( p->pAig );
        if ( vIns )
        Gia_ManForEachObjVec( vIns, p->pAig, pObj, i )
            if ( Gia_ObjIsRo(p->pAig, pObj) )
                Vec_IntPush( vOuts, Gia_ObjId( p->pAig, Gia_ObjRoToRi(p->pAig, pObj) ) );
        vIns = Vec_IntAlloc( 100 );
        Gia_ManCollectCis( p->pAig, Vec_IntArray(vOuts), Vec_IntSize(vOuts), vIns );
        vAnds = Vec_IntAlloc( 100 );
        Gia_ManCollectAnds( p->pAig, Vec_IntArray(vOuts), Vec_IntSize(vOuts), vAnds );
        Vec_PtrWriteEntry( p->vIns,  f, vIns );
        Vec_PtrWriteEntry( p->vAnds, f, vAnds );
        Vec_PtrWriteEntry( p->vOuts, f, vOuts );
    }
}

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

  Synopsis    [Moves the first nRegs entries to the end.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Gia_ManFraTransformCis( Gia_Man_t * pAig, Vec_Int_t * vCis )
{
    int i, k = 0, Entry;
    Vec_IntForEachEntryStop( vCis, Entry, i, Gia_ManRegNum(pAig) )
        assert( Entry == i+1 );
    Vec_IntForEachEntryStart( vCis, Entry, i, Gia_ManRegNum(pAig) )
        Vec_IntWriteEntry( vCis, k++, Entry );
    for ( i = 0; i < Gia_ManRegNum(pAig); i++ )
        Vec_IntWriteEntry( vCis, k++, i+1 );
    assert( k == Vec_IntSize(vCis) );
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Gia_Man_t * Gia_ManFramesInit( Gia_Man_t * pAig, Gia_ParFra_t * pPars )
{
    int fUseAllPis = 1;
    Gia_Man_t * pFrames, * pTemp;
    Gia_ManFra_t * p;
    Gia_Obj_t * pObj;
    Vec_Int_t * vIns, * vAnds, * vOuts;
    int i, f;
    p = Gia_ManFraStart( pAig, pPars );
    Gia_ManFraSupports( p );
    pFrames = Gia_ManStart( Vec_VecSizeSize((Vec_Vec_t*)p->vIns)+
        Vec_VecSizeSize((Vec_Vec_t*)p->vAnds)+Vec_VecSizeSize((Vec_Vec_t*)p->vOuts) );
762
    pFrames->pName = Abc_UtilStrsav( pAig->pName );
763
    pFrames->pSpec = Abc_UtilStrsav( pAig->pSpec );
Alan Mishchenko committed
764 765 766 767
    Gia_ManHashAlloc( pFrames );
    Gia_ManConst0(pAig)->Value = 0;
    for ( f = 0; f < pPars->nFrames; f++ )
    {
768 769 770
        vIns  = (Vec_Int_t *)Vec_PtrEntry( p->vIns,  f );
        vAnds = (Vec_Int_t *)Vec_PtrEntry( p->vAnds, f );
        vOuts = (Vec_Int_t *)Vec_PtrEntry( p->vOuts, f );
Alan Mishchenko committed
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 804 805 806 807 808 809 810 811 812 813 814 815 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
        if ( pPars->fVerbose )
            printf( "Frame %3d : CI = %6d. AND = %6d. CO = %6d.\n", 
            f, Vec_IntSize(vIns), Vec_IntSize(vAnds), Vec_IntSize(vOuts) );
        if ( fUseAllPis )
        {
            Gia_ManForEachPi( pAig, pObj, i )
                pObj->Value = Gia_ManAppendCi( pFrames );
            if ( f == 0 )
            {
                Gia_ManForEachObjVec( vIns, pAig, pObj, i )
                {
                    assert( Gia_ObjIsCi(pObj) );
                    if ( !Gia_ObjIsPi(pAig, pObj) )
                        pObj->Value = 0;
                }
            }
            else
            {
                Gia_ManForEachObjVec( vIns, pAig, pObj, i )
                {
                    assert( Gia_ObjIsCi(pObj) );
                    if ( !Gia_ObjIsPi(pAig, pObj) )
                        pObj->Value = Gia_ObjRoToRi(pAig, pObj)->Value;
                }
            }
        }
        else
        {
            if ( f == 0 )
            {
                Gia_ManForEachObjVec( vIns, pAig, pObj, i )
                {
                    assert( Gia_ObjIsCi(pObj) );
                    if ( Gia_ObjIsPi(pAig, pObj) )
                        pObj->Value = Gia_ManAppendCi( pFrames );
                    else
                        pObj->Value = 0;
                }
            }
            else
            {
                Gia_ManForEachObjVec( vIns, pAig, pObj, i )
                {
                    assert( Gia_ObjIsCi(pObj) );
                    if ( Gia_ObjIsPi(pAig, pObj) )
                        pObj->Value = Gia_ManAppendCi( pFrames );
                    else
                        pObj->Value = Gia_ObjRoToRi(pAig, pObj)->Value;
                }
            }
        }
        Gia_ManForEachObjVec( vAnds, pAig, pObj, i )
        {
            assert( Gia_ObjIsAnd(pObj) );
            pObj->Value = Gia_ManHashAnd( pFrames, Gia_ObjFanin0Copy(pObj), Gia_ObjFanin1Copy(pObj) );
        }
        Gia_ManForEachObjVec( vOuts, pAig, pObj, i )
        {
            assert( Gia_ObjIsCo(pObj) );
            if ( Gia_ObjIsPo(pAig, pObj) )
                pObj->Value = Gia_ManAppendCo( pFrames, Gia_ObjFanin0Copy(pObj) );
            else
                pObj->Value = Gia_ObjFanin0Copy(pObj);
        }
    }
    Gia_ManFraStop( p );
    Gia_ManHashStop( pFrames );
    if ( Gia_ManCombMarkUsed(pFrames) < Gia_ManAndNum(pFrames) )
    {
        pFrames = Gia_ManDupMarked( pTemp = pFrames );
        if ( pPars->fVerbose )
            printf( "Before cleanup = %d nodes. After cleanup = %d nodes.\n", 
                Gia_ManAndNum(pTemp), Gia_ManAndNum(pFrames) );
        Gia_ManStop( pTemp );
    }
    else if ( pPars->fVerbose )
            printf( "Before cleanup = %d nodes. After cleanup = %d nodes.\n", 
                Gia_ManAndNum(pFrames), Gia_ManAndNum(pFrames) );
    return pFrames;
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Gia_Man_t * Gia_ManFrames( Gia_Man_t * pAig, Gia_ParFra_t * pPars )
{
    Gia_Man_t * pFrames, * pTemp;
    Gia_Obj_t * pObj;
    int i, f;
    assert( Gia_ManRegNum(pAig) > 0 );
    assert( pPars->nFrames > 0 );
    if ( pPars->fInit )
        return Gia_ManFramesInit( pAig, pPars );
    pFrames = Gia_ManStart( pPars->nFrames * Gia_ManObjNum(pAig) );
873
    pFrames->pName = Abc_UtilStrsav( pAig->pName );
874
    pFrames->pSpec = Abc_UtilStrsav( pAig->pSpec );
Alan Mishchenko committed
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 909 910 911 912 913 914 915 916 917 918 919 920 921 922
    Gia_ManHashAlloc( pFrames );
    Gia_ManConst0(pAig)->Value = 0;
    for ( f = 0; f < pPars->nFrames; f++ )
    {
        if ( f == 0 )
        {
            Gia_ManForEachRo( pAig, pObj, i )
                pObj->Value = Gia_ManAppendCi( pFrames );
        }
        else
        {
            Gia_ManForEachRo( pAig, pObj, i )
                pObj->Value = Gia_ObjRoToRi( pAig, pObj )->Value;
        }
        Gia_ManForEachPi( pAig, pObj, i )
            pObj->Value = Gia_ManAppendCi( pFrames );
        Gia_ManForEachAnd( pAig, pObj, i )
            pObj->Value = Gia_ManHashAnd( pFrames, Gia_ObjFanin0Copy(pObj), Gia_ObjFanin1Copy(pObj) );
        Gia_ManForEachPo( pAig, pObj, i )
            pObj->Value = Gia_ManAppendCo( pFrames, Gia_ObjFanin0Copy(pObj) );
        if ( f == pPars->nFrames - 1 )
        {
            Gia_ManForEachRi( pAig, pObj, i )
                pObj->Value = Gia_ManAppendCo( pFrames, Gia_ObjFanin0Copy(pObj) );
        }
        else
        {
            Gia_ManForEachRi( pAig, pObj, i )
                pObj->Value = Gia_ObjFanin0Copy(pObj);
        }
    }
    Gia_ManHashStop( pFrames );
    Gia_ManFraTransformCis( pAig, pFrames->vCis );
    Gia_ManSetRegNum( pFrames, Gia_ManRegNum(pAig) );
    if ( Gia_ManCombMarkUsed(pFrames) < Gia_ManAndNum(pFrames) )
    {
        pFrames = Gia_ManDupMarked( pTemp = pFrames );
        if ( pPars->fVerbose )
            printf( "Before cleanup = %d nodes. After cleanup = %d nodes.\n", 
                Gia_ManAndNum(pTemp), Gia_ManAndNum(pFrames) );
        Gia_ManStop( pTemp );
    }
    else if ( pPars->fVerbose )
            printf( "Before cleanup = %d nodes. After cleanup = %d nodes.\n", 
                Gia_ManAndNum(pFrames), Gia_ManAndNum(pFrames) );
    return pFrames;
}

923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944

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

  Synopsis    [Perform init unrolling as long as PO(s) are constant 0.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Gia_Man_t * Gia_ManFramesInitSpecial( Gia_Man_t * pAig, int nFrames, int fVerbose )
{
    Gia_Man_t * pFrames, * pTemp;
    Gia_Obj_t * pObj;
    int i, f;
    assert( Gia_ManRegNum(pAig) > 0 );
    if ( nFrames > 0 )
        printf( "Computing specialized unrolling with %d frames...\n", nFrames );
    pFrames = Gia_ManStart( Gia_ManObjNum(pAig) );
    pFrames->pName = Abc_UtilStrsav( pAig->pName );
945
    pFrames->pSpec = Abc_UtilStrsav( pAig->pSpec );
946 947 948 949 950 951 952
    Gia_ManHashAlloc( pFrames );
    Gia_ManConst0(pAig)->Value = 0;
    for ( f = 0; nFrames == 0 || f < nFrames; f++ )
    {
        if ( fVerbose && (f % 100 == 0) )
        {
            printf( "%6d : ", f );
953
            Gia_ManPrintStats( pFrames, 0, 0, 0 );
954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983
        }
        Gia_ManForEachRo( pAig, pObj, i )
            pObj->Value = f ? Gia_ObjRoToRi( pAig, pObj )->Value : 0;
        Gia_ManForEachPi( pAig, pObj, i )
            pObj->Value = Gia_ManAppendCi( pFrames );
        Gia_ManForEachAnd( pAig, pObj, i )
            pObj->Value = Gia_ManHashAnd( pFrames, Gia_ObjFanin0Copy(pObj), Gia_ObjFanin1Copy(pObj) );
        Gia_ManForEachPo( pAig, pObj, i )
            if ( Gia_ObjFanin0Copy(pObj) != 0 )
                break;
        if ( i < Gia_ManPoNum(pAig) )
            break;
        Gia_ManForEachRi( pAig, pObj, i )
            pObj->Value = Gia_ObjFanin0Copy(pObj);
    }
    if ( fVerbose )
        printf( "Computed prefix of %d frames.\n", f );
    Gia_ManForEachRi( pAig, pObj, i )
        Gia_ManAppendCo( pFrames, pObj->Value );
    Gia_ManHashStop( pFrames );
    pFrames = Gia_ManCleanup( pTemp = pFrames );
    if ( fVerbose )
        printf( "Before cleanup = %d nodes. After cleanup = %d nodes.\n", 
            Gia_ManAndNum(pTemp), Gia_ManAndNum(pFrames) );
    Gia_ManStop( pTemp );
    return pFrames;
}



Alan Mishchenko committed
984 985 986 987 988
////////////////////////////////////////////////////////////////////////
///                       END OF FILE                                ///
////////////////////////////////////////////////////////////////////////


989 990
ABC_NAMESPACE_IMPL_END