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

  FileName    [darScript.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [DAG-aware AIG rewriting.]

  Synopsis    [Rewriting scripts.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

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

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

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

#include "darInt.h"
Alan Mishchenko committed
22 23 24
#include "dch.h"
#include "gia.h"
#include "giaAig.h"
Alan Mishchenko committed
25

26 27 28
ABC_NAMESPACE_IMPL_START


Alan Mishchenko committed
29 30 31 32 33 34 35 36 37 38
////////////////////////////////////////////////////////////////////////
///                        DECLARATIONS                              ///
////////////////////////////////////////////////////////////////////////

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

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

Alan Mishchenko committed
39 40 41 42 43 44 45 46 47 48 49 50 51 52
  Synopsis    [Performs one iteration of AIG rewriting.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Aig_Man_t * Dar_ManRewriteDefault( Aig_Man_t * pAig )
{
    Aig_Man_t * pTemp;
    Dar_RwrPar_t Pars, * pPars = &Pars;
    Dar_ManDefaultRwrParams( pPars );
Alan Mishchenko committed
53
    pAig = Aig_ManDupDfs( pAig ); 
Alan Mishchenko committed
54
    Dar_ManRewrite( pAig, pPars );
Alan Mishchenko committed
55
    pAig = Aig_ManDupDfs( pTemp = pAig ); 
Alan Mishchenko committed
56 57 58 59 60 61
    Aig_ManStop( pTemp );
    return pAig;
}

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

Alan Mishchenko committed
62
  Synopsis    [Reproduces script "rwsat".]
Alan Mishchenko committed
63 64 65

  Description []
               
Alan Mishchenko committed
66
  SideEffects [This procedure does not tighten level during restructuring.]
Alan Mishchenko committed
67 68 69 70

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
71 72
Aig_Man_t * Dar_ManRwsat( Aig_Man_t * pAig, int fBalance, int fVerbose )
//alias rwsat       "st; rw -l; b -l; rw -l; rf -l"
Alan Mishchenko committed
73 74 75 76 77 78 79 80 81
{
    Aig_Man_t * pTemp;

    Dar_RwrPar_t ParsRwr, * pParsRwr = &ParsRwr;
    Dar_RefPar_t ParsRef, * pParsRef = &ParsRef;

    Dar_ManDefaultRwrParams( pParsRwr );
    Dar_ManDefaultRefParams( pParsRef );

Alan Mishchenko committed
82 83
    pParsRwr->fUpdateLevel = 0;
    pParsRef->fUpdateLevel = 0;
Alan Mishchenko committed
84

Alan Mishchenko committed
85 86
    pParsRwr->fVerbose = fVerbose;
    pParsRef->fVerbose = fVerbose;
87
//printf( "1" );
Alan Mishchenko committed
88
    pAig = Aig_ManDupDfs( pAig ); 
Alan Mishchenko committed
89
    if ( fVerbose ) Aig_ManPrintStats( pAig );
Alan Mishchenko committed
90

91
//printf( "2" );
Alan Mishchenko committed
92 93 94 95 96 97 98
    // balance
    if ( fBalance )
    {
    pAig = Dar_ManBalance( pTemp = pAig, 0 );
    Aig_ManStop( pTemp );
    if ( fVerbose ) Aig_ManPrintStats( pAig );
    }
Alan Mishchenko committed
99 100
   
//Aig_ManDumpBlif( pAig, "inter.blif", NULL, NULL );
101
//printf( "3" );
Alan Mishchenko committed
102 103
    // rewrite
    Dar_ManRewrite( pAig, pParsRwr );
Alan Mishchenko committed
104
    pAig = Aig_ManDupDfs( pTemp = pAig ); 
Alan Mishchenko committed
105
    Aig_ManStop( pTemp );
Alan Mishchenko committed
106
    if ( fVerbose ) Aig_ManPrintStats( pAig );
Alan Mishchenko committed
107

108
//printf( "4" );
Alan Mishchenko committed
109 110
    // refactor
    Dar_ManRefactor( pAig, pParsRef );
Alan Mishchenko committed
111
    pAig = Aig_ManDupDfs( pTemp = pAig ); 
Alan Mishchenko committed
112
    Aig_ManStop( pTemp );
Alan Mishchenko committed
113
    if ( fVerbose ) Aig_ManPrintStats( pAig );
Alan Mishchenko committed
114

115
//printf( "5" );
Alan Mishchenko committed
116
    // balance
Alan Mishchenko committed
117
    if ( fBalance )
Alan Mishchenko committed
118
    {
Alan Mishchenko committed
119
    pAig = Dar_ManBalance( pTemp = pAig, 0 );
Alan Mishchenko committed
120
    Aig_ManStop( pTemp );
Alan Mishchenko committed
121
    if ( fVerbose ) Aig_ManPrintStats( pAig );
Alan Mishchenko committed
122
    }
Alan Mishchenko committed
123
    
124
//printf( "6" );
Alan Mishchenko committed
125 126
    // rewrite
    Dar_ManRewrite( pAig, pParsRwr );
Alan Mishchenko committed
127
    pAig = Aig_ManDupDfs( pTemp = pAig ); 
Alan Mishchenko committed
128
    Aig_ManStop( pTemp );
Alan Mishchenko committed
129
    if ( fVerbose ) Aig_ManPrintStats( pAig );
Alan Mishchenko committed
130

131
//printf( "7" );
Alan Mishchenko committed
132 133 134
    return pAig;
}

Alan Mishchenko committed
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
/**Function*************************************************************

  Synopsis    [Performs one iteration of AIG rewriting.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Dar_ManHaigPrintStats( Aig_Man_t * pAig )
{
    Aig_Obj_t * pObj;
    int Counter, i;
    Counter = 0;
    Aig_ManForEachNode( pAig, pObj, i )
        Counter += (pObj->pHaig != NULL);
    printf( "Total nodes = %6d. Equiv nodes = %6d.\n", Aig_ManNodeNum(pAig), Counter );
}
Alan Mishchenko committed
155 156 157 158 159 160 161 162 163 164 165 166

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

  Synopsis    [Reproduces script "compress".]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
167
Aig_Man_t * Dar_ManCompress( Aig_Man_t * pAig, int fBalance, int fUpdateLevel, int fPower, int fVerbose )
Alan Mishchenko committed
168 169 170 171 172 173 174 175 176 177 178 179 180
//alias compress2   "b -l; rw -l; rwz -l; b -l; rwz -l; b -l"
{
    Aig_Man_t * pTemp;

    Dar_RwrPar_t ParsRwr, * pParsRwr = &ParsRwr;
    Dar_RefPar_t ParsRef, * pParsRef = &ParsRef;

    Dar_ManDefaultRwrParams( pParsRwr );
    Dar_ManDefaultRefParams( pParsRef );

    pParsRwr->fUpdateLevel = fUpdateLevel;
    pParsRef->fUpdateLevel = fUpdateLevel;

Alan Mishchenko committed
181 182
    pParsRwr->fPower = fPower;

Alan Mishchenko committed
183 184
    pParsRwr->fVerbose = 0;//fVerbose;
    pParsRef->fVerbose = 0;//fVerbose;
Alan Mishchenko committed
185

Alan Mishchenko committed
186
    pAig = Aig_ManDupDfs( pAig ); 
Alan Mishchenko committed
187
    if ( fVerbose ) Aig_ManPrintStats( pAig );
Alan Mishchenko committed
188
/*
Alan Mishchenko committed
189
    // balance
Alan Mishchenko committed
190 191
    if ( fBalance )
    {
Alan Mishchenko committed
192 193 194
    pAig = Dar_ManBalance( pTemp = pAig, fUpdateLevel );
    Aig_ManStop( pTemp );
    if ( fVerbose ) Aig_ManPrintStats( pAig );
Alan Mishchenko committed
195
    }
Alan Mishchenko committed
196
*/    
Alan Mishchenko committed
197 198
    // rewrite
    Dar_ManRewrite( pAig, pParsRwr );
Alan Mishchenko committed
199
    pAig = Aig_ManDupDfs( pTemp = pAig ); 
Alan Mishchenko committed
200
    Aig_ManStop( pTemp );
Alan Mishchenko committed
201
    if ( fVerbose ) Aig_ManPrintStats( pAig );
Alan Mishchenko committed
202
    
Alan Mishchenko committed
203 204
    // refactor
    Dar_ManRefactor( pAig, pParsRef );
Alan Mishchenko committed
205
    pAig = Aig_ManDupDfs( pTemp = pAig ); 
Alan Mishchenko committed
206
    Aig_ManStop( pTemp );
Alan Mishchenko committed
207
    if ( fVerbose ) Aig_ManPrintStats( pAig );
Alan Mishchenko committed
208 209

    // balance
Alan Mishchenko committed
210 211 212
    if ( fBalance )
    {
    pAig = Dar_ManBalance( pTemp = pAig, fUpdateLevel );
Alan Mishchenko committed
213
    Aig_ManStop( pTemp );
Alan Mishchenko committed
214
    if ( fVerbose ) Aig_ManPrintStats( pAig );
Alan Mishchenko committed
215
    }
Alan Mishchenko committed
216 217 218 219 220 221

    pParsRwr->fUseZeros = 1;
    pParsRef->fUseZeros = 1;
    
    // rewrite
    Dar_ManRewrite( pAig, pParsRwr );
Alan Mishchenko committed
222
    pAig = Aig_ManDupDfs( pTemp = pAig ); 
Alan Mishchenko committed
223
    Aig_ManStop( pTemp );
Alan Mishchenko committed
224
    if ( fVerbose ) Aig_ManPrintStats( pAig );
Alan Mishchenko committed
225

Alan Mishchenko committed
226 227 228 229 230 231 232 233 234
    return pAig;
}

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

  Synopsis    [Reproduces script "compress2".]

  Description []
               
Alan Mishchenko committed
235
  SideEffects []
Alan Mishchenko committed
236 237 238 239

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
240
Aig_Man_t * Dar_ManCompress2( Aig_Man_t * pAig, int fBalance, int fUpdateLevel, int fFanout, int fPower, int fVerbose )
Alan Mishchenko committed
241
//alias compress2   "b -l; rw -l; rf -l; b -l; rw -l; rwz -l; b -l; rfz -l; rwz -l; b -l"
Alan Mishchenko committed
242 243 244 245 246 247 248 249 250
{
    Aig_Man_t * pTemp;

    Dar_RwrPar_t ParsRwr, * pParsRwr = &ParsRwr;
    Dar_RefPar_t ParsRef, * pParsRef = &ParsRef;

    Dar_ManDefaultRwrParams( pParsRwr );
    Dar_ManDefaultRefParams( pParsRef );

Alan Mishchenko committed
251 252
    pParsRwr->fUpdateLevel = fUpdateLevel;
    pParsRef->fUpdateLevel = fUpdateLevel;
Alan Mishchenko committed
253
    pParsRwr->fFanout = fFanout;
Alan Mishchenko committed
254
    pParsRwr->fPower = fPower;
Alan Mishchenko committed
255

Alan Mishchenko committed
256 257
    pParsRwr->fVerbose = 0;//fVerbose;
    pParsRef->fVerbose = 0;//fVerbose;
Alan Mishchenko committed
258

Alan Mishchenko committed
259
    pAig = Aig_ManDupDfs( pAig ); 
Alan Mishchenko committed
260
    if ( fVerbose ) Aig_ManPrintStats( pAig );
Alan Mishchenko committed
261
/*
Alan Mishchenko committed
262 263 264
    // balance
    if ( fBalance )
    {
Alan Mishchenko committed
265 266 267
    pAig = Dar_ManBalance( pTemp = pAig, fUpdateLevel );
    Aig_ManStop( pTemp );
    if ( fVerbose ) Aig_ManPrintStats( pAig );
Alan Mishchenko committed
268
    }
Alan Mishchenko committed
269
*/
Alan Mishchenko committed
270
    // rewrite
Alan Mishchenko committed
271 272
//    Dar_ManRewrite( pAig, pParsRwr );
    pParsRwr->fUpdateLevel = 0;  // disable level update
Alan Mishchenko committed
273
    Dar_ManRewrite( pAig, pParsRwr );
Alan Mishchenko committed
274 275
    pParsRwr->fUpdateLevel = fUpdateLevel;  // reenable level update if needed

Alan Mishchenko committed
276
    pAig = Aig_ManDupDfs( pTemp = pAig ); 
Alan Mishchenko committed
277
    Aig_ManStop( pTemp );
Alan Mishchenko committed
278
    if ( fVerbose ) Aig_ManPrintStats( pAig );
Alan Mishchenko committed
279 280 281
    
    // refactor
    Dar_ManRefactor( pAig, pParsRef );
Alan Mishchenko committed
282
    pAig = Aig_ManDupDfs( pTemp = pAig ); 
Alan Mishchenko committed
283
    Aig_ManStop( pTemp );
Alan Mishchenko committed
284
    if ( fVerbose ) Aig_ManPrintStats( pAig );
Alan Mishchenko committed
285 286

    // balance
Alan Mishchenko committed
287 288 289 290
//    if ( fBalance )
    {
    pAig = Dar_ManBalance( pTemp = pAig, fUpdateLevel );
    Aig_ManStop( pTemp );
Alan Mishchenko committed
291
    if ( fVerbose ) Aig_ManPrintStats( pAig );
Alan Mishchenko committed
292 293 294 295
    }
    
    // rewrite
    Dar_ManRewrite( pAig, pParsRwr );
Alan Mishchenko committed
296
    pAig = Aig_ManDupDfs( pTemp = pAig ); 
Alan Mishchenko committed
297
    Aig_ManStop( pTemp );
Alan Mishchenko committed
298
    if ( fVerbose ) Aig_ManPrintStats( pAig );
Alan Mishchenko committed
299 300 301 302 303 304

    pParsRwr->fUseZeros = 1;
    pParsRef->fUseZeros = 1;
    
    // rewrite
    Dar_ManRewrite( pAig, pParsRwr );
Alan Mishchenko committed
305
    pAig = Aig_ManDupDfs( pTemp = pAig ); 
Alan Mishchenko committed
306
    Aig_ManStop( pTemp );
Alan Mishchenko committed
307
    if ( fVerbose ) Aig_ManPrintStats( pAig );
Alan Mishchenko committed
308 309

    // balance
Alan Mishchenko committed
310 311
    if ( fBalance )
    {
Alan Mishchenko committed
312
    pAig = Dar_ManBalance( pTemp = pAig, fUpdateLevel );
Alan Mishchenko committed
313
    Aig_ManStop( pTemp );
Alan Mishchenko committed
314
    if ( fVerbose ) Aig_ManPrintStats( pAig );
Alan Mishchenko committed
315
    }
Alan Mishchenko committed
316
    
Alan Mishchenko committed
317 318
    // refactor
    Dar_ManRefactor( pAig, pParsRef );
Alan Mishchenko committed
319
    pAig = Aig_ManDupDfs( pTemp = pAig ); 
Alan Mishchenko committed
320
    Aig_ManStop( pTemp );
Alan Mishchenko committed
321
    if ( fVerbose ) Aig_ManPrintStats( pAig );
Alan Mishchenko committed
322
    
Alan Mishchenko committed
323 324
    // rewrite
    Dar_ManRewrite( pAig, pParsRwr );
Alan Mishchenko committed
325
    pAig = Aig_ManDupDfs( pTemp = pAig ); 
Alan Mishchenko committed
326
    Aig_ManStop( pTemp );
Alan Mishchenko committed
327
    if ( fVerbose ) Aig_ManPrintStats( pAig );
Alan Mishchenko committed
328

Alan Mishchenko committed
329 330 331 332 333
    // balance
    if ( fBalance )
    {
    pAig = Dar_ManBalance( pTemp = pAig, fUpdateLevel );
    Aig_ManStop( pTemp );
Alan Mishchenko committed
334
    if ( fVerbose ) Aig_ManPrintStats( pAig );
Alan Mishchenko committed
335
    }
Alan Mishchenko committed
336 337
    return pAig;
}
Alan Mishchenko committed
338

Alan Mishchenko committed
339 340 341 342 343 344 345 346 347 348 349
/**Function*************************************************************

  Synopsis    [Reproduces script "compress2".]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
350
Vec_Ptr_t * Dar_ManChoiceSynthesis( Aig_Man_t * pAig, int fBalance, int fUpdateLevel, int fPower, int fVerbose )
Alan Mishchenko committed
351 352 353 354
//alias resyn    "b; rw; rwz; b; rwz; b"
//alias resyn2   "b; rw; rf; b; rw; rwz; b; rfz; rwz; b"
{
    Vec_Ptr_t * vAigs;
Alan Mishchenko committed
355 356 357
    Aig_Obj_t * pObj;
    int i;

Alan Mishchenko committed
358
    vAigs = Vec_PtrAlloc( 3 );
Alan Mishchenko committed
359
    pAig = Aig_ManDupDfs(pAig);
Alan Mishchenko committed
360 361
    Vec_PtrPush( vAigs, pAig );

Alan Mishchenko committed
362 363
    Aig_ManForEachObj( pAig, pObj, i )
        pObj->pHaig = pObj;
Alan Mishchenko committed
364

Alan Mishchenko committed
365
    pAig = Dar_ManCompress(pAig, fBalance, fUpdateLevel, fPower, fVerbose);
Alan Mishchenko committed
366 367
    Vec_PtrPush( vAigs, pAig );
//Aig_ManPrintStats( pAig );
Alan Mishchenko committed
368

Alan Mishchenko committed
369
    Aig_ManForEachObj( pAig, pObj, i )
Alan Mishchenko committed
370 371
    {
        pObj->pNext = pObj->pHaig;
Alan Mishchenko committed
372
        pObj->pHaig = pObj;
Alan Mishchenko committed
373
    }
Alan Mishchenko committed
374

Alan Mishchenko committed
375
    pAig = Dar_ManCompress2(pAig, fBalance, fUpdateLevel, 1, fPower, fVerbose);
Alan Mishchenko committed
376 377
    Vec_PtrPush( vAigs, pAig );
//Aig_ManPrintStats( pAig );
Alan Mishchenko committed
378

379
    pAig = (Aig_Man_t *)Vec_PtrEntry( vAigs, 1 );
Alan Mishchenko committed
380 381 382
    Aig_ManForEachObj( pAig, pObj, i )
        pObj->pHaig = pObj->pNext;

Alan Mishchenko committed
383 384 385 386 387 388 389 390 391 392 393 394 395 396
    return vAigs;
}

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

  Synopsis    [Reproduces script "compress2".]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
397
Aig_Man_t * Dar_ManChoice( Aig_Man_t * pAig, int fBalance, int fUpdateLevel, int fConstruct, int nConfMax, int nLevelMax, int fVerbose )
Alan Mishchenko committed
398 399 400 401 402 403 404
{
    Aig_Man_t * pMan, * pTemp;
    Vec_Ptr_t * vAigs;
    int i, clk;

clk = clock();
//    vAigs = Dar_ManChoiceSynthesisExt();
Alan Mishchenko committed
405
    vAigs = Dar_ManChoiceSynthesis( pAig, fBalance, fUpdateLevel, 0, fVerbose );
Alan Mishchenko committed
406 407 408

    // swap the first and last network
    // this should lead to the primary choice being "better" because of synthesis
Alan Mishchenko committed
409
    // (it is also important when constructing choices)
Alan Mishchenko committed
410 411
    if ( !fConstruct )
    {
412
        pMan = (Aig_Man_t *)Vec_PtrPop( vAigs );
Alan Mishchenko committed
413 414 415
        Vec_PtrPush( vAigs, Vec_PtrEntry(vAigs,0) );
        Vec_PtrWriteEntry( vAigs, 0, pMan );
    }
Alan Mishchenko committed
416 417 418

if ( fVerbose )
{
Alan Mishchenko committed
419
ABC_PRT( "Synthesis time", clock() - clk );
Alan Mishchenko committed
420 421
}
clk = clock();
Alan Mishchenko committed
422 423 424 425
    if ( fConstruct )
        pMan = Aig_ManChoiceConstructive( vAigs, fVerbose );
    else
        pMan = Aig_ManChoicePartitioned( vAigs, 300, nConfMax, nLevelMax, fVerbose );
426
    Vec_PtrForEachEntry( Aig_Man_t *, vAigs, pTemp, i )
Alan Mishchenko committed
427 428 429 430
        Aig_ManStop( pTemp );
    Vec_PtrFree( vAigs );
if ( fVerbose )
{
Alan Mishchenko committed
431
ABC_PRT( "Choicing time ", clock() - clk );
Alan Mishchenko committed
432 433
}
    return pMan;
Alan Mishchenko committed
434
//    return NULL;
Alan Mishchenko committed
435
}
Alan Mishchenko committed
436

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

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

  Synopsis    [Reproduces script "compress".]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Aig_Man_t * Dar_NewCompress( Aig_Man_t * pAig, int fBalance, int fUpdateLevel, int fPower, int fVerbose )
//alias compress2   "b -l; rw -l; rwz -l; b -l; rwz -l; b -l"
{
    Aig_Man_t * pTemp;

    Dar_RwrPar_t ParsRwr, * pParsRwr = &ParsRwr;
    Dar_RefPar_t ParsRef, * pParsRef = &ParsRef;

    Dar_ManDefaultRwrParams( pParsRwr );
    Dar_ManDefaultRefParams( pParsRef );

    pParsRwr->fUpdateLevel = fUpdateLevel;
    pParsRef->fUpdateLevel = fUpdateLevel;

    pParsRwr->fPower = fPower;

    pParsRwr->fVerbose = 0;//fVerbose;
    pParsRef->fVerbose = 0;//fVerbose;

//    pAig = Aig_ManDupDfs( pAig ); 
    if ( fVerbose ) Aig_ManPrintStats( pAig );

    // rewrite
    Dar_ManRewrite( pAig, pParsRwr );
    pAig = Aig_ManDupDfs( pTemp = pAig ); 
    Aig_ManStop( pTemp );
    if ( fVerbose ) Aig_ManPrintStats( pAig );
    
    // refactor
    Dar_ManRefactor( pAig, pParsRef );
    pAig = Aig_ManDupDfs( pTemp = pAig ); 
    Aig_ManStop( pTemp );
    if ( fVerbose ) Aig_ManPrintStats( pAig );

    // balance
    if ( fBalance )
    {
    pAig = Dar_ManBalance( pTemp = pAig, fUpdateLevel );
    Aig_ManStop( pTemp );
    if ( fVerbose ) Aig_ManPrintStats( pAig );
    }

    pParsRwr->fUseZeros = 1;
    pParsRef->fUseZeros = 1;
    
    // rewrite
    Dar_ManRewrite( pAig, pParsRwr );
    pAig = Aig_ManDupDfs( pTemp = pAig ); 
    Aig_ManStop( pTemp );
    if ( fVerbose ) Aig_ManPrintStats( pAig );

    return pAig;
}

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

  Synopsis    [Reproduces script "compress2".]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Aig_Man_t * Dar_NewCompress2( Aig_Man_t * pAig, int fBalance, int fUpdateLevel, int fFanout, int fPower, int fLightSynth, int fVerbose )
//alias compress2   "b -l; rw -l; rf -l; b -l; rw -l; rwz -l; b -l; rfz -l; rwz -l; b -l"
{
    Aig_Man_t * pTemp;

    Dar_RwrPar_t ParsRwr, * pParsRwr = &ParsRwr;
    Dar_RefPar_t ParsRef, * pParsRef = &ParsRef;

    Dar_ManDefaultRwrParams( pParsRwr );
    Dar_ManDefaultRefParams( pParsRef );

    pParsRwr->fUpdateLevel = fUpdateLevel;
    pParsRef->fUpdateLevel = fUpdateLevel;
    pParsRwr->fFanout = fFanout;
    pParsRwr->fPower = fPower;

    pParsRwr->fVerbose = 0;//fVerbose;
    pParsRef->fVerbose = 0;//fVerbose;

//    pAig = Aig_ManDupDfs( pAig ); 
    if ( fVerbose ) Aig_ManPrintStats( pAig );

    // skip if lighter synthesis is requested
    if ( !fLightSynth )
    {
        // rewrite
Alan Mishchenko committed
540
        //Dar_ManRewrite( pAig, pParsRwr );
541
//        pParsRwr->fUpdateLevel = 0;  // disable level update  // this change was requested in July and later disabled
Alan Mishchenko committed
542
        Dar_ManRewrite( pAig, pParsRwr );
543
//        pParsRwr->fUpdateLevel = fUpdateLevel;  // reenable level update if needed
Alan Mishchenko committed
544

Alan Mishchenko committed
545 546 547 548 549 550 551 552 553 554 555 556 557 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 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 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
        pAig = Aig_ManDupDfs( pTemp = pAig ); 
        Aig_ManStop( pTemp );
        if ( fVerbose ) Aig_ManPrintStats( pAig );
    
        // refactor
        Dar_ManRefactor( pAig, pParsRef );
        pAig = Aig_ManDupDfs( pTemp = pAig ); 
        Aig_ManStop( pTemp );
        if ( fVerbose ) Aig_ManPrintStats( pAig );
    }

    // balance
    pAig = Dar_ManBalance( pTemp = pAig, fUpdateLevel );
    Aig_ManStop( pTemp );
    if ( fVerbose ) Aig_ManPrintStats( pAig );
    
    // skip if lighter synthesis is requested
    if ( !fLightSynth )
    {
        // rewrite
        Dar_ManRewrite( pAig, pParsRwr );
        pAig = Aig_ManDupDfs( pTemp = pAig ); 
        Aig_ManStop( pTemp );
        if ( fVerbose ) Aig_ManPrintStats( pAig );
    }

    pParsRwr->fUseZeros = 1;
    pParsRef->fUseZeros = 1;
    
    // rewrite
    Dar_ManRewrite( pAig, pParsRwr );
    pAig = Aig_ManDupDfs( pTemp = pAig ); 
    Aig_ManStop( pTemp );
    if ( fVerbose ) Aig_ManPrintStats( pAig );

    // skip if lighter synthesis is requested
    if ( !fLightSynth )
    {
        // balance
        if ( fBalance )
        {
        pAig = Dar_ManBalance( pTemp = pAig, fUpdateLevel );
        Aig_ManStop( pTemp );
        if ( fVerbose ) Aig_ManPrintStats( pAig );
        }
    }
    
    // refactor
    Dar_ManRefactor( pAig, pParsRef );
    pAig = Aig_ManDupDfs( pTemp = pAig ); 
    Aig_ManStop( pTemp );
    if ( fVerbose ) Aig_ManPrintStats( pAig );
    
    // skip if lighter synthesis is requested
    if ( !fLightSynth )
    {
        // rewrite
        Dar_ManRewrite( pAig, pParsRwr );
        pAig = Aig_ManDupDfs( pTemp = pAig ); 
        Aig_ManStop( pTemp );
        if ( fVerbose ) Aig_ManPrintStats( pAig );
    }

    // balance
    if ( fBalance )
    {
    pAig = Dar_ManBalance( pTemp = pAig, fUpdateLevel );
    Aig_ManStop( pTemp );
    if ( fVerbose ) Aig_ManPrintStats( pAig );
    }
    return pAig;
}

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

  Synopsis    [Reproduces script "compress2".]

  Description [Takes AIG manager, consumes it, and produces GIA manager.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Gia_Man_t * Dar_NewChoiceSynthesis( Aig_Man_t * pAig, int fBalance, int fUpdateLevel, int fPower, int fLightSynth, int fVerbose )
//alias resyn    "b; rw; rwz; b; rwz; b"
//alias resyn2   "b; rw; rf; b; rw; rwz; b; rfz; rwz; b"
{
    Vec_Ptr_t * vGias;
    Gia_Man_t * pGia, * pTemp;
    int i;

    vGias = Vec_PtrAlloc( 3 );
    pGia = Gia_ManFromAig(pAig);
    Vec_PtrPush( vGias, pGia );

    pAig = Dar_NewCompress( pAig, fBalance, fUpdateLevel, fPower, fVerbose );
    pGia = Gia_ManFromAig(pAig);
    Vec_PtrPush( vGias, pGia );
//Aig_ManPrintStats( pAig );

    pAig = Dar_NewCompress2( pAig, fBalance, fUpdateLevel, 1, fPower, fLightSynth, fVerbose );
    pGia = Gia_ManFromAig(pAig);
    Vec_PtrPush( vGias, pGia );
//Aig_ManPrintStats( pAig );

    Aig_ManStop( pAig );

    // swap around the first and the last
654
    pTemp = (Gia_Man_t *)Vec_PtrPop( vGias );
Alan Mishchenko committed
655 656 657 658 659 660
    Vec_PtrPush( vGias, Vec_PtrEntry(vGias,0) );
    Vec_PtrWriteEntry( vGias, 0, pTemp );

//    Aig_Man_t * pAig;
//    int i;
//    printf( "Choicing will be performed with %d AIGs:\n", Vec_PtrSize(p->vAigs) );
661
//    Vec_PtrForEachEntry( Aig_Man_t *, p->vAigs, pAig, i )
Alan Mishchenko committed
662 663 664
//        Aig_ManPrintStats( pAig );

    // derive the miter
665
    pGia = Gia_ManChoiceMiter( vGias );
Alan Mishchenko committed
666 667

    // cleanup
668
    Vec_PtrForEachEntry( Gia_Man_t *, vGias, pTemp, i )
Alan Mishchenko committed
669 670 671 672
        Gia_ManStop( pTemp );
    Vec_PtrFree( vGias );
    return pGia;
}
Alan Mishchenko committed
673 674 675 676 677 678 679 680 681 682 683 684

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

  Synopsis    [Reproduces script "compress2".]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
685
/*
Alan Mishchenko committed
686 687 688
Aig_Man_t * Dar_ManChoiceNew( Aig_Man_t * pAig, Dch_Pars_t * pPars )
{
    extern Aig_Man_t * Dch_ComputeChoices( Vec_Ptr_t * vAigs, Dch_Pars_t * pPars );
Alan Mishchenko committed
689
    extern Aig_Man_t * Cec_ComputeChoices( Vec_Ptr_t * vAigs, Dch_Pars_t * pPars );
Alan Mishchenko committed
690 691 692 693 694 695 696 697 698

    int fVerbose = pPars->fVerbose;
    int fConstruct = 0;
    Aig_Man_t * pMan, * pTemp;
    Vec_Ptr_t * vAigs;
    int i, clk;

clk = clock();
//    vAigs = Dar_ManChoiceSynthesisExt();
Alan Mishchenko committed
699 700
//    vAigs = Dar_ManChoiceSynthesis( pAig, 1, 1, pPars->fPower, fVerbose );
    vAigs = Dar_ManChoiceSynthesis( pAig, 1, 1, pPars->fPower, 0 );
Alan Mishchenko committed
701 702 703 704 705 706 707 708 709 710 711 712 713

    // swap the first and last network
    // this should lead to the primary choice being "better" because of synthesis
    // (it is also important when constructing choices)
    if ( !fConstruct )
    {
        pMan = Vec_PtrPop( vAigs );
        Vec_PtrPush( vAigs, Vec_PtrEntry(vAigs,0) );
        Vec_PtrWriteEntry( vAigs, 0, pMan );
    }

if ( fVerbose )
{
Alan Mishchenko committed
714
//ABC_PRT( "Synthesis time", clock() - clk );
Alan Mishchenko committed
715
}
Alan Mishchenko committed
716
    pPars->timeSynth = clock() - clk;
Alan Mishchenko committed
717 718

clk = clock();
Alan Mishchenko committed
719 720 721 722 723
    // perform choice computation
    if ( pPars->fUseGia )
        pMan = Cec_ComputeChoices( vAigs, pPars );
    else
        pMan = Dch_ComputeChoices( vAigs, pPars );
Alan Mishchenko committed
724 725 726 727 728 729 730 731 732 733 734 735 736

    // reconstruct the network
    pMan = Aig_ManDupDfsGuided( pTemp = pMan, Vec_PtrEntry(vAigs,0) );
    Aig_ManStop( pTemp );
    // duplicate the timing manager
    pTemp = Vec_PtrEntry( vAigs, 0 );
    if ( pTemp->pManTime )
    {
        extern void * Tim_ManDup( void * p, int fDiscrete );     
        pMan->pManTime = Tim_ManDup( pTemp->pManTime, 0 );
    }
    // reset levels
    Aig_ManChoiceLevel( pMan );
Alan Mishchenko committed
737 738
    ABC_FREE( pMan->pName );
    ABC_FREE( pMan->pSpec );
Alan Mishchenko committed
739 740 741 742
    pMan->pName = Aig_UtilStrsav( pTemp->pName );
    pMan->pSpec = Aig_UtilStrsav( pTemp->pSpec );

    // cleanup
743
    Vec_PtrForEachEntry( Aig_Man_t *, vAigs, pTemp, i )
Alan Mishchenko committed
744 745 746 747 748
        Aig_ManStop( pTemp );
    Vec_PtrFree( vAigs );

if ( fVerbose )
{
Alan Mishchenko committed
749
//ABC_PRT( "Choicing time ", clock() - clk );
Alan Mishchenko committed
750 751 752 753
}
    return pMan;
//    return NULL;
}
Alan Mishchenko committed
754 755 756 757 758 759 760 761 762 763 764 765 766 767 768
*/

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

  Synopsis    [Reproduces script "compress2".]

  Description [Consumes the input AIG to reduce memory usage.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Aig_Man_t * Dar_ManChoiceNewAig( Aig_Man_t * pAig, Dch_Pars_t * pPars )
{
769
//    extern Aig_Man_t * Dch_DeriveTotalAig( Vec_Ptr_t * vAigs );
Alan Mishchenko committed
770 771 772 773 774 775 776 777 778 779 780 781 782 783 784
    extern Aig_Man_t * Dch_ComputeChoices( Aig_Man_t * pAig, Dch_Pars_t * pPars );
    int fVerbose = pPars->fVerbose;
    Aig_Man_t * pMan, * pTemp;
    Vec_Ptr_t * vAigs;
    Vec_Ptr_t * vPios;
    void * pManTime;
    char * pName, * pSpec;
    int i, clk;

clk = clock();
    vAigs = Dar_ManChoiceSynthesis( pAig, 1, 1, pPars->fPower, fVerbose );
pPars->timeSynth = clock() - clk;
    // swap the first and last network
    // this should lead to the primary choice being "better" because of synthesis
    // (it is also important when constructing choices)
785
    pMan = (Aig_Man_t *)Vec_PtrPop( vAigs );
Alan Mishchenko committed
786 787 788 789 790 791
    Vec_PtrPush( vAigs, Vec_PtrEntry(vAigs,0) );
    Vec_PtrWriteEntry( vAigs, 0, pMan );

    // derive the total AIG
    pMan = Dch_DeriveTotalAig( vAigs );
    // cleanup
792
    Vec_PtrForEachEntry( Aig_Man_t *, vAigs, pTemp, i )
Alan Mishchenko committed
793 794 795 796 797 798 799 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
        Aig_ManStop( pTemp );
    Vec_PtrFree( vAigs );

    // compute choices
    pMan = Dch_ComputeChoices( pTemp = pMan, pPars );
    Aig_ManStop( pTemp );

    // save useful things
    pManTime = pAig->pManTime; pAig->pManTime = NULL;
    pName = Aig_UtilStrsav( pAig->pName );
    pSpec = Aig_UtilStrsav( pAig->pSpec );

    // create guidence
    vPios = Aig_ManOrderPios( pMan, pAig ); 
    Aig_ManStop( pAig );

    // reconstruct the network
    pMan = Aig_ManDupDfsGuided( pTemp = pMan, vPios );
    Aig_ManStop( pTemp );
    Vec_PtrFree( vPios );

    // reset levels
    pMan->pManTime = pManTime;
    Aig_ManChoiceLevel( pMan );

    // copy names
    ABC_FREE( pMan->pName );
    ABC_FREE( pMan->pSpec );
    pMan->pName = pName;
    pMan->pSpec = pSpec;
    return pMan;
}

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

  Synopsis    [Reproduces script "compress2".]

  Description [Consumes the input AIG to reduce memory usage.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Aig_Man_t * Dar_ManChoiceNew( Aig_Man_t * pAig, Dch_Pars_t * pPars )
{
    extern Aig_Man_t * Cec_ComputeChoices( Gia_Man_t * pGia, Dch_Pars_t * pPars );
840
//    extern Aig_Man_t * Dch_DeriveTotalAig( Vec_Ptr_t * vAigs );
Alan Mishchenko committed
841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856
    extern Aig_Man_t * Dch_ComputeChoices( Aig_Man_t * pAig, Dch_Pars_t * pPars );
    int fVerbose = pPars->fVerbose;
    Aig_Man_t * pMan, * pTemp;
    Gia_Man_t * pGia;
    Vec_Ptr_t * vPios;
    void * pManTime;
    char * pName, * pSpec;
    int clk;

    // save useful things
    pManTime = pAig->pManTime; pAig->pManTime = NULL;
    pName = Aig_UtilStrsav( pAig->pName );
    pSpec = Aig_UtilStrsav( pAig->pSpec );

    // perform synthesis
clk = clock();
857
    pGia = Dar_NewChoiceSynthesis( Aig_ManDupDfs(pAig), 1, 1, pPars->fPower, pPars->fLightSynth, pPars->fVerbose );
Alan Mishchenko committed
858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891
pPars->timeSynth = clock() - clk;

    // perform choice computation
    if ( pPars->fUseGia )
        pMan = Cec_ComputeChoices( pGia, pPars );
    else
    {
        pMan = Gia_ManToAigSkip( pGia, 3 );
        Gia_ManStop( pGia );
        pMan = Dch_ComputeChoices( pTemp = pMan, pPars );
        Aig_ManStop( pTemp );
    }

    // create guidence
    vPios = Aig_ManOrderPios( pMan, pAig ); 
    Aig_ManStop( pAig );

    // reconstruct the network
    pMan = Aig_ManDupDfsGuided( pTemp = pMan, vPios );
    Aig_ManStop( pTemp );
    Vec_PtrFree( vPios );

    // reset levels
    pMan->pManTime = pManTime;
    Aig_ManChoiceLevel( pMan );

    // copy names
    ABC_FREE( pMan->pName );
    ABC_FREE( pMan->pSpec );
    pMan->pName = pName;
    pMan->pSpec = pSpec;
    return pMan;
}

Alan Mishchenko committed
892

Alan Mishchenko committed
893 894 895 896 897
////////////////////////////////////////////////////////////////////////
///                       END OF FILE                                ///
////////////////////////////////////////////////////////////////////////


898 899
ABC_NAMESPACE_IMPL_END