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