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

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Wed Dec 11 04:06:43 PST 2024


================
@@ -0,0 +1,73 @@
+//===----------------------------------------------------------------------===//
+// 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) {
+    auto *C = dyn_cast<SCEVConstant>(S);
+    return C && this->isValue(C->getAPInt());
+  }
+};
+
+struct is_zero_int {
+  bool isValue(const APInt &C) { return C.isZero(); }
+};
+
+/// Match an integer 0 or a vector with all elements equal to 0.
+/// For vectors, this includes constants with undefined elements.
----------------
nikic wrote:

```suggestion
/// Match an integer 0.
```
SCEV does not operate on vectors at all.

Also, I don't think we really want the Zero/ZeroInt distinction in SCEV? If we really wanted to match "0 or null" we'd have to check for a SCEVUnknown with a null pointer constant, but I don't think this is something that we'd want to do when working on SCEV.

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


More information about the llvm-commits mailing list