Index: llvm/CMakeLists.txt =================================================================== --- llvm/CMakeLists.txt +++ llvm/CMakeLists.txt @@ -312,6 +312,7 @@ WebAssembly X86 XCore + M68k ) # List of targets with JIT support: Index: llvm/lib/Target/M68k/CMakeLists.txt =================================================================== --- llvm/lib/Target/M68k/CMakeLists.txt +++ llvm/lib/Target/M68k/CMakeLists.txt @@ -2,7 +2,8 @@ set(LLVM_TARGET_DEFINITIONS M68k.td) -tablegen(LLVM M68kGenRegisterInfo.inc -gen-register-info) +tablegen(LLVM M68kGenRegisterInfo.inc -gen-register-info) +tablegen(LLVM M68kGenRegisterBank.inc -gen-register-bank) tablegen(LLVM M68kGenInstrInfo.inc -gen-instr-info) tablegen(LLVM M68kGenSubtargetInfo.inc -gen-subtarget) tablegen(LLVM M68kGenMCCodeBeads.inc -gen-code-beads) @@ -17,6 +18,7 @@ add_llvm_target(M68kCodeGen M68kAsmPrinter.cpp M68kCollapseMOVEMPass.cpp + M68kCallLowering.cpp M68kExpandPseudo.cpp M68kFrameLowering.cpp M68kInstrInfo.cpp @@ -28,12 +30,16 @@ M68kSubtarget.cpp M68kTargetMachine.cpp M68kTargetObjectFile.cpp + M68kInstructionSelector.cpp + M68kLegalizerInfo.cpp + M68kRegisterBankInfo.cpp LINK_COMPONENTS Analysis AsmPrinter CodeGen Core + GlobalISel MC SelectionDAG Support Index: llvm/lib/Target/M68k/M68k.h =================================================================== --- llvm/lib/Target/M68k/M68k.h +++ llvm/lib/Target/M68k/M68k.h @@ -16,8 +16,10 @@ #define LLVM_LIB_TARGET_M68K_M68K_H namespace llvm { - +class M68kRegisterBankInfo; +class M68kSubtarget; class FunctionPass; +class InstructionSelector; class M68kTargetMachine; /// This pass converts a legalized DAG into a M68k-specific DAG, ready for @@ -45,6 +47,10 @@ /// so that all possible MOVEs are present. FunctionPass *createM68kConvertMOVToMOVMPass(); +InstructionSelector *createM68kInstructionSelector(const M68kTargetMachine &, + M68kSubtarget &, + M68kRegisterBankInfo &); + } // namespace llvm #endif Index: llvm/lib/Target/M68k/M68k.td =================================================================== --- llvm/lib/Target/M68k/M68k.td +++ llvm/lib/Target/M68k/M68k.td @@ -69,6 +69,7 @@ //===----------------------------------------------------------------------===// include "M68kRegisterInfo.td" +include "M68kRegisterBanks.td" //===----------------------------------------------------------------------===// // Instruction Descriptions Index: llvm/lib/Target/M68k/M68kCallLowering.h =================================================================== --- /dev/null +++ llvm/lib/Target/M68k/M68kCallLowering.h @@ -0,0 +1,44 @@ +//===-- M68kCallLowering.h - Call lowering ---------------------*- 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 +// +//===----------------------------------------------------------------------===// +// +/// \file +/// This file describes how to lower LLVM calls to machine code calls. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_LIB_TARGET_M68K_M68KCALLLOWERING_H +#define LLVM_LIB_TARGET_M68K_M68KCALLLOWERING_H + +#include "llvm/CodeGen/CallingConvLower.h" +#include "llvm/CodeGen/GlobalISel/CallLowering.h" +#include "llvm/CodeGen/ValueTypes.h" + +namespace llvm { + +class M68kTargetLowering; + +class M68kCallLowering : public CallLowering { + +public: + M68kCallLowering(const M68kTargetLowering &TLI); + + bool lowerReturn(MachineIRBuilder &MIRBuilder, const Value *Val, + ArrayRef VRegs, FunctionLoweringInfo &FLI, + Register SwiftErrorVReg) const override; + + bool lowerFormalArguments(MachineIRBuilder &MIRBuilder, const Function &F, + ArrayRef> VRegs, + FunctionLoweringInfo &FLI) const override; + + bool lowerCall(MachineIRBuilder &MIRBuilder, + CallLoweringInfo &Info) const override; +}; + +} // end namespace llvm + +#endif // LLVM_LIB_TARGET_M68K_M68KCALLLOWERING_H \ No newline at end of file Index: llvm/lib/Target/M68k/M68kCallLowering.cpp =================================================================== --- /dev/null +++ llvm/lib/Target/M68k/M68kCallLowering.cpp @@ -0,0 +1,52 @@ +//===-- M68kCallLowering.cpp - Call lowering -------------------*- 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 +// +//===----------------------------------------------------------------------===// +// +/// \file +/// This file implements the lowering of LLVM calls to machine code calls for +/// GlobalISel. +// +//===----------------------------------------------------------------------===// + +#include "M68kCallLowering.h" +#include "M68kISelLowering.h" +#include "llvm/CodeGen/GlobalISel/MachineIRBuilder.h" + +using namespace llvm; + +M68kCallLowering::M68kCallLowering(const M68kTargetLowering &TLI) + : CallLowering(&TLI) {} + +bool M68kCallLowering::lowerReturn(MachineIRBuilder &MIRBuilder, + const Value *Val, ArrayRef VRegs, + FunctionLoweringInfo &FLI, + Register SwiftErrorVReg) const { + + MachineInstrBuilder Ret = MIRBuilder.buildInstrNoInsert(M68kISD::RET); + + if (Val != nullptr) { + return false; + } + MIRBuilder.insertInstr(Ret); + return true; +} + +bool M68kCallLowering::lowerFormalArguments(MachineIRBuilder &MIRBuilder, + const Function &F, + ArrayRef> VRegs, + FunctionLoweringInfo &FLI) const { + + if (F.arg_empty()) + return true; + + return false; +} + +bool M68kCallLowering::lowerCall(MachineIRBuilder &MIRBuilder, + CallLoweringInfo &Info) const { + return false; +} \ No newline at end of file Index: llvm/lib/Target/M68k/M68kInstructionSelector.cpp =================================================================== --- /dev/null +++ llvm/lib/Target/M68k/M68kInstructionSelector.cpp @@ -0,0 +1,59 @@ +//===- M68kInstructionSelector.cpp ------------------------------*- C++ -*-===// +//===----------------------------------------------------------------------===// +/// \file +/// This file implements the targeting of the InstructionSelector class for +/// M68k. +/// \todo This should be generated by TableGen. +//===----------------------------------------------------------------------===// + +#include "M68kRegisterBankInfo.h" +#include "M68kSubtarget.h" +#include "M68kTargetMachine.h" +#include "llvm/Support/Debug.h" + +using namespace llvm; + +namespace { + +class M68kInstructionSelector : public InstructionSelector { +public: + M68kInstructionSelector(const M68kTargetMachine &TM, const M68kSubtarget &STI, + const M68kRegisterBankInfo &RBI); + + bool select(MachineInstr &I) override; + +private: + const M68kTargetMachine &TM; + const M68kSubtarget &STI; + const M68kInstrInfo &TII; + const M68kRegisterInfo &TRI; + const M68kRegisterBankInfo &RBI; +}; + +} // end anonymous namespace + +M68kInstructionSelector::M68kInstructionSelector( + const M68kTargetMachine &TM, const M68kSubtarget &STI, + const M68kRegisterBankInfo &RBI) + : InstructionSelector(), TM(TM), STI(STI), TII(*STI.getInstrInfo()), + TRI(*STI.getRegisterInfo()), RBI(RBI) {} + +bool M68kInstructionSelector::select(MachineInstr &I) { + + if (!isPreISelGenericOpcode(I.getOpcode())) { + // Not global isel generic opcode. + // TODO: select copy + return true; + } + + // We didn't select anything. + return false; +} + +namespace llvm { +InstructionSelector *createM68kInstructionSelector(const M68kTargetMachine &TM, + M68kSubtarget &Subtarget, + M68kRegisterBankInfo &RBI) { + return new M68kInstructionSelector(TM, Subtarget, RBI); +} +} // end namespace llvm \ No newline at end of file Index: llvm/lib/Target/M68k/M68kLegalizerInfo.h =================================================================== --- /dev/null +++ llvm/lib/Target/M68k/M68kLegalizerInfo.h @@ -0,0 +1,27 @@ +//===-- M68kLegalizerInfo.h ------------------------------------*- 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 +// +//===----------------------------------------------------------------------===// +/// \file +/// This file declares the targeting of the Machinelegalizer class for M68k. +//===----------------------------------------------------------------------===// + +#ifndef LLVM_LIB_TARGET_M68K_M68KLEGALIZERINFO_H +#define LLVM_LIB_TARGET_M68K_M68KLEGALIZERINFO_H + +#include "llvm/CodeGen/GlobalISel/LegalizerInfo.h" + +namespace llvm { + +class M68kSubtarget; + +/// This class provides the information for the target register banks. +class M68kLegalizerInfo : public LegalizerInfo { +public: + M68kLegalizerInfo(const M68kSubtarget &ST); +}; +} // end namespace llvm +#endif \ No newline at end of file Index: llvm/lib/Target/M68k/M68kLegalizerInfo.cpp =================================================================== --- /dev/null +++ llvm/lib/Target/M68k/M68kLegalizerInfo.cpp @@ -0,0 +1,22 @@ +//===-- M68kLegalizerInfo.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 +// +//===----------------------------------------------------------------------===// +/// \file +/// This file implements the targeting of the Machinelegalizer class for M68k. +//===----------------------------------------------------------------------===// + +#include "M68kLegalizerInfo.h" +#include "llvm/CodeGen/TargetOpcodes.h" +#include "llvm/CodeGen/ValueTypes.h" +#include "llvm/IR/DerivedTypes.h" +#include "llvm/IR/Type.h" + +using namespace llvm; + +M68kLegalizerInfo::M68kLegalizerInfo(const M68kSubtarget &ST) { + computeTables(); +} \ No newline at end of file Index: llvm/lib/Target/M68k/M68kRegisterBankInfo.h =================================================================== --- /dev/null +++ llvm/lib/Target/M68k/M68kRegisterBankInfo.h @@ -0,0 +1,37 @@ +//===-- M68kRegisterBankInfo.h ---------------------------------*- 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 +// +//===----------------------------------------------------------------------===// +/// \file +/// This file declares the targeting of the RegisterBankInfo class for M68k. +/// \todo This should be generated by TableGen. +//===----------------------------------------------------------------------===// + +#ifndef LLVM_LIB_TARGET_M68K_M68KREGISTERBANKINFO_H +#define LLVM_LIB_TARGET_M68K_M68KREGISTERBANKINFO_H + +#include "llvm/CodeGen/GlobalISel/RegisterBankInfo.h" + +#define GET_REGBANK_DECLARATIONS +#include "M68kGenRegisterBank.inc" + +namespace llvm { + +class TargetRegisterInfo; + +class M68kGenRegisterBankInfo : public RegisterBankInfo { +protected: +#define GET_TARGET_REGBANK_CLASS +#include "M68kGenRegisterBank.inc" +}; + +/// This class provides the information for the target register banks. +class M68kRegisterBankInfo final : public M68kGenRegisterBankInfo { +public: + M68kRegisterBankInfo(const TargetRegisterInfo &TRI); +}; +} // end namespace llvm +#endif \ No newline at end of file Index: llvm/lib/Target/M68k/M68kRegisterBankInfo.cpp =================================================================== --- /dev/null +++ llvm/lib/Target/M68k/M68kRegisterBankInfo.cpp @@ -0,0 +1,26 @@ +//===-- M68kRegisterBankInfo.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 +// +//===----------------------------------------------------------------------===// +/// \file +/// This file implements the targeting of the RegisterBankInfo class for M68k. +/// \todo This should be generated by TableGen. +//===----------------------------------------------------------------------===// + +#include "M68kRegisterBankInfo.h" +#include "MCTargetDesc/M68kMCTargetDesc.h" +#include "llvm/CodeGen/GlobalISel/RegisterBank.h" +#include "llvm/CodeGen/GlobalISel/RegisterBankInfo.h" +#include "llvm/CodeGen/MachineRegisterInfo.h" +#include "llvm/CodeGen/TargetRegisterInfo.h" + +#define GET_TARGET_REGBANK_IMPL +#include "M68kGenRegisterBank.inc" + +using namespace llvm; + +M68kRegisterBankInfo::M68kRegisterBankInfo(const TargetRegisterInfo &TRI) + : M68kGenRegisterBankInfo() {} \ No newline at end of file Index: llvm/lib/Target/M68k/M68kRegisterBanks.td =================================================================== --- /dev/null +++ llvm/lib/Target/M68k/M68kRegisterBanks.td @@ -0,0 +1,13 @@ +//=-- M68kRegisterBank.td - Describe the M68k Banks --------*- 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 +// +//===----------------------------------------------------------------------===// +// +// +//===----------------------------------------------------------------------===// + +/// General Purpose Registers. Here we define a register bank with name AnyGPR +def GPRRegBank : RegisterBank<"AnyGPR", [DR8]>; \ No newline at end of file Index: llvm/lib/Target/M68k/M68kSubtarget.h =================================================================== --- llvm/lib/Target/M68k/M68kSubtarget.h +++ llvm/lib/Target/M68k/M68kSubtarget.h @@ -17,7 +17,10 @@ #include "M68kFrameLowering.h" #include "M68kISelLowering.h" #include "M68kInstrInfo.h" - +#include "llvm/CodeGen/GlobalISel/CallLowering.h" +#include "llvm/CodeGen/GlobalISel/InstructionSelector.h" +#include "llvm/CodeGen/GlobalISel/LegalizerInfo.h" +#include "llvm/CodeGen/GlobalISel/RegisterBankInfo.h" #include "llvm/CodeGen/SelectionDAGTargetInfo.h" #include "llvm/CodeGen/TargetSubtargetInfo.h" #include "llvm/IR/DataLayout.h" @@ -151,6 +154,19 @@ const InstrItineraryData *getInstrItineraryData() const override { return &InstrItins; } + +protected: + // GlobalISel related APIs. + std::unique_ptr CallLoweringInfo; + std::unique_ptr InstSelector; + std::unique_ptr Legalizer; + std::unique_ptr RegBankInfo; + +public: + const CallLowering *getCallLowering() const override; + InstructionSelector *getInstructionSelector() const override; + const LegalizerInfo *getLegalizerInfo() const override; + const RegisterBankInfo *getRegBankInfo() const override; }; } // namespace llvm Index: llvm/lib/Target/M68k/M68kSubtarget.cpp =================================================================== --- llvm/lib/Target/M68k/M68kSubtarget.cpp +++ llvm/lib/Target/M68k/M68kSubtarget.cpp @@ -14,10 +14,12 @@ #include "M68kSubtarget.h" #include "M68k.h" +#include "M68kCallLowering.h" +#include "M68kLegalizerInfo.h" #include "M68kMachineFunction.h" +#include "M68kRegisterBankInfo.cpp" #include "M68kRegisterInfo.h" #include "M68kTargetMachine.h" - #include "llvm/CodeGen/MachineJumpTableInfo.h" #include "llvm/IR/Attributes.h" #include "llvm/IR/Function.h" @@ -50,7 +52,30 @@ : M68kGenSubtargetInfo(TT, CPU, /*TuneCPU*/ CPU, FS), TM(TM), TSInfo(), InstrInfo(initializeSubtargetDependencies(CPU, TT, FS, TM)), FrameLowering(*this, this->getStackAlignment()), TLInfo(TM, *this), - TargetTriple(TT) {} + TargetTriple(TT) { + CallLoweringInfo.reset(new M68kCallLowering(*getTargetLowering())); + Legalizer.reset(new M68kLegalizerInfo(*this)); + + auto *RBI = new M68kRegisterBankInfo(*getRegisterInfo()); + RegBankInfo.reset(RBI); + InstSelector.reset(createM68kInstructionSelector( + *static_cast(&TM), *this, *RBI)); +} +const CallLowering *M68kSubtarget::getCallLowering() const { + return CallLoweringInfo.get(); +} + +InstructionSelector *M68kSubtarget::getInstructionSelector() const { + return InstSelector.get(); +} + +const LegalizerInfo *M68kSubtarget::getLegalizerInfo() const { + return Legalizer.get(); +} + +const RegisterBankInfo *M68kSubtarget::getRegBankInfo() const { + return RegBankInfo.get(); +} bool M68kSubtarget::isPositionIndependent() const { return TM.isPositionIndependent(); Index: llvm/lib/Target/M68k/M68kTargetMachine.cpp =================================================================== --- llvm/lib/Target/M68k/M68kTargetMachine.cpp +++ llvm/lib/Target/M68k/M68kTargetMachine.cpp @@ -13,14 +13,18 @@ #include "M68kTargetMachine.h" #include "M68k.h" -#include "TargetInfo/M68kTargetInfo.h" - #include "M68kSubtarget.h" #include "M68kTargetObjectFile.h" - +#include "TargetInfo/M68kTargetInfo.h" +#include "llvm/CodeGen/GlobalISel/IRTranslator.h" +#include "llvm/CodeGen/GlobalISel/InstructionSelect.h" +#include "llvm/CodeGen/GlobalISel/Legalizer.h" +#include "llvm/CodeGen/GlobalISel/RegBankSelect.h" #include "llvm/CodeGen/Passes.h" #include "llvm/CodeGen/TargetPassConfig.h" #include "llvm/IR/LegacyPassManager.h" +#include "llvm/InitializePasses.h" +#include "llvm/PassRegistry.h" #include "llvm/Support/TargetRegistry.h" #include @@ -30,6 +34,8 @@ extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeM68kTarget() { RegisterTargetMachine X(getTheM68kTarget()); + auto *PR = PassRegistry::getPassRegistry(); + initializeGlobalISel(*PR); } namespace { @@ -134,7 +140,10 @@ const M68kSubtarget &getM68kSubtarget() const { return *getM68kTargetMachine().getSubtargetImpl(); } - + bool addIRTranslator() override; + bool addLegalizeMachineIR() override; + bool addRegBankSelect() override; + bool addGlobalInstructionSelect() override; bool addInstSelector() override; void addPreSched2() override; void addPreEmitPass() override; @@ -152,6 +161,26 @@ return false; } +bool M68kPassConfig::addIRTranslator() { + addPass(new IRTranslator()); + return false; +} + +bool M68kPassConfig::addLegalizeMachineIR() { + addPass(new Legalizer()); + return false; +} + +bool M68kPassConfig::addRegBankSelect() { + addPass(new RegBankSelect()); + return false; +} + +bool M68kPassConfig::addGlobalInstructionSelect() { + addPass(new InstructionSelect()); + return false; +} + void M68kPassConfig::addPreSched2() { addPass(createM68kExpandPseudoPass()); } void M68kPassConfig::addPreEmitPass() {