nwkFanio.c 9.56 KB
Newer Older
Alan Mishchenko committed
1 2
/**CFile****************************************************************

Alan Mishchenko committed
3
  FileName    [nwkFanio.c]
Alan Mishchenko committed
4 5 6

  SystemName  [ABC: Logic synthesis and verification system.]

Alan Mishchenko committed
7
  PackageName [Logic network representation.]
Alan Mishchenko committed
8 9 10 11 12 13 14 15 16

  Synopsis    [Manipulation of fanins/fanouts.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

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

Alan Mishchenko committed
17
  Revision    [$Id: nwkFanio.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    [Collects fanins of the node.]
Alan Mishchenko committed
37 38 39 40 41 42 43 44

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
45
void Nwk_ObjCollectFanins( Nwk_Obj_t * pNode, Vec_Ptr_t * vNodes )
Alan Mishchenko committed
46
{
Alan Mishchenko committed
47
    Nwk_Obj_t * pFanin;
Alan Mishchenko committed
48 49
    int i;
    Vec_PtrClear(vNodes);
Alan Mishchenko committed
50
    Nwk_ObjForEachFanin( pNode, pFanin, i )
Alan Mishchenko committed
51 52 53 54 55
        Vec_PtrPush( vNodes, pFanin );
}

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

Alan Mishchenko committed
56
  Synopsis    [Collects fanouts of the node.]
Alan Mishchenko committed
57 58 59 60 61 62 63 64

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
65
void Nwk_ObjCollectFanouts( Nwk_Obj_t * pNode, Vec_Ptr_t * vNodes )
Alan Mishchenko committed
66
{
Alan Mishchenko committed
67
    Nwk_Obj_t * pFanout;
Alan Mishchenko committed
68 69
    int i;
    Vec_PtrClear(vNodes);
Alan Mishchenko committed
70
    Nwk_ObjForEachFanout( pNode, pFanout, i )
Alan Mishchenko committed
71 72 73 74 75 76 77 78 79 80 81 82 83 84
        Vec_PtrPush( vNodes, pFanout );
}

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

  Synopsis    [Returns the number of the fanin of the node.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
85
int Nwk_ObjFindFanin( Nwk_Obj_t * pObj, Nwk_Obj_t * pFanin )
Alan Mishchenko committed
86
{  
Alan Mishchenko committed
87
    Nwk_Obj_t * pTemp;
Alan Mishchenko committed
88
    int i;
Alan Mishchenko committed
89
    Nwk_ObjForEachFanin( pObj, pTemp, i )
Alan Mishchenko committed
90 91 92 93 94 95 96
        if ( pTemp == pFanin )
            return i;
    return -1;
}

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

Alan Mishchenko committed
97
  Synopsis    [Returns the number of the fanout of the node.]
Alan Mishchenko committed
98 99 100 101 102 103 104 105

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
106
int Nwk_ObjFindFanout( Nwk_Obj_t * pObj, Nwk_Obj_t * pFanout )
Alan Mishchenko committed
107
{  
Alan Mishchenko committed
108
    Nwk_Obj_t * pTemp;
Alan Mishchenko committed
109
    int i;
Alan Mishchenko committed
110
    Nwk_ObjForEachFanout( pObj, pTemp, i )
Alan Mishchenko committed
111 112 113 114 115 116 117
        if ( pTemp == pFanout )
            return i;
    return -1;
}

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

Alan Mishchenko committed
118
  Synopsis    [Returns 1 if the node has to be reallocated.]
Alan Mishchenko committed
119 120 121 122 123 124 125 126

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
127
static inline int Nwk_ObjReallocIsNeeded( Nwk_Obj_t * pObj )
Alan Mishchenko committed
128
{  
Alan Mishchenko committed
129
    return pObj->nFanins + pObj->nFanouts == pObj->nFanioAlloc;
Alan Mishchenko committed
130
}
Alan Mishchenko committed
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
 
/**Function*************************************************************

  Synopsis    [Reallocates the object.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static Nwk_Obj_t * Nwk_ManReallocNode( Nwk_Obj_t * pObj )
{  
    Nwk_Obj_t ** pFanioOld = pObj->pFanio;
    assert( Nwk_ObjReallocIsNeeded(pObj) );
    pObj->pFanio = (Nwk_Obj_t **)Aig_MmFlexEntryFetch( pObj->pMan->pMemObjs, 2 * pObj->nFanioAlloc * sizeof(Nwk_Obj_t *) );
    memmove( pObj->pFanio, pFanioOld, pObj->nFanioAlloc * sizeof(Nwk_Obj_t *) );
    pObj->nFanioAlloc *= 2;
    pObj->pMan->nRealloced++;
    return NULL;
}
Alan Mishchenko committed
153 154 155 156 157 158 159 160 161 162 163 164

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

  Synopsis    [Creates fanout/fanin relationship between the nodes.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
165
void Nwk_ObjAddFanin( Nwk_Obj_t * pObj, Nwk_Obj_t * pFanin )
Alan Mishchenko committed
166 167 168 169
{
    int i;
    assert( pObj->pMan == pFanin->pMan );
    assert( pObj->Id >= 0 && pFanin->Id >= 0 );
Alan Mishchenko committed
170 171 172 173
    if ( Nwk_ObjReallocIsNeeded(pObj) )
        Nwk_ManReallocNode( pObj );
    if ( Nwk_ObjReallocIsNeeded(pFanin) )
        Nwk_ManReallocNode( pFanin );
Alan Mishchenko committed
174
    for ( i = pObj->nFanins + pObj->nFanouts; i > pObj->nFanins; i-- )
Alan Mishchenko committed
175 176 177
        pObj->pFanio[i] = pObj->pFanio[i-1];
    pObj->pFanio[pObj->nFanins++] = pFanin;
    pFanin->pFanio[pFanin->nFanins + pFanin->nFanouts++] = pObj;
178
    pObj->Level = Abc_MaxInt( pObj->Level, pFanin->Level + Nwk_ObjIsNode(pObj) );
Alan Mishchenko committed
179 180 181 182 183 184 185 186 187 188 189 190 191
}

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

  Synopsis    [Removes fanout/fanin relationship between the nodes.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
192
void Nwk_ObjDeleteFanin( Nwk_Obj_t * pObj, Nwk_Obj_t * pFanin )
Alan Mishchenko committed
193
{
Alan Mishchenko committed
194
    int i, k, Limit, fFound;
Alan Mishchenko committed
195 196
    // remove pFanin from the fanin list of pObj
    Limit = pObj->nFanins + pObj->nFanouts;
Alan Mishchenko committed
197
    fFound = 0;
Alan Mishchenko committed
198
    for ( k = i = 0; i < Limit; i++ )
Alan Mishchenko committed
199
        if ( fFound || pObj->pFanio[i] != pFanin )
Alan Mishchenko committed
200
            pObj->pFanio[k++] = pObj->pFanio[i];
Alan Mishchenko committed
201 202
        else
            fFound = 1;
Alan Mishchenko committed
203
    assert( i == k + 1 ); // if it fails, likely because of duplicated fanin
Alan Mishchenko committed
204 205 206
    pObj->nFanins--;
    // remove pObj from the fanout list of pFanin
    Limit = pFanin->nFanins + pFanin->nFanouts;
Alan Mishchenko committed
207
    fFound = 0;
Alan Mishchenko committed
208
    for ( k = i = pFanin->nFanins; i < Limit; i++ )
Alan Mishchenko committed
209
        if ( fFound || pFanin->pFanio[i] != pObj )
Alan Mishchenko committed
210
            pFanin->pFanio[k++] = pFanin->pFanio[i];
Alan Mishchenko committed
211 212
        else
            fFound = 1;
Alan Mishchenko committed
213 214
    assert( i == k + 1 ); // if it fails, likely because of duplicated fanout
    pFanin->nFanouts--; 
Alan Mishchenko committed
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
}

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

  Synopsis    [Replaces a fanin of the node.]

  Description [The node is pObj. An old fanin of this node (pFaninOld) has to be
  replaced by a new fanin (pFaninNew). Assumes that the node and the old fanin 
  are not complemented. The new fanin can be complemented. In this case, the
  polarity of the new fanin will change, compared to the polarity of the old fanin.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
231
void Nwk_ObjPatchFanin( Nwk_Obj_t * pObj, Nwk_Obj_t * pFaninOld, Nwk_Obj_t * pFaninNew )
Alan Mishchenko committed
232 233 234 235 236 237 238 239
{
    int i, k, iFanin, Limit;
    assert( pFaninOld != pFaninNew );
    assert( pObj != pFaninOld );
    assert( pObj != pFaninNew );
    assert( pObj->pMan == pFaninOld->pMan );
    assert( pObj->pMan == pFaninNew->pMan );
    // update the fanin
Alan Mishchenko committed
240
    iFanin = Nwk_ObjFindFanin( pObj, pFaninOld );
Alan Mishchenko committed
241 242
    if ( iFanin == -1 )
    {
Alan Mishchenko committed
243
        printf( "Nwk_ObjPatchFanin(); Error! Node %d is not among", pFaninOld->Id );
Alan Mishchenko committed
244
        printf( " the fanins of node %d...\n", pObj->Id );
Alan Mishchenko committed
245 246 247 248 249 250 251 252 253 254
        return;
    }
    pObj->pFanio[iFanin] = pFaninNew;
    // remove pObj from the fanout list of pFaninOld
    Limit = pFaninOld->nFanins + pFaninOld->nFanouts;
    for ( k = i = pFaninOld->nFanins; i < Limit; i++ )
        if ( pFaninOld->pFanio[i] != pObj )
            pFaninOld->pFanio[k++] = pFaninOld->pFanio[i];
    pFaninOld->nFanouts--;
    // add pObj to the fanout list of pFaninNew
Alan Mishchenko committed
255 256
    if ( Nwk_ObjReallocIsNeeded(pFaninNew) )
        Nwk_ManReallocNode( pFaninNew );
Alan Mishchenko committed
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
    pFaninNew->pFanio[pFaninNew->nFanins + pFaninNew->nFanouts++] = pObj;
}


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

  Synopsis    [Transfers fanout from the old node to the new node.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
272
void Nwk_ObjTransferFanout( Nwk_Obj_t * pNodeFrom, Nwk_Obj_t * pNodeTo )
Alan Mishchenko committed
273 274
{
    Vec_Ptr_t * vFanouts = pNodeFrom->pMan->vTemp;
Alan Mishchenko committed
275
    Nwk_Obj_t * pTemp;
Alan Mishchenko committed
276
    int nFanoutsOld, i;
Alan Mishchenko committed
277
    assert( !Nwk_ObjIsCo(pNodeFrom) && !Nwk_ObjIsCo(pNodeTo) );
Alan Mishchenko committed
278 279
    assert( pNodeFrom->pMan == pNodeTo->pMan );
    assert( pNodeFrom != pNodeTo );
Alan Mishchenko committed
280
    assert( Nwk_ObjFanoutNum(pNodeFrom) > 0 );
Alan Mishchenko committed
281
    // get the fanouts of the old node
Alan Mishchenko committed
282 283
    nFanoutsOld = Nwk_ObjFanoutNum(pNodeTo);
    Nwk_ObjCollectFanouts( pNodeFrom, vFanouts );
Alan Mishchenko committed
284
    // patch the fanin of each of them
285
    Vec_PtrForEachEntry( Nwk_Obj_t *, vFanouts, pTemp, i )
Alan Mishchenko committed
286 287 288
        Nwk_ObjPatchFanin( pTemp, pNodeFrom, pNodeTo );
    assert( Nwk_ObjFanoutNum(pNodeFrom) == 0 );
    assert( Nwk_ObjFanoutNum(pNodeTo) == nFanoutsOld + Vec_PtrSize(vFanouts) );
Alan Mishchenko committed
289 290 291 292 293 294 295 296 297 298 299 300 301
}

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

  Synopsis    [Replaces the node by a new node.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
302
void Nwk_ObjReplace( Nwk_Obj_t * pNodeOld, Nwk_Obj_t * pNodeNew )
Alan Mishchenko committed
303 304 305
{
    assert( pNodeOld->pMan == pNodeNew->pMan );
    assert( pNodeOld != pNodeNew );
Alan Mishchenko committed
306
    assert( Nwk_ObjFanoutNum(pNodeOld) > 0 );
Alan Mishchenko committed
307
    // transfer the fanouts to the old node
Alan Mishchenko committed
308
    Nwk_ObjTransferFanout( pNodeOld, pNodeNew );
Alan Mishchenko committed
309
    // remove the old node
Alan Mishchenko committed
310
    Nwk_ManDeleteNode_rec( pNodeOld );
Alan Mishchenko committed
311 312 313 314 315 316 317 318
}


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


319 320
ABC_NAMESPACE_IMPL_END