LLVM 20.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 "RISCVELFStreamer.h"
15#include "RISCVInstPrinter.h"
16#include "RISCVMCAsmInfo.h"
18#include "RISCVTargetStreamer.h"
21#include "llvm/MC/MCAsmInfo.h"
24#include "llvm/MC/MCInstrInfo.h"
28#include "llvm/MC/MCStreamer.h"
32#include <bitset>
33
34#define GET_INSTRINFO_MC_DESC
35#define ENABLE_INSTR_PREDICATE_VERIFIER
36#include "RISCVGenInstrInfo.inc"
37
38#define GET_REGINFO_MC_DESC
39#include "RISCVGenRegisterInfo.inc"
40
41#define GET_SUBTARGETINFO_MC_DESC
42#include "RISCVGenSubtargetInfo.inc"
43
45
46using namespace RISCV;
47
48#define GET_RISCVVInversePseudosTable_IMPL
49#include "RISCVGenSearchableTables.inc"
50
51} // namespace llvm::RISCVVInversePseudosTable
52
53using namespace llvm;
54
56 MCInstrInfo *X = new MCInstrInfo();
57 InitRISCVMCInstrInfo(X);
58 return X;
59}
60
63 InitRISCVMCRegisterInfo(X, RISCV::X1);
64 return X;
65}
66
68 const Triple &TT,
69 const MCTargetOptions &Options) {
70 MCAsmInfo *MAI = new RISCVMCAsmInfo(TT);
71
72 unsigned SP = MRI.getDwarfRegNum(RISCV::X2, true);
73 MCCFIInstruction Inst = MCCFIInstruction::cfiDefCfa(nullptr, SP, 0);
74 MAI->addInitialFrameState(Inst);
75
76 return MAI;
77}
78
79static MCObjectFileInfo *
81 bool LargeCodeModel = false) {
83 MOFI->initMCObjectFileInfo(Ctx, PIC, LargeCodeModel);
84 return MOFI;
85}
86
88 StringRef CPU, StringRef FS) {
89 if (CPU.empty() || CPU == "generic")
90 CPU = TT.isArch64Bit() ? "generic-rv64" : "generic-rv32";
91
92 return createRISCVMCSubtargetInfoImpl(TT, CPU, /*TuneCPU*/ CPU, FS);
93}
94
96 unsigned SyntaxVariant,
97 const MCAsmInfo &MAI,
98 const MCInstrInfo &MII,
99 const MCRegisterInfo &MRI) {
100 return new RISCVInstPrinter(MAI, MII, MRI);
101}
102
103static MCTargetStreamer *
105 const Triple &TT = STI.getTargetTriple();
106 if (TT.isOSBinFormatELF())
107 return new RISCVTargetELFStreamer(S, STI);
108 return nullptr;
109}
110
111static MCTargetStreamer *
113 MCInstPrinter *InstPrint) {
114 return new RISCVTargetAsmStreamer(S, OS);
115}
116
118 return new RISCVTargetStreamer(S);
119}
120
121namespace {
122
123class RISCVMCInstrAnalysis : public MCInstrAnalysis {
124 int64_t GPRState[31] = {};
125 std::bitset<31> GPRValidMask;
126
127 static bool isGPR(MCRegister Reg) {
128 return Reg >= RISCV::X0 && Reg <= RISCV::X31;
129 }
130
131 static unsigned getRegIndex(MCRegister Reg) {
132 assert(isGPR(Reg) && Reg != RISCV::X0 && "Invalid GPR reg");
133 return Reg - RISCV::X1;
134 }
135
136 void setGPRState(MCRegister Reg, std::optional<int64_t> Value) {
137 if (Reg == RISCV::X0)
138 return;
139
140 auto Index = getRegIndex(Reg);
141
142 if (Value) {
143 GPRState[Index] = *Value;
144 GPRValidMask.set(Index);
145 } else {
146 GPRValidMask.reset(Index);
147 }
148 }
149
150 std::optional<int64_t> getGPRState(MCRegister Reg) const {
151 if (Reg == RISCV::X0)
152 return 0;
153
154 auto Index = getRegIndex(Reg);
155
156 if (GPRValidMask.test(Index))
157 return GPRState[Index];
158 return std::nullopt;
159 }
160
161public:
162 explicit RISCVMCInstrAnalysis(const MCInstrInfo *Info)
164
165 void resetState() override { GPRValidMask.reset(); }
166
167 void updateState(const MCInst &Inst, uint64_t Addr) override {
168 // Terminators mark the end of a basic block which means the sequentially
169 // next instruction will be the first of another basic block and the current
170 // state will typically not be valid anymore. For calls, we assume all
171 // registers may be clobbered by the callee (TODO: should we take the
172 // calling convention into account?).
173 if (isTerminator(Inst) || isCall(Inst)) {
174 resetState();
175 return;
176 }
177
178 switch (Inst.getOpcode()) {
179 default: {
180 // Clear the state of all defined registers for instructions that we don't
181 // explicitly support.
182 auto NumDefs = Info->get(Inst.getOpcode()).getNumDefs();
183 for (unsigned I = 0; I < NumDefs; ++I) {
184 auto DefReg = Inst.getOperand(I).getReg();
185 if (isGPR(DefReg))
186 setGPRState(DefReg, std::nullopt);
187 }
188 break;
189 }
190 case RISCV::AUIPC:
191 setGPRState(Inst.getOperand(0).getReg(),
192 Addr + (Inst.getOperand(1).getImm() << 12));
193 break;
194 }
195 }
196
197 bool evaluateBranch(const MCInst &Inst, uint64_t Addr, uint64_t Size,
198 uint64_t &Target) const override {
199 if (isConditionalBranch(Inst)) {
200 int64_t Imm;
201 if (Size == 2)
202 Imm = Inst.getOperand(1).getImm();
203 else
204 Imm = Inst.getOperand(2).getImm();
205 Target = Addr + Imm;
206 return true;
207 }
208
209 if (Inst.getOpcode() == RISCV::C_JAL || Inst.getOpcode() == RISCV::C_J) {
210 Target = Addr + Inst.getOperand(0).getImm();
211 return true;
212 }
213
214 if (Inst.getOpcode() == RISCV::JAL) {
215 Target = Addr + Inst.getOperand(1).getImm();
216 return true;
217 }
218
219 if (Inst.getOpcode() == RISCV::JALR) {
220 if (auto TargetRegState = getGPRState(Inst.getOperand(1).getReg())) {
221 Target = *TargetRegState + Inst.getOperand(2).getImm();
222 return true;
223 }
224
225 return false;
226 }
227
228 return false;
229 }
230
231 bool isTerminator(const MCInst &Inst) const override {
233 return true;
234
235 switch (Inst.getOpcode()) {
236 default:
237 return false;
238 case RISCV::JAL:
239 case RISCV::JALR:
240 return Inst.getOperand(0).getReg() == RISCV::X0;
241 }
242 }
243
244 bool isCall(const MCInst &Inst) const override {
245 if (MCInstrAnalysis::isCall(Inst))
246 return true;
247
248 switch (Inst.getOpcode()) {
249 default:
250 return false;
251 case RISCV::JAL:
252 case RISCV::JALR:
253 return Inst.getOperand(0).getReg() != RISCV::X0;
254 }
255 }
256
257 bool isReturn(const MCInst &Inst) const override {
259 return true;
260
261 switch (Inst.getOpcode()) {
262 default:
263 return false;
264 case RISCV::JALR:
265 return Inst.getOperand(0).getReg() == RISCV::X0 &&
266 maybeReturnAddress(Inst.getOperand(1).getReg());
267 case RISCV::C_JR:
268 return maybeReturnAddress(Inst.getOperand(0).getReg());
269 }
270 }
271
272 bool isBranch(const MCInst &Inst) const override {
274 return true;
275
276 return isBranchImpl(Inst);
277 }
278
279 bool isUnconditionalBranch(const MCInst &Inst) const override {
281 return true;
282
283 return isBranchImpl(Inst);
284 }
285
286 bool isIndirectBranch(const MCInst &Inst) const override {
288 return true;
289
290 switch (Inst.getOpcode()) {
291 default:
292 return false;
293 case RISCV::JALR:
294 return Inst.getOperand(0).getReg() == RISCV::X0 &&
295 !maybeReturnAddress(Inst.getOperand(1).getReg());
296 case RISCV::C_JR:
297 return !maybeReturnAddress(Inst.getOperand(0).getReg());
298 }
299 }
300
301private:
302 static bool maybeReturnAddress(MCRegister Reg) {
303 // X1 is used for normal returns, X5 for returns from outlined functions.
304 return Reg == RISCV::X1 || Reg == RISCV::X5;
305 }
306
307 static bool isBranchImpl(const MCInst &Inst) {
308 switch (Inst.getOpcode()) {
309 default:
310 return false;
311 case RISCV::JAL:
312 return Inst.getOperand(0).getReg() == RISCV::X0;
313 case RISCV::JALR:
314 return Inst.getOperand(0).getReg() == RISCV::X0 &&
315 !maybeReturnAddress(Inst.getOperand(1).getReg());
316 case RISCV::C_JR:
317 return !maybeReturnAddress(Inst.getOperand(0).getReg());
318 }
319 }
320};
321
322} // end anonymous namespace
323
325 return new RISCVMCInstrAnalysis(Info);
326}
327
328namespace {
330 std::unique_ptr<MCAsmBackend> &&MAB,
331 std::unique_ptr<MCObjectWriter> &&MOW,
332 std::unique_ptr<MCCodeEmitter> &&MCE) {
333 return createRISCVELFStreamer(Context, std::move(MAB), std::move(MOW),
334 std::move(MCE));
335}
336} // end anonymous namespace
337
352
353 // Register the asm target streamer.
355 // Register the null target streamer.
358 }
359}
unsigned const MachineRegisterInfo * MRI
Analysis containing CSE Info
Definition: CSEInfo.cpp:27
#define LLVM_EXTERNAL_VISIBILITY
Definition: Compiler.h:128
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 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 MCTargetStreamer * createRISCVAsmTargetStreamer(MCStreamer &S, formatted_raw_ostream &OS, MCInstPrinter *InstPrint)
static MCInstrInfo * createRISCVMCInstrInfo()
static MCAsmInfo * createRISCVMCAsmInfo(const MCRegisterInfo &MRI, const Triple &TT, const MCTargetOptions &Options)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
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, 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:575
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:185
unsigned getOpcode() const
Definition: MCInst.h:199
const MCOperand & getOperand(unsigned i) const
Definition: MCInst.h:207
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:81
MCRegister getReg() const
Returns the register number.
Definition: MCInst.h:70
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:213
Generic base class for all target subtargets.
const Triple & getTargetTriple() const
Target specific streamer interface.
Definition: MCStreamer.h:94
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
constexpr bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:147
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)