Skip to content

Commit 280efae

Browse files
committed
[RISCV] Software-guard direct calls in large code model
1 parent 8308d47 commit 280efae

File tree

5 files changed

+712
-124
lines changed

5 files changed

+712
-124
lines changed

llvm/lib/Target/RISCV/RISCVISelLowering.cpp

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19696,11 +19696,14 @@ SDValue RISCVTargetLowering::LowerCall(CallLoweringInfo &CLI,
1969619696
// If the callee is a GlobalAddress/ExternalSymbol node, turn it into a
1969719697
// TargetGlobalAddress/TargetExternalSymbol node so that legalize won't
1969819698
// split it and then direct call can be matched by PseudoCALL.
19699+
bool CalleeIsLargeExternalSymbol = false;
1969919700
if (getTargetMachine().getCodeModel() == CodeModel::Large) {
1970019701
if (auto *S = dyn_cast<GlobalAddressSDNode>(Callee))
1970119702
Callee = getLargeGlobalAddress(S, DL, PtrVT, DAG);
19702-
else if (auto *S = dyn_cast<ExternalSymbolSDNode>(Callee))
19703+
else if (auto *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
1970319704
Callee = getLargeExternalSymbol(S, DL, PtrVT, DAG);
19705+
CalleeIsLargeExternalSymbol = true;
19706+
}
1970419707
} else if (GlobalAddressSDNode *S = dyn_cast<GlobalAddressSDNode>(Callee)) {
1970519708
const GlobalValue *GV = S->getGlobal();
1970619709
Callee = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, RISCVII::MO_CALL);
@@ -19736,16 +19739,32 @@ SDValue RISCVTargetLowering::LowerCall(CallLoweringInfo &CLI,
1973619739
// Emit the call.
1973719740
SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
1973819741

19742+
// Use software guarded branch for large code model non-indirect calls
19743+
// Tail call to external symbol will have a null CLI.CB and we need another
19744+
// way to determine the callsite type
19745+
bool NeedSWGuarded = false;
19746+
if (getTargetMachine().getCodeModel() == CodeModel::Large &&
19747+
Subtarget.hasStdExtZicfilp() &&
19748+
((CLI.CB && !CLI.CB->isIndirectCall()) || CalleeIsLargeExternalSymbol))
19749+
NeedSWGuarded = true;
19750+
1973919751
if (IsTailCall) {
1974019752
MF.getFrameInfo().setHasTailCall();
19741-
SDValue Ret = DAG.getNode(RISCVISD::TAIL, DL, NodeTys, Ops);
19753+
SDValue Ret;
19754+
if (NeedSWGuarded)
19755+
Ret = DAG.getNode(RISCVISD::SW_GUARDED_TAIL, DL, NodeTys, Ops);
19756+
else
19757+
Ret = DAG.getNode(RISCVISD::TAIL, DL, NodeTys, Ops);
1974219758
if (CLI.CFIType)
1974319759
Ret.getNode()->setCFIType(CLI.CFIType->getZExtValue());
1974419760
DAG.addNoMergeSiteInfo(Ret.getNode(), CLI.NoMerge);
1974519761
return Ret;
1974619762
}
1974719763

19748-
Chain = DAG.getNode(RISCVISD::CALL, DL, NodeTys, Ops);
19764+
if (NeedSWGuarded)
19765+
Chain = DAG.getNode(RISCVISD::SW_GUARDED_CALL, DL, NodeTys, Ops);
19766+
else
19767+
Chain = DAG.getNode(RISCVISD::CALL, DL, NodeTys, Ops);
1974919768
if (CLI.CFIType)
1975019769
Chain.getNode()->setCFIType(CLI.CFIType->getZExtValue());
1975119770
DAG.addNoMergeSiteInfo(Chain.getNode(), CLI.NoMerge);
@@ -20193,6 +20212,8 @@ const char *RISCVTargetLowering::getTargetNodeName(unsigned Opcode) const {
2019320212
NODE_NAME_CASE(CZERO_EQZ)
2019420213
NODE_NAME_CASE(CZERO_NEZ)
2019520214
NODE_NAME_CASE(SW_GUARDED_BRIND)
20215+
NODE_NAME_CASE(SW_GUARDED_CALL)
20216+
NODE_NAME_CASE(SW_GUARDED_TAIL)
2019620217
NODE_NAME_CASE(TUPLE_INSERT)
2019720218
NODE_NAME_CASE(TUPLE_EXTRACT)
2019820219
NODE_NAME_CASE(SF_VC_XV_SE)

llvm/lib/Target/RISCV/RISCVISelLowering.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -411,9 +411,12 @@ enum NodeType : unsigned {
411411
CZERO_EQZ, // vt.maskc for XVentanaCondOps.
412412
CZERO_NEZ, // vt.maskcn for XVentanaCondOps.
413413

414-
/// Software guarded BRIND node. Operand 0 is the chain operand and
415-
/// operand 1 is the target address.
414+
// Software guarded BRIND node. Operand 0 is the chain operand and
415+
// operand 1 is the target address.
416416
SW_GUARDED_BRIND,
417+
// Software guarded calls for large code model
418+
SW_GUARDED_CALL,
419+
SW_GUARDED_TAIL,
417420

418421
SF_VC_XV_SE,
419422
SF_VC_IV_SE,

llvm/lib/Target/RISCV/RISCVInstrInfo.td

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ def callseq_end : SDNode<"ISD::CALLSEQ_END", SDT_CallSeqEnd,
5757
def riscv_call : SDNode<"RISCVISD::CALL", SDT_RISCVCall,
5858
[SDNPHasChain, SDNPOptInGlue, SDNPOutGlue,
5959
SDNPVariadic]>;
60+
def riscv_sw_guarded_call : SDNode<"RISCVISD::SW_GUARDED_CALL", SDT_RISCVCall,
61+
[SDNPHasChain, SDNPOptInGlue, SDNPOutGlue,
62+
SDNPVariadic]>;
6063
def riscv_ret_glue : SDNode<"RISCVISD::RET_GLUE", SDTNone,
6164
[SDNPHasChain, SDNPOptInGlue, SDNPVariadic]>;
6265
def riscv_sret_glue : SDNode<"RISCVISD::SRET_GLUE", SDTNone,
@@ -69,6 +72,9 @@ def riscv_brcc : SDNode<"RISCVISD::BR_CC", SDT_RISCVBrCC,
6972
def riscv_tail : SDNode<"RISCVISD::TAIL", SDT_RISCVCall,
7073
[SDNPHasChain, SDNPOptInGlue, SDNPOutGlue,
7174
SDNPVariadic]>;
75+
def riscv_sw_guarded_tail : SDNode<"RISCVISD::SW_GUARDED_TAIL", SDT_RISCVCall,
76+
[SDNPHasChain, SDNPOptInGlue, SDNPOutGlue,
77+
SDNPVariadic]>;
7278
def riscv_sw_guarded_brind : SDNode<"RISCVISD::SW_GUARDED_BRIND",
7379
SDTBrind, [SDNPHasChain]>;
7480
def riscv_sllw : SDNode<"RISCVISD::SLLW", SDT_RISCVIntBinOpW>;
@@ -1555,10 +1561,15 @@ let Predicates = [NoStdExtZicfilp] in
15551561
def PseudoCALLIndirect : Pseudo<(outs), (ins GPRJALR:$rs1),
15561562
[(riscv_call GPRJALR:$rs1)]>,
15571563
PseudoInstExpansion<(JALR X1, GPR:$rs1, 0)>;
1558-
let Predicates = [HasStdExtZicfilp] in
1564+
let Predicates = [HasStdExtZicfilp] in {
15591565
def PseudoCALLIndirectNonX7 : Pseudo<(outs), (ins GPRJALRNonX7:$rs1),
15601566
[(riscv_call GPRJALRNonX7:$rs1)]>,
15611567
PseudoInstExpansion<(JALR X1, GPR:$rs1, 0)>;
1568+
// For large code model, non-indirect calls could be software-guarded
1569+
def PseudoCALLIndirectX7 : Pseudo<(outs), (ins GPRX7:$rs1),
1570+
[(riscv_sw_guarded_call GPRX7:$rs1)]>,
1571+
PseudoInstExpansion<(JALR X1, GPR:$rs1, 0)>;
1572+
}
15621573
}
15631574

15641575
let isBarrier = 1, isReturn = 1, isTerminator = 1 in
@@ -1579,10 +1590,15 @@ let Predicates = [NoStdExtZicfilp] in
15791590
def PseudoTAILIndirect : Pseudo<(outs), (ins GPRTC:$rs1),
15801591
[(riscv_tail GPRTC:$rs1)]>,
15811592
PseudoInstExpansion<(JALR X0, GPR:$rs1, 0)>;
1582-
let Predicates = [HasStdExtZicfilp] in
1593+
let Predicates = [HasStdExtZicfilp] in {
15831594
def PseudoTAILIndirectNonX7 : Pseudo<(outs), (ins GPRTCNonX7:$rs1),
15841595
[(riscv_tail GPRTCNonX7:$rs1)]>,
15851596
PseudoInstExpansion<(JALR X0, GPR:$rs1, 0)>;
1597+
// For large code model, non-indirect calls could be software-guarded
1598+
def PseudoTAILIndirectX7 : Pseudo<(outs), (ins GPRX7:$rs1),
1599+
[(riscv_sw_guarded_tail GPRX7:$rs1)]>,
1600+
PseudoInstExpansion<(JALR X0, GPR:$rs1, 0)>;
1601+
}
15861602
}
15871603

15881604
def : Pat<(riscv_tail (iPTR tglobaladdr:$dst)),

0 commit comments

Comments
 (0)