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