1009b1c42SEd Schouten //===- PromoteMemoryToRegister.cpp - Convert allocas to registers ---------===//
2009b1c42SEd Schouten //
3e6d15924SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e6d15924SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5e6d15924SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6009b1c42SEd Schouten //
7009b1c42SEd Schouten //===----------------------------------------------------------------------===//
8009b1c42SEd Schouten //
9009b1c42SEd Schouten // This file promotes memory references to be register references. It promotes
10009b1c42SEd Schouten // alloca instructions which only have loads and stores as uses. An alloca is
11cf099d11SDimitry Andric // transformed by using iterated dominator frontiers to place PHI nodes, then
12cf099d11SDimitry Andric // traversing the function in depth-first order to rewrite loads and stores as
13cf099d11SDimitry Andric // appropriate.
14cf099d11SDimitry Andric //
15009b1c42SEd Schouten //===----------------------------------------------------------------------===//
16009b1c42SEd Schouten
17f8af5cf6SDimitry Andric #include "llvm/ADT/ArrayRef.h"
184a16efa3SDimitry Andric #include "llvm/ADT/DenseMap.h"
194a16efa3SDimitry Andric #include "llvm/ADT/STLExtras.h"
204a16efa3SDimitry Andric #include "llvm/ADT/SmallPtrSet.h"
214a16efa3SDimitry Andric #include "llvm/ADT/SmallVector.h"
224a16efa3SDimitry Andric #include "llvm/ADT/Statistic.h"
23044eb2f6SDimitry Andric #include "llvm/ADT/Twine.h"
2471d5a254SDimitry Andric #include "llvm/Analysis/AssumptionCache.h"
25cf099d11SDimitry Andric #include "llvm/Analysis/InstructionSimplify.h"
265a5ac124SDimitry Andric #include "llvm/Analysis/IteratedDominanceFrontier.h"
27411bd29eSDimitry Andric #include "llvm/Analysis/ValueTracking.h"
28044eb2f6SDimitry Andric #include "llvm/IR/BasicBlock.h"
295ca98fd9SDimitry Andric #include "llvm/IR/CFG.h"
30044eb2f6SDimitry Andric #include "llvm/IR/Constant.h"
314a16efa3SDimitry Andric #include "llvm/IR/Constants.h"
325ca98fd9SDimitry Andric #include "llvm/IR/DIBuilder.h"
33ecbca9f5SDimitry Andric #include "llvm/IR/DebugInfo.h"
34b1c73532SDimitry Andric #include "llvm/IR/DebugProgramInstruction.h"
355ca98fd9SDimitry Andric #include "llvm/IR/Dominators.h"
364a16efa3SDimitry Andric #include "llvm/IR/Function.h"
37044eb2f6SDimitry Andric #include "llvm/IR/InstrTypes.h"
38044eb2f6SDimitry Andric #include "llvm/IR/Instruction.h"
394a16efa3SDimitry Andric #include "llvm/IR/Instructions.h"
404a16efa3SDimitry Andric #include "llvm/IR/IntrinsicInst.h"
41044eb2f6SDimitry Andric #include "llvm/IR/Intrinsics.h"
42044eb2f6SDimitry Andric #include "llvm/IR/LLVMContext.h"
435a5ac124SDimitry Andric #include "llvm/IR/Module.h"
44ac9a064cSDimitry Andric #include "llvm/IR/Operator.h"
45044eb2f6SDimitry Andric #include "llvm/IR/Type.h"
46044eb2f6SDimitry Andric #include "llvm/IR/User.h"
47044eb2f6SDimitry Andric #include "llvm/Support/Casting.h"
48ecbca9f5SDimitry Andric #include "llvm/Transforms/Utils/Local.h"
4971d5a254SDimitry Andric #include "llvm/Transforms/Utils/PromoteMemToReg.h"
50009b1c42SEd Schouten #include <algorithm>
51044eb2f6SDimitry Andric #include <cassert>
52044eb2f6SDimitry Andric #include <iterator>
53044eb2f6SDimitry Andric #include <utility>
54044eb2f6SDimitry Andric #include <vector>
55044eb2f6SDimitry Andric
56009b1c42SEd Schouten using namespace llvm;
57009b1c42SEd Schouten
585ca98fd9SDimitry Andric #define DEBUG_TYPE "mem2reg"
595ca98fd9SDimitry Andric
60009b1c42SEd Schouten STATISTIC(NumLocalPromoted, "Number of alloca's promoted within one block");
61009b1c42SEd Schouten STATISTIC(NumSingleStore, "Number of alloca's promoted with a single store");
62009b1c42SEd Schouten STATISTIC(NumDeadAlloca, "Number of dead alloca's removed");
63009b1c42SEd Schouten STATISTIC(NumPHIInsert, "Number of PHI nodes inserted");
64009b1c42SEd Schouten
isAllocaPromotable(const AllocaInst * AI)65009b1c42SEd Schouten bool llvm::isAllocaPromotable(const AllocaInst *AI) {
66009b1c42SEd Schouten // Only allow direct and non-volatile loads and stores...
675ca98fd9SDimitry Andric for (const User *U : AI->users()) {
6866e41e3cSRoman Divacky if (const LoadInst *LI = dyn_cast<LoadInst>(U)) {
6930815c53SDimitry Andric // Note that atomic loads can be transformed; atomic semantics do
7030815c53SDimitry Andric // not have any meaning for a local alloca.
71145449b1SDimitry Andric if (LI->isVolatile() || LI->getType() != AI->getAllocatedType())
72009b1c42SEd Schouten return false;
7366e41e3cSRoman Divacky } else if (const StoreInst *SI = dyn_cast<StoreInst>(U)) {
74c0981da4SDimitry Andric if (SI->getValueOperand() == AI ||
75c0981da4SDimitry Andric SI->getValueOperand()->getType() != AI->getAllocatedType())
76009b1c42SEd Schouten return false; // Don't allow a store OF the AI, only INTO the AI.
7730815c53SDimitry Andric // Note that atomic stores can be transformed; atomic semantics do
7830815c53SDimitry Andric // not have any meaning for a local alloca.
79009b1c42SEd Schouten if (SI->isVolatile())
80009b1c42SEd Schouten return false;
81411bd29eSDimitry Andric } else if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(U)) {
82b60736ecSDimitry Andric if (!II->isLifetimeStartOrEnd() && !II->isDroppable())
83411bd29eSDimitry Andric return false;
84411bd29eSDimitry Andric } else if (const BitCastInst *BCI = dyn_cast<BitCastInst>(U)) {
85b60736ecSDimitry Andric if (!onlyUsedByLifetimeMarkersOrDroppableInsts(BCI))
86411bd29eSDimitry Andric return false;
87411bd29eSDimitry Andric } else if (const GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(U)) {
88411bd29eSDimitry Andric if (!GEPI->hasAllZeroIndices())
89411bd29eSDimitry Andric return false;
90b60736ecSDimitry Andric if (!onlyUsedByLifetimeMarkersOrDroppableInsts(GEPI))
91b60736ecSDimitry Andric return false;
92b60736ecSDimitry Andric } else if (const AddrSpaceCastInst *ASCI = dyn_cast<AddrSpaceCastInst>(U)) {
93b60736ecSDimitry Andric if (!onlyUsedByLifetimeMarkers(ASCI))
94411bd29eSDimitry Andric return false;
95009b1c42SEd Schouten } else {
96009b1c42SEd Schouten return false;
97009b1c42SEd Schouten }
9866e41e3cSRoman Divacky }
99009b1c42SEd Schouten
100009b1c42SEd Schouten return true;
101009b1c42SEd Schouten }
102009b1c42SEd Schouten
103009b1c42SEd Schouten namespace {
104009b1c42SEd Schouten
createDebugValue(DIBuilder & DIB,Value * NewValue,DILocalVariable * Variable,DIExpression * Expression,const DILocation * DI,DbgVariableRecord * InsertBefore)105ac9a064cSDimitry Andric static void createDebugValue(DIBuilder &DIB, Value *NewValue,
1064df029ccSDimitry Andric DILocalVariable *Variable,
1074df029ccSDimitry Andric DIExpression *Expression, const DILocation *DI,
108ac9a064cSDimitry Andric DbgVariableRecord *InsertBefore) {
109ac9a064cSDimitry Andric // FIXME: Merge these two functions now that DIBuilder supports
110ac9a064cSDimitry Andric // DbgVariableRecords. We neeed the API to accept DbgVariableRecords as an
111ac9a064cSDimitry Andric // insert point for that to work.
1124df029ccSDimitry Andric (void)DIB;
113ac9a064cSDimitry Andric DbgVariableRecord::createDbgVariableRecord(NewValue, Variable, Expression, DI,
1144df029ccSDimitry Andric *InsertBefore);
1154df029ccSDimitry Andric }
createDebugValue(DIBuilder & DIB,Value * NewValue,DILocalVariable * Variable,DIExpression * Expression,const DILocation * DI,Instruction * InsertBefore)116ac9a064cSDimitry Andric static void createDebugValue(DIBuilder &DIB, Value *NewValue,
1174df029ccSDimitry Andric DILocalVariable *Variable,
118ac9a064cSDimitry Andric DIExpression *Expression, const DILocation *DI,
1194df029ccSDimitry Andric Instruction *InsertBefore) {
120ac9a064cSDimitry Andric DIB.insertDbgValueIntrinsic(NewValue, Variable, Expression, DI, InsertBefore);
1214df029ccSDimitry Andric }
1224df029ccSDimitry Andric
123e3b55780SDimitry Andric /// Helper for updating assignment tracking debug info when promoting allocas.
124e3b55780SDimitry Andric class AssignmentTrackingInfo {
125e3b55780SDimitry Andric /// DbgAssignIntrinsics linked to the alloca with at most one per variable
126e3b55780SDimitry Andric /// fragment. (i.e. not be a comprehensive set if there are multiple
127e3b55780SDimitry Andric /// dbg.assigns for one variable fragment).
128e3b55780SDimitry Andric SmallVector<DbgVariableIntrinsic *> DbgAssigns;
129ac9a064cSDimitry Andric SmallVector<DbgVariableRecord *> DVRAssigns;
130e3b55780SDimitry Andric
131e3b55780SDimitry Andric public:
init(AllocaInst * AI)132e3b55780SDimitry Andric void init(AllocaInst *AI) {
133e3b55780SDimitry Andric SmallSet<DebugVariable, 2> Vars;
134e3b55780SDimitry Andric for (DbgAssignIntrinsic *DAI : at::getAssignmentMarkers(AI)) {
135e3b55780SDimitry Andric if (Vars.insert(DebugVariable(DAI)).second)
136e3b55780SDimitry Andric DbgAssigns.push_back(DAI);
137e3b55780SDimitry Andric }
138ac9a064cSDimitry Andric for (DbgVariableRecord *DVR : at::getDVRAssignmentMarkers(AI)) {
139ac9a064cSDimitry Andric if (Vars.insert(DebugVariable(DVR)).second)
140ac9a064cSDimitry Andric DVRAssigns.push_back(DVR);
1414df029ccSDimitry Andric }
142e3b55780SDimitry Andric }
143e3b55780SDimitry Andric
144e3b55780SDimitry Andric /// Update assignment tracking debug info given for the to-be-deleted store
145e3b55780SDimitry Andric /// \p ToDelete that stores to this alloca.
updateForDeletedStore(StoreInst * ToDelete,DIBuilder & DIB,SmallSet<DbgAssignIntrinsic *,8> * DbgAssignsToDelete,SmallSet<DbgVariableRecord *,8> * DVRAssignsToDelete) const146ac9a064cSDimitry Andric void updateForDeletedStore(
147ac9a064cSDimitry Andric StoreInst *ToDelete, DIBuilder &DIB,
1484df029ccSDimitry Andric SmallSet<DbgAssignIntrinsic *, 8> *DbgAssignsToDelete,
149ac9a064cSDimitry Andric SmallSet<DbgVariableRecord *, 8> *DVRAssignsToDelete) const {
150e3b55780SDimitry Andric // There's nothing to do if the alloca doesn't have any variables using
151e3b55780SDimitry Andric // assignment tracking.
152ac9a064cSDimitry Andric if (DbgAssigns.empty() && DVRAssigns.empty())
153e3b55780SDimitry Andric return;
154e3b55780SDimitry Andric
1557fa27ce4SDimitry Andric // Insert a dbg.value where the linked dbg.assign is and remember to delete
1567fa27ce4SDimitry Andric // the dbg.assign later. Demoting to dbg.value isn't necessary for
1577fa27ce4SDimitry Andric // correctness but does reduce compile time and memory usage by reducing
1587fa27ce4SDimitry Andric // unnecessary function-local metadata. Remember that we've seen a
1597fa27ce4SDimitry Andric // dbg.assign for each variable fragment for the untracked store handling
1607fa27ce4SDimitry Andric // (after this loop).
1617fa27ce4SDimitry Andric SmallSet<DebugVariableAggregate, 2> VarHasDbgAssignForStore;
1624df029ccSDimitry Andric auto InsertValueForAssign = [&](auto *DbgAssign, auto *&AssignList) {
1634df029ccSDimitry Andric VarHasDbgAssignForStore.insert(DebugVariableAggregate(DbgAssign));
1644df029ccSDimitry Andric AssignList->insert(DbgAssign);
1654df029ccSDimitry Andric createDebugValue(DIB, DbgAssign->getValue(), DbgAssign->getVariable(),
1664df029ccSDimitry Andric DbgAssign->getExpression(), DbgAssign->getDebugLoc(),
1674df029ccSDimitry Andric DbgAssign);
1684df029ccSDimitry Andric };
1694df029ccSDimitry Andric for (auto *Assign : at::getAssignmentMarkers(ToDelete))
1704df029ccSDimitry Andric InsertValueForAssign(Assign, DbgAssignsToDelete);
171ac9a064cSDimitry Andric for (auto *Assign : at::getDVRAssignmentMarkers(ToDelete))
172ac9a064cSDimitry Andric InsertValueForAssign(Assign, DVRAssignsToDelete);
173e3b55780SDimitry Andric
174e3b55780SDimitry Andric // It's possible for variables using assignment tracking to have no
175e3b55780SDimitry Andric // dbg.assign linked to this store. These are variables in DbgAssigns that
176e3b55780SDimitry Andric // are missing from VarHasDbgAssignForStore. Since there isn't a dbg.assign
177e3b55780SDimitry Andric // to mark the assignment - and the store is going to be deleted - insert a
178e3b55780SDimitry Andric // dbg.value to do that now. An untracked store may be either one that
179e3b55780SDimitry Andric // cannot be represented using assignment tracking (non-const offset or
180e3b55780SDimitry Andric // size) or one that is trackable but has had its DIAssignID attachment
181e3b55780SDimitry Andric // dropped accidentally.
1824df029ccSDimitry Andric auto ConvertUnlinkedAssignToValue = [&](auto *Assign) {
1834df029ccSDimitry Andric if (VarHasDbgAssignForStore.contains(DebugVariableAggregate(Assign)))
1844df029ccSDimitry Andric return;
1854df029ccSDimitry Andric ConvertDebugDeclareToDebugValue(Assign, ToDelete, DIB);
1864df029ccSDimitry Andric };
1874df029ccSDimitry Andric for_each(DbgAssigns, ConvertUnlinkedAssignToValue);
188ac9a064cSDimitry Andric for_each(DVRAssigns, ConvertUnlinkedAssignToValue);
189e3b55780SDimitry Andric }
190e3b55780SDimitry Andric
191e3b55780SDimitry Andric /// Update assignment tracking debug info given for the newly inserted PHI \p
192e3b55780SDimitry Andric /// NewPhi.
updateForNewPhi(PHINode * NewPhi,DIBuilder & DIB) const193e3b55780SDimitry Andric void updateForNewPhi(PHINode *NewPhi, DIBuilder &DIB) const {
194e3b55780SDimitry Andric // Regardless of the position of dbg.assigns relative to stores, the
195e3b55780SDimitry Andric // incoming values into a new PHI should be the same for the (imaginary)
196e3b55780SDimitry Andric // debug-phi.
197e3b55780SDimitry Andric for (auto *DAI : DbgAssigns)
198e3b55780SDimitry Andric ConvertDebugDeclareToDebugValue(DAI, NewPhi, DIB);
199ac9a064cSDimitry Andric for (auto *DVR : DVRAssigns)
200ac9a064cSDimitry Andric ConvertDebugDeclareToDebugValue(DVR, NewPhi, DIB);
201e3b55780SDimitry Andric }
202e3b55780SDimitry Andric
clear()2034df029ccSDimitry Andric void clear() {
2044df029ccSDimitry Andric DbgAssigns.clear();
205ac9a064cSDimitry Andric DVRAssigns.clear();
2064df029ccSDimitry Andric }
empty()207ac9a064cSDimitry Andric bool empty() { return DbgAssigns.empty() && DVRAssigns.empty(); }
208e3b55780SDimitry Andric };
209e3b55780SDimitry Andric
210009b1c42SEd Schouten struct AllocaInfo {
211b60736ecSDimitry Andric using DbgUserVec = SmallVector<DbgVariableIntrinsic *, 1>;
212ac9a064cSDimitry Andric using DPUserVec = SmallVector<DbgVariableRecord *, 1>;
213b60736ecSDimitry Andric
214cf099d11SDimitry Andric SmallVector<BasicBlock *, 32> DefiningBlocks;
215cf099d11SDimitry Andric SmallVector<BasicBlock *, 32> UsingBlocks;
216009b1c42SEd Schouten
217009b1c42SEd Schouten StoreInst *OnlyStore;
218009b1c42SEd Schouten BasicBlock *OnlyBlock;
219009b1c42SEd Schouten bool OnlyUsedInOneBlock;
220009b1c42SEd Schouten
221e3b55780SDimitry Andric /// Debug users of the alloca - does not include dbg.assign intrinsics.
222b60736ecSDimitry Andric DbgUserVec DbgUsers;
223b1c73532SDimitry Andric DPUserVec DPUsers;
224e3b55780SDimitry Andric /// Helper to update assignment tracking debug info.
225e3b55780SDimitry Andric AssignmentTrackingInfo AssignmentTracking;
226009b1c42SEd Schouten
clear__anona697314a0111::AllocaInfo227009b1c42SEd Schouten void clear() {
228009b1c42SEd Schouten DefiningBlocks.clear();
229009b1c42SEd Schouten UsingBlocks.clear();
2305ca98fd9SDimitry Andric OnlyStore = nullptr;
2315ca98fd9SDimitry Andric OnlyBlock = nullptr;
232009b1c42SEd Schouten OnlyUsedInOneBlock = true;
233b60736ecSDimitry Andric DbgUsers.clear();
234b1c73532SDimitry Andric DPUsers.clear();
235e3b55780SDimitry Andric AssignmentTracking.clear();
236009b1c42SEd Schouten }
237009b1c42SEd Schouten
238f8af5cf6SDimitry Andric /// Scan the uses of the specified alloca, filling in the AllocaInfo used
239f8af5cf6SDimitry Andric /// by the rest of the pass to reason about the uses of this alloca.
AnalyzeAlloca__anona697314a0111::AllocaInfo240009b1c42SEd Schouten void AnalyzeAlloca(AllocaInst *AI) {
241009b1c42SEd Schouten clear();
242009b1c42SEd Schouten
243009b1c42SEd Schouten // As we scan the uses of the alloca instruction, keep track of stores,
244009b1c42SEd Schouten // and decide whether all of the loads and stores to the alloca are within
245009b1c42SEd Schouten // the same basic block.
246b60736ecSDimitry Andric for (User *U : AI->users()) {
247b60736ecSDimitry Andric Instruction *User = cast<Instruction>(U);
24859850d08SRoman Divacky
24959850d08SRoman Divacky if (StoreInst *SI = dyn_cast<StoreInst>(User)) {
250009b1c42SEd Schouten // Remember the basic blocks which define new values for the alloca
251009b1c42SEd Schouten DefiningBlocks.push_back(SI->getParent());
252009b1c42SEd Schouten OnlyStore = SI;
253009b1c42SEd Schouten } else {
254009b1c42SEd Schouten LoadInst *LI = cast<LoadInst>(User);
255009b1c42SEd Schouten // Otherwise it must be a load instruction, keep track of variable
256009b1c42SEd Schouten // reads.
257009b1c42SEd Schouten UsingBlocks.push_back(LI->getParent());
258009b1c42SEd Schouten }
259009b1c42SEd Schouten
260009b1c42SEd Schouten if (OnlyUsedInOneBlock) {
2615ca98fd9SDimitry Andric if (!OnlyBlock)
262009b1c42SEd Schouten OnlyBlock = User->getParent();
263009b1c42SEd Schouten else if (OnlyBlock != User->getParent())
264009b1c42SEd Schouten OnlyUsedInOneBlock = false;
265009b1c42SEd Schouten }
266009b1c42SEd Schouten }
267e3b55780SDimitry Andric DbgUserVec AllDbgUsers;
268ac9a064cSDimitry Andric SmallVector<DbgVariableRecord *> AllDPUsers;
2694df029ccSDimitry Andric findDbgUsers(AllDbgUsers, AI, &AllDPUsers);
270e3b55780SDimitry Andric std::copy_if(AllDbgUsers.begin(), AllDbgUsers.end(),
271e3b55780SDimitry Andric std::back_inserter(DbgUsers), [](DbgVariableIntrinsic *DII) {
272e3b55780SDimitry Andric return !isa<DbgAssignIntrinsic>(DII);
273e3b55780SDimitry Andric });
2744df029ccSDimitry Andric std::copy_if(AllDPUsers.begin(), AllDPUsers.end(),
2754df029ccSDimitry Andric std::back_inserter(DPUsers),
276ac9a064cSDimitry Andric [](DbgVariableRecord *DVR) { return !DVR->isDbgAssign(); });
277e3b55780SDimitry Andric AssignmentTracking.init(AI);
278009b1c42SEd Schouten }
279009b1c42SEd Schouten };
280cf099d11SDimitry Andric
281eb11fae6SDimitry Andric /// Data package used by RenamePass().
282eb11fae6SDimitry Andric struct RenamePassData {
283044eb2f6SDimitry Andric using ValVector = std::vector<Value *>;
284eb11fae6SDimitry Andric using LocationVector = std::vector<DebugLoc>;
285cf099d11SDimitry Andric
RenamePassData__anona697314a0111::RenamePassData286eb11fae6SDimitry Andric RenamePassData(BasicBlock *B, BasicBlock *P, ValVector V, LocationVector L)
287eb11fae6SDimitry Andric : BB(B), Pred(P), Values(std::move(V)), Locations(std::move(L)) {}
288044eb2f6SDimitry Andric
289f8af5cf6SDimitry Andric BasicBlock *BB;
290f8af5cf6SDimitry Andric BasicBlock *Pred;
291f8af5cf6SDimitry Andric ValVector Values;
292eb11fae6SDimitry Andric LocationVector Locations;
293cf099d11SDimitry Andric };
294f8af5cf6SDimitry Andric
295eb11fae6SDimitry Andric /// This assigns and keeps a per-bb relative ordering of load/store
296f8af5cf6SDimitry Andric /// instructions in the block that directly load or store an alloca.
297f8af5cf6SDimitry Andric ///
298f8af5cf6SDimitry Andric /// This functionality is important because it avoids scanning large basic
299f8af5cf6SDimitry Andric /// blocks multiple times when promoting many allocas in the same block.
300f8af5cf6SDimitry Andric class LargeBlockInfo {
301eb11fae6SDimitry Andric /// For each instruction that we track, keep the index of the
302f8af5cf6SDimitry Andric /// instruction.
303f8af5cf6SDimitry Andric ///
304f8af5cf6SDimitry Andric /// The index starts out as the number of the instruction from the start of
305f8af5cf6SDimitry Andric /// the block.
306f8af5cf6SDimitry Andric DenseMap<const Instruction *, unsigned> InstNumbers;
307f8af5cf6SDimitry Andric
308f8af5cf6SDimitry Andric public:
309f8af5cf6SDimitry Andric
310f8af5cf6SDimitry Andric /// This code only looks at accesses to allocas.
isInterestingInstruction(const Instruction * I)311f8af5cf6SDimitry Andric static bool isInterestingInstruction(const Instruction *I) {
312f8af5cf6SDimitry Andric return (isa<LoadInst>(I) && isa<AllocaInst>(I->getOperand(0))) ||
313f8af5cf6SDimitry Andric (isa<StoreInst>(I) && isa<AllocaInst>(I->getOperand(1)));
314f8af5cf6SDimitry Andric }
315f8af5cf6SDimitry Andric
316f8af5cf6SDimitry Andric /// Get or calculate the index of the specified instruction.
getInstructionIndex(const Instruction * I)317f8af5cf6SDimitry Andric unsigned getInstructionIndex(const Instruction *I) {
318f8af5cf6SDimitry Andric assert(isInterestingInstruction(I) &&
319f8af5cf6SDimitry Andric "Not a load/store to/from an alloca?");
320f8af5cf6SDimitry Andric
321f8af5cf6SDimitry Andric // If we already have this instruction number, return it.
322f8af5cf6SDimitry Andric DenseMap<const Instruction *, unsigned>::iterator It = InstNumbers.find(I);
323f8af5cf6SDimitry Andric if (It != InstNumbers.end())
324f8af5cf6SDimitry Andric return It->second;
325f8af5cf6SDimitry Andric
326f8af5cf6SDimitry Andric // Scan the whole block to get the instruction. This accumulates
327f8af5cf6SDimitry Andric // information for every interesting instruction in the block, in order to
328f8af5cf6SDimitry Andric // avoid gratuitus rescans.
329f8af5cf6SDimitry Andric const BasicBlock *BB = I->getParent();
330f8af5cf6SDimitry Andric unsigned InstNo = 0;
331dd58ef01SDimitry Andric for (const Instruction &BBI : *BB)
332dd58ef01SDimitry Andric if (isInterestingInstruction(&BBI))
333dd58ef01SDimitry Andric InstNumbers[&BBI] = InstNo++;
334f8af5cf6SDimitry Andric It = InstNumbers.find(I);
335f8af5cf6SDimitry Andric
336f8af5cf6SDimitry Andric assert(It != InstNumbers.end() && "Didn't insert instruction?");
337f8af5cf6SDimitry Andric return It->second;
338f8af5cf6SDimitry Andric }
339f8af5cf6SDimitry Andric
deleteValue(const Instruction * I)340f8af5cf6SDimitry Andric void deleteValue(const Instruction *I) { InstNumbers.erase(I); }
341f8af5cf6SDimitry Andric
clear()342f8af5cf6SDimitry Andric void clear() { InstNumbers.clear(); }
343f8af5cf6SDimitry Andric };
344f8af5cf6SDimitry Andric
345f8af5cf6SDimitry Andric struct PromoteMem2Reg {
346f8af5cf6SDimitry Andric /// The alloca instructions being promoted.
347f8af5cf6SDimitry Andric std::vector<AllocaInst *> Allocas;
348044eb2f6SDimitry Andric
349f8af5cf6SDimitry Andric DominatorTree &DT;
350f8af5cf6SDimitry Andric DIBuilder DIB;
351044eb2f6SDimitry Andric
35267c32a98SDimitry Andric /// A cache of @llvm.assume intrinsics used by SimplifyInstruction.
35367c32a98SDimitry Andric AssumptionCache *AC;
35467c32a98SDimitry Andric
355a303c417SDimitry Andric const SimplifyQuery SQ;
356044eb2f6SDimitry Andric
357f8af5cf6SDimitry Andric /// Reverse mapping of Allocas.
358f8af5cf6SDimitry Andric DenseMap<AllocaInst *, unsigned> AllocaLookup;
359f8af5cf6SDimitry Andric
360eb11fae6SDimitry Andric /// The PhiNodes we're adding.
361f8af5cf6SDimitry Andric ///
362f8af5cf6SDimitry Andric /// That map is used to simplify some Phi nodes as we iterate over it, so
363f8af5cf6SDimitry Andric /// it should have deterministic iterators. We could use a MapVector, but
364f8af5cf6SDimitry Andric /// since we already maintain a map from BasicBlock* to a stable numbering
365f8af5cf6SDimitry Andric /// (BBNumbers), the DenseMap is more efficient (also supports removal).
366f8af5cf6SDimitry Andric DenseMap<std::pair<unsigned, unsigned>, PHINode *> NewPhiNodes;
367f8af5cf6SDimitry Andric
368f8af5cf6SDimitry Andric /// For each PHI node, keep track of which entry in Allocas it corresponds
369f8af5cf6SDimitry Andric /// to.
370f8af5cf6SDimitry Andric DenseMap<PHINode *, unsigned> PhiToAllocaMap;
371f8af5cf6SDimitry Andric
372f8af5cf6SDimitry Andric /// For each alloca, we keep track of the dbg.declare intrinsic that
373f8af5cf6SDimitry Andric /// describes it, if any, so that we can convert it to a dbg.value
374f8af5cf6SDimitry Andric /// intrinsic if the alloca gets promoted.
375b60736ecSDimitry Andric SmallVector<AllocaInfo::DbgUserVec, 8> AllocaDbgUsers;
376b1c73532SDimitry Andric SmallVector<AllocaInfo::DPUserVec, 8> AllocaDPUsers;
377f8af5cf6SDimitry Andric
378e3b55780SDimitry Andric /// For each alloca, keep an instance of a helper class that gives us an easy
379e3b55780SDimitry Andric /// way to update assignment tracking debug info if the alloca is promoted.
380e3b55780SDimitry Andric SmallVector<AssignmentTrackingInfo, 8> AllocaATInfo;
3817fa27ce4SDimitry Andric /// A set of dbg.assigns to delete because they've been demoted to
3827fa27ce4SDimitry Andric /// dbg.values. Call cleanUpDbgAssigns to delete them.
3837fa27ce4SDimitry Andric SmallSet<DbgAssignIntrinsic *, 8> DbgAssignsToDelete;
384ac9a064cSDimitry Andric SmallSet<DbgVariableRecord *, 8> DVRAssignsToDelete;
385e3b55780SDimitry Andric
386f8af5cf6SDimitry Andric /// The set of basic blocks the renamer has already visited.
387f8af5cf6SDimitry Andric SmallPtrSet<BasicBlock *, 16> Visited;
388f8af5cf6SDimitry Andric
389f8af5cf6SDimitry Andric /// Contains a stable numbering of basic blocks to avoid non-determinstic
390f8af5cf6SDimitry Andric /// behavior.
391f8af5cf6SDimitry Andric DenseMap<BasicBlock *, unsigned> BBNumbers;
392f8af5cf6SDimitry Andric
393f8af5cf6SDimitry Andric /// Lazily compute the number of predecessors a block has.
394f8af5cf6SDimitry Andric DenseMap<const BasicBlock *, unsigned> BBNumPreds;
395f8af5cf6SDimitry Andric
396ac9a064cSDimitry Andric /// Whether the function has the no-signed-zeros-fp-math attribute set.
397ac9a064cSDimitry Andric bool NoSignedZeros = false;
398ac9a064cSDimitry Andric
399f8af5cf6SDimitry Andric public:
PromoteMem2Reg__anona697314a0111::PromoteMem2Reg400f8af5cf6SDimitry Andric PromoteMem2Reg(ArrayRef<AllocaInst *> Allocas, DominatorTree &DT,
40171d5a254SDimitry Andric AssumptionCache *AC)
402f8af5cf6SDimitry Andric : Allocas(Allocas.begin(), Allocas.end()), DT(DT),
40367c32a98SDimitry Andric DIB(*DT.getRoot()->getParent()->getParent(), /*AllowUnresolved*/ false),
404ac9a064cSDimitry Andric AC(AC), SQ(DT.getRoot()->getDataLayout(),
405a303c417SDimitry Andric nullptr, &DT, AC) {}
406f8af5cf6SDimitry Andric
407f8af5cf6SDimitry Andric void run();
408f8af5cf6SDimitry Andric
409f8af5cf6SDimitry Andric private:
RemoveFromAllocasList__anona697314a0111::PromoteMem2Reg410f8af5cf6SDimitry Andric void RemoveFromAllocasList(unsigned &AllocaIdx) {
411f8af5cf6SDimitry Andric Allocas[AllocaIdx] = Allocas.back();
412f8af5cf6SDimitry Andric Allocas.pop_back();
413f8af5cf6SDimitry Andric --AllocaIdx;
414f8af5cf6SDimitry Andric }
415f8af5cf6SDimitry Andric
getNumPreds__anona697314a0111::PromoteMem2Reg416f8af5cf6SDimitry Andric unsigned getNumPreds(const BasicBlock *BB) {
417f8af5cf6SDimitry Andric unsigned &NP = BBNumPreds[BB];
418f8af5cf6SDimitry Andric if (NP == 0)
419eb11fae6SDimitry Andric NP = pred_size(BB) + 1;
420f8af5cf6SDimitry Andric return NP - 1;
421f8af5cf6SDimitry Andric }
422f8af5cf6SDimitry Andric
423f8af5cf6SDimitry Andric void ComputeLiveInBlocks(AllocaInst *AI, AllocaInfo &Info,
42467c32a98SDimitry Andric const SmallPtrSetImpl<BasicBlock *> &DefBlocks,
42567c32a98SDimitry Andric SmallPtrSetImpl<BasicBlock *> &LiveInBlocks);
426f8af5cf6SDimitry Andric void RenamePass(BasicBlock *BB, BasicBlock *Pred,
427f8af5cf6SDimitry Andric RenamePassData::ValVector &IncVals,
428eb11fae6SDimitry Andric RenamePassData::LocationVector &IncLocs,
429f8af5cf6SDimitry Andric std::vector<RenamePassData> &Worklist);
430f8af5cf6SDimitry Andric bool QueuePhiNode(BasicBlock *BB, unsigned AllocaIdx, unsigned &Version);
4317fa27ce4SDimitry Andric
4327fa27ce4SDimitry Andric /// Delete dbg.assigns that have been demoted to dbg.values.
cleanUpDbgAssigns__anona697314a0111::PromoteMem2Reg4337fa27ce4SDimitry Andric void cleanUpDbgAssigns() {
4347fa27ce4SDimitry Andric for (auto *DAI : DbgAssignsToDelete)
4357fa27ce4SDimitry Andric DAI->eraseFromParent();
4367fa27ce4SDimitry Andric DbgAssignsToDelete.clear();
437ac9a064cSDimitry Andric for (auto *DVR : DVRAssignsToDelete)
438ac9a064cSDimitry Andric DVR->eraseFromParent();
439ac9a064cSDimitry Andric DVRAssignsToDelete.clear();
4407fa27ce4SDimitry Andric }
441f8af5cf6SDimitry Andric };
442f8af5cf6SDimitry Andric
443044eb2f6SDimitry Andric } // end anonymous namespace
444009b1c42SEd Schouten
44571d5a254SDimitry Andric /// Given a LoadInst LI this adds assume(LI != null) after it.
addAssumeNonNull(AssumptionCache * AC,LoadInst * LI)44671d5a254SDimitry Andric static void addAssumeNonNull(AssumptionCache *AC, LoadInst *LI) {
44771d5a254SDimitry Andric Function *AssumeIntrinsic =
44871d5a254SDimitry Andric Intrinsic::getDeclaration(LI->getModule(), Intrinsic::assume);
44971d5a254SDimitry Andric ICmpInst *LoadNotNull = new ICmpInst(ICmpInst::ICMP_NE, LI,
45071d5a254SDimitry Andric Constant::getNullValue(LI->getType()));
45171d5a254SDimitry Andric LoadNotNull->insertAfter(LI);
45271d5a254SDimitry Andric CallInst *CI = CallInst::Create(AssumeIntrinsic, {LoadNotNull});
45371d5a254SDimitry Andric CI->insertAfter(LoadNotNull);
454344a3780SDimitry Andric AC->registerAssumption(cast<AssumeInst>(CI));
45571d5a254SDimitry Andric }
45671d5a254SDimitry Andric
convertMetadataToAssumes(LoadInst * LI,Value * Val,const DataLayout & DL,AssumptionCache * AC,const DominatorTree * DT)457e3b55780SDimitry Andric static void convertMetadataToAssumes(LoadInst *LI, Value *Val,
458e3b55780SDimitry Andric const DataLayout &DL, AssumptionCache *AC,
459e3b55780SDimitry Andric const DominatorTree *DT) {
460ac9a064cSDimitry Andric if (isa<UndefValue>(Val) && LI->hasMetadata(LLVMContext::MD_noundef)) {
461ac9a064cSDimitry Andric // Insert non-terminator unreachable.
462ac9a064cSDimitry Andric LLVMContext &Ctx = LI->getContext();
463ac9a064cSDimitry Andric new StoreInst(ConstantInt::getTrue(Ctx),
464ac9a064cSDimitry Andric PoisonValue::get(PointerType::getUnqual(Ctx)),
465ac9a064cSDimitry Andric /*isVolatile=*/false, Align(1), LI);
466ac9a064cSDimitry Andric return;
467ac9a064cSDimitry Andric }
468ac9a064cSDimitry Andric
469e3b55780SDimitry Andric // If the load was marked as nonnull we don't want to lose that information
470e3b55780SDimitry Andric // when we erase this Load. So we preserve it with an assume. As !nonnull
471e3b55780SDimitry Andric // returns poison while assume violations are immediate undefined behavior,
472e3b55780SDimitry Andric // we can only do this if the value is known non-poison.
473e3b55780SDimitry Andric if (AC && LI->getMetadata(LLVMContext::MD_nonnull) &&
474e3b55780SDimitry Andric LI->getMetadata(LLVMContext::MD_noundef) &&
475ac9a064cSDimitry Andric !isKnownNonZero(Val, SimplifyQuery(DL, DT, AC, LI)))
476e3b55780SDimitry Andric addAssumeNonNull(AC, LI);
477e3b55780SDimitry Andric }
478e3b55780SDimitry Andric
removeIntrinsicUsers(AllocaInst * AI)479b60736ecSDimitry Andric static void removeIntrinsicUsers(AllocaInst *AI) {
480411bd29eSDimitry Andric // Knowing that this alloca is promotable, we know that it's safe to kill all
481411bd29eSDimitry Andric // instructions except for load and store.
482411bd29eSDimitry Andric
483344a3780SDimitry Andric for (Use &U : llvm::make_early_inc_range(AI->uses())) {
484344a3780SDimitry Andric Instruction *I = cast<Instruction>(U.getUser());
485411bd29eSDimitry Andric if (isa<LoadInst>(I) || isa<StoreInst>(I))
486411bd29eSDimitry Andric continue;
487411bd29eSDimitry Andric
488b60736ecSDimitry Andric // Drop the use of AI in droppable instructions.
489b60736ecSDimitry Andric if (I->isDroppable()) {
490b60736ecSDimitry Andric I->dropDroppableUse(U);
491b60736ecSDimitry Andric continue;
492b60736ecSDimitry Andric }
493b60736ecSDimitry Andric
494411bd29eSDimitry Andric if (!I->getType()->isVoidTy()) {
495411bd29eSDimitry Andric // The only users of this bitcast/GEP instruction are lifetime intrinsics.
496411bd29eSDimitry Andric // Follow the use/def chain to erase them now instead of leaving it for
497411bd29eSDimitry Andric // dead code elimination later.
498344a3780SDimitry Andric for (Use &UU : llvm::make_early_inc_range(I->uses())) {
499344a3780SDimitry Andric Instruction *Inst = cast<Instruction>(UU.getUser());
500b60736ecSDimitry Andric
501b60736ecSDimitry Andric // Drop the use of I in droppable instructions.
502b60736ecSDimitry Andric if (Inst->isDroppable()) {
503b60736ecSDimitry Andric Inst->dropDroppableUse(UU);
504b60736ecSDimitry Andric continue;
505b60736ecSDimitry Andric }
506411bd29eSDimitry Andric Inst->eraseFromParent();
507411bd29eSDimitry Andric }
508411bd29eSDimitry Andric }
509411bd29eSDimitry Andric I->eraseFromParent();
510411bd29eSDimitry Andric }
511411bd29eSDimitry Andric }
512009b1c42SEd Schouten
513eb11fae6SDimitry Andric /// Rewrite as many loads as possible given a single store.
514f8af5cf6SDimitry Andric ///
515f8af5cf6SDimitry Andric /// When there is only a single store, we can use the domtree to trivially
516f8af5cf6SDimitry Andric /// replace all of the dominated loads with the stored value. Do so, and return
517f8af5cf6SDimitry Andric /// true if this has successfully promoted the alloca entirely. If this returns
518f8af5cf6SDimitry Andric /// false there were some loads which were not dominated by the single store
519f8af5cf6SDimitry Andric /// and thus must be phi-ed with undef. We fall back to the standard alloca
520f8af5cf6SDimitry Andric /// promotion algorithm in that case.
5214df029ccSDimitry Andric static bool
rewriteSingleStoreAlloca(AllocaInst * AI,AllocaInfo & Info,LargeBlockInfo & LBI,const DataLayout & DL,DominatorTree & DT,AssumptionCache * AC,SmallSet<DbgAssignIntrinsic *,8> * DbgAssignsToDelete,SmallSet<DbgVariableRecord *,8> * DVRAssignsToDelete)5224df029ccSDimitry Andric rewriteSingleStoreAlloca(AllocaInst *AI, AllocaInfo &Info, LargeBlockInfo &LBI,
5234df029ccSDimitry Andric const DataLayout &DL, DominatorTree &DT,
5244df029ccSDimitry Andric AssumptionCache *AC,
5254df029ccSDimitry Andric SmallSet<DbgAssignIntrinsic *, 8> *DbgAssignsToDelete,
526ac9a064cSDimitry Andric SmallSet<DbgVariableRecord *, 8> *DVRAssignsToDelete) {
527f8af5cf6SDimitry Andric StoreInst *OnlyStore = Info.OnlyStore;
528ac9a064cSDimitry Andric Value *ReplVal = OnlyStore->getOperand(0);
529ac9a064cSDimitry Andric // Loads may either load the stored value or uninitialized memory (undef).
530ac9a064cSDimitry Andric // If the stored value may be poison, then replacing an uninitialized memory
531ac9a064cSDimitry Andric // load with it would be incorrect. If the store dominates the load, we know
532ac9a064cSDimitry Andric // it is always initialized.
533ac9a064cSDimitry Andric bool RequireDominatingStore =
534ac9a064cSDimitry Andric isa<Instruction>(ReplVal) || !isGuaranteedNotToBePoison(ReplVal);
535f8af5cf6SDimitry Andric BasicBlock *StoreBB = OnlyStore->getParent();
536f8af5cf6SDimitry Andric int StoreIndex = -1;
537f8af5cf6SDimitry Andric
538f8af5cf6SDimitry Andric // Clear out UsingBlocks. We will reconstruct it here if needed.
539f8af5cf6SDimitry Andric Info.UsingBlocks.clear();
540f8af5cf6SDimitry Andric
541b60736ecSDimitry Andric for (User *U : make_early_inc_range(AI->users())) {
542b60736ecSDimitry Andric Instruction *UserInst = cast<Instruction>(U);
543e6d15924SDimitry Andric if (UserInst == OnlyStore)
544f8af5cf6SDimitry Andric continue;
545f8af5cf6SDimitry Andric LoadInst *LI = cast<LoadInst>(UserInst);
546f8af5cf6SDimitry Andric
547f8af5cf6SDimitry Andric // Okay, if we have a load from the alloca, we want to replace it with the
548f8af5cf6SDimitry Andric // only value stored to the alloca. We can do this if the value is
549f8af5cf6SDimitry Andric // dominated by the store. If not, we use the rest of the mem2reg machinery
550f8af5cf6SDimitry Andric // to insert the phi nodes as needed.
551ac9a064cSDimitry Andric if (RequireDominatingStore) {
552f8af5cf6SDimitry Andric if (LI->getParent() == StoreBB) {
553f8af5cf6SDimitry Andric // If we have a use that is in the same block as the store, compare the
554f8af5cf6SDimitry Andric // indices of the two instructions to see which one came first. If the
555f8af5cf6SDimitry Andric // load came before the store, we can't handle it.
556f8af5cf6SDimitry Andric if (StoreIndex == -1)
557f8af5cf6SDimitry Andric StoreIndex = LBI.getInstructionIndex(OnlyStore);
558f8af5cf6SDimitry Andric
559f8af5cf6SDimitry Andric if (unsigned(StoreIndex) > LBI.getInstructionIndex(LI)) {
560f8af5cf6SDimitry Andric // Can't handle this load, bail out.
561f8af5cf6SDimitry Andric Info.UsingBlocks.push_back(StoreBB);
562f8af5cf6SDimitry Andric continue;
563f8af5cf6SDimitry Andric }
564e6d15924SDimitry Andric } else if (!DT.dominates(StoreBB, LI->getParent())) {
565f8af5cf6SDimitry Andric // If the load and store are in different blocks, use BB dominance to
566f8af5cf6SDimitry Andric // check their relationships. If the store doesn't dom the use, bail
567f8af5cf6SDimitry Andric // out.
568f8af5cf6SDimitry Andric Info.UsingBlocks.push_back(LI->getParent());
569f8af5cf6SDimitry Andric continue;
570f8af5cf6SDimitry Andric }
571f8af5cf6SDimitry Andric }
572f8af5cf6SDimitry Andric
573f8af5cf6SDimitry Andric // Otherwise, we *can* safely rewrite this load.
574f8af5cf6SDimitry Andric // If the replacement value is the load, this must occur in unreachable
575f8af5cf6SDimitry Andric // code.
576f8af5cf6SDimitry Andric if (ReplVal == LI)
577344a3780SDimitry Andric ReplVal = PoisonValue::get(LI->getType());
57871d5a254SDimitry Andric
579e3b55780SDimitry Andric convertMetadataToAssumes(LI, ReplVal, DL, AC, &DT);
580f8af5cf6SDimitry Andric LI->replaceAllUsesWith(ReplVal);
581f8af5cf6SDimitry Andric LI->eraseFromParent();
582f8af5cf6SDimitry Andric LBI.deleteValue(LI);
583f8af5cf6SDimitry Andric }
584f8af5cf6SDimitry Andric
585f8af5cf6SDimitry Andric // Finally, after the scan, check to see if the store is all that is left.
586f8af5cf6SDimitry Andric if (!Info.UsingBlocks.empty())
587f8af5cf6SDimitry Andric return false; // If not, we'll have to fall back for the remainder.
588f8af5cf6SDimitry Andric
589e3b55780SDimitry Andric DIBuilder DIB(*AI->getModule(), /*AllowUnresolved*/ false);
590e3b55780SDimitry Andric // Update assignment tracking info for the store we're going to delete.
5914df029ccSDimitry Andric Info.AssignmentTracking.updateForDeletedStore(
592ac9a064cSDimitry Andric Info.OnlyStore, DIB, DbgAssignsToDelete, DVRAssignsToDelete);
593e3b55780SDimitry Andric
594f8af5cf6SDimitry Andric // Record debuginfo for the store and remove the declaration's
595f8af5cf6SDimitry Andric // debuginfo.
596b1c73532SDimitry Andric auto ConvertDebugInfoForStore = [&](auto &Container) {
597b1c73532SDimitry Andric for (auto *DbgItem : Container) {
598b1c73532SDimitry Andric if (DbgItem->isAddressOfVariable()) {
599b1c73532SDimitry Andric ConvertDebugDeclareToDebugValue(DbgItem, Info.OnlyStore, DIB);
600b1c73532SDimitry Andric DbgItem->eraseFromParent();
601b1c73532SDimitry Andric } else if (DbgItem->getExpression()->startsWithDeref()) {
602b1c73532SDimitry Andric DbgItem->eraseFromParent();
603b60736ecSDimitry Andric }
604f8af5cf6SDimitry Andric }
605b1c73532SDimitry Andric };
606b1c73532SDimitry Andric ConvertDebugInfoForStore(Info.DbgUsers);
607b1c73532SDimitry Andric ConvertDebugInfoForStore(Info.DPUsers);
608e3b55780SDimitry Andric
609e3b55780SDimitry Andric // Remove dbg.assigns linked to the alloca as these are now redundant.
610e3b55780SDimitry Andric at::deleteAssignmentMarkers(AI);
611e3b55780SDimitry Andric
612f8af5cf6SDimitry Andric // Remove the (now dead) store and alloca.
613f8af5cf6SDimitry Andric Info.OnlyStore->eraseFromParent();
614f8af5cf6SDimitry Andric LBI.deleteValue(Info.OnlyStore);
615f8af5cf6SDimitry Andric
616f8af5cf6SDimitry Andric AI->eraseFromParent();
617f8af5cf6SDimitry Andric return true;
618f8af5cf6SDimitry Andric }
619f8af5cf6SDimitry Andric
620f8af5cf6SDimitry Andric /// Many allocas are only used within a single basic block. If this is the
621f8af5cf6SDimitry Andric /// case, avoid traversing the CFG and inserting a lot of potentially useless
622f8af5cf6SDimitry Andric /// PHI nodes by just performing a single linear pass over the basic block
623f8af5cf6SDimitry Andric /// using the Alloca.
624f8af5cf6SDimitry Andric ///
625f8af5cf6SDimitry Andric /// If we cannot promote this alloca (because it is read before it is written),
626dd58ef01SDimitry Andric /// return false. This is necessary in cases where, due to control flow, the
627dd58ef01SDimitry Andric /// alloca is undefined only on some control flow paths. e.g. code like
628dd58ef01SDimitry Andric /// this is correct in LLVM IR:
629dd58ef01SDimitry Andric /// // A is an alloca with no stores so far
630dd58ef01SDimitry Andric /// for (...) {
631dd58ef01SDimitry Andric /// int t = *A;
632dd58ef01SDimitry Andric /// if (!first_iteration)
633dd58ef01SDimitry Andric /// use(t);
634dd58ef01SDimitry Andric /// *A = 42;
635dd58ef01SDimitry Andric /// }
6364df029ccSDimitry Andric static bool
promoteSingleBlockAlloca(AllocaInst * AI,const AllocaInfo & Info,LargeBlockInfo & LBI,const DataLayout & DL,DominatorTree & DT,AssumptionCache * AC,SmallSet<DbgAssignIntrinsic *,8> * DbgAssignsToDelete,SmallSet<DbgVariableRecord *,8> * DVRAssignsToDelete)6374df029ccSDimitry Andric promoteSingleBlockAlloca(AllocaInst *AI, const AllocaInfo &Info,
6384df029ccSDimitry Andric LargeBlockInfo &LBI, const DataLayout &DL,
6394df029ccSDimitry Andric DominatorTree &DT, AssumptionCache *AC,
6404df029ccSDimitry Andric SmallSet<DbgAssignIntrinsic *, 8> *DbgAssignsToDelete,
641ac9a064cSDimitry Andric SmallSet<DbgVariableRecord *, 8> *DVRAssignsToDelete) {
642f8af5cf6SDimitry Andric // The trickiest case to handle is when we have large blocks. Because of this,
643f8af5cf6SDimitry Andric // this code is optimized assuming that large blocks happen. This does not
644f8af5cf6SDimitry Andric // significantly pessimize the small block case. This uses LargeBlockInfo to
645f8af5cf6SDimitry Andric // make it efficient to get the index of various operations in the block.
646f8af5cf6SDimitry Andric
647f8af5cf6SDimitry Andric // Walk the use-def list of the alloca, getting the locations of all stores.
648044eb2f6SDimitry Andric using StoresByIndexTy = SmallVector<std::pair<unsigned, StoreInst *>, 64>;
649f8af5cf6SDimitry Andric StoresByIndexTy StoresByIndex;
650f8af5cf6SDimitry Andric
6515ca98fd9SDimitry Andric for (User *U : AI->users())
6525ca98fd9SDimitry Andric if (StoreInst *SI = dyn_cast<StoreInst>(U))
653f8af5cf6SDimitry Andric StoresByIndex.push_back(std::make_pair(LBI.getInstructionIndex(SI), SI));
654f8af5cf6SDimitry Andric
655f8af5cf6SDimitry Andric // Sort the stores by their index, making it efficient to do a lookup with a
656f8af5cf6SDimitry Andric // binary search.
657d8e91e46SDimitry Andric llvm::sort(StoresByIndex, less_first());
658f8af5cf6SDimitry Andric
659f8af5cf6SDimitry Andric // Walk all of the loads from this alloca, replacing them with the nearest
660f8af5cf6SDimitry Andric // store above them, if any.
661b60736ecSDimitry Andric for (User *U : make_early_inc_range(AI->users())) {
662b60736ecSDimitry Andric LoadInst *LI = dyn_cast<LoadInst>(U);
663f8af5cf6SDimitry Andric if (!LI)
664f8af5cf6SDimitry Andric continue;
665f8af5cf6SDimitry Andric
666f8af5cf6SDimitry Andric unsigned LoadIdx = LBI.getInstructionIndex(LI);
667f8af5cf6SDimitry Andric
668f8af5cf6SDimitry Andric // Find the nearest store that has a lower index than this load.
669e6d15924SDimitry Andric StoresByIndexTy::iterator I = llvm::lower_bound(
670e6d15924SDimitry Andric StoresByIndex,
671e6d15924SDimitry Andric std::make_pair(LoadIdx, static_cast<StoreInst *>(nullptr)),
672f8af5cf6SDimitry Andric less_first());
6731f917f69SDimitry Andric Value *ReplVal;
674dd58ef01SDimitry Andric if (I == StoresByIndex.begin()) {
675dd58ef01SDimitry Andric if (StoresByIndex.empty())
676dd58ef01SDimitry Andric // If there are no stores, the load takes the undef value.
6771f917f69SDimitry Andric ReplVal = UndefValue::get(LI->getType());
678f8af5cf6SDimitry Andric else
679dd58ef01SDimitry Andric // There is no store before this load, bail out (load may be affected
680dd58ef01SDimitry Andric // by the following stores - see main comment).
681dd58ef01SDimitry Andric return false;
68271d5a254SDimitry Andric } else {
6831f917f69SDimitry Andric // Otherwise, there was a store before this load, the load takes its
6841f917f69SDimitry Andric // value.
6851f917f69SDimitry Andric ReplVal = std::prev(I)->second->getOperand(0);
6861f917f69SDimitry Andric }
6871f917f69SDimitry Andric
688e3b55780SDimitry Andric convertMetadataToAssumes(LI, ReplVal, DL, AC, &DT);
689f8af5cf6SDimitry Andric
690eb11fae6SDimitry Andric // If the replacement value is the load, this must occur in unreachable
691eb11fae6SDimitry Andric // code.
692eb11fae6SDimitry Andric if (ReplVal == LI)
693344a3780SDimitry Andric ReplVal = PoisonValue::get(LI->getType());
694eb11fae6SDimitry Andric
69571d5a254SDimitry Andric LI->replaceAllUsesWith(ReplVal);
696f8af5cf6SDimitry Andric LI->eraseFromParent();
697f8af5cf6SDimitry Andric LBI.deleteValue(LI);
698f8af5cf6SDimitry Andric }
699f8af5cf6SDimitry Andric
700f8af5cf6SDimitry Andric // Remove the (now dead) stores and alloca.
701e3b55780SDimitry Andric DIBuilder DIB(*AI->getModule(), /*AllowUnresolved*/ false);
702f8af5cf6SDimitry Andric while (!AI->use_empty()) {
7035ca98fd9SDimitry Andric StoreInst *SI = cast<StoreInst>(AI->user_back());
704e3b55780SDimitry Andric // Update assignment tracking info for the store we're going to delete.
7054df029ccSDimitry Andric Info.AssignmentTracking.updateForDeletedStore(SI, DIB, DbgAssignsToDelete,
706ac9a064cSDimitry Andric DVRAssignsToDelete);
707f8af5cf6SDimitry Andric // Record debuginfo for the store before removing it.
708b1c73532SDimitry Andric auto DbgUpdateForStore = [&](auto &Container) {
709b1c73532SDimitry Andric for (auto *DbgItem : Container) {
710b1c73532SDimitry Andric if (DbgItem->isAddressOfVariable()) {
711b1c73532SDimitry Andric ConvertDebugDeclareToDebugValue(DbgItem, SI, DIB);
712f8af5cf6SDimitry Andric }
713b60736ecSDimitry Andric }
714b1c73532SDimitry Andric };
715b1c73532SDimitry Andric DbgUpdateForStore(Info.DbgUsers);
716b1c73532SDimitry Andric DbgUpdateForStore(Info.DPUsers);
717b1c73532SDimitry Andric
718f8af5cf6SDimitry Andric SI->eraseFromParent();
719f8af5cf6SDimitry Andric LBI.deleteValue(SI);
720f8af5cf6SDimitry Andric }
721f8af5cf6SDimitry Andric
722e3b55780SDimitry Andric // Remove dbg.assigns linked to the alloca as these are now redundant.
723e3b55780SDimitry Andric at::deleteAssignmentMarkers(AI);
724f8af5cf6SDimitry Andric AI->eraseFromParent();
725f8af5cf6SDimitry Andric
726f8af5cf6SDimitry Andric // The alloca's debuginfo can be removed as well.
727b1c73532SDimitry Andric auto DbgUpdateForAlloca = [&](auto &Container) {
728b1c73532SDimitry Andric for (auto *DbgItem : Container)
729b1c73532SDimitry Andric if (DbgItem->isAddressOfVariable() ||
730b1c73532SDimitry Andric DbgItem->getExpression()->startsWithDeref())
731b1c73532SDimitry Andric DbgItem->eraseFromParent();
732b1c73532SDimitry Andric };
733b1c73532SDimitry Andric DbgUpdateForAlloca(Info.DbgUsers);
734b1c73532SDimitry Andric DbgUpdateForAlloca(Info.DPUsers);
735f8af5cf6SDimitry Andric
736f8af5cf6SDimitry Andric ++NumLocalPromoted;
737dd58ef01SDimitry Andric return true;
738f8af5cf6SDimitry Andric }
739f8af5cf6SDimitry Andric
run()740009b1c42SEd Schouten void PromoteMem2Reg::run() {
741cf099d11SDimitry Andric Function &F = *DT.getRoot()->getParent();
742009b1c42SEd Schouten
743b60736ecSDimitry Andric AllocaDbgUsers.resize(Allocas.size());
744e3b55780SDimitry Andric AllocaATInfo.resize(Allocas.size());
745b1c73532SDimitry Andric AllocaDPUsers.resize(Allocas.size());
746009b1c42SEd Schouten
747009b1c42SEd Schouten AllocaInfo Info;
748009b1c42SEd Schouten LargeBlockInfo LBI;
74901095a5dSDimitry Andric ForwardIDFCalculator IDF(DT);
750009b1c42SEd Schouten
751ac9a064cSDimitry Andric NoSignedZeros = F.getFnAttribute("no-signed-zeros-fp-math").getValueAsBool();
752ac9a064cSDimitry Andric
753009b1c42SEd Schouten for (unsigned AllocaNum = 0; AllocaNum != Allocas.size(); ++AllocaNum) {
754009b1c42SEd Schouten AllocaInst *AI = Allocas[AllocaNum];
755009b1c42SEd Schouten
756f8af5cf6SDimitry Andric assert(isAllocaPromotable(AI) && "Cannot promote non-promotable alloca!");
757009b1c42SEd Schouten assert(AI->getParent()->getParent() == &F &&
758009b1c42SEd Schouten "All allocas should be in the same function, which is same as DF!");
759009b1c42SEd Schouten
760b60736ecSDimitry Andric removeIntrinsicUsers(AI);
761411bd29eSDimitry Andric
762009b1c42SEd Schouten if (AI->use_empty()) {
763009b1c42SEd Schouten // If there are no uses of the alloca, just delete it now.
764009b1c42SEd Schouten AI->eraseFromParent();
765009b1c42SEd Schouten
766009b1c42SEd Schouten // Remove the alloca from the Allocas list, since it has been processed
767009b1c42SEd Schouten RemoveFromAllocasList(AllocaNum);
768009b1c42SEd Schouten ++NumDeadAlloca;
769009b1c42SEd Schouten continue;
770009b1c42SEd Schouten }
771009b1c42SEd Schouten
772009b1c42SEd Schouten // Calculate the set of read and write-locations for each alloca. This is
773009b1c42SEd Schouten // analogous to finding the 'uses' and 'definitions' of each variable.
774009b1c42SEd Schouten Info.AnalyzeAlloca(AI);
775009b1c42SEd Schouten
776009b1c42SEd Schouten // If there is only a single store to this value, replace any loads of
777009b1c42SEd Schouten // it that are directly dominated by the definition with the value stored.
778009b1c42SEd Schouten if (Info.DefiningBlocks.size() == 1) {
7797fa27ce4SDimitry Andric if (rewriteSingleStoreAlloca(AI, Info, LBI, SQ.DL, DT, AC,
780ac9a064cSDimitry Andric &DbgAssignsToDelete, &DVRAssignsToDelete)) {
781009b1c42SEd Schouten // The alloca has been processed, move on.
782009b1c42SEd Schouten RemoveFromAllocasList(AllocaNum);
783009b1c42SEd Schouten ++NumSingleStore;
784009b1c42SEd Schouten continue;
785009b1c42SEd Schouten }
786009b1c42SEd Schouten }
787009b1c42SEd Schouten
788009b1c42SEd Schouten // If the alloca is only read and written in one basic block, just perform a
789009b1c42SEd Schouten // linear sweep over the block to eliminate it.
790dd58ef01SDimitry Andric if (Info.OnlyUsedInOneBlock &&
7917fa27ce4SDimitry Andric promoteSingleBlockAlloca(AI, Info, LBI, SQ.DL, DT, AC,
792ac9a064cSDimitry Andric &DbgAssignsToDelete, &DVRAssignsToDelete)) {
793009b1c42SEd Schouten // The alloca has been processed, move on.
794009b1c42SEd Schouten RemoveFromAllocasList(AllocaNum);
795009b1c42SEd Schouten continue;
796009b1c42SEd Schouten }
797009b1c42SEd Schouten
798009b1c42SEd Schouten // If we haven't computed a numbering for the BB's in the function, do so
799009b1c42SEd Schouten // now.
800009b1c42SEd Schouten if (BBNumbers.empty()) {
801009b1c42SEd Schouten unsigned ID = 0;
8025a5ac124SDimitry Andric for (auto &BB : F)
8035a5ac124SDimitry Andric BBNumbers[&BB] = ID++;
804009b1c42SEd Schouten }
805009b1c42SEd Schouten
8066fe5c7aaSRoman Divacky // Remember the dbg.declare intrinsic describing this alloca, if any.
807b60736ecSDimitry Andric if (!Info.DbgUsers.empty())
808b60736ecSDimitry Andric AllocaDbgUsers[AllocaNum] = Info.DbgUsers;
809e3b55780SDimitry Andric if (!Info.AssignmentTracking.empty())
810e3b55780SDimitry Andric AllocaATInfo[AllocaNum] = Info.AssignmentTracking;
811b1c73532SDimitry Andric if (!Info.DPUsers.empty())
812b1c73532SDimitry Andric AllocaDPUsers[AllocaNum] = Info.DPUsers;
8136fe5c7aaSRoman Divacky
814009b1c42SEd Schouten // Keep the reverse mapping of the 'Allocas' array for the rename pass.
815009b1c42SEd Schouten AllocaLookup[Allocas[AllocaNum]] = AllocaNum;
816009b1c42SEd Schouten
8175a5ac124SDimitry Andric // Unique the set of defining blocks for efficient lookup.
818e6d15924SDimitry Andric SmallPtrSet<BasicBlock *, 32> DefBlocks(Info.DefiningBlocks.begin(),
819e6d15924SDimitry Andric Info.DefiningBlocks.end());
8205a5ac124SDimitry Andric
8215a5ac124SDimitry Andric // Determine which blocks the value is live in. These are blocks which lead
8225a5ac124SDimitry Andric // to uses.
8235a5ac124SDimitry Andric SmallPtrSet<BasicBlock *, 32> LiveInBlocks;
8245a5ac124SDimitry Andric ComputeLiveInBlocks(AI, Info, DefBlocks, LiveInBlocks);
8255a5ac124SDimitry Andric
8265a5ac124SDimitry Andric // At this point, we're committed to promoting the alloca using IDF's, and
8275a5ac124SDimitry Andric // the standard SSA construction algorithm. Determine which blocks need phi
8285a5ac124SDimitry Andric // nodes and see if we can optimize out some work by avoiding insertion of
8295a5ac124SDimitry Andric // dead phi nodes.
8305a5ac124SDimitry Andric IDF.setLiveInBlocks(LiveInBlocks);
8315a5ac124SDimitry Andric IDF.setDefiningBlocks(DefBlocks);
8325a5ac124SDimitry Andric SmallVector<BasicBlock *, 32> PHIBlocks;
8335a5ac124SDimitry Andric IDF.calculate(PHIBlocks);
834d8e91e46SDimitry Andric llvm::sort(PHIBlocks, [this](BasicBlock *A, BasicBlock *B) {
835e6d15924SDimitry Andric return BBNumbers.find(A)->second < BBNumbers.find(B)->second;
8365a5ac124SDimitry Andric });
8375a5ac124SDimitry Andric
8385a5ac124SDimitry Andric unsigned CurrentVersion = 0;
839044eb2f6SDimitry Andric for (BasicBlock *BB : PHIBlocks)
840044eb2f6SDimitry Andric QueuePhiNode(BB, AllocaNum, CurrentVersion);
841009b1c42SEd Schouten }
842009b1c42SEd Schouten
8437fa27ce4SDimitry Andric if (Allocas.empty()) {
8447fa27ce4SDimitry Andric cleanUpDbgAssigns();
845009b1c42SEd Schouten return; // All of the allocas must have been trivial!
8467fa27ce4SDimitry Andric }
847009b1c42SEd Schouten LBI.clear();
848009b1c42SEd Schouten
849009b1c42SEd Schouten // Set the incoming values for the basic block to be null values for all of
850009b1c42SEd Schouten // the alloca's. We do this in case there is a load of a value that has not
851009b1c42SEd Schouten // been stored yet. In this case, it will get this null value.
852009b1c42SEd Schouten RenamePassData::ValVector Values(Allocas.size());
853009b1c42SEd Schouten for (unsigned i = 0, e = Allocas.size(); i != e; ++i)
854009b1c42SEd Schouten Values[i] = UndefValue::get(Allocas[i]->getAllocatedType());
855009b1c42SEd Schouten
856eb11fae6SDimitry Andric // When handling debug info, treat all incoming values as if they have unknown
857eb11fae6SDimitry Andric // locations until proven otherwise.
858eb11fae6SDimitry Andric RenamePassData::LocationVector Locations(Allocas.size());
859eb11fae6SDimitry Andric
860009b1c42SEd Schouten // Walks all basic blocks in the function performing the SSA rename algorithm
861009b1c42SEd Schouten // and inserting the phi nodes we marked as necessary
862009b1c42SEd Schouten std::vector<RenamePassData> RenamePassWorkList;
863eb11fae6SDimitry Andric RenamePassWorkList.emplace_back(&F.front(), nullptr, std::move(Values),
864eb11fae6SDimitry Andric std::move(Locations));
865829000e0SRoman Divacky do {
866044eb2f6SDimitry Andric RenamePassData RPD = std::move(RenamePassWorkList.back());
867009b1c42SEd Schouten RenamePassWorkList.pop_back();
868009b1c42SEd Schouten // RenamePass may add new worklist entries.
869eb11fae6SDimitry Andric RenamePass(RPD.BB, RPD.Pred, RPD.Values, RPD.Locations, RenamePassWorkList);
870829000e0SRoman Divacky } while (!RenamePassWorkList.empty());
871009b1c42SEd Schouten
872009b1c42SEd Schouten // The renamer uses the Visited set to avoid infinite loops. Clear it now.
873009b1c42SEd Schouten Visited.clear();
874009b1c42SEd Schouten
875009b1c42SEd Schouten // Remove the allocas themselves from the function.
876044eb2f6SDimitry Andric for (Instruction *A : Allocas) {
877e3b55780SDimitry Andric // Remove dbg.assigns linked to the alloca as these are now redundant.
878e3b55780SDimitry Andric at::deleteAssignmentMarkers(A);
879009b1c42SEd Schouten // If there are any uses of the alloca instructions left, they must be in
880cf099d11SDimitry Andric // unreachable basic blocks that were not processed by walking the dominator
881cf099d11SDimitry Andric // tree. Just delete the users now.
882009b1c42SEd Schouten if (!A->use_empty())
883344a3780SDimitry Andric A->replaceAllUsesWith(PoisonValue::get(A->getType()));
884009b1c42SEd Schouten A->eraseFromParent();
885009b1c42SEd Schouten }
886009b1c42SEd Schouten
887145449b1SDimitry Andric // Remove alloca's dbg.declare intrinsics from the function.
888b1c73532SDimitry Andric auto RemoveDbgDeclares = [&](auto &Container) {
889b1c73532SDimitry Andric for (auto &DbgUsers : Container) {
890b1c73532SDimitry Andric for (auto *DbgItem : DbgUsers)
891b1c73532SDimitry Andric if (DbgItem->isAddressOfVariable() ||
892b1c73532SDimitry Andric DbgItem->getExpression()->startsWithDeref())
893b1c73532SDimitry Andric DbgItem->eraseFromParent();
894b60736ecSDimitry Andric }
895b1c73532SDimitry Andric };
896b1c73532SDimitry Andric RemoveDbgDeclares(AllocaDbgUsers);
897b1c73532SDimitry Andric RemoveDbgDeclares(AllocaDPUsers);
898009b1c42SEd Schouten
899009b1c42SEd Schouten // Loop over all of the PHI nodes and see if there are any that we can get
900009b1c42SEd Schouten // rid of because they merge all of the same incoming values. This can
901009b1c42SEd Schouten // happen due to undef values coming into the PHI nodes. This process is
902009b1c42SEd Schouten // iterative, because eliminating one PHI node can cause others to be removed.
903009b1c42SEd Schouten bool EliminatedAPHI = true;
904009b1c42SEd Schouten while (EliminatedAPHI) {
905009b1c42SEd Schouten EliminatedAPHI = false;
906009b1c42SEd Schouten
907522600a2SDimitry Andric // Iterating over NewPhiNodes is deterministic, so it is safe to try to
908522600a2SDimitry Andric // simplify and RAUW them as we go. If it was not, we could add uses to
9095ca98fd9SDimitry Andric // the values we replace with in a non-deterministic order, thus creating
9105ca98fd9SDimitry Andric // non-deterministic def->use chains.
911f8af5cf6SDimitry Andric for (DenseMap<std::pair<unsigned, unsigned>, PHINode *>::iterator
912f8af5cf6SDimitry Andric I = NewPhiNodes.begin(),
913f8af5cf6SDimitry Andric E = NewPhiNodes.end();
914f8af5cf6SDimitry Andric I != E;) {
915009b1c42SEd Schouten PHINode *PN = I->second;
916009b1c42SEd Schouten
917009b1c42SEd Schouten // If this PHI node merges one value and/or undefs, get the value.
918145449b1SDimitry Andric if (Value *V = simplifyInstruction(PN, SQ)) {
919009b1c42SEd Schouten PN->replaceAllUsesWith(V);
920009b1c42SEd Schouten PN->eraseFromParent();
921009b1c42SEd Schouten NewPhiNodes.erase(I++);
922009b1c42SEd Schouten EliminatedAPHI = true;
923009b1c42SEd Schouten continue;
924009b1c42SEd Schouten }
925009b1c42SEd Schouten ++I;
926009b1c42SEd Schouten }
927009b1c42SEd Schouten }
928009b1c42SEd Schouten
929009b1c42SEd Schouten // At this point, the renamer has added entries to PHI nodes for all reachable
930009b1c42SEd Schouten // code. Unfortunately, there may be unreachable blocks which the renamer
931009b1c42SEd Schouten // hasn't traversed. If this is the case, the PHI nodes may not
932009b1c42SEd Schouten // have incoming values for all predecessors. Loop over all PHI nodes we have
9337fa27ce4SDimitry Andric // created, inserting poison values if they are missing any incoming values.
934f8af5cf6SDimitry Andric for (DenseMap<std::pair<unsigned, unsigned>, PHINode *>::iterator
935f8af5cf6SDimitry Andric I = NewPhiNodes.begin(),
936f8af5cf6SDimitry Andric E = NewPhiNodes.end();
937f8af5cf6SDimitry Andric I != E; ++I) {
938009b1c42SEd Schouten // We want to do this once per basic block. As such, only process a block
939009b1c42SEd Schouten // when we find the PHI that is the first entry in the block.
940009b1c42SEd Schouten PHINode *SomePHI = I->second;
941009b1c42SEd Schouten BasicBlock *BB = SomePHI->getParent();
942009b1c42SEd Schouten if (&BB->front() != SomePHI)
943009b1c42SEd Schouten continue;
944009b1c42SEd Schouten
945009b1c42SEd Schouten // Only do work here if there the PHI nodes are missing incoming values. We
946009b1c42SEd Schouten // know that all PHI nodes that were inserted in a block will have the same
947009b1c42SEd Schouten // number of incoming values, so we can just check any of them.
948009b1c42SEd Schouten if (SomePHI->getNumIncomingValues() == getNumPreds(BB))
949009b1c42SEd Schouten continue;
950009b1c42SEd Schouten
951009b1c42SEd Schouten // Get the preds for BB.
952b60736ecSDimitry Andric SmallVector<BasicBlock *, 16> Preds(predecessors(BB));
953009b1c42SEd Schouten
954009b1c42SEd Schouten // Ok, now we know that all of the PHI nodes are missing entries for some
955009b1c42SEd Schouten // basic blocks. Start by sorting the incoming predecessors for efficient
956009b1c42SEd Schouten // access.
957d8e91e46SDimitry Andric auto CompareBBNumbers = [this](BasicBlock *A, BasicBlock *B) {
958e6d15924SDimitry Andric return BBNumbers.find(A)->second < BBNumbers.find(B)->second;
959d8e91e46SDimitry Andric };
960d8e91e46SDimitry Andric llvm::sort(Preds, CompareBBNumbers);
961009b1c42SEd Schouten
962009b1c42SEd Schouten // Now we loop through all BB's which have entries in SomePHI and remove
963009b1c42SEd Schouten // them from the Preds list.
964009b1c42SEd Schouten for (unsigned i = 0, e = SomePHI->getNumIncomingValues(); i != e; ++i) {
965009b1c42SEd Schouten // Do a log(n) search of the Preds list for the entry we want.
966e6d15924SDimitry Andric SmallVectorImpl<BasicBlock *>::iterator EntIt = llvm::lower_bound(
967e6d15924SDimitry Andric Preds, SomePHI->getIncomingBlock(i), CompareBBNumbers);
968009b1c42SEd Schouten assert(EntIt != Preds.end() && *EntIt == SomePHI->getIncomingBlock(i) &&
969009b1c42SEd Schouten "PHI node has entry for a block which is not a predecessor!");
970009b1c42SEd Schouten
971009b1c42SEd Schouten // Remove the entry
972009b1c42SEd Schouten Preds.erase(EntIt);
973009b1c42SEd Schouten }
974009b1c42SEd Schouten
975009b1c42SEd Schouten // At this point, the blocks left in the preds list must have dummy
976009b1c42SEd Schouten // entries inserted into every PHI nodes for the block. Update all the phi
977009b1c42SEd Schouten // nodes in this block that we are inserting (there could be phis before
978009b1c42SEd Schouten // mem2reg runs).
979009b1c42SEd Schouten unsigned NumBadPreds = SomePHI->getNumIncomingValues();
980009b1c42SEd Schouten BasicBlock::iterator BBI = BB->begin();
981009b1c42SEd Schouten while ((SomePHI = dyn_cast<PHINode>(BBI++)) &&
982009b1c42SEd Schouten SomePHI->getNumIncomingValues() == NumBadPreds) {
9837fa27ce4SDimitry Andric Value *PoisonVal = PoisonValue::get(SomePHI->getType());
984044eb2f6SDimitry Andric for (BasicBlock *Pred : Preds)
9857fa27ce4SDimitry Andric SomePHI->addIncoming(PoisonVal, Pred);
986009b1c42SEd Schouten }
987009b1c42SEd Schouten }
988009b1c42SEd Schouten
989009b1c42SEd Schouten NewPhiNodes.clear();
9907fa27ce4SDimitry Andric cleanUpDbgAssigns();
991009b1c42SEd Schouten }
992009b1c42SEd Schouten
993eb11fae6SDimitry Andric /// Determine which blocks the value is live in.
994f8af5cf6SDimitry Andric ///
995f8af5cf6SDimitry Andric /// These are blocks which lead to uses. Knowing this allows us to avoid
996f8af5cf6SDimitry Andric /// inserting PHI nodes into blocks which don't lead to uses (thus, the
997f8af5cf6SDimitry Andric /// inserted phi nodes would be dead).
ComputeLiveInBlocks(AllocaInst * AI,AllocaInfo & Info,const SmallPtrSetImpl<BasicBlock * > & DefBlocks,SmallPtrSetImpl<BasicBlock * > & LiveInBlocks)998f8af5cf6SDimitry Andric void PromoteMem2Reg::ComputeLiveInBlocks(
999f8af5cf6SDimitry Andric AllocaInst *AI, AllocaInfo &Info,
100067c32a98SDimitry Andric const SmallPtrSetImpl<BasicBlock *> &DefBlocks,
100167c32a98SDimitry Andric SmallPtrSetImpl<BasicBlock *> &LiveInBlocks) {
1002009b1c42SEd Schouten // To determine liveness, we must iterate through the predecessors of blocks
1003009b1c42SEd Schouten // where the def is live. Blocks are added to the worklist if we need to
1004009b1c42SEd Schouten // check their predecessors. Start with all the using blocks.
100566e41e3cSRoman Divacky SmallVector<BasicBlock *, 64> LiveInBlockWorklist(Info.UsingBlocks.begin(),
100666e41e3cSRoman Divacky Info.UsingBlocks.end());
1007009b1c42SEd Schouten
1008009b1c42SEd Schouten // If any of the using blocks is also a definition block, check to see if the
1009009b1c42SEd Schouten // definition occurs before or after the use. If it happens before the use,
1010009b1c42SEd Schouten // the value isn't really live-in.
1011009b1c42SEd Schouten for (unsigned i = 0, e = LiveInBlockWorklist.size(); i != e; ++i) {
1012009b1c42SEd Schouten BasicBlock *BB = LiveInBlockWorklist[i];
1013f8af5cf6SDimitry Andric if (!DefBlocks.count(BB))
1014f8af5cf6SDimitry Andric continue;
1015009b1c42SEd Schouten
1016009b1c42SEd Schouten // Okay, this is a block that both uses and defines the value. If the first
1017009b1c42SEd Schouten // reference to the alloca is a def (store), then we know it isn't live-in.
1018009b1c42SEd Schouten for (BasicBlock::iterator I = BB->begin();; ++I) {
1019009b1c42SEd Schouten if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
1020f8af5cf6SDimitry Andric if (SI->getOperand(1) != AI)
1021f8af5cf6SDimitry Andric continue;
1022009b1c42SEd Schouten
1023009b1c42SEd Schouten // We found a store to the alloca before a load. The alloca is not
1024009b1c42SEd Schouten // actually live-in here.
1025009b1c42SEd Schouten LiveInBlockWorklist[i] = LiveInBlockWorklist.back();
1026009b1c42SEd Schouten LiveInBlockWorklist.pop_back();
102701095a5dSDimitry Andric --i;
102801095a5dSDimitry Andric --e;
1029009b1c42SEd Schouten break;
103059850d08SRoman Divacky }
103159850d08SRoman Divacky
1032e6d15924SDimitry Andric if (LoadInst *LI = dyn_cast<LoadInst>(I))
1033009b1c42SEd Schouten // Okay, we found a load before a store to the alloca. It is actually
1034009b1c42SEd Schouten // live into this block.
1035e6d15924SDimitry Andric if (LI->getOperand(0) == AI)
1036009b1c42SEd Schouten break;
1037009b1c42SEd Schouten }
1038009b1c42SEd Schouten }
1039009b1c42SEd Schouten
1040009b1c42SEd Schouten // Now that we have a set of blocks where the phi is live-in, recursively add
1041009b1c42SEd Schouten // their predecessors until we find the full region the value is live.
1042009b1c42SEd Schouten while (!LiveInBlockWorklist.empty()) {
1043009b1c42SEd Schouten BasicBlock *BB = LiveInBlockWorklist.pop_back_val();
1044009b1c42SEd Schouten
1045009b1c42SEd Schouten // The block really is live in here, insert it into the set. If already in
1046009b1c42SEd Schouten // the set, then it has already been processed.
104767c32a98SDimitry Andric if (!LiveInBlocks.insert(BB).second)
1048009b1c42SEd Schouten continue;
1049009b1c42SEd Schouten
1050009b1c42SEd Schouten // Since the value is live into BB, it is either defined in a predecessor or
1051009b1c42SEd Schouten // live into it to. Add the preds to the worklist unless they are a
1052009b1c42SEd Schouten // defining block.
1053044eb2f6SDimitry Andric for (BasicBlock *P : predecessors(BB)) {
1054009b1c42SEd Schouten // The value is not live into a predecessor if it defines the value.
1055009b1c42SEd Schouten if (DefBlocks.count(P))
1056009b1c42SEd Schouten continue;
1057009b1c42SEd Schouten
1058009b1c42SEd Schouten // Otherwise it is, add to the worklist.
1059009b1c42SEd Schouten LiveInBlockWorklist.push_back(P);
1060009b1c42SEd Schouten }
1061009b1c42SEd Schouten }
1062009b1c42SEd Schouten }
1063009b1c42SEd Schouten
1064eb11fae6SDimitry Andric /// Queue a phi-node to be added to a basic-block for a specific Alloca.
1065009b1c42SEd Schouten ///
1066f8af5cf6SDimitry Andric /// Returns true if there wasn't already a phi-node for that variable
QueuePhiNode(BasicBlock * BB,unsigned AllocaNo,unsigned & Version)1067009b1c42SEd Schouten bool PromoteMem2Reg::QueuePhiNode(BasicBlock *BB, unsigned AllocaNo,
1068cf099d11SDimitry Andric unsigned &Version) {
1069009b1c42SEd Schouten // Look up the basic-block in question.
1070522600a2SDimitry Andric PHINode *&PN = NewPhiNodes[std::make_pair(BBNumbers[BB], AllocaNo)];
1071009b1c42SEd Schouten
1072009b1c42SEd Schouten // If the BB already has a phi node added for the i'th alloca then we're done!
1073f8af5cf6SDimitry Andric if (PN)
1074f8af5cf6SDimitry Andric return false;
1075009b1c42SEd Schouten
1076009b1c42SEd Schouten // Create a PhiNode using the dereferenced type... and add the phi-node to the
1077009b1c42SEd Schouten // BasicBlock.
10786b943ff3SDimitry Andric PN = PHINode::Create(Allocas[AllocaNo]->getAllocatedType(), getNumPreds(BB),
1079b1c73532SDimitry Andric Allocas[AllocaNo]->getName() + "." + Twine(Version++));
1080b1c73532SDimitry Andric PN->insertBefore(BB->begin());
1081009b1c42SEd Schouten ++NumPHIInsert;
1082009b1c42SEd Schouten PhiToAllocaMap[PN] = AllocaNo;
1083009b1c42SEd Schouten return true;
1084009b1c42SEd Schouten }
1085009b1c42SEd Schouten
1086eb11fae6SDimitry Andric /// Update the debug location of a phi. \p ApplyMergedLoc indicates whether to
1087eb11fae6SDimitry Andric /// create a merged location incorporating \p DL, or to set \p DL directly.
updateForIncomingValueLocation(PHINode * PN,DebugLoc DL,bool ApplyMergedLoc)1088eb11fae6SDimitry Andric static void updateForIncomingValueLocation(PHINode *PN, DebugLoc DL,
1089eb11fae6SDimitry Andric bool ApplyMergedLoc) {
1090eb11fae6SDimitry Andric if (ApplyMergedLoc)
1091eb11fae6SDimitry Andric PN->applyMergedLocation(PN->getDebugLoc(), DL);
1092eb11fae6SDimitry Andric else
1093eb11fae6SDimitry Andric PN->setDebugLoc(DL);
1094eb11fae6SDimitry Andric }
1095eb11fae6SDimitry Andric
1096eb11fae6SDimitry Andric /// Recursively traverse the CFG of the function, renaming loads and
1097f8af5cf6SDimitry Andric /// stores to the allocas which we are promoting.
1098f8af5cf6SDimitry Andric ///
1099f8af5cf6SDimitry Andric /// IncomingVals indicates what value each Alloca contains on exit from the
1100f8af5cf6SDimitry Andric /// predecessor block Pred.
RenamePass(BasicBlock * BB,BasicBlock * Pred,RenamePassData::ValVector & IncomingVals,RenamePassData::LocationVector & IncomingLocs,std::vector<RenamePassData> & Worklist)1101009b1c42SEd Schouten void PromoteMem2Reg::RenamePass(BasicBlock *BB, BasicBlock *Pred,
1102009b1c42SEd Schouten RenamePassData::ValVector &IncomingVals,
1103eb11fae6SDimitry Andric RenamePassData::LocationVector &IncomingLocs,
1104009b1c42SEd Schouten std::vector<RenamePassData> &Worklist) {
1105009b1c42SEd Schouten NextIteration:
1106009b1c42SEd Schouten // If we are inserting any phi nodes into this BB, they will already be in the
1107009b1c42SEd Schouten // block.
1108009b1c42SEd Schouten if (PHINode *APN = dyn_cast<PHINode>(BB->begin())) {
1109009b1c42SEd Schouten // If we have PHI nodes to update, compute the number of edges from Pred to
1110009b1c42SEd Schouten // BB.
1111009b1c42SEd Schouten if (PhiToAllocaMap.count(APN)) {
1112009b1c42SEd Schouten // We want to be able to distinguish between PHI nodes being inserted by
1113009b1c42SEd Schouten // this invocation of mem2reg from those phi nodes that already existed in
1114009b1c42SEd Schouten // the IR before mem2reg was run. We determine that APN is being inserted
1115009b1c42SEd Schouten // because it is missing incoming edges. All other PHI nodes being
1116009b1c42SEd Schouten // inserted by this pass of mem2reg will have the same number of incoming
1117009b1c42SEd Schouten // operands so far. Remember this count.
1118009b1c42SEd Schouten unsigned NewPHINumOperands = APN->getNumOperands();
1119009b1c42SEd Schouten
1120b60736ecSDimitry Andric unsigned NumEdges = llvm::count(successors(Pred), BB);
1121009b1c42SEd Schouten assert(NumEdges && "Must be at least one edge from Pred to BB!");
1122009b1c42SEd Schouten
1123009b1c42SEd Schouten // Add entries for all the phis.
1124009b1c42SEd Schouten BasicBlock::iterator PNI = BB->begin();
1125009b1c42SEd Schouten do {
1126009b1c42SEd Schouten unsigned AllocaNo = PhiToAllocaMap[APN];
1127009b1c42SEd Schouten
1128eb11fae6SDimitry Andric // Update the location of the phi node.
1129eb11fae6SDimitry Andric updateForIncomingValueLocation(APN, IncomingLocs[AllocaNo],
1130eb11fae6SDimitry Andric APN->getNumIncomingValues() > 0);
1131eb11fae6SDimitry Andric
1132009b1c42SEd Schouten // Add N incoming values to the PHI node.
1133009b1c42SEd Schouten for (unsigned i = 0; i != NumEdges; ++i)
1134009b1c42SEd Schouten APN->addIncoming(IncomingVals[AllocaNo], Pred);
1135009b1c42SEd Schouten
1136ac9a064cSDimitry Andric // For the sequence `return X > 0.0 ? X : -X`, it is expected that this
1137ac9a064cSDimitry Andric // results in fabs intrinsic. However, without no-signed-zeros(nsz) flag
1138ac9a064cSDimitry Andric // on the phi node generated at this stage, fabs folding does not
1139ac9a064cSDimitry Andric // happen. So, we try to infer nsz flag from the function attributes to
1140ac9a064cSDimitry Andric // enable this fabs folding.
1141ac9a064cSDimitry Andric if (isa<FPMathOperator>(APN) && NoSignedZeros)
1142ac9a064cSDimitry Andric APN->setHasNoSignedZeros(true);
1143ac9a064cSDimitry Andric
1144009b1c42SEd Schouten // The currently active variable for this block is now the PHI.
1145009b1c42SEd Schouten IncomingVals[AllocaNo] = APN;
1146e3b55780SDimitry Andric AllocaATInfo[AllocaNo].updateForNewPhi(APN, DIB);
1147b1c73532SDimitry Andric auto ConvertDbgDeclares = [&](auto &Container) {
1148b1c73532SDimitry Andric for (auto *DbgItem : Container)
1149b1c73532SDimitry Andric if (DbgItem->isAddressOfVariable())
1150b1c73532SDimitry Andric ConvertDebugDeclareToDebugValue(DbgItem, APN, DIB);
1151b1c73532SDimitry Andric };
1152b1c73532SDimitry Andric ConvertDbgDeclares(AllocaDbgUsers[AllocaNo]);
1153b1c73532SDimitry Andric ConvertDbgDeclares(AllocaDPUsers[AllocaNo]);
1154009b1c42SEd Schouten
1155009b1c42SEd Schouten // Get the next phi node.
1156009b1c42SEd Schouten ++PNI;
1157009b1c42SEd Schouten APN = dyn_cast<PHINode>(PNI);
11585ca98fd9SDimitry Andric if (!APN)
1159f8af5cf6SDimitry Andric break;
1160009b1c42SEd Schouten
1161009b1c42SEd Schouten // Verify that it is missing entries. If not, it is not being inserted
1162009b1c42SEd Schouten // by this mem2reg invocation so we want to ignore it.
1163009b1c42SEd Schouten } while (APN->getNumOperands() == NewPHINumOperands);
1164009b1c42SEd Schouten }
1165009b1c42SEd Schouten }
1166009b1c42SEd Schouten
1167009b1c42SEd Schouten // Don't revisit blocks.
116867c32a98SDimitry Andric if (!Visited.insert(BB).second)
1169f8af5cf6SDimitry Andric return;
1170009b1c42SEd Schouten
1171d8e91e46SDimitry Andric for (BasicBlock::iterator II = BB->begin(); !II->isTerminator();) {
1172dd58ef01SDimitry Andric Instruction *I = &*II++; // get the instruction, increment iterator
1173009b1c42SEd Schouten
1174009b1c42SEd Schouten if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
1175009b1c42SEd Schouten AllocaInst *Src = dyn_cast<AllocaInst>(LI->getPointerOperand());
1176f8af5cf6SDimitry Andric if (!Src)
1177f8af5cf6SDimitry Andric continue;
1178009b1c42SEd Schouten
1179cf099d11SDimitry Andric DenseMap<AllocaInst *, unsigned>::iterator AI = AllocaLookup.find(Src);
1180f8af5cf6SDimitry Andric if (AI == AllocaLookup.end())
1181f8af5cf6SDimitry Andric continue;
1182009b1c42SEd Schouten
1183009b1c42SEd Schouten Value *V = IncomingVals[AI->second];
1184e3b55780SDimitry Andric convertMetadataToAssumes(LI, V, SQ.DL, AC, &DT);
118571d5a254SDimitry Andric
1186009b1c42SEd Schouten // Anything using the load now uses the current value.
1187009b1c42SEd Schouten LI->replaceAllUsesWith(V);
1188e3b55780SDimitry Andric LI->eraseFromParent();
1189009b1c42SEd Schouten } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
1190009b1c42SEd Schouten // Delete this instruction and mark the name as the current holder of the
1191009b1c42SEd Schouten // value
1192009b1c42SEd Schouten AllocaInst *Dest = dyn_cast<AllocaInst>(SI->getPointerOperand());
1193f8af5cf6SDimitry Andric if (!Dest)
1194f8af5cf6SDimitry Andric continue;
1195009b1c42SEd Schouten
1196cf099d11SDimitry Andric DenseMap<AllocaInst *, unsigned>::iterator ai = AllocaLookup.find(Dest);
1197009b1c42SEd Schouten if (ai == AllocaLookup.end())
1198009b1c42SEd Schouten continue;
1199009b1c42SEd Schouten
1200009b1c42SEd Schouten // what value were we writing?
1201eb11fae6SDimitry Andric unsigned AllocaNo = ai->second;
1202eb11fae6SDimitry Andric IncomingVals[AllocaNo] = SI->getOperand(0);
1203eb11fae6SDimitry Andric
1204989df958SRoman Divacky // Record debuginfo for the store before removing it.
1205eb11fae6SDimitry Andric IncomingLocs[AllocaNo] = SI->getDebugLoc();
12064df029ccSDimitry Andric AllocaATInfo[AllocaNo].updateForDeletedStore(SI, DIB, &DbgAssignsToDelete,
1207ac9a064cSDimitry Andric &DVRAssignsToDelete);
1208b1c73532SDimitry Andric auto ConvertDbgDeclares = [&](auto &Container) {
1209b1c73532SDimitry Andric for (auto *DbgItem : Container)
1210b1c73532SDimitry Andric if (DbgItem->isAddressOfVariable())
1211b1c73532SDimitry Andric ConvertDebugDeclareToDebugValue(DbgItem, SI, DIB);
1212b1c73532SDimitry Andric };
1213b1c73532SDimitry Andric ConvertDbgDeclares(AllocaDbgUsers[ai->second]);
1214b1c73532SDimitry Andric ConvertDbgDeclares(AllocaDPUsers[ai->second]);
1215e3b55780SDimitry Andric SI->eraseFromParent();
1216009b1c42SEd Schouten }
1217009b1c42SEd Schouten }
1218009b1c42SEd Schouten
1219009b1c42SEd Schouten // 'Recurse' to our successors.
1220009b1c42SEd Schouten succ_iterator I = succ_begin(BB), E = succ_end(BB);
1221f8af5cf6SDimitry Andric if (I == E)
1222f8af5cf6SDimitry Andric return;
1223009b1c42SEd Schouten
1224009b1c42SEd Schouten // Keep track of the successors so we don't visit the same successor twice
1225009b1c42SEd Schouten SmallPtrSet<BasicBlock *, 8> VisitedSuccs;
1226009b1c42SEd Schouten
1227009b1c42SEd Schouten // Handle the first successor without using the worklist.
1228009b1c42SEd Schouten VisitedSuccs.insert(*I);
1229009b1c42SEd Schouten Pred = BB;
1230009b1c42SEd Schouten BB = *I;
1231009b1c42SEd Schouten ++I;
1232009b1c42SEd Schouten
1233009b1c42SEd Schouten for (; I != E; ++I)
123467c32a98SDimitry Andric if (VisitedSuccs.insert(*I).second)
1235eb11fae6SDimitry Andric Worklist.emplace_back(*I, Pred, IncomingVals, IncomingLocs);
1236009b1c42SEd Schouten
1237009b1c42SEd Schouten goto NextIteration;
1238009b1c42SEd Schouten }
1239009b1c42SEd Schouten
PromoteMemToReg(ArrayRef<AllocaInst * > Allocas,DominatorTree & DT,AssumptionCache * AC)1240f8af5cf6SDimitry Andric void llvm::PromoteMemToReg(ArrayRef<AllocaInst *> Allocas, DominatorTree &DT,
124171d5a254SDimitry Andric AssumptionCache *AC) {
1242009b1c42SEd Schouten // If there is nothing to do, bail out...
1243f8af5cf6SDimitry Andric if (Allocas.empty())
1244f8af5cf6SDimitry Andric return;
1245009b1c42SEd Schouten
124671d5a254SDimitry Andric PromoteMem2Reg(Allocas, DT, AC).run();
1247009b1c42SEd Schouten }
1248