Use ArrayRef to simplify some code.
[oota-llvm.git] / utils / TableGen / DAGISelMatcher.h
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/ADT/ArrayRef.h"
14 #include "llvm/ADT/OwningPtr.h"
15 #include "llvm/ADT/SmallVector.h"
16 #include "llvm/ADT/StringRef.h"
17 #include "llvm/CodeGen/ValueTypes.h"
18 #include "llvm/Support/Casting.h"
19
20 namespace llvm {
21   struct CodeGenRegister;
22   class CodeGenDAGPatterns;
23   class Matcher;
24   class PatternToMatch;
25   class raw_ostream;
26   class ComplexPattern;
27   class Record;
28   class SDNodeInfo;
29   class TreePredicateFn;
30   class TreePattern;
31
32 Matcher *ConvertPatternToMatcher(const PatternToMatch &Pattern,unsigned Variant,
33                                  const CodeGenDAGPatterns &CGP);
34 Matcher *OptimizeMatcher(Matcher *Matcher, const CodeGenDAGPatterns &CGP);
35 void EmitMatcherTable(const Matcher *Matcher, const CodeGenDAGPatterns &CGP,
36                       raw_ostream &OS);
37
38
39 /// Matcher - Base class for all the DAG ISel Matcher representation
40 /// nodes.
41 class Matcher {
42   // The next matcher node that is executed after this one.  Null if this is the
43   // last stage of a match.
44   OwningPtr<Matcher> Next;
45   virtual void anchor();
46 public:
47   enum KindTy {
48     // Matcher state manipulation.
49     Scope,                // Push a checking scope.
50     RecordNode,           // Record the current node.
51     RecordChild,          // Record a child of the current node.
52     RecordMemRef,         // Record the memref in the current node.
53     CaptureGlueInput,     // If the current node has an input glue, save it.
54     MoveChild,            // Move current node to specified child.
55     MoveParent,           // Move current node to parent.
56
57     // Predicate checking.
58     CheckSame,            // Fail if not same as prev match.
59     CheckChildSame,       // Fail if child not same as prev match.
60     CheckPatternPredicate,
61     CheckPredicate,       // Fail if node predicate fails.
62     CheckOpcode,          // Fail if not opcode.
63     SwitchOpcode,         // Dispatch based on opcode.
64     CheckType,            // Fail if not correct type.
65     SwitchType,           // Dispatch based on type.
66     CheckChildType,       // Fail if child has wrong type.
67     CheckInteger,         // Fail if wrong val.
68     CheckCondCode,        // Fail if not condcode.
69     CheckValueType,
70     CheckComplexPat,
71     CheckAndImm,
72     CheckOrImm,
73     CheckFoldableChainNode,
74
75     // Node creation/emisssion.
76     EmitInteger,          // Create a TargetConstant
77     EmitStringInteger,    // Create a TargetConstant from a string.
78     EmitRegister,         // Create a register.
79     EmitConvertToTarget,  // Convert a imm/fpimm to target imm/fpimm
80     EmitMergeInputChains, // Merge together a chains for an input.
81     EmitCopyToReg,        // Emit a copytoreg into a physreg.
82     EmitNode,             // Create a DAG node
83     EmitNodeXForm,        // Run a SDNodeXForm
84     MarkGlueResults,      // Indicate which interior nodes have glue results.
85     CompleteMatch,        // Finish a match and update the results.
86     MorphNodeTo           // Build a node, finish a match and update results.
87   };
88   const KindTy Kind;
89
90 protected:
91   Matcher(KindTy K) : Kind(K) {}
92 public:
93   virtual ~Matcher() {}
94
95   KindTy getKind() const { return Kind; }
96
97   Matcher *getNext() { return Next.get(); }
98   const Matcher *getNext() const { return Next.get(); }
99   void setNext(Matcher *C) { Next.reset(C); }
100   Matcher *takeNext() { return Next.take(); }
101
102   OwningPtr<Matcher> &getNextPtr() { return Next; }
103
104   bool isEqual(const Matcher *M) const {
105     if (getKind() != M->getKind()) return false;
106     return isEqualImpl(M);
107   }
108
109   unsigned getHash() const {
110     // Clear the high bit so we don't conflict with tombstones etc.
111     return ((getHashImpl() << 4) ^ getKind()) & (~0U>>1);
112   }
113
114   /// isSafeToReorderWithPatternPredicate - Return true if it is safe to sink a
115   /// PatternPredicate node past this one.
116   virtual bool isSafeToReorderWithPatternPredicate() const {
117     return false;
118   }
119
120   /// isSimplePredicateNode - Return true if this is a simple predicate that
121   /// operates on the node or its children without potential side effects or a
122   /// change of the current node.
123   bool isSimplePredicateNode() const {
124     switch (getKind()) {
125     default: return false;
126     case CheckSame:
127     case CheckChildSame:
128     case CheckPatternPredicate:
129     case CheckPredicate:
130     case CheckOpcode:
131     case CheckType:
132     case CheckChildType:
133     case CheckInteger:
134     case CheckCondCode:
135     case CheckValueType:
136     case CheckAndImm:
137     case CheckOrImm:
138     case CheckFoldableChainNode:
139       return true;
140     }
141   }
142
143   /// isSimplePredicateOrRecordNode - Return true if this is a record node or
144   /// a simple predicate.
145   bool isSimplePredicateOrRecordNode() const {
146     return isSimplePredicateNode() ||
147            getKind() == RecordNode || getKind() == RecordChild;
148   }
149
150   /// unlinkNode - Unlink the specified node from this chain.  If Other == this,
151   /// we unlink the next pointer and return it.  Otherwise we unlink Other from
152   /// the list and return this.
153   Matcher *unlinkNode(Matcher *Other);
154
155   /// canMoveBefore - Return true if this matcher is the same as Other, or if
156   /// we can move this matcher past all of the nodes in-between Other and this
157   /// node.  Other must be equal to or before this.
158   bool canMoveBefore(const Matcher *Other) const;
159
160   /// canMoveBeforeNode - Return true if it is safe to move the current matcher
161   /// across the specified one.
162   bool canMoveBeforeNode(const Matcher *Other) const;
163
164   /// isContradictory - Return true of these two matchers could never match on
165   /// the same node.
166   bool isContradictory(const Matcher *Other) const {
167     // Since this predicate is reflexive, we canonicalize the ordering so that
168     // we always match a node against nodes with kinds that are greater or equal
169     // to them.  For example, we'll pass in a CheckType node as an argument to
170     // the CheckOpcode method, not the other way around.
171     if (getKind() < Other->getKind())
172       return isContradictoryImpl(Other);
173     return Other->isContradictoryImpl(this);
174   }
175
176   void print(raw_ostream &OS, unsigned indent = 0) const;
177   void printOne(raw_ostream &OS) const;
178   void dump() const;
179 protected:
180   virtual void printImpl(raw_ostream &OS, unsigned indent) const = 0;
181   virtual bool isEqualImpl(const Matcher *M) const = 0;
182   virtual unsigned getHashImpl() const = 0;
183   virtual bool isContradictoryImpl(const Matcher *M) const { return false; }
184 };
185
186 /// ScopeMatcher - This attempts to match each of its children to find the first
187 /// one that successfully matches.  If one child fails, it tries the next child.
188 /// If none of the children match then this check fails.  It never has a 'next'.
189 class ScopeMatcher : public Matcher {
190   SmallVector<Matcher*, 4> Children;
191 public:
192   ScopeMatcher(ArrayRef<Matcher *> children)
193     : Matcher(Scope), Children(children.begin(), children.end()) {
194   }
195   virtual ~ScopeMatcher();
196
197   unsigned getNumChildren() const { return Children.size(); }
198
199   Matcher *getChild(unsigned i) { return Children[i]; }
200   const Matcher *getChild(unsigned i) const { return Children[i]; }
201
202   void resetChild(unsigned i, Matcher *N) {
203     delete Children[i];
204     Children[i] = N;
205   }
206
207   Matcher *takeChild(unsigned i) {
208     Matcher *Res = Children[i];
209     Children[i] = 0;
210     return Res;
211   }
212
213   void setNumChildren(unsigned NC) {
214     if (NC < Children.size()) {
215       // delete any children we're about to lose pointers to.
216       for (unsigned i = NC, e = Children.size(); i != e; ++i)
217         delete Children[i];
218     }
219     Children.resize(NC);
220   }
221
222   static inline bool classof(const Matcher *N) {
223     return N->getKind() == Scope;
224   }
225
226 private:
227   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
228   virtual bool isEqualImpl(const Matcher *M) const { return false; }
229   virtual unsigned getHashImpl() const { return 12312; }
230 };
231
232 /// RecordMatcher - Save the current node in the operand list.
233 class RecordMatcher : public Matcher {
234   /// WhatFor - This is a string indicating why we're recording this.  This
235   /// should only be used for comment generation not anything semantic.
236   std::string WhatFor;
237
238   /// ResultNo - The slot number in the RecordedNodes vector that this will be,
239   /// just printed as a comment.
240   unsigned ResultNo;
241 public:
242   RecordMatcher(const std::string &whatfor, unsigned resultNo)
243     : Matcher(RecordNode), WhatFor(whatfor), ResultNo(resultNo) {}
244
245   const std::string &getWhatFor() const { return WhatFor; }
246   unsigned getResultNo() const { return ResultNo; }
247
248   static inline bool classof(const Matcher *N) {
249     return N->getKind() == RecordNode;
250   }
251
252   virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
253 private:
254   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
255   virtual bool isEqualImpl(const Matcher *M) const { return true; }
256   virtual unsigned getHashImpl() const { return 0; }
257 };
258
259 /// RecordChildMatcher - Save a numbered child of the current node, or fail
260 /// the match if it doesn't exist.  This is logically equivalent to:
261 ///    MoveChild N + RecordNode + MoveParent.
262 class RecordChildMatcher : public Matcher {
263   unsigned ChildNo;
264
265   /// WhatFor - This is a string indicating why we're recording this.  This
266   /// should only be used for comment generation not anything semantic.
267   std::string WhatFor;
268
269   /// ResultNo - The slot number in the RecordedNodes vector that this will be,
270   /// just printed as a comment.
271   unsigned ResultNo;
272 public:
273   RecordChildMatcher(unsigned childno, const std::string &whatfor,
274                      unsigned resultNo)
275   : Matcher(RecordChild), ChildNo(childno), WhatFor(whatfor),
276     ResultNo(resultNo) {}
277
278   unsigned getChildNo() const { return ChildNo; }
279   const std::string &getWhatFor() const { return WhatFor; }
280   unsigned getResultNo() const { return ResultNo; }
281
282   static inline bool classof(const Matcher *N) {
283     return N->getKind() == RecordChild;
284   }
285
286   virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
287
288 private:
289   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
290   virtual bool isEqualImpl(const Matcher *M) const {
291     return cast<RecordChildMatcher>(M)->getChildNo() == getChildNo();
292   }
293   virtual unsigned getHashImpl() const { return getChildNo(); }
294 };
295
296 /// RecordMemRefMatcher - Save the current node's memref.
297 class RecordMemRefMatcher : public Matcher {
298 public:
299   RecordMemRefMatcher() : Matcher(RecordMemRef) {}
300
301   static inline bool classof(const Matcher *N) {
302     return N->getKind() == RecordMemRef;
303   }
304
305   virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
306
307 private:
308   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
309   virtual bool isEqualImpl(const Matcher *M) const { return true; }
310   virtual unsigned getHashImpl() const { return 0; }
311 };
312
313
314 /// CaptureGlueInputMatcher - If the current record has a glue input, record
315 /// it so that it is used as an input to the generated code.
316 class CaptureGlueInputMatcher : public Matcher {
317 public:
318   CaptureGlueInputMatcher() : Matcher(CaptureGlueInput) {}
319
320   static inline bool classof(const Matcher *N) {
321     return N->getKind() == CaptureGlueInput;
322   }
323
324   virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
325
326 private:
327   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
328   virtual bool isEqualImpl(const Matcher *M) const { return true; }
329   virtual unsigned getHashImpl() const { return 0; }
330 };
331
332 /// MoveChildMatcher - This tells the interpreter to move into the
333 /// specified child node.
334 class MoveChildMatcher : public Matcher {
335   unsigned ChildNo;
336 public:
337   MoveChildMatcher(unsigned childNo) : Matcher(MoveChild), ChildNo(childNo) {}
338
339   unsigned getChildNo() const { return ChildNo; }
340
341   static inline bool classof(const Matcher *N) {
342     return N->getKind() == MoveChild;
343   }
344
345   virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
346
347 private:
348   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
349   virtual bool isEqualImpl(const Matcher *M) const {
350     return cast<MoveChildMatcher>(M)->getChildNo() == getChildNo();
351   }
352   virtual unsigned getHashImpl() const { return getChildNo(); }
353 };
354
355 /// MoveParentMatcher - This tells the interpreter to move to the parent
356 /// of the current node.
357 class MoveParentMatcher : public Matcher {
358 public:
359   MoveParentMatcher() : Matcher(MoveParent) {}
360
361   static inline bool classof(const Matcher *N) {
362     return N->getKind() == MoveParent;
363   }
364
365   virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
366
367 private:
368   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
369   virtual bool isEqualImpl(const Matcher *M) const { return true; }
370   virtual unsigned getHashImpl() const { return 0; }
371 };
372
373 /// CheckSameMatcher - This checks to see if this node is exactly the same
374 /// node as the specified match that was recorded with 'Record'.  This is used
375 /// when patterns have the same name in them, like '(mul GPR:$in, GPR:$in)'.
376 class CheckSameMatcher : public Matcher {
377   unsigned MatchNumber;
378 public:
379   CheckSameMatcher(unsigned matchnumber)
380     : Matcher(CheckSame), MatchNumber(matchnumber) {}
381
382   unsigned getMatchNumber() const { return MatchNumber; }
383
384   static inline bool classof(const Matcher *N) {
385     return N->getKind() == CheckSame;
386   }
387
388   virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
389
390 private:
391   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
392   virtual bool isEqualImpl(const Matcher *M) const {
393     return cast<CheckSameMatcher>(M)->getMatchNumber() == getMatchNumber();
394   }
395   virtual unsigned getHashImpl() const { return getMatchNumber(); }
396 };
397
398 /// CheckChildSameMatcher - This checks to see if child node is exactly the same
399 /// node as the specified match that was recorded with 'Record'.  This is used
400 /// when patterns have the same name in them, like '(mul GPR:$in, GPR:$in)'.
401 class CheckChildSameMatcher : public Matcher {
402   unsigned ChildNo;
403   unsigned MatchNumber;
404 public:
405   CheckChildSameMatcher(unsigned childno, unsigned matchnumber)
406     : Matcher(CheckChildSame), ChildNo(childno), MatchNumber(matchnumber) {}
407
408   unsigned getChildNo() const { return ChildNo; }
409   unsigned getMatchNumber() const { return MatchNumber; }
410
411   static inline bool classof(const Matcher *N) {
412     return N->getKind() == CheckChildSame;
413   }
414
415   virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
416
417 private:
418   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
419   virtual bool isEqualImpl(const Matcher *M) const {
420     return cast<CheckChildSameMatcher>(M)->ChildNo == ChildNo &&
421            cast<CheckChildSameMatcher>(M)->MatchNumber == MatchNumber;
422   }
423   virtual unsigned getHashImpl() const { return (MatchNumber << 2) | ChildNo; }
424 };
425
426 /// CheckPatternPredicateMatcher - This checks the target-specific predicate
427 /// to see if the entire pattern is capable of matching.  This predicate does
428 /// not take a node as input.  This is used for subtarget feature checks etc.
429 class CheckPatternPredicateMatcher : public Matcher {
430   std::string Predicate;
431 public:
432   CheckPatternPredicateMatcher(StringRef predicate)
433     : Matcher(CheckPatternPredicate), Predicate(predicate) {}
434
435   StringRef getPredicate() const { return Predicate; }
436
437   static inline bool classof(const Matcher *N) {
438     return N->getKind() == CheckPatternPredicate;
439   }
440
441   virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
442
443 private:
444   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
445   virtual bool isEqualImpl(const Matcher *M) const {
446     return cast<CheckPatternPredicateMatcher>(M)->getPredicate() == Predicate;
447   }
448   virtual unsigned getHashImpl() const;
449 };
450
451 /// CheckPredicateMatcher - This checks the target-specific predicate to
452 /// see if the node is acceptable.
453 class CheckPredicateMatcher : public Matcher {
454   TreePattern *Pred;
455 public:
456   CheckPredicateMatcher(const TreePredicateFn &pred);
457
458   TreePredicateFn getPredicate() const;
459
460   static inline bool classof(const Matcher *N) {
461     return N->getKind() == CheckPredicate;
462   }
463
464   // TODO: Ok?
465   //virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
466
467 private:
468   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
469   virtual bool isEqualImpl(const Matcher *M) const {
470     return cast<CheckPredicateMatcher>(M)->Pred == Pred;
471   }
472   virtual unsigned getHashImpl() const;
473 };
474
475
476 /// CheckOpcodeMatcher - This checks to see if the current node has the
477 /// specified opcode, if not it fails to match.
478 class CheckOpcodeMatcher : public Matcher {
479   const SDNodeInfo &Opcode;
480 public:
481   CheckOpcodeMatcher(const SDNodeInfo &opcode)
482     : Matcher(CheckOpcode), Opcode(opcode) {}
483
484   const SDNodeInfo &getOpcode() const { return Opcode; }
485
486   static inline bool classof(const Matcher *N) {
487     return N->getKind() == CheckOpcode;
488   }
489
490   virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
491
492 private:
493   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
494   virtual bool isEqualImpl(const Matcher *M) const;
495   virtual unsigned getHashImpl() const;
496   virtual bool isContradictoryImpl(const Matcher *M) const;
497 };
498
499 /// SwitchOpcodeMatcher - Switch based on the current node's opcode, dispatching
500 /// to one matcher per opcode.  If the opcode doesn't match any of the cases,
501 /// then the match fails.  This is semantically equivalent to a Scope node where
502 /// every child does a CheckOpcode, but is much faster.
503 class SwitchOpcodeMatcher : public Matcher {
504   SmallVector<std::pair<const SDNodeInfo*, Matcher*>, 8> Cases;
505 public:
506   SwitchOpcodeMatcher(ArrayRef<std::pair<const SDNodeInfo*, Matcher*> > cases)
507     : Matcher(SwitchOpcode), Cases(cases.begin(), cases.end()) {}
508
509   static inline bool classof(const Matcher *N) {
510     return N->getKind() == SwitchOpcode;
511   }
512
513   unsigned getNumCases() const { return Cases.size(); }
514
515   const SDNodeInfo &getCaseOpcode(unsigned i) const { return *Cases[i].first; }
516   Matcher *getCaseMatcher(unsigned i) { return Cases[i].second; }
517   const Matcher *getCaseMatcher(unsigned i) const { return Cases[i].second; }
518
519 private:
520   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
521   virtual bool isEqualImpl(const Matcher *M) const { return false; }
522   virtual unsigned getHashImpl() const { return 4123; }
523 };
524
525 /// CheckTypeMatcher - This checks to see if the current node has the
526 /// specified type at the specified result, if not it fails to match.
527 class CheckTypeMatcher : public Matcher {
528   MVT::SimpleValueType Type;
529   unsigned ResNo;
530 public:
531   CheckTypeMatcher(MVT::SimpleValueType type, unsigned resno)
532     : Matcher(CheckType), Type(type), ResNo(resno) {}
533
534   MVT::SimpleValueType getType() const { return Type; }
535   unsigned getResNo() const { return ResNo; }
536
537   static inline bool classof(const Matcher *N) {
538     return N->getKind() == CheckType;
539   }
540
541   virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
542
543 private:
544   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
545   virtual bool isEqualImpl(const Matcher *M) const {
546     return cast<CheckTypeMatcher>(M)->Type == Type;
547   }
548   virtual unsigned getHashImpl() const { return Type; }
549   virtual bool isContradictoryImpl(const Matcher *M) const;
550 };
551
552 /// SwitchTypeMatcher - Switch based on the current node's type, dispatching
553 /// to one matcher per case.  If the type doesn't match any of the cases,
554 /// then the match fails.  This is semantically equivalent to a Scope node where
555 /// every child does a CheckType, but is much faster.
556 class SwitchTypeMatcher : public Matcher {
557   SmallVector<std::pair<MVT::SimpleValueType, Matcher*>, 8> Cases;
558 public:
559   SwitchTypeMatcher(ArrayRef<std::pair<MVT::SimpleValueType, Matcher*> > cases)
560   : Matcher(SwitchType), Cases(cases.begin(), cases.end()) {}
561
562   static inline bool classof(const Matcher *N) {
563     return N->getKind() == SwitchType;
564   }
565
566   unsigned getNumCases() const { return Cases.size(); }
567
568   MVT::SimpleValueType getCaseType(unsigned i) const { return Cases[i].first; }
569   Matcher *getCaseMatcher(unsigned i) { return Cases[i].second; }
570   const Matcher *getCaseMatcher(unsigned i) const { return Cases[i].second; }
571
572 private:
573   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
574   virtual bool isEqualImpl(const Matcher *M) const { return false; }
575   virtual unsigned getHashImpl() const { return 4123; }
576 };
577
578
579 /// CheckChildTypeMatcher - This checks to see if a child node has the
580 /// specified type, if not it fails to match.
581 class CheckChildTypeMatcher : public Matcher {
582   unsigned ChildNo;
583   MVT::SimpleValueType Type;
584 public:
585   CheckChildTypeMatcher(unsigned childno, MVT::SimpleValueType type)
586     : Matcher(CheckChildType), ChildNo(childno), Type(type) {}
587
588   unsigned getChildNo() const { return ChildNo; }
589   MVT::SimpleValueType getType() const { return Type; }
590
591   static inline bool classof(const Matcher *N) {
592     return N->getKind() == CheckChildType;
593   }
594
595   virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
596
597 private:
598   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
599   virtual bool isEqualImpl(const Matcher *M) const {
600     return cast<CheckChildTypeMatcher>(M)->ChildNo == ChildNo &&
601            cast<CheckChildTypeMatcher>(M)->Type == Type;
602   }
603   virtual unsigned getHashImpl() const { return (Type << 3) | ChildNo; }
604   virtual bool isContradictoryImpl(const Matcher *M) const;
605 };
606
607
608 /// CheckIntegerMatcher - This checks to see if the current node is a
609 /// ConstantSDNode with the specified integer value, if not it fails to match.
610 class CheckIntegerMatcher : public Matcher {
611   int64_t Value;
612 public:
613   CheckIntegerMatcher(int64_t value)
614     : Matcher(CheckInteger), Value(value) {}
615
616   int64_t getValue() const { return Value; }
617
618   static inline bool classof(const Matcher *N) {
619     return N->getKind() == CheckInteger;
620   }
621
622   virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
623
624 private:
625   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
626   virtual bool isEqualImpl(const Matcher *M) const {
627     return cast<CheckIntegerMatcher>(M)->Value == Value;
628   }
629   virtual unsigned getHashImpl() const { return Value; }
630   virtual bool isContradictoryImpl(const Matcher *M) const;
631 };
632
633 /// CheckCondCodeMatcher - This checks to see if the current node is a
634 /// CondCodeSDNode with the specified condition, if not it fails to match.
635 class CheckCondCodeMatcher : public Matcher {
636   StringRef CondCodeName;
637 public:
638   CheckCondCodeMatcher(StringRef condcodename)
639     : Matcher(CheckCondCode), CondCodeName(condcodename) {}
640
641   StringRef getCondCodeName() const { return CondCodeName; }
642
643   static inline bool classof(const Matcher *N) {
644     return N->getKind() == CheckCondCode;
645   }
646
647   virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
648
649 private:
650   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
651   virtual bool isEqualImpl(const Matcher *M) const {
652     return cast<CheckCondCodeMatcher>(M)->CondCodeName == CondCodeName;
653   }
654   virtual unsigned getHashImpl() const;
655 };
656
657 /// CheckValueTypeMatcher - This checks to see if the current node is a
658 /// VTSDNode with the specified type, if not it fails to match.
659 class CheckValueTypeMatcher : public Matcher {
660   StringRef TypeName;
661 public:
662   CheckValueTypeMatcher(StringRef type_name)
663     : Matcher(CheckValueType), TypeName(type_name) {}
664
665   StringRef getTypeName() const { return TypeName; }
666
667   static inline bool classof(const Matcher *N) {
668     return N->getKind() == CheckValueType;
669   }
670
671   virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
672
673 private:
674   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
675   virtual bool isEqualImpl(const Matcher *M) const {
676     return cast<CheckValueTypeMatcher>(M)->TypeName == TypeName;
677   }
678   virtual unsigned getHashImpl() const;
679   bool isContradictoryImpl(const Matcher *M) const;
680 };
681
682
683
684 /// CheckComplexPatMatcher - This node runs the specified ComplexPattern on
685 /// the current node.
686 class CheckComplexPatMatcher : public Matcher {
687   const ComplexPattern &Pattern;
688
689   /// MatchNumber - This is the recorded nodes slot that contains the node we
690   /// want to match against.
691   unsigned MatchNumber;
692
693   /// Name - The name of the node we're matching, for comment emission.
694   std::string Name;
695
696   /// FirstResult - This is the first slot in the RecordedNodes list that the
697   /// result of the match populates.
698   unsigned FirstResult;
699 public:
700   CheckComplexPatMatcher(const ComplexPattern &pattern, unsigned matchnumber,
701                          const std::string &name, unsigned firstresult)
702     : Matcher(CheckComplexPat), Pattern(pattern), MatchNumber(matchnumber),
703       Name(name), FirstResult(firstresult) {}
704
705   const ComplexPattern &getPattern() const { return Pattern; }
706   unsigned getMatchNumber() const { return MatchNumber; }
707
708   const std::string getName() const { return Name; }
709   unsigned getFirstResult() const { return FirstResult; }
710
711   static inline bool classof(const Matcher *N) {
712     return N->getKind() == CheckComplexPat;
713   }
714
715   // Not safe to move a pattern predicate past a complex pattern.
716   virtual bool isSafeToReorderWithPatternPredicate() const { return false; }
717
718 private:
719   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
720   virtual bool isEqualImpl(const Matcher *M) const {
721     return &cast<CheckComplexPatMatcher>(M)->Pattern == &Pattern &&
722            cast<CheckComplexPatMatcher>(M)->MatchNumber == MatchNumber;
723   }
724   virtual unsigned getHashImpl() const {
725     return (unsigned)(intptr_t)&Pattern ^ MatchNumber;
726   }
727 };
728
729 /// CheckAndImmMatcher - This checks to see if the current node is an 'and'
730 /// with something equivalent to the specified immediate.
731 class CheckAndImmMatcher : public Matcher {
732   int64_t Value;
733 public:
734   CheckAndImmMatcher(int64_t value)
735     : Matcher(CheckAndImm), Value(value) {}
736
737   int64_t getValue() const { return Value; }
738
739   static inline bool classof(const Matcher *N) {
740     return N->getKind() == CheckAndImm;
741   }
742
743   virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
744
745 private:
746   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
747   virtual bool isEqualImpl(const Matcher *M) const {
748     return cast<CheckAndImmMatcher>(M)->Value == Value;
749   }
750   virtual unsigned getHashImpl() const { return Value; }
751 };
752
753 /// CheckOrImmMatcher - This checks to see if the current node is an 'and'
754 /// with something equivalent to the specified immediate.
755 class CheckOrImmMatcher : public Matcher {
756   int64_t Value;
757 public:
758   CheckOrImmMatcher(int64_t value)
759     : Matcher(CheckOrImm), Value(value) {}
760
761   int64_t getValue() const { return Value; }
762
763   static inline bool classof(const Matcher *N) {
764     return N->getKind() == CheckOrImm;
765   }
766
767   virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
768
769 private:
770   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
771   virtual bool isEqualImpl(const Matcher *M) const {
772     return cast<CheckOrImmMatcher>(M)->Value == Value;
773   }
774   virtual unsigned getHashImpl() const { return Value; }
775 };
776
777 /// CheckFoldableChainNodeMatcher - This checks to see if the current node
778 /// (which defines a chain operand) is safe to fold into a larger pattern.
779 class CheckFoldableChainNodeMatcher : public Matcher {
780 public:
781   CheckFoldableChainNodeMatcher()
782     : Matcher(CheckFoldableChainNode) {}
783
784   static inline bool classof(const Matcher *N) {
785     return N->getKind() == CheckFoldableChainNode;
786   }
787
788   virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
789
790 private:
791   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
792   virtual bool isEqualImpl(const Matcher *M) const { return true; }
793   virtual unsigned getHashImpl() const { return 0; }
794 };
795
796 /// EmitIntegerMatcher - This creates a new TargetConstant.
797 class EmitIntegerMatcher : public Matcher {
798   int64_t Val;
799   MVT::SimpleValueType VT;
800 public:
801   EmitIntegerMatcher(int64_t val, MVT::SimpleValueType vt)
802     : Matcher(EmitInteger), Val(val), VT(vt) {}
803
804   int64_t getValue() const { return Val; }
805   MVT::SimpleValueType getVT() const { return VT; }
806
807   static inline bool classof(const Matcher *N) {
808     return N->getKind() == EmitInteger;
809   }
810
811 private:
812   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
813   virtual bool isEqualImpl(const Matcher *M) const {
814     return cast<EmitIntegerMatcher>(M)->Val == Val &&
815            cast<EmitIntegerMatcher>(M)->VT == VT;
816   }
817   virtual unsigned getHashImpl() const { return (Val << 4) | VT; }
818 };
819
820 /// EmitStringIntegerMatcher - A target constant whose value is represented
821 /// by a string.
822 class EmitStringIntegerMatcher : public Matcher {
823   std::string Val;
824   MVT::SimpleValueType VT;
825 public:
826   EmitStringIntegerMatcher(const std::string &val, MVT::SimpleValueType vt)
827     : Matcher(EmitStringInteger), Val(val), VT(vt) {}
828
829   const std::string &getValue() const { return Val; }
830   MVT::SimpleValueType getVT() const { return VT; }
831
832   static inline bool classof(const Matcher *N) {
833     return N->getKind() == EmitStringInteger;
834   }
835
836 private:
837   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
838   virtual bool isEqualImpl(const Matcher *M) const {
839     return cast<EmitStringIntegerMatcher>(M)->Val == Val &&
840            cast<EmitStringIntegerMatcher>(M)->VT == VT;
841   }
842   virtual unsigned getHashImpl() const;
843 };
844
845 /// EmitRegisterMatcher - This creates a new TargetConstant.
846 class EmitRegisterMatcher : public Matcher {
847   /// Reg - The def for the register that we're emitting.  If this is null, then
848   /// this is a reference to zero_reg.
849   const CodeGenRegister *Reg;
850   MVT::SimpleValueType VT;
851 public:
852   EmitRegisterMatcher(const CodeGenRegister *reg, MVT::SimpleValueType vt)
853     : Matcher(EmitRegister), Reg(reg), VT(vt) {}
854
855   const CodeGenRegister *getReg() const { return Reg; }
856   MVT::SimpleValueType getVT() const { return VT; }
857
858   static inline bool classof(const Matcher *N) {
859     return N->getKind() == EmitRegister;
860   }
861
862 private:
863   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
864   virtual bool isEqualImpl(const Matcher *M) const {
865     return cast<EmitRegisterMatcher>(M)->Reg == Reg &&
866            cast<EmitRegisterMatcher>(M)->VT == VT;
867   }
868   virtual unsigned getHashImpl() const {
869     return ((unsigned)(intptr_t)Reg) << 4 | VT;
870   }
871 };
872
873 /// EmitConvertToTargetMatcher - Emit an operation that reads a specified
874 /// recorded node and converts it from being a ISD::Constant to
875 /// ISD::TargetConstant, likewise for ConstantFP.
876 class EmitConvertToTargetMatcher : public Matcher {
877   unsigned Slot;
878 public:
879   EmitConvertToTargetMatcher(unsigned slot)
880     : Matcher(EmitConvertToTarget), Slot(slot) {}
881
882   unsigned getSlot() const { return Slot; }
883
884   static inline bool classof(const Matcher *N) {
885     return N->getKind() == EmitConvertToTarget;
886   }
887
888 private:
889   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
890   virtual bool isEqualImpl(const Matcher *M) const {
891     return cast<EmitConvertToTargetMatcher>(M)->Slot == Slot;
892   }
893   virtual unsigned getHashImpl() const { return Slot; }
894 };
895
896 /// EmitMergeInputChainsMatcher - Emit a node that merges a list of input
897 /// chains together with a token factor.  The list of nodes are the nodes in the
898 /// matched pattern that have chain input/outputs.  This node adds all input
899 /// chains of these nodes if they are not themselves a node in the pattern.
900 class EmitMergeInputChainsMatcher : public Matcher {
901   SmallVector<unsigned, 3> ChainNodes;
902 public:
903   EmitMergeInputChainsMatcher(ArrayRef<unsigned> nodes)
904     : Matcher(EmitMergeInputChains), ChainNodes(nodes.begin(), nodes.end()) {}
905
906   unsigned getNumNodes() const { return ChainNodes.size(); }
907
908   unsigned getNode(unsigned i) const {
909     assert(i < ChainNodes.size());
910     return ChainNodes[i];
911   }
912
913   static inline bool classof(const Matcher *N) {
914     return N->getKind() == EmitMergeInputChains;
915   }
916
917 private:
918   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
919   virtual bool isEqualImpl(const Matcher *M) const {
920     return cast<EmitMergeInputChainsMatcher>(M)->ChainNodes == ChainNodes;
921   }
922   virtual unsigned getHashImpl() const;
923 };
924
925 /// EmitCopyToRegMatcher - Emit a CopyToReg node from a value to a physreg,
926 /// pushing the chain and glue results.
927 ///
928 class EmitCopyToRegMatcher : public Matcher {
929   unsigned SrcSlot; // Value to copy into the physreg.
930   Record *DestPhysReg;
931 public:
932   EmitCopyToRegMatcher(unsigned srcSlot, Record *destPhysReg)
933     : Matcher(EmitCopyToReg), SrcSlot(srcSlot), DestPhysReg(destPhysReg) {}
934
935   unsigned getSrcSlot() const { return SrcSlot; }
936   Record *getDestPhysReg() const { return DestPhysReg; }
937
938   static inline bool classof(const Matcher *N) {
939     return N->getKind() == EmitCopyToReg;
940   }
941
942 private:
943   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
944   virtual bool isEqualImpl(const Matcher *M) const {
945     return cast<EmitCopyToRegMatcher>(M)->SrcSlot == SrcSlot &&
946            cast<EmitCopyToRegMatcher>(M)->DestPhysReg == DestPhysReg;
947   }
948   virtual unsigned getHashImpl() const {
949     return SrcSlot ^ ((unsigned)(intptr_t)DestPhysReg << 4);
950   }
951 };
952
953
954
955 /// EmitNodeXFormMatcher - Emit an operation that runs an SDNodeXForm on a
956 /// recorded node and records the result.
957 class EmitNodeXFormMatcher : public Matcher {
958   unsigned Slot;
959   Record *NodeXForm;
960 public:
961   EmitNodeXFormMatcher(unsigned slot, Record *nodeXForm)
962     : Matcher(EmitNodeXForm), Slot(slot), NodeXForm(nodeXForm) {}
963
964   unsigned getSlot() const { return Slot; }
965   Record *getNodeXForm() const { return NodeXForm; }
966
967   static inline bool classof(const Matcher *N) {
968     return N->getKind() == EmitNodeXForm;
969   }
970
971 private:
972   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
973   virtual bool isEqualImpl(const Matcher *M) const {
974     return cast<EmitNodeXFormMatcher>(M)->Slot == Slot &&
975            cast<EmitNodeXFormMatcher>(M)->NodeXForm == NodeXForm;
976   }
977   virtual unsigned getHashImpl() const {
978     return Slot ^ ((unsigned)(intptr_t)NodeXForm << 4);
979   }
980 };
981
982 /// EmitNodeMatcherCommon - Common class shared between EmitNode and
983 /// MorphNodeTo.
984 class EmitNodeMatcherCommon : public Matcher {
985   std::string OpcodeName;
986   const SmallVector<MVT::SimpleValueType, 3> VTs;
987   const SmallVector<unsigned, 6> Operands;
988   bool HasChain, HasInGlue, HasOutGlue, HasMemRefs;
989
990   /// NumFixedArityOperands - If this is a fixed arity node, this is set to -1.
991   /// If this is a varidic node, this is set to the number of fixed arity
992   /// operands in the root of the pattern.  The rest are appended to this node.
993   int NumFixedArityOperands;
994 public:
995   EmitNodeMatcherCommon(const std::string &opcodeName,
996                         ArrayRef<MVT::SimpleValueType> vts,
997                         ArrayRef<unsigned> operands,
998                         bool hasChain, bool hasInGlue, bool hasOutGlue,
999                         bool hasmemrefs,
1000                         int numfixedarityoperands, bool isMorphNodeTo)
1001     : Matcher(isMorphNodeTo ? MorphNodeTo : EmitNode), OpcodeName(opcodeName),
1002       VTs(vts.begin(), vts.end()), Operands(operands.begin(), operands.end()),
1003       HasChain(hasChain), HasInGlue(hasInGlue), HasOutGlue(hasOutGlue),
1004       HasMemRefs(hasmemrefs), NumFixedArityOperands(numfixedarityoperands) {}
1005
1006   const std::string &getOpcodeName() const { return OpcodeName; }
1007
1008   unsigned getNumVTs() const { return VTs.size(); }
1009   MVT::SimpleValueType getVT(unsigned i) const {
1010     assert(i < VTs.size());
1011     return VTs[i];
1012   }
1013
1014   unsigned getNumOperands() const { return Operands.size(); }
1015   unsigned getOperand(unsigned i) const {
1016     assert(i < Operands.size());
1017     return Operands[i];
1018   }
1019
1020   const SmallVectorImpl<MVT::SimpleValueType> &getVTList() const { return VTs; }
1021   const SmallVectorImpl<unsigned> &getOperandList() const { return Operands; }
1022
1023
1024   bool hasChain() const { return HasChain; }
1025   bool hasInFlag() const { return HasInGlue; }
1026   bool hasOutFlag() const { return HasOutGlue; }
1027   bool hasMemRefs() const { return HasMemRefs; }
1028   int getNumFixedArityOperands() const { return NumFixedArityOperands; }
1029
1030   static inline bool classof(const Matcher *N) {
1031     return N->getKind() == EmitNode || N->getKind() == MorphNodeTo;
1032   }
1033
1034 private:
1035   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
1036   virtual bool isEqualImpl(const Matcher *M) const;
1037   virtual unsigned getHashImpl() const;
1038 };
1039
1040 /// EmitNodeMatcher - This signals a successful match and generates a node.
1041 class EmitNodeMatcher : public EmitNodeMatcherCommon {
1042   virtual void anchor();
1043   unsigned FirstResultSlot;
1044 public:
1045   EmitNodeMatcher(const std::string &opcodeName,
1046                   ArrayRef<MVT::SimpleValueType> vts,
1047                   ArrayRef<unsigned> operands,
1048                   bool hasChain, bool hasInFlag, bool hasOutFlag,
1049                   bool hasmemrefs,
1050                   int numfixedarityoperands, unsigned firstresultslot)
1051   : EmitNodeMatcherCommon(opcodeName, vts, operands, hasChain,
1052                           hasInFlag, hasOutFlag, hasmemrefs,
1053                           numfixedarityoperands, false),
1054     FirstResultSlot(firstresultslot) {}
1055
1056   unsigned getFirstResultSlot() const { return FirstResultSlot; }
1057
1058   static inline bool classof(const Matcher *N) {
1059     return N->getKind() == EmitNode;
1060   }
1061
1062 };
1063
1064 class MorphNodeToMatcher : public EmitNodeMatcherCommon {
1065   virtual void anchor();
1066   const PatternToMatch &Pattern;
1067 public:
1068   MorphNodeToMatcher(const std::string &opcodeName,
1069                      ArrayRef<MVT::SimpleValueType> vts,
1070                      ArrayRef<unsigned> operands,
1071                      bool hasChain, bool hasInFlag, bool hasOutFlag,
1072                      bool hasmemrefs,
1073                      int numfixedarityoperands, const PatternToMatch &pattern)
1074     : EmitNodeMatcherCommon(opcodeName, vts, operands, hasChain,
1075                             hasInFlag, hasOutFlag, hasmemrefs,
1076                             numfixedarityoperands, true),
1077       Pattern(pattern) {
1078   }
1079
1080   const PatternToMatch &getPattern() const { return Pattern; }
1081
1082   static inline bool classof(const Matcher *N) {
1083     return N->getKind() == MorphNodeTo;
1084   }
1085 };
1086
1087 /// MarkGlueResultsMatcher - This node indicates which non-root nodes in the
1088 /// pattern produce glue.  This allows CompleteMatchMatcher to update them
1089 /// with the output glue of the resultant code.
1090 class MarkGlueResultsMatcher : public Matcher {
1091   SmallVector<unsigned, 3> GlueResultNodes;
1092 public:
1093   MarkGlueResultsMatcher(ArrayRef<unsigned> nodes)
1094     : Matcher(MarkGlueResults), GlueResultNodes(nodes.begin(), nodes.end()) {}
1095
1096   unsigned getNumNodes() const { return GlueResultNodes.size(); }
1097
1098   unsigned getNode(unsigned i) const {
1099     assert(i < GlueResultNodes.size());
1100     return GlueResultNodes[i];
1101   }
1102
1103   static inline bool classof(const Matcher *N) {
1104     return N->getKind() == MarkGlueResults;
1105   }
1106
1107 private:
1108   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
1109   virtual bool isEqualImpl(const Matcher *M) const {
1110     return cast<MarkGlueResultsMatcher>(M)->GlueResultNodes == GlueResultNodes;
1111   }
1112   virtual unsigned getHashImpl() const;
1113 };
1114
1115 /// CompleteMatchMatcher - Complete a match by replacing the results of the
1116 /// pattern with the newly generated nodes.  This also prints a comment
1117 /// indicating the source and dest patterns.
1118 class CompleteMatchMatcher : public Matcher {
1119   SmallVector<unsigned, 2> Results;
1120   const PatternToMatch &Pattern;
1121 public:
1122   CompleteMatchMatcher(ArrayRef<unsigned> results,
1123                        const PatternToMatch &pattern)
1124   : Matcher(CompleteMatch), Results(results.begin(), results.end()),
1125     Pattern(pattern) {}
1126
1127   unsigned getNumResults() const { return Results.size(); }
1128   unsigned getResult(unsigned R) const { return Results[R]; }
1129   const PatternToMatch &getPattern() const { return Pattern; }
1130
1131   static inline bool classof(const Matcher *N) {
1132     return N->getKind() == CompleteMatch;
1133   }
1134
1135 private:
1136   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
1137   virtual bool isEqualImpl(const Matcher *M) const {
1138     return cast<CompleteMatchMatcher>(M)->Results == Results &&
1139           &cast<CompleteMatchMatcher>(M)->Pattern == &Pattern;
1140   }
1141   virtual unsigned getHashImpl() const;
1142 };
1143
1144 } // end namespace llvm
1145
1146 #endif