Skip to content

Commit

Permalink
[MemorySSA] Make Phi cleanups consistent.
Browse files Browse the repository at this point in the history
Summary:
Make Phi cleanups consistent: remove self as a trivial Phi and
recurse to potentially remove other trivial phis.

Reviewers: george.burgess.iv

Subscribers: Prazek, sanjoy.google, llvm-commits

Tags: #llvm

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

llvm-svn: 369466
  • Loading branch information
alinas committed Aug 20, 2019
1 parent e6c299b commit 2863721
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 24 deletions.
1 change: 1 addition & 0 deletions llvm/include/llvm/Analysis/MemorySSAUpdater.h
Expand Up @@ -275,6 +275,7 @@ class MemorySSAUpdater {
getPreviousDefRecursive(BasicBlock *,
DenseMap<BasicBlock *, TrackingVH<MemoryAccess>> &);
MemoryAccess *recursePhi(MemoryAccess *Phi);
MemoryAccess *tryRemoveTrivialPhi(MemoryPhi *Phi);
template <class RangeType>
MemoryAccess *tryRemoveTrivialPhi(MemoryPhi *Phi, RangeType &Operands);
void tryRemoveTrivialPhis(ArrayRef<WeakVH> UpdatedPHIs);
Expand Down
41 changes: 17 additions & 24 deletions llvm/lib/Analysis/MemorySSAUpdater.cpp
Expand Up @@ -173,12 +173,9 @@ MemoryAccess *MemorySSAUpdater::recursePhi(MemoryAccess *Phi) {
TrackingVH<MemoryAccess> Res(Phi);
SmallVector<TrackingVH<Value>, 8> Uses;
std::copy(Phi->user_begin(), Phi->user_end(), std::back_inserter(Uses));
for (auto &U : Uses) {
if (MemoryPhi *UsePhi = dyn_cast<MemoryPhi>(&*U)) {
auto OperRange = UsePhi->operands();
tryRemoveTrivialPhi(UsePhi, OperRange);
}
}
for (auto &U : Uses)
if (MemoryPhi *UsePhi = dyn_cast<MemoryPhi>(&*U))
tryRemoveTrivialPhi(UsePhi);
return Res;
}

Expand All @@ -187,6 +184,11 @@ MemoryAccess *MemorySSAUpdater::recursePhi(MemoryAccess *Phi) {
// argument.
// IE phi(a, a) or b = phi(a, b) or c = phi(a, a, c)
// We recursively try to remove them.
MemoryAccess *MemorySSAUpdater::tryRemoveTrivialPhi(MemoryPhi *Phi) {
assert(Phi && "Can only remove concrete Phi.");
auto OperRange = Phi->operands();
return tryRemoveTrivialPhi(Phi, OperRange);
}
template <class RangeType>
MemoryAccess *MemorySSAUpdater::tryRemoveTrivialPhi(MemoryPhi *Phi,
RangeType &Operands) {
Expand Down Expand Up @@ -490,8 +492,7 @@ void MemorySSAUpdater::fixupDefs(const SmallVectorImpl<WeakVH> &Vars) {
void MemorySSAUpdater::removeEdge(BasicBlock *From, BasicBlock *To) {
if (MemoryPhi *MPhi = MSSA->getMemoryAccess(To)) {
MPhi->unorderedDeleteIncomingBlock(From);
if (MPhi->getNumIncomingValues() == 1)
removeMemoryAccess(MPhi);
tryRemoveTrivialPhi(MPhi);
}
}

Expand All @@ -507,8 +508,7 @@ void MemorySSAUpdater::removeDuplicatePhiEdgesBetween(const BasicBlock *From,
Found = true;
return false;
});
if (MPhi->getNumIncomingValues() == 1)
removeMemoryAccess(MPhi);
tryRemoveTrivialPhi(MPhi);
}
}

Expand Down Expand Up @@ -617,8 +617,7 @@ void MemorySSAUpdater::updatePhisWhenInsertingUniqueBackedgeBlock(

// If NewMPhi is a trivial phi, remove it. Its use in the header MPhi will be
// replaced with the unique value.
if (HasUniqueIncomingValue)
removeMemoryAccess(NewMPhi);
tryRemoveTrivialPhi(MPhi);
}

void MemorySSAUpdater::updateForClonedLoop(const LoopBlocksRPO &LoopBlocks,
Expand Down Expand Up @@ -1227,8 +1226,7 @@ void MemorySSAUpdater::wireOldPredecessorsToNewImmediatePredecessor(
return false;
});
Phi->addIncoming(NewPhi, New);
if (onlySingleValue(NewPhi))
removeMemoryAccess(NewPhi);
tryRemoveTrivialPhi(NewPhi);
}
}

Expand Down Expand Up @@ -1293,10 +1291,8 @@ void MemorySSAUpdater::removeMemoryAccess(MemoryAccess *MA, bool OptimizePhis) {
unsigned PhisSize = PhisToOptimize.size();
while (PhisSize-- > 0)
if (MemoryPhi *MP =
cast_or_null<MemoryPhi>(PhisToOptimize.pop_back_val())) {
auto OperRange = MP->operands();
tryRemoveTrivialPhi(MP, OperRange);
}
cast_or_null<MemoryPhi>(PhisToOptimize.pop_back_val()))
tryRemoveTrivialPhi(MP);
}
}

Expand All @@ -1310,8 +1306,7 @@ void MemorySSAUpdater::removeBlocks(
if (!DeadBlocks.count(Succ))
if (MemoryPhi *MP = MSSA->getMemoryAccess(Succ)) {
MP->unorderedDeleteIncomingBlock(BB);
if (MP->getNumIncomingValues() == 1)
removeMemoryAccess(MP);
tryRemoveTrivialPhi(MP);
}
// Drop all references of all accesses in BB
if (MemorySSA::AccessList *Acc = MSSA->getWritableBlockAccesses(BB))
Expand All @@ -1335,10 +1330,8 @@ void MemorySSAUpdater::removeBlocks(

void MemorySSAUpdater::tryRemoveTrivialPhis(ArrayRef<WeakVH> UpdatedPHIs) {
for (auto &VH : UpdatedPHIs)
if (auto *MPhi = cast_or_null<MemoryPhi>(VH)) {
auto OperRange = MPhi->operands();
tryRemoveTrivialPhi(MPhi, OperRange);
}
if (auto *MPhi = cast_or_null<MemoryPhi>(VH))
tryRemoveTrivialPhi(MPhi);
}

void MemorySSAUpdater::changeToUnreachable(const Instruction *I) {
Expand Down

0 comments on commit 2863721

Please sign in to comment.