Skip to content

Reland "[llvm][AArch64] Copy all operands when expanding BLR_BTI bundle (#78267)" #78719

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion llvm/lib/Target/AArch64/AArch64ExpandPseudoInsts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -843,8 +843,24 @@ bool AArch64ExpandPseudo::expandCALL_BTI(MachineBasicBlock &MBB,
MachineInstr *Call =
BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(Opc)).getInstr();
Call->addOperand(CallTarget);

// 1 because we already added the branch target above.
unsigned RegMaskStartIdx = 1;
// The branch is BL <target>, so we cannot attach the arguments of the called
// function to it. Those must be added as implicitly used by the branch.
while (!MI.getOperand(RegMaskStartIdx).isRegMask()) {
auto MOP = MI.getOperand(RegMaskStartIdx);
assert(MOP.isReg() && "can only add register operands");
Call->addOperand(MachineOperand::CreateReg(
MOP.getReg(), /*Def=*/false, /*Implicit=*/true, /*isKill=*/false,
/*isDead=*/false, /*isUndef=*/MOP.isUndef()));
RegMaskStartIdx++;
}
for (const MachineOperand &MO :
llvm::drop_begin(MI.operands(), RegMaskStartIdx))
Call->addOperand(MO);

Call->setCFIType(*MBB.getParent(), MI.getCFIType());
Call->copyImplicitOps(*MBB.getParent(), MI);

MachineInstr *BTI =
BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(AArch64::HINT))
Expand Down
28 changes: 28 additions & 0 deletions llvm/test/CodeGen/AArch64/blr-bti-preserves-operands.mir
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# RUN: llc -mtriple=aarch64-none-linux-gnu -run-pass=aarch64-expand-pseudo -o - %s | FileCheck %s

# When expanding a BLR_BTI, we should copy all the operands to the branch in the
# bundle. Otherwise we could end up using a register after the BL which was
# clobbered by the function that was called, or overwriting an argument to that
# function before we take the branch.
#
# The arguments to the call must become implicit arguments, because the branch
# only expects to get 1 explicit operand which is the branch target.

# CHECK: BUNDLE implicit-def $lr, implicit-def $w30, implicit-def $sp, implicit-def $wsp, implicit $sp, implicit $x0, implicit $w1 {
# CHECK: BL @_setjmp, csr_aarch64_aapcs, implicit-def $lr, implicit $sp, implicit $x0, implicit $w1, implicit-def dead $lr, implicit $sp, implicit-def $sp
# CHECK: HINT 36
# CHECK: }

--- |
define void @a() {
ret void
}

declare void @_setjmp(...)
...
---
name: a
body: |
bb.0:
BLR_BTI @_setjmp, $x0, $w1, csr_aarch64_aapcs, implicit-def dead $lr, implicit $sp, implicit-def $sp
...
23 changes: 0 additions & 23 deletions llvm/test/CodeGen/AArch64/blr-bti-preserves-regmask.mir

This file was deleted.