Skip to content

Commit 45525f6

Browse files
committed
[Xtensa] Initial codegen support from IR.
Initial codegen support for IR tests. Added basic implementation of the TargetFrameLowering, MCInstLower, AsmPrinter, RegisterInfo, InstructionInfo, TargetLowering, SelectionDAGISel.
1 parent f01b6ca commit 45525f6

20 files changed

+842
-2
lines changed

llvm/lib/Target/Xtensa/CMakeLists.txt

+10
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ set(LLVM_TARGET_DEFINITIONS Xtensa.td)
44

55
tablegen(LLVM XtensaGenAsmMatcher.inc -gen-asm-matcher)
66
tablegen(LLVM XtensaGenAsmWriter.inc -gen-asm-writer)
7+
tablegen(LLVM XtensaGenDAGISel.inc -gen-dag-isel)
78
tablegen(LLVM XtensaGenDisassemblerTables.inc -gen-disassembler)
89
tablegen(LLVM XtensaGenInstrInfo.inc -gen-instr-info)
910
tablegen(LLVM XtensaGenMCCodeEmitter.inc -gen-emitter)
@@ -13,13 +14,22 @@ tablegen(LLVM XtensaGenSubtargetInfo.inc -gen-subtarget)
1314
add_public_tablegen_target(XtensaCommonTableGen)
1415

1516
add_llvm_target(XtensaCodeGen
17+
XtensaAsmPrinter.cpp
18+
XtensaFrameLowering.cpp
19+
XtensaInstrInfo.cpp
20+
XtensaISelDAGToDAG.cpp
21+
XtensaISelLowering.cpp
22+
XtensaMCInstLower.cpp
23+
XtensaRegisterInfo.cpp
24+
XtensaSubtarget.cpp
1625
XtensaTargetMachine.cpp
1726

1827
LINK_COMPONENTS
1928
AsmPrinter
2029
CodeGen
2130
Core
2231
MC
32+
SelectionDAG
2333
Support
2434
Target
2535
XtensaDesc

llvm/lib/Target/Xtensa/Xtensa.h

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//===- Xtensa.h - Top-level interface for Xtensa representation -*- C++ -*-===//
2+
//
3+
// The LLVM Compiler Infrastructure
4+
//
5+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
6+
// See https://llvm.org/LICENSE.txt for license information.
7+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8+
//
9+
//===----------------------------------------------------------------------===//
10+
//
11+
// This file contains the entry points for global functions defined in
12+
// the LLVM Xtensa back-end.
13+
//
14+
//===----------------------------------------------------------------------===//
15+
16+
#ifndef LLVM_LIB_TARGET_XTENSA_XTENSA_H
17+
#define LLVM_LIB_TARGET_XTENSA_XTENSA_H
18+
19+
#include "MCTargetDesc/XtensaMCTargetDesc.h"
20+
#include "llvm/PassRegistry.h"
21+
#include "llvm/Support/CodeGen.h"
22+
23+
namespace llvm {
24+
class XtensaTargetMachine;
25+
class FunctionPass;
26+
27+
FunctionPass *createXtensaISelDag(XtensaTargetMachine &TM,
28+
CodeGenOptLevel OptLevel);
29+
} // namespace llvm
30+
#endif // LLVM_LIB_TARGET_XTENSA_XTENSA_H
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
//===- XtensaAsmPrinter.cpp Xtensa LLVM Assembly Printer ------------------===//
2+
//
3+
// The LLVM Compiler Infrastructure
4+
//
5+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
6+
// See https://llvm.org/LICENSE.txt for license information.
7+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8+
//
9+
//===----------------------------------------------------------------------===//
10+
//
11+
// This file contains a printer that converts from our internal representation
12+
// of machine-dependent LLVM code to GAS-format Xtensa assembly language.
13+
//
14+
//===----------------------------------------------------------------------===//
15+
16+
#include "XtensaAsmPrinter.h"
17+
#include "TargetInfo/XtensaTargetInfo.h"
18+
#include "XtensaMCInstLower.h"
19+
#include "llvm/BinaryFormat/ELF.h"
20+
#include "llvm/CodeGen/MachineModuleInfoImpls.h"
21+
#include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
22+
#include "llvm/MC/MCExpr.h"
23+
#include "llvm/MC/MCSectionELF.h"
24+
#include "llvm/MC/MCStreamer.h"
25+
#include "llvm/MC/MCSymbol.h"
26+
#include "llvm/MC/MCSymbolELF.h"
27+
#include "llvm/MC/TargetRegistry.h"
28+
29+
using namespace llvm;
30+
31+
void XtensaAsmPrinter::emitInstruction(const MachineInstr *MI) {
32+
XtensaMCInstLower Lower(MF->getContext(), *this);
33+
MCInst LoweredMI;
34+
Lower.lower(MI, LoweredMI);
35+
EmitToStreamer(*OutStreamer, LoweredMI);
36+
}
37+
38+
// Force static initialization.
39+
extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeXtensaAsmPrinter() {
40+
RegisterAsmPrinter<XtensaAsmPrinter> A(getTheXtensaTarget());
41+
}
+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
//===- XtensaAsmPrinter.h - Xtensa LLVM Assembly Printer --------*- C++-*--===//
2+
//
3+
// The LLVM Compiler Infrastructure
4+
//
5+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
6+
// See https://llvm.org/LICENSE.txt for license information.
7+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8+
//
9+
//===----------------------------------------------------------------------===//
10+
//
11+
// Xtensa Assembly printer class.
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
#ifndef LLVM_LIB_TARGET_XTENSA_XTENSAASMPRINTER_H
16+
#define LLVM_LIB_TARGET_XTENSA_XTENSAASMPRINTER_H
17+
18+
#include "XtensaTargetMachine.h"
19+
#include "llvm/CodeGen/AsmPrinter.h"
20+
#include "llvm/Support/Compiler.h"
21+
22+
namespace llvm {
23+
class MCStreamer;
24+
class MachineBasicBlock;
25+
class MachineInstr;
26+
class Module;
27+
class raw_ostream;
28+
29+
class LLVM_LIBRARY_VISIBILITY XtensaAsmPrinter : public AsmPrinter {
30+
const MCSubtargetInfo *STI;
31+
32+
public:
33+
explicit XtensaAsmPrinter(TargetMachine &TM,
34+
std::unique_ptr<MCStreamer> Streamer)
35+
: AsmPrinter(TM, std::move(Streamer)), STI(TM.getMCSubtargetInfo()) {}
36+
37+
StringRef getPassName() const override { return "Xtensa Assembly Printer"; }
38+
void emitInstruction(const MachineInstr *MI) override;
39+
};
40+
} // end namespace llvm
41+
42+
#endif /* LLVM_LIB_TARGET_XTENSA_XTENSAASMPRINTER_H */
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//===- XtensaFrameLowering.cpp - Xtensa Frame Information -----------------===//
2+
//
3+
// The LLVM Compiler Infrastructure
4+
//
5+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
6+
// See https://llvm.org/LICENSE.txt for license information.
7+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8+
//
9+
//===----------------------------------------------------------------------===//
10+
//
11+
// This file contains the Xtensa implementation of TargetFrameLowering class.
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
#include "XtensaFrameLowering.h"
16+
#include "XtensaInstrInfo.h"
17+
#include "XtensaSubtarget.h"
18+
#include "llvm/CodeGen/MachineFrameInfo.h"
19+
#include "llvm/CodeGen/MachineInstrBuilder.h"
20+
#include "llvm/CodeGen/MachineModuleInfo.h"
21+
#include "llvm/CodeGen/MachineRegisterInfo.h"
22+
#include "llvm/CodeGen/RegisterScavenging.h"
23+
#include "llvm/IR/Function.h"
24+
25+
using namespace llvm;
26+
27+
XtensaFrameLowering::XtensaFrameLowering()
28+
: TargetFrameLowering(TargetFrameLowering::StackGrowsDown, Align(4), 0,
29+
Align(4)) {}
30+
31+
// hasFP - Return true if the specified function should have a dedicated frame
32+
// pointer register. This is true if the function has variable sized allocas or
33+
// if frame pointer elimination is disabled.
34+
bool XtensaFrameLowering::hasFP(const MachineFunction &MF) const {
35+
const MachineFrameInfo &MFI = MF.getFrameInfo();
36+
return MF.getTarget().Options.DisableFramePointerElim(MF) ||
37+
MFI.hasVarSizedObjects();
38+
}
39+
40+
void XtensaFrameLowering::emitPrologue(MachineFunction &MF,
41+
MachineBasicBlock &MBB) const {}
42+
43+
void XtensaFrameLowering::emitEpilogue(MachineFunction &MF,
44+
MachineBasicBlock &MBB) const {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//===- XtensaFrameLowering.h - Define frame lowering for Xtensa --*- C++ -*-==//
2+
//
3+
// The LLVM Compiler Infrastructure
4+
//
5+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
6+
// See https://llvm.org/LICENSE.txt for license information.
7+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8+
//
9+
//===-----------------------------------------------------------------------==//
10+
11+
#ifndef LLVM_LIB_TARGET_XTENSA_XTENSAFRAMELOWERING_H
12+
#define LLVM_LIB_TARGET_XTENSA_XTENSAFRAMELOWERING_H
13+
14+
#include "llvm/CodeGen/TargetFrameLowering.h"
15+
16+
namespace llvm {
17+
class XtensaTargetMachine;
18+
class XtensaSubtarget;
19+
20+
class XtensaFrameLowering : public TargetFrameLowering {
21+
public:
22+
XtensaFrameLowering();
23+
24+
bool hasFP(const MachineFunction &MF) const override;
25+
26+
/// emitProlog/emitEpilog - These methods insert prolog and epilog code into
27+
/// the function.
28+
void emitPrologue(MachineFunction &, MachineBasicBlock &) const override;
29+
void emitEpilogue(MachineFunction &MF, MachineBasicBlock &MBB) const override;
30+
};
31+
32+
} // namespace llvm
33+
34+
#endif /* LLVM_LIB_TARGET_XTENSA_XTENSAFRAMELOWERING_H */
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
//===- XtensaISelDAGToDAG.cpp - A dag to dag inst selector for Xtensa -----===//
2+
//
3+
// The LLVM Compiler Infrastructure
4+
//
5+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
6+
// See https://llvm.org/LICENSE.txt for license information.
7+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8+
//
9+
//===----------------------------------------------------------------------===//
10+
//
11+
// This file defines an instruction selector for the Xtensa target.
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
#include "Xtensa.h"
16+
#include "XtensaTargetMachine.h"
17+
#include "llvm/CodeGen/MachineFunction.h"
18+
#include "llvm/CodeGen/MachineRegisterInfo.h"
19+
#include "llvm/CodeGen/SelectionDAGISel.h"
20+
#include "llvm/Support/Debug.h"
21+
#include "llvm/Support/raw_ostream.h"
22+
23+
using namespace llvm;
24+
25+
#define DEBUG_TYPE "xtensa-isel"
26+
27+
namespace {
28+
29+
class XtensaDAGToDAGISel : public SelectionDAGISel {
30+
public:
31+
static char ID;
32+
33+
XtensaDAGToDAGISel(XtensaTargetMachine &TM, CodeGenOptLevel OptLevel)
34+
: SelectionDAGISel(ID, TM, OptLevel) {}
35+
36+
StringRef getPassName() const override {
37+
return "Xtensa DAG->DAG Pattern Instruction Selection";
38+
}
39+
40+
void Select(SDNode *Node) override;
41+
42+
bool selectMemRegAddr(SDValue Addr, SDValue &Base, SDValue &Offset,
43+
int Scale) {
44+
report_fatal_error("MemReg address is not implemented yet");
45+
}
46+
47+
bool selectMemRegAddrISH1(SDValue Addr, SDValue &Base, SDValue &Offset) {
48+
return selectMemRegAddr(Addr, Base, Offset, 1);
49+
}
50+
51+
bool selectMemRegAddrISH2(SDValue Addr, SDValue &Base, SDValue &Offset) {
52+
return selectMemRegAddr(Addr, Base, Offset, 2);
53+
}
54+
55+
bool selectMemRegAddrISH4(SDValue Addr, SDValue &Base, SDValue &Offset) {
56+
return selectMemRegAddr(Addr, Base, Offset, 4);
57+
}
58+
59+
// Include the pieces autogenerated from the target description.
60+
#include "XtensaGenDAGISel.inc"
61+
}; // namespace
62+
} // end anonymous namespace
63+
64+
char XtensaDAGToDAGISel::ID = 0;
65+
66+
FunctionPass *llvm::createXtensaISelDag(XtensaTargetMachine &TM,
67+
CodeGenOptLevel OptLevel) {
68+
return new XtensaDAGToDAGISel(TM, OptLevel);
69+
}
70+
71+
void XtensaDAGToDAGISel::Select(SDNode *Node) {
72+
SDLoc DL(Node);
73+
74+
// If we have a custom node, we already have selected!
75+
if (Node->isMachineOpcode()) {
76+
Node->setNodeId(-1);
77+
return;
78+
}
79+
80+
SelectCode(Node);
81+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
//===- XtensaISelLowering.cpp - Xtensa DAG Lowering Implementation --------===//
2+
//
3+
// The LLVM Compiler Infrastructure
4+
//
5+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
6+
// See https://llvm.org/LICENSE.txt for license information.
7+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8+
//
9+
//===----------------------------------------------------------------------===//
10+
//
11+
// This file defines the interfaces that Xtensa uses to lower LLVM code into a
12+
// selection DAG.
13+
//
14+
//===----------------------------------------------------------------------===//
15+
16+
#include "XtensaISelLowering.h"
17+
#include "XtensaSubtarget.h"
18+
#include "XtensaTargetMachine.h"
19+
#include "llvm/CodeGen/CallingConvLower.h"
20+
#include "llvm/CodeGen/MachineFrameInfo.h"
21+
#include "llvm/CodeGen/MachineInstrBuilder.h"
22+
#include "llvm/CodeGen/MachineJumpTableInfo.h"
23+
#include "llvm/CodeGen/MachineRegisterInfo.h"
24+
#include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
25+
#include "llvm/Support/Debug.h"
26+
#include "llvm/Support/ErrorHandling.h"
27+
#include "llvm/Support/raw_ostream.h"
28+
29+
using namespace llvm;
30+
31+
#define DEBUG_TYPE "xtensa-lower"
32+
33+
XtensaTargetLowering::XtensaTargetLowering(const TargetMachine &tm,
34+
const XtensaSubtarget &STI)
35+
: TargetLowering(tm), Subtarget(STI) {
36+
// Set up the register classes.
37+
addRegisterClass(MVT::i32, &Xtensa::ARRegClass);
38+
39+
// Set up special registers.
40+
setStackPointerRegisterToSaveRestore(Xtensa::SP);
41+
42+
setSchedulingPreference(Sched::RegPressure);
43+
44+
setMinFunctionAlignment(Align(4));
45+
46+
// Compute derived properties from the register classes
47+
computeRegisterProperties(STI.getRegisterInfo());
48+
}
49+
50+
SDValue XtensaTargetLowering::LowerOperation(SDValue Op,
51+
SelectionDAG &DAG) const {
52+
switch (Op.getOpcode()) {
53+
default:
54+
report_fatal_error("Unexpected node to lower");
55+
}
56+
}
57+
58+
const char *XtensaTargetLowering::getTargetNodeName(unsigned Opcode) const {
59+
return NULL;
60+
}

0 commit comments

Comments
 (0)