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