Skip to content

Commit 1b4e6be

Browse files
committed
Emit the intrinsic only if the original was an intrinsic
1 parent b1c336b commit 1b4e6be

File tree

2 files changed

+55
-121
lines changed

2 files changed

+55
-121
lines changed

llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2376,7 +2376,13 @@ Value *LibCallSimplifier::optimizeExp2(CallInst *CI, IRBuilderBase &B) {
23762376
hasFloatVersion(M, Name))
23772377
Ret = optimizeUnaryDoubleFP(CI, B, TLI, true);
23782378

2379-
const bool UseIntrinsic = CI->doesNotAccessMemory();
2379+
// If we have an llvm.exp2 intrinsic, emit the llvm.ldexp intrinsic. If we
2380+
// have the libcall, emit the libcall.
2381+
//
2382+
// TODO: In principle we should be able to just always use the intrinsic for
2383+
// any doesNotAccessMemory callsite.
2384+
2385+
const bool UseIntrinsic = Callee->isIntrinsic();
23802386
// Bail out for vectors because the code below only expects scalars.
23812387
Type *Ty = CI->getType();
23822388
if (!UseIntrinsic && Ty->isVectorTy())

llvm/test/Transforms/InstCombine/pow-to-ldexp.ll

Lines changed: 48 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,21 @@
55

66

77
define float @pow_sitofp_f32_const_base_2(i32 %x) {
8-
; LDEXP-LABEL: define float @pow_sitofp_f32_const_base_2(
9-
; LDEXP-SAME: i32 [[X:%.*]]) {
10-
; LDEXP-NEXT: [[LDEXPF:%.*]] = tail call float @llvm.ldexp.f32.i32(float 1.000000e+00, i32 [[X]])
11-
; LDEXP-NEXT: ret float [[LDEXPF]]
12-
;
13-
; NOLDEXP-LABEL: define float @pow_sitofp_f32_const_base_2(
14-
; NOLDEXP-SAME: i32 [[X:%.*]]) {
15-
; NOLDEXP-NEXT: [[ITOFP:%.*]] = sitofp i32 [[X]] to float
16-
; NOLDEXP-NEXT: [[POW:%.*]] = tail call float @llvm.exp2.f32(float [[ITOFP]])
17-
; NOLDEXP-NEXT: ret float [[POW]]
8+
; CHECK-LABEL: define float @pow_sitofp_f32_const_base_2(
9+
; CHECK-SAME: i32 [[X:%.*]]) {
10+
; CHECK-NEXT: [[EXP2:%.*]] = tail call float @llvm.ldexp.f32.i32(float 1.000000e+00, i32 [[X]])
11+
; CHECK-NEXT: ret float [[EXP2]]
1812
;
1913
%itofp = sitofp i32 %x to float
2014
%pow = tail call float @llvm.pow.f32(float 2.000000e+00, float %itofp)
2115
ret float %pow
2216
}
2317

2418
define float @pow_sitofp_f32_const_base_2__flags(i32 %x) {
25-
; LDEXP-LABEL: define float @pow_sitofp_f32_const_base_2__flags(
26-
; LDEXP-SAME: i32 [[X:%.*]]) {
27-
; LDEXP-NEXT: [[LDEXPF:%.*]] = tail call nnan nsz float @llvm.ldexp.f32.i32(float 1.000000e+00, i32 [[X]])
28-
; LDEXP-NEXT: ret float [[LDEXPF]]
29-
;
30-
; NOLDEXP-LABEL: define float @pow_sitofp_f32_const_base_2__flags(
31-
; NOLDEXP-SAME: i32 [[X:%.*]]) {
32-
; NOLDEXP-NEXT: [[ITOFP:%.*]] = sitofp i32 [[X]] to float
33-
; NOLDEXP-NEXT: [[EXP2:%.*]] = tail call nnan nsz float @llvm.exp2.f32(float [[ITOFP]])
34-
; NOLDEXP-NEXT: ret float [[EXP2]]
19+
; CHECK-LABEL: define float @pow_sitofp_f32_const_base_2__flags(
20+
; CHECK-SAME: i32 [[X:%.*]]) {
21+
; CHECK-NEXT: [[EXP2:%.*]] = tail call nnan nsz float @llvm.ldexp.f32.i32(float 1.000000e+00, i32 [[X]])
22+
; CHECK-NEXT: ret float [[EXP2]]
3523
;
3624
%itofp = sitofp i32 %x to float
3725
%pow = tail call nsz nnan float @llvm.pow.f32(float 2.000000e+00, float %itofp)
@@ -115,16 +103,10 @@ define float @pow_sitofp_f32_const_base_16(i32 %x) {
115103
}
116104

117105
define double @pow_sitofp_f64_const_base_2(i32 %x) {
118-
; LDEXP-LABEL: define double @pow_sitofp_f64_const_base_2(
119-
; LDEXP-SAME: i32 [[X:%.*]]) {
120-
; LDEXP-NEXT: [[LDEXP:%.*]] = tail call double @llvm.ldexp.f64.i32(double 1.000000e+00, i32 [[X]])
121-
; LDEXP-NEXT: ret double [[LDEXP]]
122-
;
123-
; NOLDEXP-LABEL: define double @pow_sitofp_f64_const_base_2(
124-
; NOLDEXP-SAME: i32 [[X:%.*]]) {
125-
; NOLDEXP-NEXT: [[ITOFP:%.*]] = sitofp i32 [[X]] to double
126-
; NOLDEXP-NEXT: [[POW:%.*]] = tail call double @llvm.exp2.f64(double [[ITOFP]])
127-
; NOLDEXP-NEXT: ret double [[POW]]
106+
; CHECK-LABEL: define double @pow_sitofp_f64_const_base_2(
107+
; CHECK-SAME: i32 [[X:%.*]]) {
108+
; CHECK-NEXT: [[EXP2:%.*]] = tail call double @llvm.ldexp.f64.i32(double 1.000000e+00, i32 [[X]])
109+
; CHECK-NEXT: ret double [[EXP2]]
128110
;
129111
%itofp = sitofp i32 %x to double
130112
%pow = tail call double @llvm.pow.f64(double 2.000000e+00, double %itofp)
@@ -144,16 +126,10 @@ define half @pow_sitofp_f16_const_base_2(i32 %x) {
144126
}
145127

146128
define <2 x float> @pow_sitofp_v2f32_const_base_2(<2 x i32> %x) {
147-
; LDEXP-LABEL: define <2 x float> @pow_sitofp_v2f32_const_base_2(
148-
; LDEXP-SAME: <2 x i32> [[X:%.*]]) {
149-
; LDEXP-NEXT: [[EXP2:%.*]] = tail call <2 x float> @llvm.ldexp.v2f32.v2i32(<2 x float> <float 1.000000e+00, float 1.000000e+00>, <2 x i32> [[X]])
150-
; LDEXP-NEXT: ret <2 x float> [[EXP2]]
151-
;
152-
; NOLDEXP-LABEL: define <2 x float> @pow_sitofp_v2f32_const_base_2(
153-
; NOLDEXP-SAME: <2 x i32> [[X:%.*]]) {
154-
; NOLDEXP-NEXT: [[ITOFP:%.*]] = sitofp <2 x i32> [[X]] to <2 x float>
155-
; NOLDEXP-NEXT: [[POW:%.*]] = tail call <2 x float> @llvm.exp2.v2f32(<2 x float> [[ITOFP]])
156-
; NOLDEXP-NEXT: ret <2 x float> [[POW]]
129+
; CHECK-LABEL: define <2 x float> @pow_sitofp_v2f32_const_base_2(
130+
; CHECK-SAME: <2 x i32> [[X:%.*]]) {
131+
; CHECK-NEXT: [[EXP2:%.*]] = tail call <2 x float> @llvm.ldexp.v2f32.v2i32(<2 x float> <float 1.000000e+00, float 1.000000e+00>, <2 x i32> [[X]])
132+
; CHECK-NEXT: ret <2 x float> [[EXP2]]
157133
;
158134
%itofp = sitofp <2 x i32> %x to <2 x float>
159135
%pow = tail call <2 x float> @llvm.pow.v2f32(<2 x float> <float 2.000000e+00, float 2.000000e+00>, <2 x float> %itofp)
@@ -199,67 +175,43 @@ define <2 x float> @pow_sitofp_v2f32_const_base_mixed_2(<2 x i32> %x) {
199175
}
200176

201177
define <2 x float> @pow_sitofp_v2f32_const_base_2__flags(<2 x i32> %x) {
202-
; LDEXP-LABEL: define <2 x float> @pow_sitofp_v2f32_const_base_2__flags(
203-
; LDEXP-SAME: <2 x i32> [[X:%.*]]) {
204-
; LDEXP-NEXT: [[EXP2:%.*]] = tail call nsz afn <2 x float> @llvm.ldexp.v2f32.v2i32(<2 x float> <float 1.000000e+00, float 1.000000e+00>, <2 x i32> [[X]])
205-
; LDEXP-NEXT: ret <2 x float> [[EXP2]]
206-
;
207-
; NOLDEXP-LABEL: define <2 x float> @pow_sitofp_v2f32_const_base_2__flags(
208-
; NOLDEXP-SAME: <2 x i32> [[X:%.*]]) {
209-
; NOLDEXP-NEXT: [[ITOFP:%.*]] = sitofp <2 x i32> [[X]] to <2 x float>
210-
; NOLDEXP-NEXT: [[POW:%.*]] = tail call nsz afn <2 x float> @llvm.exp2.v2f32(<2 x float> [[ITOFP]])
211-
; NOLDEXP-NEXT: ret <2 x float> [[POW]]
178+
; CHECK-LABEL: define <2 x float> @pow_sitofp_v2f32_const_base_2__flags(
179+
; CHECK-SAME: <2 x i32> [[X:%.*]]) {
180+
; CHECK-NEXT: [[EXP2:%.*]] = tail call nsz afn <2 x float> @llvm.ldexp.v2f32.v2i32(<2 x float> <float 1.000000e+00, float 1.000000e+00>, <2 x i32> [[X]])
181+
; CHECK-NEXT: ret <2 x float> [[EXP2]]
212182
;
213183
%itofp = sitofp <2 x i32> %x to <2 x float>
214184
%pow = tail call nsz afn <2 x float> @llvm.pow.v2f32(<2 x float> <float 2.000000e+00, float 2.000000e+00>, <2 x float> %itofp)
215185
ret <2 x float> %pow
216186
}
217187

218188
define <vscale x 4 x float> @pow_sitofp_nxv4f32_const_base_2(<vscale x 4 x i32> %x) {
219-
; LDEXP-LABEL: define <vscale x 4 x float> @pow_sitofp_nxv4f32_const_base_2(
220-
; LDEXP-SAME: <vscale x 4 x i32> [[X:%.*]]) {
221-
; LDEXP-NEXT: [[EXP2:%.*]] = tail call <vscale x 4 x float> @llvm.ldexp.nxv4f32.nxv4i32(<vscale x 4 x float> shufflevector (<vscale x 4 x float> insertelement (<vscale x 4 x float> poison, float 1.000000e+00, i64 0), <vscale x 4 x float> poison, <vscale x 4 x i32> zeroinitializer), <vscale x 4 x i32> [[X]])
222-
; LDEXP-NEXT: ret <vscale x 4 x float> [[EXP2]]
223-
;
224-
; NOLDEXP-LABEL: define <vscale x 4 x float> @pow_sitofp_nxv4f32_const_base_2(
225-
; NOLDEXP-SAME: <vscale x 4 x i32> [[X:%.*]]) {
226-
; NOLDEXP-NEXT: [[ITOFP:%.*]] = sitofp <vscale x 4 x i32> [[X]] to <vscale x 4 x float>
227-
; NOLDEXP-NEXT: [[POW:%.*]] = tail call <vscale x 4 x float> @llvm.exp2.nxv4f32(<vscale x 4 x float> [[ITOFP]])
228-
; NOLDEXP-NEXT: ret <vscale x 4 x float> [[POW]]
189+
; CHECK-LABEL: define <vscale x 4 x float> @pow_sitofp_nxv4f32_const_base_2(
190+
; CHECK-SAME: <vscale x 4 x i32> [[X:%.*]]) {
191+
; CHECK-NEXT: [[EXP2:%.*]] = tail call <vscale x 4 x float> @llvm.ldexp.nxv4f32.nxv4i32(<vscale x 4 x float> shufflevector (<vscale x 4 x float> insertelement (<vscale x 4 x float> poison, float 1.000000e+00, i64 0), <vscale x 4 x float> poison, <vscale x 4 x i32> zeroinitializer), <vscale x 4 x i32> [[X]])
192+
; CHECK-NEXT: ret <vscale x 4 x float> [[EXP2]]
229193
;
230194
%itofp = sitofp <vscale x 4 x i32> %x to <vscale x 4 x float>
231195
%pow = tail call <vscale x 4 x float> @llvm.pow.nxv4f32(<vscale x 4 x float> splat (float 2.0), <vscale x 4 x float> %itofp)
232196
ret <vscale x 4 x float> %pow
233197
}
234198

235199
define <2 x half> @pow_sitofp_v2f16_const_base_2(<2 x i32> %x) {
236-
; LDEXP-LABEL: define <2 x half> @pow_sitofp_v2f16_const_base_2(
237-
; LDEXP-SAME: <2 x i32> [[X:%.*]]) {
238-
; LDEXP-NEXT: [[EXP2:%.*]] = tail call <2 x half> @llvm.ldexp.v2f16.v2i32(<2 x half> <half 0xH3C00, half 0xH3C00>, <2 x i32> [[X]])
239-
; LDEXP-NEXT: ret <2 x half> [[EXP2]]
240-
;
241-
; NOLDEXP-LABEL: define <2 x half> @pow_sitofp_v2f16_const_base_2(
242-
; NOLDEXP-SAME: <2 x i32> [[X:%.*]]) {
243-
; NOLDEXP-NEXT: [[ITOFP:%.*]] = sitofp <2 x i32> [[X]] to <2 x half>
244-
; NOLDEXP-NEXT: [[EXP2:%.*]] = tail call <2 x half> @llvm.exp2.v2f16(<2 x half> [[ITOFP]])
245-
; NOLDEXP-NEXT: ret <2 x half> [[EXP2]]
200+
; CHECK-LABEL: define <2 x half> @pow_sitofp_v2f16_const_base_2(
201+
; CHECK-SAME: <2 x i32> [[X:%.*]]) {
202+
; CHECK-NEXT: [[EXP2:%.*]] = tail call <2 x half> @llvm.ldexp.v2f16.v2i32(<2 x half> <half 0xH3C00, half 0xH3C00>, <2 x i32> [[X]])
203+
; CHECK-NEXT: ret <2 x half> [[EXP2]]
246204
;
247205
%itofp = sitofp <2 x i32> %x to <2 x half>
248206
%pow = tail call <2 x half> @llvm.pow.v2f16(<2 x half> <half 2.000000e+00, half 2.000000e+00>, <2 x half> %itofp)
249207
ret <2 x half> %pow
250208
}
251209

252210
define <2 x double> @pow_sitofp_v2f64_const_base_2(<2 x i32> %x) {
253-
; LDEXP-LABEL: define <2 x double> @pow_sitofp_v2f64_const_base_2(
254-
; LDEXP-SAME: <2 x i32> [[X:%.*]]) {
255-
; LDEXP-NEXT: [[EXP2:%.*]] = tail call <2 x double> @llvm.ldexp.v2f64.v2i32(<2 x double> <double 1.000000e+00, double 1.000000e+00>, <2 x i32> [[X]])
256-
; LDEXP-NEXT: ret <2 x double> [[EXP2]]
257-
;
258-
; NOLDEXP-LABEL: define <2 x double> @pow_sitofp_v2f64_const_base_2(
259-
; NOLDEXP-SAME: <2 x i32> [[X:%.*]]) {
260-
; NOLDEXP-NEXT: [[ITOFP:%.*]] = sitofp <2 x i32> [[X]] to <2 x double>
261-
; NOLDEXP-NEXT: [[EXP2:%.*]] = tail call <2 x double> @llvm.exp2.v2f64(<2 x double> [[ITOFP]])
262-
; NOLDEXP-NEXT: ret <2 x double> [[EXP2]]
211+
; CHECK-LABEL: define <2 x double> @pow_sitofp_v2f64_const_base_2(
212+
; CHECK-SAME: <2 x i32> [[X:%.*]]) {
213+
; CHECK-NEXT: [[EXP2:%.*]] = tail call <2 x double> @llvm.ldexp.v2f64.v2i32(<2 x double> <double 1.000000e+00, double 1.000000e+00>, <2 x i32> [[X]])
214+
; CHECK-NEXT: ret <2 x double> [[EXP2]]
263215
;
264216
%itofp = sitofp <2 x i32> %x to <2 x double>
265217
%pow = tail call <2 x double> @llvm.pow.v2f64(<2 x double> <double 2.000000e+00, double 2.000000e+00>, <2 x double> %itofp)
@@ -333,16 +285,10 @@ define <2 x double> @pow_sitofp_v2f64_const_base_8(<2 x i32> %x) {
333285
}
334286

335287
define fp128 @pow_sitofp_fp128_const_base_2(i32 %x) {
336-
; LDEXP-LABEL: define fp128 @pow_sitofp_fp128_const_base_2(
337-
; LDEXP-SAME: i32 [[X:%.*]]) {
338-
; LDEXP-NEXT: [[LDEXPL:%.*]] = tail call fp128 @llvm.ldexp.f128.i32(fp128 0xL00000000000000003FFF000000000000, i32 [[X]])
339-
; LDEXP-NEXT: ret fp128 [[LDEXPL]]
340-
;
341-
; NOLDEXP-LABEL: define fp128 @pow_sitofp_fp128_const_base_2(
342-
; NOLDEXP-SAME: i32 [[X:%.*]]) {
343-
; NOLDEXP-NEXT: [[ITOFP:%.*]] = sitofp i32 [[X]] to fp128
344-
; NOLDEXP-NEXT: [[POW:%.*]] = tail call fp128 @llvm.exp2.f128(fp128 [[ITOFP]])
345-
; NOLDEXP-NEXT: ret fp128 [[POW]]
288+
; CHECK-LABEL: define fp128 @pow_sitofp_fp128_const_base_2(
289+
; CHECK-SAME: i32 [[X:%.*]]) {
290+
; CHECK-NEXT: [[EXP2:%.*]] = tail call fp128 @llvm.ldexp.f128.i32(fp128 0xL00000000000000003FFF000000000000, i32 [[X]])
291+
; CHECK-NEXT: ret fp128 [[EXP2]]
346292
;
347293
%itofp = sitofp i32 %x to fp128
348294
%pow = tail call fp128 @llvm.pow.fp128(fp128 0xL00000000000000004000000000000000, fp128 %itofp)
@@ -412,50 +358,32 @@ define float @libcall_powf_sitofp_f32_const_base_2__flags(i32 %x) {
412358
}
413359

414360
define float @readnone_libcall_powf_sitofp_f32_const_base_2(i32 %x) {
415-
; LDEXP-LABEL: define float @readnone_libcall_powf_sitofp_f32_const_base_2(
416-
; LDEXP-SAME: i32 [[X:%.*]]) {
417-
; LDEXP-NEXT: [[LDEXPF:%.*]] = tail call float @llvm.ldexp.f32.i32(float 1.000000e+00, i32 [[X]])
418-
; LDEXP-NEXT: ret float [[LDEXPF]]
419-
;
420-
; NOLDEXP-LABEL: define float @readnone_libcall_powf_sitofp_f32_const_base_2(
421-
; NOLDEXP-SAME: i32 [[X:%.*]]) {
422-
; NOLDEXP-NEXT: [[ITOFP:%.*]] = sitofp i32 [[X]] to float
423-
; NOLDEXP-NEXT: [[POW:%.*]] = tail call float @llvm.exp2.f32(float [[ITOFP]])
424-
; NOLDEXP-NEXT: ret float [[POW]]
361+
; CHECK-LABEL: define float @readnone_libcall_powf_sitofp_f32_const_base_2(
362+
; CHECK-SAME: i32 [[X:%.*]]) {
363+
; CHECK-NEXT: [[EXP2:%.*]] = tail call float @llvm.ldexp.f32.i32(float 1.000000e+00, i32 [[X]])
364+
; CHECK-NEXT: ret float [[EXP2]]
425365
;
426366
%itofp = sitofp i32 %x to float
427367
%pow = tail call float @powf(float 2.000000e+00, float %itofp) memory(none)
428368
ret float %pow
429369
}
430370

431371
define double @readnone_libcall_pow_sitofp_f32_const_base_2(i32 %x) {
432-
; LDEXP-LABEL: define double @readnone_libcall_pow_sitofp_f32_const_base_2(
433-
; LDEXP-SAME: i32 [[X:%.*]]) {
434-
; LDEXP-NEXT: [[LDEXP:%.*]] = tail call double @llvm.ldexp.f64.i32(double 1.000000e+00, i32 [[X]])
435-
; LDEXP-NEXT: ret double [[LDEXP]]
436-
;
437-
; NOLDEXP-LABEL: define double @readnone_libcall_pow_sitofp_f32_const_base_2(
438-
; NOLDEXP-SAME: i32 [[X:%.*]]) {
439-
; NOLDEXP-NEXT: [[ITOFP:%.*]] = sitofp i32 [[X]] to double
440-
; NOLDEXP-NEXT: [[POW:%.*]] = tail call double @llvm.exp2.f64(double [[ITOFP]])
441-
; NOLDEXP-NEXT: ret double [[POW]]
372+
; CHECK-LABEL: define double @readnone_libcall_pow_sitofp_f32_const_base_2(
373+
; CHECK-SAME: i32 [[X:%.*]]) {
374+
; CHECK-NEXT: [[EXP2:%.*]] = tail call double @llvm.ldexp.f64.i32(double 1.000000e+00, i32 [[X]])
375+
; CHECK-NEXT: ret double [[EXP2]]
442376
;
443377
%itofp = sitofp i32 %x to double
444378
%pow = tail call double @pow(double 2.000000e+00, double %itofp) memory(none)
445379
ret double %pow
446380
}
447381

448382
define fp128 @readnone_libcall_powl_sitofp_fp128_const_base_2(i32 %x) {
449-
; LDEXP-LABEL: define fp128 @readnone_libcall_powl_sitofp_fp128_const_base_2(
450-
; LDEXP-SAME: i32 [[X:%.*]]) {
451-
; LDEXP-NEXT: [[LDEXPL:%.*]] = tail call fp128 @llvm.ldexp.f128.i32(fp128 0xL00000000000000003FFF000000000000, i32 [[X]])
452-
; LDEXP-NEXT: ret fp128 [[LDEXPL]]
453-
;
454-
; NOLDEXP-LABEL: define fp128 @readnone_libcall_powl_sitofp_fp128_const_base_2(
455-
; NOLDEXP-SAME: i32 [[X:%.*]]) {
456-
; NOLDEXP-NEXT: [[ITOFP:%.*]] = sitofp i32 [[X]] to fp128
457-
; NOLDEXP-NEXT: [[POW:%.*]] = tail call fp128 @llvm.exp2.f128(fp128 [[ITOFP]])
458-
; NOLDEXP-NEXT: ret fp128 [[POW]]
383+
; CHECK-LABEL: define fp128 @readnone_libcall_powl_sitofp_fp128_const_base_2(
384+
; CHECK-SAME: i32 [[X:%.*]]) {
385+
; CHECK-NEXT: [[EXP2:%.*]] = tail call fp128 @llvm.ldexp.f128.i32(fp128 0xL00000000000000003FFF000000000000, i32 [[X]])
386+
; CHECK-NEXT: ret fp128 [[EXP2]]
459387
;
460388
%itofp = sitofp i32 %x to fp128
461389
%pow = tail call fp128 @powl(fp128 0xL00000000000000004000000000000000, fp128 %itofp) memory(none)

0 commit comments

Comments
 (0)