LLVM 22.0.0git
LoongArchMCTargetDesc.cpp
Go to the documentation of this file.
1//===-- LoongArchMCTargetDesc.cpp - LoongArch Target Descriptions ---------===//
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 provides LoongArch specific target descriptions.
10//
11//===----------------------------------------------------------------------===//
12
16#include "LoongArchMCAsmInfo.h"
19#include "llvm/MC/MCAsmInfo.h"
21#include "llvm/MC/MCDwarf.h"
23#include "llvm/MC/MCInstrInfo.h"
29#include <bitset>
30
31#define GET_INSTRINFO_MC_DESC
32#define ENABLE_INSTR_PREDICATE_VERIFIER
33#include "LoongArchGenInstrInfo.inc"
34
35#define GET_REGINFO_MC_DESC
36#include "LoongArchGenRegisterInfo.inc"
37
38#define GET_SUBTARGETINFO_MC_DESC
39#include "LoongArchGenSubtargetInfo.inc"
40
41using namespace llvm;
42
45 InitLoongArchMCRegisterInfo(X, LoongArch::R1);
46 return X;
47}
48
50 MCInstrInfo *X = new MCInstrInfo();
51 InitLoongArchMCInstrInfo(X);
52 return X;
53}
54
55static MCSubtargetInfo *
57 if (CPU.empty() || CPU == "generic")
58 CPU = TT.isArch64Bit() ? "generic-la64" : "generic-la32";
59 return createLoongArchMCSubtargetInfoImpl(TT, CPU, /*TuneCPU*/ CPU, FS);
60}
61
63 const Triple &TT,
64 const MCTargetOptions &Options) {
65 MCAsmInfo *MAI = new LoongArchMCAsmInfo(TT);
66
67 // Initial state of the frame pointer is sp(r3).
68 unsigned SP = MRI.getDwarfRegNum(LoongArch::R3, true);
69 MCCFIInstruction Inst = MCCFIInstruction::cfiDefCfa(nullptr, SP, 0);
70 MAI->addInitialFrameState(Inst);
71
72 return MAI;
73}
74
76 unsigned SyntaxVariant,
77 const MCAsmInfo &MAI,
78 const MCInstrInfo &MII,
79 const MCRegisterInfo &MRI) {
80 return new LoongArchInstPrinter(MAI, MII, MRI);
81}
82
83static MCTargetStreamer *
86 ? new LoongArchTargetELFStreamer(S, STI)
87 : nullptr;
88}
89
90static MCTargetStreamer *
92 MCInstPrinter *InstPrint) {
93 return new LoongArchTargetAsmStreamer(S, OS);
94}
95
96namespace {
97
98class LoongArchMCInstrAnalysis : public MCInstrAnalysis {
99 int64_t GPRState[31] = {};
100 std::bitset<31> GPRValidMask;
101
102 static bool isGPR(MCRegister Reg) {
103 return Reg >= LoongArch::R0 && Reg <= LoongArch::R31;
104 }
105
106 static unsigned getRegIndex(MCRegister Reg) {
107 assert(isGPR(Reg) && Reg != LoongArch::R0 && "Invalid GPR reg");
108 return Reg - LoongArch::R1;
109 }
110
111 void setGPRState(MCRegister Reg, std::optional<int64_t> Value) {
112 if (Reg == LoongArch::R0)
113 return;
114
115 auto Index = getRegIndex(Reg);
116
117 if (Value) {
118 GPRState[Index] = *Value;
119 GPRValidMask.set(Index);
120 } else {
121 GPRValidMask.reset(Index);
122 }
123 }
124
125 std::optional<int64_t> getGPRState(MCRegister Reg) const {
126 if (Reg == LoongArch::R0)
127 return 0;
128
129 auto Index = getRegIndex(Reg);
130
131 if (GPRValidMask.test(Index))
132 return GPRState[Index];
133 return std::nullopt;
134 }
135
136public:
137 explicit LoongArchMCInstrAnalysis(const MCInstrInfo *Info)
139
140 void resetState() override { GPRValidMask.reset(); }
141
142 void updateState(const MCInst &Inst, uint64_t Addr) override {
143 // Terminators mark the end of a basic block which means the sequentially
144 // next instruction will be the first of another basic block and the current
145 // state will typically not be valid anymore. For calls, we assume all
146 // registers may be clobbered by the callee (TODO: should we take the
147 // calling convention into account?).
148 if (isTerminator(Inst) || isCall(Inst)) {
149 resetState();
150 return;
151 }
152
153 switch (Inst.getOpcode()) {
154 default: {
155 // Clear the state of all defined registers for instructions that we don't
156 // explicitly support.
157 auto NumDefs = Info->get(Inst.getOpcode()).getNumDefs();
158 for (unsigned I = 0; I < NumDefs; ++I) {
159 auto DefReg = Inst.getOperand(I).getReg();
160 if (isGPR(DefReg))
161 setGPRState(DefReg, std::nullopt);
162 }
163 break;
164 }
165 case LoongArch::PCADDU18I:
166 setGPRState(
167 Inst.getOperand(0).getReg(),
168 Addr + SignExtend64<38>(
169 static_cast<uint64_t>(Inst.getOperand(1).getImm()) << 18));
170 break;
171 }
172 }
173
174 bool evaluateBranch(const MCInst &Inst, uint64_t Addr, uint64_t Size,
175 uint64_t &Target) const override {
176 unsigned NumOps = Inst.getNumOperands();
177 if ((isBranch(Inst) && !isIndirectBranch(Inst)) ||
178 Inst.getOpcode() == LoongArch::BL) {
179 Target = Addr + Inst.getOperand(NumOps - 1).getImm();
180 return true;
181 }
182
183 if (Inst.getOpcode() == LoongArch::JIRL) {
184 if (auto TargetRegState = getGPRState(Inst.getOperand(1).getReg())) {
185 Target = *TargetRegState + Inst.getOperand(2).getImm();
186 return true;
187 }
188 return false;
189 }
190
191 return false;
192 }
193
194 bool isTerminator(const MCInst &Inst) const override {
196 return true;
197
198 switch (Inst.getOpcode()) {
199 default:
200 return false;
201 case LoongArch::JIRL:
202 return Inst.getOperand(0).getReg() == LoongArch::R0;
203 }
204 }
205
206 bool isCall(const MCInst &Inst) const override {
207 if (MCInstrAnalysis::isCall(Inst))
208 return true;
209
210 switch (Inst.getOpcode()) {
211 default:
212 return false;
213 case LoongArch::JIRL:
214 return Inst.getOperand(0).getReg() != LoongArch::R0;
215 }
216 }
217
218 bool isReturn(const MCInst &Inst) const override {
220 return true;
221
222 switch (Inst.getOpcode()) {
223 default:
224 return false;
225 case LoongArch::JIRL:
226 return Inst.getOperand(0).getReg() == LoongArch::R0 &&
227 Inst.getOperand(1).getReg() == LoongArch::R1;
228 }
229 }
230
231 bool isBranch(const MCInst &Inst) const override {
233 return true;
234
235 switch (Inst.getOpcode()) {
236 default:
237 return false;
238 case LoongArch::JIRL:
239 return Inst.getOperand(0).getReg() == LoongArch::R0 &&
240 Inst.getOperand(1).getReg() != LoongArch::R1;
241 }
242 }
243
244 bool isUnconditionalBranch(const MCInst &Inst) const override {
246 return true;
247
248 switch (Inst.getOpcode()) {
249 default:
250 return false;
251 case LoongArch::JIRL:
252 return Inst.getOperand(0).getReg() == LoongArch::R0 &&
253 Inst.getOperand(1).getReg() != LoongArch::R1;
254 }
255 }
256
257 bool isIndirectBranch(const MCInst &Inst) const override {
259 return true;
260
261 switch (Inst.getOpcode()) {
262 default:
263 return false;
264 case LoongArch::JIRL:
265 return Inst.getOperand(0).getReg() == LoongArch::R0 &&
266 Inst.getOperand(1).getReg() != LoongArch::R1;
267 }
268 }
269};
270
271} // end namespace
272
274 return new LoongArchMCInstrAnalysis(Info);
275}
276
277namespace {
279 std::unique_ptr<MCAsmBackend> &&MAB,
280 std::unique_ptr<MCObjectWriter> &&MOW,
281 std::unique_ptr<MCCodeEmitter> &&MCE) {
282 return createLoongArchELFStreamer(Context, std::move(MAB), std::move(MOW),
283 std::move(MCE));
284}
285} // end namespace
286
303 }
304}
unsigned const MachineRegisterInfo * MRI
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
Analysis containing CSE Info
Definition: CSEInfo.cpp:27
#define LLVM_ABI
Definition: Compiler.h:213
#define LLVM_EXTERNAL_VISIBILITY
Definition: Compiler.h:132
uint64_t Addr
uint64_t Size
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
static LVOptions Options
Definition: LVOptions.cpp:25
static MCSubtargetInfo * createLoongArchMCSubtargetInfo(const Triple &TT, StringRef CPU, StringRef FS)
static MCTargetStreamer * createLoongArchObjectTargetStreamer(MCStreamer &S, const MCSubtargetInfo &STI)
static MCInstPrinter * createLoongArchMCInstPrinter(const Triple &T, unsigned SyntaxVariant, const MCAsmInfo &MAI, const MCInstrInfo &MII, const MCRegisterInfo &MRI)
static MCRegisterInfo * createLoongArchMCRegisterInfo(const Triple &TT)
static MCTargetStreamer * createLoongArchAsmTargetStreamer(MCStreamer &S, formatted_raw_ostream &OS, MCInstPrinter *InstPrint)
static MCAsmInfo * createLoongArchMCAsmInfo(const MCRegisterInfo &MRI, const Triple &TT, const MCTargetOptions &Options)
static MCInstrAnalysis * createLoongArchInstrAnalysis(const MCInstrInfo *Info)
static MCInstrInfo * createLoongArchMCInstrInfo()
LLVM_ABI LLVM_EXTERNAL_VISIBILITY void LLVMInitializeLoongArchTargetMC()
#define I(x, y, z)
Definition: MD5.cpp:58
raw_pwrite_stream & OS
This class is intended to be used as a base class for asm properties and features specific to the tar...
Definition: MCAsmInfo.h:64
void addInitialFrameState(const MCCFIInstruction &Inst)
Definition: MCAsmInfo.cpp:74
static MCCFIInstruction cfiDefCfa(MCSymbol *L, unsigned Register, int64_t Offset, SMLoc Loc={})
.cfi_def_cfa defines a rule for computing CFA as: take address from Register and add Offset to it.
Definition: MCDwarf.h:585
Context object for machine code objects.
Definition: MCContext.h:83
This is an instance of a target assembly language printer that converts an MCInst to valid target ass...
Definition: MCInstPrinter.h:46
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:188
unsigned getNumOperands() const
Definition: MCInst.h:212
unsigned getOpcode() const
Definition: MCInst.h:202
const MCOperand & getOperand(unsigned i) const
Definition: MCInst.h:210
virtual bool isCall(const MCInst &Inst) const
virtual bool isBranch(const MCInst &Inst) const
virtual bool isUnconditionalBranch(const MCInst &Inst) const
virtual bool evaluateBranch(const MCInst &Inst, uint64_t Addr, uint64_t Size, uint64_t &Target) const
Given a branch instruction try to get the address the branch targets.
virtual bool isTerminator(const MCInst &Inst) const
virtual void resetState()
Clear the internal state. See updateState for more information.
virtual bool isReturn(const MCInst &Inst) const
virtual void updateState(const MCInst &Inst, uint64_t Addr)
Update internal state with Inst at Addr.
virtual bool isIndirectBranch(const MCInst &Inst) const
Interface to description of machine instruction set.
Definition: MCInstrInfo.h:27
int64_t getImm() const
Definition: MCInst.h:84
MCRegister getReg() const
Returns the register number.
Definition: MCInst.h:73
MCRegisterInfo base class - We assume that the target defines a static array of MCRegisterDesc object...
Wrapper class representing physical registers. Should be passed by value.
Definition: MCRegister.h:33
Streaming machine code generation interface.
Definition: MCStreamer.h:220
Generic base class for all target subtargets.
const Triple & getTargetTriple() const
Target specific streamer interface.
Definition: MCStreamer.h:93
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:55
constexpr bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:151
Target - Wrapper for Target specific information.
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:47
bool isOSBinFormatELF() const
Tests whether the OS uses the ELF binary format.
Definition: Triple.h:766
LLVM Value Representation.
Definition: Value.h:75
formatted_raw_ostream - A raw_ostream that wraps another one and keeps track of line and column posit...
Reg
All possible values of the reg field in the ModR/M byte.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
Target & getTheLoongArch64Target()
MCCodeEmitter * createLoongArchMCCodeEmitter(const MCInstrInfo &MCII, MCContext &Ctx)
MCAsmBackend * createLoongArchAsmBackend(const Target &T, const MCSubtargetInfo &STI, const MCRegisterInfo &MRI, const MCTargetOptions &Options)
Target & getTheLoongArch32Target()
MCELFStreamer * createLoongArchELFStreamer(MCContext &C, std::unique_ptr< MCAsmBackend > MAB, std::unique_ptr< MCObjectWriter > MOW, std::unique_ptr< MCCodeEmitter > MCE)
static void RegisterMCRegInfo(Target &T, Target::MCRegInfoCtorFnTy Fn)
RegisterMCRegInfo - Register a MCRegisterInfo implementation for the given target.
static void RegisterMCAsmBackend(Target &T, Target::MCAsmBackendCtorTy Fn)
RegisterMCAsmBackend - Register a MCAsmBackend implementation for the given target.
static void RegisterMCCodeEmitter(Target &T, Target::MCCodeEmitterCtorTy Fn)
RegisterMCCodeEmitter - Register a MCCodeEmitter implementation for the given target.
static void RegisterMCAsmInfo(Target &T, Target::MCAsmInfoCtorFnTy Fn)
RegisterMCAsmInfo - Register a MCAsmInfo implementation for the given target.
static void RegisterMCSubtargetInfo(Target &T, Target::MCSubtargetInfoCtorFnTy Fn)
RegisterMCSubtargetInfo - Register a MCSubtargetInfo implementation for the given target.
static void RegisterObjectTargetStreamer(Target &T, Target::ObjectTargetStreamerCtorTy Fn)
static void RegisterMCInstrAnalysis(Target &T, Target::MCInstrAnalysisCtorFnTy Fn)
RegisterMCInstrAnalysis - Register a MCInstrAnalysis implementation for the given target.
static void RegisterELFStreamer(Target &T, Target::ELFStreamerCtorTy Fn)
static void RegisterMCInstPrinter(Target &T, Target::MCInstPrinterCtorTy Fn)
RegisterMCInstPrinter - Register a MCInstPrinter implementation for the given target.
static void RegisterMCInstrInfo(Target &T, Target::MCInstrInfoCtorFnTy Fn)
RegisterMCInstrInfo - Register a MCInstrInfo implementation for the given target.
static void RegisterAsmTargetStreamer(Target &T, Target::AsmTargetStreamerCtorTy Fn)