mapperCreate.c 22.3 KB
Newer Older
Alan Mishchenko committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/**CFile****************************************************************

  FileName    [mapperCreate.c]

  PackageName [MVSIS 1.3: Multi-valued logic synthesis system.]

  Synopsis    [Generic technology mapping engine.]

  Author      [MVSIS Group]
  
  Affiliation [UC Berkeley]

  Date        [Ver. 2.0. Started - June 1, 2004.]

  Revision    [$Id: mapperCreate.c,v 1.15 2005/02/28 05:34:26 alanmi Exp $]

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

#include "mapperInt.h"

21 22 23
ABC_NAMESPACE_IMPL_START


Alan Mishchenko committed
24 25 26 27 28 29 30 31
////////////////////////////////////////////////////////////////////////
///                        DECLARATIONS                              ///
////////////////////////////////////////////////////////////////////////

static void            Map_TableCreate( Map_Man_t * p );
static void            Map_TableResize( Map_Man_t * p );

// hash key for the structural hash table
Alan Mishchenko committed
32
static inline unsigned Map_HashKey2( Map_Node_t * p0, Map_Node_t * p1, int TableSize ) { return (unsigned)(((ABC_PTRUINT_T)(p0) + (ABC_PTRUINT_T)(p1) * 12582917) % TableSize); }
Alan Mishchenko committed
33 34

////////////////////////////////////////////////////////////////////////
Alan Mishchenko committed
35
///                     FUNCTION DEFINITIONS                         ///
Alan Mishchenko committed
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
////////////////////////////////////////////////////////////////////////

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

  Synopsis    [Reads parameters from the mapping manager.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int             Map_ManReadInputNum( Map_Man_t * p )                    { return p->nInputs;    }
int             Map_ManReadOutputNum( Map_Man_t * p )                   { return p->nOutputs;   }
51
int             Map_ManReadBufNum( Map_Man_t * p )                      { return Map_NodeVecReadSize(p->vMapBufs);   }
Alan Mishchenko committed
52 53
Map_Node_t **   Map_ManReadInputs ( Map_Man_t * p )                     { return p->pInputs;    }
Map_Node_t **   Map_ManReadOutputs( Map_Man_t * p )                     { return p->pOutputs;   }
54 55
Map_Node_t **   Map_ManReadBufs( Map_Man_t * p )                        { return Map_NodeVecReadArray(p->vMapBufs);  }
Map_Node_t *    Map_ManReadBufDriver( Map_Man_t * p, int i )            { return Map_ManReadBufs(p)[i]->p1;           }
Alan Mishchenko committed
56
Map_Node_t *    Map_ManReadConst1 ( Map_Man_t * p )                     { return p->pConst1;    }
57 58
Map_Time_t *    Map_ManReadInputArrivals( Map_Man_t * p )               { return p->pInputArrivals; }
Map_Time_t *    Map_ManReadOutputRequireds( Map_Man_t * p )             { return p->pOutputRequireds; }
Alan Mishchenko committed
59
Mio_Library_t * Map_ManReadGenLib ( Map_Man_t * p )                     { return p->pSuperLib->pGenlib; }
60
int             Map_ManReadVerbose( Map_Man_t * p )                     { return p->fVerbose;   }
Alan Mishchenko committed
61
float           Map_ManReadAreaFinal( Map_Man_t * p )                   { return p->AreaFinal;  }
62 63
float           Map_ManReadRequiredGlo( Map_Man_t * p )                 { return p->fRequiredGlo; }
void            Map_ManSetOutputNames( Map_Man_t * p, char ** ppNames ) { p->ppOutputNames = ppNames;}
Alan Mishchenko committed
64 65
void            Map_ManSetAreaRecovery( Map_Man_t * p, int fAreaRecovery ) { p->fAreaRecovery = fAreaRecovery;}
void            Map_ManSetDelayTarget( Map_Man_t * p, float DelayTarget ) { p->DelayTarget = DelayTarget;}
66 67
void            Map_ManSetInputArrivals( Map_Man_t * p, Map_Time_t * pArrivals )     { p->pInputArrivals = pArrivals;    }
void            Map_ManSetOutputRequireds( Map_Man_t * p, Map_Time_t * pRequireds )  { p->pOutputRequireds = pRequireds; }
68
void            Map_ManSetObeyFanoutLimits( Map_Man_t * p, int  fObeyFanoutLimits )  { p->fObeyFanoutLimits = fObeyFanoutLimits;     }
Alan Mishchenko committed
69
void            Map_ManSetNumIterations( Map_Man_t * p, int nIterations )            { p->nIterations = nIterations;     }
70 71
int             Map_ManReadFanoutViolations( Map_Man_t * p )            { return p->nFanoutViolations;   }  
void            Map_ManSetFanoutViolations( Map_Man_t * p, int nVio )   { p->nFanoutViolations = nVio;   }  
Alan Mishchenko committed
72
void            Map_ManSetChoiceNodeNum( Map_Man_t * p, int nChoiceNodes ) { p->nChoiceNodes = nChoiceNodes; }  
73 74
void            Map_ManSetChoiceNum( Map_Man_t * p, int nChoices )         { p->nChoices = nChoices;     }   
void            Map_ManSetVerbose( Map_Man_t * p, int fVerbose )           { p->fVerbose = fVerbose;     }   
Alan Mishchenko committed
75
void            Map_ManSetSwitching( Map_Man_t * p, int fSwitching )       { p->fSwitching = fSwitching; }   
76
void            Map_ManSetSkipFanout( Map_Man_t * p, int fSkipFanout )     { p->fSkipFanout = fSkipFanout; }   
77
void            Map_ManSetUseProfile( Map_Man_t * p )                      { p->fUseProfile = 1;         }   
Alan Mishchenko committed
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98

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

  Synopsis    [Reads parameters from the mapping node.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Map_Man_t *     Map_NodeReadMan( Map_Node_t * p )                     { return p->p;                  }
char *          Map_NodeReadData( Map_Node_t * p, int fPhase )        { return fPhase? p->pData1 : p->pData0;  }
int             Map_NodeReadNum( Map_Node_t * p )                     { return p->Num;                }
int             Map_NodeReadLevel( Map_Node_t * p )                   { return Map_Regular(p)->Level; }
Map_Cut_t *     Map_NodeReadCuts( Map_Node_t * p )                    { return p->pCuts;              }
Map_Cut_t *     Map_NodeReadCutBest( Map_Node_t * p, int fPhase )     { return p->pCutBest[fPhase];   }
Map_Node_t *    Map_NodeReadOne( Map_Node_t * p )                     { return p->p1;                 }
Map_Node_t *    Map_NodeReadTwo( Map_Node_t * p )                     { return p->p2;                 }
void            Map_NodeSetData( Map_Node_t * p, int fPhase, char * pData ) { if (fPhase) p->pData1 = pData; else p->pData0 = pData; }
Alan Mishchenko committed
99 100 101
void            Map_NodeSetNextE( Map_Node_t * p, Map_Node_t * pNextE )     { p->pNextE = pNextE;       }
void            Map_NodeSetRepr( Map_Node_t * p, Map_Node_t * pRepr )       { p->pRepr = pRepr;         }
void            Map_NodeSetSwitching( Map_Node_t * p, float Switching )     { p->Switching = Switching; }
Alan Mishchenko committed
102 103 104 105 106 107 108 109 110 111 112 113 114 115

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

  Synopsis    [Checks the type of the node.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int             Map_NodeIsConst( Map_Node_t * p )    {  return (Map_Regular(p))->Num == -1;    }
int             Map_NodeIsVar( Map_Node_t * p )      {  return (Map_Regular(p))->p1 == NULL && (Map_Regular(p))->Num >= 0; }
116 117
int             Map_NodeIsBuf( Map_Node_t * p )      {  return (Map_Regular(p))->p1 != NULL && (Map_Regular(p))->p2 == NULL;  }
int             Map_NodeIsAnd( Map_Node_t * p )      {  return (Map_Regular(p))->p1 != NULL && (Map_Regular(p))->p2 != NULL;  }
Alan Mishchenko committed
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
int             Map_NodeComparePhase( Map_Node_t * p1, Map_Node_t * p2 ) { assert( !Map_IsComplement(p1) ); assert( !Map_IsComplement(p2) ); return p1->fInv ^ p2->fInv; }

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

  Synopsis    [Reads parameters from the cut.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Map_Super_t *   Map_CutReadSuperBest( Map_Cut_t * p, int fPhase ) { return p->M[fPhase].pSuperBest;}
Map_Super_t *   Map_CutReadSuper0( Map_Cut_t * p )                { return p->M[0].pSuperBest;}
Map_Super_t *   Map_CutReadSuper1( Map_Cut_t * p )                { return p->M[1].pSuperBest;}
int             Map_CutReadLeavesNum( Map_Cut_t * p )             { return p->nLeaves;  }
Map_Node_t **   Map_CutReadLeaves( Map_Cut_t * p )                { return p->ppLeaves; }
unsigned        Map_CutReadPhaseBest( Map_Cut_t * p, int fPhase ) { return p->M[fPhase].uPhaseBest;}
unsigned        Map_CutReadPhase0( Map_Cut_t * p )                { return p->M[0].uPhaseBest;}
unsigned        Map_CutReadPhase1( Map_Cut_t * p )                { return p->M[1].uPhaseBest;}
Alan Mishchenko committed
139
Map_Cut_t *     Map_CutReadNext( Map_Cut_t * p )                  { return p->pNext;          }
Alan Mishchenko committed
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191

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

  Synopsis    [Reads parameters from the supergate.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
char *          Map_SuperReadFormula( Map_Super_t * p )          {  return p->pFormula; }
Mio_Gate_t *    Map_SuperReadRoot( Map_Super_t * p )             {  return p->pRoot;    }
int             Map_SuperReadNum( Map_Super_t * p )              {  return p->Num;      }
Map_Super_t **  Map_SuperReadFanins( Map_Super_t * p )           {  return p->pFanins;  }
int             Map_SuperReadFaninNum( Map_Super_t * p )         {  return p->nFanins;  }
Map_Super_t *   Map_SuperReadNext( Map_Super_t * p )             {  return p->pNext;    }
int             Map_SuperReadNumPhases( Map_Super_t * p )        {  return p->nPhases;  }
unsigned char * Map_SuperReadPhases( Map_Super_t * p )           {  return p->uPhases;  }
int             Map_SuperReadFanoutLimit( Map_Super_t * p )      {  return p->nFanLimit;}

Mio_Library_t * Map_SuperLibReadGenLib( Map_SuperLib_t * p )     {  return p->pGenlib;  }
float           Map_SuperLibReadAreaInv( Map_SuperLib_t * p )    {  return p->AreaInv;  }
Map_Time_t      Map_SuperLibReadDelayInv( Map_SuperLib_t * p )   {  return p->tDelayInv;}
int             Map_SuperLibReadVarsMax( Map_SuperLib_t * p )    {  return p->nVarsMax; }


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

  Synopsis    [Create the mapping manager.]

  Description [The number of inputs and outputs is assumed to be
  known is advance. It is much simpler to have them fixed upfront.
  When it comes to representing the object graph in the form of
  AIG, the resulting manager is similar to the regular AIG manager, 
  except that it does not use reference counting (and therefore
  does not have garbage collections). It does have table resizing.
  The data structure is more flexible to represent additional 
  information needed for mapping.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Map_Man_t * Map_ManCreate( int nInputs, int nOutputs, int fVerbose )
{
    Map_Man_t * p;
    int i;

    // derive the supergate library
Alan Mishchenko committed
192
    if ( Abc_FrameReadLibSuper() == NULL )
Alan Mishchenko committed
193
    {
194
        printf( "The supergate library is not specified. Use \"read_super\".\n" );
Alan Mishchenko committed
195 196 197 198
        return NULL;
    }

    // start the manager
Alan Mishchenko committed
199
    p = ABC_ALLOC( Map_Man_t, 1 );
Alan Mishchenko committed
200
    memset( p, 0, sizeof(Map_Man_t) );
201
    p->pSuperLib = (Map_SuperLib_t *)Abc_FrameReadLibSuper();
Alan Mishchenko committed
202 203
    p->nVarsMax  = p->pSuperLib->nVarsMax;
    p->fVerbose  = fVerbose;
Alan Mishchenko committed
204
    p->fEpsilon  = (float)0.001;
Alan Mishchenko committed
205 206
    assert( p->nVarsMax > 0 );

Alan Mishchenko committed
207
    if ( p->nVarsMax == 5 )
Alan Mishchenko committed
208
        Extra_Truth4VarN( &p->uCanons, &p->uPhases, &p->pCounters, 8 );
Alan Mishchenko committed
209

Alan Mishchenko committed
210 211 212 213 214 215 216 217 218 219 220 221
    // start various data structures
    Map_TableCreate( p );
    Map_MappingSetupTruthTables( p->uTruths );
    Map_MappingSetupTruthTablesLarge( p->uTruthsLarge );
//    printf( "Node = %d bytes. Cut = %d bytes. Super = %d bytes.\n", sizeof(Map_Node_t), sizeof(Map_Cut_t), sizeof(Map_Super_t) ); 
    p->mmNodes    = Extra_MmFixedStart( sizeof(Map_Node_t) );
    p->mmCuts     = Extra_MmFixedStart( sizeof(Map_Cut_t) );

    // make sure the constant node will get index -1
    p->nNodes = -1;
    // create the constant node
    p->pConst1    = Map_NodeCreate( p, NULL, NULL );
222 223
    p->vMapObjs   = Map_NodeVecAlloc( 100 );
    p->vMapBufs   = Map_NodeVecAlloc( 100 );
Alan Mishchenko committed
224
    p->vVisited   = Map_NodeVecAlloc( 100 );
Alan Mishchenko committed
225 226 227

    // create the PI nodes
    p->nInputs = nInputs;
Alan Mishchenko committed
228
    p->pInputs = ABC_ALLOC( Map_Node_t *, nInputs );
Alan Mishchenko committed
229 230 231 232 233
    for ( i = 0; i < nInputs; i++ )
        p->pInputs[i] = Map_NodeCreate( p, NULL, NULL );

    // create the place for the output nodes
    p->nOutputs = nOutputs;
Alan Mishchenko committed
234
    p->pOutputs = ABC_ALLOC( Map_Node_t *, nOutputs );
Alan Mishchenko committed
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
    memset( p->pOutputs, 0, sizeof(Map_Node_t *) * nOutputs );
    return p;
}

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

  Synopsis    [Deallocates the mapping manager.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Map_ManFree( Map_Man_t * p )
{
//    int i;
253 254
//    for ( i = 0; i < p->vMapObjs->nSize; i++ )
//        Map_NodeVecFree( p->vMapObjs->pArray[i]->vFanouts );
Alan Mishchenko committed
255
//    Map_NodeVecFree( p->pConst1->vFanouts );
256 257 258
    Map_NodeVecFree( p->vMapObjs );
    Map_NodeVecFree( p->vMapBufs );
    Map_NodeVecFree( p->vVisited );
Alan Mishchenko committed
259 260 261
    if ( p->uCanons )   ABC_FREE( p->uCanons );
    if ( p->uPhases )   ABC_FREE( p->uPhases );
    if ( p->pCounters ) ABC_FREE( p->pCounters );
Alan Mishchenko committed
262 263
    Extra_MmFixedStop( p->mmNodes );
    Extra_MmFixedStop( p->mmCuts );
264
    ABC_FREE( p->pNodeDelays );
Alan Mishchenko committed
265
    ABC_FREE( p->pInputArrivals );
266
    ABC_FREE( p->pOutputRequireds );
Alan Mishchenko committed
267 268 269 270 271
    ABC_FREE( p->pInputs );
    ABC_FREE( p->pOutputs );
    ABC_FREE( p->pBins );
    ABC_FREE( p->ppOutputNames );
    ABC_FREE( p );
Alan Mishchenko committed
272 273 274 275 276
}


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

277 278 279 280 281 282 283 284 285 286 287 288 289 290
  Synopsis    [Creates node delays.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Map_ManCreateNodeDelays( Map_Man_t * p, int LogFan )
{
    Map_Node_t * pNode;
    int k;
    assert( p->pNodeDelays == NULL );
291 292
    p->pNodeDelays = ABC_CALLOC( float, p->vMapObjs->nSize );
    for ( k = 0; k < p->vMapObjs->nSize; k++ )
293
    {
294
        pNode = p->vMapObjs->pArray[k];
295 296 297 298 299 300 301 302 303 304 305
        if ( pNode->nRefs == 0 )
            continue;
        p->pNodeDelays[k] = 0.014426 * LogFan * p->pSuperLib->tDelayInv.Worst * log( (double)pNode->nRefs ); // 1.4426 = 1/ln(2)
//        printf( "%d = %d (%.2f)  ", k, pNode->nRefs, p->pNodeDelays[k] );
    }
//    printf( "\n" );
}


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

Alan Mishchenko committed
306 307 308 309 310 311 312 313 314 315 316 317 318
  Synopsis    [Deallocates the mapping manager.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Map_ManPrintTimeStats( Map_Man_t * p )
{
    printf( "N-canonical = %d. Matchings = %d.  Phases = %d.  ", p->nCanons, p->nMatches, p->nPhases );
    printf( "Choice nodes = %d. Choices = %d.\n", p->nChoiceNodes, p->nChoices );
Alan Mishchenko committed
319 320 321 322 323 324 325 326 327 328 329
    ABC_PRT( "ToMap", p->timeToMap );
    ABC_PRT( "Cuts ", p->timeCuts  );
    ABC_PRT( "Truth", p->timeTruth );
    ABC_PRT( "Match", p->timeMatch );
    ABC_PRT( "Area ", p->timeArea  );
    ABC_PRT( "Sweep", p->timeSweep );
    ABC_PRT( "ToNet", p->timeToNet );
    ABC_PRT( "TOTAL", p->timeTotal );
    if ( p->time1 ) { ABC_PRT( "time1", p->time1 ); }
    if ( p->time2 ) { ABC_PRT( "time2", p->time2 ); }
    if ( p->time3 ) { ABC_PRT( "time3", p->time3 ); }
Alan Mishchenko committed
330 331 332 333 334 335 336 337 338 339 340 341 342
}

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

  Synopsis    [Prints the mapping stats.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
343
void Map_ManPrintStatsToFile( char * pName, float Area, float Delay, abctime Time )
Alan Mishchenko committed
344 345
{
    FILE * pTable;
Alan Mishchenko committed
346
    pTable = fopen( "map_stats.txt", "a+" );
Alan Mishchenko committed
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
    fprintf( pTable, "%s ", pName );
    fprintf( pTable, "%4.2f ", Area );
    fprintf( pTable, "%4.2f ", Delay );
    fprintf( pTable, "%4.2f\n", (float)(Time)/(float)(CLOCKS_PER_SEC) );
    fclose( pTable );
}

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

  Synopsis    [Creates a new node.]

  Description [This procedure should be called to create the constant
  node and the PI nodes first.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Map_Node_t * Map_NodeCreate( Map_Man_t * p, Map_Node_t * p1, Map_Node_t * p2 )
{
    Map_Node_t * pNode;
    // create the node
    pNode = (Map_Node_t *)Extra_MmFixedEntryFetch( p->mmNodes );
    memset( pNode, 0, sizeof(Map_Node_t) );
    pNode->tRequired[0].Rise = pNode->tRequired[0].Fall = pNode->tRequired[0].Worst = MAP_FLOAT_LARGE;
    pNode->tRequired[1].Rise = pNode->tRequired[1].Fall = pNode->tRequired[1].Worst = MAP_FLOAT_LARGE;
    pNode->p1  = p1; 
    pNode->p2  = p2;
    pNode->p = p;
    // set the number of this node
    pNode->Num = p->nNodes++;
    // place to store the fanouts
//    pNode->vFanouts = Map_NodeVecAlloc( 5 );
    // store this node in the internal array
    if ( pNode->Num >= 0 )
383
        Map_NodeVecPush( p->vMapObjs, pNode );
Alan Mishchenko committed
384 385 386 387 388
    else
        pNode->fInv = 1;
    // set the level of this node
    if ( p1 ) 
    {
Alan Mishchenko committed
389
#ifdef MAP_ALLOCATE_FANOUT
Alan Mishchenko committed
390 391
        // create the fanout info
        Map_NodeAddFaninFanout( Map_Regular(p1), pNode );
392
        if ( p2 )
Alan Mishchenko committed
393
        Map_NodeAddFaninFanout( Map_Regular(p2), pNode );
Alan Mishchenko committed
394
#endif
395 396 397 398 399 400 401 402 403 404 405

        if ( p2 )
        {
            pNode->Level = 1 + MAP_MAX(Map_Regular(pNode->p1)->Level, Map_Regular(pNode->p2)->Level);
            pNode->fInv  = Map_NodeIsSimComplement(p1) & Map_NodeIsSimComplement(p2);
        }
        else
        {
            pNode->Level = Map_Regular(pNode->p1)->Level;
            pNode->fInv  = Map_NodeIsSimComplement(p1);
        }
Alan Mishchenko committed
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428
    }
    // reference the inputs (will be used to compute the number of fanouts)
    if ( p1 ) Map_NodeRef(p1);
    if ( p2 ) Map_NodeRef(p2);

    pNode->nRefEst[0] = pNode->nRefEst[1] = -1;
    return pNode;
}

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

  Synopsis    [Create the unique table of AND gates.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Map_TableCreate( Map_Man_t * pMan )
{
    assert( pMan->pBins == NULL );
429
    pMan->nBins = Abc_PrimeCudd(5000);
Alan Mishchenko committed
430
    pMan->pBins = ABC_ALLOC( Map_Node_t *, pMan->nBins );
Alan Mishchenko committed
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
    memset( pMan->pBins, 0, sizeof(Map_Node_t *) * pMan->nBins );
    pMan->nNodes = 0;
}

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

  Synopsis    [Looks up the AND2 node in the unique table.]

  Description [This procedure implements one-level hashing. All the nodes
  are hashed by their children. If the node with the same children was already
  created, it is returned by the call to this procedure. If it does not exist,
  this procedure creates a new node with these children. ]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
449
Map_Node_t * Map_NodeAnd( Map_Man_t * pMan, Map_Node_t * p1, Map_Node_t * p2 )
Alan Mishchenko committed
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507
{
    Map_Node_t * pEnt;
    unsigned Key;

    if ( p1 == p2 )
        return p1;
    if ( p1 == Map_Not(p2) )
        return Map_Not(pMan->pConst1);
    if ( Map_NodeIsConst(p1) )
    {
        if ( p1 == pMan->pConst1 )
            return p2;
        return Map_Not(pMan->pConst1);
    }
    if ( Map_NodeIsConst(p2) )
    {
        if ( p2 == pMan->pConst1 )
            return p1;
        return Map_Not(pMan->pConst1);
    }

    if ( Map_Regular(p1)->Num > Map_Regular(p2)->Num )
        pEnt = p1, p1 = p2, p2 = pEnt;

    Key = Map_HashKey2( p1, p2, pMan->nBins );
    for ( pEnt = pMan->pBins[Key]; pEnt; pEnt = pEnt->pNext )
        if ( pEnt->p1 == p1 && pEnt->p2 == p2 )
            return pEnt;
    // resize the table
    if ( pMan->nNodes >= 2 * pMan->nBins )
    {
        Map_TableResize( pMan );
        Key = Map_HashKey2( p1, p2, pMan->nBins );
    }
    // create the new node
    pEnt = Map_NodeCreate( pMan, p1, p2 );
    // add the node to the corresponding linked list in the table
    pEnt->pNext = pMan->pBins[Key];
    pMan->pBins[Key] = pEnt;
    return pEnt;
}


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

  Synopsis    [Resizes the table.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Map_TableResize( Map_Man_t * pMan )
{
    Map_Node_t ** pBinsNew;
    Map_Node_t * pEnt, * pEnt2;
508
    int nBinsNew, Counter, i;
509
    abctime clk;
Alan Mishchenko committed
510 511
    unsigned Key;

512
clk = Abc_Clock();
Alan Mishchenko committed
513
    // get the new table size
514
    nBinsNew = Abc_PrimeCudd(2 * pMan->nBins); 
Alan Mishchenko committed
515
    // allocate a new array
Alan Mishchenko committed
516
    pBinsNew = ABC_ALLOC( Map_Node_t *, nBinsNew );
Alan Mishchenko committed
517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532
    memset( pBinsNew, 0, sizeof(Map_Node_t *) * nBinsNew );
    // rehash the entries from the old table
    Counter = 0;
    for ( i = 0; i < pMan->nBins; i++ )
        for ( pEnt = pMan->pBins[i], pEnt2 = pEnt? pEnt->pNext: NULL; pEnt; 
              pEnt = pEnt2, pEnt2 = pEnt? pEnt->pNext: NULL )
        {
            Key = Map_HashKey2( pEnt->p1, pEnt->p2, nBinsNew );
            pEnt->pNext = pBinsNew[Key];
            pBinsNew[Key] = pEnt;
            Counter++;
        }
    assert( Counter == pMan->nNodes - pMan->nInputs );
    if ( pMan->fVerbose )
    {
//        printf( "Increasing the unique table size from %6d to %6d. ", pMan->nBins, nBinsNew );
533
//        ABC_PRT( "Time", Abc_Clock() - clk );
Alan Mishchenko committed
534 535
    }
    // replace the table and the parameters
Alan Mishchenko committed
536
    ABC_FREE( pMan->pBins );
Alan Mishchenko committed
537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553
    pMan->pBins = pBinsNew;
    pMan->nBins = nBinsNew;
}



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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
554
Map_Node_t * Map_NodeBuf( Map_Man_t * p, Map_Node_t * p1 )
Alan Mishchenko committed
555
{
556 557
    Map_Node_t * pNode = Map_NodeCreate( p, p1, NULL );
    Map_NodeVecPush( p->vMapBufs, pNode );
Alan Mishchenko committed
558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585
    return pNode;
}

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

  Synopsis    [Sets the node to be equivalent to the given one.]

  Description [This procedure is a work-around for the equivalence check.
  Does not verify the equivalence. Use at the user's risk.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Map_NodeSetChoice( Map_Man_t * pMan, Map_Node_t * pNodeOld, Map_Node_t * pNodeNew )
{
    pNodeNew->pNextE = pNodeOld->pNextE;
    pNodeOld->pNextE = pNodeNew;
    pNodeNew->pRepr  = pNodeOld;
}



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

586 587
ABC_NAMESPACE_IMPL_END