1 //===- DAGISelMatcher.h - Representation of DAG pattern matcher -----------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #ifndef TBLGEN_DAGISELMATCHER_H
11 #define TBLGEN_DAGISELMATCHER_H
12 
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"
18 
19 namespace llvm {
20   struct CodeGenRegister;
21   class CodeGenDAGPatterns;
22   class Matcher;
23   class PatternToMatch;
24   class raw_ostream;
25   class ComplexPattern;
26   class Record;
27   class SDNodeInfo;
28   class TreePredicateFn;
29   class TreePattern;
30 
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,
35                       raw_ostream &OS);
36 
37 
38 /// Matcher - Base class for all the the DAG ISel Matcher representation
39 /// nodes.
40 class Matcher {
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;
44 public:
45   enum KindTy {
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.
54 
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.
66     CheckValueType,
67     CheckComplexPat,
68     CheckAndImm,
69     CheckOrImm,
70     CheckFoldableChainNode,
71 
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.
84   };
85   const KindTy Kind;
86 
87 protected:
Matcher(KindTy K)88   Matcher(KindTy K) : Kind(K) {}
89 public:
~Matcher()90   virtual ~Matcher() {}
91 
getKind()92   KindTy getKind() const { return Kind; }
93 
getNext()94   Matcher *getNext() { return Next.get(); }
getNext()95   const Matcher *getNext() const { return Next.get(); }
setNext(Matcher * C)96   void setNext(Matcher *C) { Next.reset(C); }
takeNext()97   Matcher *takeNext() { return Next.take(); }
98 
getNextPtr()99   OwningPtr<Matcher> &getNextPtr() { return Next; }
100 
classof(const Matcher *)101   static inline bool classof(const Matcher *) { return true; }
102 
isEqual(const Matcher * M)103   bool isEqual(const Matcher *M) const {
104     if (getKind() != M->getKind()) return false;
105     return isEqualImpl(M);
106   }
107 
getHash()108   unsigned getHash() const {
109     // Clear the high bit so we don't conflict with tombstones etc.
110     return ((getHashImpl() << 4) ^ getKind()) & (~0U>>1);
111   }
112 
113   /// isSafeToReorderWithPatternPredicate - Return true if it is safe to sink a
114   /// PatternPredicate node past this one.
isSafeToReorderWithPatternPredicate()115   virtual bool isSafeToReorderWithPatternPredicate() const {
116     return false;
117   }
118 
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.
isSimplePredicateNode()122   bool isSimplePredicateNode() const {
123     switch (getKind()) {
124     default: return false;
125     case CheckSame:
126     case CheckPatternPredicate:
127     case CheckPredicate:
128     case CheckOpcode:
129     case CheckType:
130     case CheckChildType:
131     case CheckInteger:
132     case CheckCondCode:
133     case CheckValueType:
134     case CheckAndImm:
135     case CheckOrImm:
136     case CheckFoldableChainNode:
137       return true;
138     }
139   }
140 
141   /// isSimplePredicateOrRecordNode - Return true if this is a record node or
142   /// a simple predicate.
isSimplePredicateOrRecordNode()143   bool isSimplePredicateOrRecordNode() const {
144     return isSimplePredicateNode() ||
145            getKind() == RecordNode || getKind() == RecordChild;
146   }
147 
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);
152 
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;
157 
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;
161 
162   /// isContradictory - Return true of these two matchers could never match on
163   /// the same node.
isContradictory(const Matcher * Other)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);
172   }
173 
174   void print(raw_ostream &OS, unsigned indent = 0) const;
175   void printOne(raw_ostream &OS) const;
176   void dump() const;
177 protected:
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;
isContradictoryImpl(const Matcher * M)181   virtual bool isContradictoryImpl(const Matcher *M) const { return false; }
182 };
183 
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;
189 public:
ScopeMatcher(Matcher * const * children,unsigned numchildren)190   ScopeMatcher(Matcher *const *children, unsigned numchildren)
191     : Matcher(Scope), Children(children, children+numchildren) {
192   }
193   virtual ~ScopeMatcher();
194 
getNumChildren()195   unsigned getNumChildren() const { return Children.size(); }
196 
getChild(unsigned i)197   Matcher *getChild(unsigned i) { return Children[i]; }
getChild(unsigned i)198   const Matcher *getChild(unsigned i) const { return Children[i]; }
199 
resetChild(unsigned i,Matcher * N)200   void resetChild(unsigned i, Matcher *N) {
201     delete Children[i];
202     Children[i] = N;
203   }
204 
takeChild(unsigned i)205   Matcher *takeChild(unsigned i) {
206     Matcher *Res = Children[i];
207     Children[i] = 0;
208     return Res;
209   }
210 
setNumChildren(unsigned NC)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)
215         delete Children[i];
216     }
217     Children.resize(NC);
218   }
219 
classof(const Matcher * N)220   static inline bool classof(const Matcher *N) {
221     return N->getKind() == Scope;
222   }
223 
224 private:
225   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
isEqualImpl(const Matcher * M)226   virtual bool isEqualImpl(const Matcher *M) const { return false; }
getHashImpl()227   virtual unsigned getHashImpl() const { return 12312; }
228 };
229 
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.
234   std::string WhatFor;
235 
236   /// ResultNo - The slot number in the RecordedNodes vector that this will be,
237   /// just printed as a comment.
238   unsigned ResultNo;
239 public:
RecordMatcher(const std::string & whatfor,unsigned resultNo)240   RecordMatcher(const std::string &whatfor, unsigned resultNo)
241     : Matcher(RecordNode), WhatFor(whatfor), ResultNo(resultNo) {}
242 
getWhatFor()243   const std::string &getWhatFor() const { return WhatFor; }
getResultNo()244   unsigned getResultNo() const { return ResultNo; }
245 
classof(const Matcher * N)246   static inline bool classof(const Matcher *N) {
247     return N->getKind() == RecordNode;
248   }
249 
isSafeToReorderWithPatternPredicate()250   virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
251 private:
252   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
isEqualImpl(const Matcher * M)253   virtual bool isEqualImpl(const Matcher *M) const { return true; }
getHashImpl()254   virtual unsigned getHashImpl() const { return 0; }
255 };
256 
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 {
261   unsigned ChildNo;
262 
263   /// WhatFor - This is a string indicating why we're recording this.  This
264   /// should only be used for comment generation not anything semantic.
265   std::string WhatFor;
266 
267   /// ResultNo - The slot number in the RecordedNodes vector that this will be,
268   /// just printed as a comment.
269   unsigned ResultNo;
270 public:
RecordChildMatcher(unsigned childno,const std::string & whatfor,unsigned resultNo)271   RecordChildMatcher(unsigned childno, const std::string &whatfor,
272                      unsigned resultNo)
273   : Matcher(RecordChild), ChildNo(childno), WhatFor(whatfor),
274     ResultNo(resultNo) {}
275 
getChildNo()276   unsigned getChildNo() const { return ChildNo; }
getWhatFor()277   const std::string &getWhatFor() const { return WhatFor; }
getResultNo()278   unsigned getResultNo() const { return ResultNo; }
279 
classof(const Matcher * N)280   static inline bool classof(const Matcher *N) {
281     return N->getKind() == RecordChild;
282   }
283 
isSafeToReorderWithPatternPredicate()284   virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
285 
286 private:
287   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
isEqualImpl(const Matcher * M)288   virtual bool isEqualImpl(const Matcher *M) const {
289     return cast<RecordChildMatcher>(M)->getChildNo() == getChildNo();
290   }
getHashImpl()291   virtual unsigned getHashImpl() const { return getChildNo(); }
292 };
293 
294 /// RecordMemRefMatcher - Save the current node's memref.
295 class RecordMemRefMatcher : public Matcher {
296 public:
RecordMemRefMatcher()297   RecordMemRefMatcher() : Matcher(RecordMemRef) {}
298 
classof(const Matcher * N)299   static inline bool classof(const Matcher *N) {
300     return N->getKind() == RecordMemRef;
301   }
302 
isSafeToReorderWithPatternPredicate()303   virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
304 
305 private:
306   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
isEqualImpl(const Matcher * M)307   virtual bool isEqualImpl(const Matcher *M) const { return true; }
getHashImpl()308   virtual unsigned getHashImpl() const { return 0; }
309 };
310 
311 
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 {
315 public:
CaptureGlueInputMatcher()316   CaptureGlueInputMatcher() : Matcher(CaptureGlueInput) {}
317 
classof(const Matcher * N)318   static inline bool classof(const Matcher *N) {
319     return N->getKind() == CaptureGlueInput;
320   }
321 
isSafeToReorderWithPatternPredicate()322   virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
323 
324 private:
325   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
isEqualImpl(const Matcher * M)326   virtual bool isEqualImpl(const Matcher *M) const { return true; }
getHashImpl()327   virtual unsigned getHashImpl() const { return 0; }
328 };
329 
330 /// MoveChildMatcher - This tells the interpreter to move into the
331 /// specified child node.
332 class MoveChildMatcher : public Matcher {
333   unsigned ChildNo;
334 public:
MoveChildMatcher(unsigned childNo)335   MoveChildMatcher(unsigned childNo) : Matcher(MoveChild), ChildNo(childNo) {}
336 
getChildNo()337   unsigned getChildNo() const { return ChildNo; }
338 
classof(const Matcher * N)339   static inline bool classof(const Matcher *N) {
340     return N->getKind() == MoveChild;
341   }
342 
isSafeToReorderWithPatternPredicate()343   virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
344 
345 private:
346   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
isEqualImpl(const Matcher * M)347   virtual bool isEqualImpl(const Matcher *M) const {
348     return cast<MoveChildMatcher>(M)->getChildNo() == getChildNo();
349   }
getHashImpl()350   virtual unsigned getHashImpl() const { return getChildNo(); }
351 };
352 
353 /// MoveParentMatcher - This tells the interpreter to move to the parent
354 /// of the current node.
355 class MoveParentMatcher : public Matcher {
356 public:
MoveParentMatcher()357   MoveParentMatcher() : Matcher(MoveParent) {}
358 
classof(const Matcher * N)359   static inline bool classof(const Matcher *N) {
360     return N->getKind() == MoveParent;
361   }
362 
isSafeToReorderWithPatternPredicate()363   virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
364 
365 private:
366   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
isEqualImpl(const Matcher * M)367   virtual bool isEqualImpl(const Matcher *M) const { return true; }
getHashImpl()368   virtual unsigned getHashImpl() const { return 0; }
369 };
370 
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;
376 public:
CheckSameMatcher(unsigned matchnumber)377   CheckSameMatcher(unsigned matchnumber)
378     : Matcher(CheckSame), MatchNumber(matchnumber) {}
379 
getMatchNumber()380   unsigned getMatchNumber() const { return MatchNumber; }
381 
classof(const Matcher * N)382   static inline bool classof(const Matcher *N) {
383     return N->getKind() == CheckSame;
384   }
385 
isSafeToReorderWithPatternPredicate()386   virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
387 
388 private:
389   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
isEqualImpl(const Matcher * M)390   virtual bool isEqualImpl(const Matcher *M) const {
391     return cast<CheckSameMatcher>(M)->getMatchNumber() == getMatchNumber();
392   }
getHashImpl()393   virtual unsigned getHashImpl() const { return getMatchNumber(); }
394 };
395 
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;
401 public:
CheckPatternPredicateMatcher(StringRef predicate)402   CheckPatternPredicateMatcher(StringRef predicate)
403     : Matcher(CheckPatternPredicate), Predicate(predicate) {}
404 
getPredicate()405   StringRef getPredicate() const { return Predicate; }
406 
classof(const Matcher * N)407   static inline bool classof(const Matcher *N) {
408     return N->getKind() == CheckPatternPredicate;
409   }
410 
isSafeToReorderWithPatternPredicate()411   virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
412 
413 private:
414   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
isEqualImpl(const Matcher * M)415   virtual bool isEqualImpl(const Matcher *M) const {
416     return cast<CheckPatternPredicateMatcher>(M)->getPredicate() == Predicate;
417   }
418   virtual unsigned getHashImpl() const;
419 };
420 
421 /// CheckPredicateMatcher - This checks the target-specific predicate to
422 /// see if the node is acceptable.
423 class CheckPredicateMatcher : public Matcher {
424   TreePattern *Pred;
425 public:
426   CheckPredicateMatcher(const TreePredicateFn &pred);
427 
428   TreePredicateFn getPredicate() const;
429 
classof(const Matcher * N)430   static inline bool classof(const Matcher *N) {
431     return N->getKind() == CheckPredicate;
432   }
433 
434   // TODO: Ok?
435   //virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
436 
437 private:
438   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
isEqualImpl(const Matcher * M)439   virtual bool isEqualImpl(const Matcher *M) const {
440     return cast<CheckPredicateMatcher>(M)->Pred == Pred;
441   }
442   virtual unsigned getHashImpl() const;
443 };
444 
445 
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;
450 public:
CheckOpcodeMatcher(const SDNodeInfo & opcode)451   CheckOpcodeMatcher(const SDNodeInfo &opcode)
452     : Matcher(CheckOpcode), Opcode(opcode) {}
453 
getOpcode()454   const SDNodeInfo &getOpcode() const { return Opcode; }
455 
classof(const Matcher * N)456   static inline bool classof(const Matcher *N) {
457     return N->getKind() == CheckOpcode;
458   }
459 
isSafeToReorderWithPatternPredicate()460   virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
461 
462 private:
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;
467 };
468 
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;
475 public:
SwitchOpcodeMatcher(const std::pair<const SDNodeInfo *,Matcher * > * cases,unsigned numcases)476   SwitchOpcodeMatcher(const std::pair<const SDNodeInfo*, Matcher*> *cases,
477                       unsigned numcases)
478     : Matcher(SwitchOpcode), Cases(cases, cases+numcases) {}
479 
classof(const Matcher * N)480   static inline bool classof(const Matcher *N) {
481     return N->getKind() == SwitchOpcode;
482   }
483 
getNumCases()484   unsigned getNumCases() const { return Cases.size(); }
485 
getCaseOpcode(unsigned i)486   const SDNodeInfo &getCaseOpcode(unsigned i) const { return *Cases[i].first; }
getCaseMatcher(unsigned i)487   Matcher *getCaseMatcher(unsigned i) { return Cases[i].second; }
getCaseMatcher(unsigned i)488   const Matcher *getCaseMatcher(unsigned i) const { return Cases[i].second; }
489 
490 private:
491   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
isEqualImpl(const Matcher * M)492   virtual bool isEqualImpl(const Matcher *M) const { return false; }
getHashImpl()493   virtual unsigned getHashImpl() const { return 4123; }
494 };
495 
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;
500   unsigned ResNo;
501 public:
CheckTypeMatcher(MVT::SimpleValueType type,unsigned resno)502   CheckTypeMatcher(MVT::SimpleValueType type, unsigned resno)
503     : Matcher(CheckType), Type(type), ResNo(resno) {}
504 
getType()505   MVT::SimpleValueType getType() const { return Type; }
getResNo()506   unsigned getResNo() const { return ResNo; }
507 
classof(const Matcher * N)508   static inline bool classof(const Matcher *N) {
509     return N->getKind() == CheckType;
510   }
511 
isSafeToReorderWithPatternPredicate()512   virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
513 
514 private:
515   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
isEqualImpl(const Matcher * M)516   virtual bool isEqualImpl(const Matcher *M) const {
517     return cast<CheckTypeMatcher>(M)->Type == Type;
518   }
getHashImpl()519   virtual unsigned getHashImpl() const { return Type; }
520   virtual bool isContradictoryImpl(const Matcher *M) const;
521 };
522 
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;
529 public:
SwitchTypeMatcher(const std::pair<MVT::SimpleValueType,Matcher * > * cases,unsigned numcases)530   SwitchTypeMatcher(const std::pair<MVT::SimpleValueType, Matcher*> *cases,
531                     unsigned numcases)
532   : Matcher(SwitchType), Cases(cases, cases+numcases) {}
533 
classof(const Matcher * N)534   static inline bool classof(const Matcher *N) {
535     return N->getKind() == SwitchType;
536   }
537 
getNumCases()538   unsigned getNumCases() const { return Cases.size(); }
539 
getCaseType(unsigned i)540   MVT::SimpleValueType getCaseType(unsigned i) const { return Cases[i].first; }
getCaseMatcher(unsigned i)541   Matcher *getCaseMatcher(unsigned i) { return Cases[i].second; }
getCaseMatcher(unsigned i)542   const Matcher *getCaseMatcher(unsigned i) const { return Cases[i].second; }
543 
544 private:
545   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
isEqualImpl(const Matcher * M)546   virtual bool isEqualImpl(const Matcher *M) const { return false; }
getHashImpl()547   virtual unsigned getHashImpl() const { return 4123; }
548 };
549 
550 
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 {
554   unsigned ChildNo;
555   MVT::SimpleValueType Type;
556 public:
CheckChildTypeMatcher(unsigned childno,MVT::SimpleValueType type)557   CheckChildTypeMatcher(unsigned childno, MVT::SimpleValueType type)
558     : Matcher(CheckChildType), ChildNo(childno), Type(type) {}
559 
getChildNo()560   unsigned getChildNo() const { return ChildNo; }
getType()561   MVT::SimpleValueType getType() const { return Type; }
562 
classof(const Matcher * N)563   static inline bool classof(const Matcher *N) {
564     return N->getKind() == CheckChildType;
565   }
566 
isSafeToReorderWithPatternPredicate()567   virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
568 
569 private:
570   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
isEqualImpl(const Matcher * M)571   virtual bool isEqualImpl(const Matcher *M) const {
572     return cast<CheckChildTypeMatcher>(M)->ChildNo == ChildNo &&
573            cast<CheckChildTypeMatcher>(M)->Type == Type;
574   }
getHashImpl()575   virtual unsigned getHashImpl() const { return (Type << 3) | ChildNo; }
576   virtual bool isContradictoryImpl(const Matcher *M) const;
577 };
578 
579 
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 {
583   int64_t Value;
584 public:
CheckIntegerMatcher(int64_t value)585   CheckIntegerMatcher(int64_t value)
586     : Matcher(CheckInteger), Value(value) {}
587 
getValue()588   int64_t getValue() const { return Value; }
589 
classof(const Matcher * N)590   static inline bool classof(const Matcher *N) {
591     return N->getKind() == CheckInteger;
592   }
593 
isSafeToReorderWithPatternPredicate()594   virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
595 
596 private:
597   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
isEqualImpl(const Matcher * M)598   virtual bool isEqualImpl(const Matcher *M) const {
599     return cast<CheckIntegerMatcher>(M)->Value == Value;
600   }
getHashImpl()601   virtual unsigned getHashImpl() const { return Value; }
602   virtual bool isContradictoryImpl(const Matcher *M) const;
603 };
604 
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;
609 public:
CheckCondCodeMatcher(StringRef condcodename)610   CheckCondCodeMatcher(StringRef condcodename)
611     : Matcher(CheckCondCode), CondCodeName(condcodename) {}
612 
getCondCodeName()613   StringRef getCondCodeName() const { return CondCodeName; }
614 
classof(const Matcher * N)615   static inline bool classof(const Matcher *N) {
616     return N->getKind() == CheckCondCode;
617   }
618 
isSafeToReorderWithPatternPredicate()619   virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
620 
621 private:
622   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
isEqualImpl(const Matcher * M)623   virtual bool isEqualImpl(const Matcher *M) const {
624     return cast<CheckCondCodeMatcher>(M)->CondCodeName == CondCodeName;
625   }
626   virtual unsigned getHashImpl() const;
627 };
628 
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 {
632   StringRef TypeName;
633 public:
CheckValueTypeMatcher(StringRef type_name)634   CheckValueTypeMatcher(StringRef type_name)
635     : Matcher(CheckValueType), TypeName(type_name) {}
636 
getTypeName()637   StringRef getTypeName() const { return TypeName; }
638 
classof(const Matcher * N)639   static inline bool classof(const Matcher *N) {
640     return N->getKind() == CheckValueType;
641   }
642 
isSafeToReorderWithPatternPredicate()643   virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
644 
645 private:
646   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
isEqualImpl(const Matcher * M)647   virtual bool isEqualImpl(const Matcher *M) const {
648     return cast<CheckValueTypeMatcher>(M)->TypeName == TypeName;
649   }
650   virtual unsigned getHashImpl() const;
651   bool isContradictoryImpl(const Matcher *M) const;
652 };
653 
654 
655 
656 /// CheckComplexPatMatcher - This node runs the specified ComplexPattern on
657 /// the current node.
658 class CheckComplexPatMatcher : public Matcher {
659   const ComplexPattern &Pattern;
660 
661   /// MatchNumber - This is the recorded nodes slot that contains the node we
662   /// want to match against.
663   unsigned MatchNumber;
664 
665   /// Name - The name of the node we're matching, for comment emission.
666   std::string Name;
667 
668   /// FirstResult - This is the first slot in the RecordedNodes list that the
669   /// result of the match populates.
670   unsigned FirstResult;
671 public:
CheckComplexPatMatcher(const ComplexPattern & pattern,unsigned matchnumber,const std::string & name,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) {}
676 
getPattern()677   const ComplexPattern &getPattern() const { return Pattern; }
getMatchNumber()678   unsigned getMatchNumber() const { return MatchNumber; }
679 
getName()680   const std::string getName() const { return Name; }
getFirstResult()681   unsigned getFirstResult() const { return FirstResult; }
682 
classof(const Matcher * N)683   static inline bool classof(const Matcher *N) {
684     return N->getKind() == CheckComplexPat;
685   }
686 
687   // Not safe to move a pattern predicate past a complex pattern.
isSafeToReorderWithPatternPredicate()688   virtual bool isSafeToReorderWithPatternPredicate() const { return false; }
689 
690 private:
691   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
isEqualImpl(const Matcher * M)692   virtual bool isEqualImpl(const Matcher *M) const {
693     return &cast<CheckComplexPatMatcher>(M)->Pattern == &Pattern &&
694            cast<CheckComplexPatMatcher>(M)->MatchNumber == MatchNumber;
695   }
getHashImpl()696   virtual unsigned getHashImpl() const {
697     return (unsigned)(intptr_t)&Pattern ^ MatchNumber;
698   }
699 };
700 
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 {
704   int64_t Value;
705 public:
CheckAndImmMatcher(int64_t value)706   CheckAndImmMatcher(int64_t value)
707     : Matcher(CheckAndImm), Value(value) {}
708 
getValue()709   int64_t getValue() const { return Value; }
710 
classof(const Matcher * N)711   static inline bool classof(const Matcher *N) {
712     return N->getKind() == CheckAndImm;
713   }
714 
isSafeToReorderWithPatternPredicate()715   virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
716 
717 private:
718   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
isEqualImpl(const Matcher * M)719   virtual bool isEqualImpl(const Matcher *M) const {
720     return cast<CheckAndImmMatcher>(M)->Value == Value;
721   }
getHashImpl()722   virtual unsigned getHashImpl() const { return Value; }
723 };
724 
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 {
728   int64_t Value;
729 public:
CheckOrImmMatcher(int64_t value)730   CheckOrImmMatcher(int64_t value)
731     : Matcher(CheckOrImm), Value(value) {}
732 
getValue()733   int64_t getValue() const { return Value; }
734 
classof(const Matcher * N)735   static inline bool classof(const Matcher *N) {
736     return N->getKind() == CheckOrImm;
737   }
738 
isSafeToReorderWithPatternPredicate()739   virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
740 
741 private:
742   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
isEqualImpl(const Matcher * M)743   virtual bool isEqualImpl(const Matcher *M) const {
744     return cast<CheckOrImmMatcher>(M)->Value == Value;
745   }
getHashImpl()746   virtual unsigned getHashImpl() const { return Value; }
747 };
748 
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 {
752 public:
CheckFoldableChainNodeMatcher()753   CheckFoldableChainNodeMatcher()
754     : Matcher(CheckFoldableChainNode) {}
755 
classof(const Matcher * N)756   static inline bool classof(const Matcher *N) {
757     return N->getKind() == CheckFoldableChainNode;
758   }
759 
isSafeToReorderWithPatternPredicate()760   virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
761 
762 private:
763   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
isEqualImpl(const Matcher * M)764   virtual bool isEqualImpl(const Matcher *M) const { return true; }
getHashImpl()765   virtual unsigned getHashImpl() const { return 0; }
766 };
767 
768 /// EmitIntegerMatcher - This creates a new TargetConstant.
769 class EmitIntegerMatcher : public Matcher {
770   int64_t Val;
771   MVT::SimpleValueType VT;
772 public:
EmitIntegerMatcher(int64_t val,MVT::SimpleValueType vt)773   EmitIntegerMatcher(int64_t val, MVT::SimpleValueType vt)
774     : Matcher(EmitInteger), Val(val), VT(vt) {}
775 
getValue()776   int64_t getValue() const { return Val; }
getVT()777   MVT::SimpleValueType getVT() const { return VT; }
778 
classof(const Matcher * N)779   static inline bool classof(const Matcher *N) {
780     return N->getKind() == EmitInteger;
781   }
782 
783 private:
784   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
isEqualImpl(const Matcher * M)785   virtual bool isEqualImpl(const Matcher *M) const {
786     return cast<EmitIntegerMatcher>(M)->Val == Val &&
787            cast<EmitIntegerMatcher>(M)->VT == VT;
788   }
getHashImpl()789   virtual unsigned getHashImpl() const { return (Val << 4) | VT; }
790 };
791 
792 /// EmitStringIntegerMatcher - A target constant whose value is represented
793 /// by a string.
794 class EmitStringIntegerMatcher : public Matcher {
795   std::string Val;
796   MVT::SimpleValueType VT;
797 public:
EmitStringIntegerMatcher(const std::string & val,MVT::SimpleValueType vt)798   EmitStringIntegerMatcher(const std::string &val, MVT::SimpleValueType vt)
799     : Matcher(EmitStringInteger), Val(val), VT(vt) {}
800 
getValue()801   const std::string &getValue() const { return Val; }
getVT()802   MVT::SimpleValueType getVT() const { return VT; }
803 
classof(const Matcher * N)804   static inline bool classof(const Matcher *N) {
805     return N->getKind() == EmitStringInteger;
806   }
807 
808 private:
809   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
isEqualImpl(const Matcher * M)810   virtual bool isEqualImpl(const Matcher *M) const {
811     return cast<EmitStringIntegerMatcher>(M)->Val == Val &&
812            cast<EmitStringIntegerMatcher>(M)->VT == VT;
813   }
814   virtual unsigned getHashImpl() const;
815 };
816 
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;
823 public:
EmitRegisterMatcher(const CodeGenRegister * reg,MVT::SimpleValueType vt)824   EmitRegisterMatcher(const CodeGenRegister *reg, MVT::SimpleValueType vt)
825     : Matcher(EmitRegister), Reg(reg), VT(vt) {}
826 
getReg()827   const CodeGenRegister *getReg() const { return Reg; }
getVT()828   MVT::SimpleValueType getVT() const { return VT; }
829 
classof(const Matcher * N)830   static inline bool classof(const Matcher *N) {
831     return N->getKind() == EmitRegister;
832   }
833 
834 private:
835   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
isEqualImpl(const Matcher * M)836   virtual bool isEqualImpl(const Matcher *M) const {
837     return cast<EmitRegisterMatcher>(M)->Reg == Reg &&
838            cast<EmitRegisterMatcher>(M)->VT == VT;
839   }
getHashImpl()840   virtual unsigned getHashImpl() const {
841     return ((unsigned)(intptr_t)Reg) << 4 | VT;
842   }
843 };
844 
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 {
849   unsigned Slot;
850 public:
EmitConvertToTargetMatcher(unsigned slot)851   EmitConvertToTargetMatcher(unsigned slot)
852     : Matcher(EmitConvertToTarget), Slot(slot) {}
853 
getSlot()854   unsigned getSlot() const { return Slot; }
855 
classof(const Matcher * N)856   static inline bool classof(const Matcher *N) {
857     return N->getKind() == EmitConvertToTarget;
858   }
859 
860 private:
861   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
isEqualImpl(const Matcher * M)862   virtual bool isEqualImpl(const Matcher *M) const {
863     return cast<EmitConvertToTargetMatcher>(M)->Slot == Slot;
864   }
getHashImpl()865   virtual unsigned getHashImpl() const { return Slot; }
866 };
867 
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;
874 public:
EmitMergeInputChainsMatcher(const unsigned * nodes,unsigned NumNodes)875   EmitMergeInputChainsMatcher(const unsigned *nodes, unsigned NumNodes)
876     : Matcher(EmitMergeInputChains), ChainNodes(nodes, nodes+NumNodes) {}
877 
getNumNodes()878   unsigned getNumNodes() const { return ChainNodes.size(); }
879 
getNode(unsigned i)880   unsigned getNode(unsigned i) const {
881     assert(i < ChainNodes.size());
882     return ChainNodes[i];
883   }
884 
classof(const Matcher * N)885   static inline bool classof(const Matcher *N) {
886     return N->getKind() == EmitMergeInputChains;
887   }
888 
889 private:
890   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
isEqualImpl(const Matcher * M)891   virtual bool isEqualImpl(const Matcher *M) const {
892     return cast<EmitMergeInputChainsMatcher>(M)->ChainNodes == ChainNodes;
893   }
894   virtual unsigned getHashImpl() const;
895 };
896 
897 /// EmitCopyToRegMatcher - Emit a CopyToReg node from a value to a physreg,
898 /// pushing the chain and glue results.
899 ///
900 class EmitCopyToRegMatcher : public Matcher {
901   unsigned SrcSlot; // Value to copy into the physreg.
902   Record *DestPhysReg;
903 public:
EmitCopyToRegMatcher(unsigned srcSlot,Record * destPhysReg)904   EmitCopyToRegMatcher(unsigned srcSlot, Record *destPhysReg)
905     : Matcher(EmitCopyToReg), SrcSlot(srcSlot), DestPhysReg(destPhysReg) {}
906 
getSrcSlot()907   unsigned getSrcSlot() const { return SrcSlot; }
getDestPhysReg()908   Record *getDestPhysReg() const { return DestPhysReg; }
909 
classof(const Matcher * N)910   static inline bool classof(const Matcher *N) {
911     return N->getKind() == EmitCopyToReg;
912   }
913 
914 private:
915   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
isEqualImpl(const Matcher * M)916   virtual bool isEqualImpl(const Matcher *M) const {
917     return cast<EmitCopyToRegMatcher>(M)->SrcSlot == SrcSlot &&
918            cast<EmitCopyToRegMatcher>(M)->DestPhysReg == DestPhysReg;
919   }
getHashImpl()920   virtual unsigned getHashImpl() const {
921     return SrcSlot ^ ((unsigned)(intptr_t)DestPhysReg << 4);
922   }
923 };
924 
925 
926 
927 /// EmitNodeXFormMatcher - Emit an operation that runs an SDNodeXForm on a
928 /// recorded node and records the result.
929 class EmitNodeXFormMatcher : public Matcher {
930   unsigned Slot;
931   Record *NodeXForm;
932 public:
EmitNodeXFormMatcher(unsigned slot,Record * nodeXForm)933   EmitNodeXFormMatcher(unsigned slot, Record *nodeXForm)
934     : Matcher(EmitNodeXForm), Slot(slot), NodeXForm(nodeXForm) {}
935 
getSlot()936   unsigned getSlot() const { return Slot; }
getNodeXForm()937   Record *getNodeXForm() const { return NodeXForm; }
938 
classof(const Matcher * N)939   static inline bool classof(const Matcher *N) {
940     return N->getKind() == EmitNodeXForm;
941   }
942 
943 private:
944   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
isEqualImpl(const Matcher * M)945   virtual bool isEqualImpl(const Matcher *M) const {
946     return cast<EmitNodeXFormMatcher>(M)->Slot == Slot &&
947            cast<EmitNodeXFormMatcher>(M)->NodeXForm == NodeXForm;
948   }
getHashImpl()949   virtual unsigned getHashImpl() const {
950     return Slot ^ ((unsigned)(intptr_t)NodeXForm << 4);
951   }
952 };
953 
954 /// EmitNodeMatcherCommon - Common class shared between EmitNode and
955 /// MorphNodeTo.
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;
961 
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;
966 public:
EmitNodeMatcherCommon(const std::string & opcodeName,const MVT::SimpleValueType * vts,unsigned numvts,const unsigned * operands,unsigned numops,bool hasChain,bool hasInGlue,bool hasOutGlue,bool hasmemrefs,int numfixedarityoperands,bool isMorphNodeTo)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,
971                         bool hasmemrefs,
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) {}
977 
getOpcodeName()978   const std::string &getOpcodeName() const { return OpcodeName; }
979 
getNumVTs()980   unsigned getNumVTs() const { return VTs.size(); }
getVT(unsigned i)981   MVT::SimpleValueType getVT(unsigned i) const {
982     assert(i < VTs.size());
983     return VTs[i];
984   }
985 
getNumOperands()986   unsigned getNumOperands() const { return Operands.size(); }
getOperand(unsigned i)987   unsigned getOperand(unsigned i) const {
988     assert(i < Operands.size());
989     return Operands[i];
990   }
991 
getVTList()992   const SmallVectorImpl<MVT::SimpleValueType> &getVTList() const { return VTs; }
getOperandList()993   const SmallVectorImpl<unsigned> &getOperandList() const { return Operands; }
994 
995 
hasChain()996   bool hasChain() const { return HasChain; }
hasInFlag()997   bool hasInFlag() const { return HasInGlue; }
hasOutFlag()998   bool hasOutFlag() const { return HasOutGlue; }
hasMemRefs()999   bool hasMemRefs() const { return HasMemRefs; }
getNumFixedArityOperands()1000   int getNumFixedArityOperands() const { return NumFixedArityOperands; }
1001 
classof(const Matcher * N)1002   static inline bool classof(const Matcher *N) {
1003     return N->getKind() == EmitNode || N->getKind() == MorphNodeTo;
1004   }
1005 
1006 private:
1007   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
1008   virtual bool isEqualImpl(const Matcher *M) const;
1009   virtual unsigned getHashImpl() const;
1010 };
1011 
1012 /// EmitNodeMatcher - This signals a successful match and generates a node.
1013 class EmitNodeMatcher : public EmitNodeMatcherCommon {
1014   unsigned FirstResultSlot;
1015 public:
EmitNodeMatcher(const std::string & opcodeName,const MVT::SimpleValueType * vts,unsigned numvts,const unsigned * operands,unsigned numops,bool hasChain,bool hasInFlag,bool hasOutFlag,bool hasmemrefs,int numfixedarityoperands,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,
1020                   bool hasmemrefs,
1021                   int numfixedarityoperands, unsigned firstresultslot)
1022   : EmitNodeMatcherCommon(opcodeName, vts, numvts, operands, numops, hasChain,
1023                           hasInFlag, hasOutFlag, hasmemrefs,
1024                           numfixedarityoperands, false),
1025     FirstResultSlot(firstresultslot) {}
1026 
getFirstResultSlot()1027   unsigned getFirstResultSlot() const { return FirstResultSlot; }
1028 
classof(const Matcher * N)1029   static inline bool classof(const Matcher *N) {
1030     return N->getKind() == EmitNode;
1031   }
1032 
1033 };
1034 
1035 class MorphNodeToMatcher : public EmitNodeMatcherCommon {
1036   const PatternToMatch &Pattern;
1037 public:
MorphNodeToMatcher(const std::string & opcodeName,const MVT::SimpleValueType * vts,unsigned numvts,const unsigned * operands,unsigned numops,bool hasChain,bool hasInFlag,bool hasOutFlag,bool hasmemrefs,int numfixedarityoperands,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,
1042                      bool hasmemrefs,
1043                      int numfixedarityoperands, const PatternToMatch &pattern)
1044     : EmitNodeMatcherCommon(opcodeName, vts, numvts, operands, numops, hasChain,
1045                             hasInFlag, hasOutFlag, hasmemrefs,
1046                             numfixedarityoperands, true),
1047       Pattern(pattern) {
1048   }
1049 
getPattern()1050   const PatternToMatch &getPattern() const { return Pattern; }
1051 
classof(const Matcher * N)1052   static inline bool classof(const Matcher *N) {
1053     return N->getKind() == MorphNodeTo;
1054   }
1055 };
1056 
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;
1062 public:
MarkGlueResultsMatcher(const unsigned * nodes,unsigned NumNodes)1063   MarkGlueResultsMatcher(const unsigned *nodes, unsigned NumNodes)
1064     : Matcher(MarkGlueResults), GlueResultNodes(nodes, nodes+NumNodes) {}
1065 
getNumNodes()1066   unsigned getNumNodes() const { return GlueResultNodes.size(); }
1067 
getNode(unsigned i)1068   unsigned getNode(unsigned i) const {
1069     assert(i < GlueResultNodes.size());
1070     return GlueResultNodes[i];
1071   }
1072 
classof(const Matcher * N)1073   static inline bool classof(const Matcher *N) {
1074     return N->getKind() == MarkGlueResults;
1075   }
1076 
1077 private:
1078   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
isEqualImpl(const Matcher * M)1079   virtual bool isEqualImpl(const Matcher *M) const {
1080     return cast<MarkGlueResultsMatcher>(M)->GlueResultNodes == GlueResultNodes;
1081   }
1082   virtual unsigned getHashImpl() const;
1083 };
1084 
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;
1091 public:
CompleteMatchMatcher(const unsigned * results,unsigned numresults,const PatternToMatch & pattern)1092   CompleteMatchMatcher(const unsigned *results, unsigned numresults,
1093                        const PatternToMatch &pattern)
1094   : Matcher(CompleteMatch), Results(results, results+numresults),
1095     Pattern(pattern) {}
1096 
getNumResults()1097   unsigned getNumResults() const { return Results.size(); }
getResult(unsigned R)1098   unsigned getResult(unsigned R) const { return Results[R]; }
getPattern()1099   const PatternToMatch &getPattern() const { return Pattern; }
1100 
classof(const Matcher * N)1101   static inline bool classof(const Matcher *N) {
1102     return N->getKind() == CompleteMatch;
1103   }
1104 
1105 private:
1106   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
isEqualImpl(const Matcher * M)1107   virtual bool isEqualImpl(const Matcher *M) const {
1108     return cast<CompleteMatchMatcher>(M)->Results == Results &&
1109           &cast<CompleteMatchMatcher>(M)->Pattern == &Pattern;
1110   }
1111   virtual unsigned getHashImpl() const;
1112 };
1113 
1114 } // end namespace llvm
1115 
1116 #endif
1117