Index: llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp =================================================================== --- llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp +++ llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp @@ -892,6 +892,15 @@ if (!match(Op1, m_APInt(C))) return nullptr; + // (X | C2) + C --> (X | C2) ^ C2 + // (C2 | X) + C --> (C2 | X) ^ C2 + // iff (C2 == -C) + const APInt *C2; + if (match(Op0, m_c_Or(m_Value(), m_APInt(C2))) && + *C2 == -*C) + return BinaryOperator::CreateXor(Op0, + ConstantInt::get(Add.getType(), *C2)); + if (C->isSignMask()) { // If wrapping is not allowed, then the addition must set the sign bit: // X + (signmask) --> X | signmask @@ -906,7 +915,6 @@ // Is this add the last step in a convoluted sext? // add(zext(xor i16 X, -32768), -32768) --> sext X Type *Ty = Add.getType(); - const APInt *C2; if (match(Op0, m_ZExt(m_Xor(m_Value(X), m_APInt(C2)))) && C2->isMinSignedValue() && C2->sext(Ty->getScalarSizeInBits()) == *C) return CastInst::Create(Instruction::SExt, X, Ty); Index: llvm/test/Transforms/InstCombine/add.ll =================================================================== --- llvm/test/Transforms/InstCombine/add.ll +++ llvm/test/Transforms/InstCombine/add.ll @@ -978,3 +978,34 @@ %E = add i32 %D, 1 ret i32 %E } + +; (X | C1) + C2 --> (X | C1) ^ C1 iff (C1 == -C2) +define i32 @test44(i32 %A) { +; CHECK-LABEL: @test44( +; CHECK-NEXT: [[TMP1:%.*]] = and i32 [[A:%.*]], -124 +; CHECK-NEXT: ret i32 [[TMP1]] +; + %B = or i32 %A, 123 + %C = add i32 %B, -123 + ret i32 %C +} + +define i32 @test44_commuted(i32 %A) { +; CHECK-LABEL: @test44_commuted( +; CHECK-NEXT: [[TMP1:%.*]] = and i32 [[A:%.*]], -124 +; CHECK-NEXT: ret i32 [[TMP1]] +; + %B = or i32 123, %A + %C = add i32 %B, -123 + ret i32 %C +} + +define <2 x i32> @test44_vec(<2 x i32> %A) { +; CHECK-LABEL: @test44_vec( +; CHECK-NEXT: [[TMP1:%.*]] = and <2 x i32> [[A:%.*]], +; CHECK-NEXT: ret <2 x i32> [[TMP1]] +; + %B = or <2 x i32> %A, + %C = add <2 x i32> %B, + ret <2 x i32> %C +} Index: llvm/test/Transforms/InstCombine/sub.ll =================================================================== --- llvm/test/Transforms/InstCombine/sub.ll +++ llvm/test/Transforms/InstCombine/sub.ll @@ -1267,6 +1267,55 @@ ret <2 x i32> %res } +; Check (X | Y) - Y --> X & ~Y when Y is a constant +define i32 @test70(i32 %A) { +; CHECK-LABEL: @test70( +; CHECK-NEXT: [[TMP1:%.*]] = and i32 [[A:%.*]], -124 +; CHECK-NEXT: ret i32 [[TMP1]] +; + %B = or i32 %A, 123 + %C = sub i32 %B, 123 + ret i32 %C +} + +; Check (X | Y) - Y --> (X | Y) ^ Y doesn't happen where (X | Y) has multiple uses +define i32 @test71(i32 %A, i32 %B) { +; CHECK-LABEL: @test71( +; CHECK-NEXT: [[C:%.*]] = or i32 [[A:%.*]], [[B:%.*]] +; CHECK-NEXT: [[D:%.*]] = sub i32 [[C]], [[B]] +; CHECK-NEXT: [[E:%.*]] = mul i32 [[C]], [[D]] +; CHECK-NEXT: ret i32 [[E]] +; + %C = or i32 %A, %B + %D = sub i32 %C, %B + %E = mul i32 %C, %D + ret i32 %E +} + +; Check (X | Y) - Y --> X & ~Y where X and Y are vectors +define <2 x i32> @test72(<2 x i32> %A, <2 x i32> %B) { +; CHECK-LABEL: @test72( +; CHECK-NEXT: [[B_NOT:%.*]] = xor <2 x i32> [[B:%.*]], +; CHECK-NEXT: [[D:%.*]] = and <2 x i32> [[B_NOT]], [[A:%.*]] +; CHECK-NEXT: ret <2 x i32> [[D]] +; + %C = or <2 x i32> %A, %B + %D = sub <2 x i32> %C, %B + ret <2 x i32> %D +} + +; Check reversing sub operands won't trigger (X | Y) - Y --> X & ~Y +define i32 @test73(i32 %A, i32 %B) { +; CHECK-LABEL: @test73( +; CHECK-NEXT: [[C:%.*]] = or i32 [[A:%.*]], [[B:%.*]] +; CHECK-NEXT: [[D:%.*]] = sub i32 [[B]], [[C]] +; CHECK-NEXT: ret i32 [[D]] +; + %C = or i32 %A, %B + %D = sub i32 %B, %C + ret i32 %D +} + define i32 @nsw_inference1(i32 %x, i32 %y) { ; CHECK-LABEL: @nsw_inference1( ; CHECK-NEXT: [[X2:%.*]] = or i32 [[X:%.*]], 1024