blob: d80f19b334608391735f8e47a5cf4d9dbd3a117c [file] [log] [blame]
Chris Lattnere7d6e3c2010-02-15 08:04:42 +00001//===- DAGISelMatcher.h - Representation of DAG pattern matcher -----------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef TBLGEN_DAGISELMATCHER_H
11#define TBLGEN_DAGISELMATCHER_H
12
Chris Lattnerfdbdc8c2010-02-16 07:21:10 +000013#include "llvm/CodeGen/ValueTypes.h"
Chris Lattnere7d6e3c2010-02-15 08:04:42 +000014#include "llvm/ADT/OwningPtr.h"
15#include "llvm/ADT/StringRef.h"
Chris Lattner3163ca52010-02-21 03:22:59 +000016#include "llvm/ADT/SmallVector.h"
Chris Lattnerfdbdc8c2010-02-16 07:21:10 +000017#include "llvm/Support/Casting.h"
Chris Lattnere7d6e3c2010-02-15 08:04:42 +000018
19namespace llvm {
20 class CodeGenDAGPatterns;
Chris Lattner9a515172010-02-25 02:04:40 +000021 class Matcher;
Chris Lattnere7d6e3c2010-02-15 08:04:42 +000022 class PatternToMatch;
23 class raw_ostream;
24 class ComplexPattern;
Chris Lattnerddfb5222010-02-18 22:03:03 +000025 class Record;
Chris Lattnere9f720b2010-02-27 21:48:43 +000026 class SDNodeInfo;
Chris Lattnere7d6e3c2010-02-15 08:04:42 +000027
Chris Lattner9a515172010-02-25 02:04:40 +000028Matcher *ConvertPatternToMatcher(const PatternToMatch &Pattern,
29 const CodeGenDAGPatterns &CGP);
30Matcher *OptimizeMatcher(Matcher *Matcher);
31void EmitMatcherTable(const Matcher *Matcher, raw_ostream &OS);
Chris Lattnere7d6e3c2010-02-15 08:04:42 +000032
33
Chris Lattner9a515172010-02-25 02:04:40 +000034/// Matcher - Base class for all the the DAG ISel Matcher representation
Chris Lattnere7d6e3c2010-02-15 08:04:42 +000035/// nodes.
Chris Lattner9a515172010-02-25 02:04:40 +000036class Matcher {
Chris Lattner78039ec2010-02-18 02:53:41 +000037 // The next matcher node that is executed after this one. Null if this is the
38 // last stage of a match.
Chris Lattner9a515172010-02-25 02:04:40 +000039 OwningPtr<Matcher> Next;
Chris Lattnere7d6e3c2010-02-15 08:04:42 +000040public:
41 enum KindTy {
Chris Lattner3163ca52010-02-21 03:22:59 +000042 // Matcher state manipulation.
Chris Lattnerac10e4f2010-02-25 01:56:48 +000043 Scope, // Push a checking scope.
Chris Lattner3163ca52010-02-21 03:22:59 +000044 RecordNode, // Record the current node.
Chris Lattner3ee1bc42010-02-24 07:31:45 +000045 RecordChild, // Record a child of the current node.
Chris Lattner3163ca52010-02-21 03:22:59 +000046 RecordMemRef, // Record the memref in the current node.
47 CaptureFlagInput, // If the current node has an input flag, save it.
48 MoveChild, // Move current node to specified child.
49 MoveParent, // Move current node to parent.
Chris Lattnere7d6e3c2010-02-15 08:04:42 +000050
Chris Lattnerddfb5222010-02-18 22:03:03 +000051 // Predicate checking.
Chris Lattner3163ca52010-02-21 03:22:59 +000052 CheckSame, // Fail if not same as prev match.
Chris Lattnere7d6e3c2010-02-15 08:04:42 +000053 CheckPatternPredicate,
Chris Lattner3163ca52010-02-21 03:22:59 +000054 CheckPredicate, // Fail if node predicate fails.
55 CheckOpcode, // Fail if not opcode.
Chris Lattnerf58c08e2010-02-22 22:30:37 +000056 CheckMultiOpcode, // Fail if not in opcode list.
Chris Lattner3163ca52010-02-21 03:22:59 +000057 CheckType, // Fail if not correct type.
Chris Lattner3c664b32010-02-24 20:15:25 +000058 CheckChildType, // Fail if child has wrong type.
Chris Lattner3163ca52010-02-21 03:22:59 +000059 CheckInteger, // Fail if wrong val.
60 CheckCondCode, // Fail if not condcode.
Chris Lattnere7d6e3c2010-02-15 08:04:42 +000061 CheckValueType,
62 CheckComplexPat,
63 CheckAndImm,
Chris Lattner9de39482010-02-16 06:10:58 +000064 CheckOrImm,
Chris Lattner283adcb2010-02-17 06:23:39 +000065 CheckFoldableChainNode,
Chris Lattnerddfb5222010-02-18 22:03:03 +000066 CheckChainCompatible,
67
68 // Node creation/emisssion.
Chris Lattner3163ca52010-02-21 03:22:59 +000069 EmitInteger, // Create a TargetConstant
70 EmitStringInteger, // Create a TargetConstant from a string.
71 EmitRegister, // Create a register.
72 EmitConvertToTarget, // Convert a imm/fpimm to target imm/fpimm
73 EmitMergeInputChains, // Merge together a chains for an input.
74 EmitCopyToReg, // Emit a copytoreg into a physreg.
75 EmitNode, // Create a DAG node
76 EmitNodeXForm, // Run a SDNodeXForm
Chris Lattner69f60c82010-02-24 05:33:42 +000077 MarkFlagResults, // Indicate which interior nodes have flag results.
Chris Lattnerb085ea12010-02-21 06:03:07 +000078 CompleteMatch // Finish a match and update the results.
Chris Lattnere7d6e3c2010-02-15 08:04:42 +000079 };
80 const KindTy Kind;
Chris Lattner3b31c982010-02-18 02:49:24 +000081
Chris Lattnere7d6e3c2010-02-15 08:04:42 +000082protected:
Chris Lattner9a515172010-02-25 02:04:40 +000083 Matcher(KindTy K) : Kind(K) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +000084public:
Chris Lattner9a515172010-02-25 02:04:40 +000085 virtual ~Matcher() {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +000086
87 KindTy getKind() const { return Kind; }
Chris Lattner3b31c982010-02-18 02:49:24 +000088
Chris Lattner9a515172010-02-25 02:04:40 +000089 Matcher *getNext() { return Next.get(); }
90 const Matcher *getNext() const { return Next.get(); }
91 void setNext(Matcher *C) { Next.reset(C); }
92 Matcher *takeNext() { return Next.take(); }
Chris Lattner3ee1bc42010-02-24 07:31:45 +000093
Chris Lattner9a515172010-02-25 02:04:40 +000094 OwningPtr<Matcher> &getNextPtr() { return Next; }
Chris Lattnere7d6e3c2010-02-15 08:04:42 +000095
Chris Lattner9a515172010-02-25 02:04:40 +000096 static inline bool classof(const Matcher *) { return true; }
Chris Lattnere7d6e3c2010-02-15 08:04:42 +000097
Chris Lattner136ab782010-02-25 06:49:58 +000098 bool isEqual(const Matcher *M) const {
99 if (getKind() != M->getKind()) return false;
100 return isEqualImpl(M);
101 }
102
103 unsigned getHash() const {
Chris Lattner54ee7362010-02-26 07:35:27 +0000104 // Clear the high bit so we don't conflict with tombstones etc.
105 return ((getHashImpl() << 4) ^ getKind()) & (~0U>>1);
Chris Lattner136ab782010-02-25 06:49:58 +0000106 }
107
Chris Lattnerf4950d02010-02-27 06:22:57 +0000108 /// isSafeToReorderWithPatternPredicate - Return true if it is safe to sink a
109 /// PatternPredicate node past this one.
110 virtual bool isSafeToReorderWithPatternPredicate() const {
111 return false;
112 }
113
Chris Lattnerddfd5ab2010-02-27 07:49:13 +0000114 /// isContradictory - Return true of these two matchers could never match on
115 /// the same node.
116 bool isContradictory(const Matcher *Other) const {
117 // Since this predicate is reflexive, we canonicalize the ordering so that
118 // we always match a node against nodes with kinds that are greater or equal
119 // to them. For example, we'll pass in a CheckType node as an argument to
120 // the CheckOpcode method, not the other way around.
121 if (getKind() < Other->getKind())
122 return isContradictoryImpl(Other);
123 return Other->isContradictoryImpl(this);
124 }
125
Chris Lattner392b1bf2010-02-25 06:53:39 +0000126 void print(raw_ostream &OS, unsigned indent = 0) const;
Chris Lattnerddfd5ab2010-02-27 07:49:13 +0000127 void printOne(raw_ostream &OS) const;
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000128 void dump() const;
Chris Lattner3b31c982010-02-18 02:49:24 +0000129protected:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000130 virtual void printImpl(raw_ostream &OS, unsigned indent) const = 0;
Chris Lattner136ab782010-02-25 06:49:58 +0000131 virtual bool isEqualImpl(const Matcher *M) const = 0;
132 virtual unsigned getHashImpl() const = 0;
Chris Lattnerddfd5ab2010-02-27 07:49:13 +0000133 virtual bool isContradictoryImpl(const Matcher *M) const { return false; }
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000134};
135
Chris Lattner42363662010-02-25 19:00:39 +0000136/// ScopeMatcher - This attempts to match each of its children to find the first
137/// one that successfully matches. If one child fails, it tries the next child.
138/// If none of the children match then this check fails. It never has a 'next'.
Chris Lattner9a515172010-02-25 02:04:40 +0000139class ScopeMatcher : public Matcher {
Chris Lattner42363662010-02-25 19:00:39 +0000140 SmallVector<Matcher*, 4> Children;
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000141public:
Chris Lattner42363662010-02-25 19:00:39 +0000142 ScopeMatcher(Matcher *const *children, unsigned numchildren)
143 : Matcher(Scope), Children(children, children+numchildren) {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000144 }
Chris Lattner42363662010-02-25 19:00:39 +0000145 virtual ~ScopeMatcher();
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000146
Chris Lattner42363662010-02-25 19:00:39 +0000147 unsigned getNumChildren() const { return Children.size(); }
148
149 Matcher *getChild(unsigned i) { return Children[i]; }
150 const Matcher *getChild(unsigned i) const { return Children[i]; }
151
152 void resetChild(unsigned i, Matcher *N) {
153 delete Children[i];
154 Children[i] = N;
155 }
156
157 Matcher *takeChild(unsigned i) {
158 Matcher *Res = Children[i];
159 Children[i] = 0;
160 return Res;
161 }
Chris Lattner54ee7362010-02-26 07:35:27 +0000162
163 void setNumChildren(unsigned NC) {
164 if (NC < Children.size()) {
165 // delete any children we're about to lose pointers to.
166 for (unsigned i = NC, e = Children.size(); i != e; ++i)
167 delete Children[i];
168 }
169 Children.resize(NC);
170 }
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000171
Chris Lattner9a515172010-02-25 02:04:40 +0000172 static inline bool classof(const Matcher *N) {
Chris Lattnerac10e4f2010-02-25 01:56:48 +0000173 return N->getKind() == Scope;
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000174 }
175
Chris Lattner136ab782010-02-25 06:49:58 +0000176private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000177 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000178 virtual bool isEqualImpl(const Matcher *M) const { return false; }
Chris Lattner42363662010-02-25 19:00:39 +0000179 virtual unsigned getHashImpl() const { return 12312; }
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000180};
181
Chris Lattner9a515172010-02-25 02:04:40 +0000182/// RecordMatcher - Save the current node in the operand list.
183class RecordMatcher : public Matcher {
Chris Lattner56cf88d2010-02-17 01:03:09 +0000184 /// WhatFor - This is a string indicating why we're recording this. This
185 /// should only be used for comment generation not anything semantic.
186 std::string WhatFor;
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000187public:
Chris Lattner9a515172010-02-25 02:04:40 +0000188 RecordMatcher(const std::string &whatfor)
189 : Matcher(RecordNode), WhatFor(whatfor) {}
Chris Lattner56cf88d2010-02-17 01:03:09 +0000190
Chris Lattner086ca522010-02-17 01:27:29 +0000191 const std::string &getWhatFor() const { return WhatFor; }
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000192
Chris Lattner9a515172010-02-25 02:04:40 +0000193 static inline bool classof(const Matcher *N) {
Chris Lattnerddfb5222010-02-18 22:03:03 +0000194 return N->getKind() == RecordNode;
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000195 }
196
Chris Lattnerf4950d02010-02-27 06:22:57 +0000197 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
Chris Lattner136ab782010-02-25 06:49:58 +0000198private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000199 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000200 virtual bool isEqualImpl(const Matcher *M) const { return true; }
201 virtual unsigned getHashImpl() const { return 0; }
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000202};
203
Chris Lattner9a515172010-02-25 02:04:40 +0000204/// RecordChildMatcher - Save a numbered child of the current node, or fail
Chris Lattner3ee1bc42010-02-24 07:31:45 +0000205/// the match if it doesn't exist. This is logically equivalent to:
206/// MoveChild N + RecordNode + MoveParent.
Chris Lattner9a515172010-02-25 02:04:40 +0000207class RecordChildMatcher : public Matcher {
Chris Lattner3ee1bc42010-02-24 07:31:45 +0000208 unsigned ChildNo;
209
210 /// WhatFor - This is a string indicating why we're recording this. This
211 /// should only be used for comment generation not anything semantic.
212 std::string WhatFor;
213public:
Chris Lattner9a515172010-02-25 02:04:40 +0000214 RecordChildMatcher(unsigned childno, const std::string &whatfor)
215 : Matcher(RecordChild), ChildNo(childno), WhatFor(whatfor) {}
Chris Lattner3ee1bc42010-02-24 07:31:45 +0000216
217 unsigned getChildNo() const { return ChildNo; }
218 const std::string &getWhatFor() const { return WhatFor; }
219
Chris Lattner9a515172010-02-25 02:04:40 +0000220 static inline bool classof(const Matcher *N) {
Chris Lattner3ee1bc42010-02-24 07:31:45 +0000221 return N->getKind() == RecordChild;
222 }
223
Chris Lattnerf4950d02010-02-27 06:22:57 +0000224 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
225
Chris Lattner136ab782010-02-25 06:49:58 +0000226private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000227 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000228 virtual bool isEqualImpl(const Matcher *M) const {
229 return cast<RecordChildMatcher>(M)->getChildNo() == getChildNo();
230 }
231 virtual unsigned getHashImpl() const { return getChildNo(); }
Chris Lattner3ee1bc42010-02-24 07:31:45 +0000232};
233
Chris Lattner9a515172010-02-25 02:04:40 +0000234/// RecordMemRefMatcher - Save the current node's memref.
235class RecordMemRefMatcher : public Matcher {
Chris Lattner3163ca52010-02-21 03:22:59 +0000236public:
Chris Lattner9a515172010-02-25 02:04:40 +0000237 RecordMemRefMatcher() : Matcher(RecordMemRef) {}
Chris Lattner3163ca52010-02-21 03:22:59 +0000238
Chris Lattner9a515172010-02-25 02:04:40 +0000239 static inline bool classof(const Matcher *N) {
Chris Lattner3163ca52010-02-21 03:22:59 +0000240 return N->getKind() == RecordMemRef;
241 }
242
Chris Lattnerf4950d02010-02-27 06:22:57 +0000243 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
244
Chris Lattner136ab782010-02-25 06:49:58 +0000245private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000246 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000247 virtual bool isEqualImpl(const Matcher *M) const { return true; }
248 virtual unsigned getHashImpl() const { return 0; }
Chris Lattner3163ca52010-02-21 03:22:59 +0000249};
250
251
Chris Lattner9a515172010-02-25 02:04:40 +0000252/// CaptureFlagInputMatcher - If the current record has a flag input, record
Chris Lattner3163ca52010-02-21 03:22:59 +0000253/// it so that it is used as an input to the generated code.
Chris Lattner9a515172010-02-25 02:04:40 +0000254class CaptureFlagInputMatcher : public Matcher {
Chris Lattner3163ca52010-02-21 03:22:59 +0000255public:
Chris Lattner9a515172010-02-25 02:04:40 +0000256 CaptureFlagInputMatcher() : Matcher(CaptureFlagInput) {}
Chris Lattner3163ca52010-02-21 03:22:59 +0000257
Chris Lattner9a515172010-02-25 02:04:40 +0000258 static inline bool classof(const Matcher *N) {
Chris Lattner3163ca52010-02-21 03:22:59 +0000259 return N->getKind() == CaptureFlagInput;
260 }
261
Chris Lattnerf4950d02010-02-27 06:22:57 +0000262 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
263
Chris Lattner136ab782010-02-25 06:49:58 +0000264private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000265 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000266 virtual bool isEqualImpl(const Matcher *M) const { return true; }
267 virtual unsigned getHashImpl() const { return 0; }
Chris Lattner3163ca52010-02-21 03:22:59 +0000268};
269
Chris Lattner9a515172010-02-25 02:04:40 +0000270/// MoveChildMatcher - This tells the interpreter to move into the
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000271/// specified child node.
Chris Lattner9a515172010-02-25 02:04:40 +0000272class MoveChildMatcher : public Matcher {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000273 unsigned ChildNo;
274public:
Chris Lattner9a515172010-02-25 02:04:40 +0000275 MoveChildMatcher(unsigned childNo) : Matcher(MoveChild), ChildNo(childNo) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000276
277 unsigned getChildNo() const { return ChildNo; }
278
Chris Lattner9a515172010-02-25 02:04:40 +0000279 static inline bool classof(const Matcher *N) {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000280 return N->getKind() == MoveChild;
281 }
282
Chris Lattnerf4950d02010-02-27 06:22:57 +0000283 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
284
Chris Lattner136ab782010-02-25 06:49:58 +0000285private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000286 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000287 virtual bool isEqualImpl(const Matcher *M) const {
288 return cast<MoveChildMatcher>(M)->getChildNo() == getChildNo();
289 }
290 virtual unsigned getHashImpl() const { return getChildNo(); }
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000291};
292
Chris Lattner9a515172010-02-25 02:04:40 +0000293/// MoveParentMatcher - This tells the interpreter to move to the parent
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000294/// of the current node.
Chris Lattner9a515172010-02-25 02:04:40 +0000295class MoveParentMatcher : public Matcher {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000296public:
Chris Lattner9a515172010-02-25 02:04:40 +0000297 MoveParentMatcher() : Matcher(MoveParent) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000298
Chris Lattner9a515172010-02-25 02:04:40 +0000299 static inline bool classof(const Matcher *N) {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000300 return N->getKind() == MoveParent;
301 }
302
Chris Lattnerf4950d02010-02-27 06:22:57 +0000303 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
304
Chris Lattner136ab782010-02-25 06:49:58 +0000305private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000306 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000307 virtual bool isEqualImpl(const Matcher *M) const { return true; }
308 virtual unsigned getHashImpl() const { return 0; }
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000309};
310
Chris Lattner9a515172010-02-25 02:04:40 +0000311/// CheckSameMatcher - This checks to see if this node is exactly the same
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000312/// node as the specified match that was recorded with 'Record'. This is used
313/// when patterns have the same name in them, like '(mul GPR:$in, GPR:$in)'.
Chris Lattner9a515172010-02-25 02:04:40 +0000314class CheckSameMatcher : public Matcher {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000315 unsigned MatchNumber;
316public:
Chris Lattner9a515172010-02-25 02:04:40 +0000317 CheckSameMatcher(unsigned matchnumber)
Chris Lattner136ab782010-02-25 06:49:58 +0000318 : Matcher(CheckSame), MatchNumber(matchnumber) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000319
320 unsigned getMatchNumber() const { return MatchNumber; }
321
Chris Lattner9a515172010-02-25 02:04:40 +0000322 static inline bool classof(const Matcher *N) {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000323 return N->getKind() == CheckSame;
324 }
325
Chris Lattnerf4950d02010-02-27 06:22:57 +0000326 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
327
Chris Lattner136ab782010-02-25 06:49:58 +0000328private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000329 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000330 virtual bool isEqualImpl(const Matcher *M) const {
331 return cast<CheckSameMatcher>(M)->getMatchNumber() == getMatchNumber();
332 }
333 virtual unsigned getHashImpl() const { return getMatchNumber(); }
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000334};
335
Chris Lattner9a515172010-02-25 02:04:40 +0000336/// CheckPatternPredicateMatcher - This checks the target-specific predicate
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000337/// to see if the entire pattern is capable of matching. This predicate does
338/// not take a node as input. This is used for subtarget feature checks etc.
Chris Lattner9a515172010-02-25 02:04:40 +0000339class CheckPatternPredicateMatcher : public Matcher {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000340 std::string Predicate;
341public:
Chris Lattner9a515172010-02-25 02:04:40 +0000342 CheckPatternPredicateMatcher(StringRef predicate)
Chris Lattner136ab782010-02-25 06:49:58 +0000343 : Matcher(CheckPatternPredicate), Predicate(predicate) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000344
345 StringRef getPredicate() const { return Predicate; }
346
Chris Lattner9a515172010-02-25 02:04:40 +0000347 static inline bool classof(const Matcher *N) {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000348 return N->getKind() == CheckPatternPredicate;
349 }
350
Chris Lattnerf4950d02010-02-27 06:22:57 +0000351 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
352
Chris Lattner136ab782010-02-25 06:49:58 +0000353private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000354 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000355 virtual bool isEqualImpl(const Matcher *M) const {
356 return cast<CheckPatternPredicateMatcher>(M)->getPredicate() == Predicate;
357 }
358 virtual unsigned getHashImpl() const;
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000359};
360
Chris Lattner9a515172010-02-25 02:04:40 +0000361/// CheckPredicateMatcher - This checks the target-specific predicate to
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000362/// see if the node is acceptable.
Chris Lattner9a515172010-02-25 02:04:40 +0000363class CheckPredicateMatcher : public Matcher {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000364 StringRef PredName;
365public:
Chris Lattner9a515172010-02-25 02:04:40 +0000366 CheckPredicateMatcher(StringRef predname)
367 : Matcher(CheckPredicate), PredName(predname) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000368
369 StringRef getPredicateName() const { return PredName; }
370
Chris Lattner9a515172010-02-25 02:04:40 +0000371 static inline bool classof(const Matcher *N) {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000372 return N->getKind() == CheckPredicate;
373 }
374
Chris Lattnerf4950d02010-02-27 06:22:57 +0000375 // TODO: Ok?
376 //virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
377
Chris Lattner136ab782010-02-25 06:49:58 +0000378private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000379 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000380 virtual bool isEqualImpl(const Matcher *M) const {
381 return cast<CheckPredicateMatcher>(M)->PredName == PredName;
382 }
383 virtual unsigned getHashImpl() const;
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000384};
385
386
Chris Lattner9a515172010-02-25 02:04:40 +0000387/// CheckOpcodeMatcher - This checks to see if the current node has the
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000388/// specified opcode, if not it fails to match.
Chris Lattner9a515172010-02-25 02:04:40 +0000389class CheckOpcodeMatcher : public Matcher {
Chris Lattnere9f720b2010-02-27 21:48:43 +0000390 const SDNodeInfo &Opcode;
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000391public:
Chris Lattnere9f720b2010-02-27 21:48:43 +0000392 CheckOpcodeMatcher(const SDNodeInfo &opcode)
393 : Matcher(CheckOpcode), Opcode(opcode) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000394
Chris Lattnere9f720b2010-02-27 21:48:43 +0000395 const SDNodeInfo &getOpcode() const { return Opcode; }
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000396
Chris Lattner9a515172010-02-25 02:04:40 +0000397 static inline bool classof(const Matcher *N) {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000398 return N->getKind() == CheckOpcode;
399 }
400
Chris Lattnerf4950d02010-02-27 06:22:57 +0000401 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
402
Chris Lattner136ab782010-02-25 06:49:58 +0000403private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000404 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000405 virtual bool isEqualImpl(const Matcher *M) const {
Chris Lattnere9f720b2010-02-27 21:48:43 +0000406 return &cast<CheckOpcodeMatcher>(M)->Opcode == &Opcode;
Chris Lattner136ab782010-02-25 06:49:58 +0000407 }
408 virtual unsigned getHashImpl() const;
Chris Lattnerddfd5ab2010-02-27 07:49:13 +0000409 virtual bool isContradictoryImpl(const Matcher *M) const;
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000410};
411
Chris Lattner9a515172010-02-25 02:04:40 +0000412/// CheckMultiOpcodeMatcher - This checks to see if the current node has one
Chris Lattnerf58c08e2010-02-22 22:30:37 +0000413/// of the specified opcode, if not it fails to match.
Chris Lattner9a515172010-02-25 02:04:40 +0000414class CheckMultiOpcodeMatcher : public Matcher {
Chris Lattnere9f720b2010-02-27 21:48:43 +0000415 SmallVector<const SDNodeInfo*, 4> Opcodes;
Chris Lattnerf58c08e2010-02-22 22:30:37 +0000416public:
Chris Lattnere9f720b2010-02-27 21:48:43 +0000417 CheckMultiOpcodeMatcher(const SDNodeInfo * const *opcodes, unsigned numops)
418 : Matcher(CheckMultiOpcode), Opcodes(opcodes, opcodes+numops) {}
Chris Lattnerf58c08e2010-02-22 22:30:37 +0000419
Chris Lattnere9f720b2010-02-27 21:48:43 +0000420 unsigned getNumOpcodes() const { return Opcodes.size(); }
421 const SDNodeInfo &getOpcode(unsigned i) const { return *Opcodes[i]; }
Chris Lattnerf58c08e2010-02-22 22:30:37 +0000422
Chris Lattner9a515172010-02-25 02:04:40 +0000423 static inline bool classof(const Matcher *N) {
Chris Lattnerf58c08e2010-02-22 22:30:37 +0000424 return N->getKind() == CheckMultiOpcode;
425 }
426
Chris Lattnerf4950d02010-02-27 06:22:57 +0000427 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
428
Chris Lattner136ab782010-02-25 06:49:58 +0000429private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000430 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000431 virtual bool isEqualImpl(const Matcher *M) const {
Chris Lattnere9f720b2010-02-27 21:48:43 +0000432 return cast<CheckMultiOpcodeMatcher>(M)->Opcodes == Opcodes;
Chris Lattner136ab782010-02-25 06:49:58 +0000433 }
434 virtual unsigned getHashImpl() const;
Chris Lattnerf58c08e2010-02-22 22:30:37 +0000435};
436
437
438
Chris Lattner9a515172010-02-25 02:04:40 +0000439/// CheckTypeMatcher - This checks to see if the current node has the
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000440/// specified type, if not it fails to match.
Chris Lattner9a515172010-02-25 02:04:40 +0000441class CheckTypeMatcher : public Matcher {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000442 MVT::SimpleValueType Type;
443public:
Chris Lattner9a515172010-02-25 02:04:40 +0000444 CheckTypeMatcher(MVT::SimpleValueType type)
445 : Matcher(CheckType), Type(type) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000446
447 MVT::SimpleValueType getType() const { return Type; }
448
Chris Lattner9a515172010-02-25 02:04:40 +0000449 static inline bool classof(const Matcher *N) {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000450 return N->getKind() == CheckType;
451 }
452
Chris Lattnerf4950d02010-02-27 06:22:57 +0000453 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
454
Chris Lattner136ab782010-02-25 06:49:58 +0000455private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000456 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000457 virtual bool isEqualImpl(const Matcher *M) const {
Chris Lattner2d0e0792010-02-26 08:05:36 +0000458 return cast<CheckTypeMatcher>(M)->Type == Type;
Chris Lattner136ab782010-02-25 06:49:58 +0000459 }
460 virtual unsigned getHashImpl() const { return Type; }
Chris Lattnerddfd5ab2010-02-27 07:49:13 +0000461 virtual bool isContradictoryImpl(const Matcher *M) const;
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000462};
Chris Lattner3c664b32010-02-24 20:15:25 +0000463
Chris Lattner9a515172010-02-25 02:04:40 +0000464/// CheckChildTypeMatcher - This checks to see if a child node has the
Chris Lattner3c664b32010-02-24 20:15:25 +0000465/// specified type, if not it fails to match.
Chris Lattner9a515172010-02-25 02:04:40 +0000466class CheckChildTypeMatcher : public Matcher {
Chris Lattner3c664b32010-02-24 20:15:25 +0000467 unsigned ChildNo;
468 MVT::SimpleValueType Type;
469public:
Chris Lattner9a515172010-02-25 02:04:40 +0000470 CheckChildTypeMatcher(unsigned childno, MVT::SimpleValueType type)
471 : Matcher(CheckChildType), ChildNo(childno), Type(type) {}
Chris Lattner3c664b32010-02-24 20:15:25 +0000472
473 unsigned getChildNo() const { return ChildNo; }
474 MVT::SimpleValueType getType() const { return Type; }
475
Chris Lattner9a515172010-02-25 02:04:40 +0000476 static inline bool classof(const Matcher *N) {
Chris Lattner3c664b32010-02-24 20:15:25 +0000477 return N->getKind() == CheckChildType;
478 }
479
Chris Lattnerf4950d02010-02-27 06:22:57 +0000480 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
481
Chris Lattner136ab782010-02-25 06:49:58 +0000482private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000483 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000484 virtual bool isEqualImpl(const Matcher *M) const {
485 return cast<CheckChildTypeMatcher>(M)->ChildNo == ChildNo &&
486 cast<CheckChildTypeMatcher>(M)->Type == Type;
487 }
488 virtual unsigned getHashImpl() const { return (Type << 3) | ChildNo; }
Chris Lattnerddfd5ab2010-02-27 07:49:13 +0000489 virtual bool isContradictoryImpl(const Matcher *M) const;
Chris Lattner3c664b32010-02-24 20:15:25 +0000490};
491
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000492
Chris Lattner9a515172010-02-25 02:04:40 +0000493/// CheckIntegerMatcher - This checks to see if the current node is a
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000494/// ConstantSDNode with the specified integer value, if not it fails to match.
Chris Lattner9a515172010-02-25 02:04:40 +0000495class CheckIntegerMatcher : public Matcher {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000496 int64_t Value;
497public:
Chris Lattner9a515172010-02-25 02:04:40 +0000498 CheckIntegerMatcher(int64_t value)
499 : Matcher(CheckInteger), Value(value) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000500
501 int64_t getValue() const { return Value; }
502
Chris Lattner9a515172010-02-25 02:04:40 +0000503 static inline bool classof(const Matcher *N) {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000504 return N->getKind() == CheckInteger;
505 }
506
Chris Lattnerf4950d02010-02-27 06:22:57 +0000507 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
508
Chris Lattner136ab782010-02-25 06:49:58 +0000509private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000510 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000511 virtual bool isEqualImpl(const Matcher *M) const {
512 return cast<CheckIntegerMatcher>(M)->Value == Value;
513 }
514 virtual unsigned getHashImpl() const { return Value; }
Chris Lattner086af322010-02-27 08:11:15 +0000515 virtual bool isContradictoryImpl(const Matcher *M) const;
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000516};
517
Chris Lattner9a515172010-02-25 02:04:40 +0000518/// CheckCondCodeMatcher - This checks to see if the current node is a
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000519/// CondCodeSDNode with the specified condition, if not it fails to match.
Chris Lattner9a515172010-02-25 02:04:40 +0000520class CheckCondCodeMatcher : public Matcher {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000521 StringRef CondCodeName;
522public:
Chris Lattner9a515172010-02-25 02:04:40 +0000523 CheckCondCodeMatcher(StringRef condcodename)
524 : Matcher(CheckCondCode), CondCodeName(condcodename) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000525
526 StringRef getCondCodeName() const { return CondCodeName; }
527
Chris Lattner9a515172010-02-25 02:04:40 +0000528 static inline bool classof(const Matcher *N) {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000529 return N->getKind() == CheckCondCode;
530 }
531
Chris Lattnerf4950d02010-02-27 06:22:57 +0000532 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
533
Chris Lattner136ab782010-02-25 06:49:58 +0000534private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000535 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000536 virtual bool isEqualImpl(const Matcher *M) const {
537 return cast<CheckCondCodeMatcher>(M)->CondCodeName == CondCodeName;
538 }
539 virtual unsigned getHashImpl() const;
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000540};
541
Chris Lattner9a515172010-02-25 02:04:40 +0000542/// CheckValueTypeMatcher - This checks to see if the current node is a
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000543/// VTSDNode with the specified type, if not it fails to match.
Chris Lattner9a515172010-02-25 02:04:40 +0000544class CheckValueTypeMatcher : public Matcher {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000545 StringRef TypeName;
546public:
Chris Lattner9a515172010-02-25 02:04:40 +0000547 CheckValueTypeMatcher(StringRef type_name)
548 : Matcher(CheckValueType), TypeName(type_name) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000549
550 StringRef getTypeName() const { return TypeName; }
551
Chris Lattner9a515172010-02-25 02:04:40 +0000552 static inline bool classof(const Matcher *N) {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000553 return N->getKind() == CheckValueType;
554 }
555
Chris Lattnerf4950d02010-02-27 06:22:57 +0000556 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
557
Chris Lattner136ab782010-02-25 06:49:58 +0000558private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000559 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000560 virtual bool isEqualImpl(const Matcher *M) const {
561 return cast<CheckValueTypeMatcher>(M)->TypeName == TypeName;
562 }
563 virtual unsigned getHashImpl() const;
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000564};
565
566
567
Chris Lattner9a515172010-02-25 02:04:40 +0000568/// CheckComplexPatMatcher - This node runs the specified ComplexPattern on
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000569/// the current node.
Chris Lattner9a515172010-02-25 02:04:40 +0000570class CheckComplexPatMatcher : public Matcher {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000571 const ComplexPattern &Pattern;
572public:
Chris Lattner9a515172010-02-25 02:04:40 +0000573 CheckComplexPatMatcher(const ComplexPattern &pattern)
574 : Matcher(CheckComplexPat), Pattern(pattern) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000575
Chris Lattnerdc429fd2010-02-17 00:31:50 +0000576 const ComplexPattern &getPattern() const { return Pattern; }
577
Chris Lattner9a515172010-02-25 02:04:40 +0000578 static inline bool classof(const Matcher *N) {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000579 return N->getKind() == CheckComplexPat;
580 }
581
Chris Lattnerf4950d02010-02-27 06:22:57 +0000582 // Not safe to move a pattern predicate past a complex pattern.
583 virtual bool isSafeToReorderWithPatternPredicate() const { return false; }
584
Chris Lattner136ab782010-02-25 06:49:58 +0000585private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000586 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000587 virtual bool isEqualImpl(const Matcher *M) const {
588 return &cast<CheckComplexPatMatcher>(M)->Pattern == &Pattern;
589 }
590 virtual unsigned getHashImpl() const {
591 return (unsigned)(intptr_t)&Pattern;
592 }
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000593};
594
Chris Lattner9a515172010-02-25 02:04:40 +0000595/// CheckAndImmMatcher - This checks to see if the current node is an 'and'
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000596/// with something equivalent to the specified immediate.
Chris Lattner9a515172010-02-25 02:04:40 +0000597class CheckAndImmMatcher : public Matcher {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000598 int64_t Value;
599public:
Chris Lattner9a515172010-02-25 02:04:40 +0000600 CheckAndImmMatcher(int64_t value)
601 : Matcher(CheckAndImm), Value(value) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000602
603 int64_t getValue() const { return Value; }
604
Chris Lattner9a515172010-02-25 02:04:40 +0000605 static inline bool classof(const Matcher *N) {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000606 return N->getKind() == CheckAndImm;
607 }
608
Chris Lattnerf4950d02010-02-27 06:22:57 +0000609 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
610
Chris Lattner136ab782010-02-25 06:49:58 +0000611private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000612 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000613 virtual bool isEqualImpl(const Matcher *M) const {
614 return cast<CheckAndImmMatcher>(M)->Value == Value;
615 }
616 virtual unsigned getHashImpl() const { return Value; }
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000617};
618
Chris Lattner9a515172010-02-25 02:04:40 +0000619/// CheckOrImmMatcher - This checks to see if the current node is an 'and'
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000620/// with something equivalent to the specified immediate.
Chris Lattner9a515172010-02-25 02:04:40 +0000621class CheckOrImmMatcher : public Matcher {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000622 int64_t Value;
623public:
Chris Lattner9a515172010-02-25 02:04:40 +0000624 CheckOrImmMatcher(int64_t value)
625 : Matcher(CheckOrImm), Value(value) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000626
627 int64_t getValue() const { return Value; }
628
Chris Lattner9a515172010-02-25 02:04:40 +0000629 static inline bool classof(const Matcher *N) {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000630 return N->getKind() == CheckOrImm;
631 }
632
Chris Lattnerf4950d02010-02-27 06:22:57 +0000633 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
634
Chris Lattner136ab782010-02-25 06:49:58 +0000635private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000636 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000637 virtual bool isEqualImpl(const Matcher *M) const {
638 return cast<CheckOrImmMatcher>(M)->Value == Value;
639 }
640 virtual unsigned getHashImpl() const { return Value; }
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000641};
Chris Lattner9de39482010-02-16 06:10:58 +0000642
Chris Lattner9a515172010-02-25 02:04:40 +0000643/// CheckFoldableChainNodeMatcher - This checks to see if the current node
Chris Lattnerc6dc8f62010-02-16 19:15:55 +0000644/// (which defines a chain operand) is safe to fold into a larger pattern.
Chris Lattner9a515172010-02-25 02:04:40 +0000645class CheckFoldableChainNodeMatcher : public Matcher {
Chris Lattner9de39482010-02-16 06:10:58 +0000646public:
Chris Lattner9a515172010-02-25 02:04:40 +0000647 CheckFoldableChainNodeMatcher()
648 : Matcher(CheckFoldableChainNode) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000649
Chris Lattner9a515172010-02-25 02:04:40 +0000650 static inline bool classof(const Matcher *N) {
Chris Lattnerc6dc8f62010-02-16 19:15:55 +0000651 return N->getKind() == CheckFoldableChainNode;
Chris Lattner9de39482010-02-16 06:10:58 +0000652 }
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000653
Chris Lattnerf4950d02010-02-27 06:22:57 +0000654 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
655
Chris Lattner136ab782010-02-25 06:49:58 +0000656private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000657 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000658 virtual bool isEqualImpl(const Matcher *M) const { return true; }
659 virtual unsigned getHashImpl() const { return 0; }
Chris Lattner9de39482010-02-16 06:10:58 +0000660};
661
Chris Lattner9a515172010-02-25 02:04:40 +0000662/// CheckChainCompatibleMatcher - Verify that the current node's chain
Chris Lattner283adcb2010-02-17 06:23:39 +0000663/// operand is 'compatible' with the specified recorded node's.
Chris Lattner9a515172010-02-25 02:04:40 +0000664class CheckChainCompatibleMatcher : public Matcher {
Chris Lattner283adcb2010-02-17 06:23:39 +0000665 unsigned PreviousOp;
666public:
Chris Lattner9a515172010-02-25 02:04:40 +0000667 CheckChainCompatibleMatcher(unsigned previousop)
668 : Matcher(CheckChainCompatible), PreviousOp(previousop) {}
Chris Lattner283adcb2010-02-17 06:23:39 +0000669
670 unsigned getPreviousOp() const { return PreviousOp; }
671
Chris Lattner9a515172010-02-25 02:04:40 +0000672 static inline bool classof(const Matcher *N) {
Chris Lattner283adcb2010-02-17 06:23:39 +0000673 return N->getKind() == CheckChainCompatible;
674 }
675
Chris Lattnerf4950d02010-02-27 06:22:57 +0000676 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
677
Chris Lattner136ab782010-02-25 06:49:58 +0000678private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000679 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000680 virtual bool isEqualImpl(const Matcher *M) const {
Chris Lattner50c0b362010-02-26 08:06:02 +0000681 return cast<CheckChainCompatibleMatcher>(M)->PreviousOp == PreviousOp;
Chris Lattner136ab782010-02-25 06:49:58 +0000682 }
683 virtual unsigned getHashImpl() const { return PreviousOp; }
Chris Lattner283adcb2010-02-17 06:23:39 +0000684};
685
Chris Lattner9a515172010-02-25 02:04:40 +0000686/// EmitIntegerMatcher - This creates a new TargetConstant.
687class EmitIntegerMatcher : public Matcher {
Chris Lattnerddfb5222010-02-18 22:03:03 +0000688 int64_t Val;
689 MVT::SimpleValueType VT;
690public:
Chris Lattner9a515172010-02-25 02:04:40 +0000691 EmitIntegerMatcher(int64_t val, MVT::SimpleValueType vt)
692 : Matcher(EmitInteger), Val(val), VT(vt) {}
Chris Lattner283adcb2010-02-17 06:23:39 +0000693
Chris Lattner7043e0a2010-02-19 07:49:56 +0000694 int64_t getValue() const { return Val; }
Chris Lattnerddfb5222010-02-18 22:03:03 +0000695 MVT::SimpleValueType getVT() const { return VT; }
696
Chris Lattner9a515172010-02-25 02:04:40 +0000697 static inline bool classof(const Matcher *N) {
Chris Lattnerddfb5222010-02-18 22:03:03 +0000698 return N->getKind() == EmitInteger;
699 }
700
Chris Lattner136ab782010-02-25 06:49:58 +0000701private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000702 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000703 virtual bool isEqualImpl(const Matcher *M) const {
704 return cast<EmitIntegerMatcher>(M)->Val == Val &&
705 cast<EmitIntegerMatcher>(M)->VT == VT;
706 }
707 virtual unsigned getHashImpl() const { return (Val << 4) | VT; }
Chris Lattnerddfb5222010-02-18 22:03:03 +0000708};
Chris Lattner3163ca52010-02-21 03:22:59 +0000709
Chris Lattner9a515172010-02-25 02:04:40 +0000710/// EmitStringIntegerMatcher - A target constant whose value is represented
Chris Lattner3163ca52010-02-21 03:22:59 +0000711/// by a string.
Chris Lattner9a515172010-02-25 02:04:40 +0000712class EmitStringIntegerMatcher : public Matcher {
Chris Lattner3163ca52010-02-21 03:22:59 +0000713 std::string Val;
714 MVT::SimpleValueType VT;
715public:
Chris Lattner9a515172010-02-25 02:04:40 +0000716 EmitStringIntegerMatcher(const std::string &val, MVT::SimpleValueType vt)
717 : Matcher(EmitStringInteger), Val(val), VT(vt) {}
Chris Lattner3163ca52010-02-21 03:22:59 +0000718
719 const std::string &getValue() const { return Val; }
720 MVT::SimpleValueType getVT() const { return VT; }
721
Chris Lattner9a515172010-02-25 02:04:40 +0000722 static inline bool classof(const Matcher *N) {
Chris Lattner3163ca52010-02-21 03:22:59 +0000723 return N->getKind() == EmitStringInteger;
724 }
725
Chris Lattner136ab782010-02-25 06:49:58 +0000726private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000727 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000728 virtual bool isEqualImpl(const Matcher *M) const {
729 return cast<EmitStringIntegerMatcher>(M)->Val == Val &&
730 cast<EmitStringIntegerMatcher>(M)->VT == VT;
731 }
732 virtual unsigned getHashImpl() const;
Chris Lattner3163ca52010-02-21 03:22:59 +0000733};
Chris Lattnerddfb5222010-02-18 22:03:03 +0000734
Chris Lattner9a515172010-02-25 02:04:40 +0000735/// EmitRegisterMatcher - This creates a new TargetConstant.
736class EmitRegisterMatcher : public Matcher {
Chris Lattnerddfb5222010-02-18 22:03:03 +0000737 /// Reg - The def for the register that we're emitting. If this is null, then
738 /// this is a reference to zero_reg.
739 Record *Reg;
740 MVT::SimpleValueType VT;
741public:
Chris Lattner9a515172010-02-25 02:04:40 +0000742 EmitRegisterMatcher(Record *reg, MVT::SimpleValueType vt)
743 : Matcher(EmitRegister), Reg(reg), VT(vt) {}
Chris Lattnerddfb5222010-02-18 22:03:03 +0000744
745 Record *getReg() const { return Reg; }
746 MVT::SimpleValueType getVT() const { return VT; }
747
Chris Lattner9a515172010-02-25 02:04:40 +0000748 static inline bool classof(const Matcher *N) {
Chris Lattnerddfb5222010-02-18 22:03:03 +0000749 return N->getKind() == EmitRegister;
750 }
751
Chris Lattner136ab782010-02-25 06:49:58 +0000752private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000753 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000754 virtual bool isEqualImpl(const Matcher *M) const {
755 return cast<EmitRegisterMatcher>(M)->Reg == Reg &&
756 cast<EmitRegisterMatcher>(M)->VT == VT;
757 }
758 virtual unsigned getHashImpl() const {
759 return ((unsigned)(intptr_t)Reg) << 4 | VT;
760 }
Chris Lattnerddfb5222010-02-18 22:03:03 +0000761};
Chris Lattner3163ca52010-02-21 03:22:59 +0000762
Chris Lattner9a515172010-02-25 02:04:40 +0000763/// EmitConvertToTargetMatcher - Emit an operation that reads a specified
Chris Lattner3163ca52010-02-21 03:22:59 +0000764/// recorded node and converts it from being a ISD::Constant to
765/// ISD::TargetConstant, likewise for ConstantFP.
Chris Lattner9a515172010-02-25 02:04:40 +0000766class EmitConvertToTargetMatcher : public Matcher {
Chris Lattner3163ca52010-02-21 03:22:59 +0000767 unsigned Slot;
768public:
Chris Lattner9a515172010-02-25 02:04:40 +0000769 EmitConvertToTargetMatcher(unsigned slot)
770 : Matcher(EmitConvertToTarget), Slot(slot) {}
Chris Lattner3163ca52010-02-21 03:22:59 +0000771
772 unsigned getSlot() const { return Slot; }
773
Chris Lattner9a515172010-02-25 02:04:40 +0000774 static inline bool classof(const Matcher *N) {
Chris Lattner3163ca52010-02-21 03:22:59 +0000775 return N->getKind() == EmitConvertToTarget;
776 }
777
Chris Lattner136ab782010-02-25 06:49:58 +0000778private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000779 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000780 virtual bool isEqualImpl(const Matcher *M) const {
781 return cast<EmitConvertToTargetMatcher>(M)->Slot == Slot;
782 }
783 virtual unsigned getHashImpl() const { return Slot; }
Chris Lattner3163ca52010-02-21 03:22:59 +0000784};
785
Chris Lattner9a515172010-02-25 02:04:40 +0000786/// EmitMergeInputChainsMatcher - Emit a node that merges a list of input
Chris Lattner3163ca52010-02-21 03:22:59 +0000787/// chains together with a token factor. The list of nodes are the nodes in the
788/// matched pattern that have chain input/outputs. This node adds all input
789/// chains of these nodes if they are not themselves a node in the pattern.
Chris Lattner9a515172010-02-25 02:04:40 +0000790class EmitMergeInputChainsMatcher : public Matcher {
Chris Lattner3163ca52010-02-21 03:22:59 +0000791 SmallVector<unsigned, 3> ChainNodes;
792public:
Chris Lattner9a515172010-02-25 02:04:40 +0000793 EmitMergeInputChainsMatcher(const unsigned *nodes, unsigned NumNodes)
794 : Matcher(EmitMergeInputChains), ChainNodes(nodes, nodes+NumNodes) {}
Chris Lattner3163ca52010-02-21 03:22:59 +0000795
796 unsigned getNumNodes() const { return ChainNodes.size(); }
797
798 unsigned getNode(unsigned i) const {
799 assert(i < ChainNodes.size());
800 return ChainNodes[i];
801 }
802
Chris Lattner9a515172010-02-25 02:04:40 +0000803 static inline bool classof(const Matcher *N) {
Chris Lattner3163ca52010-02-21 03:22:59 +0000804 return N->getKind() == EmitMergeInputChains;
805 }
806
Chris Lattner136ab782010-02-25 06:49:58 +0000807private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000808 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000809 virtual bool isEqualImpl(const Matcher *M) const {
810 return cast<EmitMergeInputChainsMatcher>(M)->ChainNodes == ChainNodes;
811 }
812 virtual unsigned getHashImpl() const;
Chris Lattner3163ca52010-02-21 03:22:59 +0000813};
814
Chris Lattner9a515172010-02-25 02:04:40 +0000815/// EmitCopyToRegMatcher - Emit a CopyToReg node from a value to a physreg,
Chris Lattner3163ca52010-02-21 03:22:59 +0000816/// pushing the chain and flag results.
817///
Chris Lattner9a515172010-02-25 02:04:40 +0000818class EmitCopyToRegMatcher : public Matcher {
Chris Lattner3163ca52010-02-21 03:22:59 +0000819 unsigned SrcSlot; // Value to copy into the physreg.
820 Record *DestPhysReg;
821public:
Chris Lattner9a515172010-02-25 02:04:40 +0000822 EmitCopyToRegMatcher(unsigned srcSlot, Record *destPhysReg)
823 : Matcher(EmitCopyToReg), SrcSlot(srcSlot), DestPhysReg(destPhysReg) {}
Chris Lattner3163ca52010-02-21 03:22:59 +0000824
825 unsigned getSrcSlot() const { return SrcSlot; }
826 Record *getDestPhysReg() const { return DestPhysReg; }
827
Chris Lattner9a515172010-02-25 02:04:40 +0000828 static inline bool classof(const Matcher *N) {
Chris Lattner3163ca52010-02-21 03:22:59 +0000829 return N->getKind() == EmitCopyToReg;
830 }
831
Chris Lattner136ab782010-02-25 06:49:58 +0000832private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000833 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000834 virtual bool isEqualImpl(const Matcher *M) const {
835 return cast<EmitCopyToRegMatcher>(M)->SrcSlot == SrcSlot &&
836 cast<EmitCopyToRegMatcher>(M)->DestPhysReg == DestPhysReg;
837 }
838 virtual unsigned getHashImpl() const {
839 return SrcSlot ^ ((unsigned)(intptr_t)DestPhysReg << 4);
840 }
Chris Lattner3163ca52010-02-21 03:22:59 +0000841};
842
843
844
Chris Lattner9a515172010-02-25 02:04:40 +0000845/// EmitNodeXFormMatcher - Emit an operation that runs an SDNodeXForm on a
Chris Lattner3163ca52010-02-21 03:22:59 +0000846/// recorded node and records the result.
Chris Lattner9a515172010-02-25 02:04:40 +0000847class EmitNodeXFormMatcher : public Matcher {
Chris Lattner3163ca52010-02-21 03:22:59 +0000848 unsigned Slot;
849 Record *NodeXForm;
850public:
Chris Lattner9a515172010-02-25 02:04:40 +0000851 EmitNodeXFormMatcher(unsigned slot, Record *nodeXForm)
852 : Matcher(EmitNodeXForm), Slot(slot), NodeXForm(nodeXForm) {}
Chris Lattner3163ca52010-02-21 03:22:59 +0000853
854 unsigned getSlot() const { return Slot; }
855 Record *getNodeXForm() const { return NodeXForm; }
856
Chris Lattner9a515172010-02-25 02:04:40 +0000857 static inline bool classof(const Matcher *N) {
Chris Lattner3163ca52010-02-21 03:22:59 +0000858 return N->getKind() == EmitNodeXForm;
859 }
860
Chris Lattner136ab782010-02-25 06:49:58 +0000861private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000862 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000863 virtual bool isEqualImpl(const Matcher *M) const {
864 return cast<EmitNodeXFormMatcher>(M)->Slot == Slot &&
865 cast<EmitNodeXFormMatcher>(M)->NodeXForm == NodeXForm;
866 }
867 virtual unsigned getHashImpl() const {
868 return Slot ^ ((unsigned)(intptr_t)NodeXForm << 4);
869 }
Chris Lattner3163ca52010-02-21 03:22:59 +0000870};
Chris Lattnerddfb5222010-02-18 22:03:03 +0000871
Chris Lattner9a515172010-02-25 02:04:40 +0000872/// EmitNodeMatcher - This signals a successful match and generates a node.
873class EmitNodeMatcher : public Matcher {
Chris Lattner3163ca52010-02-21 03:22:59 +0000874 std::string OpcodeName;
875 const SmallVector<MVT::SimpleValueType, 3> VTs;
876 const SmallVector<unsigned, 6> Operands;
877 bool HasChain, HasFlag, HasMemRefs;
Chris Lattnerddfb5222010-02-18 22:03:03 +0000878
Chris Lattner3163ca52010-02-21 03:22:59 +0000879 /// NumFixedArityOperands - If this is a fixed arity node, this is set to -1.
880 /// If this is a varidic node, this is set to the number of fixed arity
881 /// operands in the root of the pattern. The rest are appended to this node.
882 int NumFixedArityOperands;
883public:
Chris Lattner9a515172010-02-25 02:04:40 +0000884 EmitNodeMatcher(const std::string &opcodeName,
Chris Lattner136ab782010-02-25 06:49:58 +0000885 const MVT::SimpleValueType *vts, unsigned numvts,
886 const unsigned *operands, unsigned numops,
887 bool hasChain, bool hasFlag, bool hasmemrefs,
888 int numfixedarityoperands)
Chris Lattner9a515172010-02-25 02:04:40 +0000889 : Matcher(EmitNode), OpcodeName(opcodeName),
Chris Lattner3163ca52010-02-21 03:22:59 +0000890 VTs(vts, vts+numvts), Operands(operands, operands+numops),
891 HasChain(hasChain), HasFlag(hasFlag), HasMemRefs(hasmemrefs),
892 NumFixedArityOperands(numfixedarityoperands) {}
893
894 const std::string &getOpcodeName() const { return OpcodeName; }
895
896 unsigned getNumVTs() const { return VTs.size(); }
897 MVT::SimpleValueType getVT(unsigned i) const {
898 assert(i < VTs.size());
899 return VTs[i];
900 }
901
902 unsigned getNumOperands() const { return Operands.size(); }
903 unsigned getOperand(unsigned i) const {
904 assert(i < Operands.size());
905 return Operands[i];
906 }
907
908 bool hasChain() const { return HasChain; }
909 bool hasFlag() const { return HasFlag; }
910 bool hasMemRefs() const { return HasMemRefs; }
911 int getNumFixedArityOperands() const { return NumFixedArityOperands; }
Chris Lattnerddfb5222010-02-18 22:03:03 +0000912
Chris Lattner9a515172010-02-25 02:04:40 +0000913 static inline bool classof(const Matcher *N) {
Chris Lattnerddfb5222010-02-18 22:03:03 +0000914 return N->getKind() == EmitNode;
915 }
916
Chris Lattner136ab782010-02-25 06:49:58 +0000917private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000918 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000919 virtual bool isEqualImpl(const Matcher *M) const;
920 virtual unsigned getHashImpl() const;
Chris Lattnerddfb5222010-02-18 22:03:03 +0000921};
Chris Lattnerb085ea12010-02-21 06:03:07 +0000922
Chris Lattner9a515172010-02-25 02:04:40 +0000923/// MarkFlagResultsMatcher - This node indicates which non-root nodes in the
924/// pattern produce flags. This allows CompleteMatchMatcher to update them
Chris Lattner69f60c82010-02-24 05:33:42 +0000925/// with the output flag of the resultant code.
Chris Lattner9a515172010-02-25 02:04:40 +0000926class MarkFlagResultsMatcher : public Matcher {
Chris Lattner69f60c82010-02-24 05:33:42 +0000927 SmallVector<unsigned, 3> FlagResultNodes;
928public:
Chris Lattner9a515172010-02-25 02:04:40 +0000929 MarkFlagResultsMatcher(const unsigned *nodes, unsigned NumNodes)
930 : Matcher(MarkFlagResults), FlagResultNodes(nodes, nodes+NumNodes) {}
Chris Lattner69f60c82010-02-24 05:33:42 +0000931
932 unsigned getNumNodes() const { return FlagResultNodes.size(); }
933
934 unsigned getNode(unsigned i) const {
935 assert(i < FlagResultNodes.size());
936 return FlagResultNodes[i];
937 }
938
Chris Lattner9a515172010-02-25 02:04:40 +0000939 static inline bool classof(const Matcher *N) {
Chris Lattner69f60c82010-02-24 05:33:42 +0000940 return N->getKind() == MarkFlagResults;
941 }
942
Chris Lattner136ab782010-02-25 06:49:58 +0000943private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000944 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000945 virtual bool isEqualImpl(const Matcher *M) const {
946 return cast<MarkFlagResultsMatcher>(M)->FlagResultNodes == FlagResultNodes;
947 }
948 virtual unsigned getHashImpl() const;
Chris Lattner69f60c82010-02-24 05:33:42 +0000949};
950
Chris Lattner9a515172010-02-25 02:04:40 +0000951/// CompleteMatchMatcher - Complete a match by replacing the results of the
Chris Lattnerb085ea12010-02-21 06:03:07 +0000952/// pattern with the newly generated nodes. This also prints a comment
953/// indicating the source and dest patterns.
Chris Lattner9a515172010-02-25 02:04:40 +0000954class CompleteMatchMatcher : public Matcher {
Chris Lattnerb085ea12010-02-21 06:03:07 +0000955 SmallVector<unsigned, 2> Results;
Chris Lattner3163ca52010-02-21 03:22:59 +0000956 const PatternToMatch &Pattern;
957public:
Chris Lattner9a515172010-02-25 02:04:40 +0000958 CompleteMatchMatcher(const unsigned *results, unsigned numresults,
Chris Lattnerb085ea12010-02-21 06:03:07 +0000959 const PatternToMatch &pattern)
Chris Lattner9a515172010-02-25 02:04:40 +0000960 : Matcher(CompleteMatch), Results(results, results+numresults),
Chris Lattnerb085ea12010-02-21 06:03:07 +0000961 Pattern(pattern) {}
962
963 unsigned getNumResults() const { return Results.size(); }
964 unsigned getResult(unsigned R) const { return Results[R]; }
Chris Lattner3163ca52010-02-21 03:22:59 +0000965 const PatternToMatch &getPattern() const { return Pattern; }
966
Chris Lattner9a515172010-02-25 02:04:40 +0000967 static inline bool classof(const Matcher *N) {
Chris Lattnerb085ea12010-02-21 06:03:07 +0000968 return N->getKind() == CompleteMatch;
Chris Lattner3163ca52010-02-21 03:22:59 +0000969 }
970
Chris Lattner136ab782010-02-25 06:49:58 +0000971private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000972 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000973 virtual bool isEqualImpl(const Matcher *M) const {
974 return cast<CompleteMatchMatcher>(M)->Results == Results &&
975 &cast<CompleteMatchMatcher>(M)->Pattern == &Pattern;
976 }
977 virtual unsigned getHashImpl() const;
Chris Lattner3163ca52010-02-21 03:22:59 +0000978};
Chris Lattnerddfb5222010-02-18 22:03:03 +0000979
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000980} // end namespace llvm
981
982#endif