<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  </head>
  <body text="#000000" bgcolor="#FFFFFF">
    >It should be possible to get llvm to produce very good code for
    the Z80...<br>
    <br>
    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>
    <br>
    <div class="moz-cite-prefix">
      <div id="rwhMsgHeader"><br>
        <hr id="rwhMsgHdrDivider" style="border:0;border-top:1px solid
          #B5C4DF;padding:0;margin:10px 0 5px 0;width:100%;"><span
          style="margin: -1.3px 0 0 0 !important;"><font style="font:
            12px Arial !important; color: #000000 !important;"
            face="Arial" color="#000000"><b>From:</b> Bruce Hoult</font></span><br>
        <span style="margin: -1.3px 0 0 0 !important;"><font
            style="font: 12px Arial !important; color: #000000
            !important;" face="Arial" color="#000000"><b>Sent:</b>
            Sunday, Jul 22, 2018 9:42 AM WEST</font></span><br>
        <span style="margin: -1.3px 0 0 0 !important;"><font
            style="font: 12px Arial !important; color: #000000
            !important;" face="Arial" color="#000000"><b>To:</b> Michael
            Stellmann</font></span><br>
        <span style="margin: -1.3px 0 0 0 !important;"><font
            style="font: 12px Arial !important; color: #000000
            !important;" face="Arial" color="#000000"><b>Cc:</b> LLVM
            Developers Mailing List</font></span><br>
        <span style="margin: -1.3px 0 0 0 !important;"><font
            style="font: 12px Arial !important; color: #000000
            !important;" face="Arial" color="#000000"><b>Subject:</b>
            [llvm-dev] Finding scratch register after function call</font></span><br>
        <br>
      </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">
      <div dir="ltr">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><br>
        </div>
        <div>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>
        <div class="gmail_quote">On Sun, Jul 22, 2018 at 12:26 AM,
          Michael Stellmann <span dir="ltr"><<a
              href="mailto:Michael.Stellmann@gmx.net" target="_blank"
              moz-do-not-send="true">Michael.Stellmann@gmx.net</a>></span>
          wrote:<br>
          <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>
            <br>
            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>
            <br>
            OK, understood. I was first thinking about doing something
            like this for small frames:<br>
            <br>
            1.  push bc # 1 byte; 11 cycles - part of call
            frame-cleanup: save scratch register<br>
            <br>
                +-----begin call-related<br>
            2.  ld <rr>,stack-param<br>
            3.  push <rr><br>
                 ... more code to load non-stack params into registers<br>
            4.  call ...<br>
            5.  pop bc # 1 byte ; 10 cycles - call frame cleanup:
            restore stack-pointer (value in BC is not used)<br>
                +-----end call-related<br>
            <br>
            6.  pop bc # part of call frame-cleanup: restore scratch
            reg's value<br>
            <br>
            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>
            <br>
            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>
            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>
            <br>
            This also allows keeping a "beneficial" param and return
            value calling convention:<br>
            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>
            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>
            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>
            <br>
            Thanks for pointing me again to the right direction!<br>
            <br>
            Michael<br>
            <br>
            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>
            <br>
          </blockquote>
        </div>
        <br>
      </div>
    </blockquote>
    <br>
  </body>
</html>