Skip to content

Commit 1a22a09

Browse files
committed
[LoongArch][GlobalISel] Adding initial GlobalISel infrastructure
Add an initial GlobalISel skeleton for LoongArch. It can only run ir translator for `ret void`.
1 parent 6aeffa1 commit 1a22a09

16 files changed

+458
-0
lines changed

llvm/lib/Target/LoongArch/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,21 @@ tablegen(LLVM LoongArchGenAsmMatcher.inc -gen-asm-matcher)
66
tablegen(LLVM LoongArchGenAsmWriter.inc -gen-asm-writer)
77
tablegen(LLVM LoongArchGenDAGISel.inc -gen-dag-isel)
88
tablegen(LLVM LoongArchGenDisassemblerTables.inc -gen-disassembler)
9+
tablegen(LLVM LoongArchGenGlobalISel.inc -gen-global-isel)
910
tablegen(LLVM LoongArchGenInstrInfo.inc -gen-instr-info)
1011
tablegen(LLVM LoongArchGenMCPseudoLowering.inc -gen-pseudo-lowering)
1112
tablegen(LLVM LoongArchGenMCCodeEmitter.inc -gen-emitter)
13+
tablegen(LLVM LoongArchGenRegisterBank.inc -gen-register-bank)
1214
tablegen(LLVM LoongArchGenRegisterInfo.inc -gen-register-info)
1315
tablegen(LLVM LoongArchGenSubtargetInfo.inc -gen-subtarget)
1416

1517
add_public_tablegen_target(LoongArchCommonTableGen)
1618

1719
add_llvm_target(LoongArchCodeGen
20+
GISel/LoongArchCallLowering.cpp
21+
GISel/LoongArchInstructionSelector.cpp
22+
GISel/LoongArchLegalizerInfo.cpp
23+
GISel/LoongArchRegisterBankInfo.cpp
1824
LoongArchAsmPrinter.cpp
1925
LoongArchDeadRegisterDefinitions.cpp
2026
LoongArchExpandAtomicPseudoInsts.cpp
@@ -37,6 +43,7 @@ add_llvm_target(LoongArchCodeGen
3743
CodeGen
3844
CodeGenTypes
3945
Core
46+
GlobalISel
4047
LoongArchDesc
4148
LoongArchInfo
4249
MC
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
//===-- LoongArchCallLowering.cpp - Call lowering ---------------*- C++ -*-===//
2+
//
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
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
/// \file
10+
/// This file implements the lowering of LLVM calls to machine code calls for
11+
/// GlobalISel.
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
#include "LoongArchCallLowering.h"
16+
#include "LoongArchISelLowering.h"
17+
#include "llvm/CodeGen/GlobalISel/MachineIRBuilder.h"
18+
19+
using namespace llvm;
20+
21+
LoongArchCallLowering::LoongArchCallLowering(const LoongArchTargetLowering &TLI)
22+
: CallLowering(&TLI) {}
23+
24+
bool LoongArchCallLowering::lowerReturn(MachineIRBuilder &MIRBuilder,
25+
const Value *Val,
26+
ArrayRef<Register> VRegs,
27+
FunctionLoweringInfo &FLI) const {
28+
MachineInstrBuilder Ret = MIRBuilder.buildInstrNoInsert(LoongArch::PseudoRET);
29+
30+
if (Val != nullptr)
31+
return false;
32+
33+
MIRBuilder.insertInstr(Ret);
34+
return true;
35+
}
36+
37+
bool LoongArchCallLowering::lowerFormalArguments(
38+
MachineIRBuilder &MIRBuilder, const Function &F,
39+
ArrayRef<ArrayRef<Register>> VRegs, FunctionLoweringInfo &FLI) const {
40+
if (F.arg_empty())
41+
return true;
42+
43+
return false;
44+
}
45+
46+
bool LoongArchCallLowering::lowerCall(MachineIRBuilder &MIRBuilder,
47+
CallLoweringInfo &Info) const {
48+
return false;
49+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//===-- LoongArchCallLowering.h - Call lowering -----------------*- C++ -*-===//
2+
//
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
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
/// \file
10+
/// This file describes how to lower LLVM calls to machine code calls.
11+
//
12+
//===----------------------------------------------------------------------===//
13+
14+
#ifndef LLVM_LIB_TARGET_LOONGARCH_LOONGARCHCALLLOWERING_H
15+
#define LLVM_LIB_TARGET_LOONGARCH_LOONGARCHCALLLOWERING_H
16+
17+
#include "llvm/CodeGen/CallingConvLower.h"
18+
#include "llvm/CodeGen/GlobalISel/CallLowering.h"
19+
20+
namespace llvm {
21+
22+
class LoongArchTargetLowering;
23+
class MachineIRBuilder;
24+
25+
class LoongArchCallLowering : public CallLowering {
26+
27+
public:
28+
LoongArchCallLowering(const LoongArchTargetLowering &TLI);
29+
30+
bool lowerReturn(MachineIRBuilder &MIRBuiler, const Value *Val,
31+
ArrayRef<Register> VRegs,
32+
FunctionLoweringInfo &FLI) const override;
33+
34+
bool lowerFormalArguments(MachineIRBuilder &MIRBuilder, const Function &F,
35+
ArrayRef<ArrayRef<Register>> VRegs,
36+
FunctionLoweringInfo &FLI) const override;
37+
38+
bool lowerCall(MachineIRBuilder &MIRBuilder,
39+
CallLoweringInfo &Info) const override;
40+
};
41+
42+
} // end namespace llvm
43+
44+
#endif // LLVM_LIB_TARGET_LOONGARCH_LOONGARCHCALLLOWERING_H
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
//===-- LoongArchInstructionSelector.cpp -------------------------*- C++ -*-==//
2+
//
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
6+
//
7+
//===----------------------------------------------------------------------===//
8+
/// \file
9+
/// This file implements the targeting of the InstructionSelector class for
10+
/// LoongArch.
11+
/// \todo This should be generated by TableGen.
12+
//===----------------------------------------------------------------------===//
13+
14+
#include "LoongArchRegisterBankInfo.h"
15+
#include "LoongArchSubtarget.h"
16+
#include "LoongArchTargetMachine.h"
17+
#include "llvm/CodeGen/GlobalISel/GIMatchTableExecutorImpl.h"
18+
#include "llvm/CodeGen/GlobalISel/InstructionSelector.h"
19+
#include "llvm/IR/IntrinsicsLoongArch.h"
20+
#include "llvm/Support/Debug.h"
21+
22+
#define DEBUG_TYPE "loongarch-isel"
23+
24+
using namespace llvm;
25+
26+
#define GET_GLOBALISEL_PREDICATE_BITSET
27+
#include "LoongArchGenGlobalISel.inc"
28+
#undef GET_GLOBALISEL_PREDICATE_BITSET
29+
30+
namespace {
31+
32+
class LoongArchInstructionSelector : public InstructionSelector {
33+
public:
34+
LoongArchInstructionSelector(const LoongArchTargetMachine &TM,
35+
const LoongArchSubtarget &STI,
36+
const LoongArchRegisterBankInfo &RBI);
37+
38+
bool select(MachineInstr &MI) override;
39+
40+
static const char *getName() { return DEBUG_TYPE; }
41+
42+
private:
43+
// tblgen-erated 'select' implementation, used as the initial selector for
44+
// the patterns that don't require complex C++.
45+
bool selectImpl(MachineInstr &I, CodeGenCoverage &CoverageInfo) const;
46+
47+
const LoongArchSubtarget &STI;
48+
const LoongArchInstrInfo &TII;
49+
const LoongArchRegisterInfo &TRI;
50+
const LoongArchRegisterBankInfo &RBI;
51+
const LoongArchTargetMachine &TM;
52+
53+
#define GET_GLOBALISEL_PREDICATES_DECL
54+
#include "LoongArchGenGlobalISel.inc"
55+
#undef GET_GLOBALISEL_PREDICATES_DECL
56+
57+
#define GET_GLOBALISEL_TEMPORARIES_DECL
58+
#include "LoongArchGenGlobalISel.inc"
59+
#undef GET_GLOBALISEL_TEMPORARIES_DECL
60+
};
61+
62+
} // end anonymous namespace
63+
64+
#define GET_GLOBALISEL_IMPL
65+
#include "LoongArchGenGlobalISel.inc"
66+
#undef GET_GLOBALISEL_IMPL
67+
68+
LoongArchInstructionSelector::LoongArchInstructionSelector(
69+
const LoongArchTargetMachine &TM, const LoongArchSubtarget &STI,
70+
const LoongArchRegisterBankInfo &RBI)
71+
: STI(STI), TII(*STI.getInstrInfo()), TRI(*STI.getRegisterInfo()), RBI(RBI),
72+
TM(TM),
73+
74+
#define GET_GLOBALISEL_PREDICATES_INIT
75+
#include "LoongArchGenGlobalISel.inc"
76+
#undef GET_GLOBALISEL_PREDICATES_INIT
77+
#define GET_GLOBALISEL_TEMPORARIES_INIT
78+
#include "LoongArchGenGlobalISel.inc"
79+
#undef GET_GLOBALISEL_TEMPORARIES_INIT
80+
{
81+
}
82+
83+
bool LoongArchInstructionSelector::select(MachineInstr &MI) {
84+
if (!isPreISelGenericOpcode(MI.getOpcode())) {
85+
// Certain non-generic instructions also need some special handling.
86+
return true;
87+
}
88+
89+
if (selectImpl(MI, *CoverageInfo))
90+
return true;
91+
92+
return false;
93+
}
94+
95+
namespace llvm {
96+
InstructionSelector *
97+
createLoongArchInstructionSelector(const LoongArchTargetMachine &TM,
98+
const LoongArchSubtarget &Subtarget,
99+
const LoongArchRegisterBankInfo &RBI) {
100+
return new LoongArchInstructionSelector(TM, Subtarget, RBI);
101+
}
102+
} // end namespace llvm
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//===-- LoongArchLegalizerInfo.cpp ------------------------------*- C++ -*-===//
2+
//
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
6+
//
7+
//===----------------------------------------------------------------------===//
8+
/// \file
9+
/// This file implements the targeting of the Machinelegalizer class for
10+
/// LoongArch.
11+
/// \todo This should be generated by TableGen.
12+
//===----------------------------------------------------------------------===//
13+
14+
#include "LoongArchLegalizerInfo.h"
15+
#include "llvm/CodeGen/TargetOpcodes.h"
16+
#include "llvm/CodeGen/ValueTypes.h"
17+
#include "llvm/IR/DerivedTypes.h"
18+
#include "llvm/IR/Type.h"
19+
20+
using namespace llvm;
21+
22+
LoongArchLegalizerInfo::LoongArchLegalizerInfo(const LoongArchSubtarget &ST) {
23+
getLegacyLegalizerInfo().computeTables();
24+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//===-- LoongArchLegalizerInfo.h --------------------------------*- C++ -*-===//
2+
//
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
6+
//
7+
//===----------------------------------------------------------------------===//
8+
/// \file
9+
/// This file declares the targeting of the Machinelegalizer class for
10+
/// LoongArch.
11+
/// \todo This should be generated by TableGen.
12+
//===----------------------------------------------------------------------===//
13+
14+
#ifndef LLVM_LIB_TARGET_LOONGARCH_LOONGARCHMACHINELEGALIZER_H
15+
#define LLVM_LIB_TARGET_LOONGARCH_LOONGARCHMACHINELEGALIZER_H
16+
17+
#include "llvm/CodeGen/GlobalISel/LegalizerInfo.h"
18+
19+
namespace llvm {
20+
21+
class LoongArchSubtarget;
22+
23+
class LoongArchLegalizerInfo : public LegalizerInfo {
24+
public:
25+
LoongArchLegalizerInfo(const LoongArchSubtarget &ST);
26+
};
27+
} // end namespace llvm
28+
29+
#endif // LLVM_LIB_TARGET_LOONGARCH_LOONGARCHMACHINELEGALIZER_H
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//===-- LoongArchRegisterBankInfo.cpp ---------------------------*- C++ -*-===//
2+
//
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
6+
//
7+
//===----------------------------------------------------------------------===//
8+
/// \file
9+
/// This file implements the targeting of the RegisterBankInfo class for
10+
/// LoongArch.
11+
/// \todo This should be generated by TableGen.
12+
//===----------------------------------------------------------------------===//
13+
14+
#include "LoongArchRegisterBankInfo.h"
15+
#include "MCTargetDesc/LoongArchMCTargetDesc.h"
16+
#include "llvm/CodeGen/MachineRegisterInfo.h"
17+
#include "llvm/CodeGen/RegisterBank.h"
18+
#include "llvm/CodeGen/RegisterBankInfo.h"
19+
#include "llvm/CodeGen/TargetRegisterInfo.h"
20+
21+
#define GET_TARGET_REGBANK_IMPL
22+
#include "LoongArchGenRegisterBank.inc"
23+
24+
using namespace llvm;
25+
26+
LoongArchRegisterBankInfo::LoongArchRegisterBankInfo(unsigned HwMode)
27+
: LoongArchGenRegisterBankInfo(HwMode) {}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//===-- LoongArchRegisterBankInfo.h -----------------------------*- C++ -*-===//
2+
//
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
6+
//
7+
//===----------------------------------------------------------------------===//
8+
/// \file
9+
/// This file declares the targeting of the RegisterBankInfo class for
10+
/// LoongArch.
11+
/// \todo This should be generated by TableGen.
12+
//===----------------------------------------------------------------------===//
13+
14+
#ifndef LLVM_LIB_TARGET_LOONGARCH_LOONGARCHREGISTERBANKINFO_H
15+
#define LLVM_LIB_TARGET_LOONGARCH_LOONGARCHREGISTERBANKINFO_H
16+
17+
#include "llvm/CodeGen/RegisterBankInfo.h"
18+
19+
#define GET_REGBANK_DECLARATIONS
20+
#include "LoongArchGenRegisterBank.inc"
21+
22+
namespace llvm {
23+
24+
class TargetRegisterInfo;
25+
26+
class LoongArchGenRegisterBankInfo : public RegisterBankInfo {
27+
protected:
28+
#define GET_TARGET_REGBANK_CLASS
29+
#include "LoongArchGenRegisterBank.inc"
30+
};
31+
32+
/// This class provides the information for the target register banks.
33+
class LoongArchRegisterBankInfo final : public LoongArchGenRegisterBankInfo {
34+
public:
35+
LoongArchRegisterBankInfo(unsigned HwMode);
36+
};
37+
} // end namespace llvm
38+
39+
#endif // LLVM_LIB_TARGET_LOONGARCH_LOONGARCHREGISTERBANKINFO_H
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//=- LoongArchRegisterBank.td - Describe the LoongArch Banks -*- tablegen -*-=//
2+
//
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
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
//
10+
//===----------------------------------------------------------------------===//
11+
12+
/// General Purpose Registers: R.
13+
def GPRBRegBank : RegisterBank<"GPRB", [GPR]>;
14+
15+
/// Floating Point Registers: F.
16+
def FPRBRegBank : RegisterBank<"FPRB", [FPR64]>;

llvm/lib/Target/LoongArch/LoongArch.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
namespace llvm {
2121
class AsmPrinter;
2222
class FunctionPass;
23+
class InstructionSelector;
24+
class LoongArchRegisterBankInfo;
25+
class LoongArchSubtarget;
2326
class LoongArchTargetMachine;
2427
class MCInst;
2528
class MCOperand;
@@ -47,6 +50,11 @@ void initializeLoongArchMergeBaseOffsetOptPass(PassRegistry &);
4750
void initializeLoongArchOptWInstrsPass(PassRegistry &);
4851
void initializeLoongArchPreRAExpandPseudoPass(PassRegistry &);
4952
void initializeLoongArchExpandPseudoPass(PassRegistry &);
53+
54+
InstructionSelector *
55+
createLoongArchInstructionSelector(const LoongArchTargetMachine &,
56+
const LoongArchSubtarget &,
57+
const LoongArchRegisterBankInfo &);
5058
} // end namespace llvm
5159

5260
#endif // LLVM_LIB_TARGET_LOONGARCH_LOONGARCH_H

0 commit comments

Comments
 (0)