xref: /llvm-project/llvm/unittests/IR/ShuffleVectorInstTest.cpp (revision e22818d5c98a5cb2babc44de8b29abc22d613b15)
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 
12 using namespace llvm;
13 
14 namespace {
15 
TEST(ShuffleVectorInst,isIdentityMask)16 TEST(ShuffleVectorInst, isIdentityMask) {
17   ASSERT_TRUE(ShuffleVectorInst::isIdentityMask({0, 1, 2, 3}, 4));
18   ASSERT_TRUE(ShuffleVectorInst::isIdentityMask({0, 1, 2, 3, -1}, 5));
19   ASSERT_TRUE(ShuffleVectorInst::isIdentityMask({0, 1, -1, 3}, 4));
20 
21   ASSERT_FALSE(ShuffleVectorInst::isIdentityMask({0, 1, 2, 3}, 3));
22   ASSERT_FALSE(ShuffleVectorInst::isIdentityMask({0, 1, 2, 3, -1}, 4));
23   ASSERT_FALSE(ShuffleVectorInst::isIdentityMask({0, 1, -1, 3}, 3));
24 
25   ASSERT_FALSE(ShuffleVectorInst::isIdentityMask({0, 1, 2, 3}, 5));
26   ASSERT_FALSE(ShuffleVectorInst::isIdentityMask({0, 1, 2, 3, -1}, 6));
27   ASSERT_FALSE(ShuffleVectorInst::isIdentityMask({0, 1, -1, 3}, 5));
28 
29   ASSERT_FALSE(ShuffleVectorInst::isIdentityMask({0, 1, 2, 4}, 4));
30   ASSERT_FALSE(ShuffleVectorInst::isIdentityMask({0, -1, 2, 4}, 4));
31 }
32 
TEST(ShuffleVectorInst,isSelectMask)33 TEST(ShuffleVectorInst, isSelectMask) {
34   ASSERT_TRUE(ShuffleVectorInst::isSelectMask({0, 5, 6, 3}, 4));
35 
36   ASSERT_FALSE(ShuffleVectorInst::isSelectMask({0, 5, 6, 3}, 3));
37   ASSERT_FALSE(ShuffleVectorInst::isSelectMask({0, 5, 6, 3}, 5));
38 
39   ASSERT_FALSE(ShuffleVectorInst::isSelectMask({0, 1, 2, 3}, 4));
40 }
41 
TEST(ShuffleVectorInst,isReverseMask)42 TEST(ShuffleVectorInst, isReverseMask) {
43   ASSERT_TRUE(ShuffleVectorInst::isReverseMask({3, 2, 1, 0}, 4));
44   ASSERT_TRUE(ShuffleVectorInst::isReverseMask({-1, -1, 1, 0}, 4));
45 
46   ASSERT_FALSE(ShuffleVectorInst::isReverseMask({3, 2, 1, 0}, 3));
47   ASSERT_FALSE(ShuffleVectorInst::isReverseMask({-1, -1, 1, 0}, 3));
48   ASSERT_FALSE(ShuffleVectorInst::isReverseMask({3, 2, 1, 0}, 5));
49   ASSERT_FALSE(ShuffleVectorInst::isReverseMask({-1, -1, 1, 0}, 5));
50 
51   ASSERT_FALSE(ShuffleVectorInst::isReverseMask({4, 3, 2, 1}, 4));
52 }
53 
TEST(ShuffleVectorInst,isZeroEltSplatMask)54 TEST(ShuffleVectorInst, isZeroEltSplatMask) {
55   ASSERT_TRUE(ShuffleVectorInst::isZeroEltSplatMask({0, 0, 0, 0}, 4));
56   ASSERT_TRUE(ShuffleVectorInst::isZeroEltSplatMask({0, -1, 0, -1}, 4));
57 
58   ASSERT_FALSE(ShuffleVectorInst::isZeroEltSplatMask({0, 0, 0, 0}, 3));
59   ASSERT_FALSE(ShuffleVectorInst::isZeroEltSplatMask({0, -1, 0, -1}, 3));
60   ASSERT_FALSE(ShuffleVectorInst::isZeroEltSplatMask({0, 0, 0, 0}, 5));
61   ASSERT_FALSE(ShuffleVectorInst::isZeroEltSplatMask({0, -1, 0, -1}, 5));
62 
63   ASSERT_FALSE(ShuffleVectorInst::isZeroEltSplatMask({1, 1, 1, 1}, 4));
64 }
65 
TEST(ShuffleVectorInst,isTransposeMask)66 TEST(ShuffleVectorInst, isTransposeMask) {
67   ASSERT_TRUE(ShuffleVectorInst::isTransposeMask({0, 4, 2, 6}, 4));
68   ASSERT_TRUE(ShuffleVectorInst::isTransposeMask({1, 5, 3, 7}, 4));
69 
70   ASSERT_FALSE(ShuffleVectorInst::isTransposeMask({0, 4, 2, 6}, 3));
71   ASSERT_FALSE(ShuffleVectorInst::isTransposeMask({1, 5, 3, 7}, 3));
72   ASSERT_FALSE(ShuffleVectorInst::isTransposeMask({0, 4, 2, 6}, 5));
73   ASSERT_FALSE(ShuffleVectorInst::isTransposeMask({1, 5, 3, 7}, 5));
74 
75   ASSERT_FALSE(ShuffleVectorInst::isTransposeMask({2, 6, 4, 8}, 4));
76 }
77 
TEST(ShuffleVectorInst,isSpliceMask)78 TEST(ShuffleVectorInst, isSpliceMask) {
79   int Index;
80 
81   ASSERT_TRUE(ShuffleVectorInst::isSpliceMask({0, 1, 2, 3}, 4, Index));
82   ASSERT_EQ(0, Index);
83 
84   ASSERT_TRUE(ShuffleVectorInst::isSpliceMask({1, 2, 3, 4, 5, 6, 7}, 7, Index));
85   ASSERT_EQ(1, Index);
86 
87   ASSERT_FALSE(ShuffleVectorInst::isSpliceMask({0, 1, 2, 3}, 3, Index));
88   ASSERT_FALSE(
89       ShuffleVectorInst::isSpliceMask({1, 2, 3, 4, 5, 6, 7}, 6, Index));
90   ASSERT_FALSE(ShuffleVectorInst::isSpliceMask({0, 1, 2, 3}, 5, Index));
91   ASSERT_FALSE(
92       ShuffleVectorInst::isSpliceMask({1, 2, 3, 4, 5, 6, 7}, 8, Index));
93 
94   ASSERT_FALSE(ShuffleVectorInst::isSpliceMask({4, 5, 6, 7}, 4, Index));
95 }
96 
TEST(ShuffleVectorInst,isExtractSubvectorMask)97 TEST(ShuffleVectorInst, isExtractSubvectorMask) {
98   int Index;
99 
100   ASSERT_TRUE(
101       ShuffleVectorInst::isExtractSubvectorMask({0, 1, 2, 3}, 8, Index));
102   ASSERT_EQ(0, Index);
103 
104   ASSERT_TRUE(
105       ShuffleVectorInst::isExtractSubvectorMask({-1, 3, 4, 5}, 8, Index));
106   ASSERT_EQ(2, Index);
107 
108   ASSERT_FALSE(
109       ShuffleVectorInst::isExtractSubvectorMask({1, 2, 3, -1}, 4, Index));
110 }
111 
TEST(ShuffleVectorInst,isInsertSubvectorMask)112 TEST(ShuffleVectorInst, isInsertSubvectorMask) {
113   int NumSubElts, Index;
114 
115   ASSERT_TRUE(ShuffleVectorInst::isInsertSubvectorMask(
116       {8, 9, 10, 11, 4, 5, 6, 7}, 8, NumSubElts, Index));
117   ASSERT_EQ(0, Index);
118   ASSERT_EQ(4, NumSubElts);
119 
120   ASSERT_TRUE(
121       ShuffleVectorInst::isInsertSubvectorMask({0, 2}, 2, NumSubElts, Index));
122   ASSERT_EQ(1, Index);
123   ASSERT_EQ(1, NumSubElts);
124 }
125 
TEST(ShuffleVectorInst,isReplicationMask)126 TEST(ShuffleVectorInst, isReplicationMask) {
127   int ReplicationFactor, VF;
128 
129   ASSERT_TRUE(ShuffleVectorInst::isReplicationMask({0, 0, 1, 1, 2, 2, 3, 3},
130                                                    ReplicationFactor, VF));
131   ASSERT_EQ(2, ReplicationFactor);
132   ASSERT_EQ(4, VF);
133 
134   ASSERT_TRUE(ShuffleVectorInst::isReplicationMask(
135       {0, 0, 0, 1, 1, 1, -1, -1, -1, 3, 3, 3, 4, 4, 4}, ReplicationFactor, VF));
136   ASSERT_EQ(3, ReplicationFactor);
137   ASSERT_EQ(5, VF);
138 }
139 
TEST(ShuffleVectorInst,isOneUseSingleSourceMask)140 TEST(ShuffleVectorInst, isOneUseSingleSourceMask) {
141   ASSERT_TRUE(
142       ShuffleVectorInst::isOneUseSingleSourceMask({0, 1, 2, 3, 3, 2, 0, 1}, 4));
143   ASSERT_TRUE(
144       ShuffleVectorInst::isOneUseSingleSourceMask({2, 3, 4, 5, 6, 7, 0, 1}, 8));
145 
146   ASSERT_FALSE(ShuffleVectorInst::isOneUseSingleSourceMask(
147       {0, -1, 2, 3, 3, 2, 0, 1}, 4));
148   ASSERT_FALSE(
149       ShuffleVectorInst::isOneUseSingleSourceMask({0, 1, 2, 3, 3, 3, 1, 0}, 4));
150 }
151 
TEST(ShuffleVectorInst,isInterleaveMask)152 TEST(ShuffleVectorInst, isInterleaveMask) {
153   SmallVector<unsigned> StartIndexes;
154   ASSERT_TRUE(ShuffleVectorInst::isInterleaveMask({0, 4, 1, 5, 2, 6, 3, 7}, 2,
155                                                   8, StartIndexes));
156   ASSERT_EQ(StartIndexes, SmallVector<unsigned>({0, 4}));
157 
158   ASSERT_FALSE(
159       ShuffleVectorInst::isInterleaveMask({0, 4, 1, 6, 2, 6, 3, 7}, 2, 8));
160 
161   ASSERT_TRUE(ShuffleVectorInst::isInterleaveMask({4, 0, 5, 1, 6, 2, 7, 3}, 2,
162                                                   8, StartIndexes));
163   ASSERT_EQ(StartIndexes, SmallVector<unsigned>({4, 0}));
164 
165   ASSERT_TRUE(ShuffleVectorInst::isInterleaveMask({4, 0, -1, 1, -1, 2, 7, 3}, 2,
166                                                   8, StartIndexes));
167   ASSERT_EQ(StartIndexes, SmallVector<unsigned>({4, 0}));
168 
169   ASSERT_TRUE(ShuffleVectorInst::isInterleaveMask({0, 2, 4, 1, 3, 5}, 3, 6,
170                                                   StartIndexes));
171   ASSERT_EQ(StartIndexes, SmallVector<unsigned>({0, 2, 4}));
172 
173   ASSERT_TRUE(ShuffleVectorInst::isInterleaveMask({4, -1, 0, 5, 3, 1}, 3, 6,
174                                                   StartIndexes));
175   ASSERT_EQ(StartIndexes, SmallVector<unsigned>({4, 2, 0}));
176 
177   ASSERT_FALSE(
178       ShuffleVectorInst::isInterleaveMask({8, 2, 12, 4, 9, 3, 13, 5}, 4, 8));
179 }
180 
181 } // end anonymous namespace
182