giaRetime.c 10 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
/**CFile****************************************************************

  FileName    [giaRetime.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [Scalable AIG package.]

  Synopsis    [Performs most-forward retiming for AIG with flop classes.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

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

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

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

#include "gia.h"

23 24 25
ABC_NAMESPACE_IMPL_START


Alan Mishchenko committed
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
////////////////////////////////////////////////////////////////////////
///                        DECLARATIONS                              ///
////////////////////////////////////////////////////////////////////////

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

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

  Synopsis    [Marks objects reachables from Const0 and PIs/

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Gia_ManMarkAutonomous_rec( Gia_Man_t * p, Gia_Obj_t * pObj )
{
    if ( Gia_ObjIsTravIdCurrent(p, pObj) )
        return pObj->fMark0;
    Gia_ObjSetTravIdCurrent(p, pObj);
    assert( pObj->fMark0 == 0 );
    if ( Gia_ObjIsPi(p, pObj) || Gia_ObjIsConst0(pObj) )
        return pObj->fMark0 = 1;
    if ( Gia_ObjIsCo(pObj) )
        return pObj->fMark0 = Gia_ManMarkAutonomous_rec( p, Gia_ObjFanin0(pObj) );
    if ( Gia_ObjIsCi(pObj) )
        return pObj->fMark0 = Gia_ManMarkAutonomous_rec( p, Gia_ObjRoToRi(p, pObj) );
    assert( Gia_ObjIsAnd(pObj) );
    if ( Gia_ManMarkAutonomous_rec( p, Gia_ObjFanin0(pObj) ) )
        return pObj->fMark0 = 1;
    return pObj->fMark0 = Gia_ManMarkAutonomous_rec( p, Gia_ObjFanin1(pObj) );
}

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

  Synopsis    [Marks with current trav ROs reachable from Const0 and PIs.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Gia_ManMarkAutonomous( Gia_Man_t * p )
{
    Gia_Obj_t * pObj;
    int i;
    Gia_ManCleanMark0( p );
    Gia_ManIncrementTravId( p );
    Gia_ManForEachRo( p, pObj, i )
        Gia_ManMarkAutonomous_rec( p, pObj );
    Gia_ManIncrementTravId( p );
    Gia_ManForEachRo( p, pObj, i )
        if ( pObj->fMark0 )
            Gia_ObjSetTravIdCurrent( p, pObj );
    Gia_ManCleanMark0( p );
}

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

  Synopsis    [Duplicates the AIG recursively.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Gia_ManRetimeDup_rec( Gia_Man_t * pNew, Gia_Obj_t * pObj )
{
    if ( ~pObj->Value )
        return;
    assert( Gia_ObjIsAnd(pObj) );
    Gia_ManRetimeDup_rec( pNew, Gia_ObjFanin0(pObj) );
    Gia_ManRetimeDup_rec( pNew, Gia_ObjFanin1(pObj) );
    pObj->Value = Gia_ManHashAnd( pNew, Gia_ObjFanin0Copy(pObj), Gia_ObjFanin1Copy(pObj) );
}

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

  Synopsis    [Duplicates the AIG while retiming the registers to the cut.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Gia_Man_t * Gia_ManRetimeDupForward( Gia_Man_t * p, Vec_Ptr_t * vCut )
{
    Gia_Man_t * pNew, * pTemp;
    Gia_Obj_t * pObj, * pObjRi, * pObjRo;
    int i;
    // create the new manager
    pNew = Gia_ManStart( Gia_ManObjNum(p) );
128
    pNew->pName = Abc_UtilStrsav( p->pName );
129
    pNew->pSpec = Abc_UtilStrsav( p->pSpec );
Alan Mishchenko committed
130 131 132 133 134 135 136 137
    Gia_ManHashAlloc( pNew );
    // create the true PIs
    Gia_ManFillValue( p );
    Gia_ManSetPhase( p );
    Gia_ManConst0(p)->Value = 0;
    Gia_ManForEachPi( p, pObj, i )
        pObj->Value = Gia_ManAppendCi( pNew );
    // create the registers
138
    Vec_PtrForEachEntry( Gia_Obj_t *, vCut, pObj, i )
139
        pObj->Value = Abc_LitNotCond( Gia_ManAppendCi(pNew), pObj->fPhase );
Alan Mishchenko committed
140 141 142 143 144 145 146 147 148 149 150 151 152
    // duplicate logic above the cut
    Gia_ManForEachCo( p, pObj, i )
        Gia_ManRetimeDup_rec( pNew, Gia_ObjFanin0(pObj) );
    // create the true POs
    Gia_ManForEachPo( p, pObj, i )
        Gia_ManAppendCo( pNew, Gia_ObjFanin0Copy(pObj) );
    // remember value in LI
    Gia_ManForEachRi( p, pObj, i )
        pObj->Value = Gia_ObjFanin0Copy(pObj);
    // transfer values from the LIs to the LOs
    Gia_ManForEachRiRo( p, pObjRi, pObjRo, i )
        pObjRo->Value = pObjRi->Value;
    // erase the data values on the internal nodes of the cut
153
    Vec_PtrForEachEntry( Gia_Obj_t *, vCut, pObj, i )
Alan Mishchenko committed
154 155 156
        if ( Gia_ObjIsAnd(pObj) )
            pObj->Value = ~0;
    // duplicate logic below the cut
157
    Vec_PtrForEachEntry( Gia_Obj_t *, vCut, pObj, i )
Alan Mishchenko committed
158 159
    {
        Gia_ManRetimeDup_rec( pNew, pObj );
160
        Gia_ManAppendCo( pNew, Abc_LitNotCond( pObj->Value, pObj->fPhase ) );
Alan Mishchenko committed
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
    }
    Gia_ManHashStop( pNew );
    Gia_ManSetRegNum( pNew, Vec_PtrSize(vCut) );
    pNew = Gia_ManCleanup( pTemp = pNew );
    Gia_ManStop( pTemp );
    return pNew;
}

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

  Synopsis    [Derives the cut for forward retiming.]

  Description [Assumes topological ordering of the nodes.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Gia_Man_t * Gia_ManRetimeForwardOne( Gia_Man_t * p, int * pnRegFixed, int * pnRegMoves )
{
    Vec_Int_t * vFlopClasses = NULL;
    Vec_Int_t * vObjClasses = NULL;
    Gia_Man_t * pNew;
    Vec_Ptr_t * vCut;
    Gia_Obj_t * pObj;
    int i;
    if ( p->vFlopClasses )
    {
Alan Mishchenko committed
190
//        printf( "Performing retiming with register classes.\n" );
Alan Mishchenko committed
191 192 193 194 195 196 197 198
        vObjClasses = Vec_IntAlloc( Gia_ManObjNum(p) );
        for ( i = 0; i < Gia_ManObjNum(p); i++ )
            Vec_IntPush( vObjClasses, -1 );
        Gia_ManForEachRo( p, pObj, i )
            Vec_IntWriteEntry( vObjClasses, Gia_ObjId(p, pObj), Vec_IntEntry(p->vFlopClasses, i) );
        vFlopClasses = Vec_IntAlloc( Gia_ManRegNum(p) );
    }
    // mark the retimable nodes
199
    Gia_ManIncrementTravId( p );
Alan Mishchenko committed
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
    Gia_ManMarkAutonomous( p );
    // mark the retimable registers with the fresh trav ID
    Gia_ManIncrementTravId( p );
    *pnRegFixed = 0;
    Gia_ManForEachRo( p, pObj, i )
        if ( Gia_ObjIsTravIdPrevious(p, pObj) )
            Gia_ObjSetTravIdCurrent(p, pObj);
        else
            (*pnRegFixed)++;
    // mark all the nodes that can be retimed forward
    *pnRegMoves = 0;
    Gia_ManForEachAnd( p, pObj, i )
        if ( Gia_ObjIsTravIdCurrent(p, Gia_ObjFanin0(pObj)) && Gia_ObjIsTravIdCurrent(p, Gia_ObjFanin1(pObj)) )
        {
            if ( vObjClasses && Vec_IntEntry(vObjClasses, Gia_ObjFaninId0(pObj, i)) != Vec_IntEntry(vObjClasses, Gia_ObjFaninId1(pObj, i)) )
                continue;
            if ( vObjClasses )
                Vec_IntWriteEntry( vObjClasses, Gia_ObjId(p, pObj), Vec_IntEntry(vObjClasses, Gia_ObjFaninId0(pObj, i)) );
            Gia_ObjSetTravIdCurrent(p, pObj);
            (*pnRegMoves)++;
        }
    // mark the remaining registers
    Gia_ManForEachRo( p, pObj, i )
        Gia_ObjSetTravIdCurrent(p, pObj);
    // find the cut (all such marked objects that fanout into unmarked nodes)
    vCut = Vec_PtrAlloc( 1000 );
    Gia_ManIncrementTravId( p );
    Gia_ManForEachObj( p, pObj, i )
    {
        if ( Gia_ObjIsTravIdPrevious(p, pObj) )
            continue;
        if ( (Gia_ObjIsCo(pObj) || Gia_ObjIsAnd(pObj)) && Gia_ObjIsTravIdPrevious(p, Gia_ObjFanin0(pObj)) )
        {
            if ( vFlopClasses )
                Vec_IntPush( vFlopClasses, Vec_IntEntry(vObjClasses, Gia_ObjFaninId0(pObj, i)) );
            Vec_PtrPush( vCut, Gia_ObjFanin0(pObj) );
            Gia_ObjSetTravIdCurrent( p, Gia_ObjFanin0(pObj) );
        }
        if ( Gia_ObjIsAnd(pObj) && Gia_ObjIsTravIdPrevious(p, Gia_ObjFanin1(pObj)) )
        {
            if ( vFlopClasses )
                Vec_IntPush( vFlopClasses, Vec_IntEntry(vObjClasses, Gia_ObjFaninId1(pObj, i)) );
            Vec_PtrPush( vCut, Gia_ObjFanin1(pObj) );
            Gia_ObjSetTravIdCurrent( p, Gia_ObjFanin1(pObj) );
        }
    }
    assert( vFlopClasses == NULL || Vec_IntSize(vFlopClasses) == Vec_PtrSize(vCut) );
    // finally derive the new manager
    pNew = Gia_ManRetimeDupForward( p, vCut );
    Vec_PtrFree( vCut );
Alan Mishchenko committed
250
    if ( vObjClasses )
Alan Mishchenko committed
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
    Vec_IntFree( vObjClasses );
    pNew->vFlopClasses = vFlopClasses;
    return pNew;
}

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

  Synopsis    [Derives the cut for forward retiming.]

  Description [Assumes topological ordering of the nodes.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Gia_Man_t * Gia_ManRetimeForward( Gia_Man_t * p, int nMaxIters, int fVerbose )
{
    Gia_Man_t * pNew, * pTemp;
270
    int i, nRegFixed, nRegMoves = 1;
271
    abctime clk;
Alan Mishchenko committed
272 273 274
    pNew = p;
    for ( i = 0; i < nMaxIters && nRegMoves > 0; i++ )
    {
275
        clk = Abc_Clock();
Alan Mishchenko committed
276 277 278 279 280
        pNew = Gia_ManRetimeForwardOne( pTemp = pNew, &nRegFixed, &nRegMoves );
        if ( fVerbose )
        {
            printf( "%2d : And = %6d. Reg = %5d. Unret = %5d. Move = %6d. ", 
                i + 1, Gia_ManAndNum(pTemp), Gia_ManRegNum(pTemp), nRegFixed, nRegMoves );
281
            ABC_PRT( "Time", Abc_Clock() - clk );
Alan Mishchenko committed
282 283 284 285 286
        }
        if ( pTemp != p )
            Gia_ManStop( pTemp );
    }
/*
287
    clk = Abc_Clock();
Alan Mishchenko committed
288 289 290
    pNew = Gia_ManReduceLaches( pNew, fVerbose );
    if ( fVerbose )
    {
291
        ABC_PRT( "Register sharing time", Abc_Clock() - clk );
Alan Mishchenko committed
292 293 294 295 296 297 298 299 300 301 302
    }
*/
    return pNew;
}


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


303 304
ABC_NAMESPACE_IMPL_END