[llvm] [IR] Deprecate ConstantExpr::getGetElementPtr() (PR #227601)
Nikita Popov via llvm-commits
llvm-commits at lists.llvm.org
Wed Sep 30 04:22:57 PDT 2026
https://github.com/nikic updated https://github.com/llvm/llvm-project/pull/227601
>From e85051796e089c5a7bd5ac888613be8d14b82572 Mon Sep 17 00:00:00 2001
From: Nikita Popov <npopov at redhat.com>
Date: Wed, 30 Sep 2026 10:08:17 +0200
Subject: [PATCH 1/2] [IR] Deprecate ConstantExpr::getGetElementPtr()
This deprecates various `ConstantExpr::getGetElementPtr()`
overloads, in favor of either `ConstantExpr::getPtrAdd()`, or
the overload taking a `DataLayout` (which converts GEP indices
to ptradd).
Part of the ptradd migration.
---
llvm/include/llvm/IR/Constants.h | 10 ++++++++++
llvm/lib/AsmParser/LLParser.cpp | 2 ++
llvm/lib/Bitcode/Reader/BitcodeReader.cpp | 2 ++
llvm/lib/IR/Constants.cpp | 2 ++
llvm/lib/IR/Core.cpp | 6 ++++++
5 files changed, 22 insertions(+)
diff --git a/llvm/include/llvm/IR/Constants.h b/llvm/include/llvm/IR/Constants.h
index 65fa76c4ae126..db5ef5dfccac8 100644
--- a/llvm/include/llvm/IR/Constants.h
+++ b/llvm/include/llvm/IR/Constants.h
@@ -1465,11 +1465,14 @@ class ConstantExpr : public Constant {
unsigned Flags = 0,
Type *OnlyIfReducedTy = nullptr);
+ LLVM_SUPPRESS_DEPRECATED_DECLARATIONS_PUSH
+
/// Getelementptr form. Value* is only accepted for convenience;
/// all elements must be Constants.
///
/// \param InRange the inrange range if present or std::nullopt.
/// \param OnlyIfReducedTy see \a getWithOperands() docs.
+ [[deprecated("Use getPtrAdd() or the overload accepting DataLayout instead")]]
static Constant *
getGetElementPtr(Type *Ty, Constant *C, ArrayRef<Constant *> IdxList,
GEPNoWrapFlags NW = GEPNoWrapFlags::none(),
@@ -1479,6 +1482,7 @@ class ConstantExpr : public Constant {
Ty, C, ArrayRef((Value *const *)IdxList.data(), IdxList.size()), NW,
InRange, OnlyIfReducedTy);
}
+ [[deprecated("Use getPtrAdd() or the overload accepting DataLayout instead")]]
static Constant *
getGetElementPtr(Type *Ty, Constant *C, Constant *Idx,
GEPNoWrapFlags NW = GEPNoWrapFlags::none(),
@@ -1490,6 +1494,7 @@ class ConstantExpr : public Constant {
return getGetElementPtr(Ty, C, cast<Value>(Idx), NW, InRange,
OnlyIfReducedTy);
}
+ [[deprecated("Use getPtrAdd() or the overload accepting DataLayout instead")]]
LLVM_ABI static Constant *
getGetElementPtr(Type *Ty, Constant *C, ArrayRef<Value *> IdxList,
GEPNoWrapFlags NW = GEPNoWrapFlags::none(),
@@ -1520,10 +1525,12 @@ class ConstantExpr : public Constant {
/// Create an "inbounds" getelementptr. See the documentation for the
/// "inbounds" flag in LangRef.html for details.
+ [[deprecated("Use getPtrAdd() or the overload accepting DataLayout instead")]]
static Constant *getInBoundsGetElementPtr(Type *Ty, Constant *C,
ArrayRef<Constant *> IdxList) {
return getGetElementPtr(Ty, C, IdxList, GEPNoWrapFlags::inBounds());
}
+ [[deprecated("Use getPtrAdd() or the overload accepting DataLayout instead")]]
static Constant *getInBoundsGetElementPtr(Type *Ty, Constant *C,
Constant *Idx) {
// This form of the function only exists to avoid ambiguous overload
@@ -1531,11 +1538,14 @@ class ConstantExpr : public Constant {
// ArrayRef<Value *>.
return getGetElementPtr(Ty, C, Idx, GEPNoWrapFlags::inBounds());
}
+ [[deprecated("Use getPtrAdd() or the overload accepting DataLayout instead")]]
static Constant *getInBoundsGetElementPtr(Type *Ty, Constant *C,
ArrayRef<Value *> IdxList) {
return getGetElementPtr(Ty, C, IdxList, GEPNoWrapFlags::inBounds());
}
+ 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());
diff --git a/llvm/lib/AsmParser/LLParser.cpp b/llvm/lib/AsmParser/LLParser.cpp
index aaee06685fc2a..a793cc00812e3 100644
--- a/llvm/lib/AsmParser/LLParser.cpp
+++ b/llvm/lib/AsmParser/LLParser.cpp
@@ -4910,8 +4910,10 @@ bool LLParser::parseValID(ValID &ID, PerFunctionState *PFS, Type *ExpectedTy) {
if (!GetElementPtrInst::getIndexedType(Ty, Indices))
return error(ID.Loc, "invalid getelementptr indices");
+ LLVM_SUPPRESS_DEPRECATED_DECLARATIONS_PUSH
ID.ConstantVal =
ConstantExpr::getGetElementPtr(Ty, Elts[0], Indices, NW, InRange);
+ LLVM_SUPPRESS_DEPRECATED_DECLARATIONS_POP
} else if (Opc == Instruction::ShuffleVector) {
if (Elts.size() != 3)
return error(ID.Loc, "expected three operands to shufflevector");
diff --git a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp
index 4b338ae6237ec..af4015ea8ac68 100644
--- a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp
+++ b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp
@@ -1765,9 +1765,11 @@ Expected<Value *> BitcodeReader::materializeValue(unsigned StartValID,
break;
}
case Instruction::GetElementPtr:
+ LLVM_SUPPRESS_DEPRECATED_DECLARATIONS_PUSH
C = ConstantExpr::getGetElementPtr(
BC->SrcElemTy, ConstOps[0], ArrayRef(ConstOps).drop_front(),
toGEPNoWrapFlags(BC->Flags), BC->getInRange());
+ LLVM_SUPPRESS_DEPRECATED_DECLARATIONS_POP
break;
case Instruction::ExtractElement:
C = ConstantExpr::getExtractElement(ConstOps[0], ConstOps[1]);
diff --git a/llvm/lib/IR/Constants.cpp b/llvm/lib/IR/Constants.cpp
index 11183f2a4bf89..5c4d2e85e7fc3 100644
--- a/llvm/lib/IR/Constants.cpp
+++ b/llvm/lib/IR/Constants.cpp
@@ -1745,9 +1745,11 @@ Constant *ConstantExpr::getWithOperands(ArrayRef<Constant *> Ops, Type *Ty,
case Instruction::GetElementPtr: {
auto *GEPO = cast<GEPOperator>(this);
assert(SrcTy || (Ops[0]->getType() == getOperand(0)->getType()));
+ LLVM_SUPPRESS_DEPRECATED_DECLARATIONS_PUSH
return ConstantExpr::getGetElementPtr(
SrcTy ? SrcTy : GEPO->getSourceElementType(), Ops[0], Ops.slice(1),
GEPO->getNoWrapFlags(), GEPO->getInRange(), OnlyIfReducedTy);
+ LLVM_SUPPRESS_DEPRECATED_DECLARATIONS_POP
}
default:
assert(getNumOperands() == 2 && "Must be binary operator?");
diff --git a/llvm/lib/IR/Core.cpp b/llvm/lib/IR/Core.cpp
index c8fe01709383c..48422331912b9 100644
--- a/llvm/lib/IR/Core.cpp
+++ b/llvm/lib/IR/Core.cpp
@@ -1911,19 +1911,23 @@ LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
LLVMValueRef LLVMConstGEP2(LLVMTypeRef Ty, LLVMValueRef ConstantVal,
LLVMValueRef *ConstantIndices, unsigned NumIndices) {
+ LLVM_SUPPRESS_DEPRECATED_DECLARATIONS_PUSH
ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
NumIndices);
Constant *Val = unwrap<Constant>(ConstantVal);
return wrap(ConstantExpr::getGetElementPtr(unwrap(Ty), Val, IdxList));
+ LLVM_SUPPRESS_DEPRECATED_DECLARATIONS_POP
}
LLVMValueRef LLVMConstInBoundsGEP2(LLVMTypeRef Ty, LLVMValueRef ConstantVal,
LLVMValueRef *ConstantIndices,
unsigned NumIndices) {
+ LLVM_SUPPRESS_DEPRECATED_DECLARATIONS_PUSH
ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
NumIndices);
Constant *Val = unwrap<Constant>(ConstantVal);
return wrap(ConstantExpr::getInBoundsGetElementPtr(unwrap(Ty), Val, IdxList));
+ LLVM_SUPPRESS_DEPRECATED_DECLARATIONS_POP
}
LLVMValueRef LLVMConstGEPWithNoWrapFlags(LLVMTypeRef Ty,
@@ -1931,11 +1935,13 @@ LLVMValueRef LLVMConstGEPWithNoWrapFlags(LLVMTypeRef Ty,
LLVMValueRef *ConstantIndices,
unsigned NumIndices,
LLVMGEPNoWrapFlags NoWrapFlags) {
+ LLVM_SUPPRESS_DEPRECATED_DECLARATIONS_PUSH
ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
NumIndices);
Constant *Val = unwrap<Constant>(ConstantVal);
return wrap(ConstantExpr::getGetElementPtr(
unwrap(Ty), Val, IdxList, mapFromLLVMGEPNoWrapFlags(NoWrapFlags)));
+ LLVM_SUPPRESS_DEPRECATED_DECLARATIONS_POP
}
LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
>From 49439eb25ed28b6cedb9a641bd7d1deeebbbfef1 Mon Sep 17 00:00:00 2001
From: Nikita Popov <npopov at redhat.com>
Date: Wed, 30 Sep 2026 11:32:36 +0200
Subject: [PATCH 2/2] Fix unit test
---
llvm/unittests/IR/ConstantsTest.cpp | 7 ++++---
1 file changed, 4 insertions(+), 3 deletions(-)
diff --git a/llvm/unittests/IR/ConstantsTest.cpp b/llvm/unittests/IR/ConstantsTest.cpp
index b0cab0027be4f..0f1cefb234a79 100644
--- a/llvm/unittests/IR/ConstantsTest.cpp
+++ b/llvm/unittests/IR/ConstantsTest.cpp
@@ -263,9 +263,11 @@ TEST(ConstantsTest, AsInstructionsTest) {
// not a normal one!
// CHECK(ConstantExpr::getGetElementPtr(Global, V, false),
// "getelementptr ptr, ptr @dummy, i32 1");
+ LLVM_SUPPRESS_DEPRECATED_DECLARATIONS_PUSH
CHECK(ConstantExpr::getInBoundsGetElementPtr(PointerType::getUnqual(Context),
Global, V),
"getelementptr inbounds ptr, ptr @dummy, i32 1");
+ LLVM_SUPPRESS_DEPRECATED_DECLARATIONS_POP
CHECK(ConstantExpr::getExtractElement(P6, One),
"extractelement <2 x i16> " P6STR ", i32 1");
@@ -292,8 +294,7 @@ TEST(ConstantsTest, ReplaceWithConstantTest) {
Constant *Global =
M->getOrInsertGlobal("dummy", PointerType::getUnqual(Context));
- Constant *GEP = ConstantExpr::getGetElementPtr(
- PointerType::getUnqual(Context), Global, One);
+ Constant *GEP = ConstantExpr::getPtrAdd(Global, One);
EXPECT_DEATH(Global->replaceAllUsesWith(GEP),
"this->replaceAllUsesWith\\(expr\\(this\\)\\) is NOT valid!");
}
@@ -360,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::getGetElementPtr(IntTy, Placeholder, C1);
+ auto *GEP = ConstantExpr::getPtrAdd(Placeholder, C1);
ASSERT_EQ(GEP->getOperand(0), Placeholder);
auto *Ref =
More information about the llvm-commits
mailing list