[llvm] [InstCombine] Fold (sub (xor X, (sext C)), (sext C)) => (select C (neg X), X) (PR #79417)

Kai Luo via llvm-commits llvm-commits at lists.llvm.org
Fri Jan 26 19:14:44 PST 2024


================
@@ -2448,6 +2448,21 @@ Instruction *InstCombinerImpl::visitSub(BinaryOperator &I) {
     }
   }
 
+  {
+    // (sub (xor X, (sext C)), (sext C)) => (select C, (neg X), X)
+    // (sub (sext C), (xor X, (sext C))) => (select C, X, (neg X))
+    Value *C, *X;
+    auto m_SubXorCmp = [&C, &X](Value *LHS, Value *RHS) {
+      return match(LHS, m_c_Xor(m_Value(X), m_SExt(m_OneUse(m_Value(C))))) &&
----------------
bzEq wrote:

> What is the purpose of the m_OneUse?

Looks no need to check `m_OneUse` of the `i1` value, since we are still using it in the result.
What concerns me is usage of `(sext i1)`, if it's still using by other instructions other than this `(sub (sext C), (xor X, (sext C)))`, not sure if it's still profitable. I'll add test cases to demonstrate as suggested by @dtcxzyw .

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


More information about the llvm-commits mailing list