abcMfs.c 14.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
/**CFile****************************************************************

  FileName    [abcMfs.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [Network and node package.]

  Synopsis    [Optimization with don't-cares.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

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

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

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

#include "base/abc/abc.h"
Alan Mishchenko committed
22
#include "bool/kit/kit.h"
Alan Mishchenko committed
23
#include "opt/sfm/sfm.h"
24
#include "base/io/ioAbc.h"
Alan Mishchenko committed
25 26 27 28 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

ABC_NAMESPACE_IMPL_START


////////////////////////////////////////////////////////////////////////
///                        DECLARATIONS                              ///
////////////////////////////////////////////////////////////////////////
 
////////////////////////////////////////////////////////////////////////
///                     FUNCTION DEFINITIONS                         ///
////////////////////////////////////////////////////////////////////////

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Vec_Ptr_t * Abc_NtkAssignIDs( Abc_Ntk_t * pNtk )
{
    Vec_Ptr_t * vNodes;
    Abc_Obj_t * pObj;
    int i;
    vNodes = Abc_NtkDfs( pNtk, 0 );
    Abc_NtkCleanCopy( pNtk );
Alan Mishchenko committed
55
    Abc_NtkForEachCi( pNtk, pObj, i )
Alan Mishchenko committed
56 57
        pObj->iTemp = i;
    Vec_PtrForEachEntry( Abc_Obj_t *, vNodes, pObj, i )
Alan Mishchenko committed
58 59 60
        pObj->iTemp = Abc_NtkCiNum(pNtk) + i;
    Abc_NtkForEachCo( pNtk, pObj, i )
        pObj->iTemp = Abc_NtkCiNum(pNtk) + Vec_PtrSize(vNodes) + i;
Alan Mishchenko committed
61 62
    return vNodes;
}
63 64 65 66 67 68
Vec_Ptr_t * Abc_NtkAssignIDs2( Abc_Ntk_t * pNtk )
{
    Vec_Ptr_t * vNodes;
    Abc_Obj_t * pObj;
    int i;
    Abc_NtkCleanCopy( pNtk );
Alan Mishchenko committed
69
    Abc_NtkForEachCi( pNtk, pObj, i )
70 71 72 73
        pObj->iTemp = i;
    vNodes = Vec_PtrAlloc( Abc_NtkNodeNum(pNtk) );
    Abc_NtkForEachNode( pNtk, pObj, i )
    {
74
        pObj->iTemp = Abc_NtkCiNum(pNtk) + Vec_PtrSize(vNodes);
75 76
        Vec_PtrPush( vNodes, pObj );
    }
77
    assert( Vec_PtrSize(vNodes) == Abc_NtkNodeNum(pNtk) );
Alan Mishchenko committed
78 79
    Abc_NtkForEachCo( pNtk, pObj, i )
        pObj->iTemp = Abc_NtkCiNum(pNtk) + Vec_PtrSize(vNodes) + i;
80 81
    return vNodes;
}
Alan Mishchenko committed
82 83 84 85 86 87 88 89 90 91 92 93

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

  Synopsis    [Extracts information about the network.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
94
Sfm_Ntk_t * Abc_NtkExtractMfs( Abc_Ntk_t * pNtk, int nFirstFixed )
Alan Mishchenko committed
95 96 97 98 99 100 101 102
{
    Vec_Ptr_t * vNodes;
    Vec_Wec_t * vFanins;
    Vec_Str_t * vFixed;
    Vec_Wrd_t * vTruths;
    Vec_Int_t * vArray;
    Abc_Obj_t * pObj, * pFanin;
    int i, k, nObjs;
103
    vNodes  = nFirstFixed ? Abc_NtkAssignIDs2(pNtk) : Abc_NtkAssignIDs(pNtk);
Alan Mishchenko committed
104
    nObjs   = Abc_NtkCiNum(pNtk) + Vec_PtrSize(vNodes) + Abc_NtkCoNum(pNtk);
Alan Mishchenko committed
105 106 107 108 109
    vFanins = Vec_WecStart( nObjs );
    vFixed  = Vec_StrStart( nObjs );
    vTruths = Vec_WrdStart( nObjs );
    Vec_PtrForEachEntry( Abc_Obj_t *, vNodes, pObj, i )
    {
Alan Mishchenko committed
110 111 112 113
        word uTruth = Abc_SopToTruth((char *)pObj->pData, Abc_ObjFaninNum(pObj));
        Vec_WrdWriteEntry( vTruths, pObj->iTemp, uTruth );
        if ( uTruth == 0 || ~uTruth == 0 )
            continue;
Alan Mishchenko committed
114
        vArray = Vec_WecEntry( vFanins, pObj->iTemp );
Alan Mishchenko committed
115 116 117 118
        Vec_IntGrow( vArray, Abc_ObjFaninNum(pObj) );
        Abc_ObjForEachFanin( pObj, pFanin, k )
            Vec_IntPush( vArray, pFanin->iTemp );
    }
Alan Mishchenko committed
119
    Abc_NtkForEachCo( pNtk, pObj, i )
Alan Mishchenko committed
120 121 122 123 124 125 126
    {
        vArray = Vec_WecEntry( vFanins, pObj->iTemp );
        Vec_IntGrow( vArray, Abc_ObjFaninNum(pObj) );
        Abc_ObjForEachFanin( pObj, pFanin, k )
            Vec_IntPush( vArray, pFanin->iTemp );
    }
    Vec_PtrFree( vNodes );
Alan Mishchenko committed
127
    for ( i = Abc_NtkCiNum(pNtk); i < Abc_NtkCiNum(pNtk) + nFirstFixed; i++ )
128
        Vec_StrWriteEntry( vFixed, i, (char)1 );
Alan Mishchenko committed
129 130 131 132 133
    // update fixed
    assert( nFirstFixed >= 0 && nFirstFixed < Abc_NtkNodeNum(pNtk) );
//    for ( i = Abc_NtkCiNum(pNtk); i + Abc_NtkCoNum(pNtk) < Abc_NtkObjNum(pNtk); i++ )
//        if ( rand() % 10 == 0 )
//            Vec_StrWriteEntry( vFixed, i, (char)1 );
134
    return Sfm_NtkConstruct( vFanins, Abc_NtkCiNum(pNtk), Abc_NtkCoNum(pNtk), vFixed, NULL, vTruths );
Alan Mishchenko committed
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
Sfm_Ntk_t * Abc_NtkExtractMfs2( Abc_Ntk_t * pNtk, int iPivot )
{
    Vec_Ptr_t * vNodes;
    Vec_Wec_t * vFanins;
    Vec_Str_t * vFixed;
    Vec_Wrd_t * vTruths;
    Vec_Int_t * vArray;
    Abc_Obj_t * pObj, * pFanin;
    int i, k, nObjs;
    vNodes  = Abc_NtkAssignIDs2(pNtk);
    nObjs   = Abc_NtkCiNum(pNtk) + Vec_PtrSize(vNodes) + Abc_NtkCoNum(pNtk);
    vFanins = Vec_WecStart( nObjs );
    vFixed  = Vec_StrStart( nObjs );
    vTruths = Vec_WrdStart( nObjs );
    Vec_PtrForEachEntry( Abc_Obj_t *, vNodes, pObj, i )
    {
        word uTruth = Abc_SopToTruth((char *)pObj->pData, Abc_ObjFaninNum(pObj));
        Vec_WrdWriteEntry( vTruths, pObj->iTemp, uTruth );
        if ( uTruth == 0 || ~uTruth == 0 )
            continue;
        vArray = Vec_WecEntry( vFanins, pObj->iTemp );
        Vec_IntGrow( vArray, Abc_ObjFaninNum(pObj) );
        Abc_ObjForEachFanin( pObj, pFanin, k )
            Vec_IntPush( vArray, pFanin->iTemp );
    }
    Abc_NtkForEachCo( pNtk, pObj, i )
    {
        vArray = Vec_WecEntry( vFanins, pObj->iTemp );
        Vec_IntGrow( vArray, Abc_ObjFaninNum(pObj) );
        Abc_ObjForEachFanin( pObj, pFanin, k )
            Vec_IntPush( vArray, pFanin->iTemp );
    }
    Vec_PtrFree( vNodes );
    // set fixed attributes
    Abc_NtkForEachNode( pNtk, pObj, i )
        if ( i >= iPivot )
172
            Vec_StrWriteEntry( vFixed, pObj->iTemp, (char)1 );
173 174
    return Sfm_NtkConstruct( vFanins, Abc_NtkCiNum(pNtk), Abc_NtkCoNum(pNtk), vFixed, NULL, vTruths );
}
Alan Mishchenko committed
175 176 177 178 179 180 181 182 183 184 185 186 187 188

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Abc_NtkInsertMfs( Abc_Ntk_t * pNtk, Sfm_Ntk_t * p )
{
Alan Mishchenko committed
189
    Vec_Int_t * vCover, * vMap, * vArray;
Alan Mishchenko committed
190
    Abc_Obj_t * pNode;
Alan Mishchenko committed
191
    word * pTruth;
Alan Mishchenko committed
192
    int i, k, Fanin;
Alan Mishchenko committed
193 194
    // map new IDs into old nodes
    vMap = Vec_IntStart( Abc_NtkObjNumMax(pNtk) );
Alan Mishchenko committed
195
    Abc_NtkForEachCi( pNtk, pNode, i )
Alan Mishchenko committed
196 197 198 199 200 201
        Vec_IntWriteEntry( vMap, pNode->iTemp, Abc_ObjId(pNode) );
    Abc_NtkForEachNode( pNtk, pNode, i )
        if ( pNode->iTemp > 0 )
            Vec_IntWriteEntry( vMap, pNode->iTemp, Abc_ObjId(pNode) );
    // remove old fanins
    Abc_NtkForEachNode( pNtk, pNode, i )
Alan Mishchenko committed
202 203
        if ( !Sfm_NodeReadFixed(p, pNode->iTemp) )
            Abc_ObjRemoveFanins( pNode );
Alan Mishchenko committed
204
    // create new fanins
Alan Mishchenko committed
205
    vCover = Vec_IntAlloc( 1 << 16 );
Alan Mishchenko committed
206
    Abc_NtkForEachNode( pNtk, pNode, i )
Alan Mishchenko committed
207 208 209 210
    {
        if ( pNode->iTemp == 0 || Sfm_NodeReadFixed(p, pNode->iTemp) )
            continue;
        if ( !Sfm_NodeReadUsed(p, pNode->iTemp) )
Alan Mishchenko committed
211
        {
Alan Mishchenko committed
212 213
            Abc_NtkDeleteObj( pNode );
            continue;
Alan Mishchenko committed
214
        }
Alan Mishchenko committed
215 216 217 218 219
        // update fanins
        vArray = Sfm_NodeReadFanins( p, pNode->iTemp );
        Vec_IntForEachEntry( vArray, Fanin, k )
            Abc_ObjAddFanin( pNode, Abc_NtkObj(pNtk, Vec_IntEntry(vMap, Fanin)) );
        pTruth = Sfm_NodeReadTruth( p, pNode->iTemp );
220
        pNode->pData = Abc_SopCreateFromTruthIsop( (Mem_Flex_t *)pNtk->pManFunc, Vec_IntSize(vArray), pTruth, vCover );
Alan Mishchenko committed
221
        assert( Abc_SopGetVarNum((char *)pNode->pData) == Vec_IntSize(vArray) );
Alan Mishchenko committed
222
    }
Alan Mishchenko committed
223
    Vec_IntFree( vCover );
Alan Mishchenko committed
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
    Vec_IntFree( vMap );
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Abc_NtkPerformMfs( Abc_Ntk_t * pNtk, Sfm_Par_t * pPars )
{
    Sfm_Ntk_t * p;
    int nFaninMax, nNodes;
Alan Mishchenko committed
242
    assert( Abc_NtkIsLogic(pNtk) );
243
    Abc_NtkSweep( pNtk, 0 );
Alan Mishchenko committed
244 245 246 247 248
    // count fanouts
    nFaninMax = Abc_NtkGetFaninMax( pNtk );
    if ( nFaninMax > 6 )
    {
        Abc_Print( 1, "Currently \"mfs\" cannot process the network containing nodes with more than 6 fanins.\n" );
249
        return 1;
Alan Mishchenko committed
250
    }
Alan Mishchenko committed
251
    if ( !Abc_NtkHasSop(pNtk) )
252
        if ( !Abc_NtkToSop( pNtk, -1, ABC_INFINITY ) )
253 254 255 256
        {
            printf( "Conversion to SOP has failed due to low resource limit.\n" );
            return 0;
        }
Alan Mishchenko committed
257
    // collect information
258
    p = Abc_NtkExtractMfs( pNtk, pPars->nFirstFixed );
Alan Mishchenko committed
259 260 261
    // perform optimization
    nNodes = Sfm_NtkPerform( p, pPars );
    if ( nNodes == 0 )
Alan Mishchenko committed
262 263 264
    {
//        Abc_Print( 1, "The network is not changed by \"mfs\".\n" );
    }
Alan Mishchenko committed
265 266 267
    else
    {
        Abc_NtkInsertMfs( pNtk, p );
Alan Mishchenko committed
268 269
        if( pPars->fVerbose )
            Abc_Print( 1, "The network has %d nodes changed by \"mfs\".\n", nNodes );
Alan Mishchenko committed
270 271 272 273 274
    }
    Sfm_NtkFree( p );
    return 1;
}

275

276 277 278 279 280 281 282 283 284 285 286 287

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

  Synopsis    [Unrolls logic network while dropping some next-state functions.]

  Description [Returns the unrolled network.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
288
Abc_Ntk_t * Abc_NtkUnrollAndDrop( Abc_Ntk_t * p, int nFrames, int nFramesAdd, Vec_Int_t * vFlops, int * piPivot )
289 290 291 292 293
{
    Abc_Ntk_t * pNtk; 
    Abc_Obj_t * pFanin, * pNode;
    Vec_Ptr_t * vNodes;
    int i, k, f, Value;
294
    assert( nFramesAdd >= 0 );
295 296 297 298 299 300 301 302 303 304 305
    assert( Abc_NtkIsLogic(p) );
    assert( Vec_IntSize(vFlops) == Abc_NtkLatchNum(p) );
    *piPivot = -1;
    // start the network
    pNtk = Abc_NtkAlloc( p->ntkType, p->ntkFunc, 1 );
    pNtk->pName = Extra_UtilStrsav(Abc_NtkName(p));
    // add CIs for the new network
    Abc_NtkForEachCi( p, pNode, i )
        pNode->pCopy = Abc_NtkCreatePi( pNtk );
    // iterate unrolling
    vNodes = Abc_NtkDfs( p, 0 );
306
    for ( f = 0; f <= nFrames + nFramesAdd; f++ )
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
    {
        if ( f > 0 )
        {
            Abc_NtkForEachPi( p, pNode, i )
                pNode->pCopy = Abc_NtkCreatePi( pNtk );
        }
        Vec_PtrForEachEntry( Abc_Obj_t *, vNodes, pNode, i )
        {
            Abc_NtkDupObj( pNtk, pNode, 0 );
            Abc_ObjForEachFanin( pNode, pFanin, k )
                Abc_ObjAddFanin( pNode->pCopy, pFanin->pCopy );
        }
        Abc_NtkForEachCo( p, pNode, i )
            pNode->pCopy = Abc_ObjFanin0(pNode)->pCopy;
        Abc_NtkForEachPo( p, pNode, i )
            Abc_ObjAddFanin( Abc_NtkCreatePo(pNtk), pNode->pCopy );
        // add buffers
        if ( f == 0 )
        {
            *piPivot = Abc_NtkObjNum(pNtk);
//            Abc_NtkForEachLatchInput( p, pNode, i )
//                pNode->pCopy = Abc_NtkCreateNodeBuf( pNtk, pNode->pCopy );
        }
        // transfer to flop outputs
        Abc_NtkForEachLatch( p, pNode, i )
            Abc_ObjFanout0(pNode)->pCopy = Abc_ObjFanin0(pNode)->pCopy;
        // add final POs
334
        if ( f > nFramesAdd )
335 336 337 338 339 340 341 342 343 344 345 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 380 381 382 383 384 385 386 387
        {
            Vec_IntForEachEntry( vFlops, Value, i )
            {
                if ( Value == 0 )
                    continue;
                pNode = Abc_NtkCo( p, Abc_NtkPoNum(p) + i );
                Abc_ObjAddFanin( Abc_NtkCreatePo(pNtk), pNode->pCopy );
            }
        }
    }
    Vec_PtrFree( vNodes );
    Abc_NtkAddDummyPiNames( pNtk );
    Abc_NtkAddDummyPoNames( pNtk );
    // perform combinational cleanup
    Abc_NtkCleanup( pNtk, 0 );
    if ( !Abc_NtkCheck( pNtk ) )
        fprintf( stdout, "Abc_NtkCreateFromNode(): Network check has failed.\n" );
    return pNtk;
}

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

  Synopsis    [Updates the original network to include optimized nodes.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Abc_NtkReinsertNodes( Abc_Ntk_t * p, Abc_Ntk_t * pNtk, int iPivot )
{
    Abc_Obj_t * pNode, * pNodeNew, * pFaninNew;
    Vec_Ptr_t * vNodes;
    int i, k;
    assert( Abc_NtkIsLogic(p) );
    assert( Abc_NtkCiNum(p) <= Abc_NtkCiNum(pNtk) );
    vNodes = Abc_NtkDfs( p, 0 );
    // clean old network
    Abc_NtkCleanCopy( p );
    Abc_NtkForEachNode( p, pNode, i )
    {
        Abc_ObjRemoveFanins( pNode );
        pNode->pData = Abc_SopRegister( (Mem_Flex_t *)p->pManFunc, (char *)" 0\n" );
    }
    // map CIs
    Abc_NtkForEachCi( p, pNode, i )
        Abc_NtkCi(pNtk, i)->pCopy = pNode;
    // map internal nodes
    assert( Vec_PtrSize(vNodes) + Abc_NtkCiNum(p) + Abc_NtkPoNum(p) == iPivot );
    Vec_PtrForEachEntry( Abc_Obj_t *, vNodes, pNode, i )
    {
388
        pNodeNew = Abc_NtkObj( pNtk, Abc_NtkCiNum(p) + i + 1 );
389 390 391 392 393 394 395
        if ( pNodeNew == NULL )
            continue;
        pNodeNew->pCopy = pNode;
    }
    // connect internal nodes
    Vec_PtrForEachEntry( Abc_Obj_t *, vNodes, pNode, i )
    {
396
        pNodeNew = Abc_NtkObj( pNtk, Abc_NtkCiNum(p) + i + 1 );
397 398 399 400 401 402 403 404 405 406
        if ( pNodeNew == NULL )
            continue;
        assert( pNodeNew->pCopy == pNode );
        Abc_ObjForEachFanin( pNodeNew, pFaninNew, k )
            Abc_ObjAddFanin( pNodeNew->pCopy, pFaninNew->pCopy );
        pNode->pData = Abc_SopRegister( (Mem_Flex_t *)p->pManFunc, (char *)pNodeNew->pData );
    }
    Vec_PtrFree( vNodes );
}

407 408 409 410 411 412 413 414 415 416 417
/**Function*************************************************************

  Synopsis    [Performs MFS for the unrolled network.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
418
int Abc_NtkMfsAfterICheck( Abc_Ntk_t * p, int nFrames, int nFramesAdd, Vec_Int_t * vFlops, Sfm_Par_t * pPars )
419 420 421 422 423 424 425 426 427 428 429
{
    Sfm_Ntk_t * pp;
    int nFaninMax, nNodes;
    Abc_Ntk_t * pNtk;
    int iPivot;
    assert( Abc_NtkIsLogic(p) );
    // count fanouts
    nFaninMax = Abc_NtkGetFaninMax( p );
    if ( nFaninMax > 6 )
    {
        Abc_Print( 1, "Currently \"mfs\" cannot process the network containing nodes with more than 6 fanins.\n" );
430
        return 0;
431
    }
432
    if ( !Abc_NtkHasSop(p) )
433
        Abc_NtkToSop( p, -1, ABC_INFINITY );
434
    // derive unfolded network
435
    pNtk = Abc_NtkUnrollAndDrop( p, nFrames, nFramesAdd, vFlops, &iPivot );
436
    Io_WriteBlifLogic( pNtk, "unroll_dump.blif", 0 );
437 438 439 440 441 442 443 444 445 446 447 448 449
    // collect information
    pp = Abc_NtkExtractMfs2( pNtk, iPivot );
    // perform optimization
    nNodes = Sfm_NtkPerform( pp, pPars );
    if ( nNodes == 0 )
    {
//        Abc_Print( 1, "The network is not changed by \"mfs\".\n" );
    }
    else
    {
        Abc_NtkInsertMfs( pNtk, pp );
        if( pPars->fVerbose )
            Abc_Print( 1, "The network has %d nodes changed by \"mfs\".\n", nNodes );
450
        Abc_NtkReinsertNodes( p, pNtk, iPivot );
451
    }
452
    Abc_NtkDelete( pNtk );
453
    Sfm_NtkFree( pp );
454 455 456
    // perform final sweep
    Abc_NtkSweep( p, 0 );
    if ( !Abc_NtkHasSop(p) )
457
        Abc_NtkToSop( p, -1, ABC_INFINITY );
458
    return 1;
459 460 461 462

}


Alan Mishchenko committed
463 464 465 466 467 468 469
////////////////////////////////////////////////////////////////////////
///                       END OF FILE                                ///
////////////////////////////////////////////////////////////////////////


ABC_NAMESPACE_IMPL_END