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