Skip to content

Commit 710596a

Browse files
committed
[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.
1 parent d8e4170 commit 710596a

File tree

3 files changed

+21
-27
lines changed

3 files changed

+21
-27
lines changed

llvm/include/llvm/Analysis/ConstantFolding.h

+9-4
Original file line numberDiff line numberDiff line change
@@ -138,10 +138,15 @@ Constant *ConstantFoldLoadFromConst(Constant *C, Type *Ty, const APInt &Offset,
138138
Constant *ConstantFoldLoadFromConst(Constant *C, Type *Ty,
139139
const DataLayout &DL);
140140

141-
/// ConstantFoldLoadFromConstPtr - Return the value that a load from C would
142-
/// produce if it is constant and determinable. If this is not determinable,
143-
/// return null.
144-
Constant *ConstantFoldLoadFromConstPtr(Constant *C, Type *Ty, const DataLayout &DL);
141+
/// Return the value that a load from C with offset Offset would produce if it
142+
/// is constant and determinable. If this is not determinable, return null.
143+
Constant *ConstantFoldLoadFromConstPtr(Constant *C, Type *Ty, APInt Offset,
144+
const DataLayout &DL);
145+
146+
/// Return the value that a load from C would produce if it is constant and
147+
/// determinable. If this is not determinable, return null.
148+
Constant *ConstantFoldLoadFromConstPtr(Constant *C, Type *Ty,
149+
const DataLayout &DL);
145150

146151
/// ConstantFoldLoadThroughGEPConstantExpr - Given a constant and a
147152
/// getelementptr constantexpr, return the constant value being addressed by the

llvm/lib/Analysis/ConstantFolding.cpp

+7-1
Original file line numberDiff line numberDiff line change
@@ -690,8 +690,8 @@ Constant *llvm::ConstantFoldLoadFromConst(Constant *C, Type *Ty,
690690
}
691691

692692
Constant *llvm::ConstantFoldLoadFromConstPtr(Constant *C, Type *Ty,
693+
APInt Offset,
693694
const DataLayout &DL) {
694-
APInt Offset(DL.getIndexTypeSizeInBits(C->getType()), 0);
695695
C = cast<Constant>(C->stripAndAccumulateConstantOffsets(
696696
DL, Offset, /* AllowNonInbounds */ true));
697697

@@ -715,6 +715,12 @@ Constant *llvm::ConstantFoldLoadFromConstPtr(Constant *C, Type *Ty,
715715
return nullptr;
716716
}
717717

718+
Constant *llvm::ConstantFoldLoadFromConstPtr(Constant *C, Type *Ty,
719+
const DataLayout &DL) {
720+
APInt Offset(DL.getIndexTypeSizeInBits(C->getType()), 0);
721+
return ConstantFoldLoadFromConstPtr(C, Ty, Offset, DL);
722+
}
723+
718724
namespace {
719725

720726
/// One of Op0/Op1 is a constant expression.

llvm/lib/Transforms/Utils/VNCoercion.cpp

+5-22
Original file line numberDiff line numberDiff line change
@@ -403,19 +403,10 @@ int analyzeLoadFromClobberingMemInst(Type *LoadTy, Value *LoadPtr,
403403
if (Offset == -1)
404404
return Offset;
405405

406-
unsigned AS = Src->getType()->getPointerAddressSpace();
407406
// Otherwise, see if we can constant fold a load from the constant with the
408407
// offset applied as appropriate.
409-
if (Offset) {
410-
Src = ConstantExpr::getBitCast(Src,
411-
Type::getInt8PtrTy(Src->getContext(), AS));
412-
Constant *OffsetCst =
413-
ConstantInt::get(Type::getInt64Ty(Src->getContext()), (unsigned)Offset);
414-
Src = ConstantExpr::getGetElementPtr(Type::getInt8Ty(Src->getContext()),
415-
Src, OffsetCst);
416-
}
417-
Src = ConstantExpr::getBitCast(Src, PointerType::get(LoadTy, AS));
418-
if (ConstantFoldLoadFromConstPtr(Src, LoadTy, DL))
408+
unsigned IndexSize = DL.getIndexTypeSizeInBits(Src->getType());
409+
if (ConstantFoldLoadFromConstPtr(Src, LoadTy, APInt(IndexSize, Offset), DL))
419410
return Offset;
420411
return -1;
421412
}
@@ -584,19 +575,11 @@ T *getMemInstValueForLoadHelper(MemIntrinsic *SrcInst, unsigned Offset,
584575
MemTransferInst *MTI = cast<MemTransferInst>(SrcInst);
585576
Constant *Src = cast<Constant>(MTI->getSource());
586577

587-
unsigned AS = Src->getType()->getPointerAddressSpace();
588578
// Otherwise, see if we can constant fold a load from the constant with the
589579
// offset applied as appropriate.
590-
if (Offset) {
591-
Src = ConstantExpr::getBitCast(Src,
592-
Type::getInt8PtrTy(Src->getContext(), AS));
593-
Constant *OffsetCst =
594-
ConstantInt::get(Type::getInt64Ty(Src->getContext()), (unsigned)Offset);
595-
Src = ConstantExpr::getGetElementPtr(Type::getInt8Ty(Src->getContext()),
596-
Src, OffsetCst);
597-
}
598-
Src = ConstantExpr::getBitCast(Src, PointerType::get(LoadTy, AS));
599-
return ConstantFoldLoadFromConstPtr(Src, LoadTy, DL);
580+
unsigned IndexSize = DL.getIndexTypeSizeInBits(Src->getType());
581+
return ConstantFoldLoadFromConstPtr(
582+
Src, LoadTy, APInt(IndexSize, Offset), DL);
600583
}
601584

602585
/// This function is called when we have a

0 commit comments

Comments
 (0)