Skip to content

[PatternMatch] Mark various matchers const (NFC) #138834

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 2 commits into from
May 8, 2025

Conversation

artagnon
Copy link
Contributor

@artagnon artagnon commented May 7, 2025

Mark matchers const and remove an extraneous template parameter in SCEVPatternMatch. Since SCEVPatternMatch is intertwined with PatternMatch, also fix constness issues there.

Mark match functions const and remove an extraneous template parameter.
@artagnon artagnon requested review from nikic and fhahn May 7, 2025 09:23
@llvmbot
Copy link
Member

llvmbot commented May 7, 2025

@llvm/pr-subscribers-llvm-transforms
@llvm/pr-subscribers-llvm-ir

@llvm/pr-subscribers-llvm-analysis

Author: Ramkumar Ramachandra (artagnon)

Changes

Mark match functions const and remove an extraneous template parameter.


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

1 Files Affected:

  • (modified) llvm/include/llvm/Analysis/ScalarEvolutionPatternMatch.h (+12-10)
diff --git a/llvm/include/llvm/Analysis/ScalarEvolutionPatternMatch.h b/llvm/include/llvm/Analysis/ScalarEvolutionPatternMatch.h
index 900f6d0fd05ab..aa53fd62a864e 100644
--- a/llvm/include/llvm/Analysis/ScalarEvolutionPatternMatch.h
+++ b/llvm/include/llvm/Analysis/ScalarEvolutionPatternMatch.h
@@ -18,13 +18,12 @@
 namespace llvm {
 namespace SCEVPatternMatch {
 
-template <typename Val, typename Pattern>
-bool match(const SCEV *S, const Pattern &P) {
-  return P.match(S);
+template <typename Pattern> bool match(const SCEV *S, const Pattern &P) {
+  return const_cast<Pattern &>(P).match(S);
 }
 
 template <typename Predicate> struct cst_pred_ty : public Predicate {
-  bool match(const SCEV *S) {
+  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);
@@ -33,20 +32,23 @@ template <typename Predicate> struct cst_pred_ty : public Predicate {
 };
 
 struct is_zero {
-  bool isValue(const APInt &C) { return C.isZero(); }
+  bool isValue(const APInt &C) const { return C.isZero(); }
 };
+
 /// Match an integer 0.
 inline cst_pred_ty<is_zero> m_scev_Zero() { return cst_pred_ty<is_zero>(); }
 
 struct is_one {
-  bool isValue(const APInt &C) { return C.isOne(); }
+  bool isValue(const APInt &C) const { return C.isOne(); }
 };
+
 /// Match an integer 1.
 inline cst_pred_ty<is_one> m_scev_One() { return cst_pred_ty<is_one>(); }
 
 struct is_all_ones {
-  bool isValue(const APInt &C) { return C.isAllOnes(); }
+  bool isValue(const APInt &C) const { return C.isAllOnes(); }
 };
+
 /// Match an integer with all bits set.
 inline cst_pred_ty<is_all_ones> m_scev_AllOnes() {
   return cst_pred_ty<is_all_ones>();
@@ -85,7 +87,7 @@ struct specificscev_ty {
 
   specificscev_ty(const SCEV *Expr) : Expr(Expr) {}
 
-  template <typename ITy> bool match(ITy *S) { return S == Expr; }
+  template <typename ITy> bool match(ITy *S) const { return S == Expr; }
 };
 
 /// Match if we have a specific specified SCEV.
@@ -97,7 +99,7 @@ template <typename SCEVTy, typename Op0_t> struct SCEVUnaryExpr_match {
 
   SCEVUnaryExpr_match(Op0_t Op0) : Op0(Op0) {}
 
-  bool match(const SCEV *S) {
+  bool match(const SCEV *S) const {
     auto *E = dyn_cast<SCEVTy>(S);
     return E && E->getNumOperands() == 1 && Op0.match(E->getOperand(0));
   }
@@ -128,7 +130,7 @@ struct SCEVBinaryExpr_match {
 
   SCEVBinaryExpr_match(Op0_t Op0, Op1_t Op1) : Op0(Op0), Op1(Op1) {}
 
-  bool match(const SCEV *S) {
+  bool match(const SCEV *S) const {
     auto *E = dyn_cast<SCEVTy>(S);
     return E && E->getNumOperands() == 2 && Op0.match(E->getOperand(0)) &&
            Op1.match(E->getOperand(1));

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.

It this needed for #138836?

@artagnon
Copy link
Contributor Author

artagnon commented May 7, 2025

It this needed for #138836?

Yes, it is needed to actually use #138836.

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

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.

LGTM, very nice to get the const_casts cleaned up! Might be worth to generalize the title to something like [PatternMatch] Mark various matchers as const. (NFC)

@artagnon artagnon changed the title [SCEVPatternMatch] Fix constness issues [PatternMatch] Mark various matchers const (NFC May 8, 2025
@artagnon artagnon changed the title [PatternMatch] Mark various matchers const (NFC [PatternMatch] Mark various matchers const (NFC) May 8, 2025
@artagnon artagnon merged commit 4eebc8d into llvm:main May 8, 2025
13 checks passed
@artagnon artagnon deleted the scevpm-constness branch May 8, 2025 10:24
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