Skip to content

NaryReassociate: Check pattern before user scan #134587

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

arsenm
Copy link
Contributor

@arsenm arsenm commented Apr 7, 2025

No description provided.

Copy link
Contributor Author

arsenm commented Apr 7, 2025

This stack of pull requests is managed by Graphite. Learn more about stacking.

@arsenm arsenm marked this pull request as ready for review April 7, 2025 07:49
@llvmbot
Copy link
Member

llvmbot commented Apr 7, 2025

@llvm/pr-subscribers-llvm-transforms

Author: Matt Arsenault (arsenm)

Changes

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

1 Files Affected:

  • (modified) llvm/lib/Transforms/Scalar/NaryReassociate.cpp (+6-6)
diff --git a/llvm/lib/Transforms/Scalar/NaryReassociate.cpp b/llvm/lib/Transforms/Scalar/NaryReassociate.cpp
index 3b3f32a0ea591..ec17443b37143 100644
--- a/llvm/lib/Transforms/Scalar/NaryReassociate.cpp
+++ b/llvm/lib/Transforms/Scalar/NaryReassociate.cpp
@@ -611,15 +611,15 @@ Value *NaryReassociatePass::tryReassociateMinOrMax(Instruction *I,
   Value *A = nullptr, *B = nullptr;
   MaxMinT m_MaxMin(m_Value(A), m_Value(B));
 
+  if (!match(LHS, m_MaxMin))
+    return nullptr;
+
   if (LHS->hasNUsesOrMore(3) ||
       // The optimization is profitable only if LHS can be removed in the end.
       // In other words LHS should be used (directly or indirectly) by I only.
-      llvm::any_of(LHS->users(),
-                    [&](auto *U) {
-                      return U != I &&
-                             !(U->hasOneUser() && *U->users().begin() == I);
-                    }) ||
-      !match(LHS, m_MaxMin))
+      llvm::any_of(LHS->users(), [&](auto *U) {
+        return U != I && !(U->hasOneUser() && *U->users().begin() == I);
+      }))
     return nullptr;
 
   auto tryCombination = [&](Value *A, const SCEV *AExpr, Value *B,

@arsenm arsenm merged commit 4a425a4 into main Apr 7, 2025
15 checks passed
@arsenm arsenm deleted the users/arsenm/nary-reassociate/check-pattern-before-user-scan branch April 7, 2025 08:25
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