darScript.c 26 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"
22 23 24
#include "proof/dch/dch.h"
#include "aig/gia/gia.h"
#include "aig/gia/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
{
    Aig_Man_t * pTemp;
75
    abctime Time = pAig->Time2Quit;
Alan Mishchenko committed
76 77 78 79 80 81 82

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

    Dar_ManDefaultRwrParams( pParsRwr );
    Dar_ManDefaultRefParams( pParsRef );

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

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

92
//printf( "2" );
Alan Mishchenko committed
93 94 95
    // balance
    if ( fBalance )
    {
96
    pAig->Time2Quit = Time;
Alan Mishchenko committed
97 98
    pAig = Dar_ManBalance( pTemp = pAig, 0 );
    Aig_ManStop( pTemp );
99
    if ( fVerbose ) printf( "Balance:   " ), Aig_ManPrintStats( pAig );
100 101
    if ( Time && Abc_Clock() > Time )
        { if ( pAig ) Aig_ManStop( pAig ); return NULL; }
Alan Mishchenko committed
102
    }
Alan Mishchenko committed
103 104
   
//Aig_ManDumpBlif( pAig, "inter.blif", NULL, NULL );
105
//printf( "3" );
Alan Mishchenko committed
106
    // rewrite
107
    pAig->Time2Quit = Time;
Alan Mishchenko committed
108
    Dar_ManRewrite( pAig, pParsRwr );
Alan Mishchenko committed
109
    pAig = Aig_ManDupDfs( pTemp = pAig ); 
Alan Mishchenko committed
110
    Aig_ManStop( pTemp );
111
    if ( fVerbose ) printf( "Rewrite:   " ), Aig_ManPrintStats( pAig );
112 113
    if ( Time && Abc_Clock() > Time )
        { if ( pAig ) Aig_ManStop( pAig ); return NULL; }
Alan Mishchenko committed
114

115
//printf( "4" );
Alan Mishchenko committed
116
    // refactor
117
    pAig->Time2Quit = Time;
Alan Mishchenko committed
118
    Dar_ManRefactor( pAig, pParsRef );
Alan Mishchenko committed
119
    pAig = Aig_ManDupDfs( pTemp = pAig ); 
Alan Mishchenko committed
120
    Aig_ManStop( pTemp );
121
    if ( fVerbose ) printf( "Refactor:  " ), Aig_ManPrintStats( pAig );
122 123
    if ( Time && Abc_Clock() > Time )
        { if ( pAig ) Aig_ManStop( pAig ); return NULL; }
Alan Mishchenko committed
124

125
//printf( "5" );
Alan Mishchenko committed
126
    // balance
Alan Mishchenko committed
127
    if ( fBalance )
Alan Mishchenko committed
128
    {
129
    pAig->Time2Quit = Time;
Alan Mishchenko committed
130
    pAig = Dar_ManBalance( pTemp = pAig, 0 );
Alan Mishchenko committed
131
    Aig_ManStop( pTemp );
132
    if ( fVerbose ) printf( "Balance:   " ), Aig_ManPrintStats( pAig );
133 134
    if ( Time && Abc_Clock() > Time )
        { if ( pAig ) Aig_ManStop( pAig ); return NULL; }
Alan Mishchenko committed
135
    }
136

137
//printf( "6" );
Alan Mishchenko committed
138
    // rewrite
139
    pAig->Time2Quit = Time;
Alan Mishchenko committed
140
    Dar_ManRewrite( pAig, pParsRwr );
Alan Mishchenko committed
141
    pAig = Aig_ManDupDfs( pTemp = pAig ); 
Alan Mishchenko committed
142
    Aig_ManStop( pTemp );
143
    if ( fVerbose ) printf( "Rewrite:   " ), Aig_ManPrintStats( pAig );
144 145
    if ( Time && Abc_Clock() > Time )
        { if ( pAig ) Aig_ManStop( pAig ); return NULL; }
Alan Mishchenko committed
146

147
//printf( "7" );
Alan Mishchenko committed
148 149 150
    return pAig;
}

Alan Mishchenko committed
151 152
/**Function*************************************************************

Alan Mishchenko committed
153 154 155 156 157 158 159 160 161
  Synopsis    [Reproduces script "compress".]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
162
Aig_Man_t * Dar_ManCompress( Aig_Man_t * pAig, int fBalance, int fUpdateLevel, int fPower, int fVerbose )
Alan Mishchenko committed
163 164 165 166 167 168 169 170 171 172 173 174 175
//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
176 177
    pParsRwr->fPower = fPower;

Alan Mishchenko committed
178 179
    pParsRwr->fVerbose = 0;//fVerbose;
    pParsRef->fVerbose = 0;//fVerbose;
Alan Mishchenko committed
180

Alan Mishchenko committed
181
    pAig = Aig_ManDupDfs( pAig ); 
182
    if ( fVerbose ) printf( "Starting:  " ), Aig_ManPrintStats( pAig );
Alan Mishchenko committed
183
/*
Alan Mishchenko committed
184
    // balance
Alan Mishchenko committed
185 186
    if ( fBalance )
    {
Alan Mishchenko committed
187 188
    pAig = Dar_ManBalance( pTemp = pAig, fUpdateLevel );
    Aig_ManStop( pTemp );
189
    if ( fVerbose ) printf( "Balance:   " ), Aig_ManPrintStats( pAig );
Alan Mishchenko committed
190
    }
Alan Mishchenko committed
191
*/    
Alan Mishchenko committed
192 193
    // rewrite
    Dar_ManRewrite( pAig, pParsRwr );
Alan Mishchenko committed
194
    pAig = Aig_ManDupDfs( pTemp = pAig ); 
Alan Mishchenko committed
195
    Aig_ManStop( pTemp );
196
    if ( fVerbose ) printf( "Rewrite:   " ), Aig_ManPrintStats( pAig );
Alan Mishchenko committed
197
    
Alan Mishchenko committed
198 199
    // refactor
    Dar_ManRefactor( pAig, pParsRef );
Alan Mishchenko committed
200
    pAig = Aig_ManDupDfs( pTemp = pAig ); 
Alan Mishchenko committed
201
    Aig_ManStop( pTemp );
202
    if ( fVerbose ) printf( "Refactor:  " ), Aig_ManPrintStats( pAig );
Alan Mishchenko committed
203 204

    // balance
Alan Mishchenko committed
205 206 207
    if ( fBalance )
    {
    pAig = Dar_ManBalance( pTemp = pAig, fUpdateLevel );
Alan Mishchenko committed
208
    Aig_ManStop( pTemp );
209
    if ( fVerbose ) printf( "Balance:   " ), Aig_ManPrintStats( pAig );
Alan Mishchenko committed
210
    }
Alan Mishchenko committed
211 212 213 214 215 216

    pParsRwr->fUseZeros = 1;
    pParsRef->fUseZeros = 1;
    
    // rewrite
    Dar_ManRewrite( pAig, pParsRwr );
Alan Mishchenko committed
217
    pAig = Aig_ManDupDfs( pTemp = pAig ); 
Alan Mishchenko committed
218
    Aig_ManStop( pTemp );
219
    if ( fVerbose ) printf( "RewriteZ:  " ), Aig_ManPrintStats( pAig );
Alan Mishchenko committed
220

Alan Mishchenko committed
221 222 223 224 225 226 227 228 229
    return pAig;
}

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

  Synopsis    [Reproduces script "compress2".]

  Description []
               
Alan Mishchenko committed
230
  SideEffects []
Alan Mishchenko committed
231 232 233 234

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
235
Aig_Man_t * Dar_ManCompress2( Aig_Man_t * pAig, int fBalance, int fUpdateLevel, int fFanout, int fPower, int fVerbose )
Alan Mishchenko committed
236
//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
237 238 239 240 241 242 243 244 245
{
    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
246 247
    pParsRwr->fUpdateLevel = fUpdateLevel;
    pParsRef->fUpdateLevel = fUpdateLevel;
Alan Mishchenko committed
248
    pParsRwr->fFanout = fFanout;
Alan Mishchenko committed
249
    pParsRwr->fPower = fPower;
Alan Mishchenko committed
250

Alan Mishchenko committed
251 252
    pParsRwr->fVerbose = 0;//fVerbose;
    pParsRef->fVerbose = 0;//fVerbose;
Alan Mishchenko committed
253

Alan Mishchenko committed
254
    pAig = Aig_ManDupDfs( pAig ); 
255
    if ( fVerbose ) printf( "Starting:  " ), Aig_ManPrintStats( pAig );
Alan Mishchenko committed
256
/*
Alan Mishchenko committed
257 258 259
    // balance
    if ( fBalance )
    {
Alan Mishchenko committed
260 261
    pAig = Dar_ManBalance( pTemp = pAig, fUpdateLevel );
    Aig_ManStop( pTemp );
262
    if ( fVerbose ) printf( "Balance:   " ), Aig_ManPrintStats( pAig );
Alan Mishchenko committed
263
    }
Alan Mishchenko committed
264
*/
Alan Mishchenko committed
265
    // rewrite
Alan Mishchenko committed
266 267
//    Dar_ManRewrite( pAig, pParsRwr );
    pParsRwr->fUpdateLevel = 0;  // disable level update
Alan Mishchenko committed
268
    Dar_ManRewrite( pAig, pParsRwr );
Alan Mishchenko committed
269 270
    pParsRwr->fUpdateLevel = fUpdateLevel;  // reenable level update if needed

Alan Mishchenko committed
271
    pAig = Aig_ManDupDfs( pTemp = pAig ); 
Alan Mishchenko committed
272
    Aig_ManStop( pTemp );
273
    if ( fVerbose ) printf( "Rewrite:   " ), Aig_ManPrintStats( pAig );
Alan Mishchenko committed
274 275 276
    
    // refactor
    Dar_ManRefactor( pAig, pParsRef );
Alan Mishchenko committed
277
    pAig = Aig_ManDupDfs( pTemp = pAig ); 
Alan Mishchenko committed
278
    Aig_ManStop( pTemp );
279
    if ( fVerbose ) printf( "Refactor:  " ), Aig_ManPrintStats( pAig );
Alan Mishchenko committed
280 281

    // balance
282
//    if ( fBalance )
Alan Mishchenko committed
283 284 285
    {
    pAig = Dar_ManBalance( pTemp = pAig, fUpdateLevel );
    Aig_ManStop( pTemp );
286
    if ( fVerbose ) printf( "Balance:   " ), Aig_ManPrintStats( pAig );
Alan Mishchenko committed
287 288 289 290
    }
    
    // rewrite
    Dar_ManRewrite( pAig, pParsRwr );
Alan Mishchenko committed
291
    pAig = Aig_ManDupDfs( pTemp = pAig ); 
Alan Mishchenko committed
292
    Aig_ManStop( pTemp );
293
    if ( fVerbose ) printf( "Rewrite:   " ), Aig_ManPrintStats( pAig );
Alan Mishchenko committed
294 295 296 297 298 299

    pParsRwr->fUseZeros = 1;
    pParsRef->fUseZeros = 1;
    
    // rewrite
    Dar_ManRewrite( pAig, pParsRwr );
Alan Mishchenko committed
300
    pAig = Aig_ManDupDfs( pTemp = pAig ); 
Alan Mishchenko committed
301
    Aig_ManStop( pTemp );
302
    if ( fVerbose ) printf( "RewriteZ:  " ), Aig_ManPrintStats( pAig );
Alan Mishchenko committed
303 304

    // balance
Alan Mishchenko committed
305 306
    if ( fBalance )
    {
Alan Mishchenko committed
307
    pAig = Dar_ManBalance( pTemp = pAig, fUpdateLevel );
Alan Mishchenko committed
308
    Aig_ManStop( pTemp );
309
    if ( fVerbose ) printf( "Balance:   " ), Aig_ManPrintStats( pAig );
Alan Mishchenko committed
310
    }
Alan Mishchenko committed
311
    
Alan Mishchenko committed
312 313
    // refactor
    Dar_ManRefactor( pAig, pParsRef );
Alan Mishchenko committed
314
    pAig = Aig_ManDupDfs( pTemp = pAig ); 
Alan Mishchenko committed
315
    Aig_ManStop( pTemp );
316
    if ( fVerbose ) printf( "RefactorZ: " ), Aig_ManPrintStats( pAig );
Alan Mishchenko committed
317
    
Alan Mishchenko committed
318 319
    // rewrite
    Dar_ManRewrite( pAig, pParsRwr );
Alan Mishchenko committed
320
    pAig = Aig_ManDupDfs( pTemp = pAig ); 
Alan Mishchenko committed
321
    Aig_ManStop( pTemp );
322
    if ( fVerbose ) printf( "RewriteZ:  " ), Aig_ManPrintStats( pAig );
Alan Mishchenko committed
323

Alan Mishchenko committed
324 325 326 327 328
    // balance
    if ( fBalance )
    {
    pAig = Dar_ManBalance( pTemp = pAig, fUpdateLevel );
    Aig_ManStop( pTemp );
329
    if ( fVerbose ) printf( "Balance:   " ), Aig_ManPrintStats( pAig );
Alan Mishchenko committed
330
    }
Alan Mishchenko committed
331 332
    return pAig;
}
Alan Mishchenko committed
333

Alan Mishchenko committed
334 335 336 337 338 339 340 341 342 343 344
/**Function*************************************************************

  Synopsis    [Reproduces script "compress2".]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
345
Vec_Ptr_t * Dar_ManChoiceSynthesis( Aig_Man_t * pAig, int fBalance, int fUpdateLevel, int fPower, int fVerbose )
Alan Mishchenko committed
346 347 348 349
//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
350

Alan Mishchenko committed
351
    vAigs = Vec_PtrAlloc( 3 );
Alan Mishchenko committed
352
    pAig = Aig_ManDupDfs(pAig);
Alan Mishchenko committed
353 354
    Vec_PtrPush( vAigs, pAig );

Alan Mishchenko committed
355
    pAig = Dar_ManCompress(pAig, fBalance, fUpdateLevel, fPower, fVerbose);
Alan Mishchenko committed
356 357
    Vec_PtrPush( vAigs, pAig );
//Aig_ManPrintStats( pAig );
Alan Mishchenko committed
358

Alan Mishchenko committed
359
    pAig = Dar_ManCompress2(pAig, fBalance, fUpdateLevel, 1, fPower, fVerbose);
Alan Mishchenko committed
360 361
    Vec_PtrPush( vAigs, pAig );
//Aig_ManPrintStats( pAig );
Alan Mishchenko committed
362

363
    pAig = (Aig_Man_t *)Vec_PtrEntry( vAigs, 1 );
Alan Mishchenko committed
364 365 366 367 368 369 370 371 372 373 374 375 376 377
    return vAigs;
}

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

  Synopsis    [Reproduces script "compress2".]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
378
Aig_Man_t * Dar_ManChoice( Aig_Man_t * pAig, int fBalance, int fUpdateLevel, int fConstruct, int nConfMax, int nLevelMax, int fVerbose )
Alan Mishchenko committed
379 380 381
{
    Aig_Man_t * pMan, * pTemp;
    Vec_Ptr_t * vAigs;
382
    int i;
383
    abctime clk;
Alan Mishchenko committed
384

385
clk = Abc_Clock();
Alan Mishchenko committed
386
//    vAigs = Dar_ManChoiceSynthesisExt();
Alan Mishchenko committed
387
    vAigs = Dar_ManChoiceSynthesis( pAig, fBalance, fUpdateLevel, 0, fVerbose );
Alan Mishchenko committed
388 389 390

    // swap the first and last network
    // this should lead to the primary choice being "better" because of synthesis
Alan Mishchenko committed
391
    // (it is also important when constructing choices)
Alan Mishchenko committed
392 393
    if ( !fConstruct )
    {
394
        pMan = (Aig_Man_t *)Vec_PtrPop( vAigs );
Alan Mishchenko committed
395 396 397
        Vec_PtrPush( vAigs, Vec_PtrEntry(vAigs,0) );
        Vec_PtrWriteEntry( vAigs, 0, pMan );
    }
Alan Mishchenko committed
398 399 400

if ( fVerbose )
{
401
ABC_PRT( "Synthesis time", Abc_Clock() - clk );
Alan Mishchenko committed
402
}
403
clk = Abc_Clock();
Alan Mishchenko committed
404 405 406 407
    if ( fConstruct )
        pMan = Aig_ManChoiceConstructive( vAigs, fVerbose );
    else
        pMan = Aig_ManChoicePartitioned( vAigs, 300, nConfMax, nLevelMax, fVerbose );
408
    Vec_PtrForEachEntry( Aig_Man_t *, vAigs, pTemp, i )
Alan Mishchenko committed
409 410 411 412
        Aig_ManStop( pTemp );
    Vec_PtrFree( vAigs );
if ( fVerbose )
{
413
ABC_PRT( "Choicing time ", Abc_Clock() - clk );
Alan Mishchenko committed
414 415
}
    return pMan;
Alan Mishchenko committed
416
//    return NULL;
Alan Mishchenko committed
417
}
Alan Mishchenko committed
418

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

/**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 ); 
451
    if ( fVerbose ) printf( "Starting:  " ), Aig_ManPrintStats( pAig );
Alan Mishchenko committed
452 453 454 455 456

    // rewrite
    Dar_ManRewrite( pAig, pParsRwr );
    pAig = Aig_ManDupDfs( pTemp = pAig ); 
    Aig_ManStop( pTemp );
457
    if ( fVerbose ) printf( "Rewrite:   " ), Aig_ManPrintStats( pAig );
Alan Mishchenko committed
458 459 460 461 462
    
    // refactor
    Dar_ManRefactor( pAig, pParsRef );
    pAig = Aig_ManDupDfs( pTemp = pAig ); 
    Aig_ManStop( pTemp );
463
    if ( fVerbose ) printf( "Refactor:  " ), Aig_ManPrintStats( pAig );
Alan Mishchenko committed
464 465 466 467 468 469

    // balance
    if ( fBalance )
    {
    pAig = Dar_ManBalance( pTemp = pAig, fUpdateLevel );
    Aig_ManStop( pTemp );
470
    if ( fVerbose ) printf( "Balance:   " ), Aig_ManPrintStats( pAig );
Alan Mishchenko committed
471 472 473 474 475 476 477 478 479
    }

    pParsRwr->fUseZeros = 1;
    pParsRef->fUseZeros = 1;
    
    // rewrite
    Dar_ManRewrite( pAig, pParsRwr );
    pAig = Aig_ManDupDfs( pTemp = pAig ); 
    Aig_ManStop( pTemp );
480
    if ( fVerbose ) printf( "RewriteZ:  " ), Aig_ManPrintStats( pAig );
Alan Mishchenko committed
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

    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 ); 
516
    if ( fVerbose ) printf( "Starting:  " ), Aig_ManPrintStats( pAig );
Alan Mishchenko committed
517 518 519 520 521

    // skip if lighter synthesis is requested
    if ( !fLightSynth )
    {
        // rewrite
Alan Mishchenko committed
522
        //Dar_ManRewrite( pAig, pParsRwr );
523
//        pParsRwr->fUpdateLevel = 0;  // disable level update  // this change was requested in July and later disabled
Alan Mishchenko committed
524
        Dar_ManRewrite( pAig, pParsRwr );
525
//        pParsRwr->fUpdateLevel = fUpdateLevel;  // reenable level update if needed
Alan Mishchenko committed
526

Alan Mishchenko committed
527 528
        pAig = Aig_ManDupDfs( pTemp = pAig ); 
        Aig_ManStop( pTemp );
529
        if ( fVerbose ) printf( "Rewrite:   " ), Aig_ManPrintStats( pAig );
Alan Mishchenko committed
530 531 532 533 534
    
        // refactor
        Dar_ManRefactor( pAig, pParsRef );
        pAig = Aig_ManDupDfs( pTemp = pAig ); 
        Aig_ManStop( pTemp );
535
        if ( fVerbose ) printf( "Refactor:  " ), Aig_ManPrintStats( pAig );
Alan Mishchenko committed
536 537 538 539 540
    }

    // balance
    pAig = Dar_ManBalance( pTemp = pAig, fUpdateLevel );
    Aig_ManStop( pTemp );
541
    if ( fVerbose ) printf( "Balance:   " ), Aig_ManPrintStats( pAig );
Alan Mishchenko committed
542 543 544 545 546 547 548 549
    
    // skip if lighter synthesis is requested
    if ( !fLightSynth )
    {
        // rewrite
        Dar_ManRewrite( pAig, pParsRwr );
        pAig = Aig_ManDupDfs( pTemp = pAig ); 
        Aig_ManStop( pTemp );
550
        if ( fVerbose ) printf( "Rewrite:   " ), Aig_ManPrintStats( pAig );
Alan Mishchenko committed
551 552 553 554 555 556 557 558 559
    }

    pParsRwr->fUseZeros = 1;
    pParsRef->fUseZeros = 1;
    
    // rewrite
    Dar_ManRewrite( pAig, pParsRwr );
    pAig = Aig_ManDupDfs( pTemp = pAig ); 
    Aig_ManStop( pTemp );
560
    if ( fVerbose ) printf( "RewriteZ:  " ), Aig_ManPrintStats( pAig );
Alan Mishchenko committed
561 562 563 564 565 566 567 568 569

    // skip if lighter synthesis is requested
    if ( !fLightSynth )
    {
        // balance
        if ( fBalance )
        {
        pAig = Dar_ManBalance( pTemp = pAig, fUpdateLevel );
        Aig_ManStop( pTemp );
570
        if ( fVerbose ) printf( "Balance:   " ), Aig_ManPrintStats( pAig );
Alan Mishchenko committed
571 572 573 574 575 576 577
        }
    }
    
    // refactor
    Dar_ManRefactor( pAig, pParsRef );
    pAig = Aig_ManDupDfs( pTemp = pAig ); 
    Aig_ManStop( pTemp );
578
    if ( fVerbose ) printf( "RefactorZ: " ), Aig_ManPrintStats( pAig );
Alan Mishchenko committed
579 580 581 582 583 584 585 586
    
    // skip if lighter synthesis is requested
    if ( !fLightSynth )
    {
        // rewrite
        Dar_ManRewrite( pAig, pParsRwr );
        pAig = Aig_ManDupDfs( pTemp = pAig ); 
        Aig_ManStop( pTemp );
587
        if ( fVerbose ) printf( "RewriteZ:  " ), Aig_ManPrintStats( pAig );
Alan Mishchenko committed
588 589 590 591 592 593 594
    }

    // balance
    if ( fBalance )
    {
    pAig = Dar_ManBalance( pTemp = pAig, fUpdateLevel );
    Aig_ManStop( pTemp );
595
    if ( fVerbose ) printf( "Balance:   " ), Aig_ManPrintStats( pAig );
Alan Mishchenko committed
596 597 598 599 600 601
    }
    return pAig;
}

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

602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622
  Synopsis    [Count the number of nodes with very high fanout count.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Dar_NewChoiceSynthesisGuard( Aig_Man_t * pAig )
{
    Aig_Obj_t * pObj;
    int i, Count = 0;
    Aig_ManForEachNode( pAig, pObj, i )
        if ( Aig_ObjRefs(pObj) > 1000 )
            Count += Aig_ObjRefs(pObj) / 1000;
    return (int)(Count > 10);
}

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

Alan Mishchenko committed
623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639
  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;

640 641 642 643 644 645 646
    if ( fUpdateLevel && Dar_NewChoiceSynthesisGuard(pAig) )
    {
        if ( fVerbose )
            printf( "Warning: Due to high fanout count of some nodes, level updating is disabled.\n" );
        fUpdateLevel = 0;
    }

Alan Mishchenko committed
647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663
    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
664
    pTemp = (Gia_Man_t *)Vec_PtrPop( vGias );
Alan Mishchenko committed
665 666 667 668 669 670
    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) );
671
//    Vec_PtrForEachEntry( Aig_Man_t *, p->vAigs, pAig, i )
Alan Mishchenko committed
672 673 674
//        Aig_ManPrintStats( pAig );

    // derive the miter
675
    pGia = Gia_ManChoiceMiter( vGias );
Alan Mishchenko committed
676 677

    // cleanup
678
    Vec_PtrForEachEntry( Gia_Man_t *, vGias, pTemp, i )
Alan Mishchenko committed
679 680 681 682
        Gia_ManStop( pTemp );
    Vec_PtrFree( vGias );
    return pGia;
}
Alan Mishchenko committed
683 684 685 686 687 688 689 690 691 692 693 694

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

  Synopsis    [Reproduces script "compress2".]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
695
/*
Alan Mishchenko committed
696 697 698
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
699
    extern Aig_Man_t * Cec_ComputeChoices( Vec_Ptr_t * vAigs, Dch_Pars_t * pPars );
Alan Mishchenko committed
700 701 702 703 704

    int fVerbose = pPars->fVerbose;
    int fConstruct = 0;
    Aig_Man_t * pMan, * pTemp;
    Vec_Ptr_t * vAigs;
705
    int i;
706
    abctime clk;
Alan Mishchenko committed
707

708
clk = Abc_Clock();
Alan Mishchenko committed
709
//    vAigs = Dar_ManChoiceSynthesisExt();
Alan Mishchenko committed
710 711
//    vAigs = Dar_ManChoiceSynthesis( pAig, 1, 1, pPars->fPower, fVerbose );
    vAigs = Dar_ManChoiceSynthesis( pAig, 1, 1, pPars->fPower, 0 );
Alan Mishchenko committed
712 713 714 715 716 717 718 719 720 721 722 723 724

    // 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 )
{
725
//ABC_PRT( "Synthesis time", Abc_Clock() - clk );
Alan Mishchenko committed
726
}
727
    pPars->timeSynth = Abc_Clock() - clk;
Alan Mishchenko committed
728

729
clk = Abc_Clock();
Alan Mishchenko committed
730 731 732 733 734
    // perform choice computation
    if ( pPars->fUseGia )
        pMan = Cec_ComputeChoices( vAigs, pPars );
    else
        pMan = Dch_ComputeChoices( vAigs, pPars );
Alan Mishchenko committed
735 736 737 738 739 740 741 742 743 744 745 746 747

    // 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
748 749
    ABC_FREE( pMan->pName );
    ABC_FREE( pMan->pSpec );
750 751
    pMan->pName = Abc_UtilStrsav( pTemp->pName );
    pMan->pSpec = Abc_UtilStrsav( pTemp->pSpec );
Alan Mishchenko committed
752 753

    // cleanup
754
    Vec_PtrForEachEntry( Aig_Man_t *, vAigs, pTemp, i )
Alan Mishchenko committed
755 756 757 758 759
        Aig_ManStop( pTemp );
    Vec_PtrFree( vAigs );

if ( fVerbose )
{
760
//ABC_PRT( "Choicing time ", Abc_Clock() - clk );
Alan Mishchenko committed
761 762 763 764
}
    return pMan;
//    return NULL;
}
Alan Mishchenko committed
765 766 767 768 769 770 771 772 773 774 775 776 777 778 779
*/

/**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 )
{
780
//    extern Aig_Man_t * Dch_DeriveTotalAig( Vec_Ptr_t * vAigs );
Alan Mishchenko committed
781 782 783 784 785 786 787
    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;
788
    int i;
789
    abctime clk;
Alan Mishchenko committed
790

791
clk = Abc_Clock();
Alan Mishchenko committed
792
    vAigs = Dar_ManChoiceSynthesis( pAig, 1, 1, pPars->fPower, fVerbose );
793
pPars->timeSynth = Abc_Clock() - clk;
Alan Mishchenko committed
794 795 796
    // 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)
797
    pMan = (Aig_Man_t *)Vec_PtrPop( vAigs );
Alan Mishchenko committed
798 799 800 801 802 803
    Vec_PtrPush( vAigs, Vec_PtrEntry(vAigs,0) );
    Vec_PtrWriteEntry( vAigs, 0, pMan );

    // derive the total AIG
    pMan = Dch_DeriveTotalAig( vAigs );
    // cleanup
804
    Vec_PtrForEachEntry( Aig_Man_t *, vAigs, pTemp, i )
Alan Mishchenko committed
805 806 807 808 809 810 811 812 813
        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;
814 815
    pName = Abc_UtilStrsav( pAig->pName );
    pSpec = Abc_UtilStrsav( pAig->pSpec );
Alan Mishchenko committed
816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851

    // 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 );
852
//    extern Aig_Man_t * Dch_DeriveTotalAig( Vec_Ptr_t * vAigs );
Alan Mishchenko committed
853
    extern Aig_Man_t * Dch_ComputeChoices( Aig_Man_t * pAig, Dch_Pars_t * pPars );
854
//    int fVerbose = pPars->fVerbose;
Alan Mishchenko committed
855 856 857 858 859
    Aig_Man_t * pMan, * pTemp;
    Gia_Man_t * pGia;
    Vec_Ptr_t * vPios;
    void * pManTime;
    char * pName, * pSpec;
860
    abctime clk;
Alan Mishchenko committed
861 862 863

    // save useful things
    pManTime = pAig->pManTime; pAig->pManTime = NULL;
864 865
    pName = Abc_UtilStrsav( pAig->pName );
    pSpec = Abc_UtilStrsav( pAig->pSpec );
Alan Mishchenko committed
866 867

    // perform synthesis
868
clk = Abc_Clock();
869
    pGia = Dar_NewChoiceSynthesis( Aig_ManDupDfs(pAig), 1, 1, pPars->fPower, pPars->fLightSynth, pPars->fVerbose );
870
pPars->timeSynth = Abc_Clock() - clk;
Alan Mishchenko committed
871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903

    // 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
904

Alan Mishchenko committed
905 906 907 908 909
////////////////////////////////////////////////////////////////////////
///                       END OF FILE                                ///
////////////////////////////////////////////////////////////////////////


910 911
ABC_NAMESPACE_IMPL_END