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