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()) {}
509 static inline bool classof(const Matcher *N) {
510 return N->getKind() == SwitchOpcode;
513 unsigned getNumCases() const { return Cases.size(); }
515 const SDNodeInfo &getCaseOpcode(unsigned i) const { return *Cases[i].first; }
516 Matcher *getCaseMatcher(unsigned i) { return Cases[i].second; }
517 const Matcher *getCaseMatcher(unsigned i) const { return Cases[i].second; }
520 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
521 virtual bool isEqualImpl(const Matcher *M) const { return false; }
522 virtual unsigned getHashImpl() const { return 4123; }
525 /// CheckTypeMatcher - This checks to see if the current node has the
526 /// specified type at the specified result, if not it fails to match.
527 class CheckTypeMatcher : public Matcher {
528 MVT::SimpleValueType Type;
531 CheckTypeMatcher(MVT::SimpleValueType type, unsigned resno)
532 : Matcher(CheckType), Type(type), ResNo(resno) {}
534 MVT::SimpleValueType getType() const { return Type; }
535 unsigned getResNo() const { return ResNo; }
537 static inline bool classof(const Matcher *N) {
538 return N->getKind() == CheckType;
541 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
544 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
545 virtual bool isEqualImpl(const Matcher *M) const {
546 return cast<CheckTypeMatcher>(M)->Type == Type;
548 virtual unsigned getHashImpl() const { return Type; }
549 virtual bool isContradictoryImpl(const Matcher *M) const;
552 /// SwitchTypeMatcher - Switch based on the current node's type, dispatching
553 /// to one matcher per case. If the type doesn't match any of the cases,
554 /// then the match fails. This is semantically equivalent to a Scope node where
555 /// every child does a CheckType, but is much faster.
556 class SwitchTypeMatcher : public Matcher {
557 SmallVector<std::pair<MVT::SimpleValueType, Matcher*>, 8> Cases;
559 SwitchTypeMatcher(ArrayRef<std::pair<MVT::SimpleValueType, Matcher*> > cases)
560 : Matcher(SwitchType), Cases(cases.begin(), cases.end()) {}
562 static inline bool classof(const Matcher *N) {
563 return N->getKind() == SwitchType;
566 unsigned getNumCases() const { return Cases.size(); }
568 MVT::SimpleValueType getCaseType(unsigned i) const { return Cases[i].first; }
569 Matcher *getCaseMatcher(unsigned i) { return Cases[i].second; }
570 const Matcher *getCaseMatcher(unsigned i) const { return Cases[i].second; }
573 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
574 virtual bool isEqualImpl(const Matcher *M) const { return false; }
575 virtual unsigned getHashImpl() const { return 4123; }
579 /// CheckChildTypeMatcher - This checks to see if a child node has the
580 /// specified type, if not it fails to match.
581 class CheckChildTypeMatcher : public Matcher {
583 MVT::SimpleValueType Type;
585 CheckChildTypeMatcher(unsigned childno, MVT::SimpleValueType type)
586 : Matcher(CheckChildType), ChildNo(childno), Type(type) {}
588 unsigned getChildNo() const { return ChildNo; }
589 MVT::SimpleValueType getType() const { return Type; }
591 static inline bool classof(const Matcher *N) {
592 return N->getKind() == CheckChildType;
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<CheckChildTypeMatcher>(M)->ChildNo == ChildNo &&
601 cast<CheckChildTypeMatcher>(M)->Type == Type;
603 virtual unsigned getHashImpl() const { return (Type << 3) | ChildNo; }
604 virtual bool isContradictoryImpl(const Matcher *M) const;
608 /// CheckIntegerMatcher - This checks to see if the current node is a
609 /// ConstantSDNode with the specified integer value, if not it fails to match.
610 class CheckIntegerMatcher : public Matcher {
613 CheckIntegerMatcher(int64_t value)
614 : Matcher(CheckInteger), Value(value) {}
616 int64_t getValue() const { return Value; }
618 static inline bool classof(const Matcher *N) {
619 return N->getKind() == CheckInteger;
622 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
625 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
626 virtual bool isEqualImpl(const Matcher *M) const {
627 return cast<CheckIntegerMatcher>(M)->Value == Value;
629 virtual unsigned getHashImpl() const { return Value; }
630 virtual bool isContradictoryImpl(const Matcher *M) const;
633 /// CheckCondCodeMatcher - This checks to see if the current node is a
634 /// CondCodeSDNode with the specified condition, if not it fails to match.
635 class CheckCondCodeMatcher : public Matcher {
636 StringRef CondCodeName;
638 CheckCondCodeMatcher(StringRef condcodename)
639 : Matcher(CheckCondCode), CondCodeName(condcodename) {}
641 StringRef getCondCodeName() const { return CondCodeName; }
643 static inline bool classof(const Matcher *N) {
644 return N->getKind() == CheckCondCode;
647 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
650 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
651 virtual bool isEqualImpl(const Matcher *M) const {
652 return cast<CheckCondCodeMatcher>(M)->CondCodeName == CondCodeName;
654 virtual unsigned getHashImpl() const;
657 /// CheckValueTypeMatcher - This checks to see if the current node is a
658 /// VTSDNode with the specified type, if not it fails to match.
659 class CheckValueTypeMatcher : public Matcher {
662 CheckValueTypeMatcher(StringRef type_name)
663 : Matcher(CheckValueType), TypeName(type_name) {}
665 StringRef getTypeName() const { return TypeName; }
667 static inline bool classof(const Matcher *N) {
668 return N->getKind() == CheckValueType;
671 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
674 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
675 virtual bool isEqualImpl(const Matcher *M) const {
676 return cast<CheckValueTypeMatcher>(M)->TypeName == TypeName;
678 virtual unsigned getHashImpl() const;
679 bool isContradictoryImpl(const Matcher *M) const;
684 /// CheckComplexPatMatcher - This node runs the specified ComplexPattern on
685 /// the current node.
686 class CheckComplexPatMatcher : public Matcher {
687 const ComplexPattern &Pattern;
689 /// MatchNumber - This is the recorded nodes slot that contains the node we
690 /// want to match against.
691 unsigned MatchNumber;
693 /// Name - The name of the node we're matching, for comment emission.
696 /// FirstResult - This is the first slot in the RecordedNodes list that the
697 /// result of the match populates.
698 unsigned FirstResult;
700 CheckComplexPatMatcher(const ComplexPattern &pattern, unsigned matchnumber,
701 const std::string &name, unsigned firstresult)
702 : Matcher(CheckComplexPat), Pattern(pattern), MatchNumber(matchnumber),
703 Name(name), FirstResult(firstresult) {}
705 const ComplexPattern &getPattern() const { return Pattern; }
706 unsigned getMatchNumber() const { return MatchNumber; }
708 const std::string getName() const { return Name; }
709 unsigned getFirstResult() const { return FirstResult; }
711 static inline bool classof(const Matcher *N) {
712 return N->getKind() == CheckComplexPat;
715 // Not safe to move a pattern predicate past a complex pattern.
716 virtual bool isSafeToReorderWithPatternPredicate() const { return false; }
719 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
720 virtual bool isEqualImpl(const Matcher *M) const {
721 return &cast<CheckComplexPatMatcher>(M)->Pattern == &Pattern &&
722 cast<CheckComplexPatMatcher>(M)->MatchNumber == MatchNumber;
724 virtual unsigned getHashImpl() const {
725 return (unsigned)(intptr_t)&Pattern ^ MatchNumber;
729 /// CheckAndImmMatcher - This checks to see if the current node is an 'and'
730 /// with something equivalent to the specified immediate.
731 class CheckAndImmMatcher : public Matcher {
734 CheckAndImmMatcher(int64_t value)
735 : Matcher(CheckAndImm), Value(value) {}
737 int64_t getValue() const { return Value; }
739 static inline bool classof(const Matcher *N) {
740 return N->getKind() == CheckAndImm;
743 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
746 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
747 virtual bool isEqualImpl(const Matcher *M) const {
748 return cast<CheckAndImmMatcher>(M)->Value == Value;
750 virtual unsigned getHashImpl() const { return Value; }
753 /// CheckOrImmMatcher - This checks to see if the current node is an 'and'
754 /// with something equivalent to the specified immediate.
755 class CheckOrImmMatcher : public Matcher {
758 CheckOrImmMatcher(int64_t value)
759 : Matcher(CheckOrImm), Value(value) {}
761 int64_t getValue() const { return Value; }
763 static inline bool classof(const Matcher *N) {
764 return N->getKind() == CheckOrImm;
767 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
770 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
771 virtual bool isEqualImpl(const Matcher *M) const {
772 return cast<CheckOrImmMatcher>(M)->Value == Value;
774 virtual unsigned getHashImpl() const { return Value; }
777 /// CheckFoldableChainNodeMatcher - This checks to see if the current node
778 /// (which defines a chain operand) is safe to fold into a larger pattern.
779 class CheckFoldableChainNodeMatcher : public Matcher {
781 CheckFoldableChainNodeMatcher()
782 : Matcher(CheckFoldableChainNode) {}
784 static inline bool classof(const Matcher *N) {
785 return N->getKind() == CheckFoldableChainNode;
788 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
791 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
792 virtual bool isEqualImpl(const Matcher *M) const { return true; }
793 virtual unsigned getHashImpl() const { return 0; }
796 /// EmitIntegerMatcher - This creates a new TargetConstant.
797 class EmitIntegerMatcher : public Matcher {
799 MVT::SimpleValueType VT;
801 EmitIntegerMatcher(int64_t val, MVT::SimpleValueType vt)
802 : Matcher(EmitInteger), Val(val), VT(vt) {}
804 int64_t getValue() const { return Val; }
805 MVT::SimpleValueType getVT() const { return VT; }
807 static inline bool classof(const Matcher *N) {
808 return N->getKind() == EmitInteger;
812 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
813 virtual bool isEqualImpl(const Matcher *M) const {
814 return cast<EmitIntegerMatcher>(M)->Val == Val &&
815 cast<EmitIntegerMatcher>(M)->VT == VT;
817 virtual unsigned getHashImpl() const { return (Val << 4) | VT; }
820 /// EmitStringIntegerMatcher - A target constant whose value is represented
822 class EmitStringIntegerMatcher : public Matcher {
824 MVT::SimpleValueType VT;
826 EmitStringIntegerMatcher(const std::string &val, MVT::SimpleValueType vt)
827 : Matcher(EmitStringInteger), Val(val), VT(vt) {}
829 const std::string &getValue() const { return Val; }
830 MVT::SimpleValueType getVT() const { return VT; }
832 static inline bool classof(const Matcher *N) {
833 return N->getKind() == EmitStringInteger;
837 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
838 virtual bool isEqualImpl(const Matcher *M) const {
839 return cast<EmitStringIntegerMatcher>(M)->Val == Val &&
840 cast<EmitStringIntegerMatcher>(M)->VT == VT;
842 virtual unsigned getHashImpl() const;
845 /// EmitRegisterMatcher - This creates a new TargetConstant.
846 class EmitRegisterMatcher : public Matcher {
847 /// Reg - The def for the register that we're emitting. If this is null, then
848 /// this is a reference to zero_reg.
849 const CodeGenRegister *Reg;
850 MVT::SimpleValueType VT;
852 EmitRegisterMatcher(const CodeGenRegister *reg, MVT::SimpleValueType vt)
853 : Matcher(EmitRegister), Reg(reg), VT(vt) {}
855 const CodeGenRegister *getReg() const { return Reg; }
856 MVT::SimpleValueType getVT() const { return VT; }
858 static inline bool classof(const Matcher *N) {
859 return N->getKind() == EmitRegister;
863 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
864 virtual bool isEqualImpl(const Matcher *M) const {
865 return cast<EmitRegisterMatcher>(M)->Reg == Reg &&
866 cast<EmitRegisterMatcher>(M)->VT == VT;
868 virtual unsigned getHashImpl() const {
869 return ((unsigned)(intptr_t)Reg) << 4 | VT;
873 /// EmitConvertToTargetMatcher - Emit an operation that reads a specified
874 /// recorded node and converts it from being a ISD::Constant to
875 /// ISD::TargetConstant, likewise for ConstantFP.
876 class EmitConvertToTargetMatcher : public Matcher {
879 EmitConvertToTargetMatcher(unsigned slot)
880 : Matcher(EmitConvertToTarget), Slot(slot) {}
882 unsigned getSlot() const { return Slot; }
884 static inline bool classof(const Matcher *N) {
885 return N->getKind() == EmitConvertToTarget;
889 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
890 virtual bool isEqualImpl(const Matcher *M) const {
891 return cast<EmitConvertToTargetMatcher>(M)->Slot == Slot;
893 virtual unsigned getHashImpl() const { return Slot; }
896 /// EmitMergeInputChainsMatcher - Emit a node that merges a list of input
897 /// chains together with a token factor. The list of nodes are the nodes in the
898 /// matched pattern that have chain input/outputs. This node adds all input
899 /// chains of these nodes if they are not themselves a node in the pattern.
900 class EmitMergeInputChainsMatcher : public Matcher {
901 SmallVector<unsigned, 3> ChainNodes;
903 EmitMergeInputChainsMatcher(ArrayRef<unsigned> nodes)
904 : Matcher(EmitMergeInputChains), ChainNodes(nodes.begin(), nodes.end()) {}
906 unsigned getNumNodes() const { return ChainNodes.size(); }
908 unsigned getNode(unsigned i) const {
909 assert(i < ChainNodes.size());
910 return ChainNodes[i];
913 static inline bool classof(const Matcher *N) {
914 return N->getKind() == EmitMergeInputChains;
918 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
919 virtual bool isEqualImpl(const Matcher *M) const {
920 return cast<EmitMergeInputChainsMatcher>(M)->ChainNodes == ChainNodes;
922 virtual unsigned getHashImpl() const;
925 /// EmitCopyToRegMatcher - Emit a CopyToReg node from a value to a physreg,
926 /// pushing the chain and glue results.
928 class EmitCopyToRegMatcher : public Matcher {
929 unsigned SrcSlot; // Value to copy into the physreg.
932 EmitCopyToRegMatcher(unsigned srcSlot, Record *destPhysReg)
933 : Matcher(EmitCopyToReg), SrcSlot(srcSlot), DestPhysReg(destPhysReg) {}
935 unsigned getSrcSlot() const { return SrcSlot; }
936 Record *getDestPhysReg() const { return DestPhysReg; }
938 static inline bool classof(const Matcher *N) {
939 return N->getKind() == EmitCopyToReg;
943 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
944 virtual bool isEqualImpl(const Matcher *M) const {
945 return cast<EmitCopyToRegMatcher>(M)->SrcSlot == SrcSlot &&
946 cast<EmitCopyToRegMatcher>(M)->DestPhysReg == DestPhysReg;
948 virtual unsigned getHashImpl() const {
949 return SrcSlot ^ ((unsigned)(intptr_t)DestPhysReg << 4);
955 /// EmitNodeXFormMatcher - Emit an operation that runs an SDNodeXForm on a
956 /// recorded node and records the result.
957 class EmitNodeXFormMatcher : public Matcher {
961 EmitNodeXFormMatcher(unsigned slot, Record *nodeXForm)
962 : Matcher(EmitNodeXForm), Slot(slot), NodeXForm(nodeXForm) {}
964 unsigned getSlot() const { return Slot; }
965 Record *getNodeXForm() const { return NodeXForm; }
967 static inline bool classof(const Matcher *N) {
968 return N->getKind() == EmitNodeXForm;
972 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
973 virtual bool isEqualImpl(const Matcher *M) const {
974 return cast<EmitNodeXFormMatcher>(M)->Slot == Slot &&
975 cast<EmitNodeXFormMatcher>(M)->NodeXForm == NodeXForm;
977 virtual unsigned getHashImpl() const {
978 return Slot ^ ((unsigned)(intptr_t)NodeXForm << 4);
982 /// EmitNodeMatcherCommon - Common class shared between EmitNode and
984 class EmitNodeMatcherCommon : public Matcher {
985 std::string OpcodeName;
986 const SmallVector<MVT::SimpleValueType, 3> VTs;
987 const SmallVector<unsigned, 6> Operands;
988 bool HasChain, HasInGlue, HasOutGlue, HasMemRefs;
990 /// NumFixedArityOperands - If this is a fixed arity node, this is set to -1.
991 /// If this is a varidic node, this is set to the number of fixed arity
992 /// operands in the root of the pattern. The rest are appended to this node.
993 int NumFixedArityOperands;
995 EmitNodeMatcherCommon(const std::string &opcodeName,
996 ArrayRef<MVT::SimpleValueType> vts,
997 ArrayRef<unsigned> operands,
998 bool hasChain, bool hasInGlue, bool hasOutGlue,
1000 int numfixedarityoperands, bool isMorphNodeTo)
1001 : Matcher(isMorphNodeTo ? MorphNodeTo : EmitNode), OpcodeName(opcodeName),
1002 VTs(vts.begin(), vts.end()), Operands(operands.begin(), operands.end()),
1003 HasChain(hasChain), HasInGlue(hasInGlue), HasOutGlue(hasOutGlue),
1004 HasMemRefs(hasmemrefs), NumFixedArityOperands(numfixedarityoperands) {}
1006 const std::string &getOpcodeName() const { return OpcodeName; }
1008 unsigned getNumVTs() const { return VTs.size(); }
1009 MVT::SimpleValueType getVT(unsigned i) const {
1010 assert(i < VTs.size());
1014 unsigned getNumOperands() const { return Operands.size(); }
1015 unsigned getOperand(unsigned i) const {
1016 assert(i < Operands.size());
1020 const SmallVectorImpl<MVT::SimpleValueType> &getVTList() const { return VTs; }
1021 const SmallVectorImpl<unsigned> &getOperandList() const { return Operands; }
1024 bool hasChain() const { return HasChain; }
1025 bool hasInFlag() const { return HasInGlue; }
1026 bool hasOutFlag() const { return HasOutGlue; }
1027 bool hasMemRefs() const { return HasMemRefs; }
1028 int getNumFixedArityOperands() const { return NumFixedArityOperands; }
1030 static inline bool classof(const Matcher *N) {
1031 return N->getKind() == EmitNode || N->getKind() == MorphNodeTo;
1035 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
1036 virtual bool isEqualImpl(const Matcher *M) const;
1037 virtual unsigned getHashImpl() const;
1040 /// EmitNodeMatcher - This signals a successful match and generates a node.
1041 class EmitNodeMatcher : public EmitNodeMatcherCommon {
1042 virtual void anchor();
1043 unsigned FirstResultSlot;
1045 EmitNodeMatcher(const std::string &opcodeName,
1046 ArrayRef<MVT::SimpleValueType> vts,
1047 ArrayRef<unsigned> operands,
1048 bool hasChain, bool hasInFlag, bool hasOutFlag,
1050 int numfixedarityoperands, unsigned firstresultslot)
1051 : EmitNodeMatcherCommon(opcodeName, vts, operands, hasChain,
1052 hasInFlag, hasOutFlag, hasmemrefs,
1053 numfixedarityoperands, false),
1054 FirstResultSlot(firstresultslot) {}
1056 unsigned getFirstResultSlot() const { return FirstResultSlot; }
1058 static inline bool classof(const Matcher *N) {
1059 return N->getKind() == EmitNode;
1064 class MorphNodeToMatcher : public EmitNodeMatcherCommon {
1065 virtual void anchor();
1066 const PatternToMatch &Pattern;
1068 MorphNodeToMatcher(const std::string &opcodeName,
1069 ArrayRef<MVT::SimpleValueType> vts,
1070 ArrayRef<unsigned> operands,
1071 bool hasChain, bool hasInFlag, bool hasOutFlag,
1073 int numfixedarityoperands, const PatternToMatch &pattern)
1074 : EmitNodeMatcherCommon(opcodeName, vts, operands, hasChain,
1075 hasInFlag, hasOutFlag, hasmemrefs,
1076 numfixedarityoperands, true),
1080 const PatternToMatch &getPattern() const { return Pattern; }
1082 static inline bool classof(const Matcher *N) {
1083 return N->getKind() == MorphNodeTo;
1087 /// MarkGlueResultsMatcher - This node indicates which non-root nodes in the
1088 /// pattern produce glue. This allows CompleteMatchMatcher to update them
1089 /// with the output glue of the resultant code.
1090 class MarkGlueResultsMatcher : public Matcher {
1091 SmallVector<unsigned, 3> GlueResultNodes;
1093 MarkGlueResultsMatcher(ArrayRef<unsigned> nodes)
1094 : Matcher(MarkGlueResults), GlueResultNodes(nodes.begin(), nodes.end()) {}
1096 unsigned getNumNodes() const { return GlueResultNodes.size(); }
1098 unsigned getNode(unsigned i) const {
1099 assert(i < GlueResultNodes.size());
1100 return GlueResultNodes[i];
1103 static inline bool classof(const Matcher *N) {
1104 return N->getKind() == MarkGlueResults;
1108 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
1109 virtual bool isEqualImpl(const Matcher *M) const {
1110 return cast<MarkGlueResultsMatcher>(M)->GlueResultNodes == GlueResultNodes;
1112 virtual unsigned getHashImpl() const;
1115 /// CompleteMatchMatcher - Complete a match by replacing the results of the
1116 /// pattern with the newly generated nodes. This also prints a comment
1117 /// indicating the source and dest patterns.
1118 class CompleteMatchMatcher : public Matcher {
1119 SmallVector<unsigned, 2> Results;
1120 const PatternToMatch &Pattern;
1122 CompleteMatchMatcher(ArrayRef<unsigned> results,
1123 const PatternToMatch &pattern)
1124 : Matcher(CompleteMatch), Results(results.begin(), results.end()),
1127 unsigned getNumResults() const { return Results.size(); }
1128 unsigned getResult(unsigned R) const { return Results[R]; }
1129 const PatternToMatch &getPattern() const { return Pattern; }
1131 static inline bool classof(const Matcher *N) {
1132 return N->getKind() == CompleteMatch;
1136 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
1137 virtual bool isEqualImpl(const Matcher *M) const {
1138 return cast<CompleteMatchMatcher>(M)->Results == Results &&
1139 &cast<CompleteMatchMatcher>(M)->Pattern == &Pattern;
1141 virtual unsigned getHashImpl() const;
1144 } // end namespace llvm