Skip to content

Commit fe107fc

Browse files
committedJun 29, 2019
[IR][Patternmatch] Add m_SpecificInt_ULT() predicate
Summary: Match an integer or vector with every element unsigned less than the Threshold. For vectors, this includes constants with undefined elements. FIXME: is it worth generalizing this to simply take ICmpInst::Predicate? Reviewers: craig.topper, spatel, nikic Reviewed By: spatel Subscribers: llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D63811 llvm-svn: 364711
1 parent 9e9eb62 commit fe107fc

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed
 

‎llvm/include/llvm/IR/PatternMatch.h

+14
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,20 @@ inline cst_pred_ty<is_lowbit_mask> m_LowBitMask() {
418418
return cst_pred_ty<is_lowbit_mask>();
419419
}
420420

421+
struct is_unsigned_less_than {
422+
const APInt *Thr;
423+
bool isValue(const APInt &C) { return C.ult(*Thr); }
424+
};
425+
/// Match an integer or vector with every element unsigned less than the
426+
/// Threshold. For vectors, this includes constants with undefined elements.
427+
/// FIXME: is it worth generalizing this to simply take ICmpInst::Predicate?
428+
inline cst_pred_ty<is_unsigned_less_than>
429+
m_SpecificInt_ULT(const APInt &Threshold) {
430+
cst_pred_ty<is_unsigned_less_than> P;
431+
P.Thr = &Threshold;
432+
return P;
433+
}
434+
421435
struct is_nan {
422436
bool isValue(const APFloat &C) { return C.isNaN(); }
423437
};

‎llvm/unittests/IR/PatternMatch.cpp

+21
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,27 @@ TEST_F(PatternMatchTest, OneUse) {
6464
EXPECT_FALSE(m_OneUse(m_Value()).match(Leaf));
6565
}
6666

67+
TEST_F(PatternMatchTest, SpecificIntULT) {
68+
Type *IntTy = IRB.getInt32Ty();
69+
unsigned BitWidth = IntTy->getScalarSizeInBits();
70+
71+
Value *Zero = ConstantInt::get(IntTy, 0);
72+
Value *One = ConstantInt::get(IntTy, 1);
73+
Value *NegOne = ConstantInt::get(IntTy, -1);
74+
75+
EXPECT_FALSE(m_SpecificInt_ULT(APInt(BitWidth, 0)).match(Zero));
76+
EXPECT_FALSE(m_SpecificInt_ULT(APInt(BitWidth, 0)).match(One));
77+
EXPECT_FALSE(m_SpecificInt_ULT(APInt(BitWidth, 0)).match(NegOne));
78+
79+
EXPECT_TRUE(m_SpecificInt_ULT(APInt(BitWidth, 1)).match(Zero));
80+
EXPECT_FALSE(m_SpecificInt_ULT(APInt(BitWidth, 1)).match(One));
81+
EXPECT_FALSE(m_SpecificInt_ULT(APInt(BitWidth, 1)).match(NegOne));
82+
83+
EXPECT_TRUE(m_SpecificInt_ULT(APInt(BitWidth, -1)).match(Zero));
84+
EXPECT_TRUE(m_SpecificInt_ULT(APInt(BitWidth, -1)).match(One));
85+
EXPECT_FALSE(m_SpecificInt_ULT(APInt(BitWidth, -1)).match(NegOne));
86+
}
87+
6788
TEST_F(PatternMatchTest, CommutativeDeferredValue) {
6889
Value *X = IRB.getInt32(1);
6990
Value *Y = IRB.getInt32(2);

0 commit comments

Comments
 (0)
Please sign in to comment.