Skip to content

Missed pointer return value optimization #11

@ZERICO2005

Description

@ZERICO2005

https://godbolt.org/z/bxT3aPjbd

The gmtime function always returns _gmtime.tm2 in HL. However, it seems that when localetime and gmtime are in the same translation unit, it decides to reload the value of _gmtime.tm2 into HL, even though HL will already always be set to that value.

struct tm *gmtime(const time_t *tp);
struct tm *localtime(const time_t *timer) {
    time_t timer2 = *timer;
    // subtracts zero since LOCALTIME_GMT_OFFSET is zero
    timer2 -= LOCALTIME_GMT_OFFSET * SECS_PER_MIN;
    return gmtime(&timer2);
}

Same translation unit as gmtime:

BB0_27:
        ld      (_gmtime.tm2), bc
        ld      hl, _gmtime.tm2
        ld      sp, ix
        pop     ix
        ret

        public  _localtime
_localtime:
        ld      hl, -4
        call    __frameset
        ld      iy, (ix + 6)
        ld      hl, (iy)
        ld      a, (iy + 3)
        ld      (ix - 4), hl
        ld      (ix - 1), a
        pea     ix - 4
        call    _gmtime
        pop     hl
        ld      hl, _gmtime.tm2
        ld      sp, ix
        pop     ix
        ret

Different translation units:

_localtime:
        ld      hl, -4
        call    __frameset
        ld      iy, (ix + 6)
        ld      hl, (iy)
        ld      a, (iy + 3)
        ld      (ix - 4), hl
        ld      (ix - 1), a
        pea     ix - 4
        call    _gmtime
        ld      sp, ix
        pop     ix
        ret
``

Metadata

Metadata

Assignees

No one assigned

    Labels

    missed optimizationMissed optimizations generate correct (no bugs) but not optimal code

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions