Skip to content

Commit f4ca6d9

Browse files
authored
AArch64: Skip analyzeCompare for subregister uses (#141654)
The API is broken and doesn't provide a way to report the used subregister, so it's unsafe to use. This will produce illegal folds if the subregister is silently dropped.
1 parent 3a51896 commit f4ca6d9

File tree

2 files changed

+77
-1
lines changed

2 files changed

+77
-1
lines changed

llvm/lib/Target/AArch64/AArch64InstrInfo.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1281,8 +1281,9 @@ bool AArch64InstrInfo::analyzeCompare(const MachineInstr &MI, Register &SrcReg,
12811281
int64_t &CmpValue) const {
12821282
// The first operand can be a frame index where we'd normally expect a
12831283
// register.
1284+
// FIXME: Pass subregisters out of analyzeCompare
12841285
assert(MI.getNumOperands() >= 2 && "All AArch64 cmps should have 2 operands");
1285-
if (!MI.getOperand(1).isReg())
1286+
if (!MI.getOperand(1).isReg() || MI.getOperand(1).getSubReg())
12861287
return false;
12871288

12881289
switch (MI.getOpcode()) {
@@ -1292,6 +1293,9 @@ bool AArch64InstrInfo::analyzeCompare(const MachineInstr &MI, Register &SrcReg,
12921293
case AArch64::PTEST_PP_ANY:
12931294
SrcReg = MI.getOperand(0).getReg();
12941295
SrcReg2 = MI.getOperand(1).getReg();
1296+
if (MI.getOperand(2).getSubReg())
1297+
return false;
1298+
12951299
// Not sure about the mask and value for now...
12961300
CmpMask = ~0;
12971301
CmpValue = 0;
@@ -1311,6 +1315,11 @@ bool AArch64InstrInfo::analyzeCompare(const MachineInstr &MI, Register &SrcReg,
13111315
// Replace SUBSWrr with SUBWrr if NZCV is not used.
13121316
SrcReg = MI.getOperand(1).getReg();
13131317
SrcReg2 = MI.getOperand(2).getReg();
1318+
1319+
// FIXME: Pass subregisters out of analyzeCompare
1320+
if (MI.getOperand(2).getSubReg())
1321+
return false;
1322+
13141323
CmpMask = ~0;
13151324
CmpValue = 0;
13161325
return true;
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py UTC_ARGS: --version 5
2+
# RUN: llc -mtriple=aarch64-- -run-pass=peephole-opt -o - %s | FileCheck %s
3+
4+
# Make sure that analyzeCompare doesn't produce illegal folds due to
5+
# ignoring the subregister index on the use operands.
6+
7+
---
8+
name: analyze_compare_subreg_use_lhs
9+
tracksRegLiveness: true
10+
body: |
11+
bb.0:
12+
liveins: $x0, $x1
13+
14+
; CHECK-LABEL: name: analyze_compare_subreg_use_lhs
15+
; CHECK: liveins: $x0, $x1
16+
; CHECK-NEXT: {{ $}}
17+
; CHECK-NEXT: [[COPY:%[0-9]+]]:gpr64 = COPY $x1
18+
; CHECK-NEXT: [[COPY1:%[0-9]+]]:gpr64 = COPY $x0
19+
; CHECK-NEXT: [[COPY2:%[0-9]+]]:gpr32 = COPY $wzr
20+
; CHECK-NEXT: [[SUBSWrr:%[0-9]+]]:gpr32 = SUBSWrr [[COPY]].sub_32, [[COPY2]], implicit-def dead $nzcv
21+
; CHECK-NEXT: [[DEF:%[0-9]+]]:gpr64all = IMPLICIT_DEF
22+
; CHECK-NEXT: [[INSERT_SUBREG:%[0-9]+]]:gpr64 = INSERT_SUBREG [[DEF]], killed [[SUBSWrr]], %subreg.sub_32
23+
; CHECK-NEXT: [[RORVXr:%[0-9]+]]:gpr64 = RORVXr [[COPY1]], killed [[INSERT_SUBREG]]
24+
; CHECK-NEXT: $x0 = COPY [[RORVXr]]
25+
; CHECK-NEXT: RET_ReallyLR implicit $x0
26+
%0:gpr64 = COPY $x1
27+
%1:gpr64 = COPY $x0
28+
%2:gpr32 = COPY $wzr
29+
%3:gpr32 = SUBSWrr %0.sub_32, %2, implicit-def dead $nzcv
30+
%4:gpr64all = IMPLICIT_DEF
31+
%5:gpr64 = INSERT_SUBREG %4, killed %3, %subreg.sub_32
32+
%6:gpr64 = RORVXr %1, killed %5
33+
$x0 = COPY %6
34+
RET_ReallyLR implicit $x0
35+
36+
...
37+
38+
---
39+
name: analyze_compare_subreg_use_rhs
40+
tracksRegLiveness: true
41+
body: |
42+
bb.0:
43+
liveins: $x0, $x1
44+
45+
; CHECK-LABEL: name: analyze_compare_subreg_use_rhs
46+
; CHECK: liveins: $x0, $x1
47+
; CHECK-NEXT: {{ $}}
48+
; CHECK-NEXT: [[COPY:%[0-9]+]]:gpr64 = COPY $x1
49+
; CHECK-NEXT: [[COPY1:%[0-9]+]]:gpr64 = COPY $x0
50+
; CHECK-NEXT: [[COPY2:%[0-9]+]]:gpr32 = COPY $wzr
51+
; CHECK-NEXT: [[SUBSWrr:%[0-9]+]]:gpr32 = SUBSWrr [[COPY2]], [[COPY]].sub_32, implicit-def dead $nzcv
52+
; CHECK-NEXT: [[DEF:%[0-9]+]]:gpr64all = IMPLICIT_DEF
53+
; CHECK-NEXT: [[INSERT_SUBREG:%[0-9]+]]:gpr64 = INSERT_SUBREG [[DEF]], killed [[SUBSWrr]], %subreg.sub_32
54+
; CHECK-NEXT: [[RORVXr:%[0-9]+]]:gpr64 = RORVXr [[COPY1]], killed [[INSERT_SUBREG]]
55+
; CHECK-NEXT: $x0 = COPY [[RORVXr]]
56+
; CHECK-NEXT: RET_ReallyLR implicit $x0
57+
%0:gpr64 = COPY $x1
58+
%1:gpr64 = COPY $x0
59+
%2:gpr32 = COPY $wzr
60+
%3:gpr32 = SUBSWrr %2, %0.sub_32, implicit-def dead $nzcv
61+
%4:gpr64all = IMPLICIT_DEF
62+
%5:gpr64 = INSERT_SUBREG %4, killed %3, %subreg.sub_32
63+
%6:gpr64 = RORVXr %1, killed %5
64+
$x0 = COPY %6
65+
RET_ReallyLR implicit $x0
66+
67+
...

0 commit comments

Comments
 (0)