LLVM 18.0.0git
RISCVMCCodeEmitter.cpp
Go to the documentation of this file.
1//===-- RISCVMCCodeEmitter.cpp - Convert RISC-V 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, SmallVectorImpl<char> &CB,
58 const MCSubtargetInfo &STI) const;
59
60 void expandAddTPRel(const MCInst &MI, SmallVectorImpl<char> &CB,
62 const MCSubtargetInfo &STI) const;
63
64 void expandLongCondBr(const MCInst &MI, SmallVectorImpl<char> &CB,
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 unsigned getRlistOpValue(const MCInst &MI, unsigned OpNo,
94 const MCSubtargetInfo &STI) const;
95
96 unsigned getRegReg(const MCInst &MI, unsigned OpNo,
98 const MCSubtargetInfo &STI) const;
99};
100} // end anonymous namespace
101
103 MCContext &Ctx) {
104 return new RISCVMCCodeEmitter(Ctx, MCII);
105}
106
107// Expand PseudoCALL(Reg), PseudoTAIL and PseudoJump to AUIPC and JALR with
108// relocation types. We expand those pseudo-instructions while encoding them,
109// meaning AUIPC and JALR won't go through RISC-V MC to MC compressed
110// instruction transformation. This is acceptable because AUIPC has no 16-bit
111// form and C_JALR has no immediate operand field. We let linker relaxation
112// deal with it. When linker relaxation is enabled, AUIPC and JALR have a
113// chance to relax to JAL.
114// If the C extension is enabled, JAL has a chance relax to C_JAL.
115void RISCVMCCodeEmitter::expandFunctionCall(const MCInst &MI,
118 const MCSubtargetInfo &STI) const {
119 MCInst TmpInst;
120 MCOperand Func;
121 MCRegister Ra;
122 if (MI.getOpcode() == RISCV::PseudoTAIL) {
123 Func = MI.getOperand(0);
124 Ra = RISCV::X6;
125 } else if (MI.getOpcode() == RISCV::PseudoCALLReg) {
126 Func = MI.getOperand(1);
127 Ra = MI.getOperand(0).getReg();
128 } else if (MI.getOpcode() == RISCV::PseudoCALL) {
129 Func = MI.getOperand(0);
130 Ra = RISCV::X1;
131 } else if (MI.getOpcode() == RISCV::PseudoJump) {
132 Func = MI.getOperand(1);
133 Ra = MI.getOperand(0).getReg();
134 }
136
137 assert(Func.isExpr() && "Expected expression");
138
139 const MCExpr *CallExpr = Func.getExpr();
140
141 // Emit AUIPC Ra, Func with R_RISCV_CALL relocation type.
142 TmpInst = MCInstBuilder(RISCV::AUIPC).addReg(Ra).addExpr(CallExpr);
143 Binary = getBinaryCodeForInstr(TmpInst, Fixups, STI);
145
146 if (MI.getOpcode() == RISCV::PseudoTAIL ||
147 MI.getOpcode() == RISCV::PseudoJump)
148 // Emit JALR X0, Ra, 0
149 TmpInst = MCInstBuilder(RISCV::JALR).addReg(RISCV::X0).addReg(Ra).addImm(0);
150 else
151 // Emit JALR Ra, Ra, 0
152 TmpInst = MCInstBuilder(RISCV::JALR).addReg(Ra).addReg(Ra).addImm(0);
153 Binary = getBinaryCodeForInstr(TmpInst, Fixups, STI);
155}
156
157// Expand PseudoAddTPRel to a simple ADD with the correct relocation.
158void RISCVMCCodeEmitter::expandAddTPRel(const MCInst &MI,
161 const MCSubtargetInfo &STI) const {
162 MCOperand DestReg = MI.getOperand(0);
163 MCOperand SrcReg = MI.getOperand(1);
164 MCOperand TPReg = MI.getOperand(2);
165 assert(TPReg.isReg() && TPReg.getReg() == RISCV::X4 &&
166 "Expected thread pointer as second input to TP-relative add");
167
168 MCOperand SrcSymbol = MI.getOperand(3);
169 assert(SrcSymbol.isExpr() &&
170 "Expected expression as third input to TP-relative add");
171
172 const RISCVMCExpr *Expr = dyn_cast<RISCVMCExpr>(SrcSymbol.getExpr());
174 "Expected tprel_add relocation on TP-relative symbol");
175
176 // Emit the correct tprel_add relocation for the symbol.
177 Fixups.push_back(MCFixup::create(
178 0, Expr, MCFixupKind(RISCV::fixup_riscv_tprel_add), MI.getLoc()));
179
180 // Emit fixup_riscv_relax for tprel_add where the relax feature is enabled.
181 if (STI.hasFeature(RISCV::FeatureRelax)) {
183 Fixups.push_back(MCFixup::create(
184 0, Dummy, MCFixupKind(RISCV::fixup_riscv_relax), MI.getLoc()));
185 }
186
187 // Emit a normal ADD instruction with the given operands.
188 MCInst TmpInst = MCInstBuilder(RISCV::ADD)
189 .addOperand(DestReg)
190 .addOperand(SrcReg)
191 .addOperand(TPReg);
192 uint32_t Binary = getBinaryCodeForInstr(TmpInst, Fixups, STI);
194}
195
196static unsigned getInvertedBranchOp(unsigned BrOp) {
197 switch (BrOp) {
198 default:
199 llvm_unreachable("Unexpected branch opcode!");
200 case RISCV::PseudoLongBEQ:
201 return RISCV::BNE;
202 case RISCV::PseudoLongBNE:
203 return RISCV::BEQ;
204 case RISCV::PseudoLongBLT:
205 return RISCV::BGE;
206 case RISCV::PseudoLongBGE:
207 return RISCV::BLT;
208 case RISCV::PseudoLongBLTU:
209 return RISCV::BGEU;
210 case RISCV::PseudoLongBGEU:
211 return RISCV::BLTU;
212 }
213}
214
215// Expand PseudoLongBxx to an inverted conditional branch and an unconditional
216// jump.
217void RISCVMCCodeEmitter::expandLongCondBr(const MCInst &MI,
220 const MCSubtargetInfo &STI) const {
221 MCRegister SrcReg1 = MI.getOperand(0).getReg();
222 MCRegister SrcReg2 = MI.getOperand(1).getReg();
223 MCOperand SrcSymbol = MI.getOperand(2);
224 unsigned Opcode = MI.getOpcode();
225 bool IsEqTest =
226 Opcode == RISCV::PseudoLongBNE || Opcode == RISCV::PseudoLongBEQ;
227
228 bool UseCompressedBr = false;
229 if (IsEqTest && (STI.hasFeature(RISCV::FeatureStdExtC) ||
230 STI.hasFeature(RISCV::FeatureStdExtZca))) {
231 if (RISCV::X8 <= SrcReg1.id() && SrcReg1.id() <= RISCV::X15 &&
232 SrcReg2.id() == RISCV::X0) {
233 UseCompressedBr = true;
234 } else if (RISCV::X8 <= SrcReg2.id() && SrcReg2.id() <= RISCV::X15 &&
235 SrcReg1.id() == RISCV::X0) {
236 std::swap(SrcReg1, SrcReg2);
237 UseCompressedBr = true;
238 }
239 }
240
242 if (UseCompressedBr) {
243 unsigned InvOpc =
244 Opcode == RISCV::PseudoLongBNE ? RISCV::C_BEQZ : RISCV::C_BNEZ;
245 MCInst TmpInst = MCInstBuilder(InvOpc).addReg(SrcReg1).addImm(6);
246 uint16_t Binary = getBinaryCodeForInstr(TmpInst, Fixups, STI);
247 support::endian::write<uint16_t>(CB, Binary, llvm::endianness::little);
248 Offset = 2;
249 } else {
250 unsigned InvOpc = getInvertedBranchOp(Opcode);
251 MCInst TmpInst =
252 MCInstBuilder(InvOpc).addReg(SrcReg1).addReg(SrcReg2).addImm(8);
253 uint32_t Binary = getBinaryCodeForInstr(TmpInst, Fixups, STI);
255 Offset = 4;
256 }
257
258 // Emit an unconditional jump to the destination.
259 MCInst TmpInst =
260 MCInstBuilder(RISCV::JAL).addReg(RISCV::X0).addOperand(SrcSymbol);
261 uint32_t Binary = getBinaryCodeForInstr(TmpInst, Fixups, STI);
263
264 Fixups.clear();
265 if (SrcSymbol.isExpr()) {
266 Fixups.push_back(MCFixup::create(Offset, SrcSymbol.getExpr(),
268 MI.getLoc()));
269 }
270}
271
272void RISCVMCCodeEmitter::encodeInstruction(const MCInst &MI,
275 const MCSubtargetInfo &STI) const {
276 const MCInstrDesc &Desc = MCII.get(MI.getOpcode());
277 // Get byte count of instruction.
278 unsigned Size = Desc.getSize();
279
280 // RISCVInstrInfo::getInstSizeInBytes expects that the total size of the
281 // expanded instructions for each pseudo is correct in the Size field of the
282 // tablegen definition for the pseudo.
283 switch (MI.getOpcode()) {
284 default:
285 break;
286 case RISCV::PseudoCALLReg:
287 case RISCV::PseudoCALL:
288 case RISCV::PseudoTAIL:
289 case RISCV::PseudoJump:
290 expandFunctionCall(MI, CB, Fixups, STI);
291 MCNumEmitted += 2;
292 return;
293 case RISCV::PseudoAddTPRel:
294 expandAddTPRel(MI, CB, Fixups, STI);
295 MCNumEmitted += 1;
296 return;
297 case RISCV::PseudoLongBEQ:
298 case RISCV::PseudoLongBNE:
299 case RISCV::PseudoLongBLT:
300 case RISCV::PseudoLongBGE:
301 case RISCV::PseudoLongBLTU:
302 case RISCV::PseudoLongBGEU:
303 expandLongCondBr(MI, CB, Fixups, STI);
304 MCNumEmitted += 2;
305 return;
306 }
307
308 switch (Size) {
309 default:
310 llvm_unreachable("Unhandled encodeInstruction length!");
311 case 2: {
312 uint16_t Bits = getBinaryCodeForInstr(MI, Fixups, STI);
313 support::endian::write<uint16_t>(CB, Bits, llvm::endianness::little);
314 break;
315 }
316 case 4: {
317 uint32_t Bits = getBinaryCodeForInstr(MI, Fixups, STI);
319 break;
320 }
321 }
322
323 ++MCNumEmitted; // Keep track of the # of mi's emitted.
324}
325
326unsigned
327RISCVMCCodeEmitter::getMachineOpValue(const MCInst &MI, const MCOperand &MO,
329 const MCSubtargetInfo &STI) const {
330
331 if (MO.isReg())
332 return Ctx.getRegisterInfo()->getEncodingValue(MO.getReg());
333
334 if (MO.isImm())
335 return static_cast<unsigned>(MO.getImm());
336
337 llvm_unreachable("Unhandled expression!");
338 return 0;
339}
340
341unsigned
342RISCVMCCodeEmitter::getImmOpValueAsr1(const MCInst &MI, unsigned OpNo,
344 const MCSubtargetInfo &STI) const {
345 const MCOperand &MO = MI.getOperand(OpNo);
346
347 if (MO.isImm()) {
348 unsigned Res = MO.getImm();
349 assert((Res & 1) == 0 && "LSB is non-zero");
350 return Res >> 1;
351 }
352
353 return getImmOpValue(MI, OpNo, Fixups, STI);
354}
355
356unsigned RISCVMCCodeEmitter::getImmOpValue(const MCInst &MI, unsigned OpNo,
358 const MCSubtargetInfo &STI) const {
359 bool EnableRelax = STI.hasFeature(RISCV::FeatureRelax);
360 const MCOperand &MO = MI.getOperand(OpNo);
361
362 MCInstrDesc const &Desc = MCII.get(MI.getOpcode());
363 unsigned MIFrm = RISCVII::getFormat(Desc.TSFlags);
364
365 // If the destination is an immediate, there is nothing to do.
366 if (MO.isImm())
367 return MO.getImm();
368
369 assert(MO.isExpr() &&
370 "getImmOpValue expects only expressions or immediates");
371 const MCExpr *Expr = MO.getExpr();
372 MCExpr::ExprKind Kind = Expr->getKind();
374 bool RelaxCandidate = false;
375 if (Kind == MCExpr::Target) {
376 const RISCVMCExpr *RVExpr = cast<RISCVMCExpr>(Expr);
377
378 switch (RVExpr->getKind()) {
382 llvm_unreachable("Unhandled fixup kind!");
384 // tprel_add is only used to indicate that a relocation should be emitted
385 // for an add instruction used in TP-relative addressing. It should not be
386 // expanded as if representing an actual instruction operand and so to
387 // encounter it here is an error.
389 "VK_RISCV_TPREL_ADD should not represent an instruction operand");
391 if (MIFrm == RISCVII::InstFormatI)
393 else if (MIFrm == RISCVII::InstFormatS)
395 else
396 llvm_unreachable("VK_RISCV_LO used with unexpected instruction format");
397 RelaxCandidate = true;
398 break;
401 RelaxCandidate = true;
402 break;
404 if (MIFrm == RISCVII::InstFormatI)
406 else if (MIFrm == RISCVII::InstFormatS)
408 else
410 "VK_RISCV_PCREL_LO used with unexpected instruction format");
411 RelaxCandidate = true;
412 break;
415 RelaxCandidate = true;
416 break;
419 break;
421 if (MIFrm == RISCVII::InstFormatI)
423 else if (MIFrm == RISCVII::InstFormatS)
425 else
427 "VK_RISCV_TPREL_LO used with unexpected instruction format");
428 RelaxCandidate = true;
429 break;
432 RelaxCandidate = true;
433 break;
436 break;
439 break;
442 RelaxCandidate = true;
443 break;
446 RelaxCandidate = true;
447 break;
448 }
449 } else if ((Kind == MCExpr::SymbolRef &&
450 cast<MCSymbolRefExpr>(Expr)->getKind() ==
452 Kind == MCExpr::Binary) {
453 // FIXME: Sub kind binary exprs have chance of underflow.
454 if (MIFrm == RISCVII::InstFormatJ) {
456 } else if (MIFrm == RISCVII::InstFormatB) {
458 } else if (MIFrm == RISCVII::InstFormatCJ) {
460 } else if (MIFrm == RISCVII::InstFormatCB) {
462 } else if (MIFrm == RISCVII::InstFormatI) {
464 }
465 }
466
467 assert(FixupKind != RISCV::fixup_riscv_invalid && "Unhandled expression!");
468
469 Fixups.push_back(
470 MCFixup::create(0, Expr, MCFixupKind(FixupKind), MI.getLoc()));
471 ++MCNumFixups;
472
473 // Ensure an R_RISCV_RELAX relocation will be emitted if linker relaxation is
474 // enabled and the current fixup will result in a relocation that may be
475 // relaxed.
476 if (EnableRelax && RelaxCandidate) {
478 Fixups.push_back(
480 MI.getLoc()));
481 ++MCNumFixups;
482 }
483
484 return 0;
485}
486
487unsigned RISCVMCCodeEmitter::getVMaskReg(const MCInst &MI, unsigned OpNo,
489 const MCSubtargetInfo &STI) const {
490 MCOperand MO = MI.getOperand(OpNo);
491 assert(MO.isReg() && "Expected a register.");
492
493 switch (MO.getReg()) {
494 default:
495 llvm_unreachable("Invalid mask register.");
496 case RISCV::V0:
497 return 0;
498 case RISCV::NoRegister:
499 return 1;
500 }
501}
502
503unsigned RISCVMCCodeEmitter::getRlistOpValue(const MCInst &MI, unsigned OpNo,
505 const MCSubtargetInfo &STI) const {
506 const MCOperand &MO = MI.getOperand(OpNo);
507 assert(MO.isImm() && "Rlist operand must be immediate");
508 auto Imm = MO.getImm();
509 assert(Imm >= 4 && "EABI is currently not implemented");
510 return Imm;
511}
512
513unsigned RISCVMCCodeEmitter::getRegReg(const MCInst &MI, unsigned OpNo,
515 const MCSubtargetInfo &STI) const {
516 const MCOperand &MO = MI.getOperand(OpNo);
517 const MCOperand &MO1 = MI.getOperand(OpNo + 1);
518 assert(MO.isReg() && MO1.isReg() && "Expected registers.");
519
520 unsigned Op = Ctx.getRegisterInfo()->getEncodingValue(MO.getReg());
521 unsigned Op1 = Ctx.getRegisterInfo()->getEncodingValue(MO1.getReg());
522
523 return Op | Op1 << 5;
524}
525
526#include "RISCVGenMCCodeEmitter.inc"
uint64_t Size
IRTranslator LLVM IR MI
static unsigned getInvertedBranchOp(unsigned BrOp)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
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
static constexpr uint32_t Opcode
Definition: aarch32.h:200
This class represents an Operation in the Expression.
MCCodeEmitter - Generic instruction encoding interface.
Definition: MCCodeEmitter.h:21
virtual void encodeInstruction(const MCInst &Inst, SmallVectorImpl< char > &CB, SmallVectorImpl< MCFixup > &Fixups, const MCSubtargetInfo &STI) const =0
Encode the given Inst to bytes and append to CB.
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
@ Binary
Binary expressions.
Definition: MCExpr.h:38
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
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:33
constexpr unsigned id() const
Definition: MCRegister.h:79
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
#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
NodeAddr< FuncNode * > Func
Definition: RDFGraph.h:393
void write(void *memory, value_type value, endianness endian)
Write a value to memory with a particular endianness.
Definition: Endian.h:91
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:456
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
Description of the encoding of one expression Op.