blob: ef7ecf400505d3ded49edeb93b563767431a07a2 [file] [log] [blame]
Chris Lattnere7d6e3c2010-02-15 08:04:42 +00001//===- DAGISelMatcher.h - Representation of DAG pattern matcher -----------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef TBLGEN_DAGISELMATCHER_H
11#define TBLGEN_DAGISELMATCHER_H
12
Chris Lattnerfdbdc8c2010-02-16 07:21:10 +000013#include "llvm/CodeGen/ValueTypes.h"
Chris Lattnere7d6e3c2010-02-15 08:04:42 +000014#include "llvm/ADT/OwningPtr.h"
15#include "llvm/ADT/StringRef.h"
Chris Lattner3163ca52010-02-21 03:22:59 +000016#include "llvm/ADT/SmallVector.h"
Chris Lattnerfdbdc8c2010-02-16 07:21:10 +000017#include "llvm/Support/Casting.h"
Chris Lattnere7d6e3c2010-02-15 08:04:42 +000018
19namespace llvm {
20 class CodeGenDAGPatterns;
Chris Lattner9a515172010-02-25 02:04:40 +000021 class Matcher;
Chris Lattnere7d6e3c2010-02-15 08:04:42 +000022 class PatternToMatch;
23 class raw_ostream;
24 class ComplexPattern;
Chris Lattnerddfb5222010-02-18 22:03:03 +000025 class Record;
Chris Lattnere9f720b2010-02-27 21:48:43 +000026 class SDNodeInfo;
Chris Lattnere7d6e3c2010-02-15 08:04:42 +000027
Chris Lattner220b9682010-03-01 07:17:40 +000028Matcher *ConvertPatternToMatcher(const PatternToMatch &Pattern,unsigned Variant,
Chris Lattner9a515172010-02-25 02:04:40 +000029 const CodeGenDAGPatterns &CGP);
Chris Lattner5cc7a832010-02-28 20:49:53 +000030Matcher *OptimizeMatcher(Matcher *Matcher, const CodeGenDAGPatterns &CGP);
Chris Lattner514d21f2010-03-01 01:54:19 +000031void EmitMatcherTable(const Matcher *Matcher, const CodeGenDAGPatterns &CGP,
32 raw_ostream &OS);
Chris Lattnere7d6e3c2010-02-15 08:04:42 +000033
34
Chris Lattner9a515172010-02-25 02:04:40 +000035/// Matcher - Base class for all the the DAG ISel Matcher representation
Chris Lattnere7d6e3c2010-02-15 08:04:42 +000036/// nodes.
Chris Lattner9a515172010-02-25 02:04:40 +000037class Matcher {
Chris Lattner78039ec2010-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 Lattner9a515172010-02-25 02:04:40 +000040 OwningPtr<Matcher> Next;
Chris Lattnere7d6e3c2010-02-15 08:04:42 +000041public:
42 enum KindTy {
Chris Lattner3163ca52010-02-21 03:22:59 +000043 // Matcher state manipulation.
Chris Lattnerac10e4f2010-02-25 01:56:48 +000044 Scope, // Push a checking scope.
Chris Lattner3163ca52010-02-21 03:22:59 +000045 RecordNode, // Record the current node.
Chris Lattner3ee1bc42010-02-24 07:31:45 +000046 RecordChild, // Record a child of the current node.
Chris Lattner3163ca52010-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 Lattnere7d6e3c2010-02-15 08:04:42 +000051
Chris Lattnerddfb5222010-02-18 22:03:03 +000052 // Predicate checking.
Chris Lattner3163ca52010-02-21 03:22:59 +000053 CheckSame, // Fail if not same as prev match.
Chris Lattnere7d6e3c2010-02-15 08:04:42 +000054 CheckPatternPredicate,
Chris Lattner3163ca52010-02-21 03:22:59 +000055 CheckPredicate, // Fail if node predicate fails.
56 CheckOpcode, // Fail if not opcode.
Chris Lattner2e9e6842010-03-01 06:59:22 +000057 SwitchOpcode, // Dispatch based on opcode.
Chris Lattner3163ca52010-02-21 03:22:59 +000058 CheckType, // Fail if not correct type.
Chris Lattnerc4188ac2010-03-03 06:28:15 +000059 SwitchType, // Dispatch based on type.
Chris Lattner3c664b32010-02-24 20:15:25 +000060 CheckChildType, // Fail if child has wrong type.
Chris Lattner3163ca52010-02-21 03:22:59 +000061 CheckInteger, // Fail if wrong val.
62 CheckCondCode, // Fail if not condcode.
Chris Lattnere7d6e3c2010-02-15 08:04:42 +000063 CheckValueType,
64 CheckComplexPat,
65 CheckAndImm,
Chris Lattner9de39482010-02-16 06:10:58 +000066 CheckOrImm,
Chris Lattner283adcb2010-02-17 06:23:39 +000067 CheckFoldableChainNode,
Chris Lattnerddfb5222010-02-18 22:03:03 +000068
69 // Node creation/emisssion.
Chris Lattner3163ca52010-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 Lattner69f60c82010-02-24 05:33:42 +000078 MarkFlagResults, // Indicate which interior nodes have flag results.
Chris Lattner7a1dab42010-02-28 02:31:26 +000079 CompleteMatch, // Finish a match and update the results.
Chris Lattner7dae4af2010-02-28 20:55:18 +000080 MorphNodeTo // Build a node, finish a match and update results.
Chris Lattnere7d6e3c2010-02-15 08:04:42 +000081 };
82 const KindTy Kind;
Chris Lattner3b31c982010-02-18 02:49:24 +000083
Chris Lattnere7d6e3c2010-02-15 08:04:42 +000084protected:
Chris Lattner9a515172010-02-25 02:04:40 +000085 Matcher(KindTy K) : Kind(K) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +000086public:
Chris Lattner9a515172010-02-25 02:04:40 +000087 virtual ~Matcher() {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +000088
89 KindTy getKind() const { return Kind; }
Chris Lattner3b31c982010-02-18 02:49:24 +000090
Chris Lattner9a515172010-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 Lattner3ee1bc42010-02-24 07:31:45 +000095
Chris Lattner9a515172010-02-25 02:04:40 +000096 OwningPtr<Matcher> &getNextPtr() { return Next; }
Chris Lattnere7d6e3c2010-02-15 08:04:42 +000097
Chris Lattner9a515172010-02-25 02:04:40 +000098 static inline bool classof(const Matcher *) { return true; }
Chris Lattnere7d6e3c2010-02-15 08:04:42 +000099
Chris Lattner136ab782010-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 Lattner54ee7362010-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 Lattner136ab782010-02-25 06:49:58 +0000108 }
109
Chris Lattnerf4950d02010-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 Lattnerc7db6e62010-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 Lattnerddfd5ab2010-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 Lattner392b1bf2010-02-25 06:53:39 +0000171 void print(raw_ostream &OS, unsigned indent = 0) const;
Chris Lattnerddfd5ab2010-02-27 07:49:13 +0000172 void printOne(raw_ostream &OS) const;
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000173 void dump() const;
Chris Lattner3b31c982010-02-18 02:49:24 +0000174protected:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000175 virtual void printImpl(raw_ostream &OS, unsigned indent) const = 0;
Chris Lattner136ab782010-02-25 06:49:58 +0000176 virtual bool isEqualImpl(const Matcher *M) const = 0;
177 virtual unsigned getHashImpl() const = 0;
Chris Lattnerddfd5ab2010-02-27 07:49:13 +0000178 virtual bool isContradictoryImpl(const Matcher *M) const { return false; }
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000179};
180
Chris Lattner42363662010-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 Lattner9a515172010-02-25 02:04:40 +0000184class ScopeMatcher : public Matcher {
Chris Lattner42363662010-02-25 19:00:39 +0000185 SmallVector<Matcher*, 4> Children;
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000186public:
Chris Lattner42363662010-02-25 19:00:39 +0000187 ScopeMatcher(Matcher *const *children, unsigned numchildren)
188 : Matcher(Scope), Children(children, children+numchildren) {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000189 }
Chris Lattner42363662010-02-25 19:00:39 +0000190 virtual ~ScopeMatcher();
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000191
Chris Lattner42363662010-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 Lattner54ee7362010-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 Lattnere7d6e3c2010-02-15 08:04:42 +0000216
Chris Lattner9a515172010-02-25 02:04:40 +0000217 static inline bool classof(const Matcher *N) {
Chris Lattnerac10e4f2010-02-25 01:56:48 +0000218 return N->getKind() == Scope;
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000219 }
220
Chris Lattner136ab782010-02-25 06:49:58 +0000221private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000222 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000223 virtual bool isEqualImpl(const Matcher *M) const { return false; }
Chris Lattner42363662010-02-25 19:00:39 +0000224 virtual unsigned getHashImpl() const { return 12312; }
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000225};
226
Chris Lattner9a515172010-02-25 02:04:40 +0000227/// RecordMatcher - Save the current node in the operand list.
228class RecordMatcher : public Matcher {
Chris Lattner56cf88d2010-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 Lattner5377f912010-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 Lattnere7d6e3c2010-02-15 08:04:42 +0000236public:
Chris Lattner5377f912010-03-01 02:24:17 +0000237 RecordMatcher(const std::string &whatfor, unsigned resultNo)
238 : Matcher(RecordNode), WhatFor(whatfor), ResultNo(resultNo) {}
Chris Lattner56cf88d2010-02-17 01:03:09 +0000239
Chris Lattner086ca522010-02-17 01:27:29 +0000240 const std::string &getWhatFor() const { return WhatFor; }
Chris Lattner5377f912010-03-01 02:24:17 +0000241 unsigned getResultNo() const { return ResultNo; }
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000242
Chris Lattner9a515172010-02-25 02:04:40 +0000243 static inline bool classof(const Matcher *N) {
Chris Lattnerddfb5222010-02-18 22:03:03 +0000244 return N->getKind() == RecordNode;
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000245 }
246
Chris Lattnerf4950d02010-02-27 06:22:57 +0000247 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
Chris Lattner136ab782010-02-25 06:49:58 +0000248private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000249 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000250 virtual bool isEqualImpl(const Matcher *M) const { return true; }
251 virtual unsigned getHashImpl() const { return 0; }
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000252};
253
Chris Lattner9a515172010-02-25 02:04:40 +0000254/// RecordChildMatcher - Save a numbered child of the current node, or fail
Chris Lattner3ee1bc42010-02-24 07:31:45 +0000255/// the match if it doesn't exist. This is logically equivalent to:
256/// MoveChild N + RecordNode + MoveParent.
Chris Lattner9a515172010-02-25 02:04:40 +0000257class RecordChildMatcher : public Matcher {
Chris Lattner3ee1bc42010-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 Lattner5377f912010-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 Lattner3ee1bc42010-02-24 07:31:45 +0000267public:
Chris Lattner5377f912010-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 Lattner3ee1bc42010-02-24 07:31:45 +0000272
273 unsigned getChildNo() const { return ChildNo; }
274 const std::string &getWhatFor() const { return WhatFor; }
Chris Lattner5377f912010-03-01 02:24:17 +0000275 unsigned getResultNo() const { return ResultNo; }
276
Chris Lattner9a515172010-02-25 02:04:40 +0000277 static inline bool classof(const Matcher *N) {
Chris Lattner3ee1bc42010-02-24 07:31:45 +0000278 return N->getKind() == RecordChild;
279 }
280
Chris Lattnerf4950d02010-02-27 06:22:57 +0000281 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
282
Chris Lattner136ab782010-02-25 06:49:58 +0000283private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000284 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-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 Lattner3ee1bc42010-02-24 07:31:45 +0000289};
290
Chris Lattner9a515172010-02-25 02:04:40 +0000291/// RecordMemRefMatcher - Save the current node's memref.
292class RecordMemRefMatcher : public Matcher {
Chris Lattner3163ca52010-02-21 03:22:59 +0000293public:
Chris Lattner9a515172010-02-25 02:04:40 +0000294 RecordMemRefMatcher() : Matcher(RecordMemRef) {}
Chris Lattner3163ca52010-02-21 03:22:59 +0000295
Chris Lattner9a515172010-02-25 02:04:40 +0000296 static inline bool classof(const Matcher *N) {
Chris Lattner3163ca52010-02-21 03:22:59 +0000297 return N->getKind() == RecordMemRef;
298 }
299
Chris Lattnerf4950d02010-02-27 06:22:57 +0000300 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
301
Chris Lattner136ab782010-02-25 06:49:58 +0000302private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000303 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000304 virtual bool isEqualImpl(const Matcher *M) const { return true; }
305 virtual unsigned getHashImpl() const { return 0; }
Chris Lattner3163ca52010-02-21 03:22:59 +0000306};
307
308
Chris Lattner9a515172010-02-25 02:04:40 +0000309/// CaptureFlagInputMatcher - If the current record has a flag input, record
Chris Lattner3163ca52010-02-21 03:22:59 +0000310/// it so that it is used as an input to the generated code.
Chris Lattner9a515172010-02-25 02:04:40 +0000311class CaptureFlagInputMatcher : public Matcher {
Chris Lattner3163ca52010-02-21 03:22:59 +0000312public:
Chris Lattner9a515172010-02-25 02:04:40 +0000313 CaptureFlagInputMatcher() : Matcher(CaptureFlagInput) {}
Chris Lattner3163ca52010-02-21 03:22:59 +0000314
Chris Lattner9a515172010-02-25 02:04:40 +0000315 static inline bool classof(const Matcher *N) {
Chris Lattner3163ca52010-02-21 03:22:59 +0000316 return N->getKind() == CaptureFlagInput;
317 }
318
Chris Lattnerf4950d02010-02-27 06:22:57 +0000319 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
320
Chris Lattner136ab782010-02-25 06:49:58 +0000321private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000322 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000323 virtual bool isEqualImpl(const Matcher *M) const { return true; }
324 virtual unsigned getHashImpl() const { return 0; }
Chris Lattner3163ca52010-02-21 03:22:59 +0000325};
326
Chris Lattner9a515172010-02-25 02:04:40 +0000327/// MoveChildMatcher - This tells the interpreter to move into the
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000328/// specified child node.
Chris Lattner9a515172010-02-25 02:04:40 +0000329class MoveChildMatcher : public Matcher {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000330 unsigned ChildNo;
331public:
Chris Lattner9a515172010-02-25 02:04:40 +0000332 MoveChildMatcher(unsigned childNo) : Matcher(MoveChild), ChildNo(childNo) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000333
334 unsigned getChildNo() const { return ChildNo; }
335
Chris Lattner9a515172010-02-25 02:04:40 +0000336 static inline bool classof(const Matcher *N) {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000337 return N->getKind() == MoveChild;
338 }
339
Chris Lattnerf4950d02010-02-27 06:22:57 +0000340 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
341
Chris Lattner136ab782010-02-25 06:49:58 +0000342private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000343 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-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 Lattnere7d6e3c2010-02-15 08:04:42 +0000348};
349
Chris Lattner9a515172010-02-25 02:04:40 +0000350/// MoveParentMatcher - This tells the interpreter to move to the parent
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000351/// of the current node.
Chris Lattner9a515172010-02-25 02:04:40 +0000352class MoveParentMatcher : public Matcher {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000353public:
Chris Lattner9a515172010-02-25 02:04:40 +0000354 MoveParentMatcher() : Matcher(MoveParent) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000355
Chris Lattner9a515172010-02-25 02:04:40 +0000356 static inline bool classof(const Matcher *N) {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000357 return N->getKind() == MoveParent;
358 }
359
Chris Lattnerf4950d02010-02-27 06:22:57 +0000360 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
361
Chris Lattner136ab782010-02-25 06:49:58 +0000362private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000363 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000364 virtual bool isEqualImpl(const Matcher *M) const { return true; }
365 virtual unsigned getHashImpl() const { return 0; }
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000366};
367
Chris Lattner9a515172010-02-25 02:04:40 +0000368/// CheckSameMatcher - This checks to see if this node is exactly the same
Chris Lattnere7d6e3c2010-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 Lattner9a515172010-02-25 02:04:40 +0000371class CheckSameMatcher : public Matcher {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000372 unsigned MatchNumber;
373public:
Chris Lattner9a515172010-02-25 02:04:40 +0000374 CheckSameMatcher(unsigned matchnumber)
Chris Lattner136ab782010-02-25 06:49:58 +0000375 : Matcher(CheckSame), MatchNumber(matchnumber) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000376
377 unsigned getMatchNumber() const { return MatchNumber; }
378
Chris Lattner9a515172010-02-25 02:04:40 +0000379 static inline bool classof(const Matcher *N) {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000380 return N->getKind() == CheckSame;
381 }
382
Chris Lattnerf4950d02010-02-27 06:22:57 +0000383 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
384
Chris Lattner136ab782010-02-25 06:49:58 +0000385private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000386 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-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 Lattnere7d6e3c2010-02-15 08:04:42 +0000391};
392
Chris Lattner9a515172010-02-25 02:04:40 +0000393/// CheckPatternPredicateMatcher - This checks the target-specific predicate
Chris Lattnere7d6e3c2010-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 Lattner9a515172010-02-25 02:04:40 +0000396class CheckPatternPredicateMatcher : public Matcher {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000397 std::string Predicate;
398public:
Chris Lattner9a515172010-02-25 02:04:40 +0000399 CheckPatternPredicateMatcher(StringRef predicate)
Chris Lattner136ab782010-02-25 06:49:58 +0000400 : Matcher(CheckPatternPredicate), Predicate(predicate) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000401
402 StringRef getPredicate() const { return Predicate; }
403
Chris Lattner9a515172010-02-25 02:04:40 +0000404 static inline bool classof(const Matcher *N) {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000405 return N->getKind() == CheckPatternPredicate;
406 }
407
Chris Lattnerf4950d02010-02-27 06:22:57 +0000408 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
409
Chris Lattner136ab782010-02-25 06:49:58 +0000410private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000411 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-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 Lattnere7d6e3c2010-02-15 08:04:42 +0000416};
417
Chris Lattner9a515172010-02-25 02:04:40 +0000418/// CheckPredicateMatcher - This checks the target-specific predicate to
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000419/// see if the node is acceptable.
Chris Lattner9a515172010-02-25 02:04:40 +0000420class CheckPredicateMatcher : public Matcher {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000421 StringRef PredName;
422public:
Chris Lattner9a515172010-02-25 02:04:40 +0000423 CheckPredicateMatcher(StringRef predname)
424 : Matcher(CheckPredicate), PredName(predname) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000425
426 StringRef getPredicateName() const { return PredName; }
427
Chris Lattner9a515172010-02-25 02:04:40 +0000428 static inline bool classof(const Matcher *N) {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000429 return N->getKind() == CheckPredicate;
430 }
431
Chris Lattnerf4950d02010-02-27 06:22:57 +0000432 // TODO: Ok?
433 //virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
434
Chris Lattner136ab782010-02-25 06:49:58 +0000435private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000436 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-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 Lattnere7d6e3c2010-02-15 08:04:42 +0000441};
442
443
Chris Lattner9a515172010-02-25 02:04:40 +0000444/// CheckOpcodeMatcher - This checks to see if the current node has the
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000445/// specified opcode, if not it fails to match.
Chris Lattner9a515172010-02-25 02:04:40 +0000446class CheckOpcodeMatcher : public Matcher {
Chris Lattnere9f720b2010-02-27 21:48:43 +0000447 const SDNodeInfo &Opcode;
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000448public:
Chris Lattnere9f720b2010-02-27 21:48:43 +0000449 CheckOpcodeMatcher(const SDNodeInfo &opcode)
450 : Matcher(CheckOpcode), Opcode(opcode) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000451
Chris Lattnere9f720b2010-02-27 21:48:43 +0000452 const SDNodeInfo &getOpcode() const { return Opcode; }
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000453
Chris Lattner9a515172010-02-25 02:04:40 +0000454 static inline bool classof(const Matcher *N) {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000455 return N->getKind() == CheckOpcode;
456 }
457
Chris Lattnerf4950d02010-02-27 06:22:57 +0000458 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
459
Chris Lattner136ab782010-02-25 06:49:58 +0000460private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000461 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner2e9e6842010-03-01 06:59:22 +0000462 virtual bool isEqualImpl(const Matcher *M) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000463 virtual unsigned getHashImpl() const;
Chris Lattnerddfd5ab2010-02-27 07:49:13 +0000464 virtual bool isContradictoryImpl(const Matcher *M) const;
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000465};
Chris Lattner2e9e6842010-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 Lattnere7d6e3c2010-02-15 08:04:42 +0000493
Chris Lattner9a515172010-02-25 02:04:40 +0000494/// CheckTypeMatcher - This checks to see if the current node has the
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000495/// specified type, if not it fails to match.
Chris Lattner9a515172010-02-25 02:04:40 +0000496class CheckTypeMatcher : public Matcher {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000497 MVT::SimpleValueType Type;
498public:
Chris Lattner9a515172010-02-25 02:04:40 +0000499 CheckTypeMatcher(MVT::SimpleValueType type)
500 : Matcher(CheckType), Type(type) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000501
502 MVT::SimpleValueType getType() const { return Type; }
503
Chris Lattner9a515172010-02-25 02:04:40 +0000504 static inline bool classof(const Matcher *N) {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000505 return N->getKind() == CheckType;
506 }
507
Chris Lattnerf4950d02010-02-27 06:22:57 +0000508 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
509
Chris Lattner136ab782010-02-25 06:49:58 +0000510private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000511 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000512 virtual bool isEqualImpl(const Matcher *M) const {
Chris Lattner2d0e0792010-02-26 08:05:36 +0000513 return cast<CheckTypeMatcher>(M)->Type == Type;
Chris Lattner136ab782010-02-25 06:49:58 +0000514 }
515 virtual unsigned getHashImpl() const { return Type; }
Chris Lattnerddfd5ab2010-02-27 07:49:13 +0000516 virtual bool isContradictoryImpl(const Matcher *M) const;
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000517};
Chris Lattner3c664b32010-02-24 20:15:25 +0000518
Chris Lattnerc4188ac2010-03-03 06:28:15 +0000519/// SwitchTypeMatcher - Switch based on the current node's type, dispatching
520/// to one matcher per case. If the type doesn't match any of the cases,
521/// then the match fails. This is semantically equivalent to a Scope node where
522/// every child does a CheckType, but is much faster.
523class SwitchTypeMatcher : public Matcher {
524 SmallVector<std::pair<MVT::SimpleValueType, Matcher*>, 8> Cases;
525public:
526 SwitchTypeMatcher(const std::pair<MVT::SimpleValueType, Matcher*> *cases,
527 unsigned numcases)
528 : Matcher(SwitchType), Cases(cases, cases+numcases) {}
529
530 static inline bool classof(const Matcher *N) {
531 return N->getKind() == SwitchType;
532 }
533
534 unsigned getNumCases() const { return Cases.size(); }
535
536 MVT::SimpleValueType getCaseType(unsigned i) const { return Cases[i].first; }
537 Matcher *getCaseMatcher(unsigned i) { return Cases[i].second; }
538 const Matcher *getCaseMatcher(unsigned i) const { return Cases[i].second; }
539
540private:
541 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
542 virtual bool isEqualImpl(const Matcher *M) const { return false; }
543 virtual unsigned getHashImpl() const { return 4123; }
544};
545
546
Chris Lattner9a515172010-02-25 02:04:40 +0000547/// CheckChildTypeMatcher - This checks to see if a child node has the
Chris Lattner3c664b32010-02-24 20:15:25 +0000548/// specified type, if not it fails to match.
Chris Lattner9a515172010-02-25 02:04:40 +0000549class CheckChildTypeMatcher : public Matcher {
Chris Lattner3c664b32010-02-24 20:15:25 +0000550 unsigned ChildNo;
551 MVT::SimpleValueType Type;
552public:
Chris Lattner9a515172010-02-25 02:04:40 +0000553 CheckChildTypeMatcher(unsigned childno, MVT::SimpleValueType type)
554 : Matcher(CheckChildType), ChildNo(childno), Type(type) {}
Chris Lattner3c664b32010-02-24 20:15:25 +0000555
556 unsigned getChildNo() const { return ChildNo; }
557 MVT::SimpleValueType getType() const { return Type; }
558
Chris Lattner9a515172010-02-25 02:04:40 +0000559 static inline bool classof(const Matcher *N) {
Chris Lattner3c664b32010-02-24 20:15:25 +0000560 return N->getKind() == CheckChildType;
561 }
562
Chris Lattnerf4950d02010-02-27 06:22:57 +0000563 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
564
Chris Lattner136ab782010-02-25 06:49:58 +0000565private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000566 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000567 virtual bool isEqualImpl(const Matcher *M) const {
568 return cast<CheckChildTypeMatcher>(M)->ChildNo == ChildNo &&
569 cast<CheckChildTypeMatcher>(M)->Type == Type;
570 }
571 virtual unsigned getHashImpl() const { return (Type << 3) | ChildNo; }
Chris Lattnerddfd5ab2010-02-27 07:49:13 +0000572 virtual bool isContradictoryImpl(const Matcher *M) const;
Chris Lattner3c664b32010-02-24 20:15:25 +0000573};
574
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000575
Chris Lattner9a515172010-02-25 02:04:40 +0000576/// CheckIntegerMatcher - This checks to see if the current node is a
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000577/// ConstantSDNode with the specified integer value, if not it fails to match.
Chris Lattner9a515172010-02-25 02:04:40 +0000578class CheckIntegerMatcher : public Matcher {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000579 int64_t Value;
580public:
Chris Lattner9a515172010-02-25 02:04:40 +0000581 CheckIntegerMatcher(int64_t value)
582 : Matcher(CheckInteger), Value(value) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000583
584 int64_t getValue() const { return Value; }
585
Chris Lattner9a515172010-02-25 02:04:40 +0000586 static inline bool classof(const Matcher *N) {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000587 return N->getKind() == CheckInteger;
588 }
589
Chris Lattnerf4950d02010-02-27 06:22:57 +0000590 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
591
Chris Lattner136ab782010-02-25 06:49:58 +0000592private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000593 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000594 virtual bool isEqualImpl(const Matcher *M) const {
595 return cast<CheckIntegerMatcher>(M)->Value == Value;
596 }
597 virtual unsigned getHashImpl() const { return Value; }
Chris Lattner086af322010-02-27 08:11:15 +0000598 virtual bool isContradictoryImpl(const Matcher *M) const;
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000599};
600
Chris Lattner9a515172010-02-25 02:04:40 +0000601/// CheckCondCodeMatcher - This checks to see if the current node is a
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000602/// CondCodeSDNode with the specified condition, if not it fails to match.
Chris Lattner9a515172010-02-25 02:04:40 +0000603class CheckCondCodeMatcher : public Matcher {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000604 StringRef CondCodeName;
605public:
Chris Lattner9a515172010-02-25 02:04:40 +0000606 CheckCondCodeMatcher(StringRef condcodename)
607 : Matcher(CheckCondCode), CondCodeName(condcodename) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000608
609 StringRef getCondCodeName() const { return CondCodeName; }
610
Chris Lattner9a515172010-02-25 02:04:40 +0000611 static inline bool classof(const Matcher *N) {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000612 return N->getKind() == CheckCondCode;
613 }
614
Chris Lattnerf4950d02010-02-27 06:22:57 +0000615 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
616
Chris Lattner136ab782010-02-25 06:49:58 +0000617private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000618 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000619 virtual bool isEqualImpl(const Matcher *M) const {
620 return cast<CheckCondCodeMatcher>(M)->CondCodeName == CondCodeName;
621 }
622 virtual unsigned getHashImpl() const;
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000623};
624
Chris Lattner9a515172010-02-25 02:04:40 +0000625/// CheckValueTypeMatcher - This checks to see if the current node is a
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000626/// VTSDNode with the specified type, if not it fails to match.
Chris Lattner9a515172010-02-25 02:04:40 +0000627class CheckValueTypeMatcher : public Matcher {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000628 StringRef TypeName;
629public:
Chris Lattner9a515172010-02-25 02:04:40 +0000630 CheckValueTypeMatcher(StringRef type_name)
631 : Matcher(CheckValueType), TypeName(type_name) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000632
633 StringRef getTypeName() const { return TypeName; }
634
Chris Lattner9a515172010-02-25 02:04:40 +0000635 static inline bool classof(const Matcher *N) {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000636 return N->getKind() == CheckValueType;
637 }
638
Chris Lattnerf4950d02010-02-27 06:22:57 +0000639 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
640
Chris Lattner136ab782010-02-25 06:49:58 +0000641private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000642 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000643 virtual bool isEqualImpl(const Matcher *M) const {
644 return cast<CheckValueTypeMatcher>(M)->TypeName == TypeName;
645 }
646 virtual unsigned getHashImpl() const;
Chris Lattnerc7db6e62010-03-07 06:29:26 +0000647 bool isContradictoryImpl(const Matcher *M) const;
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000648};
649
650
651
Chris Lattner9a515172010-02-25 02:04:40 +0000652/// CheckComplexPatMatcher - This node runs the specified ComplexPattern on
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000653/// the current node.
Chris Lattner9a515172010-02-25 02:04:40 +0000654class CheckComplexPatMatcher : public Matcher {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000655 const ComplexPattern &Pattern;
Chris Lattner609472b2010-03-04 01:23:08 +0000656
657 /// MatchNumber - This is the recorded nodes slot that contains the node we want to
658 /// match against.
659 unsigned MatchNumber;
660
661 /// Name - The name of the node we're matching, for comment emission.
662 std::string Name;
663
Chris Lattnerdc48d142010-03-04 00:28:05 +0000664 /// FirstResult - This is the first slot in the RecordedNodes list that the
665 /// result of the match populates.
666 unsigned FirstResult;
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000667public:
Chris Lattner609472b2010-03-04 01:23:08 +0000668 CheckComplexPatMatcher(const ComplexPattern &pattern, unsigned matchnumber,
669 const std::string &name, unsigned firstresult)
670 : Matcher(CheckComplexPat), Pattern(pattern), MatchNumber(matchnumber),
671 Name(name), FirstResult(firstresult) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000672
Chris Lattnerdc429fd2010-02-17 00:31:50 +0000673 const ComplexPattern &getPattern() const { return Pattern; }
Chris Lattner609472b2010-03-04 01:23:08 +0000674 unsigned getMatchNumber() const { return MatchNumber; }
675
676 const std::string getName() const { return Name; }
Chris Lattnerdc48d142010-03-04 00:28:05 +0000677 unsigned getFirstResult() const { return FirstResult; }
Chris Lattnerdc429fd2010-02-17 00:31:50 +0000678
Chris Lattner9a515172010-02-25 02:04:40 +0000679 static inline bool classof(const Matcher *N) {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000680 return N->getKind() == CheckComplexPat;
681 }
682
Chris Lattnerf4950d02010-02-27 06:22:57 +0000683 // Not safe to move a pattern predicate past a complex pattern.
684 virtual bool isSafeToReorderWithPatternPredicate() const { return false; }
685
Chris Lattner136ab782010-02-25 06:49:58 +0000686private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000687 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000688 virtual bool isEqualImpl(const Matcher *M) const {
Chris Lattner609472b2010-03-04 01:23:08 +0000689 return &cast<CheckComplexPatMatcher>(M)->Pattern == &Pattern &&
690 cast<CheckComplexPatMatcher>(M)->MatchNumber == MatchNumber;
Chris Lattner136ab782010-02-25 06:49:58 +0000691 }
692 virtual unsigned getHashImpl() const {
Chris Lattner609472b2010-03-04 01:23:08 +0000693 return (unsigned)(intptr_t)&Pattern ^ MatchNumber;
Chris Lattner136ab782010-02-25 06:49:58 +0000694 }
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000695};
696
Chris Lattner9a515172010-02-25 02:04:40 +0000697/// CheckAndImmMatcher - This checks to see if the current node is an 'and'
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000698/// with something equivalent to the specified immediate.
Chris Lattner9a515172010-02-25 02:04:40 +0000699class CheckAndImmMatcher : public Matcher {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000700 int64_t Value;
701public:
Chris Lattner9a515172010-02-25 02:04:40 +0000702 CheckAndImmMatcher(int64_t value)
703 : Matcher(CheckAndImm), Value(value) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000704
705 int64_t getValue() const { return Value; }
706
Chris Lattner9a515172010-02-25 02:04:40 +0000707 static inline bool classof(const Matcher *N) {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000708 return N->getKind() == CheckAndImm;
709 }
710
Chris Lattnerf4950d02010-02-27 06:22:57 +0000711 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
712
Chris Lattner136ab782010-02-25 06:49:58 +0000713private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000714 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000715 virtual bool isEqualImpl(const Matcher *M) const {
716 return cast<CheckAndImmMatcher>(M)->Value == Value;
717 }
718 virtual unsigned getHashImpl() const { return Value; }
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000719};
720
Chris Lattner9a515172010-02-25 02:04:40 +0000721/// CheckOrImmMatcher - This checks to see if the current node is an 'and'
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000722/// with something equivalent to the specified immediate.
Chris Lattner9a515172010-02-25 02:04:40 +0000723class CheckOrImmMatcher : public Matcher {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000724 int64_t Value;
725public:
Chris Lattner9a515172010-02-25 02:04:40 +0000726 CheckOrImmMatcher(int64_t value)
727 : Matcher(CheckOrImm), Value(value) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000728
729 int64_t getValue() const { return Value; }
730
Chris Lattner9a515172010-02-25 02:04:40 +0000731 static inline bool classof(const Matcher *N) {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000732 return N->getKind() == CheckOrImm;
733 }
734
Chris Lattnerf4950d02010-02-27 06:22:57 +0000735 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
736
Chris Lattner136ab782010-02-25 06:49:58 +0000737private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000738 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000739 virtual bool isEqualImpl(const Matcher *M) const {
740 return cast<CheckOrImmMatcher>(M)->Value == Value;
741 }
742 virtual unsigned getHashImpl() const { return Value; }
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000743};
Chris Lattner9de39482010-02-16 06:10:58 +0000744
Chris Lattner9a515172010-02-25 02:04:40 +0000745/// CheckFoldableChainNodeMatcher - This checks to see if the current node
Chris Lattnerc6dc8f62010-02-16 19:15:55 +0000746/// (which defines a chain operand) is safe to fold into a larger pattern.
Chris Lattner9a515172010-02-25 02:04:40 +0000747class CheckFoldableChainNodeMatcher : public Matcher {
Chris Lattner9de39482010-02-16 06:10:58 +0000748public:
Chris Lattner9a515172010-02-25 02:04:40 +0000749 CheckFoldableChainNodeMatcher()
750 : Matcher(CheckFoldableChainNode) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000751
Chris Lattner9a515172010-02-25 02:04:40 +0000752 static inline bool classof(const Matcher *N) {
Chris Lattnerc6dc8f62010-02-16 19:15:55 +0000753 return N->getKind() == CheckFoldableChainNode;
Chris Lattner9de39482010-02-16 06:10:58 +0000754 }
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000755
Chris Lattnerf4950d02010-02-27 06:22:57 +0000756 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
757
Chris Lattner136ab782010-02-25 06:49:58 +0000758private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000759 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000760 virtual bool isEqualImpl(const Matcher *M) const { return true; }
761 virtual unsigned getHashImpl() const { return 0; }
Chris Lattner9de39482010-02-16 06:10:58 +0000762};
763
Chris Lattner9a515172010-02-25 02:04:40 +0000764/// EmitIntegerMatcher - This creates a new TargetConstant.
765class EmitIntegerMatcher : public Matcher {
Chris Lattnerddfb5222010-02-18 22:03:03 +0000766 int64_t Val;
767 MVT::SimpleValueType VT;
768public:
Chris Lattner9a515172010-02-25 02:04:40 +0000769 EmitIntegerMatcher(int64_t val, MVT::SimpleValueType vt)
770 : Matcher(EmitInteger), Val(val), VT(vt) {}
Chris Lattner283adcb2010-02-17 06:23:39 +0000771
Chris Lattner7043e0a2010-02-19 07:49:56 +0000772 int64_t getValue() const { return Val; }
Chris Lattnerddfb5222010-02-18 22:03:03 +0000773 MVT::SimpleValueType getVT() const { return VT; }
774
Chris Lattner9a515172010-02-25 02:04:40 +0000775 static inline bool classof(const Matcher *N) {
Chris Lattnerddfb5222010-02-18 22:03:03 +0000776 return N->getKind() == EmitInteger;
777 }
778
Chris Lattner136ab782010-02-25 06:49:58 +0000779private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000780 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000781 virtual bool isEqualImpl(const Matcher *M) const {
782 return cast<EmitIntegerMatcher>(M)->Val == Val &&
783 cast<EmitIntegerMatcher>(M)->VT == VT;
784 }
785 virtual unsigned getHashImpl() const { return (Val << 4) | VT; }
Chris Lattnerddfb5222010-02-18 22:03:03 +0000786};
Chris Lattner3163ca52010-02-21 03:22:59 +0000787
Chris Lattner9a515172010-02-25 02:04:40 +0000788/// EmitStringIntegerMatcher - A target constant whose value is represented
Chris Lattner3163ca52010-02-21 03:22:59 +0000789/// by a string.
Chris Lattner9a515172010-02-25 02:04:40 +0000790class EmitStringIntegerMatcher : public Matcher {
Chris Lattner3163ca52010-02-21 03:22:59 +0000791 std::string Val;
792 MVT::SimpleValueType VT;
793public:
Chris Lattner9a515172010-02-25 02:04:40 +0000794 EmitStringIntegerMatcher(const std::string &val, MVT::SimpleValueType vt)
795 : Matcher(EmitStringInteger), Val(val), VT(vt) {}
Chris Lattner3163ca52010-02-21 03:22:59 +0000796
797 const std::string &getValue() const { return Val; }
798 MVT::SimpleValueType getVT() const { return VT; }
799
Chris Lattner9a515172010-02-25 02:04:40 +0000800 static inline bool classof(const Matcher *N) {
Chris Lattner3163ca52010-02-21 03:22:59 +0000801 return N->getKind() == EmitStringInteger;
802 }
803
Chris Lattner136ab782010-02-25 06:49:58 +0000804private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000805 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000806 virtual bool isEqualImpl(const Matcher *M) const {
807 return cast<EmitStringIntegerMatcher>(M)->Val == Val &&
808 cast<EmitStringIntegerMatcher>(M)->VT == VT;
809 }
810 virtual unsigned getHashImpl() const;
Chris Lattner3163ca52010-02-21 03:22:59 +0000811};
Chris Lattnerddfb5222010-02-18 22:03:03 +0000812
Chris Lattner9a515172010-02-25 02:04:40 +0000813/// EmitRegisterMatcher - This creates a new TargetConstant.
814class EmitRegisterMatcher : public Matcher {
Chris Lattnerddfb5222010-02-18 22:03:03 +0000815 /// Reg - The def for the register that we're emitting. If this is null, then
816 /// this is a reference to zero_reg.
817 Record *Reg;
818 MVT::SimpleValueType VT;
819public:
Chris Lattner9a515172010-02-25 02:04:40 +0000820 EmitRegisterMatcher(Record *reg, MVT::SimpleValueType vt)
821 : Matcher(EmitRegister), Reg(reg), VT(vt) {}
Chris Lattnerddfb5222010-02-18 22:03:03 +0000822
823 Record *getReg() const { return Reg; }
824 MVT::SimpleValueType getVT() const { return VT; }
825
Chris Lattner9a515172010-02-25 02:04:40 +0000826 static inline bool classof(const Matcher *N) {
Chris Lattnerddfb5222010-02-18 22:03:03 +0000827 return N->getKind() == EmitRegister;
828 }
829
Chris Lattner136ab782010-02-25 06:49:58 +0000830private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000831 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000832 virtual bool isEqualImpl(const Matcher *M) const {
833 return cast<EmitRegisterMatcher>(M)->Reg == Reg &&
834 cast<EmitRegisterMatcher>(M)->VT == VT;
835 }
836 virtual unsigned getHashImpl() const {
837 return ((unsigned)(intptr_t)Reg) << 4 | VT;
838 }
Chris Lattnerddfb5222010-02-18 22:03:03 +0000839};
Chris Lattner3163ca52010-02-21 03:22:59 +0000840
Chris Lattner9a515172010-02-25 02:04:40 +0000841/// EmitConvertToTargetMatcher - Emit an operation that reads a specified
Chris Lattner3163ca52010-02-21 03:22:59 +0000842/// recorded node and converts it from being a ISD::Constant to
843/// ISD::TargetConstant, likewise for ConstantFP.
Chris Lattner9a515172010-02-25 02:04:40 +0000844class EmitConvertToTargetMatcher : public Matcher {
Chris Lattner3163ca52010-02-21 03:22:59 +0000845 unsigned Slot;
846public:
Chris Lattner9a515172010-02-25 02:04:40 +0000847 EmitConvertToTargetMatcher(unsigned slot)
848 : Matcher(EmitConvertToTarget), Slot(slot) {}
Chris Lattner3163ca52010-02-21 03:22:59 +0000849
850 unsigned getSlot() const { return Slot; }
851
Chris Lattner9a515172010-02-25 02:04:40 +0000852 static inline bool classof(const Matcher *N) {
Chris Lattner3163ca52010-02-21 03:22:59 +0000853 return N->getKind() == EmitConvertToTarget;
854 }
855
Chris Lattner136ab782010-02-25 06:49:58 +0000856private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000857 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000858 virtual bool isEqualImpl(const Matcher *M) const {
859 return cast<EmitConvertToTargetMatcher>(M)->Slot == Slot;
860 }
861 virtual unsigned getHashImpl() const { return Slot; }
Chris Lattner3163ca52010-02-21 03:22:59 +0000862};
863
Chris Lattner9a515172010-02-25 02:04:40 +0000864/// EmitMergeInputChainsMatcher - Emit a node that merges a list of input
Chris Lattner3163ca52010-02-21 03:22:59 +0000865/// chains together with a token factor. The list of nodes are the nodes in the
866/// matched pattern that have chain input/outputs. This node adds all input
867/// chains of these nodes if they are not themselves a node in the pattern.
Chris Lattner9a515172010-02-25 02:04:40 +0000868class EmitMergeInputChainsMatcher : public Matcher {
Chris Lattner3163ca52010-02-21 03:22:59 +0000869 SmallVector<unsigned, 3> ChainNodes;
870public:
Chris Lattner9a515172010-02-25 02:04:40 +0000871 EmitMergeInputChainsMatcher(const unsigned *nodes, unsigned NumNodes)
872 : Matcher(EmitMergeInputChains), ChainNodes(nodes, nodes+NumNodes) {}
Chris Lattner3163ca52010-02-21 03:22:59 +0000873
874 unsigned getNumNodes() const { return ChainNodes.size(); }
875
876 unsigned getNode(unsigned i) const {
877 assert(i < ChainNodes.size());
878 return ChainNodes[i];
879 }
880
Chris Lattner9a515172010-02-25 02:04:40 +0000881 static inline bool classof(const Matcher *N) {
Chris Lattner3163ca52010-02-21 03:22:59 +0000882 return N->getKind() == EmitMergeInputChains;
883 }
884
Chris Lattner136ab782010-02-25 06:49:58 +0000885private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000886 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000887 virtual bool isEqualImpl(const Matcher *M) const {
888 return cast<EmitMergeInputChainsMatcher>(M)->ChainNodes == ChainNodes;
889 }
890 virtual unsigned getHashImpl() const;
Chris Lattner3163ca52010-02-21 03:22:59 +0000891};
892
Chris Lattner9a515172010-02-25 02:04:40 +0000893/// EmitCopyToRegMatcher - Emit a CopyToReg node from a value to a physreg,
Chris Lattner3163ca52010-02-21 03:22:59 +0000894/// pushing the chain and flag results.
895///
Chris Lattner9a515172010-02-25 02:04:40 +0000896class EmitCopyToRegMatcher : public Matcher {
Chris Lattner3163ca52010-02-21 03:22:59 +0000897 unsigned SrcSlot; // Value to copy into the physreg.
898 Record *DestPhysReg;
899public:
Chris Lattner9a515172010-02-25 02:04:40 +0000900 EmitCopyToRegMatcher(unsigned srcSlot, Record *destPhysReg)
901 : Matcher(EmitCopyToReg), SrcSlot(srcSlot), DestPhysReg(destPhysReg) {}
Chris Lattner3163ca52010-02-21 03:22:59 +0000902
903 unsigned getSrcSlot() const { return SrcSlot; }
904 Record *getDestPhysReg() const { return DestPhysReg; }
905
Chris Lattner9a515172010-02-25 02:04:40 +0000906 static inline bool classof(const Matcher *N) {
Chris Lattner3163ca52010-02-21 03:22:59 +0000907 return N->getKind() == EmitCopyToReg;
908 }
909
Chris Lattner136ab782010-02-25 06:49:58 +0000910private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000911 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000912 virtual bool isEqualImpl(const Matcher *M) const {
913 return cast<EmitCopyToRegMatcher>(M)->SrcSlot == SrcSlot &&
914 cast<EmitCopyToRegMatcher>(M)->DestPhysReg == DestPhysReg;
915 }
916 virtual unsigned getHashImpl() const {
917 return SrcSlot ^ ((unsigned)(intptr_t)DestPhysReg << 4);
918 }
Chris Lattner3163ca52010-02-21 03:22:59 +0000919};
920
921
922
Chris Lattner9a515172010-02-25 02:04:40 +0000923/// EmitNodeXFormMatcher - Emit an operation that runs an SDNodeXForm on a
Chris Lattner3163ca52010-02-21 03:22:59 +0000924/// recorded node and records the result.
Chris Lattner9a515172010-02-25 02:04:40 +0000925class EmitNodeXFormMatcher : public Matcher {
Chris Lattner3163ca52010-02-21 03:22:59 +0000926 unsigned Slot;
927 Record *NodeXForm;
928public:
Chris Lattner9a515172010-02-25 02:04:40 +0000929 EmitNodeXFormMatcher(unsigned slot, Record *nodeXForm)
930 : Matcher(EmitNodeXForm), Slot(slot), NodeXForm(nodeXForm) {}
Chris Lattner3163ca52010-02-21 03:22:59 +0000931
932 unsigned getSlot() const { return Slot; }
933 Record *getNodeXForm() const { return NodeXForm; }
934
Chris Lattner9a515172010-02-25 02:04:40 +0000935 static inline bool classof(const Matcher *N) {
Chris Lattner3163ca52010-02-21 03:22:59 +0000936 return N->getKind() == EmitNodeXForm;
937 }
938
Chris Lattner136ab782010-02-25 06:49:58 +0000939private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000940 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000941 virtual bool isEqualImpl(const Matcher *M) const {
942 return cast<EmitNodeXFormMatcher>(M)->Slot == Slot &&
943 cast<EmitNodeXFormMatcher>(M)->NodeXForm == NodeXForm;
944 }
945 virtual unsigned getHashImpl() const {
946 return Slot ^ ((unsigned)(intptr_t)NodeXForm << 4);
947 }
Chris Lattner3163ca52010-02-21 03:22:59 +0000948};
Chris Lattnerddfb5222010-02-18 22:03:03 +0000949
Chris Lattner7a1dab42010-02-28 02:31:26 +0000950/// EmitNodeMatcherCommon - Common class shared between EmitNode and
Chris Lattner7dae4af2010-02-28 20:55:18 +0000951/// MorphNodeTo.
Chris Lattner7a1dab42010-02-28 02:31:26 +0000952class EmitNodeMatcherCommon : public Matcher {
Chris Lattner3163ca52010-02-21 03:22:59 +0000953 std::string OpcodeName;
954 const SmallVector<MVT::SimpleValueType, 3> VTs;
955 const SmallVector<unsigned, 6> Operands;
Chris Lattner414bac82010-02-28 21:53:42 +0000956 bool HasChain, HasInFlag, HasOutFlag, HasMemRefs;
Chris Lattnerddfb5222010-02-18 22:03:03 +0000957
Chris Lattner3163ca52010-02-21 03:22:59 +0000958 /// NumFixedArityOperands - If this is a fixed arity node, this is set to -1.
959 /// If this is a varidic node, this is set to the number of fixed arity
960 /// operands in the root of the pattern. The rest are appended to this node.
961 int NumFixedArityOperands;
962public:
Chris Lattner7a1dab42010-02-28 02:31:26 +0000963 EmitNodeMatcherCommon(const std::string &opcodeName,
964 const MVT::SimpleValueType *vts, unsigned numvts,
965 const unsigned *operands, unsigned numops,
Chris Lattner414bac82010-02-28 21:53:42 +0000966 bool hasChain, bool hasInFlag, bool hasOutFlag,
967 bool hasmemrefs,
Chris Lattner7dae4af2010-02-28 20:55:18 +0000968 int numfixedarityoperands, bool isMorphNodeTo)
969 : Matcher(isMorphNodeTo ? MorphNodeTo : EmitNode), OpcodeName(opcodeName),
Chris Lattner3163ca52010-02-21 03:22:59 +0000970 VTs(vts, vts+numvts), Operands(operands, operands+numops),
Chris Lattner414bac82010-02-28 21:53:42 +0000971 HasChain(hasChain), HasInFlag(hasInFlag), HasOutFlag(hasOutFlag),
972 HasMemRefs(hasmemrefs), NumFixedArityOperands(numfixedarityoperands) {}
Chris Lattner3163ca52010-02-21 03:22:59 +0000973
974 const std::string &getOpcodeName() const { return OpcodeName; }
975
976 unsigned getNumVTs() const { return VTs.size(); }
977 MVT::SimpleValueType getVT(unsigned i) const {
978 assert(i < VTs.size());
979 return VTs[i];
980 }
Chris Lattner5cc7a832010-02-28 20:49:53 +0000981
Chris Lattner3163ca52010-02-21 03:22:59 +0000982 unsigned getNumOperands() const { return Operands.size(); }
983 unsigned getOperand(unsigned i) const {
984 assert(i < Operands.size());
985 return Operands[i];
Chris Lattner5cc7a832010-02-28 20:49:53 +0000986 }
987
988 const SmallVectorImpl<MVT::SimpleValueType> &getVTList() const { return VTs; }
989 const SmallVectorImpl<unsigned> &getOperandList() const { return Operands; }
990
Chris Lattner3163ca52010-02-21 03:22:59 +0000991
992 bool hasChain() const { return HasChain; }
Chris Lattner414bac82010-02-28 21:53:42 +0000993 bool hasInFlag() const { return HasInFlag; }
994 bool hasOutFlag() const { return HasOutFlag; }
Chris Lattner3163ca52010-02-21 03:22:59 +0000995 bool hasMemRefs() const { return HasMemRefs; }
996 int getNumFixedArityOperands() const { return NumFixedArityOperands; }
Chris Lattnerddfb5222010-02-18 22:03:03 +0000997
Chris Lattner9a515172010-02-25 02:04:40 +0000998 static inline bool classof(const Matcher *N) {
Chris Lattner7dae4af2010-02-28 20:55:18 +0000999 return N->getKind() == EmitNode || N->getKind() == MorphNodeTo;
Chris Lattnerddfb5222010-02-18 22:03:03 +00001000 }
1001
Chris Lattner136ab782010-02-25 06:49:58 +00001002private:
Chris Lattner392b1bf2010-02-25 06:53:39 +00001003 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +00001004 virtual bool isEqualImpl(const Matcher *M) const;
1005 virtual unsigned getHashImpl() const;
Chris Lattnerddfb5222010-02-18 22:03:03 +00001006};
Chris Lattnerb085ea12010-02-21 06:03:07 +00001007
Chris Lattner7a1dab42010-02-28 02:31:26 +00001008/// EmitNodeMatcher - This signals a successful match and generates a node.
1009class EmitNodeMatcher : public EmitNodeMatcherCommon {
Chris Lattner19a1fbe2010-02-28 02:41:25 +00001010 unsigned FirstResultSlot;
Chris Lattner7a1dab42010-02-28 02:31:26 +00001011public:
1012 EmitNodeMatcher(const std::string &opcodeName,
1013 const MVT::SimpleValueType *vts, unsigned numvts,
1014 const unsigned *operands, unsigned numops,
Chris Lattner414bac82010-02-28 21:53:42 +00001015 bool hasChain, bool hasInFlag, bool hasOutFlag,
1016 bool hasmemrefs,
Chris Lattner19a1fbe2010-02-28 02:41:25 +00001017 int numfixedarityoperands, unsigned firstresultslot)
Chris Lattner7a1dab42010-02-28 02:31:26 +00001018 : EmitNodeMatcherCommon(opcodeName, vts, numvts, operands, numops, hasChain,
Chris Lattner414bac82010-02-28 21:53:42 +00001019 hasInFlag, hasOutFlag, hasmemrefs,
1020 numfixedarityoperands, false),
Chris Lattner19a1fbe2010-02-28 02:41:25 +00001021 FirstResultSlot(firstresultslot) {}
1022
1023 unsigned getFirstResultSlot() const { return FirstResultSlot; }
Chris Lattner7a1dab42010-02-28 02:31:26 +00001024
1025 static inline bool classof(const Matcher *N) {
1026 return N->getKind() == EmitNode;
1027 }
1028
1029};
1030
Chris Lattner7dae4af2010-02-28 20:55:18 +00001031class MorphNodeToMatcher : public EmitNodeMatcherCommon {
Chris Lattner7a1dab42010-02-28 02:31:26 +00001032 const PatternToMatch &Pattern;
1033public:
Chris Lattner7dae4af2010-02-28 20:55:18 +00001034 MorphNodeToMatcher(const std::string &opcodeName,
1035 const MVT::SimpleValueType *vts, unsigned numvts,
1036 const unsigned *operands, unsigned numops,
Chris Lattner414bac82010-02-28 21:53:42 +00001037 bool hasChain, bool hasInFlag, bool hasOutFlag,
1038 bool hasmemrefs,
Chris Lattner7dae4af2010-02-28 20:55:18 +00001039 int numfixedarityoperands, const PatternToMatch &pattern)
Chris Lattner7a1dab42010-02-28 02:31:26 +00001040 : EmitNodeMatcherCommon(opcodeName, vts, numvts, operands, numops, hasChain,
Chris Lattner414bac82010-02-28 21:53:42 +00001041 hasInFlag, hasOutFlag, hasmemrefs,
1042 numfixedarityoperands, true),
Chris Lattner7a1dab42010-02-28 02:31:26 +00001043 Pattern(pattern) {
1044 }
1045
1046 const PatternToMatch &getPattern() const { return Pattern; }
1047
1048 static inline bool classof(const Matcher *N) {
Chris Lattner7dae4af2010-02-28 20:55:18 +00001049 return N->getKind() == MorphNodeTo;
Chris Lattner7a1dab42010-02-28 02:31:26 +00001050 }
1051};
1052
Chris Lattner9a515172010-02-25 02:04:40 +00001053/// MarkFlagResultsMatcher - This node indicates which non-root nodes in the
1054/// pattern produce flags. This allows CompleteMatchMatcher to update them
Chris Lattner69f60c82010-02-24 05:33:42 +00001055/// with the output flag of the resultant code.
Chris Lattner9a515172010-02-25 02:04:40 +00001056class MarkFlagResultsMatcher : public Matcher {
Chris Lattner69f60c82010-02-24 05:33:42 +00001057 SmallVector<unsigned, 3> FlagResultNodes;
1058public:
Chris Lattner9a515172010-02-25 02:04:40 +00001059 MarkFlagResultsMatcher(const unsigned *nodes, unsigned NumNodes)
1060 : Matcher(MarkFlagResults), FlagResultNodes(nodes, nodes+NumNodes) {}
Chris Lattner69f60c82010-02-24 05:33:42 +00001061
1062 unsigned getNumNodes() const { return FlagResultNodes.size(); }
1063
1064 unsigned getNode(unsigned i) const {
1065 assert(i < FlagResultNodes.size());
1066 return FlagResultNodes[i];
1067 }
1068
Chris Lattner9a515172010-02-25 02:04:40 +00001069 static inline bool classof(const Matcher *N) {
Chris Lattner69f60c82010-02-24 05:33:42 +00001070 return N->getKind() == MarkFlagResults;
1071 }
1072
Chris Lattner136ab782010-02-25 06:49:58 +00001073private:
Chris Lattner392b1bf2010-02-25 06:53:39 +00001074 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +00001075 virtual bool isEqualImpl(const Matcher *M) const {
1076 return cast<MarkFlagResultsMatcher>(M)->FlagResultNodes == FlagResultNodes;
1077 }
1078 virtual unsigned getHashImpl() const;
Chris Lattner69f60c82010-02-24 05:33:42 +00001079};
1080
Chris Lattner9a515172010-02-25 02:04:40 +00001081/// CompleteMatchMatcher - Complete a match by replacing the results of the
Chris Lattnerb085ea12010-02-21 06:03:07 +00001082/// pattern with the newly generated nodes. This also prints a comment
1083/// indicating the source and dest patterns.
Chris Lattner9a515172010-02-25 02:04:40 +00001084class CompleteMatchMatcher : public Matcher {
Chris Lattnerb085ea12010-02-21 06:03:07 +00001085 SmallVector<unsigned, 2> Results;
Chris Lattner3163ca52010-02-21 03:22:59 +00001086 const PatternToMatch &Pattern;
1087public:
Chris Lattner9a515172010-02-25 02:04:40 +00001088 CompleteMatchMatcher(const unsigned *results, unsigned numresults,
Chris Lattner5cc7a832010-02-28 20:49:53 +00001089 const PatternToMatch &pattern)
Chris Lattner9a515172010-02-25 02:04:40 +00001090 : Matcher(CompleteMatch), Results(results, results+numresults),
Chris Lattnerb085ea12010-02-21 06:03:07 +00001091 Pattern(pattern) {}
1092
1093 unsigned getNumResults() const { return Results.size(); }
1094 unsigned getResult(unsigned R) const { return Results[R]; }
Chris Lattner3163ca52010-02-21 03:22:59 +00001095 const PatternToMatch &getPattern() const { return Pattern; }
1096
Chris Lattner9a515172010-02-25 02:04:40 +00001097 static inline bool classof(const Matcher *N) {
Chris Lattnerb085ea12010-02-21 06:03:07 +00001098 return N->getKind() == CompleteMatch;
Chris Lattner3163ca52010-02-21 03:22:59 +00001099 }
1100
Chris Lattner136ab782010-02-25 06:49:58 +00001101private:
Chris Lattner392b1bf2010-02-25 06:53:39 +00001102 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +00001103 virtual bool isEqualImpl(const Matcher *M) const {
1104 return cast<CompleteMatchMatcher>(M)->Results == Results &&
1105 &cast<CompleteMatchMatcher>(M)->Pattern == &Pattern;
1106 }
1107 virtual unsigned getHashImpl() const;
Chris Lattner3163ca52010-02-21 03:22:59 +00001108};
Chris Lattner7a1dab42010-02-28 02:31:26 +00001109
Chris Lattnere7d6e3c2010-02-15 08:04:42 +00001110} // end namespace llvm
1111
1112#endif