Index: lib/Target/RISCV/MCTargetDesc/RISCVAsmBackend.cpp =================================================================== --- lib/Target/RISCV/MCTargetDesc/RISCVAsmBackend.cpp +++ lib/Target/RISCV/MCTargetDesc/RISCVAsmBackend.cpp @@ -74,7 +74,7 @@ } const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const override { - const static MCFixupKindInfo Infos[RISCV::NumTargetFixupKinds] = { + const static MCFixupKindInfo Infos[] = { // This table *must* be in the order that the fixup_* kinds are defined in // RISCVFixupKinds.h. // @@ -89,8 +89,11 @@ { "fixup_riscv_branch", 0, 32, MCFixupKindInfo::FKF_IsPCRel }, { "fixup_riscv_rvc_jump", 2, 11, MCFixupKindInfo::FKF_IsPCRel }, { "fixup_riscv_rvc_branch", 0, 16, MCFixupKindInfo::FKF_IsPCRel }, + { "fixup_riscv_call", 0, 64, MCFixupKindInfo::FKF_IsPCRel }, { "fixup_riscv_relax", 0, 0, 0 } }; + static_assert((array_lengthof(Infos)) == RISCV::NumTargetFixupKinds, + "Not all fixup kinds added to Infos array"); if (Kind < FirstTargetFixupKind) return MCAsmBackend::getFixupKindInfo(Kind); @@ -273,6 +276,11 @@ Value = (Sbit << 31) | (Mid6 << 25) | (Lo4 << 8) | (Hi1 << 7); return Value; } + case RISCV::fixup_riscv_call: { + uint64_t UpperImm = (Value + 0x800ULL) & 0xfffff000ULL; + uint64_t LowerImm = Value & 0xfffULL; + return UpperImm | ((LowerImm << 20) << 32); + } case RISCV::fixup_riscv_rvc_jump: { // Need to produce offset[11|4|9:8|10|6|7|3:1|5] from the 11-bit Value. unsigned Bit11 = (Value >> 11) & 0x1; Index: test/CodeGen/RISCV/function-call.ll =================================================================== --- /dev/null +++ test/CodeGen/RISCV/function-call.ll @@ -0,0 +1,21 @@ +; RUN: llc -mtriple=riscv32 -filetype=obj < %s\ +; RUN: | llvm-objdump -triple=riscv32 -mattr=-relax -d -riscv-no-aliases -\ +; RUN: | FileCheck -check-prefix=CHECK %s + +; Testing the function call offset could resovled by assembler +; when the function and callsite within the same compile unit +; and the linker relaxation is disabled. + +define i32 @foo(i32 %a, i32 %b) nounwind { + %1 = add i32 %a, %b + ret i32 %1 +} + +define i32 @bar(i32 %a) nounwind { +; CHECK-LABEL: bar: + %1 = add i32 %a, 5 + %2 = call i32 @foo(i32 %1, i32 %a) +; CHECK: auipc ra, 0 +; CHECK: jalr ra, ra, -24 + ret i32 %2 +} Index: test/MC/RISCV/function-call.s =================================================================== --- test/MC/RISCV/function-call.s +++ test/MC/RISCV/function-call.s @@ -11,12 +11,12 @@ # RELOC: R_RISCV_CALL foo 0x0 # INSTR: auipc ra, 0 # INSTR: jalr ra -# FIXUP: fixup A - offset: 0, value: foo, kind: +# FIXUP: fixup A - offset: 0, value: foo, kind: fixup_riscv_call call bar # RELOC: R_RISCV_CALL bar 0x0 # INSTR: auipc ra, 0 # INSTR: jalr ra -# FIXUP: fixup A - offset: 0, value: bar, kind: +# FIXUP: fixup A - offset: 0, value: bar, kind: fixup_riscv_call # Ensure that calls to functions whose names coincide with register names work. @@ -24,16 +24,16 @@ # RELOC: R_RISCV_CALL zero 0x0 # INSTR: auipc ra, 0 # INSTR: jalr ra -# FIXUP: fixup A - offset: 0, value: zero, kind: +# FIXUP: fixup A - offset: 0, value: zero, kind: fixup_riscv_call call f1 # RELOC: R_RISCV_CALL f1 0x0 # INSTR: auipc ra, 0 # INSTR: jalr ra -# FIXUP: fixup A - offset: 0, value: f1, kind: +# FIXUP: fixup A - offset: 0, value: f1, kind: fixup_riscv_call call ra # RELOC: R_RISCV_CALL ra 0x0 # INSTR: auipc ra, 0 # INSTR: jalr ra -# FIXUP: fixup A - offset: 0, value: ra, kind: +# FIXUP: fixup A - offset: 0, value: ra, kind: fixup_riscv_call Index: test/MC/RISCV/linker-relaxation.s =================================================================== --- test/MC/RISCV/linker-relaxation.s +++ test/MC/RISCV/linker-relaxation.s @@ -19,8 +19,8 @@ # NORELAX-RELOC-NOT: R_RISCV_RELAX # RELAX-RELOC: R_RISCV_CALL foo 0x0 # RELAX-RELOC: R_RISCV_RELAX foo 0x0 -# RELAX-FIXUP: fixup A - offset: 0, value: foo, kind: fixup_riscv_relax -# RELAX-FIXUP: fixup B - offset: 0, value: foo, kind: +# RELAX-FIXUP: fixup A - offset: 0, value: foo, kind: fixup_riscv_call +# RELAX-FIXUP: fixup B - offset: 0, value: foo, kind: fixup_riscv_relax beq s1, s1, .L1 # RELAX-RELOC: R_RISCV_BRANCH .L1 0x0 # RELAX-FIXUP: fixup A - offset: 0, value: .L1, kind: fixup_riscv_branch