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/ArrayRef.h"
14 #include "llvm/ADT/OwningPtr.h"
15 #include "llvm/ADT/SmallVector.h"
16 #include "llvm/ADT/StringRef.h"
17 #include "llvm/CodeGen/ValueTypes.h"
18 #include "llvm/Support/Casting.h"
21 struct CodeGenRegister;
22 class CodeGenDAGPatterns;
29 class TreePredicateFn;
32 Matcher *ConvertPatternToMatcher(const PatternToMatch &Pattern,unsigned Variant,
33 const CodeGenDAGPatterns &CGP);
34 Matcher *OptimizeMatcher(Matcher *Matcher, const CodeGenDAGPatterns &CGP);
35 void EmitMatcherTable(const Matcher *Matcher, const CodeGenDAGPatterns &CGP,
39 /// Matcher - Base class for all the DAG ISel Matcher representation
42 // The next matcher node that is executed after this one. Null if this is the
43 // last stage of a match.
44 OwningPtr<Matcher> Next;
45 virtual void anchor();
48 // Matcher state manipulation.
49 Scope, // Push a checking scope.
50 RecordNode, // Record the current node.
51 RecordChild, // Record a child of the current node.
52 RecordMemRef, // Record the memref in the current node.
53 CaptureGlueInput, // If the current node has an input glue, save it.
54 MoveChild, // Move current node to specified child.
55 MoveParent, // Move current node to parent.
57 // Predicate checking.
58 CheckSame, // Fail if not same as prev match.
59 CheckChildSame, // Fail if child not same as prev match.
60 CheckPatternPredicate,
61 CheckPredicate, // Fail if node predicate fails.
62 CheckOpcode, // Fail if not opcode.
63 SwitchOpcode, // Dispatch based on opcode.
64 CheckType, // Fail if not correct type.
65 SwitchType, // Dispatch based on type.
66 CheckChildType, // Fail if child has wrong type.
67 CheckInteger, // Fail if wrong val.
68 CheckCondCode, // Fail if not condcode.
73 CheckFoldableChainNode,
75 // Node creation/emisssion.
76 EmitInteger, // Create a TargetConstant
77 EmitStringInteger, // Create a TargetConstant from a string.
78 EmitRegister, // Create a register.
79 EmitConvertToTarget, // Convert a imm/fpimm to target imm/fpimm
80 EmitMergeInputChains, // Merge together a chains for an input.
81 EmitCopyToReg, // Emit a copytoreg into a physreg.
82 EmitNode, // Create a DAG node
83 EmitNodeXForm, // Run a SDNodeXForm
84 MarkGlueResults, // Indicate which interior nodes have glue results.
85 CompleteMatch, // Finish a match and update the results.
86 MorphNodeTo // Build a node, finish a match and update results.
91 Matcher(KindTy K) : Kind(K) {}
95 KindTy getKind() const { return Kind; }
97 Matcher *getNext() { return Next.get(); }
98 const Matcher *getNext() const { return Next.get(); }
99 void setNext(Matcher *C) { Next.reset(C); }
100 Matcher *takeNext() { return Next.take(); }
102 OwningPtr<Matcher> &getNextPtr() { return Next; }
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;
128 case CheckPatternPredicate:
138 case CheckFoldableChainNode:
143 /// isSimplePredicateOrRecordNode - Return true if this is a record node or
144 /// a simple predicate.
145 bool isSimplePredicateOrRecordNode() const {
146 return isSimplePredicateNode() ||
147 getKind() == RecordNode || getKind() == RecordChild;
150 /// unlinkNode - Unlink the specified node from this chain. If Other == this,
151 /// we unlink the next pointer and return it. Otherwise we unlink Other from
152 /// the list and return this.
153 Matcher *unlinkNode(Matcher *Other);
155 /// canMoveBefore - Return true if this matcher is the same as Other, or if
156 /// we can move this matcher past all of the nodes in-between Other and this
157 /// node. Other must be equal to or before this.
158 bool canMoveBefore(const Matcher *Other) const;
160 /// canMoveBeforeNode - Return true if it is safe to move the current matcher
161 /// across the specified one.
162 bool canMoveBeforeNode(const Matcher *Other) const;
164 /// isContradictory - Return true of these two matchers could never match on
166 bool isContradictory(const Matcher *Other) const {
167 // Since this predicate is reflexive, we canonicalize the ordering so that
168 // we always match a node against nodes with kinds that are greater or equal
169 // to them. For example, we'll pass in a CheckType node as an argument to
170 // the CheckOpcode method, not the other way around.
171 if (getKind() < Other->getKind())
172 return isContradictoryImpl(Other);
173 return Other->isContradictoryImpl(this);
176 void print(raw_ostream &OS, unsigned indent = 0) const;
177 void printOne(raw_ostream &OS) const;
180 virtual void printImpl(raw_ostream &OS, unsigned indent) const = 0;
181 virtual bool isEqualImpl(const Matcher *M) const = 0;
182 virtual unsigned getHashImpl() const = 0;
183 virtual bool isContradictoryImpl(const Matcher *M) const { return false; }
186 /// ScopeMatcher - This attempts to match each of its children to find the first
187 /// one that successfully matches. If one child fails, it tries the next child.
188 /// If none of the children match then this check fails. It never has a 'next'.
189 class ScopeMatcher : public Matcher {
190 SmallVector<Matcher*, 4> Children;
192 ScopeMatcher(ArrayRef<Matcher *> children)
193 : Matcher(Scope), Children(children.begin(), children.end()) {
195 virtual ~ScopeMatcher();
197 unsigned getNumChildren() const { return Children.size(); }
199 Matcher *getChild(unsigned i) { return Children[i]; }
200 const Matcher *getChild(unsigned i) const { return Children[i]; }
202 void resetChild(unsigned i, Matcher *N) {
207 Matcher *takeChild(unsigned i) {
208 Matcher *Res = Children[i];
213 void setNumChildren(unsigned NC) {
214 if (NC < Children.size()) {
215 // delete any children we're about to lose pointers to.
216 for (unsigned i = NC, e = Children.size(); i != e; ++i)
222 static inline bool classof(const Matcher *N) {
223 return N->getKind() == Scope;
227 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
228 virtual bool isEqualImpl(const Matcher *M) const { return false; }
229 virtual unsigned getHashImpl() const { return 12312; }
232 /// RecordMatcher - Save the current node in the operand list.
233 class RecordMatcher : public Matcher {
234 /// WhatFor - This is a string indicating why we're recording this. This
235 /// should only be used for comment generation not anything semantic.
238 /// ResultNo - The slot number in the RecordedNodes vector that this will be,
239 /// just printed as a comment.
242 RecordMatcher(const std::string &whatfor, unsigned resultNo)
243 : Matcher(RecordNode), WhatFor(whatfor), ResultNo(resultNo) {}
245 const std::string &getWhatFor() const { return WhatFor; }
246 unsigned getResultNo() const { return ResultNo; }
248 static inline bool classof(const Matcher *N) {
249 return N->getKind() == RecordNode;
252 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
254 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
255 virtual bool isEqualImpl(const Matcher *M) const { return true; }
256 virtual unsigned getHashImpl() const { return 0; }
259 /// RecordChildMatcher - Save a numbered child of the current node, or fail
260 /// the match if it doesn't exist. This is logically equivalent to:
261 /// MoveChild N + RecordNode + MoveParent.
262 class RecordChildMatcher : public Matcher {
265 /// WhatFor - This is a string indicating why we're recording this. This
266 /// should only be used for comment generation not anything semantic.
269 /// ResultNo - The slot number in the RecordedNodes vector that this will be,
270 /// just printed as a comment.
273 RecordChildMatcher(unsigned childno, const std::string &whatfor,
275 : Matcher(RecordChild), ChildNo(childno), WhatFor(whatfor),
276 ResultNo(resultNo) {}
278 unsigned getChildNo() const { return ChildNo; }
279 const std::string &getWhatFor() const { return WhatFor; }
280 unsigned getResultNo() const { return ResultNo; }
282 static inline bool classof(const Matcher *N) {
283 return N->getKind() == RecordChild;
286 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
289 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
290 virtual bool isEqualImpl(const Matcher *M) const {
291 return cast<RecordChildMatcher>(M)->getChildNo() == getChildNo();
293 virtual unsigned getHashImpl() const { return getChildNo(); }
296 /// RecordMemRefMatcher - Save the current node's memref.
297 class RecordMemRefMatcher : public Matcher {
299 RecordMemRefMatcher() : Matcher(RecordMemRef) {}
301 static inline bool classof(const Matcher *N) {
302 return N->getKind() == RecordMemRef;
305 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
308 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
309 virtual bool isEqualImpl(const Matcher *M) const { return true; }
310 virtual unsigned getHashImpl() const { return 0; }
314 /// CaptureGlueInputMatcher - If the current record has a glue input, record
315 /// it so that it is used as an input to the generated code.
316 class CaptureGlueInputMatcher : public Matcher {
318 CaptureGlueInputMatcher() : Matcher(CaptureGlueInput) {}
320 static inline bool classof(const Matcher *N) {
321 return N->getKind() == CaptureGlueInput;
324 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
327 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
328 virtual bool isEqualImpl(const Matcher *M) const { return true; }
329 virtual unsigned getHashImpl() const { return 0; }
332 /// MoveChildMatcher - This tells the interpreter to move into the
333 /// specified child node.
334 class MoveChildMatcher : public Matcher {
337 MoveChildMatcher(unsigned childNo) : Matcher(MoveChild), ChildNo(childNo) {}
339 unsigned getChildNo() const { return ChildNo; }
341 static inline bool classof(const Matcher *N) {
342 return N->getKind() == MoveChild;
345 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
348 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
349 virtual bool isEqualImpl(const Matcher *M) const {
350 return cast<MoveChildMatcher>(M)->getChildNo() == getChildNo();
352 virtual unsigned getHashImpl() const { return getChildNo(); }
355 /// MoveParentMatcher - This tells the interpreter to move to the parent
356 /// of the current node.
357 class MoveParentMatcher : public Matcher {
359 MoveParentMatcher() : Matcher(MoveParent) {}
361 static inline bool classof(const Matcher *N) {
362 return N->getKind() == MoveParent;
365 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
368 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
369 virtual bool isEqualImpl(const Matcher *M) const { return true; }
370 virtual unsigned getHashImpl() const { return 0; }
373 /// CheckSameMatcher - This checks to see if this node is exactly the same
374 /// node as the specified match that was recorded with 'Record'. This is used
375 /// when patterns have the same name in them, like '(mul GPR:$in, GPR:$in)'.
376 class CheckSameMatcher : public Matcher {
377 unsigned MatchNumber;
379 CheckSameMatcher(unsigned matchnumber)
380 : Matcher(CheckSame), MatchNumber(matchnumber) {}
382 unsigned getMatchNumber() const { return MatchNumber; }
384 static inline bool classof(const Matcher *N) {
385 return N->getKind() == CheckSame;
388 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
391 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
392 virtual bool isEqualImpl(const Matcher *M) const {
393 return cast<CheckSameMatcher>(M)->getMatchNumber() == getMatchNumber();
395 virtual unsigned getHashImpl() const { return getMatchNumber(); }
398 /// CheckChildSameMatcher - This checks to see if child node is exactly the same
399 /// node as the specified match that was recorded with 'Record'. This is used
400 /// when patterns have the same name in them, like '(mul GPR:$in, GPR:$in)'.
401 class CheckChildSameMatcher : public Matcher {
403 unsigned MatchNumber;
405 CheckChildSameMatcher(unsigned childno, unsigned matchnumber)
406 : Matcher(CheckChildSame), ChildNo(childno), MatchNumber(matchnumber) {}
408 unsigned getChildNo() const { return ChildNo; }
409 unsigned getMatchNumber() const { return MatchNumber; }
411 static inline bool classof(const Matcher *N) {
412 return N->getKind() == CheckChildSame;
415 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
418 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
419 virtual bool isEqualImpl(const Matcher *M) const {
420 return cast<CheckChildSameMatcher>(M)->ChildNo == ChildNo &&
421 cast<CheckChildSameMatcher>(M)->MatchNumber == MatchNumber;
423 virtual unsigned getHashImpl() const { return (MatchNumber << 2) | ChildNo; }
426 /// CheckPatternPredicateMatcher - This checks the target-specific predicate
427 /// to see if the entire pattern is capable of matching. This predicate does
428 /// not take a node as input. This is used for subtarget feature checks etc.
429 class CheckPatternPredicateMatcher : public Matcher {
430 std::string Predicate;
432 CheckPatternPredicateMatcher(StringRef predicate)
433 : Matcher(CheckPatternPredicate), Predicate(predicate) {}
435 StringRef getPredicate() const { return Predicate; }
437 static inline bool classof(const Matcher *N) {
438 return N->getKind() == CheckPatternPredicate;
441 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
444 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
445 virtual bool isEqualImpl(const Matcher *M) const {
446 return cast<CheckPatternPredicateMatcher>(M)->getPredicate() == Predicate;
448 virtual unsigned getHashImpl() const;
451 /// CheckPredicateMatcher - This checks the target-specific predicate to
452 /// see if the node is acceptable.
453 class CheckPredicateMatcher : public Matcher {
456 CheckPredicateMatcher(const TreePredicateFn &pred);
458 TreePredicateFn getPredicate() const;
460 static inline bool classof(const Matcher *N) {
461 return N->getKind() == CheckPredicate;
465 //virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
468 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
469 virtual bool isEqualImpl(const Matcher *M) const {
470 return cast<CheckPredicateMatcher>(M)->Pred == Pred;
472 virtual unsigned getHashImpl() const;
476 /// CheckOpcodeMatcher - This checks to see if the current node has the
477 /// specified opcode, if not it fails to match.
478 class CheckOpcodeMatcher : public Matcher {
479 const SDNodeInfo &Opcode;
481 CheckOpcodeMatcher(const SDNodeInfo &opcode)
482 : Matcher(CheckOpcode), Opcode(opcode) {}
484 const SDNodeInfo &getOpcode() const { return Opcode; }
486 static inline bool classof(const Matcher *N) {
487 return N->getKind() == CheckOpcode;
490 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
493 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
494 virtual bool isEqualImpl(const Matcher *M) const;
495 virtual unsigned getHashImpl() const;
496 virtual bool isContradictoryImpl(const Matcher *M) const;
499 /// SwitchOpcodeMatcher - Switch based on the current node's opcode, dispatching
500 /// to one matcher per opcode. If the opcode doesn't match any of the cases,
501 /// then the match fails. This is semantically equivalent to a Scope node where
502 /// every child does a CheckOpcode, but is much faster.
503 class SwitchOpcodeMatcher : public Matcher {
504 SmallVector<std::pair<const SDNodeInfo*, Matcher*>, 8> Cases;
506 SwitchOpcodeMatcher(ArrayRef<std::pair<const SDNodeInfo*, Matcher*> > cases)
507 : Matcher(SwitchOpcode), Cases(cases.begin(), cases.end()) {}
508 virtual ~SwitchOpcodeMatcher();
510 static inline bool classof(const Matcher *N) {
511 return N->getKind() == SwitchOpcode;
514 unsigned getNumCases() const { return Cases.size(); }
516 const SDNodeInfo &getCaseOpcode(unsigned i) const { return *Cases[i].first; }
517 Matcher *getCaseMatcher(unsigned i) { return Cases[i].second; }
518 const Matcher *getCaseMatcher(unsigned i) const { return Cases[i].second; }
521 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
522 virtual bool isEqualImpl(const Matcher *M) const { return false; }
523 virtual unsigned getHashImpl() const { return 4123; }
526 /// CheckTypeMatcher - This checks to see if the current node has the
527 /// specified type at the specified result, if not it fails to match.
528 class CheckTypeMatcher : public Matcher {
529 MVT::SimpleValueType Type;
532 CheckTypeMatcher(MVT::SimpleValueType type, unsigned resno)
533 : Matcher(CheckType), Type(type), ResNo(resno) {}
535 MVT::SimpleValueType getType() const { return Type; }
536 unsigned getResNo() const { return ResNo; }
538 static inline bool classof(const Matcher *N) {
539 return N->getKind() == CheckType;
542 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
545 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
546 virtual bool isEqualImpl(const Matcher *M) const {
547 return cast<CheckTypeMatcher>(M)->Type == Type;
549 virtual unsigned getHashImpl() const { return Type; }
550 virtual bool isContradictoryImpl(const Matcher *M) const;
553 /// SwitchTypeMatcher - Switch based on the current node's type, dispatching
554 /// to one matcher per case. If the type doesn't match any of the cases,
555 /// then the match fails. This is semantically equivalent to a Scope node where
556 /// every child does a CheckType, but is much faster.
557 class SwitchTypeMatcher : public Matcher {
558 SmallVector<std::pair<MVT::SimpleValueType, Matcher*>, 8> Cases;
560 SwitchTypeMatcher(ArrayRef<std::pair<MVT::SimpleValueType, Matcher*> > cases)
561 : Matcher(SwitchType), Cases(cases.begin(), cases.end()) {}
562 virtual ~SwitchTypeMatcher();
564 static inline bool classof(const Matcher *N) {
565 return N->getKind() == SwitchType;
568 unsigned getNumCases() const { return Cases.size(); }
570 MVT::SimpleValueType getCaseType(unsigned i) const { return Cases[i].first; }
571 Matcher *getCaseMatcher(unsigned i) { return Cases[i].second; }
572 const Matcher *getCaseMatcher(unsigned i) const { return Cases[i].second; }
575 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
576 virtual bool isEqualImpl(const Matcher *M) const { return false; }
577 virtual unsigned getHashImpl() const { return 4123; }
581 /// CheckChildTypeMatcher - This checks to see if a child node has the
582 /// specified type, if not it fails to match.
583 class CheckChildTypeMatcher : public Matcher {
585 MVT::SimpleValueType Type;
587 CheckChildTypeMatcher(unsigned childno, MVT::SimpleValueType type)
588 : Matcher(CheckChildType), ChildNo(childno), Type(type) {}
590 unsigned getChildNo() const { return ChildNo; }
591 MVT::SimpleValueType getType() const { return Type; }
593 static inline bool classof(const Matcher *N) {
594 return N->getKind() == CheckChildType;
597 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
600 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
601 virtual bool isEqualImpl(const Matcher *M) const {
602 return cast<CheckChildTypeMatcher>(M)->ChildNo == ChildNo &&
603 cast<CheckChildTypeMatcher>(M)->Type == Type;
605 virtual unsigned getHashImpl() const { return (Type << 3) | ChildNo; }
606 virtual bool isContradictoryImpl(const Matcher *M) const;
610 /// CheckIntegerMatcher - This checks to see if the current node is a
611 /// ConstantSDNode with the specified integer value, if not it fails to match.
612 class CheckIntegerMatcher : public Matcher {
615 CheckIntegerMatcher(int64_t value)
616 : Matcher(CheckInteger), Value(value) {}
618 int64_t getValue() const { return Value; }
620 static inline bool classof(const Matcher *N) {
621 return N->getKind() == CheckInteger;
624 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
627 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
628 virtual bool isEqualImpl(const Matcher *M) const {
629 return cast<CheckIntegerMatcher>(M)->Value == Value;
631 virtual unsigned getHashImpl() const { return Value; }
632 virtual bool isContradictoryImpl(const Matcher *M) const;
635 /// CheckCondCodeMatcher - This checks to see if the current node is a
636 /// CondCodeSDNode with the specified condition, if not it fails to match.
637 class CheckCondCodeMatcher : public Matcher {
638 StringRef CondCodeName;
640 CheckCondCodeMatcher(StringRef condcodename)
641 : Matcher(CheckCondCode), CondCodeName(condcodename) {}
643 StringRef getCondCodeName() const { return CondCodeName; }
645 static inline bool classof(const Matcher *N) {
646 return N->getKind() == CheckCondCode;
649 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
652 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
653 virtual bool isEqualImpl(const Matcher *M) const {
654 return cast<CheckCondCodeMatcher>(M)->CondCodeName == CondCodeName;
656 virtual unsigned getHashImpl() const;
659 /// CheckValueTypeMatcher - This checks to see if the current node is a
660 /// VTSDNode with the specified type, if not it fails to match.
661 class CheckValueTypeMatcher : public Matcher {
664 CheckValueTypeMatcher(StringRef type_name)
665 : Matcher(CheckValueType), TypeName(type_name) {}
667 StringRef getTypeName() const { return TypeName; }
669 static inline bool classof(const Matcher *N) {
670 return N->getKind() == CheckValueType;
673 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
676 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
677 virtual bool isEqualImpl(const Matcher *M) const {
678 return cast<CheckValueTypeMatcher>(M)->TypeName == TypeName;
680 virtual unsigned getHashImpl() const;
681 bool isContradictoryImpl(const Matcher *M) const;
686 /// CheckComplexPatMatcher - This node runs the specified ComplexPattern on
687 /// the current node.
688 class CheckComplexPatMatcher : public Matcher {
689 const ComplexPattern &Pattern;
691 /// MatchNumber - This is the recorded nodes slot that contains the node we
692 /// want to match against.
693 unsigned MatchNumber;
695 /// Name - The name of the node we're matching, for comment emission.
698 /// FirstResult - This is the first slot in the RecordedNodes list that the
699 /// result of the match populates.
700 unsigned FirstResult;
702 CheckComplexPatMatcher(const ComplexPattern &pattern, unsigned matchnumber,
703 const std::string &name, unsigned firstresult)
704 : Matcher(CheckComplexPat), Pattern(pattern), MatchNumber(matchnumber),
705 Name(name), FirstResult(firstresult) {}
707 const ComplexPattern &getPattern() const { return Pattern; }
708 unsigned getMatchNumber() const { return MatchNumber; }
710 const std::string getName() const { return Name; }
711 unsigned getFirstResult() const { return FirstResult; }
713 static inline bool classof(const Matcher *N) {
714 return N->getKind() == CheckComplexPat;
717 // Not safe to move a pattern predicate past a complex pattern.
718 virtual bool isSafeToReorderWithPatternPredicate() const { return false; }
721 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
722 virtual bool isEqualImpl(const Matcher *M) const {
723 return &cast<CheckComplexPatMatcher>(M)->Pattern == &Pattern &&
724 cast<CheckComplexPatMatcher>(M)->MatchNumber == MatchNumber;
726 virtual unsigned getHashImpl() const {
727 return (unsigned)(intptr_t)&Pattern ^ MatchNumber;
731 /// CheckAndImmMatcher - This checks to see if the current node is an 'and'
732 /// with something equivalent to the specified immediate.
733 class CheckAndImmMatcher : public Matcher {
736 CheckAndImmMatcher(int64_t value)
737 : Matcher(CheckAndImm), Value(value) {}
739 int64_t getValue() const { return Value; }
741 static inline bool classof(const Matcher *N) {
742 return N->getKind() == CheckAndImm;
745 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
748 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
749 virtual bool isEqualImpl(const Matcher *M) const {
750 return cast<CheckAndImmMatcher>(M)->Value == Value;
752 virtual unsigned getHashImpl() const { return Value; }
755 /// CheckOrImmMatcher - This checks to see if the current node is an 'and'
756 /// with something equivalent to the specified immediate.
757 class CheckOrImmMatcher : public Matcher {
760 CheckOrImmMatcher(int64_t value)
761 : Matcher(CheckOrImm), Value(value) {}
763 int64_t getValue() const { return Value; }
765 static inline bool classof(const Matcher *N) {
766 return N->getKind() == CheckOrImm;
769 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
772 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
773 virtual bool isEqualImpl(const Matcher *M) const {
774 return cast<CheckOrImmMatcher>(M)->Value == Value;
776 virtual unsigned getHashImpl() const { return Value; }
779 /// CheckFoldableChainNodeMatcher - This checks to see if the current node
780 /// (which defines a chain operand) is safe to fold into a larger pattern.
781 class CheckFoldableChainNodeMatcher : public Matcher {
783 CheckFoldableChainNodeMatcher()
784 : Matcher(CheckFoldableChainNode) {}
786 static inline bool classof(const Matcher *N) {
787 return N->getKind() == CheckFoldableChainNode;
790 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
793 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
794 virtual bool isEqualImpl(const Matcher *M) const { return true; }
795 virtual unsigned getHashImpl() const { return 0; }
798 /// EmitIntegerMatcher - This creates a new TargetConstant.
799 class EmitIntegerMatcher : public Matcher {
801 MVT::SimpleValueType VT;
803 EmitIntegerMatcher(int64_t val, MVT::SimpleValueType vt)
804 : Matcher(EmitInteger), Val(val), VT(vt) {}
806 int64_t getValue() const { return Val; }
807 MVT::SimpleValueType getVT() const { return VT; }
809 static inline bool classof(const Matcher *N) {
810 return N->getKind() == EmitInteger;
814 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
815 virtual bool isEqualImpl(const Matcher *M) const {
816 return cast<EmitIntegerMatcher>(M)->Val == Val &&
817 cast<EmitIntegerMatcher>(M)->VT == VT;
819 virtual unsigned getHashImpl() const { return (Val << 4) | VT; }
822 /// EmitStringIntegerMatcher - A target constant whose value is represented
824 class EmitStringIntegerMatcher : public Matcher {
826 MVT::SimpleValueType VT;
828 EmitStringIntegerMatcher(const std::string &val, MVT::SimpleValueType vt)
829 : Matcher(EmitStringInteger), Val(val), VT(vt) {}
831 const std::string &getValue() const { return Val; }
832 MVT::SimpleValueType getVT() const { return VT; }
834 static inline bool classof(const Matcher *N) {
835 return N->getKind() == EmitStringInteger;
839 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
840 virtual bool isEqualImpl(const Matcher *M) const {
841 return cast<EmitStringIntegerMatcher>(M)->Val == Val &&
842 cast<EmitStringIntegerMatcher>(M)->VT == VT;
844 virtual unsigned getHashImpl() const;
847 /// EmitRegisterMatcher - This creates a new TargetConstant.
848 class EmitRegisterMatcher : public Matcher {
849 /// Reg - The def for the register that we're emitting. If this is null, then
850 /// this is a reference to zero_reg.
851 const CodeGenRegister *Reg;
852 MVT::SimpleValueType VT;
854 EmitRegisterMatcher(const CodeGenRegister *reg, MVT::SimpleValueType vt)
855 : Matcher(EmitRegister), Reg(reg), VT(vt) {}
857 const CodeGenRegister *getReg() const { return Reg; }
858 MVT::SimpleValueType getVT() const { return VT; }
860 static inline bool classof(const Matcher *N) {
861 return N->getKind() == EmitRegister;
865 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
866 virtual bool isEqualImpl(const Matcher *M) const {
867 return cast<EmitRegisterMatcher>(M)->Reg == Reg &&
868 cast<EmitRegisterMatcher>(M)->VT == VT;
870 virtual unsigned getHashImpl() const {
871 return ((unsigned)(intptr_t)Reg) << 4 | VT;
875 /// EmitConvertToTargetMatcher - Emit an operation that reads a specified
876 /// recorded node and converts it from being a ISD::Constant to
877 /// ISD::TargetConstant, likewise for ConstantFP.
878 class EmitConvertToTargetMatcher : public Matcher {
881 EmitConvertToTargetMatcher(unsigned slot)
882 : Matcher(EmitConvertToTarget), Slot(slot) {}
884 unsigned getSlot() const { return Slot; }
886 static inline bool classof(const Matcher *N) {
887 return N->getKind() == EmitConvertToTarget;
891 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
892 virtual bool isEqualImpl(const Matcher *M) const {
893 return cast<EmitConvertToTargetMatcher>(M)->Slot == Slot;
895 virtual unsigned getHashImpl() const { return Slot; }
898 /// EmitMergeInputChainsMatcher - Emit a node that merges a list of input
899 /// chains together with a token factor. The list of nodes are the nodes in the
900 /// matched pattern that have chain input/outputs. This node adds all input
901 /// chains of these nodes if they are not themselves a node in the pattern.
902 class EmitMergeInputChainsMatcher : public Matcher {
903 SmallVector<unsigned, 3> ChainNodes;
905 EmitMergeInputChainsMatcher(ArrayRef<unsigned> nodes)
906 : Matcher(EmitMergeInputChains), ChainNodes(nodes.begin(), nodes.end()) {}
908 unsigned getNumNodes() const { return ChainNodes.size(); }
910 unsigned getNode(unsigned i) const {
911 assert(i < ChainNodes.size());
912 return ChainNodes[i];
915 static inline bool classof(const Matcher *N) {
916 return N->getKind() == EmitMergeInputChains;
920 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
921 virtual bool isEqualImpl(const Matcher *M) const {
922 return cast<EmitMergeInputChainsMatcher>(M)->ChainNodes == ChainNodes;
924 virtual unsigned getHashImpl() const;
927 /// EmitCopyToRegMatcher - Emit a CopyToReg node from a value to a physreg,
928 /// pushing the chain and glue results.
930 class EmitCopyToRegMatcher : public Matcher {
931 unsigned SrcSlot; // Value to copy into the physreg.
934 EmitCopyToRegMatcher(unsigned srcSlot, Record *destPhysReg)
935 : Matcher(EmitCopyToReg), SrcSlot(srcSlot), DestPhysReg(destPhysReg) {}
937 unsigned getSrcSlot() const { return SrcSlot; }
938 Record *getDestPhysReg() const { return DestPhysReg; }
940 static inline bool classof(const Matcher *N) {
941 return N->getKind() == EmitCopyToReg;
945 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
946 virtual bool isEqualImpl(const Matcher *M) const {
947 return cast<EmitCopyToRegMatcher>(M)->SrcSlot == SrcSlot &&
948 cast<EmitCopyToRegMatcher>(M)->DestPhysReg == DestPhysReg;
950 virtual unsigned getHashImpl() const {
951 return SrcSlot ^ ((unsigned)(intptr_t)DestPhysReg << 4);
957 /// EmitNodeXFormMatcher - Emit an operation that runs an SDNodeXForm on a
958 /// recorded node and records the result.
959 class EmitNodeXFormMatcher : public Matcher {
963 EmitNodeXFormMatcher(unsigned slot, Record *nodeXForm)
964 : Matcher(EmitNodeXForm), Slot(slot), NodeXForm(nodeXForm) {}
966 unsigned getSlot() const { return Slot; }
967 Record *getNodeXForm() const { return NodeXForm; }
969 static inline bool classof(const Matcher *N) {
970 return N->getKind() == EmitNodeXForm;
974 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
975 virtual bool isEqualImpl(const Matcher *M) const {
976 return cast<EmitNodeXFormMatcher>(M)->Slot == Slot &&
977 cast<EmitNodeXFormMatcher>(M)->NodeXForm == NodeXForm;
979 virtual unsigned getHashImpl() const {
980 return Slot ^ ((unsigned)(intptr_t)NodeXForm << 4);
984 /// EmitNodeMatcherCommon - Common class shared between EmitNode and
986 class EmitNodeMatcherCommon : public Matcher {
987 std::string OpcodeName;
988 const SmallVector<MVT::SimpleValueType, 3> VTs;
989 const SmallVector<unsigned, 6> Operands;
990 bool HasChain, HasInGlue, HasOutGlue, HasMemRefs;
992 /// NumFixedArityOperands - If this is a fixed arity node, this is set to -1.
993 /// If this is a varidic node, this is set to the number of fixed arity
994 /// operands in the root of the pattern. The rest are appended to this node.
995 int NumFixedArityOperands;
997 EmitNodeMatcherCommon(const std::string &opcodeName,
998 ArrayRef<MVT::SimpleValueType> vts,
999 ArrayRef<unsigned> operands,
1000 bool hasChain, bool hasInGlue, bool hasOutGlue,
1002 int numfixedarityoperands, bool isMorphNodeTo)
1003 : Matcher(isMorphNodeTo ? MorphNodeTo : EmitNode), OpcodeName(opcodeName),
1004 VTs(vts.begin(), vts.end()), Operands(operands.begin(), operands.end()),
1005 HasChain(hasChain), HasInGlue(hasInGlue), HasOutGlue(hasOutGlue),
1006 HasMemRefs(hasmemrefs), NumFixedArityOperands(numfixedarityoperands) {}
1008 const std::string &getOpcodeName() const { return OpcodeName; }
1010 unsigned getNumVTs() const { return VTs.size(); }
1011 MVT::SimpleValueType getVT(unsigned i) const {
1012 assert(i < VTs.size());
1016 unsigned getNumOperands() const { return Operands.size(); }
1017 unsigned getOperand(unsigned i) const {
1018 assert(i < Operands.size());
1022 const SmallVectorImpl<MVT::SimpleValueType> &getVTList() const { return VTs; }
1023 const SmallVectorImpl<unsigned> &getOperandList() const { return Operands; }
1026 bool hasChain() const { return HasChain; }
1027 bool hasInFlag() const { return HasInGlue; }
1028 bool hasOutFlag() const { return HasOutGlue; }
1029 bool hasMemRefs() const { return HasMemRefs; }
1030 int getNumFixedArityOperands() const { return NumFixedArityOperands; }
1032 static inline bool classof(const Matcher *N) {
1033 return N->getKind() == EmitNode || N->getKind() == MorphNodeTo;
1037 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
1038 virtual bool isEqualImpl(const Matcher *M) const;
1039 virtual unsigned getHashImpl() const;
1042 /// EmitNodeMatcher - This signals a successful match and generates a node.
1043 class EmitNodeMatcher : public EmitNodeMatcherCommon {
1044 virtual void anchor();
1045 unsigned FirstResultSlot;
1047 EmitNodeMatcher(const std::string &opcodeName,
1048 ArrayRef<MVT::SimpleValueType> vts,
1049 ArrayRef<unsigned> operands,
1050 bool hasChain, bool hasInFlag, bool hasOutFlag,
1052 int numfixedarityoperands, unsigned firstresultslot)
1053 : EmitNodeMatcherCommon(opcodeName, vts, operands, hasChain,
1054 hasInFlag, hasOutFlag, hasmemrefs,
1055 numfixedarityoperands, false),
1056 FirstResultSlot(firstresultslot) {}
1058 unsigned getFirstResultSlot() const { return FirstResultSlot; }
1060 static inline bool classof(const Matcher *N) {
1061 return N->getKind() == EmitNode;
1066 class MorphNodeToMatcher : public EmitNodeMatcherCommon {
1067 virtual void anchor();
1068 const PatternToMatch &Pattern;
1070 MorphNodeToMatcher(const std::string &opcodeName,
1071 ArrayRef<MVT::SimpleValueType> vts,
1072 ArrayRef<unsigned> operands,
1073 bool hasChain, bool hasInFlag, bool hasOutFlag,
1075 int numfixedarityoperands, const PatternToMatch &pattern)
1076 : EmitNodeMatcherCommon(opcodeName, vts, operands, hasChain,
1077 hasInFlag, hasOutFlag, hasmemrefs,
1078 numfixedarityoperands, true),
1082 const PatternToMatch &getPattern() const { return Pattern; }
1084 static inline bool classof(const Matcher *N) {
1085 return N->getKind() == MorphNodeTo;
1089 /// MarkGlueResultsMatcher - This node indicates which non-root nodes in the
1090 /// pattern produce glue. This allows CompleteMatchMatcher to update them
1091 /// with the output glue of the resultant code.
1092 class MarkGlueResultsMatcher : public Matcher {
1093 SmallVector<unsigned, 3> GlueResultNodes;
1095 MarkGlueResultsMatcher(ArrayRef<unsigned> nodes)
1096 : Matcher(MarkGlueResults), GlueResultNodes(nodes.begin(), nodes.end()) {}
1098 unsigned getNumNodes() const { return GlueResultNodes.size(); }
1100 unsigned getNode(unsigned i) const {
1101 assert(i < GlueResultNodes.size());
1102 return GlueResultNodes[i];
1105 static inline bool classof(const Matcher *N) {
1106 return N->getKind() == MarkGlueResults;
1110 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
1111 virtual bool isEqualImpl(const Matcher *M) const {
1112 return cast<MarkGlueResultsMatcher>(M)->GlueResultNodes == GlueResultNodes;
1114 virtual unsigned getHashImpl() const;
1117 /// CompleteMatchMatcher - Complete a match by replacing the results of the
1118 /// pattern with the newly generated nodes. This also prints a comment
1119 /// indicating the source and dest patterns.
1120 class CompleteMatchMatcher : public Matcher {
1121 SmallVector<unsigned, 2> Results;
1122 const PatternToMatch &Pattern;
1124 CompleteMatchMatcher(ArrayRef<unsigned> results,
1125 const PatternToMatch &pattern)
1126 : Matcher(CompleteMatch), Results(results.begin(), results.end()),
1129 unsigned getNumResults() const { return Results.size(); }
1130 unsigned getResult(unsigned R) const { return Results[R]; }
1131 const PatternToMatch &getPattern() const { return Pattern; }
1133 static inline bool classof(const Matcher *N) {
1134 return N->getKind() == CompleteMatch;
1138 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
1139 virtual bool isEqualImpl(const Matcher *M) const {
1140 return cast<CompleteMatchMatcher>(M)->Results == Results &&
1141 &cast<CompleteMatchMatcher>(M)->Pattern == &Pattern;
1143 virtual unsigned getHashImpl() const;
1146 } // end namespace llvm