Skip to content

Commit 0ea178b

Browse files
arsenmnikic
andauthored
SimplifyLibCalls: Emit vector ldexp intrinsics in exp2->ldexp combine (#92219)
Co-authored-by: Nikita Popov <[email protected]>
1 parent cd5ee27 commit 0ea178b

File tree

4 files changed

+37
-29
lines changed

4 files changed

+37
-29
lines changed

llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1992,11 +1992,12 @@ static Value *getIntToFPVal(Value *I2F, IRBuilderBase &B, unsigned DstWidth) {
19921992
Value *Op = cast<Instruction>(I2F)->getOperand(0);
19931993
// Make sure that the exponent fits inside an "int" of size DstWidth,
19941994
// thus avoiding any range issues that FP has not.
1995-
unsigned BitWidth = Op->getType()->getPrimitiveSizeInBits();
1996-
if (BitWidth < DstWidth ||
1997-
(BitWidth == DstWidth && isa<SIToFPInst>(I2F)))
1998-
return isa<SIToFPInst>(I2F) ? B.CreateSExt(Op, B.getIntNTy(DstWidth))
1999-
: B.CreateZExt(Op, B.getIntNTy(DstWidth));
1995+
unsigned BitWidth = Op->getType()->getScalarSizeInBits();
1996+
if (BitWidth < DstWidth || (BitWidth == DstWidth && isa<SIToFPInst>(I2F))) {
1997+
Type *IntTy = Op->getType()->getWithNewBitWidth(DstWidth);
1998+
return isa<SIToFPInst>(I2F) ? B.CreateSExt(Op, IntTy)
1999+
: B.CreateZExt(Op, IntTy);
2000+
}
20002001
}
20012002

20022003
return nullptr;
@@ -2366,10 +2367,10 @@ Value *LibCallSimplifier::optimizeExp2(CallInst *CI, IRBuilderBase &B) {
23662367
hasFloatVersion(M, Name))
23672368
Ret = optimizeUnaryDoubleFP(CI, B, TLI, true);
23682369

2370+
const bool UseIntrinsic = CI->doesNotAccessMemory();
23692371
// Bail out for vectors because the code below only expects scalars.
2370-
// TODO: This could be allowed if we had a ldexp intrinsic (D14327).
23712372
Type *Ty = CI->getType();
2372-
if (Ty->isVectorTy())
2373+
if (!UseIntrinsic && Ty->isVectorTy())
23732374
return Ret;
23742375

23752376
// exp2(sitofp(x)) -> ldexp(1.0, sext(x)) if sizeof(x) <= IntSize
@@ -2382,7 +2383,7 @@ Value *LibCallSimplifier::optimizeExp2(CallInst *CI, IRBuilderBase &B) {
23822383

23832384
// TODO: Emitting the intrinsic should not depend on whether the libcall
23842385
// is available.
2385-
if (CI->doesNotAccessMemory()) {
2386+
if (UseIntrinsic) {
23862387
return copyFlags(*CI, B.CreateIntrinsic(Intrinsic::ldexp,
23872388
{Ty, Exp->getType()},
23882389
{One, Exp}, CI));

llvm/test/Transforms/InstCombine/exp2-1.ll

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -308,20 +308,15 @@ define float @sitofp_scalar_intrinsic_with_FMF(i8 %x) {
308308

309309
define <2 x float> @sitofp_vector_intrinsic_with_FMF(<2 x i8> %x) {
310310
; LDEXP32-LABEL: @sitofp_vector_intrinsic_with_FMF(
311-
; LDEXP32-NEXT: [[S:%.*]] = sitofp <2 x i8> [[X:%.*]] to <2 x float>
312-
; LDEXP32-NEXT: [[R:%.*]] = call nnan <2 x float> @llvm.exp2.v2f32(<2 x float> [[S]])
311+
; LDEXP32-NEXT: [[TMP1:%.*]] = sext <2 x i8> [[X:%.*]] to <2 x i32>
312+
; LDEXP32-NEXT: [[R:%.*]] = call nnan <2 x float> @llvm.ldexp.v2f32.v2i32(<2 x float> <float 1.000000e+00, float 1.000000e+00>, <2 x i32> [[TMP1]])
313313
; LDEXP32-NEXT: ret <2 x float> [[R]]
314314
;
315315
; LDEXP16-LABEL: @sitofp_vector_intrinsic_with_FMF(
316-
; LDEXP16-NEXT: [[S:%.*]] = sitofp <2 x i8> [[X:%.*]] to <2 x float>
317-
; LDEXP16-NEXT: [[R:%.*]] = call nnan <2 x float> @llvm.exp2.v2f32(<2 x float> [[S]])
316+
; LDEXP16-NEXT: [[TMP1:%.*]] = sext <2 x i8> [[X:%.*]] to <2 x i16>
317+
; LDEXP16-NEXT: [[R:%.*]] = call nnan <2 x float> @llvm.ldexp.v2f32.v2i16(<2 x float> <float 1.000000e+00, float 1.000000e+00>, <2 x i16> [[TMP1]])
318318
; LDEXP16-NEXT: ret <2 x float> [[R]]
319319
;
320-
; NOLDEXPF-LABEL: @sitofp_vector_intrinsic_with_FMF(
321-
; NOLDEXPF-NEXT: [[S:%.*]] = sitofp <2 x i8> [[X:%.*]] to <2 x float>
322-
; NOLDEXPF-NEXT: [[R:%.*]] = call nnan <2 x float> @llvm.exp2.v2f32(<2 x float> [[S]])
323-
; NOLDEXPF-NEXT: ret <2 x float> [[R]]
324-
;
325320
; NOLDEXP-LABEL: @sitofp_vector_intrinsic_with_FMF(
326321
; NOLDEXP-NEXT: [[S:%.*]] = sitofp <2 x i8> [[X:%.*]] to <2 x float>
327322
; NOLDEXP-NEXT: [[R:%.*]] = call nnan <2 x float> @llvm.exp2.v2f32(<2 x float> [[S]])

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

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,17 @@ define float @exp2_f32_sitofp_i8_flags(i8 %x) {
3939
}
4040

4141
define <2 x float> @exp2_v2f32_sitofp_v2i8(<2 x i8> %x) {
42-
; CHECK-LABEL: define <2 x float> @exp2_v2f32_sitofp_v2i8(
43-
; CHECK-SAME: <2 x i8> [[X:%.*]]) {
44-
; CHECK-NEXT: [[ITOFP:%.*]] = sitofp <2 x i8> [[X]] to <2 x float>
45-
; CHECK-NEXT: [[EXP2:%.*]] = call <2 x float> @llvm.exp2.v2f32(<2 x float> [[ITOFP]])
46-
; CHECK-NEXT: ret <2 x float> [[EXP2]]
42+
; LDEXP-LABEL: define <2 x float> @exp2_v2f32_sitofp_v2i8(
43+
; LDEXP-SAME: <2 x i8> [[X:%.*]]) {
44+
; LDEXP-NEXT: [[TMP1:%.*]] = sext <2 x i8> [[X]] to <2 x i32>
45+
; LDEXP-NEXT: [[EXP2:%.*]] = call <2 x float> @llvm.ldexp.v2f32.v2i32(<2 x float> <float 1.000000e+00, float 1.000000e+00>, <2 x i32> [[TMP1]])
46+
; LDEXP-NEXT: ret <2 x float> [[EXP2]]
47+
;
48+
; NOLDEXP-LABEL: define <2 x float> @exp2_v2f32_sitofp_v2i8(
49+
; NOLDEXP-SAME: <2 x i8> [[X:%.*]]) {
50+
; NOLDEXP-NEXT: [[ITOFP:%.*]] = sitofp <2 x i8> [[X]] to <2 x float>
51+
; NOLDEXP-NEXT: [[EXP2:%.*]] = call <2 x float> @llvm.exp2.v2f32(<2 x float> [[ITOFP]])
52+
; NOLDEXP-NEXT: ret <2 x float> [[EXP2]]
4753
;
4854
%itofp = sitofp <2 x i8> %x to <2 x float>
4955
%exp2 = call <2 x float> @llvm.exp2.v2f32(<2 x float> %itofp)
@@ -117,11 +123,17 @@ define fp128 @exp2_fp128_sitofp_i8(i8 %x) {
117123
}
118124

119125
define <vscale x 4 x float> @exp2_nxv4f32_sitofp_i8(<vscale x 4 x i8> %x) {
120-
; CHECK-LABEL: define <vscale x 4 x float> @exp2_nxv4f32_sitofp_i8(
121-
; CHECK-SAME: <vscale x 4 x i8> [[X:%.*]]) {
122-
; CHECK-NEXT: [[ITOFP:%.*]] = sitofp <vscale x 4 x i8> [[X]] to <vscale x 4 x float>
123-
; CHECK-NEXT: [[EXP2:%.*]] = call <vscale x 4 x float> @llvm.exp2.nxv4f32(<vscale x 4 x float> [[ITOFP]])
124-
; CHECK-NEXT: ret <vscale x 4 x float> [[EXP2]]
126+
; LDEXP-LABEL: define <vscale x 4 x float> @exp2_nxv4f32_sitofp_i8(
127+
; LDEXP-SAME: <vscale x 4 x i8> [[X:%.*]]) {
128+
; LDEXP-NEXT: [[TMP1:%.*]] = sext <vscale x 4 x i8> [[X]] to <vscale x 4 x i32>
129+
; LDEXP-NEXT: [[EXP2:%.*]] = 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> [[TMP1]])
130+
; LDEXP-NEXT: ret <vscale x 4 x float> [[EXP2]]
131+
;
132+
; NOLDEXP-LABEL: define <vscale x 4 x float> @exp2_nxv4f32_sitofp_i8(
133+
; NOLDEXP-SAME: <vscale x 4 x i8> [[X:%.*]]) {
134+
; NOLDEXP-NEXT: [[ITOFP:%.*]] = sitofp <vscale x 4 x i8> [[X]] to <vscale x 4 x float>
135+
; NOLDEXP-NEXT: [[EXP2:%.*]] = call <vscale x 4 x float> @llvm.exp2.nxv4f32(<vscale x 4 x float> [[ITOFP]])
136+
; NOLDEXP-NEXT: ret <vscale x 4 x float> [[EXP2]]
125137
;
126138
%itofp = sitofp <vscale x 4 x i8> %x to <vscale x 4 x float>
127139
%exp2 = call <vscale x 4 x float> @llvm.exp2.nxv4f32(<vscale x 4 x float> %itofp)

llvm/test/Transforms/InstCombine/pow_fp_int.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -530,8 +530,8 @@ define double @powf_exp_const2_int_no_fast(double %base) {
530530
define <2 x float> @pow_sitofp_const_base_2_no_fast_vector(<2 x i8> %x) {
531531
; CHECK-LABEL: define <2 x float> @pow_sitofp_const_base_2_no_fast_vector(
532532
; CHECK-SAME: <2 x i8> [[X:%.*]]) {
533-
; CHECK-NEXT: [[S:%.*]] = sitofp <2 x i8> [[X]] to <2 x float>
534-
; CHECK-NEXT: [[EXP2:%.*]] = call <2 x float> @llvm.exp2.v2f32(<2 x float> [[S]])
533+
; CHECK-NEXT: [[TMP1:%.*]] = sext <2 x i8> [[X]] to <2 x i32>
534+
; CHECK-NEXT: [[EXP2:%.*]] = call <2 x float> @llvm.ldexp.v2f32.v2i32(<2 x float> <float 1.000000e+00, float 1.000000e+00>, <2 x i32> [[TMP1]])
535535
; CHECK-NEXT: ret <2 x float> [[EXP2]]
536536
;
537537
%s = sitofp <2 x i8> %x to <2 x float>

0 commit comments

Comments
 (0)