blob: f978188aae596922b4e4908348bf8fd958ccaa2f [file] [log] [blame]
Chris Lattnerda272d12010-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
13#include "llvm/ADT/OwningPtr.h"
Chris Lattner8e946be2010-02-21 03:22:59 +000014#include "llvm/ADT/SmallVector.h"
Chandler Carruth4ffd89f2012-12-04 10:37:14 +000015#include "llvm/ADT/StringRef.h"
16#include "llvm/CodeGen/ValueTypes.h"
Chris Lattner050a03d2010-02-16 07:21:10 +000017#include "llvm/Support/Casting.h"
Chris Lattnerda272d12010-02-15 08:04:42 +000018
19namespace llvm {
Jim Grosbach4a6d7352011-03-11 02:19:02 +000020 struct CodeGenRegister;
Chris Lattnerda272d12010-02-15 08:04:42 +000021 class CodeGenDAGPatterns;
Chris Lattnerb21ba712010-02-25 02:04:40 +000022 class Matcher;
Chris Lattnerda272d12010-02-15 08:04:42 +000023 class PatternToMatch;
24 class raw_ostream;
25 class ComplexPattern;
Chris Lattner845c0422010-02-18 22:03:03 +000026 class Record;
Chris Lattnera230f962010-02-27 21:48:43 +000027 class SDNodeInfo;
Chris Lattner54379062011-04-17 21:38:24 +000028 class TreePredicateFn;
29 class TreePattern;
Chris Lattnerda272d12010-02-15 08:04:42 +000030
Chris Lattnerfa342fa2010-03-01 07:17:40 +000031Matcher *ConvertPatternToMatcher(const PatternToMatch &Pattern,unsigned Variant,
Chris Lattnerb21ba712010-02-25 02:04:40 +000032 const CodeGenDAGPatterns &CGP);
Chris Lattnerc78f2a32010-02-28 20:49:53 +000033Matcher *OptimizeMatcher(Matcher *Matcher, const CodeGenDAGPatterns &CGP);
Chris Lattner4d0c9312010-03-01 01:54:19 +000034void EmitMatcherTable(const Matcher *Matcher, const CodeGenDAGPatterns &CGP,
Jim Grosbach4a6d7352011-03-11 02:19:02 +000035 raw_ostream &OS);
Chris Lattnerda272d12010-02-15 08:04:42 +000036
Jim Grosbachfbadcd02010-12-21 16:16:00 +000037
Sylvestre Ledruc8e41c52012-07-23 08:51:15 +000038/// Matcher - Base class for all the DAG ISel Matcher representation
Chris Lattnerda272d12010-02-15 08:04:42 +000039/// nodes.
Chris Lattnerb21ba712010-02-25 02:04:40 +000040class Matcher {
Chris Lattnerbd8227f2010-02-18 02:53:41 +000041 // The next matcher node that is executed after this one. Null if this is the
42 // last stage of a match.
Chris Lattnerb21ba712010-02-25 02:04:40 +000043 OwningPtr<Matcher> Next;
David Blaikie2d24e2a2011-12-20 02:50:00 +000044 virtual void anchor();
Chris Lattnerda272d12010-02-15 08:04:42 +000045public:
46 enum KindTy {
Chris Lattner8e946be2010-02-21 03:22:59 +000047 // Matcher state manipulation.
Chris Lattner60df53e2010-02-25 01:56:48 +000048 Scope, // Push a checking scope.
Chris Lattner8e946be2010-02-21 03:22:59 +000049 RecordNode, // Record the current node.
Chris Lattner19b5a752010-02-24 07:31:45 +000050 RecordChild, // Record a child of the current node.
Chris Lattner8e946be2010-02-21 03:22:59 +000051 RecordMemRef, // Record the memref in the current node.
Chris Lattner8950bca2010-12-23 17:03:20 +000052 CaptureGlueInput, // If the current node has an input glue, save it.
Chris Lattner8e946be2010-02-21 03:22:59 +000053 MoveChild, // Move current node to specified child.
54 MoveParent, // Move current node to parent.
Jim Grosbachfbadcd02010-12-21 16:16:00 +000055
Chris Lattner845c0422010-02-18 22:03:03 +000056 // Predicate checking.
Chris Lattner8e946be2010-02-21 03:22:59 +000057 CheckSame, // Fail if not same as prev match.
Chris Lattnerda272d12010-02-15 08:04:42 +000058 CheckPatternPredicate,
Chris Lattner8e946be2010-02-21 03:22:59 +000059 CheckPredicate, // Fail if node predicate fails.
60 CheckOpcode, // Fail if not opcode.
Chris Lattnereb669212010-03-01 06:59:22 +000061 SwitchOpcode, // Dispatch based on opcode.
Chris Lattner8e946be2010-02-21 03:22:59 +000062 CheckType, // Fail if not correct type.
Chris Lattnercfe2eab2010-03-03 06:28:15 +000063 SwitchType, // Dispatch based on type.
Chris Lattner23cfda72010-02-24 20:15:25 +000064 CheckChildType, // Fail if child has wrong type.
Chris Lattner8e946be2010-02-21 03:22:59 +000065 CheckInteger, // Fail if wrong val.
66 CheckCondCode, // Fail if not condcode.
Chris Lattnerda272d12010-02-15 08:04:42 +000067 CheckValueType,
68 CheckComplexPat,
69 CheckAndImm,
Chris Lattnere39650a2010-02-16 06:10:58 +000070 CheckOrImm,
Chris Lattner9a747f12010-02-17 06:23:39 +000071 CheckFoldableChainNode,
Jim Grosbachfbadcd02010-12-21 16:16:00 +000072
Chris Lattner845c0422010-02-18 22:03:03 +000073 // Node creation/emisssion.
Chris Lattner8e946be2010-02-21 03:22:59 +000074 EmitInteger, // Create a TargetConstant
75 EmitStringInteger, // Create a TargetConstant from a string.
76 EmitRegister, // Create a register.
77 EmitConvertToTarget, // Convert a imm/fpimm to target imm/fpimm
78 EmitMergeInputChains, // Merge together a chains for an input.
79 EmitCopyToReg, // Emit a copytoreg into a physreg.
80 EmitNode, // Create a DAG node
81 EmitNodeXForm, // Run a SDNodeXForm
Chris Lattner8950bca2010-12-23 17:03:20 +000082 MarkGlueResults, // Indicate which interior nodes have glue results.
Chris Lattnere86097a2010-02-28 02:31:26 +000083 CompleteMatch, // Finish a match and update the results.
Chris Lattner9a215002010-02-28 20:55:18 +000084 MorphNodeTo // Build a node, finish a match and update results.
Chris Lattnerda272d12010-02-15 08:04:42 +000085 };
86 const KindTy Kind;
Chris Lattner8ef9c792010-02-18 02:49:24 +000087
Chris Lattnerda272d12010-02-15 08:04:42 +000088protected:
Chris Lattnerb21ba712010-02-25 02:04:40 +000089 Matcher(KindTy K) : Kind(K) {}
Chris Lattnerda272d12010-02-15 08:04:42 +000090public:
Chris Lattnerb21ba712010-02-25 02:04:40 +000091 virtual ~Matcher() {}
Jim Grosbachfbadcd02010-12-21 16:16:00 +000092
Chris Lattnerda272d12010-02-15 08:04:42 +000093 KindTy getKind() const { return Kind; }
Chris Lattner8ef9c792010-02-18 02:49:24 +000094
Chris Lattnerb21ba712010-02-25 02:04:40 +000095 Matcher *getNext() { return Next.get(); }
96 const Matcher *getNext() const { return Next.get(); }
97 void setNext(Matcher *C) { Next.reset(C); }
98 Matcher *takeNext() { return Next.take(); }
Chris Lattner19b5a752010-02-24 07:31:45 +000099
Chris Lattnerb21ba712010-02-25 02:04:40 +0000100 OwningPtr<Matcher> &getNextPtr() { return Next; }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000101
Chris Lattner58aa8342010-02-25 06:49:58 +0000102 bool isEqual(const Matcher *M) const {
103 if (getKind() != M->getKind()) return false;
104 return isEqualImpl(M);
105 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000106
Chris Lattner58aa8342010-02-25 06:49:58 +0000107 unsigned getHash() const {
Chris Lattnerca56fea2010-02-26 07:35:27 +0000108 // Clear the high bit so we don't conflict with tombstones etc.
109 return ((getHashImpl() << 4) ^ getKind()) & (~0U>>1);
Chris Lattner58aa8342010-02-25 06:49:58 +0000110 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000111
Chris Lattnerd323fd42010-02-27 06:22:57 +0000112 /// isSafeToReorderWithPatternPredicate - Return true if it is safe to sink a
113 /// PatternPredicate node past this one.
114 virtual bool isSafeToReorderWithPatternPredicate() const {
115 return false;
116 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000117
Chris Lattner48aa5752010-03-07 06:29:26 +0000118 /// isSimplePredicateNode - Return true if this is a simple predicate that
119 /// operates on the node or its children without potential side effects or a
120 /// change of the current node.
121 bool isSimplePredicateNode() const {
122 switch (getKind()) {
123 default: return false;
124 case CheckSame:
125 case CheckPatternPredicate:
126 case CheckPredicate:
127 case CheckOpcode:
128 case CheckType:
129 case CheckChildType:
130 case CheckInteger:
131 case CheckCondCode:
132 case CheckValueType:
133 case CheckAndImm:
134 case CheckOrImm:
135 case CheckFoldableChainNode:
136 return true;
137 }
138 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000139
Chris Lattner48aa5752010-03-07 06:29:26 +0000140 /// isSimplePredicateOrRecordNode - Return true if this is a record node or
141 /// a simple predicate.
142 bool isSimplePredicateOrRecordNode() const {
143 return isSimplePredicateNode() ||
144 getKind() == RecordNode || getKind() == RecordChild;
145 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000146
Chris Lattner48aa5752010-03-07 06:29:26 +0000147 /// unlinkNode - Unlink the specified node from this chain. If Other == this,
148 /// we unlink the next pointer and return it. Otherwise we unlink Other from
149 /// the list and return this.
150 Matcher *unlinkNode(Matcher *Other);
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000151
Chris Lattner48aa5752010-03-07 06:29:26 +0000152 /// canMoveBefore - Return true if this matcher is the same as Other, or if
153 /// we can move this matcher past all of the nodes in-between Other and this
154 /// node. Other must be equal to or before this.
155 bool canMoveBefore(const Matcher *Other) const;
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000156
Chris Lattner48aa5752010-03-07 06:29:26 +0000157 /// canMoveBefore - Return true if it is safe to move the current matcher
158 /// across the specified one.
159 bool canMoveBeforeNode(const Matcher *Other) const;
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000160
Chris Lattner82781b92010-02-27 07:49:13 +0000161 /// isContradictory - Return true of these two matchers could never match on
162 /// the same node.
163 bool isContradictory(const Matcher *Other) const {
164 // Since this predicate is reflexive, we canonicalize the ordering so that
165 // we always match a node against nodes with kinds that are greater or equal
166 // to them. For example, we'll pass in a CheckType node as an argument to
167 // the CheckOpcode method, not the other way around.
168 if (getKind() < Other->getKind())
169 return isContradictoryImpl(Other);
170 return Other->isContradictoryImpl(this);
171 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000172
Chris Lattnera5028a62010-02-25 06:53:39 +0000173 void print(raw_ostream &OS, unsigned indent = 0) const;
Chris Lattner82781b92010-02-27 07:49:13 +0000174 void printOne(raw_ostream &OS) const;
Chris Lattnerda272d12010-02-15 08:04:42 +0000175 void dump() const;
Chris Lattner8ef9c792010-02-18 02:49:24 +0000176protected:
Chris Lattnera5028a62010-02-25 06:53:39 +0000177 virtual void printImpl(raw_ostream &OS, unsigned indent) const = 0;
Chris Lattner58aa8342010-02-25 06:49:58 +0000178 virtual bool isEqualImpl(const Matcher *M) const = 0;
179 virtual unsigned getHashImpl() const = 0;
Chris Lattner82781b92010-02-27 07:49:13 +0000180 virtual bool isContradictoryImpl(const Matcher *M) const { return false; }
Chris Lattnerda272d12010-02-15 08:04:42 +0000181};
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000182
Chris Lattnerd6c84722010-02-25 19:00:39 +0000183/// ScopeMatcher - This attempts to match each of its children to find the first
184/// one that successfully matches. If one child fails, it tries the next child.
185/// If none of the children match then this check fails. It never has a 'next'.
Chris Lattnerb21ba712010-02-25 02:04:40 +0000186class ScopeMatcher : public Matcher {
Chris Lattnerd6c84722010-02-25 19:00:39 +0000187 SmallVector<Matcher*, 4> Children;
Chris Lattnerda272d12010-02-15 08:04:42 +0000188public:
Chris Lattnerd6c84722010-02-25 19:00:39 +0000189 ScopeMatcher(Matcher *const *children, unsigned numchildren)
190 : Matcher(Scope), Children(children, children+numchildren) {
Chris Lattnerda272d12010-02-15 08:04:42 +0000191 }
Chris Lattnerd6c84722010-02-25 19:00:39 +0000192 virtual ~ScopeMatcher();
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000193
Chris Lattnerd6c84722010-02-25 19:00:39 +0000194 unsigned getNumChildren() const { return Children.size(); }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000195
Chris Lattnerd6c84722010-02-25 19:00:39 +0000196 Matcher *getChild(unsigned i) { return Children[i]; }
197 const Matcher *getChild(unsigned i) const { return Children[i]; }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000198
Chris Lattnerd6c84722010-02-25 19:00:39 +0000199 void resetChild(unsigned i, Matcher *N) {
200 delete Children[i];
201 Children[i] = N;
202 }
203
204 Matcher *takeChild(unsigned i) {
205 Matcher *Res = Children[i];
206 Children[i] = 0;
207 return Res;
208 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000209
Chris Lattnerca56fea2010-02-26 07:35:27 +0000210 void setNumChildren(unsigned NC) {
211 if (NC < Children.size()) {
212 // delete any children we're about to lose pointers to.
213 for (unsigned i = NC, e = Children.size(); i != e; ++i)
214 delete Children[i];
215 }
216 Children.resize(NC);
217 }
Chris Lattnerda272d12010-02-15 08:04:42 +0000218
Chris Lattnerb21ba712010-02-25 02:04:40 +0000219 static inline bool classof(const Matcher *N) {
Chris Lattner60df53e2010-02-25 01:56:48 +0000220 return N->getKind() == Scope;
Chris Lattnerda272d12010-02-15 08:04:42 +0000221 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000222
Chris Lattner58aa8342010-02-25 06:49:58 +0000223private:
Chris Lattnera5028a62010-02-25 06:53:39 +0000224 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner58aa8342010-02-25 06:49:58 +0000225 virtual bool isEqualImpl(const Matcher *M) const { return false; }
Chris Lattnerd6c84722010-02-25 19:00:39 +0000226 virtual unsigned getHashImpl() const { return 12312; }
Chris Lattnerda272d12010-02-15 08:04:42 +0000227};
228
Chris Lattnerb21ba712010-02-25 02:04:40 +0000229/// RecordMatcher - Save the current node in the operand list.
230class RecordMatcher : public Matcher {
Chris Lattnerc96087b2010-02-17 01:03:09 +0000231 /// WhatFor - This is a string indicating why we're recording this. This
232 /// should only be used for comment generation not anything semantic.
233 std::string WhatFor;
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000234
Chris Lattner0cebe612010-03-01 02:24:17 +0000235 /// ResultNo - The slot number in the RecordedNodes vector that this will be,
236 /// just printed as a comment.
237 unsigned ResultNo;
Chris Lattnerda272d12010-02-15 08:04:42 +0000238public:
Chris Lattner0cebe612010-03-01 02:24:17 +0000239 RecordMatcher(const std::string &whatfor, unsigned resultNo)
240 : Matcher(RecordNode), WhatFor(whatfor), ResultNo(resultNo) {}
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000241
Chris Lattnerc642b842010-02-17 01:27:29 +0000242 const std::string &getWhatFor() const { return WhatFor; }
Chris Lattner0cebe612010-03-01 02:24:17 +0000243 unsigned getResultNo() const { return ResultNo; }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000244
Chris Lattnerb21ba712010-02-25 02:04:40 +0000245 static inline bool classof(const Matcher *N) {
Chris Lattner845c0422010-02-18 22:03:03 +0000246 return N->getKind() == RecordNode;
Chris Lattnerda272d12010-02-15 08:04:42 +0000247 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000248
Chris Lattnerd323fd42010-02-27 06:22:57 +0000249 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
Chris Lattner58aa8342010-02-25 06:49:58 +0000250private:
Chris Lattnera5028a62010-02-25 06:53:39 +0000251 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner58aa8342010-02-25 06:49:58 +0000252 virtual bool isEqualImpl(const Matcher *M) const { return true; }
253 virtual unsigned getHashImpl() const { return 0; }
Chris Lattnerda272d12010-02-15 08:04:42 +0000254};
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000255
Chris Lattnerb21ba712010-02-25 02:04:40 +0000256/// RecordChildMatcher - Save a numbered child of the current node, or fail
Chris Lattner19b5a752010-02-24 07:31:45 +0000257/// the match if it doesn't exist. This is logically equivalent to:
258/// MoveChild N + RecordNode + MoveParent.
Chris Lattnerb21ba712010-02-25 02:04:40 +0000259class RecordChildMatcher : public Matcher {
Chris Lattner19b5a752010-02-24 07:31:45 +0000260 unsigned ChildNo;
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000261
Chris Lattner19b5a752010-02-24 07:31:45 +0000262 /// WhatFor - This is a string indicating why we're recording this. This
263 /// should only be used for comment generation not anything semantic.
264 std::string WhatFor;
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000265
Chris Lattner0cebe612010-03-01 02:24:17 +0000266 /// ResultNo - The slot number in the RecordedNodes vector that this will be,
267 /// just printed as a comment.
268 unsigned ResultNo;
Chris Lattner19b5a752010-02-24 07:31:45 +0000269public:
Chris Lattner0cebe612010-03-01 02:24:17 +0000270 RecordChildMatcher(unsigned childno, const std::string &whatfor,
271 unsigned resultNo)
272 : Matcher(RecordChild), ChildNo(childno), WhatFor(whatfor),
273 ResultNo(resultNo) {}
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000274
Chris Lattner19b5a752010-02-24 07:31:45 +0000275 unsigned getChildNo() const { return ChildNo; }
276 const std::string &getWhatFor() const { return WhatFor; }
Chris Lattner0cebe612010-03-01 02:24:17 +0000277 unsigned getResultNo() const { return ResultNo; }
278
Chris Lattnerb21ba712010-02-25 02:04:40 +0000279 static inline bool classof(const Matcher *N) {
Chris Lattner19b5a752010-02-24 07:31:45 +0000280 return N->getKind() == RecordChild;
281 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000282
Chris Lattnerd323fd42010-02-27 06:22:57 +0000283 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
284
Chris Lattner58aa8342010-02-25 06:49:58 +0000285private:
Chris Lattnera5028a62010-02-25 06:53:39 +0000286 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner58aa8342010-02-25 06:49:58 +0000287 virtual bool isEqualImpl(const Matcher *M) const {
288 return cast<RecordChildMatcher>(M)->getChildNo() == getChildNo();
289 }
290 virtual unsigned getHashImpl() const { return getChildNo(); }
Chris Lattner19b5a752010-02-24 07:31:45 +0000291};
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000292
Chris Lattnerb21ba712010-02-25 02:04:40 +0000293/// RecordMemRefMatcher - Save the current node's memref.
294class RecordMemRefMatcher : public Matcher {
Chris Lattner8e946be2010-02-21 03:22:59 +0000295public:
Chris Lattnerb21ba712010-02-25 02:04:40 +0000296 RecordMemRefMatcher() : Matcher(RecordMemRef) {}
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000297
Chris Lattnerb21ba712010-02-25 02:04:40 +0000298 static inline bool classof(const Matcher *N) {
Chris Lattner8e946be2010-02-21 03:22:59 +0000299 return N->getKind() == RecordMemRef;
300 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000301
Chris Lattnerd323fd42010-02-27 06:22:57 +0000302 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
303
Chris Lattner58aa8342010-02-25 06:49:58 +0000304private:
Chris Lattnera5028a62010-02-25 06:53:39 +0000305 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner58aa8342010-02-25 06:49:58 +0000306 virtual bool isEqualImpl(const Matcher *M) const { return true; }
307 virtual unsigned getHashImpl() const { return 0; }
Chris Lattner8e946be2010-02-21 03:22:59 +0000308};
309
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000310
Chris Lattner8950bca2010-12-23 17:03:20 +0000311/// CaptureGlueInputMatcher - If the current record has a glue input, record
Chris Lattner8e946be2010-02-21 03:22:59 +0000312/// it so that it is used as an input to the generated code.
Chris Lattner8950bca2010-12-23 17:03:20 +0000313class CaptureGlueInputMatcher : public Matcher {
Chris Lattner8e946be2010-02-21 03:22:59 +0000314public:
Chris Lattner8950bca2010-12-23 17:03:20 +0000315 CaptureGlueInputMatcher() : Matcher(CaptureGlueInput) {}
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000316
Chris Lattnerb21ba712010-02-25 02:04:40 +0000317 static inline bool classof(const Matcher *N) {
Chris Lattner8950bca2010-12-23 17:03:20 +0000318 return N->getKind() == CaptureGlueInput;
Chris Lattner8e946be2010-02-21 03:22:59 +0000319 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000320
Chris Lattnerd323fd42010-02-27 06:22:57 +0000321 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
322
Chris Lattner58aa8342010-02-25 06:49:58 +0000323private:
Chris Lattnera5028a62010-02-25 06:53:39 +0000324 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner58aa8342010-02-25 06:49:58 +0000325 virtual bool isEqualImpl(const Matcher *M) const { return true; }
326 virtual unsigned getHashImpl() const { return 0; }
Chris Lattner8e946be2010-02-21 03:22:59 +0000327};
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000328
Chris Lattnerb21ba712010-02-25 02:04:40 +0000329/// MoveChildMatcher - This tells the interpreter to move into the
Chris Lattnerda272d12010-02-15 08:04:42 +0000330/// specified child node.
Chris Lattnerb21ba712010-02-25 02:04:40 +0000331class MoveChildMatcher : public Matcher {
Chris Lattnerda272d12010-02-15 08:04:42 +0000332 unsigned ChildNo;
333public:
Chris Lattnerb21ba712010-02-25 02:04:40 +0000334 MoveChildMatcher(unsigned childNo) : Matcher(MoveChild), ChildNo(childNo) {}
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000335
Chris Lattnerda272d12010-02-15 08:04:42 +0000336 unsigned getChildNo() const { return ChildNo; }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000337
Chris Lattnerb21ba712010-02-25 02:04:40 +0000338 static inline bool classof(const Matcher *N) {
Chris Lattnerda272d12010-02-15 08:04:42 +0000339 return N->getKind() == MoveChild;
340 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000341
Chris Lattnerd323fd42010-02-27 06:22:57 +0000342 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
343
Chris Lattner58aa8342010-02-25 06:49:58 +0000344private:
Chris Lattnera5028a62010-02-25 06:53:39 +0000345 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner58aa8342010-02-25 06:49:58 +0000346 virtual bool isEqualImpl(const Matcher *M) const {
347 return cast<MoveChildMatcher>(M)->getChildNo() == getChildNo();
348 }
349 virtual unsigned getHashImpl() const { return getChildNo(); }
Chris Lattnerda272d12010-02-15 08:04:42 +0000350};
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000351
Chris Lattnerb21ba712010-02-25 02:04:40 +0000352/// MoveParentMatcher - This tells the interpreter to move to the parent
Chris Lattnerda272d12010-02-15 08:04:42 +0000353/// of the current node.
Chris Lattnerb21ba712010-02-25 02:04:40 +0000354class MoveParentMatcher : public Matcher {
Chris Lattnerda272d12010-02-15 08:04:42 +0000355public:
Chris Lattnerb21ba712010-02-25 02:04:40 +0000356 MoveParentMatcher() : Matcher(MoveParent) {}
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000357
Chris Lattnerb21ba712010-02-25 02:04:40 +0000358 static inline bool classof(const Matcher *N) {
Chris Lattnerda272d12010-02-15 08:04:42 +0000359 return N->getKind() == MoveParent;
360 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000361
Chris Lattnerd323fd42010-02-27 06:22:57 +0000362 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
363
Chris Lattner58aa8342010-02-25 06:49:58 +0000364private:
Chris Lattnera5028a62010-02-25 06:53:39 +0000365 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner58aa8342010-02-25 06:49:58 +0000366 virtual bool isEqualImpl(const Matcher *M) const { return true; }
367 virtual unsigned getHashImpl() const { return 0; }
Chris Lattnerda272d12010-02-15 08:04:42 +0000368};
369
Chris Lattnerb21ba712010-02-25 02:04:40 +0000370/// CheckSameMatcher - This checks to see if this node is exactly the same
Chris Lattnerda272d12010-02-15 08:04:42 +0000371/// node as the specified match that was recorded with 'Record'. This is used
372/// when patterns have the same name in them, like '(mul GPR:$in, GPR:$in)'.
Chris Lattnerb21ba712010-02-25 02:04:40 +0000373class CheckSameMatcher : public Matcher {
Chris Lattnerda272d12010-02-15 08:04:42 +0000374 unsigned MatchNumber;
375public:
Chris Lattnerb21ba712010-02-25 02:04:40 +0000376 CheckSameMatcher(unsigned matchnumber)
Chris Lattner58aa8342010-02-25 06:49:58 +0000377 : Matcher(CheckSame), MatchNumber(matchnumber) {}
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000378
Chris Lattnerda272d12010-02-15 08:04:42 +0000379 unsigned getMatchNumber() const { return MatchNumber; }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000380
Chris Lattnerb21ba712010-02-25 02:04:40 +0000381 static inline bool classof(const Matcher *N) {
Chris Lattnerda272d12010-02-15 08:04:42 +0000382 return N->getKind() == CheckSame;
383 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000384
Chris Lattnerd323fd42010-02-27 06:22:57 +0000385 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
386
Chris Lattner58aa8342010-02-25 06:49:58 +0000387private:
Chris Lattnera5028a62010-02-25 06:53:39 +0000388 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner58aa8342010-02-25 06:49:58 +0000389 virtual bool isEqualImpl(const Matcher *M) const {
390 return cast<CheckSameMatcher>(M)->getMatchNumber() == getMatchNumber();
391 }
392 virtual unsigned getHashImpl() const { return getMatchNumber(); }
Chris Lattnerda272d12010-02-15 08:04:42 +0000393};
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000394
Chris Lattnerb21ba712010-02-25 02:04:40 +0000395/// CheckPatternPredicateMatcher - This checks the target-specific predicate
Chris Lattnerda272d12010-02-15 08:04:42 +0000396/// to see if the entire pattern is capable of matching. This predicate does
397/// not take a node as input. This is used for subtarget feature checks etc.
Chris Lattnerb21ba712010-02-25 02:04:40 +0000398class CheckPatternPredicateMatcher : public Matcher {
Chris Lattnerda272d12010-02-15 08:04:42 +0000399 std::string Predicate;
400public:
Chris Lattnerb21ba712010-02-25 02:04:40 +0000401 CheckPatternPredicateMatcher(StringRef predicate)
Chris Lattner58aa8342010-02-25 06:49:58 +0000402 : Matcher(CheckPatternPredicate), Predicate(predicate) {}
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000403
Chris Lattnerda272d12010-02-15 08:04:42 +0000404 StringRef getPredicate() const { return Predicate; }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000405
Chris Lattnerb21ba712010-02-25 02:04:40 +0000406 static inline bool classof(const Matcher *N) {
Chris Lattnerda272d12010-02-15 08:04:42 +0000407 return N->getKind() == CheckPatternPredicate;
408 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000409
Chris Lattnerd323fd42010-02-27 06:22:57 +0000410 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
411
Chris Lattner58aa8342010-02-25 06:49:58 +0000412private:
Chris Lattnera5028a62010-02-25 06:53:39 +0000413 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner58aa8342010-02-25 06:49:58 +0000414 virtual bool isEqualImpl(const Matcher *M) const {
415 return cast<CheckPatternPredicateMatcher>(M)->getPredicate() == Predicate;
416 }
417 virtual unsigned getHashImpl() const;
Chris Lattnerda272d12010-02-15 08:04:42 +0000418};
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000419
Chris Lattnerb21ba712010-02-25 02:04:40 +0000420/// CheckPredicateMatcher - This checks the target-specific predicate to
Chris Lattnerda272d12010-02-15 08:04:42 +0000421/// see if the node is acceptable.
Chris Lattnerb21ba712010-02-25 02:04:40 +0000422class CheckPredicateMatcher : public Matcher {
Chris Lattner54379062011-04-17 21:38:24 +0000423 TreePattern *Pred;
Chris Lattnerda272d12010-02-15 08:04:42 +0000424public:
Chris Lattner54379062011-04-17 21:38:24 +0000425 CheckPredicateMatcher(const TreePredicateFn &pred);
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000426
Chris Lattner54379062011-04-17 21:38:24 +0000427 TreePredicateFn getPredicate() const;
Chris Lattnerda272d12010-02-15 08:04:42 +0000428
Chris Lattnerb21ba712010-02-25 02:04:40 +0000429 static inline bool classof(const Matcher *N) {
Chris Lattnerda272d12010-02-15 08:04:42 +0000430 return N->getKind() == CheckPredicate;
431 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000432
Chris Lattnerd323fd42010-02-27 06:22:57 +0000433 // TODO: Ok?
434 //virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
435
Chris Lattner58aa8342010-02-25 06:49:58 +0000436private:
Chris Lattnera5028a62010-02-25 06:53:39 +0000437 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner58aa8342010-02-25 06:49:58 +0000438 virtual bool isEqualImpl(const Matcher *M) const {
Chris Lattner54379062011-04-17 21:38:24 +0000439 return cast<CheckPredicateMatcher>(M)->Pred == Pred;
Chris Lattner58aa8342010-02-25 06:49:58 +0000440 }
441 virtual unsigned getHashImpl() const;
Chris Lattnerda272d12010-02-15 08:04:42 +0000442};
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000443
444
Chris Lattnerb21ba712010-02-25 02:04:40 +0000445/// CheckOpcodeMatcher - This checks to see if the current node has the
Chris Lattnerda272d12010-02-15 08:04:42 +0000446/// specified opcode, if not it fails to match.
Chris Lattnerb21ba712010-02-25 02:04:40 +0000447class CheckOpcodeMatcher : public Matcher {
Chris Lattnera230f962010-02-27 21:48:43 +0000448 const SDNodeInfo &Opcode;
Chris Lattnerda272d12010-02-15 08:04:42 +0000449public:
Chris Lattnera230f962010-02-27 21:48:43 +0000450 CheckOpcodeMatcher(const SDNodeInfo &opcode)
451 : Matcher(CheckOpcode), Opcode(opcode) {}
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000452
Chris Lattnera230f962010-02-27 21:48:43 +0000453 const SDNodeInfo &getOpcode() const { return Opcode; }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000454
Chris Lattnerb21ba712010-02-25 02:04:40 +0000455 static inline bool classof(const Matcher *N) {
Chris Lattnerda272d12010-02-15 08:04:42 +0000456 return N->getKind() == CheckOpcode;
457 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000458
Chris Lattnerd323fd42010-02-27 06:22:57 +0000459 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
460
Chris Lattner58aa8342010-02-25 06:49:58 +0000461private:
Chris Lattnera5028a62010-02-25 06:53:39 +0000462 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattnereb669212010-03-01 06:59:22 +0000463 virtual bool isEqualImpl(const Matcher *M) const;
Chris Lattner58aa8342010-02-25 06:49:58 +0000464 virtual unsigned getHashImpl() const;
Chris Lattner82781b92010-02-27 07:49:13 +0000465 virtual bool isContradictoryImpl(const Matcher *M) const;
Chris Lattnerda272d12010-02-15 08:04:42 +0000466};
Chris Lattnereb669212010-03-01 06:59:22 +0000467
468/// SwitchOpcodeMatcher - Switch based on the current node's opcode, dispatching
469/// to one matcher per opcode. If the opcode doesn't match any of the cases,
470/// then the match fails. This is semantically equivalent to a Scope node where
471/// every child does a CheckOpcode, but is much faster.
472class SwitchOpcodeMatcher : public Matcher {
473 SmallVector<std::pair<const SDNodeInfo*, Matcher*>, 8> Cases;
474public:
475 SwitchOpcodeMatcher(const std::pair<const SDNodeInfo*, Matcher*> *cases,
476 unsigned numcases)
477 : Matcher(SwitchOpcode), Cases(cases, cases+numcases) {}
478
479 static inline bool classof(const Matcher *N) {
480 return N->getKind() == SwitchOpcode;
481 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000482
Chris Lattnereb669212010-03-01 06:59:22 +0000483 unsigned getNumCases() const { return Cases.size(); }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000484
Chris Lattnereb669212010-03-01 06:59:22 +0000485 const SDNodeInfo &getCaseOpcode(unsigned i) const { return *Cases[i].first; }
486 Matcher *getCaseMatcher(unsigned i) { return Cases[i].second; }
487 const Matcher *getCaseMatcher(unsigned i) const { return Cases[i].second; }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000488
Chris Lattnereb669212010-03-01 06:59:22 +0000489private:
490 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
491 virtual bool isEqualImpl(const Matcher *M) const { return false; }
492 virtual unsigned getHashImpl() const { return 4123; }
493};
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000494
Chris Lattnerb21ba712010-02-25 02:04:40 +0000495/// CheckTypeMatcher - This checks to see if the current node has the
Chris Lattner084df622010-03-24 00:41:19 +0000496/// specified type at the specified result, if not it fails to match.
Chris Lattnerb21ba712010-02-25 02:04:40 +0000497class CheckTypeMatcher : public Matcher {
Chris Lattnerda272d12010-02-15 08:04:42 +0000498 MVT::SimpleValueType Type;
Chris Lattner084df622010-03-24 00:41:19 +0000499 unsigned ResNo;
Chris Lattnerda272d12010-02-15 08:04:42 +0000500public:
Chris Lattner084df622010-03-24 00:41:19 +0000501 CheckTypeMatcher(MVT::SimpleValueType type, unsigned resno)
502 : Matcher(CheckType), Type(type), ResNo(resno) {}
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000503
Chris Lattnerda272d12010-02-15 08:04:42 +0000504 MVT::SimpleValueType getType() const { return Type; }
Chris Lattner084df622010-03-24 00:41:19 +0000505 unsigned getResNo() const { return ResNo; }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000506
Chris Lattnerb21ba712010-02-25 02:04:40 +0000507 static inline bool classof(const Matcher *N) {
Chris Lattnerda272d12010-02-15 08:04:42 +0000508 return N->getKind() == CheckType;
509 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000510
Chris Lattnerd323fd42010-02-27 06:22:57 +0000511 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
512
Chris Lattner58aa8342010-02-25 06:49:58 +0000513private:
Chris Lattnera5028a62010-02-25 06:53:39 +0000514 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner58aa8342010-02-25 06:49:58 +0000515 virtual bool isEqualImpl(const Matcher *M) const {
Chris Lattner38717f62010-02-26 08:05:36 +0000516 return cast<CheckTypeMatcher>(M)->Type == Type;
Chris Lattner58aa8342010-02-25 06:49:58 +0000517 }
518 virtual unsigned getHashImpl() const { return Type; }
Chris Lattner82781b92010-02-27 07:49:13 +0000519 virtual bool isContradictoryImpl(const Matcher *M) const;
Chris Lattnerda272d12010-02-15 08:04:42 +0000520};
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000521
Chris Lattnercfe2eab2010-03-03 06:28:15 +0000522/// SwitchTypeMatcher - Switch based on the current node's type, dispatching
523/// to one matcher per case. If the type doesn't match any of the cases,
524/// then the match fails. This is semantically equivalent to a Scope node where
525/// every child does a CheckType, but is much faster.
526class SwitchTypeMatcher : public Matcher {
527 SmallVector<std::pair<MVT::SimpleValueType, Matcher*>, 8> Cases;
528public:
529 SwitchTypeMatcher(const std::pair<MVT::SimpleValueType, Matcher*> *cases,
530 unsigned numcases)
531 : Matcher(SwitchType), Cases(cases, cases+numcases) {}
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000532
Chris Lattnercfe2eab2010-03-03 06:28:15 +0000533 static inline bool classof(const Matcher *N) {
534 return N->getKind() == SwitchType;
535 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000536
Chris Lattnercfe2eab2010-03-03 06:28:15 +0000537 unsigned getNumCases() const { return Cases.size(); }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000538
Chris Lattnercfe2eab2010-03-03 06:28:15 +0000539 MVT::SimpleValueType getCaseType(unsigned i) const { return Cases[i].first; }
540 Matcher *getCaseMatcher(unsigned i) { return Cases[i].second; }
541 const Matcher *getCaseMatcher(unsigned i) const { return Cases[i].second; }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000542
Chris Lattnercfe2eab2010-03-03 06:28:15 +0000543private:
544 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
545 virtual bool isEqualImpl(const Matcher *M) const { return false; }
546 virtual unsigned getHashImpl() const { return 4123; }
547};
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000548
549
Chris Lattnerb21ba712010-02-25 02:04:40 +0000550/// CheckChildTypeMatcher - This checks to see if a child node has the
Chris Lattner23cfda72010-02-24 20:15:25 +0000551/// specified type, if not it fails to match.
Chris Lattnerb21ba712010-02-25 02:04:40 +0000552class CheckChildTypeMatcher : public Matcher {
Chris Lattner23cfda72010-02-24 20:15:25 +0000553 unsigned ChildNo;
554 MVT::SimpleValueType Type;
555public:
Chris Lattnerb21ba712010-02-25 02:04:40 +0000556 CheckChildTypeMatcher(unsigned childno, MVT::SimpleValueType type)
557 : Matcher(CheckChildType), ChildNo(childno), Type(type) {}
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000558
Chris Lattner23cfda72010-02-24 20:15:25 +0000559 unsigned getChildNo() const { return ChildNo; }
560 MVT::SimpleValueType getType() const { return Type; }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000561
Chris Lattnerb21ba712010-02-25 02:04:40 +0000562 static inline bool classof(const Matcher *N) {
Chris Lattner23cfda72010-02-24 20:15:25 +0000563 return N->getKind() == CheckChildType;
564 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000565
Chris Lattnerd323fd42010-02-27 06:22:57 +0000566 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
567
Chris Lattner58aa8342010-02-25 06:49:58 +0000568private:
Chris Lattnera5028a62010-02-25 06:53:39 +0000569 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner58aa8342010-02-25 06:49:58 +0000570 virtual bool isEqualImpl(const Matcher *M) const {
571 return cast<CheckChildTypeMatcher>(M)->ChildNo == ChildNo &&
572 cast<CheckChildTypeMatcher>(M)->Type == Type;
573 }
574 virtual unsigned getHashImpl() const { return (Type << 3) | ChildNo; }
Chris Lattner82781b92010-02-27 07:49:13 +0000575 virtual bool isContradictoryImpl(const Matcher *M) const;
Chris Lattner23cfda72010-02-24 20:15:25 +0000576};
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000577
Chris Lattnerda272d12010-02-15 08:04:42 +0000578
Chris Lattnerb21ba712010-02-25 02:04:40 +0000579/// CheckIntegerMatcher - This checks to see if the current node is a
Chris Lattnerda272d12010-02-15 08:04:42 +0000580/// ConstantSDNode with the specified integer value, if not it fails to match.
Chris Lattnerb21ba712010-02-25 02:04:40 +0000581class CheckIntegerMatcher : public Matcher {
Chris Lattnerda272d12010-02-15 08:04:42 +0000582 int64_t Value;
583public:
Chris Lattnerb21ba712010-02-25 02:04:40 +0000584 CheckIntegerMatcher(int64_t value)
585 : Matcher(CheckInteger), Value(value) {}
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000586
Chris Lattnerda272d12010-02-15 08:04:42 +0000587 int64_t getValue() const { return Value; }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000588
Chris Lattnerb21ba712010-02-25 02:04:40 +0000589 static inline bool classof(const Matcher *N) {
Chris Lattnerda272d12010-02-15 08:04:42 +0000590 return N->getKind() == CheckInteger;
591 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000592
Chris Lattnerd323fd42010-02-27 06:22:57 +0000593 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
594
Chris Lattner58aa8342010-02-25 06:49:58 +0000595private:
Chris Lattnera5028a62010-02-25 06:53:39 +0000596 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner58aa8342010-02-25 06:49:58 +0000597 virtual bool isEqualImpl(const Matcher *M) const {
598 return cast<CheckIntegerMatcher>(M)->Value == Value;
599 }
600 virtual unsigned getHashImpl() const { return Value; }
Chris Lattner24789622010-02-27 08:11:15 +0000601 virtual bool isContradictoryImpl(const Matcher *M) const;
Chris Lattnerda272d12010-02-15 08:04:42 +0000602};
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000603
Chris Lattnerb21ba712010-02-25 02:04:40 +0000604/// CheckCondCodeMatcher - This checks to see if the current node is a
Chris Lattnerda272d12010-02-15 08:04:42 +0000605/// CondCodeSDNode with the specified condition, if not it fails to match.
Chris Lattnerb21ba712010-02-25 02:04:40 +0000606class CheckCondCodeMatcher : public Matcher {
Chris Lattnerda272d12010-02-15 08:04:42 +0000607 StringRef CondCodeName;
608public:
Chris Lattnerb21ba712010-02-25 02:04:40 +0000609 CheckCondCodeMatcher(StringRef condcodename)
610 : Matcher(CheckCondCode), CondCodeName(condcodename) {}
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000611
Chris Lattnerda272d12010-02-15 08:04:42 +0000612 StringRef getCondCodeName() const { return CondCodeName; }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000613
Chris Lattnerb21ba712010-02-25 02:04:40 +0000614 static inline bool classof(const Matcher *N) {
Chris Lattnerda272d12010-02-15 08:04:42 +0000615 return N->getKind() == CheckCondCode;
616 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000617
Chris Lattnerd323fd42010-02-27 06:22:57 +0000618 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
619
Chris Lattner58aa8342010-02-25 06:49:58 +0000620private:
Chris Lattnera5028a62010-02-25 06:53:39 +0000621 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner58aa8342010-02-25 06:49:58 +0000622 virtual bool isEqualImpl(const Matcher *M) const {
623 return cast<CheckCondCodeMatcher>(M)->CondCodeName == CondCodeName;
624 }
625 virtual unsigned getHashImpl() const;
Chris Lattnerda272d12010-02-15 08:04:42 +0000626};
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000627
Chris Lattnerb21ba712010-02-25 02:04:40 +0000628/// CheckValueTypeMatcher - This checks to see if the current node is a
Chris Lattnerda272d12010-02-15 08:04:42 +0000629/// VTSDNode with the specified type, if not it fails to match.
Chris Lattnerb21ba712010-02-25 02:04:40 +0000630class CheckValueTypeMatcher : public Matcher {
Chris Lattnerda272d12010-02-15 08:04:42 +0000631 StringRef TypeName;
632public:
Chris Lattnerb21ba712010-02-25 02:04:40 +0000633 CheckValueTypeMatcher(StringRef type_name)
634 : Matcher(CheckValueType), TypeName(type_name) {}
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000635
Chris Lattnerda272d12010-02-15 08:04:42 +0000636 StringRef getTypeName() const { return TypeName; }
637
Chris Lattnerb21ba712010-02-25 02:04:40 +0000638 static inline bool classof(const Matcher *N) {
Chris Lattnerda272d12010-02-15 08:04:42 +0000639 return N->getKind() == CheckValueType;
640 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000641
Chris Lattnerd323fd42010-02-27 06:22:57 +0000642 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
643
Chris Lattner58aa8342010-02-25 06:49:58 +0000644private:
Chris Lattnera5028a62010-02-25 06:53:39 +0000645 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner58aa8342010-02-25 06:49:58 +0000646 virtual bool isEqualImpl(const Matcher *M) const {
647 return cast<CheckValueTypeMatcher>(M)->TypeName == TypeName;
648 }
649 virtual unsigned getHashImpl() const;
Chris Lattner48aa5752010-03-07 06:29:26 +0000650 bool isContradictoryImpl(const Matcher *M) const;
Chris Lattnerda272d12010-02-15 08:04:42 +0000651};
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000652
653
654
Chris Lattnerb21ba712010-02-25 02:04:40 +0000655/// CheckComplexPatMatcher - This node runs the specified ComplexPattern on
Chris Lattnerda272d12010-02-15 08:04:42 +0000656/// the current node.
Chris Lattnerb21ba712010-02-25 02:04:40 +0000657class CheckComplexPatMatcher : public Matcher {
Chris Lattnerda272d12010-02-15 08:04:42 +0000658 const ComplexPattern &Pattern;
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000659
660 /// MatchNumber - This is the recorded nodes slot that contains the node we
661 /// want to match against.
Chris Lattner57bf8a42010-03-04 01:23:08 +0000662 unsigned MatchNumber;
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000663
Chris Lattner57bf8a42010-03-04 01:23:08 +0000664 /// Name - The name of the node we're matching, for comment emission.
665 std::string Name;
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000666
Chris Lattnerd1aca7c2010-03-04 00:28:05 +0000667 /// FirstResult - This is the first slot in the RecordedNodes list that the
668 /// result of the match populates.
669 unsigned FirstResult;
Chris Lattnerda272d12010-02-15 08:04:42 +0000670public:
Chris Lattner57bf8a42010-03-04 01:23:08 +0000671 CheckComplexPatMatcher(const ComplexPattern &pattern, unsigned matchnumber,
672 const std::string &name, unsigned firstresult)
673 : Matcher(CheckComplexPat), Pattern(pattern), MatchNumber(matchnumber),
674 Name(name), FirstResult(firstresult) {}
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000675
Chris Lattnere609a512010-02-17 00:31:50 +0000676 const ComplexPattern &getPattern() const { return Pattern; }
Chris Lattner57bf8a42010-03-04 01:23:08 +0000677 unsigned getMatchNumber() const { return MatchNumber; }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000678
Chris Lattner57bf8a42010-03-04 01:23:08 +0000679 const std::string getName() const { return Name; }
Chris Lattnerd1aca7c2010-03-04 00:28:05 +0000680 unsigned getFirstResult() const { return FirstResult; }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000681
Chris Lattnerb21ba712010-02-25 02:04:40 +0000682 static inline bool classof(const Matcher *N) {
Chris Lattnerda272d12010-02-15 08:04:42 +0000683 return N->getKind() == CheckComplexPat;
684 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000685
Chris Lattnerd323fd42010-02-27 06:22:57 +0000686 // Not safe to move a pattern predicate past a complex pattern.
687 virtual bool isSafeToReorderWithPatternPredicate() const { return false; }
688
Chris Lattner58aa8342010-02-25 06:49:58 +0000689private:
Chris Lattnera5028a62010-02-25 06:53:39 +0000690 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner58aa8342010-02-25 06:49:58 +0000691 virtual bool isEqualImpl(const Matcher *M) const {
Chris Lattner57bf8a42010-03-04 01:23:08 +0000692 return &cast<CheckComplexPatMatcher>(M)->Pattern == &Pattern &&
693 cast<CheckComplexPatMatcher>(M)->MatchNumber == MatchNumber;
Chris Lattner58aa8342010-02-25 06:49:58 +0000694 }
695 virtual unsigned getHashImpl() const {
Chris Lattner57bf8a42010-03-04 01:23:08 +0000696 return (unsigned)(intptr_t)&Pattern ^ MatchNumber;
Chris Lattner58aa8342010-02-25 06:49:58 +0000697 }
Chris Lattnerda272d12010-02-15 08:04:42 +0000698};
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000699
Chris Lattnerb21ba712010-02-25 02:04:40 +0000700/// CheckAndImmMatcher - This checks to see if the current node is an 'and'
Chris Lattnerda272d12010-02-15 08:04:42 +0000701/// with something equivalent to the specified immediate.
Chris Lattnerb21ba712010-02-25 02:04:40 +0000702class CheckAndImmMatcher : public Matcher {
Chris Lattnerda272d12010-02-15 08:04:42 +0000703 int64_t Value;
704public:
Chris Lattnerb21ba712010-02-25 02:04:40 +0000705 CheckAndImmMatcher(int64_t value)
706 : Matcher(CheckAndImm), Value(value) {}
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000707
Chris Lattnerda272d12010-02-15 08:04:42 +0000708 int64_t getValue() const { return Value; }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000709
Chris Lattnerb21ba712010-02-25 02:04:40 +0000710 static inline bool classof(const Matcher *N) {
Chris Lattnerda272d12010-02-15 08:04:42 +0000711 return N->getKind() == CheckAndImm;
712 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000713
Chris Lattnerd323fd42010-02-27 06:22:57 +0000714 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
715
Chris Lattner58aa8342010-02-25 06:49:58 +0000716private:
Chris Lattnera5028a62010-02-25 06:53:39 +0000717 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner58aa8342010-02-25 06:49:58 +0000718 virtual bool isEqualImpl(const Matcher *M) const {
719 return cast<CheckAndImmMatcher>(M)->Value == Value;
720 }
721 virtual unsigned getHashImpl() const { return Value; }
Chris Lattnerda272d12010-02-15 08:04:42 +0000722};
723
Chris Lattnerb21ba712010-02-25 02:04:40 +0000724/// CheckOrImmMatcher - This checks to see if the current node is an 'and'
Chris Lattnerda272d12010-02-15 08:04:42 +0000725/// with something equivalent to the specified immediate.
Chris Lattnerb21ba712010-02-25 02:04:40 +0000726class CheckOrImmMatcher : public Matcher {
Chris Lattnerda272d12010-02-15 08:04:42 +0000727 int64_t Value;
728public:
Chris Lattnerb21ba712010-02-25 02:04:40 +0000729 CheckOrImmMatcher(int64_t value)
730 : Matcher(CheckOrImm), Value(value) {}
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000731
Chris Lattnerda272d12010-02-15 08:04:42 +0000732 int64_t getValue() const { return Value; }
733
Chris Lattnerb21ba712010-02-25 02:04:40 +0000734 static inline bool classof(const Matcher *N) {
Chris Lattnerda272d12010-02-15 08:04:42 +0000735 return N->getKind() == CheckOrImm;
736 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000737
Chris Lattnerd323fd42010-02-27 06:22:57 +0000738 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
739
Chris Lattner58aa8342010-02-25 06:49:58 +0000740private:
Chris Lattnera5028a62010-02-25 06:53:39 +0000741 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner58aa8342010-02-25 06:49:58 +0000742 virtual bool isEqualImpl(const Matcher *M) const {
743 return cast<CheckOrImmMatcher>(M)->Value == Value;
744 }
745 virtual unsigned getHashImpl() const { return Value; }
Chris Lattnerda272d12010-02-15 08:04:42 +0000746};
Chris Lattnere39650a2010-02-16 06:10:58 +0000747
Chris Lattnerb21ba712010-02-25 02:04:40 +0000748/// CheckFoldableChainNodeMatcher - This checks to see if the current node
Chris Lattner21390d72010-02-16 19:15:55 +0000749/// (which defines a chain operand) is safe to fold into a larger pattern.
Chris Lattnerb21ba712010-02-25 02:04:40 +0000750class CheckFoldableChainNodeMatcher : public Matcher {
Chris Lattnere39650a2010-02-16 06:10:58 +0000751public:
Chris Lattnerb21ba712010-02-25 02:04:40 +0000752 CheckFoldableChainNodeMatcher()
753 : Matcher(CheckFoldableChainNode) {}
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000754
Chris Lattnerb21ba712010-02-25 02:04:40 +0000755 static inline bool classof(const Matcher *N) {
Chris Lattner21390d72010-02-16 19:15:55 +0000756 return N->getKind() == CheckFoldableChainNode;
Chris Lattnere39650a2010-02-16 06:10:58 +0000757 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000758
Chris Lattnerd323fd42010-02-27 06:22:57 +0000759 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
760
Chris Lattner58aa8342010-02-25 06:49:58 +0000761private:
Chris Lattnera5028a62010-02-25 06:53:39 +0000762 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner58aa8342010-02-25 06:49:58 +0000763 virtual bool isEqualImpl(const Matcher *M) const { return true; }
764 virtual unsigned getHashImpl() const { return 0; }
Chris Lattnere39650a2010-02-16 06:10:58 +0000765};
766
Chris Lattnerb21ba712010-02-25 02:04:40 +0000767/// EmitIntegerMatcher - This creates a new TargetConstant.
768class EmitIntegerMatcher : public Matcher {
Chris Lattner845c0422010-02-18 22:03:03 +0000769 int64_t Val;
770 MVT::SimpleValueType VT;
771public:
Chris Lattnerb21ba712010-02-25 02:04:40 +0000772 EmitIntegerMatcher(int64_t val, MVT::SimpleValueType vt)
773 : Matcher(EmitInteger), Val(val), VT(vt) {}
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000774
Chris Lattner906b4992010-02-19 07:49:56 +0000775 int64_t getValue() const { return Val; }
Chris Lattner845c0422010-02-18 22:03:03 +0000776 MVT::SimpleValueType getVT() const { return VT; }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000777
Chris Lattnerb21ba712010-02-25 02:04:40 +0000778 static inline bool classof(const Matcher *N) {
Chris Lattner845c0422010-02-18 22:03:03 +0000779 return N->getKind() == EmitInteger;
780 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000781
Chris Lattner58aa8342010-02-25 06:49:58 +0000782private:
Chris Lattnera5028a62010-02-25 06:53:39 +0000783 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner58aa8342010-02-25 06:49:58 +0000784 virtual bool isEqualImpl(const Matcher *M) const {
785 return cast<EmitIntegerMatcher>(M)->Val == Val &&
786 cast<EmitIntegerMatcher>(M)->VT == VT;
787 }
788 virtual unsigned getHashImpl() const { return (Val << 4) | VT; }
Chris Lattner845c0422010-02-18 22:03:03 +0000789};
Chris Lattner8e946be2010-02-21 03:22:59 +0000790
Chris Lattnerb21ba712010-02-25 02:04:40 +0000791/// EmitStringIntegerMatcher - A target constant whose value is represented
Chris Lattner8e946be2010-02-21 03:22:59 +0000792/// by a string.
Chris Lattnerb21ba712010-02-25 02:04:40 +0000793class EmitStringIntegerMatcher : public Matcher {
Chris Lattner8e946be2010-02-21 03:22:59 +0000794 std::string Val;
795 MVT::SimpleValueType VT;
796public:
Chris Lattnerb21ba712010-02-25 02:04:40 +0000797 EmitStringIntegerMatcher(const std::string &val, MVT::SimpleValueType vt)
798 : Matcher(EmitStringInteger), Val(val), VT(vt) {}
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000799
Chris Lattner8e946be2010-02-21 03:22:59 +0000800 const std::string &getValue() const { return Val; }
801 MVT::SimpleValueType getVT() const { return VT; }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000802
Chris Lattnerb21ba712010-02-25 02:04:40 +0000803 static inline bool classof(const Matcher *N) {
Chris Lattner8e946be2010-02-21 03:22:59 +0000804 return N->getKind() == EmitStringInteger;
805 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000806
Chris Lattner58aa8342010-02-25 06:49:58 +0000807private:
Chris Lattnera5028a62010-02-25 06:53:39 +0000808 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner58aa8342010-02-25 06:49:58 +0000809 virtual bool isEqualImpl(const Matcher *M) const {
810 return cast<EmitStringIntegerMatcher>(M)->Val == Val &&
811 cast<EmitStringIntegerMatcher>(M)->VT == VT;
812 }
813 virtual unsigned getHashImpl() const;
Chris Lattner8e946be2010-02-21 03:22:59 +0000814};
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000815
Chris Lattnerb21ba712010-02-25 02:04:40 +0000816/// EmitRegisterMatcher - This creates a new TargetConstant.
817class EmitRegisterMatcher : public Matcher {
Chris Lattner845c0422010-02-18 22:03:03 +0000818 /// Reg - The def for the register that we're emitting. If this is null, then
819 /// this is a reference to zero_reg.
Jim Grosbach4a6d7352011-03-11 02:19:02 +0000820 const CodeGenRegister *Reg;
Chris Lattner845c0422010-02-18 22:03:03 +0000821 MVT::SimpleValueType VT;
822public:
Jim Grosbach4a6d7352011-03-11 02:19:02 +0000823 EmitRegisterMatcher(const CodeGenRegister *reg, MVT::SimpleValueType vt)
Chris Lattnerb21ba712010-02-25 02:04:40 +0000824 : Matcher(EmitRegister), Reg(reg), VT(vt) {}
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000825
Jim Grosbach4a6d7352011-03-11 02:19:02 +0000826 const CodeGenRegister *getReg() const { return Reg; }
Chris Lattner845c0422010-02-18 22:03:03 +0000827 MVT::SimpleValueType getVT() const { return VT; }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000828
Chris Lattnerb21ba712010-02-25 02:04:40 +0000829 static inline bool classof(const Matcher *N) {
Chris Lattner845c0422010-02-18 22:03:03 +0000830 return N->getKind() == EmitRegister;
831 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000832
Chris Lattner58aa8342010-02-25 06:49:58 +0000833private:
Chris Lattnera5028a62010-02-25 06:53:39 +0000834 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner58aa8342010-02-25 06:49:58 +0000835 virtual bool isEqualImpl(const Matcher *M) const {
836 return cast<EmitRegisterMatcher>(M)->Reg == Reg &&
837 cast<EmitRegisterMatcher>(M)->VT == VT;
838 }
839 virtual unsigned getHashImpl() const {
840 return ((unsigned)(intptr_t)Reg) << 4 | VT;
841 }
Chris Lattner845c0422010-02-18 22:03:03 +0000842};
Chris Lattner8e946be2010-02-21 03:22:59 +0000843
Chris Lattnerb21ba712010-02-25 02:04:40 +0000844/// EmitConvertToTargetMatcher - Emit an operation that reads a specified
Chris Lattner8e946be2010-02-21 03:22:59 +0000845/// recorded node and converts it from being a ISD::Constant to
846/// ISD::TargetConstant, likewise for ConstantFP.
Chris Lattnerb21ba712010-02-25 02:04:40 +0000847class EmitConvertToTargetMatcher : public Matcher {
Chris Lattner8e946be2010-02-21 03:22:59 +0000848 unsigned Slot;
849public:
Chris Lattnerb21ba712010-02-25 02:04:40 +0000850 EmitConvertToTargetMatcher(unsigned slot)
851 : Matcher(EmitConvertToTarget), Slot(slot) {}
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000852
Chris Lattner8e946be2010-02-21 03:22:59 +0000853 unsigned getSlot() const { return Slot; }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000854
Chris Lattnerb21ba712010-02-25 02:04:40 +0000855 static inline bool classof(const Matcher *N) {
Chris Lattner8e946be2010-02-21 03:22:59 +0000856 return N->getKind() == EmitConvertToTarget;
857 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000858
Chris Lattner58aa8342010-02-25 06:49:58 +0000859private:
Chris Lattnera5028a62010-02-25 06:53:39 +0000860 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner58aa8342010-02-25 06:49:58 +0000861 virtual bool isEqualImpl(const Matcher *M) const {
862 return cast<EmitConvertToTargetMatcher>(M)->Slot == Slot;
863 }
864 virtual unsigned getHashImpl() const { return Slot; }
Chris Lattner8e946be2010-02-21 03:22:59 +0000865};
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000866
Chris Lattnerb21ba712010-02-25 02:04:40 +0000867/// EmitMergeInputChainsMatcher - Emit a node that merges a list of input
Chris Lattner8e946be2010-02-21 03:22:59 +0000868/// chains together with a token factor. The list of nodes are the nodes in the
869/// matched pattern that have chain input/outputs. This node adds all input
870/// chains of these nodes if they are not themselves a node in the pattern.
Chris Lattnerb21ba712010-02-25 02:04:40 +0000871class EmitMergeInputChainsMatcher : public Matcher {
Chris Lattner8e946be2010-02-21 03:22:59 +0000872 SmallVector<unsigned, 3> ChainNodes;
873public:
Chris Lattnerb21ba712010-02-25 02:04:40 +0000874 EmitMergeInputChainsMatcher(const unsigned *nodes, unsigned NumNodes)
875 : Matcher(EmitMergeInputChains), ChainNodes(nodes, nodes+NumNodes) {}
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000876
Chris Lattner8e946be2010-02-21 03:22:59 +0000877 unsigned getNumNodes() const { return ChainNodes.size(); }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000878
Chris Lattner8e946be2010-02-21 03:22:59 +0000879 unsigned getNode(unsigned i) const {
880 assert(i < ChainNodes.size());
881 return ChainNodes[i];
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000882 }
883
Chris Lattnerb21ba712010-02-25 02:04:40 +0000884 static inline bool classof(const Matcher *N) {
Chris Lattner8e946be2010-02-21 03:22:59 +0000885 return N->getKind() == EmitMergeInputChains;
886 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000887
Chris Lattner58aa8342010-02-25 06:49:58 +0000888private:
Chris Lattnera5028a62010-02-25 06:53:39 +0000889 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner58aa8342010-02-25 06:49:58 +0000890 virtual bool isEqualImpl(const Matcher *M) const {
891 return cast<EmitMergeInputChainsMatcher>(M)->ChainNodes == ChainNodes;
892 }
893 virtual unsigned getHashImpl() const;
Chris Lattner8e946be2010-02-21 03:22:59 +0000894};
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000895
Chris Lattnerb21ba712010-02-25 02:04:40 +0000896/// EmitCopyToRegMatcher - Emit a CopyToReg node from a value to a physreg,
Chris Lattner8950bca2010-12-23 17:03:20 +0000897/// pushing the chain and glue results.
Chris Lattner8e946be2010-02-21 03:22:59 +0000898///
Chris Lattnerb21ba712010-02-25 02:04:40 +0000899class EmitCopyToRegMatcher : public Matcher {
Chris Lattner8e946be2010-02-21 03:22:59 +0000900 unsigned SrcSlot; // Value to copy into the physreg.
901 Record *DestPhysReg;
902public:
Chris Lattnerb21ba712010-02-25 02:04:40 +0000903 EmitCopyToRegMatcher(unsigned srcSlot, Record *destPhysReg)
904 : Matcher(EmitCopyToReg), SrcSlot(srcSlot), DestPhysReg(destPhysReg) {}
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000905
Chris Lattner8e946be2010-02-21 03:22:59 +0000906 unsigned getSrcSlot() const { return SrcSlot; }
907 Record *getDestPhysReg() const { return DestPhysReg; }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000908
Chris Lattnerb21ba712010-02-25 02:04:40 +0000909 static inline bool classof(const Matcher *N) {
Chris Lattner8e946be2010-02-21 03:22:59 +0000910 return N->getKind() == EmitCopyToReg;
911 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000912
Chris Lattner58aa8342010-02-25 06:49:58 +0000913private:
Chris Lattnera5028a62010-02-25 06:53:39 +0000914 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner58aa8342010-02-25 06:49:58 +0000915 virtual bool isEqualImpl(const Matcher *M) const {
916 return cast<EmitCopyToRegMatcher>(M)->SrcSlot == SrcSlot &&
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000917 cast<EmitCopyToRegMatcher>(M)->DestPhysReg == DestPhysReg;
Chris Lattner58aa8342010-02-25 06:49:58 +0000918 }
919 virtual unsigned getHashImpl() const {
920 return SrcSlot ^ ((unsigned)(intptr_t)DestPhysReg << 4);
921 }
Chris Lattner8e946be2010-02-21 03:22:59 +0000922};
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000923
924
925
Chris Lattnerb21ba712010-02-25 02:04:40 +0000926/// EmitNodeXFormMatcher - Emit an operation that runs an SDNodeXForm on a
Chris Lattner8e946be2010-02-21 03:22:59 +0000927/// recorded node and records the result.
Chris Lattnerb21ba712010-02-25 02:04:40 +0000928class EmitNodeXFormMatcher : public Matcher {
Chris Lattner8e946be2010-02-21 03:22:59 +0000929 unsigned Slot;
930 Record *NodeXForm;
931public:
Chris Lattnerb21ba712010-02-25 02:04:40 +0000932 EmitNodeXFormMatcher(unsigned slot, Record *nodeXForm)
933 : Matcher(EmitNodeXForm), Slot(slot), NodeXForm(nodeXForm) {}
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000934
Chris Lattner8e946be2010-02-21 03:22:59 +0000935 unsigned getSlot() const { return Slot; }
936 Record *getNodeXForm() const { return NodeXForm; }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000937
Chris Lattnerb21ba712010-02-25 02:04:40 +0000938 static inline bool classof(const Matcher *N) {
Chris Lattner8e946be2010-02-21 03:22:59 +0000939 return N->getKind() == EmitNodeXForm;
940 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000941
Chris Lattner58aa8342010-02-25 06:49:58 +0000942private:
Chris Lattnera5028a62010-02-25 06:53:39 +0000943 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner58aa8342010-02-25 06:49:58 +0000944 virtual bool isEqualImpl(const Matcher *M) const {
945 return cast<EmitNodeXFormMatcher>(M)->Slot == Slot &&
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000946 cast<EmitNodeXFormMatcher>(M)->NodeXForm == NodeXForm;
Chris Lattner58aa8342010-02-25 06:49:58 +0000947 }
948 virtual unsigned getHashImpl() const {
949 return Slot ^ ((unsigned)(intptr_t)NodeXForm << 4);
950 }
Chris Lattner8e946be2010-02-21 03:22:59 +0000951};
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000952
Chris Lattnere86097a2010-02-28 02:31:26 +0000953/// EmitNodeMatcherCommon - Common class shared between EmitNode and
Chris Lattner9a215002010-02-28 20:55:18 +0000954/// MorphNodeTo.
Chris Lattnere86097a2010-02-28 02:31:26 +0000955class EmitNodeMatcherCommon : public Matcher {
Chris Lattner8e946be2010-02-21 03:22:59 +0000956 std::string OpcodeName;
957 const SmallVector<MVT::SimpleValueType, 3> VTs;
958 const SmallVector<unsigned, 6> Operands;
Chris Lattner036609b2010-12-23 18:28:41 +0000959 bool HasChain, HasInGlue, HasOutGlue, HasMemRefs;
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000960
Chris Lattner8e946be2010-02-21 03:22:59 +0000961 /// NumFixedArityOperands - If this is a fixed arity node, this is set to -1.
962 /// If this is a varidic node, this is set to the number of fixed arity
963 /// operands in the root of the pattern. The rest are appended to this node.
964 int NumFixedArityOperands;
965public:
Chris Lattnere86097a2010-02-28 02:31:26 +0000966 EmitNodeMatcherCommon(const std::string &opcodeName,
967 const MVT::SimpleValueType *vts, unsigned numvts,
968 const unsigned *operands, unsigned numops,
Chris Lattner8950bca2010-12-23 17:03:20 +0000969 bool hasChain, bool hasInGlue, bool hasOutGlue,
Chris Lattnerff7fb602010-02-28 21:53:42 +0000970 bool hasmemrefs,
Chris Lattner9a215002010-02-28 20:55:18 +0000971 int numfixedarityoperands, bool isMorphNodeTo)
972 : Matcher(isMorphNodeTo ? MorphNodeTo : EmitNode), OpcodeName(opcodeName),
Chris Lattner8e946be2010-02-21 03:22:59 +0000973 VTs(vts, vts+numvts), Operands(operands, operands+numops),
Chris Lattner036609b2010-12-23 18:28:41 +0000974 HasChain(hasChain), HasInGlue(hasInGlue), HasOutGlue(hasOutGlue),
Chris Lattnerff7fb602010-02-28 21:53:42 +0000975 HasMemRefs(hasmemrefs), NumFixedArityOperands(numfixedarityoperands) {}
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000976
Chris Lattner8e946be2010-02-21 03:22:59 +0000977 const std::string &getOpcodeName() const { return OpcodeName; }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000978
Chris Lattner8e946be2010-02-21 03:22:59 +0000979 unsigned getNumVTs() const { return VTs.size(); }
980 MVT::SimpleValueType getVT(unsigned i) const {
981 assert(i < VTs.size());
982 return VTs[i];
983 }
Chris Lattnerc78f2a32010-02-28 20:49:53 +0000984
Chris Lattner8e946be2010-02-21 03:22:59 +0000985 unsigned getNumOperands() const { return Operands.size(); }
986 unsigned getOperand(unsigned i) const {
987 assert(i < Operands.size());
988 return Operands[i];
Chris Lattnerc78f2a32010-02-28 20:49:53 +0000989 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000990
Chris Lattnerc78f2a32010-02-28 20:49:53 +0000991 const SmallVectorImpl<MVT::SimpleValueType> &getVTList() const { return VTs; }
992 const SmallVectorImpl<unsigned> &getOperandList() const { return Operands; }
993
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000994
Chris Lattner8e946be2010-02-21 03:22:59 +0000995 bool hasChain() const { return HasChain; }
Chris Lattner036609b2010-12-23 18:28:41 +0000996 bool hasInFlag() const { return HasInGlue; }
997 bool hasOutFlag() const { return HasOutGlue; }
Chris Lattner8e946be2010-02-21 03:22:59 +0000998 bool hasMemRefs() const { return HasMemRefs; }
999 int getNumFixedArityOperands() const { return NumFixedArityOperands; }
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001000
Chris Lattnerb21ba712010-02-25 02:04:40 +00001001 static inline bool classof(const Matcher *N) {
Chris Lattner9a215002010-02-28 20:55:18 +00001002 return N->getKind() == EmitNode || N->getKind() == MorphNodeTo;
Chris Lattner845c0422010-02-18 22:03:03 +00001003 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001004
Chris Lattner58aa8342010-02-25 06:49:58 +00001005private:
Chris Lattnera5028a62010-02-25 06:53:39 +00001006 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner58aa8342010-02-25 06:49:58 +00001007 virtual bool isEqualImpl(const Matcher *M) const;
1008 virtual unsigned getHashImpl() const;
Chris Lattner845c0422010-02-18 22:03:03 +00001009};
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001010
Chris Lattnere86097a2010-02-28 02:31:26 +00001011/// EmitNodeMatcher - This signals a successful match and generates a node.
1012class EmitNodeMatcher : public EmitNodeMatcherCommon {
David Blaikie2d24e2a2011-12-20 02:50:00 +00001013 virtual void anchor();
Chris Lattner6281cda2010-02-28 02:41:25 +00001014 unsigned FirstResultSlot;
Chris Lattnere86097a2010-02-28 02:31:26 +00001015public:
1016 EmitNodeMatcher(const std::string &opcodeName,
1017 const MVT::SimpleValueType *vts, unsigned numvts,
1018 const unsigned *operands, unsigned numops,
Chris Lattnerff7fb602010-02-28 21:53:42 +00001019 bool hasChain, bool hasInFlag, bool hasOutFlag,
1020 bool hasmemrefs,
Chris Lattner6281cda2010-02-28 02:41:25 +00001021 int numfixedarityoperands, unsigned firstresultslot)
Chris Lattnere86097a2010-02-28 02:31:26 +00001022 : EmitNodeMatcherCommon(opcodeName, vts, numvts, operands, numops, hasChain,
Chris Lattnerff7fb602010-02-28 21:53:42 +00001023 hasInFlag, hasOutFlag, hasmemrefs,
1024 numfixedarityoperands, false),
Chris Lattner6281cda2010-02-28 02:41:25 +00001025 FirstResultSlot(firstresultslot) {}
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001026
Chris Lattner6281cda2010-02-28 02:41:25 +00001027 unsigned getFirstResultSlot() const { return FirstResultSlot; }
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001028
Chris Lattnere86097a2010-02-28 02:31:26 +00001029 static inline bool classof(const Matcher *N) {
1030 return N->getKind() == EmitNode;
1031 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001032
Chris Lattnere86097a2010-02-28 02:31:26 +00001033};
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001034
Chris Lattner9a215002010-02-28 20:55:18 +00001035class MorphNodeToMatcher : public EmitNodeMatcherCommon {
David Blaikie2d24e2a2011-12-20 02:50:00 +00001036 virtual void anchor();
Chris Lattnere86097a2010-02-28 02:31:26 +00001037 const PatternToMatch &Pattern;
1038public:
Chris Lattner9a215002010-02-28 20:55:18 +00001039 MorphNodeToMatcher(const std::string &opcodeName,
1040 const MVT::SimpleValueType *vts, unsigned numvts,
1041 const unsigned *operands, unsigned numops,
Chris Lattnerff7fb602010-02-28 21:53:42 +00001042 bool hasChain, bool hasInFlag, bool hasOutFlag,
1043 bool hasmemrefs,
Chris Lattner9a215002010-02-28 20:55:18 +00001044 int numfixedarityoperands, const PatternToMatch &pattern)
Chris Lattnere86097a2010-02-28 02:31:26 +00001045 : EmitNodeMatcherCommon(opcodeName, vts, numvts, operands, numops, hasChain,
Chris Lattnerff7fb602010-02-28 21:53:42 +00001046 hasInFlag, hasOutFlag, hasmemrefs,
1047 numfixedarityoperands, true),
Chris Lattnere86097a2010-02-28 02:31:26 +00001048 Pattern(pattern) {
1049 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001050
Chris Lattnere86097a2010-02-28 02:31:26 +00001051 const PatternToMatch &getPattern() const { return Pattern; }
1052
1053 static inline bool classof(const Matcher *N) {
Chris Lattner9a215002010-02-28 20:55:18 +00001054 return N->getKind() == MorphNodeTo;
Chris Lattnere86097a2010-02-28 02:31:26 +00001055 }
1056};
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001057
Chris Lattner8950bca2010-12-23 17:03:20 +00001058/// MarkGlueResultsMatcher - This node indicates which non-root nodes in the
1059/// pattern produce glue. This allows CompleteMatchMatcher to update them
1060/// with the output glue of the resultant code.
1061class MarkGlueResultsMatcher : public Matcher {
1062 SmallVector<unsigned, 3> GlueResultNodes;
Chris Lattner02f73582010-02-24 05:33:42 +00001063public:
Chris Lattner8950bca2010-12-23 17:03:20 +00001064 MarkGlueResultsMatcher(const unsigned *nodes, unsigned NumNodes)
1065 : Matcher(MarkGlueResults), GlueResultNodes(nodes, nodes+NumNodes) {}
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001066
Chris Lattner8950bca2010-12-23 17:03:20 +00001067 unsigned getNumNodes() const { return GlueResultNodes.size(); }
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001068
Chris Lattner02f73582010-02-24 05:33:42 +00001069 unsigned getNode(unsigned i) const {
Chris Lattner8950bca2010-12-23 17:03:20 +00001070 assert(i < GlueResultNodes.size());
1071 return GlueResultNodes[i];
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001072 }
1073
Chris Lattnerb21ba712010-02-25 02:04:40 +00001074 static inline bool classof(const Matcher *N) {
Chris Lattner8950bca2010-12-23 17:03:20 +00001075 return N->getKind() == MarkGlueResults;
Chris Lattner02f73582010-02-24 05:33:42 +00001076 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001077
Chris Lattner58aa8342010-02-25 06:49:58 +00001078private:
Chris Lattnera5028a62010-02-25 06:53:39 +00001079 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner58aa8342010-02-25 06:49:58 +00001080 virtual bool isEqualImpl(const Matcher *M) const {
Chris Lattner8950bca2010-12-23 17:03:20 +00001081 return cast<MarkGlueResultsMatcher>(M)->GlueResultNodes == GlueResultNodes;
Chris Lattner58aa8342010-02-25 06:49:58 +00001082 }
1083 virtual unsigned getHashImpl() const;
Chris Lattner02f73582010-02-24 05:33:42 +00001084};
1085
Chris Lattnerb21ba712010-02-25 02:04:40 +00001086/// CompleteMatchMatcher - Complete a match by replacing the results of the
Chris Lattner77f2e272010-02-21 06:03:07 +00001087/// pattern with the newly generated nodes. This also prints a comment
1088/// indicating the source and dest patterns.
Chris Lattnerb21ba712010-02-25 02:04:40 +00001089class CompleteMatchMatcher : public Matcher {
Chris Lattner77f2e272010-02-21 06:03:07 +00001090 SmallVector<unsigned, 2> Results;
Chris Lattner8e946be2010-02-21 03:22:59 +00001091 const PatternToMatch &Pattern;
1092public:
Chris Lattnerb21ba712010-02-25 02:04:40 +00001093 CompleteMatchMatcher(const unsigned *results, unsigned numresults,
Chris Lattnerc78f2a32010-02-28 20:49:53 +00001094 const PatternToMatch &pattern)
Chris Lattnerb21ba712010-02-25 02:04:40 +00001095 : Matcher(CompleteMatch), Results(results, results+numresults),
Chris Lattner77f2e272010-02-21 06:03:07 +00001096 Pattern(pattern) {}
1097
1098 unsigned getNumResults() const { return Results.size(); }
1099 unsigned getResult(unsigned R) const { return Results[R]; }
Chris Lattner8e946be2010-02-21 03:22:59 +00001100 const PatternToMatch &getPattern() const { return Pattern; }
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001101
Chris Lattnerb21ba712010-02-25 02:04:40 +00001102 static inline bool classof(const Matcher *N) {
Chris Lattner77f2e272010-02-21 06:03:07 +00001103 return N->getKind() == CompleteMatch;
Chris Lattner8e946be2010-02-21 03:22:59 +00001104 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001105
Chris Lattner58aa8342010-02-25 06:49:58 +00001106private:
Chris Lattnera5028a62010-02-25 06:53:39 +00001107 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner58aa8342010-02-25 06:49:58 +00001108 virtual bool isEqualImpl(const Matcher *M) const {
1109 return cast<CompleteMatchMatcher>(M)->Results == Results &&
1110 &cast<CompleteMatchMatcher>(M)->Pattern == &Pattern;
1111 }
1112 virtual unsigned getHashImpl() const;
Chris Lattner8e946be2010-02-21 03:22:59 +00001113};
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001114
Chris Lattnerda272d12010-02-15 08:04:42 +00001115} // end namespace llvm
1116
1117#endif