Skip to content

Commit 7a969ec

Browse files
authored
[PatternMatch] Use m_Not instead of m_c_Xor with m_AllOnes() (#96837)
1 parent 99251f5 commit 7a969ec

File tree

3 files changed

+21
-21
lines changed

3 files changed

+21
-21
lines changed

llvm/include/llvm/IR/PatternMatch.h

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2309,6 +2309,22 @@ m_UnordFMin(const LHS &L, const RHS &R) {
23092309
return MaxMin_match<FCmpInst, LHS, RHS, ufmin_pred_ty>(L, R);
23102310
}
23112311

2312+
/// Matches a 'Not' as 'xor V, -1' or 'xor -1, V'.
2313+
/// NOTE: we first match the 'Not' (by matching '-1'),
2314+
/// and only then match the inner matcher!
2315+
template <typename ValTy>
2316+
inline BinaryOp_match<cst_pred_ty<is_all_ones>, ValTy, Instruction::Xor, true>
2317+
m_Not(const ValTy &V) {
2318+
return m_c_Xor(m_AllOnes(), V);
2319+
}
2320+
2321+
template <typename ValTy>
2322+
inline BinaryOp_match<cst_pred_ty<is_all_ones, false>, ValTy, Instruction::Xor,
2323+
true>
2324+
m_NotForbidPoison(const ValTy &V) {
2325+
return m_c_Xor(m_AllOnesForbidPoison(), V);
2326+
}
2327+
23122328
//===----------------------------------------------------------------------===//
23132329
// Matchers for overflow check patterns: e.g. (a + b) u< a, (a ^ -1) <u b
23142330
// Note that S might be matched to other instructions than AddInst.
@@ -2343,13 +2359,13 @@ struct UAddWithOverflow_match {
23432359
return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpRHS);
23442360

23452361
Value *Op1;
2346-
auto XorExpr = m_OneUse(m_Xor(m_Value(Op1), m_AllOnes()));
2347-
// (a ^ -1) <u b
2362+
auto XorExpr = m_OneUse(m_Not(m_Value(Op1)));
2363+
// (~a) <u b
23482364
if (Pred == ICmpInst::ICMP_ULT) {
23492365
if (XorExpr.match(ICmpLHS))
23502366
return L.match(Op1) && R.match(ICmpRHS) && S.match(ICmpLHS);
23512367
}
2352-
// b > u (a ^ -1)
2368+
// b > u (~a)
23532369
if (Pred == ICmpInst::ICMP_UGT) {
23542370
if (XorExpr.match(ICmpRHS))
23552371
return L.match(Op1) && R.match(ICmpLHS) && S.match(ICmpRHS);
@@ -2659,22 +2675,6 @@ m_NSWNeg(const ValTy &V) {
26592675
return m_NSWSub(m_ZeroInt(), V);
26602676
}
26612677

2662-
/// Matches a 'Not' as 'xor V, -1' or 'xor -1, V'.
2663-
/// NOTE: we first match the 'Not' (by matching '-1'),
2664-
/// and only then match the inner matcher!
2665-
template <typename ValTy>
2666-
inline BinaryOp_match<cst_pred_ty<is_all_ones>, ValTy, Instruction::Xor, true>
2667-
m_Not(const ValTy &V) {
2668-
return m_c_Xor(m_AllOnes(), V);
2669-
}
2670-
2671-
template <typename ValTy>
2672-
inline BinaryOp_match<cst_pred_ty<is_all_ones, false>, ValTy, Instruction::Xor,
2673-
true>
2674-
m_NotForbidPoison(const ValTy &V) {
2675-
return m_c_Xor(m_AllOnesForbidPoison(), V);
2676-
}
2677-
26782678
/// Matches an SMin with LHS and RHS in either order.
26792679
template <typename LHS, typename RHS>
26802680
inline MaxMin_match<ICmpInst, LHS, RHS, smin_pred_ty, true>

llvm/lib/Target/X86/X86ISelLowering.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30557,7 +30557,7 @@ static std::pair<Value *, BitTestKind> FindSingleBitChange(Value *V) {
3055730557
bool Not = false;
3055830558
// Check if we have a NOT
3055930559
Value *PeekI;
30560-
if (match(I, m_c_Xor(m_Value(PeekI), m_AllOnes())) ||
30560+
if (match(I, m_Not(m_Value(PeekI))) ||
3056130561
match(I, m_Sub(m_AllOnes(), m_Value(PeekI)))) {
3056230562
Not = true;
3056330563
I = dyn_cast<Instruction>(PeekI);

llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ dropRedundantMaskingOfLeftShiftInput(BinaryOperator *OuterShift,
216216
// ((1 << MaskShAmt) - 1)
217217
auto MaskA = m_Add(m_Shl(m_One(), m_Value(MaskShAmt)), m_AllOnes());
218218
// (~(-1 << maskNbits))
219-
auto MaskB = m_Xor(m_Shl(m_AllOnes(), m_Value(MaskShAmt)), m_AllOnes());
219+
auto MaskB = m_Not(m_Shl(m_AllOnes(), m_Value(MaskShAmt)));
220220
// (-1 l>> MaskShAmt)
221221
auto MaskC = m_LShr(m_AllOnes(), m_Value(MaskShAmt));
222222
// ((-1 << MaskShAmt) l>> MaskShAmt)

0 commit comments

Comments
 (0)