[clang] aef9959 - [IR] Add ConstantExpr::getInBoundsPtrAdd() (#181639)
via cfe-commits
cfe-commits at lists.llvm.org
Mon Feb 16 05:25:49 PST 2026
Author: Nikita Popov
Date: 2026-02-16T14:25:43+01:00
New Revision: aef9959859827f2c30e0c3983253a07dd0b654a6
URL: https://github.com/llvm/llvm-project/commit/aef9959859827f2c30e0c3983253a07dd0b654a6
DIFF: https://github.com/llvm/llvm-project/commit/aef9959859827f2c30e0c3983253a07dd0b654a6.diff
LOG: [IR] Add ConstantExpr::getInBoundsPtrAdd() (#181639)
As a followup to https://github.com/llvm/llvm-project/pull/181365, this
adds the `getInBoundsPtrAdd()` variant and updates code to use it.
Added:
Modified:
clang/lib/CodeGen/ItaniumCXXABI.cpp
llvm/include/llvm/IR/Constants.h
llvm/lib/Transforms/IPO/LowerTypeTests.cpp
llvm/lib/Transforms/Instrumentation/IndirectCallPromotion.cpp
llvm/unittests/Transforms/Utils/CallPromotionUtilsTest.cpp
Removed:
################################################################################
diff --git a/clang/lib/CodeGen/ItaniumCXXABI.cpp b/clang/lib/CodeGen/ItaniumCXXABI.cpp
index 759e512ed0719..c62bc4998c324 100644
--- a/clang/lib/CodeGen/ItaniumCXXABI.cpp
+++ b/clang/lib/CodeGen/ItaniumCXXABI.cpp
@@ -4074,8 +4074,7 @@ 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::getInBoundsGetElementPtr(CGM.Int8Ty, VTable, Eight);
+ VTable = llvm::ConstantExpr::getInBoundsPtrAdd(VTable, Eight);
} else {
llvm::Constant *Two = llvm::ConstantInt::get(PtrDiffTy, 2);
VTable = llvm::ConstantExpr::getInBoundsGetElementPtr(CGM.GlobalsInt8PtrTy,
diff --git a/llvm/include/llvm/IR/Constants.h b/llvm/include/llvm/IR/Constants.h
index b31490f9ca660..a1f667353b26e 100644
--- a/llvm/include/llvm/IR/Constants.h
+++ b/llvm/include/llvm/IR/Constants.h
@@ -1334,6 +1334,11 @@ class ConstantExpr : public Constant {
return getGetElementPtr(Ty, C, IdxList, GEPNoWrapFlags::inBounds());
}
+ /// Create a getelementptr inbounds i8, ptr, offset constant expression.
+ static Constant *getInBoundsPtrAdd(Constant *Ptr, Constant *Offset) {
+ return getPtrAdd(Ptr, Offset, GEPNoWrapFlags::inBounds());
+ }
+
LLVM_ABI static Constant *getExtractElement(Constant *Vec, Constant *Idx,
Type *OnlyIfReducedTy = nullptr);
LLVM_ABI static Constant *getInsertElement(Constant *Vec, Constant *Elt,
diff --git a/llvm/lib/Transforms/IPO/LowerTypeTests.cpp b/llvm/lib/Transforms/IPO/LowerTypeTests.cpp
index c2b39754ba5df..518e91628a478 100644
--- a/llvm/lib/Transforms/IPO/LowerTypeTests.cpp
+++ b/llvm/lib/Transforms/IPO/LowerTypeTests.cpp
@@ -644,11 +644,8 @@ void LowerTypeTestsModule::allocateByteArrays() {
for (unsigned I = 0; I != ByteArrayInfos.size(); ++I) {
ByteArrayInfo *BAI = &ByteArrayInfos[I];
-
- Constant *Idxs[] = {ConstantInt::get(IntPtrTy, 0),
- ConstantInt::get(IntPtrTy, ByteArrayOffsets[I])};
- Constant *GEP = ConstantExpr::getInBoundsGetElementPtr(
- ByteArrayConst->getType(), ByteArray, Idxs);
+ Constant *GEP = ConstantExpr::getInBoundsPtrAdd(
+ 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
diff --git a/llvm/lib/Transforms/Instrumentation/IndirectCallPromotion.cpp b/llvm/lib/Transforms/Instrumentation/IndirectCallPromotion.cpp
index 48131955836b3..f3cdb3518ddcb 100644
--- a/llvm/lib/Transforms/Instrumentation/IndirectCallPromotion.cpp
+++ b/llvm/lib/Transforms/Instrumentation/IndirectCallPromotion.cpp
@@ -216,8 +216,8 @@ static Constant *getVTableAddressPointOffset(GlobalVariable *VTable,
assert(AddressPointOffset < VTable->getGlobalSize(M.getDataLayout()) &&
"Out-of-bound access");
- return ConstantExpr::getInBoundsGetElementPtr(
- Type::getInt8Ty(Context), VTable,
+ return ConstantExpr::getInBoundsPtrAdd(
+ VTable,
llvm::ConstantInt::get(Type::getInt32Ty(Context), AddressPointOffset));
}
diff --git a/llvm/unittests/Transforms/Utils/CallPromotionUtilsTest.cpp b/llvm/unittests/Transforms/Utils/CallPromotionUtilsTest.cpp
index 59c047afd84df..c859b0c799f08 100644
--- a/llvm/unittests/Transforms/Utils/CallPromotionUtilsTest.cpp
+++ b/llvm/unittests/Transforms/Utils/CallPromotionUtilsTest.cpp
@@ -43,8 +43,8 @@ static Constant *getVTableAddressPointOffset(GlobalVariable *VTable,
M.getDataLayout().getTypeAllocSize(VTable->getValueType()) &&
"Out-of-bound access");
- return ConstantExpr::getInBoundsGetElementPtr(
- Type::getInt8Ty(Context), VTable,
+ return ConstantExpr::getInBoundsPtrAdd(
+ VTable,
llvm::ConstantInt::get(Type::getInt32Ty(Context), AddressPointOffset));
}
More information about the cfe-commits
mailing list