xref: /freebsd/contrib/llvm-project/llvm/lib/Transforms/Utils/PromoteMemoryToRegister.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
1 //===- PromoteMemoryToRegister.cpp - Convert allocas to registers ---------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file promotes memory references to be register references.  It promotes
10 // alloca instructions which only have loads and stores as uses.  An alloca is
11 // transformed by using iterated dominator frontiers to place PHI nodes, then
12 // traversing the function in depth-first order to rewrite loads and stores as
13 // appropriate.
14 //
15 //===----------------------------------------------------------------------===//
16 
17 #include "llvm/ADT/ArrayRef.h"
18 #include "llvm/ADT/DenseMap.h"
19 #include "llvm/ADT/STLExtras.h"
20 #include "llvm/ADT/SmallPtrSet.h"
21 #include "llvm/ADT/SmallVector.h"
22 #include "llvm/ADT/Statistic.h"
23 #include "llvm/ADT/Twine.h"
24 #include "llvm/Analysis/AssumptionCache.h"
25 #include "llvm/Analysis/InstructionSimplify.h"
26 #include "llvm/Analysis/IteratedDominanceFrontier.h"
27 #include "llvm/Analysis/ValueTracking.h"
28 #include "llvm/IR/BasicBlock.h"
29 #include "llvm/IR/CFG.h"
30 #include "llvm/IR/Constant.h"
31 #include "llvm/IR/Constants.h"
32 #include "llvm/IR/DIBuilder.h"
33 #include "llvm/IR/DebugInfo.h"
34 #include "llvm/IR/DebugProgramInstruction.h"
35 #include "llvm/IR/Dominators.h"
36 #include "llvm/IR/Function.h"
37 #include "llvm/IR/InstrTypes.h"
38 #include "llvm/IR/Instruction.h"
39 #include "llvm/IR/Instructions.h"
40 #include "llvm/IR/IntrinsicInst.h"
41 #include "llvm/IR/Intrinsics.h"
42 #include "llvm/IR/LLVMContext.h"
43 #include "llvm/IR/Module.h"
44 #include "llvm/IR/Operator.h"
45 #include "llvm/IR/Type.h"
46 #include "llvm/IR/User.h"
47 #include "llvm/Support/Casting.h"
48 #include "llvm/Transforms/Utils/Local.h"
49 #include "llvm/Transforms/Utils/PromoteMemToReg.h"
50 #include <algorithm>
51 #include <cassert>
52 #include <iterator>
53 #include <utility>
54 #include <vector>
55 
56 using namespace llvm;
57 
58 #define DEBUG_TYPE "mem2reg"
59 
60 STATISTIC(NumLocalPromoted, "Number of alloca's promoted within one block");
61 STATISTIC(NumSingleStore,   "Number of alloca's promoted with a single store");
62 STATISTIC(NumDeadAlloca,    "Number of dead alloca's removed");
63 STATISTIC(NumPHIInsert,     "Number of PHI nodes inserted");
64 
isAllocaPromotable(const AllocaInst * AI)65 bool llvm::isAllocaPromotable(const AllocaInst *AI) {
66   // Only allow direct and non-volatile loads and stores...
67   for (const User *U : AI->users()) {
68     if (const LoadInst *LI = dyn_cast<LoadInst>(U)) {
69       // Note that atomic loads can be transformed; atomic semantics do
70       // not have any meaning for a local alloca.
71       if (LI->isVolatile() || LI->getType() != AI->getAllocatedType())
72         return false;
73     } else if (const StoreInst *SI = dyn_cast<StoreInst>(U)) {
74       if (SI->getValueOperand() == AI ||
75           SI->getValueOperand()->getType() != AI->getAllocatedType())
76         return false; // Don't allow a store OF the AI, only INTO the AI.
77       // Note that atomic stores can be transformed; atomic semantics do
78       // not have any meaning for a local alloca.
79       if (SI->isVolatile())
80         return false;
81     } else if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(U)) {
82       if (!II->isLifetimeStartOrEnd() && !II->isDroppable())
83         return false;
84     } else if (const BitCastInst *BCI = dyn_cast<BitCastInst>(U)) {
85       if (!onlyUsedByLifetimeMarkersOrDroppableInsts(BCI))
86         return false;
87     } else if (const GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(U)) {
88       if (!GEPI->hasAllZeroIndices())
89         return false;
90       if (!onlyUsedByLifetimeMarkersOrDroppableInsts(GEPI))
91         return false;
92     } else if (const AddrSpaceCastInst *ASCI = dyn_cast<AddrSpaceCastInst>(U)) {
93       if (!onlyUsedByLifetimeMarkers(ASCI))
94         return false;
95     } else {
96       return false;
97     }
98   }
99 
100   return true;
101 }
102 
103 namespace {
104 
createDebugValue(DIBuilder & DIB,Value * NewValue,DILocalVariable * Variable,DIExpression * Expression,const DILocation * DI,DbgVariableRecord * InsertBefore)105 static void createDebugValue(DIBuilder &DIB, Value *NewValue,
106                              DILocalVariable *Variable,
107                              DIExpression *Expression, const DILocation *DI,
108                              DbgVariableRecord *InsertBefore) {
109   // FIXME: Merge these two functions now that DIBuilder supports
110   // DbgVariableRecords. We neeed the API to accept DbgVariableRecords as an
111   // insert point for that to work.
112   (void)DIB;
113   DbgVariableRecord::createDbgVariableRecord(NewValue, Variable, Expression, DI,
114                                              *InsertBefore);
115 }
createDebugValue(DIBuilder & DIB,Value * NewValue,DILocalVariable * Variable,DIExpression * Expression,const DILocation * DI,Instruction * InsertBefore)116 static void createDebugValue(DIBuilder &DIB, Value *NewValue,
117                              DILocalVariable *Variable,
118                              DIExpression *Expression, const DILocation *DI,
119                              Instruction *InsertBefore) {
120   DIB.insertDbgValueIntrinsic(NewValue, Variable, Expression, DI, InsertBefore);
121 }
122 
123 /// Helper for updating assignment tracking debug info when promoting allocas.
124 class AssignmentTrackingInfo {
125   /// DbgAssignIntrinsics linked to the alloca with at most one per variable
126   /// fragment. (i.e. not be a comprehensive set if there are multiple
127   /// dbg.assigns for one variable fragment).
128   SmallVector<DbgVariableIntrinsic *> DbgAssigns;
129   SmallVector<DbgVariableRecord *> DVRAssigns;
130 
131 public:
init(AllocaInst * AI)132   void init(AllocaInst *AI) {
133     SmallSet<DebugVariable, 2> Vars;
134     for (DbgAssignIntrinsic *DAI : at::getAssignmentMarkers(AI)) {
135       if (Vars.insert(DebugVariable(DAI)).second)
136         DbgAssigns.push_back(DAI);
137     }
138     for (DbgVariableRecord *DVR : at::getDVRAssignmentMarkers(AI)) {
139       if (Vars.insert(DebugVariable(DVR)).second)
140         DVRAssigns.push_back(DVR);
141     }
142   }
143 
144   /// Update assignment tracking debug info given for the to-be-deleted store
145   /// \p ToDelete that stores to this alloca.
updateForDeletedStore(StoreInst * ToDelete,DIBuilder & DIB,SmallSet<DbgAssignIntrinsic *,8> * DbgAssignsToDelete,SmallSet<DbgVariableRecord *,8> * DVRAssignsToDelete) const146   void updateForDeletedStore(
147       StoreInst *ToDelete, DIBuilder &DIB,
148       SmallSet<DbgAssignIntrinsic *, 8> *DbgAssignsToDelete,
149       SmallSet<DbgVariableRecord *, 8> *DVRAssignsToDelete) const {
150     // There's nothing to do if the alloca doesn't have any variables using
151     // assignment tracking.
152     if (DbgAssigns.empty() && DVRAssigns.empty())
153       return;
154 
155     // Insert a dbg.value where the linked dbg.assign is and remember to delete
156     // the dbg.assign later. Demoting to dbg.value isn't necessary for
157     // correctness but does reduce compile time and memory usage by reducing
158     // unnecessary function-local metadata. Remember that we've seen a
159     // dbg.assign for each variable fragment for the untracked store handling
160     // (after this loop).
161     SmallSet<DebugVariableAggregate, 2> VarHasDbgAssignForStore;
162     auto InsertValueForAssign = [&](auto *DbgAssign, auto *&AssignList) {
163       VarHasDbgAssignForStore.insert(DebugVariableAggregate(DbgAssign));
164       AssignList->insert(DbgAssign);
165       createDebugValue(DIB, DbgAssign->getValue(), DbgAssign->getVariable(),
166                        DbgAssign->getExpression(), DbgAssign->getDebugLoc(),
167                        DbgAssign);
168     };
169     for (auto *Assign : at::getAssignmentMarkers(ToDelete))
170       InsertValueForAssign(Assign, DbgAssignsToDelete);
171     for (auto *Assign : at::getDVRAssignmentMarkers(ToDelete))
172       InsertValueForAssign(Assign, DVRAssignsToDelete);
173 
174     // It's possible for variables using assignment tracking to have no
175     // dbg.assign linked to this store. These are variables in DbgAssigns that
176     // are missing from VarHasDbgAssignForStore. Since there isn't a dbg.assign
177     // to mark the assignment - and the store is going to be deleted - insert a
178     // dbg.value to do that now. An untracked store may be either one that
179     // cannot be represented using assignment tracking (non-const offset or
180     // size) or one that is trackable but has had its DIAssignID attachment
181     // dropped accidentally.
182     auto ConvertUnlinkedAssignToValue = [&](auto *Assign) {
183       if (VarHasDbgAssignForStore.contains(DebugVariableAggregate(Assign)))
184         return;
185       ConvertDebugDeclareToDebugValue(Assign, ToDelete, DIB);
186     };
187     for_each(DbgAssigns, ConvertUnlinkedAssignToValue);
188     for_each(DVRAssigns, ConvertUnlinkedAssignToValue);
189   }
190 
191   /// Update assignment tracking debug info given for the newly inserted PHI \p
192   /// NewPhi.
updateForNewPhi(PHINode * NewPhi,DIBuilder & DIB) const193   void updateForNewPhi(PHINode *NewPhi, DIBuilder &DIB) const {
194     // Regardless of the position of dbg.assigns relative to stores, the
195     // incoming values into a new PHI should be the same for the (imaginary)
196     // debug-phi.
197     for (auto *DAI : DbgAssigns)
198       ConvertDebugDeclareToDebugValue(DAI, NewPhi, DIB);
199     for (auto *DVR : DVRAssigns)
200       ConvertDebugDeclareToDebugValue(DVR, NewPhi, DIB);
201   }
202 
clear()203   void clear() {
204     DbgAssigns.clear();
205     DVRAssigns.clear();
206   }
empty()207   bool empty() { return DbgAssigns.empty() && DVRAssigns.empty(); }
208 };
209 
210 struct AllocaInfo {
211   using DbgUserVec = SmallVector<DbgVariableIntrinsic *, 1>;
212   using DPUserVec = SmallVector<DbgVariableRecord *, 1>;
213 
214   SmallVector<BasicBlock *, 32> DefiningBlocks;
215   SmallVector<BasicBlock *, 32> UsingBlocks;
216 
217   StoreInst *OnlyStore;
218   BasicBlock *OnlyBlock;
219   bool OnlyUsedInOneBlock;
220 
221   /// Debug users of the alloca - does not include dbg.assign intrinsics.
222   DbgUserVec DbgUsers;
223   DPUserVec DPUsers;
224   /// Helper to update assignment tracking debug info.
225   AssignmentTrackingInfo AssignmentTracking;
226 
clear__anon4233e3b50111::AllocaInfo227   void clear() {
228     DefiningBlocks.clear();
229     UsingBlocks.clear();
230     OnlyStore = nullptr;
231     OnlyBlock = nullptr;
232     OnlyUsedInOneBlock = true;
233     DbgUsers.clear();
234     DPUsers.clear();
235     AssignmentTracking.clear();
236   }
237 
238   /// Scan the uses of the specified alloca, filling in the AllocaInfo used
239   /// by the rest of the pass to reason about the uses of this alloca.
AnalyzeAlloca__anon4233e3b50111::AllocaInfo240   void AnalyzeAlloca(AllocaInst *AI) {
241     clear();
242 
243     // As we scan the uses of the alloca instruction, keep track of stores,
244     // and decide whether all of the loads and stores to the alloca are within
245     // the same basic block.
246     for (User *U : AI->users()) {
247       Instruction *User = cast<Instruction>(U);
248 
249       if (StoreInst *SI = dyn_cast<StoreInst>(User)) {
250         // Remember the basic blocks which define new values for the alloca
251         DefiningBlocks.push_back(SI->getParent());
252         OnlyStore = SI;
253       } else {
254         LoadInst *LI = cast<LoadInst>(User);
255         // Otherwise it must be a load instruction, keep track of variable
256         // reads.
257         UsingBlocks.push_back(LI->getParent());
258       }
259 
260       if (OnlyUsedInOneBlock) {
261         if (!OnlyBlock)
262           OnlyBlock = User->getParent();
263         else if (OnlyBlock != User->getParent())
264           OnlyUsedInOneBlock = false;
265       }
266     }
267     DbgUserVec AllDbgUsers;
268     SmallVector<DbgVariableRecord *> AllDPUsers;
269     findDbgUsers(AllDbgUsers, AI, &AllDPUsers);
270     std::copy_if(AllDbgUsers.begin(), AllDbgUsers.end(),
271                  std::back_inserter(DbgUsers), [](DbgVariableIntrinsic *DII) {
272                    return !isa<DbgAssignIntrinsic>(DII);
273                  });
274     std::copy_if(AllDPUsers.begin(), AllDPUsers.end(),
275                  std::back_inserter(DPUsers),
276                  [](DbgVariableRecord *DVR) { return !DVR->isDbgAssign(); });
277     AssignmentTracking.init(AI);
278   }
279 };
280 
281 /// Data package used by RenamePass().
282 struct RenamePassData {
283   using ValVector = std::vector<Value *>;
284   using LocationVector = std::vector<DebugLoc>;
285 
RenamePassData__anon4233e3b50111::RenamePassData286   RenamePassData(BasicBlock *B, BasicBlock *P, ValVector V, LocationVector L)
287       : BB(B), Pred(P), Values(std::move(V)), Locations(std::move(L)) {}
288 
289   BasicBlock *BB;
290   BasicBlock *Pred;
291   ValVector Values;
292   LocationVector Locations;
293 };
294 
295 /// This assigns and keeps a per-bb relative ordering of load/store
296 /// instructions in the block that directly load or store an alloca.
297 ///
298 /// This functionality is important because it avoids scanning large basic
299 /// blocks multiple times when promoting many allocas in the same block.
300 class LargeBlockInfo {
301   /// For each instruction that we track, keep the index of the
302   /// instruction.
303   ///
304   /// The index starts out as the number of the instruction from the start of
305   /// the block.
306   DenseMap<const Instruction *, unsigned> InstNumbers;
307 
308 public:
309 
310   /// This code only looks at accesses to allocas.
isInterestingInstruction(const Instruction * I)311   static bool isInterestingInstruction(const Instruction *I) {
312     return (isa<LoadInst>(I) && isa<AllocaInst>(I->getOperand(0))) ||
313            (isa<StoreInst>(I) && isa<AllocaInst>(I->getOperand(1)));
314   }
315 
316   /// Get or calculate the index of the specified instruction.
getInstructionIndex(const Instruction * I)317   unsigned getInstructionIndex(const Instruction *I) {
318     assert(isInterestingInstruction(I) &&
319            "Not a load/store to/from an alloca?");
320 
321     // If we already have this instruction number, return it.
322     DenseMap<const Instruction *, unsigned>::iterator It = InstNumbers.find(I);
323     if (It != InstNumbers.end())
324       return It->second;
325 
326     // Scan the whole block to get the instruction.  This accumulates
327     // information for every interesting instruction in the block, in order to
328     // avoid gratuitus rescans.
329     const BasicBlock *BB = I->getParent();
330     unsigned InstNo = 0;
331     for (const Instruction &BBI : *BB)
332       if (isInterestingInstruction(&BBI))
333         InstNumbers[&BBI] = InstNo++;
334     It = InstNumbers.find(I);
335 
336     assert(It != InstNumbers.end() && "Didn't insert instruction?");
337     return It->second;
338   }
339 
deleteValue(const Instruction * I)340   void deleteValue(const Instruction *I) { InstNumbers.erase(I); }
341 
clear()342   void clear() { InstNumbers.clear(); }
343 };
344 
345 struct PromoteMem2Reg {
346   /// The alloca instructions being promoted.
347   std::vector<AllocaInst *> Allocas;
348 
349   DominatorTree &DT;
350   DIBuilder DIB;
351 
352   /// A cache of @llvm.assume intrinsics used by SimplifyInstruction.
353   AssumptionCache *AC;
354 
355   const SimplifyQuery SQ;
356 
357   /// Reverse mapping of Allocas.
358   DenseMap<AllocaInst *, unsigned> AllocaLookup;
359 
360   /// The PhiNodes we're adding.
361   ///
362   /// That map is used to simplify some Phi nodes as we iterate over it, so
363   /// it should have deterministic iterators.  We could use a MapVector, but
364   /// since we already maintain a map from BasicBlock* to a stable numbering
365   /// (BBNumbers), the DenseMap is more efficient (also supports removal).
366   DenseMap<std::pair<unsigned, unsigned>, PHINode *> NewPhiNodes;
367 
368   /// For each PHI node, keep track of which entry in Allocas it corresponds
369   /// to.
370   DenseMap<PHINode *, unsigned> PhiToAllocaMap;
371 
372   /// For each alloca, we keep track of the dbg.declare intrinsic that
373   /// describes it, if any, so that we can convert it to a dbg.value
374   /// intrinsic if the alloca gets promoted.
375   SmallVector<AllocaInfo::DbgUserVec, 8> AllocaDbgUsers;
376   SmallVector<AllocaInfo::DPUserVec, 8> AllocaDPUsers;
377 
378   /// For each alloca, keep an instance of a helper class that gives us an easy
379   /// way to update assignment tracking debug info if the alloca is promoted.
380   SmallVector<AssignmentTrackingInfo, 8> AllocaATInfo;
381   /// A set of dbg.assigns to delete because they've been demoted to
382   /// dbg.values. Call cleanUpDbgAssigns to delete them.
383   SmallSet<DbgAssignIntrinsic *, 8> DbgAssignsToDelete;
384   SmallSet<DbgVariableRecord *, 8> DVRAssignsToDelete;
385 
386   /// The set of basic blocks the renamer has already visited.
387   SmallPtrSet<BasicBlock *, 16> Visited;
388 
389   /// Contains a stable numbering of basic blocks to avoid non-determinstic
390   /// behavior.
391   DenseMap<BasicBlock *, unsigned> BBNumbers;
392 
393   /// Lazily compute the number of predecessors a block has.
394   DenseMap<const BasicBlock *, unsigned> BBNumPreds;
395 
396   /// Whether the function has the no-signed-zeros-fp-math attribute set.
397   bool NoSignedZeros = false;
398 
399 public:
PromoteMem2Reg__anon4233e3b50111::PromoteMem2Reg400   PromoteMem2Reg(ArrayRef<AllocaInst *> Allocas, DominatorTree &DT,
401                  AssumptionCache *AC)
402       : Allocas(Allocas.begin(), Allocas.end()), DT(DT),
403         DIB(*DT.getRoot()->getParent()->getParent(), /*AllowUnresolved*/ false),
404         AC(AC), SQ(DT.getRoot()->getDataLayout(),
405                    nullptr, &DT, AC) {}
406 
407   void run();
408 
409 private:
RemoveFromAllocasList__anon4233e3b50111::PromoteMem2Reg410   void RemoveFromAllocasList(unsigned &AllocaIdx) {
411     Allocas[AllocaIdx] = Allocas.back();
412     Allocas.pop_back();
413     --AllocaIdx;
414   }
415 
getNumPreds__anon4233e3b50111::PromoteMem2Reg416   unsigned getNumPreds(const BasicBlock *BB) {
417     unsigned &NP = BBNumPreds[BB];
418     if (NP == 0)
419       NP = pred_size(BB) + 1;
420     return NP - 1;
421   }
422 
423   void ComputeLiveInBlocks(AllocaInst *AI, AllocaInfo &Info,
424                            const SmallPtrSetImpl<BasicBlock *> &DefBlocks,
425                            SmallPtrSetImpl<BasicBlock *> &LiveInBlocks);
426   void RenamePass(BasicBlock *BB, BasicBlock *Pred,
427                   RenamePassData::ValVector &IncVals,
428                   RenamePassData::LocationVector &IncLocs,
429                   std::vector<RenamePassData> &Worklist);
430   bool QueuePhiNode(BasicBlock *BB, unsigned AllocaIdx, unsigned &Version);
431 
432   /// Delete dbg.assigns that have been demoted to dbg.values.
cleanUpDbgAssigns__anon4233e3b50111::PromoteMem2Reg433   void cleanUpDbgAssigns() {
434     for (auto *DAI : DbgAssignsToDelete)
435       DAI->eraseFromParent();
436     DbgAssignsToDelete.clear();
437     for (auto *DVR : DVRAssignsToDelete)
438       DVR->eraseFromParent();
439     DVRAssignsToDelete.clear();
440   }
441 };
442 
443 } // end anonymous namespace
444 
445 /// Given a LoadInst LI this adds assume(LI != null) after it.
addAssumeNonNull(AssumptionCache * AC,LoadInst * LI)446 static void addAssumeNonNull(AssumptionCache *AC, LoadInst *LI) {
447   Function *AssumeIntrinsic =
448       Intrinsic::getDeclaration(LI->getModule(), Intrinsic::assume);
449   ICmpInst *LoadNotNull = new ICmpInst(ICmpInst::ICMP_NE, LI,
450                                        Constant::getNullValue(LI->getType()));
451   LoadNotNull->insertAfter(LI);
452   CallInst *CI = CallInst::Create(AssumeIntrinsic, {LoadNotNull});
453   CI->insertAfter(LoadNotNull);
454   AC->registerAssumption(cast<AssumeInst>(CI));
455 }
456 
convertMetadataToAssumes(LoadInst * LI,Value * Val,const DataLayout & DL,AssumptionCache * AC,const DominatorTree * DT)457 static void convertMetadataToAssumes(LoadInst *LI, Value *Val,
458                                      const DataLayout &DL, AssumptionCache *AC,
459                                      const DominatorTree *DT) {
460   if (isa<UndefValue>(Val) && LI->hasMetadata(LLVMContext::MD_noundef)) {
461     // Insert non-terminator unreachable.
462     LLVMContext &Ctx = LI->getContext();
463     new StoreInst(ConstantInt::getTrue(Ctx),
464                   PoisonValue::get(PointerType::getUnqual(Ctx)),
465                   /*isVolatile=*/false, Align(1), LI);
466     return;
467   }
468 
469   // If the load was marked as nonnull we don't want to lose that information
470   // when we erase this Load. So we preserve it with an assume. As !nonnull
471   // returns poison while assume violations are immediate undefined behavior,
472   // we can only do this if the value is known non-poison.
473   if (AC && LI->getMetadata(LLVMContext::MD_nonnull) &&
474       LI->getMetadata(LLVMContext::MD_noundef) &&
475       !isKnownNonZero(Val, SimplifyQuery(DL, DT, AC, LI)))
476     addAssumeNonNull(AC, LI);
477 }
478 
removeIntrinsicUsers(AllocaInst * AI)479 static void removeIntrinsicUsers(AllocaInst *AI) {
480   // Knowing that this alloca is promotable, we know that it's safe to kill all
481   // instructions except for load and store.
482 
483   for (Use &U : llvm::make_early_inc_range(AI->uses())) {
484     Instruction *I = cast<Instruction>(U.getUser());
485     if (isa<LoadInst>(I) || isa<StoreInst>(I))
486       continue;
487 
488     // Drop the use of AI in droppable instructions.
489     if (I->isDroppable()) {
490       I->dropDroppableUse(U);
491       continue;
492     }
493 
494     if (!I->getType()->isVoidTy()) {
495       // The only users of this bitcast/GEP instruction are lifetime intrinsics.
496       // Follow the use/def chain to erase them now instead of leaving it for
497       // dead code elimination later.
498       for (Use &UU : llvm::make_early_inc_range(I->uses())) {
499         Instruction *Inst = cast<Instruction>(UU.getUser());
500 
501         // Drop the use of I in droppable instructions.
502         if (Inst->isDroppable()) {
503           Inst->dropDroppableUse(UU);
504           continue;
505         }
506         Inst->eraseFromParent();
507       }
508     }
509     I->eraseFromParent();
510   }
511 }
512 
513 /// Rewrite as many loads as possible given a single store.
514 ///
515 /// When there is only a single store, we can use the domtree to trivially
516 /// replace all of the dominated loads with the stored value. Do so, and return
517 /// true if this has successfully promoted the alloca entirely. If this returns
518 /// false there were some loads which were not dominated by the single store
519 /// and thus must be phi-ed with undef. We fall back to the standard alloca
520 /// promotion algorithm in that case.
521 static bool
rewriteSingleStoreAlloca(AllocaInst * AI,AllocaInfo & Info,LargeBlockInfo & LBI,const DataLayout & DL,DominatorTree & DT,AssumptionCache * AC,SmallSet<DbgAssignIntrinsic *,8> * DbgAssignsToDelete,SmallSet<DbgVariableRecord *,8> * DVRAssignsToDelete)522 rewriteSingleStoreAlloca(AllocaInst *AI, AllocaInfo &Info, LargeBlockInfo &LBI,
523                          const DataLayout &DL, DominatorTree &DT,
524                          AssumptionCache *AC,
525                          SmallSet<DbgAssignIntrinsic *, 8> *DbgAssignsToDelete,
526                          SmallSet<DbgVariableRecord *, 8> *DVRAssignsToDelete) {
527   StoreInst *OnlyStore = Info.OnlyStore;
528   Value *ReplVal = OnlyStore->getOperand(0);
529   // Loads may either load the stored value or uninitialized memory (undef).
530   // If the stored value may be poison, then replacing an uninitialized memory
531   // load with it would be incorrect. If the store dominates the load, we know
532   // it is always initialized.
533   bool RequireDominatingStore =
534       isa<Instruction>(ReplVal) || !isGuaranteedNotToBePoison(ReplVal);
535   BasicBlock *StoreBB = OnlyStore->getParent();
536   int StoreIndex = -1;
537 
538   // Clear out UsingBlocks.  We will reconstruct it here if needed.
539   Info.UsingBlocks.clear();
540 
541   for (User *U : make_early_inc_range(AI->users())) {
542     Instruction *UserInst = cast<Instruction>(U);
543     if (UserInst == OnlyStore)
544       continue;
545     LoadInst *LI = cast<LoadInst>(UserInst);
546 
547     // Okay, if we have a load from the alloca, we want to replace it with the
548     // only value stored to the alloca.  We can do this if the value is
549     // dominated by the store.  If not, we use the rest of the mem2reg machinery
550     // to insert the phi nodes as needed.
551     if (RequireDominatingStore) {
552       if (LI->getParent() == StoreBB) {
553         // If we have a use that is in the same block as the store, compare the
554         // indices of the two instructions to see which one came first.  If the
555         // load came before the store, we can't handle it.
556         if (StoreIndex == -1)
557           StoreIndex = LBI.getInstructionIndex(OnlyStore);
558 
559         if (unsigned(StoreIndex) > LBI.getInstructionIndex(LI)) {
560           // Can't handle this load, bail out.
561           Info.UsingBlocks.push_back(StoreBB);
562           continue;
563         }
564       } else if (!DT.dominates(StoreBB, LI->getParent())) {
565         // If the load and store are in different blocks, use BB dominance to
566         // check their relationships.  If the store doesn't dom the use, bail
567         // out.
568         Info.UsingBlocks.push_back(LI->getParent());
569         continue;
570       }
571     }
572 
573     // Otherwise, we *can* safely rewrite this load.
574     // If the replacement value is the load, this must occur in unreachable
575     // code.
576     if (ReplVal == LI)
577       ReplVal = PoisonValue::get(LI->getType());
578 
579     convertMetadataToAssumes(LI, ReplVal, DL, AC, &DT);
580     LI->replaceAllUsesWith(ReplVal);
581     LI->eraseFromParent();
582     LBI.deleteValue(LI);
583   }
584 
585   // Finally, after the scan, check to see if the store is all that is left.
586   if (!Info.UsingBlocks.empty())
587     return false; // If not, we'll have to fall back for the remainder.
588 
589   DIBuilder DIB(*AI->getModule(), /*AllowUnresolved*/ false);
590   // Update assignment tracking info for the store we're going to delete.
591   Info.AssignmentTracking.updateForDeletedStore(
592       Info.OnlyStore, DIB, DbgAssignsToDelete, DVRAssignsToDelete);
593 
594   // Record debuginfo for the store and remove the declaration's
595   // debuginfo.
596   auto ConvertDebugInfoForStore = [&](auto &Container) {
597     for (auto *DbgItem : Container) {
598       if (DbgItem->isAddressOfVariable()) {
599         ConvertDebugDeclareToDebugValue(DbgItem, Info.OnlyStore, DIB);
600         DbgItem->eraseFromParent();
601       } else if (DbgItem->getExpression()->startsWithDeref()) {
602         DbgItem->eraseFromParent();
603       }
604     }
605   };
606   ConvertDebugInfoForStore(Info.DbgUsers);
607   ConvertDebugInfoForStore(Info.DPUsers);
608 
609   // Remove dbg.assigns linked to the alloca as these are now redundant.
610   at::deleteAssignmentMarkers(AI);
611 
612   // Remove the (now dead) store and alloca.
613   Info.OnlyStore->eraseFromParent();
614   LBI.deleteValue(Info.OnlyStore);
615 
616   AI->eraseFromParent();
617   return true;
618 }
619 
620 /// Many allocas are only used within a single basic block.  If this is the
621 /// case, avoid traversing the CFG and inserting a lot of potentially useless
622 /// PHI nodes by just performing a single linear pass over the basic block
623 /// using the Alloca.
624 ///
625 /// If we cannot promote this alloca (because it is read before it is written),
626 /// return false.  This is necessary in cases where, due to control flow, the
627 /// alloca is undefined only on some control flow paths.  e.g. code like
628 /// this is correct in LLVM IR:
629 ///  // A is an alloca with no stores so far
630 ///  for (...) {
631 ///    int t = *A;
632 ///    if (!first_iteration)
633 ///      use(t);
634 ///    *A = 42;
635 ///  }
636 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)637 promoteSingleBlockAlloca(AllocaInst *AI, const AllocaInfo &Info,
638                          LargeBlockInfo &LBI, const DataLayout &DL,
639                          DominatorTree &DT, AssumptionCache *AC,
640                          SmallSet<DbgAssignIntrinsic *, 8> *DbgAssignsToDelete,
641                          SmallSet<DbgVariableRecord *, 8> *DVRAssignsToDelete) {
642   // The trickiest case to handle is when we have large blocks. Because of this,
643   // this code is optimized assuming that large blocks happen.  This does not
644   // significantly pessimize the small block case.  This uses LargeBlockInfo to
645   // make it efficient to get the index of various operations in the block.
646 
647   // Walk the use-def list of the alloca, getting the locations of all stores.
648   using StoresByIndexTy = SmallVector<std::pair<unsigned, StoreInst *>, 64>;
649   StoresByIndexTy StoresByIndex;
650 
651   for (User *U : AI->users())
652     if (StoreInst *SI = dyn_cast<StoreInst>(U))
653       StoresByIndex.push_back(std::make_pair(LBI.getInstructionIndex(SI), SI));
654 
655   // Sort the stores by their index, making it efficient to do a lookup with a
656   // binary search.
657   llvm::sort(StoresByIndex, less_first());
658 
659   // Walk all of the loads from this alloca, replacing them with the nearest
660   // store above them, if any.
661   for (User *U : make_early_inc_range(AI->users())) {
662     LoadInst *LI = dyn_cast<LoadInst>(U);
663     if (!LI)
664       continue;
665 
666     unsigned LoadIdx = LBI.getInstructionIndex(LI);
667 
668     // Find the nearest store that has a lower index than this load.
669     StoresByIndexTy::iterator I = llvm::lower_bound(
670         StoresByIndex,
671         std::make_pair(LoadIdx, static_cast<StoreInst *>(nullptr)),
672         less_first());
673     Value *ReplVal;
674     if (I == StoresByIndex.begin()) {
675       if (StoresByIndex.empty())
676         // If there are no stores, the load takes the undef value.
677         ReplVal = UndefValue::get(LI->getType());
678       else
679         // There is no store before this load, bail out (load may be affected
680         // by the following stores - see main comment).
681         return false;
682     } else {
683       // Otherwise, there was a store before this load, the load takes its
684       // value.
685       ReplVal = std::prev(I)->second->getOperand(0);
686     }
687 
688     convertMetadataToAssumes(LI, ReplVal, DL, AC, &DT);
689 
690     // If the replacement value is the load, this must occur in unreachable
691     // code.
692     if (ReplVal == LI)
693       ReplVal = PoisonValue::get(LI->getType());
694 
695     LI->replaceAllUsesWith(ReplVal);
696     LI->eraseFromParent();
697     LBI.deleteValue(LI);
698   }
699 
700   // Remove the (now dead) stores and alloca.
701   DIBuilder DIB(*AI->getModule(), /*AllowUnresolved*/ false);
702   while (!AI->use_empty()) {
703     StoreInst *SI = cast<StoreInst>(AI->user_back());
704     // Update assignment tracking info for the store we're going to delete.
705     Info.AssignmentTracking.updateForDeletedStore(SI, DIB, DbgAssignsToDelete,
706                                                   DVRAssignsToDelete);
707     // Record debuginfo for the store before removing it.
708     auto DbgUpdateForStore = [&](auto &Container) {
709       for (auto *DbgItem : Container) {
710         if (DbgItem->isAddressOfVariable()) {
711           ConvertDebugDeclareToDebugValue(DbgItem, SI, DIB);
712         }
713       }
714     };
715     DbgUpdateForStore(Info.DbgUsers);
716     DbgUpdateForStore(Info.DPUsers);
717 
718     SI->eraseFromParent();
719     LBI.deleteValue(SI);
720   }
721 
722   // Remove dbg.assigns linked to the alloca as these are now redundant.
723   at::deleteAssignmentMarkers(AI);
724   AI->eraseFromParent();
725 
726   // The alloca's debuginfo can be removed as well.
727   auto DbgUpdateForAlloca = [&](auto &Container) {
728     for (auto *DbgItem : Container)
729       if (DbgItem->isAddressOfVariable() ||
730           DbgItem->getExpression()->startsWithDeref())
731         DbgItem->eraseFromParent();
732   };
733   DbgUpdateForAlloca(Info.DbgUsers);
734   DbgUpdateForAlloca(Info.DPUsers);
735 
736   ++NumLocalPromoted;
737   return true;
738 }
739 
run()740 void PromoteMem2Reg::run() {
741   Function &F = *DT.getRoot()->getParent();
742 
743   AllocaDbgUsers.resize(Allocas.size());
744   AllocaATInfo.resize(Allocas.size());
745   AllocaDPUsers.resize(Allocas.size());
746 
747   AllocaInfo Info;
748   LargeBlockInfo LBI;
749   ForwardIDFCalculator IDF(DT);
750 
751   NoSignedZeros = F.getFnAttribute("no-signed-zeros-fp-math").getValueAsBool();
752 
753   for (unsigned AllocaNum = 0; AllocaNum != Allocas.size(); ++AllocaNum) {
754     AllocaInst *AI = Allocas[AllocaNum];
755 
756     assert(isAllocaPromotable(AI) && "Cannot promote non-promotable alloca!");
757     assert(AI->getParent()->getParent() == &F &&
758            "All allocas should be in the same function, which is same as DF!");
759 
760     removeIntrinsicUsers(AI);
761 
762     if (AI->use_empty()) {
763       // If there are no uses of the alloca, just delete it now.
764       AI->eraseFromParent();
765 
766       // Remove the alloca from the Allocas list, since it has been processed
767       RemoveFromAllocasList(AllocaNum);
768       ++NumDeadAlloca;
769       continue;
770     }
771 
772     // Calculate the set of read and write-locations for each alloca.  This is
773     // analogous to finding the 'uses' and 'definitions' of each variable.
774     Info.AnalyzeAlloca(AI);
775 
776     // If there is only a single store to this value, replace any loads of
777     // it that are directly dominated by the definition with the value stored.
778     if (Info.DefiningBlocks.size() == 1) {
779       if (rewriteSingleStoreAlloca(AI, Info, LBI, SQ.DL, DT, AC,
780                                    &DbgAssignsToDelete, &DVRAssignsToDelete)) {
781         // The alloca has been processed, move on.
782         RemoveFromAllocasList(AllocaNum);
783         ++NumSingleStore;
784         continue;
785       }
786     }
787 
788     // If the alloca is only read and written in one basic block, just perform a
789     // linear sweep over the block to eliminate it.
790     if (Info.OnlyUsedInOneBlock &&
791         promoteSingleBlockAlloca(AI, Info, LBI, SQ.DL, DT, AC,
792                                  &DbgAssignsToDelete, &DVRAssignsToDelete)) {
793       // The alloca has been processed, move on.
794       RemoveFromAllocasList(AllocaNum);
795       continue;
796     }
797 
798     // If we haven't computed a numbering for the BB's in the function, do so
799     // now.
800     if (BBNumbers.empty()) {
801       unsigned ID = 0;
802       for (auto &BB : F)
803         BBNumbers[&BB] = ID++;
804     }
805 
806     // Remember the dbg.declare intrinsic describing this alloca, if any.
807     if (!Info.DbgUsers.empty())
808       AllocaDbgUsers[AllocaNum] = Info.DbgUsers;
809     if (!Info.AssignmentTracking.empty())
810       AllocaATInfo[AllocaNum] = Info.AssignmentTracking;
811     if (!Info.DPUsers.empty())
812       AllocaDPUsers[AllocaNum] = Info.DPUsers;
813 
814     // Keep the reverse mapping of the 'Allocas' array for the rename pass.
815     AllocaLookup[Allocas[AllocaNum]] = AllocaNum;
816 
817     // Unique the set of defining blocks for efficient lookup.
818     SmallPtrSet<BasicBlock *, 32> DefBlocks(Info.DefiningBlocks.begin(),
819                                             Info.DefiningBlocks.end());
820 
821     // Determine which blocks the value is live in.  These are blocks which lead
822     // to uses.
823     SmallPtrSet<BasicBlock *, 32> LiveInBlocks;
824     ComputeLiveInBlocks(AI, Info, DefBlocks, LiveInBlocks);
825 
826     // At this point, we're committed to promoting the alloca using IDF's, and
827     // the standard SSA construction algorithm.  Determine which blocks need phi
828     // nodes and see if we can optimize out some work by avoiding insertion of
829     // dead phi nodes.
830     IDF.setLiveInBlocks(LiveInBlocks);
831     IDF.setDefiningBlocks(DefBlocks);
832     SmallVector<BasicBlock *, 32> PHIBlocks;
833     IDF.calculate(PHIBlocks);
834     llvm::sort(PHIBlocks, [this](BasicBlock *A, BasicBlock *B) {
835       return BBNumbers.find(A)->second < BBNumbers.find(B)->second;
836     });
837 
838     unsigned CurrentVersion = 0;
839     for (BasicBlock *BB : PHIBlocks)
840       QueuePhiNode(BB, AllocaNum, CurrentVersion);
841   }
842 
843   if (Allocas.empty()) {
844     cleanUpDbgAssigns();
845     return; // All of the allocas must have been trivial!
846   }
847   LBI.clear();
848 
849   // Set the incoming values for the basic block to be null values for all of
850   // the alloca's.  We do this in case there is a load of a value that has not
851   // been stored yet.  In this case, it will get this null value.
852   RenamePassData::ValVector Values(Allocas.size());
853   for (unsigned i = 0, e = Allocas.size(); i != e; ++i)
854     Values[i] = UndefValue::get(Allocas[i]->getAllocatedType());
855 
856   // When handling debug info, treat all incoming values as if they have unknown
857   // locations until proven otherwise.
858   RenamePassData::LocationVector Locations(Allocas.size());
859 
860   // Walks all basic blocks in the function performing the SSA rename algorithm
861   // and inserting the phi nodes we marked as necessary
862   std::vector<RenamePassData> RenamePassWorkList;
863   RenamePassWorkList.emplace_back(&F.front(), nullptr, std::move(Values),
864                                   std::move(Locations));
865   do {
866     RenamePassData RPD = std::move(RenamePassWorkList.back());
867     RenamePassWorkList.pop_back();
868     // RenamePass may add new worklist entries.
869     RenamePass(RPD.BB, RPD.Pred, RPD.Values, RPD.Locations, RenamePassWorkList);
870   } while (!RenamePassWorkList.empty());
871 
872   // The renamer uses the Visited set to avoid infinite loops.  Clear it now.
873   Visited.clear();
874 
875   // Remove the allocas themselves from the function.
876   for (Instruction *A : Allocas) {
877     // Remove dbg.assigns linked to the alloca as these are now redundant.
878     at::deleteAssignmentMarkers(A);
879     // If there are any uses of the alloca instructions left, they must be in
880     // unreachable basic blocks that were not processed by walking the dominator
881     // tree. Just delete the users now.
882     if (!A->use_empty())
883       A->replaceAllUsesWith(PoisonValue::get(A->getType()));
884     A->eraseFromParent();
885   }
886 
887   // Remove alloca's dbg.declare intrinsics from the function.
888   auto RemoveDbgDeclares = [&](auto &Container) {
889     for (auto &DbgUsers : Container) {
890       for (auto *DbgItem : DbgUsers)
891         if (DbgItem->isAddressOfVariable() ||
892             DbgItem->getExpression()->startsWithDeref())
893           DbgItem->eraseFromParent();
894     }
895   };
896   RemoveDbgDeclares(AllocaDbgUsers);
897   RemoveDbgDeclares(AllocaDPUsers);
898 
899   // Loop over all of the PHI nodes and see if there are any that we can get
900   // rid of because they merge all of the same incoming values.  This can
901   // happen due to undef values coming into the PHI nodes.  This process is
902   // iterative, because eliminating one PHI node can cause others to be removed.
903   bool EliminatedAPHI = true;
904   while (EliminatedAPHI) {
905     EliminatedAPHI = false;
906 
907     // Iterating over NewPhiNodes is deterministic, so it is safe to try to
908     // simplify and RAUW them as we go.  If it was not, we could add uses to
909     // the values we replace with in a non-deterministic order, thus creating
910     // non-deterministic def->use chains.
911     for (DenseMap<std::pair<unsigned, unsigned>, PHINode *>::iterator
912              I = NewPhiNodes.begin(),
913              E = NewPhiNodes.end();
914          I != E;) {
915       PHINode *PN = I->second;
916 
917       // If this PHI node merges one value and/or undefs, get the value.
918       if (Value *V = simplifyInstruction(PN, SQ)) {
919         PN->replaceAllUsesWith(V);
920         PN->eraseFromParent();
921         NewPhiNodes.erase(I++);
922         EliminatedAPHI = true;
923         continue;
924       }
925       ++I;
926     }
927   }
928 
929   // At this point, the renamer has added entries to PHI nodes for all reachable
930   // code.  Unfortunately, there may be unreachable blocks which the renamer
931   // hasn't traversed.  If this is the case, the PHI nodes may not
932   // have incoming values for all predecessors.  Loop over all PHI nodes we have
933   // created, inserting poison values if they are missing any incoming values.
934   for (DenseMap<std::pair<unsigned, unsigned>, PHINode *>::iterator
935            I = NewPhiNodes.begin(),
936            E = NewPhiNodes.end();
937        I != E; ++I) {
938     // We want to do this once per basic block.  As such, only process a block
939     // when we find the PHI that is the first entry in the block.
940     PHINode *SomePHI = I->second;
941     BasicBlock *BB = SomePHI->getParent();
942     if (&BB->front() != SomePHI)
943       continue;
944 
945     // Only do work here if there the PHI nodes are missing incoming values.  We
946     // know that all PHI nodes that were inserted in a block will have the same
947     // number of incoming values, so we can just check any of them.
948     if (SomePHI->getNumIncomingValues() == getNumPreds(BB))
949       continue;
950 
951     // Get the preds for BB.
952     SmallVector<BasicBlock *, 16> Preds(predecessors(BB));
953 
954     // Ok, now we know that all of the PHI nodes are missing entries for some
955     // basic blocks.  Start by sorting the incoming predecessors for efficient
956     // access.
957     auto CompareBBNumbers = [this](BasicBlock *A, BasicBlock *B) {
958       return BBNumbers.find(A)->second < BBNumbers.find(B)->second;
959     };
960     llvm::sort(Preds, CompareBBNumbers);
961 
962     // Now we loop through all BB's which have entries in SomePHI and remove
963     // them from the Preds list.
964     for (unsigned i = 0, e = SomePHI->getNumIncomingValues(); i != e; ++i) {
965       // Do a log(n) search of the Preds list for the entry we want.
966       SmallVectorImpl<BasicBlock *>::iterator EntIt = llvm::lower_bound(
967           Preds, SomePHI->getIncomingBlock(i), CompareBBNumbers);
968       assert(EntIt != Preds.end() && *EntIt == SomePHI->getIncomingBlock(i) &&
969              "PHI node has entry for a block which is not a predecessor!");
970 
971       // Remove the entry
972       Preds.erase(EntIt);
973     }
974 
975     // At this point, the blocks left in the preds list must have dummy
976     // entries inserted into every PHI nodes for the block.  Update all the phi
977     // nodes in this block that we are inserting (there could be phis before
978     // mem2reg runs).
979     unsigned NumBadPreds = SomePHI->getNumIncomingValues();
980     BasicBlock::iterator BBI = BB->begin();
981     while ((SomePHI = dyn_cast<PHINode>(BBI++)) &&
982            SomePHI->getNumIncomingValues() == NumBadPreds) {
983       Value *PoisonVal = PoisonValue::get(SomePHI->getType());
984       for (BasicBlock *Pred : Preds)
985         SomePHI->addIncoming(PoisonVal, Pred);
986     }
987   }
988 
989   NewPhiNodes.clear();
990   cleanUpDbgAssigns();
991 }
992 
993 /// Determine which blocks the value is live in.
994 ///
995 /// These are blocks which lead to uses.  Knowing this allows us to avoid
996 /// inserting PHI nodes into blocks which don't lead to uses (thus, the
997 /// inserted phi nodes would be dead).
ComputeLiveInBlocks(AllocaInst * AI,AllocaInfo & Info,const SmallPtrSetImpl<BasicBlock * > & DefBlocks,SmallPtrSetImpl<BasicBlock * > & LiveInBlocks)998 void PromoteMem2Reg::ComputeLiveInBlocks(
999     AllocaInst *AI, AllocaInfo &Info,
1000     const SmallPtrSetImpl<BasicBlock *> &DefBlocks,
1001     SmallPtrSetImpl<BasicBlock *> &LiveInBlocks) {
1002   // To determine liveness, we must iterate through the predecessors of blocks
1003   // where the def is live.  Blocks are added to the worklist if we need to
1004   // check their predecessors.  Start with all the using blocks.
1005   SmallVector<BasicBlock *, 64> LiveInBlockWorklist(Info.UsingBlocks.begin(),
1006                                                     Info.UsingBlocks.end());
1007 
1008   // If any of the using blocks is also a definition block, check to see if the
1009   // definition occurs before or after the use.  If it happens before the use,
1010   // the value isn't really live-in.
1011   for (unsigned i = 0, e = LiveInBlockWorklist.size(); i != e; ++i) {
1012     BasicBlock *BB = LiveInBlockWorklist[i];
1013     if (!DefBlocks.count(BB))
1014       continue;
1015 
1016     // Okay, this is a block that both uses and defines the value.  If the first
1017     // reference to the alloca is a def (store), then we know it isn't live-in.
1018     for (BasicBlock::iterator I = BB->begin();; ++I) {
1019       if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
1020         if (SI->getOperand(1) != AI)
1021           continue;
1022 
1023         // We found a store to the alloca before a load.  The alloca is not
1024         // actually live-in here.
1025         LiveInBlockWorklist[i] = LiveInBlockWorklist.back();
1026         LiveInBlockWorklist.pop_back();
1027         --i;
1028         --e;
1029         break;
1030       }
1031 
1032       if (LoadInst *LI = dyn_cast<LoadInst>(I))
1033         // Okay, we found a load before a store to the alloca.  It is actually
1034         // live into this block.
1035         if (LI->getOperand(0) == AI)
1036           break;
1037     }
1038   }
1039 
1040   // Now that we have a set of blocks where the phi is live-in, recursively add
1041   // their predecessors until we find the full region the value is live.
1042   while (!LiveInBlockWorklist.empty()) {
1043     BasicBlock *BB = LiveInBlockWorklist.pop_back_val();
1044 
1045     // The block really is live in here, insert it into the set.  If already in
1046     // the set, then it has already been processed.
1047     if (!LiveInBlocks.insert(BB).second)
1048       continue;
1049 
1050     // Since the value is live into BB, it is either defined in a predecessor or
1051     // live into it to.  Add the preds to the worklist unless they are a
1052     // defining block.
1053     for (BasicBlock *P : predecessors(BB)) {
1054       // The value is not live into a predecessor if it defines the value.
1055       if (DefBlocks.count(P))
1056         continue;
1057 
1058       // Otherwise it is, add to the worklist.
1059       LiveInBlockWorklist.push_back(P);
1060     }
1061   }
1062 }
1063 
1064 /// Queue a phi-node to be added to a basic-block for a specific Alloca.
1065 ///
1066 /// Returns true if there wasn't already a phi-node for that variable
QueuePhiNode(BasicBlock * BB,unsigned AllocaNo,unsigned & Version)1067 bool PromoteMem2Reg::QueuePhiNode(BasicBlock *BB, unsigned AllocaNo,
1068                                   unsigned &Version) {
1069   // Look up the basic-block in question.
1070   PHINode *&PN = NewPhiNodes[std::make_pair(BBNumbers[BB], AllocaNo)];
1071 
1072   // If the BB already has a phi node added for the i'th alloca then we're done!
1073   if (PN)
1074     return false;
1075 
1076   // Create a PhiNode using the dereferenced type... and add the phi-node to the
1077   // BasicBlock.
1078   PN = PHINode::Create(Allocas[AllocaNo]->getAllocatedType(), getNumPreds(BB),
1079                        Allocas[AllocaNo]->getName() + "." + Twine(Version++));
1080   PN->insertBefore(BB->begin());
1081   ++NumPHIInsert;
1082   PhiToAllocaMap[PN] = AllocaNo;
1083   return true;
1084 }
1085 
1086 /// Update the debug location of a phi. \p ApplyMergedLoc indicates whether to
1087 /// create a merged location incorporating \p DL, or to set \p DL directly.
updateForIncomingValueLocation(PHINode * PN,DebugLoc DL,bool ApplyMergedLoc)1088 static void updateForIncomingValueLocation(PHINode *PN, DebugLoc DL,
1089                                            bool ApplyMergedLoc) {
1090   if (ApplyMergedLoc)
1091     PN->applyMergedLocation(PN->getDebugLoc(), DL);
1092   else
1093     PN->setDebugLoc(DL);
1094 }
1095 
1096 /// Recursively traverse the CFG of the function, renaming loads and
1097 /// stores to the allocas which we are promoting.
1098 ///
1099 /// IncomingVals indicates what value each Alloca contains on exit from the
1100 /// predecessor block Pred.
RenamePass(BasicBlock * BB,BasicBlock * Pred,RenamePassData::ValVector & IncomingVals,RenamePassData::LocationVector & IncomingLocs,std::vector<RenamePassData> & Worklist)1101 void PromoteMem2Reg::RenamePass(BasicBlock *BB, BasicBlock *Pred,
1102                                 RenamePassData::ValVector &IncomingVals,
1103                                 RenamePassData::LocationVector &IncomingLocs,
1104                                 std::vector<RenamePassData> &Worklist) {
1105 NextIteration:
1106   // If we are inserting any phi nodes into this BB, they will already be in the
1107   // block.
1108   if (PHINode *APN = dyn_cast<PHINode>(BB->begin())) {
1109     // If we have PHI nodes to update, compute the number of edges from Pred to
1110     // BB.
1111     if (PhiToAllocaMap.count(APN)) {
1112       // We want to be able to distinguish between PHI nodes being inserted by
1113       // this invocation of mem2reg from those phi nodes that already existed in
1114       // the IR before mem2reg was run.  We determine that APN is being inserted
1115       // because it is missing incoming edges.  All other PHI nodes being
1116       // inserted by this pass of mem2reg will have the same number of incoming
1117       // operands so far.  Remember this count.
1118       unsigned NewPHINumOperands = APN->getNumOperands();
1119 
1120       unsigned NumEdges = llvm::count(successors(Pred), BB);
1121       assert(NumEdges && "Must be at least one edge from Pred to BB!");
1122 
1123       // Add entries for all the phis.
1124       BasicBlock::iterator PNI = BB->begin();
1125       do {
1126         unsigned AllocaNo = PhiToAllocaMap[APN];
1127 
1128         // Update the location of the phi node.
1129         updateForIncomingValueLocation(APN, IncomingLocs[AllocaNo],
1130                                        APN->getNumIncomingValues() > 0);
1131 
1132         // Add N incoming values to the PHI node.
1133         for (unsigned i = 0; i != NumEdges; ++i)
1134           APN->addIncoming(IncomingVals[AllocaNo], Pred);
1135 
1136         // For the  sequence `return X > 0.0 ? X : -X`, it is expected that this
1137         // results in fabs intrinsic. However, without no-signed-zeros(nsz) flag
1138         // on the phi node generated at this stage, fabs folding does not
1139         // happen. So, we try to infer nsz flag from the function attributes to
1140         // enable this fabs folding.
1141         if (isa<FPMathOperator>(APN) && NoSignedZeros)
1142           APN->setHasNoSignedZeros(true);
1143 
1144         // The currently active variable for this block is now the PHI.
1145         IncomingVals[AllocaNo] = APN;
1146         AllocaATInfo[AllocaNo].updateForNewPhi(APN, DIB);
1147         auto ConvertDbgDeclares = [&](auto &Container) {
1148           for (auto *DbgItem : Container)
1149             if (DbgItem->isAddressOfVariable())
1150               ConvertDebugDeclareToDebugValue(DbgItem, APN, DIB);
1151         };
1152         ConvertDbgDeclares(AllocaDbgUsers[AllocaNo]);
1153         ConvertDbgDeclares(AllocaDPUsers[AllocaNo]);
1154 
1155         // Get the next phi node.
1156         ++PNI;
1157         APN = dyn_cast<PHINode>(PNI);
1158         if (!APN)
1159           break;
1160 
1161         // Verify that it is missing entries.  If not, it is not being inserted
1162         // by this mem2reg invocation so we want to ignore it.
1163       } while (APN->getNumOperands() == NewPHINumOperands);
1164     }
1165   }
1166 
1167   // Don't revisit blocks.
1168   if (!Visited.insert(BB).second)
1169     return;
1170 
1171   for (BasicBlock::iterator II = BB->begin(); !II->isTerminator();) {
1172     Instruction *I = &*II++; // get the instruction, increment iterator
1173 
1174     if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
1175       AllocaInst *Src = dyn_cast<AllocaInst>(LI->getPointerOperand());
1176       if (!Src)
1177         continue;
1178 
1179       DenseMap<AllocaInst *, unsigned>::iterator AI = AllocaLookup.find(Src);
1180       if (AI == AllocaLookup.end())
1181         continue;
1182 
1183       Value *V = IncomingVals[AI->second];
1184       convertMetadataToAssumes(LI, V, SQ.DL, AC, &DT);
1185 
1186       // Anything using the load now uses the current value.
1187       LI->replaceAllUsesWith(V);
1188       LI->eraseFromParent();
1189     } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
1190       // Delete this instruction and mark the name as the current holder of the
1191       // value
1192       AllocaInst *Dest = dyn_cast<AllocaInst>(SI->getPointerOperand());
1193       if (!Dest)
1194         continue;
1195 
1196       DenseMap<AllocaInst *, unsigned>::iterator ai = AllocaLookup.find(Dest);
1197       if (ai == AllocaLookup.end())
1198         continue;
1199 
1200       // what value were we writing?
1201       unsigned AllocaNo = ai->second;
1202       IncomingVals[AllocaNo] = SI->getOperand(0);
1203 
1204       // Record debuginfo for the store before removing it.
1205       IncomingLocs[AllocaNo] = SI->getDebugLoc();
1206       AllocaATInfo[AllocaNo].updateForDeletedStore(SI, DIB, &DbgAssignsToDelete,
1207                                                    &DVRAssignsToDelete);
1208       auto ConvertDbgDeclares = [&](auto &Container) {
1209         for (auto *DbgItem : Container)
1210           if (DbgItem->isAddressOfVariable())
1211             ConvertDebugDeclareToDebugValue(DbgItem, SI, DIB);
1212       };
1213       ConvertDbgDeclares(AllocaDbgUsers[ai->second]);
1214       ConvertDbgDeclares(AllocaDPUsers[ai->second]);
1215       SI->eraseFromParent();
1216     }
1217   }
1218 
1219   // 'Recurse' to our successors.
1220   succ_iterator I = succ_begin(BB), E = succ_end(BB);
1221   if (I == E)
1222     return;
1223 
1224   // Keep track of the successors so we don't visit the same successor twice
1225   SmallPtrSet<BasicBlock *, 8> VisitedSuccs;
1226 
1227   // Handle the first successor without using the worklist.
1228   VisitedSuccs.insert(*I);
1229   Pred = BB;
1230   BB = *I;
1231   ++I;
1232 
1233   for (; I != E; ++I)
1234     if (VisitedSuccs.insert(*I).second)
1235       Worklist.emplace_back(*I, Pred, IncomingVals, IncomingLocs);
1236 
1237   goto NextIteration;
1238 }
1239 
PromoteMemToReg(ArrayRef<AllocaInst * > Allocas,DominatorTree & DT,AssumptionCache * AC)1240 void llvm::PromoteMemToReg(ArrayRef<AllocaInst *> Allocas, DominatorTree &DT,
1241                            AssumptionCache *AC) {
1242   // If there is nothing to do, bail out...
1243   if (Allocas.empty())
1244     return;
1245 
1246   PromoteMem2Reg(Allocas, DT, AC).run();
1247 }
1248