Skip to content

Conversation

@jameshu15869
Copy link
Contributor

Collaborators: @claby2 @bisheshank

Cranelift may generate code where a select followed by an icmp can be directly substituted with the select instruction's condition. Specifically, we can perform this optimization when we encounter code of the form

...
v1 = select v0, k1, k2
v2 = icmp eq v1, k1
...

Here, we can optimize to v2 = v0 given that k1 and k2 are constants.

In the language of the example above, this PR handles icmp eq and icmp ne when k1 or k2 are the icmp condition. We use the ISLE rewrite icmp ne x 0 as a "cast" to handle the case where the inner condition of the select is not restricted to 0 or 1 (See non_icmp_inner in issue-11578-semantics.clif)

Fixes #11578

jameshu15869 and others added 4 commits November 30, 2025 22:37
This optimizes the following:

a = select x, k1, k2
b = icmp eq a, k1

to

b = x

We shouldn't trigger this optimization when k1 == k2 because constant
propagation should optimize that case.
Implement simple icmp + select optimization
* Optimize icmp eq and ne for select-on-icmp

When we have a select followed by an icmp and the inner condition
to the select is also an icmp, we can remove the select + icmp and
directly use the inner condition.

We negated the values of the inner_condition via bxor x 1.

* Add trailing newline

* feat: avoid relying on icmp inner_cond

---------

Co-authored-by: school <[email protected]>
Co-authored-by: Edward Wibowo <[email protected]>
@jameshu15869 jameshu15869 requested a review from a team as a code owner December 7, 2025 19:48
@jameshu15869 jameshu15869 requested review from cfallin and removed request for a team December 7, 2025 19:48
@jameshu15869 jameshu15869 changed the title Cranelift: Remove redundant select + icmp instructions Cranelift: Optimize out redundant select + icmp instructions Dec 7, 2025
@github-actions github-actions bot added cranelift Issues related to the Cranelift code generator isle Related to the ISLE domain-specific language labels Dec 7, 2025
@github-actions
Copy link

github-actions bot commented Dec 7, 2025

Subscribe to Label Action

cc @cfallin, @fitzgen

Details This issue or pull request has been labeled: "cranelift", "isle"

Thus the following users have been cc'd because of the following labels:

  • cfallin: isle
  • fitzgen: isle

To subscribe or unsubscribe from this label, edit the .github/subscribe-to-label.json configuration file.

Learn more.

Copy link
Member

@cfallin cfallin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@cfallin cfallin added this pull request to the merge queue Dec 8, 2025
Merged via the queue into bytecodealliance:main with commit cdc7bfe Dec 8, 2025
45 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cranelift Issues related to the Cranelift code generator isle Related to the ISLE domain-specific language

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cranelift: missing optimization on icmp + select + icmp + brif

3 participants