Skip to content

Cherry-pick: [AVR] Fix expanding MOVW for overlapping registers #141

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions llvm/lib/Target/AVR/AVRInstrInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ void AVRInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
const AVRRegisterInfo &TRI = *STI.getRegisterInfo();
unsigned Opc;

// Not all AVR devices support the 16-bit `MOVW` instruction.
if (AVR::DREGSRegClass.contains(DestReg, SrcReg)) {
// If our AVR has `movw`, let's emit that; otherwise let's emit two separate
// `mov`s.
if (STI.hasMOVW() && AVR::DREGSMOVWRegClass.contains(DestReg, SrcReg)) {
BuildMI(MBB, MI, DL, get(AVR::MOVWRdRr), DestReg)
.addReg(SrcReg, getKillRegState(KillSrc));
Expand All @@ -57,11 +58,17 @@ void AVRInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
TRI.splitReg(DestReg, DestLo, DestHi);
TRI.splitReg(SrcReg, SrcLo, SrcHi);

// Copy each individual register with the `MOV` instruction.
BuildMI(MBB, MI, DL, get(AVR::MOVRdRr), DestLo)
.addReg(SrcLo, getKillRegState(KillSrc));
BuildMI(MBB, MI, DL, get(AVR::MOVRdRr), DestHi)
.addReg(SrcHi, getKillRegState(KillSrc));
if (DestLo == SrcHi) {
BuildMI(MBB, MI, DL, get(AVR::MOVRdRr), DestHi)
.addReg(SrcHi, getKillRegState(KillSrc));
BuildMI(MBB, MI, DL, get(AVR::MOVRdRr), DestLo)
.addReg(SrcLo, getKillRegState(KillSrc));
} else {
BuildMI(MBB, MI, DL, get(AVR::MOVRdRr), DestLo)
.addReg(SrcLo, getKillRegState(KillSrc));
BuildMI(MBB, MI, DL, get(AVR::MOVRdRr), DestHi)
.addReg(SrcHi, getKillRegState(KillSrc));
}
}
} else {
if (AVR::GPR8RegClass.contains(DestReg, SrcReg)) {
Expand Down
47 changes: 47 additions & 0 deletions llvm/test/CodeGen/AVR/pseudo/COPY.mir
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# RUN: llc -O0 %s -o - | FileCheck %s

--- |
target triple = "avr--"

define void @test_copy_nonoverlapping() {
entry:
ret void
}

define void @test_copy_overlapping() {
entry:
ret void
}

declare void @foo(i16 %0)
...

---
name: test_copy_nonoverlapping
tracksRegLiveness: true
body: |
bb.0.entry:
liveins: $r25r24

; CHECK-LABEL: test_copy_nonoverlapping:
; CHECK: mov r22, r24
; CHECK-NEXT: mov r23, r25

$r23r22 = COPY $r25r24
RCALLk @foo, implicit $r24r23
...

---
name: test_copy_overlapping
tracksRegLiveness: true
body: |
bb.0.entry:
liveins: $r24r23

; CHECK-LABEL: test_copy_overlapping:
; CHECK: mov r25, r24
; CHECK-NEXT: mov r24, r23

$r25r24 = COPY $r24r23
RCALLk @foo, implicit $r25r24
...
22 changes: 22 additions & 0 deletions llvm/test/CodeGen/AVR/rust-bug-98167.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
; RUN: llc < %s -march=avr | FileCheck %s

; The bug can be found here:
; https://github.com/rust-lang/rust/issues/98167
;
; In this test, `extractvalue` + `call` generate a copy with overlapping
; registers (`$r25r24 = COPY $r24r23`) that used to be expanded incorrectly.

define void @main() {
; CHECK-LABEL: main:
; CHECK: rcall foo
; CHECK-NEXT: mov r25, r24
; CHECK-NEXT: mov r24, r23
; CHECK-NEXT: rcall bar
%1 = call { i8, i16 } @foo()
%2 = extractvalue { i8, i16 } %1, 1
call void @bar(i16 %2)
ret void
}

declare { i8, i16 } @foo()
declare void @bar(i16 %0)