Check in the first big step of rewriting DAGISelEmitter to
[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/ADT/OwningPtr.h"
14 #include "llvm/ADT/StringRef.h"
15 #include "llvm/CodeGen/ValueTypes.h"
16
17 namespace llvm {
18   class CodeGenDAGPatterns;
19   class MatcherNode;
20   class PatternToMatch;
21   class raw_ostream;
22   class ComplexPattern;
23
24 MatcherNode *ConvertPatternToMatcher(const PatternToMatch &Pattern,
25                                      const CodeGenDAGPatterns &CGP);
26
27 void EmitMatcherTable(const MatcherNode *Matcher, raw_ostream &OS);
28
29   
30 /// MatcherNode - Base class for all the the DAG ISel Matcher representation
31 /// nodes.
32 class MatcherNode {
33 public:
34   enum KindTy {
35     EmitNode,
36     Push,           // [Push, Dest0, Dest1, Dest2, Dest3]
37     Record,         // [Record]
38     MoveChild,      // [MoveChild, Child#]
39     MoveParent,     // [MoveParent]
40     
41     CheckSame,      // [CheckSame, N]         Fail if not same as prev match.
42     CheckPatternPredicate,
43     CheckPredicate, // [CheckPredicate, P]    Fail if predicate fails.
44     CheckOpcode,    // [CheckOpcode, Opcode]  Fail if not opcode.
45     CheckType,      // [CheckType, MVT]       Fail if not correct type.
46     CheckInteger,   // [CheckInteger, int0,int1,int2,...int7] Fail if wrong val.
47     CheckCondCode,  // [CheckCondCode, CondCode] Fail if not condcode.
48     CheckValueType,
49     CheckComplexPat,
50     CheckAndImm,
51     CheckOrImm
52   };
53   const KindTy Kind;
54   
55 protected:
56   MatcherNode(KindTy K) : Kind(K) {}
57 public:
58   virtual ~MatcherNode() {}
59   
60   KindTy getKind() const { return Kind; }
61   
62   
63   static inline bool classof(const MatcherNode *) { return true; }
64   
65   virtual void print(raw_ostream &OS, unsigned indent = 0) const = 0;
66   void dump() const;
67 };
68   
69 /// EmitNodeMatcherNode - This signals a successful match and generates a node.
70 class EmitNodeMatcherNode : public MatcherNode {
71   const PatternToMatch &Pattern;
72 public:
73   EmitNodeMatcherNode(const PatternToMatch &pattern)
74     : MatcherNode(EmitNode), Pattern(pattern) {}
75
76   const PatternToMatch &getPattern() const { return Pattern; }
77
78   static inline bool classof(const MatcherNode *N) {
79     return N->getKind() == EmitNode;
80   }
81
82   virtual void print(raw_ostream &OS, unsigned indent = 0) const;
83 };
84
85 /// MatcherNodeWithChild - Every node accept the final accept state has a child
86 /// that is executed after the node runs.  This class captures this commonality.
87 class MatcherNodeWithChild : public MatcherNode {
88   OwningPtr<MatcherNode> Child;
89 public:
90   MatcherNodeWithChild(KindTy K) : MatcherNode(K) {}
91   
92   MatcherNode *getChild() { return Child.get(); }
93   const MatcherNode *getChild() const { return Child.get(); }
94   void setChild(MatcherNode *C) { Child.reset(C); }
95   
96   static inline bool classof(const MatcherNode *N) {
97     return N->getKind() != EmitNode;
98   }
99   
100 protected:
101   void printChild(raw_ostream &OS, unsigned indent) const;
102 };
103
104 /// PushMatcherNode - This pushes a failure scope on the stack and evaluates
105 /// 'child'.  If 'child' fails to match, it pops its scope and attempts to
106 /// match 'Failure'.
107 class PushMatcherNode : public MatcherNodeWithChild {
108   OwningPtr<MatcherNode> Failure;
109 public:
110   PushMatcherNode(MatcherNode *child = 0, MatcherNode *failure = 0)
111     : MatcherNodeWithChild(Push), Failure(failure) {
112     setChild(child);
113   }
114   
115   MatcherNode *getFailure() { return Failure.get(); }
116   const MatcherNode *getFailure() const { return Failure.get(); }
117   void setFailure(MatcherNode *N) { Failure.reset(N); }
118
119   static inline bool classof(const MatcherNode *N) {
120     return N->getKind() == Push;
121   }
122   
123   virtual void print(raw_ostream &OS, unsigned indent = 0) const;
124 };
125
126 /// RecordMatcherNode - Save the current node in the operand list.
127 class RecordMatcherNode : public MatcherNodeWithChild {
128 public:
129   RecordMatcherNode() : MatcherNodeWithChild(Record) {}
130   
131   static inline bool classof(const MatcherNode *N) {
132     return N->getKind() == Record;
133   }
134   
135   virtual void print(raw_ostream &OS, unsigned indent = 0) const;
136 };
137   
138 /// MoveChildMatcherNode - This tells the interpreter to move into the
139 /// specified child node.
140 class MoveChildMatcherNode : public MatcherNodeWithChild {
141   unsigned ChildNo;
142 public:
143   MoveChildMatcherNode(unsigned childNo)
144   : MatcherNodeWithChild(MoveChild), ChildNo(childNo) {}
145   
146   unsigned getChildNo() const { return ChildNo; }
147   
148   static inline bool classof(const MatcherNode *N) {
149     return N->getKind() == MoveChild;
150   }
151   
152   virtual void print(raw_ostream &OS, unsigned indent = 0) const;
153 };
154   
155 /// MoveParentMatcherNode - This tells the interpreter to move to the parent
156 /// of the current node.
157 class MoveParentMatcherNode : public MatcherNodeWithChild {
158 public:
159   MoveParentMatcherNode()
160   : MatcherNodeWithChild(MoveParent) {}
161   
162   static inline bool classof(const MatcherNode *N) {
163     return N->getKind() == MoveParent;
164   }
165   
166   virtual void print(raw_ostream &OS, unsigned indent = 0) const;
167 };
168
169 /// CheckSameMatcherNode - This checks to see if this node is exactly the same
170 /// node as the specified match that was recorded with 'Record'.  This is used
171 /// when patterns have the same name in them, like '(mul GPR:$in, GPR:$in)'.
172 class CheckSameMatcherNode : public MatcherNodeWithChild {
173   unsigned MatchNumber;
174 public:
175   CheckSameMatcherNode(unsigned matchnumber)
176   : MatcherNodeWithChild(CheckSame), MatchNumber(matchnumber) {}
177   
178   unsigned getMatchNumber() const { return MatchNumber; }
179   
180   static inline bool classof(const MatcherNode *N) {
181     return N->getKind() == CheckSame;
182   }
183   
184   virtual void print(raw_ostream &OS, unsigned indent = 0) const;
185 };
186   
187 /// CheckPatternPredicateMatcherNode - This checks the target-specific predicate
188 /// to see if the entire pattern is capable of matching.  This predicate does
189 /// not take a node as input.  This is used for subtarget feature checks etc.
190 class CheckPatternPredicateMatcherNode : public MatcherNodeWithChild {
191   std::string Predicate;
192 public:
193   CheckPatternPredicateMatcherNode(StringRef predicate)
194   : MatcherNodeWithChild(CheckPatternPredicate), Predicate(predicate) {}
195   
196   StringRef getPredicate() const { return Predicate; }
197   
198   static inline bool classof(const MatcherNode *N) {
199     return N->getKind() == CheckPatternPredicate;
200   }
201   
202   virtual void print(raw_ostream &OS, unsigned indent = 0) const;
203 };
204   
205 /// CheckPredicateMatcherNode - This checks the target-specific predicate to
206 /// see if the node is acceptable.
207 class CheckPredicateMatcherNode : public MatcherNodeWithChild {
208   StringRef PredName;
209 public:
210   CheckPredicateMatcherNode(StringRef predname)
211     : MatcherNodeWithChild(CheckPredicate), PredName(predname) {}
212   
213   StringRef getPredicateName() const { return PredName; }
214
215   static inline bool classof(const MatcherNode *N) {
216     return N->getKind() == CheckPredicate;
217   }
218   
219   virtual void print(raw_ostream &OS, unsigned indent = 0) const;
220 };
221   
222   
223 /// CheckOpcodeMatcherNode - This checks to see if the current node has the
224 /// specified opcode, if not it fails to match.
225 class CheckOpcodeMatcherNode : public MatcherNodeWithChild {
226   StringRef OpcodeName;
227 public:
228   CheckOpcodeMatcherNode(StringRef opcodename)
229     : MatcherNodeWithChild(CheckOpcode), OpcodeName(opcodename) {}
230   
231   StringRef getOpcodeName() const { return OpcodeName; }
232   
233   static inline bool classof(const MatcherNode *N) {
234     return N->getKind() == CheckOpcode;
235   }
236   
237   virtual void print(raw_ostream &OS, unsigned indent = 0) const;
238 };
239   
240 /// CheckTypeMatcherNode - This checks to see if the current node has the
241 /// specified type, if not it fails to match.
242 class CheckTypeMatcherNode : public MatcherNodeWithChild {
243   MVT::SimpleValueType Type;
244 public:
245   CheckTypeMatcherNode(MVT::SimpleValueType type)
246     : MatcherNodeWithChild(CheckType), Type(type) {}
247   
248   MVT::SimpleValueType getType() const { return Type; }
249   
250   static inline bool classof(const MatcherNode *N) {
251     return N->getKind() == CheckType;
252   }
253   
254   virtual void print(raw_ostream &OS, unsigned indent = 0) const;
255 };
256
257 /// CheckIntegerMatcherNode - This checks to see if the current node is a
258 /// ConstantSDNode with the specified integer value, if not it fails to match.
259 class CheckIntegerMatcherNode : public MatcherNodeWithChild {
260   int64_t Value;
261 public:
262   CheckIntegerMatcherNode(int64_t value)
263     : MatcherNodeWithChild(CheckInteger), Value(value) {}
264   
265   int64_t getValue() const { return Value; }
266   
267   static inline bool classof(const MatcherNode *N) {
268     return N->getKind() == CheckInteger;
269   }
270   
271   virtual void print(raw_ostream &OS, unsigned indent = 0) const;
272 };
273   
274 /// CheckCondCodeMatcherNode - This checks to see if the current node is a
275 /// CondCodeSDNode with the specified condition, if not it fails to match.
276 class CheckCondCodeMatcherNode : public MatcherNodeWithChild {
277   StringRef CondCodeName;
278 public:
279   CheckCondCodeMatcherNode(StringRef condcodename)
280   : MatcherNodeWithChild(CheckCondCode), CondCodeName(condcodename) {}
281   
282   StringRef getCondCodeName() const { return CondCodeName; }
283   
284   static inline bool classof(const MatcherNode *N) {
285     return N->getKind() == CheckCondCode;
286   }
287   
288   virtual void print(raw_ostream &OS, unsigned indent = 0) const;
289 };
290   
291 /// CheckValueTypeMatcherNode - This checks to see if the current node is a
292 /// VTSDNode with the specified type, if not it fails to match.
293 class CheckValueTypeMatcherNode : public MatcherNodeWithChild {
294   StringRef TypeName;
295 public:
296   CheckValueTypeMatcherNode(StringRef type_name)
297   : MatcherNodeWithChild(CheckValueType), TypeName(type_name) {}
298   
299   StringRef getTypeName() const { return TypeName; }
300
301   static inline bool classof(const MatcherNode *N) {
302     return N->getKind() == CheckValueType;
303   }
304   
305   virtual void print(raw_ostream &OS, unsigned indent = 0) const;
306 };
307   
308   
309   
310 /// CheckComplexPatMatcherNode - This node runs the specified ComplexPattern on
311 /// the current node.
312 class CheckComplexPatMatcherNode : public MatcherNodeWithChild {
313   const ComplexPattern &Pattern;
314 public:
315   CheckComplexPatMatcherNode(const ComplexPattern &pattern)
316   : MatcherNodeWithChild(CheckComplexPat), Pattern(pattern) {}
317   
318   static inline bool classof(const MatcherNode *N) {
319     return N->getKind() == CheckComplexPat;
320   }
321   
322   virtual void print(raw_ostream &OS, unsigned indent = 0) const;
323 };
324   
325 /// CheckAndImmMatcherNode - This checks to see if the current node is an 'and'
326 /// with something equivalent to the specified immediate.
327 class CheckAndImmMatcherNode : public MatcherNodeWithChild {
328   int64_t Value;
329 public:
330   CheckAndImmMatcherNode(int64_t value)
331   : MatcherNodeWithChild(CheckAndImm), Value(value) {}
332   
333   int64_t getValue() const { return Value; }
334   
335   static inline bool classof(const MatcherNode *N) {
336     return N->getKind() == CheckAndImm;
337   }
338   
339   virtual void print(raw_ostream &OS, unsigned indent = 0) const;
340 };
341
342 /// CheckOrImmMatcherNode - This checks to see if the current node is an 'and'
343 /// with something equivalent to the specified immediate.
344 class CheckOrImmMatcherNode : public MatcherNodeWithChild {
345   int64_t Value;
346 public:
347   CheckOrImmMatcherNode(int64_t value)
348     : MatcherNodeWithChild(CheckOrImm), Value(value) {}
349   
350   int64_t getValue() const { return Value; }
351
352   static inline bool classof(const MatcherNode *N) {
353     return N->getKind() == CheckOrImm;
354   }
355   
356   virtual void print(raw_ostream &OS, unsigned indent = 0) const;
357 };
358   
359   
360 } // end namespace llvm
361
362 #endif