[llvm] 97e921c - [PatternMatch] create and use matcher for 'not' that excludes undef elements
Sanjay Patel via llvm-commits
llvm-commits at lists.llvm.org
Thu Dec 2 05:51:18 PST 2021
Author: Sanjay Patel
Date: 2021-12-02T08:51:13-05:00
New Revision: 97e921c81fbfa75d6863cf08268373983193d420
URL: https://github.com/llvm/llvm-project/commit/97e921c81fbfa75d6863cf08268373983193d420
DIFF: https://github.com/llvm/llvm-project/commit/97e921c81fbfa75d6863cf08268373983193d420.diff
LOG: [PatternMatch] create and use matcher for 'not' that excludes undef elements
We needed a stricter version of m_Not for D114462, but I wasn't
sure if that was going to be required anywhere else, so I didn't bother
to make that reusable.
It turns out we have one more existing simplification that needs
this (currently miscompiles):
https://alive2.llvm.org/ce/z/9-nTKi
And there's at least one more fold in that family that we could add.
Differential Revision: https://reviews.llvm.org/D114882
Added:
Modified:
llvm/include/llvm/IR/PatternMatch.h
llvm/lib/Analysis/InstructionSimplify.cpp
llvm/unittests/IR/PatternMatch.cpp
Removed:
################################################################################
diff --git a/llvm/include/llvm/IR/PatternMatch.h b/llvm/include/llvm/IR/PatternMatch.h
index b858733530e3d..320deb80bb1f8 100644
--- a/llvm/include/llvm/IR/PatternMatch.h
+++ b/llvm/include/llvm/IR/PatternMatch.h
@@ -2285,6 +2285,31 @@ m_Not(const ValTy &V) {
return m_c_Xor(V, m_AllOnes());
}
+template <typename ValTy> struct NotForbidUndef_match {
+ ValTy Val;
+ NotForbidUndef_match(const ValTy &V) : Val(V) {}
+
+ template <typename OpTy> bool match(OpTy *V) {
+ // We do not use m_c_Xor because that could match an arbitrary APInt that is
+ // not -1 as C and then fail to match the other operand if it is -1.
+ // This code should still work even when both operands are constants.
+ Value *X;
+ const APInt *C;
+ if (m_Xor(m_Value(X), m_APIntForbidUndef(C)).match(V) && C->isAllOnes())
+ return Val.match(X);
+ if (m_Xor(m_APIntForbidUndef(C), m_Value(X)).match(V) && C->isAllOnes())
+ return Val.match(X);
+ return false;
+ }
+};
+
+/// Matches a bitwise 'not' as 'xor V, -1' or 'xor -1, V'. For vectors, the
+/// constant value must be composed of only -1 scalar elements.
+template <typename ValTy>
+inline NotForbidUndef_match<ValTy> m_NotForbidUndef(const ValTy &V) {
+ return NotForbidUndef_match<ValTy>(V);
+}
+
/// Matches an SMin with LHS and RHS in either order.
template <typename LHS, typename RHS>
inline MaxMin_match<ICmpInst, LHS, RHS, smin_pred_ty, true>
diff --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp
index 9eb1ccd823de4..22d2ce11cc909 100644
--- a/llvm/lib/Analysis/InstructionSimplify.cpp
+++ b/llvm/lib/Analysis/InstructionSimplify.cpp
@@ -2408,11 +2408,10 @@ static Value *SimplifyXorInst(Value *Op0, Value *Op1, const SimplifyQuery &Q,
// The 'not' op must contain a complete -1 operand (no undef elements for
// vector) for the transform to be safe.
Value *NotA;
- const APInt *C;
- if (match(X, m_c_Or(m_CombineAnd(m_Xor(m_Value(A), m_APIntForbidUndef(C)),
- m_Value(NotA)),
- m_Value(B))) &&
- match(Y, m_c_And(m_Specific(A), m_Specific(B))) && C->isAllOnes())
+ if (match(X,
+ m_c_Or(m_CombineAnd(m_NotForbidUndef(m_Value(A)), m_Value(NotA)),
+ m_Value(B))) &&
+ match(Y, m_c_And(m_Specific(A), m_Specific(B))))
return NotA;
return nullptr;
diff --git a/llvm/unittests/IR/PatternMatch.cpp b/llvm/unittests/IR/PatternMatch.cpp
index 598dcdff943f8..58d364f11836d 100644
--- a/llvm/unittests/IR/PatternMatch.cpp
+++ b/llvm/unittests/IR/PatternMatch.cpp
@@ -1727,6 +1727,41 @@ TEST_F(PatternMatchTest, VScale) {
EXPECT_FALSE(match(PtrToInt2, m_VScale(DL)));
}
+TEST_F(PatternMatchTest, NotForbidUndef) {
+ Type *ScalarTy = IRB.getInt8Ty();
+ Type *VectorTy = FixedVectorType::get(ScalarTy, 3);
+ Constant *ScalarUndef = UndefValue::get(ScalarTy);
+ Constant *ScalarOnes = Constant::getAllOnesValue(ScalarTy);
+ Constant *VectorZero = Constant::getNullValue(VectorTy);
+ Constant *VectorOnes = Constant::getAllOnesValue(VectorTy);
+
+ SmallVector<Constant *, 3> MixedElems;
+ MixedElems.push_back(ScalarOnes);
+ MixedElems.push_back(ScalarOnes);
+ MixedElems.push_back(ScalarUndef);
+ Constant *VectorMixed = ConstantVector::get(MixedElems);
+
+ Value *Not = IRB.CreateXor(VectorZero, VectorOnes);
+ Value *X;
+ EXPECT_TRUE(match(Not, m_Not(m_Value())));
+ EXPECT_TRUE(match(Not, m_NotForbidUndef(m_Value(X))));
+ EXPECT_TRUE(match(X, m_Zero()));
+
+ Value *NotCommute = IRB.CreateXor(VectorOnes, VectorZero);
+ Value *Y;
+ EXPECT_TRUE(match(NotCommute, m_Not(m_Value())));
+ EXPECT_TRUE(match(NotCommute, m_NotForbidUndef(m_Value(Y))));
+ EXPECT_TRUE(match(Y, m_Zero()));
+
+ Value *NotWithUndefs = IRB.CreateXor(VectorZero, VectorMixed);
+ EXPECT_TRUE(match(NotWithUndefs, m_Not(m_Value())));
+ EXPECT_FALSE(match(NotWithUndefs, m_NotForbidUndef(m_Value())));
+
+ Value *NotWithUndefsCommute = IRB.CreateXor(VectorMixed, VectorZero);
+ EXPECT_TRUE(match(NotWithUndefsCommute, m_Not(m_Value())));
+ EXPECT_FALSE(match(NotWithUndefsCommute, m_NotForbidUndef(m_Value(X))));
+}
+
template <typename T> struct MutableConstTest : PatternMatchTest { };
typedef ::testing::Types<std::tuple<Value*, Instruction*>,
More information about the llvm-commits
mailing list