28 #define DEBUG_TYPE "memoryssa" 45 auto Cached = CachedPreviousDef.find(BB);
46 if (Cached != CachedPreviousDef.end()) {
47 return Cached->second;
53 CachedPreviousDef.insert({BB, Result});
57 if (VisitedBlocks.count(BB)) {
62 CachedPreviousDef.insert({BB, Result});
66 if (VisitedBlocks.insert(BB).second) {
74 PhiOps.
push_back(getPreviousDefFromEnd(Pred, CachedPreviousDef));
81 auto *
Result = tryRemoveTrivialPhi(Phi, PhiOps);
85 Phi = MSSA->createMemoryPhi(BB);
101 InsertedPHIs.push_back(Phi);
107 VisitedBlocks.erase(BB);
108 CachedPreviousDef.insert({BB, Result});
119 if (
auto *LocalResult = getPreviousDefInBlock(MA))
122 return getPreviousDefRecursive(MA->
getBlock(), CachedPreviousDef);
134 if (!isa<MemoryUse>(MA)) {
137 if (Iter != Defs->rend())
143 if (!isa<MemoryUse>(U))
144 return cast<MemoryAccess>(&U);
161 return getPreviousDefRecursive(BB, CachedPreviousDef);
170 for (
auto &U : Uses) {
171 if (
MemoryPhi *UsePhi = dyn_cast<MemoryPhi>(&*U)) {
172 auto OperRange = UsePhi->operands();
173 tryRemoveTrivialPhi(UsePhi, OperRange);
184 template <
class RangeType>
186 RangeType &Operands) {
188 if (NonOptPhis.count(Phi))
193 for (
auto &
Op : Operands) {
195 if (
Op == Phi ||
Op == Same)
200 Same = cast<MemoryAccess>(&*
Op);
212 return recursePhi(Same);
216 InsertedPHIs.clear();
234 assert(i != -1 &&
"Should have found the basic block in the phi");
253 InsertedPHIs.clear();
263 if (DefBeforeSameBlock) {
268 if (isa<MemoryUse>(U.
getUser()))
280 if (!DefBeforeSameBlock) {
295 while (!FixupList.empty()) {
296 unsigned StartingPHISize = InsertedPHIs.size();
297 fixupDefs(FixupList);
300 FixupList.append(InsertedPHIs.begin() + StartingPHISize, InsertedPHIs.end());
311 if (
auto *MD = dyn_cast<MemoryDef>(FirstDef))
317 for (
auto &MP : InsertedPHIs) {
318 MemoryPhi *Phi = dyn_cast_or_null<MemoryPhi>(MP);
328 for (
auto &Var : Vars) {
329 MemoryAccess *NewDef = dyn_cast_or_null<MemoryAccess>(Var);
337 if (
MemoryPhi *Phi = dyn_cast<MemoryPhi>(NewDef))
338 NonOptPhis.erase(Phi);
341 if (++DefIter != Defs->end()) {
342 cast<MemoryDef>(DefIter)->setDefiningAccess(NewDef);
356 while (!Worklist.
empty()) {
362 auto *FirstDef = &*Defs->begin();
364 assert(!isa<MemoryPhi>(FirstDef) &&
365 "Should have already handled phi nodes!");
369 "Should have dominated the new access");
374 cast<MemoryDef>(FirstDef)->setDefiningAccess(getPreviousDef(FirstDef));
378 for (
const auto *S :
successors(FixupBlock)) {
386 if (!Seen.
insert(S).second)
396 template <
class WhereType>
400 for (
auto *U : What->
users())
401 if (
MemoryPhi *PhiUser = dyn_cast<MemoryPhi>(U))
402 NonOptPhis.insert(PhiUser);
408 MSSA->
moveTo(What, BB, Where);
411 if (
auto *MD = dyn_cast<MemoryDef>(What))
433 return moveTo(What, BB, Where);
451 auto *MUD = cast<MemoryUseOrDef>(FirstInNew);
456 : cast<MemoryUseOrDef>(&*NextIt);
469 "To block is expected to be free of MemoryAccesses.");
470 moveAllAccesses(From, To, Start);
473 MPhi->setIncomingBlock(MPhi->getBasicBlockIndex(From), To);
479 "From block is expected to have a single predecessor (To).");
480 moveAllAccesses(From, To, Start);
483 MPhi->setIncomingBlock(MPhi->getBasicBlockIndex(From), To);
493 MA = cast<MemoryAccess>(
Arg);
503 "Access list should be null for a new block.");
509 "Should have moved all predecessors.");
512 assert(!Preds.
empty() &&
"Must be moving at least one predecessor to the " 513 "new immediate predecessor.");
514 MemoryPhi *NewPhi = MSSA->createMemoryPhi(New);
517 if (PredsSet.count(B)) {
518 NewPhi->addIncoming(MA, B);
523 Phi->addIncoming(NewPhi, New);
531 "Trying to remove the live on entry def");
535 if (
MemoryPhi *MP = dyn_cast<MemoryPhi>(MA)) {
542 assert((NewDefTarget || MP->use_empty()) &&
543 "We can't delete this memory phi");
545 NewDefTarget = cast<MemoryUseOrDef>(MA)->getDefiningAccess();
549 if (!isa<MemoryUse>(MA) && !MA->
use_empty()) {
565 if (
auto *MUD = dyn_cast<MemoryUseOrDef>(U.
getUser()))
566 MUD->resetOptimized();
582 assert(TI &&
"Basic block expected to have a terminator instruction");
584 if (!DeadBlocks.count(Succ))
586 MP->unorderedDeleteIncomingBlock(BB);
587 if (MP->getNumIncomingValues() == 1)
601 for (
auto AB = Acc->begin(), AE = Acc->end(); AB != AE;) {
621 "New and old access must be in the same block");
631 "New and old access must be in the same block");
void push_back(const T &Elt)
AccessList * getWritableBlockAccesses(const BasicBlock *BB) const
static MemoryAccess * onlySingleValue(MemoryPhi *MP)
If all arguments of a MemoryPHI are defined by the same incoming argument, return that argument...
void dropAllReferences()
Drop all references to operands.
Compute iterated dominance frontiers using a linear time algorithm.
AllAccessType::self_iterator getIterator()
Get the iterators for the all access list and the defs only list We default to the all access list...
bool dominates(const MemoryAccess *A, const MemoryAccess *B) const
Given two memory accesses in potentially different blocks, determine whether MemoryAccess A dominates...
MemoryAccess * getDefiningAccess() const
Get the access that produces the memory state used by this Use.
MemoryUseOrDef * createDefinedAccess(Instruction *, MemoryAccess *)
const AccessList * getBlockAccesses(const BasicBlock *BB) const
Return the list of MemoryAccess's for a given basic block.
void removeBlocks(const SmallPtrSetImpl< BasicBlock *> &DeadBlocks)
Remove all MemoryAcceses in a set of BasicBlocks about to be deleted.
bool hasValueHandle() const
Return true if there is a value handle associated with this value.
void wireOldPredecessorsToNewImmediatePredecessor(BasicBlock *Old, BasicBlock *New, ArrayRef< BasicBlock *> Preds)
BasicBlock Old had New, an empty BasicBlock, added directly before it, and the predecessors in Preds ...
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)
int getBasicBlockIndex(const BasicBlock *BB) const
Return the first index of the specified basic block in the value list for this PHI.
DefsOnlyType::self_iterator getDefsIterator()
void setIncomingValue(unsigned I, MemoryAccess *V)
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
Represents read-only accesses to memory.
void renamePass(BasicBlock *BB, MemoryAccess *IncomingVal, SmallPtrSetImpl< BasicBlock *> &Visited)
block_iterator block_begin()
A Use represents the edge between a Value definition and its users.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void insertIntoListsForBlock(MemoryAccess *, const BasicBlock *, InsertionPlace)
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.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
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 removeFromLookups(MemoryAccess *)
Properly remove MA from all of MemorySSA's lookup tables.
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
Subclasses of this class are all able to terminate a basic block.
const BasicBlock * getSinglePredecessor() const
Return the predecessor of this block if it has a single predecessor block.
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.
size_t size() const
size - Get the array size.
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...
void moveAllAfterSpliceBlocks(BasicBlock *From, BasicBlock *To, Instruction *Start)
From block was spliced into From and To.
Interval::pred_iterator pred_end(Interval *I)
self_iterator getIterator()
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...
An intrusive list with ownership and callbacks specified/controlled by ilist_traits, only with API safe for polymorphic types.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
void moveToPlace(MemoryUseOrDef *What, BasicBlock *BB, MemorySSA::InsertionPlace Where)
MemoryUseOrDef * getMemoryAccess(const Instruction *) const
Given a memory Mod/Ref'ing instruction, get the MemorySSA access associated with it.
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
void setDefiningAccess(MemoryAccess *DMA, bool Optimized=false, Optional< AliasResult > AR=MayAlias)
unsigned getNumOperands() const
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.
BasicBlock * getBlock() const
pred_range predecessors(BasicBlock *BB)
MemoryAccess * getLiveOnEntryDef() const
void removeFromLists(MemoryAccess *, bool ShouldDelete=true)
Properly remove MA from all of MemorySSA's lists.
Class that has the common methods + fields of memory uses/defs.
iterator_range< user_iterator > users()
amdgpu Simplify well known AMD library false Value Value * Arg
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator end()
block_iterator block_end()
LLVM_NODISCARD bool empty() const
unsigned pred_size(const BasicBlock *BB)
void moveTo(MemoryUseOrDef *What, BasicBlock *BB, AccessList::iterator Where)
MemoryUseOrDef * createMemoryAccessAfter(Instruction *I, MemoryAccess *Definition, MemoryAccess *InsertPt)
void moveAllAfterMergeBlocks(BasicBlock *From, BasicBlock *To, Instruction *Start)
From block was merged into To.
bool isLiveOnEntryDef(const MemoryAccess *MA) const
Return true if MA represents the live on entry value.
reverse_iterator rend(StringRef path)
Get reverse end iterator over path.
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)
DefsOnlyType::reverse_self_iterator getReverseDefsIterator()
This file exposes an interface to building/using memory SSA to walk memory instructions using a use/d...
Represents phi nodes for memory accesses.
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...
DefsList * getWritableBlockDefs(const BasicBlock *BB) const
OutputIt copy(R &&Range, OutputIt Out)
void moveBefore(MemoryUseOrDef *What, MemoryUseOrDef *Where)
void insertIntoListsBefore(MemoryAccess *, const BasicBlock *, AccessList::iterator)
reverse_iterator rbegin()
bool empty() const
empty - Check if the array is empty.
const BasicBlock * getParent() const