blob: 4eb11010de90dd4728aeb982db4ef447f6f6fe20 [file] [log] [blame]
Chris Lattnere7d6e3c2010-02-15 08:04:42 +00001//===- 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
Chris Lattnerfdbdc8c2010-02-16 07:21:10 +000013#include "llvm/CodeGen/ValueTypes.h"
Chris Lattnere7d6e3c2010-02-15 08:04:42 +000014#include "llvm/ADT/OwningPtr.h"
15#include "llvm/ADT/StringRef.h"
Chris Lattnerfdbdc8c2010-02-16 07:21:10 +000016#include "llvm/Support/Casting.h"
Chris Lattnere7d6e3c2010-02-15 08:04:42 +000017
18namespace llvm {
19 class CodeGenDAGPatterns;
20 class MatcherNode;
21 class PatternToMatch;
22 class raw_ostream;
23 class ComplexPattern;
24
25MatcherNode *ConvertPatternToMatcher(const PatternToMatch &Pattern,
26 const CodeGenDAGPatterns &CGP);
27
28void EmitMatcherTable(const MatcherNode *Matcher, raw_ostream &OS);
29
30
31/// MatcherNode - Base class for all the the DAG ISel Matcher representation
32/// nodes.
33class MatcherNode {
Chris Lattner3b31c982010-02-18 02:49:24 +000034 OwningPtr<MatcherNode> Child;
Chris Lattnere7d6e3c2010-02-15 08:04:42 +000035public:
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,
Chris Lattner9de39482010-02-16 06:10:58 +000053 CheckOrImm,
Chris Lattner283adcb2010-02-17 06:23:39 +000054 CheckFoldableChainNode,
55 CheckChainCompatible
Chris Lattnere7d6e3c2010-02-15 08:04:42 +000056 };
57 const KindTy Kind;
Chris Lattner3b31c982010-02-18 02:49:24 +000058
Chris Lattnere7d6e3c2010-02-15 08:04:42 +000059protected:
60 MatcherNode(KindTy K) : Kind(K) {}
61public:
62 virtual ~MatcherNode() {}
63
64 KindTy getKind() const { return Kind; }
Chris Lattner3b31c982010-02-18 02:49:24 +000065
66 MatcherNode *getChild() { return Child.get(); }
67 const MatcherNode *getChild() const { return Child.get(); }
68 void setChild(MatcherNode *C) { Child.reset(C); }
Chris Lattnere7d6e3c2010-02-15 08:04:42 +000069
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;
Chris Lattner3b31c982010-02-18 02:49:24 +000074protected:
75 void printChild(raw_ostream &OS, unsigned indent) const;
Chris Lattnere7d6e3c2010-02-15 08:04:42 +000076};
77
78/// EmitNodeMatcherNode - This signals a successful match and generates a node.
79class EmitNodeMatcherNode : public MatcherNode {
80 const PatternToMatch &Pattern;
81public:
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
Chris Lattnere7d6e3c2010-02-15 08:04:42 +000094
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'.
Chris Lattner3b31c982010-02-18 02:49:24 +000098class PushMatcherNode : public MatcherNode {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +000099 OwningPtr<MatcherNode> Failure;
100public:
101 PushMatcherNode(MatcherNode *child = 0, MatcherNode *failure = 0)
Chris Lattner3b31c982010-02-18 02:49:24 +0000102 : MatcherNode(Push), Failure(failure) {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000103 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.
Chris Lattner3b31c982010-02-18 02:49:24 +0000118class RecordMatcherNode : public MatcherNode {
Chris Lattner56cf88d2010-02-17 01:03:09 +0000119 /// 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;
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000122public:
Chris Lattner086ca522010-02-17 01:27:29 +0000123 RecordMatcherNode(const std::string &whatfor)
Chris Lattner3b31c982010-02-18 02:49:24 +0000124 : MatcherNode(Record), WhatFor(whatfor) {}
Chris Lattner56cf88d2010-02-17 01:03:09 +0000125
Chris Lattner086ca522010-02-17 01:27:29 +0000126 const std::string &getWhatFor() const { return WhatFor; }
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000127
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.
Chris Lattner3b31c982010-02-18 02:49:24 +0000137class MoveChildMatcherNode : public MatcherNode {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000138 unsigned ChildNo;
139public:
140 MoveChildMatcherNode(unsigned childNo)
Chris Lattner3b31c982010-02-18 02:49:24 +0000141 : MatcherNode(MoveChild), ChildNo(childNo) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000142
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.
Chris Lattner3b31c982010-02-18 02:49:24 +0000154class MoveParentMatcherNode : public MatcherNode {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000155public:
156 MoveParentMatcherNode()
Chris Lattner3b31c982010-02-18 02:49:24 +0000157 : MatcherNode(MoveParent) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000158
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)'.
Chris Lattner3b31c982010-02-18 02:49:24 +0000169class CheckSameMatcherNode : public MatcherNode {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000170 unsigned MatchNumber;
171public:
172 CheckSameMatcherNode(unsigned matchnumber)
Chris Lattner3b31c982010-02-18 02:49:24 +0000173 : MatcherNode(CheckSame), MatchNumber(matchnumber) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000174
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.
Chris Lattner3b31c982010-02-18 02:49:24 +0000187class CheckPatternPredicateMatcherNode : public MatcherNode {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000188 std::string Predicate;
189public:
190 CheckPatternPredicateMatcherNode(StringRef predicate)
Chris Lattner3b31c982010-02-18 02:49:24 +0000191 : MatcherNode(CheckPatternPredicate), Predicate(predicate) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000192
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.
Chris Lattner3b31c982010-02-18 02:49:24 +0000204class CheckPredicateMatcherNode : public MatcherNode {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000205 StringRef PredName;
206public:
207 CheckPredicateMatcherNode(StringRef predname)
Chris Lattner3b31c982010-02-18 02:49:24 +0000208 : MatcherNode(CheckPredicate), PredName(predname) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000209
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.
Chris Lattner3b31c982010-02-18 02:49:24 +0000222class CheckOpcodeMatcherNode : public MatcherNode {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000223 StringRef OpcodeName;
224public:
225 CheckOpcodeMatcherNode(StringRef opcodename)
Chris Lattner3b31c982010-02-18 02:49:24 +0000226 : MatcherNode(CheckOpcode), OpcodeName(opcodename) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000227
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.
Chris Lattner3b31c982010-02-18 02:49:24 +0000239class CheckTypeMatcherNode : public MatcherNode {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000240 MVT::SimpleValueType Type;
241public:
242 CheckTypeMatcherNode(MVT::SimpleValueType type)
Chris Lattner3b31c982010-02-18 02:49:24 +0000243 : MatcherNode(CheckType), Type(type) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000244
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.
Chris Lattner3b31c982010-02-18 02:49:24 +0000256class CheckIntegerMatcherNode : public MatcherNode {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000257 int64_t Value;
258public:
259 CheckIntegerMatcherNode(int64_t value)
Chris Lattner3b31c982010-02-18 02:49:24 +0000260 : MatcherNode(CheckInteger), Value(value) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000261
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.
Chris Lattner3b31c982010-02-18 02:49:24 +0000273class CheckCondCodeMatcherNode : public MatcherNode {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000274 StringRef CondCodeName;
275public:
276 CheckCondCodeMatcherNode(StringRef condcodename)
Chris Lattner3b31c982010-02-18 02:49:24 +0000277 : MatcherNode(CheckCondCode), CondCodeName(condcodename) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000278
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.
Chris Lattner3b31c982010-02-18 02:49:24 +0000290class CheckValueTypeMatcherNode : public MatcherNode {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000291 StringRef TypeName;
292public:
293 CheckValueTypeMatcherNode(StringRef type_name)
Chris Lattner3b31c982010-02-18 02:49:24 +0000294 : MatcherNode(CheckValueType), TypeName(type_name) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000295
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.
Chris Lattner3b31c982010-02-18 02:49:24 +0000309class CheckComplexPatMatcherNode : public MatcherNode {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000310 const ComplexPattern &Pattern;
311public:
312 CheckComplexPatMatcherNode(const ComplexPattern &pattern)
Chris Lattner3b31c982010-02-18 02:49:24 +0000313 : MatcherNode(CheckComplexPat), Pattern(pattern) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000314
Chris Lattnerdc429fd2010-02-17 00:31:50 +0000315 const ComplexPattern &getPattern() const { return Pattern; }
316
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000317 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.
Chris Lattner3b31c982010-02-18 02:49:24 +0000326class CheckAndImmMatcherNode : public MatcherNode {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000327 int64_t Value;
328public:
329 CheckAndImmMatcherNode(int64_t value)
Chris Lattner3b31c982010-02-18 02:49:24 +0000330 : MatcherNode(CheckAndImm), Value(value) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000331
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.
Chris Lattner3b31c982010-02-18 02:49:24 +0000343class CheckOrImmMatcherNode : public MatcherNode {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000344 int64_t Value;
345public:
346 CheckOrImmMatcherNode(int64_t value)
Chris Lattner3b31c982010-02-18 02:49:24 +0000347 : MatcherNode(CheckOrImm), Value(value) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000348
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};
Chris Lattner9de39482010-02-16 06:10:58 +0000357
Chris Lattnerc6dc8f62010-02-16 19:15:55 +0000358/// CheckFoldableChainNodeMatcherNode - This checks to see if the current node
359/// (which defines a chain operand) is safe to fold into a larger pattern.
Chris Lattner3b31c982010-02-18 02:49:24 +0000360class CheckFoldableChainNodeMatcherNode : public MatcherNode {
Chris Lattner9de39482010-02-16 06:10:58 +0000361public:
Chris Lattnerc6dc8f62010-02-16 19:15:55 +0000362 CheckFoldableChainNodeMatcherNode()
Chris Lattner3b31c982010-02-18 02:49:24 +0000363 : MatcherNode(CheckFoldableChainNode) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000364
Chris Lattner9de39482010-02-16 06:10:58 +0000365 static inline bool classof(const MatcherNode *N) {
Chris Lattnerc6dc8f62010-02-16 19:15:55 +0000366 return N->getKind() == CheckFoldableChainNode;
Chris Lattner9de39482010-02-16 06:10:58 +0000367 }
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000368
Chris Lattner9de39482010-02-16 06:10:58 +0000369 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
370};
371
Chris Lattner283adcb2010-02-17 06:23:39 +0000372/// CheckChainCompatibleMatcherNode - Verify that the current node's chain
373/// operand is 'compatible' with the specified recorded node's.
Chris Lattner3b31c982010-02-18 02:49:24 +0000374class CheckChainCompatibleMatcherNode : public MatcherNode {
Chris Lattner283adcb2010-02-17 06:23:39 +0000375 unsigned PreviousOp;
376public:
377 CheckChainCompatibleMatcherNode(unsigned previousop)
Chris Lattner3b31c982010-02-18 02:49:24 +0000378 : MatcherNode(CheckChainCompatible), PreviousOp(previousop) {}
Chris Lattner283adcb2010-02-17 06:23:39 +0000379
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
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000391} // end namespace llvm
392
393#endif