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 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.release(); }
102 std::unique_ptr<Matcher> &getNextPtr() { return Next; }
104 bool isEqual(const Matcher *M) const {
105 if (getKind() != M->getKind()) return false;
106 return isEqualImpl(M);
109 /// isSimplePredicateNode - Return true if this is a simple predicate that
110 /// operates on the node or its children without potential side effects or a
111 /// change of the current node.
112 bool isSimplePredicateNode() const {
114 default: return false;
117 case CheckPatternPredicate:
123 case CheckChildInteger:
128 case CheckFoldableChainNode:
133 /// isSimplePredicateOrRecordNode - Return true if this is a record node or
134 /// a simple predicate.
135 bool isSimplePredicateOrRecordNode() const {
136 return isSimplePredicateNode() ||
137 getKind() == RecordNode || getKind() == RecordChild;
140 /// unlinkNode - Unlink the specified node from this chain. If Other == this,
141 /// we unlink the next pointer and return it. Otherwise we unlink Other from
142 /// the list and return this.
143 Matcher *unlinkNode(Matcher *Other);
145 /// canMoveBefore - Return true if this matcher is the same as Other, or if
146 /// we can move this matcher past all of the nodes in-between Other and this
147 /// node. Other must be equal to or before this.
148 bool canMoveBefore(const Matcher *Other) const;
150 /// canMoveBeforeNode - Return true if it is safe to move the current matcher
151 /// across the specified one.
152 bool canMoveBeforeNode(const Matcher *Other) const;
154 /// isContradictory - Return true of these two matchers could never match on
156 bool isContradictory(const Matcher *Other) const {
157 // Since this predicate is reflexive, we canonicalize the ordering so that
158 // we always match a node against nodes with kinds that are greater or equal
159 // to them. For example, we'll pass in a CheckType node as an argument to
160 // the CheckOpcode method, not the other way around.
161 if (getKind() < Other->getKind())
162 return isContradictoryImpl(Other);
163 return Other->isContradictoryImpl(this);
166 void print(raw_ostream &OS, unsigned indent = 0) const;
167 void printOne(raw_ostream &OS) const;
170 virtual void printImpl(raw_ostream &OS, unsigned indent) const = 0;
171 virtual bool isEqualImpl(const Matcher *M) const = 0;
172 virtual bool isContradictoryImpl(const Matcher *M) const { return false; }
175 /// ScopeMatcher - This attempts to match each of its children to find the first
176 /// one that successfully matches. If one child fails, it tries the next child.
177 /// If none of the children match then this check fails. It never has a 'next'.
178 class ScopeMatcher : public Matcher {
179 SmallVector<Matcher*, 4> Children;
181 ScopeMatcher(ArrayRef<Matcher *> children)
182 : Matcher(Scope), Children(children.begin(), children.end()) {
184 ~ScopeMatcher() override;
186 unsigned getNumChildren() const { return Children.size(); }
188 Matcher *getChild(unsigned i) { return Children[i]; }
189 const Matcher *getChild(unsigned i) const { return Children[i]; }
191 void resetChild(unsigned i, Matcher *N) {
196 Matcher *takeChild(unsigned i) {
197 Matcher *Res = Children[i];
198 Children[i] = nullptr;
202 void setNumChildren(unsigned NC) {
203 if (NC < Children.size()) {
204 // delete any children we're about to lose pointers to.
205 for (unsigned i = NC, e = Children.size(); i != e; ++i)
211 static bool classof(const Matcher *N) {
212 return N->getKind() == Scope;
216 void printImpl(raw_ostream &OS, unsigned indent) const override;
217 bool isEqualImpl(const Matcher *M) const override { return false; }
220 /// RecordMatcher - Save the current node in the operand list.
221 class RecordMatcher : public Matcher {
222 /// WhatFor - This is a string indicating why we're recording this. This
223 /// should only be used for comment generation not anything semantic.
226 /// ResultNo - The slot number in the RecordedNodes vector that this will be,
227 /// just printed as a comment.
230 RecordMatcher(const std::string &whatfor, unsigned resultNo)
231 : Matcher(RecordNode), WhatFor(whatfor), ResultNo(resultNo) {}
233 const std::string &getWhatFor() const { return WhatFor; }
234 unsigned getResultNo() const { return ResultNo; }
236 static bool classof(const Matcher *N) {
237 return N->getKind() == RecordNode;
241 void printImpl(raw_ostream &OS, unsigned indent) const override;
242 bool isEqualImpl(const Matcher *M) const override { return true; }
245 /// RecordChildMatcher - Save a numbered child of the current node, or fail
246 /// the match if it doesn't exist. This is logically equivalent to:
247 /// MoveChild N + RecordNode + MoveParent.
248 class RecordChildMatcher : public Matcher {
251 /// WhatFor - This is a string indicating why we're recording this. This
252 /// should only be used for comment generation not anything semantic.
255 /// ResultNo - The slot number in the RecordedNodes vector that this will be,
256 /// just printed as a comment.
259 RecordChildMatcher(unsigned childno, const std::string &whatfor,
261 : Matcher(RecordChild), ChildNo(childno), WhatFor(whatfor),
262 ResultNo(resultNo) {}
264 unsigned getChildNo() const { return ChildNo; }
265 const std::string &getWhatFor() const { return WhatFor; }
266 unsigned getResultNo() const { return ResultNo; }
268 static bool classof(const Matcher *N) {
269 return N->getKind() == RecordChild;
273 void printImpl(raw_ostream &OS, unsigned indent) const override;
274 bool isEqualImpl(const Matcher *M) const override {
275 return cast<RecordChildMatcher>(M)->getChildNo() == getChildNo();
279 /// RecordMemRefMatcher - Save the current node's memref.
280 class RecordMemRefMatcher : public Matcher {
282 RecordMemRefMatcher() : Matcher(RecordMemRef) {}
284 static bool classof(const Matcher *N) {
285 return N->getKind() == RecordMemRef;
289 void printImpl(raw_ostream &OS, unsigned indent) const override;
290 bool isEqualImpl(const Matcher *M) const override { return true; }
294 /// CaptureGlueInputMatcher - If the current record has a glue input, record
295 /// it so that it is used as an input to the generated code.
296 class CaptureGlueInputMatcher : public Matcher {
298 CaptureGlueInputMatcher() : Matcher(CaptureGlueInput) {}
300 static bool classof(const Matcher *N) {
301 return N->getKind() == CaptureGlueInput;
305 void printImpl(raw_ostream &OS, unsigned indent) const override;
306 bool isEqualImpl(const Matcher *M) const override { return true; }
309 /// MoveChildMatcher - This tells the interpreter to move into the
310 /// specified child node.
311 class MoveChildMatcher : public Matcher {
314 MoveChildMatcher(unsigned childNo) : Matcher(MoveChild), ChildNo(childNo) {}
316 unsigned getChildNo() const { return ChildNo; }
318 static bool classof(const Matcher *N) {
319 return N->getKind() == MoveChild;
323 void printImpl(raw_ostream &OS, unsigned indent) const override;
324 bool isEqualImpl(const Matcher *M) const override {
325 return cast<MoveChildMatcher>(M)->getChildNo() == getChildNo();
329 /// MoveParentMatcher - This tells the interpreter to move to the parent
330 /// of the current node.
331 class MoveParentMatcher : public Matcher {
333 MoveParentMatcher() : Matcher(MoveParent) {}
335 static bool classof(const Matcher *N) {
336 return N->getKind() == MoveParent;
340 void printImpl(raw_ostream &OS, unsigned indent) const override;
341 bool isEqualImpl(const Matcher *M) const override { return true; }
344 /// CheckSameMatcher - This checks to see if this node is exactly the same
345 /// node as the specified match that was recorded with 'Record'. This is used
346 /// when patterns have the same name in them, like '(mul GPR:$in, GPR:$in)'.
347 class CheckSameMatcher : public Matcher {
348 unsigned MatchNumber;
350 CheckSameMatcher(unsigned matchnumber)
351 : Matcher(CheckSame), MatchNumber(matchnumber) {}
353 unsigned getMatchNumber() const { return MatchNumber; }
355 static bool classof(const Matcher *N) {
356 return N->getKind() == CheckSame;
360 void printImpl(raw_ostream &OS, unsigned indent) const override;
361 bool isEqualImpl(const Matcher *M) const override {
362 return cast<CheckSameMatcher>(M)->getMatchNumber() == getMatchNumber();
366 /// CheckChildSameMatcher - This checks to see if child node is exactly the same
367 /// node as the specified match that was recorded with 'Record'. This is used
368 /// when patterns have the same name in them, like '(mul GPR:$in, GPR:$in)'.
369 class CheckChildSameMatcher : public Matcher {
371 unsigned MatchNumber;
373 CheckChildSameMatcher(unsigned childno, unsigned matchnumber)
374 : Matcher(CheckChildSame), ChildNo(childno), MatchNumber(matchnumber) {}
376 unsigned getChildNo() const { return ChildNo; }
377 unsigned getMatchNumber() const { return MatchNumber; }
379 static bool classof(const Matcher *N) {
380 return N->getKind() == CheckChildSame;
384 void printImpl(raw_ostream &OS, unsigned indent) const override;
385 bool isEqualImpl(const Matcher *M) const override {
386 return cast<CheckChildSameMatcher>(M)->ChildNo == ChildNo &&
387 cast<CheckChildSameMatcher>(M)->MatchNumber == MatchNumber;
391 /// CheckPatternPredicateMatcher - This checks the target-specific predicate
392 /// to see if the entire pattern is capable of matching. This predicate does
393 /// not take a node as input. This is used for subtarget feature checks etc.
394 class CheckPatternPredicateMatcher : public Matcher {
395 std::string Predicate;
397 CheckPatternPredicateMatcher(StringRef predicate)
398 : Matcher(CheckPatternPredicate), Predicate(predicate) {}
400 StringRef getPredicate() const { return Predicate; }
402 static bool classof(const Matcher *N) {
403 return N->getKind() == CheckPatternPredicate;
407 void printImpl(raw_ostream &OS, unsigned indent) const override;
408 bool isEqualImpl(const Matcher *M) const override {
409 return cast<CheckPatternPredicateMatcher>(M)->getPredicate() == Predicate;
413 /// CheckPredicateMatcher - This checks the target-specific predicate to
414 /// see if the node is acceptable.
415 class CheckPredicateMatcher : public Matcher {
418 CheckPredicateMatcher(const TreePredicateFn &pred);
420 TreePredicateFn getPredicate() const;
422 static bool classof(const Matcher *N) {
423 return N->getKind() == CheckPredicate;
427 void printImpl(raw_ostream &OS, unsigned indent) const override;
428 bool isEqualImpl(const Matcher *M) const override {
429 return cast<CheckPredicateMatcher>(M)->Pred == Pred;
434 /// CheckOpcodeMatcher - This checks to see if the current node has the
435 /// specified opcode, if not it fails to match.
436 class CheckOpcodeMatcher : public Matcher {
437 const SDNodeInfo &Opcode;
439 CheckOpcodeMatcher(const SDNodeInfo &opcode)
440 : Matcher(CheckOpcode), Opcode(opcode) {}
442 const SDNodeInfo &getOpcode() const { return Opcode; }
444 static bool classof(const Matcher *N) {
445 return N->getKind() == CheckOpcode;
449 void printImpl(raw_ostream &OS, unsigned indent) const override;
450 bool isEqualImpl(const Matcher *M) const override;
451 bool isContradictoryImpl(const Matcher *M) const override;
454 /// SwitchOpcodeMatcher - Switch based on the current node's opcode, dispatching
455 /// to one matcher per opcode. If the opcode doesn't match any of the cases,
456 /// then the match fails. This is semantically equivalent to a Scope node where
457 /// every child does a CheckOpcode, but is much faster.
458 class SwitchOpcodeMatcher : public Matcher {
459 SmallVector<std::pair<const SDNodeInfo*, Matcher*>, 8> Cases;
461 SwitchOpcodeMatcher(ArrayRef<std::pair<const SDNodeInfo*, Matcher*> > cases)
462 : Matcher(SwitchOpcode), Cases(cases.begin(), cases.end()) {}
463 ~SwitchOpcodeMatcher() override;
465 static bool classof(const Matcher *N) {
466 return N->getKind() == SwitchOpcode;
469 unsigned getNumCases() const { return Cases.size(); }
471 const SDNodeInfo &getCaseOpcode(unsigned i) const { return *Cases[i].first; }
472 Matcher *getCaseMatcher(unsigned i) { return Cases[i].second; }
473 const Matcher *getCaseMatcher(unsigned i) const { return Cases[i].second; }
476 void printImpl(raw_ostream &OS, unsigned indent) const override;
477 bool isEqualImpl(const Matcher *M) const override { return false; }
480 /// CheckTypeMatcher - This checks to see if the current node has the
481 /// specified type at the specified result, if not it fails to match.
482 class CheckTypeMatcher : public Matcher {
483 MVT::SimpleValueType Type;
486 CheckTypeMatcher(MVT::SimpleValueType type, unsigned resno)
487 : Matcher(CheckType), Type(type), ResNo(resno) {}
489 MVT::SimpleValueType getType() const { return Type; }
490 unsigned getResNo() const { return ResNo; }
492 static bool classof(const Matcher *N) {
493 return N->getKind() == CheckType;
497 void printImpl(raw_ostream &OS, unsigned indent) const override;
498 bool isEqualImpl(const Matcher *M) const override {
499 return cast<CheckTypeMatcher>(M)->Type == Type;
501 bool isContradictoryImpl(const Matcher *M) const override;
504 /// SwitchTypeMatcher - Switch based on the current node's type, dispatching
505 /// to one matcher per case. If the type doesn't match any of the cases,
506 /// then the match fails. This is semantically equivalent to a Scope node where
507 /// every child does a CheckType, but is much faster.
508 class SwitchTypeMatcher : public Matcher {
509 SmallVector<std::pair<MVT::SimpleValueType, Matcher*>, 8> Cases;
511 SwitchTypeMatcher(ArrayRef<std::pair<MVT::SimpleValueType, Matcher*> > cases)
512 : Matcher(SwitchType), Cases(cases.begin(), cases.end()) {}
513 ~SwitchTypeMatcher() override;
515 static bool classof(const Matcher *N) {
516 return N->getKind() == SwitchType;
519 unsigned getNumCases() const { return Cases.size(); }
521 MVT::SimpleValueType getCaseType(unsigned i) const { return Cases[i].first; }
522 Matcher *getCaseMatcher(unsigned i) { return Cases[i].second; }
523 const Matcher *getCaseMatcher(unsigned i) const { return Cases[i].second; }
526 void printImpl(raw_ostream &OS, unsigned indent) const override;
527 bool isEqualImpl(const Matcher *M) const override { return false; }
531 /// CheckChildTypeMatcher - This checks to see if a child node has the
532 /// specified type, if not it fails to match.
533 class CheckChildTypeMatcher : public Matcher {
535 MVT::SimpleValueType Type;
537 CheckChildTypeMatcher(unsigned childno, MVT::SimpleValueType type)
538 : Matcher(CheckChildType), ChildNo(childno), Type(type) {}
540 unsigned getChildNo() const { return ChildNo; }
541 MVT::SimpleValueType getType() const { return Type; }
543 static bool classof(const Matcher *N) {
544 return N->getKind() == CheckChildType;
548 void printImpl(raw_ostream &OS, unsigned indent) const override;
549 bool isEqualImpl(const Matcher *M) const override {
550 return cast<CheckChildTypeMatcher>(M)->ChildNo == ChildNo &&
551 cast<CheckChildTypeMatcher>(M)->Type == Type;
553 bool isContradictoryImpl(const Matcher *M) const override;
557 /// CheckIntegerMatcher - This checks to see if the current node is a
558 /// ConstantSDNode with the specified integer value, if not it fails to match.
559 class CheckIntegerMatcher : public Matcher {
562 CheckIntegerMatcher(int64_t value)
563 : Matcher(CheckInteger), Value(value) {}
565 int64_t getValue() const { return Value; }
567 static bool classof(const Matcher *N) {
568 return N->getKind() == CheckInteger;
572 void printImpl(raw_ostream &OS, unsigned indent) const override;
573 bool isEqualImpl(const Matcher *M) const override {
574 return cast<CheckIntegerMatcher>(M)->Value == Value;
576 bool isContradictoryImpl(const Matcher *M) const override;
579 /// CheckChildIntegerMatcher - This checks to see if the child node is a
580 /// ConstantSDNode with a specified integer value, if not it fails to match.
581 class CheckChildIntegerMatcher : public Matcher {
585 CheckChildIntegerMatcher(unsigned childno, int64_t value)
586 : Matcher(CheckChildInteger), ChildNo(childno), Value(value) {}
588 unsigned getChildNo() const { return ChildNo; }
589 int64_t getValue() const { return Value; }
591 static bool classof(const Matcher *N) {
592 return N->getKind() == CheckChildInteger;
596 void printImpl(raw_ostream &OS, unsigned indent) const override;
597 bool isEqualImpl(const Matcher *M) const override {
598 return cast<CheckChildIntegerMatcher>(M)->ChildNo == ChildNo &&
599 cast<CheckChildIntegerMatcher>(M)->Value == Value;
601 bool isContradictoryImpl(const Matcher *M) const override;
604 /// CheckCondCodeMatcher - This checks to see if the current node is a
605 /// CondCodeSDNode with the specified condition, if not it fails to match.
606 class CheckCondCodeMatcher : public Matcher {
607 StringRef CondCodeName;
609 CheckCondCodeMatcher(StringRef condcodename)
610 : Matcher(CheckCondCode), CondCodeName(condcodename) {}
612 StringRef getCondCodeName() const { return CondCodeName; }
614 static bool classof(const Matcher *N) {
615 return N->getKind() == CheckCondCode;
619 void printImpl(raw_ostream &OS, unsigned indent) const override;
620 bool isEqualImpl(const Matcher *M) const override {
621 return cast<CheckCondCodeMatcher>(M)->CondCodeName == CondCodeName;
625 /// CheckValueTypeMatcher - This checks to see if the current node is a
626 /// VTSDNode with the specified type, if not it fails to match.
627 class CheckValueTypeMatcher : public Matcher {
630 CheckValueTypeMatcher(StringRef type_name)
631 : Matcher(CheckValueType), TypeName(type_name) {}
633 StringRef getTypeName() const { return TypeName; }
635 static bool classof(const Matcher *N) {
636 return N->getKind() == CheckValueType;
640 void printImpl(raw_ostream &OS, unsigned indent) const override;
641 bool isEqualImpl(const Matcher *M) const override {
642 return cast<CheckValueTypeMatcher>(M)->TypeName == TypeName;
644 bool isContradictoryImpl(const Matcher *M) const override;
649 /// CheckComplexPatMatcher - This node runs the specified ComplexPattern on
650 /// the current node.
651 class CheckComplexPatMatcher : public Matcher {
652 const ComplexPattern &Pattern;
654 /// MatchNumber - This is the recorded nodes slot that contains the node we
655 /// want to match against.
656 unsigned MatchNumber;
658 /// Name - The name of the node we're matching, for comment emission.
661 /// FirstResult - This is the first slot in the RecordedNodes list that the
662 /// result of the match populates.
663 unsigned FirstResult;
665 CheckComplexPatMatcher(const ComplexPattern &pattern, unsigned matchnumber,
666 const std::string &name, unsigned firstresult)
667 : Matcher(CheckComplexPat), Pattern(pattern), MatchNumber(matchnumber),
668 Name(name), FirstResult(firstresult) {}
670 const ComplexPattern &getPattern() const { return Pattern; }
671 unsigned getMatchNumber() const { return MatchNumber; }
673 const std::string getName() const { return Name; }
674 unsigned getFirstResult() const { return FirstResult; }
676 static bool classof(const Matcher *N) {
677 return N->getKind() == CheckComplexPat;
681 void printImpl(raw_ostream &OS, unsigned indent) const override;
682 bool isEqualImpl(const Matcher *M) const override {
683 return &cast<CheckComplexPatMatcher>(M)->Pattern == &Pattern &&
684 cast<CheckComplexPatMatcher>(M)->MatchNumber == MatchNumber;
688 /// CheckAndImmMatcher - This checks to see if the current node is an 'and'
689 /// with something equivalent to the specified immediate.
690 class CheckAndImmMatcher : public Matcher {
693 CheckAndImmMatcher(int64_t value)
694 : Matcher(CheckAndImm), Value(value) {}
696 int64_t getValue() const { return Value; }
698 static bool classof(const Matcher *N) {
699 return N->getKind() == CheckAndImm;
703 void printImpl(raw_ostream &OS, unsigned indent) const override;
704 bool isEqualImpl(const Matcher *M) const override {
705 return cast<CheckAndImmMatcher>(M)->Value == Value;
709 /// CheckOrImmMatcher - This checks to see if the current node is an 'and'
710 /// with something equivalent to the specified immediate.
711 class CheckOrImmMatcher : public Matcher {
714 CheckOrImmMatcher(int64_t value)
715 : Matcher(CheckOrImm), Value(value) {}
717 int64_t getValue() const { return Value; }
719 static bool classof(const Matcher *N) {
720 return N->getKind() == CheckOrImm;
724 void printImpl(raw_ostream &OS, unsigned indent) const override;
725 bool isEqualImpl(const Matcher *M) const override {
726 return cast<CheckOrImmMatcher>(M)->Value == Value;
730 /// CheckFoldableChainNodeMatcher - This checks to see if the current node
731 /// (which defines a chain operand) is safe to fold into a larger pattern.
732 class CheckFoldableChainNodeMatcher : public Matcher {
734 CheckFoldableChainNodeMatcher()
735 : Matcher(CheckFoldableChainNode) {}
737 static bool classof(const Matcher *N) {
738 return N->getKind() == CheckFoldableChainNode;
742 void printImpl(raw_ostream &OS, unsigned indent) const override;
743 bool isEqualImpl(const Matcher *M) const override { return true; }
746 /// EmitIntegerMatcher - This creates a new TargetConstant.
747 class EmitIntegerMatcher : public Matcher {
749 MVT::SimpleValueType VT;
751 EmitIntegerMatcher(int64_t val, MVT::SimpleValueType vt)
752 : Matcher(EmitInteger), Val(val), VT(vt) {}
754 int64_t getValue() const { return Val; }
755 MVT::SimpleValueType getVT() const { return VT; }
757 static bool classof(const Matcher *N) {
758 return N->getKind() == EmitInteger;
762 void printImpl(raw_ostream &OS, unsigned indent) const override;
763 bool isEqualImpl(const Matcher *M) const override {
764 return cast<EmitIntegerMatcher>(M)->Val == Val &&
765 cast<EmitIntegerMatcher>(M)->VT == VT;
769 /// EmitStringIntegerMatcher - A target constant whose value is represented
771 class EmitStringIntegerMatcher : public Matcher {
773 MVT::SimpleValueType VT;
775 EmitStringIntegerMatcher(const std::string &val, MVT::SimpleValueType vt)
776 : Matcher(EmitStringInteger), Val(val), VT(vt) {}
778 const std::string &getValue() const { return Val; }
779 MVT::SimpleValueType getVT() const { return VT; }
781 static bool classof(const Matcher *N) {
782 return N->getKind() == EmitStringInteger;
786 void printImpl(raw_ostream &OS, unsigned indent) const override;
787 bool isEqualImpl(const Matcher *M) const override {
788 return cast<EmitStringIntegerMatcher>(M)->Val == Val &&
789 cast<EmitStringIntegerMatcher>(M)->VT == VT;
793 /// EmitRegisterMatcher - This creates a new TargetConstant.
794 class EmitRegisterMatcher : public Matcher {
795 /// Reg - The def for the register that we're emitting. If this is null, then
796 /// this is a reference to zero_reg.
797 const CodeGenRegister *Reg;
798 MVT::SimpleValueType VT;
800 EmitRegisterMatcher(const CodeGenRegister *reg, MVT::SimpleValueType vt)
801 : Matcher(EmitRegister), Reg(reg), VT(vt) {}
803 const CodeGenRegister *getReg() const { return Reg; }
804 MVT::SimpleValueType getVT() const { return VT; }
806 static bool classof(const Matcher *N) {
807 return N->getKind() == EmitRegister;
811 void printImpl(raw_ostream &OS, unsigned indent) const override;
812 bool isEqualImpl(const Matcher *M) const override {
813 return cast<EmitRegisterMatcher>(M)->Reg == Reg &&
814 cast<EmitRegisterMatcher>(M)->VT == VT;
818 /// EmitConvertToTargetMatcher - Emit an operation that reads a specified
819 /// recorded node and converts it from being a ISD::Constant to
820 /// ISD::TargetConstant, likewise for ConstantFP.
821 class EmitConvertToTargetMatcher : public Matcher {
824 EmitConvertToTargetMatcher(unsigned slot)
825 : Matcher(EmitConvertToTarget), Slot(slot) {}
827 unsigned getSlot() const { return Slot; }
829 static bool classof(const Matcher *N) {
830 return N->getKind() == EmitConvertToTarget;
834 void printImpl(raw_ostream &OS, unsigned indent) const override;
835 bool isEqualImpl(const Matcher *M) const override {
836 return cast<EmitConvertToTargetMatcher>(M)->Slot == Slot;
840 /// EmitMergeInputChainsMatcher - Emit a node that merges a list of input
841 /// chains together with a token factor. The list of nodes are the nodes in the
842 /// matched pattern that have chain input/outputs. This node adds all input
843 /// chains of these nodes if they are not themselves a node in the pattern.
844 class EmitMergeInputChainsMatcher : public Matcher {
845 SmallVector<unsigned, 3> ChainNodes;
847 EmitMergeInputChainsMatcher(ArrayRef<unsigned> nodes)
848 : Matcher(EmitMergeInputChains), ChainNodes(nodes.begin(), nodes.end()) {}
850 unsigned getNumNodes() const { return ChainNodes.size(); }
852 unsigned getNode(unsigned i) const {
853 assert(i < ChainNodes.size());
854 return ChainNodes[i];
857 static bool classof(const Matcher *N) {
858 return N->getKind() == EmitMergeInputChains;
862 void printImpl(raw_ostream &OS, unsigned indent) const override;
863 bool isEqualImpl(const Matcher *M) const override {
864 return cast<EmitMergeInputChainsMatcher>(M)->ChainNodes == ChainNodes;
868 /// EmitCopyToRegMatcher - Emit a CopyToReg node from a value to a physreg,
869 /// pushing the chain and glue results.
871 class EmitCopyToRegMatcher : public Matcher {
872 unsigned SrcSlot; // Value to copy into the physreg.
875 EmitCopyToRegMatcher(unsigned srcSlot, Record *destPhysReg)
876 : Matcher(EmitCopyToReg), SrcSlot(srcSlot), DestPhysReg(destPhysReg) {}
878 unsigned getSrcSlot() const { return SrcSlot; }
879 Record *getDestPhysReg() const { return DestPhysReg; }
881 static bool classof(const Matcher *N) {
882 return N->getKind() == EmitCopyToReg;
886 void printImpl(raw_ostream &OS, unsigned indent) const override;
887 bool isEqualImpl(const Matcher *M) const override {
888 return cast<EmitCopyToRegMatcher>(M)->SrcSlot == SrcSlot &&
889 cast<EmitCopyToRegMatcher>(M)->DestPhysReg == DestPhysReg;
895 /// EmitNodeXFormMatcher - Emit an operation that runs an SDNodeXForm on a
896 /// recorded node and records the result.
897 class EmitNodeXFormMatcher : public Matcher {
901 EmitNodeXFormMatcher(unsigned slot, Record *nodeXForm)
902 : Matcher(EmitNodeXForm), Slot(slot), NodeXForm(nodeXForm) {}
904 unsigned getSlot() const { return Slot; }
905 Record *getNodeXForm() const { return NodeXForm; }
907 static bool classof(const Matcher *N) {
908 return N->getKind() == EmitNodeXForm;
912 void printImpl(raw_ostream &OS, unsigned indent) const override;
913 bool isEqualImpl(const Matcher *M) const override {
914 return cast<EmitNodeXFormMatcher>(M)->Slot == Slot &&
915 cast<EmitNodeXFormMatcher>(M)->NodeXForm == NodeXForm;
919 /// EmitNodeMatcherCommon - Common class shared between EmitNode and
921 class EmitNodeMatcherCommon : public Matcher {
922 std::string OpcodeName;
923 const SmallVector<MVT::SimpleValueType, 3> VTs;
924 const SmallVector<unsigned, 6> Operands;
925 bool HasChain, HasInGlue, HasOutGlue, HasMemRefs;
927 /// NumFixedArityOperands - If this is a fixed arity node, this is set to -1.
928 /// If this is a varidic node, this is set to the number of fixed arity
929 /// operands in the root of the pattern. The rest are appended to this node.
930 int NumFixedArityOperands;
932 EmitNodeMatcherCommon(const std::string &opcodeName,
933 ArrayRef<MVT::SimpleValueType> vts,
934 ArrayRef<unsigned> operands,
935 bool hasChain, bool hasInGlue, bool hasOutGlue,
937 int numfixedarityoperands, bool isMorphNodeTo)
938 : Matcher(isMorphNodeTo ? MorphNodeTo : EmitNode), OpcodeName(opcodeName),
939 VTs(vts.begin(), vts.end()), Operands(operands.begin(), operands.end()),
940 HasChain(hasChain), HasInGlue(hasInGlue), HasOutGlue(hasOutGlue),
941 HasMemRefs(hasmemrefs), NumFixedArityOperands(numfixedarityoperands) {}
943 const std::string &getOpcodeName() const { return OpcodeName; }
945 unsigned getNumVTs() const { return VTs.size(); }
946 MVT::SimpleValueType getVT(unsigned i) const {
947 assert(i < VTs.size());
951 unsigned getNumOperands() const { return Operands.size(); }
952 unsigned getOperand(unsigned i) const {
953 assert(i < Operands.size());
957 const SmallVectorImpl<MVT::SimpleValueType> &getVTList() const { return VTs; }
958 const SmallVectorImpl<unsigned> &getOperandList() const { return Operands; }
961 bool hasChain() const { return HasChain; }
962 bool hasInFlag() const { return HasInGlue; }
963 bool hasOutFlag() const { return HasOutGlue; }
964 bool hasMemRefs() const { return HasMemRefs; }
965 int getNumFixedArityOperands() const { return NumFixedArityOperands; }
967 static bool classof(const Matcher *N) {
968 return N->getKind() == EmitNode || N->getKind() == MorphNodeTo;
972 void printImpl(raw_ostream &OS, unsigned indent) const override;
973 bool isEqualImpl(const Matcher *M) const override;
976 /// EmitNodeMatcher - This signals a successful match and generates a node.
977 class EmitNodeMatcher : public EmitNodeMatcherCommon {
978 void anchor() override;
979 unsigned FirstResultSlot;
981 EmitNodeMatcher(const std::string &opcodeName,
982 ArrayRef<MVT::SimpleValueType> vts,
983 ArrayRef<unsigned> operands,
984 bool hasChain, bool hasInFlag, bool hasOutFlag,
986 int numfixedarityoperands, unsigned firstresultslot)
987 : EmitNodeMatcherCommon(opcodeName, vts, operands, hasChain,
988 hasInFlag, hasOutFlag, hasmemrefs,
989 numfixedarityoperands, false),
990 FirstResultSlot(firstresultslot) {}
992 unsigned getFirstResultSlot() const { return FirstResultSlot; }
994 static bool classof(const Matcher *N) {
995 return N->getKind() == EmitNode;
1000 class MorphNodeToMatcher : public EmitNodeMatcherCommon {
1001 void anchor() override;
1002 const PatternToMatch &Pattern;
1004 MorphNodeToMatcher(const std::string &opcodeName,
1005 ArrayRef<MVT::SimpleValueType> vts,
1006 ArrayRef<unsigned> operands,
1007 bool hasChain, bool hasInFlag, bool hasOutFlag,
1009 int numfixedarityoperands, const PatternToMatch &pattern)
1010 : EmitNodeMatcherCommon(opcodeName, vts, operands, hasChain,
1011 hasInFlag, hasOutFlag, hasmemrefs,
1012 numfixedarityoperands, true),
1016 const PatternToMatch &getPattern() const { return Pattern; }
1018 static bool classof(const Matcher *N) {
1019 return N->getKind() == MorphNodeTo;
1023 /// CompleteMatchMatcher - Complete a match by replacing the results of the
1024 /// pattern with the newly generated nodes. This also prints a comment
1025 /// indicating the source and dest patterns.
1026 class CompleteMatchMatcher : public Matcher {
1027 SmallVector<unsigned, 2> Results;
1028 const PatternToMatch &Pattern;
1030 CompleteMatchMatcher(ArrayRef<unsigned> results,
1031 const PatternToMatch &pattern)
1032 : Matcher(CompleteMatch), Results(results.begin(), results.end()),
1035 unsigned getNumResults() const { return Results.size(); }
1036 unsigned getResult(unsigned R) const { return Results[R]; }
1037 const PatternToMatch &getPattern() const { return Pattern; }
1039 static bool classof(const Matcher *N) {
1040 return N->getKind() == CompleteMatch;
1044 void printImpl(raw_ostream &OS, unsigned indent) const override;
1045 bool isEqualImpl(const Matcher *M) const override {
1046 return cast<CompleteMatchMatcher>(M)->Results == Results &&
1047 &cast<CompleteMatchMatcher>(M)->Pattern == &Pattern;
1051 } // end namespace llvm