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,
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 {
131 /// WhatFor - This is a string indicating why we're recording this. This
132 /// should only be used for comment generation not anything semantic.
135 RecordMatcherNode(const std::string &whatfor)
136 : MatcherNodeWithChild(Record), WhatFor(whatfor) {}
138 const std::string &getWhatFor() const { return WhatFor; }
140 static inline bool classof(const MatcherNode *N) {
141 return N->getKind() == Record;
144 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
147 /// MoveChildMatcherNode - This tells the interpreter to move into the
148 /// specified child node.
149 class MoveChildMatcherNode : public MatcherNodeWithChild {
152 MoveChildMatcherNode(unsigned childNo)
153 : MatcherNodeWithChild(MoveChild), ChildNo(childNo) {}
155 unsigned getChildNo() const { return ChildNo; }
157 static inline bool classof(const MatcherNode *N) {
158 return N->getKind() == MoveChild;
161 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
164 /// MoveParentMatcherNode - This tells the interpreter to move to the parent
165 /// of the current node.
166 class MoveParentMatcherNode : public MatcherNodeWithChild {
168 MoveParentMatcherNode()
169 : MatcherNodeWithChild(MoveParent) {}
171 static inline bool classof(const MatcherNode *N) {
172 return N->getKind() == MoveParent;
175 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
178 /// CheckSameMatcherNode - This checks to see if this node is exactly the same
179 /// node as the specified match that was recorded with 'Record'. This is used
180 /// when patterns have the same name in them, like '(mul GPR:$in, GPR:$in)'.
181 class CheckSameMatcherNode : public MatcherNodeWithChild {
182 unsigned MatchNumber;
184 CheckSameMatcherNode(unsigned matchnumber)
185 : MatcherNodeWithChild(CheckSame), MatchNumber(matchnumber) {}
187 unsigned getMatchNumber() const { return MatchNumber; }
189 static inline bool classof(const MatcherNode *N) {
190 return N->getKind() == CheckSame;
193 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
196 /// CheckPatternPredicateMatcherNode - This checks the target-specific predicate
197 /// to see if the entire pattern is capable of matching. This predicate does
198 /// not take a node as input. This is used for subtarget feature checks etc.
199 class CheckPatternPredicateMatcherNode : public MatcherNodeWithChild {
200 std::string Predicate;
202 CheckPatternPredicateMatcherNode(StringRef predicate)
203 : MatcherNodeWithChild(CheckPatternPredicate), Predicate(predicate) {}
205 StringRef getPredicate() const { return Predicate; }
207 static inline bool classof(const MatcherNode *N) {
208 return N->getKind() == CheckPatternPredicate;
211 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
214 /// CheckPredicateMatcherNode - This checks the target-specific predicate to
215 /// see if the node is acceptable.
216 class CheckPredicateMatcherNode : public MatcherNodeWithChild {
219 CheckPredicateMatcherNode(StringRef predname)
220 : MatcherNodeWithChild(CheckPredicate), PredName(predname) {}
222 StringRef getPredicateName() const { return PredName; }
224 static inline bool classof(const MatcherNode *N) {
225 return N->getKind() == CheckPredicate;
228 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
232 /// CheckOpcodeMatcherNode - This checks to see if the current node has the
233 /// specified opcode, if not it fails to match.
234 class CheckOpcodeMatcherNode : public MatcherNodeWithChild {
235 StringRef OpcodeName;
237 CheckOpcodeMatcherNode(StringRef opcodename)
238 : MatcherNodeWithChild(CheckOpcode), OpcodeName(opcodename) {}
240 StringRef getOpcodeName() const { return OpcodeName; }
242 static inline bool classof(const MatcherNode *N) {
243 return N->getKind() == CheckOpcode;
246 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
249 /// CheckTypeMatcherNode - This checks to see if the current node has the
250 /// specified type, if not it fails to match.
251 class CheckTypeMatcherNode : public MatcherNodeWithChild {
252 MVT::SimpleValueType Type;
254 CheckTypeMatcherNode(MVT::SimpleValueType type)
255 : MatcherNodeWithChild(CheckType), Type(type) {}
257 MVT::SimpleValueType getType() const { return Type; }
259 static inline bool classof(const MatcherNode *N) {
260 return N->getKind() == CheckType;
263 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
266 /// CheckIntegerMatcherNode - This checks to see if the current node is a
267 /// ConstantSDNode with the specified integer value, if not it fails to match.
268 class CheckIntegerMatcherNode : public MatcherNodeWithChild {
271 CheckIntegerMatcherNode(int64_t value)
272 : MatcherNodeWithChild(CheckInteger), Value(value) {}
274 int64_t getValue() const { return Value; }
276 static inline bool classof(const MatcherNode *N) {
277 return N->getKind() == CheckInteger;
280 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
283 /// CheckCondCodeMatcherNode - This checks to see if the current node is a
284 /// CondCodeSDNode with the specified condition, if not it fails to match.
285 class CheckCondCodeMatcherNode : public MatcherNodeWithChild {
286 StringRef CondCodeName;
288 CheckCondCodeMatcherNode(StringRef condcodename)
289 : MatcherNodeWithChild(CheckCondCode), CondCodeName(condcodename) {}
291 StringRef getCondCodeName() const { return CondCodeName; }
293 static inline bool classof(const MatcherNode *N) {
294 return N->getKind() == CheckCondCode;
297 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
300 /// CheckValueTypeMatcherNode - This checks to see if the current node is a
301 /// VTSDNode with the specified type, if not it fails to match.
302 class CheckValueTypeMatcherNode : public MatcherNodeWithChild {
305 CheckValueTypeMatcherNode(StringRef type_name)
306 : MatcherNodeWithChild(CheckValueType), TypeName(type_name) {}
308 StringRef getTypeName() const { return TypeName; }
310 static inline bool classof(const MatcherNode *N) {
311 return N->getKind() == CheckValueType;
314 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
319 /// CheckComplexPatMatcherNode - This node runs the specified ComplexPattern on
320 /// the current node.
321 class CheckComplexPatMatcherNode : public MatcherNodeWithChild {
322 const ComplexPattern &Pattern;
324 CheckComplexPatMatcherNode(const ComplexPattern &pattern)
325 : MatcherNodeWithChild(CheckComplexPat), Pattern(pattern) {}
327 const ComplexPattern &getPattern() const { return Pattern; }
329 static inline bool classof(const MatcherNode *N) {
330 return N->getKind() == CheckComplexPat;
333 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
336 /// CheckAndImmMatcherNode - This checks to see if the current node is an 'and'
337 /// with something equivalent to the specified immediate.
338 class CheckAndImmMatcherNode : public MatcherNodeWithChild {
341 CheckAndImmMatcherNode(int64_t value)
342 : MatcherNodeWithChild(CheckAndImm), Value(value) {}
344 int64_t getValue() const { return Value; }
346 static inline bool classof(const MatcherNode *N) {
347 return N->getKind() == CheckAndImm;
350 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
353 /// CheckOrImmMatcherNode - This checks to see if the current node is an 'and'
354 /// with something equivalent to the specified immediate.
355 class CheckOrImmMatcherNode : public MatcherNodeWithChild {
358 CheckOrImmMatcherNode(int64_t value)
359 : MatcherNodeWithChild(CheckOrImm), Value(value) {}
361 int64_t getValue() const { return Value; }
363 static inline bool classof(const MatcherNode *N) {
364 return N->getKind() == CheckOrImm;
367 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
370 /// CheckFoldableChainNodeMatcherNode - This checks to see if the current node
371 /// (which defines a chain operand) is safe to fold into a larger pattern.
372 class CheckFoldableChainNodeMatcherNode : public MatcherNodeWithChild {
374 CheckFoldableChainNodeMatcherNode()
375 : MatcherNodeWithChild(CheckFoldableChainNode) {}
377 static inline bool classof(const MatcherNode *N) {
378 return N->getKind() == CheckFoldableChainNode;
381 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
384 /// CheckChainCompatibleMatcherNode - Verify that the current node's chain
385 /// operand is 'compatible' with the specified recorded node's.
386 class CheckChainCompatibleMatcherNode : public MatcherNodeWithChild {
389 CheckChainCompatibleMatcherNode(unsigned previousop)
390 : MatcherNodeWithChild(CheckChainCompatible), PreviousOp(previousop) {}
392 unsigned getPreviousOp() const { return PreviousOp; }
394 static inline bool classof(const MatcherNode *N) {
395 return N->getKind() == CheckChainCompatible;
398 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
403 } // end namespace llvm