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(const 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 virtual void anchor();
47 // Matcher state manipulation.
48 Scope
, // Push a checking scope.
49 RecordNode
, // Record the current node.
50 RecordChild
, // Record a child of the current node.
51 RecordMemRef
, // Record the memref in the current node.
52 CaptureGlueInput
, // If the current node has an input glue, save it.
53 MoveChild
, // Move current node to specified child.
54 MoveParent
, // Move current node to parent.
56 // Predicate checking.
57 CheckSame
, // Fail if not same as prev match.
58 CheckChildSame
, // Fail if child not same as prev match.
59 CheckPatternPredicate
,
60 CheckPredicate
, // Fail if node predicate fails.
61 CheckOpcode
, // Fail if not opcode.
62 SwitchOpcode
, // Dispatch based on opcode.
63 CheckType
, // Fail if not correct type.
64 SwitchType
, // Dispatch based on type.
65 CheckChildType
, // Fail if child has wrong type.
66 CheckInteger
, // Fail if wrong val.
67 CheckChildInteger
, // Fail if child is wrong val.
68 CheckCondCode
, // Fail if not condcode.
69 CheckChild2CondCode
, // Fail if child is wrong condcode.
76 CheckFoldableChainNode
,
78 // Node creation/emisssion.
79 EmitInteger
, // Create a TargetConstant
80 EmitStringInteger
, // Create a TargetConstant from a string.
81 EmitRegister
, // Create a register.
82 EmitConvertToTarget
, // Convert a imm/fpimm to target imm/fpimm
83 EmitMergeInputChains
, // Merge together a chains for an input.
84 EmitCopyToReg
, // Emit a copytoreg into a physreg.
85 EmitNode
, // Create a DAG node
86 EmitNodeXForm
, // Run a SDNodeXForm
87 CompleteMatch
, // Finish a match and update the results.
88 MorphNodeTo
// Build a node, finish a match and update results.
93 Matcher(KindTy K
) : Kind(K
) {}
97 KindTy
getKind() const { return Kind
; }
99 Matcher
*getNext() { return Next
.get(); }
100 const Matcher
*getNext() const { return Next
.get(); }
101 void setNext(Matcher
*C
) { Next
.reset(C
); }
102 Matcher
*takeNext() { return Next
.release(); }
104 std::unique_ptr
<Matcher
> &getNextPtr() { return Next
; }
106 bool isEqual(const Matcher
*M
) const {
107 if (getKind() != M
->getKind()) return false;
108 return isEqualImpl(M
);
111 /// isSimplePredicateNode - Return true if this is a simple predicate that
112 /// operates on the node or its children without potential side effects or a
113 /// change of the current node.
114 bool isSimplePredicateNode() const {
116 default: return false;
119 case CheckPatternPredicate
:
125 case CheckChildInteger
:
127 case CheckChild2CondCode
:
131 case CheckImmAllOnesV
:
132 case CheckImmAllZerosV
:
133 case CheckFoldableChainNode
:
138 /// isSimplePredicateOrRecordNode - Return true if this is a record node or
139 /// a simple predicate.
140 bool isSimplePredicateOrRecordNode() const {
141 return isSimplePredicateNode() ||
142 getKind() == RecordNode
|| getKind() == RecordChild
;
145 /// unlinkNode - Unlink the specified node from this chain. If Other == this,
146 /// we unlink the next pointer and return it. Otherwise we unlink Other from
147 /// the list and return this.
148 Matcher
*unlinkNode(Matcher
*Other
);
150 /// canMoveBefore - Return true if this matcher is the same as Other, or if
151 /// we can move this matcher past all of the nodes in-between Other and this
152 /// node. Other must be equal to or before this.
153 bool canMoveBefore(const Matcher
*Other
) const;
155 /// canMoveBeforeNode - Return true if it is safe to move the current matcher
156 /// across the specified one.
157 bool canMoveBeforeNode(const Matcher
*Other
) const;
159 /// isContradictory - Return true of these two matchers could never match on
161 bool isContradictory(const Matcher
*Other
) const {
162 // Since this predicate is reflexive, we canonicalize the ordering so that
163 // we always match a node against nodes with kinds that are greater or equal
164 // to them. For example, we'll pass in a CheckType node as an argument to
165 // the CheckOpcode method, not the other way around.
166 if (getKind() < Other
->getKind())
167 return isContradictoryImpl(Other
);
168 return Other
->isContradictoryImpl(this);
171 void print(raw_ostream
&OS
, unsigned indent
= 0) const;
172 void printOne(raw_ostream
&OS
) const;
175 virtual void printImpl(raw_ostream
&OS
, unsigned indent
) const = 0;
176 virtual bool isEqualImpl(const Matcher
*M
) const = 0;
177 virtual bool isContradictoryImpl(const Matcher
*M
) const { return false; }
180 /// ScopeMatcher - This attempts to match each of its children to find the first
181 /// one that successfully matches. If one child fails, it tries the next child.
182 /// If none of the children match then this check fails. It never has a 'next'.
183 class ScopeMatcher
: public Matcher
{
184 SmallVector
<Matcher
*, 4> Children
;
186 ScopeMatcher(ArrayRef
<Matcher
*> children
)
187 : Matcher(Scope
), Children(children
.begin(), children
.end()) {
189 ~ScopeMatcher() override
;
191 unsigned getNumChildren() const { return Children
.size(); }
193 Matcher
*getChild(unsigned i
) { return Children
[i
]; }
194 const Matcher
*getChild(unsigned i
) const { return Children
[i
]; }
196 void resetChild(unsigned i
, Matcher
*N
) {
201 Matcher
*takeChild(unsigned i
) {
202 Matcher
*Res
= Children
[i
];
203 Children
[i
] = nullptr;
207 void setNumChildren(unsigned NC
) {
208 if (NC
< Children
.size()) {
209 // delete any children we're about to lose pointers to.
210 for (unsigned i
= NC
, e
= Children
.size(); i
!= e
; ++i
)
216 static bool classof(const Matcher
*N
) {
217 return N
->getKind() == Scope
;
221 void printImpl(raw_ostream
&OS
, unsigned indent
) const override
;
222 bool isEqualImpl(const Matcher
*M
) const override
{ return false; }
225 /// RecordMatcher - Save the current node in the operand list.
226 class RecordMatcher
: public Matcher
{
227 /// WhatFor - This is a string indicating why we're recording this. This
228 /// should only be used for comment generation not anything semantic.
231 /// ResultNo - The slot number in the RecordedNodes vector that this will be,
232 /// just printed as a comment.
235 RecordMatcher(const std::string
&whatfor
, unsigned resultNo
)
236 : Matcher(RecordNode
), WhatFor(whatfor
), ResultNo(resultNo
) {}
238 const std::string
&getWhatFor() const { return WhatFor
; }
239 unsigned getResultNo() const { return ResultNo
; }
241 static bool classof(const Matcher
*N
) {
242 return N
->getKind() == RecordNode
;
246 void printImpl(raw_ostream
&OS
, unsigned indent
) const override
;
247 bool isEqualImpl(const Matcher
*M
) const override
{ return true; }
250 /// RecordChildMatcher - Save a numbered child of the current node, or fail
251 /// the match if it doesn't exist. This is logically equivalent to:
252 /// MoveChild N + RecordNode + MoveParent.
253 class RecordChildMatcher
: public Matcher
{
256 /// WhatFor - This is a string indicating why we're recording this. This
257 /// should only be used for comment generation not anything semantic.
260 /// ResultNo - The slot number in the RecordedNodes vector that this will be,
261 /// just printed as a comment.
264 RecordChildMatcher(unsigned childno
, const std::string
&whatfor
,
266 : Matcher(RecordChild
), ChildNo(childno
), WhatFor(whatfor
),
267 ResultNo(resultNo
) {}
269 unsigned getChildNo() const { return ChildNo
; }
270 const std::string
&getWhatFor() const { return WhatFor
; }
271 unsigned getResultNo() const { return ResultNo
; }
273 static bool classof(const Matcher
*N
) {
274 return N
->getKind() == RecordChild
;
278 void printImpl(raw_ostream
&OS
, unsigned indent
) const override
;
279 bool isEqualImpl(const Matcher
*M
) const override
{
280 return cast
<RecordChildMatcher
>(M
)->getChildNo() == getChildNo();
284 /// RecordMemRefMatcher - Save the current node's memref.
285 class RecordMemRefMatcher
: public Matcher
{
287 RecordMemRefMatcher() : Matcher(RecordMemRef
) {}
289 static bool classof(const Matcher
*N
) {
290 return N
->getKind() == RecordMemRef
;
294 void printImpl(raw_ostream
&OS
, unsigned indent
) const override
;
295 bool isEqualImpl(const Matcher
*M
) const override
{ return true; }
299 /// CaptureGlueInputMatcher - If the current record has a glue input, record
300 /// it so that it is used as an input to the generated code.
301 class CaptureGlueInputMatcher
: public Matcher
{
303 CaptureGlueInputMatcher() : Matcher(CaptureGlueInput
) {}
305 static bool classof(const Matcher
*N
) {
306 return N
->getKind() == CaptureGlueInput
;
310 void printImpl(raw_ostream
&OS
, unsigned indent
) const override
;
311 bool isEqualImpl(const Matcher
*M
) const override
{ return true; }
314 /// MoveChildMatcher - This tells the interpreter to move into the
315 /// specified child node.
316 class MoveChildMatcher
: public Matcher
{
319 MoveChildMatcher(unsigned childNo
) : Matcher(MoveChild
), ChildNo(childNo
) {}
321 unsigned getChildNo() const { return ChildNo
; }
323 static bool classof(const Matcher
*N
) {
324 return N
->getKind() == MoveChild
;
328 void printImpl(raw_ostream
&OS
, unsigned indent
) const override
;
329 bool isEqualImpl(const Matcher
*M
) const override
{
330 return cast
<MoveChildMatcher
>(M
)->getChildNo() == getChildNo();
334 /// MoveParentMatcher - This tells the interpreter to move to the parent
335 /// of the current node.
336 class MoveParentMatcher
: public Matcher
{
338 MoveParentMatcher() : Matcher(MoveParent
) {}
340 static bool classof(const Matcher
*N
) {
341 return N
->getKind() == MoveParent
;
345 void printImpl(raw_ostream
&OS
, unsigned indent
) const override
;
346 bool isEqualImpl(const Matcher
*M
) const override
{ return true; }
349 /// CheckSameMatcher - This checks to see if this node is exactly the same
350 /// node as the specified match that was recorded with 'Record'. This is used
351 /// when patterns have the same name in them, like '(mul GPR:$in, GPR:$in)'.
352 class CheckSameMatcher
: public Matcher
{
353 unsigned MatchNumber
;
355 CheckSameMatcher(unsigned matchnumber
)
356 : Matcher(CheckSame
), MatchNumber(matchnumber
) {}
358 unsigned getMatchNumber() const { return MatchNumber
; }
360 static bool classof(const Matcher
*N
) {
361 return N
->getKind() == CheckSame
;
365 void printImpl(raw_ostream
&OS
, unsigned indent
) const override
;
366 bool isEqualImpl(const Matcher
*M
) const override
{
367 return cast
<CheckSameMatcher
>(M
)->getMatchNumber() == getMatchNumber();
371 /// CheckChildSameMatcher - This checks to see if child node is exactly the same
372 /// node as the specified match that was recorded with 'Record'. This is used
373 /// when patterns have the same name in them, like '(mul GPR:$in, GPR:$in)'.
374 class CheckChildSameMatcher
: public Matcher
{
376 unsigned MatchNumber
;
378 CheckChildSameMatcher(unsigned childno
, unsigned matchnumber
)
379 : Matcher(CheckChildSame
), ChildNo(childno
), MatchNumber(matchnumber
) {}
381 unsigned getChildNo() const { return ChildNo
; }
382 unsigned getMatchNumber() const { return MatchNumber
; }
384 static bool classof(const Matcher
*N
) {
385 return N
->getKind() == CheckChildSame
;
389 void printImpl(raw_ostream
&OS
, unsigned indent
) const override
;
390 bool isEqualImpl(const Matcher
*M
) const override
{
391 return cast
<CheckChildSameMatcher
>(M
)->ChildNo
== ChildNo
&&
392 cast
<CheckChildSameMatcher
>(M
)->MatchNumber
== MatchNumber
;
396 /// CheckPatternPredicateMatcher - This checks the target-specific predicate
397 /// to see if the entire pattern is capable of matching. This predicate does
398 /// not take a node as input. This is used for subtarget feature checks etc.
399 class CheckPatternPredicateMatcher
: public Matcher
{
400 std::string Predicate
;
402 CheckPatternPredicateMatcher(StringRef predicate
)
403 : Matcher(CheckPatternPredicate
), Predicate(predicate
) {}
405 StringRef
getPredicate() const { return Predicate
; }
407 static bool classof(const Matcher
*N
) {
408 return N
->getKind() == CheckPatternPredicate
;
412 void printImpl(raw_ostream
&OS
, unsigned indent
) const override
;
413 bool isEqualImpl(const Matcher
*M
) const override
{
414 return cast
<CheckPatternPredicateMatcher
>(M
)->getPredicate() == Predicate
;
418 /// CheckPredicateMatcher - This checks the target-specific predicate to
419 /// see if the node is acceptable.
420 class CheckPredicateMatcher
: public Matcher
{
422 const SmallVector
<unsigned, 4> Operands
;
424 CheckPredicateMatcher(const TreePredicateFn
&pred
,
425 const SmallVectorImpl
<unsigned> &Operands
);
427 TreePredicateFn
getPredicate() const;
428 unsigned getNumOperands() const;
429 unsigned getOperandNo(unsigned i
) const;
431 static bool classof(const Matcher
*N
) {
432 return N
->getKind() == CheckPredicate
;
436 void printImpl(raw_ostream
&OS
, unsigned indent
) const override
;
437 bool isEqualImpl(const Matcher
*M
) const override
{
438 return cast
<CheckPredicateMatcher
>(M
)->Pred
== Pred
;
443 /// CheckOpcodeMatcher - This checks to see if the current node has the
444 /// specified opcode, if not it fails to match.
445 class CheckOpcodeMatcher
: public Matcher
{
446 const SDNodeInfo
&Opcode
;
448 CheckOpcodeMatcher(const SDNodeInfo
&opcode
)
449 : Matcher(CheckOpcode
), Opcode(opcode
) {}
451 const SDNodeInfo
&getOpcode() const { return Opcode
; }
453 static bool classof(const Matcher
*N
) {
454 return N
->getKind() == CheckOpcode
;
458 void printImpl(raw_ostream
&OS
, unsigned indent
) const override
;
459 bool isEqualImpl(const Matcher
*M
) const override
;
460 bool isContradictoryImpl(const Matcher
*M
) const override
;
463 /// SwitchOpcodeMatcher - Switch based on the current node's opcode, dispatching
464 /// to one matcher per opcode. If the opcode doesn't match any of the cases,
465 /// then the match fails. This is semantically equivalent to a Scope node where
466 /// every child does a CheckOpcode, but is much faster.
467 class SwitchOpcodeMatcher
: public Matcher
{
468 SmallVector
<std::pair
<const SDNodeInfo
*, Matcher
*>, 8> Cases
;
470 SwitchOpcodeMatcher(ArrayRef
<std::pair
<const SDNodeInfo
*, Matcher
*> > cases
)
471 : Matcher(SwitchOpcode
), Cases(cases
.begin(), cases
.end()) {}
472 ~SwitchOpcodeMatcher() override
;
474 static bool classof(const Matcher
*N
) {
475 return N
->getKind() == SwitchOpcode
;
478 unsigned getNumCases() const { return Cases
.size(); }
480 const SDNodeInfo
&getCaseOpcode(unsigned i
) const { return *Cases
[i
].first
; }
481 Matcher
*getCaseMatcher(unsigned i
) { return Cases
[i
].second
; }
482 const Matcher
*getCaseMatcher(unsigned i
) const { return Cases
[i
].second
; }
485 void printImpl(raw_ostream
&OS
, unsigned indent
) const override
;
486 bool isEqualImpl(const Matcher
*M
) const override
{ return false; }
489 /// CheckTypeMatcher - This checks to see if the current node has the
490 /// specified type at the specified result, if not it fails to match.
491 class CheckTypeMatcher
: public Matcher
{
492 MVT::SimpleValueType Type
;
495 CheckTypeMatcher(MVT::SimpleValueType type
, unsigned resno
)
496 : Matcher(CheckType
), Type(type
), ResNo(resno
) {}
498 MVT::SimpleValueType
getType() const { return Type
; }
499 unsigned getResNo() const { return ResNo
; }
501 static bool classof(const Matcher
*N
) {
502 return N
->getKind() == CheckType
;
506 void printImpl(raw_ostream
&OS
, unsigned indent
) const override
;
507 bool isEqualImpl(const Matcher
*M
) const override
{
508 return cast
<CheckTypeMatcher
>(M
)->Type
== Type
;
510 bool isContradictoryImpl(const Matcher
*M
) const override
;
513 /// SwitchTypeMatcher - Switch based on the current node's type, dispatching
514 /// to one matcher per case. If the type doesn't match any of the cases,
515 /// then the match fails. This is semantically equivalent to a Scope node where
516 /// every child does a CheckType, but is much faster.
517 class SwitchTypeMatcher
: public Matcher
{
518 SmallVector
<std::pair
<MVT::SimpleValueType
, Matcher
*>, 8> Cases
;
520 SwitchTypeMatcher(ArrayRef
<std::pair
<MVT::SimpleValueType
, Matcher
*> > cases
)
521 : Matcher(SwitchType
), Cases(cases
.begin(), cases
.end()) {}
522 ~SwitchTypeMatcher() override
;
524 static bool classof(const Matcher
*N
) {
525 return N
->getKind() == SwitchType
;
528 unsigned getNumCases() const { return Cases
.size(); }
530 MVT::SimpleValueType
getCaseType(unsigned i
) const { return Cases
[i
].first
; }
531 Matcher
*getCaseMatcher(unsigned i
) { return Cases
[i
].second
; }
532 const Matcher
*getCaseMatcher(unsigned i
) const { return Cases
[i
].second
; }
535 void printImpl(raw_ostream
&OS
, unsigned indent
) const override
;
536 bool isEqualImpl(const Matcher
*M
) const override
{ return false; }
540 /// CheckChildTypeMatcher - This checks to see if a child node has the
541 /// specified type, if not it fails to match.
542 class CheckChildTypeMatcher
: public Matcher
{
544 MVT::SimpleValueType Type
;
546 CheckChildTypeMatcher(unsigned childno
, MVT::SimpleValueType type
)
547 : Matcher(CheckChildType
), ChildNo(childno
), Type(type
) {}
549 unsigned getChildNo() const { return ChildNo
; }
550 MVT::SimpleValueType
getType() const { return Type
; }
552 static bool classof(const Matcher
*N
) {
553 return N
->getKind() == CheckChildType
;
557 void printImpl(raw_ostream
&OS
, unsigned indent
) const override
;
558 bool isEqualImpl(const Matcher
*M
) const override
{
559 return cast
<CheckChildTypeMatcher
>(M
)->ChildNo
== ChildNo
&&
560 cast
<CheckChildTypeMatcher
>(M
)->Type
== Type
;
562 bool isContradictoryImpl(const Matcher
*M
) const override
;
566 /// CheckIntegerMatcher - This checks to see if the current node is a
567 /// ConstantSDNode with the specified integer value, if not it fails to match.
568 class CheckIntegerMatcher
: public Matcher
{
571 CheckIntegerMatcher(int64_t value
)
572 : Matcher(CheckInteger
), Value(value
) {}
574 int64_t getValue() const { return Value
; }
576 static bool classof(const Matcher
*N
) {
577 return N
->getKind() == CheckInteger
;
581 void printImpl(raw_ostream
&OS
, unsigned indent
) const override
;
582 bool isEqualImpl(const Matcher
*M
) const override
{
583 return cast
<CheckIntegerMatcher
>(M
)->Value
== Value
;
585 bool isContradictoryImpl(const Matcher
*M
) const override
;
588 /// CheckChildIntegerMatcher - This checks to see if the child node is a
589 /// ConstantSDNode with a specified integer value, if not it fails to match.
590 class CheckChildIntegerMatcher
: public Matcher
{
594 CheckChildIntegerMatcher(unsigned childno
, int64_t value
)
595 : Matcher(CheckChildInteger
), ChildNo(childno
), Value(value
) {}
597 unsigned getChildNo() const { return ChildNo
; }
598 int64_t getValue() const { return Value
; }
600 static bool classof(const Matcher
*N
) {
601 return N
->getKind() == CheckChildInteger
;
605 void printImpl(raw_ostream
&OS
, unsigned indent
) const override
;
606 bool isEqualImpl(const Matcher
*M
) const override
{
607 return cast
<CheckChildIntegerMatcher
>(M
)->ChildNo
== ChildNo
&&
608 cast
<CheckChildIntegerMatcher
>(M
)->Value
== Value
;
610 bool isContradictoryImpl(const Matcher
*M
) const override
;
613 /// CheckCondCodeMatcher - This checks to see if the current node is a
614 /// CondCodeSDNode with the specified condition, if not it fails to match.
615 class CheckCondCodeMatcher
: public Matcher
{
616 StringRef CondCodeName
;
618 CheckCondCodeMatcher(StringRef condcodename
)
619 : Matcher(CheckCondCode
), CondCodeName(condcodename
) {}
621 StringRef
getCondCodeName() const { return CondCodeName
; }
623 static bool classof(const Matcher
*N
) {
624 return N
->getKind() == CheckCondCode
;
628 void printImpl(raw_ostream
&OS
, unsigned indent
) const override
;
629 bool isEqualImpl(const Matcher
*M
) const override
{
630 return cast
<CheckCondCodeMatcher
>(M
)->CondCodeName
== CondCodeName
;
634 /// CheckChild2CondCodeMatcher - This checks to see if child 2 node is a
635 /// CondCodeSDNode with the specified condition, if not it fails to match.
636 class CheckChild2CondCodeMatcher
: public Matcher
{
637 StringRef CondCodeName
;
639 CheckChild2CondCodeMatcher(StringRef condcodename
)
640 : Matcher(CheckChild2CondCode
), CondCodeName(condcodename
) {}
642 StringRef
getCondCodeName() const { return CondCodeName
; }
644 static bool classof(const Matcher
*N
) {
645 return N
->getKind() == CheckChild2CondCode
;
649 void printImpl(raw_ostream
&OS
, unsigned indent
) const override
;
650 bool isEqualImpl(const Matcher
*M
) const override
{
651 return cast
<CheckChild2CondCodeMatcher
>(M
)->CondCodeName
== CondCodeName
;
655 /// CheckValueTypeMatcher - This checks to see if the current node is a
656 /// VTSDNode with the specified type, if not it fails to match.
657 class CheckValueTypeMatcher
: public Matcher
{
660 CheckValueTypeMatcher(StringRef type_name
)
661 : Matcher(CheckValueType
), TypeName(type_name
) {}
663 StringRef
getTypeName() const { return TypeName
; }
665 static bool classof(const Matcher
*N
) {
666 return N
->getKind() == CheckValueType
;
670 void printImpl(raw_ostream
&OS
, unsigned indent
) const override
;
671 bool isEqualImpl(const Matcher
*M
) const override
{
672 return cast
<CheckValueTypeMatcher
>(M
)->TypeName
== TypeName
;
674 bool isContradictoryImpl(const Matcher
*M
) const override
;
679 /// CheckComplexPatMatcher - This node runs the specified ComplexPattern on
680 /// the current node.
681 class CheckComplexPatMatcher
: public Matcher
{
682 const ComplexPattern
&Pattern
;
684 /// MatchNumber - This is the recorded nodes slot that contains the node we
685 /// want to match against.
686 unsigned MatchNumber
;
688 /// Name - The name of the node we're matching, for comment emission.
691 /// FirstResult - This is the first slot in the RecordedNodes list that the
692 /// result of the match populates.
693 unsigned FirstResult
;
695 CheckComplexPatMatcher(const ComplexPattern
&pattern
, unsigned matchnumber
,
696 const std::string
&name
, unsigned firstresult
)
697 : Matcher(CheckComplexPat
), Pattern(pattern
), MatchNumber(matchnumber
),
698 Name(name
), FirstResult(firstresult
) {}
700 const ComplexPattern
&getPattern() const { return Pattern
; }
701 unsigned getMatchNumber() const { return MatchNumber
; }
703 const std::string
getName() const { return Name
; }
704 unsigned getFirstResult() const { return FirstResult
; }
706 static bool classof(const Matcher
*N
) {
707 return N
->getKind() == CheckComplexPat
;
711 void printImpl(raw_ostream
&OS
, unsigned indent
) const override
;
712 bool isEqualImpl(const Matcher
*M
) const override
{
713 return &cast
<CheckComplexPatMatcher
>(M
)->Pattern
== &Pattern
&&
714 cast
<CheckComplexPatMatcher
>(M
)->MatchNumber
== MatchNumber
;
718 /// CheckAndImmMatcher - This checks to see if the current node is an 'and'
719 /// with something equivalent to the specified immediate.
720 class CheckAndImmMatcher
: public Matcher
{
723 CheckAndImmMatcher(int64_t value
)
724 : Matcher(CheckAndImm
), Value(value
) {}
726 int64_t getValue() const { return Value
; }
728 static bool classof(const Matcher
*N
) {
729 return N
->getKind() == CheckAndImm
;
733 void printImpl(raw_ostream
&OS
, unsigned indent
) const override
;
734 bool isEqualImpl(const Matcher
*M
) const override
{
735 return cast
<CheckAndImmMatcher
>(M
)->Value
== Value
;
739 /// CheckOrImmMatcher - This checks to see if the current node is an 'and'
740 /// with something equivalent to the specified immediate.
741 class CheckOrImmMatcher
: public Matcher
{
744 CheckOrImmMatcher(int64_t value
)
745 : Matcher(CheckOrImm
), Value(value
) {}
747 int64_t getValue() const { return Value
; }
749 static bool classof(const Matcher
*N
) {
750 return N
->getKind() == CheckOrImm
;
754 void printImpl(raw_ostream
&OS
, unsigned indent
) const override
;
755 bool isEqualImpl(const Matcher
*M
) const override
{
756 return cast
<CheckOrImmMatcher
>(M
)->Value
== Value
;
760 /// CheckImmAllOnesVMatcher - This check if the current node is an build vector
762 class CheckImmAllOnesVMatcher
: public Matcher
{
764 CheckImmAllOnesVMatcher() : Matcher(CheckImmAllOnesV
) {}
766 static bool classof(const Matcher
*N
) {
767 return N
->getKind() == CheckImmAllOnesV
;
771 void printImpl(raw_ostream
&OS
, unsigned indent
) const override
;
772 bool isEqualImpl(const Matcher
*M
) const override
{ return true; }
773 bool isContradictoryImpl(const Matcher
*M
) const override
;
776 /// CheckImmAllZerosVMatcher - This check if the current node is an build vector
778 class CheckImmAllZerosVMatcher
: public Matcher
{
780 CheckImmAllZerosVMatcher() : Matcher(CheckImmAllZerosV
) {}
782 static bool classof(const Matcher
*N
) {
783 return N
->getKind() == CheckImmAllZerosV
;
787 void printImpl(raw_ostream
&OS
, unsigned indent
) const override
;
788 bool isEqualImpl(const Matcher
*M
) const override
{ return true; }
789 bool isContradictoryImpl(const Matcher
*M
) const override
;
792 /// CheckFoldableChainNodeMatcher - This checks to see if the current node
793 /// (which defines a chain operand) is safe to fold into a larger pattern.
794 class CheckFoldableChainNodeMatcher
: public Matcher
{
796 CheckFoldableChainNodeMatcher()
797 : Matcher(CheckFoldableChainNode
) {}
799 static bool classof(const Matcher
*N
) {
800 return N
->getKind() == CheckFoldableChainNode
;
804 void printImpl(raw_ostream
&OS
, unsigned indent
) const override
;
805 bool isEqualImpl(const Matcher
*M
) const override
{ return true; }
808 /// EmitIntegerMatcher - This creates a new TargetConstant.
809 class EmitIntegerMatcher
: public Matcher
{
811 MVT::SimpleValueType VT
;
813 EmitIntegerMatcher(int64_t val
, MVT::SimpleValueType vt
)
814 : Matcher(EmitInteger
), Val(val
), VT(vt
) {}
816 int64_t getValue() const { return Val
; }
817 MVT::SimpleValueType
getVT() const { return VT
; }
819 static bool classof(const Matcher
*N
) {
820 return N
->getKind() == EmitInteger
;
824 void printImpl(raw_ostream
&OS
, unsigned indent
) const override
;
825 bool isEqualImpl(const Matcher
*M
) const override
{
826 return cast
<EmitIntegerMatcher
>(M
)->Val
== Val
&&
827 cast
<EmitIntegerMatcher
>(M
)->VT
== VT
;
831 /// EmitStringIntegerMatcher - A target constant whose value is represented
833 class EmitStringIntegerMatcher
: public Matcher
{
835 MVT::SimpleValueType VT
;
837 EmitStringIntegerMatcher(const std::string
&val
, MVT::SimpleValueType vt
)
838 : Matcher(EmitStringInteger
), Val(val
), VT(vt
) {}
840 const std::string
&getValue() const { return Val
; }
841 MVT::SimpleValueType
getVT() const { return VT
; }
843 static bool classof(const Matcher
*N
) {
844 return N
->getKind() == EmitStringInteger
;
848 void printImpl(raw_ostream
&OS
, unsigned indent
) const override
;
849 bool isEqualImpl(const Matcher
*M
) const override
{
850 return cast
<EmitStringIntegerMatcher
>(M
)->Val
== Val
&&
851 cast
<EmitStringIntegerMatcher
>(M
)->VT
== VT
;
855 /// EmitRegisterMatcher - This creates a new TargetConstant.
856 class EmitRegisterMatcher
: public Matcher
{
857 /// Reg - The def for the register that we're emitting. If this is null, then
858 /// this is a reference to zero_reg.
859 const CodeGenRegister
*Reg
;
860 MVT::SimpleValueType VT
;
862 EmitRegisterMatcher(const CodeGenRegister
*reg
, MVT::SimpleValueType vt
)
863 : Matcher(EmitRegister
), Reg(reg
), VT(vt
) {}
865 const CodeGenRegister
*getReg() const { return Reg
; }
866 MVT::SimpleValueType
getVT() const { return VT
; }
868 static bool classof(const Matcher
*N
) {
869 return N
->getKind() == EmitRegister
;
873 void printImpl(raw_ostream
&OS
, unsigned indent
) const override
;
874 bool isEqualImpl(const Matcher
*M
) const override
{
875 return cast
<EmitRegisterMatcher
>(M
)->Reg
== Reg
&&
876 cast
<EmitRegisterMatcher
>(M
)->VT
== VT
;
880 /// EmitConvertToTargetMatcher - Emit an operation that reads a specified
881 /// recorded node and converts it from being a ISD::Constant to
882 /// ISD::TargetConstant, likewise for ConstantFP.
883 class EmitConvertToTargetMatcher
: public Matcher
{
886 EmitConvertToTargetMatcher(unsigned slot
)
887 : Matcher(EmitConvertToTarget
), Slot(slot
) {}
889 unsigned getSlot() const { return Slot
; }
891 static bool classof(const Matcher
*N
) {
892 return N
->getKind() == EmitConvertToTarget
;
896 void printImpl(raw_ostream
&OS
, unsigned indent
) const override
;
897 bool isEqualImpl(const Matcher
*M
) const override
{
898 return cast
<EmitConvertToTargetMatcher
>(M
)->Slot
== Slot
;
902 /// EmitMergeInputChainsMatcher - Emit a node that merges a list of input
903 /// chains together with a token factor. The list of nodes are the nodes in the
904 /// matched pattern that have chain input/outputs. This node adds all input
905 /// chains of these nodes if they are not themselves a node in the pattern.
906 class EmitMergeInputChainsMatcher
: public Matcher
{
907 SmallVector
<unsigned, 3> ChainNodes
;
909 EmitMergeInputChainsMatcher(ArrayRef
<unsigned> nodes
)
910 : Matcher(EmitMergeInputChains
), ChainNodes(nodes
.begin(), nodes
.end()) {}
912 unsigned getNumNodes() const { return ChainNodes
.size(); }
914 unsigned getNode(unsigned i
) const {
915 assert(i
< ChainNodes
.size());
916 return ChainNodes
[i
];
919 static bool classof(const Matcher
*N
) {
920 return N
->getKind() == EmitMergeInputChains
;
924 void printImpl(raw_ostream
&OS
, unsigned indent
) const override
;
925 bool isEqualImpl(const Matcher
*M
) const override
{
926 return cast
<EmitMergeInputChainsMatcher
>(M
)->ChainNodes
== ChainNodes
;
930 /// EmitCopyToRegMatcher - Emit a CopyToReg node from a value to a physreg,
931 /// pushing the chain and glue results.
933 class EmitCopyToRegMatcher
: public Matcher
{
934 unsigned SrcSlot
; // Value to copy into the physreg.
935 const CodeGenRegister
*DestPhysReg
;
938 EmitCopyToRegMatcher(unsigned srcSlot
,
939 const CodeGenRegister
*destPhysReg
)
940 : Matcher(EmitCopyToReg
), SrcSlot(srcSlot
), DestPhysReg(destPhysReg
) {}
942 unsigned getSrcSlot() const { return SrcSlot
; }
943 const CodeGenRegister
*getDestPhysReg() const { return DestPhysReg
; }
945 static bool classof(const Matcher
*N
) {
946 return N
->getKind() == EmitCopyToReg
;
950 void printImpl(raw_ostream
&OS
, unsigned indent
) const override
;
951 bool isEqualImpl(const Matcher
*M
) const override
{
952 return cast
<EmitCopyToRegMatcher
>(M
)->SrcSlot
== SrcSlot
&&
953 cast
<EmitCopyToRegMatcher
>(M
)->DestPhysReg
== DestPhysReg
;
959 /// EmitNodeXFormMatcher - Emit an operation that runs an SDNodeXForm on a
960 /// recorded node and records the result.
961 class EmitNodeXFormMatcher
: public Matcher
{
965 EmitNodeXFormMatcher(unsigned slot
, Record
*nodeXForm
)
966 : Matcher(EmitNodeXForm
), Slot(slot
), NodeXForm(nodeXForm
) {}
968 unsigned getSlot() const { return Slot
; }
969 Record
*getNodeXForm() const { return NodeXForm
; }
971 static bool classof(const Matcher
*N
) {
972 return N
->getKind() == EmitNodeXForm
;
976 void printImpl(raw_ostream
&OS
, unsigned indent
) const override
;
977 bool isEqualImpl(const Matcher
*M
) const override
{
978 return cast
<EmitNodeXFormMatcher
>(M
)->Slot
== Slot
&&
979 cast
<EmitNodeXFormMatcher
>(M
)->NodeXForm
== NodeXForm
;
983 /// EmitNodeMatcherCommon - Common class shared between EmitNode and
985 class EmitNodeMatcherCommon
: public Matcher
{
986 std::string OpcodeName
;
987 const SmallVector
<MVT::SimpleValueType
, 3> VTs
;
988 const SmallVector
<unsigned, 6> Operands
;
989 bool HasChain
, HasInGlue
, HasOutGlue
, HasMemRefs
;
991 /// NumFixedArityOperands - If this is a fixed arity node, this is set to -1.
992 /// If this is a varidic node, this is set to the number of fixed arity
993 /// operands in the root of the pattern. The rest are appended to this node.
994 int NumFixedArityOperands
;
996 EmitNodeMatcherCommon(const std::string
&opcodeName
,
997 ArrayRef
<MVT::SimpleValueType
> vts
,
998 ArrayRef
<unsigned> operands
,
999 bool hasChain
, bool hasInGlue
, bool hasOutGlue
,
1001 int numfixedarityoperands
, bool isMorphNodeTo
)
1002 : Matcher(isMorphNodeTo
? MorphNodeTo
: EmitNode
), OpcodeName(opcodeName
),
1003 VTs(vts
.begin(), vts
.end()), Operands(operands
.begin(), operands
.end()),
1004 HasChain(hasChain
), HasInGlue(hasInGlue
), HasOutGlue(hasOutGlue
),
1005 HasMemRefs(hasmemrefs
), NumFixedArityOperands(numfixedarityoperands
) {}
1007 const std::string
&getOpcodeName() const { return OpcodeName
; }
1009 unsigned getNumVTs() const { return VTs
.size(); }
1010 MVT::SimpleValueType
getVT(unsigned i
) const {
1011 assert(i
< VTs
.size());
1015 unsigned getNumOperands() const { return Operands
.size(); }
1016 unsigned getOperand(unsigned i
) const {
1017 assert(i
< Operands
.size());
1021 const SmallVectorImpl
<MVT::SimpleValueType
> &getVTList() const { return VTs
; }
1022 const SmallVectorImpl
<unsigned> &getOperandList() const { return Operands
; }
1025 bool hasChain() const { return HasChain
; }
1026 bool hasInFlag() const { return HasInGlue
; }
1027 bool hasOutFlag() const { return HasOutGlue
; }
1028 bool hasMemRefs() const { return HasMemRefs
; }
1029 int getNumFixedArityOperands() const { return NumFixedArityOperands
; }
1031 static bool classof(const Matcher
*N
) {
1032 return N
->getKind() == EmitNode
|| N
->getKind() == MorphNodeTo
;
1036 void printImpl(raw_ostream
&OS
, unsigned indent
) const override
;
1037 bool isEqualImpl(const Matcher
*M
) const override
;
1040 /// EmitNodeMatcher - This signals a successful match and generates a node.
1041 class EmitNodeMatcher
: public EmitNodeMatcherCommon
{
1042 void anchor() override
;
1043 unsigned FirstResultSlot
;
1045 EmitNodeMatcher(const std::string
&opcodeName
,
1046 ArrayRef
<MVT::SimpleValueType
> vts
,
1047 ArrayRef
<unsigned> operands
,
1048 bool hasChain
, bool hasInFlag
, bool hasOutFlag
,
1050 int numfixedarityoperands
, unsigned firstresultslot
)
1051 : EmitNodeMatcherCommon(opcodeName
, vts
, operands
, hasChain
,
1052 hasInFlag
, hasOutFlag
, hasmemrefs
,
1053 numfixedarityoperands
, false),
1054 FirstResultSlot(firstresultslot
) {}
1056 unsigned getFirstResultSlot() const { return FirstResultSlot
; }
1058 static bool classof(const Matcher
*N
) {
1059 return N
->getKind() == EmitNode
;
1064 class MorphNodeToMatcher
: public EmitNodeMatcherCommon
{
1065 void anchor() override
;
1066 const PatternToMatch
&Pattern
;
1068 MorphNodeToMatcher(const std::string
&opcodeName
,
1069 ArrayRef
<MVT::SimpleValueType
> vts
,
1070 ArrayRef
<unsigned> operands
,
1071 bool hasChain
, bool hasInFlag
, bool hasOutFlag
,
1073 int numfixedarityoperands
, const PatternToMatch
&pattern
)
1074 : EmitNodeMatcherCommon(opcodeName
, vts
, operands
, hasChain
,
1075 hasInFlag
, hasOutFlag
, hasmemrefs
,
1076 numfixedarityoperands
, true),
1080 const PatternToMatch
&getPattern() const { return Pattern
; }
1082 static bool classof(const Matcher
*N
) {
1083 return N
->getKind() == MorphNodeTo
;
1087 /// CompleteMatchMatcher - Complete a match by replacing the results of the
1088 /// pattern with the newly generated nodes. This also prints a comment
1089 /// indicating the source and dest patterns.
1090 class CompleteMatchMatcher
: public Matcher
{
1091 SmallVector
<unsigned, 2> Results
;
1092 const PatternToMatch
&Pattern
;
1094 CompleteMatchMatcher(ArrayRef
<unsigned> results
,
1095 const PatternToMatch
&pattern
)
1096 : Matcher(CompleteMatch
), Results(results
.begin(), results
.end()),
1099 unsigned getNumResults() const { return Results
.size(); }
1100 unsigned getResult(unsigned R
) const { return Results
[R
]; }
1101 const PatternToMatch
&getPattern() const { return Pattern
; }
1103 static bool classof(const Matcher
*N
) {
1104 return N
->getKind() == CompleteMatch
;
1108 void printImpl(raw_ostream
&OS
, unsigned indent
) const override
;
1109 bool isEqualImpl(const Matcher
*M
) const override
{
1110 return cast
<CompleteMatchMatcher
>(M
)->Results
== Results
&&
1111 &cast
<CompleteMatchMatcher
>(M
)->Pattern
== &Pattern
;
1115 } // end namespace llvm