Skip to content

[SCEVPatternMatch] Extend with more matchers #138836

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
merged 4 commits into from
May 9, 2025
Merged

Conversation

artagnon
Copy link
Contributor

@artagnon artagnon commented May 7, 2025

No description provided.

@artagnon artagnon requested review from nikic and fhahn May 7, 2025 09:27
@llvmbot
Copy link
Member

llvmbot commented May 7, 2025

@llvm/pr-subscribers-llvm-analysis

Author: Ramkumar Ramachandra (artagnon)

Changes

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

1 Files Affected:

  • (modified) llvm/include/llvm/Analysis/ScalarEvolutionPatternMatch.h (+40)
diff --git a/llvm/include/llvm/Analysis/ScalarEvolutionPatternMatch.h b/llvm/include/llvm/Analysis/ScalarEvolutionPatternMatch.h
index 900f6d0fd05ab..a073f45423dea 100644
--- a/llvm/include/llvm/Analysis/ScalarEvolutionPatternMatch.h
+++ b/llvm/include/llvm/Analysis/ScalarEvolutionPatternMatch.h
@@ -56,6 +56,16 @@ template <typename Class> struct class_match {
   template <typename ITy> bool match(ITy *V) const { return isa<Class>(V); }
 };
 
+inline class_match<const SCEV> m_SCEV() { return class_match<const SCEV>(); }
+
+inline class_match<const SCEVConstant> m_SCEVConstant() {
+  return class_match<const SCEVConstant>();
+}
+
+inline class_match<const SCEVUnknown> m_SCEVUnknown() {
+  return class_match<const SCEVUnknown>();
+}
+
 template <typename Class> struct bind_ty {
   Class *&VR;
 
@@ -91,6 +101,25 @@ struct specificscev_ty {
 /// Match if we have a specific specified SCEV.
 inline specificscev_ty m_Specific(const SCEV *S) { return S; }
 
+template <typename Op0_t> struct cst_match {
+  Op0_t Op0;
+
+  cst_match(Op0_t Op0) : Op0(Op0) {}
+
+  bool match(const SCEV *S) const {
+    assert((isa<SCEVCouldNotCompute>(S) || !S->getType()->isVectorTy()) &&
+           "no vector types expected from SCEVs");
+    auto *C = dyn_cast<SCEVConstant>(S);
+    return C && C->getAPInt() == Op0;
+  }
+};
+
+/// Match an SCEV constant with an APInt.
+inline cst_match<APInt> m_SCEVConstant(const APInt &V) { return V; }
+
+/// Match an SCEV constant with a plain integer.
+inline cst_match<uint64_t> m_SCEVConstant(uint64_t V) { return V; }
+
 /// Match a unary SCEV.
 template <typename SCEVTy, typename Op0_t> struct SCEVUnaryExpr_match {
   Op0_t Op0;
@@ -147,6 +176,17 @@ m_scev_Add(const Op0_t &Op0, const Op1_t &Op1) {
   return m_scev_Binary<SCEVAddExpr>(Op0, Op1);
 }
 
+template <typename Op0_t, typename Op1_t>
+inline SCEVBinaryExpr_match<SCEVMulExpr, Op0_t, Op1_t>
+m_scev_Mul(const Op0_t &Op0, const Op1_t &Op1) {
+  return m_scev_Binary<SCEVMulExpr>(Op0, Op1);
+}
+
+template <typename Op0_t, typename Op1_t>
+inline SCEVBinaryExpr_match<SCEVUDivExpr, Op0_t, Op1_t>
+m_scev_UDiv(const Op0_t &Op0, const Op1_t &Op1) {
+  return m_scev_Binary<SCEVUDivExpr>(Op0, Op1);
+}
 } // namespace SCEVPatternMatch
 } // namespace llvm
 

Copy link
Contributor

@fhahn fhahn left a comment

Choose a reason for hiding this comment

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

This should probably at least contain changes to use each new matcher, to make sure the instantiations work as expected

@artagnon
Copy link
Contributor Author

artagnon commented May 7, 2025

This should probably at least contain changes to use each new matcher, to make sure the instantiations work as expected

To actually use this, I first require #138834 merged.

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.

This looks ok, but I think the naming needs more consideration. I think the general intention is to use the same names as PatternMatch but with an m_scev prefix, so I left suggestions accordingly.

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

@artagnon artagnon merged commit 4589911 into llvm:main May 9, 2025
8 of 11 checks passed
@artagnon artagnon deleted the scevpm-extend branch May 9, 2025 08:20
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