Skip to content

[InstCombine] Missed optimization for the pattern ((a << 1) + b) - ((a+b) & a) #71948

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

Closed
XChy opened this issue Nov 10, 2023 · 4 comments
Closed
Labels
llvm:instcombine missed-optimization wontfix Issue is real, but we can't or won't fix it. Not invalid

Comments

@XChy
Copy link
Member

XChy commented Nov 10, 2023

((a << 1) + b) - ((a + b) & a) can be folded to (a + b) | a.
Missed example: https://godbolt.org/z/boz84e85j
Alive2 Proof: https://alive2.llvm.org/ce/z/j_lETs
Let me know if you think it's an optimization opportunity, please.

@XChy XChy changed the title [InstCombine] Missed optimization for the pattern ((a+b) & a) - ((a << 1) + b) [InstCombine] Missed optimization for the pattern ((a << 1) + b) - ((a+b) & a) Nov 10, 2023
@dtcxzyw
Copy link
Member

dtcxzyw commented Nov 11, 2023

What is your motivation for this pattern? Does it come from a real-world application?

@XChy
Copy link
Member Author

XChy commented Nov 11, 2023

What is your motivation for this pattern? Does it come from a real-world application?

No real-world motivation now. Actually, we are developing something like https://github.com/google/souper, and perform initial trials on peephole optimization. This issue is one of the results. Your feedback about what can be defined as missed optimization opportunity is important to me. Thanks.

@nikic
Copy link
Contributor

nikic commented Nov 11, 2023

The most reliable way to find "interesting" patterns is probably to start with real-world IR and find unoptimized patterns in it, rather than the other way around (i.e. generating patterns and then trying to determine whether they are interesting). That way you have positive proof that the pattern will improve at least one piece of real code...

But assuming we don't have any knowledge of real-world relevance, I think one criterion to look at is pattern complexity vs specificity. The pattern here is a complex one (involving five binary operators with exact operand requirements) and one that, as far as I can tell, doesn't really generalize to anything. That's the kind of pattern we usually do not want.

Compare this for example to #71789, which also has a very specific pattern that probably doesn't generalize, but it's also a very simple one.

Or compare it to #71533. The specific pattern there wouldn't be acceptable, but if you see this generalized to "KnownBits information implied by select used to simplify select arms", then that becomes a reasonable pattern to handle (though probably also technically also quite challenging).

@XChy
Copy link
Member Author

XChy commented Nov 11, 2023

@nikic @dtcxzyw, thanks for your feedback and explanation! It really helps with improving our work and methods. I will consider more about real-world application, complexity and specificity in future.

@XChy XChy added the wontfix Issue is real, but we can't or won't fix it. Not invalid label Nov 11, 2023
@XChy XChy closed this as completed Nov 11, 2023
@EugeneZelenko EugeneZelenko closed this as not planned Won't fix, can't repro, duplicate, stale Nov 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
llvm:instcombine missed-optimization wontfix Issue is real, but we can't or won't fix it. Not invalid
Projects
None yet
Development

No branches or pull requests

4 participants