Skip to content

Commit cf78715

Browse files
committed
[CSKY] First patch to construct codegen infra and generate first add instruction
Ooops. It constructs codegen infra and provide only basic code to generate first add instruction successfully. Differential Revision: https://reviews.llvm.org/D112206
1 parent 264d3b6 commit cf78715

26 files changed

+1487
-0
lines changed

llvm/lib/Target/CSKY/CMakeLists.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,36 @@ set(LLVM_TARGET_DEFINITIONS CSKY.td)
44

55
tablegen(LLVM CSKYGenAsmMatcher.inc -gen-asm-matcher)
66
tablegen(LLVM CSKYGenAsmWriter.inc -gen-asm-writer)
7+
tablegen(LLVM CSKYGenCallingConv.inc -gen-callingconv)
8+
tablegen(LLVM CSKYGenDAGISel.inc -gen-dag-isel)
79
tablegen(LLVM CSKYGenInstrInfo.inc -gen-instr-info)
810
tablegen(LLVM CSKYGenMCCodeEmitter.inc -gen-emitter)
11+
tablegen(LLVM CSKYGenMCPseudoLowering.inc -gen-pseudo-lowering)
912
tablegen(LLVM CSKYGenRegisterInfo.inc -gen-register-info)
1013
tablegen(LLVM CSKYGenSubtargetInfo.inc -gen-subtarget)
1114

1215
add_public_tablegen_target(CSKYCommonTableGen)
1316

1417
add_llvm_target(CSKYCodeGen
18+
CSKYAsmPrinter.cpp
19+
CSKYFrameLowering.cpp
20+
CSKYInstrInfo.cpp
21+
CSKYISelDAGToDAG.cpp
22+
CSKYISelLowering.cpp
23+
CSKYMCInstLower.cpp
24+
CSKYRegisterInfo.cpp
1525
CSKYSubtarget.cpp
1626
CSKYTargetMachine.cpp
1727

1828
LINK_COMPONENTS
29+
Analysis
30+
AsmPrinter
1931
Core
2032
CodeGen
33+
CSKYDesc
2134
CSKYInfo
35+
MC
36+
SelectionDAG
2237
Support
2338
Target
2439

llvm/lib/Target/CSKY/CSKY.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//===-- CSKY.h - Top-level interface for CSKY--------------------*- 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+
// This file contains the entry points for global functions defined in the LLVM
10+
// CSKY back-end.
11+
//
12+
//===----------------------------------------------------------------------===//
13+
14+
#ifndef LLVM_LIB_TARGET_CSKY_CSKY_H
15+
#define LLVM_LIB_TARGET_CSKY_CSKY_H
16+
17+
#include "llvm/Target/TargetMachine.h"
18+
19+
namespace llvm {
20+
class CSKYTargetMachine;
21+
class FunctionPass;
22+
23+
FunctionPass *createCSKYISelDag(CSKYTargetMachine &TM);
24+
25+
} // namespace llvm
26+
27+
#endif // LLVM_LIB_TARGET_CSKY_CSKY_H

llvm/lib/Target/CSKY/CSKY.td

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ def iHas10E60 : Predicate<"Subtarget->has10E60()">,
8787
//===----------------------------------------------------------------------===//
8888

8989
include "CSKYRegisterInfo.td"
90+
include "CSKYCallingConv.td"
9091
include "CSKYInstrInfo.td"
9192

9293
//===----------------------------------------------------------------------===//
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
//===-- CSKYAsmPrinter.cpp - CSKY LLVM assembly writer --------------------===//
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+
// This file contains a printer that converts from our internal representation
10+
// of machine-dependent LLVM code to the CSKY assembly language.
11+
//
12+
//===----------------------------------------------------------------------===//
13+
#include "CSKYAsmPrinter.h"
14+
#include "CSKY.h"
15+
#include "CSKYTargetMachine.h"
16+
#include "MCTargetDesc/CSKYInstPrinter.h"
17+
#include "MCTargetDesc/CSKYMCExpr.h"
18+
#include "TargetInfo/CSKYTargetInfo.h"
19+
#include "llvm/ADT/Statistic.h"
20+
#include "llvm/CodeGen/AsmPrinter.h"
21+
#include "llvm/CodeGen/MachineConstantPool.h"
22+
#include "llvm/IR/DataLayout.h"
23+
#include "llvm/MC/MCAsmInfo.h"
24+
#include "llvm/MC/MCContext.h"
25+
#include "llvm/MC/MCInstBuilder.h"
26+
#include "llvm/MC/MCStreamer.h"
27+
#include "llvm/MC/TargetRegistry.h"
28+
29+
using namespace llvm;
30+
31+
#define DEBUG_TYPE "csky-asm-printer"
32+
33+
CSKYAsmPrinter::CSKYAsmPrinter(llvm::TargetMachine &TM,
34+
std::unique_ptr<llvm::MCStreamer> Streamer)
35+
: AsmPrinter(TM, std::move(Streamer)), MCInstLowering(OutContext, *this) {}
36+
37+
bool CSKYAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
38+
Subtarget = &MF.getSubtarget<CSKYSubtarget>();
39+
return AsmPrinter::runOnMachineFunction(MF);
40+
}
41+
42+
// Simple pseudo-instructions have their lowering (with expansion to real
43+
// instructions) auto-generated.
44+
#include "CSKYGenMCPseudoLowering.inc"
45+
46+
void CSKYAsmPrinter::emitInstruction(const MachineInstr *MI) {
47+
// Do any auto-generated pseudo lowerings.
48+
if (emitPseudoExpansionLowering(*OutStreamer, MI))
49+
return;
50+
51+
MCInst TmpInst;
52+
MCInstLowering.Lower(MI, TmpInst);
53+
EmitToStreamer(*OutStreamer, TmpInst);
54+
}
55+
56+
extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeCSKYAsmPrinter() {
57+
RegisterAsmPrinter<CSKYAsmPrinter> X(getTheCSKYTarget());
58+
}

llvm/lib/Target/CSKY/CSKYAsmPrinter.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//===-- CSKYAsmPrinter.h - CSKY implementation of AsmPrinter ----*- 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+
#ifndef LLVM_LIB_TARGET_CSKY_CSKYASMPRINTER_H
10+
#define LLVM_LIB_TARGET_CSKY_CSKYASMPRINTER_H
11+
12+
#include "CSKYMCInstLower.h"
13+
#include "CSKYSubtarget.h"
14+
#include "llvm/CodeGen/AsmPrinter.h"
15+
#include "llvm/MC/MCDirectives.h"
16+
17+
namespace llvm {
18+
class LLVM_LIBRARY_VISIBILITY CSKYAsmPrinter : public AsmPrinter {
19+
CSKYMCInstLower MCInstLowering;
20+
21+
const CSKYSubtarget *Subtarget;
22+
23+
public:
24+
explicit CSKYAsmPrinter(TargetMachine &TM,
25+
std::unique_ptr<MCStreamer> Streamer);
26+
27+
StringRef getPassName() const override { return "CSKY Assembly Printer"; }
28+
29+
/// tblgen'erated driver function for lowering simple MI->MC
30+
/// pseudo instructions.
31+
bool emitPseudoExpansionLowering(MCStreamer &OutStreamer,
32+
const MachineInstr *MI);
33+
34+
void emitInstruction(const MachineInstr *MI) override;
35+
36+
bool runOnMachineFunction(MachineFunction &MF) override;
37+
};
38+
} // end namespace llvm
39+
40+
#endif // LLVM_LIB_TARGET_CSKY_CSKYASMPRINTER_H
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
//=== CSKYCallingConv.h - CSKY Custom Calling Convention Routines -*-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+
// This file contains the custom routines for the CSKY Calling Convention that
10+
// aren't done by tablegen.
11+
//
12+
//===----------------------------------------------------------------------===//
13+
14+
#ifndef LLVM_LIB_TARGET_CSKY_CSKYCALLINGCONV_H
15+
#define LLVM_LIB_TARGET_CSKY_CSKYCALLINGCONV_H
16+
17+
#include "CSKY.h"
18+
#include "CSKYSubtarget.h"
19+
#include "llvm/CodeGen/CallingConvLower.h"
20+
#include "llvm/CodeGen/TargetInstrInfo.h"
21+
#include "llvm/IR/CallingConv.h"
22+
23+
namespace llvm {
24+
25+
static bool CC_CSKY_ABIV2_SOFT_64(unsigned &ValNo, MVT &ValVT, MVT &LocVT,
26+
CCValAssign::LocInfo &LocInfo,
27+
ISD::ArgFlagsTy &ArgFlags, CCState &State) {
28+
29+
static const MCPhysReg ArgGPRs[] = {CSKY::R0, CSKY::R1, CSKY::R2, CSKY::R3};
30+
Register Reg = State.AllocateReg(ArgGPRs);
31+
LocVT = MVT::i32;
32+
if (!Reg) {
33+
unsigned StackOffset = State.AllocateStack(8, Align(4));
34+
State.addLoc(
35+
CCValAssign::getMem(ValNo, ValVT, StackOffset, LocVT, LocInfo));
36+
return true;
37+
}
38+
if (!State.AllocateReg(ArgGPRs))
39+
State.AllocateStack(4, Align(4));
40+
State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
41+
return true;
42+
}
43+
44+
static bool Ret_CSKY_ABIV2_SOFT_64(unsigned &ValNo, MVT &ValVT, MVT &LocVT,
45+
CCValAssign::LocInfo &LocInfo,
46+
ISD::ArgFlagsTy &ArgFlags, CCState &State) {
47+
48+
static const MCPhysReg ArgGPRs[] = {CSKY::R0, CSKY::R1};
49+
Register Reg = State.AllocateReg(ArgGPRs);
50+
LocVT = MVT::i32;
51+
if (!Reg)
52+
return false;
53+
54+
if (!State.AllocateReg(ArgGPRs))
55+
return false;
56+
57+
State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
58+
return true;
59+
}
60+
61+
} // namespace llvm
62+
63+
#endif
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
//===-- CSKYCallingConv.td - Calling Conventions CSKY ----*- 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+
// This describes the calling conventions for the CSKY architecture.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
def CSR_I32 : CalleeSavedRegs<(add R8, R15, (sequence "R%u", 4, 7),
14+
(sequence "R%u", 9, 11), (sequence "R%u", 16, 17), R28)>;
15+
def CSR_GPR_FPR32 : CalleeSavedRegs<(add CSR_I32, (sequence "F%u_32", 8, 15))>;
16+
def CSR_GPR_FPR64 : CalleeSavedRegs<(add CSR_I32,
17+
(sequence "F%u_64", 8, 15))>;
18+
19+
// Interrupt handler needs to save/restore all registers that are used,
20+
// both Caller and Callee saved registers.
21+
def CSR_GPR_ISR : CalleeSavedRegs<(add R8, R15,
22+
(sequence "R%u", 0, 3),
23+
(sequence "R%u", 4, 7),
24+
(sequence "R%u", 9, 13),
25+
(sequence "R%u", 16, 31))>;
26+
27+
def CSR_GPR_FPR32_ISR: CalleeSavedRegs<(add CSR_GPR_ISR,
28+
(sequence "F%u_32", 0, 15))>;
29+
def CSR_GPR_FPR64_ISR: CalleeSavedRegs<(add CSR_GPR_ISR,
30+
(sequence "F%u_64", 0, 15))>;
31+
32+
def CSR_GPR_FPR32v3_ISR: CalleeSavedRegs<(add CSR_GPR_FPR32_ISR,
33+
(sequence "F%u_32", 16, 31))>;
34+
def CSR_GPR_FPR64v3_ISR: CalleeSavedRegs<(add CSR_GPR_FPR64_ISR,
35+
(sequence "F%u_64", 16, 31))>;
36+
37+
// Needed for implementation of CSKYRegisterInfo::getNoPreservedMask()
38+
def CSR_NoRegs : CalleeSavedRegs<(add)>;
39+
40+
def CC_CSKY_ABIV2_SOFT : CallingConv<[
41+
// DSP types
42+
CCIfType<[v2i16, v4i8], CCAssignToReg<[R0, R1, R2, R3]>>,
43+
CCIfType<[v2i16, v4i8], CCAssignToStack<4, 4>>,
44+
CCIfType<[i8, i16], CCPromoteToType<i32>>,
45+
CCIfType<[f32], CCAssignToReg<[R0, R1, R2, R3]>>,
46+
CCIfType<[f32], CCAssignToStack<4, 4>>,
47+
CCIfType<[i32], CCAssignToReg<[R0, R1, R2, R3]>>,
48+
CCIfType<[i32], CCAssignToStack<4, 4>>,
49+
CCIfType<[f64], CCCustom<"CC_CSKY_ABIV2_SOFT_64">>,
50+
CCIfType<[f64], CCAssignToStack<8, 4>>
51+
]>;
52+
53+
def RetCC_CSKY_ABIV2_SOFT : CallingConv<[
54+
// DSP types
55+
CCIfType<[v2i16, v4i8], CCAssignToReg<[R0, R1]>>,
56+
CCIfType<[i8, i16], CCPromoteToType<i32>>,
57+
CCIfType<[f32], CCBitConvertToType<i32>>,
58+
CCIfType<[i32], CCAssignToReg<[R0, R1]>>,
59+
CCIfType<[f64], CCCustom<"Ret_CSKY_ABIV2_SOFT_64">>
60+
]>;
61+
62+
def CC_CSKY_ABIV2_FP : CallingConv<[
63+
// DSP types
64+
CCIfType<[v2i16, v4i8], CCAssignToReg<[R0, R1, R2, R3]>>,
65+
CCIfType<[v2i16, v4i8], CCAssignToStack<4, 4>>,
66+
CCIfType<[i8, i16], CCPromoteToType<i32>>,
67+
CCIfType<[i32], CCAssignToReg<[R0, R1, R2, R3]>>,
68+
CCIfType<[i32], CCAssignToStack<4, 4>>,
69+
CCIfType<[f32], CCAssignToReg<[F0_32, F1_32, F2_32, F3_32]>>,
70+
CCIfType<[f32], CCAssignToStack<4, 4>>,
71+
CCIfType<[f64], CCAssignToReg<[F0_64, F1_64, F2_64, F3_64]>>,
72+
CCIfType<[f64], CCAssignToStack<8, 4>>
73+
]>;
74+
75+
def RetCC_CSKY_ABIV2_FP : CallingConv<[
76+
// DSP types
77+
CCIfType<[v2i16, v4i8], CCAssignToReg<[R0, R1]>>,
78+
CCIfType<[i8, i16], CCPromoteToType<i32>>,
79+
CCIfType<[i32], CCAssignToReg<[R0, R1]>>,
80+
CCIfType<[f32], CCAssignToReg<[F0_32]>>,
81+
CCIfType<[f64], CCAssignToReg<[F0_64]>>
82+
]>;
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
//===-- CSKYFrameLowering.cpp - CSKY Frame Information ------------------===//
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+
// This file contains the CSKY implementation of TargetFrameLowering class.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "CSKYFrameLowering.h"
14+
#include "CSKYSubtarget.h"
15+
#include "llvm/CodeGen/MachineFrameInfo.h"
16+
#include "llvm/CodeGen/MachineFunction.h"
17+
#include "llvm/CodeGen/MachineInstrBuilder.h"
18+
#include "llvm/CodeGen/MachineRegisterInfo.h"
19+
#include "llvm/CodeGen/RegisterScavenging.h"
20+
#include "llvm/IR/DiagnosticInfo.h"
21+
#include "llvm/MC/MCDwarf.h"
22+
23+
using namespace llvm;
24+
25+
#define DEBUG_TYPE "csky-frame-lowering"
26+
27+
// Returns the register used to hold the frame pointer.
28+
static Register getFPReg(const CSKYSubtarget &STI) { return CSKY::R8; }
29+
30+
// To avoid the BP value clobbered by a function call, we need to choose a
31+
// callee saved register to save the value.
32+
static Register getBPReg(const CSKYSubtarget &STI) { return CSKY::R7; }
33+
34+
bool CSKYFrameLowering::hasFP(const MachineFunction &MF) const {
35+
const TargetRegisterInfo *RegInfo = MF.getSubtarget().getRegisterInfo();
36+
37+
const MachineFrameInfo &MFI = MF.getFrameInfo();
38+
return MF.getTarget().Options.DisableFramePointerElim(MF) ||
39+
RegInfo->hasStackRealignment(MF) || MFI.hasVarSizedObjects() ||
40+
MFI.isFrameAddressTaken();
41+
}
42+
43+
bool CSKYFrameLowering::hasBP(const MachineFunction &MF) const {
44+
const MachineFrameInfo &MFI = MF.getFrameInfo();
45+
46+
return MFI.hasVarSizedObjects();
47+
}
48+
49+
void CSKYFrameLowering::emitPrologue(MachineFunction &MF,
50+
MachineBasicBlock &MBB) const {
51+
// FIXME: Implement this when we have function calls
52+
}
53+
54+
void CSKYFrameLowering::emitEpilogue(MachineFunction &MF,
55+
MachineBasicBlock &MBB) const {
56+
// FIXME: Implement this when we have function calls
57+
}

0 commit comments

Comments
 (0)