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 struct CodeGenRegister;
21 class CodeGenDAGPatterns;
28 class TreePredicateFn;
31 Matcher *ConvertPatternToMatcher(const PatternToMatch &Pattern,unsigned Variant,
32 const CodeGenDAGPatterns &CGP);
33 Matcher *OptimizeMatcher(Matcher *Matcher, const CodeGenDAGPatterns &CGP);
34 void EmitMatcherTable(const Matcher *Matcher, const CodeGenDAGPatterns &CGP,
38 /// Matcher - Base class for all the the DAG ISel Matcher representation
41 // The next matcher node that is executed after this one. Null if this is the
42 // last stage of a match.
43 OwningPtr<Matcher> Next;
44 virtual void anchor();
47 // Matcher state manipulation.
48 Scope, // Push a checking scope.
49 RecordNode, // Record the current node.
50 RecordChild, // Record a child of the current node.
51 RecordMemRef, // Record the memref in the current node.
52 CaptureGlueInput, // If the current node has an input glue, save it.
53 MoveChild, // Move current node to specified child.
54 MoveParent, // Move current node to parent.
56 // Predicate checking.
57 CheckSame, // Fail if not same as prev match.
58 CheckPatternPredicate,
59 CheckPredicate, // Fail if node predicate fails.
60 CheckOpcode, // Fail if not opcode.
61 SwitchOpcode, // Dispatch based on opcode.
62 CheckType, // Fail if not correct type.
63 SwitchType, // Dispatch based on type.
64 CheckChildType, // Fail if child has wrong type.
65 CheckInteger, // Fail if wrong val.
66 CheckCondCode, // Fail if not condcode.
71 CheckFoldableChainNode,
73 // Node creation/emisssion.
74 EmitInteger, // Create a TargetConstant
75 EmitStringInteger, // Create a TargetConstant from a string.
76 EmitRegister, // Create a register.
77 EmitConvertToTarget, // Convert a imm/fpimm to target imm/fpimm
78 EmitMergeInputChains, // Merge together a chains for an input.
79 EmitCopyToReg, // Emit a copytoreg into a physreg.
80 EmitNode, // Create a DAG node
81 EmitNodeXForm, // Run a SDNodeXForm
82 MarkGlueResults, // Indicate which interior nodes have glue results.
83 CompleteMatch, // Finish a match and update the results.
84 MorphNodeTo // Build a node, finish a match and update results.
89 Matcher(KindTy K) : Kind(K) {}
93 KindTy getKind() const { return Kind; }
95 Matcher *getNext() { return Next.get(); }
96 const Matcher *getNext() const { return Next.get(); }
97 void setNext(Matcher *C) { Next.reset(C); }
98 Matcher *takeNext() { return Next.take(); }
100 OwningPtr<Matcher> &getNextPtr() { return Next; }
102 static inline bool classof(const Matcher *) { return true; }
104 bool isEqual(const Matcher *M) const {
105 if (getKind() != M->getKind()) return false;
106 return isEqualImpl(M);
109 unsigned getHash() const {
110 // Clear the high bit so we don't conflict with tombstones etc.
111 return ((getHashImpl() << 4) ^ getKind()) & (~0U>>1);
114 /// isSafeToReorderWithPatternPredicate - Return true if it is safe to sink a
115 /// PatternPredicate node past this one.
116 virtual bool isSafeToReorderWithPatternPredicate() const {
120 /// isSimplePredicateNode - Return true if this is a simple predicate that
121 /// operates on the node or its children without potential side effects or a
122 /// change of the current node.
123 bool isSimplePredicateNode() const {
125 default: return false;
127 case CheckPatternPredicate:
137 case CheckFoldableChainNode:
142 /// isSimplePredicateOrRecordNode - Return true if this is a record node or
143 /// a simple predicate.
144 bool isSimplePredicateOrRecordNode() const {
145 return isSimplePredicateNode() ||
146 getKind() == RecordNode || getKind() == RecordChild;
149 /// unlinkNode - Unlink the specified node from this chain. If Other == this,
150 /// we unlink the next pointer and return it. Otherwise we unlink Other from
151 /// the list and return this.
152 Matcher *unlinkNode(Matcher *Other);
154 /// canMoveBefore - Return true if this matcher is the same as Other, or if
155 /// we can move this matcher past all of the nodes in-between Other and this
156 /// node. Other must be equal to or before this.
157 bool canMoveBefore(const Matcher *Other) const;
159 /// canMoveBefore - Return true if it is safe to move the current matcher
160 /// across the specified one.
161 bool canMoveBeforeNode(const Matcher *Other) const;
163 /// isContradictory - Return true of these two matchers could never match on
165 bool isContradictory(const Matcher *Other) const {
166 // Since this predicate is reflexive, we canonicalize the ordering so that
167 // we always match a node against nodes with kinds that are greater or equal
168 // to them. For example, we'll pass in a CheckType node as an argument to
169 // the CheckOpcode method, not the other way around.
170 if (getKind() < Other->getKind())
171 return isContradictoryImpl(Other);
172 return Other->isContradictoryImpl(this);
175 void print(raw_ostream &OS, unsigned indent = 0) const;
176 void printOne(raw_ostream &OS) const;
179 virtual void printImpl(raw_ostream &OS, unsigned indent) const = 0;
180 virtual bool isEqualImpl(const Matcher *M) const = 0;
181 virtual unsigned getHashImpl() const = 0;
182 virtual bool isContradictoryImpl(const Matcher *M) const { return false; }
185 /// ScopeMatcher - This attempts to match each of its children to find the first
186 /// one that successfully matches. If one child fails, it tries the next child.
187 /// If none of the children match then this check fails. It never has a 'next'.
188 class ScopeMatcher : public Matcher {
189 SmallVector<Matcher*, 4> Children;
191 ScopeMatcher(Matcher *const *children, unsigned numchildren)
192 : Matcher(Scope), Children(children, children+numchildren) {
194 virtual ~ScopeMatcher();
196 unsigned getNumChildren() const { return Children.size(); }
198 Matcher *getChild(unsigned i) { return Children[i]; }
199 const Matcher *getChild(unsigned i) const { return Children[i]; }
201 void resetChild(unsigned i, Matcher *N) {
206 Matcher *takeChild(unsigned i) {
207 Matcher *Res = Children[i];
212 void setNumChildren(unsigned NC) {
213 if (NC < Children.size()) {
214 // delete any children we're about to lose pointers to.
215 for (unsigned i = NC, e = Children.size(); i != e; ++i)
221 static inline bool classof(const Matcher *N) {
222 return N->getKind() == Scope;
226 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
227 virtual bool isEqualImpl(const Matcher *M) const { return false; }
228 virtual unsigned getHashImpl() const { return 12312; }
231 /// RecordMatcher - Save the current node in the operand list.
232 class RecordMatcher : public Matcher {
233 /// WhatFor - This is a string indicating why we're recording this. This
234 /// should only be used for comment generation not anything semantic.
237 /// ResultNo - The slot number in the RecordedNodes vector that this will be,
238 /// just printed as a comment.
241 RecordMatcher(const std::string &whatfor, unsigned resultNo)
242 : Matcher(RecordNode), WhatFor(whatfor), ResultNo(resultNo) {}
244 const std::string &getWhatFor() const { return WhatFor; }
245 unsigned getResultNo() const { return ResultNo; }
247 static inline bool classof(const Matcher *N) {
248 return N->getKind() == RecordNode;
251 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
253 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
254 virtual bool isEqualImpl(const Matcher *M) const { return true; }
255 virtual unsigned getHashImpl() const { return 0; }
258 /// RecordChildMatcher - Save a numbered child of the current node, or fail
259 /// the match if it doesn't exist. This is logically equivalent to:
260 /// MoveChild N + RecordNode + MoveParent.
261 class RecordChildMatcher : public Matcher {
264 /// WhatFor - This is a string indicating why we're recording this. This
265 /// should only be used for comment generation not anything semantic.
268 /// ResultNo - The slot number in the RecordedNodes vector that this will be,
269 /// just printed as a comment.
272 RecordChildMatcher(unsigned childno, const std::string &whatfor,
274 : Matcher(RecordChild), ChildNo(childno), WhatFor(whatfor),
275 ResultNo(resultNo) {}
277 unsigned getChildNo() const { return ChildNo; }
278 const std::string &getWhatFor() const { return WhatFor; }
279 unsigned getResultNo() const { return ResultNo; }
281 static inline bool classof(const Matcher *N) {
282 return N->getKind() == RecordChild;
285 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
288 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
289 virtual bool isEqualImpl(const Matcher *M) const {
290 return cast<RecordChildMatcher>(M)->getChildNo() == getChildNo();
292 virtual unsigned getHashImpl() const { return getChildNo(); }
295 /// RecordMemRefMatcher - Save the current node's memref.
296 class RecordMemRefMatcher : public Matcher {
298 RecordMemRefMatcher() : Matcher(RecordMemRef) {}
300 static inline bool classof(const Matcher *N) {
301 return N->getKind() == RecordMemRef;
304 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
307 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
308 virtual bool isEqualImpl(const Matcher *M) const { return true; }
309 virtual unsigned getHashImpl() const { return 0; }
313 /// CaptureGlueInputMatcher - If the current record has a glue input, record
314 /// it so that it is used as an input to the generated code.
315 class CaptureGlueInputMatcher : public Matcher {
317 CaptureGlueInputMatcher() : Matcher(CaptureGlueInput) {}
319 static inline bool classof(const Matcher *N) {
320 return N->getKind() == CaptureGlueInput;
323 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
326 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
327 virtual bool isEqualImpl(const Matcher *M) const { return true; }
328 virtual unsigned getHashImpl() const { return 0; }
331 /// MoveChildMatcher - This tells the interpreter to move into the
332 /// specified child node.
333 class MoveChildMatcher : public Matcher {
336 MoveChildMatcher(unsigned childNo) : Matcher(MoveChild), ChildNo(childNo) {}
338 unsigned getChildNo() const { return ChildNo; }
340 static inline bool classof(const Matcher *N) {
341 return N->getKind() == MoveChild;
344 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
347 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
348 virtual bool isEqualImpl(const Matcher *M) const {
349 return cast<MoveChildMatcher>(M)->getChildNo() == getChildNo();
351 virtual unsigned getHashImpl() const { return getChildNo(); }
354 /// MoveParentMatcher - This tells the interpreter to move to the parent
355 /// of the current node.
356 class MoveParentMatcher : public Matcher {
358 MoveParentMatcher() : Matcher(MoveParent) {}
360 static inline bool classof(const Matcher *N) {
361 return N->getKind() == MoveParent;
364 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
367 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
368 virtual bool isEqualImpl(const Matcher *M) const { return true; }
369 virtual unsigned getHashImpl() const { return 0; }
372 /// CheckSameMatcher - This checks to see if this node is exactly the same
373 /// node as the specified match that was recorded with 'Record'. This is used
374 /// when patterns have the same name in them, like '(mul GPR:$in, GPR:$in)'.
375 class CheckSameMatcher : public Matcher {
376 unsigned MatchNumber;
378 CheckSameMatcher(unsigned matchnumber)
379 : Matcher(CheckSame), MatchNumber(matchnumber) {}
381 unsigned getMatchNumber() const { return MatchNumber; }
383 static inline bool classof(const Matcher *N) {
384 return N->getKind() == CheckSame;
387 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
390 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
391 virtual bool isEqualImpl(const Matcher *M) const {
392 return cast<CheckSameMatcher>(M)->getMatchNumber() == getMatchNumber();
394 virtual unsigned getHashImpl() const { return getMatchNumber(); }
397 /// CheckPatternPredicateMatcher - This checks the target-specific predicate
398 /// to see if the entire pattern is capable of matching. This predicate does
399 /// not take a node as input. This is used for subtarget feature checks etc.
400 class CheckPatternPredicateMatcher : public Matcher {
401 std::string Predicate;
403 CheckPatternPredicateMatcher(StringRef predicate)
404 : Matcher(CheckPatternPredicate), Predicate(predicate) {}
406 StringRef getPredicate() const { return Predicate; }
408 static inline bool classof(const Matcher *N) {
409 return N->getKind() == CheckPatternPredicate;
412 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
415 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
416 virtual bool isEqualImpl(const Matcher *M) const {
417 return cast<CheckPatternPredicateMatcher>(M)->getPredicate() == Predicate;
419 virtual unsigned getHashImpl() const;
422 /// CheckPredicateMatcher - This checks the target-specific predicate to
423 /// see if the node is acceptable.
424 class CheckPredicateMatcher : public Matcher {
427 CheckPredicateMatcher(const TreePredicateFn &pred);
429 TreePredicateFn getPredicate() const;
431 static inline bool classof(const Matcher *N) {
432 return N->getKind() == CheckPredicate;
436 //virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
439 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
440 virtual bool isEqualImpl(const Matcher *M) const {
441 return cast<CheckPredicateMatcher>(M)->Pred == Pred;
443 virtual unsigned getHashImpl() const;
447 /// CheckOpcodeMatcher - This checks to see if the current node has the
448 /// specified opcode, if not it fails to match.
449 class CheckOpcodeMatcher : public Matcher {
450 const SDNodeInfo &Opcode;
452 CheckOpcodeMatcher(const SDNodeInfo &opcode)
453 : Matcher(CheckOpcode), Opcode(opcode) {}
455 const SDNodeInfo &getOpcode() const { return Opcode; }
457 static inline bool classof(const Matcher *N) {
458 return N->getKind() == CheckOpcode;
461 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
464 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
465 virtual bool isEqualImpl(const Matcher *M) const;
466 virtual unsigned getHashImpl() const;
467 virtual bool isContradictoryImpl(const Matcher *M) const;
470 /// SwitchOpcodeMatcher - Switch based on the current node's opcode, dispatching
471 /// to one matcher per opcode. If the opcode doesn't match any of the cases,
472 /// then the match fails. This is semantically equivalent to a Scope node where
473 /// every child does a CheckOpcode, but is much faster.
474 class SwitchOpcodeMatcher : public Matcher {
475 SmallVector<std::pair<const SDNodeInfo*, Matcher*>, 8> Cases;
477 SwitchOpcodeMatcher(const std::pair<const SDNodeInfo*, Matcher*> *cases,
479 : Matcher(SwitchOpcode), Cases(cases, cases+numcases) {}
481 static inline bool classof(const Matcher *N) {
482 return N->getKind() == SwitchOpcode;
485 unsigned getNumCases() const { return Cases.size(); }
487 const SDNodeInfo &getCaseOpcode(unsigned i) const { return *Cases[i].first; }
488 Matcher *getCaseMatcher(unsigned i) { return Cases[i].second; }
489 const Matcher *getCaseMatcher(unsigned i) const { return Cases[i].second; }
492 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
493 virtual bool isEqualImpl(const Matcher *M) const { return false; }
494 virtual unsigned getHashImpl() const { return 4123; }
497 /// CheckTypeMatcher - This checks to see if the current node has the
498 /// specified type at the specified result, if not it fails to match.
499 class CheckTypeMatcher : public Matcher {
500 MVT::SimpleValueType Type;
503 CheckTypeMatcher(MVT::SimpleValueType type, unsigned resno)
504 : Matcher(CheckType), Type(type), ResNo(resno) {}
506 MVT::SimpleValueType getType() const { return Type; }
507 unsigned getResNo() const { return ResNo; }
509 static inline bool classof(const Matcher *N) {
510 return N->getKind() == CheckType;
513 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
516 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
517 virtual bool isEqualImpl(const Matcher *M) const {
518 return cast<CheckTypeMatcher>(M)->Type == Type;
520 virtual unsigned getHashImpl() const { return Type; }
521 virtual bool isContradictoryImpl(const Matcher *M) const;
524 /// SwitchTypeMatcher - Switch based on the current node's type, dispatching
525 /// to one matcher per case. If the type doesn't match any of the cases,
526 /// then the match fails. This is semantically equivalent to a Scope node where
527 /// every child does a CheckType, but is much faster.
528 class SwitchTypeMatcher : public Matcher {
529 SmallVector<std::pair<MVT::SimpleValueType, Matcher*>, 8> Cases;
531 SwitchTypeMatcher(const std::pair<MVT::SimpleValueType, Matcher*> *cases,
533 : Matcher(SwitchType), Cases(cases, cases+numcases) {}
535 static inline bool classof(const Matcher *N) {
536 return N->getKind() == SwitchType;
539 unsigned getNumCases() const { return Cases.size(); }
541 MVT::SimpleValueType getCaseType(unsigned i) const { return Cases[i].first; }
542 Matcher *getCaseMatcher(unsigned i) { return Cases[i].second; }
543 const Matcher *getCaseMatcher(unsigned i) const { return Cases[i].second; }
546 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
547 virtual bool isEqualImpl(const Matcher *M) const { return false; }
548 virtual unsigned getHashImpl() const { return 4123; }
552 /// CheckChildTypeMatcher - This checks to see if a child node has the
553 /// specified type, if not it fails to match.
554 class CheckChildTypeMatcher : public Matcher {
556 MVT::SimpleValueType Type;
558 CheckChildTypeMatcher(unsigned childno, MVT::SimpleValueType type)
559 : Matcher(CheckChildType), ChildNo(childno), Type(type) {}
561 unsigned getChildNo() const { return ChildNo; }
562 MVT::SimpleValueType getType() const { return Type; }
564 static inline bool classof(const Matcher *N) {
565 return N->getKind() == CheckChildType;
568 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
571 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
572 virtual bool isEqualImpl(const Matcher *M) const {
573 return cast<CheckChildTypeMatcher>(M)->ChildNo == ChildNo &&
574 cast<CheckChildTypeMatcher>(M)->Type == Type;
576 virtual unsigned getHashImpl() const { return (Type << 3) | ChildNo; }
577 virtual bool isContradictoryImpl(const Matcher *M) const;
581 /// CheckIntegerMatcher - This checks to see if the current node is a
582 /// ConstantSDNode with the specified integer value, if not it fails to match.
583 class CheckIntegerMatcher : public Matcher {
586 CheckIntegerMatcher(int64_t value)
587 : Matcher(CheckInteger), Value(value) {}
589 int64_t getValue() const { return Value; }
591 static inline bool classof(const Matcher *N) {
592 return N->getKind() == CheckInteger;
595 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
598 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
599 virtual bool isEqualImpl(const Matcher *M) const {
600 return cast<CheckIntegerMatcher>(M)->Value == Value;
602 virtual unsigned getHashImpl() const { return Value; }
603 virtual bool isContradictoryImpl(const Matcher *M) const;
606 /// CheckCondCodeMatcher - This checks to see if the current node is a
607 /// CondCodeSDNode with the specified condition, if not it fails to match.
608 class CheckCondCodeMatcher : public Matcher {
609 StringRef CondCodeName;
611 CheckCondCodeMatcher(StringRef condcodename)
612 : Matcher(CheckCondCode), CondCodeName(condcodename) {}
614 StringRef getCondCodeName() const { return CondCodeName; }
616 static inline bool classof(const Matcher *N) {
617 return N->getKind() == CheckCondCode;
620 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
623 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
624 virtual bool isEqualImpl(const Matcher *M) const {
625 return cast<CheckCondCodeMatcher>(M)->CondCodeName == CondCodeName;
627 virtual unsigned getHashImpl() const;
630 /// CheckValueTypeMatcher - This checks to see if the current node is a
631 /// VTSDNode with the specified type, if not it fails to match.
632 class CheckValueTypeMatcher : public Matcher {
635 CheckValueTypeMatcher(StringRef type_name)
636 : Matcher(CheckValueType), TypeName(type_name) {}
638 StringRef getTypeName() const { return TypeName; }
640 static inline bool classof(const Matcher *N) {
641 return N->getKind() == CheckValueType;
644 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
647 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
648 virtual bool isEqualImpl(const Matcher *M) const {
649 return cast<CheckValueTypeMatcher>(M)->TypeName == TypeName;
651 virtual unsigned getHashImpl() const;
652 bool isContradictoryImpl(const Matcher *M) const;
657 /// CheckComplexPatMatcher - This node runs the specified ComplexPattern on
658 /// the current node.
659 class CheckComplexPatMatcher : public Matcher {
660 const ComplexPattern &Pattern;
662 /// MatchNumber - This is the recorded nodes slot that contains the node we
663 /// want to match against.
664 unsigned MatchNumber;
666 /// Name - The name of the node we're matching, for comment emission.
669 /// FirstResult - This is the first slot in the RecordedNodes list that the
670 /// result of the match populates.
671 unsigned FirstResult;
673 CheckComplexPatMatcher(const ComplexPattern &pattern, unsigned matchnumber,
674 const std::string &name, unsigned firstresult)
675 : Matcher(CheckComplexPat), Pattern(pattern), MatchNumber(matchnumber),
676 Name(name), FirstResult(firstresult) {}
678 const ComplexPattern &getPattern() const { return Pattern; }
679 unsigned getMatchNumber() const { return MatchNumber; }
681 const std::string getName() const { return Name; }
682 unsigned getFirstResult() const { return FirstResult; }
684 static inline bool classof(const Matcher *N) {
685 return N->getKind() == CheckComplexPat;
688 // Not safe to move a pattern predicate past a complex pattern.
689 virtual bool isSafeToReorderWithPatternPredicate() const { return false; }
692 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
693 virtual bool isEqualImpl(const Matcher *M) const {
694 return &cast<CheckComplexPatMatcher>(M)->Pattern == &Pattern &&
695 cast<CheckComplexPatMatcher>(M)->MatchNumber == MatchNumber;
697 virtual unsigned getHashImpl() const {
698 return (unsigned)(intptr_t)&Pattern ^ MatchNumber;
702 /// CheckAndImmMatcher - This checks to see if the current node is an 'and'
703 /// with something equivalent to the specified immediate.
704 class CheckAndImmMatcher : public Matcher {
707 CheckAndImmMatcher(int64_t value)
708 : Matcher(CheckAndImm), Value(value) {}
710 int64_t getValue() const { return Value; }
712 static inline bool classof(const Matcher *N) {
713 return N->getKind() == CheckAndImm;
716 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
719 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
720 virtual bool isEqualImpl(const Matcher *M) const {
721 return cast<CheckAndImmMatcher>(M)->Value == Value;
723 virtual unsigned getHashImpl() const { return Value; }
726 /// CheckOrImmMatcher - This checks to see if the current node is an 'and'
727 /// with something equivalent to the specified immediate.
728 class CheckOrImmMatcher : public Matcher {
731 CheckOrImmMatcher(int64_t value)
732 : Matcher(CheckOrImm), Value(value) {}
734 int64_t getValue() const { return Value; }
736 static inline bool classof(const Matcher *N) {
737 return N->getKind() == CheckOrImm;
740 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
743 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
744 virtual bool isEqualImpl(const Matcher *M) const {
745 return cast<CheckOrImmMatcher>(M)->Value == Value;
747 virtual unsigned getHashImpl() const { return Value; }
750 /// CheckFoldableChainNodeMatcher - This checks to see if the current node
751 /// (which defines a chain operand) is safe to fold into a larger pattern.
752 class CheckFoldableChainNodeMatcher : public Matcher {
754 CheckFoldableChainNodeMatcher()
755 : Matcher(CheckFoldableChainNode) {}
757 static inline bool classof(const Matcher *N) {
758 return N->getKind() == CheckFoldableChainNode;
761 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
764 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
765 virtual bool isEqualImpl(const Matcher *M) const { return true; }
766 virtual unsigned getHashImpl() const { return 0; }
769 /// EmitIntegerMatcher - This creates a new TargetConstant.
770 class EmitIntegerMatcher : public Matcher {
772 MVT::SimpleValueType VT;
774 EmitIntegerMatcher(int64_t val, MVT::SimpleValueType vt)
775 : Matcher(EmitInteger), Val(val), VT(vt) {}
777 int64_t getValue() const { return Val; }
778 MVT::SimpleValueType getVT() const { return VT; }
780 static inline bool classof(const Matcher *N) {
781 return N->getKind() == EmitInteger;
785 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
786 virtual bool isEqualImpl(const Matcher *M) const {
787 return cast<EmitIntegerMatcher>(M)->Val == Val &&
788 cast<EmitIntegerMatcher>(M)->VT == VT;
790 virtual unsigned getHashImpl() const { return (Val << 4) | VT; }
793 /// EmitStringIntegerMatcher - A target constant whose value is represented
795 class EmitStringIntegerMatcher : public Matcher {
797 MVT::SimpleValueType VT;
799 EmitStringIntegerMatcher(const std::string &val, MVT::SimpleValueType vt)
800 : Matcher(EmitStringInteger), Val(val), VT(vt) {}
802 const std::string &getValue() const { return Val; }
803 MVT::SimpleValueType getVT() const { return VT; }
805 static inline bool classof(const Matcher *N) {
806 return N->getKind() == EmitStringInteger;
810 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
811 virtual bool isEqualImpl(const Matcher *M) const {
812 return cast<EmitStringIntegerMatcher>(M)->Val == Val &&
813 cast<EmitStringIntegerMatcher>(M)->VT == VT;
815 virtual unsigned getHashImpl() const;
818 /// EmitRegisterMatcher - This creates a new TargetConstant.
819 class EmitRegisterMatcher : public Matcher {
820 /// Reg - The def for the register that we're emitting. If this is null, then
821 /// this is a reference to zero_reg.
822 const CodeGenRegister *Reg;
823 MVT::SimpleValueType VT;
825 EmitRegisterMatcher(const CodeGenRegister *reg, MVT::SimpleValueType vt)
826 : Matcher(EmitRegister), Reg(reg), VT(vt) {}
828 const CodeGenRegister *getReg() const { return Reg; }
829 MVT::SimpleValueType getVT() const { return VT; }
831 static inline bool classof(const Matcher *N) {
832 return N->getKind() == EmitRegister;
836 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
837 virtual bool isEqualImpl(const Matcher *M) const {
838 return cast<EmitRegisterMatcher>(M)->Reg == Reg &&
839 cast<EmitRegisterMatcher>(M)->VT == VT;
841 virtual unsigned getHashImpl() const {
842 return ((unsigned)(intptr_t)Reg) << 4 | VT;
846 /// EmitConvertToTargetMatcher - Emit an operation that reads a specified
847 /// recorded node and converts it from being a ISD::Constant to
848 /// ISD::TargetConstant, likewise for ConstantFP.
849 class EmitConvertToTargetMatcher : public Matcher {
852 EmitConvertToTargetMatcher(unsigned slot)
853 : Matcher(EmitConvertToTarget), Slot(slot) {}
855 unsigned getSlot() const { return Slot; }
857 static inline bool classof(const Matcher *N) {
858 return N->getKind() == EmitConvertToTarget;
862 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
863 virtual bool isEqualImpl(const Matcher *M) const {
864 return cast<EmitConvertToTargetMatcher>(M)->Slot == Slot;
866 virtual unsigned getHashImpl() const { return Slot; }
869 /// EmitMergeInputChainsMatcher - Emit a node that merges a list of input
870 /// chains together with a token factor. The list of nodes are the nodes in the
871 /// matched pattern that have chain input/outputs. This node adds all input
872 /// chains of these nodes if they are not themselves a node in the pattern.
873 class EmitMergeInputChainsMatcher : public Matcher {
874 SmallVector<unsigned, 3> ChainNodes;
876 EmitMergeInputChainsMatcher(const unsigned *nodes, unsigned NumNodes)
877 : Matcher(EmitMergeInputChains), ChainNodes(nodes, nodes+NumNodes) {}
879 unsigned getNumNodes() const { return ChainNodes.size(); }
881 unsigned getNode(unsigned i) const {
882 assert(i < ChainNodes.size());
883 return ChainNodes[i];
886 static inline bool classof(const Matcher *N) {
887 return N->getKind() == EmitMergeInputChains;
891 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
892 virtual bool isEqualImpl(const Matcher *M) const {
893 return cast<EmitMergeInputChainsMatcher>(M)->ChainNodes == ChainNodes;
895 virtual unsigned getHashImpl() const;
898 /// EmitCopyToRegMatcher - Emit a CopyToReg node from a value to a physreg,
899 /// pushing the chain and glue results.
901 class EmitCopyToRegMatcher : public Matcher {
902 unsigned SrcSlot; // Value to copy into the physreg.
905 EmitCopyToRegMatcher(unsigned srcSlot, Record *destPhysReg)
906 : Matcher(EmitCopyToReg), SrcSlot(srcSlot), DestPhysReg(destPhysReg) {}
908 unsigned getSrcSlot() const { return SrcSlot; }
909 Record *getDestPhysReg() const { return DestPhysReg; }
911 static inline bool classof(const Matcher *N) {
912 return N->getKind() == EmitCopyToReg;
916 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
917 virtual bool isEqualImpl(const Matcher *M) const {
918 return cast<EmitCopyToRegMatcher>(M)->SrcSlot == SrcSlot &&
919 cast<EmitCopyToRegMatcher>(M)->DestPhysReg == DestPhysReg;
921 virtual unsigned getHashImpl() const {
922 return SrcSlot ^ ((unsigned)(intptr_t)DestPhysReg << 4);
928 /// EmitNodeXFormMatcher - Emit an operation that runs an SDNodeXForm on a
929 /// recorded node and records the result.
930 class EmitNodeXFormMatcher : public Matcher {
934 EmitNodeXFormMatcher(unsigned slot, Record *nodeXForm)
935 : Matcher(EmitNodeXForm), Slot(slot), NodeXForm(nodeXForm) {}
937 unsigned getSlot() const { return Slot; }
938 Record *getNodeXForm() const { return NodeXForm; }
940 static inline bool classof(const Matcher *N) {
941 return N->getKind() == EmitNodeXForm;
945 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
946 virtual bool isEqualImpl(const Matcher *M) const {
947 return cast<EmitNodeXFormMatcher>(M)->Slot == Slot &&
948 cast<EmitNodeXFormMatcher>(M)->NodeXForm == NodeXForm;
950 virtual unsigned getHashImpl() const {
951 return Slot ^ ((unsigned)(intptr_t)NodeXForm << 4);
955 /// EmitNodeMatcherCommon - Common class shared between EmitNode and
957 class EmitNodeMatcherCommon : public Matcher {
958 std::string OpcodeName;
959 const SmallVector<MVT::SimpleValueType, 3> VTs;
960 const SmallVector<unsigned, 6> Operands;
961 bool HasChain, HasInGlue, HasOutGlue, HasMemRefs;
963 /// NumFixedArityOperands - If this is a fixed arity node, this is set to -1.
964 /// If this is a varidic node, this is set to the number of fixed arity
965 /// operands in the root of the pattern. The rest are appended to this node.
966 int NumFixedArityOperands;
968 EmitNodeMatcherCommon(const std::string &opcodeName,
969 const MVT::SimpleValueType *vts, unsigned numvts,
970 const unsigned *operands, unsigned numops,
971 bool hasChain, bool hasInGlue, bool hasOutGlue,
973 int numfixedarityoperands, bool isMorphNodeTo)
974 : Matcher(isMorphNodeTo ? MorphNodeTo : EmitNode), OpcodeName(opcodeName),
975 VTs(vts, vts+numvts), Operands(operands, operands+numops),
976 HasChain(hasChain), HasInGlue(hasInGlue), HasOutGlue(hasOutGlue),
977 HasMemRefs(hasmemrefs), NumFixedArityOperands(numfixedarityoperands) {}
979 const std::string &getOpcodeName() const { return OpcodeName; }
981 unsigned getNumVTs() const { return VTs.size(); }
982 MVT::SimpleValueType getVT(unsigned i) const {
983 assert(i < VTs.size());
987 unsigned getNumOperands() const { return Operands.size(); }
988 unsigned getOperand(unsigned i) const {
989 assert(i < Operands.size());
993 const SmallVectorImpl<MVT::SimpleValueType> &getVTList() const { return VTs; }
994 const SmallVectorImpl<unsigned> &getOperandList() const { return Operands; }
997 bool hasChain() const { return HasChain; }
998 bool hasInFlag() const { return HasInGlue; }
999 bool hasOutFlag() const { return HasOutGlue; }
1000 bool hasMemRefs() const { return HasMemRefs; }
1001 int getNumFixedArityOperands() const { return NumFixedArityOperands; }
1003 static inline bool classof(const Matcher *N) {
1004 return N->getKind() == EmitNode || N->getKind() == MorphNodeTo;
1008 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
1009 virtual bool isEqualImpl(const Matcher *M) const;
1010 virtual unsigned getHashImpl() const;
1013 /// EmitNodeMatcher - This signals a successful match and generates a node.
1014 class EmitNodeMatcher : public EmitNodeMatcherCommon {
1015 virtual void anchor();
1016 unsigned FirstResultSlot;
1018 EmitNodeMatcher(const std::string &opcodeName,
1019 const MVT::SimpleValueType *vts, unsigned numvts,
1020 const unsigned *operands, unsigned numops,
1021 bool hasChain, bool hasInFlag, bool hasOutFlag,
1023 int numfixedarityoperands, unsigned firstresultslot)
1024 : EmitNodeMatcherCommon(opcodeName, vts, numvts, operands, numops, hasChain,
1025 hasInFlag, hasOutFlag, hasmemrefs,
1026 numfixedarityoperands, false),
1027 FirstResultSlot(firstresultslot) {}
1029 unsigned getFirstResultSlot() const { return FirstResultSlot; }
1031 static inline bool classof(const Matcher *N) {
1032 return N->getKind() == EmitNode;
1037 class MorphNodeToMatcher : public EmitNodeMatcherCommon {
1038 virtual void anchor();
1039 const PatternToMatch &Pattern;
1041 MorphNodeToMatcher(const std::string &opcodeName,
1042 const MVT::SimpleValueType *vts, unsigned numvts,
1043 const unsigned *operands, unsigned numops,
1044 bool hasChain, bool hasInFlag, bool hasOutFlag,
1046 int numfixedarityoperands, const PatternToMatch &pattern)
1047 : EmitNodeMatcherCommon(opcodeName, vts, numvts, operands, numops, hasChain,
1048 hasInFlag, hasOutFlag, hasmemrefs,
1049 numfixedarityoperands, true),
1053 const PatternToMatch &getPattern() const { return Pattern; }
1055 static inline bool classof(const Matcher *N) {
1056 return N->getKind() == MorphNodeTo;
1060 /// MarkGlueResultsMatcher - This node indicates which non-root nodes in the
1061 /// pattern produce glue. This allows CompleteMatchMatcher to update them
1062 /// with the output glue of the resultant code.
1063 class MarkGlueResultsMatcher : public Matcher {
1064 SmallVector<unsigned, 3> GlueResultNodes;
1066 MarkGlueResultsMatcher(const unsigned *nodes, unsigned NumNodes)
1067 : Matcher(MarkGlueResults), GlueResultNodes(nodes, nodes+NumNodes) {}
1069 unsigned getNumNodes() const { return GlueResultNodes.size(); }
1071 unsigned getNode(unsigned i) const {
1072 assert(i < GlueResultNodes.size());
1073 return GlueResultNodes[i];
1076 static inline bool classof(const Matcher *N) {
1077 return N->getKind() == MarkGlueResults;
1081 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
1082 virtual bool isEqualImpl(const Matcher *M) const {
1083 return cast<MarkGlueResultsMatcher>(M)->GlueResultNodes == GlueResultNodes;
1085 virtual unsigned getHashImpl() const;
1088 /// CompleteMatchMatcher - Complete a match by replacing the results of the
1089 /// pattern with the newly generated nodes. This also prints a comment
1090 /// indicating the source and dest patterns.
1091 class CompleteMatchMatcher : public Matcher {
1092 SmallVector<unsigned, 2> Results;
1093 const PatternToMatch &Pattern;
1095 CompleteMatchMatcher(const unsigned *results, unsigned numresults,
1096 const PatternToMatch &pattern)
1097 : Matcher(CompleteMatch), Results(results, results+numresults),
1100 unsigned getNumResults() const { return Results.size(); }
1101 unsigned getResult(unsigned R) const { return Results[R]; }
1102 const PatternToMatch &getPattern() const { return Pattern; }
1104 static inline bool classof(const Matcher *N) {
1105 return N->getKind() == CompleteMatch;
1109 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
1110 virtual bool isEqualImpl(const Matcher *M) const {
1111 return cast<CompleteMatchMatcher>(M)->Results == Results &&
1112 &cast<CompleteMatchMatcher>(M)->Pattern == &Pattern;
1114 virtual unsigned getHashImpl() const;
1117 } // end namespace llvm