cuddGenCof.c 61.4 KB
Newer Older
Alan Mishchenko committed
1 2 3 4 5 6 7 8 9
/**CFile***********************************************************************

  FileName    [cuddGenCof.c]

  PackageName [cudd]

  Synopsis    [Generalized cofactors for BDDs and ADDs.]

  Description [External procedures included in this module:
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
                <ul>
                <li> Cudd_bddConstrain()
                <li> Cudd_bddRestrict()
                <li> Cudd_bddNPAnd()
                <li> Cudd_addConstrain()
                <li> Cudd_bddConstrainDecomp()
                <li> Cudd_addRestrict()
                <li> Cudd_bddCharToVect()
                <li> Cudd_bddLICompaction()
                <li> Cudd_bddSqueeze()
                <li> Cudd_SubsetCompress()
                <li> Cudd_SupersetCompress()
                </ul>
            Internal procedures included in this module:
                <ul>
                <li> cuddBddConstrainRecur()
                <li> cuddBddRestrictRecur()
                <li> cuddBddNPAndRecur()
                <li> cuddAddConstrainRecur()
                <li> cuddAddRestrictRecur()
                <li> cuddBddLICompaction()
                </ul>
            Static procedures included in this module:
                <ul>
                <li> cuddBddConstrainDecomp()
                <li> cuddBddCharToVect()
                <li> cuddBddLICMarkEdges()
                <li> cuddBddLICBuildResult()
                <li> cuddBddSqueeze()
                </ul>
                ]
Alan Mishchenko committed
41 42 43

  Author      [Fabio Somenzi]

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
  Copyright   [Copyright (c) 1995-2004, Regents of the University of Colorado

  All rights reserved.

  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:

  Redistributions of source code must retain the above copyright
  notice, this list of conditions and the following disclaimer.

  Redistributions in binary form must reproduce the above copyright
  notice, this list of conditions and the following disclaimer in the
  documentation and/or other materials provided with the distribution.

  Neither the name of the University of Colorado nor the names of its
  contributors may be used to endorse or promote products derived from
  this software without specific prior written permission.

  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  POSSIBILITY OF SUCH DAMAGE.]
Alan Mishchenko committed
75 76 77

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

78
#include "misc/util/util_hack.h"
Alan Mishchenko committed
79 80
#include "cuddInt.h"

81 82 83
ABC_NAMESPACE_IMPL_START


Alan Mishchenko committed
84

85

Alan Mishchenko committed
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
/*---------------------------------------------------------------------------*/
/* Constant declarations                                                     */
/*---------------------------------------------------------------------------*/

/* Codes for edge markings in Cudd_bddLICompaction.  The codes are defined
** so that they can be bitwise ORed to implement the code priority scheme.
*/
#define DD_LIC_DC 0
#define DD_LIC_1  1
#define DD_LIC_0  2
#define DD_LIC_NL 3

/*---------------------------------------------------------------------------*/
/* Stucture declarations                                                     */
/*---------------------------------------------------------------------------*/


/*---------------------------------------------------------------------------*/
/* Type declarations                                                         */
/*---------------------------------------------------------------------------*/

/* Key for the cache used in the edge marking phase. */
typedef struct MarkCacheKey {
    DdNode *f;
    DdNode *c;
} MarkCacheKey;

/*---------------------------------------------------------------------------*/
/* Variable declarations                                                     */
/*---------------------------------------------------------------------------*/

#ifndef lint
118
static char rcsid[] DD_UNUSED = "$Id: cuddGenCof.c,v 1.38 2005/05/14 17:27:11 fabio Exp $";
Alan Mishchenko committed
119 120 121 122 123 124 125 126 127 128 129 130
#endif

/*---------------------------------------------------------------------------*/
/* Macro declarations                                                        */
/*---------------------------------------------------------------------------*/

/**AutomaticStart*************************************************************/

/*---------------------------------------------------------------------------*/
/* Static function prototypes                                                */
/*---------------------------------------------------------------------------*/

131 132
static int cuddBddConstrainDecomp (DdManager *dd, DdNode *f, DdNode **decomp);
static DdNode * cuddBddCharToVect (DdManager *dd, DdNode *f, DdNode *x);
133 134
static int cuddBddLICMarkEdges (DdManager *dd, DdNode *f, DdNode *c, st__table *table, st__table *cache);
static DdNode * cuddBddLICBuildResult (DdManager *dd, DdNode *f, st__table *cache, st__table *table);
135 136
static int MarkCacheHash (const char *ptr, int modulus);
static int MarkCacheCompare (const char *ptr1, const char *ptr2);
137
static enum st__retval MarkCacheCleanUp (char *key, char *value, char *arg);
138
static DdNode * cuddBddSqueeze (DdManager *dd, DdNode *l, DdNode *u);
Alan Mishchenko committed
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 166 167 168 169 170 171 172 173 174 175 176 177 178 179

/**AutomaticEnd***************************************************************/

/*---------------------------------------------------------------------------*/
/* Definition of exported functions                                          */
/*---------------------------------------------------------------------------*/


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

  Synopsis    [Computes f constrain c.]

  Description [Computes f constrain c (f @ c).
  Uses a canonical form: (f' @ c) = ( f @ c)'.  (Note: this is not true
  for c.)  List of special cases:
    <ul>
    <li> f @ 0 = 0
    <li> f @ 1 = f
    <li> 0 @ c = 0
    <li> 1 @ c = 1
    <li> f @ f = 1
    <li> f @ f'= 0
    </ul>
  Returns a pointer to the result if successful; NULL otherwise. Note that if
  F=(f1,...,fn) and reordering takes place while computing F @ c, then the
  image restriction property (Img(F,c) = Img(F @ c)) is lost.]

  SideEffects [None]

  SeeAlso     [Cudd_bddRestrict Cudd_addConstrain]

******************************************************************************/
DdNode *
Cudd_bddConstrain(
  DdManager * dd,
  DdNode * f,
  DdNode * c)
{
    DdNode *res;

    do {
180 181
        dd->reordered = 0;
        res = cuddBddConstrainRecur(dd,f,c);
Alan Mishchenko committed
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
    } while (dd->reordered == 1);
    return(res);

} /* end of Cudd_bddConstrain */


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

  Synopsis [BDD restrict according to Coudert and Madre's algorithm
  (ICCAD90).]

  Description [BDD restrict according to Coudert and Madre's algorithm
  (ICCAD90). Returns the restricted BDD if successful; otherwise NULL.
  If application of restrict results in a BDD larger than the input
  BDD, the input BDD is returned.]

  SideEffects [None]

  SeeAlso     [Cudd_bddConstrain Cudd_addRestrict]

******************************************************************************/
DdNode *
Cudd_bddRestrict(
  DdManager * dd,
  DdNode * f,
  DdNode * c)
{
    DdNode *suppF, *suppC, *commonSupport;
    DdNode *cplus, *res;
    int retval;
    int sizeF, sizeRes;

    /* Check terminal cases here to avoid computing supports in trivial cases.
    ** This also allows us notto check later for the case c == 0, in which
    ** there is no common support. */
    if (c == Cudd_Not(DD_ONE(dd))) return(Cudd_Not(DD_ONE(dd)));
    if (Cudd_IsConstant(f)) return(f);
    if (f == c) return(DD_ONE(dd));
    if (f == Cudd_Not(c)) return(Cudd_Not(DD_ONE(dd)));

    /* Check if supports intersect. */
    retval = Cudd_ClassifySupport(dd,f,c,&commonSupport,&suppF,&suppC);
    if (retval == 0) {
225
        return(NULL);
Alan Mishchenko committed
226 227 228 229 230
    }
    cuddRef(commonSupport); cuddRef(suppF); cuddRef(suppC);
    Cudd_IterDerefBdd(dd,suppF);

    if (commonSupport == DD_ONE(dd)) {
231 232 233
        Cudd_IterDerefBdd(dd,commonSupport);
        Cudd_IterDerefBdd(dd,suppC);
        return(f);
Alan Mishchenko committed
234 235 236 237 238 239
    }
    Cudd_IterDerefBdd(dd,commonSupport);

    /* Abstract from c the variables that do not appear in f. */
    cplus = Cudd_bddExistAbstract(dd, c, suppC);
    if (cplus == NULL) {
240 241
        Cudd_IterDerefBdd(dd,suppC);
        return(NULL);
Alan Mishchenko committed
242 243 244 245 246
    }
    cuddRef(cplus);
    Cudd_IterDerefBdd(dd,suppC);

    do {
247 248
        dd->reordered = 0;
        res = cuddBddRestrictRecur(dd, f, cplus);
Alan Mishchenko committed
249 250
    } while (dd->reordered == 1);
    if (res == NULL) {
251 252
        Cudd_IterDerefBdd(dd,cplus);
        return(NULL);
Alan Mishchenko committed
253 254 255 256 257 258 259 260
    }
    cuddRef(res);
    Cudd_IterDerefBdd(dd,cplus);
    /* Make restric safe by returning the smaller of the input and the
    ** result. */
    sizeF = Cudd_DagSize(f);
    sizeRes = Cudd_DagSize(res);
    if (sizeF <= sizeRes) {
261 262
        Cudd_IterDerefBdd(dd, res);
        return(f);
Alan Mishchenko committed
263
    } else {
264 265
        cuddDeref(res);
        return(res);
Alan Mishchenko committed
266 267 268 269 270 271 272
    }

} /* end of Cudd_bddRestrict */


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

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
  Synopsis    [Computes f non-polluting-and g.]

  Description [Computes f non-polluting-and g.  The non-polluting AND
  of f and g is a hybrid of AND and Restrict.  From Restrict, this
  operation takes the idea of existentially quantifying the top
  variable of the second operand if it does not appear in the first.
  Therefore, the variables that appear in the result also appear in f.
  For the rest, the function behaves like AND.  Since the two operands
  play different roles, non-polluting AND is not commutative.

  Returns a pointer to the result if successful; NULL otherwise.]

  SideEffects [None]

  SeeAlso     [Cudd_bddConstrain Cudd_bddRestrict]

******************************************************************************/
DdNode *
Cudd_bddNPAnd(
  DdManager * dd,
  DdNode * f,
  DdNode * g)
{
    DdNode *res;

    do {
        dd->reordered = 0;
        res = cuddBddNPAndRecur(dd,f,g);
    } while (dd->reordered == 1);
    return(res);

} /* end of Cudd_bddNPAnd */


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

Alan Mishchenko committed
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
  Synopsis    [Computes f constrain c for ADDs.]

  Description [Computes f constrain c (f @ c), for f an ADD and c a 0-1
  ADD.  List of special cases:
    <ul>
    <li> F @ 0 = 0
    <li> F @ 1 = F
    <li> 0 @ c = 0
    <li> 1 @ c = 1
    <li> F @ F = 1
    </ul>
  Returns a pointer to the result if successful; NULL otherwise.]

  SideEffects [None]

  SeeAlso     [Cudd_bddConstrain]

******************************************************************************/
DdNode *
Cudd_addConstrain(
  DdManager * dd,
  DdNode * f,
  DdNode * c)
{
    DdNode *res;

    do {
336 337
        dd->reordered = 0;
        res = cuddAddConstrainRecur(dd,f,c);
Alan Mishchenko committed
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
    } while (dd->reordered == 1);
    return(res);

} /* end of Cudd_addConstrain */


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

  Synopsis [BDD conjunctive decomposition as in McMillan's CAV96 paper.]

  Description [BDD conjunctive decomposition as in McMillan's CAV96
  paper.  The decomposition is canonical only for a given variable
  order. If canonicity is required, variable ordering must be disabled
  after the decomposition has been computed. Returns an array with one
  entry for each BDD variable in the manager if successful; otherwise
  NULL. The components of the solution have their reference counts
  already incremented (unlike the results of most other functions in
  the package.]

  SideEffects [None]

  SeeAlso     [Cudd_bddConstrain Cudd_bddExistAbstract]

******************************************************************************/
DdNode **
Cudd_bddConstrainDecomp(
  DdManager * dd,
  DdNode * f)
{
    DdNode **decomp;
    int res;
    int i;

    /* Create an initialize decomposition array. */
Alan Mishchenko committed
372
    decomp = ABC_ALLOC(DdNode *,dd->size);
Alan Mishchenko committed
373
    if (decomp == NULL) {
374 375
        dd->errorCode = CUDD_MEMORY_OUT;
        return(NULL);
Alan Mishchenko committed
376 377
    }
    for (i = 0; i < dd->size; i++) {
378
        decomp[i] = NULL;
Alan Mishchenko committed
379 380
    }
    do {
381 382 383 384 385 386 387
        dd->reordered = 0;
        /* Clean up the decomposition array in case reordering took place. */
        for (i = 0; i < dd->size; i++) {
            if (decomp[i] != NULL) {
                Cudd_IterDerefBdd(dd, decomp[i]);
                decomp[i] = NULL;
            }
Alan Mishchenko committed
388
        }
389
        res = cuddBddConstrainDecomp(dd,f,decomp);
Alan Mishchenko committed
390 391
    } while (dd->reordered == 1);
    if (res == 0) {
392 393
        ABC_FREE(decomp);
        return(NULL);
Alan Mishchenko committed
394 395 396
    }
    /* Missing components are constant ones. */
    for (i = 0; i < dd->size; i++) {
397 398 399 400
        if (decomp[i] == NULL) {
            decomp[i] = DD_ONE(dd);
            cuddRef(decomp[i]);
        }
Alan Mishchenko committed
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 433 434 435
    }
    return(decomp);

} /* end of Cudd_bddConstrainDecomp */


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

  Synopsis [ADD restrict according to Coudert and Madre's algorithm
  (ICCAD90).]

  Description [ADD restrict according to Coudert and Madre's algorithm
  (ICCAD90). Returns the restricted ADD if successful; otherwise NULL.
  If application of restrict results in an ADD larger than the input
  ADD, the input ADD is returned.]

  SideEffects [None]

  SeeAlso     [Cudd_addConstrain Cudd_bddRestrict]

******************************************************************************/
DdNode *
Cudd_addRestrict(
  DdManager * dd,
  DdNode * f,
  DdNode * c)
{
    DdNode *supp_f, *supp_c;
    DdNode *res, *commonSupport;
    int intersection;
    int sizeF, sizeRes;

    /* Check if supports intersect. */
    supp_f = Cudd_Support(dd, f);
    if (supp_f == NULL) {
436
        return(NULL);
Alan Mishchenko committed
437 438 439 440
    }
    cuddRef(supp_f);
    supp_c = Cudd_Support(dd, c);
    if (supp_c == NULL) {
441 442
        Cudd_RecursiveDeref(dd,supp_f);
        return(NULL);
Alan Mishchenko committed
443 444 445 446
    }
    cuddRef(supp_c);
    commonSupport = Cudd_bddLiteralSetIntersection(dd, supp_f, supp_c);
    if (commonSupport == NULL) {
447 448 449
        Cudd_RecursiveDeref(dd,supp_f);
        Cudd_RecursiveDeref(dd,supp_c);
        return(NULL);
Alan Mishchenko committed
450 451 452 453 454 455 456 457
    }
    cuddRef(commonSupport);
    Cudd_RecursiveDeref(dd,supp_f);
    Cudd_RecursiveDeref(dd,supp_c);
    intersection = commonSupport != DD_ONE(dd);
    Cudd_RecursiveDeref(dd,commonSupport);

    if (intersection) {
458 459 460 461 462 463 464 465 466 467 468 469 470
        do {
            dd->reordered = 0;
            res = cuddAddRestrictRecur(dd, f, c);
        } while (dd->reordered == 1);
        sizeF = Cudd_DagSize(f);
        sizeRes = Cudd_DagSize(res);
        if (sizeF <= sizeRes) {
            cuddRef(res);
            Cudd_RecursiveDeref(dd, res);
            return(f);
        } else {
            return(res);
        }
Alan Mishchenko committed
471
    } else {
472
        return(f);
Alan Mishchenko committed
473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493
    }

} /* end of Cudd_addRestrict */


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

  Synopsis    [Computes a vector whose image equals a non-zero function.]

  Description [Computes a vector of BDDs whose image equals a non-zero
  function.
  The result depends on the variable order. The i-th component of the vector
  depends only on the first i variables in the order.  Each BDD in the vector
  is not larger than the BDD of the given characteristic function.  This
  function is based on the description of char-to-vect in "Verification of
  Sequential Machines Using Boolean Functional Vectors" by O. Coudert, C.
  Berthet and J. C. Madre.
  Returns a pointer to an array containing the result if successful; NULL
  otherwise. The size of the array equals the number of variables in the
  manager. The components of the solution have their reference counts 
  already incremented (unlike the results of most other functions in 
494
  the package).]
Alan Mishchenko committed
495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511

  SideEffects [None]

  SeeAlso     [Cudd_bddConstrain]

******************************************************************************/
DdNode **
Cudd_bddCharToVect(
  DdManager * dd,
  DdNode * f)
{
    int i, j;
    DdNode **vect;
    DdNode *res = NULL;

    if (f == Cudd_Not(DD_ONE(dd))) return(NULL);

Alan Mishchenko committed
512
    vect = ABC_ALLOC(DdNode *, dd->size);
Alan Mishchenko committed
513
    if (vect == NULL) {
514 515
        dd->errorCode = CUDD_MEMORY_OUT;
        return(NULL);
Alan Mishchenko committed
516 517 518
    }

    do {
519 520 521 522 523 524 525 526 527 528 529 530
        dd->reordered = 0;
        for (i = 0; i < dd->size; i++) {
            res = cuddBddCharToVect(dd,f,dd->vars[dd->invperm[i]]);
            if (res == NULL) {
                /* Clean up the vector array in case reordering took place. */
                for (j = 0; j < i; j++) {
                    Cudd_IterDerefBdd(dd, vect[dd->invperm[j]]);
                }
                break;
            }
            cuddRef(res);
            vect[dd->invperm[i]] = res;
Alan Mishchenko committed
531 532 533
        }
    } while (dd->reordered == 1);
    if (res == NULL) {
534 535
        ABC_FREE(vect);
        return(NULL);
Alan Mishchenko committed
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
    }
    return(vect);

} /* end of Cudd_bddCharToVect */


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

  Synopsis    [Performs safe minimization of a BDD.]

  Description [Performs safe minimization of a BDD. Given the BDD
  <code>f</code> of a function to be minimized and a BDD
  <code>c</code> representing the care set, Cudd_bddLICompaction
  produces the BDD of a function that agrees with <code>f</code>
  wherever <code>c</code> is 1.  Safe minimization means that the size
  of the result is guaranteed not to exceed the size of
  <code>f</code>. This function is based on the DAC97 paper by Hong et
  al..  Returns a pointer to the result if successful; NULL
  otherwise.]

  SideEffects [None]

  SeeAlso     [Cudd_bddRestrict]

******************************************************************************/
DdNode *
Cudd_bddLICompaction(
  DdManager * dd /* manager */,
  DdNode * f /* function to be minimized */,
  DdNode * c /* constraint (care set) */)
{
    DdNode *res;

    do {
570 571
        dd->reordered = 0;
        res = cuddBddLICompaction(dd,f,c);
Alan Mishchenko committed
572 573 574 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
    } while (dd->reordered == 1);
    return(res);

} /* end of Cudd_bddLICompaction */


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

  Synopsis    [Finds a small BDD in a function interval.]

  Description [Finds a small BDD in a function interval. Given BDDs
  <code>l</code> and <code>u</code>, representing the lower bound and
  upper bound of a function interval, Cudd_bddSqueeze produces the BDD
  of a function within the interval with a small BDD.  Returns a
  pointer to the result if successful; NULL otherwise.]

  SideEffects [None]

  SeeAlso     [Cudd_bddRestrict Cudd_bddLICompaction]

******************************************************************************/
DdNode *
Cudd_bddSqueeze(
  DdManager * dd /* manager */,
  DdNode * l /* lower bound */,
  DdNode * u /* upper bound */)
{
    DdNode *res;
    int sizeRes, sizeL, sizeU;

    do {
603 604
        dd->reordered = 0;
        res = cuddBddSqueeze(dd,l,u);
Alan Mishchenko committed
605 606 607 608 609 610 611 612
    } while (dd->reordered == 1);
    if (res == NULL) return(NULL);
    /* We now compare the result with the bounds and return the smallest.
    ** We first compare to u, so that in case l == 0 and u == 1, we return
    ** 0 as in other minimization algorithms. */
    sizeRes = Cudd_DagSize(res);
    sizeU = Cudd_DagSize(u);
    if (sizeU <= sizeRes) {
613 614 615 616
        cuddRef(res);
        Cudd_IterDerefBdd(dd,res);
        res = u;
        sizeRes = sizeU;
Alan Mishchenko committed
617 618 619
    }
    sizeL = Cudd_DagSize(l);
    if (sizeL <= sizeRes) {
620 621 622 623
        cuddRef(res);
        Cudd_IterDerefBdd(dd,res);
        res = l;
        sizeRes = sizeL;
Alan Mishchenko committed
624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661
    }
    return(res);

} /* end of Cudd_bddSqueeze */


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

  Synopsis    [Finds a small BDD that agrees with <code>f</code> over
  <code>c</code>.]

  Description [Finds a small BDD that agrees with <code>f</code> over
  <code>c</code>.  Returns a pointer to the result if successful; NULL
  otherwise.]

  SideEffects [None]

  SeeAlso     [Cudd_bddRestrict Cudd_bddLICompaction Cudd_bddSqueeze]

******************************************************************************/
DdNode *
Cudd_bddMinimize(
  DdManager * dd,
  DdNode * f,
  DdNode * c)
{
    DdNode *cplus, *res;

    if (c == Cudd_Not(DD_ONE(dd))) return(c);
    if (Cudd_IsConstant(f)) return(f);
    if (f == c) return(DD_ONE(dd));
    if (f == Cudd_Not(c)) return(Cudd_Not(DD_ONE(dd)));

    cplus = Cudd_RemapOverApprox(dd,c,0,0,1.0);
    if (cplus == NULL) return(NULL);
    cuddRef(cplus);
    res = Cudd_bddLICompaction(dd,f,cplus);
    if (res == NULL) {
662 663
        Cudd_IterDerefBdd(dd,cplus);
        return(NULL);
Alan Mishchenko committed
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
    }
    cuddRef(res);
    Cudd_IterDerefBdd(dd,cplus);
    cuddDeref(res);
    return(res);

} /* end of Cudd_bddMinimize */


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

  Synopsis    [Find a dense subset of BDD <code>f</code>.]

  Description [Finds a dense subset of BDD <code>f</code>. Density is
  the ratio of number of minterms to number of nodes.  Uses several
  techniques in series. It is more expensive than other subsetting
  procedures, but often produces better results. See
  Cudd_SubsetShortPaths for a description of the threshold and nvars
  parameters.  Returns a pointer to the result if successful; NULL
  otherwise.]

  SideEffects [None]

  SeeAlso     [Cudd_SubsetRemap Cudd_SubsetShortPaths Cudd_SubsetHeavyBranch
  Cudd_bddSqueeze]

******************************************************************************/
DdNode *
Cudd_SubsetCompress(
  DdManager * dd /* manager */,
  DdNode * f /* BDD whose subset is sought */,
  int  nvars /* number of variables in the support of f */,
  int  threshold /* maximum number of nodes in the subset */)
{
    DdNode *res, *tmp1, *tmp2;

    tmp1 = Cudd_SubsetShortPaths(dd, f, nvars, threshold, 0);
    if (tmp1 == NULL) return(NULL);
    cuddRef(tmp1);
    tmp2 = Cudd_RemapUnderApprox(dd,tmp1,nvars,0,1.0);
    if (tmp2 == NULL) {
705 706
        Cudd_IterDerefBdd(dd,tmp1);
        return(NULL);
Alan Mishchenko committed
707 708 709 710 711
    }
    cuddRef(tmp2);
    Cudd_IterDerefBdd(dd,tmp1);
    res = Cudd_bddSqueeze(dd,tmp2,f);
    if (res == NULL) {
712 713
        Cudd_IterDerefBdd(dd,tmp2);
        return(NULL);
Alan Mishchenko committed
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
    }
    cuddRef(res);
    Cudd_IterDerefBdd(dd,tmp2);
    cuddDeref(res);
    return(res);

} /* end of Cudd_SubsetCompress */


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

  Synopsis    [Find a dense superset of BDD <code>f</code>.]

  Description [Finds a dense superset of BDD <code>f</code>. Density is
  the ratio of number of minterms to number of nodes.  Uses several
  techniques in series. It is more expensive than other supersetting
  procedures, but often produces better results. See
  Cudd_SupersetShortPaths for a description of the threshold and nvars
  parameters.  Returns a pointer to the result if successful; NULL
  otherwise.]

  SideEffects [None]

  SeeAlso     [Cudd_SubsetCompress Cudd_SupersetRemap Cudd_SupersetShortPaths
  Cudd_SupersetHeavyBranch Cudd_bddSqueeze]

******************************************************************************/
DdNode *
Cudd_SupersetCompress(
  DdManager * dd /* manager */,
  DdNode * f /* BDD whose superset is sought */,
  int  nvars /* number of variables in the support of f */,
  int  threshold /* maximum number of nodes in the superset */)
{
    DdNode *subset;

    subset = Cudd_SubsetCompress(dd, Cudd_Not(f),nvars,threshold);

    return(Cudd_NotCond(subset, (subset != NULL)));

} /* end of Cudd_SupersetCompress */


/*---------------------------------------------------------------------------*/
/* Definition of internal functions                                          */
/*---------------------------------------------------------------------------*/


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

  Synopsis    [Performs the recursive step of Cudd_bddConstrain.]

  Description [Performs the recursive step of Cudd_bddConstrain.
  Returns a pointer to the result if successful; NULL otherwise.]

  SideEffects [None]

  SeeAlso     [Cudd_bddConstrain]

******************************************************************************/
DdNode *
cuddBddConstrainRecur(
  DdManager * dd,
  DdNode * f,
  DdNode * c)
{
    DdNode       *Fv, *Fnv, *Cv, *Cnv, *t, *e, *r;
781
    DdNode       *one, *zero;
Alan Mishchenko committed
782
    unsigned int topf, topc;
783
    int          index;
Alan Mishchenko committed
784 785 786 787 788 789 790
    int          comple = 0;

    statLine(dd);
    one = DD_ONE(dd);
    zero = Cudd_Not(one);

    /* Trivial cases. */
791 792 793 794 795
    if (c == one)               return(f);
    if (c == zero)              return(zero);
    if (Cudd_IsConstant(f))     return(f);
    if (f == c)                 return(one);
    if (f == Cudd_Not(c))       return(zero);
Alan Mishchenko committed
796 797 798

    /* Make canonical to increase the utilization of the cache. */
    if (Cudd_IsComplement(f)) {
799 800
        f = Cudd_Not(f);
        comple = 1;
Alan Mishchenko committed
801 802 803 804 805 806 807 808
    }
    /* Now f is a regular pointer to a non-constant node; c is also
    ** non-constant, but may be complemented.
    */

    /* Check the cache. */
    r = cuddCacheLookup2(dd, Cudd_bddConstrain, f, c);
    if (r != NULL) {
809
        return(Cudd_NotCond(r,comple));
Alan Mishchenko committed
810 811 812 813 814 815
    }
    
    /* Recursive step. */
    topf = dd->perm[f->index];
    topc = dd->perm[Cudd_Regular(c)->index];
    if (topf <= topc) {
816 817
        index = f->index;
        Fv = cuddT(f); Fnv = cuddE(f);
Alan Mishchenko committed
818
    } else {
819 820
        index = Cudd_Regular(c)->index;
        Fv = Fnv = f;
Alan Mishchenko committed
821 822
    }
    if (topc <= topf) {
823 824 825 826 827
        Cv = cuddT(Cudd_Regular(c)); Cnv = cuddE(Cudd_Regular(c));
        if (Cudd_IsComplement(c)) {
            Cv = Cudd_Not(Cv);
            Cnv = Cudd_Not(Cnv);
        }
Alan Mishchenko committed
828
    } else {
829
        Cv = Cnv = c;
Alan Mishchenko committed
830 831 832
    }

    if (!Cudd_IsConstant(Cv)) {
833 834 835
        t = cuddBddConstrainRecur(dd, Fv, Cv);
        if (t == NULL)
            return(NULL);
Alan Mishchenko committed
836
    } else if (Cv == one) {
837 838 839 840 841 842 843 844 845 846
        t = Fv;
    } else {            /* Cv == zero: return Fnv @ Cnv */
        if (Cnv == one) {
            r = Fnv;
        } else {
            r = cuddBddConstrainRecur(dd, Fnv, Cnv);
            if (r == NULL)
                return(NULL);
        }
        return(Cudd_NotCond(r,comple));
Alan Mishchenko committed
847 848 849 850
    }
    cuddRef(t);

    if (!Cudd_IsConstant(Cnv)) {
851 852 853 854 855
        e = cuddBddConstrainRecur(dd, Fnv, Cnv);
        if (e == NULL) {
            Cudd_IterDerefBdd(dd, t);
            return(NULL);
        }
Alan Mishchenko committed
856
    } else if (Cnv == one) {
857 858 859 860
        e = Fnv;
    } else {            /* Cnv == zero: return Fv @ Cv previously computed */
        cuddDeref(t);
        return(Cudd_NotCond(t,comple));
Alan Mishchenko committed
861 862 863 864
    }
    cuddRef(e);

    if (Cudd_IsComplement(t)) {
865 866 867 868 869 870 871 872 873
        t = Cudd_Not(t);
        e = Cudd_Not(e);
        r = (t == e) ? t : cuddUniqueInter(dd, index, t, e);
        if (r == NULL) {
            Cudd_IterDerefBdd(dd, e);
            Cudd_IterDerefBdd(dd, t);
            return(NULL);
        }
        r = Cudd_Not(r);
Alan Mishchenko committed
874
    } else {
875 876 877 878 879 880
        r = (t == e) ? t : cuddUniqueInter(dd, index, t, e);
        if (r == NULL) {
            Cudd_IterDerefBdd(dd, e);
            Cudd_IterDerefBdd(dd, t);
            return(NULL);
        }
Alan Mishchenko committed
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
    }
    cuddDeref(t);
    cuddDeref(e);

    cuddCacheInsert2(dd, Cudd_bddConstrain, f, c, r);
    return(Cudd_NotCond(r,comple));

} /* end of cuddBddConstrainRecur */


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

  Synopsis    [Performs the recursive step of Cudd_bddRestrict.]

  Description [Performs the recursive step of Cudd_bddRestrict.
  Returns the restricted BDD if successful; otherwise NULL.]

  SideEffects [None]

  SeeAlso     [Cudd_bddRestrict]

******************************************************************************/
DdNode *
cuddBddRestrictRecur(
  DdManager * dd,
  DdNode * f,
  DdNode * c)
{
909
    DdNode       *Fv, *Fnv, *Cv, *Cnv, *t, *e, *r, *one, *zero;
Alan Mishchenko committed
910
    unsigned int topf, topc;
911 912
    int          index;
    int          comple = 0;
Alan Mishchenko committed
913 914 915 916 917 918

    statLine(dd);
    one = DD_ONE(dd);
    zero = Cudd_Not(one);

    /* Trivial cases */
919 920 921 922 923
    if (c == one)               return(f);
    if (c == zero)              return(zero);
    if (Cudd_IsConstant(f))     return(f);
    if (f == c)                 return(one);
    if (f == Cudd_Not(c))       return(zero);
Alan Mishchenko committed
924 925 926

    /* Make canonical to increase the utilization of the cache. */
    if (Cudd_IsComplement(f)) {
927 928
        f = Cudd_Not(f);
        comple = 1;
Alan Mishchenko committed
929 930 931 932 933 934 935 936
    }
    /* Now f is a regular pointer to a non-constant node; c is also
    ** non-constant, but may be complemented.
    */

    /* Check the cache. */
    r = cuddCacheLookup2(dd, Cudd_bddRestrict, f, c);
    if (r != NULL) {
937
        return(Cudd_NotCond(r,comple));
Alan Mishchenko committed
938 939 940 941 942
    }

    topf = dd->perm[f->index];
    topc = dd->perm[Cudd_Regular(c)->index];

943 944
    if (topc < topf) {  /* abstract top variable from c */
        DdNode *d, *s1, *s2;
Alan Mishchenko committed
945

946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964
        /* Find complements of cofactors of c. */
        if (Cudd_IsComplement(c)) {
            s1 = cuddT(Cudd_Regular(c));
            s2 = cuddE(Cudd_Regular(c));
        } else {
            s1 = Cudd_Not(cuddT(c));
            s2 = Cudd_Not(cuddE(c));
        }
        /* Take the OR by applying DeMorgan. */
        d = cuddBddAndRecur(dd, s1, s2);
        if (d == NULL) return(NULL);
        d = Cudd_Not(d);
        cuddRef(d);
        r = cuddBddRestrictRecur(dd, f, d);
        if (r == NULL) {
            Cudd_IterDerefBdd(dd, d);
            return(NULL);
        }
        cuddRef(r);
Alan Mishchenko committed
965
        Cudd_IterDerefBdd(dd, d);
966 967 968
        cuddCacheInsert2(dd, Cudd_bddRestrict, f, c, r);
        cuddDeref(r);
        return(Cudd_NotCond(r,comple));
Alan Mishchenko committed
969 970 971 972 973 974
    }

    /* Recursive step. Here topf <= topc. */
    index = f->index;
    Fv = cuddT(f); Fnv = cuddE(f);
    if (topc == topf) {
975 976 977 978 979
        Cv = cuddT(Cudd_Regular(c)); Cnv = cuddE(Cudd_Regular(c));
        if (Cudd_IsComplement(c)) {
            Cv = Cudd_Not(Cv);
            Cnv = Cudd_Not(Cnv);
        }
Alan Mishchenko committed
980
    } else {
981
        Cv = Cnv = c;
Alan Mishchenko committed
982 983 984
    }

    if (!Cudd_IsConstant(Cv)) {
985 986
        t = cuddBddRestrictRecur(dd, Fv, Cv);
        if (t == NULL) return(NULL);
Alan Mishchenko committed
987
    } else if (Cv == one) {
988 989 990 991 992 993 994 995 996
        t = Fv;
    } else {            /* Cv == zero: return(Fnv @ Cnv) */
        if (Cnv == one) {
            r = Fnv;
        } else {
            r = cuddBddRestrictRecur(dd, Fnv, Cnv);
            if (r == NULL) return(NULL);
        }
        return(Cudd_NotCond(r,comple));
Alan Mishchenko committed
997 998 999 1000
    }
    cuddRef(t);

    if (!Cudd_IsConstant(Cnv)) {
1001 1002 1003 1004 1005
        e = cuddBddRestrictRecur(dd, Fnv, Cnv);
        if (e == NULL) {
            Cudd_IterDerefBdd(dd, t);
            return(NULL);
        }
Alan Mishchenko committed
1006
    } else if (Cnv == one) {
1007 1008 1009 1010
        e = Fnv;
    } else {            /* Cnv == zero: return (Fv @ Cv) previously computed */
        cuddDeref(t);
        return(Cudd_NotCond(t,comple));
Alan Mishchenko committed
1011 1012 1013 1014
    }
    cuddRef(e);

    if (Cudd_IsComplement(t)) {
1015 1016 1017 1018 1019 1020 1021 1022 1023
        t = Cudd_Not(t);
        e = Cudd_Not(e);
        r = (t == e) ? t : cuddUniqueInter(dd, index, t, e);
        if (r == NULL) {
            Cudd_IterDerefBdd(dd, e);
            Cudd_IterDerefBdd(dd, t);
            return(NULL);
        }
        r = Cudd_Not(r);
Alan Mishchenko committed
1024
    } else {
1025 1026 1027 1028 1029 1030
        r = (t == e) ? t : cuddUniqueInter(dd, index, t, e);
        if (r == NULL) {
            Cudd_IterDerefBdd(dd, e);
            Cudd_IterDerefBdd(dd, t);
            return(NULL);
        }
Alan Mishchenko committed
1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042
    }
    cuddDeref(t);
    cuddDeref(e);

    cuddCacheInsert2(dd, Cudd_bddRestrict, f, c, r);
    return(Cudd_NotCond(r,comple));

} /* end of cuddBddRestrictRecur */


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

1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183
  Synopsis [Implements the recursive step of Cudd_bddAnd.]

  Description [Implements the recursive step of Cudd_bddNPAnd.
  Returns a pointer to the result is successful; NULL otherwise.]

  SideEffects [None]

  SeeAlso     [Cudd_bddNPAnd]

******************************************************************************/
DdNode *
cuddBddNPAndRecur(
  DdManager * manager,
  DdNode * f,
  DdNode * g)
{
    DdNode *F, *ft, *fe, *G, *gt, *ge;
    DdNode *one, *r, *t, *e;
    unsigned int topf, topg, index;

    statLine(manager);
    one = DD_ONE(manager);

    /* Terminal cases. */
    F = Cudd_Regular(f);
    G = Cudd_Regular(g);
    if (F == G) {
        if (f == g) return(one);
        else return(Cudd_Not(one));
    }
    if (G == one) {
        if (g == one) return(f);
        else return(g);
    }
    if (F == one) {
        return(f);
    }

    /* At this point f and g are not constant. */
    /* Check cache. */
    if (F->ref != 1 || G->ref != 1) {
        r = cuddCacheLookup2(manager, Cudd_bddNPAnd, f, g);
        if (r != NULL) return(r);
    }

    /* Here we can skip the use of cuddI, because the operands are known
    ** to be non-constant.
    */
    topf = manager->perm[F->index];
    topg = manager->perm[G->index];

    if (topg < topf) {  /* abstract top variable from g */
        DdNode *d;

        /* Find complements of cofactors of g. */
        if (Cudd_IsComplement(g)) {
            gt = cuddT(G);
            ge = cuddE(G);
        } else {
            gt = Cudd_Not(cuddT(g));
            ge = Cudd_Not(cuddE(g));
        }
        /* Take the OR by applying DeMorgan. */
        d = cuddBddAndRecur(manager, gt, ge);
        if (d == NULL) return(NULL);
        d = Cudd_Not(d);
        cuddRef(d);
        r = cuddBddNPAndRecur(manager, f, d);
        if (r == NULL) {
            Cudd_IterDerefBdd(manager, d);
            return(NULL);
        }
        cuddRef(r);
        Cudd_IterDerefBdd(manager, d);
        cuddCacheInsert2(manager, Cudd_bddNPAnd, f, g, r);
        cuddDeref(r);
        return(r);
    }

    /* Compute cofactors. */
    index = F->index;
    ft = cuddT(F);
    fe = cuddE(F);
    if (Cudd_IsComplement(f)) {
      ft = Cudd_Not(ft);
      fe = Cudd_Not(fe);
    }

    if (topg == topf) {
        gt = cuddT(G);
        ge = cuddE(G);
        if (Cudd_IsComplement(g)) {
            gt = Cudd_Not(gt);
            ge = Cudd_Not(ge);
        }
    } else {
        gt = ge = g;
    }

    t = cuddBddAndRecur(manager, ft, gt);
    if (t == NULL) return(NULL);
    cuddRef(t);

    e = cuddBddAndRecur(manager, fe, ge);
    if (e == NULL) {
        Cudd_IterDerefBdd(manager, t);
        return(NULL);
    }
    cuddRef(e);

    if (t == e) {
        r = t;
    } else {
        if (Cudd_IsComplement(t)) {
            r = cuddUniqueInter(manager,(int)index,Cudd_Not(t),Cudd_Not(e));
            if (r == NULL) {
                Cudd_IterDerefBdd(manager, t);
                Cudd_IterDerefBdd(manager, e);
                return(NULL);
            }
            r = Cudd_Not(r);
        } else {
            r = cuddUniqueInter(manager,(int)index,t,e);
            if (r == NULL) {
                Cudd_IterDerefBdd(manager, t);
                Cudd_IterDerefBdd(manager, e);
                return(NULL);
            }
        }
    }
    cuddDeref(e);
    cuddDeref(t);
    if (F->ref != 1 || G->ref != 1)
        cuddCacheInsert2(manager, Cudd_bddNPAnd, f, g, r);
    return(r);

} /* end of cuddBddNPAndRecur */


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

Alan Mishchenko committed
1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200
  Synopsis    [Performs the recursive step of Cudd_addConstrain.]

  Description [Performs the recursive step of Cudd_addConstrain.
  Returns a pointer to the result if successful; NULL otherwise.]

  SideEffects [None]

  SeeAlso     [Cudd_addConstrain]

******************************************************************************/
DdNode *
cuddAddConstrainRecur(
  DdManager * dd,
  DdNode * f,
  DdNode * c)
{
    DdNode       *Fv, *Fnv, *Cv, *Cnv, *t, *e, *r;
1201
    DdNode       *one, *zero;
Alan Mishchenko committed
1202
    unsigned int topf, topc;
1203
    int          index;
Alan Mishchenko committed
1204 1205 1206 1207 1208 1209

    statLine(dd);
    one = DD_ONE(dd);
    zero = DD_ZERO(dd);

    /* Trivial cases. */
1210 1211 1212 1213
    if (c == one)               return(f);
    if (c == zero)              return(zero);
    if (Cudd_IsConstant(f))     return(f);
    if (f == c)                 return(one);
Alan Mishchenko committed
1214 1215 1216 1217 1218 1219

    /* Now f and c are non-constant. */

    /* Check the cache. */
    r = cuddCacheLookup2(dd, Cudd_addConstrain, f, c);
    if (r != NULL) {
1220
        return(r);
Alan Mishchenko committed
1221 1222 1223 1224 1225 1226
    }
    
    /* Recursive step. */
    topf = dd->perm[f->index];
    topc = dd->perm[c->index];
    if (topf <= topc) {
1227 1228
        index = f->index;
        Fv = cuddT(f); Fnv = cuddE(f);
Alan Mishchenko committed
1229
    } else {
1230 1231
        index = c->index;
        Fv = Fnv = f;
Alan Mishchenko committed
1232 1233
    }
    if (topc <= topf) {
1234
        Cv = cuddT(c); Cnv = cuddE(c);
Alan Mishchenko committed
1235
    } else {
1236
        Cv = Cnv = c;
Alan Mishchenko committed
1237 1238 1239
    }

    if (!Cudd_IsConstant(Cv)) {
1240 1241 1242
        t = cuddAddConstrainRecur(dd, Fv, Cv);
        if (t == NULL)
            return(NULL);
Alan Mishchenko committed
1243
    } else if (Cv == one) {
1244 1245 1246 1247 1248 1249 1250 1251 1252 1253
        t = Fv;
    } else {            /* Cv == zero: return Fnv @ Cnv */
        if (Cnv == one) {
            r = Fnv;
        } else {
            r = cuddAddConstrainRecur(dd, Fnv, Cnv);
            if (r == NULL)
                return(NULL);
        }
        return(r);
Alan Mishchenko committed
1254 1255 1256 1257
    }
    cuddRef(t);

    if (!Cudd_IsConstant(Cnv)) {
1258 1259 1260 1261 1262
        e = cuddAddConstrainRecur(dd, Fnv, Cnv);
        if (e == NULL) {
            Cudd_RecursiveDeref(dd, t);
            return(NULL);
        }
Alan Mishchenko committed
1263
    } else if (Cnv == one) {
1264 1265 1266 1267
        e = Fnv;
    } else {            /* Cnv == zero: return Fv @ Cv previously computed */
        cuddDeref(t);
        return(t);
Alan Mishchenko committed
1268 1269 1270 1271 1272
    }
    cuddRef(e);

    r = (t == e) ? t : cuddUniqueInter(dd, index, t, e);
    if (r == NULL) {
1273 1274 1275
        Cudd_RecursiveDeref(dd, e);
        Cudd_RecursiveDeref(dd, t);
        return(NULL);
Alan Mishchenko committed
1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303
    }
    cuddDeref(t);
    cuddDeref(e);

    cuddCacheInsert2(dd, Cudd_addConstrain, f, c, r);
    return(r);

} /* end of cuddAddConstrainRecur */


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

  Synopsis    [Performs the recursive step of Cudd_addRestrict.]

  Description [Performs the recursive step of Cudd_addRestrict.
  Returns the restricted ADD if successful; otherwise NULL.]

  SideEffects [None]

  SeeAlso     [Cudd_addRestrict]

******************************************************************************/
DdNode *
cuddAddRestrictRecur(
  DdManager * dd,
  DdNode * f,
  DdNode * c)
{
1304
    DdNode       *Fv, *Fnv, *Cv, *Cnv, *t, *e, *r, *one, *zero;
Alan Mishchenko committed
1305
    unsigned int topf, topc;
1306
    int          index;
Alan Mishchenko committed
1307 1308 1309 1310 1311 1312

    statLine(dd);
    one = DD_ONE(dd);
    zero = DD_ZERO(dd);

    /* Trivial cases */
1313 1314 1315 1316
    if (c == one)               return(f);
    if (c == zero)              return(zero);
    if (Cudd_IsConstant(f))     return(f);
    if (f == c)                 return(one);
Alan Mishchenko committed
1317 1318 1319 1320 1321 1322

    /* Now f and c are non-constant. */

    /* Check the cache. */
    r = cuddCacheLookup2(dd, Cudd_addRestrict, f, c);
    if (r != NULL) {
1323
        return(r);
Alan Mishchenko committed
1324 1325 1326 1327 1328
    }

    topf = dd->perm[f->index];
    topc = dd->perm[c->index];

1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344
    if (topc < topf) {  /* abstract top variable from c */
        DdNode *d, *s1, *s2;

        /* Find cofactors of c. */
        s1 = cuddT(c);
        s2 = cuddE(c);
        /* Take the OR by applying DeMorgan. */
        d = cuddAddApplyRecur(dd, Cudd_addOr, s1, s2);
        if (d == NULL) return(NULL);
        cuddRef(d);
        r = cuddAddRestrictRecur(dd, f, d);
        if (r == NULL) {
            Cudd_RecursiveDeref(dd, d);
            return(NULL);
        }
        cuddRef(r);
Alan Mishchenko committed
1345
        Cudd_RecursiveDeref(dd, d);
1346 1347 1348
        cuddCacheInsert2(dd, Cudd_addRestrict, f, c, r);
        cuddDeref(r);
        return(r);
Alan Mishchenko committed
1349 1350 1351 1352 1353 1354
    }

    /* Recursive step. Here topf <= topc. */
    index = f->index;
    Fv = cuddT(f); Fnv = cuddE(f);
    if (topc == topf) {
1355
        Cv = cuddT(c); Cnv = cuddE(c);
Alan Mishchenko committed
1356
    } else {
1357
        Cv = Cnv = c;
Alan Mishchenko committed
1358 1359 1360
    }

    if (!Cudd_IsConstant(Cv)) {
1361 1362
        t = cuddAddRestrictRecur(dd, Fv, Cv);
        if (t == NULL) return(NULL);
Alan Mishchenko committed
1363
    } else if (Cv == one) {
1364 1365 1366 1367 1368 1369 1370 1371 1372
        t = Fv;
    } else {            /* Cv == zero: return(Fnv @ Cnv) */
        if (Cnv == one) {
            r = Fnv;
        } else {
            r = cuddAddRestrictRecur(dd, Fnv, Cnv);
            if (r == NULL) return(NULL);
        }
        return(r);
Alan Mishchenko committed
1373 1374 1375 1376
    }
    cuddRef(t);

    if (!Cudd_IsConstant(Cnv)) {
1377 1378 1379 1380 1381
        e = cuddAddRestrictRecur(dd, Fnv, Cnv);
        if (e == NULL) {
            Cudd_RecursiveDeref(dd, t);
            return(NULL);
        }
Alan Mishchenko committed
1382
    } else if (Cnv == one) {
1383 1384 1385 1386
        e = Fnv;
    } else {            /* Cnv == zero: return (Fv @ Cv) previously computed */
        cuddDeref(t);
        return(t);
Alan Mishchenko committed
1387 1388 1389 1390 1391
    }
    cuddRef(e);

    r = (t == e) ? t : cuddUniqueInter(dd, index, t, e);
    if (r == NULL) {
1392 1393 1394
        Cudd_RecursiveDeref(dd, e);
        Cudd_RecursiveDeref(dd, t);
        return(NULL);
Alan Mishchenko committed
1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430
    }
    cuddDeref(t);
    cuddDeref(e);

    cuddCacheInsert2(dd, Cudd_addRestrict, f, c, r);
    return(r);

} /* end of cuddAddRestrictRecur */



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

  Synopsis    [Performs safe minimization of a BDD.]

  Description [Performs safe minimization of a BDD. Given the BDD
  <code>f</code> of a function to be minimized and a BDD
  <code>c</code> representing the care set, Cudd_bddLICompaction
  produces the BDD of a function that agrees with <code>f</code>
  wherever <code>c</code> is 1.  Safe minimization means that the size
  of the result is guaranteed not to exceed the size of
  <code>f</code>. This function is based on the DAC97 paper by Hong et
  al..  Returns a pointer to the result if successful; NULL
  otherwise.]

  SideEffects [None]

  SeeAlso     [Cudd_bddLICompaction]

******************************************************************************/
DdNode *
cuddBddLICompaction(
  DdManager * dd /* manager */,
  DdNode * f /* function to be minimized */,
  DdNode * c /* constraint (care set) */)
{
1431
    st__table *marktable, *markcache, *buildcache;
Alan Mishchenko committed
1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447
    DdNode *res, *zero;

    zero = Cudd_Not(DD_ONE(dd));
    if (c == zero) return(zero);

    /* We need to use local caches for both steps of this operation.
    ** The results of the edge marking step are only valid as long as the
    ** edge markings themselves are available. However, the edge markings
    ** are lost at the end of one invocation of Cudd_bddLICompaction.
    ** Hence, the cache entries for the edge marking step must be
    ** invalidated at the end of this function.
    ** For the result of the building step we argue as follows. The result
    ** for a node and a given constrain depends on the BDD in which the node
    ** appears. Hence, the same node and constrain may give different results
    ** in successive invocations.
    */
1448
    marktable = st__init_table( st__ptrcmp, st__ptrhash);
Alan Mishchenko committed
1449
    if (marktable == NULL) {
1450
        return(NULL);
Alan Mishchenko committed
1451
    }
1452
    markcache = st__init_table(MarkCacheCompare,MarkCacheHash);
Alan Mishchenko committed
1453
    if (markcache == NULL) {
1454
        st__free_table(marktable);
1455
        return(NULL);
Alan Mishchenko committed
1456 1457
    }
    if (cuddBddLICMarkEdges(dd,f,c,marktable,markcache) == CUDD_OUT_OF_MEM) {
1458 1459 1460
        st__foreach(markcache, MarkCacheCleanUp, NULL);
        st__free_table(marktable);
        st__free_table(markcache);
1461
        return(NULL);
Alan Mishchenko committed
1462
    }
1463 1464 1465
    st__foreach(markcache, MarkCacheCleanUp, NULL);
    st__free_table(markcache);
    buildcache = st__init_table( st__ptrcmp, st__ptrhash);
Alan Mishchenko committed
1466
    if (buildcache == NULL) {
1467
        st__free_table(marktable);
1468
        return(NULL);
Alan Mishchenko committed
1469 1470
    }
    res = cuddBddLICBuildResult(dd,f,buildcache,marktable);
1471 1472
    st__free_table(buildcache);
    st__free_table(marktable);
Alan Mishchenko committed
1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511
    return(res);

} /* end of cuddBddLICompaction */


/*---------------------------------------------------------------------------*/
/* Definition of static functions                                            */
/*---------------------------------------------------------------------------*/


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

  Synopsis    [Performs the recursive step of Cudd_bddConstrainDecomp.]

  Description [Performs the recursive step of Cudd_bddConstrainDecomp.
  Returns f super (i) if successful; otherwise NULL.]

  SideEffects [None]

  SeeAlso     [Cudd_bddConstrainDecomp]

******************************************************************************/
static int
cuddBddConstrainDecomp(
  DdManager * dd,
  DdNode * f,
  DdNode ** decomp)
{
    DdNode *F, *fv, *fvn;
    DdNode *fAbs;
    DdNode *result;
    int ok;

    if (Cudd_IsConstant(f)) return(1);
    /* Compute complements of cofactors. */
    F = Cudd_Regular(f);
    fv = cuddT(F);
    fvn = cuddE(F);
    if (F == f) {
1512 1513
        fv = Cudd_Not(fv);
        fvn = Cudd_Not(fvn);
Alan Mishchenko committed
1514 1515 1516 1517
    }
    /* Compute abstraction of top variable. */
    fAbs = cuddBddAndRecur(dd, fv, fvn);
    if (fAbs == NULL) {
1518
        return(0);
Alan Mishchenko committed
1519 1520 1521 1522 1523 1524 1525
    }
    cuddRef(fAbs);
    fAbs = Cudd_Not(fAbs);
    /* Recursively find the next abstraction and the components of the
    ** decomposition. */
    ok = cuddBddConstrainDecomp(dd, fAbs, decomp);
    if (ok == 0) {
1526 1527
        Cudd_IterDerefBdd(dd,fAbs);
        return(0);
Alan Mishchenko committed
1528 1529 1530 1531 1532
    }
    /* Compute the component of the decomposition corresponding to the
    ** top variable and store it in the decomposition array. */
    result = cuddBddConstrainRecur(dd, f, fAbs);
    if (result == NULL) {
1533 1534
        Cudd_IterDerefBdd(dd,fAbs);
        return(0);
Alan Mishchenko committed
1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572
    }
    cuddRef(result);
    decomp[F->index] = result;
    Cudd_IterDerefBdd(dd, fAbs);
    return(1);

} /* end of cuddBddConstrainDecomp */


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

  Synopsis    [Performs the recursive step of Cudd_bddCharToVect.]

  Description [Performs the recursive step of Cudd_bddCharToVect.
  This function maintains the invariant that f is non-zero.
  Returns the i-th component of the vector if successful; otherwise NULL.]

  SideEffects [None]

  SeeAlso     [Cudd_bddCharToVect]

******************************************************************************/
static DdNode *
cuddBddCharToVect(
  DdManager * dd,
  DdNode * f,
  DdNode * x)
{
    unsigned int topf;
    unsigned int level;
    int comple;

    DdNode *one, *zero, *res, *F, *fT, *fE, *T, *E;

    statLine(dd);
    /* Check the cache. */
    res = cuddCacheLookup2(dd, cuddBddCharToVect, f, x);
    if (res != NULL) {
1573
        return(res);
Alan Mishchenko committed
1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590
    }

    F = Cudd_Regular(f);

    topf = cuddI(dd,F->index);
    level = dd->perm[x->index];

    if (topf > level) return(x);

    one = DD_ONE(dd);
    zero = Cudd_Not(one);

    comple = F != f;
    fT = Cudd_NotCond(cuddT(F),comple);
    fE = Cudd_NotCond(cuddE(F),comple);

    if (topf == level) {
1591 1592 1593
        if (fT == zero) return(zero);
        if (fE == zero) return(one);
        return(x);
Alan Mishchenko committed
1594 1595 1596 1597 1598 1599 1600 1601
    }

    /* Here topf < level. */
    if (fT == zero) return(cuddBddCharToVect(dd, fE, x));
    if (fE == zero) return(cuddBddCharToVect(dd, fT, x));

    T = cuddBddCharToVect(dd, fT, x);
    if (T == NULL) {
1602
        return(NULL);
Alan Mishchenko committed
1603 1604 1605 1606
    }
    cuddRef(T);
    E = cuddBddCharToVect(dd, fE, x);
    if (E == NULL) {
1607 1608
        Cudd_IterDerefBdd(dd,T);
        return(NULL);
Alan Mishchenko committed
1609 1610 1611 1612
    }
    cuddRef(E);
    res = cuddBddIteRecur(dd, dd->vars[F->index], T, E);
    if (res == NULL) {
1613 1614 1615
        Cudd_IterDerefBdd(dd,T);
        Cudd_IterDerefBdd(dd,E);
        return(NULL);
Alan Mishchenko committed
1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642
    }
    cuddDeref(T);
    cuddDeref(E);
    cuddCacheInsert2(dd, cuddBddCharToVect, f, x, res);
    return(res);

} /* end of cuddBddCharToVect */


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

  Synopsis    [Performs the edge marking step of Cudd_bddLICompaction.]

  Description [Performs the edge marking step of Cudd_bddLICompaction.
  Returns the LUB of the markings of the two outgoing edges of <code>f</code>
  if successful; otherwise CUDD_OUT_OF_MEM.]

  SideEffects [None]

  SeeAlso     [Cudd_bddLICompaction cuddBddLICBuildResult]

******************************************************************************/
static int
cuddBddLICMarkEdges(
  DdManager * dd,
  DdNode * f,
  DdNode * c,
1643 1644
  st__table * table,
  st__table * cache)
Alan Mishchenko committed
1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669
{
    DdNode *Fv, *Fnv, *Cv, *Cnv;
    DdNode *one, *zero;
    unsigned int topf, topc;
    int comple;
    int resT, resE, res, retval;
    char **slot;
    MarkCacheKey *key;

    one = DD_ONE(dd);
    zero = Cudd_Not(one);

    /* Terminal cases. */
    if (c == zero) return(DD_LIC_DC);
    if (f == one)  return(DD_LIC_1);
    if (f == zero) return(DD_LIC_0);

    /* Make canonical to increase the utilization of the cache. */
    comple = Cudd_IsComplement(f);
    f = Cudd_Regular(f);
    /* Now f is a regular pointer to a non-constant node; c may be
    ** constant, or it may be complemented.
    */

    /* Check the cache. */
Alan Mishchenko committed
1670
    key = ABC_ALLOC(MarkCacheKey, 1);
Alan Mishchenko committed
1671
    if (key == NULL) {
1672 1673
        dd->errorCode = CUDD_MEMORY_OUT;
        return(CUDD_OUT_OF_MEM);
Alan Mishchenko committed
1674 1675
    }
    key->f = f; key->c = c;
1676
    if ( st__lookup_int(cache, (char *)key, &res)) {
1677 1678 1679 1680 1681 1682
        ABC_FREE(key);
        if (comple) {
            if (res == DD_LIC_0) res = DD_LIC_1;
            else if (res == DD_LIC_1) res = DD_LIC_0;
        }
        return(res);
Alan Mishchenko committed
1683 1684 1685 1686 1687 1688
    }

    /* Recursive step. */
    topf = dd->perm[f->index];
    topc = cuddI(dd,Cudd_Regular(c)->index);
    if (topf <= topc) {
1689
        Fv = cuddT(f); Fnv = cuddE(f);
Alan Mishchenko committed
1690
    } else {
1691
        Fv = Fnv = f;
Alan Mishchenko committed
1692 1693
    }
    if (topc <= topf) {
1694 1695 1696 1697 1698 1699
        /* We know that c is not constant because f is not. */
        Cv = cuddT(Cudd_Regular(c)); Cnv = cuddE(Cudd_Regular(c));
        if (Cudd_IsComplement(c)) {
            Cv = Cudd_Not(Cv);
            Cnv = Cudd_Not(Cnv);
        }
Alan Mishchenko committed
1700
    } else {
1701
        Cv = Cnv = c;
Alan Mishchenko committed
1702 1703 1704 1705
    }

    resT = cuddBddLICMarkEdges(dd, Fv, Cv, table, cache);
    if (resT == CUDD_OUT_OF_MEM) {
1706 1707
        ABC_FREE(key);
        return(CUDD_OUT_OF_MEM);
Alan Mishchenko committed
1708 1709 1710
    }
    resE = cuddBddLICMarkEdges(dd, Fnv, Cnv, table, cache);
    if (resE == CUDD_OUT_OF_MEM) {
1711 1712
        ABC_FREE(key);
        return(CUDD_OUT_OF_MEM);
Alan Mishchenko committed
1713 1714 1715 1716
    }

    /* Update edge markings. */
    if (topf <= topc) {
1717
        retval = st__find_or_add(table, (char *)f, (char ***)&slot);
1718 1719 1720 1721 1722 1723 1724 1725
        if (retval == 0) {
            *slot = (char *) (ptrint)((resT << 2) | resE);
        } else if (retval == 1) {
            *slot = (char *) (ptrint)((int)((ptrint) *slot) | (resT << 2) | resE);
        } else {
            ABC_FREE(key);
            return(CUDD_OUT_OF_MEM);
        }
Alan Mishchenko committed
1726 1727 1728 1729
    }

    /* Cache result. */
    res = resT | resE;
1730
    if ( st__insert(cache, (char *)key, (char *)(ptrint)res) == st__OUT_OF_MEM) {
1731 1732
        ABC_FREE(key);
        return(CUDD_OUT_OF_MEM);
Alan Mishchenko committed
1733 1734 1735 1736
    }

    /* Take into account possible complementation. */
    if (comple) {
1737 1738
        if (res == DD_LIC_0) res = DD_LIC_1;
        else if (res == DD_LIC_1) res = DD_LIC_0;
Alan Mishchenko committed
1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760
    }
    return(res);

} /* end of cuddBddLICMarkEdges */


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

  Synopsis    [Builds the result of Cudd_bddLICompaction.]

  Description [Builds the results of Cudd_bddLICompaction.
  Returns a pointer to the minimized BDD if successful; otherwise NULL.]

  SideEffects [None]

  SeeAlso     [Cudd_bddLICompaction cuddBddLICMarkEdges]

******************************************************************************/
static DdNode *
cuddBddLICBuildResult(
  DdManager * dd,
  DdNode * f,
1761 1762
  st__table * cache,
  st__table * table)
Alan Mishchenko committed
1763 1764 1765
{
    DdNode *Fv, *Fnv, *r, *t, *e;
    DdNode *one, *zero;
1766
    int index;
Alan Mishchenko committed
1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778
    int comple;
    int markT, markE, markings;

    one = DD_ONE(dd);
    zero = Cudd_Not(one);

    if (Cudd_IsConstant(f)) return(f);
    /* Make canonical to increase the utilization of the cache. */
    comple = Cudd_IsComplement(f);
    f = Cudd_Regular(f);

    /* Check the cache. */
1779
    if ( st__lookup(cache, (const char *)f, (char **)&r)) {
1780
        return(Cudd_NotCond(r,comple));
Alan Mishchenko committed
1781 1782 1783
    }

    /* Retrieve the edge markings. */
1784
    if ( st__lookup_int(table, (char *)f, &markings) == 0)
1785
        return(NULL);
Alan Mishchenko committed
1786 1787 1788 1789 1790 1791 1792
    markT = markings >> 2;
    markE = markings & 3;

    index = f->index;
    Fv = cuddT(f); Fnv = cuddE(f);

    if (markT == DD_LIC_NL) {
1793 1794 1795 1796
        t = cuddBddLICBuildResult(dd,Fv,cache,table);
        if (t == NULL) {
            return(NULL);
        }
Alan Mishchenko committed
1797
    } else if (markT == DD_LIC_1) {
1798
        t = one;
Alan Mishchenko committed
1799
    } else {
1800
        t = zero;
Alan Mishchenko committed
1801 1802 1803
    }
    cuddRef(t);
    if (markE == DD_LIC_NL) {
1804 1805 1806 1807 1808
        e = cuddBddLICBuildResult(dd,Fnv,cache,table);
        if (e == NULL) {
            Cudd_IterDerefBdd(dd,t);
            return(NULL);
        }
Alan Mishchenko committed
1809
    } else if (markE == DD_LIC_1) {
1810
        e = one;
Alan Mishchenko committed
1811
    } else {
1812
        e = zero;
Alan Mishchenko committed
1813 1814 1815 1816
    }
    cuddRef(e);

    if (markT == DD_LIC_DC && markE != DD_LIC_DC) {
1817
        r = e;
Alan Mishchenko committed
1818
    } else if (markT != DD_LIC_DC && markE == DD_LIC_DC) {
1819
        r = t;
Alan Mishchenko committed
1820
    } else {
1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837
        if (Cudd_IsComplement(t)) {
            t = Cudd_Not(t);
            e = Cudd_Not(e);
            r = (t == e) ? t : cuddUniqueInter(dd, index, t, e);
            if (r == NULL) {
                Cudd_IterDerefBdd(dd, e);
                Cudd_IterDerefBdd(dd, t);
                return(NULL);
            }
            r = Cudd_Not(r);
        } else {
            r = (t == e) ? t : cuddUniqueInter(dd, index, t, e);
            if (r == NULL) {
                Cudd_IterDerefBdd(dd, e);
                Cudd_IterDerefBdd(dd, t);
                return(NULL);
            }
Alan Mishchenko committed
1838 1839 1840 1841 1842
        }
    }
    cuddDeref(t);
    cuddDeref(e);

1843
    if ( st__insert(cache, (char *)f, (char *)r) == st__OUT_OF_MEM) {
1844 1845 1846
        cuddRef(r);
        Cudd_IterDerefBdd(dd,r);
        return(NULL);
Alan Mishchenko committed
1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867
    }

    return(Cudd_NotCond(r,comple));

} /* end of cuddBddLICBuildResult */


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

  Synopsis    [Hash function for the computed table of cuddBddLICMarkEdges.]

  Description [Hash function for the computed table of
  cuddBddLICMarkEdges.  Returns the bucket number.]

  SideEffects [None]

  SeeAlso     [Cudd_bddLICompaction]

******************************************************************************/
static int
MarkCacheHash(
1868
  const char * ptr,
Alan Mishchenko committed
1869 1870 1871
  int  modulus)
{
    int val = 0;
1872
    MarkCacheKey *entry;
Alan Mishchenko committed
1873

1874
    entry = (MarkCacheKey *) ptr;
Alan Mishchenko committed
1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919

    val = (int) (ptrint) entry->f;
    val = val * 997 + (int) (ptrint) entry->c;

    return ((val < 0) ? -val : val) % modulus;

} /* end of MarkCacheHash */


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

  Synopsis    [Comparison function for the computed table of
  cuddBddLICMarkEdges.]

  Description [Comparison function for the computed table of
  cuddBddLICMarkEdges. Returns 0 if the two nodes of the key are equal; 1
  otherwise.]

  SideEffects [None]

  SeeAlso     [Cudd_bddLICompaction]

******************************************************************************/
static int
MarkCacheCompare(
  const char * ptr1,
  const char * ptr2)
{
    MarkCacheKey *entry1, *entry2;

    entry1 = (MarkCacheKey *) ptr1;
    entry2 = (MarkCacheKey *) ptr2;
    
    return((entry1->f != entry2->f) || (entry1->c != entry2->c));

} /* end of MarkCacheCompare */



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

  Synopsis    [Frees memory associated with computed table of
  cuddBddLICMarkEdges.]

  Description [Frees memory associated with computed table of
1920
  cuddBddLICMarkEdges. Returns st__CONTINUE.]
Alan Mishchenko committed
1921 1922 1923 1924 1925 1926

  SideEffects [None]

  SeeAlso     [Cudd_bddLICompaction]

******************************************************************************/
1927
static enum st__retval
Alan Mishchenko committed
1928 1929 1930 1931 1932 1933 1934 1935
MarkCacheCleanUp(
  char * key,
  char * value,
  char * arg)
{
    MarkCacheKey *entry;

    entry = (MarkCacheKey *) key;
Alan Mishchenko committed
1936
    ABC_FREE(entry);
1937
    return st__CONTINUE;
Alan Mishchenko committed
1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973

} /* end of MarkCacheCleanUp */


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

  Synopsis    [Performs the recursive step of Cudd_bddSqueeze.]

  Description [Performs the recursive step of Cudd_bddSqueeze.  This
  procedure exploits the fact that if we complement and swap the
  bounds of the interval we obtain a valid solution by taking the
  complement of the solution to the original problem. Therefore, we
  can enforce the condition that the upper bound is always regular.
  Returns a pointer to the result if successful; NULL otherwise.]

  SideEffects [None]

  SeeAlso     [Cudd_bddSqueeze]

******************************************************************************/
static DdNode *
cuddBddSqueeze(
  DdManager * dd,
  DdNode * l,
  DdNode * u)
{
    DdNode *one, *zero, *r, *lt, *le, *ut, *ue, *t, *e;
#if 0
    DdNode *ar;
#endif
    int comple = 0;
    unsigned int topu, topl;
    int index;

    statLine(dd);
    if (l == u) {
1974
        return(l);
Alan Mishchenko committed
1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987
    }
    one = DD_ONE(dd);
    zero = Cudd_Not(one);
    /* The only case when l == zero && u == one is at the top level,
    ** where returning either one or zero is OK. In all other cases
    ** the procedure will detect such a case and will perform
    ** remapping. Therefore the order in which we test l and u at this
    ** point is immaterial. */
    if (l == zero) return(l);
    if (u == one)  return(u);

    /* Make canonical to increase the utilization of the cache. */
    if (Cudd_IsComplement(u)) {
1988 1989 1990 1991 1992
        DdNode *temp;
        temp = Cudd_Not(l);
        l = Cudd_Not(u);
        u = temp;
        comple = 1;
Alan Mishchenko committed
1993 1994 1995 1996 1997 1998 1999 2000 2001
    }
    /* At this point u is regular and non-constant; l is non-constant, but
    ** may be complemented. */

    /* Here we could check the relative sizes. */

    /* Check the cache. */
    r = cuddCacheLookup2(dd, Cudd_bddSqueeze, l, u);
    if (r != NULL) {
2002
        return(Cudd_NotCond(r,comple));
Alan Mishchenko committed
2003 2004 2005 2006 2007 2008
    }

    /* Recursive step. */
    topu = dd->perm[u->index];
    topl = dd->perm[Cudd_Regular(l)->index];
    if (topu <= topl) {
2009 2010
        index = u->index;
        ut = cuddT(u); ue = cuddE(u);
Alan Mishchenko committed
2011
    } else {
2012 2013
        index = Cudd_Regular(l)->index;
        ut = ue = u;
Alan Mishchenko committed
2014 2015
    }
    if (topl <= topu) {
2016 2017 2018 2019 2020
        lt = cuddT(Cudd_Regular(l)); le = cuddE(Cudd_Regular(l));
        if (Cudd_IsComplement(l)) {
            lt = Cudd_Not(lt);
            le = Cudd_Not(le);
        }
Alan Mishchenko committed
2021
    } else {
2022
        lt = le = l;
Alan Mishchenko committed
2023 2024 2025 2026 2027
    }

    /* If one interval is contained in the other, use the smaller
    ** interval. This corresponds to one-sided matching. */
    if ((lt == zero || Cudd_bddLeq(dd,lt,le)) &&
2028 2029 2030 2031 2032
        (ut == one  || Cudd_bddLeq(dd,ue,ut))) { /* remap */
        r = cuddBddSqueeze(dd, le, ue);
        if (r == NULL)
            return(NULL);
        return(Cudd_NotCond(r,comple));
Alan Mishchenko committed
2033
    } else if ((le == zero || Cudd_bddLeq(dd,le,lt)) &&
2034 2035 2036 2037 2038
               (ue == one  || Cudd_bddLeq(dd,ut,ue))) { /* remap */
        r = cuddBddSqueeze(dd, lt, ut);
        if (r == NULL)
            return(NULL);
        return(Cudd_NotCond(r,comple));
Alan Mishchenko committed
2039
    } else if ((le == zero || Cudd_bddLeq(dd,le,Cudd_Not(ut))) &&
2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055
               (ue == one  || Cudd_bddLeq(dd,Cudd_Not(lt),ue))) { /* c-remap */
        t = cuddBddSqueeze(dd, lt, ut);
        cuddRef(t);
        if (Cudd_IsComplement(t)) {
            r = cuddUniqueInter(dd, index, Cudd_Not(t), t);
            if (r == NULL) {
                Cudd_IterDerefBdd(dd, t);
                return(NULL);
            }
            r = Cudd_Not(r);
        } else {
            r = cuddUniqueInter(dd, index, t, Cudd_Not(t));
            if (r == NULL) {
                Cudd_IterDerefBdd(dd, t);
                return(NULL);
            }
Alan Mishchenko committed
2056
        }
2057 2058 2059 2060 2061
        cuddDeref(t);
        if (r == NULL)
            return(NULL);
        cuddCacheInsert2(dd, Cudd_bddSqueeze, l, u, r);
        return(Cudd_NotCond(r,comple));
Alan Mishchenko committed
2062
    } else if ((lt == zero || Cudd_bddLeq(dd,lt,Cudd_Not(ue))) &&
2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078
               (ut == one  || Cudd_bddLeq(dd,Cudd_Not(le),ut))) { /* c-remap */
        e = cuddBddSqueeze(dd, le, ue);
        cuddRef(e);
        if (Cudd_IsComplement(e)) {
            r = cuddUniqueInter(dd, index, Cudd_Not(e), e);
            if (r == NULL) {
                Cudd_IterDerefBdd(dd, e);
                return(NULL);
            }
        } else {
            r = cuddUniqueInter(dd, index, e, Cudd_Not(e));
            if (r == NULL) {
                Cudd_IterDerefBdd(dd, e);
                return(NULL);
            }
            r = Cudd_Not(r);
Alan Mishchenko committed
2079
        }
2080 2081 2082 2083 2084
        cuddDeref(e);
        if (r == NULL)
            return(NULL);
        cuddCacheInsert2(dd, Cudd_bddSqueeze, l, u, r);
        return(Cudd_NotCond(r,comple));
Alan Mishchenko committed
2085 2086 2087 2088 2089 2090 2091 2092 2093
    }

#if 0
    /* If the two intervals intersect, take a solution from
    ** the intersection of the intervals. This guarantees that the
    ** splitting variable will not appear in the result.
    ** This approach corresponds to two-sided matching, and is very
    ** expensive. */
    if (Cudd_bddLeq(dd,lt,ue) && Cudd_bddLeq(dd,le,ut)) {
2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112
        DdNode *au, *al;
        au = cuddBddAndRecur(dd,ut,ue);
        if (au == NULL)
            return(NULL);
        cuddRef(au);
        al = cuddBddAndRecur(dd,Cudd_Not(lt),Cudd_Not(le));
        if (al == NULL) {
            Cudd_IterDerefBdd(dd,au);
            return(NULL);
        }
        cuddRef(al);
        al = Cudd_Not(al);
        ar = cuddBddSqueeze(dd, al, au);
        if (ar == NULL) {
            Cudd_IterDerefBdd(dd,au);
            Cudd_IterDerefBdd(dd,al);
            return(NULL);
        }
        cuddRef(ar);
Alan Mishchenko committed
2113 2114 2115
        Cudd_IterDerefBdd(dd,au);
        Cudd_IterDerefBdd(dd,al);
    } else {
2116
        ar = NULL;
Alan Mishchenko committed
2117 2118 2119 2120 2121
    }
#endif

    t = cuddBddSqueeze(dd, lt, ut);
    if (t == NULL) {
2122
        return(NULL);
Alan Mishchenko committed
2123 2124 2125 2126
    }
    cuddRef(t);
    e = cuddBddSqueeze(dd, le, ue);
    if (e == NULL) {
2127 2128
        Cudd_IterDerefBdd(dd,t);
        return(NULL);
Alan Mishchenko committed
2129 2130 2131 2132
    }
    cuddRef(e);

    if (Cudd_IsComplement(t)) {
2133 2134 2135 2136 2137 2138 2139 2140 2141
        t = Cudd_Not(t);
        e = Cudd_Not(e);
        r = (t == e) ? t : cuddUniqueInter(dd, index, t, e);
        if (r == NULL) {
            Cudd_IterDerefBdd(dd, e);
            Cudd_IterDerefBdd(dd, t);
            return(NULL);
        }
        r = Cudd_Not(r);
Alan Mishchenko committed
2142
    } else {
2143 2144 2145 2146 2147 2148
        r = (t == e) ? t : cuddUniqueInter(dd, index, t, e);
        if (r == NULL) {
            Cudd_IterDerefBdd(dd, e);
            Cudd_IterDerefBdd(dd, t);
            return(NULL);
        }
Alan Mishchenko committed
2149 2150 2151 2152 2153 2154 2155 2156 2157
    }
    cuddDeref(t);
    cuddDeref(e);

#if 0
    /* Check whether there is a result obtained by abstraction and whether
    ** it is better than the one obtained by recursion. */
    cuddRef(r);
    if (ar != NULL) {
2158 2159 2160 2161 2162 2163
        if (Cudd_DagSize(ar) <= Cudd_DagSize(r)) {
            Cudd_IterDerefBdd(dd, r);
            r = ar;
        } else {
            Cudd_IterDerefBdd(dd, ar);
        }
Alan Mishchenko committed
2164 2165 2166 2167 2168 2169 2170 2171
    }
    cuddDeref(r);
#endif

    cuddCacheInsert2(dd, Cudd_bddSqueeze, l, u, r);
    return(Cudd_NotCond(r,comple));

} /* end of cuddBddSqueeze */
2172 2173


2174 2175
ABC_NAMESPACE_IMPL_END