#392 Peephole to avoid reload of global variable addresses


The following code, when compiled using SDCC 3.3.1 #8717:

extern int proc_lastpid;
proc_table_insert(void *p)

        return (++proc_lastpid);

produces the assembler below:

   0000                      52 _proc_table_insert_start::
   0000                      53 _proc_table_insert:
                             54 ;test.c:8: return (++proc_lastpid);
                             55 ;       genPlus
                             56 ;fetchLitPair
                             57 ; peephole 51a incremented in (hl) instead of 0 (iy).
   0000 21r00r00      [10]   58         ld      hl, #_proc_lastpid+0
   0003 34            [11]   59         inc     (hl)
   0004 20 04         [12]   60         jr      NZ,00103$
                             61 ; peephole 155 changed absolute to relative conditional jump.
                             62 ;fetchLitPair
                             63 ; peephole 51a incremented in (hl) instead of 1 (iy).
   0006 21r01r00      [10]   64         ld      hl, #_proc_lastpid+1
   0009 34            [11]   65         inc     (hl)
   000A                      66 00103$:
                             67 ;       genRet
                             68 ;fetchPairLong
   000A 2Ar00r00      [16]   69         ld      hl,(_proc_lastpid)
                             70 ;       genLabel
                             71 ; peephole 149 removed unused label 00101$.
                             72 ;       genEndFunction
   000D C9            [10]   73         ret
   000E                      74 _proc_table_insert_end::

Line 69 reloads hl with proc_lastpid+1, which could be avoided. There are many ways of doing this, but I'm trying to think of stuff which is actually possible with SDCC :-)

So, I see we could do:

  • Rework the code completely. The following would use only 8 bytes and leaves the result in hl ready to return to the caller. Only works with pre-increment though, and suspect it would probably need to be done higher up in the C front end than the peephole optimiser. I suppose it would only be valid for a global variable as well, not stack-auto.

ld hl, (proc_lastpid)
inc hl
ld (proc_lastpid), hl

  • In the peephole optimiser, recognise that the reload of hl at line 64 is using the same global address (which isn't going to change, is it?) and an offset of 1 rather than 0. As there are no labels inbetween the two hl loads, we could replace the reload with "inc hl", saving 2 bytes.

I see the peephole already eliminates the use of iy, but is it possible to include something like the above as well?


Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

No, thanks