1 //===- DAGISelMatcher.h - Representation of DAG pattern matcher -----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef TBLGEN_DAGISELMATCHER_H
11 #define TBLGEN_DAGISELMATCHER_H
13 #include "llvm/ADT/OwningPtr.h"
14 #include "llvm/ADT/StringRef.h"
15 #include "llvm/CodeGen/ValueTypes.h"
18 class CodeGenDAGPatterns;
24 MatcherNode *ConvertPatternToMatcher(const PatternToMatch &Pattern,
25 const CodeGenDAGPatterns &CGP);
27 void EmitMatcherTable(const MatcherNode *Matcher, raw_ostream &OS);
30 /// MatcherNode - Base class for all the the DAG ISel Matcher representation
36 Push, // [Push, Dest0, Dest1, Dest2, Dest3]
38 MoveChild, // [MoveChild, Child#]
39 MoveParent, // [MoveParent]
41 CheckSame, // [CheckSame, N] Fail if not same as prev match.
42 CheckPatternPredicate,
43 CheckPredicate, // [CheckPredicate, P] Fail if predicate fails.
44 CheckOpcode, // [CheckOpcode, Opcode] Fail if not opcode.
45 CheckType, // [CheckType, MVT] Fail if not correct type.
46 CheckInteger, // [CheckInteger, int0,int1,int2,...int7] Fail if wrong val.
47 CheckCondCode, // [CheckCondCode, CondCode] Fail if not condcode.
56 MatcherNode(KindTy K) : Kind(K) {}
58 virtual ~MatcherNode() {}
60 KindTy getKind() const { return Kind; }
63 static inline bool classof(const MatcherNode *) { return true; }
65 virtual void print(raw_ostream &OS, unsigned indent = 0) const = 0;
69 /// EmitNodeMatcherNode - This signals a successful match and generates a node.
70 class EmitNodeMatcherNode : public MatcherNode {
71 const PatternToMatch &Pattern;
73 EmitNodeMatcherNode(const PatternToMatch &pattern)
74 : MatcherNode(EmitNode), Pattern(pattern) {}
76 const PatternToMatch &getPattern() const { return Pattern; }
78 static inline bool classof(const MatcherNode *N) {
79 return N->getKind() == EmitNode;
82 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
85 /// MatcherNodeWithChild - Every node accept the final accept state has a child
86 /// that is executed after the node runs. This class captures this commonality.
87 class MatcherNodeWithChild : public MatcherNode {
88 OwningPtr<MatcherNode> Child;
90 MatcherNodeWithChild(KindTy K) : MatcherNode(K) {}
92 MatcherNode *getChild() { return Child.get(); }
93 const MatcherNode *getChild() const { return Child.get(); }
94 void setChild(MatcherNode *C) { Child.reset(C); }
96 static inline bool classof(const MatcherNode *N) {
97 return N->getKind() != EmitNode;
101 void printChild(raw_ostream &OS, unsigned indent) const;
104 /// PushMatcherNode - This pushes a failure scope on the stack and evaluates
105 /// 'child'. If 'child' fails to match, it pops its scope and attempts to
107 class PushMatcherNode : public MatcherNodeWithChild {
108 OwningPtr<MatcherNode> Failure;
110 PushMatcherNode(MatcherNode *child = 0, MatcherNode *failure = 0)
111 : MatcherNodeWithChild(Push), Failure(failure) {
115 MatcherNode *getFailure() { return Failure.get(); }
116 const MatcherNode *getFailure() const { return Failure.get(); }
117 void setFailure(MatcherNode *N) { Failure.reset(N); }
119 static inline bool classof(const MatcherNode *N) {
120 return N->getKind() == Push;
123 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
126 /// RecordMatcherNode - Save the current node in the operand list.
127 class RecordMatcherNode : public MatcherNodeWithChild {
129 RecordMatcherNode() : MatcherNodeWithChild(Record) {}
131 static inline bool classof(const MatcherNode *N) {
132 return N->getKind() == Record;
135 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
138 /// MoveChildMatcherNode - This tells the interpreter to move into the
139 /// specified child node.
140 class MoveChildMatcherNode : public MatcherNodeWithChild {
143 MoveChildMatcherNode(unsigned childNo)
144 : MatcherNodeWithChild(MoveChild), ChildNo(childNo) {}
146 unsigned getChildNo() const { return ChildNo; }
148 static inline bool classof(const MatcherNode *N) {
149 return N->getKind() == MoveChild;
152 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
155 /// MoveParentMatcherNode - This tells the interpreter to move to the parent
156 /// of the current node.
157 class MoveParentMatcherNode : public MatcherNodeWithChild {
159 MoveParentMatcherNode()
160 : MatcherNodeWithChild(MoveParent) {}
162 static inline bool classof(const MatcherNode *N) {
163 return N->getKind() == MoveParent;
166 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
169 /// CheckSameMatcherNode - This checks to see if this node is exactly the same
170 /// node as the specified match that was recorded with 'Record'. This is used
171 /// when patterns have the same name in them, like '(mul GPR:$in, GPR:$in)'.
172 class CheckSameMatcherNode : public MatcherNodeWithChild {
173 unsigned MatchNumber;
175 CheckSameMatcherNode(unsigned matchnumber)
176 : MatcherNodeWithChild(CheckSame), MatchNumber(matchnumber) {}
178 unsigned getMatchNumber() const { return MatchNumber; }
180 static inline bool classof(const MatcherNode *N) {
181 return N->getKind() == CheckSame;
184 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
187 /// CheckPatternPredicateMatcherNode - This checks the target-specific predicate
188 /// to see if the entire pattern is capable of matching. This predicate does
189 /// not take a node as input. This is used for subtarget feature checks etc.
190 class CheckPatternPredicateMatcherNode : public MatcherNodeWithChild {
191 std::string Predicate;
193 CheckPatternPredicateMatcherNode(StringRef predicate)
194 : MatcherNodeWithChild(CheckPatternPredicate), Predicate(predicate) {}
196 StringRef getPredicate() const { return Predicate; }
198 static inline bool classof(const MatcherNode *N) {
199 return N->getKind() == CheckPatternPredicate;
202 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
205 /// CheckPredicateMatcherNode - This checks the target-specific predicate to
206 /// see if the node is acceptable.
207 class CheckPredicateMatcherNode : public MatcherNodeWithChild {
210 CheckPredicateMatcherNode(StringRef predname)
211 : MatcherNodeWithChild(CheckPredicate), PredName(predname) {}
213 StringRef getPredicateName() const { return PredName; }
215 static inline bool classof(const MatcherNode *N) {
216 return N->getKind() == CheckPredicate;
219 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
223 /// CheckOpcodeMatcherNode - This checks to see if the current node has the
224 /// specified opcode, if not it fails to match.
225 class CheckOpcodeMatcherNode : public MatcherNodeWithChild {
226 StringRef OpcodeName;
228 CheckOpcodeMatcherNode(StringRef opcodename)
229 : MatcherNodeWithChild(CheckOpcode), OpcodeName(opcodename) {}
231 StringRef getOpcodeName() const { return OpcodeName; }
233 static inline bool classof(const MatcherNode *N) {
234 return N->getKind() == CheckOpcode;
237 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
240 /// CheckTypeMatcherNode - This checks to see if the current node has the
241 /// specified type, if not it fails to match.
242 class CheckTypeMatcherNode : public MatcherNodeWithChild {
243 MVT::SimpleValueType Type;
245 CheckTypeMatcherNode(MVT::SimpleValueType type)
246 : MatcherNodeWithChild(CheckType), Type(type) {}
248 MVT::SimpleValueType getType() const { return Type; }
250 static inline bool classof(const MatcherNode *N) {
251 return N->getKind() == CheckType;
254 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
257 /// CheckIntegerMatcherNode - This checks to see if the current node is a
258 /// ConstantSDNode with the specified integer value, if not it fails to match.
259 class CheckIntegerMatcherNode : public MatcherNodeWithChild {
262 CheckIntegerMatcherNode(int64_t value)
263 : MatcherNodeWithChild(CheckInteger), Value(value) {}
265 int64_t getValue() const { return Value; }
267 static inline bool classof(const MatcherNode *N) {
268 return N->getKind() == CheckInteger;
271 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
274 /// CheckCondCodeMatcherNode - This checks to see if the current node is a
275 /// CondCodeSDNode with the specified condition, if not it fails to match.
276 class CheckCondCodeMatcherNode : public MatcherNodeWithChild {
277 StringRef CondCodeName;
279 CheckCondCodeMatcherNode(StringRef condcodename)
280 : MatcherNodeWithChild(CheckCondCode), CondCodeName(condcodename) {}
282 StringRef getCondCodeName() const { return CondCodeName; }
284 static inline bool classof(const MatcherNode *N) {
285 return N->getKind() == CheckCondCode;
288 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
291 /// CheckValueTypeMatcherNode - This checks to see if the current node is a
292 /// VTSDNode with the specified type, if not it fails to match.
293 class CheckValueTypeMatcherNode : public MatcherNodeWithChild {
296 CheckValueTypeMatcherNode(StringRef type_name)
297 : MatcherNodeWithChild(CheckValueType), TypeName(type_name) {}
299 StringRef getTypeName() const { return TypeName; }
301 static inline bool classof(const MatcherNode *N) {
302 return N->getKind() == CheckValueType;
305 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
310 /// CheckComplexPatMatcherNode - This node runs the specified ComplexPattern on
311 /// the current node.
312 class CheckComplexPatMatcherNode : public MatcherNodeWithChild {
313 const ComplexPattern &Pattern;
315 CheckComplexPatMatcherNode(const ComplexPattern &pattern)
316 : MatcherNodeWithChild(CheckComplexPat), Pattern(pattern) {}
318 static inline bool classof(const MatcherNode *N) {
319 return N->getKind() == CheckComplexPat;
322 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
325 /// CheckAndImmMatcherNode - This checks to see if the current node is an 'and'
326 /// with something equivalent to the specified immediate.
327 class CheckAndImmMatcherNode : public MatcherNodeWithChild {
330 CheckAndImmMatcherNode(int64_t value)
331 : MatcherNodeWithChild(CheckAndImm), Value(value) {}
333 int64_t getValue() const { return Value; }
335 static inline bool classof(const MatcherNode *N) {
336 return N->getKind() == CheckAndImm;
339 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
342 /// CheckOrImmMatcherNode - This checks to see if the current node is an 'and'
343 /// with something equivalent to the specified immediate.
344 class CheckOrImmMatcherNode : public MatcherNodeWithChild {
347 CheckOrImmMatcherNode(int64_t value)
348 : MatcherNodeWithChild(CheckOrImm), Value(value) {}
350 int64_t getValue() const { return Value; }
352 static inline bool classof(const MatcherNode *N) {
353 return N->getKind() == CheckOrImm;
356 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
360 } // end namespace llvm