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