Skip to content

cmd/compile: regalloc restoring to dead register #14761

Closed
@randall77

Description

@randall77
func f(p *int, v int, b bool) int {
    if b {
        *p = v
    }
    runtime.GC()
    return *p
}

This compiles to:

    0x000f 00015 (/home/khr/go/tmp2.go:5)   MOVBLZX "".b+24(FP), AX
    0x0014 00020 (/home/khr/go/tmp2.go:5)   TESTB   AL, AL
    0x0016 00022 (/home/khr/go/tmp2.go:6)   JEQ 56
    0x0018 00024 (/home/khr/go/tmp2.go:7)   MOVQ    "".p+8(FP), CX
    0x001d 00029 (/home/khr/go/tmp2.go:7)   MOVQ    "".v+16(FP), DX
    0x0022 00034 (/home/khr/go/tmp2.go:7)   MOVQ    DX, (CX)
    0x0025 00037 (/home/khr/go/tmp2.go:9)   PCDATA  $0, $0
    0x0025 00037 (/home/khr/go/tmp2.go:9)   CALL    runtime.GC(SB)
        ...
    0x0038 00056 (/home/khr/go/tmp2.go:5)   MOVQ    "".p+8(FP), CX
    0x003d 00061 (/home/khr/go/tmp2.go:9)   JMP 37

Notice the restore of p at 56 which is unnecessary, as CX is dead. This is just the register allocator trying to get the same data in the same registers as the other predecessor of instruction 37. We currently match only the values in registers which are live. But we really should match values in registers which are live and which are used at least once.

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