abcRec.c 101 KB
Newer Older
Alan Mishchenko committed
1 2 3 4 5 6 7 8 9 10
/**CFile****************************************************************

  FileName    [abcRec.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [Network and node package.]

  Synopsis    [Record of semi-canonical AIG subgraphs.]

11
  Author      [Allan Yang, Alan Mishchenko]
Alan Mishchenko committed
12
  
13
  Affiliation [Fudan University in Shanghai, UC Berkeley]
Alan Mishchenko committed
14 15 16 17 18 19 20

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

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

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

21 22 23
#include "base/abc/abc.h"
#include "map/if/if.h"
#include "bool/kit/kit.h"
Alan Mishchenko committed
24

25 26
ABC_NAMESPACE_IMPL_START

27
//#define LibOut
Alan Mishchenko committed
28 29 30 31
////////////////////////////////////////////////////////////////////////
///                        DECLARATIONS                              ///
////////////////////////////////////////////////////////////////////////

32
#define  IF_BIG_CHAR 120
Alan Mishchenko committed
33
typedef struct Abc_ManRec_t_ Abc_ManRec_t;
34 35 36
typedef struct Rec_Obj_t_ Rec_Obj_t;

typedef enum {
37 38 39
    REC_ERROR,                  //0: error
    REC_SMALL,                  //1: smaller than
    REC_EQUAL,                  //2: equal with
40
    REC_BIG,                    //3: bigger than    
41
    REC_DOMINANCE,              //4: dominance
42 43 44 45 46
    REC_BEDOMINANCED            //5: be dominated
} Abc_LookUpStatus_t;

struct Rec_Obj_t_
{
47 48 49 50
    Abc_Obj_t* obj;             // the actual structure in the library
    Rec_Obj_t* pNext;           // link to the next structure of the same functional class
    Rec_Obj_t* pCopy;           // link to the next functional class in the same bucket
    int Id;                     // structure's ID
51
    int nFrequency;             // appear times of this functional class among benchmarks
52
    unsigned char cost;         // structure's cost
53 54 55 56
    char* pinToPinDelay;        // structure's pin-to-pin delay
};


Alan Mishchenko committed
57 58
struct Abc_ManRec_t_
{
59 60 61
    Abc_Ntk_t *       pNtk;                     // the record
    Vec_Ptr_t *       vTtElems;                 // the elementary truth tables
    Vec_Ptr_t *       vTtNodes;                 // the node truth tables
62
    Mem_Fixed_t *     pMmTruth;                 // memory manager for truth tables
63 64 65 66
    Rec_Obj_t **      pBins;                    // hash table mapping truth tables into nodes
    int               nBins;                    // the number of allocated bins
    int               nVars;                    // the number of variables
    int               nVarsInit;                // the number of variables requested initially
67
    int               nWords;                   // the number of TT words
68
    int               nCuts;                    // the max number of cuts to use
69 70 71
    Mem_Fixed_t *     pMemObj;                  // memory manager for Rec objects
    int               recObjSize;               // size for one Rec object
    int               fTrim;                    // filter the library or not.
Alan Mishchenko committed
72
    // temporaries
73 74 75 76
    int *             pBytes;                   // temporary storage for minterms
    int *             pMints;                   // temporary storage for minterm counters
    unsigned *        pTemp1;                   // temporary truth table
    unsigned *        pTemp2;                   // temporary truth table
77 78 79 80 81 82 83
    unsigned *        pTempTruth;               // temporary truth table
    char *            pTempDepths;              // temporary depths
    int  *            pTempleaves;              // temporary leaves
    unsigned          tempUsign;
    unsigned          tempNleaves;
    unsigned          currentCost;
    int               currentDelay;
84 85 86 87 88
    Vec_Ptr_t *       vNodes;                   // the temporary nodes
    Vec_Ptr_t *       vTtTemps;                 // the truth tables for the internal nodes of the cut
    Vec_Ptr_t *       vLabels;                  // temporary storage for AIG node labels
    Vec_Str_t *       vCosts;                   // temporary storage for costs
    Vec_Int_t *       vMemory;                  // temporary memory for truth tables
Alan Mishchenko committed
89
    // statistics
90 91
    int               nTried;                   // the number of cuts tried
    int               nFilterSize;              // the number of same structures
92 93
    int               nFilterRedund;            // the number of same structures
    int               nFilterVolume;            // the number of same structures
94 95 96 97 98 99 100 101
    int               nFilterTruth;             // the number of same structures
    int               nFilterError;             // the number of same structures
    int               nFilterSame;              // the number of same structures
    int               nAdded;                   // the number of subgraphs added
    int               nAddedFuncs;              // the number of functions added
    int               nIfMapTried;
    int               nIfMapError;
    int               nTrimed;                  // the number of structures filtered
Alan Mishchenko committed
102
    // rewriting
103
    int               nFunsFound;               // the found functions
104
    int               nFunsNotFound;            // the missing functions
105 106 107
    int               nFunsTried;
    int               nFunsFilteredBysupport;   // the function filtered when rewriting because not all supports are in use.
    int               nFunsDelayComput;         // the times delay computed, just for statistics
108
    int               nNoBetter;                // the number of functions found but no better than the current structures.
109
    // rewriting runtime
110 111 112 113 114 115
    clock_t           timeIfTotal;              // time used on the whole process of rewriting a structure.
    clock_t           timeIfComputDelay;        // time used on the structure's delay computation.
    clock_t           timeIfCanonicize;         // time used on canonicize the function
    clock_t           timeIfDerive;             // time used on derive the final network;
    clock_t           timeIfCopmutCur;          // time used on compute the current structures info
    clock_t           timeIfOther;              // time used on other things
116
    // record runtime
117 118 119 120 121 122 123 124 125 126
    clock_t           timeTrim;                 // the runtime to filter the library
    clock_t           timeCollect;              // the runtime to collect the node of a structure.
    clock_t           timeTruth;                // the runtime to compute truth table.
    clock_t           timeCanon;                // the runtime to canonicize
    clock_t           timeInsert;               // the runtime to insert a structure.
    clock_t           timeBuild;                // the runtime to build a new structure in the library.
    clock_t           timeMerge;                // the runtime to merge libraries;
    clock_t           timeReHash;                // the runtime to resize the hash table.
    clock_t           timeOther;                // the runtime of other
    clock_t           timeTotal;                // the runtime to total.
Alan Mishchenko committed
127 128
};

129

Alan Mishchenko committed
130 131
// the truth table is canonicized in such a way that for (00000) its value is 0

132
static Rec_Obj_t ** Abc_NtkRecTableLookup( Abc_ManRec_t* p, Rec_Obj_t ** pBins, int nBins, unsigned * pTruth, int nVars);
Alan Mishchenko committed
133 134
static int          Abc_NtkRecComputeTruth( Abc_Obj_t * pObj, Vec_Ptr_t * vTtNodes, int nVars );
static int          Abc_NtkRecAddCutCheckCycle_rec( Abc_Obj_t * pRoot, Abc_Obj_t * pObj );
135
static void         Abc_NtkRecAddFromLib( Abc_Ntk_t* pNtk, Abc_Obj_t * pRoot, int nVars );
136
static void         Abc_NtkRecCurrentUnMark_rec(If_Obj_t * pObj);
Alan Mishchenko committed
137 138
static Abc_ManRec_t * s_pMan = NULL;

139 140 141 142
static inline void Abc_ObjSetMax( Abc_Obj_t * pObj, int Value ) { assert( pObj->Level < 0xff ); pObj->Level = (Value << 8) | (pObj->Level & 0xff); }
static inline void Abc_ObjClearMax( Abc_Obj_t * pObj )          { pObj->Level = (pObj->Level & 0xff); }
static inline int  Abc_ObjGetMax( Abc_Obj_t * pObj )            { return (pObj->Level >> 8) & 0xff; }

143 144 145
static inline void Abc_NtkRecFrequencyInc(Rec_Obj_t* entry)     
{   
    // the hit number of this functional class increased
146
    if (entry != NULL && entry->nFrequency < 0x7fffffff)
147 148 149
        entry->nFrequency += 1;
}

Alan Mishchenko committed
150 151 152 153 154 155
////////////////////////////////////////////////////////////////////////
///                     FUNCTION DEFINITIONS                         ///
////////////////////////////////////////////////////////////////////////

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

156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
  Synopsis    [stretch the truthtable to have more input variables.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void If_CutTruthStretch(unsigned* pInOut, int nVarS, int nVarB)
{
    int w, i;
    int step = Kit_TruthWordNum(nVarS);
    int nWords = Kit_TruthWordNum(nVarB);
    assert(step <= nWords);
    if (step == nWords)
        return;
    for (w = 0; w <nWords; w += step)
        for (i = 0; i < step; i++)
175
            pInOut[w + i] = pInOut[i];              
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
}

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

  Synopsis    [Alloc the Rec object from its manger.]

  Description []
               
  SideEffects []

  SeeAlso     []

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

Rec_Obj_t* Rec_ObjAlloc(Abc_ManRec_t* p, Abc_Obj_t* pObj, char* pinToPinDelay, char cost, int nVar)
{
    int i;
    Rec_Obj_t * pRecObj;
    pRecObj = (Rec_Obj_t *)Mem_FixedEntryFetch( p->pMemObj);
    pRecObj->pinToPinDelay = (char*)(pRecObj + 1);
    pRecObj->pNext = NULL;
    pRecObj->pCopy = NULL;
    pRecObj->obj = pObj;
    pRecObj->Id = pObj->Id;
    for (i = 0; i < nVar; i++)
        pRecObj->pinToPinDelay[i] = pinToPinDelay[i];
202 203
    pRecObj->cost = cost;   
    pRecObj->nFrequency = 0;
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
    return pRecObj;
}

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

  Synopsis    [set the property of a Rec object.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Rec_ObjSet(Abc_ManRec_t* p, Rec_Obj_t* pRecObj, Abc_Obj_t* pObj, char* newDelay, unsigned char cost, int nVar)
{
    int i;
    pRecObj->obj = pObj;
    pRecObj->Id = pObj->Id;
    pRecObj->cost = cost;
    for (i = 0; i < nVar; i++)
        pRecObj->pinToPinDelay[i] = newDelay[i];        
}

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

  Synopsis    [Compute the delay of the structure recursively.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
/* int If_CutDelayRecComput_rec(Abc_Obj_t* pObj, Vec_Str_t* vCosts)
 {
241 242 243 244 245 246 247 248
    char Delay0, Delay1, Delay;
    Abc_Obj_t *pFanin0, *pFanin1;
    pObj = Abc_ObjRegular(pObj);
    if (Abc_NodeIsTravIdCurrent(pObj) || pObj->Type == ABC_OBJ_PI)
        return Vec_StrEntry(vCosts,pObj->Id);
    Abc_NodeSetTravIdCurrent(pObj);
    Delay0 = If_CutDelayRecComput_rec(Abc_ObjFanin0(pObj), vCosts);
    Delay1 = If_CutDelayRecComput_rec(Abc_ObjFanin1(pObj), vCosts);
249
    Delay = Abc_MaxInt(Delay0, Delay1) + 1;
250 251
    Vec_StrWriteEntry(vCosts,pObj->Id,Delay);
    return Delay;
252 253 254 255 256 257 258 259 260 261 262 263 264
 }*/

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

  Synopsis    [Compute delay of the structure using pin-to-pin delay.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
265
inline int If_CutComputDelay(If_Man_t* p, Rec_Obj_t* entry, If_Cut_t* pCut, char* pCanonPerm , int nVars)
266 267 268 269 270
{
    If_Obj_t* pLeaf;
    int i, delayTemp, delayMax = -ABC_INFINITY;
    for (i = 0; i < nVars; i++)
    {
271
        pLeaf = If_ManObj(p, (pCut)->pLeaves[(int)pCanonPerm[i]]);
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
        pLeaf = If_Regular(pLeaf);
        delayTemp = entry->pinToPinDelay[i] + If_ObjCutBest(pLeaf)->Delay;
        if(delayTemp > delayMax)
            delayMax = delayTemp;
    }
    // plus each pin's delay with its pin-to-output delay, the biggest one is the delay of the structure.
    return delayMax;
}

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

  Synopsis    [Compute pin-to-pin delay of the structure.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
char If_CutDepthRecComput_rec(Abc_Obj_t* pObj, int iLeaf)
{
    char Depth0, Depth1, Depth;
    pObj = Abc_ObjRegular(pObj);
    if(pObj->Id == iLeaf)
        return 0;
    if(pObj->Type == ABC_OBJ_PI )
        return -IF_BIG_CHAR;
    Depth0 = If_CutDepthRecComput_rec(Abc_ObjFanin0(pObj), iLeaf);
    Depth1 = If_CutDepthRecComput_rec(Abc_ObjFanin1(pObj), iLeaf);
302
    Depth = Abc_MaxInt(Depth0, Depth1);
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
    Depth = (Depth == -IF_BIG_CHAR) ? -IF_BIG_CHAR : Depth + 1;
    assert(Depth <= 127);
    return Depth;
}

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

  Synopsis    [Compute area of the structure.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
319
unsigned char Abc_NtkRecAreaAndMark_rec(Abc_Obj_t* pObj)
320 321 322
{
    unsigned char Area0, Area1, Area;
    pObj = Abc_ObjRegular(pObj);
323
    if(Abc_ObjIsCi(pObj) || pObj->fMarkA == 1)
324
        return 0;
325 326
    Area0 = Abc_NtkRecAreaAndMark_rec(Abc_ObjFanin0(pObj));
    Area1 = Abc_NtkRecAreaAndMark_rec(Abc_ObjFanin1(pObj));
327 328
    Area = Area1 + Area0 + 1;
    assert(Area <= 255);
329
    pObj->fMarkA = 1;
330 331 332
    return Area;
}

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

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

  Synopsis    [Compute area of the structure.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Abc_NtkRecAreaUnMark_rec(Abc_Obj_t* pObj)
{
    pObj = Abc_ObjRegular(pObj);
    if ( Abc_ObjIsCi(pObj) || pObj->fMarkA == 0 )
        return;
    Abc_NtkRecAreaUnMark_rec( Abc_ObjFanin0(pObj) ); 
    Abc_NtkRecAreaUnMark_rec( Abc_ObjFanin1(pObj) );
    assert( pObj->fMarkA ); // loop detection
    pObj->fMarkA = 0;
}

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

  Synopsis    [Compute area of the structure.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
unsigned char Abc_NtkRecArea(Abc_Obj_t* pObj)
{
    unsigned char area;
    area = Abc_NtkRecAreaAndMark_rec(pObj);
    Abc_NtkRecAreaUnMark_rec(pObj);
    return area;
}

375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
/**Function*************************************************************

  Synopsis    [Compare delay profile of two structures.]

  Description []
               
  SideEffects []

  SeeAlso     []

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

Abc_LookUpStatus_t ABC_NtkRecDelayCompare(char* delayFromStruct, char* delayFromTable, int nVar)
{
    int i, bigThan = 0, smallThan = 0, equal = 1, dominace = 1, beDominaced = 1;
    for (i = 0; i < nVar; i++)
    {
        if(delayFromStruct[i] < delayFromTable[i])
        {
            equal = 0;
            beDominaced = 0;
            if(bigThan == 0)
                smallThan = 1;
        }
        else if (delayFromStruct[i] > delayFromTable[i])
        {
            equal = 0;
            dominace = 0;
            if (smallThan == 0)
                bigThan = 1;        
405
        }   
406
    }
407 408 409 410 411 412
    if(equal)
        return REC_EQUAL;
    else if(dominace)
        return REC_DOMINANCE;
    else if(beDominaced)
        return REC_BEDOMINANCED;
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 438 439 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
     if(bigThan)
        return REC_BIG;
    else if(smallThan)
        return REC_SMALL;
    else
        return REC_SMALL;
}

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

  Synopsis    [link a useless PO to constant 0.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Abc_NtkRecReplacePO(Abc_Obj_t* pObj )
{
    Abc_Obj_t * pConst0, * pFaninNew;
    Abc_Ntk_t * pNtk = pObj->pNtk;
    if ( Abc_ObjFanin0(pObj) == Abc_AigConst1(pNtk) )
    {
        if ( !Abc_ObjFaninC0(pObj) )
            Abc_ObjXorFaninC( pObj, 0 );
        return;
    }
    pConst0 = Abc_ObjNot( Abc_AigConst1(pNtk) );
    pFaninNew = Abc_ObjNotCond( pConst0, Abc_ObjFaninC0(pObj) );
    Abc_ObjPatchFanin( pObj, Abc_ObjFanin0(pObj), pFaninNew );
    assert( Abc_ObjChild0(pObj) == pConst0 );
    //Abc_AigCleanup( (Abc_Aig_t *)pNtk->pManFunc );
}

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

  Synopsis    [Delete a useless structure in the library.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Abc_NtkRecDeleteSubGragh(Abc_ManRec_t* p, Abc_Obj_t* pObj)
{
    Abc_Obj_t* pFanOut;
    int i, deleted = 0;
    Abc_ObjForEachFanout(pObj, pFanOut, i)
    {
        if (Abc_ObjIsCo(pFanOut))
        {
            Abc_NtkRecReplacePO(pFanOut);
            deleted++;
            p->nTrimed++;
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 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 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
    }
    assert(deleted == 1);
}

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

  Synopsis    [Check if the structure is dominant or not.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int ABC_NtkRecIsDominant(char* delayFromStruct, char* delayFromTable, int nVar)
{
    int i;
    for (i = 0; i < nVar; i++)
    {
        if(delayFromStruct[i] > delayFromTable[i])
            return 0;
    }
    return 1;
}

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

  Synopsis    [Sweep the dominated structures.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Abc_NtkRecSweepDominance(Abc_ManRec_t* p, Rec_Obj_t* previous, Rec_Obj_t* current, char * delayFromStruct, int nVars, int fTrim)
{
    Abc_Obj_t* pObj;
    while(current != NULL)
    {
        if (ABC_NtkRecIsDominant(delayFromStruct, current->pinToPinDelay, nVars))
        {
            pObj = current->obj;
            previous->pNext = current->pNext;
            current->pNext = NULL;
            Mem_FixedEntryRecycle(p->pMemObj, (char *)current);
            current = previous->pNext;
            p->nAdded--;
            // if filter the library is needed, then point the PO to a constant.
            if (fTrim)
                Abc_NtkRecDeleteSubGragh(p, pObj);
        }
        else
        {
            previous = current;
            current = current->pNext;
        }
    }
}


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

  Synopsis    [Insert a structure into the look up table.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Abc_NtkRecInsertToLookUpTable(Abc_ManRec_t* p, Rec_Obj_t** ppSpot, Abc_Obj_t* pObj, int nVars, int fTrim)
{
    char delayFromStruct[16] ;
549
    int i;  
550 551
    Abc_Obj_t* pLeaf;
    Rec_Obj_t* entry, *previous = NULL, *current = * ppSpot;
552
    unsigned char costFromStruct = Abc_NtkRecArea(pObj);
553 554 555 556 557 558
    Abc_LookUpStatus_t result;
    for (i = 0; i < nVars; i++)
    {
        pLeaf = Abc_NtkPi( p->pNtk,  i);
        pLeaf =Abc_ObjRegular(pLeaf);
        delayFromStruct[i] = If_CutDepthRecComput_rec(pObj, pLeaf->Id);
559 560 561 562
    }
    if(fTrim)
    {       
        while(1)
563
        {
564
            if (current == NULL)
565
            {
566 567 568 569 570 571 572 573 574 575 576 577 578 579
                p->nAdded++;
                entry = Rec_ObjAlloc(p, pObj, delayFromStruct, costFromStruct, nVars);          
                if(previous != NULL)
                {
                    previous->pNext = entry;
                }
                else
                {
                    // new functional class found
                    p->nAddedFuncs++;
                    *ppSpot = entry;
                    entry->nFrequency = 1;
                }
                break;
580
            }
581 582
            result = ABC_NtkRecDelayCompare(delayFromStruct, current->pinToPinDelay, nVars);    
            if(result == REC_EQUAL)
583
            {
584 585 586
                // when delay profile is equal, replace only if it has smaller cost.
                if(costFromStruct < current->cost)
                {   
587
                    Abc_NtkRecDeleteSubGragh(p, current->obj);
588 589 590
                    Rec_ObjSet(p, current, pObj, delayFromStruct, costFromStruct, nVars);
                }
                else
591
                    Abc_NtkRecDeleteSubGragh(p, pObj);
592
                break;
593
            }
594 595 596
            // when the new structure can dominate others, sweep them out of the library, delete them if required.
            else if(result == REC_DOMINANCE)
            {
597
                Abc_NtkRecDeleteSubGragh(p, current->obj);
598 599 600 601 602 603 604
                Rec_ObjSet(p, current, pObj, delayFromStruct, costFromStruct, nVars);
                Abc_NtkRecSweepDominance(p,current,current->pNext,delayFromStruct, nVars, fTrim);       
                break;
            }
            // when the new structure is domianted by an existed one, don't store it.
            else if (result == REC_BEDOMINANCED)
            {
605
                Abc_NtkRecDeleteSubGragh(p, pObj);
606 607 608 609 610 611 612 613 614 615 616 617 618 619 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
                break;
            }
            // when the new structure's delay profile is big than the current, test the next one
            else if (result == REC_BIG)
            {
                previous = current;
                current = current->pNext;
            }
            // insert the new structure to the right position, sweep the ones it can dominate.
            else if (result == REC_SMALL)
            {
                p->nAdded++;
                entry = Rec_ObjAlloc(p, pObj, delayFromStruct, costFromStruct, nVars);  
                if(previous != NULL)
                {
                    previous->pNext = entry;
                    entry->pNext = current;
                }
                else
                {
                    entry->pNext = current;
                    entry->pCopy = (*ppSpot)->pCopy;
                    entry->nFrequency = (*ppSpot)->nFrequency;
                    (*ppSpot)->pCopy = NULL;
                    (*ppSpot)->nFrequency = 0;
                    *ppSpot = entry;
                }
                Abc_NtkRecSweepDominance(p,current,current->pNext,delayFromStruct, nVars, fTrim);           
                break;
            }
            else
                assert(0);      
        }
    }
    else
    {
        if (current == NULL)
        {
            p->nAdded++;
            entry = Rec_ObjAlloc(p, pObj, delayFromStruct, costFromStruct, nVars);          
            p->nAddedFuncs++;
            *ppSpot = entry;
            entry->nFrequency = 1;
        }
        else
        {
            p->nAdded++;
            entry = Rec_ObjAlloc(p, pObj, delayFromStruct, costFromStruct, nVars);  
            entry->pNext = (*ppSpot)->pNext;
            (*ppSpot)->pNext = entry;
        }
657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672
    }
}

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

  Synopsis    [Build up the structure using library.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
 Hop_Obj_t * Abc_NtkRecBuildUp_rec(Hop_Man_t* pMan, Abc_Obj_t* pObj, Vec_Ptr_t * vNodes)
 {
673 674 675 676 677 678
     Hop_Obj_t * pRes0, *pRes1, *pRes;
    Abc_Obj_t *pRegular = Abc_ObjRegular(pObj);
    if (Abc_NodeIsTravIdCurrent(pRegular) || pRegular->Type == ABC_OBJ_PI)
        return (Hop_Obj_t *)Vec_PtrEntry(vNodes, pRegular->Id);
    Abc_NodeSetTravIdCurrent(pRegular);
    pRes0 = Abc_NtkRecBuildUp_rec(pMan, Abc_ObjFanin0(pRegular), vNodes);
679
    pRes0 = Hop_NotCond(pRes0, pRegular->fCompl0);
680
    pRes1 = Abc_NtkRecBuildUp_rec(pMan, Abc_ObjFanin1(pRegular), vNodes);
681
    pRes1 = Hop_NotCond(pRes1, pRegular->fCompl1);
682 683 684
    pRes = Hop_And(pMan, pRes0, pRes1);
    Vec_PtrWriteEntry(vNodes,pRegular->Id,pRes);
    return pRes;
685 686
 }

687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746
 /**Function*************************************************************

  Synopsis    [Build up the structure using library.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
 Hop_Obj_t * Abc_NtkRecBuildUpFromCurrent_rec(Hop_Man_t* pMan, If_Obj_t* pObj, Vec_Ptr_t * vNodes)
 {
     Hop_Obj_t * pRes0, *pRes1, *pRes;
    If_Obj_t *pRegular = If_Regular(pObj);
    if (Vec_PtrEntry(vNodes, pRegular->Id) && pRegular->fMark == 1)
        return (Hop_Obj_t *)Vec_PtrEntry(vNodes, pRegular->Id);
    pRes0 = Abc_NtkRecBuildUpFromCurrent_rec(pMan, If_ObjFanin0(pRegular), vNodes);
    pRes0 = Hop_NotCond(pRes0, pRegular->fCompl0);
    pRes1 = Abc_NtkRecBuildUpFromCurrent_rec(pMan, If_ObjFanin1(pRegular), vNodes);
    pRes1 = Hop_NotCond(pRes1, pRegular->fCompl1);
    pRes = Hop_And(pMan, pRes0, pRes1);
    Vec_PtrWriteEntry(vNodes,pRegular->Id,pRes);
    assert(pRegular->fMark == 0);
    pRegular->fMark = 1;
    return pRes;
 }

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

  Synopsis    [Derive the final network from the library.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
 Hop_Obj_t * Abc_RecFromCurrentToHop(Hop_Man_t * pMan, If_Man_t * pIfMan, If_Cut_t * pCut, If_Obj_t * pRoot)
 {
     int i;
     If_Obj_t * pLeaf;
     Hop_Obj_t* pHopObj;
     Vec_PtrGrow(s_pMan->vLabels, pIfMan->vObjs->nSize);
     s_pMan->vLabels->nSize = s_pMan->vLabels->nCap;
     If_CutForEachLeaf(pIfMan, pCut, pLeaf, i)
     {
         pHopObj = Hop_IthVar(pMan, i);
         Vec_PtrWriteEntry(s_pMan->vLabels, pLeaf->Id, pHopObj);
         assert(pLeaf->fMark == 0);
         pLeaf->fMark = 1;
     }
     pHopObj = Abc_NtkRecBuildUpFromCurrent_rec(pMan, pRoot, s_pMan->vLabels);
     Abc_NtkRecCurrentUnMark_rec(pRoot);
     return pHopObj;


 }

747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798
  /**Function*************************************************************

  Synopsis    [Look up the best strcuture in the library.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
 int Abc_NtkRecLookUpEnum(If_Man_t * pIfMan,If_Cut_t * pCut, Rec_Obj_t ** ppSpot, Rec_Obj_t ** pCandMin, char * pCanonPerm)
 {
    int DelayMin = ABC_INFINITY , Delay = -ABC_INFINITY;
    Rec_Obj_t *pCand;
    int nLeaves = pCut->nLeaves;
    assert( *ppSpot != NULL );
    for ( pCand = *ppSpot; pCand; pCand = pCand->pNext )
    {
        s_pMan->nFunsDelayComput++; 
        Delay = If_CutComputDelay(pIfMan, pCand, pCut, pCanonPerm ,nLeaves);
        if ( DelayMin > Delay )
        {
            DelayMin = Delay;
            *pCandMin = pCand;
        }
        else if(Delay == DelayMin)
        {
            if(pCand->cost < (*pCandMin)->cost)
                *pCandMin = pCand;
        }
    }
    assert( *pCandMin != NULL );
    return DelayMin;
 }

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

  Synopsis    [Look up the best strcuture in the library.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
 Rec_Obj_t * Abc_NtkRecLookUpBest(If_Man_t * pIfMan,If_Cut_t * pCut, unsigned * pInOut, char * pCanonPerm,  int * fCompl, int * delayBest)
 {
     Rec_Obj_t *pCandMin = NULL, *pCandMinCompl = NULL, **ppSpot;
     int delay = ABC_INFINITY, delayCompl = ABC_INFINITY;
     int nVars = s_pMan->nVars;
799
     //int nLeaves = pCut->nLeaves;
800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839
     ppSpot = Abc_NtkRecTableLookup(s_pMan, s_pMan->pBins, s_pMan->nBins, pInOut, nVars );
     if (*ppSpot != NULL)
         delay = Abc_NtkRecLookUpEnum(pIfMan, pCut, ppSpot, &pCandMin, pCanonPerm);
     Kit_TruthNot(pInOut, pInOut, nVars);
     ppSpot = Abc_NtkRecTableLookup(s_pMan, s_pMan->pBins, s_pMan->nBins, pInOut, nVars );
     if (*ppSpot != NULL)
         delayCompl = Abc_NtkRecLookUpEnum(pIfMan, pCut, ppSpot, &pCandMinCompl, pCanonPerm);
     if (delayBest)
         *delayBest = delay < delayCompl ? delay : delayCompl;
     if (pCandMin == NULL && pCandMinCompl == NULL)
         return NULL;
     else if (pCandMin != NULL && pCandMinCompl != NULL)
     {
         if (delay > delayCompl || (delay == delayCompl && pCandMin->cost > pCandMinCompl->cost))
         {
             if (fCompl)
                 *fCompl = 1;
             return pCandMinCompl;
         }
         else
         {
             if (fCompl)
                 *fCompl = 0;
             return pCandMin;
         }
     }
     else if (pCandMin != NULL)
     {
         if (fCompl)
             *fCompl = 0;
         return pCandMin;
     }
     else
     {
         if (fCompl)
             *fCompl = 1;
         return pCandMinCompl;
     }
 }

840 841 842 843 844 845 846 847 848 849 850
/**Function*************************************************************

  Synopsis    [Derive the final network from the library.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
851
Hop_Obj_t * Abc_RecToHop( Hop_Man_t * pMan, If_Man_t * pIfMan, If_Cut_t * pCut, If_Obj_t * pIfObj )
852
{
853
    Rec_Obj_t *pCandMin;
854 855 856
    Hop_Obj_t* pHopObj;
    Abc_Obj_t* pAbcObj;
    Abc_Ntk_t * pAig = s_pMan->pNtk;
857
    int nLeaves, i;// DelayMin = ABC_INFINITY , Delay = -ABC_INFINITY
858 859 860 861 862
    unsigned uCanonPhase;
    int nVars = s_pMan->nVars;
    char pCanonPerm[16];
    unsigned *pInOut = s_pMan->pTemp1;
    unsigned *pTemp = s_pMan->pTemp2;
863
    clock_t time = clock();
864 865
    int fCompl;
    int * pCompl = &fCompl;
866
    nLeaves = If_CutLeaveNum(pCut);
867 868
//  if (nLeaves < 3)
//      return Abc_NodeTruthToHop(pMan, pIfMan, pCut);
869
    Kit_TruthCopy(pInOut, If_CutTruth(pCut), pCut->nLimit);
870
    //special cases when cut-minimization return 2, that means there is only one leaf in the cut.
871 872 873 874
    if ((Kit_TruthIsConst0(pInOut, nLeaves) && pCut->fCompl == 0) || (Kit_TruthIsConst1(pInOut, nLeaves) && pCut->fCompl == 1))
        return Hop_ManConst0(pMan);
    if ((Kit_TruthIsConst0(pInOut, nLeaves) && pCut->fCompl == 1) || (Kit_TruthIsConst1(pInOut, nLeaves) && pCut->fCompl == 0))
        return Hop_ManConst1(pMan);
875
    if (Kit_TruthSupport(pInOut, nLeaves) != Kit_BitMask(nLeaves))
876
    {   
877 878
        for (i = 0; i < nLeaves; i++)
            if(Kit_TruthVarInSupport( pInOut, nLeaves, i ))
879
                return Hop_NotCond(Hop_IthVar(pMan, i), (pCut->fCompl ^ ((*pInOut & 0x01) > 0)));
880 881
    }
    
882 883 884 885
    for (i = 0; i < nLeaves; i++)
        pCanonPerm[i] = i;
    uCanonPhase = Kit_TruthSemiCanonicize(pInOut, pTemp, nLeaves, pCanonPerm, (short*)s_pMan->pMints);
    If_CutTruthStretch(pInOut, nLeaves, nVars);
886
    pCandMin = Abc_NtkRecLookUpBest(pIfMan, pCut, pInOut, pCanonPerm, pCompl,NULL);
887 888
    Vec_PtrGrow(s_pMan->vLabels, Abc_NtkObjNumMax(pAig));
    s_pMan->vLabels->nSize = s_pMan->vLabels->nCap;
889 890 891 892 893 894 895 896 897 898 899 900
    for (i = 0; i < nLeaves; i++)
    {
        pAbcObj = Abc_NtkPi( pAig, i );
        pHopObj = Hop_IthVar(pMan, pCanonPerm[i]);
        pHopObj = Hop_NotCond(pHopObj, ((uCanonPhase & (1 << i)) > 0));
        Vec_PtrWriteEntry(s_pMan->vLabels, pAbcObj->Id, pHopObj);
    }
    Abc_NtkIncrementTravId(pAig);
    //derive the best structure in the library.
    pHopObj = Abc_NtkRecBuildUp_rec(pMan, pCandMin->obj, s_pMan->vLabels);
    s_pMan->timeIfDerive += clock() - time;
    s_pMan->timeIfTotal += clock() - time;
901
    return Hop_NotCond(pHopObj, (pCut->fCompl)^(((uCanonPhase & (1 << nLeaves)) > 0)) ^ fCompl);    
902 903 904 905
}

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

906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940
  Synopsis    [Duplicates non-danglingn nodes and POs driven by constants.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Abc_NtkDupWithoutDangling_rec( Abc_Ntk_t * pNtkNew, Abc_Obj_t * pObj )
{
    if ( pObj->pCopy != NULL )
        return;
    assert( Abc_ObjIsNode(pObj) );
    Abc_NtkDupWithoutDangling_rec( pNtkNew, Abc_ObjFanin0(pObj) );
    Abc_NtkDupWithoutDangling_rec( pNtkNew, Abc_ObjFanin1(pObj) );
    pObj->pCopy = Abc_AigAnd( (Abc_Aig_t *)pNtkNew->pManFunc, Abc_ObjChild0Copy(pObj), Abc_ObjChild1Copy(pObj) );
}

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

  Synopsis    [Duplicates non-danglingn nodes and POs driven by constants.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Abc_Ntk_t * Abc_NtkDupWithoutDangling( Abc_Ntk_t * pNtk )
{
    Abc_Ntk_t * pNtkNew; 
    Abc_Obj_t * pObj;
    int i;
941
    assert( Abc_NtkIsStrash(pNtk) );
942 943 944 945 946 947 948 949
    // start the network
    pNtkNew = Abc_NtkAlloc( pNtk->ntkType, pNtk->ntkFunc, 1 );
    // duplicate the name and the spec
    pNtkNew->pName = Extra_UtilStrsav(pNtk->pName);
    pNtkNew->pSpec = Extra_UtilStrsav(pNtk->pSpec);
    // clean the node copy fields
    Abc_NtkCleanCopy( pNtk );
    // map the constant nodes
950
    Abc_AigConst1(pNtk)->pCopy = Abc_AigConst1(pNtkNew);
951 952 953 954 955 956 957 958 959 960 961 962 963
    // clone PIs
    Abc_NtkForEachPi( pNtk, pObj, i )
        Abc_NtkDupObj( pNtkNew, pObj, 0 );
    // recursively add non-dangling logic
    Abc_NtkForEachPo( pNtk, pObj, i )
        if ( Abc_ObjFanin0(pObj) != Abc_AigConst1(pNtk) )
            Abc_NtkDupWithoutDangling_rec( pNtkNew, Abc_ObjFanin0(pObj) );
    // clone POs
    Abc_NtkForEachPo( pNtk, pObj, i )
        if ( Abc_ObjFanin0(pObj) != Abc_AigConst1(pNtk) )
        {
            Abc_NtkDupObj( pNtkNew, pObj, 0 );
            Abc_ObjAddFanin( pObj->pCopy, Abc_ObjFanin0(pObj)->pCopy );
964 965 966
        } 
    Abc_NtkAddDummyPiNames( pNtkNew );
    Abc_NtkAddDummyPoNames( pNtkNew );
967 968 969
    if ( !Abc_NtkCheck( pNtkNew ) )
        fprintf( stdout, "Abc_NtkDupWithoutDangling(): Network check has failed.\n" );
    pNtk->pCopy = pNtkNew;
970 971 972 973 974
    return pNtkNew;
}

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

975 976 977 978 979 980 981 982 983
  Synopsis    [Filter the library.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005
void Abc_RecUpdateHashTable()
{
    Abc_ManRec_t * p = s_pMan;
    Rec_Obj_t * pEntry, * pTemp;
    int i;
    for ( i = 0; i < p->nBins; i++ )
        for ( pEntry = p->pBins[i]; pEntry; pEntry = pEntry->pCopy )
            for ( pTemp = pEntry; pTemp; pTemp = pTemp->pNext )
                pTemp->obj = pTemp->obj->pCopy;
}

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

  Synopsis    [Filter the library.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
1006 1007 1008
void Abc_NtkRecFilter(int nLimit)
{
    Rec_Obj_t * previous = NULL, * entry = NULL, * pTemp;
1009
    int i;
1010
    Abc_Ntk_t * pNtk = s_pMan->pNtk;
1011
    clock_t time = clock();
1012 1013 1014 1015 1016 1017 1018
    if (nLimit > 0)
    {
        for ( i = 0; i < s_pMan->nBins; i++ )
        {
            previous = NULL;
            for ( entry = s_pMan->pBins[i]; entry; entry = entry->pCopy)
            {
1019 1020
                assert(entry->nFrequency != 0);
                // only filter the functional classed with frequency less than nLimit.
1021
                if(entry->nFrequency > nLimit)
1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036
                {
                    previous = entry;
                    continue;
                }
                if(previous == NULL)
                {
                    s_pMan->pBins[i] = entry->pCopy;
                    previous = NULL;
                }
                else
                    previous->pCopy = entry->pCopy;

                s_pMan->nAddedFuncs--;
                //delete all the subgragh.
                for ( pTemp = entry; pTemp; pTemp = pTemp->pNext )
1037
                {       
1038 1039
                    s_pMan->nAdded--;
                    Abc_NtkRecDeleteSubGragh(s_pMan, pTemp->obj);
1040
                    Mem_FixedEntryRecycle(s_pMan->pMemObj, (char *)pTemp);              
1041 1042 1043 1044
                }
            }
        }
    }
1045 1046 1047

    // remove dangling nodes and POs driven by constants
    s_pMan->pNtk = Abc_NtkDupWithoutDangling( pNtk );
1048
    Abc_RecUpdateHashTable();
1049 1050
    Abc_NtkDelete( pNtk );

1051
    // collect runtime stats
1052 1053 1054 1055 1056 1057 1058 1059 1060
    s_pMan->timeTrim += clock() - time;
    s_pMan->timeTotal += clock() - time;
}



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

  Synopsis    [Test if the record is running.]
Alan Mishchenko committed
1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Abc_NtkRecIsRunning()
{
    return s_pMan != NULL;
}

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

1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092
  Synopsis    [Test if the record is working in trim mode.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Abc_NtkRecIsInTrimMode()
{
    return (s_pMan != NULL && s_pMan->fTrim);
}

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

  Synopsis    []
Alan Mishchenko committed
1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Abc_NtkRecVarNum()
{
    return (s_pMan != NULL)? s_pMan->nVars : -1;
}

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

1108
  Synopsis    []
Alan Mishchenko committed
1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Vec_Int_t * Abc_NtkRecMemory()
{
    return s_pMan->vMemory;
}

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

  Synopsis    [Starts the record for the given network.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
1133 1134 1135 1136 1137
void Abc_NtkRecLibMerge(Abc_Ntk_t* pNtk)
{
    int i;
    Abc_Obj_t * pObj;
    Abc_ManRec_t * p = s_pMan;
1138
    clock_t clk = clock();
1139 1140 1141 1142 1143 1144 1145 1146
    if ( Abc_NtkPiNum(pNtk) > s_pMan->nVars )
    {
        printf( "The library has more inputs than the record.\n");
        return;
    }
    Abc_NtkForEachPi( pNtk, pObj, i )
        Abc_ObjSetMax( pObj, i+1 );
    Abc_AigForEachAnd( pNtk, pObj, i )
1147
        Abc_ObjSetMax( pObj, Abc_MaxInt( Abc_ObjGetMax(Abc_ObjFanin0(pObj)), Abc_ObjGetMax(Abc_ObjFanin1(pObj)) ) );
1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171

    // insert the PO nodes into the table
    Abc_NtkForEachPo( pNtk, pObj, i )
    {
        p->nTried++;
        //if the PO's input is a constant, skip it.
        if (Abc_ObjChild0(pObj) == Abc_ObjNot( Abc_AigConst1(pNtk)))
        {
            p->nTrimed++;
            continue;
        }   
        pObj = Abc_ObjFanin0(pObj); 
        Abc_NtkRecAddFromLib(pNtk, pObj, Abc_ObjGetMax(pObj) );     
    }
    Abc_NtkForEachObj( pNtk, pObj, i )
    {
        Abc_ObjClearMax( pObj );
    }
    s_pMan->timeMerge += clock() - clk;
    s_pMan->timeTotal += clock() - clk;
}

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

1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185
  Synopsis    [Resize the hash table.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Abc_NtkRecRezieHash(Abc_ManRec_t* p)
{
    Rec_Obj_t ** pBinsNew, **ppSpot;
    Rec_Obj_t * pEntry, * pTemp;
    int nBinsNew, Counter, i;
1186
    clock_t clk = clock();
1187
    // get the new table size
1188
    nBinsNew = Cudd_Prime( 3 * p->nBins ); 
1189 1190 1191 1192 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
    printf("Hash table resize from %d to %d.\n", p->nBins, nBinsNew);
    // allocate a new array
    pBinsNew = ABC_ALLOC( Rec_Obj_t *, nBinsNew );
    memset( pBinsNew, 0, sizeof(Rec_Obj_t *) * nBinsNew );
    // rehash the entries from the old table
    Counter = 0;
    for ( i = 0; i < p->nBins; i++ )
        for ( pEntry = p->pBins[i]; pEntry;)
        {          
            pTemp = pEntry->pCopy;
            ppSpot = Abc_NtkRecTableLookup(p, pBinsNew, nBinsNew, (unsigned *)Vec_PtrEntry(p->vTtNodes, pEntry->Id), p->nVars);
            assert(*ppSpot == NULL);
            *ppSpot = pEntry;
            pEntry->pCopy = NULL;
            pEntry = pTemp;
            Counter++;
        }
    assert( Counter == p->nAddedFuncs);
    ABC_FREE( p->pBins );
    p->pBins = pBinsNew;
    p->nBins = nBinsNew;
    p->timeReHash += clock() - clk;
    p->timeTotal += clock() - clk;

}


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

1218 1219 1220 1221 1222 1223 1224 1225 1226
  Synopsis    [Starts the record for the given network.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
1227
void Abc_NtkRecStart( Abc_Ntk_t * pNtk, int nVars, int nCuts, int fTrim )
Alan Mishchenko committed
1228 1229
{
    Abc_ManRec_t * p;
1230 1231
    Abc_Obj_t * pObj;
    Rec_Obj_t** ppSpot;
Alan Mishchenko committed
1232 1233 1234
    char Buffer[10];
    unsigned * pTruth;
    int i, RetValue;
1235
    clock_t clkTotal = clock(), clk, timeInsert;
1236
    //int testNum = 0;
Alan Mishchenko committed
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

    assert( s_pMan == NULL );
    if ( pNtk == NULL )
    {
        assert( nVars > 2 && nVars <= 16 );
        pNtk = Abc_NtkAlloc( ABC_NTK_STRASH, ABC_FUNC_AIG, 1 );
        pNtk->pName = Extra_UtilStrsav( "record" );
    }
    else
    {
        if ( Abc_NtkGetChoiceNum(pNtk) > 0 )
        {
            printf( "The starting record should be a network without choice nodes.\n" );
            return;
        }
        if ( Abc_NtkPiNum(pNtk) > 16 )
        {
            printf( "The starting record should be a network with no more than %d primary inputs.\n", 16 );
            return;
        }
        if ( Abc_NtkPiNum(pNtk) > nVars )
            printf( "The starting record has %d inputs (warning only).\n", Abc_NtkPiNum(pNtk) );
        pNtk = Abc_NtkDup( pNtk );
    }
    // create the primary inputs
    for ( i = Abc_NtkPiNum(pNtk); i < nVars; i++ )
    {
        pObj = Abc_NtkCreatePi( pNtk );
        Buffer[0] = 'a' + i;
        Buffer[1] = 0;
        Abc_ObjAssignName( pObj, Buffer, NULL );
    }
    Abc_NtkCleanCopy( pNtk );
    Abc_NtkCleanEquiv( pNtk );

    // start the manager
Alan Mishchenko committed
1273
    p = ABC_ALLOC( Abc_ManRec_t, 1 );
Alan Mishchenko committed
1274 1275 1276 1277 1278 1279
    memset( p, 0, sizeof(Abc_ManRec_t) );
    p->pNtk = pNtk;
    p->nVars = Abc_NtkPiNum(pNtk);
    p->nWords = Kit_TruthWordNum( p->nVars );
    p->nCuts = nCuts;
    p->nVarsInit = nVars;
1280 1281 1282
    p->recObjSize = sizeof(Rec_Obj_t) + sizeof(char) * p->nVars;
    p->pMemObj = Mem_FixedStart(p->recObjSize);
    p->fTrim = fTrim;
Alan Mishchenko committed
1283 1284 1285 1286
    // create elementary truth tables
    p->vTtElems = Vec_PtrAlloc( 0 ); assert( p->vTtElems->pArray == NULL );
    p->vTtElems->nSize = p->nVars;
    p->vTtElems->nCap = p->nVars;
1287
    p->vTtElems->pArray = (void **)Extra_TruthElementary( p->nVars );        
1288
/*
Alan Mishchenko committed
1289 1290 1291 1292 1293
    // allocate room for node truth tables
    if ( Abc_NtkObjNum(pNtk) > (1<<14) )
        p->vTtNodes = Vec_PtrAllocSimInfo( 2 * Abc_NtkObjNum(pNtk), p->nWords );
    else
        p->vTtNodes = Vec_PtrAllocSimInfo( 1<<14, p->nWords );
1294 1295
*/
    p->vTtNodes = Vec_PtrAlloc( 1000 );
1296
    p->pMmTruth = Mem_FixedStart( sizeof(unsigned)*p->nWords );
1297
    for ( i = 0; i < Abc_NtkObjNumMax(pNtk); i++ )
1298 1299
//        Vec_PtrPush( p->vTtNodes, ABC_ALLOC(unsigned, p->nWords) );
        Vec_PtrPush( p->vTtNodes, Mem_FixedEntryFetch(p->pMmTruth) );
Alan Mishchenko committed
1300 1301

    // create hash table
1302 1303
    //p->nBins = 50011;
    p->nBins =500011;
1304 1305
    p->pBins = ABC_ALLOC( Rec_Obj_t *, p->nBins );
    memset( p->pBins, 0, sizeof(Rec_Obj_t *) * p->nBins );
Alan Mishchenko committed
1306 1307

    // set elementary tables
1308
    Kit_TruthFill( (unsigned *)Vec_PtrEntry(p->vTtNodes, 0), p->nVars );
Alan Mishchenko committed
1309
    Abc_NtkForEachPi( pNtk, pObj, i )
1310
        Kit_TruthCopy( (unsigned *)Vec_PtrEntry(p->vTtNodes, pObj->Id), (unsigned *)Vec_PtrEntry(p->vTtElems, i), p->nVars );
Alan Mishchenko committed
1311 1312 1313

    // compute the tables
clk = clock();
1314
    
Alan Mishchenko committed
1315 1316 1317 1318 1319 1320 1321
    Abc_AigForEachAnd( pNtk, pObj, i )
    {
        RetValue = Abc_NtkRecComputeTruth( pObj, p->vTtNodes, p->nVars );
        assert( RetValue );
    }
p->timeTruth += clock() - clk;

1322 1323 1324
    Abc_NtkForEachPi( pNtk, pObj, i )
        Abc_ObjSetMax( pObj, i+1 );
    Abc_AigForEachAnd( pNtk, pObj, i )
1325
        Abc_ObjSetMax( pObj, Abc_MaxInt( Abc_ObjGetMax(Abc_ObjFanin0(pObj)), Abc_ObjGetMax(Abc_ObjFanin1(pObj)) ) );
1326

Alan Mishchenko committed
1327
    // insert the PO nodes into the table
1328
    timeInsert = clock();
Alan Mishchenko committed
1329 1330 1331
    Abc_NtkForEachPo( pNtk, pObj, i )
    {
        p->nTried++;
1332 1333 1334 1335 1336
        //if the PO's input is a constant, skip it.
        if (Abc_ObjChild0(pObj) == Abc_ObjNot( Abc_AigConst1(pNtk)))
        {
            p->nTrimed++;
            continue;
1337 1338
        }   
        pObj = Abc_ObjFanin0(pObj);     
1339
        pTruth = (unsigned *)Vec_PtrEntry( p->vTtNodes, pObj->Id );
Alan Mishchenko committed
1340
        // add the resulting truth table to the hash table 
1341 1342 1343
        if(p->nAddedFuncs > 2 * p->nBins)
            Abc_NtkRecRezieHash(p);
        ppSpot = Abc_NtkRecTableLookup(p, p->pBins, p->nBins, pTruth, p->nVars );
1344 1345 1346 1347 1348 1349
        Abc_NtkRecInsertToLookUpTable(p, ppSpot, pObj, Abc_ObjGetMax(pObj), p->fTrim);       
    }
    p->timeInsert += clock() - timeInsert;
    Abc_NtkForEachObj( pNtk, pObj, i )
    {
        Abc_ObjClearMax( pObj );
Alan Mishchenko committed
1350 1351
    }
    // temporaries
Alan Mishchenko committed
1352 1353 1354 1355
    p->pBytes = ABC_ALLOC( int, 4*p->nWords );
    p->pMints = ABC_ALLOC( int, 2*p->nVars );
    p->pTemp1 = ABC_ALLOC( unsigned, p->nWords );
    p->pTemp2 = ABC_ALLOC( unsigned, p->nWords );
1356 1357 1358
    p->pTempTruth = ABC_ALLOC( unsigned, p->nWords );
    p->pTempDepths = ABC_ALLOC( char, p->nVars );
    p->pTempleaves = ABC_ALLOC( int, p->nVars );
Alan Mishchenko committed
1359
    p->vNodes = Vec_PtrAlloc( 100 );
1360
    p->vTtTemps = Vec_PtrAllocSimInfo( 1024, p->nWords );
Alan Mishchenko committed
1361
    p->vMemory = Vec_IntAlloc( Abc_TruthWordNum(p->nVars) * 1000 );
1362
    p->vLabels = Vec_PtrStart( 1000);
Alan Mishchenko committed
1363 1364 1365

    // set the manager
    s_pMan = p;
1366
    p->timeTotal += clock() - clkTotal;
Alan Mishchenko committed
1367 1368 1369 1370
}

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

1371 1372 1373 1374 1375 1376 1377 1378 1379
  Synopsis    [Dump truth tables into a file.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
1380
/*void Abc_NtkRecDumpTruthTables( Abc_ManRec_t * p )
1381
{
1382
    int nVars = 10;
1383
    FILE * pFile;
1384
    Rec_Obj_t * pObj;
1385 1386
    unsigned * pTruth;
    int i;
1387
    pFile = fopen( "tt10.txt", "wb" );
1388 1389 1390
    for ( i = 0; i < p->nBins; i++ )
        for ( pObj = p->pBins[i]; pObj; pObj = pObj->pCopy )
        {
1391
            pTruth = Vec_PtrEntry(p->vTtNodes, pObj->Id);
1392
            if ( (int)Kit_TruthSupport(pTruth, nVars) != (1<<nVars)-1 )
1393
                continue;
1394
            Extra_PrintHex( pFile, pTruth, nVars );
1395
            fprintf( pFile, " " );
1396
            Kit_DsdPrintFromTruth2( pFile, pTruth, nVars );
1397 1398 1399
            fprintf( pFile, "\n" );
        }
    fclose( pFile );
1400
}*/
1401 1402 1403

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

Alan Mishchenko committed
1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415
  Synopsis    [Returns the given record.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Abc_NtkRecStop()
{
    assert( s_pMan != NULL );
1416
//    Abc_NtkRecDumpTruthTables( s_pMan );
Alan Mishchenko committed
1417 1418
    if ( s_pMan->pNtk )
        Abc_NtkDelete( s_pMan->pNtk );
1419 1420 1421
//    Vec_PtrFreeFree( s_pMan->vTtNodes );
    Mem_FixedStop( s_pMan->pMmTruth, 0 );
    Vec_PtrFree( s_pMan->vTtNodes );
Alan Mishchenko committed
1422
    Vec_PtrFree( s_pMan->vTtElems );
Alan Mishchenko committed
1423
    ABC_FREE( s_pMan->pBins );
Alan Mishchenko committed
1424 1425

    // temporaries
Alan Mishchenko committed
1426 1427 1428 1429
    ABC_FREE( s_pMan->pBytes );
    ABC_FREE( s_pMan->pMints );
    ABC_FREE( s_pMan->pTemp1 );
    ABC_FREE( s_pMan->pTemp2 );
1430 1431
    ABC_FREE( s_pMan->pTempTruth );
    ABC_FREE( s_pMan->pTempDepths );
Alan Mishchenko committed
1432 1433 1434 1435 1436 1437
    Vec_PtrFree( s_pMan->vNodes );
    Vec_PtrFree( s_pMan->vTtTemps );
    if ( s_pMan->vLabels )
        Vec_PtrFree( s_pMan->vLabels );
    if ( s_pMan->vCosts )
        Vec_StrFree( s_pMan->vCosts );
1438 1439
    if(s_pMan->pMemObj)
        Mem_FixedStop(s_pMan->pMemObj, 0);
Alan Mishchenko committed
1440
    Vec_IntFree( s_pMan->vMemory );
1441 1442
//  if(s_pMan->vFiltered)
//      Vec_StrFree(s_pMan->vFiltered);
1443
        
Alan Mishchenko committed
1444
    ABC_FREE( s_pMan );
Alan Mishchenko committed
1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463
    s_pMan = NULL;
}

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

  Synopsis    [Returns the given record.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Abc_Ntk_t * Abc_NtkRecUse()
{
    Abc_ManRec_t * p = s_pMan;
    Abc_Ntk_t * pNtk = p->pNtk;
    assert( p != NULL );
1464
    Abc_NtkRecPs(0);
Alan Mishchenko committed
1465 1466
    p->pNtk = NULL;
    Abc_NtkRecStop();
1467
    Abc_NtkCleanData(pNtk);
Alan Mishchenko committed
1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482
    return pNtk;
}


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

  Synopsis    [Print statistics about the current record.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
1483
void Abc_NtkRecPs(int fPrintLib)
Alan Mishchenko committed
1484 1485 1486 1487 1488
{
    int Counter, Counters[17] = {0};
    int CounterS, CountersS[17] = {0};
    Abc_ManRec_t * p = s_pMan;
    Abc_Ntk_t * pNtk = p->pNtk;
1489 1490
    Rec_Obj_t * pEntry, * pTemp;
    Abc_Obj_t * pObj;
Alan Mishchenko committed
1491
    int i;
1492 1493 1494 1495
    FILE * pFile;
    unsigned* pTruth;
    Rec_Obj_t* entry;
    int j;
1496
    int nVars = s_pMan->nVars;
Alan Mishchenko committed
1497 1498 1499 1500
    // set the max PI number
    Abc_NtkForEachPi( pNtk, pObj, i )
        Abc_ObjSetMax( pObj, i+1 );
    Abc_AigForEachAnd( pNtk, pObj, i )
1501
        Abc_ObjSetMax( pObj, Abc_MaxInt( Abc_ObjGetMax(Abc_ObjFanin0(pObj)), Abc_ObjGetMax(Abc_ObjFanin1(pObj)) ) );
1502 1503 1504
if(fPrintLib)
{
    pFile = fopen( "tt10.txt", "wb" );
1505 1506 1507 1508
for ( i = 0; i < p->nBins; i++ )
    for ( entry = p->pBins[i]; entry; entry = entry->pCopy )
    {
        int tmp = 0;
1509
        pTruth = (unsigned*)Vec_PtrEntry(p->vTtNodes, entry->Id);
1510 1511 1512
        /*if ( (int)Kit_TruthSupport(pTruth, nVars) != (1<<nVars)-1 )
            continue;*/
        Extra_PrintHex( pFile, pTruth, nVars );
1513
        fprintf( pFile, " : nVars:  %d, Frequency:%d, nBin:%d  :  ", Abc_ObjGetMax(entry->obj), entry->nFrequency, i);
1514 1515 1516 1517 1518 1519 1520 1521 1522
        Kit_DsdPrintFromTruth2( pFile, pTruth, Abc_ObjGetMax(entry->obj) );
        fprintf( pFile, "\n" );
        for ( pTemp = entry; pTemp; pTemp = pTemp->pNext )
        {
            fprintf(pFile,"%d :", tmp);
            for (j = 0; j <Abc_ObjGetMax(pTemp->obj); j++)
            {
                fprintf(pFile, " %d, ", pTemp->pinToPinDelay[j]);
            }
1523
            fprintf(pFile, "cost = %d ID = %d\n", pTemp->cost, pTemp->Id);             
1524 1525 1526 1527 1528 1529
            tmp++;
        }
        fprintf( pFile, "\n");
        fprintf( pFile, "\n");
    }
    fclose( pFile) ;
1530 1531
}

Alan Mishchenko committed
1532 1533 1534 1535 1536
    // go through the table
    Counter = CounterS = 0;
    for ( i = 0; i < p->nBins; i++ )
    for ( pEntry = p->pBins[i]; pEntry; pEntry = pEntry->pCopy )
    {
1537 1538
        assert(Abc_ObjGetMax(pEntry->obj) >= 2);    
        Counters[ Abc_ObjGetMax(pEntry->obj)]++;
Alan Mishchenko committed
1539
        Counter++;
1540
        for ( pTemp = pEntry; pTemp; pTemp = pTemp->pNext )
Alan Mishchenko committed
1541
        {
1542 1543
            assert( Abc_ObjGetMax(pTemp->obj) == Abc_ObjGetMax(pEntry->obj) );
            CountersS[ Abc_ObjGetMax(pTemp->obj) ]++;
Alan Mishchenko committed
1544 1545 1546
            CounterS++;
        }
    }
1547 1548
    //printf( "Functions = %d. Expected = %d.\n", Counter, p->nAddedFuncs );
    //printf( "Subgraphs = %d. Expected = %d.\n", CounterS, p->nAdded );
Alan Mishchenko committed
1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563
    assert( Counter == p->nAddedFuncs );
    assert( CounterS == p->nAdded );
    // clean
    Abc_NtkForEachObj( pNtk, pObj, i )
    {
        Abc_ObjClearMax( pObj );
    }
    printf( "The record with %d AND nodes in %d subgraphs for %d functions with %d inputs:\n", 
        Abc_NtkNodeNum(pNtk), Abc_NtkPoNum(pNtk), p->nAddedFuncs, Abc_NtkPiNum(pNtk) );
    for ( i = 0; i <= 16; i++ )
    {
        if ( Counters[i] )
            printf( "Inputs = %2d.  Funcs = %8d.  Subgrs = %8d.  Ratio = %6.2f.\n", i, Counters[i], CountersS[i], 1.0*CountersS[i]/Counters[i] );
    }

1564 1565 1566 1567 1568 1569 1570 1571 1572
    printf( "Subgraphs tried                             = %10d. (%6.2f %%)\n", p->nTried,        !p->nTried? 0 : 100.0*p->nTried/p->nTried );
    printf( "Subgraphs filtered by support size          = %10d. (%6.2f %%)\n", p->nFilterSize,   !p->nTried? 0 : 100.0*p->nFilterSize/p->nTried );
    printf( "Subgraphs filtered by structural redundancy = %10d. (%6.2f %%)\n", p->nFilterRedund, !p->nTried? 0 : 100.0*p->nFilterRedund/p->nTried );
    printf( "Subgraphs filtered by volume                = %10d. (%6.2f %%)\n", p->nFilterVolume, !p->nTried? 0 : 100.0*p->nFilterVolume/p->nTried );
    printf( "Subgraphs filtered by TT redundancy         = %10d. (%6.2f %%)\n", p->nFilterTruth,  !p->nTried? 0 : 100.0*p->nFilterTruth/p->nTried );
    printf( "Subgraphs filtered by error                 = %10d. (%6.2f %%)\n", p->nFilterError,  !p->nTried? 0 : 100.0*p->nFilterError/p->nTried );
    printf( "Subgraphs filtered by isomorphism           = %10d. (%6.2f %%)\n", p->nFilterSame,   !p->nTried? 0 : 100.0*p->nFilterSame/p->nTried );
    printf( "Subgraphs added                             = %10d. (%6.2f %%)\n", p->nAdded,        !p->nTried? 0 : 100.0*p->nAdded/p->nTried );
    printf( "Functions added                             = %10d. (%6.2f %%)\n", p->nAddedFuncs,   !p->nTried? 0 : 100.0*p->nAddedFuncs/p->nTried );
1573 1574
    if(s_pMan->fTrim)
        printf( "Functions trimed                            = %10d. (%6.2f %%)\n", p->nTrimed,       !p->nTried? 0 : 100.0*p->nTrimed/p->nTried );
1575
    p->timeOther = p->timeTotal - p->timeCollect - p->timeTruth - p->timeCanon - p->timeInsert - p->timeBuild - p->timeTrim - p->timeMerge - p->timeReHash;
1576
    ABC_PRTP( "Collecting nodes ", p->timeCollect, p->timeTotal);
Alan Mishchenko committed
1577 1578
    ABC_PRTP( "Computing truth  ", p->timeTruth, p->timeTotal );
    ABC_PRTP( "Canonicizing     ", p->timeCanon, p->timeTotal );
1579
    ABC_PRTP( "Building         ", p->timeBuild, p->timeTotal );
1580
    ABC_PRTP( "ReHash           ", p->timeReHash, p->timeTotal );
1581
    ABC_PRTP( "Merge            ", p->timeMerge, p->timeTotal );
1582 1583 1584
    ABC_PRTP( "Insert           ", p->timeInsert, p->timeTotal);
    if(s_pMan->fTrim)
        ABC_PRTP( "Filter           ", p->timeTrim, p->timeTotal);
Alan Mishchenko committed
1585 1586
    ABC_PRTP( "Other            ", p->timeOther, p->timeTotal );
    ABC_PRTP( "TOTAL            ", p->timeTotal, p->timeTotal );
1587
    
Alan Mishchenko committed
1588
    if ( p->nFunsFound )
1589 1590 1591 1592 1593 1594 1595
    {
        printf("\n");
        printf( "During rewriting found = %d and not found = %d functions.\n", p->nFunsFound, p->nFunsNotFound );
        printf( "Functions tried                             = %10d. (%6.2f %%)\n", p->nFunsTried,    !p->nFunsTried? 0 : 100.0*p->nFunsTried/p->nFunsTried );
        printf( "Functions filtered by support               = %10d. (%6.2f %%)\n", p->nFunsFilteredBysupport, !p->nFunsFilteredBysupport? 0 : 100.0*p->nFunsFilteredBysupport/p->nFunsTried );
        printf( "Functions not found in lib                  = %10d. (%6.2f %%)\n", p->nFunsNotFound, !p->nFunsNotFound? 0 : 100.0*p->nFunsNotFound/p->nFunsTried );
        printf( "Functions founded                           = %10d. (%6.2f %%)\n", p->nFunsFound,   !p->nFunsFound? 0 : 100.0*p->nFunsFound/p->nFunsTried );
1596
        printf( "Functions delay computed                    = %10d.  Ratio   = %6.2f.\n", p->nFunsDelayComput,  !p->nFunsDelayComput? 0 : 1.0*p->nFunsDelayComput/p->nFunsFound ); 
1597 1598 1599 1600 1601 1602 1603 1604
        p->timeIfOther = p->timeIfTotal - p->timeIfCanonicize - p->timeIfComputDelay -p->timeIfDerive;
        ABC_PRTP( "Canonicize       ", p->timeIfCanonicize, p->timeIfTotal );
        ABC_PRTP( "Compute Delay    ", p->timeIfComputDelay, p->timeIfTotal );
        ABC_PRTP( "Derive           ", p->timeIfDerive, p->timeIfTotal );
        ABC_PRTP( "Other            ", p->timeIfOther, p->timeIfTotal );
        ABC_PRTP( "TOTAL            ", p->timeIfTotal, p->timeIfTotal );
    }
    
Alan Mishchenko committed
1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638
}


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

  Synopsis    [Returns the hash key.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline unsigned Abc_NtkRecTableHash( unsigned * pTruth, int nVars, int nBins, int * pPrimes )
{
    int i, nWords = Kit_TruthWordNum( nVars );
    unsigned uHash = 0;
    for ( i = 0; i < nWords; i++ )
        uHash ^= pTruth[i] * pPrimes[i & 0x7];
    return uHash % nBins;
}

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

  Synopsis    [Returns the given record.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
1639
Rec_Obj_t ** Abc_NtkRecTableLookup(Abc_ManRec_t* p,  Rec_Obj_t ** pBins, int nBins, unsigned * pTruth, int nVars )
Alan Mishchenko committed
1640 1641
{
    static int s_Primes[10] = { 1291, 1699, 2357, 4177, 5147, 5647, 6343, 7103, 7873, 8147 };
1642
    Rec_Obj_t ** ppSpot, * pEntry;
1643
    ppSpot = pBins + Abc_NtkRecTableHash( pTruth, nVars, nBins, s_Primes );
Alan Mishchenko committed
1644
    for ( pEntry = *ppSpot; pEntry; ppSpot = &pEntry->pCopy, pEntry = pEntry->pCopy )
1645
        if ( Kit_TruthIsEqualWithPhase((unsigned *)Vec_PtrEntry(p->vTtNodes, pEntry->Id), pTruth, nVars) )
Alan Mishchenko committed
1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665
            return ppSpot;
    return ppSpot;
}

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

  Synopsis    [Computes the truth table of the node.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Abc_NtkRecComputeTruth( Abc_Obj_t * pObj, Vec_Ptr_t * vTtNodes, int nVars )
{
    unsigned * pTruth, * pTruth0, * pTruth1;
    int RetValue;
    assert( Abc_ObjIsNode(pObj) );
1666 1667 1668
    pTruth  = (unsigned *)Vec_PtrEntry( vTtNodes, pObj->Id );
    pTruth0 = (unsigned *)Vec_PtrEntry( vTtNodes, Abc_ObjFaninId0(pObj) );
    pTruth1 = (unsigned *)Vec_PtrEntry( vTtNodes, Abc_ObjFaninId1(pObj) );
Alan Mishchenko committed
1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685
    Kit_TruthAndPhase( pTruth, pTruth0, pTruth1, nVars, Abc_ObjFaninC0(pObj), Abc_ObjFaninC1(pObj) );
    assert( (pTruth[0] & 1) == pObj->fPhase );
    RetValue = ((pTruth[0] & 1) == pObj->fPhase);
    return RetValue;
}

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

  Synopsis    [Performs renoding as technology mapping.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
1686
void Abc_NtkRecAdd( Abc_Ntk_t * pNtk, int fUseSOPB)
Alan Mishchenko committed
1687 1688 1689 1690 1691 1692
{
    extern Abc_Ntk_t * Abc_NtkIf( Abc_Ntk_t * pNtk, If_Par_t * pPars );
    extern int Abc_NtkRecAddCut( If_Man_t * pIfMan, If_Obj_t * pRoot, If_Cut_t * pCut );

    If_Par_t Pars, * pPars = &Pars;
    Abc_Ntk_t * pNtkNew;
1693
    clock_t clk = clock();
Alan Mishchenko committed
1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705

    if ( Abc_NtkGetChoiceNum( pNtk ) )
        printf( "Performing renoding with choices.\n" );

    // set defaults
    memset( pPars, 0, sizeof(If_Par_t) );
    // user-controlable paramters
    pPars->nLutSize    =  s_pMan->nVarsInit;
    pPars->nCutsMax    =  s_pMan->nCuts;
    pPars->nFlowIters  =  0;
    pPars->nAreaIters  =  0;
    pPars->DelayTarget = -1;
Alan Mishchenko committed
1706
    pPars->Epsilon     =  (float)0.005;
Alan Mishchenko committed
1707 1708 1709 1710 1711 1712 1713
    pPars->fPreprocess =  0;
    pPars->fArea       =  1;
    pPars->fFancy      =  0;
    pPars->fExpRed     =  0; 
    pPars->fLatchPaths =  0;
    pPars->fSeqMap     =  0;
    pPars->fVerbose    =  0;
1714
    //pPars->fCutMin     =  1;
Alan Mishchenko committed
1715
    // internal parameters
1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727
    if (fUseSOPB)
    {
        pPars->fTruth      =  1;
        pPars->fUsePerm    =  1; 
        pPars->fDelayOpt   =  1;
    }
    else
    {
        pPars->fTruth      =  0;
        pPars->fUsePerm    =  0; 
        pPars->fDelayOpt   =  0;
    }
1728 1729
    pPars->nLatchesCi  =  0;
    pPars->nLatchesCo  =  0;
Alan Mishchenko committed
1730 1731 1732 1733 1734 1735 1736
    pPars->pLutLib     =  NULL; // Abc_FrameReadLibLut();
    pPars->pTimesArr   =  NULL; 
    pPars->pTimesArr   =  NULL;   
    pPars->fUseBdds    =  0;
    pPars->fUseSops    =  0;
    pPars->fUseCnfs    =  0;
    pPars->fUseMv      =  0;
1737
    pPars->fSkipCutFilter = 1;
Alan Mishchenko committed
1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782
    pPars->pFuncCost   =  NULL;
    pPars->pFuncUser   =  Abc_NtkRecAddCut;

    // perform recording
    pNtkNew = Abc_NtkIf( pNtk, pPars );
    Abc_NtkDelete( pNtkNew );
s_pMan->timeTotal += clock() - clk;

//    if ( !Abc_NtkCheck( s_pMan->pNtk ) )
//        printf( "Abc_NtkRecAdd: The network check has failed.\n" );
}

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

  Synopsis    [Adds the cut function to the internal storage.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Abc_NtkRecCollectNodes_rec( If_Obj_t * pNode, Vec_Ptr_t * vNodes )
{
    if ( pNode->fMark )
        return;
    pNode->fMark = 1;
    assert( If_ObjIsAnd(pNode) );
    Abc_NtkRecCollectNodes_rec( If_ObjFanin0(pNode), vNodes );
    Abc_NtkRecCollectNodes_rec( If_ObjFanin1(pNode), vNodes );
    Vec_PtrPush( vNodes, pNode );
}

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

  Synopsis    [Adds the cut function to the internal storage.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802
void Abc_NtkRecCollectNodesFromLib_rec( Abc_Obj_t * pNode, Vec_Ptr_t * vNodes )
{
    if ( Abc_ObjIsPi(pNode))
        return;
    Abc_NtkRecCollectNodesFromLib_rec( Abc_ObjFanin0(pNode), vNodes );
    Abc_NtkRecCollectNodesFromLib_rec( Abc_ObjFanin1(pNode), vNodes );
    Vec_PtrPush( vNodes, pNode );
}

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

  Synopsis    [Adds the cut function to the internal storage.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833
int Abc_NtkRecCollectNodes( If_Man_t * pIfMan, If_Obj_t * pRoot, If_Cut_t * pCut, Vec_Ptr_t * vNodes )
{
    If_Obj_t * pLeaf;
    int i, RetValue = 1;

    // collect the internal nodes of the cut
    Vec_PtrClear( vNodes );
    If_CutForEachLeaf( pIfMan, pCut, pLeaf, i )
    {
        Vec_PtrPush( vNodes, pLeaf );
        assert( pLeaf->fMark == 0 );
        pLeaf->fMark = 1;
    }

    // collect other nodes
    Abc_NtkRecCollectNodes_rec( pRoot, vNodes );

    // check if there are leaves, such that both of their fanins are marked
    // this indicates a redundant cut
    If_CutForEachLeaf( pIfMan, pCut, pLeaf, i )
    {
        if ( !If_ObjIsAnd(pLeaf) )
            continue;
        if ( If_ObjFanin0(pLeaf)->fMark && If_ObjFanin1(pLeaf)->fMark )
        {
            RetValue = 0;
            break;
        }
    }

    // clean the mark
1834
    Vec_PtrForEachEntry( If_Obj_t *, vNodes, pLeaf, i )
Alan Mishchenko committed
1835 1836 1837 1838 1839
        pLeaf->fMark = 0;
/*
    if ( pRoot->Id == 2639 )
    {
        // print the cut
1840
        Vec_PtrForEachEntry( If_Obj_t *, vNodes, pLeaf, i )
Alan Mishchenko committed
1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856
        {
            if ( If_ObjIsAnd(pLeaf) )
                printf( "%4d = %c%4d & %c%4d\n", pLeaf->Id, 
                    (If_ObjFaninC0(pLeaf)? '-':'+'), If_ObjFanin0(pLeaf)->Id, 
                    (If_ObjFaninC1(pLeaf)? '-':'+'), If_ObjFanin1(pLeaf)->Id ); 
            else
                printf( "%4d = pi\n", pLeaf->Id ); 
        }
        printf( "\n" );
    }
*/
    return RetValue;
}

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

1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880
  Synopsis    [Adds the cut function to the internal storage.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Abc_NtkRecCollectNodesFromLib(Abc_Ntk_t* pNtk, Abc_Obj_t * pRoot, Vec_Ptr_t * vNodes , int nVars)
{
    int i;
    // collect the internal nodes of the cut
    Vec_PtrClear( vNodes );
    for ( i = 0; i < nVars; i++ )
        Vec_PtrPush( vNodes, Abc_NtkPi(pNtk, i));


    // collect other nodes
    Abc_NtkRecCollectNodesFromLib_rec( pRoot, vNodes );
}

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

Alan Mishchenko committed
1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901
  Synopsis    [Computes truth tables of nodes in the cut.]

  Description [Returns 0 if the TT does not depend on some cut variables.
  Or if the TT can be expressed simpler using other nodes.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Abc_NtkRecCutTruth( Vec_Ptr_t * vNodes, int nLeaves, Vec_Ptr_t * vTtTemps, Vec_Ptr_t * vTtElems )
{
    unsigned * pSims, * pSims0, * pSims1;
    unsigned * pTemp = s_pMan->pTemp2;
    unsigned uWord;
    If_Obj_t * pObj, * pObj2, * pRoot;
    int i, k, nLimit, nInputs = s_pMan->nVars;

    assert( Vec_PtrSize(vNodes) > nLeaves );

    // set the elementary truth tables and compute the truth tables of the nodes
1902
    Vec_PtrForEachEntry( If_Obj_t *, vNodes, pObj, i )
Alan Mishchenko committed
1903 1904 1905 1906 1907
    {
        pObj->pCopy = Vec_PtrEntry(vTtTemps, i);
        pSims = (unsigned *)pObj->pCopy;
        if ( i < nLeaves )
        {
1908
            Kit_TruthCopy( pSims, (unsigned *)Vec_PtrEntry(vTtElems, i), nInputs );
Alan Mishchenko committed
1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919
            continue;
        }
        assert( If_ObjIsAnd(pObj) );
        // get hold of the simulation information
        pSims0 = (unsigned *)If_ObjFanin0(pObj)->pCopy;
        pSims1 = (unsigned *)If_ObjFanin1(pObj)->pCopy;
        // simulate the node
        Kit_TruthAndPhase( pSims, pSims0, pSims1, nInputs, If_ObjFaninC0(pObj), If_ObjFaninC1(pObj) );
    }

    // check the support size
1920
    pRoot = (If_Obj_t *)Vec_PtrEntryLast( vNodes );
Alan Mishchenko committed
1921 1922 1923 1924 1925 1926 1927
    pSims = (unsigned *)pRoot->pCopy;
    if ( Kit_TruthSupport(pSims, nInputs) != Kit_BitMask(nLeaves) )
        return 0;

    // make sure none of the nodes has the same simulation info as the output
    // check pairwise comparisons
    nLimit = Vec_PtrSize(vNodes) - 1;
1928
    Vec_PtrForEachEntryStop( If_Obj_t *, vNodes, pObj, i, nLimit )
Alan Mishchenko committed
1929 1930 1931 1932
    {
        pSims0 = (unsigned *)pObj->pCopy;
        if ( Kit_TruthIsEqualWithPhase(pSims, pSims0, nInputs) )
            return 0;
1933
        Vec_PtrForEachEntryStop( If_Obj_t *, vNodes, pObj2, k, i )
Alan Mishchenko committed
1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977
        {
            if ( (If_ObjFanin0(pRoot) == pObj && If_ObjFanin1(pRoot) == pObj2) ||
                 (If_ObjFanin1(pRoot) == pObj && If_ObjFanin0(pRoot) == pObj2) )
                 continue;
            pSims1 = (unsigned *)pObj2->pCopy;

            uWord = pSims0[0] & pSims1[0];
            if ( pSims[0] == uWord || pSims[0] == ~uWord )
            {
                Kit_TruthAndPhase( pTemp, pSims0, pSims1, nInputs, 0, 0 );
                if ( Kit_TruthIsEqualWithPhase(pSims, pTemp, nInputs) )
                    return 0;
            }

            uWord = pSims0[0] & ~pSims1[0];
            if ( pSims[0] == uWord || pSims[0] == ~uWord )
            {
                Kit_TruthAndPhase( pTemp, pSims0, pSims1, nInputs, 0, 1 );
                if ( Kit_TruthIsEqualWithPhase(pSims, pTemp, nInputs) )
                    return 0;
            }

            uWord = ~pSims0[0] & pSims1[0];
            if ( pSims[0] == uWord || pSims[0] == ~uWord )
            {
                Kit_TruthAndPhase( pTemp, pSims0, pSims1, nInputs, 1, 0 );
                if ( Kit_TruthIsEqualWithPhase(pSims, pTemp, nInputs) )
                    return 0;
            }

            uWord = ~pSims0[0] & ~pSims1[0];
            if ( pSims[0] == uWord || pSims[0] == ~uWord )
            {
                Kit_TruthAndPhase( pTemp, pSims0, pSims1, nInputs, 1, 1 );
                if ( Kit_TruthIsEqualWithPhase(pSims, pTemp, nInputs) )
                    return 0;
            }
        }
    }
    return 1;
}

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

1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990
  Synopsis    [Computes truth tables of nodes in the cut.]

  Description [Returns 0 if the TT does not depend on some cut variables.
  Or if the TT can be expressed simpler using other nodes.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Abc_NtkRecCutTruthFromLib( Vec_Ptr_t * vNodes, int nLeaves, Vec_Ptr_t * vTtTemps, Vec_Ptr_t * vTtElems )
{
    unsigned * pSims, * pSims0, * pSims1;
1991
    //unsigned * pTemp = s_pMan->pTemp2;
1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021
    Abc_Obj_t * pObj, * pRoot;
    int i, nInputs = s_pMan->nVars;

    assert( Vec_PtrSize(vNodes) > nLeaves );

    // set the elementary truth tables and compute the truth tables of the nodes
    Vec_PtrForEachEntry( Abc_Obj_t *, vNodes, pObj, i )
    {
        pObj->pTemp = Vec_PtrEntry(vTtTemps, i);
        pSims = (unsigned *)pObj->pTemp;
        if ( i < nLeaves )
        {
            Kit_TruthCopy( pSims, (unsigned *)Vec_PtrEntry(vTtElems, i), nInputs );
            continue;
        }
        // get hold of the simulation information
        pSims0 = (unsigned *)Abc_ObjFanin0(pObj)->pTemp;
        pSims1 = (unsigned *)Abc_ObjFanin1(pObj)->pTemp;
        // simulate the node
        Kit_TruthAndPhase( pSims, pSims0, pSims1, nInputs, Abc_ObjFaninC0(pObj), Abc_ObjFaninC1(pObj) );
    }

    // check the support size
    pRoot = (Abc_Obj_t *)Vec_PtrEntryLast( vNodes );
    pSims = (unsigned *)pRoot->pTemp;
    assert ( Kit_TruthSupport(pSims, nInputs) == Kit_BitMask(nLeaves) );
}

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

Alan Mishchenko committed
2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044
  Synopsis    [Adds the cut function to the internal storage.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Abc_NtkRecAddCutCheckCycle_rec( Abc_Obj_t * pRoot, Abc_Obj_t * pObj )
{
    assert( pRoot->Level > 0 );
    if ( pObj->Level < pRoot->Level )
        return 1;
    if ( pObj == pRoot )
        return 0;
    if ( !Abc_NtkRecAddCutCheckCycle_rec(pRoot, Abc_ObjFanin0(pObj)) )
        return 0;
    if ( !Abc_NtkRecAddCutCheckCycle_rec(pRoot, Abc_ObjFanin1(pObj)) )
        return 0;
    return 1;
}

2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062

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

  Synopsis    [Building structures generated by SOPB.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Abc_NtkRecAddSOPB( If_Man_t * pIfMan, If_Cut_t * pCut, unsigned* pInOut, char* pCanonPerm, unsigned uCanonPhase )
{
    If_And_t Leaf;
    int i, nNodes, RetValue, nNodesBeg, timeInsert;
    Vec_Wrd_t * vAnds;
    Abc_Ntk_t * pAig = s_pMan->pNtk;
2063
    Abc_Obj_t * pAbcObj, * pFanin0, * pFanin1, * pObj = NULL, * pObjPo;
2064 2065 2066 2067 2068 2069 2070
    static int s_MaxSize[16] = { 0 };
    int nLeaves = If_CutLeaveNum(pCut);
    int nInputs = s_pMan->nVars;
    word Entry;
    If_And_t This;
    Rec_Obj_t ** ppSpot;
    char Buffer[40], Name[20], Truth[20];
2071
    clock_t timeBuild = clock();
2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177
    unsigned * pTruth;
    vAnds = If_CutDelaySopArray( pIfMan, pCut );
    if(Vec_WrdSize(vAnds) > nLeaves + 3*(nLeaves-1) + s_MaxSize[nLeaves])
    {
        s_pMan->nFilterVolume++;
        return;
    }
    Vec_PtrGrow(s_pMan->vLabels, Abc_NtkObjNumMax(pAig));
    s_pMan->vLabels->nSize = s_pMan->vLabels->nCap;
    for (i = 0; i < nLeaves; i++)
    {
        pAbcObj = Abc_NtkPi( pAig, i );
        Leaf = If_WrdToAnd(Vec_WrdEntry(vAnds, pCanonPerm[i]));
        pAbcObj = Abc_ObjNotCond(pAbcObj,Leaf.fCompl ^ ((uCanonPhase & (1 << i)) > 0));
        Vec_PtrWriteEntry(s_pMan->vLabels, Leaf.Id, pAbcObj);
    }
    nNodesBeg = Abc_NtkObjNumMax( pAig );
    Vec_WrdForEachEntryStart( vAnds, Entry, i, nLeaves )
    {
        This  = If_WrdToAnd( Entry );
        pFanin0 = Abc_ObjNotCond( (Abc_Obj_t *)Vec_PtrEntry(s_pMan->vLabels, This.iFan0), This.fCompl0 ); 
        pFanin1 = Abc_ObjNotCond( (Abc_Obj_t *)Vec_PtrEntry(s_pMan->vLabels, This.iFan1), This.fCompl1 ); 
        nNodes = Abc_NtkObjNumMax( pAig );
        pObj = Abc_AigAnd( (Abc_Aig_t *)pAig->pManFunc, pFanin0, pFanin1 );
        Vec_PtrWriteEntry(s_pMan->vLabels,This.Id,pObj);
        assert( !Abc_ObjIsComplement(pObj) );

        if ( pObj->Id == nNodes )
        {
            // increase storage for truth tables
            //            if ( Vec_PtrSize(s_pMan->vTtNodes) <= pObj->Id )
            //                Vec_PtrDoubleSimInfo(s_pMan->vTtNodes);
            while ( Vec_PtrSize(s_pMan->vTtNodes) <= pObj->Id )
                //                Vec_PtrPush( s_pMan->vTtNodes, ABC_ALLOC(unsigned, s_pMan->nWords) );
                Vec_PtrPush( s_pMan->vTtNodes, Mem_FixedEntryFetch(s_pMan->pMmTruth) );

            // compute the truth table
            RetValue = Abc_NtkRecComputeTruth( pObj, s_pMan->vTtNodes, nInputs );
            if ( RetValue == 0 )
            {
                s_pMan->nFilterError++;
                printf( "T" );
                return;
            }
        }
    }
    assert(pObj);
    pTruth = (unsigned *)Vec_PtrEntry( s_pMan->vTtNodes, pObj->Id );
    if ( Kit_TruthSupport(pTruth, nInputs) != Kit_BitMask(nLeaves) )
    {
        s_pMan->nFilterError++;
        printf( "S" );
        return;
    }

    // compare the truth tables
    if ( !Kit_TruthIsEqualWithPhase( pTruth, pInOut, nInputs ) )
    {
        s_pMan->nFilterError++;
        printf( "F" );
        return;
    }
    s_pMan->timeBuild = clock() - timeBuild;
    //    Extra_PrintBinary( stdout, pInOut, 8 ); printf( "\n" );

    // look up in the hash table and increase the hit number of the functional class
    if(s_pMan->nAddedFuncs > 2 * s_pMan->nBins)
        Abc_NtkRecRezieHash(s_pMan);
    ppSpot = Abc_NtkRecTableLookup(s_pMan, s_pMan->pBins,s_pMan->nBins , pTruth, nInputs );
    Abc_NtkRecFrequencyInc(*ppSpot);   
    // if not new nodes were added and the node has a CO fanout

    if ( nNodesBeg == Abc_NtkObjNumMax(pAig) && Abc_NodeFindCoFanout(pObj) != NULL )
    {
        s_pMan->nFilterSame++;
        //assert(*ppSpot != NULL);
        return;
    }

    // create PO for this node
    pObjPo = Abc_NtkCreatePo(pAig);
    Abc_ObjAddFanin( pObjPo, pObj );

    // assign the name to this PO
    sprintf( Name, "%d_%06d", nLeaves, Abc_NtkPoNum(pAig) );
    if ( (nInputs <= 6) && 0 )
    {
        Extra_PrintHexadecimalString( Truth, pInOut, nInputs );
        sprintf( Buffer, "%s_%s", Name, Truth );
    }
    else
    {
        sprintf( Buffer, "%s", Name );
    }
    Abc_ObjAssignName( pObjPo, Buffer, NULL );

    // add the resulting truth table to the hash table 
    timeInsert = clock();
    ppSpot = Abc_NtkRecTableLookup(s_pMan, s_pMan->pBins, s_pMan->nBins, pTruth, s_pMan->nVars );
    Abc_NtkRecInsertToLookUpTable(s_pMan, ppSpot, pObj, nLeaves, s_pMan->fTrim); 
    s_pMan->timeInsert += clock() - timeInsert;
    s_pMan->timeTotal = clock() - timeBuild;
    return;
   
}

Alan Mishchenko committed
2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193
/**Function*************************************************************

  Synopsis    [Adds the cut function to the internal storage.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Abc_NtkRecAddCut( If_Man_t * pIfMan, If_Obj_t * pRoot, If_Cut_t * pCut )
{
    static int s_MaxSize[16] = { 0 };
    char Buffer[40], Name[20], Truth[20];
    char pCanonPerm[16];
2194 2195
    Abc_Obj_t * pObj = NULL, * pFanin0, * pFanin1, * pObjPo;
    Rec_Obj_t ** ppSpot;
Alan Mishchenko committed
2196 2197 2198 2199 2200 2201 2202 2203
    Abc_Ntk_t * pAig = s_pMan->pNtk;
    If_Obj_t * pIfObj;
    Vec_Ptr_t * vNodes = s_pMan->vNodes;
    unsigned * pInOut = s_pMan->pTemp1;
    unsigned * pTemp = s_pMan->pTemp2;
    unsigned * pTruth;
    int i, RetValue, nNodes, nNodesBeg, nInputs = s_pMan->nVars, nLeaves = If_CutLeaveNum(pCut);
    unsigned uCanonPhase;
2204
    clock_t clk, timeInsert, timeBuild;
2205
    //int begin = clock();
Alan Mishchenko committed
2206 2207 2208 2209
    assert( nInputs <= 16 );
    assert( nInputs == (int)pCut->nLimit );
    s_pMan->nTried++;
    // skip small cuts
2210 2211
     if ( nLeaves < 2 )
     {
Alan Mishchenko committed
2212 2213
        s_pMan->nFilterSize++;
        return 1;
2214
     }
2215
    
Alan Mishchenko committed
2216 2217 2218
    // collect internal nodes and skip redundant cuts
clk = clock();
    RetValue = Abc_NtkRecCollectNodes( pIfMan, pRoot, pCut, vNodes );
2219

Alan Mishchenko committed
2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236
s_pMan->timeCollect += clock() - clk;
    if ( !RetValue )
    {
        s_pMan->nFilterRedund++;
        return 1;
    }

    // skip cuts with very large volume
    if ( Vec_PtrSize(vNodes) > nLeaves + 3*(nLeaves-1) + s_MaxSize[nLeaves] )
    {
        s_pMan->nFilterVolume++;
        return 1;
    }

    // compute truth table and skip the redundant structures
clk = clock();
    RetValue = Abc_NtkRecCutTruth( vNodes, nLeaves, s_pMan->vTtTemps, s_pMan->vTtElems );
2237
    s_pMan->timeTruth += clock() - clk;
Alan Mishchenko committed
2238 2239
    if ( !RetValue )
    {
2240 2241
        //fprintf(file,"redundant structures\n");
        //fclose(file);
Alan Mishchenko committed
2242 2243 2244 2245 2246 2247 2248 2249
        s_pMan->nFilterTruth++;
        return 1;
    }

    // copy the truth table
    Kit_TruthCopy( pInOut, (unsigned *)pRoot->pCopy, nInputs );

    // set permutation
2250
    for ( i = 0; i < nLeaves; i++ )
Alan Mishchenko committed
2251 2252 2253 2254
        pCanonPerm[i] = i;

    // semi-canonicize the truth table
clk = clock();
2255 2256 2257
    uCanonPhase = Kit_TruthSemiCanonicize( pInOut, pTemp, nLeaves, pCanonPerm, (short *)s_pMan->pMints );
    If_CutTruthStretch(pInOut, nLeaves, s_pMan->nVars);
    s_pMan->timeCanon += clock() - clk;
Alan Mishchenko committed
2258 2259 2260
    // pCanonPerm and uCanonPhase show what was the variable corresponding to each var in the current truth

    // go through the variables in the new truth table
2261
    timeBuild = clock();
Alan Mishchenko committed
2262 2263 2264
    for ( i = 0; i < nLeaves; i++ )
    {
        // get hold of the corresponding leaf
Alan Mishchenko committed
2265
        pIfObj = If_ManObj( pIfMan, pCut->pLeaves[(int)pCanonPerm[i]] );
Alan Mishchenko committed
2266 2267 2268 2269 2270 2271 2272 2273 2274
        // get hold of the corresponding new node
        pObj = Abc_NtkPi( pAig, i );
        pObj = Abc_ObjNotCond( pObj, (uCanonPhase & (1 << i)) );
        // map them
        pIfObj->pCopy = pObj;
    }

    // build the node and compute its truth table
    nNodesBeg = Abc_NtkObjNumMax( pAig );
2275
    Vec_PtrForEachEntryStart( If_Obj_t *, vNodes, pIfObj, i, nLeaves )
Alan Mishchenko committed
2276
    {
2277 2278
        pFanin0 = Abc_ObjNotCond( (Abc_Obj_t *)If_ObjFanin0(pIfObj)->pCopy, If_ObjFaninC0(pIfObj) );
        pFanin1 = Abc_ObjNotCond( (Abc_Obj_t *)If_ObjFanin1(pIfObj)->pCopy, If_ObjFaninC1(pIfObj) );
Alan Mishchenko committed
2279 2280

        nNodes = Abc_NtkObjNumMax( pAig );
2281
        pObj = Abc_AigAnd( (Abc_Aig_t *)pAig->pManFunc, pFanin0, pFanin1 );
Alan Mishchenko committed
2282 2283 2284 2285 2286 2287
        assert( !Abc_ObjIsComplement(pObj) );
        pIfObj->pCopy = pObj;

        if ( pObj->Id == nNodes )
        {
            // increase storage for truth tables
2288 2289 2290
//            if ( Vec_PtrSize(s_pMan->vTtNodes) <= pObj->Id )
//                Vec_PtrDoubleSimInfo(s_pMan->vTtNodes);
            while ( Vec_PtrSize(s_pMan->vTtNodes) <= pObj->Id )
2291 2292
//                Vec_PtrPush( s_pMan->vTtNodes, ABC_ALLOC(unsigned, s_pMan->nWords) );
                Vec_PtrPush( s_pMan->vTtNodes, Mem_FixedEntryFetch(s_pMan->pMmTruth) );
2293

Alan Mishchenko committed
2294 2295 2296 2297 2298 2299 2300 2301 2302 2303
            // compute the truth table
            RetValue = Abc_NtkRecComputeTruth( pObj, s_pMan->vTtNodes, nInputs );
            if ( RetValue == 0 )
            {
                s_pMan->nFilterError++;
                printf( "T" );
                return 1;
            }
        }
    }
Alan Mishchenko committed
2304
    assert(pObj);
2305
    s_pMan->timeBuild += clock() - timeBuild;
2306
    pTruth = (unsigned *)Vec_PtrEntry( s_pMan->vTtNodes, pObj->Id );
Alan Mishchenko committed
2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321
    if ( Kit_TruthSupport(pTruth, nInputs) != Kit_BitMask(nLeaves) )
    {
        s_pMan->nFilterError++;
        printf( "S" );
        return 1;
    }

    // compare the truth tables
    if ( !Kit_TruthIsEqualWithPhase( pTruth, pInOut, nInputs ) )
    {
        s_pMan->nFilterError++;
        printf( "F" );
        return 1;
    }
//    Extra_PrintBinary( stdout, pInOut, 8 ); printf( "\n" );
2322
    
2323
    // look up in the hash table and increase the hit number of the functional class
2324 2325 2326 2327
    if(s_pMan->nAddedFuncs > 2 * s_pMan->nBins)
        Abc_NtkRecRezieHash(s_pMan);
    ppSpot = Abc_NtkRecTableLookup(s_pMan, s_pMan->pBins,s_pMan->nBins , pTruth, nInputs );
    Abc_NtkRecFrequencyInc(*ppSpot);   
Alan Mishchenko committed
2328
    // if not new nodes were added and the node has a CO fanout
2329
    
Alan Mishchenko committed
2330 2331 2332
    if ( nNodesBeg == Abc_NtkObjNumMax(pAig) && Abc_NodeFindCoFanout(pObj) != NULL )
    {
        s_pMan->nFilterSame++;
2333
        //assert(*ppSpot != NULL);
Alan Mishchenko committed
2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354
        return 1;
    }

    // create PO for this node
    pObjPo = Abc_NtkCreatePo(pAig);
    Abc_ObjAddFanin( pObjPo, pObj );

    // assign the name to this PO
    sprintf( Name, "%d_%06d", nLeaves, Abc_NtkPoNum(pAig) );
    if ( (nInputs <= 6) && 0 )
    {
        Extra_PrintHexadecimalString( Truth, pInOut, nInputs );
        sprintf( Buffer, "%s_%s", Name, Truth );
    }
    else
    {
        sprintf( Buffer, "%s", Name );
    }
    Abc_ObjAssignName( pObjPo, Buffer, NULL );

    // add the resulting truth table to the hash table 
2355 2356 2357
    timeInsert = clock();
    Abc_NtkRecInsertToLookUpTable(s_pMan, ppSpot, pObj, nLeaves, s_pMan->fTrim);
    s_pMan->timeInsert += clock() - timeInsert;
2358 2359
    if (pIfMan->pPars->fDelayOpt)
        Abc_NtkRecAddSOPB(pIfMan, pCut, pTruth, pCanonPerm, uCanonPhase );
2360 2361 2362
    return 1;
}

2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382
/**Function*************************************************************

  Synopsis    [Adds the cut function to the internal storage.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Abc_NtkRecAddFromLib( Abc_Ntk_t* pNtk, Abc_Obj_t * pRoot, int nVars )
{
    char Buffer[40], Name[20], Truth[20];
    Abc_Obj_t * pObj = NULL, * pFanin0, * pFanin1, * pObjPo;
    Rec_Obj_t ** ppSpot;
    Abc_Ntk_t * pAig = s_pMan->pNtk;
    Abc_Obj_t * pAbcObj;
    Vec_Ptr_t * vNodes = s_pMan->vNodes;
    unsigned * pInOut = s_pMan->pTemp1;
2383
    //unsigned * pTemp = s_pMan->pTemp2;
2384 2385
    unsigned * pTruth;
    int i, RetValue, nNodes, nNodesBeg, nInputs = s_pMan->nVars, nLeaves = nVars;
2386
    assert( nInputs <= 16 );   
2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440
    // collect internal nodes and skip redundant cuts
    Abc_NtkRecCollectNodesFromLib(pNtk, pRoot, vNodes , nLeaves);
    Abc_NtkRecCutTruthFromLib(vNodes, nLeaves, s_pMan->vTtTemps, s_pMan->vTtElems );
    // copy the truth table
    Kit_TruthCopy( pInOut, (unsigned *)pRoot->pTemp, nInputs );
    // go through the variables in the new truth table
    for ( i = 0; i < nLeaves; i++ )
    {
        // get hold of the corresponding leaf
        pAbcObj = Abc_NtkPi(pNtk, i);
        // get hold of the corresponding new node
        pObj = Abc_NtkPi( pAig, i );
        // map them
        pAbcObj->pCopy = pObj;
    }

    // build the node and compute its truth table
    nNodesBeg = Abc_NtkObjNumMax( pAig );
    Vec_PtrForEachEntryStart( Abc_Obj_t *, vNodes, pAbcObj, i, nLeaves )
    {
        pFanin0 = Abc_ObjNotCond( (Abc_Obj_t *)Abc_ObjFanin0(pAbcObj)->pCopy, Abc_ObjFaninC0(pAbcObj) );
        pFanin1 = Abc_ObjNotCond( (Abc_Obj_t *)Abc_ObjFanin1(pAbcObj)->pCopy, Abc_ObjFaninC1(pAbcObj) );

        nNodes = Abc_NtkObjNumMax( pAig );
        pObj = Abc_AigAnd( (Abc_Aig_t *)pAig->pManFunc, pFanin0, pFanin1 );
        assert( !Abc_ObjIsComplement(pObj) );
        pAbcObj->pCopy = pObj;

        if ( pObj->Id == nNodes )
        {
            // increase storage for truth tables
//            if ( Vec_PtrSize(s_pMan->vTtNodes) <= pObj->Id )
//                Vec_PtrDoubleSimInfo(s_pMan->vTtNodes);
            while ( Vec_PtrSize(s_pMan->vTtNodes) <= pObj->Id )
//                Vec_PtrPush( s_pMan->vTtNodes, ABC_ALLOC(unsigned, s_pMan->nWords) );
                Vec_PtrPush( s_pMan->vTtNodes, Mem_FixedEntryFetch(s_pMan->pMmTruth) );
            // compute the truth table
            RetValue = Abc_NtkRecComputeTruth( pObj, s_pMan->vTtNodes, nInputs );
            if ( RetValue == 0 )
            {
                s_pMan->nFilterError++;
                printf( "T" );
            }
        }
    }
    assert(pObj);
    pTruth = (unsigned *)Vec_PtrEntry( s_pMan->vTtNodes, pObj->Id );
    assert ( Kit_TruthSupport(pTruth, nInputs) == Kit_BitMask(nLeaves) );
    // compare the truth tables
    assert (Kit_TruthIsEqual( pTruth, pInOut, nInputs ) );

//    Extra_PrintBinary( stdout, pInOut, 8 ); printf( "\n" );

    // look up in the hash table and increase the hit number of the functional class
2441 2442 2443
    if(s_pMan->nAddedFuncs > 2 * s_pMan->nBins)
        Abc_NtkRecRezieHash(s_pMan);
    ppSpot = Abc_NtkRecTableLookup(s_pMan, s_pMan->pBins, s_pMan->nBins, pTruth, nInputs );
2444 2445 2446 2447 2448

    // if not new nodes were added and the node has a CO fanout
    if ( nNodesBeg == Abc_NtkObjNumMax(pAig) && Abc_NodeFindCoFanout(pObj) != NULL )
    {
        s_pMan->nFilterSame++;
2449
        //assert(*ppSpot != NULL);
2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473
        return;
    }

    // create PO for this node
    pObjPo = Abc_NtkCreatePo(pAig);
    Abc_ObjAddFanin( pObjPo, pObj );

    // assign the name to this PO
    sprintf( Name, "%d_%06d", nLeaves, Abc_NtkPoNum(pAig) );
    if ( (nInputs <= 6) && 0 )
    {
        Extra_PrintHexadecimalString( Truth, pInOut, nInputs );
        sprintf( Buffer, "%s_%s", Name, Truth );
    }
    else
    {
        sprintf( Buffer, "%s", Name );
    }
    Abc_ObjAssignName( pObjPo, Buffer, NULL );

    // add the resulting truth table to the hash table 
    Abc_NtkRecInsertToLookUpTable(s_pMan, ppSpot, pObj, nLeaves, s_pMan->fTrim);
}

2474 2475
/**Function*************************************************************

2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501
  Synopsis    [Prints one AIG sugraph recursively.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Abc_RecPrint_rec( Abc_Obj_t * pObj )
{
    if ( Abc_ObjIsPi(pObj) )
    {
        printf( "%c", 'a' + pObj->Id - 1 );
        return;
    }
    assert( Abc_ObjIsNode(pObj) );
    printf( "(%s", Abc_ObjFaninC0(pObj)? "!" : "" );
    Abc_RecPrint_rec( Abc_ObjFanin0(pObj) );
    printf( "*%s", Abc_ObjFaninC1(pObj)? "!" : "" );
    Abc_RecPrint_rec( Abc_ObjFanin1(pObj) );
    printf( ")" );
}

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

2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644
  Synopsis    [back up the info of the cut.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Abc_NtkRecBackUpCut(If_Cut_t* pCut)
{
    int i;
    s_pMan->tempUsign = pCut->uSign;
    s_pMan->tempNleaves = pCut->nLeaves;
    for (i = 0; i < (int)pCut->nLeaves; i++)
        s_pMan->pTempleaves[i] = pCut->pLeaves[i];
    Kit_TruthCopy(s_pMan->pTempTruth, pCut->pTruth, s_pMan->nVars);    
}

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

  Synopsis    [restore the info of the cut.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Abc_NtkRecRestoreCut(If_Cut_t* pCut)
{
    int i;
    pCut->uSign = s_pMan->tempUsign;
    pCut->nLeaves = s_pMan->tempNleaves;
    for (i = 0; i < (int)pCut->nLeaves; i++)
        pCut->pLeaves[i] = s_pMan->pTempleaves[i];
    Kit_TruthCopy(pCut->pTruth ,s_pMan->pTempTruth, s_pMan->nVars);    
}

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

  Synopsis    [compute current cut's area.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Abc_NtkRecCurrentUnMark_rec(If_Obj_t * pObj)
{
    pObj = If_Regular(pObj);
    if(pObj->fMark == 0)
        return;
    if(pObj->pFanin0)
        Abc_NtkRecCurrentUnMark_rec(If_ObjFanin0(pObj));
    if(pObj->pFanin1)
        Abc_NtkRecCurrentUnMark_rec(If_ObjFanin1(pObj));
    pObj->fMark = 0;
    return;
}

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

  Synopsis    [compute current cut's area.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Abc_NtkRecCurrentMarkAndCount_rec(If_Obj_t * pObj)
{
    int Area0, Area1, Area;
    pObj = If_Regular(pObj);
    if(pObj->fMark == 1)
        return 0;
    Area0 = Abc_NtkRecCurrentMarkAndCount_rec(If_ObjFanin0(pObj));
    Area1 = Abc_NtkRecCurrentMarkAndCount_rec(If_ObjFanin1(pObj));
    Area = Area1 + Area0 + 1;
    pObj->fMark = 1;
    return Area;
}

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

  Synopsis    [compute current cut's area.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Abc_NtkRecCurrentAera(If_Man_t* p, If_Cut_t* pCut, If_Obj_t * pRoot)
{
    int Area, i;
    If_Obj_t * pLeaf;
    Vec_PtrClear( s_pMan->vNodes );
    If_CutForEachLeaf( p, pCut, pLeaf, i )
    {
        Vec_PtrPush( s_pMan->vNodes, pLeaf );
        assert( pLeaf->fMark == 0 );
        pLeaf->fMark = 1;
    }

    // collect other nodes
    Abc_NtkRecCollectNodes_rec( pRoot, s_pMan->vNodes );
    Vec_PtrForEachEntry( If_Obj_t *, s_pMan->vNodes, pLeaf, i )
        pLeaf->fMark = 0;
    If_CutForEachLeaf(p, pCut, pLeaf, i)
        pLeaf->fMark = 1;
    Area  = Abc_NtkRecCurrentMarkAndCount_rec(pRoot);
    Abc_NtkRecCurrentUnMark_rec(pRoot);
    return Area;
}

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

  Synopsis    [compute current cut's delay.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
char Abc_NtkRecCurrentDepth_rec(If_Obj_t * pObj, int iLeaf)
{
    char Depth0, Depth1, Depth;
    pObj = If_Regular(pObj);
    if(pObj->Id == iLeaf)
        return 0;
    if(pObj->fMark)
        return -IF_BIG_CHAR;
    Depth0 = Abc_NtkRecCurrentDepth_rec(If_ObjFanin0(pObj), iLeaf);
    Depth1 = Abc_NtkRecCurrentDepth_rec(If_ObjFanin1(pObj), iLeaf);
2645
    Depth = Abc_MaxInt(Depth0, Depth1);
2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712
    Depth = (Depth == -IF_BIG_CHAR) ? -IF_BIG_CHAR : Depth + 1;
    assert(Depth <= 127);
    return Depth;
}

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

  Synopsis    [compute current cut's delay.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Abc_NtkRecCurrentDepth(If_Man_t* p, If_Cut_t* pCut, If_Obj_t * pRoot)
{
    int i;
    If_Obj_t * pLeaf;
    If_CutForEachLeaf(p, pCut, pLeaf, i)
        pLeaf->fMark = 1;
    If_CutForEachLeaf(p, pCut, pLeaf, i)
        s_pMan->pTempDepths[i] = Abc_NtkRecCurrentDepth_rec(pRoot, pLeaf->Id);
    If_CutForEachLeaf(p, pCut, pLeaf, i)
        pLeaf->fMark = 0;
}

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

  Synopsis    [compute current cut's delay.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Abc_NtkRecCurrentDelay(If_Man_t* p, If_Cut_t* pCut, If_Obj_t * pRoot)
{  
    If_Obj_t* pLeaf;
    int i, delayTemp, delayMax = -ABC_INFINITY;
    Abc_NtkRecCurrentDepth(p , pCut, pRoot);
    If_CutForEachLeaf(p, pCut, pLeaf, i)
    {
        delayTemp = s_pMan->pTempDepths[i] + If_ObjCutBest(pLeaf)->Delay;
        if(delayTemp > delayMax)
            delayMax = delayTemp;
    }
    // plus each pin's delay with its pin-to-output delay, the biggest one is the delay of the structure.
    return delayMax;
}

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

  Synopsis    [compute current cut's delay and area.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Abc_NtkRecComputCurrentStructure(If_Man_t* p, If_Cut_t* pCut, If_Obj_t * pRoot)
{
2713 2714 2715 2716
//     if (pRoot->Id == 78)
//     {
//         int  a = 1;
//     }
2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761
    
    s_pMan->currentCost = Abc_NtkRecCurrentAera(p, pCut, pRoot);
    s_pMan->currentDelay = Abc_NtkRecCurrentDelay(p, pCut, pRoot);
}

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

  Synopsis    [the cut not found in the library.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void SetUselessCut(If_Cut_t* pCut)
{
    int i;
    pCut->fUseless = 1;
    pCut->fUser = 1;
    pCut->Cost = s_pMan->currentCost;
    for (i = 0; i < (int)pCut->nLeaves; i++)
        pCut->pPerm[i] = s_pMan->pTempDepths[i];
    return;   
}

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

  Synopsis    [the cut not found in the library.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void SetUseCut(If_Cut_t* pCut, Rec_Obj_t * pRecObj, char * pCanonPerm)
{
    int i;
    pCut->fUseless = 0;
    pCut->fUser = 1;
    pCut->Cost = pRecObj->cost;
    for (i = 0; i < (int)pCut->nLeaves; i++)
2762
        pCut->pPerm[(int)pCanonPerm[i]] = pRecObj->pinToPinDelay[i];
2763 2764 2765 2766 2767 2768
    return;  

}

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

2769 2770 2771 2772 2773 2774 2775 2776 2777
  Synopsis    [Computes the  delay using library.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
2778
int If_CutDelayRecCost(If_Man_t* p, If_Cut_t* pCut, If_Obj_t * pObj)
2779
{
2780
    //int fVerbose = 0;
2781
    clock_t timeDelayComput, timeTotal = clock(), timeCanonicize;
2782
    int nLeaves, i, DelayMin = ABC_INFINITY , * pDelayBest = &DelayMin;
2783 2784 2785
    char pCanonPerm[16];
    unsigned uCanonPhase;
    unsigned* pTruthRec;
2786
    Rec_Obj_t *pCandMin;
2787
    //Abc_Ntk_t *pAig = s_pMan->pNtk;
2788 2789
    unsigned *pInOut = s_pMan->pTemp1;
    unsigned *pTemp = s_pMan->pTemp2;
2790
    int nVars = s_pMan->nVars;
2791
    //int Counter;
2792 2793 2794 2795 2796 2797 2798 2799
    assert( s_pMan != NULL );
    nLeaves = If_CutLeaveNum(pCut);
    s_pMan->nFunsTried++;
    assert( nLeaves >= 2 && nLeaves <= nVars );
    Kit_TruthCopy(pInOut, If_CutTruth(pCut), nLeaves);
    //if not every variables are in the support, skip this cut.
    if ( Kit_TruthSupport(pInOut, nLeaves) != Kit_BitMask(nLeaves) )
    {
2800
        DelayMin = 0;
2801
        //s_pMan->nFunsFilteredBysupport++;
2802
        pCut->fUser = 1;
2803
        pCut->fUseless = 0;
2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816
        pCut->Cost = 1;
        for (i = 0; i < nLeaves; i++)
        {
            if(Kit_TruthVarInSupport( pInOut, nLeaves, i ))
            {
                pCut->pPerm[i] = 0;
                DelayMin = If_ObjCutBest(If_ManObj( p, pCut->pLeaves[i]))->Delay;
            }
            else
                pCut->pPerm[i] = IF_BIG_CHAR;
        }
            
        return DelayMin;
2817 2818 2819 2820 2821 2822 2823
    }
    timeCanonicize = clock();
    //canonicize
    for (i = 0; i < nLeaves; i++)
        pCanonPerm[i] = i;
    uCanonPhase = Kit_TruthSemiCanonicize(pInOut, pTemp, nLeaves, pCanonPerm, (short*)s_pMan->pMints);
    If_CutTruthStretch(pInOut, nLeaves, nVars);
2824
    s_pMan->timeIfCanonicize += clock() - timeCanonicize;   
2825 2826 2827 2828
    timeDelayComput = clock();
    pCandMin = Abc_NtkRecLookUpBest(p, pCut, pInOut, pCanonPerm, NULL,pDelayBest);
    assert (!(pCandMin == NULL && nLeaves == 2));
    s_pMan->timeIfComputDelay += clock() - timeDelayComput;
2829
    //functional class not found in the library.
2830
    if ( pCandMin == NULL )
Alan Mishchenko committed
2831
    {
2832 2833 2834
        s_pMan->nFunsNotFound++;        
        pCut->Cost = IF_COST_MAX;
        pCut->fUser = 1;
2835
        pCut->fUseless = 1;
2836
        return ABC_INFINITY;
Alan Mishchenko committed
2837
    }
2838 2839
    s_pMan->nFunsFound++; 
    // make sure the truth table is the same
2840
    pTruthRec = (unsigned*)Vec_PtrEntry( s_pMan->vTtNodes, pCandMin->Id );
2841
    if ( !Kit_TruthIsEqualWithPhase( pTruthRec, pInOut, nLeaves ) )
Alan Mishchenko committed
2842
    {
2843 2844
        assert( 0 );
        s_pMan->nIfMapError++;  
2845
        return -1;
Alan Mishchenko committed
2846
    }
2847 2848
    // mark as user cut.
    pCut->fUser = 1;
2849
    
2850

2851 2852
//     if ( fVerbose )
//         Kit_DsdPrintFromTruth( pInOut, nLeaves ), printf( "   Subgraphs: " );
2853

2854
    //find the best structure of the functional class.
2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882
//     Counter = 0;
//     for ( pCand = *ppSpot; pCand; pCand = pCand->pNext )
//     {
//         Counter++;
//         if ( fVerbose )
//         {
//             printf( "%s(", Abc_ObjIsComplement(pCand->obj)? "!" : "" );
//             Abc_RecPrint_rec( Abc_ObjRegular(pCand->obj) );
//             printf( ")   " );
//         }
//         s_pMan->nFunsDelayComput++;
//         Delay = If_CutComputDelay(p, pCand, pCut, pCanonPerm ,nLeaves);
//         if ( DelayMin > Delay )
//         {
//             //            printf( "%d ", Cost );
//             DelayMin = Delay;
//             pCandMin = pCand;
//         }
//         else if(Delay == DelayMin)
//         {
//             if(pCand->cost < pCandMin->cost)
//                 pCandMin = pCand;
//         }
//     }
//     if ( fVerbose )
//         printf( "Printed %d subgraphs.\n", Counter );
    
    //assert( pCandMin != NULL );
2883 2884
    for ( i = 0; i < nLeaves; i++ )
    {
2885
        pCut->pPerm[(int)pCanonPerm[i]] = pCandMin->pinToPinDelay[i];
2886 2887 2888
    }
    s_pMan->timeIfTotal += clock() - timeTotal;
    pCut->Cost = pCandMin->cost;
2889 2890
        return DelayMin;
    
Alan Mishchenko committed
2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903
}

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

  Synopsis    [Labels the record AIG with the corresponding new AIG nodes.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
2904
/*Abc_Obj_t * Abc_NtkRecStrashNodeLabel_rec( Abc_Ntk_t * pNtkNew, Abc_Obj_t * pObj, int fBuild, Vec_Ptr_t * vLabels )
Alan Mishchenko committed
2905 2906 2907 2908 2909
{
    Abc_Obj_t * pFanin0New, * pFanin1New, * pLabel;
    assert( !Abc_ObjIsComplement(pObj) );
    // if this node is already visited, skip
    if ( Abc_NodeIsTravIdCurrent( pObj ) )
2910
        return (Abc_Obj_t *)Vec_PtrEntry( vLabels, pObj->Id );
Alan Mishchenko committed
2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923
    assert( Abc_ObjIsNode(pObj) );
    // mark the node as visited
    Abc_NodeSetTravIdCurrent( pObj );
    // label the fanins
    pFanin0New = Abc_NtkRecStrashNodeLabel_rec( pNtkNew, Abc_ObjFanin0(pObj), fBuild, vLabels );
    pFanin1New = Abc_NtkRecStrashNodeLabel_rec( pNtkNew, Abc_ObjFanin1(pObj), fBuild, vLabels );
    // label the node if possible
    pLabel = NULL;
    if ( pFanin0New && pFanin1New )
    {
        pFanin0New = Abc_ObjNotCond( pFanin0New, Abc_ObjFaninC0(pObj) );
        pFanin1New = Abc_ObjNotCond( pFanin1New, Abc_ObjFaninC1(pObj) );
        if ( fBuild )
2924
            pLabel = Abc_AigAnd( (Abc_Aig_t *)pNtkNew->pManFunc, pFanin0New, pFanin1New );
Alan Mishchenko committed
2925
        else
2926
            pLabel = Abc_AigAndLookup( (Abc_Aig_t *)pNtkNew->pManFunc, pFanin0New, pFanin1New );
Alan Mishchenko committed
2927 2928 2929
    }
    Vec_PtrWriteEntry( vLabels, pObj->Id, pLabel );
    return pLabel;
2930
}*/
Alan Mishchenko committed
2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942

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

  Synopsis    [Counts the area of the given node.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
2943
/*int Abc_NtkRecStrashNodeCount_rec( Abc_Obj_t * pObj, Vec_Str_t * vCosts, Vec_Ptr_t * vLabels )
Alan Mishchenko committed
2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958
{
    int Cost0, Cost1;
    if ( Vec_PtrEntry( vLabels, pObj->Id ) )
        return 0;
    assert( Abc_ObjIsNode(pObj) );
    // if this node is already visited, skip
    if ( Abc_NodeIsTravIdCurrent( pObj ) )
        return Vec_StrEntry( vCosts, pObj->Id );
    // mark the node as visited
    Abc_NodeSetTravIdCurrent( pObj );
    // count for the fanins
    Cost0 = Abc_NtkRecStrashNodeCount_rec( Abc_ObjFanin0(pObj), vCosts, vLabels );
    Cost1 = Abc_NtkRecStrashNodeCount_rec( Abc_ObjFanin1(pObj), vCosts, vLabels );
    Vec_StrWriteEntry( vCosts, pObj->Id, (char)(Cost0 + Cost1 + 1) );
    return Cost0 + Cost1 + 1;
2959
}*/
Alan Mishchenko committed
2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972

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

  Synopsis    [Strashes the given node using its local function.]

  Description [Assumes that the fanins are already strashed.
  Returns 0 if the function is not found in the table.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
2973
/*int Abc_NtkRecStrashNode( Abc_Ntk_t * pNtkNew, Abc_Obj_t * pObj, unsigned * pTruth, int nVars )
Alan Mishchenko committed
2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012
{
    char pCanonPerm[16];
    Abc_Ntk_t * pAig = s_pMan->pNtk;
    unsigned * pInOut = s_pMan->pTemp1;
    unsigned * pTemp = s_pMan->pTemp2;
    unsigned * pTruthRec;
    Abc_Obj_t * pCand, * pCandMin, * pLeaf, * pFanin, ** ppSpot;
    unsigned uCanonPhase;
    int i, nLeaves, CostMin, Cost, nOnes, fCompl;

    // check if the record works
    nLeaves = Abc_ObjFaninNum(pObj);
    assert( nLeaves >= 3 && nLeaves <= s_pMan->nVars );
    pFanin = Abc_ObjFanin0(pObj);
    assert( Abc_ObjRegular(pFanin->pCopy)->pNtk == pNtkNew );
    assert( s_pMan != NULL );
    assert( nVars == s_pMan->nVars );

    // copy the truth table
    Kit_TruthCopy( pInOut, pTruth, nVars );

    // set permutation
    for ( i = 0; i < nVars; i++ )
        pCanonPerm[i] = i;

    // canonicize the truth table
    uCanonPhase = Kit_TruthSemiCanonicize( pInOut, pTemp, nVars, pCanonPerm, (short *)s_pMan->pMints );

    // get hold of the curresponding class
    ppSpot = Abc_NtkRecTableLookup( s_pMan, pInOut, nVars );
    if ( *ppSpot == NULL )
    {
        s_pMan->nFunsNotFound++;        
//        printf( "The class of a function with %d inputs is not found.\n", nLeaves );
        return 0;
    }
    s_pMan->nFunsFound++;        

    // make sure the truth table is the same
3013
    pTruthRec = (unsigned *)Vec_PtrEntry( s_pMan->vTtNodes, (*ppSpot)->Id );
Alan Mishchenko committed
3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043
    if ( !Kit_TruthIsEqualWithPhase( pTruthRec, pInOut, nVars ) )
    {
        assert( 0 );
        return 0;
    }


    // allocate storage for costs
    if ( s_pMan->vLabels && Vec_PtrSize(s_pMan->vLabels) < Abc_NtkObjNumMax(pAig) )
    {
        Vec_PtrFree( s_pMan->vLabels );
        s_pMan->vLabels = NULL;
    }
    if ( s_pMan->vLabels == NULL )
        s_pMan->vLabels = Vec_PtrStart( Abc_NtkObjNumMax(pAig) );

    // go through the variables in the new truth table
    Abc_NtkIncrementTravId( pAig );
    for ( i = 0; i < nLeaves; i++ )
    {
        // get hold of the corresponding fanin
        pFanin = Abc_ObjFanin( pObj, pCanonPerm[i] )->pCopy;
        pFanin = Abc_ObjNotCond( pFanin, (uCanonPhase & (1 << i)) );
        // label the PI of the AIG subgraphs with this fanin
        pLeaf = Abc_NtkPi( pAig, i );
        Vec_PtrWriteEntry( s_pMan->vLabels, pLeaf->Id, pFanin );
        Abc_NodeSetTravIdCurrent( pLeaf );
    }

    // go through the candidates - and recursively label them
3044
    for ( pCand = *ppSpot; pCand; pCand = (Abc_Obj_t *)pCand->pData )
Alan Mishchenko committed
3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059
        Abc_NtkRecStrashNodeLabel_rec( pNtkNew, pCand, 0, s_pMan->vLabels );


    // allocate storage for costs
    if ( s_pMan->vCosts && Vec_StrSize(s_pMan->vCosts) < Abc_NtkObjNumMax(pAig) )
    {
        Vec_StrFree( s_pMan->vCosts );
        s_pMan->vCosts = NULL;
    }
    if ( s_pMan->vCosts == NULL )
        s_pMan->vCosts = Vec_StrStart( Abc_NtkObjNumMax(pAig) );

    // find the best subgraph
    CostMin = ABC_INFINITY;
    pCandMin = NULL;
3060
    for ( pCand = *ppSpot; pCand; pCand = (Abc_Obj_t *)pCand->pData )
Alan Mishchenko committed
3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097
    {
        // label the leaves
        Abc_NtkIncrementTravId( pAig );
        // count the number of non-labeled nodes
        Cost = Abc_NtkRecStrashNodeCount_rec( pCand, s_pMan->vCosts, s_pMan->vLabels );
        if ( CostMin > Cost )
        {
//            printf( "%d ", Cost );
            CostMin = Cost;
            pCandMin = pCand;
        }
    }
//    printf( "\n" );
    assert( pCandMin != NULL );
    if ( pCandMin == NULL )
        return 0;


    // label the leaves
    Abc_NtkIncrementTravId( pAig );
    for ( i = 0; i < nLeaves; i++ )
        Abc_NodeSetTravIdCurrent( Abc_NtkPi(pAig, i) );

    // implement the subgraph
    pObj->pCopy = Abc_NtkRecStrashNodeLabel_rec( pNtkNew, pCandMin, 1, s_pMan->vLabels );
    assert( Abc_ObjRegular(pObj->pCopy)->pNtk == pNtkNew );

    // determine phase difference
    nOnes = Kit_TruthCountOnes(pTruth, nVars);
    fCompl = (nOnes > (1<< nVars)/2);
//    assert( fCompl == ((uCanonPhase & (1 << nVars)) > 0) );

    nOnes = Kit_TruthCountOnes(pTruthRec, nVars);
    fCompl ^= (nOnes > (1<< nVars)/2);
    // complement
    pObj->pCopy = Abc_ObjNotCond( pObj->pCopy, fCompl );
    return 1;
3098 3099 3100 3101 3102
}*/




Alan Mishchenko committed
3103 3104 3105 3106 3107 3108 3109


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


3110 3111
ABC_NAMESPACE_IMPL_END