Skip to content

Commit 08f0388

Browse files
committed
InstCombine: Fold and/or of fcmp into class
This is motivated by patterns like !isfinite || zero. The AMDGPU math libraries have a lot of patterns like this, and I'm trying to fix the code to be more portable and less dependent on directly calling class intrinsics. I believe this is the first place where new is.fpclass calls are introduced. There are more class-like compares that could be recognized; this is a set I currently care about plus a few extras. Keep the == 0 cases disabled for now. It depends on the denormal mode. If we just check IEEE mode now, it will break my use case without another patch I'm working on.
1 parent cbcb3ee commit 08f0388

File tree

3 files changed

+637
-813
lines changed

3 files changed

+637
-813
lines changed

llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1495,6 +1495,25 @@ Value *InstCombinerImpl::foldLogicOfFCmps(FCmpInst *LHS, FCmpInst *RHS,
14951495
return Right;
14961496
}
14971497

1498+
// Turn at least two fcmps with constants into llvm.is.fpclass.
1499+
//
1500+
// If we can represent a combined value test with one class call, we can
1501+
// potentially eliminate 4-6 instructions. If we can represent a test with a
1502+
// single fcmp with fneg and fabs, that's likely a better canonical form.
1503+
if (LHS->hasOneUse() && RHS->hasOneUse()) {
1504+
auto [ClassValRHS, ClassMaskRHS] = fcmpToClassTest(PredR, RHS0, RHS1);
1505+
if (ClassValRHS) {
1506+
auto [ClassValLHS, ClassMaskLHS] = fcmpToClassTest(PredL, LHS0, LHS1);
1507+
if (ClassValLHS == ClassValRHS) {
1508+
unsigned CombinedMask = IsAnd ? (ClassMaskLHS & ClassMaskRHS)
1509+
: (ClassMaskLHS | ClassMaskRHS);
1510+
return Builder.CreateIntrinsic(
1511+
Intrinsic::is_fpclass, {ClassValLHS->getType()},
1512+
{ClassValLHS, Builder.getInt32(CombinedMask)});
1513+
}
1514+
}
1515+
}
1516+
14981517
return nullptr;
14991518
}
15001519

0 commit comments

Comments
 (0)