Index: llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp =================================================================== --- llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp +++ llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp @@ -3371,6 +3371,10 @@ match(Op1, m_Not(m_Specific(A)))) return BinaryOperator::CreateNot(Builder.CreateAnd(A, B)); + // (~A & B) ^ A --> (A | B) -- There are 4 commuted variants. + if (match(&I, m_c_Xor(m_c_And(m_Not(m_Value(A)), m_Value(B)), m_Deferred(A)))) + return BinaryOperator::CreateOr(A, B); + // (A | B) ^ (A | C) --> (B ^ C) & ~A -- There are 4 commuted variants. // TODO: Loosen one-use restriction if common operand is a constant. Value *D; Index: llvm/test/Transforms/InstCombine/xor.ll =================================================================== --- llvm/test/Transforms/InstCombine/xor.ll +++ llvm/test/Transforms/InstCombine/xor.ll @@ -1179,9 +1179,7 @@ ; CHECK-LABEL: @test52( ; CHECK-NEXT: [[A:%.*]] = udiv i32 42, [[P1:%.*]] ; CHECK-NEXT: [[B:%.*]] = udiv i32 42, [[P2:%.*]] -; CHECK-NEXT: [[O:%.*]] = xor i32 [[A]], -1 -; CHECK-NEXT: [[R:%.*]] = and i32 [[B]], [[O]] -; CHECK-NEXT: [[Z:%.*]] = xor i32 [[R]], [[A]] +; CHECK-NEXT: [[Z:%.*]] = or i32 [[A]], [[B]] ; CHECK-NEXT: ret i32 [[Z]] ; %a = udiv i32 42, %p1 @@ -1199,9 +1197,7 @@ ; CHECK-LABEL: @test53( ; CHECK-NEXT: [[A:%.*]] = udiv i32 42, [[P1:%.*]] ; CHECK-NEXT: [[B:%.*]] = udiv i32 42, [[P2:%.*]] -; CHECK-NEXT: [[O:%.*]] = xor i32 [[B]], -1 -; CHECK-NEXT: [[R:%.*]] = and i32 [[A]], [[O]] -; CHECK-NEXT: [[Z:%.*]] = xor i32 [[R]], [[B]] +; CHECK-NEXT: [[Z:%.*]] = or i32 [[B]], [[A]] ; CHECK-NEXT: ret i32 [[Z]] ; %a = udiv i32 42, %p1 @@ -1216,9 +1212,7 @@ ; CHECK-LABEL: @test54( ; CHECK-NEXT: [[A:%.*]] = udiv i32 42, [[P1:%.*]] ; CHECK-NEXT: [[B:%.*]] = udiv i32 42, [[P2:%.*]] -; CHECK-NEXT: [[O:%.*]] = xor i32 [[A]], -1 -; CHECK-NEXT: [[R:%.*]] = and i32 [[B]], [[O]] -; CHECK-NEXT: [[Z:%.*]] = xor i32 [[R]], [[A]] +; CHECK-NEXT: [[Z:%.*]] = or i32 [[A]], [[B]] ; CHECK-NEXT: ret i32 [[Z]] ; %a = udiv i32 42, %p1 @@ -1233,9 +1227,7 @@ ; CHECK-LABEL: @test55( ; CHECK-NEXT: [[A:%.*]] = udiv i32 42, [[P1:%.*]] ; CHECK-NEXT: [[B:%.*]] = udiv i32 42, [[P2:%.*]] -; CHECK-NEXT: [[O:%.*]] = xor i32 [[A]], -1 -; CHECK-NEXT: [[R:%.*]] = and i32 [[B]], [[O]] -; CHECK-NEXT: [[Z:%.*]] = xor i32 [[A]], [[R]] +; CHECK-NEXT: [[Z:%.*]] = or i32 [[A]], [[B]] ; CHECK-NEXT: ret i32 [[Z]] ; %a = udiv i32 42, %p1