Skip to content

[InstCombine] Check isGuaranteedNotToBeUndef in haveNoCommonBitsSetSp… #74390

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 5, 2023
Merged
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
26 changes: 17 additions & 9 deletions llvm/lib/Analysis/ValueTracking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,37 +186,45 @@ KnownBits llvm::computeKnownBits(const Value *V, const APInt &DemandedElts,
SimplifyQuery(DL, DT, AC, safeCxtI(V, CxtI), UseInstrInfo));
}

static bool haveNoCommonBitsSetSpecialCases(const Value *LHS,
const Value *RHS) {
static bool haveNoCommonBitsSetSpecialCases(const Value *LHS, const Value *RHS,
const SimplifyQuery &SQ) {
// Look for an inverted mask: (X & ~M) op (Y & M).
{
Value *M;
if (match(LHS, m_c_And(m_Not(m_Value(M)), m_Value())) &&
match(RHS, m_c_And(m_Specific(M), m_Value())))
match(RHS, m_c_And(m_Specific(M), m_Value())) &&
isGuaranteedNotToBeUndef(M, SQ.AC, SQ.CxtI, SQ.DT))
return true;
}

// X op (Y & ~X)
if (match(RHS, m_c_And(m_Not(m_Specific(LHS)), m_Value())))
if (match(RHS, m_c_And(m_Not(m_Specific(LHS)), m_Value())) &&
isGuaranteedNotToBeUndef(LHS, SQ.AC, SQ.CxtI, SQ.DT))
return true;

// X op ((X & Y) ^ Y) -- this is the canonical form of the previous pattern
// for constant Y.
Value *Y;
if (match(RHS, m_c_Xor(m_c_And(m_Specific(LHS), m_Value(Y)), m_Deferred(Y))))
if (match(RHS,
m_c_Xor(m_c_And(m_Specific(LHS), m_Value(Y)), m_Deferred(Y))) &&
isGuaranteedNotToBeUndef(LHS, SQ.AC, SQ.CxtI, SQ.DT) &&
isGuaranteedNotToBeUndef(Y, SQ.AC, SQ.CxtI, SQ.DT))
return true;

// Peek through extends to find a 'not' of the other side:
// (ext Y) op ext(~Y)
if (match(LHS, m_ZExtOrSExt(m_Value(Y))) &&
match(RHS, m_ZExtOrSExt(m_Not(m_Specific(Y)))))
match(RHS, m_ZExtOrSExt(m_Not(m_Specific(Y)))) &&
isGuaranteedNotToBeUndef(Y, SQ.AC, SQ.CxtI, SQ.DT))
return true;

// Look for: (A & B) op ~(A | B)
{
Value *A, *B;
if (match(LHS, m_And(m_Value(A), m_Value(B))) &&
match(RHS, m_Not(m_c_Or(m_Specific(A), m_Specific(B)))))
match(RHS, m_Not(m_c_Or(m_Specific(A), m_Specific(B)))) &&
isGuaranteedNotToBeUndef(A, SQ.AC, SQ.CxtI, SQ.DT) &&
isGuaranteedNotToBeUndef(B, SQ.AC, SQ.CxtI, SQ.DT))
return true;
}

Expand All @@ -234,8 +242,8 @@ bool llvm::haveNoCommonBitsSet(const WithCache<const Value *> &LHSCache,
assert(LHS->getType()->isIntOrIntVectorTy() &&
"LHS and RHS should be integers");

if (haveNoCommonBitsSetSpecialCases(LHS, RHS) ||
haveNoCommonBitsSetSpecialCases(RHS, LHS))
if (haveNoCommonBitsSetSpecialCases(LHS, RHS, SQ) ||
haveNoCommonBitsSetSpecialCases(RHS, LHS, SQ))
return true;

return KnownBits::haveNoCommonBitsSet(LHSCache.getKnownBits(SQ),
Expand Down
46 changes: 23 additions & 23 deletions llvm/test/Transforms/InstCombine/add.ll
Original file line number Diff line number Diff line change
Expand Up @@ -1521,7 +1521,7 @@ define i8 @add_like_or_disjoint(i8 %x) {
ret i8 %r
}

define i8 @add_and_xor(i8 %x, i8 %y) {
define i8 @add_and_xor(i8 noundef %x, i8 %y) {
; CHECK-LABEL: @add_and_xor(
; CHECK-NEXT: [[ADD:%.*]] = or i8 [[Y:%.*]], [[X:%.*]]
; CHECK-NEXT: ret i8 [[ADD]]
Expand Down Expand Up @@ -1558,7 +1558,7 @@ define i8 @add_and_xor_wrong_op(i8 %x, i8 %y, i8 %z) {
ret i8 %add
}

define i8 @add_and_xor_commuted1(i8 %x, i8 %_y) {
define i8 @add_and_xor_commuted1(i8 noundef %x, i8 %_y) {
; CHECK-LABEL: @add_and_xor_commuted1(
; CHECK-NEXT: [[Y:%.*]] = udiv i8 42, [[_Y:%.*]]
; CHECK-NEXT: [[ADD:%.*]] = or i8 [[Y]], [[X:%.*]]
Expand All @@ -1571,7 +1571,7 @@ define i8 @add_and_xor_commuted1(i8 %x, i8 %_y) {
ret i8 %add
}

define i8 @add_and_xor_commuted2(i8 %_x, i8 %y) {
define i8 @add_and_xor_commuted2(i8 noundef %_x, i8 %y) {
; CHECK-LABEL: @add_and_xor_commuted2(
; CHECK-NEXT: [[X:%.*]] = udiv i8 42, [[_X:%.*]]
; CHECK-NEXT: [[ADD:%.*]] = or i8 [[X]], [[Y:%.*]]
Expand All @@ -1584,7 +1584,7 @@ define i8 @add_and_xor_commuted2(i8 %_x, i8 %y) {
ret i8 %add
}

define i8 @add_and_xor_commuted3(i8 %_x, i8 %_y) {
define i8 @add_and_xor_commuted3(i8 noundef %_x, i8 %_y) {
; CHECK-LABEL: @add_and_xor_commuted3(
; CHECK-NEXT: [[X:%.*]] = udiv i8 42, [[_X:%.*]]
; CHECK-NEXT: [[Y:%.*]] = udiv i8 42, [[_Y:%.*]]
Expand All @@ -1599,7 +1599,7 @@ define i8 @add_and_xor_commuted3(i8 %_x, i8 %_y) {
ret i8 %add
}

define i8 @add_and_xor_extra_use(i8 %x, i8 %y) {
define i8 @add_and_xor_extra_use(i8 noundef %x, i8 %y) {
; CHECK-LABEL: @add_and_xor_extra_use(
; CHECK-NEXT: [[XOR:%.*]] = xor i8 [[X:%.*]], -1
; CHECK-NEXT: call void @use(i8 [[XOR]])
Expand All @@ -1616,7 +1616,7 @@ define i8 @add_and_xor_extra_use(i8 %x, i8 %y) {
ret i8 %add
}

define i8 @add_xor_and_const(i8 %x) {
define i8 @add_xor_and_const(i8 noundef %x) {
; CHECK-LABEL: @add_xor_and_const(
; CHECK-NEXT: [[ADD:%.*]] = or i8 [[X:%.*]], 42
; CHECK-NEXT: ret i8 [[ADD]]
Expand All @@ -1640,7 +1640,7 @@ define i8 @add_xor_and_const_wrong_const(i8 %x) {
ret i8 %add
}

define i8 @add_xor_and_var(i8 %x, i8 %y) {
define i8 @add_xor_and_var(i8 noundef %x, i8 noundef %y) {
; CHECK-LABEL: @add_xor_and_var(
; CHECK-NEXT: [[AND:%.*]] = and i8 [[X:%.*]], [[Y:%.*]]
; CHECK-NEXT: call void @use(i8 [[AND]])
Expand Down Expand Up @@ -1684,7 +1684,7 @@ define i8 @add_xor_and_var_wrong_op2(i8 %x, i8 %y, i8 %z) {
ret i8 %add
}

define i8 @add_xor_and_var_commuted1(i8 %x, i8 %y) {
define i8 @add_xor_and_var_commuted1(i8 noundef %x, i8 noundef %y) {
; CHECK-LABEL: @add_xor_and_var_commuted1(
; CHECK-NEXT: [[AND:%.*]] = and i8 [[Y:%.*]], [[X:%.*]]
; CHECK-NEXT: call void @use(i8 [[AND]])
Expand All @@ -1698,7 +1698,7 @@ define i8 @add_xor_and_var_commuted1(i8 %x, i8 %y) {
ret i8 %add
}

define i8 @add_xor_and_var_commuted2(i8 %_x, i8 %_y) {
define i8 @add_xor_and_var_commuted2(i8 noundef %_x, i8 noundef %_y) {
; CHECK-LABEL: @add_xor_and_var_commuted2(
; CHECK-NEXT: [[X:%.*]] = udiv i8 42, [[_X:%.*]]
; CHECK-NEXT: [[Y:%.*]] = udiv i8 42, [[_Y:%.*]]
Expand All @@ -1716,7 +1716,7 @@ define i8 @add_xor_and_var_commuted2(i8 %_x, i8 %_y) {
ret i8 %add
}

define i8 @add_xor_and_var_commuted3(i8 %x, i8 %_y) {
define i8 @add_xor_and_var_commuted3(i8 noundef %x, i8 noundef %_y) {
; CHECK-LABEL: @add_xor_and_var_commuted3(
; CHECK-NEXT: [[Y:%.*]] = udiv i8 42, [[_Y:%.*]]
; CHECK-NEXT: [[AND:%.*]] = and i8 [[Y]], [[X:%.*]]
Expand All @@ -1732,7 +1732,7 @@ define i8 @add_xor_and_var_commuted3(i8 %x, i8 %_y) {
ret i8 %add
}

define i8 @add_xor_and_var_commuted4(i8 %_x, i8 %y) {
define i8 @add_xor_and_var_commuted4(i8 noundef %_x, i8 noundef %y) {
; CHECK-LABEL: @add_xor_and_var_commuted4(
; CHECK-NEXT: [[X:%.*]] = udiv i8 42, [[_X:%.*]]
; CHECK-NEXT: [[AND:%.*]] = and i8 [[X]], [[Y:%.*]]
Expand All @@ -1748,7 +1748,7 @@ define i8 @add_xor_and_var_commuted4(i8 %_x, i8 %y) {
ret i8 %add
}

define i8 @add_xor_and_var_commuted5(i8 %_x, i8 %_y) {
define i8 @add_xor_and_var_commuted5(i8 noundef %_x, i8 noundef %_y) {
; CHECK-LABEL: @add_xor_and_var_commuted5(
; CHECK-NEXT: [[X:%.*]] = udiv i8 42, [[_X:%.*]]
; CHECK-NEXT: [[Y:%.*]] = udiv i8 42, [[_Y:%.*]]
Expand All @@ -1766,7 +1766,7 @@ define i8 @add_xor_and_var_commuted5(i8 %_x, i8 %_y) {
ret i8 %add
}

define i8 @add_xor_and_var_commuted6(i8 %_x, i8 %_y) {
define i8 @add_xor_and_var_commuted6(i8 noundef %_x, i8 noundef %_y) {
; CHECK-LABEL: @add_xor_and_var_commuted6(
; CHECK-NEXT: [[X:%.*]] = udiv i8 42, [[_X:%.*]]
; CHECK-NEXT: [[Y:%.*]] = udiv i8 42, [[_Y:%.*]]
Expand All @@ -1784,7 +1784,7 @@ define i8 @add_xor_and_var_commuted6(i8 %_x, i8 %_y) {
ret i8 %add
}

define i8 @add_xor_and_var_commuted7(i8 %_x, i8 %_y) {
define i8 @add_xor_and_var_commuted7(i8 noundef %_x, i8 noundef %_y) {
; CHECK-LABEL: @add_xor_and_var_commuted7(
; CHECK-NEXT: [[X:%.*]] = udiv i8 42, [[_X:%.*]]
; CHECK-NEXT: [[Y:%.*]] = udiv i8 42, [[_Y:%.*]]
Expand All @@ -1802,7 +1802,7 @@ define i8 @add_xor_and_var_commuted7(i8 %_x, i8 %_y) {
ret i8 %add
}

define i8 @add_xor_and_var_extra_use(i8 %x, i8 %y) {
define i8 @add_xor_and_var_extra_use(i8 noundef %x, i8 noundef %y) {
; CHECK-LABEL: @add_xor_and_var_extra_use(
; CHECK-NEXT: [[AND:%.*]] = and i8 [[X:%.*]], [[Y:%.*]]
; CHECK-NEXT: call void @use(i8 [[AND]])
Expand Down Expand Up @@ -2583,7 +2583,7 @@ define <vscale x 1 x i32> @add_to_or_scalable(<vscale x 1 x i32> %in) {
ret <vscale x 1 x i32> %add
}

define i5 @zext_zext_not(i3 %x) {
define i5 @zext_zext_not(i3 noundef %x) {
; CHECK-LABEL: @zext_zext_not(
; CHECK-NEXT: ret i5 7
;
Expand All @@ -2594,7 +2594,7 @@ define i5 @zext_zext_not(i3 %x) {
ret i5 %r
}

define <2 x i5> @zext_zext_not_commute(<2 x i3> %x) {
define <2 x i5> @zext_zext_not_commute(<2 x i3> noundef %x) {
; CHECK-LABEL: @zext_zext_not_commute(
; CHECK-NEXT: ret <2 x i5> <i5 7, i5 7>
;
Expand All @@ -2605,7 +2605,7 @@ define <2 x i5> @zext_zext_not_commute(<2 x i3> %x) {
ret <2 x i5> %r
}

define i9 @sext_sext_not(i3 %x) {
define i9 @sext_sext_not(i3 noundef %x) {
; CHECK-LABEL: @sext_sext_not(
; CHECK-NEXT: ret i9 -1
;
Expand All @@ -2616,7 +2616,7 @@ define i9 @sext_sext_not(i3 %x) {
ret i9 %r
}

define i8 @sext_sext_not_commute(i3 %x) {
define i8 @sext_sext_not_commute(i3 noundef %x) {
; CHECK-LABEL: @sext_sext_not_commute(
; CHECK-NEXT: [[SX:%.*]] = sext i3 [[X:%.*]] to i8
; CHECK-NEXT: call void @use(i8 [[SX]])
Expand All @@ -2631,7 +2631,7 @@ define i8 @sext_sext_not_commute(i3 %x) {
ret i8 %r
}

define i5 @zext_sext_not(i4 %x) {
define i5 @zext_sext_not(i4 noundef %x) {
; CHECK-LABEL: @zext_sext_not(
; CHECK-NEXT: [[ZX:%.*]] = zext i4 [[X:%.*]] to i5
; CHECK-NEXT: [[NOTX:%.*]] = xor i4 [[X]], -1
Expand All @@ -2646,7 +2646,7 @@ define i5 @zext_sext_not(i4 %x) {
ret i5 %r
}

define i8 @zext_sext_not_commute(i4 %x) {
define i8 @zext_sext_not_commute(i4 noundef %x) {
; CHECK-LABEL: @zext_sext_not_commute(
; CHECK-NEXT: [[ZX:%.*]] = zext i4 [[X:%.*]] to i8
; CHECK-NEXT: call void @use(i8 [[ZX]])
Expand All @@ -2665,7 +2665,7 @@ define i8 @zext_sext_not_commute(i4 %x) {
ret i8 %r
}

define i9 @sext_zext_not(i4 %x) {
define i9 @sext_zext_not(i4 noundef %x) {
; CHECK-LABEL: @sext_zext_not(
; CHECK-NEXT: [[SX:%.*]] = sext i4 [[X:%.*]] to i9
; CHECK-NEXT: [[NOTX:%.*]] = xor i4 [[X]], -1
Expand All @@ -2680,7 +2680,7 @@ define i9 @sext_zext_not(i4 %x) {
ret i9 %r
}

define i9 @sext_zext_not_commute(i4 %x) {
define i9 @sext_zext_not_commute(i4 noundef %x) {
; CHECK-LABEL: @sext_zext_not_commute(
; CHECK-NEXT: [[SX:%.*]] = sext i4 [[X:%.*]] to i9
; CHECK-NEXT: [[NOTX:%.*]] = xor i4 [[X]], -1
Expand Down
2 changes: 1 addition & 1 deletion llvm/test/Transforms/InstCombine/and-or-not.ll
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,7 @@ define i32 @and_to_nxor_multiuse(float %fa, float %fb) {

; (a & b) | ~(a | b) --> ~(a ^ b)
; TODO: this increases instruction count if the pieces have additional users
define i32 @or_to_nxor_multiuse(i32 %a, i32 %b) {
define i32 @or_to_nxor_multiuse(i32 noundef %a, i32 noundef %b) {
; CHECK-LABEL: @or_to_nxor_multiuse(
; CHECK-NEXT: [[AND:%.*]] = and i32 [[A:%.*]], [[B:%.*]]
; CHECK-NEXT: [[OR:%.*]] = or i32 [[A]], [[B]]
Expand Down
6 changes: 3 additions & 3 deletions llvm/test/Transforms/InstCombine/logical-select.ll
Original file line number Diff line number Diff line change
Expand Up @@ -762,7 +762,7 @@ define <vscale x 2 x i64> @bitcast_vec_cond_scalable(<vscale x 16 x i1> %cond, <

; Negative test - bitcast of condition from wide source element type cannot be converted to select.

define <8 x i3> @bitcast_vec_cond_commute1(<3 x i1> %cond, <8 x i3> %pc, <8 x i3> %d) {
define <8 x i3> @bitcast_vec_cond_commute1(<3 x i1> noundef %cond, <8 x i3> %pc, <8 x i3> %d) {
; CHECK-LABEL: @bitcast_vec_cond_commute1(
; CHECK-NEXT: [[C:%.*]] = mul <8 x i3> [[PC:%.*]], [[PC]]
; CHECK-NEXT: [[S:%.*]] = sext <3 x i1> [[COND:%.*]] to <3 x i8>
Expand Down Expand Up @@ -830,7 +830,7 @@ define <2 x i16> @bitcast_vec_cond_commute3(<4 x i8> %cond, <2 x i16> %pc, <2 x

; Don't crash on invalid type for compute signbits.

define <2 x i64> @bitcast_fp_vec_cond(<2 x double> %s, <2 x i64> %c, <2 x i64> %d) {
define <2 x i64> @bitcast_fp_vec_cond(<2 x double> noundef %s, <2 x i64> %c, <2 x i64> %d) {
; CHECK-LABEL: @bitcast_fp_vec_cond(
; CHECK-NEXT: [[T9:%.*]] = bitcast <2 x double> [[S:%.*]] to <2 x i64>
; CHECK-NEXT: [[NOTT9:%.*]] = xor <2 x i64> [[T9]], <i64 -1, i64 -1>
Expand All @@ -849,7 +849,7 @@ define <2 x i64> @bitcast_fp_vec_cond(<2 x double> %s, <2 x i64> %c, <2 x i64> %

; Wider source type would be ok except poison could leak across elements.

define <2 x i64> @bitcast_int_vec_cond(i1 %b, <2 x i64> %c, <2 x i64> %d) {
define <2 x i64> @bitcast_int_vec_cond(i1 noundef %b, <2 x i64> %c, <2 x i64> %d) {
; CHECK-LABEL: @bitcast_int_vec_cond(
; CHECK-NEXT: [[S:%.*]] = sext i1 [[B:%.*]] to i128
; CHECK-NEXT: [[T9:%.*]] = bitcast i128 [[S]] to <2 x i64>
Expand Down
Loading