[clang] [Clang][CodeGen] Do not set inbounds flag for struct GEP with null base pointers (PR #130734)

Nikita Popov via cfe-commits cfe-commits at lists.llvm.org
Thu Mar 20 03:15:10 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");
+      ElementTy = GEP->getResultElementType();
+    } else {
+      ElementTy = llvm::GetElementPtrInst::getIndexedType(Addr.getElementType(),
+                                                          {Idx0, Idx1});
+    }
+    return Address(V, ElementTy,
----------------
nikic wrote:

Are the changes to this function still needed?

https://github.com/llvm/llvm-project/pull/130734


More information about the cfe-commits mailing list