Index: llvm/lib/Analysis/ValueTracking.cpp =================================================================== --- llvm/lib/Analysis/ValueTracking.cpp +++ llvm/lib/Analysis/ValueTracking.cpp @@ -270,6 +270,12 @@ 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 pattern:(A & ~B) op B + if (match(LHS, m_c_And(m_Not(m_Specific(RHS)), m_Value())) || + match(RHS, m_c_And(m_Not(m_Specific(LHS)), m_Value()))) + return true; + IntegerType *IT = cast(LHS->getType()->getScalarType()); KnownBits LHSKnown(IT->getBitWidth()); KnownBits RHSKnown(IT->getBitWidth()); Index: llvm/test/Analysis/ValueTracking/known-non-common-bits.ll =================================================================== --- /dev/null +++ llvm/test/Analysis/ValueTracking/known-non-common-bits.ll @@ -0,0 +1,25 @@ +; NOTE: Assertions have been autogenerated by utils/update_test_checks.py +; Try to fold (A & ~B) + B to A | B +; RUN: opt < %s -instcombine -S | FileCheck %s + +define dso_local i32 @_Z11and_to_xor1ii(i32 %0, i32 %1) { +; CHECK-LABEL: @_Z11and_to_xor1ii( +; CHECK-NEXT: [[TMP3:%.*]] = or i32 [[TMP0:%.*]], [[TMP1:%.*]] +; CHECK-NEXT: ret i32 [[TMP3]] +; + %3 = xor i32 %1, -1 + %4 = and i32 %3, %0 + %5 = add nsw i32 %4, %1 + ret i32 %5 +} + +define dso_local i32 @_Z11and_to_xor1ii2(i32 %0, i32 %1) { +; CHECK-LABEL: @_Z11and_to_xor1ii2( +; CHECK-NEXT: [[TMP3:%.*]] = or i32 [[TMP0:%.*]], [[TMP1:%.*]] +; CHECK-NEXT: ret i32 [[TMP3]] +; + %3 = xor i32 %1, -1 + %4 = and i32 %3, %0 + %5 = or i32 %4, %1 + ret i32 %5 +}