Skip to content

cmd/internal/obj: remove MOV $0 -> XOR "optimization" #22325

@aclements

Description

@aclements

Currently cmd/internal/obj replaces MOV* $0, r with XOR* r, r for 386 and amd64 as an "optimization". However, this is not a semantics-preserving transformation since XOR clobbers registers, while MOV does not.

This has caused pain in several places:

  1. The compiler sets a special PRESERVEFLAGS bit when it creates MOV Progs at points where the flags are live.

  2. The assembly in math/big has the ominous comment "CAUTION: Note that MOVQ $0, Rx is translated to XORQ Rx, Rx which clears the carry bit!".

  3. It caused bugs in CL 71270 that were very difficult to track down and required restructuring the assembly to work around.

We should consider removing this transformation from obj and instead just doing it in the compiler. There are only a handful of instances of this in hand-written assembly in the standard library. We could rewrite these to XOR by hand, or just not bother.

/cc @randall77 @iant

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions