From 8539fe26b7927f7a0c6228e488b8e8957e9726c6 Mon Sep 17 00:00:00 2001 From: Nick Lewycky Date: Fri, 23 May 2008 04:14:51 +0000 Subject: [PATCH] Implement X + X for vectors. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@51472 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Transforms/Scalar/InstructionCombining.cpp | 18 +++++++++++++----- .../InstCombine/2008-05-22-FoldAddIVec.ll | 6 ++++++ 2 files changed, 19 insertions(+), 5 deletions(-) create mode 100644 test/Transforms/InstCombine/2008-05-22-FoldAddIVec.ll diff --git a/lib/Transforms/Scalar/InstructionCombining.cpp b/lib/Transforms/Scalar/InstructionCombining.cpp index a398884e592..1a878681056 100644 --- a/lib/Transforms/Scalar/InstructionCombining.cpp +++ b/lib/Transforms/Scalar/InstructionCombining.cpp @@ -2292,14 +2292,22 @@ static Instruction *AssociativeOpt(BinaryOperator &Root, const Functor &F) { namespace { -// AddRHS - Implements: X + X --> X << 1 +// AddRHS - Implements: X + X --> X << 1 and X + X --> X * 2 for vectors struct AddRHS { Value *RHS; AddRHS(Value *rhs) : RHS(rhs) {} bool shouldApply(Value *LHS) const { return LHS == RHS; } Instruction *apply(BinaryOperator &Add) const { - return BinaryOperator::CreateShl(Add.getOperand(0), - ConstantInt::get(Add.getType(), 1)); + if (Add.getType()->getTypeID() == Type::VectorTyID) { + const VectorType *VTy = cast(Add.getType()); + ConstantInt *CI = ConstantInt::get(VTy->getElementType(), 2); + std::vector Elts(VTy->getNumElements(), CI); + return BinaryOperator::CreateMul(Add.getOperand(0), + ConstantVector::get(Elts)); + } else { + return BinaryOperator::CreateShl(Add.getOperand(0), + ConstantInt::get(Add.getType(), 1)); + } } }; @@ -2627,8 +2635,8 @@ Instruction *InstCombiner::visitAdd(BinaryOperator &I) { } } - // X + X --> X << 1 - if (I.getType()->isInteger() && I.getType() != Type::Int1Ty) { + // X + X --> X << 1 and X + X --> X * 2 for vectors + if (I.getType()->isIntOrIntVector() && I.getType() != Type::Int1Ty) { if (Instruction *Result = AssociativeOpt(I, AddRHS(RHS))) return Result; if (Instruction *RHSI = dyn_cast(RHS)) { diff --git a/test/Transforms/InstCombine/2008-05-22-FoldAddIVec.ll b/test/Transforms/InstCombine/2008-05-22-FoldAddIVec.ll new file mode 100644 index 00000000000..8bdf9bb2b8d --- /dev/null +++ b/test/Transforms/InstCombine/2008-05-22-FoldAddIVec.ll @@ -0,0 +1,6 @@ +; RUN: llvm-as < %s | opt -instcombine | llvm-dis | grep mul + +define <3 x i8> @f(<3 x i8> %i) { + %A = add <3 x i8> %i, %i + ret <3 x i8> %A +}