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