[llvm-nm] - Add a test case for case when we dump a symbol that belongs to a section...
[llvm-complete.git] / unittests / Analysis / VectorUtilsTest.cpp
bloba33fdb503bb42f7088afcb1ebd5e216e9f41e4a3
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 //===----------------------------------------------------------------------===//
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"
23 using namespace llvm;
25 namespace {
27 class VectorUtilsTest : public testing::Test {
28 protected:
29 void parseAssembly(const char *Assembly) {
30 SMDiagnostic Error;
31 M = parseAssemblyString(Assembly, Error, Context);
33 std::string errMsg;
34 raw_string_ostream os(errMsg);
35 Error.print("", os);
37 // A failure here means that the test itself is buggy.
38 if (!M)
39 report_fatal_error(os.str());
41 Function *F = M->getFunction("test");
42 if (F == nullptr)
43 report_fatal_error("Test must have a function named @test");
45 A = nullptr;
46 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) {
47 if (I->hasName()) {
48 if (I->getName() == "A")
49 A = &*I;
52 if (A == nullptr)
53 report_fatal_error("@test must have an instruction %A");
56 LLVMContext Context;
57 std::unique_ptr<Module> M;
58 Instruction *A;
61 struct BasicTest : public testing::Test {
62 LLVMContext Ctx;
63 std::unique_ptr<Module> M;
64 Function *F;
65 BasicBlock *BB;
66 IRBuilder<NoFolder> IRB;
68 BasicTest()
69 : M(new Module("VectorUtils", Ctx)),
70 F(Function::Create(
71 FunctionType::get(Type::getVoidTy(Ctx), /* IsVarArg */ false),
72 Function::ExternalLinkage, "f", M.get())),
73 BB(BasicBlock::Create(Ctx, "entry", F)), IRB(BB) {}
77 } // namespace
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) {
102 parseAssembly(
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"
105 " ret <2 x i8> %A\n"
106 "}\n");
107 EXPECT_TRUE(isSplatValue(A));
110 TEST_F(VectorUtilsTest, isSplatValue_11) {
111 parseAssembly(
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"
114 " ret <2 x i8> %A\n"
115 "}\n");
116 EXPECT_TRUE(isSplatValue(A));
119 TEST_F(VectorUtilsTest, isSplatValue_01) {
120 parseAssembly(
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"
123 " ret <2 x i8> %A\n"
124 "}\n");
125 EXPECT_FALSE(isSplatValue(A));
128 // FIXME: Constant (mask) splat analysis does not allow undef elements.
130 TEST_F(VectorUtilsTest, isSplatValue_0u) {
131 parseAssembly(
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"
134 " ret <2 x i8> %A\n"
135 "}\n");
136 EXPECT_FALSE(isSplatValue(A));
139 TEST_F(VectorUtilsTest, isSplatValue_Binop) {
140 parseAssembly(
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"
145 " ret <2 x i8> %A\n"
146 "}\n");
147 EXPECT_TRUE(isSplatValue(A));
150 TEST_F(VectorUtilsTest, isSplatValue_Binop_ConstantOp0) {
151 parseAssembly(
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"
155 " ret <2 x i8> %A\n"
156 "}\n");
157 EXPECT_TRUE(isSplatValue(A));
160 TEST_F(VectorUtilsTest, isSplatValue_Binop_Not_Op0) {
161 parseAssembly(
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"
166 " ret <2 x i8> %A\n"
167 "}\n");
168 EXPECT_FALSE(isSplatValue(A));
171 TEST_F(VectorUtilsTest, isSplatValue_Binop_Not_Op1) {
172 parseAssembly(
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"
177 " ret <2 x i8> %A\n"
178 "}\n");
179 EXPECT_FALSE(isSplatValue(A));
182 TEST_F(VectorUtilsTest, isSplatValue_Select) {
183 parseAssembly(
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"
189 " ret <2 x i8> %A\n"
190 "}\n");
191 EXPECT_TRUE(isSplatValue(A));
194 TEST_F(VectorUtilsTest, isSplatValue_Select_ConstantOp) {
195 parseAssembly(
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"
200 " ret <2 x i8> %A\n"
201 "}\n");
202 EXPECT_TRUE(isSplatValue(A));
205 TEST_F(VectorUtilsTest, isSplatValue_Select_NotCond) {
206 parseAssembly(
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"
211 " ret <2 x i8> %A\n"
212 "}\n");
213 EXPECT_FALSE(isSplatValue(A));
216 TEST_F(VectorUtilsTest, isSplatValue_Select_NotOp1) {
217 parseAssembly(
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"
222 " ret <2 x i8> %A\n"
223 "}\n");
224 EXPECT_FALSE(isSplatValue(A));
227 TEST_F(VectorUtilsTest, isSplatValue_Select_NotOp2) {
228 parseAssembly(
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"
233 " ret <2 x i8> %A\n"
234 "}\n");
235 EXPECT_FALSE(isSplatValue(A));
238 TEST_F(VectorUtilsTest, isSplatValue_SelectBinop) {
239 parseAssembly(
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"
246 " ret <2 x i8> %A\n"
247 "}\n");
248 EXPECT_TRUE(isSplatValue(A));
251 TEST_F(VectorUtilsTest, getSplatValueElt0) {
252 parseAssembly(
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"
256 " ret <2 x i8> %A\n"
257 "}\n");
258 EXPECT_EQ(getSplatValue(A)->getName(), "x");
261 TEST_F(VectorUtilsTest, getSplatValueEltMismatch) {
262 parseAssembly(
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"
266 " ret <2 x i8> %A\n"
267 "}\n");
268 EXPECT_EQ(getSplatValue(A), nullptr);
271 // TODO: This is a splat, but we don't recognize it.
273 TEST_F(VectorUtilsTest, getSplatValueElt1) {
274 parseAssembly(
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"
278 " ret <2 x i8> %A\n"
279 "}\n");
280 EXPECT_EQ(getSplatValue(A), nullptr);