58 #define DEBUG_TYPE "mem2reg" 60 STATISTIC(NumLocalPromoted,
"Number of alloca's promoted within one block");
61 STATISTIC(NumSingleStore,
"Number of alloca's promoted with a single store");
62 STATISTIC(NumDeadAlloca,
"Number of dead alloca's removed");
63 STATISTIC(NumPHIInsert,
"Number of PHI nodes inserted");
72 if (
const LoadInst *LI = dyn_cast<LoadInst>(U)) {
77 }
else if (
const StoreInst *
SI = dyn_cast<StoreInst>(U)) {
78 if (
SI->getOperand(0) == AI)
84 }
else if (
const IntrinsicInst *II = dyn_cast<IntrinsicInst>(U)) {
85 if (!II->isLifetimeStartOrEnd())
87 }
else if (
const BitCastInst *BCI = dyn_cast<BitCastInst>(U)) {
95 if (!GEPI->hasAllZeroIndices())
115 bool OnlyUsedInOneBlock;
117 Value *AllocaPointerVal;
121 DefiningBlocks.
clear();
125 OnlyUsedInOneBlock =
true;
126 AllocaPointerVal =
nullptr;
144 AllocaPointerVal =
SI->getOperand(0);
147 LoadInst *LI = cast<LoadInst>(User);
151 AllocaPointerVal = LI;
154 if (OnlyUsedInOneBlock) {
158 OnlyUsedInOneBlock =
false;
167 struct RenamePassData {
168 using ValVector = std::vector<Value *>;
169 using LocationVector = std::vector<DebugLoc>;
172 : BB(B), Pred(P), Values(std::move(V)), Locations(std::move(L)) {}
177 LocationVector Locations;
185 class LargeBlockInfo {
196 static bool isInterestingInstruction(
const Instruction *
I) {
197 return (isa<LoadInst>(I) && isa<AllocaInst>(I->
getOperand(0))) ||
198 (isa<StoreInst>(
I) && isa<AllocaInst>(I->
getOperand(1)));
202 unsigned getInstructionIndex(
const Instruction *I) {
203 assert(isInterestingInstruction(I) &&
204 "Not a load/store to/from an alloca?");
208 if (It != InstNumbers.
end())
217 if (isInterestingInstruction(&BBI))
218 InstNumbers[&BBI] = InstNo++;
219 It = InstNumbers.
find(I);
221 assert(It != InstNumbers.
end() &&
"Didn't insert instruction?");
230 struct PromoteMem2Reg {
232 std::vector<AllocaInst *> Allocas;
260 std::vector<Value *> PointerAllocaValues;
280 : Allocas(Allocas.
begin(), Allocas.
end()), DT(DT),
288 void RemoveFromAllocasList(
unsigned &AllocaIdx) {
289 Allocas[AllocaIdx] = Allocas.
back();
295 unsigned &NP = BBNumPreds[BB];
305 RenamePassData::ValVector &IncVals,
306 RenamePassData::LocationVector &IncLocs,
307 std::vector<RenamePassData> &Worklist);
319 LoadNotNull->insertAfter(LI);
332 if (isa<LoadInst>(I) || isa<StoreInst>(
I))
361 bool StoringGlobalVal = !isa<Instruction>(OnlyStore->
getOperand(0));
366 Info.UsingBlocks.clear();
370 if (!isa<LoadInst>(UserInst)) {
371 assert(UserInst == OnlyStore &&
"Should only have load/stores");
374 LoadInst *LI = cast<LoadInst>(UserInst);
380 if (!StoringGlobalVal) {
385 if (StoreIndex == -1)
386 StoreIndex = LBI.getInstructionIndex(OnlyStore);
388 if (
unsigned(StoreIndex) > LBI.getInstructionIndex(LI)) {
390 Info.UsingBlocks.push_back(StoreBB);
398 Info.UsingBlocks.push_back(LI->
getParent());
423 if (!Info.UsingBlocks.empty())
432 LBI.deleteValue(DII);
435 Info.OnlyStore->eraseFromParent();
436 LBI.deleteValue(Info.OnlyStore);
471 StoresByIndexTy StoresByIndex;
475 StoresByIndex.
push_back(std::make_pair(LBI.getInstructionIndex(
SI),
SI));
488 unsigned LoadIdx = LBI.getInstructionIndex(LI);
491 StoresByIndexTy::iterator
I =
493 std::make_pair(LoadIdx,
494 static_cast<StoreInst *>(
nullptr)),
496 if (I == StoresByIndex.begin()) {
497 if (StoresByIndex.empty())
508 Value *ReplVal = std::prev(I)->second->getOperand(0);
543 LBI.deleteValue(DII);
550 void PromoteMem2Reg::run() {
551 Function &
F = *DT.getRoot()->getParent();
553 AllocaDbgDeclares.resize(Allocas.size());
559 for (
unsigned AllocaNum = 0; AllocaNum != Allocas.size(); ++AllocaNum) {
564 "All allocas should be in the same function, which is same as DF!");
573 RemoveFromAllocasList(AllocaNum);
580 Info.AnalyzeAlloca(AI);
584 if (Info.DefiningBlocks.size() == 1) {
587 RemoveFromAllocasList(AllocaNum);
595 if (Info.OnlyUsedInOneBlock &&
598 RemoveFromAllocasList(AllocaNum);
604 if (BBNumbers.empty()) {
607 BBNumbers[&BB] = ID++;
611 if (!Info.DbgDeclares.empty())
612 AllocaDbgDeclares[AllocaNum] = Info.DbgDeclares;
615 AllocaLookup[Allocas[AllocaNum]] = AllocaNum;
624 DefBlocks.
insert(Info.DefiningBlocks.begin(), Info.DefiningBlocks.end());
639 if (PHIBlocks.
size() > 1)
641 return BBNumbers.lookup(A) < BBNumbers.lookup(B);
646 QueuePhiNode(BB, AllocaNum, CurrentVersion);
657 RenamePassData::ValVector Values(Allocas.size());
658 for (
unsigned i = 0, e = Allocas.size(); i != e; ++i)
663 RenamePassData::LocationVector Locations(Allocas.size());
667 std::vector<RenamePassData> RenamePassWorkList;
668 RenamePassWorkList.emplace_back(&F.
front(),
nullptr, std::move(Values),
669 std::move(Locations));
671 RenamePassData RPD = std::move(RenamePassWorkList.back());
672 RenamePassWorkList.pop_back();
674 RenamePass(RPD.BB, RPD.Pred, RPD.Values, RPD.Locations, RenamePassWorkList);
675 }
while (!RenamePassWorkList.empty());
687 A->eraseFromParent();
691 for (
auto &Declares : AllocaDbgDeclares)
692 for (
auto *DII : Declares)
693 DII->eraseFromParent();
699 bool EliminatedAPHI =
true;
700 while (EliminatedAPHI) {
701 EliminatedAPHI =
false;
708 I = NewPhiNodes.begin(),
709 E = NewPhiNodes.end();
718 EliminatedAPHI =
true;
731 I = NewPhiNodes.begin(),
732 E = NewPhiNodes.end();
738 if (&BB->
front() != SomePHI)
754 return BBNumbers.lookup(A) < BBNumbers.lookup(
B);
766 "PHI node has entry for a block which is not a predecessor!");
778 while ((SomePHI = dyn_cast<PHINode>(BBI++)) &&
802 Info.UsingBlocks.end());
807 for (
unsigned i = 0, e = LiveInBlockWorklist.size(); i != e; ++i) {
809 if (!DefBlocks.
count(BB))
816 if (
SI->getOperand(1) != AI)
821 LiveInBlockWorklist[i] = LiveInBlockWorklist.
back();
822 LiveInBlockWorklist.pop_back();
828 if (
LoadInst *LI = dyn_cast<LoadInst>(
I)) {
829 if (LI->getOperand(0) != AI)
841 while (!LiveInBlockWorklist.empty()) {
842 BasicBlock *BB = LiveInBlockWorklist.pop_back_val();
846 if (!LiveInBlocks.
insert(BB).second)
858 LiveInBlockWorklist.push_back(
P);
866 bool PromoteMem2Reg::QueuePhiNode(
BasicBlock *BB,
unsigned AllocaNo,
869 PHINode *&PN = NewPhiNodes[std::make_pair(BBNumbers[BB], AllocaNo)];
877 PN =
PHINode::Create(Allocas[AllocaNo]->getAllocatedType(), getNumPreds(BB),
881 PhiToAllocaMap[PN] = AllocaNo;
888 bool ApplyMergedLoc) {
901 RenamePassData::ValVector &IncomingVals,
902 RenamePassData::LocationVector &IncomingLocs,
903 std::vector<RenamePassData> &Worklist) {
910 if (PhiToAllocaMap.count(APN)) {
917 unsigned NewPHINumOperands = APN->getNumOperands();
920 assert(NumEdges &&
"Must be at least one edge from Pred to BB!");
925 unsigned AllocaNo = PhiToAllocaMap[APN];
929 APN->getNumIncomingValues() > 0);
932 for (
unsigned i = 0; i != NumEdges; ++i)
933 APN->addIncoming(IncomingVals[AllocaNo], Pred);
936 IncomingVals[AllocaNo] = APN;
948 }
while (APN->getNumOperands() == NewPHINumOperands);
953 if (!Visited.insert(BB).second)
959 if (
LoadInst *LI = dyn_cast<LoadInst>(I)) {
965 if (AI == AllocaLookup.
end())
968 Value *V = IncomingVals[AI->second];
978 LI->replaceAllUsesWith(V);
980 }
else if (
StoreInst *
SI = dyn_cast<StoreInst>(I)) {
988 if (ai == AllocaLookup.
end())
992 unsigned AllocaNo = ai->second;
993 IncomingVals[AllocaNo] =
SI->getOperand(0);
996 IncomingLocs[AllocaNo] =
SI->getDebugLoc();
1018 if (VisitedSuccs.
insert(*I).second)
1019 Worklist.emplace_back(*I, Pred, IncomingVals, IncomingLocs);
1027 if (Allocas.
empty())
1030 PromoteMem2Reg(Allocas, DT, AC).run();
SymbolTableList< Instruction >::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
A parsed version of the target data layout string in and methods for querying it. ...
void addIncoming(Value *V, BasicBlock *BB)
Add an incoming value to the end of the PHI list.
const T & back() const
back - Get the last element.
iterator erase(iterator where)
This class represents lattice values for constants.
static CallInst * Create(FunctionType *Ty, Value *F, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
void push_back(const T &Elt)
This class represents a function call, abstracting a target machine's calling convention.
A cache of @llvm.assume calls within a function.
STATISTIC(NumFunctions, "Total number of functions")
An instruction for reading from memory.
TinyPtrVector - This class is specialized for cases where there are normally 0 or 1 element in a vect...
bool isAllocaPromotable(const AllocaInst *AI)
Return true if this alloca is legal for promotion.
static Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
iterator begin()
Instruction iterator methods.
const DataLayout & getDataLayout() const
Get the data layout for the module's target platform.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
PointerType * getType() const
Overload to return most specific pointer type.
void setDefiningBlocks(const SmallPtrSetImpl< BasicBlock *> &Blocks)
Give the IDF calculator the set of blocks in which the value is defined.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
static StringRef getName(Value *V)
Interval::succ_iterator succ_begin(Interval *I)
succ_begin/succ_end - define methods so that Intervals may be used just like BasicBlocks can with the...
Type * getType() const
All values are typed, get the type of this value.
This is the common base class for debug info intrinsics for variables.
This class represents a no-op cast from one type to another.
MDNode * getMetadata(unsigned KindID) const
Get the metadata of given kind attached to this Instruction.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
auto lower_bound(R &&Range, ForwardIt I) -> decltype(adl_begin(Range))
Provide wrappers to std::lower_bound which take ranges instead of having to pass begin/end explicitly...
An instruction for storing to memory.
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree...
Function * getDeclaration(Module *M, ID id, ArrayRef< Type *> Tys=None)
Create or insert an LLVM Function declaration for an intrinsic, and return it.
Value * getOperand(unsigned i) const
Analysis containing CSE Info
auto count(R &&Range, const E &Element) -> typename std::iterator_traits< decltype(adl_begin(Range))>::difference_type
Wrapper function around std::count to count the number of times an element Element occurs in the give...
Interval::succ_iterator succ_end(Interval *I)
iterator find(const_arg_type_t< KeyT > Val)
bool isVoidTy() const
Return true if this is 'void'.
an instruction for type-safe pointer arithmetic to access elements of arrays and structs ...
bool erase(const KeyT &Val)
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
void setDebugLoc(DebugLoc Loc)
Set the debug location information for this instruction.
LLVM Basic Block Representation.
static void ComputeLiveInBlocks(const SmallPtrSetImpl< BasicBlock *> &UsingBlocks, const SmallPtrSetImpl< BasicBlock *> &DefBlocks, SmallPtrSetImpl< BasicBlock *> &LiveInBlocks, PredIteratorCache &PredCache)
Given sets of UsingBlocks and DefBlocks, compute the set of LiveInBlocks.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator begin()
This file contains the declarations for the subclasses of Constant, which represent the different fla...
bool onlyUsedByLifetimeMarkers(const Value *V)
Return true if the only users of this pointer are lifetime markers.
const Instruction & front() const
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...
const Instruction & back() const
This instruction compares its operands according to the predicate given to the constructor.
Interval::pred_iterator pred_end(Interval *I)
unsigned getAddressSpace() const
Return the address space of the Pointer type.
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
static UndefValue * get(Type *T)
Static factory methods - Return an 'undef' object of the specified type.
iterator erase(const_iterator CI)
Compute iterated dominance frontiers using a linear time algorithm.
static PointerType * getInt8PtrTy(LLVMContext &C, unsigned AS=0)
void sort(IteratorTy Start, IteratorTy End)
const InstListType & getInstList() const
Return the underlying instruction list container.
void setLiveInBlocks(const SmallPtrSetImpl< BasicBlock *> &Blocks)
Give the IDF calculator the set of blocks in which the value is live on entry to the block...
Iterator for intrusive lists based on ilist_node.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
TinyPtrVector< DbgVariableIntrinsic * > FindDbgAddrUses(Value *V)
Finds all intrinsics declaring local variables as living in the memory that 'V' points to...
void calculate(SmallVectorImpl< BasicBlock *> &IDFBlocks)
Calculate iterated dominance frontiers.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
bool dominates(const Instruction *Def, const Use &U) const
Return true if Def dominates a use in User.
Module.h This file contains the declarations for the Module class.
Instruction * user_back()
Specialize the methods defined in Value, as we know that an instruction can only be used by other ins...
void applyMergedLocation(const DILocation *LocA, const DILocation *LocB)
Merge 2 debug locations and apply it to the Instruction.
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...
pred_range predecessors(BasicBlock *BB)
unsigned getNumIncomingValues() const
Return the number of incoming edges.
const Module * getModule() const
Return the module owning the function this instruction belongs to or nullptr it the function does not...
bool isKnownNonZero(const Value *V, const DataLayout &DL, unsigned Depth=0, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true)
Return true if the given value is known to be non-zero when defined.
iterator_range< user_iterator > users()
static void clear(coro::Shape &Shape)
This file provides various utilities for inspecting and working with the control flow graph in LLVM I...
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator end()
const DebugLoc & getDebugLoc() const
Return the debug location for this node as a DebugLoc.
void registerAssumption(CallInst *CI)
Add an @llvm.assume intrinsic to this function's cache.
BasicBlock * getIncomingBlock(unsigned i) const
Return incoming basic block number i.
const Function * getParent() const
Return the enclosing method, or null if none.
void insertAfter(Instruction *InsertPos)
Insert an unlinked instruction into a basic block immediately after the specified instruction...
unsigned pred_size(const BasicBlock *BB)
Get the number of predecessors of BB.
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Determine the iterated dominance frontier, given a set of defining blocks, and optionally, a set of live-in blocks.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
user_iterator user_begin()
const BasicBlock & front() const
Module * getParent()
Get the module that this global value is contained inside of...
LLVM Value Representation.
void ConvertDebugDeclareToDebugValue(DbgVariableIntrinsic *DII, StoreInst *SI, DIBuilder &Builder)
===---------------------------------------------------------------——===// Dbg Intrinsic utilities ...
void PromoteMemToReg(ArrayRef< AllocaInst *> Allocas, DominatorTree &DT, AssumptionCache *AC=nullptr)
Promote the specified list of alloca instructions into scalar registers, inserting PHI nodes as appro...
Value * SimplifyInstruction(Instruction *I, const SimplifyQuery &Q, OptimizationRemarkEmitter *ORE=nullptr)
See if we can compute a simplified version of this instruction.
Function object to check whether the first component of a std::pair compares less than the first comp...
bool empty() const
empty - Check if the array is empty.
A wrapper class for inspecting calls to intrinsic functions.
const BasicBlock * getParent() const
an instruction to allocate memory on the stack