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.
52 CheckProfitableToFold,
58 MatcherNode(KindTy K) : Kind(K) {}
60 virtual ~MatcherNode() {}
62 KindTy getKind() const { return Kind; }
65 static inline bool classof(const MatcherNode *) { return true; }
67 virtual void print(raw_ostream &OS, unsigned indent = 0) const = 0;
71 /// EmitNodeMatcherNode - This signals a successful match and generates a node.
72 class EmitNodeMatcherNode : public MatcherNode {
73 const PatternToMatch &Pattern;
75 EmitNodeMatcherNode(const PatternToMatch &pattern)
76 : MatcherNode(EmitNode), Pattern(pattern) {}
78 const PatternToMatch &getPattern() const { return Pattern; }
80 static inline bool classof(const MatcherNode *N) {
81 return N->getKind() == EmitNode;
84 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
87 /// MatcherNodeWithChild - Every node accept the final accept state has a child
88 /// that is executed after the node runs. This class captures this commonality.
89 class MatcherNodeWithChild : public MatcherNode {
90 OwningPtr<MatcherNode> Child;
92 MatcherNodeWithChild(KindTy K) : MatcherNode(K) {}
94 MatcherNode *getChild() { return Child.get(); }
95 const MatcherNode *getChild() const { return Child.get(); }
96 void setChild(MatcherNode *C) { Child.reset(C); }
98 static inline bool classof(const MatcherNode *N) {
99 return N->getKind() != EmitNode;
103 void printChild(raw_ostream &OS, unsigned indent) const;
106 /// PushMatcherNode - This pushes a failure scope on the stack and evaluates
107 /// 'child'. If 'child' fails to match, it pops its scope and attempts to
109 class PushMatcherNode : public MatcherNodeWithChild {
110 OwningPtr<MatcherNode> Failure;
112 PushMatcherNode(MatcherNode *child = 0, MatcherNode *failure = 0)
113 : MatcherNodeWithChild(Push), Failure(failure) {
117 MatcherNode *getFailure() { return Failure.get(); }
118 const MatcherNode *getFailure() const { return Failure.get(); }
119 void setFailure(MatcherNode *N) { Failure.reset(N); }
121 static inline bool classof(const MatcherNode *N) {
122 return N->getKind() == Push;
125 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
128 /// RecordMatcherNode - Save the current node in the operand list.
129 class RecordMatcherNode : public MatcherNodeWithChild {
131 RecordMatcherNode() : MatcherNodeWithChild(Record) {}
133 static inline bool classof(const MatcherNode *N) {
134 return N->getKind() == Record;
137 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
140 /// MoveChildMatcherNode - This tells the interpreter to move into the
141 /// specified child node.
142 class MoveChildMatcherNode : public MatcherNodeWithChild {
145 MoveChildMatcherNode(unsigned childNo)
146 : MatcherNodeWithChild(MoveChild), ChildNo(childNo) {}
148 unsigned getChildNo() const { return ChildNo; }
150 static inline bool classof(const MatcherNode *N) {
151 return N->getKind() == MoveChild;
154 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
157 /// MoveParentMatcherNode - This tells the interpreter to move to the parent
158 /// of the current node.
159 class MoveParentMatcherNode : public MatcherNodeWithChild {
161 MoveParentMatcherNode()
162 : MatcherNodeWithChild(MoveParent) {}
164 static inline bool classof(const MatcherNode *N) {
165 return N->getKind() == MoveParent;
168 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
171 /// CheckSameMatcherNode - This checks to see if this node is exactly the same
172 /// node as the specified match that was recorded with 'Record'. This is used
173 /// when patterns have the same name in them, like '(mul GPR:$in, GPR:$in)'.
174 class CheckSameMatcherNode : public MatcherNodeWithChild {
175 unsigned MatchNumber;
177 CheckSameMatcherNode(unsigned matchnumber)
178 : MatcherNodeWithChild(CheckSame), MatchNumber(matchnumber) {}
180 unsigned getMatchNumber() const { return MatchNumber; }
182 static inline bool classof(const MatcherNode *N) {
183 return N->getKind() == CheckSame;
186 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
189 /// CheckPatternPredicateMatcherNode - This checks the target-specific predicate
190 /// to see if the entire pattern is capable of matching. This predicate does
191 /// not take a node as input. This is used for subtarget feature checks etc.
192 class CheckPatternPredicateMatcherNode : public MatcherNodeWithChild {
193 std::string Predicate;
195 CheckPatternPredicateMatcherNode(StringRef predicate)
196 : MatcherNodeWithChild(CheckPatternPredicate), Predicate(predicate) {}
198 StringRef getPredicate() const { return Predicate; }
200 static inline bool classof(const MatcherNode *N) {
201 return N->getKind() == CheckPatternPredicate;
204 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
207 /// CheckPredicateMatcherNode - This checks the target-specific predicate to
208 /// see if the node is acceptable.
209 class CheckPredicateMatcherNode : public MatcherNodeWithChild {
212 CheckPredicateMatcherNode(StringRef predname)
213 : MatcherNodeWithChild(CheckPredicate), PredName(predname) {}
215 StringRef getPredicateName() const { return PredName; }
217 static inline bool classof(const MatcherNode *N) {
218 return N->getKind() == CheckPredicate;
221 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
225 /// CheckOpcodeMatcherNode - This checks to see if the current node has the
226 /// specified opcode, if not it fails to match.
227 class CheckOpcodeMatcherNode : public MatcherNodeWithChild {
228 StringRef OpcodeName;
230 CheckOpcodeMatcherNode(StringRef opcodename)
231 : MatcherNodeWithChild(CheckOpcode), OpcodeName(opcodename) {}
233 StringRef getOpcodeName() const { return OpcodeName; }
235 static inline bool classof(const MatcherNode *N) {
236 return N->getKind() == CheckOpcode;
239 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
242 /// CheckTypeMatcherNode - This checks to see if the current node has the
243 /// specified type, if not it fails to match.
244 class CheckTypeMatcherNode : public MatcherNodeWithChild {
245 MVT::SimpleValueType Type;
247 CheckTypeMatcherNode(MVT::SimpleValueType type)
248 : MatcherNodeWithChild(CheckType), Type(type) {}
250 MVT::SimpleValueType getType() const { return Type; }
252 static inline bool classof(const MatcherNode *N) {
253 return N->getKind() == CheckType;
256 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
259 /// CheckIntegerMatcherNode - This checks to see if the current node is a
260 /// ConstantSDNode with the specified integer value, if not it fails to match.
261 class CheckIntegerMatcherNode : public MatcherNodeWithChild {
264 CheckIntegerMatcherNode(int64_t value)
265 : MatcherNodeWithChild(CheckInteger), Value(value) {}
267 int64_t getValue() const { return Value; }
269 static inline bool classof(const MatcherNode *N) {
270 return N->getKind() == CheckInteger;
273 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
276 /// CheckCondCodeMatcherNode - This checks to see if the current node is a
277 /// CondCodeSDNode with the specified condition, if not it fails to match.
278 class CheckCondCodeMatcherNode : public MatcherNodeWithChild {
279 StringRef CondCodeName;
281 CheckCondCodeMatcherNode(StringRef condcodename)
282 : MatcherNodeWithChild(CheckCondCode), CondCodeName(condcodename) {}
284 StringRef getCondCodeName() const { return CondCodeName; }
286 static inline bool classof(const MatcherNode *N) {
287 return N->getKind() == CheckCondCode;
290 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
293 /// CheckValueTypeMatcherNode - This checks to see if the current node is a
294 /// VTSDNode with the specified type, if not it fails to match.
295 class CheckValueTypeMatcherNode : public MatcherNodeWithChild {
298 CheckValueTypeMatcherNode(StringRef type_name)
299 : MatcherNodeWithChild(CheckValueType), TypeName(type_name) {}
301 StringRef getTypeName() const { return TypeName; }
303 static inline bool classof(const MatcherNode *N) {
304 return N->getKind() == CheckValueType;
307 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
312 /// CheckComplexPatMatcherNode - This node runs the specified ComplexPattern on
313 /// the current node.
314 class CheckComplexPatMatcherNode : public MatcherNodeWithChild {
315 const ComplexPattern &Pattern;
317 CheckComplexPatMatcherNode(const ComplexPattern &pattern)
318 : MatcherNodeWithChild(CheckComplexPat), Pattern(pattern) {}
320 static inline bool classof(const MatcherNode *N) {
321 return N->getKind() == CheckComplexPat;
324 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
327 /// CheckAndImmMatcherNode - This checks to see if the current node is an 'and'
328 /// with something equivalent to the specified immediate.
329 class CheckAndImmMatcherNode : public MatcherNodeWithChild {
332 CheckAndImmMatcherNode(int64_t value)
333 : MatcherNodeWithChild(CheckAndImm), Value(value) {}
335 int64_t getValue() const { return Value; }
337 static inline bool classof(const MatcherNode *N) {
338 return N->getKind() == CheckAndImm;
341 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
344 /// CheckOrImmMatcherNode - This checks to see if the current node is an 'and'
345 /// with something equivalent to the specified immediate.
346 class CheckOrImmMatcherNode : public MatcherNodeWithChild {
349 CheckOrImmMatcherNode(int64_t value)
350 : MatcherNodeWithChild(CheckOrImm), Value(value) {}
352 int64_t getValue() const { return Value; }
354 static inline bool classof(const MatcherNode *N) {
355 return N->getKind() == CheckOrImm;
358 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
361 /// CheckProfitableToFoldMatcherNode - This checks to see if the current node is
362 /// worthwhile to try to fold into a large pattern.
363 class CheckProfitableToFoldMatcherNode : public MatcherNodeWithChild {
365 CheckProfitableToFoldMatcherNode()
366 : MatcherNodeWithChild(CheckProfitableToFold) {}
368 static inline bool classof(const MatcherNode *N) {
369 return N->getKind() == CheckProfitableToFold;
372 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
375 /// CheckLegalToFoldMatcherNode - This checks to see if the current node is
376 /// legal to try to fold into a large pattern.
377 class CheckLegalToFoldMatcherNode : public MatcherNodeWithChild {
379 CheckLegalToFoldMatcherNode()
380 : MatcherNodeWithChild(CheckLegalToFold) {}
382 static inline bool classof(const MatcherNode *N) {
383 return N->getKind() == CheckLegalToFold;
386 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
388 } // end namespace llvm