1 //===- DAGISelMatcher.h - Representation of DAG pattern matcher -*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_UTILS_TABLEGEN_DAGISELMATCHER_H
10 #define LLVM_UTILS_TABLEGEN_DAGISELMATCHER_H
12 #include "llvm/ADT/ArrayRef.h"
13 #include "llvm/ADT/SmallVector.h"
14 #include "llvm/ADT/StringRef.h"
15 #include "llvm/Support/Casting.h"
16 #include "llvm/Support/MachineValueType.h"
19 struct CodeGenRegister
;
20 class CodeGenDAGPatterns
;
27 class TreePredicateFn
;
30 Matcher
*ConvertPatternToMatcher(const PatternToMatch
&Pattern
,unsigned Variant
,
31 const CodeGenDAGPatterns
&CGP
);
32 void OptimizeMatcher(std::unique_ptr
<Matcher
> &Matcher
,
33 const CodeGenDAGPatterns
&CGP
);
34 void EmitMatcherTable(Matcher
*Matcher
, const CodeGenDAGPatterns
&CGP
,
38 /// Matcher - Base class for all the DAG ISel Matcher representation
41 // The next matcher node that is executed after this one. Null if this is the
42 // last stage of a match.
43 std::unique_ptr
<Matcher
> Next
;
44 size_t Size
; // Size in bytes of matcher and all its children (if any).
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.
70 CheckChild2CondCode
, // Fail if child is wrong condcode.
77 CheckFoldableChainNode
,
79 // Node creation/emisssion.
80 EmitInteger
, // Create a TargetConstant
81 EmitStringInteger
, // Create a TargetConstant from a string.
82 EmitRegister
, // Create a register.
83 EmitConvertToTarget
, // Convert a imm/fpimm to target imm/fpimm
84 EmitMergeInputChains
, // Merge together a chains for an input.
85 EmitCopyToReg
, // Emit a copytoreg into a physreg.
86 EmitNode
, // Create a DAG node
87 EmitNodeXForm
, // Run a SDNodeXForm
88 CompleteMatch
, // Finish a match and update the results.
89 MorphNodeTo
, // Build a node, finish a match and update results.
91 // Highest enum value; watch out when adding more.
92 HighestKind
= MorphNodeTo
97 Matcher(KindTy K
) : Kind(K
) {}
101 unsigned getSize() const { return Size
; }
102 void setSize(unsigned sz
) { Size
= sz
; }
103 KindTy
getKind() const { return Kind
; }
105 Matcher
*getNext() { return Next
.get(); }
106 const Matcher
*getNext() const { return Next
.get(); }
107 void setNext(Matcher
*C
) { Next
.reset(C
); }
108 Matcher
*takeNext() { return Next
.release(); }
110 std::unique_ptr
<Matcher
> &getNextPtr() { return Next
; }
112 bool isEqual(const Matcher
*M
) const {
113 if (getKind() != M
->getKind()) return false;
114 return isEqualImpl(M
);
117 /// isSimplePredicateNode - Return true if this is a simple predicate that
118 /// operates on the node or its children without potential side effects or a
119 /// change of the current node.
120 bool isSimplePredicateNode() const {
122 default: return false;
125 case CheckPatternPredicate
:
131 case CheckChildInteger
:
133 case CheckChild2CondCode
:
137 case CheckImmAllOnesV
:
138 case CheckImmAllZerosV
:
139 case CheckFoldableChainNode
:
144 /// isSimplePredicateOrRecordNode - Return true if this is a record node or
145 /// a simple predicate.
146 bool isSimplePredicateOrRecordNode() const {
147 return isSimplePredicateNode() ||
148 getKind() == RecordNode
|| getKind() == RecordChild
;
151 /// unlinkNode - Unlink the specified node from this chain. If Other == this,
152 /// we unlink the next pointer and return it. Otherwise we unlink Other from
153 /// the list and return this.
154 Matcher
*unlinkNode(Matcher
*Other
);
156 /// canMoveBefore - Return true if this matcher is the same as Other, or if
157 /// we can move this matcher past all of the nodes in-between Other and this
158 /// node. Other must be equal to or before this.
159 bool canMoveBefore(const Matcher
*Other
) const;
161 /// canMoveBeforeNode - Return true if it is safe to move the current matcher
162 /// across the specified one.
163 bool canMoveBeforeNode(const Matcher
*Other
) const;
165 /// isContradictory - Return true of these two matchers could never match on
167 bool isContradictory(const Matcher
*Other
) const {
168 // Since this predicate is reflexive, we canonicalize the ordering so that
169 // we always match a node against nodes with kinds that are greater or equal
170 // to them. For example, we'll pass in a CheckType node as an argument to
171 // the CheckOpcode method, not the other way around.
172 if (getKind() < Other
->getKind())
173 return isContradictoryImpl(Other
);
174 return Other
->isContradictoryImpl(this);
177 void print(raw_ostream
&OS
, unsigned indent
= 0) const;
178 void printOne(raw_ostream
&OS
) const;
181 virtual void printImpl(raw_ostream
&OS
, unsigned indent
) const = 0;
182 virtual bool isEqualImpl(const Matcher
*M
) const = 0;
183 virtual bool isContradictoryImpl(const Matcher
*M
) const { return false; }
186 /// ScopeMatcher - This attempts to match each of its children to find the first
187 /// one that successfully matches. If one child fails, it tries the next child.
188 /// If none of the children match then this check fails. It never has a 'next'.
189 class ScopeMatcher
: public Matcher
{
190 SmallVector
<Matcher
*, 4> Children
;
192 ScopeMatcher(ArrayRef
<Matcher
*> children
)
193 : Matcher(Scope
), Children(children
.begin(), children
.end()) {
195 ~ScopeMatcher() override
;
197 unsigned getNumChildren() const { return Children
.size(); }
199 Matcher
*getChild(unsigned i
) { return Children
[i
]; }
200 const Matcher
*getChild(unsigned i
) const { return Children
[i
]; }
202 void resetChild(unsigned i
, Matcher
*N
) {
207 Matcher
*takeChild(unsigned i
) {
208 Matcher
*Res
= Children
[i
];
209 Children
[i
] = nullptr;
213 void setNumChildren(unsigned NC
) {
214 if (NC
< Children
.size()) {
215 // delete any children we're about to lose pointers to.
216 for (unsigned i
= NC
, e
= Children
.size(); i
!= e
; ++i
)
222 static bool classof(const Matcher
*N
) {
223 return N
->getKind() == Scope
;
227 void printImpl(raw_ostream
&OS
, unsigned indent
) const override
;
228 bool isEqualImpl(const Matcher
*M
) const override
{ return false; }
231 /// RecordMatcher - Save the current node in the operand list.
232 class RecordMatcher
: public Matcher
{
233 /// WhatFor - This is a string indicating why we're recording this. This
234 /// should only be used for comment generation not anything semantic.
237 /// ResultNo - The slot number in the RecordedNodes vector that this will be,
238 /// just printed as a comment.
241 RecordMatcher(const std::string
&whatfor
, unsigned resultNo
)
242 : Matcher(RecordNode
), WhatFor(whatfor
), ResultNo(resultNo
) {}
244 const std::string
&getWhatFor() const { return WhatFor
; }
245 unsigned getResultNo() const { return ResultNo
; }
247 static bool classof(const Matcher
*N
) {
248 return N
->getKind() == RecordNode
;
252 void printImpl(raw_ostream
&OS
, unsigned indent
) const override
;
253 bool isEqualImpl(const Matcher
*M
) const override
{ return true; }
256 /// RecordChildMatcher - Save a numbered child of the current node, or fail
257 /// the match if it doesn't exist. This is logically equivalent to:
258 /// MoveChild N + RecordNode + MoveParent.
259 class RecordChildMatcher
: public Matcher
{
262 /// WhatFor - This is a string indicating why we're recording this. This
263 /// should only be used for comment generation not anything semantic.
266 /// ResultNo - The slot number in the RecordedNodes vector that this will be,
267 /// just printed as a comment.
270 RecordChildMatcher(unsigned childno
, const std::string
&whatfor
,
272 : Matcher(RecordChild
), ChildNo(childno
), WhatFor(whatfor
),
273 ResultNo(resultNo
) {}
275 unsigned getChildNo() const { return ChildNo
; }
276 const std::string
&getWhatFor() const { return WhatFor
; }
277 unsigned getResultNo() const { return ResultNo
; }
279 static bool classof(const Matcher
*N
) {
280 return N
->getKind() == RecordChild
;
284 void printImpl(raw_ostream
&OS
, unsigned indent
) const override
;
285 bool isEqualImpl(const Matcher
*M
) const override
{
286 return cast
<RecordChildMatcher
>(M
)->getChildNo() == getChildNo();
290 /// RecordMemRefMatcher - Save the current node's memref.
291 class RecordMemRefMatcher
: public Matcher
{
293 RecordMemRefMatcher() : Matcher(RecordMemRef
) {}
295 static bool classof(const Matcher
*N
) {
296 return N
->getKind() == RecordMemRef
;
300 void printImpl(raw_ostream
&OS
, unsigned indent
) const override
;
301 bool isEqualImpl(const Matcher
*M
) const override
{ return true; }
305 /// CaptureGlueInputMatcher - If the current record has a glue input, record
306 /// it so that it is used as an input to the generated code.
307 class CaptureGlueInputMatcher
: public Matcher
{
309 CaptureGlueInputMatcher() : Matcher(CaptureGlueInput
) {}
311 static bool classof(const Matcher
*N
) {
312 return N
->getKind() == CaptureGlueInput
;
316 void printImpl(raw_ostream
&OS
, unsigned indent
) const override
;
317 bool isEqualImpl(const Matcher
*M
) const override
{ return true; }
320 /// MoveChildMatcher - This tells the interpreter to move into the
321 /// specified child node.
322 class MoveChildMatcher
: public Matcher
{
325 MoveChildMatcher(unsigned childNo
) : Matcher(MoveChild
), ChildNo(childNo
) {}
327 unsigned getChildNo() const { return ChildNo
; }
329 static bool classof(const Matcher
*N
) {
330 return N
->getKind() == MoveChild
;
334 void printImpl(raw_ostream
&OS
, unsigned indent
) const override
;
335 bool isEqualImpl(const Matcher
*M
) const override
{
336 return cast
<MoveChildMatcher
>(M
)->getChildNo() == getChildNo();
340 /// MoveParentMatcher - This tells the interpreter to move to the parent
341 /// of the current node.
342 class MoveParentMatcher
: public Matcher
{
344 MoveParentMatcher() : Matcher(MoveParent
) {}
346 static bool classof(const Matcher
*N
) {
347 return N
->getKind() == MoveParent
;
351 void printImpl(raw_ostream
&OS
, unsigned indent
) const override
;
352 bool isEqualImpl(const Matcher
*M
) const override
{ return true; }
355 /// CheckSameMatcher - This checks to see if this node is exactly the same
356 /// node as the specified match that was recorded with 'Record'. This is used
357 /// when patterns have the same name in them, like '(mul GPR:$in, GPR:$in)'.
358 class CheckSameMatcher
: public Matcher
{
359 unsigned MatchNumber
;
361 CheckSameMatcher(unsigned matchnumber
)
362 : Matcher(CheckSame
), MatchNumber(matchnumber
) {}
364 unsigned getMatchNumber() const { return MatchNumber
; }
366 static bool classof(const Matcher
*N
) {
367 return N
->getKind() == CheckSame
;
371 void printImpl(raw_ostream
&OS
, unsigned indent
) const override
;
372 bool isEqualImpl(const Matcher
*M
) const override
{
373 return cast
<CheckSameMatcher
>(M
)->getMatchNumber() == getMatchNumber();
377 /// CheckChildSameMatcher - This checks to see if child node is exactly the same
378 /// node as the specified match that was recorded with 'Record'. This is used
379 /// when patterns have the same name in them, like '(mul GPR:$in, GPR:$in)'.
380 class CheckChildSameMatcher
: public Matcher
{
382 unsigned MatchNumber
;
384 CheckChildSameMatcher(unsigned childno
, unsigned matchnumber
)
385 : Matcher(CheckChildSame
), ChildNo(childno
), MatchNumber(matchnumber
) {}
387 unsigned getChildNo() const { return ChildNo
; }
388 unsigned getMatchNumber() const { return MatchNumber
; }
390 static bool classof(const Matcher
*N
) {
391 return N
->getKind() == CheckChildSame
;
395 void printImpl(raw_ostream
&OS
, unsigned indent
) const override
;
396 bool isEqualImpl(const Matcher
*M
) const override
{
397 return cast
<CheckChildSameMatcher
>(M
)->ChildNo
== ChildNo
&&
398 cast
<CheckChildSameMatcher
>(M
)->MatchNumber
== MatchNumber
;
402 /// CheckPatternPredicateMatcher - This checks the target-specific predicate
403 /// to see if the entire pattern is capable of matching. This predicate does
404 /// not take a node as input. This is used for subtarget feature checks etc.
405 class CheckPatternPredicateMatcher
: public Matcher
{
406 std::string Predicate
;
408 CheckPatternPredicateMatcher(StringRef predicate
)
409 : Matcher(CheckPatternPredicate
), Predicate(predicate
) {}
411 StringRef
getPredicate() const { return Predicate
; }
413 static bool classof(const Matcher
*N
) {
414 return N
->getKind() == CheckPatternPredicate
;
418 void printImpl(raw_ostream
&OS
, unsigned indent
) const override
;
419 bool isEqualImpl(const Matcher
*M
) const override
{
420 return cast
<CheckPatternPredicateMatcher
>(M
)->getPredicate() == Predicate
;
424 /// CheckPredicateMatcher - This checks the target-specific predicate to
425 /// see if the node is acceptable.
426 class CheckPredicateMatcher
: public Matcher
{
428 const SmallVector
<unsigned, 4> Operands
;
430 CheckPredicateMatcher(const TreePredicateFn
&pred
,
431 const SmallVectorImpl
<unsigned> &Operands
);
433 TreePredicateFn
getPredicate() const;
434 unsigned getNumOperands() const;
435 unsigned getOperandNo(unsigned i
) const;
437 static bool classof(const Matcher
*N
) {
438 return N
->getKind() == CheckPredicate
;
442 void printImpl(raw_ostream
&OS
, unsigned indent
) const override
;
443 bool isEqualImpl(const Matcher
*M
) const override
{
444 return cast
<CheckPredicateMatcher
>(M
)->Pred
== Pred
;
449 /// CheckOpcodeMatcher - This checks to see if the current node has the
450 /// specified opcode, if not it fails to match.
451 class CheckOpcodeMatcher
: public Matcher
{
452 const SDNodeInfo
&Opcode
;
454 CheckOpcodeMatcher(const SDNodeInfo
&opcode
)
455 : Matcher(CheckOpcode
), Opcode(opcode
) {}
457 const SDNodeInfo
&getOpcode() const { return Opcode
; }
459 static bool classof(const Matcher
*N
) {
460 return N
->getKind() == CheckOpcode
;
464 void printImpl(raw_ostream
&OS
, unsigned indent
) const override
;
465 bool isEqualImpl(const Matcher
*M
) const override
;
466 bool isContradictoryImpl(const Matcher
*M
) const override
;
469 /// SwitchOpcodeMatcher - Switch based on the current node's opcode, dispatching
470 /// to one matcher per opcode. If the opcode doesn't match any of the cases,
471 /// then the match fails. This is semantically equivalent to a Scope node where
472 /// every child does a CheckOpcode, but is much faster.
473 class SwitchOpcodeMatcher
: public Matcher
{
474 SmallVector
<std::pair
<const SDNodeInfo
*, Matcher
*>, 8> Cases
;
476 SwitchOpcodeMatcher(ArrayRef
<std::pair
<const SDNodeInfo
*, Matcher
*> > cases
)
477 : Matcher(SwitchOpcode
), Cases(cases
.begin(), cases
.end()) {}
478 ~SwitchOpcodeMatcher() override
;
480 static bool classof(const Matcher
*N
) {
481 return N
->getKind() == SwitchOpcode
;
484 unsigned getNumCases() const { return Cases
.size(); }
486 const SDNodeInfo
&getCaseOpcode(unsigned i
) const { return *Cases
[i
].first
; }
487 Matcher
*getCaseMatcher(unsigned i
) { return Cases
[i
].second
; }
488 const Matcher
*getCaseMatcher(unsigned i
) const { return Cases
[i
].second
; }
491 void printImpl(raw_ostream
&OS
, unsigned indent
) const override
;
492 bool isEqualImpl(const Matcher
*M
) const override
{ return false; }
495 /// CheckTypeMatcher - This checks to see if the current node has the
496 /// specified type at the specified result, if not it fails to match.
497 class CheckTypeMatcher
: public Matcher
{
498 MVT::SimpleValueType Type
;
501 CheckTypeMatcher(MVT::SimpleValueType type
, unsigned resno
)
502 : Matcher(CheckType
), Type(type
), ResNo(resno
) {}
504 MVT::SimpleValueType
getType() const { return Type
; }
505 unsigned getResNo() const { return ResNo
; }
507 static bool classof(const Matcher
*N
) {
508 return N
->getKind() == CheckType
;
512 void printImpl(raw_ostream
&OS
, unsigned indent
) const override
;
513 bool isEqualImpl(const Matcher
*M
) const override
{
514 return cast
<CheckTypeMatcher
>(M
)->Type
== Type
;
516 bool isContradictoryImpl(const Matcher
*M
) const override
;
519 /// SwitchTypeMatcher - Switch based on the current node's type, dispatching
520 /// to one matcher per case. If the type doesn't match any of the cases,
521 /// then the match fails. This is semantically equivalent to a Scope node where
522 /// every child does a CheckType, but is much faster.
523 class SwitchTypeMatcher
: public Matcher
{
524 SmallVector
<std::pair
<MVT::SimpleValueType
, Matcher
*>, 8> Cases
;
526 SwitchTypeMatcher(ArrayRef
<std::pair
<MVT::SimpleValueType
, Matcher
*> > cases
)
527 : Matcher(SwitchType
), Cases(cases
.begin(), cases
.end()) {}
528 ~SwitchTypeMatcher() override
;
530 static bool classof(const Matcher
*N
) {
531 return N
->getKind() == SwitchType
;
534 unsigned getNumCases() const { return Cases
.size(); }
536 MVT::SimpleValueType
getCaseType(unsigned i
) const { return Cases
[i
].first
; }
537 Matcher
*getCaseMatcher(unsigned i
) { return Cases
[i
].second
; }
538 const Matcher
*getCaseMatcher(unsigned i
) const { return Cases
[i
].second
; }
541 void printImpl(raw_ostream
&OS
, unsigned indent
) const override
;
542 bool isEqualImpl(const Matcher
*M
) const override
{ return false; }
546 /// CheckChildTypeMatcher - This checks to see if a child node has the
547 /// specified type, if not it fails to match.
548 class CheckChildTypeMatcher
: public Matcher
{
550 MVT::SimpleValueType Type
;
552 CheckChildTypeMatcher(unsigned childno
, MVT::SimpleValueType type
)
553 : Matcher(CheckChildType
), ChildNo(childno
), Type(type
) {}
555 unsigned getChildNo() const { return ChildNo
; }
556 MVT::SimpleValueType
getType() const { return Type
; }
558 static bool classof(const Matcher
*N
) {
559 return N
->getKind() == CheckChildType
;
563 void printImpl(raw_ostream
&OS
, unsigned indent
) const override
;
564 bool isEqualImpl(const Matcher
*M
) const override
{
565 return cast
<CheckChildTypeMatcher
>(M
)->ChildNo
== ChildNo
&&
566 cast
<CheckChildTypeMatcher
>(M
)->Type
== Type
;
568 bool isContradictoryImpl(const Matcher
*M
) const override
;
572 /// CheckIntegerMatcher - This checks to see if the current node is a
573 /// ConstantSDNode with the specified integer value, if not it fails to match.
574 class CheckIntegerMatcher
: public Matcher
{
577 CheckIntegerMatcher(int64_t value
)
578 : Matcher(CheckInteger
), Value(value
) {}
580 int64_t getValue() const { return Value
; }
582 static bool classof(const Matcher
*N
) {
583 return N
->getKind() == CheckInteger
;
587 void printImpl(raw_ostream
&OS
, unsigned indent
) const override
;
588 bool isEqualImpl(const Matcher
*M
) const override
{
589 return cast
<CheckIntegerMatcher
>(M
)->Value
== Value
;
591 bool isContradictoryImpl(const Matcher
*M
) const override
;
594 /// CheckChildIntegerMatcher - This checks to see if the child node is a
595 /// ConstantSDNode with a specified integer value, if not it fails to match.
596 class CheckChildIntegerMatcher
: public Matcher
{
600 CheckChildIntegerMatcher(unsigned childno
, int64_t value
)
601 : Matcher(CheckChildInteger
), ChildNo(childno
), Value(value
) {}
603 unsigned getChildNo() const { return ChildNo
; }
604 int64_t getValue() const { return Value
; }
606 static bool classof(const Matcher
*N
) {
607 return N
->getKind() == CheckChildInteger
;
611 void printImpl(raw_ostream
&OS
, unsigned indent
) const override
;
612 bool isEqualImpl(const Matcher
*M
) const override
{
613 return cast
<CheckChildIntegerMatcher
>(M
)->ChildNo
== ChildNo
&&
614 cast
<CheckChildIntegerMatcher
>(M
)->Value
== Value
;
616 bool isContradictoryImpl(const Matcher
*M
) const override
;
619 /// CheckCondCodeMatcher - This checks to see if the current node is a
620 /// CondCodeSDNode with the specified condition, if not it fails to match.
621 class CheckCondCodeMatcher
: public Matcher
{
622 StringRef CondCodeName
;
624 CheckCondCodeMatcher(StringRef condcodename
)
625 : Matcher(CheckCondCode
), CondCodeName(condcodename
) {}
627 StringRef
getCondCodeName() const { return CondCodeName
; }
629 static bool classof(const Matcher
*N
) {
630 return N
->getKind() == CheckCondCode
;
634 void printImpl(raw_ostream
&OS
, unsigned indent
) const override
;
635 bool isEqualImpl(const Matcher
*M
) const override
{
636 return cast
<CheckCondCodeMatcher
>(M
)->CondCodeName
== CondCodeName
;
638 bool isContradictoryImpl(const Matcher
*M
) const override
;
641 /// CheckChild2CondCodeMatcher - This checks to see if child 2 node is a
642 /// CondCodeSDNode with the specified condition, if not it fails to match.
643 class CheckChild2CondCodeMatcher
: public Matcher
{
644 StringRef CondCodeName
;
646 CheckChild2CondCodeMatcher(StringRef condcodename
)
647 : Matcher(CheckChild2CondCode
), CondCodeName(condcodename
) {}
649 StringRef
getCondCodeName() const { return CondCodeName
; }
651 static bool classof(const Matcher
*N
) {
652 return N
->getKind() == CheckChild2CondCode
;
656 void printImpl(raw_ostream
&OS
, unsigned indent
) const override
;
657 bool isEqualImpl(const Matcher
*M
) const override
{
658 return cast
<CheckChild2CondCodeMatcher
>(M
)->CondCodeName
== CondCodeName
;
660 bool isContradictoryImpl(const Matcher
*M
) const override
;
663 /// CheckValueTypeMatcher - This checks to see if the current node is a
664 /// VTSDNode with the specified type, if not it fails to match.
665 class CheckValueTypeMatcher
: public Matcher
{
668 CheckValueTypeMatcher(StringRef type_name
)
669 : Matcher(CheckValueType
), TypeName(type_name
) {}
671 StringRef
getTypeName() const { return TypeName
; }
673 static bool classof(const Matcher
*N
) {
674 return N
->getKind() == CheckValueType
;
678 void printImpl(raw_ostream
&OS
, unsigned indent
) const override
;
679 bool isEqualImpl(const Matcher
*M
) const override
{
680 return cast
<CheckValueTypeMatcher
>(M
)->TypeName
== TypeName
;
682 bool isContradictoryImpl(const Matcher
*M
) const override
;
687 /// CheckComplexPatMatcher - This node runs the specified ComplexPattern on
688 /// the current node.
689 class CheckComplexPatMatcher
: public Matcher
{
690 const ComplexPattern
&Pattern
;
692 /// MatchNumber - This is the recorded nodes slot that contains the node we
693 /// want to match against.
694 unsigned MatchNumber
;
696 /// Name - The name of the node we're matching, for comment emission.
699 /// FirstResult - This is the first slot in the RecordedNodes list that the
700 /// result of the match populates.
701 unsigned FirstResult
;
703 CheckComplexPatMatcher(const ComplexPattern
&pattern
, unsigned matchnumber
,
704 const std::string
&name
, unsigned firstresult
)
705 : Matcher(CheckComplexPat
), Pattern(pattern
), MatchNumber(matchnumber
),
706 Name(name
), FirstResult(firstresult
) {}
708 const ComplexPattern
&getPattern() const { return Pattern
; }
709 unsigned getMatchNumber() const { return MatchNumber
; }
711 std::string
getName() const { return Name
; }
712 unsigned getFirstResult() const { return FirstResult
; }
714 static bool classof(const Matcher
*N
) {
715 return N
->getKind() == CheckComplexPat
;
719 void printImpl(raw_ostream
&OS
, unsigned indent
) const override
;
720 bool isEqualImpl(const Matcher
*M
) const override
{
721 return &cast
<CheckComplexPatMatcher
>(M
)->Pattern
== &Pattern
&&
722 cast
<CheckComplexPatMatcher
>(M
)->MatchNumber
== MatchNumber
;
726 /// CheckAndImmMatcher - This checks to see if the current node is an 'and'
727 /// with something equivalent to the specified immediate.
728 class CheckAndImmMatcher
: public Matcher
{
731 CheckAndImmMatcher(int64_t value
)
732 : Matcher(CheckAndImm
), Value(value
) {}
734 int64_t getValue() const { return Value
; }
736 static bool classof(const Matcher
*N
) {
737 return N
->getKind() == CheckAndImm
;
741 void printImpl(raw_ostream
&OS
, unsigned indent
) const override
;
742 bool isEqualImpl(const Matcher
*M
) const override
{
743 return cast
<CheckAndImmMatcher
>(M
)->Value
== Value
;
747 /// CheckOrImmMatcher - This checks to see if the current node is an 'and'
748 /// with something equivalent to the specified immediate.
749 class CheckOrImmMatcher
: public Matcher
{
752 CheckOrImmMatcher(int64_t value
)
753 : Matcher(CheckOrImm
), Value(value
) {}
755 int64_t getValue() const { return Value
; }
757 static bool classof(const Matcher
*N
) {
758 return N
->getKind() == CheckOrImm
;
762 void printImpl(raw_ostream
&OS
, unsigned indent
) const override
;
763 bool isEqualImpl(const Matcher
*M
) const override
{
764 return cast
<CheckOrImmMatcher
>(M
)->Value
== Value
;
768 /// CheckImmAllOnesVMatcher - This checks if the current node is a build_vector
769 /// or splat_vector of all ones.
770 class CheckImmAllOnesVMatcher
: public Matcher
{
772 CheckImmAllOnesVMatcher() : Matcher(CheckImmAllOnesV
) {}
774 static bool classof(const Matcher
*N
) {
775 return N
->getKind() == CheckImmAllOnesV
;
779 void printImpl(raw_ostream
&OS
, unsigned indent
) const override
;
780 bool isEqualImpl(const Matcher
*M
) const override
{ return true; }
781 bool isContradictoryImpl(const Matcher
*M
) const override
;
784 /// CheckImmAllZerosVMatcher - This checks if the current node is a
785 /// build_vector or splat_vector of all zeros.
786 class CheckImmAllZerosVMatcher
: public Matcher
{
788 CheckImmAllZerosVMatcher() : Matcher(CheckImmAllZerosV
) {}
790 static bool classof(const Matcher
*N
) {
791 return N
->getKind() == CheckImmAllZerosV
;
795 void printImpl(raw_ostream
&OS
, unsigned indent
) const override
;
796 bool isEqualImpl(const Matcher
*M
) const override
{ return true; }
797 bool isContradictoryImpl(const Matcher
*M
) const override
;
800 /// CheckFoldableChainNodeMatcher - This checks to see if the current node
801 /// (which defines a chain operand) is safe to fold into a larger pattern.
802 class CheckFoldableChainNodeMatcher
: public Matcher
{
804 CheckFoldableChainNodeMatcher()
805 : Matcher(CheckFoldableChainNode
) {}
807 static bool classof(const Matcher
*N
) {
808 return N
->getKind() == CheckFoldableChainNode
;
812 void printImpl(raw_ostream
&OS
, unsigned indent
) const override
;
813 bool isEqualImpl(const Matcher
*M
) const override
{ return true; }
816 /// EmitIntegerMatcher - This creates a new TargetConstant.
817 class EmitIntegerMatcher
: public Matcher
{
819 MVT::SimpleValueType VT
;
821 EmitIntegerMatcher(int64_t val
, MVT::SimpleValueType vt
)
822 : Matcher(EmitInteger
), Val(val
), VT(vt
) {}
824 int64_t getValue() const { return Val
; }
825 MVT::SimpleValueType
getVT() const { return VT
; }
827 static bool classof(const Matcher
*N
) {
828 return N
->getKind() == EmitInteger
;
832 void printImpl(raw_ostream
&OS
, unsigned indent
) const override
;
833 bool isEqualImpl(const Matcher
*M
) const override
{
834 return cast
<EmitIntegerMatcher
>(M
)->Val
== Val
&&
835 cast
<EmitIntegerMatcher
>(M
)->VT
== VT
;
839 /// EmitStringIntegerMatcher - A target constant whose value is represented
841 class EmitStringIntegerMatcher
: public Matcher
{
843 MVT::SimpleValueType VT
;
845 EmitStringIntegerMatcher(const std::string
&val
, MVT::SimpleValueType vt
)
846 : Matcher(EmitStringInteger
), Val(val
), VT(vt
) {}
848 const std::string
&getValue() const { return Val
; }
849 MVT::SimpleValueType
getVT() const { return VT
; }
851 static bool classof(const Matcher
*N
) {
852 return N
->getKind() == EmitStringInteger
;
856 void printImpl(raw_ostream
&OS
, unsigned indent
) const override
;
857 bool isEqualImpl(const Matcher
*M
) const override
{
858 return cast
<EmitStringIntegerMatcher
>(M
)->Val
== Val
&&
859 cast
<EmitStringIntegerMatcher
>(M
)->VT
== VT
;
863 /// EmitRegisterMatcher - This creates a new TargetConstant.
864 class EmitRegisterMatcher
: public Matcher
{
865 /// Reg - The def for the register that we're emitting. If this is null, then
866 /// this is a reference to zero_reg.
867 const CodeGenRegister
*Reg
;
868 MVT::SimpleValueType VT
;
870 EmitRegisterMatcher(const CodeGenRegister
*reg
, MVT::SimpleValueType vt
)
871 : Matcher(EmitRegister
), Reg(reg
), VT(vt
) {}
873 const CodeGenRegister
*getReg() const { return Reg
; }
874 MVT::SimpleValueType
getVT() const { return VT
; }
876 static bool classof(const Matcher
*N
) {
877 return N
->getKind() == EmitRegister
;
881 void printImpl(raw_ostream
&OS
, unsigned indent
) const override
;
882 bool isEqualImpl(const Matcher
*M
) const override
{
883 return cast
<EmitRegisterMatcher
>(M
)->Reg
== Reg
&&
884 cast
<EmitRegisterMatcher
>(M
)->VT
== VT
;
888 /// EmitConvertToTargetMatcher - Emit an operation that reads a specified
889 /// recorded node and converts it from being a ISD::Constant to
890 /// ISD::TargetConstant, likewise for ConstantFP.
891 class EmitConvertToTargetMatcher
: public Matcher
{
894 EmitConvertToTargetMatcher(unsigned slot
)
895 : Matcher(EmitConvertToTarget
), Slot(slot
) {}
897 unsigned getSlot() const { return Slot
; }
899 static bool classof(const Matcher
*N
) {
900 return N
->getKind() == EmitConvertToTarget
;
904 void printImpl(raw_ostream
&OS
, unsigned indent
) const override
;
905 bool isEqualImpl(const Matcher
*M
) const override
{
906 return cast
<EmitConvertToTargetMatcher
>(M
)->Slot
== Slot
;
910 /// EmitMergeInputChainsMatcher - Emit a node that merges a list of input
911 /// chains together with a token factor. The list of nodes are the nodes in the
912 /// matched pattern that have chain input/outputs. This node adds all input
913 /// chains of these nodes if they are not themselves a node in the pattern.
914 class EmitMergeInputChainsMatcher
: public Matcher
{
915 SmallVector
<unsigned, 3> ChainNodes
;
917 EmitMergeInputChainsMatcher(ArrayRef
<unsigned> nodes
)
918 : Matcher(EmitMergeInputChains
), ChainNodes(nodes
.begin(), nodes
.end()) {}
920 unsigned getNumNodes() const { return ChainNodes
.size(); }
922 unsigned getNode(unsigned i
) const {
923 assert(i
< ChainNodes
.size());
924 return ChainNodes
[i
];
927 static bool classof(const Matcher
*N
) {
928 return N
->getKind() == EmitMergeInputChains
;
932 void printImpl(raw_ostream
&OS
, unsigned indent
) const override
;
933 bool isEqualImpl(const Matcher
*M
) const override
{
934 return cast
<EmitMergeInputChainsMatcher
>(M
)->ChainNodes
== ChainNodes
;
938 /// EmitCopyToRegMatcher - Emit a CopyToReg node from a value to a physreg,
939 /// pushing the chain and glue results.
941 class EmitCopyToRegMatcher
: public Matcher
{
942 unsigned SrcSlot
; // Value to copy into the physreg.
943 const CodeGenRegister
*DestPhysReg
;
946 EmitCopyToRegMatcher(unsigned srcSlot
,
947 const CodeGenRegister
*destPhysReg
)
948 : Matcher(EmitCopyToReg
), SrcSlot(srcSlot
), DestPhysReg(destPhysReg
) {}
950 unsigned getSrcSlot() const { return SrcSlot
; }
951 const CodeGenRegister
*getDestPhysReg() const { return DestPhysReg
; }
953 static bool classof(const Matcher
*N
) {
954 return N
->getKind() == EmitCopyToReg
;
958 void printImpl(raw_ostream
&OS
, unsigned indent
) const override
;
959 bool isEqualImpl(const Matcher
*M
) const override
{
960 return cast
<EmitCopyToRegMatcher
>(M
)->SrcSlot
== SrcSlot
&&
961 cast
<EmitCopyToRegMatcher
>(M
)->DestPhysReg
== DestPhysReg
;
967 /// EmitNodeXFormMatcher - Emit an operation that runs an SDNodeXForm on a
968 /// recorded node and records the result.
969 class EmitNodeXFormMatcher
: public Matcher
{
973 EmitNodeXFormMatcher(unsigned slot
, Record
*nodeXForm
)
974 : Matcher(EmitNodeXForm
), Slot(slot
), NodeXForm(nodeXForm
) {}
976 unsigned getSlot() const { return Slot
; }
977 Record
*getNodeXForm() const { return NodeXForm
; }
979 static bool classof(const Matcher
*N
) {
980 return N
->getKind() == EmitNodeXForm
;
984 void printImpl(raw_ostream
&OS
, unsigned indent
) const override
;
985 bool isEqualImpl(const Matcher
*M
) const override
{
986 return cast
<EmitNodeXFormMatcher
>(M
)->Slot
== Slot
&&
987 cast
<EmitNodeXFormMatcher
>(M
)->NodeXForm
== NodeXForm
;
991 /// EmitNodeMatcherCommon - Common class shared between EmitNode and
993 class EmitNodeMatcherCommon
: public Matcher
{
994 std::string OpcodeName
;
995 const SmallVector
<MVT::SimpleValueType
, 3> VTs
;
996 const SmallVector
<unsigned, 6> Operands
;
997 bool HasChain
, HasInGlue
, HasOutGlue
, HasMemRefs
;
999 /// NumFixedArityOperands - If this is a fixed arity node, this is set to -1.
1000 /// If this is a varidic node, this is set to the number of fixed arity
1001 /// operands in the root of the pattern. The rest are appended to this node.
1002 int NumFixedArityOperands
;
1004 EmitNodeMatcherCommon(const std::string
&opcodeName
,
1005 ArrayRef
<MVT::SimpleValueType
> vts
,
1006 ArrayRef
<unsigned> operands
,
1007 bool hasChain
, bool hasInGlue
, bool hasOutGlue
,
1009 int numfixedarityoperands
, bool isMorphNodeTo
)
1010 : Matcher(isMorphNodeTo
? MorphNodeTo
: EmitNode
), OpcodeName(opcodeName
),
1011 VTs(vts
.begin(), vts
.end()), Operands(operands
.begin(), operands
.end()),
1012 HasChain(hasChain
), HasInGlue(hasInGlue
), HasOutGlue(hasOutGlue
),
1013 HasMemRefs(hasmemrefs
), NumFixedArityOperands(numfixedarityoperands
) {}
1015 const std::string
&getOpcodeName() const { return OpcodeName
; }
1017 unsigned getNumVTs() const { return VTs
.size(); }
1018 MVT::SimpleValueType
getVT(unsigned i
) const {
1019 assert(i
< VTs
.size());
1023 unsigned getNumOperands() const { return Operands
.size(); }
1024 unsigned getOperand(unsigned i
) const {
1025 assert(i
< Operands
.size());
1029 const SmallVectorImpl
<MVT::SimpleValueType
> &getVTList() const { return VTs
; }
1030 const SmallVectorImpl
<unsigned> &getOperandList() const { return Operands
; }
1033 bool hasChain() const { return HasChain
; }
1034 bool hasInFlag() const { return HasInGlue
; }
1035 bool hasOutFlag() const { return HasOutGlue
; }
1036 bool hasMemRefs() const { return HasMemRefs
; }
1037 int getNumFixedArityOperands() const { return NumFixedArityOperands
; }
1039 static bool classof(const Matcher
*N
) {
1040 return N
->getKind() == EmitNode
|| N
->getKind() == MorphNodeTo
;
1044 void printImpl(raw_ostream
&OS
, unsigned indent
) const override
;
1045 bool isEqualImpl(const Matcher
*M
) const override
;
1048 /// EmitNodeMatcher - This signals a successful match and generates a node.
1049 class EmitNodeMatcher
: public EmitNodeMatcherCommon
{
1050 void anchor() override
;
1051 unsigned FirstResultSlot
;
1053 EmitNodeMatcher(const std::string
&opcodeName
,
1054 ArrayRef
<MVT::SimpleValueType
> vts
,
1055 ArrayRef
<unsigned> operands
,
1056 bool hasChain
, bool hasInFlag
, bool hasOutFlag
,
1058 int numfixedarityoperands
, unsigned firstresultslot
)
1059 : EmitNodeMatcherCommon(opcodeName
, vts
, operands
, hasChain
,
1060 hasInFlag
, hasOutFlag
, hasmemrefs
,
1061 numfixedarityoperands
, false),
1062 FirstResultSlot(firstresultslot
) {}
1064 unsigned getFirstResultSlot() const { return FirstResultSlot
; }
1066 static bool classof(const Matcher
*N
) {
1067 return N
->getKind() == EmitNode
;
1072 class MorphNodeToMatcher
: public EmitNodeMatcherCommon
{
1073 void anchor() override
;
1074 const PatternToMatch
&Pattern
;
1076 MorphNodeToMatcher(const std::string
&opcodeName
,
1077 ArrayRef
<MVT::SimpleValueType
> vts
,
1078 ArrayRef
<unsigned> operands
,
1079 bool hasChain
, bool hasInFlag
, bool hasOutFlag
,
1081 int numfixedarityoperands
, const PatternToMatch
&pattern
)
1082 : EmitNodeMatcherCommon(opcodeName
, vts
, operands
, hasChain
,
1083 hasInFlag
, hasOutFlag
, hasmemrefs
,
1084 numfixedarityoperands
, true),
1088 const PatternToMatch
&getPattern() const { return Pattern
; }
1090 static bool classof(const Matcher
*N
) {
1091 return N
->getKind() == MorphNodeTo
;
1095 /// CompleteMatchMatcher - Complete a match by replacing the results of the
1096 /// pattern with the newly generated nodes. This also prints a comment
1097 /// indicating the source and dest patterns.
1098 class CompleteMatchMatcher
: public Matcher
{
1099 SmallVector
<unsigned, 2> Results
;
1100 const PatternToMatch
&Pattern
;
1102 CompleteMatchMatcher(ArrayRef
<unsigned> results
,
1103 const PatternToMatch
&pattern
)
1104 : Matcher(CompleteMatch
), Results(results
.begin(), results
.end()),
1107 unsigned getNumResults() const { return Results
.size(); }
1108 unsigned getResult(unsigned R
) const { return Results
[R
]; }
1109 const PatternToMatch
&getPattern() const { return Pattern
; }
1111 static bool classof(const Matcher
*N
) {
1112 return N
->getKind() == CompleteMatch
;
1116 void printImpl(raw_ostream
&OS
, unsigned indent
) const override
;
1117 bool isEqualImpl(const Matcher
*M
) const override
{
1118 return cast
<CompleteMatchMatcher
>(M
)->Results
== Results
&&
1119 &cast
<CompleteMatchMatcher
>(M
)->Pattern
== &Pattern
;
1123 } // end namespace llvm