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