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);
31 void EmitMatcherTable(const Matcher *Matcher, raw_ostream &OS);
34 /// Matcher - Base class for all the the DAG ISel Matcher representation
37 // The next matcher node that is executed after this one. Null if this is the
38 // last stage of a match.
39 OwningPtr<Matcher> Next;
42 // Matcher state manipulation.
43 Scope, // Push a checking scope.
44 RecordNode, // Record the current node.
45 RecordChild, // Record a child of the current node.
46 RecordMemRef, // Record the memref in the current node.
47 CaptureFlagInput, // If the current node has an input flag, save it.
48 MoveChild, // Move current node to specified child.
49 MoveParent, // Move current node to parent.
51 // Predicate checking.
52 CheckSame, // Fail if not same as prev match.
53 CheckPatternPredicate,
54 CheckPredicate, // Fail if node predicate fails.
55 CheckOpcode, // Fail if not opcode.
56 CheckMultiOpcode, // Fail if not in opcode list.
57 CheckType, // Fail if not correct type.
58 CheckChildType, // Fail if child has wrong type.
59 CheckInteger, // Fail if wrong val.
60 CheckCondCode, // Fail if not condcode.
65 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.
83 Matcher(KindTy K) : Kind(K) {}
87 KindTy getKind() const { return Kind; }
89 Matcher *getNext() { return Next.get(); }
90 const Matcher *getNext() const { return Next.get(); }
91 void setNext(Matcher *C) { Next.reset(C); }
92 Matcher *takeNext() { return Next.take(); }
94 OwningPtr<Matcher> &getNextPtr() { return Next; }
96 static inline bool classof(const Matcher *) { return true; }
98 bool isEqual(const Matcher *M) const {
99 if (getKind() != M->getKind()) return false;
100 return isEqualImpl(M);
103 unsigned getHash() const {
104 // Clear the high bit so we don't conflict with tombstones etc.
105 return ((getHashImpl() << 4) ^ getKind()) & (~0U>>1);
108 /// isSafeToReorderWithPatternPredicate - Return true if it is safe to sink a
109 /// PatternPredicate node past this one.
110 virtual bool isSafeToReorderWithPatternPredicate() const {
114 /// isContradictory - Return true of these two matchers could never match on
116 bool isContradictory(const Matcher *Other) const {
117 // Since this predicate is reflexive, we canonicalize the ordering so that
118 // we always match a node against nodes with kinds that are greater or equal
119 // to them. For example, we'll pass in a CheckType node as an argument to
120 // the CheckOpcode method, not the other way around.
121 if (getKind() < Other->getKind())
122 return isContradictoryImpl(Other);
123 return Other->isContradictoryImpl(this);
126 void print(raw_ostream &OS, unsigned indent = 0) const;
127 void printOne(raw_ostream &OS) const;
130 virtual void printImpl(raw_ostream &OS, unsigned indent) const = 0;
131 virtual bool isEqualImpl(const Matcher *M) const = 0;
132 virtual unsigned getHashImpl() const = 0;
133 virtual bool isContradictoryImpl(const Matcher *M) const { return false; }
136 /// ScopeMatcher - This attempts to match each of its children to find the first
137 /// one that successfully matches. If one child fails, it tries the next child.
138 /// If none of the children match then this check fails. It never has a 'next'.
139 class ScopeMatcher : public Matcher {
140 SmallVector<Matcher*, 4> Children;
142 ScopeMatcher(Matcher *const *children, unsigned numchildren)
143 : Matcher(Scope), Children(children, children+numchildren) {
145 virtual ~ScopeMatcher();
147 unsigned getNumChildren() const { return Children.size(); }
149 Matcher *getChild(unsigned i) { return Children[i]; }
150 const Matcher *getChild(unsigned i) const { return Children[i]; }
152 void resetChild(unsigned i, Matcher *N) {
157 Matcher *takeChild(unsigned i) {
158 Matcher *Res = Children[i];
163 void setNumChildren(unsigned NC) {
164 if (NC < Children.size()) {
165 // delete any children we're about to lose pointers to.
166 for (unsigned i = NC, e = Children.size(); i != e; ++i)
172 static inline bool classof(const Matcher *N) {
173 return N->getKind() == Scope;
177 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
178 virtual bool isEqualImpl(const Matcher *M) const { return false; }
179 virtual unsigned getHashImpl() const { return 12312; }
182 /// RecordMatcher - Save the current node in the operand list.
183 class RecordMatcher : public Matcher {
184 /// WhatFor - This is a string indicating why we're recording this. This
185 /// should only be used for comment generation not anything semantic.
188 RecordMatcher(const std::string &whatfor)
189 : Matcher(RecordNode), WhatFor(whatfor) {}
191 const std::string &getWhatFor() const { return WhatFor; }
193 static inline bool classof(const Matcher *N) {
194 return N->getKind() == RecordNode;
197 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
199 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
200 virtual bool isEqualImpl(const Matcher *M) const { return true; }
201 virtual unsigned getHashImpl() const { return 0; }
204 /// RecordChildMatcher - Save a numbered child of the current node, or fail
205 /// the match if it doesn't exist. This is logically equivalent to:
206 /// MoveChild N + RecordNode + MoveParent.
207 class RecordChildMatcher : public Matcher {
210 /// WhatFor - This is a string indicating why we're recording this. This
211 /// should only be used for comment generation not anything semantic.
214 RecordChildMatcher(unsigned childno, const std::string &whatfor)
215 : Matcher(RecordChild), ChildNo(childno), WhatFor(whatfor) {}
217 unsigned getChildNo() const { return ChildNo; }
218 const std::string &getWhatFor() const { return WhatFor; }
220 static inline bool classof(const Matcher *N) {
221 return N->getKind() == RecordChild;
224 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
227 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
228 virtual bool isEqualImpl(const Matcher *M) const {
229 return cast<RecordChildMatcher>(M)->getChildNo() == getChildNo();
231 virtual unsigned getHashImpl() const { return getChildNo(); }
234 /// RecordMemRefMatcher - Save the current node's memref.
235 class RecordMemRefMatcher : public Matcher {
237 RecordMemRefMatcher() : Matcher(RecordMemRef) {}
239 static inline bool classof(const Matcher *N) {
240 return N->getKind() == RecordMemRef;
243 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
246 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
247 virtual bool isEqualImpl(const Matcher *M) const { return true; }
248 virtual unsigned getHashImpl() const { return 0; }
252 /// CaptureFlagInputMatcher - If the current record has a flag input, record
253 /// it so that it is used as an input to the generated code.
254 class CaptureFlagInputMatcher : public Matcher {
256 CaptureFlagInputMatcher() : Matcher(CaptureFlagInput) {}
258 static inline bool classof(const Matcher *N) {
259 return N->getKind() == CaptureFlagInput;
262 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
265 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
266 virtual bool isEqualImpl(const Matcher *M) const { return true; }
267 virtual unsigned getHashImpl() const { return 0; }
270 /// MoveChildMatcher - This tells the interpreter to move into the
271 /// specified child node.
272 class MoveChildMatcher : public Matcher {
275 MoveChildMatcher(unsigned childNo) : Matcher(MoveChild), ChildNo(childNo) {}
277 unsigned getChildNo() const { return ChildNo; }
279 static inline bool classof(const Matcher *N) {
280 return N->getKind() == MoveChild;
283 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
286 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
287 virtual bool isEqualImpl(const Matcher *M) const {
288 return cast<MoveChildMatcher>(M)->getChildNo() == getChildNo();
290 virtual unsigned getHashImpl() const { return getChildNo(); }
293 /// MoveParentMatcher - This tells the interpreter to move to the parent
294 /// of the current node.
295 class MoveParentMatcher : public Matcher {
297 MoveParentMatcher() : Matcher(MoveParent) {}
299 static inline bool classof(const Matcher *N) {
300 return N->getKind() == MoveParent;
303 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
306 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
307 virtual bool isEqualImpl(const Matcher *M) const { return true; }
308 virtual unsigned getHashImpl() const { return 0; }
311 /// CheckSameMatcher - This checks to see if this node is exactly the same
312 /// node as the specified match that was recorded with 'Record'. This is used
313 /// when patterns have the same name in them, like '(mul GPR:$in, GPR:$in)'.
314 class CheckSameMatcher : public Matcher {
315 unsigned MatchNumber;
317 CheckSameMatcher(unsigned matchnumber)
318 : Matcher(CheckSame), MatchNumber(matchnumber) {}
320 unsigned getMatchNumber() const { return MatchNumber; }
322 static inline bool classof(const Matcher *N) {
323 return N->getKind() == CheckSame;
326 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
329 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
330 virtual bool isEqualImpl(const Matcher *M) const {
331 return cast<CheckSameMatcher>(M)->getMatchNumber() == getMatchNumber();
333 virtual unsigned getHashImpl() const { return getMatchNumber(); }
336 /// CheckPatternPredicateMatcher - This checks the target-specific predicate
337 /// to see if the entire pattern is capable of matching. This predicate does
338 /// not take a node as input. This is used for subtarget feature checks etc.
339 class CheckPatternPredicateMatcher : public Matcher {
340 std::string Predicate;
342 CheckPatternPredicateMatcher(StringRef predicate)
343 : Matcher(CheckPatternPredicate), Predicate(predicate) {}
345 StringRef getPredicate() const { return Predicate; }
347 static inline bool classof(const Matcher *N) {
348 return N->getKind() == CheckPatternPredicate;
351 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
354 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
355 virtual bool isEqualImpl(const Matcher *M) const {
356 return cast<CheckPatternPredicateMatcher>(M)->getPredicate() == Predicate;
358 virtual unsigned getHashImpl() const;
361 /// CheckPredicateMatcher - This checks the target-specific predicate to
362 /// see if the node is acceptable.
363 class CheckPredicateMatcher : public Matcher {
366 CheckPredicateMatcher(StringRef predname)
367 : Matcher(CheckPredicate), PredName(predname) {}
369 StringRef getPredicateName() const { return PredName; }
371 static inline bool classof(const Matcher *N) {
372 return N->getKind() == CheckPredicate;
376 //virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
379 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
380 virtual bool isEqualImpl(const Matcher *M) const {
381 return cast<CheckPredicateMatcher>(M)->PredName == PredName;
383 virtual unsigned getHashImpl() const;
387 /// CheckOpcodeMatcher - This checks to see if the current node has the
388 /// specified opcode, if not it fails to match.
389 class CheckOpcodeMatcher : public Matcher {
390 const SDNodeInfo &Opcode;
392 CheckOpcodeMatcher(const SDNodeInfo &opcode)
393 : Matcher(CheckOpcode), Opcode(opcode) {}
395 const SDNodeInfo &getOpcode() const { return Opcode; }
397 static inline bool classof(const Matcher *N) {
398 return N->getKind() == CheckOpcode;
401 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
404 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
405 virtual bool isEqualImpl(const Matcher *M) const {
406 return &cast<CheckOpcodeMatcher>(M)->Opcode == &Opcode;
408 virtual unsigned getHashImpl() const;
409 virtual bool isContradictoryImpl(const Matcher *M) const;
412 /// CheckMultiOpcodeMatcher - This checks to see if the current node has one
413 /// of the specified opcode, if not it fails to match.
414 class CheckMultiOpcodeMatcher : public Matcher {
415 SmallVector<const SDNodeInfo*, 4> Opcodes;
417 CheckMultiOpcodeMatcher(const SDNodeInfo * const *opcodes, unsigned numops)
418 : Matcher(CheckMultiOpcode), Opcodes(opcodes, opcodes+numops) {}
420 unsigned getNumOpcodes() const { return Opcodes.size(); }
421 const SDNodeInfo &getOpcode(unsigned i) const { return *Opcodes[i]; }
423 static inline bool classof(const Matcher *N) {
424 return N->getKind() == CheckMultiOpcode;
427 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
430 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
431 virtual bool isEqualImpl(const Matcher *M) const {
432 return cast<CheckMultiOpcodeMatcher>(M)->Opcodes == Opcodes;
434 virtual unsigned getHashImpl() const;
439 /// CheckTypeMatcher - This checks to see if the current node has the
440 /// specified type, if not it fails to match.
441 class CheckTypeMatcher : public Matcher {
442 MVT::SimpleValueType Type;
444 CheckTypeMatcher(MVT::SimpleValueType type)
445 : Matcher(CheckType), Type(type) {}
447 MVT::SimpleValueType getType() const { return Type; }
449 static inline bool classof(const Matcher *N) {
450 return N->getKind() == CheckType;
453 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
456 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
457 virtual bool isEqualImpl(const Matcher *M) const {
458 return cast<CheckTypeMatcher>(M)->Type == Type;
460 virtual unsigned getHashImpl() const { return Type; }
461 virtual bool isContradictoryImpl(const Matcher *M) const;
464 /// CheckChildTypeMatcher - This checks to see if a child node has the
465 /// specified type, if not it fails to match.
466 class CheckChildTypeMatcher : public Matcher {
468 MVT::SimpleValueType Type;
470 CheckChildTypeMatcher(unsigned childno, MVT::SimpleValueType type)
471 : Matcher(CheckChildType), ChildNo(childno), Type(type) {}
473 unsigned getChildNo() const { return ChildNo; }
474 MVT::SimpleValueType getType() const { return Type; }
476 static inline bool classof(const Matcher *N) {
477 return N->getKind() == CheckChildType;
480 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
483 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
484 virtual bool isEqualImpl(const Matcher *M) const {
485 return cast<CheckChildTypeMatcher>(M)->ChildNo == ChildNo &&
486 cast<CheckChildTypeMatcher>(M)->Type == Type;
488 virtual unsigned getHashImpl() const { return (Type << 3) | ChildNo; }
489 virtual bool isContradictoryImpl(const Matcher *M) const;
493 /// CheckIntegerMatcher - This checks to see if the current node is a
494 /// ConstantSDNode with the specified integer value, if not it fails to match.
495 class CheckIntegerMatcher : public Matcher {
498 CheckIntegerMatcher(int64_t value)
499 : Matcher(CheckInteger), Value(value) {}
501 int64_t getValue() const { return Value; }
503 static inline bool classof(const Matcher *N) {
504 return N->getKind() == CheckInteger;
507 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
510 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
511 virtual bool isEqualImpl(const Matcher *M) const {
512 return cast<CheckIntegerMatcher>(M)->Value == Value;
514 virtual unsigned getHashImpl() const { return Value; }
515 virtual bool isContradictoryImpl(const Matcher *M) const;
518 /// CheckCondCodeMatcher - This checks to see if the current node is a
519 /// CondCodeSDNode with the specified condition, if not it fails to match.
520 class CheckCondCodeMatcher : public Matcher {
521 StringRef CondCodeName;
523 CheckCondCodeMatcher(StringRef condcodename)
524 : Matcher(CheckCondCode), CondCodeName(condcodename) {}
526 StringRef getCondCodeName() const { return CondCodeName; }
528 static inline bool classof(const Matcher *N) {
529 return N->getKind() == CheckCondCode;
532 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
535 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
536 virtual bool isEqualImpl(const Matcher *M) const {
537 return cast<CheckCondCodeMatcher>(M)->CondCodeName == CondCodeName;
539 virtual unsigned getHashImpl() const;
542 /// CheckValueTypeMatcher - This checks to see if the current node is a
543 /// VTSDNode with the specified type, if not it fails to match.
544 class CheckValueTypeMatcher : public Matcher {
547 CheckValueTypeMatcher(StringRef type_name)
548 : Matcher(CheckValueType), TypeName(type_name) {}
550 StringRef getTypeName() const { return TypeName; }
552 static inline bool classof(const Matcher *N) {
553 return N->getKind() == CheckValueType;
556 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
559 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
560 virtual bool isEqualImpl(const Matcher *M) const {
561 return cast<CheckValueTypeMatcher>(M)->TypeName == TypeName;
563 virtual unsigned getHashImpl() const;
568 /// CheckComplexPatMatcher - This node runs the specified ComplexPattern on
569 /// the current node.
570 class CheckComplexPatMatcher : public Matcher {
571 const ComplexPattern &Pattern;
573 CheckComplexPatMatcher(const ComplexPattern &pattern)
574 : Matcher(CheckComplexPat), Pattern(pattern) {}
576 const ComplexPattern &getPattern() const { return Pattern; }
578 static inline bool classof(const Matcher *N) {
579 return N->getKind() == CheckComplexPat;
582 // Not safe to move a pattern predicate past a complex pattern.
583 virtual bool isSafeToReorderWithPatternPredicate() const { return false; }
586 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
587 virtual bool isEqualImpl(const Matcher *M) const {
588 return &cast<CheckComplexPatMatcher>(M)->Pattern == &Pattern;
590 virtual unsigned getHashImpl() const {
591 return (unsigned)(intptr_t)&Pattern;
595 /// CheckAndImmMatcher - This checks to see if the current node is an 'and'
596 /// with something equivalent to the specified immediate.
597 class CheckAndImmMatcher : public Matcher {
600 CheckAndImmMatcher(int64_t value)
601 : Matcher(CheckAndImm), Value(value) {}
603 int64_t getValue() const { return Value; }
605 static inline bool classof(const Matcher *N) {
606 return N->getKind() == CheckAndImm;
609 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
612 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
613 virtual bool isEqualImpl(const Matcher *M) const {
614 return cast<CheckAndImmMatcher>(M)->Value == Value;
616 virtual unsigned getHashImpl() const { return Value; }
619 /// CheckOrImmMatcher - This checks to see if the current node is an 'and'
620 /// with something equivalent to the specified immediate.
621 class CheckOrImmMatcher : public Matcher {
624 CheckOrImmMatcher(int64_t value)
625 : Matcher(CheckOrImm), Value(value) {}
627 int64_t getValue() const { return Value; }
629 static inline bool classof(const Matcher *N) {
630 return N->getKind() == CheckOrImm;
633 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
636 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
637 virtual bool isEqualImpl(const Matcher *M) const {
638 return cast<CheckOrImmMatcher>(M)->Value == Value;
640 virtual unsigned getHashImpl() const { return Value; }
643 /// CheckFoldableChainNodeMatcher - This checks to see if the current node
644 /// (which defines a chain operand) is safe to fold into a larger pattern.
645 class CheckFoldableChainNodeMatcher : public Matcher {
647 CheckFoldableChainNodeMatcher()
648 : Matcher(CheckFoldableChainNode) {}
650 static inline bool classof(const Matcher *N) {
651 return N->getKind() == CheckFoldableChainNode;
654 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
657 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
658 virtual bool isEqualImpl(const Matcher *M) const { return true; }
659 virtual unsigned getHashImpl() const { return 0; }
662 /// CheckChainCompatibleMatcher - Verify that the current node's chain
663 /// operand is 'compatible' with the specified recorded node's.
664 class CheckChainCompatibleMatcher : public Matcher {
667 CheckChainCompatibleMatcher(unsigned previousop)
668 : Matcher(CheckChainCompatible), PreviousOp(previousop) {}
670 unsigned getPreviousOp() const { return PreviousOp; }
672 static inline bool classof(const Matcher *N) {
673 return N->getKind() == CheckChainCompatible;
676 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
679 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
680 virtual bool isEqualImpl(const Matcher *M) const {
681 return cast<CheckChainCompatibleMatcher>(M)->PreviousOp == PreviousOp;
683 virtual unsigned getHashImpl() const { return PreviousOp; }
686 /// EmitIntegerMatcher - This creates a new TargetConstant.
687 class EmitIntegerMatcher : public Matcher {
689 MVT::SimpleValueType VT;
691 EmitIntegerMatcher(int64_t val, MVT::SimpleValueType vt)
692 : Matcher(EmitInteger), Val(val), VT(vt) {}
694 int64_t getValue() const { return Val; }
695 MVT::SimpleValueType getVT() const { return VT; }
697 static inline bool classof(const Matcher *N) {
698 return N->getKind() == EmitInteger;
702 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
703 virtual bool isEqualImpl(const Matcher *M) const {
704 return cast<EmitIntegerMatcher>(M)->Val == Val &&
705 cast<EmitIntegerMatcher>(M)->VT == VT;
707 virtual unsigned getHashImpl() const { return (Val << 4) | VT; }
710 /// EmitStringIntegerMatcher - A target constant whose value is represented
712 class EmitStringIntegerMatcher : public Matcher {
714 MVT::SimpleValueType VT;
716 EmitStringIntegerMatcher(const std::string &val, MVT::SimpleValueType vt)
717 : Matcher(EmitStringInteger), Val(val), VT(vt) {}
719 const std::string &getValue() const { return Val; }
720 MVT::SimpleValueType getVT() const { return VT; }
722 static inline bool classof(const Matcher *N) {
723 return N->getKind() == EmitStringInteger;
727 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
728 virtual bool isEqualImpl(const Matcher *M) const {
729 return cast<EmitStringIntegerMatcher>(M)->Val == Val &&
730 cast<EmitStringIntegerMatcher>(M)->VT == VT;
732 virtual unsigned getHashImpl() const;
735 /// EmitRegisterMatcher - This creates a new TargetConstant.
736 class EmitRegisterMatcher : public Matcher {
737 /// Reg - The def for the register that we're emitting. If this is null, then
738 /// this is a reference to zero_reg.
740 MVT::SimpleValueType VT;
742 EmitRegisterMatcher(Record *reg, MVT::SimpleValueType vt)
743 : Matcher(EmitRegister), Reg(reg), VT(vt) {}
745 Record *getReg() const { return Reg; }
746 MVT::SimpleValueType getVT() const { return VT; }
748 static inline bool classof(const Matcher *N) {
749 return N->getKind() == EmitRegister;
753 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
754 virtual bool isEqualImpl(const Matcher *M) const {
755 return cast<EmitRegisterMatcher>(M)->Reg == Reg &&
756 cast<EmitRegisterMatcher>(M)->VT == VT;
758 virtual unsigned getHashImpl() const {
759 return ((unsigned)(intptr_t)Reg) << 4 | VT;
763 /// EmitConvertToTargetMatcher - Emit an operation that reads a specified
764 /// recorded node and converts it from being a ISD::Constant to
765 /// ISD::TargetConstant, likewise for ConstantFP.
766 class EmitConvertToTargetMatcher : public Matcher {
769 EmitConvertToTargetMatcher(unsigned slot)
770 : Matcher(EmitConvertToTarget), Slot(slot) {}
772 unsigned getSlot() const { return Slot; }
774 static inline bool classof(const Matcher *N) {
775 return N->getKind() == EmitConvertToTarget;
779 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
780 virtual bool isEqualImpl(const Matcher *M) const {
781 return cast<EmitConvertToTargetMatcher>(M)->Slot == Slot;
783 virtual unsigned getHashImpl() const { return Slot; }
786 /// EmitMergeInputChainsMatcher - Emit a node that merges a list of input
787 /// chains together with a token factor. The list of nodes are the nodes in the
788 /// matched pattern that have chain input/outputs. This node adds all input
789 /// chains of these nodes if they are not themselves a node in the pattern.
790 class EmitMergeInputChainsMatcher : public Matcher {
791 SmallVector<unsigned, 3> ChainNodes;
793 EmitMergeInputChainsMatcher(const unsigned *nodes, unsigned NumNodes)
794 : Matcher(EmitMergeInputChains), ChainNodes(nodes, nodes+NumNodes) {}
796 unsigned getNumNodes() const { return ChainNodes.size(); }
798 unsigned getNode(unsigned i) const {
799 assert(i < ChainNodes.size());
800 return ChainNodes[i];
803 static inline bool classof(const Matcher *N) {
804 return N->getKind() == EmitMergeInputChains;
808 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
809 virtual bool isEqualImpl(const Matcher *M) const {
810 return cast<EmitMergeInputChainsMatcher>(M)->ChainNodes == ChainNodes;
812 virtual unsigned getHashImpl() const;
815 /// EmitCopyToRegMatcher - Emit a CopyToReg node from a value to a physreg,
816 /// pushing the chain and flag results.
818 class EmitCopyToRegMatcher : public Matcher {
819 unsigned SrcSlot; // Value to copy into the physreg.
822 EmitCopyToRegMatcher(unsigned srcSlot, Record *destPhysReg)
823 : Matcher(EmitCopyToReg), SrcSlot(srcSlot), DestPhysReg(destPhysReg) {}
825 unsigned getSrcSlot() const { return SrcSlot; }
826 Record *getDestPhysReg() const { return DestPhysReg; }
828 static inline bool classof(const Matcher *N) {
829 return N->getKind() == EmitCopyToReg;
833 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
834 virtual bool isEqualImpl(const Matcher *M) const {
835 return cast<EmitCopyToRegMatcher>(M)->SrcSlot == SrcSlot &&
836 cast<EmitCopyToRegMatcher>(M)->DestPhysReg == DestPhysReg;
838 virtual unsigned getHashImpl() const {
839 return SrcSlot ^ ((unsigned)(intptr_t)DestPhysReg << 4);
845 /// EmitNodeXFormMatcher - Emit an operation that runs an SDNodeXForm on a
846 /// recorded node and records the result.
847 class EmitNodeXFormMatcher : public Matcher {
851 EmitNodeXFormMatcher(unsigned slot, Record *nodeXForm)
852 : Matcher(EmitNodeXForm), Slot(slot), NodeXForm(nodeXForm) {}
854 unsigned getSlot() const { return Slot; }
855 Record *getNodeXForm() const { return NodeXForm; }
857 static inline bool classof(const Matcher *N) {
858 return N->getKind() == EmitNodeXForm;
862 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
863 virtual bool isEqualImpl(const Matcher *M) const {
864 return cast<EmitNodeXFormMatcher>(M)->Slot == Slot &&
865 cast<EmitNodeXFormMatcher>(M)->NodeXForm == NodeXForm;
867 virtual unsigned getHashImpl() const {
868 return Slot ^ ((unsigned)(intptr_t)NodeXForm << 4);
872 /// EmitNodeMatcher - This signals a successful match and generates a node.
873 class EmitNodeMatcher : public Matcher {
874 std::string OpcodeName;
875 const SmallVector<MVT::SimpleValueType, 3> VTs;
876 const SmallVector<unsigned, 6> Operands;
877 bool HasChain, HasFlag, HasMemRefs;
879 /// NumFixedArityOperands - If this is a fixed arity node, this is set to -1.
880 /// If this is a varidic node, this is set to the number of fixed arity
881 /// operands in the root of the pattern. The rest are appended to this node.
882 int NumFixedArityOperands;
884 EmitNodeMatcher(const std::string &opcodeName,
885 const MVT::SimpleValueType *vts, unsigned numvts,
886 const unsigned *operands, unsigned numops,
887 bool hasChain, bool hasFlag, bool hasmemrefs,
888 int numfixedarityoperands)
889 : Matcher(EmitNode), OpcodeName(opcodeName),
890 VTs(vts, vts+numvts), Operands(operands, operands+numops),
891 HasChain(hasChain), HasFlag(hasFlag), HasMemRefs(hasmemrefs),
892 NumFixedArityOperands(numfixedarityoperands) {}
894 const std::string &getOpcodeName() const { return OpcodeName; }
896 unsigned getNumVTs() const { return VTs.size(); }
897 MVT::SimpleValueType getVT(unsigned i) const {
898 assert(i < VTs.size());
902 unsigned getNumOperands() const { return Operands.size(); }
903 unsigned getOperand(unsigned i) const {
904 assert(i < Operands.size());
908 bool hasChain() const { return HasChain; }
909 bool hasFlag() const { return HasFlag; }
910 bool hasMemRefs() const { return HasMemRefs; }
911 int getNumFixedArityOperands() const { return NumFixedArityOperands; }
913 static inline bool classof(const Matcher *N) {
914 return N->getKind() == EmitNode;
918 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
919 virtual bool isEqualImpl(const Matcher *M) const;
920 virtual unsigned getHashImpl() const;
923 /// MarkFlagResultsMatcher - This node indicates which non-root nodes in the
924 /// pattern produce flags. This allows CompleteMatchMatcher to update them
925 /// with the output flag of the resultant code.
926 class MarkFlagResultsMatcher : public Matcher {
927 SmallVector<unsigned, 3> FlagResultNodes;
929 MarkFlagResultsMatcher(const unsigned *nodes, unsigned NumNodes)
930 : Matcher(MarkFlagResults), FlagResultNodes(nodes, nodes+NumNodes) {}
932 unsigned getNumNodes() const { return FlagResultNodes.size(); }
934 unsigned getNode(unsigned i) const {
935 assert(i < FlagResultNodes.size());
936 return FlagResultNodes[i];
939 static inline bool classof(const Matcher *N) {
940 return N->getKind() == MarkFlagResults;
944 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
945 virtual bool isEqualImpl(const Matcher *M) const {
946 return cast<MarkFlagResultsMatcher>(M)->FlagResultNodes == FlagResultNodes;
948 virtual unsigned getHashImpl() const;
951 /// CompleteMatchMatcher - Complete a match by replacing the results of the
952 /// pattern with the newly generated nodes. This also prints a comment
953 /// indicating the source and dest patterns.
954 class CompleteMatchMatcher : public Matcher {
955 SmallVector<unsigned, 2> Results;
956 const PatternToMatch &Pattern;
958 CompleteMatchMatcher(const unsigned *results, unsigned numresults,
959 const PatternToMatch &pattern)
960 : Matcher(CompleteMatch), Results(results, results+numresults),
963 unsigned getNumResults() const { return Results.size(); }
964 unsigned getResult(unsigned R) const { return Results[R]; }
965 const PatternToMatch &getPattern() const { return Pattern; }
967 static inline bool classof(const Matcher *N) {
968 return N->getKind() == CompleteMatch;
972 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
973 virtual bool isEqualImpl(const Matcher *M) const {
974 return cast<CompleteMatchMatcher>(M)->Results == Results &&
975 &cast<CompleteMatchMatcher>(M)->Pattern == &Pattern;
977 virtual unsigned getHashImpl() const;
980 } // end namespace llvm