TargetTest.cpp
4.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
//===-- TargetTest.cpp ------------------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "Target.h"
#include <cassert>
#include <memory>
#include "MCTargetDesc/MipsMCTargetDesc.h"
#include "llvm/Support/TargetRegistry.h"
#include "llvm/Support/TargetSelect.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
namespace llvm {
namespace exegesis {
void InitializeMipsExegesisTarget();
namespace {
using testing::AllOf;
using testing::ElementsAre;
using testing::Eq;
using testing::Matcher;
using testing::Property;
Matcher<MCOperand> IsImm(int64_t Value) {
return AllOf(Property(&MCOperand::isImm, Eq(true)),
Property(&MCOperand::getImm, Eq(Value)));
}
Matcher<MCOperand> IsReg(unsigned Reg) {
return AllOf(Property(&MCOperand::isReg, Eq(true)),
Property(&MCOperand::getReg, Eq(Reg)));
}
Matcher<MCInst> OpcodeIs(unsigned Opcode) {
return Property(&MCInst::getOpcode, Eq(Opcode));
}
Matcher<MCInst> IsLoadLow16BitImm(unsigned Reg, int64_t Value, bool IsGPR32) {
const unsigned ZeroReg = IsGPR32 ? Mips::ZERO : Mips::ZERO_64;
const unsigned ORi = IsGPR32 ? Mips::ORi : Mips::ORi64;
return AllOf(OpcodeIs(ORi),
ElementsAre(IsReg(Reg), IsReg(ZeroReg), IsImm(Value)));
}
Matcher<MCInst> IsLoadHigh16BitImm(unsigned Reg, int64_t Value, bool IsGPR32) {
const unsigned LUi = IsGPR32 ? Mips::LUi : Mips::LUi64;
return AllOf(OpcodeIs(LUi), ElementsAre(IsReg(Reg), IsImm(Value)));
}
Matcher<MCInst> IsShift(unsigned Reg, uint16_t Amount, bool IsGPR32) {
const unsigned SLL = IsGPR32 ? Mips::SLL : Mips::SLL64_64;
return AllOf(OpcodeIs(SLL),
ElementsAre(IsReg(Reg), IsReg(Reg), IsImm(Amount)));
}
constexpr const char kTriple[] = "mips-unknown-linux";
class MipsTargetTest : public ::testing::Test {
protected:
MipsTargetTest() : State(kTriple, "mips32", "") {}
static void SetUpTestCase() {
LLVMInitializeMipsTargetInfo();
LLVMInitializeMipsTarget();
LLVMInitializeMipsTargetMC();
InitializeMipsExegesisTarget();
}
std::vector<MCInst> setRegTo(unsigned Reg, const APInt &Value) {
return State.getExegesisTarget().setRegTo(State.getSubtargetInfo(), Reg,
Value);
}
LLVMState State;
};
TEST_F(MipsTargetTest, SetGPR32RegTo16BitValue) {
const uint16_t Value = 0xFFFFU;
const unsigned Reg = Mips::T0;
EXPECT_THAT(setRegTo(Reg, APInt(16, Value)),
ElementsAre(IsLoadLow16BitImm(Reg, Value, true)));
}
TEST_F(MipsTargetTest, SetGPR64RegTo16BitValue) {
const uint16_t Value = 0xFFFFU;
const unsigned Reg = Mips::T0_64;
EXPECT_THAT(setRegTo(Reg, APInt(16, Value)),
ElementsAre(IsLoadLow16BitImm(Reg, Value, false)));
}
TEST_F(MipsTargetTest, SetGPR32RegTo32BitValue) {
const uint32_t Value0 = 0xFFFF0000UL;
const unsigned Reg0 = Mips::T0;
EXPECT_THAT(setRegTo(Reg0, APInt(32, Value0)),
ElementsAre(IsLoadHigh16BitImm(Reg0, 0xFFFFU, true)));
const uint32_t Value1 = 0xFFFFFFFFUL;
const unsigned Reg1 = Mips::T1;
EXPECT_THAT(setRegTo(Reg1, APInt(32, Value1)),
ElementsAre(IsLoadHigh16BitImm(Reg1, 0xFFFFU, true),
IsLoadLow16BitImm(Reg1, 0xFFFFU, true)));
}
TEST_F(MipsTargetTest, SetGPR64RegTo32BitValue) {
const uint32_t Value0 = 0x7FFF0000UL;
const unsigned Reg0 = Mips::T0_64;
EXPECT_THAT(setRegTo(Reg0, APInt(32, Value0)),
ElementsAre(IsLoadHigh16BitImm(Reg0, 0x7FFFU, false)));
const uint32_t Value1 = 0x7FFFFFFFUL;
const unsigned Reg1 = Mips::T1_64;
EXPECT_THAT(setRegTo(Reg1, APInt(32, Value1)),
ElementsAre(IsLoadHigh16BitImm(Reg1, 0x7FFFU, false),
IsLoadLow16BitImm(Reg1, 0xFFFFU, false)));
const uint32_t Value2 = 0xFFFF0000UL;
const unsigned Reg2 = Mips::T2_64;
EXPECT_THAT(setRegTo(Reg2, APInt(32, Value2)),
ElementsAre(IsLoadLow16BitImm(Reg2, 0xFFFFU, false),
IsShift(Reg2, 16, false)));
const uint32_t Value3 = 0xFFFFFFFFUL;
const unsigned Reg3 = Mips::T3_64;
EXPECT_THAT(setRegTo(Reg3, APInt(32, Value3)),
ElementsAre(IsLoadLow16BitImm(Reg3, 0xFFFFU, false),
IsShift(Reg3, 16, false),
IsLoadLow16BitImm(Reg3, 0xFFFFU, false)));
}
TEST_F(MipsTargetTest, DefaultPfmCounters) {
const std::string Expected = "CYCLES";
EXPECT_EQ(State.getExegesisTarget().getPfmCounters("").CycleCounter,
Expected);
EXPECT_EQ(
State.getExegesisTarget().getPfmCounters("unknown_cpu").CycleCounter,
Expected);
}
} // namespace
} // namespace exegesis
} // namespace llvm