LLVM 17.0.0git
VNCoercion.cpp
Go to the documentation of this file.
4#include "llvm/IR/IRBuilder.h"
7
8#define DEBUG_TYPE "vncoerce"
9
10namespace llvm {
11namespace VNCoercion {
12
14 return Ty->isStructTy() || Ty->isArrayTy() || isa<ScalableVectorType>(Ty);
15}
16
17/// Return true if coerceAvailableValueToLoadType will succeed.
19 const DataLayout &DL) {
20 Type *StoredTy = StoredVal->getType();
21
22 if (StoredTy == LoadTy)
23 return true;
24
25 // If the loaded/stored value is a first class array/struct, or scalable type,
26 // don't try to transform them. We need to be able to bitcast to integer.
29 return false;
30
31 uint64_t StoreSize = DL.getTypeSizeInBits(StoredTy).getFixedValue();
32
33 // The store size must be byte-aligned to support future type casts.
34 if (llvm::alignTo(StoreSize, 8) != StoreSize)
35 return false;
36
37 // The store has to be at least as big as the load.
38 if (StoreSize < DL.getTypeSizeInBits(LoadTy).getFixedValue())
39 return false;
40
41 bool StoredNI = DL.isNonIntegralPointerType(StoredTy->getScalarType());
42 bool LoadNI = DL.isNonIntegralPointerType(LoadTy->getScalarType());
43 // Don't coerce non-integral pointers to integers or vice versa.
44 if (StoredNI != LoadNI) {
45 // As a special case, allow coercion of memset used to initialize
46 // an array w/null. Despite non-integral pointers not generally having a
47 // specific bit pattern, we do assume null is zero.
48 if (auto *CI = dyn_cast<Constant>(StoredVal))
49 return CI->isNullValue();
50 return false;
51 } else if (StoredNI && LoadNI &&
52 StoredTy->getPointerAddressSpace() !=
53 LoadTy->getPointerAddressSpace()) {
54 return false;
55 }
56
57
58 // The implementation below uses inttoptr for vectors of unequal size; we
59 // can't allow this for non integral pointers. We could teach it to extract
60 // exact subvectors if desired.
61 if (StoredNI && StoreSize != DL.getTypeSizeInBits(LoadTy).getFixedValue())
62 return false;
63
64 if (StoredTy->isTargetExtTy() || LoadTy->isTargetExtTy())
65 return false;
66
67 return true;
68}
69
70/// If we saw a store of a value to memory, and
71/// then a load from a must-aliased pointer of a different type, try to coerce
72/// the stored value. LoadedTy is the type of the load we want to replace.
73/// IRB is IRBuilder used to insert new instructions.
74///
75/// If we can't do it, return null.
77 IRBuilderBase &Helper,
78 const DataLayout &DL) {
79 assert(canCoerceMustAliasedValueToLoad(StoredVal, LoadedTy, DL) &&
80 "precondition violation - materialization can't fail");
81 if (auto *C = dyn_cast<Constant>(StoredVal))
82 StoredVal = ConstantFoldConstant(C, DL);
83
84 // If this is already the right type, just return it.
85 Type *StoredValTy = StoredVal->getType();
86
87 uint64_t StoredValSize = DL.getTypeSizeInBits(StoredValTy).getFixedValue();
88 uint64_t LoadedValSize = DL.getTypeSizeInBits(LoadedTy).getFixedValue();
89
90 // If the store and reload are the same size, we can always reuse it.
91 if (StoredValSize == LoadedValSize) {
92 // Pointer to Pointer -> use bitcast.
93 if (StoredValTy->isPtrOrPtrVectorTy() && LoadedTy->isPtrOrPtrVectorTy()) {
94 StoredVal = Helper.CreateBitCast(StoredVal, LoadedTy);
95 } else {
96 // Convert source pointers to integers, which can be bitcast.
97 if (StoredValTy->isPtrOrPtrVectorTy()) {
98 StoredValTy = DL.getIntPtrType(StoredValTy);
99 StoredVal = Helper.CreatePtrToInt(StoredVal, StoredValTy);
100 }
101
102 Type *TypeToCastTo = LoadedTy;
103 if (TypeToCastTo->isPtrOrPtrVectorTy())
104 TypeToCastTo = DL.getIntPtrType(TypeToCastTo);
105
106 if (StoredValTy != TypeToCastTo)
107 StoredVal = Helper.CreateBitCast(StoredVal, TypeToCastTo);
108
109 // Cast to pointer if the load needs a pointer type.
110 if (LoadedTy->isPtrOrPtrVectorTy())
111 StoredVal = Helper.CreateIntToPtr(StoredVal, LoadedTy);
112 }
113
114 if (auto *C = dyn_cast<ConstantExpr>(StoredVal))
115 StoredVal = ConstantFoldConstant(C, DL);
116
117 return StoredVal;
118 }
119 // If the loaded value is smaller than the available value, then we can
120 // extract out a piece from it. If the available value is too small, then we
121 // can't do anything.
122 assert(StoredValSize >= LoadedValSize &&
123 "canCoerceMustAliasedValueToLoad fail");
124
125 // Convert source pointers to integers, which can be manipulated.
126 if (StoredValTy->isPtrOrPtrVectorTy()) {
127 StoredValTy = DL.getIntPtrType(StoredValTy);
128 StoredVal = Helper.CreatePtrToInt(StoredVal, StoredValTy);
129 }
130
131 // Convert vectors and fp to integer, which can be manipulated.
132 if (!StoredValTy->isIntegerTy()) {
133 StoredValTy = IntegerType::get(StoredValTy->getContext(), StoredValSize);
134 StoredVal = Helper.CreateBitCast(StoredVal, StoredValTy);
135 }
136
137 // If this is a big-endian system, we need to shift the value down to the low
138 // bits so that a truncate will work.
139 if (DL.isBigEndian()) {
140 uint64_t ShiftAmt = DL.getTypeStoreSizeInBits(StoredValTy).getFixedValue() -
141 DL.getTypeStoreSizeInBits(LoadedTy).getFixedValue();
142 StoredVal = Helper.CreateLShr(
143 StoredVal, ConstantInt::get(StoredVal->getType(), ShiftAmt));
144 }
145
146 // Truncate the integer to the right size now.
147 Type *NewIntTy = IntegerType::get(StoredValTy->getContext(), LoadedValSize);
148 StoredVal = Helper.CreateTruncOrBitCast(StoredVal, NewIntTy);
149
150 if (LoadedTy != NewIntTy) {
151 // If the result is a pointer, inttoptr.
152 if (LoadedTy->isPtrOrPtrVectorTy())
153 StoredVal = Helper.CreateIntToPtr(StoredVal, LoadedTy);
154 else
155 // Otherwise, bitcast.
156 StoredVal = Helper.CreateBitCast(StoredVal, LoadedTy);
157 }
158
159 if (auto *C = dyn_cast<Constant>(StoredVal))
160 StoredVal = ConstantFoldConstant(C, DL);
161
162 return StoredVal;
163}
164
165/// This function is called when we have a memdep query of a load that ends up
166/// being a clobbering memory write (store, memset, memcpy, memmove). This
167/// means that the write *may* provide bits used by the load but we can't be
168/// sure because the pointers don't must-alias.
169///
170/// Check this case to see if there is anything more we can do before we give
171/// up. This returns -1 if we have to give up, or a byte number in the stored
172/// value of the piece that feeds the load.
173static int analyzeLoadFromClobberingWrite(Type *LoadTy, Value *LoadPtr,
174 Value *WritePtr,
175 uint64_t WriteSizeInBits,
176 const DataLayout &DL) {
177 // If the loaded/stored value is a first class array/struct, or scalable type,
178 // don't try to transform them. We need to be able to bitcast to integer.
180 return -1;
181
182 int64_t StoreOffset = 0, LoadOffset = 0;
183 Value *StoreBase =
184 GetPointerBaseWithConstantOffset(WritePtr, StoreOffset, DL);
185 Value *LoadBase = GetPointerBaseWithConstantOffset(LoadPtr, LoadOffset, DL);
186 if (StoreBase != LoadBase)
187 return -1;
188
189 uint64_t LoadSize = DL.getTypeSizeInBits(LoadTy).getFixedValue();
190
191 if ((WriteSizeInBits & 7) | (LoadSize & 7))
192 return -1;
193 uint64_t StoreSize = WriteSizeInBits / 8; // Convert to bytes.
194 LoadSize /= 8;
195
196 // If the Load isn't completely contained within the stored bits, we don't
197 // have all the bits to feed it. We could do something crazy in the future
198 // (issue a smaller load then merge the bits in) but this seems unlikely to be
199 // valuable.
200 if (StoreOffset > LoadOffset ||
201 StoreOffset + int64_t(StoreSize) < LoadOffset + int64_t(LoadSize))
202 return -1;
203
204 // Okay, we can do this transformation. Return the number of bytes into the
205 // store that the load is.
206 return LoadOffset - StoreOffset;
207}
208
209/// This function is called when we have a
210/// memdep query of a load that ends up being a clobbering store.
212 StoreInst *DepSI, const DataLayout &DL) {
213 auto *StoredVal = DepSI->getValueOperand();
214
215 // Cannot handle reading from store of first-class aggregate or scalable type.
216 if (isFirstClassAggregateOrScalableType(StoredVal->getType()))
217 return -1;
218
219 if (!canCoerceMustAliasedValueToLoad(StoredVal, LoadTy, DL))
220 return -1;
221
222 Value *StorePtr = DepSI->getPointerOperand();
223 uint64_t StoreSize =
224 DL.getTypeSizeInBits(DepSI->getValueOperand()->getType()).getFixedValue();
225 return analyzeLoadFromClobberingWrite(LoadTy, LoadPtr, StorePtr, StoreSize,
226 DL);
227}
228
229/// Looks at a memory location for a load (specified by MemLocBase, Offs, and
230/// Size) and compares it against a load.
231///
232/// If the specified load could be safely widened to a larger integer load
233/// that is 1) still efficient, 2) safe for the target, and 3) would provide
234/// the specified memory location value, then this function returns the size
235/// in bytes of the load width to use. If not, this returns zero.
236static unsigned getLoadLoadClobberFullWidthSize(const Value *MemLocBase,
237 int64_t MemLocOffs,
238 unsigned MemLocSize,
239 const LoadInst *LI) {
240 // We can only extend simple integer loads.
241 if (!isa<IntegerType>(LI->getType()) || !LI->isSimple())
242 return 0;
243
244 // Load widening is hostile to ThreadSanitizer: it may cause false positives
245 // or make the reports more cryptic (access sizes are wrong).
246 if (LI->getParent()->getParent()->hasFnAttribute(Attribute::SanitizeThread))
247 return 0;
248
249 const DataLayout &DL = LI->getModule()->getDataLayout();
250
251 // Get the base of this load.
252 int64_t LIOffs = 0;
253 const Value *LIBase =
255
256 // If the two pointers are not based on the same pointer, we can't tell that
257 // they are related.
258 if (LIBase != MemLocBase)
259 return 0;
260
261 // Okay, the two values are based on the same pointer, but returned as
262 // no-alias. This happens when we have things like two byte loads at "P+1"
263 // and "P+3". Check to see if increasing the size of the "LI" load up to its
264 // alignment (or the largest native integer type) will allow us to load all
265 // the bits required by MemLoc.
266
267 // If MemLoc is before LI, then no widening of LI will help us out.
268 if (MemLocOffs < LIOffs)
269 return 0;
270
271 // Get the alignment of the load in bytes. We assume that it is safe to load
272 // any legal integer up to this size without a problem. For example, if we're
273 // looking at an i8 load on x86-32 that is known 1024 byte aligned, we can
274 // widen it up to an i32 load. If it is known 2-byte aligned, we can widen it
275 // to i16.
276 unsigned LoadAlign = LI->getAlign().value();
277
278 int64_t MemLocEnd = MemLocOffs + MemLocSize;
279
280 // If no amount of rounding up will let MemLoc fit into LI, then bail out.
281 if (LIOffs + LoadAlign < MemLocEnd)
282 return 0;
283
284 // This is the size of the load to try. Start with the next larger power of
285 // two.
286 unsigned NewLoadByteSize = LI->getType()->getPrimitiveSizeInBits() / 8U;
287 NewLoadByteSize = NextPowerOf2(NewLoadByteSize);
288
289 while (true) {
290 // If this load size is bigger than our known alignment or would not fit
291 // into a native integer register, then we fail.
292 if (NewLoadByteSize > LoadAlign ||
293 !DL.fitsInLegalInteger(NewLoadByteSize * 8))
294 return 0;
295
296 if (LIOffs + NewLoadByteSize > MemLocEnd &&
298 Attribute::SanitizeAddress) ||
300 Attribute::SanitizeHWAddress)))
301 // We will be reading past the location accessed by the original program.
302 // While this is safe in a regular build, Address Safety analysis tools
303 // may start reporting false warnings. So, don't do widening.
304 return 0;
305
306 // If a load of this width would include all of MemLoc, then we succeed.
307 if (LIOffs + NewLoadByteSize >= MemLocEnd)
308 return NewLoadByteSize;
309
310 NewLoadByteSize <<= 1;
311 }
312}
313
314/// This function is called when we have a
315/// memdep query of a load that ends up being clobbered by another load. See if
316/// the other load can feed into the second load.
317int analyzeLoadFromClobberingLoad(Type *LoadTy, Value *LoadPtr, LoadInst *DepLI,
318 const DataLayout &DL) {
319 // Cannot handle reading from store of first-class aggregate yet.
320 if (DepLI->getType()->isStructTy() || DepLI->getType()->isArrayTy())
321 return -1;
322
323 if (!canCoerceMustAliasedValueToLoad(DepLI, LoadTy, DL))
324 return -1;
325
326 Value *DepPtr = DepLI->getPointerOperand();
327 uint64_t DepSize = DL.getTypeSizeInBits(DepLI->getType()).getFixedValue();
328 int R = analyzeLoadFromClobberingWrite(LoadTy, LoadPtr, DepPtr, DepSize, DL);
329 if (R != -1)
330 return R;
331
332 // If we have a load/load clobber an DepLI can be widened to cover this load,
333 // then we should widen it!
334 int64_t LoadOffs = 0;
335 const Value *LoadBase =
336 GetPointerBaseWithConstantOffset(LoadPtr, LoadOffs, DL);
337 unsigned LoadSize = DL.getTypeStoreSize(LoadTy).getFixedValue();
338
339 unsigned Size =
340 getLoadLoadClobberFullWidthSize(LoadBase, LoadOffs, LoadSize, DepLI);
341 if (Size == 0)
342 return -1;
343
344 // Check non-obvious conditions enforced by MDA which we rely on for being
345 // able to materialize this potentially available value
346 assert(DepLI->isSimple() && "Cannot widen volatile/atomic load!");
347 assert(DepLI->getType()->isIntegerTy() && "Can't widen non-integer load");
348
349 return analyzeLoadFromClobberingWrite(LoadTy, LoadPtr, DepPtr, Size * 8, DL);
350}
351
353 MemIntrinsic *MI, const DataLayout &DL) {
354 // If the mem operation is a non-constant size, we can't handle it.
355 ConstantInt *SizeCst = dyn_cast<ConstantInt>(MI->getLength());
356 if (!SizeCst)
357 return -1;
358 uint64_t MemSizeInBits = SizeCst->getZExtValue() * 8;
359
360 // If this is memset, we just need to see if the offset is valid in the size
361 // of the memset..
362 if (const auto *memset_inst = dyn_cast<MemSetInst>(MI)) {
363 if (DL.isNonIntegralPointerType(LoadTy->getScalarType())) {
364 auto *CI = dyn_cast<ConstantInt>(memset_inst->getValue());
365 if (!CI || !CI->isZero())
366 return -1;
367 }
368 return analyzeLoadFromClobberingWrite(LoadTy, LoadPtr, MI->getDest(),
369 MemSizeInBits, DL);
370 }
371
372 // If we have a memcpy/memmove, the only case we can handle is if this is a
373 // copy from constant memory. In that case, we can read directly from the
374 // constant memory.
375 MemTransferInst *MTI = cast<MemTransferInst>(MI);
376
377 Constant *Src = dyn_cast<Constant>(MTI->getSource());
378 if (!Src)
379 return -1;
380
381 GlobalVariable *GV = dyn_cast<GlobalVariable>(getUnderlyingObject(Src));
382 if (!GV || !GV->isConstant() || !GV->hasDefinitiveInitializer())
383 return -1;
384
385 // See if the access is within the bounds of the transfer.
386 int Offset = analyzeLoadFromClobberingWrite(LoadTy, LoadPtr, MI->getDest(),
387 MemSizeInBits, DL);
388 if (Offset == -1)
389 return Offset;
390
391 // Otherwise, see if we can constant fold a load from the constant with the
392 // offset applied as appropriate.
393 unsigned IndexSize = DL.getIndexTypeSizeInBits(Src->getType());
394 if (ConstantFoldLoadFromConstPtr(Src, LoadTy, APInt(IndexSize, Offset), DL))
395 return Offset;
396 return -1;
397}
398
400 Type *LoadTy, IRBuilderBase &Builder,
401 const DataLayout &DL) {
402 LLVMContext &Ctx = SrcVal->getType()->getContext();
403
404 // If two pointers are in the same address space, they have the same size,
405 // so we don't need to do any truncation, etc. This avoids introducing
406 // ptrtoint instructions for pointers that may be non-integral.
407 if (SrcVal->getType()->isPointerTy() && LoadTy->isPointerTy() &&
408 cast<PointerType>(SrcVal->getType())->getAddressSpace() ==
409 cast<PointerType>(LoadTy)->getAddressSpace()) {
410 return SrcVal;
411 }
412
413 uint64_t StoreSize =
414 (DL.getTypeSizeInBits(SrcVal->getType()).getFixedValue() + 7) / 8;
415 uint64_t LoadSize = (DL.getTypeSizeInBits(LoadTy).getFixedValue() + 7) / 8;
416 // Compute which bits of the stored value are being used by the load. Convert
417 // to an integer type to start with.
418 if (SrcVal->getType()->isPtrOrPtrVectorTy())
419 SrcVal =
420 Builder.CreatePtrToInt(SrcVal, DL.getIntPtrType(SrcVal->getType()));
421 if (!SrcVal->getType()->isIntegerTy())
422 SrcVal =
423 Builder.CreateBitCast(SrcVal, IntegerType::get(Ctx, StoreSize * 8));
424
425 // Shift the bits to the least significant depending on endianness.
426 unsigned ShiftAmt;
427 if (DL.isLittleEndian())
428 ShiftAmt = Offset * 8;
429 else
430 ShiftAmt = (StoreSize - LoadSize - Offset) * 8;
431 if (ShiftAmt)
432 SrcVal = Builder.CreateLShr(SrcVal,
433 ConstantInt::get(SrcVal->getType(), ShiftAmt));
434
435 if (LoadSize != StoreSize)
436 SrcVal = Builder.CreateTruncOrBitCast(SrcVal,
437 IntegerType::get(Ctx, LoadSize * 8));
438 return SrcVal;
439}
440
441/// This function is called when we have a memdep query of a load that ends up
442/// being a clobbering store. This means that the store provides bits used by
443/// the load but the pointers don't must-alias. Check this case to see if
444/// there is anything more we can do before we give up.
445Value *getStoreValueForLoad(Value *SrcVal, unsigned Offset, Type *LoadTy,
446 Instruction *InsertPt, const DataLayout &DL) {
447
448 IRBuilder<> Builder(InsertPt);
449 SrcVal = getStoreValueForLoadHelper(SrcVal, Offset, LoadTy, Builder, DL);
450 return coerceAvailableValueToLoadType(SrcVal, LoadTy, Builder, DL);
451}
452
454 Type *LoadTy, const DataLayout &DL) {
455 return ConstantFoldLoadFromConst(SrcVal, LoadTy, APInt(32, Offset), DL);
456}
457
458/// This function is called when we have a memdep query of a load that ends up
459/// being a clobbering load. This means that the load *may* provide bits used
460/// by the load but we can't be sure because the pointers don't must-alias.
461/// Check this case to see if there is anything more we can do before we give
462/// up.
463Value *getLoadValueForLoad(LoadInst *SrcVal, unsigned Offset, Type *LoadTy,
464 Instruction *InsertPt, const DataLayout &DL) {
465 // If Offset+LoadTy exceeds the size of SrcVal, then we must be wanting to
466 // widen SrcVal out to a larger load.
467 unsigned SrcValStoreSize =
468 DL.getTypeStoreSize(SrcVal->getType()).getFixedValue();
469 unsigned LoadSize = DL.getTypeStoreSize(LoadTy).getFixedValue();
470 if (Offset + LoadSize > SrcValStoreSize) {
471 assert(SrcVal->isSimple() && "Cannot widen volatile/atomic load!");
472 assert(SrcVal->getType()->isIntegerTy() && "Can't widen non-integer load");
473 // If we have a load/load clobber an DepLI can be widened to cover this
474 // load, then we should widen it to the next power of 2 size big enough!
475 unsigned NewLoadSize = llvm::bit_ceil(Offset + LoadSize);
476
477 Value *PtrVal = SrcVal->getPointerOperand();
478 // Insert the new load after the old load. This ensures that subsequent
479 // memdep queries will find the new load. We can't easily remove the old
480 // load completely because it is already in the value numbering table.
482 Type *DestTy = IntegerType::get(LoadTy->getContext(), NewLoadSize * 8);
483 Type *DestPTy =
484 PointerType::get(DestTy, PtrVal->getType()->getPointerAddressSpace());
485 Builder.SetCurrentDebugLocation(SrcVal->getDebugLoc());
486 PtrVal = Builder.CreateBitCast(PtrVal, DestPTy);
487 LoadInst *NewLoad = Builder.CreateLoad(DestTy, PtrVal);
488 NewLoad->takeName(SrcVal);
489 NewLoad->setAlignment(SrcVal->getAlign());
490
491 LLVM_DEBUG(dbgs() << "GVN WIDENED LOAD: " << *SrcVal << "\n");
492 LLVM_DEBUG(dbgs() << "TO: " << *NewLoad << "\n");
493
494 // Replace uses of the original load with the wider load. On a big endian
495 // system, we need to shift down to get the relevant bits.
496 Value *RV = NewLoad;
497 if (DL.isBigEndian())
498 RV = Builder.CreateLShr(RV, (NewLoadSize - SrcValStoreSize) * 8);
499 RV = Builder.CreateTrunc(RV, SrcVal->getType());
500 SrcVal->replaceAllUsesWith(RV);
501
502 SrcVal = NewLoad;
503 }
504
505 return getStoreValueForLoad(SrcVal, Offset, LoadTy, InsertPt, DL);
506}
507
509 Type *LoadTy, const DataLayout &DL) {
510 unsigned SrcValStoreSize =
511 DL.getTypeStoreSize(SrcVal->getType()).getFixedValue();
512 unsigned LoadSize = DL.getTypeStoreSize(LoadTy).getFixedValue();
513 if (Offset + LoadSize > SrcValStoreSize)
514 return nullptr;
515 return getConstantStoreValueForLoad(SrcVal, Offset, LoadTy, DL);
516}
517
518/// This function is called when we have a
519/// memdep query of a load that ends up being a clobbering mem intrinsic.
521 Type *LoadTy, Instruction *InsertPt,
522 const DataLayout &DL) {
523 LLVMContext &Ctx = LoadTy->getContext();
524 uint64_t LoadSize = DL.getTypeSizeInBits(LoadTy).getFixedValue() / 8;
525 IRBuilder<> Builder(InsertPt);
526
527 // We know that this method is only called when the mem transfer fully
528 // provides the bits for the load.
529 if (MemSetInst *MSI = dyn_cast<MemSetInst>(SrcInst)) {
530 // memset(P, 'x', 1234) -> splat('x'), even if x is a variable, and
531 // independently of what the offset is.
532 Value *Val = MSI->getValue();
533 if (LoadSize != 1)
534 Val =
535 Builder.CreateZExtOrBitCast(Val, IntegerType::get(Ctx, LoadSize * 8));
536 Value *OneElt = Val;
537
538 // Splat the value out to the right number of bits.
539 for (unsigned NumBytesSet = 1; NumBytesSet != LoadSize;) {
540 // If we can double the number of bytes set, do it.
541 if (NumBytesSet * 2 <= LoadSize) {
542 Value *ShVal = Builder.CreateShl(
543 Val, ConstantInt::get(Val->getType(), NumBytesSet * 8));
544 Val = Builder.CreateOr(Val, ShVal);
545 NumBytesSet <<= 1;
546 continue;
547 }
548
549 // Otherwise insert one byte at a time.
550 Value *ShVal =
551 Builder.CreateShl(Val, ConstantInt::get(Val->getType(), 1 * 8));
552 Val = Builder.CreateOr(OneElt, ShVal);
553 ++NumBytesSet;
554 }
555
556 return coerceAvailableValueToLoadType(Val, LoadTy, Builder, DL);
557 }
558
559 // Otherwise, this is a memcpy/memmove from a constant global.
560 MemTransferInst *MTI = cast<MemTransferInst>(SrcInst);
561 Constant *Src = cast<Constant>(MTI->getSource());
562 unsigned IndexSize = DL.getIndexTypeSizeInBits(Src->getType());
563 return ConstantFoldLoadFromConstPtr(Src, LoadTy, APInt(IndexSize, Offset),
564 DL);
565}
566
568 Type *LoadTy, const DataLayout &DL) {
569 LLVMContext &Ctx = LoadTy->getContext();
570 uint64_t LoadSize = DL.getTypeSizeInBits(LoadTy).getFixedValue() / 8;
571
572 // We know that this method is only called when the mem transfer fully
573 // provides the bits for the load.
574 if (MemSetInst *MSI = dyn_cast<MemSetInst>(SrcInst)) {
575 auto *Val = dyn_cast<ConstantInt>(MSI->getValue());
576 if (!Val)
577 return nullptr;
578
579 Val = ConstantInt::get(Ctx, APInt::getSplat(LoadSize * 8, Val->getValue()));
580 return ConstantFoldLoadFromConst(Val, LoadTy, DL);
581 }
582
583 // Otherwise, this is a memcpy/memmove from a constant global.
584 MemTransferInst *MTI = cast<MemTransferInst>(SrcInst);
585 Constant *Src = cast<Constant>(MTI->getSource());
586 unsigned IndexSize = DL.getIndexTypeSizeInBits(Src->getType());
587 return ConstantFoldLoadFromConstPtr(Src, LoadTy, APInt(IndexSize, Offset),
588 DL);
589}
590} // namespace VNCoercion
591} // namespace llvm
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
assume Assume Builder
#define LLVM_DEBUG(X)
Definition: Debug.h:101
uint64_t Size
IRTranslator LLVM IR MI
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Class for arbitrary precision integers.
Definition: APInt.h:75
static APInt getSplat(unsigned NewLen, const APInt &V)
Return a value containing V broadcasted over NewLen bits.
Definition: APInt.cpp:612
const Function * getParent() const
Return the enclosing method, or null if none.
Definition: BasicBlock.h:112
InstListType::iterator iterator
Instruction iterators...
Definition: BasicBlock.h:87
This is the shared class of boolean and integer constants.
Definition: Constants.h:78
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:888
uint64_t getZExtValue() const
Return the constant as a 64-bit unsigned integer value after it has been zero extended as appropriate...
Definition: Constants.h:145
This is an important base class in LLVM.
Definition: Constant.h:41
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:110
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
Definition: Function.cpp:644
bool isConstant() const
If the value is a global constant, its value is immutable throughout the runtime execution of the pro...
bool hasDefinitiveInitializer() const
hasDefinitiveInitializer - Whether the global variable has an initializer, and any other instances of...
Common base class shared among various IRBuilders.
Definition: IRBuilder.h:94
Value * CreateIntToPtr(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:2017
Value * CreateLShr(Value *LHS, Value *RHS, const Twine &Name="", bool isExact=false)
Definition: IRBuilder.h:1366
Value * CreateBitCast(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:2022
Value * CreatePtrToInt(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:2012
Value * CreateTruncOrBitCast(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:2050
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition: IRBuilder.h:2564
const DebugLoc & getDebugLoc() const
Return the debug location for this node as a DebugLoc.
Definition: Instruction.h:358
const Module * getModule() const
Return the module owning the function this instruction belongs to or nullptr it the function does not...
Definition: Instruction.cpp:70
const BasicBlock * getParent() const
Definition: Instruction.h:90
static IntegerType * get(LLVMContext &C, unsigned NumBits)
This static method is the primary way of constructing an IntegerType.
Definition: Type.cpp:331
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:67
An instruction for reading from memory.
Definition: Instructions.h:177
void setAlignment(Align Align)
Definition: Instructions.h:224
Value * getPointerOperand()
Definition: Instructions.h:264
bool isSimple() const
Definition: Instructions.h:256
Align getAlign() const
Return the alignment of the access that is being performed.
Definition: Instructions.h:220
This is the common base class for memset/memcpy/memmove.
This class wraps the llvm.memset and llvm.memset.inline intrinsics.
Value * getSource() const
This is just like getRawSource, but it strips off any cast instructions that feed it,...
This class wraps the llvm.memcpy/memmove intrinsics.
const DataLayout & getDataLayout() const
Get the data layout for the module's target platform.
Definition: Module.cpp:398
static PointerType * get(Type *ElementType, unsigned AddressSpace)
This constructs a pointer to an object of the specified type in a numbered address space.
An instruction for storing to memory.
Definition: Instructions.h:301
Value * getValueOperand()
Definition: Instructions.h:390
Value * getPointerOperand()
Definition: Instructions.h:393
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
bool isArrayTy() const
True if this is an instance of ArrayType.
Definition: Type.h:255
bool isPointerTy() const
True if this is an instance of PointerType.
Definition: Type.h:258
unsigned getPointerAddressSpace() const
Get the address space of this pointer or pointer vector type.
bool isStructTy() const
True if this is an instance of StructType.
Definition: Type.h:252
bool isTargetExtTy() const
Return true if this is a target extension type.
Definition: Type.h:207
LLVMContext & getContext() const
Return the LLVMContext in which this type was uniqued.
Definition: Type.h:129
bool isPtrOrPtrVectorTy() const
Return true if this is a pointer type or a vector of pointer types.
Definition: Type.h:264
bool isIntegerTy() const
True if this is an instance of IntegerType.
Definition: Type.h:231
TypeSize getPrimitiveSizeInBits() const LLVM_READONLY
Return the basic size of this type if it is a primitive type.
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
Definition: Type.h:350
LLVM Value Representation.
Definition: Value.h:74
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:255
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition: Value.cpp:534
void takeName(Value *V)
Transfer the name from V to this value.
Definition: Value.cpp:383
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
static int analyzeLoadFromClobberingWrite(Type *LoadTy, Value *LoadPtr, Value *WritePtr, uint64_t WriteSizeInBits, const DataLayout &DL)
This function is called when we have a memdep query of a load that ends up being a clobbering memory ...
Definition: VNCoercion.cpp:173
Constant * getConstantLoadValueForLoad(Constant *SrcVal, unsigned Offset, Type *LoadTy, const DataLayout &DL)
Definition: VNCoercion.cpp:508
Value * coerceAvailableValueToLoadType(Value *StoredVal, Type *LoadedTy, IRBuilderBase &IRB, const DataLayout &DL)
If we saw a store of a value to memory, and then a load from a must-aliased pointer of a different ty...
Definition: VNCoercion.cpp:76
static Value * getStoreValueForLoadHelper(Value *SrcVal, unsigned Offset, Type *LoadTy, IRBuilderBase &Builder, const DataLayout &DL)
Definition: VNCoercion.cpp:399
static unsigned getLoadLoadClobberFullWidthSize(const Value *MemLocBase, int64_t MemLocOffs, unsigned MemLocSize, const LoadInst *LI)
Looks at a memory location for a load (specified by MemLocBase, Offs, and Size) and compares it again...
Definition: VNCoercion.cpp:236
int analyzeLoadFromClobberingStore(Type *LoadTy, Value *LoadPtr, StoreInst *DepSI, const DataLayout &DL)
This function determines whether a value for the pointer LoadPtr can be extracted from the store at D...
Definition: VNCoercion.cpp:211
Value * getLoadValueForLoad(LoadInst *SrcVal, unsigned Offset, Type *LoadTy, Instruction *InsertPt, const DataLayout &DL)
If analyzeLoadFromClobberingLoad returned an offset, this function can be used to actually perform th...
Definition: VNCoercion.cpp:463
Value * getStoreValueForLoad(Value *SrcVal, unsigned Offset, Type *LoadTy, Instruction *InsertPt, const DataLayout &DL)
If analyzeLoadFromClobberingStore returned an offset, this function can be used to actually perform t...
Definition: VNCoercion.cpp:445
Value * getMemInstValueForLoad(MemIntrinsic *SrcInst, unsigned Offset, Type *LoadTy, Instruction *InsertPt, const DataLayout &DL)
If analyzeLoadFromClobberingMemInst returned an offset, this function can be used to actually perform...
Definition: VNCoercion.cpp:520
int analyzeLoadFromClobberingLoad(Type *LoadTy, Value *LoadPtr, LoadInst *DepLI, const DataLayout &DL)
This function determines whether a value for the pointer LoadPtr can be extracted from the load at De...
Definition: VNCoercion.cpp:317
Constant * getConstantMemInstValueForLoad(MemIntrinsic *SrcInst, unsigned Offset, Type *LoadTy, const DataLayout &DL)
Definition: VNCoercion.cpp:567
int analyzeLoadFromClobberingMemInst(Type *LoadTy, Value *LoadPtr, MemIntrinsic *DepMI, const DataLayout &DL)
This function determines whether a value for the pointer LoadPtr can be extracted from the memory int...
Definition: VNCoercion.cpp:352
static bool isFirstClassAggregateOrScalableType(Type *Ty)
Definition: VNCoercion.cpp:13
Constant * getConstantStoreValueForLoad(Constant *SrcVal, unsigned Offset, Type *LoadTy, const DataLayout &DL)
Definition: VNCoercion.cpp:453
bool canCoerceMustAliasedValueToLoad(Value *StoredVal, Type *LoadTy, const DataLayout &DL)
Return true if CoerceAvailableValueToLoadType would succeed if it was called.
Definition: VNCoercion.cpp:18
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:406
Value * GetPointerBaseWithConstantOffset(Value *Ptr, int64_t &Offset, const DataLayout &DL, bool AllowNonInbounds=true)
Analyze the specified pointer to see if it can be expressed as a base pointer plus a constant offset.
const Value * getUnderlyingObject(const Value *V, unsigned MaxLookup=6)
This method strips off any GEP address adjustments and pointer casts from the specified value,...
T bit_ceil(T Value)
Returns the smallest integral power of two no smaller than Value if Value is nonzero.
Definition: bit.h:306
Constant * ConstantFoldConstant(const Constant *C, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr)
ConstantFoldConstant - Fold the constant using the specified DataLayout.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
Constant * ConstantFoldLoadFromConst(Constant *C, Type *Ty, const APInt &Offset, const DataLayout &DL)
Extract value of C at the given Offset reinterpreted as Ty.
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Definition: Alignment.h:155
Constant * ConstantFoldLoadFromConstPtr(Constant *C, Type *Ty, APInt Offset, const DataLayout &DL)
Return the value that a load from C with offset Offset would produce if it is constant and determinab...
constexpr uint64_t NextPowerOf2(uint64_t A)
Returns the next power of two (in 64-bits) that is strictly greater than A.
Definition: MathExtras.h:450
uint64_t value() const
This is a hole in the type system and should not be abused.
Definition: Alignment.h:85