[llvm] [TargetLowering] Fold (a | b) ==/!= b -> (a & ~b) == /!= 0 when and-not exists (PR #145368)

via llvm-commits llvm-commits at lists.llvm.org
Tue Jun 24 08:58:58 PDT 2025


================
@@ -4212,6 +4212,53 @@ SDValue TargetLowering::foldSetCCWithAnd(EVT VT, SDValue N0, SDValue N1,
   return SDValue();
 }
 
+/// This helper function of SimplifySetCC tries to optimize the comparison when
+/// either operand of the SetCC node is a bitwise-or instruction.
+SDValue TargetLowering::foldSetCCWithOr(EVT VT, SDValue N0, SDValue N1,
+                                        ISD::CondCode Cond, const SDLoc &DL,
+                                        DAGCombinerInfo &DCI) const {
+  if (N1.getOpcode() == ISD::OR && N0.getOpcode() != ISD::OR)
+    std::swap(N0, N1);
+
+  SelectionDAG &DAG = DCI.DAG;
+  EVT OpVT = N0.getValueType();
+  if (N0.getOpcode() != ISD::OR || !OpVT.isInteger() ||
+      (Cond != ISD::SETEQ && Cond != ISD::SETNE))
+    return SDValue();
+
+  // Match these patterns in any of their permutations:
+  // (X | Y) == Y
+  // (X | Y) != Y
+  SDValue X, Y;
+  if (N0.getOperand(0) == N1) {
----------------
AZero13 wrote:

> OK - so maybe clean that function up first?

No we cannot use this in Target lowering. m_Or is not defined, and neither is sd_match. No function in target lowering does this, and it seems it was done this way for a reason. 

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


More information about the llvm-commits mailing list