InstructionSimplify: 'extractelement' with an undef index is undef
Summary:
An undef extract index can be arbitrarily chosen to be an
out-of-range index value, which would result in the instruction being undef.
This change closes a gap identified while working on lowering vector permute intrinsics
with variable index vectors to pure LLVM IR.
Reviewers: arsenm, spatel, majnemer
Reviewed By: arsenm, spatel
Subscribers: fhahn, nhaehnle, wdng, llvm-commits
Differential Revision: https://reviews.llvm.org/D40231
llvm-svn: 319910
diff --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp
index 622b1f2..09f516a 100644
--- a/llvm/lib/Analysis/InstructionSimplify.cpp
+++ b/llvm/lib/Analysis/InstructionSimplify.cpp
@@ -3901,6 +3901,11 @@
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<UndefValue>(Idx))
+ return UndefValue::get(Vec->getType()->getVectorElementType());
+
return nullptr;
}