[llvm-commits] [llvm] r61195 - in /llvm/trunk: lib/Transforms/Scalar/InstructionCombining.cpp test/Transforms/InstCombine/2008-12-17-SRemNegConstVec.ll

Nick Lewycky nicholas at mxc.ca
Wed Dec 17 22:31:12 PST 2008


Author: nicholas
Date: Thu Dec 18 00:31:11 2008
New Revision: 61195

URL: http://llvm.org/viewvc/llvm-project?rev=61195&view=rev
Log:
Make all the vector elements positive in an srem of constant vector.

Added:
    llvm/trunk/test/Transforms/InstCombine/2008-12-17-SRemNegConstVec.ll
Modified:
    llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp

Modified: llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp?rev=61195&r1=61194&r2=61195&view=diff

==============================================================================
--- llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp (original)
+++ llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp Thu Dec 18 00:31:11 2008
@@ -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;
 }
 

Added: llvm/trunk/test/Transforms/InstCombine/2008-12-17-SRemNegConstVec.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/2008-12-17-SRemNegConstVec.ll?rev=61195&view=auto

==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/2008-12-17-SRemNegConstVec.ll (added)
+++ llvm/trunk/test/Transforms/InstCombine/2008-12-17-SRemNegConstVec.ll Thu Dec 18 00:31:11 2008
@@ -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
+}





More information about the llvm-commits mailing list