diff --git a/llvm/include/llvm/Analysis/ConstantFolding.h b/llvm/include/llvm/Analysis/ConstantFolding.h index f4865f4e0883bf..45fb879f0c1f17 100644 --- a/llvm/include/llvm/Analysis/ConstantFolding.h +++ b/llvm/include/llvm/Analysis/ConstantFolding.h @@ -138,10 +138,15 @@ Constant *ConstantFoldLoadFromConst(Constant *C, Type *Ty, const APInt &Offset, Constant *ConstantFoldLoadFromConst(Constant *C, Type *Ty, const DataLayout &DL); -/// ConstantFoldLoadFromConstPtr - Return the value that a load from C would -/// produce if it is constant and determinable. If this is not determinable, -/// return null. -Constant *ConstantFoldLoadFromConstPtr(Constant *C, Type *Ty, const DataLayout &DL); +/// Return the value that a load from C with offset Offset would produce if it +/// is constant and determinable. If this is not determinable, return null. +Constant *ConstantFoldLoadFromConstPtr(Constant *C, Type *Ty, APInt Offset, + const DataLayout &DL); + +/// Return the value that a load from C would produce if it is constant and +/// determinable. If this is not determinable, return null. +Constant *ConstantFoldLoadFromConstPtr(Constant *C, Type *Ty, + const DataLayout &DL); /// ConstantFoldLoadThroughGEPConstantExpr - Given a constant and a /// getelementptr constantexpr, return the constant value being addressed by the diff --git a/llvm/lib/Analysis/ConstantFolding.cpp b/llvm/lib/Analysis/ConstantFolding.cpp index 8ec4ea109e2b3f..4cc3fcd149d6a2 100644 --- a/llvm/lib/Analysis/ConstantFolding.cpp +++ b/llvm/lib/Analysis/ConstantFolding.cpp @@ -690,8 +690,8 @@ Constant *llvm::ConstantFoldLoadFromConst(Constant *C, Type *Ty, } Constant *llvm::ConstantFoldLoadFromConstPtr(Constant *C, Type *Ty, + APInt Offset, const DataLayout &DL) { - APInt Offset(DL.getIndexTypeSizeInBits(C->getType()), 0); C = cast(C->stripAndAccumulateConstantOffsets( DL, Offset, /* AllowNonInbounds */ true)); @@ -715,6 +715,12 @@ Constant *llvm::ConstantFoldLoadFromConstPtr(Constant *C, Type *Ty, return nullptr; } +Constant *llvm::ConstantFoldLoadFromConstPtr(Constant *C, Type *Ty, + const DataLayout &DL) { + APInt Offset(DL.getIndexTypeSizeInBits(C->getType()), 0); + return ConstantFoldLoadFromConstPtr(C, Ty, Offset, DL); +} + namespace { /// One of Op0/Op1 is a constant expression. diff --git a/llvm/lib/Transforms/Utils/VNCoercion.cpp b/llvm/lib/Transforms/Utils/VNCoercion.cpp index 6336af25ef982b..dbe3cc93e72b48 100644 --- a/llvm/lib/Transforms/Utils/VNCoercion.cpp +++ b/llvm/lib/Transforms/Utils/VNCoercion.cpp @@ -403,19 +403,10 @@ int analyzeLoadFromClobberingMemInst(Type *LoadTy, Value *LoadPtr, if (Offset == -1) return Offset; - unsigned AS = Src->getType()->getPointerAddressSpace(); // Otherwise, see if we can constant fold a load from the constant with the // offset applied as appropriate. - if (Offset) { - Src = ConstantExpr::getBitCast(Src, - Type::getInt8PtrTy(Src->getContext(), AS)); - Constant *OffsetCst = - ConstantInt::get(Type::getInt64Ty(Src->getContext()), (unsigned)Offset); - Src = ConstantExpr::getGetElementPtr(Type::getInt8Ty(Src->getContext()), - Src, OffsetCst); - } - Src = ConstantExpr::getBitCast(Src, PointerType::get(LoadTy, AS)); - if (ConstantFoldLoadFromConstPtr(Src, LoadTy, DL)) + unsigned IndexSize = DL.getIndexTypeSizeInBits(Src->getType()); + if (ConstantFoldLoadFromConstPtr(Src, LoadTy, APInt(IndexSize, Offset), DL)) return Offset; return -1; } @@ -584,19 +575,11 @@ T *getMemInstValueForLoadHelper(MemIntrinsic *SrcInst, unsigned Offset, MemTransferInst *MTI = cast(SrcInst); Constant *Src = cast(MTI->getSource()); - unsigned AS = Src->getType()->getPointerAddressSpace(); // Otherwise, see if we can constant fold a load from the constant with the // offset applied as appropriate. - if (Offset) { - Src = ConstantExpr::getBitCast(Src, - Type::getInt8PtrTy(Src->getContext(), AS)); - Constant *OffsetCst = - ConstantInt::get(Type::getInt64Ty(Src->getContext()), (unsigned)Offset); - Src = ConstantExpr::getGetElementPtr(Type::getInt8Ty(Src->getContext()), - Src, OffsetCst); - } - Src = ConstantExpr::getBitCast(Src, PointerType::get(LoadTy, AS)); - return ConstantFoldLoadFromConstPtr(Src, LoadTy, DL); + unsigned IndexSize = DL.getIndexTypeSizeInBits(Src->getType()); + return ConstantFoldLoadFromConstPtr( + Src, LoadTy, APInt(IndexSize, Offset), DL); } /// This function is called when we have a