blob: 70031fa6d3b801b0ce0799bcf64ae87520aa26e8 [file] [log] [blame]
Chris Lattnerda272d12010-02-15 08:04:42 +00001//===- DAGISelMatcher.h - Representation of DAG pattern matcher -----------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef TBLGEN_DAGISELMATCHER_H
11#define TBLGEN_DAGISELMATCHER_H
12
13#include "llvm/ADT/OwningPtr.h"
Chris Lattner8e946be2010-02-21 03:22:59 +000014#include "llvm/ADT/SmallVector.h"
Chandler Carruth4ffd89f2012-12-04 10:37:14 +000015#include "llvm/ADT/StringRef.h"
16#include "llvm/CodeGen/ValueTypes.h"
Chris Lattner050a03d2010-02-16 07:21:10 +000017#include "llvm/Support/Casting.h"
Chris Lattnerda272d12010-02-15 08:04:42 +000018
19namespace llvm {
Jim Grosbach4a6d7352011-03-11 02:19:02 +000020 struct CodeGenRegister;
Chris Lattnerda272d12010-02-15 08:04:42 +000021 class CodeGenDAGPatterns;
Chris Lattnerb21ba712010-02-25 02:04:40 +000022 class Matcher;
Chris Lattnerda272d12010-02-15 08:04:42 +000023 class PatternToMatch;
24 class raw_ostream;
25 class ComplexPattern;
Chris Lattner845c0422010-02-18 22:03:03 +000026 class Record;
Chris Lattnera230f962010-02-27 21:48:43 +000027 class SDNodeInfo;
Chris Lattner54379062011-04-17 21:38:24 +000028 class TreePredicateFn;
29 class TreePattern;
Chris Lattnerda272d12010-02-15 08:04:42 +000030
Chris Lattnerfa342fa2010-03-01 07:17:40 +000031Matcher *ConvertPatternToMatcher(const PatternToMatch &Pattern,unsigned Variant,
Chris Lattnerb21ba712010-02-25 02:04:40 +000032 const CodeGenDAGPatterns &CGP);
Chris Lattnerc78f2a32010-02-28 20:49:53 +000033Matcher *OptimizeMatcher(Matcher *Matcher, const CodeGenDAGPatterns &CGP);
Chris Lattner4d0c9312010-03-01 01:54:19 +000034void EmitMatcherTable(const Matcher *Matcher, const CodeGenDAGPatterns &CGP,
Jim Grosbach4a6d7352011-03-11 02:19:02 +000035 raw_ostream &OS);
Chris Lattnerda272d12010-02-15 08:04:42 +000036
Jim Grosbachfbadcd02010-12-21 16:16:00 +000037
Sylvestre Ledruc8e41c52012-07-23 08:51:15 +000038/// Matcher - Base class for all the DAG ISel Matcher representation
Chris Lattnerda272d12010-02-15 08:04:42 +000039/// nodes.
Chris Lattnerb21ba712010-02-25 02:04:40 +000040class Matcher {
Chris Lattnerbd8227f2010-02-18 02:53:41 +000041 // The next matcher node that is executed after this one. Null if this is the
42 // last stage of a match.
Chris Lattnerb21ba712010-02-25 02:04:40 +000043 OwningPtr<Matcher> Next;
David Blaikie2d24e2a2011-12-20 02:50:00 +000044 virtual void anchor();
Chris Lattnerda272d12010-02-15 08:04:42 +000045public:
46 enum KindTy {
Chris Lattner8e946be2010-02-21 03:22:59 +000047 // Matcher state manipulation.
Chris Lattner60df53e2010-02-25 01:56:48 +000048 Scope, // Push a checking scope.
Chris Lattner8e946be2010-02-21 03:22:59 +000049 RecordNode, // Record the current node.
Chris Lattner19b5a752010-02-24 07:31:45 +000050 RecordChild, // Record a child of the current node.
Chris Lattner8e946be2010-02-21 03:22:59 +000051 RecordMemRef, // Record the memref in the current node.
Chris Lattner8950bca2010-12-23 17:03:20 +000052 CaptureGlueInput, // If the current node has an input glue, save it.
Chris Lattner8e946be2010-02-21 03:22:59 +000053 MoveChild, // Move current node to specified child.
54 MoveParent, // Move current node to parent.
Jim Grosbachfbadcd02010-12-21 16:16:00 +000055
Chris Lattner845c0422010-02-18 22:03:03 +000056 // Predicate checking.
Chris Lattner8e946be2010-02-21 03:22:59 +000057 CheckSame, // Fail if not same as prev match.
Craig Topper936910d2013-10-05 05:38:16 +000058 CheckChildSame, // Fail if child not same as prev match.
Chris Lattnerda272d12010-02-15 08:04:42 +000059 CheckPatternPredicate,
Chris Lattner8e946be2010-02-21 03:22:59 +000060 CheckPredicate, // Fail if node predicate fails.
61 CheckOpcode, // Fail if not opcode.
Chris Lattnereb669212010-03-01 06:59:22 +000062 SwitchOpcode, // Dispatch based on opcode.
Chris Lattner8e946be2010-02-21 03:22:59 +000063 CheckType, // Fail if not correct type.
Chris Lattnercfe2eab2010-03-03 06:28:15 +000064 SwitchType, // Dispatch based on type.
Chris Lattner23cfda72010-02-24 20:15:25 +000065 CheckChildType, // Fail if child has wrong type.
Chris Lattner8e946be2010-02-21 03:22:59 +000066 CheckInteger, // Fail if wrong val.
67 CheckCondCode, // Fail if not condcode.
Chris Lattnerda272d12010-02-15 08:04:42 +000068 CheckValueType,
69 CheckComplexPat,
70 CheckAndImm,
Chris Lattnere39650a2010-02-16 06:10:58 +000071 CheckOrImm,
Chris Lattner9a747f12010-02-17 06:23:39 +000072 CheckFoldableChainNode,
Jim Grosbachfbadcd02010-12-21 16:16:00 +000073
Chris Lattner845c0422010-02-18 22:03:03 +000074 // Node creation/emisssion.
Chris Lattner8e946be2010-02-21 03:22:59 +000075 EmitInteger, // Create a TargetConstant
76 EmitStringInteger, // Create a TargetConstant from a string.
77 EmitRegister, // Create a register.
78 EmitConvertToTarget, // Convert a imm/fpimm to target imm/fpimm
79 EmitMergeInputChains, // Merge together a chains for an input.
80 EmitCopyToReg, // Emit a copytoreg into a physreg.
81 EmitNode, // Create a DAG node
82 EmitNodeXForm, // Run a SDNodeXForm
Chris Lattner8950bca2010-12-23 17:03:20 +000083 MarkGlueResults, // Indicate which interior nodes have glue results.
Chris Lattnere86097a2010-02-28 02:31:26 +000084 CompleteMatch, // Finish a match and update the results.
Chris Lattner9a215002010-02-28 20:55:18 +000085 MorphNodeTo // Build a node, finish a match and update results.
Chris Lattnerda272d12010-02-15 08:04:42 +000086 };
87 const KindTy Kind;
Chris Lattner8ef9c792010-02-18 02:49:24 +000088
Chris Lattnerda272d12010-02-15 08:04:42 +000089protected:
Chris Lattnerb21ba712010-02-25 02:04:40 +000090 Matcher(KindTy K) : Kind(K) {}
Chris Lattnerda272d12010-02-15 08:04:42 +000091public:
Chris Lattnerb21ba712010-02-25 02:04:40 +000092 virtual ~Matcher() {}
Jim Grosbachfbadcd02010-12-21 16:16:00 +000093
Chris Lattnerda272d12010-02-15 08:04:42 +000094 KindTy getKind() const { return Kind; }
Chris Lattner8ef9c792010-02-18 02:49:24 +000095
Chris Lattnerb21ba712010-02-25 02:04:40 +000096 Matcher *getNext() { return Next.get(); }
97 const Matcher *getNext() const { return Next.get(); }
98 void setNext(Matcher *C) { Next.reset(C); }
99 Matcher *takeNext() { return Next.take(); }
Chris Lattner19b5a752010-02-24 07:31:45 +0000100
Chris Lattnerb21ba712010-02-25 02:04:40 +0000101 OwningPtr<Matcher> &getNextPtr() { return Next; }
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:
Craig Topper936910d2013-10-05 05:38:16 +0000126 case CheckChildSame:
Chris Lattner48aa5752010-03-07 06:29:26 +0000127 case CheckPatternPredicate:
128 case CheckPredicate:
129 case CheckOpcode:
130 case CheckType:
131 case CheckChildType:
132 case CheckInteger:
133 case CheckCondCode:
134 case CheckValueType:
135 case CheckAndImm:
136 case CheckOrImm:
137 case CheckFoldableChainNode:
138 return true;
139 }
140 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000141
Chris Lattner48aa5752010-03-07 06:29:26 +0000142 /// isSimplePredicateOrRecordNode - Return true if this is a record node or
143 /// a simple predicate.
144 bool isSimplePredicateOrRecordNode() const {
145 return isSimplePredicateNode() ||
146 getKind() == RecordNode || getKind() == RecordChild;
147 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000148
Chris Lattner48aa5752010-03-07 06:29:26 +0000149 /// unlinkNode - Unlink the specified node from this chain. If Other == this,
150 /// we unlink the next pointer and return it. Otherwise we unlink Other from
151 /// the list and return this.
152 Matcher *unlinkNode(Matcher *Other);
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000153
Chris Lattner48aa5752010-03-07 06:29:26 +0000154 /// canMoveBefore - Return true if this matcher is the same as Other, or if
155 /// we can move this matcher past all of the nodes in-between Other and this
156 /// node. Other must be equal to or before this.
157 bool canMoveBefore(const Matcher *Other) const;
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000158
Craig Topper78099d12013-09-25 06:40:22 +0000159 /// canMoveBeforeNode - Return true if it is safe to move the current matcher
Chris Lattner48aa5752010-03-07 06:29:26 +0000160 /// across the specified one.
161 bool canMoveBeforeNode(const Matcher *Other) const;
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000162
Chris Lattner82781b92010-02-27 07:49:13 +0000163 /// isContradictory - Return true of these two matchers could never match on
164 /// the same node.
165 bool isContradictory(const Matcher *Other) const {
166 // Since this predicate is reflexive, we canonicalize the ordering so that
167 // we always match a node against nodes with kinds that are greater or equal
168 // to them. For example, we'll pass in a CheckType node as an argument to
169 // the CheckOpcode method, not the other way around.
170 if (getKind() < Other->getKind())
171 return isContradictoryImpl(Other);
172 return Other->isContradictoryImpl(this);
173 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000174
Chris Lattnera5028a62010-02-25 06:53:39 +0000175 void print(raw_ostream &OS, unsigned indent = 0) const;
Chris Lattner82781b92010-02-27 07:49:13 +0000176 void printOne(raw_ostream &OS) const;
Chris Lattnerda272d12010-02-15 08:04:42 +0000177 void dump() const;
Chris Lattner8ef9c792010-02-18 02:49:24 +0000178protected:
Chris Lattnera5028a62010-02-25 06:53:39 +0000179 virtual void printImpl(raw_ostream &OS, unsigned indent) const = 0;
Chris Lattner58aa8342010-02-25 06:49:58 +0000180 virtual bool isEqualImpl(const Matcher *M) const = 0;
181 virtual unsigned getHashImpl() const = 0;
Chris Lattner82781b92010-02-27 07:49:13 +0000182 virtual bool isContradictoryImpl(const Matcher *M) const { return false; }
Chris Lattnerda272d12010-02-15 08:04:42 +0000183};
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000184
Chris Lattnerd6c84722010-02-25 19:00:39 +0000185/// ScopeMatcher - This attempts to match each of its children to find the first
186/// one that successfully matches. If one child fails, it tries the next child.
187/// If none of the children match then this check fails. It never has a 'next'.
Chris Lattnerb21ba712010-02-25 02:04:40 +0000188class ScopeMatcher : public Matcher {
Chris Lattnerd6c84722010-02-25 19:00:39 +0000189 SmallVector<Matcher*, 4> Children;
Chris Lattnerda272d12010-02-15 08:04:42 +0000190public:
Chris Lattnerd6c84722010-02-25 19:00:39 +0000191 ScopeMatcher(Matcher *const *children, unsigned numchildren)
192 : Matcher(Scope), Children(children, children+numchildren) {
Chris Lattnerda272d12010-02-15 08:04:42 +0000193 }
Chris Lattnerd6c84722010-02-25 19:00:39 +0000194 virtual ~ScopeMatcher();
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000195
Chris Lattnerd6c84722010-02-25 19:00:39 +0000196 unsigned getNumChildren() const { return Children.size(); }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000197
Chris Lattnerd6c84722010-02-25 19:00:39 +0000198 Matcher *getChild(unsigned i) { return Children[i]; }
199 const Matcher *getChild(unsigned i) const { return Children[i]; }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000200
Chris Lattnerd6c84722010-02-25 19:00:39 +0000201 void resetChild(unsigned i, Matcher *N) {
202 delete Children[i];
203 Children[i] = N;
204 }
205
206 Matcher *takeChild(unsigned i) {
207 Matcher *Res = Children[i];
208 Children[i] = 0;
209 return Res;
210 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000211
Chris Lattnerca56fea2010-02-26 07:35:27 +0000212 void setNumChildren(unsigned NC) {
213 if (NC < Children.size()) {
214 // delete any children we're about to lose pointers to.
215 for (unsigned i = NC, e = Children.size(); i != e; ++i)
216 delete Children[i];
217 }
218 Children.resize(NC);
219 }
Chris Lattnerda272d12010-02-15 08:04:42 +0000220
Chris Lattnerb21ba712010-02-25 02:04:40 +0000221 static inline bool classof(const Matcher *N) {
Chris Lattner60df53e2010-02-25 01:56:48 +0000222 return N->getKind() == Scope;
Chris Lattnerda272d12010-02-15 08:04:42 +0000223 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000224
Chris Lattner58aa8342010-02-25 06:49:58 +0000225private:
Chris Lattnera5028a62010-02-25 06:53:39 +0000226 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner58aa8342010-02-25 06:49:58 +0000227 virtual bool isEqualImpl(const Matcher *M) const { return false; }
Chris Lattnerd6c84722010-02-25 19:00:39 +0000228 virtual unsigned getHashImpl() const { return 12312; }
Chris Lattnerda272d12010-02-15 08:04:42 +0000229};
230
Chris Lattnerb21ba712010-02-25 02:04:40 +0000231/// RecordMatcher - Save the current node in the operand list.
232class RecordMatcher : public Matcher {
Chris Lattnerc96087b2010-02-17 01:03:09 +0000233 /// WhatFor - This is a string indicating why we're recording this. This
234 /// should only be used for comment generation not anything semantic.
235 std::string WhatFor;
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000236
Chris Lattner0cebe612010-03-01 02:24:17 +0000237 /// ResultNo - The slot number in the RecordedNodes vector that this will be,
238 /// just printed as a comment.
239 unsigned ResultNo;
Chris Lattnerda272d12010-02-15 08:04:42 +0000240public:
Chris Lattner0cebe612010-03-01 02:24:17 +0000241 RecordMatcher(const std::string &whatfor, unsigned resultNo)
242 : Matcher(RecordNode), WhatFor(whatfor), ResultNo(resultNo) {}
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000243
Chris Lattnerc642b842010-02-17 01:27:29 +0000244 const std::string &getWhatFor() const { return WhatFor; }
Chris Lattner0cebe612010-03-01 02:24:17 +0000245 unsigned getResultNo() const { return ResultNo; }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000246
Chris Lattnerb21ba712010-02-25 02:04:40 +0000247 static inline bool classof(const Matcher *N) {
Chris Lattner845c0422010-02-18 22:03:03 +0000248 return N->getKind() == RecordNode;
Chris Lattnerda272d12010-02-15 08:04:42 +0000249 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000250
Chris Lattnerd323fd42010-02-27 06:22:57 +0000251 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
Chris Lattner58aa8342010-02-25 06:49:58 +0000252private:
Chris Lattnera5028a62010-02-25 06:53:39 +0000253 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner58aa8342010-02-25 06:49:58 +0000254 virtual bool isEqualImpl(const Matcher *M) const { return true; }
255 virtual unsigned getHashImpl() const { return 0; }
Chris Lattnerda272d12010-02-15 08:04:42 +0000256};
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000257
Chris Lattnerb21ba712010-02-25 02:04:40 +0000258/// RecordChildMatcher - Save a numbered child of the current node, or fail
Chris Lattner19b5a752010-02-24 07:31:45 +0000259/// the match if it doesn't exist. This is logically equivalent to:
260/// MoveChild N + RecordNode + MoveParent.
Chris Lattnerb21ba712010-02-25 02:04:40 +0000261class RecordChildMatcher : public Matcher {
Chris Lattner19b5a752010-02-24 07:31:45 +0000262 unsigned ChildNo;
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000263
Chris Lattner19b5a752010-02-24 07:31:45 +0000264 /// WhatFor - This is a string indicating why we're recording this. This
265 /// should only be used for comment generation not anything semantic.
266 std::string WhatFor;
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000267
Chris Lattner0cebe612010-03-01 02:24:17 +0000268 /// ResultNo - The slot number in the RecordedNodes vector that this will be,
269 /// just printed as a comment.
270 unsigned ResultNo;
Chris Lattner19b5a752010-02-24 07:31:45 +0000271public:
Chris Lattner0cebe612010-03-01 02:24:17 +0000272 RecordChildMatcher(unsigned childno, const std::string &whatfor,
273 unsigned resultNo)
274 : Matcher(RecordChild), ChildNo(childno), WhatFor(whatfor),
275 ResultNo(resultNo) {}
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000276
Chris Lattner19b5a752010-02-24 07:31:45 +0000277 unsigned getChildNo() const { return ChildNo; }
278 const std::string &getWhatFor() const { return WhatFor; }
Chris Lattner0cebe612010-03-01 02:24:17 +0000279 unsigned getResultNo() const { return ResultNo; }
280
Chris Lattnerb21ba712010-02-25 02:04:40 +0000281 static inline bool classof(const Matcher *N) {
Chris Lattner19b5a752010-02-24 07:31:45 +0000282 return N->getKind() == RecordChild;
283 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000284
Chris Lattnerd323fd42010-02-27 06:22:57 +0000285 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
286
Chris Lattner58aa8342010-02-25 06:49:58 +0000287private:
Chris Lattnera5028a62010-02-25 06:53:39 +0000288 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner58aa8342010-02-25 06:49:58 +0000289 virtual bool isEqualImpl(const Matcher *M) const {
290 return cast<RecordChildMatcher>(M)->getChildNo() == getChildNo();
291 }
292 virtual unsigned getHashImpl() const { return getChildNo(); }
Chris Lattner19b5a752010-02-24 07:31:45 +0000293};
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000294
Chris Lattnerb21ba712010-02-25 02:04:40 +0000295/// RecordMemRefMatcher - Save the current node's memref.
296class RecordMemRefMatcher : public Matcher {
Chris Lattner8e946be2010-02-21 03:22:59 +0000297public:
Chris Lattnerb21ba712010-02-25 02:04:40 +0000298 RecordMemRefMatcher() : Matcher(RecordMemRef) {}
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000299
Chris Lattnerb21ba712010-02-25 02:04:40 +0000300 static inline bool classof(const Matcher *N) {
Chris Lattner8e946be2010-02-21 03:22:59 +0000301 return N->getKind() == RecordMemRef;
302 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000303
Chris Lattnerd323fd42010-02-27 06:22:57 +0000304 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
305
Chris Lattner58aa8342010-02-25 06:49:58 +0000306private:
Chris Lattnera5028a62010-02-25 06:53:39 +0000307 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner58aa8342010-02-25 06:49:58 +0000308 virtual bool isEqualImpl(const Matcher *M) const { return true; }
309 virtual unsigned getHashImpl() const { return 0; }
Chris Lattner8e946be2010-02-21 03:22:59 +0000310};
311
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000312
Chris Lattner8950bca2010-12-23 17:03:20 +0000313/// CaptureGlueInputMatcher - If the current record has a glue input, record
Chris Lattner8e946be2010-02-21 03:22:59 +0000314/// it so that it is used as an input to the generated code.
Chris Lattner8950bca2010-12-23 17:03:20 +0000315class CaptureGlueInputMatcher : public Matcher {
Chris Lattner8e946be2010-02-21 03:22:59 +0000316public:
Chris Lattner8950bca2010-12-23 17:03:20 +0000317 CaptureGlueInputMatcher() : Matcher(CaptureGlueInput) {}
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000318
Chris Lattnerb21ba712010-02-25 02:04:40 +0000319 static inline bool classof(const Matcher *N) {
Chris Lattner8950bca2010-12-23 17:03:20 +0000320 return N->getKind() == CaptureGlueInput;
Chris Lattner8e946be2010-02-21 03:22:59 +0000321 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000322
Chris Lattnerd323fd42010-02-27 06:22:57 +0000323 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
324
Chris Lattner58aa8342010-02-25 06:49:58 +0000325private:
Chris Lattnera5028a62010-02-25 06:53:39 +0000326 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner58aa8342010-02-25 06:49:58 +0000327 virtual bool isEqualImpl(const Matcher *M) const { return true; }
328 virtual unsigned getHashImpl() const { return 0; }
Chris Lattner8e946be2010-02-21 03:22:59 +0000329};
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000330
Chris Lattnerb21ba712010-02-25 02:04:40 +0000331/// MoveChildMatcher - This tells the interpreter to move into the
Chris Lattnerda272d12010-02-15 08:04:42 +0000332/// specified child node.
Chris Lattnerb21ba712010-02-25 02:04:40 +0000333class MoveChildMatcher : public Matcher {
Chris Lattnerda272d12010-02-15 08:04:42 +0000334 unsigned ChildNo;
335public:
Chris Lattnerb21ba712010-02-25 02:04:40 +0000336 MoveChildMatcher(unsigned childNo) : Matcher(MoveChild), ChildNo(childNo) {}
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000337
Chris Lattnerda272d12010-02-15 08:04:42 +0000338 unsigned getChildNo() const { return ChildNo; }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000339
Chris Lattnerb21ba712010-02-25 02:04:40 +0000340 static inline bool classof(const Matcher *N) {
Chris Lattnerda272d12010-02-15 08:04:42 +0000341 return N->getKind() == MoveChild;
342 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000343
Chris Lattnerd323fd42010-02-27 06:22:57 +0000344 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
345
Chris Lattner58aa8342010-02-25 06:49:58 +0000346private:
Chris Lattnera5028a62010-02-25 06:53:39 +0000347 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner58aa8342010-02-25 06:49:58 +0000348 virtual bool isEqualImpl(const Matcher *M) const {
349 return cast<MoveChildMatcher>(M)->getChildNo() == getChildNo();
350 }
351 virtual unsigned getHashImpl() const { return getChildNo(); }
Chris Lattnerda272d12010-02-15 08:04:42 +0000352};
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000353
Chris Lattnerb21ba712010-02-25 02:04:40 +0000354/// MoveParentMatcher - This tells the interpreter to move to the parent
Chris Lattnerda272d12010-02-15 08:04:42 +0000355/// of the current node.
Chris Lattnerb21ba712010-02-25 02:04:40 +0000356class MoveParentMatcher : public Matcher {
Chris Lattnerda272d12010-02-15 08:04:42 +0000357public:
Chris Lattnerb21ba712010-02-25 02:04:40 +0000358 MoveParentMatcher() : Matcher(MoveParent) {}
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000359
Chris Lattnerb21ba712010-02-25 02:04:40 +0000360 static inline bool classof(const Matcher *N) {
Chris Lattnerda272d12010-02-15 08:04:42 +0000361 return N->getKind() == MoveParent;
362 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000363
Chris Lattnerd323fd42010-02-27 06:22:57 +0000364 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
365
Chris Lattner58aa8342010-02-25 06:49:58 +0000366private:
Chris Lattnera5028a62010-02-25 06:53:39 +0000367 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner58aa8342010-02-25 06:49:58 +0000368 virtual bool isEqualImpl(const Matcher *M) const { return true; }
369 virtual unsigned getHashImpl() const { return 0; }
Chris Lattnerda272d12010-02-15 08:04:42 +0000370};
371
Chris Lattnerb21ba712010-02-25 02:04:40 +0000372/// CheckSameMatcher - This checks to see if this node is exactly the same
Chris Lattnerda272d12010-02-15 08:04:42 +0000373/// node as the specified match that was recorded with 'Record'. This is used
374/// when patterns have the same name in them, like '(mul GPR:$in, GPR:$in)'.
Chris Lattnerb21ba712010-02-25 02:04:40 +0000375class CheckSameMatcher : public Matcher {
Chris Lattnerda272d12010-02-15 08:04:42 +0000376 unsigned MatchNumber;
377public:
Chris Lattnerb21ba712010-02-25 02:04:40 +0000378 CheckSameMatcher(unsigned matchnumber)
Chris Lattner58aa8342010-02-25 06:49:58 +0000379 : Matcher(CheckSame), MatchNumber(matchnumber) {}
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000380
Chris Lattnerda272d12010-02-15 08:04:42 +0000381 unsigned getMatchNumber() const { return MatchNumber; }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000382
Chris Lattnerb21ba712010-02-25 02:04:40 +0000383 static inline bool classof(const Matcher *N) {
Chris Lattnerda272d12010-02-15 08:04:42 +0000384 return N->getKind() == CheckSame;
385 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000386
Chris Lattnerd323fd42010-02-27 06:22:57 +0000387 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
388
Chris Lattner58aa8342010-02-25 06:49:58 +0000389private:
Chris Lattnera5028a62010-02-25 06:53:39 +0000390 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner58aa8342010-02-25 06:49:58 +0000391 virtual bool isEqualImpl(const Matcher *M) const {
392 return cast<CheckSameMatcher>(M)->getMatchNumber() == getMatchNumber();
393 }
394 virtual unsigned getHashImpl() const { return getMatchNumber(); }
Chris Lattnerda272d12010-02-15 08:04:42 +0000395};
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000396
Craig Topper936910d2013-10-05 05:38:16 +0000397/// CheckChildSameMatcher - This checks to see if child node is exactly the same
398/// node as the specified match that was recorded with 'Record'. This is used
399/// when patterns have the same name in them, like '(mul GPR:$in, GPR:$in)'.
400class CheckChildSameMatcher : public Matcher {
401 unsigned ChildNo;
402 unsigned MatchNumber;
403public:
404 CheckChildSameMatcher(unsigned childno, unsigned matchnumber)
405 : Matcher(CheckChildSame), ChildNo(childno), MatchNumber(matchnumber) {}
406
407 unsigned getChildNo() const { return ChildNo; }
408 unsigned getMatchNumber() const { return MatchNumber; }
409
410 static inline bool classof(const Matcher *N) {
411 return N->getKind() == CheckChildSame;
412 }
413
414 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
415
416private:
417 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
418 virtual bool isEqualImpl(const Matcher *M) const {
419 return cast<CheckChildSameMatcher>(M)->ChildNo == ChildNo &&
420 cast<CheckChildSameMatcher>(M)->MatchNumber == MatchNumber;
421 }
422 virtual unsigned getHashImpl() const { return (MatchNumber << 2) | ChildNo; }
423};
424
Chris Lattnerb21ba712010-02-25 02:04:40 +0000425/// CheckPatternPredicateMatcher - This checks the target-specific predicate
Chris Lattnerda272d12010-02-15 08:04:42 +0000426/// to see if the entire pattern is capable of matching. This predicate does
427/// not take a node as input. This is used for subtarget feature checks etc.
Chris Lattnerb21ba712010-02-25 02:04:40 +0000428class CheckPatternPredicateMatcher : public Matcher {
Chris Lattnerda272d12010-02-15 08:04:42 +0000429 std::string Predicate;
430public:
Chris Lattnerb21ba712010-02-25 02:04:40 +0000431 CheckPatternPredicateMatcher(StringRef predicate)
Chris Lattner58aa8342010-02-25 06:49:58 +0000432 : Matcher(CheckPatternPredicate), Predicate(predicate) {}
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000433
Chris Lattnerda272d12010-02-15 08:04:42 +0000434 StringRef getPredicate() const { return Predicate; }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000435
Chris Lattnerb21ba712010-02-25 02:04:40 +0000436 static inline bool classof(const Matcher *N) {
Chris Lattnerda272d12010-02-15 08:04:42 +0000437 return N->getKind() == CheckPatternPredicate;
438 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000439
Chris Lattnerd323fd42010-02-27 06:22:57 +0000440 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
441
Chris Lattner58aa8342010-02-25 06:49:58 +0000442private:
Chris Lattnera5028a62010-02-25 06:53:39 +0000443 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner58aa8342010-02-25 06:49:58 +0000444 virtual bool isEqualImpl(const Matcher *M) const {
445 return cast<CheckPatternPredicateMatcher>(M)->getPredicate() == Predicate;
446 }
447 virtual unsigned getHashImpl() const;
Chris Lattnerda272d12010-02-15 08:04:42 +0000448};
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000449
Chris Lattnerb21ba712010-02-25 02:04:40 +0000450/// CheckPredicateMatcher - This checks the target-specific predicate to
Chris Lattnerda272d12010-02-15 08:04:42 +0000451/// see if the node is acceptable.
Chris Lattnerb21ba712010-02-25 02:04:40 +0000452class CheckPredicateMatcher : public Matcher {
Chris Lattner54379062011-04-17 21:38:24 +0000453 TreePattern *Pred;
Chris Lattnerda272d12010-02-15 08:04:42 +0000454public:
Chris Lattner54379062011-04-17 21:38:24 +0000455 CheckPredicateMatcher(const TreePredicateFn &pred);
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000456
Chris Lattner54379062011-04-17 21:38:24 +0000457 TreePredicateFn getPredicate() const;
Chris Lattnerda272d12010-02-15 08:04:42 +0000458
Chris Lattnerb21ba712010-02-25 02:04:40 +0000459 static inline bool classof(const Matcher *N) {
Chris Lattnerda272d12010-02-15 08:04:42 +0000460 return N->getKind() == CheckPredicate;
461 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000462
Chris Lattnerd323fd42010-02-27 06:22:57 +0000463 // TODO: Ok?
464 //virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
465
Chris Lattner58aa8342010-02-25 06:49:58 +0000466private:
Chris Lattnera5028a62010-02-25 06:53:39 +0000467 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner58aa8342010-02-25 06:49:58 +0000468 virtual bool isEqualImpl(const Matcher *M) const {
Chris Lattner54379062011-04-17 21:38:24 +0000469 return cast<CheckPredicateMatcher>(M)->Pred == Pred;
Chris Lattner58aa8342010-02-25 06:49:58 +0000470 }
471 virtual unsigned getHashImpl() const;
Chris Lattnerda272d12010-02-15 08:04:42 +0000472};
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000473
474
Chris Lattnerb21ba712010-02-25 02:04:40 +0000475/// CheckOpcodeMatcher - This checks to see if the current node has the
Chris Lattnerda272d12010-02-15 08:04:42 +0000476/// specified opcode, if not it fails to match.
Chris Lattnerb21ba712010-02-25 02:04:40 +0000477class CheckOpcodeMatcher : public Matcher {
Chris Lattnera230f962010-02-27 21:48:43 +0000478 const SDNodeInfo &Opcode;
Chris Lattnerda272d12010-02-15 08:04:42 +0000479public:
Chris Lattnera230f962010-02-27 21:48:43 +0000480 CheckOpcodeMatcher(const SDNodeInfo &opcode)
481 : Matcher(CheckOpcode), Opcode(opcode) {}
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000482
Chris Lattnera230f962010-02-27 21:48:43 +0000483 const SDNodeInfo &getOpcode() const { return Opcode; }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000484
Chris Lattnerb21ba712010-02-25 02:04:40 +0000485 static inline bool classof(const Matcher *N) {
Chris Lattnerda272d12010-02-15 08:04:42 +0000486 return N->getKind() == CheckOpcode;
487 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000488
Chris Lattnerd323fd42010-02-27 06:22:57 +0000489 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
490
Chris Lattner58aa8342010-02-25 06:49:58 +0000491private:
Chris Lattnera5028a62010-02-25 06:53:39 +0000492 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattnereb669212010-03-01 06:59:22 +0000493 virtual bool isEqualImpl(const Matcher *M) const;
Chris Lattner58aa8342010-02-25 06:49:58 +0000494 virtual unsigned getHashImpl() const;
Chris Lattner82781b92010-02-27 07:49:13 +0000495 virtual bool isContradictoryImpl(const Matcher *M) const;
Chris Lattnerda272d12010-02-15 08:04:42 +0000496};
Chris Lattnereb669212010-03-01 06:59:22 +0000497
498/// SwitchOpcodeMatcher - Switch based on the current node's opcode, dispatching
499/// to one matcher per opcode. If the opcode doesn't match any of the cases,
500/// then the match fails. This is semantically equivalent to a Scope node where
501/// every child does a CheckOpcode, but is much faster.
502class SwitchOpcodeMatcher : public Matcher {
503 SmallVector<std::pair<const SDNodeInfo*, Matcher*>, 8> Cases;
504public:
505 SwitchOpcodeMatcher(const std::pair<const SDNodeInfo*, Matcher*> *cases,
506 unsigned numcases)
507 : Matcher(SwitchOpcode), Cases(cases, cases+numcases) {}
508
509 static inline bool classof(const Matcher *N) {
510 return N->getKind() == SwitchOpcode;
511 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000512
Chris Lattnereb669212010-03-01 06:59:22 +0000513 unsigned getNumCases() const { return Cases.size(); }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000514
Chris Lattnereb669212010-03-01 06:59:22 +0000515 const SDNodeInfo &getCaseOpcode(unsigned i) const { return *Cases[i].first; }
516 Matcher *getCaseMatcher(unsigned i) { return Cases[i].second; }
517 const Matcher *getCaseMatcher(unsigned i) const { return Cases[i].second; }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000518
Chris Lattnereb669212010-03-01 06:59:22 +0000519private:
520 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
521 virtual bool isEqualImpl(const Matcher *M) const { return false; }
522 virtual unsigned getHashImpl() const { return 4123; }
523};
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000524
Chris Lattnerb21ba712010-02-25 02:04:40 +0000525/// CheckTypeMatcher - This checks to see if the current node has the
Chris Lattner084df622010-03-24 00:41:19 +0000526/// specified type at the specified result, if not it fails to match.
Chris Lattnerb21ba712010-02-25 02:04:40 +0000527class CheckTypeMatcher : public Matcher {
Chris Lattnerda272d12010-02-15 08:04:42 +0000528 MVT::SimpleValueType Type;
Chris Lattner084df622010-03-24 00:41:19 +0000529 unsigned ResNo;
Chris Lattnerda272d12010-02-15 08:04:42 +0000530public:
Chris Lattner084df622010-03-24 00:41:19 +0000531 CheckTypeMatcher(MVT::SimpleValueType type, unsigned resno)
532 : Matcher(CheckType), Type(type), ResNo(resno) {}
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000533
Chris Lattnerda272d12010-02-15 08:04:42 +0000534 MVT::SimpleValueType getType() const { return Type; }
Chris Lattner084df622010-03-24 00:41:19 +0000535 unsigned getResNo() const { return ResNo; }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000536
Chris Lattnerb21ba712010-02-25 02:04:40 +0000537 static inline bool classof(const Matcher *N) {
Chris Lattnerda272d12010-02-15 08:04:42 +0000538 return N->getKind() == CheckType;
539 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000540
Chris Lattnerd323fd42010-02-27 06:22:57 +0000541 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
542
Chris Lattner58aa8342010-02-25 06:49:58 +0000543private:
Chris Lattnera5028a62010-02-25 06:53:39 +0000544 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner58aa8342010-02-25 06:49:58 +0000545 virtual bool isEqualImpl(const Matcher *M) const {
Chris Lattner38717f62010-02-26 08:05:36 +0000546 return cast<CheckTypeMatcher>(M)->Type == Type;
Chris Lattner58aa8342010-02-25 06:49:58 +0000547 }
548 virtual unsigned getHashImpl() const { return Type; }
Chris Lattner82781b92010-02-27 07:49:13 +0000549 virtual bool isContradictoryImpl(const Matcher *M) const;
Chris Lattnerda272d12010-02-15 08:04:42 +0000550};
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000551
Chris Lattnercfe2eab2010-03-03 06:28:15 +0000552/// SwitchTypeMatcher - Switch based on the current node's type, dispatching
553/// to one matcher per case. If the type doesn't match any of the cases,
554/// then the match fails. This is semantically equivalent to a Scope node where
555/// every child does a CheckType, but is much faster.
556class SwitchTypeMatcher : public Matcher {
557 SmallVector<std::pair<MVT::SimpleValueType, Matcher*>, 8> Cases;
558public:
559 SwitchTypeMatcher(const std::pair<MVT::SimpleValueType, Matcher*> *cases,
560 unsigned numcases)
561 : Matcher(SwitchType), Cases(cases, cases+numcases) {}
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000562
Chris Lattnercfe2eab2010-03-03 06:28:15 +0000563 static inline bool classof(const Matcher *N) {
564 return N->getKind() == SwitchType;
565 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000566
Chris Lattnercfe2eab2010-03-03 06:28:15 +0000567 unsigned getNumCases() const { return Cases.size(); }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000568
Chris Lattnercfe2eab2010-03-03 06:28:15 +0000569 MVT::SimpleValueType getCaseType(unsigned i) const { return Cases[i].first; }
570 Matcher *getCaseMatcher(unsigned i) { return Cases[i].second; }
571 const Matcher *getCaseMatcher(unsigned i) const { return Cases[i].second; }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000572
Chris Lattnercfe2eab2010-03-03 06:28:15 +0000573private:
574 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
575 virtual bool isEqualImpl(const Matcher *M) const { return false; }
576 virtual unsigned getHashImpl() const { return 4123; }
577};
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000578
579
Chris Lattnerb21ba712010-02-25 02:04:40 +0000580/// CheckChildTypeMatcher - This checks to see if a child node has the
Chris Lattner23cfda72010-02-24 20:15:25 +0000581/// specified type, if not it fails to match.
Chris Lattnerb21ba712010-02-25 02:04:40 +0000582class CheckChildTypeMatcher : public Matcher {
Chris Lattner23cfda72010-02-24 20:15:25 +0000583 unsigned ChildNo;
584 MVT::SimpleValueType Type;
585public:
Chris Lattnerb21ba712010-02-25 02:04:40 +0000586 CheckChildTypeMatcher(unsigned childno, MVT::SimpleValueType type)
587 : Matcher(CheckChildType), ChildNo(childno), Type(type) {}
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000588
Chris Lattner23cfda72010-02-24 20:15:25 +0000589 unsigned getChildNo() const { return ChildNo; }
590 MVT::SimpleValueType getType() const { return Type; }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000591
Chris Lattnerb21ba712010-02-25 02:04:40 +0000592 static inline bool classof(const Matcher *N) {
Chris Lattner23cfda72010-02-24 20:15:25 +0000593 return N->getKind() == CheckChildType;
594 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000595
Chris Lattnerd323fd42010-02-27 06:22:57 +0000596 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
597
Chris Lattner58aa8342010-02-25 06:49:58 +0000598private:
Chris Lattnera5028a62010-02-25 06:53:39 +0000599 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner58aa8342010-02-25 06:49:58 +0000600 virtual bool isEqualImpl(const Matcher *M) const {
601 return cast<CheckChildTypeMatcher>(M)->ChildNo == ChildNo &&
602 cast<CheckChildTypeMatcher>(M)->Type == Type;
603 }
604 virtual unsigned getHashImpl() const { return (Type << 3) | ChildNo; }
Chris Lattner82781b92010-02-27 07:49:13 +0000605 virtual bool isContradictoryImpl(const Matcher *M) const;
Chris Lattner23cfda72010-02-24 20:15:25 +0000606};
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000607
Chris Lattnerda272d12010-02-15 08:04:42 +0000608
Chris Lattnerb21ba712010-02-25 02:04:40 +0000609/// CheckIntegerMatcher - This checks to see if the current node is a
Chris Lattnerda272d12010-02-15 08:04:42 +0000610/// ConstantSDNode with the specified integer value, if not it fails to match.
Chris Lattnerb21ba712010-02-25 02:04:40 +0000611class CheckIntegerMatcher : public Matcher {
Chris Lattnerda272d12010-02-15 08:04:42 +0000612 int64_t Value;
613public:
Chris Lattnerb21ba712010-02-25 02:04:40 +0000614 CheckIntegerMatcher(int64_t value)
615 : Matcher(CheckInteger), Value(value) {}
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000616
Chris Lattnerda272d12010-02-15 08:04:42 +0000617 int64_t getValue() const { return Value; }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000618
Chris Lattnerb21ba712010-02-25 02:04:40 +0000619 static inline bool classof(const Matcher *N) {
Chris Lattnerda272d12010-02-15 08:04:42 +0000620 return N->getKind() == CheckInteger;
621 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000622
Chris Lattnerd323fd42010-02-27 06:22:57 +0000623 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
624
Chris Lattner58aa8342010-02-25 06:49:58 +0000625private:
Chris Lattnera5028a62010-02-25 06:53:39 +0000626 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner58aa8342010-02-25 06:49:58 +0000627 virtual bool isEqualImpl(const Matcher *M) const {
628 return cast<CheckIntegerMatcher>(M)->Value == Value;
629 }
630 virtual unsigned getHashImpl() const { return Value; }
Chris Lattner24789622010-02-27 08:11:15 +0000631 virtual bool isContradictoryImpl(const Matcher *M) const;
Chris Lattnerda272d12010-02-15 08:04:42 +0000632};
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000633
Chris Lattnerb21ba712010-02-25 02:04:40 +0000634/// CheckCondCodeMatcher - This checks to see if the current node is a
Chris Lattnerda272d12010-02-15 08:04:42 +0000635/// CondCodeSDNode with the specified condition, if not it fails to match.
Chris Lattnerb21ba712010-02-25 02:04:40 +0000636class CheckCondCodeMatcher : public Matcher {
Chris Lattnerda272d12010-02-15 08:04:42 +0000637 StringRef CondCodeName;
638public:
Chris Lattnerb21ba712010-02-25 02:04:40 +0000639 CheckCondCodeMatcher(StringRef condcodename)
640 : Matcher(CheckCondCode), CondCodeName(condcodename) {}
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000641
Chris Lattnerda272d12010-02-15 08:04:42 +0000642 StringRef getCondCodeName() const { return CondCodeName; }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000643
Chris Lattnerb21ba712010-02-25 02:04:40 +0000644 static inline bool classof(const Matcher *N) {
Chris Lattnerda272d12010-02-15 08:04:42 +0000645 return N->getKind() == CheckCondCode;
646 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000647
Chris Lattnerd323fd42010-02-27 06:22:57 +0000648 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
649
Chris Lattner58aa8342010-02-25 06:49:58 +0000650private:
Chris Lattnera5028a62010-02-25 06:53:39 +0000651 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner58aa8342010-02-25 06:49:58 +0000652 virtual bool isEqualImpl(const Matcher *M) const {
653 return cast<CheckCondCodeMatcher>(M)->CondCodeName == CondCodeName;
654 }
655 virtual unsigned getHashImpl() const;
Chris Lattnerda272d12010-02-15 08:04:42 +0000656};
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000657
Chris Lattnerb21ba712010-02-25 02:04:40 +0000658/// CheckValueTypeMatcher - This checks to see if the current node is a
Chris Lattnerda272d12010-02-15 08:04:42 +0000659/// VTSDNode with the specified type, if not it fails to match.
Chris Lattnerb21ba712010-02-25 02:04:40 +0000660class CheckValueTypeMatcher : public Matcher {
Chris Lattnerda272d12010-02-15 08:04:42 +0000661 StringRef TypeName;
662public:
Chris Lattnerb21ba712010-02-25 02:04:40 +0000663 CheckValueTypeMatcher(StringRef type_name)
664 : Matcher(CheckValueType), TypeName(type_name) {}
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000665
Chris Lattnerda272d12010-02-15 08:04:42 +0000666 StringRef getTypeName() const { return TypeName; }
667
Chris Lattnerb21ba712010-02-25 02:04:40 +0000668 static inline bool classof(const Matcher *N) {
Chris Lattnerda272d12010-02-15 08:04:42 +0000669 return N->getKind() == CheckValueType;
670 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000671
Chris Lattnerd323fd42010-02-27 06:22:57 +0000672 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
673
Chris Lattner58aa8342010-02-25 06:49:58 +0000674private:
Chris Lattnera5028a62010-02-25 06:53:39 +0000675 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner58aa8342010-02-25 06:49:58 +0000676 virtual bool isEqualImpl(const Matcher *M) const {
677 return cast<CheckValueTypeMatcher>(M)->TypeName == TypeName;
678 }
679 virtual unsigned getHashImpl() const;
Chris Lattner48aa5752010-03-07 06:29:26 +0000680 bool isContradictoryImpl(const Matcher *M) const;
Chris Lattnerda272d12010-02-15 08:04:42 +0000681};
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000682
683
684
Chris Lattnerb21ba712010-02-25 02:04:40 +0000685/// CheckComplexPatMatcher - This node runs the specified ComplexPattern on
Chris Lattnerda272d12010-02-15 08:04:42 +0000686/// the current node.
Chris Lattnerb21ba712010-02-25 02:04:40 +0000687class CheckComplexPatMatcher : public Matcher {
Chris Lattnerda272d12010-02-15 08:04:42 +0000688 const ComplexPattern &Pattern;
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000689
690 /// MatchNumber - This is the recorded nodes slot that contains the node we
691 /// want to match against.
Chris Lattner57bf8a42010-03-04 01:23:08 +0000692 unsigned MatchNumber;
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000693
Chris Lattner57bf8a42010-03-04 01:23:08 +0000694 /// Name - The name of the node we're matching, for comment emission.
695 std::string Name;
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000696
Chris Lattnerd1aca7c2010-03-04 00:28:05 +0000697 /// FirstResult - This is the first slot in the RecordedNodes list that the
698 /// result of the match populates.
699 unsigned FirstResult;
Chris Lattnerda272d12010-02-15 08:04:42 +0000700public:
Chris Lattner57bf8a42010-03-04 01:23:08 +0000701 CheckComplexPatMatcher(const ComplexPattern &pattern, unsigned matchnumber,
702 const std::string &name, unsigned firstresult)
703 : Matcher(CheckComplexPat), Pattern(pattern), MatchNumber(matchnumber),
704 Name(name), FirstResult(firstresult) {}
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000705
Chris Lattnere609a512010-02-17 00:31:50 +0000706 const ComplexPattern &getPattern() const { return Pattern; }
Chris Lattner57bf8a42010-03-04 01:23:08 +0000707 unsigned getMatchNumber() const { return MatchNumber; }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000708
Chris Lattner57bf8a42010-03-04 01:23:08 +0000709 const std::string getName() const { return Name; }
Chris Lattnerd1aca7c2010-03-04 00:28:05 +0000710 unsigned getFirstResult() const { return FirstResult; }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000711
Chris Lattnerb21ba712010-02-25 02:04:40 +0000712 static inline bool classof(const Matcher *N) {
Chris Lattnerda272d12010-02-15 08:04:42 +0000713 return N->getKind() == CheckComplexPat;
714 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000715
Chris Lattnerd323fd42010-02-27 06:22:57 +0000716 // Not safe to move a pattern predicate past a complex pattern.
717 virtual bool isSafeToReorderWithPatternPredicate() const { return false; }
718
Chris Lattner58aa8342010-02-25 06:49:58 +0000719private:
Chris Lattnera5028a62010-02-25 06:53:39 +0000720 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner58aa8342010-02-25 06:49:58 +0000721 virtual bool isEqualImpl(const Matcher *M) const {
Chris Lattner57bf8a42010-03-04 01:23:08 +0000722 return &cast<CheckComplexPatMatcher>(M)->Pattern == &Pattern &&
723 cast<CheckComplexPatMatcher>(M)->MatchNumber == MatchNumber;
Chris Lattner58aa8342010-02-25 06:49:58 +0000724 }
725 virtual unsigned getHashImpl() const {
Chris Lattner57bf8a42010-03-04 01:23:08 +0000726 return (unsigned)(intptr_t)&Pattern ^ MatchNumber;
Chris Lattner58aa8342010-02-25 06:49:58 +0000727 }
Chris Lattnerda272d12010-02-15 08:04:42 +0000728};
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000729
Chris Lattnerb21ba712010-02-25 02:04:40 +0000730/// CheckAndImmMatcher - This checks to see if the current node is an 'and'
Chris Lattnerda272d12010-02-15 08:04:42 +0000731/// with something equivalent to the specified immediate.
Chris Lattnerb21ba712010-02-25 02:04:40 +0000732class CheckAndImmMatcher : public Matcher {
Chris Lattnerda272d12010-02-15 08:04:42 +0000733 int64_t Value;
734public:
Chris Lattnerb21ba712010-02-25 02:04:40 +0000735 CheckAndImmMatcher(int64_t value)
736 : Matcher(CheckAndImm), Value(value) {}
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000737
Chris Lattnerda272d12010-02-15 08:04:42 +0000738 int64_t getValue() const { return Value; }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000739
Chris Lattnerb21ba712010-02-25 02:04:40 +0000740 static inline bool classof(const Matcher *N) {
Chris Lattnerda272d12010-02-15 08:04:42 +0000741 return N->getKind() == CheckAndImm;
742 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000743
Chris Lattnerd323fd42010-02-27 06:22:57 +0000744 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
745
Chris Lattner58aa8342010-02-25 06:49:58 +0000746private:
Chris Lattnera5028a62010-02-25 06:53:39 +0000747 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner58aa8342010-02-25 06:49:58 +0000748 virtual bool isEqualImpl(const Matcher *M) const {
749 return cast<CheckAndImmMatcher>(M)->Value == Value;
750 }
751 virtual unsigned getHashImpl() const { return Value; }
Chris Lattnerda272d12010-02-15 08:04:42 +0000752};
753
Chris Lattnerb21ba712010-02-25 02:04:40 +0000754/// CheckOrImmMatcher - This checks to see if the current node is an 'and'
Chris Lattnerda272d12010-02-15 08:04:42 +0000755/// with something equivalent to the specified immediate.
Chris Lattnerb21ba712010-02-25 02:04:40 +0000756class CheckOrImmMatcher : public Matcher {
Chris Lattnerda272d12010-02-15 08:04:42 +0000757 int64_t Value;
758public:
Chris Lattnerb21ba712010-02-25 02:04:40 +0000759 CheckOrImmMatcher(int64_t value)
760 : Matcher(CheckOrImm), Value(value) {}
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000761
Chris Lattnerda272d12010-02-15 08:04:42 +0000762 int64_t getValue() const { return Value; }
763
Chris Lattnerb21ba712010-02-25 02:04:40 +0000764 static inline bool classof(const Matcher *N) {
Chris Lattnerda272d12010-02-15 08:04:42 +0000765 return N->getKind() == CheckOrImm;
766 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000767
Chris Lattnerd323fd42010-02-27 06:22:57 +0000768 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
769
Chris Lattner58aa8342010-02-25 06:49:58 +0000770private:
Chris Lattnera5028a62010-02-25 06:53:39 +0000771 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner58aa8342010-02-25 06:49:58 +0000772 virtual bool isEqualImpl(const Matcher *M) const {
773 return cast<CheckOrImmMatcher>(M)->Value == Value;
774 }
775 virtual unsigned getHashImpl() const { return Value; }
Chris Lattnerda272d12010-02-15 08:04:42 +0000776};
Chris Lattnere39650a2010-02-16 06:10:58 +0000777
Chris Lattnerb21ba712010-02-25 02:04:40 +0000778/// CheckFoldableChainNodeMatcher - This checks to see if the current node
Chris Lattner21390d72010-02-16 19:15:55 +0000779/// (which defines a chain operand) is safe to fold into a larger pattern.
Chris Lattnerb21ba712010-02-25 02:04:40 +0000780class CheckFoldableChainNodeMatcher : public Matcher {
Chris Lattnere39650a2010-02-16 06:10:58 +0000781public:
Chris Lattnerb21ba712010-02-25 02:04:40 +0000782 CheckFoldableChainNodeMatcher()
783 : Matcher(CheckFoldableChainNode) {}
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000784
Chris Lattnerb21ba712010-02-25 02:04:40 +0000785 static inline bool classof(const Matcher *N) {
Chris Lattner21390d72010-02-16 19:15:55 +0000786 return N->getKind() == CheckFoldableChainNode;
Chris Lattnere39650a2010-02-16 06:10:58 +0000787 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000788
Chris Lattnerd323fd42010-02-27 06:22:57 +0000789 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
790
Chris Lattner58aa8342010-02-25 06:49:58 +0000791private:
Chris Lattnera5028a62010-02-25 06:53:39 +0000792 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner58aa8342010-02-25 06:49:58 +0000793 virtual bool isEqualImpl(const Matcher *M) const { return true; }
794 virtual unsigned getHashImpl() const { return 0; }
Chris Lattnere39650a2010-02-16 06:10:58 +0000795};
796
Chris Lattnerb21ba712010-02-25 02:04:40 +0000797/// EmitIntegerMatcher - This creates a new TargetConstant.
798class EmitIntegerMatcher : public Matcher {
Chris Lattner845c0422010-02-18 22:03:03 +0000799 int64_t Val;
800 MVT::SimpleValueType VT;
801public:
Chris Lattnerb21ba712010-02-25 02:04:40 +0000802 EmitIntegerMatcher(int64_t val, MVT::SimpleValueType vt)
803 : Matcher(EmitInteger), Val(val), VT(vt) {}
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000804
Chris Lattner906b4992010-02-19 07:49:56 +0000805 int64_t getValue() const { return Val; }
Chris Lattner845c0422010-02-18 22:03:03 +0000806 MVT::SimpleValueType getVT() const { return VT; }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000807
Chris Lattnerb21ba712010-02-25 02:04:40 +0000808 static inline bool classof(const Matcher *N) {
Chris Lattner845c0422010-02-18 22:03:03 +0000809 return N->getKind() == EmitInteger;
810 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000811
Chris Lattner58aa8342010-02-25 06:49:58 +0000812private:
Chris Lattnera5028a62010-02-25 06:53:39 +0000813 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner58aa8342010-02-25 06:49:58 +0000814 virtual bool isEqualImpl(const Matcher *M) const {
815 return cast<EmitIntegerMatcher>(M)->Val == Val &&
816 cast<EmitIntegerMatcher>(M)->VT == VT;
817 }
818 virtual unsigned getHashImpl() const { return (Val << 4) | VT; }
Chris Lattner845c0422010-02-18 22:03:03 +0000819};
Chris Lattner8e946be2010-02-21 03:22:59 +0000820
Chris Lattnerb21ba712010-02-25 02:04:40 +0000821/// EmitStringIntegerMatcher - A target constant whose value is represented
Chris Lattner8e946be2010-02-21 03:22:59 +0000822/// by a string.
Chris Lattnerb21ba712010-02-25 02:04:40 +0000823class EmitStringIntegerMatcher : public Matcher {
Chris Lattner8e946be2010-02-21 03:22:59 +0000824 std::string Val;
825 MVT::SimpleValueType VT;
826public:
Chris Lattnerb21ba712010-02-25 02:04:40 +0000827 EmitStringIntegerMatcher(const std::string &val, MVT::SimpleValueType vt)
828 : Matcher(EmitStringInteger), Val(val), VT(vt) {}
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000829
Chris Lattner8e946be2010-02-21 03:22:59 +0000830 const std::string &getValue() const { return Val; }
831 MVT::SimpleValueType getVT() const { return VT; }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000832
Chris Lattnerb21ba712010-02-25 02:04:40 +0000833 static inline bool classof(const Matcher *N) {
Chris Lattner8e946be2010-02-21 03:22:59 +0000834 return N->getKind() == EmitStringInteger;
835 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000836
Chris Lattner58aa8342010-02-25 06:49:58 +0000837private:
Chris Lattnera5028a62010-02-25 06:53:39 +0000838 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner58aa8342010-02-25 06:49:58 +0000839 virtual bool isEqualImpl(const Matcher *M) const {
840 return cast<EmitStringIntegerMatcher>(M)->Val == Val &&
841 cast<EmitStringIntegerMatcher>(M)->VT == VT;
842 }
843 virtual unsigned getHashImpl() const;
Chris Lattner8e946be2010-02-21 03:22:59 +0000844};
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000845
Chris Lattnerb21ba712010-02-25 02:04:40 +0000846/// EmitRegisterMatcher - This creates a new TargetConstant.
847class EmitRegisterMatcher : public Matcher {
Chris Lattner845c0422010-02-18 22:03:03 +0000848 /// Reg - The def for the register that we're emitting. If this is null, then
849 /// this is a reference to zero_reg.
Jim Grosbach4a6d7352011-03-11 02:19:02 +0000850 const CodeGenRegister *Reg;
Chris Lattner845c0422010-02-18 22:03:03 +0000851 MVT::SimpleValueType VT;
852public:
Jim Grosbach4a6d7352011-03-11 02:19:02 +0000853 EmitRegisterMatcher(const CodeGenRegister *reg, MVT::SimpleValueType vt)
Chris Lattnerb21ba712010-02-25 02:04:40 +0000854 : Matcher(EmitRegister), Reg(reg), VT(vt) {}
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000855
Jim Grosbach4a6d7352011-03-11 02:19:02 +0000856 const CodeGenRegister *getReg() const { return Reg; }
Chris Lattner845c0422010-02-18 22:03:03 +0000857 MVT::SimpleValueType getVT() const { return VT; }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000858
Chris Lattnerb21ba712010-02-25 02:04:40 +0000859 static inline bool classof(const Matcher *N) {
Chris Lattner845c0422010-02-18 22:03:03 +0000860 return N->getKind() == EmitRegister;
861 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000862
Chris Lattner58aa8342010-02-25 06:49:58 +0000863private:
Chris Lattnera5028a62010-02-25 06:53:39 +0000864 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner58aa8342010-02-25 06:49:58 +0000865 virtual bool isEqualImpl(const Matcher *M) const {
866 return cast<EmitRegisterMatcher>(M)->Reg == Reg &&
867 cast<EmitRegisterMatcher>(M)->VT == VT;
868 }
869 virtual unsigned getHashImpl() const {
870 return ((unsigned)(intptr_t)Reg) << 4 | VT;
871 }
Chris Lattner845c0422010-02-18 22:03:03 +0000872};
Chris Lattner8e946be2010-02-21 03:22:59 +0000873
Chris Lattnerb21ba712010-02-25 02:04:40 +0000874/// EmitConvertToTargetMatcher - Emit an operation that reads a specified
Chris Lattner8e946be2010-02-21 03:22:59 +0000875/// recorded node and converts it from being a ISD::Constant to
876/// ISD::TargetConstant, likewise for ConstantFP.
Chris Lattnerb21ba712010-02-25 02:04:40 +0000877class EmitConvertToTargetMatcher : public Matcher {
Chris Lattner8e946be2010-02-21 03:22:59 +0000878 unsigned Slot;
879public:
Chris Lattnerb21ba712010-02-25 02:04:40 +0000880 EmitConvertToTargetMatcher(unsigned slot)
881 : Matcher(EmitConvertToTarget), Slot(slot) {}
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000882
Chris Lattner8e946be2010-02-21 03:22:59 +0000883 unsigned getSlot() const { return Slot; }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000884
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() == EmitConvertToTarget;
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<EmitConvertToTargetMatcher>(M)->Slot == Slot;
893 }
894 virtual unsigned getHashImpl() const { return Slot; }
Chris Lattner8e946be2010-02-21 03:22:59 +0000895};
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000896
Chris Lattnerb21ba712010-02-25 02:04:40 +0000897/// EmitMergeInputChainsMatcher - Emit a node that merges a list of input
Chris Lattner8e946be2010-02-21 03:22:59 +0000898/// chains together with a token factor. The list of nodes are the nodes in the
899/// matched pattern that have chain input/outputs. This node adds all input
900/// chains of these nodes if they are not themselves a node in the pattern.
Chris Lattnerb21ba712010-02-25 02:04:40 +0000901class EmitMergeInputChainsMatcher : public Matcher {
Chris Lattner8e946be2010-02-21 03:22:59 +0000902 SmallVector<unsigned, 3> ChainNodes;
903public:
Chris Lattnerb21ba712010-02-25 02:04:40 +0000904 EmitMergeInputChainsMatcher(const unsigned *nodes, unsigned NumNodes)
905 : Matcher(EmitMergeInputChains), ChainNodes(nodes, nodes+NumNodes) {}
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000906
Chris Lattner8e946be2010-02-21 03:22:59 +0000907 unsigned getNumNodes() const { return ChainNodes.size(); }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000908
Chris Lattner8e946be2010-02-21 03:22:59 +0000909 unsigned getNode(unsigned i) const {
910 assert(i < ChainNodes.size());
911 return ChainNodes[i];
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000912 }
913
Chris Lattnerb21ba712010-02-25 02:04:40 +0000914 static inline bool classof(const Matcher *N) {
Chris Lattner8e946be2010-02-21 03:22:59 +0000915 return N->getKind() == EmitMergeInputChains;
916 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000917
Chris Lattner58aa8342010-02-25 06:49:58 +0000918private:
Chris Lattnera5028a62010-02-25 06:53:39 +0000919 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner58aa8342010-02-25 06:49:58 +0000920 virtual bool isEqualImpl(const Matcher *M) const {
921 return cast<EmitMergeInputChainsMatcher>(M)->ChainNodes == ChainNodes;
922 }
923 virtual unsigned getHashImpl() const;
Chris Lattner8e946be2010-02-21 03:22:59 +0000924};
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000925
Chris Lattnerb21ba712010-02-25 02:04:40 +0000926/// EmitCopyToRegMatcher - Emit a CopyToReg node from a value to a physreg,
Chris Lattner8950bca2010-12-23 17:03:20 +0000927/// pushing the chain and glue results.
Chris Lattner8e946be2010-02-21 03:22:59 +0000928///
Chris Lattnerb21ba712010-02-25 02:04:40 +0000929class EmitCopyToRegMatcher : public Matcher {
Chris Lattner8e946be2010-02-21 03:22:59 +0000930 unsigned SrcSlot; // Value to copy into the physreg.
931 Record *DestPhysReg;
932public:
Chris Lattnerb21ba712010-02-25 02:04:40 +0000933 EmitCopyToRegMatcher(unsigned srcSlot, Record *destPhysReg)
934 : Matcher(EmitCopyToReg), SrcSlot(srcSlot), DestPhysReg(destPhysReg) {}
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000935
Chris Lattner8e946be2010-02-21 03:22:59 +0000936 unsigned getSrcSlot() const { return SrcSlot; }
937 Record *getDestPhysReg() const { return DestPhysReg; }
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() == EmitCopyToReg;
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<EmitCopyToRegMatcher>(M)->SrcSlot == SrcSlot &&
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000947 cast<EmitCopyToRegMatcher>(M)->DestPhysReg == DestPhysReg;
Chris Lattner58aa8342010-02-25 06:49:58 +0000948 }
949 virtual unsigned getHashImpl() const {
950 return SrcSlot ^ ((unsigned)(intptr_t)DestPhysReg << 4);
951 }
Chris Lattner8e946be2010-02-21 03:22:59 +0000952};
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000953
954
955
Chris Lattnerb21ba712010-02-25 02:04:40 +0000956/// EmitNodeXFormMatcher - Emit an operation that runs an SDNodeXForm on a
Chris Lattner8e946be2010-02-21 03:22:59 +0000957/// recorded node and records the result.
Chris Lattnerb21ba712010-02-25 02:04:40 +0000958class EmitNodeXFormMatcher : public Matcher {
Chris Lattner8e946be2010-02-21 03:22:59 +0000959 unsigned Slot;
960 Record *NodeXForm;
961public:
Chris Lattnerb21ba712010-02-25 02:04:40 +0000962 EmitNodeXFormMatcher(unsigned slot, Record *nodeXForm)
963 : Matcher(EmitNodeXForm), Slot(slot), NodeXForm(nodeXForm) {}
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000964
Chris Lattner8e946be2010-02-21 03:22:59 +0000965 unsigned getSlot() const { return Slot; }
966 Record *getNodeXForm() const { return NodeXForm; }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000967
Chris Lattnerb21ba712010-02-25 02:04:40 +0000968 static inline bool classof(const Matcher *N) {
Chris Lattner8e946be2010-02-21 03:22:59 +0000969 return N->getKind() == EmitNodeXForm;
970 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000971
Chris Lattner58aa8342010-02-25 06:49:58 +0000972private:
Chris Lattnera5028a62010-02-25 06:53:39 +0000973 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner58aa8342010-02-25 06:49:58 +0000974 virtual bool isEqualImpl(const Matcher *M) const {
975 return cast<EmitNodeXFormMatcher>(M)->Slot == Slot &&
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000976 cast<EmitNodeXFormMatcher>(M)->NodeXForm == NodeXForm;
Chris Lattner58aa8342010-02-25 06:49:58 +0000977 }
978 virtual unsigned getHashImpl() const {
979 return Slot ^ ((unsigned)(intptr_t)NodeXForm << 4);
980 }
Chris Lattner8e946be2010-02-21 03:22:59 +0000981};
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000982
Chris Lattnere86097a2010-02-28 02:31:26 +0000983/// EmitNodeMatcherCommon - Common class shared between EmitNode and
Chris Lattner9a215002010-02-28 20:55:18 +0000984/// MorphNodeTo.
Chris Lattnere86097a2010-02-28 02:31:26 +0000985class EmitNodeMatcherCommon : public Matcher {
Chris Lattner8e946be2010-02-21 03:22:59 +0000986 std::string OpcodeName;
987 const SmallVector<MVT::SimpleValueType, 3> VTs;
988 const SmallVector<unsigned, 6> Operands;
Chris Lattner036609b2010-12-23 18:28:41 +0000989 bool HasChain, HasInGlue, HasOutGlue, HasMemRefs;
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000990
Chris Lattner8e946be2010-02-21 03:22:59 +0000991 /// NumFixedArityOperands - If this is a fixed arity node, this is set to -1.
992 /// If this is a varidic node, this is set to the number of fixed arity
993 /// operands in the root of the pattern. The rest are appended to this node.
994 int NumFixedArityOperands;
995public:
Chris Lattnere86097a2010-02-28 02:31:26 +0000996 EmitNodeMatcherCommon(const std::string &opcodeName,
997 const MVT::SimpleValueType *vts, unsigned numvts,
998 const unsigned *operands, unsigned numops,
Chris Lattner8950bca2010-12-23 17:03:20 +0000999 bool hasChain, bool hasInGlue, bool hasOutGlue,
Chris Lattnerff7fb602010-02-28 21:53:42 +00001000 bool hasmemrefs,
Chris Lattner9a215002010-02-28 20:55:18 +00001001 int numfixedarityoperands, bool isMorphNodeTo)
1002 : Matcher(isMorphNodeTo ? MorphNodeTo : EmitNode), OpcodeName(opcodeName),
Chris Lattner8e946be2010-02-21 03:22:59 +00001003 VTs(vts, vts+numvts), Operands(operands, operands+numops),
Chris Lattner036609b2010-12-23 18:28:41 +00001004 HasChain(hasChain), HasInGlue(hasInGlue), HasOutGlue(hasOutGlue),
Chris Lattnerff7fb602010-02-28 21:53:42 +00001005 HasMemRefs(hasmemrefs), NumFixedArityOperands(numfixedarityoperands) {}
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001006
Chris Lattner8e946be2010-02-21 03:22:59 +00001007 const std::string &getOpcodeName() const { return OpcodeName; }
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001008
Chris Lattner8e946be2010-02-21 03:22:59 +00001009 unsigned getNumVTs() const { return VTs.size(); }
1010 MVT::SimpleValueType getVT(unsigned i) const {
1011 assert(i < VTs.size());
1012 return VTs[i];
1013 }
Chris Lattnerc78f2a32010-02-28 20:49:53 +00001014
Chris Lattner8e946be2010-02-21 03:22:59 +00001015 unsigned getNumOperands() const { return Operands.size(); }
1016 unsigned getOperand(unsigned i) const {
1017 assert(i < Operands.size());
1018 return Operands[i];
Chris Lattnerc78f2a32010-02-28 20:49:53 +00001019 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001020
Chris Lattnerc78f2a32010-02-28 20:49:53 +00001021 const SmallVectorImpl<MVT::SimpleValueType> &getVTList() const { return VTs; }
1022 const SmallVectorImpl<unsigned> &getOperandList() const { return Operands; }
1023
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001024
Chris Lattner8e946be2010-02-21 03:22:59 +00001025 bool hasChain() const { return HasChain; }
Chris Lattner036609b2010-12-23 18:28:41 +00001026 bool hasInFlag() const { return HasInGlue; }
1027 bool hasOutFlag() const { return HasOutGlue; }
Chris Lattner8e946be2010-02-21 03:22:59 +00001028 bool hasMemRefs() const { return HasMemRefs; }
1029 int getNumFixedArityOperands() const { return NumFixedArityOperands; }
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001030
Chris Lattnerb21ba712010-02-25 02:04:40 +00001031 static inline bool classof(const Matcher *N) {
Chris Lattner9a215002010-02-28 20:55:18 +00001032 return N->getKind() == EmitNode || N->getKind() == MorphNodeTo;
Chris Lattner845c0422010-02-18 22:03:03 +00001033 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001034
Chris Lattner58aa8342010-02-25 06:49:58 +00001035private:
Chris Lattnera5028a62010-02-25 06:53:39 +00001036 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner58aa8342010-02-25 06:49:58 +00001037 virtual bool isEqualImpl(const Matcher *M) const;
1038 virtual unsigned getHashImpl() const;
Chris Lattner845c0422010-02-18 22:03:03 +00001039};
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001040
Chris Lattnere86097a2010-02-28 02:31:26 +00001041/// EmitNodeMatcher - This signals a successful match and generates a node.
1042class EmitNodeMatcher : public EmitNodeMatcherCommon {
David Blaikie2d24e2a2011-12-20 02:50:00 +00001043 virtual void anchor();
Chris Lattner6281cda2010-02-28 02:41:25 +00001044 unsigned FirstResultSlot;
Chris Lattnere86097a2010-02-28 02:31:26 +00001045public:
1046 EmitNodeMatcher(const std::string &opcodeName,
1047 const MVT::SimpleValueType *vts, unsigned numvts,
1048 const unsigned *operands, unsigned numops,
Chris Lattnerff7fb602010-02-28 21:53:42 +00001049 bool hasChain, bool hasInFlag, bool hasOutFlag,
1050 bool hasmemrefs,
Chris Lattner6281cda2010-02-28 02:41:25 +00001051 int numfixedarityoperands, unsigned firstresultslot)
Chris Lattnere86097a2010-02-28 02:31:26 +00001052 : EmitNodeMatcherCommon(opcodeName, vts, numvts, operands, numops, hasChain,
Chris Lattnerff7fb602010-02-28 21:53:42 +00001053 hasInFlag, hasOutFlag, hasmemrefs,
1054 numfixedarityoperands, false),
Chris Lattner6281cda2010-02-28 02:41:25 +00001055 FirstResultSlot(firstresultslot) {}
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001056
Chris Lattner6281cda2010-02-28 02:41:25 +00001057 unsigned getFirstResultSlot() const { return FirstResultSlot; }
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001058
Chris Lattnere86097a2010-02-28 02:31:26 +00001059 static inline bool classof(const Matcher *N) {
1060 return N->getKind() == EmitNode;
1061 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001062
Chris Lattnere86097a2010-02-28 02:31:26 +00001063};
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001064
Chris Lattner9a215002010-02-28 20:55:18 +00001065class MorphNodeToMatcher : public EmitNodeMatcherCommon {
David Blaikie2d24e2a2011-12-20 02:50:00 +00001066 virtual void anchor();
Chris Lattnere86097a2010-02-28 02:31:26 +00001067 const PatternToMatch &Pattern;
1068public:
Chris Lattner9a215002010-02-28 20:55:18 +00001069 MorphNodeToMatcher(const std::string &opcodeName,
1070 const MVT::SimpleValueType *vts, unsigned numvts,
1071 const unsigned *operands, unsigned numops,
Chris Lattnerff7fb602010-02-28 21:53:42 +00001072 bool hasChain, bool hasInFlag, bool hasOutFlag,
1073 bool hasmemrefs,
Chris Lattner9a215002010-02-28 20:55:18 +00001074 int numfixedarityoperands, const PatternToMatch &pattern)
Chris Lattnere86097a2010-02-28 02:31:26 +00001075 : EmitNodeMatcherCommon(opcodeName, vts, numvts, operands, numops, hasChain,
Chris Lattnerff7fb602010-02-28 21:53:42 +00001076 hasInFlag, hasOutFlag, hasmemrefs,
1077 numfixedarityoperands, true),
Chris Lattnere86097a2010-02-28 02:31:26 +00001078 Pattern(pattern) {
1079 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001080
Chris Lattnere86097a2010-02-28 02:31:26 +00001081 const PatternToMatch &getPattern() const { return Pattern; }
1082
1083 static inline bool classof(const Matcher *N) {
Chris Lattner9a215002010-02-28 20:55:18 +00001084 return N->getKind() == MorphNodeTo;
Chris Lattnere86097a2010-02-28 02:31:26 +00001085 }
1086};
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001087
Chris Lattner8950bca2010-12-23 17:03:20 +00001088/// MarkGlueResultsMatcher - This node indicates which non-root nodes in the
1089/// pattern produce glue. This allows CompleteMatchMatcher to update them
1090/// with the output glue of the resultant code.
1091class MarkGlueResultsMatcher : public Matcher {
1092 SmallVector<unsigned, 3> GlueResultNodes;
Chris Lattner02f73582010-02-24 05:33:42 +00001093public:
Chris Lattner8950bca2010-12-23 17:03:20 +00001094 MarkGlueResultsMatcher(const unsigned *nodes, unsigned NumNodes)
1095 : Matcher(MarkGlueResults), GlueResultNodes(nodes, nodes+NumNodes) {}
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001096
Chris Lattner8950bca2010-12-23 17:03:20 +00001097 unsigned getNumNodes() const { return GlueResultNodes.size(); }
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001098
Chris Lattner02f73582010-02-24 05:33:42 +00001099 unsigned getNode(unsigned i) const {
Chris Lattner8950bca2010-12-23 17:03:20 +00001100 assert(i < GlueResultNodes.size());
1101 return GlueResultNodes[i];
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001102 }
1103
Chris Lattnerb21ba712010-02-25 02:04:40 +00001104 static inline bool classof(const Matcher *N) {
Chris Lattner8950bca2010-12-23 17:03:20 +00001105 return N->getKind() == MarkGlueResults;
Chris Lattner02f73582010-02-24 05:33:42 +00001106 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001107
Chris Lattner58aa8342010-02-25 06:49:58 +00001108private:
Chris Lattnera5028a62010-02-25 06:53:39 +00001109 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner58aa8342010-02-25 06:49:58 +00001110 virtual bool isEqualImpl(const Matcher *M) const {
Chris Lattner8950bca2010-12-23 17:03:20 +00001111 return cast<MarkGlueResultsMatcher>(M)->GlueResultNodes == GlueResultNodes;
Chris Lattner58aa8342010-02-25 06:49:58 +00001112 }
1113 virtual unsigned getHashImpl() const;
Chris Lattner02f73582010-02-24 05:33:42 +00001114};
1115
Chris Lattnerb21ba712010-02-25 02:04:40 +00001116/// CompleteMatchMatcher - Complete a match by replacing the results of the
Chris Lattner77f2e272010-02-21 06:03:07 +00001117/// pattern with the newly generated nodes. This also prints a comment
1118/// indicating the source and dest patterns.
Chris Lattnerb21ba712010-02-25 02:04:40 +00001119class CompleteMatchMatcher : public Matcher {
Chris Lattner77f2e272010-02-21 06:03:07 +00001120 SmallVector<unsigned, 2> Results;
Chris Lattner8e946be2010-02-21 03:22:59 +00001121 const PatternToMatch &Pattern;
1122public:
Chris Lattnerb21ba712010-02-25 02:04:40 +00001123 CompleteMatchMatcher(const unsigned *results, unsigned numresults,
Chris Lattnerc78f2a32010-02-28 20:49:53 +00001124 const PatternToMatch &pattern)
Chris Lattnerb21ba712010-02-25 02:04:40 +00001125 : Matcher(CompleteMatch), Results(results, results+numresults),
Chris Lattner77f2e272010-02-21 06:03:07 +00001126 Pattern(pattern) {}
1127
1128 unsigned getNumResults() const { return Results.size(); }
1129 unsigned getResult(unsigned R) const { return Results[R]; }
Chris Lattner8e946be2010-02-21 03:22:59 +00001130 const PatternToMatch &getPattern() const { return Pattern; }
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001131
Chris Lattnerb21ba712010-02-25 02:04:40 +00001132 static inline bool classof(const Matcher *N) {
Chris Lattner77f2e272010-02-21 06:03:07 +00001133 return N->getKind() == CompleteMatch;
Chris Lattner8e946be2010-02-21 03:22:59 +00001134 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001135
Chris Lattner58aa8342010-02-25 06:49:58 +00001136private:
Chris Lattnera5028a62010-02-25 06:53:39 +00001137 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner58aa8342010-02-25 06:49:58 +00001138 virtual bool isEqualImpl(const Matcher *M) const {
1139 return cast<CompleteMatchMatcher>(M)->Results == Results &&
1140 &cast<CompleteMatchMatcher>(M)->Pattern == &Pattern;
1141 }
1142 virtual unsigned getHashImpl() const;
Chris Lattner8e946be2010-02-21 03:22:59 +00001143};
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001144
Chris Lattnerda272d12010-02-15 08:04:42 +00001145} // end namespace llvm
1146
1147#endif