aboutsummaryrefslogtreecommitdiffstats
path: root/utils/TableGen/DAGISelMatcher.h
blob: 72bdb7bf27788137ade6ffcfc88dd65d9c594cc0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
//===- DAGISelMatcher.h - Representation of DAG pattern matcher -----------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#ifndef TBLGEN_DAGISELMATCHER_H
#define TBLGEN_DAGISELMATCHER_H

#include "llvm/ADT/OwningPtr.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/CodeGen/ValueTypes.h"

namespace llvm {
  class CodeGenDAGPatterns;
  class MatcherNode;
  class PatternToMatch;
  class raw_ostream;
  class ComplexPattern;

MatcherNode *ConvertPatternToMatcher(const PatternToMatch &Pattern,
                                     const CodeGenDAGPatterns &CGP);

void EmitMatcherTable(const MatcherNode *Matcher, raw_ostream &OS);

  
/// MatcherNode - Base class for all the the DAG ISel Matcher representation
/// nodes.
class MatcherNode {
public:
  enum KindTy {
    EmitNode,
    Push,           // [Push, Dest0, Dest1, Dest2, Dest3]
    Record,         // [Record]
    MoveChild,      // [MoveChild, Child#]
    MoveParent,     // [MoveParent]
    
    CheckSame,      // [CheckSame, N]         Fail if not same as prev match.
    CheckPatternPredicate,
    CheckPredicate, // [CheckPredicate, P]    Fail if predicate fails.
    CheckOpcode,    // [CheckOpcode, Opcode]  Fail if not opcode.
    CheckType,      // [CheckType, MVT]       Fail if not correct type.
    CheckInteger,   // [CheckInteger, int0,int1,int2,...int7] Fail if wrong val.
    CheckCondCode,  // [CheckCondCode, CondCode] Fail if not condcode.
    CheckValueType,
    CheckComplexPat,
    CheckAndImm,
    CheckOrImm
  };
  const KindTy Kind;
  
protected:
  MatcherNode(KindTy K) : Kind(K) {}
public:
  virtual ~MatcherNode() {}
  
  KindTy getKind() const { return Kind; }
  
  
  static inline bool classof(const MatcherNode *) { return true; }
  
  virtual void print(raw_ostream &OS, unsigned indent = 0) const = 0;
  void dump() const;
};
  
/// EmitNodeMatcherNode - This signals a successful match and generates a node.
class EmitNodeMatcherNode : public MatcherNode {
  const PatternToMatch &Pattern;
public:
  EmitNodeMatcherNode(const PatternToMatch &pattern)
    : MatcherNode(EmitNode), Pattern(pattern) {}

  const PatternToMatch &getPattern() const { return Pattern; }

  static inline bool classof(const MatcherNode *N) {
    return N->getKind() == EmitNode;
  }

  virtual void print(raw_ostream &OS, unsigned indent = 0) const;
};

/// MatcherNodeWithChild - Every node accept the final accept state has a child
/// that is executed after the node runs.  This class captures this commonality.
class MatcherNodeWithChild : public MatcherNode {
  OwningPtr<MatcherNode> Child;
public:
  MatcherNodeWithChild(KindTy K) : MatcherNode(K) {}
  
  MatcherNode *getChild() { return Child.get(); }
  const MatcherNode *getChild() const { return Child.get(); }
  void setChild(MatcherNode *C) { Child.reset(C); }
  
  static inline bool classof(const MatcherNode *N) {
    return N->getKind() != EmitNode;
  }
  
protected:
  void printChild(raw_ostream &OS, unsigned indent) const;
};

/// PushMatcherNode - This pushes a failure scope on the stack and evaluates
/// 'child'.  If 'child' fails to match, it pops its scope and attempts to
/// match 'Failure'.
class PushMatcherNode : public MatcherNodeWithChild {
  OwningPtr<MatcherNode> Failure;
public:
  PushMatcherNode(MatcherNode *child = 0, MatcherNode *failure = 0)
    : MatcherNodeWithChild(Push), Failure(failure) {
    setChild(child);
  }
  
  MatcherNode *getFailure() { return Failure.get(); }
  const MatcherNode *getFailure() const { return Failure.get(); }
  void setFailure(MatcherNode *N) { Failure.reset(N); }

  static inline bool classof(const MatcherNode *N) {
    return N->getKind() == Push;
  }
  
  virtual void print(raw_ostream &OS, unsigned indent = 0) const;
};

/// RecordMatcherNode - Save the current node in the operand list.
class RecordMatcherNode : public MatcherNodeWithChild {
public:
  RecordMatcherNode() : MatcherNodeWithChild(Record) {}
  
  static inline bool classof(const MatcherNode *N) {
    return N->getKind() == Record;
  }
  
  virtual void print(raw_ostream &OS, unsigned indent = 0) const;
};
  
/// MoveChildMatcherNode - This tells the interpreter to move into the
/// specified child node.
class MoveChildMatcherNode : public MatcherNodeWithChild {
  unsigned ChildNo;
public:
  MoveChildMatcherNode(unsigned childNo)
  : MatcherNodeWithChild(MoveChild), ChildNo(childNo) {}
  
  unsigned getChildNo() const { return ChildNo; }
  
  static inline bool classof(const MatcherNode *N) {
    return N->getKind() == MoveChild;
  }
  
  virtual void print(raw_ostream &OS, unsigned indent = 0) const;
};
  
/// MoveParentMatcherNode - This tells the interpreter to move to the parent
/// of the current node.
class MoveParentMatcherNode : public MatcherNodeWithChild {
public:
  MoveParentMatcherNode()
  : MatcherNodeWithChild(MoveParent) {}
  
  static inline bool classof(const MatcherNode *N) {
    return N->getKind() == MoveParent;
  }
  
  virtual void print(raw_ostream &OS, unsigned indent = 0) const;
};

/// CheckSameMatcherNode - This checks to see if this node is exactly the same
/// node as the specified match that was recorded with 'Record'.  This is used
/// when patterns have the same name in them, like '(mul GPR:$in, GPR:$in)'.
class CheckSameMatcherNode : public MatcherNodeWithChild {
  unsigned MatchNumber;
public:
  CheckSameMatcherNode(unsigned matchnumber)
  : MatcherNodeWithChild(CheckSame), MatchNumber(matchnumber) {}
  
  unsigned getMatchNumber() const { return MatchNumber; }
  
  static inline bool classof(const MatcherNode *N) {
    return N->getKind() == CheckSame;
  }
  
  virtual void print(raw_ostream &OS, unsigned indent = 0) const;
};
  
/// CheckPatternPredicateMatcherNode - This checks the target-specific predicate
/// to see if the entire pattern is capable of matching.  This predicate does
/// not take a node as input.  This is used for subtarget feature checks etc.
class CheckPatternPredicateMatcherNode : public MatcherNodeWithChild {
  std::string Predicate;
public:
  CheckPatternPredicateMatcherNode(StringRef predicate)
  : MatcherNodeWithChild(CheckPatternPredicate), Predicate(predicate) {}
  
  StringRef getPredicate() const { return Predicate; }
  
  static inline bool classof(const MatcherNode *N) {
    return N->getKind() == CheckPatternPredicate;
  }
  
  virtual void print(raw_ostream &OS, unsigned indent = 0) const;
};
  
/// CheckPredicateMatcherNode - This checks the target-specific predicate to
/// see if the node is acceptable.
class CheckPredicateMatcherNode : public MatcherNodeWithChild {
  StringRef PredName;
public:
  CheckPredicateMatcherNode(StringRef predname)
    : MatcherNodeWithChild(CheckPredicate), PredName(predname) {}
  
  StringRef getPredicateName() const { return PredName; }

  static inline bool classof(const MatcherNode *N) {
    return N->getKind() == CheckPredicate;
  }
  
  virtual void print(raw_ostream &OS, unsigned indent = 0) const;
};
  
  
/// CheckOpcodeMatcherNode - This checks to see if the current node has the
/// specified opcode, if not it fails to match.
class CheckOpcodeMatcherNode : public MatcherNodeWithChild {
  StringRef OpcodeName;
public:
  CheckOpcodeMatcherNode(StringRef opcodename)
    : MatcherNodeWithChild(CheckOpcode), OpcodeName(opcodename) {}
  
  StringRef getOpcodeName() const { return OpcodeName; }
  
  static inline bool classof(const MatcherNode *N) {
    return N->getKind() == CheckOpcode;
  }
  
  virtual void print(raw_ostream &OS, unsigned indent = 0) const;
};
  
/// CheckTypeMatcherNode - This checks to see if the current node has the
/// specified type, if not it fails to match.
class CheckTypeMatcherNode : public MatcherNodeWithChild {
  MVT::SimpleValueType Type;
public:
  CheckTypeMatcherNode(MVT::SimpleValueType type)
    : MatcherNodeWithChild(CheckType), Type(type) {}
  
  MVT::SimpleValueType getType() const { return Type; }
  
  static inline bool classof(const MatcherNode *N) {
    return N->getKind() == CheckType;
  }
  
  virtual void print(raw_ostream &OS, unsigned indent = 0) const;
};

/// CheckIntegerMatcherNode - This checks to see if the current node is a
/// ConstantSDNode with the specified integer value, if not it fails to match.
class CheckIntegerMatcherNode : public MatcherNodeWithChild {
  int64_t Value;
public:
  CheckIntegerMatcherNode(int64_t value)
    : MatcherNodeWithChild(CheckInteger), Value(value) {}
  
  int64_t getValue() const { return Value; }
  
  static inline bool classof(const MatcherNode *N) {
    return N->getKind() == CheckInteger;
  }
  
  virtual void print(raw_ostream &OS, unsigned indent = 0) const;
};
  
/// CheckCondCodeMatcherNode - This checks to see if the current node is a
/// CondCodeSDNode with the specified condition, if not it fails to match.
class CheckCondCodeMatcherNode : public MatcherNodeWithChild {
  StringRef CondCodeName;
public:
  CheckCondCodeMatcherNode(StringRef condcodename)
  : MatcherNodeWithChild(CheckCondCode), CondCodeName(condcodename) {}
  
  StringRef getCondCodeName() const { return CondCodeName; }
  
  static inline bool classof(const MatcherNode *N) {
    return N->getKind() == CheckCondCode;
  }
  
  virtual void print(raw_ostream &OS, unsigned indent = 0) const;
};
  
/// CheckValueTypeMatcherNode - This checks to see if the current node is a
/// VTSDNode with the specified type, if not it fails to match.
class CheckValueTypeMatcherNode : public MatcherNodeWithChild {
  StringRef TypeName;
public:
  CheckValueTypeMatcherNode(StringRef type_name)
  : MatcherNodeWithChild(CheckValueType), TypeName(type_name) {}
  
  StringRef getTypeName() const { return TypeName; }

  static inline bool classof(const MatcherNode *N) {
    return N->getKind() == CheckValueType;
  }
  
  virtual void print(raw_ostream &OS, unsigned indent = 0) const;
};
  
  
  
/// CheckComplexPatMatcherNode - This node runs the specified ComplexPattern on
/// the current node.
class CheckComplexPatMatcherNode : public MatcherNodeWithChild {
  const ComplexPattern &Pattern;
public:
  CheckComplexPatMatcherNode(const ComplexPattern &pattern)
  : MatcherNodeWithChild(CheckComplexPat), Pattern(pattern) {}
  
  static inline bool classof(const MatcherNode *N) {
    return N->getKind() == CheckComplexPat;
  }
  
  virtual void print(raw_ostream &OS, unsigned indent = 0) const;
};
  
/// CheckAndImmMatcherNode - This checks to see if the current node is an 'and'
/// with something equivalent to the specified immediate.
class CheckAndImmMatcherNode : public MatcherNodeWithChild {
  int64_t Value;
public:
  CheckAndImmMatcherNode(int64_t value)
  : MatcherNodeWithChild(CheckAndImm), Value(value) {}
  
  int64_t getValue() const { return Value; }
  
  static inline bool classof(const MatcherNode *N) {
    return N->getKind() == CheckAndImm;
  }
  
  virtual void print(raw_ostream &OS, unsigned indent = 0) const;
};

/// CheckOrImmMatcherNode - This checks to see if the current node is an 'and'
/// with something equivalent to the specified immediate.
class CheckOrImmMatcherNode : public MatcherNodeWithChild {
  int64_t Value;
public:
  CheckOrImmMatcherNode(int64_t value)
    : MatcherNodeWithChild(CheckOrImm), Value(value) {}
  
  int64_t getValue() const { return Value; }

  static inline bool classof(const MatcherNode *N) {
    return N->getKind() == CheckOrImm;
  }
  
  virtual void print(raw_ostream &OS, unsigned indent = 0) const;
};
  
  
} // end namespace llvm

#endif