blob: d9b25d5564301f457af14b068ce9ea4e96daece3 [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
Chris Lattner050a03d2010-02-16 07:21:10 +000013#include "llvm/CodeGen/ValueTypes.h"
Chris Lattnerda272d12010-02-15 08:04:42 +000014#include "llvm/ADT/OwningPtr.h"
15#include "llvm/ADT/StringRef.h"
Chris Lattner8e946be2010-02-21 03:22:59 +000016#include "llvm/ADT/SmallVector.h"
Chris Lattner050a03d2010-02-16 07:21:10 +000017#include "llvm/Support/Casting.h"
Chris Lattnerda272d12010-02-15 08:04:42 +000018
19namespace llvm {
20 class CodeGenDAGPatterns;
Chris Lattnerb21ba712010-02-25 02:04:40 +000021 class Matcher;
Chris Lattnerda272d12010-02-15 08:04:42 +000022 class PatternToMatch;
23 class raw_ostream;
24 class ComplexPattern;
Chris Lattner845c0422010-02-18 22:03:03 +000025 class Record;
Chris Lattnera230f962010-02-27 21:48:43 +000026 class SDNodeInfo;
Chris Lattnerda272d12010-02-15 08:04:42 +000027
Chris Lattnerfa342fa2010-03-01 07:17:40 +000028Matcher *ConvertPatternToMatcher(const PatternToMatch &Pattern,unsigned Variant,
Chris Lattnerb21ba712010-02-25 02:04:40 +000029 const CodeGenDAGPatterns &CGP);
Chris Lattnerc78f2a32010-02-28 20:49:53 +000030Matcher *OptimizeMatcher(Matcher *Matcher, const CodeGenDAGPatterns &CGP);
Chris Lattner4d0c9312010-03-01 01:54:19 +000031void EmitMatcherTable(const Matcher *Matcher, const CodeGenDAGPatterns &CGP,
32 raw_ostream &OS);
Chris Lattnerda272d12010-02-15 08:04:42 +000033
34
Chris Lattnerb21ba712010-02-25 02:04:40 +000035/// Matcher - Base class for all the the DAG ISel Matcher representation
Chris Lattnerda272d12010-02-15 08:04:42 +000036/// nodes.
Chris Lattnerb21ba712010-02-25 02:04:40 +000037class Matcher {
Chris Lattnerbd8227f2010-02-18 02:53:41 +000038 // The next matcher node that is executed after this one. Null if this is the
39 // last stage of a match.
Chris Lattnerb21ba712010-02-25 02:04:40 +000040 OwningPtr<Matcher> Next;
Chris Lattnerda272d12010-02-15 08:04:42 +000041public:
42 enum KindTy {
Chris Lattner8e946be2010-02-21 03:22:59 +000043 // Matcher state manipulation.
Chris Lattner60df53e2010-02-25 01:56:48 +000044 Scope, // Push a checking scope.
Chris Lattner8e946be2010-02-21 03:22:59 +000045 RecordNode, // Record the current node.
Chris Lattner19b5a752010-02-24 07:31:45 +000046 RecordChild, // Record a child of the current node.
Chris Lattner8e946be2010-02-21 03:22:59 +000047 RecordMemRef, // Record the memref in the current node.
48 CaptureFlagInput, // If the current node has an input flag, save it.
49 MoveChild, // Move current node to specified child.
50 MoveParent, // Move current node to parent.
Chris Lattnerda272d12010-02-15 08:04:42 +000051
Chris Lattner845c0422010-02-18 22:03:03 +000052 // Predicate checking.
Chris Lattner8e946be2010-02-21 03:22:59 +000053 CheckSame, // Fail if not same as prev match.
Chris Lattnerda272d12010-02-15 08:04:42 +000054 CheckPatternPredicate,
Chris Lattner8e946be2010-02-21 03:22:59 +000055 CheckPredicate, // Fail if node predicate fails.
56 CheckOpcode, // Fail if not opcode.
Chris Lattnereb669212010-03-01 06:59:22 +000057 SwitchOpcode, // Dispatch based on opcode.
Chris Lattner8e946be2010-02-21 03:22:59 +000058 CheckType, // Fail if not correct type.
Chris Lattnercfe2eab2010-03-03 06:28:15 +000059 SwitchType, // Dispatch based on type.
Chris Lattner23cfda72010-02-24 20:15:25 +000060 CheckChildType, // Fail if child has wrong type.
Chris Lattner8e946be2010-02-21 03:22:59 +000061 CheckInteger, // Fail if wrong val.
62 CheckCondCode, // Fail if not condcode.
Chris Lattnerda272d12010-02-15 08:04:42 +000063 CheckValueType,
64 CheckComplexPat,
65 CheckAndImm,
Chris Lattnere39650a2010-02-16 06:10:58 +000066 CheckOrImm,
Chris Lattner9a747f12010-02-17 06:23:39 +000067 CheckFoldableChainNode,
Chris Lattner845c0422010-02-18 22:03:03 +000068
69 // Node creation/emisssion.
Chris Lattner8e946be2010-02-21 03:22:59 +000070 EmitInteger, // Create a TargetConstant
71 EmitStringInteger, // Create a TargetConstant from a string.
72 EmitRegister, // Create a register.
73 EmitConvertToTarget, // Convert a imm/fpimm to target imm/fpimm
74 EmitMergeInputChains, // Merge together a chains for an input.
75 EmitCopyToReg, // Emit a copytoreg into a physreg.
76 EmitNode, // Create a DAG node
77 EmitNodeXForm, // Run a SDNodeXForm
Chris Lattner02f73582010-02-24 05:33:42 +000078 MarkFlagResults, // Indicate which interior nodes have flag results.
Chris Lattnere86097a2010-02-28 02:31:26 +000079 CompleteMatch, // Finish a match and update the results.
Chris Lattner9a215002010-02-28 20:55:18 +000080 MorphNodeTo // Build a node, finish a match and update results.
Chris Lattnerda272d12010-02-15 08:04:42 +000081 };
82 const KindTy Kind;
Chris Lattner8ef9c792010-02-18 02:49:24 +000083
Chris Lattnerda272d12010-02-15 08:04:42 +000084protected:
Chris Lattnerb21ba712010-02-25 02:04:40 +000085 Matcher(KindTy K) : Kind(K) {}
Chris Lattnerda272d12010-02-15 08:04:42 +000086public:
Chris Lattnerb21ba712010-02-25 02:04:40 +000087 virtual ~Matcher() {}
Chris Lattnerda272d12010-02-15 08:04:42 +000088
89 KindTy getKind() const { return Kind; }
Chris Lattner8ef9c792010-02-18 02:49:24 +000090
Chris Lattnerb21ba712010-02-25 02:04:40 +000091 Matcher *getNext() { return Next.get(); }
92 const Matcher *getNext() const { return Next.get(); }
93 void setNext(Matcher *C) { Next.reset(C); }
94 Matcher *takeNext() { return Next.take(); }
Chris Lattner19b5a752010-02-24 07:31:45 +000095
Chris Lattnerb21ba712010-02-25 02:04:40 +000096 OwningPtr<Matcher> &getNextPtr() { return Next; }
Chris Lattnerda272d12010-02-15 08:04:42 +000097
Chris Lattnerb21ba712010-02-25 02:04:40 +000098 static inline bool classof(const Matcher *) { return true; }
Chris Lattnerda272d12010-02-15 08:04:42 +000099
Chris Lattner58aa8342010-02-25 06:49:58 +0000100 bool isEqual(const Matcher *M) const {
101 if (getKind() != M->getKind()) return false;
102 return isEqualImpl(M);
103 }
104
105 unsigned getHash() const {
Chris Lattnerca56fea2010-02-26 07:35:27 +0000106 // Clear the high bit so we don't conflict with tombstones etc.
107 return ((getHashImpl() << 4) ^ getKind()) & (~0U>>1);
Chris Lattner58aa8342010-02-25 06:49:58 +0000108 }
109
Chris Lattnerd323fd42010-02-27 06:22:57 +0000110 /// isSafeToReorderWithPatternPredicate - Return true if it is safe to sink a
111 /// PatternPredicate node past this one.
112 virtual bool isSafeToReorderWithPatternPredicate() const {
113 return false;
114 }
115
Chris Lattner48aa5752010-03-07 06:29:26 +0000116 /// isSimplePredicateNode - Return true if this is a simple predicate that
117 /// operates on the node or its children without potential side effects or a
118 /// change of the current node.
119 bool isSimplePredicateNode() const {
120 switch (getKind()) {
121 default: return false;
122 case CheckSame:
123 case CheckPatternPredicate:
124 case CheckPredicate:
125 case CheckOpcode:
126 case CheckType:
127 case CheckChildType:
128 case CheckInteger:
129 case CheckCondCode:
130 case CheckValueType:
131 case CheckAndImm:
132 case CheckOrImm:
133 case CheckFoldableChainNode:
134 return true;
135 }
136 }
137
138 /// isSimplePredicateOrRecordNode - Return true if this is a record node or
139 /// a simple predicate.
140 bool isSimplePredicateOrRecordNode() const {
141 return isSimplePredicateNode() ||
142 getKind() == RecordNode || getKind() == RecordChild;
143 }
144
145 /// unlinkNode - Unlink the specified node from this chain. If Other == this,
146 /// we unlink the next pointer and return it. Otherwise we unlink Other from
147 /// the list and return this.
148 Matcher *unlinkNode(Matcher *Other);
149
150 /// canMoveBefore - Return true if this matcher is the same as Other, or if
151 /// we can move this matcher past all of the nodes in-between Other and this
152 /// node. Other must be equal to or before this.
153 bool canMoveBefore(const Matcher *Other) const;
154
155 /// canMoveBefore - Return true if it is safe to move the current matcher
156 /// across the specified one.
157 bool canMoveBeforeNode(const Matcher *Other) const;
158
Chris Lattner82781b92010-02-27 07:49:13 +0000159 /// isContradictory - Return true of these two matchers could never match on
160 /// the same node.
161 bool isContradictory(const Matcher *Other) const {
162 // Since this predicate is reflexive, we canonicalize the ordering so that
163 // we always match a node against nodes with kinds that are greater or equal
164 // to them. For example, we'll pass in a CheckType node as an argument to
165 // the CheckOpcode method, not the other way around.
166 if (getKind() < Other->getKind())
167 return isContradictoryImpl(Other);
168 return Other->isContradictoryImpl(this);
169 }
170
Chris Lattnera5028a62010-02-25 06:53:39 +0000171 void print(raw_ostream &OS, unsigned indent = 0) const;
Chris Lattner82781b92010-02-27 07:49:13 +0000172 void printOne(raw_ostream &OS) const;
Chris Lattnerda272d12010-02-15 08:04:42 +0000173 void dump() const;
Chris Lattner8ef9c792010-02-18 02:49:24 +0000174protected:
Chris Lattnera5028a62010-02-25 06:53:39 +0000175 virtual void printImpl(raw_ostream &OS, unsigned indent) const = 0;
Chris Lattner58aa8342010-02-25 06:49:58 +0000176 virtual bool isEqualImpl(const Matcher *M) const = 0;
177 virtual unsigned getHashImpl() const = 0;
Chris Lattner82781b92010-02-27 07:49:13 +0000178 virtual bool isContradictoryImpl(const Matcher *M) const { return false; }
Chris Lattnerda272d12010-02-15 08:04:42 +0000179};
180
Chris Lattnerd6c84722010-02-25 19:00:39 +0000181/// ScopeMatcher - This attempts to match each of its children to find the first
182/// one that successfully matches. If one child fails, it tries the next child.
183/// If none of the children match then this check fails. It never has a 'next'.
Chris Lattnerb21ba712010-02-25 02:04:40 +0000184class ScopeMatcher : public Matcher {
Chris Lattnerd6c84722010-02-25 19:00:39 +0000185 SmallVector<Matcher*, 4> Children;
Chris Lattnerda272d12010-02-15 08:04:42 +0000186public:
Chris Lattnerd6c84722010-02-25 19:00:39 +0000187 ScopeMatcher(Matcher *const *children, unsigned numchildren)
188 : Matcher(Scope), Children(children, children+numchildren) {
Chris Lattnerda272d12010-02-15 08:04:42 +0000189 }
Chris Lattnerd6c84722010-02-25 19:00:39 +0000190 virtual ~ScopeMatcher();
Chris Lattnerda272d12010-02-15 08:04:42 +0000191
Chris Lattnerd6c84722010-02-25 19:00:39 +0000192 unsigned getNumChildren() const { return Children.size(); }
193
194 Matcher *getChild(unsigned i) { return Children[i]; }
195 const Matcher *getChild(unsigned i) const { return Children[i]; }
196
197 void resetChild(unsigned i, Matcher *N) {
198 delete Children[i];
199 Children[i] = N;
200 }
201
202 Matcher *takeChild(unsigned i) {
203 Matcher *Res = Children[i];
204 Children[i] = 0;
205 return Res;
206 }
Chris Lattnerca56fea2010-02-26 07:35:27 +0000207
208 void setNumChildren(unsigned NC) {
209 if (NC < Children.size()) {
210 // delete any children we're about to lose pointers to.
211 for (unsigned i = NC, e = Children.size(); i != e; ++i)
212 delete Children[i];
213 }
214 Children.resize(NC);
215 }
Chris Lattnerda272d12010-02-15 08:04:42 +0000216
Chris Lattnerb21ba712010-02-25 02:04:40 +0000217 static inline bool classof(const Matcher *N) {
Chris Lattner60df53e2010-02-25 01:56:48 +0000218 return N->getKind() == Scope;
Chris Lattnerda272d12010-02-15 08:04:42 +0000219 }
220
Chris Lattner58aa8342010-02-25 06:49:58 +0000221private:
Chris Lattnera5028a62010-02-25 06:53:39 +0000222 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner58aa8342010-02-25 06:49:58 +0000223 virtual bool isEqualImpl(const Matcher *M) const { return false; }
Chris Lattnerd6c84722010-02-25 19:00:39 +0000224 virtual unsigned getHashImpl() const { return 12312; }
Chris Lattnerda272d12010-02-15 08:04:42 +0000225};
226
Chris Lattnerb21ba712010-02-25 02:04:40 +0000227/// RecordMatcher - Save the current node in the operand list.
228class RecordMatcher : public Matcher {
Chris Lattnerc96087b2010-02-17 01:03:09 +0000229 /// WhatFor - This is a string indicating why we're recording this. This
230 /// should only be used for comment generation not anything semantic.
231 std::string WhatFor;
Chris Lattner0cebe612010-03-01 02:24:17 +0000232
233 /// ResultNo - The slot number in the RecordedNodes vector that this will be,
234 /// just printed as a comment.
235 unsigned ResultNo;
Chris Lattnerda272d12010-02-15 08:04:42 +0000236public:
Chris Lattner0cebe612010-03-01 02:24:17 +0000237 RecordMatcher(const std::string &whatfor, unsigned resultNo)
238 : Matcher(RecordNode), WhatFor(whatfor), ResultNo(resultNo) {}
Chris Lattnerc96087b2010-02-17 01:03:09 +0000239
Chris Lattnerc642b842010-02-17 01:27:29 +0000240 const std::string &getWhatFor() const { return WhatFor; }
Chris Lattner0cebe612010-03-01 02:24:17 +0000241 unsigned getResultNo() const { return ResultNo; }
Chris Lattnerda272d12010-02-15 08:04:42 +0000242
Chris Lattnerb21ba712010-02-25 02:04:40 +0000243 static inline bool classof(const Matcher *N) {
Chris Lattner845c0422010-02-18 22:03:03 +0000244 return N->getKind() == RecordNode;
Chris Lattnerda272d12010-02-15 08:04:42 +0000245 }
246
Chris Lattnerd323fd42010-02-27 06:22:57 +0000247 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
Chris Lattner58aa8342010-02-25 06:49:58 +0000248private:
Chris Lattnera5028a62010-02-25 06:53:39 +0000249 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner58aa8342010-02-25 06:49:58 +0000250 virtual bool isEqualImpl(const Matcher *M) const { return true; }
251 virtual unsigned getHashImpl() const { return 0; }
Chris Lattnerda272d12010-02-15 08:04:42 +0000252};
253
Chris Lattnerb21ba712010-02-25 02:04:40 +0000254/// RecordChildMatcher - Save a numbered child of the current node, or fail
Chris Lattner19b5a752010-02-24 07:31:45 +0000255/// the match if it doesn't exist. This is logically equivalent to:
256/// MoveChild N + RecordNode + MoveParent.
Chris Lattnerb21ba712010-02-25 02:04:40 +0000257class RecordChildMatcher : public Matcher {
Chris Lattner19b5a752010-02-24 07:31:45 +0000258 unsigned ChildNo;
259
260 /// WhatFor - This is a string indicating why we're recording this. This
261 /// should only be used for comment generation not anything semantic.
262 std::string WhatFor;
Chris Lattner0cebe612010-03-01 02:24:17 +0000263
264 /// ResultNo - The slot number in the RecordedNodes vector that this will be,
265 /// just printed as a comment.
266 unsigned ResultNo;
Chris Lattner19b5a752010-02-24 07:31:45 +0000267public:
Chris Lattner0cebe612010-03-01 02:24:17 +0000268 RecordChildMatcher(unsigned childno, const std::string &whatfor,
269 unsigned resultNo)
270 : Matcher(RecordChild), ChildNo(childno), WhatFor(whatfor),
271 ResultNo(resultNo) {}
Chris Lattner19b5a752010-02-24 07:31:45 +0000272
273 unsigned getChildNo() const { return ChildNo; }
274 const std::string &getWhatFor() const { return WhatFor; }
Chris Lattner0cebe612010-03-01 02:24:17 +0000275 unsigned getResultNo() const { return ResultNo; }
276
Chris Lattnerb21ba712010-02-25 02:04:40 +0000277 static inline bool classof(const Matcher *N) {
Chris Lattner19b5a752010-02-24 07:31:45 +0000278 return N->getKind() == RecordChild;
279 }
280
Chris Lattnerd323fd42010-02-27 06:22:57 +0000281 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
282
Chris Lattner58aa8342010-02-25 06:49:58 +0000283private:
Chris Lattnera5028a62010-02-25 06:53:39 +0000284 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner58aa8342010-02-25 06:49:58 +0000285 virtual bool isEqualImpl(const Matcher *M) const {
286 return cast<RecordChildMatcher>(M)->getChildNo() == getChildNo();
287 }
288 virtual unsigned getHashImpl() const { return getChildNo(); }
Chris Lattner19b5a752010-02-24 07:31:45 +0000289};
290
Chris Lattnerb21ba712010-02-25 02:04:40 +0000291/// RecordMemRefMatcher - Save the current node's memref.
292class RecordMemRefMatcher : public Matcher {
Chris Lattner8e946be2010-02-21 03:22:59 +0000293public:
Chris Lattnerb21ba712010-02-25 02:04:40 +0000294 RecordMemRefMatcher() : Matcher(RecordMemRef) {}
Chris Lattner8e946be2010-02-21 03:22:59 +0000295
Chris Lattnerb21ba712010-02-25 02:04:40 +0000296 static inline bool classof(const Matcher *N) {
Chris Lattner8e946be2010-02-21 03:22:59 +0000297 return N->getKind() == RecordMemRef;
298 }
299
Chris Lattnerd323fd42010-02-27 06:22:57 +0000300 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
301
Chris Lattner58aa8342010-02-25 06:49:58 +0000302private:
Chris Lattnera5028a62010-02-25 06:53:39 +0000303 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner58aa8342010-02-25 06:49:58 +0000304 virtual bool isEqualImpl(const Matcher *M) const { return true; }
305 virtual unsigned getHashImpl() const { return 0; }
Chris Lattner8e946be2010-02-21 03:22:59 +0000306};
307
308
Chris Lattnerb21ba712010-02-25 02:04:40 +0000309/// CaptureFlagInputMatcher - If the current record has a flag input, record
Chris Lattner8e946be2010-02-21 03:22:59 +0000310/// it so that it is used as an input to the generated code.
Chris Lattnerb21ba712010-02-25 02:04:40 +0000311class CaptureFlagInputMatcher : public Matcher {
Chris Lattner8e946be2010-02-21 03:22:59 +0000312public:
Chris Lattnerb21ba712010-02-25 02:04:40 +0000313 CaptureFlagInputMatcher() : Matcher(CaptureFlagInput) {}
Chris Lattner8e946be2010-02-21 03:22:59 +0000314
Chris Lattnerb21ba712010-02-25 02:04:40 +0000315 static inline bool classof(const Matcher *N) {
Chris Lattner8e946be2010-02-21 03:22:59 +0000316 return N->getKind() == CaptureFlagInput;
317 }
318
Chris Lattnerd323fd42010-02-27 06:22:57 +0000319 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
320
Chris Lattner58aa8342010-02-25 06:49:58 +0000321private:
Chris Lattnera5028a62010-02-25 06:53:39 +0000322 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner58aa8342010-02-25 06:49:58 +0000323 virtual bool isEqualImpl(const Matcher *M) const { return true; }
324 virtual unsigned getHashImpl() const { return 0; }
Chris Lattner8e946be2010-02-21 03:22:59 +0000325};
326
Chris Lattnerb21ba712010-02-25 02:04:40 +0000327/// MoveChildMatcher - This tells the interpreter to move into the
Chris Lattnerda272d12010-02-15 08:04:42 +0000328/// specified child node.
Chris Lattnerb21ba712010-02-25 02:04:40 +0000329class MoveChildMatcher : public Matcher {
Chris Lattnerda272d12010-02-15 08:04:42 +0000330 unsigned ChildNo;
331public:
Chris Lattnerb21ba712010-02-25 02:04:40 +0000332 MoveChildMatcher(unsigned childNo) : Matcher(MoveChild), ChildNo(childNo) {}
Chris Lattnerda272d12010-02-15 08:04:42 +0000333
334 unsigned getChildNo() const { return ChildNo; }
335
Chris Lattnerb21ba712010-02-25 02:04:40 +0000336 static inline bool classof(const Matcher *N) {
Chris Lattnerda272d12010-02-15 08:04:42 +0000337 return N->getKind() == MoveChild;
338 }
339
Chris Lattnerd323fd42010-02-27 06:22:57 +0000340 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
341
Chris Lattner58aa8342010-02-25 06:49:58 +0000342private:
Chris Lattnera5028a62010-02-25 06:53:39 +0000343 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner58aa8342010-02-25 06:49:58 +0000344 virtual bool isEqualImpl(const Matcher *M) const {
345 return cast<MoveChildMatcher>(M)->getChildNo() == getChildNo();
346 }
347 virtual unsigned getHashImpl() const { return getChildNo(); }
Chris Lattnerda272d12010-02-15 08:04:42 +0000348};
349
Chris Lattnerb21ba712010-02-25 02:04:40 +0000350/// MoveParentMatcher - This tells the interpreter to move to the parent
Chris Lattnerda272d12010-02-15 08:04:42 +0000351/// of the current node.
Chris Lattnerb21ba712010-02-25 02:04:40 +0000352class MoveParentMatcher : public Matcher {
Chris Lattnerda272d12010-02-15 08:04:42 +0000353public:
Chris Lattnerb21ba712010-02-25 02:04:40 +0000354 MoveParentMatcher() : Matcher(MoveParent) {}
Chris Lattnerda272d12010-02-15 08:04:42 +0000355
Chris Lattnerb21ba712010-02-25 02:04:40 +0000356 static inline bool classof(const Matcher *N) {
Chris Lattnerda272d12010-02-15 08:04:42 +0000357 return N->getKind() == MoveParent;
358 }
359
Chris Lattnerd323fd42010-02-27 06:22:57 +0000360 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
361
Chris Lattner58aa8342010-02-25 06:49:58 +0000362private:
Chris Lattnera5028a62010-02-25 06:53:39 +0000363 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner58aa8342010-02-25 06:49:58 +0000364 virtual bool isEqualImpl(const Matcher *M) const { return true; }
365 virtual unsigned getHashImpl() const { return 0; }
Chris Lattnerda272d12010-02-15 08:04:42 +0000366};
367
Chris Lattnerb21ba712010-02-25 02:04:40 +0000368/// CheckSameMatcher - This checks to see if this node is exactly the same
Chris Lattnerda272d12010-02-15 08:04:42 +0000369/// node as the specified match that was recorded with 'Record'. This is used
370/// when patterns have the same name in them, like '(mul GPR:$in, GPR:$in)'.
Chris Lattnerb21ba712010-02-25 02:04:40 +0000371class CheckSameMatcher : public Matcher {
Chris Lattnerda272d12010-02-15 08:04:42 +0000372 unsigned MatchNumber;
373public:
Chris Lattnerb21ba712010-02-25 02:04:40 +0000374 CheckSameMatcher(unsigned matchnumber)
Chris Lattner58aa8342010-02-25 06:49:58 +0000375 : Matcher(CheckSame), MatchNumber(matchnumber) {}
Chris Lattnerda272d12010-02-15 08:04:42 +0000376
377 unsigned getMatchNumber() const { return MatchNumber; }
378
Chris Lattnerb21ba712010-02-25 02:04:40 +0000379 static inline bool classof(const Matcher *N) {
Chris Lattnerda272d12010-02-15 08:04:42 +0000380 return N->getKind() == CheckSame;
381 }
382
Chris Lattnerd323fd42010-02-27 06:22:57 +0000383 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
384
Chris Lattner58aa8342010-02-25 06:49:58 +0000385private:
Chris Lattnera5028a62010-02-25 06:53:39 +0000386 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner58aa8342010-02-25 06:49:58 +0000387 virtual bool isEqualImpl(const Matcher *M) const {
388 return cast<CheckSameMatcher>(M)->getMatchNumber() == getMatchNumber();
389 }
390 virtual unsigned getHashImpl() const { return getMatchNumber(); }
Chris Lattnerda272d12010-02-15 08:04:42 +0000391};
392
Chris Lattnerb21ba712010-02-25 02:04:40 +0000393/// CheckPatternPredicateMatcher - This checks the target-specific predicate
Chris Lattnerda272d12010-02-15 08:04:42 +0000394/// to see if the entire pattern is capable of matching. This predicate does
395/// not take a node as input. This is used for subtarget feature checks etc.
Chris Lattnerb21ba712010-02-25 02:04:40 +0000396class CheckPatternPredicateMatcher : public Matcher {
Chris Lattnerda272d12010-02-15 08:04:42 +0000397 std::string Predicate;
398public:
Chris Lattnerb21ba712010-02-25 02:04:40 +0000399 CheckPatternPredicateMatcher(StringRef predicate)
Chris Lattner58aa8342010-02-25 06:49:58 +0000400 : Matcher(CheckPatternPredicate), Predicate(predicate) {}
Chris Lattnerda272d12010-02-15 08:04:42 +0000401
402 StringRef getPredicate() const { return Predicate; }
403
Chris Lattnerb21ba712010-02-25 02:04:40 +0000404 static inline bool classof(const Matcher *N) {
Chris Lattnerda272d12010-02-15 08:04:42 +0000405 return N->getKind() == CheckPatternPredicate;
406 }
407
Chris Lattnerd323fd42010-02-27 06:22:57 +0000408 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
409
Chris Lattner58aa8342010-02-25 06:49:58 +0000410private:
Chris Lattnera5028a62010-02-25 06:53:39 +0000411 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner58aa8342010-02-25 06:49:58 +0000412 virtual bool isEqualImpl(const Matcher *M) const {
413 return cast<CheckPatternPredicateMatcher>(M)->getPredicate() == Predicate;
414 }
415 virtual unsigned getHashImpl() const;
Chris Lattnerda272d12010-02-15 08:04:42 +0000416};
417
Chris Lattnerb21ba712010-02-25 02:04:40 +0000418/// CheckPredicateMatcher - This checks the target-specific predicate to
Chris Lattnerda272d12010-02-15 08:04:42 +0000419/// see if the node is acceptable.
Chris Lattnerb21ba712010-02-25 02:04:40 +0000420class CheckPredicateMatcher : public Matcher {
Chris Lattnerda272d12010-02-15 08:04:42 +0000421 StringRef PredName;
422public:
Chris Lattnerb21ba712010-02-25 02:04:40 +0000423 CheckPredicateMatcher(StringRef predname)
424 : Matcher(CheckPredicate), PredName(predname) {}
Chris Lattnerda272d12010-02-15 08:04:42 +0000425
426 StringRef getPredicateName() const { return PredName; }
427
Chris Lattnerb21ba712010-02-25 02:04:40 +0000428 static inline bool classof(const Matcher *N) {
Chris Lattnerda272d12010-02-15 08:04:42 +0000429 return N->getKind() == CheckPredicate;
430 }
431
Chris Lattnerd323fd42010-02-27 06:22:57 +0000432 // TODO: Ok?
433 //virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
434
Chris Lattner58aa8342010-02-25 06:49:58 +0000435private:
Chris Lattnera5028a62010-02-25 06:53:39 +0000436 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner58aa8342010-02-25 06:49:58 +0000437 virtual bool isEqualImpl(const Matcher *M) const {
438 return cast<CheckPredicateMatcher>(M)->PredName == PredName;
439 }
440 virtual unsigned getHashImpl() const;
Chris Lattnerda272d12010-02-15 08:04:42 +0000441};
442
443
Chris Lattnerb21ba712010-02-25 02:04:40 +0000444/// CheckOpcodeMatcher - This checks to see if the current node has the
Chris Lattnerda272d12010-02-15 08:04:42 +0000445/// specified opcode, if not it fails to match.
Chris Lattnerb21ba712010-02-25 02:04:40 +0000446class CheckOpcodeMatcher : public Matcher {
Chris Lattnera230f962010-02-27 21:48:43 +0000447 const SDNodeInfo &Opcode;
Chris Lattnerda272d12010-02-15 08:04:42 +0000448public:
Chris Lattnera230f962010-02-27 21:48:43 +0000449 CheckOpcodeMatcher(const SDNodeInfo &opcode)
450 : Matcher(CheckOpcode), Opcode(opcode) {}
Chris Lattnerda272d12010-02-15 08:04:42 +0000451
Chris Lattnera230f962010-02-27 21:48:43 +0000452 const SDNodeInfo &getOpcode() const { return Opcode; }
Chris Lattnerda272d12010-02-15 08:04:42 +0000453
Chris Lattnerb21ba712010-02-25 02:04:40 +0000454 static inline bool classof(const Matcher *N) {
Chris Lattnerda272d12010-02-15 08:04:42 +0000455 return N->getKind() == CheckOpcode;
456 }
457
Chris Lattnerd323fd42010-02-27 06:22:57 +0000458 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
459
Chris Lattner58aa8342010-02-25 06:49:58 +0000460private:
Chris Lattnera5028a62010-02-25 06:53:39 +0000461 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattnereb669212010-03-01 06:59:22 +0000462 virtual bool isEqualImpl(const Matcher *M) const;
Chris Lattner58aa8342010-02-25 06:49:58 +0000463 virtual unsigned getHashImpl() const;
Chris Lattner82781b92010-02-27 07:49:13 +0000464 virtual bool isContradictoryImpl(const Matcher *M) const;
Chris Lattnerda272d12010-02-15 08:04:42 +0000465};
Chris Lattnereb669212010-03-01 06:59:22 +0000466
467/// SwitchOpcodeMatcher - Switch based on the current node's opcode, dispatching
468/// to one matcher per opcode. If the opcode doesn't match any of the cases,
469/// then the match fails. This is semantically equivalent to a Scope node where
470/// every child does a CheckOpcode, but is much faster.
471class SwitchOpcodeMatcher : public Matcher {
472 SmallVector<std::pair<const SDNodeInfo*, Matcher*>, 8> Cases;
473public:
474 SwitchOpcodeMatcher(const std::pair<const SDNodeInfo*, Matcher*> *cases,
475 unsigned numcases)
476 : Matcher(SwitchOpcode), Cases(cases, cases+numcases) {}
477
478 static inline bool classof(const Matcher *N) {
479 return N->getKind() == SwitchOpcode;
480 }
481
482 unsigned getNumCases() const { return Cases.size(); }
483
484 const SDNodeInfo &getCaseOpcode(unsigned i) const { return *Cases[i].first; }
485 Matcher *getCaseMatcher(unsigned i) { return Cases[i].second; }
486 const Matcher *getCaseMatcher(unsigned i) const { return Cases[i].second; }
487
488private:
489 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
490 virtual bool isEqualImpl(const Matcher *M) const { return false; }
491 virtual unsigned getHashImpl() const { return 4123; }
492};
Chris Lattnerda272d12010-02-15 08:04:42 +0000493
Chris Lattnerb21ba712010-02-25 02:04:40 +0000494/// CheckTypeMatcher - This checks to see if the current node has the
Chris Lattner084df622010-03-24 00:41:19 +0000495/// specified type at the specified result, if not it fails to match.
Chris Lattnerb21ba712010-02-25 02:04:40 +0000496class CheckTypeMatcher : public Matcher {
Chris Lattnerda272d12010-02-15 08:04:42 +0000497 MVT::SimpleValueType Type;
Chris Lattner084df622010-03-24 00:41:19 +0000498 unsigned ResNo;
Chris Lattnerda272d12010-02-15 08:04:42 +0000499public:
Chris Lattner084df622010-03-24 00:41:19 +0000500 CheckTypeMatcher(MVT::SimpleValueType type, unsigned resno)
501 : Matcher(CheckType), Type(type), ResNo(resno) {}
Chris Lattnerda272d12010-02-15 08:04:42 +0000502
503 MVT::SimpleValueType getType() const { return Type; }
Chris Lattner084df622010-03-24 00:41:19 +0000504 unsigned getResNo() const { return ResNo; }
Chris Lattnerda272d12010-02-15 08:04:42 +0000505
Chris Lattnerb21ba712010-02-25 02:04:40 +0000506 static inline bool classof(const Matcher *N) {
Chris Lattnerda272d12010-02-15 08:04:42 +0000507 return N->getKind() == CheckType;
508 }
509
Chris Lattnerd323fd42010-02-27 06:22:57 +0000510 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
511
Chris Lattner58aa8342010-02-25 06:49:58 +0000512private:
Chris Lattnera5028a62010-02-25 06:53:39 +0000513 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner58aa8342010-02-25 06:49:58 +0000514 virtual bool isEqualImpl(const Matcher *M) const {
Chris Lattner38717f62010-02-26 08:05:36 +0000515 return cast<CheckTypeMatcher>(M)->Type == Type;
Chris Lattner58aa8342010-02-25 06:49:58 +0000516 }
517 virtual unsigned getHashImpl() const { return Type; }
Chris Lattner82781b92010-02-27 07:49:13 +0000518 virtual bool isContradictoryImpl(const Matcher *M) const;
Chris Lattnerda272d12010-02-15 08:04:42 +0000519};
Chris Lattner23cfda72010-02-24 20:15:25 +0000520
Chris Lattnercfe2eab2010-03-03 06:28:15 +0000521/// SwitchTypeMatcher - Switch based on the current node's type, dispatching
522/// to one matcher per case. If the type doesn't match any of the cases,
523/// then the match fails. This is semantically equivalent to a Scope node where
524/// every child does a CheckType, but is much faster.
525class SwitchTypeMatcher : public Matcher {
526 SmallVector<std::pair<MVT::SimpleValueType, Matcher*>, 8> Cases;
527public:
528 SwitchTypeMatcher(const std::pair<MVT::SimpleValueType, Matcher*> *cases,
529 unsigned numcases)
530 : Matcher(SwitchType), Cases(cases, cases+numcases) {}
531
532 static inline bool classof(const Matcher *N) {
533 return N->getKind() == SwitchType;
534 }
535
536 unsigned getNumCases() const { return Cases.size(); }
537
538 MVT::SimpleValueType getCaseType(unsigned i) const { return Cases[i].first; }
539 Matcher *getCaseMatcher(unsigned i) { return Cases[i].second; }
540 const Matcher *getCaseMatcher(unsigned i) const { return Cases[i].second; }
541
542private:
543 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
544 virtual bool isEqualImpl(const Matcher *M) const { return false; }
545 virtual unsigned getHashImpl() const { return 4123; }
546};
547
548
Chris Lattnerb21ba712010-02-25 02:04:40 +0000549/// CheckChildTypeMatcher - This checks to see if a child node has the
Chris Lattner23cfda72010-02-24 20:15:25 +0000550/// specified type, if not it fails to match.
Chris Lattnerb21ba712010-02-25 02:04:40 +0000551class CheckChildTypeMatcher : public Matcher {
Chris Lattner23cfda72010-02-24 20:15:25 +0000552 unsigned ChildNo;
553 MVT::SimpleValueType Type;
554public:
Chris Lattnerb21ba712010-02-25 02:04:40 +0000555 CheckChildTypeMatcher(unsigned childno, MVT::SimpleValueType type)
556 : Matcher(CheckChildType), ChildNo(childno), Type(type) {}
Chris Lattner23cfda72010-02-24 20:15:25 +0000557
558 unsigned getChildNo() const { return ChildNo; }
559 MVT::SimpleValueType getType() const { return Type; }
560
Chris Lattnerb21ba712010-02-25 02:04:40 +0000561 static inline bool classof(const Matcher *N) {
Chris Lattner23cfda72010-02-24 20:15:25 +0000562 return N->getKind() == CheckChildType;
563 }
564
Chris Lattnerd323fd42010-02-27 06:22:57 +0000565 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
566
Chris Lattner58aa8342010-02-25 06:49:58 +0000567private:
Chris Lattnera5028a62010-02-25 06:53:39 +0000568 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner58aa8342010-02-25 06:49:58 +0000569 virtual bool isEqualImpl(const Matcher *M) const {
570 return cast<CheckChildTypeMatcher>(M)->ChildNo == ChildNo &&
571 cast<CheckChildTypeMatcher>(M)->Type == Type;
572 }
573 virtual unsigned getHashImpl() const { return (Type << 3) | ChildNo; }
Chris Lattner82781b92010-02-27 07:49:13 +0000574 virtual bool isContradictoryImpl(const Matcher *M) const;
Chris Lattner23cfda72010-02-24 20:15:25 +0000575};
576
Chris Lattnerda272d12010-02-15 08:04:42 +0000577
Chris Lattnerb21ba712010-02-25 02:04:40 +0000578/// CheckIntegerMatcher - This checks to see if the current node is a
Chris Lattnerda272d12010-02-15 08:04:42 +0000579/// ConstantSDNode with the specified integer value, if not it fails to match.
Chris Lattnerb21ba712010-02-25 02:04:40 +0000580class CheckIntegerMatcher : public Matcher {
Chris Lattnerda272d12010-02-15 08:04:42 +0000581 int64_t Value;
582public:
Chris Lattnerb21ba712010-02-25 02:04:40 +0000583 CheckIntegerMatcher(int64_t value)
584 : Matcher(CheckInteger), Value(value) {}
Chris Lattnerda272d12010-02-15 08:04:42 +0000585
586 int64_t getValue() const { return Value; }
587
Chris Lattnerb21ba712010-02-25 02:04:40 +0000588 static inline bool classof(const Matcher *N) {
Chris Lattnerda272d12010-02-15 08:04:42 +0000589 return N->getKind() == CheckInteger;
590 }
591
Chris Lattnerd323fd42010-02-27 06:22:57 +0000592 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
593
Chris Lattner58aa8342010-02-25 06:49:58 +0000594private:
Chris Lattnera5028a62010-02-25 06:53:39 +0000595 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner58aa8342010-02-25 06:49:58 +0000596 virtual bool isEqualImpl(const Matcher *M) const {
597 return cast<CheckIntegerMatcher>(M)->Value == Value;
598 }
599 virtual unsigned getHashImpl() const { return Value; }
Chris Lattner24789622010-02-27 08:11:15 +0000600 virtual bool isContradictoryImpl(const Matcher *M) const;
Chris Lattnerda272d12010-02-15 08:04:42 +0000601};
602
Chris Lattnerb21ba712010-02-25 02:04:40 +0000603/// CheckCondCodeMatcher - This checks to see if the current node is a
Chris Lattnerda272d12010-02-15 08:04:42 +0000604/// CondCodeSDNode with the specified condition, if not it fails to match.
Chris Lattnerb21ba712010-02-25 02:04:40 +0000605class CheckCondCodeMatcher : public Matcher {
Chris Lattnerda272d12010-02-15 08:04:42 +0000606 StringRef CondCodeName;
607public:
Chris Lattnerb21ba712010-02-25 02:04:40 +0000608 CheckCondCodeMatcher(StringRef condcodename)
609 : Matcher(CheckCondCode), CondCodeName(condcodename) {}
Chris Lattnerda272d12010-02-15 08:04:42 +0000610
611 StringRef getCondCodeName() const { return CondCodeName; }
612
Chris Lattnerb21ba712010-02-25 02:04:40 +0000613 static inline bool classof(const Matcher *N) {
Chris Lattnerda272d12010-02-15 08:04:42 +0000614 return N->getKind() == CheckCondCode;
615 }
616
Chris Lattnerd323fd42010-02-27 06:22:57 +0000617 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
618
Chris Lattner58aa8342010-02-25 06:49:58 +0000619private:
Chris Lattnera5028a62010-02-25 06:53:39 +0000620 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner58aa8342010-02-25 06:49:58 +0000621 virtual bool isEqualImpl(const Matcher *M) const {
622 return cast<CheckCondCodeMatcher>(M)->CondCodeName == CondCodeName;
623 }
624 virtual unsigned getHashImpl() const;
Chris Lattnerda272d12010-02-15 08:04:42 +0000625};
626
Chris Lattnerb21ba712010-02-25 02:04:40 +0000627/// CheckValueTypeMatcher - This checks to see if the current node is a
Chris Lattnerda272d12010-02-15 08:04:42 +0000628/// VTSDNode with the specified type, if not it fails to match.
Chris Lattnerb21ba712010-02-25 02:04:40 +0000629class CheckValueTypeMatcher : public Matcher {
Chris Lattnerda272d12010-02-15 08:04:42 +0000630 StringRef TypeName;
631public:
Chris Lattnerb21ba712010-02-25 02:04:40 +0000632 CheckValueTypeMatcher(StringRef type_name)
633 : Matcher(CheckValueType), TypeName(type_name) {}
Chris Lattnerda272d12010-02-15 08:04:42 +0000634
635 StringRef getTypeName() const { return TypeName; }
636
Chris Lattnerb21ba712010-02-25 02:04:40 +0000637 static inline bool classof(const Matcher *N) {
Chris Lattnerda272d12010-02-15 08:04:42 +0000638 return N->getKind() == CheckValueType;
639 }
640
Chris Lattnerd323fd42010-02-27 06:22:57 +0000641 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
642
Chris Lattner58aa8342010-02-25 06:49:58 +0000643private:
Chris Lattnera5028a62010-02-25 06:53:39 +0000644 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner58aa8342010-02-25 06:49:58 +0000645 virtual bool isEqualImpl(const Matcher *M) const {
646 return cast<CheckValueTypeMatcher>(M)->TypeName == TypeName;
647 }
648 virtual unsigned getHashImpl() const;
Chris Lattner48aa5752010-03-07 06:29:26 +0000649 bool isContradictoryImpl(const Matcher *M) const;
Chris Lattnerda272d12010-02-15 08:04:42 +0000650};
651
652
653
Chris Lattnerb21ba712010-02-25 02:04:40 +0000654/// CheckComplexPatMatcher - This node runs the specified ComplexPattern on
Chris Lattnerda272d12010-02-15 08:04:42 +0000655/// the current node.
Chris Lattnerb21ba712010-02-25 02:04:40 +0000656class CheckComplexPatMatcher : public Matcher {
Chris Lattnerda272d12010-02-15 08:04:42 +0000657 const ComplexPattern &Pattern;
Chris Lattner57bf8a42010-03-04 01:23:08 +0000658
659 /// MatchNumber - This is the recorded nodes slot that contains the node we want to
660 /// match against.
661 unsigned MatchNumber;
662
663 /// Name - The name of the node we're matching, for comment emission.
664 std::string Name;
665
Chris Lattnerd1aca7c2010-03-04 00:28:05 +0000666 /// FirstResult - This is the first slot in the RecordedNodes list that the
667 /// result of the match populates.
668 unsigned FirstResult;
Chris Lattnerda272d12010-02-15 08:04:42 +0000669public:
Chris Lattner57bf8a42010-03-04 01:23:08 +0000670 CheckComplexPatMatcher(const ComplexPattern &pattern, unsigned matchnumber,
671 const std::string &name, unsigned firstresult)
672 : Matcher(CheckComplexPat), Pattern(pattern), MatchNumber(matchnumber),
673 Name(name), FirstResult(firstresult) {}
Chris Lattnerda272d12010-02-15 08:04:42 +0000674
Chris Lattnere609a512010-02-17 00:31:50 +0000675 const ComplexPattern &getPattern() const { return Pattern; }
Chris Lattner57bf8a42010-03-04 01:23:08 +0000676 unsigned getMatchNumber() const { return MatchNumber; }
677
678 const std::string getName() const { return Name; }
Chris Lattnerd1aca7c2010-03-04 00:28:05 +0000679 unsigned getFirstResult() const { return FirstResult; }
Chris Lattnere609a512010-02-17 00:31:50 +0000680
Chris Lattnerb21ba712010-02-25 02:04:40 +0000681 static inline bool classof(const Matcher *N) {
Chris Lattnerda272d12010-02-15 08:04:42 +0000682 return N->getKind() == CheckComplexPat;
683 }
684
Chris Lattnerd323fd42010-02-27 06:22:57 +0000685 // Not safe to move a pattern predicate past a complex pattern.
686 virtual bool isSafeToReorderWithPatternPredicate() const { return false; }
687
Chris Lattner58aa8342010-02-25 06:49:58 +0000688private:
Chris Lattnera5028a62010-02-25 06:53:39 +0000689 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner58aa8342010-02-25 06:49:58 +0000690 virtual bool isEqualImpl(const Matcher *M) const {
Chris Lattner57bf8a42010-03-04 01:23:08 +0000691 return &cast<CheckComplexPatMatcher>(M)->Pattern == &Pattern &&
692 cast<CheckComplexPatMatcher>(M)->MatchNumber == MatchNumber;
Chris Lattner58aa8342010-02-25 06:49:58 +0000693 }
694 virtual unsigned getHashImpl() const {
Chris Lattner57bf8a42010-03-04 01:23:08 +0000695 return (unsigned)(intptr_t)&Pattern ^ MatchNumber;
Chris Lattner58aa8342010-02-25 06:49:58 +0000696 }
Chris Lattnerda272d12010-02-15 08:04:42 +0000697};
698
Chris Lattnerb21ba712010-02-25 02:04:40 +0000699/// CheckAndImmMatcher - This checks to see if the current node is an 'and'
Chris Lattnerda272d12010-02-15 08:04:42 +0000700/// with something equivalent to the specified immediate.
Chris Lattnerb21ba712010-02-25 02:04:40 +0000701class CheckAndImmMatcher : public Matcher {
Chris Lattnerda272d12010-02-15 08:04:42 +0000702 int64_t Value;
703public:
Chris Lattnerb21ba712010-02-25 02:04:40 +0000704 CheckAndImmMatcher(int64_t value)
705 : Matcher(CheckAndImm), Value(value) {}
Chris Lattnerda272d12010-02-15 08:04:42 +0000706
707 int64_t getValue() const { return Value; }
708
Chris Lattnerb21ba712010-02-25 02:04:40 +0000709 static inline bool classof(const Matcher *N) {
Chris Lattnerda272d12010-02-15 08:04:42 +0000710 return N->getKind() == CheckAndImm;
711 }
712
Chris Lattnerd323fd42010-02-27 06:22:57 +0000713 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
714
Chris Lattner58aa8342010-02-25 06:49:58 +0000715private:
Chris Lattnera5028a62010-02-25 06:53:39 +0000716 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner58aa8342010-02-25 06:49:58 +0000717 virtual bool isEqualImpl(const Matcher *M) const {
718 return cast<CheckAndImmMatcher>(M)->Value == Value;
719 }
720 virtual unsigned getHashImpl() const { return Value; }
Chris Lattnerda272d12010-02-15 08:04:42 +0000721};
722
Chris Lattnerb21ba712010-02-25 02:04:40 +0000723/// CheckOrImmMatcher - This checks to see if the current node is an 'and'
Chris Lattnerda272d12010-02-15 08:04:42 +0000724/// with something equivalent to the specified immediate.
Chris Lattnerb21ba712010-02-25 02:04:40 +0000725class CheckOrImmMatcher : public Matcher {
Chris Lattnerda272d12010-02-15 08:04:42 +0000726 int64_t Value;
727public:
Chris Lattnerb21ba712010-02-25 02:04:40 +0000728 CheckOrImmMatcher(int64_t value)
729 : Matcher(CheckOrImm), Value(value) {}
Chris Lattnerda272d12010-02-15 08:04:42 +0000730
731 int64_t getValue() const { return Value; }
732
Chris Lattnerb21ba712010-02-25 02:04:40 +0000733 static inline bool classof(const Matcher *N) {
Chris Lattnerda272d12010-02-15 08:04:42 +0000734 return N->getKind() == CheckOrImm;
735 }
736
Chris Lattnerd323fd42010-02-27 06:22:57 +0000737 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
738
Chris Lattner58aa8342010-02-25 06:49:58 +0000739private:
Chris Lattnera5028a62010-02-25 06:53:39 +0000740 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner58aa8342010-02-25 06:49:58 +0000741 virtual bool isEqualImpl(const Matcher *M) const {
742 return cast<CheckOrImmMatcher>(M)->Value == Value;
743 }
744 virtual unsigned getHashImpl() const { return Value; }
Chris Lattnerda272d12010-02-15 08:04:42 +0000745};
Chris Lattnere39650a2010-02-16 06:10:58 +0000746
Chris Lattnerb21ba712010-02-25 02:04:40 +0000747/// CheckFoldableChainNodeMatcher - This checks to see if the current node
Chris Lattner21390d72010-02-16 19:15:55 +0000748/// (which defines a chain operand) is safe to fold into a larger pattern.
Chris Lattnerb21ba712010-02-25 02:04:40 +0000749class CheckFoldableChainNodeMatcher : public Matcher {
Chris Lattnere39650a2010-02-16 06:10:58 +0000750public:
Chris Lattnerb21ba712010-02-25 02:04:40 +0000751 CheckFoldableChainNodeMatcher()
752 : Matcher(CheckFoldableChainNode) {}
Chris Lattnerda272d12010-02-15 08:04:42 +0000753
Chris Lattnerb21ba712010-02-25 02:04:40 +0000754 static inline bool classof(const Matcher *N) {
Chris Lattner21390d72010-02-16 19:15:55 +0000755 return N->getKind() == CheckFoldableChainNode;
Chris Lattnere39650a2010-02-16 06:10:58 +0000756 }
Chris Lattnerda272d12010-02-15 08:04:42 +0000757
Chris Lattnerd323fd42010-02-27 06:22:57 +0000758 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
759
Chris Lattner58aa8342010-02-25 06:49:58 +0000760private:
Chris Lattnera5028a62010-02-25 06:53:39 +0000761 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner58aa8342010-02-25 06:49:58 +0000762 virtual bool isEqualImpl(const Matcher *M) const { return true; }
763 virtual unsigned getHashImpl() const { return 0; }
Chris Lattnere39650a2010-02-16 06:10:58 +0000764};
765
Chris Lattnerb21ba712010-02-25 02:04:40 +0000766/// EmitIntegerMatcher - This creates a new TargetConstant.
767class EmitIntegerMatcher : public Matcher {
Chris Lattner845c0422010-02-18 22:03:03 +0000768 int64_t Val;
769 MVT::SimpleValueType VT;
770public:
Chris Lattnerb21ba712010-02-25 02:04:40 +0000771 EmitIntegerMatcher(int64_t val, MVT::SimpleValueType vt)
772 : Matcher(EmitInteger), Val(val), VT(vt) {}
Chris Lattner9a747f12010-02-17 06:23:39 +0000773
Chris Lattner906b4992010-02-19 07:49:56 +0000774 int64_t getValue() const { return Val; }
Chris Lattner845c0422010-02-18 22:03:03 +0000775 MVT::SimpleValueType getVT() const { return VT; }
776
Chris Lattnerb21ba712010-02-25 02:04:40 +0000777 static inline bool classof(const Matcher *N) {
Chris Lattner845c0422010-02-18 22:03:03 +0000778 return N->getKind() == EmitInteger;
779 }
780
Chris Lattner58aa8342010-02-25 06:49:58 +0000781private:
Chris Lattnera5028a62010-02-25 06:53:39 +0000782 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner58aa8342010-02-25 06:49:58 +0000783 virtual bool isEqualImpl(const Matcher *M) const {
784 return cast<EmitIntegerMatcher>(M)->Val == Val &&
785 cast<EmitIntegerMatcher>(M)->VT == VT;
786 }
787 virtual unsigned getHashImpl() const { return (Val << 4) | VT; }
Chris Lattner845c0422010-02-18 22:03:03 +0000788};
Chris Lattner8e946be2010-02-21 03:22:59 +0000789
Chris Lattnerb21ba712010-02-25 02:04:40 +0000790/// EmitStringIntegerMatcher - A target constant whose value is represented
Chris Lattner8e946be2010-02-21 03:22:59 +0000791/// by a string.
Chris Lattnerb21ba712010-02-25 02:04:40 +0000792class EmitStringIntegerMatcher : public Matcher {
Chris Lattner8e946be2010-02-21 03:22:59 +0000793 std::string Val;
794 MVT::SimpleValueType VT;
795public:
Chris Lattnerb21ba712010-02-25 02:04:40 +0000796 EmitStringIntegerMatcher(const std::string &val, MVT::SimpleValueType vt)
797 : Matcher(EmitStringInteger), Val(val), VT(vt) {}
Chris Lattner8e946be2010-02-21 03:22:59 +0000798
799 const std::string &getValue() const { return Val; }
800 MVT::SimpleValueType getVT() const { return VT; }
801
Chris Lattnerb21ba712010-02-25 02:04:40 +0000802 static inline bool classof(const Matcher *N) {
Chris Lattner8e946be2010-02-21 03:22:59 +0000803 return N->getKind() == EmitStringInteger;
804 }
805
Chris Lattner58aa8342010-02-25 06:49:58 +0000806private:
Chris Lattnera5028a62010-02-25 06:53:39 +0000807 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner58aa8342010-02-25 06:49:58 +0000808 virtual bool isEqualImpl(const Matcher *M) const {
809 return cast<EmitStringIntegerMatcher>(M)->Val == Val &&
810 cast<EmitStringIntegerMatcher>(M)->VT == VT;
811 }
812 virtual unsigned getHashImpl() const;
Chris Lattner8e946be2010-02-21 03:22:59 +0000813};
Chris Lattner845c0422010-02-18 22:03:03 +0000814
Chris Lattnerb21ba712010-02-25 02:04:40 +0000815/// EmitRegisterMatcher - This creates a new TargetConstant.
816class EmitRegisterMatcher : public Matcher {
Chris Lattner845c0422010-02-18 22:03:03 +0000817 /// Reg - The def for the register that we're emitting. If this is null, then
818 /// this is a reference to zero_reg.
819 Record *Reg;
820 MVT::SimpleValueType VT;
821public:
Chris Lattnerb21ba712010-02-25 02:04:40 +0000822 EmitRegisterMatcher(Record *reg, MVT::SimpleValueType vt)
823 : Matcher(EmitRegister), Reg(reg), VT(vt) {}
Chris Lattner845c0422010-02-18 22:03:03 +0000824
825 Record *getReg() const { return Reg; }
826 MVT::SimpleValueType getVT() const { return VT; }
827
Chris Lattnerb21ba712010-02-25 02:04:40 +0000828 static inline bool classof(const Matcher *N) {
Chris Lattner845c0422010-02-18 22:03:03 +0000829 return N->getKind() == EmitRegister;
830 }
831
Chris Lattner58aa8342010-02-25 06:49:58 +0000832private:
Chris Lattnera5028a62010-02-25 06:53:39 +0000833 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner58aa8342010-02-25 06:49:58 +0000834 virtual bool isEqualImpl(const Matcher *M) const {
835 return cast<EmitRegisterMatcher>(M)->Reg == Reg &&
836 cast<EmitRegisterMatcher>(M)->VT == VT;
837 }
838 virtual unsigned getHashImpl() const {
839 return ((unsigned)(intptr_t)Reg) << 4 | VT;
840 }
Chris Lattner845c0422010-02-18 22:03:03 +0000841};
Chris Lattner8e946be2010-02-21 03:22:59 +0000842
Chris Lattnerb21ba712010-02-25 02:04:40 +0000843/// EmitConvertToTargetMatcher - Emit an operation that reads a specified
Chris Lattner8e946be2010-02-21 03:22:59 +0000844/// recorded node and converts it from being a ISD::Constant to
845/// ISD::TargetConstant, likewise for ConstantFP.
Chris Lattnerb21ba712010-02-25 02:04:40 +0000846class EmitConvertToTargetMatcher : public Matcher {
Chris Lattner8e946be2010-02-21 03:22:59 +0000847 unsigned Slot;
848public:
Chris Lattnerb21ba712010-02-25 02:04:40 +0000849 EmitConvertToTargetMatcher(unsigned slot)
850 : Matcher(EmitConvertToTarget), Slot(slot) {}
Chris Lattner8e946be2010-02-21 03:22:59 +0000851
852 unsigned getSlot() const { return Slot; }
853
Chris Lattnerb21ba712010-02-25 02:04:40 +0000854 static inline bool classof(const Matcher *N) {
Chris Lattner8e946be2010-02-21 03:22:59 +0000855 return N->getKind() == EmitConvertToTarget;
856 }
857
Chris Lattner58aa8342010-02-25 06:49:58 +0000858private:
Chris Lattnera5028a62010-02-25 06:53:39 +0000859 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner58aa8342010-02-25 06:49:58 +0000860 virtual bool isEqualImpl(const Matcher *M) const {
861 return cast<EmitConvertToTargetMatcher>(M)->Slot == Slot;
862 }
863 virtual unsigned getHashImpl() const { return Slot; }
Chris Lattner8e946be2010-02-21 03:22:59 +0000864};
865
Chris Lattnerb21ba712010-02-25 02:04:40 +0000866/// EmitMergeInputChainsMatcher - Emit a node that merges a list of input
Chris Lattner8e946be2010-02-21 03:22:59 +0000867/// chains together with a token factor. The list of nodes are the nodes in the
868/// matched pattern that have chain input/outputs. This node adds all input
869/// chains of these nodes if they are not themselves a node in the pattern.
Chris Lattnerb21ba712010-02-25 02:04:40 +0000870class EmitMergeInputChainsMatcher : public Matcher {
Chris Lattner8e946be2010-02-21 03:22:59 +0000871 SmallVector<unsigned, 3> ChainNodes;
872public:
Chris Lattnerb21ba712010-02-25 02:04:40 +0000873 EmitMergeInputChainsMatcher(const unsigned *nodes, unsigned NumNodes)
874 : Matcher(EmitMergeInputChains), ChainNodes(nodes, nodes+NumNodes) {}
Chris Lattner8e946be2010-02-21 03:22:59 +0000875
876 unsigned getNumNodes() const { return ChainNodes.size(); }
877
878 unsigned getNode(unsigned i) const {
879 assert(i < ChainNodes.size());
880 return ChainNodes[i];
881 }
882
Chris Lattnerb21ba712010-02-25 02:04:40 +0000883 static inline bool classof(const Matcher *N) {
Chris Lattner8e946be2010-02-21 03:22:59 +0000884 return N->getKind() == EmitMergeInputChains;
885 }
886
Chris Lattner58aa8342010-02-25 06:49:58 +0000887private:
Chris Lattnera5028a62010-02-25 06:53:39 +0000888 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner58aa8342010-02-25 06:49:58 +0000889 virtual bool isEqualImpl(const Matcher *M) const {
890 return cast<EmitMergeInputChainsMatcher>(M)->ChainNodes == ChainNodes;
891 }
892 virtual unsigned getHashImpl() const;
Chris Lattner8e946be2010-02-21 03:22:59 +0000893};
894
Chris Lattnerb21ba712010-02-25 02:04:40 +0000895/// EmitCopyToRegMatcher - Emit a CopyToReg node from a value to a physreg,
Chris Lattner8e946be2010-02-21 03:22:59 +0000896/// pushing the chain and flag results.
897///
Chris Lattnerb21ba712010-02-25 02:04:40 +0000898class EmitCopyToRegMatcher : public Matcher {
Chris Lattner8e946be2010-02-21 03:22:59 +0000899 unsigned SrcSlot; // Value to copy into the physreg.
900 Record *DestPhysReg;
901public:
Chris Lattnerb21ba712010-02-25 02:04:40 +0000902 EmitCopyToRegMatcher(unsigned srcSlot, Record *destPhysReg)
903 : Matcher(EmitCopyToReg), SrcSlot(srcSlot), DestPhysReg(destPhysReg) {}
Chris Lattner8e946be2010-02-21 03:22:59 +0000904
905 unsigned getSrcSlot() const { return SrcSlot; }
906 Record *getDestPhysReg() const { return DestPhysReg; }
907
Chris Lattnerb21ba712010-02-25 02:04:40 +0000908 static inline bool classof(const Matcher *N) {
Chris Lattner8e946be2010-02-21 03:22:59 +0000909 return N->getKind() == EmitCopyToReg;
910 }
911
Chris Lattner58aa8342010-02-25 06:49:58 +0000912private:
Chris Lattnera5028a62010-02-25 06:53:39 +0000913 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner58aa8342010-02-25 06:49:58 +0000914 virtual bool isEqualImpl(const Matcher *M) const {
915 return cast<EmitCopyToRegMatcher>(M)->SrcSlot == SrcSlot &&
916 cast<EmitCopyToRegMatcher>(M)->DestPhysReg == DestPhysReg;
917 }
918 virtual unsigned getHashImpl() const {
919 return SrcSlot ^ ((unsigned)(intptr_t)DestPhysReg << 4);
920 }
Chris Lattner8e946be2010-02-21 03:22:59 +0000921};
922
923
924
Chris Lattnerb21ba712010-02-25 02:04:40 +0000925/// EmitNodeXFormMatcher - Emit an operation that runs an SDNodeXForm on a
Chris Lattner8e946be2010-02-21 03:22:59 +0000926/// recorded node and records the result.
Chris Lattnerb21ba712010-02-25 02:04:40 +0000927class EmitNodeXFormMatcher : public Matcher {
Chris Lattner8e946be2010-02-21 03:22:59 +0000928 unsigned Slot;
929 Record *NodeXForm;
930public:
Chris Lattnerb21ba712010-02-25 02:04:40 +0000931 EmitNodeXFormMatcher(unsigned slot, Record *nodeXForm)
932 : Matcher(EmitNodeXForm), Slot(slot), NodeXForm(nodeXForm) {}
Chris Lattner8e946be2010-02-21 03:22:59 +0000933
934 unsigned getSlot() const { return Slot; }
935 Record *getNodeXForm() const { return NodeXForm; }
936
Chris Lattnerb21ba712010-02-25 02:04:40 +0000937 static inline bool classof(const Matcher *N) {
Chris Lattner8e946be2010-02-21 03:22:59 +0000938 return N->getKind() == EmitNodeXForm;
939 }
940
Chris Lattner58aa8342010-02-25 06:49:58 +0000941private:
Chris Lattnera5028a62010-02-25 06:53:39 +0000942 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner58aa8342010-02-25 06:49:58 +0000943 virtual bool isEqualImpl(const Matcher *M) const {
944 return cast<EmitNodeXFormMatcher>(M)->Slot == Slot &&
945 cast<EmitNodeXFormMatcher>(M)->NodeXForm == NodeXForm;
946 }
947 virtual unsigned getHashImpl() const {
948 return Slot ^ ((unsigned)(intptr_t)NodeXForm << 4);
949 }
Chris Lattner8e946be2010-02-21 03:22:59 +0000950};
Chris Lattner845c0422010-02-18 22:03:03 +0000951
Chris Lattnere86097a2010-02-28 02:31:26 +0000952/// EmitNodeMatcherCommon - Common class shared between EmitNode and
Chris Lattner9a215002010-02-28 20:55:18 +0000953/// MorphNodeTo.
Chris Lattnere86097a2010-02-28 02:31:26 +0000954class EmitNodeMatcherCommon : public Matcher {
Chris Lattner8e946be2010-02-21 03:22:59 +0000955 std::string OpcodeName;
956 const SmallVector<MVT::SimpleValueType, 3> VTs;
957 const SmallVector<unsigned, 6> Operands;
Chris Lattnerff7fb602010-02-28 21:53:42 +0000958 bool HasChain, HasInFlag, HasOutFlag, HasMemRefs;
Chris Lattner845c0422010-02-18 22:03:03 +0000959
Chris Lattner8e946be2010-02-21 03:22:59 +0000960 /// NumFixedArityOperands - If this is a fixed arity node, this is set to -1.
961 /// If this is a varidic node, this is set to the number of fixed arity
962 /// operands in the root of the pattern. The rest are appended to this node.
963 int NumFixedArityOperands;
964public:
Chris Lattnere86097a2010-02-28 02:31:26 +0000965 EmitNodeMatcherCommon(const std::string &opcodeName,
966 const MVT::SimpleValueType *vts, unsigned numvts,
967 const unsigned *operands, unsigned numops,
Chris Lattnerff7fb602010-02-28 21:53:42 +0000968 bool hasChain, bool hasInFlag, bool hasOutFlag,
969 bool hasmemrefs,
Chris Lattner9a215002010-02-28 20:55:18 +0000970 int numfixedarityoperands, bool isMorphNodeTo)
971 : Matcher(isMorphNodeTo ? MorphNodeTo : EmitNode), OpcodeName(opcodeName),
Chris Lattner8e946be2010-02-21 03:22:59 +0000972 VTs(vts, vts+numvts), Operands(operands, operands+numops),
Chris Lattnerff7fb602010-02-28 21:53:42 +0000973 HasChain(hasChain), HasInFlag(hasInFlag), HasOutFlag(hasOutFlag),
974 HasMemRefs(hasmemrefs), NumFixedArityOperands(numfixedarityoperands) {}
Chris Lattner8e946be2010-02-21 03:22:59 +0000975
976 const std::string &getOpcodeName() const { return OpcodeName; }
977
978 unsigned getNumVTs() const { return VTs.size(); }
979 MVT::SimpleValueType getVT(unsigned i) const {
980 assert(i < VTs.size());
981 return VTs[i];
982 }
Chris Lattnerc78f2a32010-02-28 20:49:53 +0000983
Chris Lattner8e946be2010-02-21 03:22:59 +0000984 unsigned getNumOperands() const { return Operands.size(); }
985 unsigned getOperand(unsigned i) const {
986 assert(i < Operands.size());
987 return Operands[i];
Chris Lattnerc78f2a32010-02-28 20:49:53 +0000988 }
989
990 const SmallVectorImpl<MVT::SimpleValueType> &getVTList() const { return VTs; }
991 const SmallVectorImpl<unsigned> &getOperandList() const { return Operands; }
992
Chris Lattner8e946be2010-02-21 03:22:59 +0000993
994 bool hasChain() const { return HasChain; }
Chris Lattnerff7fb602010-02-28 21:53:42 +0000995 bool hasInFlag() const { return HasInFlag; }
996 bool hasOutFlag() const { return HasOutFlag; }
Chris Lattner8e946be2010-02-21 03:22:59 +0000997 bool hasMemRefs() const { return HasMemRefs; }
998 int getNumFixedArityOperands() const { return NumFixedArityOperands; }
Chris Lattner845c0422010-02-18 22:03:03 +0000999
Chris Lattnerb21ba712010-02-25 02:04:40 +00001000 static inline bool classof(const Matcher *N) {
Chris Lattner9a215002010-02-28 20:55:18 +00001001 return N->getKind() == EmitNode || N->getKind() == MorphNodeTo;
Chris Lattner845c0422010-02-18 22:03:03 +00001002 }
1003
Chris Lattner58aa8342010-02-25 06:49:58 +00001004private:
Chris Lattnera5028a62010-02-25 06:53:39 +00001005 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner58aa8342010-02-25 06:49:58 +00001006 virtual bool isEqualImpl(const Matcher *M) const;
1007 virtual unsigned getHashImpl() const;
Chris Lattner845c0422010-02-18 22:03:03 +00001008};
Chris Lattner77f2e272010-02-21 06:03:07 +00001009
Chris Lattnere86097a2010-02-28 02:31:26 +00001010/// EmitNodeMatcher - This signals a successful match and generates a node.
1011class EmitNodeMatcher : public EmitNodeMatcherCommon {
Chris Lattner6281cda2010-02-28 02:41:25 +00001012 unsigned FirstResultSlot;
Chris Lattnere86097a2010-02-28 02:31:26 +00001013public:
1014 EmitNodeMatcher(const std::string &opcodeName,
1015 const MVT::SimpleValueType *vts, unsigned numvts,
1016 const unsigned *operands, unsigned numops,
Chris Lattnerff7fb602010-02-28 21:53:42 +00001017 bool hasChain, bool hasInFlag, bool hasOutFlag,
1018 bool hasmemrefs,
Chris Lattner6281cda2010-02-28 02:41:25 +00001019 int numfixedarityoperands, unsigned firstresultslot)
Chris Lattnere86097a2010-02-28 02:31:26 +00001020 : EmitNodeMatcherCommon(opcodeName, vts, numvts, operands, numops, hasChain,
Chris Lattnerff7fb602010-02-28 21:53:42 +00001021 hasInFlag, hasOutFlag, hasmemrefs,
1022 numfixedarityoperands, false),
Chris Lattner6281cda2010-02-28 02:41:25 +00001023 FirstResultSlot(firstresultslot) {}
1024
1025 unsigned getFirstResultSlot() const { return FirstResultSlot; }
Chris Lattnere86097a2010-02-28 02:31:26 +00001026
1027 static inline bool classof(const Matcher *N) {
1028 return N->getKind() == EmitNode;
1029 }
1030
1031};
1032
Chris Lattner9a215002010-02-28 20:55:18 +00001033class MorphNodeToMatcher : public EmitNodeMatcherCommon {
Chris Lattnere86097a2010-02-28 02:31:26 +00001034 const PatternToMatch &Pattern;
1035public:
Chris Lattner9a215002010-02-28 20:55:18 +00001036 MorphNodeToMatcher(const std::string &opcodeName,
1037 const MVT::SimpleValueType *vts, unsigned numvts,
1038 const unsigned *operands, unsigned numops,
Chris Lattnerff7fb602010-02-28 21:53:42 +00001039 bool hasChain, bool hasInFlag, bool hasOutFlag,
1040 bool hasmemrefs,
Chris Lattner9a215002010-02-28 20:55:18 +00001041 int numfixedarityoperands, const PatternToMatch &pattern)
Chris Lattnere86097a2010-02-28 02:31:26 +00001042 : EmitNodeMatcherCommon(opcodeName, vts, numvts, operands, numops, hasChain,
Chris Lattnerff7fb602010-02-28 21:53:42 +00001043 hasInFlag, hasOutFlag, hasmemrefs,
1044 numfixedarityoperands, true),
Chris Lattnere86097a2010-02-28 02:31:26 +00001045 Pattern(pattern) {
1046 }
1047
1048 const PatternToMatch &getPattern() const { return Pattern; }
1049
1050 static inline bool classof(const Matcher *N) {
Chris Lattner9a215002010-02-28 20:55:18 +00001051 return N->getKind() == MorphNodeTo;
Chris Lattnere86097a2010-02-28 02:31:26 +00001052 }
1053};
1054
Chris Lattnerb21ba712010-02-25 02:04:40 +00001055/// MarkFlagResultsMatcher - This node indicates which non-root nodes in the
1056/// pattern produce flags. This allows CompleteMatchMatcher to update them
Chris Lattner02f73582010-02-24 05:33:42 +00001057/// with the output flag of the resultant code.
Chris Lattnerb21ba712010-02-25 02:04:40 +00001058class MarkFlagResultsMatcher : public Matcher {
Chris Lattner02f73582010-02-24 05:33:42 +00001059 SmallVector<unsigned, 3> FlagResultNodes;
1060public:
Chris Lattnerb21ba712010-02-25 02:04:40 +00001061 MarkFlagResultsMatcher(const unsigned *nodes, unsigned NumNodes)
1062 : Matcher(MarkFlagResults), FlagResultNodes(nodes, nodes+NumNodes) {}
Chris Lattner02f73582010-02-24 05:33:42 +00001063
1064 unsigned getNumNodes() const { return FlagResultNodes.size(); }
1065
1066 unsigned getNode(unsigned i) const {
1067 assert(i < FlagResultNodes.size());
1068 return FlagResultNodes[i];
1069 }
1070
Chris Lattnerb21ba712010-02-25 02:04:40 +00001071 static inline bool classof(const Matcher *N) {
Chris Lattner02f73582010-02-24 05:33:42 +00001072 return N->getKind() == MarkFlagResults;
1073 }
1074
Chris Lattner58aa8342010-02-25 06:49:58 +00001075private:
Chris Lattnera5028a62010-02-25 06:53:39 +00001076 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner58aa8342010-02-25 06:49:58 +00001077 virtual bool isEqualImpl(const Matcher *M) const {
1078 return cast<MarkFlagResultsMatcher>(M)->FlagResultNodes == FlagResultNodes;
1079 }
1080 virtual unsigned getHashImpl() const;
Chris Lattner02f73582010-02-24 05:33:42 +00001081};
1082
Chris Lattnerb21ba712010-02-25 02:04:40 +00001083/// CompleteMatchMatcher - Complete a match by replacing the results of the
Chris Lattner77f2e272010-02-21 06:03:07 +00001084/// pattern with the newly generated nodes. This also prints a comment
1085/// indicating the source and dest patterns.
Chris Lattnerb21ba712010-02-25 02:04:40 +00001086class CompleteMatchMatcher : public Matcher {
Chris Lattner77f2e272010-02-21 06:03:07 +00001087 SmallVector<unsigned, 2> Results;
Chris Lattner8e946be2010-02-21 03:22:59 +00001088 const PatternToMatch &Pattern;
1089public:
Chris Lattnerb21ba712010-02-25 02:04:40 +00001090 CompleteMatchMatcher(const unsigned *results, unsigned numresults,
Chris Lattnerc78f2a32010-02-28 20:49:53 +00001091 const PatternToMatch &pattern)
Chris Lattnerb21ba712010-02-25 02:04:40 +00001092 : Matcher(CompleteMatch), Results(results, results+numresults),
Chris Lattner77f2e272010-02-21 06:03:07 +00001093 Pattern(pattern) {}
1094
1095 unsigned getNumResults() const { return Results.size(); }
1096 unsigned getResult(unsigned R) const { return Results[R]; }
Chris Lattner8e946be2010-02-21 03:22:59 +00001097 const PatternToMatch &getPattern() const { return Pattern; }
1098
Chris Lattnerb21ba712010-02-25 02:04:40 +00001099 static inline bool classof(const Matcher *N) {
Chris Lattner77f2e272010-02-21 06:03:07 +00001100 return N->getKind() == CompleteMatch;
Chris Lattner8e946be2010-02-21 03:22:59 +00001101 }
1102
Chris Lattner58aa8342010-02-25 06:49:58 +00001103private:
Chris Lattnera5028a62010-02-25 06:53:39 +00001104 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner58aa8342010-02-25 06:49:58 +00001105 virtual bool isEqualImpl(const Matcher *M) const {
1106 return cast<CompleteMatchMatcher>(M)->Results == Results &&
1107 &cast<CompleteMatchMatcher>(M)->Pattern == &Pattern;
1108 }
1109 virtual unsigned getHashImpl() const;
Chris Lattner8e946be2010-02-21 03:22:59 +00001110};
Chris Lattnere86097a2010-02-28 02:31:26 +00001111
Chris Lattnerda272d12010-02-15 08:04:42 +00001112} // end namespace llvm
1113
1114#endif