Skip to content

JIT sometimes emits unnecessary movsxd instructions when performing logical operations against consts #32089

Closed

Description

@GrabYourPitchforks

Repro, courtesy sharplab:

public static bool CompareAgainstConstInt(int a) {
    return (a & 0x40) == 0;
}
    
public static bool CompareAgainstConstUInt(int a) {
    return (a & 0x40u) == 0;
}
; C.CompareAgainstConstInt(Int32)
    L0000: test cl, 0x40
    L0003: setz al
    L0006: movzx eax, al
    L0009: ret

; C.CompareAgainstConstUInt(Int32)
    L0000: movsxd rax, ecx   ; <-- this is unnecessary
    L0003: test al, 0x40
    L0005: setz al
    L0008: movzx eax, al
    L000b: ret

category:cq
theme:optimization
skill-level:intermediate
cost:medium

Activity

  1. GrabYourPitchforks commented on Feb 11, 2020

    @GrabYourPitchforks
    MemberAuthor

    The Roslyn behavior makes sense to me, so I don't think an analyzer is needed. What this issue tracks is that I was hoping the JIT could recognize that the sign-extension was throwaway work (since the comparison is against a constant) and that it could avoid emitting the instruction.

    You can also see this with other types like byte and short, not just int:

    public static bool Foo(byte i) {
        return (i & 0x20) == 0;
    }
    ; C.Foo(Byte)
        L0000: movzx eax, cl  ; <-- not needed
        L0003: test al, 0x20
        L0005: setz al
        L0008: movzx eax, al
        L000b: ret
  2. AndyAyersMS commented on Feb 11, 2020

    @AndyAyersMS
    Member

    This is fairly endemic; the jit does not do the analysis to determine when entire trees of computation can be safely and more profitably done on a smaller (or sometimes, larger) type.

  3. AndyAyersMS commented on Feb 13, 2020

    @AndyAyersMS
    Member

    I can't find any overall issue talking about narrowing/widening -- @dotnet/jit-contrib anyone know of one?

  4. removed
    untriagedNew issue has not been triaged by the area owner
    on Feb 18, 2020
  5. added this to the Future milestone on Feb 18, 2020
  6. BruceForstall commented on Oct 30, 2022

    @BruceForstall
    Contributor

    Going to close this in favor of #32504

  7. ghost locked as resolved and limited conversation to collaborators on Nov 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMIoptimization

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions