teach the optimizer that opcode == ISD::STORE is 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   virtual bool isContradictoryImpl(const Matcher *M) const;
515 };
516   
517 /// CheckCondCodeMatcher - This checks to see if the current node is a
518 /// CondCodeSDNode with the specified condition, if not it fails to match.
519 class CheckCondCodeMatcher : public Matcher {
520   StringRef CondCodeName;
521 public:
522   CheckCondCodeMatcher(StringRef condcodename)
523     : Matcher(CheckCondCode), CondCodeName(condcodename) {}
524   
525   StringRef getCondCodeName() const { return CondCodeName; }
526   
527   static inline bool classof(const Matcher *N) {
528     return N->getKind() == CheckCondCode;
529   }
530   
531   virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
532
533 private:
534   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
535   virtual bool isEqualImpl(const Matcher *M) const {
536     return cast<CheckCondCodeMatcher>(M)->CondCodeName == CondCodeName;
537   }
538   virtual unsigned getHashImpl() const;
539 };
540   
541 /// CheckValueTypeMatcher - This checks to see if the current node is a
542 /// VTSDNode with the specified type, if not it fails to match.
543 class CheckValueTypeMatcher : public Matcher {
544   StringRef TypeName;
545 public:
546   CheckValueTypeMatcher(StringRef type_name)
547     : Matcher(CheckValueType), TypeName(type_name) {}
548   
549   StringRef getTypeName() const { return TypeName; }
550
551   static inline bool classof(const Matcher *N) {
552     return N->getKind() == CheckValueType;
553   }
554   
555   virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
556
557 private:
558   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
559   virtual bool isEqualImpl(const Matcher *M) const {
560     return cast<CheckValueTypeMatcher>(M)->TypeName == TypeName;
561   }
562   virtual unsigned getHashImpl() const;
563 };
564   
565   
566   
567 /// CheckComplexPatMatcher - This node runs the specified ComplexPattern on
568 /// the current node.
569 class CheckComplexPatMatcher : public Matcher {
570   const ComplexPattern &Pattern;
571 public:
572   CheckComplexPatMatcher(const ComplexPattern &pattern)
573     : Matcher(CheckComplexPat), Pattern(pattern) {}
574   
575   const ComplexPattern &getPattern() const { return Pattern; }
576   
577   static inline bool classof(const Matcher *N) {
578     return N->getKind() == CheckComplexPat;
579   }
580   
581   // Not safe to move a pattern predicate past a complex pattern.
582   virtual bool isSafeToReorderWithPatternPredicate() const { return false; }
583
584 private:
585   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
586   virtual bool isEqualImpl(const Matcher *M) const {
587     return &cast<CheckComplexPatMatcher>(M)->Pattern == &Pattern;
588   }
589   virtual unsigned getHashImpl() const {
590     return (unsigned)(intptr_t)&Pattern;
591   }
592 };
593   
594 /// CheckAndImmMatcher - This checks to see if the current node is an 'and'
595 /// with something equivalent to the specified immediate.
596 class CheckAndImmMatcher : public Matcher {
597   int64_t Value;
598 public:
599   CheckAndImmMatcher(int64_t value)
600     : Matcher(CheckAndImm), Value(value) {}
601   
602   int64_t getValue() const { return Value; }
603   
604   static inline bool classof(const Matcher *N) {
605     return N->getKind() == CheckAndImm;
606   }
607   
608   virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
609
610 private:
611   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
612   virtual bool isEqualImpl(const Matcher *M) const {
613     return cast<CheckAndImmMatcher>(M)->Value == Value;
614   }
615   virtual unsigned getHashImpl() const { return Value; }
616 };
617
618 /// CheckOrImmMatcher - This checks to see if the current node is an 'and'
619 /// with something equivalent to the specified immediate.
620 class CheckOrImmMatcher : public Matcher {
621   int64_t Value;
622 public:
623   CheckOrImmMatcher(int64_t value)
624     : Matcher(CheckOrImm), Value(value) {}
625   
626   int64_t getValue() const { return Value; }
627
628   static inline bool classof(const Matcher *N) {
629     return N->getKind() == CheckOrImm;
630   }
631   
632   virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
633
634 private:
635   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
636   virtual bool isEqualImpl(const Matcher *M) const {
637     return cast<CheckOrImmMatcher>(M)->Value == Value;
638   }
639   virtual unsigned getHashImpl() const { return Value; }
640 };
641
642 /// CheckFoldableChainNodeMatcher - This checks to see if the current node
643 /// (which defines a chain operand) is safe to fold into a larger pattern.
644 class CheckFoldableChainNodeMatcher : public Matcher {
645 public:
646   CheckFoldableChainNodeMatcher()
647     : Matcher(CheckFoldableChainNode) {}
648   
649   static inline bool classof(const Matcher *N) {
650     return N->getKind() == CheckFoldableChainNode;
651   }
652   
653   virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
654
655 private:
656   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
657   virtual bool isEqualImpl(const Matcher *M) const { return true; }
658   virtual unsigned getHashImpl() const { return 0; }
659 };
660
661 /// CheckChainCompatibleMatcher - Verify that the current node's chain
662 /// operand is 'compatible' with the specified recorded node's.
663 class CheckChainCompatibleMatcher : public Matcher {
664   unsigned PreviousOp;
665 public:
666   CheckChainCompatibleMatcher(unsigned previousop)
667     : Matcher(CheckChainCompatible), PreviousOp(previousop) {}
668   
669   unsigned getPreviousOp() const { return PreviousOp; }
670   
671   static inline bool classof(const Matcher *N) {
672     return N->getKind() == CheckChainCompatible;
673   }
674   
675   virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
676
677 private:
678   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
679   virtual bool isEqualImpl(const Matcher *M) const {
680     return cast<CheckChainCompatibleMatcher>(M)->PreviousOp == PreviousOp;
681   }
682   virtual unsigned getHashImpl() const { return PreviousOp; }
683 };
684   
685 /// EmitIntegerMatcher - This creates a new TargetConstant.
686 class EmitIntegerMatcher : public Matcher {
687   int64_t Val;
688   MVT::SimpleValueType VT;
689 public:
690   EmitIntegerMatcher(int64_t val, MVT::SimpleValueType vt)
691     : Matcher(EmitInteger), Val(val), VT(vt) {}
692   
693   int64_t getValue() const { return Val; }
694   MVT::SimpleValueType getVT() const { return VT; }
695   
696   static inline bool classof(const Matcher *N) {
697     return N->getKind() == EmitInteger;
698   }
699   
700 private:
701   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
702   virtual bool isEqualImpl(const Matcher *M) const {
703     return cast<EmitIntegerMatcher>(M)->Val == Val &&
704            cast<EmitIntegerMatcher>(M)->VT == VT;
705   }
706   virtual unsigned getHashImpl() const { return (Val << 4) | VT; }
707 };
708
709 /// EmitStringIntegerMatcher - A target constant whose value is represented
710 /// by a string.
711 class EmitStringIntegerMatcher : public Matcher {
712   std::string Val;
713   MVT::SimpleValueType VT;
714 public:
715   EmitStringIntegerMatcher(const std::string &val, MVT::SimpleValueType vt)
716     : Matcher(EmitStringInteger), Val(val), VT(vt) {}
717   
718   const std::string &getValue() const { return Val; }
719   MVT::SimpleValueType getVT() const { return VT; }
720   
721   static inline bool classof(const Matcher *N) {
722     return N->getKind() == EmitStringInteger;
723   }
724   
725 private:
726   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
727   virtual bool isEqualImpl(const Matcher *M) const {
728     return cast<EmitStringIntegerMatcher>(M)->Val == Val &&
729            cast<EmitStringIntegerMatcher>(M)->VT == VT;
730   }
731   virtual unsigned getHashImpl() const;
732 };
733   
734 /// EmitRegisterMatcher - This creates a new TargetConstant.
735 class EmitRegisterMatcher : public Matcher {
736   /// Reg - The def for the register that we're emitting.  If this is null, then
737   /// this is a reference to zero_reg.
738   Record *Reg;
739   MVT::SimpleValueType VT;
740 public:
741   EmitRegisterMatcher(Record *reg, MVT::SimpleValueType vt)
742     : Matcher(EmitRegister), Reg(reg), VT(vt) {}
743   
744   Record *getReg() const { return Reg; }
745   MVT::SimpleValueType getVT() const { return VT; }
746   
747   static inline bool classof(const Matcher *N) {
748     return N->getKind() == EmitRegister;
749   }
750   
751 private:
752   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
753   virtual bool isEqualImpl(const Matcher *M) const {
754     return cast<EmitRegisterMatcher>(M)->Reg == Reg &&
755            cast<EmitRegisterMatcher>(M)->VT == VT;
756   }
757   virtual unsigned getHashImpl() const {
758     return ((unsigned)(intptr_t)Reg) << 4 | VT;
759   }
760 };
761
762 /// EmitConvertToTargetMatcher - Emit an operation that reads a specified
763 /// recorded node and converts it from being a ISD::Constant to
764 /// ISD::TargetConstant, likewise for ConstantFP.
765 class EmitConvertToTargetMatcher : public Matcher {
766   unsigned Slot;
767 public:
768   EmitConvertToTargetMatcher(unsigned slot)
769     : Matcher(EmitConvertToTarget), Slot(slot) {}
770   
771   unsigned getSlot() const { return Slot; }
772   
773   static inline bool classof(const Matcher *N) {
774     return N->getKind() == EmitConvertToTarget;
775   }
776   
777 private:
778   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
779   virtual bool isEqualImpl(const Matcher *M) const {
780     return cast<EmitConvertToTargetMatcher>(M)->Slot == Slot;
781   }
782   virtual unsigned getHashImpl() const { return Slot; }
783 };
784   
785 /// EmitMergeInputChainsMatcher - Emit a node that merges a list of input
786 /// chains together with a token factor.  The list of nodes are the nodes in the
787 /// matched pattern that have chain input/outputs.  This node adds all input
788 /// chains of these nodes if they are not themselves a node in the pattern.
789 class EmitMergeInputChainsMatcher : public Matcher {
790   SmallVector<unsigned, 3> ChainNodes;
791 public:
792   EmitMergeInputChainsMatcher(const unsigned *nodes, unsigned NumNodes)
793     : Matcher(EmitMergeInputChains), ChainNodes(nodes, nodes+NumNodes) {}
794   
795   unsigned getNumNodes() const { return ChainNodes.size(); }
796   
797   unsigned getNode(unsigned i) const {
798     assert(i < ChainNodes.size());
799     return ChainNodes[i];
800   }  
801   
802   static inline bool classof(const Matcher *N) {
803     return N->getKind() == EmitMergeInputChains;
804   }
805   
806 private:
807   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
808   virtual bool isEqualImpl(const Matcher *M) const {
809     return cast<EmitMergeInputChainsMatcher>(M)->ChainNodes == ChainNodes;
810   }
811   virtual unsigned getHashImpl() const;
812 };
813   
814 /// EmitCopyToRegMatcher - Emit a CopyToReg node from a value to a physreg,
815 /// pushing the chain and flag results.
816 ///
817 class EmitCopyToRegMatcher : public Matcher {
818   unsigned SrcSlot; // Value to copy into the physreg.
819   Record *DestPhysReg;
820 public:
821   EmitCopyToRegMatcher(unsigned srcSlot, Record *destPhysReg)
822     : Matcher(EmitCopyToReg), SrcSlot(srcSlot), DestPhysReg(destPhysReg) {}
823   
824   unsigned getSrcSlot() const { return SrcSlot; }
825   Record *getDestPhysReg() const { return DestPhysReg; }
826   
827   static inline bool classof(const Matcher *N) {
828     return N->getKind() == EmitCopyToReg;
829   }
830   
831 private:
832   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
833   virtual bool isEqualImpl(const Matcher *M) const {
834     return cast<EmitCopyToRegMatcher>(M)->SrcSlot == SrcSlot &&
835            cast<EmitCopyToRegMatcher>(M)->DestPhysReg == DestPhysReg; 
836   }
837   virtual unsigned getHashImpl() const {
838     return SrcSlot ^ ((unsigned)(intptr_t)DestPhysReg << 4);
839   }
840 };
841   
842     
843   
844 /// EmitNodeXFormMatcher - Emit an operation that runs an SDNodeXForm on a
845 /// recorded node and records the result.
846 class EmitNodeXFormMatcher : public Matcher {
847   unsigned Slot;
848   Record *NodeXForm;
849 public:
850   EmitNodeXFormMatcher(unsigned slot, Record *nodeXForm)
851     : Matcher(EmitNodeXForm), Slot(slot), NodeXForm(nodeXForm) {}
852   
853   unsigned getSlot() const { return Slot; }
854   Record *getNodeXForm() const { return NodeXForm; }
855   
856   static inline bool classof(const Matcher *N) {
857     return N->getKind() == EmitNodeXForm;
858   }
859   
860 private:
861   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
862   virtual bool isEqualImpl(const Matcher *M) const {
863     return cast<EmitNodeXFormMatcher>(M)->Slot == Slot &&
864            cast<EmitNodeXFormMatcher>(M)->NodeXForm == NodeXForm; 
865   }
866   virtual unsigned getHashImpl() const {
867     return Slot ^ ((unsigned)(intptr_t)NodeXForm << 4);
868   }
869 };
870   
871 /// EmitNodeMatcher - This signals a successful match and generates a node.
872 class EmitNodeMatcher : public Matcher {
873   std::string OpcodeName;
874   const SmallVector<MVT::SimpleValueType, 3> VTs;
875   const SmallVector<unsigned, 6> Operands;
876   bool HasChain, HasFlag, HasMemRefs;
877   
878   /// NumFixedArityOperands - If this is a fixed arity node, this is set to -1.
879   /// If this is a varidic node, this is set to the number of fixed arity
880   /// operands in the root of the pattern.  The rest are appended to this node.
881   int NumFixedArityOperands;
882 public:
883   EmitNodeMatcher(const std::string &opcodeName,
884                   const MVT::SimpleValueType *vts, unsigned numvts,
885                   const unsigned *operands, unsigned numops,
886                   bool hasChain, bool hasFlag, bool hasmemrefs,
887                   int numfixedarityoperands)
888     : Matcher(EmitNode), OpcodeName(opcodeName),
889       VTs(vts, vts+numvts), Operands(operands, operands+numops),
890       HasChain(hasChain), HasFlag(hasFlag), HasMemRefs(hasmemrefs),
891       NumFixedArityOperands(numfixedarityoperands) {}
892   
893   const std::string &getOpcodeName() const { return OpcodeName; }
894   
895   unsigned getNumVTs() const { return VTs.size(); }
896   MVT::SimpleValueType getVT(unsigned i) const {
897     assert(i < VTs.size());
898     return VTs[i];
899   }
900   
901   unsigned getNumOperands() const { return Operands.size(); }
902   unsigned getOperand(unsigned i) const {
903     assert(i < Operands.size());
904     return Operands[i];
905   }  
906   
907   bool hasChain() const { return HasChain; }
908   bool hasFlag() const { return HasFlag; }
909   bool hasMemRefs() const { return HasMemRefs; }
910   int getNumFixedArityOperands() const { return NumFixedArityOperands; }
911   
912   static inline bool classof(const Matcher *N) {
913     return N->getKind() == EmitNode;
914   }
915   
916 private:
917   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
918   virtual bool isEqualImpl(const Matcher *M) const;
919   virtual unsigned getHashImpl() const;
920 };
921   
922 /// MarkFlagResultsMatcher - This node indicates which non-root nodes in the
923 /// pattern produce flags.  This allows CompleteMatchMatcher to update them
924 /// with the output flag of the resultant code.
925 class MarkFlagResultsMatcher : public Matcher {
926   SmallVector<unsigned, 3> FlagResultNodes;
927 public:
928   MarkFlagResultsMatcher(const unsigned *nodes, unsigned NumNodes)
929     : Matcher(MarkFlagResults), FlagResultNodes(nodes, nodes+NumNodes) {}
930   
931   unsigned getNumNodes() const { return FlagResultNodes.size(); }
932   
933   unsigned getNode(unsigned i) const {
934     assert(i < FlagResultNodes.size());
935     return FlagResultNodes[i];
936   }  
937   
938   static inline bool classof(const Matcher *N) {
939     return N->getKind() == MarkFlagResults;
940   }
941   
942 private:
943   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
944   virtual bool isEqualImpl(const Matcher *M) const {
945     return cast<MarkFlagResultsMatcher>(M)->FlagResultNodes == FlagResultNodes;
946   }
947   virtual unsigned getHashImpl() const;
948 };
949
950 /// CompleteMatchMatcher - Complete a match by replacing the results of the
951 /// pattern with the newly generated nodes.  This also prints a comment
952 /// indicating the source and dest patterns.
953 class CompleteMatchMatcher : public Matcher {
954   SmallVector<unsigned, 2> Results;
955   const PatternToMatch &Pattern;
956 public:
957   CompleteMatchMatcher(const unsigned *results, unsigned numresults,
958                            const PatternToMatch &pattern)
959   : Matcher(CompleteMatch), Results(results, results+numresults),
960     Pattern(pattern) {}
961
962   unsigned getNumResults() const { return Results.size(); }
963   unsigned getResult(unsigned R) const { return Results[R]; }
964   const PatternToMatch &getPattern() const { return Pattern; }
965   
966   static inline bool classof(const Matcher *N) {
967     return N->getKind() == CompleteMatch;
968   }
969   
970 private:
971   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
972   virtual bool isEqualImpl(const Matcher *M) const {
973     return cast<CompleteMatchMatcher>(M)->Results == Results &&
974           &cast<CompleteMatchMatcher>(M)->Pattern == &Pattern;
975   }
976   virtual unsigned getHashImpl() const;
977 };
978   
979 } // end namespace llvm
980
981 #endif