Make all the vector elements positive in an srem of constant vector.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@61195 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Transforms/Scalar/InstructionCombining.cpp b/lib/Transforms/Scalar/InstructionCombining.cpp
index 4914c11..8d54e0c 100644
--- a/lib/Transforms/Scalar/InstructionCombining.cpp
+++ b/lib/Transforms/Scalar/InstructionCombining.cpp
@@ -3089,6 +3089,29 @@
     }
   }
 
+  // If it's a constant vector, flip any negative values positive.
+  if (isa<VectorType>(I.getType())) {
+    if (ConstantVector *RHSV = dyn_cast<ConstantVector>(Op1)) {
+      unsigned VWidth = RHSV->getNumOperands();
+      std::vector<Constant *> Elts(VWidth);
+
+      for (unsigned i = 0; i != VWidth; ++i) {
+        if (ConstantInt *RHS = dyn_cast<ConstantInt>(RHSV->getOperand(i))) {
+          if (RHS->getValue().isNegative())
+            Elts[i] = cast<ConstantInt>(ConstantExpr::getNeg(RHS));
+          else
+            Elts[i] = RHS;
+        }
+      }
+
+      Constant *NewRHSV = ConstantVector::get(Elts);
+      if (NewRHSV != RHSV) {
+        I.setOperand(1, NewRHSV);
+        return &I;
+      }
+    }
+  }
+
   return 0;
 }