1 //===- InstCombineLoadStoreAlloca.cpp -------------------------------------===//
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 implements the visit functions for load, store and alloca.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "InstCombineInternal.h"
14 #include "llvm/ADT/MapVector.h"
15 #include "llvm/ADT/SmallString.h"
16 #include "llvm/ADT/Statistic.h"
17 #include "llvm/Analysis/Loads.h"
18 #include "llvm/Transforms/Utils/Local.h"
19 #include "llvm/IR/ConstantRange.h"
20 #include "llvm/IR/DataLayout.h"
21 #include "llvm/IR/DebugInfoMetadata.h"
22 #include "llvm/IR/IntrinsicInst.h"
23 #include "llvm/IR/LLVMContext.h"
24 #include "llvm/IR/MDBuilder.h"
25 #include "llvm/IR/PatternMatch.h"
26 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
27 using namespace llvm;
28 using namespace PatternMatch;
29 
30 #define DEBUG_TYPE "instcombine"
31 
32 STATISTIC(NumDeadStore,    "Number of dead stores eliminated");
33 STATISTIC(NumGlobalCopies, "Number of allocas copied from constant global");
34 
35 /// pointsToConstantGlobal - Return true if V (possibly indirectly) points to
36 /// some part of a constant global variable.  This intentionally only accepts
37 /// constant expressions because we can't rewrite arbitrary instructions.
pointsToConstantGlobal(Value * V)38 static bool pointsToConstantGlobal(Value *V) {
39   if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V))
40     return GV->isConstant();
41 
42   if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
43     if (CE->getOpcode() == Instruction::BitCast ||
44         CE->getOpcode() == Instruction::AddrSpaceCast ||
45         CE->getOpcode() == Instruction::GetElementPtr)
46       return pointsToConstantGlobal(CE->getOperand(0));
47   }
48   return false;
49 }
50 
51 /// isOnlyCopiedFromConstantGlobal - Recursively walk the uses of a (derived)
52 /// pointer to an alloca.  Ignore any reads of the pointer, return false if we
53 /// see any stores or other unknown uses.  If we see pointer arithmetic, keep
54 /// track of whether it moves the pointer (with IsOffset) but otherwise traverse
55 /// the uses.  If we see a memcpy/memmove that targets an unoffseted pointer to
56 /// the alloca, and if the source pointer is a pointer to a constant global, we
57 /// can optimize this.
58 static bool
isOnlyCopiedFromConstantGlobal(Value * V,MemTransferInst * & TheCopy,SmallVectorImpl<Instruction * > & ToDelete)59 isOnlyCopiedFromConstantGlobal(Value *V, MemTransferInst *&TheCopy,
60                                SmallVectorImpl<Instruction *> &ToDelete) {
61   // We track lifetime intrinsics as we encounter them.  If we decide to go
62   // ahead and replace the value with the global, this lets the caller quickly
63   // eliminate the markers.
64 
65   SmallVector<std::pair<Value *, bool>, 35> ValuesToInspect;
66   ValuesToInspect.emplace_back(V, false);
67   while (!ValuesToInspect.empty()) {
68     auto ValuePair = ValuesToInspect.pop_back_val();
69     const bool IsOffset = ValuePair.second;
70     for (auto &U : ValuePair.first->uses()) {
71       auto *I = cast<Instruction>(U.getUser());
72 
73       if (auto *LI = dyn_cast<LoadInst>(I)) {
74         // Ignore non-volatile loads, they are always ok.
75         if (!LI->isSimple()) return false;
76         continue;
77       }
78 
79       if (isa<BitCastInst>(I) || isa<AddrSpaceCastInst>(I)) {
80         // If uses of the bitcast are ok, we are ok.
81         ValuesToInspect.emplace_back(I, IsOffset);
82         continue;
83       }
84       if (auto *GEP = dyn_cast<GetElementPtrInst>(I)) {
85         // If the GEP has all zero indices, it doesn't offset the pointer. If it
86         // doesn't, it does.
87         ValuesToInspect.emplace_back(I, IsOffset || !GEP->hasAllZeroIndices());
88         continue;
89       }
90 
91       if (auto *Call = dyn_cast<CallBase>(I)) {
92         // If this is the function being called then we treat it like a load and
93         // ignore it.
94         if (Call->isCallee(&U))
95           continue;
96 
97         unsigned DataOpNo = Call->getDataOperandNo(&U);
98         bool IsArgOperand = Call->isArgOperand(&U);
99 
100         // Inalloca arguments are clobbered by the call.
101         if (IsArgOperand && Call->isInAllocaArgument(DataOpNo))
102           return false;
103 
104         // If this is a readonly/readnone call site, then we know it is just a
105         // load (but one that potentially returns the value itself), so we can
106         // ignore it if we know that the value isn't captured.
107         if (Call->onlyReadsMemory() &&
108             (Call->use_empty() || Call->doesNotCapture(DataOpNo)))
109           continue;
110 
111         // If this is being passed as a byval argument, the caller is making a
112         // copy, so it is only a read of the alloca.
113         if (IsArgOperand && Call->isByValArgument(DataOpNo))
114           continue;
115       }
116 
117       // Lifetime intrinsics can be handled by the caller.
118       if (I->isLifetimeStartOrEnd()) {
119         assert(I->use_empty() && "Lifetime markers have no result to use!");
120         ToDelete.push_back(I);
121         continue;
122       }
123 
124       // If this is isn't our memcpy/memmove, reject it as something we can't
125       // handle.
126       MemTransferInst *MI = dyn_cast<MemTransferInst>(I);
127       if (!MI)
128         return false;
129 
130       // If the transfer is using the alloca as a source of the transfer, then
131       // ignore it since it is a load (unless the transfer is volatile).
132       if (U.getOperandNo() == 1) {
133         if (MI->isVolatile()) return false;
134         continue;
135       }
136 
137       // If we already have seen a copy, reject the second one.
138       if (TheCopy) return false;
139 
140       // If the pointer has been offset from the start of the alloca, we can't
141       // safely handle this.
142       if (IsOffset) return false;
143 
144       // If the memintrinsic isn't using the alloca as the dest, reject it.
145       if (U.getOperandNo() != 0) return false;
146 
147       // If the source of the memcpy/move is not a constant global, reject it.
148       if (!pointsToConstantGlobal(MI->getSource()))
149         return false;
150 
151       // Otherwise, the transform is safe.  Remember the copy instruction.
152       TheCopy = MI;
153     }
154   }
155   return true;
156 }
157 
158 /// isOnlyCopiedFromConstantGlobal - Return true if the specified alloca is only
159 /// modified by a copy from a constant global.  If we can prove this, we can
160 /// replace any uses of the alloca with uses of the global directly.
161 static MemTransferInst *
isOnlyCopiedFromConstantGlobal(AllocaInst * AI,SmallVectorImpl<Instruction * > & ToDelete)162 isOnlyCopiedFromConstantGlobal(AllocaInst *AI,
163                                SmallVectorImpl<Instruction *> &ToDelete) {
164   MemTransferInst *TheCopy = nullptr;
165   if (isOnlyCopiedFromConstantGlobal(AI, TheCopy, ToDelete))
166     return TheCopy;
167   return nullptr;
168 }
169 
170 /// Returns true if V is dereferenceable for size of alloca.
isDereferenceableForAllocaSize(const Value * V,const AllocaInst * AI,const DataLayout & DL)171 static bool isDereferenceableForAllocaSize(const Value *V, const AllocaInst *AI,
172                                            const DataLayout &DL) {
173   if (AI->isArrayAllocation())
174     return false;
175   uint64_t AllocaSize = DL.getTypeStoreSize(AI->getAllocatedType());
176   if (!AllocaSize)
177     return false;
178   return isDereferenceableAndAlignedPointer(V, AI->getAlignment(),
179                                             APInt(64, AllocaSize), DL);
180 }
181 
simplifyAllocaArraySize(InstCombiner & IC,AllocaInst & AI)182 static Instruction *simplifyAllocaArraySize(InstCombiner &IC, AllocaInst &AI) {
183   // Check for array size of 1 (scalar allocation).
184   if (!AI.isArrayAllocation()) {
185     // i32 1 is the canonical array size for scalar allocations.
186     if (AI.getArraySize()->getType()->isIntegerTy(32))
187       return nullptr;
188 
189     // Canonicalize it.
190     Value *V = IC.Builder.getInt32(1);
191     AI.setOperand(0, V);
192     return &AI;
193   }
194 
195   // Convert: alloca Ty, C - where C is a constant != 1 into: alloca [C x Ty], 1
196   if (const ConstantInt *C = dyn_cast<ConstantInt>(AI.getArraySize())) {
197     if (C->getValue().getActiveBits() <= 64) {
198       Type *NewTy = ArrayType::get(AI.getAllocatedType(), C->getZExtValue());
199       AllocaInst *New = IC.Builder.CreateAlloca(NewTy, nullptr, AI.getName());
200       New->setAlignment(AI.getAlignment());
201 
202       // Scan to the end of the allocation instructions, to skip over a block of
203       // allocas if possible...also skip interleaved debug info
204       //
205       BasicBlock::iterator It(New);
206       while (isa<AllocaInst>(*It) || isa<DbgInfoIntrinsic>(*It))
207         ++It;
208 
209       // Now that I is pointing to the first non-allocation-inst in the block,
210       // insert our getelementptr instruction...
211       //
212       Type *IdxTy = IC.getDataLayout().getIntPtrType(AI.getType());
213       Value *NullIdx = Constant::getNullValue(IdxTy);
214       Value *Idx[2] = {NullIdx, NullIdx};
215       Instruction *GEP = GetElementPtrInst::CreateInBounds(
216           NewTy, New, Idx, New->getName() + ".sub");
217       IC.InsertNewInstBefore(GEP, *It);
218 
219       // Now make everything use the getelementptr instead of the original
220       // allocation.
221       return IC.replaceInstUsesWith(AI, GEP);
222     }
223   }
224 
225   if (isa<UndefValue>(AI.getArraySize()))
226     return IC.replaceInstUsesWith(AI, Constant::getNullValue(AI.getType()));
227 
228   // Ensure that the alloca array size argument has type intptr_t, so that
229   // any casting is exposed early.
230   Type *IntPtrTy = IC.getDataLayout().getIntPtrType(AI.getType());
231   if (AI.getArraySize()->getType() != IntPtrTy) {
232     Value *V = IC.Builder.CreateIntCast(AI.getArraySize(), IntPtrTy, false);
233     AI.setOperand(0, V);
234     return &AI;
235   }
236 
237   return nullptr;
238 }
239 
240 namespace {
241 // If I and V are pointers in different address space, it is not allowed to
242 // use replaceAllUsesWith since I and V have different types. A
243 // non-target-specific transformation should not use addrspacecast on V since
244 // the two address space may be disjoint depending on target.
245 //
246 // This class chases down uses of the old pointer until reaching the load
247 // instructions, then replaces the old pointer in the load instructions with
248 // the new pointer. If during the chasing it sees bitcast or GEP, it will
249 // create new bitcast or GEP with the new pointer and use them in the load
250 // instruction.
251 class PointerReplacer {
252 public:
PointerReplacer(InstCombiner & IC)253   PointerReplacer(InstCombiner &IC) : IC(IC) {}
254   void replacePointer(Instruction &I, Value *V);
255 
256 private:
257   void findLoadAndReplace(Instruction &I);
258   void replace(Instruction *I);
259   Value *getReplacement(Value *I);
260 
261   SmallVector<Instruction *, 4> Path;
262   MapVector<Value *, Value *> WorkMap;
263   InstCombiner &IC;
264 };
265 } // end anonymous namespace
266 
findLoadAndReplace(Instruction & I)267 void PointerReplacer::findLoadAndReplace(Instruction &I) {
268   for (auto U : I.users()) {
269     auto *Inst = dyn_cast<Instruction>(&*U);
270     if (!Inst)
271       return;
272     LLVM_DEBUG(dbgs() << "Found pointer user: " << *U << '\n');
273     if (isa<LoadInst>(Inst)) {
274       for (auto P : Path)
275         replace(P);
276       replace(Inst);
277     } else if (isa<GetElementPtrInst>(Inst) || isa<BitCastInst>(Inst)) {
278       Path.push_back(Inst);
279       findLoadAndReplace(*Inst);
280       Path.pop_back();
281     } else {
282       return;
283     }
284   }
285 }
286 
getReplacement(Value * V)287 Value *PointerReplacer::getReplacement(Value *V) {
288   auto Loc = WorkMap.find(V);
289   if (Loc != WorkMap.end())
290     return Loc->second;
291   return nullptr;
292 }
293 
replace(Instruction * I)294 void PointerReplacer::replace(Instruction *I) {
295   if (getReplacement(I))
296     return;
297 
298   if (auto *LT = dyn_cast<LoadInst>(I)) {
299     auto *V = getReplacement(LT->getPointerOperand());
300     assert(V && "Operand not replaced");
301     auto *NewI = new LoadInst(I->getType(), V);
302     NewI->takeName(LT);
303     IC.InsertNewInstWith(NewI, *LT);
304     IC.replaceInstUsesWith(*LT, NewI);
305     WorkMap[LT] = NewI;
306   } else if (auto *GEP = dyn_cast<GetElementPtrInst>(I)) {
307     auto *V = getReplacement(GEP->getPointerOperand());
308     assert(V && "Operand not replaced");
309     SmallVector<Value *, 8> Indices;
310     Indices.append(GEP->idx_begin(), GEP->idx_end());
311     auto *NewI = GetElementPtrInst::Create(
312         V->getType()->getPointerElementType(), V, Indices);
313     IC.InsertNewInstWith(NewI, *GEP);
314     NewI->takeName(GEP);
315     WorkMap[GEP] = NewI;
316   } else if (auto *BC = dyn_cast<BitCastInst>(I)) {
317     auto *V = getReplacement(BC->getOperand(0));
318     assert(V && "Operand not replaced");
319     auto *NewT = PointerType::get(BC->getType()->getPointerElementType(),
320                                   V->getType()->getPointerAddressSpace());
321     auto *NewI = new BitCastInst(V, NewT);
322     IC.InsertNewInstWith(NewI, *BC);
323     NewI->takeName(BC);
324     WorkMap[BC] = NewI;
325   } else {
326     llvm_unreachable("should never reach here");
327   }
328 }
329 
replacePointer(Instruction & I,Value * V)330 void PointerReplacer::replacePointer(Instruction &I, Value *V) {
331 #ifndef NDEBUG
332   auto *PT = cast<PointerType>(I.getType());
333   auto *NT = cast<PointerType>(V->getType());
334   assert(PT != NT && PT->getElementType() == NT->getElementType() &&
335          "Invalid usage");
336 #endif
337   WorkMap[&I] = V;
338   findLoadAndReplace(I);
339 }
340 
visitAllocaInst(AllocaInst & AI)341 Instruction *InstCombiner::visitAllocaInst(AllocaInst &AI) {
342   if (auto *I = simplifyAllocaArraySize(*this, AI))
343     return I;
344 
345   if (AI.getAllocatedType()->isSized()) {
346     // If the alignment is 0 (unspecified), assign it the preferred alignment.
347     if (AI.getAlignment() == 0)
348       AI.setAlignment(DL.getPrefTypeAlignment(AI.getAllocatedType()));
349 
350     // Move all alloca's of zero byte objects to the entry block and merge them
351     // together.  Note that we only do this for alloca's, because malloc should
352     // allocate and return a unique pointer, even for a zero byte allocation.
353     if (DL.getTypeAllocSize(AI.getAllocatedType()) == 0) {
354       // For a zero sized alloca there is no point in doing an array allocation.
355       // This is helpful if the array size is a complicated expression not used
356       // elsewhere.
357       if (AI.isArrayAllocation()) {
358         AI.setOperand(0, ConstantInt::get(AI.getArraySize()->getType(), 1));
359         return &AI;
360       }
361 
362       // Get the first instruction in the entry block.
363       BasicBlock &EntryBlock = AI.getParent()->getParent()->getEntryBlock();
364       Instruction *FirstInst = EntryBlock.getFirstNonPHIOrDbg();
365       if (FirstInst != &AI) {
366         // If the entry block doesn't start with a zero-size alloca then move
367         // this one to the start of the entry block.  There is no problem with
368         // dominance as the array size was forced to a constant earlier already.
369         AllocaInst *EntryAI = dyn_cast<AllocaInst>(FirstInst);
370         if (!EntryAI || !EntryAI->getAllocatedType()->isSized() ||
371             DL.getTypeAllocSize(EntryAI->getAllocatedType()) != 0) {
372           AI.moveBefore(FirstInst);
373           return &AI;
374         }
375 
376         // If the alignment of the entry block alloca is 0 (unspecified),
377         // assign it the preferred alignment.
378         if (EntryAI->getAlignment() == 0)
379           EntryAI->setAlignment(
380               DL.getPrefTypeAlignment(EntryAI->getAllocatedType()));
381         // Replace this zero-sized alloca with the one at the start of the entry
382         // block after ensuring that the address will be aligned enough for both
383         // types.
384         unsigned MaxAlign = std::max(EntryAI->getAlignment(),
385                                      AI.getAlignment());
386         EntryAI->setAlignment(MaxAlign);
387         if (AI.getType() != EntryAI->getType())
388           return new BitCastInst(EntryAI, AI.getType());
389         return replaceInstUsesWith(AI, EntryAI);
390       }
391     }
392   }
393 
394   if (AI.getAlignment()) {
395     // Check to see if this allocation is only modified by a memcpy/memmove from
396     // a constant global whose alignment is equal to or exceeds that of the
397     // allocation.  If this is the case, we can change all users to use
398     // the constant global instead.  This is commonly produced by the CFE by
399     // constructs like "void foo() { int A[] = {1,2,3,4,5,6,7,8,9...}; }" if 'A'
400     // is only subsequently read.
401     SmallVector<Instruction *, 4> ToDelete;
402     if (MemTransferInst *Copy = isOnlyCopiedFromConstantGlobal(&AI, ToDelete)) {
403       unsigned SourceAlign = getOrEnforceKnownAlignment(
404           Copy->getSource(), AI.getAlignment(), DL, &AI, &AC, &DT);
405       if (AI.getAlignment() <= SourceAlign &&
406           isDereferenceableForAllocaSize(Copy->getSource(), &AI, DL)) {
407         LLVM_DEBUG(dbgs() << "Found alloca equal to global: " << AI << '\n');
408         LLVM_DEBUG(dbgs() << "  memcpy = " << *Copy << '\n');
409         for (unsigned i = 0, e = ToDelete.size(); i != e; ++i)
410           eraseInstFromFunction(*ToDelete[i]);
411         Constant *TheSrc = cast<Constant>(Copy->getSource());
412         auto *SrcTy = TheSrc->getType();
413         auto *DestTy = PointerType::get(AI.getType()->getPointerElementType(),
414                                         SrcTy->getPointerAddressSpace());
415         Constant *Cast =
416             ConstantExpr::getPointerBitCastOrAddrSpaceCast(TheSrc, DestTy);
417         if (AI.getType()->getPointerAddressSpace() ==
418             SrcTy->getPointerAddressSpace()) {
419           Instruction *NewI = replaceInstUsesWith(AI, Cast);
420           eraseInstFromFunction(*Copy);
421           ++NumGlobalCopies;
422           return NewI;
423         } else {
424           PointerReplacer PtrReplacer(*this);
425           PtrReplacer.replacePointer(AI, Cast);
426           ++NumGlobalCopies;
427         }
428       }
429     }
430   }
431 
432   // At last, use the generic allocation site handler to aggressively remove
433   // unused allocas.
434   return visitAllocSite(AI);
435 }
436 
437 // Are we allowed to form a atomic load or store of this type?
isSupportedAtomicType(Type * Ty)438 static bool isSupportedAtomicType(Type *Ty) {
439   return Ty->isIntOrPtrTy() || Ty->isFloatingPointTy();
440 }
441 
442 /// Helper to combine a load to a new type.
443 ///
444 /// This just does the work of combining a load to a new type. It handles
445 /// metadata, etc., and returns the new instruction. The \c NewTy should be the
446 /// loaded *value* type. This will convert it to a pointer, cast the operand to
447 /// that pointer type, load it, etc.
448 ///
449 /// Note that this will create all of the instructions with whatever insert
450 /// point the \c InstCombiner currently is using.
combineLoadToNewType(InstCombiner & IC,LoadInst & LI,Type * NewTy,const Twine & Suffix="")451 static LoadInst *combineLoadToNewType(InstCombiner &IC, LoadInst &LI, Type *NewTy,
452                                       const Twine &Suffix = "") {
453   assert((!LI.isAtomic() || isSupportedAtomicType(NewTy)) &&
454          "can't fold an atomic load to requested type");
455 
456   Value *Ptr = LI.getPointerOperand();
457   unsigned AS = LI.getPointerAddressSpace();
458   SmallVector<std::pair<unsigned, MDNode *>, 8> MD;
459   LI.getAllMetadata(MD);
460 
461   Value *NewPtr = nullptr;
462   if (!(match(Ptr, m_BitCast(m_Value(NewPtr))) &&
463         NewPtr->getType()->getPointerElementType() == NewTy &&
464         NewPtr->getType()->getPointerAddressSpace() == AS))
465     NewPtr = IC.Builder.CreateBitCast(Ptr, NewTy->getPointerTo(AS));
466 
467   LoadInst *NewLoad = IC.Builder.CreateAlignedLoad(
468       NewTy, NewPtr, LI.getAlignment(), LI.isVolatile(), LI.getName() + Suffix);
469   NewLoad->setAtomic(LI.getOrdering(), LI.getSyncScopeID());
470   MDBuilder MDB(NewLoad->getContext());
471   for (const auto &MDPair : MD) {
472     unsigned ID = MDPair.first;
473     MDNode *N = MDPair.second;
474     // Note, essentially every kind of metadata should be preserved here! This
475     // routine is supposed to clone a load instruction changing *only its type*.
476     // The only metadata it makes sense to drop is metadata which is invalidated
477     // when the pointer type changes. This should essentially never be the case
478     // in LLVM, but we explicitly switch over only known metadata to be
479     // conservatively correct. If you are adding metadata to LLVM which pertains
480     // to loads, you almost certainly want to add it here.
481     switch (ID) {
482     case LLVMContext::MD_dbg:
483     case LLVMContext::MD_tbaa:
484     case LLVMContext::MD_prof:
485     case LLVMContext::MD_fpmath:
486     case LLVMContext::MD_tbaa_struct:
487     case LLVMContext::MD_invariant_load:
488     case LLVMContext::MD_alias_scope:
489     case LLVMContext::MD_noalias:
490     case LLVMContext::MD_nontemporal:
491     case LLVMContext::MD_mem_parallel_loop_access:
492     case LLVMContext::MD_access_group:
493       // All of these directly apply.
494       NewLoad->setMetadata(ID, N);
495       break;
496 
497     case LLVMContext::MD_nonnull:
498       copyNonnullMetadata(LI, N, *NewLoad);
499       break;
500     case LLVMContext::MD_align:
501     case LLVMContext::MD_dereferenceable:
502     case LLVMContext::MD_dereferenceable_or_null:
503       // These only directly apply if the new type is also a pointer.
504       if (NewTy->isPointerTy())
505         NewLoad->setMetadata(ID, N);
506       break;
507     case LLVMContext::MD_range:
508       copyRangeMetadata(IC.getDataLayout(), LI, N, *NewLoad);
509       break;
510     }
511   }
512   return NewLoad;
513 }
514 
515 /// Combine a store to a new type.
516 ///
517 /// Returns the newly created store instruction.
combineStoreToNewValue(InstCombiner & IC,StoreInst & SI,Value * V)518 static StoreInst *combineStoreToNewValue(InstCombiner &IC, StoreInst &SI, Value *V) {
519   assert((!SI.isAtomic() || isSupportedAtomicType(V->getType())) &&
520          "can't fold an atomic store of requested type");
521 
522   Value *Ptr = SI.getPointerOperand();
523   unsigned AS = SI.getPointerAddressSpace();
524   SmallVector<std::pair<unsigned, MDNode *>, 8> MD;
525   SI.getAllMetadata(MD);
526 
527   StoreInst *NewStore = IC.Builder.CreateAlignedStore(
528       V, IC.Builder.CreateBitCast(Ptr, V->getType()->getPointerTo(AS)),
529       SI.getAlignment(), SI.isVolatile());
530   NewStore->setAtomic(SI.getOrdering(), SI.getSyncScopeID());
531   for (const auto &MDPair : MD) {
532     unsigned ID = MDPair.first;
533     MDNode *N = MDPair.second;
534     // Note, essentially every kind of metadata should be preserved here! This
535     // routine is supposed to clone a store instruction changing *only its
536     // type*. The only metadata it makes sense to drop is metadata which is
537     // invalidated when the pointer type changes. This should essentially
538     // never be the case in LLVM, but we explicitly switch over only known
539     // metadata to be conservatively correct. If you are adding metadata to
540     // LLVM which pertains to stores, you almost certainly want to add it
541     // here.
542     switch (ID) {
543     case LLVMContext::MD_dbg:
544     case LLVMContext::MD_tbaa:
545     case LLVMContext::MD_prof:
546     case LLVMContext::MD_fpmath:
547     case LLVMContext::MD_tbaa_struct:
548     case LLVMContext::MD_alias_scope:
549     case LLVMContext::MD_noalias:
550     case LLVMContext::MD_nontemporal:
551     case LLVMContext::MD_mem_parallel_loop_access:
552     case LLVMContext::MD_access_group:
553       // All of these directly apply.
554       NewStore->setMetadata(ID, N);
555       break;
556     case LLVMContext::MD_invariant_load:
557     case LLVMContext::MD_nonnull:
558     case LLVMContext::MD_range:
559     case LLVMContext::MD_align:
560     case LLVMContext::MD_dereferenceable:
561     case LLVMContext::MD_dereferenceable_or_null:
562       // These don't apply for stores.
563       break;
564     }
565   }
566 
567   return NewStore;
568 }
569 
570 /// Returns true if instruction represent minmax pattern like:
571 ///   select ((cmp load V1, load V2), V1, V2).
isMinMaxWithLoads(Value * V)572 static bool isMinMaxWithLoads(Value *V) {
573   assert(V->getType()->isPointerTy() && "Expected pointer type.");
574   // Ignore possible ty* to ixx* bitcast.
575   V = peekThroughBitcast(V);
576   // Check that select is select ((cmp load V1, load V2), V1, V2) - minmax
577   // pattern.
578   CmpInst::Predicate Pred;
579   Instruction *L1;
580   Instruction *L2;
581   Value *LHS;
582   Value *RHS;
583   if (!match(V, m_Select(m_Cmp(Pred, m_Instruction(L1), m_Instruction(L2)),
584                          m_Value(LHS), m_Value(RHS))))
585     return false;
586   return (match(L1, m_Load(m_Specific(LHS))) &&
587           match(L2, m_Load(m_Specific(RHS)))) ||
588          (match(L1, m_Load(m_Specific(RHS))) &&
589           match(L2, m_Load(m_Specific(LHS))));
590 }
591 
592 /// Combine loads to match the type of their uses' value after looking
593 /// through intervening bitcasts.
594 ///
595 /// The core idea here is that if the result of a load is used in an operation,
596 /// we should load the type most conducive to that operation. For example, when
597 /// loading an integer and converting that immediately to a pointer, we should
598 /// instead directly load a pointer.
599 ///
600 /// However, this routine must never change the width of a load or the number of
601 /// loads as that would introduce a semantic change. This combine is expected to
602 /// be a semantic no-op which just allows loads to more closely model the types
603 /// of their consuming operations.
604 ///
605 /// Currently, we also refuse to change the precise type used for an atomic load
606 /// or a volatile load. This is debatable, and might be reasonable to change
607 /// later. However, it is risky in case some backend or other part of LLVM is
608 /// relying on the exact type loaded to select appropriate atomic operations.
combineLoadToOperationType(InstCombiner & IC,LoadInst & LI)609 static Instruction *combineLoadToOperationType(InstCombiner &IC, LoadInst &LI) {
610   // FIXME: We could probably with some care handle both volatile and ordered
611   // atomic loads here but it isn't clear that this is important.
612   if (!LI.isUnordered())
613     return nullptr;
614 
615   if (LI.use_empty())
616     return nullptr;
617 
618   // swifterror values can't be bitcasted.
619   if (LI.getPointerOperand()->isSwiftError())
620     return nullptr;
621 
622   Type *Ty = LI.getType();
623   const DataLayout &DL = IC.getDataLayout();
624 
625   // Try to canonicalize loads which are only ever stored to operate over
626   // integers instead of any other type. We only do this when the loaded type
627   // is sized and has a size exactly the same as its store size and the store
628   // size is a legal integer type.
629   // Do not perform canonicalization if minmax pattern is found (to avoid
630   // infinite loop).
631   if (!Ty->isIntegerTy() && Ty->isSized() &&
632       DL.isLegalInteger(DL.getTypeStoreSizeInBits(Ty)) &&
633       DL.typeSizeEqualsStoreSize(Ty) &&
634       !DL.isNonIntegralPointerType(Ty) &&
635       !isMinMaxWithLoads(
636           peekThroughBitcast(LI.getPointerOperand(), /*OneUseOnly=*/true))) {
637     if (all_of(LI.users(), [&LI](User *U) {
638           auto *SI = dyn_cast<StoreInst>(U);
639           return SI && SI->getPointerOperand() != &LI &&
640                  !SI->getPointerOperand()->isSwiftError();
641         })) {
642       LoadInst *NewLoad = combineLoadToNewType(
643           IC, LI,
644           Type::getIntNTy(LI.getContext(), DL.getTypeStoreSizeInBits(Ty)));
645       // Replace all the stores with stores of the newly loaded value.
646       for (auto UI = LI.user_begin(), UE = LI.user_end(); UI != UE;) {
647         auto *SI = cast<StoreInst>(*UI++);
648         IC.Builder.SetInsertPoint(SI);
649         combineStoreToNewValue(IC, *SI, NewLoad);
650         IC.eraseInstFromFunction(*SI);
651       }
652       assert(LI.use_empty() && "Failed to remove all users of the load!");
653       // Return the old load so the combiner can delete it safely.
654       return &LI;
655     }
656   }
657 
658   // Fold away bit casts of the loaded value by loading the desired type.
659   // We can do this for BitCastInsts as well as casts from and to pointer types,
660   // as long as those are noops (i.e., the source or dest type have the same
661   // bitwidth as the target's pointers).
662   if (LI.hasOneUse())
663     if (auto* CI = dyn_cast<CastInst>(LI.user_back()))
664       if (CI->isNoopCast(DL))
665         if (!LI.isAtomic() || isSupportedAtomicType(CI->getDestTy())) {
666           LoadInst *NewLoad = combineLoadToNewType(IC, LI, CI->getDestTy());
667           CI->replaceAllUsesWith(NewLoad);
668           IC.eraseInstFromFunction(*CI);
669           return &LI;
670         }
671 
672   // FIXME: We should also canonicalize loads of vectors when their elements are
673   // cast to other types.
674   return nullptr;
675 }
676 
unpackLoadToAggregate(InstCombiner & IC,LoadInst & LI)677 static Instruction *unpackLoadToAggregate(InstCombiner &IC, LoadInst &LI) {
678   // FIXME: We could probably with some care handle both volatile and atomic
679   // stores here but it isn't clear that this is important.
680   if (!LI.isSimple())
681     return nullptr;
682 
683   Type *T = LI.getType();
684   if (!T->isAggregateType())
685     return nullptr;
686 
687   StringRef Name = LI.getName();
688   assert(LI.getAlignment() && "Alignment must be set at this point");
689 
690   if (auto *ST = dyn_cast<StructType>(T)) {
691     // If the struct only have one element, we unpack.
692     auto NumElements = ST->getNumElements();
693     if (NumElements == 1) {
694       LoadInst *NewLoad = combineLoadToNewType(IC, LI, ST->getTypeAtIndex(0U),
695                                                ".unpack");
696       AAMDNodes AAMD;
697       LI.getAAMetadata(AAMD);
698       NewLoad->setAAMetadata(AAMD);
699       return IC.replaceInstUsesWith(LI, IC.Builder.CreateInsertValue(
700         UndefValue::get(T), NewLoad, 0, Name));
701     }
702 
703     // We don't want to break loads with padding here as we'd loose
704     // the knowledge that padding exists for the rest of the pipeline.
705     const DataLayout &DL = IC.getDataLayout();
706     auto *SL = DL.getStructLayout(ST);
707     if (SL->hasPadding())
708       return nullptr;
709 
710     auto Align = LI.getAlignment();
711     if (!Align)
712       Align = DL.getABITypeAlignment(ST);
713 
714     auto *Addr = LI.getPointerOperand();
715     auto *IdxType = Type::getInt32Ty(T->getContext());
716     auto *Zero = ConstantInt::get(IdxType, 0);
717 
718     Value *V = UndefValue::get(T);
719     for (unsigned i = 0; i < NumElements; i++) {
720       Value *Indices[2] = {
721         Zero,
722         ConstantInt::get(IdxType, i),
723       };
724       auto *Ptr = IC.Builder.CreateInBoundsGEP(ST, Addr, makeArrayRef(Indices),
725                                                Name + ".elt");
726       auto EltAlign = MinAlign(Align, SL->getElementOffset(i));
727       auto *L = IC.Builder.CreateAlignedLoad(ST->getElementType(i), Ptr,
728                                              EltAlign, Name + ".unpack");
729       // Propagate AA metadata. It'll still be valid on the narrowed load.
730       AAMDNodes AAMD;
731       LI.getAAMetadata(AAMD);
732       L->setAAMetadata(AAMD);
733       V = IC.Builder.CreateInsertValue(V, L, i);
734     }
735 
736     V->setName(Name);
737     return IC.replaceInstUsesWith(LI, V);
738   }
739 
740   if (auto *AT = dyn_cast<ArrayType>(T)) {
741     auto *ET = AT->getElementType();
742     auto NumElements = AT->getNumElements();
743     if (NumElements == 1) {
744       LoadInst *NewLoad = combineLoadToNewType(IC, LI, ET, ".unpack");
745       AAMDNodes AAMD;
746       LI.getAAMetadata(AAMD);
747       NewLoad->setAAMetadata(AAMD);
748       return IC.replaceInstUsesWith(LI, IC.Builder.CreateInsertValue(
749         UndefValue::get(T), NewLoad, 0, Name));
750     }
751 
752     // Bail out if the array is too large. Ideally we would like to optimize
753     // arrays of arbitrary size but this has a terrible impact on compile time.
754     // The threshold here is chosen arbitrarily, maybe needs a little bit of
755     // tuning.
756     if (NumElements > IC.MaxArraySizeForCombine)
757       return nullptr;
758 
759     const DataLayout &DL = IC.getDataLayout();
760     auto EltSize = DL.getTypeAllocSize(ET);
761     auto Align = LI.getAlignment();
762     if (!Align)
763       Align = DL.getABITypeAlignment(T);
764 
765     auto *Addr = LI.getPointerOperand();
766     auto *IdxType = Type::getInt64Ty(T->getContext());
767     auto *Zero = ConstantInt::get(IdxType, 0);
768 
769     Value *V = UndefValue::get(T);
770     uint64_t Offset = 0;
771     for (uint64_t i = 0; i < NumElements; i++) {
772       Value *Indices[2] = {
773         Zero,
774         ConstantInt::get(IdxType, i),
775       };
776       auto *Ptr = IC.Builder.CreateInBoundsGEP(AT, Addr, makeArrayRef(Indices),
777                                                Name + ".elt");
778       auto *L = IC.Builder.CreateAlignedLoad(
779           AT->getElementType(), Ptr, MinAlign(Align, Offset), Name + ".unpack");
780       AAMDNodes AAMD;
781       LI.getAAMetadata(AAMD);
782       L->setAAMetadata(AAMD);
783       V = IC.Builder.CreateInsertValue(V, L, i);
784       Offset += EltSize;
785     }
786 
787     V->setName(Name);
788     return IC.replaceInstUsesWith(LI, V);
789   }
790 
791   return nullptr;
792 }
793 
794 // If we can determine that all possible objects pointed to by the provided
795 // pointer value are, not only dereferenceable, but also definitively less than
796 // or equal to the provided maximum size, then return true. Otherwise, return
797 // false (constant global values and allocas fall into this category).
798 //
799 // FIXME: This should probably live in ValueTracking (or similar).
isObjectSizeLessThanOrEq(Value * V,uint64_t MaxSize,const DataLayout & DL)800 static bool isObjectSizeLessThanOrEq(Value *V, uint64_t MaxSize,
801                                      const DataLayout &DL) {
802   SmallPtrSet<Value *, 4> Visited;
803   SmallVector<Value *, 4> Worklist(1, V);
804 
805   do {
806     Value *P = Worklist.pop_back_val();
807     P = P->stripPointerCasts();
808 
809     if (!Visited.insert(P).second)
810       continue;
811 
812     if (SelectInst *SI = dyn_cast<SelectInst>(P)) {
813       Worklist.push_back(SI->getTrueValue());
814       Worklist.push_back(SI->getFalseValue());
815       continue;
816     }
817 
818     if (PHINode *PN = dyn_cast<PHINode>(P)) {
819       for (Value *IncValue : PN->incoming_values())
820         Worklist.push_back(IncValue);
821       continue;
822     }
823 
824     if (GlobalAlias *GA = dyn_cast<GlobalAlias>(P)) {
825       if (GA->isInterposable())
826         return false;
827       Worklist.push_back(GA->getAliasee());
828       continue;
829     }
830 
831     // If we know how big this object is, and it is less than MaxSize, continue
832     // searching. Otherwise, return false.
833     if (AllocaInst *AI = dyn_cast<AllocaInst>(P)) {
834       if (!AI->getAllocatedType()->isSized())
835         return false;
836 
837       ConstantInt *CS = dyn_cast<ConstantInt>(AI->getArraySize());
838       if (!CS)
839         return false;
840 
841       uint64_t TypeSize = DL.getTypeAllocSize(AI->getAllocatedType());
842       // Make sure that, even if the multiplication below would wrap as an
843       // uint64_t, we still do the right thing.
844       if ((CS->getValue().zextOrSelf(128)*APInt(128, TypeSize)).ugt(MaxSize))
845         return false;
846       continue;
847     }
848 
849     if (GlobalVariable *GV = dyn_cast<GlobalVariable>(P)) {
850       if (!GV->hasDefinitiveInitializer() || !GV->isConstant())
851         return false;
852 
853       uint64_t InitSize = DL.getTypeAllocSize(GV->getValueType());
854       if (InitSize > MaxSize)
855         return false;
856       continue;
857     }
858 
859     return false;
860   } while (!Worklist.empty());
861 
862   return true;
863 }
864 
865 // If we're indexing into an object of a known size, and the outer index is
866 // not a constant, but having any value but zero would lead to undefined
867 // behavior, replace it with zero.
868 //
869 // For example, if we have:
870 // @f.a = private unnamed_addr constant [1 x i32] [i32 12], align 4
871 // ...
872 // %arrayidx = getelementptr inbounds [1 x i32]* @f.a, i64 0, i64 %x
873 // ... = load i32* %arrayidx, align 4
874 // Then we know that we can replace %x in the GEP with i64 0.
875 //
876 // FIXME: We could fold any GEP index to zero that would cause UB if it were
877 // not zero. Currently, we only handle the first such index. Also, we could
878 // also search through non-zero constant indices if we kept track of the
879 // offsets those indices implied.
canReplaceGEPIdxWithZero(InstCombiner & IC,GetElementPtrInst * GEPI,Instruction * MemI,unsigned & Idx)880 static bool canReplaceGEPIdxWithZero(InstCombiner &IC, GetElementPtrInst *GEPI,
881                                      Instruction *MemI, unsigned &Idx) {
882   if (GEPI->getNumOperands() < 2)
883     return false;
884 
885   // Find the first non-zero index of a GEP. If all indices are zero, return
886   // one past the last index.
887   auto FirstNZIdx = [](const GetElementPtrInst *GEPI) {
888     unsigned I = 1;
889     for (unsigned IE = GEPI->getNumOperands(); I != IE; ++I) {
890       Value *V = GEPI->getOperand(I);
891       if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
892         if (CI->isZero())
893           continue;
894 
895       break;
896     }
897 
898     return I;
899   };
900 
901   // Skip through initial 'zero' indices, and find the corresponding pointer
902   // type. See if the next index is not a constant.
903   Idx = FirstNZIdx(GEPI);
904   if (Idx == GEPI->getNumOperands())
905     return false;
906   if (isa<Constant>(GEPI->getOperand(Idx)))
907     return false;
908 
909   SmallVector<Value *, 4> Ops(GEPI->idx_begin(), GEPI->idx_begin() + Idx);
910   Type *AllocTy =
911     GetElementPtrInst::getIndexedType(GEPI->getSourceElementType(), Ops);
912   if (!AllocTy || !AllocTy->isSized())
913     return false;
914   const DataLayout &DL = IC.getDataLayout();
915   uint64_t TyAllocSize = DL.getTypeAllocSize(AllocTy);
916 
917   // If there are more indices after the one we might replace with a zero, make
918   // sure they're all non-negative. If any of them are negative, the overall
919   // address being computed might be before the base address determined by the
920   // first non-zero index.
921   auto IsAllNonNegative = [&]() {
922     for (unsigned i = Idx+1, e = GEPI->getNumOperands(); i != e; ++i) {
923       KnownBits Known = IC.computeKnownBits(GEPI->getOperand(i), 0, MemI);
924       if (Known.isNonNegative())
925         continue;
926       return false;
927     }
928 
929     return true;
930   };
931 
932   // FIXME: If the GEP is not inbounds, and there are extra indices after the
933   // one we'll replace, those could cause the address computation to wrap
934   // (rendering the IsAllNonNegative() check below insufficient). We can do
935   // better, ignoring zero indices (and other indices we can prove small
936   // enough not to wrap).
937   if (Idx+1 != GEPI->getNumOperands() && !GEPI->isInBounds())
938     return false;
939 
940   // Note that isObjectSizeLessThanOrEq will return true only if the pointer is
941   // also known to be dereferenceable.
942   return isObjectSizeLessThanOrEq(GEPI->getOperand(0), TyAllocSize, DL) &&
943          IsAllNonNegative();
944 }
945 
946 // If we're indexing into an object with a variable index for the memory
947 // access, but the object has only one element, we can assume that the index
948 // will always be zero. If we replace the GEP, return it.
949 template <typename T>
replaceGEPIdxWithZero(InstCombiner & IC,Value * Ptr,T & MemI)950 static Instruction *replaceGEPIdxWithZero(InstCombiner &IC, Value *Ptr,
951                                           T &MemI) {
952   if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(Ptr)) {
953     unsigned Idx;
954     if (canReplaceGEPIdxWithZero(IC, GEPI, &MemI, Idx)) {
955       Instruction *NewGEPI = GEPI->clone();
956       NewGEPI->setOperand(Idx,
957         ConstantInt::get(GEPI->getOperand(Idx)->getType(), 0));
958       NewGEPI->insertBefore(GEPI);
959       MemI.setOperand(MemI.getPointerOperandIndex(), NewGEPI);
960       return NewGEPI;
961     }
962   }
963 
964   return nullptr;
965 }
966 
canSimplifyNullStoreOrGEP(StoreInst & SI)967 static bool canSimplifyNullStoreOrGEP(StoreInst &SI) {
968   if (NullPointerIsDefined(SI.getFunction(), SI.getPointerAddressSpace()))
969     return false;
970 
971   auto *Ptr = SI.getPointerOperand();
972   if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(Ptr))
973     Ptr = GEPI->getOperand(0);
974   return (isa<ConstantPointerNull>(Ptr) &&
975           !NullPointerIsDefined(SI.getFunction(), SI.getPointerAddressSpace()));
976 }
977 
canSimplifyNullLoadOrGEP(LoadInst & LI,Value * Op)978 static bool canSimplifyNullLoadOrGEP(LoadInst &LI, Value *Op) {
979   if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(Op)) {
980     const Value *GEPI0 = GEPI->getOperand(0);
981     if (isa<ConstantPointerNull>(GEPI0) &&
982         !NullPointerIsDefined(LI.getFunction(), GEPI->getPointerAddressSpace()))
983       return true;
984   }
985   if (isa<UndefValue>(Op) ||
986       (isa<ConstantPointerNull>(Op) &&
987        !NullPointerIsDefined(LI.getFunction(), LI.getPointerAddressSpace())))
988     return true;
989   return false;
990 }
991 
visitLoadInst(LoadInst & LI)992 Instruction *InstCombiner::visitLoadInst(LoadInst &LI) {
993   Value *Op = LI.getOperand(0);
994 
995   // Try to canonicalize the loaded type.
996   if (Instruction *Res = combineLoadToOperationType(*this, LI))
997     return Res;
998 
999   // Attempt to improve the alignment.
1000   unsigned KnownAlign = getOrEnforceKnownAlignment(
1001       Op, DL.getPrefTypeAlignment(LI.getType()), DL, &LI, &AC, &DT);
1002   unsigned LoadAlign = LI.getAlignment();
1003   unsigned EffectiveLoadAlign =
1004       LoadAlign != 0 ? LoadAlign : DL.getABITypeAlignment(LI.getType());
1005 
1006   if (KnownAlign > EffectiveLoadAlign)
1007     LI.setAlignment(KnownAlign);
1008   else if (LoadAlign == 0)
1009     LI.setAlignment(EffectiveLoadAlign);
1010 
1011   // Replace GEP indices if possible.
1012   if (Instruction *NewGEPI = replaceGEPIdxWithZero(*this, Op, LI)) {
1013       Worklist.Add(NewGEPI);
1014       return &LI;
1015   }
1016 
1017   if (Instruction *Res = unpackLoadToAggregate(*this, LI))
1018     return Res;
1019 
1020   // Do really simple store-to-load forwarding and load CSE, to catch cases
1021   // where there are several consecutive memory accesses to the same location,
1022   // separated by a few arithmetic operations.
1023   BasicBlock::iterator BBI(LI);
1024   bool IsLoadCSE = false;
1025   if (Value *AvailableVal = FindAvailableLoadedValue(
1026           &LI, LI.getParent(), BBI, DefMaxInstsToScan, AA, &IsLoadCSE)) {
1027     if (IsLoadCSE)
1028       combineMetadataForCSE(cast<LoadInst>(AvailableVal), &LI, false);
1029 
1030     return replaceInstUsesWith(
1031         LI, Builder.CreateBitOrPointerCast(AvailableVal, LI.getType(),
1032                                            LI.getName() + ".cast"));
1033   }
1034 
1035   // None of the following transforms are legal for volatile/ordered atomic
1036   // loads.  Most of them do apply for unordered atomics.
1037   if (!LI.isUnordered()) return nullptr;
1038 
1039   // load(gep null, ...) -> unreachable
1040   // load null/undef -> unreachable
1041   // TODO: Consider a target hook for valid address spaces for this xforms.
1042   if (canSimplifyNullLoadOrGEP(LI, Op)) {
1043     // Insert a new store to null instruction before the load to indicate
1044     // that this code is not reachable.  We do this instead of inserting
1045     // an unreachable instruction directly because we cannot modify the
1046     // CFG.
1047     StoreInst *SI = new StoreInst(UndefValue::get(LI.getType()),
1048                                   Constant::getNullValue(Op->getType()), &LI);
1049     SI->setDebugLoc(LI.getDebugLoc());
1050     return replaceInstUsesWith(LI, UndefValue::get(LI.getType()));
1051   }
1052 
1053   if (Op->hasOneUse()) {
1054     // Change select and PHI nodes to select values instead of addresses: this
1055     // helps alias analysis out a lot, allows many others simplifications, and
1056     // exposes redundancy in the code.
1057     //
1058     // Note that we cannot do the transformation unless we know that the
1059     // introduced loads cannot trap!  Something like this is valid as long as
1060     // the condition is always false: load (select bool %C, int* null, int* %G),
1061     // but it would not be valid if we transformed it to load from null
1062     // unconditionally.
1063     //
1064     if (SelectInst *SI = dyn_cast<SelectInst>(Op)) {
1065       // load (select (Cond, &V1, &V2))  --> select(Cond, load &V1, load &V2).
1066       unsigned Align = LI.getAlignment();
1067       if (isSafeToLoadUnconditionally(SI->getOperand(1), LI.getType(), Align,
1068                                       DL, SI) &&
1069           isSafeToLoadUnconditionally(SI->getOperand(2), LI.getType(), Align,
1070                                       DL, SI)) {
1071         LoadInst *V1 =
1072             Builder.CreateLoad(LI.getType(), SI->getOperand(1),
1073                                SI->getOperand(1)->getName() + ".val");
1074         LoadInst *V2 =
1075             Builder.CreateLoad(LI.getType(), SI->getOperand(2),
1076                                SI->getOperand(2)->getName() + ".val");
1077         assert(LI.isUnordered() && "implied by above");
1078         V1->setAlignment(Align);
1079         V1->setAtomic(LI.getOrdering(), LI.getSyncScopeID());
1080         V2->setAlignment(Align);
1081         V2->setAtomic(LI.getOrdering(), LI.getSyncScopeID());
1082         return SelectInst::Create(SI->getCondition(), V1, V2);
1083       }
1084 
1085       // load (select (cond, null, P)) -> load P
1086       if (isa<ConstantPointerNull>(SI->getOperand(1)) &&
1087           !NullPointerIsDefined(SI->getFunction(),
1088                                 LI.getPointerAddressSpace())) {
1089         LI.setOperand(0, SI->getOperand(2));
1090         return &LI;
1091       }
1092 
1093       // load (select (cond, P, null)) -> load P
1094       if (isa<ConstantPointerNull>(SI->getOperand(2)) &&
1095           !NullPointerIsDefined(SI->getFunction(),
1096                                 LI.getPointerAddressSpace())) {
1097         LI.setOperand(0, SI->getOperand(1));
1098         return &LI;
1099       }
1100     }
1101   }
1102   return nullptr;
1103 }
1104 
1105 /// Look for extractelement/insertvalue sequence that acts like a bitcast.
1106 ///
1107 /// \returns underlying value that was "cast", or nullptr otherwise.
1108 ///
1109 /// For example, if we have:
1110 ///
1111 ///     %E0 = extractelement <2 x double> %U, i32 0
1112 ///     %V0 = insertvalue [2 x double] undef, double %E0, 0
1113 ///     %E1 = extractelement <2 x double> %U, i32 1
1114 ///     %V1 = insertvalue [2 x double] %V0, double %E1, 1
1115 ///
1116 /// and the layout of a <2 x double> is isomorphic to a [2 x double],
1117 /// then %V1 can be safely approximated by a conceptual "bitcast" of %U.
1118 /// Note that %U may contain non-undef values where %V1 has undef.
likeBitCastFromVector(InstCombiner & IC,Value * V)1119 static Value *likeBitCastFromVector(InstCombiner &IC, Value *V) {
1120   Value *U = nullptr;
1121   while (auto *IV = dyn_cast<InsertValueInst>(V)) {
1122     auto *E = dyn_cast<ExtractElementInst>(IV->getInsertedValueOperand());
1123     if (!E)
1124       return nullptr;
1125     auto *W = E->getVectorOperand();
1126     if (!U)
1127       U = W;
1128     else if (U != W)
1129       return nullptr;
1130     auto *CI = dyn_cast<ConstantInt>(E->getIndexOperand());
1131     if (!CI || IV->getNumIndices() != 1 || CI->getZExtValue() != *IV->idx_begin())
1132       return nullptr;
1133     V = IV->getAggregateOperand();
1134   }
1135   if (!isa<UndefValue>(V) ||!U)
1136     return nullptr;
1137 
1138   auto *UT = cast<VectorType>(U->getType());
1139   auto *VT = V->getType();
1140   // Check that types UT and VT are bitwise isomorphic.
1141   const auto &DL = IC.getDataLayout();
1142   if (DL.getTypeStoreSizeInBits(UT) != DL.getTypeStoreSizeInBits(VT)) {
1143     return nullptr;
1144   }
1145   if (auto *AT = dyn_cast<ArrayType>(VT)) {
1146     if (AT->getNumElements() != UT->getNumElements())
1147       return nullptr;
1148   } else {
1149     auto *ST = cast<StructType>(VT);
1150     if (ST->getNumElements() != UT->getNumElements())
1151       return nullptr;
1152     for (const auto *EltT : ST->elements()) {
1153       if (EltT != UT->getElementType())
1154         return nullptr;
1155     }
1156   }
1157   return U;
1158 }
1159 
1160 /// Combine stores to match the type of value being stored.
1161 ///
1162 /// The core idea here is that the memory does not have any intrinsic type and
1163 /// where we can we should match the type of a store to the type of value being
1164 /// stored.
1165 ///
1166 /// However, this routine must never change the width of a store or the number of
1167 /// stores as that would introduce a semantic change. This combine is expected to
1168 /// be a semantic no-op which just allows stores to more closely model the types
1169 /// of their incoming values.
1170 ///
1171 /// Currently, we also refuse to change the precise type used for an atomic or
1172 /// volatile store. This is debatable, and might be reasonable to change later.
1173 /// However, it is risky in case some backend or other part of LLVM is relying
1174 /// on the exact type stored to select appropriate atomic operations.
1175 ///
1176 /// \returns true if the store was successfully combined away. This indicates
1177 /// the caller must erase the store instruction. We have to let the caller erase
1178 /// the store instruction as otherwise there is no way to signal whether it was
1179 /// combined or not: IC.EraseInstFromFunction returns a null pointer.
combineStoreToValueType(InstCombiner & IC,StoreInst & SI)1180 static bool combineStoreToValueType(InstCombiner &IC, StoreInst &SI) {
1181   // FIXME: We could probably with some care handle both volatile and ordered
1182   // atomic stores here but it isn't clear that this is important.
1183   if (!SI.isUnordered())
1184     return false;
1185 
1186   // swifterror values can't be bitcasted.
1187   if (SI.getPointerOperand()->isSwiftError())
1188     return false;
1189 
1190   Value *V = SI.getValueOperand();
1191 
1192   // Fold away bit casts of the stored value by storing the original type.
1193   if (auto *BC = dyn_cast<BitCastInst>(V)) {
1194     V = BC->getOperand(0);
1195     if (!SI.isAtomic() || isSupportedAtomicType(V->getType())) {
1196       combineStoreToNewValue(IC, SI, V);
1197       return true;
1198     }
1199   }
1200 
1201   if (Value *U = likeBitCastFromVector(IC, V))
1202     if (!SI.isAtomic() || isSupportedAtomicType(U->getType())) {
1203       combineStoreToNewValue(IC, SI, U);
1204       return true;
1205     }
1206 
1207   // FIXME: We should also canonicalize stores of vectors when their elements
1208   // are cast to other types.
1209   return false;
1210 }
1211 
unpackStoreToAggregate(InstCombiner & IC,StoreInst & SI)1212 static bool unpackStoreToAggregate(InstCombiner &IC, StoreInst &SI) {
1213   // FIXME: We could probably with some care handle both volatile and atomic
1214   // stores here but it isn't clear that this is important.
1215   if (!SI.isSimple())
1216     return false;
1217 
1218   Value *V = SI.getValueOperand();
1219   Type *T = V->getType();
1220 
1221   if (!T->isAggregateType())
1222     return false;
1223 
1224   if (auto *ST = dyn_cast<StructType>(T)) {
1225     // If the struct only have one element, we unpack.
1226     unsigned Count = ST->getNumElements();
1227     if (Count == 1) {
1228       V = IC.Builder.CreateExtractValue(V, 0);
1229       combineStoreToNewValue(IC, SI, V);
1230       return true;
1231     }
1232 
1233     // We don't want to break loads with padding here as we'd loose
1234     // the knowledge that padding exists for the rest of the pipeline.
1235     const DataLayout &DL = IC.getDataLayout();
1236     auto *SL = DL.getStructLayout(ST);
1237     if (SL->hasPadding())
1238       return false;
1239 
1240     auto Align = SI.getAlignment();
1241     if (!Align)
1242       Align = DL.getABITypeAlignment(ST);
1243 
1244     SmallString<16> EltName = V->getName();
1245     EltName += ".elt";
1246     auto *Addr = SI.getPointerOperand();
1247     SmallString<16> AddrName = Addr->getName();
1248     AddrName += ".repack";
1249 
1250     auto *IdxType = Type::getInt32Ty(ST->getContext());
1251     auto *Zero = ConstantInt::get(IdxType, 0);
1252     for (unsigned i = 0; i < Count; i++) {
1253       Value *Indices[2] = {
1254         Zero,
1255         ConstantInt::get(IdxType, i),
1256       };
1257       auto *Ptr = IC.Builder.CreateInBoundsGEP(ST, Addr, makeArrayRef(Indices),
1258                                                AddrName);
1259       auto *Val = IC.Builder.CreateExtractValue(V, i, EltName);
1260       auto EltAlign = MinAlign(Align, SL->getElementOffset(i));
1261       llvm::Instruction *NS = IC.Builder.CreateAlignedStore(Val, Ptr, EltAlign);
1262       AAMDNodes AAMD;
1263       SI.getAAMetadata(AAMD);
1264       NS->setAAMetadata(AAMD);
1265     }
1266 
1267     return true;
1268   }
1269 
1270   if (auto *AT = dyn_cast<ArrayType>(T)) {
1271     // If the array only have one element, we unpack.
1272     auto NumElements = AT->getNumElements();
1273     if (NumElements == 1) {
1274       V = IC.Builder.CreateExtractValue(V, 0);
1275       combineStoreToNewValue(IC, SI, V);
1276       return true;
1277     }
1278 
1279     // Bail out if the array is too large. Ideally we would like to optimize
1280     // arrays of arbitrary size but this has a terrible impact on compile time.
1281     // The threshold here is chosen arbitrarily, maybe needs a little bit of
1282     // tuning.
1283     if (NumElements > IC.MaxArraySizeForCombine)
1284       return false;
1285 
1286     const DataLayout &DL = IC.getDataLayout();
1287     auto EltSize = DL.getTypeAllocSize(AT->getElementType());
1288     auto Align = SI.getAlignment();
1289     if (!Align)
1290       Align = DL.getABITypeAlignment(T);
1291 
1292     SmallString<16> EltName = V->getName();
1293     EltName += ".elt";
1294     auto *Addr = SI.getPointerOperand();
1295     SmallString<16> AddrName = Addr->getName();
1296     AddrName += ".repack";
1297 
1298     auto *IdxType = Type::getInt64Ty(T->getContext());
1299     auto *Zero = ConstantInt::get(IdxType, 0);
1300 
1301     uint64_t Offset = 0;
1302     for (uint64_t i = 0; i < NumElements; i++) {
1303       Value *Indices[2] = {
1304         Zero,
1305         ConstantInt::get(IdxType, i),
1306       };
1307       auto *Ptr = IC.Builder.CreateInBoundsGEP(AT, Addr, makeArrayRef(Indices),
1308                                                AddrName);
1309       auto *Val = IC.Builder.CreateExtractValue(V, i, EltName);
1310       auto EltAlign = MinAlign(Align, Offset);
1311       Instruction *NS = IC.Builder.CreateAlignedStore(Val, Ptr, EltAlign);
1312       AAMDNodes AAMD;
1313       SI.getAAMetadata(AAMD);
1314       NS->setAAMetadata(AAMD);
1315       Offset += EltSize;
1316     }
1317 
1318     return true;
1319   }
1320 
1321   return false;
1322 }
1323 
1324 /// equivalentAddressValues - Test if A and B will obviously have the same
1325 /// value. This includes recognizing that %t0 and %t1 will have the same
1326 /// value in code like this:
1327 ///   %t0 = getelementptr \@a, 0, 3
1328 ///   store i32 0, i32* %t0
1329 ///   %t1 = getelementptr \@a, 0, 3
1330 ///   %t2 = load i32* %t1
1331 ///
equivalentAddressValues(Value * A,Value * B)1332 static bool equivalentAddressValues(Value *A, Value *B) {
1333   // Test if the values are trivially equivalent.
1334   if (A == B) return true;
1335 
1336   // Test if the values come form identical arithmetic instructions.
1337   // This uses isIdenticalToWhenDefined instead of isIdenticalTo because
1338   // its only used to compare two uses within the same basic block, which
1339   // means that they'll always either have the same value or one of them
1340   // will have an undefined value.
1341   if (isa<BinaryOperator>(A) ||
1342       isa<CastInst>(A) ||
1343       isa<PHINode>(A) ||
1344       isa<GetElementPtrInst>(A))
1345     if (Instruction *BI = dyn_cast<Instruction>(B))
1346       if (cast<Instruction>(A)->isIdenticalToWhenDefined(BI))
1347         return true;
1348 
1349   // Otherwise they may not be equivalent.
1350   return false;
1351 }
1352 
1353 /// Converts store (bitcast (load (bitcast (select ...)))) to
1354 /// store (load (select ...)), where select is minmax:
1355 /// select ((cmp load V1, load V2), V1, V2).
removeBitcastsFromLoadStoreOnMinMax(InstCombiner & IC,StoreInst & SI)1356 static bool removeBitcastsFromLoadStoreOnMinMax(InstCombiner &IC,
1357                                                 StoreInst &SI) {
1358   // bitcast?
1359   if (!match(SI.getPointerOperand(), m_BitCast(m_Value())))
1360     return false;
1361   // load? integer?
1362   Value *LoadAddr;
1363   if (!match(SI.getValueOperand(), m_Load(m_BitCast(m_Value(LoadAddr)))))
1364     return false;
1365   auto *LI = cast<LoadInst>(SI.getValueOperand());
1366   if (!LI->getType()->isIntegerTy())
1367     return false;
1368   if (!isMinMaxWithLoads(LoadAddr))
1369     return false;
1370 
1371   if (!all_of(LI->users(), [LI, LoadAddr](User *U) {
1372         auto *SI = dyn_cast<StoreInst>(U);
1373         return SI && SI->getPointerOperand() != LI &&
1374                peekThroughBitcast(SI->getPointerOperand()) != LoadAddr &&
1375                !SI->getPointerOperand()->isSwiftError();
1376       }))
1377     return false;
1378 
1379   IC.Builder.SetInsertPoint(LI);
1380   LoadInst *NewLI = combineLoadToNewType(
1381       IC, *LI, LoadAddr->getType()->getPointerElementType());
1382   // Replace all the stores with stores of the newly loaded value.
1383   for (auto *UI : LI->users()) {
1384     auto *USI = cast<StoreInst>(UI);
1385     IC.Builder.SetInsertPoint(USI);
1386     combineStoreToNewValue(IC, *USI, NewLI);
1387   }
1388   IC.replaceInstUsesWith(*LI, UndefValue::get(LI->getType()));
1389   IC.eraseInstFromFunction(*LI);
1390   return true;
1391 }
1392 
visitStoreInst(StoreInst & SI)1393 Instruction *InstCombiner::visitStoreInst(StoreInst &SI) {
1394   Value *Val = SI.getOperand(0);
1395   Value *Ptr = SI.getOperand(1);
1396 
1397   // Try to canonicalize the stored type.
1398   if (combineStoreToValueType(*this, SI))
1399     return eraseInstFromFunction(SI);
1400 
1401   // Attempt to improve the alignment.
1402   unsigned KnownAlign = getOrEnforceKnownAlignment(
1403       Ptr, DL.getPrefTypeAlignment(Val->getType()), DL, &SI, &AC, &DT);
1404   unsigned StoreAlign = SI.getAlignment();
1405   unsigned EffectiveStoreAlign =
1406       StoreAlign != 0 ? StoreAlign : DL.getABITypeAlignment(Val->getType());
1407 
1408   if (KnownAlign > EffectiveStoreAlign)
1409     SI.setAlignment(KnownAlign);
1410   else if (StoreAlign == 0)
1411     SI.setAlignment(EffectiveStoreAlign);
1412 
1413   // Try to canonicalize the stored type.
1414   if (unpackStoreToAggregate(*this, SI))
1415     return eraseInstFromFunction(SI);
1416 
1417   if (removeBitcastsFromLoadStoreOnMinMax(*this, SI))
1418     return eraseInstFromFunction(SI);
1419 
1420   // Replace GEP indices if possible.
1421   if (Instruction *NewGEPI = replaceGEPIdxWithZero(*this, Ptr, SI)) {
1422       Worklist.Add(NewGEPI);
1423       return &SI;
1424   }
1425 
1426   // Don't hack volatile/ordered stores.
1427   // FIXME: Some bits are legal for ordered atomic stores; needs refactoring.
1428   if (!SI.isUnordered()) return nullptr;
1429 
1430   // If the RHS is an alloca with a single use, zapify the store, making the
1431   // alloca dead.
1432   if (Ptr->hasOneUse()) {
1433     if (isa<AllocaInst>(Ptr))
1434       return eraseInstFromFunction(SI);
1435     if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Ptr)) {
1436       if (isa<AllocaInst>(GEP->getOperand(0))) {
1437         if (GEP->getOperand(0)->hasOneUse())
1438           return eraseInstFromFunction(SI);
1439       }
1440     }
1441   }
1442 
1443   // If we have a store to a location which is known constant, we can conclude
1444   // that the store must be storing the constant value (else the memory
1445   // wouldn't be constant), and this must be a noop.
1446   if (AA->pointsToConstantMemory(Ptr))
1447     return eraseInstFromFunction(SI);
1448 
1449   // Do really simple DSE, to catch cases where there are several consecutive
1450   // stores to the same location, separated by a few arithmetic operations. This
1451   // situation often occurs with bitfield accesses.
1452   BasicBlock::iterator BBI(SI);
1453   for (unsigned ScanInsts = 6; BBI != SI.getParent()->begin() && ScanInsts;
1454        --ScanInsts) {
1455     --BBI;
1456     // Don't count debug info directives, lest they affect codegen,
1457     // and we skip pointer-to-pointer bitcasts, which are NOPs.
1458     if (isa<DbgInfoIntrinsic>(BBI) ||
1459         (isa<BitCastInst>(BBI) && BBI->getType()->isPointerTy())) {
1460       ScanInsts++;
1461       continue;
1462     }
1463 
1464     if (StoreInst *PrevSI = dyn_cast<StoreInst>(BBI)) {
1465       // Prev store isn't volatile, and stores to the same location?
1466       if (PrevSI->isUnordered() && equivalentAddressValues(PrevSI->getOperand(1),
1467                                                         SI.getOperand(1))) {
1468         ++NumDeadStore;
1469         ++BBI;
1470         eraseInstFromFunction(*PrevSI);
1471         continue;
1472       }
1473       break;
1474     }
1475 
1476     // If this is a load, we have to stop.  However, if the loaded value is from
1477     // the pointer we're loading and is producing the pointer we're storing,
1478     // then *this* store is dead (X = load P; store X -> P).
1479     if (LoadInst *LI = dyn_cast<LoadInst>(BBI)) {
1480       if (LI == Val && equivalentAddressValues(LI->getOperand(0), Ptr)) {
1481         assert(SI.isUnordered() && "can't eliminate ordering operation");
1482         return eraseInstFromFunction(SI);
1483       }
1484 
1485       // Otherwise, this is a load from some other location.  Stores before it
1486       // may not be dead.
1487       break;
1488     }
1489 
1490     // Don't skip over loads, throws or things that can modify memory.
1491     if (BBI->mayWriteToMemory() || BBI->mayReadFromMemory() || BBI->mayThrow())
1492       break;
1493   }
1494 
1495   // store X, null    -> turns into 'unreachable' in SimplifyCFG
1496   // store X, GEP(null, Y) -> turns into 'unreachable' in SimplifyCFG
1497   if (canSimplifyNullStoreOrGEP(SI)) {
1498     if (!isa<UndefValue>(Val)) {
1499       SI.setOperand(0, UndefValue::get(Val->getType()));
1500       if (Instruction *U = dyn_cast<Instruction>(Val))
1501         Worklist.Add(U);  // Dropped a use.
1502     }
1503     return nullptr;  // Do not modify these!
1504   }
1505 
1506   // store undef, Ptr -> noop
1507   if (isa<UndefValue>(Val))
1508     return eraseInstFromFunction(SI);
1509 
1510   // If this store is the second-to-last instruction in the basic block
1511   // (excluding debug info and bitcasts of pointers) and if the block ends with
1512   // an unconditional branch, try to move the store to the successor block.
1513   BBI = SI.getIterator();
1514   do {
1515     ++BBI;
1516   } while (isa<DbgInfoIntrinsic>(BBI) ||
1517            (isa<BitCastInst>(BBI) && BBI->getType()->isPointerTy()));
1518 
1519   if (BranchInst *BI = dyn_cast<BranchInst>(BBI))
1520     if (BI->isUnconditional())
1521       mergeStoreIntoSuccessor(SI);
1522 
1523   return nullptr;
1524 }
1525 
1526 /// Try to transform:
1527 ///   if () { *P = v1; } else { *P = v2 }
1528 /// or:
1529 ///   *P = v1; if () { *P = v2; }
1530 /// into a phi node with a store in the successor.
mergeStoreIntoSuccessor(StoreInst & SI)1531 bool InstCombiner::mergeStoreIntoSuccessor(StoreInst &SI) {
1532   assert(SI.isUnordered() &&
1533          "This code has not been audited for volatile or ordered store case.");
1534 
1535   // Check if the successor block has exactly 2 incoming edges.
1536   BasicBlock *StoreBB = SI.getParent();
1537   BasicBlock *DestBB = StoreBB->getTerminator()->getSuccessor(0);
1538   if (!DestBB->hasNPredecessors(2))
1539     return false;
1540 
1541   // Capture the other block (the block that doesn't contain our store).
1542   pred_iterator PredIter = pred_begin(DestBB);
1543   if (*PredIter == StoreBB)
1544     ++PredIter;
1545   BasicBlock *OtherBB = *PredIter;
1546 
1547   // Bail out if all of the relevant blocks aren't distinct. This can happen,
1548   // for example, if SI is in an infinite loop.
1549   if (StoreBB == DestBB || OtherBB == DestBB)
1550     return false;
1551 
1552   // Verify that the other block ends in a branch and is not otherwise empty.
1553   BasicBlock::iterator BBI(OtherBB->getTerminator());
1554   BranchInst *OtherBr = dyn_cast<BranchInst>(BBI);
1555   if (!OtherBr || BBI == OtherBB->begin())
1556     return false;
1557 
1558   // If the other block ends in an unconditional branch, check for the 'if then
1559   // else' case. There is an instruction before the branch.
1560   StoreInst *OtherStore = nullptr;
1561   if (OtherBr->isUnconditional()) {
1562     --BBI;
1563     // Skip over debugging info.
1564     while (isa<DbgInfoIntrinsic>(BBI) ||
1565            (isa<BitCastInst>(BBI) && BBI->getType()->isPointerTy())) {
1566       if (BBI==OtherBB->begin())
1567         return false;
1568       --BBI;
1569     }
1570     // If this isn't a store, isn't a store to the same location, or is not the
1571     // right kind of store, bail out.
1572     OtherStore = dyn_cast<StoreInst>(BBI);
1573     if (!OtherStore || OtherStore->getOperand(1) != SI.getOperand(1) ||
1574         !SI.isSameOperationAs(OtherStore))
1575       return false;
1576   } else {
1577     // Otherwise, the other block ended with a conditional branch. If one of the
1578     // destinations is StoreBB, then we have the if/then case.
1579     if (OtherBr->getSuccessor(0) != StoreBB &&
1580         OtherBr->getSuccessor(1) != StoreBB)
1581       return false;
1582 
1583     // Okay, we know that OtherBr now goes to Dest and StoreBB, so this is an
1584     // if/then triangle. See if there is a store to the same ptr as SI that
1585     // lives in OtherBB.
1586     for (;; --BBI) {
1587       // Check to see if we find the matching store.
1588       if ((OtherStore = dyn_cast<StoreInst>(BBI))) {
1589         if (OtherStore->getOperand(1) != SI.getOperand(1) ||
1590             !SI.isSameOperationAs(OtherStore))
1591           return false;
1592         break;
1593       }
1594       // If we find something that may be using or overwriting the stored
1595       // value, or if we run out of instructions, we can't do the transform.
1596       if (BBI->mayReadFromMemory() || BBI->mayThrow() ||
1597           BBI->mayWriteToMemory() || BBI == OtherBB->begin())
1598         return false;
1599     }
1600 
1601     // In order to eliminate the store in OtherBr, we have to make sure nothing
1602     // reads or overwrites the stored value in StoreBB.
1603     for (BasicBlock::iterator I = StoreBB->begin(); &*I != &SI; ++I) {
1604       // FIXME: This should really be AA driven.
1605       if (I->mayReadFromMemory() || I->mayThrow() || I->mayWriteToMemory())
1606         return false;
1607     }
1608   }
1609 
1610   // Insert a PHI node now if we need it.
1611   Value *MergedVal = OtherStore->getOperand(0);
1612   // The debug locations of the original instructions might differ. Merge them.
1613   DebugLoc MergedLoc = DILocation::getMergedLocation(SI.getDebugLoc(),
1614                                                      OtherStore->getDebugLoc());
1615   if (MergedVal != SI.getOperand(0)) {
1616     PHINode *PN = PHINode::Create(MergedVal->getType(), 2, "storemerge");
1617     PN->addIncoming(SI.getOperand(0), SI.getParent());
1618     PN->addIncoming(OtherStore->getOperand(0), OtherBB);
1619     MergedVal = InsertNewInstBefore(PN, DestBB->front());
1620     PN->setDebugLoc(MergedLoc);
1621   }
1622 
1623   // Advance to a place where it is safe to insert the new store and insert it.
1624   BBI = DestBB->getFirstInsertionPt();
1625   StoreInst *NewSI = new StoreInst(MergedVal, SI.getOperand(1),
1626                                    SI.isVolatile(), SI.getAlignment(),
1627                                    SI.getOrdering(), SI.getSyncScopeID());
1628   InsertNewInstBefore(NewSI, *BBI);
1629   NewSI->setDebugLoc(MergedLoc);
1630 
1631   // If the two stores had AA tags, merge them.
1632   AAMDNodes AATags;
1633   SI.getAAMetadata(AATags);
1634   if (AATags) {
1635     OtherStore->getAAMetadata(AATags, /* Merge = */ true);
1636     NewSI->setAAMetadata(AATags);
1637   }
1638 
1639   // Nuke the old stores.
1640   eraseInstFromFunction(SI);
1641   eraseInstFromFunction(*OtherStore);
1642   return true;
1643 }
1644