10 #define DEBUG_TYPE "vncoerce"
12 namespace VNCoercion {
35 template <
class T,
class HelperClass>
40 "precondition violation - materialization can't fail");
41 if (
auto *
C = dyn_cast<Constant>(StoredVal))
43 StoredVal = FoldedStoredVal;
46 Type *StoredValTy = StoredVal->getType();
52 if (StoredValSize == LoadedValSize) {
55 StoredVal = Helper.CreateBitCast(StoredVal, LoadedTy);
60 StoredVal = Helper.CreatePtrToInt(StoredVal, StoredValTy);
63 Type *TypeToCastTo = LoadedTy;
67 if (StoredValTy != TypeToCastTo)
68 StoredVal = Helper.CreateBitCast(StoredVal, TypeToCastTo);
72 StoredVal = Helper.CreateIntToPtr(StoredVal, LoadedTy);
75 if (
auto *
C = dyn_cast<ConstantExpr>(StoredVal))
77 StoredVal = FoldedStoredVal;
84 assert(StoredValSize >= LoadedValSize &&
85 "canCoerceMustAliasedValueToLoad fail");
90 StoredVal = Helper.CreatePtrToInt(StoredVal, StoredValTy);
96 StoredVal = Helper.CreateBitCast(StoredVal, StoredValTy);
104 StoredVal = Helper.CreateLShr(
110 StoredVal = Helper.CreateTruncOrBitCast(StoredVal, NewIntTy);
112 if (LoadedTy != NewIntTy) {
115 StoredVal = Helper.CreateIntToPtr(StoredVal, LoadedTy);
118 StoredVal = Helper.CreateBitCast(StoredVal, LoadedTy);
121 if (
auto *
C = dyn_cast<Constant>(StoredVal))
123 StoredVal = FoldedStoredVal;
149 uint64_t WriteSizeInBits,
156 int64_t StoreOffset = 0, LoadOffset = 0;
160 if (StoreBase != LoadBase)
173 if ((WriteSizeInBits & 7) | (LoadSize & 7))
175 uint64_t StoreSize = WriteSizeInBits / 8;
178 bool isAAFailure =
false;
179 if (StoreOffset < LoadOffset)
180 isAAFailure = StoreOffset + int64_t(StoreSize) <= LoadOffset;
182 isAAFailure = LoadOffset + int64_t(LoadSize) <= StoreOffset;
191 if (StoreOffset > LoadOffset ||
192 StoreOffset + StoreSize < LoadOffset + LoadSize)
197 return LoadOffset - StoreOffset;
233 int64_t LoadOffs = 0;
234 const Value *LoadBase =
239 LoadBase, LoadOffs, LoadSize, DepLI);
245 assert(DepLI->
isSimple() &&
"Cannot widen volatile/atomic load!");
284 unsigned AS = Src->getType()->getPointerAddressSpace();
299 template <
class T,
class HelperClass>
303 LLVMContext &Ctx = SrcVal->getType()->getContext();
308 if (SrcVal->getType()->isPointerTy() && LoadTy->
isPointerTy() &&
309 cast<PointerType>(SrcVal->getType())->getAddressSpace() ==
310 cast<PointerType>(LoadTy)->getAddressSpace()) {
318 if (SrcVal->getType()->isPtrOrPtrVectorTy())
319 SrcVal = Helper.CreatePtrToInt(SrcVal, DL.
getIntPtrType(SrcVal->getType()));
320 if (!SrcVal->getType()->isIntegerTy())
321 SrcVal = Helper.CreateBitCast(SrcVal,
IntegerType::get(Ctx, StoreSize * 8));
326 ShiftAmt = Offset * 8;
328 ShiftAmt = (StoreSize - LoadSize -
Offset) * 8;
330 SrcVal = Helper.CreateLShr(SrcVal,
333 if (LoadSize != StoreSize)
334 SrcVal = Helper.CreateTruncOrBitCast(SrcVal,
369 if (Offset + LoadSize > SrcValStoreSize) {
370 assert(SrcVal->
isSimple() &&
"Cannot widen volatile/atomic load!");
374 unsigned NewLoadSize = Offset + LoadSize;
386 Builder.SetCurrentDebugLocation(SrcVal->
getDebugLoc());
387 PtrVal = Builder.CreateBitCast(PtrVal, DestPTy);
388 LoadInst *NewLoad = Builder.CreateLoad(PtrVal);
392 DEBUG(
dbgs() <<
"GVN WIDENED LOAD: " << *SrcVal <<
"\n");
393 DEBUG(
dbgs() <<
"TO: " << *NewLoad <<
"\n");
399 RV = Builder.CreateLShr(RV, (NewLoadSize - SrcValStoreSize) * 8);
400 RV = Builder.CreateTrunc(RV, SrcVal->
getType());
413 if (Offset + LoadSize > SrcValStoreSize)
418 template <
class T,
class HelperClass>
420 Type *LoadTy, HelperClass &Helper,
427 if (
MemSetInst *MSI = dyn_cast<MemSetInst>(SrcInst)) {
430 T *Val = cast<T>(MSI->getValue());
437 for (
unsigned NumBytesSet = 1; NumBytesSet != LoadSize;) {
439 if (NumBytesSet * 2 <= LoadSize) {
440 T *ShVal = Helper.CreateShl(
442 Val = Helper.CreateOr(Val, ShVal);
449 Val = Helper.CreateOr(OneElt, ShVal);
479 return getMemInstValueForLoadHelper<Value, IRBuilder<>>(SrcInst,
Offset,
480 LoadTy, Builder, DL);
487 if (
auto *MSI = dyn_cast<MemSetInst>(SrcInst))
488 if (!isa<Constant>(MSI->getValue()))
491 return getMemInstValueForLoadHelper<Constant, ConstantFolder>(SrcInst,
Offset,
Value * getValueOperand()
A parsed version of the target data layout string in and methods for querying it. ...
int analyzeLoadFromClobberingLoad(Type *LoadTy, Value *LoadPtr, LoadInst *DepLI, const DataLayout &DL)
This function determines whether a value for the pointer LoadPtr can be extracted from the load at De...
bool isNonIntegralPointerType(PointerType *PT) const
Intrinsic::ID getIntrinsicID() const
Return the intrinsic ID of this intrinsic.
static PointerType * get(Type *ElementType, unsigned AddressSpace)
This constructs a pointer to an object of the specified type in a numbered address space...
static Constant * getGetElementPtr(Type *Ty, Constant *C, ArrayRef< Constant * > IdxList, bool InBounds=false, Optional< unsigned > InRangeIndex=None, Type *OnlyIfReducedTy=nullptr)
Getelementptr form.
bool isPtrOrPtrVectorTy() const
Return true if this is a pointer type or a vector of pointer types.
This class wraps the llvm.memset intrinsic.
An instruction for reading from memory.
static IntegerType * getInt64Ty(LLVMContext &C)
Constant * getConstantMemInstValueForLoad(MemIntrinsic *SrcInst, unsigned Offset, Type *LoadTy, const DataLayout &DL)
Value * getMemInstValueForLoad(MemIntrinsic *SrcInst, unsigned Offset, Type *LoadTy, Instruction *InsertPt, const DataLayout &DL)
If analyzeLoadFromClobberingMemInst returned an offset, this function can be used to actually perform...
Value * coerceAvailableValueToLoadType(Value *StoredVal, Type *LoadedTy, IRBuilder<> &IRB, const DataLayout &DL)
If we saw a store of a value to memory, and then a load from a must-aliased pointer of a different ty...
static unsigned getLoadLoadClobberFullWidthSize(const Value *MemLocBase, int64_t MemLocOffs, unsigned MemLocSize, const LoadInst *LI)
Looks at a memory location for a load (specified by MemLocBase, Offs, and Size) and compares it again...
int analyzeLoadFromClobberingMemInst(Type *LoadTy, Value *LoadPtr, MemIntrinsic *DepMI, const DataLayout &DL)
This function determines whether a value for the pointer LoadPtr can be extracted from the memory int...
Constant * getConstantStoreValueForLoad(Constant *SrcVal, unsigned Offset, Type *LoadTy, const DataLayout &DL)
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
uint64_t getZExtValue() const
Return the constant as a 64-bit unsigned integer value after it has been zero extended as appropriate...
Constant * ConstantFoldConstant(const Constant *C, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr)
ConstantFoldConstant - Attempt to fold the constant using the specified DataLayout.
LLVMContext & getContext() const
Return the LLVMContext in which this type was uniqued.
Value * GetPointerBaseWithConstantOffset(Value *Ptr, int64_t &Offset, const DataLayout &DL)
Analyze the specified pointer to see if it can be expressed as a base pointer plus a constant offset...
bool canCoerceMustAliasedValueToLoad(Value *StoredVal, Type *LoadTy, const DataLayout &DL)
Return true if CoerceAvailableValueToLoadType would succeed if it was called.
static T * getStoreValueForLoadHelper(T *SrcVal, unsigned Offset, Type *LoadTy, HelperClass &Helper, const DataLayout &DL)
ConstantFolder - Create constants with minimum, target independent, folding.
bool isLittleEndian() const
Layout endianness...
An instruction for storing to memory.
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
bool isArrayTy() const
True if this is an instance of ArrayType.
void takeName(Value *V)
Transfer the name from V to this value.
int analyzeLoadFromClobberingStore(Type *LoadTy, Value *LoadPtr, StoreInst *DepSI, const DataLayout &DL)
This function determines whether a value for the pointer LoadPtr can be extracted from the store at D...
static Constant * getBitCast(Constant *C, Type *Ty, bool OnlyIfReduced=false)
constexpr bool isPowerOf2_32(uint32_t Value)
Return true if the argument is a power of two > 0.
The instances of the Type class are immutable: once they are created, they are never changed...
This is an important class for using LLVM in a threaded context.
uint64_t getTypeStoreSizeInBits(Type *Ty) const
Returns the maximum number of bits that may be overwritten by storing the specified type; always a mu...
This is an important base class in LLVM.
Constant * getConstantLoadValueForLoad(Constant *SrcVal, unsigned Offset, Type *LoadTy, const DataLayout &DL)
const DebugLoc & getDebugLoc() const
Return the debug location for this node as a DebugLoc.
static int analyzeLoadFromClobberingWrite(Type *LoadTy, Value *LoadPtr, Value *WritePtr, uint64_t WriteSizeInBits, const DataLayout &DL)
This function is called when we have a memdep query of a load that ends up being a clobbering memory ...
Value * getPointerOperand()
Value * getLoadValueForLoad(LoadInst *SrcVal, unsigned Offset, Type *LoadTy, Instruction *InsertPt, const DataLayout &DL)
If analyzeLoadFromClobberingLoad returned an offset, this function can be used to actually perform th...
void setAlignment(unsigned Align)
Constant * ConstantFoldLoadFromConstPtr(Constant *C, Type *Ty, const DataLayout &DL)
ConstantFoldLoadFromConstPtr - Return the value that a load from C would produce if it is constant an...
bool isPointerTy() const
True if this is an instance of PointerType.
uint64_t NextPowerOf2(uint64_t A)
Returns the next power of two (in 64-bits) that is strictly greater than A.
static PointerType * getInt8PtrTy(LLVMContext &C, unsigned AS=0)
Value * GetUnderlyingObject(Value *V, const DataLayout &DL, unsigned MaxLookup=6)
This method strips off any GEP address adjustments and pointer casts from the specified value...
IntegerType * getIntPtrType(LLVMContext &C, unsigned AddressSpace=0) const
Returns an integer type with size at least as big as that of a pointer in the given address space...
static IntegerType * get(LLVMContext &C, unsigned NumBits)
This static method is the primary way of constructing an IntegerType.
This is the common base class for memset/memcpy/memmove.
This is the shared class of boolean and integer constants.
Value * getDest() const
This is just like getRawDest, but it strips off any cast instructions that feed it, giving the original input.
Type * getType() const
All values are typed, get the type of this value.
Value * getLength() const
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
If Ty is a vector type, return a Constant with a splat of the given value.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
static T * coerceAvailableValueToLoadTypeHelper(T *StoredVal, Type *LoadedTy, HelperClass &Helper, const DataLayout &DL)
bool isConstant() const
If the value is a global constant, its value is immutable throughout the runtime execution of the pro...
bool isIntegerTy() const
True if this is an instance of IntegerType.
InstListType::iterator iterator
Instruction iterators...
constexpr char Size[]
Key for Kernel::Arg::Metadata::mSize.
bool isStructTy() const
True if this is an instance of StructType.
Value * getSource() const
This is just like getRawSource, but it strips off any cast instructions that feed it...
This class wraps the llvm.memcpy/memmove intrinsics.
Value * getStoreValueForLoad(Value *SrcVal, unsigned Offset, Type *LoadTy, Instruction *InsertPt, const DataLayout &DL)
If analyzeLoadFromClobberingStore returned an offset, this function can be used to actually perform t...
unsigned getAlignment() const
Return the alignment of the access that is being performed.
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
uint64_t getTypeStoreSize(Type *Ty) const
Returns the maximum number of bytes that may be overwritten by storing the specified type...
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM Value Representation.
uint64_t getTypeSizeInBits(Type *Ty) const
Size examples:
T * getMemInstValueForLoadHelper(MemIntrinsic *SrcInst, unsigned Offset, Type *LoadTy, HelperClass &Helper, const DataLayout &DL)
unsigned getPointerAddressSpace() const
Get the address space of this pointer or pointer vector type.
Value * getPointerOperand()
static IntegerType * getInt8Ty(LLVMContext &C)
const BasicBlock * getParent() const