xref: /llvm-project/llvm/unittests/Analysis/VectorUtilsTest.cpp (revision 52b48a70d3752f9db36ddcfd26d0451c009b19fc)
1 //===- VectorUtilsTest.cpp - VectorUtils 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/Analysis/VectorUtils.h"
10 #include "llvm/Analysis/ValueTracking.h"
11 #include "llvm/AsmParser/Parser.h"
12 #include "llvm/IR/Function.h"
13 #include "llvm/IR/InstIterator.h"
14 #include "llvm/IR/IRBuilder.h"
15 #include "llvm/IR/LLVMContext.h"
16 #include "llvm/IR/Module.h"
17 #include "llvm/IR/NoFolder.h"
18 #include "llvm/Support/ErrorHandling.h"
19 #include "llvm/Support/SourceMgr.h"
20 #include "gtest/gtest.h"
21 
22 using namespace llvm;
23 
24 namespace {
25 
26 class VectorUtilsTest : public testing::Test {
27 protected:
28   void parseAssembly(const char *Assembly) {
29     SMDiagnostic Error;
30     M = parseAssemblyString(Assembly, Error, Context);
31 
32     std::string errMsg;
33     raw_string_ostream os(errMsg);
34     Error.print("", os);
35 
36     // A failure here means that the test itself is buggy.
37     if (!M)
38       report_fatal_error(Twine(errMsg));
39 
40     Function *F = M->getFunction("test");
41     if (F == nullptr)
42       report_fatal_error("Test must have a function named @test");
43 
44     A = nullptr;
45     for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) {
46       if (I->hasName()) {
47         if (I->getName() == "A")
48           A = &*I;
49       }
50     }
51     if (A == nullptr)
52       report_fatal_error("@test must have an instruction %A");
53   }
54 
55   LLVMContext Context;
56   std::unique_ptr<Module> M;
57   Instruction *A;
58 };
59 
60 struct BasicTest : public testing::Test {
61   LLVMContext Ctx;
62   std::unique_ptr<Module> M;
63   Function *F;
64   BasicBlock *BB;
65   IRBuilder<NoFolder> IRB;
66 
67   BasicTest()
68       : M(new Module("VectorUtils", Ctx)),
69         F(Function::Create(
70             FunctionType::get(Type::getVoidTy(Ctx), /* IsVarArg */ false),
71             Function::ExternalLinkage, "f", M.get())),
72         BB(BasicBlock::Create(Ctx, "entry", F)), IRB(BB) {}
73 };
74 
75 
76 } // namespace
77 
78 TEST_F(BasicTest, isSplat) {
79   Value *UndefVec = UndefValue::get(FixedVectorType::get(IRB.getInt8Ty(), 4));
80   EXPECT_TRUE(isSplatValue(UndefVec));
81 
82   Constant *UndefScalar = UndefValue::get(IRB.getInt8Ty());
83   EXPECT_FALSE(isSplatValue(UndefScalar));
84 
85   Constant *ScalarC = IRB.getInt8(42);
86   EXPECT_FALSE(isSplatValue(ScalarC));
87 
88   Constant *OtherScalarC = IRB.getInt8(-42);
89   Constant *NonSplatC = ConstantVector::get({ScalarC, OtherScalarC});
90   EXPECT_FALSE(isSplatValue(NonSplatC));
91 
92   Value *SplatC = IRB.CreateVectorSplat(5, ScalarC);
93   EXPECT_TRUE(isSplatValue(SplatC));
94 
95   Value *SplatC_SVE =
96       IRB.CreateVectorSplat(ElementCount::getScalable(5), ScalarC);
97   EXPECT_TRUE(isSplatValue(SplatC_SVE));
98 
99   // FIXME: Constant splat analysis does not allow undef elements.
100   Constant *SplatWithUndefC = ConstantVector::get({ScalarC, UndefScalar});
101   EXPECT_FALSE(isSplatValue(SplatWithUndefC));
102 }
103 
104 TEST_F(BasicTest, narrowShuffleMaskElts) {
105   SmallVector<int, 16> ScaledMask;
106   narrowShuffleMaskElts(1, {3,2,0,-2}, ScaledMask);
107   EXPECT_EQ(ArrayRef(ScaledMask), ArrayRef({3, 2, 0, -2}));
108   narrowShuffleMaskElts(4, {3,2,0,-1}, ScaledMask);
109   EXPECT_EQ(ArrayRef(ScaledMask), ArrayRef({12, 13, 14, 15, 8, 9, 10, 11, 0, 1,
110                                             2, 3, -1, -1, -1, -1}));
111 }
112 
113 TEST_F(BasicTest, widenShuffleMaskElts) {
114   SmallVector<int, 16> WideMask;
115   SmallVector<int, 16> NarrowMask;
116 
117   // scale == 1 is a copy
118   EXPECT_TRUE(widenShuffleMaskElts(1, {3,2,0,-1}, WideMask));
119   EXPECT_EQ(ArrayRef(WideMask), ArrayRef({3, 2, 0, -1}));
120 
121   // back to original mask
122   narrowShuffleMaskElts(1, ArrayRef(WideMask), NarrowMask);
123   EXPECT_EQ(ArrayRef(NarrowMask), ArrayRef({3, 2, 0, -1}));
124 
125   // can't widen non-consecutive 3/2
126   EXPECT_FALSE(widenShuffleMaskElts(2, {3,2,0,-1}, WideMask));
127 
128   // can't widen if not evenly divisible
129   EXPECT_FALSE(widenShuffleMaskElts(2, {0,1,2}, WideMask));
130 
131   // can always widen identity to single element
132   EXPECT_TRUE(widenShuffleMaskElts(3, {0,1,2}, WideMask));
133   EXPECT_EQ(ArrayRef(WideMask), ArrayRef({0}));
134 
135   // back to original mask
136   narrowShuffleMaskElts(3, ArrayRef(WideMask), NarrowMask);
137   EXPECT_EQ(ArrayRef(NarrowMask), ArrayRef({0, 1, 2}));
138 
139   // groups of 4 must be consecutive/undef
140   EXPECT_TRUE(widenShuffleMaskElts(4, {12,13,14,15,8,9,10,11,0,1,2,3,-1,-1,-1,-1}, WideMask));
141   EXPECT_EQ(ArrayRef(WideMask), ArrayRef({3, 2, 0, -1}));
142 
143   // back to original mask
144   narrowShuffleMaskElts(4, ArrayRef(WideMask), NarrowMask);
145   EXPECT_EQ(ArrayRef(NarrowMask), ArrayRef({12, 13, 14, 15, 8, 9, 10, 11, 0, 1,
146                                             2, 3, -1, -1, -1, -1}));
147 
148   // groups of 2 must be consecutive/undef
149   EXPECT_FALSE(widenShuffleMaskElts(2, {12,12,14,15,8,9,10,11,0,1,2,3,-1,-1,-1,-1}, WideMask));
150 
151   // groups of 3 must be consecutive/undef
152   EXPECT_TRUE(widenShuffleMaskElts(3, {6,7,8,0,1,2,-1,-1,-1}, WideMask));
153   EXPECT_EQ(ArrayRef(WideMask), ArrayRef({2, 0, -1}));
154 
155   // back to original mask
156   narrowShuffleMaskElts(3, ArrayRef(WideMask), NarrowMask);
157   EXPECT_EQ(ArrayRef(NarrowMask), ArrayRef({6, 7, 8, 0, 1, 2, -1, -1, -1}));
158 
159   // groups of 3 must be consecutive/undef (partial undefs are not ok)
160   EXPECT_FALSE(widenShuffleMaskElts(3, {-1,7,8,0,-1,2,-1,-1,-1}, WideMask));
161 
162   // negative indexes must match across a wide element
163   EXPECT_FALSE(widenShuffleMaskElts(2, {-1,-2,-1,-1}, WideMask));
164 
165   // negative indexes must match across a wide element
166   EXPECT_TRUE(widenShuffleMaskElts(2, {-2,-2,-3,-3}, WideMask));
167   EXPECT_EQ(ArrayRef(WideMask), ArrayRef({-2, -3}));
168 }
169 
170 TEST_F(BasicTest, getShuffleMaskWithWidestElts) {
171   SmallVector<int, 16> WideMask;
172 
173   // can not widen anything here.
174   getShuffleMaskWithWidestElts({3, 2, 0, -1}, WideMask);
175   EXPECT_EQ(ArrayRef(WideMask), ArrayRef({3, 2, 0, -1}));
176 
177   // can't widen non-consecutive 3/2
178   getShuffleMaskWithWidestElts({3, 2, 0, -1}, WideMask);
179   EXPECT_EQ(ArrayRef(WideMask), ArrayRef({3, 2, 0, -1}));
180 
181   // can always widen identity to single element
182   getShuffleMaskWithWidestElts({0, 1, 2}, WideMask);
183   EXPECT_EQ(ArrayRef(WideMask), ArrayRef({0}));
184 
185   // groups of 4 must be consecutive/undef
186   getShuffleMaskWithWidestElts(
187       {12, 13, 14, 15, 8, 9, 10, 11, 0, 1, 2, 3, -1, -1, -1, -1}, WideMask);
188   EXPECT_EQ(ArrayRef(WideMask), ArrayRef({3, 2, 0, -1}));
189 
190   // groups of 2 must be consecutive/undef
191   getShuffleMaskWithWidestElts(
192       {12, 12, 14, 15, 8, 9, 10, 11, 0, 1, 2, 3, -1, -1, -1, -1}, WideMask);
193   EXPECT_EQ(ArrayRef(WideMask), ArrayRef({12, 12, 14, 15, 8, 9, 10, 11, 0, 1, 2,
194                                           3, -1, -1, -1, -1}));
195 
196   // groups of 3 must be consecutive/undef
197   getShuffleMaskWithWidestElts({6, 7, 8, 0, 1, 2, -1, -1, -1}, WideMask);
198   EXPECT_EQ(ArrayRef(WideMask), ArrayRef({2, 0, -1}));
199 
200   // groups of 3 must be consecutive/undef (partial undefs are not ok)
201   getShuffleMaskWithWidestElts({-1, 7, 8, 0, -1, 2, -1, -1, -1}, WideMask);
202   EXPECT_EQ(ArrayRef(WideMask), ArrayRef({-1, 7, 8, 0, -1, 2, -1, -1, -1}));
203 
204   // negative indexes must match across a wide element
205   getShuffleMaskWithWidestElts({-1, -2, -1, -1}, WideMask);
206   EXPECT_EQ(ArrayRef(WideMask), ArrayRef({-1, -2, -1, -1}));
207 
208   // negative indexes must match across a wide element
209   getShuffleMaskWithWidestElts({-2, -2, -3, -3}, WideMask);
210   EXPECT_EQ(ArrayRef(WideMask), ArrayRef({-2, -3}));
211 }
212 
213 TEST_F(BasicTest, getShuffleDemandedElts) {
214   APInt LHS, RHS;
215 
216   // broadcast zero
217   EXPECT_TRUE(getShuffleDemandedElts(4, {0, 0, 0, 0}, APInt(4,0xf), LHS, RHS));
218   EXPECT_EQ(LHS.getZExtValue(), 0x1U);
219   EXPECT_EQ(RHS.getZExtValue(), 0x0U);
220 
221   // broadcast zero (with non-permitted undefs)
222   EXPECT_FALSE(getShuffleDemandedElts(2, {0, -1}, APInt(2, 0x3), LHS, RHS));
223 
224   // broadcast zero (with permitted undefs)
225   EXPECT_TRUE(getShuffleDemandedElts(3, {0, 0, -1}, APInt(3, 0x7), LHS, RHS, true));
226   EXPECT_EQ(LHS.getZExtValue(), 0x1U);
227   EXPECT_EQ(RHS.getZExtValue(), 0x0U);
228 
229   // broadcast one in demanded
230   EXPECT_TRUE(getShuffleDemandedElts(4, {1, 1, 1, -1}, APInt(4, 0x7), LHS, RHS));
231   EXPECT_EQ(LHS.getZExtValue(), 0x2U);
232   EXPECT_EQ(RHS.getZExtValue(), 0x0U);
233 
234   // broadcast 7 in demanded
235   EXPECT_TRUE(getShuffleDemandedElts(4, {7, 0, 7, 7}, APInt(4, 0xd), LHS, RHS));
236   EXPECT_EQ(LHS.getZExtValue(), 0x0U);
237   EXPECT_EQ(RHS.getZExtValue(), 0x8U);
238 
239   // general test
240   EXPECT_TRUE(getShuffleDemandedElts(4, {4, 2, 7, 3}, APInt(4, 0xf), LHS, RHS));
241   EXPECT_EQ(LHS.getZExtValue(), 0xcU);
242   EXPECT_EQ(RHS.getZExtValue(), 0x9U);
243 }
244 
245 TEST_F(BasicTest, getHorizontalDemandedEltsForFirstOperand) {
246   APInt LHS, RHS;
247 
248   getHorizDemandedEltsForFirstOperand(128, APInt(4, 0b0000), LHS, RHS);
249   EXPECT_EQ(LHS.getZExtValue(), 0b0000U);
250   EXPECT_EQ(RHS.getZExtValue(), 0b0000U);
251 
252   getHorizDemandedEltsForFirstOperand(128, APInt(4, 0b0001), LHS, RHS);
253   EXPECT_EQ(LHS.getZExtValue(), 0b0001U);
254   EXPECT_EQ(RHS.getZExtValue(), 0b0000U);
255 
256   getHorizDemandedEltsForFirstOperand(128, APInt(4, 0b1000), LHS, RHS);
257   EXPECT_EQ(LHS.getZExtValue(), 0b0000U);
258   EXPECT_EQ(RHS.getZExtValue(), 0b0100U);
259 
260   getHorizDemandedEltsForFirstOperand(128, APInt(4, 0b0110), LHS, RHS);
261   EXPECT_EQ(LHS.getZExtValue(), 0b0100U);
262   EXPECT_EQ(RHS.getZExtValue(), 0b0001U);
263 
264   getHorizDemandedEltsForFirstOperand(256, APInt(4, 0b0100), LHS, RHS);
265   EXPECT_EQ(LHS.getZExtValue(), 0b0100U);
266   EXPECT_EQ(RHS.getZExtValue(), 0b0000U);
267 }
268 
269 TEST_F(BasicTest, getSplatIndex) {
270   EXPECT_EQ(getSplatIndex({0,0,0}), 0);
271   EXPECT_EQ(getSplatIndex({1,0,0}), -1);     // no splat
272   EXPECT_EQ(getSplatIndex({0,1,1}), -1);     // no splat
273   EXPECT_EQ(getSplatIndex({42,42,42}), 42);  // array size is independent of splat index
274   EXPECT_EQ(getSplatIndex({42,42,-1}), 42);  // ignore negative
275   EXPECT_EQ(getSplatIndex({-1,42,-1}), 42);  // ignore negatives
276   EXPECT_EQ(getSplatIndex({-4,42,-42}), 42); // ignore all negatives
277   EXPECT_EQ(getSplatIndex({-4,-1,-42}), -1); // all negative values map to -1
278 }
279 
280 TEST_F(VectorUtilsTest, isSplatValue_00) {
281   parseAssembly(
282       "define <2 x i8> @test(<2 x i8> %x) {\n"
283       "  %A = shufflevector <2 x i8> %x, <2 x i8> undef, <2 x i32> zeroinitializer\n"
284       "  ret <2 x i8> %A\n"
285       "}\n");
286   EXPECT_TRUE(isSplatValue(A));
287 }
288 
289 TEST_F(VectorUtilsTest, isSplatValue_00_index0) {
290   parseAssembly(
291       "define <2 x i8> @test(<2 x i8> %x) {\n"
292       "  %A = shufflevector <2 x i8> %x, <2 x i8> undef, <2 x i32> zeroinitializer\n"
293       "  ret <2 x i8> %A\n"
294       "}\n");
295   EXPECT_TRUE(isSplatValue(A, 0));
296 }
297 
298 TEST_F(VectorUtilsTest, isSplatValue_00_index1) {
299   parseAssembly(
300       "define <2 x i8> @test(<2 x i8> %x) {\n"
301       "  %A = shufflevector <2 x i8> %x, <2 x i8> undef, <2 x i32> zeroinitializer\n"
302       "  ret <2 x i8> %A\n"
303       "}\n");
304   EXPECT_FALSE(isSplatValue(A, 1));
305 }
306 
307 TEST_F(VectorUtilsTest, isSplatValue_11) {
308   parseAssembly(
309       "define <2 x i8> @test(<2 x i8> %x) {\n"
310       "  %A = shufflevector <2 x i8> %x, <2 x i8> undef, <2 x i32> <i32 1, i32 1>\n"
311       "  ret <2 x i8> %A\n"
312       "}\n");
313   EXPECT_TRUE(isSplatValue(A));
314 }
315 
316 TEST_F(VectorUtilsTest, isSplatValue_11_index0) {
317   parseAssembly(
318       "define <2 x i8> @test(<2 x i8> %x) {\n"
319       "  %A = shufflevector <2 x i8> %x, <2 x i8> undef, <2 x i32> <i32 1, i32 1>\n"
320       "  ret <2 x i8> %A\n"
321       "}\n");
322   EXPECT_FALSE(isSplatValue(A, 0));
323 }
324 
325 TEST_F(VectorUtilsTest, isSplatValue_11_index1) {
326   parseAssembly(
327       "define <2 x i8> @test(<2 x i8> %x) {\n"
328       "  %A = shufflevector <2 x i8> %x, <2 x i8> undef, <2 x i32> <i32 1, i32 1>\n"
329       "  ret <2 x i8> %A\n"
330       "}\n");
331   EXPECT_TRUE(isSplatValue(A, 1));
332 }
333 
334 TEST_F(VectorUtilsTest, isSplatValue_01) {
335   parseAssembly(
336       "define <2 x i8> @test(<2 x i8> %x) {\n"
337       "  %A = shufflevector <2 x i8> %x, <2 x i8> undef, <2 x i32> <i32 0, i32 1>\n"
338       "  ret <2 x i8> %A\n"
339       "}\n");
340   EXPECT_FALSE(isSplatValue(A));
341 }
342 
343 TEST_F(VectorUtilsTest, isSplatValue_01_index0) {
344   parseAssembly(
345       "define <2 x i8> @test(<2 x i8> %x) {\n"
346       "  %A = shufflevector <2 x i8> %x, <2 x i8> undef, <2 x i32> <i32 0, i32 1>\n"
347       "  ret <2 x i8> %A\n"
348       "}\n");
349   EXPECT_FALSE(isSplatValue(A, 0));
350 }
351 
352 TEST_F(VectorUtilsTest, isSplatValue_01_index1) {
353   parseAssembly(
354       "define <2 x i8> @test(<2 x i8> %x) {\n"
355       "  %A = shufflevector <2 x i8> %x, <2 x i8> undef, <2 x i32> <i32 0, i32 1>\n"
356       "  ret <2 x i8> %A\n"
357       "}\n");
358   EXPECT_FALSE(isSplatValue(A, 1));
359 }
360 
361 // FIXME: Allow undef matching with Constant (mask) splat analysis.
362 
363 TEST_F(VectorUtilsTest, isSplatValue_0u) {
364   parseAssembly(
365       "define <2 x i8> @test(<2 x i8> %x) {\n"
366       "  %A = shufflevector <2 x i8> %x, <2 x i8> undef, <2 x i32> <i32 0, i32 undef>\n"
367       "  ret <2 x i8> %A\n"
368       "}\n");
369   EXPECT_FALSE(isSplatValue(A));
370 }
371 
372 // FIXME: Allow undef matching with Constant (mask) splat analysis.
373 
374 TEST_F(VectorUtilsTest, isSplatValue_0u_index0) {
375   parseAssembly(
376       "define <2 x i8> @test(<2 x i8> %x) {\n"
377       "  %A = shufflevector <2 x i8> %x, <2 x i8> undef, <2 x i32> <i32 0, i32 undef>\n"
378       "  ret <2 x i8> %A\n"
379       "}\n");
380   EXPECT_FALSE(isSplatValue(A, 0));
381 }
382 
383 TEST_F(VectorUtilsTest, isSplatValue_0u_index1) {
384   parseAssembly(
385       "define <2 x i8> @test(<2 x i8> %x) {\n"
386       "  %A = shufflevector <2 x i8> %x, <2 x i8> undef, <2 x i32> <i32 0, i32 undef>\n"
387       "  ret <2 x i8> %A\n"
388       "}\n");
389   EXPECT_FALSE(isSplatValue(A, 1));
390 }
391 
392 TEST_F(VectorUtilsTest, isSplatValue_Binop) {
393   parseAssembly(
394       "define <2 x i8> @test(<2 x i8> %x) {\n"
395       "  %v0 = shufflevector <2 x i8> %x, <2 x i8> undef, <2 x i32> <i32 0, i32 0>\n"
396       "  %v1 = shufflevector <2 x i8> %x, <2 x i8> undef, <2 x i32> <i32 1, i32 1>\n"
397       "  %A = udiv <2 x i8> %v0, %v1\n"
398       "  ret <2 x i8> %A\n"
399       "}\n");
400   EXPECT_TRUE(isSplatValue(A));
401 }
402 
403 TEST_F(VectorUtilsTest, isSplatValue_Binop_index0) {
404   parseAssembly(
405       "define <2 x i8> @test(<2 x i8> %x) {\n"
406       "  %v0 = shufflevector <2 x i8> %x, <2 x i8> undef, <2 x i32> <i32 0, i32 0>\n"
407       "  %v1 = shufflevector <2 x i8> %x, <2 x i8> undef, <2 x i32> <i32 1, i32 1>\n"
408       "  %A = udiv <2 x i8> %v0, %v1\n"
409       "  ret <2 x i8> %A\n"
410       "}\n");
411   EXPECT_FALSE(isSplatValue(A, 0));
412 }
413 
414 TEST_F(VectorUtilsTest, isSplatValue_Binop_index1) {
415   parseAssembly(
416       "define <2 x i8> @test(<2 x i8> %x) {\n"
417       "  %v0 = shufflevector <2 x i8> %x, <2 x i8> undef, <2 x i32> <i32 0, i32 0>\n"
418       "  %v1 = shufflevector <2 x i8> %x, <2 x i8> undef, <2 x i32> <i32 1, i32 1>\n"
419       "  %A = udiv <2 x i8> %v0, %v1\n"
420       "  ret <2 x i8> %A\n"
421       "}\n");
422   EXPECT_FALSE(isSplatValue(A, 1));
423 }
424 
425 TEST_F(VectorUtilsTest, isSplatValue_Binop_ConstantOp0) {
426   parseAssembly(
427       "define <2 x i8> @test(<2 x i8> %x) {\n"
428       "  %v1 = shufflevector <2 x i8> %x, <2 x i8> undef, <2 x i32> <i32 1, i32 1>\n"
429       "  %A = ashr <2 x i8> <i8 42, i8 42>, %v1\n"
430       "  ret <2 x i8> %A\n"
431       "}\n");
432   EXPECT_TRUE(isSplatValue(A));
433 }
434 
435 TEST_F(VectorUtilsTest, isSplatValue_Binop_ConstantOp0_index0) {
436   parseAssembly(
437       "define <2 x i8> @test(<2 x i8> %x) {\n"
438       "  %v1 = shufflevector <2 x i8> %x, <2 x i8> undef, <2 x i32> <i32 1, i32 1>\n"
439       "  %A = ashr <2 x i8> <i8 42, i8 42>, %v1\n"
440       "  ret <2 x i8> %A\n"
441       "}\n");
442   EXPECT_FALSE(isSplatValue(A, 0));
443 }
444 
445 TEST_F(VectorUtilsTest, isSplatValue_Binop_ConstantOp0_index1) {
446   parseAssembly(
447       "define <2 x i8> @test(<2 x i8> %x) {\n"
448       "  %v1 = shufflevector <2 x i8> %x, <2 x i8> undef, <2 x i32> <i32 1, i32 1>\n"
449       "  %A = ashr <2 x i8> <i8 42, i8 42>, %v1\n"
450       "  ret <2 x i8> %A\n"
451       "}\n");
452   EXPECT_TRUE(isSplatValue(A, 1));
453 }
454 
455 TEST_F(VectorUtilsTest, isSplatValue_Binop_Not_Op0) {
456   parseAssembly(
457       "define <2 x i8> @test(<2 x i8> %x) {\n"
458       "  %v0 = shufflevector <2 x i8> %x, <2 x i8> undef, <2 x i32> <i32 1, i32 0>\n"
459       "  %v1 = shufflevector <2 x i8> %x, <2 x i8> undef, <2 x i32> <i32 1, i32 1>\n"
460       "  %A = add <2 x i8> %v0, %v1\n"
461       "  ret <2 x i8> %A\n"
462       "}\n");
463   EXPECT_FALSE(isSplatValue(A));
464 }
465 
466 TEST_F(VectorUtilsTest, isSplatValue_Binop_Not_Op1) {
467   parseAssembly(
468       "define <2 x i8> @test(<2 x i8> %x) {\n"
469       "  %v0 = shufflevector <2 x i8> %x, <2 x i8> undef, <2 x i32> <i32 1, i32 1>\n"
470       "  %v1 = shufflevector <2 x i8> %x, <2 x i8> undef, <2 x i32> <i32 0, i32 1>\n"
471       "  %A = shl <2 x i8> %v0, %v1\n"
472       "  ret <2 x i8> %A\n"
473       "}\n");
474   EXPECT_FALSE(isSplatValue(A));
475 }
476 
477 TEST_F(VectorUtilsTest, isSplatValue_Select) {
478   parseAssembly(
479       "define <2 x i8> @test(<2 x i1> %x, <2 x i8> %y, <2 x i8> %z) {\n"
480       "  %v0 = shufflevector <2 x i1> %x, <2 x i1> undef, <2 x i32> <i32 1, i32 1>\n"
481       "  %v1 = shufflevector <2 x i8> %y, <2 x i8> undef, <2 x i32> <i32 0, i32 0>\n"
482       "  %v2 = shufflevector <2 x i8> %z, <2 x i8> undef, <2 x i32> <i32 1, i32 1>\n"
483       "  %A = select <2 x i1> %v0, <2 x i8> %v1, <2 x i8> %v2\n"
484       "  ret <2 x i8> %A\n"
485       "}\n");
486   EXPECT_TRUE(isSplatValue(A));
487 }
488 
489 TEST_F(VectorUtilsTest, isSplatValue_Select_ConstantOp) {
490   parseAssembly(
491       "define <2 x i8> @test(<2 x i1> %x, <2 x i8> %y, <2 x i8> %z) {\n"
492       "  %v0 = shufflevector <2 x i1> %x, <2 x i1> undef, <2 x i32> <i32 1, i32 1>\n"
493       "  %v2 = shufflevector <2 x i8> %z, <2 x i8> undef, <2 x i32> <i32 1, i32 1>\n"
494       "  %A = select <2 x i1> %v0, <2 x i8> <i8 42, i8 42>, <2 x i8> %v2\n"
495       "  ret <2 x i8> %A\n"
496       "}\n");
497   EXPECT_TRUE(isSplatValue(A));
498 }
499 
500 TEST_F(VectorUtilsTest, isSplatValue_Select_NotCond) {
501   parseAssembly(
502       "define <2 x i8> @test(<2 x i1> %x, <2 x i8> %y, <2 x i8> %z) {\n"
503       "  %v1 = shufflevector <2 x i8> %y, <2 x i8> undef, <2 x i32> <i32 0, i32 0>\n"
504       "  %v2 = shufflevector <2 x i8> %z, <2 x i8> undef, <2 x i32> <i32 1, i32 1>\n"
505       "  %A = select <2 x i1> %x, <2 x i8> %v1, <2 x i8> %v2\n"
506       "  ret <2 x i8> %A\n"
507       "}\n");
508   EXPECT_FALSE(isSplatValue(A));
509 }
510 
511 TEST_F(VectorUtilsTest, isSplatValue_Select_NotOp1) {
512   parseAssembly(
513       "define <2 x i8> @test(<2 x i1> %x, <2 x i8> %y, <2 x i8> %z) {\n"
514       "  %v0 = shufflevector <2 x i1> %x, <2 x i1> undef, <2 x i32> <i32 1, i32 1>\n"
515       "  %v2 = shufflevector <2 x i8> %z, <2 x i8> undef, <2 x i32> <i32 1, i32 1>\n"
516       "  %A = select <2 x i1> %v0, <2 x i8> %y, <2 x i8> %v2\n"
517       "  ret <2 x i8> %A\n"
518       "}\n");
519   EXPECT_FALSE(isSplatValue(A));
520 }
521 
522 TEST_F(VectorUtilsTest, isSplatValue_Select_NotOp2) {
523   parseAssembly(
524       "define <2 x i8> @test(<2 x i1> %x, <2 x i8> %y, <2 x i8> %z) {\n"
525       "  %v0 = shufflevector <2 x i1> %x, <2 x i1> undef, <2 x i32> <i32 1, i32 1>\n"
526       "  %v1 = shufflevector <2 x i8> %y, <2 x i8> undef, <2 x i32> <i32 0, i32 0>\n"
527       "  %A = select <2 x i1> %v0, <2 x i8> %v1, <2 x i8> %z\n"
528       "  ret <2 x i8> %A\n"
529       "}\n");
530   EXPECT_FALSE(isSplatValue(A));
531 }
532 
533 TEST_F(VectorUtilsTest, isSplatValue_SelectBinop) {
534   parseAssembly(
535       "define <2 x i8> @test(<2 x i1> %x, <2 x i8> %y, <2 x i8> %z) {\n"
536       "  %v0 = shufflevector <2 x i1> %x, <2 x i1> undef, <2 x i32> <i32 1, i32 1>\n"
537       "  %v1 = shufflevector <2 x i8> %y, <2 x i8> undef, <2 x i32> <i32 0, i32 0>\n"
538       "  %v2 = shufflevector <2 x i8> %z, <2 x i8> undef, <2 x i32> <i32 1, i32 1>\n"
539       "  %bo = xor <2 x i8> %v1, %v2\n"
540       "  %A = select <2 x i1> %v0, <2 x i8> %bo, <2 x i8> %v2\n"
541       "  ret <2 x i8> %A\n"
542       "}\n");
543   EXPECT_TRUE(isSplatValue(A));
544 }
545 
546 TEST_F(VectorUtilsTest, getSplatValueElt0) {
547   parseAssembly(
548       "define <2 x i8> @test(i8 %x) {\n"
549       "  %ins = insertelement <2 x i8> undef, i8 %x, i32 0\n"
550       "  %A = shufflevector <2 x i8> %ins, <2 x i8> undef, <2 x i32> zeroinitializer\n"
551       "  ret <2 x i8> %A\n"
552       "}\n");
553   EXPECT_EQ(getSplatValue(A)->getName(), "x");
554 }
555 
556 TEST_F(VectorUtilsTest, getSplatValueEltMismatch) {
557   parseAssembly(
558       "define <2 x i8> @test(i8 %x) {\n"
559       "  %ins = insertelement <2 x i8> undef, i8 %x, i32 1\n"
560       "  %A = shufflevector <2 x i8> %ins, <2 x i8> undef, <2 x i32> zeroinitializer\n"
561       "  ret <2 x i8> %A\n"
562       "}\n");
563   EXPECT_EQ(getSplatValue(A), nullptr);
564 }
565 
566 // TODO: This is a splat, but we don't recognize it.
567 
568 TEST_F(VectorUtilsTest, getSplatValueElt1) {
569   parseAssembly(
570       "define <2 x i8> @test(i8 %x) {\n"
571       "  %ins = insertelement <2 x i8> undef, i8 %x, i32 1\n"
572       "  %A = shufflevector <2 x i8> %ins, <2 x i8> undef, <2 x i32> <i32 1, i32 1>\n"
573       "  ret <2 x i8> %A\n"
574       "}\n");
575   EXPECT_EQ(getSplatValue(A), nullptr);
576 }
577 
578 ////////////////////////////////////////////////////////////////////////////////
579 // VFShape API tests.
580 ////////////////////////////////////////////////////////////////////////////////
581 
582 class VFShapeAPITest : public testing::Test {
583 protected:
584   void SetUp() override {
585     M = parseAssemblyString(IR, Err, Ctx);
586     // Get the only call instruction in the block, which is the first
587     // instruction.
588     CI = dyn_cast<CallInst>(&*(instructions(M->getFunction("f")).begin()));
589   }
590 
591   const char *IR = "define i32 @f(i32 %a, i64 %b, double %c) {\n"
592                    " %1 = call i32 @g(i32 %a, i64 %b, double %c)\n"
593                    "  ret i32 %1\n"
594                    "}\n"
595                    "declare i32 @g(i32, i64, double)\n";
596   LLVMContext Ctx;
597   SMDiagnostic Err;
598   std::unique_ptr<Module> M;
599   CallInst *CI;
600   // Dummy shape with no parameters, overwritten by buildShape when invoked.
601   VFShape Shape = {/*VF*/ ElementCount::getFixed(2), /*Parameters*/ {}};
602   VFShape Expected;
603   SmallVector<VFParameter, 8> &ExpectedParams = Expected.Parameters;
604 
605   void buildShape(ElementCount VF, bool HasGlobalPred) {
606     Shape = VFShape::get(CI->getFunctionType(), VF, HasGlobalPred);
607   }
608 
609   bool validParams(ArrayRef<VFParameter> Parameters) {
610     Shape.Parameters = SmallVector<VFParameter, 8>(Parameters);
611     return Shape.hasValidParameterList();
612   }
613 };
614 
615 TEST_F(VFShapeAPITest, API_buildVFShape) {
616   buildShape(/*VF*/ ElementCount::getFixed(2), /*HasGlobalPred*/ false);
617   Expected = {/*VF*/ ElementCount::getFixed(2), /*Parameters*/ {
618                   {0, VFParamKind::Vector},
619                   {1, VFParamKind::Vector},
620                   {2, VFParamKind::Vector},
621               }};
622   EXPECT_EQ(Shape, Expected);
623 
624   buildShape(/*VF*/ ElementCount::getFixed(4), /*HasGlobalPred*/ true);
625   Expected = {/*VF*/ ElementCount::getFixed(4), /*Parameters*/ {
626                   {0, VFParamKind::Vector},
627                   {1, VFParamKind::Vector},
628                   {2, VFParamKind::Vector},
629                   {3, VFParamKind::GlobalPredicate},
630               }};
631   EXPECT_EQ(Shape, Expected);
632 
633   buildShape(/*VF*/ ElementCount::getScalable(16), /*HasGlobalPred*/ false);
634   Expected = {/*VF*/ ElementCount::getScalable(16), /*Parameters*/ {
635                   {0, VFParamKind::Vector},
636                   {1, VFParamKind::Vector},
637                   {2, VFParamKind::Vector},
638               }};
639   EXPECT_EQ(Shape, Expected);
640 }
641 
642 TEST_F(VFShapeAPITest, API_getScalarShape) {
643   buildShape(/*VF*/ ElementCount::getFixed(1), /*HasGlobalPred*/ false);
644   EXPECT_EQ(VFShape::getScalarShape(CI->getFunctionType()), Shape);
645 }
646 
647 TEST_F(VFShapeAPITest, API_getVectorizedFunction) {
648   VFShape ScalarShape = VFShape::getScalarShape(CI->getFunctionType());
649   EXPECT_EQ(VFDatabase(*CI).getVectorizedFunction(ScalarShape),
650             M->getFunction("g"));
651 
652   buildShape(/*VF*/ ElementCount::getScalable(1), /*HasGlobalPred*/ false);
653   EXPECT_EQ(VFDatabase(*CI).getVectorizedFunction(Shape), nullptr);
654   buildShape(/*VF*/ ElementCount::getFixed(1), /*HasGlobalPred*/ true);
655   EXPECT_EQ(VFDatabase(*CI).getVectorizedFunction(Shape), nullptr);
656   buildShape(/*VF*/ ElementCount::getScalable(1), /*HasGlobalPred*/ true);
657   EXPECT_EQ(VFDatabase(*CI).getVectorizedFunction(Shape), nullptr);
658 }
659 
660 TEST_F(VFShapeAPITest, API_updateVFShape) {
661 
662   buildShape(/*VF*/ ElementCount::getFixed(2), /*HasGlobalPred*/ false);
663   Shape.updateParam({0 /*Pos*/, VFParamKind::OMP_Linear, 1, Align(4)});
664   Expected = {/*VF*/ ElementCount::getFixed(2), /*Parameters*/ {
665                   {0, VFParamKind::OMP_Linear, 1, Align(4)},
666                   {1, VFParamKind::Vector},
667                   {2, VFParamKind::Vector},
668               }};
669   EXPECT_EQ(Shape, Expected);
670 
671   // From this point on, we update only the parameters of the VFShape,
672   // so we update only the reference of the expected Parameters.
673   Shape.updateParam({1 /*Pos*/, VFParamKind::OMP_Uniform});
674   ExpectedParams = {
675       {0, VFParamKind::OMP_Linear, 1, Align(4)},
676       {1, VFParamKind::OMP_Uniform},
677       {2, VFParamKind::Vector},
678   };
679   EXPECT_EQ(Shape, Expected);
680 
681   Shape.updateParam({2 /*Pos*/, VFParamKind::OMP_LinearRefPos, 1});
682   ExpectedParams = {
683       {0, VFParamKind::OMP_Linear, 1, Align(4)},
684       {1, VFParamKind::OMP_Uniform},
685       {2, VFParamKind::OMP_LinearRefPos, 1},
686   };
687   EXPECT_EQ(Shape, Expected);
688 }
689 
690 TEST_F(VFShapeAPITest, API_updateVFShape_GlobalPredicate) {
691 
692   buildShape(/*VF*/ ElementCount::getScalable(2), /*HasGlobalPred*/ true);
693   Shape.updateParam({1 /*Pos*/, VFParamKind::OMP_Uniform});
694   Expected = {/*VF*/ ElementCount::getScalable(2),
695               /*Parameters*/ {{0, VFParamKind::Vector},
696                               {1, VFParamKind::OMP_Uniform},
697                               {2, VFParamKind::Vector},
698                               {3, VFParamKind::GlobalPredicate}}};
699   EXPECT_EQ(Shape, Expected);
700 }
701 
702 TEST_F(VFShapeAPITest, Parameters_Valid) {
703   // ParamPos in order.
704   EXPECT_TRUE(validParams({{0, VFParamKind::Vector}}));
705   EXPECT_TRUE(
706       validParams({{0, VFParamKind::Vector}, {1, VFParamKind::Vector}}));
707   EXPECT_TRUE(validParams({{0, VFParamKind::Vector},
708                            {1, VFParamKind::Vector},
709                            {2, VFParamKind::Vector}}));
710 
711   // GlocalPredicate is unique.
712   EXPECT_TRUE(validParams({{0, VFParamKind::Vector},
713                            {1, VFParamKind::Vector},
714                            {2, VFParamKind::Vector},
715                            {3, VFParamKind::GlobalPredicate}}));
716 
717   EXPECT_TRUE(validParams({{0, VFParamKind::Vector},
718                            {1, VFParamKind::GlobalPredicate},
719                            {2, VFParamKind::Vector}}));
720 }
721 
722 TEST_F(VFShapeAPITest, Parameters_ValidOpenMPLinear) {
723 // Valid linear constant step (>0).
724 #define __BUILD_PARAMETERS(Kind, Val)                                          \
725   {                                                                            \
726     { 0, Kind, Val }                                                           \
727   }
728   EXPECT_TRUE(validParams(__BUILD_PARAMETERS(VFParamKind::OMP_Linear, 1)));
729   EXPECT_TRUE(validParams(__BUILD_PARAMETERS(VFParamKind::OMP_LinearRef, 2)));
730   EXPECT_TRUE(validParams(__BUILD_PARAMETERS(VFParamKind::OMP_LinearVal, 4)));
731   EXPECT_TRUE(validParams(__BUILD_PARAMETERS(VFParamKind::OMP_LinearUVal, 33)));
732 #undef __BUILD_PARAMETERS
733 
734 // Valid linear runtime step (the step parameter is marked uniform).
735 #define __BUILD_PARAMETERS(Kind)                                               \
736   {                                                                            \
737     {0, VFParamKind::OMP_Uniform}, {1, VFParamKind::Vector}, { 2, Kind, 0 }    \
738   }
739   EXPECT_TRUE(validParams(__BUILD_PARAMETERS(VFParamKind::OMP_LinearPos)));
740   EXPECT_TRUE(validParams(__BUILD_PARAMETERS(VFParamKind::OMP_LinearRefPos)));
741   EXPECT_TRUE(validParams(__BUILD_PARAMETERS(VFParamKind::OMP_LinearValPos)));
742   EXPECT_TRUE(validParams(__BUILD_PARAMETERS(VFParamKind::OMP_LinearUValPos)));
743 #undef __BUILD_PARAMETERS
744 }
745 
746 TEST_F(VFShapeAPITest, Parameters_Invalid) {
747 #ifndef NDEBUG
748   // Wrong order is checked by an assertion: make sure that the
749   // assertion is not removed.
750   EXPECT_DEATH(validParams({{1, VFParamKind::Vector}}),
751                "Broken parameter list.");
752   EXPECT_DEATH(
753       validParams({{1, VFParamKind::Vector}, {0, VFParamKind::Vector}}),
754       "Broken parameter list.");
755 #endif
756 
757   // GlobalPredicate is not unique
758   EXPECT_FALSE(validParams({{0, VFParamKind::Vector},
759                             {1, VFParamKind::GlobalPredicate},
760                             {2, VFParamKind::GlobalPredicate}}));
761   EXPECT_FALSE(validParams({{0, VFParamKind::GlobalPredicate},
762                             {1, VFParamKind::Vector},
763                             {2, VFParamKind::GlobalPredicate}}));
764 }
765 
766 TEST_F(VFShapeAPITest, Parameters_InvalidOpenMPLinear) {
767 // Compile time linear steps must be non-zero (compile time invariant).
768 #define __BUILD_PARAMETERS(Kind)                                               \
769   {                                                                            \
770     { 0, Kind, 0 }                                                             \
771   }
772   EXPECT_FALSE(validParams(__BUILD_PARAMETERS(VFParamKind::OMP_Linear)));
773   EXPECT_FALSE(validParams(__BUILD_PARAMETERS(VFParamKind::OMP_LinearRef)));
774   EXPECT_FALSE(validParams(__BUILD_PARAMETERS(VFParamKind::OMP_LinearVal)));
775   EXPECT_FALSE(validParams(__BUILD_PARAMETERS(VFParamKind::OMP_LinearUVal)));
776 #undef __BUILD_PARAMETERS
777 
778 // The step of a runtime linear parameter must be marked
779 // as uniform (runtime invariant).
780 #define __BUILD_PARAMETERS(Kind)                                               \
781   {                                                                            \
782     {0, VFParamKind::OMP_Uniform}, {1, VFParamKind::Vector}, { 2, Kind, 1 }    \
783   }
784   EXPECT_FALSE(validParams(__BUILD_PARAMETERS(VFParamKind::OMP_LinearPos)));
785   EXPECT_FALSE(validParams(__BUILD_PARAMETERS(VFParamKind::OMP_LinearRefPos)));
786   EXPECT_FALSE(validParams(__BUILD_PARAMETERS(VFParamKind::OMP_LinearValPos)));
787   EXPECT_FALSE(validParams(__BUILD_PARAMETERS(VFParamKind::OMP_LinearUValPos)));
788 #undef __BUILD_PARAMETERS
789 
790 // The linear step parameter can't point at itself.
791 #define __BUILD_PARAMETERS(Kind)                                               \
792   {                                                                            \
793     {0, VFParamKind::Vector}, {1, VFParamKind::Vector}, { 2, Kind, 2 }         \
794   }
795   EXPECT_FALSE(validParams(__BUILD_PARAMETERS(VFParamKind::OMP_LinearPos)));
796   EXPECT_FALSE(validParams(__BUILD_PARAMETERS(VFParamKind::OMP_LinearRefPos)));
797   EXPECT_FALSE(validParams(__BUILD_PARAMETERS(VFParamKind::OMP_LinearValPos)));
798   EXPECT_FALSE(validParams(__BUILD_PARAMETERS(VFParamKind::OMP_LinearUValPos)));
799 #undef __BUILD_PARAMETERS
800 
801 // Linear parameter (runtime) is out of range.
802 #define __BUILD_PARAMETERS(Kind)                                               \
803   {                                                                            \
804     {0, VFParamKind::Vector}, {1, VFParamKind::Vector}, { 2, Kind, 3 }         \
805   }
806   EXPECT_FALSE(validParams(__BUILD_PARAMETERS(VFParamKind::OMP_LinearPos)));
807   EXPECT_FALSE(validParams(__BUILD_PARAMETERS(VFParamKind::OMP_LinearRefPos)));
808   EXPECT_FALSE(validParams(__BUILD_PARAMETERS(VFParamKind::OMP_LinearValPos)));
809   EXPECT_FALSE(validParams(__BUILD_PARAMETERS(VFParamKind::OMP_LinearUValPos)));
810 #undef __BUILD_PARAMETERS
811 }
812