@@ -80,10 +80,9 @@ MaxRecurseDepth("max-recurse-depth", cl::Hidden, cl::init(1000), cl::ZeroOrMore,
80
80
struct llvm ::GVN::Expression {
81
81
uint32_t opcode;
82
82
Type *type;
83
- bool commutative;
84
83
SmallVector<uint32_t , 4 > varargs;
85
84
86
- Expression (uint32_t o = ~2U ) : opcode(o), commutative( false ) {}
85
+ Expression (uint32_t o = ~2U ) : opcode(o) {}
87
86
88
87
bool operator ==(const Expression &other) const {
89
88
if (opcode != other.opcode )
@@ -247,7 +246,6 @@ GVN::Expression GVN::ValueTable::createExpr(Instruction *I) {
247
246
assert (I->getNumOperands () == 2 && " Unsupported commutative instruction!" );
248
247
if (e.varargs [0 ] > e.varargs [1 ])
249
248
std::swap (e.varargs [0 ], e.varargs [1 ]);
250
- e.commutative = true ;
251
249
}
252
250
253
251
if (CmpInst *C = dyn_cast<CmpInst>(I)) {
@@ -258,7 +256,6 @@ GVN::Expression GVN::ValueTable::createExpr(Instruction *I) {
258
256
Predicate = CmpInst::getSwappedPredicate (Predicate);
259
257
}
260
258
e.opcode = (C->getOpcode () << 8 ) | Predicate;
261
- e.commutative = true ;
262
259
} else if (InsertValueInst *E = dyn_cast<InsertValueInst>(I)) {
263
260
for (InsertValueInst::idx_iterator II = E->idx_begin (), IE = E->idx_end ();
264
261
II != IE; ++II)
@@ -284,7 +281,6 @@ GVN::Expression GVN::ValueTable::createCmpExpr(unsigned Opcode,
284
281
Predicate = CmpInst::getSwappedPredicate (Predicate);
285
282
}
286
283
e.opcode = (Opcode << 8 ) | Predicate;
287
- e.commutative = true ;
288
284
return e;
289
285
}
290
286
@@ -352,25 +348,25 @@ GVN::ValueTable::~ValueTable() = default;
352
348
// / add - Insert a value into the table with a specified value number.
353
349
void GVN::ValueTable::add (Value *V, uint32_t num) {
354
350
valueNumbering.insert (std::make_pair (V, num));
355
- if (PHINode *PN = dyn_cast<PHINode>(V))
356
- NumberingPhi[num] = PN;
357
351
}
358
352
359
353
uint32_t GVN::ValueTable::lookupOrAddCall (CallInst *C) {
360
354
if (AA->doesNotAccessMemory (C)) {
361
355
Expression exp = createExpr (C);
362
- uint32_t e = assignExpNewValueNum (exp ).first ;
356
+ uint32_t &e = expressionNumbering[exp ];
357
+ if (!e) e = nextValueNumber++;
363
358
valueNumbering[C] = e;
364
359
return e;
365
360
} else if (AA->onlyReadsMemory (C)) {
366
361
Expression exp = createExpr (C);
367
- auto ValNum = assignExpNewValueNum (exp );
368
- if (ValNum.second ) {
369
- valueNumbering[C] = ValNum.first ;
370
- return ValNum.first ;
362
+ uint32_t &e = expressionNumbering[exp ];
363
+ if (!e) {
364
+ e = nextValueNumber++;
365
+ valueNumbering[C] = e;
366
+ return e;
371
367
}
372
368
if (!MD) {
373
- uint32_t e = assignExpNewValueNum ( exp ). first ;
369
+ e = nextValueNumber++ ;
374
370
valueNumbering[C] = e;
375
371
return e;
376
372
}
@@ -526,29 +522,23 @@ uint32_t GVN::ValueTable::lookupOrAdd(Value *V) {
526
522
case Instruction::ExtractValue:
527
523
exp = createExtractvalueExpr (cast<ExtractValueInst>(I));
528
524
break ;
529
- case Instruction::PHI:
530
- valueNumbering[V] = nextValueNumber;
531
- NumberingPhi[nextValueNumber] = cast<PHINode>(V);
532
- return nextValueNumber++;
533
525
default :
534
526
valueNumbering[V] = nextValueNumber;
535
527
return nextValueNumber++;
536
528
}
537
529
538
- uint32_t e = assignExpNewValueNum (exp ).first ;
530
+ uint32_t & e = expressionNumbering[exp ];
531
+ if (!e) e = nextValueNumber++;
539
532
valueNumbering[V] = e;
540
533
return e;
541
534
}
542
535
543
536
// / Returns the value number of the specified value. Fails if
544
537
// / the value has not yet been numbered.
545
- uint32_t GVN::ValueTable::lookup (Value *V, bool Verify ) const {
538
+ uint32_t GVN::ValueTable::lookup (Value *V) const {
546
539
DenseMap<Value*, uint32_t >::const_iterator VI = valueNumbering.find (V);
547
- if (Verify) {
548
- assert (VI != valueNumbering.end () && " Value not numbered?" );
549
- return VI->second ;
550
- }
551
- return (VI != valueNumbering.end ()) ? VI->second : 0 ;
540
+ assert (VI != valueNumbering.end () && " Value not numbered?" );
541
+ return VI->second ;
552
542
}
553
543
554
544
// / Returns the value number of the given comparison,
@@ -559,29 +549,21 @@ uint32_t GVN::ValueTable::lookupOrAddCmp(unsigned Opcode,
559
549
CmpInst::Predicate Predicate,
560
550
Value *LHS, Value *RHS) {
561
551
Expression exp = createCmpExpr (Opcode, Predicate, LHS, RHS);
562
- return assignExpNewValueNum (exp ).first ;
552
+ uint32_t & e = expressionNumbering[exp ];
553
+ if (!e) e = nextValueNumber++;
554
+ return e;
563
555
}
564
556
565
557
// / Remove all entries from the ValueTable.
566
558
void GVN::ValueTable::clear () {
567
559
valueNumbering.clear ();
568
560
expressionNumbering.clear ();
569
- NumberingPhi.clear ();
570
- PhiTranslateTable.clear ();
571
- BlockRPONumber.clear ();
572
561
nextValueNumber = 1 ;
573
- Expressions.clear ();
574
- ExprIdx.clear ();
575
- nextExprNumber = 0 ;
576
562
}
577
563
578
564
// / Remove a value from the value numbering.
579
565
void GVN::ValueTable::erase (Value *V) {
580
- uint32_t Num = valueNumbering.lookup (V);
581
566
valueNumbering.erase (V);
582
- // If V is PHINode, V <--> value number is an one-to-one mapping.
583
- if (isa<PHINode>(V))
584
- NumberingPhi.erase (Num);
585
567
}
586
568
587
569
// / verifyRemoved - Verify that the value is removed from all internal data
@@ -1469,97 +1451,6 @@ bool GVN::processLoad(LoadInst *L) {
1469
1451
return false ;
1470
1452
}
1471
1453
1472
- // / Return a pair the first field showing the value number of \p Exp and the
1473
- // / second field showing whether it is a value number newly created.
1474
- std::pair<uint32_t , bool >
1475
- GVN::ValueTable::assignExpNewValueNum (Expression &Exp) {
1476
- uint32_t &e = expressionNumbering[Exp];
1477
- bool CreateNewValNum = !e;
1478
- if (CreateNewValNum) {
1479
- Expressions.push_back (Exp);
1480
- if (ExprIdx.size () < nextValueNumber + 1 )
1481
- ExprIdx.resize (nextValueNumber * 2 );
1482
- e = nextValueNumber;
1483
- ExprIdx[nextValueNumber++] = nextExprNumber++;
1484
- }
1485
- return {e, CreateNewValNum};
1486
- }
1487
-
1488
- void GVN::ValueTable::assignBlockRPONumber (Function &F) {
1489
- uint32_t NextBlockNumber = 1 ;
1490
- ReversePostOrderTraversal<Function *> RPOT (&F);
1491
- for (BasicBlock *BB : RPOT)
1492
- BlockRPONumber[BB] = NextBlockNumber++;
1493
- }
1494
-
1495
- // / Return whether all the values related with the same \p num are
1496
- // / defined in \p BB.
1497
- bool GVN::ValueTable::areAllValsInBB (uint32_t Num, const BasicBlock *BB,
1498
- GVN &Gvn) {
1499
- LeaderTableEntry *Vals = &Gvn.LeaderTable [Num];
1500
- while (Vals && Vals->BB == BB)
1501
- Vals = Vals->Next ;
1502
- return !Vals;
1503
- }
1504
-
1505
- // / Wrap phiTranslateImpl to provide caching functionality.
1506
- uint32_t GVN::ValueTable::phiTranslate (const BasicBlock *Pred,
1507
- const BasicBlock *PhiBlock, uint32_t Num,
1508
- GVN &Gvn) {
1509
- auto FindRes = PhiTranslateTable.find ({Num, Pred});
1510
- if (FindRes != PhiTranslateTable.end ())
1511
- return FindRes->second ;
1512
- uint32_t NewNum = phiTranslateImpl (Pred, PhiBlock, Num, Gvn);
1513
- PhiTranslateTable.insert ({{Num, Pred}, NewNum});
1514
- return NewNum;
1515
- }
1516
-
1517
- // / Translate value number \p Num using phis, so that it has the values of
1518
- // / the phis in BB.
1519
- uint32_t GVN::ValueTable::phiTranslateImpl (const BasicBlock *Pred,
1520
- const BasicBlock *PhiBlock,
1521
- uint32_t Num, GVN &Gvn) {
1522
- if (PHINode *PN = NumberingPhi[Num]) {
1523
- if (BlockRPONumber[Pred] >= BlockRPONumber[PhiBlock])
1524
- return Num;
1525
- for (unsigned i = 0 ; i != PN->getNumIncomingValues (); ++i) {
1526
- if (PN->getParent () == PhiBlock && PN->getIncomingBlock (i) == Pred)
1527
- if (uint32_t TransVal = lookup (PN->getIncomingValue (i), false ))
1528
- return TransVal;
1529
- }
1530
- return Num;
1531
- }
1532
-
1533
- // If there is any value related with Num is defined in a BB other than
1534
- // PhiBlock, it cannot depend on a phi in PhiBlock without going through
1535
- // a backedge. We can do an early exit in that case to save compile time.
1536
- if (!areAllValsInBB (Num, PhiBlock, Gvn))
1537
- return Num;
1538
-
1539
- if (ExprIdx[Num] == 0 || Num >= ExprIdx.size ())
1540
- return Num;
1541
- Expression Exp = Expressions[ExprIdx[Num]];
1542
-
1543
- for (unsigned i = 0 ; i < Exp.varargs .size (); i++)
1544
- Exp.varargs [i] = phiTranslate (Pred, PhiBlock, Exp.varargs [i], Gvn);
1545
-
1546
- if (Exp.commutative ) {
1547
- assert (Exp.varargs .size () == 2 && " Unsupported commutative expression!" );
1548
- if (Exp.varargs [0 ] > Exp.varargs [1 ]) {
1549
- std::swap (Exp.varargs [0 ], Exp.varargs [1 ]);
1550
- uint32_t Opcode = Exp.opcode >> 8 ;
1551
- if (Opcode == Instruction::ICmp || Opcode == Instruction::FCmp)
1552
- Exp.opcode = (Opcode << 8 ) |
1553
- CmpInst::getSwappedPredicate (
1554
- static_cast <CmpInst::Predicate>(Exp.opcode & 255 ));
1555
- }
1556
- }
1557
-
1558
- if (uint32_t NewNum = expressionNumbering[Exp])
1559
- return NewNum;
1560
- return Num;
1561
- }
1562
-
1563
1454
// In order to find a leader for a given value number at a
1564
1455
// specific basic block, we first obtain the list of all Values for that number,
1565
1456
// and then scan the list to find one whose block dominates the block in
@@ -1965,7 +1856,6 @@ bool GVN::runImpl(Function &F, AssumptionCache &RunAC, DominatorTree &RunDT,
1965
1856
// Fabricate val-num for dead-code in order to suppress assertion in
1966
1857
// performPRE().
1967
1858
assignValNumForDeadCode ();
1968
- VN.assignBlockRPONumber (F);
1969
1859
bool PREChanged = true ;
1970
1860
while (PREChanged) {
1971
1861
PREChanged = performPRE (F);
@@ -2055,9 +1945,7 @@ bool GVN::performScalarPREInsertion(Instruction *Instr, BasicBlock *Pred,
2055
1945
success = false ;
2056
1946
break ;
2057
1947
}
2058
- uint32_t TValNo =
2059
- VN.phiTranslate (Pred, Instr->getParent (), VN.lookup (Op), *this );
2060
- if (Value *V = findLeader (Pred, TValNo)) {
1948
+ if (Value *V = findLeader (Pred, VN.lookup (Op))) {
2061
1949
Instr->setOperand (i, V);
2062
1950
} else {
2063
1951
success = false ;
@@ -2074,12 +1962,10 @@ bool GVN::performScalarPREInsertion(Instruction *Instr, BasicBlock *Pred,
2074
1962
Instr->insertBefore (Pred->getTerminator ());
2075
1963
Instr->setName (Instr->getName () + " .pre" );
2076
1964
Instr->setDebugLoc (Instr->getDebugLoc ());
2077
-
2078
- unsigned Num = VN.lookupOrAdd (Instr);
2079
- VN.add (Instr, Num);
1965
+ VN.add (Instr, ValNo);
2080
1966
2081
1967
// Update the availability map to include the new instruction.
2082
- addToLeaderTable (Num , Instr, Pred);
1968
+ addToLeaderTable (ValNo , Instr, Pred);
2083
1969
return true ;
2084
1970
}
2085
1971
@@ -2128,8 +2014,7 @@ bool GVN::performScalarPRE(Instruction *CurInst) {
2128
2014
break ;
2129
2015
}
2130
2016
2131
- uint32_t TValNo = VN.phiTranslate (P, CurrentBlock, ValNo, *this );
2132
- Value *predV = findLeader (P, TValNo);
2017
+ Value *predV = findLeader (P, ValNo);
2133
2018
if (!predV) {
2134
2019
predMap.push_back (std::make_pair (static_cast <Value *>(nullptr ), P));
2135
2020
PREPred = P;
0 commit comments