cnfUtil.c 7.26 KB
Newer Older
Alan Mishchenko committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/**CFile****************************************************************

  FileName    [cnfUtil.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [AIG-to-CNF conversion.]

  Synopsis    []

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

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

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

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

#include "cnf.h"

23 24 25
ABC_NAMESPACE_IMPL_START


Alan Mishchenko committed
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
////////////////////////////////////////////////////////////////////////
///                        DECLARATIONS                              ///
////////////////////////////////////////////////////////////////////////

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

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

  Synopsis    [Computes area, references, and nodes used in the mapping.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
45
int Aig_ManScanMapping_rec( Cnf_Man_t * p, Aig_Obj_t * pObj, Vec_Ptr_t * vMapped )
Alan Mishchenko committed
46
{
Alan Mishchenko committed
47
    Aig_Obj_t * pLeaf;
Alan Mishchenko committed
48 49
    Dar_Cut_t * pCutBest;
    int aArea, i;
50
    if ( pObj->nRefs++ || Aig_ObjIsCi(pObj) || Aig_ObjIsConst1(pObj) )
Alan Mishchenko committed
51
        return 0;
Alan Mishchenko committed
52
    assert( Aig_ObjIsAnd(pObj) );
Alan Mishchenko committed
53 54 55 56
    // collect the node first to derive pre-order
    if ( vMapped )
        Vec_PtrPush( vMapped, pObj );
    // visit the transitive fanin of the selected cut
Alan Mishchenko committed
57 58 59 60 61
    if ( pObj->fMarkB )
    {
        Vec_Ptr_t * vSuper = Vec_PtrAlloc( 100 );
        Aig_ObjCollectSuper( pObj, vSuper );
        aArea = Vec_PtrSize(vSuper) + 1;
62
        Vec_PtrForEachEntry( Aig_Obj_t *, vSuper, pLeaf, i )
Alan Mishchenko committed
63 64 65 66 67 68 69 70 71 72 73 74
            aArea += Aig_ManScanMapping_rec( p, Aig_Regular(pLeaf), vMapped );
        Vec_PtrFree( vSuper );
        ////////////////////////////
        pObj->fMarkB = 1;
    }
    else
    {
        pCutBest = Dar_ObjBestCut( pObj );
        aArea = Cnf_CutSopCost( p, pCutBest );
        Dar_CutForEachLeaf( p->pManAig, pCutBest, pLeaf, i )
            aArea += Aig_ManScanMapping_rec( p, pLeaf, vMapped );
    }
Alan Mishchenko committed
75 76 77 78 79 80 81 82 83 84 85 86 87 88
    return aArea;
}

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

  Synopsis    [Computes area, references, and nodes used in the mapping.]

  Description [Collects the nodes in reverse topological order.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
89
Vec_Ptr_t * Aig_ManScanMapping( Cnf_Man_t * p, int fCollect )
Alan Mishchenko committed
90 91
{
    Vec_Ptr_t * vMapped = NULL;
Alan Mishchenko committed
92
    Aig_Obj_t * pObj;
Alan Mishchenko committed
93 94
    int i;
    // clean all references
Alan Mishchenko committed
95
    Aig_ManForEachObj( p->pManAig, pObj, i )
Alan Mishchenko committed
96 97 98 99 100 101
        pObj->nRefs = 0;
    // allocate the array
    if ( fCollect )
        vMapped = Vec_PtrAlloc( 1000 );
    // collect nodes reachable from POs in the DFS order through the best cuts
    p->aArea = 0;
102
    Aig_ManForEachCo( p->pManAig, pObj, i )
Alan Mishchenko committed
103
        p->aArea += Aig_ManScanMapping_rec( p, Aig_ObjFanin0(pObj), vMapped );
104
//    printf( "Variables = %6d. Clauses = %8d.\n", vMapped? Vec_PtrSize(vMapped) + Aig_ManCiNum(p->pManAig) + 1 : 0, p->aArea + 2 );
Alan Mishchenko committed
105 106 107 108 109 110 111 112 113 114 115 116 117 118
    return vMapped;
}

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

  Synopsis    [Computes area, references, and nodes used in the mapping.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
119
int Cnf_ManScanMapping_rec( Cnf_Man_t * p, Aig_Obj_t * pObj, Vec_Ptr_t * vMapped, int fPreorder )
Alan Mishchenko committed
120
{
Alan Mishchenko committed
121
    Aig_Obj_t * pLeaf;
Alan Mishchenko committed
122 123
    Cnf_Cut_t * pCutBest;
    int aArea, i;
124
    if ( pObj->nRefs++ || Aig_ObjIsCi(pObj) || Aig_ObjIsConst1(pObj) )
Alan Mishchenko committed
125
        return 0;
Alan Mishchenko committed
126
    assert( Aig_ObjIsAnd(pObj) );
Alan Mishchenko committed
127
    assert( pObj->pData != NULL );
Alan Mishchenko committed
128 129 130
    // add the node to the mapping
    if ( vMapped && fPreorder )
         Vec_PtrPush( vMapped, pObj );
Alan Mishchenko committed
131
    // visit the transitive fanin of the selected cut
Alan Mishchenko committed
132 133 134 135 136
    if ( pObj->fMarkB )
    {
        Vec_Ptr_t * vSuper = Vec_PtrAlloc( 100 );
        Aig_ObjCollectSuper( pObj, vSuper );
        aArea = Vec_PtrSize(vSuper) + 1;
137
        Vec_PtrForEachEntry( Aig_Obj_t *, vSuper, pLeaf, i )
Alan Mishchenko committed
138 139 140 141 142 143 144
            aArea += Cnf_ManScanMapping_rec( p, Aig_Regular(pLeaf), vMapped, fPreorder );
        Vec_PtrFree( vSuper );
        ////////////////////////////
        pObj->fMarkB = 1;
    }
    else
    {
145
        pCutBest = (Cnf_Cut_t *)pObj->pData;
Alan Mishchenko committed
146
//        assert( pCutBest->nFanins > 0 );
Alan Mishchenko committed
147 148 149 150 151 152 153 154
        assert( pCutBest->Cost < 127 );
        aArea = pCutBest->Cost;
        Cnf_CutForEachLeaf( p->pManAig, pCutBest, pLeaf, i )
            aArea += Cnf_ManScanMapping_rec( p, pLeaf, vMapped, fPreorder );
    }
    // add the node to the mapping
    if ( vMapped && !fPreorder )
         Vec_PtrPush( vMapped, pObj );
Alan Mishchenko committed
155 156 157 158 159 160 161 162 163 164 165 166 167 168
    return aArea;
}

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

  Synopsis    [Computes area, references, and nodes used in the mapping.]

  Description [Collects the nodes in reverse topological order.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
169
Vec_Ptr_t * Cnf_ManScanMapping( Cnf_Man_t * p, int fCollect, int fPreorder )
Alan Mishchenko committed
170 171
{
    Vec_Ptr_t * vMapped = NULL;
Alan Mishchenko committed
172
    Aig_Obj_t * pObj;
Alan Mishchenko committed
173 174
    int i;
    // clean all references
Alan Mishchenko committed
175
    Aig_ManForEachObj( p->pManAig, pObj, i )
Alan Mishchenko committed
176 177 178 179 180 181
        pObj->nRefs = 0;
    // allocate the array
    if ( fCollect )
        vMapped = Vec_PtrAlloc( 1000 );
    // collect nodes reachable from POs in the DFS order through the best cuts
    p->aArea = 0;
182
    Aig_ManForEachCo( p->pManAig, pObj, i )
Alan Mishchenko committed
183
        p->aArea += Cnf_ManScanMapping_rec( p, Aig_ObjFanin0(pObj), vMapped, fPreorder );
184
//    printf( "Variables = %6d. Clauses = %8d.\n", vMapped? Vec_PtrSize(vMapped) + Aig_ManCiNum(p->pManAig) + 1 : 0, p->aArea + 2 );
Alan Mishchenko committed
185 186 187
    return vMapped;
}

Alan Mishchenko committed
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
/**Function*************************************************************

  Synopsis    [Returns the array of CI IDs.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Vec_Int_t * Cnf_DataCollectCiSatNums( Cnf_Dat_t * pCnf, Aig_Man_t * p )
{
    Vec_Int_t * vCiIds;
    Aig_Obj_t * pObj;
    int i;
204
    vCiIds = Vec_IntAlloc( Aig_ManCiNum(p) );
205
    Aig_ManForEachCi( p, pObj, i )
Alan Mishchenko committed
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
        Vec_IntPush( vCiIds, pCnf->pVarNums[pObj->Id] );
    return vCiIds;
}

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

  Synopsis    [Returns the array of CI IDs.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Vec_Int_t * Cnf_DataCollectCoSatNums( Cnf_Dat_t * pCnf, Aig_Man_t * p )
{
    Vec_Int_t * vCoIds;
    Aig_Obj_t * pObj;
    int i;
226
    vCoIds = Vec_IntAlloc( Aig_ManCoNum(p) );
227
    Aig_ManForEachCo( p, pObj, i )
Alan Mishchenko committed
228 229 230 231
        Vec_IntPush( vCoIds, pCnf->pVarNums[pObj->Id] );
    return vCoIds;
}

Alan Mishchenko committed
232 233 234 235 236
////////////////////////////////////////////////////////////////////////
///                       END OF FILE                                ///
////////////////////////////////////////////////////////////////////////


237 238
ABC_NAMESPACE_IMPL_END