Skip to content

Commit 06689bb

Browse files
dianqkMingcongBai
authored andcommitted
[AArch64] ORRWrs is copy instruction when there's no implicit def of the X register (llvm#75184)
Follows llvm#74682 (comment). Fixes llvm#74680. (cherry picked from commit 7649d22)
1 parent b055ef8 commit 06689bb

File tree

6 files changed

+79
-8
lines changed

6 files changed

+79
-8
lines changed

llvm/include/llvm/CodeGen/TargetInstrInfo.h

+23
Original file line numberDiff line numberDiff line change
@@ -1019,6 +1019,11 @@ class TargetInstrInfo : public MCInstrInfo {
10191019
return std::nullopt;
10201020
}
10211021

1022+
virtual std::optional<DestSourcePair>
1023+
isCopyLikeInstrImpl(const MachineInstr &MI) const {
1024+
return std::nullopt;
1025+
}
1026+
10221027
/// Return true if the given terminator MI is not expected to spill. This
10231028
/// sets the live interval as not spillable and adjusts phi node lowering to
10241029
/// not introduce copies after the terminator. Use with care, these are
@@ -1044,6 +1049,24 @@ class TargetInstrInfo : public MCInstrInfo {
10441049
return isCopyInstrImpl(MI);
10451050
}
10461051

1052+
// Similar to `isCopyInstr`, but adds non-copy semantics on MIR, but
1053+
// ultimately generates a copy instruction.
1054+
std::optional<DestSourcePair> isCopyLikeInstr(const MachineInstr &MI) const {
1055+
if (auto IsCopyInstr = isCopyInstr(MI))
1056+
return IsCopyInstr;
1057+
return isCopyLikeInstrImpl(MI);
1058+
}
1059+
1060+
bool isFullCopyInstr(const MachineInstr &MI) const {
1061+
auto DestSrc = isCopyInstr(MI);
1062+
if (!DestSrc)
1063+
return false;
1064+
1065+
const MachineOperand *DestRegOp = DestSrc->Destination;
1066+
const MachineOperand *SrcRegOp = DestSrc->Source;
1067+
return !DestRegOp->getSubReg() && !SrcRegOp->getSubReg();
1068+
}
1069+
10471070
/// If the specific machine instruction is an instruction that adds an
10481071
/// immediate value and a physical register, and stores the result in
10491072
/// the given physical register \c Reg, return a pair of the source

llvm/lib/CodeGen/LiveDebugValues/InstrRefBasedImpl.cpp

+1-1
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

+2-2
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

+18-5
Original file line numberDiff line numberDiff line change
@@ -8269,19 +8269,32 @@ AArch64InstrInfo::isCopyInstrImpl(const MachineInstr &MI) const {
82698269
// and zero immediate operands used as an alias for mov instruction.
82708270
if (MI.getOpcode() == AArch64::ORRWrs &&
82718271
MI.getOperand(1).getReg() == AArch64::WZR &&
8272-
MI.getOperand(3).getImm() == 0x0) {
8272+
MI.getOperand(3).getImm() == 0x0 &&
8273+
// Check that the w->w move is not a zero-extending w->x mov.
8274+
(!MI.getOperand(0).getReg().isVirtual() ||
8275+
MI.getOperand(0).getSubReg() == 0) &&
8276+
(!MI.getOperand(0).getReg().isPhysical() ||
8277+
MI.findRegisterDefOperandIdx(MI.getOperand(0).getReg() - AArch64::W0 +
8278+
AArch64::X0) == -1))
82738279
return DestSourcePair{MI.getOperand(0), MI.getOperand(2)};
8274-
}
82758280

82768281
if (MI.getOpcode() == AArch64::ORRXrs &&
82778282
MI.getOperand(1).getReg() == AArch64::XZR &&
8278-
MI.getOperand(3).getImm() == 0x0) {
8283+
MI.getOperand(3).getImm() == 0x0)
82798284
return DestSourcePair{MI.getOperand(0), MI.getOperand(2)};
8280-
}
82818285

82828286
return std::nullopt;
82838287
}
82848288

8289+
std::optional<DestSourcePair>
8290+
AArch64InstrInfo::isCopyLikeInstrImpl(const MachineInstr &MI) const {
8291+
if (MI.getOpcode() == AArch64::ORRWrs &&
8292+
MI.getOperand(1).getReg() == AArch64::WZR &&
8293+
MI.getOperand(3).getImm() == 0x0)
8294+
return DestSourcePair{MI.getOperand(0), MI.getOperand(2)};
8295+
return std::nullopt;
8296+
}
8297+
82858298
std::optional<RegImmPair>
82868299
AArch64InstrInfo::isAddImmediate(const MachineInstr &MI, Register Reg) const {
82878300
int Sign = 1;
@@ -8325,7 +8338,7 @@ static std::optional<ParamLoadedValue>
83258338
describeORRLoadedValue(const MachineInstr &MI, Register DescribedReg,
83268339
const TargetInstrInfo *TII,
83278340
const TargetRegisterInfo *TRI) {
8328-
auto DestSrc = TII->isCopyInstr(MI);
8341+
auto DestSrc = TII->isCopyLikeInstr(MI);
83298342
if (!DestSrc)
83308343
return std::nullopt;
83318344

llvm/lib/Target/AArch64/AArch64InstrInfo.h

+2
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,8 @@ class AArch64InstrInfo final : public AArch64GenInstrInfo {
349349
/// registers as machine operands.
350350
std::optional<DestSourcePair>
351351
isCopyInstrImpl(const MachineInstr &MI) const override;
352+
std::optional<DestSourcePair>
353+
isCopyLikeInstrImpl(const MachineInstr &MI) const override;
352354

353355
private:
354356
unsigned getInstBundleLength(const MachineInstr &MI) const;
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)