[llvm] r359936 - Update PatternMatcher for FNeg

Cameron McInally via llvm-commits llvm-commits at lists.llvm.org
Fri May 3 14:19:12 PDT 2019


Author: mcinally
Date: Fri May  3 14:19:12 2019
New Revision: 359936

URL: http://llvm.org/viewvc/llvm-project?rev=359936&view=rev
Log:
Update PatternMatcher for FNeg

Match both FNeg(X) and FSub(+-0.0, X) in FNeg_match

Differential Revision: https://reviews.llvm.org/D61520

Modified:
    llvm/trunk/include/llvm/IR/PatternMatch.h
    llvm/trunk/unittests/IR/PatternMatch.cpp

Modified: llvm/trunk/include/llvm/IR/PatternMatch.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/IR/PatternMatch.h?rev=359936&r1=359935&r2=359936&view=diff
==============================================================================
--- llvm/trunk/include/llvm/IR/PatternMatch.h (original)
+++ llvm/trunk/include/llvm/IR/PatternMatch.h Fri May  3 14:19:12 2019
@@ -667,18 +667,26 @@ template <typename Op_t> struct FNeg_mat
   FNeg_match(const Op_t &Op) : X(Op) {}
   template <typename OpTy> bool match(OpTy *V) {
     auto *FPMO = dyn_cast<FPMathOperator>(V);
-    if (!FPMO || FPMO->getOpcode() != Instruction::FSub)
-      return false;
-    if (FPMO->hasNoSignedZeros()) {
-      // With 'nsz', any zero goes.
-      if (!cstfp_pred_ty<is_any_zero_fp>().match(FPMO->getOperand(0)))
-        return false;
-    } else {
-      // Without 'nsz', we need fsub -0.0, X exactly.
-      if (!cstfp_pred_ty<is_neg_zero_fp>().match(FPMO->getOperand(0)))
-        return false;
+    if (!FPMO) return false;
+
+    if (FPMO->getOpcode() == Instruction::FNeg)
+      return X.match(FPMO->getOperand(0));
+
+    if (FPMO->getOpcode() == Instruction::FSub) {
+      if (FPMO->hasNoSignedZeros()) {
+        // With 'nsz', any zero goes.
+        if (!cstfp_pred_ty<is_any_zero_fp>().match(FPMO->getOperand(0)))
+          return false;
+      } else {
+        // Without 'nsz', we need fsub -0.0, X exactly.
+        if (!cstfp_pred_ty<is_neg_zero_fp>().match(FPMO->getOperand(0)))
+          return false;
+      }
+
+      return X.match(FPMO->getOperand(1));
     }
-    return X.match(FPMO->getOperand(1));
+
+    return false;
   }
 };
 

Modified: llvm/trunk/unittests/IR/PatternMatch.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/IR/PatternMatch.cpp?rev=359936&r1=359935&r2=359936&view=diff
==============================================================================
--- llvm/trunk/unittests/IR/PatternMatch.cpp (original)
+++ llvm/trunk/unittests/IR/PatternMatch.cpp Fri May  3 14:19:12 2019
@@ -589,6 +589,35 @@ TEST_F(PatternMatchTest, VectorUndefFloa
   EXPECT_TRUE(match(VectorZeroUndef, m_AnyZeroFP()));
 }
 
+TEST_F(PatternMatchTest, FloatingPointFNeg) {
+  Type *FltTy = IRB.getFloatTy();
+  Value *One = ConstantFP::get(FltTy, 1.0);
+  Value *Z = ConstantFP::get(FltTy, 0.0);
+  Value *NZ = ConstantFP::get(FltTy, -0.0);
+  Value *V = IRB.CreateFNeg(One);
+  Value *V1 = IRB.CreateFSub(NZ, One);
+  Value *V2 = IRB.CreateFSub(Z, One);
+  Value *V3 = IRB.CreateFAdd(NZ, One);
+  Value *Match;
+
+  // Test FNeg(1.0)
+  EXPECT_TRUE(match(V, m_FNeg(m_Value(Match))));
+  EXPECT_EQ(One, Match);
+
+  // Test FSub(-0.0, 1.0)
+  EXPECT_TRUE(match(V1, m_FNeg(m_Value(Match))));
+  EXPECT_EQ(One, Match);
+
+  // Test FSub(0.0, 1.0)
+  EXPECT_FALSE(match(V2, m_FNeg(m_Value(Match))));
+  cast<Instruction>(V2)->setHasNoSignedZeros(true);
+  EXPECT_TRUE(match(V2, m_FNeg(m_Value(Match))));
+  EXPECT_EQ(One, Match);
+
+  // Test FAdd(-0.0, 1.0)
+  EXPECT_FALSE(match(V3, m_FNeg(m_Value(Match))));
+}
+
 template <typename T> struct MutableConstTest : PatternMatchTest { };
 
 typedef ::testing::Types<std::tuple<Value*, Instruction*>,




More information about the llvm-commits mailing list