Skip to content

Commit f4ef793

Browse files
committed
[AVR] Optimize int8 arithmetic right shift 6 bits
Reviewed By: aykevl Differential Revision: https://reviews.llvm.org/D115593
1 parent 961f51f commit f4ef793

File tree

3 files changed

+58
-1
lines changed

3 files changed

+58
-1
lines changed

llvm/lib/Target/AVR/AVRExpandPseudoInsts.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ class AVRExpandPseudo : public MachineFunctionPass {
9292
/// Specific shift implementation.
9393
bool expandLSLB7Rd(Block &MBB, BlockIt MBBI);
9494
bool expandLSRB7Rd(Block &MBB, BlockIt MBBI);
95+
bool expandASRB6Rd(Block &MBB, BlockIt MBBI);
9596
bool expandASRB7Rd(Block &MBB, BlockIt MBBI);
9697
bool expandLSLW4Rd(Block &MBB, BlockIt MBBI);
9798
bool expandLSRW4Rd(Block &MBB, BlockIt MBBI);
@@ -1921,6 +1922,45 @@ bool AVRExpandPseudo::expand<AVR::LSRBNRd>(Block &MBB, BlockIt MBBI) {
19211922
}
19221923
}
19231924

1925+
bool AVRExpandPseudo::expandASRB6Rd(Block &MBB, BlockIt MBBI) {
1926+
MachineInstr &MI = *MBBI;
1927+
Register DstReg = MI.getOperand(0).getReg();
1928+
bool DstIsDead = MI.getOperand(0).isDead();
1929+
bool DstIsKill = MI.getOperand(1).isKill();
1930+
bool ImpIsDead = MI.getOperand(3).isDead();
1931+
1932+
// bst r24, 6
1933+
// lsl r24
1934+
// sbc r24, r24
1935+
// bld r24, 0
1936+
1937+
buildMI(MBB, MBBI, AVR::BST)
1938+
.addReg(DstReg)
1939+
.addImm(6)
1940+
->getOperand(2)
1941+
.setIsUndef(true);
1942+
1943+
buildMI(MBB, MBBI, AVR::ADDRdRr) // LSL Rd <==> ADD Rd, Rd
1944+
.addReg(DstReg, RegState::Define | getDeadRegState(DstIsDead))
1945+
.addReg(DstReg, getKillRegState(DstIsKill))
1946+
.addReg(DstReg, getKillRegState(DstIsKill));
1947+
1948+
buildMI(MBB, MBBI, AVR::SBCRdRr)
1949+
.addReg(DstReg, RegState::Define | getDeadRegState(DstIsDead))
1950+
.addReg(DstReg, getKillRegState(DstIsKill))
1951+
.addReg(DstReg, getKillRegState(DstIsKill));
1952+
1953+
buildMI(MBB, MBBI, AVR::BLD)
1954+
.addReg(DstReg, RegState::Define | getDeadRegState(DstIsDead))
1955+
.addReg(DstReg, getKillRegState(DstIsKill))
1956+
.addImm(0)
1957+
->getOperand(3)
1958+
.setIsKill();
1959+
1960+
MI.eraseFromParent();
1961+
return true;
1962+
}
1963+
19241964
bool AVRExpandPseudo::expandASRB7Rd(Block &MBB, BlockIt MBBI) {
19251965
MachineInstr &MI = *MBBI;
19261966
Register DstReg = MI.getOperand(0).getReg();
@@ -1957,6 +1997,8 @@ bool AVRExpandPseudo::expand<AVR::ASRBNRd>(Block &MBB, BlockIt MBBI) {
19571997
MachineInstr &MI = *MBBI;
19581998
unsigned Imm = MI.getOperand(2).getImm();
19591999
switch (Imm) {
2000+
case 6:
2001+
return expandASRB6Rd(MBB, MBBI);
19602002
case 7:
19612003
return expandASRB7Rd(MBB, MBBI);
19622004
default:

llvm/lib/Target/AVR/AVRISelLowering.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,11 @@ SDValue AVRTargetLowering::LowerShifts(SDValue Op, SelectionDAG &DAG) const {
359359
Victim = DAG.getNode(AVRISD::LSRBN, dl, VT, Victim,
360360
DAG.getConstant(7, dl, VT));
361361
ShiftAmount = 0;
362+
} else if (Op.getOpcode() == ISD::SRA && ShiftAmount == 6) {
363+
// Optimize ASR when ShiftAmount == 6.
364+
Victim = DAG.getNode(AVRISD::ASRBN, dl, VT, Victim,
365+
DAG.getConstant(6, dl, VT));
366+
ShiftAmount = 0;
362367
} else if (Op.getOpcode() == ISD::SRA && ShiftAmount == 7) {
363368
// Optimize ASR when ShiftAmount == 7.
364369
Victim = DAG.getNode(AVRISD::ASRBN, dl, VT, Victim,

llvm/test/CodeGen/AVR/shift.ll

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
; RUN: llc < %s -march=avr | FileCheck %s
1+
; RUN: llc < %s -march=avr -verify-machineinstrs | FileCheck %s
22

33
; Optimize for speed.
44
; CHECK-LABEL: shift_i8_i8_speed
@@ -171,6 +171,16 @@ define i8 @lsr_i8_7(i8 %a) {
171171
ret i8 %result
172172
}
173173

174+
define i8 @asr_i8_6(i8 %a) {
175+
; CHECK-LABEL: asr_i8_6
176+
; CHECK: bst r24, 6
177+
; CHECK-NEXT: lsl r24
178+
; CHECK-NEXT: sbc r24, r24
179+
; CHECK-NEXT: bld r24, 0
180+
%result = ashr i8 %a, 6
181+
ret i8 %result
182+
}
183+
174184
define i8 @asr_i8_7(i8 %a) {
175185
; CHECK-LABEL: asr_i8_7
176186
; CHECK: lsl r24

0 commit comments

Comments
 (0)