giaAig.c 23.8 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    [giaAig.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [Scalable AIG package.]

  Synopsis    [Transformation between AIG manager.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

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

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

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

Alan Mishchenko committed
21
#include "giaAig.h"
22 23 24
#include "proof/fra/fra.h"
#include "proof/dch/dch.h"
#include "opt/dar/dar.h"
25
#include "opt/dau/dau.h"
26 27 28

ABC_NAMESPACE_IMPL_START

Alan Mishchenko committed
29 30 31 32 33

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

34 35
static inline int Gia_ObjChild0Copy( Aig_Obj_t * pObj )  { return Abc_LitNotCond( Aig_ObjFanin0(pObj)->iData, Aig_ObjFaninC0(pObj) ); }
static inline int Gia_ObjChild1Copy( Aig_Obj_t * pObj )  { return Abc_LitNotCond( Aig_ObjFanin1(pObj)->iData, Aig_ObjFaninC1(pObj) ); }
Alan Mishchenko committed
36 37 38 39 40 41 42 43 44 45

static inline Aig_Obj_t * Gia_ObjChild0Copy2( Aig_Obj_t ** ppNodes, Gia_Obj_t * pObj, int Id )  { return Aig_NotCond( ppNodes[Gia_ObjFaninId0(pObj, Id)], Gia_ObjFaninC0(pObj) ); }
static inline Aig_Obj_t * Gia_ObjChild1Copy2( Aig_Obj_t ** ppNodes, Gia_Obj_t * pObj, int Id )  { return Aig_NotCond( ppNodes[Gia_ObjFaninId1(pObj, Id)], Gia_ObjFaninC1(pObj) ); }

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

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

46
  Synopsis    [Duplicates AIG in the DFS order.]
Alan Mishchenko committed
47 48 49 50 51 52 53 54

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
55
void Gia_ManFromAig_rec( Gia_Man_t * pNew, Aig_Man_t * p, Aig_Obj_t * pObj )
Alan Mishchenko committed
56
{
Alan Mishchenko committed
57 58
    Aig_Obj_t * pNext;
    if ( pObj->iData )
Alan Mishchenko committed
59 60
        return;
    assert( Aig_ObjIsNode(pObj) );
Alan Mishchenko committed
61 62
    Gia_ManFromAig_rec( pNew, p, Aig_ObjFanin0(pObj) );
    Gia_ManFromAig_rec( pNew, p, Aig_ObjFanin1(pObj) );
Alan Mishchenko committed
63
    pObj->iData = Gia_ManAppendAnd( pNew, Gia_ObjChild0Copy(pObj), Gia_ObjChild1Copy(pObj) );
Alan Mishchenko committed
64 65 66 67
    if ( p->pEquivs && (pNext = Aig_ObjEquiv(p, pObj)) )
    {
        int iObjNew, iNextNew;
        Gia_ManFromAig_rec( pNew, p, pNext );
68 69
        iObjNew  = Abc_Lit2Var(pObj->iData);
        iNextNew = Abc_Lit2Var(pNext->iData);
Alan Mishchenko committed
70 71 72
        if ( pNew->pNexts )
            pNew->pNexts[iObjNew] = iNextNew;        
    }
Alan Mishchenko committed
73
}
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
Gia_Man_t * Gia_ManFromAig( Aig_Man_t * p )
{
    Gia_Man_t * pNew;
    Aig_Obj_t * pObj;
    int i;
    // create the new manager
    pNew = Gia_ManStart( Aig_ManObjNum(p) );
    pNew->pName = Abc_UtilStrsav( p->pName );
    pNew->pSpec = Abc_UtilStrsav( p->pSpec );
    pNew->nConstrs = p->nConstrs;
    // create room to store equivalences
    if ( p->pEquivs )
        pNew->pNexts = ABC_CALLOC( int, Aig_ManObjNum(p) );
    // create the PIs
    Aig_ManCleanData( p );
    Aig_ManConst1(p)->iData = 1;
    Aig_ManForEachCi( p, pObj, i )
        pObj->iData = Gia_ManAppendCi( pNew );
    // add logic for the POs
    Aig_ManForEachCo( p, pObj, i )
        Gia_ManFromAig_rec( pNew, p, Aig_ObjFanin0(pObj) );        
    Aig_ManForEachCo( p, pObj, i )
        Gia_ManAppendCo( pNew, Gia_ObjChild0Copy(pObj) );
    Gia_ManSetRegNum( pNew, Aig_ManRegNum(p) );
    if ( pNew->pNexts )
        Gia_ManDeriveReprs( pNew );
    return pNew;
}
Alan Mishchenko committed
102 103 104

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

105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
  Synopsis    [Checks integrity of choice nodes.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Gia_ManCheckChoices_rec( Gia_Man_t * p, Gia_Obj_t * pObj )
{
    if ( !pObj || !Gia_ObjIsAnd(pObj) || pObj->fPhase )
        return;
    pObj->fPhase = 1;
    Gia_ManCheckChoices_rec( p, Gia_ObjFanin0(pObj) );
    Gia_ManCheckChoices_rec( p, Gia_ObjFanin1(pObj) );
    Gia_ManCheckChoices_rec( p, Gia_ObjSiblObj(p, Gia_ObjId(p, pObj)) );
}
void Gia_ManCheckChoices( Gia_Man_t * p )
{
    Gia_Obj_t * pObj;
    int i, fFound = 0;
    Gia_ManCleanPhase( p );
    Gia_ManForEachCo( p, pObj, i )
        Gia_ManCheckChoices_rec( p, Gia_ObjFanin0(pObj) );
    Gia_ManForEachAnd( p, pObj, i )
        if ( !pObj->fPhase )
            printf( "Object %d is dangling.\n", i ), fFound = 1;
    if ( !fFound )
        printf( "There are no dangling objects.\n" );
    Gia_ManCleanPhase( p );
}

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

Alan Mishchenko committed
140 141 142 143 144 145 146 147 148
  Synopsis    [Duplicates AIG in the DFS order.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
void Gia_ManFromAigChoices_rec( Gia_Man_t * pNew, Aig_Man_t * p, Aig_Obj_t * pObj )
{
    if ( pObj == NULL || pObj->iData )
        return;
    assert( Aig_ObjIsNode(pObj) );
    Gia_ManFromAigChoices_rec( pNew, p, Aig_ObjFanin0(pObj) );
    Gia_ManFromAigChoices_rec( pNew, p, Aig_ObjFanin1(pObj) );
    Gia_ManFromAigChoices_rec( pNew, p, Aig_ObjEquiv(p, pObj) );
    pObj->iData = Gia_ManAppendAnd( pNew, Gia_ObjChild0Copy(pObj), Gia_ObjChild1Copy(pObj) );
    if ( Aig_ObjEquiv(p, pObj) )
    {
        int iObjNew, iNextNew;
        iObjNew  = Abc_Lit2Var(pObj->iData);
        iNextNew = Abc_Lit2Var(Aig_ObjEquiv(p, pObj)->iData);
        assert( iObjNew > iNextNew );
        assert( Gia_ObjIsAnd(Gia_ManObj(pNew, iNextNew)) );
        pNew->pSibls[iObjNew] = iNextNew;        
    }
}
Gia_Man_t * Gia_ManFromAigChoices( Aig_Man_t * p )
Alan Mishchenko committed
169 170 171 172
{
    Gia_Man_t * pNew;
    Aig_Obj_t * pObj;
    int i;
173
    assert( p->pEquivs != NULL );
Alan Mishchenko committed
174 175
    // create the new manager
    pNew = Gia_ManStart( Aig_ManObjNum(p) );
176
    pNew->pName = Abc_UtilStrsav( p->pName );
177
    pNew->pSpec = Abc_UtilStrsav( p->pSpec );
178
    pNew->nConstrs = p->nConstrs;
Alan Mishchenko committed
179
    // create room to store equivalences
180
    pNew->pSibls = ABC_CALLOC( int, Aig_ManObjNum(p) );
Alan Mishchenko committed
181 182 183
    // create the PIs
    Aig_ManCleanData( p );
    Aig_ManConst1(p)->iData = 1;
184
    Aig_ManForEachCi( p, pObj, i )
Alan Mishchenko committed
185
        pObj->iData = Gia_ManAppendCi( pNew );
Alan Mishchenko committed
186
    // add logic for the POs
187
    Aig_ManForEachCo( p, pObj, i )
188
        Gia_ManFromAigChoices_rec( pNew, p, Aig_ObjFanin0(pObj) );        
189
    Aig_ManForEachCo( p, pObj, i )
Alan Mishchenko committed
190 191
        Gia_ManAppendCo( pNew, Gia_ObjChild0Copy(pObj) );
    Gia_ManSetRegNum( pNew, Aig_ManRegNum(p) );
192
    //assert( Gia_ManObjNum(pNew) == Aig_ManObjNum(p) );
193
    //Gia_ManCheckChoices( pNew );
Alan Mishchenko committed
194 195 196 197 198
    return pNew;
}

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

Alan Mishchenko committed
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
  Synopsis    [Duplicates AIG in the DFS order.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Gia_Man_t * Gia_ManFromAigSimple( Aig_Man_t * p )
{
    Gia_Man_t * pNew;
    Aig_Obj_t * pObj;
    int i;
    // create the new manager
    pNew = Gia_ManStart( Aig_ManObjNum(p) );
215
    pNew->pName = Abc_UtilStrsav( p->pName );
216
    pNew->pSpec = Abc_UtilStrsav( p->pSpec );
217
    pNew->nConstrs = p->nConstrs;
Alan Mishchenko committed
218 219 220 221 222 223
    // create the PIs
    Aig_ManCleanData( p );
    Aig_ManForEachObj( p, pObj, i )
    {
        if ( Aig_ObjIsAnd(pObj) )
            pObj->iData = Gia_ManAppendAnd( pNew, Gia_ObjChild0Copy(pObj), Gia_ObjChild1Copy(pObj) );
224
        else if ( Aig_ObjIsCi(pObj) )
Alan Mishchenko committed
225
            pObj->iData = Gia_ManAppendCi( pNew );
226
        else if ( Aig_ObjIsCo(pObj) )
Alan Mishchenko committed
227 228 229 230 231 232 233 234 235 236 237 238
            pObj->iData = Gia_ManAppendCo( pNew, Gia_ObjChild0Copy(pObj) );
        else if ( Aig_ObjIsConst1(pObj) )
            pObj->iData = 1;
        else
            assert( 0 );
    }
    Gia_ManSetRegNum( pNew, Aig_ManRegNum(p) );
    return pNew;
}

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

Alan Mishchenko committed
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
  Synopsis    [Handles choices as additional combinational outputs.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Gia_Man_t * Gia_ManFromAigSwitch( Aig_Man_t * p )
{
    Gia_Man_t * pNew;
    Aig_Obj_t * pObj;
    int i;
    // create the new manager
    pNew = Gia_ManStart( Aig_ManObjNum(p) );
255
    pNew->pName = Abc_UtilStrsav( p->pName );
256
    pNew->pSpec = Abc_UtilStrsav( p->pSpec );
257
    pNew->nConstrs = p->nConstrs;
Alan Mishchenko committed
258 259 260
    // create the PIs
    Aig_ManCleanData( p );
    Aig_ManConst1(p)->iData = 1;
261
    Aig_ManForEachCi( p, pObj, i )
Alan Mishchenko committed
262 263 264 265 266
        pObj->iData = Gia_ManAppendCi( pNew );
    // add POs corresponding to the nodes with choices
    Aig_ManForEachNode( p, pObj, i )
        if ( Aig_ObjRefs(pObj) == 0 )
        {
Alan Mishchenko committed
267
            Gia_ManFromAig_rec( pNew, p, pObj );        
Alan Mishchenko committed
268 269 270
            Gia_ManAppendCo( pNew, pObj->iData );
        }
    // add logic for the POs
271
    Aig_ManForEachCo( p, pObj, i )
Alan Mishchenko committed
272
        Gia_ManFromAig_rec( pNew, p, Aig_ObjFanin0(pObj) );        
273
    Aig_ManForEachCo( p, pObj, i )
274
        pObj->iData = Gia_ManAppendCo( pNew, Gia_ObjChild0Copy(pObj) );
Alan Mishchenko committed
275 276 277 278 279 280
    Gia_ManSetRegNum( pNew, Aig_ManRegNum(p) );
    return pNew;
}

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

281
  Synopsis    [Duplicates AIG in the DFS order.]
Alan Mishchenko committed
282 283 284 285 286 287 288 289 290 291

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Gia_ManToAig_rec( Aig_Man_t * pNew, Aig_Obj_t ** ppNodes, Gia_Man_t * p, Gia_Obj_t * pObj )
{
Alan Mishchenko committed
292
    Gia_Obj_t * pNext;
Alan Mishchenko committed
293 294 295
    if ( ppNodes[Gia_ObjId(p, pObj)] )
        return;
    if ( Gia_ObjIsCi(pObj) )
296
        ppNodes[Gia_ObjId(p, pObj)] = Aig_ObjCreateCi( pNew );
Alan Mishchenko committed
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
    else
    {
        assert( Gia_ObjIsAnd(pObj) );
        Gia_ManToAig_rec( pNew, ppNodes, p, Gia_ObjFanin0(pObj) );
        Gia_ManToAig_rec( pNew, ppNodes, p, Gia_ObjFanin1(pObj) );
        ppNodes[Gia_ObjId(p, pObj)] = Aig_And( pNew, Gia_ObjChild0Copy2(ppNodes, pObj, Gia_ObjId(p, pObj)), Gia_ObjChild1Copy2(ppNodes, pObj, Gia_ObjId(p, pObj)) );
    }
    if ( pNew->pEquivs && (pNext = Gia_ObjNextObj(p, Gia_ObjId(p, pObj))) )
    {
        Aig_Obj_t * pObjNew, * pNextNew;
        Gia_ManToAig_rec( pNew, ppNodes, p, pNext );
        pObjNew  = ppNodes[Gia_ObjId(p, pObj)];
        pNextNew = ppNodes[Gia_ObjId(p, pNext)];
        if ( pNew->pEquivs )
            pNew->pEquivs[Aig_Regular(pObjNew)->Id] = Aig_Regular(pNextNew);        
Alan Mishchenko committed
312 313
    }
}
Alan Mishchenko committed
314
Aig_Man_t * Gia_ManToAig( Gia_Man_t * p, int fChoices )
Alan Mishchenko committed
315 316 317 318 319
{
    Aig_Man_t * pNew;
    Aig_Obj_t ** ppNodes;
    Gia_Obj_t * pObj;
    int i;
Alan Mishchenko committed
320
    assert( !fChoices || (p->pNexts && p->pReprs) );
Alan Mishchenko committed
321 322
    // create the new manager
    pNew = Aig_ManStart( Gia_ManAndNum(p) );
323
    pNew->pName = Abc_UtilStrsav( p->pName );
324
    pNew->pSpec = Abc_UtilStrsav( p->pSpec );
325
    pNew->nConstrs = p->nConstrs;
326
//    pNew->pSpec = Abc_UtilStrsav( p->pName );
Alan Mishchenko committed
327 328 329
    // duplicate representation of choice nodes
    if ( fChoices )
        pNew->pEquivs = ABC_CALLOC( Aig_Obj_t *, Gia_ManObjNum(p) );
Alan Mishchenko committed
330
    // create the PIs
Alan Mishchenko committed
331
    ppNodes = ABC_CALLOC( Aig_Obj_t *, Gia_ManObjNum(p) );
Alan Mishchenko committed
332 333
    ppNodes[0] = Aig_ManConst0(pNew);
    Gia_ManForEachCi( p, pObj, i )
334
        ppNodes[Gia_ObjId(p, pObj)] = Aig_ObjCreateCi( pNew );
335 336 337 338
    // transfer level
    if ( p->vLevels )
    Gia_ManForEachCi( p, pObj, i )
        Aig_ObjSetLevel( ppNodes[Gia_ObjId(p, pObj)], Gia_ObjLevel(p, pObj) );
Alan Mishchenko committed
339 340 341 342
    // add logic for the POs
    Gia_ManForEachCo( p, pObj, i )
    {
        Gia_ManToAig_rec( pNew, ppNodes, p, Gia_ObjFanin0(pObj) );        
343
        ppNodes[Gia_ObjId(p, pObj)] = Aig_ObjCreateCo( pNew, Gia_ObjChild0Copy2(ppNodes, pObj, Gia_ObjId(p, pObj)) );
Alan Mishchenko committed
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
    }
    Aig_ManSetRegNum( pNew, Gia_ManRegNum(p) );
    ABC_FREE( ppNodes );
    return pNew;
}

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

  Synopsis    [Duplicates AIG in the DFS order.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Aig_Man_t * Gia_ManToAigSkip( Gia_Man_t * p, int nOutDelta )
{
    Aig_Man_t * pNew;
    Aig_Obj_t ** ppNodes;
    Gia_Obj_t * pObj;
    int i;
    assert( p->pNexts == NULL && p->pReprs == NULL );
    assert( nOutDelta > 0 && Gia_ManCoNum(p) % nOutDelta == 0 );
    // create the new manager
    pNew = Aig_ManStart( Gia_ManAndNum(p) );
371
    pNew->pName = Abc_UtilStrsav( p->pName );
372
    pNew->pSpec = Abc_UtilStrsav( p->pSpec );
373
    pNew->nConstrs = p->nConstrs;
374
//    pNew->pSpec = Abc_UtilStrsav( p->pName );
Alan Mishchenko committed
375 376 377 378
    // create the PIs
    ppNodes = ABC_CALLOC( Aig_Obj_t *, Gia_ManObjNum(p) );
    ppNodes[0] = Aig_ManConst0(pNew);
    Gia_ManForEachCi( p, pObj, i )
379
        ppNodes[Gia_ObjId(p, pObj)] = Aig_ObjCreateCi( pNew );
Alan Mishchenko committed
380 381 382 383 384 385
    // add logic for the POs
    Gia_ManForEachCo( p, pObj, i )
    {
        Gia_ManToAig_rec( pNew, ppNodes, p, Gia_ObjFanin0(pObj) );        
        if ( i % nOutDelta != 0 )
            continue;
386
        ppNodes[Gia_ObjId(p, pObj)] = Aig_ObjCreateCo( pNew, Gia_ObjChild0Copy2(ppNodes, pObj, Gia_ObjId(p, pObj)) );
Alan Mishchenko committed
387 388 389 390 391 392
    }
    Aig_ManSetRegNum( pNew, Gia_ManRegNum(p) );
    ABC_FREE( ppNodes );
    return pNew;
}

Alan Mishchenko committed
393 394 395 396 397 398 399 400 401 402 403
/**Function*************************************************************

  Synopsis    [Duplicates AIG in the DFS order.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
404 405 406 407 408 409
Aig_Man_t * Gia_ManToAigSimple( Gia_Man_t * p )
{
    Aig_Man_t * pNew;
    Aig_Obj_t ** ppNodes;
    Gia_Obj_t * pObj;
    int i;
410
    ppNodes = ABC_FALLOC( Aig_Obj_t *, Gia_ManObjNum(p) );
411 412
    // create the new manager
    pNew = Aig_ManStart( Gia_ManObjNum(p) );
413
    pNew->pName = Abc_UtilStrsav( p->pName );
414
    pNew->pSpec = Abc_UtilStrsav( p->pSpec );
415 416 417 418 419 420 421
    pNew->nConstrs = p->nConstrs;
    // create the PIs
    Gia_ManForEachObj( p, pObj, i )
    {
        if ( Gia_ObjIsAnd(pObj) )
            ppNodes[i] = Aig_And( pNew, Gia_ObjChild0Copy2(ppNodes, pObj, Gia_ObjId(p, pObj)), Gia_ObjChild1Copy2(ppNodes, pObj, Gia_ObjId(p, pObj)) );
        else if ( Gia_ObjIsCi(pObj) )
422
            ppNodes[i] = Aig_ObjCreateCi( pNew );
423
        else if ( Gia_ObjIsCo(pObj) )
424
            ppNodes[i] = Aig_ObjCreateCo( pNew, Gia_ObjChild0Copy2(ppNodes, pObj, Gia_ObjId(p, pObj)) );
425 426 427 428
        else if ( Gia_ObjIsConst0(pObj) )
            ppNodes[i] = Aig_ManConst0(pNew);
        else
            assert( 0 );
429
        pObj->Value = Abc_Var2Lit( Aig_ObjId(Aig_Regular(ppNodes[i])), Aig_IsComplement(ppNodes[i]) );
Alan Mishchenko committed
430
        assert( i == 0 || Aig_ObjId(ppNodes[i]) == i );
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
    }
    Aig_ManSetRegNum( pNew, Gia_ManRegNum(p) );
    ABC_FREE( ppNodes );
    return pNew;
}

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

  Synopsis    [Duplicates AIG in the DFS order.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
448 449 450 451 452 453 454
Aig_Man_t * Gia_ManCofactorAig( Aig_Man_t * p, int nFrames, int nCofFanLit )
{
    Aig_Man_t * pMan;
    Gia_Man_t * pGia, * pTemp;
    pGia = Gia_ManFromAig( p );
    pGia = Gia_ManUnrollAndCofactor( pTemp = pGia, nFrames, nCofFanLit, 1 );
    Gia_ManStop( pTemp );
Alan Mishchenko committed
455
    pMan = Gia_ManToAig( pGia, 0 );
Alan Mishchenko committed
456 457 458 459
    Gia_ManStop( pGia );
    return pMan;
}

Alan Mishchenko committed
460 461 462 463 464

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

  Synopsis    [Transfers representatives from pGia to pAig.]

465
  Description [Assumes that pGia was created from pAig.]
Alan Mishchenko committed
466 467 468 469 470 471
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
472
void Gia_ManReprToAigRepr( Aig_Man_t * pAig, Gia_Man_t * pGia )
Alan Mishchenko committed
473 474 475 476
{
    Aig_Obj_t * pObj;
    Gia_Obj_t * pGiaObj, * pGiaRepr;
    int i;
477
    assert( pAig->pReprs == NULL );
Alan Mishchenko committed
478 479
    assert( pGia->pReprs != NULL );
    // move pointers from AIG to GIA
480
    Aig_ManForEachObj( pAig, pObj, i )
Alan Mishchenko committed
481
    {
482 483
        assert( i == 0 || !Abc_LitIsCompl(pObj->iData) );
        pGiaObj = Gia_ManObj( pGia, Abc_Lit2Var(pObj->iData) );
Alan Mishchenko committed
484 485 486
        pGiaObj->Value = i;
    }
    // set the pointers to the nodes in AIG
487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503
    Aig_ManReprStart( pAig, Aig_ManObjNumMax(pAig) );
    Gia_ManForEachObj( pGia, pGiaObj, i )
    {
        pGiaRepr = Gia_ObjReprObj( pGia, i );
        if ( pGiaRepr == NULL )
            continue;
        Aig_ObjCreateRepr( pAig, Aig_ManObj(pAig, pGiaRepr->Value), Aig_ManObj(pAig, pGiaObj->Value) );
    }
}
void Gia_ManReprToAigRepr2( Aig_Man_t * pAig, Gia_Man_t * pGia )
{
    Gia_Obj_t * pGiaObj, * pGiaRepr;
    int i;
    assert( pAig->pReprs == NULL );
    assert( pGia->pReprs != NULL );
    // set the pointers to the nodes in AIG
    Aig_ManReprStart( pAig, Aig_ManObjNumMax(pAig) );
Alan Mishchenko committed
504 505 506 507 508
    Gia_ManForEachObj( pGia, pGiaObj, i )
    {
        pGiaRepr = Gia_ObjReprObj( pGia, i );
        if ( pGiaRepr == NULL )
            continue;
509
        Aig_ObjCreateRepr( pAig, Aig_ManObj(pAig, Abc_Lit2Var(pGiaRepr->Value)), Aig_ManObj(pAig, Abc_Lit2Var(pGiaObj->Value)) );
Alan Mishchenko committed
510 511 512
    }
}

Alan Mishchenko committed
513 514
/**Function*************************************************************

515
  Synopsis    [Transfers representatives from pAig to pGia.]
Alan Mishchenko committed
516 517 518 519 520 521 522 523

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
524
void Gia_ManReprFromAigRepr( Aig_Man_t * pAig, Gia_Man_t * pGia )
Alan Mishchenko committed
525
{
526 527 528 529 530
    Gia_Obj_t * pObjGia; 
    Aig_Obj_t * pObjAig, * pReprAig;
    int i;
    assert( pAig->pReprs != NULL );
    assert( pGia->pReprs == NULL );
531
    assert( Gia_ManObjNum(pGia) - Gia_ManCoNum(pGia) == Aig_ManObjNum(pAig) - Aig_ManCoNum(pAig) );
532 533 534
    pGia->pReprs = ABC_CALLOC( Gia_Rpr_t, Gia_ManObjNum(pGia) );
    for ( i = 0; i < Gia_ManObjNum(pGia); i++ )
        Gia_ObjSetRepr( pGia, i, GIA_VOID );
535
    // move pointers from GIA to AIG
536 537 538 539
    Gia_ManForEachObj( pGia, pObjGia, i )
    {
        if ( Gia_ObjIsCo(pObjGia) )
            continue;
540 541
        assert( i == 0 || !Abc_LitIsCompl(Gia_ObjValue(pObjGia)) );
        pObjAig  = Aig_ManObj( pAig, Abc_Lit2Var(Gia_ObjValue(pObjGia)) );
542 543 544 545
        pObjAig->iData = i;
    }
    Aig_ManForEachObj( pAig, pObjAig, i )
    {
546
        if ( Aig_ObjIsCo(pObjAig) )
547 548 549 550 551 552 553 554
            continue;
        if ( pAig->pReprs[i] == NULL )
            continue;
        pReprAig = pAig->pReprs[i];
        Gia_ObjSetRepr( pGia, pObjAig->iData, pReprAig->iData );
    }
    pGia->pNexts = Gia_ManDeriveNexts( pGia );
}
555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575
void Gia_ManReprFromAigRepr2( Aig_Man_t * pAig, Gia_Man_t * pGia )
{
    Aig_Obj_t * pObjAig, * pReprAig;
    int i;
    assert( pAig->pReprs != NULL );
    assert( pGia->pReprs == NULL );
    assert( Gia_ManObjNum(pGia) - Gia_ManCoNum(pGia) == Aig_ManObjNum(pAig) - Aig_ManCoNum(pAig) );
    pGia->pReprs = ABC_CALLOC( Gia_Rpr_t, Gia_ManObjNum(pGia) );
    for ( i = 0; i < Gia_ManObjNum(pGia); i++ )
        Gia_ObjSetRepr( pGia, i, GIA_VOID );
    Aig_ManForEachObj( pAig, pObjAig, i )
    {
        if ( Aig_ObjIsCo(pObjAig) )
            continue;
        if ( pAig->pReprs[i] == NULL )
            continue;
        pReprAig = pAig->pReprs[i];
        Gia_ObjSetRepr( pGia, Abc_Lit2Var(pObjAig->iData), Abc_Lit2Var(pReprAig->iData) );
    }
    pGia->pNexts = Gia_ManDeriveNexts( pGia );
}
576 577 578 579 580 581 582 583 584 585 586 587 588 589

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

  Synopsis    [Applies DC2 to the GIA manager.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Gia_Man_t * Gia_ManCompress2( Gia_Man_t * p, int fUpdateLevel, int fVerbose )
{
Alan Mishchenko committed
590 591
    Gia_Man_t * pGia;
    Aig_Man_t * pNew, * pTemp;
592 593
    if ( p->pManTime && p->vLevels == NULL )
        Gia_ManLevelWithBoxes( p );
Alan Mishchenko committed
594
    pNew = Gia_ManToAig( p, 0 );
595
    pNew = Dar_ManCompress2( pTemp = pNew, 1, fUpdateLevel, 1, 0, fVerbose );
Alan Mishchenko committed
596 597 598
    Aig_ManStop( pTemp );
    pGia = Gia_ManFromAig( pNew );
    Aig_ManStop( pNew );
599
    Gia_ManTransferTiming( pGia, p );
Alan Mishchenko committed
600 601 602
    return pGia;
}

603 604 605 606 607 608 609 610 611 612 613
/**Function*************************************************************

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634
int Gia_ManTestChoices( Gia_Man_t * p )
{
    Gia_Obj_t * pObj; int i;
    Vec_Int_t * vPointed = Vec_IntStart( Gia_ManObjNum(p) );
    Gia_ManForEachAnd( p, pObj, i )
        if ( Gia_ObjSibl(p, i) )
            Vec_IntWriteEntry( vPointed, Gia_ObjSibl(p, i), 1 );
    Gia_ManCreateRefs( p );
    Gia_ManForEachAnd( p, pObj, i )
        if ( Vec_IntEntry(vPointed, i) && Gia_ObjRefNumId(p, i) > 0 )
        {
            printf( "Gia_ManCheckChoices: Member %d", i );
            printf( " of a choice node has %d fanouts.\n", Gia_ObjRefNumId(p, i) );
            ABC_FREE( p->pRefs );
            Vec_IntFree( vPointed );
            return 0;
        }
    ABC_FREE( p->pRefs );
    Vec_IntFree( vPointed );
    return 1;
}
635 636
Gia_Man_t * Gia_ManPerformDch( Gia_Man_t * p, void * pPars )
{
637
    int fUseMapping = 0;
638
    Gia_Man_t * pGia, * pGia1;
639
    Aig_Man_t * pNew;
640 641
    if ( p->pManTime && p->vLevels == NULL )
        Gia_ManLevelWithBoxes( p );
642
    if ( fUseMapping && Gia_ManHasMapping(p) )
643 644 645 646 647
        pGia1 = (Gia_Man_t *)Dsm_ManDeriveGia( p, 0 );
    else
        pGia1 = Gia_ManDup( p );
    pNew = Gia_ManToAig( pGia1, 0 );
    Gia_ManStop( pGia1 );
648
    pNew = Dar_ManChoiceNew( pNew, (Dch_Pars_t *)pPars );
649 650
//    pGia = Gia_ManFromAig( pNew );
    pGia = Gia_ManFromAigChoices( pNew );
651
    Aig_ManStop( pNew );
652 653 654 655 656
    if ( !p->pManTime && !Gia_ManTestChoices(pGia) )
    {
        Gia_ManStop( pGia );
        pGia = Gia_ManDup( p );
    }
657
    Gia_ManTransferTiming( pGia, p );
658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675
    return pGia;
}

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

  Synopsis    [Computes equivalences after structural sequential cleanup.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Gia_ManSeqCleanupClasses( Gia_Man_t * p, int fConst, int fEquiv, int fVerbose )
{
    Aig_Man_t * pNew, * pTemp;
    pNew  = Gia_ManToAigSimple( p );
676
    pTemp = Aig_ManScl( pNew, fConst, fEquiv, 0, -1, -1, fVerbose, 0 );
677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694
    Gia_ManReprFromAigRepr( pNew, p );
    Aig_ManStop( pTemp );
    Aig_ManStop( pNew );
}

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

  Synopsis    [Solves SAT problem.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Gia_ManSolveSat( Gia_Man_t * p )
{
695
//    extern int Fra_FraigSat( Aig_Man_t * pMan, ABC_INT64_T nConfLimit, ABC_INT64_T nInsLimit, int fFlipBits, int fAndOuts, int fNewSolver, int fVerbose );
696
    Aig_Man_t * pNew;
697
    int RetValue;//, clk = Abc_Clock();
698
    pNew = Gia_ManToAig( p, 0 );
699
    RetValue = Fra_FraigSat( pNew, 10000000, 0, 0, 0, 0, 1, 1, 0, 0 );
700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724
    if ( RetValue == 0 )
    {
        Gia_Obj_t * pObj;
        int i, * pInit = (int *)pNew->pData;
        Gia_ManConst0(p)->fMark0 = 0;
        Gia_ManForEachPi( p, pObj, i )
            pObj->fMark0 = pInit[i];
        Gia_ManForEachAnd( p, pObj, i )
            pObj->fMark0 = (Gia_ObjFanin0(pObj)->fMark0 ^ Gia_ObjFaninC0(pObj)) & 
                           (Gia_ObjFanin1(pObj)->fMark0 ^ Gia_ObjFaninC1(pObj));
        Gia_ManForEachPo( p, pObj, i )
            pObj->fMark0 = (Gia_ObjFanin0(pObj)->fMark0 ^ Gia_ObjFaninC0(pObj));
        Gia_ManForEachPo( p, pObj, i )
            if ( pObj->fMark0 != 1 )
                break;
        if ( i != Gia_ManPoNum(p) )
            Abc_Print( 1, "Counter-example verification has failed.  " );
//        else
//            Abc_Print( 1, "Counter-example verification succeeded.  " );
    }
/*
    else if ( RetValue == 1 )
        Abc_Print( 1, "The SAT problem is unsatisfiable.  " );
    else if ( RetValue == -1 )
        Abc_Print( 1, "The SAT problem is undecided.  " );
725
    Abc_PrintTime( 1, "Time", Abc_Clock() - clk );
726 727 728 729 730 731
*/
    Aig_ManStop( pNew );
    return RetValue;
}


Alan Mishchenko committed
732 733 734 735 736
////////////////////////////////////////////////////////////////////////
///                       END OF FILE                                ///
////////////////////////////////////////////////////////////////////////


737 738
ABC_NAMESPACE_IMPL_END