diff --git a/llvm/lib/Target/RISCV/RISCVFrameLowering.cpp b/llvm/lib/Target/RISCV/RISCVFrameLowering.cpp --- a/llvm/lib/Target/RISCV/RISCVFrameLowering.cpp +++ b/llvm/lib/Target/RISCV/RISCVFrameLowering.cpp @@ -52,18 +52,18 @@ bool IsRV64 = STI.hasFeature(RISCV::Feature64Bit); int64_t SlotSize = STI.getXLen() / 8; // Store return address to shadow call stack - // s[w|d] ra, 0(gp) // addi gp, gp, [4|8] - BuildMI(MBB, MI, DL, TII->get(IsRV64 ? RISCV::SD : RISCV::SW)) - .addReg(RAReg) - .addReg(SCSPReg) - .addImm(0) - .setMIFlag(MachineInstr::FrameSetup); + // s[w|d] ra, -[4|8](gp) BuildMI(MBB, MI, DL, TII->get(RISCV::ADDI)) .addReg(SCSPReg, RegState::Define) .addReg(SCSPReg) .addImm(SlotSize) .setMIFlag(MachineInstr::FrameSetup); + BuildMI(MBB, MI, DL, TII->get(IsRV64 ? RISCV::SD : RISCV::SW)) + .addReg(RAReg) + .addReg(SCSPReg) + .addImm(-SlotSize) + .setMIFlag(MachineInstr::FrameSetup); // Emit a CFI instruction that causes SlotSize to be subtracted from the value // of the shadow stack pointer when unwinding past this frame. diff --git a/llvm/test/CodeGen/RISCV/shadowcallstack.ll b/llvm/test/CodeGen/RISCV/shadowcallstack.ll --- a/llvm/test/CodeGen/RISCV/shadowcallstack.ll +++ b/llvm/test/CodeGen/RISCV/shadowcallstack.ll @@ -34,8 +34,8 @@ define i32 @f3() shadowcallstack { ; RV32-LABEL: f3: ; RV32: # %bb.0: -; RV32-NEXT: sw ra, 0(gp) ; RV32-NEXT: addi gp, gp, 4 +; RV32-NEXT: sw ra, -4(gp) ; RV32-NEXT: .cfi_escape 0x16, 0x03, 0x02, 0x73, 0x7c # ; RV32-NEXT: addi sp, sp, -16 ; RV32-NEXT: .cfi_def_cfa_offset 16 @@ -51,8 +51,8 @@ ; ; RV64-LABEL: f3: ; RV64: # %bb.0: -; RV64-NEXT: sd ra, 0(gp) ; RV64-NEXT: addi gp, gp, 8 +; RV64-NEXT: sd ra, -8(gp) ; RV64-NEXT: .cfi_escape 0x16, 0x03, 0x02, 0x73, 0x78 # ; RV64-NEXT: addi sp, sp, -16 ; RV64-NEXT: .cfi_def_cfa_offset 16 @@ -73,8 +73,8 @@ define i32 @f4() shadowcallstack { ; RV32-LABEL: f4: ; RV32: # %bb.0: -; RV32-NEXT: sw ra, 0(gp) ; RV32-NEXT: addi gp, gp, 4 +; RV32-NEXT: sw ra, -4(gp) ; RV32-NEXT: .cfi_escape 0x16, 0x03, 0x02, 0x73, 0x7c # ; RV32-NEXT: addi sp, sp, -16 ; RV32-NEXT: .cfi_def_cfa_offset 16 @@ -108,8 +108,8 @@ ; ; RV64-LABEL: f4: ; RV64: # %bb.0: -; RV64-NEXT: sd ra, 0(gp) ; RV64-NEXT: addi gp, gp, 8 +; RV64-NEXT: sd ra, -8(gp) ; RV64-NEXT: .cfi_escape 0x16, 0x03, 0x02, 0x73, 0x78 # ; RV64-NEXT: addi sp, sp, -32 ; RV64-NEXT: .cfi_def_cfa_offset 32 @@ -153,8 +153,8 @@ define i32 @f5() shadowcallstack nounwind { ; RV32-LABEL: f5: ; RV32: # %bb.0: -; RV32-NEXT: sw ra, 0(gp) ; RV32-NEXT: addi gp, gp, 4 +; RV32-NEXT: sw ra, -4(gp) ; RV32-NEXT: addi sp, sp, -16 ; RV32-NEXT: sw ra, 12(sp) # 4-byte Folded Spill ; RV32-NEXT: call bar@plt @@ -166,8 +166,8 @@ ; ; RV64-LABEL: f5: ; RV64: # %bb.0: -; RV64-NEXT: sd ra, 0(gp) ; RV64-NEXT: addi gp, gp, 8 +; RV64-NEXT: sd ra, -8(gp) ; RV64-NEXT: addi sp, sp, -16 ; RV64-NEXT: sd ra, 8(sp) # 8-byte Folded Spill ; RV64-NEXT: call bar@plt