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/CodeGen/ValueTypes.h"
14 #include "llvm/ADT/OwningPtr.h"
15 #include "llvm/ADT/StringRef.h"
16 #include "llvm/Support/Casting.h"
19 class CodeGenDAGPatterns;
25 MatcherNode *ConvertPatternToMatcher(const PatternToMatch &Pattern,
26 const CodeGenDAGPatterns &CGP);
28 void EmitMatcherTable(const MatcherNode *Matcher, raw_ostream &OS);
31 /// MatcherNode - Base class for all the the DAG ISel Matcher representation
37 Push, // [Push, Dest0, Dest1, Dest2, Dest3]
39 MoveChild, // [MoveChild, Child#]
40 MoveParent, // [MoveParent]
42 CheckSame, // [CheckSame, N] Fail if not same as prev match.
43 CheckPatternPredicate,
44 CheckPredicate, // [CheckPredicate, P] Fail if predicate fails.
45 CheckOpcode, // [CheckOpcode, Opcode] Fail if not opcode.
46 CheckType, // [CheckType, MVT] Fail if not correct type.
47 CheckInteger, // [CheckInteger, int0,int1,int2,...int7] Fail if wrong val.
48 CheckCondCode, // [CheckCondCode, CondCode] Fail if not condcode.
53 CheckProfitableToFold,
59 MatcherNode(KindTy K) : Kind(K) {}
61 virtual ~MatcherNode() {}
63 KindTy getKind() const { return Kind; }
66 static inline bool classof(const MatcherNode *) { return true; }
68 virtual void print(raw_ostream &OS, unsigned indent = 0) const = 0;
72 /// EmitNodeMatcherNode - This signals a successful match and generates a node.
73 class EmitNodeMatcherNode : public MatcherNode {
74 const PatternToMatch &Pattern;
76 EmitNodeMatcherNode(const PatternToMatch &pattern)
77 : MatcherNode(EmitNode), Pattern(pattern) {}
79 const PatternToMatch &getPattern() const { return Pattern; }
81 static inline bool classof(const MatcherNode *N) {
82 return N->getKind() == EmitNode;
85 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
88 /// MatcherNodeWithChild - Every node accept the final accept state has a child
89 /// that is executed after the node runs. This class captures this commonality.
90 class MatcherNodeWithChild : public MatcherNode {
91 OwningPtr<MatcherNode> Child;
93 MatcherNodeWithChild(KindTy K) : MatcherNode(K) {}
95 MatcherNode *getChild() { return Child.get(); }
96 const MatcherNode *getChild() const { return Child.get(); }
97 void setChild(MatcherNode *C) { Child.reset(C); }
99 static inline bool classof(const MatcherNode *N) {
100 return N->getKind() != EmitNode;
104 void printChild(raw_ostream &OS, unsigned indent) const;
107 /// PushMatcherNode - This pushes a failure scope on the stack and evaluates
108 /// 'child'. If 'child' fails to match, it pops its scope and attempts to
110 class PushMatcherNode : public MatcherNodeWithChild {
111 OwningPtr<MatcherNode> Failure;
113 PushMatcherNode(MatcherNode *child = 0, MatcherNode *failure = 0)
114 : MatcherNodeWithChild(Push), Failure(failure) {
118 MatcherNode *getFailure() { return Failure.get(); }
119 const MatcherNode *getFailure() const { return Failure.get(); }
120 void setFailure(MatcherNode *N) { Failure.reset(N); }
122 static inline bool classof(const MatcherNode *N) {
123 return N->getKind() == Push;
126 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
129 /// RecordMatcherNode - Save the current node in the operand list.
130 class RecordMatcherNode : public MatcherNodeWithChild {
132 RecordMatcherNode() : MatcherNodeWithChild(Record) {}
134 static inline bool classof(const MatcherNode *N) {
135 return N->getKind() == Record;
138 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
141 /// MoveChildMatcherNode - This tells the interpreter to move into the
142 /// specified child node.
143 class MoveChildMatcherNode : public MatcherNodeWithChild {
146 MoveChildMatcherNode(unsigned childNo)
147 : MatcherNodeWithChild(MoveChild), ChildNo(childNo) {}
149 unsigned getChildNo() const { return ChildNo; }
151 static inline bool classof(const MatcherNode *N) {
152 return N->getKind() == MoveChild;
155 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
158 /// MoveParentMatcherNode - This tells the interpreter to move to the parent
159 /// of the current node.
160 class MoveParentMatcherNode : public MatcherNodeWithChild {
162 MoveParentMatcherNode()
163 : MatcherNodeWithChild(MoveParent) {}
165 static inline bool classof(const MatcherNode *N) {
166 return N->getKind() == MoveParent;
169 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
172 /// CheckSameMatcherNode - This checks to see if this node is exactly the same
173 /// node as the specified match that was recorded with 'Record'. This is used
174 /// when patterns have the same name in them, like '(mul GPR:$in, GPR:$in)'.
175 class CheckSameMatcherNode : public MatcherNodeWithChild {
176 unsigned MatchNumber;
178 CheckSameMatcherNode(unsigned matchnumber)
179 : MatcherNodeWithChild(CheckSame), MatchNumber(matchnumber) {}
181 unsigned getMatchNumber() const { return MatchNumber; }
183 static inline bool classof(const MatcherNode *N) {
184 return N->getKind() == CheckSame;
187 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
190 /// CheckPatternPredicateMatcherNode - This checks the target-specific predicate
191 /// to see if the entire pattern is capable of matching. This predicate does
192 /// not take a node as input. This is used for subtarget feature checks etc.
193 class CheckPatternPredicateMatcherNode : public MatcherNodeWithChild {
194 std::string Predicate;
196 CheckPatternPredicateMatcherNode(StringRef predicate)
197 : MatcherNodeWithChild(CheckPatternPredicate), Predicate(predicate) {}
199 StringRef getPredicate() const { return Predicate; }
201 static inline bool classof(const MatcherNode *N) {
202 return N->getKind() == CheckPatternPredicate;
205 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
208 /// CheckPredicateMatcherNode - This checks the target-specific predicate to
209 /// see if the node is acceptable.
210 class CheckPredicateMatcherNode : public MatcherNodeWithChild {
213 CheckPredicateMatcherNode(StringRef predname)
214 : MatcherNodeWithChild(CheckPredicate), PredName(predname) {}
216 StringRef getPredicateName() const { return PredName; }
218 static inline bool classof(const MatcherNode *N) {
219 return N->getKind() == CheckPredicate;
222 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
226 /// CheckOpcodeMatcherNode - This checks to see if the current node has the
227 /// specified opcode, if not it fails to match.
228 class CheckOpcodeMatcherNode : public MatcherNodeWithChild {
229 StringRef OpcodeName;
231 CheckOpcodeMatcherNode(StringRef opcodename)
232 : MatcherNodeWithChild(CheckOpcode), OpcodeName(opcodename) {}
234 StringRef getOpcodeName() const { return OpcodeName; }
236 static inline bool classof(const MatcherNode *N) {
237 return N->getKind() == CheckOpcode;
240 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
243 /// CheckTypeMatcherNode - This checks to see if the current node has the
244 /// specified type, if not it fails to match.
245 class CheckTypeMatcherNode : public MatcherNodeWithChild {
246 MVT::SimpleValueType Type;
248 CheckTypeMatcherNode(MVT::SimpleValueType type)
249 : MatcherNodeWithChild(CheckType), Type(type) {}
251 MVT::SimpleValueType getType() const { return Type; }
253 static inline bool classof(const MatcherNode *N) {
254 return N->getKind() == CheckType;
257 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
260 /// CheckIntegerMatcherNode - This checks to see if the current node is a
261 /// ConstantSDNode with the specified integer value, if not it fails to match.
262 class CheckIntegerMatcherNode : public MatcherNodeWithChild {
265 CheckIntegerMatcherNode(int64_t value)
266 : MatcherNodeWithChild(CheckInteger), Value(value) {}
268 int64_t getValue() const { return Value; }
270 static inline bool classof(const MatcherNode *N) {
271 return N->getKind() == CheckInteger;
274 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
277 /// CheckCondCodeMatcherNode - This checks to see if the current node is a
278 /// CondCodeSDNode with the specified condition, if not it fails to match.
279 class CheckCondCodeMatcherNode : public MatcherNodeWithChild {
280 StringRef CondCodeName;
282 CheckCondCodeMatcherNode(StringRef condcodename)
283 : MatcherNodeWithChild(CheckCondCode), CondCodeName(condcodename) {}
285 StringRef getCondCodeName() const { return CondCodeName; }
287 static inline bool classof(const MatcherNode *N) {
288 return N->getKind() == CheckCondCode;
291 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
294 /// CheckValueTypeMatcherNode - This checks to see if the current node is a
295 /// VTSDNode with the specified type, if not it fails to match.
296 class CheckValueTypeMatcherNode : public MatcherNodeWithChild {
299 CheckValueTypeMatcherNode(StringRef type_name)
300 : MatcherNodeWithChild(CheckValueType), TypeName(type_name) {}
302 StringRef getTypeName() const { return TypeName; }
304 static inline bool classof(const MatcherNode *N) {
305 return N->getKind() == CheckValueType;
308 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
313 /// CheckComplexPatMatcherNode - This node runs the specified ComplexPattern on
314 /// the current node.
315 class CheckComplexPatMatcherNode : public MatcherNodeWithChild {
316 const ComplexPattern &Pattern;
318 CheckComplexPatMatcherNode(const ComplexPattern &pattern)
319 : MatcherNodeWithChild(CheckComplexPat), Pattern(pattern) {}
321 static inline bool classof(const MatcherNode *N) {
322 return N->getKind() == CheckComplexPat;
325 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
328 /// CheckAndImmMatcherNode - This checks to see if the current node is an 'and'
329 /// with something equivalent to the specified immediate.
330 class CheckAndImmMatcherNode : public MatcherNodeWithChild {
333 CheckAndImmMatcherNode(int64_t value)
334 : MatcherNodeWithChild(CheckAndImm), Value(value) {}
336 int64_t getValue() const { return Value; }
338 static inline bool classof(const MatcherNode *N) {
339 return N->getKind() == CheckAndImm;
342 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
345 /// CheckOrImmMatcherNode - This checks to see if the current node is an 'and'
346 /// with something equivalent to the specified immediate.
347 class CheckOrImmMatcherNode : public MatcherNodeWithChild {
350 CheckOrImmMatcherNode(int64_t value)
351 : MatcherNodeWithChild(CheckOrImm), Value(value) {}
353 int64_t getValue() const { return Value; }
355 static inline bool classof(const MatcherNode *N) {
356 return N->getKind() == CheckOrImm;
359 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
362 /// CheckProfitableToFoldMatcherNode - This checks to see if the current node is
363 /// worthwhile to try to fold into a large pattern.
364 class CheckProfitableToFoldMatcherNode : public MatcherNodeWithChild {
366 CheckProfitableToFoldMatcherNode()
367 : MatcherNodeWithChild(CheckProfitableToFold) {}
369 static inline bool classof(const MatcherNode *N) {
370 return N->getKind() == CheckProfitableToFold;
373 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
376 /// CheckLegalToFoldMatcherNode - This checks to see if the current node is
377 /// legal to try to fold into a large pattern.
378 class CheckLegalToFoldMatcherNode : public MatcherNodeWithChild {
380 CheckLegalToFoldMatcherNode()
381 : MatcherNodeWithChild(CheckLegalToFold) {}
383 static inline bool classof(const MatcherNode *N) {
384 return N->getKind() == CheckLegalToFold;
387 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
389 } // end namespace llvm