30 #define DEBUG_TYPE "memoryssa"
45 return getPreviousDefFromEnd(Pred);
46 }
else if (VisitedBlocks.count(BB)) {
50 return MSSA->createMemoryPhi(BB);
51 }
else if (VisitedBlocks.insert(BB).second) {
59 PhiOps.
push_back(getPreviousDefFromEnd(Pred));
64 bool PHIExistsButNeedsUpdate =
false;
70 PHIExistsButNeedsUpdate =
true;
74 auto *Result = tryRemoveTrivialPhi(Phi, PhiOps);
78 Phi = MSSA->createMemoryPhi(BB);
81 if (PHIExistsButNeedsUpdate) {
92 if (
MemoryPhi *MP = dyn_cast<MemoryPhi>(Result))
93 InsertedPHIs.push_back(MP);
95 VisitedBlocks.erase(BB);
106 auto *LocalResult = getPreviousDefInBlock(MA);
108 return LocalResult ? LocalResult : getPreviousDefRecursive(MA->
getBlock());
120 if (!isa<MemoryUse>(MA)) {
123 if (Iter != Defs->rend())
129 if (!isa<MemoryUse>(U))
130 return cast<MemoryAccess>(&U);
145 return getPreviousDefRecursive(BB);
154 for (
auto &U : Uses) {
155 if (
MemoryPhi *UsePhi = dyn_cast<MemoryPhi>(&*U)) {
156 auto OperRange = UsePhi->operands();
157 tryRemoveTrivialPhi(UsePhi, OperRange);
168 template <
class RangeType>
170 RangeType &Operands) {
173 for (
auto &
Op : Operands) {
175 if (
Op == Phi ||
Op == Same)
180 Same = cast<MemoryAccess>(
Op);
192 return recursePhi(Same);
196 InsertedPHIs.clear();
214 assert(i != -1 &&
"Should have found the basic block in the phi");
233 InsertedPHIs.clear();
237 bool DefBeforeSameBlock = DefBefore !=
nullptr;
239 DefBefore = getPreviousDefRecursive(MD->
getBlock());
245 if (DefBeforeSameBlock) {
250 if (isa<MemoryUse>(U.
getUser()))
263 if (!DefBeforeSameBlock) {
278 while (!FixupList.empty()) {
279 unsigned StartingPHISize = InsertedPHIs.size();
280 fixupDefs(FixupList);
283 FixupList.append(InsertedPHIs.end() - StartingPHISize, InsertedPHIs.end());
294 if (
auto *MD = dyn_cast<MemoryDef>(FirstDef))
300 for (
auto *MP : InsertedPHIs)
301 MSSA->
renamePass(MP->getBlock(),
nullptr, Visited);
308 for (
auto *NewDef : Vars) {
311 auto DefIter = NewDef->getDefsIterator();
314 if (++DefIter != Defs->end()) {
315 cast<MemoryDef>(DefIter)->setDefiningAccess(NewDef);
322 for (
const auto *S :
successors(NewDef->getBlock())) {
329 while (!Worklist.
empty()) {
335 auto *FirstDef = &*Defs->begin();
337 assert(!isa<MemoryPhi>(FirstDef) &&
338 "Should have already handled phi nodes!");
342 "Should have dominated the new access");
347 cast<MemoryDef>(FirstDef)->setDefiningAccess(getPreviousDef(FirstDef));
351 for (
const auto *S :
successors(FixupBlock)) {
359 if (!Seen.
insert(S).second)
369 template <
class WhereType>
376 MSSA->
moveTo(What, BB, Where);
379 if (
auto *MD = dyn_cast<MemoryDef>(What))
397 return moveTo(What, BB, Where);
407 MA = cast<MemoryAccess>(Arg);
416 "Trying to remove the live on entry def");
420 if (
MemoryPhi *MP = dyn_cast<MemoryPhi>(MA)) {
427 assert((NewDefTarget || MP->use_empty()) &&
428 "We can't delete this memory phi");
430 NewDefTarget = cast<MemoryUseOrDef>(MA)->getDefiningAccess();
434 if (!isa<MemoryUse>(MA) && !MA->
use_empty()) {
450 if (
auto *MUD = dyn_cast<MemoryUseOrDef>(U.
getUser()))
451 MUD->resetOptimized();
473 "New and old access must be in the same block");
483 "New and old access must be in the same block");
void push_back(const T &Elt)
bool dominates(const MemoryAccess *A, const MemoryAccess *B) const
Given two memory accesses in potentially different blocks, determine whether MemoryAccess A dominates...
static MemoryAccess * onlySingleValue(MemoryPhi *MP)
If all arguments of a MemoryPHI are defined by the same incoming argument, return that argument...
AllAccessType::self_iterator getIterator()
Get the iterators for the all access list and the defs only list We default to the all access list...
unsigned getNumOperands() const
void insertIntoListsForBlock(MemoryAccess *, const BasicBlock *, InsertionPlace)
void moveAfter(MemoryUseOrDef *What, MemoryUseOrDef *Where)
Represents a read-write access to memory, whether it is a must-alias, or a may-alias.
void insertUse(MemoryUse *Use)
void setIncomingValue(unsigned I, MemoryAccess *V)
Represents read-only accesses to memory.
block_iterator block_begin()
A Use represents the edge between a Value definition and its users.
AccessList * getWritableBlockAccesses(const BasicBlock *BB) const
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
MemoryUseOrDef * getMemoryAccess(const Instruction *) const
Given a memory Mod/Ref'ing instruction, get the MemorySSA access associated with it.
MemoryAccess * getDefiningAccess() const
Get the access that produces the memory state used by this Use.
LLVM_NODISCARD bool empty() const
BasicBlock * getBlock() const
void removeMemoryAccess(MemoryAccess *)
Remove a MemoryAccess from MemorySSA, including updating all definitions and uses.
User * getUser() const LLVM_READONLY
Returns the User that contains this Use.
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
static void ValueIsRAUWd(Value *Old, Value *New)
MemoryUseOrDef * createMemoryAccessBefore(Instruction *I, MemoryAccess *Definition, MemoryUseOrDef *InsertPt)
Create a MemoryAccess in MemorySSA before or after an existing MemoryAccess.
void renamePass(BasicBlock *BB, MemoryAccess *IncomingVal, SmallPtrSetImpl< BasicBlock * > &Visited)
AllAccessType::reverse_self_iterator getReverseIterator()
LLVM Basic Block Representation.
static void setMemoryPhiValueForBlock(MemoryPhi *MP, const BasicBlock *BB, MemoryAccess *NewDef)
Value handle that tracks a Value across RAUW.
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator begin()
void addIncoming(MemoryAccess *V, BasicBlock *BB)
Add an incoming value to the end of the PHI list.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
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...
InsertionPlace
Used in various insertion functions to specify whether we are talking about the beginning or end of a...
static const unsigned End
Interval::pred_iterator pred_end(Interval *I)
bool hasValueHandle() const
Return true if there is a value handle associated with this value.
MemoryAccess * createMemoryAccessInBB(Instruction *I, MemoryAccess *Definition, const BasicBlock *BB, MemorySSA::InsertionPlace Point)
Create a MemoryAccess in MemorySSA at a specified point in a block, with a specified clobbering defin...
MemoryAccess * getLiveOnEntryDef() const
void moveToPlace(MemoryUseOrDef *What, BasicBlock *BB, MemorySSA::InsertionPlace Where)
void removeFromLists(MemoryAccess *, bool ShouldDelete=true)
Properly remove MA from all of MemorySSA's lists.
void insertIntoListsBefore(MemoryAccess *, const BasicBlock *, AccessList::iterator)
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Module.h This file contains the declarations for the Module class.
pred_range predecessors(BasicBlock *BB)
void moveTo(MemoryUseOrDef *What, BasicBlock *BB, AccessList::iterator Where)
const BasicBlock * getSinglePredecessor() const
Return the predecessor of this block if it has a single predecessor block.
Class that has the common methods + fields of memory uses/defs.
MemoryUseOrDef * createDefinedAccess(Instruction *, MemoryAccess *)
llvm_unreachable("Unknown RegBank!")
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator end()
block_iterator block_end()
void setDefiningAccess(MemoryAccess *DMA, bool Optimized=false)
MemoryUseOrDef * createMemoryAccessAfter(Instruction *I, MemoryAccess *Definition, MemoryAccess *InsertPt)
reverse_iterator rend(StringRef path)
Get reverse end iterator over path.
void removeFromLookups(MemoryAccess *)
Properly remove MA from all of MemorySSA's lookup tables.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
user_iterator user_begin()
void insertDef(MemoryDef *Def, bool RenameUses=false)
Insert a definition into the MemorySSA IR.
succ_range successors(BasicBlock *BB)
int getBasicBlockIndex(const BasicBlock *BB) const
Return the first index of the specified basic block in the value list for this PHI.
DefsOnlyType::reverse_self_iterator getReverseDefsIterator()
This file exposes an interface to building/using memory SSA to walk memory instructions using a use/d...
DefsList * getWritableBlockDefs(const BasicBlock *BB) const
Represents phi nodes for memory accesses.
void moveBefore(MemoryUseOrDef *What, MemoryUseOrDef *Where)
const BasicBlock * getParent() const
reverse_iterator rbegin()
bool isLiveOnEntryDef(const MemoryAccess *MA) const
Return true if MA represents the live on entry value.