#include "llvm/Transforms/Utils/VNCoercion.h"
#include "llvm/Analysis/ConstantFolding.h"
#include "llvm/Analysis/ValueTracking.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/IntrinsicInst.h"
#include "llvm/Support/Debug.h"
#define DEBUG_TYPE "vncoerce"
namespace llvm {
namespace VNCoercion {
static bool isFirstClassAggregateOrScalableType(Type *Ty) {
return Ty->isStructTy() || Ty->isArrayTy() || isa<ScalableVectorType>(Ty);
}
bool canCoerceMustAliasedValueToLoad(Value *StoredVal, Type *LoadTy,
const DataLayout &DL) {
Type *StoredTy = StoredVal->getType();
if (StoredTy == LoadTy)
return true;
if (isFirstClassAggregateOrScalableType(LoadTy) ||
isFirstClassAggregateOrScalableType(StoredTy))
return false;
uint64_t StoreSize = DL.getTypeSizeInBits(StoredTy).getFixedSize();
if (llvm::alignTo(StoreSize, 8) != StoreSize)
return false;
if (StoreSize < DL.getTypeSizeInBits(LoadTy).getFixedSize())
return false;
bool StoredNI = DL.isNonIntegralPointerType(StoredTy->getScalarType());
bool LoadNI = DL.isNonIntegralPointerType(LoadTy->getScalarType());
if (StoredNI != LoadNI) {
if (auto *CI = dyn_cast<Constant>(StoredVal))
return CI->isNullValue();
return false;
} else if (StoredNI && LoadNI &&
StoredTy->getPointerAddressSpace() !=
LoadTy->getPointerAddressSpace()) {
return false;
}
if (StoredNI && StoreSize != DL.getTypeSizeInBits(LoadTy).getFixedSize())
return false;
return true;
}
Value *coerceAvailableValueToLoadType(Value *StoredVal, Type *LoadedTy,
IRBuilderBase &Helper,
const DataLayout &DL) {
assert(canCoerceMustAliasedValueToLoad(StoredVal, LoadedTy, DL) &&
"precondition violation - materialization can't fail");
if (auto *C = dyn_cast<Constant>(StoredVal))
StoredVal = ConstantFoldConstant(C, DL);
Type *StoredValTy = StoredVal->getType();
uint64_t StoredValSize = DL.getTypeSizeInBits(StoredValTy).getFixedSize();
uint64_t LoadedValSize = DL.getTypeSizeInBits(LoadedTy).getFixedSize();
if (StoredValSize == LoadedValSize) {
if (StoredValTy->isPtrOrPtrVectorTy() && LoadedTy->isPtrOrPtrVectorTy()) {
StoredVal = Helper.CreateBitCast(StoredVal, LoadedTy);
} else {
if (StoredValTy->isPtrOrPtrVectorTy()) {
StoredValTy = DL.getIntPtrType(StoredValTy);
StoredVal = Helper.CreatePtrToInt(StoredVal, StoredValTy);
}
Type *TypeToCastTo = LoadedTy;
if (TypeToCastTo->isPtrOrPtrVectorTy())
TypeToCastTo = DL.getIntPtrType(TypeToCastTo);
if (StoredValTy != TypeToCastTo)
StoredVal = Helper.CreateBitCast(StoredVal, TypeToCastTo);
if (LoadedTy->isPtrOrPtrVectorTy())
StoredVal = Helper.CreateIntToPtr(StoredVal, LoadedTy);
}
if (auto *C = dyn_cast<ConstantExpr>(StoredVal))
StoredVal = ConstantFoldConstant(C, DL);
return StoredVal;
}
assert(StoredValSize >= LoadedValSize &&
"canCoerceMustAliasedValueToLoad fail");
if (StoredValTy->isPtrOrPtrVectorTy()) {
StoredValTy = DL.getIntPtrType(StoredValTy);
StoredVal = Helper.CreatePtrToInt(StoredVal, StoredValTy);
}
if (!StoredValTy->isIntegerTy()) {
StoredValTy = IntegerType::get(StoredValTy->getContext(), StoredValSize);
StoredVal = Helper.CreateBitCast(StoredVal, StoredValTy);
}
if (DL.isBigEndian()) {
uint64_t ShiftAmt = DL.getTypeStoreSizeInBits(StoredValTy).getFixedSize() -
DL.getTypeStoreSizeInBits(LoadedTy).getFixedSize();
StoredVal = Helper.CreateLShr(
StoredVal, ConstantInt::get(StoredVal->getType(), ShiftAmt));
}
Type *NewIntTy = IntegerType::get(StoredValTy->getContext(), LoadedValSize);
StoredVal = Helper.CreateTruncOrBitCast(StoredVal, NewIntTy);
if (LoadedTy != NewIntTy) {
if (LoadedTy->isPtrOrPtrVectorTy())
StoredVal = Helper.CreateIntToPtr(StoredVal, LoadedTy);
else
StoredVal = Helper.CreateBitCast(StoredVal, LoadedTy);
}
if (auto *C = dyn_cast<Constant>(StoredVal))
StoredVal = ConstantFoldConstant(C, DL);
return StoredVal;
}
static int analyzeLoadFromClobberingWrite(Type *LoadTy, Value *LoadPtr,
Value *WritePtr,
uint64_t WriteSizeInBits,
const DataLayout &DL) {
if (isFirstClassAggregateOrScalableType(LoadTy))
return -1;
int64_t StoreOffset = 0, LoadOffset = 0;
Value *StoreBase =
GetPointerBaseWithConstantOffset(WritePtr, StoreOffset, DL);
Value *LoadBase = GetPointerBaseWithConstantOffset(LoadPtr, LoadOffset, DL);
if (StoreBase != LoadBase)
return -1;
uint64_t LoadSize = DL.getTypeSizeInBits(LoadTy).getFixedSize();
if ((WriteSizeInBits & 7) | (LoadSize & 7))
return -1;
uint64_t StoreSize = WriteSizeInBits / 8; LoadSize /= 8;
if (StoreOffset > LoadOffset ||
StoreOffset + int64_t(StoreSize) < LoadOffset + int64_t(LoadSize))
return -1;
return LoadOffset - StoreOffset;
}
int analyzeLoadFromClobberingStore(Type *LoadTy, Value *LoadPtr,
StoreInst *DepSI, const DataLayout &DL) {
auto *StoredVal = DepSI->getValueOperand();
if (isFirstClassAggregateOrScalableType(StoredVal->getType()))
return -1;
if (!canCoerceMustAliasedValueToLoad(StoredVal, LoadTy, DL))
return -1;
Value *StorePtr = DepSI->getPointerOperand();
uint64_t StoreSize =
DL.getTypeSizeInBits(DepSI->getValueOperand()->getType()).getFixedSize();
return analyzeLoadFromClobberingWrite(LoadTy, LoadPtr, StorePtr, StoreSize,
DL);
}
static unsigned getLoadLoadClobberFullWidthSize(const Value *MemLocBase,
int64_t MemLocOffs,
unsigned MemLocSize,
const LoadInst *LI) {
if (!isa<IntegerType>(LI->getType()) || !LI->isSimple())
return 0;
if (LI->getParent()->getParent()->hasFnAttribute(Attribute::SanitizeThread))
return 0;
const DataLayout &DL = LI->getModule()->getDataLayout();
int64_t LIOffs = 0;
const Value *LIBase =
GetPointerBaseWithConstantOffset(LI->getPointerOperand(), LIOffs, DL);
if (LIBase != MemLocBase)
return 0;
if (MemLocOffs < LIOffs)
return 0;
unsigned LoadAlign = LI->getAlign().value();
int64_t MemLocEnd = MemLocOffs + MemLocSize;
if (LIOffs + LoadAlign < MemLocEnd)
return 0;
unsigned NewLoadByteSize = LI->getType()->getPrimitiveSizeInBits() / 8U;
NewLoadByteSize = NextPowerOf2(NewLoadByteSize);
while (true) {
if (NewLoadByteSize > LoadAlign ||
!DL.fitsInLegalInteger(NewLoadByteSize * 8))
return 0;
if (LIOffs + NewLoadByteSize > MemLocEnd &&
(LI->getParent()->getParent()->hasFnAttribute(
Attribute::SanitizeAddress) ||
LI->getParent()->getParent()->hasFnAttribute(
Attribute::SanitizeHWAddress)))
return 0;
if (LIOffs + NewLoadByteSize >= MemLocEnd)
return NewLoadByteSize;
NewLoadByteSize <<= 1;
}
}
int analyzeLoadFromClobberingLoad(Type *LoadTy, Value *LoadPtr, LoadInst *DepLI,
const DataLayout &DL) {
if (DepLI->getType()->isStructTy() || DepLI->getType()->isArrayTy())
return -1;
if (!canCoerceMustAliasedValueToLoad(DepLI, LoadTy, DL))
return -1;
Value *DepPtr = DepLI->getPointerOperand();
uint64_t DepSize = DL.getTypeSizeInBits(DepLI->getType()).getFixedSize();
int R = analyzeLoadFromClobberingWrite(LoadTy, LoadPtr, DepPtr, DepSize, DL);
if (R != -1)
return R;
int64_t LoadOffs = 0;
const Value *LoadBase =
GetPointerBaseWithConstantOffset(LoadPtr, LoadOffs, DL);
unsigned LoadSize = DL.getTypeStoreSize(LoadTy).getFixedSize();
unsigned Size =
getLoadLoadClobberFullWidthSize(LoadBase, LoadOffs, LoadSize, DepLI);
if (Size == 0)
return -1;
assert(DepLI->isSimple() && "Cannot widen volatile/atomic load!");
assert(DepLI->getType()->isIntegerTy() && "Can't widen non-integer load");
return analyzeLoadFromClobberingWrite(LoadTy, LoadPtr, DepPtr, Size * 8, DL);
}
int analyzeLoadFromClobberingMemInst(Type *LoadTy, Value *LoadPtr,
MemIntrinsic *MI, const DataLayout &DL) {
ConstantInt *SizeCst = dyn_cast<ConstantInt>(MI->getLength());
if (!SizeCst)
return -1;
uint64_t MemSizeInBits = SizeCst->getZExtValue() * 8;
if (const auto *memset_inst = dyn_cast<MemSetInst>(MI)) {
if (DL.isNonIntegralPointerType(LoadTy->getScalarType())) {
auto *CI = dyn_cast<ConstantInt>(memset_inst->getValue());
if (!CI || !CI->isZero())
return -1;
}
return analyzeLoadFromClobberingWrite(LoadTy, LoadPtr, MI->getDest(),
MemSizeInBits, DL);
}
MemTransferInst *MTI = cast<MemTransferInst>(MI);
Constant *Src = dyn_cast<Constant>(MTI->getSource());
if (!Src)
return -1;
GlobalVariable *GV = dyn_cast<GlobalVariable>(getUnderlyingObject(Src));
if (!GV || !GV->isConstant() || !GV->hasDefinitiveInitializer())
return -1;
int Offset = analyzeLoadFromClobberingWrite(LoadTy, LoadPtr, MI->getDest(),
MemSizeInBits, DL);
if (Offset == -1)
return Offset;
unsigned IndexSize = DL.getIndexTypeSizeInBits(Src->getType());
if (ConstantFoldLoadFromConstPtr(Src, LoadTy, APInt(IndexSize, Offset), DL))
return Offset;
return -1;
}
static Value *getStoreValueForLoadHelper(Value *SrcVal, unsigned Offset,
Type *LoadTy, IRBuilderBase &Builder,
const DataLayout &DL) {
LLVMContext &Ctx = SrcVal->getType()->getContext();
if (SrcVal->getType()->isPointerTy() && LoadTy->isPointerTy() &&
cast<PointerType>(SrcVal->getType())->getAddressSpace() ==
cast<PointerType>(LoadTy)->getAddressSpace()) {
return SrcVal;
}
uint64_t StoreSize =
(DL.getTypeSizeInBits(SrcVal->getType()).getFixedSize() + 7) / 8;
uint64_t LoadSize = (DL.getTypeSizeInBits(LoadTy).getFixedSize() + 7) / 8;
if (SrcVal->getType()->isPtrOrPtrVectorTy())
SrcVal =
Builder.CreatePtrToInt(SrcVal, DL.getIntPtrType(SrcVal->getType()));
if (!SrcVal->getType()->isIntegerTy())
SrcVal =
Builder.CreateBitCast(SrcVal, IntegerType::get(Ctx, StoreSize * 8));
unsigned ShiftAmt;
if (DL.isLittleEndian())
ShiftAmt = Offset * 8;
else
ShiftAmt = (StoreSize - LoadSize - Offset) * 8;
if (ShiftAmt)
SrcVal = Builder.CreateLShr(SrcVal,
ConstantInt::get(SrcVal->getType(), ShiftAmt));
if (LoadSize != StoreSize)
SrcVal = Builder.CreateTruncOrBitCast(SrcVal,
IntegerType::get(Ctx, LoadSize * 8));
return SrcVal;
}
Value *getStoreValueForLoad(Value *SrcVal, unsigned Offset, Type *LoadTy,
Instruction *InsertPt, const DataLayout &DL) {
IRBuilder<> Builder(InsertPt);
SrcVal = getStoreValueForLoadHelper(SrcVal, Offset, LoadTy, Builder, DL);
return coerceAvailableValueToLoadType(SrcVal, LoadTy, Builder, DL);
}
Constant *getConstantStoreValueForLoad(Constant *SrcVal, unsigned Offset,
Type *LoadTy, const DataLayout &DL) {
return ConstantFoldLoadFromConst(SrcVal, LoadTy, APInt(32, Offset), DL);
}
Value *getLoadValueForLoad(LoadInst *SrcVal, unsigned Offset, Type *LoadTy,
Instruction *InsertPt, const DataLayout &DL) {
unsigned SrcValStoreSize =
DL.getTypeStoreSize(SrcVal->getType()).getFixedSize();
unsigned LoadSize = DL.getTypeStoreSize(LoadTy).getFixedSize();
if (Offset + LoadSize > SrcValStoreSize) {
assert(SrcVal->isSimple() && "Cannot widen volatile/atomic load!");
assert(SrcVal->getType()->isIntegerTy() && "Can't widen non-integer load");
unsigned NewLoadSize = Offset + LoadSize;
if (!isPowerOf2_32(NewLoadSize))
NewLoadSize = NextPowerOf2(NewLoadSize);
Value *PtrVal = SrcVal->getPointerOperand();
IRBuilder<> Builder(SrcVal->getParent(), ++BasicBlock::iterator(SrcVal));
Type *DestTy = IntegerType::get(LoadTy->getContext(), NewLoadSize * 8);
Type *DestPTy =
PointerType::get(DestTy, PtrVal->getType()->getPointerAddressSpace());
Builder.SetCurrentDebugLocation(SrcVal->getDebugLoc());
PtrVal = Builder.CreateBitCast(PtrVal, DestPTy);
LoadInst *NewLoad = Builder.CreateLoad(DestTy, PtrVal);
NewLoad->takeName(SrcVal);
NewLoad->setAlignment(SrcVal->getAlign());
LLVM_DEBUG(dbgs() << "GVN WIDENED LOAD: " << *SrcVal << "\n");
LLVM_DEBUG(dbgs() << "TO: " << *NewLoad << "\n");
Value *RV = NewLoad;
if (DL.isBigEndian())
RV = Builder.CreateLShr(RV, (NewLoadSize - SrcValStoreSize) * 8);
RV = Builder.CreateTrunc(RV, SrcVal->getType());
SrcVal->replaceAllUsesWith(RV);
SrcVal = NewLoad;
}
return getStoreValueForLoad(SrcVal, Offset, LoadTy, InsertPt, DL);
}
Constant *getConstantLoadValueForLoad(Constant *SrcVal, unsigned Offset,
Type *LoadTy, const DataLayout &DL) {
unsigned SrcValStoreSize =
DL.getTypeStoreSize(SrcVal->getType()).getFixedSize();
unsigned LoadSize = DL.getTypeStoreSize(LoadTy).getFixedSize();
if (Offset + LoadSize > SrcValStoreSize)
return nullptr;
return getConstantStoreValueForLoad(SrcVal, Offset, LoadTy, DL);
}
Value *getMemInstValueForLoad(MemIntrinsic *SrcInst, unsigned Offset,
Type *LoadTy, Instruction *InsertPt,
const DataLayout &DL) {
LLVMContext &Ctx = LoadTy->getContext();
uint64_t LoadSize = DL.getTypeSizeInBits(LoadTy).getFixedSize() / 8;
IRBuilder<> Builder(InsertPt);
if (MemSetInst *MSI = dyn_cast<MemSetInst>(SrcInst)) {
Value *Val = MSI->getValue();
if (LoadSize != 1)
Val =
Builder.CreateZExtOrBitCast(Val, IntegerType::get(Ctx, LoadSize * 8));
Value *OneElt = Val;
for (unsigned NumBytesSet = 1; NumBytesSet != LoadSize;) {
if (NumBytesSet * 2 <= LoadSize) {
Value *ShVal = Builder.CreateShl(
Val, ConstantInt::get(Val->getType(), NumBytesSet * 8));
Val = Builder.CreateOr(Val, ShVal);
NumBytesSet <<= 1;
continue;
}
Value *ShVal =
Builder.CreateShl(Val, ConstantInt::get(Val->getType(), 1 * 8));
Val = Builder.CreateOr(OneElt, ShVal);
++NumBytesSet;
}
return coerceAvailableValueToLoadType(Val, LoadTy, Builder, DL);
}
MemTransferInst *MTI = cast<MemTransferInst>(SrcInst);
Constant *Src = cast<Constant>(MTI->getSource());
unsigned IndexSize = DL.getIndexTypeSizeInBits(Src->getType());
return ConstantFoldLoadFromConstPtr(Src, LoadTy, APInt(IndexSize, Offset),
DL);
}
Constant *getConstantMemInstValueForLoad(MemIntrinsic *SrcInst, unsigned Offset,
Type *LoadTy, const DataLayout &DL) {
LLVMContext &Ctx = LoadTy->getContext();
uint64_t LoadSize = DL.getTypeSizeInBits(LoadTy).getFixedSize() / 8;
if (MemSetInst *MSI = dyn_cast<MemSetInst>(SrcInst)) {
auto *Val = dyn_cast<ConstantInt>(MSI->getValue());
if (!Val)
return nullptr;
Val = ConstantInt::get(Ctx, APInt::getSplat(LoadSize * 8, Val->getValue()));
return ConstantFoldLoadFromConst(Val, LoadTy, DL);
}
MemTransferInst *MTI = cast<MemTransferInst>(SrcInst);
Constant *Src = cast<Constant>(MTI->getSource());
unsigned IndexSize = DL.getIndexTypeSizeInBits(Src->getType());
return ConstantFoldLoadFromConstPtr(Src, LoadTy, APInt(IndexSize, Offset),
DL);
}
} }