llvm.org GIT mirror llvm / 10f7551
[Analysis] add unit test file for VectorUtils; NFC git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@362972 91177308-0d34-0410-b5e6-96231b3b80d8 Sanjay Patel 3 months ago
2 changed file(s) with 92 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
3333 UnrollAnalyzerTest.cpp
3434 ValueLatticeTest.cpp
3535 ValueTrackingTest.cpp
36 VectorUtilsTest.cpp
3637 )
0 //===- VectorUtilsTest.cpp - VectorUtils tests ------------------------===//
1 //
2 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
3 // See https://llvm.org/LICENSE.txt for license information.
4 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5 //
6 //===----------------------------------------------------------------------===//
7
8 #include "llvm/Analysis/VectorUtils.h"
9 #include "llvm/Analysis/ValueTracking.h"
10 #include "llvm/AsmParser/Parser.h"
11 #include "llvm/IR/Function.h"
12 #include "llvm/IR/InstIterator.h"
13 #include "llvm/IR/LLVMContext.h"
14 #include "llvm/IR/Module.h"
15 #include "llvm/Support/ErrorHandling.h"
16 #include "llvm/Support/SourceMgr.h"
17 #include "llvm/Support/KnownBits.h"
18 #include "gtest/gtest.h"
19
20 using namespace llvm;
21
22 namespace {
23
24 class VectorUtilsTest : public testing::Test {
25 protected:
26 void parseAssembly(const char *Assembly) {
27 SMDiagnostic Error;
28 M = parseAssemblyString(Assembly, Error, Context);
29
30 std::string errMsg;
31 raw_string_ostream os(errMsg);
32 Error.print("", os);
33
34 // A failure here means that the test itself is buggy.
35 if (!M)
36 report_fatal_error(os.str());
37
38 Function *F = M->getFunction("test");
39 if (F == nullptr)
40 report_fatal_error("Test must have a function named @test");
41
42 A = nullptr;
43 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) {
44 if (I->hasName()) {
45 if (I->getName() == "A")
46 A = &*I;
47 }
48 }
49 if (A == nullptr)
50 report_fatal_error("@test must have an instruction %A");
51 }
52
53 LLVMContext Context;
54 std::unique_ptr M;
55 Instruction *A;
56 };
57
58 } // namespace
59
60 TEST_F(VectorUtilsTest, getSplatValueElt0) {
61 parseAssembly(
62 "define <2 x i8> @test(i8 %x) {\n"
63 " %ins = insertelement <2 x i8> undef, i8 %x, i32 0\n"
64 " %A = shufflevector <2 x i8> %ins, <2 x i8> undef, <2 x i32> zeroinitializer\n"
65 " ret <2 x i8> %A\n"
66 "}\n");
67 EXPECT_EQ(getSplatValue(A)->getName(), "x");
68 }
69
70 TEST_F(VectorUtilsTest, getSplatValueEltMismatch) {
71 parseAssembly(
72 "define <2 x i8> @test(i8 %x) {\n"
73 " %ins = insertelement <2 x i8> undef, i8 %x, i32 1\n"
74 " %A = shufflevector <2 x i8> %ins, <2 x i8> undef, <2 x i32> zeroinitializer\n"
75 " ret <2 x i8> %A\n"
76 "}\n");
77 EXPECT_EQ(getSplatValue(A), nullptr);
78 }
79
80 // TODO: This is a splat, but we don't recognize it.
81
82 TEST_F(VectorUtilsTest, getSplatValueElt1) {
83 parseAssembly(
84 "define <2 x i8> @test(i8 %x) {\n"
85 " %ins = insertelement <2 x i8> undef, i8 %x, i32 1\n"
86 " %A = shufflevector <2 x i8> %ins, <2 x i8> undef, <2 x i32> \n"
87 " ret <2 x i8> %A\n"
88 "}\n");
89 EXPECT_EQ(getSplatValue(A), nullptr);
90 }