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 // The next matcher node that is executed after this one. Null if this is the
35 // last stage of a match.
36 OwningPtr<MatcherNode> Next;
40 Push, // [Push, Dest0, Dest1, Dest2, Dest3]
42 MoveChild, // [MoveChild, Child#]
43 MoveParent, // [MoveParent]
45 CheckSame, // [CheckSame, N] Fail if not same as prev match.
46 CheckPatternPredicate,
47 CheckPredicate, // [CheckPredicate, P] Fail if predicate fails.
48 CheckOpcode, // [CheckOpcode, Opcode] Fail if not opcode.
49 CheckType, // [CheckType, MVT] Fail if not correct type.
50 CheckInteger, // [CheckInteger, int0,int1,int2,...int7] Fail if wrong val.
51 CheckCondCode, // [CheckCondCode, CondCode] Fail if not condcode.
56 CheckFoldableChainNode,
62 MatcherNode(KindTy K) : Kind(K) {}
64 virtual ~MatcherNode() {}
66 KindTy getKind() const { return Kind; }
68 MatcherNode *getNext() { return Next.get(); }
69 const MatcherNode *getNext() const { return Next.get(); }
70 void setNext(MatcherNode *C) { Next.reset(C); }
72 static inline bool classof(const MatcherNode *) { return true; }
74 virtual void print(raw_ostream &OS, unsigned indent = 0) const = 0;
77 void printNext(raw_ostream &OS, unsigned indent) const;
80 /// EmitNodeMatcherNode - This signals a successful match and generates a node.
81 class EmitNodeMatcherNode : public MatcherNode {
82 const PatternToMatch &Pattern;
84 EmitNodeMatcherNode(const PatternToMatch &pattern)
85 : MatcherNode(EmitNode), Pattern(pattern) {}
87 const PatternToMatch &getPattern() const { return Pattern; }
89 static inline bool classof(const MatcherNode *N) {
90 return N->getKind() == EmitNode;
93 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
97 /// PushMatcherNode - This pushes a failure scope on the stack and evaluates
98 /// 'Next'. If 'Next' fails to match, it pops its scope and attempts to
100 class PushMatcherNode : public MatcherNode {
101 OwningPtr<MatcherNode> Failure;
103 PushMatcherNode(MatcherNode *next = 0, MatcherNode *failure = 0)
104 : MatcherNode(Push), Failure(failure) {
108 MatcherNode *getFailure() { return Failure.get(); }
109 const MatcherNode *getFailure() const { return Failure.get(); }
110 void setFailure(MatcherNode *N) { Failure.reset(N); }
112 static inline bool classof(const MatcherNode *N) {
113 return N->getKind() == Push;
116 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
119 /// RecordMatcherNode - Save the current node in the operand list.
120 class RecordMatcherNode : public MatcherNode {
121 /// WhatFor - This is a string indicating why we're recording this. This
122 /// should only be used for comment generation not anything semantic.
125 RecordMatcherNode(const std::string &whatfor)
126 : MatcherNode(Record), WhatFor(whatfor) {}
128 const std::string &getWhatFor() const { return WhatFor; }
130 static inline bool classof(const MatcherNode *N) {
131 return N->getKind() == Record;
134 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
137 /// MoveChildMatcherNode - This tells the interpreter to move into the
138 /// specified child node.
139 class MoveChildMatcherNode : public MatcherNode {
142 MoveChildMatcherNode(unsigned childNo)
143 : MatcherNode(MoveChild), ChildNo(childNo) {}
145 unsigned getChildNo() const { return ChildNo; }
147 static inline bool classof(const MatcherNode *N) {
148 return N->getKind() == MoveChild;
151 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
154 /// MoveParentMatcherNode - This tells the interpreter to move to the parent
155 /// of the current node.
156 class MoveParentMatcherNode : public MatcherNode {
158 MoveParentMatcherNode()
159 : MatcherNode(MoveParent) {}
161 static inline bool classof(const MatcherNode *N) {
162 return N->getKind() == MoveParent;
165 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
168 /// CheckSameMatcherNode - This checks to see if this node is exactly the same
169 /// node as the specified match that was recorded with 'Record'. This is used
170 /// when patterns have the same name in them, like '(mul GPR:$in, GPR:$in)'.
171 class CheckSameMatcherNode : public MatcherNode {
172 unsigned MatchNumber;
174 CheckSameMatcherNode(unsigned matchnumber)
175 : MatcherNode(CheckSame), MatchNumber(matchnumber) {}
177 unsigned getMatchNumber() const { return MatchNumber; }
179 static inline bool classof(const MatcherNode *N) {
180 return N->getKind() == CheckSame;
183 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
186 /// CheckPatternPredicateMatcherNode - This checks the target-specific predicate
187 /// to see if the entire pattern is capable of matching. This predicate does
188 /// not take a node as input. This is used for subtarget feature checks etc.
189 class CheckPatternPredicateMatcherNode : public MatcherNode {
190 std::string Predicate;
192 CheckPatternPredicateMatcherNode(StringRef predicate)
193 : MatcherNode(CheckPatternPredicate), Predicate(predicate) {}
195 StringRef getPredicate() const { return Predicate; }
197 static inline bool classof(const MatcherNode *N) {
198 return N->getKind() == CheckPatternPredicate;
201 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
204 /// CheckPredicateMatcherNode - This checks the target-specific predicate to
205 /// see if the node is acceptable.
206 class CheckPredicateMatcherNode : public MatcherNode {
209 CheckPredicateMatcherNode(StringRef predname)
210 : MatcherNode(CheckPredicate), PredName(predname) {}
212 StringRef getPredicateName() const { return PredName; }
214 static inline bool classof(const MatcherNode *N) {
215 return N->getKind() == CheckPredicate;
218 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
222 /// CheckOpcodeMatcherNode - This checks to see if the current node has the
223 /// specified opcode, if not it fails to match.
224 class CheckOpcodeMatcherNode : public MatcherNode {
225 StringRef OpcodeName;
227 CheckOpcodeMatcherNode(StringRef opcodename)
228 : MatcherNode(CheckOpcode), OpcodeName(opcodename) {}
230 StringRef getOpcodeName() const { return OpcodeName; }
232 static inline bool classof(const MatcherNode *N) {
233 return N->getKind() == CheckOpcode;
236 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
239 /// CheckTypeMatcherNode - This checks to see if the current node has the
240 /// specified type, if not it fails to match.
241 class CheckTypeMatcherNode : public MatcherNode {
242 MVT::SimpleValueType Type;
244 CheckTypeMatcherNode(MVT::SimpleValueType type)
245 : MatcherNode(CheckType), Type(type) {}
247 MVT::SimpleValueType getType() const { return Type; }
249 static inline bool classof(const MatcherNode *N) {
250 return N->getKind() == CheckType;
253 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
256 /// CheckIntegerMatcherNode - This checks to see if the current node is a
257 /// ConstantSDNode with the specified integer value, if not it fails to match.
258 class CheckIntegerMatcherNode : public MatcherNode {
261 CheckIntegerMatcherNode(int64_t value)
262 : MatcherNode(CheckInteger), Value(value) {}
264 int64_t getValue() const { return Value; }
266 static inline bool classof(const MatcherNode *N) {
267 return N->getKind() == CheckInteger;
270 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
273 /// CheckCondCodeMatcherNode - This checks to see if the current node is a
274 /// CondCodeSDNode with the specified condition, if not it fails to match.
275 class CheckCondCodeMatcherNode : public MatcherNode {
276 StringRef CondCodeName;
278 CheckCondCodeMatcherNode(StringRef condcodename)
279 : MatcherNode(CheckCondCode), CondCodeName(condcodename) {}
281 StringRef getCondCodeName() const { return CondCodeName; }
283 static inline bool classof(const MatcherNode *N) {
284 return N->getKind() == CheckCondCode;
287 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
290 /// CheckValueTypeMatcherNode - This checks to see if the current node is a
291 /// VTSDNode with the specified type, if not it fails to match.
292 class CheckValueTypeMatcherNode : public MatcherNode {
295 CheckValueTypeMatcherNode(StringRef type_name)
296 : MatcherNode(CheckValueType), TypeName(type_name) {}
298 StringRef getTypeName() const { return TypeName; }
300 static inline bool classof(const MatcherNode *N) {
301 return N->getKind() == CheckValueType;
304 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
309 /// CheckComplexPatMatcherNode - This node runs the specified ComplexPattern on
310 /// the current node.
311 class CheckComplexPatMatcherNode : public MatcherNode {
312 const ComplexPattern &Pattern;
314 CheckComplexPatMatcherNode(const ComplexPattern &pattern)
315 : MatcherNode(CheckComplexPat), Pattern(pattern) {}
317 const ComplexPattern &getPattern() const { return Pattern; }
319 static inline bool classof(const MatcherNode *N) {
320 return N->getKind() == CheckComplexPat;
323 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
326 /// CheckAndImmMatcherNode - This checks to see if the current node is an 'and'
327 /// with something equivalent to the specified immediate.
328 class CheckAndImmMatcherNode : public MatcherNode {
331 CheckAndImmMatcherNode(int64_t value)
332 : MatcherNode(CheckAndImm), Value(value) {}
334 int64_t getValue() const { return Value; }
336 static inline bool classof(const MatcherNode *N) {
337 return N->getKind() == CheckAndImm;
340 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
343 /// CheckOrImmMatcherNode - This checks to see if the current node is an 'and'
344 /// with something equivalent to the specified immediate.
345 class CheckOrImmMatcherNode : public MatcherNode {
348 CheckOrImmMatcherNode(int64_t value)
349 : MatcherNode(CheckOrImm), Value(value) {}
351 int64_t getValue() const { return Value; }
353 static inline bool classof(const MatcherNode *N) {
354 return N->getKind() == CheckOrImm;
357 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
360 /// CheckFoldableChainNodeMatcherNode - This checks to see if the current node
361 /// (which defines a chain operand) is safe to fold into a larger pattern.
362 class CheckFoldableChainNodeMatcherNode : public MatcherNode {
364 CheckFoldableChainNodeMatcherNode()
365 : MatcherNode(CheckFoldableChainNode) {}
367 static inline bool classof(const MatcherNode *N) {
368 return N->getKind() == CheckFoldableChainNode;
371 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
374 /// CheckChainCompatibleMatcherNode - Verify that the current node's chain
375 /// operand is 'compatible' with the specified recorded node's.
376 class CheckChainCompatibleMatcherNode : public MatcherNode {
379 CheckChainCompatibleMatcherNode(unsigned previousop)
380 : MatcherNode(CheckChainCompatible), PreviousOp(previousop) {}
382 unsigned getPreviousOp() const { return PreviousOp; }
384 static inline bool classof(const MatcherNode *N) {
385 return N->getKind() == CheckChainCompatible;
388 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
393 } // end namespace llvm