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;
28 Matcher *ConvertPatternToMatcher(const PatternToMatch &Pattern,unsigned Variant,
29 const CodeGenDAGPatterns &CGP);
30 Matcher *OptimizeMatcher(Matcher *Matcher, const CodeGenDAGPatterns &CGP);
31 void EmitMatcherTable(const Matcher *Matcher, const CodeGenDAGPatterns &CGP,
35 /// Matcher - Base class for all the the DAG ISel Matcher representation
38 // The next matcher node that is executed after this one. Null if this is the
39 // last stage of a match.
40 OwningPtr<Matcher> Next;
43 // Matcher state manipulation.
44 Scope, // Push a checking scope.
45 RecordNode, // Record the current node.
46 RecordChild, // Record a child of the current node.
47 RecordMemRef, // Record the memref in the current node.
48 CaptureFlagInput, // If the current node has an input flag, save it.
49 MoveChild, // Move current node to specified child.
50 MoveParent, // Move current node to parent.
52 // Predicate checking.
53 CheckSame, // Fail if not same as prev match.
54 CheckPatternPredicate,
55 CheckPredicate, // Fail if node predicate fails.
56 CheckOpcode, // Fail if not opcode.
57 SwitchOpcode, // Dispatch based on opcode.
58 CheckType, // Fail if not correct type.
59 CheckChildType, // Fail if child has wrong type.
60 CheckInteger, // Fail if wrong val.
61 CheckCondCode, // Fail if not condcode.
66 CheckFoldableChainNode,
68 // Node creation/emisssion.
69 EmitInteger, // Create a TargetConstant
70 EmitStringInteger, // Create a TargetConstant from a string.
71 EmitRegister, // Create a register.
72 EmitConvertToTarget, // Convert a imm/fpimm to target imm/fpimm
73 EmitMergeInputChains, // Merge together a chains for an input.
74 EmitCopyToReg, // Emit a copytoreg into a physreg.
75 EmitNode, // Create a DAG node
76 EmitNodeXForm, // Run a SDNodeXForm
77 MarkFlagResults, // Indicate which interior nodes have flag results.
78 CompleteMatch, // Finish a match and update the results.
79 MorphNodeTo // Build a node, finish a match and update results.
84 Matcher(KindTy K) : Kind(K) {}
88 KindTy getKind() const { return Kind; }
90 Matcher *getNext() { return Next.get(); }
91 const Matcher *getNext() const { return Next.get(); }
92 void setNext(Matcher *C) { Next.reset(C); }
93 Matcher *takeNext() { return Next.take(); }
95 OwningPtr<Matcher> &getNextPtr() { return Next; }
97 static inline bool classof(const Matcher *) { return true; }
99 bool isEqual(const Matcher *M) const {
100 if (getKind() != M->getKind()) return false;
101 return isEqualImpl(M);
104 unsigned getHash() const {
105 // Clear the high bit so we don't conflict with tombstones etc.
106 return ((getHashImpl() << 4) ^ getKind()) & (~0U>>1);
109 /// isSafeToReorderWithPatternPredicate - Return true if it is safe to sink a
110 /// PatternPredicate node past this one.
111 virtual bool isSafeToReorderWithPatternPredicate() const {
115 /// isContradictory - Return true of these two matchers could never match on
117 bool isContradictory(const Matcher *Other) const {
118 // Since this predicate is reflexive, we canonicalize the ordering so that
119 // we always match a node against nodes with kinds that are greater or equal
120 // to them. For example, we'll pass in a CheckType node as an argument to
121 // the CheckOpcode method, not the other way around.
122 if (getKind() < Other->getKind())
123 return isContradictoryImpl(Other);
124 return Other->isContradictoryImpl(this);
127 void print(raw_ostream &OS, unsigned indent = 0) const;
128 void printOne(raw_ostream &OS) const;
131 virtual void printImpl(raw_ostream &OS, unsigned indent) const = 0;
132 virtual bool isEqualImpl(const Matcher *M) const = 0;
133 virtual unsigned getHashImpl() const = 0;
134 virtual bool isContradictoryImpl(const Matcher *M) const { return false; }
137 /// ScopeMatcher - This attempts to match each of its children to find the first
138 /// one that successfully matches. If one child fails, it tries the next child.
139 /// If none of the children match then this check fails. It never has a 'next'.
140 class ScopeMatcher : public Matcher {
141 SmallVector<Matcher*, 4> Children;
143 ScopeMatcher(Matcher *const *children, unsigned numchildren)
144 : Matcher(Scope), Children(children, children+numchildren) {
146 virtual ~ScopeMatcher();
148 unsigned getNumChildren() const { return Children.size(); }
150 Matcher *getChild(unsigned i) { return Children[i]; }
151 const Matcher *getChild(unsigned i) const { return Children[i]; }
153 void resetChild(unsigned i, Matcher *N) {
158 Matcher *takeChild(unsigned i) {
159 Matcher *Res = Children[i];
164 void setNumChildren(unsigned NC) {
165 if (NC < Children.size()) {
166 // delete any children we're about to lose pointers to.
167 for (unsigned i = NC, e = Children.size(); i != e; ++i)
173 static inline bool classof(const Matcher *N) {
174 return N->getKind() == Scope;
178 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
179 virtual bool isEqualImpl(const Matcher *M) const { return false; }
180 virtual unsigned getHashImpl() const { return 12312; }
183 /// RecordMatcher - Save the current node in the operand list.
184 class RecordMatcher : public Matcher {
185 /// WhatFor - This is a string indicating why we're recording this. This
186 /// should only be used for comment generation not anything semantic.
189 /// ResultNo - The slot number in the RecordedNodes vector that this will be,
190 /// just printed as a comment.
193 RecordMatcher(const std::string &whatfor, unsigned resultNo)
194 : Matcher(RecordNode), WhatFor(whatfor), ResultNo(resultNo) {}
196 const std::string &getWhatFor() const { return WhatFor; }
197 unsigned getResultNo() const { return ResultNo; }
199 static inline bool classof(const Matcher *N) {
200 return N->getKind() == RecordNode;
203 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
205 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
206 virtual bool isEqualImpl(const Matcher *M) const { return true; }
207 virtual unsigned getHashImpl() const { return 0; }
210 /// RecordChildMatcher - Save a numbered child of the current node, or fail
211 /// the match if it doesn't exist. This is logically equivalent to:
212 /// MoveChild N + RecordNode + MoveParent.
213 class RecordChildMatcher : public Matcher {
216 /// WhatFor - This is a string indicating why we're recording this. This
217 /// should only be used for comment generation not anything semantic.
220 /// ResultNo - The slot number in the RecordedNodes vector that this will be,
221 /// just printed as a comment.
224 RecordChildMatcher(unsigned childno, const std::string &whatfor,
226 : Matcher(RecordChild), ChildNo(childno), WhatFor(whatfor),
227 ResultNo(resultNo) {}
229 unsigned getChildNo() const { return ChildNo; }
230 const std::string &getWhatFor() const { return WhatFor; }
231 unsigned getResultNo() const { return ResultNo; }
233 static inline bool classof(const Matcher *N) {
234 return N->getKind() == RecordChild;
237 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
240 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
241 virtual bool isEqualImpl(const Matcher *M) const {
242 return cast<RecordChildMatcher>(M)->getChildNo() == getChildNo();
244 virtual unsigned getHashImpl() const { return getChildNo(); }
247 /// RecordMemRefMatcher - Save the current node's memref.
248 class RecordMemRefMatcher : public Matcher {
250 RecordMemRefMatcher() : Matcher(RecordMemRef) {}
252 static inline bool classof(const Matcher *N) {
253 return N->getKind() == RecordMemRef;
256 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
259 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
260 virtual bool isEqualImpl(const Matcher *M) const { return true; }
261 virtual unsigned getHashImpl() const { return 0; }
265 /// CaptureFlagInputMatcher - If the current record has a flag input, record
266 /// it so that it is used as an input to the generated code.
267 class CaptureFlagInputMatcher : public Matcher {
269 CaptureFlagInputMatcher() : Matcher(CaptureFlagInput) {}
271 static inline bool classof(const Matcher *N) {
272 return N->getKind() == CaptureFlagInput;
275 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
278 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
279 virtual bool isEqualImpl(const Matcher *M) const { return true; }
280 virtual unsigned getHashImpl() const { return 0; }
283 /// MoveChildMatcher - This tells the interpreter to move into the
284 /// specified child node.
285 class MoveChildMatcher : public Matcher {
288 MoveChildMatcher(unsigned childNo) : Matcher(MoveChild), ChildNo(childNo) {}
290 unsigned getChildNo() const { return ChildNo; }
292 static inline bool classof(const Matcher *N) {
293 return N->getKind() == MoveChild;
296 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
299 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
300 virtual bool isEqualImpl(const Matcher *M) const {
301 return cast<MoveChildMatcher>(M)->getChildNo() == getChildNo();
303 virtual unsigned getHashImpl() const { return getChildNo(); }
306 /// MoveParentMatcher - This tells the interpreter to move to the parent
307 /// of the current node.
308 class MoveParentMatcher : public Matcher {
310 MoveParentMatcher() : Matcher(MoveParent) {}
312 static inline bool classof(const Matcher *N) {
313 return N->getKind() == MoveParent;
316 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
319 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
320 virtual bool isEqualImpl(const Matcher *M) const { return true; }
321 virtual unsigned getHashImpl() const { return 0; }
324 /// CheckSameMatcher - This checks to see if this node is exactly the same
325 /// node as the specified match that was recorded with 'Record'. This is used
326 /// when patterns have the same name in them, like '(mul GPR:$in, GPR:$in)'.
327 class CheckSameMatcher : public Matcher {
328 unsigned MatchNumber;
330 CheckSameMatcher(unsigned matchnumber)
331 : Matcher(CheckSame), MatchNumber(matchnumber) {}
333 unsigned getMatchNumber() const { return MatchNumber; }
335 static inline bool classof(const Matcher *N) {
336 return N->getKind() == CheckSame;
339 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
342 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
343 virtual bool isEqualImpl(const Matcher *M) const {
344 return cast<CheckSameMatcher>(M)->getMatchNumber() == getMatchNumber();
346 virtual unsigned getHashImpl() const { return getMatchNumber(); }
349 /// CheckPatternPredicateMatcher - This checks the target-specific predicate
350 /// to see if the entire pattern is capable of matching. This predicate does
351 /// not take a node as input. This is used for subtarget feature checks etc.
352 class CheckPatternPredicateMatcher : public Matcher {
353 std::string Predicate;
355 CheckPatternPredicateMatcher(StringRef predicate)
356 : Matcher(CheckPatternPredicate), Predicate(predicate) {}
358 StringRef getPredicate() const { return Predicate; }
360 static inline bool classof(const Matcher *N) {
361 return N->getKind() == CheckPatternPredicate;
364 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
367 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
368 virtual bool isEqualImpl(const Matcher *M) const {
369 return cast<CheckPatternPredicateMatcher>(M)->getPredicate() == Predicate;
371 virtual unsigned getHashImpl() const;
374 /// CheckPredicateMatcher - This checks the target-specific predicate to
375 /// see if the node is acceptable.
376 class CheckPredicateMatcher : public Matcher {
379 CheckPredicateMatcher(StringRef predname)
380 : Matcher(CheckPredicate), PredName(predname) {}
382 StringRef getPredicateName() const { return PredName; }
384 static inline bool classof(const Matcher *N) {
385 return N->getKind() == CheckPredicate;
389 //virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
392 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
393 virtual bool isEqualImpl(const Matcher *M) const {
394 return cast<CheckPredicateMatcher>(M)->PredName == PredName;
396 virtual unsigned getHashImpl() const;
400 /// CheckOpcodeMatcher - This checks to see if the current node has the
401 /// specified opcode, if not it fails to match.
402 class CheckOpcodeMatcher : public Matcher {
403 const SDNodeInfo &Opcode;
405 CheckOpcodeMatcher(const SDNodeInfo &opcode)
406 : Matcher(CheckOpcode), Opcode(opcode) {}
408 const SDNodeInfo &getOpcode() const { return Opcode; }
410 static inline bool classof(const Matcher *N) {
411 return N->getKind() == CheckOpcode;
414 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
417 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
418 virtual bool isEqualImpl(const Matcher *M) const;
419 virtual unsigned getHashImpl() const;
420 virtual bool isContradictoryImpl(const Matcher *M) const;
423 /// SwitchOpcodeMatcher - Switch based on the current node's opcode, dispatching
424 /// to one matcher per opcode. If the opcode doesn't match any of the cases,
425 /// then the match fails. This is semantically equivalent to a Scope node where
426 /// every child does a CheckOpcode, but is much faster.
427 class SwitchOpcodeMatcher : public Matcher {
428 SmallVector<std::pair<const SDNodeInfo*, Matcher*>, 8> Cases;
430 SwitchOpcodeMatcher(const std::pair<const SDNodeInfo*, Matcher*> *cases,
432 : Matcher(SwitchOpcode), Cases(cases, cases+numcases) {}
434 static inline bool classof(const Matcher *N) {
435 return N->getKind() == SwitchOpcode;
438 unsigned getNumCases() const { return Cases.size(); }
440 const SDNodeInfo &getCaseOpcode(unsigned i) const { return *Cases[i].first; }
441 Matcher *getCaseMatcher(unsigned i) { return Cases[i].second; }
442 const Matcher *getCaseMatcher(unsigned i) const { return Cases[i].second; }
445 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
446 virtual bool isEqualImpl(const Matcher *M) const { return false; }
447 virtual unsigned getHashImpl() const { return 4123; }
450 /// CheckTypeMatcher - This checks to see if the current node has the
451 /// specified type, if not it fails to match.
452 class CheckTypeMatcher : public Matcher {
453 MVT::SimpleValueType Type;
455 CheckTypeMatcher(MVT::SimpleValueType type)
456 : Matcher(CheckType), Type(type) {}
458 MVT::SimpleValueType getType() const { return Type; }
460 static inline bool classof(const Matcher *N) {
461 return N->getKind() == CheckType;
464 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
467 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
468 virtual bool isEqualImpl(const Matcher *M) const {
469 return cast<CheckTypeMatcher>(M)->Type == Type;
471 virtual unsigned getHashImpl() const { return Type; }
472 virtual bool isContradictoryImpl(const Matcher *M) const;
475 /// CheckChildTypeMatcher - This checks to see if a child node has the
476 /// specified type, if not it fails to match.
477 class CheckChildTypeMatcher : public Matcher {
479 MVT::SimpleValueType Type;
481 CheckChildTypeMatcher(unsigned childno, MVT::SimpleValueType type)
482 : Matcher(CheckChildType), ChildNo(childno), Type(type) {}
484 unsigned getChildNo() const { return ChildNo; }
485 MVT::SimpleValueType getType() const { return Type; }
487 static inline bool classof(const Matcher *N) {
488 return N->getKind() == CheckChildType;
491 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
494 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
495 virtual bool isEqualImpl(const Matcher *M) const {
496 return cast<CheckChildTypeMatcher>(M)->ChildNo == ChildNo &&
497 cast<CheckChildTypeMatcher>(M)->Type == Type;
499 virtual unsigned getHashImpl() const { return (Type << 3) | ChildNo; }
500 virtual bool isContradictoryImpl(const Matcher *M) const;
504 /// CheckIntegerMatcher - This checks to see if the current node is a
505 /// ConstantSDNode with the specified integer value, if not it fails to match.
506 class CheckIntegerMatcher : public Matcher {
509 CheckIntegerMatcher(int64_t value)
510 : Matcher(CheckInteger), Value(value) {}
512 int64_t getValue() const { return Value; }
514 static inline bool classof(const Matcher *N) {
515 return N->getKind() == CheckInteger;
518 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
521 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
522 virtual bool isEqualImpl(const Matcher *M) const {
523 return cast<CheckIntegerMatcher>(M)->Value == Value;
525 virtual unsigned getHashImpl() const { return Value; }
526 virtual bool isContradictoryImpl(const Matcher *M) const;
529 /// CheckCondCodeMatcher - This checks to see if the current node is a
530 /// CondCodeSDNode with the specified condition, if not it fails to match.
531 class CheckCondCodeMatcher : public Matcher {
532 StringRef CondCodeName;
534 CheckCondCodeMatcher(StringRef condcodename)
535 : Matcher(CheckCondCode), CondCodeName(condcodename) {}
537 StringRef getCondCodeName() const { return CondCodeName; }
539 static inline bool classof(const Matcher *N) {
540 return N->getKind() == CheckCondCode;
543 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
546 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
547 virtual bool isEqualImpl(const Matcher *M) const {
548 return cast<CheckCondCodeMatcher>(M)->CondCodeName == CondCodeName;
550 virtual unsigned getHashImpl() const;
553 /// CheckValueTypeMatcher - This checks to see if the current node is a
554 /// VTSDNode with the specified type, if not it fails to match.
555 class CheckValueTypeMatcher : public Matcher {
558 CheckValueTypeMatcher(StringRef type_name)
559 : Matcher(CheckValueType), TypeName(type_name) {}
561 StringRef getTypeName() const { return TypeName; }
563 static inline bool classof(const Matcher *N) {
564 return N->getKind() == CheckValueType;
567 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
570 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
571 virtual bool isEqualImpl(const Matcher *M) const {
572 return cast<CheckValueTypeMatcher>(M)->TypeName == TypeName;
574 virtual unsigned getHashImpl() const;
579 /// CheckComplexPatMatcher - This node runs the specified ComplexPattern on
580 /// the current node.
581 class CheckComplexPatMatcher : public Matcher {
582 const ComplexPattern &Pattern;
584 CheckComplexPatMatcher(const ComplexPattern &pattern)
585 : Matcher(CheckComplexPat), Pattern(pattern) {}
587 const ComplexPattern &getPattern() const { return Pattern; }
589 static inline bool classof(const Matcher *N) {
590 return N->getKind() == CheckComplexPat;
593 // Not safe to move a pattern predicate past a complex pattern.
594 virtual bool isSafeToReorderWithPatternPredicate() const { return false; }
597 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
598 virtual bool isEqualImpl(const Matcher *M) const {
599 return &cast<CheckComplexPatMatcher>(M)->Pattern == &Pattern;
601 virtual unsigned getHashImpl() const {
602 return (unsigned)(intptr_t)&Pattern;
606 /// CheckAndImmMatcher - This checks to see if the current node is an 'and'
607 /// with something equivalent to the specified immediate.
608 class CheckAndImmMatcher : public Matcher {
611 CheckAndImmMatcher(int64_t value)
612 : Matcher(CheckAndImm), Value(value) {}
614 int64_t getValue() const { return Value; }
616 static inline bool classof(const Matcher *N) {
617 return N->getKind() == CheckAndImm;
620 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
623 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
624 virtual bool isEqualImpl(const Matcher *M) const {
625 return cast<CheckAndImmMatcher>(M)->Value == Value;
627 virtual unsigned getHashImpl() const { return Value; }
630 /// CheckOrImmMatcher - This checks to see if the current node is an 'and'
631 /// with something equivalent to the specified immediate.
632 class CheckOrImmMatcher : public Matcher {
635 CheckOrImmMatcher(int64_t value)
636 : Matcher(CheckOrImm), Value(value) {}
638 int64_t getValue() const { return Value; }
640 static inline bool classof(const Matcher *N) {
641 return N->getKind() == CheckOrImm;
644 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
647 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
648 virtual bool isEqualImpl(const Matcher *M) const {
649 return cast<CheckOrImmMatcher>(M)->Value == Value;
651 virtual unsigned getHashImpl() const { return Value; }
654 /// CheckFoldableChainNodeMatcher - This checks to see if the current node
655 /// (which defines a chain operand) is safe to fold into a larger pattern.
656 class CheckFoldableChainNodeMatcher : public Matcher {
658 CheckFoldableChainNodeMatcher()
659 : Matcher(CheckFoldableChainNode) {}
661 static inline bool classof(const Matcher *N) {
662 return N->getKind() == CheckFoldableChainNode;
665 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
668 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
669 virtual bool isEqualImpl(const Matcher *M) const { return true; }
670 virtual unsigned getHashImpl() const { return 0; }
673 /// EmitIntegerMatcher - This creates a new TargetConstant.
674 class EmitIntegerMatcher : public Matcher {
676 MVT::SimpleValueType VT;
678 EmitIntegerMatcher(int64_t val, MVT::SimpleValueType vt)
679 : Matcher(EmitInteger), Val(val), VT(vt) {}
681 int64_t getValue() const { return Val; }
682 MVT::SimpleValueType getVT() const { return VT; }
684 static inline bool classof(const Matcher *N) {
685 return N->getKind() == EmitInteger;
689 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
690 virtual bool isEqualImpl(const Matcher *M) const {
691 return cast<EmitIntegerMatcher>(M)->Val == Val &&
692 cast<EmitIntegerMatcher>(M)->VT == VT;
694 virtual unsigned getHashImpl() const { return (Val << 4) | VT; }
697 /// EmitStringIntegerMatcher - A target constant whose value is represented
699 class EmitStringIntegerMatcher : public Matcher {
701 MVT::SimpleValueType VT;
703 EmitStringIntegerMatcher(const std::string &val, MVT::SimpleValueType vt)
704 : Matcher(EmitStringInteger), Val(val), VT(vt) {}
706 const std::string &getValue() const { return Val; }
707 MVT::SimpleValueType getVT() const { return VT; }
709 static inline bool classof(const Matcher *N) {
710 return N->getKind() == EmitStringInteger;
714 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
715 virtual bool isEqualImpl(const Matcher *M) const {
716 return cast<EmitStringIntegerMatcher>(M)->Val == Val &&
717 cast<EmitStringIntegerMatcher>(M)->VT == VT;
719 virtual unsigned getHashImpl() const;
722 /// EmitRegisterMatcher - This creates a new TargetConstant.
723 class EmitRegisterMatcher : public Matcher {
724 /// Reg - The def for the register that we're emitting. If this is null, then
725 /// this is a reference to zero_reg.
727 MVT::SimpleValueType VT;
729 EmitRegisterMatcher(Record *reg, MVT::SimpleValueType vt)
730 : Matcher(EmitRegister), Reg(reg), VT(vt) {}
732 Record *getReg() const { return Reg; }
733 MVT::SimpleValueType getVT() const { return VT; }
735 static inline bool classof(const Matcher *N) {
736 return N->getKind() == EmitRegister;
740 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
741 virtual bool isEqualImpl(const Matcher *M) const {
742 return cast<EmitRegisterMatcher>(M)->Reg == Reg &&
743 cast<EmitRegisterMatcher>(M)->VT == VT;
745 virtual unsigned getHashImpl() const {
746 return ((unsigned)(intptr_t)Reg) << 4 | VT;
750 /// EmitConvertToTargetMatcher - Emit an operation that reads a specified
751 /// recorded node and converts it from being a ISD::Constant to
752 /// ISD::TargetConstant, likewise for ConstantFP.
753 class EmitConvertToTargetMatcher : public Matcher {
756 EmitConvertToTargetMatcher(unsigned slot)
757 : Matcher(EmitConvertToTarget), Slot(slot) {}
759 unsigned getSlot() const { return Slot; }
761 static inline bool classof(const Matcher *N) {
762 return N->getKind() == EmitConvertToTarget;
766 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
767 virtual bool isEqualImpl(const Matcher *M) const {
768 return cast<EmitConvertToTargetMatcher>(M)->Slot == Slot;
770 virtual unsigned getHashImpl() const { return Slot; }
773 /// EmitMergeInputChainsMatcher - Emit a node that merges a list of input
774 /// chains together with a token factor. The list of nodes are the nodes in the
775 /// matched pattern that have chain input/outputs. This node adds all input
776 /// chains of these nodes if they are not themselves a node in the pattern.
777 class EmitMergeInputChainsMatcher : public Matcher {
778 SmallVector<unsigned, 3> ChainNodes;
780 EmitMergeInputChainsMatcher(const unsigned *nodes, unsigned NumNodes)
781 : Matcher(EmitMergeInputChains), ChainNodes(nodes, nodes+NumNodes) {}
783 unsigned getNumNodes() const { return ChainNodes.size(); }
785 unsigned getNode(unsigned i) const {
786 assert(i < ChainNodes.size());
787 return ChainNodes[i];
790 static inline bool classof(const Matcher *N) {
791 return N->getKind() == EmitMergeInputChains;
795 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
796 virtual bool isEqualImpl(const Matcher *M) const {
797 return cast<EmitMergeInputChainsMatcher>(M)->ChainNodes == ChainNodes;
799 virtual unsigned getHashImpl() const;
802 /// EmitCopyToRegMatcher - Emit a CopyToReg node from a value to a physreg,
803 /// pushing the chain and flag results.
805 class EmitCopyToRegMatcher : public Matcher {
806 unsigned SrcSlot; // Value to copy into the physreg.
809 EmitCopyToRegMatcher(unsigned srcSlot, Record *destPhysReg)
810 : Matcher(EmitCopyToReg), SrcSlot(srcSlot), DestPhysReg(destPhysReg) {}
812 unsigned getSrcSlot() const { return SrcSlot; }
813 Record *getDestPhysReg() const { return DestPhysReg; }
815 static inline bool classof(const Matcher *N) {
816 return N->getKind() == EmitCopyToReg;
820 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
821 virtual bool isEqualImpl(const Matcher *M) const {
822 return cast<EmitCopyToRegMatcher>(M)->SrcSlot == SrcSlot &&
823 cast<EmitCopyToRegMatcher>(M)->DestPhysReg == DestPhysReg;
825 virtual unsigned getHashImpl() const {
826 return SrcSlot ^ ((unsigned)(intptr_t)DestPhysReg << 4);
832 /// EmitNodeXFormMatcher - Emit an operation that runs an SDNodeXForm on a
833 /// recorded node and records the result.
834 class EmitNodeXFormMatcher : public Matcher {
838 EmitNodeXFormMatcher(unsigned slot, Record *nodeXForm)
839 : Matcher(EmitNodeXForm), Slot(slot), NodeXForm(nodeXForm) {}
841 unsigned getSlot() const { return Slot; }
842 Record *getNodeXForm() const { return NodeXForm; }
844 static inline bool classof(const Matcher *N) {
845 return N->getKind() == EmitNodeXForm;
849 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
850 virtual bool isEqualImpl(const Matcher *M) const {
851 return cast<EmitNodeXFormMatcher>(M)->Slot == Slot &&
852 cast<EmitNodeXFormMatcher>(M)->NodeXForm == NodeXForm;
854 virtual unsigned getHashImpl() const {
855 return Slot ^ ((unsigned)(intptr_t)NodeXForm << 4);
859 /// EmitNodeMatcherCommon - Common class shared between EmitNode and
861 class EmitNodeMatcherCommon : public Matcher {
862 std::string OpcodeName;
863 const SmallVector<MVT::SimpleValueType, 3> VTs;
864 const SmallVector<unsigned, 6> Operands;
865 bool HasChain, HasInFlag, HasOutFlag, HasMemRefs;
867 /// NumFixedArityOperands - If this is a fixed arity node, this is set to -1.
868 /// If this is a varidic node, this is set to the number of fixed arity
869 /// operands in the root of the pattern. The rest are appended to this node.
870 int NumFixedArityOperands;
872 EmitNodeMatcherCommon(const std::string &opcodeName,
873 const MVT::SimpleValueType *vts, unsigned numvts,
874 const unsigned *operands, unsigned numops,
875 bool hasChain, bool hasInFlag, bool hasOutFlag,
877 int numfixedarityoperands, bool isMorphNodeTo)
878 : Matcher(isMorphNodeTo ? MorphNodeTo : EmitNode), OpcodeName(opcodeName),
879 VTs(vts, vts+numvts), Operands(operands, operands+numops),
880 HasChain(hasChain), HasInFlag(hasInFlag), HasOutFlag(hasOutFlag),
881 HasMemRefs(hasmemrefs), NumFixedArityOperands(numfixedarityoperands) {}
883 const std::string &getOpcodeName() const { return OpcodeName; }
885 unsigned getNumVTs() const { return VTs.size(); }
886 MVT::SimpleValueType getVT(unsigned i) const {
887 assert(i < VTs.size());
891 unsigned getNumOperands() const { return Operands.size(); }
892 unsigned getOperand(unsigned i) const {
893 assert(i < Operands.size());
897 const SmallVectorImpl<MVT::SimpleValueType> &getVTList() const { return VTs; }
898 const SmallVectorImpl<unsigned> &getOperandList() const { return Operands; }
901 bool hasChain() const { return HasChain; }
902 bool hasInFlag() const { return HasInFlag; }
903 bool hasOutFlag() const { return HasOutFlag; }
904 bool hasMemRefs() const { return HasMemRefs; }
905 int getNumFixedArityOperands() const { return NumFixedArityOperands; }
907 static inline bool classof(const Matcher *N) {
908 return N->getKind() == EmitNode || N->getKind() == MorphNodeTo;
912 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
913 virtual bool isEqualImpl(const Matcher *M) const;
914 virtual unsigned getHashImpl() const;
917 /// EmitNodeMatcher - This signals a successful match and generates a node.
918 class EmitNodeMatcher : public EmitNodeMatcherCommon {
919 unsigned FirstResultSlot;
921 EmitNodeMatcher(const std::string &opcodeName,
922 const MVT::SimpleValueType *vts, unsigned numvts,
923 const unsigned *operands, unsigned numops,
924 bool hasChain, bool hasInFlag, bool hasOutFlag,
926 int numfixedarityoperands, unsigned firstresultslot)
927 : EmitNodeMatcherCommon(opcodeName, vts, numvts, operands, numops, hasChain,
928 hasInFlag, hasOutFlag, hasmemrefs,
929 numfixedarityoperands, false),
930 FirstResultSlot(firstresultslot) {}
932 unsigned getFirstResultSlot() const { return FirstResultSlot; }
934 static inline bool classof(const Matcher *N) {
935 return N->getKind() == EmitNode;
940 class MorphNodeToMatcher : public EmitNodeMatcherCommon {
941 const PatternToMatch &Pattern;
943 MorphNodeToMatcher(const std::string &opcodeName,
944 const MVT::SimpleValueType *vts, unsigned numvts,
945 const unsigned *operands, unsigned numops,
946 bool hasChain, bool hasInFlag, bool hasOutFlag,
948 int numfixedarityoperands, const PatternToMatch &pattern)
949 : EmitNodeMatcherCommon(opcodeName, vts, numvts, operands, numops, hasChain,
950 hasInFlag, hasOutFlag, hasmemrefs,
951 numfixedarityoperands, true),
955 const PatternToMatch &getPattern() const { return Pattern; }
957 static inline bool classof(const Matcher *N) {
958 return N->getKind() == MorphNodeTo;
962 /// MarkFlagResultsMatcher - This node indicates which non-root nodes in the
963 /// pattern produce flags. This allows CompleteMatchMatcher to update them
964 /// with the output flag of the resultant code.
965 class MarkFlagResultsMatcher : public Matcher {
966 SmallVector<unsigned, 3> FlagResultNodes;
968 MarkFlagResultsMatcher(const unsigned *nodes, unsigned NumNodes)
969 : Matcher(MarkFlagResults), FlagResultNodes(nodes, nodes+NumNodes) {}
971 unsigned getNumNodes() const { return FlagResultNodes.size(); }
973 unsigned getNode(unsigned i) const {
974 assert(i < FlagResultNodes.size());
975 return FlagResultNodes[i];
978 static inline bool classof(const Matcher *N) {
979 return N->getKind() == MarkFlagResults;
983 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
984 virtual bool isEqualImpl(const Matcher *M) const {
985 return cast<MarkFlagResultsMatcher>(M)->FlagResultNodes == FlagResultNodes;
987 virtual unsigned getHashImpl() const;
990 /// CompleteMatchMatcher - Complete a match by replacing the results of the
991 /// pattern with the newly generated nodes. This also prints a comment
992 /// indicating the source and dest patterns.
993 class CompleteMatchMatcher : public Matcher {
994 SmallVector<unsigned, 2> Results;
995 const PatternToMatch &Pattern;
997 CompleteMatchMatcher(const unsigned *results, unsigned numresults,
998 const PatternToMatch &pattern)
999 : Matcher(CompleteMatch), Results(results, results+numresults),
1002 unsigned getNumResults() const { return Results.size(); }
1003 unsigned getResult(unsigned R) const { return Results[R]; }
1004 const PatternToMatch &getPattern() const { return Pattern; }
1006 static inline bool classof(const Matcher *N) {
1007 return N->getKind() == CompleteMatch;
1011 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
1012 virtual bool isEqualImpl(const Matcher *M) const {
1013 return cast<CompleteMatchMatcher>(M)->Results == Results &&
1014 &cast<CompleteMatchMatcher>(M)->Pattern == &Pattern;
1016 virtual unsigned getHashImpl() const;
1019 } // end namespace llvm