diff --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp index 622b1f25df62..09f516a8cb9b 100644 --- a/llvm/lib/Analysis/InstructionSimplify.cpp +++ b/llvm/lib/Analysis/InstructionSimplify.cpp @@ -3901,6 +3901,11 @@ static Value *SimplifyExtractElementInst(Value *Vec, Value *Idx, const SimplifyQ if (Value *Elt = findScalarElement(Vec, IdxC->getZExtValue())) return Elt; + // An undef extract index can be arbitrarily chosen to be an out-of-range + // index value, which would result in the instruction being undef. + if (isa(Idx)) + return UndefValue::get(Vec->getType()->getVectorElementType()); + return nullptr; } diff --git a/llvm/test/Transforms/InstSimplify/2011-09-05-InsertExtractValue.ll b/llvm/test/Transforms/InstSimplify/2011-09-05-InsertExtractValue.ll index 441bc1adca7e..2c35ed7f3909 100644 --- a/llvm/test/Transforms/InstSimplify/2011-09-05-InsertExtractValue.ll +++ b/llvm/test/Transforms/InstSimplify/2011-09-05-InsertExtractValue.ll @@ -46,3 +46,10 @@ define i8 @test4(<8 x i8> %V) { ; CHECK-NEXT: %[[extract:.*]] = extractelement <8 x i8> %[[add]], i32 6 ; CHECK-NEXT: ret i8 %[[extract]] } + +define i32 @test5(<4 x i32> %V) { + %extract = extractelement <4 x i32> %V, i32 undef + ret i32 %extract +} +; CHECK-LABEL: @test5( +; CHECK: ret i32 undef