Delete the Matchers stored in the SmallVectors in SwitchOpcodeMatcher/SwitchTypeMatcher.
[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   virtual ~SwitchOpcodeMatcher();
509
510   static inline bool classof(const Matcher *N) {
511     return N->getKind() == SwitchOpcode;
512   }
513
514   unsigned getNumCases() const { return Cases.size(); }
515
516   const SDNodeInfo &getCaseOpcode(unsigned i) const { return *Cases[i].first; }
517   Matcher *getCaseMatcher(unsigned i) { return Cases[i].second; }
518   const Matcher *getCaseMatcher(unsigned i) const { return Cases[i].second; }
519
520 private:
521   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
522   virtual bool isEqualImpl(const Matcher *M) const { return false; }
523   virtual unsigned getHashImpl() const { return 4123; }
524 };
525
526 /// CheckTypeMatcher - This checks to see if the current node has the
527 /// specified type at the specified result, if not it fails to match.
528 class CheckTypeMatcher : public Matcher {
529   MVT::SimpleValueType Type;
530   unsigned ResNo;
531 public:
532   CheckTypeMatcher(MVT::SimpleValueType type, unsigned resno)
533     : Matcher(CheckType), Type(type), ResNo(resno) {}
534
535   MVT::SimpleValueType getType() const { return Type; }
536   unsigned getResNo() const { return ResNo; }
537
538   static inline bool classof(const Matcher *N) {
539     return N->getKind() == CheckType;
540   }
541
542   virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
543
544 private:
545   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
546   virtual bool isEqualImpl(const Matcher *M) const {
547     return cast<CheckTypeMatcher>(M)->Type == Type;
548   }
549   virtual unsigned getHashImpl() const { return Type; }
550   virtual bool isContradictoryImpl(const Matcher *M) const;
551 };
552
553 /// SwitchTypeMatcher - Switch based on the current node's type, dispatching
554 /// to one matcher per case.  If the type doesn't match any of the cases,
555 /// then the match fails.  This is semantically equivalent to a Scope node where
556 /// every child does a CheckType, but is much faster.
557 class SwitchTypeMatcher : public Matcher {
558   SmallVector<std::pair<MVT::SimpleValueType, Matcher*>, 8> Cases;
559 public:
560   SwitchTypeMatcher(ArrayRef<std::pair<MVT::SimpleValueType, Matcher*> > cases)
561   : Matcher(SwitchType), Cases(cases.begin(), cases.end()) {}
562   virtual ~SwitchTypeMatcher();
563
564   static inline bool classof(const Matcher *N) {
565     return N->getKind() == SwitchType;
566   }
567
568   unsigned getNumCases() const { return Cases.size(); }
569
570   MVT::SimpleValueType getCaseType(unsigned i) const { return Cases[i].first; }
571   Matcher *getCaseMatcher(unsigned i) { return Cases[i].second; }
572   const Matcher *getCaseMatcher(unsigned i) const { return Cases[i].second; }
573
574 private:
575   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
576   virtual bool isEqualImpl(const Matcher *M) const { return false; }
577   virtual unsigned getHashImpl() const { return 4123; }
578 };
579
580
581 /// CheckChildTypeMatcher - This checks to see if a child node has the
582 /// specified type, if not it fails to match.
583 class CheckChildTypeMatcher : public Matcher {
584   unsigned ChildNo;
585   MVT::SimpleValueType Type;
586 public:
587   CheckChildTypeMatcher(unsigned childno, MVT::SimpleValueType type)
588     : Matcher(CheckChildType), ChildNo(childno), Type(type) {}
589
590   unsigned getChildNo() const { return ChildNo; }
591   MVT::SimpleValueType getType() const { return Type; }
592
593   static inline bool classof(const Matcher *N) {
594     return N->getKind() == CheckChildType;
595   }
596
597   virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
598
599 private:
600   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
601   virtual bool isEqualImpl(const Matcher *M) const {
602     return cast<CheckChildTypeMatcher>(M)->ChildNo == ChildNo &&
603            cast<CheckChildTypeMatcher>(M)->Type == Type;
604   }
605   virtual unsigned getHashImpl() const { return (Type << 3) | ChildNo; }
606   virtual bool isContradictoryImpl(const Matcher *M) const;
607 };
608
609
610 /// CheckIntegerMatcher - This checks to see if the current node is a
611 /// ConstantSDNode with the specified integer value, if not it fails to match.
612 class CheckIntegerMatcher : public Matcher {
613   int64_t Value;
614 public:
615   CheckIntegerMatcher(int64_t value)
616     : Matcher(CheckInteger), Value(value) {}
617
618   int64_t getValue() const { return Value; }
619
620   static inline bool classof(const Matcher *N) {
621     return N->getKind() == CheckInteger;
622   }
623
624   virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
625
626 private:
627   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
628   virtual bool isEqualImpl(const Matcher *M) const {
629     return cast<CheckIntegerMatcher>(M)->Value == Value;
630   }
631   virtual unsigned getHashImpl() const { return Value; }
632   virtual bool isContradictoryImpl(const Matcher *M) const;
633 };
634
635 /// CheckCondCodeMatcher - This checks to see if the current node is a
636 /// CondCodeSDNode with the specified condition, if not it fails to match.
637 class CheckCondCodeMatcher : public Matcher {
638   StringRef CondCodeName;
639 public:
640   CheckCondCodeMatcher(StringRef condcodename)
641     : Matcher(CheckCondCode), CondCodeName(condcodename) {}
642
643   StringRef getCondCodeName() const { return CondCodeName; }
644
645   static inline bool classof(const Matcher *N) {
646     return N->getKind() == CheckCondCode;
647   }
648
649   virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
650
651 private:
652   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
653   virtual bool isEqualImpl(const Matcher *M) const {
654     return cast<CheckCondCodeMatcher>(M)->CondCodeName == CondCodeName;
655   }
656   virtual unsigned getHashImpl() const;
657 };
658
659 /// CheckValueTypeMatcher - This checks to see if the current node is a
660 /// VTSDNode with the specified type, if not it fails to match.
661 class CheckValueTypeMatcher : public Matcher {
662   StringRef TypeName;
663 public:
664   CheckValueTypeMatcher(StringRef type_name)
665     : Matcher(CheckValueType), TypeName(type_name) {}
666
667   StringRef getTypeName() const { return TypeName; }
668
669   static inline bool classof(const Matcher *N) {
670     return N->getKind() == CheckValueType;
671   }
672
673   virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
674
675 private:
676   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
677   virtual bool isEqualImpl(const Matcher *M) const {
678     return cast<CheckValueTypeMatcher>(M)->TypeName == TypeName;
679   }
680   virtual unsigned getHashImpl() const;
681   bool isContradictoryImpl(const Matcher *M) const;
682 };
683
684
685
686 /// CheckComplexPatMatcher - This node runs the specified ComplexPattern on
687 /// the current node.
688 class CheckComplexPatMatcher : public Matcher {
689   const ComplexPattern &Pattern;
690
691   /// MatchNumber - This is the recorded nodes slot that contains the node we
692   /// want to match against.
693   unsigned MatchNumber;
694
695   /// Name - The name of the node we're matching, for comment emission.
696   std::string Name;
697
698   /// FirstResult - This is the first slot in the RecordedNodes list that the
699   /// result of the match populates.
700   unsigned FirstResult;
701 public:
702   CheckComplexPatMatcher(const ComplexPattern &pattern, unsigned matchnumber,
703                          const std::string &name, unsigned firstresult)
704     : Matcher(CheckComplexPat), Pattern(pattern), MatchNumber(matchnumber),
705       Name(name), FirstResult(firstresult) {}
706
707   const ComplexPattern &getPattern() const { return Pattern; }
708   unsigned getMatchNumber() const { return MatchNumber; }
709
710   const std::string getName() const { return Name; }
711   unsigned getFirstResult() const { return FirstResult; }
712
713   static inline bool classof(const Matcher *N) {
714     return N->getKind() == CheckComplexPat;
715   }
716
717   // Not safe to move a pattern predicate past a complex pattern.
718   virtual bool isSafeToReorderWithPatternPredicate() const { return false; }
719
720 private:
721   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
722   virtual bool isEqualImpl(const Matcher *M) const {
723     return &cast<CheckComplexPatMatcher>(M)->Pattern == &Pattern &&
724            cast<CheckComplexPatMatcher>(M)->MatchNumber == MatchNumber;
725   }
726   virtual unsigned getHashImpl() const {
727     return (unsigned)(intptr_t)&Pattern ^ MatchNumber;
728   }
729 };
730
731 /// CheckAndImmMatcher - This checks to see if the current node is an 'and'
732 /// with something equivalent to the specified immediate.
733 class CheckAndImmMatcher : public Matcher {
734   int64_t Value;
735 public:
736   CheckAndImmMatcher(int64_t value)
737     : Matcher(CheckAndImm), Value(value) {}
738
739   int64_t getValue() const { return Value; }
740
741   static inline bool classof(const Matcher *N) {
742     return N->getKind() == CheckAndImm;
743   }
744
745   virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
746
747 private:
748   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
749   virtual bool isEqualImpl(const Matcher *M) const {
750     return cast<CheckAndImmMatcher>(M)->Value == Value;
751   }
752   virtual unsigned getHashImpl() const { return Value; }
753 };
754
755 /// CheckOrImmMatcher - This checks to see if the current node is an 'and'
756 /// with something equivalent to the specified immediate.
757 class CheckOrImmMatcher : public Matcher {
758   int64_t Value;
759 public:
760   CheckOrImmMatcher(int64_t value)
761     : Matcher(CheckOrImm), Value(value) {}
762
763   int64_t getValue() const { return Value; }
764
765   static inline bool classof(const Matcher *N) {
766     return N->getKind() == CheckOrImm;
767   }
768
769   virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
770
771 private:
772   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
773   virtual bool isEqualImpl(const Matcher *M) const {
774     return cast<CheckOrImmMatcher>(M)->Value == Value;
775   }
776   virtual unsigned getHashImpl() const { return Value; }
777 };
778
779 /// CheckFoldableChainNodeMatcher - This checks to see if the current node
780 /// (which defines a chain operand) is safe to fold into a larger pattern.
781 class CheckFoldableChainNodeMatcher : public Matcher {
782 public:
783   CheckFoldableChainNodeMatcher()
784     : Matcher(CheckFoldableChainNode) {}
785
786   static inline bool classof(const Matcher *N) {
787     return N->getKind() == CheckFoldableChainNode;
788   }
789
790   virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
791
792 private:
793   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
794   virtual bool isEqualImpl(const Matcher *M) const { return true; }
795   virtual unsigned getHashImpl() const { return 0; }
796 };
797
798 /// EmitIntegerMatcher - This creates a new TargetConstant.
799 class EmitIntegerMatcher : public Matcher {
800   int64_t Val;
801   MVT::SimpleValueType VT;
802 public:
803   EmitIntegerMatcher(int64_t val, MVT::SimpleValueType vt)
804     : Matcher(EmitInteger), Val(val), VT(vt) {}
805
806   int64_t getValue() const { return Val; }
807   MVT::SimpleValueType getVT() const { return VT; }
808
809   static inline bool classof(const Matcher *N) {
810     return N->getKind() == EmitInteger;
811   }
812
813 private:
814   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
815   virtual bool isEqualImpl(const Matcher *M) const {
816     return cast<EmitIntegerMatcher>(M)->Val == Val &&
817            cast<EmitIntegerMatcher>(M)->VT == VT;
818   }
819   virtual unsigned getHashImpl() const { return (Val << 4) | VT; }
820 };
821
822 /// EmitStringIntegerMatcher - A target constant whose value is represented
823 /// by a string.
824 class EmitStringIntegerMatcher : public Matcher {
825   std::string Val;
826   MVT::SimpleValueType VT;
827 public:
828   EmitStringIntegerMatcher(const std::string &val, MVT::SimpleValueType vt)
829     : Matcher(EmitStringInteger), Val(val), VT(vt) {}
830
831   const std::string &getValue() const { return Val; }
832   MVT::SimpleValueType getVT() const { return VT; }
833
834   static inline bool classof(const Matcher *N) {
835     return N->getKind() == EmitStringInteger;
836   }
837
838 private:
839   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
840   virtual bool isEqualImpl(const Matcher *M) const {
841     return cast<EmitStringIntegerMatcher>(M)->Val == Val &&
842            cast<EmitStringIntegerMatcher>(M)->VT == VT;
843   }
844   virtual unsigned getHashImpl() const;
845 };
846
847 /// EmitRegisterMatcher - This creates a new TargetConstant.
848 class EmitRegisterMatcher : public Matcher {
849   /// Reg - The def for the register that we're emitting.  If this is null, then
850   /// this is a reference to zero_reg.
851   const CodeGenRegister *Reg;
852   MVT::SimpleValueType VT;
853 public:
854   EmitRegisterMatcher(const CodeGenRegister *reg, MVT::SimpleValueType vt)
855     : Matcher(EmitRegister), Reg(reg), VT(vt) {}
856
857   const CodeGenRegister *getReg() const { return Reg; }
858   MVT::SimpleValueType getVT() const { return VT; }
859
860   static inline bool classof(const Matcher *N) {
861     return N->getKind() == EmitRegister;
862   }
863
864 private:
865   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
866   virtual bool isEqualImpl(const Matcher *M) const {
867     return cast<EmitRegisterMatcher>(M)->Reg == Reg &&
868            cast<EmitRegisterMatcher>(M)->VT == VT;
869   }
870   virtual unsigned getHashImpl() const {
871     return ((unsigned)(intptr_t)Reg) << 4 | VT;
872   }
873 };
874
875 /// EmitConvertToTargetMatcher - Emit an operation that reads a specified
876 /// recorded node and converts it from being a ISD::Constant to
877 /// ISD::TargetConstant, likewise for ConstantFP.
878 class EmitConvertToTargetMatcher : public Matcher {
879   unsigned Slot;
880 public:
881   EmitConvertToTargetMatcher(unsigned slot)
882     : Matcher(EmitConvertToTarget), Slot(slot) {}
883
884   unsigned getSlot() const { return Slot; }
885
886   static inline bool classof(const Matcher *N) {
887     return N->getKind() == EmitConvertToTarget;
888   }
889
890 private:
891   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
892   virtual bool isEqualImpl(const Matcher *M) const {
893     return cast<EmitConvertToTargetMatcher>(M)->Slot == Slot;
894   }
895   virtual unsigned getHashImpl() const { return Slot; }
896 };
897
898 /// EmitMergeInputChainsMatcher - Emit a node that merges a list of input
899 /// chains together with a token factor.  The list of nodes are the nodes in the
900 /// matched pattern that have chain input/outputs.  This node adds all input
901 /// chains of these nodes if they are not themselves a node in the pattern.
902 class EmitMergeInputChainsMatcher : public Matcher {
903   SmallVector<unsigned, 3> ChainNodes;
904 public:
905   EmitMergeInputChainsMatcher(ArrayRef<unsigned> nodes)
906     : Matcher(EmitMergeInputChains), ChainNodes(nodes.begin(), nodes.end()) {}
907
908   unsigned getNumNodes() const { return ChainNodes.size(); }
909
910   unsigned getNode(unsigned i) const {
911     assert(i < ChainNodes.size());
912     return ChainNodes[i];
913   }
914
915   static inline bool classof(const Matcher *N) {
916     return N->getKind() == EmitMergeInputChains;
917   }
918
919 private:
920   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
921   virtual bool isEqualImpl(const Matcher *M) const {
922     return cast<EmitMergeInputChainsMatcher>(M)->ChainNodes == ChainNodes;
923   }
924   virtual unsigned getHashImpl() const;
925 };
926
927 /// EmitCopyToRegMatcher - Emit a CopyToReg node from a value to a physreg,
928 /// pushing the chain and glue results.
929 ///
930 class EmitCopyToRegMatcher : public Matcher {
931   unsigned SrcSlot; // Value to copy into the physreg.
932   Record *DestPhysReg;
933 public:
934   EmitCopyToRegMatcher(unsigned srcSlot, Record *destPhysReg)
935     : Matcher(EmitCopyToReg), SrcSlot(srcSlot), DestPhysReg(destPhysReg) {}
936
937   unsigned getSrcSlot() const { return SrcSlot; }
938   Record *getDestPhysReg() const { return DestPhysReg; }
939
940   static inline bool classof(const Matcher *N) {
941     return N->getKind() == EmitCopyToReg;
942   }
943
944 private:
945   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
946   virtual bool isEqualImpl(const Matcher *M) const {
947     return cast<EmitCopyToRegMatcher>(M)->SrcSlot == SrcSlot &&
948            cast<EmitCopyToRegMatcher>(M)->DestPhysReg == DestPhysReg;
949   }
950   virtual unsigned getHashImpl() const {
951     return SrcSlot ^ ((unsigned)(intptr_t)DestPhysReg << 4);
952   }
953 };
954
955
956
957 /// EmitNodeXFormMatcher - Emit an operation that runs an SDNodeXForm on a
958 /// recorded node and records the result.
959 class EmitNodeXFormMatcher : public Matcher {
960   unsigned Slot;
961   Record *NodeXForm;
962 public:
963   EmitNodeXFormMatcher(unsigned slot, Record *nodeXForm)
964     : Matcher(EmitNodeXForm), Slot(slot), NodeXForm(nodeXForm) {}
965
966   unsigned getSlot() const { return Slot; }
967   Record *getNodeXForm() const { return NodeXForm; }
968
969   static inline bool classof(const Matcher *N) {
970     return N->getKind() == EmitNodeXForm;
971   }
972
973 private:
974   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
975   virtual bool isEqualImpl(const Matcher *M) const {
976     return cast<EmitNodeXFormMatcher>(M)->Slot == Slot &&
977            cast<EmitNodeXFormMatcher>(M)->NodeXForm == NodeXForm;
978   }
979   virtual unsigned getHashImpl() const {
980     return Slot ^ ((unsigned)(intptr_t)NodeXForm << 4);
981   }
982 };
983
984 /// EmitNodeMatcherCommon - Common class shared between EmitNode and
985 /// MorphNodeTo.
986 class EmitNodeMatcherCommon : public Matcher {
987   std::string OpcodeName;
988   const SmallVector<MVT::SimpleValueType, 3> VTs;
989   const SmallVector<unsigned, 6> Operands;
990   bool HasChain, HasInGlue, HasOutGlue, HasMemRefs;
991
992   /// NumFixedArityOperands - If this is a fixed arity node, this is set to -1.
993   /// If this is a varidic node, this is set to the number of fixed arity
994   /// operands in the root of the pattern.  The rest are appended to this node.
995   int NumFixedArityOperands;
996 public:
997   EmitNodeMatcherCommon(const std::string &opcodeName,
998                         ArrayRef<MVT::SimpleValueType> vts,
999                         ArrayRef<unsigned> operands,
1000                         bool hasChain, bool hasInGlue, bool hasOutGlue,
1001                         bool hasmemrefs,
1002                         int numfixedarityoperands, bool isMorphNodeTo)
1003     : Matcher(isMorphNodeTo ? MorphNodeTo : EmitNode), OpcodeName(opcodeName),
1004       VTs(vts.begin(), vts.end()), Operands(operands.begin(), operands.end()),
1005       HasChain(hasChain), HasInGlue(hasInGlue), HasOutGlue(hasOutGlue),
1006       HasMemRefs(hasmemrefs), NumFixedArityOperands(numfixedarityoperands) {}
1007
1008   const std::string &getOpcodeName() const { return OpcodeName; }
1009
1010   unsigned getNumVTs() const { return VTs.size(); }
1011   MVT::SimpleValueType getVT(unsigned i) const {
1012     assert(i < VTs.size());
1013     return VTs[i];
1014   }
1015
1016   unsigned getNumOperands() const { return Operands.size(); }
1017   unsigned getOperand(unsigned i) const {
1018     assert(i < Operands.size());
1019     return Operands[i];
1020   }
1021
1022   const SmallVectorImpl<MVT::SimpleValueType> &getVTList() const { return VTs; }
1023   const SmallVectorImpl<unsigned> &getOperandList() const { return Operands; }
1024
1025
1026   bool hasChain() const { return HasChain; }
1027   bool hasInFlag() const { return HasInGlue; }
1028   bool hasOutFlag() const { return HasOutGlue; }
1029   bool hasMemRefs() const { return HasMemRefs; }
1030   int getNumFixedArityOperands() const { return NumFixedArityOperands; }
1031
1032   static inline bool classof(const Matcher *N) {
1033     return N->getKind() == EmitNode || N->getKind() == MorphNodeTo;
1034   }
1035
1036 private:
1037   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
1038   virtual bool isEqualImpl(const Matcher *M) const;
1039   virtual unsigned getHashImpl() const;
1040 };
1041
1042 /// EmitNodeMatcher - This signals a successful match and generates a node.
1043 class EmitNodeMatcher : public EmitNodeMatcherCommon {
1044   virtual void anchor();
1045   unsigned FirstResultSlot;
1046 public:
1047   EmitNodeMatcher(const std::string &opcodeName,
1048                   ArrayRef<MVT::SimpleValueType> vts,
1049                   ArrayRef<unsigned> operands,
1050                   bool hasChain, bool hasInFlag, bool hasOutFlag,
1051                   bool hasmemrefs,
1052                   int numfixedarityoperands, unsigned firstresultslot)
1053   : EmitNodeMatcherCommon(opcodeName, vts, operands, hasChain,
1054                           hasInFlag, hasOutFlag, hasmemrefs,
1055                           numfixedarityoperands, false),
1056     FirstResultSlot(firstresultslot) {}
1057
1058   unsigned getFirstResultSlot() const { return FirstResultSlot; }
1059
1060   static inline bool classof(const Matcher *N) {
1061     return N->getKind() == EmitNode;
1062   }
1063
1064 };
1065
1066 class MorphNodeToMatcher : public EmitNodeMatcherCommon {
1067   virtual void anchor();
1068   const PatternToMatch &Pattern;
1069 public:
1070   MorphNodeToMatcher(const std::string &opcodeName,
1071                      ArrayRef<MVT::SimpleValueType> vts,
1072                      ArrayRef<unsigned> operands,
1073                      bool hasChain, bool hasInFlag, bool hasOutFlag,
1074                      bool hasmemrefs,
1075                      int numfixedarityoperands, const PatternToMatch &pattern)
1076     : EmitNodeMatcherCommon(opcodeName, vts, operands, hasChain,
1077                             hasInFlag, hasOutFlag, hasmemrefs,
1078                             numfixedarityoperands, true),
1079       Pattern(pattern) {
1080   }
1081
1082   const PatternToMatch &getPattern() const { return Pattern; }
1083
1084   static inline bool classof(const Matcher *N) {
1085     return N->getKind() == MorphNodeTo;
1086   }
1087 };
1088
1089 /// MarkGlueResultsMatcher - This node indicates which non-root nodes in the
1090 /// pattern produce glue.  This allows CompleteMatchMatcher to update them
1091 /// with the output glue of the resultant code.
1092 class MarkGlueResultsMatcher : public Matcher {
1093   SmallVector<unsigned, 3> GlueResultNodes;
1094 public:
1095   MarkGlueResultsMatcher(ArrayRef<unsigned> nodes)
1096     : Matcher(MarkGlueResults), GlueResultNodes(nodes.begin(), nodes.end()) {}
1097
1098   unsigned getNumNodes() const { return GlueResultNodes.size(); }
1099
1100   unsigned getNode(unsigned i) const {
1101     assert(i < GlueResultNodes.size());
1102     return GlueResultNodes[i];
1103   }
1104
1105   static inline bool classof(const Matcher *N) {
1106     return N->getKind() == MarkGlueResults;
1107   }
1108
1109 private:
1110   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
1111   virtual bool isEqualImpl(const Matcher *M) const {
1112     return cast<MarkGlueResultsMatcher>(M)->GlueResultNodes == GlueResultNodes;
1113   }
1114   virtual unsigned getHashImpl() const;
1115 };
1116
1117 /// CompleteMatchMatcher - Complete a match by replacing the results of the
1118 /// pattern with the newly generated nodes.  This also prints a comment
1119 /// indicating the source and dest patterns.
1120 class CompleteMatchMatcher : public Matcher {
1121   SmallVector<unsigned, 2> Results;
1122   const PatternToMatch &Pattern;
1123 public:
1124   CompleteMatchMatcher(ArrayRef<unsigned> results,
1125                        const PatternToMatch &pattern)
1126   : Matcher(CompleteMatch), Results(results.begin(), results.end()),
1127     Pattern(pattern) {}
1128
1129   unsigned getNumResults() const { return Results.size(); }
1130   unsigned getResult(unsigned R) const { return Results[R]; }
1131   const PatternToMatch &getPattern() const { return Pattern; }
1132
1133   static inline bool classof(const Matcher *N) {
1134     return N->getKind() == CompleteMatch;
1135   }
1136
1137 private:
1138   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
1139   virtual bool isEqualImpl(const Matcher *M) const {
1140     return cast<CompleteMatchMatcher>(M)->Results == Results &&
1141           &cast<CompleteMatchMatcher>(M)->Pattern == &Pattern;
1142   }
1143   virtual unsigned getHashImpl() const;
1144 };
1145
1146 } // end namespace llvm
1147
1148 #endif