1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-10-21 12:02:58 +02:00

Misc cleanups and simplifications for NewGVN.

Mostly use a bit more idiomatic C++ where we can,
so we can combine some things later.

Reviewers: davide

Subscribers: llvm-commits

Differential Revision: https://reviews.llvm.org/D28111

llvm-svn: 290550
This commit is contained in:
Daniel Berlin 2016-12-26 19:57:25 +00:00
parent b7b850ccf1
commit b5897010bc
2 changed files with 91 additions and 49 deletions

View File

@ -215,6 +215,24 @@ public:
OS << "} "; OS << "} ";
} }
}; };
class op_inserter
: public std::iterator<std::output_iterator_tag, void, void, void, void> {
private:
typedef BasicExpression Container;
Container *BE;
public:
explicit op_inserter(BasicExpression &E) : BE(&E) {}
explicit op_inserter(BasicExpression *E) : BE(E) {}
op_inserter &operator=(Value *val) {
BE->op_push_back(val);
return *this;
}
op_inserter &operator*() { return *this; }
op_inserter &operator++() { return *this; }
op_inserter &operator++(int) { return *this; }
};
class CallExpression final : public BasicExpression { class CallExpression final : public BasicExpression {
private: private:
@ -417,6 +435,24 @@ public:
OS << "}"; OS << "}";
} }
}; };
class int_op_inserter
: public std::iterator<std::output_iterator_tag, void, void, void, void> {
private:
typedef AggregateValueExpression Container;
Container *AVE;
public:
explicit int_op_inserter(AggregateValueExpression &E) : AVE(&E) {}
explicit int_op_inserter(AggregateValueExpression *E) : AVE(E) {}
int_op_inserter &operator=(unsigned int val) {
AVE->int_op_push_back(val);
return *this;
}
int_op_inserter &operator*() { return *this; }
int_op_inserter &operator++() { return *this; }
int_op_inserter &operator++(int) { return *this; }
};
class PHIExpression final : public BasicExpression { class PHIExpression final : public BasicExpression {
private: private:

View File

@ -407,20 +407,22 @@ PHIExpression *NewGVN::createPHIExpression(Instruction *I) {
E->allocateOperands(ArgRecycler, ExpressionAllocator); E->allocateOperands(ArgRecycler, ExpressionAllocator);
E->setType(I->getType()); E->setType(I->getType());
E->setOpcode(I->getOpcode()); E->setOpcode(I->getOpcode());
for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
BasicBlock *B = PN->getIncomingBlock(i); auto ReachablePhiArg = [&](const Use &U) {
if (!ReachableBlocks.count(B)) { return ReachableBlocks.count(PN->getIncomingBlock(U));
DEBUG(dbgs() << "Skipping unreachable block " << getBlockName(B) };
<< " in PHI node " << *PN << "\n");
continue; // Filter out unreachable operands
} auto Filtered = make_filter_range(PN->operands(), ReachablePhiArg);
if (I->getOperand(i) != I) {
const BasicBlockEdge BBE(B, PhiBlock); std::transform(Filtered.begin(), Filtered.end(), op_inserter(E),
E->op_push_back(lookupOperandLeader(I->getOperand(i), I, BBE)); [&](const Use &U) -> Value * {
} else { // Don't try to transform self-defined phis
E->op_push_back(I->getOperand(i)); if (U == PN)
} return PN;
} const BasicBlockEdge BBE(PN->getIncomingBlock(U), PhiBlock);
return lookupOperandLeader(U, I, BBE);
});
return E; return E;
} }
@ -436,12 +438,14 @@ bool NewGVN::setBasicExpressionInfo(Instruction *I, BasicExpression *E,
E->setOpcode(I->getOpcode()); E->setOpcode(I->getOpcode());
E->allocateOperands(ArgRecycler, ExpressionAllocator); E->allocateOperands(ArgRecycler, ExpressionAllocator);
for (auto &O : I->operands()) { // Transform the operand array into an operand leader array, and keep track of
// whether all members are constant.
std::transform(I->op_begin(), I->op_end(), op_inserter(E), [&](Value *O) {
auto Operand = lookupOperandLeader(O, I, B); auto Operand = lookupOperandLeader(O, I, B);
if (!isa<Constant>(Operand)) AllConstant &= isa<Constant>(Operand);
AllConstant = false; return Operand;
E->op_push_back(Operand); });
}
return AllConstant; return AllConstant;
} }
@ -615,19 +619,14 @@ NewGVN::createAggregateValueExpression(Instruction *I, const BasicBlock *B) {
AggregateValueExpression(I->getNumOperands(), II->getNumIndices()); AggregateValueExpression(I->getNumOperands(), II->getNumIndices());
setBasicExpressionInfo(I, E, B); setBasicExpressionInfo(I, E, B);
E->allocateIntOperands(ExpressionAllocator); E->allocateIntOperands(ExpressionAllocator);
std::copy(II->idx_begin(), II->idx_end(), int_op_inserter(E));
for (auto &Index : II->indices())
E->int_op_push_back(Index);
return E; return E;
} else if (auto *EI = dyn_cast<ExtractValueInst>(I)) { } else if (auto *EI = dyn_cast<ExtractValueInst>(I)) {
AggregateValueExpression *E = new (ExpressionAllocator) AggregateValueExpression *E = new (ExpressionAllocator)
AggregateValueExpression(I->getNumOperands(), EI->getNumIndices()); AggregateValueExpression(I->getNumOperands(), EI->getNumIndices());
setBasicExpressionInfo(EI, E, B); setBasicExpressionInfo(EI, E, B);
E->allocateIntOperands(ExpressionAllocator); E->allocateIntOperands(ExpressionAllocator);
std::copy(EI->idx_begin(), EI->idx_end(), int_op_inserter(E));
for (auto &Index : EI->indices())
E->int_op_push_back(Index);
return E; return E;
} }
llvm_unreachable("Unhandled type of aggregate value operation"); llvm_unreachable("Unhandled type of aggregate value operation");
@ -777,9 +776,11 @@ const Expression *NewGVN::performSymbolicCallEvaluation(Instruction *I,
CallInst *CI = cast<CallInst>(I); CallInst *CI = cast<CallInst>(I);
if (AA->doesNotAccessMemory(CI)) if (AA->doesNotAccessMemory(CI))
return createCallExpression(CI, nullptr, B); return createCallExpression(CI, nullptr, B);
else if (AA->onlyReadsMemory(CI)) else if (AA->onlyReadsMemory(CI)) {
return createCallExpression(CI, MSSAWalker->getClobberingMemoryAccess(CI), MemoryAccess *DefiningAccess = MSSAWalker->getClobberingMemoryAccess(CI);
return createCallExpression(CI, lookupMemoryAccessEquiv(DefiningAccess),
B); B);
}
else else
return nullptr; return nullptr;
} }
@ -1226,13 +1227,12 @@ void NewGVN::initializeCongruenceClasses(Function &F) {
NextCongruenceNum = 2; NextCongruenceNum = 2;
// Initialize all other instructions to be in INITIAL class. // Initialize all other instructions to be in INITIAL class.
CongruenceClass::MemberSet InitialValues; CongruenceClass::MemberSet InitialValues;
for (auto &B : F)
for (auto &I : B)
InitialValues.insert(&I);
InitialClass = createCongruenceClass(NULL, NULL); InitialClass = createCongruenceClass(NULL, NULL);
for (auto L : InitialValues) for (auto &B : F)
ValueToClass[L] = InitialClass; for (auto &I : B) {
InitialValues.insert(&I);
ValueToClass[&I] = InitialClass;
}
InitialClass->Members.swap(InitialValues); InitialClass->Members.swap(InitialValues);
// Initialize arguments to be in their own unique congruence classes // Initialize arguments to be in their own unique congruence classes
@ -1472,15 +1472,15 @@ bool NewGVN::runGVN(Function &F, DominatorTree *_DT, AssumptionCache *_AC,
} }
// Delete all unreachable blocks. // Delete all unreachable blocks.
for (auto &B : F) { auto UnreachableBlockPred =
BasicBlock *BB = &B; [&](const BasicBlock &BB) { return !ReachableBlocks.count(&BB); };
if (!ReachableBlocks.count(BB)) {
DEBUG(dbgs() << "We believe block " << getBlockName(BB) for (auto &BB : make_filter_range(F, UnreachableBlockPred)) {
DEBUG(dbgs() << "We believe block " << getBlockName(&BB)
<< " is unreachable\n"); << " is unreachable\n");
deleteInstructionsInBlock(BB); deleteInstructionsInBlock(&BB);
Changed = true; Changed = true;
} }
}
cleanupTables(); cleanupTables();
return Changed; return Changed;
@ -1750,18 +1750,24 @@ bool NewGVN::eliminateInstructions(Function &F) {
// values, and eliminating them. However, this is mildly // values, and eliminating them. However, this is mildly
// pointless. It requires doing lookups on every instruction, // pointless. It requires doing lookups on every instruction,
// regardless of whether we will ever eliminate it. For // regardless of whether we will ever eliminate it. For
// instructions part of most singleton congruence class, we know we // instructions part of most singleton congruence classes, we know we
// will never eliminate it. // will never eliminate them.
// Instead, this eliminator looks at the congruence classes directly, sorts // Instead, this eliminator looks at the congruence classes directly, sorts
// them into a DFS ordering of the dominator tree, and then we just // them into a DFS ordering of the dominator tree, and then we just
// perform eliminate straight on the sets by walking the congruence // perform elimination straight on the sets by walking the congruence
// class member uses in order, and eliminate the ones dominated by the // class member uses in order, and eliminate the ones dominated by the
// last member. This is technically O(N log N) where N = number of // last member. This is worst case O(E log E) where E = number of
// instructions (since in theory all instructions may be in the same // instructions in a single congruence class. In theory, this is all
// congruence class). // instructions. In practice, it is much faster, as most instructions are
// either in singleton congruence classes or can't possibly be eliminated
// anyway (if there are no overlapping DFS ranges in class).
// When we find something not dominated, it becomes the new leader // When we find something not dominated, it becomes the new leader
// for elimination purposes // for elimination purposes.
// TODO: If we wanted to be faster, We could remove any members with no
// overlapping ranges while sorting, as we will never eliminate anything
// with those members, as they don't dominate anything else in our set.
bool AnythingReplaced = false; bool AnythingReplaced = false;