[clang] [llvm] [IR] Add ConstantExpr::getPtrAdd() (PR #181365)
via cfe-commits
cfe-commits at lists.llvm.org
Fri Feb 13 06:30:45 PST 2026
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-llvm-ir
Author: Nikita Popov (nikic)
<details>
<summary>Changes</summary>
Add a ConstantExpr::getPtrAdd() API that creates a getelementptr i8 constant expression, similar to IRBuilder::CreatePtrAdd(). In the future this will create a ptradd expression.
---
Full diff: https://github.com/llvm/llvm-project/pull/181365.diff
9 Files Affected:
- (modified) clang/lib/CodeGen/CGExprConstant.cpp (+1-1)
- (modified) clang/lib/CodeGen/CGObjCMac.cpp (+1-1)
- (modified) llvm/include/llvm/IR/Constants.h (+10)
- (modified) llvm/lib/Analysis/ConstantFolding.cpp (+2-3)
- (modified) llvm/lib/Analysis/ScalarEvolution.cpp (+1-2)
- (modified) llvm/lib/Transforms/IPO/GlobalSplit.cpp (+2-2)
- (modified) llvm/lib/Transforms/IPO/LowerTypeTests.cpp (+2-2)
- (modified) llvm/lib/Transforms/IPO/WholeProgramDevirt.cpp (+2-2)
- (modified) llvm/unittests/IR/PatternMatch.cpp (+1-2)
``````````diff
diff --git a/clang/lib/CodeGen/CGExprConstant.cpp b/clang/lib/CodeGen/CGExprConstant.cpp
index c316642a87baf..5ecb6fd789a1a 100644
--- a/clang/lib/CodeGen/CGExprConstant.cpp
+++ b/clang/lib/CodeGen/CGExprConstant.cpp
@@ -2121,7 +2121,7 @@ class ConstantLValueEmitter : public ConstStmtVisitor<ConstantLValueEmitter,
if (!hasNonZeroOffset())
return C;
- return llvm::ConstantExpr::getGetElementPtr(CGM.Int8Ty, C, getOffset());
+ return llvm::ConstantExpr::getPtrAdd(C, getOffset());
}
};
diff --git a/clang/lib/CodeGen/CGObjCMac.cpp b/clang/lib/CodeGen/CGObjCMac.cpp
index 4dadf5113a8e3..1193de379bc9d 100644
--- a/clang/lib/CodeGen/CGObjCMac.cpp
+++ b/clang/lib/CodeGen/CGObjCMac.cpp
@@ -7369,7 +7369,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::getGetElementPtr(CGM.Int8Ty, ClassGV, Idx);
+ return llvm::ConstantExpr::getPtrAdd(ClassGV, Idx);
}
llvm::Value *
diff --git a/llvm/include/llvm/IR/Constants.h b/llvm/include/llvm/IR/Constants.h
index f35b249e9686d..b31490f9ca660 100644
--- a/llvm/include/llvm/IR/Constants.h
+++ b/llvm/include/llvm/IR/Constants.h
@@ -1306,6 +1306,16 @@ class ConstantExpr : public Constant {
std::optional<ConstantRange> InRange = std::nullopt,
Type *OnlyIfReducedTy = nullptr);
+ /// Create a getelementptr i8, ptr, offset constant expression.
+ static Constant *
+ getPtrAdd(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);
+ }
+
/// Create an "inbounds" getelementptr. See the documentation for the
/// "inbounds" flag in LangRef.html for details.
static Constant *getInBoundsGetElementPtr(Type *Ty, Constant *C,
diff --git a/llvm/lib/Analysis/ConstantFolding.cpp b/llvm/lib/Analysis/ConstantFolding.cpp
index ab060b1b9320a..f5db33f8e63df 100644
--- a/llvm/lib/Analysis/ConstantFolding.cpp
+++ b/llvm/lib/Analysis/ConstantFolding.cpp
@@ -985,9 +985,8 @@ Constant *SymbolicallyEvaluateGEP(const GEPOperator *GEP,
// Otherwise canonicalize this to a single ptradd.
LLVMContext &Ctx = Ptr->getContext();
- return ConstantExpr::getGetElementPtr(Type::getInt8Ty(Ctx), Ptr,
- ConstantInt::get(Ctx, Offset), NW,
- InRange);
+ return ConstantExpr::getPtrAdd(Ptr, ConstantInt::get(Ctx, Offset), NW,
+ InRange);
}
/// Attempt to constant fold an instruction with the
diff --git a/llvm/lib/Analysis/ScalarEvolution.cpp b/llvm/lib/Analysis/ScalarEvolution.cpp
index ae9ce311ec08f..a4d1dbd42a8f3 100644
--- a/llvm/lib/Analysis/ScalarEvolution.cpp
+++ b/llvm/lib/Analysis/ScalarEvolution.cpp
@@ -10032,8 +10032,7 @@ static Constant *BuildConstantFromSCEV(const SCEV *V) {
if (OpC->getType()->isPointerTy()) {
// The offsets have been converted to bytes. We can add bytes using
// an i8 GEP.
- C = ConstantExpr::getGetElementPtr(Type::getInt8Ty(C->getContext()),
- OpC, C);
+ C = ConstantExpr::getPtrAdd(OpC, C);
} else {
C = ConstantExpr::getAdd(C, OpC);
}
diff --git a/llvm/lib/Transforms/IPO/GlobalSplit.cpp b/llvm/lib/Transforms/IPO/GlobalSplit.cpp
index 746da9f238145..ccc91cc355787 100644
--- a/llvm/lib/Transforms/IPO/GlobalSplit.cpp
+++ b/llvm/lib/Transforms/IPO/GlobalSplit.cpp
@@ -153,8 +153,8 @@ static bool splitGlobal(GlobalVariable &GV) {
for (const GEPInfo &Info : Infos) {
assert(Info.MemberIndex < SplitGlobals.size() && "Invalid member");
- auto *NewGEP = ConstantExpr::getGetElementPtr(
- Type::getInt8Ty(GV.getContext()), SplitGlobals[Info.MemberIndex],
+ auto *NewGEP = ConstantExpr::getPtrAdd(
+ 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 c84bdaa1b0f86..c77ea2e28540b 100644
--- a/llvm/lib/Transforms/IPO/LowerTypeTests.cpp
+++ b/llvm/lib/Transforms/IPO/LowerTypeTests.cpp
@@ -1210,8 +1210,8 @@ void LowerTypeTestsModule::lowerTypeTestCalls(
uint64_t GlobalOffset =
BSI.ByteOffset + ((BSI.BitSize - 1) << BSI.AlignLog2);
- TIL.OffsetedGlobal = ConstantExpr::getGetElementPtr(
- Int8Ty, CombinedGlobalAddr, ConstantInt::get(IntPtrTy, GlobalOffset)),
+ TIL.OffsetedGlobal = ConstantExpr::getPtrAdd(
+ 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 950b429edacb7..8c2420a45a24c 100644
--- a/llvm/lib/Transforms/IPO/WholeProgramDevirt.cpp
+++ b/llvm/lib/Transforms/IPO/WholeProgramDevirt.cpp
@@ -1803,8 +1803,8 @@ void DevirtModule::applyUniqueRetValOpt(CallSiteInfo &CSInfo, StringRef FnName,
}
Constant *DevirtModule::getMemberAddr(const TypeMemberInfo *M) {
- return ConstantExpr::getGetElementPtr(Int8Ty, M->Bits->GV,
- ConstantInt::get(Int64Ty, M->Offset));
+ return ConstantExpr::getPtrAdd(M->Bits->GV,
+ ConstantInt::get(Int64Ty, M->Offset));
}
bool DevirtModule::tryUniqueRetValOpt(
diff --git a/llvm/unittests/IR/PatternMatch.cpp b/llvm/unittests/IR/PatternMatch.cpp
index 8831de500efb3..220e35d37c0be 100644
--- a/llvm/unittests/IR/PatternMatch.cpp
+++ b/llvm/unittests/IR/PatternMatch.cpp
@@ -2606,8 +2606,7 @@ 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::getGetElementPtr(Type::getInt8Ty(Ctx), Null, Offset);
+ Value *PtrAddConst = ConstantExpr::getPtrAdd(Null, Offset);
Value *A, *B;
EXPECT_TRUE(match(PtrAdd, m_PtrAdd(m_Value(A), m_Value(B))));
``````````
</details>
https://github.com/llvm/llvm-project/pull/181365
More information about the cfe-commits
mailing list