xref: /freebsd-src/contrib/llvm-project/llvm/lib/Transforms/Utils/PromoteMemoryToRegister.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
10b57cec5SDimitry Andric //===- PromoteMemoryToRegister.cpp - Convert allocas to registers ---------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This file promotes memory references to be register references.  It promotes
100b57cec5SDimitry Andric // alloca instructions which only have loads and stores as uses.  An alloca is
110b57cec5SDimitry Andric // transformed by using iterated dominator frontiers to place PHI nodes, then
120b57cec5SDimitry Andric // traversing the function in depth-first order to rewrite loads and stores as
130b57cec5SDimitry Andric // appropriate.
140b57cec5SDimitry Andric //
150b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
160b57cec5SDimitry Andric 
170b57cec5SDimitry Andric #include "llvm/ADT/ArrayRef.h"
180b57cec5SDimitry Andric #include "llvm/ADT/DenseMap.h"
190b57cec5SDimitry Andric #include "llvm/ADT/STLExtras.h"
200b57cec5SDimitry Andric #include "llvm/ADT/SmallPtrSet.h"
210b57cec5SDimitry Andric #include "llvm/ADT/SmallVector.h"
220b57cec5SDimitry Andric #include "llvm/ADT/Statistic.h"
230b57cec5SDimitry Andric #include "llvm/ADT/Twine.h"
240b57cec5SDimitry Andric #include "llvm/Analysis/AssumptionCache.h"
250b57cec5SDimitry Andric #include "llvm/Analysis/InstructionSimplify.h"
260b57cec5SDimitry Andric #include "llvm/Analysis/IteratedDominanceFrontier.h"
270b57cec5SDimitry Andric #include "llvm/Analysis/ValueTracking.h"
280b57cec5SDimitry Andric #include "llvm/IR/BasicBlock.h"
290b57cec5SDimitry Andric #include "llvm/IR/CFG.h"
300b57cec5SDimitry Andric #include "llvm/IR/Constant.h"
310b57cec5SDimitry Andric #include "llvm/IR/Constants.h"
320b57cec5SDimitry Andric #include "llvm/IR/DIBuilder.h"
331fd87a68SDimitry Andric #include "llvm/IR/DebugInfo.h"
345f757f3fSDimitry Andric #include "llvm/IR/DebugProgramInstruction.h"
350b57cec5SDimitry Andric #include "llvm/IR/Dominators.h"
360b57cec5SDimitry Andric #include "llvm/IR/Function.h"
370b57cec5SDimitry Andric #include "llvm/IR/InstrTypes.h"
380b57cec5SDimitry Andric #include "llvm/IR/Instruction.h"
390b57cec5SDimitry Andric #include "llvm/IR/Instructions.h"
400b57cec5SDimitry Andric #include "llvm/IR/IntrinsicInst.h"
410b57cec5SDimitry Andric #include "llvm/IR/Intrinsics.h"
420b57cec5SDimitry Andric #include "llvm/IR/LLVMContext.h"
430b57cec5SDimitry Andric #include "llvm/IR/Module.h"
44*0fca6ea1SDimitry Andric #include "llvm/IR/Operator.h"
450b57cec5SDimitry Andric #include "llvm/IR/Type.h"
460b57cec5SDimitry Andric #include "llvm/IR/User.h"
470b57cec5SDimitry Andric #include "llvm/Support/Casting.h"
481fd87a68SDimitry Andric #include "llvm/Transforms/Utils/Local.h"
490b57cec5SDimitry Andric #include "llvm/Transforms/Utils/PromoteMemToReg.h"
500b57cec5SDimitry Andric #include <algorithm>
510b57cec5SDimitry Andric #include <cassert>
520b57cec5SDimitry Andric #include <iterator>
530b57cec5SDimitry Andric #include <utility>
540b57cec5SDimitry Andric #include <vector>
550b57cec5SDimitry Andric 
560b57cec5SDimitry Andric using namespace llvm;
570b57cec5SDimitry Andric 
580b57cec5SDimitry Andric #define DEBUG_TYPE "mem2reg"
590b57cec5SDimitry Andric 
600b57cec5SDimitry Andric STATISTIC(NumLocalPromoted, "Number of alloca's promoted within one block");
610b57cec5SDimitry Andric STATISTIC(NumSingleStore,   "Number of alloca's promoted with a single store");
620b57cec5SDimitry Andric STATISTIC(NumDeadAlloca,    "Number of dead alloca's removed");
630b57cec5SDimitry Andric STATISTIC(NumPHIInsert,     "Number of PHI nodes inserted");
640b57cec5SDimitry Andric 
650b57cec5SDimitry Andric bool llvm::isAllocaPromotable(const AllocaInst *AI) {
660b57cec5SDimitry Andric   // Only allow direct and non-volatile loads and stores...
670b57cec5SDimitry Andric   for (const User *U : AI->users()) {
680b57cec5SDimitry Andric     if (const LoadInst *LI = dyn_cast<LoadInst>(U)) {
690b57cec5SDimitry Andric       // Note that atomic loads can be transformed; atomic semantics do
700b57cec5SDimitry Andric       // not have any meaning for a local alloca.
7181ad6265SDimitry Andric       if (LI->isVolatile() || LI->getType() != AI->getAllocatedType())
720b57cec5SDimitry Andric         return false;
730b57cec5SDimitry Andric     } else if (const StoreInst *SI = dyn_cast<StoreInst>(U)) {
74349cc55cSDimitry Andric       if (SI->getValueOperand() == AI ||
75349cc55cSDimitry Andric           SI->getValueOperand()->getType() != AI->getAllocatedType())
760b57cec5SDimitry Andric         return false; // Don't allow a store OF the AI, only INTO the AI.
770b57cec5SDimitry Andric       // Note that atomic stores can be transformed; atomic semantics do
780b57cec5SDimitry Andric       // not have any meaning for a local alloca.
790b57cec5SDimitry Andric       if (SI->isVolatile())
800b57cec5SDimitry Andric         return false;
810b57cec5SDimitry Andric     } else if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(U)) {
82e8d8bef9SDimitry Andric       if (!II->isLifetimeStartOrEnd() && !II->isDroppable())
830b57cec5SDimitry Andric         return false;
840b57cec5SDimitry Andric     } else if (const BitCastInst *BCI = dyn_cast<BitCastInst>(U)) {
85e8d8bef9SDimitry Andric       if (!onlyUsedByLifetimeMarkersOrDroppableInsts(BCI))
860b57cec5SDimitry Andric         return false;
870b57cec5SDimitry Andric     } else if (const GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(U)) {
880b57cec5SDimitry Andric       if (!GEPI->hasAllZeroIndices())
890b57cec5SDimitry Andric         return false;
90e8d8bef9SDimitry Andric       if (!onlyUsedByLifetimeMarkersOrDroppableInsts(GEPI))
91e8d8bef9SDimitry Andric         return false;
92e8d8bef9SDimitry Andric     } else if (const AddrSpaceCastInst *ASCI = dyn_cast<AddrSpaceCastInst>(U)) {
93e8d8bef9SDimitry Andric       if (!onlyUsedByLifetimeMarkers(ASCI))
940b57cec5SDimitry Andric         return false;
950b57cec5SDimitry Andric     } else {
960b57cec5SDimitry Andric       return false;
970b57cec5SDimitry Andric     }
980b57cec5SDimitry Andric   }
990b57cec5SDimitry Andric 
1000b57cec5SDimitry Andric   return true;
1010b57cec5SDimitry Andric }
1020b57cec5SDimitry Andric 
1030b57cec5SDimitry Andric namespace {
1040b57cec5SDimitry Andric 
105*0fca6ea1SDimitry Andric static void createDebugValue(DIBuilder &DIB, Value *NewValue,
1067a6dacacSDimitry Andric                              DILocalVariable *Variable,
1077a6dacacSDimitry Andric                              DIExpression *Expression, const DILocation *DI,
108*0fca6ea1SDimitry Andric                              DbgVariableRecord *InsertBefore) {
109*0fca6ea1SDimitry Andric   // FIXME: Merge these two functions now that DIBuilder supports
110*0fca6ea1SDimitry Andric   // DbgVariableRecords. We neeed the API to accept DbgVariableRecords as an
111*0fca6ea1SDimitry Andric   // insert point for that to work.
1127a6dacacSDimitry Andric   (void)DIB;
113*0fca6ea1SDimitry Andric   DbgVariableRecord::createDbgVariableRecord(NewValue, Variable, Expression, DI,
1147a6dacacSDimitry Andric                                              *InsertBefore);
1157a6dacacSDimitry Andric }
116*0fca6ea1SDimitry Andric static void createDebugValue(DIBuilder &DIB, Value *NewValue,
1177a6dacacSDimitry Andric                              DILocalVariable *Variable,
118*0fca6ea1SDimitry Andric                              DIExpression *Expression, const DILocation *DI,
1197a6dacacSDimitry Andric                              Instruction *InsertBefore) {
120*0fca6ea1SDimitry Andric   DIB.insertDbgValueIntrinsic(NewValue, Variable, Expression, DI, InsertBefore);
1217a6dacacSDimitry Andric }
1227a6dacacSDimitry Andric 
123bdd1243dSDimitry Andric /// Helper for updating assignment tracking debug info when promoting allocas.
124bdd1243dSDimitry Andric class AssignmentTrackingInfo {
125bdd1243dSDimitry Andric   /// DbgAssignIntrinsics linked to the alloca with at most one per variable
126bdd1243dSDimitry Andric   /// fragment. (i.e. not be a comprehensive set if there are multiple
127bdd1243dSDimitry Andric   /// dbg.assigns for one variable fragment).
128bdd1243dSDimitry Andric   SmallVector<DbgVariableIntrinsic *> DbgAssigns;
129*0fca6ea1SDimitry Andric   SmallVector<DbgVariableRecord *> DVRAssigns;
130bdd1243dSDimitry Andric 
131bdd1243dSDimitry Andric public:
132bdd1243dSDimitry Andric   void init(AllocaInst *AI) {
133bdd1243dSDimitry Andric     SmallSet<DebugVariable, 2> Vars;
134bdd1243dSDimitry Andric     for (DbgAssignIntrinsic *DAI : at::getAssignmentMarkers(AI)) {
135bdd1243dSDimitry Andric       if (Vars.insert(DebugVariable(DAI)).second)
136bdd1243dSDimitry Andric         DbgAssigns.push_back(DAI);
137bdd1243dSDimitry Andric     }
138*0fca6ea1SDimitry Andric     for (DbgVariableRecord *DVR : at::getDVRAssignmentMarkers(AI)) {
139*0fca6ea1SDimitry Andric       if (Vars.insert(DebugVariable(DVR)).second)
140*0fca6ea1SDimitry Andric         DVRAssigns.push_back(DVR);
1417a6dacacSDimitry Andric     }
142bdd1243dSDimitry Andric   }
143bdd1243dSDimitry Andric 
144bdd1243dSDimitry Andric   /// Update assignment tracking debug info given for the to-be-deleted store
145bdd1243dSDimitry Andric   /// \p ToDelete that stores to this alloca.
146*0fca6ea1SDimitry Andric   void updateForDeletedStore(
147*0fca6ea1SDimitry Andric       StoreInst *ToDelete, DIBuilder &DIB,
1487a6dacacSDimitry Andric       SmallSet<DbgAssignIntrinsic *, 8> *DbgAssignsToDelete,
149*0fca6ea1SDimitry Andric       SmallSet<DbgVariableRecord *, 8> *DVRAssignsToDelete) const {
150bdd1243dSDimitry Andric     // There's nothing to do if the alloca doesn't have any variables using
151bdd1243dSDimitry Andric     // assignment tracking.
152*0fca6ea1SDimitry Andric     if (DbgAssigns.empty() && DVRAssigns.empty())
153bdd1243dSDimitry Andric       return;
154bdd1243dSDimitry Andric 
15506c3fb27SDimitry Andric     // Insert a dbg.value where the linked dbg.assign is and remember to delete
15606c3fb27SDimitry Andric     // the dbg.assign later. Demoting to dbg.value isn't necessary for
15706c3fb27SDimitry Andric     // correctness but does reduce compile time and memory usage by reducing
15806c3fb27SDimitry Andric     // unnecessary function-local metadata. Remember that we've seen a
15906c3fb27SDimitry Andric     // dbg.assign for each variable fragment for the untracked store handling
16006c3fb27SDimitry Andric     // (after this loop).
16106c3fb27SDimitry Andric     SmallSet<DebugVariableAggregate, 2> VarHasDbgAssignForStore;
1627a6dacacSDimitry Andric     auto InsertValueForAssign = [&](auto *DbgAssign, auto *&AssignList) {
1637a6dacacSDimitry Andric       VarHasDbgAssignForStore.insert(DebugVariableAggregate(DbgAssign));
1647a6dacacSDimitry Andric       AssignList->insert(DbgAssign);
1657a6dacacSDimitry Andric       createDebugValue(DIB, DbgAssign->getValue(), DbgAssign->getVariable(),
1667a6dacacSDimitry Andric                        DbgAssign->getExpression(), DbgAssign->getDebugLoc(),
1677a6dacacSDimitry Andric                        DbgAssign);
1687a6dacacSDimitry Andric     };
1697a6dacacSDimitry Andric     for (auto *Assign : at::getAssignmentMarkers(ToDelete))
1707a6dacacSDimitry Andric       InsertValueForAssign(Assign, DbgAssignsToDelete);
171*0fca6ea1SDimitry Andric     for (auto *Assign : at::getDVRAssignmentMarkers(ToDelete))
172*0fca6ea1SDimitry Andric       InsertValueForAssign(Assign, DVRAssignsToDelete);
173bdd1243dSDimitry Andric 
174bdd1243dSDimitry Andric     // It's possible for variables using assignment tracking to have no
175bdd1243dSDimitry Andric     // dbg.assign linked to this store. These are variables in DbgAssigns that
176bdd1243dSDimitry Andric     // are missing from VarHasDbgAssignForStore. Since there isn't a dbg.assign
177bdd1243dSDimitry Andric     // to mark the assignment - and the store is going to be deleted - insert a
178bdd1243dSDimitry Andric     // dbg.value to do that now. An untracked store may be either one that
179bdd1243dSDimitry Andric     // cannot be represented using assignment tracking (non-const offset or
180bdd1243dSDimitry Andric     // size) or one that is trackable but has had its DIAssignID attachment
181bdd1243dSDimitry Andric     // dropped accidentally.
1827a6dacacSDimitry Andric     auto ConvertUnlinkedAssignToValue = [&](auto *Assign) {
1837a6dacacSDimitry Andric       if (VarHasDbgAssignForStore.contains(DebugVariableAggregate(Assign)))
1847a6dacacSDimitry Andric         return;
1857a6dacacSDimitry Andric       ConvertDebugDeclareToDebugValue(Assign, ToDelete, DIB);
1867a6dacacSDimitry Andric     };
1877a6dacacSDimitry Andric     for_each(DbgAssigns, ConvertUnlinkedAssignToValue);
188*0fca6ea1SDimitry Andric     for_each(DVRAssigns, ConvertUnlinkedAssignToValue);
189bdd1243dSDimitry Andric   }
190bdd1243dSDimitry Andric 
191bdd1243dSDimitry Andric   /// Update assignment tracking debug info given for the newly inserted PHI \p
192bdd1243dSDimitry Andric   /// NewPhi.
193bdd1243dSDimitry Andric   void updateForNewPhi(PHINode *NewPhi, DIBuilder &DIB) const {
194bdd1243dSDimitry Andric     // Regardless of the position of dbg.assigns relative to stores, the
195bdd1243dSDimitry Andric     // incoming values into a new PHI should be the same for the (imaginary)
196bdd1243dSDimitry Andric     // debug-phi.
197bdd1243dSDimitry Andric     for (auto *DAI : DbgAssigns)
198bdd1243dSDimitry Andric       ConvertDebugDeclareToDebugValue(DAI, NewPhi, DIB);
199*0fca6ea1SDimitry Andric     for (auto *DVR : DVRAssigns)
200*0fca6ea1SDimitry Andric       ConvertDebugDeclareToDebugValue(DVR, NewPhi, DIB);
201bdd1243dSDimitry Andric   }
202bdd1243dSDimitry Andric 
2037a6dacacSDimitry Andric   void clear() {
2047a6dacacSDimitry Andric     DbgAssigns.clear();
205*0fca6ea1SDimitry Andric     DVRAssigns.clear();
2067a6dacacSDimitry Andric   }
207*0fca6ea1SDimitry Andric   bool empty() { return DbgAssigns.empty() && DVRAssigns.empty(); }
208bdd1243dSDimitry Andric };
209bdd1243dSDimitry Andric 
2100b57cec5SDimitry Andric struct AllocaInfo {
211e8d8bef9SDimitry Andric   using DbgUserVec = SmallVector<DbgVariableIntrinsic *, 1>;
212*0fca6ea1SDimitry Andric   using DPUserVec = SmallVector<DbgVariableRecord *, 1>;
213e8d8bef9SDimitry Andric 
2140b57cec5SDimitry Andric   SmallVector<BasicBlock *, 32> DefiningBlocks;
2150b57cec5SDimitry Andric   SmallVector<BasicBlock *, 32> UsingBlocks;
2160b57cec5SDimitry Andric 
2170b57cec5SDimitry Andric   StoreInst *OnlyStore;
2180b57cec5SDimitry Andric   BasicBlock *OnlyBlock;
2190b57cec5SDimitry Andric   bool OnlyUsedInOneBlock;
2200b57cec5SDimitry Andric 
221bdd1243dSDimitry Andric   /// Debug users of the alloca - does not include dbg.assign intrinsics.
222e8d8bef9SDimitry Andric   DbgUserVec DbgUsers;
2235f757f3fSDimitry Andric   DPUserVec DPUsers;
224bdd1243dSDimitry Andric   /// Helper to update assignment tracking debug info.
225bdd1243dSDimitry Andric   AssignmentTrackingInfo AssignmentTracking;
2260b57cec5SDimitry Andric 
2270b57cec5SDimitry Andric   void clear() {
2280b57cec5SDimitry Andric     DefiningBlocks.clear();
2290b57cec5SDimitry Andric     UsingBlocks.clear();
2300b57cec5SDimitry Andric     OnlyStore = nullptr;
2310b57cec5SDimitry Andric     OnlyBlock = nullptr;
2320b57cec5SDimitry Andric     OnlyUsedInOneBlock = true;
233e8d8bef9SDimitry Andric     DbgUsers.clear();
2345f757f3fSDimitry Andric     DPUsers.clear();
235bdd1243dSDimitry Andric     AssignmentTracking.clear();
2360b57cec5SDimitry Andric   }
2370b57cec5SDimitry Andric 
2380b57cec5SDimitry Andric   /// Scan the uses of the specified alloca, filling in the AllocaInfo used
2390b57cec5SDimitry Andric   /// by the rest of the pass to reason about the uses of this alloca.
2400b57cec5SDimitry Andric   void AnalyzeAlloca(AllocaInst *AI) {
2410b57cec5SDimitry Andric     clear();
2420b57cec5SDimitry Andric 
2430b57cec5SDimitry Andric     // As we scan the uses of the alloca instruction, keep track of stores,
2440b57cec5SDimitry Andric     // and decide whether all of the loads and stores to the alloca are within
2450b57cec5SDimitry Andric     // the same basic block.
246e8d8bef9SDimitry Andric     for (User *U : AI->users()) {
247e8d8bef9SDimitry Andric       Instruction *User = cast<Instruction>(U);
2480b57cec5SDimitry Andric 
2490b57cec5SDimitry Andric       if (StoreInst *SI = dyn_cast<StoreInst>(User)) {
2500b57cec5SDimitry Andric         // Remember the basic blocks which define new values for the alloca
2510b57cec5SDimitry Andric         DefiningBlocks.push_back(SI->getParent());
2520b57cec5SDimitry Andric         OnlyStore = SI;
2530b57cec5SDimitry Andric       } else {
2540b57cec5SDimitry Andric         LoadInst *LI = cast<LoadInst>(User);
2550b57cec5SDimitry Andric         // Otherwise it must be a load instruction, keep track of variable
2560b57cec5SDimitry Andric         // reads.
2570b57cec5SDimitry Andric         UsingBlocks.push_back(LI->getParent());
2580b57cec5SDimitry Andric       }
2590b57cec5SDimitry Andric 
2600b57cec5SDimitry Andric       if (OnlyUsedInOneBlock) {
2610b57cec5SDimitry Andric         if (!OnlyBlock)
2620b57cec5SDimitry Andric           OnlyBlock = User->getParent();
2630b57cec5SDimitry Andric         else if (OnlyBlock != User->getParent())
2640b57cec5SDimitry Andric           OnlyUsedInOneBlock = false;
2650b57cec5SDimitry Andric       }
2660b57cec5SDimitry Andric     }
267bdd1243dSDimitry Andric     DbgUserVec AllDbgUsers;
268*0fca6ea1SDimitry Andric     SmallVector<DbgVariableRecord *> AllDPUsers;
2697a6dacacSDimitry Andric     findDbgUsers(AllDbgUsers, AI, &AllDPUsers);
270bdd1243dSDimitry Andric     std::copy_if(AllDbgUsers.begin(), AllDbgUsers.end(),
271bdd1243dSDimitry Andric                  std::back_inserter(DbgUsers), [](DbgVariableIntrinsic *DII) {
272bdd1243dSDimitry Andric                    return !isa<DbgAssignIntrinsic>(DII);
273bdd1243dSDimitry Andric                  });
2747a6dacacSDimitry Andric     std::copy_if(AllDPUsers.begin(), AllDPUsers.end(),
2757a6dacacSDimitry Andric                  std::back_inserter(DPUsers),
276*0fca6ea1SDimitry Andric                  [](DbgVariableRecord *DVR) { return !DVR->isDbgAssign(); });
277bdd1243dSDimitry Andric     AssignmentTracking.init(AI);
2780b57cec5SDimitry Andric   }
2790b57cec5SDimitry Andric };
2800b57cec5SDimitry Andric 
2810b57cec5SDimitry Andric /// Data package used by RenamePass().
2820b57cec5SDimitry Andric struct RenamePassData {
2830b57cec5SDimitry Andric   using ValVector = std::vector<Value *>;
2840b57cec5SDimitry Andric   using LocationVector = std::vector<DebugLoc>;
2850b57cec5SDimitry Andric 
2860b57cec5SDimitry Andric   RenamePassData(BasicBlock *B, BasicBlock *P, ValVector V, LocationVector L)
2870b57cec5SDimitry Andric       : BB(B), Pred(P), Values(std::move(V)), Locations(std::move(L)) {}
2880b57cec5SDimitry Andric 
2890b57cec5SDimitry Andric   BasicBlock *BB;
2900b57cec5SDimitry Andric   BasicBlock *Pred;
2910b57cec5SDimitry Andric   ValVector Values;
2920b57cec5SDimitry Andric   LocationVector Locations;
2930b57cec5SDimitry Andric };
2940b57cec5SDimitry Andric 
2950b57cec5SDimitry Andric /// This assigns and keeps a per-bb relative ordering of load/store
2960b57cec5SDimitry Andric /// instructions in the block that directly load or store an alloca.
2970b57cec5SDimitry Andric ///
2980b57cec5SDimitry Andric /// This functionality is important because it avoids scanning large basic
2990b57cec5SDimitry Andric /// blocks multiple times when promoting many allocas in the same block.
3000b57cec5SDimitry Andric class LargeBlockInfo {
3010b57cec5SDimitry Andric   /// For each instruction that we track, keep the index of the
3020b57cec5SDimitry Andric   /// instruction.
3030b57cec5SDimitry Andric   ///
3040b57cec5SDimitry Andric   /// The index starts out as the number of the instruction from the start of
3050b57cec5SDimitry Andric   /// the block.
3060b57cec5SDimitry Andric   DenseMap<const Instruction *, unsigned> InstNumbers;
3070b57cec5SDimitry Andric 
3080b57cec5SDimitry Andric public:
3090b57cec5SDimitry Andric 
3100b57cec5SDimitry Andric   /// This code only looks at accesses to allocas.
3110b57cec5SDimitry Andric   static bool isInterestingInstruction(const Instruction *I) {
3120b57cec5SDimitry Andric     return (isa<LoadInst>(I) && isa<AllocaInst>(I->getOperand(0))) ||
3130b57cec5SDimitry Andric            (isa<StoreInst>(I) && isa<AllocaInst>(I->getOperand(1)));
3140b57cec5SDimitry Andric   }
3150b57cec5SDimitry Andric 
3160b57cec5SDimitry Andric   /// Get or calculate the index of the specified instruction.
3170b57cec5SDimitry Andric   unsigned getInstructionIndex(const Instruction *I) {
3180b57cec5SDimitry Andric     assert(isInterestingInstruction(I) &&
3190b57cec5SDimitry Andric            "Not a load/store to/from an alloca?");
3200b57cec5SDimitry Andric 
3210b57cec5SDimitry Andric     // If we already have this instruction number, return it.
3220b57cec5SDimitry Andric     DenseMap<const Instruction *, unsigned>::iterator It = InstNumbers.find(I);
3230b57cec5SDimitry Andric     if (It != InstNumbers.end())
3240b57cec5SDimitry Andric       return It->second;
3250b57cec5SDimitry Andric 
3260b57cec5SDimitry Andric     // Scan the whole block to get the instruction.  This accumulates
3270b57cec5SDimitry Andric     // information for every interesting instruction in the block, in order to
3280b57cec5SDimitry Andric     // avoid gratuitus rescans.
3290b57cec5SDimitry Andric     const BasicBlock *BB = I->getParent();
3300b57cec5SDimitry Andric     unsigned InstNo = 0;
3310b57cec5SDimitry Andric     for (const Instruction &BBI : *BB)
3320b57cec5SDimitry Andric       if (isInterestingInstruction(&BBI))
3330b57cec5SDimitry Andric         InstNumbers[&BBI] = InstNo++;
3340b57cec5SDimitry Andric     It = InstNumbers.find(I);
3350b57cec5SDimitry Andric 
3360b57cec5SDimitry Andric     assert(It != InstNumbers.end() && "Didn't insert instruction?");
3370b57cec5SDimitry Andric     return It->second;
3380b57cec5SDimitry Andric   }
3390b57cec5SDimitry Andric 
3400b57cec5SDimitry Andric   void deleteValue(const Instruction *I) { InstNumbers.erase(I); }
3410b57cec5SDimitry Andric 
3420b57cec5SDimitry Andric   void clear() { InstNumbers.clear(); }
3430b57cec5SDimitry Andric };
3440b57cec5SDimitry Andric 
3450b57cec5SDimitry Andric struct PromoteMem2Reg {
3460b57cec5SDimitry Andric   /// The alloca instructions being promoted.
3470b57cec5SDimitry Andric   std::vector<AllocaInst *> Allocas;
3480b57cec5SDimitry Andric 
3490b57cec5SDimitry Andric   DominatorTree &DT;
3500b57cec5SDimitry Andric   DIBuilder DIB;
3510b57cec5SDimitry Andric 
3520b57cec5SDimitry Andric   /// A cache of @llvm.assume intrinsics used by SimplifyInstruction.
3530b57cec5SDimitry Andric   AssumptionCache *AC;
3540b57cec5SDimitry Andric 
3550b57cec5SDimitry Andric   const SimplifyQuery SQ;
3560b57cec5SDimitry Andric 
3570b57cec5SDimitry Andric   /// Reverse mapping of Allocas.
3580b57cec5SDimitry Andric   DenseMap<AllocaInst *, unsigned> AllocaLookup;
3590b57cec5SDimitry Andric 
3600b57cec5SDimitry Andric   /// The PhiNodes we're adding.
3610b57cec5SDimitry Andric   ///
3620b57cec5SDimitry Andric   /// That map is used to simplify some Phi nodes as we iterate over it, so
3630b57cec5SDimitry Andric   /// it should have deterministic iterators.  We could use a MapVector, but
3640b57cec5SDimitry Andric   /// since we already maintain a map from BasicBlock* to a stable numbering
3650b57cec5SDimitry Andric   /// (BBNumbers), the DenseMap is more efficient (also supports removal).
3660b57cec5SDimitry Andric   DenseMap<std::pair<unsigned, unsigned>, PHINode *> NewPhiNodes;
3670b57cec5SDimitry Andric 
3680b57cec5SDimitry Andric   /// For each PHI node, keep track of which entry in Allocas it corresponds
3690b57cec5SDimitry Andric   /// to.
3700b57cec5SDimitry Andric   DenseMap<PHINode *, unsigned> PhiToAllocaMap;
3710b57cec5SDimitry Andric 
3720b57cec5SDimitry Andric   /// For each alloca, we keep track of the dbg.declare intrinsic that
3730b57cec5SDimitry Andric   /// describes it, if any, so that we can convert it to a dbg.value
3740b57cec5SDimitry Andric   /// intrinsic if the alloca gets promoted.
375e8d8bef9SDimitry Andric   SmallVector<AllocaInfo::DbgUserVec, 8> AllocaDbgUsers;
3765f757f3fSDimitry Andric   SmallVector<AllocaInfo::DPUserVec, 8> AllocaDPUsers;
3770b57cec5SDimitry Andric 
378bdd1243dSDimitry Andric   /// For each alloca, keep an instance of a helper class that gives us an easy
379bdd1243dSDimitry Andric   /// way to update assignment tracking debug info if the alloca is promoted.
380bdd1243dSDimitry Andric   SmallVector<AssignmentTrackingInfo, 8> AllocaATInfo;
38106c3fb27SDimitry Andric   /// A set of dbg.assigns to delete because they've been demoted to
38206c3fb27SDimitry Andric   /// dbg.values. Call cleanUpDbgAssigns to delete them.
38306c3fb27SDimitry Andric   SmallSet<DbgAssignIntrinsic *, 8> DbgAssignsToDelete;
384*0fca6ea1SDimitry Andric   SmallSet<DbgVariableRecord *, 8> DVRAssignsToDelete;
385bdd1243dSDimitry Andric 
3860b57cec5SDimitry Andric   /// The set of basic blocks the renamer has already visited.
3870b57cec5SDimitry Andric   SmallPtrSet<BasicBlock *, 16> Visited;
3880b57cec5SDimitry Andric 
3890b57cec5SDimitry Andric   /// Contains a stable numbering of basic blocks to avoid non-determinstic
3900b57cec5SDimitry Andric   /// behavior.
3910b57cec5SDimitry Andric   DenseMap<BasicBlock *, unsigned> BBNumbers;
3920b57cec5SDimitry Andric 
3930b57cec5SDimitry Andric   /// Lazily compute the number of predecessors a block has.
3940b57cec5SDimitry Andric   DenseMap<const BasicBlock *, unsigned> BBNumPreds;
3950b57cec5SDimitry Andric 
396*0fca6ea1SDimitry Andric   /// Whether the function has the no-signed-zeros-fp-math attribute set.
397*0fca6ea1SDimitry Andric   bool NoSignedZeros = false;
398*0fca6ea1SDimitry Andric 
3990b57cec5SDimitry Andric public:
4000b57cec5SDimitry Andric   PromoteMem2Reg(ArrayRef<AllocaInst *> Allocas, DominatorTree &DT,
4010b57cec5SDimitry Andric                  AssumptionCache *AC)
4020b57cec5SDimitry Andric       : Allocas(Allocas.begin(), Allocas.end()), DT(DT),
4030b57cec5SDimitry Andric         DIB(*DT.getRoot()->getParent()->getParent(), /*AllowUnresolved*/ false),
404*0fca6ea1SDimitry Andric         AC(AC), SQ(DT.getRoot()->getDataLayout(),
4050b57cec5SDimitry Andric                    nullptr, &DT, AC) {}
4060b57cec5SDimitry Andric 
4070b57cec5SDimitry Andric   void run();
4080b57cec5SDimitry Andric 
4090b57cec5SDimitry Andric private:
4100b57cec5SDimitry Andric   void RemoveFromAllocasList(unsigned &AllocaIdx) {
4110b57cec5SDimitry Andric     Allocas[AllocaIdx] = Allocas.back();
4120b57cec5SDimitry Andric     Allocas.pop_back();
4130b57cec5SDimitry Andric     --AllocaIdx;
4140b57cec5SDimitry Andric   }
4150b57cec5SDimitry Andric 
4160b57cec5SDimitry Andric   unsigned getNumPreds(const BasicBlock *BB) {
4170b57cec5SDimitry Andric     unsigned &NP = BBNumPreds[BB];
4180b57cec5SDimitry Andric     if (NP == 0)
4190b57cec5SDimitry Andric       NP = pred_size(BB) + 1;
4200b57cec5SDimitry Andric     return NP - 1;
4210b57cec5SDimitry Andric   }
4220b57cec5SDimitry Andric 
4230b57cec5SDimitry Andric   void ComputeLiveInBlocks(AllocaInst *AI, AllocaInfo &Info,
4240b57cec5SDimitry Andric                            const SmallPtrSetImpl<BasicBlock *> &DefBlocks,
4250b57cec5SDimitry Andric                            SmallPtrSetImpl<BasicBlock *> &LiveInBlocks);
4260b57cec5SDimitry Andric   void RenamePass(BasicBlock *BB, BasicBlock *Pred,
4270b57cec5SDimitry Andric                   RenamePassData::ValVector &IncVals,
4280b57cec5SDimitry Andric                   RenamePassData::LocationVector &IncLocs,
4290b57cec5SDimitry Andric                   std::vector<RenamePassData> &Worklist);
4300b57cec5SDimitry Andric   bool QueuePhiNode(BasicBlock *BB, unsigned AllocaIdx, unsigned &Version);
43106c3fb27SDimitry Andric 
43206c3fb27SDimitry Andric   /// Delete dbg.assigns that have been demoted to dbg.values.
43306c3fb27SDimitry Andric   void cleanUpDbgAssigns() {
43406c3fb27SDimitry Andric     for (auto *DAI : DbgAssignsToDelete)
43506c3fb27SDimitry Andric       DAI->eraseFromParent();
43606c3fb27SDimitry Andric     DbgAssignsToDelete.clear();
437*0fca6ea1SDimitry Andric     for (auto *DVR : DVRAssignsToDelete)
438*0fca6ea1SDimitry Andric       DVR->eraseFromParent();
439*0fca6ea1SDimitry Andric     DVRAssignsToDelete.clear();
44006c3fb27SDimitry Andric   }
4410b57cec5SDimitry Andric };
4420b57cec5SDimitry Andric 
4430b57cec5SDimitry Andric } // end anonymous namespace
4440b57cec5SDimitry Andric 
4450b57cec5SDimitry Andric /// Given a LoadInst LI this adds assume(LI != null) after it.
4460b57cec5SDimitry Andric static void addAssumeNonNull(AssumptionCache *AC, LoadInst *LI) {
4470b57cec5SDimitry Andric   Function *AssumeIntrinsic =
4480b57cec5SDimitry Andric       Intrinsic::getDeclaration(LI->getModule(), Intrinsic::assume);
4490b57cec5SDimitry Andric   ICmpInst *LoadNotNull = new ICmpInst(ICmpInst::ICMP_NE, LI,
4500b57cec5SDimitry Andric                                        Constant::getNullValue(LI->getType()));
4510b57cec5SDimitry Andric   LoadNotNull->insertAfter(LI);
4520b57cec5SDimitry Andric   CallInst *CI = CallInst::Create(AssumeIntrinsic, {LoadNotNull});
4530b57cec5SDimitry Andric   CI->insertAfter(LoadNotNull);
454fe6060f1SDimitry Andric   AC->registerAssumption(cast<AssumeInst>(CI));
4550b57cec5SDimitry Andric }
4560b57cec5SDimitry Andric 
457bdd1243dSDimitry Andric static void convertMetadataToAssumes(LoadInst *LI, Value *Val,
458bdd1243dSDimitry Andric                                      const DataLayout &DL, AssumptionCache *AC,
459bdd1243dSDimitry Andric                                      const DominatorTree *DT) {
460*0fca6ea1SDimitry Andric   if (isa<UndefValue>(Val) && LI->hasMetadata(LLVMContext::MD_noundef)) {
461*0fca6ea1SDimitry Andric     // Insert non-terminator unreachable.
462*0fca6ea1SDimitry Andric     LLVMContext &Ctx = LI->getContext();
463*0fca6ea1SDimitry Andric     new StoreInst(ConstantInt::getTrue(Ctx),
464*0fca6ea1SDimitry Andric                   PoisonValue::get(PointerType::getUnqual(Ctx)),
465*0fca6ea1SDimitry Andric                   /*isVolatile=*/false, Align(1), LI);
466*0fca6ea1SDimitry Andric     return;
467*0fca6ea1SDimitry Andric   }
468*0fca6ea1SDimitry Andric 
469bdd1243dSDimitry Andric   // If the load was marked as nonnull we don't want to lose that information
470bdd1243dSDimitry Andric   // when we erase this Load. So we preserve it with an assume. As !nonnull
471bdd1243dSDimitry Andric   // returns poison while assume violations are immediate undefined behavior,
472bdd1243dSDimitry Andric   // we can only do this if the value is known non-poison.
473bdd1243dSDimitry Andric   if (AC && LI->getMetadata(LLVMContext::MD_nonnull) &&
474bdd1243dSDimitry Andric       LI->getMetadata(LLVMContext::MD_noundef) &&
475*0fca6ea1SDimitry Andric       !isKnownNonZero(Val, SimplifyQuery(DL, DT, AC, LI)))
476bdd1243dSDimitry Andric     addAssumeNonNull(AC, LI);
477bdd1243dSDimitry Andric }
478bdd1243dSDimitry Andric 
479e8d8bef9SDimitry Andric static void removeIntrinsicUsers(AllocaInst *AI) {
4800b57cec5SDimitry Andric   // Knowing that this alloca is promotable, we know that it's safe to kill all
4810b57cec5SDimitry Andric   // instructions except for load and store.
4820b57cec5SDimitry Andric 
483fe6060f1SDimitry Andric   for (Use &U : llvm::make_early_inc_range(AI->uses())) {
484fe6060f1SDimitry Andric     Instruction *I = cast<Instruction>(U.getUser());
4850b57cec5SDimitry Andric     if (isa<LoadInst>(I) || isa<StoreInst>(I))
4860b57cec5SDimitry Andric       continue;
4870b57cec5SDimitry Andric 
488e8d8bef9SDimitry Andric     // Drop the use of AI in droppable instructions.
489e8d8bef9SDimitry Andric     if (I->isDroppable()) {
490e8d8bef9SDimitry Andric       I->dropDroppableUse(U);
491e8d8bef9SDimitry Andric       continue;
492e8d8bef9SDimitry Andric     }
493e8d8bef9SDimitry Andric 
4940b57cec5SDimitry Andric     if (!I->getType()->isVoidTy()) {
4950b57cec5SDimitry Andric       // The only users of this bitcast/GEP instruction are lifetime intrinsics.
4960b57cec5SDimitry Andric       // Follow the use/def chain to erase them now instead of leaving it for
4970b57cec5SDimitry Andric       // dead code elimination later.
498fe6060f1SDimitry Andric       for (Use &UU : llvm::make_early_inc_range(I->uses())) {
499fe6060f1SDimitry Andric         Instruction *Inst = cast<Instruction>(UU.getUser());
500e8d8bef9SDimitry Andric 
501e8d8bef9SDimitry Andric         // Drop the use of I in droppable instructions.
502e8d8bef9SDimitry Andric         if (Inst->isDroppable()) {
503e8d8bef9SDimitry Andric           Inst->dropDroppableUse(UU);
504e8d8bef9SDimitry Andric           continue;
505e8d8bef9SDimitry Andric         }
5060b57cec5SDimitry Andric         Inst->eraseFromParent();
5070b57cec5SDimitry Andric       }
5080b57cec5SDimitry Andric     }
5090b57cec5SDimitry Andric     I->eraseFromParent();
5100b57cec5SDimitry Andric   }
5110b57cec5SDimitry Andric }
5120b57cec5SDimitry Andric 
5130b57cec5SDimitry Andric /// Rewrite as many loads as possible given a single store.
5140b57cec5SDimitry Andric ///
5150b57cec5SDimitry Andric /// When there is only a single store, we can use the domtree to trivially
5160b57cec5SDimitry Andric /// replace all of the dominated loads with the stored value. Do so, and return
5170b57cec5SDimitry Andric /// true if this has successfully promoted the alloca entirely. If this returns
5180b57cec5SDimitry Andric /// false there were some loads which were not dominated by the single store
5190b57cec5SDimitry Andric /// and thus must be phi-ed with undef. We fall back to the standard alloca
5200b57cec5SDimitry Andric /// promotion algorithm in that case.
5217a6dacacSDimitry Andric static bool
5227a6dacacSDimitry Andric rewriteSingleStoreAlloca(AllocaInst *AI, AllocaInfo &Info, LargeBlockInfo &LBI,
5237a6dacacSDimitry Andric                          const DataLayout &DL, DominatorTree &DT,
5247a6dacacSDimitry Andric                          AssumptionCache *AC,
5257a6dacacSDimitry Andric                          SmallSet<DbgAssignIntrinsic *, 8> *DbgAssignsToDelete,
526*0fca6ea1SDimitry Andric                          SmallSet<DbgVariableRecord *, 8> *DVRAssignsToDelete) {
5270b57cec5SDimitry Andric   StoreInst *OnlyStore = Info.OnlyStore;
528*0fca6ea1SDimitry Andric   Value *ReplVal = OnlyStore->getOperand(0);
529*0fca6ea1SDimitry Andric   // Loads may either load the stored value or uninitialized memory (undef).
530*0fca6ea1SDimitry Andric   // If the stored value may be poison, then replacing an uninitialized memory
531*0fca6ea1SDimitry Andric   // load with it would be incorrect. If the store dominates the load, we know
532*0fca6ea1SDimitry Andric   // it is always initialized.
533*0fca6ea1SDimitry Andric   bool RequireDominatingStore =
534*0fca6ea1SDimitry Andric       isa<Instruction>(ReplVal) || !isGuaranteedNotToBePoison(ReplVal);
5350b57cec5SDimitry Andric   BasicBlock *StoreBB = OnlyStore->getParent();
5360b57cec5SDimitry Andric   int StoreIndex = -1;
5370b57cec5SDimitry Andric 
5380b57cec5SDimitry Andric   // Clear out UsingBlocks.  We will reconstruct it here if needed.
5390b57cec5SDimitry Andric   Info.UsingBlocks.clear();
5400b57cec5SDimitry Andric 
541e8d8bef9SDimitry Andric   for (User *U : make_early_inc_range(AI->users())) {
542e8d8bef9SDimitry Andric     Instruction *UserInst = cast<Instruction>(U);
5430b57cec5SDimitry Andric     if (UserInst == OnlyStore)
5440b57cec5SDimitry Andric       continue;
5450b57cec5SDimitry Andric     LoadInst *LI = cast<LoadInst>(UserInst);
5460b57cec5SDimitry Andric 
5470b57cec5SDimitry Andric     // Okay, if we have a load from the alloca, we want to replace it with the
5480b57cec5SDimitry Andric     // only value stored to the alloca.  We can do this if the value is
5490b57cec5SDimitry Andric     // dominated by the store.  If not, we use the rest of the mem2reg machinery
5500b57cec5SDimitry Andric     // to insert the phi nodes as needed.
551*0fca6ea1SDimitry Andric     if (RequireDominatingStore) {
5520b57cec5SDimitry Andric       if (LI->getParent() == StoreBB) {
5530b57cec5SDimitry Andric         // If we have a use that is in the same block as the store, compare the
5540b57cec5SDimitry Andric         // indices of the two instructions to see which one came first.  If the
5550b57cec5SDimitry Andric         // load came before the store, we can't handle it.
5560b57cec5SDimitry Andric         if (StoreIndex == -1)
5570b57cec5SDimitry Andric           StoreIndex = LBI.getInstructionIndex(OnlyStore);
5580b57cec5SDimitry Andric 
5590b57cec5SDimitry Andric         if (unsigned(StoreIndex) > LBI.getInstructionIndex(LI)) {
5600b57cec5SDimitry Andric           // Can't handle this load, bail out.
5610b57cec5SDimitry Andric           Info.UsingBlocks.push_back(StoreBB);
5620b57cec5SDimitry Andric           continue;
5630b57cec5SDimitry Andric         }
5640b57cec5SDimitry Andric       } else if (!DT.dominates(StoreBB, LI->getParent())) {
5650b57cec5SDimitry Andric         // If the load and store are in different blocks, use BB dominance to
5660b57cec5SDimitry Andric         // check their relationships.  If the store doesn't dom the use, bail
5670b57cec5SDimitry Andric         // out.
5680b57cec5SDimitry Andric         Info.UsingBlocks.push_back(LI->getParent());
5690b57cec5SDimitry Andric         continue;
5700b57cec5SDimitry Andric       }
5710b57cec5SDimitry Andric     }
5720b57cec5SDimitry Andric 
5730b57cec5SDimitry Andric     // Otherwise, we *can* safely rewrite this load.
5740b57cec5SDimitry Andric     // If the replacement value is the load, this must occur in unreachable
5750b57cec5SDimitry Andric     // code.
5760b57cec5SDimitry Andric     if (ReplVal == LI)
577fe6060f1SDimitry Andric       ReplVal = PoisonValue::get(LI->getType());
5780b57cec5SDimitry Andric 
579bdd1243dSDimitry Andric     convertMetadataToAssumes(LI, ReplVal, DL, AC, &DT);
5800b57cec5SDimitry Andric     LI->replaceAllUsesWith(ReplVal);
5810b57cec5SDimitry Andric     LI->eraseFromParent();
5820b57cec5SDimitry Andric     LBI.deleteValue(LI);
5830b57cec5SDimitry Andric   }
5840b57cec5SDimitry Andric 
5850b57cec5SDimitry Andric   // Finally, after the scan, check to see if the store is all that is left.
5860b57cec5SDimitry Andric   if (!Info.UsingBlocks.empty())
5870b57cec5SDimitry Andric     return false; // If not, we'll have to fall back for the remainder.
5880b57cec5SDimitry Andric 
589bdd1243dSDimitry Andric   DIBuilder DIB(*AI->getModule(), /*AllowUnresolved*/ false);
590bdd1243dSDimitry Andric   // Update assignment tracking info for the store we're going to delete.
5917a6dacacSDimitry Andric   Info.AssignmentTracking.updateForDeletedStore(
592*0fca6ea1SDimitry Andric       Info.OnlyStore, DIB, DbgAssignsToDelete, DVRAssignsToDelete);
593bdd1243dSDimitry Andric 
5940b57cec5SDimitry Andric   // Record debuginfo for the store and remove the declaration's
5950b57cec5SDimitry Andric   // debuginfo.
5965f757f3fSDimitry Andric   auto ConvertDebugInfoForStore = [&](auto &Container) {
5975f757f3fSDimitry Andric     for (auto *DbgItem : Container) {
5985f757f3fSDimitry Andric       if (DbgItem->isAddressOfVariable()) {
5995f757f3fSDimitry Andric         ConvertDebugDeclareToDebugValue(DbgItem, Info.OnlyStore, DIB);
6005f757f3fSDimitry Andric         DbgItem->eraseFromParent();
6015f757f3fSDimitry Andric       } else if (DbgItem->getExpression()->startsWithDeref()) {
6025f757f3fSDimitry Andric         DbgItem->eraseFromParent();
603e8d8bef9SDimitry Andric       }
6040b57cec5SDimitry Andric     }
6055f757f3fSDimitry Andric   };
6065f757f3fSDimitry Andric   ConvertDebugInfoForStore(Info.DbgUsers);
6075f757f3fSDimitry Andric   ConvertDebugInfoForStore(Info.DPUsers);
608bdd1243dSDimitry Andric 
609bdd1243dSDimitry Andric   // Remove dbg.assigns linked to the alloca as these are now redundant.
610bdd1243dSDimitry Andric   at::deleteAssignmentMarkers(AI);
611bdd1243dSDimitry Andric 
6120b57cec5SDimitry Andric   // Remove the (now dead) store and alloca.
6130b57cec5SDimitry Andric   Info.OnlyStore->eraseFromParent();
6140b57cec5SDimitry Andric   LBI.deleteValue(Info.OnlyStore);
6150b57cec5SDimitry Andric 
6160b57cec5SDimitry Andric   AI->eraseFromParent();
6170b57cec5SDimitry Andric   return true;
6180b57cec5SDimitry Andric }
6190b57cec5SDimitry Andric 
6200b57cec5SDimitry Andric /// Many allocas are only used within a single basic block.  If this is the
6210b57cec5SDimitry Andric /// case, avoid traversing the CFG and inserting a lot of potentially useless
6220b57cec5SDimitry Andric /// PHI nodes by just performing a single linear pass over the basic block
6230b57cec5SDimitry Andric /// using the Alloca.
6240b57cec5SDimitry Andric ///
6250b57cec5SDimitry Andric /// If we cannot promote this alloca (because it is read before it is written),
6260b57cec5SDimitry Andric /// return false.  This is necessary in cases where, due to control flow, the
6270b57cec5SDimitry Andric /// alloca is undefined only on some control flow paths.  e.g. code like
6280b57cec5SDimitry Andric /// this is correct in LLVM IR:
6290b57cec5SDimitry Andric ///  // A is an alloca with no stores so far
6300b57cec5SDimitry Andric ///  for (...) {
6310b57cec5SDimitry Andric ///    int t = *A;
6320b57cec5SDimitry Andric ///    if (!first_iteration)
6330b57cec5SDimitry Andric ///      use(t);
6340b57cec5SDimitry Andric ///    *A = 42;
6350b57cec5SDimitry Andric ///  }
6367a6dacacSDimitry Andric static bool
6377a6dacacSDimitry Andric promoteSingleBlockAlloca(AllocaInst *AI, const AllocaInfo &Info,
6387a6dacacSDimitry Andric                          LargeBlockInfo &LBI, const DataLayout &DL,
6397a6dacacSDimitry Andric                          DominatorTree &DT, AssumptionCache *AC,
6407a6dacacSDimitry Andric                          SmallSet<DbgAssignIntrinsic *, 8> *DbgAssignsToDelete,
641*0fca6ea1SDimitry Andric                          SmallSet<DbgVariableRecord *, 8> *DVRAssignsToDelete) {
6420b57cec5SDimitry Andric   // The trickiest case to handle is when we have large blocks. Because of this,
6430b57cec5SDimitry Andric   // this code is optimized assuming that large blocks happen.  This does not
6440b57cec5SDimitry Andric   // significantly pessimize the small block case.  This uses LargeBlockInfo to
6450b57cec5SDimitry Andric   // make it efficient to get the index of various operations in the block.
6460b57cec5SDimitry Andric 
6470b57cec5SDimitry Andric   // Walk the use-def list of the alloca, getting the locations of all stores.
6480b57cec5SDimitry Andric   using StoresByIndexTy = SmallVector<std::pair<unsigned, StoreInst *>, 64>;
6490b57cec5SDimitry Andric   StoresByIndexTy StoresByIndex;
6500b57cec5SDimitry Andric 
6510b57cec5SDimitry Andric   for (User *U : AI->users())
6520b57cec5SDimitry Andric     if (StoreInst *SI = dyn_cast<StoreInst>(U))
6530b57cec5SDimitry Andric       StoresByIndex.push_back(std::make_pair(LBI.getInstructionIndex(SI), SI));
6540b57cec5SDimitry Andric 
6550b57cec5SDimitry Andric   // Sort the stores by their index, making it efficient to do a lookup with a
6560b57cec5SDimitry Andric   // binary search.
6570b57cec5SDimitry Andric   llvm::sort(StoresByIndex, less_first());
6580b57cec5SDimitry Andric 
6590b57cec5SDimitry Andric   // Walk all of the loads from this alloca, replacing them with the nearest
6600b57cec5SDimitry Andric   // store above them, if any.
661e8d8bef9SDimitry Andric   for (User *U : make_early_inc_range(AI->users())) {
662e8d8bef9SDimitry Andric     LoadInst *LI = dyn_cast<LoadInst>(U);
6630b57cec5SDimitry Andric     if (!LI)
6640b57cec5SDimitry Andric       continue;
6650b57cec5SDimitry Andric 
6660b57cec5SDimitry Andric     unsigned LoadIdx = LBI.getInstructionIndex(LI);
6670b57cec5SDimitry Andric 
6680b57cec5SDimitry Andric     // Find the nearest store that has a lower index than this load.
6690b57cec5SDimitry Andric     StoresByIndexTy::iterator I = llvm::lower_bound(
6700b57cec5SDimitry Andric         StoresByIndex,
6710b57cec5SDimitry Andric         std::make_pair(LoadIdx, static_cast<StoreInst *>(nullptr)),
6720b57cec5SDimitry Andric         less_first());
673753f127fSDimitry Andric     Value *ReplVal;
6740b57cec5SDimitry Andric     if (I == StoresByIndex.begin()) {
6750b57cec5SDimitry Andric       if (StoresByIndex.empty())
6760b57cec5SDimitry Andric         // If there are no stores, the load takes the undef value.
677753f127fSDimitry Andric         ReplVal = UndefValue::get(LI->getType());
6780b57cec5SDimitry Andric       else
6790b57cec5SDimitry Andric         // There is no store before this load, bail out (load may be affected
6800b57cec5SDimitry Andric         // by the following stores - see main comment).
6810b57cec5SDimitry Andric         return false;
6820b57cec5SDimitry Andric     } else {
683753f127fSDimitry Andric       // Otherwise, there was a store before this load, the load takes its
684753f127fSDimitry Andric       // value.
685753f127fSDimitry Andric       ReplVal = std::prev(I)->second->getOperand(0);
686753f127fSDimitry Andric     }
687753f127fSDimitry Andric 
688bdd1243dSDimitry Andric     convertMetadataToAssumes(LI, ReplVal, DL, AC, &DT);
6890b57cec5SDimitry Andric 
6900b57cec5SDimitry Andric     // If the replacement value is the load, this must occur in unreachable
6910b57cec5SDimitry Andric     // code.
6920b57cec5SDimitry Andric     if (ReplVal == LI)
693fe6060f1SDimitry Andric       ReplVal = PoisonValue::get(LI->getType());
6940b57cec5SDimitry Andric 
6950b57cec5SDimitry Andric     LI->replaceAllUsesWith(ReplVal);
6960b57cec5SDimitry Andric     LI->eraseFromParent();
6970b57cec5SDimitry Andric     LBI.deleteValue(LI);
6980b57cec5SDimitry Andric   }
6990b57cec5SDimitry Andric 
7000b57cec5SDimitry Andric   // Remove the (now dead) stores and alloca.
701bdd1243dSDimitry Andric   DIBuilder DIB(*AI->getModule(), /*AllowUnresolved*/ false);
7020b57cec5SDimitry Andric   while (!AI->use_empty()) {
7030b57cec5SDimitry Andric     StoreInst *SI = cast<StoreInst>(AI->user_back());
704bdd1243dSDimitry Andric     // Update assignment tracking info for the store we're going to delete.
7057a6dacacSDimitry Andric     Info.AssignmentTracking.updateForDeletedStore(SI, DIB, DbgAssignsToDelete,
706*0fca6ea1SDimitry Andric                                                   DVRAssignsToDelete);
7070b57cec5SDimitry Andric     // Record debuginfo for the store before removing it.
7085f757f3fSDimitry Andric     auto DbgUpdateForStore = [&](auto &Container) {
7095f757f3fSDimitry Andric       for (auto *DbgItem : Container) {
7105f757f3fSDimitry Andric         if (DbgItem->isAddressOfVariable()) {
7115f757f3fSDimitry Andric           ConvertDebugDeclareToDebugValue(DbgItem, SI, DIB);
7120b57cec5SDimitry Andric         }
713e8d8bef9SDimitry Andric       }
7145f757f3fSDimitry Andric     };
7155f757f3fSDimitry Andric     DbgUpdateForStore(Info.DbgUsers);
7165f757f3fSDimitry Andric     DbgUpdateForStore(Info.DPUsers);
7175f757f3fSDimitry Andric 
7180b57cec5SDimitry Andric     SI->eraseFromParent();
7190b57cec5SDimitry Andric     LBI.deleteValue(SI);
7200b57cec5SDimitry Andric   }
7210b57cec5SDimitry Andric 
722bdd1243dSDimitry Andric   // Remove dbg.assigns linked to the alloca as these are now redundant.
723bdd1243dSDimitry Andric   at::deleteAssignmentMarkers(AI);
7240b57cec5SDimitry Andric   AI->eraseFromParent();
7250b57cec5SDimitry Andric 
7260b57cec5SDimitry Andric   // The alloca's debuginfo can be removed as well.
7275f757f3fSDimitry Andric   auto DbgUpdateForAlloca = [&](auto &Container) {
7285f757f3fSDimitry Andric     for (auto *DbgItem : Container)
7295f757f3fSDimitry Andric       if (DbgItem->isAddressOfVariable() ||
7305f757f3fSDimitry Andric           DbgItem->getExpression()->startsWithDeref())
7315f757f3fSDimitry Andric         DbgItem->eraseFromParent();
7325f757f3fSDimitry Andric   };
7335f757f3fSDimitry Andric   DbgUpdateForAlloca(Info.DbgUsers);
7345f757f3fSDimitry Andric   DbgUpdateForAlloca(Info.DPUsers);
7350b57cec5SDimitry Andric 
7360b57cec5SDimitry Andric   ++NumLocalPromoted;
7370b57cec5SDimitry Andric   return true;
7380b57cec5SDimitry Andric }
7390b57cec5SDimitry Andric 
7400b57cec5SDimitry Andric void PromoteMem2Reg::run() {
7410b57cec5SDimitry Andric   Function &F = *DT.getRoot()->getParent();
7420b57cec5SDimitry Andric 
743e8d8bef9SDimitry Andric   AllocaDbgUsers.resize(Allocas.size());
744bdd1243dSDimitry Andric   AllocaATInfo.resize(Allocas.size());
7455f757f3fSDimitry Andric   AllocaDPUsers.resize(Allocas.size());
7460b57cec5SDimitry Andric 
7470b57cec5SDimitry Andric   AllocaInfo Info;
7480b57cec5SDimitry Andric   LargeBlockInfo LBI;
7490b57cec5SDimitry Andric   ForwardIDFCalculator IDF(DT);
7500b57cec5SDimitry Andric 
751*0fca6ea1SDimitry Andric   NoSignedZeros = F.getFnAttribute("no-signed-zeros-fp-math").getValueAsBool();
752*0fca6ea1SDimitry Andric 
7530b57cec5SDimitry Andric   for (unsigned AllocaNum = 0; AllocaNum != Allocas.size(); ++AllocaNum) {
7540b57cec5SDimitry Andric     AllocaInst *AI = Allocas[AllocaNum];
7550b57cec5SDimitry Andric 
7560b57cec5SDimitry Andric     assert(isAllocaPromotable(AI) && "Cannot promote non-promotable alloca!");
7570b57cec5SDimitry Andric     assert(AI->getParent()->getParent() == &F &&
7580b57cec5SDimitry Andric            "All allocas should be in the same function, which is same as DF!");
7590b57cec5SDimitry Andric 
760e8d8bef9SDimitry Andric     removeIntrinsicUsers(AI);
7610b57cec5SDimitry Andric 
7620b57cec5SDimitry Andric     if (AI->use_empty()) {
7630b57cec5SDimitry Andric       // If there are no uses of the alloca, just delete it now.
7640b57cec5SDimitry Andric       AI->eraseFromParent();
7650b57cec5SDimitry Andric 
7660b57cec5SDimitry Andric       // Remove the alloca from the Allocas list, since it has been processed
7670b57cec5SDimitry Andric       RemoveFromAllocasList(AllocaNum);
7680b57cec5SDimitry Andric       ++NumDeadAlloca;
7690b57cec5SDimitry Andric       continue;
7700b57cec5SDimitry Andric     }
7710b57cec5SDimitry Andric 
7720b57cec5SDimitry Andric     // Calculate the set of read and write-locations for each alloca.  This is
7730b57cec5SDimitry Andric     // analogous to finding the 'uses' and 'definitions' of each variable.
7740b57cec5SDimitry Andric     Info.AnalyzeAlloca(AI);
7750b57cec5SDimitry Andric 
7760b57cec5SDimitry Andric     // If there is only a single store to this value, replace any loads of
7770b57cec5SDimitry Andric     // it that are directly dominated by the definition with the value stored.
7780b57cec5SDimitry Andric     if (Info.DefiningBlocks.size() == 1) {
77906c3fb27SDimitry Andric       if (rewriteSingleStoreAlloca(AI, Info, LBI, SQ.DL, DT, AC,
780*0fca6ea1SDimitry Andric                                    &DbgAssignsToDelete, &DVRAssignsToDelete)) {
7810b57cec5SDimitry Andric         // The alloca has been processed, move on.
7820b57cec5SDimitry Andric         RemoveFromAllocasList(AllocaNum);
7830b57cec5SDimitry Andric         ++NumSingleStore;
7840b57cec5SDimitry Andric         continue;
7850b57cec5SDimitry Andric       }
7860b57cec5SDimitry Andric     }
7870b57cec5SDimitry Andric 
7880b57cec5SDimitry Andric     // If the alloca is only read and written in one basic block, just perform a
7890b57cec5SDimitry Andric     // linear sweep over the block to eliminate it.
7900b57cec5SDimitry Andric     if (Info.OnlyUsedInOneBlock &&
79106c3fb27SDimitry Andric         promoteSingleBlockAlloca(AI, Info, LBI, SQ.DL, DT, AC,
792*0fca6ea1SDimitry Andric                                  &DbgAssignsToDelete, &DVRAssignsToDelete)) {
7930b57cec5SDimitry Andric       // The alloca has been processed, move on.
7940b57cec5SDimitry Andric       RemoveFromAllocasList(AllocaNum);
7950b57cec5SDimitry Andric       continue;
7960b57cec5SDimitry Andric     }
7970b57cec5SDimitry Andric 
7980b57cec5SDimitry Andric     // If we haven't computed a numbering for the BB's in the function, do so
7990b57cec5SDimitry Andric     // now.
8000b57cec5SDimitry Andric     if (BBNumbers.empty()) {
8010b57cec5SDimitry Andric       unsigned ID = 0;
8020b57cec5SDimitry Andric       for (auto &BB : F)
8030b57cec5SDimitry Andric         BBNumbers[&BB] = ID++;
8040b57cec5SDimitry Andric     }
8050b57cec5SDimitry Andric 
8060b57cec5SDimitry Andric     // Remember the dbg.declare intrinsic describing this alloca, if any.
807e8d8bef9SDimitry Andric     if (!Info.DbgUsers.empty())
808e8d8bef9SDimitry Andric       AllocaDbgUsers[AllocaNum] = Info.DbgUsers;
809bdd1243dSDimitry Andric     if (!Info.AssignmentTracking.empty())
810bdd1243dSDimitry Andric       AllocaATInfo[AllocaNum] = Info.AssignmentTracking;
8115f757f3fSDimitry Andric     if (!Info.DPUsers.empty())
8125f757f3fSDimitry Andric       AllocaDPUsers[AllocaNum] = Info.DPUsers;
8130b57cec5SDimitry Andric 
8140b57cec5SDimitry Andric     // Keep the reverse mapping of the 'Allocas' array for the rename pass.
8150b57cec5SDimitry Andric     AllocaLookup[Allocas[AllocaNum]] = AllocaNum;
8160b57cec5SDimitry Andric 
8170b57cec5SDimitry Andric     // Unique the set of defining blocks for efficient lookup.
8180b57cec5SDimitry Andric     SmallPtrSet<BasicBlock *, 32> DefBlocks(Info.DefiningBlocks.begin(),
8190b57cec5SDimitry Andric                                             Info.DefiningBlocks.end());
8200b57cec5SDimitry Andric 
8210b57cec5SDimitry Andric     // Determine which blocks the value is live in.  These are blocks which lead
8220b57cec5SDimitry Andric     // to uses.
8230b57cec5SDimitry Andric     SmallPtrSet<BasicBlock *, 32> LiveInBlocks;
8240b57cec5SDimitry Andric     ComputeLiveInBlocks(AI, Info, DefBlocks, LiveInBlocks);
8250b57cec5SDimitry Andric 
8260b57cec5SDimitry Andric     // At this point, we're committed to promoting the alloca using IDF's, and
8270b57cec5SDimitry Andric     // the standard SSA construction algorithm.  Determine which blocks need phi
8280b57cec5SDimitry Andric     // nodes and see if we can optimize out some work by avoiding insertion of
8290b57cec5SDimitry Andric     // dead phi nodes.
8300b57cec5SDimitry Andric     IDF.setLiveInBlocks(LiveInBlocks);
8310b57cec5SDimitry Andric     IDF.setDefiningBlocks(DefBlocks);
8320b57cec5SDimitry Andric     SmallVector<BasicBlock *, 32> PHIBlocks;
8330b57cec5SDimitry Andric     IDF.calculate(PHIBlocks);
8340b57cec5SDimitry Andric     llvm::sort(PHIBlocks, [this](BasicBlock *A, BasicBlock *B) {
8350b57cec5SDimitry Andric       return BBNumbers.find(A)->second < BBNumbers.find(B)->second;
8360b57cec5SDimitry Andric     });
8370b57cec5SDimitry Andric 
8380b57cec5SDimitry Andric     unsigned CurrentVersion = 0;
8390b57cec5SDimitry Andric     for (BasicBlock *BB : PHIBlocks)
8400b57cec5SDimitry Andric       QueuePhiNode(BB, AllocaNum, CurrentVersion);
8410b57cec5SDimitry Andric   }
8420b57cec5SDimitry Andric 
84306c3fb27SDimitry Andric   if (Allocas.empty()) {
84406c3fb27SDimitry Andric     cleanUpDbgAssigns();
8450b57cec5SDimitry Andric     return; // All of the allocas must have been trivial!
84606c3fb27SDimitry Andric   }
8470b57cec5SDimitry Andric   LBI.clear();
8480b57cec5SDimitry Andric 
8490b57cec5SDimitry Andric   // Set the incoming values for the basic block to be null values for all of
8500b57cec5SDimitry Andric   // the alloca's.  We do this in case there is a load of a value that has not
8510b57cec5SDimitry Andric   // been stored yet.  In this case, it will get this null value.
8520b57cec5SDimitry Andric   RenamePassData::ValVector Values(Allocas.size());
8530b57cec5SDimitry Andric   for (unsigned i = 0, e = Allocas.size(); i != e; ++i)
8540b57cec5SDimitry Andric     Values[i] = UndefValue::get(Allocas[i]->getAllocatedType());
8550b57cec5SDimitry Andric 
8560b57cec5SDimitry Andric   // When handling debug info, treat all incoming values as if they have unknown
8570b57cec5SDimitry Andric   // locations until proven otherwise.
8580b57cec5SDimitry Andric   RenamePassData::LocationVector Locations(Allocas.size());
8590b57cec5SDimitry Andric 
8600b57cec5SDimitry Andric   // Walks all basic blocks in the function performing the SSA rename algorithm
8610b57cec5SDimitry Andric   // and inserting the phi nodes we marked as necessary
8620b57cec5SDimitry Andric   std::vector<RenamePassData> RenamePassWorkList;
8630b57cec5SDimitry Andric   RenamePassWorkList.emplace_back(&F.front(), nullptr, std::move(Values),
8640b57cec5SDimitry Andric                                   std::move(Locations));
8650b57cec5SDimitry Andric   do {
8660b57cec5SDimitry Andric     RenamePassData RPD = std::move(RenamePassWorkList.back());
8670b57cec5SDimitry Andric     RenamePassWorkList.pop_back();
8680b57cec5SDimitry Andric     // RenamePass may add new worklist entries.
8690b57cec5SDimitry Andric     RenamePass(RPD.BB, RPD.Pred, RPD.Values, RPD.Locations, RenamePassWorkList);
8700b57cec5SDimitry Andric   } while (!RenamePassWorkList.empty());
8710b57cec5SDimitry Andric 
8720b57cec5SDimitry Andric   // The renamer uses the Visited set to avoid infinite loops.  Clear it now.
8730b57cec5SDimitry Andric   Visited.clear();
8740b57cec5SDimitry Andric 
8750b57cec5SDimitry Andric   // Remove the allocas themselves from the function.
8760b57cec5SDimitry Andric   for (Instruction *A : Allocas) {
877bdd1243dSDimitry Andric     // Remove dbg.assigns linked to the alloca as these are now redundant.
878bdd1243dSDimitry Andric     at::deleteAssignmentMarkers(A);
8790b57cec5SDimitry Andric     // If there are any uses of the alloca instructions left, they must be in
8800b57cec5SDimitry Andric     // unreachable basic blocks that were not processed by walking the dominator
8810b57cec5SDimitry Andric     // tree. Just delete the users now.
8820b57cec5SDimitry Andric     if (!A->use_empty())
883fe6060f1SDimitry Andric       A->replaceAllUsesWith(PoisonValue::get(A->getType()));
8840b57cec5SDimitry Andric     A->eraseFromParent();
8850b57cec5SDimitry Andric   }
8860b57cec5SDimitry Andric 
88781ad6265SDimitry Andric   // Remove alloca's dbg.declare intrinsics from the function.
8885f757f3fSDimitry Andric   auto RemoveDbgDeclares = [&](auto &Container) {
8895f757f3fSDimitry Andric     for (auto &DbgUsers : Container) {
8905f757f3fSDimitry Andric       for (auto *DbgItem : DbgUsers)
8915f757f3fSDimitry Andric         if (DbgItem->isAddressOfVariable() ||
8925f757f3fSDimitry Andric             DbgItem->getExpression()->startsWithDeref())
8935f757f3fSDimitry Andric           DbgItem->eraseFromParent();
894e8d8bef9SDimitry Andric     }
8955f757f3fSDimitry Andric   };
8965f757f3fSDimitry Andric   RemoveDbgDeclares(AllocaDbgUsers);
8975f757f3fSDimitry Andric   RemoveDbgDeclares(AllocaDPUsers);
8980b57cec5SDimitry Andric 
8990b57cec5SDimitry Andric   // Loop over all of the PHI nodes and see if there are any that we can get
9000b57cec5SDimitry Andric   // rid of because they merge all of the same incoming values.  This can
9010b57cec5SDimitry Andric   // happen due to undef values coming into the PHI nodes.  This process is
9020b57cec5SDimitry Andric   // iterative, because eliminating one PHI node can cause others to be removed.
9030b57cec5SDimitry Andric   bool EliminatedAPHI = true;
9040b57cec5SDimitry Andric   while (EliminatedAPHI) {
9050b57cec5SDimitry Andric     EliminatedAPHI = false;
9060b57cec5SDimitry Andric 
9070b57cec5SDimitry Andric     // Iterating over NewPhiNodes is deterministic, so it is safe to try to
9080b57cec5SDimitry Andric     // simplify and RAUW them as we go.  If it was not, we could add uses to
9090b57cec5SDimitry Andric     // the values we replace with in a non-deterministic order, thus creating
9100b57cec5SDimitry Andric     // non-deterministic def->use chains.
9110b57cec5SDimitry Andric     for (DenseMap<std::pair<unsigned, unsigned>, PHINode *>::iterator
9120b57cec5SDimitry Andric              I = NewPhiNodes.begin(),
9130b57cec5SDimitry Andric              E = NewPhiNodes.end();
9140b57cec5SDimitry Andric          I != E;) {
9150b57cec5SDimitry Andric       PHINode *PN = I->second;
9160b57cec5SDimitry Andric 
9170b57cec5SDimitry Andric       // If this PHI node merges one value and/or undefs, get the value.
91881ad6265SDimitry Andric       if (Value *V = simplifyInstruction(PN, SQ)) {
9190b57cec5SDimitry Andric         PN->replaceAllUsesWith(V);
9200b57cec5SDimitry Andric         PN->eraseFromParent();
9210b57cec5SDimitry Andric         NewPhiNodes.erase(I++);
9220b57cec5SDimitry Andric         EliminatedAPHI = true;
9230b57cec5SDimitry Andric         continue;
9240b57cec5SDimitry Andric       }
9250b57cec5SDimitry Andric       ++I;
9260b57cec5SDimitry Andric     }
9270b57cec5SDimitry Andric   }
9280b57cec5SDimitry Andric 
9290b57cec5SDimitry Andric   // At this point, the renamer has added entries to PHI nodes for all reachable
9300b57cec5SDimitry Andric   // code.  Unfortunately, there may be unreachable blocks which the renamer
9310b57cec5SDimitry Andric   // hasn't traversed.  If this is the case, the PHI nodes may not
9320b57cec5SDimitry Andric   // have incoming values for all predecessors.  Loop over all PHI nodes we have
93306c3fb27SDimitry Andric   // created, inserting poison values if they are missing any incoming values.
9340b57cec5SDimitry Andric   for (DenseMap<std::pair<unsigned, unsigned>, PHINode *>::iterator
9350b57cec5SDimitry Andric            I = NewPhiNodes.begin(),
9360b57cec5SDimitry Andric            E = NewPhiNodes.end();
9370b57cec5SDimitry Andric        I != E; ++I) {
9380b57cec5SDimitry Andric     // We want to do this once per basic block.  As such, only process a block
9390b57cec5SDimitry Andric     // when we find the PHI that is the first entry in the block.
9400b57cec5SDimitry Andric     PHINode *SomePHI = I->second;
9410b57cec5SDimitry Andric     BasicBlock *BB = SomePHI->getParent();
9420b57cec5SDimitry Andric     if (&BB->front() != SomePHI)
9430b57cec5SDimitry Andric       continue;
9440b57cec5SDimitry Andric 
9450b57cec5SDimitry Andric     // Only do work here if there the PHI nodes are missing incoming values.  We
9460b57cec5SDimitry Andric     // know that all PHI nodes that were inserted in a block will have the same
9470b57cec5SDimitry Andric     // number of incoming values, so we can just check any of them.
9480b57cec5SDimitry Andric     if (SomePHI->getNumIncomingValues() == getNumPreds(BB))
9490b57cec5SDimitry Andric       continue;
9500b57cec5SDimitry Andric 
9510b57cec5SDimitry Andric     // Get the preds for BB.
952e8d8bef9SDimitry Andric     SmallVector<BasicBlock *, 16> Preds(predecessors(BB));
9530b57cec5SDimitry Andric 
9540b57cec5SDimitry Andric     // Ok, now we know that all of the PHI nodes are missing entries for some
9550b57cec5SDimitry Andric     // basic blocks.  Start by sorting the incoming predecessors for efficient
9560b57cec5SDimitry Andric     // access.
9570b57cec5SDimitry Andric     auto CompareBBNumbers = [this](BasicBlock *A, BasicBlock *B) {
9580b57cec5SDimitry Andric       return BBNumbers.find(A)->second < BBNumbers.find(B)->second;
9590b57cec5SDimitry Andric     };
9600b57cec5SDimitry Andric     llvm::sort(Preds, CompareBBNumbers);
9610b57cec5SDimitry Andric 
9620b57cec5SDimitry Andric     // Now we loop through all BB's which have entries in SomePHI and remove
9630b57cec5SDimitry Andric     // them from the Preds list.
9640b57cec5SDimitry Andric     for (unsigned i = 0, e = SomePHI->getNumIncomingValues(); i != e; ++i) {
9650b57cec5SDimitry Andric       // Do a log(n) search of the Preds list for the entry we want.
9660b57cec5SDimitry Andric       SmallVectorImpl<BasicBlock *>::iterator EntIt = llvm::lower_bound(
9670b57cec5SDimitry Andric           Preds, SomePHI->getIncomingBlock(i), CompareBBNumbers);
9680b57cec5SDimitry Andric       assert(EntIt != Preds.end() && *EntIt == SomePHI->getIncomingBlock(i) &&
9690b57cec5SDimitry Andric              "PHI node has entry for a block which is not a predecessor!");
9700b57cec5SDimitry Andric 
9710b57cec5SDimitry Andric       // Remove the entry
9720b57cec5SDimitry Andric       Preds.erase(EntIt);
9730b57cec5SDimitry Andric     }
9740b57cec5SDimitry Andric 
9750b57cec5SDimitry Andric     // At this point, the blocks left in the preds list must have dummy
9760b57cec5SDimitry Andric     // entries inserted into every PHI nodes for the block.  Update all the phi
9770b57cec5SDimitry Andric     // nodes in this block that we are inserting (there could be phis before
9780b57cec5SDimitry Andric     // mem2reg runs).
9790b57cec5SDimitry Andric     unsigned NumBadPreds = SomePHI->getNumIncomingValues();
9800b57cec5SDimitry Andric     BasicBlock::iterator BBI = BB->begin();
9810b57cec5SDimitry Andric     while ((SomePHI = dyn_cast<PHINode>(BBI++)) &&
9820b57cec5SDimitry Andric            SomePHI->getNumIncomingValues() == NumBadPreds) {
98306c3fb27SDimitry Andric       Value *PoisonVal = PoisonValue::get(SomePHI->getType());
9840b57cec5SDimitry Andric       for (BasicBlock *Pred : Preds)
98506c3fb27SDimitry Andric         SomePHI->addIncoming(PoisonVal, Pred);
9860b57cec5SDimitry Andric     }
9870b57cec5SDimitry Andric   }
9880b57cec5SDimitry Andric 
9890b57cec5SDimitry Andric   NewPhiNodes.clear();
99006c3fb27SDimitry Andric   cleanUpDbgAssigns();
9910b57cec5SDimitry Andric }
9920b57cec5SDimitry Andric 
9930b57cec5SDimitry Andric /// Determine which blocks the value is live in.
9940b57cec5SDimitry Andric ///
9950b57cec5SDimitry Andric /// These are blocks which lead to uses.  Knowing this allows us to avoid
9960b57cec5SDimitry Andric /// inserting PHI nodes into blocks which don't lead to uses (thus, the
9970b57cec5SDimitry Andric /// inserted phi nodes would be dead).
9980b57cec5SDimitry Andric void PromoteMem2Reg::ComputeLiveInBlocks(
9990b57cec5SDimitry Andric     AllocaInst *AI, AllocaInfo &Info,
10000b57cec5SDimitry Andric     const SmallPtrSetImpl<BasicBlock *> &DefBlocks,
10010b57cec5SDimitry Andric     SmallPtrSetImpl<BasicBlock *> &LiveInBlocks) {
10020b57cec5SDimitry Andric   // To determine liveness, we must iterate through the predecessors of blocks
10030b57cec5SDimitry Andric   // where the def is live.  Blocks are added to the worklist if we need to
10040b57cec5SDimitry Andric   // check their predecessors.  Start with all the using blocks.
10050b57cec5SDimitry Andric   SmallVector<BasicBlock *, 64> LiveInBlockWorklist(Info.UsingBlocks.begin(),
10060b57cec5SDimitry Andric                                                     Info.UsingBlocks.end());
10070b57cec5SDimitry Andric 
10080b57cec5SDimitry Andric   // If any of the using blocks is also a definition block, check to see if the
10090b57cec5SDimitry Andric   // definition occurs before or after the use.  If it happens before the use,
10100b57cec5SDimitry Andric   // the value isn't really live-in.
10110b57cec5SDimitry Andric   for (unsigned i = 0, e = LiveInBlockWorklist.size(); i != e; ++i) {
10120b57cec5SDimitry Andric     BasicBlock *BB = LiveInBlockWorklist[i];
10130b57cec5SDimitry Andric     if (!DefBlocks.count(BB))
10140b57cec5SDimitry Andric       continue;
10150b57cec5SDimitry Andric 
10160b57cec5SDimitry Andric     // Okay, this is a block that both uses and defines the value.  If the first
10170b57cec5SDimitry Andric     // reference to the alloca is a def (store), then we know it isn't live-in.
10180b57cec5SDimitry Andric     for (BasicBlock::iterator I = BB->begin();; ++I) {
10190b57cec5SDimitry Andric       if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
10200b57cec5SDimitry Andric         if (SI->getOperand(1) != AI)
10210b57cec5SDimitry Andric           continue;
10220b57cec5SDimitry Andric 
10230b57cec5SDimitry Andric         // We found a store to the alloca before a load.  The alloca is not
10240b57cec5SDimitry Andric         // actually live-in here.
10250b57cec5SDimitry Andric         LiveInBlockWorklist[i] = LiveInBlockWorklist.back();
10260b57cec5SDimitry Andric         LiveInBlockWorklist.pop_back();
10270b57cec5SDimitry Andric         --i;
10280b57cec5SDimitry Andric         --e;
10290b57cec5SDimitry Andric         break;
10300b57cec5SDimitry Andric       }
10310b57cec5SDimitry Andric 
10320b57cec5SDimitry Andric       if (LoadInst *LI = dyn_cast<LoadInst>(I))
10330b57cec5SDimitry Andric         // Okay, we found a load before a store to the alloca.  It is actually
10340b57cec5SDimitry Andric         // live into this block.
10350b57cec5SDimitry Andric         if (LI->getOperand(0) == AI)
10360b57cec5SDimitry Andric           break;
10370b57cec5SDimitry Andric     }
10380b57cec5SDimitry Andric   }
10390b57cec5SDimitry Andric 
10400b57cec5SDimitry Andric   // Now that we have a set of blocks where the phi is live-in, recursively add
10410b57cec5SDimitry Andric   // their predecessors until we find the full region the value is live.
10420b57cec5SDimitry Andric   while (!LiveInBlockWorklist.empty()) {
10430b57cec5SDimitry Andric     BasicBlock *BB = LiveInBlockWorklist.pop_back_val();
10440b57cec5SDimitry Andric 
10450b57cec5SDimitry Andric     // The block really is live in here, insert it into the set.  If already in
10460b57cec5SDimitry Andric     // the set, then it has already been processed.
10470b57cec5SDimitry Andric     if (!LiveInBlocks.insert(BB).second)
10480b57cec5SDimitry Andric       continue;
10490b57cec5SDimitry Andric 
10500b57cec5SDimitry Andric     // Since the value is live into BB, it is either defined in a predecessor or
10510b57cec5SDimitry Andric     // live into it to.  Add the preds to the worklist unless they are a
10520b57cec5SDimitry Andric     // defining block.
10530b57cec5SDimitry Andric     for (BasicBlock *P : predecessors(BB)) {
10540b57cec5SDimitry Andric       // The value is not live into a predecessor if it defines the value.
10550b57cec5SDimitry Andric       if (DefBlocks.count(P))
10560b57cec5SDimitry Andric         continue;
10570b57cec5SDimitry Andric 
10580b57cec5SDimitry Andric       // Otherwise it is, add to the worklist.
10590b57cec5SDimitry Andric       LiveInBlockWorklist.push_back(P);
10600b57cec5SDimitry Andric     }
10610b57cec5SDimitry Andric   }
10620b57cec5SDimitry Andric }
10630b57cec5SDimitry Andric 
10640b57cec5SDimitry Andric /// Queue a phi-node to be added to a basic-block for a specific Alloca.
10650b57cec5SDimitry Andric ///
10660b57cec5SDimitry Andric /// Returns true if there wasn't already a phi-node for that variable
10670b57cec5SDimitry Andric bool PromoteMem2Reg::QueuePhiNode(BasicBlock *BB, unsigned AllocaNo,
10680b57cec5SDimitry Andric                                   unsigned &Version) {
10690b57cec5SDimitry Andric   // Look up the basic-block in question.
10700b57cec5SDimitry Andric   PHINode *&PN = NewPhiNodes[std::make_pair(BBNumbers[BB], AllocaNo)];
10710b57cec5SDimitry Andric 
10720b57cec5SDimitry Andric   // If the BB already has a phi node added for the i'th alloca then we're done!
10730b57cec5SDimitry Andric   if (PN)
10740b57cec5SDimitry Andric     return false;
10750b57cec5SDimitry Andric 
10760b57cec5SDimitry Andric   // Create a PhiNode using the dereferenced type... and add the phi-node to the
10770b57cec5SDimitry Andric   // BasicBlock.
10780b57cec5SDimitry Andric   PN = PHINode::Create(Allocas[AllocaNo]->getAllocatedType(), getNumPreds(BB),
10795f757f3fSDimitry Andric                        Allocas[AllocaNo]->getName() + "." + Twine(Version++));
10805f757f3fSDimitry Andric   PN->insertBefore(BB->begin());
10810b57cec5SDimitry Andric   ++NumPHIInsert;
10820b57cec5SDimitry Andric   PhiToAllocaMap[PN] = AllocaNo;
10830b57cec5SDimitry Andric   return true;
10840b57cec5SDimitry Andric }
10850b57cec5SDimitry Andric 
10860b57cec5SDimitry Andric /// Update the debug location of a phi. \p ApplyMergedLoc indicates whether to
10870b57cec5SDimitry Andric /// create a merged location incorporating \p DL, or to set \p DL directly.
10880b57cec5SDimitry Andric static void updateForIncomingValueLocation(PHINode *PN, DebugLoc DL,
10890b57cec5SDimitry Andric                                            bool ApplyMergedLoc) {
10900b57cec5SDimitry Andric   if (ApplyMergedLoc)
10910b57cec5SDimitry Andric     PN->applyMergedLocation(PN->getDebugLoc(), DL);
10920b57cec5SDimitry Andric   else
10930b57cec5SDimitry Andric     PN->setDebugLoc(DL);
10940b57cec5SDimitry Andric }
10950b57cec5SDimitry Andric 
10960b57cec5SDimitry Andric /// Recursively traverse the CFG of the function, renaming loads and
10970b57cec5SDimitry Andric /// stores to the allocas which we are promoting.
10980b57cec5SDimitry Andric ///
10990b57cec5SDimitry Andric /// IncomingVals indicates what value each Alloca contains on exit from the
11000b57cec5SDimitry Andric /// predecessor block Pred.
11010b57cec5SDimitry Andric void PromoteMem2Reg::RenamePass(BasicBlock *BB, BasicBlock *Pred,
11020b57cec5SDimitry Andric                                 RenamePassData::ValVector &IncomingVals,
11030b57cec5SDimitry Andric                                 RenamePassData::LocationVector &IncomingLocs,
11040b57cec5SDimitry Andric                                 std::vector<RenamePassData> &Worklist) {
11050b57cec5SDimitry Andric NextIteration:
11060b57cec5SDimitry Andric   // If we are inserting any phi nodes into this BB, they will already be in the
11070b57cec5SDimitry Andric   // block.
11080b57cec5SDimitry Andric   if (PHINode *APN = dyn_cast<PHINode>(BB->begin())) {
11090b57cec5SDimitry Andric     // If we have PHI nodes to update, compute the number of edges from Pred to
11100b57cec5SDimitry Andric     // BB.
11110b57cec5SDimitry Andric     if (PhiToAllocaMap.count(APN)) {
11120b57cec5SDimitry Andric       // We want to be able to distinguish between PHI nodes being inserted by
11130b57cec5SDimitry Andric       // this invocation of mem2reg from those phi nodes that already existed in
11140b57cec5SDimitry Andric       // the IR before mem2reg was run.  We determine that APN is being inserted
11150b57cec5SDimitry Andric       // because it is missing incoming edges.  All other PHI nodes being
11160b57cec5SDimitry Andric       // inserted by this pass of mem2reg will have the same number of incoming
11170b57cec5SDimitry Andric       // operands so far.  Remember this count.
11180b57cec5SDimitry Andric       unsigned NewPHINumOperands = APN->getNumOperands();
11190b57cec5SDimitry Andric 
1120e8d8bef9SDimitry Andric       unsigned NumEdges = llvm::count(successors(Pred), BB);
11210b57cec5SDimitry Andric       assert(NumEdges && "Must be at least one edge from Pred to BB!");
11220b57cec5SDimitry Andric 
11230b57cec5SDimitry Andric       // Add entries for all the phis.
11240b57cec5SDimitry Andric       BasicBlock::iterator PNI = BB->begin();
11250b57cec5SDimitry Andric       do {
11260b57cec5SDimitry Andric         unsigned AllocaNo = PhiToAllocaMap[APN];
11270b57cec5SDimitry Andric 
11280b57cec5SDimitry Andric         // Update the location of the phi node.
11290b57cec5SDimitry Andric         updateForIncomingValueLocation(APN, IncomingLocs[AllocaNo],
11300b57cec5SDimitry Andric                                        APN->getNumIncomingValues() > 0);
11310b57cec5SDimitry Andric 
11320b57cec5SDimitry Andric         // Add N incoming values to the PHI node.
11330b57cec5SDimitry Andric         for (unsigned i = 0; i != NumEdges; ++i)
11340b57cec5SDimitry Andric           APN->addIncoming(IncomingVals[AllocaNo], Pred);
11350b57cec5SDimitry Andric 
1136*0fca6ea1SDimitry Andric         // For the  sequence `return X > 0.0 ? X : -X`, it is expected that this
1137*0fca6ea1SDimitry Andric         // results in fabs intrinsic. However, without no-signed-zeros(nsz) flag
1138*0fca6ea1SDimitry Andric         // on the phi node generated at this stage, fabs folding does not
1139*0fca6ea1SDimitry Andric         // happen. So, we try to infer nsz flag from the function attributes to
1140*0fca6ea1SDimitry Andric         // enable this fabs folding.
1141*0fca6ea1SDimitry Andric         if (isa<FPMathOperator>(APN) && NoSignedZeros)
1142*0fca6ea1SDimitry Andric           APN->setHasNoSignedZeros(true);
1143*0fca6ea1SDimitry Andric 
11440b57cec5SDimitry Andric         // The currently active variable for this block is now the PHI.
11450b57cec5SDimitry Andric         IncomingVals[AllocaNo] = APN;
1146bdd1243dSDimitry Andric         AllocaATInfo[AllocaNo].updateForNewPhi(APN, DIB);
11475f757f3fSDimitry Andric         auto ConvertDbgDeclares = [&](auto &Container) {
11485f757f3fSDimitry Andric           for (auto *DbgItem : Container)
11495f757f3fSDimitry Andric             if (DbgItem->isAddressOfVariable())
11505f757f3fSDimitry Andric               ConvertDebugDeclareToDebugValue(DbgItem, APN, DIB);
11515f757f3fSDimitry Andric         };
11525f757f3fSDimitry Andric         ConvertDbgDeclares(AllocaDbgUsers[AllocaNo]);
11535f757f3fSDimitry Andric         ConvertDbgDeclares(AllocaDPUsers[AllocaNo]);
11540b57cec5SDimitry Andric 
11550b57cec5SDimitry Andric         // Get the next phi node.
11560b57cec5SDimitry Andric         ++PNI;
11570b57cec5SDimitry Andric         APN = dyn_cast<PHINode>(PNI);
11580b57cec5SDimitry Andric         if (!APN)
11590b57cec5SDimitry Andric           break;
11600b57cec5SDimitry Andric 
11610b57cec5SDimitry Andric         // Verify that it is missing entries.  If not, it is not being inserted
11620b57cec5SDimitry Andric         // by this mem2reg invocation so we want to ignore it.
11630b57cec5SDimitry Andric       } while (APN->getNumOperands() == NewPHINumOperands);
11640b57cec5SDimitry Andric     }
11650b57cec5SDimitry Andric   }
11660b57cec5SDimitry Andric 
11670b57cec5SDimitry Andric   // Don't revisit blocks.
11680b57cec5SDimitry Andric   if (!Visited.insert(BB).second)
11690b57cec5SDimitry Andric     return;
11700b57cec5SDimitry Andric 
11710b57cec5SDimitry Andric   for (BasicBlock::iterator II = BB->begin(); !II->isTerminator();) {
11720b57cec5SDimitry Andric     Instruction *I = &*II++; // get the instruction, increment iterator
11730b57cec5SDimitry Andric 
11740b57cec5SDimitry Andric     if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
11750b57cec5SDimitry Andric       AllocaInst *Src = dyn_cast<AllocaInst>(LI->getPointerOperand());
11760b57cec5SDimitry Andric       if (!Src)
11770b57cec5SDimitry Andric         continue;
11780b57cec5SDimitry Andric 
11790b57cec5SDimitry Andric       DenseMap<AllocaInst *, unsigned>::iterator AI = AllocaLookup.find(Src);
11800b57cec5SDimitry Andric       if (AI == AllocaLookup.end())
11810b57cec5SDimitry Andric         continue;
11820b57cec5SDimitry Andric 
11830b57cec5SDimitry Andric       Value *V = IncomingVals[AI->second];
1184bdd1243dSDimitry Andric       convertMetadataToAssumes(LI, V, SQ.DL, AC, &DT);
11850b57cec5SDimitry Andric 
11860b57cec5SDimitry Andric       // Anything using the load now uses the current value.
11870b57cec5SDimitry Andric       LI->replaceAllUsesWith(V);
1188bdd1243dSDimitry Andric       LI->eraseFromParent();
11890b57cec5SDimitry Andric     } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
11900b57cec5SDimitry Andric       // Delete this instruction and mark the name as the current holder of the
11910b57cec5SDimitry Andric       // value
11920b57cec5SDimitry Andric       AllocaInst *Dest = dyn_cast<AllocaInst>(SI->getPointerOperand());
11930b57cec5SDimitry Andric       if (!Dest)
11940b57cec5SDimitry Andric         continue;
11950b57cec5SDimitry Andric 
11960b57cec5SDimitry Andric       DenseMap<AllocaInst *, unsigned>::iterator ai = AllocaLookup.find(Dest);
11970b57cec5SDimitry Andric       if (ai == AllocaLookup.end())
11980b57cec5SDimitry Andric         continue;
11990b57cec5SDimitry Andric 
12000b57cec5SDimitry Andric       // what value were we writing?
12010b57cec5SDimitry Andric       unsigned AllocaNo = ai->second;
12020b57cec5SDimitry Andric       IncomingVals[AllocaNo] = SI->getOperand(0);
12030b57cec5SDimitry Andric 
12040b57cec5SDimitry Andric       // Record debuginfo for the store before removing it.
12050b57cec5SDimitry Andric       IncomingLocs[AllocaNo] = SI->getDebugLoc();
12067a6dacacSDimitry Andric       AllocaATInfo[AllocaNo].updateForDeletedStore(SI, DIB, &DbgAssignsToDelete,
1207*0fca6ea1SDimitry Andric                                                    &DVRAssignsToDelete);
12085f757f3fSDimitry Andric       auto ConvertDbgDeclares = [&](auto &Container) {
12095f757f3fSDimitry Andric         for (auto *DbgItem : Container)
12105f757f3fSDimitry Andric           if (DbgItem->isAddressOfVariable())
12115f757f3fSDimitry Andric             ConvertDebugDeclareToDebugValue(DbgItem, SI, DIB);
12125f757f3fSDimitry Andric       };
12135f757f3fSDimitry Andric       ConvertDbgDeclares(AllocaDbgUsers[ai->second]);
12145f757f3fSDimitry Andric       ConvertDbgDeclares(AllocaDPUsers[ai->second]);
1215bdd1243dSDimitry Andric       SI->eraseFromParent();
12160b57cec5SDimitry Andric     }
12170b57cec5SDimitry Andric   }
12180b57cec5SDimitry Andric 
12190b57cec5SDimitry Andric   // 'Recurse' to our successors.
12200b57cec5SDimitry Andric   succ_iterator I = succ_begin(BB), E = succ_end(BB);
12210b57cec5SDimitry Andric   if (I == E)
12220b57cec5SDimitry Andric     return;
12230b57cec5SDimitry Andric 
12240b57cec5SDimitry Andric   // Keep track of the successors so we don't visit the same successor twice
12250b57cec5SDimitry Andric   SmallPtrSet<BasicBlock *, 8> VisitedSuccs;
12260b57cec5SDimitry Andric 
12270b57cec5SDimitry Andric   // Handle the first successor without using the worklist.
12280b57cec5SDimitry Andric   VisitedSuccs.insert(*I);
12290b57cec5SDimitry Andric   Pred = BB;
12300b57cec5SDimitry Andric   BB = *I;
12310b57cec5SDimitry Andric   ++I;
12320b57cec5SDimitry Andric 
12330b57cec5SDimitry Andric   for (; I != E; ++I)
12340b57cec5SDimitry Andric     if (VisitedSuccs.insert(*I).second)
12350b57cec5SDimitry Andric       Worklist.emplace_back(*I, Pred, IncomingVals, IncomingLocs);
12360b57cec5SDimitry Andric 
12370b57cec5SDimitry Andric   goto NextIteration;
12380b57cec5SDimitry Andric }
12390b57cec5SDimitry Andric 
12400b57cec5SDimitry Andric void llvm::PromoteMemToReg(ArrayRef<AllocaInst *> Allocas, DominatorTree &DT,
12410b57cec5SDimitry Andric                            AssumptionCache *AC) {
12420b57cec5SDimitry Andric   // If there is nothing to do, bail out...
12430b57cec5SDimitry Andric   if (Allocas.empty())
12440b57cec5SDimitry Andric     return;
12450b57cec5SDimitry Andric 
12460b57cec5SDimitry Andric   PromoteMem2Reg(Allocas, DT, AC).run();
12470b57cec5SDimitry Andric }
1248