Skip to content

Commit 979e8ae

Browse files
committed
[AArch64] Check opcode before trying to extract register from operand
When matching FNEG patterns for the MachineCombiner we need to check for opcodes first, before trying to extract a register from an operand. Otherwise handling of instructions with non-register operands causes the compiler to crash. Differential Revision: https://reviews.llvm.org/D158473
1 parent 514b38c commit 979e8ae

File tree

2 files changed

+132
-2
lines changed

2 files changed

+132
-2
lines changed

llvm/lib/Target/AArch64/AArch64InstrInfo.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5514,8 +5514,8 @@ static bool getFNEGPatterns(MachineInstr &Root,
55145514
auto Match = [&](unsigned Opcode, MachineCombinerPattern Pattern) -> bool {
55155515
MachineOperand &MO = Root.getOperand(1);
55165516
MachineInstr *MI = MRI.getUniqueVRegDef(MO.getReg());
5517-
if (MI != nullptr && MRI.hasOneNonDBGUse(MI->getOperand(0).getReg()) &&
5518-
(MI->getOpcode() == Opcode) &&
5517+
if (MI != nullptr && (MI->getOpcode() == Opcode) &&
5518+
MRI.hasOneNonDBGUse(MI->getOperand(0).getReg()) &&
55195519
Root.getFlag(MachineInstr::MIFlag::FmContract) &&
55205520
Root.getFlag(MachineInstr::MIFlag::FmNsz) &&
55215521
MI->getFlag(MachineInstr::MIFlag::FmContract) &&
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py UTC_ARGS: --version 2
2+
# RUN: llc -mtriple aarch64 -run-pass=machine-combiner -o - %s | FileCheck %s
3+
--- |
4+
target datalayout = "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128"
5+
target triple = "aarch64"
6+
7+
@c = global double 0.000000e+00, align 8
8+
9+
define void @emit_fneg_with_non_register_operand(double %c) {
10+
entry:
11+
%0 = load double, ptr @c, align 8
12+
%1 = tail call double asm sideeffect "", "=w,0"(double %0)
13+
%2 = load double, ptr @c, align 8
14+
%3 = tail call double asm sideeffect "", "=w,0"(double %2)
15+
%fneg = fneg double %1
16+
%cmp = fcmp oeq double %3, %fneg
17+
br i1 %cmp, label %if.then, label %if.end
18+
19+
if.then: ; preds = %entry
20+
tail call void @b(double noundef %1)
21+
ret void
22+
23+
if.end: ; preds = %entry
24+
ret void
25+
}
26+
27+
declare void @b(double noundef)
28+
29+
...
30+
---
31+
name: emit_fneg_with_non_register_operand
32+
alignment: 4
33+
exposesReturnsTwice: false
34+
legalized: false
35+
regBankSelected: false
36+
selected: false
37+
failedISel: false
38+
tracksRegLiveness: true
39+
hasWinCFI: false
40+
callsEHReturn: false
41+
callsUnwindInit: false
42+
hasEHCatchret: false
43+
hasEHScopes: false
44+
hasEHFunclets: false
45+
isOutlined: false
46+
debugInstrRef: false
47+
failsVerification: false
48+
tracksDebugUserValues: false
49+
registers:
50+
- { id: 0, class: fpr64, preferred-register: '' }
51+
- { id: 1, class: fpr64, preferred-register: '' }
52+
- { id: 2, class: fpr64, preferred-register: '' }
53+
- { id: 3, class: fpr64, preferred-register: '' }
54+
- { id: 4, class: fpr64, preferred-register: '' }
55+
- { id: 5, class: fpr64, preferred-register: '' }
56+
- { id: 6, class: gpr64common, preferred-register: '' }
57+
- { id: 7, class: fpr64, preferred-register: '' }
58+
liveins: []
59+
frameInfo:
60+
isFrameAddressTaken: false
61+
isReturnAddressTaken: false
62+
hasStackMap: false
63+
hasPatchPoint: false
64+
stackSize: 0
65+
offsetAdjustment: 0
66+
maxAlignment: 1
67+
adjustsStack: false
68+
hasCalls: false
69+
stackProtector: ''
70+
functionContext: ''
71+
maxCallFrameSize: 0
72+
cvBytesOfCalleeSavedRegisters: 0
73+
hasOpaqueSPAdjustment: false
74+
hasVAStart: false
75+
hasMustTailInVarArgFunc: false
76+
hasTailCall: true
77+
localFrameSize: 0
78+
savePoint: ''
79+
restorePoint: ''
80+
fixedStack: []
81+
stack: []
82+
entry_values: []
83+
callSites: []
84+
debugValueSubstitutions: []
85+
constants: []
86+
machineFunctionInfo: {}
87+
body: |
88+
; CHECK-LABEL: name: emit_fneg_with_non_register_operand
89+
; CHECK: bb.0.entry:
90+
; CHECK-NEXT: successors: %bb.1(0x50000000), %bb.2(0x30000000)
91+
; CHECK-NEXT: {{ $}}
92+
; CHECK-NEXT: [[LOADgot:%[0-9]+]]:gpr64common = LOADgot target-flags(aarch64-got) @c
93+
; CHECK-NEXT: [[LDRDui:%[0-9]+]]:fpr64 = LDRDui [[LOADgot]], 0 :: (dereferenceable load (s64) from @c)
94+
; CHECK-NEXT: INLINEASM &"", 1 /* sideeffect attdialect */, 2359306 /* regdef:FPR64 */, def %2, 2147483657 /* reguse tiedto:$0 */, [[LDRDui]](tied-def 3)
95+
; CHECK-NEXT: [[COPY:%[0-9]+]]:fpr64 = COPY %2
96+
; CHECK-NEXT: [[LDRDui1:%[0-9]+]]:fpr64 = LDRDui [[LOADgot]], 0 :: (dereferenceable load (s64) from @c)
97+
; CHECK-NEXT: INLINEASM &"", 1 /* sideeffect attdialect */, 2359306 /* regdef:FPR64 */, def %4, 2147483657 /* reguse tiedto:$0 */, [[LDRDui1]](tied-def 3)
98+
; CHECK-NEXT: [[FNEGDr:%[0-9]+]]:fpr64 = FNEGDr %2
99+
; CHECK-NEXT: nofpexcept FCMPDrr %4, killed [[FNEGDr]], implicit-def $nzcv, implicit $fpcr
100+
; CHECK-NEXT: Bcc 1, %bb.2, implicit $nzcv
101+
; CHECK-NEXT: B %bb.1
102+
; CHECK-NEXT: {{ $}}
103+
; CHECK-NEXT: bb.1.if.then:
104+
; CHECK-NEXT: $d0 = COPY [[COPY]]
105+
; CHECK-NEXT: TCRETURNdi @b, 0, csr_aarch64_aapcs, implicit $sp, implicit $d0
106+
; CHECK-NEXT: {{ $}}
107+
; CHECK-NEXT: bb.2.if.end:
108+
; CHECK-NEXT: RET_ReallyLR
109+
bb.0.entry:
110+
successors: %bb.1(0x50000000), %bb.2(0x30000000)
111+
112+
%6:gpr64common = LOADgot target-flags(aarch64-got) @c
113+
%3:fpr64 = LDRDui %6, 0 :: (dereferenceable load (s64) from @c)
114+
INLINEASM &"", 1 /* sideeffect attdialect */, 2359306 /* regdef:FPR64 */, def %2, 2147483657 /* reguse tiedto:$0 */, %3(tied-def 3)
115+
%0:fpr64 = COPY %2
116+
%5:fpr64 = LDRDui %6, 0 :: (dereferenceable load (s64) from @c)
117+
INLINEASM &"", 1 /* sideeffect attdialect */, 2359306 /* regdef:FPR64 */, def %4, 2147483657 /* reguse tiedto:$0 */, %5(tied-def 3)
118+
%7:fpr64 = FNEGDr %2
119+
nofpexcept FCMPDrr %4, killed %7, implicit-def $nzcv, implicit $fpcr
120+
Bcc 1, %bb.2, implicit $nzcv
121+
B %bb.1
122+
123+
bb.1.if.then:
124+
$d0 = COPY %0
125+
TCRETURNdi @b, 0, csr_aarch64_aapcs, implicit $sp, implicit $d0
126+
127+
bb.2.if.end:
128+
RET_ReallyLR
129+
130+
...

0 commit comments

Comments
 (0)