diff options
| -rw-r--r-- | llvm/include/llvm/IR/Instructions.h | 13 | ||||
| -rw-r--r-- | llvm/lib/Analysis/InstructionSimplify.cpp | 8 | ||||
| -rw-r--r-- | llvm/unittests/IR/InstructionsTest.cpp | 7 |
3 files changed, 21 insertions, 7 deletions
diff --git a/llvm/include/llvm/IR/Instructions.h b/llvm/include/llvm/IR/Instructions.h index 844a7273eca..1b75b2cd7ba 100644 --- a/llvm/include/llvm/IR/Instructions.h +++ b/llvm/include/llvm/IR/Instructions.h @@ -2261,6 +2261,19 @@ public: return Mask; } + /// Change values in a shuffle permute mask assuming the two vector operands + /// of length InVecNumElts have swapped position. + static void commuteShuffleMask(MutableArrayRef<int> Mask, + unsigned InVecNumElts) { + for (int &Idx : Mask) { + if (Idx == -1) + continue; + Idx = Idx < (int)InVecNumElts ? Idx + InVecNumElts : Idx - InVecNumElts; + assert(Idx >= 0 && Idx < (int)InVecNumElts * 2 && + "shufflevector mask index out of range"); + } + } + // Methods for support type inquiry through isa, cast, and dyn_cast: static inline bool classof(const Instruction *I) { return I->getOpcode() == Instruction::ShuffleVector; diff --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp index 4a713f441ce..4e41ea95e3a 100644 --- a/llvm/lib/Analysis/InstructionSimplify.cpp +++ b/llvm/lib/Analysis/InstructionSimplify.cpp @@ -4126,13 +4126,7 @@ static Value *SimplifyShuffleVectorInst(Value *Op0, Value *Op1, Constant *Mask, // second one. if (Op0Const && !Op1Const) { std::swap(Op0, Op1); - for (int &Idx : Indices) { - if (Idx == -1) - continue; - Idx = Idx < (int)InVecNumElts ? Idx + InVecNumElts : Idx - InVecNumElts; - assert(Idx >= 0 && Idx < (int)InVecNumElts * 2 && - "shufflevector mask index out of range"); - } + ShuffleVectorInst::commuteShuffleMask(Indices, InVecNumElts); Mask = ConstantDataVector::get( Mask->getContext(), makeArrayRef(reinterpret_cast<uint32_t *>(Indices.data()), diff --git a/llvm/unittests/IR/InstructionsTest.cpp b/llvm/unittests/IR/InstructionsTest.cpp index 7c75aaec175..b8d398c5cc3 100644 --- a/llvm/unittests/IR/InstructionsTest.cpp +++ b/llvm/unittests/IR/InstructionsTest.cpp @@ -21,6 +21,7 @@ #include "llvm/IR/Module.h" #include "llvm/IR/NoFolder.h" #include "llvm/IR/Operator.h" +#include "gmock/gmock-matchers.h" #include "gtest/gtest.h" #include <memory> @@ -740,5 +741,11 @@ TEST(InstructionsTest, SwitchInst) { EXPECT_EQ(BB1.get(), Handle.getCaseSuccessor()); } +TEST(InstructionsTest, CommuteShuffleMask) { + SmallVector<int, 16> Indices({-1, 0, 7}); + ShuffleVectorInst::commuteShuffleMask(Indices, 4); + EXPECT_THAT(Indices, testing::ContainerEq(ArrayRef<int>({-1, 4, 3}))); +} + } // end anonymous namespace } // end namespace llvm |

