[IR] Don't accept nullptr as GEP element type
authorNikita Popov <nikita.ppv@gmail.com>
Thu, 8 Jul 2021 18:56:05 +0000 (20:56 +0200)
committerNikita Popov <nikita.ppv@gmail.com>
Fri, 9 Jul 2021 15:37:43 +0000 (17:37 +0200)
GetElementPtrInst::Create() (and IRBuilder methods based on it)
currently accept nullptr as the element type, and will fetch the
element type from the pointer in that case. Remove this fallback,
as it is incompatible with opaque pointers. I've removed a handful
of leftover calls using this behavior as a preliminary step.

Out-of-tree code affected by this change should either pass a proper
type, or can temporarily explicitly call getPointerElementType(),
if the newly added assertion is encountered.

Differential Revision: https://reviews.llvm.org/D105653

llvm/include/llvm/IR/Instructions.h

index a5cebf0..0c43a56 100644 (file)
@@ -956,13 +956,9 @@ public:
                                    const Twine &NameStr = "",
                                    Instruction *InsertBefore = nullptr) {
     unsigned Values = 1 + unsigned(IdxList.size());
-    if (!PointeeType) {
-      PointeeType =
-          cast<PointerType>(Ptr->getType()->getScalarType())->getElementType();
-    } else {
-      assert(cast<PointerType>(Ptr->getType()->getScalarType())
-                 ->isOpaqueOrPointeeTypeMatches(PointeeType));
-    }
+    assert(PointeeType && "Must specify element type");
+    assert(cast<PointerType>(Ptr->getType()->getScalarType())
+               ->isOpaqueOrPointeeTypeMatches(PointeeType));
     return new (Values) GetElementPtrInst(PointeeType, Ptr, IdxList, Values,
                                           NameStr, InsertBefore);
   }
@@ -972,13 +968,9 @@ public:
                                    const Twine &NameStr,
                                    BasicBlock *InsertAtEnd) {
     unsigned Values = 1 + unsigned(IdxList.size());
-    if (!PointeeType) {
-      PointeeType =
-          cast<PointerType>(Ptr->getType()->getScalarType())->getElementType();
-    } else {
-      assert(cast<PointerType>(Ptr->getType()->getScalarType())
-                 ->isOpaqueOrPointeeTypeMatches(PointeeType));
-    }
+    assert(PointeeType && "Must specify element type");
+    assert(cast<PointerType>(Ptr->getType()->getScalarType())
+               ->isOpaqueOrPointeeTypeMatches(PointeeType));
     return new (Values) GetElementPtrInst(PointeeType, Ptr, IdxList, Values,
                                           NameStr, InsertAtEnd);
   }