nwkSpeedup.c 13.3 KB
Newer Older
Alan Mishchenko committed
1 2
/**CFile****************************************************************

Alan Mishchenko committed
3
  FileName    [nwkSpeedup.c]
Alan Mishchenko committed
4 5 6 7 8

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [Netlist representation.]

Alan Mishchenko committed
9
  Synopsis    [Global delay optimization using structural choices.]
Alan Mishchenko committed
10 11 12 13 14 15 16

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

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

Alan Mishchenko committed
17
  Revision    [$Id: nwkSpeedup.c,v 1.00 2005/06/20 00:00:00 alanmi Exp $]
Alan Mishchenko committed
18 19 20

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

Alan Mishchenko committed
21
#include "nwk.h"
Alan Mishchenko committed
22

23 24 25
ABC_NAMESPACE_IMPL_START


Alan Mishchenko committed
26 27 28 29 30 31 32 33 34 35
////////////////////////////////////////////////////////////////////////
///                        DECLARATIONS                              ///
////////////////////////////////////////////////////////////////////////

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

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

Alan Mishchenko committed
36
  Synopsis    [Adds strashed nodes for one node.]
Alan Mishchenko committed
37 38 39 40 41 42 43 44

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
45
int Aig_ManSpeedupNode_rec( Aig_Man_t * pAig, Aig_Obj_t * pNode, Vec_Ptr_t * vNodes )
Alan Mishchenko committed
46
{
Alan Mishchenko committed
47 48
    if ( Aig_ObjIsTravIdCurrent(pAig, pNode) )
        return 1;
49
    if ( Aig_ObjIsCi(pNode) )
Alan Mishchenko committed
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
        return 0;
    assert( Aig_ObjIsNode(pNode) );
    Aig_ObjSetTravIdCurrent( pAig, pNode );
    if ( !Aig_ManSpeedupNode_rec( pAig, Aig_ObjFanin0(pNode), vNodes ) )
        return 0;
    if ( !Aig_ManSpeedupNode_rec( pAig, Aig_ObjFanin1(pNode), vNodes ) )
        return 0;
    Vec_PtrPush( vNodes, pNode );
    return 1;
}

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

  Synopsis    [Adds strashed nodes for one node.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Aig_ManSpeedupNode( Nwk_Man_t * pNtk, Aig_Man_t * pAig, Nwk_Obj_t * pNode, Vec_Ptr_t * vLeaves, Vec_Ptr_t * vTimes )
{
    Vec_Ptr_t * vNodes;
    Nwk_Obj_t * pObj, * pObj2;
    Aig_Obj_t * ppCofs[32], * pAnd, * pTemp;
    int nCofs, i, k, nSkip;

    // quit of regulars are the same
80 81 82
    Vec_PtrForEachEntry( Nwk_Obj_t *, vLeaves, pObj, i )
    Vec_PtrForEachEntry( Nwk_Obj_t *, vLeaves, pObj2, k )
        if ( i != k && Aig_Regular((Aig_Obj_t *)pObj->pCopy) == Aig_Regular((Aig_Obj_t *)pObj2->pCopy) )
Alan Mishchenko committed
83 84 85 86 87 88 89 90 91
        {
//            printf( "Identical after structural hashing!!!\n" );
            return;
        }

    // collect the AIG nodes
    vNodes = Vec_PtrAlloc( 100 );
    Aig_ManIncrementTravId( pAig );
    Aig_ObjSetTravIdCurrent( pAig, Aig_ManConst1(pAig) );
92
    Vec_PtrForEachEntry( Nwk_Obj_t *, vLeaves, pObj, i )
Alan Mishchenko committed
93
    {
94
        pAnd = (Aig_Obj_t *)pObj->pCopy;
Alan Mishchenko committed
95 96 97
        Aig_ObjSetTravIdCurrent( pAig, Aig_Regular(pAnd) );
    }
    // traverse from the root node
98
    pAnd = (Aig_Obj_t *)pNode->pCopy;
Alan Mishchenko committed
99 100 101 102 103 104 105 106 107 108 109
    if ( !Aig_ManSpeedupNode_rec( pAig, Aig_Regular(pAnd), vNodes ) )
    {
//        printf( "Bad node!!!\n" );
        Vec_PtrFree( vNodes );
        return;
    }

    // derive cofactors
    nCofs = (1 << Vec_PtrSize(vTimes));
    for ( i = 0; i < nCofs; i++ )
    {
110
        Vec_PtrForEachEntry( Nwk_Obj_t *, vLeaves, pObj, k )
Alan Mishchenko committed
111
        {
112
            pAnd = (Aig_Obj_t *)pObj->pCopy;
Alan Mishchenko committed
113 114
            Aig_Regular(pAnd)->pData = Aig_Regular(pAnd);
        }
115
        Vec_PtrForEachEntry( Nwk_Obj_t *, vTimes, pObj, k )
Alan Mishchenko committed
116
        {
117
            pAnd = (Aig_Obj_t *)pObj->pCopy;
Alan Mishchenko committed
118 119
            Aig_Regular(pAnd)->pData = Aig_NotCond( Aig_ManConst1(pAig), ((i & (1<<k)) == 0) );
        }
120
        Vec_PtrForEachEntry( Aig_Obj_t *, vNodes, pTemp, k )
Alan Mishchenko committed
121 122
            pTemp->pData = Aig_And( pAig, Aig_ObjChild0Copy(pTemp), Aig_ObjChild1Copy(pTemp) );
        // save the result
123 124
        pAnd = (Aig_Obj_t *)pNode->pCopy;
        ppCofs[i] = Aig_NotCond( (Aig_Obj_t *)Aig_Regular(pAnd)->pData, Aig_IsComplement(pAnd) );
Alan Mishchenko committed
125 126 127 128 129 130 131
    }
    Vec_PtrFree( vNodes );

//Nwk_ObjAddFanin( Nwk_ManCreatePo(pAig), ppCofs[0] );
//Nwk_ObjAddFanin( Nwk_ManCreatePo(pAig), ppCofs[1] );

    // collect the resulting tree
132
    Vec_PtrForEachEntry( Nwk_Obj_t *, vTimes, pObj, k )
Alan Mishchenko committed
133 134
        for ( nSkip = (1<<k), i = 0; i < nCofs; i += 2*nSkip )
        {
135
            pAnd = (Aig_Obj_t *)pObj->pCopy;
Alan Mishchenko committed
136 137 138 139 140
            ppCofs[i] = Aig_Mux( pAig, Aig_Regular(pAnd), ppCofs[i+nSkip], ppCofs[i] );
        }
//Nwk_ObjAddFanin( Nwk_ManCreatePo(pAig), ppCofs[0] );

    // create choice node
141
    pAnd  = Aig_Regular((Aig_Obj_t *)pNode->pCopy); // repr
Alan Mishchenko committed
142
    pTemp = Aig_Regular(ppCofs[0]);    // new
Alan Mishchenko committed
143
    if ( Aig_ObjEquiv(pAig, pAnd) == NULL && Aig_ObjEquiv(pAig, pTemp) == NULL && !Aig_ObjCheckTfi(pAig, pTemp, pAnd) )
Alan Mishchenko committed
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
        pAig->pEquivs[pAnd->Id] = pTemp;
}

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

  Synopsis    [Determines timing-critical edges of the node.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
unsigned Nwk_ManDelayTraceTCEdges( Nwk_Man_t * pNtk, Nwk_Obj_t * pNode, float tDelta, int fUseLutLib )
{
    int pPinPerm[32];
    float pPinDelays[32];
162
    If_LibLut_t * pLutLib = fUseLutLib? pNtk->pLutLib : NULL;
Alan Mishchenko committed
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
    Nwk_Obj_t * pFanin;
    unsigned uResult = 0;
    float tRequired, * pDelays;
    int k;
    tRequired = Nwk_ObjRequired(pNode);
    if ( pLutLib == NULL )
    {
        Nwk_ObjForEachFanin( pNode, pFanin, k )
            if ( tRequired < Nwk_ObjArrival(pFanin) + 1.0 + tDelta )
                uResult |= (1 << k);
    }
    else if ( !pLutLib->fVarPinDelays )
    {
        pDelays = pLutLib->pLutDelays[Nwk_ObjFaninNum(pNode)];
        Nwk_ObjForEachFanin( pNode, pFanin, k )
            if ( tRequired < Nwk_ObjArrival(pFanin) + pDelays[0] + tDelta )
                uResult |= (1 << k);
    }
    else
    {
        pDelays = pLutLib->pLutDelays[Nwk_ObjFaninNum(pNode)];
        Nwk_ManDelayTraceSortPins( pNode, pPinPerm, pPinDelays );
        Nwk_ObjForEachFanin( pNode, pFanin, k )
            if ( tRequired < Nwk_ObjArrival(Nwk_ObjFanin(pNode,pPinPerm[k])) + pDelays[k] + tDelta )
                uResult |= (1 << pPinPerm[k]);
    }
    return uResult;
}

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

  Synopsis    [Adds choices to speed up the network by the given percentage.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Aig_Man_t * Nwk_ManSpeedup( Nwk_Man_t * pNtk, int fUseLutLib, int Percentage, int Degree, int fVerbose, int fVeryVerbose )
{
    Aig_Man_t * pAig, * pTemp;
    Vec_Ptr_t * vTimeCries, * vTimeFanins;
    Nwk_Obj_t * pNode, * pFanin, * pFanin2;
    Aig_Obj_t * pAnd;
209
    If_LibLut_t * pTempLib = pNtk->pLutLib;
210
    Tim_Man_t * pTempTim = NULL; 
Alan Mishchenko committed
211 212 213 214 215
    float tDelta, tArrival;
    int i, k, k2, Counter, CounterRes, nTimeCris;
    unsigned * puTCEdges;
    // perform delay trace
    if ( !fUseLutLib )
216
    {
Alan Mishchenko committed
217
        pNtk->pLutLib = NULL;
218 219 220 221 222 223
        if ( pNtk->pManTime )
        {
            pTempTim = pNtk->pManTime;
            pNtk->pManTime = Tim_ManDup( pTempTim, 1 );
        }
    }
Alan Mishchenko committed
224 225 226 227 228 229 230 231 232 233 234
    tArrival = Nwk_ManDelayTraceLut( pNtk );
    tDelta = fUseLutLib ? tArrival*Percentage/100.0 : 1.0;
    if ( fVerbose )
    {
        printf( "Max delay = %.2f. Delta = %.2f. ", tArrival, tDelta );
        printf( "Using %s model. ", fUseLutLib? "LUT library" : "unit-delay" );
        if ( fUseLutLib )
            printf( "Percentage = %d. ", Percentage );
        printf( "\n" );
    }
    // mark the timing critical nodes and edges
Alan Mishchenko committed
235
    puTCEdges = ABC_ALLOC( unsigned, Nwk_ManObjNumMax(pNtk) );
Alan Mishchenko committed
236
    memset( puTCEdges, 0, sizeof(unsigned) * Nwk_ManObjNumMax(pNtk) );
Alan Mishchenko committed
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
    Nwk_ManForEachNode( pNtk, pNode, i )
    {
        if ( Nwk_ObjSlack(pNode) >= tDelta )
            continue;
        puTCEdges[pNode->Id] = Nwk_ManDelayTraceTCEdges( pNtk, pNode, tDelta, fUseLutLib );
    }
    if ( fVerbose )
    {
        Counter = CounterRes = 0;
        Nwk_ManForEachNode( pNtk, pNode, i )
        {
            Nwk_ObjForEachFanin( pNode, pFanin, k )
                if ( !Nwk_ObjIsCi(pFanin) && Nwk_ObjSlack(pFanin) < tDelta )
                    Counter++;
            CounterRes += Aig_WordCountOnes( puTCEdges[pNode->Id] );
        }
        printf( "Edges: Total = %7d. 0-slack = %7d. Critical = %7d. Ratio = %4.2f\n", 
            Nwk_ManGetTotalFanins(pNtk), Counter, CounterRes, Counter? 1.0*CounterRes/Counter : 0.0 );
    }
    // start the resulting network
    pAig = Nwk_ManStrash( pNtk );
Alan Mishchenko committed
258
    pAig->pEquivs = ABC_ALLOC( Aig_Obj_t *, 3 * Aig_ManObjNumMax(pAig) );
Alan Mishchenko committed
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
    memset( pAig->pEquivs, 0, sizeof(Aig_Obj_t *) * 3 * Aig_ManObjNumMax(pAig) );

    // collect nodes to be used for resynthesis
    Counter = CounterRes = 0;
    vTimeCries = Vec_PtrAlloc( 16 );
    vTimeFanins = Vec_PtrAlloc( 16 );
    Nwk_ManForEachNode( pNtk, pNode, i )
    {
        if ( Nwk_ObjSlack(pNode) >= tDelta )
            continue;
        // count the number of non-PI timing-critical nodes
        nTimeCris = 0;
        Nwk_ObjForEachFanin( pNode, pFanin, k )
            if ( !Nwk_ObjIsCi(pFanin) && (puTCEdges[pNode->Id] & (1<<k)) )
                nTimeCris++;
        if ( !fVeryVerbose && nTimeCris == 0 )
            continue;
        Counter++;
        // count the total number of timing critical second-generation nodes
        Vec_PtrClear( vTimeCries );
        if ( nTimeCris )
        {
            Nwk_ObjForEachFanin( pNode, pFanin, k )
                if ( !Nwk_ObjIsCi(pFanin) && (puTCEdges[pNode->Id] & (1<<k)) )
                    Nwk_ObjForEachFanin( pFanin, pFanin2, k2 )
                        if ( puTCEdges[pFanin->Id] & (1<<k2) )
                            Vec_PtrPushUnique( vTimeCries, pFanin2 );
        }
//        if ( !fVeryVerbose && (Vec_PtrSize(vTimeCries) == 0 || Vec_PtrSize(vTimeCries) > Degree) )
        if ( (Vec_PtrSize(vTimeCries) == 0 || Vec_PtrSize(vTimeCries) > Degree) )
            continue;
        CounterRes++;
        // collect second generation nodes
        Vec_PtrClear( vTimeFanins );
        Nwk_ObjForEachFanin( pNode, pFanin, k )
        {
            if ( Nwk_ObjIsCi(pFanin) )
                Vec_PtrPushUnique( vTimeFanins, pFanin );
            else
                Nwk_ObjForEachFanin( pFanin, pFanin2, k2 )
                    Vec_PtrPushUnique( vTimeFanins, pFanin2 );                    
        }
        // print the results
        if ( fVeryVerbose )
        {
        printf( "%5d Node %5d : %d %2d %2d  ", Counter, pNode->Id, 
            nTimeCris, Vec_PtrSize(vTimeCries), Vec_PtrSize(vTimeFanins) );
        Nwk_ObjForEachFanin( pNode, pFanin, k )
            printf( "%d(%.2f)%s ", pFanin->Id, Nwk_ObjSlack(pFanin), (puTCEdges[pNode->Id] & (1<<k))? "*":"" );
        printf( "\n" );
        }
        // add the node to choices
        if ( Vec_PtrSize(vTimeCries) == 0 || Vec_PtrSize(vTimeCries) > Degree )
            continue;
        // order the fanins in the increasing order of criticalily
        if ( Vec_PtrSize(vTimeCries) > 1 )
        {
316 317
            pFanin = (Nwk_Obj_t *)Vec_PtrEntry( vTimeCries, 0 );
            pFanin2 = (Nwk_Obj_t *)Vec_PtrEntry( vTimeCries, 1 );
Alan Mishchenko committed
318 319 320 321 322 323 324 325
            if ( Nwk_ObjSlack(pFanin) < Nwk_ObjSlack(pFanin2) )
            {
                Vec_PtrWriteEntry( vTimeCries, 0, pFanin2 );
                Vec_PtrWriteEntry( vTimeCries, 1, pFanin );
            }
        }
        if ( Vec_PtrSize(vTimeCries) > 2 )
        {
326 327
            pFanin = (Nwk_Obj_t *)Vec_PtrEntry( vTimeCries, 1 );
            pFanin2 = (Nwk_Obj_t *)Vec_PtrEntry( vTimeCries, 2 );
Alan Mishchenko committed
328 329 330 331 332
            if ( Nwk_ObjSlack(pFanin) < Nwk_ObjSlack(pFanin2) )
            {
                Vec_PtrWriteEntry( vTimeCries, 1, pFanin2 );
                Vec_PtrWriteEntry( vTimeCries, 2, pFanin );
            }
333 334
            pFanin = (Nwk_Obj_t *)Vec_PtrEntry( vTimeCries, 0 );
            pFanin2 = (Nwk_Obj_t *)Vec_PtrEntry( vTimeCries, 1 );
Alan Mishchenko committed
335 336 337 338 339 340 341 342 343 344 345
            if ( Nwk_ObjSlack(pFanin) < Nwk_ObjSlack(pFanin2) )
            {
                Vec_PtrWriteEntry( vTimeCries, 0, pFanin2 );
                Vec_PtrWriteEntry( vTimeCries, 1, pFanin );
            }
        }
        // add choice
        Aig_ManSpeedupNode( pNtk, pAig, pNode, vTimeFanins, vTimeCries );
    }
    Vec_PtrFree( vTimeCries );
    Vec_PtrFree( vTimeFanins );
Alan Mishchenko committed
346
    ABC_FREE( puTCEdges );
Alan Mishchenko committed
347 348 349 350 351 352
    if ( fVerbose )
        printf( "Nodes: Total = %7d. 0-slack = %7d. Workable = %7d. Ratio = %4.2f\n", 
        Nwk_ManNodeNum(pNtk), Counter, CounterRes, Counter? 1.0*CounterRes/Counter : 0.0 ); 

    // remove invalid choice nodes
    Aig_ManForEachNode( pAig, pAnd, i )
Alan Mishchenko committed
353
        if ( Aig_ObjEquiv(pAig, pAnd) )
Alan Mishchenko committed
354
        {
Alan Mishchenko committed
355
            if ( Aig_ObjRefs(Aig_ObjEquiv(pAig, pAnd)) > 0 )
Alan Mishchenko committed
356 357 358 359 360 361
                pAig->pEquivs[pAnd->Id] = NULL;
        }

    // put back the library
    if ( !fUseLutLib )
        pNtk->pLutLib = pTempLib;
362 363 364 365 366
    if ( pTempTim )
    {
        Tim_ManStop( pNtk->pManTime );
        pNtk->pManTime = pTempTim;
    }
Alan Mishchenko committed
367 368 369 370 371 372 373

    // reconstruct the network
    pAig = Aig_ManDupDfs( pTemp = pAig );
    Aig_ManStop( pTemp );
    // reset levels
    Aig_ManChoiceLevel( pAig );
    return pAig;
Alan Mishchenko committed
374
}
Alan Mishchenko committed
375 376 377 378 379 380

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


381 382
ABC_NAMESPACE_IMPL_END