From d00bdb627b38ad63ad00bcfbd7dee28d704bfc3a Mon Sep 17 00:00:00 2001 From: David Majnemer Date: Tue, 18 Aug 2015 22:07:25 +0000 Subject: [PATCH] [InstSimplify] Don't assume getAggregateElement will succeed It isn't always possible to get a value from getAggregateElement. This fixes PR24488. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@245365 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Analysis/InstructionSimplify.cpp | 5 ----- lib/Analysis/VectorUtils.cpp | 8 ++++---- .../InstSimplify/2011-09-05-InsertExtractValue.ll | 10 ++++++++++ 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/lib/Analysis/InstructionSimplify.cpp b/lib/Analysis/InstructionSimplify.cpp index f9d4524f012..db8639613fb 100644 --- a/lib/Analysis/InstructionSimplify.cpp +++ b/lib/Analysis/InstructionSimplify.cpp @@ -3578,11 +3578,6 @@ static Value *SimplifyExtractElementInst(Value *Vec, Value *Idx, const Query &, unsigned IndexVal = IdxC->getZExtValue(); unsigned VectorWidth = Vec->getType()->getVectorNumElements(); - // If this is extracting an invalid index, turn this into undef, to avoid - // crashing the code below. - if (IndexVal >= VectorWidth) - return UndefValue::get(Vec->getType()->getVectorElementType()); - if (Value *Elt = findScalarElement(Vec, IndexVal)) return Elt; } diff --git a/lib/Analysis/VectorUtils.cpp b/lib/Analysis/VectorUtils.cpp index 9a0af3bd8be..72140952ecb 100644 --- a/lib/Analysis/VectorUtils.cpp +++ b/lib/Analysis/VectorUtils.cpp @@ -398,10 +398,10 @@ Value *llvm::findScalarElement(Value *V, unsigned EltNo) { // Extract a value from a vector add operation with a constant zero. Value *Val = nullptr; Constant *Con = nullptr; - if (match(V, m_Add(m_Value(Val), m_Constant(Con)))) { - if (Con->getAggregateElement(EltNo)->isNullValue()) - return findScalarElement(Val, EltNo); - } + if (match(V, m_Add(m_Value(Val), m_Constant(Con)))) + if (Constant *Elt = Con->getAggregateElement(EltNo)) + if (Elt->isNullValue()) + return findScalarElement(Val, EltNo); // Otherwise, we don't know. return nullptr; diff --git a/test/Transforms/InstSimplify/2011-09-05-InsertExtractValue.ll b/test/Transforms/InstSimplify/2011-09-05-InsertExtractValue.ll index 7e391aba304..441bc1adca7 100644 --- a/test/Transforms/InstSimplify/2011-09-05-InsertExtractValue.ll +++ b/test/Transforms/InstSimplify/2011-09-05-InsertExtractValue.ll @@ -36,3 +36,13 @@ define i32 @test3(i32 %a, float %b) { ; CHECK-LABEL: @test3( ; CHECK: ret i32 %a } + +define i8 @test4(<8 x i8> %V) { + %add = add <8 x i8> %V, bitcast (double 0x319BEB8FD172E36 to <8 x i8>) + %extract = extractelement <8 x i8> %add, i32 6 + ret i8 %extract +; CHECK-LABEL: @test4( +; CHECK: %[[add:.*]] = add <8 x i8> %V, bitcast (<1 x double> to <8 x i8>) +; CHECK-NEXT: %[[extract:.*]] = extractelement <8 x i8> %[[add]], i32 6 +; CHECK-NEXT: ret i8 %[[extract]] +} -- 2.34.1