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