From 2a8f6597a3814e82e7b202e557541410c9282c33 Mon Sep 17 00:00:00 2001 From: Nick Lewycky Date: Thu, 18 Dec 2008 06:31:11 +0000 Subject: [PATCH] Make all the vector elements positive in an srem of constant vector. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@61195 91177308-0d34-0410-b5e6-96231b3b80d8 --- .../Scalar/InstructionCombining.cpp | 23 +++++++++++++++++++ .../InstCombine/2008-12-17-SRemNegConstVec.ll | 7 ++++++ 2 files changed, 30 insertions(+) create mode 100644 test/Transforms/InstCombine/2008-12-17-SRemNegConstVec.ll diff --git a/lib/Transforms/Scalar/InstructionCombining.cpp b/lib/Transforms/Scalar/InstructionCombining.cpp index 4914c110cb7..8d54e0c983a 100644 --- a/lib/Transforms/Scalar/InstructionCombining.cpp +++ b/lib/Transforms/Scalar/InstructionCombining.cpp @@ -3089,6 +3089,29 @@ Instruction *InstCombiner::visitSRem(BinaryOperator &I) { } } + // If it's a constant vector, flip any negative values positive. + if (isa(I.getType())) { + if (ConstantVector *RHSV = dyn_cast(Op1)) { + unsigned VWidth = RHSV->getNumOperands(); + std::vector Elts(VWidth); + + for (unsigned i = 0; i != VWidth; ++i) { + if (ConstantInt *RHS = dyn_cast(RHSV->getOperand(i))) { + if (RHS->getValue().isNegative()) + Elts[i] = cast(ConstantExpr::getNeg(RHS)); + else + Elts[i] = RHS; + } + } + + Constant *NewRHSV = ConstantVector::get(Elts); + if (NewRHSV != RHSV) { + I.setOperand(1, NewRHSV); + return &I; + } + } + } + return 0; } diff --git a/test/Transforms/InstCombine/2008-12-17-SRemNegConstVec.ll b/test/Transforms/InstCombine/2008-12-17-SRemNegConstVec.ll new file mode 100644 index 00000000000..f970b96ed4d --- /dev/null +++ b/test/Transforms/InstCombine/2008-12-17-SRemNegConstVec.ll @@ -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, + ret <2 x i8> %A +} -- 2.34.1