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 class CodeGenDAGPatterns;
28 Matcher *ConvertPatternToMatcher(const PatternToMatch &Pattern,
29 const CodeGenDAGPatterns &CGP);
30 Matcher *OptimizeMatcher(Matcher *Matcher, const CodeGenDAGPatterns &CGP);
31 void EmitMatcherTable(const Matcher *Matcher, const CodeGenDAGPatterns &CGP,
35 /// Matcher - Base class for all the the DAG ISel Matcher representation
38 // The next matcher node that is executed after this one. Null if this is the
39 // last stage of a match.
40 OwningPtr<Matcher> Next;
43 // Matcher state manipulation.
44 Scope, // Push a checking scope.
45 RecordNode, // Record the current node.
46 RecordChild, // Record a child of the current node.
47 RecordMemRef, // Record the memref in the current node.
48 CaptureFlagInput, // If the current node has an input flag, save it.
49 MoveChild, // Move current node to specified child.
50 MoveParent, // Move current node to parent.
52 // Predicate checking.
53 CheckSame, // Fail if not same as prev match.
54 CheckPatternPredicate,
55 CheckPredicate, // Fail if node predicate fails.
56 CheckOpcode, // Fail if not opcode.
57 SwitchOpcode, // Dispatch based on opcode.
58 CheckMultiOpcode, // Fail if not in opcode list.
59 CheckType, // Fail if not correct type.
60 CheckChildType, // Fail if child has wrong type.
61 CheckInteger, // Fail if wrong val.
62 CheckCondCode, // Fail if not condcode.
67 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 MarkFlagResults, // Indicate which interior nodes have flag 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 /// isContradictory - Return true of these two matchers could never match on
119 bool isContradictory(const Matcher *Other) const {
120 // Since this predicate is reflexive, we canonicalize the ordering so that
121 // we always match a node against nodes with kinds that are greater or equal
122 // to them. For example, we'll pass in a CheckType node as an argument to
123 // the CheckOpcode method, not the other way around.
124 if (getKind() < Other->getKind())
125 return isContradictoryImpl(Other);
126 return Other->isContradictoryImpl(this);
129 void print(raw_ostream &OS, unsigned indent = 0) const;
130 void printOne(raw_ostream &OS) const;
133 virtual void printImpl(raw_ostream &OS, unsigned indent) const = 0;
134 virtual bool isEqualImpl(const Matcher *M) const = 0;
135 virtual unsigned getHashImpl() const = 0;
136 virtual bool isContradictoryImpl(const Matcher *M) const { return false; }
139 /// ScopeMatcher - This attempts to match each of its children to find the first
140 /// one that successfully matches. If one child fails, it tries the next child.
141 /// If none of the children match then this check fails. It never has a 'next'.
142 class ScopeMatcher : public Matcher {
143 SmallVector<Matcher*, 4> Children;
145 ScopeMatcher(Matcher *const *children, unsigned numchildren)
146 : Matcher(Scope), Children(children, children+numchildren) {
148 virtual ~ScopeMatcher();
150 unsigned getNumChildren() const { return Children.size(); }
152 Matcher *getChild(unsigned i) { return Children[i]; }
153 const Matcher *getChild(unsigned i) const { return Children[i]; }
155 void resetChild(unsigned i, Matcher *N) {
160 Matcher *takeChild(unsigned i) {
161 Matcher *Res = Children[i];
166 void setNumChildren(unsigned NC) {
167 if (NC < Children.size()) {
168 // delete any children we're about to lose pointers to.
169 for (unsigned i = NC, e = Children.size(); i != e; ++i)
175 static inline bool classof(const Matcher *N) {
176 return N->getKind() == Scope;
180 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
181 virtual bool isEqualImpl(const Matcher *M) const { return false; }
182 virtual unsigned getHashImpl() const { return 12312; }
185 /// RecordMatcher - Save the current node in the operand list.
186 class RecordMatcher : public Matcher {
187 /// WhatFor - This is a string indicating why we're recording this. This
188 /// should only be used for comment generation not anything semantic.
191 /// ResultNo - The slot number in the RecordedNodes vector that this will be,
192 /// just printed as a comment.
195 RecordMatcher(const std::string &whatfor, unsigned resultNo)
196 : Matcher(RecordNode), WhatFor(whatfor), ResultNo(resultNo) {}
198 const std::string &getWhatFor() const { return WhatFor; }
199 unsigned getResultNo() const { return ResultNo; }
201 static inline bool classof(const Matcher *N) {
202 return N->getKind() == RecordNode;
205 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
207 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
208 virtual bool isEqualImpl(const Matcher *M) const { return true; }
209 virtual unsigned getHashImpl() const { return 0; }
212 /// RecordChildMatcher - Save a numbered child of the current node, or fail
213 /// the match if it doesn't exist. This is logically equivalent to:
214 /// MoveChild N + RecordNode + MoveParent.
215 class RecordChildMatcher : public Matcher {
218 /// WhatFor - This is a string indicating why we're recording this. This
219 /// should only be used for comment generation not anything semantic.
222 /// ResultNo - The slot number in the RecordedNodes vector that this will be,
223 /// just printed as a comment.
226 RecordChildMatcher(unsigned childno, const std::string &whatfor,
228 : Matcher(RecordChild), ChildNo(childno), WhatFor(whatfor),
229 ResultNo(resultNo) {}
231 unsigned getChildNo() const { return ChildNo; }
232 const std::string &getWhatFor() const { return WhatFor; }
233 unsigned getResultNo() const { return ResultNo; }
235 static inline bool classof(const Matcher *N) {
236 return N->getKind() == RecordChild;
239 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
242 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
243 virtual bool isEqualImpl(const Matcher *M) const {
244 return cast<RecordChildMatcher>(M)->getChildNo() == getChildNo();
246 virtual unsigned getHashImpl() const { return getChildNo(); }
249 /// RecordMemRefMatcher - Save the current node's memref.
250 class RecordMemRefMatcher : public Matcher {
252 RecordMemRefMatcher() : Matcher(RecordMemRef) {}
254 static inline bool classof(const Matcher *N) {
255 return N->getKind() == RecordMemRef;
258 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
261 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
262 virtual bool isEqualImpl(const Matcher *M) const { return true; }
263 virtual unsigned getHashImpl() const { return 0; }
267 /// CaptureFlagInputMatcher - If the current record has a flag input, record
268 /// it so that it is used as an input to the generated code.
269 class CaptureFlagInputMatcher : public Matcher {
271 CaptureFlagInputMatcher() : Matcher(CaptureFlagInput) {}
273 static inline bool classof(const Matcher *N) {
274 return N->getKind() == CaptureFlagInput;
277 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
280 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
281 virtual bool isEqualImpl(const Matcher *M) const { return true; }
282 virtual unsigned getHashImpl() const { return 0; }
285 /// MoveChildMatcher - This tells the interpreter to move into the
286 /// specified child node.
287 class MoveChildMatcher : public Matcher {
290 MoveChildMatcher(unsigned childNo) : Matcher(MoveChild), ChildNo(childNo) {}
292 unsigned getChildNo() const { return ChildNo; }
294 static inline bool classof(const Matcher *N) {
295 return N->getKind() == MoveChild;
298 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
301 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
302 virtual bool isEqualImpl(const Matcher *M) const {
303 return cast<MoveChildMatcher>(M)->getChildNo() == getChildNo();
305 virtual unsigned getHashImpl() const { return getChildNo(); }
308 /// MoveParentMatcher - This tells the interpreter to move to the parent
309 /// of the current node.
310 class MoveParentMatcher : public Matcher {
312 MoveParentMatcher() : Matcher(MoveParent) {}
314 static inline bool classof(const Matcher *N) {
315 return N->getKind() == MoveParent;
318 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
321 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
322 virtual bool isEqualImpl(const Matcher *M) const { return true; }
323 virtual unsigned getHashImpl() const { return 0; }
326 /// CheckSameMatcher - This checks to see if this node is exactly the same
327 /// node as the specified match that was recorded with 'Record'. This is used
328 /// when patterns have the same name in them, like '(mul GPR:$in, GPR:$in)'.
329 class CheckSameMatcher : public Matcher {
330 unsigned MatchNumber;
332 CheckSameMatcher(unsigned matchnumber)
333 : Matcher(CheckSame), MatchNumber(matchnumber) {}
335 unsigned getMatchNumber() const { return MatchNumber; }
337 static inline bool classof(const Matcher *N) {
338 return N->getKind() == CheckSame;
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<CheckSameMatcher>(M)->getMatchNumber() == getMatchNumber();
348 virtual unsigned getHashImpl() const { return getMatchNumber(); }
351 /// CheckPatternPredicateMatcher - This checks the target-specific predicate
352 /// to see if the entire pattern is capable of matching. This predicate does
353 /// not take a node as input. This is used for subtarget feature checks etc.
354 class CheckPatternPredicateMatcher : public Matcher {
355 std::string Predicate;
357 CheckPatternPredicateMatcher(StringRef predicate)
358 : Matcher(CheckPatternPredicate), Predicate(predicate) {}
360 StringRef getPredicate() const { return Predicate; }
362 static inline bool classof(const Matcher *N) {
363 return N->getKind() == CheckPatternPredicate;
366 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
369 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
370 virtual bool isEqualImpl(const Matcher *M) const {
371 return cast<CheckPatternPredicateMatcher>(M)->getPredicate() == Predicate;
373 virtual unsigned getHashImpl() const;
376 /// CheckPredicateMatcher - This checks the target-specific predicate to
377 /// see if the node is acceptable.
378 class CheckPredicateMatcher : public Matcher {
381 CheckPredicateMatcher(StringRef predname)
382 : Matcher(CheckPredicate), PredName(predname) {}
384 StringRef getPredicateName() const { return PredName; }
386 static inline bool classof(const Matcher *N) {
387 return N->getKind() == CheckPredicate;
391 //virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
394 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
395 virtual bool isEqualImpl(const Matcher *M) const {
396 return cast<CheckPredicateMatcher>(M)->PredName == PredName;
398 virtual unsigned getHashImpl() const;
402 /// CheckOpcodeMatcher - This checks to see if the current node has the
403 /// specified opcode, if not it fails to match.
404 class CheckOpcodeMatcher : public Matcher {
405 const SDNodeInfo &Opcode;
407 CheckOpcodeMatcher(const SDNodeInfo &opcode)
408 : Matcher(CheckOpcode), Opcode(opcode) {}
410 const SDNodeInfo &getOpcode() const { return Opcode; }
412 static inline bool classof(const Matcher *N) {
413 return N->getKind() == CheckOpcode;
416 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
419 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
420 virtual bool isEqualImpl(const Matcher *M) const;
421 virtual unsigned getHashImpl() const;
422 virtual bool isContradictoryImpl(const Matcher *M) const;
425 /// SwitchOpcodeMatcher - Switch based on the current node's opcode, dispatching
426 /// to one matcher per opcode. If the opcode doesn't match any of the cases,
427 /// then the match fails. This is semantically equivalent to a Scope node where
428 /// every child does a CheckOpcode, but is much faster.
429 class SwitchOpcodeMatcher : public Matcher {
430 SmallVector<std::pair<const SDNodeInfo*, Matcher*>, 8> Cases;
432 SwitchOpcodeMatcher(const std::pair<const SDNodeInfo*, Matcher*> *cases,
434 : Matcher(SwitchOpcode), Cases(cases, cases+numcases) {}
436 static inline bool classof(const Matcher *N) {
437 return N->getKind() == SwitchOpcode;
440 unsigned getNumCases() const { return Cases.size(); }
442 const SDNodeInfo &getCaseOpcode(unsigned i) const { return *Cases[i].first; }
443 Matcher *getCaseMatcher(unsigned i) { return Cases[i].second; }
444 const Matcher *getCaseMatcher(unsigned i) const { return Cases[i].second; }
447 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
448 virtual bool isEqualImpl(const Matcher *M) const { return false; }
449 virtual unsigned getHashImpl() const { return 4123; }
452 /// CheckMultiOpcodeMatcher - This checks to see if the current node has one
453 /// of the specified opcode, if not it fails to match.
454 class CheckMultiOpcodeMatcher : public Matcher {
455 SmallVector<const SDNodeInfo*, 4> Opcodes;
457 CheckMultiOpcodeMatcher(const SDNodeInfo * const *opcodes, unsigned numops)
458 : Matcher(CheckMultiOpcode), Opcodes(opcodes, opcodes+numops) {}
460 unsigned getNumOpcodes() const { return Opcodes.size(); }
461 const SDNodeInfo &getOpcode(unsigned i) const { return *Opcodes[i]; }
463 static inline bool classof(const Matcher *N) {
464 return N->getKind() == CheckMultiOpcode;
467 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
470 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
471 virtual bool isEqualImpl(const Matcher *M) const {
472 return cast<CheckMultiOpcodeMatcher>(M)->Opcodes == Opcodes;
474 virtual unsigned getHashImpl() const;
479 /// CheckTypeMatcher - This checks to see if the current node has the
480 /// specified type, if not it fails to match.
481 class CheckTypeMatcher : public Matcher {
482 MVT::SimpleValueType Type;
484 CheckTypeMatcher(MVT::SimpleValueType type)
485 : Matcher(CheckType), Type(type) {}
487 MVT::SimpleValueType getType() const { return Type; }
489 static inline bool classof(const Matcher *N) {
490 return N->getKind() == CheckType;
493 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
496 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
497 virtual bool isEqualImpl(const Matcher *M) const {
498 return cast<CheckTypeMatcher>(M)->Type == Type;
500 virtual unsigned getHashImpl() const { return Type; }
501 virtual bool isContradictoryImpl(const Matcher *M) const;
504 /// CheckChildTypeMatcher - This checks to see if a child node has the
505 /// specified type, if not it fails to match.
506 class CheckChildTypeMatcher : public Matcher {
508 MVT::SimpleValueType Type;
510 CheckChildTypeMatcher(unsigned childno, MVT::SimpleValueType type)
511 : Matcher(CheckChildType), ChildNo(childno), Type(type) {}
513 unsigned getChildNo() const { return ChildNo; }
514 MVT::SimpleValueType getType() const { return Type; }
516 static inline bool classof(const Matcher *N) {
517 return N->getKind() == CheckChildType;
520 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
523 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
524 virtual bool isEqualImpl(const Matcher *M) const {
525 return cast<CheckChildTypeMatcher>(M)->ChildNo == ChildNo &&
526 cast<CheckChildTypeMatcher>(M)->Type == Type;
528 virtual unsigned getHashImpl() const { return (Type << 3) | ChildNo; }
529 virtual bool isContradictoryImpl(const Matcher *M) const;
533 /// CheckIntegerMatcher - This checks to see if the current node is a
534 /// ConstantSDNode with the specified integer value, if not it fails to match.
535 class CheckIntegerMatcher : public Matcher {
538 CheckIntegerMatcher(int64_t value)
539 : Matcher(CheckInteger), Value(value) {}
541 int64_t getValue() const { return Value; }
543 static inline bool classof(const Matcher *N) {
544 return N->getKind() == CheckInteger;
547 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
550 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
551 virtual bool isEqualImpl(const Matcher *M) const {
552 return cast<CheckIntegerMatcher>(M)->Value == Value;
554 virtual unsigned getHashImpl() const { return Value; }
555 virtual bool isContradictoryImpl(const Matcher *M) const;
558 /// CheckCondCodeMatcher - This checks to see if the current node is a
559 /// CondCodeSDNode with the specified condition, if not it fails to match.
560 class CheckCondCodeMatcher : public Matcher {
561 StringRef CondCodeName;
563 CheckCondCodeMatcher(StringRef condcodename)
564 : Matcher(CheckCondCode), CondCodeName(condcodename) {}
566 StringRef getCondCodeName() const { return CondCodeName; }
568 static inline bool classof(const Matcher *N) {
569 return N->getKind() == CheckCondCode;
572 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
575 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
576 virtual bool isEqualImpl(const Matcher *M) const {
577 return cast<CheckCondCodeMatcher>(M)->CondCodeName == CondCodeName;
579 virtual unsigned getHashImpl() const;
582 /// CheckValueTypeMatcher - This checks to see if the current node is a
583 /// VTSDNode with the specified type, if not it fails to match.
584 class CheckValueTypeMatcher : public Matcher {
587 CheckValueTypeMatcher(StringRef type_name)
588 : Matcher(CheckValueType), TypeName(type_name) {}
590 StringRef getTypeName() const { return TypeName; }
592 static inline bool classof(const Matcher *N) {
593 return N->getKind() == CheckValueType;
596 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
599 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
600 virtual bool isEqualImpl(const Matcher *M) const {
601 return cast<CheckValueTypeMatcher>(M)->TypeName == TypeName;
603 virtual unsigned getHashImpl() const;
608 /// CheckComplexPatMatcher - This node runs the specified ComplexPattern on
609 /// the current node.
610 class CheckComplexPatMatcher : public Matcher {
611 const ComplexPattern &Pattern;
613 CheckComplexPatMatcher(const ComplexPattern &pattern)
614 : Matcher(CheckComplexPat), Pattern(pattern) {}
616 const ComplexPattern &getPattern() const { return Pattern; }
618 static inline bool classof(const Matcher *N) {
619 return N->getKind() == CheckComplexPat;
622 // Not safe to move a pattern predicate past a complex pattern.
623 virtual bool isSafeToReorderWithPatternPredicate() const { return false; }
626 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
627 virtual bool isEqualImpl(const Matcher *M) const {
628 return &cast<CheckComplexPatMatcher>(M)->Pattern == &Pattern;
630 virtual unsigned getHashImpl() const {
631 return (unsigned)(intptr_t)&Pattern;
635 /// CheckAndImmMatcher - This checks to see if the current node is an 'and'
636 /// with something equivalent to the specified immediate.
637 class CheckAndImmMatcher : public Matcher {
640 CheckAndImmMatcher(int64_t value)
641 : Matcher(CheckAndImm), Value(value) {}
643 int64_t getValue() const { return Value; }
645 static inline bool classof(const Matcher *N) {
646 return N->getKind() == CheckAndImm;
649 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
652 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
653 virtual bool isEqualImpl(const Matcher *M) const {
654 return cast<CheckAndImmMatcher>(M)->Value == Value;
656 virtual unsigned getHashImpl() const { return Value; }
659 /// CheckOrImmMatcher - This checks to see if the current node is an 'and'
660 /// with something equivalent to the specified immediate.
661 class CheckOrImmMatcher : public Matcher {
664 CheckOrImmMatcher(int64_t value)
665 : Matcher(CheckOrImm), Value(value) {}
667 int64_t getValue() const { return Value; }
669 static inline bool classof(const Matcher *N) {
670 return N->getKind() == CheckOrImm;
673 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
676 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
677 virtual bool isEqualImpl(const Matcher *M) const {
678 return cast<CheckOrImmMatcher>(M)->Value == Value;
680 virtual unsigned getHashImpl() const { return Value; }
683 /// CheckFoldableChainNodeMatcher - This checks to see if the current node
684 /// (which defines a chain operand) is safe to fold into a larger pattern.
685 class CheckFoldableChainNodeMatcher : public Matcher {
687 CheckFoldableChainNodeMatcher()
688 : Matcher(CheckFoldableChainNode) {}
690 static inline bool classof(const Matcher *N) {
691 return N->getKind() == CheckFoldableChainNode;
694 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
697 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
698 virtual bool isEqualImpl(const Matcher *M) const { return true; }
699 virtual unsigned getHashImpl() const { return 0; }
702 /// CheckChainCompatibleMatcher - Verify that the current node's chain
703 /// operand is 'compatible' with the specified recorded node's.
704 class CheckChainCompatibleMatcher : public Matcher {
707 CheckChainCompatibleMatcher(unsigned previousop)
708 : Matcher(CheckChainCompatible), PreviousOp(previousop) {}
710 unsigned getPreviousOp() const { return PreviousOp; }
712 static inline bool classof(const Matcher *N) {
713 return N->getKind() == CheckChainCompatible;
716 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
719 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
720 virtual bool isEqualImpl(const Matcher *M) const {
721 return cast<CheckChainCompatibleMatcher>(M)->PreviousOp == PreviousOp;
723 virtual unsigned getHashImpl() const { return PreviousOp; }
726 /// EmitIntegerMatcher - This creates a new TargetConstant.
727 class EmitIntegerMatcher : public Matcher {
729 MVT::SimpleValueType VT;
731 EmitIntegerMatcher(int64_t val, MVT::SimpleValueType vt)
732 : Matcher(EmitInteger), Val(val), VT(vt) {}
734 int64_t getValue() const { return Val; }
735 MVT::SimpleValueType getVT() const { return VT; }
737 static inline bool classof(const Matcher *N) {
738 return N->getKind() == EmitInteger;
742 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
743 virtual bool isEqualImpl(const Matcher *M) const {
744 return cast<EmitIntegerMatcher>(M)->Val == Val &&
745 cast<EmitIntegerMatcher>(M)->VT == VT;
747 virtual unsigned getHashImpl() const { return (Val << 4) | VT; }
750 /// EmitStringIntegerMatcher - A target constant whose value is represented
752 class EmitStringIntegerMatcher : public Matcher {
754 MVT::SimpleValueType VT;
756 EmitStringIntegerMatcher(const std::string &val, MVT::SimpleValueType vt)
757 : Matcher(EmitStringInteger), Val(val), VT(vt) {}
759 const std::string &getValue() const { return Val; }
760 MVT::SimpleValueType getVT() const { return VT; }
762 static inline bool classof(const Matcher *N) {
763 return N->getKind() == EmitStringInteger;
767 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
768 virtual bool isEqualImpl(const Matcher *M) const {
769 return cast<EmitStringIntegerMatcher>(M)->Val == Val &&
770 cast<EmitStringIntegerMatcher>(M)->VT == VT;
772 virtual unsigned getHashImpl() const;
775 /// EmitRegisterMatcher - This creates a new TargetConstant.
776 class EmitRegisterMatcher : public Matcher {
777 /// Reg - The def for the register that we're emitting. If this is null, then
778 /// this is a reference to zero_reg.
780 MVT::SimpleValueType VT;
782 EmitRegisterMatcher(Record *reg, MVT::SimpleValueType vt)
783 : Matcher(EmitRegister), Reg(reg), VT(vt) {}
785 Record *getReg() const { return Reg; }
786 MVT::SimpleValueType getVT() const { return VT; }
788 static inline bool classof(const Matcher *N) {
789 return N->getKind() == EmitRegister;
793 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
794 virtual bool isEqualImpl(const Matcher *M) const {
795 return cast<EmitRegisterMatcher>(M)->Reg == Reg &&
796 cast<EmitRegisterMatcher>(M)->VT == VT;
798 virtual unsigned getHashImpl() const {
799 return ((unsigned)(intptr_t)Reg) << 4 | VT;
803 /// EmitConvertToTargetMatcher - Emit an operation that reads a specified
804 /// recorded node and converts it from being a ISD::Constant to
805 /// ISD::TargetConstant, likewise for ConstantFP.
806 class EmitConvertToTargetMatcher : public Matcher {
809 EmitConvertToTargetMatcher(unsigned slot)
810 : Matcher(EmitConvertToTarget), Slot(slot) {}
812 unsigned getSlot() const { return Slot; }
814 static inline bool classof(const Matcher *N) {
815 return N->getKind() == EmitConvertToTarget;
819 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
820 virtual bool isEqualImpl(const Matcher *M) const {
821 return cast<EmitConvertToTargetMatcher>(M)->Slot == Slot;
823 virtual unsigned getHashImpl() const { return Slot; }
826 /// EmitMergeInputChainsMatcher - Emit a node that merges a list of input
827 /// chains together with a token factor. The list of nodes are the nodes in the
828 /// matched pattern that have chain input/outputs. This node adds all input
829 /// chains of these nodes if they are not themselves a node in the pattern.
830 class EmitMergeInputChainsMatcher : public Matcher {
831 SmallVector<unsigned, 3> ChainNodes;
833 EmitMergeInputChainsMatcher(const unsigned *nodes, unsigned NumNodes)
834 : Matcher(EmitMergeInputChains), ChainNodes(nodes, nodes+NumNodes) {}
836 unsigned getNumNodes() const { return ChainNodes.size(); }
838 unsigned getNode(unsigned i) const {
839 assert(i < ChainNodes.size());
840 return ChainNodes[i];
843 static inline bool classof(const Matcher *N) {
844 return N->getKind() == EmitMergeInputChains;
848 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
849 virtual bool isEqualImpl(const Matcher *M) const {
850 return cast<EmitMergeInputChainsMatcher>(M)->ChainNodes == ChainNodes;
852 virtual unsigned getHashImpl() const;
855 /// EmitCopyToRegMatcher - Emit a CopyToReg node from a value to a physreg,
856 /// pushing the chain and flag results.
858 class EmitCopyToRegMatcher : public Matcher {
859 unsigned SrcSlot; // Value to copy into the physreg.
862 EmitCopyToRegMatcher(unsigned srcSlot, Record *destPhysReg)
863 : Matcher(EmitCopyToReg), SrcSlot(srcSlot), DestPhysReg(destPhysReg) {}
865 unsigned getSrcSlot() const { return SrcSlot; }
866 Record *getDestPhysReg() const { return DestPhysReg; }
868 static inline bool classof(const Matcher *N) {
869 return N->getKind() == EmitCopyToReg;
873 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
874 virtual bool isEqualImpl(const Matcher *M) const {
875 return cast<EmitCopyToRegMatcher>(M)->SrcSlot == SrcSlot &&
876 cast<EmitCopyToRegMatcher>(M)->DestPhysReg == DestPhysReg;
878 virtual unsigned getHashImpl() const {
879 return SrcSlot ^ ((unsigned)(intptr_t)DestPhysReg << 4);
885 /// EmitNodeXFormMatcher - Emit an operation that runs an SDNodeXForm on a
886 /// recorded node and records the result.
887 class EmitNodeXFormMatcher : public Matcher {
891 EmitNodeXFormMatcher(unsigned slot, Record *nodeXForm)
892 : Matcher(EmitNodeXForm), Slot(slot), NodeXForm(nodeXForm) {}
894 unsigned getSlot() const { return Slot; }
895 Record *getNodeXForm() const { return NodeXForm; }
897 static inline bool classof(const Matcher *N) {
898 return N->getKind() == EmitNodeXForm;
902 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
903 virtual bool isEqualImpl(const Matcher *M) const {
904 return cast<EmitNodeXFormMatcher>(M)->Slot == Slot &&
905 cast<EmitNodeXFormMatcher>(M)->NodeXForm == NodeXForm;
907 virtual unsigned getHashImpl() const {
908 return Slot ^ ((unsigned)(intptr_t)NodeXForm << 4);
912 /// EmitNodeMatcherCommon - Common class shared between EmitNode and
914 class EmitNodeMatcherCommon : public Matcher {
915 std::string OpcodeName;
916 const SmallVector<MVT::SimpleValueType, 3> VTs;
917 const SmallVector<unsigned, 6> Operands;
918 bool HasChain, HasInFlag, HasOutFlag, HasMemRefs;
920 /// NumFixedArityOperands - If this is a fixed arity node, this is set to -1.
921 /// If this is a varidic node, this is set to the number of fixed arity
922 /// operands in the root of the pattern. The rest are appended to this node.
923 int NumFixedArityOperands;
925 EmitNodeMatcherCommon(const std::string &opcodeName,
926 const MVT::SimpleValueType *vts, unsigned numvts,
927 const unsigned *operands, unsigned numops,
928 bool hasChain, bool hasInFlag, bool hasOutFlag,
930 int numfixedarityoperands, bool isMorphNodeTo)
931 : Matcher(isMorphNodeTo ? MorphNodeTo : EmitNode), OpcodeName(opcodeName),
932 VTs(vts, vts+numvts), Operands(operands, operands+numops),
933 HasChain(hasChain), HasInFlag(hasInFlag), HasOutFlag(hasOutFlag),
934 HasMemRefs(hasmemrefs), NumFixedArityOperands(numfixedarityoperands) {}
936 const std::string &getOpcodeName() const { return OpcodeName; }
938 unsigned getNumVTs() const { return VTs.size(); }
939 MVT::SimpleValueType getVT(unsigned i) const {
940 assert(i < VTs.size());
944 unsigned getNumOperands() const { return Operands.size(); }
945 unsigned getOperand(unsigned i) const {
946 assert(i < Operands.size());
950 const SmallVectorImpl<MVT::SimpleValueType> &getVTList() const { return VTs; }
951 const SmallVectorImpl<unsigned> &getOperandList() const { return Operands; }
954 bool hasChain() const { return HasChain; }
955 bool hasInFlag() const { return HasInFlag; }
956 bool hasOutFlag() const { return HasOutFlag; }
957 bool hasMemRefs() const { return HasMemRefs; }
958 int getNumFixedArityOperands() const { return NumFixedArityOperands; }
960 static inline bool classof(const Matcher *N) {
961 return N->getKind() == EmitNode || N->getKind() == MorphNodeTo;
965 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
966 virtual bool isEqualImpl(const Matcher *M) const;
967 virtual unsigned getHashImpl() const;
970 /// EmitNodeMatcher - This signals a successful match and generates a node.
971 class EmitNodeMatcher : public EmitNodeMatcherCommon {
972 unsigned FirstResultSlot;
974 EmitNodeMatcher(const std::string &opcodeName,
975 const MVT::SimpleValueType *vts, unsigned numvts,
976 const unsigned *operands, unsigned numops,
977 bool hasChain, bool hasInFlag, bool hasOutFlag,
979 int numfixedarityoperands, unsigned firstresultslot)
980 : EmitNodeMatcherCommon(opcodeName, vts, numvts, operands, numops, hasChain,
981 hasInFlag, hasOutFlag, hasmemrefs,
982 numfixedarityoperands, false),
983 FirstResultSlot(firstresultslot) {}
985 unsigned getFirstResultSlot() const { return FirstResultSlot; }
987 static inline bool classof(const Matcher *N) {
988 return N->getKind() == EmitNode;
993 class MorphNodeToMatcher : public EmitNodeMatcherCommon {
994 const PatternToMatch &Pattern;
996 MorphNodeToMatcher(const std::string &opcodeName,
997 const MVT::SimpleValueType *vts, unsigned numvts,
998 const unsigned *operands, unsigned numops,
999 bool hasChain, bool hasInFlag, bool hasOutFlag,
1001 int numfixedarityoperands, const PatternToMatch &pattern)
1002 : EmitNodeMatcherCommon(opcodeName, vts, numvts, operands, numops, hasChain,
1003 hasInFlag, hasOutFlag, hasmemrefs,
1004 numfixedarityoperands, true),
1008 const PatternToMatch &getPattern() const { return Pattern; }
1010 static inline bool classof(const Matcher *N) {
1011 return N->getKind() == MorphNodeTo;
1015 /// MarkFlagResultsMatcher - This node indicates which non-root nodes in the
1016 /// pattern produce flags. This allows CompleteMatchMatcher to update them
1017 /// with the output flag of the resultant code.
1018 class MarkFlagResultsMatcher : public Matcher {
1019 SmallVector<unsigned, 3> FlagResultNodes;
1021 MarkFlagResultsMatcher(const unsigned *nodes, unsigned NumNodes)
1022 : Matcher(MarkFlagResults), FlagResultNodes(nodes, nodes+NumNodes) {}
1024 unsigned getNumNodes() const { return FlagResultNodes.size(); }
1026 unsigned getNode(unsigned i) const {
1027 assert(i < FlagResultNodes.size());
1028 return FlagResultNodes[i];
1031 static inline bool classof(const Matcher *N) {
1032 return N->getKind() == MarkFlagResults;
1036 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
1037 virtual bool isEqualImpl(const Matcher *M) const {
1038 return cast<MarkFlagResultsMatcher>(M)->FlagResultNodes == FlagResultNodes;
1040 virtual unsigned getHashImpl() const;
1043 /// CompleteMatchMatcher - Complete a match by replacing the results of the
1044 /// pattern with the newly generated nodes. This also prints a comment
1045 /// indicating the source and dest patterns.
1046 class CompleteMatchMatcher : public Matcher {
1047 SmallVector<unsigned, 2> Results;
1048 const PatternToMatch &Pattern;
1050 CompleteMatchMatcher(const unsigned *results, unsigned numresults,
1051 const PatternToMatch &pattern)
1052 : Matcher(CompleteMatch), Results(results, results+numresults),
1055 unsigned getNumResults() const { return Results.size(); }
1056 unsigned getResult(unsigned R) const { return Results[R]; }
1057 const PatternToMatch &getPattern() const { return Pattern; }
1059 static inline bool classof(const Matcher *N) {
1060 return N->getKind() == CompleteMatch;
1064 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
1065 virtual bool isEqualImpl(const Matcher *M) const {
1066 return cast<CompleteMatchMatcher>(M)->Results == Results &&
1067 &cast<CompleteMatchMatcher>(M)->Pattern == &Pattern;
1069 virtual unsigned getHashImpl() const;
1072 } // end namespace llvm