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/Support/Casting.h"
17 #include "llvm/Support/MachineValueType.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 {
417 const SmallVector<unsigned, 4> Operands;
419 CheckPredicateMatcher(const TreePredicateFn &pred,
420 const SmallVectorImpl<unsigned> &Operands);
422 TreePredicateFn getPredicate() const;
423 unsigned getNumOperands() const;
424 unsigned getOperandNo(unsigned i) const;
426 static bool classof(const Matcher *N) {
427 return N->getKind() == CheckPredicate;
431 void printImpl(raw_ostream &OS, unsigned indent) const override;
432 bool isEqualImpl(const Matcher *M) const override {
433 return cast<CheckPredicateMatcher>(M)->Pred == Pred;
438 /// CheckOpcodeMatcher - This checks to see if the current node has the
439 /// specified opcode, if not it fails to match.
440 class CheckOpcodeMatcher : public Matcher {
441 const SDNodeInfo &Opcode;
443 CheckOpcodeMatcher(const SDNodeInfo &opcode)
444 : Matcher(CheckOpcode), Opcode(opcode) {}
446 const SDNodeInfo &getOpcode() const { return Opcode; }
448 static bool classof(const Matcher *N) {
449 return N->getKind() == CheckOpcode;
453 void printImpl(raw_ostream &OS, unsigned indent) const override;
454 bool isEqualImpl(const Matcher *M) const override;
455 bool isContradictoryImpl(const Matcher *M) const override;
458 /// SwitchOpcodeMatcher - Switch based on the current node's opcode, dispatching
459 /// to one matcher per opcode. If the opcode doesn't match any of the cases,
460 /// then the match fails. This is semantically equivalent to a Scope node where
461 /// every child does a CheckOpcode, but is much faster.
462 class SwitchOpcodeMatcher : public Matcher {
463 SmallVector<std::pair<const SDNodeInfo*, Matcher*>, 8> Cases;
465 SwitchOpcodeMatcher(ArrayRef<std::pair<const SDNodeInfo*, Matcher*> > cases)
466 : Matcher(SwitchOpcode), Cases(cases.begin(), cases.end()) {}
467 ~SwitchOpcodeMatcher() override;
469 static bool classof(const Matcher *N) {
470 return N->getKind() == SwitchOpcode;
473 unsigned getNumCases() const { return Cases.size(); }
475 const SDNodeInfo &getCaseOpcode(unsigned i) const { return *Cases[i].first; }
476 Matcher *getCaseMatcher(unsigned i) { return Cases[i].second; }
477 const Matcher *getCaseMatcher(unsigned i) const { return Cases[i].second; }
480 void printImpl(raw_ostream &OS, unsigned indent) const override;
481 bool isEqualImpl(const Matcher *M) const override { return false; }
484 /// CheckTypeMatcher - This checks to see if the current node has the
485 /// specified type at the specified result, if not it fails to match.
486 class CheckTypeMatcher : public Matcher {
487 MVT::SimpleValueType Type;
490 CheckTypeMatcher(MVT::SimpleValueType type, unsigned resno)
491 : Matcher(CheckType), Type(type), ResNo(resno) {}
493 MVT::SimpleValueType getType() const { return Type; }
494 unsigned getResNo() const { return ResNo; }
496 static bool classof(const Matcher *N) {
497 return N->getKind() == CheckType;
501 void printImpl(raw_ostream &OS, unsigned indent) const override;
502 bool isEqualImpl(const Matcher *M) const override {
503 return cast<CheckTypeMatcher>(M)->Type == Type;
505 bool isContradictoryImpl(const Matcher *M) const override;
508 /// SwitchTypeMatcher - Switch based on the current node's type, dispatching
509 /// to one matcher per case. If the type doesn't match any of the cases,
510 /// then the match fails. This is semantically equivalent to a Scope node where
511 /// every child does a CheckType, but is much faster.
512 class SwitchTypeMatcher : public Matcher {
513 SmallVector<std::pair<MVT::SimpleValueType, Matcher*>, 8> Cases;
515 SwitchTypeMatcher(ArrayRef<std::pair<MVT::SimpleValueType, Matcher*> > cases)
516 : Matcher(SwitchType), Cases(cases.begin(), cases.end()) {}
517 ~SwitchTypeMatcher() override;
519 static bool classof(const Matcher *N) {
520 return N->getKind() == SwitchType;
523 unsigned getNumCases() const { return Cases.size(); }
525 MVT::SimpleValueType getCaseType(unsigned i) const { return Cases[i].first; }
526 Matcher *getCaseMatcher(unsigned i) { return Cases[i].second; }
527 const Matcher *getCaseMatcher(unsigned i) const { return Cases[i].second; }
530 void printImpl(raw_ostream &OS, unsigned indent) const override;
531 bool isEqualImpl(const Matcher *M) const override { return false; }
535 /// CheckChildTypeMatcher - This checks to see if a child node has the
536 /// specified type, if not it fails to match.
537 class CheckChildTypeMatcher : public Matcher {
539 MVT::SimpleValueType Type;
541 CheckChildTypeMatcher(unsigned childno, MVT::SimpleValueType type)
542 : Matcher(CheckChildType), ChildNo(childno), Type(type) {}
544 unsigned getChildNo() const { return ChildNo; }
545 MVT::SimpleValueType getType() const { return Type; }
547 static bool classof(const Matcher *N) {
548 return N->getKind() == CheckChildType;
552 void printImpl(raw_ostream &OS, unsigned indent) const override;
553 bool isEqualImpl(const Matcher *M) const override {
554 return cast<CheckChildTypeMatcher>(M)->ChildNo == ChildNo &&
555 cast<CheckChildTypeMatcher>(M)->Type == Type;
557 bool isContradictoryImpl(const Matcher *M) const override;
561 /// CheckIntegerMatcher - This checks to see if the current node is a
562 /// ConstantSDNode with the specified integer value, if not it fails to match.
563 class CheckIntegerMatcher : public Matcher {
566 CheckIntegerMatcher(int64_t value)
567 : Matcher(CheckInteger), Value(value) {}
569 int64_t getValue() const { return Value; }
571 static bool classof(const Matcher *N) {
572 return N->getKind() == CheckInteger;
576 void printImpl(raw_ostream &OS, unsigned indent) const override;
577 bool isEqualImpl(const Matcher *M) const override {
578 return cast<CheckIntegerMatcher>(M)->Value == Value;
580 bool isContradictoryImpl(const Matcher *M) const override;
583 /// CheckChildIntegerMatcher - This checks to see if the child node is a
584 /// ConstantSDNode with a specified integer value, if not it fails to match.
585 class CheckChildIntegerMatcher : public Matcher {
589 CheckChildIntegerMatcher(unsigned childno, int64_t value)
590 : Matcher(CheckChildInteger), ChildNo(childno), Value(value) {}
592 unsigned getChildNo() const { return ChildNo; }
593 int64_t getValue() const { return Value; }
595 static bool classof(const Matcher *N) {
596 return N->getKind() == CheckChildInteger;
600 void printImpl(raw_ostream &OS, unsigned indent) const override;
601 bool isEqualImpl(const Matcher *M) const override {
602 return cast<CheckChildIntegerMatcher>(M)->ChildNo == ChildNo &&
603 cast<CheckChildIntegerMatcher>(M)->Value == Value;
605 bool isContradictoryImpl(const Matcher *M) const override;
608 /// CheckCondCodeMatcher - This checks to see if the current node is a
609 /// CondCodeSDNode with the specified condition, if not it fails to match.
610 class CheckCondCodeMatcher : public Matcher {
611 StringRef CondCodeName;
613 CheckCondCodeMatcher(StringRef condcodename)
614 : Matcher(CheckCondCode), CondCodeName(condcodename) {}
616 StringRef getCondCodeName() const { return CondCodeName; }
618 static bool classof(const Matcher *N) {
619 return N->getKind() == CheckCondCode;
623 void printImpl(raw_ostream &OS, unsigned indent) const override;
624 bool isEqualImpl(const Matcher *M) const override {
625 return cast<CheckCondCodeMatcher>(M)->CondCodeName == CondCodeName;
629 /// CheckValueTypeMatcher - This checks to see if the current node is a
630 /// VTSDNode with the specified type, if not it fails to match.
631 class CheckValueTypeMatcher : public Matcher {
634 CheckValueTypeMatcher(StringRef type_name)
635 : Matcher(CheckValueType), TypeName(type_name) {}
637 StringRef getTypeName() const { return TypeName; }
639 static bool classof(const Matcher *N) {
640 return N->getKind() == CheckValueType;
644 void printImpl(raw_ostream &OS, unsigned indent) const override;
645 bool isEqualImpl(const Matcher *M) const override {
646 return cast<CheckValueTypeMatcher>(M)->TypeName == TypeName;
648 bool isContradictoryImpl(const Matcher *M) const override;
653 /// CheckComplexPatMatcher - This node runs the specified ComplexPattern on
654 /// the current node.
655 class CheckComplexPatMatcher : public Matcher {
656 const ComplexPattern &Pattern;
658 /// MatchNumber - This is the recorded nodes slot that contains the node we
659 /// want to match against.
660 unsigned MatchNumber;
662 /// Name - The name of the node we're matching, for comment emission.
665 /// FirstResult - This is the first slot in the RecordedNodes list that the
666 /// result of the match populates.
667 unsigned FirstResult;
669 CheckComplexPatMatcher(const ComplexPattern &pattern, unsigned matchnumber,
670 const std::string &name, unsigned firstresult)
671 : Matcher(CheckComplexPat), Pattern(pattern), MatchNumber(matchnumber),
672 Name(name), FirstResult(firstresult) {}
674 const ComplexPattern &getPattern() const { return Pattern; }
675 unsigned getMatchNumber() const { return MatchNumber; }
677 const std::string getName() const { return Name; }
678 unsigned getFirstResult() const { return FirstResult; }
680 static bool classof(const Matcher *N) {
681 return N->getKind() == CheckComplexPat;
685 void printImpl(raw_ostream &OS, unsigned indent) const override;
686 bool isEqualImpl(const Matcher *M) const override {
687 return &cast<CheckComplexPatMatcher>(M)->Pattern == &Pattern &&
688 cast<CheckComplexPatMatcher>(M)->MatchNumber == MatchNumber;
692 /// CheckAndImmMatcher - This checks to see if the current node is an 'and'
693 /// with something equivalent to the specified immediate.
694 class CheckAndImmMatcher : public Matcher {
697 CheckAndImmMatcher(int64_t value)
698 : Matcher(CheckAndImm), Value(value) {}
700 int64_t getValue() const { return Value; }
702 static bool classof(const Matcher *N) {
703 return N->getKind() == CheckAndImm;
707 void printImpl(raw_ostream &OS, unsigned indent) const override;
708 bool isEqualImpl(const Matcher *M) const override {
709 return cast<CheckAndImmMatcher>(M)->Value == Value;
713 /// CheckOrImmMatcher - This checks to see if the current node is an 'and'
714 /// with something equivalent to the specified immediate.
715 class CheckOrImmMatcher : public Matcher {
718 CheckOrImmMatcher(int64_t value)
719 : Matcher(CheckOrImm), Value(value) {}
721 int64_t getValue() const { return Value; }
723 static bool classof(const Matcher *N) {
724 return N->getKind() == CheckOrImm;
728 void printImpl(raw_ostream &OS, unsigned indent) const override;
729 bool isEqualImpl(const Matcher *M) const override {
730 return cast<CheckOrImmMatcher>(M)->Value == Value;
734 /// CheckFoldableChainNodeMatcher - This checks to see if the current node
735 /// (which defines a chain operand) is safe to fold into a larger pattern.
736 class CheckFoldableChainNodeMatcher : public Matcher {
738 CheckFoldableChainNodeMatcher()
739 : Matcher(CheckFoldableChainNode) {}
741 static bool classof(const Matcher *N) {
742 return N->getKind() == CheckFoldableChainNode;
746 void printImpl(raw_ostream &OS, unsigned indent) const override;
747 bool isEqualImpl(const Matcher *M) const override { return true; }
750 /// EmitIntegerMatcher - This creates a new TargetConstant.
751 class EmitIntegerMatcher : public Matcher {
753 MVT::SimpleValueType VT;
755 EmitIntegerMatcher(int64_t val, MVT::SimpleValueType vt)
756 : Matcher(EmitInteger), Val(val), VT(vt) {}
758 int64_t getValue() const { return Val; }
759 MVT::SimpleValueType getVT() const { return VT; }
761 static bool classof(const Matcher *N) {
762 return N->getKind() == EmitInteger;
766 void printImpl(raw_ostream &OS, unsigned indent) const override;
767 bool isEqualImpl(const Matcher *M) const override {
768 return cast<EmitIntegerMatcher>(M)->Val == Val &&
769 cast<EmitIntegerMatcher>(M)->VT == VT;
773 /// EmitStringIntegerMatcher - A target constant whose value is represented
775 class EmitStringIntegerMatcher : public Matcher {
777 MVT::SimpleValueType VT;
779 EmitStringIntegerMatcher(const std::string &val, MVT::SimpleValueType vt)
780 : Matcher(EmitStringInteger), Val(val), VT(vt) {}
782 const std::string &getValue() const { return Val; }
783 MVT::SimpleValueType getVT() const { return VT; }
785 static bool classof(const Matcher *N) {
786 return N->getKind() == EmitStringInteger;
790 void printImpl(raw_ostream &OS, unsigned indent) const override;
791 bool isEqualImpl(const Matcher *M) const override {
792 return cast<EmitStringIntegerMatcher>(M)->Val == Val &&
793 cast<EmitStringIntegerMatcher>(M)->VT == VT;
797 /// EmitRegisterMatcher - This creates a new TargetConstant.
798 class EmitRegisterMatcher : public Matcher {
799 /// Reg - The def for the register that we're emitting. If this is null, then
800 /// this is a reference to zero_reg.
801 const CodeGenRegister *Reg;
802 MVT::SimpleValueType VT;
804 EmitRegisterMatcher(const CodeGenRegister *reg, MVT::SimpleValueType vt)
805 : Matcher(EmitRegister), Reg(reg), VT(vt) {}
807 const CodeGenRegister *getReg() const { return Reg; }
808 MVT::SimpleValueType getVT() const { return VT; }
810 static bool classof(const Matcher *N) {
811 return N->getKind() == EmitRegister;
815 void printImpl(raw_ostream &OS, unsigned indent) const override;
816 bool isEqualImpl(const Matcher *M) const override {
817 return cast<EmitRegisterMatcher>(M)->Reg == Reg &&
818 cast<EmitRegisterMatcher>(M)->VT == VT;
822 /// EmitConvertToTargetMatcher - Emit an operation that reads a specified
823 /// recorded node and converts it from being a ISD::Constant to
824 /// ISD::TargetConstant, likewise for ConstantFP.
825 class EmitConvertToTargetMatcher : public Matcher {
828 EmitConvertToTargetMatcher(unsigned slot)
829 : Matcher(EmitConvertToTarget), Slot(slot) {}
831 unsigned getSlot() const { return Slot; }
833 static bool classof(const Matcher *N) {
834 return N->getKind() == EmitConvertToTarget;
838 void printImpl(raw_ostream &OS, unsigned indent) const override;
839 bool isEqualImpl(const Matcher *M) const override {
840 return cast<EmitConvertToTargetMatcher>(M)->Slot == Slot;
844 /// EmitMergeInputChainsMatcher - Emit a node that merges a list of input
845 /// chains together with a token factor. The list of nodes are the nodes in the
846 /// matched pattern that have chain input/outputs. This node adds all input
847 /// chains of these nodes if they are not themselves a node in the pattern.
848 class EmitMergeInputChainsMatcher : public Matcher {
849 SmallVector<unsigned, 3> ChainNodes;
851 EmitMergeInputChainsMatcher(ArrayRef<unsigned> nodes)
852 : Matcher(EmitMergeInputChains), ChainNodes(nodes.begin(), nodes.end()) {}
854 unsigned getNumNodes() const { return ChainNodes.size(); }
856 unsigned getNode(unsigned i) const {
857 assert(i < ChainNodes.size());
858 return ChainNodes[i];
861 static bool classof(const Matcher *N) {
862 return N->getKind() == EmitMergeInputChains;
866 void printImpl(raw_ostream &OS, unsigned indent) const override;
867 bool isEqualImpl(const Matcher *M) const override {
868 return cast<EmitMergeInputChainsMatcher>(M)->ChainNodes == ChainNodes;
872 /// EmitCopyToRegMatcher - Emit a CopyToReg node from a value to a physreg,
873 /// pushing the chain and glue results.
875 class EmitCopyToRegMatcher : public Matcher {
876 unsigned SrcSlot; // Value to copy into the physreg.
879 EmitCopyToRegMatcher(unsigned srcSlot, Record *destPhysReg)
880 : Matcher(EmitCopyToReg), SrcSlot(srcSlot), DestPhysReg(destPhysReg) {}
882 unsigned getSrcSlot() const { return SrcSlot; }
883 Record *getDestPhysReg() const { return DestPhysReg; }
885 static bool classof(const Matcher *N) {
886 return N->getKind() == EmitCopyToReg;
890 void printImpl(raw_ostream &OS, unsigned indent) const override;
891 bool isEqualImpl(const Matcher *M) const override {
892 return cast<EmitCopyToRegMatcher>(M)->SrcSlot == SrcSlot &&
893 cast<EmitCopyToRegMatcher>(M)->DestPhysReg == DestPhysReg;
899 /// EmitNodeXFormMatcher - Emit an operation that runs an SDNodeXForm on a
900 /// recorded node and records the result.
901 class EmitNodeXFormMatcher : public Matcher {
905 EmitNodeXFormMatcher(unsigned slot, Record *nodeXForm)
906 : Matcher(EmitNodeXForm), Slot(slot), NodeXForm(nodeXForm) {}
908 unsigned getSlot() const { return Slot; }
909 Record *getNodeXForm() const { return NodeXForm; }
911 static bool classof(const Matcher *N) {
912 return N->getKind() == EmitNodeXForm;
916 void printImpl(raw_ostream &OS, unsigned indent) const override;
917 bool isEqualImpl(const Matcher *M) const override {
918 return cast<EmitNodeXFormMatcher>(M)->Slot == Slot &&
919 cast<EmitNodeXFormMatcher>(M)->NodeXForm == NodeXForm;
923 /// EmitNodeMatcherCommon - Common class shared between EmitNode and
925 class EmitNodeMatcherCommon : public Matcher {
926 std::string OpcodeName;
927 const SmallVector<MVT::SimpleValueType, 3> VTs;
928 const SmallVector<unsigned, 6> Operands;
929 bool HasChain, HasInGlue, HasOutGlue, HasMemRefs;
931 /// NumFixedArityOperands - If this is a fixed arity node, this is set to -1.
932 /// If this is a varidic node, this is set to the number of fixed arity
933 /// operands in the root of the pattern. The rest are appended to this node.
934 int NumFixedArityOperands;
936 EmitNodeMatcherCommon(const std::string &opcodeName,
937 ArrayRef<MVT::SimpleValueType> vts,
938 ArrayRef<unsigned> operands,
939 bool hasChain, bool hasInGlue, bool hasOutGlue,
941 int numfixedarityoperands, bool isMorphNodeTo)
942 : Matcher(isMorphNodeTo ? MorphNodeTo : EmitNode), OpcodeName(opcodeName),
943 VTs(vts.begin(), vts.end()), Operands(operands.begin(), operands.end()),
944 HasChain(hasChain), HasInGlue(hasInGlue), HasOutGlue(hasOutGlue),
945 HasMemRefs(hasmemrefs), NumFixedArityOperands(numfixedarityoperands) {}
947 const std::string &getOpcodeName() const { return OpcodeName; }
949 unsigned getNumVTs() const { return VTs.size(); }
950 MVT::SimpleValueType getVT(unsigned i) const {
951 assert(i < VTs.size());
955 unsigned getNumOperands() const { return Operands.size(); }
956 unsigned getOperand(unsigned i) const {
957 assert(i < Operands.size());
961 const SmallVectorImpl<MVT::SimpleValueType> &getVTList() const { return VTs; }
962 const SmallVectorImpl<unsigned> &getOperandList() const { return Operands; }
965 bool hasChain() const { return HasChain; }
966 bool hasInFlag() const { return HasInGlue; }
967 bool hasOutFlag() const { return HasOutGlue; }
968 bool hasMemRefs() const { return HasMemRefs; }
969 int getNumFixedArityOperands() const { return NumFixedArityOperands; }
971 static bool classof(const Matcher *N) {
972 return N->getKind() == EmitNode || N->getKind() == MorphNodeTo;
976 void printImpl(raw_ostream &OS, unsigned indent) const override;
977 bool isEqualImpl(const Matcher *M) const override;
980 /// EmitNodeMatcher - This signals a successful match and generates a node.
981 class EmitNodeMatcher : public EmitNodeMatcherCommon {
982 void anchor() override;
983 unsigned FirstResultSlot;
985 EmitNodeMatcher(const std::string &opcodeName,
986 ArrayRef<MVT::SimpleValueType> vts,
987 ArrayRef<unsigned> operands,
988 bool hasChain, bool hasInFlag, bool hasOutFlag,
990 int numfixedarityoperands, unsigned firstresultslot)
991 : EmitNodeMatcherCommon(opcodeName, vts, operands, hasChain,
992 hasInFlag, hasOutFlag, hasmemrefs,
993 numfixedarityoperands, false),
994 FirstResultSlot(firstresultslot) {}
996 unsigned getFirstResultSlot() const { return FirstResultSlot; }
998 static bool classof(const Matcher *N) {
999 return N->getKind() == EmitNode;
1004 class MorphNodeToMatcher : public EmitNodeMatcherCommon {
1005 void anchor() override;
1006 const PatternToMatch &Pattern;
1008 MorphNodeToMatcher(const std::string &opcodeName,
1009 ArrayRef<MVT::SimpleValueType> vts,
1010 ArrayRef<unsigned> operands,
1011 bool hasChain, bool hasInFlag, bool hasOutFlag,
1013 int numfixedarityoperands, const PatternToMatch &pattern)
1014 : EmitNodeMatcherCommon(opcodeName, vts, operands, hasChain,
1015 hasInFlag, hasOutFlag, hasmemrefs,
1016 numfixedarityoperands, true),
1020 const PatternToMatch &getPattern() const { return Pattern; }
1022 static bool classof(const Matcher *N) {
1023 return N->getKind() == MorphNodeTo;
1027 /// CompleteMatchMatcher - Complete a match by replacing the results of the
1028 /// pattern with the newly generated nodes. This also prints a comment
1029 /// indicating the source and dest patterns.
1030 class CompleteMatchMatcher : public Matcher {
1031 SmallVector<unsigned, 2> Results;
1032 const PatternToMatch &Pattern;
1034 CompleteMatchMatcher(ArrayRef<unsigned> results,
1035 const PatternToMatch &pattern)
1036 : Matcher(CompleteMatch), Results(results.begin(), results.end()),
1039 unsigned getNumResults() const { return Results.size(); }
1040 unsigned getResult(unsigned R) const { return Results[R]; }
1041 const PatternToMatch &getPattern() const { return Pattern; }
1043 static bool classof(const Matcher *N) {
1044 return N->getKind() == CompleteMatch;
1048 void printImpl(raw_ostream &OS, unsigned indent) const override;
1049 bool isEqualImpl(const Matcher *M) const override {
1050 return cast<CompleteMatchMatcher>(M)->Results == Results &&
1051 &cast<CompleteMatchMatcher>(M)->Pattern == &Pattern;
1055 } // end namespace llvm