Skip to content

Commit 0fb8304

Browse files
badumbatishgithub-actions[bot]
authored andcommitted
Automerge: [InstSimplify] Fold trig functions call of poison to poison (#148969)
Fold trig functions call of poison to poison. This includes sin, cos, asin, acos, atan, atan2, sinh, cosh, sincos, sincospi. Test cases are fixed and also added to llvm/test/Transforms/InstSimplify/fold-intrinsics.ll just like in llvm/llvm-project#146750
2 parents 4268ecd + 2206c7d commit 0fb8304

File tree

6 files changed

+219
-14
lines changed

6 files changed

+219
-14
lines changed

llvm/lib/Analysis/ValueTracking.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7914,6 +7914,18 @@ bool llvm::intrinsicPropagatesPoison(Intrinsic::ID IID) {
79147914
case Intrinsic::smul_fix_sat:
79157915
case Intrinsic::pow:
79167916
case Intrinsic::powi:
7917+
case Intrinsic::sin:
7918+
case Intrinsic::sinh:
7919+
case Intrinsic::cos:
7920+
case Intrinsic::cosh:
7921+
case Intrinsic::sincos:
7922+
case Intrinsic::sincospi:
7923+
case Intrinsic::tan:
7924+
case Intrinsic::tanh:
7925+
case Intrinsic::asin:
7926+
case Intrinsic::acos:
7927+
case Intrinsic::atan:
7928+
case Intrinsic::atan2:
79177929
case Intrinsic::canonicalize:
79187930
case Intrinsic::sqrt:
79197931
return true;

llvm/test/Transforms/InstSimplify/ConstProp/atan-intrinsic.ll

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@ define double @test_atan_neg0() {
3535

3636
define double @test_atan_poison() {
3737
; CHECK-LABEL: define double @test_atan_poison() {
38-
; CHECK-NEXT: [[RES:%.*]] = call double @llvm.atan.f64(double poison)
39-
; CHECK-NEXT: ret double [[RES]]
38+
; CHECK-NEXT: ret double poison
4039
;
4140
%res = call double @llvm.atan.f64(double poison)
4241
ret double %res

llvm/test/Transforms/InstSimplify/ConstProp/sinh-cosh-intrinsics.ll

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@ define double @test_sinh_neg0() {
3535

3636
define double @test_sinh_poison() {
3737
; CHECK-LABEL: define double @test_sinh_poison() {
38-
; CHECK-NEXT: [[RES:%.*]] = call double @llvm.sinh.f64(double poison)
39-
; CHECK-NEXT: ret double [[RES]]
38+
; CHECK-NEXT: ret double poison
4039
;
4140
%res = call double @llvm.sinh.f64(double poison)
4241
ret double %res
@@ -121,8 +120,7 @@ define double @test_cosh_neg0() {
121120

122121
define double @test_cosh_poison() {
123122
; CHECK-LABEL: define double @test_cosh_poison() {
124-
; CHECK-NEXT: [[RES:%.*]] = call double @llvm.cosh.f64(double poison)
125-
; CHECK-NEXT: ret double [[RES]]
123+
; CHECK-NEXT: ret double poison
126124
;
127125
%res = call double @llvm.cosh.f64(double poison)
128126
ret double %res

llvm/test/Transforms/InstSimplify/fold-intrinsics.ll

Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,202 @@ define void @pow_poison(i16 %arg_int,float %arg_flt, ptr %P) {
8787

8888
ret void
8989
}
90+
91+
define void @sin_poison(ptr %P) {
92+
; CHECK-LABEL: @sin_poison(
93+
; CHECK-NEXT: store volatile float poison, ptr [[P:%.*]], align 4
94+
; CHECK-NEXT: store volatile <2 x float> poison, ptr [[P]], align 8
95+
; CHECK-NEXT: store volatile <4 x double> poison, ptr [[P]], align 32
96+
; CHECK-NEXT: store volatile float poison, ptr [[P]], align 4
97+
; CHECK-NEXT: store volatile <2 x float> poison, ptr [[P]], align 8
98+
; CHECK-NEXT: store volatile <4 x double> poison, ptr [[P]], align 32
99+
; CHECK-NEXT: ret void
100+
;
101+
%sin_f32 = call float @llvm.sin(float poison)
102+
store volatile float %sin_f32, ptr %P
103+
104+
%sin_2xf32 = call <2 x float> @llvm.sin(<2 x float> poison)
105+
store volatile <2 x float> %sin_2xf32, ptr %P
106+
107+
%sin_4xf64 = call <4 x double> @llvm.sin(<4 x double> poison)
108+
store volatile <4 x double> %sin_4xf64, ptr %P
109+
110+
%asin_f32 = call float @llvm.asin(float poison)
111+
store volatile float %asin_f32, ptr %P
112+
113+
%asin_2xf32 = call <2 x float> @llvm.asin(<2 x float> poison)
114+
store volatile <2 x float> %asin_2xf32, ptr %P
115+
116+
%asin_4xf64 = call <4 x double> @llvm.asin(<4 x double> poison)
117+
store volatile <4 x double> %asin_4xf64, ptr %P
118+
119+
ret void
120+
}
121+
122+
123+
define void @cos_poison(ptr %P) {
124+
; CHECK-LABEL: @cos_poison(
125+
; CHECK-NEXT: store volatile float poison, ptr [[P:%.*]], align 4
126+
; CHECK-NEXT: store volatile <2 x float> poison, ptr [[P]], align 8
127+
; CHECK-NEXT: store volatile <4 x double> poison, ptr [[P]], align 32
128+
; CHECK-NEXT: store volatile float poison, ptr [[P]], align 4
129+
; CHECK-NEXT: store volatile <2 x float> poison, ptr [[P]], align 8
130+
; CHECK-NEXT: store volatile <4 x double> poison, ptr [[P]], align 32
131+
; CHECK-NEXT: ret void
132+
;
133+
%cos_f32 = call float @llvm.cos(float poison)
134+
store volatile float %cos_f32, ptr %P
135+
136+
%cos_2xf32 = call <2 x float> @llvm.cos(<2 x float> poison)
137+
store volatile <2 x float> %cos_2xf32, ptr %P
138+
139+
%cos_4xf64 = call <4 x double> @llvm.cos(<4 x double> poison)
140+
store volatile <4 x double> %cos_4xf64, ptr %P
141+
142+
%acos_f32 = call float @llvm.acos(float poison)
143+
store volatile float %acos_f32, ptr %P
144+
145+
%acos_2xf32 = call <2 x float> @llvm.acos(<2 x float> poison)
146+
store volatile <2 x float> %acos_2xf32, ptr %P
147+
148+
%acos_4xf64 = call <4 x double> @llvm.acos(<4 x double> poison)
149+
store volatile <4 x double> %acos_4xf64, ptr %P
150+
151+
ret void
152+
}
153+
154+
155+
define void @tan_poison(ptr %P) {
156+
; CHECK-LABEL: @tan_poison(
157+
; CHECK-NEXT: store volatile float poison, ptr [[P:%.*]], align 4
158+
; CHECK-NEXT: store volatile <2 x float> poison, ptr [[P]], align 8
159+
; CHECK-NEXT: store volatile <4 x double> poison, ptr [[P]], align 32
160+
; CHECK-NEXT: store volatile float poison, ptr [[P]], align 4
161+
; CHECK-NEXT: store volatile <2 x float> poison, ptr [[P]], align 8
162+
; CHECK-NEXT: store volatile <4 x double> poison, ptr [[P]], align 32
163+
; CHECK-NEXT: store volatile float poison, ptr [[P]], align 4
164+
; CHECK-NEXT: store volatile <2 x float> poison, ptr [[P]], align 8
165+
; CHECK-NEXT: store volatile <4 x double> poison, ptr [[P]], align 32
166+
; CHECK-NEXT: ret void
167+
;
168+
%tan_f32 = call float @llvm.tan(float poison)
169+
store volatile float %tan_f32, ptr %P
170+
171+
%tan_2xf32 = call <2 x float> @llvm.tan(<2 x float> poison)
172+
store volatile <2 x float> %tan_2xf32, ptr %P
173+
174+
%tan_4xf64 = call <4 x double> @llvm.tan(<4 x double> poison)
175+
store volatile <4 x double> %tan_4xf64, ptr %P
176+
177+
%atan_f32 = call float @llvm.atan(float poison)
178+
store volatile float %atan_f32, ptr %P
179+
180+
%atan_2xf32 = call <2 x float> @llvm.atan(<2 x float> poison)
181+
store volatile <2 x float> %atan_2xf32, ptr %P
182+
183+
%atan_4xf64 = call <4 x double> @llvm.atan(<4 x double> poison)
184+
store volatile <4 x double> %atan_4xf64, ptr %P
185+
186+
%atan2_f32 = call float @llvm.atan2(float poison, float poison)
187+
store volatile float %atan2_f32, ptr %P
188+
189+
%atan2_2xf32 = call <2 x float> @llvm.atan2(<2 x float> poison, <2 x float> poison)
190+
store volatile <2 x float> %atan2_2xf32, ptr %P
191+
192+
%atan2_4xf64 = call <4 x double> @llvm.atan2(<4 x double> poison, <4 x double> poison)
193+
store volatile <4 x double> %atan2_4xf64, ptr %P
194+
195+
ret void
196+
}
197+
198+
199+
define void @sincos_poison(ptr %P) {
200+
; CHECK-LABEL: @sincos_poison(
201+
; CHECK-NEXT: store volatile { float, float } poison, ptr [[P:%.*]], align 4
202+
; CHECK-NEXT: store volatile { <2 x float>, <2 x float> } poison, ptr [[P]], align 8
203+
; CHECK-NEXT: store volatile { <4 x double>, <4 x double> } poison, ptr [[P]], align 32
204+
; CHECK-NEXT: store volatile { float, float } poison, ptr [[P]], align 4
205+
; CHECK-NEXT: store volatile { <2 x float>, <2 x float> } poison, ptr [[P]], align 8
206+
; CHECK-NEXT: store volatile { <4 x double>, <4 x double> } poison, ptr [[P]], align 32
207+
; CHECK-NEXT: ret void
208+
;
209+
%sincos_f32 = call { float, float } @llvm.sincos(float poison)
210+
store volatile { float, float } %sincos_f32, ptr %P
211+
212+
%sincos_2xf32 = call { <2 x float>, <2 x float> } @llvm.sincos(<2 x float> poison)
213+
store volatile { <2 x float>, <2 x float> } %sincos_2xf32, ptr %P
214+
215+
%sincos_4xf64 = call { <4 x double>, <4 x double> } @llvm.sincos(<4 x double> poison)
216+
store volatile { <4 x double>, <4 x double> } %sincos_4xf64, ptr %P
217+
218+
%sincospi_f32 = call { float, float } @llvm.sincospi(float poison)
219+
store volatile { float, float } %sincospi_f32, ptr %P
220+
221+
%sincospi_2xf32 = call { <2 x float>, <2 x float> } @llvm.sincospi(<2 x float> poison)
222+
store volatile { <2 x float>, <2 x float> } %sincospi_2xf32, ptr %P
223+
224+
%sincospi_4xf64 = call { <4 x double>, <4 x double> } @llvm.sincospi(<4 x double> poison)
225+
store volatile { <4 x double>, <4 x double> } %sincospi_4xf64, ptr %P
226+
227+
ret void
228+
}
229+
230+
231+
define void @sinh_poison(ptr %P) {
232+
; CHECK-LABEL: @sinh_poison(
233+
; CHECK-NEXT: store volatile float poison, ptr [[P:%.*]], align 4
234+
; CHECK-NEXT: store volatile <2 x float> poison, ptr [[P]], align 8
235+
; CHECK-NEXT: store volatile <4 x double> poison, ptr [[P]], align 32
236+
; CHECK-NEXT: ret void
237+
;
238+
%sinh_f32 = call float @llvm.sinh(float poison)
239+
store volatile float %sinh_f32, ptr %P
240+
241+
%sinh_2xf32 = call <2 x float> @llvm.sinh(<2 x float> poison)
242+
store volatile <2 x float> %sinh_2xf32, ptr %P
243+
244+
%sinh_4xf64 = call <4 x double> @llvm.sinh(<4 x double> poison)
245+
store volatile <4 x double> %sinh_4xf64, ptr %P
246+
247+
ret void
248+
}
249+
250+
251+
define void @cosh_poison(ptr %P) {
252+
; CHECK-LABEL: @cosh_poison(
253+
; CHECK-NEXT: store volatile float poison, ptr [[P:%.*]], align 4
254+
; CHECK-NEXT: store volatile <2 x float> poison, ptr [[P]], align 8
255+
; CHECK-NEXT: store volatile <4 x double> poison, ptr [[P]], align 32
256+
; CHECK-NEXT: ret void
257+
;
258+
%cosh_f32 = call float @llvm.cosh(float poison)
259+
store volatile float %cosh_f32, ptr %P
260+
261+
%cosh_2xf32 = call <2 x float> @llvm.cosh(<2 x float> poison)
262+
store volatile <2 x float> %cosh_2xf32, ptr %P
263+
264+
%cosh_4xf64 = call <4 x double> @llvm.cosh(<4 x double> poison)
265+
store volatile <4 x double> %cosh_4xf64, ptr %P
266+
267+
ret void
268+
}
269+
270+
271+
define void @tanh_poison(ptr %P) {
272+
; CHECK-LABEL: @tanh_poison(
273+
; CHECK-NEXT: store volatile float poison, ptr [[P:%.*]], align 4
274+
; CHECK-NEXT: store volatile <2 x float> poison, ptr [[P]], align 8
275+
; CHECK-NEXT: store volatile <4 x double> poison, ptr [[P]], align 32
276+
; CHECK-NEXT: ret void
277+
;
278+
%tanh_f32 = call float @llvm.tanh(float poison)
279+
store volatile float %tanh_f32, ptr %P
280+
281+
%tanh_2xf32 = call <2 x float> @llvm.tanh(<2 x float> poison)
282+
store volatile <2 x float> %tanh_2xf32, ptr %P
283+
284+
%tanh_4xf64 = call <4 x double> @llvm.tanh(<4 x double> poison)
285+
store volatile <4 x double> %tanh_4xf64, ptr %P
286+
287+
ret void
288+
}

llvm/test/Transforms/InstSimplify/sincos.ll

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,26 +50,23 @@ define { <2 x float>, <2 x float> } @sincos_zero_vector() {
5050

5151
define { float, float } @sincos_poison() {
5252
; CHECK-LABEL: define { float, float } @sincos_poison() {
53-
; CHECK-NEXT: [[RET:%.*]] = call { float, float } @llvm.sincos.f32(float poison)
54-
; CHECK-NEXT: ret { float, float } [[RET]]
53+
; CHECK-NEXT: ret { float, float } poison
5554
;
5655
%ret = call { float, float } @llvm.sincos.f32(float poison)
5756
ret { float, float } %ret
5857
}
5958

6059
define { <2 x float>, <2 x float> } @sincos_poison_vector() {
6160
; CHECK-LABEL: define { <2 x float>, <2 x float> } @sincos_poison_vector() {
62-
; CHECK-NEXT: [[RET:%.*]] = call { <2 x float>, <2 x float> } @llvm.sincos.v2f32(<2 x float> poison)
63-
; CHECK-NEXT: ret { <2 x float>, <2 x float> } [[RET]]
61+
; CHECK-NEXT: ret { <2 x float>, <2 x float> } poison
6462
;
6563
%ret = call { <2 x float>, <2 x float> } @llvm.sincos.v2f32(<2 x float> poison)
6664
ret { <2 x float>, <2 x float> } %ret
6765
}
6866

6967
define { <vscale x 2 x float>, <vscale x 2 x float> } @sincos_poison_scalable_vector() {
7068
; CHECK-LABEL: define { <vscale x 2 x float>, <vscale x 2 x float> } @sincos_poison_scalable_vector() {
71-
; CHECK-NEXT: [[RET:%.*]] = call { <vscale x 2 x float>, <vscale x 2 x float> } @llvm.sincos.nxv2f32(<vscale x 2 x float> poison)
72-
; CHECK-NEXT: ret { <vscale x 2 x float>, <vscale x 2 x float> } [[RET]]
69+
; CHECK-NEXT: ret { <vscale x 2 x float>, <vscale x 2 x float> } poison
7370
;
7471
%ret = call { <vscale x 2 x float>, <vscale x 2 x float> } @llvm.sincos.nxv2f32(<vscale x 2 x float> poison)
7572
ret { <vscale x 2 x float>, <vscale x 2 x float> } %ret

llvm/unittests/Analysis/ValueTrackingTest.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -912,8 +912,8 @@ TEST(ValueTracking, propagatesPoison) {
912912
{false, "call i32 @llvm.fshr.i32(i32 %x, i32 %y, i32 %shamt)", 2},
913913
{true, "call float @llvm.sqrt.f32(float %fx)", 0},
914914
{true, "call float @llvm.powi.f32.i32(float %fx, i32 %x)", 0},
915-
{false, "call float @llvm.sin.f32(float %fx)", 0},
916-
{false, "call float @llvm.cos.f32(float %fx)", 0},
915+
{true, "call float @llvm.sin.f32(float %fx)", 0},
916+
{true, "call float @llvm.cos.f32(float %fx)", 0},
917917
{true, "call float @llvm.pow.f32(float %fx, float %fy)", 0},
918918
{false, "call float @llvm.exp.f32(float %fx)", 0},
919919
{false, "call float @llvm.exp2.f32(float %fx)", 0},

0 commit comments

Comments
 (0)