1#include "llvm/Transforms/Utils/VNCoercion.h"
2#include "llvm/Analysis/ConstantFolding.h"
3#include "llvm/Analysis/ValueTracking.h"
4#include "llvm/IR/IRBuilder.h"
5#include "llvm/IR/IntrinsicInst.h"
6
7#define DEBUG_TYPE "vncoerce"
8
9using namespace llvm;
10using namespace VNCoercion;
11
12static bool isFirstClassAggregateOrScalableType(Type *Ty) {
13 return Ty->isStructTy() || Ty->isArrayTy() || Ty->isScalableTy();
14}
15
16/// Return true if coerceAvailableValueToLoadType will succeed.
17bool VNCoercion::canCoerceMustAliasedValueToLoad(Value *StoredVal, Type *LoadTy,
18 Function *F) {
19 Type *StoredTy = StoredVal->getType();
20 if (StoredTy == LoadTy)
21 return true;
22
23 const DataLayout &DL = F->getDataLayout();
24 TypeSize MinStoreSize = DL.getTypeSizeInBits(Ty: StoredTy);
25 TypeSize LoadSize = DL.getTypeSizeInBits(Ty: LoadTy);
26 if (isa<ScalableVectorType>(Val: StoredTy) && isa<ScalableVectorType>(Val: LoadTy) &&
27 MinStoreSize == LoadSize)
28 return true;
29
30 // If the loaded/stored value is a first class array/struct, don't try to
31 // transform them. We need to be able to bitcast to integer. For scalable
32 // vectors forwarded to fixed-sized vectors @llvm.vector.extract is used.
33 if (isa<ScalableVectorType>(Val: StoredTy) && isa<FixedVectorType>(Val: LoadTy)) {
34 if (StoredTy->getScalarType() != LoadTy->getScalarType())
35 return false;
36
37 // If it is known at compile-time that the VScale is larger than one,
38 // use that information to allow for wider loads.
39 const auto &Attrs = F->getAttributes().getFnAttrs();
40 unsigned MinVScale = Attrs.getVScaleRangeMin();
41 MinStoreSize =
42 TypeSize::getFixed(ExactSize: MinStoreSize.getKnownMinValue() * MinVScale);
43 } else if (isFirstClassAggregateOrScalableType(Ty: LoadTy) ||
44 isFirstClassAggregateOrScalableType(Ty: StoredTy)) {
45 return false;
46 }
47
48 // The store size must be byte-aligned to support future type casts.
49 if (llvm::alignTo(Size: MinStoreSize, Align: 8) != MinStoreSize)
50 return false;
51
52 // The store has to be at least as big as the load.
53 if (!TypeSize::isKnownGE(LHS: MinStoreSize, RHS: LoadSize))
54 return false;
55
56 bool StoredNI = DL.isNonIntegralPointerType(Ty: StoredTy->getScalarType());
57 bool LoadNI = DL.isNonIntegralPointerType(Ty: LoadTy->getScalarType());
58 // Don't coerce non-integral pointers to integers or vice versa.
59 if (StoredNI != LoadNI) {
60 // As a special case, allow coercion of memset used to initialize
61 // an array w/null. Despite non-integral pointers not generally having a
62 // specific bit pattern, we do assume null is zero.
63 if (auto *CI = dyn_cast<Constant>(Val: StoredVal))
64 return CI->isNullValue();
65 return false;
66 } else if (StoredNI && LoadNI &&
67 StoredTy->getPointerAddressSpace() !=
68 LoadTy->getPointerAddressSpace()) {
69 return false;
70 }
71
72 // The implementation below uses inttoptr for vectors of unequal size; we
73 // can't allow this for non integral pointers. We could teach it to extract
74 // exact subvectors if desired.
75 if (StoredNI && (StoredTy->isScalableTy() || MinStoreSize != LoadSize))
76 return false;
77
78 if (StoredTy->isTargetExtTy() || LoadTy->isTargetExtTy())
79 return false;
80
81 return true;
82}
83
84/// If we saw a store of a value to memory, and
85/// then a load from a must-aliased pointer of a different type, try to coerce
86/// the stored value. LoadedTy is the type of the load we want to replace.
87/// IRB is IRBuilder used to insert new instructions.
88///
89/// If we can't do it, return null.
90Value *VNCoercion::coerceAvailableValueToLoadType(Value *StoredVal,
91 Type *LoadedTy,
92 IRBuilderBase &Helper,
93 Function *F) {
94 assert(canCoerceMustAliasedValueToLoad(StoredVal, LoadedTy, F) &&
95 "precondition violation - materialization can't fail");
96 const DataLayout &DL = F->getDataLayout();
97 if (auto *C = dyn_cast<Constant>(Val: StoredVal))
98 StoredVal = ConstantFoldConstant(C, DL);
99
100 // If this is already the right type, just return it.
101 Type *StoredValTy = StoredVal->getType();
102
103 // If this is a scalable vector forwarded to a fixed vector load, create
104 // a @llvm.vector.extract instead of bitcasts.
105 if (isa<ScalableVectorType>(Val: StoredVal->getType()) &&
106 isa<FixedVectorType>(Val: LoadedTy)) {
107 return Helper.CreateIntrinsic(RetTy: LoadedTy, ID: Intrinsic::vector_extract,
108 Args: {StoredVal, Helper.getInt64(C: 0)});
109 }
110
111 TypeSize StoredValSize = DL.getTypeSizeInBits(Ty: StoredValTy);
112 TypeSize LoadedValSize = DL.getTypeSizeInBits(Ty: LoadedTy);
113
114 // If the store and reload are the same size, we can always reuse it.
115 if (StoredValSize == LoadedValSize) {
116 // Pointer to Pointer -> use bitcast.
117 if (StoredValTy->isPtrOrPtrVectorTy() && LoadedTy->isPtrOrPtrVectorTy()) {
118 StoredVal = Helper.CreateBitCast(V: StoredVal, DestTy: LoadedTy);
119 } else {
120 // Convert source pointers to integers, which can be bitcast.
121 if (StoredValTy->isPtrOrPtrVectorTy()) {
122 StoredValTy = DL.getIntPtrType(StoredValTy);
123 StoredVal = Helper.CreatePtrToInt(V: StoredVal, DestTy: StoredValTy);
124 }
125
126 Type *TypeToCastTo = LoadedTy;
127 if (TypeToCastTo->isPtrOrPtrVectorTy())
128 TypeToCastTo = DL.getIntPtrType(TypeToCastTo);
129
130 if (StoredValTy != TypeToCastTo)
131 StoredVal = Helper.CreateBitCast(V: StoredVal, DestTy: TypeToCastTo);
132
133 // Cast to pointer if the load needs a pointer type.
134 if (LoadedTy->isPtrOrPtrVectorTy())
135 StoredVal = Helper.CreateIntToPtr(V: StoredVal, DestTy: LoadedTy);
136 }
137
138 if (auto *C = dyn_cast<ConstantExpr>(Val: StoredVal))
139 StoredVal = ConstantFoldConstant(C, DL);
140
141 return StoredVal;
142 }
143 // If the loaded value is smaller than the available value, then we can
144 // extract out a piece from it. If the available value is too small, then we
145 // can't do anything.
146 assert(!StoredValSize.isScalable() &&
147 TypeSize::isKnownGE(StoredValSize, LoadedValSize) &&
148 "canCoerceMustAliasedValueToLoad fail");
149
150 // Convert source pointers to integers, which can be manipulated.
151 if (StoredValTy->isPtrOrPtrVectorTy()) {
152 StoredValTy = DL.getIntPtrType(StoredValTy);
153 StoredVal = Helper.CreatePtrToInt(V: StoredVal, DestTy: StoredValTy);
154 }
155
156 // Convert vectors and fp to integer, which can be manipulated.
157 if (!StoredValTy->isIntegerTy()) {
158 StoredValTy = IntegerType::get(C&: StoredValTy->getContext(), NumBits: StoredValSize);
159 StoredVal = Helper.CreateBitCast(V: StoredVal, DestTy: StoredValTy);
160 }
161
162 // If this is a big-endian system, we need to shift the value down to the low
163 // bits so that a truncate will work.
164 if (DL.isBigEndian()) {
165 uint64_t ShiftAmt = DL.getTypeStoreSizeInBits(Ty: StoredValTy).getFixedValue() -
166 DL.getTypeStoreSizeInBits(Ty: LoadedTy).getFixedValue();
167 StoredVal = Helper.CreateLShr(
168 LHS: StoredVal, RHS: ConstantInt::get(Ty: StoredVal->getType(), V: ShiftAmt));
169 }
170
171 // Truncate the integer to the right size now.
172 Type *NewIntTy = IntegerType::get(C&: StoredValTy->getContext(), NumBits: LoadedValSize);
173 StoredVal = Helper.CreateTruncOrBitCast(V: StoredVal, DestTy: NewIntTy);
174
175 if (LoadedTy != NewIntTy) {
176 // If the result is a pointer, inttoptr.
177 if (LoadedTy->isPtrOrPtrVectorTy())
178 StoredVal = Helper.CreateIntToPtr(V: StoredVal, DestTy: LoadedTy);
179 else
180 // Otherwise, bitcast.
181 StoredVal = Helper.CreateBitCast(V: StoredVal, DestTy: LoadedTy);
182 }
183
184 if (auto *C = dyn_cast<Constant>(Val: StoredVal))
185 StoredVal = ConstantFoldConstant(C, DL);
186
187 return StoredVal;
188}
189
190/// This function is called when we have a memdep query of a load that ends up
191/// being a clobbering memory write (store, memset, memcpy, memmove). This
192/// means that the write *may* provide bits used by the load but we can't be
193/// sure because the pointers don't must-alias.
194///
195/// Check this case to see if there is anything more we can do before we give
196/// up. This returns -1 if we have to give up, or a byte number in the stored
197/// value of the piece that feeds the load.
198static int analyzeLoadFromClobberingWrite(Type *LoadTy, Value *LoadPtr,
199 Value *WritePtr,
200 uint64_t WriteSizeInBits,
201 const DataLayout &DL) {
202 // If the loaded/stored value is a first class array/struct, or scalable type,
203 // don't try to transform them. We need to be able to bitcast to integer.
204 if (isFirstClassAggregateOrScalableType(Ty: LoadTy))
205 return -1;
206
207 int64_t StoreOffset = 0, LoadOffset = 0;
208 Value *StoreBase =
209 GetPointerBaseWithConstantOffset(Ptr: WritePtr, Offset&: StoreOffset, DL);
210 Value *LoadBase = GetPointerBaseWithConstantOffset(Ptr: LoadPtr, Offset&: LoadOffset, DL);
211 if (StoreBase != LoadBase)
212 return -1;
213
214 uint64_t LoadSize = DL.getTypeSizeInBits(Ty: LoadTy).getFixedValue();
215
216 if ((WriteSizeInBits & 7) | (LoadSize & 7))
217 return -1;
218 uint64_t StoreSize = WriteSizeInBits / 8; // Convert to bytes.
219 LoadSize /= 8;
220
221 // If the Load isn't completely contained within the stored bits, we don't
222 // have all the bits to feed it. We could do something crazy in the future
223 // (issue a smaller load then merge the bits in) but this seems unlikely to be
224 // valuable.
225 if (StoreOffset > LoadOffset ||
226 StoreOffset + int64_t(StoreSize) < LoadOffset + int64_t(LoadSize))
227 return -1;
228
229 // Okay, we can do this transformation. Return the number of bytes into the
230 // store that the load is.
231 return LoadOffset - StoreOffset;
232}
233
234/// This function is called when we have a
235/// memdep query of a load that ends up being a clobbering store.
236int VNCoercion::analyzeLoadFromClobberingStore(Type *LoadTy, Value *LoadPtr,
237 StoreInst *DepSI,
238 const DataLayout &DL) {
239 auto *StoredVal = DepSI->getValueOperand();
240
241 // Cannot handle reading from store of first-class aggregate or scalable type.
242 if (isFirstClassAggregateOrScalableType(Ty: StoredVal->getType()))
243 return -1;
244
245 if (!canCoerceMustAliasedValueToLoad(StoredVal, LoadTy, F: DepSI->getFunction()))
246 return -1;
247
248 Value *StorePtr = DepSI->getPointerOperand();
249 uint64_t StoreSize =
250 DL.getTypeSizeInBits(Ty: DepSI->getValueOperand()->getType()).getFixedValue();
251 return analyzeLoadFromClobberingWrite(LoadTy, LoadPtr, WritePtr: StorePtr, WriteSizeInBits: StoreSize,
252 DL);
253}
254
255/// This function is called when we have a
256/// memdep query of a load that ends up being clobbered by another load. See if
257/// the other load can feed into the second load.
258int VNCoercion::analyzeLoadFromClobberingLoad(Type *LoadTy, Value *LoadPtr,
259 LoadInst *DepLI,
260 const DataLayout &DL) {
261 // Cannot handle reading from store of first-class aggregate or scalable type.
262 if (isFirstClassAggregateOrScalableType(Ty: DepLI->getType()))
263 return -1;
264
265 if (!canCoerceMustAliasedValueToLoad(StoredVal: DepLI, LoadTy, F: DepLI->getFunction()))
266 return -1;
267
268 Value *DepPtr = DepLI->getPointerOperand();
269 uint64_t DepSize = DL.getTypeSizeInBits(Ty: DepLI->getType()).getFixedValue();
270 return analyzeLoadFromClobberingWrite(LoadTy, LoadPtr, WritePtr: DepPtr, WriteSizeInBits: DepSize, DL);
271}
272
273int VNCoercion::analyzeLoadFromClobberingMemInst(Type *LoadTy, Value *LoadPtr,
274 MemIntrinsic *MI,
275 const DataLayout &DL) {
276 // If the mem operation is a non-constant size, we can't handle it.
277 ConstantInt *SizeCst = dyn_cast<ConstantInt>(Val: MI->getLength());
278 if (!SizeCst)
279 return -1;
280 uint64_t MemSizeInBits = SizeCst->getZExtValue() * 8;
281
282 // If this is memset, we just need to see if the offset is valid in the size
283 // of the memset..
284 if (const auto *Memset = dyn_cast<MemSetInst>(Val: MI)) {
285 if (DL.isNonIntegralPointerType(Ty: LoadTy->getScalarType())) {
286 auto *CI = dyn_cast<ConstantInt>(Val: Memset->getValue());
287 if (!CI || !CI->isZero())
288 return -1;
289 }
290 return analyzeLoadFromClobberingWrite(LoadTy, LoadPtr, WritePtr: MI->getDest(),
291 WriteSizeInBits: MemSizeInBits, DL);
292 }
293
294 // If we have a memcpy/memmove, the only case we can handle is if this is a
295 // copy from constant memory. In that case, we can read directly from the
296 // constant memory.
297 MemTransferInst *MTI = cast<MemTransferInst>(Val: MI);
298
299 Constant *Src = dyn_cast<Constant>(Val: MTI->getSource());
300 if (!Src)
301 return -1;
302
303 GlobalVariable *GV = dyn_cast<GlobalVariable>(Val: getUnderlyingObject(V: Src));
304 if (!GV || !GV->isConstant() || !GV->hasDefinitiveInitializer())
305 return -1;
306
307 // See if the access is within the bounds of the transfer.
308 int Offset = analyzeLoadFromClobberingWrite(LoadTy, LoadPtr, WritePtr: MI->getDest(),
309 WriteSizeInBits: MemSizeInBits, DL);
310 if (Offset == -1)
311 return Offset;
312
313 // Otherwise, see if we can constant fold a load from the constant with the
314 // offset applied as appropriate.
315 unsigned IndexSize = DL.getIndexTypeSizeInBits(Ty: Src->getType());
316 if (ConstantFoldLoadFromConstPtr(C: Src, Ty: LoadTy, Offset: APInt(IndexSize, Offset), DL))
317 return Offset;
318 return -1;
319}
320
321static Value *getStoreValueForLoadHelper(Value *SrcVal, unsigned Offset,
322 Type *LoadTy, IRBuilderBase &Builder,
323 const DataLayout &DL) {
324 LLVMContext &Ctx = SrcVal->getType()->getContext();
325 Type *SrcTy = SrcVal->getType();
326
327 // If two pointers or pointer vectors have the same size and address space,
328 // we don't need to do any truncation, etc. This avoids introducing
329 // ptrtoint, which drops provenance and is invalid for non-integral pointers.
330 if (SrcTy->isPtrOrPtrVectorTy() && LoadTy->isPtrOrPtrVectorTy() &&
331 SrcTy->getPointerAddressSpace() == LoadTy->getPointerAddressSpace() &&
332 DL.getTypeSizeInBits(Ty: SrcTy) == DL.getTypeSizeInBits(Ty: LoadTy)) {
333 return SrcVal;
334 }
335
336 // Return scalable values directly to avoid needing to bitcast to integer
337 // types, as we do not support non-zero Offsets.
338 if (isa<ScalableVectorType>(Val: LoadTy)) {
339 assert(Offset == 0 && "Expected a zero offset for scalable types");
340 return SrcVal;
341 }
342
343 // For the case of a scalable vector being forwarded to a fixed-sized load,
344 // only equal element types are allowed and a @llvm.vector.extract will be
345 // used instead of bitcasts.
346 if (isa<ScalableVectorType>(Val: SrcVal->getType()) &&
347 isa<FixedVectorType>(Val: LoadTy)) {
348 assert(Offset == 0 &&
349 SrcVal->getType()->getScalarType() == LoadTy->getScalarType());
350 return SrcVal;
351 }
352
353 uint64_t StoreSize =
354 (DL.getTypeSizeInBits(Ty: SrcVal->getType()).getFixedValue() + 7) / 8;
355 uint64_t LoadSize = (DL.getTypeSizeInBits(Ty: LoadTy).getFixedValue() + 7) / 8;
356
357 // Shift the bits to the least significant depending on endianness.
358 unsigned ShiftAmt;
359 if (DL.isLittleEndian())
360 ShiftAmt = Offset * 8;
361 else
362 ShiftAmt = (StoreSize - LoadSize - Offset) * 8;
363
364 // Integers drop provenance, and one poison bit poisons the whole value.
365 if (SrcTy->isByteOrByteVectorTy() ||
366 (SrcTy->isPtrOrPtrVectorTy() && LoadTy->isByteOrByteVectorTy())) {
367 unsigned SrcBits = DL.getTypeSizeInBits(Ty: SrcTy).getFixedValue();
368 unsigned LoadBits = DL.getTypeSizeInBits(Ty: LoadTy).getFixedValue();
369 if (LoadBits == SrcBits)
370 return Builder.CreateBitCast(V: SrcVal, DestTy: LoadTy);
371 // The lanes of the stored and loaded vectors may not line up, so extract
372 // from the stored value as a scalar byte type.
373 SrcVal = Builder.CreateBitCast(V: SrcVal, DestTy: Type::getByteNTy(C&: Ctx, N: SrcBits));
374 Type *ExtractTy =
375 LoadTy->isVectorTy() ? Type::getByteNTy(C&: Ctx, N: LoadBits) : LoadTy;
376 SrcVal =
377 Builder.CreateBitExtract(Ty: ExtractTy, Src: SrcVal, Offset: Builder.getInt32(C: ShiftAmt));
378 return Builder.CreateBitCast(V: SrcVal, DestTy: LoadTy);
379 }
380
381 // Compute which bits of the stored value are being used by the load. Convert
382 // to an integer type to start with.
383 if (SrcVal->getType()->isPtrOrPtrVectorTy())
384 SrcVal =
385 Builder.CreatePtrToInt(V: SrcVal, DestTy: DL.getIntPtrType(SrcVal->getType()));
386 if (!SrcVal->getType()->isIntegerTy())
387 SrcVal =
388 Builder.CreateBitCast(V: SrcVal, DestTy: IntegerType::get(C&: Ctx, NumBits: StoreSize * 8));
389
390 if (ShiftAmt)
391 SrcVal = Builder.CreateLShr(LHS: SrcVal,
392 RHS: ConstantInt::get(Ty: SrcVal->getType(), V: ShiftAmt));
393
394 if (LoadSize != StoreSize)
395 SrcVal = Builder.CreateTruncOrBitCast(V: SrcVal,
396 DestTy: IntegerType::get(C&: Ctx, NumBits: LoadSize * 8));
397 return SrcVal;
398}
399
400Value *VNCoercion::getValueForLoad(Value *SrcVal, unsigned Offset, Type *LoadTy,
401 Instruction *InsertPt, Function *F) {
402 const DataLayout &DL = F->getDataLayout();
403#ifndef NDEBUG
404 TypeSize MinSrcValSize = DL.getTypeStoreSize(SrcVal->getType());
405 TypeSize LoadSize = DL.getTypeStoreSize(LoadTy);
406 if (MinSrcValSize.isScalable() && !LoadSize.isScalable())
407 MinSrcValSize =
408 TypeSize::getFixed(MinSrcValSize.getKnownMinValue() *
409 F->getAttributes().getFnAttrs().getVScaleRangeMin());
410 assert((MinSrcValSize.isScalable() || Offset + LoadSize <= MinSrcValSize) &&
411 "Expected Offset + LoadSize <= SrcValSize");
412 assert((!MinSrcValSize.isScalable() ||
413 (Offset == 0 && TypeSize::isKnownLE(LoadSize, MinSrcValSize))) &&
414 "Expected offset of zero and LoadSize <= SrcValSize");
415#endif
416 IRBuilder<> Builder(InsertPt);
417 SrcVal = getStoreValueForLoadHelper(SrcVal, Offset, LoadTy, Builder, DL);
418 return coerceAvailableValueToLoadType(StoredVal: SrcVal, LoadedTy: LoadTy, Helper&: Builder, F);
419}
420
421Constant *VNCoercion::getConstantValueForLoad(Constant *SrcVal, unsigned Offset,
422 Type *LoadTy,
423 const DataLayout &DL) {
424#ifndef NDEBUG
425 unsigned SrcValSize = DL.getTypeStoreSize(SrcVal->getType()).getFixedValue();
426 unsigned LoadSize = DL.getTypeStoreSize(LoadTy).getFixedValue();
427 assert(Offset + LoadSize <= SrcValSize);
428#endif
429 return ConstantFoldLoadFromConst(C: SrcVal, Ty: LoadTy, Offset: APInt(32, Offset), DL);
430}
431
432/// This function is called when we have a
433/// memdep query of a load that ends up being a clobbering mem intrinsic.
434Value *VNCoercion::getMemInstValueForLoad(MemIntrinsic *SrcInst,
435 unsigned Offset, Type *LoadTy,
436 Instruction *InsertPt,
437 const DataLayout &DL) {
438 LLVMContext &Ctx = LoadTy->getContext();
439 uint64_t LoadSize = DL.getTypeSizeInBits(Ty: LoadTy).getFixedValue() / 8;
440 IRBuilder<> Builder(InsertPt);
441
442 // We know that this method is only called when the mem transfer fully
443 // provides the bits for the load.
444 if (MemSetInst *MSI = dyn_cast<MemSetInst>(Val: SrcInst)) {
445 // memset(P, 'x', 1234) -> splat('x'), even if x is a variable, and
446 // independently of what the offset is.
447 Value *Val = MSI->getValue();
448 if (LoadSize != 1)
449 Val =
450 Builder.CreateZExtOrBitCast(V: Val, DestTy: IntegerType::get(C&: Ctx, NumBits: LoadSize * 8));
451 Value *OneElt = Val;
452
453 // Splat the value out to the right number of bits.
454 for (unsigned NumBytesSet = 1; NumBytesSet != LoadSize;) {
455 // If we can double the number of bytes set, do it.
456 if (NumBytesSet * 2 <= LoadSize) {
457 Value *ShVal = Builder.CreateShl(
458 LHS: Val, RHS: ConstantInt::get(Ty: Val->getType(), V: NumBytesSet * 8));
459 Val = Builder.CreateOr(LHS: Val, RHS: ShVal);
460 NumBytesSet <<= 1;
461 continue;
462 }
463
464 // Otherwise insert one byte at a time.
465 Value *ShVal =
466 Builder.CreateShl(LHS: Val, RHS: ConstantInt::get(Ty: Val->getType(), V: 1 * 8));
467 Val = Builder.CreateOr(LHS: OneElt, RHS: ShVal);
468 ++NumBytesSet;
469 }
470
471 return coerceAvailableValueToLoadType(StoredVal: Val, LoadedTy: LoadTy, Helper&: Builder,
472 F: InsertPt->getFunction());
473 }
474
475 // Otherwise, this is a memcpy/memmove from a constant global.
476 MemTransferInst *MTI = cast<MemTransferInst>(Val: SrcInst);
477 Constant *Src = cast<Constant>(Val: MTI->getSource());
478 unsigned IndexSize = DL.getIndexTypeSizeInBits(Ty: Src->getType());
479 return ConstantFoldLoadFromConstPtr(C: Src, Ty: LoadTy, Offset: APInt(IndexSize, Offset),
480 DL);
481}
482
483Constant *VNCoercion::getConstantMemInstValueForLoad(MemIntrinsic *SrcInst,
484 unsigned Offset,
485 Type *LoadTy,
486 const DataLayout &DL) {
487 LLVMContext &Ctx = LoadTy->getContext();
488 uint64_t LoadSize = DL.getTypeSizeInBits(Ty: LoadTy).getFixedValue() / 8;
489
490 // We know that this method is only called when the mem transfer fully
491 // provides the bits for the load.
492 if (MemSetInst *MSI = dyn_cast<MemSetInst>(Val: SrcInst)) {
493 auto *Val = dyn_cast<ConstantInt>(Val: MSI->getValue());
494 if (!Val)
495 return nullptr;
496
497 Val = ConstantInt::get(Context&: Ctx, V: APInt::getSplat(NewLen: LoadSize * 8, V: Val->getValue()));
498 return ConstantFoldLoadFromConst(C: Val, Ty: LoadTy, DL);
499 }
500
501 // Otherwise, this is a memcpy/memmove from a constant global.
502 MemTransferInst *MTI = cast<MemTransferInst>(Val: SrcInst);
503 Constant *Src = cast<Constant>(Val: MTI->getSource());
504 unsigned IndexSize = DL.getIndexTypeSizeInBits(Ty: Src->getType());
505 return ConstantFoldLoadFromConstPtr(C: Src, Ty: LoadTy, Offset: APInt(IndexSize, Offset),
506 DL);
507}
508