Skip to content

Commit

Permalink
[PatternMatch] define m_Not using m_Xor and cst_pred_ty
Browse files Browse the repository at this point in the history
Using cst_pred_ty in the definition allows us to match vectors with undef elements.

This is a continuation of an effort to make all pattern matchers allow undef elements in vectors:
rL325437
rL325466
D43792

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

llvm-svn: 326823
  • Loading branch information
rotateright committed Mar 6, 2018
1 parent 846e578 commit ed2211d
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 38 deletions.
32 changes: 8 additions & 24 deletions llvm/include/llvm/IR/PatternMatch.h
Expand Up @@ -1036,34 +1036,11 @@ template <typename Op_t> struct LoadClass_match {
template <typename OpTy> inline LoadClass_match<OpTy> m_Load(const OpTy &Op) {
return LoadClass_match<OpTy>(Op);
}

//===----------------------------------------------------------------------===//
// Matchers for unary operators
//

template <typename LHS_t> struct not_match {
LHS_t L;

not_match(const LHS_t &LHS) : L(LHS) {}

template <typename OpTy> bool match(OpTy *V) {
if (auto *O = dyn_cast<Operator>(V))
if (O->getOpcode() == Instruction::Xor) {
if (isAllOnes(O->getOperand(1)))
return L.match(O->getOperand(0));
if (isAllOnes(O->getOperand(0)))
return L.match(O->getOperand(1));
}
return false;
}

private:
bool isAllOnes(Value *V) {
return isa<Constant>(V) && cast<Constant>(V)->isAllOnesValue();
}
};

template <typename LHS> inline not_match<LHS> m_Not(const LHS &L) { return L; }

template <typename LHS_t> struct neg_match {
LHS_t L;

Expand Down Expand Up @@ -1590,6 +1567,13 @@ inline BinaryOp_match<LHS, RHS, Instruction::Xor, true> m_c_Xor(const LHS &L,
return BinaryOp_match<LHS, RHS, Instruction::Xor, true>(L, R);
}

/// Matches a 'Not' as 'xor V, -1' or 'xor -1, V'.
template <typename ValTy>
inline BinaryOp_match<ValTy, cst_pred_ty<is_all_ones>, Instruction::Xor, true>
m_Not(const ValTy &V) {
return m_c_Xor(V, m_AllOnes());
}

/// 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>
Expand Down
5 changes: 1 addition & 4 deletions llvm/test/Transforms/InstCombine/sub.ll
Expand Up @@ -73,12 +73,9 @@ define <2 x i8> @notnotsub_vec(<2 x i8> %x, <2 x i8> %y) {
ret <2 x i8> %sub
}

; FIXME:
define <2 x i8> @notnotsub_vec_undef_elts(<2 x i8> %x, <2 x i8> %y) {
; CHECK-LABEL: @notnotsub_vec_undef_elts(
; CHECK-NEXT: [[NX:%.*]] = xor <2 x i8> [[X:%.*]], <i8 undef, i8 -1>
; CHECK-NEXT: [[NY:%.*]] = xor <2 x i8> [[Y:%.*]], <i8 -1, i8 undef>
; CHECK-NEXT: [[SUB:%.*]] = sub <2 x i8> [[NX]], [[NY]]
; CHECK-NEXT: [[SUB:%.*]] = sub <2 x i8> [[Y:%.*]], [[X:%.*]]
; CHECK-NEXT: ret <2 x i8> [[SUB]]
;
%nx = xor <2 x i8> %x, <i8 undef, i8 -1>
Expand Down
16 changes: 6 additions & 10 deletions llvm/test/Transforms/InstCombine/vector-xor.ll
Expand Up @@ -107,10 +107,8 @@ define <4 x i32> @test_v4i32_not_ashr_not(<4 x i32> %x, <4 x i32> %y) {

define <4 x i32> @test_v4i32_not_ashr_not_undef(<4 x i32> %x, <4 x i32> %y) {
; CHECK-LABEL: @test_v4i32_not_ashr_not_undef(
; CHECK-NEXT: [[TMP1:%.*]] = xor <4 x i32> [[X:%.*]], <i32 -1, i32 -1, i32 -1, i32 undef>
; CHECK-NEXT: [[TMP2:%.*]] = ashr <4 x i32> [[TMP1]], [[Y:%.*]]
; CHECK-NEXT: [[TMP3:%.*]] = xor <4 x i32> [[TMP2]], <i32 -1, i32 -1, i32 undef, i32 -1>
; CHECK-NEXT: ret <4 x i32> [[TMP3]]
; CHECK-NEXT: [[TMP1:%.*]] = ashr <4 x i32> [[X:%.*]], [[Y:%.*]]
; CHECK-NEXT: ret <4 x i32> [[TMP1]]
;
%1 = xor <4 x i32> <i32 -1, i32 -1, i32 -1, i32 undef>, %x
%2 = ashr <4 x i32> %1, %y
Expand Down Expand Up @@ -142,9 +140,8 @@ define <4 x i32> @test_v4i32_not_ashr_negative_const(<4 x i32> %a0) {

define <4 x i32> @test_v4i32_not_ashr_negative_const_undef(<4 x i32> %a0) {
; CHECK-LABEL: @test_v4i32_not_ashr_negative_const_undef(
; CHECK-NEXT: [[TMP1:%.*]] = ashr <4 x i32> <i32 -3, i32 -5, i32 undef, i32 -9>, [[A0:%.*]]
; CHECK-NEXT: [[TMP2:%.*]] = xor <4 x i32> [[TMP1]], <i32 -1, i32 -1, i32 -1, i32 undef>
; CHECK-NEXT: ret <4 x i32> [[TMP2]]
; CHECK-NEXT: [[TMP1:%.*]] = lshr <4 x i32> <i32 2, i32 4, i32 undef, i32 8>, [[A0:%.*]]
; CHECK-NEXT: ret <4 x i32> [[TMP1]]
;
%1 = ashr <4 x i32> <i32 -3, i32 -5, i32 undef, i32 -9>, %a0
%2 = xor <4 x i32> <i32 -1, i32 -1, i32 -1, i32 undef>, %1
Expand Down Expand Up @@ -175,9 +172,8 @@ define <4 x i32> @test_v4i32_not_lshr_nonnegative_const(<4 x i32> %a0) {

define <4 x i32> @test_v4i32_not_lshr_nonnegative_const_undef(<4 x i32> %a0) {
; CHECK-LABEL: @test_v4i32_not_lshr_nonnegative_const_undef(
; CHECK-NEXT: [[TMP1:%.*]] = lshr <4 x i32> <i32 3, i32 5, i32 undef, i32 9>, [[A0:%.*]]
; CHECK-NEXT: [[TMP2:%.*]] = xor <4 x i32> [[TMP1]], <i32 -1, i32 -1, i32 -1, i32 undef>
; CHECK-NEXT: ret <4 x i32> [[TMP2]]
; CHECK-NEXT: [[TMP1:%.*]] = ashr <4 x i32> <i32 -4, i32 -6, i32 undef, i32 -10>, [[A0:%.*]]
; CHECK-NEXT: ret <4 x i32> [[TMP1]]
;
%1 = lshr <4 x i32> <i32 3, i32 5, i32 undef, i32 9>, %a0
%2 = xor <4 x i32> <i32 -1, i32 -1, i32 -1, i32 undef>, %1
Expand Down

0 comments on commit ed2211d

Please sign in to comment.