[InstSimplify] Don't assume getAggregateElement will succeed
authorDavid Majnemer <david.majnemer@gmail.com>
Tue, 18 Aug 2015 22:07:25 +0000 (22:07 +0000)
committerDavid Majnemer <david.majnemer@gmail.com>
Tue, 18 Aug 2015 22:07:25 +0000 (22:07 +0000)
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
lib/Analysis/VectorUtils.cpp
test/Transforms/InstSimplify/2011-09-05-InsertExtractValue.ll

index f9d4524f01238020abfebfbef4191100caaa7193..db8639613fbee779e27e359ddcead9d7d9ba245d 100644 (file)
@@ -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;
   }
index 9a0af3bd8be04e49e165925484d21e487acc4684..72140952ecb37d3c1b928ac91033f9f5451bdd37 100644 (file)
@@ -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;
index 7e391aba30456b2204a85252b9f0cff0a028214c..441bc1adca7e36b75739a2ced4aab69d6dda31a4 100644 (file)
@@ -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> <double 0x319BEB8FD172E36> to <8 x i8>)
+; CHECK-NEXT: %[[extract:.*]] = extractelement <8 x i8> %[[add]], i32 6
+; CHECK-NEXT: ret i8 %[[extract]]
+}