From 02c21060029cff4fdfd9eab3011a1fec6de2f9a0 Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Fri, 11 Mar 2022 10:55:48 +0100 Subject: [PATCH] [InstSimplify] Handle vector GEP when simplifying zero indices If the base is a scalar and the index is a vector, we can't simplify, as this is effectively a splat operation. --- llvm/lib/Analysis/InstructionSimplify.cpp | 3 ++- .../Transforms/InstSimplify/opaque-ptr.ll | 25 +++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp index 78850e4627c2..77d600ac5883 100644 --- a/llvm/lib/Analysis/InstructionSimplify.cpp +++ b/llvm/lib/Analysis/InstructionSimplify.cpp @@ -4499,7 +4499,8 @@ static Value *SimplifyGEPInst(Type *SrcTy, Value *Ptr, // For opaque pointers an all-zero GEP is a no-op. For typed pointers, // it may be equivalent to a bitcast. - if (Ptr->getType()->isOpaquePointerTy() && + if (Ptr->getType()->getScalarType()->isOpaquePointerTy() && + Ptr->getType() == GEPTy && all_of(Indices, [](const auto *V) { return match(V, m_Zero()); })) return Ptr; diff --git a/llvm/test/Transforms/InstSimplify/opaque-ptr.ll b/llvm/test/Transforms/InstSimplify/opaque-ptr.ll index 902b7c9392d5..48e75ac186ec 100644 --- a/llvm/test/Transforms/InstSimplify/opaque-ptr.ll +++ b/llvm/test/Transforms/InstSimplify/opaque-ptr.ll @@ -26,3 +26,28 @@ define ptr @gep_non_zero_indices2(ptr %p) { %p2 = getelementptr { i64, i64 }, ptr %p, i64 1, i32 0 ret ptr %p2 } + +define <2 x ptr> @scalar_base_vector_index(ptr %p) { +; CHECK-LABEL: @scalar_base_vector_index( +; CHECK-NEXT: [[G:%.*]] = getelementptr { i64, i64 }, ptr [[P:%.*]], <2 x i64> zeroinitializer, i32 0 +; CHECK-NEXT: ret <2 x ptr> [[G]] +; + %g = getelementptr { i64, i64 }, ptr %p, <2 x i64> zeroinitializer, i32 0 + ret <2 x ptr> %g +} + +define <2 x ptr> @vector_base_vector_index(<2 x ptr> %p) { +; CHECK-LABEL: @vector_base_vector_index( +; CHECK-NEXT: ret <2 x ptr> [[P:%.*]] +; + %g = getelementptr { i64, i64 }, <2 x ptr> %p, <2 x i64> zeroinitializer, i32 0 + ret <2 x ptr> %g +} + +define <2 x ptr> @vector_base_scalar_index(<2 x ptr> %p) { +; CHECK-LABEL: @vector_base_scalar_index( +; CHECK-NEXT: ret <2 x ptr> [[P:%.*]] +; + %g = getelementptr { i64, i64 }, <2 x ptr> %p, i64 0, i32 0 + ret <2 x ptr> %g +}