1 | //===- Predicate.h - Predicate class ----------------------------*- C++ -*-===// |
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | // |
9 | // Wrapper around predicates defined in TableGen. |
10 | // |
11 | //===----------------------------------------------------------------------===// |
12 | |
13 | #ifndef MLIR_TABLEGEN_PREDICATE_H_ |
14 | #define MLIR_TABLEGEN_PREDICATE_H_ |
15 | |
16 | #include "mlir/Support/LLVM.h" |
17 | #include "llvm/ADT/Hashing.h" |
18 | |
19 | #include <string> |
20 | #include <vector> |
21 | |
22 | namespace llvm { |
23 | class Init; |
24 | class ListInit; |
25 | class Record; |
26 | class SMLoc; |
27 | } // namespace llvm |
28 | |
29 | namespace mlir { |
30 | namespace tblgen { |
31 | |
32 | // A logical predicate. This class must closely follow the definition of |
33 | // TableGen class 'Pred'. |
34 | class Pred { |
35 | public: |
36 | // Constructs the null Predicate (e.g., always true). |
37 | explicit Pred() {} |
38 | // Construct a Predicate from a record. |
39 | explicit Pred(const llvm::Record *record); |
40 | // Construct a Predicate from an initializer. |
41 | explicit Pred(const llvm::Init *init); |
42 | |
43 | // Check if the predicate is defined. Callers may use this to interpret the |
44 | // missing predicate as either true (e.g. in filters) or false (e.g. in |
45 | // precondition verification). |
46 | bool isNull() const { return def == nullptr; } |
47 | |
48 | // Get the predicate condition. This may dispatch to getConditionImpl() of |
49 | // the underlying predicate type. |
50 | std::string getCondition() const; |
51 | |
52 | // Whether the predicate is a combination of other predicates, i.e. an |
53 | // record of type CombinedPred. |
54 | bool isCombined() const; |
55 | |
56 | // Get the location of the predicate. |
57 | ArrayRef<SMLoc> getLoc() const; |
58 | |
59 | // Records are pointer-comparable. |
60 | bool operator==(const Pred &other) const { return def == other.def; } |
61 | |
62 | // Return true if the predicate is not null. |
63 | operator bool() const { return def; } |
64 | |
65 | // Hash a predicate by its pointer value. |
66 | friend llvm::hash_code hash_value(Pred pred) { |
67 | return llvm::hash_value(ptr: pred.def); |
68 | } |
69 | |
70 | /// Return the underlying def. |
71 | const llvm::Record &getDef() const { return *def; } |
72 | |
73 | protected: |
74 | // The TableGen definition of this predicate. |
75 | const llvm::Record *def{nullptr}; |
76 | }; |
77 | |
78 | // A logical predicate wrapping a C expression. This class must closely follow |
79 | // the definition of TableGen class 'CPred'. |
80 | class CPred : public Pred { |
81 | public: |
82 | // Construct a CPred from a record. |
83 | explicit CPred(const llvm::Record *record); |
84 | // Construct a CPred an initializer. |
85 | explicit CPred(const llvm::Init *init); |
86 | |
87 | // Get the predicate condition. |
88 | std::string getConditionImpl() const; |
89 | }; |
90 | |
91 | // A logical predicate that is a combination of other predicates. This class |
92 | // must closely follow the definition of TableGen class 'CombinedPred'. |
93 | class CombinedPred : public Pred { |
94 | public: |
95 | // Construct a CombinedPred from a record. |
96 | explicit CombinedPred(const llvm::Record *record); |
97 | // Construct a CombinedPred from an initializer. |
98 | explicit CombinedPred(const llvm::Init *init); |
99 | |
100 | // Get the predicate condition. |
101 | std::string getConditionImpl() const; |
102 | |
103 | // Get the definition of the combiner used in this predicate. |
104 | const llvm::Record *getCombinerDef() const; |
105 | |
106 | // Get the predicates that are combined by this predicate. |
107 | std::vector<llvm::Record *> getChildren() const; |
108 | }; |
109 | |
110 | // A combined predicate that requires all child predicates of 'CPred' type to |
111 | // have their expression rewritten with a simple string substitution rule. |
112 | class SubstLeavesPred : public CombinedPred { |
113 | public: |
114 | // Get the replacement pattern. |
115 | StringRef getPattern() const; |
116 | // Get the string used to replace the pattern. |
117 | StringRef getReplacement() const; |
118 | }; |
119 | |
120 | // A combined predicate that prepends a prefix and appends a suffix to the |
121 | // predicate string composed from a child predicate. |
122 | class ConcatPred : public CombinedPred { |
123 | public: |
124 | StringRef getPrefix() const; |
125 | StringRef getSuffix() const; |
126 | }; |
127 | |
128 | } // namespace tblgen |
129 | } // namespace mlir |
130 | |
131 | #endif // MLIR_TABLEGEN_PREDICATE_H_ |
132 | |