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