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/ADT/SmallVector.h"
17 #include "llvm/Support/Casting.h"
20 class CodeGenDAGPatterns;
27 MatcherNode *ConvertPatternToMatcher(const PatternToMatch &Pattern,
28 const CodeGenDAGPatterns &CGP);
30 void EmitMatcherTable(const MatcherNode *Matcher, raw_ostream &OS);
33 /// MatcherNode - Base class for all the the DAG ISel Matcher representation
36 // The next matcher node that is executed after this one. Null if this is the
37 // last stage of a match.
38 OwningPtr<MatcherNode> Next;
41 // Matcher state manipulation.
42 Push, // Push a checking scope.
43 RecordNode, // Record the current node.
44 RecordMemRef, // Record the memref in the current node.
45 CaptureFlagInput, // If the current node has an input flag, save it.
46 MoveChild, // Move current node to specified child.
47 MoveParent, // Move current node to parent.
49 // Predicate checking.
50 CheckSame, // Fail if not same as prev match.
51 CheckPatternPredicate,
52 CheckPredicate, // Fail if node predicate fails.
53 CheckOpcode, // Fail if not opcode.
54 CheckType, // Fail if not correct type.
55 CheckInteger, // Fail if wrong val.
56 CheckCondCode, // Fail if not condcode.
61 CheckFoldableChainNode,
64 // Node creation/emisssion.
65 EmitInteger, // Create a TargetConstant
66 EmitStringInteger, // Create a TargetConstant from a string.
67 EmitRegister, // Create a register.
68 EmitConvertToTarget, // Convert a imm/fpimm to target imm/fpimm
69 EmitMergeInputChains, // Merge together a chains for an input.
70 EmitCopyToReg, // Emit a copytoreg into a physreg.
71 EmitNode, // Create a DAG node
72 EmitNodeXForm, // Run a SDNodeXForm
73 PatternMarker // Comment for printing.
78 MatcherNode(KindTy K) : Kind(K) {}
80 virtual ~MatcherNode() {}
82 KindTy getKind() const { return Kind; }
84 MatcherNode *getNext() { return Next.get(); }
85 const MatcherNode *getNext() const { return Next.get(); }
86 void setNext(MatcherNode *C) { Next.reset(C); }
88 static inline bool classof(const MatcherNode *) { return true; }
90 virtual void print(raw_ostream &OS, unsigned indent = 0) const = 0;
93 void printNext(raw_ostream &OS, unsigned indent) const;
96 /// PushMatcherNode - This pushes a failure scope on the stack and evaluates
97 /// 'Next'. If 'Next' fails to match, it pops its scope and attempts to
99 class PushMatcherNode : public MatcherNode {
100 OwningPtr<MatcherNode> Failure;
102 PushMatcherNode(MatcherNode *next = 0, MatcherNode *failure = 0)
103 : MatcherNode(Push), Failure(failure) {
107 MatcherNode *getFailure() { return Failure.get(); }
108 const MatcherNode *getFailure() const { return Failure.get(); }
109 void setFailure(MatcherNode *N) { Failure.reset(N); }
111 static inline bool classof(const MatcherNode *N) {
112 return N->getKind() == Push;
115 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
118 /// RecordMatcherNode - Save the current node in the operand list.
119 class RecordMatcherNode : public MatcherNode {
120 /// WhatFor - This is a string indicating why we're recording this. This
121 /// should only be used for comment generation not anything semantic.
124 RecordMatcherNode(const std::string &whatfor)
125 : MatcherNode(RecordNode), WhatFor(whatfor) {}
127 const std::string &getWhatFor() const { return WhatFor; }
129 static inline bool classof(const MatcherNode *N) {
130 return N->getKind() == RecordNode;
133 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
136 /// RecordMemRefMatcherNode - Save the current node's memref.
137 class RecordMemRefMatcherNode : public MatcherNode {
139 RecordMemRefMatcherNode() : MatcherNode(RecordMemRef) {}
141 static inline bool classof(const MatcherNode *N) {
142 return N->getKind() == RecordMemRef;
145 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
149 /// CaptureFlagInputMatcherNode - If the current record has a flag input, record
150 /// it so that it is used as an input to the generated code.
151 class CaptureFlagInputMatcherNode : public MatcherNode {
153 CaptureFlagInputMatcherNode()
154 : MatcherNode(CaptureFlagInput) {}
156 static inline bool classof(const MatcherNode *N) {
157 return N->getKind() == CaptureFlagInput;
160 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
163 /// MoveChildMatcherNode - This tells the interpreter to move into the
164 /// specified child node.
165 class MoveChildMatcherNode : public MatcherNode {
168 MoveChildMatcherNode(unsigned childNo)
169 : MatcherNode(MoveChild), ChildNo(childNo) {}
171 unsigned getChildNo() const { return ChildNo; }
173 static inline bool classof(const MatcherNode *N) {
174 return N->getKind() == MoveChild;
177 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
180 /// MoveParentMatcherNode - This tells the interpreter to move to the parent
181 /// of the current node.
182 class MoveParentMatcherNode : public MatcherNode {
184 MoveParentMatcherNode()
185 : MatcherNode(MoveParent) {}
187 static inline bool classof(const MatcherNode *N) {
188 return N->getKind() == MoveParent;
191 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
194 /// CheckSameMatcherNode - This checks to see if this node is exactly the same
195 /// node as the specified match that was recorded with 'Record'. This is used
196 /// when patterns have the same name in them, like '(mul GPR:$in, GPR:$in)'.
197 class CheckSameMatcherNode : public MatcherNode {
198 unsigned MatchNumber;
200 CheckSameMatcherNode(unsigned matchnumber)
201 : MatcherNode(CheckSame), MatchNumber(matchnumber) {}
203 unsigned getMatchNumber() const { return MatchNumber; }
205 static inline bool classof(const MatcherNode *N) {
206 return N->getKind() == CheckSame;
209 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
212 /// CheckPatternPredicateMatcherNode - This checks the target-specific predicate
213 /// to see if the entire pattern is capable of matching. This predicate does
214 /// not take a node as input. This is used for subtarget feature checks etc.
215 class CheckPatternPredicateMatcherNode : public MatcherNode {
216 std::string Predicate;
218 CheckPatternPredicateMatcherNode(StringRef predicate)
219 : MatcherNode(CheckPatternPredicate), Predicate(predicate) {}
221 StringRef getPredicate() const { return Predicate; }
223 static inline bool classof(const MatcherNode *N) {
224 return N->getKind() == CheckPatternPredicate;
227 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
230 /// CheckPredicateMatcherNode - This checks the target-specific predicate to
231 /// see if the node is acceptable.
232 class CheckPredicateMatcherNode : public MatcherNode {
235 CheckPredicateMatcherNode(StringRef predname)
236 : MatcherNode(CheckPredicate), PredName(predname) {}
238 StringRef getPredicateName() const { return PredName; }
240 static inline bool classof(const MatcherNode *N) {
241 return N->getKind() == CheckPredicate;
244 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
248 /// CheckOpcodeMatcherNode - This checks to see if the current node has the
249 /// specified opcode, if not it fails to match.
250 class CheckOpcodeMatcherNode : public MatcherNode {
251 StringRef OpcodeName;
253 CheckOpcodeMatcherNode(StringRef opcodename)
254 : MatcherNode(CheckOpcode), OpcodeName(opcodename) {}
256 StringRef getOpcodeName() const { return OpcodeName; }
258 static inline bool classof(const MatcherNode *N) {
259 return N->getKind() == CheckOpcode;
262 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
265 /// CheckTypeMatcherNode - This checks to see if the current node has the
266 /// specified type, if not it fails to match.
267 class CheckTypeMatcherNode : public MatcherNode {
268 MVT::SimpleValueType Type;
270 CheckTypeMatcherNode(MVT::SimpleValueType type)
271 : MatcherNode(CheckType), Type(type) {}
273 MVT::SimpleValueType getType() const { return Type; }
275 static inline bool classof(const MatcherNode *N) {
276 return N->getKind() == CheckType;
279 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
282 /// CheckIntegerMatcherNode - This checks to see if the current node is a
283 /// ConstantSDNode with the specified integer value, if not it fails to match.
284 class CheckIntegerMatcherNode : public MatcherNode {
287 CheckIntegerMatcherNode(int64_t value)
288 : MatcherNode(CheckInteger), Value(value) {}
290 int64_t getValue() const { return Value; }
292 static inline bool classof(const MatcherNode *N) {
293 return N->getKind() == CheckInteger;
296 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
299 /// CheckCondCodeMatcherNode - This checks to see if the current node is a
300 /// CondCodeSDNode with the specified condition, if not it fails to match.
301 class CheckCondCodeMatcherNode : public MatcherNode {
302 StringRef CondCodeName;
304 CheckCondCodeMatcherNode(StringRef condcodename)
305 : MatcherNode(CheckCondCode), CondCodeName(condcodename) {}
307 StringRef getCondCodeName() const { return CondCodeName; }
309 static inline bool classof(const MatcherNode *N) {
310 return N->getKind() == CheckCondCode;
313 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
316 /// CheckValueTypeMatcherNode - This checks to see if the current node is a
317 /// VTSDNode with the specified type, if not it fails to match.
318 class CheckValueTypeMatcherNode : public MatcherNode {
321 CheckValueTypeMatcherNode(StringRef type_name)
322 : MatcherNode(CheckValueType), TypeName(type_name) {}
324 StringRef getTypeName() const { return TypeName; }
326 static inline bool classof(const MatcherNode *N) {
327 return N->getKind() == CheckValueType;
330 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
335 /// CheckComplexPatMatcherNode - This node runs the specified ComplexPattern on
336 /// the current node.
337 class CheckComplexPatMatcherNode : public MatcherNode {
338 const ComplexPattern &Pattern;
340 CheckComplexPatMatcherNode(const ComplexPattern &pattern)
341 : MatcherNode(CheckComplexPat), Pattern(pattern) {}
343 const ComplexPattern &getPattern() const { return Pattern; }
345 static inline bool classof(const MatcherNode *N) {
346 return N->getKind() == CheckComplexPat;
349 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
352 /// CheckAndImmMatcherNode - This checks to see if the current node is an 'and'
353 /// with something equivalent to the specified immediate.
354 class CheckAndImmMatcherNode : public MatcherNode {
357 CheckAndImmMatcherNode(int64_t value)
358 : MatcherNode(CheckAndImm), Value(value) {}
360 int64_t getValue() const { return Value; }
362 static inline bool classof(const MatcherNode *N) {
363 return N->getKind() == CheckAndImm;
366 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
369 /// CheckOrImmMatcherNode - This checks to see if the current node is an 'and'
370 /// with something equivalent to the specified immediate.
371 class CheckOrImmMatcherNode : public MatcherNode {
374 CheckOrImmMatcherNode(int64_t value)
375 : MatcherNode(CheckOrImm), Value(value) {}
377 int64_t getValue() const { return Value; }
379 static inline bool classof(const MatcherNode *N) {
380 return N->getKind() == CheckOrImm;
383 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
386 /// CheckFoldableChainNodeMatcherNode - This checks to see if the current node
387 /// (which defines a chain operand) is safe to fold into a larger pattern.
388 class CheckFoldableChainNodeMatcherNode : public MatcherNode {
390 CheckFoldableChainNodeMatcherNode()
391 : MatcherNode(CheckFoldableChainNode) {}
393 static inline bool classof(const MatcherNode *N) {
394 return N->getKind() == CheckFoldableChainNode;
397 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
400 /// CheckChainCompatibleMatcherNode - Verify that the current node's chain
401 /// operand is 'compatible' with the specified recorded node's.
402 class CheckChainCompatibleMatcherNode : public MatcherNode {
405 CheckChainCompatibleMatcherNode(unsigned previousop)
406 : MatcherNode(CheckChainCompatible), PreviousOp(previousop) {}
408 unsigned getPreviousOp() const { return PreviousOp; }
410 static inline bool classof(const MatcherNode *N) {
411 return N->getKind() == CheckChainCompatible;
414 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
417 /// EmitIntegerMatcherNode - This creates a new TargetConstant.
418 class EmitIntegerMatcherNode : public MatcherNode {
420 MVT::SimpleValueType VT;
422 EmitIntegerMatcherNode(int64_t val, MVT::SimpleValueType vt)
423 : MatcherNode(EmitInteger), Val(val), VT(vt) {}
425 int64_t getValue() const { return Val; }
426 MVT::SimpleValueType getVT() const { return VT; }
428 static inline bool classof(const MatcherNode *N) {
429 return N->getKind() == EmitInteger;
432 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
435 /// EmitStringIntegerMatcherNode - A target constant whose value is represented
437 class EmitStringIntegerMatcherNode : public MatcherNode {
439 MVT::SimpleValueType VT;
441 EmitStringIntegerMatcherNode(const std::string &val, MVT::SimpleValueType vt)
442 : MatcherNode(EmitStringInteger), Val(val), VT(vt) {}
444 const std::string &getValue() const { return Val; }
445 MVT::SimpleValueType getVT() const { return VT; }
447 static inline bool classof(const MatcherNode *N) {
448 return N->getKind() == EmitStringInteger;
451 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
454 /// EmitRegisterMatcherNode - This creates a new TargetConstant.
455 class EmitRegisterMatcherNode : public MatcherNode {
456 /// Reg - The def for the register that we're emitting. If this is null, then
457 /// this is a reference to zero_reg.
459 MVT::SimpleValueType VT;
461 EmitRegisterMatcherNode(Record *reg, MVT::SimpleValueType vt)
462 : MatcherNode(EmitRegister), Reg(reg), VT(vt) {}
464 Record *getReg() const { return Reg; }
465 MVT::SimpleValueType getVT() const { return VT; }
467 static inline bool classof(const MatcherNode *N) {
468 return N->getKind() == EmitRegister;
471 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
474 /// EmitConvertToTargetMatcherNode - Emit an operation that reads a specified
475 /// recorded node and converts it from being a ISD::Constant to
476 /// ISD::TargetConstant, likewise for ConstantFP.
477 class EmitConvertToTargetMatcherNode : public MatcherNode {
480 EmitConvertToTargetMatcherNode(unsigned slot)
481 : MatcherNode(EmitConvertToTarget), Slot(slot) {}
483 unsigned getSlot() const { return Slot; }
485 static inline bool classof(const MatcherNode *N) {
486 return N->getKind() == EmitConvertToTarget;
489 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
492 /// EmitMergeInputChainsMatcherNode - Emit a node that merges a list of input
493 /// chains together with a token factor. The list of nodes are the nodes in the
494 /// matched pattern that have chain input/outputs. This node adds all input
495 /// chains of these nodes if they are not themselves a node in the pattern.
496 class EmitMergeInputChainsMatcherNode : public MatcherNode {
497 SmallVector<unsigned, 3> ChainNodes;
499 EmitMergeInputChainsMatcherNode(const unsigned *nodes, unsigned NumNodes)
500 : MatcherNode(EmitMergeInputChains), ChainNodes(nodes, nodes+NumNodes) {}
502 unsigned getNumNodes() const { return ChainNodes.size(); }
504 unsigned getNode(unsigned i) const {
505 assert(i < ChainNodes.size());
506 return ChainNodes[i];
509 static inline bool classof(const MatcherNode *N) {
510 return N->getKind() == EmitMergeInputChains;
513 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
516 /// EmitCopyToRegMatcherNode - Emit a CopyToReg node from a value to a physreg,
517 /// pushing the chain and flag results.
519 class EmitCopyToRegMatcherNode : public MatcherNode {
520 unsigned SrcSlot; // Value to copy into the physreg.
523 EmitCopyToRegMatcherNode(unsigned srcSlot, Record *destPhysReg)
524 : MatcherNode(EmitCopyToReg), SrcSlot(srcSlot), DestPhysReg(destPhysReg) {}
526 unsigned getSrcSlot() const { return SrcSlot; }
527 Record *getDestPhysReg() const { return DestPhysReg; }
529 static inline bool classof(const MatcherNode *N) {
530 return N->getKind() == EmitCopyToReg;
533 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
538 /// EmitNodeXFormMatcherNode - Emit an operation that runs an SDNodeXForm on a
539 /// recorded node and records the result.
540 class EmitNodeXFormMatcherNode : public MatcherNode {
544 EmitNodeXFormMatcherNode(unsigned slot, Record *nodeXForm)
545 : MatcherNode(EmitNodeXForm), Slot(slot), NodeXForm(nodeXForm) {}
547 unsigned getSlot() const { return Slot; }
548 Record *getNodeXForm() const { return NodeXForm; }
550 static inline bool classof(const MatcherNode *N) {
551 return N->getKind() == EmitNodeXForm;
554 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
557 /// EmitNodeMatcherNode - This signals a successful match and generates a node.
558 class EmitNodeMatcherNode : public MatcherNode {
559 std::string OpcodeName;
560 const SmallVector<MVT::SimpleValueType, 3> VTs;
561 const SmallVector<unsigned, 6> Operands;
562 bool HasChain, HasFlag, HasMemRefs;
564 /// NumFixedArityOperands - If this is a fixed arity node, this is set to -1.
565 /// If this is a varidic node, this is set to the number of fixed arity
566 /// operands in the root of the pattern. The rest are appended to this node.
567 int NumFixedArityOperands;
569 EmitNodeMatcherNode(const std::string &opcodeName,
570 const MVT::SimpleValueType *vts, unsigned numvts,
571 const unsigned *operands, unsigned numops,
572 bool hasChain, bool hasFlag, bool hasmemrefs,
573 int numfixedarityoperands)
574 : MatcherNode(EmitNode), OpcodeName(opcodeName),
575 VTs(vts, vts+numvts), Operands(operands, operands+numops),
576 HasChain(hasChain), HasFlag(hasFlag), HasMemRefs(hasmemrefs),
577 NumFixedArityOperands(numfixedarityoperands) {}
579 const std::string &getOpcodeName() const { return OpcodeName; }
581 unsigned getNumVTs() const { return VTs.size(); }
582 MVT::SimpleValueType getVT(unsigned i) const {
583 assert(i < VTs.size());
587 unsigned getNumOperands() const { return Operands.size(); }
588 unsigned getOperand(unsigned i) const {
589 assert(i < Operands.size());
593 bool hasChain() const { return HasChain; }
594 bool hasFlag() const { return HasFlag; }
595 bool hasMemRefs() const { return HasMemRefs; }
596 int getNumFixedArityOperands() const { return NumFixedArityOperands; }
598 static inline bool classof(const MatcherNode *N) {
599 return N->getKind() == EmitNode;
602 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
605 /// PatternMarkerMatcherNode - This prints as a comment indicating the source
606 /// and dest patterns.
607 class PatternMarkerMatcherNode : public MatcherNode {
608 const PatternToMatch &Pattern;
610 PatternMarkerMatcherNode(const PatternToMatch &pattern)
611 : MatcherNode(PatternMarker), Pattern(pattern) {}
613 const PatternToMatch &getPattern() const { return Pattern; }
615 static inline bool classof(const MatcherNode *N) {
616 return N->getKind() == PatternMarker;
619 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
623 } // end namespace llvm