LLVM 17.0.0git
RISCVMCCodeEmitter.cpp
Go to the documentation of this file.
1//===-- RISCVMCCodeEmitter.cpp - Convert RISCV code to machine code -------===//
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 implements the RISCVMCCodeEmitter class.
10//
11//===----------------------------------------------------------------------===//
12
17#include "llvm/ADT/Statistic.h"
18#include "llvm/MC/MCAsmInfo.h"
20#include "llvm/MC/MCContext.h"
21#include "llvm/MC/MCExpr.h"
22#include "llvm/MC/MCInst.h"
24#include "llvm/MC/MCInstrInfo.h"
27#include "llvm/MC/MCSymbol.h"
31
32using namespace llvm;
33
34#define DEBUG_TYPE "mccodeemitter"
35
36STATISTIC(MCNumEmitted, "Number of MC instructions emitted");
37STATISTIC(MCNumFixups, "Number of MC fixups created");
38
39namespace {
40class RISCVMCCodeEmitter : public MCCodeEmitter {
41 RISCVMCCodeEmitter(const RISCVMCCodeEmitter &) = delete;
42 void operator=(const RISCVMCCodeEmitter &) = delete;
43 MCContext &Ctx;
44 MCInstrInfo const &MCII;
45
46public:
47 RISCVMCCodeEmitter(MCContext &ctx, MCInstrInfo const &MCII)
48 : Ctx(ctx), MCII(MCII) {}
49
50 ~RISCVMCCodeEmitter() override = default;
51
54 const MCSubtargetInfo &STI) const override;
55
56 void expandFunctionCall(const MCInst &MI, raw_ostream &OS,
58 const MCSubtargetInfo &STI) const;
59
60 void expandAddTPRel(const MCInst &MI, raw_ostream &OS,
62 const MCSubtargetInfo &STI) const;
63
64 void expandLongCondBr(const MCInst &MI, raw_ostream &OS,
66 const MCSubtargetInfo &STI) const;
67
68 /// TableGen'erated function for getting the binary encoding for an
69 /// instruction.
70 uint64_t getBinaryCodeForInstr(const MCInst &MI,
72 const MCSubtargetInfo &STI) const;
73
74 /// Return binary encoding of operand. If the machine operand requires
75 /// relocation, record the relocation and return zero.
76 unsigned getMachineOpValue(const MCInst &MI, const MCOperand &MO,
78 const MCSubtargetInfo &STI) const;
79
80 unsigned getImmOpValueAsr1(const MCInst &MI, unsigned OpNo,
82 const MCSubtargetInfo &STI) const;
83
84 unsigned getImmOpValue(const MCInst &MI, unsigned OpNo,
86 const MCSubtargetInfo &STI) const;
87
88 unsigned getVMaskReg(const MCInst &MI, unsigned OpNo,
90 const MCSubtargetInfo &STI) const;
91};
92} // end anonymous namespace
93
95 MCContext &Ctx) {
96 return new RISCVMCCodeEmitter(Ctx, MCII);
97}
98
99// Expand PseudoCALL(Reg), PseudoTAIL and PseudoJump to AUIPC and JALR with
100// relocation types. We expand those pseudo-instructions while encoding them,
101// meaning AUIPC and JALR won't go through RISCV MC to MC compressed
102// instruction transformation. This is acceptable because AUIPC has no 16-bit
103// form and C_JALR has no immediate operand field. We let linker relaxation
104// deal with it. When linker relaxation is enabled, AUIPC and JALR have a
105// chance to relax to JAL.
106// If the C extension is enabled, JAL has a chance relax to C_JAL.
107void RISCVMCCodeEmitter::expandFunctionCall(const MCInst &MI, raw_ostream &OS,
109 const MCSubtargetInfo &STI) const {
110 MCInst TmpInst;
111 MCOperand Func;
112 MCRegister Ra;
113 if (MI.getOpcode() == RISCV::PseudoTAIL) {
114 Func = MI.getOperand(0);
115 Ra = RISCV::X6;
116 } else if (MI.getOpcode() == RISCV::PseudoCALLReg) {
117 Func = MI.getOperand(1);
118 Ra = MI.getOperand(0).getReg();
119 } else if (MI.getOpcode() == RISCV::PseudoCALL) {
120 Func = MI.getOperand(0);
121 Ra = RISCV::X1;
122 } else if (MI.getOpcode() == RISCV::PseudoJump) {
123 Func = MI.getOperand(1);
124 Ra = MI.getOperand(0).getReg();
125 }
127
128 assert(Func.isExpr() && "Expected expression");
129
130 const MCExpr *CallExpr = Func.getExpr();
131
132 // Emit AUIPC Ra, Func with R_RISCV_CALL relocation type.
133 TmpInst = MCInstBuilder(RISCV::AUIPC).addReg(Ra).addExpr(CallExpr);
134 Binary = getBinaryCodeForInstr(TmpInst, Fixups, STI);
136
137 if (MI.getOpcode() == RISCV::PseudoTAIL ||
138 MI.getOpcode() == RISCV::PseudoJump)
139 // Emit JALR X0, Ra, 0
140 TmpInst = MCInstBuilder(RISCV::JALR).addReg(RISCV::X0).addReg(Ra).addImm(0);
141 else
142 // Emit JALR Ra, Ra, 0
143 TmpInst = MCInstBuilder(RISCV::JALR).addReg(Ra).addReg(Ra).addImm(0);
144 Binary = getBinaryCodeForInstr(TmpInst, Fixups, STI);
146}
147
148// Expand PseudoAddTPRel to a simple ADD with the correct relocation.
149void RISCVMCCodeEmitter::expandAddTPRel(const MCInst &MI, raw_ostream &OS,
151 const MCSubtargetInfo &STI) const {
152 MCOperand DestReg = MI.getOperand(0);
153 MCOperand SrcReg = MI.getOperand(1);
154 MCOperand TPReg = MI.getOperand(2);
155 assert(TPReg.isReg() && TPReg.getReg() == RISCV::X4 &&
156 "Expected thread pointer as second input to TP-relative add");
157
158 MCOperand SrcSymbol = MI.getOperand(3);
159 assert(SrcSymbol.isExpr() &&
160 "Expected expression as third input to TP-relative add");
161
162 const RISCVMCExpr *Expr = dyn_cast<RISCVMCExpr>(SrcSymbol.getExpr());
164 "Expected tprel_add relocation on TP-relative symbol");
165
166 // Emit the correct tprel_add relocation for the symbol.
167 Fixups.push_back(MCFixup::create(
168 0, Expr, MCFixupKind(RISCV::fixup_riscv_tprel_add), MI.getLoc()));
169
170 // Emit fixup_riscv_relax for tprel_add where the relax feature is enabled.
171 if (STI.hasFeature(RISCV::FeatureRelax)) {
173 Fixups.push_back(MCFixup::create(
174 0, Dummy, MCFixupKind(RISCV::fixup_riscv_relax), MI.getLoc()));
175 }
176
177 // Emit a normal ADD instruction with the given operands.
178 MCInst TmpInst = MCInstBuilder(RISCV::ADD)
179 .addOperand(DestReg)
180 .addOperand(SrcReg)
181 .addOperand(TPReg);
182 uint32_t Binary = getBinaryCodeForInstr(TmpInst, Fixups, STI);
184}
185
186static unsigned getInvertedBranchOp(unsigned BrOp) {
187 switch (BrOp) {
188 default:
189 llvm_unreachable("Unexpected branch opcode!");
190 case RISCV::PseudoLongBEQ:
191 return RISCV::BNE;
192 case RISCV::PseudoLongBNE:
193 return RISCV::BEQ;
194 case RISCV::PseudoLongBLT:
195 return RISCV::BGE;
196 case RISCV::PseudoLongBGE:
197 return RISCV::BLT;
198 case RISCV::PseudoLongBLTU:
199 return RISCV::BGEU;
200 case RISCV::PseudoLongBGEU:
201 return RISCV::BLTU;
202 }
203}
204
205// Expand PseudoLongBxx to an inverted conditional branch and an unconditional
206// jump.
207void RISCVMCCodeEmitter::expandLongCondBr(const MCInst &MI, raw_ostream &OS,
209 const MCSubtargetInfo &STI) const {
210 MCRegister SrcReg1 = MI.getOperand(0).getReg();
211 MCRegister SrcReg2 = MI.getOperand(1).getReg();
212 MCOperand SrcSymbol = MI.getOperand(2);
213 unsigned Opcode = MI.getOpcode();
214 bool IsEqTest =
215 Opcode == RISCV::PseudoLongBNE || Opcode == RISCV::PseudoLongBEQ;
216
217 bool UseCompressedBr = false;
218 if (IsEqTest && (STI.hasFeature(RISCV::FeatureStdExtC) ||
219 STI.hasFeature(RISCV::FeatureExtZca))) {
220 if (RISCV::X8 <= SrcReg1.id() && SrcReg1.id() <= RISCV::X15 &&
221 SrcReg2.id() == RISCV::X0) {
222 UseCompressedBr = true;
223 } else if (RISCV::X8 <= SrcReg2.id() && SrcReg2.id() <= RISCV::X15 &&
224 SrcReg1.id() == RISCV::X0) {
225 std::swap(SrcReg1, SrcReg2);
226 UseCompressedBr = true;
227 }
228 }
229
231 if (UseCompressedBr) {
232 unsigned InvOpc =
233 Opcode == RISCV::PseudoLongBNE ? RISCV::C_BEQZ : RISCV::C_BNEZ;
234 MCInst TmpInst = MCInstBuilder(InvOpc).addReg(SrcReg1).addImm(6);
235 uint16_t Binary = getBinaryCodeForInstr(TmpInst, Fixups, STI);
236 support::endian::write<uint16_t>(OS, Binary, support::little);
237 Offset = 2;
238 } else {
239 unsigned InvOpc = getInvertedBranchOp(Opcode);
240 MCInst TmpInst =
241 MCInstBuilder(InvOpc).addReg(SrcReg1).addReg(SrcReg2).addImm(8);
242 uint32_t Binary = getBinaryCodeForInstr(TmpInst, Fixups, STI);
244 Offset = 4;
245 }
246
247 // Emit an unconditional jump to the destination.
248 MCInst TmpInst =
249 MCInstBuilder(RISCV::JAL).addReg(RISCV::X0).addOperand(SrcSymbol);
250 uint32_t Binary = getBinaryCodeForInstr(TmpInst, Fixups, STI);
252
253 Fixups.clear();
254 if (SrcSymbol.isExpr()) {
255 Fixups.push_back(MCFixup::create(Offset, SrcSymbol.getExpr(),
257 MI.getLoc()));
258 }
259}
260
261void RISCVMCCodeEmitter::encodeInstruction(const MCInst &MI, raw_ostream &OS,
263 const MCSubtargetInfo &STI) const {
264 const MCInstrDesc &Desc = MCII.get(MI.getOpcode());
265 // Get byte count of instruction.
266 unsigned Size = Desc.getSize();
267
268 // RISCVInstrInfo::getInstSizeInBytes expects that the total size of the
269 // expanded instructions for each pseudo is correct in the Size field of the
270 // tablegen definition for the pseudo.
271 switch (MI.getOpcode()) {
272 default:
273 break;
274 case RISCV::PseudoCALLReg:
275 case RISCV::PseudoCALL:
276 case RISCV::PseudoTAIL:
277 case RISCV::PseudoJump:
278 expandFunctionCall(MI, OS, Fixups, STI);
279 MCNumEmitted += 2;
280 return;
281 case RISCV::PseudoAddTPRel:
282 expandAddTPRel(MI, OS, Fixups, STI);
283 MCNumEmitted += 1;
284 return;
285 case RISCV::PseudoLongBEQ:
286 case RISCV::PseudoLongBNE:
287 case RISCV::PseudoLongBLT:
288 case RISCV::PseudoLongBGE:
289 case RISCV::PseudoLongBLTU:
290 case RISCV::PseudoLongBGEU:
291 expandLongCondBr(MI, OS, Fixups, STI);
292 MCNumEmitted += 2;
293 return;
294 }
295
296 switch (Size) {
297 default:
298 llvm_unreachable("Unhandled encodeInstruction length!");
299 case 2: {
300 uint16_t Bits = getBinaryCodeForInstr(MI, Fixups, STI);
301 support::endian::write<uint16_t>(OS, Bits, support::little);
302 break;
303 }
304 case 4: {
305 uint32_t Bits = getBinaryCodeForInstr(MI, Fixups, STI);
307 break;
308 }
309 }
310
311 ++MCNumEmitted; // Keep track of the # of mi's emitted.
312}
313
314unsigned
315RISCVMCCodeEmitter::getMachineOpValue(const MCInst &MI, const MCOperand &MO,
317 const MCSubtargetInfo &STI) const {
318
319 if (MO.isReg())
320 return Ctx.getRegisterInfo()->getEncodingValue(MO.getReg());
321
322 if (MO.isImm())
323 return static_cast<unsigned>(MO.getImm());
324
325 llvm_unreachable("Unhandled expression!");
326 return 0;
327}
328
329unsigned
330RISCVMCCodeEmitter::getImmOpValueAsr1(const MCInst &MI, unsigned OpNo,
332 const MCSubtargetInfo &STI) const {
333 const MCOperand &MO = MI.getOperand(OpNo);
334
335 if (MO.isImm()) {
336 unsigned Res = MO.getImm();
337 assert((Res & 1) == 0 && "LSB is non-zero");
338 return Res >> 1;
339 }
340
341 return getImmOpValue(MI, OpNo, Fixups, STI);
342}
343
344unsigned RISCVMCCodeEmitter::getImmOpValue(const MCInst &MI, unsigned OpNo,
346 const MCSubtargetInfo &STI) const {
347 bool EnableRelax = STI.hasFeature(RISCV::FeatureRelax);
348 const MCOperand &MO = MI.getOperand(OpNo);
349
350 MCInstrDesc const &Desc = MCII.get(MI.getOpcode());
351 unsigned MIFrm = RISCVII::getFormat(Desc.TSFlags);
352
353 // If the destination is an immediate, there is nothing to do.
354 if (MO.isImm())
355 return MO.getImm();
356
357 assert(MO.isExpr() &&
358 "getImmOpValue expects only expressions or immediates");
359 const MCExpr *Expr = MO.getExpr();
360 MCExpr::ExprKind Kind = Expr->getKind();
362 bool RelaxCandidate = false;
363 if (Kind == MCExpr::Target) {
364 const RISCVMCExpr *RVExpr = cast<RISCVMCExpr>(Expr);
365
366 switch (RVExpr->getKind()) {
370 llvm_unreachable("Unhandled fixup kind!");
372 // tprel_add is only used to indicate that a relocation should be emitted
373 // for an add instruction used in TP-relative addressing. It should not be
374 // expanded as if representing an actual instruction operand and so to
375 // encounter it here is an error.
377 "VK_RISCV_TPREL_ADD should not represent an instruction operand");
379 if (MIFrm == RISCVII::InstFormatI)
381 else if (MIFrm == RISCVII::InstFormatS)
383 else
384 llvm_unreachable("VK_RISCV_LO used with unexpected instruction format");
385 RelaxCandidate = true;
386 break;
389 RelaxCandidate = true;
390 break;
392 if (MIFrm == RISCVII::InstFormatI)
394 else if (MIFrm == RISCVII::InstFormatS)
396 else
398 "VK_RISCV_PCREL_LO used with unexpected instruction format");
399 RelaxCandidate = true;
400 break;
403 RelaxCandidate = true;
404 break;
407 break;
409 if (MIFrm == RISCVII::InstFormatI)
411 else if (MIFrm == RISCVII::InstFormatS)
413 else
415 "VK_RISCV_TPREL_LO used with unexpected instruction format");
416 RelaxCandidate = true;
417 break;
420 RelaxCandidate = true;
421 break;
424 break;
427 break;
430 RelaxCandidate = true;
431 break;
434 RelaxCandidate = true;
435 break;
436 }
437 } else if (Kind == MCExpr::SymbolRef &&
438 cast<MCSymbolRefExpr>(Expr)->getKind() == MCSymbolRefExpr::VK_None) {
439 if (MIFrm == RISCVII::InstFormatJ) {
441 } else if (MIFrm == RISCVII::InstFormatB) {
443 } else if (MIFrm == RISCVII::InstFormatCJ) {
445 } else if (MIFrm == RISCVII::InstFormatCB) {
447 }
448 }
449
450 assert(FixupKind != RISCV::fixup_riscv_invalid && "Unhandled expression!");
451
452 Fixups.push_back(
453 MCFixup::create(0, Expr, MCFixupKind(FixupKind), MI.getLoc()));
454 ++MCNumFixups;
455
456 // Ensure an R_RISCV_RELAX relocation will be emitted if linker relaxation is
457 // enabled and the current fixup will result in a relocation that may be
458 // relaxed.
459 if (EnableRelax && RelaxCandidate) {
461 Fixups.push_back(
463 MI.getLoc()));
464 ++MCNumFixups;
465 }
466
467 return 0;
468}
469
470unsigned RISCVMCCodeEmitter::getVMaskReg(const MCInst &MI, unsigned OpNo,
472 const MCSubtargetInfo &STI) const {
473 MCOperand MO = MI.getOperand(OpNo);
474 assert(MO.isReg() && "Expected a register.");
475
476 switch (MO.getReg()) {
477 default:
478 llvm_unreachable("Invalid mask register.");
479 case RISCV::V0:
480 return 0;
481 case RISCV::NoRegister:
482 return 1;
483 }
484}
485
486#include "RISCVGenMCCodeEmitter.inc"
uint64_t Size
IRTranslator LLVM IR MI
static unsigned getInvertedBranchOp(unsigned BrOp)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
raw_pwrite_stream & OS
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
Definition: Statistic.h:167
MCCodeEmitter - Generic instruction encoding interface.
Definition: MCCodeEmitter.h:21
virtual void encodeInstruction(const MCInst &Inst, raw_ostream &OS, SmallVectorImpl< MCFixup > &Fixups, const MCSubtargetInfo &STI) const =0
EncodeInstruction - Encode the given Inst to bytes on the output stream OS.
MCCodeEmitter & operator=(const MCCodeEmitter &)=delete
static const MCConstantExpr * create(int64_t Value, MCContext &Ctx, bool PrintInHex=false, unsigned SizeInBytes=0)
Definition: MCExpr.cpp:194
Context object for machine code objects.
Definition: MCContext.h:76
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:35
@ SymbolRef
References to labels and assigned expressions.
Definition: MCExpr.h:40
@ Target
Target specific expression.
Definition: MCExpr.h:42
ExprKind getKind() const
Definition: MCExpr.h:81
static MCFixup create(uint32_t Offset, const MCExpr *Value, MCFixupKind Kind, SMLoc Loc=SMLoc())
Definition: MCFixup.h:87
MCInstBuilder & addOperand(const MCOperand &Op)
Add an operand.
Definition: MCInstBuilder.h:67
MCInstBuilder & addReg(unsigned Reg)
Add a new register operand.
Definition: MCInstBuilder.h:31
MCInstBuilder & addImm(int64_t Val)
Add a new integer immediate operand.
Definition: MCInstBuilder.h:37
MCInstBuilder & addExpr(const MCExpr *Val)
Add a new MCExpr operand.
Definition: MCInstBuilder.h:55
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:184
Describe properties that are true of each instruction in the target description file.
Definition: MCInstrDesc.h:198
unsigned getSize() const
Return the number of bytes in the encoding of this instruction, or zero if the encoding size cannot b...
Definition: MCInstrDesc.h:600
Interface to description of machine instruction set.
Definition: MCInstrInfo.h:26
Instances of this class represent operands of the MCInst class.
Definition: MCInst.h:36
int64_t getImm() const
Definition: MCInst.h:80
bool isImm() const
Definition: MCInst.h:62
unsigned getReg() const
Returns the register number.
Definition: MCInst.h:69
bool isReg() const
Definition: MCInst.h:61
const MCExpr * getExpr() const
Definition: MCInst.h:114
bool isExpr() const
Definition: MCInst.h:65
Wrapper class representing physical registers. Should be passed by value.
Definition: MCRegister.h:24
unsigned id() const
Definition: MCRegister.h:72
Generic base class for all target subtargets.
bool hasFeature(unsigned Feature) const
VariantKind getKind() const
Definition: RISCVMCExpr.h:56
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:577
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
static unsigned getFormat(uint64_t TSFlags)
@ fixup_riscv_tprel_lo12_s
@ fixup_riscv_tls_got_hi20
@ fixup_riscv_pcrel_lo12_i
@ fixup_riscv_pcrel_lo12_s
@ fixup_riscv_tprel_lo12_i
void write(void *memory, value_type value, endianness endian)
Write a value to memory with a particular endianness.
Definition: Endian.h:97
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:406
MCCodeEmitter * createRISCVMCCodeEmitter(const MCInstrInfo &MCII, MCContext &Ctx)
MCFixupKind
Extensible enumeration to represent the type of a fixup.
Definition: MCFixup.h:21
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition: BitVector.h:860