1 #include "llvm/Transforms/Utils/VNCoercion.h"
2 #include "llvm/Analysis/AliasAnalysis.h"
3 #include "llvm/Analysis/ConstantFolding.h"
4 #include "llvm/Analysis/ValueTracking.h"
5 #include "llvm/IR/IRBuilder.h"
6 #include "llvm/IR/IntrinsicInst.h"
7 #include "llvm/Support/Debug.h"
8 
9 #define DEBUG_TYPE "vncoerce"
10 namespace llvm {
11 namespace VNCoercion {
12 
13 /// Return true if coerceAvailableValueToLoadType will succeed.
14 bool canCoerceMustAliasedValueToLoad(Value *StoredVal, Type *LoadTy,
15                                      const DataLayout &DL) {
16   Type *StoredTy = StoredVal->getType();
17   if (StoredTy == LoadTy)
18     return true;
19 
20   // If the loaded or stored value is an first class array or struct, don't try
21   // to transform them.  We need to be able to bitcast to integer.
22   if (LoadTy->isStructTy() || LoadTy->isArrayTy() || StoredTy->isStructTy() ||
23       StoredTy->isArrayTy())
24     return false;
25 
26   uint64_t StoreSize = DL.getTypeSizeInBits(StoredTy);
27 
28   // The store size must be byte-aligned to support future type casts.
29   if (llvm::alignTo(StoreSize, 8) != StoreSize)
30     return false;
31 
32   // The store has to be at least as big as the load.
33   if (StoreSize < DL.getTypeSizeInBits(LoadTy))
34     return false;
35 
36   // Don't coerce non-integral pointers to integers or vice versa.
37   if (DL.isNonIntegralPointerType(StoredVal->getType()->getScalarType()) !=
38       DL.isNonIntegralPointerType(LoadTy->getScalarType())) {
39     // As a special case, allow coercion of memset used to initialize
40     // an array w/null.  Despite non-integral pointers not generally having a
41     // specific bit pattern, we do assume null is zero.
42     if (auto *CI = dyn_cast<Constant>(StoredVal))
43       return CI->isNullValue();
44     return false;
45   }
46 
47   return true;
48 }
49 
50 template <class T, class HelperClass>
51 static T *coerceAvailableValueToLoadTypeHelper(T *StoredVal, Type *LoadedTy,
52                                                HelperClass &Helper,
53                                                const DataLayout &DL) {
54   assert(canCoerceMustAliasedValueToLoad(StoredVal, LoadedTy, DL) &&
55          "precondition violation - materialization can't fail");
56   if (auto *C = dyn_cast<Constant>(StoredVal))
57     StoredVal = ConstantFoldConstant(C, DL);
58 
59   // If this is already the right type, just return it.
60   Type *StoredValTy = StoredVal->getType();
61 
62   uint64_t StoredValSize = DL.getTypeSizeInBits(StoredValTy);
63   uint64_t LoadedValSize = DL.getTypeSizeInBits(LoadedTy);
64 
65   // If the store and reload are the same size, we can always reuse it.
66   if (StoredValSize == LoadedValSize) {
67     // Pointer to Pointer -> use bitcast.
68     if (StoredValTy->isPtrOrPtrVectorTy() && LoadedTy->isPtrOrPtrVectorTy()) {
69       StoredVal = Helper.CreateBitCast(StoredVal, LoadedTy);
70     } else {
71       // Convert source pointers to integers, which can be bitcast.
72       if (StoredValTy->isPtrOrPtrVectorTy()) {
73         StoredValTy = DL.getIntPtrType(StoredValTy);
74         StoredVal = Helper.CreatePtrToInt(StoredVal, StoredValTy);
75       }
76 
77       Type *TypeToCastTo = LoadedTy;
78       if (TypeToCastTo->isPtrOrPtrVectorTy())
79         TypeToCastTo = DL.getIntPtrType(TypeToCastTo);
80 
81       if (StoredValTy != TypeToCastTo)
82         StoredVal = Helper.CreateBitCast(StoredVal, TypeToCastTo);
83 
84       // Cast to pointer if the load needs a pointer type.
85       if (LoadedTy->isPtrOrPtrVectorTy())
86         StoredVal = Helper.CreateIntToPtr(StoredVal, LoadedTy);
87     }
88 
89     if (auto *C = dyn_cast<ConstantExpr>(StoredVal))
90       StoredVal = ConstantFoldConstant(C, DL);
91 
92     return StoredVal;
93   }
94   // If the loaded value is smaller than the available value, then we can
95   // extract out a piece from it.  If the available value is too small, then we
96   // can't do anything.
97   assert(StoredValSize >= LoadedValSize &&
98          "canCoerceMustAliasedValueToLoad fail");
99 
100   // Convert source pointers to integers, which can be manipulated.
101   if (StoredValTy->isPtrOrPtrVectorTy()) {
102     StoredValTy = DL.getIntPtrType(StoredValTy);
103     StoredVal = Helper.CreatePtrToInt(StoredVal, StoredValTy);
104   }
105 
106   // Convert vectors and fp to integer, which can be manipulated.
107   if (!StoredValTy->isIntegerTy()) {
108     StoredValTy = IntegerType::get(StoredValTy->getContext(), StoredValSize);
109     StoredVal = Helper.CreateBitCast(StoredVal, StoredValTy);
110   }
111 
112   // If this is a big-endian system, we need to shift the value down to the low
113   // bits so that a truncate will work.
114   if (DL.isBigEndian()) {
115     uint64_t ShiftAmt = DL.getTypeStoreSizeInBits(StoredValTy) -
116                         DL.getTypeStoreSizeInBits(LoadedTy);
117     StoredVal = Helper.CreateLShr(
118         StoredVal, ConstantInt::get(StoredVal->getType(), ShiftAmt));
119   }
120 
121   // Truncate the integer to the right size now.
122   Type *NewIntTy = IntegerType::get(StoredValTy->getContext(), LoadedValSize);
123   StoredVal = Helper.CreateTruncOrBitCast(StoredVal, NewIntTy);
124 
125   if (LoadedTy != NewIntTy) {
126     // If the result is a pointer, inttoptr.
127     if (LoadedTy->isPtrOrPtrVectorTy())
128       StoredVal = Helper.CreateIntToPtr(StoredVal, LoadedTy);
129     else
130       // Otherwise, bitcast.
131       StoredVal = Helper.CreateBitCast(StoredVal, LoadedTy);
132   }
133 
134   if (auto *C = dyn_cast<Constant>(StoredVal))
135     StoredVal = ConstantFoldConstant(C, DL);
136 
137   return StoredVal;
138 }
139 
140 /// If we saw a store of a value to memory, and
141 /// then a load from a must-aliased pointer of a different type, try to coerce
142 /// the stored value.  LoadedTy is the type of the load we want to replace.
143 /// IRB is IRBuilder used to insert new instructions.
144 ///
145 /// If we can't do it, return null.
146 Value *coerceAvailableValueToLoadType(Value *StoredVal, Type *LoadedTy,
147                                       IRBuilderBase &IRB,
148                                       const DataLayout &DL) {
149   return coerceAvailableValueToLoadTypeHelper(StoredVal, LoadedTy, IRB, DL);
150 }
151 
152 /// This function is called when we have a memdep query of a load that ends up
153 /// being a clobbering memory write (store, memset, memcpy, memmove).  This
154 /// means that the write *may* provide bits used by the load but we can't be
155 /// sure because the pointers don't must-alias.
156 ///
157 /// Check this case to see if there is anything more we can do before we give
158 /// up.  This returns -1 if we have to give up, or a byte number in the stored
159 /// value of the piece that feeds the load.
160 static int analyzeLoadFromClobberingWrite(Type *LoadTy, Value *LoadPtr,
161                                           Value *WritePtr,
162                                           uint64_t WriteSizeInBits,
163                                           const DataLayout &DL) {
164   // If the loaded or stored value is a first class array or struct, don't try
165   // to transform them.  We need to be able to bitcast to integer.
166   if (LoadTy->isStructTy() || LoadTy->isArrayTy())
167     return -1;
168 
169   int64_t StoreOffset = 0, LoadOffset = 0;
170   Value *StoreBase =
171       GetPointerBaseWithConstantOffset(WritePtr, StoreOffset, DL);
172   Value *LoadBase = GetPointerBaseWithConstantOffset(LoadPtr, LoadOffset, DL);
173   if (StoreBase != LoadBase)
174     return -1;
175 
176   // If the load and store are to the exact same address, they should have been
177   // a must alias.  AA must have gotten confused.
178   // FIXME: Study to see if/when this happens.  One case is forwarding a memset
179   // to a load from the base of the memset.
180 
181   // If the load and store don't overlap at all, the store doesn't provide
182   // anything to the load.  In this case, they really don't alias at all, AA
183   // must have gotten confused.
184   uint64_t LoadSize = DL.getTypeSizeInBits(LoadTy);
185 
186   if ((WriteSizeInBits & 7) | (LoadSize & 7))
187     return -1;
188   uint64_t StoreSize = WriteSizeInBits / 8; // Convert to bytes.
189   LoadSize /= 8;
190 
191   bool isAAFailure = false;
192   if (StoreOffset < LoadOffset)
193     isAAFailure = StoreOffset + int64_t(StoreSize) <= LoadOffset;
194   else
195     isAAFailure = LoadOffset + int64_t(LoadSize) <= StoreOffset;
196 
197   if (isAAFailure)
198     return -1;
199 
200   // If the Load isn't completely contained within the stored bits, we don't
201   // have all the bits to feed it.  We could do something crazy in the future
202   // (issue a smaller load then merge the bits in) but this seems unlikely to be
203   // valuable.
204   if (StoreOffset > LoadOffset ||
205       StoreOffset + StoreSize < LoadOffset + LoadSize)
206     return -1;
207 
208   // Okay, we can do this transformation.  Return the number of bytes into the
209   // store that the load is.
210   return LoadOffset - StoreOffset;
211 }
212 
213 /// This function is called when we have a
214 /// memdep query of a load that ends up being a clobbering store.
215 int analyzeLoadFromClobberingStore(Type *LoadTy, Value *LoadPtr,
216                                    StoreInst *DepSI, const DataLayout &DL) {
217   auto *StoredVal = DepSI->getValueOperand();
218 
219   // Cannot handle reading from store of first-class aggregate yet.
220   if (StoredVal->getType()->isStructTy() ||
221       StoredVal->getType()->isArrayTy())
222     return -1;
223 
224   // Don't coerce non-integral pointers to integers or vice versa.
225   if (DL.isNonIntegralPointerType(StoredVal->getType()->getScalarType()) !=
226       DL.isNonIntegralPointerType(LoadTy->getScalarType())) {
227     // Allow casts of zero values to null as a special case
228     auto *CI = dyn_cast<Constant>(StoredVal);
229     if (!CI || !CI->isNullValue())
230       return -1;
231   }
232 
233   Value *StorePtr = DepSI->getPointerOperand();
234   uint64_t StoreSize =
235       DL.getTypeSizeInBits(DepSI->getValueOperand()->getType());
236   return analyzeLoadFromClobberingWrite(LoadTy, LoadPtr, StorePtr, StoreSize,
237                                         DL);
238 }
239 
240 /// Looks at a memory location for a load (specified by MemLocBase, Offs, and
241 /// Size) and compares it against a load.
242 ///
243 /// If the specified load could be safely widened to a larger integer load
244 /// that is 1) still efficient, 2) safe for the target, and 3) would provide
245 /// the specified memory location value, then this function returns the size
246 /// in bytes of the load width to use.  If not, this returns zero.
247 static unsigned getLoadLoadClobberFullWidthSize(const Value *MemLocBase,
248                                                 int64_t MemLocOffs,
249                                                 unsigned MemLocSize,
250                                                 const LoadInst *LI) {
251   // We can only extend simple integer loads.
252   if (!isa<IntegerType>(LI->getType()) || !LI->isSimple())
253     return 0;
254 
255   // Load widening is hostile to ThreadSanitizer: it may cause false positives
256   // or make the reports more cryptic (access sizes are wrong).
257   if (LI->getParent()->getParent()->hasFnAttribute(Attribute::SanitizeThread))
258     return 0;
259 
260   const DataLayout &DL = LI->getModule()->getDataLayout();
261 
262   // Get the base of this load.
263   int64_t LIOffs = 0;
264   const Value *LIBase =
265       GetPointerBaseWithConstantOffset(LI->getPointerOperand(), LIOffs, DL);
266 
267   // If the two pointers are not based on the same pointer, we can't tell that
268   // they are related.
269   if (LIBase != MemLocBase)
270     return 0;
271 
272   // Okay, the two values are based on the same pointer, but returned as
273   // no-alias.  This happens when we have things like two byte loads at "P+1"
274   // and "P+3".  Check to see if increasing the size of the "LI" load up to its
275   // alignment (or the largest native integer type) will allow us to load all
276   // the bits required by MemLoc.
277 
278   // If MemLoc is before LI, then no widening of LI will help us out.
279   if (MemLocOffs < LIOffs)
280     return 0;
281 
282   // Get the alignment of the load in bytes.  We assume that it is safe to load
283   // any legal integer up to this size without a problem.  For example, if we're
284   // looking at an i8 load on x86-32 that is known 1024 byte aligned, we can
285   // widen it up to an i32 load.  If it is known 2-byte aligned, we can widen it
286   // to i16.
287   unsigned LoadAlign = LI->getAlignment();
288 
289   int64_t MemLocEnd = MemLocOffs + MemLocSize;
290 
291   // If no amount of rounding up will let MemLoc fit into LI, then bail out.
292   if (LIOffs + LoadAlign < MemLocEnd)
293     return 0;
294 
295   // This is the size of the load to try.  Start with the next larger power of
296   // two.
297   unsigned NewLoadByteSize = LI->getType()->getPrimitiveSizeInBits() / 8U;
298   NewLoadByteSize = NextPowerOf2(NewLoadByteSize);
299 
300   while (true) {
301     // If this load size is bigger than our known alignment or would not fit
302     // into a native integer register, then we fail.
303     if (NewLoadByteSize > LoadAlign ||
304         !DL.fitsInLegalInteger(NewLoadByteSize * 8))
305       return 0;
306 
307     if (LIOffs + NewLoadByteSize > MemLocEnd &&
308         (LI->getParent()->getParent()->hasFnAttribute(
309              Attribute::SanitizeAddress) ||
310          LI->getParent()->getParent()->hasFnAttribute(
311              Attribute::SanitizeHWAddress)))
312       // We will be reading past the location accessed by the original program.
313       // While this is safe in a regular build, Address Safety analysis tools
314       // may start reporting false warnings. So, don't do widening.
315       return 0;
316 
317     // If a load of this width would include all of MemLoc, then we succeed.
318     if (LIOffs + NewLoadByteSize >= MemLocEnd)
319       return NewLoadByteSize;
320 
321     NewLoadByteSize <<= 1;
322   }
323 }
324 
325 /// This function is called when we have a
326 /// memdep query of a load that ends up being clobbered by another load.  See if
327 /// the other load can feed into the second load.
328 int analyzeLoadFromClobberingLoad(Type *LoadTy, Value *LoadPtr, LoadInst *DepLI,
329                                   const DataLayout &DL) {
330   // Cannot handle reading from store of first-class aggregate yet.
331   if (DepLI->getType()->isStructTy() || DepLI->getType()->isArrayTy())
332     return -1;
333 
334   // Don't coerce non-integral pointers to integers or vice versa.
335   if (DL.isNonIntegralPointerType(DepLI->getType()->getScalarType()) !=
336       DL.isNonIntegralPointerType(LoadTy->getScalarType()))
337     return -1;
338 
339   Value *DepPtr = DepLI->getPointerOperand();
340   uint64_t DepSize = DL.getTypeSizeInBits(DepLI->getType());
341   int R = analyzeLoadFromClobberingWrite(LoadTy, LoadPtr, DepPtr, DepSize, DL);
342   if (R != -1)
343     return R;
344 
345   // If we have a load/load clobber an DepLI can be widened to cover this load,
346   // then we should widen it!
347   int64_t LoadOffs = 0;
348   const Value *LoadBase =
349       GetPointerBaseWithConstantOffset(LoadPtr, LoadOffs, DL);
350   unsigned LoadSize = DL.getTypeStoreSize(LoadTy);
351 
352   unsigned Size =
353       getLoadLoadClobberFullWidthSize(LoadBase, LoadOffs, LoadSize, DepLI);
354   if (Size == 0)
355     return -1;
356 
357   // Check non-obvious conditions enforced by MDA which we rely on for being
358   // able to materialize this potentially available value
359   assert(DepLI->isSimple() && "Cannot widen volatile/atomic load!");
360   assert(DepLI->getType()->isIntegerTy() && "Can't widen non-integer load");
361 
362   return analyzeLoadFromClobberingWrite(LoadTy, LoadPtr, DepPtr, Size * 8, DL);
363 }
364 
365 int analyzeLoadFromClobberingMemInst(Type *LoadTy, Value *LoadPtr,
366                                      MemIntrinsic *MI, const DataLayout &DL) {
367   // If the mem operation is a non-constant size, we can't handle it.
368   ConstantInt *SizeCst = dyn_cast<ConstantInt>(MI->getLength());
369   if (!SizeCst)
370     return -1;
371   uint64_t MemSizeInBits = SizeCst->getZExtValue() * 8;
372 
373   // If this is memset, we just need to see if the offset is valid in the size
374   // of the memset..
375   if (MI->getIntrinsicID() == Intrinsic::memset) {
376     if (DL.isNonIntegralPointerType(LoadTy->getScalarType())) {
377       auto *CI = dyn_cast<ConstantInt>(cast<MemSetInst>(MI)->getValue());
378       if (!CI || !CI->isZero())
379         return -1;
380     }
381     return analyzeLoadFromClobberingWrite(LoadTy, LoadPtr, MI->getDest(),
382                                           MemSizeInBits, DL);
383   }
384 
385   // If we have a memcpy/memmove, the only case we can handle is if this is a
386   // copy from constant memory.  In that case, we can read directly from the
387   // constant memory.
388   MemTransferInst *MTI = cast<MemTransferInst>(MI);
389 
390   Constant *Src = dyn_cast<Constant>(MTI->getSource());
391   if (!Src)
392     return -1;
393 
394   GlobalVariable *GV = dyn_cast<GlobalVariable>(GetUnderlyingObject(Src, DL));
395   if (!GV || !GV->isConstant() || !GV->hasDefinitiveInitializer())
396     return -1;
397 
398   // See if the access is within the bounds of the transfer.
399   int Offset = analyzeLoadFromClobberingWrite(LoadTy, LoadPtr, MI->getDest(),
400                                               MemSizeInBits, DL);
401   if (Offset == -1)
402     return Offset;
403 
404   // Don't coerce non-integral pointers to integers or vice versa, and the
405   // memtransfer is implicitly a raw byte code
406   if (DL.isNonIntegralPointerType(LoadTy->getScalarType()))
407     // TODO: Can allow nullptrs from constant zeros
408     return -1;
409 
410   unsigned AS = Src->getType()->getPointerAddressSpace();
411   // Otherwise, see if we can constant fold a load from the constant with the
412   // offset applied as appropriate.
413   Src =
414       ConstantExpr::getBitCast(Src, Type::getInt8PtrTy(Src->getContext(), AS));
415   Constant *OffsetCst =
416       ConstantInt::get(Type::getInt64Ty(Src->getContext()), (unsigned)Offset);
417   Src = ConstantExpr::getGetElementPtr(Type::getInt8Ty(Src->getContext()), Src,
418                                        OffsetCst);
419   Src = ConstantExpr::getBitCast(Src, PointerType::get(LoadTy, AS));
420   if (ConstantFoldLoadFromConstPtr(Src, LoadTy, DL))
421     return Offset;
422   return -1;
423 }
424 
425 template <class T, class HelperClass>
426 static T *getStoreValueForLoadHelper(T *SrcVal, unsigned Offset, Type *LoadTy,
427                                      HelperClass &Helper,
428                                      const DataLayout &DL) {
429   LLVMContext &Ctx = SrcVal->getType()->getContext();
430 
431   // If two pointers are in the same address space, they have the same size,
432   // so we don't need to do any truncation, etc. This avoids introducing
433   // ptrtoint instructions for pointers that may be non-integral.
434   if (SrcVal->getType()->isPointerTy() && LoadTy->isPointerTy() &&
435       cast<PointerType>(SrcVal->getType())->getAddressSpace() ==
436           cast<PointerType>(LoadTy)->getAddressSpace()) {
437     return SrcVal;
438   }
439 
440   uint64_t StoreSize = (DL.getTypeSizeInBits(SrcVal->getType()) + 7) / 8;
441   uint64_t LoadSize = (DL.getTypeSizeInBits(LoadTy) + 7) / 8;
442   // Compute which bits of the stored value are being used by the load.  Convert
443   // to an integer type to start with.
444   if (SrcVal->getType()->isPtrOrPtrVectorTy())
445     SrcVal = Helper.CreatePtrToInt(SrcVal, DL.getIntPtrType(SrcVal->getType()));
446   if (!SrcVal->getType()->isIntegerTy())
447     SrcVal = Helper.CreateBitCast(SrcVal, IntegerType::get(Ctx, StoreSize * 8));
448 
449   // Shift the bits to the least significant depending on endianness.
450   unsigned ShiftAmt;
451   if (DL.isLittleEndian())
452     ShiftAmt = Offset * 8;
453   else
454     ShiftAmt = (StoreSize - LoadSize - Offset) * 8;
455   if (ShiftAmt)
456     SrcVal = Helper.CreateLShr(SrcVal,
457                                ConstantInt::get(SrcVal->getType(), ShiftAmt));
458 
459   if (LoadSize != StoreSize)
460     SrcVal = Helper.CreateTruncOrBitCast(SrcVal,
461                                          IntegerType::get(Ctx, LoadSize * 8));
462   return SrcVal;
463 }
464 
465 /// This function is called when we have a memdep query of a load that ends up
466 /// being a clobbering store.  This means that the store provides bits used by
467 /// the load but the pointers don't must-alias.  Check this case to see if
468 /// there is anything more we can do before we give up.
469 Value *getStoreValueForLoad(Value *SrcVal, unsigned Offset, Type *LoadTy,
470                             Instruction *InsertPt, const DataLayout &DL) {
471 
472   IRBuilder<> Builder(InsertPt);
473   SrcVal = getStoreValueForLoadHelper(SrcVal, Offset, LoadTy, Builder, DL);
474   return coerceAvailableValueToLoadTypeHelper(SrcVal, LoadTy, Builder, DL);
475 }
476 
477 Constant *getConstantStoreValueForLoad(Constant *SrcVal, unsigned Offset,
478                                        Type *LoadTy, const DataLayout &DL) {
479   ConstantFolder F;
480   SrcVal = getStoreValueForLoadHelper(SrcVal, Offset, LoadTy, F, DL);
481   return coerceAvailableValueToLoadTypeHelper(SrcVal, LoadTy, F, DL);
482 }
483 
484 /// This function is called when we have a memdep query of a load that ends up
485 /// being a clobbering load.  This means that the load *may* provide bits used
486 /// by the load but we can't be sure because the pointers don't must-alias.
487 /// Check this case to see if there is anything more we can do before we give
488 /// up.
489 Value *getLoadValueForLoad(LoadInst *SrcVal, unsigned Offset, Type *LoadTy,
490                            Instruction *InsertPt, const DataLayout &DL) {
491   // If Offset+LoadTy exceeds the size of SrcVal, then we must be wanting to
492   // widen SrcVal out to a larger load.
493   unsigned SrcValStoreSize = DL.getTypeStoreSize(SrcVal->getType());
494   unsigned LoadSize = DL.getTypeStoreSize(LoadTy);
495   if (Offset + LoadSize > SrcValStoreSize) {
496     assert(SrcVal->isSimple() && "Cannot widen volatile/atomic load!");
497     assert(SrcVal->getType()->isIntegerTy() && "Can't widen non-integer load");
498     // If we have a load/load clobber an DepLI can be widened to cover this
499     // load, then we should widen it to the next power of 2 size big enough!
500     unsigned NewLoadSize = Offset + LoadSize;
501     if (!isPowerOf2_32(NewLoadSize))
502       NewLoadSize = NextPowerOf2(NewLoadSize);
503 
504     Value *PtrVal = SrcVal->getPointerOperand();
505     // Insert the new load after the old load.  This ensures that subsequent
506     // memdep queries will find the new load.  We can't easily remove the old
507     // load completely because it is already in the value numbering table.
508     IRBuilder<> Builder(SrcVal->getParent(), ++BasicBlock::iterator(SrcVal));
509     Type *DestTy = IntegerType::get(LoadTy->getContext(), NewLoadSize * 8);
510     Type *DestPTy =
511         PointerType::get(DestTy, PtrVal->getType()->getPointerAddressSpace());
512     Builder.SetCurrentDebugLocation(SrcVal->getDebugLoc());
513     PtrVal = Builder.CreateBitCast(PtrVal, DestPTy);
514     LoadInst *NewLoad = Builder.CreateLoad(DestTy, PtrVal);
515     NewLoad->takeName(SrcVal);
516     NewLoad->setAlignment(MaybeAlign(SrcVal->getAlignment()));
517 
518     LLVM_DEBUG(dbgs() << "GVN WIDENED LOAD: " << *SrcVal << "\n");
519     LLVM_DEBUG(dbgs() << "TO: " << *NewLoad << "\n");
520 
521     // Replace uses of the original load with the wider load.  On a big endian
522     // system, we need to shift down to get the relevant bits.
523     Value *RV = NewLoad;
524     if (DL.isBigEndian())
525       RV = Builder.CreateLShr(RV, (NewLoadSize - SrcValStoreSize) * 8);
526     RV = Builder.CreateTrunc(RV, SrcVal->getType());
527     SrcVal->replaceAllUsesWith(RV);
528 
529     SrcVal = NewLoad;
530   }
531 
532   return getStoreValueForLoad(SrcVal, Offset, LoadTy, InsertPt, DL);
533 }
534 
535 Constant *getConstantLoadValueForLoad(Constant *SrcVal, unsigned Offset,
536                                       Type *LoadTy, const DataLayout &DL) {
537   unsigned SrcValStoreSize = DL.getTypeStoreSize(SrcVal->getType());
538   unsigned LoadSize = DL.getTypeStoreSize(LoadTy);
539   if (Offset + LoadSize > SrcValStoreSize)
540     return nullptr;
541   return getConstantStoreValueForLoad(SrcVal, Offset, LoadTy, DL);
542 }
543 
544 template <class T, class HelperClass>
545 T *getMemInstValueForLoadHelper(MemIntrinsic *SrcInst, unsigned Offset,
546                                 Type *LoadTy, HelperClass &Helper,
547                                 const DataLayout &DL) {
548   LLVMContext &Ctx = LoadTy->getContext();
549   uint64_t LoadSize = DL.getTypeSizeInBits(LoadTy) / 8;
550 
551   // We know that this method is only called when the mem transfer fully
552   // provides the bits for the load.
553   if (MemSetInst *MSI = dyn_cast<MemSetInst>(SrcInst)) {
554     // memset(P, 'x', 1234) -> splat('x'), even if x is a variable, and
555     // independently of what the offset is.
556     T *Val = cast<T>(MSI->getValue());
557     if (LoadSize != 1)
558       Val =
559           Helper.CreateZExtOrBitCast(Val, IntegerType::get(Ctx, LoadSize * 8));
560     T *OneElt = Val;
561 
562     // Splat the value out to the right number of bits.
563     for (unsigned NumBytesSet = 1; NumBytesSet != LoadSize;) {
564       // If we can double the number of bytes set, do it.
565       if (NumBytesSet * 2 <= LoadSize) {
566         T *ShVal = Helper.CreateShl(
567             Val, ConstantInt::get(Val->getType(), NumBytesSet * 8));
568         Val = Helper.CreateOr(Val, ShVal);
569         NumBytesSet <<= 1;
570         continue;
571       }
572 
573       // Otherwise insert one byte at a time.
574       T *ShVal = Helper.CreateShl(Val, ConstantInt::get(Val->getType(), 1 * 8));
575       Val = Helper.CreateOr(OneElt, ShVal);
576       ++NumBytesSet;
577     }
578 
579     return coerceAvailableValueToLoadTypeHelper(Val, LoadTy, Helper, DL);
580   }
581 
582   // Otherwise, this is a memcpy/memmove from a constant global.
583   MemTransferInst *MTI = cast<MemTransferInst>(SrcInst);
584   Constant *Src = cast<Constant>(MTI->getSource());
585   unsigned AS = Src->getType()->getPointerAddressSpace();
586 
587   // Otherwise, see if we can constant fold a load from the constant with the
588   // offset applied as appropriate.
589   Src =
590       ConstantExpr::getBitCast(Src, Type::getInt8PtrTy(Src->getContext(), AS));
591   Constant *OffsetCst =
592       ConstantInt::get(Type::getInt64Ty(Src->getContext()), (unsigned)Offset);
593   Src = ConstantExpr::getGetElementPtr(Type::getInt8Ty(Src->getContext()), Src,
594                                        OffsetCst);
595   Src = ConstantExpr::getBitCast(Src, PointerType::get(LoadTy, AS));
596   return ConstantFoldLoadFromConstPtr(Src, LoadTy, DL);
597 }
598 
599 /// This function is called when we have a
600 /// memdep query of a load that ends up being a clobbering mem intrinsic.
601 Value *getMemInstValueForLoad(MemIntrinsic *SrcInst, unsigned Offset,
602                               Type *LoadTy, Instruction *InsertPt,
603                               const DataLayout &DL) {
604   IRBuilder<> Builder(InsertPt);
605   return getMemInstValueForLoadHelper<Value, IRBuilder<>>(SrcInst, Offset,
606                                                           LoadTy, Builder, DL);
607 }
608 
609 Constant *getConstantMemInstValueForLoad(MemIntrinsic *SrcInst, unsigned Offset,
610                                          Type *LoadTy, const DataLayout &DL) {
611   // The only case analyzeLoadFromClobberingMemInst cannot be converted to a
612   // constant is when it's a memset of a non-constant.
613   if (auto *MSI = dyn_cast<MemSetInst>(SrcInst))
614     if (!isa<Constant>(MSI->getValue()))
615       return nullptr;
616   ConstantFolder F;
617   return getMemInstValueForLoadHelper<Constant, ConstantFolder>(SrcInst, Offset,
618                                                                 LoadTy, F, DL);
619 }
620 } // namespace VNCoercion
621 } // namespace llvm
622