cnfPost.c 6.81 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    [cnfPost.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: cnfPost.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 45 46
////////////////////////////////////////////////////////////////////////
///                        DECLARATIONS                              ///
////////////////////////////////////////////////////////////////////////

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

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Cnf_ManPostprocess_old( Cnf_Man_t * p )
{
Alan Mishchenko committed
47
//    extern int Aig_ManLargeCutEval( Aig_Man_t * p, Aig_Obj_t * pRoot, Dar_Cut_t * pCutR, Dar_Cut_t * pCutL, int Leaf );
Alan Mishchenko committed
48 49
    int nNew, Gain, nGain = 0, nVars = 0;

Alan Mishchenko committed
50
    Aig_Obj_t * pObj, * pFan;
Alan Mishchenko committed
51 52
    Dar_Cut_t * pCutBest, * pCut;
    int i, k;//, a, b, Counter;
Alan Mishchenko committed
53
    Aig_ManForEachObj( p->pManAig, pObj, i )
Alan Mishchenko committed
54
    {
Alan Mishchenko committed
55
        if ( !Aig_ObjIsNode(pObj) )
Alan Mishchenko committed
56 57 58
            continue;
        if ( pObj->nRefs == 0 )
            continue;
Alan Mishchenko committed
59 60 61
//        pCutBest = Aig_ObjBestCut(pObj);
        pCutBest = NULL;

Alan Mishchenko committed
62 63
        Dar_CutForEachLeaf( p->pManAig, pCutBest, pFan, k )
        {
Alan Mishchenko committed
64
            if ( !Aig_ObjIsNode(pFan) )
Alan Mishchenko committed
65 66 67 68
                continue;
            assert( pFan->nRefs != 0 );
            if ( pFan->nRefs != 1 )
                continue;
Alan Mishchenko committed
69 70
//            pCut = Aig_ObjBestCut(pFan);
            pCut = NULL;
Alan Mishchenko committed
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
/*
            // find how many common variable they have
            Counter = 0;
            for ( a = 0; a < (int)pCut->nLeaves; a++ )
            {
                for ( b = 0; b < (int)pCutBest->nLeaves; b++ )
                    if ( pCut->pLeaves[a] == pCutBest->pLeaves[b] )
                        break;
                if ( b == (int)pCutBest->nLeaves )
                    continue;
                Counter++;
            }
            printf( "%d ", Counter );
*/
            // find the new truth table after collapsing these two cuts
Alan Mishchenko committed
86 87 88 89 90 91


//            nNew = Aig_ManLargeCutEval( p->pManAig, pObj, pCutBest, pCut, pFan->Id );
            nNew = 0;


Alan Mishchenko committed
92 93 94
//            printf( "%d+%d=%d:%d(%d) ", pCutBest->Cost, pCut->Cost, 
//                pCutBest->Cost+pCut->Cost, nNew, pCutBest->Cost+pCut->Cost-nNew );

Alan Mishchenko committed
95
            Gain = pCutBest->Value + pCut->Value - nNew;
Alan Mishchenko committed
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
            if ( Gain > 0 )
            {
                nGain += Gain;
                nVars++;
            }
        }
    }
    printf( "Total gain = %d.  Vars = %d.\n", nGain, nVars );
}

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

  Synopsis    [Transfers cuts of the mapped nodes into internal representation.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Cnf_ManTransferCuts( Cnf_Man_t * p )
{
Alan Mishchenko committed
119
    Aig_Obj_t * pObj;
Alan Mishchenko committed
120
    int i;
Alan Mishchenko committed
121 122
    Aig_MmFlexRestart( p->pMemCuts );
    Aig_ManForEachObj( p->pManAig, pObj, i )
Alan Mishchenko committed
123
    {
Alan Mishchenko committed
124
        if ( Aig_ObjIsNode(pObj) && pObj->nRefs > 0 )
Alan Mishchenko committed
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
            pObj->pData = Cnf_CutCreate( p, pObj );
        else
            pObj->pData = NULL;
    }
}

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

  Synopsis    [Transfers cuts of the mapped nodes into internal representation.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Cnf_ManFreeCuts( Cnf_Man_t * p )
{
Alan Mishchenko committed
144
    Aig_Obj_t * pObj;
Alan Mishchenko committed
145
    int i;
Alan Mishchenko committed
146
    Aig_ManForEachObj( p->pManAig, pObj, i )
Alan Mishchenko committed
147 148
        if ( pObj->pData )
        {
149
            Cnf_CutFree( (Cnf_Cut_t *)pObj->pData );
Alan Mishchenko committed
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
            pObj->pData = NULL;
        }
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Cnf_ManPostprocess( Cnf_Man_t * p )
{
    Cnf_Cut_t * pCut, * pCutFan, * pCutRes;
Alan Mishchenko committed
168
    Aig_Obj_t * pObj, * pFan;
Alan Mishchenko committed
169 170
    int Order[16], Costs[16];
    int i, k, fChanges;
Alan Mishchenko committed
171
    Aig_ManForEachNode( p->pManAig, pObj, i )
Alan Mishchenko committed
172 173 174 175 176 177 178 179 180
    {
        if ( pObj->nRefs == 0 )
            continue;
        pCut = Cnf_ObjBestCut(pObj);

        // sort fanins according to their size
        Cnf_CutForEachLeaf( p->pManAig, pCut, pFan, k )
        {
            Order[k] = k;
Alan Mishchenko committed
181
            Costs[k] = Aig_ObjIsNode(pFan)? Cnf_ObjBestCut(pFan)->Cost : 0;
Alan Mishchenko committed
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
        }
        // sort the cuts by Weight
        do {
            int Temp;
            fChanges = 0;
            for ( k = 0; k < pCut->nFanins - 1; k++ )
            {
                if ( Costs[Order[k]] <= Costs[Order[k+1]] )
                    continue;
                Temp = Order[k];
                Order[k] = Order[k+1];
                Order[k+1] = Temp;
                fChanges = 1;
            }
        } while ( fChanges );


//        Cnf_CutForEachLeaf( p->pManAig, pCut, pFan, k )
Alan Mishchenko committed
200
        for ( k = 0; (k < (int)(pCut)->nFanins) && ((pFan) = Aig_ManObj(p->pManAig, (pCut)->pFanins[Order[k]])); k++ )
Alan Mishchenko committed
201
        {
Alan Mishchenko committed
202
            if ( !Aig_ObjIsNode(pFan) )
Alan Mishchenko committed
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
                continue;
            assert( pFan->nRefs != 0 );
            if ( pFan->nRefs != 1 )
                continue;
            pCutFan = Cnf_ObjBestCut(pFan);
            // try composing these two cuts
//            Cnf_CutPrint( pCut );
            pCutRes = Cnf_CutCompose( p, pCut, pCutFan, pFan->Id );
//            Cnf_CutPrint( pCut );
//            printf( "\n" );
            // check if the cost if reduced
            if ( pCutRes == NULL || pCutRes->Cost == 127 || pCutRes->Cost > pCut->Cost + pCutFan->Cost )
            {
                if ( pCutRes )
                    Cnf_CutFree( pCutRes );
                continue;
            }
            // update the cut
            Cnf_ObjSetBestCut( pObj, pCutRes );
            Cnf_ObjSetBestCut( pFan, NULL );
            Cnf_CutUpdateRefs( p, pCut, pCutFan, pCutRes );
            assert( pFan->nRefs == 0 );
            Cnf_CutFree( pCut );
            Cnf_CutFree( pCutFan );
            break;
        }
    }
}

////////////////////////////////////////////////////////////////////////
///                       END OF FILE                                ///
////////////////////////////////////////////////////////////////////////


237 238
ABC_NAMESPACE_IMPL_END