Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4750,8 +4750,22 @@ Instruction *InstCombinerImpl::foldNot(BinaryOperator &I) {
BinaryOperator *NotVal;
if (match(NotOp, m_BinOp(NotVal))) {
// ~((-X) | Y) --> (X - 1) & (~Y)
if (match(NotVal,
m_OneUse(m_c_Or(m_OneUse(m_NSWNeg(m_Value(X))), m_Value(Y))))) {
// If -X is nsw, then X - 1 must be too.
// This is because -X can only be nsw if X is any number that is not
// INT_MIN. And if X is not INT_MIN, then X - 1 must be nsw.
Value *DecX = Builder.CreateAdd(X, ConstantInt::getAllOnesValue(Ty), "",
/*HasNUW=*/false, /*HasNSW=*/true);
Value *NotY = Builder.CreateNot(Y);
return BinaryOperator::CreateAnd(DecX, NotY);
}

if (match(NotVal,
m_OneUse(m_c_Or(m_OneUse(m_Neg(m_Value(X))), m_Value(Y))))) {
// If -X is nsw, then X - 1 must be too.
// This is because -X can only be nsw if X is any number that is not
// INT_MIN. And if X is not INT_MIN, then X - 1 must be nsw.
Value *DecX = Builder.CreateAdd(X, ConstantInt::getAllOnesValue(Ty));
Value *NotY = Builder.CreateNot(Y);
return BinaryOperator::CreateAnd(DecX, NotY);
Expand Down
28 changes: 28 additions & 0 deletions llvm/test/Transforms/InstCombine/not.ll
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,34 @@ define <3 x i5> @not_or_neg_commute_vec(<3 x i5> %x, <3 x i5> %p) {
ret <3 x i5> %not
}

define i8 @not_or_neg_nsw(i8 %x, i8 %y) {
; CHECK-LABEL: @not_or_neg_nsw(
; CHECK-NEXT: [[TMP1:%.*]] = add nsw i8 [[Y:%.*]], -1
; CHECK-NEXT: [[TMP2:%.*]] = xor i8 [[X:%.*]], -1
; CHECK-NEXT: [[NOT:%.*]] = and i8 [[TMP1]], [[TMP2]]
; CHECK-NEXT: ret i8 [[NOT]]
;
%s = sub nsw i8 0, %y
%o = or i8 %s, %x
%not = xor i8 %o, -1
ret i8 %not
}

define <3 x i5> @not_or_neg_commute_vec_nsw(<3 x i5> %x, <3 x i5> %p) {
; CHECK-LABEL: @not_or_neg_commute_vec_nsw(
; CHECK-NEXT: [[Y:%.*]] = mul <3 x i5> [[P:%.*]], <i5 1, i5 2, i5 3>
; CHECK-NEXT: [[TMP1:%.*]] = add nsw <3 x i5> [[X:%.*]], splat (i5 -1)
; CHECK-NEXT: [[TMP2:%.*]] = xor <3 x i5> [[Y]], splat (i5 -1)
; CHECK-NEXT: [[NOT:%.*]] = and <3 x i5> [[TMP1]], [[TMP2]]
; CHECK-NEXT: ret <3 x i5> [[NOT]]
;
%y = mul <3 x i5> %p, <i5 1, i5 2, i5 3> ; thwart complexity-based-canonicalization
%s = sub nsw <3 x i5> <i5 0, i5 0, i5 poison>, %x
%o = or <3 x i5> %y, %s
%not = xor <3 x i5> %o, <i5 -1, i5 poison, i5 -1>
ret <3 x i5> %not
}

; negative test

define i8 @not_or_neg_use1(i8 %x, i8 %y) {
Expand Down
Loading