https://github.com/nikic created https://github.com/llvm/llvm-project/pull/181639
As a followup to https://github.com/llvm/llvm-project/pull/181365, this adds the `getInBoundsPtrAdd()` variant and updates code to use it. >From 3226add90b7dc8e0f5dce79130767036c667b9ae Mon Sep 17 00:00:00 2001 From: Nikita Popov <[email protected]> Date: Mon, 16 Feb 2026 12:50:10 +0100 Subject: [PATCH] [IR] Add ConstantExpr::getInBoundsPtrAdd() --- clang/lib/CodeGen/ItaniumCXXABI.cpp | 3 +-- llvm/include/llvm/IR/Constants.h | 5 +++++ llvm/lib/Transforms/IPO/LowerTypeTests.cpp | 7 ++----- .../Transforms/Instrumentation/IndirectCallPromotion.cpp | 4 ++-- llvm/unittests/Transforms/Utils/CallPromotionUtilsTest.cpp | 4 ++-- 5 files changed, 12 insertions(+), 11 deletions(-) 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)); } _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
