Index: llvm/lib/Analysis/ValueTracking.cpp =================================================================== --- llvm/lib/Analysis/ValueTracking.cpp +++ llvm/lib/Analysis/ValueTracking.cpp @@ -275,13 +275,25 @@ assert(LHS->getType()->isIntOrIntVectorTy() && "LHS and RHS should be integers"); // 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()))) - return true; - if (match(RHS, m_c_And(m_Not(m_Value(M)), m_Value())) && - match(LHS, m_c_And(m_Specific(M), m_Value()))) - return true; + { + 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()))) + return true; + if (match(RHS, m_c_And(m_Not(m_Value(M)), m_Value())) && + match(LHS, m_c_And(m_Specific(M), m_Value()))) + 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))))) + return true; + if (match(RHS, m_And(m_Value(A), m_Value(B))) && + match(LHS, m_Not(m_c_Or(m_Specific(A), m_Specific(B))))) + return true; + } IntegerType *IT = cast(LHS->getType()->getScalarType()); KnownBits LHSKnown(IT->getBitWidth()); KnownBits RHSKnown(IT->getBitWidth()); Index: llvm/test/Transforms/InstCombine/pr53357.ll =================================================================== --- llvm/test/Transforms/InstCombine/pr53357.ll +++ llvm/test/Transforms/InstCombine/pr53357.ll @@ -5,11 +5,9 @@ ; (x & y) + ~(x | y) define i32 @src(i32 %0, i32 %1) { ; CHECK-LABEL: @src( -; CHECK-NEXT: [[TMP3:%.*]] = and i32 [[TMP1:%.*]], [[TMP0:%.*]] -; CHECK-NEXT: [[TMP4:%.*]] = or i32 [[TMP1]], [[TMP0]] -; CHECK-NEXT: [[TMP5:%.*]] = xor i32 [[TMP4]], -1 -; CHECK-NEXT: [[TMP6:%.*]] = add i32 [[TMP3]], [[TMP5]] -; CHECK-NEXT: ret i32 [[TMP6]] +; CHECK-NEXT: [[TMP3:%.*]] = xor i32 [[TMP1:%.*]], [[TMP0:%.*]] +; CHECK-NEXT: [[TMP4:%.*]] = xor i32 [[TMP3]], -1 +; CHECK-NEXT: ret i32 [[TMP4]] ; %3 = and i32 %1, %0 %4 = or i32 %1, %0 @@ -23,11 +21,9 @@ ; CHECK-LABEL: @src_thwart( ; CHECK-NEXT: [[X:%.*]] = sdiv i32 42, [[TMP0:%.*]] ; CHECK-NEXT: [[Y:%.*]] = sdiv i32 43, [[TMP1:%.*]] -; CHECK-NEXT: [[TMP3:%.*]] = and i32 [[Y]], [[X]] -; CHECK-NEXT: [[TMP4:%.*]] = or i32 [[Y]], [[X]] -; CHECK-NEXT: [[TMP5:%.*]] = xor i32 [[TMP4]], -1 -; CHECK-NEXT: [[TMP6:%.*]] = add i32 [[TMP3]], [[TMP5]] -; CHECK-NEXT: ret i32 [[TMP6]] +; CHECK-NEXT: [[TMP3:%.*]] = xor i32 [[Y]], [[X]] +; CHECK-NEXT: [[TMP4:%.*]] = xor i32 [[TMP3]], -1 +; CHECK-NEXT: ret i32 [[TMP4]] ; %x = sdiv i32 42, %0 ; thwart complexity-based canonicalization %y = sdiv i32 43, %1 ; thwart complexity-based canonicalization @@ -42,11 +38,9 @@ ; (x & y) + ~(y | x) define i32 @src2(i32 %0, i32 %1) { ; CHECK-LABEL: @src2( -; CHECK-NEXT: [[TMP3:%.*]] = and i32 [[TMP1:%.*]], [[TMP0:%.*]] -; CHECK-NEXT: [[TMP4:%.*]] = or i32 [[TMP0]], [[TMP1]] -; CHECK-NEXT: [[TMP5:%.*]] = xor i32 [[TMP4]], -1 -; CHECK-NEXT: [[TMP6:%.*]] = add i32 [[TMP3]], [[TMP5]] -; CHECK-NEXT: ret i32 [[TMP6]] +; CHECK-NEXT: [[TMP3:%.*]] = xor i32 [[TMP1:%.*]], [[TMP0:%.*]] +; CHECK-NEXT: [[TMP4:%.*]] = xor i32 [[TMP3]], -1 +; CHECK-NEXT: ret i32 [[TMP4]] ; %3 = and i32 %1, %0 %4 = or i32 %0, %1 @@ -60,11 +54,9 @@ ; CHECK-LABEL: @src2_thwart( ; CHECK-NEXT: [[X:%.*]] = sdiv i32 42, [[TMP0:%.*]] ; CHECK-NEXT: [[Y:%.*]] = sdiv i32 43, [[TMP1:%.*]] -; CHECK-NEXT: [[TMP3:%.*]] = and i32 [[Y]], [[X]] -; CHECK-NEXT: [[TMP4:%.*]] = or i32 [[X]], [[Y]] -; CHECK-NEXT: [[TMP5:%.*]] = xor i32 [[TMP4]], -1 -; CHECK-NEXT: [[TMP6:%.*]] = add i32 [[TMP3]], [[TMP5]] -; CHECK-NEXT: ret i32 [[TMP6]] +; CHECK-NEXT: [[TMP3:%.*]] = xor i32 [[Y]], [[X]] +; CHECK-NEXT: [[TMP4:%.*]] = xor i32 [[TMP3]], -1 +; CHECK-NEXT: ret i32 [[TMP4]] ; %x = sdiv i32 42, %0 ; thwart complexity-based canonicalization %y = sdiv i32 43, %1 ; thwart complexity-based canonicalization @@ -78,11 +70,9 @@ ; (x & y) + (~x & ~y) define i32 @src3(i32 %0, i32 %1) { ; CHECK-LABEL: @src3( -; CHECK-NEXT: [[TMP3:%.*]] = and i32 [[TMP1:%.*]], [[TMP0:%.*]] -; CHECK-NEXT: [[DOTDEMORGAN:%.*]] = or i32 [[TMP0]], [[TMP1]] -; CHECK-NEXT: [[TMP4:%.*]] = xor i32 [[DOTDEMORGAN]], -1 -; CHECK-NEXT: [[TMP5:%.*]] = add i32 [[TMP3]], [[TMP4]] -; CHECK-NEXT: ret i32 [[TMP5]] +; CHECK-NEXT: [[TMP3:%.*]] = xor i32 [[TMP1:%.*]], [[TMP0:%.*]] +; CHECK-NEXT: [[TMP4:%.*]] = xor i32 [[TMP3]], -1 +; CHECK-NEXT: ret i32 [[TMP4]] ; %3 = and i32 %1, %0 %4 = xor i32 %0, -1 @@ -97,11 +87,9 @@ ; CHECK-LABEL: @src3_thwart( ; CHECK-NEXT: [[X:%.*]] = sdiv i32 42, [[TMP0:%.*]] ; CHECK-NEXT: [[Y:%.*]] = sdiv i32 43, [[TMP1:%.*]] -; CHECK-NEXT: [[TMP3:%.*]] = and i32 [[Y]], [[X]] -; CHECK-NEXT: [[DOTDEMORGAN:%.*]] = or i32 [[X]], [[Y]] -; CHECK-NEXT: [[TMP4:%.*]] = xor i32 [[DOTDEMORGAN]], -1 -; CHECK-NEXT: [[TMP5:%.*]] = add i32 [[TMP3]], [[TMP4]] -; CHECK-NEXT: ret i32 [[TMP5]] +; CHECK-NEXT: [[TMP3:%.*]] = xor i32 [[Y]], [[X]] +; CHECK-NEXT: [[TMP4:%.*]] = xor i32 [[TMP3]], -1 +; CHECK-NEXT: ret i32 [[TMP4]] ; %x = sdiv i32 42, %0 ; thwart complexity-based canonicalization %y = sdiv i32 43, %1 ; thwart complexity-based canonicalization @@ -116,11 +104,9 @@ ; ~(x | y) + (y & x) define i32 @src4(i32 %0, i32 %1) { ; CHECK-LABEL: @src4( -; CHECK-NEXT: [[TMP3:%.*]] = and i32 [[TMP0:%.*]], [[TMP1:%.*]] -; CHECK-NEXT: [[TMP4:%.*]] = or i32 [[TMP1]], [[TMP0]] -; CHECK-NEXT: [[TMP5:%.*]] = xor i32 [[TMP4]], -1 -; CHECK-NEXT: [[TMP6:%.*]] = add i32 [[TMP3]], [[TMP5]] -; CHECK-NEXT: ret i32 [[TMP6]] +; CHECK-NEXT: [[TMP3:%.*]] = xor i32 [[TMP0:%.*]], [[TMP1:%.*]] +; CHECK-NEXT: [[TMP4:%.*]] = xor i32 [[TMP3]], -1 +; CHECK-NEXT: ret i32 [[TMP4]] ; %3 = and i32 %0, %1 %4 = or i32 %1, %0 @@ -134,11 +120,9 @@ ; CHECK-LABEL: @src4_thwart( ; CHECK-NEXT: [[X:%.*]] = sdiv i32 42, [[TMP0:%.*]] ; CHECK-NEXT: [[Y:%.*]] = sdiv i32 43, [[TMP1:%.*]] -; CHECK-NEXT: [[TMP3:%.*]] = and i32 [[X]], [[Y]] -; CHECK-NEXT: [[TMP4:%.*]] = or i32 [[Y]], [[X]] -; CHECK-NEXT: [[TMP5:%.*]] = xor i32 [[TMP4]], -1 -; CHECK-NEXT: [[TMP6:%.*]] = add i32 [[TMP3]], [[TMP5]] -; CHECK-NEXT: ret i32 [[TMP6]] +; CHECK-NEXT: [[TMP3:%.*]] = xor i32 [[X]], [[Y]] +; CHECK-NEXT: [[TMP4:%.*]] = xor i32 [[TMP3]], -1 +; CHECK-NEXT: ret i32 [[TMP4]] ; %x = sdiv i32 42, %0 ; thwart complexity-based canonicalization %y = sdiv i32 43, %1 ; thwart complexity-based canonicalization @@ -152,11 +136,9 @@ ; ~(x | y) + (x & y) define i32 @src5(i32 %0, i32 %1) { ; CHECK-LABEL: @src5( -; CHECK-NEXT: [[TMP3:%.*]] = or i32 [[TMP1:%.*]], [[TMP0:%.*]] +; CHECK-NEXT: [[TMP3:%.*]] = xor i32 [[TMP1:%.*]], [[TMP0:%.*]] ; CHECK-NEXT: [[TMP4:%.*]] = xor i32 [[TMP3]], -1 -; CHECK-NEXT: [[TMP5:%.*]] = and i32 [[TMP1]], [[TMP0]] -; CHECK-NEXT: [[TMP6:%.*]] = add i32 [[TMP5]], [[TMP4]] -; CHECK-NEXT: ret i32 [[TMP6]] +; CHECK-NEXT: ret i32 [[TMP4]] ; %3 = or i32 %1, %0 %4 = xor i32 %3, -1 @@ -170,11 +152,9 @@ ; CHECK-LABEL: @src5_thwart( ; CHECK-NEXT: [[X:%.*]] = sdiv i32 42, [[TMP0:%.*]] ; CHECK-NEXT: [[Y:%.*]] = sdiv i32 43, [[TMP1:%.*]] -; CHECK-NEXT: [[TMP3:%.*]] = or i32 [[Y]], [[X]] +; CHECK-NEXT: [[TMP3:%.*]] = xor i32 [[Y]], [[X]] ; CHECK-NEXT: [[TMP4:%.*]] = xor i32 [[TMP3]], -1 -; CHECK-NEXT: [[TMP5:%.*]] = and i32 [[Y]], [[X]] -; CHECK-NEXT: [[TMP6:%.*]] = add i32 [[TMP5]], [[TMP4]] -; CHECK-NEXT: ret i32 [[TMP6]] +; CHECK-NEXT: ret i32 [[TMP4]] ; %x = sdiv i32 42, %0 ; thwart complexity-based canonicalization %y = sdiv i32 43, %1 ; thwart complexity-based canonicalization @@ -188,11 +168,9 @@ ; (x & y) + (~x & ~y) define i32 @src6(i32 %0, i32 %1) { ; CHECK-LABEL: @src6( -; CHECK-NEXT: [[DOTDEMORGAN:%.*]] = or i32 [[TMP0:%.*]], [[TMP1:%.*]] -; CHECK-NEXT: [[TMP3:%.*]] = xor i32 [[DOTDEMORGAN]], -1 -; CHECK-NEXT: [[TMP4:%.*]] = and i32 [[TMP1]], [[TMP0]] -; CHECK-NEXT: [[TMP5:%.*]] = add i32 [[TMP4]], [[TMP3]] -; CHECK-NEXT: ret i32 [[TMP5]] +; CHECK-NEXT: [[TMP3:%.*]] = xor i32 [[TMP1:%.*]], [[TMP0:%.*]] +; CHECK-NEXT: [[TMP4:%.*]] = xor i32 [[TMP3]], -1 +; CHECK-NEXT: ret i32 [[TMP4]] ; %3 = xor i32 %0, -1 %4 = xor i32 %1, -1 @@ -207,11 +185,9 @@ ; CHECK-LABEL: @src6_thwart( ; CHECK-NEXT: [[X:%.*]] = sdiv i32 42, [[TMP0:%.*]] ; CHECK-NEXT: [[Y:%.*]] = sdiv i32 43, [[TMP1:%.*]] -; CHECK-NEXT: [[DOTDEMORGAN:%.*]] = or i32 [[X]], [[Y]] -; CHECK-NEXT: [[TMP3:%.*]] = xor i32 [[DOTDEMORGAN]], -1 -; CHECK-NEXT: [[TMP4:%.*]] = and i32 [[Y]], [[X]] -; CHECK-NEXT: [[TMP5:%.*]] = add i32 [[TMP4]], [[TMP3]] -; CHECK-NEXT: ret i32 [[TMP5]] +; CHECK-NEXT: [[TMP3:%.*]] = xor i32 [[Y]], [[X]] +; CHECK-NEXT: [[TMP4:%.*]] = xor i32 [[TMP3]], -1 +; CHECK-NEXT: ret i32 [[TMP4]] ; %x = sdiv i32 42, %0 ; thwart complexity-based canonicalization %y = sdiv i32 43, %1 ; thwart complexity-based canonicalization @@ -226,11 +202,9 @@ ; vector version of src6 define <2 x i32> @src6_vec(<2 x i32> %0, <2 x i32> %1) { ; CHECK-LABEL: @src6_vec( -; CHECK-NEXT: [[DOTDEMORGAN:%.*]] = or <2 x i32> [[TMP0:%.*]], [[TMP1:%.*]] -; CHECK-NEXT: [[TMP3:%.*]] = xor <2 x i32> [[DOTDEMORGAN]], -; CHECK-NEXT: [[TMP4:%.*]] = and <2 x i32> [[TMP1]], [[TMP0]] -; CHECK-NEXT: [[TMP5:%.*]] = add <2 x i32> [[TMP4]], [[TMP3]] -; CHECK-NEXT: ret <2 x i32> [[TMP5]] +; CHECK-NEXT: [[TMP3:%.*]] = xor <2 x i32> [[TMP1:%.*]], [[TMP0:%.*]] +; CHECK-NEXT: [[TMP4:%.*]] = xor <2 x i32> [[TMP3]], +; CHECK-NEXT: ret <2 x i32> [[TMP4]] ; %3 = xor <2 x i32> %0, %4 = xor <2 x i32> %1, @@ -243,11 +217,9 @@ ; check the transformation is still valid with undef define <2 x i32> @src6_vec_undef(<2 x i32> %0, <2 x i32> %1) { ; CHECK-LABEL: @src6_vec_undef( -; CHECK-NEXT: [[DOTDEMORGAN:%.*]] = or <2 x i32> [[TMP0:%.*]], [[TMP1:%.*]] -; CHECK-NEXT: [[TMP3:%.*]] = xor <2 x i32> [[DOTDEMORGAN]], -; CHECK-NEXT: [[TMP4:%.*]] = and <2 x i32> [[TMP1]], [[TMP0]] -; CHECK-NEXT: [[TMP5:%.*]] = add <2 x i32> [[TMP4]], [[TMP3]] -; CHECK-NEXT: ret <2 x i32> [[TMP5]] +; CHECK-NEXT: [[TMP3:%.*]] = xor <2 x i32> [[TMP1:%.*]], [[TMP0:%.*]] +; CHECK-NEXT: [[TMP4:%.*]] = xor <2 x i32> [[TMP3]], +; CHECK-NEXT: ret <2 x i32> [[TMP4]] ; %3 = xor <2 x i32> %0, %4 = xor <2 x i32> %1, @@ -262,11 +234,9 @@ ; CHECK-LABEL: @src6_vec_thwart( ; CHECK-NEXT: [[X:%.*]] = sdiv <2 x i32> , [[TMP0:%.*]] ; CHECK-NEXT: [[Y:%.*]] = sdiv <2 x i32> , [[TMP1:%.*]] -; CHECK-NEXT: [[DOTDEMORGAN:%.*]] = or <2 x i32> [[X]], [[Y]] -; CHECK-NEXT: [[TMP3:%.*]] = xor <2 x i32> [[DOTDEMORGAN]], -; CHECK-NEXT: [[TMP4:%.*]] = and <2 x i32> [[Y]], [[X]] -; CHECK-NEXT: [[TMP5:%.*]] = add <2 x i32> [[TMP4]], [[TMP3]] -; CHECK-NEXT: ret <2 x i32> [[TMP5]] +; CHECK-NEXT: [[TMP3:%.*]] = xor <2 x i32> [[Y]], [[X]] +; CHECK-NEXT: [[TMP4:%.*]] = xor <2 x i32> [[TMP3]], +; CHECK-NEXT: ret <2 x i32> [[TMP4]] ; %x = sdiv <2 x i32> , %0 ; thwart complexity-based canonicalization %y = sdiv <2 x i32> , %1 ; thwart complexity-based canonicalization