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; }
516 /// CheckCondCodeMatcher - This checks to see if the current node is a
517 /// CondCodeSDNode with the specified condition, if not it fails to match.
518 class CheckCondCodeMatcher : public Matcher {
519 StringRef CondCodeName;
521 CheckCondCodeMatcher(StringRef condcodename)
522 : Matcher(CheckCondCode), CondCodeName(condcodename) {}
524 StringRef getCondCodeName() const { return CondCodeName; }
526 static inline bool classof(const Matcher *N) {
527 return N->getKind() == CheckCondCode;
530 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
533 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
534 virtual bool isEqualImpl(const Matcher *M) const {
535 return cast<CheckCondCodeMatcher>(M)->CondCodeName == CondCodeName;
537 virtual unsigned getHashImpl() const;
540 /// CheckValueTypeMatcher - This checks to see if the current node is a
541 /// VTSDNode with the specified type, if not it fails to match.
542 class CheckValueTypeMatcher : public Matcher {
545 CheckValueTypeMatcher(StringRef type_name)
546 : Matcher(CheckValueType), TypeName(type_name) {}
548 StringRef getTypeName() const { return TypeName; }
550 static inline bool classof(const Matcher *N) {
551 return N->getKind() == CheckValueType;
554 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
557 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
558 virtual bool isEqualImpl(const Matcher *M) const {
559 return cast<CheckValueTypeMatcher>(M)->TypeName == TypeName;
561 virtual unsigned getHashImpl() const;
566 /// CheckComplexPatMatcher - This node runs the specified ComplexPattern on
567 /// the current node.
568 class CheckComplexPatMatcher : public Matcher {
569 const ComplexPattern &Pattern;
571 CheckComplexPatMatcher(const ComplexPattern &pattern)
572 : Matcher(CheckComplexPat), Pattern(pattern) {}
574 const ComplexPattern &getPattern() const { return Pattern; }
576 static inline bool classof(const Matcher *N) {
577 return N->getKind() == CheckComplexPat;
580 // Not safe to move a pattern predicate past a complex pattern.
581 virtual bool isSafeToReorderWithPatternPredicate() const { return false; }
584 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
585 virtual bool isEqualImpl(const Matcher *M) const {
586 return &cast<CheckComplexPatMatcher>(M)->Pattern == &Pattern;
588 virtual unsigned getHashImpl() const {
589 return (unsigned)(intptr_t)&Pattern;
593 /// CheckAndImmMatcher - This checks to see if the current node is an 'and'
594 /// with something equivalent to the specified immediate.
595 class CheckAndImmMatcher : public Matcher {
598 CheckAndImmMatcher(int64_t value)
599 : Matcher(CheckAndImm), Value(value) {}
601 int64_t getValue() const { return Value; }
603 static inline bool classof(const Matcher *N) {
604 return N->getKind() == CheckAndImm;
607 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
610 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
611 virtual bool isEqualImpl(const Matcher *M) const {
612 return cast<CheckAndImmMatcher>(M)->Value == Value;
614 virtual unsigned getHashImpl() const { return Value; }
617 /// CheckOrImmMatcher - This checks to see if the current node is an 'and'
618 /// with something equivalent to the specified immediate.
619 class CheckOrImmMatcher : public Matcher {
622 CheckOrImmMatcher(int64_t value)
623 : Matcher(CheckOrImm), Value(value) {}
625 int64_t getValue() const { return Value; }
627 static inline bool classof(const Matcher *N) {
628 return N->getKind() == CheckOrImm;
631 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
634 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
635 virtual bool isEqualImpl(const Matcher *M) const {
636 return cast<CheckOrImmMatcher>(M)->Value == Value;
638 virtual unsigned getHashImpl() const { return Value; }
641 /// CheckFoldableChainNodeMatcher - This checks to see if the current node
642 /// (which defines a chain operand) is safe to fold into a larger pattern.
643 class CheckFoldableChainNodeMatcher : public Matcher {
645 CheckFoldableChainNodeMatcher()
646 : Matcher(CheckFoldableChainNode) {}
648 static inline bool classof(const Matcher *N) {
649 return N->getKind() == CheckFoldableChainNode;
652 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
655 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
656 virtual bool isEqualImpl(const Matcher *M) const { return true; }
657 virtual unsigned getHashImpl() const { return 0; }
660 /// CheckChainCompatibleMatcher - Verify that the current node's chain
661 /// operand is 'compatible' with the specified recorded node's.
662 class CheckChainCompatibleMatcher : public Matcher {
665 CheckChainCompatibleMatcher(unsigned previousop)
666 : Matcher(CheckChainCompatible), PreviousOp(previousop) {}
668 unsigned getPreviousOp() const { return PreviousOp; }
670 static inline bool classof(const Matcher *N) {
671 return N->getKind() == CheckChainCompatible;
674 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
677 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
678 virtual bool isEqualImpl(const Matcher *M) const {
679 return cast<CheckChainCompatibleMatcher>(M)->PreviousOp == PreviousOp;
681 virtual unsigned getHashImpl() const { return PreviousOp; }
684 /// EmitIntegerMatcher - This creates a new TargetConstant.
685 class EmitIntegerMatcher : public Matcher {
687 MVT::SimpleValueType VT;
689 EmitIntegerMatcher(int64_t val, MVT::SimpleValueType vt)
690 : Matcher(EmitInteger), Val(val), VT(vt) {}
692 int64_t getValue() const { return Val; }
693 MVT::SimpleValueType getVT() const { return VT; }
695 static inline bool classof(const Matcher *N) {
696 return N->getKind() == EmitInteger;
700 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
701 virtual bool isEqualImpl(const Matcher *M) const {
702 return cast<EmitIntegerMatcher>(M)->Val == Val &&
703 cast<EmitIntegerMatcher>(M)->VT == VT;
705 virtual unsigned getHashImpl() const { return (Val << 4) | VT; }
708 /// EmitStringIntegerMatcher - A target constant whose value is represented
710 class EmitStringIntegerMatcher : public Matcher {
712 MVT::SimpleValueType VT;
714 EmitStringIntegerMatcher(const std::string &val, MVT::SimpleValueType vt)
715 : Matcher(EmitStringInteger), Val(val), VT(vt) {}
717 const std::string &getValue() const { return Val; }
718 MVT::SimpleValueType getVT() const { return VT; }
720 static inline bool classof(const Matcher *N) {
721 return N->getKind() == EmitStringInteger;
725 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
726 virtual bool isEqualImpl(const Matcher *M) const {
727 return cast<EmitStringIntegerMatcher>(M)->Val == Val &&
728 cast<EmitStringIntegerMatcher>(M)->VT == VT;
730 virtual unsigned getHashImpl() const;
733 /// EmitRegisterMatcher - This creates a new TargetConstant.
734 class EmitRegisterMatcher : public Matcher {
735 /// Reg - The def for the register that we're emitting. If this is null, then
736 /// this is a reference to zero_reg.
738 MVT::SimpleValueType VT;
740 EmitRegisterMatcher(Record *reg, MVT::SimpleValueType vt)
741 : Matcher(EmitRegister), Reg(reg), VT(vt) {}
743 Record *getReg() const { return Reg; }
744 MVT::SimpleValueType getVT() const { return VT; }
746 static inline bool classof(const Matcher *N) {
747 return N->getKind() == EmitRegister;
751 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
752 virtual bool isEqualImpl(const Matcher *M) const {
753 return cast<EmitRegisterMatcher>(M)->Reg == Reg &&
754 cast<EmitRegisterMatcher>(M)->VT == VT;
756 virtual unsigned getHashImpl() const {
757 return ((unsigned)(intptr_t)Reg) << 4 | VT;
761 /// EmitConvertToTargetMatcher - Emit an operation that reads a specified
762 /// recorded node and converts it from being a ISD::Constant to
763 /// ISD::TargetConstant, likewise for ConstantFP.
764 class EmitConvertToTargetMatcher : public Matcher {
767 EmitConvertToTargetMatcher(unsigned slot)
768 : Matcher(EmitConvertToTarget), Slot(slot) {}
770 unsigned getSlot() const { return Slot; }
772 static inline bool classof(const Matcher *N) {
773 return N->getKind() == EmitConvertToTarget;
777 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
778 virtual bool isEqualImpl(const Matcher *M) const {
779 return cast<EmitConvertToTargetMatcher>(M)->Slot == Slot;
781 virtual unsigned getHashImpl() const { return Slot; }
784 /// EmitMergeInputChainsMatcher - Emit a node that merges a list of input
785 /// chains together with a token factor. The list of nodes are the nodes in the
786 /// matched pattern that have chain input/outputs. This node adds all input
787 /// chains of these nodes if they are not themselves a node in the pattern.
788 class EmitMergeInputChainsMatcher : public Matcher {
789 SmallVector<unsigned, 3> ChainNodes;
791 EmitMergeInputChainsMatcher(const unsigned *nodes, unsigned NumNodes)
792 : Matcher(EmitMergeInputChains), ChainNodes(nodes, nodes+NumNodes) {}
794 unsigned getNumNodes() const { return ChainNodes.size(); }
796 unsigned getNode(unsigned i) const {
797 assert(i < ChainNodes.size());
798 return ChainNodes[i];
801 static inline bool classof(const Matcher *N) {
802 return N->getKind() == EmitMergeInputChains;
806 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
807 virtual bool isEqualImpl(const Matcher *M) const {
808 return cast<EmitMergeInputChainsMatcher>(M)->ChainNodes == ChainNodes;
810 virtual unsigned getHashImpl() const;
813 /// EmitCopyToRegMatcher - Emit a CopyToReg node from a value to a physreg,
814 /// pushing the chain and flag results.
816 class EmitCopyToRegMatcher : public Matcher {
817 unsigned SrcSlot; // Value to copy into the physreg.
820 EmitCopyToRegMatcher(unsigned srcSlot, Record *destPhysReg)
821 : Matcher(EmitCopyToReg), SrcSlot(srcSlot), DestPhysReg(destPhysReg) {}
823 unsigned getSrcSlot() const { return SrcSlot; }
824 Record *getDestPhysReg() const { return DestPhysReg; }
826 static inline bool classof(const Matcher *N) {
827 return N->getKind() == EmitCopyToReg;
831 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
832 virtual bool isEqualImpl(const Matcher *M) const {
833 return cast<EmitCopyToRegMatcher>(M)->SrcSlot == SrcSlot &&
834 cast<EmitCopyToRegMatcher>(M)->DestPhysReg == DestPhysReg;
836 virtual unsigned getHashImpl() const {
837 return SrcSlot ^ ((unsigned)(intptr_t)DestPhysReg << 4);
843 /// EmitNodeXFormMatcher - Emit an operation that runs an SDNodeXForm on a
844 /// recorded node and records the result.
845 class EmitNodeXFormMatcher : public Matcher {
849 EmitNodeXFormMatcher(unsigned slot, Record *nodeXForm)
850 : Matcher(EmitNodeXForm), Slot(slot), NodeXForm(nodeXForm) {}
852 unsigned getSlot() const { return Slot; }
853 Record *getNodeXForm() const { return NodeXForm; }
855 static inline bool classof(const Matcher *N) {
856 return N->getKind() == EmitNodeXForm;
860 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
861 virtual bool isEqualImpl(const Matcher *M) const {
862 return cast<EmitNodeXFormMatcher>(M)->Slot == Slot &&
863 cast<EmitNodeXFormMatcher>(M)->NodeXForm == NodeXForm;
865 virtual unsigned getHashImpl() const {
866 return Slot ^ ((unsigned)(intptr_t)NodeXForm << 4);
870 /// EmitNodeMatcher - This signals a successful match and generates a node.
871 class EmitNodeMatcher : public Matcher {
872 std::string OpcodeName;
873 const SmallVector<MVT::SimpleValueType, 3> VTs;
874 const SmallVector<unsigned, 6> Operands;
875 bool HasChain, HasFlag, HasMemRefs;
877 /// NumFixedArityOperands - If this is a fixed arity node, this is set to -1.
878 /// If this is a varidic node, this is set to the number of fixed arity
879 /// operands in the root of the pattern. The rest are appended to this node.
880 int NumFixedArityOperands;
882 EmitNodeMatcher(const std::string &opcodeName,
883 const MVT::SimpleValueType *vts, unsigned numvts,
884 const unsigned *operands, unsigned numops,
885 bool hasChain, bool hasFlag, bool hasmemrefs,
886 int numfixedarityoperands)
887 : Matcher(EmitNode), OpcodeName(opcodeName),
888 VTs(vts, vts+numvts), Operands(operands, operands+numops),
889 HasChain(hasChain), HasFlag(hasFlag), HasMemRefs(hasmemrefs),
890 NumFixedArityOperands(numfixedarityoperands) {}
892 const std::string &getOpcodeName() const { return OpcodeName; }
894 unsigned getNumVTs() const { return VTs.size(); }
895 MVT::SimpleValueType getVT(unsigned i) const {
896 assert(i < VTs.size());
900 unsigned getNumOperands() const { return Operands.size(); }
901 unsigned getOperand(unsigned i) const {
902 assert(i < Operands.size());
906 bool hasChain() const { return HasChain; }
907 bool hasFlag() const { return HasFlag; }
908 bool hasMemRefs() const { return HasMemRefs; }
909 int getNumFixedArityOperands() const { return NumFixedArityOperands; }
911 static inline bool classof(const Matcher *N) {
912 return N->getKind() == EmitNode;
916 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
917 virtual bool isEqualImpl(const Matcher *M) const;
918 virtual unsigned getHashImpl() const;
921 /// MarkFlagResultsMatcher - This node indicates which non-root nodes in the
922 /// pattern produce flags. This allows CompleteMatchMatcher to update them
923 /// with the output flag of the resultant code.
924 class MarkFlagResultsMatcher : public Matcher {
925 SmallVector<unsigned, 3> FlagResultNodes;
927 MarkFlagResultsMatcher(const unsigned *nodes, unsigned NumNodes)
928 : Matcher(MarkFlagResults), FlagResultNodes(nodes, nodes+NumNodes) {}
930 unsigned getNumNodes() const { return FlagResultNodes.size(); }
932 unsigned getNode(unsigned i) const {
933 assert(i < FlagResultNodes.size());
934 return FlagResultNodes[i];
937 static inline bool classof(const Matcher *N) {
938 return N->getKind() == MarkFlagResults;
942 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
943 virtual bool isEqualImpl(const Matcher *M) const {
944 return cast<MarkFlagResultsMatcher>(M)->FlagResultNodes == FlagResultNodes;
946 virtual unsigned getHashImpl() const;
949 /// CompleteMatchMatcher - Complete a match by replacing the results of the
950 /// pattern with the newly generated nodes. This also prints a comment
951 /// indicating the source and dest patterns.
952 class CompleteMatchMatcher : public Matcher {
953 SmallVector<unsigned, 2> Results;
954 const PatternToMatch &Pattern;
956 CompleteMatchMatcher(const unsigned *results, unsigned numresults,
957 const PatternToMatch &pattern)
958 : Matcher(CompleteMatch), Results(results, results+numresults),
961 unsigned getNumResults() const { return Results.size(); }
962 unsigned getResult(unsigned R) const { return Results[R]; }
963 const PatternToMatch &getPattern() const { return Pattern; }
965 static inline bool classof(const Matcher *N) {
966 return N->getKind() == CompleteMatch;
970 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
971 virtual bool isEqualImpl(const Matcher *M) const {
972 return cast<CompleteMatchMatcher>(M)->Results == Results &&
973 &cast<CompleteMatchMatcher>(M)->Pattern == &Pattern;
975 virtual unsigned getHashImpl() const;
978 } // end namespace llvm