28 #define DEBUG_TYPE "mem2reg"
54 STATISTIC(NumLocalPromoted,
"Number of alloca's promoted within one block");
55 STATISTIC(NumSingleStore,
"Number of alloca's promoted with a single store");
56 STATISTIC(NumDeadAlloca,
"Number of dead alloca's removed");
57 STATISTIC(NumPHIInsert,
"Number of PHI nodes inserted");
67 if (
const LoadInst *
LI = dyn_cast<LoadInst>(U)) {
72 }
else if (
const StoreInst *SI = dyn_cast<StoreInst>(U)) {
73 if (SI->getOperand(0) == AI)
79 }
else if (
const IntrinsicInst *II = dyn_cast<IntrinsicInst>(U)) {
83 }
else if (
const BitCastInst *BCI = dyn_cast<BitCastInst>(U)) {
91 if (!GEPI->hasAllZeroIndices())
111 bool OnlyUsedInOneBlock;
113 Value *AllocaPointerVal;
117 DefiningBlocks.clear();
121 OnlyUsedInOneBlock =
true;
122 AllocaPointerVal = 0;
138 if (
StoreInst *SI = dyn_cast<StoreInst>(User)) {
140 DefiningBlocks.push_back(SI->getParent());
148 AllocaPointerVal =
LI;
151 if (OnlyUsedInOneBlock) {
155 OnlyUsedInOneBlock =
false;
164 class RenamePassData {
166 typedef std::vector<Value *> ValVector;
168 RenamePassData() : BB(NULL), Pred(NULL), Values() {}
170 : BB(B), Pred(P), Values(V) {}
175 void swap(RenamePassData &RHS) {
178 Values.swap(RHS.Values);
187 class LargeBlockInfo {
198 static bool isInterestingInstruction(
const Instruction *
I) {
199 return (isa<LoadInst>(I) && isa<AllocaInst>(I->
getOperand(0))) ||
200 (isa<StoreInst>(
I) && isa<AllocaInst>(I->
getOperand(1)));
204 unsigned getInstructionIndex(
const Instruction *I) {
205 assert(isInterestingInstruction(I) &&
206 "Not a load/store to/from an alloca?");
210 if (It != InstNumbers.
end())
220 if (isInterestingInstruction(BBI))
221 InstNumbers[BBI] = InstNo++;
222 It = InstNumbers.
find(I);
224 assert(It != InstNumbers.
end() &&
"Didn't insert instruction?");
230 void clear() { InstNumbers.clear(); }
233 struct PromoteMem2Reg {
235 std::vector<AllocaInst *> Allocas;
260 std::vector<Value *> PointerAllocaValues;
284 : Allocas(Allocas.
begin(), Allocas.
end()), DT(DT),
290 void RemoveFromAllocasList(
unsigned &AllocaIdx) {
291 Allocas[AllocaIdx] = Allocas.back();
297 unsigned &NP = BBNumPreds[BB];
303 void DetermineInsertionPoint(
AllocaInst *AI,
unsigned AllocaNum,
305 void ComputeLiveInBlocks(
AllocaInst *AI, AllocaInfo &Info,
309 RenamePassData::ValVector &IncVals,
310 std::vector<RenamePassData> &Worklist);
311 bool QueuePhiNode(
BasicBlock *BB,
unsigned AllocaIdx,
unsigned &Version);
324 if (isa<LoadInst>(I) || isa<StoreInst>(
I))
355 bool StoringGlobalVal = !isa<Instruction>(OnlyStore->
getOperand(0));
360 Info.UsingBlocks.clear();
364 if (!isa<LoadInst>(UserInst)) {
365 assert(UserInst == OnlyStore &&
"Should only have load/stores");
368 LoadInst *LI = cast<LoadInst>(UserInst);
374 if (!StoringGlobalVal) {
379 if (StoreIndex == -1)
380 StoreIndex = LBI.getInstructionIndex(OnlyStore);
382 if (
unsigned(StoreIndex) > LBI.getInstructionIndex(LI)) {
384 Info.UsingBlocks.push_back(StoreBB);
393 Info.UsingBlocks.push_back(LI->
getParent());
412 if (!Info.UsingBlocks.empty())
420 DDI->eraseFromParent();
421 LBI.deleteValue(DDI);
424 Info.OnlyStore->eraseFromParent();
425 LBI.deleteValue(Info.OnlyStore);
457 StoresByIndexTy StoresByIndex;
461 if (
StoreInst *SI = dyn_cast<StoreInst>(*UI))
462 StoresByIndex.
push_back(std::make_pair(LBI.getInstructionIndex(SI), SI));
466 std::sort(StoresByIndex.begin(), StoresByIndex.end(),
less_first());
475 unsigned LoadIdx = LBI.getInstructionIndex(LI);
478 StoresByIndexTy::iterator I =
479 std::lower_bound(StoresByIndex.begin(), StoresByIndex.end(),
480 std::make_pair(LoadIdx, static_cast<StoreInst *>(0)),
483 if (I == StoresByIndex.begin())
515 DDI->eraseFromParent();
516 LBI.deleteValue(DDI);
522 void PromoteMem2Reg::run() {
523 Function &
F = *DT.getRoot()->getParent();
526 PointerAllocaValues.resize(Allocas.size());
527 AllocaDbgDeclares.resize(Allocas.size());
532 for (
unsigned AllocaNum = 0; AllocaNum != Allocas.size(); ++AllocaNum) {
537 "All allocas should be in the same function, which is same as DF!");
544 AST->deleteValue(AI);
548 RemoveFromAllocasList(AllocaNum);
555 Info.AnalyzeAlloca(AI);
559 if (Info.DefiningBlocks.size() == 1) {
562 RemoveFromAllocasList(AllocaNum);
570 if (Info.OnlyUsedInOneBlock) {
574 RemoveFromAllocasList(AllocaNum);
579 if (DomLevels.empty()) {
586 while (!Worklist.
empty()) {
588 unsigned ChildLevel = DomLevels[Node] + 1;
591 DomLevels[*CI] = ChildLevel;
599 if (BBNumbers.empty()) {
608 PointerAllocaValues[AllocaNum] = Info.AllocaPointerVal;
612 AllocaDbgDeclares[AllocaNum] = Info.DbgDeclare;
615 AllocaLookup[Allocas[AllocaNum]] = AllocaNum;
621 DetermineInsertionPoint(AI, AllocaNum, Info);
633 RenamePassData::ValVector Values(Allocas.size());
634 for (
unsigned i = 0, e = Allocas.size(); i != e; ++i)
640 std::vector<RenamePassData> RenamePassWorkList;
641 RenamePassWorkList.push_back(RenamePassData(F.
begin(), 0, Values));
644 RPD.swap(RenamePassWorkList.back());
645 RenamePassWorkList.pop_back();
647 RenamePass(RPD.BB, RPD.Pred, RPD.Values, RenamePassWorkList);
648 }
while (!RenamePassWorkList.empty());
654 for (
unsigned i = 0, e = Allocas.size(); i != e; ++i) {
668 for (
unsigned i = 0, e = AllocaDbgDeclares.size(); i != e; ++i)
670 DDI->eraseFromParent();
676 bool EliminatedAPHI =
true;
677 while (EliminatedAPHI) {
678 EliminatedAPHI =
false;
685 I = NewPhiNodes.begin(),
686 E = NewPhiNodes.end();
693 AST->deleteValue(PN);
696 NewPhiNodes.erase(I++);
697 EliminatedAPHI =
true;
711 I = NewPhiNodes.begin(),
712 E = NewPhiNodes.end();
718 if (&BB->
front() != SomePHI)
733 std::sort(Preds.begin(), Preds.end());
742 "PHI node has entry for a block which is not a predecessor!");
754 while ((SomePHI = dyn_cast<PHINode>(BBI++)) &&
757 for (
unsigned pred = 0, e = Preds.size(); pred != e; ++pred)
770 void PromoteMem2Reg::ComputeLiveInBlocks(
779 Info.UsingBlocks.end());
784 for (
unsigned i = 0, e = LiveInBlockWorklist.size(); i != e; ++i) {
786 if (!DefBlocks.
count(BB))
792 if (
StoreInst *SI = dyn_cast<StoreInst>(I)) {
793 if (SI->getOperand(1) != AI)
798 LiveInBlockWorklist[i] = LiveInBlockWorklist.
back();
799 LiveInBlockWorklist.pop_back();
804 if (
LoadInst *LI = dyn_cast<LoadInst>(I)) {
817 while (!LiveInBlockWorklist.empty()) {
818 BasicBlock *BB = LiveInBlockWorklist.pop_back_val();
822 if (!LiveInBlocks.
insert(BB))
832 if (DefBlocks.
count(P))
836 LiveInBlockWorklist.push_back(P);
845 void PromoteMem2Reg::DetermineInsertionPoint(
AllocaInst *AI,
unsigned AllocaNum,
849 DefBlocks.
insert(Info.DefiningBlocks.begin(), Info.DefiningBlocks.end());
854 ComputeLiveInBlocks(AI, Info, DefBlocks, LiveInBlocks);
858 typedef std::pair<DomTreeNode *, unsigned> DomTreeNodePair;
859 typedef std::priority_queue<DomTreeNodePair, SmallVector<DomTreeNodePair, 32>,
867 PQ.push(std::make_pair(Node, DomLevels[Node]));
873 while (!PQ.empty()) {
874 DomTreeNodePair RootPair = PQ.top();
877 unsigned RootLevel = RootPair.second;
887 while (!Worklist.
empty()) {
897 if (SuccNode->
getIDom() == Node)
900 unsigned SuccLevel = DomLevels[SuccNode];
901 if (SuccLevel > RootLevel)
904 if (!Visited.
insert(SuccNode))
908 if (!LiveInBlocks.
count(SuccBB))
911 DFBlocks.
push_back(std::make_pair(BBNumbers[SuccBB], SuccBB));
912 if (!DefBlocks.
count(SuccBB))
913 PQ.push(std::make_pair(SuccNode, SuccLevel));
918 if (!Visited.
count(*CI))
924 if (DFBlocks.
size() > 1)
925 std::sort(DFBlocks.
begin(), DFBlocks.
end());
927 unsigned CurrentVersion = 0;
928 for (
unsigned i = 0, e = DFBlocks.
size(); i != e; ++i)
929 QueuePhiNode(DFBlocks[i].second, AllocaNum, CurrentVersion);
935 bool PromoteMem2Reg::QueuePhiNode(
BasicBlock *BB,
unsigned AllocaNo,
938 PHINode *&PN = NewPhiNodes[std::make_pair(BBNumbers[BB], AllocaNo)];
946 PN =
PHINode::Create(Allocas[AllocaNo]->getAllocatedType(), getNumPreds(BB),
950 PhiToAllocaMap[PN] = AllocaNo;
953 AST->copyValue(PointerAllocaValues[AllocaNo], PN);
964 RenamePassData::ValVector &IncomingVals,
965 std::vector<RenamePassData> &Worklist) {
972 if (PhiToAllocaMap.count(APN)) {
979 unsigned NewPHINumOperands = APN->getNumOperands();
982 assert(NumEdges &&
"Must be at least one edge from Pred to BB!");
987 unsigned AllocaNo = PhiToAllocaMap[APN];
990 for (
unsigned i = 0; i != NumEdges; ++i)
991 APN->addIncoming(IncomingVals[AllocaNo], Pred);
994 IncomingVals[AllocaNo] = APN;
1004 }
while (APN->getNumOperands() == NewPHINumOperands);
1015 if (
LoadInst *LI = dyn_cast<LoadInst>(I)) {
1021 if (AI == AllocaLookup.
end())
1024 Value *V = IncomingVals[AI->second];
1029 AST->deleteValue(LI);
1031 }
else if (
StoreInst *SI = dyn_cast<StoreInst>(I)) {
1039 if (ai == AllocaLookup.
end())
1043 IncomingVals[ai->second] = SI->getOperand(0);
1066 if (VisitedSuccs.
insert(*I))
1067 Worklist.push_back(RenamePassData(*I, Pred, IncomingVals));
1075 if (Allocas.
empty())
1078 PromoteMem2Reg(Allocas, DT, AST).run();
void push_back(const T &Elt)
const_iterator end(StringRef path)
Get end iterator over path.
void addIncoming(Value *V, BasicBlock *BB)
const Instruction & back() const
Function object to check whether the second component of a std::pair compares less than the second co...
DbgDeclareInst * FindAllocaDbgDeclare(Value *V)
enable_if_c<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
const_iterator begin(StringRef path)
Get begin iterator over path.
const Function * getParent() const
Return the enclosing method, or null if none.
const Instruction & front() const
void PromoteMemToReg(ArrayRef< AllocaInst * > Allocas, DominatorTree &DT, AliasSetTracker *AST=0)
Promote the specified list of alloca instructions into scalar registers, inserting PHI nodes as appro...
LoopInfoBase< BlockT, LoopT > * LI
bool isAllocaPromotable(const AllocaInst *AI)
Return true if this alloca is legal for promotion.
void swap(OwningPtr< T > &a, OwningPtr< T > &b)
DomTreeNodeBase< NodeT > * getIDom() const
T LLVM_ATTRIBUTE_UNUSED_RESULT pop_back_val()
ID
LLVM Calling Convention Representation.
Interval::succ_iterator succ_begin(Interval *I)
bool count(PtrType Ptr) const
count - Return true if the specified pointer is in the set.
bool LLVM_ATTRIBUTE_UNUSED_RESULT empty() const
This class represents a no-op cast from one type to another.
void replaceAllUsesWith(Value *V)
Interval::succ_iterator succ_end(Interval *I)
unsigned getNumIncomingValues() const
LLVM Basic Block Representation.
bool onlyUsedByLifetimeMarkers(const Value *V)
Interval::pred_iterator pred_begin(Interval *I)
static PHINode * Create(Type *Ty, unsigned NumReservedValues, const Twine &NameStr="", Instruction *InsertBefore=0)
BasicBlock * getIncomingBlock(unsigned i) const
const InstListType & getInstList() const
Return the underlying instruction list container.
Value * getOperand(unsigned i) const
Interval::pred_iterator pred_end(Interval *I)
Value * getPointerOperand()
bool empty() const
empty - Check if the array is empty.
bool dominates(const DomTreeNode *A, const DomTreeNode *B) const
static UndefValue * get(Type *T)
Value * SimplifyInstruction(Instruction *I, const DataLayout *TD=0, const TargetLibraryInfo *TLI=0, const DominatorTree *DT=0)
LLVMContext & getContext() const
All values hold a context through their type.
static PointerType * getInt8PtrTy(LLVMContext &C, unsigned AS=0)
iterator erase(iterator where)
SmallPtrSetIterator - This implements a const_iterator for SmallPtrSet.
bool ConvertDebugDeclareToDebugValue(DbgDeclareInst *DDI, StoreInst *SI, DIBuilder &Builder)
bool erase(const KeyT &Val)
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
std::string getName(ID id, ArrayRef< Type * > Tys=None)
std::vector< DomTreeNodeBase< NodeT > * >::iterator iterator
LLVM Value Representation.
static const Function * getParent(const Value *V)
ItTy prior(ItTy it, Dist n)
iterator find(const KeyT &Val)
Function object to check whether the first component of a std::pair compares less than the first comp...
const BasicBlock * getParent() const
void deleteValue(Value *PtrVal)
bool isVoidTy() const
isVoidTy - Return true if this is 'void'.