darLib.c 42.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
/**CFile****************************************************************

  FileName    [darLib.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [DAG-aware AIG rewriting.]

  Synopsis    [Library of AIG subgraphs used for rewriting.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

  Date        [Ver. 1.0. Started - April 28, 2007.]

  Revision    [$Id: darLib.c,v 1.00 2007/04/28 00:00:00 alanmi Exp $]

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

Alan Mishchenko committed
21
#include "darInt.h"
22
#include "aig/gia/gia.h"
23 24 25 26
#include "dar.h"

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

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

typedef struct Dar_Lib_t_            Dar_Lib_t;
typedef struct Dar_LibObj_t_         Dar_LibObj_t;
typedef struct Dar_LibDat_t_         Dar_LibDat_t;

struct Dar_LibObj_t_ // library object (2 words)
{
    unsigned         Fan0    : 16;  // the first fanin
    unsigned         Fan1    : 16;  // the second fanin
    unsigned         fCompl0 :  1;  // the first compl attribute
    unsigned         fCompl1 :  1;  // the second compl attribute
    unsigned         fPhase  :  1;  // the phase of the node
    unsigned         fTerm   :  1;  // indicates a PI
    unsigned         Num     : 28;  // internal use
};

struct Dar_LibDat_t_ // library object data
{
49
    union { 
Alan Mishchenko committed
50
    Aig_Obj_t *      pFunc;         // the corresponding AIG node if it exists
51
    int              iGunc; };      // the corresponding AIG node if it exists
Alan Mishchenko committed
52 53
    int              Level;         // level of this node after it is constructured
    int              TravId;        // traversal ID of the library object data
Alan Mishchenko committed
54
    float            dProb;         // probability of the node being 1
Alan Mishchenko committed
55
    unsigned char    fMffc;         // set to one if node is part of MFFC
Alan Mishchenko committed
56
    unsigned char    nLats[3];      // the number of latches on the input/output stem
Alan Mishchenko committed
57 58 59 60 61 62 63 64 65 66 67 68
};

struct Dar_Lib_t_ // library 
{
    // objects
    Dar_LibObj_t *   pObjs;         // the set of library objects
    int              nObjs;         // the number of objects used
    int              iObj;          // the current object
    // structures by class
    int              nSubgr[222];   // the number of subgraphs by class
    int *            pSubgr[222];   // the subgraphs for each class
    int *            pSubgrMem;     // memory for subgraph pointers
Alan Mishchenko committed
69 70 71 72 73 74 75 76 77 78
    int              nSubgrTotal;   // the total number of subgraph
    // structure priorities
    int *            pPriosMem;     // memory for priority of structures
    int *            pPrios[222];   // pointers to the priority numbers
    // structure places in the priorities
    int *            pPlaceMem;     // memory for places of structures in the priority lists
    int *            pPlace[222];   // pointers to the places numbers
    // structure scores
    int *            pScoreMem;     // memory for scores of structures
    int *            pScore[222];   // pointers to the scores numbers
Alan Mishchenko committed
79 80 81 82
    // nodes by class
    int              nNodes[222];   // the number of nodes by class
    int *            pNodes[222];   // the nodes for each class
    int *            pNodesMem;     // memory for nodes pointers
Alan Mishchenko committed
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
    int              nNodesTotal;   // the total number of nodes
    // prepared library
    int              nSubgraphs;
    int              nNodes0Max;
    // nodes by class
    int              nNodes0[222];   // the number of nodes by class
    int *            pNodes0[222];   // the nodes for each class
    int *            pNodes0Mem;     // memory for nodes pointers
    int              nNodes0Total;   // the total number of nodes
    // structures by class
    int              nSubgr0[222];   // the number of subgraphs by class
    int *            pSubgr0[222];   // the subgraphs for each class
    int *            pSubgr0Mem;     // memory for subgraph pointers
    int              nSubgr0Total;   // the total number of subgraph
    // object data
    Dar_LibDat_t *   pDatas;
    int              nDatas;
    // information about NPN classes
Alan Mishchenko committed
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
    char **          pPerms4;
    unsigned short * puCanons; 
    char *           pPhases; 
    char *           pPerms; 
    unsigned char *  pMap;
};

static Dar_Lib_t * s_DarLib = NULL;

static inline Dar_LibObj_t * Dar_LibObj( Dar_Lib_t * p, int Id )    { return p->pObjs + Id; }
static inline int            Dar_LibObjTruth( Dar_LibObj_t * pObj ) { return pObj->Num < (0xFFFF & ~pObj->Num) ? pObj->Num : (0xFFFF & ~pObj->Num); }

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

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

  Synopsis    [Starts the library.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
128
Dar_Lib_t * Dar_LibAlloc( int nObjs )
Alan Mishchenko committed
129 130 131
{
    unsigned uTruths[4] = { 0xAAAA, 0xCCCC, 0xF0F0, 0xFF00 };
    Dar_Lib_t * p;
Alan Mishchenko committed
132
    int i;//, clk = clock();
Alan Mishchenko committed
133
    p = ABC_ALLOC( Dar_Lib_t, 1 );
Alan Mishchenko committed
134 135 136
    memset( p, 0, sizeof(Dar_Lib_t) );
    // allocate objects
    p->nObjs = nObjs;
Alan Mishchenko committed
137
    p->pObjs = ABC_ALLOC( Dar_LibObj_t, nObjs );
Alan Mishchenko committed
138 139
    memset( p->pObjs, 0, sizeof(Dar_LibObj_t) * nObjs );
    // allocate canonical data
Alan Mishchenko committed
140 141
    p->pPerms4 = Dar_Permutations( 4 );
    Dar_Truth4VarNPN( &p->puCanons, &p->pPhases, &p->pPerms, &p->pMap );
Alan Mishchenko committed
142 143 144 145 146 147 148
    // start the elementary objects
    p->iObj = 4;
    for ( i = 0; i < 4; i++ )
    {
        p->pObjs[i].fTerm = 1;
        p->pObjs[i].Num = uTruths[i];
    }
Alan Mishchenko committed
149
//    ABC_PRT( "Library start", clock() - clk );
Alan Mishchenko committed
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
    return p;
}

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

  Synopsis    [Frees the library.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Dar_LibFree( Dar_Lib_t * p )
{
Alan Mishchenko committed
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
    ABC_FREE( p->pObjs );
    ABC_FREE( p->pDatas );
    ABC_FREE( p->pNodesMem );
    ABC_FREE( p->pNodes0Mem );
    ABC_FREE( p->pSubgrMem );
    ABC_FREE( p->pSubgr0Mem );
    ABC_FREE( p->pPriosMem );
    ABC_FREE( p->pPlaceMem );
    ABC_FREE( p->pScoreMem );
    ABC_FREE( p->pPerms4 );
    ABC_FREE( p->puCanons );
    ABC_FREE( p->pPhases );
    ABC_FREE( p->pPerms );
    ABC_FREE( p->pMap );
    ABC_FREE( p );
Alan Mishchenko committed
181 182 183 184
}

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

Alan Mishchenko committed
185 186 187 188 189 190 191 192 193
  Synopsis    [Returns canonical truth tables.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
int Dar_LibReturnClass( unsigned uTruth )
{
    return s_DarLib->pMap[uTruth & 0xffff];
}


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

  Synopsis    [Returns canonical truth tables.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
void Dar_LibReturnCanonicals( unsigned * pCanons )
{
    int Visits[222] = {0};
    int i, k;
    // find canonical truth tables
    for ( i = k = 0; i < (1<<16); i++ )
        if ( !Visits[s_DarLib->pMap[i]] )
        {
            Visits[s_DarLib->pMap[i]] = 1;
            pCanons[k++] = ((i<<16) | i);
        }
    assert( k == 222 );
}

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

Alan Mishchenko committed
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
  Synopsis    [Adds one AND to the library.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Dar_LibAddNode( Dar_Lib_t * p, int Id0, int Id1, int fCompl0, int fCompl1 )
{
    Dar_LibObj_t * pFan0 = Dar_LibObj( p, Id0 );
    Dar_LibObj_t * pFan1 = Dar_LibObj( p, Id1 );
    Dar_LibObj_t * pObj  = p->pObjs + p->iObj++;
    pObj->Fan0 = Id0;
    pObj->Fan1 = Id1;
    pObj->fCompl0 = fCompl0;
    pObj->fCompl1 = fCompl1;
    pObj->fPhase = (fCompl0 ^ pFan0->fPhase) & (fCompl1 ^ pFan1->fPhase);
    pObj->Num = 0xFFFF & (fCompl0? ~pFan0->Num : pFan0->Num) & (fCompl1? ~pFan1->Num : pFan1->Num);
}

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

  Synopsis    [Adds one AND to the library.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
260
void Dar_LibSetup_rec( Dar_Lib_t * p, Dar_LibObj_t * pObj, int Class, int fCollect )
Alan Mishchenko committed
261 262 263 264
{
    if ( pObj->fTerm || (int)pObj->Num == Class )
        return;
    pObj->Num = Class;
Alan Mishchenko committed
265 266 267
    Dar_LibSetup_rec( p, Dar_LibObj(p, pObj->Fan0), Class, fCollect );
    Dar_LibSetup_rec( p, Dar_LibObj(p, pObj->Fan1), Class, fCollect );
    if ( fCollect )
Alan Mishchenko committed
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
        p->pNodes[Class][ p->nNodes[Class]++ ] = pObj-p->pObjs;
    else
        p->nNodes[Class]++;
}

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

  Synopsis    [Adds one AND to the library.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
284
void Dar_LibSetup( Dar_Lib_t * p, Vec_Int_t * vOuts, Vec_Int_t * vPrios )
Alan Mishchenko committed
285
{
Alan Mishchenko committed
286
    int fTraining = 0;
Alan Mishchenko committed
287
    Dar_LibObj_t * pObj;
Alan Mishchenko committed
288
    int nNodesTotal, uTruth, Class, Out, i, k;
Alan Mishchenko committed
289 290 291 292 293 294 295 296 297 298 299 300 301
    assert( p->iObj == p->nObjs );

    // count the number of representatives of each class
    for ( i = 0; i < 222; i++ )
        p->nSubgr[i] = p->nNodes[i] = 0;
    Vec_IntForEachEntry( vOuts, Out, i )
    {
        pObj = Dar_LibObj( p, Out );
        uTruth = Dar_LibObjTruth( pObj );
        Class = p->pMap[uTruth];
        p->nSubgr[Class]++;
    }
    // allocate memory for the roots of each class
Alan Mishchenko committed
302 303
    p->pSubgrMem = ABC_ALLOC( int, Vec_IntSize(vOuts) );
    p->pSubgr0Mem = ABC_ALLOC( int, Vec_IntSize(vOuts) );
Alan Mishchenko committed
304
    p->nSubgrTotal = 0;
Alan Mishchenko committed
305 306
    for ( i = 0; i < 222; i++ )
    {
Alan Mishchenko committed
307
        p->pSubgr[i] = p->pSubgrMem + p->nSubgrTotal;
Alan Mishchenko committed
308
        p->pSubgr0[i] = p->pSubgr0Mem + p->nSubgrTotal;
Alan Mishchenko committed
309
        p->nSubgrTotal += p->nSubgr[i];
Alan Mishchenko committed
310 311
        p->nSubgr[i] = 0;
    }
Alan Mishchenko committed
312
    assert( p->nSubgrTotal == Vec_IntSize(vOuts) );
Alan Mishchenko committed
313 314 315 316 317 318 319 320 321
    // add the outputs to storage
    Vec_IntForEachEntry( vOuts, Out, i )
    {
        pObj = Dar_LibObj( p, Out );
        uTruth = Dar_LibObjTruth( pObj );
        Class = p->pMap[uTruth];
        p->pSubgr[Class][ p->nSubgr[Class]++ ] = Out;
    }

Alan Mishchenko committed
322 323 324
    if ( fTraining )
    {
        // allocate memory for the priority of roots of each class
Alan Mishchenko committed
325
        p->pPriosMem = ABC_ALLOC( int, Vec_IntSize(vOuts) );
Alan Mishchenko committed
326 327 328 329 330 331 332 333 334 335 336 337
        p->nSubgrTotal = 0;
        for ( i = 0; i < 222; i++ )
        {
            p->pPrios[i] = p->pPriosMem + p->nSubgrTotal;
            p->nSubgrTotal += p->nSubgr[i];
            for ( k = 0; k < p->nSubgr[i]; k++ )
                p->pPrios[i][k] = k;

        }
        assert( p->nSubgrTotal == Vec_IntSize(vOuts) );

        // allocate memory for the priority of roots of each class
Alan Mishchenko committed
338
        p->pPlaceMem = ABC_ALLOC( int, Vec_IntSize(vOuts) );
Alan Mishchenko committed
339 340 341 342 343 344 345 346 347 348 349 350
        p->nSubgrTotal = 0;
        for ( i = 0; i < 222; i++ )
        {
            p->pPlace[i] = p->pPlaceMem + p->nSubgrTotal;
            p->nSubgrTotal += p->nSubgr[i];
            for ( k = 0; k < p->nSubgr[i]; k++ )
                p->pPlace[i][k] = k;

        }
        assert( p->nSubgrTotal == Vec_IntSize(vOuts) );

        // allocate memory for the priority of roots of each class
Alan Mishchenko committed
351
        p->pScoreMem = ABC_ALLOC( int, Vec_IntSize(vOuts) );
Alan Mishchenko committed
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
        p->nSubgrTotal = 0;
        for ( i = 0; i < 222; i++ )
        {
            p->pScore[i] = p->pScoreMem + p->nSubgrTotal;
            p->nSubgrTotal += p->nSubgr[i];
            for ( k = 0; k < p->nSubgr[i]; k++ )
                p->pScore[i][k] = 0;

        }
        assert( p->nSubgrTotal == Vec_IntSize(vOuts) );
    }
    else
    {
        int Counter = 0;
        // allocate memory for the priority of roots of each class
Alan Mishchenko committed
367
        p->pPriosMem = ABC_ALLOC( int, Vec_IntSize(vOuts) );
Alan Mishchenko committed
368 369 370 371 372 373 374 375 376 377 378 379 380
        p->nSubgrTotal = 0;
        for ( i = 0; i < 222; i++ )
        {
            p->pPrios[i] = p->pPriosMem + p->nSubgrTotal;
            p->nSubgrTotal += p->nSubgr[i];
            for ( k = 0; k < p->nSubgr[i]; k++ )
                p->pPrios[i][k] = Vec_IntEntry(vPrios, Counter++);

        }
        assert( p->nSubgrTotal == Vec_IntSize(vOuts) );
        assert( Counter == Vec_IntSize(vPrios) );
    }

Alan Mishchenko committed
381
    // create traversal IDs
Alan Mishchenko committed
382
    for ( i = 0; i < p->iObj; i++ )
Alan Mishchenko committed
383
        Dar_LibObj(p, i)->Num = 0xff;
Alan Mishchenko committed
384 385 386
    // count nodes in each class
    for ( i = 0; i < 222; i++ )
        for ( k = 0; k < p->nSubgr[i]; k++ )
Alan Mishchenko committed
387
            Dar_LibSetup_rec( p, Dar_LibObj(p, p->pSubgr[i][k]), i, 0 );
Alan Mishchenko committed
388
    // count the total number of nodes
Alan Mishchenko committed
389
    p->nNodesTotal = 0;
Alan Mishchenko committed
390
    for ( i = 0; i < 222; i++ )
Alan Mishchenko committed
391
        p->nNodesTotal += p->nNodes[i];
Alan Mishchenko committed
392
    // allocate memory for the nodes of each class
Alan Mishchenko committed
393 394
    p->pNodesMem = ABC_ALLOC( int, p->nNodesTotal );
    p->pNodes0Mem = ABC_ALLOC( int, p->nNodesTotal );
Alan Mishchenko committed
395
    p->nNodesTotal = 0;
Alan Mishchenko committed
396 397
    for ( i = 0; i < 222; i++ )
    {
Alan Mishchenko committed
398 399 400
        p->pNodes[i] = p->pNodesMem + p->nNodesTotal;
        p->pNodes0[i] = p->pNodes0Mem + p->nNodesTotal;
        p->nNodesTotal += p->nNodes[i];
Alan Mishchenko committed
401 402
        p->nNodes[i] = 0;
    }
Alan Mishchenko committed
403 404 405
    // create traversal IDs
    for ( i = 0; i < p->iObj; i++ )
        Dar_LibObj(p, i)->Num = 0xff;
Alan Mishchenko committed
406
    // add the nodes to storage
Alan Mishchenko committed
407
    nNodesTotal = 0;
Alan Mishchenko committed
408
    for ( i = 0; i < 222; i++ )
Alan Mishchenko committed
409 410
    {
         for ( k = 0; k < p->nSubgr[i]; k++ )
Alan Mishchenko committed
411
            Dar_LibSetup_rec( p, Dar_LibObj(p, p->pSubgr[i][k]), i, 1 );
Alan Mishchenko committed
412
         nNodesTotal += p->nNodes[i];
Alan Mishchenko committed
413
//printf( "Class %3d : Subgraphs = %4d. Nodes = %5d.\n", i, p->nSubgr[i], p->nNodes[i] );
Alan Mishchenko committed
414
    }
Alan Mishchenko committed
415
    assert( nNodesTotal == p->nNodesTotal );
Alan Mishchenko committed
416 417 418
     // prepare the number of the PI nodes
    for ( i = 0; i < 4; i++ )
        Dar_LibObj(p, i)->Num = i;
Alan Mishchenko committed
419 420 421 422
}

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

Alan Mishchenko committed
423 424 425 426 427 428 429 430 431 432 433 434 435
  Synopsis    [Starts the library.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Dar_LibCreateData( Dar_Lib_t * p, int nDatas )
{
    if ( p->nDatas == nDatas )
        return;
Alan Mishchenko committed
436
    ABC_FREE( p->pDatas );
Alan Mishchenko committed
437 438
    // allocate datas
    p->nDatas = nDatas;
Alan Mishchenko committed
439
    p->pDatas = ABC_ALLOC( Dar_LibDat_t, nDatas );
Alan Mishchenko committed
440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498
    memset( p->pDatas, 0, sizeof(Dar_LibDat_t) * nDatas );
}

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

  Synopsis    [Adds one AND to the library.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Dar_LibSetup0_rec( Dar_Lib_t * p, Dar_LibObj_t * pObj, int Class, int fCollect )
{
    if ( pObj->fTerm || (int)pObj->Num == Class )
        return;
    pObj->Num = Class;
    Dar_LibSetup0_rec( p, Dar_LibObj(p, pObj->Fan0), Class, fCollect );
    Dar_LibSetup0_rec( p, Dar_LibObj(p, pObj->Fan1), Class, fCollect );
    if ( fCollect )
        p->pNodes0[Class][ p->nNodes0[Class]++ ] = pObj-p->pObjs;
    else
        p->nNodes0[Class]++;
}

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

  Synopsis    [Starts the library.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Dar_LibPrepare( int nSubgraphs )
{
    Dar_Lib_t * p = s_DarLib;
    int i, k, nNodes0Total;
    if ( p->nSubgraphs == nSubgraphs )
        return;

    // favor special classes:
    //  1 : F = (!d*!c*!b*!a)
    //  4 : F = (!d*!c*!(b*a))
    // 12 : F = (!d*!(c*!(!b*!a)))
    // 20 : F = (!d*!(c*b*a))

    // set the subgraph counters 
    p->nSubgr0Total = 0;
    for ( i = 0; i < 222; i++ )
    {
//        if ( i == 1 || i == 4 || i == 12 || i == 20 ) // special classes 
        if ( i == 1 ) // special classes 
            p->nSubgr0[i] = p->nSubgr[i];
        else
499
            p->nSubgr0[i] = Abc_MinInt( p->nSubgr[i], nSubgraphs );
Alan Mishchenko committed
500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520
        p->nSubgr0Total += p->nSubgr0[i];
        for ( k = 0; k < p->nSubgr0[i]; k++ )
            p->pSubgr0[i][k] = p->pSubgr[i][ p->pPrios[i][k] ];
    }

    // count the number of nodes
    // clean node counters
    for ( i = 0; i < 222; i++ )
        p->nNodes0[i] = 0;
    // create traversal IDs
    for ( i = 0; i < p->iObj; i++ )
        Dar_LibObj(p, i)->Num = 0xff;
    // count nodes in each class
    // count the total number of nodes and the largest class
    p->nNodes0Total = 0;
    p->nNodes0Max = 0;
    for ( i = 0; i < 222; i++ )
    {
        for ( k = 0; k < p->nSubgr0[i]; k++ )
            Dar_LibSetup0_rec( p, Dar_LibObj(p, p->pSubgr0[i][k]), i, 0 );
        p->nNodes0Total += p->nNodes0[i];
521
        p->nNodes0Max = Abc_MaxInt( p->nNodes0Max, p->nNodes0[i] );
Alan Mishchenko committed
522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549
    }

    // clean node counters
    for ( i = 0; i < 222; i++ )
        p->nNodes0[i] = 0;
    // create traversal IDs
    for ( i = 0; i < p->iObj; i++ )
        Dar_LibObj(p, i)->Num = 0xff;
    // add the nodes to storage
    nNodes0Total = 0;
    for ( i = 0; i < 222; i++ )
    {
        for ( k = 0; k < p->nSubgr0[i]; k++ )
            Dar_LibSetup0_rec( p, Dar_LibObj(p, p->pSubgr0[i][k]), i, 1 );
         nNodes0Total += p->nNodes0[i];
    }
    assert( nNodes0Total == p->nNodes0Total );
     // prepare the number of the PI nodes
    for ( i = 0; i < 4; i++ )
        Dar_LibObj(p, i)->Num = i;

    // realloc the datas
    Dar_LibCreateData( p, p->nNodes0Max + 32 ); 
    // allocated more because Dar_LibBuildBest() sometimes requires more entries
}

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

Alan Mishchenko committed
550 551 552 553 554 555 556 557 558 559 560
  Synopsis    [Reads library from array.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Dar_Lib_t * Dar_LibRead()
{
Alan Mishchenko committed
561
    Vec_Int_t * vObjs, * vOuts, * vPrios;
Alan Mishchenko committed
562 563 564
    Dar_Lib_t * p;
    int i;
    // read nodes and outputs
Alan Mishchenko committed
565 566 567
    vObjs  = Dar_LibReadNodes();
    vOuts  = Dar_LibReadOuts();
    vPrios = Dar_LibReadPrios();
Alan Mishchenko committed
568
    // create library
Alan Mishchenko committed
569
    p = Dar_LibAlloc( Vec_IntSize(vObjs)/2 + 4 );
Alan Mishchenko committed
570 571 572 573 574
    // create nodes
    for ( i = 0; i < vObjs->nSize; i += 2 )
        Dar_LibAddNode( p, vObjs->pArray[i] >> 1, vObjs->pArray[i+1] >> 1,
            vObjs->pArray[i] & 1, vObjs->pArray[i+1] & 1 );
    // create outputs
Alan Mishchenko committed
575
    Dar_LibSetup( p, vOuts, vPrios );
Alan Mishchenko committed
576 577
    Vec_IntFree( vObjs );
    Vec_IntFree( vOuts );
Alan Mishchenko committed
578
    Vec_IntFree( vPrios );
Alan Mishchenko committed
579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594
    return p;
}

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

  Synopsis    [Starts the library.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Dar_LibStart()
{
595
//    clock_t clk = clock();
Alan Mishchenko committed
596 597
    assert( s_DarLib == NULL );
    s_DarLib = Dar_LibRead();
Alan Mishchenko committed
598
//    printf( "The 4-input library started with %d nodes and %d subgraphs. ", s_DarLib->nObjs - 4, s_DarLib->nSubgrTotal );
Alan Mishchenko committed
599
//    ABC_PRT( "Time", clock() - clk );
Alan Mishchenko committed
600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619
}

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

  Synopsis    [Stops the library.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Dar_LibStop()
{
    assert( s_DarLib != NULL );
    Dar_LibFree( s_DarLib );
    s_DarLib = NULL;
}

Alan Mishchenko committed
620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690
/**Function*************************************************************

  Synopsis    [Updates the score of the class and adjusts the priority of this class.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Dar_LibIncrementScore( int Class, int Out, int Gain )
{
    int * pPrios = s_DarLib->pPrios[Class];  // pPrios[i] = Out
    int * pPlace = s_DarLib->pPlace[Class];  // pPlace[Out] = i
    int * pScore = s_DarLib->pScore[Class];  // score of Out
    int Out2;
    assert( Class >= 0 && Class < 222 );
    assert( Out >= 0 && Out < s_DarLib->nSubgr[Class] );
    assert( pPlace[pPrios[Out]] == Out );
    // increment the score
    pScore[Out] += Gain;
    // move the out in the order 
    while ( pPlace[Out] > 0 && pScore[Out] > pScore[ pPrios[pPlace[Out]-1] ] )
    {
        // get the previous output in the priority list
        Out2 = pPrios[pPlace[Out]-1];
        // swap Out and Out2
        pPlace[Out]--;
        pPlace[Out2]++;
        pPrios[pPlace[Out]] = Out;
        pPrios[pPlace[Out2]] = Out2;
    }
}

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

  Synopsis    [Prints out the priorities into the file.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Dar_LibDumpPriorities()
{
    int i, k, Out, Out2, Counter = 0, Printed = 0;
    printf( "\nOutput priorities (total = %d):\n", s_DarLib->nSubgrTotal );
    for ( i = 0; i < 222; i++ )
    {
//        printf( "Class%d: ", i );
        for ( k = 0; k < s_DarLib->nSubgr[i]; k++ )
        {
            Out = s_DarLib->pPrios[i][k];
            Out2 = k == 0 ? Out : s_DarLib->pPrios[i][k-1];
            assert( s_DarLib->pScore[i][Out2] >= s_DarLib->pScore[i][Out] );
//            printf( "%d(%d), ", Out, s_DarLib->pScore[i][Out] );
            printf( "%d, ", Out );
            Printed++;
            if ( ++Counter == 15 )
            {
                printf( "\n" );
                Counter = 0;
            }
        }
    }
    printf( "\n" );
    assert( Printed == s_DarLib->nSubgrTotal );
}
Alan Mishchenko committed
691 692 693 694 695 696 697 698 699 700 701 702 703 704 705


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

  Synopsis    [Matches the cut with its canonical form.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Dar_LibCutMatch( Dar_Man_t * p, Dar_Cut_t * pCut )
{
Alan Mishchenko committed
706
    Aig_Obj_t * pFanin;
Alan Mishchenko committed
707 708 709 710 711 712
    unsigned uPhase;
    char * pPerm;
    int i;
    assert( pCut->nLeaves == 4 );
    // get the fanin permutation
    uPhase = s_DarLib->pPhases[pCut->uTruth];
Alan Mishchenko committed
713
    pPerm = s_DarLib->pPerms4[ (int)s_DarLib->pPerms[pCut->uTruth] ];
Alan Mishchenko committed
714 715 716
    // collect fanins with the corresponding permutation/phase
    for ( i = 0; i < (int)pCut->nLeaves; i++ )
    {
Alan Mishchenko committed
717
        pFanin = Aig_ManObj( p->pAig, pCut->pLeaves[ (int)pPerm[i] ] );
Alan Mishchenko committed
718 719 720 721 722
        if ( pFanin == NULL )
        {
            p->nCutsBad++;
            return 0;
        }
Alan Mishchenko committed
723
        pFanin = Aig_NotCond(pFanin, ((uPhase >> i) & 1) );
Alan Mishchenko committed
724
        s_DarLib->pDatas[i].pFunc = pFanin;
Alan Mishchenko committed
725
        s_DarLib->pDatas[i].Level = Aig_Regular(pFanin)->Level;
Alan Mishchenko committed
726 727 728
        // copy the propability of node being one
        if ( p->pPars->fPower )
        {
729
            float Prob = Abc_Int2Float( Vec_IntEntry( p->pAig->vProbs, Aig_ObjId(Aig_Regular(pFanin)) ) );
Alan Mishchenko committed
730 731
            s_DarLib->pDatas[i].dProb = Aig_IsComplement(pFanin)? 1.0-Prob : Prob;
        }
Alan Mishchenko committed
732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749
    }
    p->nCutsGood++;
    return 1;
}



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

  Synopsis    [Marks the MFFC of the node.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
750
int Dar_LibCutMarkMffc( Aig_Man_t * p, Aig_Obj_t * pRoot, int nLeaves, float * pPower )
Alan Mishchenko committed
751
{
Alan Mishchenko committed
752
    int i, nNodes;
Alan Mishchenko committed
753
    // mark the cut leaves
Alan Mishchenko committed
754 755
    for ( i = 0; i < nLeaves; i++ )
        Aig_Regular(s_DarLib->pDatas[i].pFunc)->nRefs++;
Alan Mishchenko committed
756
    // label MFFC with current ID
Alan Mishchenko committed
757
    nNodes = Aig_NodeMffcLabel( p, pRoot, pPower );
Alan Mishchenko committed
758
    // unmark the cut leaves
Alan Mishchenko committed
759 760
    for ( i = 0; i < nLeaves; i++ )
        Aig_Regular(s_DarLib->pDatas[i].pFunc)->nRefs--;
Alan Mishchenko committed
761
    return nNodes;
Alan Mishchenko committed
762 763
}

Alan Mishchenko committed
764 765 766 767 768 769 770 771 772 773 774 775 776 777 778
/**Function*************************************************************

  Synopsis    [Evaluates one cut.]

  Description [Returns the best gain.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Dar_LibObjPrint_rec( Dar_LibObj_t * pObj )
{
    if ( pObj->fTerm )
    {
Alan Mishchenko committed
779
        printf( "%c", 'a' + (int)(pObj - s_DarLib->pObjs) );
Alan Mishchenko committed
780 781 782 783 784 785 786 787 788 789 790 791
        return;
    }
    printf( "(" );
    Dar_LibObjPrint_rec( Dar_LibObj(s_DarLib, pObj->Fan0) );
    if ( pObj->fCompl0 )
        printf( "\'" );
    Dar_LibObjPrint_rec( Dar_LibObj(s_DarLib, pObj->Fan1) );
    if ( pObj->fCompl0 )
        printf( "\'" );
    printf( ")" );
}

Alan Mishchenko committed
792 793 794 795 796 797 798 799 800 801 802 803

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

  Synopsis    [Assigns numbers to the nodes of one class.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
804
void Dar_LibEvalAssignNums( Dar_Man_t * p, int Class, Aig_Obj_t * pRoot )
Alan Mishchenko committed
805 806
{
    Dar_LibObj_t * pObj;
Alan Mishchenko committed
807
    Dar_LibDat_t * pData, * pData0, * pData1;
Alan Mishchenko committed
808
    Aig_Obj_t * pFanin0, * pFanin1;
Alan Mishchenko committed
809
    int i;
Alan Mishchenko committed
810
    for ( i = 0; i < s_DarLib->nNodes0[Class]; i++ )
Alan Mishchenko committed
811
    {
Alan Mishchenko committed
812
        // get one class node, assign its temporary number and set its data
Alan Mishchenko committed
813
        pObj = Dar_LibObj(s_DarLib, s_DarLib->pNodes0[Class][i]);
Alan Mishchenko committed
814
        pObj->Num = 4 + i;
Alan Mishchenko committed
815
        assert( (int)pObj->Num < s_DarLib->nNodes0Max + 4 );
Alan Mishchenko committed
816
        pData = s_DarLib->pDatas + pObj->Num;
Alan Mishchenko committed
817
        pData->fMffc = 0;
Alan Mishchenko committed
818 819
        pData->pFunc = NULL;
        pData->TravId = 0xFFFF;
Alan Mishchenko committed
820

Alan Mishchenko committed
821
        // explore the fanins
Alan Mishchenko committed
822 823
        assert( (int)Dar_LibObj(s_DarLib, pObj->Fan0)->Num < s_DarLib->nNodes0Max + 4 );
        assert( (int)Dar_LibObj(s_DarLib, pObj->Fan1)->Num < s_DarLib->nNodes0Max + 4 );
Alan Mishchenko committed
824 825
        pData0 = s_DarLib->pDatas + Dar_LibObj(s_DarLib, pObj->Fan0)->Num;
        pData1 = s_DarLib->pDatas + Dar_LibObj(s_DarLib, pObj->Fan1)->Num;
826
        pData->Level = 1 + Abc_MaxInt(pData0->Level, pData1->Level);
Alan Mishchenko committed
827
        if ( pData0->pFunc == NULL || pData1->pFunc == NULL )
Alan Mishchenko committed
828
            continue;
Alan Mishchenko committed
829 830
        pFanin0 = Aig_NotCond( pData0->pFunc, pObj->fCompl0 );
        pFanin1 = Aig_NotCond( pData1->pFunc, pObj->fCompl1 );
Alan Mishchenko committed
831 832
        if ( Aig_Regular(pFanin0) == pRoot || Aig_Regular(pFanin1) == pRoot )
            continue;
Alan Mishchenko committed
833
        pData->pFunc = Aig_TableLookupTwo( p->pAig, pFanin0, pFanin1 );
Alan Mishchenko committed
834 835 836 837 838
        if ( pData->pFunc )
        {
            // update the level to be more accurate
            pData->Level = Aig_Regular(pData->pFunc)->Level;
            // mark the node if it is part of MFFC
Alan Mishchenko committed
839
            pData->fMffc = Aig_ObjIsTravIdCurrent(p->pAig, Aig_Regular(pData->pFunc));
Alan Mishchenko committed
840 841 842
            // assign the probability
            if ( p->pPars->fPower )
            {
843
                float Prob = Abc_Int2Float( Vec_IntEntry( p->pAig->vProbs, Aig_ObjId(Aig_Regular(pData->pFunc)) ) );
Alan Mishchenko committed
844 845
                pData->dProb = Aig_IsComplement(pData->pFunc)? 1.0-Prob : Prob;
            }
Alan Mishchenko committed
846
        }
Alan Mishchenko committed
847 848 849 850 851 852 853 854 855 856 857 858 859 860
    }
}

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

  Synopsis    [Evaluates one cut.]

  Description [Returns the best gain.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
861
int Dar_LibEval_rec( Dar_LibObj_t * pObj, int Out, int nNodesSaved, int Required, float * pPower )
Alan Mishchenko committed
862 863
{
    Dar_LibDat_t * pData;
Alan Mishchenko committed
864
    float Power0, Power1;
Alan Mishchenko committed
865
    int Area;
Alan Mishchenko committed
866 867
    if ( pPower )
        *pPower = (float)0.0;
Alan Mishchenko committed
868 869 870 871
    pData = s_DarLib->pDatas + pObj->Num;
    if ( pData->TravId == Out )
        return 0;
    pData->TravId = Out;
Alan Mishchenko committed
872
    if ( pObj->fTerm )
Alan Mishchenko committed
873 874 875
    {
        if ( pPower )
            *pPower = pData->dProb;
Alan Mishchenko committed
876
        return 0;
Alan Mishchenko committed
877
    }
Alan Mishchenko committed
878
    assert( pObj->Num > 3 );
Alan Mishchenko committed
879 880
    if ( pData->Level > Required )
        return 0xff;
Alan Mishchenko committed
881
    if ( pData->pFunc && !pData->fMffc )
Alan Mishchenko committed
882 883 884
    {
        if ( pPower )
            *pPower = pData->dProb;
Alan Mishchenko committed
885
        return 0;
Alan Mishchenko committed
886
    }
Alan Mishchenko committed
887 888
    // this is a new node - get a bound on the area of its branches
    nNodesSaved--;
Alan Mishchenko committed
889
    Area = Dar_LibEval_rec( Dar_LibObj(s_DarLib, pObj->Fan0), Out, nNodesSaved, Required+1, pPower? &Power0 : NULL );
Alan Mishchenko committed
890
    if ( Area > nNodesSaved )
Alan Mishchenko committed
891
        return 0xff;
Alan Mishchenko committed
892
    Area += Dar_LibEval_rec( Dar_LibObj(s_DarLib, pObj->Fan1), Out, nNodesSaved, Required+1, pPower? &Power1 : NULL );
Alan Mishchenko committed
893
    if ( Area > nNodesSaved )
Alan Mishchenko committed
894
        return 0xff;
Alan Mishchenko committed
895 896 897 898 899 900 901 902 903
    if ( pPower )
    {
        Dar_LibDat_t * pData0 = s_DarLib->pDatas + Dar_LibObj(s_DarLib, pObj->Fan0)->Num;
        Dar_LibDat_t * pData1 = s_DarLib->pDatas + Dar_LibObj(s_DarLib, pObj->Fan1)->Num;
        pData->dProb = (pObj->fCompl0? 1.0 - pData0->dProb : pData0->dProb)*
                       (pObj->fCompl1? 1.0 - pData1->dProb : pData1->dProb);
        *pPower = Power0 + 2.0 * pData0->dProb * (1.0 - pData0->dProb) +
                  Power1 + 2.0 * pData1->dProb * (1.0 - pData1->dProb);
    }
Alan Mishchenko committed
904
    return Area + 1;
Alan Mishchenko committed
905 906 907 908 909 910 911 912 913 914 915 916 917
}

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

  Synopsis    [Evaluates one cut.]

  Description [Returns the best gain.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
918
void Dar_LibEval( Dar_Man_t * p, Aig_Obj_t * pRoot, Dar_Cut_t * pCut, int Required, int * pnMffcSize )
Alan Mishchenko committed
919
{
Alan Mishchenko committed
920
    int fTraining = 0;
Alan Mishchenko committed
921
    float PowerSaved, PowerAdded;
Alan Mishchenko committed
922
    Dar_LibObj_t * pObj;
923 924
    int Out, k, Class, nNodesSaved, nNodesAdded, nNodesGained;
    clock_t clk = clock();
Alan Mishchenko committed
925 926
    if ( pCut->nLeaves != 4 )
        return;
Alan Mishchenko committed
927
    // check if the cut exits and assigns leaves and their levels
Alan Mishchenko committed
928
    if ( !Dar_LibCutMatch(p, pCut) )
Alan Mishchenko committed
929
        return;
Alan Mishchenko committed
930
    // mark MFFC of the node
Alan Mishchenko committed
931
    nNodesSaved = Dar_LibCutMarkMffc( p->pAig, pRoot, pCut->nLeaves, p->pPars->fPower? &PowerSaved : NULL );
Alan Mishchenko committed
932 933
    // evaluate the cut
    Class = s_DarLib->pMap[pCut->uTruth];
Alan Mishchenko committed
934
    Dar_LibEvalAssignNums( p, Class, pRoot );
Alan Mishchenko committed
935
    // profile outputs by their savings
Alan Mishchenko committed
936 937 938
    p->nTotalSubgs += s_DarLib->nSubgr0[Class];
    p->ClassSubgs[Class] += s_DarLib->nSubgr0[Class];
    for ( Out = 0; Out < s_DarLib->nSubgr0[Class]; Out++ )
Alan Mishchenko committed
939
    {
Alan Mishchenko committed
940 941 942
        pObj = Dar_LibObj(s_DarLib, s_DarLib->pSubgr0[Class][Out]);
        if ( Aig_Regular(s_DarLib->pDatas[pObj->Num].pFunc) == pRoot )
            continue;
Alan Mishchenko committed
943
        nNodesAdded = Dar_LibEval_rec( pObj, Out, nNodesSaved - !p->pPars->fUseZeros, Required, p->pPars->fPower? &PowerAdded : NULL );
Alan Mishchenko committed
944
        nNodesGained = nNodesSaved - nNodesAdded;
Alan Mishchenko committed
945 946
        if ( p->pPars->fPower && PowerSaved < PowerAdded )
            continue;
Alan Mishchenko committed
947 948
        if ( fTraining && nNodesGained >= 0 )
            Dar_LibIncrementScore( Class, Out, nNodesGained + 1 );
Alan Mishchenko committed
949
        if ( nNodesGained < 0 || (nNodesGained == 0 && !p->pPars->fUseZeros) )
Alan Mishchenko committed
950
            continue;
Alan Mishchenko committed
951 952
        if ( nNodesGained <  p->GainBest || 
            (nNodesGained == p->GainBest && s_DarLib->pDatas[pObj->Num].Level >= p->LevelBest) )
Alan Mishchenko committed
953 954 955
            continue;
        // remember this possibility
        Vec_PtrClear( p->vLeavesBest );
Alan Mishchenko committed
956
        for ( k = 0; k < (int)pCut->nLeaves; k++ )
Alan Mishchenko committed
957
            Vec_PtrPush( p->vLeavesBest, s_DarLib->pDatas[k].pFunc );
Alan Mishchenko committed
958
        p->OutBest    = s_DarLib->pSubgr0[Class][Out];
Alan Mishchenko committed
959
        p->OutNumBest = Out;
Alan Mishchenko committed
960 961 962
        p->LevelBest  = s_DarLib->pDatas[pObj->Num].Level;
        p->GainBest   = nNodesGained;
        p->ClassBest  = Class;
Alan Mishchenko committed
963
        assert( p->LevelBest <= Required );
964
        *pnMffcSize   = nNodesSaved;
Alan Mishchenko committed
965
    }
Alan Mishchenko committed
966 967 968
clk = clock() - clk;
p->ClassTimes[Class] += clk;
p->timeEval += clk;
Alan Mishchenko committed
969 970 971 972
}

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

Alan Mishchenko committed
973
  Synopsis    [Clears the fields of the nodes used in this cut.]
Alan Mishchenko committed
974 975 976 977 978 979 980 981

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
982
void Dar_LibBuildClear_rec( Dar_LibObj_t * pObj, int * pCounter )
Alan Mishchenko committed
983 984 985
{
    if ( pObj->fTerm )
        return;
Alan Mishchenko committed
986
    pObj->Num = (*pCounter)++;
Alan Mishchenko committed
987
    s_DarLib->pDatas[ pObj->Num ].pFunc = NULL;
Alan Mishchenko committed
988 989
    Dar_LibBuildClear_rec( Dar_LibObj(s_DarLib, pObj->Fan0), pCounter );
    Dar_LibBuildClear_rec( Dar_LibObj(s_DarLib, pObj->Fan1), pCounter );
Alan Mishchenko committed
990 991 992 993 994 995 996 997 998 999 1000 1001 1002
}

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

  Synopsis    [Reconstructs the best cut.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
1003
Aig_Obj_t * Dar_LibBuildBest_rec( Dar_Man_t * p, Dar_LibObj_t * pObj )
Alan Mishchenko committed
1004
{
Alan Mishchenko committed
1005
    Aig_Obj_t * pFanin0, * pFanin1;
Alan Mishchenko committed
1006 1007 1008 1009 1010
    Dar_LibDat_t * pData = s_DarLib->pDatas + pObj->Num;
    if ( pData->pFunc )
        return pData->pFunc;
    pFanin0 = Dar_LibBuildBest_rec( p, Dar_LibObj(s_DarLib, pObj->Fan0) );
    pFanin1 = Dar_LibBuildBest_rec( p, Dar_LibObj(s_DarLib, pObj->Fan1) );
Alan Mishchenko committed
1011 1012 1013
    pFanin0 = Aig_NotCond( pFanin0, pObj->fCompl0 );
    pFanin1 = Aig_NotCond( pFanin1, pObj->fCompl1 );
    pData->pFunc = Aig_And( p->pAig, pFanin0, pFanin1 );
Alan Mishchenko committed
1014
//    assert( pData->Level == (int)Aig_Regular(pData->pFunc)->Level );
Alan Mishchenko committed
1015
    return pData->pFunc;
Alan Mishchenko committed
1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028
}

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

  Synopsis    [Reconstructs the best cut.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
1029
Aig_Obj_t * Dar_LibBuildBest( Dar_Man_t * p )
Alan Mishchenko committed
1030
{
Alan Mishchenko committed
1031
    int i, Counter = 4;
Alan Mishchenko committed
1032
    for ( i = 0; i < Vec_PtrSize(p->vLeavesBest); i++ )
1033
        s_DarLib->pDatas[i].pFunc = (Aig_Obj_t *)Vec_PtrEntry( p->vLeavesBest, i );
Alan Mishchenko committed
1034
    Dar_LibBuildClear_rec( Dar_LibObj(s_DarLib, p->OutBest), &Counter );
Alan Mishchenko committed
1035 1036 1037
    return Dar_LibBuildBest_rec( p, Dar_LibObj(s_DarLib, p->OutBest) );
}

Alan Mishchenko committed
1038

1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068




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

  Synopsis    [Matches the cut with its canonical form.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Dar2_LibCutMatch( Gia_Man_t * p, Vec_Int_t * vCutLits, unsigned uTruth )
{
    unsigned uPhase;
    char * pPerm;
    int i;
    assert( Vec_IntSize(vCutLits) == 4 );
    // get the fanin permutation
    uPhase = s_DarLib->pPhases[uTruth];
    pPerm  = s_DarLib->pPerms4[ (int)s_DarLib->pPerms[uTruth] ];
    // collect fanins with the corresponding permutation/phase
    for ( i = 0; i < Vec_IntSize(vCutLits); i++ )
    {
//        pFanin = Gia_ManObj( p, pCut->pLeaves[ (int)pPerm[i] ] );
//        pFanin = Gia_ManObj( p, Vec_IntEntry( vCutLits, (int)pPerm[i] ) );
//        pFanin = Gia_ObjFromLit( p, Vec_IntEntry( vCutLits, (int)pPerm[i] ) );
1069
        s_DarLib->pDatas[i].iGunc = Abc_LitNotCond( Vec_IntEntry(vCutLits, (int)pPerm[i]), ((uPhase >> i) & 1) );
1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106
        s_DarLib->pDatas[i].Level = Gia_ObjLevel( p, Gia_Regular(Gia_ObjFromLit(p, s_DarLib->pDatas[i].iGunc)) );
    }
    return 1;
}

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

  Synopsis    [Assigns numbers to the nodes of one class.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Dar2_LibEvalAssignNums( Gia_Man_t * p, int Class )
{
    Dar_LibObj_t * pObj;
    Dar_LibDat_t * pData, * pData0, * pData1;
    int iFanin0, iFanin1, i, iLit;
    for ( i = 0; i < s_DarLib->nNodes0[Class]; i++ )
    {
        // get one class node, assign its temporary number and set its data
        pObj = Dar_LibObj(s_DarLib, s_DarLib->pNodes0[Class][i]);
        pObj->Num = 4 + i;
        assert( (int)pObj->Num < s_DarLib->nNodes0Max + 4 );
        pData = s_DarLib->pDatas + pObj->Num;
        pData->fMffc = 0;
        pData->iGunc = -1;
        pData->TravId = 0xFFFF;

        // explore the fanins
        assert( (int)Dar_LibObj(s_DarLib, pObj->Fan0)->Num < s_DarLib->nNodes0Max + 4 );
        assert( (int)Dar_LibObj(s_DarLib, pObj->Fan1)->Num < s_DarLib->nNodes0Max + 4 );
        pData0 = s_DarLib->pDatas + Dar_LibObj(s_DarLib, pObj->Fan0)->Num;
        pData1 = s_DarLib->pDatas + Dar_LibObj(s_DarLib, pObj->Fan1)->Num;
1107
        pData->Level = 1 + Abc_MaxInt(pData0->Level, pData1->Level);
1108 1109
        if ( pData0->iGunc == -1 || pData1->iGunc == -1 )
            continue;
1110 1111
        iFanin0 = Abc_LitNotCond( pData0->iGunc, pObj->fCompl0 );
        iFanin1 = Abc_LitNotCond( pData1->iGunc, pObj->fCompl1 );
1112
        // compute the resulting literal
1113
        if ( iFanin0 == 0 || iFanin1 == 0 || iFanin0 == Abc_LitNot(iFanin1) )
1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188
            iLit = 0;
        else if ( iFanin0 == 1 || iFanin0 == iFanin1 )
            iLit = iFanin1;
        else if ( iFanin1 == 1 )
            iLit = iFanin0;
        else
        {
            iLit = Gia_ManHashLookup( p, Gia_ObjFromLit(p, iFanin0), Gia_ObjFromLit(p, iFanin1) );
            if ( iLit == 0 )
                iLit = -1;
        }
        pData->iGunc = iLit;
        if ( pData->iGunc >= 0 )
        {
            // update the level to be more accurate
            pData->Level = Gia_ObjLevel( p, Gia_Regular(Gia_ObjFromLit(p, pData->iGunc)) );
            // mark the node if it is part of MFFC
//            pData->fMffc = Gia_ObjIsTravIdCurrentArray(p, Gia_Regular(pData->pGunc));
        }
    }
}

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

  Synopsis    [Evaluates one cut.]

  Description [Returns the best gain.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Dar2_LibEval_rec( Dar_LibObj_t * pObj, int Out )
{
    Dar_LibDat_t * pData;
    int Area;
    pData = s_DarLib->pDatas + pObj->Num;
    if ( pData->TravId == Out )
        return 0;
    pData->TravId = Out;
    if ( pObj->fTerm )
        return 0;
    assert( pObj->Num > 3 );
    if ( pData->iGunc >= 0 )//&& !pData->fMffc )
        return 0;
    // this is a new node - get a bound on the area of its branches
//    nNodesSaved--;
    Area = Dar2_LibEval_rec( Dar_LibObj(s_DarLib, pObj->Fan0), Out );
//    if ( Area > nNodesSaved )
//        return 0xff;
    Area += Dar2_LibEval_rec( Dar_LibObj(s_DarLib, pObj->Fan1), Out );
//    if ( Area > nNodesSaved )
//        return 0xff;
    return Area + 1;
}

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

  Synopsis    [Evaluates one cut.]

  Description [Returns the best gain.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Dar2_LibEval( Gia_Man_t * p, Vec_Int_t * vCutLits, unsigned uTruth, int fKeepLevel, Vec_Int_t * vLeavesBest2 )
{
    int p_OutBest    = -1;
    int p_OutNumBest = -1;
    int p_LevelBest  =  1000000;
    int p_GainBest   = -1000000;
    int p_ClassBest  = -1;
1189
//    int fTraining    =  0;
1190
    Dar_LibObj_t * pObj;
1191
    int Out, k, Class, nNodesSaved, nNodesAdded, nNodesGained;
1192
//    clock_t clk = clock();
1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284
    assert( Vec_IntSize(vCutLits) == 4 );
    assert( (uTruth >> 16) == 0 );
    // check if the cut exits and assigns leaves and their levels
    if ( !Dar2_LibCutMatch(p, vCutLits, uTruth) )
        return -1;
    // mark MFFC of the node
//    nNodesSaved = Dar2_LibCutMarkMffc( p->pAig, pRoot, pCut->nLeaves, p->pPars->fPower? &PowerSaved : NULL );
    nNodesSaved = 0;
    // evaluate the cut
    Class = s_DarLib->pMap[uTruth];
    Dar2_LibEvalAssignNums( p, Class );
    // profile outputs by their savings
//    p->nTotalSubgs += s_DarLib->nSubgr0[Class];
//    p->ClassSubgs[Class] += s_DarLib->nSubgr0[Class];
    for ( Out = 0; Out < s_DarLib->nSubgr0[Class]; Out++ )
    {
        pObj = Dar_LibObj(s_DarLib, s_DarLib->pSubgr0[Class][Out]);
//        nNodesAdded = Dar2_LibEval_rec( pObj, Out, nNodesSaved - !p->pPars->fUseZeros, Required, p->pPars->fPower? &PowerAdded : NULL );
        nNodesAdded = Dar2_LibEval_rec( pObj, Out );
        nNodesGained = nNodesSaved - nNodesAdded;
        if ( fKeepLevel )
        {
            if ( s_DarLib->pDatas[pObj->Num].Level >  p_LevelBest || 
                (s_DarLib->pDatas[pObj->Num].Level == p_LevelBest && nNodesGained <= p_GainBest) )
                continue;
        }
        else
        {
            if ( nNodesGained <  p_GainBest || 
                (nNodesGained == p_GainBest && s_DarLib->pDatas[pObj->Num].Level >= p_LevelBest) )
                continue;
        }
        // remember this possibility
        Vec_IntClear( vLeavesBest2 );
        for ( k = 0; k < Vec_IntSize(vCutLits); k++ )
            Vec_IntPush( vLeavesBest2, s_DarLib->pDatas[k].iGunc );
        p_OutBest    = s_DarLib->pSubgr0[Class][Out];
        p_OutNumBest = Out;
        p_LevelBest  = s_DarLib->pDatas[pObj->Num].Level;
        p_GainBest   = nNodesGained;
        p_ClassBest  = Class;
//        assert( p_LevelBest <= Required );
    }
//clk = clock() - clk;
//p->ClassTimes[Class] += clk;
//p->timeEval += clk;
    assert( p_OutBest != -1 );
    return p_OutBest;
}

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

  Synopsis    [Clears the fields of the nodes used i this cut.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Dar2_LibBuildClear_rec( Dar_LibObj_t * pObj, int * pCounter )
{
    if ( pObj->fTerm )
        return;
    pObj->Num = (*pCounter)++;
    s_DarLib->pDatas[ pObj->Num ].iGunc = -1;
    Dar2_LibBuildClear_rec( Dar_LibObj(s_DarLib, pObj->Fan0), pCounter );
    Dar2_LibBuildClear_rec( Dar_LibObj(s_DarLib, pObj->Fan1), pCounter );
}

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

  Synopsis    [Reconstructs the best cut.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Dar2_LibBuildBest_rec( Gia_Man_t * p, Dar_LibObj_t * pObj )
{
    Gia_Obj_t * pNode;
    Dar_LibDat_t * pData;
    int iFanin0, iFanin1;
    pData = s_DarLib->pDatas + pObj->Num;
    if ( pData->iGunc >= 0 )
        return pData->iGunc;
    iFanin0 = Dar2_LibBuildBest_rec( p, Dar_LibObj(s_DarLib, pObj->Fan0) );
    iFanin1 = Dar2_LibBuildBest_rec( p, Dar_LibObj(s_DarLib, pObj->Fan1) );
1285 1286
    iFanin0 = Abc_LitNotCond( iFanin0, pObj->fCompl0 );
    iFanin1 = Abc_LitNotCond( iFanin1, pObj->fCompl1 );
1287
    pData->iGunc = Gia_ManHashAnd( p, iFanin0, iFanin1 );
1288
    pNode = Gia_ManObj( p, Abc_Lit2Var(pData->iGunc) );
1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332
    if ( Gia_ObjIsAnd( pNode ) )
        Gia_ObjSetAndLevel( p, pNode );
    Gia_ObjSetPhase( pNode );
    return pData->iGunc;
}

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

  Synopsis    [Reconstructs the best cut.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Dar2_LibBuildBest( Gia_Man_t * p, Vec_Int_t * vLeavesBest2, int OutBest )
{
    int i, iLeaf, Counter = 4;
    assert( Vec_IntSize(vLeavesBest2) == 4 );
    Vec_IntForEachEntry( vLeavesBest2, iLeaf, i )
        s_DarLib->pDatas[i].iGunc = iLeaf;
    Dar2_LibBuildClear_rec( Dar_LibObj(s_DarLib, OutBest), &Counter );
    return Dar2_LibBuildBest_rec( p, Dar_LibObj(s_DarLib, OutBest) );
}

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

  Synopsis    [Evaluate and build the new node.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Dar_LibEvalBuild( Gia_Man_t * p, Vec_Int_t * vCutLits, unsigned uTruth, int fKeepLevel, Vec_Int_t * vLeavesBest2 )
{
    int OutBest = Dar2_LibEval( p, vCutLits, uTruth, fKeepLevel, vLeavesBest2 );
    return Dar2_LibBuildBest( p, vLeavesBest2, OutBest );
}

Alan Mishchenko committed
1333 1334 1335 1336 1337
////////////////////////////////////////////////////////////////////////
///                       END OF FILE                                ///
////////////////////////////////////////////////////////////////////////


1338 1339
ABC_NAMESPACE_IMPL_END