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);
29 MatcherNode *OptimizeMatcher(MatcherNode *Matcher);
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 RecordChild, // Record a child of the current node.
45 RecordMemRef, // Record the memref in the current node.
46 CaptureFlagInput, // If the current node has an input flag, save it.
47 MoveChild, // Move current node to specified child.
48 MoveParent, // Move current node to parent.
50 // Predicate checking.
51 CheckSame, // Fail if not same as prev match.
52 CheckPatternPredicate,
53 CheckPredicate, // Fail if node predicate fails.
54 CheckOpcode, // Fail if not opcode.
55 CheckMultiOpcode, // Fail if not in opcode list.
56 CheckType, // Fail if not correct type.
57 CheckInteger, // Fail if wrong val.
58 CheckCondCode, // Fail if not condcode.
63 CheckFoldableChainNode,
66 // Node creation/emisssion.
67 EmitInteger, // Create a TargetConstant
68 EmitStringInteger, // Create a TargetConstant from a string.
69 EmitRegister, // Create a register.
70 EmitConvertToTarget, // Convert a imm/fpimm to target imm/fpimm
71 EmitMergeInputChains, // Merge together a chains for an input.
72 EmitCopyToReg, // Emit a copytoreg into a physreg.
73 EmitNode, // Create a DAG node
74 EmitNodeXForm, // Run a SDNodeXForm
75 MarkFlagResults, // Indicate which interior nodes have flag results.
76 CompleteMatch // Finish a match and update the results.
81 MatcherNode(KindTy K) : Kind(K) {}
83 virtual ~MatcherNode() {}
85 KindTy getKind() const { return Kind; }
87 MatcherNode *getNext() { return Next.get(); }
88 const MatcherNode *getNext() const { return Next.get(); }
89 void setNext(MatcherNode *C) { Next.reset(C); }
90 MatcherNode *takeNext() { return Next.take(); }
92 OwningPtr<MatcherNode> &getNextPtr() { return Next; }
94 static inline bool classof(const MatcherNode *) { return true; }
96 virtual void print(raw_ostream &OS, unsigned indent = 0) const = 0;
99 void printNext(raw_ostream &OS, unsigned indent) const;
102 /// PushMatcherNode - This pushes a failure scope on the stack and evaluates
103 /// 'Next'. If 'Next' fails to match, it pops its scope and attempts to
105 class PushMatcherNode : public MatcherNode {
106 OwningPtr<MatcherNode> Failure;
108 PushMatcherNode(MatcherNode *next = 0, MatcherNode *failure = 0)
109 : MatcherNode(Push), Failure(failure) {
113 MatcherNode *getFailure() { return Failure.get(); }
114 const MatcherNode *getFailure() const { return Failure.get(); }
115 void setFailure(MatcherNode *N) { Failure.reset(N); }
116 OwningPtr<MatcherNode> &getFailurePtr() { return Failure; }
118 static inline bool classof(const MatcherNode *N) {
119 return N->getKind() == Push;
122 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
125 /// RecordMatcherNode - Save the current node in the operand list.
126 class RecordMatcherNode : public MatcherNode {
127 /// WhatFor - This is a string indicating why we're recording this. This
128 /// should only be used for comment generation not anything semantic.
131 RecordMatcherNode(const std::string &whatfor)
132 : MatcherNode(RecordNode), WhatFor(whatfor) {}
134 const std::string &getWhatFor() const { return WhatFor; }
136 static inline bool classof(const MatcherNode *N) {
137 return N->getKind() == RecordNode;
140 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
143 /// RecordChildMatcherNode - Save a numbered child of the current node, or fail
144 /// the match if it doesn't exist. This is logically equivalent to:
145 /// MoveChild N + RecordNode + MoveParent.
146 class RecordChildMatcherNode : public MatcherNode {
149 /// WhatFor - This is a string indicating why we're recording this. This
150 /// should only be used for comment generation not anything semantic.
153 RecordChildMatcherNode(unsigned childno, const std::string &whatfor)
154 : MatcherNode(RecordChild), ChildNo(childno), WhatFor(whatfor) {}
156 unsigned getChildNo() const { return ChildNo; }
157 const std::string &getWhatFor() const { return WhatFor; }
159 static inline bool classof(const MatcherNode *N) {
160 return N->getKind() == RecordChild;
163 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
166 /// RecordMemRefMatcherNode - Save the current node's memref.
167 class RecordMemRefMatcherNode : public MatcherNode {
169 RecordMemRefMatcherNode() : MatcherNode(RecordMemRef) {}
171 static inline bool classof(const MatcherNode *N) {
172 return N->getKind() == RecordMemRef;
175 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
179 /// CaptureFlagInputMatcherNode - If the current record has a flag input, record
180 /// it so that it is used as an input to the generated code.
181 class CaptureFlagInputMatcherNode : public MatcherNode {
183 CaptureFlagInputMatcherNode()
184 : MatcherNode(CaptureFlagInput) {}
186 static inline bool classof(const MatcherNode *N) {
187 return N->getKind() == CaptureFlagInput;
190 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
193 /// MoveChildMatcherNode - This tells the interpreter to move into the
194 /// specified child node.
195 class MoveChildMatcherNode : public MatcherNode {
198 MoveChildMatcherNode(unsigned childNo)
199 : MatcherNode(MoveChild), ChildNo(childNo) {}
201 unsigned getChildNo() const { return ChildNo; }
203 static inline bool classof(const MatcherNode *N) {
204 return N->getKind() == MoveChild;
207 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
210 /// MoveParentMatcherNode - This tells the interpreter to move to the parent
211 /// of the current node.
212 class MoveParentMatcherNode : public MatcherNode {
214 MoveParentMatcherNode()
215 : MatcherNode(MoveParent) {}
217 static inline bool classof(const MatcherNode *N) {
218 return N->getKind() == MoveParent;
221 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
224 /// CheckSameMatcherNode - This checks to see if this node is exactly the same
225 /// node as the specified match that was recorded with 'Record'. This is used
226 /// when patterns have the same name in them, like '(mul GPR:$in, GPR:$in)'.
227 class CheckSameMatcherNode : public MatcherNode {
228 unsigned MatchNumber;
230 CheckSameMatcherNode(unsigned matchnumber)
231 : MatcherNode(CheckSame), MatchNumber(matchnumber) {}
233 unsigned getMatchNumber() const { return MatchNumber; }
235 static inline bool classof(const MatcherNode *N) {
236 return N->getKind() == CheckSame;
239 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
242 /// CheckPatternPredicateMatcherNode - This checks the target-specific predicate
243 /// to see if the entire pattern is capable of matching. This predicate does
244 /// not take a node as input. This is used for subtarget feature checks etc.
245 class CheckPatternPredicateMatcherNode : public MatcherNode {
246 std::string Predicate;
248 CheckPatternPredicateMatcherNode(StringRef predicate)
249 : MatcherNode(CheckPatternPredicate), Predicate(predicate) {}
251 StringRef getPredicate() const { return Predicate; }
253 static inline bool classof(const MatcherNode *N) {
254 return N->getKind() == CheckPatternPredicate;
257 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
260 /// CheckPredicateMatcherNode - This checks the target-specific predicate to
261 /// see if the node is acceptable.
262 class CheckPredicateMatcherNode : public MatcherNode {
265 CheckPredicateMatcherNode(StringRef predname)
266 : MatcherNode(CheckPredicate), PredName(predname) {}
268 StringRef getPredicateName() const { return PredName; }
270 static inline bool classof(const MatcherNode *N) {
271 return N->getKind() == CheckPredicate;
274 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
278 /// CheckOpcodeMatcherNode - This checks to see if the current node has the
279 /// specified opcode, if not it fails to match.
280 class CheckOpcodeMatcherNode : public MatcherNode {
281 StringRef OpcodeName;
283 CheckOpcodeMatcherNode(StringRef opcodename)
284 : MatcherNode(CheckOpcode), OpcodeName(opcodename) {}
286 StringRef getOpcodeName() const { return OpcodeName; }
288 static inline bool classof(const MatcherNode *N) {
289 return N->getKind() == CheckOpcode;
292 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
295 /// CheckMultiOpcodeMatcherNode - This checks to see if the current node has one
296 /// of the specified opcode, if not it fails to match.
297 class CheckMultiOpcodeMatcherNode : public MatcherNode {
298 SmallVector<StringRef, 4> OpcodeNames;
300 CheckMultiOpcodeMatcherNode(const StringRef *opcodes, unsigned numops)
301 : MatcherNode(CheckMultiOpcode), OpcodeNames(opcodes, opcodes+numops) {}
303 unsigned getNumOpcodeNames() const { return OpcodeNames.size(); }
304 StringRef getOpcodeName(unsigned i) const { return OpcodeNames[i]; }
306 static inline bool classof(const MatcherNode *N) {
307 return N->getKind() == CheckMultiOpcode;
310 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
315 /// CheckTypeMatcherNode - This checks to see if the current node has the
316 /// specified type, if not it fails to match.
317 class CheckTypeMatcherNode : public MatcherNode {
318 MVT::SimpleValueType Type;
320 CheckTypeMatcherNode(MVT::SimpleValueType type)
321 : MatcherNode(CheckType), Type(type) {}
323 MVT::SimpleValueType getType() const { return Type; }
325 static inline bool classof(const MatcherNode *N) {
326 return N->getKind() == CheckType;
329 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
332 /// CheckIntegerMatcherNode - This checks to see if the current node is a
333 /// ConstantSDNode with the specified integer value, if not it fails to match.
334 class CheckIntegerMatcherNode : public MatcherNode {
337 CheckIntegerMatcherNode(int64_t value)
338 : MatcherNode(CheckInteger), Value(value) {}
340 int64_t getValue() const { return Value; }
342 static inline bool classof(const MatcherNode *N) {
343 return N->getKind() == CheckInteger;
346 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
349 /// CheckCondCodeMatcherNode - This checks to see if the current node is a
350 /// CondCodeSDNode with the specified condition, if not it fails to match.
351 class CheckCondCodeMatcherNode : public MatcherNode {
352 StringRef CondCodeName;
354 CheckCondCodeMatcherNode(StringRef condcodename)
355 : MatcherNode(CheckCondCode), CondCodeName(condcodename) {}
357 StringRef getCondCodeName() const { return CondCodeName; }
359 static inline bool classof(const MatcherNode *N) {
360 return N->getKind() == CheckCondCode;
363 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
366 /// CheckValueTypeMatcherNode - This checks to see if the current node is a
367 /// VTSDNode with the specified type, if not it fails to match.
368 class CheckValueTypeMatcherNode : public MatcherNode {
371 CheckValueTypeMatcherNode(StringRef type_name)
372 : MatcherNode(CheckValueType), TypeName(type_name) {}
374 StringRef getTypeName() const { return TypeName; }
376 static inline bool classof(const MatcherNode *N) {
377 return N->getKind() == CheckValueType;
380 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
385 /// CheckComplexPatMatcherNode - This node runs the specified ComplexPattern on
386 /// the current node.
387 class CheckComplexPatMatcherNode : public MatcherNode {
388 const ComplexPattern &Pattern;
390 CheckComplexPatMatcherNode(const ComplexPattern &pattern)
391 : MatcherNode(CheckComplexPat), Pattern(pattern) {}
393 const ComplexPattern &getPattern() const { return Pattern; }
395 static inline bool classof(const MatcherNode *N) {
396 return N->getKind() == CheckComplexPat;
399 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
402 /// CheckAndImmMatcherNode - This checks to see if the current node is an 'and'
403 /// with something equivalent to the specified immediate.
404 class CheckAndImmMatcherNode : public MatcherNode {
407 CheckAndImmMatcherNode(int64_t value)
408 : MatcherNode(CheckAndImm), Value(value) {}
410 int64_t getValue() const { return Value; }
412 static inline bool classof(const MatcherNode *N) {
413 return N->getKind() == CheckAndImm;
416 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
419 /// CheckOrImmMatcherNode - This checks to see if the current node is an 'and'
420 /// with something equivalent to the specified immediate.
421 class CheckOrImmMatcherNode : public MatcherNode {
424 CheckOrImmMatcherNode(int64_t value)
425 : MatcherNode(CheckOrImm), Value(value) {}
427 int64_t getValue() const { return Value; }
429 static inline bool classof(const MatcherNode *N) {
430 return N->getKind() == CheckOrImm;
433 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
436 /// CheckFoldableChainNodeMatcherNode - This checks to see if the current node
437 /// (which defines a chain operand) is safe to fold into a larger pattern.
438 class CheckFoldableChainNodeMatcherNode : public MatcherNode {
440 CheckFoldableChainNodeMatcherNode()
441 : MatcherNode(CheckFoldableChainNode) {}
443 static inline bool classof(const MatcherNode *N) {
444 return N->getKind() == CheckFoldableChainNode;
447 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
450 /// CheckChainCompatibleMatcherNode - Verify that the current node's chain
451 /// operand is 'compatible' with the specified recorded node's.
452 class CheckChainCompatibleMatcherNode : public MatcherNode {
455 CheckChainCompatibleMatcherNode(unsigned previousop)
456 : MatcherNode(CheckChainCompatible), PreviousOp(previousop) {}
458 unsigned getPreviousOp() const { return PreviousOp; }
460 static inline bool classof(const MatcherNode *N) {
461 return N->getKind() == CheckChainCompatible;
464 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
467 /// EmitIntegerMatcherNode - This creates a new TargetConstant.
468 class EmitIntegerMatcherNode : public MatcherNode {
470 MVT::SimpleValueType VT;
472 EmitIntegerMatcherNode(int64_t val, MVT::SimpleValueType vt)
473 : MatcherNode(EmitInteger), Val(val), VT(vt) {}
475 int64_t getValue() const { return Val; }
476 MVT::SimpleValueType getVT() const { return VT; }
478 static inline bool classof(const MatcherNode *N) {
479 return N->getKind() == EmitInteger;
482 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
485 /// EmitStringIntegerMatcherNode - A target constant whose value is represented
487 class EmitStringIntegerMatcherNode : public MatcherNode {
489 MVT::SimpleValueType VT;
491 EmitStringIntegerMatcherNode(const std::string &val, MVT::SimpleValueType vt)
492 : MatcherNode(EmitStringInteger), Val(val), VT(vt) {}
494 const std::string &getValue() const { return Val; }
495 MVT::SimpleValueType getVT() const { return VT; }
497 static inline bool classof(const MatcherNode *N) {
498 return N->getKind() == EmitStringInteger;
501 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
504 /// EmitRegisterMatcherNode - This creates a new TargetConstant.
505 class EmitRegisterMatcherNode : public MatcherNode {
506 /// Reg - The def for the register that we're emitting. If this is null, then
507 /// this is a reference to zero_reg.
509 MVT::SimpleValueType VT;
511 EmitRegisterMatcherNode(Record *reg, MVT::SimpleValueType vt)
512 : MatcherNode(EmitRegister), Reg(reg), VT(vt) {}
514 Record *getReg() const { return Reg; }
515 MVT::SimpleValueType getVT() const { return VT; }
517 static inline bool classof(const MatcherNode *N) {
518 return N->getKind() == EmitRegister;
521 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
524 /// EmitConvertToTargetMatcherNode - Emit an operation that reads a specified
525 /// recorded node and converts it from being a ISD::Constant to
526 /// ISD::TargetConstant, likewise for ConstantFP.
527 class EmitConvertToTargetMatcherNode : public MatcherNode {
530 EmitConvertToTargetMatcherNode(unsigned slot)
531 : MatcherNode(EmitConvertToTarget), Slot(slot) {}
533 unsigned getSlot() const { return Slot; }
535 static inline bool classof(const MatcherNode *N) {
536 return N->getKind() == EmitConvertToTarget;
539 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
542 /// EmitMergeInputChainsMatcherNode - Emit a node that merges a list of input
543 /// chains together with a token factor. The list of nodes are the nodes in the
544 /// matched pattern that have chain input/outputs. This node adds all input
545 /// chains of these nodes if they are not themselves a node in the pattern.
546 class EmitMergeInputChainsMatcherNode : public MatcherNode {
547 SmallVector<unsigned, 3> ChainNodes;
549 EmitMergeInputChainsMatcherNode(const unsigned *nodes, unsigned NumNodes)
550 : MatcherNode(EmitMergeInputChains), ChainNodes(nodes, nodes+NumNodes) {}
552 unsigned getNumNodes() const { return ChainNodes.size(); }
554 unsigned getNode(unsigned i) const {
555 assert(i < ChainNodes.size());
556 return ChainNodes[i];
559 static inline bool classof(const MatcherNode *N) {
560 return N->getKind() == EmitMergeInputChains;
563 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
566 /// EmitCopyToRegMatcherNode - Emit a CopyToReg node from a value to a physreg,
567 /// pushing the chain and flag results.
569 class EmitCopyToRegMatcherNode : public MatcherNode {
570 unsigned SrcSlot; // Value to copy into the physreg.
573 EmitCopyToRegMatcherNode(unsigned srcSlot, Record *destPhysReg)
574 : MatcherNode(EmitCopyToReg), SrcSlot(srcSlot), DestPhysReg(destPhysReg) {}
576 unsigned getSrcSlot() const { return SrcSlot; }
577 Record *getDestPhysReg() const { return DestPhysReg; }
579 static inline bool classof(const MatcherNode *N) {
580 return N->getKind() == EmitCopyToReg;
583 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
588 /// EmitNodeXFormMatcherNode - Emit an operation that runs an SDNodeXForm on a
589 /// recorded node and records the result.
590 class EmitNodeXFormMatcherNode : public MatcherNode {
594 EmitNodeXFormMatcherNode(unsigned slot, Record *nodeXForm)
595 : MatcherNode(EmitNodeXForm), Slot(slot), NodeXForm(nodeXForm) {}
597 unsigned getSlot() const { return Slot; }
598 Record *getNodeXForm() const { return NodeXForm; }
600 static inline bool classof(const MatcherNode *N) {
601 return N->getKind() == EmitNodeXForm;
604 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
607 /// EmitNodeMatcherNode - This signals a successful match and generates a node.
608 class EmitNodeMatcherNode : public MatcherNode {
609 std::string OpcodeName;
610 const SmallVector<MVT::SimpleValueType, 3> VTs;
611 const SmallVector<unsigned, 6> Operands;
612 bool HasChain, HasFlag, HasMemRefs;
614 /// NumFixedArityOperands - If this is a fixed arity node, this is set to -1.
615 /// If this is a varidic node, this is set to the number of fixed arity
616 /// operands in the root of the pattern. The rest are appended to this node.
617 int NumFixedArityOperands;
619 EmitNodeMatcherNode(const std::string &opcodeName,
620 const MVT::SimpleValueType *vts, unsigned numvts,
621 const unsigned *operands, unsigned numops,
622 bool hasChain, bool hasFlag, bool hasmemrefs,
623 int numfixedarityoperands)
624 : MatcherNode(EmitNode), OpcodeName(opcodeName),
625 VTs(vts, vts+numvts), Operands(operands, operands+numops),
626 HasChain(hasChain), HasFlag(hasFlag), HasMemRefs(hasmemrefs),
627 NumFixedArityOperands(numfixedarityoperands) {}
629 const std::string &getOpcodeName() const { return OpcodeName; }
631 unsigned getNumVTs() const { return VTs.size(); }
632 MVT::SimpleValueType getVT(unsigned i) const {
633 assert(i < VTs.size());
637 unsigned getNumOperands() const { return Operands.size(); }
638 unsigned getOperand(unsigned i) const {
639 assert(i < Operands.size());
643 bool hasChain() const { return HasChain; }
644 bool hasFlag() const { return HasFlag; }
645 bool hasMemRefs() const { return HasMemRefs; }
646 int getNumFixedArityOperands() const { return NumFixedArityOperands; }
648 static inline bool classof(const MatcherNode *N) {
649 return N->getKind() == EmitNode;
652 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
655 /// MarkFlagResultsMatcherNode - This node indicates which non-root nodes in the
656 /// pattern produce flags. This allows CompleteMatchMatcherNode to update them
657 /// with the output flag of the resultant code.
658 class MarkFlagResultsMatcherNode : public MatcherNode {
659 SmallVector<unsigned, 3> FlagResultNodes;
661 MarkFlagResultsMatcherNode(const unsigned *nodes, unsigned NumNodes)
662 : MatcherNode(MarkFlagResults), FlagResultNodes(nodes, nodes+NumNodes) {}
664 unsigned getNumNodes() const { return FlagResultNodes.size(); }
666 unsigned getNode(unsigned i) const {
667 assert(i < FlagResultNodes.size());
668 return FlagResultNodes[i];
671 static inline bool classof(const MatcherNode *N) {
672 return N->getKind() == MarkFlagResults;
675 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
678 /// CompleteMatchMatcherNode - Complete a match by replacing the results of the
679 /// pattern with the newly generated nodes. This also prints a comment
680 /// indicating the source and dest patterns.
681 class CompleteMatchMatcherNode : public MatcherNode {
682 SmallVector<unsigned, 2> Results;
683 const PatternToMatch &Pattern;
685 CompleteMatchMatcherNode(const unsigned *results, unsigned numresults,
686 const PatternToMatch &pattern)
687 : MatcherNode(CompleteMatch), Results(results, results+numresults),
690 unsigned getNumResults() const { return Results.size(); }
691 unsigned getResult(unsigned R) const { return Results[R]; }
692 const PatternToMatch &getPattern() const { return Pattern; }
694 static inline bool classof(const MatcherNode *N) {
695 return N->getKind() == CompleteMatch;
698 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
702 } // end namespace llvm