[llvm] ValueTracking: Teach computeKnownFPClass to look at bitcast + integer max (PR #184073)
Matt Arsenault via llvm-commits
llvm-commits at lists.llvm.org
Mon Mar 2 03:04:37 PST 2026
https://github.com/arsenm updated https://github.com/llvm/llvm-project/pull/184073
>From 0e9cb93475d6637a4b1fab77e5249b5aa841e8ab Mon Sep 17 00:00:00 2001
From: Matt Arsenault <Matthew.Arsenault at amd.com>
Date: Sat, 28 Feb 2026 16:23:17 +0100
Subject: [PATCH] ValueTracking: Teach computeKnownFPClass to look at bitcast +
integer max
The returned class will still be one of the bitpatterns.
This pattern is used in rocm device libraries in assorted functions, e.g.,
https://github.com/ROCm/llvm-project/blob/amd-staging/amd/device-libs/ocml/src/rlen3F.cl#L20
I believe it is blocking the eliminationg of finite checks in some of the more
complex functions.
---
llvm/lib/Analysis/ValueTracking.cpp | 34 ++-
.../Attributor/nofpclass-bitcast.ll | 213 ++++++++++++++++++
2 files changed, 240 insertions(+), 7 deletions(-)
create mode 100644 llvm/test/Transforms/Attributor/nofpclass-bitcast.ll
diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index 3f925844a4020..6f496f120a9d1 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -5971,8 +5971,28 @@ void computeKnownFPClass(const Value *V, const APInt &DemandedElts,
!Src->getType()->isIntOrIntVectorTy())
break;
- const Type *Ty = Op->getType()->getScalarType();
- KnownBits Bits(Ty->getScalarSizeInBits());
+ const Type *Ty = Op->getType();
+
+ Value *CastLHS, *CastRHS;
+
+ // Match bitcast(umax(bitcast(a), bitcast(b)))
+ if (match(Src, m_c_MaxOrMin(m_BitCast(m_Value(CastLHS)),
+ m_BitCast(m_Value(CastRHS)))) &&
+ CastLHS->getType() == Ty && CastRHS->getType() == Ty) {
+ KnownFPClass KnownLHS, KnownRHS;
+ computeKnownFPClass(CastRHS, DemandedElts, InterestedClasses, KnownRHS, Q,
+ Depth + 1);
+ if (!KnownRHS.isUnknown()) {
+ computeKnownFPClass(CastLHS, DemandedElts, InterestedClasses, KnownLHS,
+ Q, Depth + 1);
+ Known = KnownLHS | KnownRHS;
+ }
+
+ return;
+ }
+
+ const Type *EltTy = Ty->getScalarType();
+ KnownBits Bits(EltTy->getPrimitiveSizeInBits());
computeKnownBits(Src, DemandedElts, Bits, Q, Depth + 1);
// Transfer information from the sign bit.
@@ -5981,7 +6001,7 @@ void computeKnownFPClass(const Value *V, const APInt &DemandedElts,
else if (Bits.isNegative())
Known.signBitMustBeOne();
- if (Ty->isIEEELikeFPTy()) {
+ if (EltTy->isIEEELikeFPTy()) {
// IEEE floats are NaN when all bits of the exponent plus at least one of
// the fraction bits are 1. This means:
// - If we assume unknown bits are 0 and the value is NaN, it will
@@ -5989,15 +6009,15 @@ void computeKnownFPClass(const Value *V, const APInt &DemandedElts,
// - If we assume unknown bits are 1 and the value is not NaN, it can
// never be NaN
// Note: They do not hold for x86_fp80 format.
- if (APFloat(Ty->getFltSemantics(), Bits.One).isNaN())
+ if (APFloat(EltTy->getFltSemantics(), Bits.One).isNaN())
Known.KnownFPClasses = fcNan;
- else if (!APFloat(Ty->getFltSemantics(), ~Bits.Zero).isNaN())
+ else if (!APFloat(EltTy->getFltSemantics(), ~Bits.Zero).isNaN())
Known.knownNot(fcNan);
// Build KnownBits representing Inf and check if it must be equal or
// unequal to this value.
auto InfKB = KnownBits::makeConstant(
- APFloat::getInf(Ty->getFltSemantics()).bitcastToAPInt());
+ APFloat::getInf(EltTy->getFltSemantics()).bitcastToAPInt());
InfKB.Zero.clearSignBit();
if (const auto InfResult = KnownBits::eq(Bits, InfKB)) {
assert(!InfResult.value());
@@ -6009,7 +6029,7 @@ void computeKnownFPClass(const Value *V, const APInt &DemandedElts,
// Build KnownBits representing Zero and check if it must be equal or
// unequal to this value.
auto ZeroKB = KnownBits::makeConstant(
- APFloat::getZero(Ty->getFltSemantics()).bitcastToAPInt());
+ APFloat::getZero(EltTy->getFltSemantics()).bitcastToAPInt());
ZeroKB.Zero.clearSignBit();
if (const auto ZeroResult = KnownBits::eq(Bits, ZeroKB)) {
assert(!ZeroResult.value());
diff --git a/llvm/test/Transforms/Attributor/nofpclass-bitcast.ll b/llvm/test/Transforms/Attributor/nofpclass-bitcast.ll
new file mode 100644
index 0000000000000..267d0ad37d131
--- /dev/null
+++ b/llvm/test/Transforms/Attributor/nofpclass-bitcast.ll
@@ -0,0 +1,213 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6
+; RUN: opt -S -aa-pipeline=basic-aa -passes=attributor -attributor-manifest-internal < %s | FileCheck --check-prefixes=CHECK,TUNIT %s
+
+define float @bitcast_umax_not_nan(float nofpclass(nan) %x, float nofpclass(nan) %y) {
+; CHECK-LABEL: define nofpclass(nan) float @bitcast_umax_not_nan(
+; CHECK-SAME: float nofpclass(nan) [[X:%.*]], float nofpclass(nan) [[Y:%.*]]) #[[ATTR0:[0-9]+]] {
+; CHECK-NEXT: [[CAST_X:%.*]] = bitcast float [[X]] to i32
+; CHECK-NEXT: [[CAST_Y:%.*]] = bitcast float [[Y]] to i32
+; CHECK-NEXT: [[UMAX:%.*]] = call i32 @llvm.umax.i32(i32 [[CAST_X]], i32 [[CAST_Y]]) #[[ATTR2:[0-9]+]]
+; CHECK-NEXT: [[CAST:%.*]] = bitcast i32 [[UMAX]] to float
+; CHECK-NEXT: ret float [[CAST]]
+;
+ %cast.x = bitcast float %x to i32
+ %cast.y = bitcast float %y to i32
+ %umax = call i32 @llvm.umax.i32(i32 %cast.x, i32 %cast.y)
+ %cast = bitcast i32 %umax to float
+ ret float %cast
+}
+
+define float @bitcast_umin_not_nan(float nofpclass(nan) %x, float nofpclass(nan) %y) {
+; CHECK-LABEL: define nofpclass(nan) float @bitcast_umin_not_nan(
+; CHECK-SAME: float nofpclass(nan) [[X:%.*]], float nofpclass(nan) [[Y:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT: [[CAST_X:%.*]] = bitcast float [[X]] to i32
+; CHECK-NEXT: [[CAST_Y:%.*]] = bitcast float [[Y]] to i32
+; CHECK-NEXT: [[UMIN:%.*]] = call i32 @llvm.umin.i32(i32 [[CAST_X]], i32 [[CAST_Y]]) #[[ATTR2]]
+; CHECK-NEXT: [[CAST:%.*]] = bitcast i32 [[UMIN]] to float
+; CHECK-NEXT: ret float [[CAST]]
+;
+ %cast.x = bitcast float %x to i32
+ %cast.y = bitcast float %y to i32
+ %umin = call i32 @llvm.umin.i32(i32 %cast.x, i32 %cast.y)
+ %cast = bitcast i32 %umin to float
+ ret float %cast
+}
+
+define float @bitcast_smax_not_nan(float nofpclass(nan) %x, float nofpclass(nan) %y) {
+; CHECK-LABEL: define nofpclass(nan) float @bitcast_smax_not_nan(
+; CHECK-SAME: float nofpclass(nan) [[X:%.*]], float nofpclass(nan) [[Y:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT: [[CAST_X:%.*]] = bitcast float [[X]] to i32
+; CHECK-NEXT: [[CAST_Y:%.*]] = bitcast float [[Y]] to i32
+; CHECK-NEXT: [[SMAX:%.*]] = call i32 @llvm.smax.i32(i32 [[CAST_X]], i32 [[CAST_Y]]) #[[ATTR2]]
+; CHECK-NEXT: [[CAST:%.*]] = bitcast i32 [[SMAX]] to float
+; CHECK-NEXT: ret float [[CAST]]
+;
+ %cast.x = bitcast float %x to i32
+ %cast.y = bitcast float %y to i32
+ %smax = call i32 @llvm.smax.i32(i32 %cast.x, i32 %cast.y)
+ %cast = bitcast i32 %smax to float
+ ret float %cast
+}
+
+define float @bitcast_smin_not_nan(float nofpclass(nan) %x, float nofpclass(nan) %y) {
+; CHECK-LABEL: define nofpclass(nan) float @bitcast_smin_not_nan(
+; CHECK-SAME: float nofpclass(nan) [[X:%.*]], float nofpclass(nan) [[Y:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT: [[CAST_X:%.*]] = bitcast float [[X]] to i32
+; CHECK-NEXT: [[CAST_Y:%.*]] = bitcast float [[Y]] to i32
+; CHECK-NEXT: [[SMIN:%.*]] = call i32 @llvm.smin.i32(i32 [[CAST_X]], i32 [[CAST_Y]]) #[[ATTR2]]
+; CHECK-NEXT: [[CAST:%.*]] = bitcast i32 [[SMIN]] to float
+; CHECK-NEXT: ret float [[CAST]]
+;
+ %cast.x = bitcast float %x to i32
+ %cast.y = bitcast float %y to i32
+ %smin = call i32 @llvm.smin.i32(i32 %cast.x, i32 %cast.y)
+ %cast = bitcast i32 %smin to float
+ ret float %cast
+}
+
+define <2 x float> @bitcast_umax_not_nan_vec(<2 x float> nofpclass(nan) %x, <2 x float> nofpclass(nan) %y) {
+; CHECK-LABEL: define nofpclass(nan) <2 x float> @bitcast_umax_not_nan_vec(
+; CHECK-SAME: <2 x float> nofpclass(nan) [[X:%.*]], <2 x float> nofpclass(nan) [[Y:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT: [[CAST_X:%.*]] = bitcast <2 x float> [[X]] to <2 x i32>
+; CHECK-NEXT: [[CAST_Y:%.*]] = bitcast <2 x float> [[Y]] to <2 x i32>
+; CHECK-NEXT: [[UMAX:%.*]] = call <2 x i32> @llvm.umax.v2i32(<2 x i32> [[CAST_X]], <2 x i32> [[CAST_Y]]) #[[ATTR2]]
+; CHECK-NEXT: [[CAST:%.*]] = bitcast <2 x i32> [[UMAX]] to <2 x float>
+; CHECK-NEXT: ret <2 x float> [[CAST]]
+;
+ %cast.x = bitcast <2 x float> %x to <2 x i32>
+ %cast.y = bitcast <2 x float> %y to <2 x i32>
+ %umax = call <2 x i32> @llvm.umax.v2i32(<2 x i32> %cast.x, <2 x i32> %cast.y)
+ %cast = bitcast <2 x i32> %umax to <2 x float>
+ ret <2 x float> %cast
+}
+
+define float @bitcast_umax_mismatch_different(float nofpclass(inf) %x, float nofpclass(nan) %y) {
+; CHECK-LABEL: define float @bitcast_umax_mismatch_different(
+; CHECK-SAME: float nofpclass(inf) [[X:%.*]], float nofpclass(nan) [[Y:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT: [[CAST_X:%.*]] = bitcast float [[X]] to i32
+; CHECK-NEXT: [[CAST_Y:%.*]] = bitcast float [[Y]] to i32
+; CHECK-NEXT: [[UMAX:%.*]] = call i32 @llvm.umax.i32(i32 [[CAST_X]], i32 [[CAST_Y]]) #[[ATTR2]]
+; CHECK-NEXT: [[CAST:%.*]] = bitcast i32 [[UMAX]] to float
+; CHECK-NEXT: ret float [[CAST]]
+;
+ %cast.x = bitcast float %x to i32
+ %cast.y = bitcast float %y to i32
+ %umax = call i32 @llvm.umax.i32(i32 %cast.x, i32 %cast.y)
+ %cast = bitcast i32 %umax to float
+ ret float %cast
+}
+
+define float @bitcast_umax_missing_lhs(float %x, float nofpclass(nan) %y) {
+; CHECK-LABEL: define float @bitcast_umax_missing_lhs(
+; CHECK-SAME: float [[X:%.*]], float nofpclass(nan) [[Y:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT: [[CAST_X:%.*]] = bitcast float [[X]] to i32
+; CHECK-NEXT: [[CAST_Y:%.*]] = bitcast float [[Y]] to i32
+; CHECK-NEXT: [[UMAX:%.*]] = call i32 @llvm.umax.i32(i32 [[CAST_X]], i32 [[CAST_Y]]) #[[ATTR2]]
+; CHECK-NEXT: [[CAST:%.*]] = bitcast i32 [[UMAX]] to float
+; CHECK-NEXT: ret float [[CAST]]
+;
+ %cast.x = bitcast float %x to i32
+ %cast.y = bitcast float %y to i32
+ %umax = call i32 @llvm.umax.i32(i32 %cast.x, i32 %cast.y)
+ %cast = bitcast i32 %umax to float
+ ret float %cast
+}
+
+define float @bitcast_umax_missing_rhs(float nofpclass(nan) %x, float %y) {
+; CHECK-LABEL: define float @bitcast_umax_missing_rhs(
+; CHECK-SAME: float nofpclass(nan) [[X:%.*]], float [[Y:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT: [[CAST_X:%.*]] = bitcast float [[X]] to i32
+; CHECK-NEXT: [[CAST_Y:%.*]] = bitcast float [[Y]] to i32
+; CHECK-NEXT: [[UMAX:%.*]] = call i32 @llvm.umax.i32(i32 [[CAST_X]], i32 [[CAST_Y]]) #[[ATTR2]]
+; CHECK-NEXT: [[CAST:%.*]] = bitcast i32 [[UMAX]] to float
+; CHECK-NEXT: ret float [[CAST]]
+;
+ %cast.x = bitcast float %x to i32
+ %cast.y = bitcast float %y to i32
+ %umax = call i32 @llvm.umax.i32(i32 %cast.x, i32 %cast.y)
+ %cast = bitcast i32 %umax to float
+ ret float %cast
+}
+
+define float @bitcast_umax_not_nan__not_elementwise_cast(<2 x half> nofpclass(nan) %x, <2 x half> nofpclass(nan) %y) {
+; CHECK-LABEL: define float @bitcast_umax_not_nan__not_elementwise_cast(
+; CHECK-SAME: <2 x half> nofpclass(nan) [[X:%.*]], <2 x half> nofpclass(nan) [[Y:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT: [[CAST_X:%.*]] = bitcast <2 x half> [[X]] to i32
+; CHECK-NEXT: [[CAST_Y:%.*]] = bitcast <2 x half> [[Y]] to i32
+; CHECK-NEXT: [[UMAX:%.*]] = call i32 @llvm.umax.i32(i32 [[CAST_X]], i32 [[CAST_Y]]) #[[ATTR2]]
+; CHECK-NEXT: [[CAST:%.*]] = bitcast i32 [[UMAX]] to float
+; CHECK-NEXT: ret float [[CAST]]
+;
+ %cast.x = bitcast <2 x half> %x to i32
+ %cast.y = bitcast <2 x half> %y to i32
+ %umax = call i32 @llvm.umax.i32(i32 %cast.x, i32 %cast.y)
+ %cast = bitcast i32 %umax to float
+ ret float %cast
+}
+
+define bfloat @bitcast_wrong_type_umax_not_nan(half nofpclass(nan) %x, half nofpclass(nan) %y) {
+; CHECK-LABEL: define bfloat @bitcast_wrong_type_umax_not_nan(
+; CHECK-SAME: half nofpclass(nan) [[X:%.*]], half nofpclass(nan) [[Y:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT: [[CAST_X:%.*]] = bitcast half [[X]] to i16
+; CHECK-NEXT: [[CAST_Y:%.*]] = bitcast half [[Y]] to i16
+; CHECK-NEXT: [[UMAX:%.*]] = call i16 @llvm.umax.i16(i16 [[CAST_X]], i16 [[CAST_Y]]) #[[ATTR2]]
+; CHECK-NEXT: [[CAST:%.*]] = bitcast i16 [[UMAX]] to bfloat
+; CHECK-NEXT: ret bfloat [[CAST]]
+;
+ %cast.x = bitcast half %x to i16
+ %cast.y = bitcast half %y to i16
+ %umax = call i16 @llvm.umax.i16(i16 %cast.x, i16 %cast.y)
+ %cast = bitcast i16 %umax to bfloat
+ ret bfloat %cast
+}
+
+define <2 x bfloat> @bitcast_wrong_type_umax_vec_not_nan(<2 x half> nofpclass(nan) %x, <2 x half> nofpclass(nan) %y) {
+; CHECK-LABEL: define <2 x bfloat> @bitcast_wrong_type_umax_vec_not_nan(
+; CHECK-SAME: <2 x half> nofpclass(nan) [[X:%.*]], <2 x half> nofpclass(nan) [[Y:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT: [[CAST_X:%.*]] = bitcast <2 x half> [[X]] to <2 x i16>
+; CHECK-NEXT: [[CAST_Y:%.*]] = bitcast <2 x half> [[Y]] to <2 x i16>
+; CHECK-NEXT: [[UMAX:%.*]] = call <2 x i16> @llvm.umax.v2i16(<2 x i16> [[CAST_X]], <2 x i16> [[CAST_Y]]) #[[ATTR2]]
+; CHECK-NEXT: [[CAST:%.*]] = bitcast <2 x i16> [[UMAX]] to <2 x bfloat>
+; CHECK-NEXT: ret <2 x bfloat> [[CAST]]
+;
+ %cast.x = bitcast <2 x half> %x to <2 x i16>
+ %cast.y = bitcast <2 x half> %y to <2 x i16>
+ %umax = call <2 x i16> @llvm.umax.v2i16(<2 x i16> %cast.x, <2 x i16> %cast.y)
+ %cast = bitcast <2 x i16> %umax to <2 x bfloat>
+ ret <2 x bfloat> %cast
+}
+
+define half @bitcast_wrong_type_lhs_umax_not_nan(bfloat nofpclass(nan) %x, half nofpclass(nan) %y) {
+; CHECK-LABEL: define half @bitcast_wrong_type_lhs_umax_not_nan(
+; CHECK-SAME: bfloat nofpclass(nan) [[X:%.*]], half nofpclass(nan) [[Y:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT: [[CAST_X:%.*]] = bitcast bfloat [[X]] to i16
+; CHECK-NEXT: [[CAST_Y:%.*]] = bitcast half [[Y]] to i16
+; CHECK-NEXT: [[UMAX:%.*]] = call i16 @llvm.umax.i16(i16 [[CAST_X]], i16 [[CAST_Y]]) #[[ATTR2]]
+; CHECK-NEXT: [[CAST:%.*]] = bitcast i16 [[UMAX]] to half
+; CHECK-NEXT: ret half [[CAST]]
+;
+ %cast.x = bitcast bfloat %x to i16
+ %cast.y = bitcast half %y to i16
+ %umax = call i16 @llvm.umax.i16(i16 %cast.x, i16 %cast.y)
+ %cast = bitcast i16 %umax to half
+ ret half %cast
+}
+
+define half @bitcast_wrong_type_rhs_umax_not_nan(half nofpclass(nan) %x, bfloat nofpclass(nan) %y) {
+; CHECK-LABEL: define half @bitcast_wrong_type_rhs_umax_not_nan(
+; CHECK-SAME: half nofpclass(nan) [[X:%.*]], bfloat nofpclass(nan) [[Y:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT: [[CAST_X:%.*]] = bitcast half [[X]] to i16
+; CHECK-NEXT: [[CAST_Y:%.*]] = bitcast bfloat [[Y]] to i16
+; CHECK-NEXT: [[UMAX:%.*]] = call i16 @llvm.umax.i16(i16 [[CAST_X]], i16 [[CAST_Y]]) #[[ATTR2]]
+; CHECK-NEXT: [[CAST:%.*]] = bitcast i16 [[UMAX]] to half
+; CHECK-NEXT: ret half [[CAST]]
+;
+ %cast.x = bitcast half %x to i16
+ %cast.y = bitcast bfloat %y to i16
+ %umax = call i16 @llvm.umax.i16(i16 %cast.x, i16 %cast.y)
+ %cast = bitcast i16 %umax to half
+ ret half %cast
+}
+
+;; NOTE: These prefixes are unused and the list is autogenerated. Do not add tests below this line:
+; TUNIT: {{.*}}
More information about the llvm-commits
mailing list