<html><head><meta http-equiv="Content-Type" content="text/html; charset=us-ascii"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class="">I didn't read through all the variations possible here, but the approach taken by other LLVM targets is to create new vregs during early PrologEpilogInsertion which are later allocated using the RegisterScavenger.<div class=""><br class=""></div><div class="">Register scavenging has some limitations: VReg live ranges mustn't cross basic block boundaries and you cannot create new slots on the stackframe meaning you often have to conservatively keep an emergency spillslot as part of your stackframe even though it may not always get used.</div><div class=""><br class=""></div><div class="">The scavenger will then either use a free register or perform an emergency spill/reload within the basic block to free a register.</div><div class=""><br class=""></div><div class="">- Matthias<br class=""><div><br class=""><blockquote type="cite" class=""><div class="">On Jul 22, 2018, at 12:58 AM, Michael Stellmann via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" class="">llvm-dev@lists.llvm.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class="">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" class="">
<div text="#000000" bgcolor="#FFFFFF" class="">
>It should be possible to get llvm to produce very good code for
the Z80...<br class="">
<br class="">
Yes, I was thinking that too. These techniques didn't exist back
then, so I'm really looking forward to the point where the first
regular C sources can be compiled and see the magic happening in
action live :)<br class="">
<br class="">
<div class="moz-cite-prefix">
<div id="rwhMsgHeader" class=""><br class="">
<hr id="rwhMsgHdrDivider" style="border:0;border-top:1px solid
#B5C4DF;padding:0;margin:10px 0 5px 0;width:100%;" class=""><span style="margin: -1.3px 0 0 0 !important;" class=""><font style="font-style: normal !important; font-variant-caps: normal !important; font-weight: normal !important; font-stretch: normal !important; font-size: 12px !important; line-height: normal !important; font-family: Arial !important;" face="Arial" class=""><b class="">From:</b> Bruce Hoult</font></span><br class="">
<span style="margin: -1.3px 0 0 0 !important;" class=""><font style="font-style: normal !important; font-variant-caps: normal !important; font-weight: normal !important; font-stretch: normal !important; font-size: 12px !important; line-height: normal !important; font-family: Arial !important;" face="Arial" class=""><b class="">Sent:</b>
Sunday, Jul 22, 2018 9:42 AM WEST</font></span><br class="">
<span style="margin: -1.3px 0 0 0 !important;" class=""><font style="font-style: normal !important; font-variant-caps: normal !important; font-weight: normal !important; font-stretch: normal !important; font-size: 12px !important; line-height: normal !important; font-family: Arial !important;" face="Arial" class=""><b class="">To:</b> Michael
Stellmann</font></span><br class="">
<span style="margin: -1.3px 0 0 0 !important;" class=""><font style="font-style: normal !important; font-variant-caps: normal !important; font-weight: normal !important; font-stretch: normal !important; font-size: 12px !important; line-height: normal !important; font-family: Arial !important;" face="Arial" class=""><b class="">Cc:</b> LLVM
Developers Mailing List</font></span><br class="">
<span style="margin: -1.3px 0 0 0 !important;" class=""><font style="font-style: normal !important; font-variant-caps: normal !important; font-weight: normal !important; font-stretch: normal !important; font-size: 12px !important; line-height: normal !important; font-family: Arial !important;" face="Arial" class=""><b class="">Subject:</b>
[llvm-dev] Finding scratch register after function call</font></span><br class="">
<br class="">
</div>
</div>
<blockquote type="cite" cite="mid:CAP8PnuS+JMDO3QwNkxBVy1_KVscE70=J3tMq+EWcdv820qt4GA@mail.gmail.com" style="border:none !important; margin-left:0px !important;
margin-right:0px !important; margin-top:0px !important;
padding-left:0px !important; padding-right:0px !important" class="">
<div dir="ltr" class="">I had a quick look at some reference material this
time :-) I also did some work on a DEC Rainbow and a Kaypro CP/M
luggable a few years later. The compilers of the time were just
awful!
<div class=""><br class="">
</div>
<div class="">It should be possible to get llvm to produce very good code
for the Z80 -- in larger functions probably better than any
human would put the effort in to achieve. In particular, what
the human regards as the "same variable" (but a different SSA
value) might live in different registers at different times.</div>
</div>
<div class="gmail_extra"><br class="">
<div class="gmail_quote">On Sun, Jul 22, 2018 at 12:26 AM,
Michael Stellmann <span dir="ltr" class=""><<a href="mailto:Michael.Stellmann@gmx.net" target="_blank" moz-do-not-send="true" class="">Michael.Stellmann@gmx.net</a>></span>
wrote:<br class="">
<blockquote class="gmail_quote" style="border:none !important;
margin-left:0px !important; margin-right:0px !important;
margin-top:0px !important; padding-left:0px !important;
padding-right:0px !important">Thanks Bruce,<br class="">
<br class="">
and elaborately as ever. Again, I'm surprised about your
very thorough Z80 knowledge when you said you only did
little on the ZX81 in the eighties :D<br class="">
<br class="">
OK, understood. I was first thinking about doing something
like this for small frames:<br class="">
<br class="">
1. push bc # 1 byte; 11 cycles - part of call
frame-cleanup: save scratch register<br class="">
<br class="">
+-----begin call-related<br class="">
2. ld <rr>,stack-param<br class="">
3. push <rr><br class="">
... more code to load non-stack params into registers<br class="">
4. call ...<br class="">
5. pop bc # 1 byte ; 10 cycles - call frame cleanup:
restore stack-pointer (value in BC is not used)<br class="">
+-----end call-related<br class="">
<br class="">
6. pop bc # part of call frame-cleanup: restore scratch
reg's value<br class="">
<br class="">
The stack cleanup would insert line 5, and have to insert
lines 1 and 6 - summing up to 3 bytes of instructions - and
maybe the outer two could be eliminated in a late
optimization pass, when register usage is known.<br class="">
<br class="">
But then again - looking at your math and the *total* mem
and cycles, incl. setup and tear-down - convinced me of
dropping my complex idea with saving "BC" and use it for
cleanup or sacrificing the calling convention. The
complexity just doesn't justify the gains. Instead, going
for easy-to-implement solutions:<br class="">
For small call frames with only 1 or 2 params on the stack,
two "inc sp" (1 byte, 6 cycles per inst) per parameter can
be used, and your "big stack frame" suggestion for larger
ones.<br class="">
<br class="">
This also allows keeping a "beneficial" param and return
value calling convention:<br class="">
I want to assign the first 3 (HL + DE + BC - or at least 2)
function params to registers, so the stack cleanup is only
required for functions with more than 3 parameters at all -
or vararg funcs.<br class="">
And only functions with more than 5 params will need the
"big stack frame" cleanup. Those cases are rare (or at least
can be avoided easily by a developer), or, knowing the
mechanics, shouldn't be used for time critical inner loops
anyway.<br class="">
Being able to keep HL for the return value allows very
efficient nested function calls in the form
"Func1(Func2(nnn));", as register shuffling can be avoided -
the result of Func2 can be passed directly Func1.<br class="">
<br class="">
Thanks for pointing me again to the right direction!<br class="">
<br class="">
Michael<br class="">
<br class="">
Oh, and BTW, I'm planning to do the backend primarily for
the MSX - my first computer in 1984. Just for the fun of it,
I started now writing a small game for it after 25+ years of
absence, and was wondering what 30+ years compiler
technology would be able to achieve on such a simple (but
challenging, as in "not-alway-straightforward") CPU ;-)<br class="">
<br class="">
</blockquote>
</div>
<br class="">
</div>
</blockquote>
<br class="">
</div>
_______________________________________________<br class="">LLVM Developers mailing list<br class=""><a href="mailto:llvm-dev@lists.llvm.org" class="">llvm-dev@lists.llvm.org</a><br class="">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev<br class=""></div></blockquote></div><br class=""></div></body></html>