Skip to content

[SelectionDAG] Peek through freeze to see XOR #147821

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 9, 2025

It does not matter if xor is undef or not, because x ^ x is 0, even undef.

Yes this is more of a change that aligns with x ^ x being 0 even if undef.

@AZero13 AZero13 marked this pull request as ready for review July 9, 2025 20:41
@llvmbot llvmbot added the llvm:SelectionDAG SelectionDAGISel as well label Jul 9, 2025
@llvmbot
Copy link
Member

llvmbot commented Jul 9, 2025

@llvm/pr-subscribers-backend-x86

@llvm/pr-subscribers-llvm-selectiondag

Author: AZero13 (AZero13)

Changes

It does not matter if xor is undef or not, because x ^ x is 0, even undef.


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

1 Files Affected:

  • (modified) llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp (+8-1)
diff --git a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
index 9ffdda28f7899..b10a6cc9a6678 100644
--- a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
@@ -10016,8 +10016,15 @@ SDValue DAGCombiner::visitXOR(SDNode *N) {
     }
   }
 
+  auto PeekThroughFreeze = [](SDValue N) {
+    if (N->getOpcode() == ISD::FREEZE && N.hasOneUse())
+      return N->getOperand(0);
+    return N;
+  };
+
   // fold (xor x, x) -> 0
-  if (N0 == N1)
+  // FIXME: Refactor this and sub and other similar operations together.
+  if (PeekThroughFreeze(N0) == PeekThroughFreeze(N1))
     return tryFoldToZero(DL, TLI, VT, DAG, LegalOperations);
 
   // fold (xor (shl 1, x), -1) -> (rotl ~1, x)

@AZero13 AZero13 marked this pull request as draft July 9, 2025 21:27
@RKSimon
Copy link
Collaborator

RKSimon commented Jul 10, 2025

DAG now aggressively pushes freeze up through logic ops, why is this patch necessary?

@AZero13 AZero13 closed this Jul 10, 2025
@topperc
Copy link
Collaborator

topperc commented Jul 10, 2025

DAG now aggressively pushes freeze up through logic ops, why is this patch necessary?

In this case the input the XOR is a freeze. We don't know what the input the freeze is. Could the input to the freeze be an operation we don't push up across?

@AZero13
Copy link
Contributor Author

AZero13 commented Jul 10, 2025

#148022

Well tests say otherwise.

@topperc
Copy link
Collaborator

topperc commented Jul 10, 2025

#148022

Well tests say otherwise.

Ok then I'll write the test https://godbolt.org/z/v65a16W86 or https://godbolt.org/z/aWohvzx3T

@AZero13 AZero13 reopened this Jul 11, 2025
@AZero13 AZero13 marked this pull request as ready for review July 11, 2025 01:42
@AZero13
Copy link
Contributor Author

AZero13 commented Jul 11, 2025

@topperc Okay, it works.

@AZero13 AZero13 changed the title Peek through freeze to see XOR [SelectionDAG] Peek through freeze to see XOR Jul 11, 2025
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
; RUN: llc -mtriple=x86_64-unknown-linux-gnu < %s 2>&1 | FileCheck %s --check-prefix=X86ASM

define i32 @sub_freeze(i32 %x, i32 %y) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please can you put these in freeze-binary.ll

; X86ASM-NEXT: retq
%a = add nuw i32 %x, %y
%b = freeze i32 %a
%c = sub i32 %a, %b
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nikic @topperc isn't the real problem that we've ended up with frozen and unfrozen versions of the same value? It looks like DAG is missing an equivalent of InstCombinerImpl::freezeOtherUses - WDYT?

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.

4 participants