1 | //===-- TargetTest.cpp ------------------------------------------*- C++ -*-===// |
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 "Target.h" |
10 | |
11 | #include <cassert> |
12 | #include <memory> |
13 | |
14 | #include "MCTargetDesc/AArch64MCTargetDesc.h" |
15 | #include "llvm/MC/TargetRegistry.h" |
16 | #include "llvm/Support/TargetSelect.h" |
17 | #include "gmock/gmock.h" |
18 | #include "gtest/gtest.h" |
19 | |
20 | namespace llvm { |
21 | namespace exegesis { |
22 | |
23 | void InitializeAArch64ExegesisTarget(); |
24 | |
25 | namespace { |
26 | |
27 | using testing::IsEmpty; |
28 | using testing::Not; |
29 | using testing::NotNull; |
30 | |
31 | constexpr const char kTriple[] = "aarch64-unknown-linux" ; |
32 | |
33 | class AArch64TargetTest : public ::testing::Test { |
34 | protected: |
35 | AArch64TargetTest() |
36 | : ExegesisTarget_(ExegesisTarget::lookup(TT: Triple(kTriple))) { |
37 | EXPECT_THAT(ExegesisTarget_, NotNull()); |
38 | std::string error; |
39 | Target_ = TargetRegistry::lookupTarget(Triple: kTriple, Error&: error); |
40 | EXPECT_THAT(Target_, NotNull()); |
41 | STI_.reset( |
42 | p: Target_->createMCSubtargetInfo(TheTriple: kTriple, CPU: "generic" , /*no features*/ Features: "" )); |
43 | } |
44 | |
45 | static void SetUpTestCase() { |
46 | LLVMInitializeAArch64TargetInfo(); |
47 | LLVMInitializeAArch64Target(); |
48 | LLVMInitializeAArch64TargetMC(); |
49 | InitializeAArch64ExegesisTarget(); |
50 | } |
51 | |
52 | std::vector<MCInst> setRegTo(unsigned Reg, const APInt &Value) { |
53 | return ExegesisTarget_->setRegTo(STI: *STI_, Reg, Value); |
54 | } |
55 | |
56 | const Target *Target_; |
57 | const ExegesisTarget *const ExegesisTarget_; |
58 | std::unique_ptr<MCSubtargetInfo> STI_; |
59 | }; |
60 | |
61 | TEST_F(AArch64TargetTest, SetRegToConstant) { |
62 | // The AArch64 target currently doesn't know how to set register values. |
63 | const auto Insts = setRegTo(AArch64::X0, APInt()); |
64 | EXPECT_THAT(Insts, Not(IsEmpty())); |
65 | } |
66 | |
67 | TEST_F(AArch64TargetTest, DefaultPfmCounters) { |
68 | const std::string Expected = "CPU_CYCLES" ; |
69 | EXPECT_EQ(ExegesisTarget_->getPfmCounters("" ).CycleCounter, Expected); |
70 | EXPECT_EQ(ExegesisTarget_->getPfmCounters("unknown_cpu" ).CycleCounter, |
71 | Expected); |
72 | } |
73 | |
74 | } // namespace |
75 | } // namespace exegesis |
76 | } // namespace llvm |
77 | |