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