Unverified Commit 116ab786 authored by Ayke van Laethem's avatar Ayke van Laethem
Browse files

[AVR] Make use of the constant value 0 in R1

The register R1 is defined to have the constant value 0 in the avr-gcc
calling convention (which we follow). Unfortunately, we don't really
make use of it. This patch replaces `LDI 0` instructions with a copy
from R1.

This reduces code size: my AVR build of compiler-rt goes from 50660 to
50240 bytes of code size, which is a 0.8% reduction. Presumably it will
also improve execution speed, although I didn't measure this.

Differential Revision: https://reviews.llvm.org/D117425
parent 15335918
...@@ -1695,6 +1695,18 @@ MachineBasicBlock *AVRTargetLowering::insertMul(MachineInstr &MI, ...@@ -1695,6 +1695,18 @@ MachineBasicBlock *AVRTargetLowering::insertMul(MachineInstr &MI,
return BB; return BB;
} }
// Insert a read from R1, which almost always contains the value 0.
MachineBasicBlock *
AVRTargetLowering::insertCopyR1(MachineInstr &MI, MachineBasicBlock *BB) const {
const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
MachineBasicBlock::iterator I(MI);
BuildMI(*BB, I, MI.getDebugLoc(), TII.get(AVR::COPY))
.add(MI.getOperand(0))
.addReg(AVR::R1);
MI.eraseFromParent();
return BB;
}
MachineBasicBlock * MachineBasicBlock *
AVRTargetLowering::EmitInstrWithCustomInserter(MachineInstr &MI, AVRTargetLowering::EmitInstrWithCustomInserter(MachineInstr &MI,
MachineBasicBlock *MBB) const { MachineBasicBlock *MBB) const {
...@@ -1717,6 +1729,8 @@ AVRTargetLowering::EmitInstrWithCustomInserter(MachineInstr &MI, ...@@ -1717,6 +1729,8 @@ AVRTargetLowering::EmitInstrWithCustomInserter(MachineInstr &MI,
case AVR::MULRdRr: case AVR::MULRdRr:
case AVR::MULSRdRr: case AVR::MULSRdRr:
return insertMul(MI, MBB); return insertMul(MI, MBB);
case AVR::CopyR1:
return insertCopyR1(MI, MBB);
} }
assert((Opc == AVR::Select16 || Opc == AVR::Select8) && assert((Opc == AVR::Select16 || Opc == AVR::Select8) &&
......
...@@ -187,6 +187,8 @@ protected: ...@@ -187,6 +187,8 @@ protected:
private: private:
MachineBasicBlock *insertShift(MachineInstr &MI, MachineBasicBlock *BB) const; MachineBasicBlock *insertShift(MachineInstr &MI, MachineBasicBlock *BB) const;
MachineBasicBlock *insertMul(MachineInstr &MI, MachineBasicBlock *BB) const; MachineBasicBlock *insertMul(MachineInstr &MI, MachineBasicBlock *BB) const;
MachineBasicBlock *insertCopyR1(MachineInstr &MI,
MachineBasicBlock *BB) const;
}; };
} // end namespace llvm } // end namespace llvm
......
...@@ -2390,6 +2390,10 @@ def Asr16 : ShiftPseudo<(outs DREGS ...@@ -2390,6 +2390,10 @@ def Asr16 : ShiftPseudo<(outs DREGS
: $src, i8 : $src, i8
: $cnt))]>; : $cnt))]>;
// lowered to a copy from R1, which contains the value zero.
let usesCustomInserter=1 in
def CopyR1 : Pseudo<(outs GPR8:$rd), (ins), "clrz\t$rd", [(set i8:$rd, 0)]>;
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
// Non-Instruction Patterns // Non-Instruction Patterns
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
......
...@@ -18,7 +18,7 @@ entry-block: ...@@ -18,7 +18,7 @@ entry-block:
; CHECK: ldi [[RET:r[0-9]+]], 1 ; CHECK: ldi [[RET:r[0-9]+]], 1
; CHECK: cp {{.*}}[[HIGH]], {{.*}}[[LOW]] ; CHECK: cp {{.*}}[[HIGH]], {{.*}}[[LOW]]
; CHECK: brne [[LABEL:.LBB[_0-9]+]] ; CHECK: brne [[LABEL:.LBB[_0-9]+]]
; CHECK: ldi {{.*}}[[RET]], 0 ; CHECK: mov {{.*}}[[RET]], r1
; CHECK: {{.*}}[[LABEL]] ; CHECK: {{.*}}[[LABEL]]
; CHECK: ret ; CHECK: ret
} }
......
...@@ -6,8 +6,7 @@ ...@@ -6,8 +6,7 @@
; CHECK-LABEL: foo ; CHECK-LABEL: foo
define void @foo() { define void @foo() {
; CHECK: ldi [[SRC:r[0-9]+]], 0 ; CHECK: st [[PTRREG:X|Y|Z]], r1
; CHECK-NEXT: st [[PTRREG:X|Y|Z]], [[SRC]]
store i8 0, i8* undef, align 4 store i8 0, i8* undef, align 4
ret void ret void
} }
...@@ -14,7 +14,7 @@ entry-block: ...@@ -14,7 +14,7 @@ entry-block:
; CHECK: ldi [[RET:r[0-9]+]], 1 ; CHECK: ldi [[RET:r[0-9]+]], 1
; CHECK: cpi {{.*}}[[HIGH]], 0 ; CHECK: cpi {{.*}}[[HIGH]], 0
; CHECK: brne [[LABEL:.LBB[_0-9]+]] ; CHECK: brne [[LABEL:.LBB[_0-9]+]]
; CHECK: ldi {{.*}}[[RET]], 0 ; CHECK: mov {{.*}}[[RET]], r1
; CHECK: {{.*}}[[LABEL]] ; CHECK: {{.*}}[[LABEL]]
; CHECK: ret ; CHECK: ret
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment