Skip to content

Commit 082fa56

Browse files
[ARM] Fix MOVCC peephole to not use an incorrect register class
The MOVCC peephole eliminates a MOVCC by making one of its inputs a conditional instruction, but when doing this it should be using both inputs of the MOVCC to decide on the register class to use as otherwise we can get an error when using -verify-machineinstrs. Differential Revision: https://reviews.llvm.org/D111714
1 parent 8c3adce commit 082fa56

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

llvm/lib/Target/ARM/ARMBaseInstrInfo.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2371,9 +2371,13 @@ ARMBaseInstrInfo::optimizeSelect(MachineInstr &MI,
23712371

23722372
// Find new register class to use.
23732373
MachineOperand FalseReg = MI.getOperand(Invert ? 2 : 1);
2374+
MachineOperand TrueReg = MI.getOperand(Invert ? 1 : 2);
23742375
Register DestReg = MI.getOperand(0).getReg();
2375-
const TargetRegisterClass *PreviousClass = MRI.getRegClass(FalseReg.getReg());
2376-
if (!MRI.constrainRegClass(DestReg, PreviousClass))
2376+
const TargetRegisterClass *FalseClass = MRI.getRegClass(FalseReg.getReg());
2377+
const TargetRegisterClass *TrueClass = MRI.getRegClass(TrueReg.getReg());
2378+
if (!MRI.constrainRegClass(DestReg, FalseClass))
2379+
return nullptr;
2380+
if (!MRI.constrainRegClass(DestReg, TrueClass))
23772381
return nullptr;
23782382

23792383
// Create a new predicated version of DefMI.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py
2+
# RUN: llc -run-pass=peephole-opt %s -o - -verify-machineinstrs | FileCheck %s
3+
4+
# Make sure the MOVCC to conditional instruction peephole doesn't change the
5+
# register class to one that's invalid.
6+
7+
--- |
8+
target triple = "armv7-unknown-unknown"
9+
define i32 @test(i32 %x, i32 %y) {
10+
ret i32 undef
11+
}
12+
...
13+
---
14+
name: test
15+
tracksRegLiveness: true
16+
body: |
17+
bb.0 (%ir-block.0):
18+
liveins: $r0, $r1
19+
; CHECK-LABEL: name: test
20+
; CHECK: liveins: $r0, $r1
21+
; CHECK: [[COPY:%[0-9]+]]:gpr = COPY $r0
22+
; CHECK-NEXT: [[COPY1:%[0-9]+]]:gpr = COPY $r1
23+
; CHECK-NEXT: [[MOVsi:%[0-9]+]]:gpr = MOVsi [[COPY1]], 27, 14 /* CC::al */, $noreg, $noreg
24+
; CHECK-NEXT: [[ORRrsi:%[0-9]+]]:gpr = ORRrsi [[MOVsi]], [[COPY1]], 234, 14 /* CC::al */, $noreg, $noreg
25+
; CHECK-NEXT: [[MOVsi1:%[0-9]+]]:gpr = MOVsi [[COPY1]], 155, 14 /* CC::al */, $noreg, $noreg
26+
; CHECK-NEXT: [[ORRrsi1:%[0-9]+]]:gprnopc = ORRrsi killed [[MOVsi1]], killed [[MOVsi]], 106, 14 /* CC::al */, $noreg, $noreg
27+
; CHECK-NEXT: TSTri [[COPY1]], 1, 14 /* CC::al */, $noreg, implicit-def $cpsr
28+
; CHECK-NEXT: [[UXTH:%[0-9]+]]:gprnopc = UXTH killed [[ORRrsi1]], 0, 0 /* CC::eq */, $cpsr, implicit [[ORRrsi]](tied-def 0)
29+
; CHECK-NEXT: $r0 = COPY killed [[UXTH]]
30+
; CHECK-NEXT: BX_RET 14 /* CC::al */, $noreg, implicit $r0
31+
%0:gpr = COPY $r0
32+
%1:gpr = COPY $r1
33+
%2:gpr = MOVsi %1:gpr, 27, 14, $noreg, $noreg
34+
%3:gpr = ORRrsi %2:gpr, %1:gpr, 234, 14, $noreg, $noreg
35+
%4:gpr = MOVsi %1:gpr, 155, 14, $noreg, $noreg
36+
%5:gprnopc = ORRrsi killed %4:gpr, killed %2:gpr, 106, 14, $noreg, $noreg
37+
%6:gprnopc = UXTH killed %5:gprnopc, 0, 14, $noreg
38+
TSTri %1:gpr, 1, 14, $noreg, implicit-def $cpsr
39+
%7:gpr = MOVCCr %3:gpr, killed %6:gprnopc, 0, $cpsr
40+
$r0 = COPY killed %7
41+
BX_RET 14, $noreg, implicit $r0

0 commit comments

Comments
 (0)