1 //===- DAGISelMatcher.h - Representation of DAG pattern matcher -*- C++ -*-===//
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 LLVM_UTILS_TABLEGEN_DAGISELMATCHER_H
11 #define LLVM_UTILS_TABLEGEN_DAGISELMATCHER_H
13 #include "llvm/ADT/ArrayRef.h"
14 #include "llvm/ADT/SmallVector.h"
15 #include "llvm/ADT/StringRef.h"
16 #include "llvm/CodeGen/MachineValueType.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 void OptimizeMatcher(std::unique_ptr<Matcher> &Matcher,
34 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 std::unique_ptr<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 CheckChildInteger, // Fail if child is wrong val.
69 CheckCondCode, // Fail if not condcode.
74 CheckFoldableChainNode,
76 // Node creation/emisssion.
77 EmitInteger, // Create a TargetConstant
78 EmitStringInteger, // Create a TargetConstant from a string.
79 EmitRegister, // Create a register.
80 EmitConvertToTarget, // Convert a imm/fpimm to target imm/fpimm
81 EmitMergeInputChains, // Merge together a chains for an input.
82 EmitCopyToReg, // Emit a copytoreg into a physreg.
83 EmitNode, // Create a DAG node
84 EmitNodeXForm, // Run a SDNodeXForm
85 MarkGlueResults, // Indicate which interior nodes have glue results.
86 CompleteMatch, // Finish a match and update the results.
87 MorphNodeTo // Build a node, finish a match and update results.
92 Matcher(KindTy K) : Kind(K) {}
96 KindTy getKind() const { return Kind; }
98 Matcher *getNext() { return Next.get(); }
99 const Matcher *getNext() const { return Next.get(); }
100 void setNext(Matcher *C) { Next.reset(C); }
101 Matcher *takeNext() { return Next.release(); }
103 std::unique_ptr<Matcher> &getNextPtr() { return Next; }
105 bool isEqual(const Matcher *M) const {
106 if (getKind() != M->getKind()) return false;
107 return isEqualImpl(M);
110 unsigned getHash() const {
111 // Clear the high bit so we don't conflict with tombstones etc.
112 return ((getHashImpl() << 4) ^ getKind()) & (~0U>>1);
115 /// isSafeToReorderWithPatternPredicate - Return true if it is safe to sink a
116 /// PatternPredicate node past this one.
117 virtual bool isSafeToReorderWithPatternPredicate() const {
121 /// isSimplePredicateNode - Return true if this is a simple predicate that
122 /// operates on the node or its children without potential side effects or a
123 /// change of the current node.
124 bool isSimplePredicateNode() const {
126 default: return false;
129 case CheckPatternPredicate:
135 case CheckChildInteger:
140 case CheckFoldableChainNode:
145 /// isSimplePredicateOrRecordNode - Return true if this is a record node or
146 /// a simple predicate.
147 bool isSimplePredicateOrRecordNode() const {
148 return isSimplePredicateNode() ||
149 getKind() == RecordNode || getKind() == RecordChild;
152 /// unlinkNode - Unlink the specified node from this chain. If Other == this,
153 /// we unlink the next pointer and return it. Otherwise we unlink Other from
154 /// the list and return this.
155 Matcher *unlinkNode(Matcher *Other);
157 /// canMoveBefore - Return true if this matcher is the same as Other, or if
158 /// we can move this matcher past all of the nodes in-between Other and this
159 /// node. Other must be equal to or before this.
160 bool canMoveBefore(const Matcher *Other) const;
162 /// canMoveBeforeNode - Return true if it is safe to move the current matcher
163 /// across the specified one.
164 bool canMoveBeforeNode(const Matcher *Other) const;
166 /// isContradictory - Return true of these two matchers could never match on
168 bool isContradictory(const Matcher *Other) const {
169 // Since this predicate is reflexive, we canonicalize the ordering so that
170 // we always match a node against nodes with kinds that are greater or equal
171 // to them. For example, we'll pass in a CheckType node as an argument to
172 // the CheckOpcode method, not the other way around.
173 if (getKind() < Other->getKind())
174 return isContradictoryImpl(Other);
175 return Other->isContradictoryImpl(this);
178 void print(raw_ostream &OS, unsigned indent = 0) const;
179 void printOne(raw_ostream &OS) const;
182 virtual void printImpl(raw_ostream &OS, unsigned indent) const = 0;
183 virtual bool isEqualImpl(const Matcher *M) const = 0;
184 virtual unsigned getHashImpl() const = 0;
185 virtual bool isContradictoryImpl(const Matcher *M) const { return false; }
188 /// ScopeMatcher - This attempts to match each of its children to find the first
189 /// one that successfully matches. If one child fails, it tries the next child.
190 /// If none of the children match then this check fails. It never has a 'next'.
191 class ScopeMatcher : public Matcher {
192 SmallVector<Matcher*, 4> Children;
194 ScopeMatcher(ArrayRef<Matcher *> children)
195 : Matcher(Scope), Children(children.begin(), children.end()) {
197 virtual ~ScopeMatcher();
199 unsigned getNumChildren() const { return Children.size(); }
201 Matcher *getChild(unsigned i) { return Children[i]; }
202 const Matcher *getChild(unsigned i) const { return Children[i]; }
204 void resetChild(unsigned i, Matcher *N) {
209 Matcher *takeChild(unsigned i) {
210 Matcher *Res = Children[i];
211 Children[i] = nullptr;
215 void setNumChildren(unsigned NC) {
216 if (NC < Children.size()) {
217 // delete any children we're about to lose pointers to.
218 for (unsigned i = NC, e = Children.size(); i != e; ++i)
224 static inline bool classof(const Matcher *N) {
225 return N->getKind() == Scope;
229 void printImpl(raw_ostream &OS, unsigned indent) const override;
230 bool isEqualImpl(const Matcher *M) const override { return false; }
231 unsigned getHashImpl() const override { return 12312; }
234 /// RecordMatcher - Save the current node in the operand list.
235 class RecordMatcher : public Matcher {
236 /// WhatFor - This is a string indicating why we're recording this. This
237 /// should only be used for comment generation not anything semantic.
240 /// ResultNo - The slot number in the RecordedNodes vector that this will be,
241 /// just printed as a comment.
244 RecordMatcher(const std::string &whatfor, unsigned resultNo)
245 : Matcher(RecordNode), WhatFor(whatfor), ResultNo(resultNo) {}
247 const std::string &getWhatFor() const { return WhatFor; }
248 unsigned getResultNo() const { return ResultNo; }
250 static inline bool classof(const Matcher *N) {
251 return N->getKind() == RecordNode;
254 bool isSafeToReorderWithPatternPredicate() const override { return true; }
256 void printImpl(raw_ostream &OS, unsigned indent) const override;
257 bool isEqualImpl(const Matcher *M) const override { return true; }
258 unsigned getHashImpl() const override { return 0; }
261 /// RecordChildMatcher - Save a numbered child of the current node, or fail
262 /// the match if it doesn't exist. This is logically equivalent to:
263 /// MoveChild N + RecordNode + MoveParent.
264 class RecordChildMatcher : public Matcher {
267 /// WhatFor - This is a string indicating why we're recording this. This
268 /// should only be used for comment generation not anything semantic.
271 /// ResultNo - The slot number in the RecordedNodes vector that this will be,
272 /// just printed as a comment.
275 RecordChildMatcher(unsigned childno, const std::string &whatfor,
277 : Matcher(RecordChild), ChildNo(childno), WhatFor(whatfor),
278 ResultNo(resultNo) {}
280 unsigned getChildNo() const { return ChildNo; }
281 const std::string &getWhatFor() const { return WhatFor; }
282 unsigned getResultNo() const { return ResultNo; }
284 static inline bool classof(const Matcher *N) {
285 return N->getKind() == RecordChild;
288 bool isSafeToReorderWithPatternPredicate() const override { return true; }
291 void printImpl(raw_ostream &OS, unsigned indent) const override;
292 bool isEqualImpl(const Matcher *M) const override {
293 return cast<RecordChildMatcher>(M)->getChildNo() == getChildNo();
295 unsigned getHashImpl() const override { return getChildNo(); }
298 /// RecordMemRefMatcher - Save the current node's memref.
299 class RecordMemRefMatcher : public Matcher {
301 RecordMemRefMatcher() : Matcher(RecordMemRef) {}
303 static inline bool classof(const Matcher *N) {
304 return N->getKind() == RecordMemRef;
307 bool isSafeToReorderWithPatternPredicate() const override { return true; }
310 void printImpl(raw_ostream &OS, unsigned indent) const override;
311 bool isEqualImpl(const Matcher *M) const override { return true; }
312 unsigned getHashImpl() const override { return 0; }
316 /// CaptureGlueInputMatcher - If the current record has a glue input, record
317 /// it so that it is used as an input to the generated code.
318 class CaptureGlueInputMatcher : public Matcher {
320 CaptureGlueInputMatcher() : Matcher(CaptureGlueInput) {}
322 static inline bool classof(const Matcher *N) {
323 return N->getKind() == CaptureGlueInput;
326 bool isSafeToReorderWithPatternPredicate() const override { return true; }
329 void printImpl(raw_ostream &OS, unsigned indent) const override;
330 bool isEqualImpl(const Matcher *M) const override { return true; }
331 unsigned getHashImpl() const override { return 0; }
334 /// MoveChildMatcher - This tells the interpreter to move into the
335 /// specified child node.
336 class MoveChildMatcher : public Matcher {
339 MoveChildMatcher(unsigned childNo) : Matcher(MoveChild), ChildNo(childNo) {}
341 unsigned getChildNo() const { return ChildNo; }
343 static inline bool classof(const Matcher *N) {
344 return N->getKind() == MoveChild;
347 bool isSafeToReorderWithPatternPredicate() const override { return true; }
350 void printImpl(raw_ostream &OS, unsigned indent) const override;
351 bool isEqualImpl(const Matcher *M) const override {
352 return cast<MoveChildMatcher>(M)->getChildNo() == getChildNo();
354 unsigned getHashImpl() const override { return getChildNo(); }
357 /// MoveParentMatcher - This tells the interpreter to move to the parent
358 /// of the current node.
359 class MoveParentMatcher : public Matcher {
361 MoveParentMatcher() : Matcher(MoveParent) {}
363 static inline bool classof(const Matcher *N) {
364 return N->getKind() == MoveParent;
367 bool isSafeToReorderWithPatternPredicate() const override { return true; }
370 void printImpl(raw_ostream &OS, unsigned indent) const override;
371 bool isEqualImpl(const Matcher *M) const override { return true; }
372 unsigned getHashImpl() const override { return 0; }
375 /// CheckSameMatcher - This checks to see if this node is exactly the same
376 /// node as the specified match that was recorded with 'Record'. This is used
377 /// when patterns have the same name in them, like '(mul GPR:$in, GPR:$in)'.
378 class CheckSameMatcher : public Matcher {
379 unsigned MatchNumber;
381 CheckSameMatcher(unsigned matchnumber)
382 : Matcher(CheckSame), MatchNumber(matchnumber) {}
384 unsigned getMatchNumber() const { return MatchNumber; }
386 static inline bool classof(const Matcher *N) {
387 return N->getKind() == CheckSame;
390 bool isSafeToReorderWithPatternPredicate() const override { return true; }
393 void printImpl(raw_ostream &OS, unsigned indent) const override;
394 bool isEqualImpl(const Matcher *M) const override {
395 return cast<CheckSameMatcher>(M)->getMatchNumber() == getMatchNumber();
397 unsigned getHashImpl() const override { return getMatchNumber(); }
400 /// CheckChildSameMatcher - This checks to see if child node is exactly the same
401 /// node as the specified match that was recorded with 'Record'. This is used
402 /// when patterns have the same name in them, like '(mul GPR:$in, GPR:$in)'.
403 class CheckChildSameMatcher : public Matcher {
405 unsigned MatchNumber;
407 CheckChildSameMatcher(unsigned childno, unsigned matchnumber)
408 : Matcher(CheckChildSame), ChildNo(childno), MatchNumber(matchnumber) {}
410 unsigned getChildNo() const { return ChildNo; }
411 unsigned getMatchNumber() const { return MatchNumber; }
413 static inline bool classof(const Matcher *N) {
414 return N->getKind() == CheckChildSame;
417 bool isSafeToReorderWithPatternPredicate() const override { return true; }
420 void printImpl(raw_ostream &OS, unsigned indent) const override;
421 bool isEqualImpl(const Matcher *M) const override {
422 return cast<CheckChildSameMatcher>(M)->ChildNo == ChildNo &&
423 cast<CheckChildSameMatcher>(M)->MatchNumber == MatchNumber;
425 unsigned getHashImpl() const override { return (MatchNumber << 2) | ChildNo; }
428 /// CheckPatternPredicateMatcher - This checks the target-specific predicate
429 /// to see if the entire pattern is capable of matching. This predicate does
430 /// not take a node as input. This is used for subtarget feature checks etc.
431 class CheckPatternPredicateMatcher : public Matcher {
432 std::string Predicate;
434 CheckPatternPredicateMatcher(StringRef predicate)
435 : Matcher(CheckPatternPredicate), Predicate(predicate) {}
437 StringRef getPredicate() const { return Predicate; }
439 static inline bool classof(const Matcher *N) {
440 return N->getKind() == CheckPatternPredicate;
443 bool isSafeToReorderWithPatternPredicate() const override { return true; }
446 void printImpl(raw_ostream &OS, unsigned indent) const override;
447 bool isEqualImpl(const Matcher *M) const override {
448 return cast<CheckPatternPredicateMatcher>(M)->getPredicate() == Predicate;
450 unsigned getHashImpl() const override;
453 /// CheckPredicateMatcher - This checks the target-specific predicate to
454 /// see if the node is acceptable.
455 class CheckPredicateMatcher : public Matcher {
458 CheckPredicateMatcher(const TreePredicateFn &pred);
460 TreePredicateFn getPredicate() const;
462 static inline bool classof(const Matcher *N) {
463 return N->getKind() == CheckPredicate;
467 //virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
470 void printImpl(raw_ostream &OS, unsigned indent) const override;
471 bool isEqualImpl(const Matcher *M) const override {
472 return cast<CheckPredicateMatcher>(M)->Pred == Pred;
474 unsigned getHashImpl() const override;
478 /// CheckOpcodeMatcher - This checks to see if the current node has the
479 /// specified opcode, if not it fails to match.
480 class CheckOpcodeMatcher : public Matcher {
481 const SDNodeInfo &Opcode;
483 CheckOpcodeMatcher(const SDNodeInfo &opcode)
484 : Matcher(CheckOpcode), Opcode(opcode) {}
486 const SDNodeInfo &getOpcode() const { return Opcode; }
488 static inline bool classof(const Matcher *N) {
489 return N->getKind() == CheckOpcode;
492 bool isSafeToReorderWithPatternPredicate() const override { return true; }
495 void printImpl(raw_ostream &OS, unsigned indent) const override;
496 bool isEqualImpl(const Matcher *M) const override;
497 unsigned getHashImpl() const override;
498 bool isContradictoryImpl(const Matcher *M) const override;
501 /// SwitchOpcodeMatcher - Switch based on the current node's opcode, dispatching
502 /// to one matcher per opcode. If the opcode doesn't match any of the cases,
503 /// then the match fails. This is semantically equivalent to a Scope node where
504 /// every child does a CheckOpcode, but is much faster.
505 class SwitchOpcodeMatcher : public Matcher {
506 SmallVector<std::pair<const SDNodeInfo*, Matcher*>, 8> Cases;
508 SwitchOpcodeMatcher(ArrayRef<std::pair<const SDNodeInfo*, Matcher*> > cases)
509 : Matcher(SwitchOpcode), Cases(cases.begin(), cases.end()) {}
510 virtual ~SwitchOpcodeMatcher();
512 static inline bool classof(const Matcher *N) {
513 return N->getKind() == SwitchOpcode;
516 unsigned getNumCases() const { return Cases.size(); }
518 const SDNodeInfo &getCaseOpcode(unsigned i) const { return *Cases[i].first; }
519 Matcher *getCaseMatcher(unsigned i) { return Cases[i].second; }
520 const Matcher *getCaseMatcher(unsigned i) const { return Cases[i].second; }
523 void printImpl(raw_ostream &OS, unsigned indent) const override;
524 bool isEqualImpl(const Matcher *M) const override { return false; }
525 unsigned getHashImpl() const override { return 4123; }
528 /// CheckTypeMatcher - This checks to see if the current node has the
529 /// specified type at the specified result, if not it fails to match.
530 class CheckTypeMatcher : public Matcher {
531 MVT::SimpleValueType Type;
534 CheckTypeMatcher(MVT::SimpleValueType type, unsigned resno)
535 : Matcher(CheckType), Type(type), ResNo(resno) {}
537 MVT::SimpleValueType getType() const { return Type; }
538 unsigned getResNo() const { return ResNo; }
540 static inline bool classof(const Matcher *N) {
541 return N->getKind() == CheckType;
544 bool isSafeToReorderWithPatternPredicate() const override { return true; }
547 void printImpl(raw_ostream &OS, unsigned indent) const override;
548 bool isEqualImpl(const Matcher *M) const override {
549 return cast<CheckTypeMatcher>(M)->Type == Type;
551 unsigned getHashImpl() const override { return Type; }
552 bool isContradictoryImpl(const Matcher *M) const override;
555 /// SwitchTypeMatcher - Switch based on the current node's type, dispatching
556 /// to one matcher per case. If the type doesn't match any of the cases,
557 /// then the match fails. This is semantically equivalent to a Scope node where
558 /// every child does a CheckType, but is much faster.
559 class SwitchTypeMatcher : public Matcher {
560 SmallVector<std::pair<MVT::SimpleValueType, Matcher*>, 8> Cases;
562 SwitchTypeMatcher(ArrayRef<std::pair<MVT::SimpleValueType, Matcher*> > cases)
563 : Matcher(SwitchType), Cases(cases.begin(), cases.end()) {}
564 virtual ~SwitchTypeMatcher();
566 static inline bool classof(const Matcher *N) {
567 return N->getKind() == SwitchType;
570 unsigned getNumCases() const { return Cases.size(); }
572 MVT::SimpleValueType getCaseType(unsigned i) const { return Cases[i].first; }
573 Matcher *getCaseMatcher(unsigned i) { return Cases[i].second; }
574 const Matcher *getCaseMatcher(unsigned i) const { return Cases[i].second; }
577 void printImpl(raw_ostream &OS, unsigned indent) const override;
578 bool isEqualImpl(const Matcher *M) const override { return false; }
579 unsigned getHashImpl() const override { return 4123; }
583 /// CheckChildTypeMatcher - This checks to see if a child node has the
584 /// specified type, if not it fails to match.
585 class CheckChildTypeMatcher : public Matcher {
587 MVT::SimpleValueType Type;
589 CheckChildTypeMatcher(unsigned childno, MVT::SimpleValueType type)
590 : Matcher(CheckChildType), ChildNo(childno), Type(type) {}
592 unsigned getChildNo() const { return ChildNo; }
593 MVT::SimpleValueType getType() const { return Type; }
595 static inline bool classof(const Matcher *N) {
596 return N->getKind() == CheckChildType;
599 bool isSafeToReorderWithPatternPredicate() const override { return true; }
602 void printImpl(raw_ostream &OS, unsigned indent) const override;
603 bool isEqualImpl(const Matcher *M) const override {
604 return cast<CheckChildTypeMatcher>(M)->ChildNo == ChildNo &&
605 cast<CheckChildTypeMatcher>(M)->Type == Type;
607 unsigned getHashImpl() const override { return (Type << 3) | ChildNo; }
608 bool isContradictoryImpl(const Matcher *M) const override;
612 /// CheckIntegerMatcher - This checks to see if the current node is a
613 /// ConstantSDNode with the specified integer value, if not it fails to match.
614 class CheckIntegerMatcher : public Matcher {
617 CheckIntegerMatcher(int64_t value)
618 : Matcher(CheckInteger), Value(value) {}
620 int64_t getValue() const { return Value; }
622 static inline bool classof(const Matcher *N) {
623 return N->getKind() == CheckInteger;
626 bool isSafeToReorderWithPatternPredicate() const override { return true; }
629 void printImpl(raw_ostream &OS, unsigned indent) const override;
630 bool isEqualImpl(const Matcher *M) const override {
631 return cast<CheckIntegerMatcher>(M)->Value == Value;
633 unsigned getHashImpl() const override { return Value; }
634 bool isContradictoryImpl(const Matcher *M) const override;
637 /// CheckChildIntegerMatcher - This checks to see if the child node is a
638 /// ConstantSDNode with a specified integer value, if not it fails to match.
639 class CheckChildIntegerMatcher : public Matcher {
643 CheckChildIntegerMatcher(unsigned childno, int64_t value)
644 : Matcher(CheckChildInteger), ChildNo(childno), Value(value) {}
646 unsigned getChildNo() const { return ChildNo; }
647 int64_t getValue() const { return Value; }
649 static inline bool classof(const Matcher *N) {
650 return N->getKind() == CheckChildInteger;
653 bool isSafeToReorderWithPatternPredicate() const override { return true; }
656 void printImpl(raw_ostream &OS, unsigned indent) const override;
657 bool isEqualImpl(const Matcher *M) const override {
658 return cast<CheckChildIntegerMatcher>(M)->ChildNo == ChildNo &&
659 cast<CheckChildIntegerMatcher>(M)->Value == Value;
661 unsigned getHashImpl() const override { return (Value << 3) | ChildNo; }
662 bool isContradictoryImpl(const Matcher *M) const override;
665 /// CheckCondCodeMatcher - This checks to see if the current node is a
666 /// CondCodeSDNode with the specified condition, if not it fails to match.
667 class CheckCondCodeMatcher : public Matcher {
668 StringRef CondCodeName;
670 CheckCondCodeMatcher(StringRef condcodename)
671 : Matcher(CheckCondCode), CondCodeName(condcodename) {}
673 StringRef getCondCodeName() const { return CondCodeName; }
675 static inline bool classof(const Matcher *N) {
676 return N->getKind() == CheckCondCode;
679 bool isSafeToReorderWithPatternPredicate() const override { return true; }
682 void printImpl(raw_ostream &OS, unsigned indent) const override;
683 bool isEqualImpl(const Matcher *M) const override {
684 return cast<CheckCondCodeMatcher>(M)->CondCodeName == CondCodeName;
686 unsigned getHashImpl() const override;
689 /// CheckValueTypeMatcher - This checks to see if the current node is a
690 /// VTSDNode with the specified type, if not it fails to match.
691 class CheckValueTypeMatcher : public Matcher {
694 CheckValueTypeMatcher(StringRef type_name)
695 : Matcher(CheckValueType), TypeName(type_name) {}
697 StringRef getTypeName() const { return TypeName; }
699 static inline bool classof(const Matcher *N) {
700 return N->getKind() == CheckValueType;
703 bool isSafeToReorderWithPatternPredicate() const override { return true; }
706 void printImpl(raw_ostream &OS, unsigned indent) const override;
707 bool isEqualImpl(const Matcher *M) const override {
708 return cast<CheckValueTypeMatcher>(M)->TypeName == TypeName;
710 unsigned getHashImpl() const override;
711 bool isContradictoryImpl(const Matcher *M) const override;
716 /// CheckComplexPatMatcher - This node runs the specified ComplexPattern on
717 /// the current node.
718 class CheckComplexPatMatcher : public Matcher {
719 const ComplexPattern &Pattern;
721 /// MatchNumber - This is the recorded nodes slot that contains the node we
722 /// want to match against.
723 unsigned MatchNumber;
725 /// Name - The name of the node we're matching, for comment emission.
728 /// FirstResult - This is the first slot in the RecordedNodes list that the
729 /// result of the match populates.
730 unsigned FirstResult;
732 CheckComplexPatMatcher(const ComplexPattern &pattern, unsigned matchnumber,
733 const std::string &name, unsigned firstresult)
734 : Matcher(CheckComplexPat), Pattern(pattern), MatchNumber(matchnumber),
735 Name(name), FirstResult(firstresult) {}
737 const ComplexPattern &getPattern() const { return Pattern; }
738 unsigned getMatchNumber() const { return MatchNumber; }
740 const std::string getName() const { return Name; }
741 unsigned getFirstResult() const { return FirstResult; }
743 static inline bool classof(const Matcher *N) {
744 return N->getKind() == CheckComplexPat;
747 // Not safe to move a pattern predicate past a complex pattern.
748 bool isSafeToReorderWithPatternPredicate() const override { return false; }
751 void printImpl(raw_ostream &OS, unsigned indent) const override;
752 bool isEqualImpl(const Matcher *M) const override {
753 return &cast<CheckComplexPatMatcher>(M)->Pattern == &Pattern &&
754 cast<CheckComplexPatMatcher>(M)->MatchNumber == MatchNumber;
756 unsigned getHashImpl() const override {
757 return (unsigned)(intptr_t)&Pattern ^ MatchNumber;
761 /// CheckAndImmMatcher - This checks to see if the current node is an 'and'
762 /// with something equivalent to the specified immediate.
763 class CheckAndImmMatcher : public Matcher {
766 CheckAndImmMatcher(int64_t value)
767 : Matcher(CheckAndImm), Value(value) {}
769 int64_t getValue() const { return Value; }
771 static inline bool classof(const Matcher *N) {
772 return N->getKind() == CheckAndImm;
775 bool isSafeToReorderWithPatternPredicate() const override { return true; }
778 void printImpl(raw_ostream &OS, unsigned indent) const override;
779 bool isEqualImpl(const Matcher *M) const override {
780 return cast<CheckAndImmMatcher>(M)->Value == Value;
782 unsigned getHashImpl() const override { return Value; }
785 /// CheckOrImmMatcher - This checks to see if the current node is an 'and'
786 /// with something equivalent to the specified immediate.
787 class CheckOrImmMatcher : public Matcher {
790 CheckOrImmMatcher(int64_t value)
791 : Matcher(CheckOrImm), Value(value) {}
793 int64_t getValue() const { return Value; }
795 static inline bool classof(const Matcher *N) {
796 return N->getKind() == CheckOrImm;
799 bool isSafeToReorderWithPatternPredicate() const override { return true; }
802 void printImpl(raw_ostream &OS, unsigned indent) const override;
803 bool isEqualImpl(const Matcher *M) const override {
804 return cast<CheckOrImmMatcher>(M)->Value == Value;
806 unsigned getHashImpl() const override { return Value; }
809 /// CheckFoldableChainNodeMatcher - This checks to see if the current node
810 /// (which defines a chain operand) is safe to fold into a larger pattern.
811 class CheckFoldableChainNodeMatcher : public Matcher {
813 CheckFoldableChainNodeMatcher()
814 : Matcher(CheckFoldableChainNode) {}
816 static inline bool classof(const Matcher *N) {
817 return N->getKind() == CheckFoldableChainNode;
820 bool isSafeToReorderWithPatternPredicate() const override { return true; }
823 void printImpl(raw_ostream &OS, unsigned indent) const override;
824 bool isEqualImpl(const Matcher *M) const override { return true; }
825 unsigned getHashImpl() const override { return 0; }
828 /// EmitIntegerMatcher - This creates a new TargetConstant.
829 class EmitIntegerMatcher : public Matcher {
831 MVT::SimpleValueType VT;
833 EmitIntegerMatcher(int64_t val, MVT::SimpleValueType vt)
834 : Matcher(EmitInteger), Val(val), VT(vt) {}
836 int64_t getValue() const { return Val; }
837 MVT::SimpleValueType getVT() const { return VT; }
839 static inline bool classof(const Matcher *N) {
840 return N->getKind() == EmitInteger;
844 void printImpl(raw_ostream &OS, unsigned indent) const override;
845 bool isEqualImpl(const Matcher *M) const override {
846 return cast<EmitIntegerMatcher>(M)->Val == Val &&
847 cast<EmitIntegerMatcher>(M)->VT == VT;
849 unsigned getHashImpl() const override { return (Val << 4) | VT; }
852 /// EmitStringIntegerMatcher - A target constant whose value is represented
854 class EmitStringIntegerMatcher : public Matcher {
856 MVT::SimpleValueType VT;
858 EmitStringIntegerMatcher(const std::string &val, MVT::SimpleValueType vt)
859 : Matcher(EmitStringInteger), Val(val), VT(vt) {}
861 const std::string &getValue() const { return Val; }
862 MVT::SimpleValueType getVT() const { return VT; }
864 static inline bool classof(const Matcher *N) {
865 return N->getKind() == EmitStringInteger;
869 void printImpl(raw_ostream &OS, unsigned indent) const override;
870 bool isEqualImpl(const Matcher *M) const override {
871 return cast<EmitStringIntegerMatcher>(M)->Val == Val &&
872 cast<EmitStringIntegerMatcher>(M)->VT == VT;
874 unsigned getHashImpl() const override;
877 /// EmitRegisterMatcher - This creates a new TargetConstant.
878 class EmitRegisterMatcher : public Matcher {
879 /// Reg - The def for the register that we're emitting. If this is null, then
880 /// this is a reference to zero_reg.
881 const CodeGenRegister *Reg;
882 MVT::SimpleValueType VT;
884 EmitRegisterMatcher(const CodeGenRegister *reg, MVT::SimpleValueType vt)
885 : Matcher(EmitRegister), Reg(reg), VT(vt) {}
887 const CodeGenRegister *getReg() const { return Reg; }
888 MVT::SimpleValueType getVT() const { return VT; }
890 static inline bool classof(const Matcher *N) {
891 return N->getKind() == EmitRegister;
895 void printImpl(raw_ostream &OS, unsigned indent) const override;
896 bool isEqualImpl(const Matcher *M) const override {
897 return cast<EmitRegisterMatcher>(M)->Reg == Reg &&
898 cast<EmitRegisterMatcher>(M)->VT == VT;
900 unsigned getHashImpl() const override {
901 return ((unsigned)(intptr_t)Reg) << 4 | VT;
905 /// EmitConvertToTargetMatcher - Emit an operation that reads a specified
906 /// recorded node and converts it from being a ISD::Constant to
907 /// ISD::TargetConstant, likewise for ConstantFP.
908 class EmitConvertToTargetMatcher : public Matcher {
911 EmitConvertToTargetMatcher(unsigned slot)
912 : Matcher(EmitConvertToTarget), Slot(slot) {}
914 unsigned getSlot() const { return Slot; }
916 static inline bool classof(const Matcher *N) {
917 return N->getKind() == EmitConvertToTarget;
921 void printImpl(raw_ostream &OS, unsigned indent) const override;
922 bool isEqualImpl(const Matcher *M) const override {
923 return cast<EmitConvertToTargetMatcher>(M)->Slot == Slot;
925 unsigned getHashImpl() const override { return Slot; }
928 /// EmitMergeInputChainsMatcher - Emit a node that merges a list of input
929 /// chains together with a token factor. The list of nodes are the nodes in the
930 /// matched pattern that have chain input/outputs. This node adds all input
931 /// chains of these nodes if they are not themselves a node in the pattern.
932 class EmitMergeInputChainsMatcher : public Matcher {
933 SmallVector<unsigned, 3> ChainNodes;
935 EmitMergeInputChainsMatcher(ArrayRef<unsigned> nodes)
936 : Matcher(EmitMergeInputChains), ChainNodes(nodes.begin(), nodes.end()) {}
938 unsigned getNumNodes() const { return ChainNodes.size(); }
940 unsigned getNode(unsigned i) const {
941 assert(i < ChainNodes.size());
942 return ChainNodes[i];
945 static inline bool classof(const Matcher *N) {
946 return N->getKind() == EmitMergeInputChains;
950 void printImpl(raw_ostream &OS, unsigned indent) const override;
951 bool isEqualImpl(const Matcher *M) const override {
952 return cast<EmitMergeInputChainsMatcher>(M)->ChainNodes == ChainNodes;
954 unsigned getHashImpl() const override;
957 /// EmitCopyToRegMatcher - Emit a CopyToReg node from a value to a physreg,
958 /// pushing the chain and glue results.
960 class EmitCopyToRegMatcher : public Matcher {
961 unsigned SrcSlot; // Value to copy into the physreg.
964 EmitCopyToRegMatcher(unsigned srcSlot, Record *destPhysReg)
965 : Matcher(EmitCopyToReg), SrcSlot(srcSlot), DestPhysReg(destPhysReg) {}
967 unsigned getSrcSlot() const { return SrcSlot; }
968 Record *getDestPhysReg() const { return DestPhysReg; }
970 static inline bool classof(const Matcher *N) {
971 return N->getKind() == EmitCopyToReg;
975 void printImpl(raw_ostream &OS, unsigned indent) const override;
976 bool isEqualImpl(const Matcher *M) const override {
977 return cast<EmitCopyToRegMatcher>(M)->SrcSlot == SrcSlot &&
978 cast<EmitCopyToRegMatcher>(M)->DestPhysReg == DestPhysReg;
980 unsigned getHashImpl() const override {
981 return SrcSlot ^ ((unsigned)(intptr_t)DestPhysReg << 4);
987 /// EmitNodeXFormMatcher - Emit an operation that runs an SDNodeXForm on a
988 /// recorded node and records the result.
989 class EmitNodeXFormMatcher : public Matcher {
993 EmitNodeXFormMatcher(unsigned slot, Record *nodeXForm)
994 : Matcher(EmitNodeXForm), Slot(slot), NodeXForm(nodeXForm) {}
996 unsigned getSlot() const { return Slot; }
997 Record *getNodeXForm() const { return NodeXForm; }
999 static inline bool classof(const Matcher *N) {
1000 return N->getKind() == EmitNodeXForm;
1004 void printImpl(raw_ostream &OS, unsigned indent) const override;
1005 bool isEqualImpl(const Matcher *M) const override {
1006 return cast<EmitNodeXFormMatcher>(M)->Slot == Slot &&
1007 cast<EmitNodeXFormMatcher>(M)->NodeXForm == NodeXForm;
1009 unsigned getHashImpl() const override {
1010 return Slot ^ ((unsigned)(intptr_t)NodeXForm << 4);
1014 /// EmitNodeMatcherCommon - Common class shared between EmitNode and
1016 class EmitNodeMatcherCommon : public Matcher {
1017 std::string OpcodeName;
1018 const SmallVector<MVT::SimpleValueType, 3> VTs;
1019 const SmallVector<unsigned, 6> Operands;
1020 bool HasChain, HasInGlue, HasOutGlue, HasMemRefs;
1022 /// NumFixedArityOperands - If this is a fixed arity node, this is set to -1.
1023 /// If this is a varidic node, this is set to the number of fixed arity
1024 /// operands in the root of the pattern. The rest are appended to this node.
1025 int NumFixedArityOperands;
1027 EmitNodeMatcherCommon(const std::string &opcodeName,
1028 ArrayRef<MVT::SimpleValueType> vts,
1029 ArrayRef<unsigned> operands,
1030 bool hasChain, bool hasInGlue, bool hasOutGlue,
1032 int numfixedarityoperands, bool isMorphNodeTo)
1033 : Matcher(isMorphNodeTo ? MorphNodeTo : EmitNode), OpcodeName(opcodeName),
1034 VTs(vts.begin(), vts.end()), Operands(operands.begin(), operands.end()),
1035 HasChain(hasChain), HasInGlue(hasInGlue), HasOutGlue(hasOutGlue),
1036 HasMemRefs(hasmemrefs), NumFixedArityOperands(numfixedarityoperands) {}
1038 const std::string &getOpcodeName() const { return OpcodeName; }
1040 unsigned getNumVTs() const { return VTs.size(); }
1041 MVT::SimpleValueType getVT(unsigned i) const {
1042 assert(i < VTs.size());
1046 unsigned getNumOperands() const { return Operands.size(); }
1047 unsigned getOperand(unsigned i) const {
1048 assert(i < Operands.size());
1052 const SmallVectorImpl<MVT::SimpleValueType> &getVTList() const { return VTs; }
1053 const SmallVectorImpl<unsigned> &getOperandList() const { return Operands; }
1056 bool hasChain() const { return HasChain; }
1057 bool hasInFlag() const { return HasInGlue; }
1058 bool hasOutFlag() const { return HasOutGlue; }
1059 bool hasMemRefs() const { return HasMemRefs; }
1060 int getNumFixedArityOperands() const { return NumFixedArityOperands; }
1062 static inline bool classof(const Matcher *N) {
1063 return N->getKind() == EmitNode || N->getKind() == MorphNodeTo;
1067 void printImpl(raw_ostream &OS, unsigned indent) const override;
1068 bool isEqualImpl(const Matcher *M) const override;
1069 unsigned getHashImpl() const override;
1072 /// EmitNodeMatcher - This signals a successful match and generates a node.
1073 class EmitNodeMatcher : public EmitNodeMatcherCommon {
1074 void anchor() override;
1075 unsigned FirstResultSlot;
1077 EmitNodeMatcher(const std::string &opcodeName,
1078 ArrayRef<MVT::SimpleValueType> vts,
1079 ArrayRef<unsigned> operands,
1080 bool hasChain, bool hasInFlag, bool hasOutFlag,
1082 int numfixedarityoperands, unsigned firstresultslot)
1083 : EmitNodeMatcherCommon(opcodeName, vts, operands, hasChain,
1084 hasInFlag, hasOutFlag, hasmemrefs,
1085 numfixedarityoperands, false),
1086 FirstResultSlot(firstresultslot) {}
1088 unsigned getFirstResultSlot() const { return FirstResultSlot; }
1090 static inline bool classof(const Matcher *N) {
1091 return N->getKind() == EmitNode;
1096 class MorphNodeToMatcher : public EmitNodeMatcherCommon {
1097 void anchor() override;
1098 const PatternToMatch &Pattern;
1100 MorphNodeToMatcher(const std::string &opcodeName,
1101 ArrayRef<MVT::SimpleValueType> vts,
1102 ArrayRef<unsigned> operands,
1103 bool hasChain, bool hasInFlag, bool hasOutFlag,
1105 int numfixedarityoperands, const PatternToMatch &pattern)
1106 : EmitNodeMatcherCommon(opcodeName, vts, operands, hasChain,
1107 hasInFlag, hasOutFlag, hasmemrefs,
1108 numfixedarityoperands, true),
1112 const PatternToMatch &getPattern() const { return Pattern; }
1114 static inline bool classof(const Matcher *N) {
1115 return N->getKind() == MorphNodeTo;
1119 /// MarkGlueResultsMatcher - This node indicates which non-root nodes in the
1120 /// pattern produce glue. This allows CompleteMatchMatcher to update them
1121 /// with the output glue of the resultant code.
1122 class MarkGlueResultsMatcher : public Matcher {
1123 SmallVector<unsigned, 3> GlueResultNodes;
1125 MarkGlueResultsMatcher(ArrayRef<unsigned> nodes)
1126 : Matcher(MarkGlueResults), GlueResultNodes(nodes.begin(), nodes.end()) {}
1128 unsigned getNumNodes() const { return GlueResultNodes.size(); }
1130 unsigned getNode(unsigned i) const {
1131 assert(i < GlueResultNodes.size());
1132 return GlueResultNodes[i];
1135 static inline bool classof(const Matcher *N) {
1136 return N->getKind() == MarkGlueResults;
1140 void printImpl(raw_ostream &OS, unsigned indent) const override;
1141 bool isEqualImpl(const Matcher *M) const override {
1142 return cast<MarkGlueResultsMatcher>(M)->GlueResultNodes == GlueResultNodes;
1144 unsigned getHashImpl() const override;
1147 /// CompleteMatchMatcher - Complete a match by replacing the results of the
1148 /// pattern with the newly generated nodes. This also prints a comment
1149 /// indicating the source and dest patterns.
1150 class CompleteMatchMatcher : public Matcher {
1151 SmallVector<unsigned, 2> Results;
1152 const PatternToMatch &Pattern;
1154 CompleteMatchMatcher(ArrayRef<unsigned> results,
1155 const PatternToMatch &pattern)
1156 : Matcher(CompleteMatch), Results(results.begin(), results.end()),
1159 unsigned getNumResults() const { return Results.size(); }
1160 unsigned getResult(unsigned R) const { return Results[R]; }
1161 const PatternToMatch &getPattern() const { return Pattern; }
1163 static inline bool classof(const Matcher *N) {
1164 return N->getKind() == CompleteMatch;
1168 void printImpl(raw_ostream &OS, unsigned indent) const override;
1169 bool isEqualImpl(const Matcher *M) const override {
1170 return cast<CompleteMatchMatcher>(M)->Results == Results &&
1171 &cast<CompleteMatchMatcher>(M)->Pattern == &Pattern;
1173 unsigned getHashImpl() const override;
1176 } // end namespace llvm