• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include "llvm/Transforms/Utils/VNCoercion.h"
2 #include "llvm/Analysis/AliasAnalysis.h"
3 #include "llvm/Analysis/ConstantFolding.h"
4 #include "llvm/Analysis/MemoryDependenceAnalysis.h"
5 #include "llvm/Analysis/ValueTracking.h"
6 #include "llvm/IR/IRBuilder.h"
7 #include "llvm/IR/IntrinsicInst.h"
8 #include "llvm/Support/Debug.h"
9 
10 #define DEBUG_TYPE "vncoerce"
11 namespace llvm {
12 namespace VNCoercion {
13 
14 /// Return true if coerceAvailableValueToLoadType will succeed.
canCoerceMustAliasedValueToLoad(Value * StoredVal,Type * LoadTy,const DataLayout & DL)15 bool canCoerceMustAliasedValueToLoad(Value *StoredVal, Type *LoadTy,
16                                      const DataLayout &DL) {
17   // If the loaded or stored value is an first class array or struct, don't try
18   // to transform them.  We need to be able to bitcast to integer.
19   if (LoadTy->isStructTy() || LoadTy->isArrayTy() ||
20       StoredVal->getType()->isStructTy() || StoredVal->getType()->isArrayTy())
21     return false;
22 
23   uint64_t StoreSize = DL.getTypeSizeInBits(StoredVal->getType());
24 
25   // The store size must be byte-aligned to support future type casts.
26   if (llvm::alignTo(StoreSize, 8) != StoreSize)
27     return false;
28 
29   // The store has to be at least as big as the load.
30   if (StoreSize < DL.getTypeSizeInBits(LoadTy))
31     return false;
32 
33   // Don't coerce non-integral pointers to integers or vice versa.
34   if (DL.isNonIntegralPointerType(StoredVal->getType()) !=
35       DL.isNonIntegralPointerType(LoadTy))
36     return false;
37 
38   return true;
39 }
40 
41 template <class T, class HelperClass>
coerceAvailableValueToLoadTypeHelper(T * StoredVal,Type * LoadedTy,HelperClass & Helper,const DataLayout & DL)42 static T *coerceAvailableValueToLoadTypeHelper(T *StoredVal, Type *LoadedTy,
43                                                HelperClass &Helper,
44                                                const DataLayout &DL) {
45   assert(canCoerceMustAliasedValueToLoad(StoredVal, LoadedTy, DL) &&
46          "precondition violation - materialization can't fail");
47   if (auto *C = dyn_cast<Constant>(StoredVal))
48     if (auto *FoldedStoredVal = ConstantFoldConstant(C, DL))
49       StoredVal = FoldedStoredVal;
50 
51   // If this is already the right type, just return it.
52   Type *StoredValTy = StoredVal->getType();
53 
54   uint64_t StoredValSize = DL.getTypeSizeInBits(StoredValTy);
55   uint64_t LoadedValSize = DL.getTypeSizeInBits(LoadedTy);
56 
57   // If the store and reload are the same size, we can always reuse it.
58   if (StoredValSize == LoadedValSize) {
59     // Pointer to Pointer -> use bitcast.
60     if (StoredValTy->isPtrOrPtrVectorTy() && LoadedTy->isPtrOrPtrVectorTy()) {
61       StoredVal = Helper.CreateBitCast(StoredVal, LoadedTy);
62     } else {
63       // Convert source pointers to integers, which can be bitcast.
64       if (StoredValTy->isPtrOrPtrVectorTy()) {
65         StoredValTy = DL.getIntPtrType(StoredValTy);
66         StoredVal = Helper.CreatePtrToInt(StoredVal, StoredValTy);
67       }
68 
69       Type *TypeToCastTo = LoadedTy;
70       if (TypeToCastTo->isPtrOrPtrVectorTy())
71         TypeToCastTo = DL.getIntPtrType(TypeToCastTo);
72 
73       if (StoredValTy != TypeToCastTo)
74         StoredVal = Helper.CreateBitCast(StoredVal, TypeToCastTo);
75 
76       // Cast to pointer if the load needs a pointer type.
77       if (LoadedTy->isPtrOrPtrVectorTy())
78         StoredVal = Helper.CreateIntToPtr(StoredVal, LoadedTy);
79     }
80 
81     if (auto *C = dyn_cast<ConstantExpr>(StoredVal))
82       if (auto *FoldedStoredVal = ConstantFoldConstant(C, DL))
83         StoredVal = FoldedStoredVal;
84 
85     return StoredVal;
86   }
87   // If the loaded value is smaller than the available value, then we can
88   // extract out a piece from it.  If the available value is too small, then we
89   // can't do anything.
90   assert(StoredValSize >= LoadedValSize &&
91          "canCoerceMustAliasedValueToLoad fail");
92 
93   // Convert source pointers to integers, which can be manipulated.
94   if (StoredValTy->isPtrOrPtrVectorTy()) {
95     StoredValTy = DL.getIntPtrType(StoredValTy);
96     StoredVal = Helper.CreatePtrToInt(StoredVal, StoredValTy);
97   }
98 
99   // Convert vectors and fp to integer, which can be manipulated.
100   if (!StoredValTy->isIntegerTy()) {
101     StoredValTy = IntegerType::get(StoredValTy->getContext(), StoredValSize);
102     StoredVal = Helper.CreateBitCast(StoredVal, StoredValTy);
103   }
104 
105   // If this is a big-endian system, we need to shift the value down to the low
106   // bits so that a truncate will work.
107   if (DL.isBigEndian()) {
108     uint64_t ShiftAmt = DL.getTypeStoreSizeInBits(StoredValTy) -
109                         DL.getTypeStoreSizeInBits(LoadedTy);
110     StoredVal = Helper.CreateLShr(
111         StoredVal, ConstantInt::get(StoredVal->getType(), ShiftAmt));
112   }
113 
114   // Truncate the integer to the right size now.
115   Type *NewIntTy = IntegerType::get(StoredValTy->getContext(), LoadedValSize);
116   StoredVal = Helper.CreateTruncOrBitCast(StoredVal, NewIntTy);
117 
118   if (LoadedTy != NewIntTy) {
119     // If the result is a pointer, inttoptr.
120     if (LoadedTy->isPtrOrPtrVectorTy())
121       StoredVal = Helper.CreateIntToPtr(StoredVal, LoadedTy);
122     else
123       // Otherwise, bitcast.
124       StoredVal = Helper.CreateBitCast(StoredVal, LoadedTy);
125   }
126 
127   if (auto *C = dyn_cast<Constant>(StoredVal))
128     if (auto *FoldedStoredVal = ConstantFoldConstant(C, DL))
129       StoredVal = FoldedStoredVal;
130 
131   return StoredVal;
132 }
133 
134 /// If we saw a store of a value to memory, and
135 /// then a load from a must-aliased pointer of a different type, try to coerce
136 /// the stored value.  LoadedTy is the type of the load we want to replace.
137 /// IRB is IRBuilder used to insert new instructions.
138 ///
139 /// If we can't do it, return null.
coerceAvailableValueToLoadType(Value * StoredVal,Type * LoadedTy,IRBuilder<> & IRB,const DataLayout & DL)140 Value *coerceAvailableValueToLoadType(Value *StoredVal, Type *LoadedTy,
141                                       IRBuilder<> &IRB, const DataLayout &DL) {
142   return coerceAvailableValueToLoadTypeHelper(StoredVal, LoadedTy, IRB, DL);
143 }
144 
145 /// This function is called when we have a memdep query of a load that ends up
146 /// being a clobbering memory write (store, memset, memcpy, memmove).  This
147 /// means that the write *may* provide bits used by the load but we can't be
148 /// sure because the pointers don't must-alias.
149 ///
150 /// Check this case to see if there is anything more we can do before we give
151 /// up.  This returns -1 if we have to give up, or a byte number in the stored
152 /// value of the piece that feeds the load.
analyzeLoadFromClobberingWrite(Type * LoadTy,Value * LoadPtr,Value * WritePtr,uint64_t WriteSizeInBits,const DataLayout & DL)153 static int analyzeLoadFromClobberingWrite(Type *LoadTy, Value *LoadPtr,
154                                           Value *WritePtr,
155                                           uint64_t WriteSizeInBits,
156                                           const DataLayout &DL) {
157   // If the loaded or stored value is a first class array or struct, don't try
158   // to transform them.  We need to be able to bitcast to integer.
159   if (LoadTy->isStructTy() || LoadTy->isArrayTy())
160     return -1;
161 
162   int64_t StoreOffset = 0, LoadOffset = 0;
163   Value *StoreBase =
164       GetPointerBaseWithConstantOffset(WritePtr, StoreOffset, DL);
165   Value *LoadBase = GetPointerBaseWithConstantOffset(LoadPtr, LoadOffset, DL);
166   if (StoreBase != LoadBase)
167     return -1;
168 
169   // If the load and store are to the exact same address, they should have been
170   // a must alias.  AA must have gotten confused.
171   // FIXME: Study to see if/when this happens.  One case is forwarding a memset
172   // to a load from the base of the memset.
173 
174   // If the load and store don't overlap at all, the store doesn't provide
175   // anything to the load.  In this case, they really don't alias at all, AA
176   // must have gotten confused.
177   uint64_t LoadSize = DL.getTypeSizeInBits(LoadTy);
178 
179   if ((WriteSizeInBits & 7) | (LoadSize & 7))
180     return -1;
181   uint64_t StoreSize = WriteSizeInBits / 8; // Convert to bytes.
182   LoadSize /= 8;
183 
184   bool isAAFailure = false;
185   if (StoreOffset < LoadOffset)
186     isAAFailure = StoreOffset + int64_t(StoreSize) <= LoadOffset;
187   else
188     isAAFailure = LoadOffset + int64_t(LoadSize) <= StoreOffset;
189 
190   if (isAAFailure)
191     return -1;
192 
193   // If the Load isn't completely contained within the stored bits, we don't
194   // have all the bits to feed it.  We could do something crazy in the future
195   // (issue a smaller load then merge the bits in) but this seems unlikely to be
196   // valuable.
197   if (StoreOffset > LoadOffset ||
198       StoreOffset + StoreSize < LoadOffset + LoadSize)
199     return -1;
200 
201   // Okay, we can do this transformation.  Return the number of bytes into the
202   // store that the load is.
203   return LoadOffset - StoreOffset;
204 }
205 
206 /// This function is called when we have a
207 /// memdep query of a load that ends up being a clobbering store.
analyzeLoadFromClobberingStore(Type * LoadTy,Value * LoadPtr,StoreInst * DepSI,const DataLayout & DL)208 int analyzeLoadFromClobberingStore(Type *LoadTy, Value *LoadPtr,
209                                    StoreInst *DepSI, const DataLayout &DL) {
210   // Cannot handle reading from store of first-class aggregate yet.
211   if (DepSI->getValueOperand()->getType()->isStructTy() ||
212       DepSI->getValueOperand()->getType()->isArrayTy())
213     return -1;
214 
215   Value *StorePtr = DepSI->getPointerOperand();
216   uint64_t StoreSize =
217       DL.getTypeSizeInBits(DepSI->getValueOperand()->getType());
218   return analyzeLoadFromClobberingWrite(LoadTy, LoadPtr, StorePtr, StoreSize,
219                                         DL);
220 }
221 
222 /// This function is called when we have a
223 /// memdep query of a load that ends up being clobbered by another load.  See if
224 /// the other load can feed into the second load.
analyzeLoadFromClobberingLoad(Type * LoadTy,Value * LoadPtr,LoadInst * DepLI,const DataLayout & DL)225 int analyzeLoadFromClobberingLoad(Type *LoadTy, Value *LoadPtr, LoadInst *DepLI,
226                                   const DataLayout &DL) {
227   // Cannot handle reading from store of first-class aggregate yet.
228   if (DepLI->getType()->isStructTy() || DepLI->getType()->isArrayTy())
229     return -1;
230 
231   Value *DepPtr = DepLI->getPointerOperand();
232   uint64_t DepSize = DL.getTypeSizeInBits(DepLI->getType());
233   int R = analyzeLoadFromClobberingWrite(LoadTy, LoadPtr, DepPtr, DepSize, DL);
234   if (R != -1)
235     return R;
236 
237   // If we have a load/load clobber an DepLI can be widened to cover this load,
238   // then we should widen it!
239   int64_t LoadOffs = 0;
240   const Value *LoadBase =
241       GetPointerBaseWithConstantOffset(LoadPtr, LoadOffs, DL);
242   unsigned LoadSize = DL.getTypeStoreSize(LoadTy);
243 
244   unsigned Size = MemoryDependenceResults::getLoadLoadClobberFullWidthSize(
245       LoadBase, LoadOffs, LoadSize, DepLI);
246   if (Size == 0)
247     return -1;
248 
249   // Check non-obvious conditions enforced by MDA which we rely on for being
250   // able to materialize this potentially available value
251   assert(DepLI->isSimple() && "Cannot widen volatile/atomic load!");
252   assert(DepLI->getType()->isIntegerTy() && "Can't widen non-integer load");
253 
254   return analyzeLoadFromClobberingWrite(LoadTy, LoadPtr, DepPtr, Size * 8, DL);
255 }
256 
analyzeLoadFromClobberingMemInst(Type * LoadTy,Value * LoadPtr,MemIntrinsic * MI,const DataLayout & DL)257 int analyzeLoadFromClobberingMemInst(Type *LoadTy, Value *LoadPtr,
258                                      MemIntrinsic *MI, const DataLayout &DL) {
259   // If the mem operation is a non-constant size, we can't handle it.
260   ConstantInt *SizeCst = dyn_cast<ConstantInt>(MI->getLength());
261   if (!SizeCst)
262     return -1;
263   uint64_t MemSizeInBits = SizeCst->getZExtValue() * 8;
264 
265   // If this is memset, we just need to see if the offset is valid in the size
266   // of the memset..
267   if (MI->getIntrinsicID() == Intrinsic::memset)
268     return analyzeLoadFromClobberingWrite(LoadTy, LoadPtr, MI->getDest(),
269                                           MemSizeInBits, DL);
270 
271   // If we have a memcpy/memmove, the only case we can handle is if this is a
272   // copy from constant memory.  In that case, we can read directly from the
273   // constant memory.
274   MemTransferInst *MTI = cast<MemTransferInst>(MI);
275 
276   Constant *Src = dyn_cast<Constant>(MTI->getSource());
277   if (!Src)
278     return -1;
279 
280   GlobalVariable *GV = dyn_cast<GlobalVariable>(GetUnderlyingObject(Src, DL));
281   if (!GV || !GV->isConstant())
282     return -1;
283 
284   // See if the access is within the bounds of the transfer.
285   int Offset = analyzeLoadFromClobberingWrite(LoadTy, LoadPtr, MI->getDest(),
286                                               MemSizeInBits, DL);
287   if (Offset == -1)
288     return Offset;
289 
290   unsigned AS = Src->getType()->getPointerAddressSpace();
291   // Otherwise, see if we can constant fold a load from the constant with the
292   // offset applied as appropriate.
293   Src =
294       ConstantExpr::getBitCast(Src, Type::getInt8PtrTy(Src->getContext(), AS));
295   Constant *OffsetCst =
296       ConstantInt::get(Type::getInt64Ty(Src->getContext()), (unsigned)Offset);
297   Src = ConstantExpr::getGetElementPtr(Type::getInt8Ty(Src->getContext()), Src,
298                                        OffsetCst);
299   Src = ConstantExpr::getBitCast(Src, PointerType::get(LoadTy, AS));
300   if (ConstantFoldLoadFromConstPtr(Src, LoadTy, DL))
301     return Offset;
302   return -1;
303 }
304 
305 template <class T, class HelperClass>
getStoreValueForLoadHelper(T * SrcVal,unsigned Offset,Type * LoadTy,HelperClass & Helper,const DataLayout & DL)306 static T *getStoreValueForLoadHelper(T *SrcVal, unsigned Offset, Type *LoadTy,
307                                      HelperClass &Helper,
308                                      const DataLayout &DL) {
309   LLVMContext &Ctx = SrcVal->getType()->getContext();
310 
311   // If two pointers are in the same address space, they have the same size,
312   // so we don't need to do any truncation, etc. This avoids introducing
313   // ptrtoint instructions for pointers that may be non-integral.
314   if (SrcVal->getType()->isPointerTy() && LoadTy->isPointerTy() &&
315       cast<PointerType>(SrcVal->getType())->getAddressSpace() ==
316           cast<PointerType>(LoadTy)->getAddressSpace()) {
317     return SrcVal;
318   }
319 
320   uint64_t StoreSize = (DL.getTypeSizeInBits(SrcVal->getType()) + 7) / 8;
321   uint64_t LoadSize = (DL.getTypeSizeInBits(LoadTy) + 7) / 8;
322   // Compute which bits of the stored value are being used by the load.  Convert
323   // to an integer type to start with.
324   if (SrcVal->getType()->isPtrOrPtrVectorTy())
325     SrcVal = Helper.CreatePtrToInt(SrcVal, DL.getIntPtrType(SrcVal->getType()));
326   if (!SrcVal->getType()->isIntegerTy())
327     SrcVal = Helper.CreateBitCast(SrcVal, IntegerType::get(Ctx, StoreSize * 8));
328 
329   // Shift the bits to the least significant depending on endianness.
330   unsigned ShiftAmt;
331   if (DL.isLittleEndian())
332     ShiftAmt = Offset * 8;
333   else
334     ShiftAmt = (StoreSize - LoadSize - Offset) * 8;
335   if (ShiftAmt)
336     SrcVal = Helper.CreateLShr(SrcVal,
337                                ConstantInt::get(SrcVal->getType(), ShiftAmt));
338 
339   if (LoadSize != StoreSize)
340     SrcVal = Helper.CreateTruncOrBitCast(SrcVal,
341                                          IntegerType::get(Ctx, LoadSize * 8));
342   return SrcVal;
343 }
344 
345 /// This function is called when we have a memdep query of a load that ends up
346 /// being a clobbering store.  This means that the store provides bits used by
347 /// the load but the pointers don't must-alias.  Check this case to see if
348 /// there is anything more we can do before we give up.
getStoreValueForLoad(Value * SrcVal,unsigned Offset,Type * LoadTy,Instruction * InsertPt,const DataLayout & DL)349 Value *getStoreValueForLoad(Value *SrcVal, unsigned Offset, Type *LoadTy,
350                             Instruction *InsertPt, const DataLayout &DL) {
351 
352   IRBuilder<> Builder(InsertPt);
353   SrcVal = getStoreValueForLoadHelper(SrcVal, Offset, LoadTy, Builder, DL);
354   return coerceAvailableValueToLoadTypeHelper(SrcVal, LoadTy, Builder, DL);
355 }
356 
getConstantStoreValueForLoad(Constant * SrcVal,unsigned Offset,Type * LoadTy,const DataLayout & DL)357 Constant *getConstantStoreValueForLoad(Constant *SrcVal, unsigned Offset,
358                                        Type *LoadTy, const DataLayout &DL) {
359   ConstantFolder F;
360   SrcVal = getStoreValueForLoadHelper(SrcVal, Offset, LoadTy, F, DL);
361   return coerceAvailableValueToLoadTypeHelper(SrcVal, LoadTy, F, DL);
362 }
363 
364 /// This function is called when we have a memdep query of a load that ends up
365 /// being a clobbering load.  This means that the load *may* provide bits used
366 /// by the load but we can't be sure because the pointers don't must-alias.
367 /// Check this case to see if there is anything more we can do before we give
368 /// up.
getLoadValueForLoad(LoadInst * SrcVal,unsigned Offset,Type * LoadTy,Instruction * InsertPt,const DataLayout & DL)369 Value *getLoadValueForLoad(LoadInst *SrcVal, unsigned Offset, Type *LoadTy,
370                            Instruction *InsertPt, const DataLayout &DL) {
371   // If Offset+LoadTy exceeds the size of SrcVal, then we must be wanting to
372   // widen SrcVal out to a larger load.
373   unsigned SrcValStoreSize = DL.getTypeStoreSize(SrcVal->getType());
374   unsigned LoadSize = DL.getTypeStoreSize(LoadTy);
375   if (Offset + LoadSize > SrcValStoreSize) {
376     assert(SrcVal->isSimple() && "Cannot widen volatile/atomic load!");
377     assert(SrcVal->getType()->isIntegerTy() && "Can't widen non-integer load");
378     // If we have a load/load clobber an DepLI can be widened to cover this
379     // load, then we should widen it to the next power of 2 size big enough!
380     unsigned NewLoadSize = Offset + LoadSize;
381     if (!isPowerOf2_32(NewLoadSize))
382       NewLoadSize = NextPowerOf2(NewLoadSize);
383 
384     Value *PtrVal = SrcVal->getPointerOperand();
385     // Insert the new load after the old load.  This ensures that subsequent
386     // memdep queries will find the new load.  We can't easily remove the old
387     // load completely because it is already in the value numbering table.
388     IRBuilder<> Builder(SrcVal->getParent(), ++BasicBlock::iterator(SrcVal));
389     Type *DestPTy = IntegerType::get(LoadTy->getContext(), NewLoadSize * 8);
390     DestPTy =
391         PointerType::get(DestPTy, PtrVal->getType()->getPointerAddressSpace());
392     Builder.SetCurrentDebugLocation(SrcVal->getDebugLoc());
393     PtrVal = Builder.CreateBitCast(PtrVal, DestPTy);
394     LoadInst *NewLoad = Builder.CreateLoad(PtrVal);
395     NewLoad->takeName(SrcVal);
396     NewLoad->setAlignment(SrcVal->getAlignment());
397 
398     LLVM_DEBUG(dbgs() << "GVN WIDENED LOAD: " << *SrcVal << "\n");
399     LLVM_DEBUG(dbgs() << "TO: " << *NewLoad << "\n");
400 
401     // Replace uses of the original load with the wider load.  On a big endian
402     // system, we need to shift down to get the relevant bits.
403     Value *RV = NewLoad;
404     if (DL.isBigEndian())
405       RV = Builder.CreateLShr(RV, (NewLoadSize - SrcValStoreSize) * 8);
406     RV = Builder.CreateTrunc(RV, SrcVal->getType());
407     SrcVal->replaceAllUsesWith(RV);
408 
409     SrcVal = NewLoad;
410   }
411 
412   return getStoreValueForLoad(SrcVal, Offset, LoadTy, InsertPt, DL);
413 }
414 
getConstantLoadValueForLoad(Constant * SrcVal,unsigned Offset,Type * LoadTy,const DataLayout & DL)415 Constant *getConstantLoadValueForLoad(Constant *SrcVal, unsigned Offset,
416                                       Type *LoadTy, const DataLayout &DL) {
417   unsigned SrcValStoreSize = DL.getTypeStoreSize(SrcVal->getType());
418   unsigned LoadSize = DL.getTypeStoreSize(LoadTy);
419   if (Offset + LoadSize > SrcValStoreSize)
420     return nullptr;
421   return getConstantStoreValueForLoad(SrcVal, Offset, LoadTy, DL);
422 }
423 
424 template <class T, class HelperClass>
getMemInstValueForLoadHelper(MemIntrinsic * SrcInst,unsigned Offset,Type * LoadTy,HelperClass & Helper,const DataLayout & DL)425 T *getMemInstValueForLoadHelper(MemIntrinsic *SrcInst, unsigned Offset,
426                                 Type *LoadTy, HelperClass &Helper,
427                                 const DataLayout &DL) {
428   LLVMContext &Ctx = LoadTy->getContext();
429   uint64_t LoadSize = DL.getTypeSizeInBits(LoadTy) / 8;
430 
431   // We know that this method is only called when the mem transfer fully
432   // provides the bits for the load.
433   if (MemSetInst *MSI = dyn_cast<MemSetInst>(SrcInst)) {
434     // memset(P, 'x', 1234) -> splat('x'), even if x is a variable, and
435     // independently of what the offset is.
436     T *Val = cast<T>(MSI->getValue());
437     if (LoadSize != 1)
438       Val =
439           Helper.CreateZExtOrBitCast(Val, IntegerType::get(Ctx, LoadSize * 8));
440     T *OneElt = Val;
441 
442     // Splat the value out to the right number of bits.
443     for (unsigned NumBytesSet = 1; NumBytesSet != LoadSize;) {
444       // If we can double the number of bytes set, do it.
445       if (NumBytesSet * 2 <= LoadSize) {
446         T *ShVal = Helper.CreateShl(
447             Val, ConstantInt::get(Val->getType(), NumBytesSet * 8));
448         Val = Helper.CreateOr(Val, ShVal);
449         NumBytesSet <<= 1;
450         continue;
451       }
452 
453       // Otherwise insert one byte at a time.
454       T *ShVal = Helper.CreateShl(Val, ConstantInt::get(Val->getType(), 1 * 8));
455       Val = Helper.CreateOr(OneElt, ShVal);
456       ++NumBytesSet;
457     }
458 
459     return coerceAvailableValueToLoadTypeHelper(Val, LoadTy, Helper, DL);
460   }
461 
462   // Otherwise, this is a memcpy/memmove from a constant global.
463   MemTransferInst *MTI = cast<MemTransferInst>(SrcInst);
464   Constant *Src = cast<Constant>(MTI->getSource());
465   unsigned AS = Src->getType()->getPointerAddressSpace();
466 
467   // Otherwise, see if we can constant fold a load from the constant with the
468   // offset applied as appropriate.
469   Src =
470       ConstantExpr::getBitCast(Src, Type::getInt8PtrTy(Src->getContext(), AS));
471   Constant *OffsetCst =
472       ConstantInt::get(Type::getInt64Ty(Src->getContext()), (unsigned)Offset);
473   Src = ConstantExpr::getGetElementPtr(Type::getInt8Ty(Src->getContext()), Src,
474                                        OffsetCst);
475   Src = ConstantExpr::getBitCast(Src, PointerType::get(LoadTy, AS));
476   return ConstantFoldLoadFromConstPtr(Src, LoadTy, DL);
477 }
478 
479 /// This function is called when we have a
480 /// memdep query of a load that ends up being a clobbering mem intrinsic.
getMemInstValueForLoad(MemIntrinsic * SrcInst,unsigned Offset,Type * LoadTy,Instruction * InsertPt,const DataLayout & DL)481 Value *getMemInstValueForLoad(MemIntrinsic *SrcInst, unsigned Offset,
482                               Type *LoadTy, Instruction *InsertPt,
483                               const DataLayout &DL) {
484   IRBuilder<> Builder(InsertPt);
485   return getMemInstValueForLoadHelper<Value, IRBuilder<>>(SrcInst, Offset,
486                                                           LoadTy, Builder, DL);
487 }
488 
getConstantMemInstValueForLoad(MemIntrinsic * SrcInst,unsigned Offset,Type * LoadTy,const DataLayout & DL)489 Constant *getConstantMemInstValueForLoad(MemIntrinsic *SrcInst, unsigned Offset,
490                                          Type *LoadTy, const DataLayout &DL) {
491   // The only case analyzeLoadFromClobberingMemInst cannot be converted to a
492   // constant is when it's a memset of a non-constant.
493   if (auto *MSI = dyn_cast<MemSetInst>(SrcInst))
494     if (!isa<Constant>(MSI->getValue()))
495       return nullptr;
496   ConstantFolder F;
497   return getMemInstValueForLoadHelper<Constant, ConstantFolder>(SrcInst, Offset,
498                                                                 LoadTy, F, DL);
499 }
500 } // namespace VNCoercion
501 } // namespace llvm
502