blob: dcb8da71086eef0dbeb56de5b1b763c97db4a3a6 [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 {
Jim Grosbach4a6d7352011-03-11 02:19:02 +000020 struct CodeGenRegister;
Chris Lattnerda272d12010-02-15 08:04:42 +000021 class CodeGenDAGPatterns;
Chris Lattnerb21ba712010-02-25 02:04:40 +000022 class Matcher;
Chris Lattnerda272d12010-02-15 08:04:42 +000023 class PatternToMatch;
24 class raw_ostream;
25 class ComplexPattern;
Chris Lattner845c0422010-02-18 22:03:03 +000026 class Record;
Chris Lattnera230f962010-02-27 21:48:43 +000027 class SDNodeInfo;
Chris Lattner54379062011-04-17 21:38:24 +000028 class TreePredicateFn;
29 class TreePattern;
Chris Lattnerda272d12010-02-15 08:04:42 +000030
Chris Lattnerfa342fa2010-03-01 07:17:40 +000031Matcher *ConvertPatternToMatcher(const PatternToMatch &Pattern,unsigned Variant,
Chris Lattnerb21ba712010-02-25 02:04:40 +000032 const CodeGenDAGPatterns &CGP);
Chris Lattnerc78f2a32010-02-28 20:49:53 +000033Matcher *OptimizeMatcher(Matcher *Matcher, const CodeGenDAGPatterns &CGP);
Chris Lattner4d0c9312010-03-01 01:54:19 +000034void EmitMatcherTable(const Matcher *Matcher, const CodeGenDAGPatterns &CGP,
Jim Grosbach4a6d7352011-03-11 02:19:02 +000035 raw_ostream &OS);
Chris Lattnerda272d12010-02-15 08:04:42 +000036
Jim Grosbachfbadcd02010-12-21 16:16:00 +000037
Chris Lattnerb21ba712010-02-25 02:04:40 +000038/// Matcher - Base class for all the the DAG ISel Matcher representation
Chris Lattnerda272d12010-02-15 08:04:42 +000039/// nodes.
Chris Lattnerb21ba712010-02-25 02:04:40 +000040class Matcher {
Chris Lattnerbd8227f2010-02-18 02:53:41 +000041 // The next matcher node that is executed after this one. Null if this is the
42 // last stage of a match.
Chris Lattnerb21ba712010-02-25 02:04:40 +000043 OwningPtr<Matcher> Next;
Chris Lattnerda272d12010-02-15 08:04:42 +000044public:
45 enum KindTy {
Chris Lattner8e946be2010-02-21 03:22:59 +000046 // Matcher state manipulation.
Chris Lattner60df53e2010-02-25 01:56:48 +000047 Scope, // Push a checking scope.
Chris Lattner8e946be2010-02-21 03:22:59 +000048 RecordNode, // Record the current node.
Chris Lattner19b5a752010-02-24 07:31:45 +000049 RecordChild, // Record a child of the current node.
Chris Lattner8e946be2010-02-21 03:22:59 +000050 RecordMemRef, // Record the memref in the current node.
Chris Lattner8950bca2010-12-23 17:03:20 +000051 CaptureGlueInput, // If the current node has an input glue, save it.
Chris Lattner8e946be2010-02-21 03:22:59 +000052 MoveChild, // Move current node to specified child.
53 MoveParent, // Move current node to parent.
Jim Grosbachfbadcd02010-12-21 16:16:00 +000054
Chris Lattner845c0422010-02-18 22:03:03 +000055 // Predicate checking.
Chris Lattner8e946be2010-02-21 03:22:59 +000056 CheckSame, // Fail if not same as prev match.
Chris Lattnerda272d12010-02-15 08:04:42 +000057 CheckPatternPredicate,
Chris Lattner8e946be2010-02-21 03:22:59 +000058 CheckPredicate, // Fail if node predicate fails.
59 CheckOpcode, // Fail if not opcode.
Chris Lattnereb669212010-03-01 06:59:22 +000060 SwitchOpcode, // Dispatch based on opcode.
Chris Lattner8e946be2010-02-21 03:22:59 +000061 CheckType, // Fail if not correct type.
Chris Lattnercfe2eab2010-03-03 06:28:15 +000062 SwitchType, // Dispatch based on type.
Chris Lattner23cfda72010-02-24 20:15:25 +000063 CheckChildType, // Fail if child has wrong type.
Chris Lattner8e946be2010-02-21 03:22:59 +000064 CheckInteger, // Fail if wrong val.
65 CheckCondCode, // Fail if not condcode.
Chris Lattnerda272d12010-02-15 08:04:42 +000066 CheckValueType,
67 CheckComplexPat,
68 CheckAndImm,
Chris Lattnere39650a2010-02-16 06:10:58 +000069 CheckOrImm,
Chris Lattner9a747f12010-02-17 06:23:39 +000070 CheckFoldableChainNode,
Jim Grosbachfbadcd02010-12-21 16:16:00 +000071
Chris Lattner845c0422010-02-18 22:03:03 +000072 // Node creation/emisssion.
Chris Lattner8e946be2010-02-21 03:22:59 +000073 EmitInteger, // Create a TargetConstant
74 EmitStringInteger, // Create a TargetConstant from a string.
75 EmitRegister, // Create a register.
76 EmitConvertToTarget, // Convert a imm/fpimm to target imm/fpimm
77 EmitMergeInputChains, // Merge together a chains for an input.
78 EmitCopyToReg, // Emit a copytoreg into a physreg.
79 EmitNode, // Create a DAG node
80 EmitNodeXForm, // Run a SDNodeXForm
Chris Lattner8950bca2010-12-23 17:03:20 +000081 MarkGlueResults, // Indicate which interior nodes have glue results.
Chris Lattnere86097a2010-02-28 02:31:26 +000082 CompleteMatch, // Finish a match and update the results.
Chris Lattner9a215002010-02-28 20:55:18 +000083 MorphNodeTo // Build a node, finish a match and update results.
Chris Lattnerda272d12010-02-15 08:04:42 +000084 };
85 const KindTy Kind;
Chris Lattner8ef9c792010-02-18 02:49:24 +000086
Chris Lattnerda272d12010-02-15 08:04:42 +000087protected:
Chris Lattnerb21ba712010-02-25 02:04:40 +000088 Matcher(KindTy K) : Kind(K) {}
Chris Lattnerda272d12010-02-15 08:04:42 +000089public:
Chris Lattnerb21ba712010-02-25 02:04:40 +000090 virtual ~Matcher() {}
Jim Grosbachfbadcd02010-12-21 16:16:00 +000091
Chris Lattnerda272d12010-02-15 08:04:42 +000092 KindTy getKind() const { return Kind; }
Chris Lattner8ef9c792010-02-18 02:49:24 +000093
Chris Lattnerb21ba712010-02-25 02:04:40 +000094 Matcher *getNext() { return Next.get(); }
95 const Matcher *getNext() const { return Next.get(); }
96 void setNext(Matcher *C) { Next.reset(C); }
97 Matcher *takeNext() { return Next.take(); }
Chris Lattner19b5a752010-02-24 07:31:45 +000098
Chris Lattnerb21ba712010-02-25 02:04:40 +000099 OwningPtr<Matcher> &getNextPtr() { return Next; }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000100
Chris Lattnerb21ba712010-02-25 02:04:40 +0000101 static inline bool classof(const Matcher *) { return true; }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000102
Chris Lattner58aa8342010-02-25 06:49:58 +0000103 bool isEqual(const Matcher *M) const {
104 if (getKind() != M->getKind()) return false;
105 return isEqualImpl(M);
106 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000107
Chris Lattner58aa8342010-02-25 06:49:58 +0000108 unsigned getHash() const {
Chris Lattnerca56fea2010-02-26 07:35:27 +0000109 // Clear the high bit so we don't conflict with tombstones etc.
110 return ((getHashImpl() << 4) ^ getKind()) & (~0U>>1);
Chris Lattner58aa8342010-02-25 06:49:58 +0000111 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000112
Chris Lattnerd323fd42010-02-27 06:22:57 +0000113 /// isSafeToReorderWithPatternPredicate - Return true if it is safe to sink a
114 /// PatternPredicate node past this one.
115 virtual bool isSafeToReorderWithPatternPredicate() const {
116 return false;
117 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000118
Chris Lattner48aa5752010-03-07 06:29:26 +0000119 /// isSimplePredicateNode - Return true if this is a simple predicate that
120 /// operates on the node or its children without potential side effects or a
121 /// change of the current node.
122 bool isSimplePredicateNode() const {
123 switch (getKind()) {
124 default: return false;
125 case CheckSame:
126 case CheckPatternPredicate:
127 case CheckPredicate:
128 case CheckOpcode:
129 case CheckType:
130 case CheckChildType:
131 case CheckInteger:
132 case CheckCondCode:
133 case CheckValueType:
134 case CheckAndImm:
135 case CheckOrImm:
136 case CheckFoldableChainNode:
137 return true;
138 }
139 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000140
Chris Lattner48aa5752010-03-07 06:29:26 +0000141 /// isSimplePredicateOrRecordNode - Return true if this is a record node or
142 /// a simple predicate.
143 bool isSimplePredicateOrRecordNode() const {
144 return isSimplePredicateNode() ||
145 getKind() == RecordNode || getKind() == RecordChild;
146 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000147
Chris Lattner48aa5752010-03-07 06:29:26 +0000148 /// unlinkNode - Unlink the specified node from this chain. If Other == this,
149 /// we unlink the next pointer and return it. Otherwise we unlink Other from
150 /// the list and return this.
151 Matcher *unlinkNode(Matcher *Other);
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000152
Chris Lattner48aa5752010-03-07 06:29:26 +0000153 /// canMoveBefore - Return true if this matcher is the same as Other, or if
154 /// we can move this matcher past all of the nodes in-between Other and this
155 /// node. Other must be equal to or before this.
156 bool canMoveBefore(const Matcher *Other) const;
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000157
Chris Lattner48aa5752010-03-07 06:29:26 +0000158 /// canMoveBefore - Return true if it is safe to move the current matcher
159 /// across the specified one.
160 bool canMoveBeforeNode(const Matcher *Other) const;
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000161
Chris Lattner82781b92010-02-27 07:49:13 +0000162 /// isContradictory - Return true of these two matchers could never match on
163 /// the same node.
164 bool isContradictory(const Matcher *Other) const {
165 // Since this predicate is reflexive, we canonicalize the ordering so that
166 // we always match a node against nodes with kinds that are greater or equal
167 // to them. For example, we'll pass in a CheckType node as an argument to
168 // the CheckOpcode method, not the other way around.
169 if (getKind() < Other->getKind())
170 return isContradictoryImpl(Other);
171 return Other->isContradictoryImpl(this);
172 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000173
Chris Lattnera5028a62010-02-25 06:53:39 +0000174 void print(raw_ostream &OS, unsigned indent = 0) const;
Chris Lattner82781b92010-02-27 07:49:13 +0000175 void printOne(raw_ostream &OS) const;
Chris Lattnerda272d12010-02-15 08:04:42 +0000176 void dump() const;
Chris Lattner8ef9c792010-02-18 02:49:24 +0000177protected:
Chris Lattnera5028a62010-02-25 06:53:39 +0000178 virtual void printImpl(raw_ostream &OS, unsigned indent) const = 0;
Chris Lattner58aa8342010-02-25 06:49:58 +0000179 virtual bool isEqualImpl(const Matcher *M) const = 0;
180 virtual unsigned getHashImpl() const = 0;
Chris Lattner82781b92010-02-27 07:49:13 +0000181 virtual bool isContradictoryImpl(const Matcher *M) const { return false; }
Chris Lattnerda272d12010-02-15 08:04:42 +0000182};
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000183
Chris Lattnerd6c84722010-02-25 19:00:39 +0000184/// ScopeMatcher - This attempts to match each of its children to find the first
185/// one that successfully matches. If one child fails, it tries the next child.
186/// If none of the children match then this check fails. It never has a 'next'.
Chris Lattnerb21ba712010-02-25 02:04:40 +0000187class ScopeMatcher : public Matcher {
Chris Lattnerd6c84722010-02-25 19:00:39 +0000188 SmallVector<Matcher*, 4> Children;
Chris Lattnerda272d12010-02-15 08:04:42 +0000189public:
Chris Lattnerd6c84722010-02-25 19:00:39 +0000190 ScopeMatcher(Matcher *const *children, unsigned numchildren)
191 : Matcher(Scope), Children(children, children+numchildren) {
Chris Lattnerda272d12010-02-15 08:04:42 +0000192 }
Chris Lattnerd6c84722010-02-25 19:00:39 +0000193 virtual ~ScopeMatcher();
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000194
Chris Lattnerd6c84722010-02-25 19:00:39 +0000195 unsigned getNumChildren() const { return Children.size(); }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000196
Chris Lattnerd6c84722010-02-25 19:00:39 +0000197 Matcher *getChild(unsigned i) { return Children[i]; }
198 const Matcher *getChild(unsigned i) const { return Children[i]; }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000199
Chris Lattnerd6c84722010-02-25 19:00:39 +0000200 void resetChild(unsigned i, Matcher *N) {
201 delete Children[i];
202 Children[i] = N;
203 }
204
205 Matcher *takeChild(unsigned i) {
206 Matcher *Res = Children[i];
207 Children[i] = 0;
208 return Res;
209 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000210
Chris Lattnerca56fea2010-02-26 07:35:27 +0000211 void setNumChildren(unsigned NC) {
212 if (NC < Children.size()) {
213 // delete any children we're about to lose pointers to.
214 for (unsigned i = NC, e = Children.size(); i != e; ++i)
215 delete Children[i];
216 }
217 Children.resize(NC);
218 }
Chris Lattnerda272d12010-02-15 08:04:42 +0000219
Chris Lattnerb21ba712010-02-25 02:04:40 +0000220 static inline bool classof(const Matcher *N) {
Chris Lattner60df53e2010-02-25 01:56:48 +0000221 return N->getKind() == Scope;
Chris Lattnerda272d12010-02-15 08:04:42 +0000222 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000223
Chris Lattner58aa8342010-02-25 06:49:58 +0000224private:
Chris Lattnera5028a62010-02-25 06:53:39 +0000225 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner58aa8342010-02-25 06:49:58 +0000226 virtual bool isEqualImpl(const Matcher *M) const { return false; }
Chris Lattnerd6c84722010-02-25 19:00:39 +0000227 virtual unsigned getHashImpl() const { return 12312; }
Chris Lattnerda272d12010-02-15 08:04:42 +0000228};
229
Chris Lattnerb21ba712010-02-25 02:04:40 +0000230/// RecordMatcher - Save the current node in the operand list.
231class RecordMatcher : public Matcher {
Chris Lattnerc96087b2010-02-17 01:03:09 +0000232 /// WhatFor - This is a string indicating why we're recording this. This
233 /// should only be used for comment generation not anything semantic.
234 std::string WhatFor;
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000235
Chris Lattner0cebe612010-03-01 02:24:17 +0000236 /// ResultNo - The slot number in the RecordedNodes vector that this will be,
237 /// just printed as a comment.
238 unsigned ResultNo;
Chris Lattnerda272d12010-02-15 08:04:42 +0000239public:
Chris Lattner0cebe612010-03-01 02:24:17 +0000240 RecordMatcher(const std::string &whatfor, unsigned resultNo)
241 : Matcher(RecordNode), WhatFor(whatfor), ResultNo(resultNo) {}
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000242
Chris Lattnerc642b842010-02-17 01:27:29 +0000243 const std::string &getWhatFor() const { return WhatFor; }
Chris Lattner0cebe612010-03-01 02:24:17 +0000244 unsigned getResultNo() const { return ResultNo; }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000245
Chris Lattnerb21ba712010-02-25 02:04:40 +0000246 static inline bool classof(const Matcher *N) {
Chris Lattner845c0422010-02-18 22:03:03 +0000247 return N->getKind() == RecordNode;
Chris Lattnerda272d12010-02-15 08:04:42 +0000248 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000249
Chris Lattnerd323fd42010-02-27 06:22:57 +0000250 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
Chris Lattner58aa8342010-02-25 06:49:58 +0000251private:
Chris Lattnera5028a62010-02-25 06:53:39 +0000252 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner58aa8342010-02-25 06:49:58 +0000253 virtual bool isEqualImpl(const Matcher *M) const { return true; }
254 virtual unsigned getHashImpl() const { return 0; }
Chris Lattnerda272d12010-02-15 08:04:42 +0000255};
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000256
Chris Lattnerb21ba712010-02-25 02:04:40 +0000257/// RecordChildMatcher - Save a numbered child of the current node, or fail
Chris Lattner19b5a752010-02-24 07:31:45 +0000258/// the match if it doesn't exist. This is logically equivalent to:
259/// MoveChild N + RecordNode + MoveParent.
Chris Lattnerb21ba712010-02-25 02:04:40 +0000260class RecordChildMatcher : public Matcher {
Chris Lattner19b5a752010-02-24 07:31:45 +0000261 unsigned ChildNo;
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000262
Chris Lattner19b5a752010-02-24 07:31:45 +0000263 /// WhatFor - This is a string indicating why we're recording this. This
264 /// should only be used for comment generation not anything semantic.
265 std::string WhatFor;
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000266
Chris Lattner0cebe612010-03-01 02:24:17 +0000267 /// ResultNo - The slot number in the RecordedNodes vector that this will be,
268 /// just printed as a comment.
269 unsigned ResultNo;
Chris Lattner19b5a752010-02-24 07:31:45 +0000270public:
Chris Lattner0cebe612010-03-01 02:24:17 +0000271 RecordChildMatcher(unsigned childno, const std::string &whatfor,
272 unsigned resultNo)
273 : Matcher(RecordChild), ChildNo(childno), WhatFor(whatfor),
274 ResultNo(resultNo) {}
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000275
Chris Lattner19b5a752010-02-24 07:31:45 +0000276 unsigned getChildNo() const { return ChildNo; }
277 const std::string &getWhatFor() const { return WhatFor; }
Chris Lattner0cebe612010-03-01 02:24:17 +0000278 unsigned getResultNo() const { return ResultNo; }
279
Chris Lattnerb21ba712010-02-25 02:04:40 +0000280 static inline bool classof(const Matcher *N) {
Chris Lattner19b5a752010-02-24 07:31:45 +0000281 return N->getKind() == RecordChild;
282 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000283
Chris Lattnerd323fd42010-02-27 06:22:57 +0000284 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
285
Chris Lattner58aa8342010-02-25 06:49:58 +0000286private:
Chris Lattnera5028a62010-02-25 06:53:39 +0000287 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner58aa8342010-02-25 06:49:58 +0000288 virtual bool isEqualImpl(const Matcher *M) const {
289 return cast<RecordChildMatcher>(M)->getChildNo() == getChildNo();
290 }
291 virtual unsigned getHashImpl() const { return getChildNo(); }
Chris Lattner19b5a752010-02-24 07:31:45 +0000292};
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000293
Chris Lattnerb21ba712010-02-25 02:04:40 +0000294/// RecordMemRefMatcher - Save the current node's memref.
295class RecordMemRefMatcher : public Matcher {
Chris Lattner8e946be2010-02-21 03:22:59 +0000296public:
Chris Lattnerb21ba712010-02-25 02:04:40 +0000297 RecordMemRefMatcher() : Matcher(RecordMemRef) {}
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000298
Chris Lattnerb21ba712010-02-25 02:04:40 +0000299 static inline bool classof(const Matcher *N) {
Chris Lattner8e946be2010-02-21 03:22:59 +0000300 return N->getKind() == RecordMemRef;
301 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000302
Chris Lattnerd323fd42010-02-27 06:22:57 +0000303 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
304
Chris Lattner58aa8342010-02-25 06:49:58 +0000305private:
Chris Lattnera5028a62010-02-25 06:53:39 +0000306 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner58aa8342010-02-25 06:49:58 +0000307 virtual bool isEqualImpl(const Matcher *M) const { return true; }
308 virtual unsigned getHashImpl() const { return 0; }
Chris Lattner8e946be2010-02-21 03:22:59 +0000309};
310
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000311
Chris Lattner8950bca2010-12-23 17:03:20 +0000312/// CaptureGlueInputMatcher - If the current record has a glue input, record
Chris Lattner8e946be2010-02-21 03:22:59 +0000313/// it so that it is used as an input to the generated code.
Chris Lattner8950bca2010-12-23 17:03:20 +0000314class CaptureGlueInputMatcher : public Matcher {
Chris Lattner8e946be2010-02-21 03:22:59 +0000315public:
Chris Lattner8950bca2010-12-23 17:03:20 +0000316 CaptureGlueInputMatcher() : Matcher(CaptureGlueInput) {}
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000317
Chris Lattnerb21ba712010-02-25 02:04:40 +0000318 static inline bool classof(const Matcher *N) {
Chris Lattner8950bca2010-12-23 17:03:20 +0000319 return N->getKind() == CaptureGlueInput;
Chris Lattner8e946be2010-02-21 03:22:59 +0000320 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000321
Chris Lattnerd323fd42010-02-27 06:22:57 +0000322 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
323
Chris Lattner58aa8342010-02-25 06:49:58 +0000324private:
Chris Lattnera5028a62010-02-25 06:53:39 +0000325 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner58aa8342010-02-25 06:49:58 +0000326 virtual bool isEqualImpl(const Matcher *M) const { return true; }
327 virtual unsigned getHashImpl() const { return 0; }
Chris Lattner8e946be2010-02-21 03:22:59 +0000328};
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000329
Chris Lattnerb21ba712010-02-25 02:04:40 +0000330/// MoveChildMatcher - This tells the interpreter to move into the
Chris Lattnerda272d12010-02-15 08:04:42 +0000331/// specified child node.
Chris Lattnerb21ba712010-02-25 02:04:40 +0000332class MoveChildMatcher : public Matcher {
Chris Lattnerda272d12010-02-15 08:04:42 +0000333 unsigned ChildNo;
334public:
Chris Lattnerb21ba712010-02-25 02:04:40 +0000335 MoveChildMatcher(unsigned childNo) : Matcher(MoveChild), ChildNo(childNo) {}
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000336
Chris Lattnerda272d12010-02-15 08:04:42 +0000337 unsigned getChildNo() const { return ChildNo; }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000338
Chris Lattnerb21ba712010-02-25 02:04:40 +0000339 static inline bool classof(const Matcher *N) {
Chris Lattnerda272d12010-02-15 08:04:42 +0000340 return N->getKind() == MoveChild;
341 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000342
Chris Lattnerd323fd42010-02-27 06:22:57 +0000343 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
344
Chris Lattner58aa8342010-02-25 06:49:58 +0000345private:
Chris Lattnera5028a62010-02-25 06:53:39 +0000346 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner58aa8342010-02-25 06:49:58 +0000347 virtual bool isEqualImpl(const Matcher *M) const {
348 return cast<MoveChildMatcher>(M)->getChildNo() == getChildNo();
349 }
350 virtual unsigned getHashImpl() const { return getChildNo(); }
Chris Lattnerda272d12010-02-15 08:04:42 +0000351};
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000352
Chris Lattnerb21ba712010-02-25 02:04:40 +0000353/// MoveParentMatcher - This tells the interpreter to move to the parent
Chris Lattnerda272d12010-02-15 08:04:42 +0000354/// of the current node.
Chris Lattnerb21ba712010-02-25 02:04:40 +0000355class MoveParentMatcher : public Matcher {
Chris Lattnerda272d12010-02-15 08:04:42 +0000356public:
Chris Lattnerb21ba712010-02-25 02:04:40 +0000357 MoveParentMatcher() : Matcher(MoveParent) {}
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000358
Chris Lattnerb21ba712010-02-25 02:04:40 +0000359 static inline bool classof(const Matcher *N) {
Chris Lattnerda272d12010-02-15 08:04:42 +0000360 return N->getKind() == MoveParent;
361 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000362
Chris Lattnerd323fd42010-02-27 06:22:57 +0000363 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
364
Chris Lattner58aa8342010-02-25 06:49:58 +0000365private:
Chris Lattnera5028a62010-02-25 06:53:39 +0000366 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner58aa8342010-02-25 06:49:58 +0000367 virtual bool isEqualImpl(const Matcher *M) const { return true; }
368 virtual unsigned getHashImpl() const { return 0; }
Chris Lattnerda272d12010-02-15 08:04:42 +0000369};
370
Chris Lattnerb21ba712010-02-25 02:04:40 +0000371/// CheckSameMatcher - This checks to see if this node is exactly the same
Chris Lattnerda272d12010-02-15 08:04:42 +0000372/// node as the specified match that was recorded with 'Record'. This is used
373/// when patterns have the same name in them, like '(mul GPR:$in, GPR:$in)'.
Chris Lattnerb21ba712010-02-25 02:04:40 +0000374class CheckSameMatcher : public Matcher {
Chris Lattnerda272d12010-02-15 08:04:42 +0000375 unsigned MatchNumber;
376public:
Chris Lattnerb21ba712010-02-25 02:04:40 +0000377 CheckSameMatcher(unsigned matchnumber)
Chris Lattner58aa8342010-02-25 06:49:58 +0000378 : Matcher(CheckSame), MatchNumber(matchnumber) {}
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000379
Chris Lattnerda272d12010-02-15 08:04:42 +0000380 unsigned getMatchNumber() const { return MatchNumber; }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000381
Chris Lattnerb21ba712010-02-25 02:04:40 +0000382 static inline bool classof(const Matcher *N) {
Chris Lattnerda272d12010-02-15 08:04:42 +0000383 return N->getKind() == CheckSame;
384 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000385
Chris Lattnerd323fd42010-02-27 06:22:57 +0000386 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
387
Chris Lattner58aa8342010-02-25 06:49:58 +0000388private:
Chris Lattnera5028a62010-02-25 06:53:39 +0000389 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner58aa8342010-02-25 06:49:58 +0000390 virtual bool isEqualImpl(const Matcher *M) const {
391 return cast<CheckSameMatcher>(M)->getMatchNumber() == getMatchNumber();
392 }
393 virtual unsigned getHashImpl() const { return getMatchNumber(); }
Chris Lattnerda272d12010-02-15 08:04:42 +0000394};
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000395
Chris Lattnerb21ba712010-02-25 02:04:40 +0000396/// CheckPatternPredicateMatcher - This checks the target-specific predicate
Chris Lattnerda272d12010-02-15 08:04:42 +0000397/// to see if the entire pattern is capable of matching. This predicate does
398/// not take a node as input. This is used for subtarget feature checks etc.
Chris Lattnerb21ba712010-02-25 02:04:40 +0000399class CheckPatternPredicateMatcher : public Matcher {
Chris Lattnerda272d12010-02-15 08:04:42 +0000400 std::string Predicate;
401public:
Chris Lattnerb21ba712010-02-25 02:04:40 +0000402 CheckPatternPredicateMatcher(StringRef predicate)
Chris Lattner58aa8342010-02-25 06:49:58 +0000403 : Matcher(CheckPatternPredicate), Predicate(predicate) {}
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000404
Chris Lattnerda272d12010-02-15 08:04:42 +0000405 StringRef getPredicate() const { return Predicate; }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000406
Chris Lattnerb21ba712010-02-25 02:04:40 +0000407 static inline bool classof(const Matcher *N) {
Chris Lattnerda272d12010-02-15 08:04:42 +0000408 return N->getKind() == CheckPatternPredicate;
409 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000410
Chris Lattnerd323fd42010-02-27 06:22:57 +0000411 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
412
Chris Lattner58aa8342010-02-25 06:49:58 +0000413private:
Chris Lattnera5028a62010-02-25 06:53:39 +0000414 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner58aa8342010-02-25 06:49:58 +0000415 virtual bool isEqualImpl(const Matcher *M) const {
416 return cast<CheckPatternPredicateMatcher>(M)->getPredicate() == Predicate;
417 }
418 virtual unsigned getHashImpl() const;
Chris Lattnerda272d12010-02-15 08:04:42 +0000419};
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000420
Chris Lattnerb21ba712010-02-25 02:04:40 +0000421/// CheckPredicateMatcher - This checks the target-specific predicate to
Chris Lattnerda272d12010-02-15 08:04:42 +0000422/// see if the node is acceptable.
Chris Lattnerb21ba712010-02-25 02:04:40 +0000423class CheckPredicateMatcher : public Matcher {
Chris Lattner54379062011-04-17 21:38:24 +0000424 TreePattern *Pred;
Chris Lattnerda272d12010-02-15 08:04:42 +0000425public:
Chris Lattner54379062011-04-17 21:38:24 +0000426 CheckPredicateMatcher(const TreePredicateFn &pred);
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000427
Chris Lattner54379062011-04-17 21:38:24 +0000428 TreePredicateFn getPredicate() const;
Chris Lattnerda272d12010-02-15 08:04:42 +0000429
Chris Lattnerb21ba712010-02-25 02:04:40 +0000430 static inline bool classof(const Matcher *N) {
Chris Lattnerda272d12010-02-15 08:04:42 +0000431 return N->getKind() == CheckPredicate;
432 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000433
Chris Lattnerd323fd42010-02-27 06:22:57 +0000434 // TODO: Ok?
435 //virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
436
Chris Lattner58aa8342010-02-25 06:49:58 +0000437private:
Chris Lattnera5028a62010-02-25 06:53:39 +0000438 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner58aa8342010-02-25 06:49:58 +0000439 virtual bool isEqualImpl(const Matcher *M) const {
Chris Lattner54379062011-04-17 21:38:24 +0000440 return cast<CheckPredicateMatcher>(M)->Pred == Pred;
Chris Lattner58aa8342010-02-25 06:49:58 +0000441 }
442 virtual unsigned getHashImpl() const;
Chris Lattnerda272d12010-02-15 08:04:42 +0000443};
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000444
445
Chris Lattnerb21ba712010-02-25 02:04:40 +0000446/// CheckOpcodeMatcher - This checks to see if the current node has the
Chris Lattnerda272d12010-02-15 08:04:42 +0000447/// specified opcode, if not it fails to match.
Chris Lattnerb21ba712010-02-25 02:04:40 +0000448class CheckOpcodeMatcher : public Matcher {
Chris Lattnera230f962010-02-27 21:48:43 +0000449 const SDNodeInfo &Opcode;
Chris Lattnerda272d12010-02-15 08:04:42 +0000450public:
Chris Lattnera230f962010-02-27 21:48:43 +0000451 CheckOpcodeMatcher(const SDNodeInfo &opcode)
452 : Matcher(CheckOpcode), Opcode(opcode) {}
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000453
Chris Lattnera230f962010-02-27 21:48:43 +0000454 const SDNodeInfo &getOpcode() const { return Opcode; }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000455
Chris Lattnerb21ba712010-02-25 02:04:40 +0000456 static inline bool classof(const Matcher *N) {
Chris Lattnerda272d12010-02-15 08:04:42 +0000457 return N->getKind() == CheckOpcode;
458 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000459
Chris Lattnerd323fd42010-02-27 06:22:57 +0000460 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
461
Chris Lattner58aa8342010-02-25 06:49:58 +0000462private:
Chris Lattnera5028a62010-02-25 06:53:39 +0000463 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattnereb669212010-03-01 06:59:22 +0000464 virtual bool isEqualImpl(const Matcher *M) const;
Chris Lattner58aa8342010-02-25 06:49:58 +0000465 virtual unsigned getHashImpl() const;
Chris Lattner82781b92010-02-27 07:49:13 +0000466 virtual bool isContradictoryImpl(const Matcher *M) const;
Chris Lattnerda272d12010-02-15 08:04:42 +0000467};
Chris Lattnereb669212010-03-01 06:59:22 +0000468
469/// SwitchOpcodeMatcher - Switch based on the current node's opcode, dispatching
470/// to one matcher per opcode. If the opcode doesn't match any of the cases,
471/// then the match fails. This is semantically equivalent to a Scope node where
472/// every child does a CheckOpcode, but is much faster.
473class SwitchOpcodeMatcher : public Matcher {
474 SmallVector<std::pair<const SDNodeInfo*, Matcher*>, 8> Cases;
475public:
476 SwitchOpcodeMatcher(const std::pair<const SDNodeInfo*, Matcher*> *cases,
477 unsigned numcases)
478 : Matcher(SwitchOpcode), Cases(cases, cases+numcases) {}
479
480 static inline bool classof(const Matcher *N) {
481 return N->getKind() == SwitchOpcode;
482 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000483
Chris Lattnereb669212010-03-01 06:59:22 +0000484 unsigned getNumCases() const { return Cases.size(); }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000485
Chris Lattnereb669212010-03-01 06:59:22 +0000486 const SDNodeInfo &getCaseOpcode(unsigned i) const { return *Cases[i].first; }
487 Matcher *getCaseMatcher(unsigned i) { return Cases[i].second; }
488 const Matcher *getCaseMatcher(unsigned i) const { return Cases[i].second; }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000489
Chris Lattnereb669212010-03-01 06:59:22 +0000490private:
491 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
492 virtual bool isEqualImpl(const Matcher *M) const { return false; }
493 virtual unsigned getHashImpl() const { return 4123; }
494};
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000495
Chris Lattnerb21ba712010-02-25 02:04:40 +0000496/// CheckTypeMatcher - This checks to see if the current node has the
Chris Lattner084df622010-03-24 00:41:19 +0000497/// specified type at the specified result, if not it fails to match.
Chris Lattnerb21ba712010-02-25 02:04:40 +0000498class CheckTypeMatcher : public Matcher {
Chris Lattnerda272d12010-02-15 08:04:42 +0000499 MVT::SimpleValueType Type;
Chris Lattner084df622010-03-24 00:41:19 +0000500 unsigned ResNo;
Chris Lattnerda272d12010-02-15 08:04:42 +0000501public:
Chris Lattner084df622010-03-24 00:41:19 +0000502 CheckTypeMatcher(MVT::SimpleValueType type, unsigned resno)
503 : Matcher(CheckType), Type(type), ResNo(resno) {}
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000504
Chris Lattnerda272d12010-02-15 08:04:42 +0000505 MVT::SimpleValueType getType() const { return Type; }
Chris Lattner084df622010-03-24 00:41:19 +0000506 unsigned getResNo() const { return ResNo; }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000507
Chris Lattnerb21ba712010-02-25 02:04:40 +0000508 static inline bool classof(const Matcher *N) {
Chris Lattnerda272d12010-02-15 08:04:42 +0000509 return N->getKind() == CheckType;
510 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000511
Chris Lattnerd323fd42010-02-27 06:22:57 +0000512 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
513
Chris Lattner58aa8342010-02-25 06:49:58 +0000514private:
Chris Lattnera5028a62010-02-25 06:53:39 +0000515 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner58aa8342010-02-25 06:49:58 +0000516 virtual bool isEqualImpl(const Matcher *M) const {
Chris Lattner38717f62010-02-26 08:05:36 +0000517 return cast<CheckTypeMatcher>(M)->Type == Type;
Chris Lattner58aa8342010-02-25 06:49:58 +0000518 }
519 virtual unsigned getHashImpl() const { return Type; }
Chris Lattner82781b92010-02-27 07:49:13 +0000520 virtual bool isContradictoryImpl(const Matcher *M) const;
Chris Lattnerda272d12010-02-15 08:04:42 +0000521};
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000522
Chris Lattnercfe2eab2010-03-03 06:28:15 +0000523/// SwitchTypeMatcher - Switch based on the current node's type, dispatching
524/// to one matcher per case. If the type doesn't match any of the cases,
525/// then the match fails. This is semantically equivalent to a Scope node where
526/// every child does a CheckType, but is much faster.
527class SwitchTypeMatcher : public Matcher {
528 SmallVector<std::pair<MVT::SimpleValueType, Matcher*>, 8> Cases;
529public:
530 SwitchTypeMatcher(const std::pair<MVT::SimpleValueType, Matcher*> *cases,
531 unsigned numcases)
532 : Matcher(SwitchType), Cases(cases, cases+numcases) {}
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000533
Chris Lattnercfe2eab2010-03-03 06:28:15 +0000534 static inline bool classof(const Matcher *N) {
535 return N->getKind() == SwitchType;
536 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000537
Chris Lattnercfe2eab2010-03-03 06:28:15 +0000538 unsigned getNumCases() const { return Cases.size(); }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000539
Chris Lattnercfe2eab2010-03-03 06:28:15 +0000540 MVT::SimpleValueType getCaseType(unsigned i) const { return Cases[i].first; }
541 Matcher *getCaseMatcher(unsigned i) { return Cases[i].second; }
542 const Matcher *getCaseMatcher(unsigned i) const { return Cases[i].second; }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000543
Chris Lattnercfe2eab2010-03-03 06:28:15 +0000544private:
545 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
546 virtual bool isEqualImpl(const Matcher *M) const { return false; }
547 virtual unsigned getHashImpl() const { return 4123; }
548};
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000549
550
Chris Lattnerb21ba712010-02-25 02:04:40 +0000551/// CheckChildTypeMatcher - This checks to see if a child node has the
Chris Lattner23cfda72010-02-24 20:15:25 +0000552/// specified type, if not it fails to match.
Chris Lattnerb21ba712010-02-25 02:04:40 +0000553class CheckChildTypeMatcher : public Matcher {
Chris Lattner23cfda72010-02-24 20:15:25 +0000554 unsigned ChildNo;
555 MVT::SimpleValueType Type;
556public:
Chris Lattnerb21ba712010-02-25 02:04:40 +0000557 CheckChildTypeMatcher(unsigned childno, MVT::SimpleValueType type)
558 : Matcher(CheckChildType), ChildNo(childno), Type(type) {}
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000559
Chris Lattner23cfda72010-02-24 20:15:25 +0000560 unsigned getChildNo() const { return ChildNo; }
561 MVT::SimpleValueType getType() const { return Type; }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000562
Chris Lattnerb21ba712010-02-25 02:04:40 +0000563 static inline bool classof(const Matcher *N) {
Chris Lattner23cfda72010-02-24 20:15:25 +0000564 return N->getKind() == CheckChildType;
565 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000566
Chris Lattnerd323fd42010-02-27 06:22:57 +0000567 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
568
Chris Lattner58aa8342010-02-25 06:49:58 +0000569private:
Chris Lattnera5028a62010-02-25 06:53:39 +0000570 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner58aa8342010-02-25 06:49:58 +0000571 virtual bool isEqualImpl(const Matcher *M) const {
572 return cast<CheckChildTypeMatcher>(M)->ChildNo == ChildNo &&
573 cast<CheckChildTypeMatcher>(M)->Type == Type;
574 }
575 virtual unsigned getHashImpl() const { return (Type << 3) | ChildNo; }
Chris Lattner82781b92010-02-27 07:49:13 +0000576 virtual bool isContradictoryImpl(const Matcher *M) const;
Chris Lattner23cfda72010-02-24 20:15:25 +0000577};
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000578
Chris Lattnerda272d12010-02-15 08:04:42 +0000579
Chris Lattnerb21ba712010-02-25 02:04:40 +0000580/// CheckIntegerMatcher - This checks to see if the current node is a
Chris Lattnerda272d12010-02-15 08:04:42 +0000581/// ConstantSDNode with the specified integer value, if not it fails to match.
Chris Lattnerb21ba712010-02-25 02:04:40 +0000582class CheckIntegerMatcher : public Matcher {
Chris Lattnerda272d12010-02-15 08:04:42 +0000583 int64_t Value;
584public:
Chris Lattnerb21ba712010-02-25 02:04:40 +0000585 CheckIntegerMatcher(int64_t value)
586 : Matcher(CheckInteger), Value(value) {}
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000587
Chris Lattnerda272d12010-02-15 08:04:42 +0000588 int64_t getValue() const { return Value; }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000589
Chris Lattnerb21ba712010-02-25 02:04:40 +0000590 static inline bool classof(const Matcher *N) {
Chris Lattnerda272d12010-02-15 08:04:42 +0000591 return N->getKind() == CheckInteger;
592 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000593
Chris Lattnerd323fd42010-02-27 06:22:57 +0000594 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
595
Chris Lattner58aa8342010-02-25 06:49:58 +0000596private:
Chris Lattnera5028a62010-02-25 06:53:39 +0000597 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner58aa8342010-02-25 06:49:58 +0000598 virtual bool isEqualImpl(const Matcher *M) const {
599 return cast<CheckIntegerMatcher>(M)->Value == Value;
600 }
601 virtual unsigned getHashImpl() const { return Value; }
Chris Lattner24789622010-02-27 08:11:15 +0000602 virtual bool isContradictoryImpl(const Matcher *M) const;
Chris Lattnerda272d12010-02-15 08:04:42 +0000603};
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000604
Chris Lattnerb21ba712010-02-25 02:04:40 +0000605/// CheckCondCodeMatcher - This checks to see if the current node is a
Chris Lattnerda272d12010-02-15 08:04:42 +0000606/// CondCodeSDNode with the specified condition, if not it fails to match.
Chris Lattnerb21ba712010-02-25 02:04:40 +0000607class CheckCondCodeMatcher : public Matcher {
Chris Lattnerda272d12010-02-15 08:04:42 +0000608 StringRef CondCodeName;
609public:
Chris Lattnerb21ba712010-02-25 02:04:40 +0000610 CheckCondCodeMatcher(StringRef condcodename)
611 : Matcher(CheckCondCode), CondCodeName(condcodename) {}
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000612
Chris Lattnerda272d12010-02-15 08:04:42 +0000613 StringRef getCondCodeName() const { return CondCodeName; }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000614
Chris Lattnerb21ba712010-02-25 02:04:40 +0000615 static inline bool classof(const Matcher *N) {
Chris Lattnerda272d12010-02-15 08:04:42 +0000616 return N->getKind() == CheckCondCode;
617 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000618
Chris Lattnerd323fd42010-02-27 06:22:57 +0000619 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
620
Chris Lattner58aa8342010-02-25 06:49:58 +0000621private:
Chris Lattnera5028a62010-02-25 06:53:39 +0000622 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner58aa8342010-02-25 06:49:58 +0000623 virtual bool isEqualImpl(const Matcher *M) const {
624 return cast<CheckCondCodeMatcher>(M)->CondCodeName == CondCodeName;
625 }
626 virtual unsigned getHashImpl() const;
Chris Lattnerda272d12010-02-15 08:04:42 +0000627};
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000628
Chris Lattnerb21ba712010-02-25 02:04:40 +0000629/// CheckValueTypeMatcher - This checks to see if the current node is a
Chris Lattnerda272d12010-02-15 08:04:42 +0000630/// VTSDNode with the specified type, if not it fails to match.
Chris Lattnerb21ba712010-02-25 02:04:40 +0000631class CheckValueTypeMatcher : public Matcher {
Chris Lattnerda272d12010-02-15 08:04:42 +0000632 StringRef TypeName;
633public:
Chris Lattnerb21ba712010-02-25 02:04:40 +0000634 CheckValueTypeMatcher(StringRef type_name)
635 : Matcher(CheckValueType), TypeName(type_name) {}
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000636
Chris Lattnerda272d12010-02-15 08:04:42 +0000637 StringRef getTypeName() const { return TypeName; }
638
Chris Lattnerb21ba712010-02-25 02:04:40 +0000639 static inline bool classof(const Matcher *N) {
Chris Lattnerda272d12010-02-15 08:04:42 +0000640 return N->getKind() == CheckValueType;
641 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000642
Chris Lattnerd323fd42010-02-27 06:22:57 +0000643 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
644
Chris Lattner58aa8342010-02-25 06:49:58 +0000645private:
Chris Lattnera5028a62010-02-25 06:53:39 +0000646 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner58aa8342010-02-25 06:49:58 +0000647 virtual bool isEqualImpl(const Matcher *M) const {
648 return cast<CheckValueTypeMatcher>(M)->TypeName == TypeName;
649 }
650 virtual unsigned getHashImpl() const;
Chris Lattner48aa5752010-03-07 06:29:26 +0000651 bool isContradictoryImpl(const Matcher *M) const;
Chris Lattnerda272d12010-02-15 08:04:42 +0000652};
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000653
654
655
Chris Lattnerb21ba712010-02-25 02:04:40 +0000656/// CheckComplexPatMatcher - This node runs the specified ComplexPattern on
Chris Lattnerda272d12010-02-15 08:04:42 +0000657/// the current node.
Chris Lattnerb21ba712010-02-25 02:04:40 +0000658class CheckComplexPatMatcher : public Matcher {
Chris Lattnerda272d12010-02-15 08:04:42 +0000659 const ComplexPattern &Pattern;
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000660
661 /// MatchNumber - This is the recorded nodes slot that contains the node we
662 /// want to match against.
Chris Lattner57bf8a42010-03-04 01:23:08 +0000663 unsigned MatchNumber;
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000664
Chris Lattner57bf8a42010-03-04 01:23:08 +0000665 /// Name - The name of the node we're matching, for comment emission.
666 std::string Name;
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000667
Chris Lattnerd1aca7c2010-03-04 00:28:05 +0000668 /// FirstResult - This is the first slot in the RecordedNodes list that the
669 /// result of the match populates.
670 unsigned FirstResult;
Chris Lattnerda272d12010-02-15 08:04:42 +0000671public:
Chris Lattner57bf8a42010-03-04 01:23:08 +0000672 CheckComplexPatMatcher(const ComplexPattern &pattern, unsigned matchnumber,
673 const std::string &name, unsigned firstresult)
674 : Matcher(CheckComplexPat), Pattern(pattern), MatchNumber(matchnumber),
675 Name(name), FirstResult(firstresult) {}
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000676
Chris Lattnere609a512010-02-17 00:31:50 +0000677 const ComplexPattern &getPattern() const { return Pattern; }
Chris Lattner57bf8a42010-03-04 01:23:08 +0000678 unsigned getMatchNumber() const { return MatchNumber; }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000679
Chris Lattner57bf8a42010-03-04 01:23:08 +0000680 const std::string getName() const { return Name; }
Chris Lattnerd1aca7c2010-03-04 00:28:05 +0000681 unsigned getFirstResult() const { return FirstResult; }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000682
Chris Lattnerb21ba712010-02-25 02:04:40 +0000683 static inline bool classof(const Matcher *N) {
Chris Lattnerda272d12010-02-15 08:04:42 +0000684 return N->getKind() == CheckComplexPat;
685 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000686
Chris Lattnerd323fd42010-02-27 06:22:57 +0000687 // Not safe to move a pattern predicate past a complex pattern.
688 virtual bool isSafeToReorderWithPatternPredicate() const { return false; }
689
Chris Lattner58aa8342010-02-25 06:49:58 +0000690private:
Chris Lattnera5028a62010-02-25 06:53:39 +0000691 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner58aa8342010-02-25 06:49:58 +0000692 virtual bool isEqualImpl(const Matcher *M) const {
Chris Lattner57bf8a42010-03-04 01:23:08 +0000693 return &cast<CheckComplexPatMatcher>(M)->Pattern == &Pattern &&
694 cast<CheckComplexPatMatcher>(M)->MatchNumber == MatchNumber;
Chris Lattner58aa8342010-02-25 06:49:58 +0000695 }
696 virtual unsigned getHashImpl() const {
Chris Lattner57bf8a42010-03-04 01:23:08 +0000697 return (unsigned)(intptr_t)&Pattern ^ MatchNumber;
Chris Lattner58aa8342010-02-25 06:49:58 +0000698 }
Chris Lattnerda272d12010-02-15 08:04:42 +0000699};
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000700
Chris Lattnerb21ba712010-02-25 02:04:40 +0000701/// CheckAndImmMatcher - This checks to see if the current node is an 'and'
Chris Lattnerda272d12010-02-15 08:04:42 +0000702/// with something equivalent to the specified immediate.
Chris Lattnerb21ba712010-02-25 02:04:40 +0000703class CheckAndImmMatcher : public Matcher {
Chris Lattnerda272d12010-02-15 08:04:42 +0000704 int64_t Value;
705public:
Chris Lattnerb21ba712010-02-25 02:04:40 +0000706 CheckAndImmMatcher(int64_t value)
707 : Matcher(CheckAndImm), Value(value) {}
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000708
Chris Lattnerda272d12010-02-15 08:04:42 +0000709 int64_t getValue() const { return Value; }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000710
Chris Lattnerb21ba712010-02-25 02:04:40 +0000711 static inline bool classof(const Matcher *N) {
Chris Lattnerda272d12010-02-15 08:04:42 +0000712 return N->getKind() == CheckAndImm;
713 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000714
Chris Lattnerd323fd42010-02-27 06:22:57 +0000715 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
716
Chris Lattner58aa8342010-02-25 06:49:58 +0000717private:
Chris Lattnera5028a62010-02-25 06:53:39 +0000718 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner58aa8342010-02-25 06:49:58 +0000719 virtual bool isEqualImpl(const Matcher *M) const {
720 return cast<CheckAndImmMatcher>(M)->Value == Value;
721 }
722 virtual unsigned getHashImpl() const { return Value; }
Chris Lattnerda272d12010-02-15 08:04:42 +0000723};
724
Chris Lattnerb21ba712010-02-25 02:04:40 +0000725/// CheckOrImmMatcher - This checks to see if the current node is an 'and'
Chris Lattnerda272d12010-02-15 08:04:42 +0000726/// with something equivalent to the specified immediate.
Chris Lattnerb21ba712010-02-25 02:04:40 +0000727class CheckOrImmMatcher : public Matcher {
Chris Lattnerda272d12010-02-15 08:04:42 +0000728 int64_t Value;
729public:
Chris Lattnerb21ba712010-02-25 02:04:40 +0000730 CheckOrImmMatcher(int64_t value)
731 : Matcher(CheckOrImm), Value(value) {}
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000732
Chris Lattnerda272d12010-02-15 08:04:42 +0000733 int64_t getValue() const { return Value; }
734
Chris Lattnerb21ba712010-02-25 02:04:40 +0000735 static inline bool classof(const Matcher *N) {
Chris Lattnerda272d12010-02-15 08:04:42 +0000736 return N->getKind() == CheckOrImm;
737 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000738
Chris Lattnerd323fd42010-02-27 06:22:57 +0000739 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
740
Chris Lattner58aa8342010-02-25 06:49:58 +0000741private:
Chris Lattnera5028a62010-02-25 06:53:39 +0000742 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner58aa8342010-02-25 06:49:58 +0000743 virtual bool isEqualImpl(const Matcher *M) const {
744 return cast<CheckOrImmMatcher>(M)->Value == Value;
745 }
746 virtual unsigned getHashImpl() const { return Value; }
Chris Lattnerda272d12010-02-15 08:04:42 +0000747};
Chris Lattnere39650a2010-02-16 06:10:58 +0000748
Chris Lattnerb21ba712010-02-25 02:04:40 +0000749/// CheckFoldableChainNodeMatcher - This checks to see if the current node
Chris Lattner21390d72010-02-16 19:15:55 +0000750/// (which defines a chain operand) is safe to fold into a larger pattern.
Chris Lattnerb21ba712010-02-25 02:04:40 +0000751class CheckFoldableChainNodeMatcher : public Matcher {
Chris Lattnere39650a2010-02-16 06:10:58 +0000752public:
Chris Lattnerb21ba712010-02-25 02:04:40 +0000753 CheckFoldableChainNodeMatcher()
754 : Matcher(CheckFoldableChainNode) {}
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000755
Chris Lattnerb21ba712010-02-25 02:04:40 +0000756 static inline bool classof(const Matcher *N) {
Chris Lattner21390d72010-02-16 19:15:55 +0000757 return N->getKind() == CheckFoldableChainNode;
Chris Lattnere39650a2010-02-16 06:10:58 +0000758 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000759
Chris Lattnerd323fd42010-02-27 06:22:57 +0000760 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
761
Chris Lattner58aa8342010-02-25 06:49:58 +0000762private:
Chris Lattnera5028a62010-02-25 06:53:39 +0000763 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner58aa8342010-02-25 06:49:58 +0000764 virtual bool isEqualImpl(const Matcher *M) const { return true; }
765 virtual unsigned getHashImpl() const { return 0; }
Chris Lattnere39650a2010-02-16 06:10:58 +0000766};
767
Chris Lattnerb21ba712010-02-25 02:04:40 +0000768/// EmitIntegerMatcher - This creates a new TargetConstant.
769class EmitIntegerMatcher : public Matcher {
Chris Lattner845c0422010-02-18 22:03:03 +0000770 int64_t Val;
771 MVT::SimpleValueType VT;
772public:
Chris Lattnerb21ba712010-02-25 02:04:40 +0000773 EmitIntegerMatcher(int64_t val, MVT::SimpleValueType vt)
774 : Matcher(EmitInteger), Val(val), VT(vt) {}
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000775
Chris Lattner906b4992010-02-19 07:49:56 +0000776 int64_t getValue() const { return Val; }
Chris Lattner845c0422010-02-18 22:03:03 +0000777 MVT::SimpleValueType getVT() const { return VT; }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000778
Chris Lattnerb21ba712010-02-25 02:04:40 +0000779 static inline bool classof(const Matcher *N) {
Chris Lattner845c0422010-02-18 22:03:03 +0000780 return N->getKind() == EmitInteger;
781 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000782
Chris Lattner58aa8342010-02-25 06:49:58 +0000783private:
Chris Lattnera5028a62010-02-25 06:53:39 +0000784 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner58aa8342010-02-25 06:49:58 +0000785 virtual bool isEqualImpl(const Matcher *M) const {
786 return cast<EmitIntegerMatcher>(M)->Val == Val &&
787 cast<EmitIntegerMatcher>(M)->VT == VT;
788 }
789 virtual unsigned getHashImpl() const { return (Val << 4) | VT; }
Chris Lattner845c0422010-02-18 22:03:03 +0000790};
Chris Lattner8e946be2010-02-21 03:22:59 +0000791
Chris Lattnerb21ba712010-02-25 02:04:40 +0000792/// EmitStringIntegerMatcher - A target constant whose value is represented
Chris Lattner8e946be2010-02-21 03:22:59 +0000793/// by a string.
Chris Lattnerb21ba712010-02-25 02:04:40 +0000794class EmitStringIntegerMatcher : public Matcher {
Chris Lattner8e946be2010-02-21 03:22:59 +0000795 std::string Val;
796 MVT::SimpleValueType VT;
797public:
Chris Lattnerb21ba712010-02-25 02:04:40 +0000798 EmitStringIntegerMatcher(const std::string &val, MVT::SimpleValueType vt)
799 : Matcher(EmitStringInteger), Val(val), VT(vt) {}
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000800
Chris Lattner8e946be2010-02-21 03:22:59 +0000801 const std::string &getValue() const { return Val; }
802 MVT::SimpleValueType getVT() const { return VT; }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000803
Chris Lattnerb21ba712010-02-25 02:04:40 +0000804 static inline bool classof(const Matcher *N) {
Chris Lattner8e946be2010-02-21 03:22:59 +0000805 return N->getKind() == EmitStringInteger;
806 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000807
Chris Lattner58aa8342010-02-25 06:49:58 +0000808private:
Chris Lattnera5028a62010-02-25 06:53:39 +0000809 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner58aa8342010-02-25 06:49:58 +0000810 virtual bool isEqualImpl(const Matcher *M) const {
811 return cast<EmitStringIntegerMatcher>(M)->Val == Val &&
812 cast<EmitStringIntegerMatcher>(M)->VT == VT;
813 }
814 virtual unsigned getHashImpl() const;
Chris Lattner8e946be2010-02-21 03:22:59 +0000815};
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000816
Chris Lattnerb21ba712010-02-25 02:04:40 +0000817/// EmitRegisterMatcher - This creates a new TargetConstant.
818class EmitRegisterMatcher : public Matcher {
Chris Lattner845c0422010-02-18 22:03:03 +0000819 /// Reg - The def for the register that we're emitting. If this is null, then
820 /// this is a reference to zero_reg.
Jim Grosbach4a6d7352011-03-11 02:19:02 +0000821 const CodeGenRegister *Reg;
Chris Lattner845c0422010-02-18 22:03:03 +0000822 MVT::SimpleValueType VT;
823public:
Jim Grosbach4a6d7352011-03-11 02:19:02 +0000824 EmitRegisterMatcher(const CodeGenRegister *reg, MVT::SimpleValueType vt)
Chris Lattnerb21ba712010-02-25 02:04:40 +0000825 : Matcher(EmitRegister), Reg(reg), VT(vt) {}
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000826
Jim Grosbach4a6d7352011-03-11 02:19:02 +0000827 const CodeGenRegister *getReg() const { return Reg; }
Chris Lattner845c0422010-02-18 22:03:03 +0000828 MVT::SimpleValueType getVT() const { return VT; }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000829
Chris Lattnerb21ba712010-02-25 02:04:40 +0000830 static inline bool classof(const Matcher *N) {
Chris Lattner845c0422010-02-18 22:03:03 +0000831 return N->getKind() == EmitRegister;
832 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000833
Chris Lattner58aa8342010-02-25 06:49:58 +0000834private:
Chris Lattnera5028a62010-02-25 06:53:39 +0000835 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner58aa8342010-02-25 06:49:58 +0000836 virtual bool isEqualImpl(const Matcher *M) const {
837 return cast<EmitRegisterMatcher>(M)->Reg == Reg &&
838 cast<EmitRegisterMatcher>(M)->VT == VT;
839 }
840 virtual unsigned getHashImpl() const {
841 return ((unsigned)(intptr_t)Reg) << 4 | VT;
842 }
Chris Lattner845c0422010-02-18 22:03:03 +0000843};
Chris Lattner8e946be2010-02-21 03:22:59 +0000844
Chris Lattnerb21ba712010-02-25 02:04:40 +0000845/// EmitConvertToTargetMatcher - Emit an operation that reads a specified
Chris Lattner8e946be2010-02-21 03:22:59 +0000846/// recorded node and converts it from being a ISD::Constant to
847/// ISD::TargetConstant, likewise for ConstantFP.
Chris Lattnerb21ba712010-02-25 02:04:40 +0000848class EmitConvertToTargetMatcher : public Matcher {
Chris Lattner8e946be2010-02-21 03:22:59 +0000849 unsigned Slot;
850public:
Chris Lattnerb21ba712010-02-25 02:04:40 +0000851 EmitConvertToTargetMatcher(unsigned slot)
852 : Matcher(EmitConvertToTarget), Slot(slot) {}
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000853
Chris Lattner8e946be2010-02-21 03:22:59 +0000854 unsigned getSlot() const { return Slot; }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000855
Chris Lattnerb21ba712010-02-25 02:04:40 +0000856 static inline bool classof(const Matcher *N) {
Chris Lattner8e946be2010-02-21 03:22:59 +0000857 return N->getKind() == EmitConvertToTarget;
858 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000859
Chris Lattner58aa8342010-02-25 06:49:58 +0000860private:
Chris Lattnera5028a62010-02-25 06:53:39 +0000861 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner58aa8342010-02-25 06:49:58 +0000862 virtual bool isEqualImpl(const Matcher *M) const {
863 return cast<EmitConvertToTargetMatcher>(M)->Slot == Slot;
864 }
865 virtual unsigned getHashImpl() const { return Slot; }
Chris Lattner8e946be2010-02-21 03:22:59 +0000866};
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000867
Chris Lattnerb21ba712010-02-25 02:04:40 +0000868/// EmitMergeInputChainsMatcher - Emit a node that merges a list of input
Chris Lattner8e946be2010-02-21 03:22:59 +0000869/// chains together with a token factor. The list of nodes are the nodes in the
870/// matched pattern that have chain input/outputs. This node adds all input
871/// chains of these nodes if they are not themselves a node in the pattern.
Chris Lattnerb21ba712010-02-25 02:04:40 +0000872class EmitMergeInputChainsMatcher : public Matcher {
Chris Lattner8e946be2010-02-21 03:22:59 +0000873 SmallVector<unsigned, 3> ChainNodes;
874public:
Chris Lattnerb21ba712010-02-25 02:04:40 +0000875 EmitMergeInputChainsMatcher(const unsigned *nodes, unsigned NumNodes)
876 : Matcher(EmitMergeInputChains), ChainNodes(nodes, nodes+NumNodes) {}
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000877
Chris Lattner8e946be2010-02-21 03:22:59 +0000878 unsigned getNumNodes() const { return ChainNodes.size(); }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000879
Chris Lattner8e946be2010-02-21 03:22:59 +0000880 unsigned getNode(unsigned i) const {
881 assert(i < ChainNodes.size());
882 return ChainNodes[i];
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000883 }
884
Chris Lattnerb21ba712010-02-25 02:04:40 +0000885 static inline bool classof(const Matcher *N) {
Chris Lattner8e946be2010-02-21 03:22:59 +0000886 return N->getKind() == EmitMergeInputChains;
887 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000888
Chris Lattner58aa8342010-02-25 06:49:58 +0000889private:
Chris Lattnera5028a62010-02-25 06:53:39 +0000890 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner58aa8342010-02-25 06:49:58 +0000891 virtual bool isEqualImpl(const Matcher *M) const {
892 return cast<EmitMergeInputChainsMatcher>(M)->ChainNodes == ChainNodes;
893 }
894 virtual unsigned getHashImpl() const;
Chris Lattner8e946be2010-02-21 03:22:59 +0000895};
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000896
Chris Lattnerb21ba712010-02-25 02:04:40 +0000897/// EmitCopyToRegMatcher - Emit a CopyToReg node from a value to a physreg,
Chris Lattner8950bca2010-12-23 17:03:20 +0000898/// pushing the chain and glue results.
Chris Lattner8e946be2010-02-21 03:22:59 +0000899///
Chris Lattnerb21ba712010-02-25 02:04:40 +0000900class EmitCopyToRegMatcher : public Matcher {
Chris Lattner8e946be2010-02-21 03:22:59 +0000901 unsigned SrcSlot; // Value to copy into the physreg.
902 Record *DestPhysReg;
903public:
Chris Lattnerb21ba712010-02-25 02:04:40 +0000904 EmitCopyToRegMatcher(unsigned srcSlot, Record *destPhysReg)
905 : Matcher(EmitCopyToReg), SrcSlot(srcSlot), DestPhysReg(destPhysReg) {}
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000906
Chris Lattner8e946be2010-02-21 03:22:59 +0000907 unsigned getSrcSlot() const { return SrcSlot; }
908 Record *getDestPhysReg() const { return DestPhysReg; }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000909
Chris Lattnerb21ba712010-02-25 02:04:40 +0000910 static inline bool classof(const Matcher *N) {
Chris Lattner8e946be2010-02-21 03:22:59 +0000911 return N->getKind() == EmitCopyToReg;
912 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000913
Chris Lattner58aa8342010-02-25 06:49:58 +0000914private:
Chris Lattnera5028a62010-02-25 06:53:39 +0000915 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner58aa8342010-02-25 06:49:58 +0000916 virtual bool isEqualImpl(const Matcher *M) const {
917 return cast<EmitCopyToRegMatcher>(M)->SrcSlot == SrcSlot &&
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000918 cast<EmitCopyToRegMatcher>(M)->DestPhysReg == DestPhysReg;
Chris Lattner58aa8342010-02-25 06:49:58 +0000919 }
920 virtual unsigned getHashImpl() const {
921 return SrcSlot ^ ((unsigned)(intptr_t)DestPhysReg << 4);
922 }
Chris Lattner8e946be2010-02-21 03:22:59 +0000923};
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000924
925
926
Chris Lattnerb21ba712010-02-25 02:04:40 +0000927/// EmitNodeXFormMatcher - Emit an operation that runs an SDNodeXForm on a
Chris Lattner8e946be2010-02-21 03:22:59 +0000928/// recorded node and records the result.
Chris Lattnerb21ba712010-02-25 02:04:40 +0000929class EmitNodeXFormMatcher : public Matcher {
Chris Lattner8e946be2010-02-21 03:22:59 +0000930 unsigned Slot;
931 Record *NodeXForm;
932public:
Chris Lattnerb21ba712010-02-25 02:04:40 +0000933 EmitNodeXFormMatcher(unsigned slot, Record *nodeXForm)
934 : Matcher(EmitNodeXForm), Slot(slot), NodeXForm(nodeXForm) {}
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000935
Chris Lattner8e946be2010-02-21 03:22:59 +0000936 unsigned getSlot() const { return Slot; }
937 Record *getNodeXForm() const { return NodeXForm; }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000938
Chris Lattnerb21ba712010-02-25 02:04:40 +0000939 static inline bool classof(const Matcher *N) {
Chris Lattner8e946be2010-02-21 03:22:59 +0000940 return N->getKind() == EmitNodeXForm;
941 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000942
Chris Lattner58aa8342010-02-25 06:49:58 +0000943private:
Chris Lattnera5028a62010-02-25 06:53:39 +0000944 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner58aa8342010-02-25 06:49:58 +0000945 virtual bool isEqualImpl(const Matcher *M) const {
946 return cast<EmitNodeXFormMatcher>(M)->Slot == Slot &&
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000947 cast<EmitNodeXFormMatcher>(M)->NodeXForm == NodeXForm;
Chris Lattner58aa8342010-02-25 06:49:58 +0000948 }
949 virtual unsigned getHashImpl() const {
950 return Slot ^ ((unsigned)(intptr_t)NodeXForm << 4);
951 }
Chris Lattner8e946be2010-02-21 03:22:59 +0000952};
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000953
Chris Lattnere86097a2010-02-28 02:31:26 +0000954/// EmitNodeMatcherCommon - Common class shared between EmitNode and
Chris Lattner9a215002010-02-28 20:55:18 +0000955/// MorphNodeTo.
Chris Lattnere86097a2010-02-28 02:31:26 +0000956class EmitNodeMatcherCommon : public Matcher {
Chris Lattner8e946be2010-02-21 03:22:59 +0000957 std::string OpcodeName;
958 const SmallVector<MVT::SimpleValueType, 3> VTs;
959 const SmallVector<unsigned, 6> Operands;
Chris Lattner036609b2010-12-23 18:28:41 +0000960 bool HasChain, HasInGlue, HasOutGlue, HasMemRefs;
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000961
Chris Lattner8e946be2010-02-21 03:22:59 +0000962 /// NumFixedArityOperands - If this is a fixed arity node, this is set to -1.
963 /// If this is a varidic node, this is set to the number of fixed arity
964 /// operands in the root of the pattern. The rest are appended to this node.
965 int NumFixedArityOperands;
966public:
Chris Lattnere86097a2010-02-28 02:31:26 +0000967 EmitNodeMatcherCommon(const std::string &opcodeName,
968 const MVT::SimpleValueType *vts, unsigned numvts,
969 const unsigned *operands, unsigned numops,
Chris Lattner8950bca2010-12-23 17:03:20 +0000970 bool hasChain, bool hasInGlue, bool hasOutGlue,
Chris Lattnerff7fb602010-02-28 21:53:42 +0000971 bool hasmemrefs,
Chris Lattner9a215002010-02-28 20:55:18 +0000972 int numfixedarityoperands, bool isMorphNodeTo)
973 : Matcher(isMorphNodeTo ? MorphNodeTo : EmitNode), OpcodeName(opcodeName),
Chris Lattner8e946be2010-02-21 03:22:59 +0000974 VTs(vts, vts+numvts), Operands(operands, operands+numops),
Chris Lattner036609b2010-12-23 18:28:41 +0000975 HasChain(hasChain), HasInGlue(hasInGlue), HasOutGlue(hasOutGlue),
Chris Lattnerff7fb602010-02-28 21:53:42 +0000976 HasMemRefs(hasmemrefs), NumFixedArityOperands(numfixedarityoperands) {}
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000977
Chris Lattner8e946be2010-02-21 03:22:59 +0000978 const std::string &getOpcodeName() const { return OpcodeName; }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000979
Chris Lattner8e946be2010-02-21 03:22:59 +0000980 unsigned getNumVTs() const { return VTs.size(); }
981 MVT::SimpleValueType getVT(unsigned i) const {
982 assert(i < VTs.size());
983 return VTs[i];
984 }
Chris Lattnerc78f2a32010-02-28 20:49:53 +0000985
Chris Lattner8e946be2010-02-21 03:22:59 +0000986 unsigned getNumOperands() const { return Operands.size(); }
987 unsigned getOperand(unsigned i) const {
988 assert(i < Operands.size());
989 return Operands[i];
Chris Lattnerc78f2a32010-02-28 20:49:53 +0000990 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000991
Chris Lattnerc78f2a32010-02-28 20:49:53 +0000992 const SmallVectorImpl<MVT::SimpleValueType> &getVTList() const { return VTs; }
993 const SmallVectorImpl<unsigned> &getOperandList() const { return Operands; }
994
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000995
Chris Lattner8e946be2010-02-21 03:22:59 +0000996 bool hasChain() const { return HasChain; }
Chris Lattner036609b2010-12-23 18:28:41 +0000997 bool hasInFlag() const { return HasInGlue; }
998 bool hasOutFlag() const { return HasOutGlue; }
Chris Lattner8e946be2010-02-21 03:22:59 +0000999 bool hasMemRefs() const { return HasMemRefs; }
1000 int getNumFixedArityOperands() const { return NumFixedArityOperands; }
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001001
Chris Lattnerb21ba712010-02-25 02:04:40 +00001002 static inline bool classof(const Matcher *N) {
Chris Lattner9a215002010-02-28 20:55:18 +00001003 return N->getKind() == EmitNode || N->getKind() == MorphNodeTo;
Chris Lattner845c0422010-02-18 22:03:03 +00001004 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001005
Chris Lattner58aa8342010-02-25 06:49:58 +00001006private:
Chris Lattnera5028a62010-02-25 06:53:39 +00001007 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner58aa8342010-02-25 06:49:58 +00001008 virtual bool isEqualImpl(const Matcher *M) const;
1009 virtual unsigned getHashImpl() const;
Chris Lattner845c0422010-02-18 22:03:03 +00001010};
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001011
Chris Lattnere86097a2010-02-28 02:31:26 +00001012/// EmitNodeMatcher - This signals a successful match and generates a node.
1013class EmitNodeMatcher : public EmitNodeMatcherCommon {
Chris Lattner6281cda2010-02-28 02:41:25 +00001014 unsigned FirstResultSlot;
Chris Lattnere86097a2010-02-28 02:31:26 +00001015public:
1016 EmitNodeMatcher(const std::string &opcodeName,
1017 const MVT::SimpleValueType *vts, unsigned numvts,
1018 const unsigned *operands, unsigned numops,
Chris Lattnerff7fb602010-02-28 21:53:42 +00001019 bool hasChain, bool hasInFlag, bool hasOutFlag,
1020 bool hasmemrefs,
Chris Lattner6281cda2010-02-28 02:41:25 +00001021 int numfixedarityoperands, unsigned firstresultslot)
Chris Lattnere86097a2010-02-28 02:31:26 +00001022 : EmitNodeMatcherCommon(opcodeName, vts, numvts, operands, numops, hasChain,
Chris Lattnerff7fb602010-02-28 21:53:42 +00001023 hasInFlag, hasOutFlag, hasmemrefs,
1024 numfixedarityoperands, false),
Chris Lattner6281cda2010-02-28 02:41:25 +00001025 FirstResultSlot(firstresultslot) {}
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001026
Chris Lattner6281cda2010-02-28 02:41:25 +00001027 unsigned getFirstResultSlot() const { return FirstResultSlot; }
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001028
Chris Lattnere86097a2010-02-28 02:31:26 +00001029 static inline bool classof(const Matcher *N) {
1030 return N->getKind() == EmitNode;
1031 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001032
Chris Lattnere86097a2010-02-28 02:31:26 +00001033};
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001034
Chris Lattner9a215002010-02-28 20:55:18 +00001035class MorphNodeToMatcher : public EmitNodeMatcherCommon {
Chris Lattnere86097a2010-02-28 02:31:26 +00001036 const PatternToMatch &Pattern;
1037public:
Chris Lattner9a215002010-02-28 20:55:18 +00001038 MorphNodeToMatcher(const std::string &opcodeName,
1039 const MVT::SimpleValueType *vts, unsigned numvts,
1040 const unsigned *operands, unsigned numops,
Chris Lattnerff7fb602010-02-28 21:53:42 +00001041 bool hasChain, bool hasInFlag, bool hasOutFlag,
1042 bool hasmemrefs,
Chris Lattner9a215002010-02-28 20:55:18 +00001043 int numfixedarityoperands, const PatternToMatch &pattern)
Chris Lattnere86097a2010-02-28 02:31:26 +00001044 : EmitNodeMatcherCommon(opcodeName, vts, numvts, operands, numops, hasChain,
Chris Lattnerff7fb602010-02-28 21:53:42 +00001045 hasInFlag, hasOutFlag, hasmemrefs,
1046 numfixedarityoperands, true),
Chris Lattnere86097a2010-02-28 02:31:26 +00001047 Pattern(pattern) {
1048 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001049
Chris Lattnere86097a2010-02-28 02:31:26 +00001050 const PatternToMatch &getPattern() const { return Pattern; }
1051
1052 static inline bool classof(const Matcher *N) {
Chris Lattner9a215002010-02-28 20:55:18 +00001053 return N->getKind() == MorphNodeTo;
Chris Lattnere86097a2010-02-28 02:31:26 +00001054 }
1055};
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001056
Chris Lattner8950bca2010-12-23 17:03:20 +00001057/// MarkGlueResultsMatcher - This node indicates which non-root nodes in the
1058/// pattern produce glue. This allows CompleteMatchMatcher to update them
1059/// with the output glue of the resultant code.
1060class MarkGlueResultsMatcher : public Matcher {
1061 SmallVector<unsigned, 3> GlueResultNodes;
Chris Lattner02f73582010-02-24 05:33:42 +00001062public:
Chris Lattner8950bca2010-12-23 17:03:20 +00001063 MarkGlueResultsMatcher(const unsigned *nodes, unsigned NumNodes)
1064 : Matcher(MarkGlueResults), GlueResultNodes(nodes, nodes+NumNodes) {}
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001065
Chris Lattner8950bca2010-12-23 17:03:20 +00001066 unsigned getNumNodes() const { return GlueResultNodes.size(); }
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001067
Chris Lattner02f73582010-02-24 05:33:42 +00001068 unsigned getNode(unsigned i) const {
Chris Lattner8950bca2010-12-23 17:03:20 +00001069 assert(i < GlueResultNodes.size());
1070 return GlueResultNodes[i];
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001071 }
1072
Chris Lattnerb21ba712010-02-25 02:04:40 +00001073 static inline bool classof(const Matcher *N) {
Chris Lattner8950bca2010-12-23 17:03:20 +00001074 return N->getKind() == MarkGlueResults;
Chris Lattner02f73582010-02-24 05:33:42 +00001075 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001076
Chris Lattner58aa8342010-02-25 06:49:58 +00001077private:
Chris Lattnera5028a62010-02-25 06:53:39 +00001078 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner58aa8342010-02-25 06:49:58 +00001079 virtual bool isEqualImpl(const Matcher *M) const {
Chris Lattner8950bca2010-12-23 17:03:20 +00001080 return cast<MarkGlueResultsMatcher>(M)->GlueResultNodes == GlueResultNodes;
Chris Lattner58aa8342010-02-25 06:49:58 +00001081 }
1082 virtual unsigned getHashImpl() const;
Chris Lattner02f73582010-02-24 05:33:42 +00001083};
1084
Chris Lattnerb21ba712010-02-25 02:04:40 +00001085/// CompleteMatchMatcher - Complete a match by replacing the results of the
Chris Lattner77f2e272010-02-21 06:03:07 +00001086/// pattern with the newly generated nodes. This also prints a comment
1087/// indicating the source and dest patterns.
Chris Lattnerb21ba712010-02-25 02:04:40 +00001088class CompleteMatchMatcher : public Matcher {
Chris Lattner77f2e272010-02-21 06:03:07 +00001089 SmallVector<unsigned, 2> Results;
Chris Lattner8e946be2010-02-21 03:22:59 +00001090 const PatternToMatch &Pattern;
1091public:
Chris Lattnerb21ba712010-02-25 02:04:40 +00001092 CompleteMatchMatcher(const unsigned *results, unsigned numresults,
Chris Lattnerc78f2a32010-02-28 20:49:53 +00001093 const PatternToMatch &pattern)
Chris Lattnerb21ba712010-02-25 02:04:40 +00001094 : Matcher(CompleteMatch), Results(results, results+numresults),
Chris Lattner77f2e272010-02-21 06:03:07 +00001095 Pattern(pattern) {}
1096
1097 unsigned getNumResults() const { return Results.size(); }
1098 unsigned getResult(unsigned R) const { return Results[R]; }
Chris Lattner8e946be2010-02-21 03:22:59 +00001099 const PatternToMatch &getPattern() const { return Pattern; }
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001100
Chris Lattnerb21ba712010-02-25 02:04:40 +00001101 static inline bool classof(const Matcher *N) {
Chris Lattner77f2e272010-02-21 06:03:07 +00001102 return N->getKind() == CompleteMatch;
Chris Lattner8e946be2010-02-21 03:22:59 +00001103 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001104
Chris Lattner58aa8342010-02-25 06:49:58 +00001105private:
Chris Lattnera5028a62010-02-25 06:53:39 +00001106 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner58aa8342010-02-25 06:49:58 +00001107 virtual bool isEqualImpl(const Matcher *M) const {
1108 return cast<CompleteMatchMatcher>(M)->Results == Results &&
1109 &cast<CompleteMatchMatcher>(M)->Pattern == &Pattern;
1110 }
1111 virtual unsigned getHashImpl() const;
Chris Lattner8e946be2010-02-21 03:22:59 +00001112};
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001113
Chris Lattnerda272d12010-02-15 08:04:42 +00001114} // end namespace llvm
1115
1116#endif