-
Notifications
You must be signed in to change notification settings - Fork 13.5k
[InstCombine] optimize powi(X,Y) * X with Ofast #69998
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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 |
---|---|---|
|
@@ -125,22 +125,55 @@ entry: | |
ret double %mul | ||
} | ||
|
||
define double @powi_fmul_powi_no_reassoc(double %x, i32 %y, i32 %z) { | ||
; CHECK-LABEL: @powi_fmul_powi_no_reassoc( | ||
; Negative test: Missing reassoc flag on fmul | ||
define double @powi_fmul_powi_no_reassoc1(double %x, i32 %y, i32 %z) { | ||
; CHECK-LABEL: @powi_fmul_powi_no_reassoc1( | ||
; CHECK-NEXT: entry: | ||
; CHECK-NEXT: [[P1:%.*]] = tail call double @llvm.powi.f64.i32(double [[X:%.*]], i32 [[Y:%.*]]) | ||
; CHECK-NEXT: [[P2:%.*]] = tail call double @llvm.powi.f64.i32(double [[X]], i32 [[Z:%.*]]) | ||
; CHECK-NEXT: [[P1:%.*]] = tail call reassoc double @llvm.powi.f64.i32(double [[X:%.*]], i32 [[Y:%.*]]) | ||
; CHECK-NEXT: [[P2:%.*]] = tail call reassoc double @llvm.powi.f64.i32(double [[X]], i32 [[Z:%.*]]) | ||
; CHECK-NEXT: [[MUL:%.*]] = fmul double [[P2]], [[P1]] | ||
; CHECK-NEXT: ret double [[MUL]] | ||
; | ||
entry: | ||
%p1 = tail call double @llvm.powi.f64.i32(double %x, i32 %y) | ||
%p2 = tail call double @llvm.powi.f64.i32(double %x, i32 %z) | ||
%p1 = tail call reassoc double @llvm.powi.f64.i32(double %x, i32 %y) | ||
%p2 = tail call reassoc double @llvm.powi.f64.i32(double %x, i32 %z) | ||
%mul = fmul double %p2, %p1 | ||
ret double %mul | ||
} | ||
|
||
; Negative test: Missing reassoc flag on 2nd operand | ||
define double @powi_fmul_powi_no_reassoc2(double %x, i32 %y, i32 %z) { | ||
; CHECK-LABEL: @powi_fmul_powi_no_reassoc2( | ||
; CHECK-NEXT: entry: | ||
; CHECK-NEXT: [[P1:%.*]] = tail call reassoc double @llvm.powi.f64.i32(double [[X:%.*]], i32 [[Y:%.*]]) | ||
; CHECK-NEXT: [[P2:%.*]] = tail call double @llvm.powi.f64.i32(double [[X]], i32 [[Z:%.*]]) | ||
; CHECK-NEXT: [[MUL:%.*]] = fmul reassoc double [[P2]], [[P1]] | ||
; CHECK-NEXT: ret double [[MUL]] | ||
; | ||
entry: | ||
%p1 = tail call reassoc double @llvm.powi.f64.i32(double %x, i32 %y) | ||
%p2 = tail call double @llvm.powi.f64.i32(double %x, i32 %z) | ||
%mul = fmul reassoc double %p2, %p1 | ||
ret double %mul | ||
} | ||
|
||
; Negative test: Missing reassoc flag on 1st operand | ||
define double @powi_fmul_powi_no_reassoc3(double %x, i32 %y, i32 %z) { | ||
; CHECK-LABEL: @powi_fmul_powi_no_reassoc3( | ||
; CHECK-NEXT: entry: | ||
; CHECK-NEXT: [[P1:%.*]] = tail call double @llvm.powi.f64.i32(double [[X:%.*]], i32 [[Y:%.*]]) | ||
; CHECK-NEXT: [[P2:%.*]] = tail call reassoc double @llvm.powi.f64.i32(double [[X]], i32 [[Z:%.*]]) | ||
; CHECK-NEXT: [[MUL:%.*]] = fmul reassoc double [[P2]], [[P1]] | ||
; CHECK-NEXT: ret double [[MUL]] | ||
; | ||
entry: | ||
%p1 = tail call double @llvm.powi.f64.i32(double %x, i32 %y) | ||
%p2 = tail call reassoc double @llvm.powi.f64.i32(double %x, i32 %z) | ||
%mul = fmul reassoc double %p2, %p1 | ||
ret double %mul | ||
} | ||
|
||
; All of the fmul and its operands should have the reassoc flags | ||
define double @powi_fmul_powi(double %x, i32 %y, i32 %z) { | ||
; CHECK-LABEL: @powi_fmul_powi( | ||
; CHECK-NEXT: entry: | ||
|
@@ -149,8 +182,8 @@ define double @powi_fmul_powi(double %x, i32 %y, i32 %z) { | |
; CHECK-NEXT: ret double [[MUL]] | ||
; | ||
entry: | ||
%p1 = tail call double @llvm.powi.f64.i32(double %x, i32 %y) | ||
%p2 = tail call double @llvm.powi.f64.i32(double %x, i32 %z) | ||
%p1 = tail call reassoc double @llvm.powi.f64.i32(double %x, i32 %y) | ||
%p2 = tail call reassoc double @llvm.powi.f64.i32(double %x, i32 %z) | ||
%mul = fmul reassoc double %p2, %p1 | ||
ret double %mul | ||
} | ||
|
@@ -163,8 +196,8 @@ define double @powi_fmul_powi_fast_on_fmul(double %x, i32 %y, i32 %z) { | |
; CHECK-NEXT: ret double [[MUL]] | ||
; | ||
entry: | ||
%p1 = tail call double @llvm.powi.f64.i32(double %x, i32 %y) | ||
%p2 = tail call double @llvm.powi.f64.i32(double %x, i32 %z) | ||
%p1 = tail call fast double @llvm.powi.f64.i32(double %x, i32 %y) | ||
%p2 = tail call fast double @llvm.powi.f64.i32(double %x, i32 %z) | ||
%mul = fmul fast double %p2, %p1 | ||
ret double %mul | ||
} | ||
|
@@ -192,42 +225,57 @@ define double @powi_fmul_powi_same_power(double %x, i32 %y, i32 %z) { | |
; CHECK-NEXT: ret double [[MUL]] | ||
; | ||
entry: | ||
%p1 = tail call double @llvm.powi.f64.i32(double %x, i32 %y) | ||
%p2 = tail call double @llvm.powi.f64.i32(double %x, i32 %y) | ||
%p1 = tail call reassoc double @llvm.powi.f64.i32(double %x, i32 %y) | ||
%p2 = tail call reassoc double @llvm.powi.f64.i32(double %x, i32 %y) | ||
%mul = fmul reassoc double %p2, %p1 | ||
ret double %mul | ||
} | ||
|
||
define double @powi_fmul_powi_different_integer_types(double %x, i32 %y, i16 %z) { | ||
; CHECK-LABEL: @powi_fmul_powi_different_integer_types( | ||
; CHECK-NEXT: entry: | ||
; CHECK-NEXT: [[P1:%.*]] = tail call reassoc double @llvm.powi.f64.i32(double [[X:%.*]], i32 [[Y:%.*]]) | ||
; CHECK-NEXT: [[P2:%.*]] = tail call reassoc double @llvm.powi.f64.i16(double [[X]], i16 [[Z:%.*]]) | ||
; CHECK-NEXT: [[MUL:%.*]] = fmul reassoc double [[P2]], [[P1]] | ||
; CHECK-NEXT: ret double [[MUL]] | ||
; | ||
entry: | ||
%p1 = tail call reassoc double @llvm.powi.f64.i32(double %x, i32 %y) | ||
%p2 = tail call reassoc double @llvm.powi.f64.i16(double %x, i16 %z) | ||
%mul = fmul reassoc double %p2, %p1 | ||
ret double %mul | ||
} | ||
|
||
define double @powi_fmul_powi_use_first(double %x, i32 %y, i32 %z) { | ||
; CHECK-LABEL: @powi_fmul_powi_use_first( | ||
; CHECK-NEXT: entry: | ||
; CHECK-NEXT: [[P1:%.*]] = tail call double @llvm.powi.f64.i32(double [[X:%.*]], i32 [[Y:%.*]]) | ||
; CHECK-NEXT: [[P1:%.*]] = tail call reassoc double @llvm.powi.f64.i32(double [[X:%.*]], i32 [[Y:%.*]]) | ||
; CHECK-NEXT: tail call void @use(double [[P1]]) | ||
; CHECK-NEXT: [[TMP0:%.*]] = add i32 [[Y]], [[Z:%.*]] | ||
; CHECK-NEXT: [[MUL:%.*]] = call reassoc double @llvm.powi.f64.i32(double [[X]], i32 [[TMP0]]) | ||
; CHECK-NEXT: ret double [[MUL]] | ||
; | ||
entry: | ||
%p1 = tail call double @llvm.powi.f64.i32(double %x, i32 %y) | ||
%p1 = tail call reassoc double @llvm.powi.f64.i32(double %x, i32 %y) | ||
tail call void @use(double %p1) | ||
%p2 = tail call double @llvm.powi.f64.i32(double %x, i32 %z) | ||
%p2 = tail call reassoc double @llvm.powi.f64.i32(double %x, i32 %z) | ||
%mul = fmul reassoc double %p1, %p2 | ||
ret double %mul | ||
} | ||
|
||
define double @powi_fmul_powi_use_second(double %x, i32 %y, i32 %z) { | ||
; CHECK-LABEL: @powi_fmul_powi_use_second( | ||
; CHECK-NEXT: entry: | ||
; CHECK-NEXT: [[P1:%.*]] = tail call double @llvm.powi.f64.i32(double [[X:%.*]], i32 [[Z:%.*]]) | ||
; CHECK-NEXT: [[P1:%.*]] = tail call reassoc double @llvm.powi.f64.i32(double [[X:%.*]], i32 [[Z:%.*]]) | ||
; CHECK-NEXT: tail call void @use(double [[P1]]) | ||
; CHECK-NEXT: [[TMP0:%.*]] = add i32 [[Y:%.*]], [[Z]] | ||
; CHECK-NEXT: [[MUL:%.*]] = call reassoc double @llvm.powi.f64.i32(double [[X]], i32 [[TMP0]]) | ||
; CHECK-NEXT: ret double [[MUL]] | ||
; | ||
entry: | ||
%p1 = tail call double @llvm.powi.f64.i32(double %x, i32 %z) | ||
%p1 = tail call reassoc double @llvm.powi.f64.i32(double %x, i32 %z) | ||
tail call void @use(double %p1) | ||
%p2 = tail call double @llvm.powi.f64.i32(double %x, i32 %y) | ||
%p2 = tail call reassoc double @llvm.powi.f64.i32(double %x, i32 %y) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we don't have a test where the 2 powi declarations have different integer types, should add one |
||
%mul = fmul reassoc double %p2, %p1 | ||
ret double %mul | ||
} | ||
|
@@ -333,11 +381,60 @@ define double @fdiv_pow_powi_negative(double %x) { | |
; Negative test: The 2nd powi argument is a variable | ||
define double @fdiv_pow_powi_negative_variable(double %x, i32 %y) { | ||
; CHECK-LABEL: @fdiv_pow_powi_negative_variable( | ||
; CHECK-NEXT: [[P1:%.*]] = call double @llvm.powi.f64.i32(double [[X:%.*]], i32 [[Y:%.*]]) | ||
; CHECK-NEXT: [[P1:%.*]] = call reassoc double @llvm.powi.f64.i32(double [[X:%.*]], i32 [[Y:%.*]]) | ||
; CHECK-NEXT: [[DIV:%.*]] = fdiv reassoc nnan double [[P1]], [[X]] | ||
; CHECK-NEXT: ret double [[DIV]] | ||
; | ||
%p1 = call double @llvm.powi.f64.i32(double %x, i32 %y) | ||
%p1 = call reassoc double @llvm.powi.f64.i32(double %x, i32 %y) | ||
%div = fdiv reassoc nnan double %p1, %x | ||
ret double %div | ||
} | ||
|
||
; powi(X, Y) * X --> powi(X, Y+1) | ||
define double @powi_fmul_powi_x(double noundef %x) { | ||
; CHECK-LABEL: @powi_fmul_powi_x( | ||
; CHECK-NEXT: [[MUL:%.*]] = call reassoc double @llvm.powi.f64.i32(double [[X:%.*]], i32 4) | ||
; CHECK-NEXT: ret double [[MUL]] | ||
; | ||
%p1 = tail call reassoc double @llvm.powi.f64.i32(double %x, i32 3) | ||
%mul = fmul reassoc double %p1, %x | ||
ret double %mul | ||
} | ||
|
||
; Negative test: Multi-use | ||
define double @powi_fmul_powi_x_multi_use(double noundef %x) { | ||
; CHECK-LABEL: @powi_fmul_powi_x_multi_use( | ||
; CHECK-NEXT: [[P1:%.*]] = tail call double @llvm.powi.f64.i32(double [[X:%.*]], i32 3) | ||
; CHECK-NEXT: tail call void @use(double [[P1]]) | ||
; CHECK-NEXT: [[MUL:%.*]] = fmul reassoc double [[P1]], [[X]] | ||
; CHECK-NEXT: ret double [[MUL]] | ||
; | ||
%p1 = tail call double @llvm.powi.f64.i32(double %x, i32 3) | ||
tail call void @use(double %p1) | ||
%mul = fmul reassoc double %p1, %x | ||
ret double %mul | ||
} | ||
|
||
; Negative test: Miss fmf flag | ||
define double @powi_fmul_powi_x_missing_reassoc(double noundef %x) { | ||
; CHECK-LABEL: @powi_fmul_powi_x_missing_reassoc( | ||
; CHECK-NEXT: [[P1:%.*]] = tail call double @llvm.powi.f64.i32(double [[X:%.*]], i32 3) | ||
; CHECK-NEXT: [[MUL:%.*]] = fmul double [[P1]], [[X]] | ||
; CHECK-NEXT: ret double [[MUL]] | ||
; | ||
%p1 = tail call double @llvm.powi.f64.i32(double %x, i32 3) | ||
%mul = fmul double %p1, %x | ||
ret double %mul | ||
} | ||
|
||
; Negative test: overflow | ||
define double @powi_fmul_powi_x_overflow(double noundef %x) { | ||
; CHECK-LABEL: @powi_fmul_powi_x_overflow( | ||
; CHECK-NEXT: [[P1:%.*]] = tail call double @llvm.powi.f64.i32(double [[X:%.*]], i32 2147483647) | ||
; CHECK-NEXT: [[MUL:%.*]] = fmul reassoc double [[P1]], [[X]] | ||
; CHECK-NEXT: ret double [[MUL]] | ||
; | ||
%p1 = tail call double @llvm.powi.f64.i32(double %x, i32 2147483647) ; INT_MAX | ||
%mul = fmul reassoc double %p1, %x | ||
ret double %mul | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This isn't quite the API I had in my mind. I envisioned the required flags as a template parameter to the existing matchers, so you could have something like:
m_FMul<Reassoc>(), m_Intrinsic<powi, Reassoc>
However I wasn't expecting you to do anything for this in this patch. This is fine for now