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,
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 CheckMultiOpcode, // Fail if not in opcode list.
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,
69 // Node creation/emisssion.
70 EmitInteger, // Create a TargetConstant
71 EmitStringInteger, // Create a TargetConstant from a string.
72 EmitRegister, // Create a register.
73 EmitConvertToTarget, // Convert a imm/fpimm to target imm/fpimm
74 EmitMergeInputChains, // Merge together a chains for an input.
75 EmitCopyToReg, // Emit a copytoreg into a physreg.
76 EmitNode, // Create a DAG node
77 EmitNodeXForm, // Run a SDNodeXForm
78 MarkFlagResults, // Indicate which interior nodes have flag results.
79 CompleteMatch, // Finish a match and update the results.
80 MorphNodeTo // Build a node, finish a match and update results.
85 Matcher(KindTy K) : Kind(K) {}
89 KindTy getKind() const { return Kind; }
91 Matcher *getNext() { return Next.get(); }
92 const Matcher *getNext() const { return Next.get(); }
93 void setNext(Matcher *C) { Next.reset(C); }
94 Matcher *takeNext() { return Next.take(); }
96 OwningPtr<Matcher> &getNextPtr() { return Next; }
98 static inline bool classof(const Matcher *) { return true; }
100 bool isEqual(const Matcher *M) const {
101 if (getKind() != M->getKind()) return false;
102 return isEqualImpl(M);
105 unsigned getHash() const {
106 // Clear the high bit so we don't conflict with tombstones etc.
107 return ((getHashImpl() << 4) ^ getKind()) & (~0U>>1);
110 /// isSafeToReorderWithPatternPredicate - Return true if it is safe to sink a
111 /// PatternPredicate node past this one.
112 virtual bool isSafeToReorderWithPatternPredicate() const {
116 /// isContradictory - Return true of these two matchers could never match on
118 bool isContradictory(const Matcher *Other) const {
119 // Since this predicate is reflexive, we canonicalize the ordering so that
120 // we always match a node against nodes with kinds that are greater or equal
121 // to them. For example, we'll pass in a CheckType node as an argument to
122 // the CheckOpcode method, not the other way around.
123 if (getKind() < Other->getKind())
124 return isContradictoryImpl(Other);
125 return Other->isContradictoryImpl(this);
128 void print(raw_ostream &OS, unsigned indent = 0) const;
129 void printOne(raw_ostream &OS) const;
132 virtual void printImpl(raw_ostream &OS, unsigned indent) const = 0;
133 virtual bool isEqualImpl(const Matcher *M) const = 0;
134 virtual unsigned getHashImpl() const = 0;
135 virtual bool isContradictoryImpl(const Matcher *M) const { return false; }
138 /// ScopeMatcher - This attempts to match each of its children to find the first
139 /// one that successfully matches. If one child fails, it tries the next child.
140 /// If none of the children match then this check fails. It never has a 'next'.
141 class ScopeMatcher : public Matcher {
142 SmallVector<Matcher*, 4> Children;
144 ScopeMatcher(Matcher *const *children, unsigned numchildren)
145 : Matcher(Scope), Children(children, children+numchildren) {
147 virtual ~ScopeMatcher();
149 unsigned getNumChildren() const { return Children.size(); }
151 Matcher *getChild(unsigned i) { return Children[i]; }
152 const Matcher *getChild(unsigned i) const { return Children[i]; }
154 void resetChild(unsigned i, Matcher *N) {
159 Matcher *takeChild(unsigned i) {
160 Matcher *Res = Children[i];
165 void setNumChildren(unsigned NC) {
166 if (NC < Children.size()) {
167 // delete any children we're about to lose pointers to.
168 for (unsigned i = NC, e = Children.size(); i != e; ++i)
174 static inline bool classof(const Matcher *N) {
175 return N->getKind() == Scope;
179 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
180 virtual bool isEqualImpl(const Matcher *M) const { return false; }
181 virtual unsigned getHashImpl() const { return 12312; }
184 /// RecordMatcher - Save the current node in the operand list.
185 class RecordMatcher : public Matcher {
186 /// WhatFor - This is a string indicating why we're recording this. This
187 /// should only be used for comment generation not anything semantic.
190 RecordMatcher(const std::string &whatfor)
191 : Matcher(RecordNode), WhatFor(whatfor) {}
193 const std::string &getWhatFor() const { return WhatFor; }
195 static inline bool classof(const Matcher *N) {
196 return N->getKind() == RecordNode;
199 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
201 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
202 virtual bool isEqualImpl(const Matcher *M) const { return true; }
203 virtual unsigned getHashImpl() const { return 0; }
206 /// RecordChildMatcher - Save a numbered child of the current node, or fail
207 /// the match if it doesn't exist. This is logically equivalent to:
208 /// MoveChild N + RecordNode + MoveParent.
209 class RecordChildMatcher : public Matcher {
212 /// WhatFor - This is a string indicating why we're recording this. This
213 /// should only be used for comment generation not anything semantic.
216 RecordChildMatcher(unsigned childno, const std::string &whatfor)
217 : Matcher(RecordChild), ChildNo(childno), WhatFor(whatfor) {}
219 unsigned getChildNo() const { return ChildNo; }
220 const std::string &getWhatFor() const { return WhatFor; }
222 static inline bool classof(const Matcher *N) {
223 return N->getKind() == RecordChild;
226 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
229 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
230 virtual bool isEqualImpl(const Matcher *M) const {
231 return cast<RecordChildMatcher>(M)->getChildNo() == getChildNo();
233 virtual unsigned getHashImpl() const { return getChildNo(); }
236 /// RecordMemRefMatcher - Save the current node's memref.
237 class RecordMemRefMatcher : public Matcher {
239 RecordMemRefMatcher() : Matcher(RecordMemRef) {}
241 static inline bool classof(const Matcher *N) {
242 return N->getKind() == RecordMemRef;
245 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
248 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
249 virtual bool isEqualImpl(const Matcher *M) const { return true; }
250 virtual unsigned getHashImpl() const { return 0; }
254 /// CaptureFlagInputMatcher - If the current record has a flag input, record
255 /// it so that it is used as an input to the generated code.
256 class CaptureFlagInputMatcher : public Matcher {
258 CaptureFlagInputMatcher() : Matcher(CaptureFlagInput) {}
260 static inline bool classof(const Matcher *N) {
261 return N->getKind() == CaptureFlagInput;
264 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
267 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
268 virtual bool isEqualImpl(const Matcher *M) const { return true; }
269 virtual unsigned getHashImpl() const { return 0; }
272 /// MoveChildMatcher - This tells the interpreter to move into the
273 /// specified child node.
274 class MoveChildMatcher : public Matcher {
277 MoveChildMatcher(unsigned childNo) : Matcher(MoveChild), ChildNo(childNo) {}
279 unsigned getChildNo() const { return ChildNo; }
281 static inline bool classof(const Matcher *N) {
282 return N->getKind() == MoveChild;
285 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
288 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
289 virtual bool isEqualImpl(const Matcher *M) const {
290 return cast<MoveChildMatcher>(M)->getChildNo() == getChildNo();
292 virtual unsigned getHashImpl() const { return getChildNo(); }
295 /// MoveParentMatcher - This tells the interpreter to move to the parent
296 /// of the current node.
297 class MoveParentMatcher : public Matcher {
299 MoveParentMatcher() : Matcher(MoveParent) {}
301 static inline bool classof(const Matcher *N) {
302 return N->getKind() == MoveParent;
305 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
308 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
309 virtual bool isEqualImpl(const Matcher *M) const { return true; }
310 virtual unsigned getHashImpl() const { return 0; }
313 /// CheckSameMatcher - This checks to see if this node is exactly the same
314 /// node as the specified match that was recorded with 'Record'. This is used
315 /// when patterns have the same name in them, like '(mul GPR:$in, GPR:$in)'.
316 class CheckSameMatcher : public Matcher {
317 unsigned MatchNumber;
319 CheckSameMatcher(unsigned matchnumber)
320 : Matcher(CheckSame), MatchNumber(matchnumber) {}
322 unsigned getMatchNumber() const { return MatchNumber; }
324 static inline bool classof(const Matcher *N) {
325 return N->getKind() == CheckSame;
328 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
331 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
332 virtual bool isEqualImpl(const Matcher *M) const {
333 return cast<CheckSameMatcher>(M)->getMatchNumber() == getMatchNumber();
335 virtual unsigned getHashImpl() const { return getMatchNumber(); }
338 /// CheckPatternPredicateMatcher - This checks the target-specific predicate
339 /// to see if the entire pattern is capable of matching. This predicate does
340 /// not take a node as input. This is used for subtarget feature checks etc.
341 class CheckPatternPredicateMatcher : public Matcher {
342 std::string Predicate;
344 CheckPatternPredicateMatcher(StringRef predicate)
345 : Matcher(CheckPatternPredicate), Predicate(predicate) {}
347 StringRef getPredicate() const { return Predicate; }
349 static inline bool classof(const Matcher *N) {
350 return N->getKind() == CheckPatternPredicate;
353 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
356 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
357 virtual bool isEqualImpl(const Matcher *M) const {
358 return cast<CheckPatternPredicateMatcher>(M)->getPredicate() == Predicate;
360 virtual unsigned getHashImpl() const;
363 /// CheckPredicateMatcher - This checks the target-specific predicate to
364 /// see if the node is acceptable.
365 class CheckPredicateMatcher : public Matcher {
368 CheckPredicateMatcher(StringRef predname)
369 : Matcher(CheckPredicate), PredName(predname) {}
371 StringRef getPredicateName() const { return PredName; }
373 static inline bool classof(const Matcher *N) {
374 return N->getKind() == CheckPredicate;
378 //virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
381 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
382 virtual bool isEqualImpl(const Matcher *M) const {
383 return cast<CheckPredicateMatcher>(M)->PredName == PredName;
385 virtual unsigned getHashImpl() const;
389 /// CheckOpcodeMatcher - This checks to see if the current node has the
390 /// specified opcode, if not it fails to match.
391 class CheckOpcodeMatcher : public Matcher {
392 const SDNodeInfo &Opcode;
394 CheckOpcodeMatcher(const SDNodeInfo &opcode)
395 : Matcher(CheckOpcode), Opcode(opcode) {}
397 const SDNodeInfo &getOpcode() const { return Opcode; }
399 static inline bool classof(const Matcher *N) {
400 return N->getKind() == CheckOpcode;
403 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
406 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
407 virtual bool isEqualImpl(const Matcher *M) const {
408 return &cast<CheckOpcodeMatcher>(M)->Opcode == &Opcode;
410 virtual unsigned getHashImpl() const;
411 virtual bool isContradictoryImpl(const Matcher *M) const;
414 /// CheckMultiOpcodeMatcher - This checks to see if the current node has one
415 /// of the specified opcode, if not it fails to match.
416 class CheckMultiOpcodeMatcher : public Matcher {
417 SmallVector<const SDNodeInfo*, 4> Opcodes;
419 CheckMultiOpcodeMatcher(const SDNodeInfo * const *opcodes, unsigned numops)
420 : Matcher(CheckMultiOpcode), Opcodes(opcodes, opcodes+numops) {}
422 unsigned getNumOpcodes() const { return Opcodes.size(); }
423 const SDNodeInfo &getOpcode(unsigned i) const { return *Opcodes[i]; }
425 static inline bool classof(const Matcher *N) {
426 return N->getKind() == CheckMultiOpcode;
429 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
432 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
433 virtual bool isEqualImpl(const Matcher *M) const {
434 return cast<CheckMultiOpcodeMatcher>(M)->Opcodes == Opcodes;
436 virtual unsigned getHashImpl() const;
441 /// CheckTypeMatcher - This checks to see if the current node has the
442 /// specified type, if not it fails to match.
443 class CheckTypeMatcher : public Matcher {
444 MVT::SimpleValueType Type;
446 CheckTypeMatcher(MVT::SimpleValueType type)
447 : Matcher(CheckType), Type(type) {}
449 MVT::SimpleValueType getType() const { return Type; }
451 static inline bool classof(const Matcher *N) {
452 return N->getKind() == CheckType;
455 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
458 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
459 virtual bool isEqualImpl(const Matcher *M) const {
460 return cast<CheckTypeMatcher>(M)->Type == Type;
462 virtual unsigned getHashImpl() const { return Type; }
463 virtual bool isContradictoryImpl(const Matcher *M) const;
466 /// CheckChildTypeMatcher - This checks to see if a child node has the
467 /// specified type, if not it fails to match.
468 class CheckChildTypeMatcher : public Matcher {
470 MVT::SimpleValueType Type;
472 CheckChildTypeMatcher(unsigned childno, MVT::SimpleValueType type)
473 : Matcher(CheckChildType), ChildNo(childno), Type(type) {}
475 unsigned getChildNo() const { return ChildNo; }
476 MVT::SimpleValueType getType() const { return Type; }
478 static inline bool classof(const Matcher *N) {
479 return N->getKind() == CheckChildType;
482 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
485 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
486 virtual bool isEqualImpl(const Matcher *M) const {
487 return cast<CheckChildTypeMatcher>(M)->ChildNo == ChildNo &&
488 cast<CheckChildTypeMatcher>(M)->Type == Type;
490 virtual unsigned getHashImpl() const { return (Type << 3) | ChildNo; }
491 virtual bool isContradictoryImpl(const Matcher *M) const;
495 /// CheckIntegerMatcher - This checks to see if the current node is a
496 /// ConstantSDNode with the specified integer value, if not it fails to match.
497 class CheckIntegerMatcher : public Matcher {
500 CheckIntegerMatcher(int64_t value)
501 : Matcher(CheckInteger), Value(value) {}
503 int64_t getValue() const { return Value; }
505 static inline bool classof(const Matcher *N) {
506 return N->getKind() == CheckInteger;
509 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
512 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
513 virtual bool isEqualImpl(const Matcher *M) const {
514 return cast<CheckIntegerMatcher>(M)->Value == Value;
516 virtual unsigned getHashImpl() const { return Value; }
517 virtual bool isContradictoryImpl(const Matcher *M) const;
520 /// CheckCondCodeMatcher - This checks to see if the current node is a
521 /// CondCodeSDNode with the specified condition, if not it fails to match.
522 class CheckCondCodeMatcher : public Matcher {
523 StringRef CondCodeName;
525 CheckCondCodeMatcher(StringRef condcodename)
526 : Matcher(CheckCondCode), CondCodeName(condcodename) {}
528 StringRef getCondCodeName() const { return CondCodeName; }
530 static inline bool classof(const Matcher *N) {
531 return N->getKind() == CheckCondCode;
534 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
537 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
538 virtual bool isEqualImpl(const Matcher *M) const {
539 return cast<CheckCondCodeMatcher>(M)->CondCodeName == CondCodeName;
541 virtual unsigned getHashImpl() const;
544 /// CheckValueTypeMatcher - This checks to see if the current node is a
545 /// VTSDNode with the specified type, if not it fails to match.
546 class CheckValueTypeMatcher : public Matcher {
549 CheckValueTypeMatcher(StringRef type_name)
550 : Matcher(CheckValueType), TypeName(type_name) {}
552 StringRef getTypeName() const { return TypeName; }
554 static inline bool classof(const Matcher *N) {
555 return N->getKind() == CheckValueType;
558 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
561 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
562 virtual bool isEqualImpl(const Matcher *M) const {
563 return cast<CheckValueTypeMatcher>(M)->TypeName == TypeName;
565 virtual unsigned getHashImpl() const;
570 /// CheckComplexPatMatcher - This node runs the specified ComplexPattern on
571 /// the current node.
572 class CheckComplexPatMatcher : public Matcher {
573 const ComplexPattern &Pattern;
575 CheckComplexPatMatcher(const ComplexPattern &pattern)
576 : Matcher(CheckComplexPat), Pattern(pattern) {}
578 const ComplexPattern &getPattern() const { return Pattern; }
580 static inline bool classof(const Matcher *N) {
581 return N->getKind() == CheckComplexPat;
584 // Not safe to move a pattern predicate past a complex pattern.
585 virtual bool isSafeToReorderWithPatternPredicate() const { return false; }
588 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
589 virtual bool isEqualImpl(const Matcher *M) const {
590 return &cast<CheckComplexPatMatcher>(M)->Pattern == &Pattern;
592 virtual unsigned getHashImpl() const {
593 return (unsigned)(intptr_t)&Pattern;
597 /// CheckAndImmMatcher - This checks to see if the current node is an 'and'
598 /// with something equivalent to the specified immediate.
599 class CheckAndImmMatcher : public Matcher {
602 CheckAndImmMatcher(int64_t value)
603 : Matcher(CheckAndImm), Value(value) {}
605 int64_t getValue() const { return Value; }
607 static inline bool classof(const Matcher *N) {
608 return N->getKind() == CheckAndImm;
611 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
614 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
615 virtual bool isEqualImpl(const Matcher *M) const {
616 return cast<CheckAndImmMatcher>(M)->Value == Value;
618 virtual unsigned getHashImpl() const { return Value; }
621 /// CheckOrImmMatcher - This checks to see if the current node is an 'and'
622 /// with something equivalent to the specified immediate.
623 class CheckOrImmMatcher : public Matcher {
626 CheckOrImmMatcher(int64_t value)
627 : Matcher(CheckOrImm), Value(value) {}
629 int64_t getValue() const { return Value; }
631 static inline bool classof(const Matcher *N) {
632 return N->getKind() == CheckOrImm;
635 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
638 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
639 virtual bool isEqualImpl(const Matcher *M) const {
640 return cast<CheckOrImmMatcher>(M)->Value == Value;
642 virtual unsigned getHashImpl() const { return Value; }
645 /// CheckFoldableChainNodeMatcher - This checks to see if the current node
646 /// (which defines a chain operand) is safe to fold into a larger pattern.
647 class CheckFoldableChainNodeMatcher : public Matcher {
649 CheckFoldableChainNodeMatcher()
650 : Matcher(CheckFoldableChainNode) {}
652 static inline bool classof(const Matcher *N) {
653 return N->getKind() == CheckFoldableChainNode;
656 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
659 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
660 virtual bool isEqualImpl(const Matcher *M) const { return true; }
661 virtual unsigned getHashImpl() const { return 0; }
664 /// CheckChainCompatibleMatcher - Verify that the current node's chain
665 /// operand is 'compatible' with the specified recorded node's.
666 class CheckChainCompatibleMatcher : public Matcher {
669 CheckChainCompatibleMatcher(unsigned previousop)
670 : Matcher(CheckChainCompatible), PreviousOp(previousop) {}
672 unsigned getPreviousOp() const { return PreviousOp; }
674 static inline bool classof(const Matcher *N) {
675 return N->getKind() == CheckChainCompatible;
678 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
681 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
682 virtual bool isEqualImpl(const Matcher *M) const {
683 return cast<CheckChainCompatibleMatcher>(M)->PreviousOp == PreviousOp;
685 virtual unsigned getHashImpl() const { return PreviousOp; }
688 /// EmitIntegerMatcher - This creates a new TargetConstant.
689 class EmitIntegerMatcher : public Matcher {
691 MVT::SimpleValueType VT;
693 EmitIntegerMatcher(int64_t val, MVT::SimpleValueType vt)
694 : Matcher(EmitInteger), Val(val), VT(vt) {}
696 int64_t getValue() const { return Val; }
697 MVT::SimpleValueType getVT() const { return VT; }
699 static inline bool classof(const Matcher *N) {
700 return N->getKind() == EmitInteger;
704 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
705 virtual bool isEqualImpl(const Matcher *M) const {
706 return cast<EmitIntegerMatcher>(M)->Val == Val &&
707 cast<EmitIntegerMatcher>(M)->VT == VT;
709 virtual unsigned getHashImpl() const { return (Val << 4) | VT; }
712 /// EmitStringIntegerMatcher - A target constant whose value is represented
714 class EmitStringIntegerMatcher : public Matcher {
716 MVT::SimpleValueType VT;
718 EmitStringIntegerMatcher(const std::string &val, MVT::SimpleValueType vt)
719 : Matcher(EmitStringInteger), Val(val), VT(vt) {}
721 const std::string &getValue() const { return Val; }
722 MVT::SimpleValueType getVT() const { return VT; }
724 static inline bool classof(const Matcher *N) {
725 return N->getKind() == EmitStringInteger;
729 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
730 virtual bool isEqualImpl(const Matcher *M) const {
731 return cast<EmitStringIntegerMatcher>(M)->Val == Val &&
732 cast<EmitStringIntegerMatcher>(M)->VT == VT;
734 virtual unsigned getHashImpl() const;
737 /// EmitRegisterMatcher - This creates a new TargetConstant.
738 class EmitRegisterMatcher : public Matcher {
739 /// Reg - The def for the register that we're emitting. If this is null, then
740 /// this is a reference to zero_reg.
742 MVT::SimpleValueType VT;
744 EmitRegisterMatcher(Record *reg, MVT::SimpleValueType vt)
745 : Matcher(EmitRegister), Reg(reg), VT(vt) {}
747 Record *getReg() const { return Reg; }
748 MVT::SimpleValueType getVT() const { return VT; }
750 static inline bool classof(const Matcher *N) {
751 return N->getKind() == EmitRegister;
755 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
756 virtual bool isEqualImpl(const Matcher *M) const {
757 return cast<EmitRegisterMatcher>(M)->Reg == Reg &&
758 cast<EmitRegisterMatcher>(M)->VT == VT;
760 virtual unsigned getHashImpl() const {
761 return ((unsigned)(intptr_t)Reg) << 4 | VT;
765 /// EmitConvertToTargetMatcher - Emit an operation that reads a specified
766 /// recorded node and converts it from being a ISD::Constant to
767 /// ISD::TargetConstant, likewise for ConstantFP.
768 class EmitConvertToTargetMatcher : public Matcher {
771 EmitConvertToTargetMatcher(unsigned slot)
772 : Matcher(EmitConvertToTarget), Slot(slot) {}
774 unsigned getSlot() const { return Slot; }
776 static inline bool classof(const Matcher *N) {
777 return N->getKind() == EmitConvertToTarget;
781 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
782 virtual bool isEqualImpl(const Matcher *M) const {
783 return cast<EmitConvertToTargetMatcher>(M)->Slot == Slot;
785 virtual unsigned getHashImpl() const { return Slot; }
788 /// EmitMergeInputChainsMatcher - Emit a node that merges a list of input
789 /// chains together with a token factor. The list of nodes are the nodes in the
790 /// matched pattern that have chain input/outputs. This node adds all input
791 /// chains of these nodes if they are not themselves a node in the pattern.
792 class EmitMergeInputChainsMatcher : public Matcher {
793 SmallVector<unsigned, 3> ChainNodes;
795 EmitMergeInputChainsMatcher(const unsigned *nodes, unsigned NumNodes)
796 : Matcher(EmitMergeInputChains), ChainNodes(nodes, nodes+NumNodes) {}
798 unsigned getNumNodes() const { return ChainNodes.size(); }
800 unsigned getNode(unsigned i) const {
801 assert(i < ChainNodes.size());
802 return ChainNodes[i];
805 static inline bool classof(const Matcher *N) {
806 return N->getKind() == EmitMergeInputChains;
810 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
811 virtual bool isEqualImpl(const Matcher *M) const {
812 return cast<EmitMergeInputChainsMatcher>(M)->ChainNodes == ChainNodes;
814 virtual unsigned getHashImpl() const;
817 /// EmitCopyToRegMatcher - Emit a CopyToReg node from a value to a physreg,
818 /// pushing the chain and flag results.
820 class EmitCopyToRegMatcher : public Matcher {
821 unsigned SrcSlot; // Value to copy into the physreg.
824 EmitCopyToRegMatcher(unsigned srcSlot, Record *destPhysReg)
825 : Matcher(EmitCopyToReg), SrcSlot(srcSlot), DestPhysReg(destPhysReg) {}
827 unsigned getSrcSlot() const { return SrcSlot; }
828 Record *getDestPhysReg() const { return DestPhysReg; }
830 static inline bool classof(const Matcher *N) {
831 return N->getKind() == EmitCopyToReg;
835 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
836 virtual bool isEqualImpl(const Matcher *M) const {
837 return cast<EmitCopyToRegMatcher>(M)->SrcSlot == SrcSlot &&
838 cast<EmitCopyToRegMatcher>(M)->DestPhysReg == DestPhysReg;
840 virtual unsigned getHashImpl() const {
841 return SrcSlot ^ ((unsigned)(intptr_t)DestPhysReg << 4);
847 /// EmitNodeXFormMatcher - Emit an operation that runs an SDNodeXForm on a
848 /// recorded node and records the result.
849 class EmitNodeXFormMatcher : public Matcher {
853 EmitNodeXFormMatcher(unsigned slot, Record *nodeXForm)
854 : Matcher(EmitNodeXForm), Slot(slot), NodeXForm(nodeXForm) {}
856 unsigned getSlot() const { return Slot; }
857 Record *getNodeXForm() const { return NodeXForm; }
859 static inline bool classof(const Matcher *N) {
860 return N->getKind() == EmitNodeXForm;
864 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
865 virtual bool isEqualImpl(const Matcher *M) const {
866 return cast<EmitNodeXFormMatcher>(M)->Slot == Slot &&
867 cast<EmitNodeXFormMatcher>(M)->NodeXForm == NodeXForm;
869 virtual unsigned getHashImpl() const {
870 return Slot ^ ((unsigned)(intptr_t)NodeXForm << 4);
874 /// EmitNodeMatcherCommon - Common class shared between EmitNode and
876 class EmitNodeMatcherCommon : public Matcher {
877 std::string OpcodeName;
878 const SmallVector<MVT::SimpleValueType, 3> VTs;
879 const SmallVector<unsigned, 6> Operands;
880 bool HasChain, HasInFlag, HasOutFlag, HasMemRefs;
882 /// NumFixedArityOperands - If this is a fixed arity node, this is set to -1.
883 /// If this is a varidic node, this is set to the number of fixed arity
884 /// operands in the root of the pattern. The rest are appended to this node.
885 int NumFixedArityOperands;
887 EmitNodeMatcherCommon(const std::string &opcodeName,
888 const MVT::SimpleValueType *vts, unsigned numvts,
889 const unsigned *operands, unsigned numops,
890 bool hasChain, bool hasInFlag, bool hasOutFlag,
892 int numfixedarityoperands, bool isMorphNodeTo)
893 : Matcher(isMorphNodeTo ? MorphNodeTo : EmitNode), OpcodeName(opcodeName),
894 VTs(vts, vts+numvts), Operands(operands, operands+numops),
895 HasChain(hasChain), HasInFlag(hasInFlag), HasOutFlag(hasOutFlag),
896 HasMemRefs(hasmemrefs), NumFixedArityOperands(numfixedarityoperands) {}
898 const std::string &getOpcodeName() const { return OpcodeName; }
900 unsigned getNumVTs() const { return VTs.size(); }
901 MVT::SimpleValueType getVT(unsigned i) const {
902 assert(i < VTs.size());
906 unsigned getNumOperands() const { return Operands.size(); }
907 unsigned getOperand(unsigned i) const {
908 assert(i < Operands.size());
912 const SmallVectorImpl<MVT::SimpleValueType> &getVTList() const { return VTs; }
913 const SmallVectorImpl<unsigned> &getOperandList() const { return Operands; }
916 bool hasChain() const { return HasChain; }
917 bool hasInFlag() const { return HasInFlag; }
918 bool hasOutFlag() const { return HasOutFlag; }
919 bool hasMemRefs() const { return HasMemRefs; }
920 int getNumFixedArityOperands() const { return NumFixedArityOperands; }
922 static inline bool classof(const Matcher *N) {
923 return N->getKind() == EmitNode || N->getKind() == MorphNodeTo;
927 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
928 virtual bool isEqualImpl(const Matcher *M) const;
929 virtual unsigned getHashImpl() const;
932 /// EmitNodeMatcher - This signals a successful match and generates a node.
933 class EmitNodeMatcher : public EmitNodeMatcherCommon {
934 unsigned FirstResultSlot;
936 EmitNodeMatcher(const std::string &opcodeName,
937 const MVT::SimpleValueType *vts, unsigned numvts,
938 const unsigned *operands, unsigned numops,
939 bool hasChain, bool hasInFlag, bool hasOutFlag,
941 int numfixedarityoperands, unsigned firstresultslot)
942 : EmitNodeMatcherCommon(opcodeName, vts, numvts, operands, numops, hasChain,
943 hasInFlag, hasOutFlag, hasmemrefs,
944 numfixedarityoperands, false),
945 FirstResultSlot(firstresultslot) {}
947 unsigned getFirstResultSlot() const { return FirstResultSlot; }
949 static inline bool classof(const Matcher *N) {
950 return N->getKind() == EmitNode;
955 class MorphNodeToMatcher : public EmitNodeMatcherCommon {
956 const PatternToMatch &Pattern;
958 MorphNodeToMatcher(const std::string &opcodeName,
959 const MVT::SimpleValueType *vts, unsigned numvts,
960 const unsigned *operands, unsigned numops,
961 bool hasChain, bool hasInFlag, bool hasOutFlag,
963 int numfixedarityoperands, const PatternToMatch &pattern)
964 : EmitNodeMatcherCommon(opcodeName, vts, numvts, operands, numops, hasChain,
965 hasInFlag, hasOutFlag, hasmemrefs,
966 numfixedarityoperands, true),
970 const PatternToMatch &getPattern() const { return Pattern; }
972 static inline bool classof(const Matcher *N) {
973 return N->getKind() == MorphNodeTo;
977 /// MarkFlagResultsMatcher - This node indicates which non-root nodes in the
978 /// pattern produce flags. This allows CompleteMatchMatcher to update them
979 /// with the output flag of the resultant code.
980 class MarkFlagResultsMatcher : public Matcher {
981 SmallVector<unsigned, 3> FlagResultNodes;
983 MarkFlagResultsMatcher(const unsigned *nodes, unsigned NumNodes)
984 : Matcher(MarkFlagResults), FlagResultNodes(nodes, nodes+NumNodes) {}
986 unsigned getNumNodes() const { return FlagResultNodes.size(); }
988 unsigned getNode(unsigned i) const {
989 assert(i < FlagResultNodes.size());
990 return FlagResultNodes[i];
993 static inline bool classof(const Matcher *N) {
994 return N->getKind() == MarkFlagResults;
998 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
999 virtual bool isEqualImpl(const Matcher *M) const {
1000 return cast<MarkFlagResultsMatcher>(M)->FlagResultNodes == FlagResultNodes;
1002 virtual unsigned getHashImpl() const;
1005 /// CompleteMatchMatcher - Complete a match by replacing the results of the
1006 /// pattern with the newly generated nodes. This also prints a comment
1007 /// indicating the source and dest patterns.
1008 class CompleteMatchMatcher : public Matcher {
1009 SmallVector<unsigned, 2> Results;
1010 const PatternToMatch &Pattern;
1012 CompleteMatchMatcher(const unsigned *results, unsigned numresults,
1013 const PatternToMatch &pattern)
1014 : Matcher(CompleteMatch), Results(results, results+numresults),
1017 unsigned getNumResults() const { return Results.size(); }
1018 unsigned getResult(unsigned R) const { return Results[R]; }
1019 const PatternToMatch &getPattern() const { return Pattern; }
1021 static inline bool classof(const Matcher *N) {
1022 return N->getKind() == CompleteMatch;
1026 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
1027 virtual bool isEqualImpl(const Matcher *M) const {
1028 return cast<CompleteMatchMatcher>(M)->Results == Results &&
1029 &cast<CompleteMatchMatcher>(M)->Pattern == &Pattern;
1031 virtual unsigned getHashImpl() const;
1034 } // end namespace llvm