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