Skip to content

Commit 621be9e

Browse files
author
Yeting Kuo
committed
[RISCV] Avoid using x7/t2 for indirect branches which need landing pad.
When Zicfilp enabled, this avoids selecting brind/call/tail to PseudoBRIND/CALLIndirect/TAILIndirect, since it may use X7 as its scratch register and be identified as a software guarded jump. There is an another PR #66762 to use software guarded jump for jumptable branch.
1 parent 8902f12 commit 621be9e

File tree

4 files changed

+83
-2
lines changed

4 files changed

+83
-2
lines changed

llvm/lib/Target/RISCV/RISCVFeatures.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -672,6 +672,8 @@ def FeatureStdExtZicfilp
672672
def HasStdExtZicfilp : Predicate<"Subtarget->hasStdExtZicfilp()">,
673673
AssemblerPredicate<(all_of FeatureStdExtZicfilp),
674674
"'Zicfilp' (Landing pad)">;
675+
def NoStdExtZicfilp : Predicate<"!Subtarget->hasStdExtZicfilp()">,
676+
AssemblerPredicate<(all_of (not FeatureStdExtZicfilp))>;
675677

676678
def FeatureStdExtZicond
677679
: SubtargetFeature<"experimental-zicond", "HasStdExtZicond", "true",

llvm/lib/Target/RISCV/RISCVInstrInfo.td

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1641,9 +1641,25 @@ let isBarrier = 1, isBranch = 1, isIndirectBranch = 1, isTerminator = 1 in
16411641
def PseudoBRIND : Pseudo<(outs), (ins GPRJALR:$rs1, simm12:$imm12), []>,
16421642
PseudoInstExpansion<(JALR X0, GPR:$rs1, simm12:$imm12)>;
16431643

1644+
let Predicates = [HasStdExtZicfilp],
1645+
isBarrier = 1, isBranch = 1, isIndirectBranch = 1, isTerminator = 1 in {
1646+
def PseudoBRINDNonX7 : Pseudo<(outs), (ins GPRJALRNonX7:$rs1, simm12:$imm12), []>,
1647+
PseudoInstExpansion<(JALR X0, GPR:$rs1, simm12:$imm12)>;
1648+
}
1649+
1650+
// For Zicfilp, need to avoid using X7/T2 for indirect branches which need
1651+
// landing pad.
1652+
let Predicates = [HasStdExtZicfilp] in {
1653+
def : Pat<(brind GPRJALRNonX7:$rs1), (PseudoBRINDNonX7 GPRJALRNonX7:$rs1, 0)>;
1654+
def : Pat<(brind (add GPRJALRNonX7:$rs1, simm12:$imm12)),
1655+
(PseudoBRINDNonX7 GPRJALRNonX7:$rs1, simm12:$imm12)>;
1656+
}
1657+
1658+
let Predicates = [NoStdExtZicfilp] in {
16441659
def : Pat<(brind GPRJALR:$rs1), (PseudoBRIND GPRJALR:$rs1, 0)>;
16451660
def : Pat<(brind (add GPRJALR:$rs1, simm12:$imm12)),
16461661
(PseudoBRIND GPRJALR:$rs1, simm12:$imm12)>;
1662+
}
16471663

16481664
// PseudoCALLReg is a generic pseudo instruction for calls which will eventually
16491665
// expand to auipc and jalr while encoding, with any given register used as the
@@ -1673,10 +1689,16 @@ def : Pat<(riscv_call texternalsym:$func), (PseudoCALL texternalsym:$func)>;
16731689
def : Pat<(riscv_sret_glue), (SRET (XLenVT X0), (XLenVT X0))>;
16741690
def : Pat<(riscv_mret_glue), (MRET (XLenVT X0), (XLenVT X0))>;
16751691

1676-
let isCall = 1, Defs = [X1] in
1692+
let isCall = 1, Defs = [X1] in {
1693+
let Predicates = [NoStdExtZicfilp] in
16771694
def PseudoCALLIndirect : Pseudo<(outs), (ins GPRJALR:$rs1),
16781695
[(riscv_call GPRJALR:$rs1)]>,
16791696
PseudoInstExpansion<(JALR X1, GPR:$rs1, 0)>;
1697+
let Predicates = [HasStdExtZicfilp] in
1698+
def PseudoCALLIndirectNonX7 : Pseudo<(outs), (ins GPRJALRNonX7:$rs1),
1699+
[(riscv_call GPRJALRNonX7:$rs1)]>,
1700+
PseudoInstExpansion<(JALR X1, GPR:$rs1, 0)>;
1701+
}
16801702

16811703
let isBarrier = 1, isReturn = 1, isTerminator = 1 in
16821704
def PseudoRET : Pseudo<(outs), (ins), [(riscv_ret_glue)]>,
@@ -1691,10 +1713,16 @@ def PseudoTAIL : Pseudo<(outs), (ins call_symbol:$dst), [],
16911713
"tail", "$dst">,
16921714
Sched<[WriteIALU, WriteJalr, ReadJalr]>;
16931715

1694-
let isCall = 1, isTerminator = 1, isReturn = 1, isBarrier = 1, Uses = [X2] in
1716+
let isCall = 1, isTerminator = 1, isReturn = 1, isBarrier = 1, Uses = [X2] in {
1717+
let Predicates = [NoStdExtZicfilp] in
16951718
def PseudoTAILIndirect : Pseudo<(outs), (ins GPRTC:$rs1),
16961719
[(riscv_tail GPRTC:$rs1)]>,
16971720
PseudoInstExpansion<(JALR X0, GPR:$rs1, 0)>;
1721+
let Predicates = [HasStdExtZicfilp] in
1722+
def PseudoTAILIndirectNonX7 : Pseudo<(outs), (ins GPRTCNonX7:$rs1),
1723+
[(riscv_tail GPRTCNonX7:$rs1)]>,
1724+
PseudoInstExpansion<(JALR X0, GPR:$rs1, 0)>;
1725+
}
16981726

16991727
def : Pat<(riscv_tail (iPTR tglobaladdr:$dst)),
17001728
(PseudoTAIL tglobaladdr:$dst)>;

llvm/lib/Target/RISCV/RISCVRegisterInfo.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,8 @@ def GPRNoX0X2 : GPRRegisterClass<(sub GPR, X0, X2)>;
148148
// by tablegen.
149149
def GPRJALR : GPRRegisterClass<(sub GPR, (sequence "X%u", 0, 5))>;
150150

151+
def GPRJALRNonX7 : GPRRegisterClass<(sub GPRJALR, X7)>;
152+
151153
def GPRC : GPRRegisterClass<(add (sequence "X%u", 10, 15),
152154
(sequence "X%u", 8, 9))>;
153155

@@ -158,6 +160,7 @@ def GPRC : GPRRegisterClass<(add (sequence "X%u", 10, 15),
158160
def GPRTC : GPRRegisterClass<(add (sequence "X%u", 6, 7),
159161
(sequence "X%u", 10, 17),
160162
(sequence "X%u", 28, 31))>;
163+
def GPRTCNonX7 : GPRRegisterClass<(sub GPRTC, X7)>;
161164

162165
def SP : GPRRegisterClass<(add X2)>;
163166

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py UTC_ARGS: --version 3
2+
; RUN: llc -mtriple=riscv64 -stop-after=finalize-isel < %s | FileCheck %s
3+
; RUN: llc -mtriple=riscv64 -mattr=+experimental-zicfilp -stop-after=finalize-isel < %s | FileCheck -check-prefixes=ZICFILP %s
4+
5+
@brind.arr = internal unnamed_addr constant [2 x ptr] [ptr blockaddress(@brind, %5), ptr blockaddress(@brind, %8)], align 8
6+
@x = dso_local global i32 0, align 4
7+
8+
define void @brind(i32 noundef signext %0) {
9+
; CHECK-LABEL: name: brind
10+
; CHECK: PseudoBRIND killed [[VAR:%.*]], 0
11+
; ZICFILP-LABEL: name: brind
12+
; ZICFILP: PseudoBRINDNonX7 killed [[VAR:%.*]], 0
13+
%2 = sext i32 %0 to i64
14+
%3 = getelementptr inbounds [2 x ptr], ptr @brind.arr, i64 0, i64 %2
15+
%4 = load ptr, ptr %3, align 8
16+
indirectbr ptr %4, [label %5, label %8]
17+
18+
5: ; preds = %1
19+
%6 = load i32, ptr @x, align 4
20+
%7 = add nsw i32 %6, 2
21+
store i32 %7, ptr @x, align 4
22+
br label %8
23+
24+
8: ; preds = %5, %1
25+
%9 = load i32, ptr @x, align 4
26+
%10 = add nsw i32 %9, 1
27+
store i32 %10, ptr @x, align 4
28+
ret void
29+
}
30+
31+
define i32 @indirect_call(ptr %0) {
32+
; CHECK-LABEL: name: indirect_call
33+
; CHECK: PseudoCALLIndirect
34+
; ZICFILP-LABEL: name: indirect_call
35+
; ZICFILP: PseudoCALLIndirectNonX7
36+
call void %0()
37+
ret i32 0
38+
}
39+
40+
41+
define void @indirect_tail(ptr %0) {
42+
; CHECK-LABEL: name: indirect_tail
43+
; CHECK: PseudoTAILIndirect
44+
; ZICFILP-LABEL: name: indirect_tail
45+
; ZICFILP: PseudoTAILIndirectNonX7
46+
tail call void %0()
47+
ret void
48+
}

0 commit comments

Comments
 (0)