-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[MergeICmps] Merge adjacent comparisons to constants #133817
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
Open
PhilippRados
wants to merge
23
commits into
llvm:main
Choose a base branch
from
PhilippRados:bits_all_ones_opt
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
+1,504
−209
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
… adjacent memory blocks with constants
…cond to last inst holds cond-result
…; Is deleted when folded during expand-memcmp pass
Contributor
Author
|
@nikic Can you review this PR (since you commented on the original issue) or can you add someone else to look at it? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This pull request aims to fix #117853.
General idea
It extends the existing
MergeICmpspass to not only merge comparisons like:a.a == b.a && a.b == b.bbut also comparisons with arbitrary constants such asa.a == 245 && a.b == -1.Changes
Since the original pass only worked under the assumption that a single comparison could happen per basic block this had to be altered to allow multiple comparisons in a single basic block. This is because constant comparisons get flattened into a single block using a
selectinstruction before theMergeICmpspass is run.How it works
Whenever a matching comparison is encountered it adds it to the cmp-chain. Then when all comparisons have been found it
sorts them meaning all const-comparisons are followed by all bce-comparisons (depends on which was first). Then it goes through all comparisons in the chain and merges the ones adjacent to each other. Comparisons inside a flattened select-block can only be merged if every comparison in that block is merged (this is a rather defensive approach).
The const merging works by building a global constant struct for every merge. This needs to be a global-const in order to be constant folded by the expand-memcmp pass where it is then also removed.
Example
A single comparison chain can now be made up of both BCE-comparisons (two offsets to the same base) and const-comparisons (contiguous offsets to the same base with a constant).
This means that the expression:
can be turned into:
Issues in the current implementation, waiting for feedback on this