Skip to content

Commit b0f98d3

Browse files
committed
[Analysis] add unit test file for VectorUtils; NFC
llvm-svn: 362972
1 parent 5d84ccb commit b0f98d3

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed

llvm/unittests/Analysis/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,5 @@ add_llvm_unittest(AnalysisTests
3434
UnrollAnalyzerTest.cpp
3535
ValueLatticeTest.cpp
3636
ValueTrackingTest.cpp
37+
VectorUtilsTest.cpp
3738
)
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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/LLVMContext.h"
15+
#include "llvm/IR/Module.h"
16+
#include "llvm/Support/ErrorHandling.h"
17+
#include "llvm/Support/SourceMgr.h"
18+
#include "llvm/Support/KnownBits.h"
19+
#include "gtest/gtest.h"
20+
21+
using namespace llvm;
22+
23+
namespace {
24+
25+
class VectorUtilsTest : public testing::Test {
26+
protected:
27+
void parseAssembly(const char *Assembly) {
28+
SMDiagnostic Error;
29+
M = parseAssemblyString(Assembly, Error, Context);
30+
31+
std::string errMsg;
32+
raw_string_ostream os(errMsg);
33+
Error.print("", os);
34+
35+
// A failure here means that the test itself is buggy.
36+
if (!M)
37+
report_fatal_error(os.str());
38+
39+
Function *F = M->getFunction("test");
40+
if (F == nullptr)
41+
report_fatal_error("Test must have a function named @test");
42+
43+
A = nullptr;
44+
for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) {
45+
if (I->hasName()) {
46+
if (I->getName() == "A")
47+
A = &*I;
48+
}
49+
}
50+
if (A == nullptr)
51+
report_fatal_error("@test must have an instruction %A");
52+
}
53+
54+
LLVMContext Context;
55+
std::unique_ptr<Module> M;
56+
Instruction *A;
57+
};
58+
59+
} // namespace
60+
61+
TEST_F(VectorUtilsTest, getSplatValueElt0) {
62+
parseAssembly(
63+
"define <2 x i8> @test(i8 %x) {\n"
64+
" %ins = insertelement <2 x i8> undef, i8 %x, i32 0\n"
65+
" %A = shufflevector <2 x i8> %ins, <2 x i8> undef, <2 x i32> zeroinitializer\n"
66+
" ret <2 x i8> %A\n"
67+
"}\n");
68+
EXPECT_EQ(getSplatValue(A)->getName(), "x");
69+
}
70+
71+
TEST_F(VectorUtilsTest, getSplatValueEltMismatch) {
72+
parseAssembly(
73+
"define <2 x i8> @test(i8 %x) {\n"
74+
" %ins = insertelement <2 x i8> undef, i8 %x, i32 1\n"
75+
" %A = shufflevector <2 x i8> %ins, <2 x i8> undef, <2 x i32> zeroinitializer\n"
76+
" ret <2 x i8> %A\n"
77+
"}\n");
78+
EXPECT_EQ(getSplatValue(A), nullptr);
79+
}
80+
81+
// TODO: This is a splat, but we don't recognize it.
82+
83+
TEST_F(VectorUtilsTest, getSplatValueElt1) {
84+
parseAssembly(
85+
"define <2 x i8> @test(i8 %x) {\n"
86+
" %ins = insertelement <2 x i8> undef, i8 %x, i32 1\n"
87+
" %A = shufflevector <2 x i8> %ins, <2 x i8> undef, <2 x i32> <i32 1, i32 1>\n"
88+
" ret <2 x i8> %A\n"
89+
"}\n");
90+
EXPECT_EQ(getSplatValue(A), nullptr);
91+
}

0 commit comments

Comments
 (0)