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 // Clear the high bit so we don't conflict with tombstones etc.
104 return ((getHashImpl() << 4) ^ getKind()) & (~0U>>1);
107 void print(raw_ostream &OS, unsigned indent = 0) const;
110 virtual void printImpl(raw_ostream &OS, unsigned indent) const = 0;
111 virtual bool isEqualImpl(const Matcher *M) const = 0;
112 virtual unsigned getHashImpl() const = 0;
115 /// ScopeMatcher - This attempts to match each of its children to find the first
116 /// one that successfully matches. If one child fails, it tries the next child.
117 /// If none of the children match then this check fails. It never has a 'next'.
118 class ScopeMatcher : public Matcher {
119 SmallVector<Matcher*, 4> Children;
121 ScopeMatcher(Matcher *const *children, unsigned numchildren)
122 : Matcher(Scope), Children(children, children+numchildren) {
124 virtual ~ScopeMatcher();
126 unsigned getNumChildren() const { return Children.size(); }
128 Matcher *getChild(unsigned i) { return Children[i]; }
129 const Matcher *getChild(unsigned i) const { return Children[i]; }
131 void resetChild(unsigned i, Matcher *N) {
136 Matcher *takeChild(unsigned i) {
137 Matcher *Res = Children[i];
142 void setNumChildren(unsigned NC) {
143 if (NC < Children.size()) {
144 // delete any children we're about to lose pointers to.
145 for (unsigned i = NC, e = Children.size(); i != e; ++i)
151 static inline bool classof(const Matcher *N) {
152 return N->getKind() == Scope;
156 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
157 virtual bool isEqualImpl(const Matcher *M) const { return false; }
158 virtual unsigned getHashImpl() const { return 12312; }
161 /// RecordMatcher - Save the current node in the operand list.
162 class RecordMatcher : public Matcher {
163 /// WhatFor - This is a string indicating why we're recording this. This
164 /// should only be used for comment generation not anything semantic.
167 RecordMatcher(const std::string &whatfor)
168 : Matcher(RecordNode), WhatFor(whatfor) {}
170 const std::string &getWhatFor() const { return WhatFor; }
172 static inline bool classof(const Matcher *N) {
173 return N->getKind() == RecordNode;
177 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
178 virtual bool isEqualImpl(const Matcher *M) const { return true; }
179 virtual unsigned getHashImpl() const { return 0; }
182 /// RecordChildMatcher - Save a numbered child of the current node, or fail
183 /// the match if it doesn't exist. This is logically equivalent to:
184 /// MoveChild N + RecordNode + MoveParent.
185 class RecordChildMatcher : public Matcher {
188 /// WhatFor - This is a string indicating why we're recording this. This
189 /// should only be used for comment generation not anything semantic.
192 RecordChildMatcher(unsigned childno, const std::string &whatfor)
193 : Matcher(RecordChild), ChildNo(childno), WhatFor(whatfor) {}
195 unsigned getChildNo() const { return ChildNo; }
196 const std::string &getWhatFor() const { return WhatFor; }
198 static inline bool classof(const Matcher *N) {
199 return N->getKind() == RecordChild;
203 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
204 virtual bool isEqualImpl(const Matcher *M) const {
205 return cast<RecordChildMatcher>(M)->getChildNo() == getChildNo();
207 virtual unsigned getHashImpl() const { return getChildNo(); }
210 /// RecordMemRefMatcher - Save the current node's memref.
211 class RecordMemRefMatcher : public Matcher {
213 RecordMemRefMatcher() : Matcher(RecordMemRef) {}
215 static inline bool classof(const Matcher *N) {
216 return N->getKind() == RecordMemRef;
220 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
221 virtual bool isEqualImpl(const Matcher *M) const { return true; }
222 virtual unsigned getHashImpl() const { return 0; }
226 /// CaptureFlagInputMatcher - If the current record has a flag input, record
227 /// it so that it is used as an input to the generated code.
228 class CaptureFlagInputMatcher : public Matcher {
230 CaptureFlagInputMatcher() : Matcher(CaptureFlagInput) {}
232 static inline bool classof(const Matcher *N) {
233 return N->getKind() == CaptureFlagInput;
237 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
238 virtual bool isEqualImpl(const Matcher *M) const { return true; }
239 virtual unsigned getHashImpl() const { return 0; }
242 /// MoveChildMatcher - This tells the interpreter to move into the
243 /// specified child node.
244 class MoveChildMatcher : public Matcher {
247 MoveChildMatcher(unsigned childNo) : Matcher(MoveChild), ChildNo(childNo) {}
249 unsigned getChildNo() const { return ChildNo; }
251 static inline bool classof(const Matcher *N) {
252 return N->getKind() == MoveChild;
256 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
257 virtual bool isEqualImpl(const Matcher *M) const {
258 return cast<MoveChildMatcher>(M)->getChildNo() == getChildNo();
260 virtual unsigned getHashImpl() const { return getChildNo(); }
263 /// MoveParentMatcher - This tells the interpreter to move to the parent
264 /// of the current node.
265 class MoveParentMatcher : public Matcher {
267 MoveParentMatcher() : Matcher(MoveParent) {}
269 static inline bool classof(const Matcher *N) {
270 return N->getKind() == MoveParent;
274 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
275 virtual bool isEqualImpl(const Matcher *M) const { return true; }
276 virtual unsigned getHashImpl() const { return 0; }
279 /// CheckSameMatcher - This checks to see if this node is exactly the same
280 /// node as the specified match that was recorded with 'Record'. This is used
281 /// when patterns have the same name in them, like '(mul GPR:$in, GPR:$in)'.
282 class CheckSameMatcher : public Matcher {
283 unsigned MatchNumber;
285 CheckSameMatcher(unsigned matchnumber)
286 : Matcher(CheckSame), MatchNumber(matchnumber) {}
288 unsigned getMatchNumber() const { return MatchNumber; }
290 static inline bool classof(const Matcher *N) {
291 return N->getKind() == CheckSame;
295 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
296 virtual bool isEqualImpl(const Matcher *M) const {
297 return cast<CheckSameMatcher>(M)->getMatchNumber() == getMatchNumber();
299 virtual unsigned getHashImpl() const { return getMatchNumber(); }
302 /// CheckPatternPredicateMatcher - This checks the target-specific predicate
303 /// to see if the entire pattern is capable of matching. This predicate does
304 /// not take a node as input. This is used for subtarget feature checks etc.
305 class CheckPatternPredicateMatcher : public Matcher {
306 std::string Predicate;
308 CheckPatternPredicateMatcher(StringRef predicate)
309 : Matcher(CheckPatternPredicate), Predicate(predicate) {}
311 StringRef getPredicate() const { return Predicate; }
313 static inline bool classof(const Matcher *N) {
314 return N->getKind() == CheckPatternPredicate;
318 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
319 virtual bool isEqualImpl(const Matcher *M) const {
320 return cast<CheckPatternPredicateMatcher>(M)->getPredicate() == Predicate;
322 virtual unsigned getHashImpl() const;
325 /// CheckPredicateMatcher - This checks the target-specific predicate to
326 /// see if the node is acceptable.
327 class CheckPredicateMatcher : public Matcher {
330 CheckPredicateMatcher(StringRef predname)
331 : Matcher(CheckPredicate), PredName(predname) {}
333 StringRef getPredicateName() const { return PredName; }
335 static inline bool classof(const Matcher *N) {
336 return N->getKind() == CheckPredicate;
340 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
341 virtual bool isEqualImpl(const Matcher *M) const {
342 return cast<CheckPredicateMatcher>(M)->PredName == PredName;
344 virtual unsigned getHashImpl() const;
348 /// CheckOpcodeMatcher - This checks to see if the current node has the
349 /// specified opcode, if not it fails to match.
350 class CheckOpcodeMatcher : public Matcher {
351 StringRef OpcodeName;
353 CheckOpcodeMatcher(StringRef opcodename)
354 : Matcher(CheckOpcode), OpcodeName(opcodename) {}
356 StringRef getOpcodeName() const { return OpcodeName; }
358 static inline bool classof(const Matcher *N) {
359 return N->getKind() == CheckOpcode;
363 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
364 virtual bool isEqualImpl(const Matcher *M) const {
365 return cast<CheckOpcodeMatcher>(M)->OpcodeName == OpcodeName;
367 virtual unsigned getHashImpl() const;
370 /// CheckMultiOpcodeMatcher - This checks to see if the current node has one
371 /// of the specified opcode, if not it fails to match.
372 class CheckMultiOpcodeMatcher : public Matcher {
373 SmallVector<StringRef, 4> OpcodeNames;
375 CheckMultiOpcodeMatcher(const StringRef *opcodes, unsigned numops)
376 : Matcher(CheckMultiOpcode), OpcodeNames(opcodes, opcodes+numops) {}
378 unsigned getNumOpcodeNames() const { return OpcodeNames.size(); }
379 StringRef getOpcodeName(unsigned i) const { return OpcodeNames[i]; }
381 static inline bool classof(const Matcher *N) {
382 return N->getKind() == CheckMultiOpcode;
386 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
387 virtual bool isEqualImpl(const Matcher *M) const {
388 return cast<CheckMultiOpcodeMatcher>(M)->OpcodeNames == OpcodeNames;
390 virtual unsigned getHashImpl() const;
395 /// CheckTypeMatcher - This checks to see if the current node has the
396 /// specified type, if not it fails to match.
397 class CheckTypeMatcher : public Matcher {
398 MVT::SimpleValueType Type;
400 CheckTypeMatcher(MVT::SimpleValueType type)
401 : Matcher(CheckType), Type(type) {}
403 MVT::SimpleValueType getType() const { return Type; }
405 static inline bool classof(const Matcher *N) {
406 return N->getKind() == CheckType;
410 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
411 virtual bool isEqualImpl(const Matcher *M) const {
412 return cast<CheckTypeMatcher>(M)->Type == Type;
414 virtual unsigned getHashImpl() const { return Type; }
417 /// CheckChildTypeMatcher - This checks to see if a child node has the
418 /// specified type, if not it fails to match.
419 class CheckChildTypeMatcher : public Matcher {
421 MVT::SimpleValueType Type;
423 CheckChildTypeMatcher(unsigned childno, MVT::SimpleValueType type)
424 : Matcher(CheckChildType), ChildNo(childno), Type(type) {}
426 unsigned getChildNo() const { return ChildNo; }
427 MVT::SimpleValueType getType() const { return Type; }
429 static inline bool classof(const Matcher *N) {
430 return N->getKind() == CheckChildType;
434 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
435 virtual bool isEqualImpl(const Matcher *M) const {
436 return cast<CheckChildTypeMatcher>(M)->ChildNo == ChildNo &&
437 cast<CheckChildTypeMatcher>(M)->Type == Type;
439 virtual unsigned getHashImpl() const { return (Type << 3) | ChildNo; }
443 /// CheckIntegerMatcher - This checks to see if the current node is a
444 /// ConstantSDNode with the specified integer value, if not it fails to match.
445 class CheckIntegerMatcher : public Matcher {
448 CheckIntegerMatcher(int64_t value)
449 : Matcher(CheckInteger), Value(value) {}
451 int64_t getValue() const { return Value; }
453 static inline bool classof(const Matcher *N) {
454 return N->getKind() == CheckInteger;
458 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
459 virtual bool isEqualImpl(const Matcher *M) const {
460 return cast<CheckIntegerMatcher>(M)->Value == Value;
462 virtual unsigned getHashImpl() const { return Value; }
465 /// CheckCondCodeMatcher - This checks to see if the current node is a
466 /// CondCodeSDNode with the specified condition, if not it fails to match.
467 class CheckCondCodeMatcher : public Matcher {
468 StringRef CondCodeName;
470 CheckCondCodeMatcher(StringRef condcodename)
471 : Matcher(CheckCondCode), CondCodeName(condcodename) {}
473 StringRef getCondCodeName() const { return CondCodeName; }
475 static inline bool classof(const Matcher *N) {
476 return N->getKind() == CheckCondCode;
480 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
481 virtual bool isEqualImpl(const Matcher *M) const {
482 return cast<CheckCondCodeMatcher>(M)->CondCodeName == CondCodeName;
484 virtual unsigned getHashImpl() const;
487 /// CheckValueTypeMatcher - This checks to see if the current node is a
488 /// VTSDNode with the specified type, if not it fails to match.
489 class CheckValueTypeMatcher : public Matcher {
492 CheckValueTypeMatcher(StringRef type_name)
493 : Matcher(CheckValueType), TypeName(type_name) {}
495 StringRef getTypeName() const { return TypeName; }
497 static inline bool classof(const Matcher *N) {
498 return N->getKind() == CheckValueType;
502 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
503 virtual bool isEqualImpl(const Matcher *M) const {
504 return cast<CheckValueTypeMatcher>(M)->TypeName == TypeName;
506 virtual unsigned getHashImpl() const;
511 /// CheckComplexPatMatcher - This node runs the specified ComplexPattern on
512 /// the current node.
513 class CheckComplexPatMatcher : public Matcher {
514 const ComplexPattern &Pattern;
516 CheckComplexPatMatcher(const ComplexPattern &pattern)
517 : Matcher(CheckComplexPat), Pattern(pattern) {}
519 const ComplexPattern &getPattern() const { return Pattern; }
521 static inline bool classof(const Matcher *N) {
522 return N->getKind() == CheckComplexPat;
526 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
527 virtual bool isEqualImpl(const Matcher *M) const {
528 return &cast<CheckComplexPatMatcher>(M)->Pattern == &Pattern;
530 virtual unsigned getHashImpl() const {
531 return (unsigned)(intptr_t)&Pattern;
535 /// CheckAndImmMatcher - This checks to see if the current node is an 'and'
536 /// with something equivalent to the specified immediate.
537 class CheckAndImmMatcher : public Matcher {
540 CheckAndImmMatcher(int64_t value)
541 : Matcher(CheckAndImm), Value(value) {}
543 int64_t getValue() const { return Value; }
545 static inline bool classof(const Matcher *N) {
546 return N->getKind() == CheckAndImm;
550 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
551 virtual bool isEqualImpl(const Matcher *M) const {
552 return cast<CheckAndImmMatcher>(M)->Value == Value;
554 virtual unsigned getHashImpl() const { return Value; }
557 /// CheckOrImmMatcher - This checks to see if the current node is an 'and'
558 /// with something equivalent to the specified immediate.
559 class CheckOrImmMatcher : public Matcher {
562 CheckOrImmMatcher(int64_t value)
563 : Matcher(CheckOrImm), Value(value) {}
565 int64_t getValue() const { return Value; }
567 static inline bool classof(const Matcher *N) {
568 return N->getKind() == CheckOrImm;
572 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
573 virtual bool isEqualImpl(const Matcher *M) const {
574 return cast<CheckOrImmMatcher>(M)->Value == Value;
576 virtual unsigned getHashImpl() const { return Value; }
579 /// CheckFoldableChainNodeMatcher - This checks to see if the current node
580 /// (which defines a chain operand) is safe to fold into a larger pattern.
581 class CheckFoldableChainNodeMatcher : public Matcher {
583 CheckFoldableChainNodeMatcher()
584 : Matcher(CheckFoldableChainNode) {}
586 static inline bool classof(const Matcher *N) {
587 return N->getKind() == CheckFoldableChainNode;
591 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
592 virtual bool isEqualImpl(const Matcher *M) const { return true; }
593 virtual unsigned getHashImpl() const { return 0; }
596 /// CheckChainCompatibleMatcher - Verify that the current node's chain
597 /// operand is 'compatible' with the specified recorded node's.
598 class CheckChainCompatibleMatcher : public Matcher {
601 CheckChainCompatibleMatcher(unsigned previousop)
602 : Matcher(CheckChainCompatible), PreviousOp(previousop) {}
604 unsigned getPreviousOp() const { return PreviousOp; }
606 static inline bool classof(const Matcher *N) {
607 return N->getKind() == CheckChainCompatible;
611 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
612 virtual bool isEqualImpl(const Matcher *M) const {
613 return cast<CheckChainCompatibleMatcher>(this)->PreviousOp == PreviousOp;
615 virtual unsigned getHashImpl() const { return PreviousOp; }
618 /// EmitIntegerMatcher - This creates a new TargetConstant.
619 class EmitIntegerMatcher : public Matcher {
621 MVT::SimpleValueType VT;
623 EmitIntegerMatcher(int64_t val, MVT::SimpleValueType vt)
624 : Matcher(EmitInteger), Val(val), VT(vt) {}
626 int64_t getValue() const { return Val; }
627 MVT::SimpleValueType getVT() const { return VT; }
629 static inline bool classof(const Matcher *N) {
630 return N->getKind() == EmitInteger;
634 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
635 virtual bool isEqualImpl(const Matcher *M) const {
636 return cast<EmitIntegerMatcher>(M)->Val == Val &&
637 cast<EmitIntegerMatcher>(M)->VT == VT;
639 virtual unsigned getHashImpl() const { return (Val << 4) | VT; }
642 /// EmitStringIntegerMatcher - A target constant whose value is represented
644 class EmitStringIntegerMatcher : public Matcher {
646 MVT::SimpleValueType VT;
648 EmitStringIntegerMatcher(const std::string &val, MVT::SimpleValueType vt)
649 : Matcher(EmitStringInteger), Val(val), VT(vt) {}
651 const std::string &getValue() const { return Val; }
652 MVT::SimpleValueType getVT() const { return VT; }
654 static inline bool classof(const Matcher *N) {
655 return N->getKind() == EmitStringInteger;
659 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
660 virtual bool isEqualImpl(const Matcher *M) const {
661 return cast<EmitStringIntegerMatcher>(M)->Val == Val &&
662 cast<EmitStringIntegerMatcher>(M)->VT == VT;
664 virtual unsigned getHashImpl() const;
667 /// EmitRegisterMatcher - This creates a new TargetConstant.
668 class EmitRegisterMatcher : public Matcher {
669 /// Reg - The def for the register that we're emitting. If this is null, then
670 /// this is a reference to zero_reg.
672 MVT::SimpleValueType VT;
674 EmitRegisterMatcher(Record *reg, MVT::SimpleValueType vt)
675 : Matcher(EmitRegister), Reg(reg), VT(vt) {}
677 Record *getReg() const { return Reg; }
678 MVT::SimpleValueType getVT() const { return VT; }
680 static inline bool classof(const Matcher *N) {
681 return N->getKind() == EmitRegister;
685 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
686 virtual bool isEqualImpl(const Matcher *M) const {
687 return cast<EmitRegisterMatcher>(M)->Reg == Reg &&
688 cast<EmitRegisterMatcher>(M)->VT == VT;
690 virtual unsigned getHashImpl() const {
691 return ((unsigned)(intptr_t)Reg) << 4 | VT;
695 /// EmitConvertToTargetMatcher - Emit an operation that reads a specified
696 /// recorded node and converts it from being a ISD::Constant to
697 /// ISD::TargetConstant, likewise for ConstantFP.
698 class EmitConvertToTargetMatcher : public Matcher {
701 EmitConvertToTargetMatcher(unsigned slot)
702 : Matcher(EmitConvertToTarget), Slot(slot) {}
704 unsigned getSlot() const { return Slot; }
706 static inline bool classof(const Matcher *N) {
707 return N->getKind() == EmitConvertToTarget;
711 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
712 virtual bool isEqualImpl(const Matcher *M) const {
713 return cast<EmitConvertToTargetMatcher>(M)->Slot == Slot;
715 virtual unsigned getHashImpl() const { return Slot; }
718 /// EmitMergeInputChainsMatcher - Emit a node that merges a list of input
719 /// chains together with a token factor. The list of nodes are the nodes in the
720 /// matched pattern that have chain input/outputs. This node adds all input
721 /// chains of these nodes if they are not themselves a node in the pattern.
722 class EmitMergeInputChainsMatcher : public Matcher {
723 SmallVector<unsigned, 3> ChainNodes;
725 EmitMergeInputChainsMatcher(const unsigned *nodes, unsigned NumNodes)
726 : Matcher(EmitMergeInputChains), ChainNodes(nodes, nodes+NumNodes) {}
728 unsigned getNumNodes() const { return ChainNodes.size(); }
730 unsigned getNode(unsigned i) const {
731 assert(i < ChainNodes.size());
732 return ChainNodes[i];
735 static inline bool classof(const Matcher *N) {
736 return N->getKind() == EmitMergeInputChains;
740 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
741 virtual bool isEqualImpl(const Matcher *M) const {
742 return cast<EmitMergeInputChainsMatcher>(M)->ChainNodes == ChainNodes;
744 virtual unsigned getHashImpl() const;
747 /// EmitCopyToRegMatcher - Emit a CopyToReg node from a value to a physreg,
748 /// pushing the chain and flag results.
750 class EmitCopyToRegMatcher : public Matcher {
751 unsigned SrcSlot; // Value to copy into the physreg.
754 EmitCopyToRegMatcher(unsigned srcSlot, Record *destPhysReg)
755 : Matcher(EmitCopyToReg), SrcSlot(srcSlot), DestPhysReg(destPhysReg) {}
757 unsigned getSrcSlot() const { return SrcSlot; }
758 Record *getDestPhysReg() const { return DestPhysReg; }
760 static inline bool classof(const Matcher *N) {
761 return N->getKind() == EmitCopyToReg;
765 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
766 virtual bool isEqualImpl(const Matcher *M) const {
767 return cast<EmitCopyToRegMatcher>(M)->SrcSlot == SrcSlot &&
768 cast<EmitCopyToRegMatcher>(M)->DestPhysReg == DestPhysReg;
770 virtual unsigned getHashImpl() const {
771 return SrcSlot ^ ((unsigned)(intptr_t)DestPhysReg << 4);
777 /// EmitNodeXFormMatcher - Emit an operation that runs an SDNodeXForm on a
778 /// recorded node and records the result.
779 class EmitNodeXFormMatcher : public Matcher {
783 EmitNodeXFormMatcher(unsigned slot, Record *nodeXForm)
784 : Matcher(EmitNodeXForm), Slot(slot), NodeXForm(nodeXForm) {}
786 unsigned getSlot() const { return Slot; }
787 Record *getNodeXForm() const { return NodeXForm; }
789 static inline bool classof(const Matcher *N) {
790 return N->getKind() == EmitNodeXForm;
794 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
795 virtual bool isEqualImpl(const Matcher *M) const {
796 return cast<EmitNodeXFormMatcher>(M)->Slot == Slot &&
797 cast<EmitNodeXFormMatcher>(M)->NodeXForm == NodeXForm;
799 virtual unsigned getHashImpl() const {
800 return Slot ^ ((unsigned)(intptr_t)NodeXForm << 4);
804 /// EmitNodeMatcher - This signals a successful match and generates a node.
805 class EmitNodeMatcher : public Matcher {
806 std::string OpcodeName;
807 const SmallVector<MVT::SimpleValueType, 3> VTs;
808 const SmallVector<unsigned, 6> Operands;
809 bool HasChain, HasFlag, HasMemRefs;
811 /// NumFixedArityOperands - If this is a fixed arity node, this is set to -1.
812 /// If this is a varidic node, this is set to the number of fixed arity
813 /// operands in the root of the pattern. The rest are appended to this node.
814 int NumFixedArityOperands;
816 EmitNodeMatcher(const std::string &opcodeName,
817 const MVT::SimpleValueType *vts, unsigned numvts,
818 const unsigned *operands, unsigned numops,
819 bool hasChain, bool hasFlag, bool hasmemrefs,
820 int numfixedarityoperands)
821 : Matcher(EmitNode), OpcodeName(opcodeName),
822 VTs(vts, vts+numvts), Operands(operands, operands+numops),
823 HasChain(hasChain), HasFlag(hasFlag), HasMemRefs(hasmemrefs),
824 NumFixedArityOperands(numfixedarityoperands) {}
826 const std::string &getOpcodeName() const { return OpcodeName; }
828 unsigned getNumVTs() const { return VTs.size(); }
829 MVT::SimpleValueType getVT(unsigned i) const {
830 assert(i < VTs.size());
834 unsigned getNumOperands() const { return Operands.size(); }
835 unsigned getOperand(unsigned i) const {
836 assert(i < Operands.size());
840 bool hasChain() const { return HasChain; }
841 bool hasFlag() const { return HasFlag; }
842 bool hasMemRefs() const { return HasMemRefs; }
843 int getNumFixedArityOperands() const { return NumFixedArityOperands; }
845 static inline bool classof(const Matcher *N) {
846 return N->getKind() == EmitNode;
850 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
851 virtual bool isEqualImpl(const Matcher *M) const;
852 virtual unsigned getHashImpl() const;
855 /// MarkFlagResultsMatcher - This node indicates which non-root nodes in the
856 /// pattern produce flags. This allows CompleteMatchMatcher to update them
857 /// with the output flag of the resultant code.
858 class MarkFlagResultsMatcher : public Matcher {
859 SmallVector<unsigned, 3> FlagResultNodes;
861 MarkFlagResultsMatcher(const unsigned *nodes, unsigned NumNodes)
862 : Matcher(MarkFlagResults), FlagResultNodes(nodes, nodes+NumNodes) {}
864 unsigned getNumNodes() const { return FlagResultNodes.size(); }
866 unsigned getNode(unsigned i) const {
867 assert(i < FlagResultNodes.size());
868 return FlagResultNodes[i];
871 static inline bool classof(const Matcher *N) {
872 return N->getKind() == MarkFlagResults;
876 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
877 virtual bool isEqualImpl(const Matcher *M) const {
878 return cast<MarkFlagResultsMatcher>(M)->FlagResultNodes == FlagResultNodes;
880 virtual unsigned getHashImpl() const;
883 /// CompleteMatchMatcher - Complete a match by replacing the results of the
884 /// pattern with the newly generated nodes. This also prints a comment
885 /// indicating the source and dest patterns.
886 class CompleteMatchMatcher : public Matcher {
887 SmallVector<unsigned, 2> Results;
888 const PatternToMatch &Pattern;
890 CompleteMatchMatcher(const unsigned *results, unsigned numresults,
891 const PatternToMatch &pattern)
892 : Matcher(CompleteMatch), Results(results, results+numresults),
895 unsigned getNumResults() const { return Results.size(); }
896 unsigned getResult(unsigned R) const { return Results[R]; }
897 const PatternToMatch &getPattern() const { return Pattern; }
899 static inline bool classof(const Matcher *N) {
900 return N->getKind() == CompleteMatch;
904 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
905 virtual bool isEqualImpl(const Matcher *M) const {
906 return cast<CompleteMatchMatcher>(M)->Results == Results &&
907 &cast<CompleteMatchMatcher>(M)->Pattern == &Pattern;
909 virtual unsigned getHashImpl() const;
912 } // end namespace llvm