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 /// isContradictory - Return true of these two matchers could never match on
115 bool isContradictory(const Matcher *Other) const {
116 // Since this predicate is reflexive, we canonicalize the ordering so that
117 // we always match a node against nodes with kinds that are greater or equal
118 // to them. For example, we'll pass in a CheckType node as an argument to
119 // the CheckOpcode method, not the other way around.
120 if (getKind() < Other->getKind())
121 return isContradictoryImpl(Other);
122 return Other->isContradictoryImpl(this);
125 void print(raw_ostream &OS, unsigned indent = 0) const;
126 void printOne(raw_ostream &OS) const;
129 virtual void printImpl(raw_ostream &OS, unsigned indent) const = 0;
130 virtual bool isEqualImpl(const Matcher *M) const = 0;
131 virtual unsigned getHashImpl() const = 0;
132 virtual bool isContradictoryImpl(const Matcher *M) const { return false; }
135 /// ScopeMatcher - This attempts to match each of its children to find the first
136 /// one that successfully matches. If one child fails, it tries the next child.
137 /// If none of the children match then this check fails. It never has a 'next'.
138 class ScopeMatcher : public Matcher {
139 SmallVector<Matcher*, 4> Children;
141 ScopeMatcher(Matcher *const *children, unsigned numchildren)
142 : Matcher(Scope), Children(children, children+numchildren) {
144 virtual ~ScopeMatcher();
146 unsigned getNumChildren() const { return Children.size(); }
148 Matcher *getChild(unsigned i) { return Children[i]; }
149 const Matcher *getChild(unsigned i) const { return Children[i]; }
151 void resetChild(unsigned i, Matcher *N) {
156 Matcher *takeChild(unsigned i) {
157 Matcher *Res = Children[i];
162 void setNumChildren(unsigned NC) {
163 if (NC < Children.size()) {
164 // delete any children we're about to lose pointers to.
165 for (unsigned i = NC, e = Children.size(); i != e; ++i)
171 static inline bool classof(const Matcher *N) {
172 return N->getKind() == Scope;
176 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
177 virtual bool isEqualImpl(const Matcher *M) const { return false; }
178 virtual unsigned getHashImpl() const { return 12312; }
181 /// RecordMatcher - Save the current node in the operand list.
182 class RecordMatcher : public Matcher {
183 /// WhatFor - This is a string indicating why we're recording this. This
184 /// should only be used for comment generation not anything semantic.
187 RecordMatcher(const std::string &whatfor)
188 : Matcher(RecordNode), WhatFor(whatfor) {}
190 const std::string &getWhatFor() const { return WhatFor; }
192 static inline bool classof(const Matcher *N) {
193 return N->getKind() == RecordNode;
196 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
198 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
199 virtual bool isEqualImpl(const Matcher *M) const { return true; }
200 virtual unsigned getHashImpl() const { return 0; }
203 /// RecordChildMatcher - Save a numbered child of the current node, or fail
204 /// the match if it doesn't exist. This is logically equivalent to:
205 /// MoveChild N + RecordNode + MoveParent.
206 class RecordChildMatcher : public Matcher {
209 /// WhatFor - This is a string indicating why we're recording this. This
210 /// should only be used for comment generation not anything semantic.
213 RecordChildMatcher(unsigned childno, const std::string &whatfor)
214 : Matcher(RecordChild), ChildNo(childno), WhatFor(whatfor) {}
216 unsigned getChildNo() const { return ChildNo; }
217 const std::string &getWhatFor() const { return WhatFor; }
219 static inline bool classof(const Matcher *N) {
220 return N->getKind() == RecordChild;
223 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
226 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
227 virtual bool isEqualImpl(const Matcher *M) const {
228 return cast<RecordChildMatcher>(M)->getChildNo() == getChildNo();
230 virtual unsigned getHashImpl() const { return getChildNo(); }
233 /// RecordMemRefMatcher - Save the current node's memref.
234 class RecordMemRefMatcher : public Matcher {
236 RecordMemRefMatcher() : Matcher(RecordMemRef) {}
238 static inline bool classof(const Matcher *N) {
239 return N->getKind() == RecordMemRef;
242 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
245 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
246 virtual bool isEqualImpl(const Matcher *M) const { return true; }
247 virtual unsigned getHashImpl() const { return 0; }
251 /// CaptureFlagInputMatcher - If the current record has a flag input, record
252 /// it so that it is used as an input to the generated code.
253 class CaptureFlagInputMatcher : public Matcher {
255 CaptureFlagInputMatcher() : Matcher(CaptureFlagInput) {}
257 static inline bool classof(const Matcher *N) {
258 return N->getKind() == CaptureFlagInput;
261 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
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 /// MoveChildMatcher - This tells the interpreter to move into the
270 /// specified child node.
271 class MoveChildMatcher : public Matcher {
274 MoveChildMatcher(unsigned childNo) : Matcher(MoveChild), ChildNo(childNo) {}
276 unsigned getChildNo() const { return ChildNo; }
278 static inline bool classof(const Matcher *N) {
279 return N->getKind() == MoveChild;
282 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
285 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
286 virtual bool isEqualImpl(const Matcher *M) const {
287 return cast<MoveChildMatcher>(M)->getChildNo() == getChildNo();
289 virtual unsigned getHashImpl() const { return getChildNo(); }
292 /// MoveParentMatcher - This tells the interpreter to move to the parent
293 /// of the current node.
294 class MoveParentMatcher : public Matcher {
296 MoveParentMatcher() : Matcher(MoveParent) {}
298 static inline bool classof(const Matcher *N) {
299 return N->getKind() == MoveParent;
302 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
305 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
306 virtual bool isEqualImpl(const Matcher *M) const { return true; }
307 virtual unsigned getHashImpl() const { return 0; }
310 /// CheckSameMatcher - This checks to see if this node is exactly the same
311 /// node as the specified match that was recorded with 'Record'. This is used
312 /// when patterns have the same name in them, like '(mul GPR:$in, GPR:$in)'.
313 class CheckSameMatcher : public Matcher {
314 unsigned MatchNumber;
316 CheckSameMatcher(unsigned matchnumber)
317 : Matcher(CheckSame), MatchNumber(matchnumber) {}
319 unsigned getMatchNumber() const { return MatchNumber; }
321 static inline bool classof(const Matcher *N) {
322 return N->getKind() == CheckSame;
325 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
328 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
329 virtual bool isEqualImpl(const Matcher *M) const {
330 return cast<CheckSameMatcher>(M)->getMatchNumber() == getMatchNumber();
332 virtual unsigned getHashImpl() const { return getMatchNumber(); }
335 /// CheckPatternPredicateMatcher - This checks the target-specific predicate
336 /// to see if the entire pattern is capable of matching. This predicate does
337 /// not take a node as input. This is used for subtarget feature checks etc.
338 class CheckPatternPredicateMatcher : public Matcher {
339 std::string Predicate;
341 CheckPatternPredicateMatcher(StringRef predicate)
342 : Matcher(CheckPatternPredicate), Predicate(predicate) {}
344 StringRef getPredicate() const { return Predicate; }
346 static inline bool classof(const Matcher *N) {
347 return N->getKind() == CheckPatternPredicate;
350 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
353 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
354 virtual bool isEqualImpl(const Matcher *M) const {
355 return cast<CheckPatternPredicateMatcher>(M)->getPredicate() == Predicate;
357 virtual unsigned getHashImpl() const;
360 /// CheckPredicateMatcher - This checks the target-specific predicate to
361 /// see if the node is acceptable.
362 class CheckPredicateMatcher : public Matcher {
365 CheckPredicateMatcher(StringRef predname)
366 : Matcher(CheckPredicate), PredName(predname) {}
368 StringRef getPredicateName() const { return PredName; }
370 static inline bool classof(const Matcher *N) {
371 return N->getKind() == CheckPredicate;
375 //virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
378 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
379 virtual bool isEqualImpl(const Matcher *M) const {
380 return cast<CheckPredicateMatcher>(M)->PredName == PredName;
382 virtual unsigned getHashImpl() const;
386 /// CheckOpcodeMatcher - This checks to see if the current node has the
387 /// specified opcode, if not it fails to match.
388 class CheckOpcodeMatcher : public Matcher {
389 StringRef OpcodeName;
391 CheckOpcodeMatcher(StringRef opcodename)
392 : Matcher(CheckOpcode), OpcodeName(opcodename) {}
394 StringRef getOpcodeName() const { return OpcodeName; }
396 static inline bool classof(const Matcher *N) {
397 return N->getKind() == CheckOpcode;
400 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
403 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
404 virtual bool isEqualImpl(const Matcher *M) const {
405 return cast<CheckOpcodeMatcher>(M)->OpcodeName == OpcodeName;
407 virtual unsigned getHashImpl() const;
408 virtual bool isContradictoryImpl(const Matcher *M) const;
411 /// CheckMultiOpcodeMatcher - This checks to see if the current node has one
412 /// of the specified opcode, if not it fails to match.
413 class CheckMultiOpcodeMatcher : public Matcher {
414 SmallVector<StringRef, 4> OpcodeNames;
416 CheckMultiOpcodeMatcher(const StringRef *opcodes, unsigned numops)
417 : Matcher(CheckMultiOpcode), OpcodeNames(opcodes, opcodes+numops) {}
419 unsigned getNumOpcodeNames() const { return OpcodeNames.size(); }
420 StringRef getOpcodeName(unsigned i) const { return OpcodeNames[i]; }
422 static inline bool classof(const Matcher *N) {
423 return N->getKind() == CheckMultiOpcode;
426 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
429 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
430 virtual bool isEqualImpl(const Matcher *M) const {
431 return cast<CheckMultiOpcodeMatcher>(M)->OpcodeNames == OpcodeNames;
433 virtual unsigned getHashImpl() const;
438 /// CheckTypeMatcher - This checks to see if the current node has the
439 /// specified type, if not it fails to match.
440 class CheckTypeMatcher : public Matcher {
441 MVT::SimpleValueType Type;
443 CheckTypeMatcher(MVT::SimpleValueType type)
444 : Matcher(CheckType), Type(type) {}
446 MVT::SimpleValueType getType() const { return Type; }
448 static inline bool classof(const Matcher *N) {
449 return N->getKind() == CheckType;
452 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
455 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
456 virtual bool isEqualImpl(const Matcher *M) const {
457 return cast<CheckTypeMatcher>(M)->Type == Type;
459 virtual unsigned getHashImpl() const { return Type; }
460 virtual bool isContradictoryImpl(const Matcher *M) const;
463 /// CheckChildTypeMatcher - This checks to see if a child node has the
464 /// specified type, if not it fails to match.
465 class CheckChildTypeMatcher : public Matcher {
467 MVT::SimpleValueType Type;
469 CheckChildTypeMatcher(unsigned childno, MVT::SimpleValueType type)
470 : Matcher(CheckChildType), ChildNo(childno), Type(type) {}
472 unsigned getChildNo() const { return ChildNo; }
473 MVT::SimpleValueType getType() const { return Type; }
475 static inline bool classof(const Matcher *N) {
476 return N->getKind() == CheckChildType;
479 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
482 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
483 virtual bool isEqualImpl(const Matcher *M) const {
484 return cast<CheckChildTypeMatcher>(M)->ChildNo == ChildNo &&
485 cast<CheckChildTypeMatcher>(M)->Type == Type;
487 virtual unsigned getHashImpl() const { return (Type << 3) | ChildNo; }
488 virtual bool isContradictoryImpl(const Matcher *M) const;
492 /// CheckIntegerMatcher - This checks to see if the current node is a
493 /// ConstantSDNode with the specified integer value, if not it fails to match.
494 class CheckIntegerMatcher : public Matcher {
497 CheckIntegerMatcher(int64_t value)
498 : Matcher(CheckInteger), Value(value) {}
500 int64_t getValue() const { return Value; }
502 static inline bool classof(const Matcher *N) {
503 return N->getKind() == CheckInteger;
506 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
509 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
510 virtual bool isEqualImpl(const Matcher *M) const {
511 return cast<CheckIntegerMatcher>(M)->Value == Value;
513 virtual unsigned getHashImpl() const { return Value; }
514 virtual bool isContradictoryImpl(const Matcher *M) const;
517 /// CheckCondCodeMatcher - This checks to see if the current node is a
518 /// CondCodeSDNode with the specified condition, if not it fails to match.
519 class CheckCondCodeMatcher : public Matcher {
520 StringRef CondCodeName;
522 CheckCondCodeMatcher(StringRef condcodename)
523 : Matcher(CheckCondCode), CondCodeName(condcodename) {}
525 StringRef getCondCodeName() const { return CondCodeName; }
527 static inline bool classof(const Matcher *N) {
528 return N->getKind() == CheckCondCode;
531 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
534 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
535 virtual bool isEqualImpl(const Matcher *M) const {
536 return cast<CheckCondCodeMatcher>(M)->CondCodeName == CondCodeName;
538 virtual unsigned getHashImpl() const;
541 /// CheckValueTypeMatcher - This checks to see if the current node is a
542 /// VTSDNode with the specified type, if not it fails to match.
543 class CheckValueTypeMatcher : public Matcher {
546 CheckValueTypeMatcher(StringRef type_name)
547 : Matcher(CheckValueType), TypeName(type_name) {}
549 StringRef getTypeName() const { return TypeName; }
551 static inline bool classof(const Matcher *N) {
552 return N->getKind() == CheckValueType;
555 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
558 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
559 virtual bool isEqualImpl(const Matcher *M) const {
560 return cast<CheckValueTypeMatcher>(M)->TypeName == TypeName;
562 virtual unsigned getHashImpl() const;
567 /// CheckComplexPatMatcher - This node runs the specified ComplexPattern on
568 /// the current node.
569 class CheckComplexPatMatcher : public Matcher {
570 const ComplexPattern &Pattern;
572 CheckComplexPatMatcher(const ComplexPattern &pattern)
573 : Matcher(CheckComplexPat), Pattern(pattern) {}
575 const ComplexPattern &getPattern() const { return Pattern; }
577 static inline bool classof(const Matcher *N) {
578 return N->getKind() == CheckComplexPat;
581 // Not safe to move a pattern predicate past a complex pattern.
582 virtual bool isSafeToReorderWithPatternPredicate() const { return false; }
585 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
586 virtual bool isEqualImpl(const Matcher *M) const {
587 return &cast<CheckComplexPatMatcher>(M)->Pattern == &Pattern;
589 virtual unsigned getHashImpl() const {
590 return (unsigned)(intptr_t)&Pattern;
594 /// CheckAndImmMatcher - This checks to see if the current node is an 'and'
595 /// with something equivalent to the specified immediate.
596 class CheckAndImmMatcher : public Matcher {
599 CheckAndImmMatcher(int64_t value)
600 : Matcher(CheckAndImm), Value(value) {}
602 int64_t getValue() const { return Value; }
604 static inline bool classof(const Matcher *N) {
605 return N->getKind() == CheckAndImm;
608 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
611 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
612 virtual bool isEqualImpl(const Matcher *M) const {
613 return cast<CheckAndImmMatcher>(M)->Value == Value;
615 virtual unsigned getHashImpl() const { return Value; }
618 /// CheckOrImmMatcher - This checks to see if the current node is an 'and'
619 /// with something equivalent to the specified immediate.
620 class CheckOrImmMatcher : public Matcher {
623 CheckOrImmMatcher(int64_t value)
624 : Matcher(CheckOrImm), Value(value) {}
626 int64_t getValue() const { return Value; }
628 static inline bool classof(const Matcher *N) {
629 return N->getKind() == CheckOrImm;
632 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
635 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
636 virtual bool isEqualImpl(const Matcher *M) const {
637 return cast<CheckOrImmMatcher>(M)->Value == Value;
639 virtual unsigned getHashImpl() const { return Value; }
642 /// CheckFoldableChainNodeMatcher - This checks to see if the current node
643 /// (which defines a chain operand) is safe to fold into a larger pattern.
644 class CheckFoldableChainNodeMatcher : public Matcher {
646 CheckFoldableChainNodeMatcher()
647 : Matcher(CheckFoldableChainNode) {}
649 static inline bool classof(const Matcher *N) {
650 return N->getKind() == CheckFoldableChainNode;
653 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
656 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
657 virtual bool isEqualImpl(const Matcher *M) const { return true; }
658 virtual unsigned getHashImpl() const { return 0; }
661 /// CheckChainCompatibleMatcher - Verify that the current node's chain
662 /// operand is 'compatible' with the specified recorded node's.
663 class CheckChainCompatibleMatcher : public Matcher {
666 CheckChainCompatibleMatcher(unsigned previousop)
667 : Matcher(CheckChainCompatible), PreviousOp(previousop) {}
669 unsigned getPreviousOp() const { return PreviousOp; }
671 static inline bool classof(const Matcher *N) {
672 return N->getKind() == CheckChainCompatible;
675 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
678 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
679 virtual bool isEqualImpl(const Matcher *M) const {
680 return cast<CheckChainCompatibleMatcher>(M)->PreviousOp == PreviousOp;
682 virtual unsigned getHashImpl() const { return PreviousOp; }
685 /// EmitIntegerMatcher - This creates a new TargetConstant.
686 class EmitIntegerMatcher : public Matcher {
688 MVT::SimpleValueType VT;
690 EmitIntegerMatcher(int64_t val, MVT::SimpleValueType vt)
691 : Matcher(EmitInteger), Val(val), VT(vt) {}
693 int64_t getValue() const { return Val; }
694 MVT::SimpleValueType getVT() const { return VT; }
696 static inline bool classof(const Matcher *N) {
697 return N->getKind() == EmitInteger;
701 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
702 virtual bool isEqualImpl(const Matcher *M) const {
703 return cast<EmitIntegerMatcher>(M)->Val == Val &&
704 cast<EmitIntegerMatcher>(M)->VT == VT;
706 virtual unsigned getHashImpl() const { return (Val << 4) | VT; }
709 /// EmitStringIntegerMatcher - A target constant whose value is represented
711 class EmitStringIntegerMatcher : public Matcher {
713 MVT::SimpleValueType VT;
715 EmitStringIntegerMatcher(const std::string &val, MVT::SimpleValueType vt)
716 : Matcher(EmitStringInteger), Val(val), VT(vt) {}
718 const std::string &getValue() const { return Val; }
719 MVT::SimpleValueType getVT() const { return VT; }
721 static inline bool classof(const Matcher *N) {
722 return N->getKind() == EmitStringInteger;
726 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
727 virtual bool isEqualImpl(const Matcher *M) const {
728 return cast<EmitStringIntegerMatcher>(M)->Val == Val &&
729 cast<EmitStringIntegerMatcher>(M)->VT == VT;
731 virtual unsigned getHashImpl() const;
734 /// EmitRegisterMatcher - This creates a new TargetConstant.
735 class EmitRegisterMatcher : public Matcher {
736 /// Reg - The def for the register that we're emitting. If this is null, then
737 /// this is a reference to zero_reg.
739 MVT::SimpleValueType VT;
741 EmitRegisterMatcher(Record *reg, MVT::SimpleValueType vt)
742 : Matcher(EmitRegister), Reg(reg), VT(vt) {}
744 Record *getReg() const { return Reg; }
745 MVT::SimpleValueType getVT() const { return VT; }
747 static inline bool classof(const Matcher *N) {
748 return N->getKind() == EmitRegister;
752 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
753 virtual bool isEqualImpl(const Matcher *M) const {
754 return cast<EmitRegisterMatcher>(M)->Reg == Reg &&
755 cast<EmitRegisterMatcher>(M)->VT == VT;
757 virtual unsigned getHashImpl() const {
758 return ((unsigned)(intptr_t)Reg) << 4 | VT;
762 /// EmitConvertToTargetMatcher - Emit an operation that reads a specified
763 /// recorded node and converts it from being a ISD::Constant to
764 /// ISD::TargetConstant, likewise for ConstantFP.
765 class EmitConvertToTargetMatcher : public Matcher {
768 EmitConvertToTargetMatcher(unsigned slot)
769 : Matcher(EmitConvertToTarget), Slot(slot) {}
771 unsigned getSlot() const { return Slot; }
773 static inline bool classof(const Matcher *N) {
774 return N->getKind() == EmitConvertToTarget;
778 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
779 virtual bool isEqualImpl(const Matcher *M) const {
780 return cast<EmitConvertToTargetMatcher>(M)->Slot == Slot;
782 virtual unsigned getHashImpl() const { return Slot; }
785 /// EmitMergeInputChainsMatcher - Emit a node that merges a list of input
786 /// chains together with a token factor. The list of nodes are the nodes in the
787 /// matched pattern that have chain input/outputs. This node adds all input
788 /// chains of these nodes if they are not themselves a node in the pattern.
789 class EmitMergeInputChainsMatcher : public Matcher {
790 SmallVector<unsigned, 3> ChainNodes;
792 EmitMergeInputChainsMatcher(const unsigned *nodes, unsigned NumNodes)
793 : Matcher(EmitMergeInputChains), ChainNodes(nodes, nodes+NumNodes) {}
795 unsigned getNumNodes() const { return ChainNodes.size(); }
797 unsigned getNode(unsigned i) const {
798 assert(i < ChainNodes.size());
799 return ChainNodes[i];
802 static inline bool classof(const Matcher *N) {
803 return N->getKind() == EmitMergeInputChains;
807 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
808 virtual bool isEqualImpl(const Matcher *M) const {
809 return cast<EmitMergeInputChainsMatcher>(M)->ChainNodes == ChainNodes;
811 virtual unsigned getHashImpl() const;
814 /// EmitCopyToRegMatcher - Emit a CopyToReg node from a value to a physreg,
815 /// pushing the chain and flag results.
817 class EmitCopyToRegMatcher : public Matcher {
818 unsigned SrcSlot; // Value to copy into the physreg.
821 EmitCopyToRegMatcher(unsigned srcSlot, Record *destPhysReg)
822 : Matcher(EmitCopyToReg), SrcSlot(srcSlot), DestPhysReg(destPhysReg) {}
824 unsigned getSrcSlot() const { return SrcSlot; }
825 Record *getDestPhysReg() const { return DestPhysReg; }
827 static inline bool classof(const Matcher *N) {
828 return N->getKind() == EmitCopyToReg;
832 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
833 virtual bool isEqualImpl(const Matcher *M) const {
834 return cast<EmitCopyToRegMatcher>(M)->SrcSlot == SrcSlot &&
835 cast<EmitCopyToRegMatcher>(M)->DestPhysReg == DestPhysReg;
837 virtual unsigned getHashImpl() const {
838 return SrcSlot ^ ((unsigned)(intptr_t)DestPhysReg << 4);
844 /// EmitNodeXFormMatcher - Emit an operation that runs an SDNodeXForm on a
845 /// recorded node and records the result.
846 class EmitNodeXFormMatcher : public Matcher {
850 EmitNodeXFormMatcher(unsigned slot, Record *nodeXForm)
851 : Matcher(EmitNodeXForm), Slot(slot), NodeXForm(nodeXForm) {}
853 unsigned getSlot() const { return Slot; }
854 Record *getNodeXForm() const { return NodeXForm; }
856 static inline bool classof(const Matcher *N) {
857 return N->getKind() == EmitNodeXForm;
861 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
862 virtual bool isEqualImpl(const Matcher *M) const {
863 return cast<EmitNodeXFormMatcher>(M)->Slot == Slot &&
864 cast<EmitNodeXFormMatcher>(M)->NodeXForm == NodeXForm;
866 virtual unsigned getHashImpl() const {
867 return Slot ^ ((unsigned)(intptr_t)NodeXForm << 4);
871 /// EmitNodeMatcher - This signals a successful match and generates a node.
872 class EmitNodeMatcher : public Matcher {
873 std::string OpcodeName;
874 const SmallVector<MVT::SimpleValueType, 3> VTs;
875 const SmallVector<unsigned, 6> Operands;
876 bool HasChain, HasFlag, HasMemRefs;
878 /// NumFixedArityOperands - If this is a fixed arity node, this is set to -1.
879 /// If this is a varidic node, this is set to the number of fixed arity
880 /// operands in the root of the pattern. The rest are appended to this node.
881 int NumFixedArityOperands;
883 EmitNodeMatcher(const std::string &opcodeName,
884 const MVT::SimpleValueType *vts, unsigned numvts,
885 const unsigned *operands, unsigned numops,
886 bool hasChain, bool hasFlag, bool hasmemrefs,
887 int numfixedarityoperands)
888 : Matcher(EmitNode), OpcodeName(opcodeName),
889 VTs(vts, vts+numvts), Operands(operands, operands+numops),
890 HasChain(hasChain), HasFlag(hasFlag), HasMemRefs(hasmemrefs),
891 NumFixedArityOperands(numfixedarityoperands) {}
893 const std::string &getOpcodeName() const { return OpcodeName; }
895 unsigned getNumVTs() const { return VTs.size(); }
896 MVT::SimpleValueType getVT(unsigned i) const {
897 assert(i < VTs.size());
901 unsigned getNumOperands() const { return Operands.size(); }
902 unsigned getOperand(unsigned i) const {
903 assert(i < Operands.size());
907 bool hasChain() const { return HasChain; }
908 bool hasFlag() const { return HasFlag; }
909 bool hasMemRefs() const { return HasMemRefs; }
910 int getNumFixedArityOperands() const { return NumFixedArityOperands; }
912 static inline bool classof(const Matcher *N) {
913 return N->getKind() == EmitNode;
917 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
918 virtual bool isEqualImpl(const Matcher *M) const;
919 virtual unsigned getHashImpl() const;
922 /// MarkFlagResultsMatcher - This node indicates which non-root nodes in the
923 /// pattern produce flags. This allows CompleteMatchMatcher to update them
924 /// with the output flag of the resultant code.
925 class MarkFlagResultsMatcher : public Matcher {
926 SmallVector<unsigned, 3> FlagResultNodes;
928 MarkFlagResultsMatcher(const unsigned *nodes, unsigned NumNodes)
929 : Matcher(MarkFlagResults), FlagResultNodes(nodes, nodes+NumNodes) {}
931 unsigned getNumNodes() const { return FlagResultNodes.size(); }
933 unsigned getNode(unsigned i) const {
934 assert(i < FlagResultNodes.size());
935 return FlagResultNodes[i];
938 static inline bool classof(const Matcher *N) {
939 return N->getKind() == MarkFlagResults;
943 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
944 virtual bool isEqualImpl(const Matcher *M) const {
945 return cast<MarkFlagResultsMatcher>(M)->FlagResultNodes == FlagResultNodes;
947 virtual unsigned getHashImpl() const;
950 /// CompleteMatchMatcher - Complete a match by replacing the results of the
951 /// pattern with the newly generated nodes. This also prints a comment
952 /// indicating the source and dest patterns.
953 class CompleteMatchMatcher : public Matcher {
954 SmallVector<unsigned, 2> Results;
955 const PatternToMatch &Pattern;
957 CompleteMatchMatcher(const unsigned *results, unsigned numresults,
958 const PatternToMatch &pattern)
959 : Matcher(CompleteMatch), Results(results, results+numresults),
962 unsigned getNumResults() const { return Results.size(); }
963 unsigned getResult(unsigned R) const { return Results[R]; }
964 const PatternToMatch &getPattern() const { return Pattern; }
966 static inline bool classof(const Matcher *N) {
967 return N->getKind() == CompleteMatch;
971 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
972 virtual bool isEqualImpl(const Matcher *M) const {
973 return cast<CompleteMatchMatcher>(M)->Results == Results &&
974 &cast<CompleteMatchMatcher>(M)->Pattern == &Pattern;
976 virtual unsigned getHashImpl() const;
979 } // end namespace llvm