Skip to content

x64: clean up br_if(op(a, b)) codegen #11125

Closed

Description

@fitzgen

Follow up issue to #11122

The semantics of br_if are such that we branch to the consequent on a non-zero condition value and the alternative on a zero condition value. Often we are emitting x64 instructions to to the op and then explicitly test whether the result is zero or non-zero and branch accordingly. However, for many ops we can instead just do some kind of test/compare on a and b directly, instead of the op and then a non-zero test.

For example given

    local.get 0
    local.get 1
    i32.or
    if ;; label = @1
      call 0
    else
      call 1
    end

we currently emit the comparison as

00000019    0b d1                             or edx, ecx
0000001b    85 d2                             test edx, edx
0000001d    0f 85 15 00 00 00                 jne 0x38
...

but we should be able to do something like

or edx, ecx
je 0x38
...

instead.

I think that we can add special cases for br_if(op(a, b)) for all ops in {iadd, isub, bor, bxor}. Maybe more. (Note that we already have a br_if(band(a, b)) special case).

Note that doing this might require some additional refactorings in the x64 assembler library and its ISLE constructor generation: #11122 (comment)

Activity

  1. added
    cranelift
    cranelift:goal:optimize-speed
    cranelift:area:x64
    on Jun 24, 2025
  2. added a commit that references this issue on Nov 16, 2026
    c45892c
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

    craneliftcranelift:area:x64cranelift:goal:optimize-speed

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions