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