cuddEssent.c 8.68 KB
Newer Older
Alan Mishchenko committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/**CFile***********************************************************************

  FileName    [cuddEssent.c]

  PackageName [cudd]

  Synopsis    [Functions for the detection of essential variables.]

  Description [External procedures included in this file:
        <ul>
        <li> Cudd_FindEssential()
        <li> Cudd_bddIsVarEssential()
        </ul>
    Static procedures included in this module:
        <ul>
        <li> ddFindEssentialRecur()
        </ul>]

  Author      [Fabio Somenzi]

  Copyright   [This file was created at the University of Colorado at
  Boulder.  The University of Colorado at Boulder makes no warranty
  about the suitability of this software for any purpose.  It is
  presented on an AS IS basis.]

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

Alan Mishchenko committed
28
#include "util_hack.h"
Alan Mishchenko committed
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
#include "cuddInt.h"

/*---------------------------------------------------------------------------*/
/* Constant declarations                                                     */
/*---------------------------------------------------------------------------*/

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

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

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

#ifndef lint
static char rcsid[] DD_UNUSED = "$Id: cuddEssent.c,v 1.1.1.1 2003/02/24 22:23:51 wjiang Exp $";
#endif

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

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

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

static DdNode * ddFindEssentialRecur ARGS((DdManager *dd, DdNode *f));

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


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


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

  Synopsis    [Finds the essential variables of a DD.]

  Description [Returns the cube of the essential variables. A positive
  literal means that the variable must be set to 1 for the function to be
  1. A negative literal means that the variable must be set to 0 for the
  function to be 1. Returns a pointer to the cube BDD if successful;
  NULL otherwise.]

  SideEffects [None]

  SeeAlso     [Cudd_bddIsVarEssential]

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

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

} /* end of Cudd_FindEssential */


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

  Synopsis    [Determines whether a given variable is essential with a
  given phase in a BDD.]

  Description [Determines whether a given variable is essential with a
  given phase in a BDD. Uses Cudd_bddIteConstant. Returns 1 if phase == 1
  and f-->x_id, or if phase == 0 and f-->x_id'.]

  SideEffects [None]

  SeeAlso     [Cudd_FindEssential]

******************************************************************************/
int
Cudd_bddIsVarEssential(
  DdManager * manager,
  DdNode * f,
  int  id,
  int  phase)
{
    DdNode    *var;
    int        res;
    DdNode    *one, *zero;

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

    var = cuddUniqueInter(manager, id, one, zero);

    var = Cudd_NotCond(var,phase == 0);

    res = Cudd_bddIteConstant(manager, Cudd_Not(f), one, var) == one;

    return(res);

} /* end of Cudd_bddIsVarEssential */


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


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


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

  Synopsis    [Implements the recursive step of Cudd_FindEssential.]

  Description [Implements the recursive step of Cudd_FindEssential.
  Returns a pointer to the cube BDD if successful; NULL otherwise.]

  SideEffects [None]

******************************************************************************/
static DdNode *
ddFindEssentialRecur(
  DdManager * dd,
  DdNode * f)
{
    DdNode    *T, *E, *F;
    DdNode    *essT, *essE, *res;
    int        index;
    DdNode    *one, *lzero, *azero;

    one = DD_ONE(dd);
    F = Cudd_Regular(f);
    /* If f is constant the set of essential variables is empty. */
    if (cuddIsConstant(F)) return(one);

    res = cuddCacheLookup1(dd,Cudd_FindEssential,f);
    if (res != NULL) {
    return(res);
    }

    lzero = Cudd_Not(one);
    azero = DD_ZERO(dd);
    /* Find cofactors: here f is non-constant. */
    T = cuddT(F);
    E = cuddE(F);
    if (Cudd_IsComplement(f)) {
    T = Cudd_Not(T); E = Cudd_Not(E);
    }

    index = F->index;
    if (Cudd_IsConstant(T) && T != lzero && T != azero) {
    /* if E is zero, index is essential, otherwise there are no
    ** essentials, because index is not essential and no other variable
    ** can be, since setting index = 1 makes the function constant and
    ** different from 0.
    */
    if (E == lzero || E == azero) {
        res = dd->vars[index];
    } else {
        res = one;
    }
    } else if (T == lzero || T == azero) {
    if (Cudd_IsConstant(E)) { /* E cannot be zero here */
        res = Cudd_Not(dd->vars[index]);
    } else { /* E == non-constant */
        /* find essentials in the else branch */
        essE = ddFindEssentialRecur(dd,E);
        if (essE == NULL) {
        return(NULL);
        }
        cuddRef(essE);

        /* add index to the set with negative phase */
        res = cuddUniqueInter(dd,index,one,Cudd_Not(essE));
        if (res == NULL) {
        Cudd_RecursiveDeref(dd,essE);
        return(NULL);
        }
        res = Cudd_Not(res);
        cuddDeref(essE);
    }
    } else { /* T == non-const */
    if (E == lzero || E == azero) {
        /* find essentials in the then branch */
        essT = ddFindEssentialRecur(dd,T);
        if (essT == NULL) {
        return(NULL);
        }
        cuddRef(essT);

        /* add index to the set with positive phase */
        /* use And because essT may be complemented */
        res = cuddBddAndRecur(dd,dd->vars[index],essT);
        if (res == NULL) {
        Cudd_RecursiveDeref(dd,essT);
        return(NULL);
        }
        cuddDeref(essT);
    } else if (!Cudd_IsConstant(E)) {
        /* if E is a non-zero constant there are no essentials
        ** because T is non-constant.
        */
        essT = ddFindEssentialRecur(dd,T);
        if (essT == NULL) {
        return(NULL);
        }
        if (essT == one) {
        res = one;
        } else {
        cuddRef(essT);
        essE = ddFindEssentialRecur(dd,E);
        if (essE == NULL) {
            Cudd_RecursiveDeref(dd,essT);
            return(NULL);
        }
        cuddRef(essE);

        /* res = intersection(essT, essE) */
        res = cuddBddLiteralSetIntersectionRecur(dd,essT,essE);
        if (res == NULL) {
            Cudd_RecursiveDeref(dd,essT);
            Cudd_RecursiveDeref(dd,essE);
            return(NULL);
        }
        cuddRef(res);
        Cudd_RecursiveDeref(dd,essT);
        Cudd_RecursiveDeref(dd,essE);
        cuddDeref(res);
        }
    } else {    /* E is a non-zero constant */
        res = one;
    }
    }

    cuddCacheInsert1(dd,Cudd_FindEssential, f, res);
    return(res);

} /* end of ddFindEssentialRecur */