[llvm] 22f9159 - [BitcodeReader] Support GEP without indices

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Thu Mar 10 06:48:56 PST 2022


Author: Nikita Popov
Date: 2022-03-10T15:48:47+01:00
New Revision: 22f9159bed3eb0694682590b68b879df64f010c4

URL: https://github.com/llvm/llvm-project/commit/22f9159bed3eb0694682590b68b879df64f010c4
DIFF: https://github.com/llvm/llvm-project/commit/22f9159bed3eb0694682590b68b879df64f010c4.diff

LOG: [BitcodeReader] Support GEP without indices

LLVM considers these to be legal, so make sure the bitcode reader
can read them. I broke this when implementing opaque pointer
auto upgrade support.

Added: 
    llvm/test/Bitcode/getelementptr-zero-indices.ll

Modified: 
    llvm/lib/Bitcode/Reader/BitcodeReader.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp
index edeacf4955c3f..214eb159007fe 100644
--- a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp
+++ b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp
@@ -4456,18 +4456,20 @@ Error BitcodeReader::parseFunctionBody(Function *F) {
       I = GetElementPtrInst::Create(Ty, BasePtr, GEPIdx);
 
       ResTypeID = TyID;
-      auto GTI = std::next(gep_type_begin(I));
-      for (Value *Idx : drop_begin(cast<GEPOperator>(I)->indices())) {
-        unsigned SubType = 0;
-        if (GTI.isStruct()) {
-          ConstantInt *IdxC =
-              Idx->getType()->isVectorTy()
-                  ? cast<ConstantInt>(cast<Constant>(Idx)->getSplatValue())
-                  : cast<ConstantInt>(Idx);
-          SubType = IdxC->getZExtValue();
+      if (cast<GEPOperator>(I)->getNumIndices() != 0) {
+        auto GTI = std::next(gep_type_begin(I));
+        for (Value *Idx : drop_begin(cast<GEPOperator>(I)->indices())) {
+          unsigned SubType = 0;
+          if (GTI.isStruct()) {
+            ConstantInt *IdxC =
+                Idx->getType()->isVectorTy()
+                    ? cast<ConstantInt>(cast<Constant>(Idx)->getSplatValue())
+                    : cast<ConstantInt>(Idx);
+            SubType = IdxC->getZExtValue();
+          }
+          ResTypeID = getContainedTypeID(ResTypeID, SubType);
+          ++GTI;
         }
-        ResTypeID = getContainedTypeID(ResTypeID, SubType);
-        ++GTI;
       }
 
       // At this point ResTypeID is the result element type. We need a pointer

diff  --git a/llvm/test/Bitcode/getelementptr-zero-indices.ll b/llvm/test/Bitcode/getelementptr-zero-indices.ll
new file mode 100644
index 0000000000000..13b80aa8ee300
--- /dev/null
+++ b/llvm/test/Bitcode/getelementptr-zero-indices.ll
@@ -0,0 +1,8 @@
+; RUN: llvm-as < %s | llvm-dis | FileCheck %s
+
+; CHECK: %g = getelementptr i8, i8* %p
+
+define i8* @ptr(i8* %p) {
+  %g = getelementptr i8, i8* %p
+  ret i8* %p
+}


        


More information about the llvm-commits mailing list