LLVM 19.0.0git
RISCVMCTargetDesc.cpp
Go to the documentation of this file.
1//===-- RISCVMCTargetDesc.cpp - RISC-V 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 RISC-V specific target descriptions.
10///
11//===----------------------------------------------------------------------===//
12
13#include "RISCVMCTargetDesc.h"
14#include "RISCVBaseInfo.h"
15#include "RISCVELFStreamer.h"
16#include "RISCVInstPrinter.h"
17#include "RISCVMCAsmInfo.h"
19#include "RISCVTargetStreamer.h"
21#include "llvm/ADT/STLExtras.h"
23#include "llvm/MC/MCAsmInfo.h"
26#include "llvm/MC/MCInstrInfo.h"
30#include "llvm/MC/MCStreamer.h"
34#include <bitset>
35
36#define GET_INSTRINFO_MC_DESC
37#define ENABLE_INSTR_PREDICATE_VERIFIER
38#include "RISCVGenInstrInfo.inc"
39
40#define GET_REGINFO_MC_DESC
41#include "RISCVGenRegisterInfo.inc"
42
43#define GET_SUBTARGETINFO_MC_DESC
44#include "RISCVGenSubtargetInfo.inc"
45
47
48using namespace RISCV;
49
50#define GET_RISCVVInversePseudosTable_IMPL
51#include "RISCVGenSearchableTables.inc"
52
53} // namespace llvm::RISCVVInversePseudosTable
54
55using namespace llvm;
56
58 MCInstrInfo *X = new MCInstrInfo();
59 InitRISCVMCInstrInfo(X);
60 return X;
61}
62
65 InitRISCVMCRegisterInfo(X, RISCV::X1);
66 return X;
67}
68
70 const Triple &TT,
71 const MCTargetOptions &Options) {
72 MCAsmInfo *MAI = new RISCVMCAsmInfo(TT);
73
74 MCRegister SP = MRI.getDwarfRegNum(RISCV::X2, true);
75 MCCFIInstruction Inst = MCCFIInstruction::cfiDefCfa(nullptr, SP, 0);
76 MAI->addInitialFrameState(Inst);
77
78 return MAI;
79}
80
81static MCObjectFileInfo *
83 bool LargeCodeModel = false) {
85 MOFI->initMCObjectFileInfo(Ctx, PIC, LargeCodeModel);
86 return MOFI;
87}
88
90 StringRef CPU, StringRef FS) {
91 if (CPU.empty() || CPU == "generic")
92 CPU = TT.isArch64Bit() ? "generic-rv64" : "generic-rv32";
93
94 return createRISCVMCSubtargetInfoImpl(TT, CPU, /*TuneCPU*/ CPU, FS);
95}
96
98 unsigned SyntaxVariant,
99 const MCAsmInfo &MAI,
100 const MCInstrInfo &MII,
101 const MCRegisterInfo &MRI) {
102 return new RISCVInstPrinter(MAI, MII, MRI);
103}
104
105static MCTargetStreamer *
107 const Triple &TT = STI.getTargetTriple();
108 if (TT.isOSBinFormatELF())
109 return new RISCVTargetELFStreamer(S, STI);
110 return nullptr;
111}
112
115 MCInstPrinter *InstPrint,
116 bool isVerboseAsm) {
117 return new RISCVTargetAsmStreamer(S, OS);
118}
119
121 return new RISCVTargetStreamer(S);
122}
123
124namespace {
125
126class RISCVMCInstrAnalysis : public MCInstrAnalysis {
127 int64_t GPRState[31] = {};
128 std::bitset<31> GPRValidMask;
129
130 static bool isGPR(unsigned Reg) {
131 return Reg >= RISCV::X0 && Reg <= RISCV::X31;
132 }
133
134 static unsigned getRegIndex(unsigned Reg) {
135 assert(isGPR(Reg) && Reg != RISCV::X0 && "Invalid GPR reg");
136 return Reg - RISCV::X1;
137 }
138
139 void setGPRState(unsigned Reg, std::optional<int64_t> Value) {
140 if (Reg == RISCV::X0)
141 return;
142
143 auto Index = getRegIndex(Reg);
144
145 if (Value) {
146 GPRState[Index] = *Value;
147 GPRValidMask.set(Index);
148 } else {
149 GPRValidMask.reset(Index);
150 }
151 }
152
153 std::optional<int64_t> getGPRState(unsigned Reg) const {
154 if (Reg == RISCV::X0)
155 return 0;
156
157 auto Index = getRegIndex(Reg);
158
159 if (GPRValidMask.test(Index))
160 return GPRState[Index];
161 return std::nullopt;
162 }
163
164public:
165 explicit RISCVMCInstrAnalysis(const MCInstrInfo *Info)
167
168 void resetState() override { GPRValidMask.reset(); }
169
170 void updateState(const MCInst &Inst, uint64_t Addr) override {
171 // Terminators mark the end of a basic block which means the sequentially
172 // next instruction will be the first of another basic block and the current
173 // state will typically not be valid anymore. For calls, we assume all
174 // registers may be clobbered by the callee (TODO: should we take the
175 // calling convention into account?).
176 if (isTerminator(Inst) || isCall(Inst)) {
177 resetState();
178 return;
179 }
180
181 switch (Inst.getOpcode()) {
182 default: {
183 // Clear the state of all defined registers for instructions that we don't
184 // explicitly support.
185 auto NumDefs = Info->get(Inst.getOpcode()).getNumDefs();
186 for (unsigned I = 0; I < NumDefs; ++I) {
187 auto DefReg = Inst.getOperand(I).getReg();
188 if (isGPR(DefReg))
189 setGPRState(DefReg, std::nullopt);
190 }
191 break;
192 }
193 case RISCV::AUIPC:
194 setGPRState(Inst.getOperand(0).getReg(),
195 Addr + (Inst.getOperand(1).getImm() << 12));
196 break;
197 }
198 }
199
200 bool evaluateBranch(const MCInst &Inst, uint64_t Addr, uint64_t Size,
201 uint64_t &Target) const override {
202 if (isConditionalBranch(Inst)) {
203 int64_t Imm;
204 if (Size == 2)
205 Imm = Inst.getOperand(1).getImm();
206 else
207 Imm = Inst.getOperand(2).getImm();
208 Target = Addr + Imm;
209 return true;
210 }
211
212 if (Inst.getOpcode() == RISCV::C_JAL || Inst.getOpcode() == RISCV::C_J) {
213 Target = Addr + Inst.getOperand(0).getImm();
214 return true;
215 }
216
217 if (Inst.getOpcode() == RISCV::JAL) {
218 Target = Addr + Inst.getOperand(1).getImm();
219 return true;
220 }
221
222 if (Inst.getOpcode() == RISCV::JALR) {
223 if (auto TargetRegState = getGPRState(Inst.getOperand(1).getReg())) {
224 Target = *TargetRegState + Inst.getOperand(2).getImm();
225 return true;
226 }
227
228 return false;
229 }
230
231 return false;
232 }
233
234 bool isTerminator(const MCInst &Inst) const override {
236 return true;
237
238 switch (Inst.getOpcode()) {
239 default:
240 return false;
241 case RISCV::JAL:
242 case RISCV::JALR:
243 return Inst.getOperand(0).getReg() == RISCV::X0;
244 }
245 }
246
247 bool isCall(const MCInst &Inst) const override {
248 if (MCInstrAnalysis::isCall(Inst))
249 return true;
250
251 switch (Inst.getOpcode()) {
252 default:
253 return false;
254 case RISCV::JAL:
255 case RISCV::JALR:
256 return Inst.getOperand(0).getReg() != RISCV::X0;
257 }
258 }
259
260 bool isReturn(const MCInst &Inst) const override {
262 return true;
263
264 switch (Inst.getOpcode()) {
265 default:
266 return false;
267 case RISCV::JALR:
268 return Inst.getOperand(0).getReg() == RISCV::X0 &&
269 maybeReturnAddress(Inst.getOperand(1).getReg());
270 case RISCV::C_JR:
271 return maybeReturnAddress(Inst.getOperand(0).getReg());
272 }
273 }
274
275 bool isBranch(const MCInst &Inst) const override {
277 return true;
278
279 return isBranchImpl(Inst);
280 }
281
282 bool isUnconditionalBranch(const MCInst &Inst) const override {
284 return true;
285
286 return isBranchImpl(Inst);
287 }
288
289 bool isIndirectBranch(const MCInst &Inst) const override {
291 return true;
292
293 switch (Inst.getOpcode()) {
294 default:
295 return false;
296 case RISCV::JALR:
297 return Inst.getOperand(0).getReg() == RISCV::X0 &&
298 !maybeReturnAddress(Inst.getOperand(1).getReg());
299 case RISCV::C_JR:
300 return !maybeReturnAddress(Inst.getOperand(0).getReg());
301 }
302 }
303
304private:
305 static bool maybeReturnAddress(unsigned Reg) {
306 // X1 is used for normal returns, X5 for returns from outlined functions.
307 return Reg == RISCV::X1 || Reg == RISCV::X5;
308 }
309
310 static bool isBranchImpl(const MCInst &Inst) {
311 switch (Inst.getOpcode()) {
312 default:
313 return false;
314 case RISCV::JAL:
315 return Inst.getOperand(0).getReg() == RISCV::X0;
316 case RISCV::JALR:
317 return Inst.getOperand(0).getReg() == RISCV::X0 &&
318 !maybeReturnAddress(Inst.getOperand(1).getReg());
319 case RISCV::C_JR:
320 return !maybeReturnAddress(Inst.getOperand(0).getReg());
321 }
322 }
323};
324
325} // end anonymous namespace
326
328 return new RISCVMCInstrAnalysis(Info);
329}
330
331namespace {
333 std::unique_ptr<MCAsmBackend> &&MAB,
334 std::unique_ptr<MCObjectWriter> &&MOW,
335 std::unique_ptr<MCCodeEmitter> &&MCE) {
336 return createRISCVELFStreamer(Context, std::move(MAB), std::move(MOW),
337 std::move(MCE));
338}
339} // end anonymous namespace
340
355
356 // Register the asm target streamer.
358 // Register the null target streamer.
361 }
362}
unsigned const MachineRegisterInfo * MRI
Analysis containing CSE Info
Definition: CSEInfo.cpp:27
#define LLVM_EXTERNAL_VISIBILITY
Definition: Compiler.h:135
uint64_t Addr
uint64_t Size
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
static LVOptions Options
Definition: LVOptions.cpp:25
#define I(x, y, z)
Definition: MD5.cpp:58
PassInstrumentationCallbacks PIC
static MCRegisterInfo * createRISCVMCRegisterInfo(const Triple &TT)
LLVM_EXTERNAL_VISIBILITY void LLVMInitializeRISCVTargetMC()
static MCInstPrinter * createRISCVMCInstPrinter(const Triple &T, unsigned SyntaxVariant, const MCAsmInfo &MAI, const MCInstrInfo &MII, const MCRegisterInfo &MRI)
static MCTargetStreamer * createRISCVAsmTargetStreamer(MCStreamer &S, formatted_raw_ostream &OS, MCInstPrinter *InstPrint, bool isVerboseAsm)
static MCObjectFileInfo * createRISCVMCObjectFileInfo(MCContext &Ctx, bool PIC, bool LargeCodeModel=false)
static MCTargetStreamer * createRISCVNullTargetStreamer(MCStreamer &S)
static MCSubtargetInfo * createRISCVMCSubtargetInfo(const Triple &TT, StringRef CPU, StringRef FS)
static MCTargetStreamer * createRISCVObjectTargetStreamer(MCStreamer &S, const MCSubtargetInfo &STI)
static MCInstrAnalysis * createRISCVInstrAnalysis(const MCInstrInfo *Info)
static MCInstrInfo * createRISCVMCInstrInfo()
static MCAsmInfo * createRISCVMCAsmInfo(const MCRegisterInfo &MRI, const Triple &TT, const MCTargetOptions &Options)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file contains some templates that are useful if you are working with the STL at all.
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:56
void addInitialFrameState(const MCCFIInstruction &Inst)
Definition: MCAsmInfo.cpp:75
static MCCFIInstruction cfiDefCfa(MCSymbol *L, unsigned Register, int 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:541
Context object for machine code objects.
Definition: MCContext.h:81
This is an instance of a target assembly language printer that converts an MCInst to valid target ass...
Definition: MCInstPrinter.h:45
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:184
unsigned getOpcode() const
Definition: MCInst.h:198
const MCOperand & getOperand(unsigned i) const
Definition: MCInst.h:206
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 isConditionalBranch(const MCInst &Inst) const
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:26
void initMCObjectFileInfo(MCContext &MCCtx, bool PIC, bool LargeCodeModel=false)
int64_t getImm() const
Definition: MCInst.h:80
unsigned getReg() const
Returns the register number.
Definition: MCInst.h:69
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:212
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:50
constexpr bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:134
Target - Wrapper for Target specific information.
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
LLVM Value Representation.
Definition: Value.h:74
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
MCCodeEmitter * createRISCVMCCodeEmitter(const MCInstrInfo &MCII, MCContext &Ctx)
Target & getTheRISCV32Target()
MCAsmBackend * createRISCVAsmBackend(const Target &T, const MCSubtargetInfo &STI, const MCRegisterInfo &MRI, const MCTargetOptions &Options)
Target & getTheRISCV64Target()
MCELFStreamer * createRISCVELFStreamer(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 RegisterNullTargetStreamer(Target &T, Target::NullTargetStreamerCtorTy 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 RegisterMCObjectFileInfo(Target &T, Target::MCObjectFileInfoCtorFnTy Fn)
Register a MCObjectFileInfo implementation for the given target.
static void RegisterAsmTargetStreamer(Target &T, Target::AsmTargetStreamerCtorTy Fn)