Skip to content

Commit f1fcecf

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 to PseudoBRIND, since PseudoBRIND may uses X7 as rs1 and be identified as a software guarded jump. There is an another PR llvm#66762 to use software guarded jump for jumptable branch.
1 parent 8902f12 commit f1fcecf

File tree

4 files changed

+69
-0
lines changed

4 files changed

+69
-0
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: 16 additions & 0 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

llvm/lib/Target/RISCV/RISCVRegisterInfo.td

Lines changed: 2 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

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
@foo.arr = internal unnamed_addr constant [2 x ptr] [ptr blockaddress(@foo, %5), ptr blockaddress(@foo, %8)], align 8
6+
@x = dso_local global i32 0, align 4
7+
8+
define void @foo(i32 noundef signext %0) {
9+
; CHECK: PseudoBRIND killed [[VAR:%.*]], 0
10+
; ZICFILP: PseudoBRINDNonX7 killed [[VAR:%.*]], 0
11+
%2 = sext i32 %0 to i64
12+
%3 = getelementptr inbounds [2 x ptr], ptr @foo.arr, i64 0, i64 %2
13+
%4 = load ptr, ptr %3, align 8
14+
indirectbr ptr %4, [label %5, label %8]
15+
16+
5: ; preds = %1
17+
%6 = load i32, ptr @x, align 4
18+
%7 = add nsw i32 %6, 2
19+
store i32 %7, ptr @x, align 4
20+
br label %8
21+
22+
8: ; preds = %5, %1
23+
%9 = load i32, ptr @x, align 4
24+
%10 = add nsw i32 %9, 1
25+
store i32 %10, ptr @x, align 4
26+
ret void
27+
}
28+
29+
define void @bar(i32 noundef signext %0) {
30+
; CHECK: PseudoBRIND killed [[VAR:%.*]], 4
31+
; ZICFILP: PseudoBRINDNonX7 killed [[VAR:%.*]], 4
32+
%2 = sext i32 %0 to i64
33+
%3 = getelementptr inbounds [2 x ptr], ptr @foo.arr, i64 0, i64 %2
34+
%4 = load ptr, ptr %3, align 8
35+
%t = getelementptr inbounds i8, ptr @foo.arr, i64 4
36+
indirectbr ptr %t, [label %5, label %8]
37+
38+
5: ; preds = %1
39+
%6 = load i32, ptr @x, align 4
40+
%7 = add nsw i32 %6, 2
41+
store i32 %7, ptr @x, align 4
42+
br label %8
43+
44+
8: ; preds = %5, %1
45+
%9 = load i32, ptr @x, align 4
46+
%10 = add nsw i32 %9, 1
47+
store i32 %10, ptr @x, align 4
48+
ret void
49+
}

0 commit comments

Comments
 (0)