Skip to content

cmd/compile: range loop over []byte, []int can avoid base pointer increments #15809

@randall77

Description

@randall77
var t [256]byte

func f(b *[16]byte) {
    for i, v := range b {
        b[i] = t[v]
    }
}

compiles to:

    0x000f 00015 (tmp1.go:7)    MOVBLZX (AX), DX
    0x0012 00018 (tmp1.go:7)    LEAQ    "".t(SB), BX
    0x0019 00025 (tmp1.go:7)    MOVBLZX (BX)(DX*1), DX
    0x001d 00029 (tmp1.go:7)    MOVQ    "".b+8(FP), BX
    0x0022 00034 (tmp1.go:7)    MOVB    DL, (BX)(CX*1)
    0x0025 00037 (tmp1.go:6)    INCQ    AX
    0x0028 00040 (tmp1.go:6)    INCQ    CX
    0x002b 00043 (tmp1.go:6)    CMPQ    CX, $16
    0x002f 00047 (tmp1.go:6)    JLT $0, 15

It looks like we strength-reduced the index into b into a pointer increment. But the terminating condition still uses the integer, so we end up having 2 induction variables instead of one. We should either compute the terminating condition using an end pointer (and throw away the induction variable CX), or throw away the pointer induction variable (AX) and use an indexed load.

@brtzsnr

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions