1 //===- VectorUtilsTest.cpp - VectorUtils tests ------------------------===//
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
7 //===----------------------------------------------------------------------===//
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 "llvm/Support/KnownBits.h"
21 #include "gtest/gtest.h"
27 class VectorUtilsTest
: public testing::Test
{
29 void parseAssembly(const char *Assembly
) {
31 M
= parseAssemblyString(Assembly
, Error
, Context
);
34 raw_string_ostream
os(errMsg
);
37 // A failure here means that the test itself is buggy.
39 report_fatal_error(os
.str());
41 Function
*F
= M
->getFunction("test");
43 report_fatal_error("Test must have a function named @test");
46 for (inst_iterator I
= inst_begin(F
), E
= inst_end(F
); I
!= E
; ++I
) {
48 if (I
->getName() == "A")
53 report_fatal_error("@test must have an instruction %A");
57 std::unique_ptr
<Module
> M
;
61 struct BasicTest
: public testing::Test
{
63 std::unique_ptr
<Module
> M
;
66 IRBuilder
<NoFolder
> IRB
;
69 : M(new Module("VectorUtils", Ctx
)),
71 FunctionType::get(Type::getVoidTy(Ctx
), /* IsVarArg */ false),
72 Function::ExternalLinkage
, "f", M
.get())),
73 BB(BasicBlock::Create(Ctx
, "entry", F
)), IRB(BB
) {}
79 TEST_F(BasicTest
, isSplat
) {
80 Value
*UndefVec
= UndefValue::get(VectorType::get(IRB
.getInt8Ty(), 4));
81 EXPECT_TRUE(isSplatValue(UndefVec
));
83 Constant
*UndefScalar
= UndefValue::get(IRB
.getInt8Ty());
84 EXPECT_FALSE(isSplatValue(UndefScalar
));
86 Constant
*ScalarC
= IRB
.getInt8(42);
87 EXPECT_FALSE(isSplatValue(ScalarC
));
89 Constant
*OtherScalarC
= IRB
.getInt8(-42);
90 Constant
*NonSplatC
= ConstantVector::get({ScalarC
, OtherScalarC
});
91 EXPECT_FALSE(isSplatValue(NonSplatC
));
93 Value
*SplatC
= IRB
.CreateVectorSplat(5, ScalarC
);
94 EXPECT_TRUE(isSplatValue(SplatC
));
96 // FIXME: Constant splat analysis does not allow undef elements.
97 Constant
*SplatWithUndefC
= ConstantVector::get({ScalarC
, UndefScalar
});
98 EXPECT_FALSE(isSplatValue(SplatWithUndefC
));
101 TEST_F(VectorUtilsTest
, isSplatValue_00
) {
103 "define <2 x i8> @test(<2 x i8> %x) {\n"
104 " %A = shufflevector <2 x i8> %x, <2 x i8> undef, <2 x i32> zeroinitializer\n"
107 EXPECT_TRUE(isSplatValue(A
));
110 TEST_F(VectorUtilsTest
, isSplatValue_11
) {
112 "define <2 x i8> @test(<2 x i8> %x) {\n"
113 " %A = shufflevector <2 x i8> %x, <2 x i8> undef, <2 x i32> <i32 1, i32 1>\n"
116 EXPECT_TRUE(isSplatValue(A
));
119 TEST_F(VectorUtilsTest
, isSplatValue_01
) {
121 "define <2 x i8> @test(<2 x i8> %x) {\n"
122 " %A = shufflevector <2 x i8> %x, <2 x i8> undef, <2 x i32> <i32 0, i32 1>\n"
125 EXPECT_FALSE(isSplatValue(A
));
128 // FIXME: Constant (mask) splat analysis does not allow undef elements.
130 TEST_F(VectorUtilsTest
, isSplatValue_0u
) {
132 "define <2 x i8> @test(<2 x i8> %x) {\n"
133 " %A = shufflevector <2 x i8> %x, <2 x i8> undef, <2 x i32> <i32 0, i32 undef>\n"
136 EXPECT_FALSE(isSplatValue(A
));
139 TEST_F(VectorUtilsTest
, isSplatValue_Binop
) {
141 "define <2 x i8> @test(<2 x i8> %x) {\n"
142 " %v0 = shufflevector <2 x i8> %x, <2 x i8> undef, <2 x i32> <i32 0, i32 0>\n"
143 " %v1 = shufflevector <2 x i8> %x, <2 x i8> undef, <2 x i32> <i32 1, i32 1>\n"
144 " %A = udiv <2 x i8> %v0, %v1\n"
147 EXPECT_TRUE(isSplatValue(A
));
150 TEST_F(VectorUtilsTest
, isSplatValue_Binop_ConstantOp0
) {
152 "define <2 x i8> @test(<2 x i8> %x) {\n"
153 " %v1 = shufflevector <2 x i8> %x, <2 x i8> undef, <2 x i32> <i32 1, i32 1>\n"
154 " %A = ashr <2 x i8> <i8 42, i8 42>, %v1\n"
157 EXPECT_TRUE(isSplatValue(A
));
160 TEST_F(VectorUtilsTest
, isSplatValue_Binop_Not_Op0
) {
162 "define <2 x i8> @test(<2 x i8> %x) {\n"
163 " %v0 = shufflevector <2 x i8> %x, <2 x i8> undef, <2 x i32> <i32 1, i32 0>\n"
164 " %v1 = shufflevector <2 x i8> %x, <2 x i8> undef, <2 x i32> <i32 1, i32 1>\n"
165 " %A = add <2 x i8> %v0, %v1\n"
168 EXPECT_FALSE(isSplatValue(A
));
171 TEST_F(VectorUtilsTest
, isSplatValue_Binop_Not_Op1
) {
173 "define <2 x i8> @test(<2 x i8> %x) {\n"
174 " %v0 = shufflevector <2 x i8> %x, <2 x i8> undef, <2 x i32> <i32 1, i32 1>\n"
175 " %v1 = shufflevector <2 x i8> %x, <2 x i8> undef, <2 x i32> <i32 0, i32 1>\n"
176 " %A = shl <2 x i8> %v0, %v1\n"
179 EXPECT_FALSE(isSplatValue(A
));
182 TEST_F(VectorUtilsTest
, isSplatValue_Select
) {
184 "define <2 x i8> @test(<2 x i1> %x, <2 x i8> %y, <2 x i8> %z) {\n"
185 " %v0 = shufflevector <2 x i1> %x, <2 x i1> undef, <2 x i32> <i32 1, i32 1>\n"
186 " %v1 = shufflevector <2 x i8> %y, <2 x i8> undef, <2 x i32> <i32 0, i32 0>\n"
187 " %v2 = shufflevector <2 x i8> %z, <2 x i8> undef, <2 x i32> <i32 1, i32 1>\n"
188 " %A = select <2 x i1> %v0, <2 x i8> %v1, <2 x i8> %v2\n"
191 EXPECT_TRUE(isSplatValue(A
));
194 TEST_F(VectorUtilsTest
, isSplatValue_Select_ConstantOp
) {
196 "define <2 x i8> @test(<2 x i1> %x, <2 x i8> %y, <2 x i8> %z) {\n"
197 " %v0 = shufflevector <2 x i1> %x, <2 x i1> undef, <2 x i32> <i32 1, i32 1>\n"
198 " %v2 = shufflevector <2 x i8> %z, <2 x i8> undef, <2 x i32> <i32 1, i32 1>\n"
199 " %A = select <2 x i1> %v0, <2 x i8> <i8 42, i8 42>, <2 x i8> %v2\n"
202 EXPECT_TRUE(isSplatValue(A
));
205 TEST_F(VectorUtilsTest
, isSplatValue_Select_NotCond
) {
207 "define <2 x i8> @test(<2 x i1> %x, <2 x i8> %y, <2 x i8> %z) {\n"
208 " %v1 = shufflevector <2 x i8> %y, <2 x i8> undef, <2 x i32> <i32 0, i32 0>\n"
209 " %v2 = shufflevector <2 x i8> %z, <2 x i8> undef, <2 x i32> <i32 1, i32 1>\n"
210 " %A = select <2 x i1> %x, <2 x i8> %v1, <2 x i8> %v2\n"
213 EXPECT_FALSE(isSplatValue(A
));
216 TEST_F(VectorUtilsTest
, isSplatValue_Select_NotOp1
) {
218 "define <2 x i8> @test(<2 x i1> %x, <2 x i8> %y, <2 x i8> %z) {\n"
219 " %v0 = shufflevector <2 x i1> %x, <2 x i1> undef, <2 x i32> <i32 1, i32 1>\n"
220 " %v2 = shufflevector <2 x i8> %z, <2 x i8> undef, <2 x i32> <i32 1, i32 1>\n"
221 " %A = select <2 x i1> %v0, <2 x i8> %y, <2 x i8> %v2\n"
224 EXPECT_FALSE(isSplatValue(A
));
227 TEST_F(VectorUtilsTest
, isSplatValue_Select_NotOp2
) {
229 "define <2 x i8> @test(<2 x i1> %x, <2 x i8> %y, <2 x i8> %z) {\n"
230 " %v0 = shufflevector <2 x i1> %x, <2 x i1> undef, <2 x i32> <i32 1, i32 1>\n"
231 " %v1 = shufflevector <2 x i8> %y, <2 x i8> undef, <2 x i32> <i32 0, i32 0>\n"
232 " %A = select <2 x i1> %v0, <2 x i8> %v1, <2 x i8> %z\n"
235 EXPECT_FALSE(isSplatValue(A
));
238 TEST_F(VectorUtilsTest
, isSplatValue_SelectBinop
) {
240 "define <2 x i8> @test(<2 x i1> %x, <2 x i8> %y, <2 x i8> %z) {\n"
241 " %v0 = shufflevector <2 x i1> %x, <2 x i1> undef, <2 x i32> <i32 1, i32 1>\n"
242 " %v1 = shufflevector <2 x i8> %y, <2 x i8> undef, <2 x i32> <i32 0, i32 0>\n"
243 " %v2 = shufflevector <2 x i8> %z, <2 x i8> undef, <2 x i32> <i32 1, i32 1>\n"
244 " %bo = xor <2 x i8> %v1, %v2\n"
245 " %A = select <2 x i1> %v0, <2 x i8> %bo, <2 x i8> %v2\n"
248 EXPECT_TRUE(isSplatValue(A
));
251 TEST_F(VectorUtilsTest
, getSplatValueElt0
) {
253 "define <2 x i8> @test(i8 %x) {\n"
254 " %ins = insertelement <2 x i8> undef, i8 %x, i32 0\n"
255 " %A = shufflevector <2 x i8> %ins, <2 x i8> undef, <2 x i32> zeroinitializer\n"
258 EXPECT_EQ(getSplatValue(A
)->getName(), "x");
261 TEST_F(VectorUtilsTest
, getSplatValueEltMismatch
) {
263 "define <2 x i8> @test(i8 %x) {\n"
264 " %ins = insertelement <2 x i8> undef, i8 %x, i32 1\n"
265 " %A = shufflevector <2 x i8> %ins, <2 x i8> undef, <2 x i32> zeroinitializer\n"
268 EXPECT_EQ(getSplatValue(A
), nullptr);
271 // TODO: This is a splat, but we don't recognize it.
273 TEST_F(VectorUtilsTest
, getSplatValueElt1
) {
275 "define <2 x i8> @test(i8 %x) {\n"
276 " %ins = insertelement <2 x i8> undef, i8 %x, i32 1\n"
277 " %A = shufflevector <2 x i8> %ins, <2 x i8> undef, <2 x i32> <i32 1, i32 1>\n"
280 EXPECT_EQ(getSplatValue(A
), nullptr);