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 CheckFoldableChainNode
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 {
130 /// WhatFor - This is a string indicating why we're recording this. This
131 /// should only be used for comment generation not anything semantic.
134 RecordMatcherNode(const std::string &whatfor)
135 : MatcherNodeWithChild(Record), WhatFor(whatfor) {}
137 const std::string &getWhatFor() const { return WhatFor; }
139 static inline bool classof(const MatcherNode *N) {
140 return N->getKind() == Record;
143 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
146 /// MoveChildMatcherNode - This tells the interpreter to move into the
147 /// specified child node.
148 class MoveChildMatcherNode : public MatcherNodeWithChild {
151 MoveChildMatcherNode(unsigned childNo)
152 : MatcherNodeWithChild(MoveChild), ChildNo(childNo) {}
154 unsigned getChildNo() const { return ChildNo; }
156 static inline bool classof(const MatcherNode *N) {
157 return N->getKind() == MoveChild;
160 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
163 /// MoveParentMatcherNode - This tells the interpreter to move to the parent
164 /// of the current node.
165 class MoveParentMatcherNode : public MatcherNodeWithChild {
167 MoveParentMatcherNode()
168 : MatcherNodeWithChild(MoveParent) {}
170 static inline bool classof(const MatcherNode *N) {
171 return N->getKind() == MoveParent;
174 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
177 /// CheckSameMatcherNode - This checks to see if this node is exactly the same
178 /// node as the specified match that was recorded with 'Record'. This is used
179 /// when patterns have the same name in them, like '(mul GPR:$in, GPR:$in)'.
180 class CheckSameMatcherNode : public MatcherNodeWithChild {
181 unsigned MatchNumber;
183 CheckSameMatcherNode(unsigned matchnumber)
184 : MatcherNodeWithChild(CheckSame), MatchNumber(matchnumber) {}
186 unsigned getMatchNumber() const { return MatchNumber; }
188 static inline bool classof(const MatcherNode *N) {
189 return N->getKind() == CheckSame;
192 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
195 /// CheckPatternPredicateMatcherNode - This checks the target-specific predicate
196 /// to see if the entire pattern is capable of matching. This predicate does
197 /// not take a node as input. This is used for subtarget feature checks etc.
198 class CheckPatternPredicateMatcherNode : public MatcherNodeWithChild {
199 std::string Predicate;
201 CheckPatternPredicateMatcherNode(StringRef predicate)
202 : MatcherNodeWithChild(CheckPatternPredicate), Predicate(predicate) {}
204 StringRef getPredicate() const { return Predicate; }
206 static inline bool classof(const MatcherNode *N) {
207 return N->getKind() == CheckPatternPredicate;
210 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
213 /// CheckPredicateMatcherNode - This checks the target-specific predicate to
214 /// see if the node is acceptable.
215 class CheckPredicateMatcherNode : public MatcherNodeWithChild {
218 CheckPredicateMatcherNode(StringRef predname)
219 : MatcherNodeWithChild(CheckPredicate), PredName(predname) {}
221 StringRef getPredicateName() const { return PredName; }
223 static inline bool classof(const MatcherNode *N) {
224 return N->getKind() == CheckPredicate;
227 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
231 /// CheckOpcodeMatcherNode - This checks to see if the current node has the
232 /// specified opcode, if not it fails to match.
233 class CheckOpcodeMatcherNode : public MatcherNodeWithChild {
234 StringRef OpcodeName;
236 CheckOpcodeMatcherNode(StringRef opcodename)
237 : MatcherNodeWithChild(CheckOpcode), OpcodeName(opcodename) {}
239 StringRef getOpcodeName() const { return OpcodeName; }
241 static inline bool classof(const MatcherNode *N) {
242 return N->getKind() == CheckOpcode;
245 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
248 /// CheckTypeMatcherNode - This checks to see if the current node has the
249 /// specified type, if not it fails to match.
250 class CheckTypeMatcherNode : public MatcherNodeWithChild {
251 MVT::SimpleValueType Type;
253 CheckTypeMatcherNode(MVT::SimpleValueType type)
254 : MatcherNodeWithChild(CheckType), Type(type) {}
256 MVT::SimpleValueType getType() const { return Type; }
258 static inline bool classof(const MatcherNode *N) {
259 return N->getKind() == CheckType;
262 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
265 /// CheckIntegerMatcherNode - This checks to see if the current node is a
266 /// ConstantSDNode with the specified integer value, if not it fails to match.
267 class CheckIntegerMatcherNode : public MatcherNodeWithChild {
270 CheckIntegerMatcherNode(int64_t value)
271 : MatcherNodeWithChild(CheckInteger), Value(value) {}
273 int64_t getValue() const { return Value; }
275 static inline bool classof(const MatcherNode *N) {
276 return N->getKind() == CheckInteger;
279 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
282 /// CheckCondCodeMatcherNode - This checks to see if the current node is a
283 /// CondCodeSDNode with the specified condition, if not it fails to match.
284 class CheckCondCodeMatcherNode : public MatcherNodeWithChild {
285 StringRef CondCodeName;
287 CheckCondCodeMatcherNode(StringRef condcodename)
288 : MatcherNodeWithChild(CheckCondCode), CondCodeName(condcodename) {}
290 StringRef getCondCodeName() const { return CondCodeName; }
292 static inline bool classof(const MatcherNode *N) {
293 return N->getKind() == CheckCondCode;
296 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
299 /// CheckValueTypeMatcherNode - This checks to see if the current node is a
300 /// VTSDNode with the specified type, if not it fails to match.
301 class CheckValueTypeMatcherNode : public MatcherNodeWithChild {
304 CheckValueTypeMatcherNode(StringRef type_name)
305 : MatcherNodeWithChild(CheckValueType), TypeName(type_name) {}
307 StringRef getTypeName() const { return TypeName; }
309 static inline bool classof(const MatcherNode *N) {
310 return N->getKind() == CheckValueType;
313 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
318 /// CheckComplexPatMatcherNode - This node runs the specified ComplexPattern on
319 /// the current node.
320 class CheckComplexPatMatcherNode : public MatcherNodeWithChild {
321 const ComplexPattern &Pattern;
323 CheckComplexPatMatcherNode(const ComplexPattern &pattern)
324 : MatcherNodeWithChild(CheckComplexPat), Pattern(pattern) {}
326 const ComplexPattern &getPattern() const { return Pattern; }
328 static inline bool classof(const MatcherNode *N) {
329 return N->getKind() == CheckComplexPat;
332 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
335 /// CheckAndImmMatcherNode - This checks to see if the current node is an 'and'
336 /// with something equivalent to the specified immediate.
337 class CheckAndImmMatcherNode : public MatcherNodeWithChild {
340 CheckAndImmMatcherNode(int64_t value)
341 : MatcherNodeWithChild(CheckAndImm), Value(value) {}
343 int64_t getValue() const { return Value; }
345 static inline bool classof(const MatcherNode *N) {
346 return N->getKind() == CheckAndImm;
349 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
352 /// CheckOrImmMatcherNode - This checks to see if the current node is an 'and'
353 /// with something equivalent to the specified immediate.
354 class CheckOrImmMatcherNode : public MatcherNodeWithChild {
357 CheckOrImmMatcherNode(int64_t value)
358 : MatcherNodeWithChild(CheckOrImm), Value(value) {}
360 int64_t getValue() const { return Value; }
362 static inline bool classof(const MatcherNode *N) {
363 return N->getKind() == CheckOrImm;
366 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
369 /// CheckFoldableChainNodeMatcherNode - This checks to see if the current node
370 /// (which defines a chain operand) is safe to fold into a larger pattern.
371 class CheckFoldableChainNodeMatcherNode : public MatcherNodeWithChild {
373 CheckFoldableChainNodeMatcherNode()
374 : MatcherNodeWithChild(CheckFoldableChainNode) {}
376 static inline bool classof(const MatcherNode *N) {
377 return N->getKind() == CheckFoldableChainNode;
380 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
383 } // end namespace llvm