[InstCombine] Refinement of r299915. Only consider a ConstantVector for Neg if all...
authorCraig Topper <craig.topper@gmail.com>
Tue, 11 Apr 2017 06:32:48 +0000 (06:32 +0000)
committerCraig Topper <craig.topper@gmail.com>
Tue, 11 Apr 2017 06:32:48 +0000 (06:32 +0000)
llvm-svn: 299917

llvm/lib/Transforms/InstCombine/InstructionCombining.cpp

index 3c6f07e..4782f47 100644 (file)
@@ -726,9 +726,20 @@ Value *InstCombiner::dyn_castNegVal(Value *V) const {
     if (C->getType()->getElementType()->isIntegerTy())
       return ConstantExpr::getNeg(C);
 
-  if (ConstantVector *C = dyn_cast<ConstantVector>(V))
-    if (C->getType()->getElementType()->isIntegerTy())
-      return ConstantExpr::getNeg(C);
+  if (ConstantVector *CV = dyn_cast<ConstantVector>(V)) {
+    for (unsigned i = 0, e = CV->getNumOperands(); i != e; ++i) {
+      Constant *Elt = CV->getAggregateElement(i);
+      if (!Elt)
+        return nullptr;
+
+      if (isa<UndefValue>(Elt))
+        continue;
+
+      if (!isa<ConstantInt>(Elt))
+        return nullptr;
+    }
+    return ConstantExpr::getNeg(CV);
+  }
 
   return nullptr;
 }