cutNode.c 31.6 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    [cutNode.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [K-feasible cut computation package.]

  Synopsis    [Procedures to compute cuts for a node.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

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

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

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

#include "cutInt.h"

23 24 25
ABC_NAMESPACE_IMPL_START


Alan Mishchenko committed
26 27 28 29
////////////////////////////////////////////////////////////////////////
///                        DECLARATIONS                              ///
////////////////////////////////////////////////////////////////////////

Alan Mishchenko committed
30 31
static int Cut_NodeMapping( Cut_Man_t * p, Cut_Cut_t * pCuts, int Node, int Node0, int Node1 );
static int Cut_NodeMapping2( Cut_Man_t * p, Cut_Cut_t * pCuts, int Node, int Node0, int Node1 );
Alan Mishchenko committed
32

Alan Mishchenko committed
33
////////////////////////////////////////////////////////////////////////
Alan Mishchenko committed
34
///                     FUNCTION DEFINITIONS                         ///
Alan Mishchenko committed
35 36 37 38
////////////////////////////////////////////////////////////////////////

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

Alan Mishchenko committed
39
  Synopsis    [Returns 1 if pDom is contained in pCut.]
Alan Mishchenko committed
40 41 42 43 44 45 46 47

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
48
static inline int Cut_CutCheckDominance( Cut_Cut_t * pDom, Cut_Cut_t * pCut )
Alan Mishchenko committed
49
{
Alan Mishchenko committed
50 51 52 53 54 55 56 57 58 59 60
    int i, k;
    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;
Alan Mishchenko committed
61 62 63 64
}

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

Alan Mishchenko committed
65
  Synopsis    [Filters cuts using dominance.]
Alan Mishchenko committed
66 67 68 69 70 71 72 73

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
74 75 76 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 109 110 111 112 113 114 115 116 117 118 119 120 121 122
static inline void Cut_CutFilter( Cut_Man_t * p, Cut_Cut_t * pList )
{ 
    Cut_Cut_t * pListR, ** ppListR = &pListR;
    Cut_Cut_t * pCut, * pCut2, * pDom, * pPrev;
    // save the first cut
    *ppListR = pList, ppListR = &pList->pNext;
    // try to filter out other cuts
    pPrev = pList;
    Cut_ListForEachCutSafe( pList->pNext, pCut, pCut2 )
    {
        assert( pCut->nLeaves > 1 );
        // go through all the previous cuts up to pCut
        Cut_ListForEachCutStop( pList->pNext, pDom, pCut )
        {
            if ( pDom->nLeaves > pCut->nLeaves )
                continue;
            if ( (pDom->uSign & pCut->uSign) != pDom->uSign )
                continue;
            if ( Cut_CutCheckDominance( pDom, pCut ) )
                break;
        }
        if ( pDom != pCut ) // pDom is contained in pCut - recycle pCut
        {
            // make sure cuts are connected after removing
            pPrev->pNext = pCut->pNext;
            // recycle the cut
            Cut_CutRecycle( p, pCut );
        }
        else // pDom is NOT contained in pCut - save pCut
        {
            *ppListR = pCut, ppListR = &pCut->pNext;
            pPrev = pCut;
        }
    }
    *ppListR = NULL;
}

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

  Synopsis    [Checks equality of one cut.]

  Description [Returns 1 if the cut is removed.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline int Cut_CutFilterOneEqual( Cut_Man_t * p, Cut_List_t * pSuperList, Cut_Cut_t * pCut )
Alan Mishchenko committed
123
{
Alan Mishchenko committed
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
    Cut_Cut_t * pTemp;
    Cut_ListForEachCut( pSuperList->pHead[pCut->nLeaves], pTemp )
    {
        // skip the non-contained cuts
        if ( pCut->uSign != pTemp->uSign )
            continue;
        // check containment seriously
        if ( Cut_CutCheckDominance( pTemp, pCut ) )
        {
            p->nCutsFilter++;
            Cut_CutRecycle( p, pCut );
            return 1;
        }
    }
    return 0;
Alan Mishchenko committed
139 140 141 142
}

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

Alan Mishchenko committed
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
  Synopsis    [Checks containment for one cut.]

  Description [Returns 1 if the cut is removed.]
               
  SideEffects [May remove other cuts in the set.]

  SeeAlso     []

***********************************************************************/
static inline int Cut_CutFilterOne( Cut_Man_t * p, Cut_List_t * pSuperList, Cut_Cut_t * pCut )
{
    Cut_Cut_t * pTemp, * pTemp2, ** ppTail;
    int a;

    // check if this cut is filtered out by smaller cuts
    for ( a = 2; a <= (int)pCut->nLeaves; a++ )
    {
        Cut_ListForEachCut( pSuperList->pHead[a], pTemp )
        {
            // skip the non-contained cuts
            if ( (pTemp->uSign & pCut->uSign) != pTemp->uSign )
                continue;
            // check containment seriously
            if ( Cut_CutCheckDominance( pTemp, pCut ) )
            {
                p->nCutsFilter++;
                Cut_CutRecycle( p, pCut );
                return 1;
            }
        }
    }

    // filter out other cuts using this one
    for ( a = pCut->nLeaves + 1; a <= (int)pCut->nVarsMax; a++ )
    {
        ppTail = pSuperList->pHead + a;
        Cut_ListForEachCutSafe( pSuperList->pHead[a], pTemp, pTemp2 )
        {
            // skip the non-contained cuts
            if ( (pTemp->uSign & pCut->uSign) != pCut->uSign )
            {
                ppTail = &pTemp->pNext;
                continue;
            }
            // check containment seriously
            if ( Cut_CutCheckDominance( pCut, pTemp ) )
            {
                p->nCutsFilter++;
                p->nNodeCuts--;
                // move the head
                if ( pSuperList->pHead[a] == pTemp )
                    pSuperList->pHead[a] = pTemp->pNext;
                // move the tail
                if ( pSuperList->ppTail[a] == &pTemp->pNext )
                    pSuperList->ppTail[a] = ppTail;
                // skip the given cut in the list
                *ppTail = pTemp->pNext;
                // recycle pTemp
                Cut_CutRecycle( p, pTemp );
            }
            else
                ppTail = &pTemp->pNext;
        }
        assert( ppTail == pSuperList->ppTail[a] );
        assert( *ppTail == NULL );
    }

    return 0;
}

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

  Synopsis    [Checks if the cut is local and can be removed.]

  Description [Returns 1 if the cut is removed.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline int Cut_CutFilterGlobal( Cut_Man_t * p, Cut_Cut_t * pCut )
{
    int a;
    if ( pCut->nLeaves == 1 )
        return 0;
    for ( a = 0; a < (int)pCut->nLeaves; a++ )
        if ( Vec_IntEntry( p->vNodeAttrs, pCut->pLeaves[a] ) ) // global
            return 0;
    // there is no global nodes, the cut should be removed
    p->nCutsFilter++;
    Cut_CutRecycle( p, pCut );
    return 1;
}


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

  Synopsis    [Checks containment for one cut.]

  Description [Returns 1 if the cut is removed.]
               
  SideEffects [May remove other cuts in the set.]

  SeeAlso     []

***********************************************************************/
static inline int Cut_CutFilterOld( Cut_Man_t * p, Cut_Cut_t * pList, Cut_Cut_t * pCut )
{
    Cut_Cut_t * pPrev, * pTemp, * pTemp2, ** ppTail;

    // check if this cut is filtered out by smaller cuts
    pPrev = NULL;
    Cut_ListForEachCut( pList, pTemp )
    {
        if ( pTemp->nLeaves > pCut->nLeaves )
            break;
        pPrev = pTemp;
        // skip the non-contained cuts
        if ( (pTemp->uSign & pCut->uSign) != pTemp->uSign )
            continue;
        // check containment seriously
        if ( Cut_CutCheckDominance( pTemp, pCut ) )
        {
            p->nCutsFilter++;
            Cut_CutRecycle( p, pCut );
            return 1;
        }
    }
    assert( pPrev->pNext == pTemp );

    // filter out other cuts using this one
    ppTail = &pPrev->pNext;
    Cut_ListForEachCutSafe( pTemp, pTemp, pTemp2 )
    {
        // skip the non-contained cuts
        if ( (pTemp->uSign & pCut->uSign) != pCut->uSign )
        {
            ppTail = &pTemp->pNext;
            continue;
        }
        // check containment seriously
        if ( Cut_CutCheckDominance( pCut, pTemp ) )
        {
            p->nCutsFilter++;
            p->nNodeCuts--;
            // skip the given cut in the list
            *ppTail = pTemp->pNext;
            // recycle pTemp
            Cut_CutRecycle( p, pTemp );
        }
        else
            ppTail = &pTemp->pNext;
    }
    assert( *ppTail == NULL );
    return 0;
}

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

  Synopsis    [Processes two cuts.]

  Description [Returns 1 if the limit has been reached.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline int Cut_CutProcessTwo( Cut_Man_t * p, Cut_Cut_t * pCut0, Cut_Cut_t * pCut1, Cut_List_t * pSuperList )
{
    Cut_Cut_t * pCut;
    // merge the cuts
    if ( pCut0->nLeaves >= pCut1->nLeaves )
        pCut = Cut_CutMergeTwo( p, pCut0, pCut1 );
    else
        pCut = Cut_CutMergeTwo( p, pCut1, pCut0 );
    if ( pCut == NULL )
        return 0;
    assert( p->pParams->fSeq || pCut->nLeaves > 1 );
    // set the signature
    pCut->uSign = pCut0->uSign | pCut1->uSign;
    if ( p->pParams->fRecord )
        pCut->Num0 = pCut0->Num0, pCut->Num1 = pCut1->Num0;
    // check containment
    if ( p->pParams->fFilter )
    {
        if ( Cut_CutFilterOne(p, pSuperList, pCut) )
//        if ( Cut_CutFilterOneEqual(p, pSuperList, pCut) )
            return 0;
        if ( p->pParams->fSeq )
        {
            if ( p->pCompareOld && Cut_CutFilterOld(p, p->pCompareOld, pCut) )
                return 0;
            if ( p->pCompareNew && Cut_CutFilterOld(p, p->pCompareNew, pCut) )
                return 0;
        }
    }

    if ( p->pParams->fGlobal )
    {
        assert( p->vNodeAttrs != NULL );
        if ( Cut_CutFilterGlobal( p, pCut ) )
            return 0;
    }

    // compute the truth table
    if ( p->pParams->fTruth )
        Cut_TruthCompute( p, pCut, pCut0, pCut1, p->fCompl0, p->fCompl1 );
    // add to the list
    Cut_ListAdd( pSuperList, pCut );
    // return status (0 if okay; 1 if exceeded the limit)
    return ++p->nNodeCuts == p->pParams->nKeepMax;
}
 
/**Function*************************************************************

  Synopsis    [Computes the cuts by merging cuts at two nodes.]
Alan Mishchenko committed
361 362 363 364 365 366 367 368

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
369
Cut_Cut_t * Cut_NodeComputeCuts( Cut_Man_t * p, int Node, int Node0, int Node1, int fCompl0, int fCompl1, int fTriv, int TreeCode )
Alan Mishchenko committed
370
{
Alan Mishchenko committed
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
    Cut_List_t Super, * pSuper = &Super;
    Cut_Cut_t * pList, * pCut;
    int clk;
    // start the number of cuts at the node
    p->nNodes++;
    p->nNodeCuts = 0;
    // prepare information for recording
    if ( p->pParams->fRecord )
    {
        Cut_CutNumberList( Cut_NodeReadCutsNew(p, Node0) );
        Cut_CutNumberList( Cut_NodeReadCutsNew(p, Node1) );
    }
    // compute the cuts
clk = clock();
    Cut_ListStart( pSuper );
    Cut_NodeDoComputeCuts( p, pSuper, Node, fCompl0, fCompl1, Cut_NodeReadCutsNew(p, Node0), Cut_NodeReadCutsNew(p, Node1), fTriv, TreeCode );
    pList = Cut_ListFinish( pSuper );
p->timeMerge += clock() - clk;
    // verify the result of cut computation
//    Cut_CutListVerify( pList );
    // performing the recording
    if ( p->pParams->fRecord )
    {
        Vec_IntWriteEntry( p->vNodeStarts, Node, Vec_IntSize(p->vCutPairs) );
        Cut_ListForEachCut( pList, pCut )
            Vec_IntPush( p->vCutPairs, ((pCut->Num1 << 16) | pCut->Num0) );
        Vec_IntWriteEntry( p->vNodeCuts, Node, Vec_IntSize(p->vCutPairs) - Vec_IntEntry(p->vNodeStarts, Node) );
    }
Alan Mishchenko committed
399 400 401 402 403 404 405
    if ( p->pParams->fRecordAig )
    {
        extern void Aig_RManRecord( unsigned * pTruth, int nVarsInit );
        Cut_ListForEachCut( pList, pCut )
            if ( Cut_CutReadLeaveNum(pCut) > 4 )
                Aig_RManRecord( Cut_CutReadTruth(pCut), Cut_CutReadLeaveNum(pCut) );
    }
Alan Mishchenko committed
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
    // check if the node is over the list
    if ( p->nNodeCuts == p->pParams->nKeepMax )
        p->nCutsLimit++;
    // set the list at the node
    Vec_PtrFillExtra( p->vCutsNew, Node + 1, NULL );
    assert( Cut_NodeReadCutsNew(p, Node) == NULL );
    /////
//    pList->pNext = NULL;
    /////
    Cut_NodeWriteCutsNew( p, Node, pList );
    // filter the cuts
//clk = clock();
//    if ( p->pParams->fFilter )
//        Cut_CutFilter( p, pList0 );
//p->timeFilter += clock() - clk;
    // perform mapping of this node with these cuts
clk = clock();
    if ( p->pParams->fMap && !p->pParams->fSeq )
    {
//        int Delay1, Delay2;
//        Delay1 = Cut_NodeMapping( p, pList, Node, Node0, Node1 );    
//        Delay2 = Cut_NodeMapping2( p, pList, Node, Node0, Node1 );   
//        assert( Delay1 >= Delay2 );
        Cut_NodeMapping( p, pList, Node, Node0, Node1 );
    }
p->timeMap += clock() - clk;
    return pList;
Alan Mishchenko committed
433
}
Alan Mishchenko committed
434 435 436

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

Alan Mishchenko committed
437
  Synopsis    [Returns optimum delay mapping.]
Alan Mishchenko committed
438 439 440 441 442 443 444 445

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
446
int Cut_NodeMapping2( Cut_Man_t * p, Cut_Cut_t * pCuts, int Node, int Node0, int Node1 )
Alan Mishchenko committed
447
{
Alan Mishchenko committed
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
    Cut_Cut_t * pCut;
    int DelayMin, DelayCur, i;
    if ( pCuts == NULL )
        p->nDelayMin = -1;
    if ( p->nDelayMin == -1 )
        return -1;
    DelayMin = 1000000;
    Cut_ListForEachCut( pCuts, pCut )
    {
        if ( pCut->nLeaves == 1 )
            continue;
        DelayCur = 0;
        for ( i = 0; i < (int)pCut->nLeaves; i++ )
            if ( DelayCur < Vec_IntEntry(p->vDelays, pCut->pLeaves[i]) )
                DelayCur = Vec_IntEntry(p->vDelays, pCut->pLeaves[i]);
        if ( DelayMin > DelayCur )
            DelayMin = DelayCur;
    }
    if ( DelayMin == 1000000 )
    {
         p->nDelayMin = -1;
         return -1;
    }
    DelayMin++;
    Vec_IntWriteEntry( p->vDelays, Node, DelayMin );
    if ( p->nDelayMin < DelayMin )
        p->nDelayMin = DelayMin;
    return DelayMin;
Alan Mishchenko committed
476 477
}

Alan Mishchenko committed
478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495
/**Function*************************************************************

  Synopsis    [Returns optimum delay mapping using the largest cut.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Cut_NodeMapping( Cut_Man_t * p, Cut_Cut_t * pCuts, int Node, int Node0, int Node1 )
{
    Cut_Cut_t * pCut0, * pCut1, * pCut;
    int Delay0, Delay1, Delay;
    // get the fanin cuts
    Delay0 = Vec_IntEntry( p->vDelays2, Node0 );
    Delay1 = Vec_IntEntry( p->vDelays2, Node1 );
496 497
    pCut0 = (Delay0 == 0) ? (Cut_Cut_t *)Vec_PtrEntry( p->vCutsNew, Node0 ) : (Cut_Cut_t *)Vec_PtrEntry( p->vCutsMax, Node0 );
    pCut1 = (Delay1 == 0) ? (Cut_Cut_t *)Vec_PtrEntry( p->vCutsNew, Node1 ) : (Cut_Cut_t *)Vec_PtrEntry( p->vCutsMax, Node1 );
Alan Mishchenko committed
498 499 500 501 502
    if ( Delay0 == Delay1 )
        Delay = (Delay0 == 0) ? Delay0 + 1: Delay0;
    else if ( Delay0 > Delay1 )
    {
        Delay = Delay0;
503
        pCut1 = (Cut_Cut_t *)Vec_PtrEntry( p->vCutsNew, Node1 );
Alan Mishchenko committed
504 505 506 507 508
        assert( pCut1->nLeaves == 1 );
    }
    else // if ( Delay0 < Delay1 )
    {
        Delay = Delay1;
509
        pCut0 = (Cut_Cut_t *)Vec_PtrEntry( p->vCutsNew, Node0 );
Alan Mishchenko committed
510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531
        assert( pCut0->nLeaves == 1 );
    }
    // merge the cuts
    if ( pCut0->nLeaves < pCut1->nLeaves )
        pCut  = Cut_CutMergeTwo( p, pCut1, pCut0 );
    else
        pCut  = Cut_CutMergeTwo( p, pCut0, pCut1 );
    if ( pCut == NULL )
    {
        Delay++;
        pCut = Cut_CutAlloc( p );
        pCut->nLeaves = 2;
        pCut->pLeaves[0] = Node0 < Node1 ? Node0 : Node1;
        pCut->pLeaves[1] = Node0 < Node1 ? Node1 : Node0;
    }
    assert( Delay > 0 );
    Vec_IntWriteEntry( p->vDelays2, Node, Delay );
    Vec_PtrWriteEntry( p->vCutsMax, Node, pCut );
    if ( p->nDelayMin < Delay )
        p->nDelayMin = Delay;
    return Delay;
}
Alan Mishchenko committed
532 533 534

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

Alan Mishchenko committed
535
  Synopsis    [Computes area after mapping.]
Alan Mishchenko committed
536 537 538 539 540 541 542 543

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
544
int Cut_ManMappingArea_rec( Cut_Man_t * p, int Node )
Alan Mishchenko committed
545
{
Alan Mishchenko committed
546 547 548 549
    Cut_Cut_t * pCut;
    int i, Counter;
    if ( p->vCutsMax == NULL )
        return 0;
550
    pCut = (Cut_Cut_t *)Vec_PtrEntry( p->vCutsMax, Node );
Alan Mishchenko committed
551 552 553 554 555 556 557
    if ( pCut == NULL || pCut->nLeaves == 1 )
        return 0;
    Counter = 0;
    for ( i = 0; i < (int)pCut->nLeaves; i++ )
        Counter += Cut_ManMappingArea_rec( p, pCut->pLeaves[i] );
    Vec_PtrWriteEntry( p->vCutsMax, Node, NULL );
    return 1 + Counter;
Alan Mishchenko committed
558 559
}

Alan Mishchenko committed
560

Alan Mishchenko committed
561 562 563 564 565 566 567 568 569 570 571
/**Function*************************************************************

  Synopsis    [Computes the cuts by merging cuts at two nodes.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
572
void Cut_NodeDoComputeCuts( Cut_Man_t * p, Cut_List_t * pSuper, int Node, int fCompl0, int fCompl1, Cut_Cut_t * pList0, Cut_Cut_t * pList1, int fTriv, int TreeCode )
Alan Mishchenko committed
573
{
Alan Mishchenko committed
574 575
    Cut_Cut_t * pStop0, * pStop1, * pTemp0, * pTemp1;
    Cut_Cut_t * pStore0 = NULL, * pStore1 = NULL; // Suppress "might be used uninitialized"
Alan Mishchenko committed
576 577 578 579 580 581 582 583 584
    int i, nCutsOld, Limit;
    // start with the elementary cut
    if ( fTriv ) 
    {
//        printf( "Creating trivial cut %d.\n", Node );
        pTemp0 = Cut_CutCreateTriv( p, Node );
        Cut_ListAdd( pSuper, pTemp0 );
        p->nNodeCuts++;
    }
Alan Mishchenko committed
585
    // get the cut lists of children
Alan Mishchenko committed
586 587 588 589 590 591
    if ( pList0 == NULL || pList1 == NULL || (p->pParams->fLocal && TreeCode)  )
        return;

    // remember the old number of cuts
    nCutsOld = p->nCutsCur;
    Limit = p->pParams->nVarsMax;
Alan Mishchenko committed
592 593 594 595 596
    // get the simultation bit of the node
    p->fSimul = (fCompl0 ^ pList0->fSimul) & (fCompl1 ^ pList1->fSimul);
    // set temporary variables
    p->fCompl0 = fCompl0;
    p->fCompl1 = fCompl1;
Alan Mishchenko committed
597 598 599 600 601 602 603 604 605 606 607 608 609
    // if tree cuts are computed, make sure only the unit cuts propagate over the DAG nodes
    if ( TreeCode & 1 )
    {
        assert( pList0->nLeaves == 1 );
        pStore0 = pList0->pNext;
        pList0->pNext = NULL;
    }
    if ( TreeCode & 2 )
    {
        assert( pList1->nLeaves == 1 );
        pStore1 = pList1->pNext;
        pList1->pNext = NULL;
    }
Alan Mishchenko committed
610 611 612 613 614 615 616
    // find the point in the list where the max-var cuts begin
    Cut_ListForEachCut( pList0, pStop0 )
        if ( pStop0->nLeaves == (unsigned)Limit )
            break;
    Cut_ListForEachCut( pList1, pStop1 )
        if ( pStop1->nLeaves == (unsigned)Limit )
            break;
Alan Mishchenko committed
617

Alan Mishchenko committed
618 619 620
    // small by small
    Cut_ListForEachCutStop( pList0, pTemp0, pStop0 )
    Cut_ListForEachCutStop( pList1, pTemp1, pStop1 )
Alan Mishchenko committed
621 622 623 624 625 626
    {
        if ( Cut_CutProcessTwo( p, pTemp0, pTemp1, pSuper ) )
            goto Quits;
    }
    // small by large
    Cut_ListForEachCutStop( pList0, pTemp0, pStop0 )
Alan Mishchenko committed
627
    Cut_ListForEachCut( pStop1, pTemp1 )
Alan Mishchenko committed
628 629 630 631 632 633 634 635
    {
        if ( (pTemp0->uSign & pTemp1->uSign) != pTemp0->uSign )
            continue;
        if ( Cut_CutProcessTwo( p, pTemp0, pTemp1, pSuper ) )
            goto Quits;
    }
    // small by large
    Cut_ListForEachCutStop( pList1, pTemp1, pStop1 )
Alan Mishchenko committed
636
    Cut_ListForEachCut( pStop0, pTemp0 )
Alan Mishchenko committed
637 638 639 640 641 642
    {
        if ( (pTemp0->uSign & pTemp1->uSign) != pTemp1->uSign )
            continue;
        if ( Cut_CutProcessTwo( p, pTemp0, pTemp1, pSuper ) )
            goto Quits;
    }
Alan Mishchenko committed
643 644 645 646 647
    // large by large
    Cut_ListForEachCut( pStop0, pTemp0 )
    Cut_ListForEachCut( pStop1, pTemp1 )
    {
        assert( pTemp0->nLeaves == (unsigned)Limit && pTemp1->nLeaves == (unsigned)Limit );
Alan Mishchenko committed
648 649
        if ( pTemp0->uSign != pTemp1->uSign )
            continue;
Alan Mishchenko committed
650 651 652 653 654
        for ( i = 0; i < Limit; i++ )
            if ( pTemp0->pLeaves[i] != pTemp1->pLeaves[i] )
                break;
        if ( i < Limit )
            continue;
Alan Mishchenko committed
655 656 657 658 659 660 661 662 663 664
        if ( Cut_CutProcessTwo( p, pTemp0, pTemp1, pSuper ) )
            goto Quits;
    } 
    if ( p->nNodeCuts == 0 )
        p->nNodesNoCuts++;
Quits:
    if ( TreeCode & 1 )
        pList0->pNext = pStore0;
    if ( TreeCode & 2 )
        pList1->pNext = pStore1;
Alan Mishchenko committed
665 666 667 668 669 670 671 672 673 674 675 676 677 678 679
}

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

  Synopsis    [Computes the cuts by unioning cuts at a choice node.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Cut_Cut_t * Cut_NodeUnionCuts( Cut_Man_t * p, Vec_Int_t * vNodes )
{
Alan Mishchenko committed
680
    Cut_List_t Super, * pSuper = &Super;
Alan Mishchenko committed
681 682
    Cut_Cut_t * pList, * pListStart, * pCut, * pCut2;
    Cut_Cut_t * pTop = NULL; // Suppress "might be used uninitialized"
Alan Mishchenko committed
683 684 685 686
    int i, k, Node, Root, Limit = p->pParams->nVarsMax;
    int clk = clock();

    // start the new list
Alan Mishchenko committed
687
    Cut_ListStart( pSuper );
Alan Mishchenko committed
688 689 690

    // remember the root node to save the resulting cuts
    Root = Vec_IntEntry( vNodes, 0 );
Alan Mishchenko committed
691
    p->nNodeCuts = 1;
Alan Mishchenko committed
692 693 694 695 696 697

    // collect small cuts first
    Vec_PtrClear( p->vTemp );
    Vec_IntForEachEntry( vNodes, Node, i )
    {
        // get the cuts of this node
Alan Mishchenko committed
698 699
        pList = Cut_NodeReadCutsNew( p, Node );
        Cut_NodeWriteCutsNew( p, Node, NULL );
Alan Mishchenko committed
700 701 702
        assert( pList );
        // remember the starting point
        pListStart = pList->pNext;
Alan Mishchenko committed
703
        pList->pNext = NULL;
Alan Mishchenko committed
704 705
        // save or recycle the elementary cut
        if ( i == 0 )
Alan Mishchenko committed
706
            Cut_ListAdd( pSuper, pList ), pTop = pList;
Alan Mishchenko committed
707 708 709 710 711 712 713 714 715 716
        else
            Cut_CutRecycle( p, pList );
        // save all the cuts that are smaller than the limit
        Cut_ListForEachCutSafe( pListStart, pCut, pCut2 )
        {
            if ( pCut->nLeaves == (unsigned)Limit )
            {
                Vec_PtrPush( p->vTemp, pCut );
                break;
            }
Alan Mishchenko committed
717 718
            // check containment
            if ( p->pParams->fFilter && Cut_CutFilterOne( p, pSuper, pCut ) )
Alan Mishchenko committed
719 720 721
                continue;
            // set the complemented bit by comparing the first cut with the current cut
            pCut->fCompl = pTop->fSimul ^ pCut->fSimul;
Alan Mishchenko committed
722 723
            pListStart = pCut->pNext;
            pCut->pNext = NULL;
Alan Mishchenko committed
724
            // add to the list
Alan Mishchenko committed
725 726
            Cut_ListAdd( pSuper, pCut );
            if ( ++p->nNodeCuts == p->pParams->nKeepMax )
Alan Mishchenko committed
727 728
            {
                // recycle the rest of the cuts of this node
Alan Mishchenko committed
729
                Cut_ListForEachCutSafe( pListStart, pCut, pCut2 )
Alan Mishchenko committed
730 731 732 733 734
                    Cut_CutRecycle( p, pCut );
                // recycle all cuts of other nodes
                Vec_IntForEachEntryStart( vNodes, Node, k, i+1 )
                    Cut_NodeFreeCuts( p, Node );
                // recycle the saved cuts of other nodes
735
                Vec_PtrForEachEntry( Cut_Cut_t *, p->vTemp, pList, k )
Alan Mishchenko committed
736 737 738 739 740
                    Cut_ListForEachCutSafe( pList, pCut, pCut2 )
                        Cut_CutRecycle( p, pCut );
                goto finish;
            }
        }
Alan Mishchenko committed
741
    } 
Alan Mishchenko committed
742
    // collect larger cuts next
743
    Vec_PtrForEachEntry( Cut_Cut_t *, p->vTemp, pList, i )
Alan Mishchenko committed
744 745 746
    {
        Cut_ListForEachCutSafe( pList, pCut, pCut2 )
        {
Alan Mishchenko committed
747 748
            // check containment
            if ( p->pParams->fFilter && Cut_CutFilterOne( p, pSuper, pCut ) )
Alan Mishchenko committed
749 750 751
                continue;
            // set the complemented bit
            pCut->fCompl = pTop->fSimul ^ pCut->fSimul;
Alan Mishchenko committed
752 753
            pListStart = pCut->pNext;
            pCut->pNext = NULL;
Alan Mishchenko committed
754
            // add to the list
Alan Mishchenko committed
755 756
            Cut_ListAdd( pSuper, pCut );
            if ( ++p->nNodeCuts == p->pParams->nKeepMax )
Alan Mishchenko committed
757 758
            {
                // recycle the rest of the cuts
Alan Mishchenko committed
759
                Cut_ListForEachCutSafe( pListStart, pCut, pCut2 )
Alan Mishchenko committed
760 761
                    Cut_CutRecycle( p, pCut );
                // recycle the saved cuts of other nodes
762
                Vec_PtrForEachEntryStart( Cut_Cut_t *, p->vTemp, pList, k, i+1 )
Alan Mishchenko committed
763 764 765 766 767 768 769 770
                    Cut_ListForEachCutSafe( pList, pCut, pCut2 )
                        Cut_CutRecycle( p, pCut );
                goto finish;
            }
        }
    }
finish :
    // set the cuts at the node
Alan Mishchenko committed
771 772 773
    assert( Cut_NodeReadCutsNew(p, Root) == NULL );
    pList = Cut_ListFinish( pSuper );
    Cut_NodeWriteCutsNew( p, Root, pList );
Alan Mishchenko committed
774 775
p->timeUnion += clock() - clk;
    // filter the cuts
Alan Mishchenko committed
776 777 778 779
//clk = clock();
//    if ( p->pParams->fFilter )
//        Cut_CutFilter( p, pList );
//p->timeFilter += clock() - clk;
Alan Mishchenko committed
780
    p->nNodes -= vNodes->nSize - 1;
Alan Mishchenko committed
781 782 783
    return pList;
}

Alan Mishchenko committed
784 785
/**Function*************************************************************

Alan Mishchenko committed
786
  Synopsis    [Computes the cuts by unioning cuts at a choice node.]
Alan Mishchenko committed
787 788 789 790 791 792 793 794

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
795
Cut_Cut_t * Cut_NodeUnionCutsSeq( Cut_Man_t * p, Vec_Int_t * vNodes, int CutSetNum, int fFirst )
Alan Mishchenko committed
796
{
Alan Mishchenko committed
797 798 799 800
    Cut_List_t Super, * pSuper = &Super;
    Cut_Cut_t * pList, * pListStart, * pCut, * pCut2, * pTop;
    int i, k, Node, Root, Limit = p->pParams->nVarsMax;
    int clk = clock();
Alan Mishchenko committed
801

Alan Mishchenko committed
802 803
    // start the new list
    Cut_ListStart( pSuper );
Alan Mishchenko committed
804

Alan Mishchenko committed
805 806 807
    // remember the root node to save the resulting cuts
    Root = Vec_IntEntry( vNodes, 0 );
    p->nNodeCuts = 1;
Alan Mishchenko committed
808

Alan Mishchenko committed
809 810 811
    // store the original lists for comparison
    p->pCompareOld = Cut_NodeReadCutsOld( p, Root );
    p->pCompareNew = (CutSetNum >= 0)? Cut_NodeReadCutsNew( p, Root ) : NULL;
Alan Mishchenko committed
812

Alan Mishchenko committed
813 814 815 816 817
    // get the topmost cut
    pTop = NULL;
    if ( (pTop = Cut_NodeReadCutsOld( p, Root )) == NULL )
        pTop = Cut_NodeReadCutsNew( p, Root );
    assert( pTop != NULL );
Alan Mishchenko committed
818

Alan Mishchenko committed
819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835
    // collect small cuts first
    Vec_PtrClear( p->vTemp );
    Vec_IntForEachEntry( vNodes, Node, i )
    {
        // get the cuts of this node
        if ( i == 0 && CutSetNum >= 0 )
        {
            pList = Cut_NodeReadCutsTemp( p, CutSetNum );
            Cut_NodeWriteCutsTemp( p, CutSetNum, NULL );
        }
        else
        {
            pList = Cut_NodeReadCutsNew( p, Node );
            Cut_NodeWriteCutsNew( p, Node, NULL );
        }
        if ( pList == NULL )
            continue;
Alan Mishchenko committed
836

Alan Mishchenko committed
837 838 839 840 841 842 843 844 845 846 847 848 849 850
        // process the cuts
        if ( fFirst )
        {
            // remember the starting point
            pListStart = pList->pNext;
            pList->pNext = NULL;
            // save or recycle the elementary cut
            if ( i == 0 )
                Cut_ListAdd( pSuper, pList );
            else
                Cut_CutRecycle( p, pList );
        }
        else
            pListStart = pList;
Alan Mishchenko committed
851

Alan Mishchenko committed
852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874
        // save all the cuts that are smaller than the limit
        Cut_ListForEachCutSafe( pListStart, pCut, pCut2 )
        {
            if ( pCut->nLeaves == (unsigned)Limit )
            {
                Vec_PtrPush( p->vTemp, pCut );
                break;
            }
            // check containment
//            if ( p->pParams->fFilter && Cut_CutFilterOne( p, pSuper, pCut ) )
//                continue;
            if ( p->pParams->fFilter )
            {
                if ( Cut_CutFilterOne(p, pSuper, pCut) )
                    continue;
                if ( p->pParams->fSeq )
                {
                    if ( p->pCompareOld && Cut_CutFilterOld(p, p->pCompareOld, pCut) )
                        continue;
                    if ( p->pCompareNew && Cut_CutFilterOld(p, p->pCompareNew, pCut) )
                        continue;
                }
            }
Alan Mishchenko committed
875

Alan Mishchenko committed
876 877 878 879 880 881 882 883 884 885 886 887 888 889 890
            // set the complemented bit by comparing the first cut with the current cut
            pCut->fCompl = pTop->fSimul ^ pCut->fSimul;
            pListStart = pCut->pNext;
            pCut->pNext = NULL;
            // add to the list
            Cut_ListAdd( pSuper, pCut );
            if ( ++p->nNodeCuts == p->pParams->nKeepMax )
            {
                // recycle the rest of the cuts of this node
                Cut_ListForEachCutSafe( pListStart, pCut, pCut2 )
                    Cut_CutRecycle( p, pCut );
                // recycle all cuts of other nodes
                Vec_IntForEachEntryStart( vNodes, Node, k, i+1 )
                    Cut_NodeFreeCuts( p, Node );
                // recycle the saved cuts of other nodes
891
                Vec_PtrForEachEntry( Cut_Cut_t *, p->vTemp, pList, k )
Alan Mishchenko committed
892 893 894 895 896 897 898
                    Cut_ListForEachCutSafe( pList, pCut, pCut2 )
                        Cut_CutRecycle( p, pCut );
                goto finish;
            }
        }
    } 
    // collect larger cuts next
899
    Vec_PtrForEachEntry( Cut_Cut_t *, p->vTemp, pList, i )
Alan Mishchenko committed
900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917
    {
        Cut_ListForEachCutSafe( pList, pCut, pCut2 )
        {
            // check containment
//            if ( p->pParams->fFilter && Cut_CutFilterOne( p, pSuper, pCut ) )
//                continue;
            if ( p->pParams->fFilter )
            {
                if ( Cut_CutFilterOne(p, pSuper, pCut) )
                    continue;
                if ( p->pParams->fSeq )
                {
                    if ( p->pCompareOld && Cut_CutFilterOld(p, p->pCompareOld, pCut) )
                        continue;
                    if ( p->pCompareNew && Cut_CutFilterOld(p, p->pCompareNew, pCut) )
                        continue;
                }
            }
Alan Mishchenko committed
918

Alan Mishchenko committed
919 920 921 922 923 924 925 926 927 928 929 930
            // set the complemented bit
            pCut->fCompl = pTop->fSimul ^ pCut->fSimul;
            pListStart   = pCut->pNext;
            pCut->pNext  = NULL;
            // add to the list
            Cut_ListAdd( pSuper, pCut );
            if ( ++p->nNodeCuts == p->pParams->nKeepMax )
            {
                // recycle the rest of the cuts
                Cut_ListForEachCutSafe( pListStart, pCut, pCut2 )
                    Cut_CutRecycle( p, pCut );
                // recycle the saved cuts of other nodes
931
                Vec_PtrForEachEntryStart( Cut_Cut_t *, p->vTemp, pList, k, i+1 )
Alan Mishchenko committed
932 933 934 935 936 937 938 939 940
                    Cut_ListForEachCutSafe( pList, pCut, pCut2 )
                        Cut_CutRecycle( p, pCut );
                goto finish;
            }
        }
    }
finish :
    // set the cuts at the node
    pList = Cut_ListFinish( pSuper );
Alan Mishchenko committed
941

Alan Mishchenko committed
942 943 944 945 946 947 948 949
    // set the lists at the node
//    assert( Cut_NodeReadCutsNew(p, Root) == NULL );
//    Cut_NodeWriteCutsNew( p, Root, pList );
    if ( CutSetNum >= 0 )
    {
        assert( Cut_NodeReadCutsTemp(p, CutSetNum) == NULL );
        Cut_NodeWriteCutsTemp( p, CutSetNum, pList );
    }
Alan Mishchenko committed
950
    else
Alan Mishchenko committed
951 952 953 954 955 956 957 958 959 960 961 962 963 964
    {
        assert( Cut_NodeReadCutsNew(p, Root) == NULL );
        Cut_NodeWriteCutsNew( p, Root, pList );
    }

p->timeUnion += clock() - clk;
    // filter the cuts
//clk = clock();
//    if ( p->pParams->fFilter )
//        Cut_CutFilter( p, pList );
//p->timeFilter += clock() - clk;
//    if ( fFirst )
//        p->nNodes -= vNodes->nSize - 1;
    return pList;
Alan Mishchenko committed
965 966
}

Alan Mishchenko committed
967

Alan Mishchenko committed
968 969
/**Function*************************************************************

Alan Mishchenko committed
970
  Synopsis    [Verifies that the list contains only non-dominated cuts.]
Alan Mishchenko committed
971 972 973 974 975 976 977 978

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
979
int Cut_CutListVerify( Cut_Cut_t * pList )
Alan Mishchenko committed
980
{ 
Alan Mishchenko committed
981 982
    Cut_Cut_t * pCut, * pDom;
    Cut_ListForEachCut( pList, pCut )
Alan Mishchenko committed
983
    {
Alan Mishchenko committed
984
        Cut_ListForEachCutStop( pList, pDom, pCut )
Alan Mishchenko committed
985
        {
Alan Mishchenko committed
986
            if ( Cut_CutCheckDominance( pDom, pCut ) )
Alan Mishchenko committed
987
            {
Alan Mishchenko committed
988 989 990 991
                printf( "******************* These are contained cuts:\n" );
                Cut_CutPrint( pDom, 1 );
                Cut_CutPrint( pDom, 1 );
                return 0;
Alan Mishchenko committed
992 993 994
            }
        }
    }
Alan Mishchenko committed
995
    return 1;
Alan Mishchenko committed
996 997
}

Alan Mishchenko committed
998 999 1000 1001 1002
////////////////////////////////////////////////////////////////////////
///                       END OF FILE                                ///
////////////////////////////////////////////////////////////////////////


1003 1004
ABC_NAMESPACE_IMPL_END