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