resFilter.c 15.6 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 23
/**CFile****************************************************************

  FileName    [resFilter.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [Resynthesis package.]

  Synopsis    [Filtering resubstitution candidates.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

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

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

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

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

24 25 26
ABC_NAMESPACE_IMPL_START


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

static unsigned * Res_FilterCollectFaninInfo( Res_Win_t * pWin, Res_Sim_t * pSim, unsigned uMask );
static int        Res_FilterCriticalFanin( Abc_Obj_t * pNode );

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

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

  Synopsis    [Finds sets of feasible candidates.]

  Description []
               
  SideEffects []

  SeeAlso     []
 
***********************************************************************/
int Res_FilterCandidates( Res_Win_t * pWin, Abc_Ntk_t * pAig, Res_Sim_t * pSim, Vec_Vec_t * vResubs, Vec_Vec_t * vResubsW, int nFaninsMax, int fArea )
{
    Abc_Obj_t * pFanin, * pFanin2, * pFaninTemp;
    unsigned * pInfo, * pInfoDiv, * pInfoDiv2;
    int Counter, RetValue, i, i2, d, d2, iDiv, iDiv2, k;

    // check that the info the node is one
56
    pInfo = (unsigned *)Vec_PtrEntry( pSim->vOuts, 1 );
Alan Mishchenko committed
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
    RetValue = Abc_InfoIsOne( pInfo, pSim->nWordsOut );
    if ( RetValue == 0 )
    {
//        printf( "Failed 1!\n" );
        return 0;
    }

    // collect the fanin info
    pInfo = Res_FilterCollectFaninInfo( pWin, pSim, ~0 );
    RetValue = Abc_InfoIsOne( pInfo, pSim->nWordsOut );
    if ( RetValue == 0 )
    {
//        printf( "Failed 2!\n" );
        return 0;
    }

    // try removing each fanin
//    printf( "Fanins: " );
    Counter = 0;
    Vec_VecClear( vResubs );
    Vec_VecClear( vResubsW );
    Abc_ObjForEachFanin( pWin->pNode, pFanin, i )
    {
        if ( fArea && Abc_ObjFanoutNum(pFanin) > 1 )
            continue;
        // get simulation info without this fanin
        pInfo = Res_FilterCollectFaninInfo( pWin, pSim, ~(1 << i) );
        RetValue = Abc_InfoIsOne( pInfo, pSim->nWordsOut );
        if ( RetValue )
        {
//            printf( "Node %4d. Candidate fanin %4d.\n", pWin->pNode->Id, pFanin->Id );
            // collect the nodes
            Vec_VecPush( vResubs, Counter, Abc_NtkPo(pAig,0) );
            Vec_VecPush( vResubs, Counter, Abc_NtkPo(pAig,1) );
            Abc_ObjForEachFanin( pWin->pNode, pFaninTemp, k )
            {
                if ( k != i )
                {
                    Vec_VecPush( vResubs, Counter, Abc_NtkPo(pAig,2+k) );
                    Vec_VecPush( vResubsW, Counter, pFaninTemp );
                }
            }
            Counter++;
            if ( Counter == Vec_VecSize(vResubs) )
                return Counter;    
        }
    }

    // try replacing each critical fanin by a non-critical fanin
    Abc_ObjForEachFanin( pWin->pNode, pFanin, i )
    {
        if ( Abc_ObjFanoutNum(pFanin) > 1 )
            continue;
        // get simulation info without this fanin
        pInfo = Res_FilterCollectFaninInfo( pWin, pSim, ~(1 << i) );
        // go over the set of divisors
        for ( d = Abc_ObjFaninNum(pWin->pNode) + 2; d < Abc_NtkPoNum(pAig); d++ )
        {
115
            pInfoDiv = (unsigned *)Vec_PtrEntry( pSim->vOuts, d );
Alan Mishchenko committed
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
            iDiv = d - (Abc_ObjFaninNum(pWin->pNode) + 2);
            if ( !Abc_InfoIsOrOne( pInfo, pInfoDiv, pSim->nWordsOut ) )
                continue;
            // collect the nodes
            Vec_VecPush( vResubs, Counter, Abc_NtkPo(pAig,0) );
            Vec_VecPush( vResubs, Counter, Abc_NtkPo(pAig,1) );
            // collect the remaning fanins and the divisor
            Abc_ObjForEachFanin( pWin->pNode, pFaninTemp, k )
            {
                if ( k != i )
                {
                    Vec_VecPush( vResubs, Counter, Abc_NtkPo(pAig,2+k) );
                    Vec_VecPush( vResubsW, Counter, pFaninTemp );
                }
            }
            // collect the divisor
            Vec_VecPush( vResubs, Counter, Abc_NtkPo(pAig,d) );
            Vec_VecPush( vResubsW, Counter, Vec_PtrEntry(pWin->vDivs, iDiv) );
            Counter++;
            if ( Counter == Vec_VecSize(vResubs) )
                return Counter;           
        }
    }

    // consider the case when two fanins can be added instead of one
    if ( Abc_ObjFaninNum(pWin->pNode) < nFaninsMax )
    {
        // try to replace each critical fanin by two non-critical fanins
        Abc_ObjForEachFanin( pWin->pNode, pFanin, i )
        {
            if ( Abc_ObjFanoutNum(pFanin) > 1 )
                continue;
            // get simulation info without this fanin
            pInfo = Res_FilterCollectFaninInfo( pWin, pSim, ~(1 << i) );
            // go over the set of divisors
            for ( d = Abc_ObjFaninNum(pWin->pNode) + 2; d < Abc_NtkPoNum(pAig); d++ )
            {
153
                pInfoDiv = (unsigned *)Vec_PtrEntry( pSim->vOuts, d );
Alan Mishchenko committed
154 155 156 157
                iDiv = d - (Abc_ObjFaninNum(pWin->pNode) + 2);
                // go through the second divisor
                for ( d2 = d + 1; d2 < Abc_NtkPoNum(pAig); d2++ )
                {
158
                    pInfoDiv2 = (unsigned *)Vec_PtrEntry( pSim->vOuts, d2 );
Alan Mishchenko committed
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
                    iDiv2 = d2 - (Abc_ObjFaninNum(pWin->pNode) + 2);
                    if ( !Abc_InfoIsOrOne3( pInfo, pInfoDiv, pInfoDiv2, pSim->nWordsOut ) )
                        continue;
                    // collect the nodes
                    Vec_VecPush( vResubs, Counter, Abc_NtkPo(pAig,0) );
                    Vec_VecPush( vResubs, Counter, Abc_NtkPo(pAig,1) );
                    // collect the remaning fanins and the divisor
                    Abc_ObjForEachFanin( pWin->pNode, pFaninTemp, k )
                    {
                        if ( k != i )
                        {
                            Vec_VecPush( vResubs, Counter, Abc_NtkPo(pAig,2+k) );
                            Vec_VecPush( vResubsW, Counter, pFaninTemp );
                        }
                    }
                    // collect the divisor
                    Vec_VecPush( vResubs, Counter, Abc_NtkPo(pAig,d) );
                    Vec_VecPush( vResubs, Counter, Abc_NtkPo(pAig,d2) );
                    Vec_VecPush( vResubsW, Counter, Vec_PtrEntry(pWin->vDivs, iDiv) );
                    Vec_VecPush( vResubsW, Counter, Vec_PtrEntry(pWin->vDivs, iDiv2) );
                    Counter++;
                    if ( Counter == Vec_VecSize(vResubs) )
                        return Counter;           
                }
            }
        }
    }

    // try to replace two nets by one
    if ( !fArea )
    {
        Abc_ObjForEachFanin( pWin->pNode, pFanin, i )
        {
            for ( i2 = i + 1; i2 < Abc_ObjFaninNum(pWin->pNode); i2++ )
            {
                pFanin2 = Abc_ObjFanin(pWin->pNode, i2);
                // get simulation info without these fanins
                pInfo = Res_FilterCollectFaninInfo( pWin, pSim, (~(1 << i)) & (~(1 << i2)) );
                // go over the set of divisors
                for ( d = Abc_ObjFaninNum(pWin->pNode) + 2; d < Abc_NtkPoNum(pAig); d++ )
                {
200
                    pInfoDiv = (unsigned *)Vec_PtrEntry( pSim->vOuts, d );
Alan Mishchenko committed
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
                    iDiv = d - (Abc_ObjFaninNum(pWin->pNode) + 2);
                    if ( !Abc_InfoIsOrOne( pInfo, pInfoDiv, pSim->nWordsOut ) )
                        continue;
                    // collect the nodes
                    Vec_VecPush( vResubs, Counter, Abc_NtkPo(pAig,0) );
                    Vec_VecPush( vResubs, Counter, Abc_NtkPo(pAig,1) );
                    // collect the remaning fanins and the divisor
                    Abc_ObjForEachFanin( pWin->pNode, pFaninTemp, k )
                    {
                        if ( k != i && k != i2 )
                        {
                            Vec_VecPush( vResubs, Counter, Abc_NtkPo(pAig,2+k) );
                            Vec_VecPush( vResubsW, Counter, pFaninTemp );
                        }
                    }
                    // collect the divisor
                    Vec_VecPush( vResubs, Counter, Abc_NtkPo(pAig,d) );
                    Vec_VecPush( vResubsW, Counter, Vec_PtrEntry(pWin->vDivs, iDiv) );
                    Counter++;
                    if ( Counter == Vec_VecSize(vResubs) )
                        return Counter;           
                }
            }
        }
    }
    return Counter;
}


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

  Synopsis    [Finds sets of feasible candidates.]

  Description [This procedure is a special case of the above.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Res_FilterCandidatesArea( Res_Win_t * pWin, Abc_Ntk_t * pAig, Res_Sim_t * pSim, Vec_Vec_t * vResubs, Vec_Vec_t * vResubsW, int nFaninsMax )
{
    Abc_Obj_t * pFanin;
    unsigned * pInfo, * pInfoDiv, * pInfoDiv2;
    int Counter, RetValue, d, d2, k, iDiv, iDiv2, iBest;

    // check that the info the node is one
248
    pInfo = (unsigned *)Vec_PtrEntry( pSim->vOuts, 1 );
Alan Mishchenko committed
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
    RetValue = Abc_InfoIsOne( pInfo, pSim->nWordsOut );
    if ( RetValue == 0 )
    {
//        printf( "Failed 1!\n" );
        return 0;
    }

    // collect the fanin info
    pInfo = Res_FilterCollectFaninInfo( pWin, pSim, ~0 );
    RetValue = Abc_InfoIsOne( pInfo, pSim->nWordsOut );
    if ( RetValue == 0 )
    {
//        printf( "Failed 2!\n" );
        return 0;
    }

    // try removing fanins
//    printf( "Fanins: " );
    Counter = 0;
    Vec_VecClear( vResubs );
    Vec_VecClear( vResubsW );
    // get the best fanins
    iBest = Res_FilterCriticalFanin( pWin->pNode );
    if ( iBest == -1 )
        return 0;

    // get the info without the critical fanin
    pInfo = Res_FilterCollectFaninInfo( pWin, pSim, ~(1 << iBest) );
    RetValue = Abc_InfoIsOne( pInfo, pSim->nWordsOut );
    if ( RetValue )
    {
//        printf( "Can be done without one!\n" );
        // collect the nodes
        Vec_VecPush( vResubs, Counter, Abc_NtkPo(pAig,0) );
        Vec_VecPush( vResubs, Counter, Abc_NtkPo(pAig,1) );
        Abc_ObjForEachFanin( pWin->pNode, pFanin, k )
        {
            if ( k != iBest )
            {
                Vec_VecPush( vResubs, Counter, Abc_NtkPo(pAig,2+k) );
                Vec_VecPush( vResubsW, Counter, pFanin );
            }
        }
        Counter++;
//        printf( "*" );
        return Counter;
    }

    // go through the divisors
    for ( d = Abc_ObjFaninNum(pWin->pNode) + 2; d < Abc_NtkPoNum(pAig); d++ )
    {
300
        pInfoDiv = (unsigned *)Vec_PtrEntry( pSim->vOuts, d );
Alan Mishchenko committed
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
        iDiv = d - (Abc_ObjFaninNum(pWin->pNode) + 2);
        if ( !Abc_InfoIsOrOne( pInfo, pInfoDiv, pSim->nWordsOut ) )
            continue;
//if ( Abc_ObjLevel(pWin->pNode) <= Abc_ObjLevel( Vec_PtrEntry(pWin->vDivs, iDiv) ) )
//    printf( "Node level = %d. Divisor level = %d.\n", Abc_ObjLevel(pWin->pNode), Abc_ObjLevel( Vec_PtrEntry(pWin->vDivs, iDiv) ) );
        // collect the nodes
        Vec_VecPush( vResubs, Counter, Abc_NtkPo(pAig,0) );
        Vec_VecPush( vResubs, Counter, Abc_NtkPo(pAig,1) );
        // collect the remaning fanins and the divisor
        Abc_ObjForEachFanin( pWin->pNode, pFanin, k )
        {
            if ( k != iBest )
            {
                Vec_VecPush( vResubs, Counter, Abc_NtkPo(pAig,2+k) );
                Vec_VecPush( vResubsW, Counter, pFanin );
            }
        }
        // collect the divisor
        Vec_VecPush( vResubs, Counter, Abc_NtkPo(pAig,d) );
        Vec_VecPush( vResubsW, Counter, Vec_PtrEntry(pWin->vDivs, iDiv) );
        Counter++;

        if ( Counter == Vec_VecSize(vResubs) )
            break;            
    }

    if ( Counter > 0 || Abc_ObjFaninNum(pWin->pNode) >= nFaninsMax )
        return Counter;

    // try to find the node pairs
    for ( d = Abc_ObjFaninNum(pWin->pNode) + 2; d < Abc_NtkPoNum(pAig); d++ )
    {
333
        pInfoDiv = (unsigned *)Vec_PtrEntry( pSim->vOuts, d );
Alan Mishchenko committed
334 335 336 337
        iDiv = d - (Abc_ObjFaninNum(pWin->pNode) + 2);
        // go through the second divisor
        for ( d2 = d + 1; d2 < Abc_NtkPoNum(pAig); d2++ )
        {
338
            pInfoDiv2 = (unsigned *)Vec_PtrEntry( pSim->vOuts, d2 );
Alan Mishchenko committed
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 381 382 383 384 385 386 387
            iDiv2 = d2 - (Abc_ObjFaninNum(pWin->pNode) + 2);

            if ( !Abc_InfoIsOrOne3( pInfo, pInfoDiv, pInfoDiv2, pSim->nWordsOut ) )
                continue;
            // collect the nodes
            Vec_VecPush( vResubs, Counter, Abc_NtkPo(pAig,0) );
            Vec_VecPush( vResubs, Counter, Abc_NtkPo(pAig,1) );
            // collect the remaning fanins and the divisor
            Abc_ObjForEachFanin( pWin->pNode, pFanin, k )
            {
                if ( k != iBest )
                {
                    Vec_VecPush( vResubs, Counter, Abc_NtkPo(pAig,2+k) );
                    Vec_VecPush( vResubsW, Counter, pFanin );
                }
            }
            // collect the divisor
            Vec_VecPush( vResubs, Counter, Abc_NtkPo(pAig,d) );
            Vec_VecPush( vResubs, Counter, Abc_NtkPo(pAig,d2) );
            Vec_VecPush( vResubsW, Counter, Vec_PtrEntry(pWin->vDivs, iDiv) );
            Vec_VecPush( vResubsW, Counter, Vec_PtrEntry(pWin->vDivs, iDiv2) );
            Counter++;

            if ( Counter == Vec_VecSize(vResubs) )
                break;            
        }
        if ( Counter == Vec_VecSize(vResubs) )
            break;            
    }
    return Counter;
}


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

  Synopsis    [Finds sets of feasible candidates.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
unsigned * Res_FilterCollectFaninInfo( Res_Win_t * pWin, Res_Sim_t * pSim, unsigned uMask )
{
    Abc_Obj_t * pFanin;
    unsigned * pInfo;
    int i;
388
    pInfo = (unsigned *)Vec_PtrEntry( pSim->vOuts, 0 );
Alan Mishchenko committed
389 390 391 392
    Abc_InfoClear( pInfo, pSim->nWordsOut );
    Abc_ObjForEachFanin( pWin->pNode, pFanin, i )
    {
        if ( uMask & (1 << i) )
393
            Abc_InfoOr( pInfo, (unsigned *)Vec_PtrEntry(pSim->vOuts, 2+i), pSim->nWordsOut );
Alan Mishchenko committed
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437
    }
    return pInfo;
}


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

  Synopsis    [Returns the index of the most critical fanin.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Res_FilterCriticalFanin( Abc_Obj_t * pNode )
{
    Abc_Obj_t * pFanin;
    int i, iBest = -1, CostMax = 0, CostCur;
    Abc_ObjForEachFanin( pNode, pFanin, i )
    {
        if ( !Abc_ObjIsNode(pFanin) )
            continue;
        if ( Abc_ObjFanoutNum(pFanin) > 1 )
            continue;
        CostCur = Res_WinVisitMffc( pFanin );
        if ( CostMax < CostCur )
        {
            CostMax = CostCur;
            iBest = i;
        }
    }
//    if ( CostMax > 0 )
//        printf( "<%d>", CostMax );
    return iBest;
}


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


438 439
ABC_NAMESPACE_IMPL_END