Skip to content

Commit 46bc40e

Browse files
committed
Recommit "[AArch64] Lower calls with rv_marker attribute."
This recommits a87fccb with a fix to mark the destination operand of the marker instruction as def, to fix a machine verifier failure. This reverts the revert commit c0f2cea.
1 parent 4855a10 commit 46bc40e

File tree

6 files changed

+242
-2
lines changed

6 files changed

+242
-2
lines changed

llvm/lib/Target/AArch64/AArch64ExpandPseudoInsts.cpp

+44
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ class AArch64ExpandPseudo : public MachineFunctionPass {
8383
bool expandSVESpillFill(MachineBasicBlock &MBB,
8484
MachineBasicBlock::iterator MBBI, unsigned Opc,
8585
unsigned N);
86+
bool expandCALL_RVMARKER(MachineBasicBlock &MBB,
87+
MachineBasicBlock::iterator MBBI);
8688
};
8789

8890
} // end anonymous namespace
@@ -627,6 +629,46 @@ bool AArch64ExpandPseudo::expandSVESpillFill(MachineBasicBlock &MBB,
627629
return true;
628630
}
629631

632+
bool AArch64ExpandPseudo::expandCALL_RVMARKER(
633+
MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI) {
634+
// Expand CALL_RVMARKER pseudo to a branch, followed by the special `mov x29,
635+
// x29` marker. Mark the sequence as bundle, to avoid passes moving other code
636+
// in between.
637+
MachineInstr &MI = *MBBI;
638+
639+
MachineInstr *OriginalCall;
640+
MachineOperand &CallTarget = MI.getOperand(0);
641+
assert((CallTarget.isGlobal() || CallTarget.isReg()) &&
642+
"invalid operand for regular call");
643+
unsigned Opc = CallTarget.isGlobal() ? AArch64::BL : AArch64::BLR;
644+
OriginalCall = BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(Opc)).getInstr();
645+
OriginalCall->addOperand(CallTarget);
646+
647+
unsigned RegMaskStartIdx = 1;
648+
// Skip register arguments. Those are added during ISel, but are not
649+
// needed for the concrete branch.
650+
while (!MI.getOperand(RegMaskStartIdx).isRegMask()) {
651+
assert(MI.getOperand(RegMaskStartIdx).isReg() &&
652+
"should only skip register operands");
653+
RegMaskStartIdx++;
654+
}
655+
for (; RegMaskStartIdx < MI.getNumOperands(); ++RegMaskStartIdx)
656+
OriginalCall->addOperand(MI.getOperand(RegMaskStartIdx));
657+
658+
auto *Marker = BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(AArch64::ORRXrs))
659+
.addReg(AArch64::FP, RegState::Define)
660+
.addReg(AArch64::XZR)
661+
.addReg(AArch64::FP)
662+
.addImm(0)
663+
.getInstr();
664+
if (MI.shouldUpdateCallSiteInfo())
665+
MBB.getParent()->moveCallSiteInfo(&MI, Marker);
666+
MI.eraseFromParent();
667+
finalizeBundle(MBB, OriginalCall->getIterator(),
668+
std::next(Marker->getIterator()));
669+
return true;
670+
}
671+
630672
/// If MBBI references a pseudo instruction that should be expanded here,
631673
/// do the expansion and return true. Otherwise return false.
632674
bool AArch64ExpandPseudo::expandMI(MachineBasicBlock &MBB,
@@ -1014,6 +1056,8 @@ bool AArch64ExpandPseudo::expandMI(MachineBasicBlock &MBB,
10141056
return expandSVESpillFill(MBB, MBBI, AArch64::LDR_ZXI, 3);
10151057
case AArch64::LDR_ZZXI:
10161058
return expandSVESpillFill(MBB, MBBI, AArch64::LDR_ZXI, 2);
1059+
case AArch64::BLR_RVMARKER:
1060+
return expandCALL_RVMARKER(MBB, MBBI);
10171061
}
10181062
return false;
10191063
}

llvm/lib/Target/AArch64/AArch64ISelLowering.cpp

+11-1
Original file line numberDiff line numberDiff line change
@@ -1934,6 +1934,7 @@ const char *AArch64TargetLowering::getTargetNodeName(unsigned Opcode) const {
19341934
MAKE_CASE(AArch64ISD::INDEX_VECTOR)
19351935
MAKE_CASE(AArch64ISD::UABD)
19361936
MAKE_CASE(AArch64ISD::SABD)
1937+
MAKE_CASE(AArch64ISD::CALL_RVMARKER)
19371938
}
19381939
#undef MAKE_CASE
19391940
return nullptr;
@@ -5539,8 +5540,17 @@ AArch64TargetLowering::LowerCall(CallLoweringInfo &CLI,
55395540
return Ret;
55405541
}
55415542

5543+
unsigned CallOpc = AArch64ISD::CALL;
5544+
// Calls marked with "rv_marker" are special. They should be expanded to the
5545+
// call, directly followed by a special marker sequence. Use the CALL_RVMARKER
5546+
// to do that.
5547+
if (CLI.CB && CLI.CB->hasRetAttr("rv_marker")) {
5548+
assert(!IsTailCall && "tail calls cannot be marked with rv_marker");
5549+
CallOpc = AArch64ISD::CALL_RVMARKER;
5550+
}
5551+
55425552
// Returns a chain and a flag for retval copy to use.
5543-
Chain = DAG.getNode(AArch64ISD::CALL, DL, NodeTys, Ops);
5553+
Chain = DAG.getNode(CallOpc, DL, NodeTys, Ops);
55445554
DAG.addNoMergeSiteInfo(Chain.getNode(), CLI.NoMerge);
55455555
InFlag = Chain.getValue(1);
55465556
DAG.addCallSiteInfo(Chain.getNode(), std::move(CSInfo));

llvm/lib/Target/AArch64/AArch64ISelLowering.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,11 @@ enum NodeType : unsigned {
417417

418418
LDP,
419419
STP,
420-
STNP
420+
STNP,
421+
422+
// Pseudo for a OBJC call that gets emitted together with a special `mov
423+
// x29, x29` marker instruction.
424+
CALL_RVMARKER
421425
};
422426

423427
} // end namespace AArch64ISD

llvm/lib/Target/AArch64/AArch64InstrInfo.td

+12
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,12 @@ def AArch64call : SDNode<"AArch64ISD::CALL",
399399
SDTypeProfile<0, -1, [SDTCisPtrTy<0>]>,
400400
[SDNPHasChain, SDNPOptInGlue, SDNPOutGlue,
401401
SDNPVariadic]>;
402+
403+
def AArch64call_rvmarker: SDNode<"AArch64ISD::CALL_RVMARKER",
404+
SDTypeProfile<0, -1, [SDTCisPtrTy<0>]>,
405+
[SDNPHasChain, SDNPOptInGlue, SDNPOutGlue,
406+
SDNPVariadic]>;
407+
402408
def AArch64brcond : SDNode<"AArch64ISD::BRCOND", SDT_AArch64Brcond,
403409
[SDNPHasChain]>;
404410
def AArch64cbz : SDNode<"AArch64ISD::CBZ", SDT_AArch64cbz,
@@ -2089,6 +2095,8 @@ let isCall = 1, Defs = [LR], Uses = [SP] in {
20892095
def BLRNoIP : Pseudo<(outs), (ins GPR64noip:$Rn), []>,
20902096
Sched<[WriteBrReg]>,
20912097
PseudoInstExpansion<(BLR GPR64:$Rn)>;
2098+
def BLR_RVMARKER : Pseudo<(outs), (ins variable_ops), []>,
2099+
Sched<[WriteBrReg]>;
20922100
} // isCall
20932101

20942102
def : Pat<(AArch64call GPR64:$Rn),
@@ -2098,6 +2106,10 @@ def : Pat<(AArch64call GPR64noip:$Rn),
20982106
(BLRNoIP GPR64noip:$Rn)>,
20992107
Requires<[SLSBLRMitigation]>;
21002108

2109+
def : Pat<(AArch64call_rvmarker GPR64:$Rn),
2110+
(BLR_RVMARKER GPR64:$Rn)>,
2111+
Requires<[NoSLSBLRMitigation]>;
2112+
21012113
let isBranch = 1, isTerminator = 1, isBarrier = 1, isIndirectBranch = 1 in {
21022114
def BR : BranchReg<0b0000, "br", [(brind GPR64:$Rn)]>;
21032115
} // isBranch, isTerminator, isBarrier, isIndirectBranch
+149
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
; RUN: llc -o - %s | FileCheck --check-prefix=SELDAG --check-prefix=CHECK %s
2+
; RUN: llc -global-isel -o - %s | FileCheck --check-prefix=GISEL --check-prefix=CHECK %s
3+
4+
; TODO: support marker generation with GlobalISel
5+
target datalayout = "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128"
6+
target triple = "arm64-apple-iphoneos"
7+
8+
declare i8* @foo0(i32)
9+
declare i8* @foo1()
10+
11+
declare void @llvm.objc.release(i8*)
12+
declare void @objc_object(i8*)
13+
14+
declare void @foo2(i8*)
15+
16+
declare void @llvm.lifetime.start.p0i8(i64 immarg, i8* nocapture)
17+
18+
declare %struct.S* @_ZN1SD1Ev(%struct.S* nonnull dereferenceable(1))
19+
20+
declare void @llvm.lifetime.end.p0i8(i64 immarg, i8* nocapture)
21+
22+
23+
%struct.S = type { i8 }
24+
25+
@g = global i8* null, align 8
26+
@fptr = global i8* ()* null, align 8
27+
28+
define i8* @rv_marker_1() {
29+
; CHECK-LABEL: rv_marker_1:
30+
; CHECK: .cfi_offset w30, -16
31+
; CHECK-NEXT: bl foo1
32+
; SELDAG-NEXT: mov x29, x29
33+
; GISEL-NOT: mov x29, x29
34+
;
35+
entry:
36+
%call = call "rv_marker" i8* @foo1()
37+
ret i8* %call
38+
}
39+
40+
define void @rv_marker_2_select(i32 %c) {
41+
; CHECK-LABEL: rv_marker_2_select:
42+
; SELDAG: cinc w0, w8, eq
43+
; GISEL: csinc w0, w8, wzr, eq
44+
; CHECK-NEXT: bl foo0
45+
; SELDAG-NEXT: mov x29, x29
46+
; CHECK-NEXT: ldr x30, [sp], #16
47+
; CHECK-NEXT: b foo2
48+
;
49+
entry:
50+
%tobool.not = icmp eq i32 %c, 0
51+
%.sink = select i1 %tobool.not, i32 2, i32 1
52+
%call1 = call "rv_marker" i8* @foo0(i32 %.sink)
53+
tail call void @foo2(i8* %call1)
54+
ret void
55+
}
56+
57+
define void @rv_marker_3() personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*) {
58+
; CHECK-LABEL: rv_marker_3
59+
; CHECK: .cfi_offset w30, -32
60+
; CHECK-NEXT: bl foo1
61+
; SELDAG-NEXT: mov x29, x29
62+
;
63+
entry:
64+
%call = call "rv_marker" i8* @foo1()
65+
invoke void @objc_object(i8* %call) #5
66+
to label %invoke.cont unwind label %lpad
67+
68+
invoke.cont: ; preds = %entry
69+
tail call void @llvm.objc.release(i8* %call)
70+
ret void
71+
72+
lpad: ; preds = %entry
73+
%0 = landingpad { i8*, i32 }
74+
cleanup
75+
tail call void @llvm.objc.release(i8* %call)
76+
resume { i8*, i32 } %0
77+
}
78+
79+
define void @rv_marker_4() personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*) {
80+
; CHECK-LABEL: rv_marker_4
81+
; CHECK: .Ltmp3:
82+
; CHECK-NEXT: bl foo1
83+
; SELDAG-NEXT: mov x29, x29
84+
; CHECK-NEXT: .Ltmp4:
85+
;
86+
entry:
87+
%s = alloca %struct.S, align 1
88+
%0 = getelementptr inbounds %struct.S, %struct.S* %s, i64 0, i32 0
89+
call void @llvm.lifetime.start.p0i8(i64 1, i8* nonnull %0) #2
90+
%call = invoke "rv_marker" i8* @foo1()
91+
to label %invoke.cont unwind label %lpad
92+
93+
invoke.cont: ; preds = %entry
94+
invoke void @objc_object(i8* %call) #5
95+
to label %invoke.cont2 unwind label %lpad1
96+
97+
invoke.cont2: ; preds = %invoke.cont
98+
tail call void @llvm.objc.release(i8* %call)
99+
%call3 = call %struct.S* @_ZN1SD1Ev(%struct.S* nonnull dereferenceable(1) %s)
100+
call void @llvm.lifetime.end.p0i8(i64 1, i8* nonnull %0)
101+
ret void
102+
103+
lpad: ; preds = %entry
104+
%1 = landingpad { i8*, i32 }
105+
cleanup
106+
br label %ehcleanup
107+
108+
lpad1: ; preds = %invoke.cont
109+
%2 = landingpad { i8*, i32 }
110+
cleanup
111+
tail call void @llvm.objc.release(i8* %call)
112+
br label %ehcleanup
113+
114+
ehcleanup: ; preds = %lpad1, %lpad
115+
%.pn = phi { i8*, i32 } [ %2, %lpad1 ], [ %1, %lpad ]
116+
%call4 = call %struct.S* @_ZN1SD1Ev(%struct.S* nonnull dereferenceable(1) %s)
117+
call void @llvm.lifetime.end.p0i8(i64 1, i8* nonnull %0)
118+
resume { i8*, i32 } %.pn
119+
}
120+
121+
define i8* @rv_marker_5_indirect_call() {
122+
; CHECK-LABEL: rv_marker_5_indirect_call
123+
; CHECK: ldr [[ADDR:x[0-9]+]], [
124+
; CHECK-NEXT: blr [[ADDR]]
125+
; SLEDAG-NEXT: mov x29, x29
126+
; GISEL-NOT: mov x29, x29
127+
;
128+
entry:
129+
%0 = load i8* ()*, i8* ()** @fptr, align 8
130+
%call = call "rv_marker" i8* %0()
131+
tail call void @foo2(i8* %call)
132+
ret i8* %call
133+
}
134+
135+
declare void @foo(i64, i64, i64)
136+
137+
define void @rv_marker_multiarg(i64 %a, i64 %b, i64 %c) {
138+
; CHECK-LABEL: rv_marker_multiarg
139+
; CHECK: mov [[TMP:x[0-9]+]], x0
140+
; CHECK-NEXT: mov x0, x2
141+
; CHECK-NEXT: mov x2, [[TMP]]
142+
; CHECK-NEXT: bl foo
143+
; SELDAG-NEXT: mov x29, x29
144+
; GISEL-NOT: mov x29, x29
145+
call "rv_marker" void @foo(i64 %c, i64 %b, i64 %a)
146+
ret void
147+
}
148+
149+
declare i32 @__gxx_personality_v0(...)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# RUN: llc -run-pass=aarch64-expand-pseudo -mtriple=arm64-apple-ios -o - -emit-call-site-info %s | FileCheck %s
2+
3+
# CHECK-LABEL: test_1_callsite_info
4+
# CHECK: bb.0.entry:
5+
# CHECK-NEXT: BUNDLE implicit-def $lr, implicit-def $w30, implicit-def $sp, implicit-def $wsp, implicit-def dead $x0, implicit-def $fp, implicit-def $w29, implicit $x0, implicit $sp, implicit $xzr, implicit $fp {
6+
# CHECK-NEXT: BLR $x0, csr_aarch64_aapcs, implicit-def $lr, implicit $sp, implicit-def dead $lr, implicit $sp, implicit-def $sp, implicit-def dead $x0
7+
# CHECK-NEXT: ORRXrs $xzr, $fp, 0
8+
# CHECK-NEXT: }
9+
# CHECK-NEXT: RET undef $lr, implicit killed $w0
10+
---
11+
name: test_1_callsite_info
12+
callSites:
13+
- {bb: 0, offset: 0, fwdArgRegs:
14+
- { arg: 0, reg: '$x0' } }
15+
body: |
16+
bb.0.entry:
17+
liveins: $lr, $x0
18+
19+
BLR_RVMARKER $x0, csr_aarch64_aapcs, implicit-def dead $lr, implicit $sp, implicit-def $sp, implicit-def dead $x0
20+
RET_ReallyLR implicit killed $w0
21+
...

0 commit comments

Comments
 (0)