Skip to content

ADT: Replace FPClassTest fabs with inverse_fabs and unknown_sign #66390

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 14, 2023

Conversation

arsenm
Copy link
Contributor

@arsenm arsenm commented Sep 14, 2023

The names are confusing. It's supposed to return the possible classes for the source value of a fabs. Some places were using fneg as a way to express unknown sign, but it's clearer to have something separate for this.

unknown_sign will be used in a subsequent patch and avoids a small bug in copysign handling.

The names are confusing. It's supposed to return the possible classes
for the source value of a fabs. Some places were using fneg as a
way to express unknown sign, but it's clearer to have something
separate for this.

unknown_sign will be used in a subsequent patch and avoids
a small bug in copysign handling.
@llvmbot
Copy link
Member

llvmbot commented Sep 14, 2023

@llvm/pr-subscribers-llvm-transforms

Changes The names are confusing. It's supposed to return the possible classes for the source value of a fabs. Some places were using fneg as a way to express unknown sign, but it's clearer to have something separate for this.

unknown_sign will be used in a subsequent patch and avoids a small bug in copysign handling.

Full diff: https://github.com/llvm/llvm-project/pull/66390.diff

3 Files Affected:

  • (modified) llvm/include/llvm/ADT/FloatingPointMode.h (+6-2)
  • (modified) llvm/lib/Support/FloatingPointMode.cpp (+14-1)
  • (modified) llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp (+1-1)
diff --git a/llvm/include/llvm/ADT/FloatingPointMode.h b/llvm/include/llvm/ADT/FloatingPointMode.h
index d80a811fd447a78..468de085d1bede4 100644
--- a/llvm/include/llvm/ADT/FloatingPointMode.h
+++ b/llvm/include/llvm/ADT/FloatingPointMode.h
@@ -269,8 +269,12 @@ LLVM_DECLARE_ENUM_AS_BITMASK(FPClassTest, /* LargestValue */ fcPosInf);
 /// Return the test mask which returns true if the value's sign bit is flipped.
 FPClassTest fneg(FPClassTest Mask);
 
-/// Return the test mask which returns true if the value's sign bit is cleared.
-FPClassTest fabs(FPClassTest Mask);
+/// Return the test mask which returns true after fabs is applied to the value.
+FPClassTest inverse_fabs(FPClassTest Mask);
+
+/// Return the test mask which returns true if the value could have the same set
+/// of classes, but with a different sign.
+FPClassTest unknown_sign(FPClassTest Mask);
 
 /// Write a human readable form of \p Mask to \p OS
 raw_ostream &operator<<(raw_ostream &OS, FPClassTest Mask);
diff --git a/llvm/lib/Support/FloatingPointMode.cpp b/llvm/lib/Support/FloatingPointMode.cpp
index 9543884ff46edd5..5a2836eb8243422 100644
--- a/llvm/lib/Support/FloatingPointMode.cpp
+++ b/llvm/lib/Support/FloatingPointMode.cpp
@@ -32,7 +32,7 @@ FPClassTest llvm::fneg(FPClassTest Mask) {
   return NewMask;
 }
 
-FPClassTest llvm::fabs(FPClassTest Mask) {
+FPClassTest llvm::inverse_fabs(FPClassTest Mask) {
   FPClassTest NewMask = Mask & fcNan;
   if (Mask & fcPosZero)
     NewMask |= fcZero;
@@ -45,6 +45,19 @@ FPClassTest llvm::fabs(FPClassTest Mask) {
   return NewMask;
 }
 
+FPClassTest llvm::unknown_sign(FPClassTest Mask) {
+  FPClassTest NewMask = Mask & fcNan;
+  if (Mask & fcZero)
+    NewMask |= fcZero;
+  if (Mask & fcSubnormal)
+    NewMask |= fcSubnormal;
+  if (Mask & fcNormal)
+    NewMask |= fcNormal;
+  if (Mask & fcInf)
+    NewMask |= fcInf;
+  return NewMask;
+}
+
 // Every bitfield has a unique name and one or more aliasing names that cover
 // multiple bits. Names should be listed in order of preference, with higher
 // popcounts listed first.
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
index 17cfd90ac209392..c6100f24b0507de 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
@@ -899,7 +899,7 @@ Instruction *InstCombinerImpl::foldIntrinsicIsFPClass(IntrinsicInst &II) {
 
   Value *FAbsSrc;
   if (match(Src0, m_FAbs(m_Value(FAbsSrc)))) {
-    II.setArgOperand(1, ConstantInt::get(Src1->getType(), fabs(Mask)));
+    II.setArgOperand(1, ConstantInt::get(Src1->getType(), inverse_fabs(Mask)));
     return replaceOperand(II, 0, FAbsSrc);
   }
 

Copy link
Collaborator

@RKSimon RKSimon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM - cheers

@arsenm arsenm merged commit 07acfe3 into llvm:main Sep 14, 2023
@arsenm arsenm deleted the rename-fpclasstest-fabs branch September 14, 2023 16:47
ZijunZhaoCCK pushed a commit to ZijunZhaoCCK/llvm-project that referenced this pull request Sep 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants