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