Skip to content

[TargetLowering] Add and (rot X, Y), Z ==/!= -1 --> (and X, Z) ==/!= -1 to foldSetCCWithRotate #147068

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

AZero13
Copy link
Contributor

@AZero13 AZero13 commented Jul 4, 2025

This adds the complement to the or transform of the same variety

@llvmbot llvmbot added the llvm:SelectionDAG SelectionDAGISel as well label Jul 4, 2025
@AZero13 AZero13 marked this pull request as draft July 4, 2025 14:32
@llvmbot
Copy link
Member

llvmbot commented Jul 4, 2025

@llvm/pr-subscribers-backend-x86

@llvm/pr-subscribers-llvm-selectiondag

Author: AZero13 (AZero13)

Changes

Full diff: https://github.com/llvm/llvm-project/pull/147068.diff

1 Files Affected:

  • (modified) llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp (+15-1)
diff --git a/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp b/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp
index 000f8cc6786a5..f552104072405 100644
--- a/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp
@@ -4567,7 +4567,6 @@ static SDValue foldSetCCWithRotate(EVT VT, SDValue N0, SDValue N1,
   // or (rot X, Y), Z ==/!= 0 --> (or X, Z) ==/!= 0
   // or Z, (rot X, Y) ==/!= 0 --> (or X, Z) ==/!= 0
   //
-  // TODO: Add the 'and' with -1 sibling.
   // TODO: Recurse through a series of 'or' ops to find the rotate.
   EVT OpVT = N0.getValueType();
   if (N0.hasOneUse() && N0.getOpcode() == ISD::OR && C1->isZero()) {
@@ -4581,6 +4580,21 @@ static SDValue foldSetCCWithRotate(EVT VT, SDValue N0, SDValue N1,
     }
   }
 
+  // and (rot X, Y), Z ==/!= -1 --> (and X, Z) ==/!= -1
+  // and Z, (rot X, Y) ==/!= -1 --> (and X, Z) ==/!= -1
+  //
+  // TODO: Recursively peek through a series of 'and' ops to find the rotate.
+  if (N0.hasOneUse() && N0.getOpcode() == ISD::AND && C1->isAllOnes()) {
+    if (SDValue R = getRotateSource(N0.getOperand(0))) {
+      SDValue NewAnd = DAG.getNode(ISD::AND, dl, OpVT, R, N0.getOperand(1));
+      return DAG.getSetCC(dl, VT, NewAnd, N1, Cond);
+    }
+    if (SDValue R = getRotateSource(N0.getOperand(1))) {
+      SDValue NewAnd = DAG.getNode(ISD::AND, dl, OpVT, R, N0.getOperand(0));
+      return DAG.getSetCC(dl, VT, NewAnd, N1, Cond);
+    }
+  }
+
   return SDValue();
 }
 

@AZero13 AZero13 marked this pull request as ready for review July 4, 2025 15:46
…-1 to foldSetCCWithRotate

This adds the complement to the or version of this transformation.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend:X86 llvm:SelectionDAG SelectionDAGISel as well
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants