summaryrefslogtreecommitdiffstats
path: root/llvm/lib/Analysis
diff options
context:
space:
mode:
Diffstat (limited to 'llvm/lib/Analysis')
-rw-r--r--llvm/lib/Analysis/LoopInfo.cpp16
-rw-r--r--llvm/lib/Analysis/MemorySSAUpdater.cpp81
2 files changed, 90 insertions, 7 deletions
diff --git a/llvm/lib/Analysis/LoopInfo.cpp b/llvm/lib/Analysis/LoopInfo.cpp
index b9789394d41..aa933d98f24 100644
--- a/llvm/lib/Analysis/LoopInfo.cpp
+++ b/llvm/lib/Analysis/LoopInfo.cpp
@@ -19,6 +19,8 @@
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/Analysis/LoopInfoImpl.h"
#include "llvm/Analysis/LoopIterator.h"
+#include "llvm/Analysis/MemorySSA.h"
+#include "llvm/Analysis/MemorySSAUpdater.h"
#include "llvm/Analysis/ValueTracking.h"
#include "llvm/Config/llvm-config.h"
#include "llvm/IR/CFG.h"
@@ -64,15 +66,16 @@ bool Loop::hasLoopInvariantOperands(const Instruction *I) const {
return all_of(I->operands(), [this](Value *V) { return isLoopInvariant(V); });
}
-bool Loop::makeLoopInvariant(Value *V, bool &Changed,
- Instruction *InsertPt) const {
+bool Loop::makeLoopInvariant(Value *V, bool &Changed, Instruction *InsertPt,
+ MemorySSAUpdater *MSSAU) const {
if (Instruction *I = dyn_cast<Instruction>(V))
- return makeLoopInvariant(I, Changed, InsertPt);
+ return makeLoopInvariant(I, Changed, InsertPt, MSSAU);
return true; // All non-instructions are loop-invariant.
}
bool Loop::makeLoopInvariant(Instruction *I, bool &Changed,
- Instruction *InsertPt) const {
+ Instruction *InsertPt,
+ MemorySSAUpdater *MSSAU) const {
// Test if the value is already loop-invariant.
if (isLoopInvariant(I))
return true;
@@ -93,11 +96,14 @@ bool Loop::makeLoopInvariant(Instruction *I, bool &Changed,
}
// Don't hoist instructions with loop-variant operands.
for (Value *Operand : I->operands())
- if (!makeLoopInvariant(Operand, Changed, InsertPt))
+ if (!makeLoopInvariant(Operand, Changed, InsertPt, MSSAU))
return false;
// Hoist.
I->moveBefore(InsertPt);
+ if (MSSAU)
+ if (auto *MUD = MSSAU->getMemorySSA()->getMemoryAccess(I))
+ MSSAU->moveToPlace(MUD, InsertPt->getParent(), MemorySSA::End);
// There is possibility of hoisting this instruction above some arbitrary
// condition. Any metadata defined on it can be control dependent on this
diff --git a/llvm/lib/Analysis/MemorySSAUpdater.cpp b/llvm/lib/Analysis/MemorySSAUpdater.cpp
index 8fe7c685102..b0f7dc69e5a 100644
--- a/llvm/lib/Analysis/MemorySSAUpdater.cpp
+++ b/llvm/lib/Analysis/MemorySSAUpdater.cpp
@@ -463,8 +463,8 @@ void MemorySSAUpdater::removeEdge(BasicBlock *From, BasicBlock *To) {
}
}
-void MemorySSAUpdater::removeDuplicatePhiEdgesBetween(BasicBlock *From,
- BasicBlock *To) {
+void MemorySSAUpdater::removeDuplicatePhiEdgesBetween(const BasicBlock *From,
+ const BasicBlock *To) {
if (MemoryPhi *MPhi = MSSA->getMemoryAccess(To)) {
bool Found = false;
MPhi->unorderedDeleteIncomingIf([&](const MemoryAccess *, BasicBlock *B) {
@@ -522,6 +522,46 @@ void MemorySSAUpdater::cloneUsesAndDefs(BasicBlock *BB, BasicBlock *NewBB,
}
}
+void MemorySSAUpdater::updatePhisWhenInsertingUniqueBackedgeBlock(
+ BasicBlock *Header, BasicBlock *Preheader, BasicBlock *BEBlock) {
+ auto *MPhi = MSSA->getMemoryAccess(Header);
+ if (!MPhi)
+ return;
+
+ // Create phi node in the backedge block and populate it with the same
+ // incoming values as MPhi. Skip incoming values coming from Preheader.
+ auto *NewMPhi = MSSA->createMemoryPhi(BEBlock);
+ bool HasUniqueIncomingValue = true;
+ MemoryAccess *UniqueValue = nullptr;
+ for (unsigned I = 0, E = MPhi->getNumIncomingValues(); I != E; ++I) {
+ BasicBlock *IBB = MPhi->getIncomingBlock(I);
+ MemoryAccess *IV = MPhi->getIncomingValue(I);
+ if (IBB != Preheader) {
+ NewMPhi->addIncoming(IV, IBB);
+ if (HasUniqueIncomingValue) {
+ if (!UniqueValue)
+ UniqueValue = IV;
+ else if (UniqueValue != IV)
+ HasUniqueIncomingValue = false;
+ }
+ }
+ }
+
+ // Update incoming edges into MPhi. Remove all but the incoming edge from
+ // Preheader. Add an edge from NewMPhi
+ auto *AccFromPreheader = MPhi->getIncomingValueForBlock(Preheader);
+ MPhi->setIncomingValue(0, AccFromPreheader);
+ MPhi->setIncomingBlock(0, Preheader);
+ for (unsigned I = MPhi->getNumIncomingValues() - 1; I >= 1; --I)
+ MPhi->unorderedDeleteIncoming(I);
+ MPhi->addIncoming(NewMPhi, BEBlock);
+
+ // 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);
+}
+
void MemorySSAUpdater::updateForClonedLoop(const LoopBlocksRPO &LoopBlocks,
ArrayRef<BasicBlock *> ExitBlocks,
const ValueToValueMapTy &VMap,
@@ -1223,6 +1263,43 @@ void MemorySSAUpdater::tryRemoveTrivialPhis(ArrayRef<WeakVH> UpdatedPHIs) {
}
}
+void MemorySSAUpdater::changeToUnreachable(const Instruction *I) {
+ const BasicBlock *BB = I->getParent();
+ // Remove memory accesses in BB for I and all following instructions.
+ auto BBI = I->getIterator(), BBE = BB->end();
+ // FIXME: If this becomes too expensive, iterate until the first instruction
+ // with a memory access, then iterate over MemoryAccesses.
+ while (BBI != BBE)
+ removeMemoryAccess(&*(BBI++));
+ // Update phis in BB's successors to remove BB.
+ SmallVector<WeakVH, 16> UpdatedPHIs;
+ for (const BasicBlock *Successor : successors(BB)) {
+ removeDuplicatePhiEdgesBetween(BB, Successor);
+ if (MemoryPhi *MPhi = MSSA->getMemoryAccess(Successor)) {
+ MPhi->unorderedDeleteIncomingBlock(BB);
+ UpdatedPHIs.push_back(MPhi);
+ }
+ }
+ // Optimize trivial phis.
+ tryRemoveTrivialPhis(UpdatedPHIs);
+}
+
+void MemorySSAUpdater::changeCondBranchToUnconditionalTo(const BranchInst *BI,
+ const BasicBlock *To) {
+ const BasicBlock *BB = BI->getParent();
+ SmallVector<WeakVH, 16> UpdatedPHIs;
+ for (const BasicBlock *Succ : successors(BB)) {
+ removeDuplicatePhiEdgesBetween(BB, Succ);
+ if (Succ != To)
+ if (auto *MPhi = MSSA->getMemoryAccess(Succ)) {
+ MPhi->unorderedDeleteIncomingBlock(BB);
+ UpdatedPHIs.push_back(MPhi);
+ }
+ }
+ // Optimize trivial phis.
+ tryRemoveTrivialPhis(UpdatedPHIs);
+}
+
MemoryAccess *MemorySSAUpdater::createMemoryAccessInBB(
Instruction *I, MemoryAccess *Definition, const BasicBlock *BB,
MemorySSA::InsertionPlace Point) {
OpenPOWER on IntegriCloud