Skip to content

Commit 890f872

Browse files
authored
[RISCV] Use t3 for static chain register when branch CFI is enabled (#142344)
Use t3 for static chain register when branch CFI is enabled to align with gcc.[1] [1] https://github.com/gcc-mirror/gcc/blob/master/gcc/config/riscv/riscv.h#L417
1 parent 0210750 commit 890f872

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

llvm/lib/Target/RISCV/RISCVCallingConv.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "RISCVCallingConv.h"
1414
#include "RISCVSubtarget.h"
1515
#include "llvm/IR/DataLayout.h"
16+
#include "llvm/IR/Module.h"
1617
#include "llvm/MC/MCRegister.h"
1718

1819
using namespace llvm;
@@ -333,9 +334,15 @@ bool llvm::CC_RISCV(unsigned ValNo, MVT ValVT, MVT LocVT,
333334
MVT XLenVT = Subtarget.getXLenVT();
334335

335336
// Static chain parameter must not be passed in normal argument registers,
336-
// so we assign t2 for it as done in GCC's __builtin_call_with_static_chain
337+
// so we assign t2/t3 for it as done in GCC's __builtin_call_with_static_chain
338+
bool HasCFBranch =
339+
Subtarget.hasStdExtZicfilp() &&
340+
MF.getFunction().getParent()->getModuleFlag("cf-protection-branch");
341+
// Normal: t2, Branch control flow protection: t3
342+
const auto StaticChainReg = HasCFBranch ? RISCV::X28 : RISCV::X7;
343+
337344
if (ArgFlags.isNest()) {
338-
if (MCRegister Reg = State.AllocateReg(RISCV::X7)) {
345+
if (MCRegister Reg = State.AllocateReg(StaticChainReg)) {
339346
State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
340347
return false;
341348
}

llvm/test/CodeGen/RISCV/nest-register.ll

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
; RUN: | FileCheck -check-prefix=RV32I %s
44
; RUN: llc -mtriple=riscv64 -verify-machineinstrs < %s \
55
; RUN: | FileCheck -check-prefix=RV64I %s
6+
; RUN: llc -mtriple=riscv64 -mattr=+experimental-zicfilp -verify-machineinstrs < %s \
7+
; RUN: | FileCheck -check-prefix=RV64I-ZICFILP %s
68

79
; Tests that the 'nest' parameter attribute causes the relevant parameter to be
810
; passed in the right register.
@@ -17,6 +19,12 @@ define ptr @nest_receiver(ptr nest %arg) nounwind {
1719
; RV64I: # %bb.0:
1820
; RV64I-NEXT: mv a0, t2
1921
; RV64I-NEXT: ret
22+
;
23+
; RV64I-ZICFILP-LABEL: nest_receiver:
24+
; RV64I-ZICFILP: # %bb.0:
25+
; RV64I-ZICFILP-NEXT: lpad 0
26+
; RV64I-ZICFILP-NEXT: mv a0, t3
27+
; RV64I-ZICFILP-NEXT: ret
2028
ret ptr %arg
2129
}
2230

@@ -40,6 +48,21 @@ define ptr @nest_caller(ptr %arg) nounwind {
4048
; RV64I-NEXT: ld ra, 8(sp) # 8-byte Folded Reload
4149
; RV64I-NEXT: addi sp, sp, 16
4250
; RV64I-NEXT: ret
51+
;
52+
; RV64I-ZICFILP-LABEL: nest_caller:
53+
; RV64I-ZICFILP: # %bb.0:
54+
; RV64I-ZICFILP-NEXT: lpad 0
55+
; RV64I-ZICFILP-NEXT: addi sp, sp, -16
56+
; RV64I-ZICFILP-NEXT: sd ra, 8(sp) # 8-byte Folded Spill
57+
; RV64I-ZICFILP-NEXT: mv t3, a0
58+
; RV64I-ZICFILP-NEXT: call nest_receiver
59+
; RV64I-ZICFILP-NEXT: ld ra, 8(sp) # 8-byte Folded Reload
60+
; RV64I-ZICFILP-NEXT: addi sp, sp, 16
61+
; RV64I-ZICFILP-NEXT: ret
4362
%result = call ptr @nest_receiver(ptr nest %arg)
4463
ret ptr %result
4564
}
65+
66+
!llvm.module.flags = !{!0}
67+
68+
!0 = !{i32 8, !"cf-protection-branch", i32 1}

0 commit comments

Comments
 (0)