Unverified Commit 444ac341 authored by Roman Lebedev's avatar Roman Lebedev
Browse files

[APInt][PatternMatch] Add 'is non-positive' predicate

It will be useful for implementing the fold mentioned in
https://bugs.llvm.org/show_bug.cgi?id=44100#c4
parent 898df29c
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
@@ -389,6 +389,11 @@ public:
  /// \returns true if this APInt is positive.
  bool isStrictlyPositive() const { return isNonNegative() && !isNullValue(); }

  /// Determine if this APInt Value is non-positive (<= 0).
  ///
  /// \returns true if this APInt is non-positive.
  bool isNonPositive() const { return !isStrictlyPositive(); }

  /// Determine if all bits are set
  ///
  /// This checks to see if the value has all bits of the APInt are set or not.
+23 −1
Original line number Diff line number Diff line
@@ -366,7 +366,7 @@ inline api_pred_ty<is_negative> m_Negative(const APInt *&V) {
struct is_nonnegative {
  bool isValue(const APInt &C) { return C.isNonNegative(); }
};
/// Match an integer or vector of nonnegative values.
/// Match an integer or vector of non-negative values.
/// For vectors, this includes constants with undefined elements.
inline cst_pred_ty<is_nonnegative> m_NonNegative() {
  return cst_pred_ty<is_nonnegative>();
@@ -375,6 +375,28 @@ inline api_pred_ty<is_nonnegative> m_NonNegative(const APInt *&V) {
  return V;
}

struct is_strictlypositive {
  bool isValue(const APInt &C) { return C.isStrictlyPositive(); }
};
/// Match an integer or vector of strictly positive values.
/// For vectors, this includes constants with undefined elements.
inline cst_pred_ty<is_strictlypositive> m_StrictlyPositive() {
  return cst_pred_ty<is_strictlypositive>();
}
inline api_pred_ty<is_strictlypositive> m_StrictlyPositive(const APInt *&V) {
  return V;
}

struct is_nonpositive {
  bool isValue(const APInt &C) { return C.isNonPositive(); }
};
/// Match an integer or vector of non-positive values.
/// For vectors, this includes constants with undefined elements.
inline cst_pred_ty<is_nonpositive> m_NonPositive() {
  return cst_pred_ty<is_nonpositive>();
}
inline api_pred_ty<is_nonpositive> m_NonPositive(const APInt *&V) { return V; }

struct is_one {
  bool isValue(const APInt &C) { return C.isOneValue(); }
};
+17 −0
Original line number Diff line number Diff line
@@ -2845,4 +2845,21 @@ TEST(APIntTest, GetMostSignificantDifferentBitExaustive) {
  }
}

TEST(APIntTest, SignbitZeroChecks) {
  EXPECT_TRUE(APInt(8, -1).isNegative());
  EXPECT_FALSE(APInt(8, -1).isNonNegative());
  EXPECT_FALSE(APInt(8, -1).isStrictlyPositive());
  EXPECT_TRUE(APInt(8, -1).isNonPositive());

  EXPECT_FALSE(APInt(8, 0).isNegative());
  EXPECT_TRUE(APInt(8, 0).isNonNegative());
  EXPECT_FALSE(APInt(8, 0).isStrictlyPositive());
  EXPECT_TRUE(APInt(8, 0).isNonPositive());

  EXPECT_FALSE(APInt(8, 1).isNegative());
  EXPECT_TRUE(APInt(8, 1).isNonNegative());
  EXPECT_TRUE(APInt(8, 1).isStrictlyPositive());
  EXPECT_FALSE(APInt(8, 1).isNonPositive());
}

} // end anonymous namespace
+24 −0
Original line number Diff line number Diff line
@@ -182,6 +182,30 @@ TEST_F(PatternMatchTest, SpecificIntUGT) {
          .match(NegOne));
}

TEST_F(PatternMatchTest, SignbitZeroChecks) {
  Type *IntTy = IRB.getInt32Ty();
  unsigned BitWidth = IntTy->getScalarSizeInBits();

  Value *Zero = ConstantInt::get(IntTy, 0);
  Value *One = ConstantInt::get(IntTy, 1);
  Value *NegOne = ConstantInt::get(IntTy, -1);

  EXPECT_TRUE(m_Negative().match(NegOne));
  EXPECT_FALSE(m_NonNegative().match(NegOne));
  EXPECT_FALSE(m_StrictlyPositive().match(NegOne));
  EXPECT_TRUE(m_NonPositive().match(NegOne));

  EXPECT_FALSE(m_Negative().match(Zero));
  EXPECT_TRUE(m_NonNegative().match(Zero));
  EXPECT_FALSE(m_StrictlyPositive().match(Zero));
  EXPECT_TRUE(m_NonPositive().match(Zero));

  EXPECT_FALSE(m_Negative().match(One));
  EXPECT_TRUE(m_NonNegative().match(One));
  EXPECT_TRUE(m_StrictlyPositive().match(One));
  EXPECT_FALSE(m_NonPositive().match(One));
}

TEST_F(PatternMatchTest, SpecificIntUGE) {
  Type *IntTy = IRB.getInt32Ty();
  unsigned BitWidth = IntTy->getScalarSizeInBits();