aboutsummaryrefslogtreecommitdiff
path: root/test/Transforms/InstSimplify/fast-math.ll
blob: 90532fa5db84332df2e1eb9df5bd21f4098894dd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
; RUN: opt < %s -instsimplify -S | FileCheck %s

;; x * 0 ==> 0 when no-nans and no-signed-zero
; CHECK: mul_zero_1
define float @mul_zero_1(float %a) {
  %b = fmul nsz nnan float %a, 0.0
; CHECK: ret float 0.0
  ret float %b
}
; CHECK: mul_zero_2
define float @mul_zero_2(float %a) {
  %b = fmul fast float 0.0, %a
; CHECK: ret float 0.0
  ret float %b
}

;; x * 0 =/=> 0 when there could be nans or -0
; CHECK: no_mul_zero_1
define float @no_mul_zero_1(float %a) {
  %b = fmul nsz float %a, 0.0
; CHECK: ret float %b
  ret float %b
}
; CHECK: no_mul_zero_2
define float @no_mul_zero_2(float %a) {
  %b = fmul nnan float %a, 0.0
; CHECK: ret float %b
  ret float %b
}
; CHECK: no_mul_zero_3
define float @no_mul_zero_3(float %a) {
  %b = fmul float %a, 0.0
; CHECK: ret float %b
  ret float %b
}

; fadd [nnan ninf] X, (fsub [nnan ninf] 0, X) ==> 0
;   where nnan and ninf have to occur at least once somewhere in this
;   expression
; CHECK: fadd_fsub_0
define float @fadd_fsub_0(float %a) {
; X + -X ==> 0
  %t1 = fsub nnan ninf float 0.0, %a
  %zero1 = fadd nnan ninf float %t1, %a

  %t2 = fsub nnan float 0.0, %a
  %zero2 = fadd ninf float %t2, %a

  %t3 = fsub nnan ninf float 0.0, %a
  %zero3 = fadd float %t3, %a

  %t4 = fsub float 0.0, %a
  %zero4 = fadd nnan ninf float %t4, %a

; Dont fold this
; CHECK: %nofold = fsub float 0.0
  %nofold = fsub float 0.0, %a
; CHECK: %no_zero = fadd nnan float %nofold, %a
  %no_zero = fadd nnan float %nofold, %a

; Coalesce the folded zeros
  %zero5 = fadd float %zero1, %zero2
  %zero6 = fadd float %zero3, %zero4
  %zero7 = fadd float %zero5, %zero6

; Should get folded
  %ret = fadd nsz float %no_zero, %zero7

; CHECK: ret float %no_zero
  ret float %ret
}

; fsub nnan x, x ==> 0.0
; CHECK-LABEL: @fsub_x_x(
define float @fsub_x_x(float %a) {
; X - X ==> 0
  %zero1 = fsub nnan float %a, %a

; Dont fold
; CHECK: %no_zero1 = fsub
  %no_zero1 = fsub ninf float %a, %a
; CHECK: %no_zero2 = fsub
  %no_zero2 = fsub float %a, %a
; CHECK: %no_zero = fadd
  %no_zero = fadd float %no_zero1, %no_zero2

; Should get folded
  %ret = fadd nsz float %no_zero, %zero1

; CHECK: ret float %no_zero
  ret float %ret
}

; fadd nsz X, 0 ==> X
; CHECK-LABEL: @nofold_fadd_x_0(
define float @nofold_fadd_x_0(float %a) {
; Dont fold
; CHECK: %no_zero1 = fadd
  %no_zero1 = fadd ninf float %a, 0.0
; CHECK: %no_zero2 = fadd
  %no_zero2 = fadd nnan float %a, 0.0
; CHECK: %no_zero = fadd
  %no_zero = fadd float %no_zero1, %no_zero2

; CHECK: ret float %no_zero
  ret float %no_zero
}

; fdiv nsz nnan 0, X ==> 0
define double @fdiv_zero_by_x(double %X) {
; CHECK-LABEL: @fdiv_zero_by_x(
; 0 / X -> 0
  %r = fdiv nnan nsz double 0.0, %X
  ret double %r
; CHECK: ret double 0
}

define float @fdiv_self(float %f) {
  %div = fdiv nnan float %f, %f
  ret float %div
; CHECK-LABEL: fdiv_self
; CHECK: ret float 1.000000e+00
}

define float @fdiv_self_invalid(float %f) {
  %div = fdiv float %f, %f
  ret float %div
; CHECK-LABEL: fdiv_self_invalid
; CHECK: %div = fdiv float %f, %f
; CHECK-NEXT: ret float %div
}

define float @fdiv_neg1(float %f) {
  %neg = fsub fast float -0.000000e+00, %f
  %div = fdiv nnan float %neg, %f
  ret float %div
; CHECK-LABEL: fdiv_neg1
; CHECK: ret float -1.000000e+00
}

define float @fdiv_neg2(float %f) {
  %neg = fsub fast float 0.000000e+00, %f
  %div = fdiv nnan float %neg, %f
  ret float %div
; CHECK-LABEL: fdiv_neg2
; CHECK: ret float -1.000000e+00
}

define float @fdiv_neg_invalid(float %f) {
  %neg = fsub fast float -0.000000e+00, %f
  %div = fdiv float %neg, %f
  ret float %div
; CHECK-LABEL: fdiv_neg_invalid
; CHECK: %neg = fsub fast float -0.000000e+00, %f
; CHECK-NEXT: %div = fdiv float %neg, %f
; CHECK-NEXT: ret float %div
}

define float @fdiv_neg_swapped1(float %f) {
  %neg = fsub float -0.000000e+00, %f
  %div = fdiv nnan float %f, %neg
  ret float %div
; CHECK-LABEL: fdiv_neg_swapped1
; CHECK: ret float -1.000000e+00
}

define float @fdiv_neg_swapped2(float %f) {
  %neg = fsub float 0.000000e+00, %f
  %div = fdiv nnan float %f, %neg
  ret float %div
; CHECK-LABEL: fdiv_neg_swapped2
; CHECK: ret float -1.000000e+00
}