[clang] [Clang][CodeGen] Do not set inbounds flag for struct GEP with null base pointers (PR #130734)
Yingwei Zheng via cfe-commits
cfe-commits at lists.llvm.org
Mon Mar 31 02:04:33 PDT 2025
================
@@ -64,21 +64,27 @@ class CGBuilderTy : public CGBuilderBaseTy {
Address createConstGEP2_32(Address Addr, unsigned Idx0, unsigned Idx1,
const llvm::Twine &Name) {
const llvm::DataLayout &DL = BB->getDataLayout();
- llvm::GetElementPtrInst *GEP;
+ llvm::Value *V;
if (IsInBounds)
- GEP = cast<llvm::GetElementPtrInst>(CreateConstInBoundsGEP2_32(
- Addr.getElementType(), emitRawPointerFromAddress(Addr), Idx0, Idx1,
- Name));
+ V = CreateConstInBoundsGEP2_32(Addr.getElementType(),
+ emitRawPointerFromAddress(Addr), Idx0,
+ Idx1, Name);
else
- GEP = cast<llvm::GetElementPtrInst>(CreateConstGEP2_32(
- Addr.getElementType(), emitRawPointerFromAddress(Addr), Idx0, Idx1,
- Name));
+ V = CreateConstGEP2_32(Addr.getElementType(),
+ emitRawPointerFromAddress(Addr), Idx0, Idx1, Name);
llvm::APInt Offset(
DL.getIndexSizeInBits(Addr.getType()->getPointerAddressSpace()), 0,
/*isSigned=*/true);
- if (!GEP->accumulateConstantOffset(DL, Offset))
- llvm_unreachable("offset of GEP with constants is always computable");
- return Address(GEP, GEP->getResultElementType(),
+ llvm::Type *ElementTy = nullptr;
+ if (auto *GEP = dyn_cast<llvm::GEPOperator>(V)) {
+ if (!GEP->accumulateConstantOffset(DL, Offset))
+ llvm_unreachable("offset of GEP with constants is always computable");
----------------
dtcxzyw wrote:
Fixed.
https://github.com/llvm/llvm-project/pull/130734
More information about the cfe-commits
mailing list