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/ADT/OwningPtr.h"
14 #include "llvm/ADT/SmallVector.h"
15 #include "llvm/ADT/StringRef.h"
16 #include "llvm/CodeGen/ValueTypes.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 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 bool isEqual(const Matcher *M) const {
103 if (getKind() != M->getKind()) return false;
104 return isEqualImpl(M);
107 unsigned getHash() const {
108 // Clear the high bit so we don't conflict with tombstones etc.
109 return ((getHashImpl() << 4) ^ getKind()) & (~0U>>1);
112 /// isSafeToReorderWithPatternPredicate - Return true if it is safe to sink a
113 /// PatternPredicate node past this one.
114 virtual bool isSafeToReorderWithPatternPredicate() const {
118 /// isSimplePredicateNode - Return true if this is a simple predicate that
119 /// operates on the node or its children without potential side effects or a
120 /// change of the current node.
121 bool isSimplePredicateNode() const {
123 default: return false;
125 case CheckPatternPredicate:
135 case CheckFoldableChainNode:
140 /// isSimplePredicateOrRecordNode - Return true if this is a record node or
141 /// a simple predicate.
142 bool isSimplePredicateOrRecordNode() const {
143 return isSimplePredicateNode() ||
144 getKind() == RecordNode || getKind() == RecordChild;
147 /// unlinkNode - Unlink the specified node from this chain. If Other == this,
148 /// we unlink the next pointer and return it. Otherwise we unlink Other from
149 /// the list and return this.
150 Matcher *unlinkNode(Matcher *Other);
152 /// canMoveBefore - Return true if this matcher is the same as Other, or if
153 /// we can move this matcher past all of the nodes in-between Other and this
154 /// node. Other must be equal to or before this.
155 bool canMoveBefore(const Matcher *Other) const;
157 /// canMoveBeforeNode - Return true if it is safe to move the current matcher
158 /// across the specified one.
159 bool canMoveBeforeNode(const Matcher *Other) const;
161 /// isContradictory - Return true of these two matchers could never match on
163 bool isContradictory(const Matcher *Other) const {
164 // Since this predicate is reflexive, we canonicalize the ordering so that
165 // we always match a node against nodes with kinds that are greater or equal
166 // to them. For example, we'll pass in a CheckType node as an argument to
167 // the CheckOpcode method, not the other way around.
168 if (getKind() < Other->getKind())
169 return isContradictoryImpl(Other);
170 return Other->isContradictoryImpl(this);
173 void print(raw_ostream &OS, unsigned indent = 0) const;
174 void printOne(raw_ostream &OS) const;
177 virtual void printImpl(raw_ostream &OS, unsigned indent) const = 0;
178 virtual bool isEqualImpl(const Matcher *M) const = 0;
179 virtual unsigned getHashImpl() const = 0;
180 virtual bool isContradictoryImpl(const Matcher *M) const { return false; }
183 /// ScopeMatcher - This attempts to match each of its children to find the first
184 /// one that successfully matches. If one child fails, it tries the next child.
185 /// If none of the children match then this check fails. It never has a 'next'.
186 class ScopeMatcher : public Matcher {
187 SmallVector<Matcher*, 4> Children;
189 ScopeMatcher(Matcher *const *children, unsigned numchildren)
190 : Matcher(Scope), Children(children, children+numchildren) {
192 virtual ~ScopeMatcher();
194 unsigned getNumChildren() const { return Children.size(); }
196 Matcher *getChild(unsigned i) { return Children[i]; }
197 const Matcher *getChild(unsigned i) const { return Children[i]; }
199 void resetChild(unsigned i, Matcher *N) {
204 Matcher *takeChild(unsigned i) {
205 Matcher *Res = Children[i];
210 void setNumChildren(unsigned NC) {
211 if (NC < Children.size()) {
212 // delete any children we're about to lose pointers to.
213 for (unsigned i = NC, e = Children.size(); i != e; ++i)
219 static inline bool classof(const Matcher *N) {
220 return N->getKind() == Scope;
224 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
225 virtual bool isEqualImpl(const Matcher *M) const { return false; }
226 virtual unsigned getHashImpl() const { return 12312; }
229 /// RecordMatcher - Save the current node in the operand list.
230 class RecordMatcher : public Matcher {
231 /// WhatFor - This is a string indicating why we're recording this. This
232 /// should only be used for comment generation not anything semantic.
235 /// ResultNo - The slot number in the RecordedNodes vector that this will be,
236 /// just printed as a comment.
239 RecordMatcher(const std::string &whatfor, unsigned resultNo)
240 : Matcher(RecordNode), WhatFor(whatfor), ResultNo(resultNo) {}
242 const std::string &getWhatFor() const { return WhatFor; }
243 unsigned getResultNo() const { return ResultNo; }
245 static inline bool classof(const Matcher *N) {
246 return N->getKind() == RecordNode;
249 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
251 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
252 virtual bool isEqualImpl(const Matcher *M) const { return true; }
253 virtual unsigned getHashImpl() const { return 0; }
256 /// RecordChildMatcher - Save a numbered child of the current node, or fail
257 /// the match if it doesn't exist. This is logically equivalent to:
258 /// MoveChild N + RecordNode + MoveParent.
259 class RecordChildMatcher : public Matcher {
262 /// WhatFor - This is a string indicating why we're recording this. This
263 /// should only be used for comment generation not anything semantic.
266 /// ResultNo - The slot number in the RecordedNodes vector that this will be,
267 /// just printed as a comment.
270 RecordChildMatcher(unsigned childno, const std::string &whatfor,
272 : Matcher(RecordChild), ChildNo(childno), WhatFor(whatfor),
273 ResultNo(resultNo) {}
275 unsigned getChildNo() const { return ChildNo; }
276 const std::string &getWhatFor() const { return WhatFor; }
277 unsigned getResultNo() const { return ResultNo; }
279 static inline bool classof(const Matcher *N) {
280 return N->getKind() == RecordChild;
283 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
286 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
287 virtual bool isEqualImpl(const Matcher *M) const {
288 return cast<RecordChildMatcher>(M)->getChildNo() == getChildNo();
290 virtual unsigned getHashImpl() const { return getChildNo(); }
293 /// RecordMemRefMatcher - Save the current node's memref.
294 class RecordMemRefMatcher : public Matcher {
296 RecordMemRefMatcher() : Matcher(RecordMemRef) {}
298 static inline bool classof(const Matcher *N) {
299 return N->getKind() == RecordMemRef;
302 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
305 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
306 virtual bool isEqualImpl(const Matcher *M) const { return true; }
307 virtual unsigned getHashImpl() const { return 0; }
311 /// CaptureGlueInputMatcher - If the current record has a glue input, record
312 /// it so that it is used as an input to the generated code.
313 class CaptureGlueInputMatcher : public Matcher {
315 CaptureGlueInputMatcher() : Matcher(CaptureGlueInput) {}
317 static inline bool classof(const Matcher *N) {
318 return N->getKind() == CaptureGlueInput;
321 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
324 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
325 virtual bool isEqualImpl(const Matcher *M) const { return true; }
326 virtual unsigned getHashImpl() const { return 0; }
329 /// MoveChildMatcher - This tells the interpreter to move into the
330 /// specified child node.
331 class MoveChildMatcher : public Matcher {
334 MoveChildMatcher(unsigned childNo) : Matcher(MoveChild), ChildNo(childNo) {}
336 unsigned getChildNo() const { return ChildNo; }
338 static inline bool classof(const Matcher *N) {
339 return N->getKind() == MoveChild;
342 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
345 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
346 virtual bool isEqualImpl(const Matcher *M) const {
347 return cast<MoveChildMatcher>(M)->getChildNo() == getChildNo();
349 virtual unsigned getHashImpl() const { return getChildNo(); }
352 /// MoveParentMatcher - This tells the interpreter to move to the parent
353 /// of the current node.
354 class MoveParentMatcher : public Matcher {
356 MoveParentMatcher() : Matcher(MoveParent) {}
358 static inline bool classof(const Matcher *N) {
359 return N->getKind() == MoveParent;
362 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
365 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
366 virtual bool isEqualImpl(const Matcher *M) const { return true; }
367 virtual unsigned getHashImpl() const { return 0; }
370 /// CheckSameMatcher - This checks to see if this node is exactly the same
371 /// node as the specified match that was recorded with 'Record'. This is used
372 /// when patterns have the same name in them, like '(mul GPR:$in, GPR:$in)'.
373 class CheckSameMatcher : public Matcher {
374 unsigned MatchNumber;
376 CheckSameMatcher(unsigned matchnumber)
377 : Matcher(CheckSame), MatchNumber(matchnumber) {}
379 unsigned getMatchNumber() const { return MatchNumber; }
381 static inline bool classof(const Matcher *N) {
382 return N->getKind() == CheckSame;
385 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
388 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
389 virtual bool isEqualImpl(const Matcher *M) const {
390 return cast<CheckSameMatcher>(M)->getMatchNumber() == getMatchNumber();
392 virtual unsigned getHashImpl() const { return getMatchNumber(); }
395 /// CheckPatternPredicateMatcher - This checks the target-specific predicate
396 /// to see if the entire pattern is capable of matching. This predicate does
397 /// not take a node as input. This is used for subtarget feature checks etc.
398 class CheckPatternPredicateMatcher : public Matcher {
399 std::string Predicate;
401 CheckPatternPredicateMatcher(StringRef predicate)
402 : Matcher(CheckPatternPredicate), Predicate(predicate) {}
404 StringRef getPredicate() const { return Predicate; }
406 static inline bool classof(const Matcher *N) {
407 return N->getKind() == CheckPatternPredicate;
410 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
413 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
414 virtual bool isEqualImpl(const Matcher *M) const {
415 return cast<CheckPatternPredicateMatcher>(M)->getPredicate() == Predicate;
417 virtual unsigned getHashImpl() const;
420 /// CheckPredicateMatcher - This checks the target-specific predicate to
421 /// see if the node is acceptable.
422 class CheckPredicateMatcher : public Matcher {
425 CheckPredicateMatcher(const TreePredicateFn &pred);
427 TreePredicateFn getPredicate() const;
429 static inline bool classof(const Matcher *N) {
430 return N->getKind() == CheckPredicate;
434 //virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
437 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
438 virtual bool isEqualImpl(const Matcher *M) const {
439 return cast<CheckPredicateMatcher>(M)->Pred == Pred;
441 virtual unsigned getHashImpl() const;
445 /// CheckOpcodeMatcher - This checks to see if the current node has the
446 /// specified opcode, if not it fails to match.
447 class CheckOpcodeMatcher : public Matcher {
448 const SDNodeInfo &Opcode;
450 CheckOpcodeMatcher(const SDNodeInfo &opcode)
451 : Matcher(CheckOpcode), Opcode(opcode) {}
453 const SDNodeInfo &getOpcode() const { return Opcode; }
455 static inline bool classof(const Matcher *N) {
456 return N->getKind() == CheckOpcode;
459 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
462 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
463 virtual bool isEqualImpl(const Matcher *M) const;
464 virtual unsigned getHashImpl() const;
465 virtual bool isContradictoryImpl(const Matcher *M) const;
468 /// SwitchOpcodeMatcher - Switch based on the current node's opcode, dispatching
469 /// to one matcher per opcode. If the opcode doesn't match any of the cases,
470 /// then the match fails. This is semantically equivalent to a Scope node where
471 /// every child does a CheckOpcode, but is much faster.
472 class SwitchOpcodeMatcher : public Matcher {
473 SmallVector<std::pair<const SDNodeInfo*, Matcher*>, 8> Cases;
475 SwitchOpcodeMatcher(const std::pair<const SDNodeInfo*, Matcher*> *cases,
477 : Matcher(SwitchOpcode), Cases(cases, cases+numcases) {}
479 static inline bool classof(const Matcher *N) {
480 return N->getKind() == SwitchOpcode;
483 unsigned getNumCases() const { return Cases.size(); }
485 const SDNodeInfo &getCaseOpcode(unsigned i) const { return *Cases[i].first; }
486 Matcher *getCaseMatcher(unsigned i) { return Cases[i].second; }
487 const Matcher *getCaseMatcher(unsigned i) const { return Cases[i].second; }
490 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
491 virtual bool isEqualImpl(const Matcher *M) const { return false; }
492 virtual unsigned getHashImpl() const { return 4123; }
495 /// CheckTypeMatcher - This checks to see if the current node has the
496 /// specified type at the specified result, if not it fails to match.
497 class CheckTypeMatcher : public Matcher {
498 MVT::SimpleValueType Type;
501 CheckTypeMatcher(MVT::SimpleValueType type, unsigned resno)
502 : Matcher(CheckType), Type(type), ResNo(resno) {}
504 MVT::SimpleValueType getType() const { return Type; }
505 unsigned getResNo() const { return ResNo; }
507 static inline bool classof(const Matcher *N) {
508 return N->getKind() == CheckType;
511 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
514 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
515 virtual bool isEqualImpl(const Matcher *M) const {
516 return cast<CheckTypeMatcher>(M)->Type == Type;
518 virtual unsigned getHashImpl() const { return Type; }
519 virtual bool isContradictoryImpl(const Matcher *M) const;
522 /// SwitchTypeMatcher - Switch based on the current node's type, dispatching
523 /// to one matcher per case. If the type doesn't match any of the cases,
524 /// then the match fails. This is semantically equivalent to a Scope node where
525 /// every child does a CheckType, but is much faster.
526 class SwitchTypeMatcher : public Matcher {
527 SmallVector<std::pair<MVT::SimpleValueType, Matcher*>, 8> Cases;
529 SwitchTypeMatcher(const std::pair<MVT::SimpleValueType, Matcher*> *cases,
531 : Matcher(SwitchType), Cases(cases, cases+numcases) {}
533 static inline bool classof(const Matcher *N) {
534 return N->getKind() == SwitchType;
537 unsigned getNumCases() const { return Cases.size(); }
539 MVT::SimpleValueType getCaseType(unsigned i) const { return Cases[i].first; }
540 Matcher *getCaseMatcher(unsigned i) { return Cases[i].second; }
541 const Matcher *getCaseMatcher(unsigned i) const { return Cases[i].second; }
544 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
545 virtual bool isEqualImpl(const Matcher *M) const { return false; }
546 virtual unsigned getHashImpl() const { return 4123; }
550 /// CheckChildTypeMatcher - This checks to see if a child node has the
551 /// specified type, if not it fails to match.
552 class CheckChildTypeMatcher : public Matcher {
554 MVT::SimpleValueType Type;
556 CheckChildTypeMatcher(unsigned childno, MVT::SimpleValueType type)
557 : Matcher(CheckChildType), ChildNo(childno), Type(type) {}
559 unsigned getChildNo() const { return ChildNo; }
560 MVT::SimpleValueType getType() const { return Type; }
562 static inline bool classof(const Matcher *N) {
563 return N->getKind() == CheckChildType;
566 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
569 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
570 virtual bool isEqualImpl(const Matcher *M) const {
571 return cast<CheckChildTypeMatcher>(M)->ChildNo == ChildNo &&
572 cast<CheckChildTypeMatcher>(M)->Type == Type;
574 virtual unsigned getHashImpl() const { return (Type << 3) | ChildNo; }
575 virtual bool isContradictoryImpl(const Matcher *M) const;
579 /// CheckIntegerMatcher - This checks to see if the current node is a
580 /// ConstantSDNode with the specified integer value, if not it fails to match.
581 class CheckIntegerMatcher : public Matcher {
584 CheckIntegerMatcher(int64_t value)
585 : Matcher(CheckInteger), Value(value) {}
587 int64_t getValue() const { return Value; }
589 static inline bool classof(const Matcher *N) {
590 return N->getKind() == CheckInteger;
593 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
596 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
597 virtual bool isEqualImpl(const Matcher *M) const {
598 return cast<CheckIntegerMatcher>(M)->Value == Value;
600 virtual unsigned getHashImpl() const { return Value; }
601 virtual bool isContradictoryImpl(const Matcher *M) const;
604 /// CheckCondCodeMatcher - This checks to see if the current node is a
605 /// CondCodeSDNode with the specified condition, if not it fails to match.
606 class CheckCondCodeMatcher : public Matcher {
607 StringRef CondCodeName;
609 CheckCondCodeMatcher(StringRef condcodename)
610 : Matcher(CheckCondCode), CondCodeName(condcodename) {}
612 StringRef getCondCodeName() const { return CondCodeName; }
614 static inline bool classof(const Matcher *N) {
615 return N->getKind() == CheckCondCode;
618 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
621 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
622 virtual bool isEqualImpl(const Matcher *M) const {
623 return cast<CheckCondCodeMatcher>(M)->CondCodeName == CondCodeName;
625 virtual unsigned getHashImpl() const;
628 /// CheckValueTypeMatcher - This checks to see if the current node is a
629 /// VTSDNode with the specified type, if not it fails to match.
630 class CheckValueTypeMatcher : public Matcher {
633 CheckValueTypeMatcher(StringRef type_name)
634 : Matcher(CheckValueType), TypeName(type_name) {}
636 StringRef getTypeName() const { return TypeName; }
638 static inline bool classof(const Matcher *N) {
639 return N->getKind() == CheckValueType;
642 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
645 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
646 virtual bool isEqualImpl(const Matcher *M) const {
647 return cast<CheckValueTypeMatcher>(M)->TypeName == TypeName;
649 virtual unsigned getHashImpl() const;
650 bool isContradictoryImpl(const Matcher *M) const;
655 /// CheckComplexPatMatcher - This node runs the specified ComplexPattern on
656 /// the current node.
657 class CheckComplexPatMatcher : public Matcher {
658 const ComplexPattern &Pattern;
660 /// MatchNumber - This is the recorded nodes slot that contains the node we
661 /// want to match against.
662 unsigned MatchNumber;
664 /// Name - The name of the node we're matching, for comment emission.
667 /// FirstResult - This is the first slot in the RecordedNodes list that the
668 /// result of the match populates.
669 unsigned FirstResult;
671 CheckComplexPatMatcher(const ComplexPattern &pattern, unsigned matchnumber,
672 const std::string &name, unsigned firstresult)
673 : Matcher(CheckComplexPat), Pattern(pattern), MatchNumber(matchnumber),
674 Name(name), FirstResult(firstresult) {}
676 const ComplexPattern &getPattern() const { return Pattern; }
677 unsigned getMatchNumber() const { return MatchNumber; }
679 const std::string getName() const { return Name; }
680 unsigned getFirstResult() const { return FirstResult; }
682 static inline bool classof(const Matcher *N) {
683 return N->getKind() == CheckComplexPat;
686 // Not safe to move a pattern predicate past a complex pattern.
687 virtual bool isSafeToReorderWithPatternPredicate() const { return false; }
690 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
691 virtual bool isEqualImpl(const Matcher *M) const {
692 return &cast<CheckComplexPatMatcher>(M)->Pattern == &Pattern &&
693 cast<CheckComplexPatMatcher>(M)->MatchNumber == MatchNumber;
695 virtual unsigned getHashImpl() const {
696 return (unsigned)(intptr_t)&Pattern ^ MatchNumber;
700 /// CheckAndImmMatcher - This checks to see if the current node is an 'and'
701 /// with something equivalent to the specified immediate.
702 class CheckAndImmMatcher : public Matcher {
705 CheckAndImmMatcher(int64_t value)
706 : Matcher(CheckAndImm), Value(value) {}
708 int64_t getValue() const { return Value; }
710 static inline bool classof(const Matcher *N) {
711 return N->getKind() == CheckAndImm;
714 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
717 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
718 virtual bool isEqualImpl(const Matcher *M) const {
719 return cast<CheckAndImmMatcher>(M)->Value == Value;
721 virtual unsigned getHashImpl() const { return Value; }
724 /// CheckOrImmMatcher - This checks to see if the current node is an 'and'
725 /// with something equivalent to the specified immediate.
726 class CheckOrImmMatcher : public Matcher {
729 CheckOrImmMatcher(int64_t value)
730 : Matcher(CheckOrImm), Value(value) {}
732 int64_t getValue() const { return Value; }
734 static inline bool classof(const Matcher *N) {
735 return N->getKind() == CheckOrImm;
738 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
741 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
742 virtual bool isEqualImpl(const Matcher *M) const {
743 return cast<CheckOrImmMatcher>(M)->Value == Value;
745 virtual unsigned getHashImpl() const { return Value; }
748 /// CheckFoldableChainNodeMatcher - This checks to see if the current node
749 /// (which defines a chain operand) is safe to fold into a larger pattern.
750 class CheckFoldableChainNodeMatcher : public Matcher {
752 CheckFoldableChainNodeMatcher()
753 : Matcher(CheckFoldableChainNode) {}
755 static inline bool classof(const Matcher *N) {
756 return N->getKind() == CheckFoldableChainNode;
759 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
762 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
763 virtual bool isEqualImpl(const Matcher *M) const { return true; }
764 virtual unsigned getHashImpl() const { return 0; }
767 /// EmitIntegerMatcher - This creates a new TargetConstant.
768 class EmitIntegerMatcher : public Matcher {
770 MVT::SimpleValueType VT;
772 EmitIntegerMatcher(int64_t val, MVT::SimpleValueType vt)
773 : Matcher(EmitInteger), Val(val), VT(vt) {}
775 int64_t getValue() const { return Val; }
776 MVT::SimpleValueType getVT() const { return VT; }
778 static inline bool classof(const Matcher *N) {
779 return N->getKind() == EmitInteger;
783 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
784 virtual bool isEqualImpl(const Matcher *M) const {
785 return cast<EmitIntegerMatcher>(M)->Val == Val &&
786 cast<EmitIntegerMatcher>(M)->VT == VT;
788 virtual unsigned getHashImpl() const { return (Val << 4) | VT; }
791 /// EmitStringIntegerMatcher - A target constant whose value is represented
793 class EmitStringIntegerMatcher : public Matcher {
795 MVT::SimpleValueType VT;
797 EmitStringIntegerMatcher(const std::string &val, MVT::SimpleValueType vt)
798 : Matcher(EmitStringInteger), Val(val), VT(vt) {}
800 const std::string &getValue() const { return Val; }
801 MVT::SimpleValueType getVT() const { return VT; }
803 static inline bool classof(const Matcher *N) {
804 return N->getKind() == EmitStringInteger;
808 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
809 virtual bool isEqualImpl(const Matcher *M) const {
810 return cast<EmitStringIntegerMatcher>(M)->Val == Val &&
811 cast<EmitStringIntegerMatcher>(M)->VT == VT;
813 virtual unsigned getHashImpl() const;
816 /// EmitRegisterMatcher - This creates a new TargetConstant.
817 class EmitRegisterMatcher : public Matcher {
818 /// Reg - The def for the register that we're emitting. If this is null, then
819 /// this is a reference to zero_reg.
820 const CodeGenRegister *Reg;
821 MVT::SimpleValueType VT;
823 EmitRegisterMatcher(const CodeGenRegister *reg, MVT::SimpleValueType vt)
824 : Matcher(EmitRegister), Reg(reg), VT(vt) {}
826 const CodeGenRegister *getReg() const { return Reg; }
827 MVT::SimpleValueType getVT() const { return VT; }
829 static inline bool classof(const Matcher *N) {
830 return N->getKind() == EmitRegister;
834 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
835 virtual bool isEqualImpl(const Matcher *M) const {
836 return cast<EmitRegisterMatcher>(M)->Reg == Reg &&
837 cast<EmitRegisterMatcher>(M)->VT == VT;
839 virtual unsigned getHashImpl() const {
840 return ((unsigned)(intptr_t)Reg) << 4 | VT;
844 /// EmitConvertToTargetMatcher - Emit an operation that reads a specified
845 /// recorded node and converts it from being a ISD::Constant to
846 /// ISD::TargetConstant, likewise for ConstantFP.
847 class EmitConvertToTargetMatcher : public Matcher {
850 EmitConvertToTargetMatcher(unsigned slot)
851 : Matcher(EmitConvertToTarget), Slot(slot) {}
853 unsigned getSlot() const { return Slot; }
855 static inline bool classof(const Matcher *N) {
856 return N->getKind() == EmitConvertToTarget;
860 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
861 virtual bool isEqualImpl(const Matcher *M) const {
862 return cast<EmitConvertToTargetMatcher>(M)->Slot == Slot;
864 virtual unsigned getHashImpl() const { return Slot; }
867 /// EmitMergeInputChainsMatcher - Emit a node that merges a list of input
868 /// chains together with a token factor. The list of nodes are the nodes in the
869 /// matched pattern that have chain input/outputs. This node adds all input
870 /// chains of these nodes if they are not themselves a node in the pattern.
871 class EmitMergeInputChainsMatcher : public Matcher {
872 SmallVector<unsigned, 3> ChainNodes;
874 EmitMergeInputChainsMatcher(const unsigned *nodes, unsigned NumNodes)
875 : Matcher(EmitMergeInputChains), ChainNodes(nodes, nodes+NumNodes) {}
877 unsigned getNumNodes() const { return ChainNodes.size(); }
879 unsigned getNode(unsigned i) const {
880 assert(i < ChainNodes.size());
881 return ChainNodes[i];
884 static inline bool classof(const Matcher *N) {
885 return N->getKind() == EmitMergeInputChains;
889 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
890 virtual bool isEqualImpl(const Matcher *M) const {
891 return cast<EmitMergeInputChainsMatcher>(M)->ChainNodes == ChainNodes;
893 virtual unsigned getHashImpl() const;
896 /// EmitCopyToRegMatcher - Emit a CopyToReg node from a value to a physreg,
897 /// pushing the chain and glue results.
899 class EmitCopyToRegMatcher : public Matcher {
900 unsigned SrcSlot; // Value to copy into the physreg.
903 EmitCopyToRegMatcher(unsigned srcSlot, Record *destPhysReg)
904 : Matcher(EmitCopyToReg), SrcSlot(srcSlot), DestPhysReg(destPhysReg) {}
906 unsigned getSrcSlot() const { return SrcSlot; }
907 Record *getDestPhysReg() const { return DestPhysReg; }
909 static inline bool classof(const Matcher *N) {
910 return N->getKind() == EmitCopyToReg;
914 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
915 virtual bool isEqualImpl(const Matcher *M) const {
916 return cast<EmitCopyToRegMatcher>(M)->SrcSlot == SrcSlot &&
917 cast<EmitCopyToRegMatcher>(M)->DestPhysReg == DestPhysReg;
919 virtual unsigned getHashImpl() const {
920 return SrcSlot ^ ((unsigned)(intptr_t)DestPhysReg << 4);
926 /// EmitNodeXFormMatcher - Emit an operation that runs an SDNodeXForm on a
927 /// recorded node and records the result.
928 class EmitNodeXFormMatcher : public Matcher {
932 EmitNodeXFormMatcher(unsigned slot, Record *nodeXForm)
933 : Matcher(EmitNodeXForm), Slot(slot), NodeXForm(nodeXForm) {}
935 unsigned getSlot() const { return Slot; }
936 Record *getNodeXForm() const { return NodeXForm; }
938 static inline bool classof(const Matcher *N) {
939 return N->getKind() == EmitNodeXForm;
943 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
944 virtual bool isEqualImpl(const Matcher *M) const {
945 return cast<EmitNodeXFormMatcher>(M)->Slot == Slot &&
946 cast<EmitNodeXFormMatcher>(M)->NodeXForm == NodeXForm;
948 virtual unsigned getHashImpl() const {
949 return Slot ^ ((unsigned)(intptr_t)NodeXForm << 4);
953 /// EmitNodeMatcherCommon - Common class shared between EmitNode and
955 class EmitNodeMatcherCommon : public Matcher {
956 std::string OpcodeName;
957 const SmallVector<MVT::SimpleValueType, 3> VTs;
958 const SmallVector<unsigned, 6> Operands;
959 bool HasChain, HasInGlue, HasOutGlue, HasMemRefs;
961 /// NumFixedArityOperands - If this is a fixed arity node, this is set to -1.
962 /// If this is a varidic node, this is set to the number of fixed arity
963 /// operands in the root of the pattern. The rest are appended to this node.
964 int NumFixedArityOperands;
966 EmitNodeMatcherCommon(const std::string &opcodeName,
967 const MVT::SimpleValueType *vts, unsigned numvts,
968 const unsigned *operands, unsigned numops,
969 bool hasChain, bool hasInGlue, bool hasOutGlue,
971 int numfixedarityoperands, bool isMorphNodeTo)
972 : Matcher(isMorphNodeTo ? MorphNodeTo : EmitNode), OpcodeName(opcodeName),
973 VTs(vts, vts+numvts), Operands(operands, operands+numops),
974 HasChain(hasChain), HasInGlue(hasInGlue), HasOutGlue(hasOutGlue),
975 HasMemRefs(hasmemrefs), NumFixedArityOperands(numfixedarityoperands) {}
977 const std::string &getOpcodeName() const { return OpcodeName; }
979 unsigned getNumVTs() const { return VTs.size(); }
980 MVT::SimpleValueType getVT(unsigned i) const {
981 assert(i < VTs.size());
985 unsigned getNumOperands() const { return Operands.size(); }
986 unsigned getOperand(unsigned i) const {
987 assert(i < Operands.size());
991 const SmallVectorImpl<MVT::SimpleValueType> &getVTList() const { return VTs; }
992 const SmallVectorImpl<unsigned> &getOperandList() const { return Operands; }
995 bool hasChain() const { return HasChain; }
996 bool hasInFlag() const { return HasInGlue; }
997 bool hasOutFlag() const { return HasOutGlue; }
998 bool hasMemRefs() const { return HasMemRefs; }
999 int getNumFixedArityOperands() const { return NumFixedArityOperands; }
1001 static inline bool classof(const Matcher *N) {
1002 return N->getKind() == EmitNode || N->getKind() == MorphNodeTo;
1006 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
1007 virtual bool isEqualImpl(const Matcher *M) const;
1008 virtual unsigned getHashImpl() const;
1011 /// EmitNodeMatcher - This signals a successful match and generates a node.
1012 class EmitNodeMatcher : public EmitNodeMatcherCommon {
1013 virtual void anchor();
1014 unsigned FirstResultSlot;
1016 EmitNodeMatcher(const std::string &opcodeName,
1017 const MVT::SimpleValueType *vts, unsigned numvts,
1018 const unsigned *operands, unsigned numops,
1019 bool hasChain, bool hasInFlag, bool hasOutFlag,
1021 int numfixedarityoperands, unsigned firstresultslot)
1022 : EmitNodeMatcherCommon(opcodeName, vts, numvts, operands, numops, hasChain,
1023 hasInFlag, hasOutFlag, hasmemrefs,
1024 numfixedarityoperands, false),
1025 FirstResultSlot(firstresultslot) {}
1027 unsigned getFirstResultSlot() const { return FirstResultSlot; }
1029 static inline bool classof(const Matcher *N) {
1030 return N->getKind() == EmitNode;
1035 class MorphNodeToMatcher : public EmitNodeMatcherCommon {
1036 virtual void anchor();
1037 const PatternToMatch &Pattern;
1039 MorphNodeToMatcher(const std::string &opcodeName,
1040 const MVT::SimpleValueType *vts, unsigned numvts,
1041 const unsigned *operands, unsigned numops,
1042 bool hasChain, bool hasInFlag, bool hasOutFlag,
1044 int numfixedarityoperands, const PatternToMatch &pattern)
1045 : EmitNodeMatcherCommon(opcodeName, vts, numvts, operands, numops, hasChain,
1046 hasInFlag, hasOutFlag, hasmemrefs,
1047 numfixedarityoperands, true),
1051 const PatternToMatch &getPattern() const { return Pattern; }
1053 static inline bool classof(const Matcher *N) {
1054 return N->getKind() == MorphNodeTo;
1058 /// MarkGlueResultsMatcher - This node indicates which non-root nodes in the
1059 /// pattern produce glue. This allows CompleteMatchMatcher to update them
1060 /// with the output glue of the resultant code.
1061 class MarkGlueResultsMatcher : public Matcher {
1062 SmallVector<unsigned, 3> GlueResultNodes;
1064 MarkGlueResultsMatcher(const unsigned *nodes, unsigned NumNodes)
1065 : Matcher(MarkGlueResults), GlueResultNodes(nodes, nodes+NumNodes) {}
1067 unsigned getNumNodes() const { return GlueResultNodes.size(); }
1069 unsigned getNode(unsigned i) const {
1070 assert(i < GlueResultNodes.size());
1071 return GlueResultNodes[i];
1074 static inline bool classof(const Matcher *N) {
1075 return N->getKind() == MarkGlueResults;
1079 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
1080 virtual bool isEqualImpl(const Matcher *M) const {
1081 return cast<MarkGlueResultsMatcher>(M)->GlueResultNodes == GlueResultNodes;
1083 virtual unsigned getHashImpl() const;
1086 /// CompleteMatchMatcher - Complete a match by replacing the results of the
1087 /// pattern with the newly generated nodes. This also prints a comment
1088 /// indicating the source and dest patterns.
1089 class CompleteMatchMatcher : public Matcher {
1090 SmallVector<unsigned, 2> Results;
1091 const PatternToMatch &Pattern;
1093 CompleteMatchMatcher(const unsigned *results, unsigned numresults,
1094 const PatternToMatch &pattern)
1095 : Matcher(CompleteMatch), Results(results, results+numresults),
1098 unsigned getNumResults() const { return Results.size(); }
1099 unsigned getResult(unsigned R) const { return Results[R]; }
1100 const PatternToMatch &getPattern() const { return Pattern; }
1102 static inline bool classof(const Matcher *N) {
1103 return N->getKind() == CompleteMatch;
1107 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
1108 virtual bool isEqualImpl(const Matcher *M) const {
1109 return cast<CompleteMatchMatcher>(M)->Results == Results &&
1110 &cast<CompleteMatchMatcher>(M)->Pattern == &Pattern;
1112 virtual unsigned getHashImpl() const;
1115 } // end namespace llvm