sfmNtk.c 11.9 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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
/**CFile****************************************************************

  FileName    [sfmNtk.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [SAT-based optimization using internal don't-cares.]

  Synopsis    [Logic network.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

  Date        [Ver. 1.0. Started - June 20, 2005.]

  Revision    [$Id: sfmNtk.c,v 1.00 2005/06/20 00:00:00 alanmi Exp $]

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

#include "sfmInt.h"

ABC_NAMESPACE_IMPL_START


////////////////////////////////////////////////////////////////////////
///                        DECLARATIONS                              ///
////////////////////////////////////////////////////////////////////////

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

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
45
void Sfm_CheckConsistency( Vec_Wec_t * vFanins, int nPis, int nPos, Vec_Str_t * vFixed )
Alan Mishchenko committed
46
{
Alan Mishchenko committed
47 48 49 50
    Vec_Int_t * vArray;
    int i, k, Fanin;
    // check entries
    Vec_WecForEachLevel( vFanins, vArray, i )
Alan Mishchenko committed
51
    {
Alan Mishchenko committed
52
        // PIs have no fanins
Alan Mishchenko committed
53
        if ( i < nPis )
Alan Mishchenko committed
54 55 56 57 58 59
            assert( Vec_IntSize(vArray) == 0 && Vec_StrEntry(vFixed, i) == (char)0 );
        // nodes are in a topo order; POs cannot be fanins
        Vec_IntForEachEntry( vArray, Fanin, k )
            assert( Fanin < i && Fanin + nPos < Vec_WecSize(vFanins) );
        // POs have one fanout
        if ( i + nPos >= Vec_WecSize(vFanins) )
Alan Mishchenko committed
60
            assert( Vec_IntSize(vArray) == 1 && Vec_StrEntry(vFixed, i) == (char)0 );
Alan Mishchenko committed
61 62 63 64 65 66 67 68 69 70 71 72 73 74
    }
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
75
void Sfm_CreateFanout( Vec_Wec_t * vFanins, Vec_Wec_t * vFanouts )
Alan Mishchenko committed
76 77 78 79
{
    Vec_Int_t * vArray;
    int i, k, Fanin;
    // count fanouts
Alan Mishchenko committed
80
    Vec_WecInit( vFanouts, Vec_WecSize(vFanins) );
Alan Mishchenko committed
81 82 83 84 85 86 87 88
    Vec_WecForEachLevel( vFanins, vArray, i )
        Vec_IntForEachEntry( vArray, Fanin, k )
            Vec_WecEntry( vFanouts, Fanin )->nSize++;
    // allocate fanins
    Vec_WecForEachLevel( vFanouts, vArray, i )
    {
        k = vArray->nSize; vArray->nSize = 0;
        Vec_IntGrow( vArray, k );
Alan Mishchenko committed
89
    }
Alan Mishchenko committed
90 91 92 93 94
    // add fanouts
    Vec_WecForEachLevel( vFanins, vArray, i )
        Vec_IntForEachEntry( vArray, Fanin, k )
            Vec_IntPush( Vec_WecEntry( vFanouts, Fanin ), i );
    // verify
Alan Mishchenko committed
95
    Vec_WecForEachLevel( vFanouts, vArray, i )
Alan Mishchenko committed
96 97 98 99 100 101 102 103 104 105 106 107 108 109
        assert( Vec_IntSize(vArray) == Vec_IntCap(vArray) );
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
110
static inline int Sfm_ObjLevelNew( Vec_Int_t * vArray, Vec_Int_t * vLevels, int fAddLevel )
Alan Mishchenko committed
111
{
Alan Mishchenko committed
112 113
    int k, Fanin, Level = 0;
    Vec_IntForEachEntry( vArray, Fanin, k )
Alan Mishchenko committed
114
        Level = Abc_MaxInt( Level, Vec_IntEntry(vLevels, Fanin) );
115
    return Level + fAddLevel;
Alan Mishchenko committed
116
}
117
void Sfm_CreateLevel( Vec_Wec_t * vFanins, Vec_Int_t * vLevels, Vec_Str_t * vEmpty )
Alan Mishchenko committed
118
{
Alan Mishchenko committed
119
    Vec_Int_t * vArray;
Alan Mishchenko committed
120
    int i;
Alan Mishchenko committed
121
    assert( Vec_IntSize(vLevels) == 0 );
Alan Mishchenko committed
122
    Vec_IntFill( vLevels, Vec_WecSize(vFanins), 0 );
Alan Mishchenko committed
123
    Vec_WecForEachLevel( vFanins, vArray, i )
124
        Vec_IntWriteEntry( vLevels, i, Sfm_ObjLevelNew(vArray, vLevels, Sfm_ObjAddsLevelArray(vEmpty, i)) );
Alan Mishchenko committed
125 126 127 128 129 130 131 132 133 134 135 136 137
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
138
static inline int Sfm_ObjLevelNewR( Vec_Int_t * vArray, Vec_Int_t * vLevelsR, int fAddLevel )
Alan Mishchenko committed
139 140 141 142
{
    int k, Fanout, LevelR = 0;
    Vec_IntForEachEntry( vArray, Fanout, k )
        LevelR = Abc_MaxInt( LevelR, Vec_IntEntry(vLevelsR, Fanout) );
143
    return LevelR + fAddLevel;
Alan Mishchenko committed
144
}
145
void Sfm_CreateLevelR( Vec_Wec_t * vFanouts, Vec_Int_t * vLevelsR, Vec_Str_t * vEmpty )
Alan Mishchenko committed
146 147 148 149 150 151
{
    Vec_Int_t * vArray;
    int i;
    assert( Vec_IntSize(vLevelsR) == 0 );
    Vec_IntFill( vLevelsR, Vec_WecSize(vFanouts), 0 );
    Vec_WecForEachLevelReverse( vFanouts, vArray, i )
152
        Vec_IntWriteEntry( vLevelsR, i, Sfm_ObjLevelNewR(vArray, vLevelsR, Sfm_ObjAddsLevelArray(vEmpty, i)) );
Alan Mishchenko committed
153 154 155 156 157 158 159 160 161 162 163 164 165
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
166
Sfm_Ntk_t * Sfm_NtkConstruct( Vec_Wec_t * vFanins, int nPis, int nPos, Vec_Str_t * vFixed, Vec_Str_t * vEmpty, Vec_Wrd_t * vTruths )
Alan Mishchenko committed
167 168 169 170 171 172 173 174 175 176
{
    Sfm_Ntk_t * p;
    Sfm_CheckConsistency( vFanins, nPis, nPos, vFixed );
    p = ABC_CALLOC( Sfm_Ntk_t, 1 );
    p->nObjs    = Vec_WecSize( vFanins );
    p->nPis     = nPis;
    p->nPos     = nPos;
    p->nNodes   = p->nObjs - p->nPis - p->nPos;
    // user data
    p->vFixed   = vFixed;
177
    p->vEmpty   = vEmpty;
Alan Mishchenko committed
178
    p->vTruths  = vTruths;
Alan Mishchenko committed
179 180
    p->vFanins  = *vFanins;
    ABC_FREE( vFanins );
Alan Mishchenko committed
181
    // attributes
Alan Mishchenko committed
182
    Sfm_CreateFanout( &p->vFanins, &p->vFanouts );
183 184
    Sfm_CreateLevel( &p->vFanins, &p->vLevels, vEmpty );
    Sfm_CreateLevelR( &p->vFanouts, &p->vLevelsR, vEmpty );
Alan Mishchenko committed
185 186 187
    Vec_IntFill( &p->vCounts,   p->nObjs,  0 );
    Vec_IntFill( &p->vTravIds,  p->nObjs,  0 );
    Vec_IntFill( &p->vTravIds2, p->nObjs,  0 );
Alan Mishchenko committed
188 189
    Vec_IntFill( &p->vId2Var,   2*p->nObjs, -1 );
    Vec_IntFill( &p->vVar2Id,   2*p->nObjs, -1 );
Alan Mishchenko committed
190 191
    p->vCover   = Vec_IntAlloc( 1 << 16 );
    p->vCnfs    = Sfm_CreateCnf( p );
Alan Mishchenko committed
192 193
    return p;
}
Alan Mishchenko committed
194 195
void Sfm_NtkPrepare( Sfm_Ntk_t * p )
{
Alan Mishchenko committed
196
    p->nLevelMax = Vec_IntFindMax(&p->vLevels) + p->pPars->nGrowthLevel;
Alan Mishchenko committed
197
    p->vNodes    = Vec_IntAlloc( 1000 );
Alan Mishchenko committed
198 199
    p->vDivs     = Vec_IntAlloc( 100 );
    p->vRoots    = Vec_IntAlloc( 1000 );
Alan Mishchenko committed
200
    p->vTfo      = Vec_IntAlloc( 1000 );
Alan Mishchenko committed
201
    p->vDivCexes = Vec_WrdStart( p->pPars->nWinSizeMax );
Alan Mishchenko committed
202 203 204 205
    p->vOrder    = Vec_IntAlloc( 100 );
    p->vDivVars  = Vec_IntAlloc( 100 );
    p->vDivIds   = Vec_IntAlloc( 1000 );
    p->vLits     = Vec_IntAlloc( 100 );
Alan Mishchenko committed
206
    p->vValues   = Vec_IntAlloc( 100 );
Alan Mishchenko committed
207 208
    p->vClauses  = Vec_WecAlloc( 100 );
    p->vFaninMap = Vec_IntAlloc( 10 );
Alan Mishchenko committed
209
    p->pSat      = sat_solver_new();
Alan Mishchenko committed
210
    sat_solver_setnvars( p->pSat, p->pPars->nWinSizeMax );
Alan Mishchenko committed
211
}
Alan Mishchenko committed
212 213
void Sfm_NtkFree( Sfm_Ntk_t * p )
{
Alan Mishchenko committed
214 215
    // user data
    Vec_StrFree( p->vFixed );
216
    Vec_StrFreeP( &p->vEmpty );
Alan Mishchenko committed
217
    Vec_WrdFree( p->vTruths );
Alan Mishchenko committed
218
    Vec_WecErase( &p->vFanins );
Alan Mishchenko committed
219
    // attributes
Alan Mishchenko committed
220 221
    Vec_WecErase( &p->vFanouts );
    ABC_FREE( p->vLevels.pArray );
Alan Mishchenko committed
222
    ABC_FREE( p->vLevelsR.pArray );
Alan Mishchenko committed
223
    ABC_FREE( p->vCounts.pArray );
Alan Mishchenko committed
224
    ABC_FREE( p->vTravIds.pArray );
Alan Mishchenko committed
225
    ABC_FREE( p->vTravIds2.pArray );
Alan Mishchenko committed
226 227 228 229 230 231
    ABC_FREE( p->vId2Var.pArray );
    ABC_FREE( p->vVar2Id.pArray );
    Vec_WecFree( p->vCnfs );
    Vec_IntFree( p->vCover );
    // other data
    Vec_IntFreeP( &p->vNodes );
Alan Mishchenko committed
232 233
    Vec_IntFreeP( &p->vDivs  );
    Vec_IntFreeP( &p->vRoots );
Alan Mishchenko committed
234
    Vec_IntFreeP( &p->vTfo   );
Alan Mishchenko committed
235
    Vec_WrdFreeP( &p->vDivCexes );
Alan Mishchenko committed
236 237
    Vec_IntFreeP( &p->vOrder );
    Vec_IntFreeP( &p->vDivVars );
Alan Mishchenko committed
238
    Vec_IntFreeP( &p->vDivIds );
Alan Mishchenko committed
239
    Vec_IntFreeP( &p->vLits  );
Alan Mishchenko committed
240
    Vec_IntFreeP( &p->vValues );
Alan Mishchenko committed
241 242
    Vec_WecFreeP( &p->vClauses );
    Vec_IntFreeP( &p->vFaninMap );
Alan Mishchenko committed
243
    if ( p->pSat  ) sat_solver_delete( p->pSat );
Alan Mishchenko committed
244 245 246
    ABC_FREE( p );
}

Alan Mishchenko committed
247 248
/**Function*************************************************************

Alan Mishchenko committed
249 250 251 252 253 254 255 256 257 258 259 260
  Synopsis    [Performs resubstitution for the node.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Sfm_NtkRemoveFanin( Sfm_Ntk_t * p, int iNode, int iFanin )
{
    int RetValue;
Alan Mishchenko committed
261 262
    assert( Sfm_ObjIsNode(p, iNode) );
    assert( !Sfm_ObjIsPo(p, iFanin) );
Alan Mishchenko committed
263 264 265 266 267 268 269 270 271
    RetValue = Vec_IntRemove( Sfm_ObjFiArray(p, iNode), iFanin );
    assert( RetValue );
    RetValue = Vec_IntRemove( Sfm_ObjFoArray(p, iFanin), iNode );
    assert( RetValue );
}
void Sfm_NtkAddFanin( Sfm_Ntk_t * p, int iNode, int iFanin )
{
    if ( iFanin < 0 )
        return;
Alan Mishchenko committed
272 273
    assert( Sfm_ObjIsNode(p, iNode) );
    assert( !Sfm_ObjIsPo(p, iFanin) );
Alan Mishchenko committed
274 275 276 277 278 279 280 281
    assert( Vec_IntFind( Sfm_ObjFiArray(p, iNode), iFanin ) == -1 );
    assert( Vec_IntFind( Sfm_ObjFoArray(p, iFanin), iNode ) == -1 );
    Vec_IntPush( Sfm_ObjFiArray(p, iNode), iFanin );
    Vec_IntPush( Sfm_ObjFoArray(p, iFanin), iNode );
}
void Sfm_NtkDeleteObj_rec( Sfm_Ntk_t * p, int iNode )
{
    int i, iFanin;
Alan Mishchenko committed
282
    if ( Sfm_ObjFanoutNum(p, iNode) > 0 || Sfm_ObjIsPi(p, iNode) || Sfm_ObjIsFixed(p, iNode) )
Alan Mishchenko committed
283
        return;
Alan Mishchenko committed
284
    assert( Sfm_ObjIsNode(p, iNode) );
Alan Mishchenko committed
285 286 287
    Sfm_ObjForEachFanin( p, iNode, iFanin, i )
    {
        int RetValue = Vec_IntRemove( Sfm_ObjFoArray(p, iFanin), iNode );  assert( RetValue );
Alan Mishchenko committed
288
        Sfm_NtkDeleteObj_rec( p, iFanin );
Alan Mishchenko committed
289 290
    }
    Vec_IntClear( Sfm_ObjFiArray(p, iNode) );
Alan Mishchenko committed
291
    Vec_WrdWriteEntry( p->vTruths, iNode, (word)0 );
Alan Mishchenko committed
292 293 294 295
}
void Sfm_NtkUpdateLevel_rec( Sfm_Ntk_t * p, int iNode )
{
    int i, iFanout;
296
    int LevelNew = Sfm_ObjLevelNew( Sfm_ObjFiArray(p, iNode), &p->vLevels, Sfm_ObjAddsLevel(p, iNode) );
Alan Mishchenko committed
297 298 299
    if ( LevelNew == Sfm_ObjLevel(p, iNode) )
        return;
    Sfm_ObjSetLevel( p, iNode, LevelNew );
Alan Mishchenko committed
300 301 302 303 304 305
    Sfm_ObjForEachFanout( p, iNode, iFanout, i )
        Sfm_NtkUpdateLevel_rec( p, iFanout );
}
void Sfm_NtkUpdateLevelR_rec( Sfm_Ntk_t * p, int iNode )
{
    int i, iFanin;
306
    int LevelNew = Sfm_ObjLevelNewR( Sfm_ObjFoArray(p, iNode), &p->vLevelsR, Sfm_ObjAddsLevel(p, iNode) );
Alan Mishchenko committed
307 308 309 310 311
    if ( LevelNew == Sfm_ObjLevelR(p, iNode) )
        return;
    Sfm_ObjSetLevelR( p, iNode, LevelNew );
    Sfm_ObjForEachFanin( p, iNode, iFanin, i )
        Sfm_NtkUpdateLevelR_rec( p, iFanin );
Alan Mishchenko committed
312 313 314 315
}
void Sfm_NtkUpdate( Sfm_Ntk_t * p, int iNode, int f, int iFaninNew, word uTruth )
{
    int iFanin = Sfm_ObjFanin( p, iNode, f );
Alan Mishchenko committed
316
    assert( Sfm_ObjIsNode(p, iNode) );
Alan Mishchenko committed
317 318 319 320 321 322
    assert( iFanin != iFaninNew );
    if ( uTruth == 0 || ~uTruth == 0 )
    {
        Sfm_ObjForEachFanin( p, iNode, iFanin, f )
        {
            int RetValue = Vec_IntRemove( Sfm_ObjFoArray(p, iFanin), iNode );  assert( RetValue );
Alan Mishchenko committed
323
            Sfm_NtkDeleteObj_rec( p, iFanin );
Alan Mishchenko committed
324 325 326 327 328 329 330 331 332 333 334
        }
        Vec_IntClear( Sfm_ObjFiArray(p, iNode) );
    }
    else
    {
        // replace old fanin by new fanin
        Sfm_NtkRemoveFanin( p, iNode, iFanin );
        Sfm_NtkAddFanin( p, iNode, iFaninNew );
        // recursively remove MFFC
        Sfm_NtkDeleteObj_rec( p, iFanin );
    }
Alan Mishchenko committed
335 336
    // update logic level
    Sfm_NtkUpdateLevel_rec( p, iNode );
Alan Mishchenko committed
337 338 339 340
    if ( iFaninNew != -1 )
        Sfm_NtkUpdateLevelR_rec( p, iFaninNew );
    if ( Sfm_ObjFanoutNum(p, iFanin) > 0 )
        Sfm_NtkUpdateLevelR_rec( p, iFanin );
Alan Mishchenko committed
341 342 343 344 345 346 347
    // update truth table
    Vec_WrdWriteEntry( p->vTruths, iNode, uTruth );
    Sfm_TruthToCnf( uTruth, Sfm_ObjFaninNum(p, iNode), p->vCover, (Vec_Str_t *)Vec_WecEntry(p->vCnfs, iNode) );
}

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

Alan Mishchenko committed
348 349 350 351 352 353 354 355 356 357 358
  Synopsis    [Public APIs of this network.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Vec_Int_t *  Sfm_NodeReadFanins( Sfm_Ntk_t * p, int i )
{
Alan Mishchenko committed
359
    return Vec_WecEntry( &p->vFanins, i );
Alan Mishchenko committed
360
}
Alan Mishchenko committed
361
word * Sfm_NodeReadTruth( Sfm_Ntk_t * p, int i )
Alan Mishchenko committed
362
{
Alan Mishchenko committed
363
    return Vec_WrdEntryP( p->vTruths, i );
Alan Mishchenko committed
364 365 366 367 368
}
int Sfm_NodeReadFixed( Sfm_Ntk_t * p, int i )
{
    return (int)Vec_StrEntry( p->vFixed, i );
}
Alan Mishchenko committed
369 370 371 372
int Sfm_NodeReadUsed( Sfm_Ntk_t * p, int i )
{
    return (Sfm_ObjFaninNum(p, i) > 0) || (Sfm_ObjFanoutNum(p, i) > 0);
}
Alan Mishchenko committed
373 374 375 376 377 378 379 380

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


ABC_NAMESPACE_IMPL_END