[ConstantFold] Return the correct type when folding a GEP with vector indices.
authorDavide Italiano <davide@freebsd.org>
Fri, 15 Sep 2017 20:53:05 +0000 (20:53 +0000)
committerDavide Italiano <davide@freebsd.org>
Fri, 15 Sep 2017 20:53:05 +0000 (20:53 +0000)
As Eli pointed out (and I got wrong in the first place), langref says: "The
getelementptr returns a vector of pointers, instead of a single address, when one
or more of its arguments is a vector. In such cases, all vector arguments should
have the same number of elements, and every scalar argument will be effectively
broadcast into a vector during address calculation."

Costantfold for gep doesn't really take in account this paragraph, returning a
pointer instead of a vector of pointer which triggers an assertion in RAUW,
as we're trying to replace values with mistmatching types.

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

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@313394 91177308-0d34-0410-b5e6-96231b3b80d8

lib/IR/ConstantFold.cpp
test/Transforms/InstCombine/pr34627.ll [new file with mode: 0644]

index 311b0a76ce8ab507e6ee10c587825d0ebb9c3719..60dd20e4659f07776a4265b79acf5089c2ce41ce 100644 (file)
@@ -2062,9 +2062,20 @@ Constant *llvm::ConstantFoldGetElementPtr(Type *PointeeTy, Constant *C,
       Type *Ty = GetElementPtrInst::getIndexedType(PointeeTy, Idxs);
 
       assert(Ty && "Invalid indices for GEP!");
+      Type *OrigGEPTy = PointerType::get(Ty, PtrTy->getAddressSpace());
       Type *GEPTy = PointerType::get(Ty, PtrTy->getAddressSpace());
       if (VectorType *VT = dyn_cast<VectorType>(C->getType()))
-        GEPTy = VectorType::get(GEPTy, VT->getNumElements());
+        GEPTy = VectorType::get(OrigGEPTy, VT->getNumElements());
+
+      // The GEP returns a vector of pointers when one of more of
+      // its arguments is a vector.
+      for (unsigned i = 0, e = Idxs.size(); i != e; ++i) {
+        if (auto *VT = dyn_cast<VectorType>(Idxs[i]->getType())) {
+          GEPTy = VectorType::get(OrigGEPTy, VT->getNumElements());
+          break;
+        }
+      }
+
       return Constant::getNullValue(GEPTy);
     }
   }
diff --git a/test/Transforms/InstCombine/pr34627.ll b/test/Transforms/InstCombine/pr34627.ll
new file mode 100644 (file)
index 0000000..8935ecf
--- /dev/null
@@ -0,0 +1,11 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; RUN: opt -S -instcombine %s |FileCheck %s
+
+define <2 x i16> @patatino() {
+; CHECK-LABEL: @patatino(
+; CHECK-NEXT:    ret <2 x i16> zeroinitializer
+;
+  %tmp2 = getelementptr inbounds [1 x i16], [1 x i16]* null, i16 0, <2 x i16> undef
+  %tmp3 = ptrtoint <2 x i16*> %tmp2 to <2 x i16>
+  ret <2 x i16> %tmp3
+}