llvm-project
181 строка · 6.7 Кб
1//===- llvm/unittest/IR/ShuffleVectorInstTest.cpp - Shuffle unit tests ----===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "llvm/IR/Instructions.h"
10#include "gtest/gtest.h"
11
12using namespace llvm;
13
14namespace {
15
16TEST(ShuffleVectorInst, isIdentityMask) {
17ASSERT_TRUE(ShuffleVectorInst::isIdentityMask({0, 1, 2, 3}, 4));
18ASSERT_TRUE(ShuffleVectorInst::isIdentityMask({0, 1, 2, 3, -1}, 5));
19ASSERT_TRUE(ShuffleVectorInst::isIdentityMask({0, 1, -1, 3}, 4));
20
21ASSERT_FALSE(ShuffleVectorInst::isIdentityMask({0, 1, 2, 3}, 3));
22ASSERT_FALSE(ShuffleVectorInst::isIdentityMask({0, 1, 2, 3, -1}, 4));
23ASSERT_FALSE(ShuffleVectorInst::isIdentityMask({0, 1, -1, 3}, 3));
24
25ASSERT_FALSE(ShuffleVectorInst::isIdentityMask({0, 1, 2, 3}, 5));
26ASSERT_FALSE(ShuffleVectorInst::isIdentityMask({0, 1, 2, 3, -1}, 6));
27ASSERT_FALSE(ShuffleVectorInst::isIdentityMask({0, 1, -1, 3}, 5));
28
29ASSERT_FALSE(ShuffleVectorInst::isIdentityMask({0, 1, 2, 4}, 4));
30ASSERT_FALSE(ShuffleVectorInst::isIdentityMask({0, -1, 2, 4}, 4));
31}
32
33TEST(ShuffleVectorInst, isSelectMask) {
34ASSERT_TRUE(ShuffleVectorInst::isSelectMask({0, 5, 6, 3}, 4));
35
36ASSERT_FALSE(ShuffleVectorInst::isSelectMask({0, 5, 6, 3}, 3));
37ASSERT_FALSE(ShuffleVectorInst::isSelectMask({0, 5, 6, 3}, 5));
38
39ASSERT_FALSE(ShuffleVectorInst::isSelectMask({0, 1, 2, 3}, 4));
40}
41
42TEST(ShuffleVectorInst, isReverseMask) {
43ASSERT_TRUE(ShuffleVectorInst::isReverseMask({3, 2, 1, 0}, 4));
44ASSERT_TRUE(ShuffleVectorInst::isReverseMask({-1, -1, 1, 0}, 4));
45
46ASSERT_FALSE(ShuffleVectorInst::isReverseMask({3, 2, 1, 0}, 3));
47ASSERT_FALSE(ShuffleVectorInst::isReverseMask({-1, -1, 1, 0}, 3));
48ASSERT_FALSE(ShuffleVectorInst::isReverseMask({3, 2, 1, 0}, 5));
49ASSERT_FALSE(ShuffleVectorInst::isReverseMask({-1, -1, 1, 0}, 5));
50
51ASSERT_FALSE(ShuffleVectorInst::isReverseMask({4, 3, 2, 1}, 4));
52}
53
54TEST(ShuffleVectorInst, isZeroEltSplatMask) {
55ASSERT_TRUE(ShuffleVectorInst::isZeroEltSplatMask({0, 0, 0, 0}, 4));
56ASSERT_TRUE(ShuffleVectorInst::isZeroEltSplatMask({0, -1, 0, -1}, 4));
57
58ASSERT_FALSE(ShuffleVectorInst::isZeroEltSplatMask({0, 0, 0, 0}, 3));
59ASSERT_FALSE(ShuffleVectorInst::isZeroEltSplatMask({0, -1, 0, -1}, 3));
60ASSERT_FALSE(ShuffleVectorInst::isZeroEltSplatMask({0, 0, 0, 0}, 5));
61ASSERT_FALSE(ShuffleVectorInst::isZeroEltSplatMask({0, -1, 0, -1}, 5));
62
63ASSERT_FALSE(ShuffleVectorInst::isZeroEltSplatMask({1, 1, 1, 1}, 4));
64}
65
66TEST(ShuffleVectorInst, isTransposeMask) {
67ASSERT_TRUE(ShuffleVectorInst::isTransposeMask({0, 4, 2, 6}, 4));
68ASSERT_TRUE(ShuffleVectorInst::isTransposeMask({1, 5, 3, 7}, 4));
69
70ASSERT_FALSE(ShuffleVectorInst::isTransposeMask({0, 4, 2, 6}, 3));
71ASSERT_FALSE(ShuffleVectorInst::isTransposeMask({1, 5, 3, 7}, 3));
72ASSERT_FALSE(ShuffleVectorInst::isTransposeMask({0, 4, 2, 6}, 5));
73ASSERT_FALSE(ShuffleVectorInst::isTransposeMask({1, 5, 3, 7}, 5));
74
75ASSERT_FALSE(ShuffleVectorInst::isTransposeMask({2, 6, 4, 8}, 4));
76}
77
78TEST(ShuffleVectorInst, isSpliceMask) {
79int Index;
80
81ASSERT_TRUE(ShuffleVectorInst::isSpliceMask({0, 1, 2, 3}, 4, Index));
82ASSERT_EQ(0, Index);
83
84ASSERT_TRUE(ShuffleVectorInst::isSpliceMask({1, 2, 3, 4, 5, 6, 7}, 7, Index));
85ASSERT_EQ(1, Index);
86
87ASSERT_FALSE(ShuffleVectorInst::isSpliceMask({0, 1, 2, 3}, 3, Index));
88ASSERT_FALSE(
89ShuffleVectorInst::isSpliceMask({1, 2, 3, 4, 5, 6, 7}, 6, Index));
90ASSERT_FALSE(ShuffleVectorInst::isSpliceMask({0, 1, 2, 3}, 5, Index));
91ASSERT_FALSE(
92ShuffleVectorInst::isSpliceMask({1, 2, 3, 4, 5, 6, 7}, 8, Index));
93
94ASSERT_FALSE(ShuffleVectorInst::isSpliceMask({4, 5, 6, 7}, 4, Index));
95}
96
97TEST(ShuffleVectorInst, isExtractSubvectorMask) {
98int Index;
99
100ASSERT_TRUE(
101ShuffleVectorInst::isExtractSubvectorMask({0, 1, 2, 3}, 8, Index));
102ASSERT_EQ(0, Index);
103
104ASSERT_TRUE(
105ShuffleVectorInst::isExtractSubvectorMask({-1, 3, 4, 5}, 8, Index));
106ASSERT_EQ(2, Index);
107
108ASSERT_FALSE(
109ShuffleVectorInst::isExtractSubvectorMask({1, 2, 3, -1}, 4, Index));
110}
111
112TEST(ShuffleVectorInst, isInsertSubvectorMask) {
113int NumSubElts, Index;
114
115ASSERT_TRUE(ShuffleVectorInst::isInsertSubvectorMask(
116{8, 9, 10, 11, 4, 5, 6, 7}, 8, NumSubElts, Index));
117ASSERT_EQ(0, Index);
118ASSERT_EQ(4, NumSubElts);
119
120ASSERT_TRUE(
121ShuffleVectorInst::isInsertSubvectorMask({0, 2}, 2, NumSubElts, Index));
122ASSERT_EQ(1, Index);
123ASSERT_EQ(1, NumSubElts);
124}
125
126TEST(ShuffleVectorInst, isReplicationMask) {
127int ReplicationFactor, VF;
128
129ASSERT_TRUE(ShuffleVectorInst::isReplicationMask({0, 0, 1, 1, 2, 2, 3, 3},
130ReplicationFactor, VF));
131ASSERT_EQ(2, ReplicationFactor);
132ASSERT_EQ(4, VF);
133
134ASSERT_TRUE(ShuffleVectorInst::isReplicationMask(
135{0, 0, 0, 1, 1, 1, -1, -1, -1, 3, 3, 3, 4, 4, 4}, ReplicationFactor, VF));
136ASSERT_EQ(3, ReplicationFactor);
137ASSERT_EQ(5, VF);
138}
139
140TEST(ShuffleVectorInst, isOneUseSingleSourceMask) {
141ASSERT_TRUE(
142ShuffleVectorInst::isOneUseSingleSourceMask({0, 1, 2, 3, 3, 2, 0, 1}, 4));
143ASSERT_TRUE(
144ShuffleVectorInst::isOneUseSingleSourceMask({2, 3, 4, 5, 6, 7, 0, 1}, 8));
145
146ASSERT_FALSE(ShuffleVectorInst::isOneUseSingleSourceMask(
147{0, -1, 2, 3, 3, 2, 0, 1}, 4));
148ASSERT_FALSE(
149ShuffleVectorInst::isOneUseSingleSourceMask({0, 1, 2, 3, 3, 3, 1, 0}, 4));
150}
151
152TEST(ShuffleVectorInst, isInterleaveMask) {
153SmallVector<unsigned> StartIndexes;
154ASSERT_TRUE(ShuffleVectorInst::isInterleaveMask({0, 4, 1, 5, 2, 6, 3, 7}, 2,
1558, StartIndexes));
156ASSERT_EQ(StartIndexes, SmallVector<unsigned>({0, 4}));
157
158ASSERT_FALSE(
159ShuffleVectorInst::isInterleaveMask({0, 4, 1, 6, 2, 6, 3, 7}, 2, 8));
160
161ASSERT_TRUE(ShuffleVectorInst::isInterleaveMask({4, 0, 5, 1, 6, 2, 7, 3}, 2,
1628, StartIndexes));
163ASSERT_EQ(StartIndexes, SmallVector<unsigned>({4, 0}));
164
165ASSERT_TRUE(ShuffleVectorInst::isInterleaveMask({4, 0, -1, 1, -1, 2, 7, 3}, 2,
1668, StartIndexes));
167ASSERT_EQ(StartIndexes, SmallVector<unsigned>({4, 0}));
168
169ASSERT_TRUE(ShuffleVectorInst::isInterleaveMask({0, 2, 4, 1, 3, 5}, 3, 6,
170StartIndexes));
171ASSERT_EQ(StartIndexes, SmallVector<unsigned>({0, 2, 4}));
172
173ASSERT_TRUE(ShuffleVectorInst::isInterleaveMask({4, -1, 0, 5, 3, 1}, 3, 6,
174StartIndexes));
175ASSERT_EQ(StartIndexes, SmallVector<unsigned>({4, 2, 0}));
176
177ASSERT_FALSE(
178ShuffleVectorInst::isInterleaveMask({8, 2, 12, 4, 9, 3, 13, 5}, 4, 8));
179}
180
181} // end anonymous namespace
182