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
;
29 Matcher
*ConvertPatternToMatcher(const PatternToMatch
&Pattern
,unsigned Variant
,
30 const CodeGenDAGPatterns
&CGP
);
31 Matcher
*OptimizeMatcher(Matcher
*Matcher
, const CodeGenDAGPatterns
&CGP
);
32 void EmitMatcherTable(const Matcher
*Matcher
, const CodeGenDAGPatterns
&CGP
,
36 /// Matcher - Base class for all the the DAG ISel Matcher representation
39 // The next matcher node that is executed after this one. Null if this is the
40 // last stage of a match.
41 OwningPtr
<Matcher
> Next
;
44 // Matcher state manipulation.
45 Scope
, // Push a checking scope.
46 RecordNode
, // Record the current node.
47 RecordChild
, // Record a child of the current node.
48 RecordMemRef
, // Record the memref in the current node.
49 CaptureGlueInput
, // If the current node has an input glue, save it.
50 MoveChild
, // Move current node to specified child.
51 MoveParent
, // Move current node to parent.
53 // Predicate checking.
54 CheckSame
, // Fail if not same as prev match.
55 CheckPatternPredicate
,
56 CheckPredicate
, // Fail if node predicate fails.
57 CheckOpcode
, // Fail if not opcode.
58 SwitchOpcode
, // Dispatch based on opcode.
59 CheckType
, // Fail if not correct type.
60 SwitchType
, // Dispatch based on type.
61 CheckChildType
, // Fail if child has wrong type.
62 CheckInteger
, // Fail if wrong val.
63 CheckCondCode
, // Fail if not condcode.
68 CheckFoldableChainNode
,
70 // Node creation/emisssion.
71 EmitInteger
, // Create a TargetConstant
72 EmitStringInteger
, // Create a TargetConstant from a string.
73 EmitRegister
, // Create a register.
74 EmitConvertToTarget
, // Convert a imm/fpimm to target imm/fpimm
75 EmitMergeInputChains
, // Merge together a chains for an input.
76 EmitCopyToReg
, // Emit a copytoreg into a physreg.
77 EmitNode
, // Create a DAG node
78 EmitNodeXForm
, // Run a SDNodeXForm
79 MarkGlueResults
, // Indicate which interior nodes have glue results.
80 CompleteMatch
, // Finish a match and update the results.
81 MorphNodeTo
// Build a node, finish a match and update results.
86 Matcher(KindTy K
) : Kind(K
) {}
90 KindTy
getKind() const { return Kind
; }
92 Matcher
*getNext() { return Next
.get(); }
93 const Matcher
*getNext() const { return Next
.get(); }
94 void setNext(Matcher
*C
) { Next
.reset(C
); }
95 Matcher
*takeNext() { return Next
.take(); }
97 OwningPtr
<Matcher
> &getNextPtr() { return Next
; }
99 static inline bool classof(const Matcher
*) { return true; }
101 bool isEqual(const Matcher
*M
) const {
102 if (getKind() != M
->getKind()) return false;
103 return isEqualImpl(M
);
106 unsigned getHash() const {
107 // Clear the high bit so we don't conflict with tombstones etc.
108 return ((getHashImpl() << 4) ^ getKind()) & (~0U>>1);
111 /// isSafeToReorderWithPatternPredicate - Return true if it is safe to sink a
112 /// PatternPredicate node past this one.
113 virtual bool isSafeToReorderWithPatternPredicate() const {
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;
124 case CheckPatternPredicate
:
134 case CheckFoldableChainNode
:
139 /// isSimplePredicateOrRecordNode - Return true if this is a record node or
140 /// a simple predicate.
141 bool isSimplePredicateOrRecordNode() const {
142 return isSimplePredicateNode() ||
143 getKind() == RecordNode
|| getKind() == RecordChild
;
146 /// unlinkNode - Unlink the specified node from this chain. If Other == this,
147 /// we unlink the next pointer and return it. Otherwise we unlink Other from
148 /// the list and return this.
149 Matcher
*unlinkNode(Matcher
*Other
);
151 /// canMoveBefore - Return true if this matcher is the same as Other, or if
152 /// we can move this matcher past all of the nodes in-between Other and this
153 /// node. Other must be equal to or before this.
154 bool canMoveBefore(const Matcher
*Other
) const;
156 /// canMoveBefore - Return true if it is safe to move the current matcher
157 /// across the specified one.
158 bool canMoveBeforeNode(const Matcher
*Other
) const;
160 /// isContradictory - Return true of these two matchers could never match on
162 bool isContradictory(const Matcher
*Other
) const {
163 // Since this predicate is reflexive, we canonicalize the ordering so that
164 // we always match a node against nodes with kinds that are greater or equal
165 // to them. For example, we'll pass in a CheckType node as an argument to
166 // the CheckOpcode method, not the other way around.
167 if (getKind() < Other
->getKind())
168 return isContradictoryImpl(Other
);
169 return Other
->isContradictoryImpl(this);
172 void print(raw_ostream
&OS
, unsigned indent
= 0) const;
173 void printOne(raw_ostream
&OS
) const;
176 virtual void printImpl(raw_ostream
&OS
, unsigned indent
) const = 0;
177 virtual bool isEqualImpl(const Matcher
*M
) const = 0;
178 virtual unsigned getHashImpl() const = 0;
179 virtual bool isContradictoryImpl(const Matcher
*M
) const { return false; }
182 /// ScopeMatcher - This attempts to match each of its children to find the first
183 /// one that successfully matches. If one child fails, it tries the next child.
184 /// If none of the children match then this check fails. It never has a 'next'.
185 class ScopeMatcher
: public Matcher
{
186 SmallVector
<Matcher
*, 4> Children
;
188 ScopeMatcher(Matcher
*const *children
, unsigned numchildren
)
189 : Matcher(Scope
), Children(children
, children
+numchildren
) {
191 virtual ~ScopeMatcher();
193 unsigned getNumChildren() const { return Children
.size(); }
195 Matcher
*getChild(unsigned i
) { return Children
[i
]; }
196 const Matcher
*getChild(unsigned i
) const { return Children
[i
]; }
198 void resetChild(unsigned i
, Matcher
*N
) {
203 Matcher
*takeChild(unsigned i
) {
204 Matcher
*Res
= Children
[i
];
209 void setNumChildren(unsigned NC
) {
210 if (NC
< Children
.size()) {
211 // delete any children we're about to lose pointers to.
212 for (unsigned i
= NC
, e
= Children
.size(); i
!= e
; ++i
)
218 static inline bool classof(const Matcher
*N
) {
219 return N
->getKind() == Scope
;
223 virtual void printImpl(raw_ostream
&OS
, unsigned indent
) const;
224 virtual bool isEqualImpl(const Matcher
*M
) const { return false; }
225 virtual unsigned getHashImpl() const { return 12312; }
228 /// RecordMatcher - Save the current node in the operand list.
229 class RecordMatcher
: public Matcher
{
230 /// WhatFor - This is a string indicating why we're recording this. This
231 /// should only be used for comment generation not anything semantic.
234 /// ResultNo - The slot number in the RecordedNodes vector that this will be,
235 /// just printed as a comment.
238 RecordMatcher(const std::string
&whatfor
, unsigned resultNo
)
239 : Matcher(RecordNode
), WhatFor(whatfor
), ResultNo(resultNo
) {}
241 const std::string
&getWhatFor() const { return WhatFor
; }
242 unsigned getResultNo() const { return ResultNo
; }
244 static inline bool classof(const Matcher
*N
) {
245 return N
->getKind() == RecordNode
;
248 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
250 virtual void printImpl(raw_ostream
&OS
, unsigned indent
) const;
251 virtual bool isEqualImpl(const Matcher
*M
) const { return true; }
252 virtual unsigned getHashImpl() const { return 0; }
255 /// RecordChildMatcher - Save a numbered child of the current node, or fail
256 /// the match if it doesn't exist. This is logically equivalent to:
257 /// MoveChild N + RecordNode + MoveParent.
258 class RecordChildMatcher
: public Matcher
{
261 /// WhatFor - This is a string indicating why we're recording this. This
262 /// should only be used for comment generation not anything semantic.
265 /// ResultNo - The slot number in the RecordedNodes vector that this will be,
266 /// just printed as a comment.
269 RecordChildMatcher(unsigned childno
, const std::string
&whatfor
,
271 : Matcher(RecordChild
), ChildNo(childno
), WhatFor(whatfor
),
272 ResultNo(resultNo
) {}
274 unsigned getChildNo() const { return ChildNo
; }
275 const std::string
&getWhatFor() const { return WhatFor
; }
276 unsigned getResultNo() const { return ResultNo
; }
278 static inline bool classof(const Matcher
*N
) {
279 return N
->getKind() == RecordChild
;
282 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
285 virtual void printImpl(raw_ostream
&OS
, unsigned indent
) const;
286 virtual bool isEqualImpl(const Matcher
*M
) const {
287 return cast
<RecordChildMatcher
>(M
)->getChildNo() == getChildNo();
289 virtual unsigned getHashImpl() const { return getChildNo(); }
292 /// RecordMemRefMatcher - Save the current node's memref.
293 class RecordMemRefMatcher
: public Matcher
{
295 RecordMemRefMatcher() : Matcher(RecordMemRef
) {}
297 static inline bool classof(const Matcher
*N
) {
298 return N
->getKind() == RecordMemRef
;
301 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
304 virtual void printImpl(raw_ostream
&OS
, unsigned indent
) const;
305 virtual bool isEqualImpl(const Matcher
*M
) const { return true; }
306 virtual unsigned getHashImpl() const { return 0; }
310 /// CaptureGlueInputMatcher - If the current record has a glue input, record
311 /// it so that it is used as an input to the generated code.
312 class CaptureGlueInputMatcher
: public Matcher
{
314 CaptureGlueInputMatcher() : Matcher(CaptureGlueInput
) {}
316 static inline bool classof(const Matcher
*N
) {
317 return N
->getKind() == CaptureGlueInput
;
320 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
323 virtual void printImpl(raw_ostream
&OS
, unsigned indent
) const;
324 virtual bool isEqualImpl(const Matcher
*M
) const { return true; }
325 virtual unsigned getHashImpl() const { return 0; }
328 /// MoveChildMatcher - This tells the interpreter to move into the
329 /// specified child node.
330 class MoveChildMatcher
: public Matcher
{
333 MoveChildMatcher(unsigned childNo
) : Matcher(MoveChild
), ChildNo(childNo
) {}
335 unsigned getChildNo() const { return ChildNo
; }
337 static inline bool classof(const Matcher
*N
) {
338 return N
->getKind() == MoveChild
;
341 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
344 virtual void printImpl(raw_ostream
&OS
, unsigned indent
) const;
345 virtual bool isEqualImpl(const Matcher
*M
) const {
346 return cast
<MoveChildMatcher
>(M
)->getChildNo() == getChildNo();
348 virtual unsigned getHashImpl() const { return getChildNo(); }
351 /// MoveParentMatcher - This tells the interpreter to move to the parent
352 /// of the current node.
353 class MoveParentMatcher
: public Matcher
{
355 MoveParentMatcher() : Matcher(MoveParent
) {}
357 static inline bool classof(const Matcher
*N
) {
358 return N
->getKind() == MoveParent
;
361 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
364 virtual void printImpl(raw_ostream
&OS
, unsigned indent
) const;
365 virtual bool isEqualImpl(const Matcher
*M
) const { return true; }
366 virtual unsigned getHashImpl() const { return 0; }
369 /// CheckSameMatcher - This checks to see if this node is exactly the same
370 /// node as the specified match that was recorded with 'Record'. This is used
371 /// when patterns have the same name in them, like '(mul GPR:$in, GPR:$in)'.
372 class CheckSameMatcher
: public Matcher
{
373 unsigned MatchNumber
;
375 CheckSameMatcher(unsigned matchnumber
)
376 : Matcher(CheckSame
), MatchNumber(matchnumber
) {}
378 unsigned getMatchNumber() const { return MatchNumber
; }
380 static inline bool classof(const Matcher
*N
) {
381 return N
->getKind() == CheckSame
;
384 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
387 virtual void printImpl(raw_ostream
&OS
, unsigned indent
) const;
388 virtual bool isEqualImpl(const Matcher
*M
) const {
389 return cast
<CheckSameMatcher
>(M
)->getMatchNumber() == getMatchNumber();
391 virtual unsigned getHashImpl() const { return getMatchNumber(); }
394 /// CheckPatternPredicateMatcher - This checks the target-specific predicate
395 /// to see if the entire pattern is capable of matching. This predicate does
396 /// not take a node as input. This is used for subtarget feature checks etc.
397 class CheckPatternPredicateMatcher
: public Matcher
{
398 std::string Predicate
;
400 CheckPatternPredicateMatcher(StringRef predicate
)
401 : Matcher(CheckPatternPredicate
), Predicate(predicate
) {}
403 StringRef
getPredicate() const { return Predicate
; }
405 static inline bool classof(const Matcher
*N
) {
406 return N
->getKind() == CheckPatternPredicate
;
409 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
412 virtual void printImpl(raw_ostream
&OS
, unsigned indent
) const;
413 virtual bool isEqualImpl(const Matcher
*M
) const {
414 return cast
<CheckPatternPredicateMatcher
>(M
)->getPredicate() == Predicate
;
416 virtual unsigned getHashImpl() const;
419 /// CheckPredicateMatcher - This checks the target-specific predicate to
420 /// see if the node is acceptable.
421 class CheckPredicateMatcher
: public Matcher
{
424 CheckPredicateMatcher(StringRef predname
)
425 : Matcher(CheckPredicate
), PredName(predname
) {}
427 StringRef
getPredicateName() const { return PredName
; }
429 static inline bool classof(const Matcher
*N
) {
430 return N
->getKind() == CheckPredicate
;
434 //virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
437 virtual void printImpl(raw_ostream
&OS
, unsigned indent
) const;
438 virtual bool isEqualImpl(const Matcher
*M
) const {
439 return cast
<CheckPredicateMatcher
>(M
)->PredName
== PredName
;
441 virtual unsigned getHashImpl() const;
445 /// CheckOpcodeMatcher - This checks to see if the current node has the
446 /// specified opcode, if not it fails to match.
447 class CheckOpcodeMatcher
: public Matcher
{
448 const SDNodeInfo
&Opcode
;
450 CheckOpcodeMatcher(const SDNodeInfo
&opcode
)
451 : Matcher(CheckOpcode
), Opcode(opcode
) {}
453 const SDNodeInfo
&getOpcode() const { return Opcode
; }
455 static inline bool classof(const Matcher
*N
) {
456 return N
->getKind() == CheckOpcode
;
459 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
462 virtual void printImpl(raw_ostream
&OS
, unsigned indent
) const;
463 virtual bool isEqualImpl(const Matcher
*M
) const;
464 virtual unsigned getHashImpl() const;
465 virtual bool isContradictoryImpl(const Matcher
*M
) const;
468 /// SwitchOpcodeMatcher - Switch based on the current node's opcode, dispatching
469 /// to one matcher per opcode. If the opcode doesn't match any of the cases,
470 /// then the match fails. This is semantically equivalent to a Scope node where
471 /// every child does a CheckOpcode, but is much faster.
472 class SwitchOpcodeMatcher
: public Matcher
{
473 SmallVector
<std::pair
<const SDNodeInfo
*, Matcher
*>, 8> Cases
;
475 SwitchOpcodeMatcher(const std::pair
<const SDNodeInfo
*, Matcher
*> *cases
,
477 : Matcher(SwitchOpcode
), Cases(cases
, cases
+numcases
) {}
479 static inline bool classof(const Matcher
*N
) {
480 return N
->getKind() == SwitchOpcode
;
483 unsigned getNumCases() const { return Cases
.size(); }
485 const SDNodeInfo
&getCaseOpcode(unsigned i
) const { return *Cases
[i
].first
; }
486 Matcher
*getCaseMatcher(unsigned i
) { return Cases
[i
].second
; }
487 const Matcher
*getCaseMatcher(unsigned i
) const { return Cases
[i
].second
; }
490 virtual void printImpl(raw_ostream
&OS
, unsigned indent
) const;
491 virtual bool isEqualImpl(const Matcher
*M
) const { return false; }
492 virtual unsigned getHashImpl() const { return 4123; }
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 inline bool classof(const Matcher
*N
) {
508 return N
->getKind() == CheckType
;
511 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
514 virtual void printImpl(raw_ostream
&OS
, unsigned indent
) const;
515 virtual bool isEqualImpl(const Matcher
*M
) const {
516 return cast
<CheckTypeMatcher
>(M
)->Type
== Type
;
518 virtual unsigned getHashImpl() const { return Type
; }
519 virtual bool isContradictoryImpl(const Matcher
*M
) const;
522 /// SwitchTypeMatcher - Switch based on the current node's type, dispatching
523 /// to one matcher per case. If the type doesn't match any of the cases,
524 /// then the match fails. This is semantically equivalent to a Scope node where
525 /// every child does a CheckType, but is much faster.
526 class SwitchTypeMatcher
: public Matcher
{
527 SmallVector
<std::pair
<MVT::SimpleValueType
, Matcher
*>, 8> Cases
;
529 SwitchTypeMatcher(const std::pair
<MVT::SimpleValueType
, Matcher
*> *cases
,
531 : Matcher(SwitchType
), Cases(cases
, cases
+numcases
) {}
533 static inline bool classof(const Matcher
*N
) {
534 return N
->getKind() == SwitchType
;
537 unsigned getNumCases() const { return Cases
.size(); }
539 MVT::SimpleValueType
getCaseType(unsigned i
) const { return Cases
[i
].first
; }
540 Matcher
*getCaseMatcher(unsigned i
) { return Cases
[i
].second
; }
541 const Matcher
*getCaseMatcher(unsigned i
) const { return Cases
[i
].second
; }
544 virtual void printImpl(raw_ostream
&OS
, unsigned indent
) const;
545 virtual bool isEqualImpl(const Matcher
*M
) const { return false; }
546 virtual unsigned getHashImpl() const { return 4123; }
550 /// CheckChildTypeMatcher - This checks to see if a child node has the
551 /// specified type, if not it fails to match.
552 class CheckChildTypeMatcher
: public Matcher
{
554 MVT::SimpleValueType Type
;
556 CheckChildTypeMatcher(unsigned childno
, MVT::SimpleValueType type
)
557 : Matcher(CheckChildType
), ChildNo(childno
), Type(type
) {}
559 unsigned getChildNo() const { return ChildNo
; }
560 MVT::SimpleValueType
getType() const { return Type
; }
562 static inline bool classof(const Matcher
*N
) {
563 return N
->getKind() == CheckChildType
;
566 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
569 virtual void printImpl(raw_ostream
&OS
, unsigned indent
) const;
570 virtual bool isEqualImpl(const Matcher
*M
) const {
571 return cast
<CheckChildTypeMatcher
>(M
)->ChildNo
== ChildNo
&&
572 cast
<CheckChildTypeMatcher
>(M
)->Type
== Type
;
574 virtual unsigned getHashImpl() const { return (Type
<< 3) | ChildNo
; }
575 virtual bool isContradictoryImpl(const Matcher
*M
) const;
579 /// CheckIntegerMatcher - This checks to see if the current node is a
580 /// ConstantSDNode with the specified integer value, if not it fails to match.
581 class CheckIntegerMatcher
: public Matcher
{
584 CheckIntegerMatcher(int64_t value
)
585 : Matcher(CheckInteger
), Value(value
) {}
587 int64_t getValue() const { return Value
; }
589 static inline bool classof(const Matcher
*N
) {
590 return N
->getKind() == CheckInteger
;
593 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
596 virtual void printImpl(raw_ostream
&OS
, unsigned indent
) const;
597 virtual bool isEqualImpl(const Matcher
*M
) const {
598 return cast
<CheckIntegerMatcher
>(M
)->Value
== Value
;
600 virtual unsigned getHashImpl() const { return Value
; }
601 virtual bool isContradictoryImpl(const Matcher
*M
) const;
604 /// CheckCondCodeMatcher - This checks to see if the current node is a
605 /// CondCodeSDNode with the specified condition, if not it fails to match.
606 class CheckCondCodeMatcher
: public Matcher
{
607 StringRef CondCodeName
;
609 CheckCondCodeMatcher(StringRef condcodename
)
610 : Matcher(CheckCondCode
), CondCodeName(condcodename
) {}
612 StringRef
getCondCodeName() const { return CondCodeName
; }
614 static inline bool classof(const Matcher
*N
) {
615 return N
->getKind() == CheckCondCode
;
618 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
621 virtual void printImpl(raw_ostream
&OS
, unsigned indent
) const;
622 virtual bool isEqualImpl(const Matcher
*M
) const {
623 return cast
<CheckCondCodeMatcher
>(M
)->CondCodeName
== CondCodeName
;
625 virtual unsigned getHashImpl() const;
628 /// CheckValueTypeMatcher - This checks to see if the current node is a
629 /// VTSDNode with the specified type, if not it fails to match.
630 class CheckValueTypeMatcher
: public Matcher
{
633 CheckValueTypeMatcher(StringRef type_name
)
634 : Matcher(CheckValueType
), TypeName(type_name
) {}
636 StringRef
getTypeName() const { return TypeName
; }
638 static inline bool classof(const Matcher
*N
) {
639 return N
->getKind() == CheckValueType
;
642 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
645 virtual void printImpl(raw_ostream
&OS
, unsigned indent
) const;
646 virtual bool isEqualImpl(const Matcher
*M
) const {
647 return cast
<CheckValueTypeMatcher
>(M
)->TypeName
== TypeName
;
649 virtual unsigned getHashImpl() const;
650 bool isContradictoryImpl(const Matcher
*M
) const;
655 /// CheckComplexPatMatcher - This node runs the specified ComplexPattern on
656 /// the current node.
657 class CheckComplexPatMatcher
: public Matcher
{
658 const ComplexPattern
&Pattern
;
660 /// MatchNumber - This is the recorded nodes slot that contains the node we
661 /// want to match against.
662 unsigned MatchNumber
;
664 /// Name - The name of the node we're matching, for comment emission.
667 /// FirstResult - This is the first slot in the RecordedNodes list that the
668 /// result of the match populates.
669 unsigned FirstResult
;
671 CheckComplexPatMatcher(const ComplexPattern
&pattern
, unsigned matchnumber
,
672 const std::string
&name
, unsigned firstresult
)
673 : Matcher(CheckComplexPat
), Pattern(pattern
), MatchNumber(matchnumber
),
674 Name(name
), FirstResult(firstresult
) {}
676 const ComplexPattern
&getPattern() const { return Pattern
; }
677 unsigned getMatchNumber() const { return MatchNumber
; }
679 const std::string
getName() const { return Name
; }
680 unsigned getFirstResult() const { return FirstResult
; }
682 static inline bool classof(const Matcher
*N
) {
683 return N
->getKind() == CheckComplexPat
;
686 // Not safe to move a pattern predicate past a complex pattern.
687 virtual bool isSafeToReorderWithPatternPredicate() const { return false; }
690 virtual void printImpl(raw_ostream
&OS
, unsigned indent
) const;
691 virtual bool isEqualImpl(const Matcher
*M
) const {
692 return &cast
<CheckComplexPatMatcher
>(M
)->Pattern
== &Pattern
&&
693 cast
<CheckComplexPatMatcher
>(M
)->MatchNumber
== MatchNumber
;
695 virtual unsigned getHashImpl() const {
696 return (unsigned)(intptr_t)&Pattern
^ MatchNumber
;
700 /// CheckAndImmMatcher - This checks to see if the current node is an 'and'
701 /// with something equivalent to the specified immediate.
702 class CheckAndImmMatcher
: public Matcher
{
705 CheckAndImmMatcher(int64_t value
)
706 : Matcher(CheckAndImm
), Value(value
) {}
708 int64_t getValue() const { return Value
; }
710 static inline bool classof(const Matcher
*N
) {
711 return N
->getKind() == CheckAndImm
;
714 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
717 virtual void printImpl(raw_ostream
&OS
, unsigned indent
) const;
718 virtual bool isEqualImpl(const Matcher
*M
) const {
719 return cast
<CheckAndImmMatcher
>(M
)->Value
== Value
;
721 virtual unsigned getHashImpl() const { return Value
; }
724 /// CheckOrImmMatcher - This checks to see if the current node is an 'and'
725 /// with something equivalent to the specified immediate.
726 class CheckOrImmMatcher
: public Matcher
{
729 CheckOrImmMatcher(int64_t value
)
730 : Matcher(CheckOrImm
), Value(value
) {}
732 int64_t getValue() const { return Value
; }
734 static inline bool classof(const Matcher
*N
) {
735 return N
->getKind() == CheckOrImm
;
738 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
741 virtual void printImpl(raw_ostream
&OS
, unsigned indent
) const;
742 virtual bool isEqualImpl(const Matcher
*M
) const {
743 return cast
<CheckOrImmMatcher
>(M
)->Value
== Value
;
745 virtual unsigned getHashImpl() const { return Value
; }
748 /// CheckFoldableChainNodeMatcher - This checks to see if the current node
749 /// (which defines a chain operand) is safe to fold into a larger pattern.
750 class CheckFoldableChainNodeMatcher
: public Matcher
{
752 CheckFoldableChainNodeMatcher()
753 : Matcher(CheckFoldableChainNode
) {}
755 static inline bool classof(const Matcher
*N
) {
756 return N
->getKind() == CheckFoldableChainNode
;
759 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
762 virtual void printImpl(raw_ostream
&OS
, unsigned indent
) const;
763 virtual bool isEqualImpl(const Matcher
*M
) const { return true; }
764 virtual unsigned getHashImpl() const { return 0; }
767 /// EmitIntegerMatcher - This creates a new TargetConstant.
768 class EmitIntegerMatcher
: public Matcher
{
770 MVT::SimpleValueType VT
;
772 EmitIntegerMatcher(int64_t val
, MVT::SimpleValueType vt
)
773 : Matcher(EmitInteger
), Val(val
), VT(vt
) {}
775 int64_t getValue() const { return Val
; }
776 MVT::SimpleValueType
getVT() const { return VT
; }
778 static inline bool classof(const Matcher
*N
) {
779 return N
->getKind() == EmitInteger
;
783 virtual void printImpl(raw_ostream
&OS
, unsigned indent
) const;
784 virtual bool isEqualImpl(const Matcher
*M
) const {
785 return cast
<EmitIntegerMatcher
>(M
)->Val
== Val
&&
786 cast
<EmitIntegerMatcher
>(M
)->VT
== VT
;
788 virtual unsigned getHashImpl() const { return (Val
<< 4) | VT
; }
791 /// EmitStringIntegerMatcher - A target constant whose value is represented
793 class EmitStringIntegerMatcher
: public Matcher
{
795 MVT::SimpleValueType VT
;
797 EmitStringIntegerMatcher(const std::string
&val
, MVT::SimpleValueType vt
)
798 : Matcher(EmitStringInteger
), Val(val
), VT(vt
) {}
800 const std::string
&getValue() const { return Val
; }
801 MVT::SimpleValueType
getVT() const { return VT
; }
803 static inline bool classof(const Matcher
*N
) {
804 return N
->getKind() == EmitStringInteger
;
808 virtual void printImpl(raw_ostream
&OS
, unsigned indent
) const;
809 virtual bool isEqualImpl(const Matcher
*M
) const {
810 return cast
<EmitStringIntegerMatcher
>(M
)->Val
== Val
&&
811 cast
<EmitStringIntegerMatcher
>(M
)->VT
== VT
;
813 virtual unsigned getHashImpl() const;
816 /// EmitRegisterMatcher - This creates a new TargetConstant.
817 class EmitRegisterMatcher
: public Matcher
{
818 /// Reg - The def for the register that we're emitting. If this is null, then
819 /// this is a reference to zero_reg.
820 const CodeGenRegister
*Reg
;
821 MVT::SimpleValueType VT
;
823 EmitRegisterMatcher(const CodeGenRegister
*reg
, MVT::SimpleValueType vt
)
824 : Matcher(EmitRegister
), Reg(reg
), VT(vt
) {}
826 const CodeGenRegister
*getReg() const { return Reg
; }
827 MVT::SimpleValueType
getVT() const { return VT
; }
829 static inline bool classof(const Matcher
*N
) {
830 return N
->getKind() == EmitRegister
;
834 virtual void printImpl(raw_ostream
&OS
, unsigned indent
) const;
835 virtual bool isEqualImpl(const Matcher
*M
) const {
836 return cast
<EmitRegisterMatcher
>(M
)->Reg
== Reg
&&
837 cast
<EmitRegisterMatcher
>(M
)->VT
== VT
;
839 virtual unsigned getHashImpl() const {
840 return ((unsigned)(intptr_t)Reg
) << 4 | VT
;
844 /// EmitConvertToTargetMatcher - Emit an operation that reads a specified
845 /// recorded node and converts it from being a ISD::Constant to
846 /// ISD::TargetConstant, likewise for ConstantFP.
847 class EmitConvertToTargetMatcher
: public Matcher
{
850 EmitConvertToTargetMatcher(unsigned slot
)
851 : Matcher(EmitConvertToTarget
), Slot(slot
) {}
853 unsigned getSlot() const { return Slot
; }
855 static inline bool classof(const Matcher
*N
) {
856 return N
->getKind() == EmitConvertToTarget
;
860 virtual void printImpl(raw_ostream
&OS
, unsigned indent
) const;
861 virtual bool isEqualImpl(const Matcher
*M
) const {
862 return cast
<EmitConvertToTargetMatcher
>(M
)->Slot
== Slot
;
864 virtual unsigned getHashImpl() const { return Slot
; }
867 /// EmitMergeInputChainsMatcher - Emit a node that merges a list of input
868 /// chains together with a token factor. The list of nodes are the nodes in the
869 /// matched pattern that have chain input/outputs. This node adds all input
870 /// chains of these nodes if they are not themselves a node in the pattern.
871 class EmitMergeInputChainsMatcher
: public Matcher
{
872 SmallVector
<unsigned, 3> ChainNodes
;
874 EmitMergeInputChainsMatcher(const unsigned *nodes
, unsigned NumNodes
)
875 : Matcher(EmitMergeInputChains
), ChainNodes(nodes
, nodes
+NumNodes
) {}
877 unsigned getNumNodes() const { return ChainNodes
.size(); }
879 unsigned getNode(unsigned i
) const {
880 assert(i
< ChainNodes
.size());
881 return ChainNodes
[i
];
884 static inline bool classof(const Matcher
*N
) {
885 return N
->getKind() == EmitMergeInputChains
;
889 virtual void printImpl(raw_ostream
&OS
, unsigned indent
) const;
890 virtual bool isEqualImpl(const Matcher
*M
) const {
891 return cast
<EmitMergeInputChainsMatcher
>(M
)->ChainNodes
== ChainNodes
;
893 virtual unsigned getHashImpl() const;
896 /// EmitCopyToRegMatcher - Emit a CopyToReg node from a value to a physreg,
897 /// pushing the chain and glue results.
899 class EmitCopyToRegMatcher
: public Matcher
{
900 unsigned SrcSlot
; // Value to copy into the physreg.
903 EmitCopyToRegMatcher(unsigned srcSlot
, Record
*destPhysReg
)
904 : Matcher(EmitCopyToReg
), SrcSlot(srcSlot
), DestPhysReg(destPhysReg
) {}
906 unsigned getSrcSlot() const { return SrcSlot
; }
907 Record
*getDestPhysReg() const { return DestPhysReg
; }
909 static inline bool classof(const Matcher
*N
) {
910 return N
->getKind() == EmitCopyToReg
;
914 virtual void printImpl(raw_ostream
&OS
, unsigned indent
) const;
915 virtual bool isEqualImpl(const Matcher
*M
) const {
916 return cast
<EmitCopyToRegMatcher
>(M
)->SrcSlot
== SrcSlot
&&
917 cast
<EmitCopyToRegMatcher
>(M
)->DestPhysReg
== DestPhysReg
;
919 virtual unsigned getHashImpl() const {
920 return SrcSlot
^ ((unsigned)(intptr_t)DestPhysReg
<< 4);
926 /// EmitNodeXFormMatcher - Emit an operation that runs an SDNodeXForm on a
927 /// recorded node and records the result.
928 class EmitNodeXFormMatcher
: public Matcher
{
932 EmitNodeXFormMatcher(unsigned slot
, Record
*nodeXForm
)
933 : Matcher(EmitNodeXForm
), Slot(slot
), NodeXForm(nodeXForm
) {}
935 unsigned getSlot() const { return Slot
; }
936 Record
*getNodeXForm() const { return NodeXForm
; }
938 static inline bool classof(const Matcher
*N
) {
939 return N
->getKind() == EmitNodeXForm
;
943 virtual void printImpl(raw_ostream
&OS
, unsigned indent
) const;
944 virtual bool isEqualImpl(const Matcher
*M
) const {
945 return cast
<EmitNodeXFormMatcher
>(M
)->Slot
== Slot
&&
946 cast
<EmitNodeXFormMatcher
>(M
)->NodeXForm
== NodeXForm
;
948 virtual unsigned getHashImpl() const {
949 return Slot
^ ((unsigned)(intptr_t)NodeXForm
<< 4);
953 /// EmitNodeMatcherCommon - Common class shared between EmitNode and
955 class EmitNodeMatcherCommon
: public Matcher
{
956 std::string OpcodeName
;
957 const SmallVector
<MVT::SimpleValueType
, 3> VTs
;
958 const SmallVector
<unsigned, 6> Operands
;
959 bool HasChain
, HasInGlue
, HasOutGlue
, HasMemRefs
;
961 /// NumFixedArityOperands - If this is a fixed arity node, this is set to -1.
962 /// If this is a varidic node, this is set to the number of fixed arity
963 /// operands in the root of the pattern. The rest are appended to this node.
964 int NumFixedArityOperands
;
966 EmitNodeMatcherCommon(const std::string
&opcodeName
,
967 const MVT::SimpleValueType
*vts
, unsigned numvts
,
968 const unsigned *operands
, unsigned numops
,
969 bool hasChain
, bool hasInGlue
, bool hasOutGlue
,
971 int numfixedarityoperands
, bool isMorphNodeTo
)
972 : Matcher(isMorphNodeTo
? MorphNodeTo
: EmitNode
), OpcodeName(opcodeName
),
973 VTs(vts
, vts
+numvts
), Operands(operands
, operands
+numops
),
974 HasChain(hasChain
), HasInGlue(hasInGlue
), HasOutGlue(hasOutGlue
),
975 HasMemRefs(hasmemrefs
), NumFixedArityOperands(numfixedarityoperands
) {}
977 const std::string
&getOpcodeName() const { return OpcodeName
; }
979 unsigned getNumVTs() const { return VTs
.size(); }
980 MVT::SimpleValueType
getVT(unsigned i
) const {
981 assert(i
< VTs
.size());
985 unsigned getNumOperands() const { return Operands
.size(); }
986 unsigned getOperand(unsigned i
) const {
987 assert(i
< Operands
.size());
991 const SmallVectorImpl
<MVT::SimpleValueType
> &getVTList() const { return VTs
; }
992 const SmallVectorImpl
<unsigned> &getOperandList() const { return Operands
; }
995 bool hasChain() const { return HasChain
; }
996 bool hasInFlag() const { return HasInGlue
; }
997 bool hasOutFlag() const { return HasOutGlue
; }
998 bool hasMemRefs() const { return HasMemRefs
; }
999 int getNumFixedArityOperands() const { return NumFixedArityOperands
; }
1001 static inline bool classof(const Matcher
*N
) {
1002 return N
->getKind() == EmitNode
|| N
->getKind() == MorphNodeTo
;
1006 virtual void printImpl(raw_ostream
&OS
, unsigned indent
) const;
1007 virtual bool isEqualImpl(const Matcher
*M
) const;
1008 virtual unsigned getHashImpl() const;
1011 /// EmitNodeMatcher - This signals a successful match and generates a node.
1012 class EmitNodeMatcher
: public EmitNodeMatcherCommon
{
1013 unsigned FirstResultSlot
;
1015 EmitNodeMatcher(const std::string
&opcodeName
,
1016 const MVT::SimpleValueType
*vts
, unsigned numvts
,
1017 const unsigned *operands
, unsigned numops
,
1018 bool hasChain
, bool hasInFlag
, bool hasOutFlag
,
1020 int numfixedarityoperands
, unsigned firstresultslot
)
1021 : EmitNodeMatcherCommon(opcodeName
, vts
, numvts
, operands
, numops
, hasChain
,
1022 hasInFlag
, hasOutFlag
, hasmemrefs
,
1023 numfixedarityoperands
, false),
1024 FirstResultSlot(firstresultslot
) {}
1026 unsigned getFirstResultSlot() const { return FirstResultSlot
; }
1028 static inline bool classof(const Matcher
*N
) {
1029 return N
->getKind() == EmitNode
;
1034 class MorphNodeToMatcher
: public EmitNodeMatcherCommon
{
1035 const PatternToMatch
&Pattern
;
1037 MorphNodeToMatcher(const std::string
&opcodeName
,
1038 const MVT::SimpleValueType
*vts
, unsigned numvts
,
1039 const unsigned *operands
, unsigned numops
,
1040 bool hasChain
, bool hasInFlag
, bool hasOutFlag
,
1042 int numfixedarityoperands
, const PatternToMatch
&pattern
)
1043 : EmitNodeMatcherCommon(opcodeName
, vts
, numvts
, operands
, numops
, hasChain
,
1044 hasInFlag
, hasOutFlag
, hasmemrefs
,
1045 numfixedarityoperands
, true),
1049 const PatternToMatch
&getPattern() const { return Pattern
; }
1051 static inline bool classof(const Matcher
*N
) {
1052 return N
->getKind() == MorphNodeTo
;
1056 /// MarkGlueResultsMatcher - This node indicates which non-root nodes in the
1057 /// pattern produce glue. This allows CompleteMatchMatcher to update them
1058 /// with the output glue of the resultant code.
1059 class MarkGlueResultsMatcher
: public Matcher
{
1060 SmallVector
<unsigned, 3> GlueResultNodes
;
1062 MarkGlueResultsMatcher(const unsigned *nodes
, unsigned NumNodes
)
1063 : Matcher(MarkGlueResults
), GlueResultNodes(nodes
, nodes
+NumNodes
) {}
1065 unsigned getNumNodes() const { return GlueResultNodes
.size(); }
1067 unsigned getNode(unsigned i
) const {
1068 assert(i
< GlueResultNodes
.size());
1069 return GlueResultNodes
[i
];
1072 static inline bool classof(const Matcher
*N
) {
1073 return N
->getKind() == MarkGlueResults
;
1077 virtual void printImpl(raw_ostream
&OS
, unsigned indent
) const;
1078 virtual bool isEqualImpl(const Matcher
*M
) const {
1079 return cast
<MarkGlueResultsMatcher
>(M
)->GlueResultNodes
== GlueResultNodes
;
1081 virtual unsigned getHashImpl() const;
1084 /// CompleteMatchMatcher - Complete a match by replacing the results of the
1085 /// pattern with the newly generated nodes. This also prints a comment
1086 /// indicating the source and dest patterns.
1087 class CompleteMatchMatcher
: public Matcher
{
1088 SmallVector
<unsigned, 2> Results
;
1089 const PatternToMatch
&Pattern
;
1091 CompleteMatchMatcher(const unsigned *results
, unsigned numresults
,
1092 const PatternToMatch
&pattern
)
1093 : Matcher(CompleteMatch
), Results(results
, results
+numresults
),
1096 unsigned getNumResults() const { return Results
.size(); }
1097 unsigned getResult(unsigned R
) const { return Results
[R
]; }
1098 const PatternToMatch
&getPattern() const { return Pattern
; }
1100 static inline bool classof(const Matcher
*N
) {
1101 return N
->getKind() == CompleteMatch
;
1105 virtual void printImpl(raw_ostream
&OS
, unsigned indent
) const;
1106 virtual bool isEqualImpl(const Matcher
*M
) const {
1107 return cast
<CompleteMatchMatcher
>(M
)->Results
== Results
&&
1108 &cast
<CompleteMatchMatcher
>(M
)->Pattern
== &Pattern
;
1110 virtual unsigned getHashImpl() const;
1113 } // end namespace llvm