Skip to content

JIT could further optimize "== 0" checks in Boolean logic #13573

Closed
@GrabYourPitchforks

Description

@GrabYourPitchforks

There are some well-known bit manipulation patterns that the JIT doesn't currently optimize. For instance:

public static bool AreZero(int x, int y) {
    return x == 0 && y == 0;
}

Produces this codegen:

AreZero(Int32, Int32)
    L0000: test ecx, ecx
    L0002: jnz L000d
    L0004: test edx, edx
    L0006: setz al
    L0009: movzx eax, al
    L000c: ret
    L000d: xor eax, eax
    L000f: ret

But it could produce this more optimal codegen, folding the two comparisons together into a single or and zero check:

AreZero(Int32, Int32)
    L0000: or edx, ecx
    L0002: setz al
    L0005: movzx eax, al
    L0008: ret

category:cq
theme:basic-cq
skill-level:beginner
cost:small

Metadata

Metadata

Assignees

Labels

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

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions