Skip to content

Commit bc275d6

Browse files
committed
[InstSimplify] Fold (X ^ C) & (X ^ ~C) into zero and (X ^ C) | (X ^ ~C) to minus one.
1 parent 966d3ae commit bc275d6

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

llvm/lib/Analysis/InstructionSimplify.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2204,6 +2204,13 @@ static Value *simplifyAndInst(Value *Op0, Value *Op1, const SimplifyQuery &Q,
22042204
match(Op1, m_c_Xor(m_Specific(Or), m_Specific(Y))))
22052205
return Constant::getNullValue(Op0->getType());
22062206

2207+
const APInt *C1;
2208+
Value *A;
2209+
// (A ^ C) & (A ^ ~C) -> 0
2210+
if (match(Op0, m_Xor(m_Value(A), m_APInt(C1))) &&
2211+
match(Op1, m_Xor(m_Specific(A), m_SpecificInt(~*C1))))
2212+
return Constant::getNullValue(Op0->getType());
2213+
22072214
if (Op0->getType()->isIntOrIntVectorTy(1)) {
22082215
if (std::optional<bool> Implied = isImpliedCondition(Op0, Op1, Q.DL)) {
22092216
// If Op0 is true implies Op1 is true, then Op0 is a subset of Op1.
@@ -2473,6 +2480,11 @@ static Value *simplifyOrInst(Value *Op0, Value *Op1, const SimplifyQuery &Q,
24732480
if (Value *V = threadBinOpOverPHI(Instruction::Or, Op0, Op1, Q, MaxRecurse))
24742481
return V;
24752482

2483+
// (A ^ C) | (A ^ ~C) -> -1, i.e. all bits set to one.
2484+
if (match(Op0, m_Xor(m_Value(A), m_APInt(C1))) &&
2485+
match(Op1, m_Xor(m_Specific(A), m_SpecificInt(~*C1))))
2486+
return Constant::getAllOnesValue(Op0->getType());
2487+
24762488
if (Op0->getType()->isIntOrIntVectorTy(1)) {
24772489
if (std::optional<bool> Implied =
24782490
isImpliedCondition(Op0, Op1, Q.DL, false)) {

0 commit comments

Comments
 (0)