darCut.c 22.9 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
/**CFile****************************************************************

  FileName    [darCut.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [DAG-aware AIG rewriting.]

  Synopsis    [Computation of 4-input cuts.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

  Date        [Ver. 1.0. Started - April 28, 2007.]

  Revision    [$Id: darCut.c,v 1.00 2007/04/28 00:00:00 alanmi Exp $]

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

Alan Mishchenko committed
21
#include "darInt.h"
Alan Mishchenko committed
22

23 24 25
ABC_NAMESPACE_IMPL_START


Alan Mishchenko committed
26 27 28 29 30 31 32 33 34 35
////////////////////////////////////////////////////////////////////////
///                        DECLARATIONS                              ///
////////////////////////////////////////////////////////////////////////

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

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

Alan Mishchenko committed
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
  Synopsis    [Prints one cut.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Dar_CutPrint( Dar_Cut_t * pCut )
{
    unsigned i;
    printf( "{" );
    for ( i = 0; i < pCut->nLeaves; i++ )
        printf( " %d", pCut->pLeaves[i] );
    printf( " }\n" );
}

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

  Synopsis    [Prints one cut.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Dar_ObjCutPrint( Aig_Man_t * p, Aig_Obj_t * pObj )
{
    Dar_Cut_t * pCut;
    int i;
    printf( "Cuts for node %d:\n", pObj->Id );
    Dar_ObjForEachCut( pObj, pCut, i )
        Dar_CutPrint( pCut );
//    printf( "\n" );
}

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

Alan Mishchenko committed
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
  Synopsis    [Returns the number of 1s in the machine word.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline int Dar_WordCountOnes( unsigned uWord )
{
    uWord = (uWord & 0x55555555) + ((uWord>>1) & 0x55555555);
    uWord = (uWord & 0x33333333) + ((uWord>>2) & 0x33333333);
    uWord = (uWord & 0x0F0F0F0F) + ((uWord>>4) & 0x0F0F0F0F);
    uWord = (uWord & 0x00FF00FF) + ((uWord>>8) & 0x00FF00FF);
    return  (uWord & 0x0000FFFF) + (uWord>>16);
}

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

  Synopsis    [Compute the cost of the cut.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline int Dar_CutFindValue( Dar_Man_t * p, Dar_Cut_t * pCut )
{
    Aig_Obj_t * pLeaf;
Alan Mishchenko committed
109
    int i, Value, nOnes;
Alan Mishchenko committed
110 111
    assert( pCut->fUsed );
    Value = 0;
Alan Mishchenko committed
112
    nOnes = 0;
Alan Mishchenko committed
113 114 115 116 117 118
    Dar_CutForEachLeaf( p->pAig, pCut, pLeaf, i )
    {
        if ( pLeaf == NULL )
            return 0;
        assert( pLeaf != NULL );
        Value += pLeaf->nRefs;
Alan Mishchenko committed
119
        nOnes += (pLeaf->nRefs == 1);
Alan Mishchenko committed
120
    }
Alan Mishchenko committed
121 122 123
    if ( pCut->nLeaves < 2 )
        return 1001;
//    Value = Value * 100 / pCut->nLeaves;
Alan Mishchenko committed
124 125
    if ( Value > 1000 )
        Value = 1000;
Alan Mishchenko committed
126 127
    if ( nOnes > 3 )
        Value = 5 - nOnes;
Alan Mishchenko committed
128 129 130 131 132
    return Value;
}

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

133
  Synopsis    [Returns the next free cut to use.]
Alan Mishchenko committed
134

Alan Mishchenko committed
135
  Description [Uses the cut with the smallest value.]
Alan Mishchenko committed
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline Dar_Cut_t * Dar_CutFindFree( Dar_Man_t * p, Aig_Obj_t * pObj )
{
    Dar_Cut_t * pCut, * pCutMax;
    int i;
    pCutMax = NULL;
    Dar_ObjForEachCutAll( pObj, pCut, i )
    {
        if ( pCut->fUsed == 0 )
            return pCut;
        if ( pCut->nLeaves < 3 )
            continue;
        if ( pCutMax == NULL || pCutMax->Value > pCut->Value )
            pCutMax = pCut;
    }
    if ( pCutMax == NULL )
    {
        Dar_ObjForEachCutAll( pObj, pCut, i )
        {
            if ( pCut->nLeaves < 2 )
                continue;
            if ( pCutMax == NULL || pCutMax->Value > pCut->Value )
                pCutMax = pCut;
        }
    }
Alan Mishchenko committed
166 167 168 169 170 171 172 173
    if ( pCutMax == NULL )
    {
        Dar_ObjForEachCutAll( pObj, pCut, i )
        {
            if ( pCutMax == NULL || pCutMax->Value > pCut->Value )
                pCutMax = pCut;
        }
    }
Alan Mishchenko committed
174 175 176 177 178 179 180
    assert( pCutMax != NULL );
    pCutMax->fUsed = 0;
    return pCutMax;
}

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

Alan Mishchenko committed
181 182 183 184 185 186 187 188 189 190 191 192
  Synopsis    [Returns 1 if pDom is contained in pCut.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline int Dar_CutCheckDominance( Dar_Cut_t * pDom, Dar_Cut_t * pCut )
{
    int i, k;
Alan Mishchenko committed
193
    assert( pDom->fUsed && pCut->fUsed );
Alan Mishchenko committed
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
    for ( i = 0; i < (int)pDom->nLeaves; i++ )
    {
        for ( k = 0; k < (int)pCut->nLeaves; k++ )
            if ( pDom->pLeaves[i] == pCut->pLeaves[k] )
                break;
        if ( k == (int)pCut->nLeaves ) // node i in pDom is not contained in pCut
            return 0;
    }
    // every node in pDom is contained in pCut
    return 1;
}

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

  Synopsis    [Returns 1 if the cut is contained.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
217
static inline int Dar_CutFilter( Aig_Obj_t * pObj, Dar_Cut_t * pCut )
Alan Mishchenko committed
218 219
{ 
    Dar_Cut_t * pTemp;
Alan Mishchenko committed
220 221 222 223
    int i;
    assert( pCut->fUsed );
    // go through the cuts of the node
    Dar_ObjForEachCut( pObj, pTemp, i )
Alan Mishchenko committed
224
    {
Alan Mishchenko committed
225 226
        if ( pTemp == pCut )
            continue;
Alan Mishchenko committed
227 228 229 230 231 232 233 234 235
        if ( pTemp->nLeaves > pCut->nLeaves )
        {
            // skip the non-contained cuts
            if ( (pTemp->uSign & pCut->uSign) != pCut->uSign )
                continue;
            // check containment seriously
            if ( Dar_CutCheckDominance( pCut, pTemp ) )
            {
                // remove contained cut
Alan Mishchenko committed
236
                pTemp->fUsed = 0;
Alan Mishchenko committed
237 238 239 240 241 242 243 244 245
            }
         }
        else
        {
            // skip the non-contained cuts
            if ( (pTemp->uSign & pCut->uSign) != pTemp->uSign )
                continue;
            // check containment seriously
            if ( Dar_CutCheckDominance( pTemp, pCut ) )
Alan Mishchenko committed
246
            {
Alan Mishchenko committed
247 248
                // remove the given cut
                pCut->fUsed = 0;
Alan Mishchenko committed
249
                return 1;
Alan Mishchenko committed
250
            }
Alan Mishchenko committed
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
        }
    }
    return 0;
}

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

  Synopsis    [Merges two cuts.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline int Dar_CutMergeOrdered( Dar_Cut_t * pC, Dar_Cut_t * pC0, Dar_Cut_t * pC1 )
{ 
    int i, k, c;
    assert( pC0->nLeaves >= pC1->nLeaves );

    // the case of the largest cut sizes
    if ( pC0->nLeaves == 4 && pC1->nLeaves == 4 )
    {
        if ( pC0->uSign != pC1->uSign )
            return 0;
        for ( i = 0; i < (int)pC0->nLeaves; i++ )
            if ( pC0->pLeaves[i] != pC1->pLeaves[i] )
                return 0;
        for ( i = 0; i < (int)pC0->nLeaves; i++ )
            pC->pLeaves[i] = pC0->pLeaves[i];
        pC->nLeaves = pC0->nLeaves;
        return 1;
    }

    // the case when one of the cuts is the largest
    if ( pC0->nLeaves == 4 )
    {
        if ( (pC0->uSign & pC1->uSign) != pC1->uSign )
            return 0;
        for ( i = 0; i < (int)pC1->nLeaves; i++ )
        {
            for ( k = (int)pC0->nLeaves - 1; k >= 0; k-- )
                if ( pC0->pLeaves[k] == pC1->pLeaves[i] )
                    break;
            if ( k == -1 ) // did not find
                return 0;
        }
        for ( i = 0; i < (int)pC0->nLeaves; i++ )
            pC->pLeaves[i] = pC0->pLeaves[i];
        pC->nLeaves = pC0->nLeaves;
        return 1;
    }

    // compare two cuts with different numbers
    i = k = 0;
    for ( c = 0; c < 4; c++ )
    {
        if ( k == (int)pC1->nLeaves )
        {
            if ( i == (int)pC0->nLeaves )
            {
                pC->nLeaves = c;
                return 1;
            }
            pC->pLeaves[c] = pC0->pLeaves[i++];
            continue;
        }
        if ( i == (int)pC0->nLeaves )
        {
            if ( k == (int)pC1->nLeaves )
            {
                pC->nLeaves = c;
                return 1;
            }
            pC->pLeaves[c] = pC1->pLeaves[k++];
            continue;
        }
        if ( pC0->pLeaves[i] < pC1->pLeaves[k] )
        {
            pC->pLeaves[c] = pC0->pLeaves[i++];
            continue;
        }
        if ( pC0->pLeaves[i] > pC1->pLeaves[k] )
        {
            pC->pLeaves[c] = pC1->pLeaves[k++];
            continue;
        }
        pC->pLeaves[c] = pC0->pLeaves[i++]; 
        k++;
    }
    if ( i < (int)pC0->nLeaves || k < (int)pC1->nLeaves )
        return 0;
    pC->nLeaves = c;
    return 1;
}

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

  Synopsis    [Prepares the object for FPGA mapping.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
359
static inline int Dar_CutMerge( Dar_Cut_t * pCut, Dar_Cut_t * pCut0, Dar_Cut_t * pCut1 )
Alan Mishchenko committed
360
{ 
Alan Mishchenko committed
361
    assert( !pCut->fUsed );
Alan Mishchenko committed
362
    // merge the nodes
Alan Mishchenko committed
363
    if ( pCut0->nLeaves <= pCut1->nLeaves )
Alan Mishchenko committed
364 365 366 367 368 369
    {
        if ( !Dar_CutMergeOrdered( pCut, pCut1, pCut0 ) )
            return 0;
    }
    else
    {
Alan Mishchenko committed
370
        if ( !Dar_CutMergeOrdered( pCut, pCut0, pCut1 ) )
Alan Mishchenko committed
371 372 373
            return 0;
    }
    pCut->uSign = pCut0->uSign | pCut1->uSign;
Alan Mishchenko committed
374
    pCut->fUsed = 1;
Alan Mishchenko committed
375 376 377 378 379 380 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 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
    return 1;
}


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

  Synopsis    [Computes the stretching phase of the cut w.r.t. the merged cut.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline unsigned Dar_CutTruthPhase( Dar_Cut_t * pCut, Dar_Cut_t * pCut1 )
{
    unsigned uPhase = 0;
    int i, k;
    for ( i = k = 0; i < (int)pCut->nLeaves; i++ )
    {
        if ( k == (int)pCut1->nLeaves )
            break;
        if ( pCut->pLeaves[i] < pCut1->pLeaves[k] )
            continue;
        assert( pCut->pLeaves[i] == pCut1->pLeaves[k] );
        uPhase |= (1 << i);
        k++;
    }
    return uPhase;
}

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

  Synopsis    [Swaps two advancent variables of the truth table.]

  Description [Swaps variable iVar and iVar+1.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline unsigned Dar_CutTruthSwapAdjacentVars( unsigned uTruth, int iVar )
{
    assert( iVar >= 0 && iVar <= 2 );
    if ( iVar == 0 )
        return (uTruth & 0x99999999) | ((uTruth & 0x22222222) << 1) | ((uTruth & 0x44444444) >> 1);
    if ( iVar == 1 )
        return (uTruth & 0xC3C3C3C3) | ((uTruth & 0x0C0C0C0C) << 2) | ((uTruth & 0x30303030) >> 2);
    if ( iVar == 2 )
        return (uTruth & 0xF00FF00F) | ((uTruth & 0x00F000F0) << 4) | ((uTruth & 0x0F000F00) >> 4);
    assert( 0 );
    return 0;
}

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

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
  Synopsis    [Swaps polarity of the variable.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline unsigned Dar_CutTruthSwapPolarity( unsigned uTruth, int iVar )
{
    assert( iVar >= 0 && iVar <= 3 );
    if ( iVar == 0 )
        return ((uTruth & 0xAAAA) >> 1) | ((uTruth & 0x5555) << 1);
    if ( iVar == 1 )
        return ((uTruth & 0xCCCC) >> 2) | ((uTruth & 0x3333) << 2);
    if ( iVar == 2 )
        return ((uTruth & 0xF0F0) >> 4) | ((uTruth & 0x0F0F) << 4);
    if ( iVar == 3 )
        return ((uTruth & 0xFF00) >> 8) | ((uTruth & 0x00FF) << 8);
    assert( 0 );
    return 0;
}

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

Alan Mishchenko committed
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
  Synopsis    [Expands the truth table according to the phase.]

  Description [The input and output truth tables are in pIn/pOut. The current number
  of variables is nVars. The total number of variables in nVarsAll. The last argument
  (Phase) contains shows where the variables should go.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline unsigned Dar_CutTruthStretch( unsigned uTruth, int nVars, unsigned Phase )
{
    int i, k, Var = nVars - 1;
    for ( i = 3; i >= 0; i-- )
        if ( Phase & (1 << i) )
        {
            for ( k = Var; k < i; k++ )
                uTruth = Dar_CutTruthSwapAdjacentVars( uTruth, k );
            Var--;
        }
    assert( Var == -1 );
    return uTruth;
}

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

  Synopsis    [Shrinks the truth table according to the phase.]

  Description [The input and output truth tables are in pIn/pOut. The current number
  of variables is nVars. The total number of variables in nVarsAll. The last argument
  (Phase) contains shows what variables should remain.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline unsigned Dar_CutTruthShrink( unsigned uTruth, int nVars, unsigned Phase )
{
    int i, k, Var = 0;
    for ( i = 0; i < 4; i++ )
        if ( Phase & (1 << i) )
        {
            for ( k = i-1; k >= Var; k-- )
                uTruth = Dar_CutTruthSwapAdjacentVars( uTruth, k );
            Var++;
        }
    return uTruth;
}

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

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 568 569 570
  Synopsis    [Sort variables by their ID.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
unsigned Dar_CutSortVars( unsigned uTruth, int * pVars )
{
    int i, Temp, fChange, Counter = 0;
    // replace -1 by large number
    for ( i = 0; i < 4; i++ )
    {
        if ( pVars[i] == -1 )
            pVars[i] = 0x3FFFFFFF;
        else
            if ( Abc_LitIsCompl(pVars[i]) )
        {
            pVars[i] = Abc_LitNot( pVars[i] );
            uTruth = Dar_CutTruthSwapPolarity( uTruth, i );
        }
    }

    // permute variables
    do {
        fChange = 0;
        for ( i = 0; i < 3; i++ )
        {
            if ( pVars[i] <= pVars[i+1] )
                continue;
            Counter++;
            fChange = 1;

            Temp = pVars[i];
            pVars[i] = pVars[i+1];
            pVars[i+1] = Temp;

            uTruth = Dar_CutTruthSwapAdjacentVars( uTruth, i );
        }
    } while ( fChange );

    // replace large number by -1
    for ( i = 0; i < 4; i++ )
    {
        if ( pVars[i] == 0x3FFFFFFF )
            pVars[i] = -1;
//        printf( "%d ", pVars[i] );
    }
//    printf( "\n" );

    return uTruth;
}



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

Alan Mishchenko committed
571 572 573 574 575 576 577 578 579
  Synopsis    [Performs truth table computation.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
580
static inline unsigned Dar_CutTruth( Dar_Cut_t * pCut, Dar_Cut_t * pCut0, Dar_Cut_t * pCut1, int fCompl0, int fCompl1 )
Alan Mishchenko committed
581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599
{
    unsigned uTruth0 = fCompl0 ? ~pCut0->uTruth : pCut0->uTruth;
    unsigned uTruth1 = fCompl1 ? ~pCut1->uTruth : pCut1->uTruth;
    uTruth0 = Dar_CutTruthStretch( uTruth0, pCut0->nLeaves, Dar_CutTruthPhase(pCut, pCut0) );
    uTruth1 = Dar_CutTruthStretch( uTruth1, pCut1->nLeaves, Dar_CutTruthPhase(pCut, pCut1) );
    return uTruth0 & uTruth1;
}

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

  Synopsis    [Minimize support of the cut.]

  Description [Returns 1 if the node's support has changed]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
600
static inline int Dar_CutSuppMinimize( Dar_Cut_t * pCut )
Alan Mishchenko committed
601 602 603 604 605 606 607 608 609
{
    unsigned uMasks[4][2] = {
        { 0x5555, 0xAAAA },
        { 0x3333, 0xCCCC },
        { 0x0F0F, 0xF0F0 },
        { 0x00FF, 0xFF00 }
    };
    unsigned uPhase = 0, uTruth = 0xFFFF & pCut->uTruth;
    int i, k, nLeaves;
Alan Mishchenko committed
610
    assert( pCut->fUsed );
Alan Mishchenko committed
611
    // compute the support of the cut's function
Alan Mishchenko committed
612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628
    nLeaves = pCut->nLeaves;
    for ( i = 0; i < (int)pCut->nLeaves; i++ )
        if ( (uTruth & uMasks[i][0]) == ((uTruth & uMasks[i][1]) >> (1 << i)) )
            nLeaves--;
        else
            uPhase |= (1 << i);
    if ( nLeaves == (int)pCut->nLeaves )
        return 0;
    // shrink the truth table
    uTruth = Dar_CutTruthShrink( uTruth, pCut->nLeaves, uPhase );
    pCut->uTruth = 0xFFFF & uTruth;
    // update leaves and signature
    pCut->uSign = 0;
    for ( i = k = 0; i < (int)pCut->nLeaves; i++ )
    {
        if ( !(uPhase & (1 << i)) )
            continue;    
Alan Mishchenko committed
629 630
        pCut->pLeaves[k++] = pCut->pLeaves[i];
        pCut->uSign |= Aig_ObjCutSign( pCut->pLeaves[i] );
Alan Mishchenko committed
631 632
    }
    assert( k == nLeaves );
Alan Mishchenko committed
633
    pCut->nLeaves = nLeaves;
Alan Mishchenko committed
634 635 636 637 638 639 640 641 642 643 644 645 646 647
    return 1;
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
648
void Dar_ManCutsFree( Dar_Man_t * p )
Alan Mishchenko committed
649
{
Alan Mishchenko committed
650 651 652 653 654
    if ( p->pMemCuts == NULL )
        return;
    Aig_MmFixedStop( p->pMemCuts, 0 );
    p->pMemCuts = NULL;
//    Aig_ManCleanData( p );
Alan Mishchenko committed
655 656 657 658 659 660 661 662 663 664 665 666 667
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
668
Dar_Cut_t * Dar_ObjPrepareCuts( Dar_Man_t * p, Aig_Obj_t * pObj )
Alan Mishchenko committed
669
{
Alan Mishchenko committed
670
    Dar_Cut_t * pCutSet, * pCut;
Alan Mishchenko committed
671
    int i;
Alan Mishchenko committed
672 673 674 675
    assert( Dar_ObjCuts(pObj) == NULL );
    pObj->nCuts = p->pPars->nCutsMax;
    // create the cutset of the node
    pCutSet = (Dar_Cut_t *)Aig_MmFixedEntryFetch( p->pMemCuts );
Alan Mishchenko committed
676
    memset( pCutSet, 0, p->pPars->nCutsMax * sizeof(Dar_Cut_t) );
Alan Mishchenko committed
677
    Dar_ObjSetCuts( pObj, pCutSet );
Alan Mishchenko committed
678
    Dar_ObjForEachCutAll( pObj, pCut, i )
Alan Mishchenko committed
679
        pCut->fUsed = 0;
Alan Mishchenko committed
680
    Vec_PtrPush( p->vCutNodes, pObj );
Alan Mishchenko committed
681 682 683 684
    // add unit cut if needed
    pCut = pCutSet;
    pCut->fUsed = 1;
    if ( Aig_ObjIsConst1(pObj) )
Alan Mishchenko committed
685
    {
Alan Mishchenko committed
686 687 688
        pCut->nLeaves = 0;
        pCut->uSign = 0;
        pCut->uTruth = 0xFFFF;
Alan Mishchenko committed
689
    }
Alan Mishchenko committed
690 691 692 693 694 695 696 697
    else
    {
        pCut->nLeaves = 1;
        pCut->pLeaves[0] = pObj->Id;
        pCut->uSign = Aig_ObjCutSign( pObj->Id );
        pCut->uTruth = 0xAAAA;
    }
    pCut->Value = Dar_CutFindValue( p, pCut );
Alan Mishchenko committed
698 699
    if ( p->nCutMemUsed < Aig_MmFixedReadMemUsage(p->pMemCuts)/(1<<20) )
        p->nCutMemUsed = Aig_MmFixedReadMemUsage(p->pMemCuts)/(1<<20);
Alan Mishchenko committed
700
    return pCutSet;
Alan Mishchenko committed
701 702 703 704 705 706 707 708 709 710 711 712 713
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
714
void Dar_ManCutsRestart( Dar_Man_t * p, Aig_Obj_t * pRoot )
Alan Mishchenko committed
715 716 717
{
    Aig_Obj_t * pObj;
    int i;
Alan Mishchenko committed
718
    Dar_ObjSetCuts( Aig_ManConst1(p->pAig), NULL );
719
    Vec_PtrForEachEntry( Aig_Obj_t *, p->vCutNodes, pObj, i )
Alan Mishchenko committed
720 721 722
        if ( !Aig_ObjIsNone(pObj) )
            Dar_ObjSetCuts( pObj, NULL );
    Vec_PtrClear( p->vCutNodes );
Alan Mishchenko committed
723 724 725 726 727 728 729 730 731 732 733 734 735 736 737
    Aig_MmFixedRestart( p->pMemCuts );
    Dar_ObjPrepareCuts( p, Aig_ManConst1(p->pAig) );
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
738
Dar_Cut_t * Dar_ObjComputeCuts( Dar_Man_t * p, Aig_Obj_t * pObj, int fSkipTtMin )
Alan Mishchenko committed
739
{
Alan Mishchenko committed
740 741 742 743 744
    Aig_Obj_t * pFanin0 = Aig_ObjReal_rec( Aig_ObjChild0(pObj) );
    Aig_Obj_t * pFanin1 = Aig_ObjReal_rec( Aig_ObjChild1(pObj) );
    Aig_Obj_t * pFaninR0 = Aig_Regular(pFanin0);
    Aig_Obj_t * pFaninR1 = Aig_Regular(pFanin1);
    Dar_Cut_t * pCutSet, * pCut0, * pCut1, * pCut;
745
    int i, k; 
Alan Mishchenko committed
746 747 748 749 750 751 752 753 754

    assert( !Aig_IsComplement(pObj) );
    assert( Aig_ObjIsNode(pObj) );
    assert( Dar_ObjCuts(pObj) == NULL );
    assert( Dar_ObjCuts(pFaninR0) != NULL );
    assert( Dar_ObjCuts(pFaninR1) != NULL );

    // set up the first cut
    pCutSet = Dar_ObjPrepareCuts( p, pObj );
Alan Mishchenko committed
755
    // make sure fanins cuts are computed
Alan Mishchenko committed
756 757
    Dar_ObjForEachCut( pFaninR0, pCut0, i )
    Dar_ObjForEachCut( pFaninR1, pCut1, k )
Alan Mishchenko committed
758
    {
Alan Mishchenko committed
759 760 761 762 763 764
        p->nCutsAll++;
        // make sure K-feasible cut exists
        if ( Dar_WordCountOnes(pCut0->uSign | pCut1->uSign) > 4 )
            continue;
        // get the next cut of this node
        pCut = Dar_CutFindFree( p, pObj );
Alan Mishchenko committed
765 766
        // create the new cut
        if ( !Dar_CutMerge( pCut, pCut0, pCut1 ) )
Alan Mishchenko committed
767 768
        {
            assert( !pCut->fUsed );
Alan Mishchenko committed
769
            continue;
Alan Mishchenko committed
770 771
        }
        p->nCutsTried++;
Alan Mishchenko committed
772
        // check dominance
Alan Mishchenko committed
773 774 775
        if ( Dar_CutFilter( pObj, pCut ) )
        {
            assert( !pCut->fUsed );
Alan Mishchenko committed
776
            continue;
Alan Mishchenko committed
777
        }
Alan Mishchenko committed
778
        // compute truth table
Alan Mishchenko committed
779
        pCut->uTruth = 0xFFFF & Dar_CutTruth( pCut, pCut0, pCut1, Aig_IsComplement(pFanin0), Aig_IsComplement(pFanin1) );
Alan Mishchenko committed
780

Alan Mishchenko committed
781
        // minimize support of the cut
782
        if ( !fSkipTtMin && Dar_CutSuppMinimize( pCut ) )
Alan Mishchenko committed
783
        {
784
            int RetValue = Dar_CutFilter( pObj, pCut );
Alan Mishchenko committed
785
            assert( !RetValue );
Alan Mishchenko committed
786 787
        }

Alan Mishchenko committed
788 789 790 791
        // assign the value of the cut
        pCut->Value = Dar_CutFindValue( p, pCut );
        // if the cut contains removed node, do not use it
        if ( pCut->Value == 0 )
Alan Mishchenko committed
792
        {
Alan Mishchenko committed
793 794
            p->nCutsSkipped++;
            pCut->fUsed = 0;
Alan Mishchenko committed
795
        }
Alan Mishchenko committed
796 797
        else if ( pCut->nLeaves < 2 )
            return pCutSet;
Alan Mishchenko committed
798
    }
Alan Mishchenko committed
799 800 801 802 803 804
    // count the number of nontrivial cuts cuts
    Dar_ObjForEachCut( pObj, pCut, i )
        p->nCutsUsed += pCut->fUsed;
    // discount trivial cut
    p->nCutsUsed--;
    return pCutSet;
Alan Mishchenko committed
805 806 807 808 809 810 811 812 813 814 815 816 817
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
818
Dar_Cut_t * Dar_ObjComputeCuts_rec( Dar_Man_t * p, Aig_Obj_t * pObj )
Alan Mishchenko committed
819
{
Alan Mishchenko committed
820 821
    if ( Dar_ObjCuts(pObj) )
        return Dar_ObjCuts(pObj);
822
    if ( Aig_ObjIsCi(pObj) )
Alan Mishchenko committed
823
        return Dar_ObjPrepareCuts( p, pObj );
Alan Mishchenko committed
824 825 826 827
    if ( Aig_ObjIsBuf(pObj) )
        return Dar_ObjComputeCuts_rec( p, Aig_ObjFanin0(pObj) );
    Dar_ObjComputeCuts_rec( p, Aig_ObjFanin0(pObj) );
    Dar_ObjComputeCuts_rec( p, Aig_ObjFanin1(pObj) );
828
    return Dar_ObjComputeCuts( p, pObj, 0 );
Alan Mishchenko committed
829 830 831 832 833 834 835
}

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


836 837
ABC_NAMESPACE_IMPL_END