sfmTim.c 15.3 KB
Newer Older
1 2
/**CFile****************************************************************

3
  FileName    [sfmTim.c]
4 5 6 7 8 9 10 11 12 13 14 15 16

  SystemName  [ABC: Logic synthesis and verification system.]

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

  Synopsis    [Timing manager.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

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

17
  Revision    [$Id: sfmTim.c,v 1.00 2005/06/20 00:00:00 alanmi Exp $]
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36

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

#include "sfmInt.h"

ABC_NAMESPACE_IMPL_START


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

struct Sfm_Tim_t_
{
    // external
    Mio_Library_t *   pLib;        // library
    Scl_Con_t *       pExt;        // external timing
    Abc_Ntk_t *       pNtk;        // mapped network
    int               Delay;       // the largest delay
37
    int               DeltaCrit;   // critical delay delta
38 39 40
    // timing info
    Vec_Int_t         vTimArrs;    // arrivals (rise/fall)
    Vec_Int_t         vTimReqs;    // required (rise/fall)
41 42
    // incremental timing
    Vec_Wec_t         vLevels;     // levels
43 44
    // critical path
    Vec_Int_t         vPath;       // critical path
45
    Vec_Wrd_t         vSortData;   // node priority order
46 47
};

48 49 50
static inline int * Sfm_TimArrId( Sfm_Tim_t * p, int Id )                    { return Vec_IntEntryP( &p->vTimArrs,  Abc_Var2Lit(Id, 0) );               }
static inline int * Sfm_TimReqId( Sfm_Tim_t * p, int Id )                    { return Vec_IntEntryP( &p->vTimReqs,  Abc_Var2Lit(Id, 0) );               }

51 52 53
static inline int * Sfm_TimArr( Sfm_Tim_t * p, Abc_Obj_t * pNode )           { return Vec_IntEntryP( &p->vTimArrs,  Abc_Var2Lit(Abc_ObjId(pNode), 0) ); }
static inline int * Sfm_TimReq( Sfm_Tim_t * p, Abc_Obj_t * pNode )           { return Vec_IntEntryP( &p->vTimReqs,  Abc_Var2Lit(Abc_ObjId(pNode), 0) ); }

54 55
static inline int   Sfm_TimArrMaxId( Sfm_Tim_t * p, int Id )                 { int * a = Sfm_TimArrId(p, Id); return Abc_MaxInt(a[0], a[1]);            }

56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
static inline int   Sfm_TimArrMax( Sfm_Tim_t * p, Abc_Obj_t * pNode )        { int * a = Sfm_TimArr(p, pNode); return Abc_MaxInt(a[0], a[1]);           }
static inline void  Sfm_TimSetReq( Sfm_Tim_t * p, Abc_Obj_t * pNode, int t ) { int * r = Sfm_TimReq(p, pNode); r[0] = r[1] = t;                         }
static inline int   Sfm_TimSlack( Sfm_Tim_t * p, Abc_Obj_t * pNode )         { int * r = Sfm_TimReq(p, pNode), * a = Sfm_TimArr(p, pNode); return Abc_MinInt(r[0]-a[0], r[1]-a[1]); }

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

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
75
static inline void Sfm_TimEdgeArrival( Sfm_Tim_t * p, Mio_Pin_t * pPin, int * pTimeIn, int * pTimeOut )
76 77
{
    Mio_PinPhase_t PinPhase = Mio_PinReadPhase(pPin);
78 79
    int tDelayBlockRise = Scl_Flt2Int(Mio_PinReadDelayBlockRise(pPin));  
    int tDelayBlockFall = Scl_Flt2Int(Mio_PinReadDelayBlockFall(pPin));  
80 81 82 83 84 85 86 87 88 89 90
    if ( PinPhase != MIO_PHASE_INV )  // NONINV phase is present
    {
        pTimeOut[0] = Abc_MaxInt( pTimeOut[0], pTimeIn[0] + tDelayBlockRise );
        pTimeOut[1] = Abc_MaxInt( pTimeOut[1], pTimeIn[1] + tDelayBlockFall );
    }
    if ( PinPhase != MIO_PHASE_NONINV )  // INV phase is present
    {
        pTimeOut[0] = Abc_MaxInt( pTimeOut[0], pTimeIn[1] + tDelayBlockRise );
        pTimeOut[1] = Abc_MaxInt( pTimeOut[1], pTimeIn[0] + tDelayBlockFall );
    }
}
91
static inline void Sfm_TimGateArrival( Sfm_Tim_t * p, Mio_Gate_t * pGate, int ** pTimesIn, int * pTimeOut )
92 93
{
    Mio_Pin_t * pPin;  int i = 0;
94
    pTimeOut[0] = pTimeOut[1] = 0;
95
    Mio_GateForEachPin( pGate, pPin )
96
        Sfm_TimEdgeArrival( p, pPin, pTimesIn[i++], pTimeOut );
97 98
    assert( i == Mio_GateReadPinNum(pGate) );
}
99 100 101 102 103 104 105 106 107
static inline void Sfm_TimNodeArrival( Sfm_Tim_t * p, Abc_Obj_t * pNode )
{
    int i, iFanin, * pTimesIn[6];
    int * pTimeOut = Sfm_TimArr(p, pNode);
    assert( Abc_ObjFaninNum(pNode) <= 6 );
    Abc_ObjForEachFaninId( pNode, iFanin, i )
        pTimesIn[i] = Sfm_TimArrId( p, iFanin );
    Sfm_TimGateArrival( p, (Mio_Gate_t *)pNode->pData, pTimesIn, pTimeOut );
}
108

109
static inline void Sfm_TimEdgeRequired( Sfm_Tim_t * p, Mio_Pin_t * pPin, int * pTimeIn, int * pTimeOut )
110 111
{
    Mio_PinPhase_t PinPhase = Mio_PinReadPhase(pPin);
112 113
    int tDelayBlockRise = Scl_Flt2Int(Mio_PinReadDelayBlockRise(pPin));  
    int tDelayBlockFall = Scl_Flt2Int(Mio_PinReadDelayBlockFall(pPin));  
114 115 116 117 118 119 120 121 122 123 124
    if ( PinPhase != MIO_PHASE_INV )  // NONINV phase is present
    {
        pTimeIn[0] = Abc_MinInt( pTimeIn[0], pTimeOut[0] - tDelayBlockRise );
        pTimeIn[1] = Abc_MinInt( pTimeIn[1], pTimeOut[1] - tDelayBlockFall );
    }
    if ( PinPhase != MIO_PHASE_NONINV )  // INV phase is present
    {
        pTimeIn[0] = Abc_MinInt( pTimeIn[0], pTimeOut[1] - tDelayBlockRise );
        pTimeIn[1] = Abc_MinInt( pTimeIn[1], pTimeOut[0] - tDelayBlockFall );
    }
}
125
static inline void Sfm_TimGateRequired( Sfm_Tim_t * p, Mio_Gate_t * pGate, int ** pTimesIn, int * pTimeOut )
126 127 128
{
    Mio_Pin_t * pPin;  int i = 0;
    Mio_GateForEachPin( pGate, pPin )
129
        Sfm_TimEdgeRequired( p, pPin, pTimesIn[i++], pTimeOut );
130 131
    assert( i == Mio_GateReadPinNum(pGate) );
}
132 133 134 135 136 137 138 139 140
void Sfm_TimNodeRequired( Sfm_Tim_t * p, Abc_Obj_t * pNode )
{
    int i, iFanin, * pTimesIn[6];
    int * pTimeOut = Sfm_TimReq(p, pNode);
    assert( Abc_ObjFaninNum(pNode) <= 6 );
    Abc_ObjForEachFaninId( pNode, iFanin, i )
        pTimesIn[i] = Sfm_TimReqId( p, iFanin );
    Sfm_TimGateRequired( p, (Mio_Gate_t *)pNode->pData, pTimesIn, pTimeOut );
}
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 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


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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Sfm_TimCriticalPath_int( Sfm_Tim_t * p, Abc_Obj_t * pObj, Vec_Int_t * vPath, int SlackMax )
{
    Abc_Obj_t * pNext; int i;
    if ( Abc_NodeIsTravIdCurrent( pObj ) )
        return;
    Abc_NodeSetTravIdCurrent( pObj );
    assert( Abc_ObjIsNode(pObj) );
    Abc_ObjForEachFanin( pObj, pNext, i )
    {
        if ( Abc_ObjIsCi(pNext) || Abc_ObjFaninNum(pNext) == 0 )
            continue;
        assert( Abc_ObjIsNode(pNext) );
        if ( Sfm_TimSlack(p, pNext) <= SlackMax )
            Sfm_TimCriticalPath_int( p, pNext, vPath, SlackMax );
    }
    if ( Abc_ObjFaninNum(pObj) > 0 )
        Vec_IntPush( vPath, Abc_ObjId(pObj) );
}
int Sfm_TimCriticalPath( Sfm_Tim_t * p, int Window )
{
    int i, SlackMax = p->Delay * Window / 100;
    Abc_Obj_t * pObj, * pNext; 
    Vec_IntClear( &p->vPath );
    Abc_NtkIncrementTravId( p->pNtk ); 
    Abc_NtkForEachCo( p->pNtk, pObj, i )
    {
        pNext = Abc_ObjFanin0(pObj);
        if ( Abc_ObjIsCi(pNext) || Abc_ObjFaninNum(pNext) == 0 )
            continue;
        assert( Abc_ObjIsNode(pNext) );
        if ( Sfm_TimSlack(p, pNext) <= SlackMax )
            Sfm_TimCriticalPath_int( p, pNext, &p->vPath, SlackMax );
    }
    return Vec_IntSize(&p->vPath);
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Sfm_TimTrace( Sfm_Tim_t * p )
{
    Abc_Obj_t * pObj; int i, Delay = 0;
    Vec_Ptr_t * vNodes = Abc_NtkDfs( p->pNtk, 1 );
    Vec_PtrForEachEntry( Abc_Obj_t *, vNodes, pObj, i )
206
        Sfm_TimNodeArrival( p, pObj );
207 208
    Abc_NtkForEachCo( p->pNtk, pObj, i )
        Delay = Abc_MaxInt( Delay, Sfm_TimArrMax(p, Abc_ObjFanin0(pObj)) );
209
    Vec_IntFill( &p->vTimReqs, 2*Abc_NtkObjNumMax(p->pNtk), ABC_INFINITY );
210 211 212
    Abc_NtkForEachCo( p->pNtk, pObj, i )
        Sfm_TimSetReq( p, Abc_ObjFanin0(pObj), Delay );
    Vec_PtrForEachEntryReverse( Abc_Obj_t *, vNodes, pObj, i )
213
        Sfm_TimNodeRequired( p, pObj );
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
    Vec_PtrFree( vNodes );
    return Delay;
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
229
Sfm_Tim_t * Sfm_TimStart( Mio_Library_t * pLib, Scl_Con_t * pExt, Abc_Ntk_t * pNtk, int DeltaCrit )
230 231 232 233 234
{
    Sfm_Tim_t * p = ABC_CALLOC( Sfm_Tim_t, 1 );
    p->pLib = pLib;
    p->pExt = pExt;
    p->pNtk = pNtk;
235 236
    Vec_IntFill( &p->vTimArrs,  3*Abc_NtkObjNumMax(pNtk), 0 );
    Vec_IntFill( &p->vTimReqs,  3*Abc_NtkObjNumMax(pNtk), 0 );
237
    p->Delay = Sfm_TimTrace( p );
238
    assert( DeltaCrit > 0 && DeltaCrit < Scl_Flt2Int(1000.0) );
239
    p->DeltaCrit = DeltaCrit;
240 241 242 243 244 245
    return p;
}
void Sfm_TimStop( Sfm_Tim_t * p )
{
    Vec_IntErase( &p->vTimArrs );
    Vec_IntErase( &p->vTimReqs );
246
    Vec_WecErase( &p->vLevels );
247
    Vec_IntErase( &p->vPath );
248
    Vec_WrdErase( &p->vSortData );
249 250
    ABC_FREE( p );
}
251 252 253 254 255 256 257 258
int Sfm_TimReadNtkDelay( Sfm_Tim_t * p )
{
    return p->Delay;
}
int Sfm_TimReadObjDelay( Sfm_Tim_t * p, int iObj )
{
    return Sfm_TimArrMaxId(p, iObj);
}
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Sfm_TimTest( Abc_Ntk_t * pNtk )
{
    Mio_Library_t * pLib = (Mio_Library_t *)pNtk->pManFunc;
274
    Sfm_Tim_t * p = Sfm_TimStart( pLib, NULL, pNtk, 100 );
275
    printf( "Max delay = %.2f.  Path = %d (%d).\n", Scl_Int2Flt(p->Delay), Sfm_TimCriticalPath(p, 1), Abc_NtkNodeNum(p->pNtk) );
276 277 278
    Sfm_TimStop( p );
}

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 316 317 318 319
/**Function*************************************************************

  Synopsis    [Levelized structure.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline void Sfm_TimUpdateClean( Sfm_Tim_t * p )
{
    Vec_Int_t * vLevel;
    Abc_Obj_t * pObj;
    int i, k;
    Vec_WecForEachLevel( &p->vLevels, vLevel, i )
    {
        Abc_NtkForEachObjVec( vLevel, p->pNtk, pObj, k )
        {
            assert( pObj->fMarkC == 1 );
            pObj->fMarkC = 0;
        }
        Vec_IntClear( vLevel );
    }
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Sfm_TimUpdateTiming( Sfm_Tim_t * p, Vec_Int_t * vTimeNodes )
{
    assert( Vec_IntSize(vTimeNodes) > 0 && Vec_IntSize(vTimeNodes) <= 2 );
320 321
    Vec_IntFillExtra( &p->vTimArrs, 2*Abc_NtkObjNumMax(p->pNtk), 0 );
    Vec_IntFillExtra( &p->vTimReqs, 2*Abc_NtkObjNumMax(p->pNtk), 0 );
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
    p->Delay = Sfm_TimTrace( p );
}

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

  Synopsis    [Sort an array of nodes using their max arrival times.]

  Description [Returns the number of new divisor nodes.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Sfm_TimSortArrayByArrival( Sfm_Tim_t * p, Vec_Int_t * vNodes, int iPivot )
{
    word Entry; 
339 340
    int i, Id, Time, nDivNew = -1; 
    int MaxDelay = ABC_INFINITY/2+Sfm_TimArrMaxId(p, iPivot);
341
    assert( p->DeltaCrit > 0 );
342 343 344
    // collect nodes
    Vec_WrdClear( &p->vSortData );
    Vec_IntForEachEntry( vNodes, Id, i )
345 346 347 348 349
    {
        Time = Sfm_TimArrMaxId( p, Id );
        assert( -ABC_INFINITY/2 < Time && Time < ABC_INFINITY/2 );
        Vec_WrdPush( &p->vSortData, ((word)Id << 32) | (ABC_INFINITY/2+Time) );
    }
350 351 352 353 354 355 356
    // sort nodes by delay
    Abc_QuickSort3( Vec_WrdArray(&p->vSortData), Vec_WrdSize(&p->vSortData), 0 );
    // collect sorted nodes and find place where divisors end
    Vec_IntClear( vNodes );
    Vec_WrdForEachEntry( &p->vSortData, Entry, i )
    {
        Vec_IntPush( vNodes, (int)(Entry >> 32) );
357
        if ( nDivNew == -1 && ((int)Entry) + p->DeltaCrit > MaxDelay )
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
            nDivNew = i;
    }
    return nDivNew;
}

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

  Synopsis    [Priority of nodes to try remapping for delay.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
374
int Sfm_TimPriorityNodes( Sfm_Tim_t * p, Vec_Int_t * vCands, int Window )
375 376 377
{
    Vec_Int_t * vLevel;
    Abc_Obj_t * pObj;
378 379
    int i, k;
    assert( Window >= 0 && Window <= 100 );
380
    // collect critical path
381
    Sfm_TimCriticalPath( p, Window );
382 383 384 385 386 387 388 389 390 391 392 393
    // add nodes to the levelized structure
    Sfm_TimUpdateClean( p );
    Abc_NtkForEachObjVec( &p->vPath, p->pNtk, pObj, i )
    {
        assert( pObj->fMarkC == 0 );
        pObj->fMarkC = 1;
        Vec_WecPush( &p->vLevels, Abc_ObjLevel(pObj), Abc_ObjId(pObj) );
    }
    // prioritize nodes by expected gain
    Vec_WecSort( &p->vLevels, 0 );
    Vec_IntClear( vCands );
    Vec_WecForEachLevel( &p->vLevels, vLevel, i )
394
        Abc_NtkForEachObjVec( vLevel, p->pNtk, pObj, k )
395 396
            if ( !pObj->fMarkA )
                Vec_IntPush( vCands, Abc_ObjId(pObj) );
397
//    printf( "Path = %5d   Cand = %5d\n", Vec_IntSize(&p->vPath) );
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
    return Vec_IntSize(vCands) > 0;
}

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

  Synopsis    [Returns 1 if node is relatively non-critical compared to the pivot.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Sfm_TimNodeIsNonCritical( Sfm_Tim_t * p, Abc_Obj_t * pPivot, Abc_Obj_t * pNode )
{
414
    return Sfm_TimArrMax(p, pNode) + p->DeltaCrit <= Sfm_TimArrMax(p, pPivot);
415 416 417 418 419 420 421 422 423 424 425 426 427
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
428
int Sfm_TimEvalRemapping( Sfm_Tim_t * p, Vec_Int_t * vFanins, Vec_Int_t * vMap, Mio_Gate_t * pGate1, char * pFans1, Mio_Gate_t * pGate2, char * pFans2 )
429 430 431 432 433 434 435
{
    int TimeOut[2][2];
    int * pTimesIn1[6], * pTimesIn2[6];
    int i, nFanins1, nFanins2;
    // process the first gate
    nFanins1 = Mio_GateReadPinNum( pGate1 );
    for ( i = 0; i < nFanins1; i++ )
436
        pTimesIn1[i] = Sfm_TimArrId( p, Vec_IntEntry(vMap, Vec_IntEntry(vFanins, (int)pFans1[i])) );
437 438 439 440 441 442 443 444 445
    Sfm_TimGateArrival( p, pGate1, pTimesIn1, TimeOut[0] );
    if ( pGate2 == NULL )
        return Abc_MaxInt(TimeOut[0][0], TimeOut[0][1]);
    // process the second gate
    nFanins2 = Mio_GateReadPinNum( pGate2 );
    for ( i = 0; i < nFanins2; i++ )
        if ( (int)pFans2[i] == 16 )
            pTimesIn2[i] = TimeOut[0];
        else
446
            pTimesIn2[i] = Sfm_TimArrId( p, Vec_IntEntry(vMap, Vec_IntEntry(vFanins, (int)pFans2[i])) );
447 448 449 450
    Sfm_TimGateArrival( p, pGate2, pTimesIn2, TimeOut[1] );
    return Abc_MaxInt(TimeOut[1][0], TimeOut[1][1]);
}

451 452 453 454 455 456 457 458

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


ABC_NAMESPACE_IMPL_END