LLVM 22.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"
30
31using namespace llvm;
32
33#define DEBUG_TYPE "mccodeemitter"
34
35STATISTIC(MCNumEmitted, "Number of MC instructions emitted");
36STATISTIC(MCNumFixups, "Number of MC fixups created");
37
38namespace {
39class RISCVMCCodeEmitter : public MCCodeEmitter {
40 RISCVMCCodeEmitter(const RISCVMCCodeEmitter &) = delete;
41 void operator=(const RISCVMCCodeEmitter &) = delete;
42 MCContext &Ctx;
43 MCInstrInfo const &MCII;
44
45public:
46 RISCVMCCodeEmitter(MCContext &ctx, MCInstrInfo const &MCII)
47 : Ctx(ctx), MCII(MCII) {}
48
49 ~RISCVMCCodeEmitter() override = default;
50
51 void encodeInstruction(const MCInst &MI, SmallVectorImpl<char> &CB,
52 SmallVectorImpl<MCFixup> &Fixups,
53 const MCSubtargetInfo &STI) const override;
54
55 void expandFunctionCall(const MCInst &MI, SmallVectorImpl<char> &CB,
56 SmallVectorImpl<MCFixup> &Fixups,
57 const MCSubtargetInfo &STI) const;
58
59 void expandTLSDESCCall(const MCInst &MI, SmallVectorImpl<char> &CB,
60 SmallVectorImpl<MCFixup> &Fixups,
61 const MCSubtargetInfo &STI) const;
62
63 void expandAddTPRel(const MCInst &MI, SmallVectorImpl<char> &CB,
64 SmallVectorImpl<MCFixup> &Fixups,
65 const MCSubtargetInfo &STI) const;
66
67 void expandLongCondBr(const MCInst &MI, SmallVectorImpl<char> &CB,
68 SmallVectorImpl<MCFixup> &Fixups,
69 const MCSubtargetInfo &STI) const;
70
71 void expandQCLongCondBrImm(const MCInst &MI, SmallVectorImpl<char> &CB,
72 SmallVectorImpl<MCFixup> &Fixups,
73 const MCSubtargetInfo &STI, unsigned Size) const;
74
75 /// TableGen'erated function for getting the binary encoding for an
76 /// instruction.
77 uint64_t getBinaryCodeForInstr(const MCInst &MI,
78 SmallVectorImpl<MCFixup> &Fixups,
79 const MCSubtargetInfo &STI) const;
80
81 /// Return binary encoding of operand. If the machine operand requires
82 /// relocation, record the relocation and return zero.
83 uint64_t getMachineOpValue(const MCInst &MI, const MCOperand &MO,
84 SmallVectorImpl<MCFixup> &Fixups,
85 const MCSubtargetInfo &STI) const;
86
87 uint64_t getImmOpValueMinus1(const MCInst &MI, unsigned OpNo,
88 SmallVectorImpl<MCFixup> &Fixups,
89 const MCSubtargetInfo &STI) const;
90
91 uint64_t getImmOpValueSlist(const MCInst &MI, unsigned OpNo,
92 SmallVectorImpl<MCFixup> &Fixups,
93 const MCSubtargetInfo &STI) const;
94
95 template <unsigned N>
96 unsigned getImmOpValueAsrN(const MCInst &MI, unsigned OpNo,
97 SmallVectorImpl<MCFixup> &Fixups,
98 const MCSubtargetInfo &STI) const;
99
100 uint64_t getImmOpValueZibi(const MCInst &MI, unsigned OpNo,
101 SmallVectorImpl<MCFixup> &Fixups,
102 const MCSubtargetInfo &STI) const;
103
104 uint64_t getImmOpValue(const MCInst &MI, unsigned OpNo,
105 SmallVectorImpl<MCFixup> &Fixups,
106 const MCSubtargetInfo &STI) const;
107
108 unsigned getVMaskReg(const MCInst &MI, unsigned OpNo,
109 SmallVectorImpl<MCFixup> &Fixups,
110 const MCSubtargetInfo &STI) const;
111
112 unsigned getRlistOpValue(const MCInst &MI, unsigned OpNo,
113 SmallVectorImpl<MCFixup> &Fixups,
114 const MCSubtargetInfo &STI) const;
115
116 unsigned getRlistS0OpValue(const MCInst &MI, unsigned OpNo,
117 SmallVectorImpl<MCFixup> &Fixups,
118 const MCSubtargetInfo &STI) const;
119};
120} // end anonymous namespace
121
123 MCContext &Ctx) {
124 return new RISCVMCCodeEmitter(Ctx, MCII);
125}
126
128 const MCExpr *Value, uint16_t Kind) {
129 bool PCRel = false;
130 switch (Kind) {
131 case ELF::R_RISCV_CALL_PLT:
144 PCRel = true;
145 }
146 Fixups.push_back(MCFixup::create(Offset, Value, Kind, PCRel));
147}
148
149// Expand PseudoCALL(Reg), PseudoTAIL and PseudoJump to AUIPC and JALR with
150// relocation types. We expand those pseudo-instructions while encoding them,
151// meaning AUIPC and JALR won't go through RISC-V MC to MC compressed
152// instruction transformation. This is acceptable because AUIPC has no 16-bit
153// form and C_JALR has no immediate operand field. We let linker relaxation
154// deal with it. When linker relaxation is enabled, AUIPC and JALR have a
155// chance to relax to JAL.
156// If the C extension is enabled, JAL has a chance relax to C_JAL.
157void RISCVMCCodeEmitter::expandFunctionCall(const MCInst &MI,
160 const MCSubtargetInfo &STI) const {
161 MCInst TmpInst;
162 MCOperand Func;
163 MCRegister Ra;
164 if (MI.getOpcode() == RISCV::PseudoTAIL) {
165 Func = MI.getOperand(0);
167 } else if (MI.getOpcode() == RISCV::PseudoCALLReg) {
168 Func = MI.getOperand(1);
169 Ra = MI.getOperand(0).getReg();
170 } else if (MI.getOpcode() == RISCV::PseudoCALL) {
171 Func = MI.getOperand(0);
172 Ra = RISCV::X1;
173 } else if (MI.getOpcode() == RISCV::PseudoJump) {
174 Func = MI.getOperand(1);
175 Ra = MI.getOperand(0).getReg();
176 }
177 uint32_t Binary;
178
179 assert(Func.isExpr() && "Expected expression");
180
181 const MCExpr *CallExpr = Func.getExpr();
182
183 // Emit AUIPC Ra, Func with R_RISCV_CALL relocation type.
184 TmpInst = MCInstBuilder(RISCV::AUIPC).addReg(Ra).addExpr(CallExpr);
185 Binary = getBinaryCodeForInstr(TmpInst, Fixups, STI);
187
188 if (MI.getOpcode() == RISCV::PseudoTAIL ||
189 MI.getOpcode() == RISCV::PseudoJump)
190 // Emit JALR X0, Ra, 0
191 TmpInst = MCInstBuilder(RISCV::JALR).addReg(RISCV::X0).addReg(Ra).addImm(0);
192 else
193 // Emit JALR Ra, Ra, 0
194 TmpInst = MCInstBuilder(RISCV::JALR).addReg(Ra).addReg(Ra).addImm(0);
195 Binary = getBinaryCodeForInstr(TmpInst, Fixups, STI);
197}
198
199void RISCVMCCodeEmitter::expandTLSDESCCall(const MCInst &MI,
200 SmallVectorImpl<char> &CB,
201 SmallVectorImpl<MCFixup> &Fixups,
202 const MCSubtargetInfo &STI) const {
203 MCOperand SrcSymbol = MI.getOperand(3);
204 assert(SrcSymbol.isExpr() &&
205 "Expected expression as first input to TLSDESCCALL");
206 const auto *Expr = dyn_cast<MCSpecifierExpr>(SrcSymbol.getExpr());
207 MCRegister Link = MI.getOperand(0).getReg();
208 MCRegister Dest = MI.getOperand(1).getReg();
209 int64_t Imm = MI.getOperand(2).getImm();
210 addFixup(Fixups, 0, Expr, ELF::R_RISCV_TLSDESC_CALL);
211 MCInst Call =
212 MCInstBuilder(RISCV::JALR).addReg(Link).addReg(Dest).addImm(Imm);
213
214 uint32_t Binary = getBinaryCodeForInstr(Call, Fixups, STI);
216}
217
218// Expand PseudoAddTPRel to a simple ADD with the correct relocation.
219void RISCVMCCodeEmitter::expandAddTPRel(const MCInst &MI,
220 SmallVectorImpl<char> &CB,
221 SmallVectorImpl<MCFixup> &Fixups,
222 const MCSubtargetInfo &STI) const {
223 MCOperand DestReg = MI.getOperand(0);
224 MCOperand SrcReg = MI.getOperand(1);
225 MCOperand TPReg = MI.getOperand(2);
226 assert(TPReg.isReg() && TPReg.getReg() == RISCV::X4 &&
227 "Expected thread pointer as second input to TP-relative add");
228
229 MCOperand SrcSymbol = MI.getOperand(3);
230 assert(SrcSymbol.isExpr() &&
231 "Expected expression as third input to TP-relative add");
232
233 const auto *Expr = dyn_cast<MCSpecifierExpr>(SrcSymbol.getExpr());
234 assert(Expr && Expr->getSpecifier() == ELF::R_RISCV_TPREL_ADD &&
235 "Expected tprel_add relocation on TP-relative symbol");
236
237 addFixup(Fixups, 0, Expr, ELF::R_RISCV_TPREL_ADD);
238 if (STI.hasFeature(RISCV::FeatureRelax))
239 Fixups.back().setLinkerRelaxable();
240
241 // Emit a normal ADD instruction with the given operands.
242 MCInst TmpInst = MCInstBuilder(RISCV::ADD)
243 .addOperand(DestReg)
244 .addOperand(SrcReg)
245 .addOperand(TPReg);
246 uint32_t Binary = getBinaryCodeForInstr(TmpInst, Fixups, STI);
248}
249
250static unsigned getInvertedBranchOp(unsigned BrOp) {
251 switch (BrOp) {
252 default:
253 llvm_unreachable("Unexpected branch opcode!");
254 case RISCV::PseudoLongBEQ:
255 return RISCV::BNE;
256 case RISCV::PseudoLongBNE:
257 return RISCV::BEQ;
258 case RISCV::PseudoLongBLT:
259 return RISCV::BGE;
260 case RISCV::PseudoLongBGE:
261 return RISCV::BLT;
262 case RISCV::PseudoLongBLTU:
263 return RISCV::BGEU;
264 case RISCV::PseudoLongBGEU:
265 return RISCV::BLTU;
266 case RISCV::PseudoLongQC_BEQI:
267 return RISCV::QC_BNEI;
268 case RISCV::PseudoLongQC_BNEI:
269 return RISCV::QC_BEQI;
270 case RISCV::PseudoLongQC_BLTI:
271 return RISCV::QC_BGEI;
272 case RISCV::PseudoLongQC_BGEI:
273 return RISCV::QC_BLTI;
274 case RISCV::PseudoLongQC_BLTUI:
275 return RISCV::QC_BGEUI;
276 case RISCV::PseudoLongQC_BGEUI:
277 return RISCV::QC_BLTUI;
278 case RISCV::PseudoLongQC_E_BEQI:
279 return RISCV::QC_E_BNEI;
280 case RISCV::PseudoLongQC_E_BNEI:
281 return RISCV::QC_E_BEQI;
282 case RISCV::PseudoLongQC_E_BLTI:
283 return RISCV::QC_E_BGEI;
284 case RISCV::PseudoLongQC_E_BGEI:
285 return RISCV::QC_E_BLTI;
286 case RISCV::PseudoLongQC_E_BLTUI:
287 return RISCV::QC_E_BGEUI;
288 case RISCV::PseudoLongQC_E_BGEUI:
289 return RISCV::QC_E_BLTUI;
290 }
291}
292
293// Expand PseudoLongBxx to an inverted conditional branch and an unconditional
294// jump.
295void RISCVMCCodeEmitter::expandLongCondBr(const MCInst &MI,
296 SmallVectorImpl<char> &CB,
297 SmallVectorImpl<MCFixup> &Fixups,
298 const MCSubtargetInfo &STI) const {
299 MCRegister SrcReg1 = MI.getOperand(0).getReg();
300 MCRegister SrcReg2 = MI.getOperand(1).getReg();
301 MCOperand SrcSymbol = MI.getOperand(2);
302 unsigned Opcode = MI.getOpcode();
303 bool IsEqTest =
304 Opcode == RISCV::PseudoLongBNE || Opcode == RISCV::PseudoLongBEQ;
305
306 bool UseCompressedBr = false;
307 if (IsEqTest && STI.hasFeature(RISCV::FeatureStdExtZca)) {
308 if (RISCV::X8 <= SrcReg1.id() && SrcReg1.id() <= RISCV::X15 &&
309 SrcReg2.id() == RISCV::X0) {
310 UseCompressedBr = true;
311 } else if (RISCV::X8 <= SrcReg2.id() && SrcReg2.id() <= RISCV::X15 &&
312 SrcReg1.id() == RISCV::X0) {
313 std::swap(SrcReg1, SrcReg2);
314 UseCompressedBr = true;
315 }
316 }
317
318 uint32_t Offset;
319 if (UseCompressedBr) {
320 unsigned InvOpc =
321 Opcode == RISCV::PseudoLongBNE ? RISCV::C_BEQZ : RISCV::C_BNEZ;
322 MCInst TmpInst = MCInstBuilder(InvOpc).addReg(SrcReg1).addImm(6);
323 uint16_t Binary = getBinaryCodeForInstr(TmpInst, Fixups, STI);
325 Offset = 2;
326 } else {
327 unsigned InvOpc = getInvertedBranchOp(Opcode);
328 MCInst TmpInst =
329 MCInstBuilder(InvOpc).addReg(SrcReg1).addReg(SrcReg2).addImm(8);
330 uint32_t Binary = getBinaryCodeForInstr(TmpInst, Fixups, STI);
332 Offset = 4;
333 }
334
335 // Save the number fixups.
336 size_t FixupStartIndex = Fixups.size();
337
338 // Emit an unconditional jump to the destination.
339 MCInst TmpInst =
340 MCInstBuilder(RISCV::JAL).addReg(RISCV::X0).addOperand(SrcSymbol);
341 uint32_t Binary = getBinaryCodeForInstr(TmpInst, Fixups, STI);
343
344 // Drop any fixup added so we can add the correct one.
345 Fixups.resize(FixupStartIndex);
346
347 if (SrcSymbol.isExpr()) {
348 addFixup(Fixups, Offset, SrcSymbol.getExpr(), RISCV::fixup_riscv_jal);
349 if (STI.hasFeature(RISCV::FeatureRelax))
350 Fixups.back().setLinkerRelaxable();
351 }
352}
353
354// Expand PseudoLongQC_(E_)Bxxx to an inverted conditional branch and an
355// unconditional jump.
356void RISCVMCCodeEmitter::expandQCLongCondBrImm(const MCInst &MI,
357 SmallVectorImpl<char> &CB,
358 SmallVectorImpl<MCFixup> &Fixups,
359 const MCSubtargetInfo &STI,
360 unsigned Size) const {
361 MCRegister SrcReg1 = MI.getOperand(0).getReg();
362 auto BrImm = MI.getOperand(1).getImm();
363 MCOperand SrcSymbol = MI.getOperand(2);
364 unsigned Opcode = MI.getOpcode();
365 uint32_t Offset;
366 unsigned InvOpc = getInvertedBranchOp(Opcode);
367 // Emit inverted conditional branch with offset:
368 // 8 (QC.BXXX(4) + JAL(4))
369 // or
370 // 10 (QC.E.BXXX(6) + JAL(4)).
371 if (Size == 4) {
372 MCInst TmpBr =
373 MCInstBuilder(InvOpc).addReg(SrcReg1).addImm(BrImm).addImm(8);
374 uint32_t BrBinary = getBinaryCodeForInstr(TmpBr, Fixups, STI);
376 } else {
377 MCInst TmpBr =
378 MCInstBuilder(InvOpc).addReg(SrcReg1).addImm(BrImm).addImm(10);
379 uint64_t BrBinary =
380 getBinaryCodeForInstr(TmpBr, Fixups, STI) & 0xffff'ffff'ffffu;
381 SmallVector<char, 8> Encoding;
383 assert(Encoding[6] == 0 && Encoding[7] == 0 &&
384 "Unexpected encoding for 48-bit instruction");
385 Encoding.truncate(6);
386 CB.append(Encoding);
387 }
388 Offset = Size;
389 // Save the number fixups.
390 size_t FixupStartIndex = Fixups.size();
391 // Emit an unconditional jump to the destination.
392 MCInst TmpJ =
393 MCInstBuilder(RISCV::JAL).addReg(RISCV::X0).addOperand(SrcSymbol);
394 uint32_t JBinary = getBinaryCodeForInstr(TmpJ, Fixups, STI);
396 // Drop any fixup added so we can add the correct one.
397 Fixups.resize(FixupStartIndex);
398 if (SrcSymbol.isExpr()) {
399 addFixup(Fixups, Offset, SrcSymbol.getExpr(), RISCV::fixup_riscv_jal);
400 if (STI.hasFeature(RISCV::FeatureRelax))
401 Fixups.back().setLinkerRelaxable();
402 }
403}
404
405void RISCVMCCodeEmitter::encodeInstruction(const MCInst &MI,
406 SmallVectorImpl<char> &CB,
407 SmallVectorImpl<MCFixup> &Fixups,
408 const MCSubtargetInfo &STI) const {
409 const MCInstrDesc &Desc = MCII.get(MI.getOpcode());
410 // Get byte count of instruction.
411 unsigned Size = Desc.getSize();
412
413 // RISCVInstrInfo::getInstSizeInBytes expects that the total size of the
414 // expanded instructions for each pseudo is correct in the Size field of the
415 // tablegen definition for the pseudo.
416 switch (MI.getOpcode()) {
417 default:
418 break;
419 case RISCV::PseudoCALLReg:
420 case RISCV::PseudoCALL:
421 case RISCV::PseudoTAIL:
422 case RISCV::PseudoJump:
423 expandFunctionCall(MI, CB, Fixups, STI);
424 MCNumEmitted += 2;
425 return;
426 case RISCV::PseudoAddTPRel:
427 expandAddTPRel(MI, CB, Fixups, STI);
428 MCNumEmitted += 1;
429 return;
430 case RISCV::PseudoLongBEQ:
431 case RISCV::PseudoLongBNE:
432 case RISCV::PseudoLongBLT:
433 case RISCV::PseudoLongBGE:
434 case RISCV::PseudoLongBLTU:
435 case RISCV::PseudoLongBGEU:
436 expandLongCondBr(MI, CB, Fixups, STI);
437 MCNumEmitted += 2;
438 return;
439 case RISCV::PseudoLongQC_BEQI:
440 case RISCV::PseudoLongQC_BNEI:
441 case RISCV::PseudoLongQC_BLTI:
442 case RISCV::PseudoLongQC_BGEI:
443 case RISCV::PseudoLongQC_BLTUI:
444 case RISCV::PseudoLongQC_BGEUI:
445 expandQCLongCondBrImm(MI, CB, Fixups, STI, 4);
446 MCNumEmitted += 2;
447 return;
448 case RISCV::PseudoLongQC_E_BEQI:
449 case RISCV::PseudoLongQC_E_BNEI:
450 case RISCV::PseudoLongQC_E_BLTI:
451 case RISCV::PseudoLongQC_E_BGEI:
452 case RISCV::PseudoLongQC_E_BLTUI:
453 case RISCV::PseudoLongQC_E_BGEUI:
454 expandQCLongCondBrImm(MI, CB, Fixups, STI, 6);
455 MCNumEmitted += 2;
456 return;
457 case RISCV::PseudoTLSDESCCall:
458 expandTLSDESCCall(MI, CB, Fixups, STI);
459 MCNumEmitted += 1;
460 return;
461 }
462
463 switch (Size) {
464 default:
465 llvm_unreachable("Unhandled encodeInstruction length!");
466 case 2: {
467 uint16_t Bits = getBinaryCodeForInstr(MI, Fixups, STI);
469 break;
470 }
471 case 4: {
472 uint32_t Bits = getBinaryCodeForInstr(MI, Fixups, STI);
474 break;
475 }
476 case 6: {
477 uint64_t Bits = getBinaryCodeForInstr(MI, Fixups, STI) & 0xffff'ffff'ffffu;
478 SmallVector<char, 8> Encoding;
480 assert(Encoding[6] == 0 && Encoding[7] == 0 &&
481 "Unexpected encoding for 48-bit instruction");
482 Encoding.truncate(6);
483 CB.append(Encoding);
484 break;
485 }
486 case 8: {
487 uint64_t Bits = getBinaryCodeForInstr(MI, Fixups, STI);
489 break;
490 }
491 }
492
493 ++MCNumEmitted; // Keep track of the # of mi's emitted.
494}
495
496uint64_t
497RISCVMCCodeEmitter::getMachineOpValue(const MCInst &MI, const MCOperand &MO,
498 SmallVectorImpl<MCFixup> &Fixups,
499 const MCSubtargetInfo &STI) const {
500
501 if (MO.isReg())
502 return Ctx.getRegisterInfo()->getEncodingValue(MO.getReg());
503
504 if (MO.isImm())
505 return MO.getImm();
506
507 llvm_unreachable("Unhandled expression!");
508 return 0;
509}
510
511uint64_t
512RISCVMCCodeEmitter::getImmOpValueMinus1(const MCInst &MI, unsigned OpNo,
513 SmallVectorImpl<MCFixup> &Fixups,
514 const MCSubtargetInfo &STI) const {
515 const MCOperand &MO = MI.getOperand(OpNo);
516
517 if (MO.isImm()) {
518 uint64_t Res = MO.getImm();
519 return (Res - 1);
520 }
521
522 llvm_unreachable("Unhandled expression!");
523 return 0;
524}
525
526uint64_t
527RISCVMCCodeEmitter::getImmOpValueSlist(const MCInst &MI, unsigned OpNo,
528 SmallVectorImpl<MCFixup> &Fixups,
529 const MCSubtargetInfo &STI) const {
530 const MCOperand &MO = MI.getOperand(OpNo);
531 assert(MO.isImm() && "Slist operand must be immediate");
532
533 uint64_t Res = MO.getImm();
534 switch (Res) {
535 case 0:
536 return 0;
537 case 1:
538 return 1;
539 case 2:
540 return 2;
541 case 4:
542 return 3;
543 case 8:
544 return 4;
545 case 16:
546 return 5;
547 case 15:
548 return 6;
549 case 31:
550 return 7;
551 default:
552 llvm_unreachable("Unhandled Slist value!");
553 }
554}
555
556template <unsigned N>
557unsigned
558RISCVMCCodeEmitter::getImmOpValueAsrN(const MCInst &MI, unsigned OpNo,
559 SmallVectorImpl<MCFixup> &Fixups,
560 const MCSubtargetInfo &STI) const {
561 const MCOperand &MO = MI.getOperand(OpNo);
562
563 if (MO.isImm()) {
564 uint64_t Res = MO.getImm();
565 assert((Res & ((1 << N) - 1)) == 0 && "LSB is non-zero");
566 return Res >> N;
567 }
568
569 return getImmOpValue(MI, OpNo, Fixups, STI);
570}
571
572uint64_t
573RISCVMCCodeEmitter::getImmOpValueZibi(const MCInst &MI, unsigned OpNo,
574 SmallVectorImpl<MCFixup> &Fixups,
575 const MCSubtargetInfo &STI) const {
576 const MCOperand &MO = MI.getOperand(OpNo);
577 assert(MO.isImm() && "Zibi operand must be an immediate");
578 int64_t Res = MO.getImm();
579 if (Res == -1)
580 return 0;
581
582 return Res;
583}
584
585uint64_t RISCVMCCodeEmitter::getImmOpValue(const MCInst &MI, unsigned OpNo,
586 SmallVectorImpl<MCFixup> &Fixups,
587 const MCSubtargetInfo &STI) const {
588 bool EnableRelax = STI.hasFeature(RISCV::FeatureRelax);
589 const MCOperand &MO = MI.getOperand(OpNo);
590
591 MCInstrDesc const &Desc = MCII.get(MI.getOpcode());
592 unsigned MIFrm = RISCVII::getFormat(Desc.TSFlags);
593
594 // If the destination is an immediate, there is nothing to do.
595 if (MO.isImm())
596 return MO.getImm();
597
598 assert(MO.isExpr() &&
599 "getImmOpValue expects only expressions or immediates");
600 const MCExpr *Expr = MO.getExpr();
601 MCExpr::ExprKind Kind = Expr->getKind();
602
603 // `RelaxCandidate` must be set to `true` in two cases:
604 // - The fixup's relocation gets a R_RISCV_RELAX relocation
605 // - The underlying instruction may be relaxed to an instruction that gets a
606 // `R_RISCV_RELAX` relocation.
607 //
608 // The actual emission of `R_RISCV_RELAX` will be handled in
609 // `RISCVAsmBackend::applyFixup`.
610 bool RelaxCandidate = false;
611 auto AsmRelaxToLinkerRelaxable = [&]() -> void {
612 if (!STI.hasFeature(RISCV::FeatureExactAssembly))
613 RelaxCandidate = true;
614 };
615
617 if (Kind == MCExpr::Specifier) {
618 const auto *RVExpr = cast<MCSpecifierExpr>(Expr);
619 FixupKind = RVExpr->getSpecifier();
620 switch (RVExpr->getSpecifier()) {
621 default:
623 "invalid specifier");
624 break;
625 case ELF::R_RISCV_TPREL_ADD:
626 // tprel_add is only used to indicate that a relocation should be emitted
627 // for an add instruction used in TP-relative addressing. It should not be
628 // expanded as if representing an actual instruction operand and so to
629 // encounter it here is an error.
631 "ELF::R_RISCV_TPREL_ADD should not represent an instruction operand");
632 case RISCV::S_LO:
633 if (MIFrm == RISCVII::InstFormatI)
635 else if (MIFrm == RISCVII::InstFormatS)
637 else
638 llvm_unreachable("VK_LO used with unexpected instruction format");
639 RelaxCandidate = true;
640 break;
641 case ELF::R_RISCV_HI20:
643 RelaxCandidate = true;
644 break;
646 if (MIFrm == RISCVII::InstFormatI)
648 else if (MIFrm == RISCVII::InstFormatS)
650 else
651 llvm_unreachable("VK_PCREL_LO used with unexpected instruction format");
652 RelaxCandidate = true;
653 break;
654 case ELF::R_RISCV_PCREL_HI20:
656 RelaxCandidate = true;
657 break;
659 if (MIFrm == RISCVII::InstFormatI)
660 FixupKind = ELF::R_RISCV_TPREL_LO12_I;
661 else if (MIFrm == RISCVII::InstFormatS)
662 FixupKind = ELF::R_RISCV_TPREL_LO12_S;
663 else
664 llvm_unreachable("VK_TPREL_LO used with unexpected instruction format");
665 RelaxCandidate = true;
666 break;
667 case ELF::R_RISCV_CALL_PLT:
669 RelaxCandidate = true;
670 break;
673 RelaxCandidate = true;
674 break;
675 case ELF::R_RISCV_GOT_HI20:
676 case ELF::R_RISCV_TPREL_HI20:
677 case ELF::R_RISCV_TLSDESC_HI20:
678 RelaxCandidate = true;
679 break;
680 }
681 } else if (Kind == MCExpr::SymbolRef || Kind == MCExpr::Binary) {
682 // FIXME: Sub kind binary exprs have chance of underflow.
683 if (MIFrm == RISCVII::InstFormatJ) {
685 RelaxCandidate = true;
686 } else if (MIFrm == RISCVII::InstFormatB) {
688 // Relaxes to B<cc>; JAL, with fixup_riscv_jal
689 AsmRelaxToLinkerRelaxable();
690 } else if (MIFrm == RISCVII::InstFormatCJ) {
692 // Relaxes to JAL with fixup_riscv_jal
693 AsmRelaxToLinkerRelaxable();
694 } else if (MIFrm == RISCVII::InstFormatCB) {
696 // Relaxes to B<cc>; JAL, with fixup_riscv_jal
697 AsmRelaxToLinkerRelaxable();
698 } else if (MIFrm == RISCVII::InstFormatCI) {
700 // Relaxes to `QC.E.LI` with fixup_riscv_qc_e_32
701 if (STI.hasFeature(RISCV::FeatureVendorXqcili))
702 AsmRelaxToLinkerRelaxable();
703 } else if (MIFrm == RISCVII::InstFormatI) {
705 } else if (MIFrm == RISCVII::InstFormatQC_EB) {
707 // Relaxes to QC.E.B<cc>I; JAL, with fixup_riscv_jal
708 AsmRelaxToLinkerRelaxable();
709 } else if (MIFrm == RISCVII::InstFormatQC_EAI) {
711 RelaxCandidate = true;
712 } else if (MIFrm == RISCVII::InstFormatQC_EJ) {
714 RelaxCandidate = true;
715 } else if (MIFrm == RISCVII::InstFormatNDS_BRANCH_10) {
717 }
718 }
719
720 assert(FixupKind != RISCV::fixup_riscv_invalid && "Unhandled expression!");
721
722 addFixup(Fixups, 0, Expr, FixupKind);
723 // If linker relaxation is enabled and supported by this relocation, set a bit
724 // so that the assembler knows the size of the instruction is not fixed/known,
725 // and the relocation will need a R_RISCV_RELAX relocation.
726 if (EnableRelax && RelaxCandidate)
727 Fixups.back().setLinkerRelaxable();
728 ++MCNumFixups;
729
730 return 0;
731}
732
733unsigned RISCVMCCodeEmitter::getVMaskReg(const MCInst &MI, unsigned OpNo,
734 SmallVectorImpl<MCFixup> &Fixups,
735 const MCSubtargetInfo &STI) const {
736 MCOperand MO = MI.getOperand(OpNo);
737 assert(MO.isReg() && "Expected a register.");
738
739 switch (MO.getReg().id()) {
740 default:
741 llvm_unreachable("Invalid mask register.");
742 case RISCV::V0:
743 return 0;
744 case RISCV::NoRegister:
745 return 1;
746 }
747}
748
749unsigned RISCVMCCodeEmitter::getRlistOpValue(const MCInst &MI, unsigned OpNo,
750 SmallVectorImpl<MCFixup> &Fixups,
751 const MCSubtargetInfo &STI) const {
752 const MCOperand &MO = MI.getOperand(OpNo);
753 assert(MO.isImm() && "Rlist operand must be immediate");
754 auto Imm = MO.getImm();
755 assert(Imm >= 4 && "EABI is currently not implemented");
756 return Imm;
757}
758unsigned
759RISCVMCCodeEmitter::getRlistS0OpValue(const MCInst &MI, unsigned OpNo,
760 SmallVectorImpl<MCFixup> &Fixups,
761 const MCSubtargetInfo &STI) const {
762 const MCOperand &MO = MI.getOperand(OpNo);
763 assert(MO.isImm() && "Rlist operand must be immediate");
764 auto Imm = MO.getImm();
765 assert(Imm >= 4 && "EABI is currently not implemented");
766 assert(Imm != RISCVZC::RA && "Rlist operand must include s0");
767 return Imm;
768}
769
770#include "RISCVGenMCCodeEmitter.inc"
static void addFixup(SmallVectorImpl< MCFixup > &Fixups, uint32_t Offset, const MCExpr *Value, uint16_t Kind, bool PCRel=false)
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
IRTranslator LLVM IR MI
static unsigned getInvertedBranchOp(unsigned BrOp)
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:171
MCCodeEmitter - Generic instruction encoding interface.
Context object for machine code objects.
Definition MCContext.h:83
const MCRegisterInfo * getRegisterInfo() const
Definition MCContext.h:414
Base class for the full range of assembler expressions which are needed for parsing.
Definition MCExpr.h:34
@ SymbolRef
References to labels and assigned expressions.
Definition MCExpr.h:43
@ Specifier
Expression with a relocation specifier.
Definition MCExpr.h:45
@ Binary
Binary expressions.
Definition MCExpr.h:41
ExprKind getKind() const
Definition MCExpr.h:85
static MCFixup create(uint32_t Offset, const MCExpr *Value, MCFixupKind Kind, bool PCRel=false)
Consider bit fields if we need more flags.
Definition MCFixup.h:86
Instances of this class represent a single low-level machine instruction.
Definition MCInst.h:188
void addOperand(const MCOperand Op)
Definition MCInst.h:215
Interface to description of machine instruction set.
Definition MCInstrInfo.h:27
const MCInstrDesc & get(unsigned Opcode) const
Return the machine instruction descriptor that corresponds to the specified instruction opcode.
Definition MCInstrInfo.h:90
int64_t getImm() const
Definition MCInst.h:84
bool isImm() const
Definition MCInst.h:66
bool isReg() const
Definition MCInst.h:65
MCRegister getReg() const
Returns the register number.
Definition MCInst.h:73
const MCExpr * getExpr() const
Definition MCInst.h:118
bool isExpr() const
Definition MCInst.h:69
uint16_t getEncodingValue(MCRegister Reg) const
Returns the encoding for Reg.
constexpr unsigned id() const
Definition MCRegister.h:82
Generic base class for all target subtargets.
bool hasFeature(unsigned Feature) const
const FeatureBitset & getFeatureBits() const
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
void truncate(size_type N)
Like resize, but requires that N is less than size().
LLVM Value Representation.
Definition Value.h:75
CallInst * Call
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
static unsigned getFormat(uint64_t TSFlags)
static MCRegister getTailExpandUseRegNo(const FeatureBitset &FeatureBits)
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:96
This is an optimization pass for GlobalISel generic memory operations.
Definition Types.h:26
@ Offset
Definition DWP.cpp:532
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
MCCodeEmitter * createRISCVMCCodeEmitter(const MCInstrInfo &MCII, MCContext &Ctx)
Op::Description Desc
static Lanai::Fixups FixupKind(const MCExpr *Expr)
static void addFixup(SmallVectorImpl< MCFixup > &Fixups, uint32_t Offset, const MCExpr *Value, uint16_t Kind)
@ FirstTargetFixupKind
Definition MCFixup.h:44
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition BitVector.h:872
#define N