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 /// isSafeToReorderWithPatternPredicate - Return true if it is safe to sink a
108 /// PatternPredicate node past this one.
109 virtual bool isSafeToReorderWithPatternPredicate() const {
113 void print(raw_ostream &OS, unsigned indent = 0) const;
116 virtual void printImpl(raw_ostream &OS, unsigned indent) const = 0;
117 virtual bool isEqualImpl(const Matcher *M) const = 0;
118 virtual unsigned getHashImpl() const = 0;
121 /// ScopeMatcher - This attempts to match each of its children to find the first
122 /// one that successfully matches. If one child fails, it tries the next child.
123 /// If none of the children match then this check fails. It never has a 'next'.
124 class ScopeMatcher : public Matcher {
125 SmallVector<Matcher*, 4> Children;
127 ScopeMatcher(Matcher *const *children, unsigned numchildren)
128 : Matcher(Scope), Children(children, children+numchildren) {
130 virtual ~ScopeMatcher();
132 unsigned getNumChildren() const { return Children.size(); }
134 Matcher *getChild(unsigned i) { return Children[i]; }
135 const Matcher *getChild(unsigned i) const { return Children[i]; }
137 void resetChild(unsigned i, Matcher *N) {
142 Matcher *takeChild(unsigned i) {
143 Matcher *Res = Children[i];
148 void setNumChildren(unsigned NC) {
149 if (NC < Children.size()) {
150 // delete any children we're about to lose pointers to.
151 for (unsigned i = NC, e = Children.size(); i != e; ++i)
157 static inline bool classof(const Matcher *N) {
158 return N->getKind() == Scope;
162 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
163 virtual bool isEqualImpl(const Matcher *M) const { return false; }
164 virtual unsigned getHashImpl() const { return 12312; }
167 /// RecordMatcher - Save the current node in the operand list.
168 class RecordMatcher : public Matcher {
169 /// WhatFor - This is a string indicating why we're recording this. This
170 /// should only be used for comment generation not anything semantic.
173 RecordMatcher(const std::string &whatfor)
174 : Matcher(RecordNode), WhatFor(whatfor) {}
176 const std::string &getWhatFor() const { return WhatFor; }
178 static inline bool classof(const Matcher *N) {
179 return N->getKind() == RecordNode;
182 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
184 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
185 virtual bool isEqualImpl(const Matcher *M) const { return true; }
186 virtual unsigned getHashImpl() const { return 0; }
189 /// RecordChildMatcher - Save a numbered child of the current node, or fail
190 /// the match if it doesn't exist. This is logically equivalent to:
191 /// MoveChild N + RecordNode + MoveParent.
192 class RecordChildMatcher : public Matcher {
195 /// WhatFor - This is a string indicating why we're recording this. This
196 /// should only be used for comment generation not anything semantic.
199 RecordChildMatcher(unsigned childno, const std::string &whatfor)
200 : Matcher(RecordChild), ChildNo(childno), WhatFor(whatfor) {}
202 unsigned getChildNo() const { return ChildNo; }
203 const std::string &getWhatFor() const { return WhatFor; }
205 static inline bool classof(const Matcher *N) {
206 return N->getKind() == RecordChild;
209 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
212 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
213 virtual bool isEqualImpl(const Matcher *M) const {
214 return cast<RecordChildMatcher>(M)->getChildNo() == getChildNo();
216 virtual unsigned getHashImpl() const { return getChildNo(); }
219 /// RecordMemRefMatcher - Save the current node's memref.
220 class RecordMemRefMatcher : public Matcher {
222 RecordMemRefMatcher() : Matcher(RecordMemRef) {}
224 static inline bool classof(const Matcher *N) {
225 return N->getKind() == RecordMemRef;
228 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
231 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
232 virtual bool isEqualImpl(const Matcher *M) const { return true; }
233 virtual unsigned getHashImpl() const { return 0; }
237 /// CaptureFlagInputMatcher - If the current record has a flag input, record
238 /// it so that it is used as an input to the generated code.
239 class CaptureFlagInputMatcher : public Matcher {
241 CaptureFlagInputMatcher() : Matcher(CaptureFlagInput) {}
243 static inline bool classof(const Matcher *N) {
244 return N->getKind() == CaptureFlagInput;
247 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
250 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
251 virtual bool isEqualImpl(const Matcher *M) const { return true; }
252 virtual unsigned getHashImpl() const { return 0; }
255 /// MoveChildMatcher - This tells the interpreter to move into the
256 /// specified child node.
257 class MoveChildMatcher : public Matcher {
260 MoveChildMatcher(unsigned childNo) : Matcher(MoveChild), ChildNo(childNo) {}
262 unsigned getChildNo() const { return ChildNo; }
264 static inline bool classof(const Matcher *N) {
265 return N->getKind() == MoveChild;
268 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
271 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
272 virtual bool isEqualImpl(const Matcher *M) const {
273 return cast<MoveChildMatcher>(M)->getChildNo() == getChildNo();
275 virtual unsigned getHashImpl() const { return getChildNo(); }
278 /// MoveParentMatcher - This tells the interpreter to move to the parent
279 /// of the current node.
280 class MoveParentMatcher : public Matcher {
282 MoveParentMatcher() : Matcher(MoveParent) {}
284 static inline bool classof(const Matcher *N) {
285 return N->getKind() == MoveParent;
288 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
291 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
292 virtual bool isEqualImpl(const Matcher *M) const { return true; }
293 virtual unsigned getHashImpl() const { return 0; }
296 /// CheckSameMatcher - This checks to see if this node is exactly the same
297 /// node as the specified match that was recorded with 'Record'. This is used
298 /// when patterns have the same name in them, like '(mul GPR:$in, GPR:$in)'.
299 class CheckSameMatcher : public Matcher {
300 unsigned MatchNumber;
302 CheckSameMatcher(unsigned matchnumber)
303 : Matcher(CheckSame), MatchNumber(matchnumber) {}
305 unsigned getMatchNumber() const { return MatchNumber; }
307 static inline bool classof(const Matcher *N) {
308 return N->getKind() == CheckSame;
311 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
314 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
315 virtual bool isEqualImpl(const Matcher *M) const {
316 return cast<CheckSameMatcher>(M)->getMatchNumber() == getMatchNumber();
318 virtual unsigned getHashImpl() const { return getMatchNumber(); }
321 /// CheckPatternPredicateMatcher - This checks the target-specific predicate
322 /// to see if the entire pattern is capable of matching. This predicate does
323 /// not take a node as input. This is used for subtarget feature checks etc.
324 class CheckPatternPredicateMatcher : public Matcher {
325 std::string Predicate;
327 CheckPatternPredicateMatcher(StringRef predicate)
328 : Matcher(CheckPatternPredicate), Predicate(predicate) {}
330 StringRef getPredicate() const { return Predicate; }
332 static inline bool classof(const Matcher *N) {
333 return N->getKind() == CheckPatternPredicate;
336 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
339 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
340 virtual bool isEqualImpl(const Matcher *M) const {
341 return cast<CheckPatternPredicateMatcher>(M)->getPredicate() == Predicate;
343 virtual unsigned getHashImpl() const;
346 /// CheckPredicateMatcher - This checks the target-specific predicate to
347 /// see if the node is acceptable.
348 class CheckPredicateMatcher : public Matcher {
351 CheckPredicateMatcher(StringRef predname)
352 : Matcher(CheckPredicate), PredName(predname) {}
354 StringRef getPredicateName() const { return PredName; }
356 static inline bool classof(const Matcher *N) {
357 return N->getKind() == CheckPredicate;
361 //virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
364 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
365 virtual bool isEqualImpl(const Matcher *M) const {
366 return cast<CheckPredicateMatcher>(M)->PredName == PredName;
368 virtual unsigned getHashImpl() const;
372 /// CheckOpcodeMatcher - This checks to see if the current node has the
373 /// specified opcode, if not it fails to match.
374 class CheckOpcodeMatcher : public Matcher {
375 StringRef OpcodeName;
377 CheckOpcodeMatcher(StringRef opcodename)
378 : Matcher(CheckOpcode), OpcodeName(opcodename) {}
380 StringRef getOpcodeName() const { return OpcodeName; }
382 static inline bool classof(const Matcher *N) {
383 return N->getKind() == CheckOpcode;
386 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
389 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
390 virtual bool isEqualImpl(const Matcher *M) const {
391 return cast<CheckOpcodeMatcher>(M)->OpcodeName == OpcodeName;
393 virtual unsigned getHashImpl() const;
396 /// CheckMultiOpcodeMatcher - This checks to see if the current node has one
397 /// of the specified opcode, if not it fails to match.
398 class CheckMultiOpcodeMatcher : public Matcher {
399 SmallVector<StringRef, 4> OpcodeNames;
401 CheckMultiOpcodeMatcher(const StringRef *opcodes, unsigned numops)
402 : Matcher(CheckMultiOpcode), OpcodeNames(opcodes, opcodes+numops) {}
404 unsigned getNumOpcodeNames() const { return OpcodeNames.size(); }
405 StringRef getOpcodeName(unsigned i) const { return OpcodeNames[i]; }
407 static inline bool classof(const Matcher *N) {
408 return N->getKind() == CheckMultiOpcode;
411 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
414 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
415 virtual bool isEqualImpl(const Matcher *M) const {
416 return cast<CheckMultiOpcodeMatcher>(M)->OpcodeNames == OpcodeNames;
418 virtual unsigned getHashImpl() const;
423 /// CheckTypeMatcher - This checks to see if the current node has the
424 /// specified type, if not it fails to match.
425 class CheckTypeMatcher : public Matcher {
426 MVT::SimpleValueType Type;
428 CheckTypeMatcher(MVT::SimpleValueType type)
429 : Matcher(CheckType), Type(type) {}
431 MVT::SimpleValueType getType() const { return Type; }
433 static inline bool classof(const Matcher *N) {
434 return N->getKind() == CheckType;
437 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
440 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
441 virtual bool isEqualImpl(const Matcher *M) const {
442 return cast<CheckTypeMatcher>(M)->Type == Type;
444 virtual unsigned getHashImpl() const { return Type; }
447 /// CheckChildTypeMatcher - This checks to see if a child node has the
448 /// specified type, if not it fails to match.
449 class CheckChildTypeMatcher : public Matcher {
451 MVT::SimpleValueType Type;
453 CheckChildTypeMatcher(unsigned childno, MVT::SimpleValueType type)
454 : Matcher(CheckChildType), ChildNo(childno), Type(type) {}
456 unsigned getChildNo() const { return ChildNo; }
457 MVT::SimpleValueType getType() const { return Type; }
459 static inline bool classof(const Matcher *N) {
460 return N->getKind() == CheckChildType;
463 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
466 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
467 virtual bool isEqualImpl(const Matcher *M) const {
468 return cast<CheckChildTypeMatcher>(M)->ChildNo == ChildNo &&
469 cast<CheckChildTypeMatcher>(M)->Type == Type;
471 virtual unsigned getHashImpl() const { return (Type << 3) | ChildNo; }
475 /// CheckIntegerMatcher - This checks to see if the current node is a
476 /// ConstantSDNode with the specified integer value, if not it fails to match.
477 class CheckIntegerMatcher : public Matcher {
480 CheckIntegerMatcher(int64_t value)
481 : Matcher(CheckInteger), Value(value) {}
483 int64_t getValue() const { return Value; }
485 static inline bool classof(const Matcher *N) {
486 return N->getKind() == CheckInteger;
489 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
492 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
493 virtual bool isEqualImpl(const Matcher *M) const {
494 return cast<CheckIntegerMatcher>(M)->Value == Value;
496 virtual unsigned getHashImpl() const { return Value; }
499 /// CheckCondCodeMatcher - This checks to see if the current node is a
500 /// CondCodeSDNode with the specified condition, if not it fails to match.
501 class CheckCondCodeMatcher : public Matcher {
502 StringRef CondCodeName;
504 CheckCondCodeMatcher(StringRef condcodename)
505 : Matcher(CheckCondCode), CondCodeName(condcodename) {}
507 StringRef getCondCodeName() const { return CondCodeName; }
509 static inline bool classof(const Matcher *N) {
510 return N->getKind() == CheckCondCode;
513 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
516 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
517 virtual bool isEqualImpl(const Matcher *M) const {
518 return cast<CheckCondCodeMatcher>(M)->CondCodeName == CondCodeName;
520 virtual unsigned getHashImpl() const;
523 /// CheckValueTypeMatcher - This checks to see if the current node is a
524 /// VTSDNode with the specified type, if not it fails to match.
525 class CheckValueTypeMatcher : public Matcher {
528 CheckValueTypeMatcher(StringRef type_name)
529 : Matcher(CheckValueType), TypeName(type_name) {}
531 StringRef getTypeName() const { return TypeName; }
533 static inline bool classof(const Matcher *N) {
534 return N->getKind() == CheckValueType;
537 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
540 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
541 virtual bool isEqualImpl(const Matcher *M) const {
542 return cast<CheckValueTypeMatcher>(M)->TypeName == TypeName;
544 virtual unsigned getHashImpl() const;
549 /// CheckComplexPatMatcher - This node runs the specified ComplexPattern on
550 /// the current node.
551 class CheckComplexPatMatcher : public Matcher {
552 const ComplexPattern &Pattern;
554 CheckComplexPatMatcher(const ComplexPattern &pattern)
555 : Matcher(CheckComplexPat), Pattern(pattern) {}
557 const ComplexPattern &getPattern() const { return Pattern; }
559 static inline bool classof(const Matcher *N) {
560 return N->getKind() == CheckComplexPat;
563 // Not safe to move a pattern predicate past a complex pattern.
564 virtual bool isSafeToReorderWithPatternPredicate() const { return false; }
567 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
568 virtual bool isEqualImpl(const Matcher *M) const {
569 return &cast<CheckComplexPatMatcher>(M)->Pattern == &Pattern;
571 virtual unsigned getHashImpl() const {
572 return (unsigned)(intptr_t)&Pattern;
576 /// CheckAndImmMatcher - This checks to see if the current node is an 'and'
577 /// with something equivalent to the specified immediate.
578 class CheckAndImmMatcher : public Matcher {
581 CheckAndImmMatcher(int64_t value)
582 : Matcher(CheckAndImm), Value(value) {}
584 int64_t getValue() const { return Value; }
586 static inline bool classof(const Matcher *N) {
587 return N->getKind() == CheckAndImm;
590 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
593 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
594 virtual bool isEqualImpl(const Matcher *M) const {
595 return cast<CheckAndImmMatcher>(M)->Value == Value;
597 virtual unsigned getHashImpl() const { return Value; }
600 /// CheckOrImmMatcher - This checks to see if the current node is an 'and'
601 /// with something equivalent to the specified immediate.
602 class CheckOrImmMatcher : public Matcher {
605 CheckOrImmMatcher(int64_t value)
606 : Matcher(CheckOrImm), Value(value) {}
608 int64_t getValue() const { return Value; }
610 static inline bool classof(const Matcher *N) {
611 return N->getKind() == CheckOrImm;
614 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
617 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
618 virtual bool isEqualImpl(const Matcher *M) const {
619 return cast<CheckOrImmMatcher>(M)->Value == Value;
621 virtual unsigned getHashImpl() const { return Value; }
624 /// CheckFoldableChainNodeMatcher - This checks to see if the current node
625 /// (which defines a chain operand) is safe to fold into a larger pattern.
626 class CheckFoldableChainNodeMatcher : public Matcher {
628 CheckFoldableChainNodeMatcher()
629 : Matcher(CheckFoldableChainNode) {}
631 static inline bool classof(const Matcher *N) {
632 return N->getKind() == CheckFoldableChainNode;
635 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
638 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
639 virtual bool isEqualImpl(const Matcher *M) const { return true; }
640 virtual unsigned getHashImpl() const { return 0; }
643 /// CheckChainCompatibleMatcher - Verify that the current node's chain
644 /// operand is 'compatible' with the specified recorded node's.
645 class CheckChainCompatibleMatcher : public Matcher {
648 CheckChainCompatibleMatcher(unsigned previousop)
649 : Matcher(CheckChainCompatible), PreviousOp(previousop) {}
651 unsigned getPreviousOp() const { return PreviousOp; }
653 static inline bool classof(const Matcher *N) {
654 return N->getKind() == CheckChainCompatible;
657 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
660 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
661 virtual bool isEqualImpl(const Matcher *M) const {
662 return cast<CheckChainCompatibleMatcher>(M)->PreviousOp == PreviousOp;
664 virtual unsigned getHashImpl() const { return PreviousOp; }
667 /// EmitIntegerMatcher - This creates a new TargetConstant.
668 class EmitIntegerMatcher : public Matcher {
670 MVT::SimpleValueType VT;
672 EmitIntegerMatcher(int64_t val, MVT::SimpleValueType vt)
673 : Matcher(EmitInteger), Val(val), VT(vt) {}
675 int64_t getValue() const { return Val; }
676 MVT::SimpleValueType getVT() const { return VT; }
678 static inline bool classof(const Matcher *N) {
679 return N->getKind() == EmitInteger;
683 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
684 virtual bool isEqualImpl(const Matcher *M) const {
685 return cast<EmitIntegerMatcher>(M)->Val == Val &&
686 cast<EmitIntegerMatcher>(M)->VT == VT;
688 virtual unsigned getHashImpl() const { return (Val << 4) | VT; }
691 /// EmitStringIntegerMatcher - A target constant whose value is represented
693 class EmitStringIntegerMatcher : public Matcher {
695 MVT::SimpleValueType VT;
697 EmitStringIntegerMatcher(const std::string &val, MVT::SimpleValueType vt)
698 : Matcher(EmitStringInteger), Val(val), VT(vt) {}
700 const std::string &getValue() const { return Val; }
701 MVT::SimpleValueType getVT() const { return VT; }
703 static inline bool classof(const Matcher *N) {
704 return N->getKind() == EmitStringInteger;
708 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
709 virtual bool isEqualImpl(const Matcher *M) const {
710 return cast<EmitStringIntegerMatcher>(M)->Val == Val &&
711 cast<EmitStringIntegerMatcher>(M)->VT == VT;
713 virtual unsigned getHashImpl() const;
716 /// EmitRegisterMatcher - This creates a new TargetConstant.
717 class EmitRegisterMatcher : public Matcher {
718 /// Reg - The def for the register that we're emitting. If this is null, then
719 /// this is a reference to zero_reg.
721 MVT::SimpleValueType VT;
723 EmitRegisterMatcher(Record *reg, MVT::SimpleValueType vt)
724 : Matcher(EmitRegister), Reg(reg), VT(vt) {}
726 Record *getReg() const { return Reg; }
727 MVT::SimpleValueType getVT() const { return VT; }
729 static inline bool classof(const Matcher *N) {
730 return N->getKind() == EmitRegister;
734 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
735 virtual bool isEqualImpl(const Matcher *M) const {
736 return cast<EmitRegisterMatcher>(M)->Reg == Reg &&
737 cast<EmitRegisterMatcher>(M)->VT == VT;
739 virtual unsigned getHashImpl() const {
740 return ((unsigned)(intptr_t)Reg) << 4 | VT;
744 /// EmitConvertToTargetMatcher - Emit an operation that reads a specified
745 /// recorded node and converts it from being a ISD::Constant to
746 /// ISD::TargetConstant, likewise for ConstantFP.
747 class EmitConvertToTargetMatcher : public Matcher {
750 EmitConvertToTargetMatcher(unsigned slot)
751 : Matcher(EmitConvertToTarget), Slot(slot) {}
753 unsigned getSlot() const { return Slot; }
755 static inline bool classof(const Matcher *N) {
756 return N->getKind() == EmitConvertToTarget;
760 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
761 virtual bool isEqualImpl(const Matcher *M) const {
762 return cast<EmitConvertToTargetMatcher>(M)->Slot == Slot;
764 virtual unsigned getHashImpl() const { return Slot; }
767 /// EmitMergeInputChainsMatcher - Emit a node that merges a list of input
768 /// chains together with a token factor. The list of nodes are the nodes in the
769 /// matched pattern that have chain input/outputs. This node adds all input
770 /// chains of these nodes if they are not themselves a node in the pattern.
771 class EmitMergeInputChainsMatcher : public Matcher {
772 SmallVector<unsigned, 3> ChainNodes;
774 EmitMergeInputChainsMatcher(const unsigned *nodes, unsigned NumNodes)
775 : Matcher(EmitMergeInputChains), ChainNodes(nodes, nodes+NumNodes) {}
777 unsigned getNumNodes() const { return ChainNodes.size(); }
779 unsigned getNode(unsigned i) const {
780 assert(i < ChainNodes.size());
781 return ChainNodes[i];
784 static inline bool classof(const Matcher *N) {
785 return N->getKind() == EmitMergeInputChains;
789 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
790 virtual bool isEqualImpl(const Matcher *M) const {
791 return cast<EmitMergeInputChainsMatcher>(M)->ChainNodes == ChainNodes;
793 virtual unsigned getHashImpl() const;
796 /// EmitCopyToRegMatcher - Emit a CopyToReg node from a value to a physreg,
797 /// pushing the chain and flag results.
799 class EmitCopyToRegMatcher : public Matcher {
800 unsigned SrcSlot; // Value to copy into the physreg.
803 EmitCopyToRegMatcher(unsigned srcSlot, Record *destPhysReg)
804 : Matcher(EmitCopyToReg), SrcSlot(srcSlot), DestPhysReg(destPhysReg) {}
806 unsigned getSrcSlot() const { return SrcSlot; }
807 Record *getDestPhysReg() const { return DestPhysReg; }
809 static inline bool classof(const Matcher *N) {
810 return N->getKind() == EmitCopyToReg;
814 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
815 virtual bool isEqualImpl(const Matcher *M) const {
816 return cast<EmitCopyToRegMatcher>(M)->SrcSlot == SrcSlot &&
817 cast<EmitCopyToRegMatcher>(M)->DestPhysReg == DestPhysReg;
819 virtual unsigned getHashImpl() const {
820 return SrcSlot ^ ((unsigned)(intptr_t)DestPhysReg << 4);
826 /// EmitNodeXFormMatcher - Emit an operation that runs an SDNodeXForm on a
827 /// recorded node and records the result.
828 class EmitNodeXFormMatcher : public Matcher {
832 EmitNodeXFormMatcher(unsigned slot, Record *nodeXForm)
833 : Matcher(EmitNodeXForm), Slot(slot), NodeXForm(nodeXForm) {}
835 unsigned getSlot() const { return Slot; }
836 Record *getNodeXForm() const { return NodeXForm; }
838 static inline bool classof(const Matcher *N) {
839 return N->getKind() == EmitNodeXForm;
843 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
844 virtual bool isEqualImpl(const Matcher *M) const {
845 return cast<EmitNodeXFormMatcher>(M)->Slot == Slot &&
846 cast<EmitNodeXFormMatcher>(M)->NodeXForm == NodeXForm;
848 virtual unsigned getHashImpl() const {
849 return Slot ^ ((unsigned)(intptr_t)NodeXForm << 4);
853 /// EmitNodeMatcher - This signals a successful match and generates a node.
854 class EmitNodeMatcher : public Matcher {
855 std::string OpcodeName;
856 const SmallVector<MVT::SimpleValueType, 3> VTs;
857 const SmallVector<unsigned, 6> Operands;
858 bool HasChain, HasFlag, HasMemRefs;
860 /// NumFixedArityOperands - If this is a fixed arity node, this is set to -1.
861 /// If this is a varidic node, this is set to the number of fixed arity
862 /// operands in the root of the pattern. The rest are appended to this node.
863 int NumFixedArityOperands;
865 EmitNodeMatcher(const std::string &opcodeName,
866 const MVT::SimpleValueType *vts, unsigned numvts,
867 const unsigned *operands, unsigned numops,
868 bool hasChain, bool hasFlag, bool hasmemrefs,
869 int numfixedarityoperands)
870 : Matcher(EmitNode), OpcodeName(opcodeName),
871 VTs(vts, vts+numvts), Operands(operands, operands+numops),
872 HasChain(hasChain), HasFlag(hasFlag), HasMemRefs(hasmemrefs),
873 NumFixedArityOperands(numfixedarityoperands) {}
875 const std::string &getOpcodeName() const { return OpcodeName; }
877 unsigned getNumVTs() const { return VTs.size(); }
878 MVT::SimpleValueType getVT(unsigned i) const {
879 assert(i < VTs.size());
883 unsigned getNumOperands() const { return Operands.size(); }
884 unsigned getOperand(unsigned i) const {
885 assert(i < Operands.size());
889 bool hasChain() const { return HasChain; }
890 bool hasFlag() const { return HasFlag; }
891 bool hasMemRefs() const { return HasMemRefs; }
892 int getNumFixedArityOperands() const { return NumFixedArityOperands; }
894 static inline bool classof(const Matcher *N) {
895 return N->getKind() == EmitNode;
899 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
900 virtual bool isEqualImpl(const Matcher *M) const;
901 virtual unsigned getHashImpl() const;
904 /// MarkFlagResultsMatcher - This node indicates which non-root nodes in the
905 /// pattern produce flags. This allows CompleteMatchMatcher to update them
906 /// with the output flag of the resultant code.
907 class MarkFlagResultsMatcher : public Matcher {
908 SmallVector<unsigned, 3> FlagResultNodes;
910 MarkFlagResultsMatcher(const unsigned *nodes, unsigned NumNodes)
911 : Matcher(MarkFlagResults), FlagResultNodes(nodes, nodes+NumNodes) {}
913 unsigned getNumNodes() const { return FlagResultNodes.size(); }
915 unsigned getNode(unsigned i) const {
916 assert(i < FlagResultNodes.size());
917 return FlagResultNodes[i];
920 static inline bool classof(const Matcher *N) {
921 return N->getKind() == MarkFlagResults;
925 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
926 virtual bool isEqualImpl(const Matcher *M) const {
927 return cast<MarkFlagResultsMatcher>(M)->FlagResultNodes == FlagResultNodes;
929 virtual unsigned getHashImpl() const;
932 /// CompleteMatchMatcher - Complete a match by replacing the results of the
933 /// pattern with the newly generated nodes. This also prints a comment
934 /// indicating the source and dest patterns.
935 class CompleteMatchMatcher : public Matcher {
936 SmallVector<unsigned, 2> Results;
937 const PatternToMatch &Pattern;
939 CompleteMatchMatcher(const unsigned *results, unsigned numresults,
940 const PatternToMatch &pattern)
941 : Matcher(CompleteMatch), Results(results, results+numresults),
944 unsigned getNumResults() const { return Results.size(); }
945 unsigned getResult(unsigned R) const { return Results[R]; }
946 const PatternToMatch &getPattern() const { return Pattern; }
948 static inline bool classof(const Matcher *N) {
949 return N->getKind() == CompleteMatch;
953 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
954 virtual bool isEqualImpl(const Matcher *M) const {
955 return cast<CompleteMatchMatcher>(M)->Results == Results &&
956 &cast<CompleteMatchMatcher>(M)->Pattern == &Pattern;
958 virtual unsigned getHashImpl() const;
961 } // end namespace llvm