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