summaryrefslogtreecommitdiffstats
path: root/src/opt/res/resWin.c
blob: fa74b21968a5741114df1158c9c7072323867870 (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
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
250
251
252
253
254
255
256
257
258
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/**CFile****************************************************************

  FileName    [resWin.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [Resynthesis package.]

  Synopsis    [Windowing algorithm.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

  Date        [Ver. 1.0. Started - January 15, 2007.]

  Revision    [$Id: resWin.c,v 1.00 2007/01/15 00:00:00 alanmi Exp $]

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

#include "abc.h"
#include "resInt.h"

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

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

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

  Synopsis    [Allocates the window.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Res_Win_t * Res_WinAlloc()
{
    Res_Win_t * p;
    p = ALLOC( Res_Win_t, 1 );
    memset( p, 0, sizeof(Res_Win_t) );
    p->vLevels = Vec_VecStart( 128 );
    p->vLeaves = Vec_PtrAlloc( 512 );
    p->vRoots  = Vec_PtrAlloc( 512 );
    p->vDivs   = Vec_PtrAlloc( 512 );
    return p;
}

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

  Synopsis    [Frees the window.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Res_WinFree( Res_Win_t * p )
{
    Vec_VecFree( p->vLevels );
    Vec_PtrFree( p->vLeaves );
    Vec_PtrFree( p->vRoots  );
    Vec_PtrFree( p->vDivs   );
    free( p );
}

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

  Synopsis    [Collects nodes in the limited TFI of the node.]

  Description [Marks the collected nodes.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Res_WinCollectNodeTfi( Res_Win_t * p )
{
    Vec_Ptr_t * vFront;
    Abc_Obj_t * pObj, * pFanin;
    int i, k, m;
    // expand storage for levels
    if ( Vec_VecSize( p->vLevels ) <= (int)p->pNode->Level + p->nWinTfoMax )
        Vec_VecExpand( p->vLevels, (int)p->pNode->Level + p->nWinTfoMax );
    // start the frontier
    Vec_VecClear( p->vLevels );
    Res_WinAddNode( p, p->pNode );
    // add one level at a time
    Vec_VecForEachLevelReverseStartStop( p->vLevels, vFront, i, p->pNode->Level, p->nLevLeaves + 1 )
    {
        Vec_PtrForEachEntry( vFront, pObj, k )
            Abc_ObjForEachFanin( pObj, pFanin, m )
                if ( !pFanin->fMarkA )
                    Res_WinAddNode( p, pFanin );
    }
}

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

  Synopsis    [Collect all the nodes that fanin into the window nodes.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Res_WinCollectLeaves( Res_Win_t * p )
{
    Vec_Ptr_t * vLevel;
    Abc_Obj_t * pObj;
    int i, k;
    // add the leaves
    Vec_PtrClear( p->vLeaves );
    // collect the nodes below the given level
    Vec_VecForEachEntryStartStop( p->vLevels, pObj, i, k, 0, p->nLevLeaves )
        Vec_PtrPush( p->vLeaves, pObj );
    // remove leaves from the set
    Vec_VecForEachLevelStartStop( p->vLevels, vLevel, i, 0, p->nLevLeaves )
        Vec_PtrClear( vLevel );
}

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

  Synopsis    [Marks the TFO of the collected nodes up to the given level.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Res_WinSweepLeafTfo_rec( Abc_Obj_t * pObj, int nLevelLimit, Abc_Obj_t * pNode )
{
    Abc_Obj_t * pFanout;
    int i;
    if ( Abc_ObjIsCo(pObj) || (int)pObj->Level > nLevelLimit || pObj == pNode )
        return;
    if ( Abc_NodeIsTravIdCurrent(pObj) )
        return;
    Abc_NodeSetTravIdCurrent( pObj );
    Abc_ObjForEachFanout( pObj, pFanout, i )
        Res_WinSweepLeafTfo_rec( pFanout, nLevelLimit, pNode );
}

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

  Synopsis    [Marks the TFO of the collected nodes up to the given level.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Res_WinSweepLeafTfo( Res_Win_t * p )
{
    Abc_Obj_t * pObj;
    int i;
    Abc_NtkIncrementTravId( p->pNode->pNtk );
    Vec_PtrForEachEntry( p->vLeaves, pObj, i )
        Res_WinSweepLeafTfo_rec( pObj, p->pNode->Level + p->nWinTfoMax, p->pNode );
}

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

  Synopsis    [Recursively collects the roots.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Res_WinCollectRoots_rec( Abc_Obj_t * pObj, Vec_Ptr_t * vRoots )
{
    Abc_Obj_t * pFanout;
    int i;
    assert( Abc_ObjIsNode(pObj) );
    // check if the node has all fanouts marked
    Abc_ObjForEachFanout( pObj, pFanout, i )
        if ( !Abc_NodeIsTravIdCurrent(pFanout) )
            break;
    // if some of the fanouts are unmarked, add the node to the root
    if ( i < Abc_ObjFanoutNum(pObj) ) 
    {
        Vec_PtrPushUnique( vRoots, pObj );
        return;
    }
    // otherwise, call recursively
    Abc_ObjForEachFanout( pObj, pFanout, i )
        Res_WinCollectRoots_rec( pFanout, vRoots );
}

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

  Synopsis    [Collects the roots of the window.]

  Description [Roots of the window are the nodes that have at least
  one fanout that it not in the TFO of the leaves.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Res_WinCollectRoots( Res_Win_t * p )
{
    assert( !Abc_NodeIsTravIdCurrent(p->pNode) );
    Vec_PtrClear( p->vRoots );
    Res_WinCollectRoots_rec( p->pNode, p->vRoots );
}

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

  Synopsis    [Recursively adds missing nodes and leaves.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Res_WinAddMissing_rec( Res_Win_t * p, Abc_Obj_t * pObj )
{
    Abc_Obj_t * pFanin;
    int i;
    if ( pObj->fMarkA )
        return;
    if ( !Abc_NodeIsTravIdCurrent(pObj) || (int)pObj->Level <= p->nLevLeaves )
    {
        p->nLeavesPlus++;
        Vec_PtrPush( p->vLeaves, pObj );
        pObj->fMarkA = 1;
        return;
    }
    Res_WinAddNode( p, pObj );
    // visit the fanins of the node
    Abc_ObjForEachFanin( pObj, pFanin, i )
        Res_WinAddMissing_rec( p, pFanin );
}

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

  Synopsis    [Adds to the window nodes and leaves in the TFI of the roots.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Res_WinAddMissing( Res_Win_t * p )
{
    Abc_Obj_t * pObj;
    int i;
    Vec_PtrForEachEntry( p->vRoots, pObj, i )
        Res_WinAddMissing_rec( p, pObj );
}

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

  Synopsis    [Unmarks the leaves and nodes of the window.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Res_WinUnmark( Res_Win_t * p )
{
    Abc_Obj_t * pObj;
    int i, k;
    Vec_VecForEachEntry( p->vLevels, pObj, i, k )
        pObj->fMarkA = 0;
    Vec_PtrForEachEntry( p->vLeaves, pObj, i )
        pObj->fMarkA = 0;
}

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

  Synopsis    [Verifies the window.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Res_WinVerify( Res_Win_t * p )
{
    Abc_Obj_t * pObj, * pFanin;
    int i, k, f;
    // make sure the nodes are not marked
    Abc_NtkForEachObj( p->pNode->pNtk, pObj, i )
        assert( !pObj->fMarkA );
    // mark the leaves
    Vec_PtrForEachEntry( p->vLeaves, pObj, i )
        pObj->fMarkA = 1;
    // make sure all nodes in the topological order have their fanins in the set
    Vec_VecForEachEntryStartStop( p->vLevels, pObj, i, k, p->nLevLeaves + 1, (int)p->pNode->Level + p->nWinTfoMax )
    {
        assert( (int)pObj->Level == i );
        assert( !pObj->fMarkA );
        Abc_ObjForEachFanin( pObj, pFanin, f )
            assert( pFanin->fMarkA );
        pObj->fMarkA = 1;
    }
    // make sure the roots are marked
    Vec_PtrForEachEntry( p->vRoots, pObj, i )
        assert( pObj->fMarkA );
    // unmark 
    Res_WinUnmark( p );
}

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

  Synopsis    [Computes the window.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Res_WinCompute( Abc_Obj_t * pNode, int nWinTfiMax, int nWinTfoMax, Res_Win_t * p )
{
    assert( Abc_ObjIsNode(pNode) );
    assert( nWinTfiMax > 0 && nWinTfoMax > 0 );
    // initialize the window
    p->pNode = pNode;
    p->nWinTfiMax = nWinTfiMax;
    p->nWinTfoMax = nWinTfoMax;
    p->nLeavesPlus = 0;
    p->nLevLeaves = ABC_MAX( 0, ((int)p->pNode->Level) - p->nWinTfiMax - 1 );
    // collect the nodes in TFI cone of pNode above the level of leaves (p->nLevLeaves)
    Res_WinCollectNodeTfi( p );
    // find the leaves of the window
    Res_WinCollectLeaves( p );
    // mark the TFO of the collected nodes up to the given level (p->pNode->Level + p->nWinTfoMax)
    Res_WinSweepLeafTfo( p );
    // find the roots of the window
    Res_WinCollectRoots( p );
    // add the nodes in the TFI of the roots that are not yet in the window
    Res_WinAddMissing( p );
    // unmark window nodes
    Res_WinUnmark( p );
    // clear the divisor information
    p->nLevDivMax = 0;
    p->nDivsPlus = 0;
    Vec_PtrClear( p->vDivs );
    // verify the resulting window
//    Res_WinVerify( p );
    return 1;
}

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