[llvm] PatternMatch: Add matchers for positive or negative infinity (PR #188933)
via llvm-commits
llvm-commits at lists.llvm.org
Fri Mar 27 02:26:01 PDT 2026
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-llvm-ir
Author: Matt Arsenault (arsenm)
<details>
<summary>Changes</summary>
The existing m_Inf deceptively matches both positive and negative
infinities. Add variants that match the specific sign.
---
Full diff: https://github.com/llvm/llvm-project/pull/188933.diff
2 Files Affected:
- (modified) llvm/include/llvm/IR/PatternMatch.h (+18)
- (modified) llvm/unittests/IR/PatternMatch.cpp (+6)
``````````diff
diff --git a/llvm/include/llvm/IR/PatternMatch.h b/llvm/include/llvm/IR/PatternMatch.h
index 94ae65a24b3e2..2160146245137 100644
--- a/llvm/include/llvm/IR/PatternMatch.h
+++ b/llvm/include/llvm/IR/PatternMatch.h
@@ -763,6 +763,24 @@ struct is_inf {
/// For vectors, this includes constants with undefined elements.
inline cstfp_pred_ty<is_inf> m_Inf() { return cstfp_pred_ty<is_inf>(); }
+template <bool IsNegative> struct is_signed_inf {
+ bool isValue(const APFloat &C) const {
+ return C.isInfinity() && IsNegative == C.isNegative();
+ }
+};
+
+/// Match a positive infinity FP constant.
+/// For vectors, this includes constants with undefined elements.
+inline cstfp_pred_ty<is_signed_inf<false>> m_PosInf() {
+ return cstfp_pred_ty<is_signed_inf<false>>();
+}
+
+/// Match a negative infinity FP constant.
+/// For vectors, this includes constants with undefined elements.
+inline cstfp_pred_ty<is_signed_inf<true>> m_NegInf() {
+ return cstfp_pred_ty<is_signed_inf<true>>();
+}
+
struct is_noninf {
bool isValue(const APFloat &C) const { return !C.isInfinity(); }
};
diff --git a/llvm/unittests/IR/PatternMatch.cpp b/llvm/unittests/IR/PatternMatch.cpp
index 8a4385b62ab72..8cbec2cba8392 100644
--- a/llvm/unittests/IR/PatternMatch.cpp
+++ b/llvm/unittests/IR/PatternMatch.cpp
@@ -1755,6 +1755,12 @@ TEST_F(PatternMatchTest, VectorUndefFloat) {
EXPECT_TRUE(match(VectorInfPoison, m_Inf()));
EXPECT_FALSE(match(VectorNaNPoison, m_Inf()));
+ EXPECT_TRUE(match(ScalarPosInf, m_PosInf()));
+ EXPECT_FALSE(match(ScalarNegInf, m_PosInf()));
+
+ EXPECT_FALSE(match(ScalarPosInf, m_NegInf()));
+ EXPECT_TRUE(match(ScalarNegInf, m_NegInf()));
+
EXPECT_FALSE(match(ScalarUndef, m_NonInf()));
EXPECT_FALSE(match(VectorUndef, m_NonInf()));
EXPECT_FALSE(match(VectorZeroUndef, m_NonInf()));
``````````
</details>
https://github.com/llvm/llvm-project/pull/188933
More information about the llvm-commits
mailing list