[llvm] [SCEV] Add initial pattern matching for SCEV constants. (NFC) (PR #119389)

Florian Hahn via llvm-commits llvm-commits at lists.llvm.org
Wed Dec 11 12:47:15 PST 2024


================
@@ -0,0 +1,63 @@
+//===----------------------------------------------------------------------===//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// This file provides a simple and efficient mechanism for performing general
+// tree-based pattern matches on SCEVs, based on LLVM's IR pattern matchers.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_ANALYSIS_SCALAREVOLUTIONPATTERNMATCH_H
+#define LLVM_ANALYSIS_SCALAREVOLUTIONPATTERNMATCH_H
+
+#include "llvm/Analysis/ScalarEvolutionExpressions.h"
+
+namespace llvm {
+namespace SCEVPatternMatch {
+
+template <typename Val, typename Pattern>
+bool match(const SCEV *S, const Pattern &P) {
+  return P.match(S);
+}
+
+template <typename Predicate> struct cst_pred_ty : public Predicate {
+  bool match(const SCEV *S) {
+    assert((isa<SCEVCouldNotCompute>(S) || !S->getType()->isVectorTy()) &&
+           "no vector types expected from SCEVs");
+    auto *C = dyn_cast<SCEVConstant>(S);
+    return C && this->isValue(C->getAPInt());
+  }
+};
+
+struct is_zero {
+  template <typename ITy> bool match(ITy *S) {
+    assert((isa<SCEVCouldNotCompute>(S) || !S->getType()->isVectorTy()) &&
+           "no vector types expected from SCEVs");
+    auto *C = dyn_cast<SCEVConstant>(S);
+    return C && C->getValue()->isNullValue();
+  }
+};
+/// Match any null constant.
+inline is_zero m_scev_Zero() { return is_zero(); }
----------------
fhahn wrote:

Updated, thanks

https://github.com/llvm/llvm-project/pull/119389


More information about the llvm-commits mailing list