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;
26 MatcherNode *ConvertPatternToMatcher(const PatternToMatch &Pattern,
27 const CodeGenDAGPatterns &CGP);
29 void EmitMatcherTable(const MatcherNode *Matcher, raw_ostream &OS);
32 /// MatcherNode - Base class for all the the DAG ISel Matcher representation
35 // The next matcher node that is executed after this one. Null if this is the
36 // last stage of a match.
37 OwningPtr<MatcherNode> Next;
40 // Stack manipulation.
41 Push, // Push a checking scope.
42 RecordNode, // Record the current node.
43 MoveChild, // Move current node to specified child.
44 MoveParent, // Move current node to parent.
46 // Predicate checking.
47 CheckSame, // Fail if not same as prev match.
48 CheckPatternPredicate,
49 CheckPredicate, // Fail if node predicate fails.
50 CheckOpcode, // Fail if not opcode.
51 CheckType, // Fail if not correct type.
52 CheckInteger, // Fail if wrong val.
53 CheckCondCode, // Fail if not condcode.
58 CheckFoldableChainNode,
61 // Node creation/emisssion.
62 EmitInteger, // Create a TargetConstant
63 EmitRegister, // Create a register.
69 MatcherNode(KindTy K) : Kind(K) {}
71 virtual ~MatcherNode() {}
73 KindTy getKind() const { return Kind; }
75 MatcherNode *getNext() { return Next.get(); }
76 const MatcherNode *getNext() const { return Next.get(); }
77 void setNext(MatcherNode *C) { Next.reset(C); }
79 static inline bool classof(const MatcherNode *) { return true; }
81 virtual void print(raw_ostream &OS, unsigned indent = 0) const = 0;
84 void printNext(raw_ostream &OS, unsigned indent) const;
87 /// PushMatcherNode - This pushes a failure scope on the stack and evaluates
88 /// 'Next'. If 'Next' fails to match, it pops its scope and attempts to
90 class PushMatcherNode : public MatcherNode {
91 OwningPtr<MatcherNode> Failure;
93 PushMatcherNode(MatcherNode *next = 0, MatcherNode *failure = 0)
94 : MatcherNode(Push), Failure(failure) {
98 MatcherNode *getFailure() { return Failure.get(); }
99 const MatcherNode *getFailure() const { return Failure.get(); }
100 void setFailure(MatcherNode *N) { Failure.reset(N); }
102 static inline bool classof(const MatcherNode *N) {
103 return N->getKind() == Push;
106 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
109 /// RecordMatcherNode - Save the current node in the operand list.
110 class RecordMatcherNode : public MatcherNode {
111 /// WhatFor - This is a string indicating why we're recording this. This
112 /// should only be used for comment generation not anything semantic.
115 RecordMatcherNode(const std::string &whatfor)
116 : MatcherNode(RecordNode), WhatFor(whatfor) {}
118 const std::string &getWhatFor() const { return WhatFor; }
120 static inline bool classof(const MatcherNode *N) {
121 return N->getKind() == RecordNode;
124 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
127 /// MoveChildMatcherNode - This tells the interpreter to move into the
128 /// specified child node.
129 class MoveChildMatcherNode : public MatcherNode {
132 MoveChildMatcherNode(unsigned childNo)
133 : MatcherNode(MoveChild), ChildNo(childNo) {}
135 unsigned getChildNo() const { return ChildNo; }
137 static inline bool classof(const MatcherNode *N) {
138 return N->getKind() == MoveChild;
141 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
144 /// MoveParentMatcherNode - This tells the interpreter to move to the parent
145 /// of the current node.
146 class MoveParentMatcherNode : public MatcherNode {
148 MoveParentMatcherNode()
149 : MatcherNode(MoveParent) {}
151 static inline bool classof(const MatcherNode *N) {
152 return N->getKind() == MoveParent;
155 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
158 /// CheckSameMatcherNode - This checks to see if this node is exactly the same
159 /// node as the specified match that was recorded with 'Record'. This is used
160 /// when patterns have the same name in them, like '(mul GPR:$in, GPR:$in)'.
161 class CheckSameMatcherNode : public MatcherNode {
162 unsigned MatchNumber;
164 CheckSameMatcherNode(unsigned matchnumber)
165 : MatcherNode(CheckSame), MatchNumber(matchnumber) {}
167 unsigned getMatchNumber() const { return MatchNumber; }
169 static inline bool classof(const MatcherNode *N) {
170 return N->getKind() == CheckSame;
173 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
176 /// CheckPatternPredicateMatcherNode - This checks the target-specific predicate
177 /// to see if the entire pattern is capable of matching. This predicate does
178 /// not take a node as input. This is used for subtarget feature checks etc.
179 class CheckPatternPredicateMatcherNode : public MatcherNode {
180 std::string Predicate;
182 CheckPatternPredicateMatcherNode(StringRef predicate)
183 : MatcherNode(CheckPatternPredicate), Predicate(predicate) {}
185 StringRef getPredicate() const { return Predicate; }
187 static inline bool classof(const MatcherNode *N) {
188 return N->getKind() == CheckPatternPredicate;
191 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
194 /// CheckPredicateMatcherNode - This checks the target-specific predicate to
195 /// see if the node is acceptable.
196 class CheckPredicateMatcherNode : public MatcherNode {
199 CheckPredicateMatcherNode(StringRef predname)
200 : MatcherNode(CheckPredicate), PredName(predname) {}
202 StringRef getPredicateName() const { return PredName; }
204 static inline bool classof(const MatcherNode *N) {
205 return N->getKind() == CheckPredicate;
208 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
212 /// CheckOpcodeMatcherNode - This checks to see if the current node has the
213 /// specified opcode, if not it fails to match.
214 class CheckOpcodeMatcherNode : public MatcherNode {
215 StringRef OpcodeName;
217 CheckOpcodeMatcherNode(StringRef opcodename)
218 : MatcherNode(CheckOpcode), OpcodeName(opcodename) {}
220 StringRef getOpcodeName() const { return OpcodeName; }
222 static inline bool classof(const MatcherNode *N) {
223 return N->getKind() == CheckOpcode;
226 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
229 /// CheckTypeMatcherNode - This checks to see if the current node has the
230 /// specified type, if not it fails to match.
231 class CheckTypeMatcherNode : public MatcherNode {
232 MVT::SimpleValueType Type;
234 CheckTypeMatcherNode(MVT::SimpleValueType type)
235 : MatcherNode(CheckType), Type(type) {}
237 MVT::SimpleValueType getType() const { return Type; }
239 static inline bool classof(const MatcherNode *N) {
240 return N->getKind() == CheckType;
243 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
246 /// CheckIntegerMatcherNode - This checks to see if the current node is a
247 /// ConstantSDNode with the specified integer value, if not it fails to match.
248 class CheckIntegerMatcherNode : public MatcherNode {
251 CheckIntegerMatcherNode(int64_t value)
252 : MatcherNode(CheckInteger), Value(value) {}
254 int64_t getValue() const { return Value; }
256 static inline bool classof(const MatcherNode *N) {
257 return N->getKind() == CheckInteger;
260 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
263 /// CheckCondCodeMatcherNode - This checks to see if the current node is a
264 /// CondCodeSDNode with the specified condition, if not it fails to match.
265 class CheckCondCodeMatcherNode : public MatcherNode {
266 StringRef CondCodeName;
268 CheckCondCodeMatcherNode(StringRef condcodename)
269 : MatcherNode(CheckCondCode), CondCodeName(condcodename) {}
271 StringRef getCondCodeName() const { return CondCodeName; }
273 static inline bool classof(const MatcherNode *N) {
274 return N->getKind() == CheckCondCode;
277 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
280 /// CheckValueTypeMatcherNode - This checks to see if the current node is a
281 /// VTSDNode with the specified type, if not it fails to match.
282 class CheckValueTypeMatcherNode : public MatcherNode {
285 CheckValueTypeMatcherNode(StringRef type_name)
286 : MatcherNode(CheckValueType), TypeName(type_name) {}
288 StringRef getTypeName() const { return TypeName; }
290 static inline bool classof(const MatcherNode *N) {
291 return N->getKind() == CheckValueType;
294 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
299 /// CheckComplexPatMatcherNode - This node runs the specified ComplexPattern on
300 /// the current node.
301 class CheckComplexPatMatcherNode : public MatcherNode {
302 const ComplexPattern &Pattern;
304 CheckComplexPatMatcherNode(const ComplexPattern &pattern)
305 : MatcherNode(CheckComplexPat), Pattern(pattern) {}
307 const ComplexPattern &getPattern() const { return Pattern; }
309 static inline bool classof(const MatcherNode *N) {
310 return N->getKind() == CheckComplexPat;
313 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
316 /// CheckAndImmMatcherNode - This checks to see if the current node is an 'and'
317 /// with something equivalent to the specified immediate.
318 class CheckAndImmMatcherNode : public MatcherNode {
321 CheckAndImmMatcherNode(int64_t value)
322 : MatcherNode(CheckAndImm), Value(value) {}
324 int64_t getValue() const { return Value; }
326 static inline bool classof(const MatcherNode *N) {
327 return N->getKind() == CheckAndImm;
330 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
333 /// CheckOrImmMatcherNode - This checks to see if the current node is an 'and'
334 /// with something equivalent to the specified immediate.
335 class CheckOrImmMatcherNode : public MatcherNode {
338 CheckOrImmMatcherNode(int64_t value)
339 : MatcherNode(CheckOrImm), Value(value) {}
341 int64_t getValue() const { return Value; }
343 static inline bool classof(const MatcherNode *N) {
344 return N->getKind() == CheckOrImm;
347 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
350 /// CheckFoldableChainNodeMatcherNode - This checks to see if the current node
351 /// (which defines a chain operand) is safe to fold into a larger pattern.
352 class CheckFoldableChainNodeMatcherNode : public MatcherNode {
354 CheckFoldableChainNodeMatcherNode()
355 : MatcherNode(CheckFoldableChainNode) {}
357 static inline bool classof(const MatcherNode *N) {
358 return N->getKind() == CheckFoldableChainNode;
361 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
364 /// CheckChainCompatibleMatcherNode - Verify that the current node's chain
365 /// operand is 'compatible' with the specified recorded node's.
366 class CheckChainCompatibleMatcherNode : public MatcherNode {
369 CheckChainCompatibleMatcherNode(unsigned previousop)
370 : MatcherNode(CheckChainCompatible), PreviousOp(previousop) {}
372 unsigned getPreviousOp() const { return PreviousOp; }
374 static inline bool classof(const MatcherNode *N) {
375 return N->getKind() == CheckChainCompatible;
378 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
381 /// EmitIntegerMatcherNode - This creates a new TargetConstant.
382 class EmitIntegerMatcherNode : public MatcherNode {
384 MVT::SimpleValueType VT;
386 EmitIntegerMatcherNode(int64_t val, MVT::SimpleValueType vt)
387 : MatcherNode(EmitInteger), Val(val), VT(vt) {}
389 int64_t getVal() const { return Val; }
390 MVT::SimpleValueType getVT() const { return VT; }
392 static inline bool classof(const MatcherNode *N) {
393 return N->getKind() == EmitInteger;
396 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
399 /// EmitRegisterMatcherNode - This creates a new TargetConstant.
400 class EmitRegisterMatcherNode : public MatcherNode {
401 /// Reg - The def for the register that we're emitting. If this is null, then
402 /// this is a reference to zero_reg.
404 MVT::SimpleValueType VT;
406 EmitRegisterMatcherNode(Record *reg, MVT::SimpleValueType vt)
407 : MatcherNode(EmitRegister), Reg(reg), VT(vt) {}
409 Record *getReg() const { return Reg; }
410 MVT::SimpleValueType getVT() const { return VT; }
412 static inline bool classof(const MatcherNode *N) {
413 return N->getKind() == EmitRegister;
416 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
419 /// EmitNodeMatcherNode - This signals a successful match and generates a node.
420 class EmitNodeMatcherNode : public MatcherNode {
421 const PatternToMatch &Pattern;
423 EmitNodeMatcherNode(const PatternToMatch &pattern)
424 : MatcherNode(EmitNode), Pattern(pattern) {}
426 const PatternToMatch &getPattern() const { return Pattern; }
428 static inline bool classof(const MatcherNode *N) {
429 return N->getKind() == EmitNode;
432 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
435 } // end namespace llvm