95 #define DEBUG_TYPE "mldst-motion"
109 const int MagicCompileTimeControl = 250;
125 bool isStoreSinkBarrierInRange(
const Instruction &Start,
135 void MergedLoadStoreMotion::removeInstruction(
Instruction *Inst) {
138 MD->removeInstruction(Inst);
139 if (
auto *LI = dyn_cast<LoadInst>(Inst))
140 MD->invalidateCachedPointerInfo(LI->getPointerOperand());
142 MD->invalidateCachedPointerInfo(Inst);
152 assert(isDiamondHead(BB) &&
"Basic block is not head of a diamond");
159 bool MergedLoadStoreMotion::isDiamondHead(
BasicBlock *BB) {
163 if (!BI || !BI->isConditional())
177 if (!Succ0Succ || !Succ1Succ || Succ0Succ != Succ1Succ)
191 bool MergedLoadStoreMotion::isStoreSinkBarrierInRange(
const Instruction &Start,
198 return AA->canInstructionRangeModRef(Start, End, Loc,
MRI_ModRef);
218 !isStoreSinkBarrierInRange(*Store1->getNextNode(), BB1->
back(), Loc1) &&
219 !isStoreSinkBarrierInRange(*Store0->
getNextNode(), BB0->back(), Loc0)) {
240 NewPN->addIncoming(Opd2, S1->
getParent());
241 if (MD && NewPN->getType()->isPtrOrPtrVectorTy())
242 MD->invalidateCachedPointerInfo(NewPN);
256 if (A0 && A1 && A0->isIdenticalTo(A1) && A0->hasOneUse() &&
257 (A0->getParent() == S0->
getParent()) && A1->hasOneUse() &&
258 (A1->getParent() == S1->
getParent()) && isa<GetElementPtrInst>(A0)) {
260 dbgs() <<
"Instruction Left\n"; S0->
dump();
dbgs() <<
"\n";
261 dbgs() <<
"Instruction Right\n"; S1->
dump();
dbgs() <<
"\n");
272 ANew->insertBefore(SNew);
278 if (
PHINode *NewPN = getPHIOperand(BB, S0, S1))
280 removeInstruction(S0);
281 removeInstruction(S1);
282 A0->replaceAllUsesWith(ANew);
283 removeInstruction(A0);
284 A1->replaceAllUsesWith(ANew);
285 removeInstruction(A1);
297 bool MergedLoadStoreMotion::mergeStores(
BasicBlock *
T) {
299 bool MergedStores =
false;
300 assert(T &&
"Footer of a diamond cannot be empty");
315 int Size1 = Pred1->
size();
330 if (NStores * Size1 >= MagicCompileTimeControl)
332 if (
StoreInst *S1 = canSinkFromBlock(Pred1, S0)) {
333 bool Res = sinkStore(T, S0, S1);
354 bool Changed =
false;
364 if (isDiamondHead(BB)) {
365 Changed |= mergeStores(getDiamondTail(BB));
372 class MergedLoadStoreMotionLegacyPass :
public FunctionPass {
383 bool runOnFunction(
Function &F)
override {
387 auto *MDWP = getAnalysisIfAvailable<MemoryDependenceWrapperPass>();
388 return Impl.run(F, MDWP ? &MDWP->getMemDep() :
nullptr,
389 getAnalysis<AAResultsWrapperPass>().getAAResults());
408 return new MergedLoadStoreMotionLegacyPass();
412 "MergedLoadStoreMotion",
false,
false)
420 MergedLoadStoreMotion Impl;
423 if (!Impl.run(F, MD, AA))
Legacy wrapper pass to provide the GlobalsAAResult object.
Value * getValueOperand()
SymbolTableList< Instruction >::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
Provides a lazy, caching interface for making common memory aliasing information queries, backed by LLVM's alias analysis passes.
void addIncoming(Value *V, BasicBlock *BB)
Add an incoming value to the end of the PHI list.
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
const Instruction & back() const
NodeTy * getNextNode()
Get the next node, or nullptr for the list tail.
This is the interface for a simple mod/ref and alias analysis over globals.
void dropUnknownNonDebugMetadata(ArrayRef< unsigned > KnownIDs)
Drop all unknown metadata except for debug locations.
bool isPtrOrPtrVectorTy() const
Return true if this is a pointer type or a vector of pointer types.
const Function * getParent() const
Return the enclosing method, or null if none.
const Instruction & front() const
reverse_iterator rbegin()
StringRef getName() const
Return a constant reference to the value's name.
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
mldst MergedLoadStoreMotion
An analysis that produces MemoryDependenceResults for a function.
Instruction * clone() const
Create a copy of 'this' instruction that is identical in all ways except the following: ...
auto reverse(ContainerTy &&C, typename std::enable_if< has_rbegin< ContainerTy >::value >::type *=nullptr) -> decltype(make_range(C.rbegin(), C.rend()))
void andIRFlags(const Value *V)
Logical 'and' of any supported wrapping, exact, and fast-math flags of V and this instruction...
An instruction for storing to memory.
const BasicBlock * getSingleSuccessor() const
Return the successor of this block if it has a single successor.
static MemoryLocation get(const LoadInst *LI)
Return a location with information about the memory reference by the given instruction.
A set of analyses that are preserved following a run of a transformation pass.
void insertBefore(Instruction *InsertPos)
Insert an unlinked instruction into a basic block immediately before the specified instruction...
LLVM Basic Block Representation.
Conditional or Unconditional Branch instruction.
const_iterator getFirstInsertionPt() const
Returns an iterator to the first instruction in this block that is suitable for inserting a non-PHI i...
A manager for alias analyses.
Interval::pred_iterator pred_begin(Interval *I)
pred_begin/pred_end - define methods so that Intervals may be used just like BasicBlocks can with the...
Represent the analysis usage information of a pass.
Analysis pass providing a never-invalidated alias analysis result.
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE,"Assign register bank of generic virtual registers", false, false) RegBankSelect
static const unsigned End
FunctionPass class - This class is used to implement most global optimizations.
Interval::pred_iterator pred_end(Interval *I)
self_iterator getIterator()
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
A wrapper analysis pass for the legacy pass manager that exposes a MemoryDepnedenceResults instance...
void dump() const
Support for debugging, callable in GDB: V->dump()
Representation for a specific memory location.
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
Iterator for intrusive lists based on ilist_node.
Type * getType() const
All values are typed, get the type of this value.
static PHINode * Create(Type *Ty, unsigned NumReservedValues, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
Constructors - NumReservedValues is a hint for the number of incoming edges that this phi node will h...
void setPreservesCFG()
This function should be called by the pass, iff they do not:
BasicBlock * getSuccessor(unsigned idx) const
Return the specified successor.
const BasicBlock * getSinglePredecessor() const
Return the predecessor of this block if it has a single predecessor block.
void setOperand(unsigned i, Value *Val)
FunctionPass * createMergedLoadStoreMotionPass()
createMergedLoadStoreMotionPass - The public interface to this file.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
INITIALIZE_PASS_BEGIN(MergedLoadStoreMotionLegacyPass,"mldst-motion","MergedLoadStoreMotion", false, false) INITIALIZE_PASS_END(MergedLoadStoreMotionLegacyPass
Represents analyses that only rely on functions' control flow.
This pass performs merges of loads and stores on both sides of a.
bool mayThrow() const
Return true if this instruction may throw an exception.
void preserveSet()
Mark an analysis set as preserved.
The access both references and modifies the value stored in memory.
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
void preserve()
Mark an analysis as preserved.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM Value Representation.
A container for analyses that lazily runs them and caches their results.
bool isSameOperationAs(const Instruction *I, unsigned flags=0) const
This function determines if the specified instruction executes the same operation as the current one...
A wrapper pass to provide the legacy pass manager access to a suitably prepared AAResults object...
const TerminatorInst * getTerminator() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
void initializeMergedLoadStoreMotionLegacyPassPass(PassRegistry &)
Value * getPointerOperand()
const BasicBlock * getParent() const