xref: /llvm-project/llvm/test/Transforms/InstCombine/math-odd-even-parity.ll (revision 1901f442ca6374787e6810adb573d138f80893dd)
1; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 4
2; RUN: opt < %s -passes=instcombine -S | FileCheck %s
3
4declare double @erf(double)
5declare double @cos(double)
6declare double @fabs(double)
7
8declare void @use(double) nounwind
9
10; Check odd parity: -erf(-x) == erf(x)
11define double @test_erf(double %x) {
12; CHECK-LABEL: define double @test_erf(
13; CHECK-SAME: double [[X:%.*]]) {
14; CHECK-NEXT:    [[RES:%.*]] = tail call reassoc double @erf(double [[X]])
15; CHECK-NEXT:    ret double [[RES]]
16;
17  %neg_x = fneg double %x
18  %res = tail call reassoc double @erf(double %neg_x)
19  %neg_res = fneg double %res
20  ret double %neg_res
21}
22
23; Check even parity: cos(fabs(x)) == cos(x)
24define double @test_cos_fabs(double %x) {
25; CHECK-LABEL: define double @test_cos_fabs(
26; CHECK-SAME: double [[X:%.*]]) {
27; CHECK-NEXT:    [[RES:%.*]] = tail call reassoc double @cos(double [[X]])
28; CHECK-NEXT:    ret double [[RES]]
29;
30  %fabs_res = call double @fabs(double %x)
31  %res = tail call reassoc double @cos(double %fabs_res)
32  ret double %res
33}
34
35; Do nothing in case of multi-use
36define double @test_erf_multi_use(double %x) {
37; CHECK-LABEL: define double @test_erf_multi_use(
38; CHECK-SAME: double [[X:%.*]]) {
39; CHECK-NEXT:    [[NEG_X:%.*]] = fneg double [[X]]
40; CHECK-NEXT:    call void @use(double [[NEG_X]])
41; CHECK-NEXT:    [[RES:%.*]] = call double @erf(double [[NEG_X]])
42; CHECK-NEXT:    [[NEG_RES:%.*]] = fneg double [[RES]]
43; CHECK-NEXT:    ret double [[NEG_RES]]
44;
45  %neg_x = fneg double %x
46  call void @use(double %neg_x)
47  %res = call double @erf(double %neg_x)
48  %neg_res = fneg double %res
49  ret double %neg_res
50}
51