-
Notifications
You must be signed in to change notification settings - Fork 14.9k
[DAG] Optimize Constant Xor And And Not Operation #161784
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
manik-muk
wants to merge
10
commits into
llvm:main
Choose a base branch
from
manik-muk:optimizeConstantXorAndAndNot
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
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
af5dcb5
added optimization and tests
manik-muk b176fd6
Move constant XOR AND ANDNOT optimization to generic DAG combiner
manik-muk 4a2e546
changed combiner logic to account for infinite loops
manik-muk 7fb0e39
refactored to remove goto
manik-muk d9c1a75
addressed comments
manik-muk be724f2
Merge branch 'main' into optimizeConstantXorAndAndNot
manik-muk 6fcb51f
only changed modified lines of code
manik-muk 9022deb
refactored to use sd match
manik-muk 7e6ffa7
addressed comments
manik-muk de88c96
made use case more generalizable to all similar patterns
manik-muk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py | ||
; RUN: llc -mtriple=x86_64-unknown-unknown -mattr=-bmi < %s | FileCheck %s --check-prefixes=CHECK,NOBMI | ||
; RUN: llc -mtriple=x86_64-unknown-unknown -mattr=+bmi < %s | FileCheck %s --check-prefixes=CHECK,BMI | ||
|
||
; Test the optimization described in issue #161630: | ||
; (Constant XOR a) & b & ~c should compile to allow andn to be done in parallel with xor | ||
|
||
define i64 @test_constant_xor_and_andnot(i64 %a, i64 %b, i64 %c) { | ||
; NOBMI-LABEL: test_constant_xor_and_andnot: | ||
; NOBMI: # %bb.0: | ||
; NOBMI-NEXT: movq %rdx, %rax | ||
; NOBMI-NEXT: xorq $1234, %rdi # imm = 0x4D2 | ||
; NOBMI-NEXT: andq %rsi, %rdi | ||
; NOBMI-NEXT: notq %rax | ||
; NOBMI-NEXT: andq %rdi, %rax | ||
; NOBMI-NEXT: retq | ||
; | ||
; BMI-LABEL: test_constant_xor_and_andnot: | ||
; BMI: # %bb.0: | ||
; BMI-NEXT: xorq $1234, %rdi # imm = 0x4D2 | ||
; BMI-NEXT: andnq %rsi, %rdx, %rax | ||
; BMI-NEXT: andq %rdi, %rax | ||
; BMI-NEXT: retq | ||
%xor = xor i64 %a, 1234 | ||
%and1 = and i64 %xor, %b | ||
%not_c = xor i64 %c, -1 | ||
%result = and i64 %and1, %not_c | ||
ret i64 %result | ||
} | ||
|
||
define i32 @test_constant_xor_and_andnot_32(i32 %a, i32 %b, i32 %c) { | ||
; NOBMI-LABEL: test_constant_xor_and_andnot_32: | ||
; NOBMI: # %bb.0: | ||
; NOBMI-NEXT: movl %edx, %eax | ||
; NOBMI-NEXT: xorl $5678, %edi # imm = 0x162E | ||
; NOBMI-NEXT: andl %esi, %edi | ||
; NOBMI-NEXT: notl %eax | ||
; NOBMI-NEXT: andl %edi, %eax | ||
; NOBMI-NEXT: retq | ||
; | ||
; BMI-LABEL: test_constant_xor_and_andnot_32: | ||
; BMI: # %bb.0: | ||
; BMI-NEXT: xorl $5678, %edi # imm = 0x162E | ||
; BMI-NEXT: andnl %esi, %edx, %eax | ||
; BMI-NEXT: andl %edi, %eax | ||
; BMI-NEXT: retq | ||
%xor = xor i32 %a, 5678 | ||
%and1 = and i32 %xor, %b | ||
%not_c = xor i32 %c, -1 | ||
%result = and i32 %and1, %not_c | ||
ret i32 %result | ||
} | ||
|
||
; Test with different operand order | ||
define i64 @test_constant_xor_and_andnot_swapped(i64 %a, i64 %b, i64 %c) { | ||
; NOBMI-LABEL: test_constant_xor_and_andnot_swapped: | ||
; NOBMI: # %bb.0: | ||
; NOBMI-NEXT: movq %rdx, %rax | ||
; NOBMI-NEXT: xorq $1234, %rdi # imm = 0x4D2 | ||
; NOBMI-NEXT: andq %rsi, %rdi | ||
; NOBMI-NEXT: notq %rax | ||
; NOBMI-NEXT: andq %rdi, %rax | ||
; NOBMI-NEXT: retq | ||
; | ||
; BMI-LABEL: test_constant_xor_and_andnot_swapped: | ||
; BMI: # %bb.0: | ||
; BMI-NEXT: xorq $1234, %rdi # imm = 0x4D2 | ||
; BMI-NEXT: andnq %rsi, %rdx, %rax | ||
; BMI-NEXT: andq %rdi, %rax | ||
; BMI-NEXT: retq | ||
%xor = xor i64 %a, 1234 | ||
%and1 = and i64 %b, %xor | ||
%not_c = xor i64 %c, -1 | ||
%result = and i64 %and1, %not_c | ||
ret i64 %result | ||
} | ||
|
||
; Test with different operand order for the final AND | ||
define i64 @test_constant_xor_and_andnot_final_swapped(i64 %a, i64 %b, i64 %c) { | ||
; NOBMI-LABEL: test_constant_xor_and_andnot_final_swapped: | ||
; NOBMI: # %bb.0: | ||
; NOBMI-NEXT: movq %rdx, %rax | ||
; NOBMI-NEXT: xorq $1234, %rdi # imm = 0x4D2 | ||
; NOBMI-NEXT: andq %rsi, %rdi | ||
; NOBMI-NEXT: notq %rax | ||
; NOBMI-NEXT: andq %rdi, %rax | ||
; NOBMI-NEXT: retq | ||
; | ||
; BMI-LABEL: test_constant_xor_and_andnot_final_swapped: | ||
; BMI: # %bb.0: | ||
; BMI-NEXT: xorq $1234, %rdi # imm = 0x4D2 | ||
; BMI-NEXT: andnq %rsi, %rdx, %rax | ||
; BMI-NEXT: andq %rdi, %rax | ||
; BMI-NEXT: retq | ||
%xor = xor i64 %a, 1234 | ||
%and1 = and i64 %xor, %b | ||
%not_c = xor i64 %c, -1 | ||
%result = and i64 %not_c, %and1 | ||
ret i64 %result | ||
} | ||
;; NOTE: These prefixes are unused and the list is autogenerated. Do not add tests below this line: | ||
; CHECK: {{.*}} |
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
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.
Uh oh!
There was an error while loading. Please reload this page.