1 //===- DAGISelMatcher.h - Representation of DAG pattern matcher -----------===//
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 TBLGEN_DAGISELMATCHER_H
11 #define TBLGEN_DAGISELMATCHER_H
13 #include "llvm/CodeGen/ValueTypes.h"
14 #include "llvm/ADT/OwningPtr.h"
15 #include "llvm/ADT/StringRef.h"
16 #include "llvm/ADT/SmallVector.h"
17 #include "llvm/Support/Casting.h"
20 struct CodeGenRegister
;
21 class CodeGenDAGPatterns
;
28 class TreePredicateFn
;
31 Matcher
*ConvertPatternToMatcher(const PatternToMatch
&Pattern
,unsigned Variant
,
32 const CodeGenDAGPatterns
&CGP
);
33 Matcher
*OptimizeMatcher(Matcher
*Matcher
, const CodeGenDAGPatterns
&CGP
);
34 void EmitMatcherTable(const Matcher
*Matcher
, const CodeGenDAGPatterns
&CGP
,
38 /// Matcher - Base class for all the 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 OwningPtr
<Matcher
> Next
;
46 // Matcher state manipulation.
47 Scope
, // Push a checking scope.
48 RecordNode
, // Record the current node.
49 RecordChild
, // Record a child of the current node.
50 RecordMemRef
, // Record the memref in the current node.
51 CaptureGlueInput
, // If the current node has an input glue, save it.
52 MoveChild
, // Move current node to specified child.
53 MoveParent
, // Move current node to parent.
55 // Predicate checking.
56 CheckSame
, // Fail if not same as prev match.
57 CheckPatternPredicate
,
58 CheckPredicate
, // Fail if node predicate fails.
59 CheckOpcode
, // Fail if not opcode.
60 SwitchOpcode
, // Dispatch based on opcode.
61 CheckType
, // Fail if not correct type.
62 SwitchType
, // Dispatch based on type.
63 CheckChildType
, // Fail if child has wrong type.
64 CheckInteger
, // Fail if wrong val.
65 CheckCondCode
, // Fail if not condcode.
70 CheckFoldableChainNode
,
72 // Node creation/emisssion.
73 EmitInteger
, // Create a TargetConstant
74 EmitStringInteger
, // Create a TargetConstant from a string.
75 EmitRegister
, // Create a register.
76 EmitConvertToTarget
, // Convert a imm/fpimm to target imm/fpimm
77 EmitMergeInputChains
, // Merge together a chains for an input.
78 EmitCopyToReg
, // Emit a copytoreg into a physreg.
79 EmitNode
, // Create a DAG node
80 EmitNodeXForm
, // Run a SDNodeXForm
81 MarkGlueResults
, // Indicate which interior nodes have glue results.
82 CompleteMatch
, // Finish a match and update the results.
83 MorphNodeTo
// Build a node, finish a match and update results.
88 Matcher(KindTy K
) : Kind(K
) {}
92 KindTy
getKind() const { return Kind
; }
94 Matcher
*getNext() { return Next
.get(); }
95 const Matcher
*getNext() const { return Next
.get(); }
96 void setNext(Matcher
*C
) { Next
.reset(C
); }
97 Matcher
*takeNext() { return Next
.take(); }
99 OwningPtr
<Matcher
> &getNextPtr() { return Next
; }
101 static inline bool classof(const Matcher
*) { return true; }
103 bool isEqual(const Matcher
*M
) const {
104 if (getKind() != M
->getKind()) return false;
105 return isEqualImpl(M
);
108 unsigned getHash() const {
109 // Clear the high bit so we don't conflict with tombstones etc.
110 return ((getHashImpl() << 4) ^ getKind()) & (~0U>>1);
113 /// isSafeToReorderWithPatternPredicate - Return true if it is safe to sink a
114 /// PatternPredicate node past this one.
115 virtual bool isSafeToReorderWithPatternPredicate() const {
119 /// isSimplePredicateNode - Return true if this is a simple predicate that
120 /// operates on the node or its children without potential side effects or a
121 /// change of the current node.
122 bool isSimplePredicateNode() const {
124 default: return false;
126 case CheckPatternPredicate
:
136 case CheckFoldableChainNode
:
141 /// isSimplePredicateOrRecordNode - Return true if this is a record node or
142 /// a simple predicate.
143 bool isSimplePredicateOrRecordNode() const {
144 return isSimplePredicateNode() ||
145 getKind() == RecordNode
|| getKind() == RecordChild
;
148 /// unlinkNode - Unlink the specified node from this chain. If Other == this,
149 /// we unlink the next pointer and return it. Otherwise we unlink Other from
150 /// the list and return this.
151 Matcher
*unlinkNode(Matcher
*Other
);
153 /// canMoveBefore - Return true if this matcher is the same as Other, or if
154 /// we can move this matcher past all of the nodes in-between Other and this
155 /// node. Other must be equal to or before this.
156 bool canMoveBefore(const Matcher
*Other
) const;
158 /// canMoveBefore - Return true if it is safe to move the current matcher
159 /// across the specified one.
160 bool canMoveBeforeNode(const Matcher
*Other
) const;
162 /// isContradictory - Return true of these two matchers could never match on
164 bool isContradictory(const Matcher
*Other
) const {
165 // Since this predicate is reflexive, we canonicalize the ordering so that
166 // we always match a node against nodes with kinds that are greater or equal
167 // to them. For example, we'll pass in a CheckType node as an argument to
168 // the CheckOpcode method, not the other way around.
169 if (getKind() < Other
->getKind())
170 return isContradictoryImpl(Other
);
171 return Other
->isContradictoryImpl(this);
174 void print(raw_ostream
&OS
, unsigned indent
= 0) const;
175 void printOne(raw_ostream
&OS
) const;
178 virtual void printImpl(raw_ostream
&OS
, unsigned indent
) const = 0;
179 virtual bool isEqualImpl(const Matcher
*M
) const = 0;
180 virtual unsigned getHashImpl() const = 0;
181 virtual bool isContradictoryImpl(const Matcher
*M
) const { return false; }
184 /// ScopeMatcher - This attempts to match each of its children to find the first
185 /// one that successfully matches. If one child fails, it tries the next child.
186 /// If none of the children match then this check fails. It never has a 'next'.
187 class ScopeMatcher
: public Matcher
{
188 SmallVector
<Matcher
*, 4> Children
;
190 ScopeMatcher(Matcher
*const *children
, unsigned numchildren
)
191 : Matcher(Scope
), Children(children
, children
+numchildren
) {
193 virtual ~ScopeMatcher();
195 unsigned getNumChildren() const { return Children
.size(); }
197 Matcher
*getChild(unsigned i
) { return Children
[i
]; }
198 const Matcher
*getChild(unsigned i
) const { return Children
[i
]; }
200 void resetChild(unsigned i
, Matcher
*N
) {
205 Matcher
*takeChild(unsigned i
) {
206 Matcher
*Res
= Children
[i
];
211 void setNumChildren(unsigned NC
) {
212 if (NC
< Children
.size()) {
213 // delete any children we're about to lose pointers to.
214 for (unsigned i
= NC
, e
= Children
.size(); i
!= e
; ++i
)
220 static inline bool classof(const Matcher
*N
) {
221 return N
->getKind() == Scope
;
225 virtual void printImpl(raw_ostream
&OS
, unsigned indent
) const;
226 virtual bool isEqualImpl(const Matcher
*M
) const { return false; }
227 virtual unsigned getHashImpl() const { return 12312; }
230 /// RecordMatcher - Save the current node in the operand list.
231 class RecordMatcher
: public Matcher
{
232 /// WhatFor - This is a string indicating why we're recording this. This
233 /// should only be used for comment generation not anything semantic.
236 /// ResultNo - The slot number in the RecordedNodes vector that this will be,
237 /// just printed as a comment.
240 RecordMatcher(const std::string
&whatfor
, unsigned resultNo
)
241 : Matcher(RecordNode
), WhatFor(whatfor
), ResultNo(resultNo
) {}
243 const std::string
&getWhatFor() const { return WhatFor
; }
244 unsigned getResultNo() const { return ResultNo
; }
246 static inline bool classof(const Matcher
*N
) {
247 return N
->getKind() == RecordNode
;
250 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
252 virtual void printImpl(raw_ostream
&OS
, unsigned indent
) const;
253 virtual bool isEqualImpl(const Matcher
*M
) const { return true; }
254 virtual unsigned getHashImpl() const { return 0; }
257 /// RecordChildMatcher - Save a numbered child of the current node, or fail
258 /// the match if it doesn't exist. This is logically equivalent to:
259 /// MoveChild N + RecordNode + MoveParent.
260 class RecordChildMatcher
: public Matcher
{
263 /// WhatFor - This is a string indicating why we're recording this. This
264 /// should only be used for comment generation not anything semantic.
267 /// ResultNo - The slot number in the RecordedNodes vector that this will be,
268 /// just printed as a comment.
271 RecordChildMatcher(unsigned childno
, const std::string
&whatfor
,
273 : Matcher(RecordChild
), ChildNo(childno
), WhatFor(whatfor
),
274 ResultNo(resultNo
) {}
276 unsigned getChildNo() const { return ChildNo
; }
277 const std::string
&getWhatFor() const { return WhatFor
; }
278 unsigned getResultNo() const { return ResultNo
; }
280 static inline bool classof(const Matcher
*N
) {
281 return N
->getKind() == RecordChild
;
284 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
287 virtual void printImpl(raw_ostream
&OS
, unsigned indent
) const;
288 virtual bool isEqualImpl(const Matcher
*M
) const {
289 return cast
<RecordChildMatcher
>(M
)->getChildNo() == getChildNo();
291 virtual unsigned getHashImpl() const { return getChildNo(); }
294 /// RecordMemRefMatcher - Save the current node's memref.
295 class RecordMemRefMatcher
: public Matcher
{
297 RecordMemRefMatcher() : Matcher(RecordMemRef
) {}
299 static inline bool classof(const Matcher
*N
) {
300 return N
->getKind() == RecordMemRef
;
303 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
306 virtual void printImpl(raw_ostream
&OS
, unsigned indent
) const;
307 virtual bool isEqualImpl(const Matcher
*M
) const { return true; }
308 virtual unsigned getHashImpl() const { return 0; }
312 /// CaptureGlueInputMatcher - If the current record has a glue input, record
313 /// it so that it is used as an input to the generated code.
314 class CaptureGlueInputMatcher
: public Matcher
{
316 CaptureGlueInputMatcher() : Matcher(CaptureGlueInput
) {}
318 static inline bool classof(const Matcher
*N
) {
319 return N
->getKind() == CaptureGlueInput
;
322 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
325 virtual void printImpl(raw_ostream
&OS
, unsigned indent
) const;
326 virtual bool isEqualImpl(const Matcher
*M
) const { return true; }
327 virtual unsigned getHashImpl() const { return 0; }
330 /// MoveChildMatcher - This tells the interpreter to move into the
331 /// specified child node.
332 class MoveChildMatcher
: public Matcher
{
335 MoveChildMatcher(unsigned childNo
) : Matcher(MoveChild
), ChildNo(childNo
) {}
337 unsigned getChildNo() const { return ChildNo
; }
339 static inline bool classof(const Matcher
*N
) {
340 return N
->getKind() == MoveChild
;
343 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
346 virtual void printImpl(raw_ostream
&OS
, unsigned indent
) const;
347 virtual bool isEqualImpl(const Matcher
*M
) const {
348 return cast
<MoveChildMatcher
>(M
)->getChildNo() == getChildNo();
350 virtual unsigned getHashImpl() const { return getChildNo(); }
353 /// MoveParentMatcher - This tells the interpreter to move to the parent
354 /// of the current node.
355 class MoveParentMatcher
: public Matcher
{
357 MoveParentMatcher() : Matcher(MoveParent
) {}
359 static inline bool classof(const Matcher
*N
) {
360 return N
->getKind() == MoveParent
;
363 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
366 virtual void printImpl(raw_ostream
&OS
, unsigned indent
) const;
367 virtual bool isEqualImpl(const Matcher
*M
) const { return true; }
368 virtual unsigned getHashImpl() const { return 0; }
371 /// CheckSameMatcher - This checks to see if this 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 CheckSameMatcher
: public Matcher
{
375 unsigned MatchNumber
;
377 CheckSameMatcher(unsigned matchnumber
)
378 : Matcher(CheckSame
), MatchNumber(matchnumber
) {}
380 unsigned getMatchNumber() const { return MatchNumber
; }
382 static inline bool classof(const Matcher
*N
) {
383 return N
->getKind() == CheckSame
;
386 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
389 virtual void printImpl(raw_ostream
&OS
, unsigned indent
) const;
390 virtual bool isEqualImpl(const Matcher
*M
) const {
391 return cast
<CheckSameMatcher
>(M
)->getMatchNumber() == getMatchNumber();
393 virtual unsigned getHashImpl() const { return getMatchNumber(); }
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 inline bool classof(const Matcher
*N
) {
408 return N
->getKind() == CheckPatternPredicate
;
411 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
414 virtual void printImpl(raw_ostream
&OS
, unsigned indent
) const;
415 virtual bool isEqualImpl(const Matcher
*M
) const {
416 return cast
<CheckPatternPredicateMatcher
>(M
)->getPredicate() == Predicate
;
418 virtual unsigned getHashImpl() const;
421 /// CheckPredicateMatcher - This checks the target-specific predicate to
422 /// see if the node is acceptable.
423 class CheckPredicateMatcher
: public Matcher
{
426 CheckPredicateMatcher(const TreePredicateFn
&pred
);
428 TreePredicateFn
getPredicate() const;
430 static inline bool classof(const Matcher
*N
) {
431 return N
->getKind() == CheckPredicate
;
435 //virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
438 virtual void printImpl(raw_ostream
&OS
, unsigned indent
) const;
439 virtual bool isEqualImpl(const Matcher
*M
) const {
440 return cast
<CheckPredicateMatcher
>(M
)->Pred
== Pred
;
442 virtual unsigned getHashImpl() const;
446 /// CheckOpcodeMatcher - This checks to see if the current node has the
447 /// specified opcode, if not it fails to match.
448 class CheckOpcodeMatcher
: public Matcher
{
449 const SDNodeInfo
&Opcode
;
451 CheckOpcodeMatcher(const SDNodeInfo
&opcode
)
452 : Matcher(CheckOpcode
), Opcode(opcode
) {}
454 const SDNodeInfo
&getOpcode() const { return Opcode
; }
456 static inline bool classof(const Matcher
*N
) {
457 return N
->getKind() == CheckOpcode
;
460 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
463 virtual void printImpl(raw_ostream
&OS
, unsigned indent
) const;
464 virtual bool isEqualImpl(const Matcher
*M
) const;
465 virtual unsigned getHashImpl() const;
466 virtual bool isContradictoryImpl(const Matcher
*M
) const;
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(const std::pair
<const SDNodeInfo
*, Matcher
*> *cases
,
478 : Matcher(SwitchOpcode
), Cases(cases
, cases
+numcases
) {}
480 static inline 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 virtual void printImpl(raw_ostream
&OS
, unsigned indent
) const;
492 virtual bool isEqualImpl(const Matcher
*M
) const { return false; }
493 virtual unsigned getHashImpl() const { return 4123; }
496 /// CheckTypeMatcher - This checks to see if the current node has the
497 /// specified type at the specified result, if not it fails to match.
498 class CheckTypeMatcher
: public Matcher
{
499 MVT::SimpleValueType Type
;
502 CheckTypeMatcher(MVT::SimpleValueType type
, unsigned resno
)
503 : Matcher(CheckType
), Type(type
), ResNo(resno
) {}
505 MVT::SimpleValueType
getType() const { return Type
; }
506 unsigned getResNo() const { return ResNo
; }
508 static inline bool classof(const Matcher
*N
) {
509 return N
->getKind() == CheckType
;
512 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
515 virtual void printImpl(raw_ostream
&OS
, unsigned indent
) const;
516 virtual bool isEqualImpl(const Matcher
*M
) const {
517 return cast
<CheckTypeMatcher
>(M
)->Type
== Type
;
519 virtual unsigned getHashImpl() const { return Type
; }
520 virtual bool isContradictoryImpl(const Matcher
*M
) const;
523 /// SwitchTypeMatcher - Switch based on the current node's type, dispatching
524 /// to one matcher per case. If the type doesn't match any of the cases,
525 /// then the match fails. This is semantically equivalent to a Scope node where
526 /// every child does a CheckType, but is much faster.
527 class SwitchTypeMatcher
: public Matcher
{
528 SmallVector
<std::pair
<MVT::SimpleValueType
, Matcher
*>, 8> Cases
;
530 SwitchTypeMatcher(const std::pair
<MVT::SimpleValueType
, Matcher
*> *cases
,
532 : Matcher(SwitchType
), Cases(cases
, cases
+numcases
) {}
534 static inline bool classof(const Matcher
*N
) {
535 return N
->getKind() == SwitchType
;
538 unsigned getNumCases() const { return Cases
.size(); }
540 MVT::SimpleValueType
getCaseType(unsigned i
) const { return Cases
[i
].first
; }
541 Matcher
*getCaseMatcher(unsigned i
) { return Cases
[i
].second
; }
542 const Matcher
*getCaseMatcher(unsigned i
) const { return Cases
[i
].second
; }
545 virtual void printImpl(raw_ostream
&OS
, unsigned indent
) const;
546 virtual bool isEqualImpl(const Matcher
*M
) const { return false; }
547 virtual unsigned getHashImpl() const { return 4123; }
551 /// CheckChildTypeMatcher - This checks to see if a child node has the
552 /// specified type, if not it fails to match.
553 class CheckChildTypeMatcher
: public Matcher
{
555 MVT::SimpleValueType Type
;
557 CheckChildTypeMatcher(unsigned childno
, MVT::SimpleValueType type
)
558 : Matcher(CheckChildType
), ChildNo(childno
), Type(type
) {}
560 unsigned getChildNo() const { return ChildNo
; }
561 MVT::SimpleValueType
getType() const { return Type
; }
563 static inline bool classof(const Matcher
*N
) {
564 return N
->getKind() == CheckChildType
;
567 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
570 virtual void printImpl(raw_ostream
&OS
, unsigned indent
) const;
571 virtual bool isEqualImpl(const Matcher
*M
) const {
572 return cast
<CheckChildTypeMatcher
>(M
)->ChildNo
== ChildNo
&&
573 cast
<CheckChildTypeMatcher
>(M
)->Type
== Type
;
575 virtual unsigned getHashImpl() const { return (Type
<< 3) | ChildNo
; }
576 virtual bool isContradictoryImpl(const Matcher
*M
) const;
580 /// CheckIntegerMatcher - This checks to see if the current node is a
581 /// ConstantSDNode with the specified integer value, if not it fails to match.
582 class CheckIntegerMatcher
: public Matcher
{
585 CheckIntegerMatcher(int64_t value
)
586 : Matcher(CheckInteger
), Value(value
) {}
588 int64_t getValue() const { return Value
; }
590 static inline bool classof(const Matcher
*N
) {
591 return N
->getKind() == CheckInteger
;
594 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
597 virtual void printImpl(raw_ostream
&OS
, unsigned indent
) const;
598 virtual bool isEqualImpl(const Matcher
*M
) const {
599 return cast
<CheckIntegerMatcher
>(M
)->Value
== Value
;
601 virtual unsigned getHashImpl() const { return Value
; }
602 virtual bool isContradictoryImpl(const Matcher
*M
) const;
605 /// CheckCondCodeMatcher - This checks to see if the current node is a
606 /// CondCodeSDNode with the specified condition, if not it fails to match.
607 class CheckCondCodeMatcher
: public Matcher
{
608 StringRef CondCodeName
;
610 CheckCondCodeMatcher(StringRef condcodename
)
611 : Matcher(CheckCondCode
), CondCodeName(condcodename
) {}
613 StringRef
getCondCodeName() const { return CondCodeName
; }
615 static inline bool classof(const Matcher
*N
) {
616 return N
->getKind() == CheckCondCode
;
619 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
622 virtual void printImpl(raw_ostream
&OS
, unsigned indent
) const;
623 virtual bool isEqualImpl(const Matcher
*M
) const {
624 return cast
<CheckCondCodeMatcher
>(M
)->CondCodeName
== CondCodeName
;
626 virtual unsigned getHashImpl() const;
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 inline bool classof(const Matcher
*N
) {
640 return N
->getKind() == CheckValueType
;
643 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
646 virtual void printImpl(raw_ostream
&OS
, unsigned indent
) const;
647 virtual bool isEqualImpl(const Matcher
*M
) const {
648 return cast
<CheckValueTypeMatcher
>(M
)->TypeName
== TypeName
;
650 virtual unsigned getHashImpl() const;
651 bool isContradictoryImpl(const Matcher
*M
) const;
656 /// CheckComplexPatMatcher - This node runs the specified ComplexPattern on
657 /// the current node.
658 class CheckComplexPatMatcher
: public Matcher
{
659 const ComplexPattern
&Pattern
;
661 /// MatchNumber - This is the recorded nodes slot that contains the node we
662 /// want to match against.
663 unsigned MatchNumber
;
665 /// Name - The name of the node we're matching, for comment emission.
668 /// FirstResult - This is the first slot in the RecordedNodes list that the
669 /// result of the match populates.
670 unsigned FirstResult
;
672 CheckComplexPatMatcher(const ComplexPattern
&pattern
, unsigned matchnumber
,
673 const std::string
&name
, unsigned firstresult
)
674 : Matcher(CheckComplexPat
), Pattern(pattern
), MatchNumber(matchnumber
),
675 Name(name
), FirstResult(firstresult
) {}
677 const ComplexPattern
&getPattern() const { return Pattern
; }
678 unsigned getMatchNumber() const { return MatchNumber
; }
680 const std::string
getName() const { return Name
; }
681 unsigned getFirstResult() const { return FirstResult
; }
683 static inline bool classof(const Matcher
*N
) {
684 return N
->getKind() == CheckComplexPat
;
687 // Not safe to move a pattern predicate past a complex pattern.
688 virtual bool isSafeToReorderWithPatternPredicate() const { return false; }
691 virtual void printImpl(raw_ostream
&OS
, unsigned indent
) const;
692 virtual bool isEqualImpl(const Matcher
*M
) const {
693 return &cast
<CheckComplexPatMatcher
>(M
)->Pattern
== &Pattern
&&
694 cast
<CheckComplexPatMatcher
>(M
)->MatchNumber
== MatchNumber
;
696 virtual unsigned getHashImpl() const {
697 return (unsigned)(intptr_t)&Pattern
^ MatchNumber
;
701 /// CheckAndImmMatcher - This checks to see if the current node is an 'and'
702 /// with something equivalent to the specified immediate.
703 class CheckAndImmMatcher
: public Matcher
{
706 CheckAndImmMatcher(int64_t value
)
707 : Matcher(CheckAndImm
), Value(value
) {}
709 int64_t getValue() const { return Value
; }
711 static inline bool classof(const Matcher
*N
) {
712 return N
->getKind() == CheckAndImm
;
715 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
718 virtual void printImpl(raw_ostream
&OS
, unsigned indent
) const;
719 virtual bool isEqualImpl(const Matcher
*M
) const {
720 return cast
<CheckAndImmMatcher
>(M
)->Value
== Value
;
722 virtual unsigned getHashImpl() const { return Value
; }
725 /// CheckOrImmMatcher - This checks to see if the current node is an 'and'
726 /// with something equivalent to the specified immediate.
727 class CheckOrImmMatcher
: public Matcher
{
730 CheckOrImmMatcher(int64_t value
)
731 : Matcher(CheckOrImm
), Value(value
) {}
733 int64_t getValue() const { return Value
; }
735 static inline bool classof(const Matcher
*N
) {
736 return N
->getKind() == CheckOrImm
;
739 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
742 virtual void printImpl(raw_ostream
&OS
, unsigned indent
) const;
743 virtual bool isEqualImpl(const Matcher
*M
) const {
744 return cast
<CheckOrImmMatcher
>(M
)->Value
== Value
;
746 virtual unsigned getHashImpl() const { return Value
; }
749 /// CheckFoldableChainNodeMatcher - This checks to see if the current node
750 /// (which defines a chain operand) is safe to fold into a larger pattern.
751 class CheckFoldableChainNodeMatcher
: public Matcher
{
753 CheckFoldableChainNodeMatcher()
754 : Matcher(CheckFoldableChainNode
) {}
756 static inline bool classof(const Matcher
*N
) {
757 return N
->getKind() == CheckFoldableChainNode
;
760 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
763 virtual void printImpl(raw_ostream
&OS
, unsigned indent
) const;
764 virtual bool isEqualImpl(const Matcher
*M
) const { return true; }
765 virtual unsigned getHashImpl() const { return 0; }
768 /// EmitIntegerMatcher - This creates a new TargetConstant.
769 class EmitIntegerMatcher
: public Matcher
{
771 MVT::SimpleValueType VT
;
773 EmitIntegerMatcher(int64_t val
, MVT::SimpleValueType vt
)
774 : Matcher(EmitInteger
), Val(val
), VT(vt
) {}
776 int64_t getValue() const { return Val
; }
777 MVT::SimpleValueType
getVT() const { return VT
; }
779 static inline bool classof(const Matcher
*N
) {
780 return N
->getKind() == EmitInteger
;
784 virtual void printImpl(raw_ostream
&OS
, unsigned indent
) const;
785 virtual bool isEqualImpl(const Matcher
*M
) const {
786 return cast
<EmitIntegerMatcher
>(M
)->Val
== Val
&&
787 cast
<EmitIntegerMatcher
>(M
)->VT
== VT
;
789 virtual unsigned getHashImpl() const { return (Val
<< 4) | VT
; }
792 /// EmitStringIntegerMatcher - A target constant whose value is represented
794 class EmitStringIntegerMatcher
: public Matcher
{
796 MVT::SimpleValueType VT
;
798 EmitStringIntegerMatcher(const std::string
&val
, MVT::SimpleValueType vt
)
799 : Matcher(EmitStringInteger
), Val(val
), VT(vt
) {}
801 const std::string
&getValue() const { return Val
; }
802 MVT::SimpleValueType
getVT() const { return VT
; }
804 static inline bool classof(const Matcher
*N
) {
805 return N
->getKind() == EmitStringInteger
;
809 virtual void printImpl(raw_ostream
&OS
, unsigned indent
) const;
810 virtual bool isEqualImpl(const Matcher
*M
) const {
811 return cast
<EmitStringIntegerMatcher
>(M
)->Val
== Val
&&
812 cast
<EmitStringIntegerMatcher
>(M
)->VT
== VT
;
814 virtual unsigned getHashImpl() const;
817 /// EmitRegisterMatcher - This creates a new TargetConstant.
818 class EmitRegisterMatcher
: public Matcher
{
819 /// Reg - The def for the register that we're emitting. If this is null, then
820 /// this is a reference to zero_reg.
821 const CodeGenRegister
*Reg
;
822 MVT::SimpleValueType VT
;
824 EmitRegisterMatcher(const CodeGenRegister
*reg
, MVT::SimpleValueType vt
)
825 : Matcher(EmitRegister
), Reg(reg
), VT(vt
) {}
827 const CodeGenRegister
*getReg() const { return Reg
; }
828 MVT::SimpleValueType
getVT() const { return VT
; }
830 static inline bool classof(const Matcher
*N
) {
831 return N
->getKind() == EmitRegister
;
835 virtual void printImpl(raw_ostream
&OS
, unsigned indent
) const;
836 virtual bool isEqualImpl(const Matcher
*M
) const {
837 return cast
<EmitRegisterMatcher
>(M
)->Reg
== Reg
&&
838 cast
<EmitRegisterMatcher
>(M
)->VT
== VT
;
840 virtual unsigned getHashImpl() const {
841 return ((unsigned)(intptr_t)Reg
) << 4 | VT
;
845 /// EmitConvertToTargetMatcher - Emit an operation that reads a specified
846 /// recorded node and converts it from being a ISD::Constant to
847 /// ISD::TargetConstant, likewise for ConstantFP.
848 class EmitConvertToTargetMatcher
: public Matcher
{
851 EmitConvertToTargetMatcher(unsigned slot
)
852 : Matcher(EmitConvertToTarget
), Slot(slot
) {}
854 unsigned getSlot() const { return Slot
; }
856 static inline bool classof(const Matcher
*N
) {
857 return N
->getKind() == EmitConvertToTarget
;
861 virtual void printImpl(raw_ostream
&OS
, unsigned indent
) const;
862 virtual bool isEqualImpl(const Matcher
*M
) const {
863 return cast
<EmitConvertToTargetMatcher
>(M
)->Slot
== Slot
;
865 virtual unsigned getHashImpl() const { return Slot
; }
868 /// EmitMergeInputChainsMatcher - Emit a node that merges a list of input
869 /// chains together with a token factor. The list of nodes are the nodes in the
870 /// matched pattern that have chain input/outputs. This node adds all input
871 /// chains of these nodes if they are not themselves a node in the pattern.
872 class EmitMergeInputChainsMatcher
: public Matcher
{
873 SmallVector
<unsigned, 3> ChainNodes
;
875 EmitMergeInputChainsMatcher(const unsigned *nodes
, unsigned NumNodes
)
876 : Matcher(EmitMergeInputChains
), ChainNodes(nodes
, nodes
+NumNodes
) {}
878 unsigned getNumNodes() const { return ChainNodes
.size(); }
880 unsigned getNode(unsigned i
) const {
881 assert(i
< ChainNodes
.size());
882 return ChainNodes
[i
];
885 static inline bool classof(const Matcher
*N
) {
886 return N
->getKind() == EmitMergeInputChains
;
890 virtual void printImpl(raw_ostream
&OS
, unsigned indent
) const;
891 virtual bool isEqualImpl(const Matcher
*M
) const {
892 return cast
<EmitMergeInputChainsMatcher
>(M
)->ChainNodes
== ChainNodes
;
894 virtual unsigned getHashImpl() const;
897 /// EmitCopyToRegMatcher - Emit a CopyToReg node from a value to a physreg,
898 /// pushing the chain and glue results.
900 class EmitCopyToRegMatcher
: public Matcher
{
901 unsigned SrcSlot
; // Value to copy into the physreg.
904 EmitCopyToRegMatcher(unsigned srcSlot
, Record
*destPhysReg
)
905 : Matcher(EmitCopyToReg
), SrcSlot(srcSlot
), DestPhysReg(destPhysReg
) {}
907 unsigned getSrcSlot() const { return SrcSlot
; }
908 Record
*getDestPhysReg() const { return DestPhysReg
; }
910 static inline bool classof(const Matcher
*N
) {
911 return N
->getKind() == EmitCopyToReg
;
915 virtual void printImpl(raw_ostream
&OS
, unsigned indent
) const;
916 virtual bool isEqualImpl(const Matcher
*M
) const {
917 return cast
<EmitCopyToRegMatcher
>(M
)->SrcSlot
== SrcSlot
&&
918 cast
<EmitCopyToRegMatcher
>(M
)->DestPhysReg
== DestPhysReg
;
920 virtual unsigned getHashImpl() const {
921 return SrcSlot
^ ((unsigned)(intptr_t)DestPhysReg
<< 4);
927 /// EmitNodeXFormMatcher - Emit an operation that runs an SDNodeXForm on a
928 /// recorded node and records the result.
929 class EmitNodeXFormMatcher
: public Matcher
{
933 EmitNodeXFormMatcher(unsigned slot
, Record
*nodeXForm
)
934 : Matcher(EmitNodeXForm
), Slot(slot
), NodeXForm(nodeXForm
) {}
936 unsigned getSlot() const { return Slot
; }
937 Record
*getNodeXForm() const { return NodeXForm
; }
939 static inline bool classof(const Matcher
*N
) {
940 return N
->getKind() == EmitNodeXForm
;
944 virtual void printImpl(raw_ostream
&OS
, unsigned indent
) const;
945 virtual bool isEqualImpl(const Matcher
*M
) const {
946 return cast
<EmitNodeXFormMatcher
>(M
)->Slot
== Slot
&&
947 cast
<EmitNodeXFormMatcher
>(M
)->NodeXForm
== NodeXForm
;
949 virtual unsigned getHashImpl() const {
950 return Slot
^ ((unsigned)(intptr_t)NodeXForm
<< 4);
954 /// EmitNodeMatcherCommon - Common class shared between EmitNode and
956 class EmitNodeMatcherCommon
: public Matcher
{
957 std::string OpcodeName
;
958 const SmallVector
<MVT::SimpleValueType
, 3> VTs
;
959 const SmallVector
<unsigned, 6> Operands
;
960 bool HasChain
, HasInGlue
, HasOutGlue
, HasMemRefs
;
962 /// NumFixedArityOperands - If this is a fixed arity node, this is set to -1.
963 /// If this is a varidic node, this is set to the number of fixed arity
964 /// operands in the root of the pattern. The rest are appended to this node.
965 int NumFixedArityOperands
;
967 EmitNodeMatcherCommon(const std::string
&opcodeName
,
968 const MVT::SimpleValueType
*vts
, unsigned numvts
,
969 const unsigned *operands
, unsigned numops
,
970 bool hasChain
, bool hasInGlue
, bool hasOutGlue
,
972 int numfixedarityoperands
, bool isMorphNodeTo
)
973 : Matcher(isMorphNodeTo
? MorphNodeTo
: EmitNode
), OpcodeName(opcodeName
),
974 VTs(vts
, vts
+numvts
), Operands(operands
, operands
+numops
),
975 HasChain(hasChain
), HasInGlue(hasInGlue
), HasOutGlue(hasOutGlue
),
976 HasMemRefs(hasmemrefs
), NumFixedArityOperands(numfixedarityoperands
) {}
978 const std::string
&getOpcodeName() const { return OpcodeName
; }
980 unsigned getNumVTs() const { return VTs
.size(); }
981 MVT::SimpleValueType
getVT(unsigned i
) const {
982 assert(i
< VTs
.size());
986 unsigned getNumOperands() const { return Operands
.size(); }
987 unsigned getOperand(unsigned i
) const {
988 assert(i
< Operands
.size());
992 const SmallVectorImpl
<MVT::SimpleValueType
> &getVTList() const { return VTs
; }
993 const SmallVectorImpl
<unsigned> &getOperandList() const { return Operands
; }
996 bool hasChain() const { return HasChain
; }
997 bool hasInFlag() const { return HasInGlue
; }
998 bool hasOutFlag() const { return HasOutGlue
; }
999 bool hasMemRefs() const { return HasMemRefs
; }
1000 int getNumFixedArityOperands() const { return NumFixedArityOperands
; }
1002 static inline bool classof(const Matcher
*N
) {
1003 return N
->getKind() == EmitNode
|| N
->getKind() == MorphNodeTo
;
1007 virtual void printImpl(raw_ostream
&OS
, unsigned indent
) const;
1008 virtual bool isEqualImpl(const Matcher
*M
) const;
1009 virtual unsigned getHashImpl() const;
1012 /// EmitNodeMatcher - This signals a successful match and generates a node.
1013 class EmitNodeMatcher
: public EmitNodeMatcherCommon
{
1014 unsigned FirstResultSlot
;
1016 EmitNodeMatcher(const std::string
&opcodeName
,
1017 const MVT::SimpleValueType
*vts
, unsigned numvts
,
1018 const unsigned *operands
, unsigned numops
,
1019 bool hasChain
, bool hasInFlag
, bool hasOutFlag
,
1021 int numfixedarityoperands
, unsigned firstresultslot
)
1022 : EmitNodeMatcherCommon(opcodeName
, vts
, numvts
, operands
, numops
, hasChain
,
1023 hasInFlag
, hasOutFlag
, hasmemrefs
,
1024 numfixedarityoperands
, false),
1025 FirstResultSlot(firstresultslot
) {}
1027 unsigned getFirstResultSlot() const { return FirstResultSlot
; }
1029 static inline bool classof(const Matcher
*N
) {
1030 return N
->getKind() == EmitNode
;
1035 class MorphNodeToMatcher
: public EmitNodeMatcherCommon
{
1036 const PatternToMatch
&Pattern
;
1038 MorphNodeToMatcher(const std::string
&opcodeName
,
1039 const MVT::SimpleValueType
*vts
, unsigned numvts
,
1040 const unsigned *operands
, unsigned numops
,
1041 bool hasChain
, bool hasInFlag
, bool hasOutFlag
,
1043 int numfixedarityoperands
, const PatternToMatch
&pattern
)
1044 : EmitNodeMatcherCommon(opcodeName
, vts
, numvts
, operands
, numops
, hasChain
,
1045 hasInFlag
, hasOutFlag
, hasmemrefs
,
1046 numfixedarityoperands
, true),
1050 const PatternToMatch
&getPattern() const { return Pattern
; }
1052 static inline bool classof(const Matcher
*N
) {
1053 return N
->getKind() == MorphNodeTo
;
1057 /// MarkGlueResultsMatcher - This node indicates which non-root nodes in the
1058 /// pattern produce glue. This allows CompleteMatchMatcher to update them
1059 /// with the output glue of the resultant code.
1060 class MarkGlueResultsMatcher
: public Matcher
{
1061 SmallVector
<unsigned, 3> GlueResultNodes
;
1063 MarkGlueResultsMatcher(const unsigned *nodes
, unsigned NumNodes
)
1064 : Matcher(MarkGlueResults
), GlueResultNodes(nodes
, nodes
+NumNodes
) {}
1066 unsigned getNumNodes() const { return GlueResultNodes
.size(); }
1068 unsigned getNode(unsigned i
) const {
1069 assert(i
< GlueResultNodes
.size());
1070 return GlueResultNodes
[i
];
1073 static inline bool classof(const Matcher
*N
) {
1074 return N
->getKind() == MarkGlueResults
;
1078 virtual void printImpl(raw_ostream
&OS
, unsigned indent
) const;
1079 virtual bool isEqualImpl(const Matcher
*M
) const {
1080 return cast
<MarkGlueResultsMatcher
>(M
)->GlueResultNodes
== GlueResultNodes
;
1082 virtual unsigned getHashImpl() const;
1085 /// CompleteMatchMatcher - Complete a match by replacing the results of the
1086 /// pattern with the newly generated nodes. This also prints a comment
1087 /// indicating the source and dest patterns.
1088 class CompleteMatchMatcher
: public Matcher
{
1089 SmallVector
<unsigned, 2> Results
;
1090 const PatternToMatch
&Pattern
;
1092 CompleteMatchMatcher(const unsigned *results
, unsigned numresults
,
1093 const PatternToMatch
&pattern
)
1094 : Matcher(CompleteMatch
), Results(results
, results
+numresults
),
1097 unsigned getNumResults() const { return Results
.size(); }
1098 unsigned getResult(unsigned R
) const { return Results
[R
]; }
1099 const PatternToMatch
&getPattern() const { return Pattern
; }
1101 static inline bool classof(const Matcher
*N
) {
1102 return N
->getKind() == CompleteMatch
;
1106 virtual void printImpl(raw_ostream
&OS
, unsigned indent
) const;
1107 virtual bool isEqualImpl(const Matcher
*M
) const {
1108 return cast
<CompleteMatchMatcher
>(M
)->Results
== Results
&&
1109 &cast
<CompleteMatchMatcher
>(M
)->Pattern
== &Pattern
;
1111 virtual unsigned getHashImpl() const;
1114 } // end namespace llvm