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
34 OwningPtr<MatcherNode> Child;
38 Push, // [Push, Dest0, Dest1, Dest2, Dest3]
40 MoveChild, // [MoveChild, Child#]
41 MoveParent, // [MoveParent]
43 CheckSame, // [CheckSame, N] Fail if not same as prev match.
44 CheckPatternPredicate,
45 CheckPredicate, // [CheckPredicate, P] Fail if predicate fails.
46 CheckOpcode, // [CheckOpcode, Opcode] Fail if not opcode.
47 CheckType, // [CheckType, MVT] Fail if not correct type.
48 CheckInteger, // [CheckInteger, int0,int1,int2,...int7] Fail if wrong val.
49 CheckCondCode, // [CheckCondCode, CondCode] Fail if not condcode.
54 CheckFoldableChainNode,
60 MatcherNode(KindTy K) : Kind(K) {}
62 virtual ~MatcherNode() {}
64 KindTy getKind() const { return Kind; }
66 MatcherNode *getChild() { return Child.get(); }
67 const MatcherNode *getChild() const { return Child.get(); }
68 void setChild(MatcherNode *C) { Child.reset(C); }
70 static inline bool classof(const MatcherNode *) { return true; }
72 virtual void print(raw_ostream &OS, unsigned indent = 0) const = 0;
75 void printChild(raw_ostream &OS, unsigned indent) const;
78 /// EmitNodeMatcherNode - This signals a successful match and generates a node.
79 class EmitNodeMatcherNode : public MatcherNode {
80 const PatternToMatch &Pattern;
82 EmitNodeMatcherNode(const PatternToMatch &pattern)
83 : MatcherNode(EmitNode), Pattern(pattern) {}
85 const PatternToMatch &getPattern() const { return Pattern; }
87 static inline bool classof(const MatcherNode *N) {
88 return N->getKind() == EmitNode;
91 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
95 /// PushMatcherNode - This pushes a failure scope on the stack and evaluates
96 /// 'child'. If 'child' fails to match, it pops its scope and attempts to
98 class PushMatcherNode : public MatcherNode {
99 OwningPtr<MatcherNode> Failure;
101 PushMatcherNode(MatcherNode *child = 0, MatcherNode *failure = 0)
102 : MatcherNode(Push), Failure(failure) {
106 MatcherNode *getFailure() { return Failure.get(); }
107 const MatcherNode *getFailure() const { return Failure.get(); }
108 void setFailure(MatcherNode *N) { Failure.reset(N); }
110 static inline bool classof(const MatcherNode *N) {
111 return N->getKind() == Push;
114 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
117 /// RecordMatcherNode - Save the current node in the operand list.
118 class RecordMatcherNode : public MatcherNode {
119 /// WhatFor - This is a string indicating why we're recording this. This
120 /// should only be used for comment generation not anything semantic.
123 RecordMatcherNode(const std::string &whatfor)
124 : MatcherNode(Record), WhatFor(whatfor) {}
126 const std::string &getWhatFor() const { return WhatFor; }
128 static inline bool classof(const MatcherNode *N) {
129 return N->getKind() == Record;
132 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
135 /// MoveChildMatcherNode - This tells the interpreter to move into the
136 /// specified child node.
137 class MoveChildMatcherNode : public MatcherNode {
140 MoveChildMatcherNode(unsigned childNo)
141 : MatcherNode(MoveChild), ChildNo(childNo) {}
143 unsigned getChildNo() const { return ChildNo; }
145 static inline bool classof(const MatcherNode *N) {
146 return N->getKind() == MoveChild;
149 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
152 /// MoveParentMatcherNode - This tells the interpreter to move to the parent
153 /// of the current node.
154 class MoveParentMatcherNode : public MatcherNode {
156 MoveParentMatcherNode()
157 : MatcherNode(MoveParent) {}
159 static inline bool classof(const MatcherNode *N) {
160 return N->getKind() == MoveParent;
163 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
166 /// CheckSameMatcherNode - This checks to see if this node is exactly the same
167 /// node as the specified match that was recorded with 'Record'. This is used
168 /// when patterns have the same name in them, like '(mul GPR:$in, GPR:$in)'.
169 class CheckSameMatcherNode : public MatcherNode {
170 unsigned MatchNumber;
172 CheckSameMatcherNode(unsigned matchnumber)
173 : MatcherNode(CheckSame), MatchNumber(matchnumber) {}
175 unsigned getMatchNumber() const { return MatchNumber; }
177 static inline bool classof(const MatcherNode *N) {
178 return N->getKind() == CheckSame;
181 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
184 /// CheckPatternPredicateMatcherNode - This checks the target-specific predicate
185 /// to see if the entire pattern is capable of matching. This predicate does
186 /// not take a node as input. This is used for subtarget feature checks etc.
187 class CheckPatternPredicateMatcherNode : public MatcherNode {
188 std::string Predicate;
190 CheckPatternPredicateMatcherNode(StringRef predicate)
191 : MatcherNode(CheckPatternPredicate), Predicate(predicate) {}
193 StringRef getPredicate() const { return Predicate; }
195 static inline bool classof(const MatcherNode *N) {
196 return N->getKind() == CheckPatternPredicate;
199 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
202 /// CheckPredicateMatcherNode - This checks the target-specific predicate to
203 /// see if the node is acceptable.
204 class CheckPredicateMatcherNode : public MatcherNode {
207 CheckPredicateMatcherNode(StringRef predname)
208 : MatcherNode(CheckPredicate), PredName(predname) {}
210 StringRef getPredicateName() const { return PredName; }
212 static inline bool classof(const MatcherNode *N) {
213 return N->getKind() == CheckPredicate;
216 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
220 /// CheckOpcodeMatcherNode - This checks to see if the current node has the
221 /// specified opcode, if not it fails to match.
222 class CheckOpcodeMatcherNode : public MatcherNode {
223 StringRef OpcodeName;
225 CheckOpcodeMatcherNode(StringRef opcodename)
226 : MatcherNode(CheckOpcode), OpcodeName(opcodename) {}
228 StringRef getOpcodeName() const { return OpcodeName; }
230 static inline bool classof(const MatcherNode *N) {
231 return N->getKind() == CheckOpcode;
234 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
237 /// CheckTypeMatcherNode - This checks to see if the current node has the
238 /// specified type, if not it fails to match.
239 class CheckTypeMatcherNode : public MatcherNode {
240 MVT::SimpleValueType Type;
242 CheckTypeMatcherNode(MVT::SimpleValueType type)
243 : MatcherNode(CheckType), Type(type) {}
245 MVT::SimpleValueType getType() const { return Type; }
247 static inline bool classof(const MatcherNode *N) {
248 return N->getKind() == CheckType;
251 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
254 /// CheckIntegerMatcherNode - This checks to see if the current node is a
255 /// ConstantSDNode with the specified integer value, if not it fails to match.
256 class CheckIntegerMatcherNode : public MatcherNode {
259 CheckIntegerMatcherNode(int64_t value)
260 : MatcherNode(CheckInteger), Value(value) {}
262 int64_t getValue() const { return Value; }
264 static inline bool classof(const MatcherNode *N) {
265 return N->getKind() == CheckInteger;
268 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
271 /// CheckCondCodeMatcherNode - This checks to see if the current node is a
272 /// CondCodeSDNode with the specified condition, if not it fails to match.
273 class CheckCondCodeMatcherNode : public MatcherNode {
274 StringRef CondCodeName;
276 CheckCondCodeMatcherNode(StringRef condcodename)
277 : MatcherNode(CheckCondCode), CondCodeName(condcodename) {}
279 StringRef getCondCodeName() const { return CondCodeName; }
281 static inline bool classof(const MatcherNode *N) {
282 return N->getKind() == CheckCondCode;
285 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
288 /// CheckValueTypeMatcherNode - This checks to see if the current node is a
289 /// VTSDNode with the specified type, if not it fails to match.
290 class CheckValueTypeMatcherNode : public MatcherNode {
293 CheckValueTypeMatcherNode(StringRef type_name)
294 : MatcherNode(CheckValueType), TypeName(type_name) {}
296 StringRef getTypeName() const { return TypeName; }
298 static inline bool classof(const MatcherNode *N) {
299 return N->getKind() == CheckValueType;
302 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
307 /// CheckComplexPatMatcherNode - This node runs the specified ComplexPattern on
308 /// the current node.
309 class CheckComplexPatMatcherNode : public MatcherNode {
310 const ComplexPattern &Pattern;
312 CheckComplexPatMatcherNode(const ComplexPattern &pattern)
313 : MatcherNode(CheckComplexPat), Pattern(pattern) {}
315 const ComplexPattern &getPattern() const { return Pattern; }
317 static inline bool classof(const MatcherNode *N) {
318 return N->getKind() == CheckComplexPat;
321 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
324 /// CheckAndImmMatcherNode - This checks to see if the current node is an 'and'
325 /// with something equivalent to the specified immediate.
326 class CheckAndImmMatcherNode : public MatcherNode {
329 CheckAndImmMatcherNode(int64_t value)
330 : MatcherNode(CheckAndImm), Value(value) {}
332 int64_t getValue() const { return Value; }
334 static inline bool classof(const MatcherNode *N) {
335 return N->getKind() == CheckAndImm;
338 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
341 /// CheckOrImmMatcherNode - This checks to see if the current node is an 'and'
342 /// with something equivalent to the specified immediate.
343 class CheckOrImmMatcherNode : public MatcherNode {
346 CheckOrImmMatcherNode(int64_t value)
347 : MatcherNode(CheckOrImm), Value(value) {}
349 int64_t getValue() const { return Value; }
351 static inline bool classof(const MatcherNode *N) {
352 return N->getKind() == CheckOrImm;
355 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
358 /// CheckFoldableChainNodeMatcherNode - This checks to see if the current node
359 /// (which defines a chain operand) is safe to fold into a larger pattern.
360 class CheckFoldableChainNodeMatcherNode : public MatcherNode {
362 CheckFoldableChainNodeMatcherNode()
363 : MatcherNode(CheckFoldableChainNode) {}
365 static inline bool classof(const MatcherNode *N) {
366 return N->getKind() == CheckFoldableChainNode;
369 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
372 /// CheckChainCompatibleMatcherNode - Verify that the current node's chain
373 /// operand is 'compatible' with the specified recorded node's.
374 class CheckChainCompatibleMatcherNode : public MatcherNode {
377 CheckChainCompatibleMatcherNode(unsigned previousop)
378 : MatcherNode(CheckChainCompatible), PreviousOp(previousop) {}
380 unsigned getPreviousOp() const { return PreviousOp; }
382 static inline bool classof(const MatcherNode *N) {
383 return N->getKind() == CheckChainCompatible;
386 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
391 } // end namespace llvm