Index: llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp =================================================================== --- llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp +++ llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp @@ -2275,6 +2275,23 @@ } } + // If we are clearing the sign bit of a floating-point value, convert this to + // fabs, then cast back to integer. + // + // Assumes any IEEE-represented type has the sign bit in the high bit. + // TODO: Unify with APInt matcher. This version allows undef unlike m_APInt + Value *CastOp; + if (match(Op0, m_BitCast(m_Value(CastOp))) && + match(Op1, m_MaxSignedValue())) { + Type *EltTy = CastOp->getType()->getScalarType(); + if (EltTy->isFloatingPointTy() && EltTy->isIEEE() && + EltTy->getPrimitiveSizeInBits() == + I.getType()->getScalarType()->getPrimitiveSizeInBits()) { + Value *FAbs = Builder.CreateUnaryIntrinsic(Intrinsic::fabs, CastOp); + return new BitCastInst(FAbs, I.getType()); + } + } + if (match(&I, m_And(m_OneUse(m_Shl(m_ZExt(m_Value(X)), m_Value(Y))), m_SignMask())) && match(Y, m_SpecificInt_ICMP( Index: llvm/test/Transforms/InstCombine/fabs-as-int.ll =================================================================== --- llvm/test/Transforms/InstCombine/fabs-as-int.ll +++ llvm/test/Transforms/InstCombine/fabs-as-int.ll @@ -4,10 +4,8 @@ define float @fabs_as_int_f32_castback(float %val) { ; CHECK-LABEL: define float @fabs_as_int_f32_castback ; CHECK-SAME: (float [[VAL:%.*]]) { -; CHECK-NEXT: [[BITCAST:%.*]] = bitcast float [[VAL]] to i32 -; CHECK-NEXT: [[AND:%.*]] = and i32 [[BITCAST]], 2147483647 -; CHECK-NEXT: [[FABS:%.*]] = bitcast i32 [[AND]] to float -; CHECK-NEXT: ret float [[FABS]] +; CHECK-NEXT: [[TMP1:%.*]] = call float @llvm.fabs.f32(float [[VAL]]) +; CHECK-NEXT: ret float [[TMP1]] ; %bitcast = bitcast float %val to i32 %and = and i32 %bitcast, 2147483647 @@ -18,10 +16,8 @@ define float @not_fabs_as_int_f32_castback_wrongconst(float %val) { ; CHECK-LABEL: define float @not_fabs_as_int_f32_castback_wrongconst ; CHECK-SAME: (float [[VAL:%.*]]) { -; CHECK-NEXT: [[BITCAST:%.*]] = bitcast float [[VAL]] to i32 -; CHECK-NEXT: [[AND:%.*]] = and i32 [[BITCAST]], 2147483647 -; CHECK-NEXT: [[FABS:%.*]] = bitcast i32 [[AND]] to float -; CHECK-NEXT: ret float [[FABS]] +; CHECK-NEXT: [[TMP1:%.*]] = call float @llvm.fabs.f32(float [[VAL]]) +; CHECK-NEXT: ret float [[TMP1]] ; %bitcast = bitcast float %val to i32 %and = and i32 %bitcast, 2147483647 @@ -32,11 +28,9 @@ define float @fabs_as_int_f32_castback_multi_use(float %val, ptr %ptr) { ; CHECK-LABEL: define float @fabs_as_int_f32_castback_multi_use ; CHECK-SAME: (float [[VAL:%.*]], ptr [[PTR:%.*]]) { -; CHECK-NEXT: [[BITCAST:%.*]] = bitcast float [[VAL]] to i32 -; CHECK-NEXT: [[AND:%.*]] = and i32 [[BITCAST]], 2147483647 -; CHECK-NEXT: store i32 [[AND]], ptr [[PTR]], align 4 -; CHECK-NEXT: [[FABS:%.*]] = bitcast i32 [[AND]] to float -; CHECK-NEXT: ret float [[FABS]] +; CHECK-NEXT: [[TMP1:%.*]] = call float @llvm.fabs.f32(float [[VAL]]) +; CHECK-NEXT: store float [[TMP1]], ptr [[PTR]], align 4 +; CHECK-NEXT: ret float [[TMP1]] ; %bitcast = bitcast float %val to i32 %and = and i32 %bitcast, 2147483647 @@ -48,8 +42,8 @@ define i64 @fabs_as_int_f64(double %x) { ; CHECK-LABEL: define i64 @fabs_as_int_f64 ; CHECK-SAME: (double [[X:%.*]]) { -; CHECK-NEXT: [[BC:%.*]] = bitcast double [[X]] to i64 -; CHECK-NEXT: [[AND:%.*]] = and i64 [[BC]], 9223372036854775807 +; CHECK-NEXT: [[TMP1:%.*]] = call double @llvm.fabs.f64(double [[X]]) +; CHECK-NEXT: [[AND:%.*]] = bitcast double [[TMP1]] to i64 ; CHECK-NEXT: ret i64 [[AND]] ; %bc = bitcast double %x to i64 @@ -60,8 +54,8 @@ define <2 x i64> @fabs_as_int_v2f64(<2 x double> %x) { ; CHECK-LABEL: define <2 x i64> @fabs_as_int_v2f64 ; CHECK-SAME: (<2 x double> [[X:%.*]]) { -; CHECK-NEXT: [[BC:%.*]] = bitcast <2 x double> [[X]] to <2 x i64> -; CHECK-NEXT: [[AND:%.*]] = and <2 x i64> [[BC]], +; CHECK-NEXT: [[TMP1:%.*]] = call <2 x double> @llvm.fabs.v2f64(<2 x double> [[X]]) +; CHECK-NEXT: [[AND:%.*]] = bitcast <2 x double> [[TMP1]] to <2 x i64> ; CHECK-NEXT: ret <2 x i64> [[AND]] ; %bc = bitcast <2 x double> %x to <2 x i64> @@ -72,8 +66,8 @@ define i64 @fabs_as_int_f64_swap(double %x) { ; CHECK-LABEL: define i64 @fabs_as_int_f64_swap ; CHECK-SAME: (double [[X:%.*]]) { -; CHECK-NEXT: [[BC:%.*]] = bitcast double [[X]] to i64 -; CHECK-NEXT: [[AND:%.*]] = and i64 [[BC]], 9223372036854775807 +; CHECK-NEXT: [[TMP1:%.*]] = call double @llvm.fabs.f64(double [[X]]) +; CHECK-NEXT: [[AND:%.*]] = bitcast double [[TMP1]] to i64 ; CHECK-NEXT: ret i64 [[AND]] ; %bc = bitcast double %x to i64 @@ -84,8 +78,8 @@ define i32 @fabs_as_int_f32(float %x) { ; CHECK-LABEL: define i32 @fabs_as_int_f32 ; CHECK-SAME: (float [[X:%.*]]) { -; CHECK-NEXT: [[BC:%.*]] = bitcast float [[X]] to i32 -; CHECK-NEXT: [[AND:%.*]] = and i32 [[BC]], 2147483647 +; CHECK-NEXT: [[TMP1:%.*]] = call float @llvm.fabs.f32(float [[X]]) +; CHECK-NEXT: [[AND:%.*]] = bitcast float [[TMP1]] to i32 ; CHECK-NEXT: ret i32 [[AND]] ; %bc = bitcast float %x to i32 @@ -96,8 +90,8 @@ define <2 x i32> @fabs_as_int_v2f32(<2 x float> %x) { ; CHECK-LABEL: define <2 x i32> @fabs_as_int_v2f32 ; CHECK-SAME: (<2 x float> [[X:%.*]]) { -; CHECK-NEXT: [[BC:%.*]] = bitcast <2 x float> [[X]] to <2 x i32> -; CHECK-NEXT: [[AND:%.*]] = and <2 x i32> [[BC]], +; CHECK-NEXT: [[TMP1:%.*]] = call <2 x float> @llvm.fabs.v2f32(<2 x float> [[X]]) +; CHECK-NEXT: [[AND:%.*]] = bitcast <2 x float> [[TMP1]] to <2 x i32> ; CHECK-NEXT: ret <2 x i32> [[AND]] ; %bc = bitcast <2 x float> %x to <2 x i32> @@ -120,8 +114,8 @@ define <3 x i32> @fabs_as_int_v3f32_undef(<3 x float> %x) { ; CHECK-LABEL: define <3 x i32> @fabs_as_int_v3f32_undef ; CHECK-SAME: (<3 x float> [[X:%.*]]) { -; CHECK-NEXT: [[BC:%.*]] = bitcast <3 x float> [[X]] to <3 x i32> -; CHECK-NEXT: [[AND:%.*]] = and <3 x i32> [[BC]], +; CHECK-NEXT: [[TMP1:%.*]] = call <3 x float> @llvm.fabs.v3f32(<3 x float> [[X]]) +; CHECK-NEXT: [[AND:%.*]] = bitcast <3 x float> [[TMP1]] to <3 x i32> ; CHECK-NEXT: ret <3 x i32> [[AND]] ; %bc = bitcast <3 x float> %x to <3 x i32> @@ -173,8 +167,8 @@ define i128 @fabs_as_int_fp128_f64_mask(fp128 %x) { ; CHECK-LABEL: define i128 @fabs_as_int_fp128_f64_mask ; CHECK-SAME: (fp128 [[X:%.*]]) { -; CHECK-NEXT: [[BC:%.*]] = bitcast fp128 [[X]] to i128 -; CHECK-NEXT: [[AND:%.*]] = and i128 [[BC]], 170141183460469231731687303715884105727 +; CHECK-NEXT: [[TMP1:%.*]] = call fp128 @llvm.fabs.f128(fp128 [[X]]) +; CHECK-NEXT: [[AND:%.*]] = bitcast fp128 [[TMP1]] to i128 ; CHECK-NEXT: ret i128 [[AND]] ; %bc = bitcast fp128 %x to i128 @@ -185,8 +179,8 @@ define i128 @fabs_as_int_fp128_f128_mask(fp128 %x) { ; CHECK-LABEL: define i128 @fabs_as_int_fp128_f128_mask ; CHECK-SAME: (fp128 [[X:%.*]]) { -; CHECK-NEXT: [[BC:%.*]] = bitcast fp128 [[X]] to i128 -; CHECK-NEXT: [[AND:%.*]] = and i128 [[BC]], 170141183460469231731687303715884105727 +; CHECK-NEXT: [[TMP1:%.*]] = call fp128 @llvm.fabs.f128(fp128 [[X]]) +; CHECK-NEXT: [[AND:%.*]] = bitcast fp128 [[TMP1]] to i128 ; CHECK-NEXT: ret i128 [[AND]] ; %bc = bitcast fp128 %x to i128 @@ -197,8 +191,8 @@ define i16 @fabs_as_int_f16(half %x) { ; CHECK-LABEL: define i16 @fabs_as_int_f16 ; CHECK-SAME: (half [[X:%.*]]) { -; CHECK-NEXT: [[BC:%.*]] = bitcast half [[X]] to i16 -; CHECK-NEXT: [[AND:%.*]] = and i16 [[BC]], 32767 +; CHECK-NEXT: [[TMP1:%.*]] = call half @llvm.fabs.f16(half [[X]]) +; CHECK-NEXT: [[AND:%.*]] = bitcast half [[TMP1]] to i16 ; CHECK-NEXT: ret i16 [[AND]] ; %bc = bitcast half %x to i16 @@ -209,8 +203,8 @@ define <2 x i16> @fabs_as_int_v2f16(<2 x half> %x) { ; CHECK-LABEL: define <2 x i16> @fabs_as_int_v2f16 ; CHECK-SAME: (<2 x half> [[X:%.*]]) { -; CHECK-NEXT: [[BC:%.*]] = bitcast <2 x half> [[X]] to <2 x i16> -; CHECK-NEXT: [[AND:%.*]] = and <2 x i16> [[BC]], +; CHECK-NEXT: [[TMP1:%.*]] = call <2 x half> @llvm.fabs.v2f16(<2 x half> [[X]]) +; CHECK-NEXT: [[AND:%.*]] = bitcast <2 x half> [[TMP1]] to <2 x i16> ; CHECK-NEXT: ret <2 x i16> [[AND]] ; %bc = bitcast <2 x half> %x to <2 x i16> @@ -221,8 +215,8 @@ define i16 @fabs_as_int_bf16(bfloat %x) { ; CHECK-LABEL: define i16 @fabs_as_int_bf16 ; CHECK-SAME: (bfloat [[X:%.*]]) { -; CHECK-NEXT: [[BC:%.*]] = bitcast bfloat [[X]] to i16 -; CHECK-NEXT: [[AND:%.*]] = and i16 [[BC]], 32767 +; CHECK-NEXT: [[TMP1:%.*]] = call bfloat @llvm.fabs.bf16(bfloat [[X]]) +; CHECK-NEXT: [[AND:%.*]] = bitcast bfloat [[TMP1]] to i16 ; CHECK-NEXT: ret i16 [[AND]] ; %bc = bitcast bfloat %x to i16 @@ -233,8 +227,8 @@ define <2 x i16> @fabs_as_int_v2bf16(<2 x bfloat> %x) { ; CHECK-LABEL: define <2 x i16> @fabs_as_int_v2bf16 ; CHECK-SAME: (<2 x bfloat> [[X:%.*]]) { -; CHECK-NEXT: [[BC:%.*]] = bitcast <2 x bfloat> [[X]] to <2 x i16> -; CHECK-NEXT: [[AND:%.*]] = and <2 x i16> [[BC]], +; CHECK-NEXT: [[TMP1:%.*]] = call <2 x bfloat> @llvm.fabs.v2bf16(<2 x bfloat> [[X]]) +; CHECK-NEXT: [[AND:%.*]] = bitcast <2 x bfloat> [[TMP1]] to <2 x i16> ; CHECK-NEXT: ret <2 x i16> [[AND]] ; %bc = bitcast <2 x bfloat> %x to <2 x i16>