Skip to content

Commit 3250ae3

Browse files
committed
Revert rL303923 since it broke the sanitizer bootstrap build bot.
llvm-svn: 303969
1 parent 25d9ba9 commit 3250ae3

File tree

5 files changed

+26
-271
lines changed

5 files changed

+26
-271
lines changed

llvm/include/llvm/Transforms/Scalar/GVN.h

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -68,24 +68,6 @@ class GVN : public PassInfoMixin<GVN> {
6868
class ValueTable {
6969
DenseMap<Value *, uint32_t> valueNumbering;
7070
DenseMap<Expression, uint32_t> expressionNumbering;
71-
72-
// Expressions is the vector of Expression. ExprIdx is the mapping from
73-
// value number to the index of Expression in Expressions. We use it
74-
// instead of a DenseMap because filling such mapping is faster than
75-
// filling a DenseMap and the compile time is a little better.
76-
uint32_t nextExprNumber;
77-
std::vector<Expression> Expressions;
78-
std::vector<uint32_t> ExprIdx;
79-
// Value number to PHINode mapping. Used for phi-translate in scalarpre.
80-
DenseMap<uint32_t, PHINode *> NumberingPhi;
81-
// Cache for phi-translate in scalarpre.
82-
typedef DenseMap<std::pair<uint32_t, const BasicBlock *>, uint32_t>
83-
PhiTranslateMap;
84-
PhiTranslateMap PhiTranslateTable;
85-
// Map the block to reversed postorder traversal number. It is used to
86-
// find back edge easily.
87-
DenseMap<const BasicBlock *, uint32_t> BlockRPONumber;
88-
8971
AliasAnalysis *AA;
9072
MemoryDependenceResults *MD;
9173
DominatorTree *DT;
@@ -97,10 +79,6 @@ class GVN : public PassInfoMixin<GVN> {
9779
Value *LHS, Value *RHS);
9880
Expression createExtractvalueExpr(ExtractValueInst *EI);
9981
uint32_t lookupOrAddCall(CallInst *C);
100-
uint32_t phiTranslateImpl(const BasicBlock *BB, const BasicBlock *PhiBlock,
101-
uint32_t Num, GVN &Gvn);
102-
std::pair<uint32_t, bool> assignExpNewValueNum(Expression &exp);
103-
bool areAllValsInBB(uint32_t num, const BasicBlock *BB, GVN &Gvn);
10482

10583
public:
10684
ValueTable();
@@ -109,12 +87,9 @@ class GVN : public PassInfoMixin<GVN> {
10987
~ValueTable();
11088

11189
uint32_t lookupOrAdd(Value *V);
112-
uint32_t lookup(Value *V, bool Verify = true) const;
90+
uint32_t lookup(Value *V) const;
11391
uint32_t lookupOrAddCmp(unsigned Opcode, CmpInst::Predicate Pred,
11492
Value *LHS, Value *RHS);
115-
uint32_t phiTranslate(const BasicBlock *BB, const BasicBlock *PhiBlock,
116-
uint32_t Num, GVN &Gvn);
117-
void assignBlockRPONumber(Function &F);
11893
bool exists(Value *V) const;
11994
void add(Value *V, uint32_t num);
12095
void clear();

llvm/lib/Transforms/Scalar/GVN.cpp

Lines changed: 21 additions & 136 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,9 @@ MaxRecurseDepth("max-recurse-depth", cl::Hidden, cl::init(1000), cl::ZeroOrMore,
8080
struct llvm::GVN::Expression {
8181
uint32_t opcode;
8282
Type *type;
83-
bool commutative;
8483
SmallVector<uint32_t, 4> varargs;
8584

86-
Expression(uint32_t o = ~2U) : opcode(o), commutative(false) {}
85+
Expression(uint32_t o = ~2U) : opcode(o) {}
8786

8887
bool operator==(const Expression &other) const {
8988
if (opcode != other.opcode)
@@ -247,7 +246,6 @@ GVN::Expression GVN::ValueTable::createExpr(Instruction *I) {
247246
assert(I->getNumOperands() == 2 && "Unsupported commutative instruction!");
248247
if (e.varargs[0] > e.varargs[1])
249248
std::swap(e.varargs[0], e.varargs[1]);
250-
e.commutative = true;
251249
}
252250

253251
if (CmpInst *C = dyn_cast<CmpInst>(I)) {
@@ -258,7 +256,6 @@ GVN::Expression GVN::ValueTable::createExpr(Instruction *I) {
258256
Predicate = CmpInst::getSwappedPredicate(Predicate);
259257
}
260258
e.opcode = (C->getOpcode() << 8) | Predicate;
261-
e.commutative = true;
262259
} else if (InsertValueInst *E = dyn_cast<InsertValueInst>(I)) {
263260
for (InsertValueInst::idx_iterator II = E->idx_begin(), IE = E->idx_end();
264261
II != IE; ++II)
@@ -284,7 +281,6 @@ GVN::Expression GVN::ValueTable::createCmpExpr(unsigned Opcode,
284281
Predicate = CmpInst::getSwappedPredicate(Predicate);
285282
}
286283
e.opcode = (Opcode << 8) | Predicate;
287-
e.commutative = true;
288284
return e;
289285
}
290286

@@ -352,25 +348,25 @@ GVN::ValueTable::~ValueTable() = default;
352348
/// add - Insert a value into the table with a specified value number.
353349
void GVN::ValueTable::add(Value *V, uint32_t num) {
354350
valueNumbering.insert(std::make_pair(V, num));
355-
if (PHINode *PN = dyn_cast<PHINode>(V))
356-
NumberingPhi[num] = PN;
357351
}
358352

359353
uint32_t GVN::ValueTable::lookupOrAddCall(CallInst *C) {
360354
if (AA->doesNotAccessMemory(C)) {
361355
Expression exp = createExpr(C);
362-
uint32_t e = assignExpNewValueNum(exp).first;
356+
uint32_t &e = expressionNumbering[exp];
357+
if (!e) e = nextValueNumber++;
363358
valueNumbering[C] = e;
364359
return e;
365360
} else if (AA->onlyReadsMemory(C)) {
366361
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;
371367
}
372368
if (!MD) {
373-
uint32_t e = assignExpNewValueNum(exp).first;
369+
e = nextValueNumber++;
374370
valueNumbering[C] = e;
375371
return e;
376372
}
@@ -526,29 +522,23 @@ uint32_t GVN::ValueTable::lookupOrAdd(Value *V) {
526522
case Instruction::ExtractValue:
527523
exp = createExtractvalueExpr(cast<ExtractValueInst>(I));
528524
break;
529-
case Instruction::PHI:
530-
valueNumbering[V] = nextValueNumber;
531-
NumberingPhi[nextValueNumber] = cast<PHINode>(V);
532-
return nextValueNumber++;
533525
default:
534526
valueNumbering[V] = nextValueNumber;
535527
return nextValueNumber++;
536528
}
537529

538-
uint32_t e = assignExpNewValueNum(exp).first;
530+
uint32_t& e = expressionNumbering[exp];
531+
if (!e) e = nextValueNumber++;
539532
valueNumbering[V] = e;
540533
return e;
541534
}
542535

543536
/// Returns the value number of the specified value. Fails if
544537
/// 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 {
546539
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;
552542
}
553543

554544
/// Returns the value number of the given comparison,
@@ -559,29 +549,21 @@ uint32_t GVN::ValueTable::lookupOrAddCmp(unsigned Opcode,
559549
CmpInst::Predicate Predicate,
560550
Value *LHS, Value *RHS) {
561551
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;
563555
}
564556

565557
/// Remove all entries from the ValueTable.
566558
void GVN::ValueTable::clear() {
567559
valueNumbering.clear();
568560
expressionNumbering.clear();
569-
NumberingPhi.clear();
570-
PhiTranslateTable.clear();
571-
BlockRPONumber.clear();
572561
nextValueNumber = 1;
573-
Expressions.clear();
574-
ExprIdx.clear();
575-
nextExprNumber = 0;
576562
}
577563

578564
/// Remove a value from the value numbering.
579565
void GVN::ValueTable::erase(Value *V) {
580-
uint32_t Num = valueNumbering.lookup(V);
581566
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);
585567
}
586568

587569
/// verifyRemoved - Verify that the value is removed from all internal data
@@ -1469,97 +1451,6 @@ bool GVN::processLoad(LoadInst *L) {
14691451
return false;
14701452
}
14711453

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-
15631454
// In order to find a leader for a given value number at a
15641455
// specific basic block, we first obtain the list of all Values for that number,
15651456
// 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,
19651856
// Fabricate val-num for dead-code in order to suppress assertion in
19661857
// performPRE().
19671858
assignValNumForDeadCode();
1968-
VN.assignBlockRPONumber(F);
19691859
bool PREChanged = true;
19701860
while (PREChanged) {
19711861
PREChanged = performPRE(F);
@@ -2055,9 +1945,7 @@ bool GVN::performScalarPREInsertion(Instruction *Instr, BasicBlock *Pred,
20551945
success = false;
20561946
break;
20571947
}
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))) {
20611949
Instr->setOperand(i, V);
20621950
} else {
20631951
success = false;
@@ -2074,12 +1962,10 @@ bool GVN::performScalarPREInsertion(Instruction *Instr, BasicBlock *Pred,
20741962
Instr->insertBefore(Pred->getTerminator());
20751963
Instr->setName(Instr->getName() + ".pre");
20761964
Instr->setDebugLoc(Instr->getDebugLoc());
2077-
2078-
unsigned Num = VN.lookupOrAdd(Instr);
2079-
VN.add(Instr, Num);
1965+
VN.add(Instr, ValNo);
20801966

20811967
// Update the availability map to include the new instruction.
2082-
addToLeaderTable(Num, Instr, Pred);
1968+
addToLeaderTable(ValNo, Instr, Pred);
20831969
return true;
20841970
}
20851971

@@ -2128,8 +2014,7 @@ bool GVN::performScalarPRE(Instruction *CurInst) {
21282014
break;
21292015
}
21302016

2131-
uint32_t TValNo = VN.phiTranslate(P, CurrentBlock, ValNo, *this);
2132-
Value *predV = findLeader(P, TValNo);
2017+
Value *predV = findLeader(P, ValNo);
21332018
if (!predV) {
21342019
predMap.push_back(std::make_pair(static_cast<Value *>(nullptr), P));
21352020
PREPred = P;

0 commit comments

Comments
 (0)