Have m_One also match constant vectors for which every element is 1.
authorDuncan Sands <baldrick@free.fr>
Tue, 1 Feb 2011 08:39:12 +0000 (08:39 +0000)
committerDuncan Sands <baldrick@free.fr>
Tue, 1 Feb 2011 08:39:12 +0000 (08:39 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@124655 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/Constants.h
include/llvm/Support/PatternMatch.h
lib/Analysis/InstructionSimplify.cpp
lib/VMCore/Constants.cpp
test/Transforms/InstSimplify/2011-02-01-Vector.ll [new file with mode: 0644]

index 738c90cee741f05572e423327c43791eab55921e..a7653948f13671b771651195bacf640c0e915965 100644 (file)
@@ -500,7 +500,7 @@ public:
 
   /// getSplatValue - If this is a splat constant, meaning that all of the
   /// elements have the same value, return that value. Otherwise return NULL.
-  Constant *getSplatValue();
+  Constant *getSplatValue() const;
 
   virtual void destroyConstant();
   virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
index 322ed436c9fb31d3d1bc4aba986c82ee19245c37..b203c1ee1a5beb11a8eda18191c793bbeef4102c 100644 (file)
@@ -90,13 +90,16 @@ inline zero_ty m_Zero() { return zero_ty(); }
 struct one_ty {
   template<typename ITy>
   bool match(ITy *V) {
-    if (const ConstantInt *C = dyn_cast<ConstantInt>(V))
-      return C->isOne();
+    if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
+      return CI->isOne();
+    if (const ConstantVector *CV = dyn_cast<ConstantVector>(V))
+      if (ConstantInt *CI = cast_or_null<ConstantInt>(CV->getSplatValue()))
+        return CI->isOne();
     return false;
   }
 };
 
-/// m_One() - Match an integer 1.
+/// m_One() - Match an integer 1 or a vector with all elements equal to 1.
 inline one_ty m_One() { return one_ty(); }
   
 struct all_ones_ty {
index a907150b9a27a5e61eef79ec8c12e87e209e4982..bce9a1fa0786d61912174adbb206731cc301cf25 100644 (file)
@@ -785,12 +785,6 @@ static Value *SimplifyDiv(unsigned Opcode, Value *Op0, Value *Op1,
   // X / 1 -> X
   if (match(Op1, m_One()))
     return Op0;
-  // Vector case. TODO: Have m_One match vectors.
-  if (ConstantVector *Op1V = dyn_cast<ConstantVector>(Op1)) {
-    if (ConstantInt *X = cast_or_null<ConstantInt>(Op1V->getSplatValue()))
-      if (X->isOne())
-        return Op0;
-  }
 
   if (Op0->getType()->isIntegerTy(1))
     // It can't be division by zero, hence it must be division by one.
index 3521ee7b16cfd60ffcd0d9069de2ecc153d44111..62117b22e6c9ec452c2a3b1e16c2577db3349579 100644 (file)
@@ -1033,7 +1033,7 @@ bool ConstantVector::isAllOnesValue() const {
 
 /// getSplatValue - If this is a splat constant, where all of the
 /// elements have the same value, return that value. Otherwise return null.
-Constant *ConstantVector::getSplatValue() {
+Constant *ConstantVector::getSplatValue() const {
   // Check out first element.
   Constant *Elt = getOperand(0);
   // Then make sure all remaining elements point to the same value.
diff --git a/test/Transforms/InstSimplify/2011-02-01-Vector.ll b/test/Transforms/InstSimplify/2011-02-01-Vector.ll
new file mode 100644 (file)
index 0000000..3039a66
--- /dev/null
@@ -0,0 +1,8 @@
+; RUN: opt < %s -instsimplify -S | FileCheck %s
+
+define <2 x i32> @sdiv(<2 x i32> %x) {
+; CHECK: @sdiv
+  %div = sdiv <2 x i32> %x, <i32 1, i32 1>
+  ret <2 x i32> %div
+; CHECK: ret <2 x i32> %x
+}