Jinsong Ji | 56c74cf | 2018-11-20 14:41:59 +0000 | [diff] [blame] | 1 | //===-- TargetTest.cpp ------------------------------------------*- C++ -*-===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Jinsong Ji | 56c74cf | 2018-11-20 14:41:59 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
John Brawn | c4ed600 | 2018-07-03 10:10:29 +0000 | [diff] [blame] | 9 | #include "Target.h" |
| 10 | |
| 11 | #include <cassert> |
| 12 | #include <memory> |
| 13 | |
| 14 | #include "MCTargetDesc/AArch64MCTargetDesc.h" |
John Brawn | b371ccc | 2018-07-03 10:52:20 +0000 | [diff] [blame] | 15 | #include "llvm/Support/TargetRegistry.h" |
| 16 | #include "llvm/Support/TargetSelect.h" |
John Brawn | c4ed600 | 2018-07-03 10:10:29 +0000 | [diff] [blame] | 17 | #include "gmock/gmock.h" |
| 18 | #include "gtest/gtest.h" |
| 19 | |
Fangrui Song | 32401af | 2018-10-22 17:10:47 +0000 | [diff] [blame] | 20 | namespace llvm { |
John Brawn | c4ed600 | 2018-07-03 10:10:29 +0000 | [diff] [blame] | 21 | namespace exegesis { |
| 22 | |
| 23 | void InitializeAArch64ExegesisTarget(); |
| 24 | |
| 25 | namespace { |
| 26 | |
| 27 | using testing::Gt; |
Guillaume Chatelet | c96a97b | 2018-09-20 12:22:18 +0000 | [diff] [blame] | 28 | using testing::IsEmpty; |
| 29 | using testing::Not; |
John Brawn | c4ed600 | 2018-07-03 10:10:29 +0000 | [diff] [blame] | 30 | using testing::NotNull; |
John Brawn | c4ed600 | 2018-07-03 10:10:29 +0000 | [diff] [blame] | 31 | |
Guillaume Chatelet | 12ca74e | 2018-09-20 13:37:04 +0000 | [diff] [blame] | 32 | constexpr const char kTriple[] = "aarch64-unknown-linux"; |
John Brawn | b371ccc | 2018-07-03 10:52:20 +0000 | [diff] [blame] | 33 | |
John Brawn | c4ed600 | 2018-07-03 10:10:29 +0000 | [diff] [blame] | 34 | class AArch64TargetTest : public ::testing::Test { |
| 35 | protected: |
| 36 | AArch64TargetTest() |
Clement Courbet | d422d3a | 2019-10-09 11:29:21 +0000 | [diff] [blame] | 37 | : ExegesisTarget_(ExegesisTarget::lookup(Triple(kTriple))) { |
John Brawn | b371ccc | 2018-07-03 10:52:20 +0000 | [diff] [blame] | 38 | EXPECT_THAT(ExegesisTarget_, NotNull()); |
| 39 | std::string error; |
Clement Courbet | d422d3a | 2019-10-09 11:29:21 +0000 | [diff] [blame] | 40 | Target_ = TargetRegistry::lookupTarget(kTriple, error); |
John Brawn | c4ed600 | 2018-07-03 10:10:29 +0000 | [diff] [blame] | 41 | EXPECT_THAT(Target_, NotNull()); |
Guillaume Chatelet | c96a97b | 2018-09-20 12:22:18 +0000 | [diff] [blame] | 42 | STI_.reset( |
Guillaume Chatelet | 12ca74e | 2018-09-20 13:37:04 +0000 | [diff] [blame] | 43 | Target_->createMCSubtargetInfo(kTriple, "generic", /*no features*/ "")); |
John Brawn | c4ed600 | 2018-07-03 10:10:29 +0000 | [diff] [blame] | 44 | } |
Guillaume Chatelet | c96a97b | 2018-09-20 12:22:18 +0000 | [diff] [blame] | 45 | |
John Brawn | b371ccc | 2018-07-03 10:52:20 +0000 | [diff] [blame] | 46 | static void SetUpTestCase() { |
| 47 | LLVMInitializeAArch64TargetInfo(); |
| 48 | LLVMInitializeAArch64Target(); |
| 49 | LLVMInitializeAArch64TargetMC(); |
| 50 | InitializeAArch64ExegesisTarget(); |
| 51 | } |
John Brawn | c4ed600 | 2018-07-03 10:10:29 +0000 | [diff] [blame] | 52 | |
Guillaume Chatelet | c96a97b | 2018-09-20 12:22:18 +0000 | [diff] [blame] | 53 | std::vector<MCInst> setRegTo(unsigned Reg, const APInt &Value) { |
| 54 | return ExegesisTarget_->setRegTo(*STI_, Reg, Value); |
| 55 | } |
| 56 | |
Clement Courbet | d422d3a | 2019-10-09 11:29:21 +0000 | [diff] [blame] | 57 | const Target *Target_; |
John Brawn | b371ccc | 2018-07-03 10:52:20 +0000 | [diff] [blame] | 58 | const ExegesisTarget *const ExegesisTarget_; |
Clement Courbet | d422d3a | 2019-10-09 11:29:21 +0000 | [diff] [blame] | 59 | std::unique_ptr<MCSubtargetInfo> STI_; |
John Brawn | c4ed600 | 2018-07-03 10:10:29 +0000 | [diff] [blame] | 60 | }; |
| 61 | |
Guillaume Chatelet | c96a97b | 2018-09-20 12:22:18 +0000 | [diff] [blame] | 62 | TEST_F(AArch64TargetTest, SetRegToConstant) { |
| 63 | // The AArch64 target currently doesn't know how to set register values. |
Clement Courbet | d422d3a | 2019-10-09 11:29:21 +0000 | [diff] [blame] | 64 | const auto Insts = setRegTo(AArch64::X0, APInt()); |
Guillaume Chatelet | c96a97b | 2018-09-20 12:22:18 +0000 | [diff] [blame] | 65 | EXPECT_THAT(Insts, Not(IsEmpty())); |
| 66 | } |
| 67 | |
Clement Courbet | eee2e06 | 2018-11-09 13:15:32 +0000 | [diff] [blame] | 68 | TEST_F(AArch64TargetTest, DefaultPfmCounters) { |
Clement Courbet | df78bf1 | 2018-11-09 14:08:29 +0000 | [diff] [blame] | 69 | const std::string Expected = "CPU_CYCLES"; |
| 70 | EXPECT_EQ(ExegesisTarget_->getPfmCounters("").CycleCounter, Expected); |
Clement Courbet | eee2e06 | 2018-11-09 13:15:32 +0000 | [diff] [blame] | 71 | EXPECT_EQ(ExegesisTarget_->getPfmCounters("unknown_cpu").CycleCounter, |
Clement Courbet | df78bf1 | 2018-11-09 14:08:29 +0000 | [diff] [blame] | 72 | Expected); |
Clement Courbet | eee2e06 | 2018-11-09 13:15:32 +0000 | [diff] [blame] | 73 | } |
| 74 | |
John Brawn | c4ed600 | 2018-07-03 10:10:29 +0000 | [diff] [blame] | 75 | } // namespace |
| 76 | } // namespace exegesis |
Fangrui Song | 32401af | 2018-10-22 17:10:47 +0000 | [diff] [blame] | 77 | } // namespace llvm |