10 #define DEBUG_TYPE "vncoerce" 12 namespace VNCoercion {
18 if (StoredTy == LoadTy)
43 if (
auto *CI = dyn_cast<Constant>(StoredVal))
44 return CI->isNullValue();
51 template <
class T,
class HelperClass>
56 "precondition violation - materialization can't fail");
57 if (
auto *
C = dyn_cast<Constant>(StoredVal))
59 StoredVal = FoldedStoredVal;
62 Type *StoredValTy = StoredVal->getType();
68 if (StoredValSize == LoadedValSize) {
71 StoredVal = Helper.CreateBitCast(StoredVal, LoadedTy);
76 StoredVal = Helper.CreatePtrToInt(StoredVal, StoredValTy);
79 Type *TypeToCastTo = LoadedTy;
83 if (StoredValTy != TypeToCastTo)
84 StoredVal = Helper.CreateBitCast(StoredVal, TypeToCastTo);
88 StoredVal = Helper.CreateIntToPtr(StoredVal, LoadedTy);
91 if (
auto *
C = dyn_cast<ConstantExpr>(StoredVal))
93 StoredVal = FoldedStoredVal;
100 assert(StoredValSize >= LoadedValSize &&
101 "canCoerceMustAliasedValueToLoad fail");
106 StoredVal = Helper.CreatePtrToInt(StoredVal, StoredValTy);
112 StoredVal = Helper.CreateBitCast(StoredVal, StoredValTy);
120 StoredVal = Helper.CreateLShr(
126 StoredVal = Helper.CreateTruncOrBitCast(StoredVal, NewIntTy);
128 if (LoadedTy != NewIntTy) {
131 StoredVal = Helper.CreateIntToPtr(StoredVal, LoadedTy);
134 StoredVal = Helper.CreateBitCast(StoredVal, LoadedTy);
137 if (
auto *
C = dyn_cast<Constant>(StoredVal))
139 StoredVal = FoldedStoredVal;
165 uint64_t WriteSizeInBits,
172 int64_t StoreOffset = 0, LoadOffset = 0;
176 if (StoreBase != LoadBase)
189 if ((WriteSizeInBits & 7) | (LoadSize & 7))
191 uint64_t StoreSize = WriteSizeInBits / 8;
194 bool isAAFailure =
false;
195 if (StoreOffset < LoadOffset)
196 isAAFailure = StoreOffset + int64_t(StoreSize) <= LoadOffset;
198 isAAFailure = LoadOffset + int64_t(LoadSize) <= StoreOffset;
207 if (StoreOffset > LoadOffset ||
208 StoreOffset + StoreSize < LoadOffset + LoadSize)
213 return LoadOffset - StoreOffset;
223 if (StoredVal->getType()->isStructTy() ||
224 StoredVal->getType()->isArrayTy())
232 if (!CI || !CI->isNullValue())
265 int64_t LoadOffs = 0;
266 const Value *LoadBase =
271 LoadBase, LoadOffs, LoadSize, DepLI);
277 assert(DepLI->
isSimple() &&
"Cannot widen volatile/atomic load!");
296 if (!CI || !CI->isZero())
328 unsigned AS = Src->getType()->getPointerAddressSpace();
343 template <
class T,
class HelperClass>
347 LLVMContext &Ctx = SrcVal->getType()->getContext();
352 if (SrcVal->getType()->isPointerTy() && LoadTy->
isPointerTy() &&
353 cast<PointerType>(SrcVal->getType())->getAddressSpace() ==
354 cast<PointerType>(LoadTy)->getAddressSpace()) {
362 if (SrcVal->getType()->isPtrOrPtrVectorTy())
363 SrcVal = Helper.CreatePtrToInt(SrcVal, DL.
getIntPtrType(SrcVal->getType()));
364 if (!SrcVal->getType()->isIntegerTy())
365 SrcVal = Helper.CreateBitCast(SrcVal,
IntegerType::get(Ctx, StoreSize * 8));
370 ShiftAmt = Offset * 8;
372 ShiftAmt = (StoreSize - LoadSize -
Offset) * 8;
374 SrcVal = Helper.CreateLShr(SrcVal,
377 if (LoadSize != StoreSize)
378 SrcVal = Helper.CreateTruncOrBitCast(SrcVal,
413 if (Offset + LoadSize > SrcValStoreSize) {
414 assert(SrcVal->
isSimple() &&
"Cannot widen volatile/atomic load!");
418 unsigned NewLoadSize = Offset + LoadSize;
430 Builder.SetCurrentDebugLocation(SrcVal->
getDebugLoc());
431 PtrVal = Builder.CreateBitCast(PtrVal, DestPTy);
432 LoadInst *NewLoad = Builder.CreateLoad(DestTy, PtrVal);
443 RV = Builder.CreateLShr(RV, (NewLoadSize - SrcValStoreSize) * 8);
444 RV = Builder.CreateTrunc(RV, SrcVal->
getType());
457 if (Offset + LoadSize > SrcValStoreSize)
462 template <
class T,
class HelperClass>
464 Type *LoadTy, HelperClass &Helper,
471 if (
MemSetInst *MSI = dyn_cast<MemSetInst>(SrcInst)) {
474 T *Val = cast<T>(MSI->getValue());
481 for (
unsigned NumBytesSet = 1; NumBytesSet != LoadSize;) {
483 if (NumBytesSet * 2 <= LoadSize) {
484 T *ShVal = Helper.CreateShl(
486 Val = Helper.CreateOr(Val, ShVal);
493 Val = Helper.CreateOr(OneElt, ShVal);
523 return getMemInstValueForLoadHelper<Value, IRBuilder<>>(SrcInst,
Offset,
524 LoadTy, Builder, DL);
531 if (
auto *MSI = dyn_cast<MemSetInst>(SrcInst))
532 if (!isa<Constant>(MSI->getValue()))
535 return getMemInstValueForLoadHelper<Constant, ConstantFolder>(SrcInst,
Offset,
Value * getValueOperand()
A parsed version of the target data layout string in and methods for querying it. ...
bool hasDefinitiveInitializer() const
hasDefinitiveInitializer - Whether the global variable has an initializer, and any other instances of...
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...
Value * GetPointerBaseWithConstantOffset(Value *Ptr, int64_t &Offset, const DataLayout &DL, bool AllowNonInbounds=true)
Analyze the specified pointer to see if it can be expressed as a base pointer plus a constant offset...
This class represents lattice values for constants.
static Constant * getGetElementPtr(Type *Ty, Constant *C, ArrayRef< Constant *> IdxList, bool InBounds=false, Optional< unsigned > InRangeIndex=None, Type *OnlyIfReducedTy=nullptr)
Getelementptr form.
static PointerType * get(Type *ElementType, unsigned AddressSpace)
This constructs a pointer to an object of the specified type in a numbered address space...
This class wraps the llvm.memset intrinsic.
unsigned getPointerAddressSpace() const
Get the address space of this pointer or pointer vector type.
An instruction for reading from memory.
static IntegerType * getInt64Ty(LLVMContext &C)
TypeSize getTypeSizeInBits(Type *Ty) const
Size examples:
Value * getLength() const
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...
TypeSize getTypeStoreSizeInBits(Type *Ty) const
Returns the maximum number of bits that may be overwritten by storing the specified type; always a mu...
LLVMContext & getContext() const
Return the LLVMContext in which this type was uniqued.
Value * getDest() const
This is just like getRawDest, but it strips off any cast instructions (including addrspacecast) that ...
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...
bool isConstant() const
If the value is a global constant, its value is immutable throughout the runtime execution of the pro...
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)
bool isIntegerTy() const
True if this is an instance of IntegerType.
Constant * ConstantFoldConstant(const Constant *C, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr)
ConstantFoldConstant - Attempt to fold the constant using the specified DataLayout.
Type * getType() const
All values are typed, get the type of this value.
TypeSize getTypeStoreSize(Type *Ty) const
Returns the maximum number of bytes that may be overwritten by storing the specified type...
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.
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...
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
static Constant * getBitCast(Constant *C, Type *Ty, bool OnlyIfReduced=false)
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...
uint64_t getZExtValue() const
Return the constant as a 64-bit unsigned integer value after it has been zero extended as appropriate...
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.
This is an important base class in LLVM.
Constant * getConstantLoadValueForLoad(Constant *SrcVal, unsigned Offset, Type *LoadTy, const DataLayout &DL)
bool isPointerTy() const
True if this is an instance of PointerType.
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...
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...
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...
Intrinsic::ID getIntrinsicID() const
Return the intrinsic ID of this intrinsic.
bool isPtrOrPtrVectorTy() const
Return true if this is a pointer type or a vector of pointer types.
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.
This struct is a compact representation of a valid (power of two) or undefined (0) alignment...
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)
InstListType::iterator iterator
Instruction iterators...
bool isNonIntegralPointerType(PointerType *PT) const
This class wraps the llvm.memcpy/memmove intrinsics.
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
const DebugLoc & getDebugLoc() const
Return the debug location for this node as a DebugLoc.
unsigned getAlignment() const
Return the alignment of the access that is being performed.
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...
void setAlignment(MaybeAlign Alignment)
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM Value Representation.
T * getMemInstValueForLoadHelper(MemIntrinsic *SrcInst, unsigned Offset, Type *LoadTy, HelperClass &Helper, const DataLayout &DL)
Value * getSource() const
This is just like getRawSource, but it strips off any cast instructions that feed it...
Value * getPointerOperand()
static IntegerType * getInt8Ty(LLVMContext &C)
bool isStructTy() const
True if this is an instance of StructType.
bool isArrayTy() const
True if this is an instance of ArrayType.
const BasicBlock * getParent() const