From 69941fd0a0531cdcb77baf4a5a89bbdc79117489 Mon Sep 17 00:00:00 2001 From: Nick Lewycky Date: Thu, 27 Nov 2008 20:21:08 +0000 Subject: [PATCH] Add a couple of missed optimizations on integer vectors. Multiply and divide by 1, as well as multiply by -1. llvm-svn: 60182 --- .../Scalar/InstructionCombining.cpp | 28 +++++++++++++++---- .../InstCombine/2008-11-27-IDivVector.ll | 11 ++++++++ .../InstCombine/2008-11-27-MultiplyIntVec.ll | 11 ++++++++ 3 files changed, 44 insertions(+), 6 deletions(-) create mode 100644 llvm/test/Transforms/InstCombine/2008-11-27-IDivVector.ll create mode 100644 llvm/test/Transforms/InstCombine/2008-11-27-MultiplyIntVec.ll diff --git a/llvm/lib/Transforms/Scalar/InstructionCombining.cpp b/llvm/lib/Transforms/Scalar/InstructionCombining.cpp index 5631bd0ee008..661b0475cc59 100644 --- a/llvm/lib/Transforms/Scalar/InstructionCombining.cpp +++ b/llvm/lib/Transforms/Scalar/InstructionCombining.cpp @@ -2576,12 +2576,21 @@ Instruction *InstCombiner::visitMul(BinaryOperator &I) { } else if (isa(Op1->getType())) { if (isa(Op1)) return ReplaceInstUsesWith(I, Op1); - - // As above, vector X*splat(1.0) -> X in all defined cases. - if (ConstantVector *Op1V = dyn_cast(Op1)) - if (ConstantFP *F = dyn_cast_or_null(Op1V->getSplatValue())) - if (F->isExactlyValue(1.0)) - return ReplaceInstUsesWith(I, Op0); + + if (ConstantVector *Op1V = dyn_cast(Op1)) { + if (Op1V->isAllOnesValue()) // X * -1 == 0 - X + return BinaryOperator::CreateNeg(Op0, I.getName()); + + // As above, vector X*splat(1.0) -> X in all defined cases. + if (Constant *Splat = Op1V->getSplatValue()) { + if (ConstantFP *F = dyn_cast(Splat)) + if (F->isExactlyValue(1.0)) + return ReplaceInstUsesWith(I, Op0); + if (ConstantInt *CI = dyn_cast(Splat)) + if (CI->equalsInt(1)) + return ReplaceInstUsesWith(I, Op0); + } + } } if (BinaryOperator *Op0I = dyn_cast(Op0)) @@ -2856,6 +2865,13 @@ Instruction *InstCombiner::commonIDivTransforms(BinaryOperator &I) { if (I.getType() == Type::Int1Ty) return ReplaceInstUsesWith(I, Op0); + if (ConstantVector *Op1V = dyn_cast(Op1)) { + if (ConstantInt *X = cast_or_null(Op1V->getSplatValue())) + // div X, 1 == X + if (X->isOne()) + return ReplaceInstUsesWith(I, Op0); + } + return 0; } diff --git a/llvm/test/Transforms/InstCombine/2008-11-27-IDivVector.ll b/llvm/test/Transforms/InstCombine/2008-11-27-IDivVector.ll new file mode 100644 index 000000000000..4275e1191a88 --- /dev/null +++ b/llvm/test/Transforms/InstCombine/2008-11-27-IDivVector.ll @@ -0,0 +1,11 @@ +; RUN: llvm-as < %s | opt -instcombine | llvm-dis | not grep div + +define <2 x i8> @f(<2 x i8> %x) { + %A = udiv <2 x i8> %x, + ret <2 x i8> %A +} + +define <2 x i8> @g(<2 x i8> %x) { + %A = sdiv <2 x i8> %x, + ret <2 x i8> %A +} diff --git a/llvm/test/Transforms/InstCombine/2008-11-27-MultiplyIntVec.ll b/llvm/test/Transforms/InstCombine/2008-11-27-MultiplyIntVec.ll new file mode 100644 index 000000000000..544e9abbbcb7 --- /dev/null +++ b/llvm/test/Transforms/InstCombine/2008-11-27-MultiplyIntVec.ll @@ -0,0 +1,11 @@ +; RUN: llvm-as < %s | opt -instcombine | llvm-dis | not grep mul + +define <2 x i8> @f(<2 x i8> %x) { + %A = mul <2 x i8> %x, + ret <2 x i8> %A +} + +define <2 x i8> @g(<2 x i8> %x) { + %A = mul <2 x i8> %x, + ret <2 x i8> %A +} -- GitLab