Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[InstCombine] Missed optimization in math expression: squashing sqrt …
…functions Summary: This patch enables folding under -ffast-math flag sqrt(a) * sqrt(b) -> sqrt(a*b) Reviewers: hfinkel, spatel, davide Reviewed By: spatel, davide Subscribers: davide, llvm-commits Differential Revision: https://reviews.llvm.org/D41322 llvm-svn: 321637
- Loading branch information
Showing
2 changed files
with
84 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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,67 @@ | ||
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py | ||
; RUN: opt -S -instcombine < %s | FileCheck %s | ||
|
||
declare double @llvm.sqrt.f64(double) nounwind readnone speculatable | ||
declare void @use(double) | ||
|
||
; sqrt(a) * sqrt(b) no math flags | ||
define double @sqrt_a_sqrt_b(double %a, double %b) { | ||
; CHECK-LABEL: @sqrt_a_sqrt_b( | ||
; CHECK-NEXT: [[TMP1:%.*]] = call double @llvm.sqrt.f64(double [[A:%.*]]) | ||
; CHECK-NEXT: [[TMP2:%.*]] = call double @llvm.sqrt.f64(double [[B:%.*]]) | ||
; CHECK-NEXT: [[MUL:%.*]] = fmul double [[TMP1]], [[TMP2]] | ||
; CHECK-NEXT: ret double [[MUL]] | ||
; | ||
%1 = call double @llvm.sqrt.f64(double %a) | ||
%2 = call double @llvm.sqrt.f64(double %b) | ||
%mul = fmul double %1, %2 | ||
ret double %mul | ||
} | ||
|
||
; sqrt(a) * sqrt(b) fast-math, multiple uses | ||
define double @sqrt_a_sqrt_b_multiple_uses(double %a, double %b) { | ||
; CHECK-LABEL: @sqrt_a_sqrt_b_multiple_uses( | ||
; CHECK-NEXT: [[TMP1:%.*]] = call fast double @llvm.sqrt.f64(double [[A:%.*]]) | ||
; CHECK-NEXT: [[TMP2:%.*]] = call fast double @llvm.sqrt.f64(double [[B:%.*]]) | ||
; CHECK-NEXT: [[MUL:%.*]] = fmul fast double [[TMP1]], [[TMP2]] | ||
; CHECK-NEXT: call void @use(double [[TMP2]]) | ||
; CHECK-NEXT: ret double [[MUL]] | ||
; | ||
%1 = call fast double @llvm.sqrt.f64(double %a) | ||
%2 = call fast double @llvm.sqrt.f64(double %b) | ||
%mul = fmul fast double %1, %2 | ||
call void @use(double %2) | ||
ret double %mul | ||
} | ||
|
||
; sqrt(a) * sqrt(b) => sqrt(a*b) with fast-math | ||
define double @sqrt_a_sqrt_b_fast(double %a, double %b) { | ||
; CHECK-LABEL: @sqrt_a_sqrt_b_fast( | ||
; CHECK-NEXT: [[TMP1:%.*]] = fmul fast double [[A:%.*]], [[B:%.*]] | ||
; CHECK-NEXT: [[TMP2:%.*]] = call fast double @llvm.sqrt.f64(double [[TMP1]]) | ||
; CHECK-NEXT: ret double [[TMP2]] | ||
; | ||
%1 = call fast double @llvm.sqrt.f64(double %a) | ||
%2 = call fast double @llvm.sqrt.f64(double %b) | ||
%mul = fmul fast double %1, %2 | ||
ret double %mul | ||
} | ||
|
||
; sqrt(a) * sqrt(b) * sqrt(c) * sqrt(d) => sqrt(a*b*c+d) with fast-math | ||
define double @sqrt_a_sqrt_b_sqrt_c_sqrt_d_fast(double %a, double %b, double %c, double %d) { | ||
; CHECK-LABEL: @sqrt_a_sqrt_b_sqrt_c_sqrt_d_fast( | ||
; CHECK-NEXT: [[TMP1:%.*]] = fmul fast double [[A:%.*]], [[B:%.*]] | ||
; CHECK-NEXT: [[TMP2:%.*]] = fmul fast double [[TMP1]], [[C:%.*]] | ||
; CHECK-NEXT: [[TMP3:%.*]] = fmul fast double [[TMP2]], [[D:%.*]] | ||
; CHECK-NEXT: [[TMP4:%.*]] = call fast double @llvm.sqrt.f64(double [[TMP3]]) | ||
; CHECK-NEXT: ret double [[TMP4]] | ||
; | ||
%1 = call fast double @llvm.sqrt.f64(double %a) | ||
%2 = call fast double @llvm.sqrt.f64(double %b) | ||
%mul = fmul fast double %1, %2 | ||
%3 = call fast double @llvm.sqrt.f64(double %c) | ||
%mul1 = fmul fast double %mul, %3 | ||
%4 = call fast double @llvm.sqrt.f64(double %d) | ||
%mul2 = fmul fast double %mul1, %4 | ||
ret double %mul2 | ||
} |