Skip to content
This repository was archived by the owner on Feb 5, 2019. It is now read-only.

Mark MergedLoadStoreMotion as not preserving MemDep results #102

Merged
merged 1 commit into from
Jan 25, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 10 additions & 43 deletions lib/Transforms/Scalar/MergedLoadStoreMotion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@
#include "llvm/Analysis/GlobalsModRef.h"
#include "llvm/Analysis/Loads.h"
#include "llvm/Analysis/MemoryBuiltins.h"
#include "llvm/Analysis/MemoryDependenceAnalysis.h"
#include "llvm/Analysis/ValueTracking.h"
#include "llvm/IR/Metadata.h"
#include "llvm/IR/PatternMatch.h"
Expand All @@ -98,7 +97,6 @@ namespace {
// MergedLoadStoreMotion Pass
//===----------------------------------------------------------------------===//
class MergedLoadStoreMotion {
MemoryDependenceResults *MD = nullptr;
AliasAnalysis *AA = nullptr;

// The mergeLoad/Store algorithms could have Size0 * Size1 complexity,
Expand All @@ -108,14 +106,9 @@ class MergedLoadStoreMotion {
const int MagicCompileTimeControl = 250;

public:
bool run(Function &F, MemoryDependenceResults *MD, AliasAnalysis &AA);
bool run(Function &F, AliasAnalysis &AA);

private:
///
/// \brief Remove instruction from parent and update memory dependence
/// analysis.
///
void removeInstruction(Instruction *Inst);
BasicBlock *getDiamondTail(BasicBlock *BB);
bool isDiamondHead(BasicBlock *BB);
// Routines for hoisting loads
Expand All @@ -138,22 +131,6 @@ class MergedLoadStoreMotion {
};
} // end anonymous namespace

///
/// \brief Remove instruction from parent and update memory dependence analysis.
///
void MergedLoadStoreMotion::removeInstruction(Instruction *Inst) {
// Notify the memory dependence analysis.
if (MD) {
MD->removeInstruction(Inst);
if (auto *LI = dyn_cast<LoadInst>(Inst))
MD->invalidateCachedPointerInfo(LI->getPointerOperand());
if (Inst->getType()->isPtrOrPtrVectorTy()) {
MD->invalidateCachedPointerInfo(Inst);
}
}
Inst->eraseFromParent();
}

///
/// \brief Return tail block of a diamond.
///
Expand Down Expand Up @@ -273,10 +250,10 @@ void MergedLoadStoreMotion::hoistInstruction(BasicBlock *BB,
HoistedInst->insertBefore(HoistPt);

HoistCand->replaceAllUsesWith(HoistedInst);
removeInstruction(HoistCand);
HoistCand->eraseFromParent();
// Replace the else block instruction.
ElseInst->replaceAllUsesWith(HoistedInst);
removeInstruction(ElseInst);
ElseInst->eraseFromParent();
}

///
Expand Down Expand Up @@ -410,8 +387,6 @@ PHINode *MergedLoadStoreMotion::getPHIOperand(BasicBlock *BB, StoreInst *S0,
&BB->front());
NewPN->addIncoming(Opd1, S0->getParent());
NewPN->addIncoming(Opd2, S1->getParent());
if (MD && NewPN->getType()->getScalarType()->isPointerTy())
MD->invalidateCachedPointerInfo(NewPN);
return NewPN;
}

Expand Down Expand Up @@ -449,12 +424,12 @@ bool MergedLoadStoreMotion::sinkStore(BasicBlock *BB, StoreInst *S0,
// New PHI operand? Use it.
if (PHINode *NewPN = getPHIOperand(BB, S0, S1))
SNew->setOperand(0, NewPN);
removeInstruction(S0);
removeInstruction(S1);
S0->eraseFromParent();
S1->eraseFromParent();
A0->replaceAllUsesWith(ANew);
removeInstruction(A0);
A0->eraseFromParent();
A1->replaceAllUsesWith(ANew);
removeInstruction(A1);
A1->eraseFromParent();
return true;
}
return false;
Expand Down Expand Up @@ -518,9 +493,7 @@ bool MergedLoadStoreMotion::mergeStores(BasicBlock *T) {
return MergedStores;
}

bool MergedLoadStoreMotion::run(Function &F, MemoryDependenceResults *MD,
AliasAnalysis &AA) {
this->MD = MD;
bool MergedLoadStoreMotion::run(Function &F, AliasAnalysis &AA) {
this->AA = &AA;

bool Changed = false;
Expand Down Expand Up @@ -557,17 +530,14 @@ class MergedLoadStoreMotionLegacyPass : public FunctionPass {
if (skipFunction(F))
return false;
MergedLoadStoreMotion Impl;
auto *MDWP = getAnalysisIfAvailable<MemoryDependenceWrapperPass>();
return Impl.run(F, MDWP ? &MDWP->getMemDep() : nullptr,
getAnalysis<AAResultsWrapperPass>().getAAResults());
return Impl.run(F, getAnalysis<AAResultsWrapperPass>().getAAResults());
}

private:
void getAnalysisUsage(AnalysisUsage &AU) const override {
AU.setPreservesCFG();
AU.addRequired<AAResultsWrapperPass>();
AU.addPreserved<GlobalsAAWrapperPass>();
AU.addPreserved<MemoryDependenceWrapperPass>();
}
};

Expand All @@ -583,22 +553,19 @@ FunctionPass *llvm::createMergedLoadStoreMotionPass() {

INITIALIZE_PASS_BEGIN(MergedLoadStoreMotionLegacyPass, "mldst-motion",
"MergedLoadStoreMotion", false, false)
INITIALIZE_PASS_DEPENDENCY(MemoryDependenceWrapperPass)
INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
INITIALIZE_PASS_END(MergedLoadStoreMotionLegacyPass, "mldst-motion",
"MergedLoadStoreMotion", false, false)

PreservedAnalyses
MergedLoadStoreMotionPass::run(Function &F, FunctionAnalysisManager &AM) {
MergedLoadStoreMotion Impl;
auto *MD = AM.getCachedResult<MemoryDependenceAnalysis>(F);
auto &AA = AM.getResult<AAManager>(F);
if (!Impl.run(F, MD, AA))
if (!Impl.run(F, AA))
return PreservedAnalyses::all();

// FIXME: This should also 'preserve the CFG'.
PreservedAnalyses PA;
PA.preserve<GlobalsAA>();
PA.preserve<MemoryDependenceAnalysis>();
return PA;
}