From 710596a1e15188171edd5c6fffe6b7fe483ca594 Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Sat, 23 Oct 2021 17:58:07 +0200 Subject: [PATCH] [ConstantFolding] Accept offset in ConstantFoldLoadFromConstPtr (NFCI) As this API is now internally offset-based, we can accept a starting offset and remove the need to create a temporary bitcast+gep sequence to perform an offset load. The API now mirrors the ConstantFoldLoadFromConst() API. --- llvm/include/llvm/Analysis/ConstantFolding.h | 13 +++++++--- llvm/lib/Analysis/ConstantFolding.cpp | 8 +++++- llvm/lib/Transforms/Utils/VNCoercion.cpp | 27 ++++---------------- 3 files changed, 21 insertions(+), 27 deletions(-) diff --git a/llvm/include/llvm/Analysis/ConstantFolding.h b/llvm/include/llvm/Analysis/ConstantFolding.h index f4865f4e0883b..45fb879f0c1f1 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 8ec4ea109e2b3..4cc3fcd149d6a 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 6336af25ef982..dbe3cc93e72b4 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