Implement X + X for vectors.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@51472 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Transforms/Scalar/InstructionCombining.cpp b/lib/Transforms/Scalar/InstructionCombining.cpp
index a398884..1a87868 100644
--- a/lib/Transforms/Scalar/InstructionCombining.cpp
+++ b/lib/Transforms/Scalar/InstructionCombining.cpp
@@ -2292,14 +2292,22 @@
 
 namespace {
 
-// AddRHS - Implements: X + X --> X << 1
+// AddRHS - Implements: X + X --> X << 1 and X + X --> X * 2 for vectors
 struct AddRHS {
   Value *RHS;
   AddRHS(Value *rhs) : RHS(rhs) {}
   bool shouldApply(Value *LHS) const { return LHS == RHS; }
   Instruction *apply(BinaryOperator &Add) const {
-    return BinaryOperator::CreateShl(Add.getOperand(0),
-                                  ConstantInt::get(Add.getType(), 1));
+    if (Add.getType()->getTypeID() == Type::VectorTyID) {
+      const VectorType *VTy = cast<VectorType>(Add.getType());
+      ConstantInt *CI = ConstantInt::get(VTy->getElementType(), 2);
+      std::vector<Constant*> Elts(VTy->getNumElements(), CI);
+      return BinaryOperator::CreateMul(Add.getOperand(0),
+                                       ConstantVector::get(Elts));
+    } else {
+      return BinaryOperator::CreateShl(Add.getOperand(0),
+                                       ConstantInt::get(Add.getType(), 1));
+    }
   }
 };
 
@@ -2627,8 +2635,8 @@
     }
   }
 
-  // X + X --> X << 1
-  if (I.getType()->isInteger() && I.getType() != Type::Int1Ty) {
+  // X + X --> X << 1 and X + X --> X * 2 for vectors
+  if (I.getType()->isIntOrIntVector() && I.getType() != Type::Int1Ty) {
     if (Instruction *Result = AssociativeOpt(I, AddRHS(RHS))) return Result;
 
     if (Instruction *RHSI = dyn_cast<Instruction>(RHS)) {