convert the new matcher to check intermediate nodes for a single
[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 public:
131   RecordMatcherNode() : MatcherNodeWithChild(Record) {}
132   
133   static inline bool classof(const MatcherNode *N) {
134     return N->getKind() == Record;
135   }
136   
137   virtual void print(raw_ostream &OS, unsigned indent = 0) const;
138 };
139   
140 /// MoveChildMatcherNode - This tells the interpreter to move into the
141 /// specified child node.
142 class MoveChildMatcherNode : public MatcherNodeWithChild {
143   unsigned ChildNo;
144 public:
145   MoveChildMatcherNode(unsigned childNo)
146   : MatcherNodeWithChild(MoveChild), ChildNo(childNo) {}
147   
148   unsigned getChildNo() const { return ChildNo; }
149   
150   static inline bool classof(const MatcherNode *N) {
151     return N->getKind() == MoveChild;
152   }
153   
154   virtual void print(raw_ostream &OS, unsigned indent = 0) const;
155 };
156   
157 /// MoveParentMatcherNode - This tells the interpreter to move to the parent
158 /// of the current node.
159 class MoveParentMatcherNode : public MatcherNodeWithChild {
160 public:
161   MoveParentMatcherNode()
162   : MatcherNodeWithChild(MoveParent) {}
163   
164   static inline bool classof(const MatcherNode *N) {
165     return N->getKind() == MoveParent;
166   }
167   
168   virtual void print(raw_ostream &OS, unsigned indent = 0) const;
169 };
170
171 /// CheckSameMatcherNode - This checks to see if this node is exactly the same
172 /// node as the specified match that was recorded with 'Record'.  This is used
173 /// when patterns have the same name in them, like '(mul GPR:$in, GPR:$in)'.
174 class CheckSameMatcherNode : public MatcherNodeWithChild {
175   unsigned MatchNumber;
176 public:
177   CheckSameMatcherNode(unsigned matchnumber)
178   : MatcherNodeWithChild(CheckSame), MatchNumber(matchnumber) {}
179   
180   unsigned getMatchNumber() const { return MatchNumber; }
181   
182   static inline bool classof(const MatcherNode *N) {
183     return N->getKind() == CheckSame;
184   }
185   
186   virtual void print(raw_ostream &OS, unsigned indent = 0) const;
187 };
188   
189 /// CheckPatternPredicateMatcherNode - This checks the target-specific predicate
190 /// to see if the entire pattern is capable of matching.  This predicate does
191 /// not take a node as input.  This is used for subtarget feature checks etc.
192 class CheckPatternPredicateMatcherNode : public MatcherNodeWithChild {
193   std::string Predicate;
194 public:
195   CheckPatternPredicateMatcherNode(StringRef predicate)
196   : MatcherNodeWithChild(CheckPatternPredicate), Predicate(predicate) {}
197   
198   StringRef getPredicate() const { return Predicate; }
199   
200   static inline bool classof(const MatcherNode *N) {
201     return N->getKind() == CheckPatternPredicate;
202   }
203   
204   virtual void print(raw_ostream &OS, unsigned indent = 0) const;
205 };
206   
207 /// CheckPredicateMatcherNode - This checks the target-specific predicate to
208 /// see if the node is acceptable.
209 class CheckPredicateMatcherNode : public MatcherNodeWithChild {
210   StringRef PredName;
211 public:
212   CheckPredicateMatcherNode(StringRef predname)
213     : MatcherNodeWithChild(CheckPredicate), PredName(predname) {}
214   
215   StringRef getPredicateName() const { return PredName; }
216
217   static inline bool classof(const MatcherNode *N) {
218     return N->getKind() == CheckPredicate;
219   }
220   
221   virtual void print(raw_ostream &OS, unsigned indent = 0) const;
222 };
223   
224   
225 /// CheckOpcodeMatcherNode - This checks to see if the current node has the
226 /// specified opcode, if not it fails to match.
227 class CheckOpcodeMatcherNode : public MatcherNodeWithChild {
228   StringRef OpcodeName;
229 public:
230   CheckOpcodeMatcherNode(StringRef opcodename)
231     : MatcherNodeWithChild(CheckOpcode), OpcodeName(opcodename) {}
232   
233   StringRef getOpcodeName() const { return OpcodeName; }
234   
235   static inline bool classof(const MatcherNode *N) {
236     return N->getKind() == CheckOpcode;
237   }
238   
239   virtual void print(raw_ostream &OS, unsigned indent = 0) const;
240 };
241   
242 /// CheckTypeMatcherNode - This checks to see if the current node has the
243 /// specified type, if not it fails to match.
244 class CheckTypeMatcherNode : public MatcherNodeWithChild {
245   MVT::SimpleValueType Type;
246 public:
247   CheckTypeMatcherNode(MVT::SimpleValueType type)
248     : MatcherNodeWithChild(CheckType), Type(type) {}
249   
250   MVT::SimpleValueType getType() const { return Type; }
251   
252   static inline bool classof(const MatcherNode *N) {
253     return N->getKind() == CheckType;
254   }
255   
256   virtual void print(raw_ostream &OS, unsigned indent = 0) const;
257 };
258
259 /// CheckIntegerMatcherNode - This checks to see if the current node is a
260 /// ConstantSDNode with the specified integer value, if not it fails to match.
261 class CheckIntegerMatcherNode : public MatcherNodeWithChild {
262   int64_t Value;
263 public:
264   CheckIntegerMatcherNode(int64_t value)
265     : MatcherNodeWithChild(CheckInteger), Value(value) {}
266   
267   int64_t getValue() const { return Value; }
268   
269   static inline bool classof(const MatcherNode *N) {
270     return N->getKind() == CheckInteger;
271   }
272   
273   virtual void print(raw_ostream &OS, unsigned indent = 0) const;
274 };
275   
276 /// CheckCondCodeMatcherNode - This checks to see if the current node is a
277 /// CondCodeSDNode with the specified condition, if not it fails to match.
278 class CheckCondCodeMatcherNode : public MatcherNodeWithChild {
279   StringRef CondCodeName;
280 public:
281   CheckCondCodeMatcherNode(StringRef condcodename)
282   : MatcherNodeWithChild(CheckCondCode), CondCodeName(condcodename) {}
283   
284   StringRef getCondCodeName() const { return CondCodeName; }
285   
286   static inline bool classof(const MatcherNode *N) {
287     return N->getKind() == CheckCondCode;
288   }
289   
290   virtual void print(raw_ostream &OS, unsigned indent = 0) const;
291 };
292   
293 /// CheckValueTypeMatcherNode - This checks to see if the current node is a
294 /// VTSDNode with the specified type, if not it fails to match.
295 class CheckValueTypeMatcherNode : public MatcherNodeWithChild {
296   StringRef TypeName;
297 public:
298   CheckValueTypeMatcherNode(StringRef type_name)
299   : MatcherNodeWithChild(CheckValueType), TypeName(type_name) {}
300   
301   StringRef getTypeName() const { return TypeName; }
302
303   static inline bool classof(const MatcherNode *N) {
304     return N->getKind() == CheckValueType;
305   }
306   
307   virtual void print(raw_ostream &OS, unsigned indent = 0) const;
308 };
309   
310   
311   
312 /// CheckComplexPatMatcherNode - This node runs the specified ComplexPattern on
313 /// the current node.
314 class CheckComplexPatMatcherNode : public MatcherNodeWithChild {
315   const ComplexPattern &Pattern;
316 public:
317   CheckComplexPatMatcherNode(const ComplexPattern &pattern)
318   : MatcherNodeWithChild(CheckComplexPat), Pattern(pattern) {}
319   
320   static inline bool classof(const MatcherNode *N) {
321     return N->getKind() == CheckComplexPat;
322   }
323   
324   virtual void print(raw_ostream &OS, unsigned indent = 0) const;
325 };
326   
327 /// CheckAndImmMatcherNode - This checks to see if the current node is an 'and'
328 /// with something equivalent to the specified immediate.
329 class CheckAndImmMatcherNode : public MatcherNodeWithChild {
330   int64_t Value;
331 public:
332   CheckAndImmMatcherNode(int64_t value)
333   : MatcherNodeWithChild(CheckAndImm), Value(value) {}
334   
335   int64_t getValue() const { return Value; }
336   
337   static inline bool classof(const MatcherNode *N) {
338     return N->getKind() == CheckAndImm;
339   }
340   
341   virtual void print(raw_ostream &OS, unsigned indent = 0) const;
342 };
343
344 /// CheckOrImmMatcherNode - This checks to see if the current node is an 'and'
345 /// with something equivalent to the specified immediate.
346 class CheckOrImmMatcherNode : public MatcherNodeWithChild {
347   int64_t Value;
348 public:
349   CheckOrImmMatcherNode(int64_t value)
350     : MatcherNodeWithChild(CheckOrImm), Value(value) {}
351   
352   int64_t getValue() const { return Value; }
353
354   static inline bool classof(const MatcherNode *N) {
355     return N->getKind() == CheckOrImm;
356   }
357   
358   virtual void print(raw_ostream &OS, unsigned indent = 0) const;
359 };
360
361 /// CheckFoldableChainNodeMatcherNode - This checks to see if the current node
362 /// (which defines a chain operand) is safe to fold into a larger pattern.
363 class CheckFoldableChainNodeMatcherNode : public MatcherNodeWithChild {
364 public:
365   CheckFoldableChainNodeMatcherNode()
366     : MatcherNodeWithChild(CheckFoldableChainNode) {}
367   
368   static inline bool classof(const MatcherNode *N) {
369     return N->getKind() == CheckFoldableChainNode;
370   }
371   
372   virtual void print(raw_ostream &OS, unsigned indent = 0) const;
373 };
374
375 } // end namespace llvm
376
377 #endif