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

  FileName    [giaEdge.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [Scalable AIG package.]

  Synopsis    [Edge-related procedures.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

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

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

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

#include "gia.h"
22
#include "misc/tim/tim.h"
23 24 25 26 27 28 29

ABC_NAMESPACE_IMPL_START

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

30
static inline int Gia_ObjEdgeCount( int iObj, Vec_Int_t * vEdge1, Vec_Int_t * vEdge2 )
31 32 33
{
    return (Vec_IntEntry(vEdge1, iObj) > 0) + (Vec_IntEntry(vEdge2, iObj) > 0);
}
34
static inline int Gia_ObjEdgeAdd( int iObj, int iNext, Vec_Int_t * vEdge1, Vec_Int_t * vEdge2 )
35
{
36
    int RetValue = 0;
37 38 39 40
    if ( Vec_IntEntry(vEdge1, iObj) == 0 )
        Vec_IntWriteEntry(vEdge1, iObj, iNext);
    else if ( Vec_IntEntry(vEdge2, iObj) == 0 )
        Vec_IntWriteEntry(vEdge2, iObj, iNext);
41 42 43 44 45 46 47 48 49 50 51 52 53 54
    else RetValue = 1;
    return RetValue;
}
static inline void Gia_ObjEdgeRemove( int iObj, int iNext, Vec_Int_t * vEdge1, Vec_Int_t * vEdge2 )
{
    assert( Vec_IntEntry(vEdge1, iObj) == iNext || Vec_IntEntry(vEdge2, iObj) == iNext );
    if ( Vec_IntEntry(vEdge1, iObj) == iNext )
        Vec_IntWriteEntry( vEdge1, iObj, Vec_IntEntry(vEdge2, iObj) );
    Vec_IntWriteEntry( vEdge2, iObj, 0 );
}
static inline void Gia_ObjEdgeClean( int iObj, Vec_Int_t * vEdge1, Vec_Int_t * vEdge2 )
{
    Vec_IntWriteEntry( vEdge1, iObj, 0 );
    Vec_IntWriteEntry( vEdge2, iObj, 0 );
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
}

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

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

  Synopsis    [Transforms edge assignment.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Gia_ManEdgeFromArray( Gia_Man_t * p, Vec_Int_t * vArray )
{
74
    int i, iObj1, iObj2, Count = 0;
75
    Vec_IntFreeP( &p->vEdge1 );
76
    Vec_IntFreeP( &p->vEdge2 );
77 78 79 80
    p->vEdge1 = Vec_IntStart( Gia_ManObjNum(p) );
    p->vEdge2 = Vec_IntStart( Gia_ManObjNum(p) );
    Vec_IntForEachEntryDouble( vArray, iObj1, iObj2, i )
    {
81
        assert( iObj1 < iObj2 );
82 83
        Count += Gia_ObjEdgeAdd( iObj1, iObj2, p->vEdge1, p->vEdge2 );
        Count += Gia_ObjEdgeAdd( iObj2, iObj1, p->vEdge1, p->vEdge2 );
84
    }
85 86
    if ( Count ) 
        printf( "Found %d violations during edge conversion.\n", Count );
87 88 89
}
Vec_Int_t * Gia_ManEdgeToArray( Gia_Man_t * p )
{
90
    int iObj, iFanin;
91 92 93 94
    Vec_Int_t * vArray = Vec_IntAlloc( 1000 );
    assert( p->vEdge1 && p->vEdge2 );
    assert( Vec_IntSize(p->vEdge1) == Gia_ManObjNum(p) );
    assert( Vec_IntSize(p->vEdge2) == Gia_ManObjNum(p) );
95
    for ( iObj = 0; iObj < Gia_ManObjNum(p); iObj++ )
96
    {
97 98 99 100 101 102
        iFanin = Vec_IntEntry( p->vEdge1, iObj );
        if ( iFanin && iFanin < iObj )
            Vec_IntPushTwo( vArray, iFanin, iObj );
        iFanin = Vec_IntEntry( p->vEdge2, iObj );
        if ( iFanin && iFanin < iObj )
            Vec_IntPushTwo( vArray, iFanin, iObj );
103 104 105 106 107 108
    }
    return vArray;
}

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

109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
  Synopsis    [Prints mapping statistics.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Gia_ManConvertPackingToEdges( Gia_Man_t * p )
{
    int i, k, Entry, nEntries, nEntries2, nNodes[4], Count = 0;
    if ( p->vPacking == NULL )
        return;
    Vec_IntFreeP( &p->vEdge1 );
    Vec_IntFreeP( &p->vEdge2 );
    p->vEdge1 = Vec_IntStart( Gia_ManObjNum(p) );
    p->vEdge2 = Vec_IntStart( Gia_ManObjNum(p) );
    // iterate through structures
    nEntries = Vec_IntEntry( p->vPacking, 0 );
    nEntries2 = 0;
    Vec_IntForEachEntryStart( p->vPacking, Entry, i, 1 )
    {
        assert( Entry > 0 && Entry < 4 );
        i++;
        for ( k = 0; k < Entry; k++, i++ )
            nNodes[k] = Vec_IntEntry(p->vPacking, i);
        i--;
        nEntries2++;
        // create edges
        if ( Entry == 2 )
        {
            Count += Gia_ObjEdgeAdd( nNodes[0], nNodes[1], p->vEdge1, p->vEdge2 );
            Count += Gia_ObjEdgeAdd( nNodes[1], nNodes[0], p->vEdge1, p->vEdge2 );
        }
        else if ( Entry == 3 )
        {
            Count += Gia_ObjEdgeAdd( nNodes[0], nNodes[2], p->vEdge1, p->vEdge2 );
            Count += Gia_ObjEdgeAdd( nNodes[2], nNodes[0], p->vEdge1, p->vEdge2 );
            Count += Gia_ObjEdgeAdd( nNodes[1], nNodes[2], p->vEdge1, p->vEdge2 );
            Count += Gia_ObjEdgeAdd( nNodes[2], nNodes[1], p->vEdge1, p->vEdge2 );
        }
    }
    assert( nEntries == nEntries2 );
153 154
    if ( Count )
        printf( "Skipped %d illegal edges.\n", Count );
155 156 157 158
}

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

159 160 161 162 163 164 165 166 167 168 169 170 171
  Synopsis    [Evaluates given edge assignment.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline int Gia_ObjHaveEdge( Gia_Man_t * p, int iObj, int iNext )
{
    return Vec_IntEntry(p->vEdge1, iObj) == iNext || Vec_IntEntry(p->vEdge2, iObj) == iNext;
}
172 173 174 175
int Gia_ObjCheckEdge( Gia_Man_t * p, int iObj, int iNext )
{
    return Gia_ObjHaveEdge( p, iObj, iNext );
}
176 177
static inline int Gia_ObjEvalEdgeDelay( Gia_Man_t * p, int iObj, Vec_Int_t * vDelay )
{
178
    int nEdgeDelay = 2;
179
    int i, iFan, Delay, DelayMax = 0;
180
    if ( Gia_ManHasMapping(p) && Gia_ObjIsLut(p, iObj) )
181
    {
182 183 184
        assert( Gia_ObjLutSize(p, iObj) <= 4 );
        Gia_LutForEachFanin( p, iObj, iFan, i )
        {
185
            Delay = Vec_IntEntry(vDelay, iFan) + (Gia_ObjHaveEdge(p, iObj, iFan) ? nEdgeDelay : 10);
186 187 188 189 190 191 192 193
            DelayMax = Abc_MaxInt( DelayMax, Delay );
        }
    }
    else if ( Gia_ObjIsLut2(p, iObj) )
    {
        assert( Gia_ObjLutSize2(p, iObj) <= 4 );
        Gia_LutForEachFanin2( p, iObj, iFan, i )
        {
194
            Delay = Vec_IntEntry(vDelay, iFan) + (Gia_ObjHaveEdge(p, iObj, iFan) ? nEdgeDelay : 10);
195 196
            DelayMax = Abc_MaxInt( DelayMax, Delay );
        }
197
    }
198
    else assert( 0 );
199 200 201 202 203 204 205 206
    return DelayMax;
}
int Gia_ManEvalEdgeDelay( Gia_Man_t * p )
{
    int k, iLut, DelayMax = 0;
    assert( p->vEdge1 && p->vEdge2 );
    Vec_IntFreeP( &p->vEdgeDelay );
    p->vEdgeDelay = Vec_IntStart( Gia_ManObjNum(p) );
207
    if ( Gia_ManHasMapping(p) )
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
    {
        if ( p->pManTime != NULL && Tim_ManBoxNum((Tim_Man_t*)p->pManTime) )
        {
            Gia_Obj_t * pObj; 
            Vec_Int_t * vNodes = Gia_ManOrderWithBoxes( p );
            Tim_ManIncrementTravId( (Tim_Man_t*)p->pManTime );
            Gia_ManForEachObjVec( vNodes, p, pObj, k )
            {
                iLut = Gia_ObjId( p, pObj );
                if ( Gia_ObjIsAnd(pObj) )
                {
                    if ( Gia_ObjIsLut(p, iLut) )
                        Vec_IntWriteEntry( p->vEdgeDelay, iLut, Gia_ObjEvalEdgeDelay(p, iLut, p->vEdgeDelay) );
                }
                else if ( Gia_ObjIsCi(pObj) )
                {
                    int arrTime = Tim_ManGetCiArrival( (Tim_Man_t*)p->pManTime, Gia_ObjCioId(pObj) );
                    Vec_IntWriteEntry( p->vEdgeDelay,  iLut, arrTime );
                }
                else if ( Gia_ObjIsCo(pObj) )
                {
                    int arrTime = Vec_IntEntry( p->vEdgeDelay, Gia_ObjFaninId0(pObj, iLut) );
                    Tim_ManSetCoArrival( (Tim_Man_t*)p->pManTime, Gia_ObjCioId(pObj), arrTime );
                }
                else if ( !Gia_ObjIsConst0(pObj) ) 
                    assert( 0 );
            }
            Vec_IntFree( vNodes );
        }
        else
        {
            Gia_ManForEachLut( p, iLut )
                Vec_IntWriteEntry( p->vEdgeDelay, iLut, Gia_ObjEvalEdgeDelay(p, iLut, p->vEdgeDelay) );
        }
    }
243
    else if ( Gia_ManHasMapping2(p) )
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
    {
        if ( p->pManTime != NULL && Tim_ManBoxNum((Tim_Man_t*)p->pManTime) )
        {
            Gia_Obj_t * pObj; 
            Vec_Int_t * vNodes = Gia_ManOrderWithBoxes( p );
            Tim_ManIncrementTravId( (Tim_Man_t*)p->pManTime );
            Gia_ManForEachObjVec( vNodes, p, pObj, k )
            {
                iLut = Gia_ObjId( p, pObj );
                if ( Gia_ObjIsAnd(pObj) )
                {
                    if ( Gia_ObjIsLut2(p, iLut) )
                        Vec_IntWriteEntry( p->vEdgeDelay, iLut, Gia_ObjEvalEdgeDelay(p, iLut, p->vEdgeDelay) );
                }
                else if ( Gia_ObjIsCi(pObj) )
                {
                    int arrTime = Tim_ManGetCiArrival( (Tim_Man_t*)p->pManTime, Gia_ObjCioId(pObj) );
                    Vec_IntWriteEntry( p->vEdgeDelay,  iLut, arrTime );
                }
                else if ( Gia_ObjIsCo(pObj) )
                {
                    int arrTime = Vec_IntEntry( p->vEdgeDelay, Gia_ObjFaninId0(pObj, iLut) );
                    Tim_ManSetCoArrival( (Tim_Man_t*)p->pManTime, Gia_ObjCioId(pObj), arrTime );
                }
                else if ( !Gia_ObjIsConst0(pObj) ) 
                    assert( 0 );
            }
            Vec_IntFree( vNodes );
        }
        else
        {
            Gia_ManForEachLut2( p, iLut )
                Vec_IntWriteEntry( p->vEdgeDelay, iLut, Gia_ObjEvalEdgeDelay(p, iLut, p->vEdgeDelay) );
        }
    }
279
    else assert( 0 );
280 281 282 283
    Gia_ManForEachCoDriverId( p, iLut, k )
        DelayMax = Abc_MaxInt( DelayMax, Vec_IntEntry(p->vEdgeDelay, iLut) );
    return DelayMax;
}
284 285 286 287
int Gia_ManEvalEdgeCount( Gia_Man_t * p )
{
    return (Vec_IntCountPositive(p->vEdge1) + Vec_IntCountPositive(p->vEdge2))/2;
}
288 289 290 291 292 293 294 295 296 297 298 299 300


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

  Synopsis    [Finds edge assignment.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
301
int Gia_ObjComputeEdgeDelay( Gia_Man_t * p, int iObj, Vec_Int_t * vDelay, Vec_Int_t * vEdge1, Vec_Int_t * vEdge2, int fUseTwo )
302
{
303
    int i, iFan, Delay, Status1, Status2;
304
    int DelayMax = 0, DelayMax2 = 0, nCountMax = 0;
305 306 307
    int iFanMax1 = -1, iFanMax2 = -1;
    Vec_IntWriteEntry(vEdge1, iObj, 0);
    Vec_IntWriteEntry(vEdge2, iObj, 0);
308
    if ( Gia_ManHasMapping(p) && Gia_ObjIsLut(p, iObj) )
309
    {
310 311
        assert( Gia_ObjLutSize(p, iObj) <= 4 );
        Gia_LutForEachFanin( p, iObj, iFan, i )
312
        {
313
            Delay = Vec_IntEntry( vDelay, iFan ) + 10;
314 315
            if ( DelayMax < Delay )
            {
316
                DelayMax2 = DelayMax;
317 318 319 320 321 322 323 324
                DelayMax  = Delay;
                iFanMax1  = iFan;
                nCountMax = 1;
            }
            else if ( DelayMax == Delay )
            {
                iFanMax2  = iFan;
                nCountMax++;
325 326
                if ( !fUseTwo )
                    DelayMax2 = DelayMax;
327
            }
328 329
            else
                DelayMax2 = Abc_MaxInt( DelayMax2, Delay );
330
        }
331 332 333 334 335
    }
    else if ( Gia_ObjIsLut2(p, iObj) )
    {
        assert( Gia_ObjLutSize2(p, iObj) <= 4 );
        Gia_LutForEachFanin2( p, iObj, iFan, i )
336
        {
337
            Delay = Vec_IntEntry( vDelay, iFan ) + 10;
338 339
            if ( DelayMax < Delay )
            {
340
                DelayMax2 = DelayMax;
341 342 343 344 345 346 347 348
                DelayMax  = Delay;
                iFanMax1  = iFan;
                nCountMax = 1;
            }
            else if ( DelayMax == Delay )
            {
                iFanMax2  = iFan;
                nCountMax++;
349 350
                if ( !fUseTwo )
                    DelayMax2 = DelayMax;
351
            }
352 353
            else
                DelayMax2 = Abc_MaxInt( DelayMax2, Delay );
354 355
        }
    }
356
    else assert( 0 );
357
    assert( nCountMax > 0 );
358
    if ( DelayMax <= 10 )
359 360
    {} // skip first level
    else if ( nCountMax == 1 )
361
    {
362 363 364 365 366
        Status1 = Gia_ObjEdgeCount( iFanMax1, vEdge1, vEdge2 );
        if ( Status1 <= 1 )
        {
            Gia_ObjEdgeAdd( iFanMax1, iObj, vEdge1, vEdge2 );
            Gia_ObjEdgeAdd( iObj, iFanMax1, vEdge1, vEdge2 );
367 368 369
            DelayMax = Abc_MaxInt( DelayMax2, DelayMax - 8 );
            Vec_IntWriteEntry( vDelay, iObj, DelayMax );
            return DelayMax;
370
        }
371
    }
372
    else if ( fUseTwo && nCountMax == 2 )
373
    {
374 375 376 377 378 379 380 381
        Status1 = Gia_ObjEdgeCount( iFanMax1, vEdge1, vEdge2 );
        Status2 = Gia_ObjEdgeCount( iFanMax2, vEdge1, vEdge2 );
        if ( Status1 <= 1 && Status2 <= 1 )
        {
            Gia_ObjEdgeAdd( iFanMax1, iObj, vEdge1, vEdge2 );
            Gia_ObjEdgeAdd( iFanMax2, iObj, vEdge1, vEdge2 );
            Gia_ObjEdgeAdd( iObj, iFanMax1, vEdge1, vEdge2 );
            Gia_ObjEdgeAdd( iObj, iFanMax2, vEdge1, vEdge2 );
382 383 384
            DelayMax = Abc_MaxInt( DelayMax2, DelayMax - 8 );
            Vec_IntWriteEntry( vDelay, iObj, DelayMax );
            return DelayMax;
385
        }
386 387
    }
    Vec_IntWriteEntry( vDelay, iObj, DelayMax );
388
    return DelayMax;
389
}
390
int Gia_ManComputeEdgeDelay( Gia_Man_t * p, int fUseTwo )
391
{
392
    int k, iLut, DelayMax = 0;
393 394
    Vec_IntFreeP( &p->vEdgeDelay );
    Vec_IntFreeP( &p->vEdge1 );
395
    Vec_IntFreeP( &p->vEdge2 );
396 397 398
    p->vEdge1 = Vec_IntStart( Gia_ManObjNum(p) );
    p->vEdge2 = Vec_IntStart( Gia_ManObjNum(p) );
    p->vEdgeDelay = Vec_IntStart( Gia_ManObjNum(p) );
399
    if ( Gia_ManHasMapping(p) )
400 401 402 403 404 405 406 407 408 409 410 411
    {
        if ( p->pManTime != NULL && Tim_ManBoxNum((Tim_Man_t*)p->pManTime) )
        {
            Gia_Obj_t * pObj; 
            Vec_Int_t * vNodes = Gia_ManOrderWithBoxes( p );
            Tim_ManIncrementTravId( (Tim_Man_t*)p->pManTime );
            Gia_ManForEachObjVec( vNodes, p, pObj, k )
            {
                iLut = Gia_ObjId( p, pObj );
                if ( Gia_ObjIsAnd(pObj) )
                {
                    if ( Gia_ObjIsLut(p, iLut) )
412
                        Gia_ObjComputeEdgeDelay( p, iLut, p->vEdgeDelay, p->vEdge1, p->vEdge2, fUseTwo );
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
                }
                else if ( Gia_ObjIsCi(pObj) )
                {
                    int arrTime = Tim_ManGetCiArrival( (Tim_Man_t*)p->pManTime, Gia_ObjCioId(pObj) );
                    Vec_IntWriteEntry( p->vEdgeDelay,  iLut, arrTime );
                }
                else if ( Gia_ObjIsCo(pObj) )
                {
                    int arrTime = Vec_IntEntry( p->vEdgeDelay, Gia_ObjFaninId0(pObj, iLut) );
                    Tim_ManSetCoArrival( (Tim_Man_t*)p->pManTime, Gia_ObjCioId(pObj), arrTime );
                }
                else if ( !Gia_ObjIsConst0(pObj) ) 
                    assert( 0 );
            }
            Vec_IntFree( vNodes );
        }
        else
        {
            Gia_ManForEachLut( p, iLut )
432
                Gia_ObjComputeEdgeDelay( p, iLut, p->vEdgeDelay, p->vEdge1, p->vEdge2, fUseTwo );
433 434
        }
    }
435
    else if ( Gia_ManHasMapping2(p) )
436 437 438 439 440 441 442 443 444 445 446 447
    {
        if ( p->pManTime != NULL && Tim_ManBoxNum((Tim_Man_t*)p->pManTime) )
        {
            Gia_Obj_t * pObj; 
            Vec_Int_t * vNodes = Gia_ManOrderWithBoxes( p );
            Tim_ManIncrementTravId( (Tim_Man_t*)p->pManTime );
            Gia_ManForEachObjVec( vNodes, p, pObj, k )
            {
                iLut = Gia_ObjId( p, pObj );
                if ( Gia_ObjIsAnd(pObj) )
                {
                    if ( Gia_ObjIsLut2(p, iLut) )
448
                        Gia_ObjComputeEdgeDelay( p, iLut, p->vEdgeDelay, p->vEdge1, p->vEdge2, fUseTwo );
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
                }
                else if ( Gia_ObjIsCi(pObj) )
                {
                    int arrTime = Tim_ManGetCiArrival( (Tim_Man_t*)p->pManTime, Gia_ObjCioId(pObj) );
                    Vec_IntWriteEntry( p->vEdgeDelay,  iLut, arrTime );
                }
                else if ( Gia_ObjIsCo(pObj) )
                {
                    int arrTime = Vec_IntEntry( p->vEdgeDelay, Gia_ObjFaninId0(pObj, iLut) );
                    Tim_ManSetCoArrival( (Tim_Man_t*)p->pManTime, Gia_ObjCioId(pObj), arrTime );
                }
                else if ( !Gia_ObjIsConst0(pObj) ) 
                    assert( 0 );
            }
            Vec_IntFree( vNodes );
        }
        else
        {
            Gia_ManForEachLut2( p, iLut )
468
                Gia_ObjComputeEdgeDelay( p, iLut, p->vEdgeDelay, p->vEdge1, p->vEdge2, fUseTwo );
469 470
        }
    }
471
    else assert( 0 );
472 473
    Gia_ManForEachCoDriverId( p, iLut, k )
        DelayMax = Abc_MaxInt( DelayMax, Vec_IntEntry(p->vEdgeDelay, iLut) );
474
    //printf( "The number of edges = %d.  Delay = %d.\n", Gia_ManEvalEdgeCount(p), DelayMax );
475 476 477 478 479
    return DelayMax;
}

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

480
  Synopsis    [Finds edge assignment.]
481 482 483 484 485 486 487 488

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
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 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567
int Gia_ObjComputeEdgeDelay2( Gia_Man_t * p, int iObj, Vec_Int_t * vDelay, Vec_Int_t * vEdge1, Vec_Int_t * vEdge2, Vec_Int_t * vFanMax1, Vec_Int_t * vFanMax2, Vec_Int_t * vCountMax )
{
    int i, iFan, DelayFanin, Status1, Status2;
    int DelayMax = 0, nCountMax = 0;
    int iFanMax1 = -1, iFanMax2 = -1;
    Vec_IntWriteEntry(vEdge1, iObj, 0);
    Vec_IntWriteEntry(vEdge2, iObj, 0);
    // analyze this node
    DelayMax = Vec_IntEntry( vDelay, iObj );
    nCountMax = Vec_IntEntry( vCountMax, iObj );
    if ( DelayMax == 0 )
    {}
    else if ( nCountMax == 1 )
    {
        iFanMax1 = Vec_IntEntry( vFanMax1, iObj );
        Status1 = Gia_ObjEdgeCount( iFanMax1, vEdge1, vEdge2 );
        if ( Status1 <= 1 )
        {
            Gia_ObjEdgeAdd( iFanMax1, iObj, vEdge1, vEdge2 );
            Gia_ObjEdgeAdd( iObj, iFanMax1, vEdge1, vEdge2 );
            DelayMax--;
        }
    }
    else if ( nCountMax == 2 )
    {
        iFanMax1 = Vec_IntEntry( vFanMax1, iObj );
        iFanMax2 = Vec_IntEntry( vFanMax2, iObj );
        Status1 = Gia_ObjEdgeCount( iFanMax1, vEdge1, vEdge2 );
        Status2 = Gia_ObjEdgeCount( iFanMax2, vEdge1, vEdge2 );
        if ( Status1 <= 1 && Status2 <= 1 )
        {
            Gia_ObjEdgeAdd( iFanMax1, iObj, vEdge1, vEdge2 );
            Gia_ObjEdgeAdd( iFanMax2, iObj, vEdge1, vEdge2 );
            Gia_ObjEdgeAdd( iObj, iFanMax1, vEdge1, vEdge2 );
            Gia_ObjEdgeAdd( iObj, iFanMax2, vEdge1, vEdge2 );
            DelayMax--;
        }
    }
    Vec_IntWriteEntry( vDelay, iObj, DelayMax );
    // computed DelayMax at this point
    if ( Gia_ManHasMapping(p) && Gia_ObjIsLut(p, iObj) )
    {
        Gia_LutForEachFanin( p, iObj, iFan, i )
        {
            DelayFanin = Vec_IntEntry( vDelay, iFan );
            if ( DelayFanin < DelayMax + 1 )
            {
                Vec_IntWriteEntry( vDelay, iFan, DelayMax + 1 );
                Vec_IntWriteEntry( vFanMax1, iFan, iObj );
                Vec_IntWriteEntry( vCountMax, iFan, 1 );
            }
            else if ( DelayFanin == DelayMax + 1 )
            {
                Vec_IntWriteEntry( vFanMax2, iFan, iObj );
                Vec_IntAddToEntry( vCountMax, iFan, 1 );
            }
        }
    }
    else if ( Gia_ObjIsLut2(p, iObj) )
    {
        Gia_LutForEachFanin2( p, iObj, iFan, i )
        {
            DelayFanin = Vec_IntEntry( vDelay, iFan );
            if ( DelayFanin < DelayMax + 1 )
            {
                Vec_IntWriteEntry( vDelay, iFan, DelayMax + 1 );
                Vec_IntWriteEntry( vFanMax1, iFan, iObj );
                Vec_IntWriteEntry( vCountMax, iFan, 1 );
            }
            else if ( DelayFanin == DelayMax + 1 )
            {
                Vec_IntWriteEntry( vFanMax2, iFan, iObj );
                Vec_IntAddToEntry( vCountMax, iFan, 1 );
            }
        }
    }
    else assert( 0 );
    return DelayMax;
}
568 569
int Gia_ManComputeEdgeDelay2( Gia_Man_t * p )
{
570
    int k, iLut, DelayMax = 0;
571 572 573
    Vec_Int_t * vFanMax1 = Vec_IntStart( Gia_ManObjNum(p) );
    Vec_Int_t * vFanMax2 = Vec_IntStart( Gia_ManObjNum(p) );
    Vec_Int_t * vCountMax = Vec_IntStart( Gia_ManObjNum(p) );
574
    assert( p->pManTime == NULL );
575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615
    Vec_IntFreeP( &p->vEdgeDelay );
    Vec_IntFreeP( &p->vEdge1 );
    Vec_IntFreeP( &p->vEdge2 );
    p->vEdgeDelay = Vec_IntStart( Gia_ManObjNum(p) );
    p->vEdge1 = Vec_IntStart( Gia_ManObjNum(p) );
    p->vEdge2 = Vec_IntStart( Gia_ManObjNum(p) );
//    Gia_ManForEachCoDriverId( p, iLut, k )
//        Vec_IntWriteEntry( p->vEdgeDelay, iLut, 1 );
    if ( Gia_ManHasMapping(p) )
        Gia_ManForEachLutReverse( p, iLut )
            Gia_ObjComputeEdgeDelay2( p, iLut, p->vEdgeDelay, p->vEdge1, p->vEdge2, vFanMax1, vFanMax2, vCountMax );
    else if ( Gia_ManHasMapping2(p) )
        Gia_ManForEachLut2Reverse( p, iLut )
            Gia_ObjComputeEdgeDelay2( p, iLut, p->vEdgeDelay, p->vEdge1, p->vEdge2, vFanMax1, vFanMax2, vCountMax );
    else assert( 0 );
    Gia_ManForEachCiId( p, iLut, k )
        DelayMax = Abc_MaxInt( DelayMax, Vec_IntEntry(p->vEdgeDelay, iLut) );
    Vec_IntFree( vFanMax1 );
    Vec_IntFree( vFanMax2 );
    Vec_IntFree( vCountMax );
    //printf( "The number of edges = %d.  Delay = %d.\n", Gia_ManEvalEdgeCount(p), DelayMax );
    return DelayMax;
}

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

  Synopsis    [Finds edge assignment.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Gia_ManUpdateMapping( Gia_Man_t * p, Vec_Int_t * vNodes, Vec_Wec_t * vWin )
{
    int i, iNode;
    Vec_IntForEachEntry( vNodes, iNode, i )
        ABC_SWAP( Vec_Int_t, *Vec_WecEntry(p->vMapping2, iNode), *Vec_WecEntry(vWin, i) );
}
616
int Gia_ManEvalWindowInc( Gia_Man_t * p, Vec_Int_t * vLeaves, Vec_Int_t * vNodes, Vec_Wec_t * vWin, Vec_Int_t * vTemp, int fUseTwo )
617 618 619 620 621 622 623 624 625 626
{
    int i, iLut, Delay, DelayMax = 0;
    assert( Vec_IntSize(vNodes) == Vec_WecSize(vWin) );
    Gia_ManUpdateMapping( p, vNodes, vWin );
    Gia_ManCollectTfo( p, vLeaves, vTemp );
    Vec_IntReverseOrder( vTemp );
    Vec_IntForEachEntry( vTemp, iLut, i )
    {
        if ( !Gia_ObjIsLut(p, iLut) )
            continue;
627
        Delay = Gia_ObjComputeEdgeDelay( p, iLut, p->vEdgeDelay, p->vEdge1, p->vEdge2, fUseTwo );
628 629 630 631 632
        DelayMax = Abc_MaxInt( DelayMax, Delay );
    }
    Gia_ManUpdateMapping( p, vNodes, vWin );
    return DelayMax;
}
633
int Gia_ManEvalWindow( Gia_Man_t * p, Vec_Int_t * vLeaves, Vec_Int_t * vNodes, Vec_Wec_t * vWin, Vec_Int_t * vTemp, int fUseTwo )
634 635 636 637
{
    int DelayMax;
    assert( Vec_IntSize(vNodes) == Vec_WecSize(vWin) );
    Gia_ManUpdateMapping( p, vNodes, vWin );
638
    DelayMax = Gia_ManComputeEdgeDelay( p, fUseTwo );
639 640
    Gia_ManUpdateMapping( p, vNodes, vWin );
    return DelayMax;
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 698 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 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 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 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 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 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 984 985 986 987 988 989 990 991 992 993



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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Edg_ManToMapping( Gia_Man_t * p )
{
    int iObj, iFanin, k;
    assert( Gia_ManHasMapping(p) );
    Vec_WecFreeP( &p->vMapping2 );
    Vec_WecFreeP( &p->vFanouts2 );
    p->vMapping2 = Vec_WecStart( Gia_ManObjNum(p) );
    p->vFanouts2 = Vec_WecStart( Gia_ManObjNum(p) );
    Gia_ManForEachLut( p, iObj )
    {
        assert( Gia_ObjLutSize(p, iObj) <= 4 );
        Gia_LutForEachFanin( p, iObj, iFanin, k )
        {
            Vec_WecPush( p->vMapping2, iObj, iFanin );
            Vec_WecPush( p->vFanouts2, iFanin, iObj );
        }
    }
}

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

  Synopsis    [Computes delay for the given edge assignment.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline int Edg_ObjEvalEdgeDelay( Gia_Man_t * p, int iObj, Vec_Int_t * vDelay )
{
    int DelayEdge = 0; // 2;
    int DelayNoEdge = 1;
    int i, iFan, Delay, DelayMax = 0;
    assert( Gia_ObjIsLut2(p, iObj) );
    Gia_LutForEachFanin2( p, iObj, iFan, i )
    {
        Delay = Vec_IntEntry(vDelay, iFan) + (Gia_ObjHaveEdge(p, iObj, iFan) ? DelayEdge : DelayNoEdge);
        DelayMax = Abc_MaxInt( DelayMax, Delay );
    }
    //printf( "Obj %d - Level %d\n", iObj, DelayMax );
    return DelayMax;
}
int Edg_ManEvalEdgeDelay( Gia_Man_t * p )
{
    int iLut, Delay, DelayMax = 0;
    assert( p->vEdge1 && p->vEdge2 );
    if ( p->vEdgeDelay == NULL )
        p->vEdgeDelay = Vec_IntStart( Gia_ManObjNum(p) );
    else
        Vec_IntFill( p->vEdgeDelay, Gia_ManObjNum(p), 0 );
    Gia_ManForEachLut2( p, iLut )
    {
        Delay = Edg_ObjEvalEdgeDelay(p, iLut, p->vEdgeDelay);
        Vec_IntWriteEntry( p->vEdgeDelay, iLut, Delay );
        DelayMax = Abc_MaxInt( DelayMax, Delay );
    }
    return DelayMax;
}

static inline int Edg_ObjEvalEdgeDelayR( Gia_Man_t * p, int iObj, Vec_Int_t * vDelay )
{
    int DelayEdge = 0; // 2;
    int DelayNoEdge = 1;
    int i, iFan, Delay, DelayMax = 0;
    assert( Gia_ObjIsLut2(p, iObj) );
    Gia_LutForEachFanout2( p, iObj, iFan, i )
    {
        Delay = Vec_IntEntry(vDelay, iFan) + (Gia_ObjHaveEdge(p, iObj, iFan) ? DelayEdge : DelayNoEdge);
        DelayMax = Abc_MaxInt( DelayMax, Delay );
    }
    //printf( "Obj %d - LevelR %d\n", iObj, DelayMax );
    return DelayMax;
}
int Edg_ManEvalEdgeDelayR( Gia_Man_t * p )
{
//    int k, DelayNoEdge = 1;
    int iLut, Delay, DelayMax = 0;
    assert( p->vEdge1 && p->vEdge2 );
    if ( p->vEdgeDelayR == NULL )
        p->vEdgeDelayR = Vec_IntStart( Gia_ManObjNum(p) );
    else
        Vec_IntFill( p->vEdgeDelayR, Gia_ManObjNum(p), 0 );
//    Gia_ManForEachCoDriverId( p, iLut, k )
//        Vec_IntWriteEntry( p->vEdgeDelayR, iLut, DelayNoEdge );
    Gia_ManForEachLut2Reverse( p, iLut )
    {
        Delay = Edg_ObjEvalEdgeDelayR(p, iLut, p->vEdgeDelayR);
        Vec_IntWriteEntry( p->vEdgeDelayR, iLut, Delay );
        DelayMax = Abc_MaxInt( DelayMax, Delay );
    }
    return DelayMax;
}

void Edg_ManCollectCritEdges( Gia_Man_t * p, Vec_Wec_t * vEdges, int DelayMax )
{
    Vec_Int_t * vLevel;
    int k, iLut, Delay1, Delay2;
    assert( p->vEdge1 && p->vEdge2 );
    Vec_WecClear( vEdges );
    Vec_WecInit( vEdges, DelayMax + 1 );
    Gia_ManForEachLut2( p, iLut )
    {
        Delay1 = Vec_IntEntry( p->vEdgeDelay, iLut );
        Delay2 = Vec_IntEntry( p->vEdgeDelayR, iLut );
        assert( Delay1 + Delay2 <= DelayMax );
        if ( Delay1 + Delay2 == DelayMax )
            Vec_WecPush( vEdges, Delay1, iLut );
    }
    // every level should have critical nodes, except the first one
    //Vec_WecPrint( vEdges, 0 );
    Vec_WecForEachLevelStart( vEdges, vLevel, k, 1 )
        assert( Vec_IntSize(vLevel) > 0 );
}


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

  Synopsis    [Update one node.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Edg_ObjImprove( Gia_Man_t * p, int iObj, int nEdgeLimit, int DelayMax, int fVerbose )
{
    int nFaninsC = 0,   nFanoutsC = 0;    // critical
    int nFaninsEC = 0,  nFanoutsEC = 0;   // edge-critical
    int nFaninsENC = 0, nFanoutsENC = 0;  // edge-non-critial
    int pFanins[4], pFanouts[4];
    int nEdgeDiff, nEdges = 0, Count = 0;
    int i, iNext, Delay1, Delay2;
    // count how many fanins have critical edge
    Delay1 = Vec_IntEntry( p->vEdgeDelayR, iObj );
    //if ( Delay1 > 1 )
    Gia_LutForEachFanin2( p, iObj, iNext, i )
    {
        if ( !Gia_ObjIsAnd(Gia_ManObj(p, iNext)) )
            continue;
        Delay2 = Vec_IntEntry( p->vEdgeDelay, iNext );
        if ( Gia_ObjHaveEdge(p, iObj, iNext) )
        {
            nEdges++;
            assert( Delay1 + Delay2 <= DelayMax );
            if ( Delay1 + Delay2 == DelayMax )
                nFaninsEC++;
            else
                nFaninsENC++;
        }
        else
        {
            assert( Delay1 + Delay2 + 1 <= DelayMax );
            if ( Delay1 + Delay2 + 1 == DelayMax )
                pFanins[nFaninsC++] = iNext;
        }
    }
    // count how many fanouts have critical edge
    Delay1 = Vec_IntEntry( p->vEdgeDelay, iObj );
    //if ( Delay2 < DelayMax - 1 )
    Gia_LutForEachFanout2( p, iObj, iNext, i )
    {
        //if ( !Gia_ObjIsAnd(Gia_ManObj(p, iNext)) )
        //    continue;
        assert( Gia_ObjIsAnd(Gia_ManObj(p, iNext)) );
        Delay2 = Vec_IntEntry( p->vEdgeDelayR, iNext );
        if ( Gia_ObjHaveEdge(p, iObj, iNext) )
        {
            nEdges++;
            assert( Delay1 + Delay2 <= DelayMax );
            if ( Delay1 + Delay2 == DelayMax )
                nFanoutsEC++;
            else
                nFanoutsENC++;
        }
        else
        {
            assert( Delay1 + Delay2 + 1 <= DelayMax );
            if ( Delay1 + Delay2 + 1 == DelayMax )
            {
                if ( nFanoutsC < nEdgeLimit )
                    pFanouts[nFanoutsC] = iNext;
                nFanoutsC++;
            }
        }
    }
    if ( fVerbose )
    {
        printf( "%8d : ", iObj );
        printf( "Edges = %d  ", nEdges );
        printf( "Fanins (all %d  EC %d  ENC %d  C %d)  ", 
            Gia_ObjLutSize2(p, iObj), nFaninsEC, nFaninsENC, nFaninsC );
        printf( "Fanouts (all %d  EC %d  ENC %d  C %d)  ", 
            Gia_ObjLutFanoutNum2(p, iObj), nFanoutsEC, nFanoutsENC, nFanoutsC );
    }
    // consider simple cases
    assert( nEdges <= nEdgeLimit );
    if ( nEdges == nEdgeLimit )
    {
        if ( fVerbose )
            printf( "Full\n" );
        return 0;
    }
    nEdgeDiff = nEdgeLimit - nEdges;
    // check if fanins or fanouts could be improved
    if ( nFaninsEC == 0 && nFaninsC && nFaninsC <= nEdgeDiff )
    {
        for ( i = 0; i < nFaninsC; i++ )
            if ( Gia_ObjEdgeCount(pFanins[i], p->vEdge1, p->vEdge2) == nEdgeLimit )
                break;
        if ( i == nFaninsC )
        {
            for ( i = 0; i < nFaninsC; i++ )
            {
                Count += Gia_ObjEdgeAdd( iObj, pFanins[i], p->vEdge1, p->vEdge2 );
                Count += Gia_ObjEdgeAdd( pFanins[i], iObj, p->vEdge1, p->vEdge2 );
            }
            if ( Count )
                printf( "Wrong number of edges.\n" );
            if ( fVerbose )
                printf( "Fixed %d critical fanins\n", nFaninsC );
            return 1;
        }
    }
    if ( nFanoutsEC == 0 && nFanoutsC && nFanoutsC <= nEdgeDiff )
    {
        for ( i = 0; i < nFanoutsC; i++ )
            if ( Gia_ObjEdgeCount(pFanouts[i], p->vEdge1, p->vEdge2) == nEdgeLimit )
                break;
        if ( i == nFanoutsC )
        {
            for ( i = 0; i < nFanoutsC; i++ )
            {
                Count += Gia_ObjEdgeAdd( iObj, pFanouts[i], p->vEdge1, p->vEdge2 );
                Count += Gia_ObjEdgeAdd( pFanouts[i], iObj, p->vEdge1, p->vEdge2 );
            }
            if ( Count )
                printf( "Wrong number of edges.\n" );
            if ( fVerbose )
                printf( "Fixed %d critical fanouts\n", nFanoutsC );
            return 1;
        }
    }
    if ( fVerbose )
        printf( "Cannot fix\n" );
    return 0;
}

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

  Synopsis    [Finds edge assignment.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Edg_ManAssignEdgeNew( Gia_Man_t * p, int nEdges, int fVerbose )
{
    int DelayNoEdge = 1;
    int fLevelVerbose = 0;
    Vec_Int_t * vLevel;
    Vec_Wec_t * vEdges = Vec_WecStart(0);
    Vec_Int_t * vEdge1 = NULL, * vEdge2 = NULL;
    int DelayD = 0, DelayR = 0, DelayPrev = ABC_INFINITY;
    int k, j, i, iLast = -1, iObj;
    if ( fVerbose )
        printf( "Running edge assignment with E = %d.\n", nEdges );
    // create fanouts
    Edg_ManToMapping( p );
    // create empty assignment
    Vec_IntFreeP( &p->vEdge1 );
    Vec_IntFreeP( &p->vEdge2 );
    p->vEdge1 = Vec_IntStart( Gia_ManObjNum(p) );
    p->vEdge2 = Vec_IntStart( Gia_ManObjNum(p) );
    // perform optimization
    for ( i = 0; i < 10000; i++ )
    {
        // if there is no improvement after 10 iterations, quit
        if ( i > iLast + 50 )
            break;
        // create delay information
        DelayD = Edg_ManEvalEdgeDelay( p );
        DelayR = Edg_ManEvalEdgeDelayR( p );
        assert( DelayD == DelayR + DelayNoEdge );
        if ( DelayPrev > DelayD )
        {
            //printf( "Saving backup point at %d levels.\n", DelayD );
            Vec_IntFreeP( &vEdge1 );  vEdge1 = Vec_IntDup( p->vEdge1 );
            Vec_IntFreeP( &vEdge2 );  vEdge2 = Vec_IntDup( p->vEdge2 );
            DelayPrev = DelayD;
            iLast = i;
        }
        if ( fVerbose )
            printf( "\nIter %4d : Delay = %4d\n", i, DelayD );
        // collect critical nodes (nodes with critical edges)
        Edg_ManCollectCritEdges( p, vEdges, DelayD );
        // sort levels according to the number of critical edges
        if ( fLevelVerbose )
        {
            Vec_WecForEachLevel( vEdges, vLevel, k )
                Vec_IntPush( vLevel, k );
        }
        Vec_WecSort( vEdges, 0 );
        if ( fLevelVerbose )
        {
            Vec_WecForEachLevel( vEdges, vLevel, k )
            {
                int Level = Vec_IntPop( vLevel );
                printf( "%d: Level %2d : ", k, Level );
                Vec_IntPrint( vLevel );
            }
        }
        Vec_WecForEachLevel( vEdges, vLevel, k )
        {
            Vec_IntForEachEntry( vLevel, iObj, j )
                if ( Edg_ObjImprove(p, iObj, nEdges, DelayD, fVerbose) ) // improved
                    break;
            if ( j < Vec_IntSize(vLevel) )
                break;
        }
        if ( k == Vec_WecSize(vEdges) ) // if we could not improve anything, quit
            break;
    }
    Vec_WecFree( vEdges );
    // update to the saved version
    Vec_IntFreeP( &p->vEdge1 );  p->vEdge1 = vEdge1;
    Vec_IntFreeP( &p->vEdge2 );  p->vEdge2 = vEdge2;
    return DelayD;
}


994 995 996 997 998 999 1000
////////////////////////////////////////////////////////////////////////
///                       END OF FILE                                ///
////////////////////////////////////////////////////////////////////////


ABC_NAMESPACE_IMPL_END