blob: 41fbcb4c84a7680bb3b9113ae343be948a1f01ac [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 Lattner78039ec2010-02-18 02:53:41 +000034 // 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;
Chris Lattnere7d6e3c2010-02-15 08:04:42 +000037public:
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,
Chris Lattner9de39482010-02-16 06:10:58 +000055 CheckOrImm,
Chris Lattner283adcb2010-02-17 06:23:39 +000056 CheckFoldableChainNode,
57 CheckChainCompatible
Chris Lattnere7d6e3c2010-02-15 08:04:42 +000058 };
59 const KindTy Kind;
Chris Lattner3b31c982010-02-18 02:49:24 +000060
Chris Lattnere7d6e3c2010-02-15 08:04:42 +000061protected:
62 MatcherNode(KindTy K) : Kind(K) {}
63public:
64 virtual ~MatcherNode() {}
65
66 KindTy getKind() const { return Kind; }
Chris Lattner3b31c982010-02-18 02:49:24 +000067
Chris Lattner78039ec2010-02-18 02:53:41 +000068 MatcherNode *getNext() { return Next.get(); }
69 const MatcherNode *getNext() const { return Next.get(); }
70 void setNext(MatcherNode *C) { Next.reset(C); }
Chris Lattnere7d6e3c2010-02-15 08:04:42 +000071
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;
Chris Lattner3b31c982010-02-18 02:49:24 +000076protected:
Chris Lattner78039ec2010-02-18 02:53:41 +000077 void printNext(raw_ostream &OS, unsigned indent) const;
Chris Lattnere7d6e3c2010-02-15 08:04:42 +000078};
79
80/// EmitNodeMatcherNode - This signals a successful match and generates a node.
81class EmitNodeMatcherNode : public MatcherNode {
82 const PatternToMatch &Pattern;
83public:
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
Chris Lattnere7d6e3c2010-02-15 08:04:42 +000096
97/// PushMatcherNode - This pushes a failure scope on the stack and evaluates
Chris Lattner78039ec2010-02-18 02:53:41 +000098/// 'Next'. If 'Next' fails to match, it pops its scope and attempts to
Chris Lattnere7d6e3c2010-02-15 08:04:42 +000099/// match 'Failure'.
Chris Lattner3b31c982010-02-18 02:49:24 +0000100class PushMatcherNode : public MatcherNode {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000101 OwningPtr<MatcherNode> Failure;
102public:
Chris Lattner78039ec2010-02-18 02:53:41 +0000103 PushMatcherNode(MatcherNode *next = 0, MatcherNode *failure = 0)
Chris Lattner3b31c982010-02-18 02:49:24 +0000104 : MatcherNode(Push), Failure(failure) {
Chris Lattner78039ec2010-02-18 02:53:41 +0000105 setNext(next);
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000106 }
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.
Chris Lattner3b31c982010-02-18 02:49:24 +0000120class RecordMatcherNode : public MatcherNode {
Chris Lattner56cf88d2010-02-17 01:03:09 +0000121 /// 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;
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000124public:
Chris Lattner086ca522010-02-17 01:27:29 +0000125 RecordMatcherNode(const std::string &whatfor)
Chris Lattner3b31c982010-02-18 02:49:24 +0000126 : MatcherNode(Record), WhatFor(whatfor) {}
Chris Lattner56cf88d2010-02-17 01:03:09 +0000127
Chris Lattner086ca522010-02-17 01:27:29 +0000128 const std::string &getWhatFor() const { return WhatFor; }
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000129
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.
Chris Lattner3b31c982010-02-18 02:49:24 +0000139class MoveChildMatcherNode : public MatcherNode {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000140 unsigned ChildNo;
141public:
142 MoveChildMatcherNode(unsigned childNo)
Chris Lattner3b31c982010-02-18 02:49:24 +0000143 : MatcherNode(MoveChild), ChildNo(childNo) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000144
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.
Chris Lattner3b31c982010-02-18 02:49:24 +0000156class MoveParentMatcherNode : public MatcherNode {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000157public:
158 MoveParentMatcherNode()
Chris Lattner3b31c982010-02-18 02:49:24 +0000159 : MatcherNode(MoveParent) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000160
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)'.
Chris Lattner3b31c982010-02-18 02:49:24 +0000171class CheckSameMatcherNode : public MatcherNode {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000172 unsigned MatchNumber;
173public:
174 CheckSameMatcherNode(unsigned matchnumber)
Chris Lattner3b31c982010-02-18 02:49:24 +0000175 : MatcherNode(CheckSame), MatchNumber(matchnumber) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000176
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.
Chris Lattner3b31c982010-02-18 02:49:24 +0000189class CheckPatternPredicateMatcherNode : public MatcherNode {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000190 std::string Predicate;
191public:
192 CheckPatternPredicateMatcherNode(StringRef predicate)
Chris Lattner3b31c982010-02-18 02:49:24 +0000193 : MatcherNode(CheckPatternPredicate), Predicate(predicate) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000194
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.
Chris Lattner3b31c982010-02-18 02:49:24 +0000206class CheckPredicateMatcherNode : public MatcherNode {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000207 StringRef PredName;
208public:
209 CheckPredicateMatcherNode(StringRef predname)
Chris Lattner3b31c982010-02-18 02:49:24 +0000210 : MatcherNode(CheckPredicate), PredName(predname) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000211
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.
Chris Lattner3b31c982010-02-18 02:49:24 +0000224class CheckOpcodeMatcherNode : public MatcherNode {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000225 StringRef OpcodeName;
226public:
227 CheckOpcodeMatcherNode(StringRef opcodename)
Chris Lattner3b31c982010-02-18 02:49:24 +0000228 : MatcherNode(CheckOpcode), OpcodeName(opcodename) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000229
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.
Chris Lattner3b31c982010-02-18 02:49:24 +0000241class CheckTypeMatcherNode : public MatcherNode {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000242 MVT::SimpleValueType Type;
243public:
244 CheckTypeMatcherNode(MVT::SimpleValueType type)
Chris Lattner3b31c982010-02-18 02:49:24 +0000245 : MatcherNode(CheckType), Type(type) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000246
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.
Chris Lattner3b31c982010-02-18 02:49:24 +0000258class CheckIntegerMatcherNode : public MatcherNode {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000259 int64_t Value;
260public:
261 CheckIntegerMatcherNode(int64_t value)
Chris Lattner3b31c982010-02-18 02:49:24 +0000262 : MatcherNode(CheckInteger), Value(value) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000263
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.
Chris Lattner3b31c982010-02-18 02:49:24 +0000275class CheckCondCodeMatcherNode : public MatcherNode {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000276 StringRef CondCodeName;
277public:
278 CheckCondCodeMatcherNode(StringRef condcodename)
Chris Lattner3b31c982010-02-18 02:49:24 +0000279 : MatcherNode(CheckCondCode), CondCodeName(condcodename) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000280
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.
Chris Lattner3b31c982010-02-18 02:49:24 +0000292class CheckValueTypeMatcherNode : public MatcherNode {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000293 StringRef TypeName;
294public:
295 CheckValueTypeMatcherNode(StringRef type_name)
Chris Lattner3b31c982010-02-18 02:49:24 +0000296 : MatcherNode(CheckValueType), TypeName(type_name) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000297
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.
Chris Lattner3b31c982010-02-18 02:49:24 +0000311class CheckComplexPatMatcherNode : public MatcherNode {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000312 const ComplexPattern &Pattern;
313public:
314 CheckComplexPatMatcherNode(const ComplexPattern &pattern)
Chris Lattner3b31c982010-02-18 02:49:24 +0000315 : MatcherNode(CheckComplexPat), Pattern(pattern) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000316
Chris Lattnerdc429fd2010-02-17 00:31:50 +0000317 const ComplexPattern &getPattern() const { return Pattern; }
318
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000319 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.
Chris Lattner3b31c982010-02-18 02:49:24 +0000328class CheckAndImmMatcherNode : public MatcherNode {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000329 int64_t Value;
330public:
331 CheckAndImmMatcherNode(int64_t value)
Chris Lattner3b31c982010-02-18 02:49:24 +0000332 : MatcherNode(CheckAndImm), Value(value) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000333
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.
Chris Lattner3b31c982010-02-18 02:49:24 +0000345class CheckOrImmMatcherNode : public MatcherNode {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000346 int64_t Value;
347public:
348 CheckOrImmMatcherNode(int64_t value)
Chris Lattner3b31c982010-02-18 02:49:24 +0000349 : MatcherNode(CheckOrImm), Value(value) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000350
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};
Chris Lattner9de39482010-02-16 06:10:58 +0000359
Chris Lattnerc6dc8f62010-02-16 19:15:55 +0000360/// CheckFoldableChainNodeMatcherNode - This checks to see if the current node
361/// (which defines a chain operand) is safe to fold into a larger pattern.
Chris Lattner3b31c982010-02-18 02:49:24 +0000362class CheckFoldableChainNodeMatcherNode : public MatcherNode {
Chris Lattner9de39482010-02-16 06:10:58 +0000363public:
Chris Lattnerc6dc8f62010-02-16 19:15:55 +0000364 CheckFoldableChainNodeMatcherNode()
Chris Lattner3b31c982010-02-18 02:49:24 +0000365 : MatcherNode(CheckFoldableChainNode) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000366
Chris Lattner9de39482010-02-16 06:10:58 +0000367 static inline bool classof(const MatcherNode *N) {
Chris Lattnerc6dc8f62010-02-16 19:15:55 +0000368 return N->getKind() == CheckFoldableChainNode;
Chris Lattner9de39482010-02-16 06:10:58 +0000369 }
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000370
Chris Lattner9de39482010-02-16 06:10:58 +0000371 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
372};
373
Chris Lattner283adcb2010-02-17 06:23:39 +0000374/// CheckChainCompatibleMatcherNode - Verify that the current node's chain
375/// operand is 'compatible' with the specified recorded node's.
Chris Lattner3b31c982010-02-18 02:49:24 +0000376class CheckChainCompatibleMatcherNode : public MatcherNode {
Chris Lattner283adcb2010-02-17 06:23:39 +0000377 unsigned PreviousOp;
378public:
379 CheckChainCompatibleMatcherNode(unsigned previousop)
Chris Lattner3b31c982010-02-18 02:49:24 +0000380 : MatcherNode(CheckChainCompatible), PreviousOp(previousop) {}
Chris Lattner283adcb2010-02-17 06:23:39 +0000381
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
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000393} // end namespace llvm
394
395#endif