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