From 7681c6da606efcc392f76b8acea8301cb5fc7a0a Mon Sep 17 00:00:00 2001 From: Duncan Sands Date: Tue, 1 Feb 2011 08:39:12 +0000 Subject: [PATCH] Have m_One also match constant vectors for which every element is 1. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@124655 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/Constants.h | 2 +- include/llvm/Support/PatternMatch.h | 9 ++++++--- lib/Analysis/InstructionSimplify.cpp | 6 ------ lib/VMCore/Constants.cpp | 2 +- test/Transforms/InstSimplify/2011-02-01-Vector.ll | 8 ++++++++ 5 files changed, 16 insertions(+), 11 deletions(-) create mode 100644 test/Transforms/InstSimplify/2011-02-01-Vector.ll diff --git a/include/llvm/Constants.h b/include/llvm/Constants.h index 738c90cee74..a7653948f13 100644 --- a/include/llvm/Constants.h +++ b/include/llvm/Constants.h @@ -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); diff --git a/include/llvm/Support/PatternMatch.h b/include/llvm/Support/PatternMatch.h index 322ed436c9f..b203c1ee1a5 100644 --- a/include/llvm/Support/PatternMatch.h +++ b/include/llvm/Support/PatternMatch.h @@ -90,13 +90,16 @@ inline zero_ty m_Zero() { return zero_ty(); } struct one_ty { template bool match(ITy *V) { - if (const ConstantInt *C = dyn_cast(V)) - return C->isOne(); + if (const ConstantInt *CI = dyn_cast(V)) + return CI->isOne(); + if (const ConstantVector *CV = dyn_cast(V)) + if (ConstantInt *CI = cast_or_null(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 { diff --git a/lib/Analysis/InstructionSimplify.cpp b/lib/Analysis/InstructionSimplify.cpp index a907150b9a2..bce9a1fa078 100644 --- a/lib/Analysis/InstructionSimplify.cpp +++ b/lib/Analysis/InstructionSimplify.cpp @@ -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(Op1)) { - if (ConstantInt *X = cast_or_null(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. diff --git a/lib/VMCore/Constants.cpp b/lib/VMCore/Constants.cpp index 3521ee7b16c..62117b22e6c 100644 --- a/lib/VMCore/Constants.cpp +++ b/lib/VMCore/Constants.cpp @@ -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 index 00000000000..3039a663fa4 --- /dev/null +++ b/test/Transforms/InstSimplify/2011-02-01-Vector.ll @@ -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, + ret <2 x i32> %div +; CHECK: ret <2 x i32> %x +} -- 2.34.1