Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify applying bitwise operators on sign extended values #1111

Closed

Conversation

can-leh-emmtrix
Copy link
Contributor

Since the result of comparison and logical operators in C is of type int, complex logical expressions may result in expression trees where sext, extract and logical operators are used together (especially when the programmer uses temporary variables of type int to store boolean values and instcombine has not been executed).

This PR introduces the following simplifications for bit vectors of the same width a and b:

sext(a) & sext(b) -> sext(a & b)
sext(a) | sext(b) -> sext(a | b)
sext(a) ^ sext(b) -> sext(a ^ b)
~sext(a) -> sext(~a)

The pattern extract(sext(x), ...) is already handled by the existing simplifications.

If this breaks other important cases, it might be possible to enforce that a.bits() = b.bits() = 1, although I have personally seen a major impact anywhere else yet.

@nunoplopes
Copy link
Member

These rewrite may increase the size of the formula, by adding an additional sext, hence it's not always a win. In general we try to only have local & global optimal rewrites to avoid memory & perf impact.
Do you have any performance numbers?

@nunoplopes
Copy link
Member

I've run a bunch of benchmarks over the weekend and this patch doesn't help. Most benchmarks get a bit slower actually.

@nunoplopes nunoplopes closed this Dec 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants