Skip to content

Conversation

@AZero13
Copy link
Contributor

@AZero13 AZero13 commented Jul 3, 2025

If x - y == 0, then x ^ y == 0. Therefore, we can do the exact same checks.

https://alive2.llvm.org/ce/z/MtBRoj

@AZero13 AZero13 requested a review from nikic as a code owner July 3, 2025 23:55
@llvmbot llvmbot added the llvm:analysis Includes value tracking, cost tables and constant folding label Jul 3, 2025
@llvmbot
Copy link
Member

llvmbot commented Jul 3, 2025

@llvm/pr-subscribers-llvm-transforms

@llvm/pr-subscribers-llvm-analysis

Author: AZero13 (AZero13)

Changes

It goes without saying that for x ^ y to be 0, x and y must be equal.


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

1 Files Affected:

  • (modified) llvm/lib/Analysis/ValueTracking.cpp (+4)
diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index e576f4899810a..ba8ae3a975729 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -3043,6 +3043,10 @@ static bool isKnownNonZeroFromOperator(const Operator *I,
     // (X ^ (X != 0)) is non zero
     if (matchOpWithOpEqZero(I->getOperand(0), I->getOperand(1)))
       return true;
+    // X ^ Y != 0 if X != Y.
+    if (isKnownNonEqual(I->getOperand(0), I->getOperand(1), DemandedElts, Q,
+                        Depth))
+      return true;
     break;
   case Instruction::Or:
     // (X | (X != 0)) is non zero

@AZero13 AZero13 marked this pull request as draft July 3, 2025 23:56
@AZero13
Copy link
Contributor Author

AZero13 commented Jul 4, 2025

I really cannot find a case for this, but this is just too obvious and a fast path.

@AZero13
Copy link
Contributor Author

AZero13 commented Jul 4, 2025

@dtcxzyw Can I see if it affects opt at all so I can use it to make some tests.

@AZero13 AZero13 marked this pull request as ready for review July 4, 2025 00:45
@AZero13 AZero13 changed the title [ValueTracking] X ^ Y != 0 if X != Y. [ValueTracking] X ^ Y != 0 if X != Y Jul 4, 2025
@AZero13 AZero13 force-pushed the xorss branch 2 times, most recently from f6d250b to 2636776 Compare July 4, 2025 13:33
@AZero13
Copy link
Contributor Author

AZero13 commented Jul 4, 2025

Done! @nikic

Copy link
Contributor

@nikic nikic left a comment

Choose a reason for hiding this comment

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

Please also add a test showing an improvement (you can take inspiration from the llvm-opt-benchmark results).

@llvmbot llvmbot added llvm:instcombine Covers the InstCombine, InstSimplify and AggressiveInstCombine passes llvm:transforms labels Jul 5, 2025
@AZero13
Copy link
Contributor Author

AZero13 commented Jul 5, 2025

Please also add a test showing an improvement (you can take inspiration from the llvm-opt-benchmark results).

Done!

@AZero13 AZero13 requested a review from nikic July 5, 2025 15:07
@AZero13
Copy link
Contributor Author

AZero13 commented Jul 5, 2025

Actually, I have a better idea!

@AZero13
Copy link
Contributor Author

AZero13 commented Jul 5, 2025

@nikic I am going to have XOR and sub take the same path:

https://alive2.llvm.org/ce/z/MtBRoj

@AZero13 AZero13 changed the title [ValueTracking] X ^ Y != 0 if X != Y [ValueTracking] Have sub and xor in KnownNonZero take the same exact path Jul 5, 2025
…path

If x - y == 0, then x ^ y == 0. Therefore, we can do the exact same checks.

https://alive2.llvm.org/ce/z/MtBRoj
Copy link
Contributor

@nikic nikic left a comment

Choose a reason for hiding this comment

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

LGTM

@AZero13
Copy link
Contributor Author

AZero13 commented Jul 5, 2025

Thanks. I don't have merge permissions.

@AZero13
Copy link
Contributor Author

AZero13 commented Jul 6, 2025

@nikic

@nikic nikic merged commit 16dad11 into llvm:main Jul 7, 2025
9 checks passed
@AZero13 AZero13 deleted the xorss branch July 7, 2025 13:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

llvm:analysis Includes value tracking, cost tables and constant folding llvm:instcombine Covers the InstCombine, InstSimplify and AggressiveInstCombine passes llvm:transforms

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants