Skip to content

Commit 7649d22

Browse files
authored
[AArch64] ORRWrs is copy instruction when there's no implicit def of the X register (#75184)
Follows #74682 (comment). Fixes #74680.
1 parent b94a46b commit 7649d22

File tree

6 files changed

+69
-8
lines changed

6 files changed

+69
-8
lines changed

llvm/include/llvm/CodeGen/TargetInstrInfo.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1025,6 +1025,11 @@ class TargetInstrInfo : public MCInstrInfo {
10251025
return std::nullopt;
10261026
}
10271027

1028+
virtual std::optional<DestSourcePair>
1029+
isCopyLikeInstrImpl(const MachineInstr &MI) const {
1030+
return std::nullopt;
1031+
}
1032+
10281033
/// Return true if the given terminator MI is not expected to spill. This
10291034
/// sets the live interval as not spillable and adjusts phi node lowering to
10301035
/// not introduce copies after the terminator. Use with care, these are
@@ -1050,6 +1055,14 @@ class TargetInstrInfo : public MCInstrInfo {
10501055
return isCopyInstrImpl(MI);
10511056
}
10521057

1058+
// Similar to `isCopyInstr`, but adds non-copy semantics on MIR, but
1059+
// ultimately generates a copy instruction.
1060+
std::optional<DestSourcePair> isCopyLikeInstr(const MachineInstr &MI) const {
1061+
if (auto IsCopyInstr = isCopyInstr(MI))
1062+
return IsCopyInstr;
1063+
return isCopyLikeInstrImpl(MI);
1064+
}
1065+
10531066
bool isFullCopyInstr(const MachineInstr &MI) const {
10541067
auto DestSrc = isCopyInstr(MI);
10551068
if (!DestSrc)

llvm/lib/CodeGen/LiveDebugValues/InstrRefBasedImpl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2116,7 +2116,7 @@ bool InstrRefBasedLDV::transferSpillOrRestoreInst(MachineInstr &MI) {
21162116
}
21172117

21182118
bool InstrRefBasedLDV::transferRegisterCopy(MachineInstr &MI) {
2119-
auto DestSrc = TII->isCopyInstr(MI);
2119+
auto DestSrc = TII->isCopyLikeInstr(MI);
21202120
if (!DestSrc)
21212121
return false;
21222122

llvm/lib/CodeGen/LiveDebugValues/VarLocBasedImpl.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1364,7 +1364,7 @@ void VarLocBasedLDV::removeEntryValue(const MachineInstr &MI,
13641364
// TODO: Try to keep tracking of an entry value if we encounter a propagated
13651365
// DBG_VALUE describing the copy of the entry value. (Propagated entry value
13661366
// does not indicate the parameter modification.)
1367-
auto DestSrc = TII->isCopyInstr(*TransferInst);
1367+
auto DestSrc = TII->isCopyLikeInstr(*TransferInst);
13681368
if (DestSrc) {
13691369
const MachineOperand *SrcRegOp, *DestRegOp;
13701370
SrcRegOp = DestSrc->Source;
@@ -1840,7 +1840,7 @@ void VarLocBasedLDV::transferRegisterCopy(MachineInstr &MI,
18401840
OpenRangesSet &OpenRanges,
18411841
VarLocMap &VarLocIDs,
18421842
TransferMap &Transfers) {
1843-
auto DestSrc = TII->isCopyInstr(MI);
1843+
auto DestSrc = TII->isCopyLikeInstr(MI);
18441844
if (!DestSrc)
18451845
return;
18461846

llvm/lib/Target/AArch64/AArch64InstrInfo.cpp

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9185,19 +9185,32 @@ AArch64InstrInfo::isCopyInstrImpl(const MachineInstr &MI) const {
91859185
// and zero immediate operands used as an alias for mov instruction.
91869186
if (MI.getOpcode() == AArch64::ORRWrs &&
91879187
MI.getOperand(1).getReg() == AArch64::WZR &&
9188-
MI.getOperand(3).getImm() == 0x0) {
9188+
MI.getOperand(3).getImm() == 0x0 &&
9189+
// Check that the w->w move is not a zero-extending w->x mov.
9190+
(!MI.getOperand(0).getReg().isVirtual() ||
9191+
MI.getOperand(0).getSubReg() == 0) &&
9192+
(!MI.getOperand(0).getReg().isPhysical() ||
9193+
MI.findRegisterDefOperandIdx(MI.getOperand(0).getReg() - AArch64::W0 +
9194+
AArch64::X0) == -1))
91899195
return DestSourcePair{MI.getOperand(0), MI.getOperand(2)};
9190-
}
91919196

91929197
if (MI.getOpcode() == AArch64::ORRXrs &&
91939198
MI.getOperand(1).getReg() == AArch64::XZR &&
9194-
MI.getOperand(3).getImm() == 0x0) {
9199+
MI.getOperand(3).getImm() == 0x0)
91959200
return DestSourcePair{MI.getOperand(0), MI.getOperand(2)};
9196-
}
91979201

91989202
return std::nullopt;
91999203
}
92009204

9205+
std::optional<DestSourcePair>
9206+
AArch64InstrInfo::isCopyLikeInstrImpl(const MachineInstr &MI) const {
9207+
if (MI.getOpcode() == AArch64::ORRWrs &&
9208+
MI.getOperand(1).getReg() == AArch64::WZR &&
9209+
MI.getOperand(3).getImm() == 0x0)
9210+
return DestSourcePair{MI.getOperand(0), MI.getOperand(2)};
9211+
return std::nullopt;
9212+
}
9213+
92019214
std::optional<RegImmPair>
92029215
AArch64InstrInfo::isAddImmediate(const MachineInstr &MI, Register Reg) const {
92039216
int Sign = 1;
@@ -9241,7 +9254,7 @@ static std::optional<ParamLoadedValue>
92419254
describeORRLoadedValue(const MachineInstr &MI, Register DescribedReg,
92429255
const TargetInstrInfo *TII,
92439256
const TargetRegisterInfo *TRI) {
9244-
auto DestSrc = TII->isCopyInstr(MI);
9257+
auto DestSrc = TII->isCopyLikeInstr(MI);
92459258
if (!DestSrc)
92469259
return std::nullopt;
92479260

llvm/lib/Target/AArch64/AArch64InstrInfo.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,8 @@ class AArch64InstrInfo final : public AArch64GenInstrInfo {
401401
/// registers as machine operands.
402402
std::optional<DestSourcePair>
403403
isCopyInstrImpl(const MachineInstr &MI) const override;
404+
std::optional<DestSourcePair>
405+
isCopyLikeInstrImpl(const MachineInstr &MI) const override;
404406

405407
private:
406408
unsigned getInstBundleLength(const MachineInstr &MI) const;
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py UTC_ARGS: --version 4
2+
# RUN: llc -o - %s --run-pass=machine-cp -mcp-use-is-copy-instr -mtriple=arm64-apple-macos --verify-machineinstrs | FileCheck %s
3+
4+
---
5+
name: test
6+
tracksRegLiveness: true
7+
body: |
8+
; CHECK-LABEL: name: test
9+
; CHECK: bb.0:
10+
; CHECK-NEXT: successors: %bb.1(0x80000000)
11+
; CHECK-NEXT: liveins: $w0
12+
; CHECK-NEXT: {{ $}}
13+
; CHECK-NEXT: $x8 = ORRXrs $xzr, $x0, 0, implicit $w0
14+
; CHECK-NEXT: $w8 = ORRWrs $wzr, $w0, 0, implicit-def $x8
15+
; CHECK-NEXT: {{ $}}
16+
; CHECK-NEXT: bb.1:
17+
; CHECK-NEXT: liveins: $x8
18+
; CHECK-NEXT: {{ $}}
19+
; CHECK-NEXT: $x0 = ADDXri $x8, 1, 0
20+
; CHECK-NEXT: RET undef $lr, implicit $x0
21+
bb.0:
22+
successors: %bb.1(0x80000000)
23+
liveins: $w0
24+
25+
$x8 = ORRXrs $xzr, $x0, 0, implicit $w0
26+
$w8 = ORRWrs $wzr, $w0, 0, implicit-def $x8
27+
28+
bb.1:
29+
liveins: $x8
30+
$x0 = ADDXri $x8, 1, 0
31+
32+
RET undef $lr, implicit $x0
33+
...

0 commit comments

Comments
 (0)