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 18 #include <string> 19 #include <vector> 20 21 namespace llvm { 22 class Init; 23 class ListInit; 24 class Record; 25 class SMLoc; 26 } // end namespace llvm 27 28 namespace mlir { 29 namespace tblgen { 30 31 // A logical predicate. This class must closely follow the definition of 32 // TableGen class 'Pred'. 33 class Pred { 34 public: 35 // Constructs the null Predicate (e.g., always true). Pred()36 explicit Pred() : def(nullptr) {} 37 // Construct a Predicate from a record. 38 explicit Pred(const llvm::Record *record); 39 // Construct a Predicate from an initializer. 40 explicit Pred(const llvm::Init *init); 41 42 // Check if the predicate is defined. Callers may use this to interpret the 43 // missing predicate as either true (e.g. in filters) or false (e.g. in 44 // precondition verification). isNull()45 bool isNull() const { return def == nullptr; } 46 47 // Get the predicate condition. This may dispatch to getConditionImpl() of 48 // the underlying predicate type. 49 std::string getCondition() const; 50 51 // Whether the predicate is a combination of other predicates, i.e. an 52 // record of type CombinedPred. 53 bool isCombined() const; 54 55 // Records are pointer-comparable. 56 bool operator==(const Pred &other) const { return def == other.def; } 57 58 // Get the location of the predicate. 59 ArrayRef<llvm::SMLoc> getLoc() const; 60 61 protected: 62 // The TableGen definition of this predicate. 63 const llvm::Record *def; 64 }; 65 66 // A logical predicate wrapping a C expression. This class must closely follow 67 // the definition of TableGen class 'CPred'. 68 class CPred : public Pred { 69 public: 70 // Construct a CPred from a record. 71 explicit CPred(const llvm::Record *record); 72 // Construct a CPred an initializer. 73 explicit CPred(const llvm::Init *init); 74 75 // Get the predicate condition. 76 std::string getConditionImpl() const; 77 }; 78 79 // A logical predicate that is a combination of other predicates. This class 80 // must closely follow the definition of TableGen class 'CombinedPred'. 81 class CombinedPred : public Pred { 82 public: 83 // Construct a CombinedPred from a record. 84 explicit CombinedPred(const llvm::Record *record); 85 // Construct a CombinedPred from an initializer. 86 explicit CombinedPred(const llvm::Init *init); 87 88 // Get the predicate condition. 89 std::string getConditionImpl() const; 90 91 // Get the definition of the combiner used in this predicate. 92 const llvm::Record *getCombinerDef() const; 93 94 // Get the predicates that are combined by this predicate. 95 const std::vector<llvm::Record *> getChildren() const; 96 }; 97 98 // A combined predicate that requires all child predicates of 'CPred' type to 99 // have their expression rewritten with a simple string substitution rule. 100 class SubstLeavesPred : public CombinedPred { 101 public: 102 // Get the replacement pattern. 103 StringRef getPattern() const; 104 // Get the string used to replace the pattern. 105 StringRef getReplacement() const; 106 }; 107 108 // A combined predicate that prepends a prefix and appends a suffix to the 109 // predicate string composed from a child predicate. 110 class ConcatPred : public CombinedPred { 111 public: 112 StringRef getPrefix() const; 113 StringRef getSuffix() const; 114 }; 115 116 } // end namespace tblgen 117 } // end namespace mlir 118 119 #endif // MLIR_TABLEGEN_PREDICATE_H_ 120