[llvm] r302420 - IR: Add a shufflevector mask commutation helper function. NFC.
Zvi Rackover via llvm-commits
llvm-commits at lists.llvm.org
Mon May 8 05:40:19 PDT 2017
Author: zvi
Date: Mon May 8 07:40:18 2017
New Revision: 302420
URL: http://llvm.org/viewvc/llvm-project?rev=302420&view=rev
Log:
IR: Add a shufflevector mask commutation helper function. NFC.
Summary:
Following up on Sanjay's suggetion in D32955, move this functionality
into ShuffleVectornstruction.
Reviewers: spatel, RKSimon
Reviewed By: RKSimon
Subscribers: llvm-commits
Differential Revision: https://reviews.llvm.org/D32956
Modified:
llvm/trunk/include/llvm/IR/Instructions.h
llvm/trunk/lib/Analysis/InstructionSimplify.cpp
llvm/trunk/unittests/IR/InstructionsTest.cpp
Modified: llvm/trunk/include/llvm/IR/Instructions.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/IR/Instructions.h?rev=302420&r1=302419&r2=302420&view=diff
==============================================================================
--- llvm/trunk/include/llvm/IR/Instructions.h (original)
+++ llvm/trunk/include/llvm/IR/Instructions.h Mon May 8 07:40:18 2017
@@ -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;
Modified: llvm/trunk/lib/Analysis/InstructionSimplify.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/InstructionSimplify.cpp?rev=302420&r1=302419&r2=302420&view=diff
==============================================================================
--- llvm/trunk/lib/Analysis/InstructionSimplify.cpp (original)
+++ llvm/trunk/lib/Analysis/InstructionSimplify.cpp Mon May 8 07:40:18 2017
@@ -4126,13 +4126,7 @@ static Value *SimplifyShuffleVectorInst(
// 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()),
Modified: llvm/trunk/unittests/IR/InstructionsTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/IR/InstructionsTest.cpp?rev=302420&r1=302419&r2=302420&view=diff
==============================================================================
--- llvm/trunk/unittests/IR/InstructionsTest.cpp (original)
+++ llvm/trunk/unittests/IR/InstructionsTest.cpp Mon May 8 07:40:18 2017
@@ -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
More information about the llvm-commits
mailing list