Make all the vector elements positive in an srem of constant vector.
authorNick Lewycky <nicholas@mxc.ca>
Thu, 18 Dec 2008 06:31:11 +0000 (06:31 +0000)
committerNick Lewycky <nicholas@mxc.ca>
Thu, 18 Dec 2008 06:31:11 +0000 (06:31 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@61195 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Transforms/Scalar/InstructionCombining.cpp
test/Transforms/InstCombine/2008-12-17-SRemNegConstVec.ll [new file with mode: 0644]

index 4914c110cb7f5a602af82a98c5cf65e9feafda71..8d54e0c983a4f6c83c369862c487d50a4b9403c0 100644 (file)
@@ -3089,6 +3089,29 @@ Instruction *InstCombiner::visitSRem(BinaryOperator &I) {
     }
   }
 
+  // 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;
 }
 
diff --git a/test/Transforms/InstCombine/2008-12-17-SRemNegConstVec.ll b/test/Transforms/InstCombine/2008-12-17-SRemNegConstVec.ll
new file mode 100644 (file)
index 0000000..f970b96
--- /dev/null
@@ -0,0 +1,7 @@
+; RUN: llvm-as < %s | opt -instcombine | llvm-dis | grep {i8 2, i8 2}
+; PR2756
+
+define <2 x i8> @foo(<2 x i8> %x) {
+  %A = srem <2 x i8> %x, <i8 2, i8 -2>
+  ret <2 x i8> %A
+}