cuddGenCof.c 61.5 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
#endif

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

125 126 127
#ifdef __cplusplus
extern "C" {
#endif
Alan Mishchenko committed
128 129 130 131 132 133 134

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

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

135 136
static int cuddBddConstrainDecomp (DdManager *dd, DdNode *f, DdNode **decomp);
static DdNode * cuddBddCharToVect (DdManager *dd, DdNode *f, DdNode *x);
137 138
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);
139 140
static int MarkCacheHash (const char *ptr, int modulus);
static int MarkCacheCompare (const char *ptr1, const char *ptr2);
141
static enum st__retval MarkCacheCleanUp (char *key, char *value, char *arg);
142
static DdNode * cuddBddSqueeze (DdManager *dd, DdNode *l, DdNode *u);
Alan Mishchenko committed
143 144 145

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

146 147 148
#ifdef __cplusplus
}
#endif
Alan Mishchenko committed
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

/*---------------------------------------------------------------------------*/
/* 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 {
188 189
        dd->reordered = 0;
        res = cuddBddConstrainRecur(dd,f,c);
Alan Mishchenko committed
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
    } 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) {
233
        return(NULL);
Alan Mishchenko committed
234 235 236 237 238
    }
    cuddRef(commonSupport); cuddRef(suppF); cuddRef(suppC);
    Cudd_IterDerefBdd(dd,suppF);

    if (commonSupport == DD_ONE(dd)) {
239 240 241
        Cudd_IterDerefBdd(dd,commonSupport);
        Cudd_IterDerefBdd(dd,suppC);
        return(f);
Alan Mishchenko committed
242 243 244 245 246 247
    }
    Cudd_IterDerefBdd(dd,commonSupport);

    /* Abstract from c the variables that do not appear in f. */
    cplus = Cudd_bddExistAbstract(dd, c, suppC);
    if (cplus == NULL) {
248 249
        Cudd_IterDerefBdd(dd,suppC);
        return(NULL);
Alan Mishchenko committed
250 251 252 253 254
    }
    cuddRef(cplus);
    Cudd_IterDerefBdd(dd,suppC);

    do {
255 256
        dd->reordered = 0;
        res = cuddBddRestrictRecur(dd, f, cplus);
Alan Mishchenko committed
257 258
    } while (dd->reordered == 1);
    if (res == NULL) {
259 260
        Cudd_IterDerefBdd(dd,cplus);
        return(NULL);
Alan Mishchenko committed
261 262 263 264 265 266 267 268
    }
    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) {
269 270
        Cudd_IterDerefBdd(dd, res);
        return(f);
Alan Mishchenko committed
271
    } else {
272 273
        cuddDeref(res);
        return(res);
Alan Mishchenko committed
274 275 276 277 278 279 280
    }

} /* end of Cudd_bddRestrict */


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

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
  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
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
  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 {
344 345
        dd->reordered = 0;
        res = cuddAddConstrainRecur(dd,f,c);
Alan Mishchenko committed
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 372 373 374 375 376 377 378 379
    } 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
380
    decomp = ABC_ALLOC(DdNode *,dd->size);
Alan Mishchenko committed
381
    if (decomp == NULL) {
382 383
        dd->errorCode = CUDD_MEMORY_OUT;
        return(NULL);
Alan Mishchenko committed
384 385
    }
    for (i = 0; i < dd->size; i++) {
386
        decomp[i] = NULL;
Alan Mishchenko committed
387 388
    }
    do {
389 390 391 392 393 394 395
        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
396
        }
397
        res = cuddBddConstrainDecomp(dd,f,decomp);
Alan Mishchenko committed
398 399
    } while (dd->reordered == 1);
    if (res == 0) {
400 401
        ABC_FREE(decomp);
        return(NULL);
Alan Mishchenko committed
402 403 404
    }
    /* Missing components are constant ones. */
    for (i = 0; i < dd->size; i++) {
405 406 407 408
        if (decomp[i] == NULL) {
            decomp[i] = DD_ONE(dd);
            cuddRef(decomp[i]);
        }
Alan Mishchenko committed
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 436 437 438 439 440 441 442 443
    }
    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) {
444
        return(NULL);
Alan Mishchenko committed
445 446 447 448
    }
    cuddRef(supp_f);
    supp_c = Cudd_Support(dd, c);
    if (supp_c == NULL) {
449 450
        Cudd_RecursiveDeref(dd,supp_f);
        return(NULL);
Alan Mishchenko committed
451 452 453 454
    }
    cuddRef(supp_c);
    commonSupport = Cudd_bddLiteralSetIntersection(dd, supp_f, supp_c);
    if (commonSupport == NULL) {
455 456 457
        Cudd_RecursiveDeref(dd,supp_f);
        Cudd_RecursiveDeref(dd,supp_c);
        return(NULL);
Alan Mishchenko committed
458 459 460 461 462 463 464 465
    }
    cuddRef(commonSupport);
    Cudd_RecursiveDeref(dd,supp_f);
    Cudd_RecursiveDeref(dd,supp_c);
    intersection = commonSupport != DD_ONE(dd);
    Cudd_RecursiveDeref(dd,commonSupport);

    if (intersection) {
466 467 468 469 470 471 472 473 474 475 476 477 478
        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
479
    } else {
480
        return(f);
Alan Mishchenko committed
481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
    }

} /* 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 
502
  the package).]
Alan Mishchenko committed
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519

  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
520
    vect = ABC_ALLOC(DdNode *, dd->size);
Alan Mishchenko committed
521
    if (vect == NULL) {
522 523
        dd->errorCode = CUDD_MEMORY_OUT;
        return(NULL);
Alan Mishchenko committed
524 525 526
    }

    do {
527 528 529 530 531 532 533 534 535 536 537 538
        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
539 540 541
        }
    } while (dd->reordered == 1);
    if (res == NULL) {
542 543
        ABC_FREE(vect);
        return(NULL);
Alan Mishchenko committed
544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577
    }
    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 {
578 579
        dd->reordered = 0;
        res = cuddBddLICompaction(dd,f,c);
Alan Mishchenko committed
580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610
    } 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 {
611 612
        dd->reordered = 0;
        res = cuddBddSqueeze(dd,l,u);
Alan Mishchenko committed
613 614 615 616 617 618 619 620
    } 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) {
621 622 623 624
        cuddRef(res);
        Cudd_IterDerefBdd(dd,res);
        res = u;
        sizeRes = sizeU;
Alan Mishchenko committed
625 626 627
    }
    sizeL = Cudd_DagSize(l);
    if (sizeL <= sizeRes) {
628 629 630 631
        cuddRef(res);
        Cudd_IterDerefBdd(dd,res);
        res = l;
        sizeRes = sizeL;
Alan Mishchenko committed
632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669
    }
    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) {
670 671
        Cudd_IterDerefBdd(dd,cplus);
        return(NULL);
Alan Mishchenko committed
672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712
    }
    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) {
713 714
        Cudd_IterDerefBdd(dd,tmp1);
        return(NULL);
Alan Mishchenko committed
715 716 717 718 719
    }
    cuddRef(tmp2);
    Cudd_IterDerefBdd(dd,tmp1);
    res = Cudd_bddSqueeze(dd,tmp2,f);
    if (res == NULL) {
720 721
        Cudd_IterDerefBdd(dd,tmp2);
        return(NULL);
Alan Mishchenko committed
722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788
    }
    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;
789
    DdNode       *one, *zero;
Alan Mishchenko committed
790
    unsigned int topf, topc;
791
    int          index;
Alan Mishchenko committed
792 793 794 795 796 797 798
    int          comple = 0;

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

    /* Trivial cases. */
799 800 801 802 803
    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
804 805 806

    /* Make canonical to increase the utilization of the cache. */
    if (Cudd_IsComplement(f)) {
807 808
        f = Cudd_Not(f);
        comple = 1;
Alan Mishchenko committed
809 810 811 812 813 814 815 816
    }
    /* 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) {
817
        return(Cudd_NotCond(r,comple));
Alan Mishchenko committed
818 819 820 821 822 823
    }
    
    /* Recursive step. */
    topf = dd->perm[f->index];
    topc = dd->perm[Cudd_Regular(c)->index];
    if (topf <= topc) {
824 825
        index = f->index;
        Fv = cuddT(f); Fnv = cuddE(f);
Alan Mishchenko committed
826
    } else {
827 828
        index = Cudd_Regular(c)->index;
        Fv = Fnv = f;
Alan Mishchenko committed
829 830
    }
    if (topc <= topf) {
831 832 833 834 835
        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
836
    } else {
837
        Cv = Cnv = c;
Alan Mishchenko committed
838 839 840
    }

    if (!Cudd_IsConstant(Cv)) {
841 842 843
        t = cuddBddConstrainRecur(dd, Fv, Cv);
        if (t == NULL)
            return(NULL);
Alan Mishchenko committed
844
    } else if (Cv == one) {
845 846 847 848 849 850 851 852 853 854
        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
855 856 857 858
    }
    cuddRef(t);

    if (!Cudd_IsConstant(Cnv)) {
859 860 861 862 863
        e = cuddBddConstrainRecur(dd, Fnv, Cnv);
        if (e == NULL) {
            Cudd_IterDerefBdd(dd, t);
            return(NULL);
        }
Alan Mishchenko committed
864
    } else if (Cnv == one) {
865 866 867 868
        e = Fnv;
    } else {            /* Cnv == zero: return Fv @ Cv previously computed */
        cuddDeref(t);
        return(Cudd_NotCond(t,comple));
Alan Mishchenko committed
869 870 871 872
    }
    cuddRef(e);

    if (Cudd_IsComplement(t)) {
873 874 875 876 877 878 879 880 881
        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
882
    } else {
883 884 885 886 887 888
        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
889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916
    }
    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)
{
917
    DdNode       *Fv, *Fnv, *Cv, *Cnv, *t, *e, *r, *one, *zero;
Alan Mishchenko committed
918
    unsigned int topf, topc;
919 920
    int          index;
    int          comple = 0;
Alan Mishchenko committed
921 922 923 924 925 926

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

    /* Trivial cases */
927 928 929 930 931
    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
932 933 934

    /* Make canonical to increase the utilization of the cache. */
    if (Cudd_IsComplement(f)) {
935 936
        f = Cudd_Not(f);
        comple = 1;
Alan Mishchenko committed
937 938 939 940 941 942 943 944
    }
    /* 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) {
945
        return(Cudd_NotCond(r,comple));
Alan Mishchenko committed
946 947 948 949 950
    }

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

951 952
    if (topc < topf) {  /* abstract top variable from c */
        DdNode *d, *s1, *s2;
Alan Mishchenko committed
953

954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972
        /* 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
973
        Cudd_IterDerefBdd(dd, d);
974 975 976
        cuddCacheInsert2(dd, Cudd_bddRestrict, f, c, r);
        cuddDeref(r);
        return(Cudd_NotCond(r,comple));
Alan Mishchenko committed
977 978 979 980 981 982
    }

    /* Recursive step. Here topf <= topc. */
    index = f->index;
    Fv = cuddT(f); Fnv = cuddE(f);
    if (topc == topf) {
983 984 985 986 987
        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
988
    } else {
989
        Cv = Cnv = c;
Alan Mishchenko committed
990 991 992
    }

    if (!Cudd_IsConstant(Cv)) {
993 994
        t = cuddBddRestrictRecur(dd, Fv, Cv);
        if (t == NULL) return(NULL);
Alan Mishchenko committed
995
    } else if (Cv == one) {
996 997 998 999 1000 1001 1002 1003 1004
        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
1005 1006 1007 1008
    }
    cuddRef(t);

    if (!Cudd_IsConstant(Cnv)) {
1009 1010 1011 1012 1013
        e = cuddBddRestrictRecur(dd, Fnv, Cnv);
        if (e == NULL) {
            Cudd_IterDerefBdd(dd, t);
            return(NULL);
        }
Alan Mishchenko committed
1014
    } else if (Cnv == one) {
1015 1016 1017 1018
        e = Fnv;
    } else {            /* Cnv == zero: return (Fv @ Cv) previously computed */
        cuddDeref(t);
        return(Cudd_NotCond(t,comple));
Alan Mishchenko committed
1019 1020 1021 1022
    }
    cuddRef(e);

    if (Cudd_IsComplement(t)) {
1023 1024 1025 1026 1027 1028 1029 1030 1031
        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
1032
    } else {
1033 1034 1035 1036 1037 1038
        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
1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
    }
    cuddDeref(t);
    cuddDeref(e);

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

} /* end of cuddBddRestrictRecur */


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

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 1184 1185 1186 1187 1188 1189 1190 1191
  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
1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208
  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;
1209
    DdNode       *one, *zero;
Alan Mishchenko committed
1210
    unsigned int topf, topc;
1211
    int          index;
Alan Mishchenko committed
1212 1213 1214 1215 1216 1217

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

    /* Trivial cases. */
1218 1219 1220 1221
    if (c == one)               return(f);
    if (c == zero)              return(zero);
    if (Cudd_IsConstant(f))     return(f);
    if (f == c)                 return(one);
Alan Mishchenko committed
1222 1223 1224 1225 1226 1227

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

    /* Check the cache. */
    r = cuddCacheLookup2(dd, Cudd_addConstrain, f, c);
    if (r != NULL) {
1228
        return(r);
Alan Mishchenko committed
1229 1230 1231 1232 1233 1234
    }
    
    /* Recursive step. */
    topf = dd->perm[f->index];
    topc = dd->perm[c->index];
    if (topf <= topc) {
1235 1236
        index = f->index;
        Fv = cuddT(f); Fnv = cuddE(f);
Alan Mishchenko committed
1237
    } else {
1238 1239
        index = c->index;
        Fv = Fnv = f;
Alan Mishchenko committed
1240 1241
    }
    if (topc <= topf) {
1242
        Cv = cuddT(c); Cnv = cuddE(c);
Alan Mishchenko committed
1243
    } else {
1244
        Cv = Cnv = c;
Alan Mishchenko committed
1245 1246 1247
    }

    if (!Cudd_IsConstant(Cv)) {
1248 1249 1250
        t = cuddAddConstrainRecur(dd, Fv, Cv);
        if (t == NULL)
            return(NULL);
Alan Mishchenko committed
1251
    } else if (Cv == one) {
1252 1253 1254 1255 1256 1257 1258 1259 1260 1261
        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
1262 1263 1264 1265
    }
    cuddRef(t);

    if (!Cudd_IsConstant(Cnv)) {
1266 1267 1268 1269 1270
        e = cuddAddConstrainRecur(dd, Fnv, Cnv);
        if (e == NULL) {
            Cudd_RecursiveDeref(dd, t);
            return(NULL);
        }
Alan Mishchenko committed
1271
    } else if (Cnv == one) {
1272 1273 1274 1275
        e = Fnv;
    } else {            /* Cnv == zero: return Fv @ Cv previously computed */
        cuddDeref(t);
        return(t);
Alan Mishchenko committed
1276 1277 1278 1279 1280
    }
    cuddRef(e);

    r = (t == e) ? t : cuddUniqueInter(dd, index, t, e);
    if (r == NULL) {
1281 1282 1283
        Cudd_RecursiveDeref(dd, e);
        Cudd_RecursiveDeref(dd, t);
        return(NULL);
Alan Mishchenko committed
1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311
    }
    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)
{
1312
    DdNode       *Fv, *Fnv, *Cv, *Cnv, *t, *e, *r, *one, *zero;
Alan Mishchenko committed
1313
    unsigned int topf, topc;
1314
    int          index;
Alan Mishchenko committed
1315 1316 1317 1318 1319 1320

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

    /* Trivial cases */
1321 1322 1323 1324
    if (c == one)               return(f);
    if (c == zero)              return(zero);
    if (Cudd_IsConstant(f))     return(f);
    if (f == c)                 return(one);
Alan Mishchenko committed
1325 1326 1327 1328 1329 1330

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

    /* Check the cache. */
    r = cuddCacheLookup2(dd, Cudd_addRestrict, f, c);
    if (r != NULL) {
1331
        return(r);
Alan Mishchenko committed
1332 1333 1334 1335 1336
    }

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

1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352
    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
1353
        Cudd_RecursiveDeref(dd, d);
1354 1355 1356
        cuddCacheInsert2(dd, Cudd_addRestrict, f, c, r);
        cuddDeref(r);
        return(r);
Alan Mishchenko committed
1357 1358 1359 1360 1361 1362
    }

    /* Recursive step. Here topf <= topc. */
    index = f->index;
    Fv = cuddT(f); Fnv = cuddE(f);
    if (topc == topf) {
1363
        Cv = cuddT(c); Cnv = cuddE(c);
Alan Mishchenko committed
1364
    } else {
1365
        Cv = Cnv = c;
Alan Mishchenko committed
1366 1367 1368
    }

    if (!Cudd_IsConstant(Cv)) {
1369 1370
        t = cuddAddRestrictRecur(dd, Fv, Cv);
        if (t == NULL) return(NULL);
Alan Mishchenko committed
1371
    } else if (Cv == one) {
1372 1373 1374 1375 1376 1377 1378 1379 1380
        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
1381 1382 1383 1384
    }
    cuddRef(t);

    if (!Cudd_IsConstant(Cnv)) {
1385 1386 1387 1388 1389
        e = cuddAddRestrictRecur(dd, Fnv, Cnv);
        if (e == NULL) {
            Cudd_RecursiveDeref(dd, t);
            return(NULL);
        }
Alan Mishchenko committed
1390
    } else if (Cnv == one) {
1391 1392 1393 1394
        e = Fnv;
    } else {            /* Cnv == zero: return (Fv @ Cv) previously computed */
        cuddDeref(t);
        return(t);
Alan Mishchenko committed
1395 1396 1397 1398 1399
    }
    cuddRef(e);

    r = (t == e) ? t : cuddUniqueInter(dd, index, t, e);
    if (r == NULL) {
1400 1401 1402
        Cudd_RecursiveDeref(dd, e);
        Cudd_RecursiveDeref(dd, t);
        return(NULL);
Alan Mishchenko committed
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 1431 1432 1433 1434 1435 1436 1437 1438
    }
    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) */)
{
1439
    st__table *marktable, *markcache, *buildcache;
Alan Mishchenko committed
1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455
    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.
    */
1456
    marktable = st__init_table( st__ptrcmp, st__ptrhash);
Alan Mishchenko committed
1457
    if (marktable == NULL) {
1458
        return(NULL);
Alan Mishchenko committed
1459
    }
1460
    markcache = st__init_table(MarkCacheCompare,MarkCacheHash);
Alan Mishchenko committed
1461
    if (markcache == NULL) {
1462
        st__free_table(marktable);
1463
        return(NULL);
Alan Mishchenko committed
1464 1465
    }
    if (cuddBddLICMarkEdges(dd,f,c,marktable,markcache) == CUDD_OUT_OF_MEM) {
1466 1467 1468
        st__foreach(markcache, MarkCacheCleanUp, NULL);
        st__free_table(marktable);
        st__free_table(markcache);
1469
        return(NULL);
Alan Mishchenko committed
1470
    }
1471 1472 1473
    st__foreach(markcache, MarkCacheCleanUp, NULL);
    st__free_table(markcache);
    buildcache = st__init_table( st__ptrcmp, st__ptrhash);
Alan Mishchenko committed
1474
    if (buildcache == NULL) {
1475
        st__free_table(marktable);
1476
        return(NULL);
Alan Mishchenko committed
1477 1478
    }
    res = cuddBddLICBuildResult(dd,f,buildcache,marktable);
1479 1480
    st__free_table(buildcache);
    st__free_table(marktable);
Alan Mishchenko committed
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 1512 1513 1514 1515 1516 1517 1518 1519
    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) {
1520 1521
        fv = Cudd_Not(fv);
        fvn = Cudd_Not(fvn);
Alan Mishchenko committed
1522 1523 1524 1525
    }
    /* Compute abstraction of top variable. */
    fAbs = cuddBddAndRecur(dd, fv, fvn);
    if (fAbs == NULL) {
1526
        return(0);
Alan Mishchenko committed
1527 1528 1529 1530 1531 1532 1533
    }
    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) {
1534 1535
        Cudd_IterDerefBdd(dd,fAbs);
        return(0);
Alan Mishchenko committed
1536 1537 1538 1539 1540
    }
    /* 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) {
1541 1542
        Cudd_IterDerefBdd(dd,fAbs);
        return(0);
Alan Mishchenko committed
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 1573 1574 1575 1576 1577 1578 1579 1580
    }
    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) {
1581
        return(res);
Alan Mishchenko committed
1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598
    }

    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) {
1599 1600 1601
        if (fT == zero) return(zero);
        if (fE == zero) return(one);
        return(x);
Alan Mishchenko committed
1602 1603 1604 1605 1606 1607 1608 1609
    }

    /* 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) {
1610
        return(NULL);
Alan Mishchenko committed
1611 1612 1613 1614
    }
    cuddRef(T);
    E = cuddBddCharToVect(dd, fE, x);
    if (E == NULL) {
1615 1616
        Cudd_IterDerefBdd(dd,T);
        return(NULL);
Alan Mishchenko committed
1617 1618 1619 1620
    }
    cuddRef(E);
    res = cuddBddIteRecur(dd, dd->vars[F->index], T, E);
    if (res == NULL) {
1621 1622 1623
        Cudd_IterDerefBdd(dd,T);
        Cudd_IterDerefBdd(dd,E);
        return(NULL);
Alan Mishchenko committed
1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650
    }
    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,
1651 1652
  st__table * table,
  st__table * cache)
Alan Mishchenko committed
1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677
{
    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
1678
    key = ABC_ALLOC(MarkCacheKey, 1);
Alan Mishchenko committed
1679
    if (key == NULL) {
1680 1681
        dd->errorCode = CUDD_MEMORY_OUT;
        return(CUDD_OUT_OF_MEM);
Alan Mishchenko committed
1682 1683
    }
    key->f = f; key->c = c;
1684
    if ( st__lookup_int(cache, (char *)key, &res)) {
1685 1686 1687 1688 1689 1690
        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
1691 1692 1693 1694 1695 1696
    }

    /* Recursive step. */
    topf = dd->perm[f->index];
    topc = cuddI(dd,Cudd_Regular(c)->index);
    if (topf <= topc) {
1697
        Fv = cuddT(f); Fnv = cuddE(f);
Alan Mishchenko committed
1698
    } else {
1699
        Fv = Fnv = f;
Alan Mishchenko committed
1700 1701
    }
    if (topc <= topf) {
1702 1703 1704 1705 1706 1707
        /* 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
1708
    } else {
1709
        Cv = Cnv = c;
Alan Mishchenko committed
1710 1711 1712 1713
    }

    resT = cuddBddLICMarkEdges(dd, Fv, Cv, table, cache);
    if (resT == CUDD_OUT_OF_MEM) {
1714 1715
        ABC_FREE(key);
        return(CUDD_OUT_OF_MEM);
Alan Mishchenko committed
1716 1717 1718
    }
    resE = cuddBddLICMarkEdges(dd, Fnv, Cnv, table, cache);
    if (resE == CUDD_OUT_OF_MEM) {
1719 1720
        ABC_FREE(key);
        return(CUDD_OUT_OF_MEM);
Alan Mishchenko committed
1721 1722 1723 1724
    }

    /* Update edge markings. */
    if (topf <= topc) {
1725
        retval = st__find_or_add(table, (char *)f, (char ***)&slot);
1726 1727 1728 1729 1730 1731 1732 1733
        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
1734 1735 1736 1737
    }

    /* Cache result. */
    res = resT | resE;
1738
    if ( st__insert(cache, (char *)key, (char *)(ptrint)res) == st__OUT_OF_MEM) {
1739 1740
        ABC_FREE(key);
        return(CUDD_OUT_OF_MEM);
Alan Mishchenko committed
1741 1742 1743 1744
    }

    /* Take into account possible complementation. */
    if (comple) {
1745 1746
        if (res == DD_LIC_0) res = DD_LIC_1;
        else if (res == DD_LIC_1) res = DD_LIC_0;
Alan Mishchenko committed
1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768
    }
    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,
1769 1770
  st__table * cache,
  st__table * table)
Alan Mishchenko committed
1771 1772 1773
{
    DdNode *Fv, *Fnv, *r, *t, *e;
    DdNode *one, *zero;
1774
    int index;
Alan Mishchenko committed
1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786
    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. */
1787
    if ( st__lookup(cache, (const char *)f, (char **)&r)) {
1788
        return(Cudd_NotCond(r,comple));
Alan Mishchenko committed
1789 1790 1791
    }

    /* Retrieve the edge markings. */
1792
    if ( st__lookup_int(table, (char *)f, &markings) == 0)
1793
        return(NULL);
Alan Mishchenko committed
1794 1795 1796 1797 1798 1799 1800
    markT = markings >> 2;
    markE = markings & 3;

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

    if (markT == DD_LIC_NL) {
1801 1802 1803 1804
        t = cuddBddLICBuildResult(dd,Fv,cache,table);
        if (t == NULL) {
            return(NULL);
        }
Alan Mishchenko committed
1805
    } else if (markT == DD_LIC_1) {
1806
        t = one;
Alan Mishchenko committed
1807
    } else {
1808
        t = zero;
Alan Mishchenko committed
1809 1810 1811
    }
    cuddRef(t);
    if (markE == DD_LIC_NL) {
1812 1813 1814 1815 1816
        e = cuddBddLICBuildResult(dd,Fnv,cache,table);
        if (e == NULL) {
            Cudd_IterDerefBdd(dd,t);
            return(NULL);
        }
Alan Mishchenko committed
1817
    } else if (markE == DD_LIC_1) {
1818
        e = one;
Alan Mishchenko committed
1819
    } else {
1820
        e = zero;
Alan Mishchenko committed
1821 1822 1823 1824
    }
    cuddRef(e);

    if (markT == DD_LIC_DC && markE != DD_LIC_DC) {
1825
        r = e;
Alan Mishchenko committed
1826
    } else if (markT != DD_LIC_DC && markE == DD_LIC_DC) {
1827
        r = t;
Alan Mishchenko committed
1828
    } else {
1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845
        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
1846 1847 1848 1849 1850
        }
    }
    cuddDeref(t);
    cuddDeref(e);

1851
    if ( st__insert(cache, (char *)f, (char *)r) == st__OUT_OF_MEM) {
1852 1853 1854
        cuddRef(r);
        Cudd_IterDerefBdd(dd,r);
        return(NULL);
Alan Mishchenko committed
1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875
    }

    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(
1876
  const char * ptr,
Alan Mishchenko committed
1877 1878 1879
  int  modulus)
{
    int val = 0;
1880
    MarkCacheKey *entry;
Alan Mishchenko committed
1881

1882
    entry = (MarkCacheKey *) ptr;
Alan Mishchenko committed
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 1920 1921 1922 1923 1924 1925 1926 1927

    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
1928
  cuddBddLICMarkEdges. Returns st__CONTINUE.]
Alan Mishchenko committed
1929 1930 1931 1932 1933 1934

  SideEffects [None]

  SeeAlso     [Cudd_bddLICompaction]

******************************************************************************/
1935
static enum st__retval
Alan Mishchenko committed
1936 1937 1938 1939 1940 1941 1942 1943
MarkCacheCleanUp(
  char * key,
  char * value,
  char * arg)
{
    MarkCacheKey *entry;

    entry = (MarkCacheKey *) key;
Alan Mishchenko committed
1944
    ABC_FREE(entry);
1945
    return st__CONTINUE;
Alan Mishchenko committed
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 1974 1975 1976 1977 1978 1979 1980 1981

} /* 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) {
1982
        return(l);
Alan Mishchenko committed
1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995
    }
    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)) {
1996 1997 1998 1999 2000
        DdNode *temp;
        temp = Cudd_Not(l);
        l = Cudd_Not(u);
        u = temp;
        comple = 1;
Alan Mishchenko committed
2001 2002 2003 2004 2005 2006 2007 2008 2009
    }
    /* 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) {
2010
        return(Cudd_NotCond(r,comple));
Alan Mishchenko committed
2011 2012 2013 2014 2015 2016
    }

    /* Recursive step. */
    topu = dd->perm[u->index];
    topl = dd->perm[Cudd_Regular(l)->index];
    if (topu <= topl) {
2017 2018
        index = u->index;
        ut = cuddT(u); ue = cuddE(u);
Alan Mishchenko committed
2019
    } else {
2020 2021
        index = Cudd_Regular(l)->index;
        ut = ue = u;
Alan Mishchenko committed
2022 2023
    }
    if (topl <= topu) {
2024 2025 2026 2027 2028
        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
2029
    } else {
2030
        lt = le = l;
Alan Mishchenko committed
2031 2032 2033 2034 2035
    }

    /* 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)) &&
2036 2037 2038 2039 2040
        (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
2041
    } else if ((le == zero || Cudd_bddLeq(dd,le,lt)) &&
2042 2043 2044 2045 2046
               (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
2047
    } else if ((le == zero || Cudd_bddLeq(dd,le,Cudd_Not(ut))) &&
2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063
               (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
2064
        }
2065 2066 2067 2068 2069
        cuddDeref(t);
        if (r == NULL)
            return(NULL);
        cuddCacheInsert2(dd, Cudd_bddSqueeze, l, u, r);
        return(Cudd_NotCond(r,comple));
Alan Mishchenko committed
2070
    } else if ((lt == zero || Cudd_bddLeq(dd,lt,Cudd_Not(ue))) &&
2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086
               (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
2087
        }
2088 2089 2090 2091 2092
        cuddDeref(e);
        if (r == NULL)
            return(NULL);
        cuddCacheInsert2(dd, Cudd_bddSqueeze, l, u, r);
        return(Cudd_NotCond(r,comple));
Alan Mishchenko committed
2093 2094 2095 2096 2097 2098 2099 2100 2101
    }

#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)) {
2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120
        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
2121 2122 2123
        Cudd_IterDerefBdd(dd,au);
        Cudd_IterDerefBdd(dd,al);
    } else {
2124
        ar = NULL;
Alan Mishchenko committed
2125 2126 2127 2128 2129
    }
#endif

    t = cuddBddSqueeze(dd, lt, ut);
    if (t == NULL) {
2130
        return(NULL);
Alan Mishchenko committed
2131 2132 2133 2134
    }
    cuddRef(t);
    e = cuddBddSqueeze(dd, le, ue);
    if (e == NULL) {
2135 2136
        Cudd_IterDerefBdd(dd,t);
        return(NULL);
Alan Mishchenko committed
2137 2138 2139 2140
    }
    cuddRef(e);

    if (Cudd_IsComplement(t)) {
2141 2142 2143 2144 2145 2146 2147 2148 2149
        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
2150
    } else {
2151 2152 2153 2154 2155 2156
        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
2157 2158 2159 2160 2161 2162 2163 2164 2165
    }
    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) {
2166 2167 2168 2169 2170 2171
        if (Cudd_DagSize(ar) <= Cudd_DagSize(r)) {
            Cudd_IterDerefBdd(dd, r);
            r = ar;
        } else {
            Cudd_IterDerefBdd(dd, ar);
        }
Alan Mishchenko committed
2172 2173 2174 2175 2176 2177 2178 2179
    }
    cuddDeref(r);
#endif

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

} /* end of cuddBddSqueeze */
2180 2181


2182 2183
ABC_NAMESPACE_IMPL_END