Page MenuHomePhabricator

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

Authored by aykevl on Jan 16 2022, 4:35 AM.

Details

Summary

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.


This patch took me a looong time, with many failed attempts. Finally I have something that works. It can still be improved, but that can happen in follow up patches. A 0.8% decrease in code size is already pretty significant.

Diff Detail

Event Timeline

aykevl created this revision.Jan 16 2022, 4:35 AM
aykevl requested review of this revision.Jan 16 2022, 4:35 AM
Herald added a project: Restricted Project. · View Herald TranscriptJan 16 2022, 4:35 AM
aykevl edited the summary of this revision. (Show Details)Jan 16 2022, 4:36 AM
benshi001 added inline comments.Jan 21 2022, 4:13 AM
llvm/lib/Target/AVR/AVRISelLowering.cpp
1699

I think we need to check the device family, AFAIK, r17 is used as zero_reg on avr-tiny family.

benshi001 added inline comments.Jan 21 2022, 6:15 AM
llvm/lib/Target/AVR/AVRISelLowering.cpp
1699

do we need to make a helper function int getRegZero(void) in AVRSubtarget.h, just like I have done as getIORegRAMPZ(void) ? The new helper function will return r17 for avr-tiny devices.

aykevl added inline comments.Jan 22 2022, 10:40 AM
llvm/lib/Target/AVR/AVRISelLowering.cpp
1699

You are correct. However, there are many other places in the compiler that assume R1 is the zero register (and R0 is the scratch register). For example, here:
https://github.com/llvm/llvm-project/blob/main/llvm/lib/Target/AVR/AVRRegisterInfo.cpp#L53-L57
I agree we should use R16 and R17 for avr-tiny, but I would prefer to do that all in once patch. I can do that after this patch and D117725 have landed? What do you think?

benshi001 accepted this revision.Jan 23 2022, 6:09 AM
benshi001 added inline comments.
llvm/lib/Target/AVR/AVRISelLowering.cpp
1699

I agree with you. Actually I am planing and investigating implementation of avttiny.

This revision is now accepted and ready to land.Jan 23 2022, 6:09 AM
This revision was landed with ongoing or failed builds.Jan 23 2022, 8:08 AM
This revision was automatically updated to reflect the committed changes.