@@ -576,8 +576,21 @@ uint64_t RISCVMCCodeEmitter::getImmOpValue(const MCInst &MI, unsigned OpNo,
576576 " getImmOpValue expects only expressions or immediates" );
577577 const MCExpr *Expr = MO.getExpr ();
578578 MCExpr::ExprKind Kind = Expr->getKind ();
579- unsigned FixupKind = RISCV::fixup_riscv_invalid;
579+
580+ // `RelaxCandidate` must be set to `true` in two cases:
581+ // - The fixup's relocation gets a R_RISCV_RELAX relocation
582+ // - The underlying instruction may be relaxed to an instruction that gets a
583+ // `R_RISCV_RELAX` relocation.
584+ //
585+ // The actual emission of `R_RISCV_RELAX` will be handled in
586+ // `RISCVAsmBackend::applyFixup`.
580587 bool RelaxCandidate = false ;
588+ auto AsmRelaxToLinkerRelaxableWithFeature = [&](unsigned Feature) -> void {
589+ if (!STI.hasFeature (RISCV::FeatureExactAssembly) && STI.hasFeature (Feature))
590+ RelaxCandidate = true ;
591+ };
592+
593+ unsigned FixupKind = RISCV::fixup_riscv_invalid;
581594 if (Kind == MCExpr::Specifier) {
582595 const auto *RVExpr = cast<MCSpecifierExpr>(Expr);
583596 FixupKind = RVExpr->getSpecifier ();
@@ -644,18 +657,26 @@ uint64_t RISCVMCCodeEmitter::getImmOpValue(const MCInst &MI, unsigned OpNo,
644657 // FIXME: Sub kind binary exprs have chance of underflow.
645658 if (MIFrm == RISCVII::InstFormatJ) {
646659 FixupKind = RISCV::fixup_riscv_jal;
660+ AsmRelaxToLinkerRelaxableWithFeature (RISCV::FeatureVendorXqcilb);
647661 } else if (MIFrm == RISCVII::InstFormatB) {
648662 FixupKind = RISCV::fixup_riscv_branch;
663+ // This might be assembler relaxed to `b<cc>; jal` but we cannot relax
664+ // the `jal` again in the assembler.
649665 } else if (MIFrm == RISCVII::InstFormatCJ) {
650666 FixupKind = RISCV::fixup_riscv_rvc_jump;
667+ AsmRelaxToLinkerRelaxableWithFeature (RISCV::FeatureVendorXqcilb);
651668 } else if (MIFrm == RISCVII::InstFormatCB) {
652669 FixupKind = RISCV::fixup_riscv_rvc_branch;
670+ // This might be assembler relaxed to `b<cc>; jal` but we cannot relax
671+ // the `jal` again in the assembler.
653672 } else if (MIFrm == RISCVII::InstFormatCI) {
654673 FixupKind = RISCV::fixup_riscv_rvc_imm;
655674 } else if (MIFrm == RISCVII::InstFormatI) {
656675 FixupKind = RISCV::fixup_riscv_12_i;
657676 } else if (MIFrm == RISCVII::InstFormatQC_EB) {
658677 FixupKind = RISCV::fixup_riscv_qc_e_branch;
678+ // This might be assembler relaxed to `qc.e.b<cc>; jal` but we cannot
679+ // relax the `jal` again in the assembler.
659680 } else if (MIFrm == RISCVII::InstFormatQC_EAI) {
660681 FixupKind = RISCV::fixup_riscv_qc_e_32;
661682 RelaxCandidate = true ;
@@ -670,9 +691,9 @@ uint64_t RISCVMCCodeEmitter::getImmOpValue(const MCInst &MI, unsigned OpNo,
670691 assert (FixupKind != RISCV::fixup_riscv_invalid && " Unhandled expression!" );
671692
672693 addFixup (Fixups, 0 , Expr, FixupKind);
673- // If linker relaxation is enabled and supported by this relocation, set
674- // a bit so that if fixup is unresolved, a R_RISCV_RELAX relocation will be
675- // appended .
694+ // If linker relaxation is enabled and supported by this relocation, set a bit
695+ // so that the assembler knows the size of the instruction is not fixed/known,
696+ // and the relocation will need a R_RISCV_RELAX relocation .
676697 if (EnableRelax && RelaxCandidate)
677698 Fixups.back ().setLinkerRelaxable ();
678699 ++MCNumFixups;
0 commit comments