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