LLVM 23.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 getYBNDSWImmOpValue(const MCInst &MI, unsigned OpNo,
109 SmallVectorImpl<MCFixup> &Fixups,
110 const MCSubtargetInfo &STI) const;
111
112 unsigned getVMaskReg(const MCInst &MI, unsigned OpNo,
113 SmallVectorImpl<MCFixup> &Fixups,
114 const MCSubtargetInfo &STI) const;
115
116 unsigned getRlistOpValue(const MCInst &MI, unsigned OpNo,
117 SmallVectorImpl<MCFixup> &Fixups,
118 const MCSubtargetInfo &STI) const;
119
120 unsigned getRlistS0OpValue(const MCInst &MI, unsigned OpNo,
121 SmallVectorImpl<MCFixup> &Fixups,
122 const MCSubtargetInfo &STI) const;
123};
124} // end anonymous namespace
125
127 MCContext &Ctx) {
128 return new RISCVMCCodeEmitter(Ctx, MCII);
129}
130
132 const MCExpr *Value, uint16_t Kind) {
133 bool PCRel = false;
134 switch (Kind) {
135 case ELF::R_RISCV_CALL_PLT:
148 PCRel = true;
149 }
150 Fixups.push_back(MCFixup::create(Offset, Value, Kind, PCRel));
151}
152
153// Expand PseudoCALL(Reg), PseudoTAIL and PseudoJump to AUIPC and JALR with
154// relocation types. We expand those pseudo-instructions while encoding them,
155// meaning AUIPC and JALR won't go through RISC-V MC to MC compressed
156// instruction transformation. This is acceptable because AUIPC has no 16-bit
157// form and C_JALR has no immediate operand field. We let linker relaxation
158// deal with it. When linker relaxation is enabled, AUIPC and JALR have a
159// chance to relax to JAL.
160// If the C extension is enabled, JAL has a chance relax to C_JAL.
161void RISCVMCCodeEmitter::expandFunctionCall(const MCInst &MI,
164 const MCSubtargetInfo &STI) const {
165 MCInst TmpInst;
166 MCOperand Func;
167 MCRegister Ra;
168 if (MI.getOpcode() == RISCV::PseudoTAIL) {
169 Func = MI.getOperand(0);
171 } else if (MI.getOpcode() == RISCV::PseudoCALLReg) {
172 Func = MI.getOperand(1);
173 Ra = MI.getOperand(0).getReg();
174 } else if (MI.getOpcode() == RISCV::PseudoCALL) {
175 Func = MI.getOperand(0);
176 Ra = RISCV::X1;
177 } else if (MI.getOpcode() == RISCV::PseudoJump) {
178 Func = MI.getOperand(1);
179 Ra = MI.getOperand(0).getReg();
180 }
181 uint32_t Binary;
182
183 assert(Func.isExpr() && "Expected expression");
184
185 const MCExpr *CallExpr = Func.getExpr();
186
188 MCOperand FuncOp = MCOperand::createExpr(CallExpr);
189 if (MI.getOpcode() == RISCV::PseudoTAIL ||
190 MI.getOpcode() == RISCV::PseudoJump)
191 // Emit JAL X0, Func
192 TmpInst = MCInstBuilder(RISCV::JAL).addReg(RISCV::X0).addOperand(FuncOp);
193 else
194 // Emit JAL Ra, Func
195 TmpInst = MCInstBuilder(RISCV::JAL).addReg(Ra).addOperand(FuncOp);
196 Binary = getBinaryCodeForInstr(TmpInst, Fixups, STI);
198 return;
199 }
200 // Emit AUIPC Ra, Func with R_RISCV_CALL relocation type.
201 TmpInst = MCInstBuilder(RISCV::AUIPC).addReg(Ra).addExpr(CallExpr);
202 Binary = getBinaryCodeForInstr(TmpInst, Fixups, STI);
204
205 if (MI.getOpcode() == RISCV::PseudoTAIL ||
206 MI.getOpcode() == RISCV::PseudoJump)
207 // Emit JALR X0, Ra, 0
208 TmpInst = MCInstBuilder(RISCV::JALR).addReg(RISCV::X0).addReg(Ra).addImm(0);
209 else
210 // Emit JALR Ra, Ra, 0
211 TmpInst = MCInstBuilder(RISCV::JALR).addReg(Ra).addReg(Ra).addImm(0);
212 Binary = getBinaryCodeForInstr(TmpInst, Fixups, STI);
214}
215
216void RISCVMCCodeEmitter::expandTLSDESCCall(const MCInst &MI,
217 SmallVectorImpl<char> &CB,
218 SmallVectorImpl<MCFixup> &Fixups,
219 const MCSubtargetInfo &STI) const {
220 MCOperand SrcSymbol = MI.getOperand(3);
221 assert(SrcSymbol.isExpr() &&
222 "Expected expression as first input to TLSDESCCALL");
223 const auto *Expr = dyn_cast<MCSpecifierExpr>(SrcSymbol.getExpr());
224 MCRegister Link = MI.getOperand(0).getReg();
225 MCRegister Dest = MI.getOperand(1).getReg();
226 int64_t Imm = MI.getOperand(2).getImm();
227 addFixup(Fixups, 0, Expr, ELF::R_RISCV_TLSDESC_CALL);
228 MCInst Call =
229 MCInstBuilder(RISCV::JALR).addReg(Link).addReg(Dest).addImm(Imm);
230
231 uint32_t Binary = getBinaryCodeForInstr(Call, Fixups, STI);
233}
234
235// Expand PseudoAddTPRel to a simple ADD with the correct relocation.
236void RISCVMCCodeEmitter::expandAddTPRel(const MCInst &MI,
237 SmallVectorImpl<char> &CB,
238 SmallVectorImpl<MCFixup> &Fixups,
239 const MCSubtargetInfo &STI) const {
240 MCOperand DestReg = MI.getOperand(0);
241 MCOperand SrcReg = MI.getOperand(1);
242 MCOperand TPReg = MI.getOperand(2);
243 assert(TPReg.isReg() && TPReg.getReg() == RISCV::X4 &&
244 "Expected thread pointer as second input to TP-relative add");
245
246 MCOperand SrcSymbol = MI.getOperand(3);
247 assert(SrcSymbol.isExpr() &&
248 "Expected expression as third input to TP-relative add");
249
250 const auto *Expr = dyn_cast<MCSpecifierExpr>(SrcSymbol.getExpr());
251 assert(Expr && Expr->getSpecifier() == ELF::R_RISCV_TPREL_ADD &&
252 "Expected tprel_add relocation on TP-relative symbol");
253
254 addFixup(Fixups, 0, Expr, ELF::R_RISCV_TPREL_ADD);
255 if (STI.hasFeature(RISCV::FeatureRelax))
256 Fixups.back().setLinkerRelaxable();
257
258 // Emit a normal ADD instruction with the given operands.
259 MCInst TmpInst = MCInstBuilder(RISCV::ADD)
260 .addOperand(DestReg)
261 .addOperand(SrcReg)
262 .addOperand(TPReg);
263 uint32_t Binary = getBinaryCodeForInstr(TmpInst, Fixups, STI);
265}
266
267static unsigned getInvertedBranchOp(unsigned BrOp) {
268 switch (BrOp) {
269 default:
270 llvm_unreachable("Unexpected branch opcode!");
271 case RISCV::PseudoLongBEQ:
272 return RISCV::BNE;
273 case RISCV::PseudoLongBNE:
274 return RISCV::BEQ;
275 case RISCV::PseudoLongBEQI:
276 return RISCV::BNEI;
277 case RISCV::PseudoLongBNEI:
278 return RISCV::BEQI;
279 case RISCV::PseudoLongBLT:
280 return RISCV::BGE;
281 case RISCV::PseudoLongBGE:
282 return RISCV::BLT;
283 case RISCV::PseudoLongBLTU:
284 return RISCV::BGEU;
285 case RISCV::PseudoLongBGEU:
286 return RISCV::BLTU;
287 case RISCV::PseudoLongQC_BEQI:
288 return RISCV::QC_BNEI;
289 case RISCV::PseudoLongQC_BNEI:
290 return RISCV::QC_BEQI;
291 case RISCV::PseudoLongQC_BLTI:
292 return RISCV::QC_BGEI;
293 case RISCV::PseudoLongQC_BGEI:
294 return RISCV::QC_BLTI;
295 case RISCV::PseudoLongQC_BLTUI:
296 return RISCV::QC_BGEUI;
297 case RISCV::PseudoLongQC_BGEUI:
298 return RISCV::QC_BLTUI;
299 case RISCV::PseudoLongQC_E_BEQI:
300 return RISCV::QC_E_BNEI;
301 case RISCV::PseudoLongQC_E_BNEI:
302 return RISCV::QC_E_BEQI;
303 case RISCV::PseudoLongQC_E_BLTI:
304 return RISCV::QC_E_BGEI;
305 case RISCV::PseudoLongQC_E_BGEI:
306 return RISCV::QC_E_BLTI;
307 case RISCV::PseudoLongQC_E_BLTUI:
308 return RISCV::QC_E_BGEUI;
309 case RISCV::PseudoLongQC_E_BGEUI:
310 return RISCV::QC_E_BLTUI;
311 }
312}
313
314// Expand PseudoLongBxx to an inverted conditional branch and an unconditional
315// jump.
316void RISCVMCCodeEmitter::expandLongCondBr(const MCInst &MI,
317 SmallVectorImpl<char> &CB,
318 SmallVectorImpl<MCFixup> &Fixups,
319 const MCSubtargetInfo &STI) const {
320 MCRegister SrcReg1 = MI.getOperand(0).getReg();
321 const MCOperand &Src2 = MI.getOperand(1);
322 const MCOperand &SrcSymbol = MI.getOperand(2);
323 unsigned Opcode = MI.getOpcode();
324 bool IsEqTest =
325 Opcode == RISCV::PseudoLongBNE || Opcode == RISCV::PseudoLongBEQ;
326
327 bool UseCompressedBr = false;
328 if (IsEqTest && STI.hasFeature(RISCV::FeatureStdExtZca)) {
329 MCRegister SrcReg2 = Src2.getReg();
330 if (RISCV::X8 <= SrcReg1.id() && SrcReg1.id() <= RISCV::X15 &&
331 SrcReg2.id() == RISCV::X0) {
332 UseCompressedBr = true;
333 } else if (RISCV::X8 <= SrcReg2.id() && SrcReg2.id() <= RISCV::X15 &&
334 SrcReg1.id() == RISCV::X0) {
335 std::swap(SrcReg1, SrcReg2);
336 UseCompressedBr = true;
337 }
338 }
339
340 uint32_t Offset;
341 if (UseCompressedBr) {
342 unsigned InvOpc =
343 Opcode == RISCV::PseudoLongBNE ? RISCV::C_BEQZ : RISCV::C_BNEZ;
344 MCInst TmpInst = MCInstBuilder(InvOpc).addReg(SrcReg1).addImm(6);
345 uint16_t Binary = getBinaryCodeForInstr(TmpInst, Fixups, STI);
347 Offset = 2;
348 } else {
349 unsigned InvOpc = getInvertedBranchOp(Opcode);
350 MCInst TmpInst =
351 MCInstBuilder(InvOpc).addReg(SrcReg1).addOperand(Src2).addImm(8);
352 uint32_t Binary = getBinaryCodeForInstr(TmpInst, Fixups, STI);
354 Offset = 4;
355 }
356
357 // Save the number fixups.
358 size_t FixupStartIndex = Fixups.size();
359
360 // Emit an unconditional jump to the destination.
361 MCInst TmpInst =
362 MCInstBuilder(RISCV::JAL).addReg(RISCV::X0).addOperand(SrcSymbol);
363 uint32_t Binary = getBinaryCodeForInstr(TmpInst, Fixups, STI);
365
366 // Drop any fixup added so we can add the correct one.
367 Fixups.resize(FixupStartIndex);
368
369 if (SrcSymbol.isExpr()) {
370 addFixup(Fixups, Offset, SrcSymbol.getExpr(), RISCV::fixup_riscv_jal);
371 if (STI.hasFeature(RISCV::FeatureRelax))
372 Fixups.back().setLinkerRelaxable();
373 }
374}
375
376// Expand PseudoLongQC_(E_)Bxxx to an inverted conditional branch and an
377// unconditional jump.
378void RISCVMCCodeEmitter::expandQCLongCondBrImm(const MCInst &MI,
379 SmallVectorImpl<char> &CB,
380 SmallVectorImpl<MCFixup> &Fixups,
381 const MCSubtargetInfo &STI,
382 unsigned Size) const {
383 MCRegister SrcReg1 = MI.getOperand(0).getReg();
384 auto BrImm = MI.getOperand(1).getImm();
385 MCOperand SrcSymbol = MI.getOperand(2);
386 unsigned Opcode = MI.getOpcode();
387 uint32_t Offset;
388 unsigned InvOpc = getInvertedBranchOp(Opcode);
389 // Emit inverted conditional branch with offset:
390 // 8 (QC.BXXX(4) + JAL(4))
391 // or
392 // 10 (QC.E.BXXX(6) + JAL(4)).
393 if (Size == 4) {
394 MCInst TmpBr =
395 MCInstBuilder(InvOpc).addReg(SrcReg1).addImm(BrImm).addImm(8);
396 uint32_t BrBinary = getBinaryCodeForInstr(TmpBr, Fixups, STI);
398 } else {
399 MCInst TmpBr =
400 MCInstBuilder(InvOpc).addReg(SrcReg1).addImm(BrImm).addImm(10);
401 uint64_t BrBinary =
402 getBinaryCodeForInstr(TmpBr, Fixups, STI) & 0xffff'ffff'ffffu;
403 SmallVector<char, 8> Encoding;
405 assert(Encoding[6] == 0 && Encoding[7] == 0 &&
406 "Unexpected encoding for 48-bit instruction");
407 Encoding.truncate(6);
408 CB.append(Encoding);
409 }
410 Offset = Size;
411 // Save the number fixups.
412 size_t FixupStartIndex = Fixups.size();
413 // Emit an unconditional jump to the destination.
414 MCInst TmpJ =
415 MCInstBuilder(RISCV::JAL).addReg(RISCV::X0).addOperand(SrcSymbol);
416 uint32_t JBinary = getBinaryCodeForInstr(TmpJ, Fixups, STI);
418 // Drop any fixup added so we can add the correct one.
419 Fixups.resize(FixupStartIndex);
420 if (SrcSymbol.isExpr()) {
421 addFixup(Fixups, Offset, SrcSymbol.getExpr(), RISCV::fixup_riscv_jal);
422 if (STI.hasFeature(RISCV::FeatureRelax))
423 Fixups.back().setLinkerRelaxable();
424 }
425}
426
427void RISCVMCCodeEmitter::encodeInstruction(const MCInst &MI,
428 SmallVectorImpl<char> &CB,
429 SmallVectorImpl<MCFixup> &Fixups,
430 const MCSubtargetInfo &STI) const {
431 const MCInstrDesc &Desc = MCII.get(MI.getOpcode());
432 // Get byte count of instruction.
433 unsigned Size = Desc.getSize();
434
435 // RISCVInstrInfo::getInstSizeInBytes expects that the total size of the
436 // expanded instructions for each pseudo is correct in the Size field of the
437 // tablegen definition for the pseudo.
438 switch (MI.getOpcode()) {
439 default:
440 break;
441 case RISCV::PseudoCALLReg:
442 case RISCV::PseudoCALL:
443 case RISCV::PseudoTAIL:
444 case RISCV::PseudoJump:
445 expandFunctionCall(MI, CB, Fixups, STI);
446 MCNumEmitted += 2;
447 return;
448 case RISCV::PseudoAddTPRel:
449 expandAddTPRel(MI, CB, Fixups, STI);
450 MCNumEmitted += 1;
451 return;
452 case RISCV::PseudoLongBEQ:
453 case RISCV::PseudoLongBNE:
454 case RISCV::PseudoLongBEQI:
455 case RISCV::PseudoLongBNEI:
456 case RISCV::PseudoLongBLT:
457 case RISCV::PseudoLongBGE:
458 case RISCV::PseudoLongBLTU:
459 case RISCV::PseudoLongBGEU:
460 expandLongCondBr(MI, CB, Fixups, STI);
461 MCNumEmitted += 2;
462 return;
463 case RISCV::PseudoLongQC_BEQI:
464 case RISCV::PseudoLongQC_BNEI:
465 case RISCV::PseudoLongQC_BLTI:
466 case RISCV::PseudoLongQC_BGEI:
467 case RISCV::PseudoLongQC_BLTUI:
468 case RISCV::PseudoLongQC_BGEUI:
469 expandQCLongCondBrImm(MI, CB, Fixups, STI, 4);
470 MCNumEmitted += 2;
471 return;
472 case RISCV::PseudoLongQC_E_BEQI:
473 case RISCV::PseudoLongQC_E_BNEI:
474 case RISCV::PseudoLongQC_E_BLTI:
475 case RISCV::PseudoLongQC_E_BGEI:
476 case RISCV::PseudoLongQC_E_BLTUI:
477 case RISCV::PseudoLongQC_E_BGEUI:
478 expandQCLongCondBrImm(MI, CB, Fixups, STI, 6);
479 MCNumEmitted += 2;
480 return;
481 case RISCV::PseudoTLSDESCCall:
482 expandTLSDESCCall(MI, CB, Fixups, STI);
483 MCNumEmitted += 1;
484 return;
485 }
486
487 switch (Size) {
488 default:
489 llvm_unreachable("Unhandled encodeInstruction length!");
490 case 2: {
491 uint16_t Bits = getBinaryCodeForInstr(MI, Fixups, STI);
493 break;
494 }
495 case 4: {
496 uint32_t Bits = getBinaryCodeForInstr(MI, Fixups, STI);
498 break;
499 }
500 case 6: {
501 uint64_t Bits = getBinaryCodeForInstr(MI, Fixups, STI) & 0xffff'ffff'ffffu;
502 SmallVector<char, 8> Encoding;
504 assert(Encoding[6] == 0 && Encoding[7] == 0 &&
505 "Unexpected encoding for 48-bit instruction");
506 Encoding.truncate(6);
507 CB.append(Encoding);
508 break;
509 }
510 case 8: {
511 uint64_t Bits = getBinaryCodeForInstr(MI, Fixups, STI);
513 break;
514 }
515 }
516
517 ++MCNumEmitted; // Keep track of the # of mi's emitted.
518}
519
520uint64_t
521RISCVMCCodeEmitter::getMachineOpValue(const MCInst &MI, const MCOperand &MO,
522 SmallVectorImpl<MCFixup> &Fixups,
523 const MCSubtargetInfo &STI) const {
524
525 if (MO.isReg())
526 return Ctx.getRegisterInfo()->getEncodingValue(MO.getReg());
527
528 if (MO.isImm())
529 return MO.getImm();
530
531 llvm_unreachable("Unhandled expression!");
532 return 0;
533}
534
535uint64_t
536RISCVMCCodeEmitter::getImmOpValueMinus1(const MCInst &MI, unsigned OpNo,
537 SmallVectorImpl<MCFixup> &Fixups,
538 const MCSubtargetInfo &STI) const {
539 const MCOperand &MO = MI.getOperand(OpNo);
540
541 if (MO.isImm()) {
542 uint64_t Res = MO.getImm();
543 return (Res - 1);
544 }
545
546 llvm_unreachable("Unhandled expression!");
547 return 0;
548}
549
550uint64_t
551RISCVMCCodeEmitter::getImmOpValueSlist(const MCInst &MI, unsigned OpNo,
552 SmallVectorImpl<MCFixup> &Fixups,
553 const MCSubtargetInfo &STI) const {
554 const MCOperand &MO = MI.getOperand(OpNo);
555 assert(MO.isImm() && "Slist operand must be immediate");
556
557 uint64_t Res = MO.getImm();
558 switch (Res) {
559 case 0:
560 return 0;
561 case 1:
562 return 1;
563 case 2:
564 return 2;
565 case 4:
566 return 3;
567 case 8:
568 return 4;
569 case 16:
570 return 5;
571 case 15:
572 return 6;
573 case 31:
574 return 7;
575 default:
576 llvm_unreachable("Unhandled Slist value!");
577 }
578}
579
580template <unsigned N>
581unsigned
582RISCVMCCodeEmitter::getImmOpValueAsrN(const MCInst &MI, unsigned OpNo,
583 SmallVectorImpl<MCFixup> &Fixups,
584 const MCSubtargetInfo &STI) const {
585 const MCOperand &MO = MI.getOperand(OpNo);
586
587 if (MO.isImm()) {
588 uint64_t Res = MO.getImm();
589 assert((Res & ((1 << N) - 1)) == 0 && "LSB is non-zero");
590 return Res >> N;
591 }
592
593 return getImmOpValue(MI, OpNo, Fixups, STI);
594}
595
596uint64_t
597RISCVMCCodeEmitter::getImmOpValueZibi(const MCInst &MI, unsigned OpNo,
598 SmallVectorImpl<MCFixup> &Fixups,
599 const MCSubtargetInfo &STI) const {
600 const MCOperand &MO = MI.getOperand(OpNo);
601 assert(MO.isImm() && "Zibi operand must be an immediate");
602 int64_t Res = MO.getImm();
603 if (Res == -1)
604 return 0;
605
606 return Res;
607}
608
609uint64_t RISCVMCCodeEmitter::getImmOpValue(const MCInst &MI, unsigned OpNo,
610 SmallVectorImpl<MCFixup> &Fixups,
611 const MCSubtargetInfo &STI) const {
612 bool EnableRelax = STI.hasFeature(RISCV::FeatureRelax);
613 const MCOperand &MO = MI.getOperand(OpNo);
614
615 MCInstrDesc const &Desc = MCII.get(MI.getOpcode());
616 unsigned MIFrm = RISCVII::getFormat(Desc.TSFlags);
617
618 // If the destination is an immediate, there is nothing to do.
619 if (MO.isImm())
620 return MO.getImm();
621
622 assert(MO.isExpr() &&
623 "getImmOpValue expects only expressions or immediates");
624 const MCExpr *Expr = MO.getExpr();
625 MCExpr::ExprKind Kind = Expr->getKind();
626
627 // `RelaxCandidate` must be set to `true` in two cases:
628 // - The fixup's relocation gets a R_RISCV_RELAX relocation
629 // - The underlying instruction may be relaxed to an instruction that gets a
630 // `R_RISCV_RELAX` relocation.
631 //
632 // The actual emission of `R_RISCV_RELAX` will be handled in
633 // `RISCVAsmBackend::applyFixup`.
634 bool RelaxCandidate = false;
635 auto AsmRelaxToLinkerRelaxable = [&]() -> void {
636 if (!STI.hasFeature(RISCV::FeatureExactAssembly))
637 RelaxCandidate = true;
638 };
639
641 if (Kind == MCExpr::Specifier) {
642 const auto *RVExpr = cast<MCSpecifierExpr>(Expr);
643 FixupKind = RVExpr->getSpecifier();
644 switch (RVExpr->getSpecifier()) {
645 default:
647 "invalid specifier");
648 break;
649 case ELF::R_RISCV_TPREL_ADD:
650 // tprel_add is only used to indicate that a relocation should be emitted
651 // for an add instruction used in TP-relative addressing. It should not be
652 // expanded as if representing an actual instruction operand and so to
653 // encounter it here is an error.
655 "ELF::R_RISCV_TPREL_ADD should not represent an instruction operand");
656 case RISCV::S_LO:
657 if (MIFrm == RISCVII::InstFormatI)
659 else if (MIFrm == RISCVII::InstFormatS)
661 else
662 llvm_unreachable("VK_LO used with unexpected instruction format");
663 RelaxCandidate = true;
664 break;
665 case ELF::R_RISCV_HI20:
667 RelaxCandidate = true;
668 break;
670 if (MIFrm == RISCVII::InstFormatI)
672 else if (MIFrm == RISCVII::InstFormatS)
674 else
675 llvm_unreachable("VK_PCREL_LO used with unexpected instruction format");
676 RelaxCandidate = true;
677 break;
680 RelaxCandidate = true;
681 break;
682 case RISCV::S_GOT_HI:
683 FixupKind = ELF::R_RISCV_GOT_HI20;
684 RelaxCandidate = true;
685 break;
687 if (MIFrm == RISCVII::InstFormatI)
688 FixupKind = ELF::R_RISCV_TPREL_LO12_I;
689 else if (MIFrm == RISCVII::InstFormatS)
690 FixupKind = ELF::R_RISCV_TPREL_LO12_S;
691 else
692 llvm_unreachable("VK_TPREL_LO used with unexpected instruction format");
693 RelaxCandidate = true;
694 break;
698 break;
699 }
701 RelaxCandidate = true;
702 break;
705 RelaxCandidate = true;
706 break;
707 case ELF::R_RISCV_GOT_HI20:
708 case ELF::R_RISCV_TPREL_HI20:
709 case ELF::R_RISCV_TLSDESC_HI20:
710 RelaxCandidate = true;
711 break;
712 }
713 } else if (Kind == MCExpr::SymbolRef || Kind == MCExpr::Binary) {
714 // FIXME: Sub kind binary exprs have chance of underflow.
715 if (MIFrm == RISCVII::InstFormatJ) {
717 RelaxCandidate = true;
718 } else if (MIFrm == RISCVII::InstFormatB) {
720 // Relaxes to B<cc>; JAL, with fixup_riscv_jal
721 AsmRelaxToLinkerRelaxable();
722 } else if (MIFrm == RISCVII::InstFormatCJ) {
724 // Relaxes to JAL with fixup_riscv_jal
725 AsmRelaxToLinkerRelaxable();
726 } else if (MIFrm == RISCVII::InstFormatCB) {
728 // Relaxes to B<cc>; JAL, with fixup_riscv_jal
729 AsmRelaxToLinkerRelaxable();
730 } else if (MIFrm == RISCVII::InstFormatCI) {
732 // Relaxes to `QC.E.LI` with fixup_riscv_qc_e_32
733 if (STI.hasFeature(RISCV::FeatureVendorXqcili))
734 AsmRelaxToLinkerRelaxable();
735 } else if (MIFrm == RISCVII::InstFormatI) {
737 } else if (MIFrm == RISCVII::InstFormatQC_EB) {
739 // Relaxes to QC.E.B<cc>I; JAL, with fixup_riscv_jal
740 AsmRelaxToLinkerRelaxable();
741 } else if (MIFrm == RISCVII::InstFormatQC_EAI) {
743 RelaxCandidate = true;
744 } else if (MIFrm == RISCVII::InstFormatQC_EJ) {
746 RelaxCandidate = true;
747 } else if (MIFrm == RISCVII::InstFormatNDS_BRANCH_10) {
749 }
750 }
751
752 assert(FixupKind != RISCV::fixup_riscv_invalid && "Unhandled expression!");
753
754 addFixup(Fixups, 0, Expr, FixupKind);
755 // If linker relaxation is enabled and supported by this relocation, set a bit
756 // so that the assembler knows the size of the instruction is not fixed/known,
757 // and the relocation will need a R_RISCV_RELAX relocation.
758 if (EnableRelax && RelaxCandidate)
759 Fixups.back().setLinkerRelaxable();
760 ++MCNumFixups;
761
762 return 0;
763}
764
765unsigned
766RISCVMCCodeEmitter::getYBNDSWImmOpValue(const MCInst &MI, unsigned OpNo,
767 SmallVectorImpl<MCFixup> &Fixups,
768 const MCSubtargetInfo &STI) const {
769 unsigned Imm = getImmOpValue(MI, OpNo, Fixups, STI);
770 assert(RISCV::isValidYBNDSWImm(Imm) && "Should have been checked before");
771 // YBNDSWI decodes to the requested length result as follows:
772 // If imm[8:0] == 0, result is 4096.
773 if (Imm == 4096)
774 return 0;
775 // If imm[8] == 0 and imm[7:0] != 0, result is imm[7:0] (1, 2, ..., 255).
776 if (Imm > 0 && Imm <= 255)
777 return Imm;
778 // If imm[8] == 1 and imm[7:5] == 0, result is
779 // `256 | (imm[3:0] << 4) | (imm[4] << 3)` (256, 264, ..., 504).
780 if (Imm >= 256 && Imm <= 504 && (Imm % 8) == 0) {
781 // Encode the multiples of 8 in this range in odd-even buckets, setting bit
782 // 4 of the immediate to 1 for odd multiples of 8.
783 unsigned MultipleOf8 = (Imm - 256) >> 3;
784 unsigned OddMultiple = MultipleOf8 & 1;
785 unsigned Bits3To0 = MultipleOf8 >> 1;
786 return 256 | (OddMultiple << 4) | Bits3To0;
787 }
788 // Otherwise, result is imm[7:0] << 4 (512, 528, ... 4080).
789 if (Imm >= 512 && Imm <= 4080 && (Imm % 16) == 0)
790 return 256 | (Imm >> 4);
791 llvm_unreachable("Invalid immediate for YBNDSWI");
792}
793
794unsigned RISCVMCCodeEmitter::getVMaskReg(const MCInst &MI, unsigned OpNo,
795 SmallVectorImpl<MCFixup> &Fixups,
796 const MCSubtargetInfo &STI) const {
797 MCOperand MO = MI.getOperand(OpNo);
798 assert(MO.isReg() && "Expected a register.");
799
800 switch (MO.getReg().id()) {
801 default:
802 llvm_unreachable("Invalid mask register.");
803 case RISCV::V0:
804 return 0;
805 case RISCV::NoRegister:
806 return 1;
807 }
808}
809
810unsigned RISCVMCCodeEmitter::getRlistOpValue(const MCInst &MI, unsigned OpNo,
811 SmallVectorImpl<MCFixup> &Fixups,
812 const MCSubtargetInfo &STI) const {
813 const MCOperand &MO = MI.getOperand(OpNo);
814 assert(MO.isImm() && "Rlist operand must be immediate");
815 auto Imm = MO.getImm();
816 assert(Imm >= 4 && "EABI is currently not implemented");
817 return Imm;
818}
819unsigned
820RISCVMCCodeEmitter::getRlistS0OpValue(const MCInst &MI, unsigned OpNo,
821 SmallVectorImpl<MCFixup> &Fixups,
822 const MCSubtargetInfo &STI) const {
823 const MCOperand &MO = MI.getOperand(OpNo);
824 assert(MO.isImm() && "Rlist operand must be immediate");
825 auto Imm = MO.getImm();
826 assert(Imm >= 4 && "EABI is currently not implemented");
827 assert(Imm != RISCVZC::RA && "Rlist operand must include s0");
828 return Imm;
829}
830
831#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:411
const Triple & getTargetTriple() const
Definition MCContext.h:397
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
static MCOperand createExpr(const MCExpr *Val)
Definition MCInst.h:166
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 Triple & getTargetTriple() 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().
bool isOSBinFormatMachO() const
Tests whether the environment is MachO.
Definition Triple.h:791
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)
bool isValidYBNDSWImm(int64_t Imm)
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.
@ Offset
Definition DWP.cpp:558
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:876
#define N