Index: lib/Target/Mips/CMakeLists.txt =================================================================== --- lib/Target/Mips/CMakeLists.txt +++ lib/Target/Mips/CMakeLists.txt @@ -13,6 +13,7 @@ tablegen(LLVM MipsGenRegisterBank.inc -gen-register-bank) tablegen(LLVM MipsGenRegisterInfo.inc -gen-register-info) tablegen(LLVM MipsGenSubtargetInfo.inc -gen-subtarget) +tablegen(LLVM MipsGenExegesis.inc -gen-exegesis) add_public_tablegen_target(MipsCommonTableGen) Index: lib/Target/Mips/Mips.td =================================================================== --- lib/Target/Mips/Mips.td +++ lib/Target/Mips/Mips.td @@ -263,3 +263,9 @@ let AssemblyParserVariants = [MipsAsmParserVariant]; let AllowRegisterRenaming = 1; } + +//===----------------------------------------------------------------------===// +// Pfm Counters +//===----------------------------------------------------------------------===// + +include "MipsPfmCounters.td" Index: lib/Target/Mips/MipsPfmCounters.td =================================================================== --- /dev/null +++ lib/Target/Mips/MipsPfmCounters.td @@ -0,0 +1,18 @@ +//===-- MipsPfmCounters.td - Mips Hardware Counters --------*- tablegen -*-===// +// +// 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 +// +//===----------------------------------------------------------------------===// +// +// This describes the available hardware counters for Mips. +// +//===----------------------------------------------------------------------===// + +def CpuCyclesPfmCounter : PfmCounter<"CYCLES">; + +def DefaultPfmCounters : ProcPfmCounters { + let CycleCounter = CpuCyclesPfmCounter; +} +def : PfmCountersDefaultBinding; Index: tools/llvm-exegesis/lib/Assembler.cpp =================================================================== --- tools/llvm-exegesis/lib/Assembler.cpp +++ tools/llvm-exegesis/lib/Assembler.cpp @@ -235,9 +235,10 @@ ET.addTargetSpecificPasses(PM); TPC->printAndVerify("After ExegesisTarget::addTargetSpecificPasses"); // Adding the following passes: + // - postrapseudos: expands pseudo return instructions used on some targets. // - machineverifier: checks that the MachineFunction is well formed. // - prologepilog: saves and restore callee saved registers. - for (const char *PassName : {"machineverifier", "prologepilog"}) + for (const char *PassName : {"postrapseudos", "machineverifier", "prologepilog"}) if (addPass(PM, PassName, *TPC)) llvm::report_fatal_error("Unable to add a mandatory pass"); TPC->setInitialized(); Index: tools/llvm-exegesis/lib/CMakeLists.txt =================================================================== --- tools/llvm-exegesis/lib/CMakeLists.txt +++ tools/llvm-exegesis/lib/CMakeLists.txt @@ -12,6 +12,10 @@ add_subdirectory(PowerPC) set(TARGETS_TO_APPEND "${TARGETS_TO_APPEND} PowerPC") endif() +if (LLVM_TARGETS_TO_BUILD MATCHES "Mips") + add_subdirectory(Mips) + set(TARGETS_TO_APPEND "${TARGETS_TO_APPEND} Mips") +endif() set(LLVM_EXEGESIS_TARGETS "${LLVM_EXEGESIS_TARGETS} ${TARGETS_TO_APPEND}" PARENT_SCOPE) Index: tools/llvm-exegesis/lib/Mips/CMakeLists.txt =================================================================== --- /dev/null +++ tools/llvm-exegesis/lib/Mips/CMakeLists.txt @@ -0,0 +1,18 @@ +include_directories( + ${LLVM_MAIN_SRC_DIR}/lib/Target/Mips + ${LLVM_BINARY_DIR}/lib/Target/Mips + ) + +add_library(LLVMExegesisMips + STATIC + Target.cpp + ) + +llvm_update_compile_flags(LLVMExegesisMips) +llvm_map_components_to_libnames(libs + Mips + Exegesis + ) + +target_link_libraries(LLVMExegesisMips ${libs}) +set_target_properties(LLVMExegesisMips PROPERTIES FOLDER "Libraries") Index: tools/llvm-exegesis/lib/Mips/LLVMBuild.txt =================================================================== --- /dev/null +++ tools/llvm-exegesis/lib/Mips/LLVMBuild.txt @@ -0,0 +1,21 @@ +;===- ./tools/llvm-exegesis/lib/Mips/LLVMBuild.txt -------------*- Conf -*--===; +; +; 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 +; +;===------------------------------------------------------------------------===; +; +; This is an LLVMBuild description file for the components in this subdirectory. +; +; For more information on the LLVMBuild system, please see: +; +; http://llvm.org/docs/LLVMBuild.html +; +;===------------------------------------------------------------------------===; + +[component_0] +type = Library +name = ExegesisMips +parent = Libraries +required_libraries = Mips Index: tools/llvm-exegesis/lib/Mips/Target.cpp =================================================================== --- /dev/null +++ tools/llvm-exegesis/lib/Mips/Target.cpp @@ -0,0 +1,68 @@ +//===-- Target.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 "../Latency.h" +#include "Mips.h" +#include "MipsRegisterInfo.h" + +namespace llvm { +namespace exegesis { + +#include "MipsGenExegesis.inc" + +namespace { +class ExegesisMipsTarget : public ExegesisTarget { +public: + ExegesisMipsTarget() : ExegesisTarget(MipsCpuPfmCounters) {} + +private: + std::vector setRegTo(const llvm::MCSubtargetInfo &STI, + unsigned Reg, + const llvm::APInt &Value) const override; + bool matchesArch(llvm::Triple::ArchType Arch) const override { + return Arch == llvm::Triple::mips || Arch == llvm::Triple::mipsel || + Arch == llvm::Triple::mips64 || Arch == llvm::Triple::mips64el; + } +}; +} // end anonymous namespace + +// Generates instruction to load an immediate value into a register. +static llvm::MCInst loadImmediate(unsigned Reg, unsigned RegBitWidth, + const llvm::APInt &Value) { + if (Value.getActiveBits() > 16) + llvm_unreachable("Not implemented for Values wider than 16 bits"); + if (Value.getBitWidth() > RegBitWidth) + llvm_unreachable("Value must fit in the Register"); + return llvm::MCInstBuilder(llvm::Mips::ORi) + .addReg(Reg) + .addReg(llvm::Mips::ZERO) + .addImm(Value.getZExtValue()); +} + +std::vector +ExegesisMipsTarget::setRegTo(const llvm::MCSubtargetInfo &STI, unsigned Reg, + const llvm::APInt &Value) const { + if (llvm::Mips::GPR32RegClass.contains(Reg)) + return {loadImmediate(Reg, 32, Value)}; + if (llvm::Mips::GPR64RegClass.contains(Reg)) + return {loadImmediate(Reg, 64, Value)}; + llvm::errs() << "setRegTo is not implemented, results will be unreliable\n"; + return {}; +} + +static ExegesisTarget *getTheExegesisMipsTarget() { + static ExegesisMipsTarget Target; + return &Target; +} + +void InitializeMipsExegesisTarget() { + ExegesisTarget::registerTarget(getTheExegesisMipsTarget()); +} + +} // namespace exegesis +} // namespace llvm Index: unittests/tools/llvm-exegesis/CMakeLists.txt =================================================================== --- unittests/tools/llvm-exegesis/CMakeLists.txt +++ unittests/tools/llvm-exegesis/CMakeLists.txt @@ -30,3 +30,6 @@ if(LLVM_TARGETS_TO_BUILD MATCHES "PowerPC") add_subdirectory(PowerPC) endif() +if(LLVM_TARGETS_TO_BUILD MATCHES "Mips") + add_subdirectory(Mips) +endif() Index: unittests/tools/llvm-exegesis/Mips/CMakeLists.txt =================================================================== --- /dev/null +++ unittests/tools/llvm-exegesis/Mips/CMakeLists.txt @@ -0,0 +1,21 @@ +include_directories( + ${LLVM_MAIN_SRC_DIR}/lib/Target/Mips + ${LLVM_BINARY_DIR}/lib/Target/Mips + ${LLVM_MAIN_SRC_DIR}/tools/llvm-exegesis/lib + ) + +set(LLVM_LINK_COMPONENTS + MC + MCParser + Object + Support + Symbolize + Mips + ) + +add_llvm_unittest(LLVMExegesisMipsTests + TargetTest.cpp + ) +target_link_libraries(LLVMExegesisMipsTests PRIVATE + LLVMExegesis + LLVMExegesisMips) Index: unittests/tools/llvm-exegesis/Mips/TargetTest.cpp =================================================================== --- /dev/null +++ unittests/tools/llvm-exegesis/Mips/TargetTest.cpp @@ -0,0 +1,69 @@ +//===-- 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 +#include + +#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 llvm::APInt; +using llvm::MCInst; +using testing::Gt; +using testing::IsEmpty; +using testing::Not; +using testing::NotNull; + +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 setRegTo(unsigned Reg, const APInt &Value) { + return State.getExegesisTarget().setRegTo(State.getSubtargetInfo(), Reg, + Value); + } + + LLVMState State; +}; + +TEST_F(MipsTargetTest, SetRegToConstant) { + const auto Insts = setRegTo(llvm::Mips::T0, llvm::APInt()); + EXPECT_THAT(Insts, Not(IsEmpty())); +} + +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