1 //===- DAGISelMatcher.h - Representation of DAG pattern matcher -----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef TBLGEN_DAGISELMATCHER_H
11 #define TBLGEN_DAGISELMATCHER_H
13 #include "llvm/CodeGen/ValueTypes.h"
14 #include "llvm/ADT/OwningPtr.h"
15 #include "llvm/ADT/StringRef.h"
16 #include "llvm/ADT/SmallVector.h"
17 #include "llvm/Support/Casting.h"
20 class CodeGenDAGPatterns;
28 Matcher *ConvertPatternToMatcher(const PatternToMatch &Pattern,unsigned Variant,
29 const CodeGenDAGPatterns &CGP);
30 Matcher *OptimizeMatcher(Matcher *Matcher, const CodeGenDAGPatterns &CGP);
31 void EmitMatcherTable(const Matcher *Matcher, const CodeGenDAGPatterns &CGP,
35 /// Matcher - Base class for all the the DAG ISel Matcher representation
38 // The next matcher node that is executed after this one. Null if this is the
39 // last stage of a match.
40 OwningPtr<Matcher> Next;
43 // Matcher state manipulation.
44 Scope, // Push a checking scope.
45 RecordNode, // Record the current node.
46 RecordChild, // Record a child of the current node.
47 RecordMemRef, // Record the memref in the current node.
48 CaptureFlagInput, // If the current node has an input flag, save it.
49 MoveChild, // Move current node to specified child.
50 MoveParent, // Move current node to parent.
52 // Predicate checking.
53 CheckSame, // Fail if not same as prev match.
54 CheckPatternPredicate,
55 CheckPredicate, // Fail if node predicate fails.
56 CheckOpcode, // Fail if not opcode.
57 SwitchOpcode, // Dispatch based on opcode.
58 CheckMultiOpcode, // Fail if not in opcode list.
59 CheckType, // Fail if not correct type.
60 CheckChildType, // Fail if child has wrong type.
61 CheckInteger, // Fail if wrong val.
62 CheckCondCode, // Fail if not condcode.
67 CheckFoldableChainNode,
70 // Node creation/emisssion.
71 EmitInteger, // Create a TargetConstant
72 EmitStringInteger, // Create a TargetConstant from a string.
73 EmitRegister, // Create a register.
74 EmitConvertToTarget, // Convert a imm/fpimm to target imm/fpimm
75 EmitMergeInputChains, // Merge together a chains for an input.
76 EmitCopyToReg, // Emit a copytoreg into a physreg.
77 EmitNode, // Create a DAG node
78 EmitNodeXForm, // Run a SDNodeXForm
79 MarkFlagResults, // Indicate which interior nodes have flag results.
80 CompleteMatch, // Finish a match and update the results.
81 MorphNodeTo // Build a node, finish a match and update results.
86 Matcher(KindTy K) : Kind(K) {}
90 KindTy getKind() const { return Kind; }
92 Matcher *getNext() { return Next.get(); }
93 const Matcher *getNext() const { return Next.get(); }
94 void setNext(Matcher *C) { Next.reset(C); }
95 Matcher *takeNext() { return Next.take(); }
97 OwningPtr<Matcher> &getNextPtr() { return Next; }
99 static inline bool classof(const Matcher *) { return true; }
101 bool isEqual(const Matcher *M) const {
102 if (getKind() != M->getKind()) return false;
103 return isEqualImpl(M);
106 unsigned getHash() const {
107 // Clear the high bit so we don't conflict with tombstones etc.
108 return ((getHashImpl() << 4) ^ getKind()) & (~0U>>1);
111 /// isSafeToReorderWithPatternPredicate - Return true if it is safe to sink a
112 /// PatternPredicate node past this one.
113 virtual bool isSafeToReorderWithPatternPredicate() const {
117 /// isContradictory - Return true of these two matchers could never match on
119 bool isContradictory(const Matcher *Other) const {
120 // Since this predicate is reflexive, we canonicalize the ordering so that
121 // we always match a node against nodes with kinds that are greater or equal
122 // to them. For example, we'll pass in a CheckType node as an argument to
123 // the CheckOpcode method, not the other way around.
124 if (getKind() < Other->getKind())
125 return isContradictoryImpl(Other);
126 return Other->isContradictoryImpl(this);
129 void print(raw_ostream &OS, unsigned indent = 0) const;
130 void printOne(raw_ostream &OS) const;
133 virtual void printImpl(raw_ostream &OS, unsigned indent) const = 0;
134 virtual bool isEqualImpl(const Matcher *M) const = 0;
135 virtual unsigned getHashImpl() const = 0;
136 virtual bool isContradictoryImpl(const Matcher *M) const { return false; }
139 /// ScopeMatcher - This attempts to match each of its children to find the first
140 /// one that successfully matches. If one child fails, it tries the next child.
141 /// If none of the children match then this check fails. It never has a 'next'.
142 class ScopeMatcher : public Matcher {
143 SmallVector<Matcher*, 4> Children;
145 ScopeMatcher(Matcher *const *children, unsigned numchildren)
146 : Matcher(Scope), Children(children, children+numchildren) {
148 virtual ~ScopeMatcher();
150 unsigned getNumChildren() const { return Children.size(); }
152 Matcher *getChild(unsigned i) { return Children[i]; }
153 const Matcher *getChild(unsigned i) const { return Children[i]; }
155 void resetChild(unsigned i, Matcher *N) {
160 Matcher *takeChild(unsigned i) {
161 Matcher *Res = Children[i];
166 void setNumChildren(unsigned NC) {
167 if (NC < Children.size()) {
168 // delete any children we're about to lose pointers to.
169 for (unsigned i = NC, e = Children.size(); i != e; ++i)
175 static inline bool classof(const Matcher *N) {
176 return N->getKind() == Scope;
180 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
181 virtual bool isEqualImpl(const Matcher *M) const { return false; }
182 virtual unsigned getHashImpl() const { return 12312; }
185 /// RecordMatcher - Save the current node in the operand list.
186 class RecordMatcher : public Matcher {
187 /// WhatFor - This is a string indicating why we're recording this. This
188 /// should only be used for comment generation not anything semantic.
191 /// ResultNo - The slot number in the RecordedNodes vector that this will be,
192 /// just printed as a comment.
195 RecordMatcher(const std::string &whatfor, unsigned resultNo)
196 : Matcher(RecordNode), WhatFor(whatfor), ResultNo(resultNo) {}
198 const std::string &getWhatFor() const { return WhatFor; }
199 unsigned getResultNo() const { return ResultNo; }
201 static inline bool classof(const Matcher *N) {
202 return N->getKind() == RecordNode;
205 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
207 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
208 virtual bool isEqualImpl(const Matcher *M) const { return true; }
209 virtual unsigned getHashImpl() const { return 0; }
212 /// RecordChildMatcher - Save a numbered child of the current node, or fail
213 /// the match if it doesn't exist. This is logically equivalent to:
214 /// MoveChild N + RecordNode + MoveParent.
215 class RecordChildMatcher : public Matcher {
218 /// WhatFor - This is a string indicating why we're recording this. This
219 /// should only be used for comment generation not anything semantic.
222 /// ResultNo - The slot number in the RecordedNodes vector that this will be,
223 /// just printed as a comment.
226 RecordChildMatcher(unsigned childno, const std::string &whatfor,
228 : Matcher(RecordChild), ChildNo(childno), WhatFor(whatfor),
229 ResultNo(resultNo) {}
231 unsigned getChildNo() const { return ChildNo; }
232 const std::string &getWhatFor() const { return WhatFor; }
233 unsigned getResultNo() const { return ResultNo; }
235 static inline bool classof(const Matcher *N) {
236 return N->getKind() == RecordChild;
239 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
242 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
243 virtual bool isEqualImpl(const Matcher *M) const {
244 return cast<RecordChildMatcher>(M)->getChildNo() == getChildNo();
246 virtual unsigned getHashImpl() const { return getChildNo(); }
249 /// RecordMemRefMatcher - Save the current node's memref.
250 class RecordMemRefMatcher : public Matcher {
252 RecordMemRefMatcher() : Matcher(RecordMemRef) {}
254 static inline bool classof(const Matcher *N) {
255 return N->getKind() == RecordMemRef;
258 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
261 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
262 virtual bool isEqualImpl(const Matcher *M) const { return true; }
263 virtual unsigned getHashImpl() const { return 0; }
267 /// CaptureFlagInputMatcher - If the current record has a flag input, record
268 /// it so that it is used as an input to the generated code.
269 class CaptureFlagInputMatcher : public Matcher {
271 CaptureFlagInputMatcher() : Matcher(CaptureFlagInput) {}
273 static inline bool classof(const Matcher *N) {
274 return N->getKind() == CaptureFlagInput;
277 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
280 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
281 virtual bool isEqualImpl(const Matcher *M) const { return true; }
282 virtual unsigned getHashImpl() const { return 0; }
285 /// MoveChildMatcher - This tells the interpreter to move into the
286 /// specified child node.
287 class MoveChildMatcher : public Matcher {
290 MoveChildMatcher(unsigned childNo) : Matcher(MoveChild), ChildNo(childNo) {}
292 unsigned getChildNo() const { return ChildNo; }
294 static inline bool classof(const Matcher *N) {
295 return N->getKind() == MoveChild;
298 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
301 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
302 virtual bool isEqualImpl(const Matcher *M) const {
303 return cast<MoveChildMatcher>(M)->getChildNo() == getChildNo();
305 virtual unsigned getHashImpl() const { return getChildNo(); }
308 /// MoveParentMatcher - This tells the interpreter to move to the parent
309 /// of the current node.
310 class MoveParentMatcher : public Matcher {
312 MoveParentMatcher() : Matcher(MoveParent) {}
314 static inline bool classof(const Matcher *N) {
315 return N->getKind() == MoveParent;
318 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
321 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
322 virtual bool isEqualImpl(const Matcher *M) const { return true; }
323 virtual unsigned getHashImpl() const { return 0; }
326 /// CheckSameMatcher - This checks to see if this node is exactly the same
327 /// node as the specified match that was recorded with 'Record'. This is used
328 /// when patterns have the same name in them, like '(mul GPR:$in, GPR:$in)'.
329 class CheckSameMatcher : public Matcher {
330 unsigned MatchNumber;
332 CheckSameMatcher(unsigned matchnumber)
333 : Matcher(CheckSame), MatchNumber(matchnumber) {}
335 unsigned getMatchNumber() const { return MatchNumber; }
337 static inline bool classof(const Matcher *N) {
338 return N->getKind() == CheckSame;
341 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
344 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
345 virtual bool isEqualImpl(const Matcher *M) const {
346 return cast<CheckSameMatcher>(M)->getMatchNumber() == getMatchNumber();
348 virtual unsigned getHashImpl() const { return getMatchNumber(); }
351 /// CheckPatternPredicateMatcher - This checks the target-specific predicate
352 /// to see if the entire pattern is capable of matching. This predicate does
353 /// not take a node as input. This is used for subtarget feature checks etc.
354 class CheckPatternPredicateMatcher : public Matcher {
355 std::string Predicate;
357 CheckPatternPredicateMatcher(StringRef predicate)
358 : Matcher(CheckPatternPredicate), Predicate(predicate) {}
360 StringRef getPredicate() const { return Predicate; }
362 static inline bool classof(const Matcher *N) {
363 return N->getKind() == CheckPatternPredicate;
366 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
369 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
370 virtual bool isEqualImpl(const Matcher *M) const {
371 return cast<CheckPatternPredicateMatcher>(M)->getPredicate() == Predicate;
373 virtual unsigned getHashImpl() const;
376 /// CheckPredicateMatcher - This checks the target-specific predicate to
377 /// see if the node is acceptable.
378 class CheckPredicateMatcher : public Matcher {
381 CheckPredicateMatcher(StringRef predname)
382 : Matcher(CheckPredicate), PredName(predname) {}
384 StringRef getPredicateName() const { return PredName; }
386 static inline bool classof(const Matcher *N) {
387 return N->getKind() == CheckPredicate;
391 //virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
394 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
395 virtual bool isEqualImpl(const Matcher *M) const {
396 return cast<CheckPredicateMatcher>(M)->PredName == PredName;
398 virtual unsigned getHashImpl() const;
402 /// CheckOpcodeMatcher - This checks to see if the current node has the
403 /// specified opcode, if not it fails to match.
404 class CheckOpcodeMatcher : public Matcher {
405 const SDNodeInfo &Opcode;
407 CheckOpcodeMatcher(const SDNodeInfo &opcode)
408 : Matcher(CheckOpcode), Opcode(opcode) {}
410 const SDNodeInfo &getOpcode() const { return Opcode; }
412 static inline bool classof(const Matcher *N) {
413 return N->getKind() == CheckOpcode;
416 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
419 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
420 virtual bool isEqualImpl(const Matcher *M) const;
421 virtual unsigned getHashImpl() const;
422 virtual bool isContradictoryImpl(const Matcher *M) const;
425 /// SwitchOpcodeMatcher - Switch based on the current node's opcode, dispatching
426 /// to one matcher per opcode. If the opcode doesn't match any of the cases,
427 /// then the match fails. This is semantically equivalent to a Scope node where
428 /// every child does a CheckOpcode, but is much faster.
429 class SwitchOpcodeMatcher : public Matcher {
430 SmallVector<std::pair<const SDNodeInfo*, Matcher*>, 8> Cases;
432 SwitchOpcodeMatcher(const std::pair<const SDNodeInfo*, Matcher*> *cases,
434 : Matcher(SwitchOpcode), Cases(cases, cases+numcases) {}
436 static inline bool classof(const Matcher *N) {
437 return N->getKind() == SwitchOpcode;
440 unsigned getNumCases() const { return Cases.size(); }
442 const SDNodeInfo &getCaseOpcode(unsigned i) const { return *Cases[i].first; }
443 Matcher *getCaseMatcher(unsigned i) { return Cases[i].second; }
444 const Matcher *getCaseMatcher(unsigned i) const { return Cases[i].second; }
447 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
448 virtual bool isEqualImpl(const Matcher *M) const { return false; }
449 virtual unsigned getHashImpl() const { return 4123; }
452 /// CheckTypeMatcher - This checks to see if the current node has the
453 /// specified type, if not it fails to match.
454 class CheckTypeMatcher : public Matcher {
455 MVT::SimpleValueType Type;
457 CheckTypeMatcher(MVT::SimpleValueType type)
458 : Matcher(CheckType), Type(type) {}
460 MVT::SimpleValueType getType() const { return Type; }
462 static inline bool classof(const Matcher *N) {
463 return N->getKind() == CheckType;
466 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
469 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
470 virtual bool isEqualImpl(const Matcher *M) const {
471 return cast<CheckTypeMatcher>(M)->Type == Type;
473 virtual unsigned getHashImpl() const { return Type; }
474 virtual bool isContradictoryImpl(const Matcher *M) const;
477 /// CheckChildTypeMatcher - This checks to see if a child node has the
478 /// specified type, if not it fails to match.
479 class CheckChildTypeMatcher : public Matcher {
481 MVT::SimpleValueType Type;
483 CheckChildTypeMatcher(unsigned childno, MVT::SimpleValueType type)
484 : Matcher(CheckChildType), ChildNo(childno), Type(type) {}
486 unsigned getChildNo() const { return ChildNo; }
487 MVT::SimpleValueType getType() const { return Type; }
489 static inline bool classof(const Matcher *N) {
490 return N->getKind() == CheckChildType;
493 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
496 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
497 virtual bool isEqualImpl(const Matcher *M) const {
498 return cast<CheckChildTypeMatcher>(M)->ChildNo == ChildNo &&
499 cast<CheckChildTypeMatcher>(M)->Type == Type;
501 virtual unsigned getHashImpl() const { return (Type << 3) | ChildNo; }
502 virtual bool isContradictoryImpl(const Matcher *M) const;
506 /// CheckIntegerMatcher - This checks to see if the current node is a
507 /// ConstantSDNode with the specified integer value, if not it fails to match.
508 class CheckIntegerMatcher : public Matcher {
511 CheckIntegerMatcher(int64_t value)
512 : Matcher(CheckInteger), Value(value) {}
514 int64_t getValue() const { return Value; }
516 static inline bool classof(const Matcher *N) {
517 return N->getKind() == CheckInteger;
520 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
523 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
524 virtual bool isEqualImpl(const Matcher *M) const {
525 return cast<CheckIntegerMatcher>(M)->Value == Value;
527 virtual unsigned getHashImpl() const { return Value; }
528 virtual bool isContradictoryImpl(const Matcher *M) const;
531 /// CheckCondCodeMatcher - This checks to see if the current node is a
532 /// CondCodeSDNode with the specified condition, if not it fails to match.
533 class CheckCondCodeMatcher : public Matcher {
534 StringRef CondCodeName;
536 CheckCondCodeMatcher(StringRef condcodename)
537 : Matcher(CheckCondCode), CondCodeName(condcodename) {}
539 StringRef getCondCodeName() const { return CondCodeName; }
541 static inline bool classof(const Matcher *N) {
542 return N->getKind() == CheckCondCode;
545 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
548 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
549 virtual bool isEqualImpl(const Matcher *M) const {
550 return cast<CheckCondCodeMatcher>(M)->CondCodeName == CondCodeName;
552 virtual unsigned getHashImpl() const;
555 /// CheckValueTypeMatcher - This checks to see if the current node is a
556 /// VTSDNode with the specified type, if not it fails to match.
557 class CheckValueTypeMatcher : public Matcher {
560 CheckValueTypeMatcher(StringRef type_name)
561 : Matcher(CheckValueType), TypeName(type_name) {}
563 StringRef getTypeName() const { return TypeName; }
565 static inline bool classof(const Matcher *N) {
566 return N->getKind() == CheckValueType;
569 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
572 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
573 virtual bool isEqualImpl(const Matcher *M) const {
574 return cast<CheckValueTypeMatcher>(M)->TypeName == TypeName;
576 virtual unsigned getHashImpl() const;
581 /// CheckComplexPatMatcher - This node runs the specified ComplexPattern on
582 /// the current node.
583 class CheckComplexPatMatcher : public Matcher {
584 const ComplexPattern &Pattern;
586 CheckComplexPatMatcher(const ComplexPattern &pattern)
587 : Matcher(CheckComplexPat), Pattern(pattern) {}
589 const ComplexPattern &getPattern() const { return Pattern; }
591 static inline bool classof(const Matcher *N) {
592 return N->getKind() == CheckComplexPat;
595 // Not safe to move a pattern predicate past a complex pattern.
596 virtual bool isSafeToReorderWithPatternPredicate() const { return false; }
599 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
600 virtual bool isEqualImpl(const Matcher *M) const {
601 return &cast<CheckComplexPatMatcher>(M)->Pattern == &Pattern;
603 virtual unsigned getHashImpl() const {
604 return (unsigned)(intptr_t)&Pattern;
608 /// CheckAndImmMatcher - This checks to see if the current node is an 'and'
609 /// with something equivalent to the specified immediate.
610 class CheckAndImmMatcher : public Matcher {
613 CheckAndImmMatcher(int64_t value)
614 : Matcher(CheckAndImm), Value(value) {}
616 int64_t getValue() const { return Value; }
618 static inline bool classof(const Matcher *N) {
619 return N->getKind() == CheckAndImm;
622 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
625 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
626 virtual bool isEqualImpl(const Matcher *M) const {
627 return cast<CheckAndImmMatcher>(M)->Value == Value;
629 virtual unsigned getHashImpl() const { return Value; }
632 /// CheckOrImmMatcher - This checks to see if the current node is an 'and'
633 /// with something equivalent to the specified immediate.
634 class CheckOrImmMatcher : public Matcher {
637 CheckOrImmMatcher(int64_t value)
638 : Matcher(CheckOrImm), Value(value) {}
640 int64_t getValue() const { return Value; }
642 static inline bool classof(const Matcher *N) {
643 return N->getKind() == CheckOrImm;
646 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
649 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
650 virtual bool isEqualImpl(const Matcher *M) const {
651 return cast<CheckOrImmMatcher>(M)->Value == Value;
653 virtual unsigned getHashImpl() const { return Value; }
656 /// CheckFoldableChainNodeMatcher - This checks to see if the current node
657 /// (which defines a chain operand) is safe to fold into a larger pattern.
658 class CheckFoldableChainNodeMatcher : public Matcher {
660 CheckFoldableChainNodeMatcher()
661 : Matcher(CheckFoldableChainNode) {}
663 static inline bool classof(const Matcher *N) {
664 return N->getKind() == CheckFoldableChainNode;
667 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
670 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
671 virtual bool isEqualImpl(const Matcher *M) const { return true; }
672 virtual unsigned getHashImpl() const { return 0; }
675 /// CheckChainCompatibleMatcher - Verify that the current node's chain
676 /// operand is 'compatible' with the specified recorded node's.
677 class CheckChainCompatibleMatcher : public Matcher {
680 CheckChainCompatibleMatcher(unsigned previousop)
681 : Matcher(CheckChainCompatible), PreviousOp(previousop) {}
683 unsigned getPreviousOp() const { return PreviousOp; }
685 static inline bool classof(const Matcher *N) {
686 return N->getKind() == CheckChainCompatible;
689 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
692 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
693 virtual bool isEqualImpl(const Matcher *M) const {
694 return cast<CheckChainCompatibleMatcher>(M)->PreviousOp == PreviousOp;
696 virtual unsigned getHashImpl() const { return PreviousOp; }
699 /// EmitIntegerMatcher - This creates a new TargetConstant.
700 class EmitIntegerMatcher : public Matcher {
702 MVT::SimpleValueType VT;
704 EmitIntegerMatcher(int64_t val, MVT::SimpleValueType vt)
705 : Matcher(EmitInteger), Val(val), VT(vt) {}
707 int64_t getValue() const { return Val; }
708 MVT::SimpleValueType getVT() const { return VT; }
710 static inline bool classof(const Matcher *N) {
711 return N->getKind() == EmitInteger;
715 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
716 virtual bool isEqualImpl(const Matcher *M) const {
717 return cast<EmitIntegerMatcher>(M)->Val == Val &&
718 cast<EmitIntegerMatcher>(M)->VT == VT;
720 virtual unsigned getHashImpl() const { return (Val << 4) | VT; }
723 /// EmitStringIntegerMatcher - A target constant whose value is represented
725 class EmitStringIntegerMatcher : public Matcher {
727 MVT::SimpleValueType VT;
729 EmitStringIntegerMatcher(const std::string &val, MVT::SimpleValueType vt)
730 : Matcher(EmitStringInteger), Val(val), VT(vt) {}
732 const std::string &getValue() const { return Val; }
733 MVT::SimpleValueType getVT() const { return VT; }
735 static inline bool classof(const Matcher *N) {
736 return N->getKind() == EmitStringInteger;
740 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
741 virtual bool isEqualImpl(const Matcher *M) const {
742 return cast<EmitStringIntegerMatcher>(M)->Val == Val &&
743 cast<EmitStringIntegerMatcher>(M)->VT == VT;
745 virtual unsigned getHashImpl() const;
748 /// EmitRegisterMatcher - This creates a new TargetConstant.
749 class EmitRegisterMatcher : public Matcher {
750 /// Reg - The def for the register that we're emitting. If this is null, then
751 /// this is a reference to zero_reg.
753 MVT::SimpleValueType VT;
755 EmitRegisterMatcher(Record *reg, MVT::SimpleValueType vt)
756 : Matcher(EmitRegister), Reg(reg), VT(vt) {}
758 Record *getReg() const { return Reg; }
759 MVT::SimpleValueType getVT() const { return VT; }
761 static inline bool classof(const Matcher *N) {
762 return N->getKind() == EmitRegister;
766 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
767 virtual bool isEqualImpl(const Matcher *M) const {
768 return cast<EmitRegisterMatcher>(M)->Reg == Reg &&
769 cast<EmitRegisterMatcher>(M)->VT == VT;
771 virtual unsigned getHashImpl() const {
772 return ((unsigned)(intptr_t)Reg) << 4 | VT;
776 /// EmitConvertToTargetMatcher - Emit an operation that reads a specified
777 /// recorded node and converts it from being a ISD::Constant to
778 /// ISD::TargetConstant, likewise for ConstantFP.
779 class EmitConvertToTargetMatcher : public Matcher {
782 EmitConvertToTargetMatcher(unsigned slot)
783 : Matcher(EmitConvertToTarget), Slot(slot) {}
785 unsigned getSlot() const { return Slot; }
787 static inline bool classof(const Matcher *N) {
788 return N->getKind() == EmitConvertToTarget;
792 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
793 virtual bool isEqualImpl(const Matcher *M) const {
794 return cast<EmitConvertToTargetMatcher>(M)->Slot == Slot;
796 virtual unsigned getHashImpl() const { return Slot; }
799 /// EmitMergeInputChainsMatcher - Emit a node that merges a list of input
800 /// chains together with a token factor. The list of nodes are the nodes in the
801 /// matched pattern that have chain input/outputs. This node adds all input
802 /// chains of these nodes if they are not themselves a node in the pattern.
803 class EmitMergeInputChainsMatcher : public Matcher {
804 SmallVector<unsigned, 3> ChainNodes;
806 EmitMergeInputChainsMatcher(const unsigned *nodes, unsigned NumNodes)
807 : Matcher(EmitMergeInputChains), ChainNodes(nodes, nodes+NumNodes) {}
809 unsigned getNumNodes() const { return ChainNodes.size(); }
811 unsigned getNode(unsigned i) const {
812 assert(i < ChainNodes.size());
813 return ChainNodes[i];
816 static inline bool classof(const Matcher *N) {
817 return N->getKind() == EmitMergeInputChains;
821 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
822 virtual bool isEqualImpl(const Matcher *M) const {
823 return cast<EmitMergeInputChainsMatcher>(M)->ChainNodes == ChainNodes;
825 virtual unsigned getHashImpl() const;
828 /// EmitCopyToRegMatcher - Emit a CopyToReg node from a value to a physreg,
829 /// pushing the chain and flag results.
831 class EmitCopyToRegMatcher : public Matcher {
832 unsigned SrcSlot; // Value to copy into the physreg.
835 EmitCopyToRegMatcher(unsigned srcSlot, Record *destPhysReg)
836 : Matcher(EmitCopyToReg), SrcSlot(srcSlot), DestPhysReg(destPhysReg) {}
838 unsigned getSrcSlot() const { return SrcSlot; }
839 Record *getDestPhysReg() const { return DestPhysReg; }
841 static inline bool classof(const Matcher *N) {
842 return N->getKind() == EmitCopyToReg;
846 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
847 virtual bool isEqualImpl(const Matcher *M) const {
848 return cast<EmitCopyToRegMatcher>(M)->SrcSlot == SrcSlot &&
849 cast<EmitCopyToRegMatcher>(M)->DestPhysReg == DestPhysReg;
851 virtual unsigned getHashImpl() const {
852 return SrcSlot ^ ((unsigned)(intptr_t)DestPhysReg << 4);
858 /// EmitNodeXFormMatcher - Emit an operation that runs an SDNodeXForm on a
859 /// recorded node and records the result.
860 class EmitNodeXFormMatcher : public Matcher {
864 EmitNodeXFormMatcher(unsigned slot, Record *nodeXForm)
865 : Matcher(EmitNodeXForm), Slot(slot), NodeXForm(nodeXForm) {}
867 unsigned getSlot() const { return Slot; }
868 Record *getNodeXForm() const { return NodeXForm; }
870 static inline bool classof(const Matcher *N) {
871 return N->getKind() == EmitNodeXForm;
875 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
876 virtual bool isEqualImpl(const Matcher *M) const {
877 return cast<EmitNodeXFormMatcher>(M)->Slot == Slot &&
878 cast<EmitNodeXFormMatcher>(M)->NodeXForm == NodeXForm;
880 virtual unsigned getHashImpl() const {
881 return Slot ^ ((unsigned)(intptr_t)NodeXForm << 4);
885 /// EmitNodeMatcherCommon - Common class shared between EmitNode and
887 class EmitNodeMatcherCommon : public Matcher {
888 std::string OpcodeName;
889 const SmallVector<MVT::SimpleValueType, 3> VTs;
890 const SmallVector<unsigned, 6> Operands;
891 bool HasChain, HasInFlag, HasOutFlag, HasMemRefs;
893 /// NumFixedArityOperands - If this is a fixed arity node, this is set to -1.
894 /// If this is a varidic node, this is set to the number of fixed arity
895 /// operands in the root of the pattern. The rest are appended to this node.
896 int NumFixedArityOperands;
898 EmitNodeMatcherCommon(const std::string &opcodeName,
899 const MVT::SimpleValueType *vts, unsigned numvts,
900 const unsigned *operands, unsigned numops,
901 bool hasChain, bool hasInFlag, bool hasOutFlag,
903 int numfixedarityoperands, bool isMorphNodeTo)
904 : Matcher(isMorphNodeTo ? MorphNodeTo : EmitNode), OpcodeName(opcodeName),
905 VTs(vts, vts+numvts), Operands(operands, operands+numops),
906 HasChain(hasChain), HasInFlag(hasInFlag), HasOutFlag(hasOutFlag),
907 HasMemRefs(hasmemrefs), NumFixedArityOperands(numfixedarityoperands) {}
909 const std::string &getOpcodeName() const { return OpcodeName; }
911 unsigned getNumVTs() const { return VTs.size(); }
912 MVT::SimpleValueType getVT(unsigned i) const {
913 assert(i < VTs.size());
917 unsigned getNumOperands() const { return Operands.size(); }
918 unsigned getOperand(unsigned i) const {
919 assert(i < Operands.size());
923 const SmallVectorImpl<MVT::SimpleValueType> &getVTList() const { return VTs; }
924 const SmallVectorImpl<unsigned> &getOperandList() const { return Operands; }
927 bool hasChain() const { return HasChain; }
928 bool hasInFlag() const { return HasInFlag; }
929 bool hasOutFlag() const { return HasOutFlag; }
930 bool hasMemRefs() const { return HasMemRefs; }
931 int getNumFixedArityOperands() const { return NumFixedArityOperands; }
933 static inline bool classof(const Matcher *N) {
934 return N->getKind() == EmitNode || N->getKind() == MorphNodeTo;
938 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
939 virtual bool isEqualImpl(const Matcher *M) const;
940 virtual unsigned getHashImpl() const;
943 /// EmitNodeMatcher - This signals a successful match and generates a node.
944 class EmitNodeMatcher : public EmitNodeMatcherCommon {
945 unsigned FirstResultSlot;
947 EmitNodeMatcher(const std::string &opcodeName,
948 const MVT::SimpleValueType *vts, unsigned numvts,
949 const unsigned *operands, unsigned numops,
950 bool hasChain, bool hasInFlag, bool hasOutFlag,
952 int numfixedarityoperands, unsigned firstresultslot)
953 : EmitNodeMatcherCommon(opcodeName, vts, numvts, operands, numops, hasChain,
954 hasInFlag, hasOutFlag, hasmemrefs,
955 numfixedarityoperands, false),
956 FirstResultSlot(firstresultslot) {}
958 unsigned getFirstResultSlot() const { return FirstResultSlot; }
960 static inline bool classof(const Matcher *N) {
961 return N->getKind() == EmitNode;
966 class MorphNodeToMatcher : public EmitNodeMatcherCommon {
967 const PatternToMatch &Pattern;
969 MorphNodeToMatcher(const std::string &opcodeName,
970 const MVT::SimpleValueType *vts, unsigned numvts,
971 const unsigned *operands, unsigned numops,
972 bool hasChain, bool hasInFlag, bool hasOutFlag,
974 int numfixedarityoperands, const PatternToMatch &pattern)
975 : EmitNodeMatcherCommon(opcodeName, vts, numvts, operands, numops, hasChain,
976 hasInFlag, hasOutFlag, hasmemrefs,
977 numfixedarityoperands, true),
981 const PatternToMatch &getPattern() const { return Pattern; }
983 static inline bool classof(const Matcher *N) {
984 return N->getKind() == MorphNodeTo;
988 /// MarkFlagResultsMatcher - This node indicates which non-root nodes in the
989 /// pattern produce flags. This allows CompleteMatchMatcher to update them
990 /// with the output flag of the resultant code.
991 class MarkFlagResultsMatcher : public Matcher {
992 SmallVector<unsigned, 3> FlagResultNodes;
994 MarkFlagResultsMatcher(const unsigned *nodes, unsigned NumNodes)
995 : Matcher(MarkFlagResults), FlagResultNodes(nodes, nodes+NumNodes) {}
997 unsigned getNumNodes() const { return FlagResultNodes.size(); }
999 unsigned getNode(unsigned i) const {
1000 assert(i < FlagResultNodes.size());
1001 return FlagResultNodes[i];
1004 static inline bool classof(const Matcher *N) {
1005 return N->getKind() == MarkFlagResults;
1009 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
1010 virtual bool isEqualImpl(const Matcher *M) const {
1011 return cast<MarkFlagResultsMatcher>(M)->FlagResultNodes == FlagResultNodes;
1013 virtual unsigned getHashImpl() const;
1016 /// CompleteMatchMatcher - Complete a match by replacing the results of the
1017 /// pattern with the newly generated nodes. This also prints a comment
1018 /// indicating the source and dest patterns.
1019 class CompleteMatchMatcher : public Matcher {
1020 SmallVector<unsigned, 2> Results;
1021 const PatternToMatch &Pattern;
1023 CompleteMatchMatcher(const unsigned *results, unsigned numresults,
1024 const PatternToMatch &pattern)
1025 : Matcher(CompleteMatch), Results(results, results+numresults),
1028 unsigned getNumResults() const { return Results.size(); }
1029 unsigned getResult(unsigned R) const { return Results[R]; }
1030 const PatternToMatch &getPattern() const { return Pattern; }
1032 static inline bool classof(const Matcher *N) {
1033 return N->getKind() == CompleteMatch;
1037 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
1038 virtual bool isEqualImpl(const Matcher *M) const {
1039 return cast<CompleteMatchMatcher>(M)->Results == Results &&
1040 &cast<CompleteMatchMatcher>(M)->Pattern == &Pattern;
1042 virtual unsigned getHashImpl() const;
1045 } // end namespace llvm