1 //===-- TargetTest.cpp ------------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
14 #include "MCTargetDesc/MipsMCTargetDesc.h"
15 #include "llvm/Support/TargetRegistry.h"
16 #include "llvm/Support/TargetSelect.h"
17 #include "gmock/gmock.h"
18 #include "gtest/gtest.h"
23 void InitializeMipsExegesisTarget();
28 using testing::ElementsAre
;
30 using testing::Matcher
;
31 using testing::Property
;
33 Matcher
<MCOperand
> IsImm(int64_t Value
) {
34 return AllOf(Property(&MCOperand::isImm
, Eq(true)),
35 Property(&MCOperand::getImm
, Eq(Value
)));
38 Matcher
<MCOperand
> IsReg(unsigned Reg
) {
39 return AllOf(Property(&MCOperand::isReg
, Eq(true)),
40 Property(&MCOperand::getReg
, Eq(Reg
)));
43 Matcher
<MCInst
> OpcodeIs(unsigned Opcode
) {
44 return Property(&MCInst::getOpcode
, Eq(Opcode
));
47 Matcher
<MCInst
> IsLoadLowImm(int64_t Reg
, int64_t Value
) {
48 return AllOf(OpcodeIs(Mips::ORi
),
49 ElementsAre(IsReg(Reg
), IsReg(Mips::ZERO
), IsImm(Value
)));
52 constexpr const char kTriple
[] = "mips-unknown-linux";
54 class MipsTargetTest
: public ::testing::Test
{
56 MipsTargetTest() : State(kTriple
, "mips32", "") {}
58 static void SetUpTestCase() {
59 LLVMInitializeMipsTargetInfo();
60 LLVMInitializeMipsTarget();
61 LLVMInitializeMipsTargetMC();
62 InitializeMipsExegesisTarget();
65 std::vector
<MCInst
> setRegTo(unsigned Reg
, const APInt
&Value
) {
66 return State
.getExegesisTarget().setRegTo(State
.getSubtargetInfo(), Reg
,
73 TEST_F(MipsTargetTest
, SetRegToConstant
) {
74 const uint16_t Value
= 0xFFFFU
;
75 const unsigned Reg
= Mips::T0
;
76 EXPECT_THAT(setRegTo(Reg
, APInt(16, Value
)),
77 ElementsAre(IsLoadLowImm(Reg
, Value
)));
80 TEST_F(MipsTargetTest
, DefaultPfmCounters
) {
81 const std::string Expected
= "CYCLES";
82 EXPECT_EQ(State
.getExegesisTarget().getPfmCounters("").CycleCounter
,
85 State
.getExegesisTarget().getPfmCounters("unknown_cpu").CycleCounter
,
90 } // namespace exegesis