1 //===- DAGISelMatcher.h - Representation of DAG pattern matcher -----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef TBLGEN_DAGISELMATCHER_H
11 #define TBLGEN_DAGISELMATCHER_H
13 #include "llvm/CodeGen/ValueTypes.h"
14 #include "llvm/ADT/OwningPtr.h"
15 #include "llvm/ADT/StringRef.h"
16 #include "llvm/ADT/SmallVector.h"
17 #include "llvm/Support/Casting.h"
20 class CodeGenDAGPatterns;
27 Matcher *ConvertPatternToMatcher(const PatternToMatch &Pattern,
28 const CodeGenDAGPatterns &CGP);
29 Matcher *OptimizeMatcher(Matcher *Matcher);
30 void EmitMatcherTable(const Matcher *Matcher, raw_ostream &OS);
33 /// Matcher - Base class for all the the DAG ISel Matcher representation
36 // The next matcher node that is executed after this one. Null if this is the
37 // last stage of a match.
38 OwningPtr<Matcher> Next;
41 // Matcher state manipulation.
42 Scope, // Push a checking scope.
43 RecordNode, // Record the current node.
44 RecordChild, // Record a child of the current node.
45 RecordMemRef, // Record the memref in the current node.
46 CaptureFlagInput, // If the current node has an input flag, save it.
47 MoveChild, // Move current node to specified child.
48 MoveParent, // Move current node to parent.
50 // Predicate checking.
51 CheckSame, // Fail if not same as prev match.
52 CheckPatternPredicate,
53 CheckPredicate, // Fail if node predicate fails.
54 CheckOpcode, // Fail if not opcode.
55 CheckMultiOpcode, // Fail if not in opcode list.
56 CheckType, // Fail if not correct type.
57 CheckChildType, // Fail if child has wrong type.
58 CheckInteger, // Fail if wrong val.
59 CheckCondCode, // Fail if not condcode.
64 CheckFoldableChainNode,
67 // Node creation/emisssion.
68 EmitInteger, // Create a TargetConstant
69 EmitStringInteger, // Create a TargetConstant from a string.
70 EmitRegister, // Create a register.
71 EmitConvertToTarget, // Convert a imm/fpimm to target imm/fpimm
72 EmitMergeInputChains, // Merge together a chains for an input.
73 EmitCopyToReg, // Emit a copytoreg into a physreg.
74 EmitNode, // Create a DAG node
75 EmitNodeXForm, // Run a SDNodeXForm
76 MarkFlagResults, // Indicate which interior nodes have flag results.
77 CompleteMatch // Finish a match and update the results.
82 Matcher(KindTy K) : Kind(K) {}
86 KindTy getKind() const { return Kind; }
88 Matcher *getNext() { return Next.get(); }
89 const Matcher *getNext() const { return Next.get(); }
90 void setNext(Matcher *C) { Next.reset(C); }
91 Matcher *takeNext() { return Next.take(); }
93 OwningPtr<Matcher> &getNextPtr() { return Next; }
95 static inline bool classof(const Matcher *) { return true; }
97 bool isEqual(const Matcher *M) const {
98 if (getKind() != M->getKind()) return false;
99 return isEqualImpl(M);
102 unsigned getHash() const {
103 return (getHashImpl() << 4) ^ getKind();
106 virtual void print(raw_ostream &OS, unsigned indent = 0) const = 0;
109 void printNext(raw_ostream &OS, unsigned indent) const;
110 virtual bool isEqualImpl(const Matcher *M) const = 0;
111 virtual unsigned getHashImpl() const = 0;
114 /// ScopeMatcher - This pushes a failure scope on the stack and evaluates
115 /// 'Check'. If 'Check' fails to match, it pops its scope and continues on to
117 class ScopeMatcher : public Matcher {
118 OwningPtr<Matcher> Check;
120 ScopeMatcher(Matcher *check = 0, Matcher *next = 0)
121 : Matcher(Scope), Check(check) {
125 Matcher *getCheck() { return Check.get(); }
126 const Matcher *getCheck() const { return Check.get(); }
127 void setCheck(Matcher *N) { Check.reset(N); }
128 OwningPtr<Matcher> &getCheckPtr() { return Check; }
130 static inline bool classof(const Matcher *N) {
131 return N->getKind() == Scope;
135 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
136 virtual bool isEqualImpl(const Matcher *M) const { return false; }
137 virtual unsigned getHashImpl() const { return 0; }
140 /// RecordMatcher - Save the current node in the operand list.
141 class RecordMatcher : public Matcher {
142 /// WhatFor - This is a string indicating why we're recording this. This
143 /// should only be used for comment generation not anything semantic.
146 RecordMatcher(const std::string &whatfor)
147 : Matcher(RecordNode), WhatFor(whatfor) {}
149 const std::string &getWhatFor() const { return WhatFor; }
151 static inline bool classof(const Matcher *N) {
152 return N->getKind() == RecordNode;
156 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
157 virtual bool isEqualImpl(const Matcher *M) const { return true; }
158 virtual unsigned getHashImpl() const { return 0; }
161 /// RecordChildMatcher - Save a numbered child of the current node, or fail
162 /// the match if it doesn't exist. This is logically equivalent to:
163 /// MoveChild N + RecordNode + MoveParent.
164 class RecordChildMatcher : public Matcher {
167 /// WhatFor - This is a string indicating why we're recording this. This
168 /// should only be used for comment generation not anything semantic.
171 RecordChildMatcher(unsigned childno, const std::string &whatfor)
172 : Matcher(RecordChild), ChildNo(childno), WhatFor(whatfor) {}
174 unsigned getChildNo() const { return ChildNo; }
175 const std::string &getWhatFor() const { return WhatFor; }
177 static inline bool classof(const Matcher *N) {
178 return N->getKind() == RecordChild;
182 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
183 virtual bool isEqualImpl(const Matcher *M) const {
184 return cast<RecordChildMatcher>(M)->getChildNo() == getChildNo();
186 virtual unsigned getHashImpl() const { return getChildNo(); }
189 /// RecordMemRefMatcher - Save the current node's memref.
190 class RecordMemRefMatcher : public Matcher {
192 RecordMemRefMatcher() : Matcher(RecordMemRef) {}
194 static inline bool classof(const Matcher *N) {
195 return N->getKind() == RecordMemRef;
199 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
200 virtual bool isEqualImpl(const Matcher *M) const { return true; }
201 virtual unsigned getHashImpl() const { return 0; }
205 /// CaptureFlagInputMatcher - If the current record has a flag input, record
206 /// it so that it is used as an input to the generated code.
207 class CaptureFlagInputMatcher : public Matcher {
209 CaptureFlagInputMatcher() : Matcher(CaptureFlagInput) {}
211 static inline bool classof(const Matcher *N) {
212 return N->getKind() == CaptureFlagInput;
216 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
217 virtual bool isEqualImpl(const Matcher *M) const { return true; }
218 virtual unsigned getHashImpl() const { return 0; }
221 /// MoveChildMatcher - This tells the interpreter to move into the
222 /// specified child node.
223 class MoveChildMatcher : public Matcher {
226 MoveChildMatcher(unsigned childNo) : Matcher(MoveChild), ChildNo(childNo) {}
228 unsigned getChildNo() const { return ChildNo; }
230 static inline bool classof(const Matcher *N) {
231 return N->getKind() == MoveChild;
235 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
236 virtual bool isEqualImpl(const Matcher *M) const {
237 return cast<MoveChildMatcher>(M)->getChildNo() == getChildNo();
239 virtual unsigned getHashImpl() const { return getChildNo(); }
242 /// MoveParentMatcher - This tells the interpreter to move to the parent
243 /// of the current node.
244 class MoveParentMatcher : public Matcher {
246 MoveParentMatcher() : Matcher(MoveParent) {}
248 static inline bool classof(const Matcher *N) {
249 return N->getKind() == MoveParent;
253 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
254 virtual bool isEqualImpl(const Matcher *M) const { return true; }
255 virtual unsigned getHashImpl() const { return 0; }
258 /// CheckSameMatcher - This checks to see if this node is exactly the same
259 /// node as the specified match that was recorded with 'Record'. This is used
260 /// when patterns have the same name in them, like '(mul GPR:$in, GPR:$in)'.
261 class CheckSameMatcher : public Matcher {
262 unsigned MatchNumber;
264 CheckSameMatcher(unsigned matchnumber)
265 : Matcher(CheckSame), MatchNumber(matchnumber) {}
267 unsigned getMatchNumber() const { return MatchNumber; }
269 static inline bool classof(const Matcher *N) {
270 return N->getKind() == CheckSame;
274 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
275 virtual bool isEqualImpl(const Matcher *M) const {
276 return cast<CheckSameMatcher>(M)->getMatchNumber() == getMatchNumber();
278 virtual unsigned getHashImpl() const { return getMatchNumber(); }
281 /// CheckPatternPredicateMatcher - This checks the target-specific predicate
282 /// to see if the entire pattern is capable of matching. This predicate does
283 /// not take a node as input. This is used for subtarget feature checks etc.
284 class CheckPatternPredicateMatcher : public Matcher {
285 std::string Predicate;
287 CheckPatternPredicateMatcher(StringRef predicate)
288 : Matcher(CheckPatternPredicate), Predicate(predicate) {}
290 StringRef getPredicate() const { return Predicate; }
292 static inline bool classof(const Matcher *N) {
293 return N->getKind() == CheckPatternPredicate;
297 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
298 virtual bool isEqualImpl(const Matcher *M) const {
299 return cast<CheckPatternPredicateMatcher>(M)->getPredicate() == Predicate;
301 virtual unsigned getHashImpl() const;
304 /// CheckPredicateMatcher - This checks the target-specific predicate to
305 /// see if the node is acceptable.
306 class CheckPredicateMatcher : public Matcher {
309 CheckPredicateMatcher(StringRef predname)
310 : Matcher(CheckPredicate), PredName(predname) {}
312 StringRef getPredicateName() const { return PredName; }
314 static inline bool classof(const Matcher *N) {
315 return N->getKind() == CheckPredicate;
319 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
320 virtual bool isEqualImpl(const Matcher *M) const {
321 return cast<CheckPredicateMatcher>(M)->PredName == PredName;
323 virtual unsigned getHashImpl() const;
327 /// CheckOpcodeMatcher - This checks to see if the current node has the
328 /// specified opcode, if not it fails to match.
329 class CheckOpcodeMatcher : public Matcher {
330 StringRef OpcodeName;
332 CheckOpcodeMatcher(StringRef opcodename)
333 : Matcher(CheckOpcode), OpcodeName(opcodename) {}
335 StringRef getOpcodeName() const { return OpcodeName; }
337 static inline bool classof(const Matcher *N) {
338 return N->getKind() == CheckOpcode;
342 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
343 virtual bool isEqualImpl(const Matcher *M) const {
344 return cast<CheckOpcodeMatcher>(M)->OpcodeName == OpcodeName;
346 virtual unsigned getHashImpl() const;
349 /// CheckMultiOpcodeMatcher - This checks to see if the current node has one
350 /// of the specified opcode, if not it fails to match.
351 class CheckMultiOpcodeMatcher : public Matcher {
352 SmallVector<StringRef, 4> OpcodeNames;
354 CheckMultiOpcodeMatcher(const StringRef *opcodes, unsigned numops)
355 : Matcher(CheckMultiOpcode), OpcodeNames(opcodes, opcodes+numops) {}
357 unsigned getNumOpcodeNames() const { return OpcodeNames.size(); }
358 StringRef getOpcodeName(unsigned i) const { return OpcodeNames[i]; }
360 static inline bool classof(const Matcher *N) {
361 return N->getKind() == CheckMultiOpcode;
365 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
366 virtual bool isEqualImpl(const Matcher *M) const {
367 return cast<CheckMultiOpcodeMatcher>(M)->OpcodeNames == OpcodeNames;
369 virtual unsigned getHashImpl() const;
374 /// CheckTypeMatcher - This checks to see if the current node has the
375 /// specified type, if not it fails to match.
376 class CheckTypeMatcher : public Matcher {
377 MVT::SimpleValueType Type;
379 CheckTypeMatcher(MVT::SimpleValueType type)
380 : Matcher(CheckType), Type(type) {}
382 MVT::SimpleValueType getType() const { return Type; }
384 static inline bool classof(const Matcher *N) {
385 return N->getKind() == CheckType;
389 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
390 virtual bool isEqualImpl(const Matcher *M) const {
391 return cast<CheckTypeMatcher>(this)->Type == Type;
393 virtual unsigned getHashImpl() const { return Type; }
396 /// CheckChildTypeMatcher - This checks to see if a child node has the
397 /// specified type, if not it fails to match.
398 class CheckChildTypeMatcher : public Matcher {
400 MVT::SimpleValueType Type;
402 CheckChildTypeMatcher(unsigned childno, MVT::SimpleValueType type)
403 : Matcher(CheckChildType), ChildNo(childno), Type(type) {}
405 unsigned getChildNo() const { return ChildNo; }
406 MVT::SimpleValueType getType() const { return Type; }
408 static inline bool classof(const Matcher *N) {
409 return N->getKind() == CheckChildType;
413 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
414 virtual bool isEqualImpl(const Matcher *M) const {
415 return cast<CheckChildTypeMatcher>(M)->ChildNo == ChildNo &&
416 cast<CheckChildTypeMatcher>(M)->Type == Type;
418 virtual unsigned getHashImpl() const { return (Type << 3) | ChildNo; }
422 /// CheckIntegerMatcher - This checks to see if the current node is a
423 /// ConstantSDNode with the specified integer value, if not it fails to match.
424 class CheckIntegerMatcher : public Matcher {
427 CheckIntegerMatcher(int64_t value)
428 : Matcher(CheckInteger), Value(value) {}
430 int64_t getValue() const { return Value; }
432 static inline bool classof(const Matcher *N) {
433 return N->getKind() == CheckInteger;
437 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
438 virtual bool isEqualImpl(const Matcher *M) const {
439 return cast<CheckIntegerMatcher>(M)->Value == Value;
441 virtual unsigned getHashImpl() const { return Value; }
444 /// CheckCondCodeMatcher - This checks to see if the current node is a
445 /// CondCodeSDNode with the specified condition, if not it fails to match.
446 class CheckCondCodeMatcher : public Matcher {
447 StringRef CondCodeName;
449 CheckCondCodeMatcher(StringRef condcodename)
450 : Matcher(CheckCondCode), CondCodeName(condcodename) {}
452 StringRef getCondCodeName() const { return CondCodeName; }
454 static inline bool classof(const Matcher *N) {
455 return N->getKind() == CheckCondCode;
459 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
460 virtual bool isEqualImpl(const Matcher *M) const {
461 return cast<CheckCondCodeMatcher>(M)->CondCodeName == CondCodeName;
463 virtual unsigned getHashImpl() const;
466 /// CheckValueTypeMatcher - This checks to see if the current node is a
467 /// VTSDNode with the specified type, if not it fails to match.
468 class CheckValueTypeMatcher : public Matcher {
471 CheckValueTypeMatcher(StringRef type_name)
472 : Matcher(CheckValueType), TypeName(type_name) {}
474 StringRef getTypeName() const { return TypeName; }
476 static inline bool classof(const Matcher *N) {
477 return N->getKind() == CheckValueType;
481 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
482 virtual bool isEqualImpl(const Matcher *M) const {
483 return cast<CheckValueTypeMatcher>(M)->TypeName == TypeName;
485 virtual unsigned getHashImpl() const;
490 /// CheckComplexPatMatcher - This node runs the specified ComplexPattern on
491 /// the current node.
492 class CheckComplexPatMatcher : public Matcher {
493 const ComplexPattern &Pattern;
495 CheckComplexPatMatcher(const ComplexPattern &pattern)
496 : Matcher(CheckComplexPat), Pattern(pattern) {}
498 const ComplexPattern &getPattern() const { return Pattern; }
500 static inline bool classof(const Matcher *N) {
501 return N->getKind() == CheckComplexPat;
505 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
506 virtual bool isEqualImpl(const Matcher *M) const {
507 return &cast<CheckComplexPatMatcher>(M)->Pattern == &Pattern;
509 virtual unsigned getHashImpl() const {
510 return (unsigned)(intptr_t)&Pattern;
514 /// CheckAndImmMatcher - This checks to see if the current node is an 'and'
515 /// with something equivalent to the specified immediate.
516 class CheckAndImmMatcher : public Matcher {
519 CheckAndImmMatcher(int64_t value)
520 : Matcher(CheckAndImm), Value(value) {}
522 int64_t getValue() const { return Value; }
524 static inline bool classof(const Matcher *N) {
525 return N->getKind() == CheckAndImm;
529 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
530 virtual bool isEqualImpl(const Matcher *M) const {
531 return cast<CheckAndImmMatcher>(M)->Value == Value;
533 virtual unsigned getHashImpl() const { return Value; }
536 /// CheckOrImmMatcher - This checks to see if the current node is an 'and'
537 /// with something equivalent to the specified immediate.
538 class CheckOrImmMatcher : public Matcher {
541 CheckOrImmMatcher(int64_t value)
542 : Matcher(CheckOrImm), Value(value) {}
544 int64_t getValue() const { return Value; }
546 static inline bool classof(const Matcher *N) {
547 return N->getKind() == CheckOrImm;
551 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
552 virtual bool isEqualImpl(const Matcher *M) const {
553 return cast<CheckOrImmMatcher>(M)->Value == Value;
555 virtual unsigned getHashImpl() const { return Value; }
558 /// CheckFoldableChainNodeMatcher - This checks to see if the current node
559 /// (which defines a chain operand) is safe to fold into a larger pattern.
560 class CheckFoldableChainNodeMatcher : public Matcher {
562 CheckFoldableChainNodeMatcher()
563 : Matcher(CheckFoldableChainNode) {}
565 static inline bool classof(const Matcher *N) {
566 return N->getKind() == CheckFoldableChainNode;
570 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
571 virtual bool isEqualImpl(const Matcher *M) const { return true; }
572 virtual unsigned getHashImpl() const { return 0; }
575 /// CheckChainCompatibleMatcher - Verify that the current node's chain
576 /// operand is 'compatible' with the specified recorded node's.
577 class CheckChainCompatibleMatcher : public Matcher {
580 CheckChainCompatibleMatcher(unsigned previousop)
581 : Matcher(CheckChainCompatible), PreviousOp(previousop) {}
583 unsigned getPreviousOp() const { return PreviousOp; }
585 static inline bool classof(const Matcher *N) {
586 return N->getKind() == CheckChainCompatible;
590 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
591 virtual bool isEqualImpl(const Matcher *M) const {
592 return cast<CheckChainCompatibleMatcher>(this)->PreviousOp == PreviousOp;
594 virtual unsigned getHashImpl() const { return PreviousOp; }
597 /// EmitIntegerMatcher - This creates a new TargetConstant.
598 class EmitIntegerMatcher : public Matcher {
600 MVT::SimpleValueType VT;
602 EmitIntegerMatcher(int64_t val, MVT::SimpleValueType vt)
603 : Matcher(EmitInteger), Val(val), VT(vt) {}
605 int64_t getValue() const { return Val; }
606 MVT::SimpleValueType getVT() const { return VT; }
608 static inline bool classof(const Matcher *N) {
609 return N->getKind() == EmitInteger;
613 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
614 virtual bool isEqualImpl(const Matcher *M) const {
615 return cast<EmitIntegerMatcher>(M)->Val == Val &&
616 cast<EmitIntegerMatcher>(M)->VT == VT;
618 virtual unsigned getHashImpl() const { return (Val << 4) | VT; }
621 /// EmitStringIntegerMatcher - A target constant whose value is represented
623 class EmitStringIntegerMatcher : public Matcher {
625 MVT::SimpleValueType VT;
627 EmitStringIntegerMatcher(const std::string &val, MVT::SimpleValueType vt)
628 : Matcher(EmitStringInteger), Val(val), VT(vt) {}
630 const std::string &getValue() const { return Val; }
631 MVT::SimpleValueType getVT() const { return VT; }
633 static inline bool classof(const Matcher *N) {
634 return N->getKind() == EmitStringInteger;
638 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
639 virtual bool isEqualImpl(const Matcher *M) const {
640 return cast<EmitStringIntegerMatcher>(M)->Val == Val &&
641 cast<EmitStringIntegerMatcher>(M)->VT == VT;
643 virtual unsigned getHashImpl() const;
646 /// EmitRegisterMatcher - This creates a new TargetConstant.
647 class EmitRegisterMatcher : public Matcher {
648 /// Reg - The def for the register that we're emitting. If this is null, then
649 /// this is a reference to zero_reg.
651 MVT::SimpleValueType VT;
653 EmitRegisterMatcher(Record *reg, MVT::SimpleValueType vt)
654 : Matcher(EmitRegister), Reg(reg), VT(vt) {}
656 Record *getReg() const { return Reg; }
657 MVT::SimpleValueType getVT() const { return VT; }
659 static inline bool classof(const Matcher *N) {
660 return N->getKind() == EmitRegister;
664 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
665 virtual bool isEqualImpl(const Matcher *M) const {
666 return cast<EmitRegisterMatcher>(M)->Reg == Reg &&
667 cast<EmitRegisterMatcher>(M)->VT == VT;
669 virtual unsigned getHashImpl() const {
670 return ((unsigned)(intptr_t)Reg) << 4 | VT;
674 /// EmitConvertToTargetMatcher - Emit an operation that reads a specified
675 /// recorded node and converts it from being a ISD::Constant to
676 /// ISD::TargetConstant, likewise for ConstantFP.
677 class EmitConvertToTargetMatcher : public Matcher {
680 EmitConvertToTargetMatcher(unsigned slot)
681 : Matcher(EmitConvertToTarget), Slot(slot) {}
683 unsigned getSlot() const { return Slot; }
685 static inline bool classof(const Matcher *N) {
686 return N->getKind() == EmitConvertToTarget;
690 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
691 virtual bool isEqualImpl(const Matcher *M) const {
692 return cast<EmitConvertToTargetMatcher>(M)->Slot == Slot;
694 virtual unsigned getHashImpl() const { return Slot; }
697 /// EmitMergeInputChainsMatcher - Emit a node that merges a list of input
698 /// chains together with a token factor. The list of nodes are the nodes in the
699 /// matched pattern that have chain input/outputs. This node adds all input
700 /// chains of these nodes if they are not themselves a node in the pattern.
701 class EmitMergeInputChainsMatcher : public Matcher {
702 SmallVector<unsigned, 3> ChainNodes;
704 EmitMergeInputChainsMatcher(const unsigned *nodes, unsigned NumNodes)
705 : Matcher(EmitMergeInputChains), ChainNodes(nodes, nodes+NumNodes) {}
707 unsigned getNumNodes() const { return ChainNodes.size(); }
709 unsigned getNode(unsigned i) const {
710 assert(i < ChainNodes.size());
711 return ChainNodes[i];
714 static inline bool classof(const Matcher *N) {
715 return N->getKind() == EmitMergeInputChains;
719 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
720 virtual bool isEqualImpl(const Matcher *M) const {
721 return cast<EmitMergeInputChainsMatcher>(M)->ChainNodes == ChainNodes;
723 virtual unsigned getHashImpl() const;
726 /// EmitCopyToRegMatcher - Emit a CopyToReg node from a value to a physreg,
727 /// pushing the chain and flag results.
729 class EmitCopyToRegMatcher : public Matcher {
730 unsigned SrcSlot; // Value to copy into the physreg.
733 EmitCopyToRegMatcher(unsigned srcSlot, Record *destPhysReg)
734 : Matcher(EmitCopyToReg), SrcSlot(srcSlot), DestPhysReg(destPhysReg) {}
736 unsigned getSrcSlot() const { return SrcSlot; }
737 Record *getDestPhysReg() const { return DestPhysReg; }
739 static inline bool classof(const Matcher *N) {
740 return N->getKind() == EmitCopyToReg;
744 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
745 virtual bool isEqualImpl(const Matcher *M) const {
746 return cast<EmitCopyToRegMatcher>(M)->SrcSlot == SrcSlot &&
747 cast<EmitCopyToRegMatcher>(M)->DestPhysReg == DestPhysReg;
749 virtual unsigned getHashImpl() const {
750 return SrcSlot ^ ((unsigned)(intptr_t)DestPhysReg << 4);
756 /// EmitNodeXFormMatcher - Emit an operation that runs an SDNodeXForm on a
757 /// recorded node and records the result.
758 class EmitNodeXFormMatcher : public Matcher {
762 EmitNodeXFormMatcher(unsigned slot, Record *nodeXForm)
763 : Matcher(EmitNodeXForm), Slot(slot), NodeXForm(nodeXForm) {}
765 unsigned getSlot() const { return Slot; }
766 Record *getNodeXForm() const { return NodeXForm; }
768 static inline bool classof(const Matcher *N) {
769 return N->getKind() == EmitNodeXForm;
773 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
774 virtual bool isEqualImpl(const Matcher *M) const {
775 return cast<EmitNodeXFormMatcher>(M)->Slot == Slot &&
776 cast<EmitNodeXFormMatcher>(M)->NodeXForm == NodeXForm;
778 virtual unsigned getHashImpl() const {
779 return Slot ^ ((unsigned)(intptr_t)NodeXForm << 4);
783 /// EmitNodeMatcher - This signals a successful match and generates a node.
784 class EmitNodeMatcher : public Matcher {
785 std::string OpcodeName;
786 const SmallVector<MVT::SimpleValueType, 3> VTs;
787 const SmallVector<unsigned, 6> Operands;
788 bool HasChain, HasFlag, HasMemRefs;
790 /// NumFixedArityOperands - If this is a fixed arity node, this is set to -1.
791 /// If this is a varidic node, this is set to the number of fixed arity
792 /// operands in the root of the pattern. The rest are appended to this node.
793 int NumFixedArityOperands;
795 EmitNodeMatcher(const std::string &opcodeName,
796 const MVT::SimpleValueType *vts, unsigned numvts,
797 const unsigned *operands, unsigned numops,
798 bool hasChain, bool hasFlag, bool hasmemrefs,
799 int numfixedarityoperands)
800 : Matcher(EmitNode), OpcodeName(opcodeName),
801 VTs(vts, vts+numvts), Operands(operands, operands+numops),
802 HasChain(hasChain), HasFlag(hasFlag), HasMemRefs(hasmemrefs),
803 NumFixedArityOperands(numfixedarityoperands) {}
805 const std::string &getOpcodeName() const { return OpcodeName; }
807 unsigned getNumVTs() const { return VTs.size(); }
808 MVT::SimpleValueType getVT(unsigned i) const {
809 assert(i < VTs.size());
813 unsigned getNumOperands() const { return Operands.size(); }
814 unsigned getOperand(unsigned i) const {
815 assert(i < Operands.size());
819 bool hasChain() const { return HasChain; }
820 bool hasFlag() const { return HasFlag; }
821 bool hasMemRefs() const { return HasMemRefs; }
822 int getNumFixedArityOperands() const { return NumFixedArityOperands; }
824 static inline bool classof(const Matcher *N) {
825 return N->getKind() == EmitNode;
829 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
830 virtual bool isEqualImpl(const Matcher *M) const;
831 virtual unsigned getHashImpl() const;
834 /// MarkFlagResultsMatcher - This node indicates which non-root nodes in the
835 /// pattern produce flags. This allows CompleteMatchMatcher to update them
836 /// with the output flag of the resultant code.
837 class MarkFlagResultsMatcher : public Matcher {
838 SmallVector<unsigned, 3> FlagResultNodes;
840 MarkFlagResultsMatcher(const unsigned *nodes, unsigned NumNodes)
841 : Matcher(MarkFlagResults), FlagResultNodes(nodes, nodes+NumNodes) {}
843 unsigned getNumNodes() const { return FlagResultNodes.size(); }
845 unsigned getNode(unsigned i) const {
846 assert(i < FlagResultNodes.size());
847 return FlagResultNodes[i];
850 static inline bool classof(const Matcher *N) {
851 return N->getKind() == MarkFlagResults;
855 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
856 virtual bool isEqualImpl(const Matcher *M) const {
857 return cast<MarkFlagResultsMatcher>(M)->FlagResultNodes == FlagResultNodes;
859 virtual unsigned getHashImpl() const;
862 /// CompleteMatchMatcher - Complete a match by replacing the results of the
863 /// pattern with the newly generated nodes. This also prints a comment
864 /// indicating the source and dest patterns.
865 class CompleteMatchMatcher : public Matcher {
866 SmallVector<unsigned, 2> Results;
867 const PatternToMatch &Pattern;
869 CompleteMatchMatcher(const unsigned *results, unsigned numresults,
870 const PatternToMatch &pattern)
871 : Matcher(CompleteMatch), Results(results, results+numresults),
874 unsigned getNumResults() const { return Results.size(); }
875 unsigned getResult(unsigned R) const { return Results[R]; }
876 const PatternToMatch &getPattern() const { return Pattern; }
878 static inline bool classof(const Matcher *N) {
879 return N->getKind() == CompleteMatch;
883 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
884 virtual bool isEqualImpl(const Matcher *M) const {
885 return cast<CompleteMatchMatcher>(M)->Results == Results &&
886 &cast<CompleteMatchMatcher>(M)->Pattern == &Pattern;
888 virtual unsigned getHashImpl() const;
891 } // end namespace llvm