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 Matcher *ConvertPatternToMatcher(const PatternToMatch &Pattern,
28 const CodeGenDAGPatterns &CGP);
29 Matcher *OptimizeMatcher(Matcher *Matcher);
30 void EmitMatcherTable(const Matcher *Matcher, raw_ostream &OS);
33 /// Matcher - 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<Matcher> Next;
41 // Matcher state manipulation.
42 Scope, // 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 CheckChildType, // Fail if child has wrong type.
58 CheckInteger, // Fail if wrong val.
59 CheckCondCode, // Fail if not condcode.
64 CheckFoldableChainNode,
67 // Node creation/emisssion.
68 EmitInteger, // Create a TargetConstant
69 EmitStringInteger, // Create a TargetConstant from a string.
70 EmitRegister, // Create a register.
71 EmitConvertToTarget, // Convert a imm/fpimm to target imm/fpimm
72 EmitMergeInputChains, // Merge together a chains for an input.
73 EmitCopyToReg, // Emit a copytoreg into a physreg.
74 EmitNode, // Create a DAG node
75 EmitNodeXForm, // Run a SDNodeXForm
76 MarkFlagResults, // Indicate which interior nodes have flag results.
77 CompleteMatch // Finish a match and update the results.
82 Matcher(KindTy K) : Kind(K) {}
86 KindTy getKind() const { return Kind; }
88 Matcher *getNext() { return Next.get(); }
89 const Matcher *getNext() const { return Next.get(); }
90 void setNext(Matcher *C) { Next.reset(C); }
91 Matcher *takeNext() { return Next.take(); }
93 OwningPtr<Matcher> &getNextPtr() { return Next; }
95 static inline bool classof(const Matcher *) { return true; }
97 bool isEqual(const Matcher *M) const {
98 if (getKind() != M->getKind()) return false;
99 return isEqualImpl(M);
102 unsigned getHash() const {
103 return (getHashImpl() << 4) ^ getKind();
106 void print(raw_ostream &OS, unsigned indent = 0) const;
109 virtual void printImpl(raw_ostream &OS, unsigned indent) const = 0;
110 virtual bool isEqualImpl(const Matcher *M) const = 0;
111 virtual unsigned getHashImpl() const = 0;
114 /// ScopeMatcher - This attempts to match each of its children to find the first
115 /// one that successfully matches. If one child fails, it tries the next child.
116 /// If none of the children match then this check fails. It never has a 'next'.
117 class ScopeMatcher : public Matcher {
118 SmallVector<Matcher*, 4> Children;
120 ScopeMatcher(Matcher *const *children, unsigned numchildren)
121 : Matcher(Scope), Children(children, children+numchildren) {
123 virtual ~ScopeMatcher();
125 unsigned getNumChildren() const { return Children.size(); }
127 Matcher *getChild(unsigned i) { return Children[i]; }
128 const Matcher *getChild(unsigned i) const { return Children[i]; }
130 void resetChild(unsigned i, Matcher *N) {
135 Matcher *takeChild(unsigned i) {
136 Matcher *Res = Children[i];
141 static inline bool classof(const Matcher *N) {
142 return N->getKind() == Scope;
146 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
147 virtual bool isEqualImpl(const Matcher *M) const { return false; }
148 virtual unsigned getHashImpl() const { return 12312; }
151 /// RecordMatcher - Save the current node in the operand list.
152 class RecordMatcher : public Matcher {
153 /// WhatFor - This is a string indicating why we're recording this. This
154 /// should only be used for comment generation not anything semantic.
157 RecordMatcher(const std::string &whatfor)
158 : Matcher(RecordNode), WhatFor(whatfor) {}
160 const std::string &getWhatFor() const { return WhatFor; }
162 static inline bool classof(const Matcher *N) {
163 return N->getKind() == RecordNode;
167 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
168 virtual bool isEqualImpl(const Matcher *M) const { return true; }
169 virtual unsigned getHashImpl() const { return 0; }
172 /// RecordChildMatcher - Save a numbered child of the current node, or fail
173 /// the match if it doesn't exist. This is logically equivalent to:
174 /// MoveChild N + RecordNode + MoveParent.
175 class RecordChildMatcher : public Matcher {
178 /// WhatFor - This is a string indicating why we're recording this. This
179 /// should only be used for comment generation not anything semantic.
182 RecordChildMatcher(unsigned childno, const std::string &whatfor)
183 : Matcher(RecordChild), ChildNo(childno), WhatFor(whatfor) {}
185 unsigned getChildNo() const { return ChildNo; }
186 const std::string &getWhatFor() const { return WhatFor; }
188 static inline bool classof(const Matcher *N) {
189 return N->getKind() == RecordChild;
193 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
194 virtual bool isEqualImpl(const Matcher *M) const {
195 return cast<RecordChildMatcher>(M)->getChildNo() == getChildNo();
197 virtual unsigned getHashImpl() const { return getChildNo(); }
200 /// RecordMemRefMatcher - Save the current node's memref.
201 class RecordMemRefMatcher : public Matcher {
203 RecordMemRefMatcher() : Matcher(RecordMemRef) {}
205 static inline bool classof(const Matcher *N) {
206 return N->getKind() == RecordMemRef;
210 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
211 virtual bool isEqualImpl(const Matcher *M) const { return true; }
212 virtual unsigned getHashImpl() const { return 0; }
216 /// CaptureFlagInputMatcher - If the current record has a flag input, record
217 /// it so that it is used as an input to the generated code.
218 class CaptureFlagInputMatcher : public Matcher {
220 CaptureFlagInputMatcher() : Matcher(CaptureFlagInput) {}
222 static inline bool classof(const Matcher *N) {
223 return N->getKind() == CaptureFlagInput;
227 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
228 virtual bool isEqualImpl(const Matcher *M) const { return true; }
229 virtual unsigned getHashImpl() const { return 0; }
232 /// MoveChildMatcher - This tells the interpreter to move into the
233 /// specified child node.
234 class MoveChildMatcher : public Matcher {
237 MoveChildMatcher(unsigned childNo) : Matcher(MoveChild), ChildNo(childNo) {}
239 unsigned getChildNo() const { return ChildNo; }
241 static inline bool classof(const Matcher *N) {
242 return N->getKind() == MoveChild;
246 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
247 virtual bool isEqualImpl(const Matcher *M) const {
248 return cast<MoveChildMatcher>(M)->getChildNo() == getChildNo();
250 virtual unsigned getHashImpl() const { return getChildNo(); }
253 /// MoveParentMatcher - This tells the interpreter to move to the parent
254 /// of the current node.
255 class MoveParentMatcher : public Matcher {
257 MoveParentMatcher() : Matcher(MoveParent) {}
259 static inline bool classof(const Matcher *N) {
260 return N->getKind() == MoveParent;
264 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
265 virtual bool isEqualImpl(const Matcher *M) const { return true; }
266 virtual unsigned getHashImpl() const { return 0; }
269 /// CheckSameMatcher - This checks to see if this node is exactly the same
270 /// node as the specified match that was recorded with 'Record'. This is used
271 /// when patterns have the same name in them, like '(mul GPR:$in, GPR:$in)'.
272 class CheckSameMatcher : public Matcher {
273 unsigned MatchNumber;
275 CheckSameMatcher(unsigned matchnumber)
276 : Matcher(CheckSame), MatchNumber(matchnumber) {}
278 unsigned getMatchNumber() const { return MatchNumber; }
280 static inline bool classof(const Matcher *N) {
281 return N->getKind() == CheckSame;
285 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
286 virtual bool isEqualImpl(const Matcher *M) const {
287 return cast<CheckSameMatcher>(M)->getMatchNumber() == getMatchNumber();
289 virtual unsigned getHashImpl() const { return getMatchNumber(); }
292 /// CheckPatternPredicateMatcher - This checks the target-specific predicate
293 /// to see if the entire pattern is capable of matching. This predicate does
294 /// not take a node as input. This is used for subtarget feature checks etc.
295 class CheckPatternPredicateMatcher : public Matcher {
296 std::string Predicate;
298 CheckPatternPredicateMatcher(StringRef predicate)
299 : Matcher(CheckPatternPredicate), Predicate(predicate) {}
301 StringRef getPredicate() const { return Predicate; }
303 static inline bool classof(const Matcher *N) {
304 return N->getKind() == CheckPatternPredicate;
308 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
309 virtual bool isEqualImpl(const Matcher *M) const {
310 return cast<CheckPatternPredicateMatcher>(M)->getPredicate() == Predicate;
312 virtual unsigned getHashImpl() const;
315 /// CheckPredicateMatcher - This checks the target-specific predicate to
316 /// see if the node is acceptable.
317 class CheckPredicateMatcher : public Matcher {
320 CheckPredicateMatcher(StringRef predname)
321 : Matcher(CheckPredicate), PredName(predname) {}
323 StringRef getPredicateName() const { return PredName; }
325 static inline bool classof(const Matcher *N) {
326 return N->getKind() == CheckPredicate;
330 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
331 virtual bool isEqualImpl(const Matcher *M) const {
332 return cast<CheckPredicateMatcher>(M)->PredName == PredName;
334 virtual unsigned getHashImpl() const;
338 /// CheckOpcodeMatcher - This checks to see if the current node has the
339 /// specified opcode, if not it fails to match.
340 class CheckOpcodeMatcher : public Matcher {
341 StringRef OpcodeName;
343 CheckOpcodeMatcher(StringRef opcodename)
344 : Matcher(CheckOpcode), OpcodeName(opcodename) {}
346 StringRef getOpcodeName() const { return OpcodeName; }
348 static inline bool classof(const Matcher *N) {
349 return N->getKind() == CheckOpcode;
353 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
354 virtual bool isEqualImpl(const Matcher *M) const {
355 return cast<CheckOpcodeMatcher>(M)->OpcodeName == OpcodeName;
357 virtual unsigned getHashImpl() const;
360 /// CheckMultiOpcodeMatcher - This checks to see if the current node has one
361 /// of the specified opcode, if not it fails to match.
362 class CheckMultiOpcodeMatcher : public Matcher {
363 SmallVector<StringRef, 4> OpcodeNames;
365 CheckMultiOpcodeMatcher(const StringRef *opcodes, unsigned numops)
366 : Matcher(CheckMultiOpcode), OpcodeNames(opcodes, opcodes+numops) {}
368 unsigned getNumOpcodeNames() const { return OpcodeNames.size(); }
369 StringRef getOpcodeName(unsigned i) const { return OpcodeNames[i]; }
371 static inline bool classof(const Matcher *N) {
372 return N->getKind() == CheckMultiOpcode;
376 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
377 virtual bool isEqualImpl(const Matcher *M) const {
378 return cast<CheckMultiOpcodeMatcher>(M)->OpcodeNames == OpcodeNames;
380 virtual unsigned getHashImpl() const;
385 /// CheckTypeMatcher - This checks to see if the current node has the
386 /// specified type, if not it fails to match.
387 class CheckTypeMatcher : public Matcher {
388 MVT::SimpleValueType Type;
390 CheckTypeMatcher(MVT::SimpleValueType type)
391 : Matcher(CheckType), Type(type) {}
393 MVT::SimpleValueType getType() const { return Type; }
395 static inline bool classof(const Matcher *N) {
396 return N->getKind() == CheckType;
400 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
401 virtual bool isEqualImpl(const Matcher *M) const {
402 return cast<CheckTypeMatcher>(this)->Type == Type;
404 virtual unsigned getHashImpl() const { return Type; }
407 /// CheckChildTypeMatcher - This checks to see if a child node has the
408 /// specified type, if not it fails to match.
409 class CheckChildTypeMatcher : public Matcher {
411 MVT::SimpleValueType Type;
413 CheckChildTypeMatcher(unsigned childno, MVT::SimpleValueType type)
414 : Matcher(CheckChildType), ChildNo(childno), Type(type) {}
416 unsigned getChildNo() const { return ChildNo; }
417 MVT::SimpleValueType getType() const { return Type; }
419 static inline bool classof(const Matcher *N) {
420 return N->getKind() == CheckChildType;
424 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
425 virtual bool isEqualImpl(const Matcher *M) const {
426 return cast<CheckChildTypeMatcher>(M)->ChildNo == ChildNo &&
427 cast<CheckChildTypeMatcher>(M)->Type == Type;
429 virtual unsigned getHashImpl() const { return (Type << 3) | ChildNo; }
433 /// CheckIntegerMatcher - This checks to see if the current node is a
434 /// ConstantSDNode with the specified integer value, if not it fails to match.
435 class CheckIntegerMatcher : public Matcher {
438 CheckIntegerMatcher(int64_t value)
439 : Matcher(CheckInteger), Value(value) {}
441 int64_t getValue() const { return Value; }
443 static inline bool classof(const Matcher *N) {
444 return N->getKind() == CheckInteger;
448 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
449 virtual bool isEqualImpl(const Matcher *M) const {
450 return cast<CheckIntegerMatcher>(M)->Value == Value;
452 virtual unsigned getHashImpl() const { return Value; }
455 /// CheckCondCodeMatcher - This checks to see if the current node is a
456 /// CondCodeSDNode with the specified condition, if not it fails to match.
457 class CheckCondCodeMatcher : public Matcher {
458 StringRef CondCodeName;
460 CheckCondCodeMatcher(StringRef condcodename)
461 : Matcher(CheckCondCode), CondCodeName(condcodename) {}
463 StringRef getCondCodeName() const { return CondCodeName; }
465 static inline bool classof(const Matcher *N) {
466 return N->getKind() == CheckCondCode;
470 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
471 virtual bool isEqualImpl(const Matcher *M) const {
472 return cast<CheckCondCodeMatcher>(M)->CondCodeName == CondCodeName;
474 virtual unsigned getHashImpl() const;
477 /// CheckValueTypeMatcher - This checks to see if the current node is a
478 /// VTSDNode with the specified type, if not it fails to match.
479 class CheckValueTypeMatcher : public Matcher {
482 CheckValueTypeMatcher(StringRef type_name)
483 : Matcher(CheckValueType), TypeName(type_name) {}
485 StringRef getTypeName() const { return TypeName; }
487 static inline bool classof(const Matcher *N) {
488 return N->getKind() == CheckValueType;
492 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
493 virtual bool isEqualImpl(const Matcher *M) const {
494 return cast<CheckValueTypeMatcher>(M)->TypeName == TypeName;
496 virtual unsigned getHashImpl() const;
501 /// CheckComplexPatMatcher - This node runs the specified ComplexPattern on
502 /// the current node.
503 class CheckComplexPatMatcher : public Matcher {
504 const ComplexPattern &Pattern;
506 CheckComplexPatMatcher(const ComplexPattern &pattern)
507 : Matcher(CheckComplexPat), Pattern(pattern) {}
509 const ComplexPattern &getPattern() const { return Pattern; }
511 static inline bool classof(const Matcher *N) {
512 return N->getKind() == CheckComplexPat;
516 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
517 virtual bool isEqualImpl(const Matcher *M) const {
518 return &cast<CheckComplexPatMatcher>(M)->Pattern == &Pattern;
520 virtual unsigned getHashImpl() const {
521 return (unsigned)(intptr_t)&Pattern;
525 /// CheckAndImmMatcher - This checks to see if the current node is an 'and'
526 /// with something equivalent to the specified immediate.
527 class CheckAndImmMatcher : public Matcher {
530 CheckAndImmMatcher(int64_t value)
531 : Matcher(CheckAndImm), Value(value) {}
533 int64_t getValue() const { return Value; }
535 static inline bool classof(const Matcher *N) {
536 return N->getKind() == CheckAndImm;
540 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
541 virtual bool isEqualImpl(const Matcher *M) const {
542 return cast<CheckAndImmMatcher>(M)->Value == Value;
544 virtual unsigned getHashImpl() const { return Value; }
547 /// CheckOrImmMatcher - This checks to see if the current node is an 'and'
548 /// with something equivalent to the specified immediate.
549 class CheckOrImmMatcher : public Matcher {
552 CheckOrImmMatcher(int64_t value)
553 : Matcher(CheckOrImm), Value(value) {}
555 int64_t getValue() const { return Value; }
557 static inline bool classof(const Matcher *N) {
558 return N->getKind() == CheckOrImm;
562 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
563 virtual bool isEqualImpl(const Matcher *M) const {
564 return cast<CheckOrImmMatcher>(M)->Value == Value;
566 virtual unsigned getHashImpl() const { return Value; }
569 /// CheckFoldableChainNodeMatcher - This checks to see if the current node
570 /// (which defines a chain operand) is safe to fold into a larger pattern.
571 class CheckFoldableChainNodeMatcher : public Matcher {
573 CheckFoldableChainNodeMatcher()
574 : Matcher(CheckFoldableChainNode) {}
576 static inline bool classof(const Matcher *N) {
577 return N->getKind() == CheckFoldableChainNode;
581 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
582 virtual bool isEqualImpl(const Matcher *M) const { return true; }
583 virtual unsigned getHashImpl() const { return 0; }
586 /// CheckChainCompatibleMatcher - Verify that the current node's chain
587 /// operand is 'compatible' with the specified recorded node's.
588 class CheckChainCompatibleMatcher : public Matcher {
591 CheckChainCompatibleMatcher(unsigned previousop)
592 : Matcher(CheckChainCompatible), PreviousOp(previousop) {}
594 unsigned getPreviousOp() const { return PreviousOp; }
596 static inline bool classof(const Matcher *N) {
597 return N->getKind() == CheckChainCompatible;
601 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
602 virtual bool isEqualImpl(const Matcher *M) const {
603 return cast<CheckChainCompatibleMatcher>(this)->PreviousOp == PreviousOp;
605 virtual unsigned getHashImpl() const { return PreviousOp; }
608 /// EmitIntegerMatcher - This creates a new TargetConstant.
609 class EmitIntegerMatcher : public Matcher {
611 MVT::SimpleValueType VT;
613 EmitIntegerMatcher(int64_t val, MVT::SimpleValueType vt)
614 : Matcher(EmitInteger), Val(val), VT(vt) {}
616 int64_t getValue() const { return Val; }
617 MVT::SimpleValueType getVT() const { return VT; }
619 static inline bool classof(const Matcher *N) {
620 return N->getKind() == EmitInteger;
624 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
625 virtual bool isEqualImpl(const Matcher *M) const {
626 return cast<EmitIntegerMatcher>(M)->Val == Val &&
627 cast<EmitIntegerMatcher>(M)->VT == VT;
629 virtual unsigned getHashImpl() const { return (Val << 4) | VT; }
632 /// EmitStringIntegerMatcher - A target constant whose value is represented
634 class EmitStringIntegerMatcher : public Matcher {
636 MVT::SimpleValueType VT;
638 EmitStringIntegerMatcher(const std::string &val, MVT::SimpleValueType vt)
639 : Matcher(EmitStringInteger), Val(val), VT(vt) {}
641 const std::string &getValue() const { return Val; }
642 MVT::SimpleValueType getVT() const { return VT; }
644 static inline bool classof(const Matcher *N) {
645 return N->getKind() == EmitStringInteger;
649 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
650 virtual bool isEqualImpl(const Matcher *M) const {
651 return cast<EmitStringIntegerMatcher>(M)->Val == Val &&
652 cast<EmitStringIntegerMatcher>(M)->VT == VT;
654 virtual unsigned getHashImpl() const;
657 /// EmitRegisterMatcher - This creates a new TargetConstant.
658 class EmitRegisterMatcher : public Matcher {
659 /// Reg - The def for the register that we're emitting. If this is null, then
660 /// this is a reference to zero_reg.
662 MVT::SimpleValueType VT;
664 EmitRegisterMatcher(Record *reg, MVT::SimpleValueType vt)
665 : Matcher(EmitRegister), Reg(reg), VT(vt) {}
667 Record *getReg() const { return Reg; }
668 MVT::SimpleValueType getVT() const { return VT; }
670 static inline bool classof(const Matcher *N) {
671 return N->getKind() == EmitRegister;
675 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
676 virtual bool isEqualImpl(const Matcher *M) const {
677 return cast<EmitRegisterMatcher>(M)->Reg == Reg &&
678 cast<EmitRegisterMatcher>(M)->VT == VT;
680 virtual unsigned getHashImpl() const {
681 return ((unsigned)(intptr_t)Reg) << 4 | VT;
685 /// EmitConvertToTargetMatcher - Emit an operation that reads a specified
686 /// recorded node and converts it from being a ISD::Constant to
687 /// ISD::TargetConstant, likewise for ConstantFP.
688 class EmitConvertToTargetMatcher : public Matcher {
691 EmitConvertToTargetMatcher(unsigned slot)
692 : Matcher(EmitConvertToTarget), Slot(slot) {}
694 unsigned getSlot() const { return Slot; }
696 static inline bool classof(const Matcher *N) {
697 return N->getKind() == EmitConvertToTarget;
701 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
702 virtual bool isEqualImpl(const Matcher *M) const {
703 return cast<EmitConvertToTargetMatcher>(M)->Slot == Slot;
705 virtual unsigned getHashImpl() const { return Slot; }
708 /// EmitMergeInputChainsMatcher - Emit a node that merges a list of input
709 /// chains together with a token factor. The list of nodes are the nodes in the
710 /// matched pattern that have chain input/outputs. This node adds all input
711 /// chains of these nodes if they are not themselves a node in the pattern.
712 class EmitMergeInputChainsMatcher : public Matcher {
713 SmallVector<unsigned, 3> ChainNodes;
715 EmitMergeInputChainsMatcher(const unsigned *nodes, unsigned NumNodes)
716 : Matcher(EmitMergeInputChains), ChainNodes(nodes, nodes+NumNodes) {}
718 unsigned getNumNodes() const { return ChainNodes.size(); }
720 unsigned getNode(unsigned i) const {
721 assert(i < ChainNodes.size());
722 return ChainNodes[i];
725 static inline bool classof(const Matcher *N) {
726 return N->getKind() == EmitMergeInputChains;
730 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
731 virtual bool isEqualImpl(const Matcher *M) const {
732 return cast<EmitMergeInputChainsMatcher>(M)->ChainNodes == ChainNodes;
734 virtual unsigned getHashImpl() const;
737 /// EmitCopyToRegMatcher - Emit a CopyToReg node from a value to a physreg,
738 /// pushing the chain and flag results.
740 class EmitCopyToRegMatcher : public Matcher {
741 unsigned SrcSlot; // Value to copy into the physreg.
744 EmitCopyToRegMatcher(unsigned srcSlot, Record *destPhysReg)
745 : Matcher(EmitCopyToReg), SrcSlot(srcSlot), DestPhysReg(destPhysReg) {}
747 unsigned getSrcSlot() const { return SrcSlot; }
748 Record *getDestPhysReg() const { return DestPhysReg; }
750 static inline bool classof(const Matcher *N) {
751 return N->getKind() == EmitCopyToReg;
755 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
756 virtual bool isEqualImpl(const Matcher *M) const {
757 return cast<EmitCopyToRegMatcher>(M)->SrcSlot == SrcSlot &&
758 cast<EmitCopyToRegMatcher>(M)->DestPhysReg == DestPhysReg;
760 virtual unsigned getHashImpl() const {
761 return SrcSlot ^ ((unsigned)(intptr_t)DestPhysReg << 4);
767 /// EmitNodeXFormMatcher - Emit an operation that runs an SDNodeXForm on a
768 /// recorded node and records the result.
769 class EmitNodeXFormMatcher : public Matcher {
773 EmitNodeXFormMatcher(unsigned slot, Record *nodeXForm)
774 : Matcher(EmitNodeXForm), Slot(slot), NodeXForm(nodeXForm) {}
776 unsigned getSlot() const { return Slot; }
777 Record *getNodeXForm() const { return NodeXForm; }
779 static inline bool classof(const Matcher *N) {
780 return N->getKind() == EmitNodeXForm;
784 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
785 virtual bool isEqualImpl(const Matcher *M) const {
786 return cast<EmitNodeXFormMatcher>(M)->Slot == Slot &&
787 cast<EmitNodeXFormMatcher>(M)->NodeXForm == NodeXForm;
789 virtual unsigned getHashImpl() const {
790 return Slot ^ ((unsigned)(intptr_t)NodeXForm << 4);
794 /// EmitNodeMatcher - This signals a successful match and generates a node.
795 class EmitNodeMatcher : public Matcher {
796 std::string OpcodeName;
797 const SmallVector<MVT::SimpleValueType, 3> VTs;
798 const SmallVector<unsigned, 6> Operands;
799 bool HasChain, HasFlag, HasMemRefs;
801 /// NumFixedArityOperands - If this is a fixed arity node, this is set to -1.
802 /// If this is a varidic node, this is set to the number of fixed arity
803 /// operands in the root of the pattern. The rest are appended to this node.
804 int NumFixedArityOperands;
806 EmitNodeMatcher(const std::string &opcodeName,
807 const MVT::SimpleValueType *vts, unsigned numvts,
808 const unsigned *operands, unsigned numops,
809 bool hasChain, bool hasFlag, bool hasmemrefs,
810 int numfixedarityoperands)
811 : Matcher(EmitNode), OpcodeName(opcodeName),
812 VTs(vts, vts+numvts), Operands(operands, operands+numops),
813 HasChain(hasChain), HasFlag(hasFlag), HasMemRefs(hasmemrefs),
814 NumFixedArityOperands(numfixedarityoperands) {}
816 const std::string &getOpcodeName() const { return OpcodeName; }
818 unsigned getNumVTs() const { return VTs.size(); }
819 MVT::SimpleValueType getVT(unsigned i) const {
820 assert(i < VTs.size());
824 unsigned getNumOperands() const { return Operands.size(); }
825 unsigned getOperand(unsigned i) const {
826 assert(i < Operands.size());
830 bool hasChain() const { return HasChain; }
831 bool hasFlag() const { return HasFlag; }
832 bool hasMemRefs() const { return HasMemRefs; }
833 int getNumFixedArityOperands() const { return NumFixedArityOperands; }
835 static inline bool classof(const Matcher *N) {
836 return N->getKind() == EmitNode;
840 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
841 virtual bool isEqualImpl(const Matcher *M) const;
842 virtual unsigned getHashImpl() const;
845 /// MarkFlagResultsMatcher - This node indicates which non-root nodes in the
846 /// pattern produce flags. This allows CompleteMatchMatcher to update them
847 /// with the output flag of the resultant code.
848 class MarkFlagResultsMatcher : public Matcher {
849 SmallVector<unsigned, 3> FlagResultNodes;
851 MarkFlagResultsMatcher(const unsigned *nodes, unsigned NumNodes)
852 : Matcher(MarkFlagResults), FlagResultNodes(nodes, nodes+NumNodes) {}
854 unsigned getNumNodes() const { return FlagResultNodes.size(); }
856 unsigned getNode(unsigned i) const {
857 assert(i < FlagResultNodes.size());
858 return FlagResultNodes[i];
861 static inline bool classof(const Matcher *N) {
862 return N->getKind() == MarkFlagResults;
866 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
867 virtual bool isEqualImpl(const Matcher *M) const {
868 return cast<MarkFlagResultsMatcher>(M)->FlagResultNodes == FlagResultNodes;
870 virtual unsigned getHashImpl() const;
873 /// CompleteMatchMatcher - Complete a match by replacing the results of the
874 /// pattern with the newly generated nodes. This also prints a comment
875 /// indicating the source and dest patterns.
876 class CompleteMatchMatcher : public Matcher {
877 SmallVector<unsigned, 2> Results;
878 const PatternToMatch &Pattern;
880 CompleteMatchMatcher(const unsigned *results, unsigned numresults,
881 const PatternToMatch &pattern)
882 : Matcher(CompleteMatch), Results(results, results+numresults),
885 unsigned getNumResults() const { return Results.size(); }
886 unsigned getResult(unsigned R) const { return Results[R]; }
887 const PatternToMatch &getPattern() const { return Pattern; }
889 static inline bool classof(const Matcher *N) {
890 return N->getKind() == CompleteMatch;
894 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
895 virtual bool isEqualImpl(const Matcher *M) const {
896 return cast<CompleteMatchMatcher>(M)->Results == Results &&
897 &cast<CompleteMatchMatcher>(M)->Pattern == &Pattern;
899 virtual unsigned getHashImpl() const;
902 } // end namespace llvm