1 //===- DAGISelMatcher.h - Representation of DAG pattern matcher -----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef TBLGEN_DAGISELMATCHER_H
11 #define TBLGEN_DAGISELMATCHER_H
13 #include "llvm/CodeGen/ValueTypes.h"
14 #include "llvm/ADT/OwningPtr.h"
15 #include "llvm/ADT/StringRef.h"
16 #include "llvm/ADT/SmallVector.h"
17 #include "llvm/Support/Casting.h"
20 class CodeGenDAGPatterns;
28 Matcher *ConvertPatternToMatcher(const PatternToMatch &Pattern,unsigned Variant,
29 const CodeGenDAGPatterns &CGP);
30 Matcher *OptimizeMatcher(Matcher *Matcher, const CodeGenDAGPatterns &CGP);
31 void EmitMatcherTable(const Matcher *Matcher, const CodeGenDAGPatterns &CGP,
35 /// Matcher - Base class for all the the DAG ISel Matcher representation
38 // The next matcher node that is executed after this one. Null if this is the
39 // last stage of a match.
40 OwningPtr<Matcher> Next;
43 // Matcher state manipulation.
44 Scope, // Push a checking scope.
45 RecordNode, // Record the current node.
46 RecordChild, // Record a child of the current node.
47 RecordMemRef, // Record the memref in the current node.
48 CaptureFlagInput, // If the current node has an input flag, save it.
49 MoveChild, // Move current node to specified child.
50 MoveParent, // Move current node to parent.
52 // Predicate checking.
53 CheckSame, // Fail if not same as prev match.
54 CheckPatternPredicate,
55 CheckPredicate, // Fail if node predicate fails.
56 CheckOpcode, // Fail if not opcode.
57 SwitchOpcode, // Dispatch based on opcode.
58 CheckType, // Fail if not correct type.
59 SwitchType, // Dispatch based on type.
60 CheckChildType, // Fail if child has wrong type.
61 CheckInteger, // Fail if wrong val.
62 CheckCondCode, // Fail if not condcode.
67 CheckFoldableChainNode,
69 // Node creation/emisssion.
70 EmitInteger, // Create a TargetConstant
71 EmitStringInteger, // Create a TargetConstant from a string.
72 EmitRegister, // Create a register.
73 EmitConvertToTarget, // Convert a imm/fpimm to target imm/fpimm
74 EmitMergeInputChains, // Merge together a chains for an input.
75 EmitCopyToReg, // Emit a copytoreg into a physreg.
76 EmitNode, // Create a DAG node
77 EmitNodeXForm, // Run a SDNodeXForm
78 MarkFlagResults, // Indicate which interior nodes have flag results.
79 CompleteMatch, // Finish a match and update the results.
80 MorphNodeTo // Build a node, finish a match and update results.
85 Matcher(KindTy K) : Kind(K) {}
89 KindTy getKind() const { return Kind; }
91 Matcher *getNext() { return Next.get(); }
92 const Matcher *getNext() const { return Next.get(); }
93 void setNext(Matcher *C) { Next.reset(C); }
94 Matcher *takeNext() { return Next.take(); }
96 OwningPtr<Matcher> &getNextPtr() { return Next; }
98 static inline bool classof(const Matcher *) { return true; }
100 bool isEqual(const Matcher *M) const {
101 if (getKind() != M->getKind()) return false;
102 return isEqualImpl(M);
105 unsigned getHash() const {
106 // Clear the high bit so we don't conflict with tombstones etc.
107 return ((getHashImpl() << 4) ^ getKind()) & (~0U>>1);
110 /// isSafeToReorderWithPatternPredicate - Return true if it is safe to sink a
111 /// PatternPredicate node past this one.
112 virtual bool isSafeToReorderWithPatternPredicate() const {
116 /// isSimplePredicateNode - Return true if this is a simple predicate that
117 /// operates on the node or its children without potential side effects or a
118 /// change of the current node.
119 bool isSimplePredicateNode() const {
121 default: return false;
123 case CheckPatternPredicate:
133 case CheckFoldableChainNode:
138 /// isSimplePredicateOrRecordNode - Return true if this is a record node or
139 /// a simple predicate.
140 bool isSimplePredicateOrRecordNode() const {
141 return isSimplePredicateNode() ||
142 getKind() == RecordNode || getKind() == RecordChild;
145 /// unlinkNode - Unlink the specified node from this chain. If Other == this,
146 /// we unlink the next pointer and return it. Otherwise we unlink Other from
147 /// the list and return this.
148 Matcher *unlinkNode(Matcher *Other);
150 /// canMoveBefore - Return true if this matcher is the same as Other, or if
151 /// we can move this matcher past all of the nodes in-between Other and this
152 /// node. Other must be equal to or before this.
153 bool canMoveBefore(const Matcher *Other) const;
155 /// canMoveBefore - Return true if it is safe to move the current matcher
156 /// across the specified one.
157 bool canMoveBeforeNode(const Matcher *Other) const;
159 /// isContradictory - Return true of these two matchers could never match on
161 bool isContradictory(const Matcher *Other) const {
162 // Since this predicate is reflexive, we canonicalize the ordering so that
163 // we always match a node against nodes with kinds that are greater or equal
164 // to them. For example, we'll pass in a CheckType node as an argument to
165 // the CheckOpcode method, not the other way around.
166 if (getKind() < Other->getKind())
167 return isContradictoryImpl(Other);
168 return Other->isContradictoryImpl(this);
171 void print(raw_ostream &OS, unsigned indent = 0) const;
172 void printOne(raw_ostream &OS) const;
175 virtual void printImpl(raw_ostream &OS, unsigned indent) const = 0;
176 virtual bool isEqualImpl(const Matcher *M) const = 0;
177 virtual unsigned getHashImpl() const = 0;
178 virtual bool isContradictoryImpl(const Matcher *M) const { return false; }
181 /// ScopeMatcher - This attempts to match each of its children to find the first
182 /// one that successfully matches. If one child fails, it tries the next child.
183 /// If none of the children match then this check fails. It never has a 'next'.
184 class ScopeMatcher : public Matcher {
185 SmallVector<Matcher*, 4> Children;
187 ScopeMatcher(Matcher *const *children, unsigned numchildren)
188 : Matcher(Scope), Children(children, children+numchildren) {
190 virtual ~ScopeMatcher();
192 unsigned getNumChildren() const { return Children.size(); }
194 Matcher *getChild(unsigned i) { return Children[i]; }
195 const Matcher *getChild(unsigned i) const { return Children[i]; }
197 void resetChild(unsigned i, Matcher *N) {
202 Matcher *takeChild(unsigned i) {
203 Matcher *Res = Children[i];
208 void setNumChildren(unsigned NC) {
209 if (NC < Children.size()) {
210 // delete any children we're about to lose pointers to.
211 for (unsigned i = NC, e = Children.size(); i != e; ++i)
217 static inline bool classof(const Matcher *N) {
218 return N->getKind() == Scope;
222 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
223 virtual bool isEqualImpl(const Matcher *M) const { return false; }
224 virtual unsigned getHashImpl() const { return 12312; }
227 /// RecordMatcher - Save the current node in the operand list.
228 class RecordMatcher : public Matcher {
229 /// WhatFor - This is a string indicating why we're recording this. This
230 /// should only be used for comment generation not anything semantic.
233 /// ResultNo - The slot number in the RecordedNodes vector that this will be,
234 /// just printed as a comment.
237 RecordMatcher(const std::string &whatfor, unsigned resultNo)
238 : Matcher(RecordNode), WhatFor(whatfor), ResultNo(resultNo) {}
240 const std::string &getWhatFor() const { return WhatFor; }
241 unsigned getResultNo() const { return ResultNo; }
243 static inline bool classof(const Matcher *N) {
244 return N->getKind() == RecordNode;
247 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
249 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
250 virtual bool isEqualImpl(const Matcher *M) const { return true; }
251 virtual unsigned getHashImpl() const { return 0; }
254 /// RecordChildMatcher - Save a numbered child of the current node, or fail
255 /// the match if it doesn't exist. This is logically equivalent to:
256 /// MoveChild N + RecordNode + MoveParent.
257 class RecordChildMatcher : public Matcher {
260 /// WhatFor - This is a string indicating why we're recording this. This
261 /// should only be used for comment generation not anything semantic.
264 /// ResultNo - The slot number in the RecordedNodes vector that this will be,
265 /// just printed as a comment.
268 RecordChildMatcher(unsigned childno, const std::string &whatfor,
270 : Matcher(RecordChild), ChildNo(childno), WhatFor(whatfor),
271 ResultNo(resultNo) {}
273 unsigned getChildNo() const { return ChildNo; }
274 const std::string &getWhatFor() const { return WhatFor; }
275 unsigned getResultNo() const { return ResultNo; }
277 static inline bool classof(const Matcher *N) {
278 return N->getKind() == RecordChild;
281 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
284 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
285 virtual bool isEqualImpl(const Matcher *M) const {
286 return cast<RecordChildMatcher>(M)->getChildNo() == getChildNo();
288 virtual unsigned getHashImpl() const { return getChildNo(); }
291 /// RecordMemRefMatcher - Save the current node's memref.
292 class RecordMemRefMatcher : public Matcher {
294 RecordMemRefMatcher() : Matcher(RecordMemRef) {}
296 static inline bool classof(const Matcher *N) {
297 return N->getKind() == RecordMemRef;
300 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
303 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
304 virtual bool isEqualImpl(const Matcher *M) const { return true; }
305 virtual unsigned getHashImpl() const { return 0; }
309 /// CaptureFlagInputMatcher - If the current record has a flag input, record
310 /// it so that it is used as an input to the generated code.
311 class CaptureFlagInputMatcher : public Matcher {
313 CaptureFlagInputMatcher() : Matcher(CaptureFlagInput) {}
315 static inline bool classof(const Matcher *N) {
316 return N->getKind() == CaptureFlagInput;
319 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
322 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
323 virtual bool isEqualImpl(const Matcher *M) const { return true; }
324 virtual unsigned getHashImpl() const { return 0; }
327 /// MoveChildMatcher - This tells the interpreter to move into the
328 /// specified child node.
329 class MoveChildMatcher : public Matcher {
332 MoveChildMatcher(unsigned childNo) : Matcher(MoveChild), ChildNo(childNo) {}
334 unsigned getChildNo() const { return ChildNo; }
336 static inline bool classof(const Matcher *N) {
337 return N->getKind() == MoveChild;
340 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
343 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
344 virtual bool isEqualImpl(const Matcher *M) const {
345 return cast<MoveChildMatcher>(M)->getChildNo() == getChildNo();
347 virtual unsigned getHashImpl() const { return getChildNo(); }
350 /// MoveParentMatcher - This tells the interpreter to move to the parent
351 /// of the current node.
352 class MoveParentMatcher : public Matcher {
354 MoveParentMatcher() : Matcher(MoveParent) {}
356 static inline bool classof(const Matcher *N) {
357 return N->getKind() == MoveParent;
360 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
363 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
364 virtual bool isEqualImpl(const Matcher *M) const { return true; }
365 virtual unsigned getHashImpl() const { return 0; }
368 /// CheckSameMatcher - This checks to see if this node is exactly the same
369 /// node as the specified match that was recorded with 'Record'. This is used
370 /// when patterns have the same name in them, like '(mul GPR:$in, GPR:$in)'.
371 class CheckSameMatcher : public Matcher {
372 unsigned MatchNumber;
374 CheckSameMatcher(unsigned matchnumber)
375 : Matcher(CheckSame), MatchNumber(matchnumber) {}
377 unsigned getMatchNumber() const { return MatchNumber; }
379 static inline bool classof(const Matcher *N) {
380 return N->getKind() == CheckSame;
383 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
386 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
387 virtual bool isEqualImpl(const Matcher *M) const {
388 return cast<CheckSameMatcher>(M)->getMatchNumber() == getMatchNumber();
390 virtual unsigned getHashImpl() const { return getMatchNumber(); }
393 /// CheckPatternPredicateMatcher - This checks the target-specific predicate
394 /// to see if the entire pattern is capable of matching. This predicate does
395 /// not take a node as input. This is used for subtarget feature checks etc.
396 class CheckPatternPredicateMatcher : public Matcher {
397 std::string Predicate;
399 CheckPatternPredicateMatcher(StringRef predicate)
400 : Matcher(CheckPatternPredicate), Predicate(predicate) {}
402 StringRef getPredicate() const { return Predicate; }
404 static inline bool classof(const Matcher *N) {
405 return N->getKind() == CheckPatternPredicate;
408 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
411 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
412 virtual bool isEqualImpl(const Matcher *M) const {
413 return cast<CheckPatternPredicateMatcher>(M)->getPredicate() == Predicate;
415 virtual unsigned getHashImpl() const;
418 /// CheckPredicateMatcher - This checks the target-specific predicate to
419 /// see if the node is acceptable.
420 class CheckPredicateMatcher : public Matcher {
423 CheckPredicateMatcher(StringRef predname)
424 : Matcher(CheckPredicate), PredName(predname) {}
426 StringRef getPredicateName() const { return PredName; }
428 static inline bool classof(const Matcher *N) {
429 return N->getKind() == CheckPredicate;
433 //virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
436 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
437 virtual bool isEqualImpl(const Matcher *M) const {
438 return cast<CheckPredicateMatcher>(M)->PredName == PredName;
440 virtual unsigned getHashImpl() const;
444 /// CheckOpcodeMatcher - This checks to see if the current node has the
445 /// specified opcode, if not it fails to match.
446 class CheckOpcodeMatcher : public Matcher {
447 const SDNodeInfo &Opcode;
449 CheckOpcodeMatcher(const SDNodeInfo &opcode)
450 : Matcher(CheckOpcode), Opcode(opcode) {}
452 const SDNodeInfo &getOpcode() const { return Opcode; }
454 static inline bool classof(const Matcher *N) {
455 return N->getKind() == CheckOpcode;
458 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
461 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
462 virtual bool isEqualImpl(const Matcher *M) const;
463 virtual unsigned getHashImpl() const;
464 virtual bool isContradictoryImpl(const Matcher *M) const;
467 /// SwitchOpcodeMatcher - Switch based on the current node's opcode, dispatching
468 /// to one matcher per opcode. If the opcode doesn't match any of the cases,
469 /// then the match fails. This is semantically equivalent to a Scope node where
470 /// every child does a CheckOpcode, but is much faster.
471 class SwitchOpcodeMatcher : public Matcher {
472 SmallVector<std::pair<const SDNodeInfo*, Matcher*>, 8> Cases;
474 SwitchOpcodeMatcher(const std::pair<const SDNodeInfo*, Matcher*> *cases,
476 : Matcher(SwitchOpcode), Cases(cases, cases+numcases) {}
478 static inline bool classof(const Matcher *N) {
479 return N->getKind() == SwitchOpcode;
482 unsigned getNumCases() const { return Cases.size(); }
484 const SDNodeInfo &getCaseOpcode(unsigned i) const { return *Cases[i].first; }
485 Matcher *getCaseMatcher(unsigned i) { return Cases[i].second; }
486 const Matcher *getCaseMatcher(unsigned i) const { return Cases[i].second; }
489 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
490 virtual bool isEqualImpl(const Matcher *M) const { return false; }
491 virtual unsigned getHashImpl() const { return 4123; }
494 /// CheckTypeMatcher - This checks to see if the current node has the
495 /// specified type, if not it fails to match.
496 class CheckTypeMatcher : public Matcher {
497 MVT::SimpleValueType Type;
499 CheckTypeMatcher(MVT::SimpleValueType type)
500 : Matcher(CheckType), Type(type) {}
502 MVT::SimpleValueType getType() const { return Type; }
504 static inline bool classof(const Matcher *N) {
505 return N->getKind() == CheckType;
508 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
511 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
512 virtual bool isEqualImpl(const Matcher *M) const {
513 return cast<CheckTypeMatcher>(M)->Type == Type;
515 virtual unsigned getHashImpl() const { return Type; }
516 virtual bool isContradictoryImpl(const Matcher *M) const;
519 /// SwitchTypeMatcher - Switch based on the current node's type, dispatching
520 /// to one matcher per case. If the type doesn't match any of the cases,
521 /// then the match fails. This is semantically equivalent to a Scope node where
522 /// every child does a CheckType, but is much faster.
523 class SwitchTypeMatcher : public Matcher {
524 SmallVector<std::pair<MVT::SimpleValueType, Matcher*>, 8> Cases;
526 SwitchTypeMatcher(const std::pair<MVT::SimpleValueType, Matcher*> *cases,
528 : Matcher(SwitchType), Cases(cases, cases+numcases) {}
530 static inline bool classof(const Matcher *N) {
531 return N->getKind() == SwitchType;
534 unsigned getNumCases() const { return Cases.size(); }
536 MVT::SimpleValueType getCaseType(unsigned i) const { return Cases[i].first; }
537 Matcher *getCaseMatcher(unsigned i) { return Cases[i].second; }
538 const Matcher *getCaseMatcher(unsigned i) const { return Cases[i].second; }
541 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
542 virtual bool isEqualImpl(const Matcher *M) const { return false; }
543 virtual unsigned getHashImpl() const { return 4123; }
547 /// CheckChildTypeMatcher - This checks to see if a child node has the
548 /// specified type, if not it fails to match.
549 class CheckChildTypeMatcher : public Matcher {
551 MVT::SimpleValueType Type;
553 CheckChildTypeMatcher(unsigned childno, MVT::SimpleValueType type)
554 : Matcher(CheckChildType), ChildNo(childno), Type(type) {}
556 unsigned getChildNo() const { return ChildNo; }
557 MVT::SimpleValueType getType() const { return Type; }
559 static inline bool classof(const Matcher *N) {
560 return N->getKind() == CheckChildType;
563 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
566 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
567 virtual bool isEqualImpl(const Matcher *M) const {
568 return cast<CheckChildTypeMatcher>(M)->ChildNo == ChildNo &&
569 cast<CheckChildTypeMatcher>(M)->Type == Type;
571 virtual unsigned getHashImpl() const { return (Type << 3) | ChildNo; }
572 virtual bool isContradictoryImpl(const Matcher *M) const;
576 /// CheckIntegerMatcher - This checks to see if the current node is a
577 /// ConstantSDNode with the specified integer value, if not it fails to match.
578 class CheckIntegerMatcher : public Matcher {
581 CheckIntegerMatcher(int64_t value)
582 : Matcher(CheckInteger), Value(value) {}
584 int64_t getValue() const { return Value; }
586 static inline bool classof(const Matcher *N) {
587 return N->getKind() == CheckInteger;
590 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
593 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
594 virtual bool isEqualImpl(const Matcher *M) const {
595 return cast<CheckIntegerMatcher>(M)->Value == Value;
597 virtual unsigned getHashImpl() const { return Value; }
598 virtual bool isContradictoryImpl(const Matcher *M) const;
601 /// CheckCondCodeMatcher - This checks to see if the current node is a
602 /// CondCodeSDNode with the specified condition, if not it fails to match.
603 class CheckCondCodeMatcher : public Matcher {
604 StringRef CondCodeName;
606 CheckCondCodeMatcher(StringRef condcodename)
607 : Matcher(CheckCondCode), CondCodeName(condcodename) {}
609 StringRef getCondCodeName() const { return CondCodeName; }
611 static inline bool classof(const Matcher *N) {
612 return N->getKind() == CheckCondCode;
615 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
618 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
619 virtual bool isEqualImpl(const Matcher *M) const {
620 return cast<CheckCondCodeMatcher>(M)->CondCodeName == CondCodeName;
622 virtual unsigned getHashImpl() const;
625 /// CheckValueTypeMatcher - This checks to see if the current node is a
626 /// VTSDNode with the specified type, if not it fails to match.
627 class CheckValueTypeMatcher : public Matcher {
630 CheckValueTypeMatcher(StringRef type_name)
631 : Matcher(CheckValueType), TypeName(type_name) {}
633 StringRef getTypeName() const { return TypeName; }
635 static inline bool classof(const Matcher *N) {
636 return N->getKind() == CheckValueType;
639 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
642 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
643 virtual bool isEqualImpl(const Matcher *M) const {
644 return cast<CheckValueTypeMatcher>(M)->TypeName == TypeName;
646 virtual unsigned getHashImpl() const;
647 bool isContradictoryImpl(const Matcher *M) const;
652 /// CheckComplexPatMatcher - This node runs the specified ComplexPattern on
653 /// the current node.
654 class CheckComplexPatMatcher : public Matcher {
655 const ComplexPattern &Pattern;
657 /// MatchNumber - This is the recorded nodes slot that contains the node we want to
659 unsigned MatchNumber;
661 /// Name - The name of the node we're matching, for comment emission.
664 /// FirstResult - This is the first slot in the RecordedNodes list that the
665 /// result of the match populates.
666 unsigned FirstResult;
668 CheckComplexPatMatcher(const ComplexPattern &pattern, unsigned matchnumber,
669 const std::string &name, unsigned firstresult)
670 : Matcher(CheckComplexPat), Pattern(pattern), MatchNumber(matchnumber),
671 Name(name), FirstResult(firstresult) {}
673 const ComplexPattern &getPattern() const { return Pattern; }
674 unsigned getMatchNumber() const { return MatchNumber; }
676 const std::string getName() const { return Name; }
677 unsigned getFirstResult() const { return FirstResult; }
679 static inline bool classof(const Matcher *N) {
680 return N->getKind() == CheckComplexPat;
683 // Not safe to move a pattern predicate past a complex pattern.
684 virtual bool isSafeToReorderWithPatternPredicate() const { return false; }
687 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
688 virtual bool isEqualImpl(const Matcher *M) const {
689 return &cast<CheckComplexPatMatcher>(M)->Pattern == &Pattern &&
690 cast<CheckComplexPatMatcher>(M)->MatchNumber == MatchNumber;
692 virtual unsigned getHashImpl() const {
693 return (unsigned)(intptr_t)&Pattern ^ MatchNumber;
697 /// CheckAndImmMatcher - This checks to see if the current node is an 'and'
698 /// with something equivalent to the specified immediate.
699 class CheckAndImmMatcher : public Matcher {
702 CheckAndImmMatcher(int64_t value)
703 : Matcher(CheckAndImm), Value(value) {}
705 int64_t getValue() const { return Value; }
707 static inline bool classof(const Matcher *N) {
708 return N->getKind() == CheckAndImm;
711 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
714 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
715 virtual bool isEqualImpl(const Matcher *M) const {
716 return cast<CheckAndImmMatcher>(M)->Value == Value;
718 virtual unsigned getHashImpl() const { return Value; }
721 /// CheckOrImmMatcher - This checks to see if the current node is an 'and'
722 /// with something equivalent to the specified immediate.
723 class CheckOrImmMatcher : public Matcher {
726 CheckOrImmMatcher(int64_t value)
727 : Matcher(CheckOrImm), Value(value) {}
729 int64_t getValue() const { return Value; }
731 static inline bool classof(const Matcher *N) {
732 return N->getKind() == CheckOrImm;
735 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
738 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
739 virtual bool isEqualImpl(const Matcher *M) const {
740 return cast<CheckOrImmMatcher>(M)->Value == Value;
742 virtual unsigned getHashImpl() const { return Value; }
745 /// CheckFoldableChainNodeMatcher - This checks to see if the current node
746 /// (which defines a chain operand) is safe to fold into a larger pattern.
747 class CheckFoldableChainNodeMatcher : public Matcher {
749 CheckFoldableChainNodeMatcher()
750 : Matcher(CheckFoldableChainNode) {}
752 static inline bool classof(const Matcher *N) {
753 return N->getKind() == CheckFoldableChainNode;
756 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
759 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
760 virtual bool isEqualImpl(const Matcher *M) const { return true; }
761 virtual unsigned getHashImpl() const { return 0; }
764 /// EmitIntegerMatcher - This creates a new TargetConstant.
765 class EmitIntegerMatcher : public Matcher {
767 MVT::SimpleValueType VT;
769 EmitIntegerMatcher(int64_t val, MVT::SimpleValueType vt)
770 : Matcher(EmitInteger), Val(val), VT(vt) {}
772 int64_t getValue() const { return Val; }
773 MVT::SimpleValueType getVT() const { return VT; }
775 static inline bool classof(const Matcher *N) {
776 return N->getKind() == EmitInteger;
780 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
781 virtual bool isEqualImpl(const Matcher *M) const {
782 return cast<EmitIntegerMatcher>(M)->Val == Val &&
783 cast<EmitIntegerMatcher>(M)->VT == VT;
785 virtual unsigned getHashImpl() const { return (Val << 4) | VT; }
788 /// EmitStringIntegerMatcher - A target constant whose value is represented
790 class EmitStringIntegerMatcher : public Matcher {
792 MVT::SimpleValueType VT;
794 EmitStringIntegerMatcher(const std::string &val, MVT::SimpleValueType vt)
795 : Matcher(EmitStringInteger), Val(val), VT(vt) {}
797 const std::string &getValue() const { return Val; }
798 MVT::SimpleValueType getVT() const { return VT; }
800 static inline bool classof(const Matcher *N) {
801 return N->getKind() == EmitStringInteger;
805 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
806 virtual bool isEqualImpl(const Matcher *M) const {
807 return cast<EmitStringIntegerMatcher>(M)->Val == Val &&
808 cast<EmitStringIntegerMatcher>(M)->VT == VT;
810 virtual unsigned getHashImpl() const;
813 /// EmitRegisterMatcher - This creates a new TargetConstant.
814 class EmitRegisterMatcher : public Matcher {
815 /// Reg - The def for the register that we're emitting. If this is null, then
816 /// this is a reference to zero_reg.
818 MVT::SimpleValueType VT;
820 EmitRegisterMatcher(Record *reg, MVT::SimpleValueType vt)
821 : Matcher(EmitRegister), Reg(reg), VT(vt) {}
823 Record *getReg() const { return Reg; }
824 MVT::SimpleValueType getVT() const { return VT; }
826 static inline bool classof(const Matcher *N) {
827 return N->getKind() == EmitRegister;
831 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
832 virtual bool isEqualImpl(const Matcher *M) const {
833 return cast<EmitRegisterMatcher>(M)->Reg == Reg &&
834 cast<EmitRegisterMatcher>(M)->VT == VT;
836 virtual unsigned getHashImpl() const {
837 return ((unsigned)(intptr_t)Reg) << 4 | VT;
841 /// EmitConvertToTargetMatcher - Emit an operation that reads a specified
842 /// recorded node and converts it from being a ISD::Constant to
843 /// ISD::TargetConstant, likewise for ConstantFP.
844 class EmitConvertToTargetMatcher : public Matcher {
847 EmitConvertToTargetMatcher(unsigned slot)
848 : Matcher(EmitConvertToTarget), Slot(slot) {}
850 unsigned getSlot() const { return Slot; }
852 static inline bool classof(const Matcher *N) {
853 return N->getKind() == EmitConvertToTarget;
857 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
858 virtual bool isEqualImpl(const Matcher *M) const {
859 return cast<EmitConvertToTargetMatcher>(M)->Slot == Slot;
861 virtual unsigned getHashImpl() const { return Slot; }
864 /// EmitMergeInputChainsMatcher - Emit a node that merges a list of input
865 /// chains together with a token factor. The list of nodes are the nodes in the
866 /// matched pattern that have chain input/outputs. This node adds all input
867 /// chains of these nodes if they are not themselves a node in the pattern.
868 class EmitMergeInputChainsMatcher : public Matcher {
869 SmallVector<unsigned, 3> ChainNodes;
871 EmitMergeInputChainsMatcher(const unsigned *nodes, unsigned NumNodes)
872 : Matcher(EmitMergeInputChains), ChainNodes(nodes, nodes+NumNodes) {}
874 unsigned getNumNodes() const { return ChainNodes.size(); }
876 unsigned getNode(unsigned i) const {
877 assert(i < ChainNodes.size());
878 return ChainNodes[i];
881 static inline bool classof(const Matcher *N) {
882 return N->getKind() == EmitMergeInputChains;
886 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
887 virtual bool isEqualImpl(const Matcher *M) const {
888 return cast<EmitMergeInputChainsMatcher>(M)->ChainNodes == ChainNodes;
890 virtual unsigned getHashImpl() const;
893 /// EmitCopyToRegMatcher - Emit a CopyToReg node from a value to a physreg,
894 /// pushing the chain and flag results.
896 class EmitCopyToRegMatcher : public Matcher {
897 unsigned SrcSlot; // Value to copy into the physreg.
900 EmitCopyToRegMatcher(unsigned srcSlot, Record *destPhysReg)
901 : Matcher(EmitCopyToReg), SrcSlot(srcSlot), DestPhysReg(destPhysReg) {}
903 unsigned getSrcSlot() const { return SrcSlot; }
904 Record *getDestPhysReg() const { return DestPhysReg; }
906 static inline bool classof(const Matcher *N) {
907 return N->getKind() == EmitCopyToReg;
911 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
912 virtual bool isEqualImpl(const Matcher *M) const {
913 return cast<EmitCopyToRegMatcher>(M)->SrcSlot == SrcSlot &&
914 cast<EmitCopyToRegMatcher>(M)->DestPhysReg == DestPhysReg;
916 virtual unsigned getHashImpl() const {
917 return SrcSlot ^ ((unsigned)(intptr_t)DestPhysReg << 4);
923 /// EmitNodeXFormMatcher - Emit an operation that runs an SDNodeXForm on a
924 /// recorded node and records the result.
925 class EmitNodeXFormMatcher : public Matcher {
929 EmitNodeXFormMatcher(unsigned slot, Record *nodeXForm)
930 : Matcher(EmitNodeXForm), Slot(slot), NodeXForm(nodeXForm) {}
932 unsigned getSlot() const { return Slot; }
933 Record *getNodeXForm() const { return NodeXForm; }
935 static inline bool classof(const Matcher *N) {
936 return N->getKind() == EmitNodeXForm;
940 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
941 virtual bool isEqualImpl(const Matcher *M) const {
942 return cast<EmitNodeXFormMatcher>(M)->Slot == Slot &&
943 cast<EmitNodeXFormMatcher>(M)->NodeXForm == NodeXForm;
945 virtual unsigned getHashImpl() const {
946 return Slot ^ ((unsigned)(intptr_t)NodeXForm << 4);
950 /// EmitNodeMatcherCommon - Common class shared between EmitNode and
952 class EmitNodeMatcherCommon : public Matcher {
953 std::string OpcodeName;
954 const SmallVector<MVT::SimpleValueType, 3> VTs;
955 const SmallVector<unsigned, 6> Operands;
956 bool HasChain, HasInFlag, HasOutFlag, HasMemRefs;
958 /// NumFixedArityOperands - If this is a fixed arity node, this is set to -1.
959 /// If this is a varidic node, this is set to the number of fixed arity
960 /// operands in the root of the pattern. The rest are appended to this node.
961 int NumFixedArityOperands;
963 EmitNodeMatcherCommon(const std::string &opcodeName,
964 const MVT::SimpleValueType *vts, unsigned numvts,
965 const unsigned *operands, unsigned numops,
966 bool hasChain, bool hasInFlag, bool hasOutFlag,
968 int numfixedarityoperands, bool isMorphNodeTo)
969 : Matcher(isMorphNodeTo ? MorphNodeTo : EmitNode), OpcodeName(opcodeName),
970 VTs(vts, vts+numvts), Operands(operands, operands+numops),
971 HasChain(hasChain), HasInFlag(hasInFlag), HasOutFlag(hasOutFlag),
972 HasMemRefs(hasmemrefs), NumFixedArityOperands(numfixedarityoperands) {}
974 const std::string &getOpcodeName() const { return OpcodeName; }
976 unsigned getNumVTs() const { return VTs.size(); }
977 MVT::SimpleValueType getVT(unsigned i) const {
978 assert(i < VTs.size());
982 unsigned getNumOperands() const { return Operands.size(); }
983 unsigned getOperand(unsigned i) const {
984 assert(i < Operands.size());
988 const SmallVectorImpl<MVT::SimpleValueType> &getVTList() const { return VTs; }
989 const SmallVectorImpl<unsigned> &getOperandList() const { return Operands; }
992 bool hasChain() const { return HasChain; }
993 bool hasInFlag() const { return HasInFlag; }
994 bool hasOutFlag() const { return HasOutFlag; }
995 bool hasMemRefs() const { return HasMemRefs; }
996 int getNumFixedArityOperands() const { return NumFixedArityOperands; }
998 static inline bool classof(const Matcher *N) {
999 return N->getKind() == EmitNode || N->getKind() == MorphNodeTo;
1003 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
1004 virtual bool isEqualImpl(const Matcher *M) const;
1005 virtual unsigned getHashImpl() const;
1008 /// EmitNodeMatcher - This signals a successful match and generates a node.
1009 class EmitNodeMatcher : public EmitNodeMatcherCommon {
1010 unsigned FirstResultSlot;
1012 EmitNodeMatcher(const std::string &opcodeName,
1013 const MVT::SimpleValueType *vts, unsigned numvts,
1014 const unsigned *operands, unsigned numops,
1015 bool hasChain, bool hasInFlag, bool hasOutFlag,
1017 int numfixedarityoperands, unsigned firstresultslot)
1018 : EmitNodeMatcherCommon(opcodeName, vts, numvts, operands, numops, hasChain,
1019 hasInFlag, hasOutFlag, hasmemrefs,
1020 numfixedarityoperands, false),
1021 FirstResultSlot(firstresultslot) {}
1023 unsigned getFirstResultSlot() const { return FirstResultSlot; }
1025 static inline bool classof(const Matcher *N) {
1026 return N->getKind() == EmitNode;
1031 class MorphNodeToMatcher : public EmitNodeMatcherCommon {
1032 const PatternToMatch &Pattern;
1034 MorphNodeToMatcher(const std::string &opcodeName,
1035 const MVT::SimpleValueType *vts, unsigned numvts,
1036 const unsigned *operands, unsigned numops,
1037 bool hasChain, bool hasInFlag, bool hasOutFlag,
1039 int numfixedarityoperands, const PatternToMatch &pattern)
1040 : EmitNodeMatcherCommon(opcodeName, vts, numvts, operands, numops, hasChain,
1041 hasInFlag, hasOutFlag, hasmemrefs,
1042 numfixedarityoperands, true),
1046 const PatternToMatch &getPattern() const { return Pattern; }
1048 static inline bool classof(const Matcher *N) {
1049 return N->getKind() == MorphNodeTo;
1053 /// MarkFlagResultsMatcher - This node indicates which non-root nodes in the
1054 /// pattern produce flags. This allows CompleteMatchMatcher to update them
1055 /// with the output flag of the resultant code.
1056 class MarkFlagResultsMatcher : public Matcher {
1057 SmallVector<unsigned, 3> FlagResultNodes;
1059 MarkFlagResultsMatcher(const unsigned *nodes, unsigned NumNodes)
1060 : Matcher(MarkFlagResults), FlagResultNodes(nodes, nodes+NumNodes) {}
1062 unsigned getNumNodes() const { return FlagResultNodes.size(); }
1064 unsigned getNode(unsigned i) const {
1065 assert(i < FlagResultNodes.size());
1066 return FlagResultNodes[i];
1069 static inline bool classof(const Matcher *N) {
1070 return N->getKind() == MarkFlagResults;
1074 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
1075 virtual bool isEqualImpl(const Matcher *M) const {
1076 return cast<MarkFlagResultsMatcher>(M)->FlagResultNodes == FlagResultNodes;
1078 virtual unsigned getHashImpl() const;
1081 /// CompleteMatchMatcher - Complete a match by replacing the results of the
1082 /// pattern with the newly generated nodes. This also prints a comment
1083 /// indicating the source and dest patterns.
1084 class CompleteMatchMatcher : public Matcher {
1085 SmallVector<unsigned, 2> Results;
1086 const PatternToMatch &Pattern;
1088 CompleteMatchMatcher(const unsigned *results, unsigned numresults,
1089 const PatternToMatch &pattern)
1090 : Matcher(CompleteMatch), Results(results, results+numresults),
1093 unsigned getNumResults() const { return Results.size(); }
1094 unsigned getResult(unsigned R) const { return Results[R]; }
1095 const PatternToMatch &getPattern() const { return Pattern; }
1097 static inline bool classof(const Matcher *N) {
1098 return N->getKind() == CompleteMatch;
1102 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
1103 virtual bool isEqualImpl(const Matcher *M) const {
1104 return cast<CompleteMatchMatcher>(M)->Results == Results &&
1105 &cast<CompleteMatchMatcher>(M)->Pattern == &Pattern;
1107 virtual unsigned getHashImpl() const;
1110 } // end namespace llvm