[llvm-branch-commits] [clang] [llvm] [IRBuilder] Add getByteTy and use it in CreatePtrAdd (PR #106539)
Sergei Barannikov via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Tue Oct 6 20:53:47 PDT 2026
https://github.com/s-barannikov updated https://github.com/llvm/llvm-project/pull/106539
>From fed33b620b692e34079051298a5c8121f77ad531 Mon Sep 17 00:00:00 2001
From: Sergei Barannikov <barannikov88 at gmail.com>
Date: Thu, 22 Aug 2024 15:10:58 +0300
Subject: [PATCH 1/2] [IRBuilder] Add getByteTy and use it in CreatePtrAdd
The change requires DataLayout instance to be available, which, in turn,
requires insertion point to be set. In-tree tests detected only one case
when the function was called without setting an insertion point, it was
changed to create a constant expression directly.
---
llvm/include/llvm/IR/IRBuilder.h | 10 ++++++++--
llvm/unittests/IR/IRBuilderTest.cpp | 22 ++++++++++++++++++++++
2 files changed, 30 insertions(+), 2 deletions(-)
diff --git a/llvm/include/llvm/IR/IRBuilder.h b/llvm/include/llvm/IR/IRBuilder.h
index bbeefa8ef90c0..331c89c3218b4 100644
--- a/llvm/include/llvm/IR/IRBuilder.h
+++ b/llvm/include/llvm/IR/IRBuilder.h
@@ -483,6 +483,12 @@ class IRBuilderBase {
// Type creation methods
//===--------------------------------------------------------------------===//
+ /// Fetch the type representing a byte.
+ IntegerType *getByteTy() {
+ const DataLayout &DL = getDataLayout();
+ return Type::getIntNTy(Context, DL.getByteWidth());
+ }
+
/// Fetch the type representing an 8-bit byte.
ByteType *getByte8Ty() { return Type::getByte8Ty(Context); }
@@ -2089,12 +2095,12 @@ class IRBuilderBase {
Value *CreatePtrAdd(Value *Ptr, Value *Offset, const Twine &Name = "",
GEPNoWrapFlags NW = GEPNoWrapFlags::none()) {
- return CreateGEP(getInt8Ty(), Ptr, Offset, Name, NW);
+ return CreateGEP(getByteTy(), Ptr, Offset, Name, NW);
}
Value *CreateInBoundsPtrAdd(Value *Ptr, Value *Offset,
const Twine &Name = "") {
- return CreateGEP(getInt8Ty(), Ptr, Offset, Name,
+ return CreateGEP(getByteTy(), Ptr, Offset, Name,
GEPNoWrapFlags::inBounds());
}
diff --git a/llvm/unittests/IR/IRBuilderTest.cpp b/llvm/unittests/IR/IRBuilderTest.cpp
index 14a3065b989ad..4261e98c084d9 100644
--- a/llvm/unittests/IR/IRBuilderTest.cpp
+++ b/llvm/unittests/IR/IRBuilderTest.cpp
@@ -595,6 +595,14 @@ TEST_F(IRBuilderTest, DataLayout) {
EXPECT_FALSE(M->getDataLayout().isLegalInteger(32));
}
+TEST_F(IRBuilderTest, GetByteTy) {
+ IRBuilder<> Builder(BB);
+
+ EXPECT_TRUE(Builder.getByteTy()->isIntegerTy(8));
+ M->setDataLayout("b:32");
+ EXPECT_TRUE(Builder.getByteTy()->isIntegerTy(32));
+}
+
TEST_F(IRBuilderTest, GetIntTy) {
IRBuilder<> Builder(BB);
IntegerType *Ty1 = Builder.getInt1Ty();
@@ -606,6 +614,20 @@ TEST_F(IRBuilderTest, GetIntTy) {
EXPECT_EQ(IntPtrTy, IntegerType::get(Ctx, IntPtrBitSize));
}
+TEST_F(IRBuilderTest, CreatePtrAdd) {
+ IRBuilder<> Builder(BB);
+
+ M->setDataLayout("b:16-p:32:32");
+ Value *V = Builder.CreatePtrAdd(GV, ConstantInt::get(Ctx, APInt(32, 42)));
+ ASSERT_TRUE(isa<GEPOperator>(V));
+ EXPECT_TRUE(cast<GEPOperator>(V)->getResultElementType()->isIntegerTy(16));
+
+ M->setDataLayout("b:32-p:64:32");
+ V = Builder.CreateInBoundsPtrAdd(GV, ConstantInt::get(Ctx, APInt(64, 42)));
+ ASSERT_TRUE(isa<GEPOperator>(V));
+ EXPECT_TRUE(cast<GEPOperator>(V)->getResultElementType()->isIntegerTy(32));
+}
+
TEST_F(IRBuilderTest, UnaryOperators) {
IRBuilder<NoFolder> Builder(BB);
Value *V = Builder.CreateLoad(GV->getValueType(), GV);
>From 1d2b7882c72a259c7e8bc45d8fd263aad02d391b Mon Sep 17 00:00:00 2001
From: Sergei Barannikov <barannikov88 at gmail.com>
Date: Sun, 22 Feb 2026 20:17:40 +0300
Subject: [PATCH 2/2] Use DL in ConstantExpr::getPtrAdd() /
ConstantExpr::getInBoundsPtrAdd()
---
clang/lib/CodeGen/CGExprConstant.cpp | 2 +-
clang/lib/CodeGen/CGObjCMac.cpp | 2 +-
clang/lib/CodeGen/ItaniumCXXABI.cpp | 3 ++-
llvm/bindings/ocaml/llvm/llvm.ml | 2 +-
llvm/bindings/ocaml/llvm/llvm.mli | 7 ++---
llvm/bindings/ocaml/llvm/llvm_ocaml.c | 11 ++++----
llvm/include/llvm-c/Core.h | 3 ++-
llvm/include/llvm/IR/Constants.h | 22 +++++++--------
llvm/lib/Analysis/ConstantFolding.cpp | 2 +-
llvm/lib/Analysis/ScalarEvolution.cpp | 16 +++++------
.../Frontend/Offloading/OffloadWrapper.cpp | 4 +--
llvm/lib/IR/Constants.cpp | 17 +++++++++---
llvm/lib/IR/Core.cpp | 6 +++--
llvm/lib/Target/XCore/XCoreISelLowering.cpp | 3 ++-
llvm/lib/Transforms/IPO/GlobalSplit.cpp | 2 +-
llvm/lib/Transforms/IPO/LowerTypeTests.cpp | 8 +++---
.../lib/Transforms/IPO/WholeProgramDevirt.cpp | 2 +-
.../Instrumentation/IndirectCallPromotion.cpp | 2 +-
.../Instrumentation/SanitizerCoverage.cpp | 5 ++--
llvm/test/Bindings/OCaml/core.ml | 12 +++++----
llvm/unittests/IR/ConstantsTest.cpp | 27 ++++++++++---------
llvm/unittests/IR/PatternMatch.cpp | 3 ++-
.../Utils/CallPromotionUtilsTest.cpp | 2 +-
23 files changed, 94 insertions(+), 69 deletions(-)
diff --git a/clang/lib/CodeGen/CGExprConstant.cpp b/clang/lib/CodeGen/CGExprConstant.cpp
index e2ec07bbae711..bb3a4160f2a18 100644
--- a/clang/lib/CodeGen/CGExprConstant.cpp
+++ b/clang/lib/CodeGen/CGExprConstant.cpp
@@ -2221,7 +2221,7 @@ class ConstantLValueEmitter : public ConstStmtVisitor<ConstantLValueEmitter,
if (!hasNonZeroOffset())
return C;
- return llvm::ConstantExpr::getPtrAdd(C, getOffset());
+ return llvm::ConstantExpr::getPtrAdd(CGM.getDataLayout(), C, getOffset());
}
};
diff --git a/clang/lib/CodeGen/CGObjCMac.cpp b/clang/lib/CodeGen/CGObjCMac.cpp
index 6129a11ac5952..6a660b73c9944 100644
--- a/clang/lib/CodeGen/CGObjCMac.cpp
+++ b/clang/lib/CodeGen/CGObjCMac.cpp
@@ -8313,7 +8313,7 @@ CGObjCNonFragileABIMac::GetClassGlobalForClassRef(const ObjCInterfaceDecl *ID) {
// Stub classes are pointer-aligned. Classrefs pointing at stub classes
// must set the least significant bit set to 1.
auto *Idx = llvm::ConstantInt::get(CGM.Int32Ty, 1);
- return llvm::ConstantExpr::getPtrAdd(ClassGV, Idx);
+ return llvm::ConstantExpr::getPtrAdd(CGM.getDataLayout(), ClassGV, Idx);
}
llvm::Value *
diff --git a/clang/lib/CodeGen/ItaniumCXXABI.cpp b/clang/lib/CodeGen/ItaniumCXXABI.cpp
index 5fe80e16f68c8..184d195d79e89 100644
--- a/clang/lib/CodeGen/ItaniumCXXABI.cpp
+++ b/clang/lib/CodeGen/ItaniumCXXABI.cpp
@@ -4010,7 +4010,8 @@ void ItaniumRTTIBuilder::BuildVTablePointer(const Type *Ty,
// The vtable address point is 8 bytes after its start:
// 4 for the offset to top + 4 for the relative offset to rtti.
llvm::Constant *Eight = llvm::ConstantInt::get(CGM.Int32Ty, 8);
- VTable = llvm::ConstantExpr::getInBoundsPtrAdd(VTable, Eight);
+ VTable = llvm::ConstantExpr::getInBoundsPtrAdd(CGM.getDataLayout(), VTable,
+ Eight);
} else {
llvm::Constant *Two = llvm::ConstantInt::get(PtrDiffTy, 2);
VTable = llvm::ConstantExpr::getGetElementPtr(
diff --git a/llvm/bindings/ocaml/llvm/llvm.ml b/llvm/bindings/ocaml/llvm/llvm.ml
index 9fa54d57bb8e6..09337d9c46204 100644
--- a/llvm/bindings/ocaml/llvm/llvm.ml
+++ b/llvm/bindings/ocaml/llvm/llvm.ml
@@ -703,7 +703,7 @@ external const_sub : llvalue -> llvalue -> llvalue = "llvm_const_sub"
external const_nsw_sub : llvalue -> llvalue -> llvalue = "llvm_const_nsw_sub"
external const_nuw_sub : llvalue -> llvalue -> llvalue = "llvm_const_nuw_sub"
external const_xor : llvalue -> llvalue -> llvalue = "llvm_const_xor"
-external const_ptradd : llvalue -> llvalue -> int -> llvalue
+external const_ptradd : DataLayout.t -> llvalue -> llvalue -> int -> llvalue
= "llvm_const_ptradd"
external const_ptradd_from_indices : DataLayout.t -> lltype -> llvalue ->
llvalue array -> int -> llvalue option
diff --git a/llvm/bindings/ocaml/llvm/llvm.mli b/llvm/bindings/ocaml/llvm/llvm.mli
index b51ca2183ac5a..7bbbf06f7d246 100644
--- a/llvm/bindings/ocaml/llvm/llvm.mli
+++ b/llvm/bindings/ocaml/llvm/llvm.mli
@@ -1205,11 +1205,12 @@ val const_nuw_sub : llvalue -> llvalue -> llvalue
See the method [llvm::ConstantExpr::getXor]. *)
val const_xor : llvalue -> llvalue -> llvalue
-(** [const_ptradd pc offset flags] returns the constant ptradd
- (getelementptr i8) of [pc] with constant [offset] and the given
+(** [const_ptradd dl pc offset flags] returns the constant ptradd
+ (getelementptr iN) of [pc] with constant [offset] and the given
{!GEPNoWrapFlags} no-wrap flags (combined with [lor]).
+ iN is the width of a byte derived from the data layout [dl].
See the method [llvm::ConstantExpr::getPtrAdd]. *)
-val const_ptradd : llvalue -> llvalue -> int -> llvalue
+val const_ptradd : DataLayout.t -> llvalue -> llvalue -> int -> llvalue
(** [const_ptradd_from_indices dl srcty pc indices flags] returns the constant
ptradd of [pc] with the offset derived from the data layout [dl], the
diff --git a/llvm/bindings/ocaml/llvm/llvm_ocaml.c b/llvm/bindings/ocaml/llvm/llvm_ocaml.c
index 596ef35dffa20..26c6e6229dac3 100644
--- a/llvm/bindings/ocaml/llvm/llvm_ocaml.c
+++ b/llvm/bindings/ocaml/llvm/llvm_ocaml.c
@@ -1195,11 +1195,12 @@ value llvm_const_xor(value LHS, value RHS) {
return to_val(Value);
}
-/* llvalue -> llvalue -> int -> llvalue */
-value llvm_const_ptradd(value ConstantVal, value ConstantOffset,
- value NoWrapFlags) {
- LLVMValueRef Value = LLVMConstPtrAdd(
- Value_val(ConstantVal), Value_val(ConstantOffset), Int_val(NoWrapFlags));
+/* DataLayout.t -> llvalue -> llvalue -> int -> llvalue */
+value llvm_const_ptradd(value DataLayout, value ConstantVal,
+ value ConstantOffset, value NoWrapFlags) {
+ LLVMValueRef Value =
+ LLVMConstPtrAdd(DataLayout_val(DataLayout), Value_val(ConstantVal),
+ Value_val(ConstantOffset), Int_val(NoWrapFlags));
return to_val(Value);
}
diff --git a/llvm/include/llvm-c/Core.h b/llvm/include/llvm-c/Core.h
index 3296c427aa5ce..50766f8646251 100644
--- a/llvm/include/llvm-c/Core.h
+++ b/llvm/include/llvm-c/Core.h
@@ -2800,7 +2800,8 @@ LLVM_C_ABI LLVM_ATTRIBUTE_C_DEPRECATED(
*
* @see llvm::ConstantExpr::getPtrAdd()
*/
-LLVM_C_ABI LLVMValueRef LLVMConstPtrAdd(LLVMValueRef ConstantVal,
+LLVM_C_ABI LLVMValueRef LLVMConstPtrAdd(LLVMTargetDataRef DataLayout,
+ LLVMValueRef ConstantVal,
LLVMValueRef ConstantOffset,
LLVMGEPNoWrapFlags NoWrapFlags);
diff --git a/llvm/include/llvm/IR/Constants.h b/llvm/include/llvm/IR/Constants.h
index db5ef5dfccac8..510bcf2ad9232 100644
--- a/llvm/include/llvm/IR/Constants.h
+++ b/llvm/include/llvm/IR/Constants.h
@@ -1502,8 +1502,8 @@ class ConstantExpr : public Constant {
Type *OnlyIfReducedTy = nullptr);
/// Create a getelementptr constant expression in canonical ptradd form
- /// (getelementptr i8) by converting GEP indices to offsets using the
- /// provided data layout.
+ /// (getelementptr iN; N is the width of a byte) by converting GEP indices
+ /// to offsets using the provided data layout.
///
/// Returns nullptr if the indices cannot be converted to ptradd form.
LLVM_ABI static Constant *
@@ -1513,15 +1513,13 @@ class ConstantExpr : public Constant {
std::optional<ConstantRange> InRange = std::nullopt,
Type *OnlyIfReducedTy = nullptr);
- /// Create a getelementptr i8, ptr, offset constant expression.
+ /// Create a getelementptr iN, ptr, offset constant expression,
+ /// where iN is a byte-sized integer type.
static Constant *
- getPtrAdd(Constant *Ptr, Constant *Offset,
+ getPtrAdd(const DataLayout &DL, Constant *Ptr, Constant *Offset,
GEPNoWrapFlags NW = GEPNoWrapFlags::none(),
std::optional<ConstantRange> InRange = std::nullopt,
- Type *OnlyIfReduced = nullptr) {
- return getGetElementPtr(Type::getInt8Ty(Ptr->getContext()), Ptr, Offset, NW,
- InRange, OnlyIfReduced);
- }
+ Type *OnlyIfReduced = nullptr);
/// Create an "inbounds" getelementptr. See the documentation for the
/// "inbounds" flag in LangRef.html for details.
@@ -1546,9 +1544,11 @@ class ConstantExpr : public Constant {
LLVM_SUPPRESS_DEPRECATED_DECLARATIONS_POP
- /// Create a getelementptr inbounds i8, ptr, offset constant expression.
- static Constant *getInBoundsPtrAdd(Constant *Ptr, Constant *Offset) {
- return getPtrAdd(Ptr, Offset, GEPNoWrapFlags::inBounds());
+ /// Create a getelementptr inbounds iN, ptr, offset constant expression,
+ /// where iN is a byte-sized integer type.
+ static Constant *getInBoundsPtrAdd(const DataLayout &DL, Constant *Ptr,
+ Constant *Offset) {
+ return getPtrAdd(DL, Ptr, Offset, GEPNoWrapFlags::inBounds());
}
LLVM_ABI static Constant *getExtractElement(Constant *Vec, Constant *Idx,
diff --git a/llvm/lib/Analysis/ConstantFolding.cpp b/llvm/lib/Analysis/ConstantFolding.cpp
index b09f348a3b30a..a9badfd92bb0e 100644
--- a/llvm/lib/Analysis/ConstantFolding.cpp
+++ b/llvm/lib/Analysis/ConstantFolding.cpp
@@ -1119,7 +1119,7 @@ Constant *SymbolicallyEvaluateGEP(const GEPOperator *GEP,
// Otherwise canonicalize this to a single ptradd.
LLVMContext &Ctx = Ptr->getContext();
- return ConstantExpr::getPtrAdd(Ptr, ConstantInt::get(Ctx, Offset), NW,
+ return ConstantExpr::getPtrAdd(DL, Ptr, ConstantInt::get(Ctx, Offset), NW,
InRange);
}
diff --git a/llvm/lib/Analysis/ScalarEvolution.cpp b/llvm/lib/Analysis/ScalarEvolution.cpp
index f00064313a7c0..d1c32ac01cdff 100644
--- a/llvm/lib/Analysis/ScalarEvolution.cpp
+++ b/llvm/lib/Analysis/ScalarEvolution.cpp
@@ -10113,7 +10113,7 @@ SCEVUse ScalarEvolution::getSCEVAtExit(const SCEV *V, const Loop *L,
/// will return Constants for objects which aren't represented by a
/// SCEVConstant, because SCEVConstant is restricted to ConstantInt.
/// Returns NULL if the SCEV isn't representable as a Constant.
-static Constant *BuildConstantFromSCEV(const SCEV *V) {
+static Constant *BuildConstantFromSCEV(const SCEV *V, const DataLayout &DL) {
switch (V->getSCEVType()) {
case scCouldNotCompute:
case scAddRecExpr:
@@ -10125,14 +10125,14 @@ static Constant *BuildConstantFromSCEV(const SCEV *V) {
return dyn_cast<Constant>(cast<SCEVUnknown>(V)->getValue());
case scPtrToAddr: {
const SCEVPtrToAddrExpr *P2I = cast<SCEVPtrToAddrExpr>(V);
- if (Constant *CastOp = BuildConstantFromSCEV(P2I->getOperand()))
+ if (Constant *CastOp = BuildConstantFromSCEV(P2I->getOperand(), DL))
return ConstantExpr::getPtrToAddr(CastOp, P2I->getType());
return nullptr;
}
case scTruncate: {
const SCEVTruncateExpr *ST = cast<SCEVTruncateExpr>(V);
- if (Constant *CastOp = BuildConstantFromSCEV(ST->getOperand()))
+ if (Constant *CastOp = BuildConstantFromSCEV(ST->getOperand(), DL))
return ConstantExpr::getTrunc(CastOp, ST->getType());
return nullptr;
}
@@ -10140,7 +10140,7 @@ static Constant *BuildConstantFromSCEV(const SCEV *V) {
const SCEVAddExpr *SA = cast<SCEVAddExpr>(V);
Constant *C = nullptr;
for (const SCEV *Op : SA->operands()) {
- Constant *OpC = BuildConstantFromSCEV(Op);
+ Constant *OpC = BuildConstantFromSCEV(Op, DL);
if (!OpC)
return nullptr;
if (!C) {
@@ -10150,9 +10150,9 @@ static Constant *BuildConstantFromSCEV(const SCEV *V) {
assert(!C->getType()->isPointerTy() &&
"Can only have one pointer, and it must be last");
if (OpC->getType()->isPointerTy()) {
- // The offsets have been converted to bytes. We can add bytes using
- // an i8 GEP.
- C = ConstantExpr::getPtrAdd(OpC, C);
+ // The offsets have been converted to bytes. We can add bytes using
+ // a ptradd.
+ C = ConstantExpr::getPtrAdd(DL, OpC, C);
} else {
C = ConstantExpr::getAdd(C, OpC);
}
@@ -10378,7 +10378,7 @@ SCEVUse ScalarEvolution::computeSCEVAtScope(const SCEV *V, const Loop *L) {
const SCEV *OpV = getSCEVAtScope(OrigV, L);
MadeImprovement |= OrigV != OpV;
- Constant *C = BuildConstantFromSCEV(OpV);
+ Constant *C = BuildConstantFromSCEV(OpV, DL);
if (!C)
return V;
assert(C->getType() == Op->getType() && "Type mismatch");
diff --git a/llvm/lib/Frontend/Offloading/OffloadWrapper.cpp b/llvm/lib/Frontend/Offloading/OffloadWrapper.cpp
index bcd65513bc842..5e67cd155fe37 100644
--- a/llvm/lib/Frontend/Offloading/OffloadWrapper.cpp
+++ b/llvm/lib/Frontend/Offloading/OffloadWrapper.cpp
@@ -168,8 +168,8 @@ GlobalVariable *createBinDesc(Module &M, ArrayRef<ArrayRef<char>> Bufs,
auto *Begin = ConstantInt::get(getSizeTTy(M), BeginOffset);
auto *Size = ConstantInt::get(getSizeTTy(M), EndOffset);
- auto *ImageB = ConstantExpr::getPtrAdd(Image, Begin);
- auto *ImageE = ConstantExpr::getPtrAdd(Image, Size);
+ auto *ImageB = ConstantExpr::getPtrAdd(M.getDataLayout(), Image, Begin);
+ auto *ImageE = ConstantExpr::getPtrAdd(M.getDataLayout(), Image, Size);
ImagesInits.push_back(ConstantStruct::get(getDeviceImageTy(M), ImageB,
ImageE, EntriesB, EntriesE));
diff --git a/llvm/lib/IR/Constants.cpp b/llvm/lib/IR/Constants.cpp
index 59c2a97a851ab..3975c7fce6d75 100644
--- a/llvm/lib/IR/Constants.cpp
+++ b/llvm/lib/IR/Constants.cpp
@@ -2750,8 +2750,9 @@ Constant *ConstantExpr::getGetElementPtr(const DataLayout &DL, Type *Ty,
std::optional<ConstantRange> InRange,
Type *OnlyIfReducedTy) {
// Handle already canonical GEP.
- if (Ty->isIntegerTy(8) && Idxs[0]->getType() == DL.getIndexType(C->getType()))
- return getPtrAdd(C, Idxs[0], NW, InRange, OnlyIfReducedTy);
+ if (Ty->isIntegerTy(DL.getByteWidth()) &&
+ Idxs[0]->getType() == DL.getIndexType(C->getType()))
+ return getPtrAdd(DL, C, Idxs[0], NW, InRange, OnlyIfReducedTy);
// Some API require an ArrayRef of Value * instead of Constant *.
ArrayRef<Value *> ValIdxs =
@@ -2819,7 +2820,17 @@ Constant *ConstantExpr::getGetElementPtr(const DataLayout &DL, Type *Ty,
return nullptr;
}
- return getPtrAdd(C, Offset, NW, InRange, OnlyIfReducedTy);
+ return getPtrAdd(DL, C, Offset, NW, InRange, OnlyIfReducedTy);
+}
+
+Constant *ConstantExpr::getPtrAdd(const DataLayout &DL, Constant *Ptr,
+ Constant *Offset, GEPNoWrapFlags NW,
+ std::optional<ConstantRange> InRange,
+ Type *OnlyIfReduced) {
+ LLVM_SUPPRESS_DEPRECATED_DECLARATIONS_PUSH
+ return getGetElementPtr(Type::getIntNTy(Ptr->getContext(), DL.getByteWidth()),
+ Ptr, Offset, NW, InRange, OnlyIfReduced);
+ LLVM_SUPPRESS_DEPRECATED_DECLARATIONS_POP
}
Constant *ConstantExpr::getExtractElement(Constant *Val, Constant *Idx,
diff --git a/llvm/lib/IR/Core.cpp b/llvm/lib/IR/Core.cpp
index dcd6a9f6c3a33..4b18f13af4550 100644
--- a/llvm/lib/IR/Core.cpp
+++ b/llvm/lib/IR/Core.cpp
@@ -1944,10 +1944,12 @@ LLVMValueRef LLVMConstGEPWithNoWrapFlags(LLVMTypeRef Ty,
LLVM_SUPPRESS_DEPRECATED_DECLARATIONS_POP
}
-LLVMValueRef LLVMConstPtrAdd(LLVMValueRef ConstantVal,
+LLVMValueRef LLVMConstPtrAdd(LLVMTargetDataRef DataLayout,
+ LLVMValueRef ConstantVal,
LLVMValueRef ConstantOffset,
LLVMGEPNoWrapFlags NoWrapFlags) {
- return wrap(ConstantExpr::getPtrAdd(unwrap<Constant>(ConstantVal),
+ return wrap(ConstantExpr::getPtrAdd(*unwrap(DataLayout),
+ unwrap<Constant>(ConstantVal),
unwrap<Constant>(ConstantOffset),
mapFromLLVMGEPNoWrapFlags(NoWrapFlags)));
}
diff --git a/llvm/lib/Target/XCore/XCoreISelLowering.cpp b/llvm/lib/Target/XCore/XCoreISelLowering.cpp
index 8b193cfab7a74..92dd430deefaf 100644
--- a/llvm/lib/Target/XCore/XCoreISelLowering.cpp
+++ b/llvm/lib/Target/XCore/XCoreISelLowering.cpp
@@ -264,7 +264,8 @@ LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const
// Ideally we would not fold in offset with an index <= 11.
Type *Ty = Type::getInt32Ty(*DAG.getContext());
Constant *Idx = ConstantInt::get(Ty, Offset);
- Constant *GAI = ConstantExpr::getPtrAdd(const_cast<GlobalValue *>(GV), Idx);
+ Constant *GAI = ConstantExpr::getPtrAdd(DAG.getDataLayout(),
+ const_cast<GlobalValue *>(GV), Idx);
SDValue CP = DAG.getConstantPool(GAI, MVT::i32);
return DAG.getLoad(getPointerTy(DAG.getDataLayout()), DL,
DAG.getEntryNode(), CP, MachinePointerInfo());
diff --git a/llvm/lib/Transforms/IPO/GlobalSplit.cpp b/llvm/lib/Transforms/IPO/GlobalSplit.cpp
index ccc91cc355787..ec1618eea9bfd 100644
--- a/llvm/lib/Transforms/IPO/GlobalSplit.cpp
+++ b/llvm/lib/Transforms/IPO/GlobalSplit.cpp
@@ -154,7 +154,7 @@ static bool splitGlobal(GlobalVariable &GV) {
for (const GEPInfo &Info : Infos) {
assert(Info.MemberIndex < SplitGlobals.size() && "Invalid member");
auto *NewGEP = ConstantExpr::getPtrAdd(
- SplitGlobals[Info.MemberIndex],
+ DL, SplitGlobals[Info.MemberIndex],
ConstantInt::get(GV.getContext(), Info.MemberRelativeOffset),
Info.GEP->isInBounds());
Info.GEP->replaceAllUsesWith(NewGEP);
diff --git a/llvm/lib/Transforms/IPO/LowerTypeTests.cpp b/llvm/lib/Transforms/IPO/LowerTypeTests.cpp
index ab3b2e51c885b..d9e22ae0bc81f 100644
--- a/llvm/lib/Transforms/IPO/LowerTypeTests.cpp
+++ b/llvm/lib/Transforms/IPO/LowerTypeTests.cpp
@@ -937,7 +937,8 @@ void LowerTypeTestsModule::allocateByteArrays() {
for (unsigned I = 0; I != ByteArrayInfos.size(); ++I) {
ByteArrayInfo *BAI = &ByteArrayInfos[I];
Constant *GEP = ConstantExpr::getInBoundsPtrAdd(
- ByteArray, ConstantInt::get(IntPtrTy, ByteArrayOffsets[I]));
+ M.getDataLayout(), ByteArray,
+ ConstantInt::get(IntPtrTy, ByteArrayOffsets[I]));
// Create an alias instead of RAUW'ing the gep directly. On x86 this ensures
// that the pc-relative displacement is folded into the lea instead of the
@@ -1498,8 +1499,9 @@ void LowerTypeTestsModule::lowerTypeTestCalls(
uint64_t GlobalOffset =
BSI.ByteOffset + ((BSI.BitSize - 1) << BSI.AlignLog2);
- TIL.OffsetedGlobal = ConstantExpr::getPtrAdd(
- CombinedGlobalAddr, ConstantInt::get(IntPtrTy, GlobalOffset)),
+ TIL.OffsetedGlobal =
+ ConstantExpr::getPtrAdd(M.getDataLayout(), CombinedGlobalAddr,
+ ConstantInt::get(IntPtrTy, GlobalOffset)),
TIL.AlignLog2 = ConstantInt::get(IntPtrTy, BSI.AlignLog2);
TIL.SizeM1 = ConstantInt::get(IntPtrTy, BSI.BitSize - 1);
if (BSI.isAllOnes()) {
diff --git a/llvm/lib/Transforms/IPO/WholeProgramDevirt.cpp b/llvm/lib/Transforms/IPO/WholeProgramDevirt.cpp
index 88c57d2195bbd..e66e343ceaa79 100644
--- a/llvm/lib/Transforms/IPO/WholeProgramDevirt.cpp
+++ b/llvm/lib/Transforms/IPO/WholeProgramDevirt.cpp
@@ -1805,7 +1805,7 @@ void DevirtModule::applyUniqueRetValOpt(CallSiteInfo &CSInfo, StringRef FnName,
}
Constant *DevirtModule::getMemberAddr(const TypeMemberInfo *M) {
- return ConstantExpr::getPtrAdd(M->Bits->GV,
+ return ConstantExpr::getPtrAdd(this->M.getDataLayout(), M->Bits->GV,
ConstantInt::get(Int64Ty, M->Offset));
}
diff --git a/llvm/lib/Transforms/Instrumentation/IndirectCallPromotion.cpp b/llvm/lib/Transforms/Instrumentation/IndirectCallPromotion.cpp
index f18287ba9de27..1f4e7b3f1f750 100644
--- a/llvm/lib/Transforms/Instrumentation/IndirectCallPromotion.cpp
+++ b/llvm/lib/Transforms/Instrumentation/IndirectCallPromotion.cpp
@@ -225,7 +225,7 @@ static Constant *getVTableAddressPointOffset(GlobalVariable *VTable,
"Out-of-bound access");
return ConstantExpr::getInBoundsPtrAdd(
- VTable,
+ M.getDataLayout(), VTable,
llvm::ConstantInt::get(Type::getInt32Ty(Context), AddressPointOffset));
}
diff --git a/llvm/lib/Transforms/Instrumentation/SanitizerCoverage.cpp b/llvm/lib/Transforms/Instrumentation/SanitizerCoverage.cpp
index 9e59f60d3d926..0af8ff28b680a 100644
--- a/llvm/lib/Transforms/Instrumentation/SanitizerCoverage.cpp
+++ b/llvm/lib/Transforms/Instrumentation/SanitizerCoverage.cpp
@@ -384,8 +384,9 @@ ModuleSanitizerCoverage::CreateSecStartEnd(Module &M, const char *Section,
// Account for the fact that on windows-msvc __start_* symbols actually
// point to a uint64_t before the start of the array.
- auto *GEP = ConstantExpr::getPtrAdd(
- SecStart, ConstantInt::get(IntptrTy, sizeof(uint64_t)));
+ auto *GEP =
+ ConstantExpr::getPtrAdd(M.getDataLayout(), SecStart,
+ ConstantInt::get(IntptrTy, sizeof(uint64_t)));
return std::make_pair(GEP, SecEnd);
}
diff --git a/llvm/test/Bindings/OCaml/core.ml b/llvm/test/Bindings/OCaml/core.ml
index 9c09320be91bc..d4d90e2805573 100644
--- a/llvm/test/Bindings/OCaml/core.ml
+++ b/llvm/test/Bindings/OCaml/core.ml
@@ -297,8 +297,8 @@ let test_constants () =
ignore (define_global "const_trunc" (const_trunc (const_add foldbomb five)
i8_type) m);
ignore (define_global "const_ptrtoint" (const_ptrtoint
- (const_ptradd (const_null (pointer_type context)) (const_int i32_type 1)
- GEPNoWrapFlags.none)
+ (const_ptradd (data_layout m) (const_null (pointer_type context))
+ (const_int i32_type 1) GEPNoWrapFlags.none)
i32_type) m);
ignore (define_global "const_inttoptr" (const_inttoptr (const_add foldbomb five)
void_ptr) m);
@@ -317,11 +317,13 @@ let test_constants () =
* CHECK: const_shufflevector = global <4 x i32> <i32 0, i32 1, i32 1, i32 0>
*)
ignore (define_global "const_ptradd"
- (const_ptradd foldbomb_gv five GEPNoWrapFlags.none) m);
+ (const_ptradd (data_layout m) foldbomb_gv five GEPNoWrapFlags.none)
+ m);
ignore (define_global "const_ptradd_inbounds"
- (const_ptradd foldbomb_gv five GEPNoWrapFlags.inbounds) m);
+ (const_ptradd (data_layout m) foldbomb_gv five GEPNoWrapFlags.inbounds)
+ m);
ignore (define_global "const_ptradd_inbounds_nuw"
- (const_ptradd foldbomb_gv five
+ (const_ptradd (data_layout m) foldbomb_gv five
(GEPNoWrapFlags.inbounds lor GEPNoWrapFlags.nuw)) m);
ignore (define_global "const_ptradd_from_indices"
(Option.get (const_ptradd_from_indices (data_layout m) i32_type
diff --git a/llvm/unittests/IR/ConstantsTest.cpp b/llvm/unittests/IR/ConstantsTest.cpp
index 920385c4caaab..2f7143d7813c9 100644
--- a/llvm/unittests/IR/ConstantsTest.cpp
+++ b/llvm/unittests/IR/ConstantsTest.cpp
@@ -294,7 +294,7 @@ TEST(ConstantsTest, ReplaceWithConstantTest) {
Constant *Global =
M->getOrInsertGlobal("dummy", PointerType::getUnqual(Context));
- Constant *GEP = ConstantExpr::getPtrAdd(Global, One);
+ Constant *GEP = ConstantExpr::getPtrAdd(M->getDataLayout(), Global, One);
EXPECT_DEATH(Global->replaceAllUsesWith(GEP),
"this->replaceAllUsesWith\\(expr\\(this\\)\\) is NOT valid!");
}
@@ -361,7 +361,7 @@ TEST(ConstantsTest, GEPReplaceWithConstant) {
auto *C1 = ConstantInt::get(IntTy, 1);
auto *Placeholder = new GlobalVariable(
*M, IntTy, false, GlobalValue::ExternalWeakLinkage, nullptr);
- auto *GEP = ConstantExpr::getPtrAdd(Placeholder, C1);
+ auto *GEP = ConstantExpr::getPtrAdd(M->getDataLayout(), Placeholder, C1);
ASSERT_EQ(GEP->getOperand(0), Placeholder);
auto *Ref =
@@ -970,27 +970,27 @@ TEST(ConstantsTest, GetElementPtrDataLayout) {
// No-op.
EXPECT_EQ(ConstantExpr::getGetElementPtr(DL, I8, Ptr, I64_10),
- ConstantExpr::getPtrAdd(Ptr, I64_10));
+ ConstantExpr::getPtrAdd(DL, Ptr, I64_10));
// Index type is canonicalized.
EXPECT_EQ(ConstantExpr::getGetElementPtr(DL, I8, Ptr, I32_10),
- ConstantExpr::getPtrAdd(Ptr, I64_10));
+ ConstantExpr::getPtrAdd(DL, Ptr, I64_10));
EXPECT_EQ(ConstantExpr::getGetElementPtr(DL, I8, Ptr, I128_10),
- ConstantExpr::getPtrAdd(Ptr, I64_10));
+ ConstantExpr::getPtrAdd(DL, Ptr, I64_10));
// Non-i8 base type.
EXPECT_EQ(ConstantExpr::getGetElementPtr(DL, I32, Ptr, I64_10),
- ConstantExpr::getPtrAdd(Ptr, I64_40));
+ ConstantExpr::getPtrAdd(DL, Ptr, I64_40));
// Multiple indices.
EXPECT_EQ(ConstantExpr::getGetElementPtr(DL, A4I32, Ptr, {I64_1, I64_10}),
- ConstantExpr::getPtrAdd(Ptr, ConstantInt::get(I64, 56)));
+ ConstantExpr::getPtrAdd(DL, Ptr, ConstantInt::get(I64, 56)));
// Vector base pointer, scalar index.
EXPECT_EQ(ConstantExpr::getGetElementPtr(DL, I32, PtrVec, I64_10),
- ConstantExpr::getPtrAdd(PtrVec, I64_40));
+ ConstantExpr::getPtrAdd(DL, PtrVec, I64_40));
// Scalar base pointer, vector index
EXPECT_EQ(ConstantExpr::getGetElementPtr(DL, I32, Ptr, V2I64_10),
- ConstantExpr::getPtrAdd(Ptr, V2I64_40));
+ ConstantExpr::getPtrAdd(DL, Ptr, V2I64_40));
// Vector base pointer, vector index.
EXPECT_EQ(ConstantExpr::getGetElementPtr(DL, I32, PtrVec, V2I64_10),
- ConstantExpr::getPtrAdd(PtrVec, V2I64_40));
+ ConstantExpr::getPtrAdd(DL, PtrVec, V2I64_40));
// Can't represent scale * constexpr.
EXPECT_EQ(nullptr, ConstantExpr::getGetElementPtr(DL, I32, Ptr, PtrToInt64));
@@ -1010,9 +1010,10 @@ TEST(ConstantsTest, PtrAddCAPI) {
Constant *PtrToInt = ConstantExpr::getPtrToInt(Ptr, I64);
Constant *I64_1 = ConstantInt::get(I64, 1);
- EXPECT_EQ(
- unwrap(LLVMConstPtrAdd(wrap(Ptr), wrap(I64_1), LLVMGEPFlagNUW)),
- ConstantExpr::getPtrAdd(Ptr, I64_1, GEPNoWrapFlags::noUnsignedWrap()));
+ EXPECT_EQ(unwrap(LLVMConstPtrAdd(wrap(&DL), wrap(Ptr), wrap(I64_1),
+ LLVMGEPFlagNUW)),
+ ConstantExpr::getPtrAdd(DL, Ptr, I64_1,
+ GEPNoWrapFlags::noUnsignedWrap()));
LLVMValueRef Indices[1] = {wrap(I64_1)};
EXPECT_EQ(unwrap(LLVMConstPtrAddFromIndices(wrap(&DL), wrap(I32), wrap(Ptr),
diff --git a/llvm/unittests/IR/PatternMatch.cpp b/llvm/unittests/IR/PatternMatch.cpp
index 7bf92f7761a51..2cad6b38a5e94 100644
--- a/llvm/unittests/IR/PatternMatch.cpp
+++ b/llvm/unittests/IR/PatternMatch.cpp
@@ -2719,7 +2719,8 @@ TEST_F(PatternMatchTest, PtrAdd) {
Constant *Offset = ConstantInt::get(IdxTy, 42);
Value *PtrAdd = IRB.CreatePtrAdd(Null, Offset);
Value *OtherGEP = IRB.CreateGEP(IdxTy, Null, Offset);
- Value *PtrAddConst = ConstantExpr::getPtrAdd(Null, Offset);
+ Value *PtrAddConst =
+ ConstantExpr::getPtrAdd(M->getDataLayout(), Null, Offset);
Value *A, *B;
EXPECT_TRUE(match(PtrAdd, m_PtrAdd(m_Value(A), m_Value(B))));
diff --git a/llvm/unittests/Transforms/Utils/CallPromotionUtilsTest.cpp b/llvm/unittests/Transforms/Utils/CallPromotionUtilsTest.cpp
index c859b0c799f08..7951db904d71d 100644
--- a/llvm/unittests/Transforms/Utils/CallPromotionUtilsTest.cpp
+++ b/llvm/unittests/Transforms/Utils/CallPromotionUtilsTest.cpp
@@ -44,7 +44,7 @@ static Constant *getVTableAddressPointOffset(GlobalVariable *VTable,
"Out-of-bound access");
return ConstantExpr::getInBoundsPtrAdd(
- VTable,
+ M.getDataLayout(), VTable,
llvm::ConstantInt::get(Type::getInt32Ty(Context), AddressPointOffset));
}
More information about the llvm-branch-commits
mailing list