summaryrefslogtreecommitdiffstats
path: root/src/opt/rwr/rwrEva.c
blob: ddc9d7423f8c02e87d71aec8bd268496a7177eb0 (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
/**CFile****************************************************************

  FileName    [rwrDec.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [DAG-aware AIG rewriting package.]

  Synopsis    [Evaluation and decomposition procedures.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

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

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

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

#include "rwr.h"
#include "ft.h"

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

static Vec_Int_t * Rwr_CutEvaluate( Rwr_Man_t * p, Abc_Obj_t * pRoot, Rwr_Cut_t * pCut, int NodeMax, int LevelMax );

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

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

  Synopsis    [Performs rewriting for one node.]

  Description [This procedure considers all the cuts computed for the node
  and tries to rewrite each of them using the "forest" of different AIG
  structures precomputed and stored in the RWR manager. 
  Determines the best rewriting and computes the gain in the number of AIG
  nodes in the final network. In the end, p->vFanins contains information 
  about the best cut that can be used for rewriting, while p->vForm gives 
  the decomposition tree (represented using factored form data structure).
  Returns gain in the number of nodes or -1 if node cannot be rewritten.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Rwr_NodeRewrite( Rwr_Man_t * p, Abc_Obj_t * pNode )
{
    Vec_Int_t * vForm;
    Rwr_Cut_t * pCut;
    int Required, nNodesSaved;
    int i, BestGain = -1;
    // compute the cuts for this node
    Rwr_NodeComputeCuts( p, pNode );
    // get the required times
    Required = Vec_IntEntry( p->vReqTimes, pNode->Id );
    // label MFFC with current ID
    nNodesSaved = Abc_NodeMffcLabel( pNode );
    // go through the cuts
    for ( pCut = (Rwr_Cut_t *)pNode->pCopy, pCut = pCut->pNext; pCut; pCut = pCut->pNext )
    {
        // evaluate the cut
        vForm = Rwr_CutEvaluate( p, pNode, pCut, nNodesSaved, Required );
        // check if the cut is better than the currently best one
        if ( vForm != NULL && BestGain < (int)pCut->Volume )
        {
            assert( pCut->Volume >= 0 );
            BestGain  = pCut->Volume;
            // save this form
            p->vForm = vForm;
            // collect fanins
            Vec_PtrClear( p->vFanins );
            for ( i = 0; i < (int)pCut->nLeaves; i++ )
                Vec_PtrPush( p->vFanins, pCut->ppLeaves[i] );
        }
    }
    return BestGain;
}

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

  Synopsis    [Evaluates the cut.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Vec_Int_t * Rwr_CutEvaluate( Rwr_Man_t * p, Abc_Obj_t * pRoot, Rwr_Cut_t * pCut, int NodeMax, int LevelMax )
{
    Vec_Ptr_t Vector = {0,0,0}, * vFanins = &Vector;
    Vec_Ptr_t * vSubgraphs;
    Vec_Int_t * vFormBest;
    Rwr_Node_t * pNode;
    int GainCur, GainBest = -1, i;
    // find the matching class of subgraphs
    vSubgraphs = Vec_VecEntry( p->vClasses, p->pMap[pCut->uTruth] );
    // determine the best subgraph
    Vec_PtrForEachEntry( vSubgraphs, pNode, i )
    {
        // create the fanin array
        vFanins->nSize  = pCut->nLeaves;
        vFanins->pArray = pCut->ppLeaves;
        // detect how many unlabeled nodes will be reused
        GainCur = Abc_NodeStrashDecCount( pRoot->pNtk->pManFunc, vFanins, (Vec_Int_t *)pNode->pNext, 
            p->vLevNums, NodeMax, LevelMax );
        if ( GainBest < GainCur )
        {
            GainBest  = GainCur;
            vFormBest = (Vec_Int_t *)pNode->pNext;
        }
    }
    if ( GainBest == -1 )
        return NULL;
    pCut->Volume = GainBest;
    return vFormBest;
}


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

  Synopsis    [Adds one node.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Rwr_TravCollect_rec( Rwr_Man_t * p, Rwr_Node_t * pNode, Vec_Int_t * vForm )
{
    Ft_Node_t Node, NodeA, NodeB;
    int Node0, Node1;
    // elementary variable
    if ( pNode->fUsed )
        return ((pNode->Id - 1) << 1); 
    // previously visited node
    if ( pNode->TravId == p->nTravIds )
        return pNode->Volume;
    pNode->TravId = p->nTravIds;
    // solve for children
    Node0 = Rwr_TravCollect_rec( p, Rwr_Regular(pNode->p0), vForm );
    Node1 = Rwr_TravCollect_rec( p, Rwr_Regular(pNode->p1), vForm );
    // create the decomposition node(s)
    if ( pNode->fExor )
    {
        assert( !Rwr_IsComplement(pNode->p0) );
        assert( !Rwr_IsComplement(pNode->p1) );
        NodeA.fIntern = 1;
        NodeA.fConst  = 0;
        NodeA.fCompl  = 0;
        NodeA.fCompl0 = !(Node0 & 1);
        NodeA.fCompl1 =  (Node1 & 1);
        NodeA.iFanin0 = (Node0 >> 1);
        NodeA.iFanin1 = (Node1 >> 1);
        Vec_IntPush( vForm, Ft_Node2Int(NodeA) );

        NodeB.fIntern = 1;
        NodeB.fConst  = 0;
        NodeB.fCompl  = 0;
        NodeB.fCompl0 =  (Node0 & 1);
        NodeB.fCompl1 = !(Node1 & 1);
        NodeB.iFanin0 = (Node0 >> 1);
        NodeB.iFanin1 = (Node1 >> 1);
        Vec_IntPush( vForm, Ft_Node2Int(NodeB) );

        Node.fIntern = 1;
        Node.fConst  = 0;
        Node.fCompl  = 0;
        Node.fCompl0 = 1;
        Node.fCompl1 = 1;
        Node.iFanin0 = vForm->nSize - 2;
        Node.iFanin1 = vForm->nSize - 1;
        Vec_IntPush( vForm, Ft_Node2Int(Node) );
    }
    else
    {
        Node.fIntern = 1;
        Node.fConst  = 0;
        Node.fCompl  = 0;
        Node.fCompl0 = Rwr_IsComplement(pNode->p0) ^ (Node0 & 1);
        Node.fCompl1 = Rwr_IsComplement(pNode->p1) ^ (Node1 & 1);
        Node.iFanin0 = (Node0 >> 1);
        Node.iFanin1 = (Node1 >> 1);
        Vec_IntPush( vForm, Ft_Node2Int(Node) );
    }
    // save the number of this node
    pNode->Volume = ((vForm->nSize - 1) << 1) | pNode->fExor;
    return pNode->Volume;
}

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

  Synopsis    [Preprocesses subgraphs rooted at this node.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Rwr_NodePreprocess( Rwr_Man_t * p, Rwr_Node_t * pNode )
{
    Vec_Int_t * vForm;
    int i, Root;
    vForm = Vec_IntAlloc( 10 );
    for ( i = 0; i < 5; i++ )
        Vec_IntPush( vForm, 0 );
    // collect the nodes
    Rwr_ManIncTravId( p );
    Root = Rwr_TravCollect_rec( p, pNode, vForm );
    if ( Root & 1 )
        Ft_FactorComplement( vForm );
    pNode->pNext = (Rwr_Node_t *)vForm;
}

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

  Synopsis    [Preprocesses computed library of subgraphs.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Rwr_ManPreprocess( Rwr_Man_t * p )
{
    Rwr_Node_t * pNode;
    int i, k;
    // put the nodes into the structure
    p->vClasses = Vec_VecAlloc( 222 );
    for ( i = 0; i < p->nFuncs; i++ )
        for ( pNode = p->pTable[i]; pNode; pNode = pNode->pNext )
            Vec_VecPush( p->vClasses, p->pMap[pNode->uTruth], pNode );
    // compute decomposition forms for each node
    Vec_VecForEachEntry( p->vClasses, pNode, i, k )
        Rwr_NodePreprocess( p, pNode );
}

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