Skip to content

[ValueTracking] Remove opcode whitelist from matchSimpleRecurrence. #144031

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

Merged

Conversation

rj-jesus
Copy link
Contributor

Note: I'm not sure how to test this, but #143878 is affected by this change.

Note: I'm not sure how this can be tested, but llvm#143878 is affected by
this change.
@llvmbot
Copy link
Member

llvmbot commented Jun 13, 2025

@llvm/pr-subscribers-llvm-analysis

Author: Ricardo Jesus (rj-jesus)

Changes

Note: I'm not sure how to test this, but #143878 is affected by this change.


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

1 Files Affected:

  • (modified) llvm/lib/Analysis/ValueTracking.cpp (+3-1)
diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index 0a460786d00ea..a9edb02440b5e 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -9072,7 +9072,7 @@ bool llvm::matchSimpleRecurrence(const PHINode *P, BinaryOperator *&BO,
     switch (Opcode) {
     default:
       continue;
-    // TODO: Expand list -- xor, gep, uadd.sat etc.
+    // TODO: Expand list -- gep, uadd.sat etc.
     case Instruction::LShr:
     case Instruction::AShr:
     case Instruction::Shl:
@@ -9082,7 +9082,9 @@ bool llvm::matchSimpleRecurrence(const PHINode *P, BinaryOperator *&BO,
     case Instruction::URem:
     case Instruction::And:
     case Instruction::Or:
+    case Instruction::Xor:
     case Instruction::Mul:
+    case Instruction::FAdd:
     case Instruction::FMul: {
       Value *LL = LU->getOperand(0);
       Value *LR = LU->getOperand(1);

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.

Should we just remove the opcode white list entirely? Ultimately the caller has to decide which opcodes it supports, so I'm not sure this list inside matchSimpleRecurrence really has value.

@rj-jesus
Copy link
Contributor Author

Thanks, that makes sense to me. Removing the whitelist leads to an ICE on:

LLVM :: Analysis/HashRecognize/cyclic-redundancy-check.ll

Presumably due to an unhandled opcode in llvm/lib/Analysis/HashRecognize.cpp (FDIV it seems).

This seems to sort it:

diff --git a/llvm/lib/Analysis/HashRecognize.cpp b/llvm/lib/Analysis/HashRecognize.cpp
index b245548dea6d..cf172f6871cf 100644
--- a/llvm/lib/Analysis/HashRecognize.cpp
+++ b/llvm/lib/Analysis/HashRecognize.cpp
@@ -580,6 +580,8 @@ HashRecognize::recognizeCRC() const {
   if (!ByteOrderSwapped)
     return "Loop with non-unit bitshifts";
   if (SimpleRecurrence) {
+    if (!SE.isSCEVable(SimpleRecurrence.BO->getType()))
+      return "Type is not SCEVable";
     if (isBigEndianBitShift(SE.getSCEV(SimpleRecurrence.BO)) !=
         ByteOrderSwapped)
       return "Loop with non-unit bitshifts";

Does that look like a reasonable change?
CC @artagnon

@nikic
Copy link
Contributor

nikic commented Jun 13, 2025

@rj-jesus I'd check for integer specifically instead of isSCEVable. I'd also move the getSCEV call into isBigEndianBitShift to cover both call-sites. (Generally the use of SCEV for that check seems a bit questionable, but that's unrelated to this patch).

Note: This also fixes a latent bug in HashRecognize due to unhandled
opcodes.
@rj-jesus
Copy link
Contributor Author

@nikic Thanks very much, I've made the changes you suggested, including removing the whitelist from matchSimpleRecurrence. I've also added a new test to llvm/test/Analysis/HashRecognize/cyclic-redundancy-check.ll since with the new SCEV changes we no longer exercise the code path that the previously failing test covered.

Please let me know if you have any other suggestions or comments. :)

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

@rj-jesus rj-jesus changed the title [ValueTracking] Handle FADD and XOR in matchSimpleRecurrence. [ValueTracking] Remove opcode whitelist from matchSimpleRecurrence. Jun 13, 2025
Copy link
Contributor

@artagnon artagnon left a comment

Choose a reason for hiding this comment

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

LGTM, thanks!

@rj-jesus rj-jesus merged commit cca454b into llvm:main Jun 16, 2025
7 checks passed
@rj-jesus rj-jesus deleted the rjj/enable-match-simple-recurrence-fadd-xor branch June 16, 2025 08:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants