Skip to content

[LoongArch][GlobalISel] Adding initial GlobalISel infrastructure #76912

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions llvm/lib/Target/LoongArch/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,29 @@ tablegen(LLVM LoongArchGenAsmMatcher.inc -gen-asm-matcher)
tablegen(LLVM LoongArchGenAsmWriter.inc -gen-asm-writer)
tablegen(LLVM LoongArchGenDAGISel.inc -gen-dag-isel)
tablegen(LLVM LoongArchGenDisassemblerTables.inc -gen-disassembler)
tablegen(LLVM LoongArchGenGlobalISel.inc -gen-global-isel)
tablegen(LLVM LoongArchGenInstrInfo.inc -gen-instr-info)
tablegen(LLVM LoongArchGenMCPseudoLowering.inc -gen-pseudo-lowering)
tablegen(LLVM LoongArchGenMCCodeEmitter.inc -gen-emitter)
tablegen(LLVM LoongArchGenRegisterBank.inc -gen-register-bank)
tablegen(LLVM LoongArchGenRegisterInfo.inc -gen-register-info)
tablegen(LLVM LoongArchGenSubtargetInfo.inc -gen-subtarget)

add_public_tablegen_target(LoongArchCommonTableGen)

add_llvm_target(LoongArchCodeGen
LoongArchAsmPrinter.cpp
LoongArchCallLowering.cpp
LoongArchExpandAtomicPseudoInsts.cpp
LoongArchExpandPseudoInsts.cpp
LoongArchFrameLowering.cpp
LoongArchInstrInfo.cpp
LoongArchInstructionSelector.cpp
LoongArchISelDAGToDAG.cpp
LoongArchISelLowering.cpp
LoongArchLegalizerInfo.cpp
LoongArchMCInstLower.cpp
LoongArchRegisterBankInfo.cpp
LoongArchRegisterInfo.cpp
LoongArchSubtarget.cpp
LoongArchTargetMachine.cpp
Expand All @@ -42,6 +48,7 @@ add_llvm_target(LoongArchCodeGen
Support
Target
TargetParser
GlobalISel

ADD_TO_COMPONENT
LoongArch
Expand Down
8 changes: 8 additions & 0 deletions llvm/lib/Target/LoongArch/LoongArch.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@
namespace llvm {
class AsmPrinter;
class FunctionPass;
class InstructionSelector;
class LoongArchTargetMachine;
class LoongArchRegisterBankInfo;
class LoongArchSubtarget;
class MCInst;
class MCOperand;
class MachineInstr;
Expand All @@ -41,6 +44,11 @@ void initializeLoongArchDAGToDAGISelPass(PassRegistry &);
void initializeLoongArchExpandAtomicPseudoPass(PassRegistry &);
void initializeLoongArchPreRAExpandPseudoPass(PassRegistry &);
void initializeLoongArchExpandPseudoPass(PassRegistry &);

InstructionSelector *
createLoongArchInstructionSelector(const LoongArchTargetMachine &,
LoongArchSubtarget &,
LoongArchRegisterBankInfo &);
} // end namespace llvm

#endif // LLVM_LIB_TARGET_LOONGARCH_LOONGARCH_H
1 change: 1 addition & 0 deletions llvm/lib/Target/LoongArch/LoongArch.td
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ def FeatureRelax
include "LoongArchRegisterInfo.td"
include "LoongArchCallingConv.td"
include "LoongArchInstrInfo.td"
include "LoongArchRegisterBanks.td"

//===----------------------------------------------------------------------===//
// LoongArch processors supported.
Expand Down
52 changes: 52 additions & 0 deletions llvm/lib/Target/LoongArch/LoongArchCallLowering.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
//===-- LoongArchCallLowering.cpp - Call lowering ---------------*- C++ -*-===//
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the filename is too generic, you can put this file and other files in a seperate sub-folder like GISel.

//
// 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 "LoongArchCallLowering.h"
#include "LoongArchISelLowering.h"
#include "llvm/CodeGen/GlobalISel/MachineIRBuilder.h"

using namespace llvm;

LoongArchCallLowering::LoongArchCallLowering(const LoongArchTargetLowering &TLI)
: CallLowering(&TLI) {}

bool LoongArchCallLowering::lowerReturn(MachineIRBuilder &MIRBuilder,
const Value *Val,
ArrayRef<Register> VRegs,
FunctionLoweringInfo &FLI,
Register SwiftErrorVReg) const {

MachineInstrBuilder Ret = MIRBuilder.buildInstrNoInsert(LoongArch::PseudoRET);

if (Val != nullptr)
return false;

MIRBuilder.insertInstr(Ret);
return true;
}

bool LoongArchCallLowering::lowerFormalArguments(
MachineIRBuilder &MIRBuilder, const Function &F,
ArrayRef<ArrayRef<Register>> VRegs, FunctionLoweringInfo &FLI) const {

if (F.arg_empty())
return true;

return false;
}

bool LoongArchCallLowering::lowerCall(MachineIRBuilder &MIRBuilder,
CallLoweringInfo &Info) const {
return false;
}
44 changes: 44 additions & 0 deletions llvm/lib/Target/LoongArch/LoongArchCallLowering.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//===-- LoongArchCallLowering.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_LoongArch_LoongArchCALLLOWERING_H
#define LLVM_LIB_TARGET_LoongArch_LoongArchCALLLOWERING_H

#include "llvm/CodeGen/CallingConvLower.h"
#include "llvm/CodeGen/GlobalISel/CallLowering.h"
#include "llvm/CodeGen/ValueTypes.h"

namespace llvm {

class LoongArchTargetLowering;

class LoongArchCallLowering : public CallLowering {

public:
LoongArchCallLowering(const LoongArchTargetLowering &TLI);

bool lowerReturn(MachineIRBuilder &MIRBuiler, const Value *Val,
ArrayRef<Register> VRegs, FunctionLoweringInfo &FLI,
Register SwiftErrorVReg) const override;

bool lowerFormalArguments(MachineIRBuilder &MIRBuilder, const Function &F,
ArrayRef<ArrayRef<Register>> VRegs,
FunctionLoweringInfo &FLI) const override;

bool lowerCall(MachineIRBuilder &MIRBuilder,
CallLoweringInfo &Info) const override;
};

} // end namespace llvm

#endif // LLVM_LIB_TARGET_LoongArch_LoongArchCALLLOWERING_H
104 changes: 104 additions & 0 deletions llvm/lib/Target/LoongArch/LoongArchInstructionSelector.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
//===-- LoongArchInstructionSelector.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 InstructionSelector class for
/// LoongArch.
/// \todo This should be generated by TableGen.
//===----------------------------------------------------------------------===//

#include "LoongArchRegisterBankInfo.h"
#include "LoongArchSubtarget.h"
#include "LoongArchTargetMachine.h"
#include "llvm/CodeGen/GlobalISel/GIMatchTableExecutorImpl.h"
#include "llvm/CodeGen/GlobalISel/InstructionSelector.h"
#include "llvm/IR/IntrinsicsLoongArch.h"
#include "llvm/Support/Debug.h"

#define DEBUG_TYPE "loongarch-isel"

using namespace llvm;

#define GET_GLOBALISEL_PREDICATE_BITSET
#include "LoongArchGenGlobalISel.inc"
#undef GET_GLOBALISEL_PREDICATE_BITSET

namespace {

class LoongArchInstructionSelector : public InstructionSelector {
public:
LoongArchInstructionSelector(const LoongArchTargetMachine &TM,
const LoongArchSubtarget &STI,
const LoongArchRegisterBankInfo &RBI);

bool select(MachineInstr &I) override;
static const char *getName() { return DEBUG_TYPE; }

private:
bool selectImpl(MachineInstr &I, CodeGenCoverage &CoverageInfo) const;

const LoongArchSubtarget &STI;
const LoongArchInstrInfo &TII;
const LoongArchRegisterInfo &TRI;
const LoongArchRegisterBankInfo &RBI;

// FIXME: This is necessary because DAGISel uses "Subtarget->" and GlobalISel
// uses "STI." in the code generated by TableGen. We need to unify the name of
// Subtarget variable.
const LoongArchSubtarget *Subtarget = &STI;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this variable Subtarget necessary?


#define GET_GLOBALISEL_PREDICATES_DECL
#include "LoongArchGenGlobalISel.inc"
#undef GET_GLOBALISEL_PREDICATES_DECL

#define GET_GLOBALISEL_TEMPORARIES_DECL
#include "LoongArchGenGlobalISel.inc"
#undef GET_GLOBALISEL_TEMPORARIES_DECL
};

} // end anonymous namespace

#define GET_GLOBALISEL_IMPL
#include "LoongArchGenGlobalISel.inc"
#undef GET_GLOBALISEL_IMPL

LoongArchInstructionSelector::LoongArchInstructionSelector(
const LoongArchTargetMachine &TM, const LoongArchSubtarget &STI,
const LoongArchRegisterBankInfo &RBI)
: InstructionSelector(), STI(STI), TII(*STI.getInstrInfo()),
TRI(*STI.getRegisterInfo()), RBI(RBI),

#define GET_GLOBALISEL_PREDICATES_INIT
#include "LoongArchGenGlobalISel.inc"
#undef GET_GLOBALISEL_PREDICATES_INIT
#define GET_GLOBALISEL_TEMPORARIES_INIT
#include "LoongArchGenGlobalISel.inc"
#undef GET_GLOBALISEL_TEMPORARIES_INIT
{
}

bool LoongArchInstructionSelector::select(MachineInstr &I) {

if (!isPreISelGenericOpcode(I.getOpcode())) {
// Certain non-generic instructions also need some special handling.
return true;
}

if (selectImpl(I, *CoverageInfo))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could fold to return selectImpl but I assume this will fill in other code later

return true;

return false;
}

namespace llvm {
InstructionSelector *
createLoongArchInstructionSelector(const LoongArchTargetMachine &TM,
LoongArchSubtarget &Subtarget,
LoongArchRegisterBankInfo &RBI) {
return new LoongArchInstructionSelector(TM, Subtarget, RBI);
}
} // end namespace llvm
23 changes: 23 additions & 0 deletions llvm/lib/Target/LoongArch/LoongArchLegalizerInfo.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//===-- LoongArchLegalizerInfo.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
/// LoongArch. \todo This should be generated by TableGen.
//===----------------------------------------------------------------------===//

#include "LoongArchLegalizerInfo.h"
#include "llvm/CodeGen/TargetOpcodes.h"
#include "llvm/CodeGen/ValueTypes.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/Type.h"

using namespace llvm;

LoongArchLegalizerInfo::LoongArchLegalizerInfo(const LoongArchSubtarget &ST) {
getLegacyLegalizerInfo().computeTables();
}
28 changes: 28 additions & 0 deletions llvm/lib/Target/LoongArch/LoongArchLegalizerInfo.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//===-- LoongArchLegalizerInfo.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
/// LoongArch. \todo This should be generated by TableGen.
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIB_TARGET_LoongArch_LoongArchMACHINELEGALIZER_H
#define LLVM_LIB_TARGET_LoongArch_LoongArchMACHINELEGALIZER_H

#include "llvm/CodeGen/GlobalISel/LegalizerInfo.h"

namespace llvm {

class LoongArchSubtarget;

/// This class provides the information for the target register banks.
class LoongArchLegalizerInfo : public LegalizerInfo {
public:
LoongArchLegalizerInfo(const LoongArchSubtarget &ST);
};
} // end namespace llvm
#endif
26 changes: 26 additions & 0 deletions llvm/lib/Target/LoongArch/LoongArchRegisterBankInfo.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//===-- LoongArchRegisterBankInfo.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
/// LoongArch. \todo This should be generated by TableGen.
//===----------------------------------------------------------------------===//

#include "LoongArchRegisterBankInfo.h"
#include "MCTargetDesc/LoongArchMCTargetDesc.h"
#include "llvm/CodeGen/MachineRegisterInfo.h"
#include "llvm/CodeGen/RegisterBank.h"
#include "llvm/CodeGen/RegisterBankInfo.h"
#include "llvm/CodeGen/TargetRegisterInfo.h"

#define GET_TARGET_REGBANK_IMPL
#include "LoongArchGenRegisterBank.inc"

using namespace llvm;

LoongArchRegisterBankInfo::LoongArchRegisterBankInfo(unsigned HwMode)
: LoongArchGenRegisterBankInfo(HwMode) {}
37 changes: 37 additions & 0 deletions llvm/lib/Target/LoongArch/LoongArchRegisterBankInfo.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//===-- LoongArchRegisterBankInfo.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
/// LoongArch. \todo This should be generated by TableGen.
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIB_TARGET_LoongArch_LoongArchREGISTERBANKINFO_H
#define LLVM_LIB_TARGET_LoongArch_LoongArchREGISTERBANKINFO_H

#include "llvm/CodeGen/RegisterBankInfo.h"

#define GET_REGBANK_DECLARATIONS
#include "LoongArchGenRegisterBank.inc"

namespace llvm {

class TargetRegisterInfo;

class LoongArchGenRegisterBankInfo : public RegisterBankInfo {
protected:
#define GET_TARGET_REGBANK_CLASS
#include "LoongArchGenRegisterBank.inc"
};

/// This class provides the information for the target register banks.
class LoongArchRegisterBankInfo final : public LoongArchGenRegisterBankInfo {
public:
LoongArchRegisterBankInfo(unsigned HwMode);
};
} // end namespace llvm
#endif
Loading