blob: 7b6fbdb8bf5e061bcdf327fba34d1b26ddc9ca1e [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;
Chris Lattnerddfb5222010-02-18 22:03:03 +000024 class Record;
Chris Lattnere7d6e3c2010-02-15 08:04:42 +000025
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 {
Chris Lattner78039ec2010-02-18 02:53:41 +000035 // 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;
Chris Lattnere7d6e3c2010-02-15 08:04:42 +000038public:
39 enum KindTy {
Chris Lattnerddfb5222010-02-18 22:03:03 +000040 // 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.
Chris Lattnere7d6e3c2010-02-15 08:04:42 +000045
Chris Lattnerddfb5222010-02-18 22:03:03 +000046 // Predicate checking.
47 CheckSame, // Fail if not same as prev match.
Chris Lattnere7d6e3c2010-02-15 08:04:42 +000048 CheckPatternPredicate,
Chris Lattnerddfb5222010-02-18 22:03:03 +000049 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.
Chris Lattnere7d6e3c2010-02-15 08:04:42 +000054 CheckValueType,
55 CheckComplexPat,
56 CheckAndImm,
Chris Lattner9de39482010-02-16 06:10:58 +000057 CheckOrImm,
Chris Lattner283adcb2010-02-17 06:23:39 +000058 CheckFoldableChainNode,
Chris Lattnerddfb5222010-02-18 22:03:03 +000059 CheckChainCompatible,
60
61 // Node creation/emisssion.
62 EmitInteger, // Create a TargetConstant
63 EmitRegister, // Create a register.
64 EmitNode
Chris Lattnere7d6e3c2010-02-15 08:04:42 +000065 };
66 const KindTy Kind;
Chris Lattner3b31c982010-02-18 02:49:24 +000067
Chris Lattnere7d6e3c2010-02-15 08:04:42 +000068protected:
69 MatcherNode(KindTy K) : Kind(K) {}
70public:
71 virtual ~MatcherNode() {}
72
73 KindTy getKind() const { return Kind; }
Chris Lattner3b31c982010-02-18 02:49:24 +000074
Chris Lattner78039ec2010-02-18 02:53:41 +000075 MatcherNode *getNext() { return Next.get(); }
76 const MatcherNode *getNext() const { return Next.get(); }
77 void setNext(MatcherNode *C) { Next.reset(C); }
Chris Lattnere7d6e3c2010-02-15 08:04:42 +000078
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;
Chris Lattner3b31c982010-02-18 02:49:24 +000083protected:
Chris Lattner78039ec2010-02-18 02:53:41 +000084 void printNext(raw_ostream &OS, unsigned indent) const;
Chris Lattnere7d6e3c2010-02-15 08:04:42 +000085};
86
Chris Lattnere7d6e3c2010-02-15 08:04:42 +000087/// PushMatcherNode - This pushes a failure scope on the stack and evaluates
Chris Lattner78039ec2010-02-18 02:53:41 +000088/// 'Next'. If 'Next' fails to match, it pops its scope and attempts to
Chris Lattnere7d6e3c2010-02-15 08:04:42 +000089/// match 'Failure'.
Chris Lattner3b31c982010-02-18 02:49:24 +000090class PushMatcherNode : public MatcherNode {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +000091 OwningPtr<MatcherNode> Failure;
92public:
Chris Lattner78039ec2010-02-18 02:53:41 +000093 PushMatcherNode(MatcherNode *next = 0, MatcherNode *failure = 0)
Chris Lattner3b31c982010-02-18 02:49:24 +000094 : MatcherNode(Push), Failure(failure) {
Chris Lattner78039ec2010-02-18 02:53:41 +000095 setNext(next);
Chris Lattnere7d6e3c2010-02-15 08:04:42 +000096 }
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.
Chris Lattner3b31c982010-02-18 02:49:24 +0000110class RecordMatcherNode : public MatcherNode {
Chris Lattner56cf88d2010-02-17 01:03:09 +0000111 /// 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;
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000114public:
Chris Lattner086ca522010-02-17 01:27:29 +0000115 RecordMatcherNode(const std::string &whatfor)
Chris Lattnerddfb5222010-02-18 22:03:03 +0000116 : MatcherNode(RecordNode), WhatFor(whatfor) {}
Chris Lattner56cf88d2010-02-17 01:03:09 +0000117
Chris Lattner086ca522010-02-17 01:27:29 +0000118 const std::string &getWhatFor() const { return WhatFor; }
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000119
120 static inline bool classof(const MatcherNode *N) {
Chris Lattnerddfb5222010-02-18 22:03:03 +0000121 return N->getKind() == RecordNode;
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000122 }
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.
Chris Lattner3b31c982010-02-18 02:49:24 +0000129class MoveChildMatcherNode : public MatcherNode {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000130 unsigned ChildNo;
131public:
132 MoveChildMatcherNode(unsigned childNo)
Chris Lattner3b31c982010-02-18 02:49:24 +0000133 : MatcherNode(MoveChild), ChildNo(childNo) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000134
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.
Chris Lattner3b31c982010-02-18 02:49:24 +0000146class MoveParentMatcherNode : public MatcherNode {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000147public:
148 MoveParentMatcherNode()
Chris Lattner3b31c982010-02-18 02:49:24 +0000149 : MatcherNode(MoveParent) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000150
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)'.
Chris Lattner3b31c982010-02-18 02:49:24 +0000161class CheckSameMatcherNode : public MatcherNode {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000162 unsigned MatchNumber;
163public:
164 CheckSameMatcherNode(unsigned matchnumber)
Chris Lattner3b31c982010-02-18 02:49:24 +0000165 : MatcherNode(CheckSame), MatchNumber(matchnumber) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000166
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.
Chris Lattner3b31c982010-02-18 02:49:24 +0000179class CheckPatternPredicateMatcherNode : public MatcherNode {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000180 std::string Predicate;
181public:
182 CheckPatternPredicateMatcherNode(StringRef predicate)
Chris Lattner3b31c982010-02-18 02:49:24 +0000183 : MatcherNode(CheckPatternPredicate), Predicate(predicate) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000184
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.
Chris Lattner3b31c982010-02-18 02:49:24 +0000196class CheckPredicateMatcherNode : public MatcherNode {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000197 StringRef PredName;
198public:
199 CheckPredicateMatcherNode(StringRef predname)
Chris Lattner3b31c982010-02-18 02:49:24 +0000200 : MatcherNode(CheckPredicate), PredName(predname) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000201
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.
Chris Lattner3b31c982010-02-18 02:49:24 +0000214class CheckOpcodeMatcherNode : public MatcherNode {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000215 StringRef OpcodeName;
216public:
217 CheckOpcodeMatcherNode(StringRef opcodename)
Chris Lattner3b31c982010-02-18 02:49:24 +0000218 : MatcherNode(CheckOpcode), OpcodeName(opcodename) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000219
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.
Chris Lattner3b31c982010-02-18 02:49:24 +0000231class CheckTypeMatcherNode : public MatcherNode {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000232 MVT::SimpleValueType Type;
233public:
234 CheckTypeMatcherNode(MVT::SimpleValueType type)
Chris Lattner3b31c982010-02-18 02:49:24 +0000235 : MatcherNode(CheckType), Type(type) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000236
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.
Chris Lattner3b31c982010-02-18 02:49:24 +0000248class CheckIntegerMatcherNode : public MatcherNode {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000249 int64_t Value;
250public:
251 CheckIntegerMatcherNode(int64_t value)
Chris Lattner3b31c982010-02-18 02:49:24 +0000252 : MatcherNode(CheckInteger), Value(value) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000253
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.
Chris Lattner3b31c982010-02-18 02:49:24 +0000265class CheckCondCodeMatcherNode : public MatcherNode {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000266 StringRef CondCodeName;
267public:
268 CheckCondCodeMatcherNode(StringRef condcodename)
Chris Lattner3b31c982010-02-18 02:49:24 +0000269 : MatcherNode(CheckCondCode), CondCodeName(condcodename) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000270
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.
Chris Lattner3b31c982010-02-18 02:49:24 +0000282class CheckValueTypeMatcherNode : public MatcherNode {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000283 StringRef TypeName;
284public:
285 CheckValueTypeMatcherNode(StringRef type_name)
Chris Lattner3b31c982010-02-18 02:49:24 +0000286 : MatcherNode(CheckValueType), TypeName(type_name) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000287
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.
Chris Lattner3b31c982010-02-18 02:49:24 +0000301class CheckComplexPatMatcherNode : public MatcherNode {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000302 const ComplexPattern &Pattern;
303public:
304 CheckComplexPatMatcherNode(const ComplexPattern &pattern)
Chris Lattner3b31c982010-02-18 02:49:24 +0000305 : MatcherNode(CheckComplexPat), Pattern(pattern) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000306
Chris Lattnerdc429fd2010-02-17 00:31:50 +0000307 const ComplexPattern &getPattern() const { return Pattern; }
308
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000309 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.
Chris Lattner3b31c982010-02-18 02:49:24 +0000318class CheckAndImmMatcherNode : public MatcherNode {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000319 int64_t Value;
320public:
321 CheckAndImmMatcherNode(int64_t value)
Chris Lattner3b31c982010-02-18 02:49:24 +0000322 : MatcherNode(CheckAndImm), Value(value) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000323
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.
Chris Lattner3b31c982010-02-18 02:49:24 +0000335class CheckOrImmMatcherNode : public MatcherNode {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000336 int64_t Value;
337public:
338 CheckOrImmMatcherNode(int64_t value)
Chris Lattner3b31c982010-02-18 02:49:24 +0000339 : MatcherNode(CheckOrImm), Value(value) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000340
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};
Chris Lattner9de39482010-02-16 06:10:58 +0000349
Chris Lattnerc6dc8f62010-02-16 19:15:55 +0000350/// CheckFoldableChainNodeMatcherNode - This checks to see if the current node
351/// (which defines a chain operand) is safe to fold into a larger pattern.
Chris Lattner3b31c982010-02-18 02:49:24 +0000352class CheckFoldableChainNodeMatcherNode : public MatcherNode {
Chris Lattner9de39482010-02-16 06:10:58 +0000353public:
Chris Lattnerc6dc8f62010-02-16 19:15:55 +0000354 CheckFoldableChainNodeMatcherNode()
Chris Lattner3b31c982010-02-18 02:49:24 +0000355 : MatcherNode(CheckFoldableChainNode) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000356
Chris Lattner9de39482010-02-16 06:10:58 +0000357 static inline bool classof(const MatcherNode *N) {
Chris Lattnerc6dc8f62010-02-16 19:15:55 +0000358 return N->getKind() == CheckFoldableChainNode;
Chris Lattner9de39482010-02-16 06:10:58 +0000359 }
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000360
Chris Lattner9de39482010-02-16 06:10:58 +0000361 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
362};
363
Chris Lattner283adcb2010-02-17 06:23:39 +0000364/// CheckChainCompatibleMatcherNode - Verify that the current node's chain
365/// operand is 'compatible' with the specified recorded node's.
Chris Lattner3b31c982010-02-18 02:49:24 +0000366class CheckChainCompatibleMatcherNode : public MatcherNode {
Chris Lattner283adcb2010-02-17 06:23:39 +0000367 unsigned PreviousOp;
368public:
369 CheckChainCompatibleMatcherNode(unsigned previousop)
Chris Lattner3b31c982010-02-18 02:49:24 +0000370 : MatcherNode(CheckChainCompatible), PreviousOp(previousop) {}
Chris Lattner283adcb2010-02-17 06:23:39 +0000371
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
Chris Lattnerddfb5222010-02-18 22:03:03 +0000381/// 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) {}
Chris Lattner283adcb2010-02-17 06:23:39 +0000388
Chris Lattnerddfb5222010-02-18 22:03:03 +0000389 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
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000435} // end namespace llvm
436
437#endif