blob: 8ff6744bf7806df94b8ef65941732c6f202a22f0 [file] [log] [blame]
Sanjay Patelb0f98d32019-06-10 18:19:05 +00001//===- 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
21using namespace llvm;
22
23namespace {
24
25class VectorUtilsTest : public testing::Test {
26protected:
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
61TEST_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
71TEST_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
83TEST_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}