Skip to content

Conversation

@cdleary
Copy link
Collaborator

@cdleary cdleary commented Jan 17, 2026

Rewrites via the observation that ~x and x+1 are "zero-equivalent".

  // Pattern:
  //   `(eq|ne)(sel(c, cases=[not(x), add(x, 1)]), 0)` =>
  //   `(eq|ne)(x, all_ones(width(x)))`
  //
  // Why this works (not/inc zero-equivalence):
  // - `(~x == 0)`  <=>  `(x == all_ones)`
  // - `(x+1 == 0)` <=>  `(x == all_ones)`   (note: given wraparound arithmetic)
  // Therefore for `y = sel(c, [~x, x+1])`, `(y != 0)` is exactly `(x !=
  // all_ones)`, independent of selector `c`.

@cdleary cdleary marked this pull request as ready for review January 17, 2026 22:13
Copy link
Contributor

@allight allight left a comment

Choose a reason for hiding this comment

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

This seems like a weird piece of code to end up with in normal compilation. I wonder if there's some more generic way to recognize cases like these.

LGTM though.

@copybara-service copybara-service bot merged commit 7d688fc into google:main Jan 20, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants