summaryrefslogtreecommitdiffstats
path: root/src/base/abcs/abcRetime.c
blob: 13b8a926c57393bb7077e1c2052ff8dbc691621b (plain)
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
45
46
47
48
49
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
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
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
/**CFile****************************************************************

  FileName    [abcSeqRetime.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [Network and node package.]

  Synopsis    [Peforms retiming for optimal clock cycle.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

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

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

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

#include "abc.h"

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

// storing arrival times in the nodes
static inline int  Abc_NodeReadLValue( Abc_Obj_t * pNode )            { return Vec_IntEntry( (pNode)->pNtk->pData, (pNode)->Id );        }
static inline void Abc_NodeSetLValue( Abc_Obj_t * pNode, int Value )  { Vec_IntWriteEntry( (pNode)->pNtk->pData, (pNode)->Id, (Value) ); }

// the internal procedures
static int Abc_NtkRetimeSearch_rec( Abc_Ntk_t * pNtk, int FiMin, int FiMax );
static int Abc_NtkRetimeForPeriod( Abc_Ntk_t * pNtk, int Fi );
static int Abc_NodeUpdateLValue( Abc_Obj_t * pObj, int Fi );

// node status after updating its arrival time
enum { ABC_UPDATE_FAIL, ABC_UPDATE_NO, ABC_UPDATE_YES };

////////////////////////////////////////////////////////////////////////
///                     FUNCTION DEFITIONS                           ///
////////////////////////////////////////////////////////////////////////

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

  Synopsis    [Retimes AIG for optimal delay.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Abc_NtkSeqRetimeDelay( Abc_Ntk_t * pNtk )
{
    int FiMax, FiBest;
    assert( Abc_NtkIsSeq( pNtk ) );

    // start storage for sequential arrival times
    assert( pNtk->pData == NULL );
    pNtk->pData = Vec_IntAlloc( 0 );

    // get the maximum possible clock
    FiMax = Abc_NtkNodeNum(pNtk);

    // make sure this clock period is feasible
    assert( Abc_NtkRetimeForPeriod( pNtk, FiMax ) );

    // search for the optimal clock period between 0 and nLevelMax
    FiBest = Abc_NtkRetimeSearch_rec( pNtk, 0, FiMax );
    // print the result
    printf( "The best clock period is %3d.\n", FiBest );

    // free storage
    Vec_IntFree( pNtk->pData );
    pNtk->pData = NULL;
}

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

  Synopsis    [Performs binary search for the optimal clock period.]

  Description [Assumes that FiMin is infeasible while FiMax is feasible.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Abc_NtkRetimeSearch_rec( Abc_Ntk_t * pNtk, int FiMin, int FiMax )
{
    int Median;

    assert( FiMin < FiMax );
    if ( FiMin + 1 == FiMax )
        return FiMax;

    Median = FiMin + (FiMax - FiMin)/2;

    if ( Abc_NtkRetimeForPeriod( pNtk, Median ) )
        return Abc_NtkRetimeSearch_rec( pNtk, FiMin, Median ); // Median is feasible
    else 
        return Abc_NtkRetimeSearch_rec( pNtk, Median, FiMax ); // Median is infeasible
}

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

  Synopsis    [Returns 1 if retiming with this clock period is feasible.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Abc_NtkRetimeForPeriod( Abc_Ntk_t * pNtk, int Fi )
{
    Vec_Ptr_t * vFrontier;
    Abc_Obj_t * pObj, * pFanout;
    int RetValue, i, k;

    // set l-values of all nodes to be minus infinity
    Vec_IntFill( pNtk->pData, Abc_NtkObjNumMax(pNtk), -ABC_INFINITY );

    // start the frontier by including PI fanouts
    vFrontier = Vec_PtrAlloc( 100 );
    Abc_NtkForEachPi( pNtk, pObj, i )
    {
        Abc_NodeSetLValue( pObj, 0 );
        Abc_ObjForEachFanout( pObj, pFanout, k )
            if ( pFanout->fMarkA == 0 )
            {   
                Vec_PtrPush( vFrontier, pFanout );
                pFanout->fMarkA = 1;
            }
    }

    // iterate until convergence
    Vec_PtrForEachEntry( vFrontier, pObj, i )
    {
        RetValue = Abc_NodeUpdateLValue( pObj, Fi );
        if ( RetValue == ABC_UPDATE_FAIL )
            break;
        // unmark the node as processed
        pObj->fMarkA = 0;
        if ( RetValue == ABC_UPDATE_NO ) 
            continue;
        assert( RetValue == ABC_UPDATE_YES );
        // arrival times have changed - add fanouts to the frontier
        Abc_ObjForEachFanout( pObj, pFanout, k )
            if ( pFanout->fMarkA == 0 )
            {   
                Vec_PtrPush( vFrontier, pFanout );
                pFanout->fMarkA = 1;
            }
    }
    // clean the nodes
    Vec_PtrForEachEntryStart( vFrontier, pObj, k, i )
        pObj->fMarkA = 0;

    // report the results
    if ( RetValue == ABC_UPDATE_FAIL )
        printf( "Period = %3d.  Updated nodes = %6d.    Infeasible\n", Fi, vFrontier->nSize );
    else
        printf( "Period = %3d.  Updated nodes = %6d.    Feasible\n",   Fi, vFrontier->nSize );
    Vec_PtrFree( vFrontier );
    return RetValue != ABC_UPDATE_FAIL;
}

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

  Synopsis    [Computes the l-value of the node.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Abc_NodeUpdateLValue( Abc_Obj_t * pObj, int Fi )
{
    int lValueNew, lValue0, lValue1;
    assert( !Abc_ObjIsPi(pObj) );
    lValue0 = Abc_NodeReadLValue(Abc_ObjFanin0(pObj)) - Fi * Abc_ObjFaninL0(pObj);
    if ( Abc_ObjFaninNum(pObj) == 2 )
        lValue1 = Abc_NodeReadLValue(Abc_ObjFanin1(pObj)) - Fi * Abc_ObjFaninL1(pObj);
    else
        lValue1 = -ABC_INFINITY;
    lValueNew = 1 + ABC_MAX( lValue0, lValue1 );
    if ( Abc_ObjIsPo(pObj) && lValueNew > Fi )
        return ABC_UPDATE_FAIL;
    if ( lValueNew == Abc_NodeReadLValue(pObj) )
        return ABC_UPDATE_NO;
    Abc_NodeSetLValue( pObj, lValueNew );
    return ABC_UPDATE_YES;
}

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