change the scope node to include a list of children to be checked
[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/CodeGen/ValueTypes.h"
14 #include "llvm/ADT/OwningPtr.h"
15 #include "llvm/ADT/StringRef.h"
16 #include "llvm/ADT/SmallVector.h"
17 #include "llvm/Support/Casting.h"
18
19 namespace llvm {
20   class CodeGenDAGPatterns;
21   class Matcher;
22   class PatternToMatch;
23   class raw_ostream;
24   class ComplexPattern;
25   class Record;
26
27 Matcher *ConvertPatternToMatcher(const PatternToMatch &Pattern,
28                                  const CodeGenDAGPatterns &CGP);
29 Matcher *OptimizeMatcher(Matcher *Matcher);
30 void EmitMatcherTable(const Matcher *Matcher, raw_ostream &OS);
31
32   
33 /// Matcher - Base class for all the the DAG ISel Matcher representation
34 /// nodes.
35 class Matcher {
36   // The next matcher node that is executed after this one.  Null if this is the
37   // last stage of a match.
38   OwningPtr<Matcher> Next;
39 public:
40   enum KindTy {
41     // Matcher state manipulation.
42     Scope,                // Push a checking scope.
43     RecordNode,           // Record the current node.
44     RecordChild,          // Record a child of the current node.
45     RecordMemRef,         // Record the memref in the current node.
46     CaptureFlagInput,     // If the current node has an input flag, save it.
47     MoveChild,            // Move current node to specified child.
48     MoveParent,           // Move current node to parent.
49     
50     // Predicate checking.
51     CheckSame,            // Fail if not same as prev match.
52     CheckPatternPredicate,
53     CheckPredicate,       // Fail if node predicate fails.
54     CheckOpcode,          // Fail if not opcode.
55     CheckMultiOpcode,     // Fail if not in opcode list.
56     CheckType,            // Fail if not correct type.
57     CheckChildType,       // Fail if child has wrong type.
58     CheckInteger,         // Fail if wrong val.
59     CheckCondCode,        // Fail if not condcode.
60     CheckValueType,
61     CheckComplexPat,
62     CheckAndImm,
63     CheckOrImm,
64     CheckFoldableChainNode,
65     CheckChainCompatible,
66     
67     // Node creation/emisssion.
68     EmitInteger,          // Create a TargetConstant
69     EmitStringInteger,    // Create a TargetConstant from a string.
70     EmitRegister,         // Create a register.
71     EmitConvertToTarget,  // Convert a imm/fpimm to target imm/fpimm
72     EmitMergeInputChains, // Merge together a chains for an input.
73     EmitCopyToReg,        // Emit a copytoreg into a physreg.
74     EmitNode,             // Create a DAG node
75     EmitNodeXForm,        // Run a SDNodeXForm
76     MarkFlagResults,      // Indicate which interior nodes have flag results.
77     CompleteMatch         // Finish a match and update the results.
78   };
79   const KindTy Kind;
80
81 protected:
82   Matcher(KindTy K) : Kind(K) {}
83 public:
84   virtual ~Matcher() {}
85   
86   KindTy getKind() const { return Kind; }
87
88   Matcher *getNext() { return Next.get(); }
89   const Matcher *getNext() const { return Next.get(); }
90   void setNext(Matcher *C) { Next.reset(C); }
91   Matcher *takeNext() { return Next.take(); }
92
93   OwningPtr<Matcher> &getNextPtr() { return Next; }
94   
95   static inline bool classof(const Matcher *) { return true; }
96   
97   bool isEqual(const Matcher *M) const {
98     if (getKind() != M->getKind()) return false;
99     return isEqualImpl(M);
100   }
101   
102   unsigned getHash() const {
103     return (getHashImpl() << 4) ^ getKind();
104   }
105   
106   void print(raw_ostream &OS, unsigned indent = 0) const;
107   void dump() const;
108 protected:
109   virtual void printImpl(raw_ostream &OS, unsigned indent) const = 0;
110   virtual bool isEqualImpl(const Matcher *M) const = 0;
111   virtual unsigned getHashImpl() const = 0;
112 };
113   
114 /// ScopeMatcher - This attempts to match each of its children to find the first
115 /// one that successfully matches.  If one child fails, it tries the next child.
116 /// If none of the children match then this check fails.  It never has a 'next'.
117 class ScopeMatcher : public Matcher {
118   SmallVector<Matcher*, 4> Children;
119 public:
120   ScopeMatcher(Matcher *const *children, unsigned numchildren)
121     : Matcher(Scope), Children(children, children+numchildren) {
122   }
123   virtual ~ScopeMatcher();
124   
125   unsigned getNumChildren() const { return Children.size(); }
126   
127   Matcher *getChild(unsigned i) { return Children[i]; }
128   const Matcher *getChild(unsigned i) const { return Children[i]; }
129   
130   void resetChild(unsigned i, Matcher *N) {
131     delete Children[i];
132     Children[i] = N;
133   }
134
135   Matcher *takeChild(unsigned i) {
136     Matcher *Res = Children[i];
137     Children[i] = 0;
138     return Res;
139   }
140
141   static inline bool classof(const Matcher *N) {
142     return N->getKind() == Scope;
143   }
144   
145 private:
146   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
147   virtual bool isEqualImpl(const Matcher *M) const { return false; }
148   virtual unsigned getHashImpl() const { return 12312; }
149 };
150
151 /// RecordMatcher - Save the current node in the operand list.
152 class RecordMatcher : public Matcher {
153   /// WhatFor - This is a string indicating why we're recording this.  This
154   /// should only be used for comment generation not anything semantic.
155   std::string WhatFor;
156 public:
157   RecordMatcher(const std::string &whatfor)
158     : Matcher(RecordNode), WhatFor(whatfor) {}
159   
160   const std::string &getWhatFor() const { return WhatFor; }
161   
162   static inline bool classof(const Matcher *N) {
163     return N->getKind() == RecordNode;
164   }
165   
166 private:
167   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
168   virtual bool isEqualImpl(const Matcher *M) const { return true; }
169   virtual unsigned getHashImpl() const { return 0; }
170 };
171   
172 /// RecordChildMatcher - Save a numbered child of the current node, or fail
173 /// the match if it doesn't exist.  This is logically equivalent to:
174 ///    MoveChild N + RecordNode + MoveParent.
175 class RecordChildMatcher : public Matcher {
176   unsigned ChildNo;
177   
178   /// WhatFor - This is a string indicating why we're recording this.  This
179   /// should only be used for comment generation not anything semantic.
180   std::string WhatFor;
181 public:
182   RecordChildMatcher(unsigned childno, const std::string &whatfor)
183   : Matcher(RecordChild), ChildNo(childno), WhatFor(whatfor) {}
184   
185   unsigned getChildNo() const { return ChildNo; }
186   const std::string &getWhatFor() const { return WhatFor; }
187   
188   static inline bool classof(const Matcher *N) {
189     return N->getKind() == RecordChild;
190   }
191   
192 private:
193   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
194   virtual bool isEqualImpl(const Matcher *M) const {
195     return cast<RecordChildMatcher>(M)->getChildNo() == getChildNo();
196   }
197   virtual unsigned getHashImpl() const { return getChildNo(); }
198 };
199   
200 /// RecordMemRefMatcher - Save the current node's memref.
201 class RecordMemRefMatcher : public Matcher {
202 public:
203   RecordMemRefMatcher() : Matcher(RecordMemRef) {}
204   
205   static inline bool classof(const Matcher *N) {
206     return N->getKind() == RecordMemRef;
207   }
208   
209 private:
210   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
211   virtual bool isEqualImpl(const Matcher *M) const { return true; }
212   virtual unsigned getHashImpl() const { return 0; }
213 };
214
215   
216 /// CaptureFlagInputMatcher - If the current record has a flag input, record
217 /// it so that it is used as an input to the generated code.
218 class CaptureFlagInputMatcher : public Matcher {
219 public:
220   CaptureFlagInputMatcher() : Matcher(CaptureFlagInput) {}
221   
222   static inline bool classof(const Matcher *N) {
223     return N->getKind() == CaptureFlagInput;
224   }
225   
226 private:
227   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
228   virtual bool isEqualImpl(const Matcher *M) const { return true; }
229   virtual unsigned getHashImpl() const { return 0; }
230 };
231   
232 /// MoveChildMatcher - This tells the interpreter to move into the
233 /// specified child node.
234 class MoveChildMatcher : public Matcher {
235   unsigned ChildNo;
236 public:
237   MoveChildMatcher(unsigned childNo) : Matcher(MoveChild), ChildNo(childNo) {}
238   
239   unsigned getChildNo() const { return ChildNo; }
240   
241   static inline bool classof(const Matcher *N) {
242     return N->getKind() == MoveChild;
243   }
244   
245 private:
246   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
247   virtual bool isEqualImpl(const Matcher *M) const {
248     return cast<MoveChildMatcher>(M)->getChildNo() == getChildNo();
249   }
250   virtual unsigned getHashImpl() const { return getChildNo(); }
251 };
252   
253 /// MoveParentMatcher - This tells the interpreter to move to the parent
254 /// of the current node.
255 class MoveParentMatcher : public Matcher {
256 public:
257   MoveParentMatcher() : Matcher(MoveParent) {}
258   
259   static inline bool classof(const Matcher *N) {
260     return N->getKind() == MoveParent;
261   }
262   
263 private:
264   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
265   virtual bool isEqualImpl(const Matcher *M) const { return true; }
266   virtual unsigned getHashImpl() const { return 0; }
267 };
268
269 /// CheckSameMatcher - This checks to see if this node is exactly the same
270 /// node as the specified match that was recorded with 'Record'.  This is used
271 /// when patterns have the same name in them, like '(mul GPR:$in, GPR:$in)'.
272 class CheckSameMatcher : public Matcher {
273   unsigned MatchNumber;
274 public:
275   CheckSameMatcher(unsigned matchnumber)
276     : Matcher(CheckSame), MatchNumber(matchnumber) {}
277   
278   unsigned getMatchNumber() const { return MatchNumber; }
279   
280   static inline bool classof(const Matcher *N) {
281     return N->getKind() == CheckSame;
282   }
283   
284 private:
285   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
286   virtual bool isEqualImpl(const Matcher *M) const {
287     return cast<CheckSameMatcher>(M)->getMatchNumber() == getMatchNumber();
288   }
289   virtual unsigned getHashImpl() const { return getMatchNumber(); }
290 };
291   
292 /// CheckPatternPredicateMatcher - This checks the target-specific predicate
293 /// to see if the entire pattern is capable of matching.  This predicate does
294 /// not take a node as input.  This is used for subtarget feature checks etc.
295 class CheckPatternPredicateMatcher : public Matcher {
296   std::string Predicate;
297 public:
298   CheckPatternPredicateMatcher(StringRef predicate)
299     : Matcher(CheckPatternPredicate), Predicate(predicate) {}
300   
301   StringRef getPredicate() const { return Predicate; }
302   
303   static inline bool classof(const Matcher *N) {
304     return N->getKind() == CheckPatternPredicate;
305   }
306   
307 private:
308   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
309   virtual bool isEqualImpl(const Matcher *M) const {
310     return cast<CheckPatternPredicateMatcher>(M)->getPredicate() == Predicate;
311   }
312   virtual unsigned getHashImpl() const;
313 };
314   
315 /// CheckPredicateMatcher - This checks the target-specific predicate to
316 /// see if the node is acceptable.
317 class CheckPredicateMatcher : public Matcher {
318   StringRef PredName;
319 public:
320   CheckPredicateMatcher(StringRef predname)
321     : Matcher(CheckPredicate), PredName(predname) {}
322   
323   StringRef getPredicateName() const { return PredName; }
324
325   static inline bool classof(const Matcher *N) {
326     return N->getKind() == CheckPredicate;
327   }
328   
329 private:
330   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
331   virtual bool isEqualImpl(const Matcher *M) const {
332     return cast<CheckPredicateMatcher>(M)->PredName == PredName;
333   }
334   virtual unsigned getHashImpl() const;
335 };
336   
337   
338 /// CheckOpcodeMatcher - This checks to see if the current node has the
339 /// specified opcode, if not it fails to match.
340 class CheckOpcodeMatcher : public Matcher {
341   StringRef OpcodeName;
342 public:
343   CheckOpcodeMatcher(StringRef opcodename)
344     : Matcher(CheckOpcode), OpcodeName(opcodename) {}
345   
346   StringRef getOpcodeName() const { return OpcodeName; }
347   
348   static inline bool classof(const Matcher *N) {
349     return N->getKind() == CheckOpcode;
350   }
351   
352 private:
353   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
354   virtual bool isEqualImpl(const Matcher *M) const {
355     return cast<CheckOpcodeMatcher>(M)->OpcodeName == OpcodeName;
356   }
357   virtual unsigned getHashImpl() const;
358 };
359   
360 /// CheckMultiOpcodeMatcher - This checks to see if the current node has one
361 /// of the specified opcode, if not it fails to match.
362 class CheckMultiOpcodeMatcher : public Matcher {
363   SmallVector<StringRef, 4> OpcodeNames;
364 public:
365   CheckMultiOpcodeMatcher(const StringRef *opcodes, unsigned numops)
366     : Matcher(CheckMultiOpcode), OpcodeNames(opcodes, opcodes+numops) {}
367   
368   unsigned getNumOpcodeNames() const { return OpcodeNames.size(); }
369   StringRef getOpcodeName(unsigned i) const { return OpcodeNames[i]; }
370   
371   static inline bool classof(const Matcher *N) {
372     return N->getKind() == CheckMultiOpcode;
373   }
374   
375 private:
376   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
377   virtual bool isEqualImpl(const Matcher *M) const {
378     return cast<CheckMultiOpcodeMatcher>(M)->OpcodeNames == OpcodeNames;
379   }
380   virtual unsigned getHashImpl() const;
381 };
382   
383   
384   
385 /// CheckTypeMatcher - This checks to see if the current node has the
386 /// specified type, if not it fails to match.
387 class CheckTypeMatcher : public Matcher {
388   MVT::SimpleValueType Type;
389 public:
390   CheckTypeMatcher(MVT::SimpleValueType type)
391     : Matcher(CheckType), Type(type) {}
392   
393   MVT::SimpleValueType getType() const { return Type; }
394   
395   static inline bool classof(const Matcher *N) {
396     return N->getKind() == CheckType;
397   }
398   
399 private:
400   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
401   virtual bool isEqualImpl(const Matcher *M) const {
402     return cast<CheckTypeMatcher>(this)->Type == Type;
403   }
404   virtual unsigned getHashImpl() const { return Type; }
405 };
406   
407 /// CheckChildTypeMatcher - This checks to see if a child node has the
408 /// specified type, if not it fails to match.
409 class CheckChildTypeMatcher : public Matcher {
410   unsigned ChildNo;
411   MVT::SimpleValueType Type;
412 public:
413   CheckChildTypeMatcher(unsigned childno, MVT::SimpleValueType type)
414     : Matcher(CheckChildType), ChildNo(childno), Type(type) {}
415   
416   unsigned getChildNo() const { return ChildNo; }
417   MVT::SimpleValueType getType() const { return Type; }
418   
419   static inline bool classof(const Matcher *N) {
420     return N->getKind() == CheckChildType;
421   }
422   
423 private:
424   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
425   virtual bool isEqualImpl(const Matcher *M) const {
426     return cast<CheckChildTypeMatcher>(M)->ChildNo == ChildNo &&
427            cast<CheckChildTypeMatcher>(M)->Type == Type;
428   }
429   virtual unsigned getHashImpl() const { return (Type << 3) | ChildNo; }
430 };
431   
432
433 /// CheckIntegerMatcher - This checks to see if the current node is a
434 /// ConstantSDNode with the specified integer value, if not it fails to match.
435 class CheckIntegerMatcher : public Matcher {
436   int64_t Value;
437 public:
438   CheckIntegerMatcher(int64_t value)
439     : Matcher(CheckInteger), Value(value) {}
440   
441   int64_t getValue() const { return Value; }
442   
443   static inline bool classof(const Matcher *N) {
444     return N->getKind() == CheckInteger;
445   }
446   
447 private:
448   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
449   virtual bool isEqualImpl(const Matcher *M) const {
450     return cast<CheckIntegerMatcher>(M)->Value == Value;
451   }
452   virtual unsigned getHashImpl() const { return Value; }
453 };
454   
455 /// CheckCondCodeMatcher - This checks to see if the current node is a
456 /// CondCodeSDNode with the specified condition, if not it fails to match.
457 class CheckCondCodeMatcher : public Matcher {
458   StringRef CondCodeName;
459 public:
460   CheckCondCodeMatcher(StringRef condcodename)
461     : Matcher(CheckCondCode), CondCodeName(condcodename) {}
462   
463   StringRef getCondCodeName() const { return CondCodeName; }
464   
465   static inline bool classof(const Matcher *N) {
466     return N->getKind() == CheckCondCode;
467   }
468   
469 private:
470   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
471   virtual bool isEqualImpl(const Matcher *M) const {
472     return cast<CheckCondCodeMatcher>(M)->CondCodeName == CondCodeName;
473   }
474   virtual unsigned getHashImpl() const;
475 };
476   
477 /// CheckValueTypeMatcher - This checks to see if the current node is a
478 /// VTSDNode with the specified type, if not it fails to match.
479 class CheckValueTypeMatcher : public Matcher {
480   StringRef TypeName;
481 public:
482   CheckValueTypeMatcher(StringRef type_name)
483     : Matcher(CheckValueType), TypeName(type_name) {}
484   
485   StringRef getTypeName() const { return TypeName; }
486
487   static inline bool classof(const Matcher *N) {
488     return N->getKind() == CheckValueType;
489   }
490   
491 private:
492   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
493   virtual bool isEqualImpl(const Matcher *M) const {
494     return cast<CheckValueTypeMatcher>(M)->TypeName == TypeName;
495   }
496   virtual unsigned getHashImpl() const;
497 };
498   
499   
500   
501 /// CheckComplexPatMatcher - This node runs the specified ComplexPattern on
502 /// the current node.
503 class CheckComplexPatMatcher : public Matcher {
504   const ComplexPattern &Pattern;
505 public:
506   CheckComplexPatMatcher(const ComplexPattern &pattern)
507     : Matcher(CheckComplexPat), Pattern(pattern) {}
508   
509   const ComplexPattern &getPattern() const { return Pattern; }
510   
511   static inline bool classof(const Matcher *N) {
512     return N->getKind() == CheckComplexPat;
513   }
514   
515 private:
516   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
517   virtual bool isEqualImpl(const Matcher *M) const {
518     return &cast<CheckComplexPatMatcher>(M)->Pattern == &Pattern;
519   }
520   virtual unsigned getHashImpl() const {
521     return (unsigned)(intptr_t)&Pattern;
522   }
523 };
524   
525 /// CheckAndImmMatcher - This checks to see if the current node is an 'and'
526 /// with something equivalent to the specified immediate.
527 class CheckAndImmMatcher : public Matcher {
528   int64_t Value;
529 public:
530   CheckAndImmMatcher(int64_t value)
531     : Matcher(CheckAndImm), Value(value) {}
532   
533   int64_t getValue() const { return Value; }
534   
535   static inline bool classof(const Matcher *N) {
536     return N->getKind() == CheckAndImm;
537   }
538   
539 private:
540   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
541   virtual bool isEqualImpl(const Matcher *M) const {
542     return cast<CheckAndImmMatcher>(M)->Value == Value;
543   }
544   virtual unsigned getHashImpl() const { return Value; }
545 };
546
547 /// CheckOrImmMatcher - This checks to see if the current node is an 'and'
548 /// with something equivalent to the specified immediate.
549 class CheckOrImmMatcher : public Matcher {
550   int64_t Value;
551 public:
552   CheckOrImmMatcher(int64_t value)
553     : Matcher(CheckOrImm), Value(value) {}
554   
555   int64_t getValue() const { return Value; }
556
557   static inline bool classof(const Matcher *N) {
558     return N->getKind() == CheckOrImm;
559   }
560   
561 private:
562   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
563   virtual bool isEqualImpl(const Matcher *M) const {
564     return cast<CheckOrImmMatcher>(M)->Value == Value;
565   }
566   virtual unsigned getHashImpl() const { return Value; }
567 };
568
569 /// CheckFoldableChainNodeMatcher - This checks to see if the current node
570 /// (which defines a chain operand) is safe to fold into a larger pattern.
571 class CheckFoldableChainNodeMatcher : public Matcher {
572 public:
573   CheckFoldableChainNodeMatcher()
574     : Matcher(CheckFoldableChainNode) {}
575   
576   static inline bool classof(const Matcher *N) {
577     return N->getKind() == CheckFoldableChainNode;
578   }
579   
580 private:
581   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
582   virtual bool isEqualImpl(const Matcher *M) const { return true; }
583   virtual unsigned getHashImpl() const { return 0; }
584 };
585
586 /// CheckChainCompatibleMatcher - Verify that the current node's chain
587 /// operand is 'compatible' with the specified recorded node's.
588 class CheckChainCompatibleMatcher : public Matcher {
589   unsigned PreviousOp;
590 public:
591   CheckChainCompatibleMatcher(unsigned previousop)
592     : Matcher(CheckChainCompatible), PreviousOp(previousop) {}
593   
594   unsigned getPreviousOp() const { return PreviousOp; }
595   
596   static inline bool classof(const Matcher *N) {
597     return N->getKind() == CheckChainCompatible;
598   }
599   
600 private:
601   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
602   virtual bool isEqualImpl(const Matcher *M) const {
603     return cast<CheckChainCompatibleMatcher>(this)->PreviousOp == PreviousOp;
604   }
605   virtual unsigned getHashImpl() const { return PreviousOp; }
606 };
607   
608 /// EmitIntegerMatcher - This creates a new TargetConstant.
609 class EmitIntegerMatcher : public Matcher {
610   int64_t Val;
611   MVT::SimpleValueType VT;
612 public:
613   EmitIntegerMatcher(int64_t val, MVT::SimpleValueType vt)
614     : Matcher(EmitInteger), Val(val), VT(vt) {}
615   
616   int64_t getValue() const { return Val; }
617   MVT::SimpleValueType getVT() const { return VT; }
618   
619   static inline bool classof(const Matcher *N) {
620     return N->getKind() == EmitInteger;
621   }
622   
623 private:
624   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
625   virtual bool isEqualImpl(const Matcher *M) const {
626     return cast<EmitIntegerMatcher>(M)->Val == Val &&
627            cast<EmitIntegerMatcher>(M)->VT == VT;
628   }
629   virtual unsigned getHashImpl() const { return (Val << 4) | VT; }
630 };
631
632 /// EmitStringIntegerMatcher - A target constant whose value is represented
633 /// by a string.
634 class EmitStringIntegerMatcher : public Matcher {
635   std::string Val;
636   MVT::SimpleValueType VT;
637 public:
638   EmitStringIntegerMatcher(const std::string &val, MVT::SimpleValueType vt)
639     : Matcher(EmitStringInteger), Val(val), VT(vt) {}
640   
641   const std::string &getValue() const { return Val; }
642   MVT::SimpleValueType getVT() const { return VT; }
643   
644   static inline bool classof(const Matcher *N) {
645     return N->getKind() == EmitStringInteger;
646   }
647   
648 private:
649   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
650   virtual bool isEqualImpl(const Matcher *M) const {
651     return cast<EmitStringIntegerMatcher>(M)->Val == Val &&
652            cast<EmitStringIntegerMatcher>(M)->VT == VT;
653   }
654   virtual unsigned getHashImpl() const;
655 };
656   
657 /// EmitRegisterMatcher - This creates a new TargetConstant.
658 class EmitRegisterMatcher : public Matcher {
659   /// Reg - The def for the register that we're emitting.  If this is null, then
660   /// this is a reference to zero_reg.
661   Record *Reg;
662   MVT::SimpleValueType VT;
663 public:
664   EmitRegisterMatcher(Record *reg, MVT::SimpleValueType vt)
665     : Matcher(EmitRegister), Reg(reg), VT(vt) {}
666   
667   Record *getReg() const { return Reg; }
668   MVT::SimpleValueType getVT() const { return VT; }
669   
670   static inline bool classof(const Matcher *N) {
671     return N->getKind() == EmitRegister;
672   }
673   
674 private:
675   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
676   virtual bool isEqualImpl(const Matcher *M) const {
677     return cast<EmitRegisterMatcher>(M)->Reg == Reg &&
678            cast<EmitRegisterMatcher>(M)->VT == VT;
679   }
680   virtual unsigned getHashImpl() const {
681     return ((unsigned)(intptr_t)Reg) << 4 | VT;
682   }
683 };
684
685 /// EmitConvertToTargetMatcher - Emit an operation that reads a specified
686 /// recorded node and converts it from being a ISD::Constant to
687 /// ISD::TargetConstant, likewise for ConstantFP.
688 class EmitConvertToTargetMatcher : public Matcher {
689   unsigned Slot;
690 public:
691   EmitConvertToTargetMatcher(unsigned slot)
692     : Matcher(EmitConvertToTarget), Slot(slot) {}
693   
694   unsigned getSlot() const { return Slot; }
695   
696   static inline bool classof(const Matcher *N) {
697     return N->getKind() == EmitConvertToTarget;
698   }
699   
700 private:
701   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
702   virtual bool isEqualImpl(const Matcher *M) const {
703     return cast<EmitConvertToTargetMatcher>(M)->Slot == Slot;
704   }
705   virtual unsigned getHashImpl() const { return Slot; }
706 };
707   
708 /// EmitMergeInputChainsMatcher - Emit a node that merges a list of input
709 /// chains together with a token factor.  The list of nodes are the nodes in the
710 /// matched pattern that have chain input/outputs.  This node adds all input
711 /// chains of these nodes if they are not themselves a node in the pattern.
712 class EmitMergeInputChainsMatcher : public Matcher {
713   SmallVector<unsigned, 3> ChainNodes;
714 public:
715   EmitMergeInputChainsMatcher(const unsigned *nodes, unsigned NumNodes)
716     : Matcher(EmitMergeInputChains), ChainNodes(nodes, nodes+NumNodes) {}
717   
718   unsigned getNumNodes() const { return ChainNodes.size(); }
719   
720   unsigned getNode(unsigned i) const {
721     assert(i < ChainNodes.size());
722     return ChainNodes[i];
723   }  
724   
725   static inline bool classof(const Matcher *N) {
726     return N->getKind() == EmitMergeInputChains;
727   }
728   
729 private:
730   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
731   virtual bool isEqualImpl(const Matcher *M) const {
732     return cast<EmitMergeInputChainsMatcher>(M)->ChainNodes == ChainNodes;
733   }
734   virtual unsigned getHashImpl() const;
735 };
736   
737 /// EmitCopyToRegMatcher - Emit a CopyToReg node from a value to a physreg,
738 /// pushing the chain and flag results.
739 ///
740 class EmitCopyToRegMatcher : public Matcher {
741   unsigned SrcSlot; // Value to copy into the physreg.
742   Record *DestPhysReg;
743 public:
744   EmitCopyToRegMatcher(unsigned srcSlot, Record *destPhysReg)
745     : Matcher(EmitCopyToReg), SrcSlot(srcSlot), DestPhysReg(destPhysReg) {}
746   
747   unsigned getSrcSlot() const { return SrcSlot; }
748   Record *getDestPhysReg() const { return DestPhysReg; }
749   
750   static inline bool classof(const Matcher *N) {
751     return N->getKind() == EmitCopyToReg;
752   }
753   
754 private:
755   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
756   virtual bool isEqualImpl(const Matcher *M) const {
757     return cast<EmitCopyToRegMatcher>(M)->SrcSlot == SrcSlot &&
758            cast<EmitCopyToRegMatcher>(M)->DestPhysReg == DestPhysReg; 
759   }
760   virtual unsigned getHashImpl() const {
761     return SrcSlot ^ ((unsigned)(intptr_t)DestPhysReg << 4);
762   }
763 };
764   
765     
766   
767 /// EmitNodeXFormMatcher - Emit an operation that runs an SDNodeXForm on a
768 /// recorded node and records the result.
769 class EmitNodeXFormMatcher : public Matcher {
770   unsigned Slot;
771   Record *NodeXForm;
772 public:
773   EmitNodeXFormMatcher(unsigned slot, Record *nodeXForm)
774     : Matcher(EmitNodeXForm), Slot(slot), NodeXForm(nodeXForm) {}
775   
776   unsigned getSlot() const { return Slot; }
777   Record *getNodeXForm() const { return NodeXForm; }
778   
779   static inline bool classof(const Matcher *N) {
780     return N->getKind() == EmitNodeXForm;
781   }
782   
783 private:
784   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
785   virtual bool isEqualImpl(const Matcher *M) const {
786     return cast<EmitNodeXFormMatcher>(M)->Slot == Slot &&
787            cast<EmitNodeXFormMatcher>(M)->NodeXForm == NodeXForm; 
788   }
789   virtual unsigned getHashImpl() const {
790     return Slot ^ ((unsigned)(intptr_t)NodeXForm << 4);
791   }
792 };
793   
794 /// EmitNodeMatcher - This signals a successful match and generates a node.
795 class EmitNodeMatcher : public Matcher {
796   std::string OpcodeName;
797   const SmallVector<MVT::SimpleValueType, 3> VTs;
798   const SmallVector<unsigned, 6> Operands;
799   bool HasChain, HasFlag, HasMemRefs;
800   
801   /// NumFixedArityOperands - If this is a fixed arity node, this is set to -1.
802   /// If this is a varidic node, this is set to the number of fixed arity
803   /// operands in the root of the pattern.  The rest are appended to this node.
804   int NumFixedArityOperands;
805 public:
806   EmitNodeMatcher(const std::string &opcodeName,
807                   const MVT::SimpleValueType *vts, unsigned numvts,
808                   const unsigned *operands, unsigned numops,
809                   bool hasChain, bool hasFlag, bool hasmemrefs,
810                   int numfixedarityoperands)
811     : Matcher(EmitNode), OpcodeName(opcodeName),
812       VTs(vts, vts+numvts), Operands(operands, operands+numops),
813       HasChain(hasChain), HasFlag(hasFlag), HasMemRefs(hasmemrefs),
814       NumFixedArityOperands(numfixedarityoperands) {}
815   
816   const std::string &getOpcodeName() const { return OpcodeName; }
817   
818   unsigned getNumVTs() const { return VTs.size(); }
819   MVT::SimpleValueType getVT(unsigned i) const {
820     assert(i < VTs.size());
821     return VTs[i];
822   }
823   
824   unsigned getNumOperands() const { return Operands.size(); }
825   unsigned getOperand(unsigned i) const {
826     assert(i < Operands.size());
827     return Operands[i];
828   }  
829   
830   bool hasChain() const { return HasChain; }
831   bool hasFlag() const { return HasFlag; }
832   bool hasMemRefs() const { return HasMemRefs; }
833   int getNumFixedArityOperands() const { return NumFixedArityOperands; }
834   
835   static inline bool classof(const Matcher *N) {
836     return N->getKind() == EmitNode;
837   }
838   
839 private:
840   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
841   virtual bool isEqualImpl(const Matcher *M) const;
842   virtual unsigned getHashImpl() const;
843 };
844   
845 /// MarkFlagResultsMatcher - This node indicates which non-root nodes in the
846 /// pattern produce flags.  This allows CompleteMatchMatcher to update them
847 /// with the output flag of the resultant code.
848 class MarkFlagResultsMatcher : public Matcher {
849   SmallVector<unsigned, 3> FlagResultNodes;
850 public:
851   MarkFlagResultsMatcher(const unsigned *nodes, unsigned NumNodes)
852     : Matcher(MarkFlagResults), FlagResultNodes(nodes, nodes+NumNodes) {}
853   
854   unsigned getNumNodes() const { return FlagResultNodes.size(); }
855   
856   unsigned getNode(unsigned i) const {
857     assert(i < FlagResultNodes.size());
858     return FlagResultNodes[i];
859   }  
860   
861   static inline bool classof(const Matcher *N) {
862     return N->getKind() == MarkFlagResults;
863   }
864   
865 private:
866   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
867   virtual bool isEqualImpl(const Matcher *M) const {
868     return cast<MarkFlagResultsMatcher>(M)->FlagResultNodes == FlagResultNodes;
869   }
870   virtual unsigned getHashImpl() const;
871 };
872
873 /// CompleteMatchMatcher - Complete a match by replacing the results of the
874 /// pattern with the newly generated nodes.  This also prints a comment
875 /// indicating the source and dest patterns.
876 class CompleteMatchMatcher : public Matcher {
877   SmallVector<unsigned, 2> Results;
878   const PatternToMatch &Pattern;
879 public:
880   CompleteMatchMatcher(const unsigned *results, unsigned numresults,
881                            const PatternToMatch &pattern)
882   : Matcher(CompleteMatch), Results(results, results+numresults),
883     Pattern(pattern) {}
884
885   unsigned getNumResults() const { return Results.size(); }
886   unsigned getResult(unsigned R) const { return Results[R]; }
887   const PatternToMatch &getPattern() const { return Pattern; }
888   
889   static inline bool classof(const Matcher *N) {
890     return N->getKind() == CompleteMatch;
891   }
892   
893 private:
894   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
895   virtual bool isEqualImpl(const Matcher *M) const {
896     return cast<CompleteMatchMatcher>(M)->Results == Results &&
897           &cast<CompleteMatchMatcher>(M)->Pattern == &Pattern;
898   }
899   virtual unsigned getHashImpl() const;
900 };
901   
902 } // end namespace llvm
903
904 #endif