[llvm] [InstCombine] Fold Xor with or disjoint (PR #105992)

via llvm-commits llvm-commits at lists.llvm.org
Sun Sep 1 11:51:09 PDT 2024


================
@@ -4706,6 +4706,15 @@ Instruction *InstCombinerImpl::visitXor(BinaryOperator &I) {
     return Xor;
 
   Value *X, *Y;
+
+  // (A | B) ^ C -> (A ^ C) ^ B
+  // C ^ (A | B) -> B ^ (A ^ C)
+  if (match(&I, m_c_Xor(m_OneUse(m_c_DisjointOr(m_Value(X), m_Value(Y))),
+                        m_Value(M)))) {
+    Value *XorAC = Builder.CreateXor(X, M);
+    return BinaryOperator::CreateXor(XorAC, Y);
+  }
----------------
goldsteinn wrote:

Is this desirable? I'd say `disjoint or` is preferable to an `xor` in most if not all respects.

I would limit this if `X ^ M` folds. You can test it with:
`if (Value * XorAC = simplifyBinOp(Instruction::Xor, X, M, SQ)`

https://github.com/llvm/llvm-project/pull/105992


More information about the llvm-commits mailing list