blob: ec61fcd1dab47e396af8b70c4e62c72d6643600f [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 Lattnere7d6e3c2010-02-15 08:04:42 +000026
Chris Lattner9a515172010-02-25 02:04:40 +000027Matcher *ConvertPatternToMatcher(const PatternToMatch &Pattern,
28 const CodeGenDAGPatterns &CGP);
29Matcher *OptimizeMatcher(Matcher *Matcher);
30void EmitMatcherTable(const Matcher *Matcher, raw_ostream &OS);
Chris Lattnere7d6e3c2010-02-15 08:04:42 +000031
32
Chris Lattner9a515172010-02-25 02:04:40 +000033/// Matcher - Base class for all the the DAG ISel Matcher representation
Chris Lattnere7d6e3c2010-02-15 08:04:42 +000034/// nodes.
Chris Lattner9a515172010-02-25 02:04:40 +000035class Matcher {
Chris Lattner78039ec2010-02-18 02:53:41 +000036 // The next matcher node that is executed after this one. Null if this is the
37 // last stage of a match.
Chris Lattner9a515172010-02-25 02:04:40 +000038 OwningPtr<Matcher> Next;
Chris Lattnere7d6e3c2010-02-15 08:04:42 +000039public:
40 enum KindTy {
Chris Lattner3163ca52010-02-21 03:22:59 +000041 // Matcher state manipulation.
Chris Lattnerac10e4f2010-02-25 01:56:48 +000042 Scope, // Push a checking scope.
Chris Lattner3163ca52010-02-21 03:22:59 +000043 RecordNode, // Record the current node.
Chris Lattner3ee1bc42010-02-24 07:31:45 +000044 RecordChild, // Record a child of the current node.
Chris Lattner3163ca52010-02-21 03:22:59 +000045 RecordMemRef, // Record the memref in the current node.
46 CaptureFlagInput, // If the current node has an input flag, save it.
47 MoveChild, // Move current node to specified child.
48 MoveParent, // Move current node to parent.
Chris Lattnere7d6e3c2010-02-15 08:04:42 +000049
Chris Lattnerddfb5222010-02-18 22:03:03 +000050 // Predicate checking.
Chris Lattner3163ca52010-02-21 03:22:59 +000051 CheckSame, // Fail if not same as prev match.
Chris Lattnere7d6e3c2010-02-15 08:04:42 +000052 CheckPatternPredicate,
Chris Lattner3163ca52010-02-21 03:22:59 +000053 CheckPredicate, // Fail if node predicate fails.
54 CheckOpcode, // Fail if not opcode.
Chris Lattnerf58c08e2010-02-22 22:30:37 +000055 CheckMultiOpcode, // Fail if not in opcode list.
Chris Lattner3163ca52010-02-21 03:22:59 +000056 CheckType, // Fail if not correct type.
Chris Lattner3c664b32010-02-24 20:15:25 +000057 CheckChildType, // Fail if child has wrong type.
Chris Lattner3163ca52010-02-21 03:22:59 +000058 CheckInteger, // Fail if wrong val.
59 CheckCondCode, // Fail if not condcode.
Chris Lattnere7d6e3c2010-02-15 08:04:42 +000060 CheckValueType,
61 CheckComplexPat,
62 CheckAndImm,
Chris Lattner9de39482010-02-16 06:10:58 +000063 CheckOrImm,
Chris Lattner283adcb2010-02-17 06:23:39 +000064 CheckFoldableChainNode,
Chris Lattnerddfb5222010-02-18 22:03:03 +000065 CheckChainCompatible,
66
67 // Node creation/emisssion.
Chris Lattner3163ca52010-02-21 03:22:59 +000068 EmitInteger, // Create a TargetConstant
69 EmitStringInteger, // Create a TargetConstant from a string.
70 EmitRegister, // Create a register.
71 EmitConvertToTarget, // Convert a imm/fpimm to target imm/fpimm
72 EmitMergeInputChains, // Merge together a chains for an input.
73 EmitCopyToReg, // Emit a copytoreg into a physreg.
74 EmitNode, // Create a DAG node
75 EmitNodeXForm, // Run a SDNodeXForm
Chris Lattner69f60c82010-02-24 05:33:42 +000076 MarkFlagResults, // Indicate which interior nodes have flag results.
Chris Lattnerb085ea12010-02-21 06:03:07 +000077 CompleteMatch // Finish a match and update the results.
Chris Lattnere7d6e3c2010-02-15 08:04:42 +000078 };
79 const KindTy Kind;
Chris Lattner3b31c982010-02-18 02:49:24 +000080
Chris Lattnere7d6e3c2010-02-15 08:04:42 +000081protected:
Chris Lattner9a515172010-02-25 02:04:40 +000082 Matcher(KindTy K) : Kind(K) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +000083public:
Chris Lattner9a515172010-02-25 02:04:40 +000084 virtual ~Matcher() {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +000085
86 KindTy getKind() const { return Kind; }
Chris Lattner3b31c982010-02-18 02:49:24 +000087
Chris Lattner9a515172010-02-25 02:04:40 +000088 Matcher *getNext() { return Next.get(); }
89 const Matcher *getNext() const { return Next.get(); }
90 void setNext(Matcher *C) { Next.reset(C); }
91 Matcher *takeNext() { return Next.take(); }
Chris Lattner3ee1bc42010-02-24 07:31:45 +000092
Chris Lattner9a515172010-02-25 02:04:40 +000093 OwningPtr<Matcher> &getNextPtr() { return Next; }
Chris Lattnere7d6e3c2010-02-15 08:04:42 +000094
Chris Lattner9a515172010-02-25 02:04:40 +000095 static inline bool classof(const Matcher *) { return true; }
Chris Lattnere7d6e3c2010-02-15 08:04:42 +000096
Chris Lattner136ab782010-02-25 06:49:58 +000097 bool isEqual(const Matcher *M) const {
98 if (getKind() != M->getKind()) return false;
99 return isEqualImpl(M);
100 }
101
102 unsigned getHash() const {
Chris Lattner54ee7362010-02-26 07:35:27 +0000103 // Clear the high bit so we don't conflict with tombstones etc.
104 return ((getHashImpl() << 4) ^ getKind()) & (~0U>>1);
Chris Lattner136ab782010-02-25 06:49:58 +0000105 }
106
Chris Lattner392b1bf2010-02-25 06:53:39 +0000107 void print(raw_ostream &OS, unsigned indent = 0) const;
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000108 void dump() const;
Chris Lattner3b31c982010-02-18 02:49:24 +0000109protected:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000110 virtual void printImpl(raw_ostream &OS, unsigned indent) const = 0;
Chris Lattner136ab782010-02-25 06:49:58 +0000111 virtual bool isEqualImpl(const Matcher *M) const = 0;
112 virtual unsigned getHashImpl() const = 0;
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000113};
114
Chris Lattner42363662010-02-25 19:00:39 +0000115/// ScopeMatcher - This attempts to match each of its children to find the first
116/// one that successfully matches. If one child fails, it tries the next child.
117/// If none of the children match then this check fails. It never has a 'next'.
Chris Lattner9a515172010-02-25 02:04:40 +0000118class ScopeMatcher : public Matcher {
Chris Lattner42363662010-02-25 19:00:39 +0000119 SmallVector<Matcher*, 4> Children;
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000120public:
Chris Lattner42363662010-02-25 19:00:39 +0000121 ScopeMatcher(Matcher *const *children, unsigned numchildren)
122 : Matcher(Scope), Children(children, children+numchildren) {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000123 }
Chris Lattner42363662010-02-25 19:00:39 +0000124 virtual ~ScopeMatcher();
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000125
Chris Lattner42363662010-02-25 19:00:39 +0000126 unsigned getNumChildren() const { return Children.size(); }
127
128 Matcher *getChild(unsigned i) { return Children[i]; }
129 const Matcher *getChild(unsigned i) const { return Children[i]; }
130
131 void resetChild(unsigned i, Matcher *N) {
132 delete Children[i];
133 Children[i] = N;
134 }
135
136 Matcher *takeChild(unsigned i) {
137 Matcher *Res = Children[i];
138 Children[i] = 0;
139 return Res;
140 }
Chris Lattner54ee7362010-02-26 07:35:27 +0000141
142 void setNumChildren(unsigned NC) {
143 if (NC < Children.size()) {
144 // delete any children we're about to lose pointers to.
145 for (unsigned i = NC, e = Children.size(); i != e; ++i)
146 delete Children[i];
147 }
148 Children.resize(NC);
149 }
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000150
Chris Lattner9a515172010-02-25 02:04:40 +0000151 static inline bool classof(const Matcher *N) {
Chris Lattnerac10e4f2010-02-25 01:56:48 +0000152 return N->getKind() == Scope;
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000153 }
154
Chris Lattner136ab782010-02-25 06:49:58 +0000155private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000156 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000157 virtual bool isEqualImpl(const Matcher *M) const { return false; }
Chris Lattner42363662010-02-25 19:00:39 +0000158 virtual unsigned getHashImpl() const { return 12312; }
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000159};
160
Chris Lattner9a515172010-02-25 02:04:40 +0000161/// RecordMatcher - Save the current node in the operand list.
162class RecordMatcher : public Matcher {
Chris Lattner56cf88d2010-02-17 01:03:09 +0000163 /// WhatFor - This is a string indicating why we're recording this. This
164 /// should only be used for comment generation not anything semantic.
165 std::string WhatFor;
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000166public:
Chris Lattner9a515172010-02-25 02:04:40 +0000167 RecordMatcher(const std::string &whatfor)
168 : Matcher(RecordNode), WhatFor(whatfor) {}
Chris Lattner56cf88d2010-02-17 01:03:09 +0000169
Chris Lattner086ca522010-02-17 01:27:29 +0000170 const std::string &getWhatFor() const { return WhatFor; }
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000171
Chris Lattner9a515172010-02-25 02:04:40 +0000172 static inline bool classof(const Matcher *N) {
Chris Lattnerddfb5222010-02-18 22:03:03 +0000173 return N->getKind() == RecordNode;
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 true; }
179 virtual unsigned getHashImpl() const { return 0; }
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000180};
181
Chris Lattner9a515172010-02-25 02:04:40 +0000182/// RecordChildMatcher - Save a numbered child of the current node, or fail
Chris Lattner3ee1bc42010-02-24 07:31:45 +0000183/// the match if it doesn't exist. This is logically equivalent to:
184/// MoveChild N + RecordNode + MoveParent.
Chris Lattner9a515172010-02-25 02:04:40 +0000185class RecordChildMatcher : public Matcher {
Chris Lattner3ee1bc42010-02-24 07:31:45 +0000186 unsigned ChildNo;
187
188 /// WhatFor - This is a string indicating why we're recording this. This
189 /// should only be used for comment generation not anything semantic.
190 std::string WhatFor;
191public:
Chris Lattner9a515172010-02-25 02:04:40 +0000192 RecordChildMatcher(unsigned childno, const std::string &whatfor)
193 : Matcher(RecordChild), ChildNo(childno), WhatFor(whatfor) {}
Chris Lattner3ee1bc42010-02-24 07:31:45 +0000194
195 unsigned getChildNo() const { return ChildNo; }
196 const std::string &getWhatFor() const { return WhatFor; }
197
Chris Lattner9a515172010-02-25 02:04:40 +0000198 static inline bool classof(const Matcher *N) {
Chris Lattner3ee1bc42010-02-24 07:31:45 +0000199 return N->getKind() == RecordChild;
200 }
201
Chris Lattner136ab782010-02-25 06:49:58 +0000202private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000203 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000204 virtual bool isEqualImpl(const Matcher *M) const {
205 return cast<RecordChildMatcher>(M)->getChildNo() == getChildNo();
206 }
207 virtual unsigned getHashImpl() const { return getChildNo(); }
Chris Lattner3ee1bc42010-02-24 07:31:45 +0000208};
209
Chris Lattner9a515172010-02-25 02:04:40 +0000210/// RecordMemRefMatcher - Save the current node's memref.
211class RecordMemRefMatcher : public Matcher {
Chris Lattner3163ca52010-02-21 03:22:59 +0000212public:
Chris Lattner9a515172010-02-25 02:04:40 +0000213 RecordMemRefMatcher() : Matcher(RecordMemRef) {}
Chris Lattner3163ca52010-02-21 03:22:59 +0000214
Chris Lattner9a515172010-02-25 02:04:40 +0000215 static inline bool classof(const Matcher *N) {
Chris Lattner3163ca52010-02-21 03:22:59 +0000216 return N->getKind() == RecordMemRef;
217 }
218
Chris Lattner136ab782010-02-25 06:49:58 +0000219private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000220 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000221 virtual bool isEqualImpl(const Matcher *M) const { return true; }
222 virtual unsigned getHashImpl() const { return 0; }
Chris Lattner3163ca52010-02-21 03:22:59 +0000223};
224
225
Chris Lattner9a515172010-02-25 02:04:40 +0000226/// CaptureFlagInputMatcher - If the current record has a flag input, record
Chris Lattner3163ca52010-02-21 03:22:59 +0000227/// it so that it is used as an input to the generated code.
Chris Lattner9a515172010-02-25 02:04:40 +0000228class CaptureFlagInputMatcher : public Matcher {
Chris Lattner3163ca52010-02-21 03:22:59 +0000229public:
Chris Lattner9a515172010-02-25 02:04:40 +0000230 CaptureFlagInputMatcher() : Matcher(CaptureFlagInput) {}
Chris Lattner3163ca52010-02-21 03:22:59 +0000231
Chris Lattner9a515172010-02-25 02:04:40 +0000232 static inline bool classof(const Matcher *N) {
Chris Lattner3163ca52010-02-21 03:22:59 +0000233 return N->getKind() == CaptureFlagInput;
234 }
235
Chris Lattner136ab782010-02-25 06:49:58 +0000236private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000237 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000238 virtual bool isEqualImpl(const Matcher *M) const { return true; }
239 virtual unsigned getHashImpl() const { return 0; }
Chris Lattner3163ca52010-02-21 03:22:59 +0000240};
241
Chris Lattner9a515172010-02-25 02:04:40 +0000242/// MoveChildMatcher - This tells the interpreter to move into the
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000243/// specified child node.
Chris Lattner9a515172010-02-25 02:04:40 +0000244class MoveChildMatcher : public Matcher {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000245 unsigned ChildNo;
246public:
Chris Lattner9a515172010-02-25 02:04:40 +0000247 MoveChildMatcher(unsigned childNo) : Matcher(MoveChild), ChildNo(childNo) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000248
249 unsigned getChildNo() const { return ChildNo; }
250
Chris Lattner9a515172010-02-25 02:04:40 +0000251 static inline bool classof(const Matcher *N) {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000252 return N->getKind() == MoveChild;
253 }
254
Chris Lattner136ab782010-02-25 06:49:58 +0000255private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000256 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000257 virtual bool isEqualImpl(const Matcher *M) const {
258 return cast<MoveChildMatcher>(M)->getChildNo() == getChildNo();
259 }
260 virtual unsigned getHashImpl() const { return getChildNo(); }
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000261};
262
Chris Lattner9a515172010-02-25 02:04:40 +0000263/// MoveParentMatcher - This tells the interpreter to move to the parent
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000264/// of the current node.
Chris Lattner9a515172010-02-25 02:04:40 +0000265class MoveParentMatcher : public Matcher {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000266public:
Chris Lattner9a515172010-02-25 02:04:40 +0000267 MoveParentMatcher() : Matcher(MoveParent) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000268
Chris Lattner9a515172010-02-25 02:04:40 +0000269 static inline bool classof(const Matcher *N) {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000270 return N->getKind() == MoveParent;
271 }
272
Chris Lattner136ab782010-02-25 06:49:58 +0000273private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000274 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000275 virtual bool isEqualImpl(const Matcher *M) const { return true; }
276 virtual unsigned getHashImpl() const { return 0; }
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000277};
278
Chris Lattner9a515172010-02-25 02:04:40 +0000279/// CheckSameMatcher - This checks to see if this node is exactly the same
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000280/// node as the specified match that was recorded with 'Record'. This is used
281/// when patterns have the same name in them, like '(mul GPR:$in, GPR:$in)'.
Chris Lattner9a515172010-02-25 02:04:40 +0000282class CheckSameMatcher : public Matcher {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000283 unsigned MatchNumber;
284public:
Chris Lattner9a515172010-02-25 02:04:40 +0000285 CheckSameMatcher(unsigned matchnumber)
Chris Lattner136ab782010-02-25 06:49:58 +0000286 : Matcher(CheckSame), MatchNumber(matchnumber) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000287
288 unsigned getMatchNumber() const { return MatchNumber; }
289
Chris Lattner9a515172010-02-25 02:04:40 +0000290 static inline bool classof(const Matcher *N) {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000291 return N->getKind() == CheckSame;
292 }
293
Chris Lattner136ab782010-02-25 06:49:58 +0000294private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000295 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000296 virtual bool isEqualImpl(const Matcher *M) const {
297 return cast<CheckSameMatcher>(M)->getMatchNumber() == getMatchNumber();
298 }
299 virtual unsigned getHashImpl() const { return getMatchNumber(); }
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000300};
301
Chris Lattner9a515172010-02-25 02:04:40 +0000302/// CheckPatternPredicateMatcher - This checks the target-specific predicate
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000303/// to see if the entire pattern is capable of matching. This predicate does
304/// not take a node as input. This is used for subtarget feature checks etc.
Chris Lattner9a515172010-02-25 02:04:40 +0000305class CheckPatternPredicateMatcher : public Matcher {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000306 std::string Predicate;
307public:
Chris Lattner9a515172010-02-25 02:04:40 +0000308 CheckPatternPredicateMatcher(StringRef predicate)
Chris Lattner136ab782010-02-25 06:49:58 +0000309 : Matcher(CheckPatternPredicate), Predicate(predicate) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000310
311 StringRef getPredicate() const { return Predicate; }
312
Chris Lattner9a515172010-02-25 02:04:40 +0000313 static inline bool classof(const Matcher *N) {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000314 return N->getKind() == CheckPatternPredicate;
315 }
316
Chris Lattner136ab782010-02-25 06:49:58 +0000317private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000318 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000319 virtual bool isEqualImpl(const Matcher *M) const {
320 return cast<CheckPatternPredicateMatcher>(M)->getPredicate() == Predicate;
321 }
322 virtual unsigned getHashImpl() const;
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000323};
324
Chris Lattner9a515172010-02-25 02:04:40 +0000325/// CheckPredicateMatcher - This checks the target-specific predicate to
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000326/// see if the node is acceptable.
Chris Lattner9a515172010-02-25 02:04:40 +0000327class CheckPredicateMatcher : public Matcher {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000328 StringRef PredName;
329public:
Chris Lattner9a515172010-02-25 02:04:40 +0000330 CheckPredicateMatcher(StringRef predname)
331 : Matcher(CheckPredicate), PredName(predname) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000332
333 StringRef getPredicateName() const { return PredName; }
334
Chris Lattner9a515172010-02-25 02:04:40 +0000335 static inline bool classof(const Matcher *N) {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000336 return N->getKind() == CheckPredicate;
337 }
338
Chris Lattner136ab782010-02-25 06:49:58 +0000339private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000340 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000341 virtual bool isEqualImpl(const Matcher *M) const {
342 return cast<CheckPredicateMatcher>(M)->PredName == PredName;
343 }
344 virtual unsigned getHashImpl() const;
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000345};
346
347
Chris Lattner9a515172010-02-25 02:04:40 +0000348/// CheckOpcodeMatcher - This checks to see if the current node has the
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000349/// specified opcode, if not it fails to match.
Chris Lattner9a515172010-02-25 02:04:40 +0000350class CheckOpcodeMatcher : public Matcher {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000351 StringRef OpcodeName;
352public:
Chris Lattner9a515172010-02-25 02:04:40 +0000353 CheckOpcodeMatcher(StringRef opcodename)
354 : Matcher(CheckOpcode), OpcodeName(opcodename) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000355
356 StringRef getOpcodeName() const { return OpcodeName; }
357
Chris Lattner9a515172010-02-25 02:04:40 +0000358 static inline bool classof(const Matcher *N) {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000359 return N->getKind() == CheckOpcode;
360 }
361
Chris Lattner136ab782010-02-25 06:49:58 +0000362private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000363 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000364 virtual bool isEqualImpl(const Matcher *M) const {
365 return cast<CheckOpcodeMatcher>(M)->OpcodeName == OpcodeName;
366 }
367 virtual unsigned getHashImpl() const;
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000368};
369
Chris Lattner9a515172010-02-25 02:04:40 +0000370/// CheckMultiOpcodeMatcher - This checks to see if the current node has one
Chris Lattnerf58c08e2010-02-22 22:30:37 +0000371/// of the specified opcode, if not it fails to match.
Chris Lattner9a515172010-02-25 02:04:40 +0000372class CheckMultiOpcodeMatcher : public Matcher {
Chris Lattnerf58c08e2010-02-22 22:30:37 +0000373 SmallVector<StringRef, 4> OpcodeNames;
374public:
Chris Lattner9a515172010-02-25 02:04:40 +0000375 CheckMultiOpcodeMatcher(const StringRef *opcodes, unsigned numops)
376 : Matcher(CheckMultiOpcode), OpcodeNames(opcodes, opcodes+numops) {}
Chris Lattnerf58c08e2010-02-22 22:30:37 +0000377
378 unsigned getNumOpcodeNames() const { return OpcodeNames.size(); }
379 StringRef getOpcodeName(unsigned i) const { return OpcodeNames[i]; }
380
Chris Lattner9a515172010-02-25 02:04:40 +0000381 static inline bool classof(const Matcher *N) {
Chris Lattnerf58c08e2010-02-22 22:30:37 +0000382 return N->getKind() == CheckMultiOpcode;
383 }
384
Chris Lattner136ab782010-02-25 06:49:58 +0000385private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000386 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000387 virtual bool isEqualImpl(const Matcher *M) const {
388 return cast<CheckMultiOpcodeMatcher>(M)->OpcodeNames == OpcodeNames;
389 }
390 virtual unsigned getHashImpl() const;
Chris Lattnerf58c08e2010-02-22 22:30:37 +0000391};
392
393
394
Chris Lattner9a515172010-02-25 02:04:40 +0000395/// CheckTypeMatcher - This checks to see if the current node has the
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000396/// specified type, if not it fails to match.
Chris Lattner9a515172010-02-25 02:04:40 +0000397class CheckTypeMatcher : public Matcher {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000398 MVT::SimpleValueType Type;
399public:
Chris Lattner9a515172010-02-25 02:04:40 +0000400 CheckTypeMatcher(MVT::SimpleValueType type)
401 : Matcher(CheckType), Type(type) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000402
403 MVT::SimpleValueType getType() const { return Type; }
404
Chris Lattner9a515172010-02-25 02:04:40 +0000405 static inline bool classof(const Matcher *N) {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000406 return N->getKind() == CheckType;
407 }
408
Chris Lattner136ab782010-02-25 06:49:58 +0000409private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000410 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000411 virtual bool isEqualImpl(const Matcher *M) const {
Chris Lattner2d0e0792010-02-26 08:05:36 +0000412 return cast<CheckTypeMatcher>(M)->Type == Type;
Chris Lattner136ab782010-02-25 06:49:58 +0000413 }
414 virtual unsigned getHashImpl() const { return Type; }
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000415};
Chris Lattner3c664b32010-02-24 20:15:25 +0000416
Chris Lattner9a515172010-02-25 02:04:40 +0000417/// CheckChildTypeMatcher - This checks to see if a child node has the
Chris Lattner3c664b32010-02-24 20:15:25 +0000418/// specified type, if not it fails to match.
Chris Lattner9a515172010-02-25 02:04:40 +0000419class CheckChildTypeMatcher : public Matcher {
Chris Lattner3c664b32010-02-24 20:15:25 +0000420 unsigned ChildNo;
421 MVT::SimpleValueType Type;
422public:
Chris Lattner9a515172010-02-25 02:04:40 +0000423 CheckChildTypeMatcher(unsigned childno, MVT::SimpleValueType type)
424 : Matcher(CheckChildType), ChildNo(childno), Type(type) {}
Chris Lattner3c664b32010-02-24 20:15:25 +0000425
426 unsigned getChildNo() const { return ChildNo; }
427 MVT::SimpleValueType getType() const { return Type; }
428
Chris Lattner9a515172010-02-25 02:04:40 +0000429 static inline bool classof(const Matcher *N) {
Chris Lattner3c664b32010-02-24 20:15:25 +0000430 return N->getKind() == CheckChildType;
431 }
432
Chris Lattner136ab782010-02-25 06:49:58 +0000433private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000434 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000435 virtual bool isEqualImpl(const Matcher *M) const {
436 return cast<CheckChildTypeMatcher>(M)->ChildNo == ChildNo &&
437 cast<CheckChildTypeMatcher>(M)->Type == Type;
438 }
439 virtual unsigned getHashImpl() const { return (Type << 3) | ChildNo; }
Chris Lattner3c664b32010-02-24 20:15:25 +0000440};
441
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000442
Chris Lattner9a515172010-02-25 02:04:40 +0000443/// CheckIntegerMatcher - This checks to see if the current node is a
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000444/// ConstantSDNode with the specified integer value, if not it fails to match.
Chris Lattner9a515172010-02-25 02:04:40 +0000445class CheckIntegerMatcher : public Matcher {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000446 int64_t Value;
447public:
Chris Lattner9a515172010-02-25 02:04:40 +0000448 CheckIntegerMatcher(int64_t value)
449 : Matcher(CheckInteger), Value(value) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000450
451 int64_t getValue() const { return Value; }
452
Chris Lattner9a515172010-02-25 02:04:40 +0000453 static inline bool classof(const Matcher *N) {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000454 return N->getKind() == CheckInteger;
455 }
456
Chris Lattner136ab782010-02-25 06:49:58 +0000457private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000458 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000459 virtual bool isEqualImpl(const Matcher *M) const {
460 return cast<CheckIntegerMatcher>(M)->Value == Value;
461 }
462 virtual unsigned getHashImpl() const { return Value; }
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000463};
464
Chris Lattner9a515172010-02-25 02:04:40 +0000465/// CheckCondCodeMatcher - This checks to see if the current node is a
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000466/// CondCodeSDNode with the specified condition, if not it fails to match.
Chris Lattner9a515172010-02-25 02:04:40 +0000467class CheckCondCodeMatcher : public Matcher {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000468 StringRef CondCodeName;
469public:
Chris Lattner9a515172010-02-25 02:04:40 +0000470 CheckCondCodeMatcher(StringRef condcodename)
471 : Matcher(CheckCondCode), CondCodeName(condcodename) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000472
473 StringRef getCondCodeName() const { return CondCodeName; }
474
Chris Lattner9a515172010-02-25 02:04:40 +0000475 static inline bool classof(const Matcher *N) {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000476 return N->getKind() == CheckCondCode;
477 }
478
Chris Lattner136ab782010-02-25 06:49:58 +0000479private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000480 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000481 virtual bool isEqualImpl(const Matcher *M) const {
482 return cast<CheckCondCodeMatcher>(M)->CondCodeName == CondCodeName;
483 }
484 virtual unsigned getHashImpl() const;
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000485};
486
Chris Lattner9a515172010-02-25 02:04:40 +0000487/// CheckValueTypeMatcher - This checks to see if the current node is a
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000488/// VTSDNode with the specified type, if not it fails to match.
Chris Lattner9a515172010-02-25 02:04:40 +0000489class CheckValueTypeMatcher : public Matcher {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000490 StringRef TypeName;
491public:
Chris Lattner9a515172010-02-25 02:04:40 +0000492 CheckValueTypeMatcher(StringRef type_name)
493 : Matcher(CheckValueType), TypeName(type_name) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000494
495 StringRef getTypeName() const { return TypeName; }
496
Chris Lattner9a515172010-02-25 02:04:40 +0000497 static inline bool classof(const Matcher *N) {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000498 return N->getKind() == CheckValueType;
499 }
500
Chris Lattner136ab782010-02-25 06:49:58 +0000501private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000502 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000503 virtual bool isEqualImpl(const Matcher *M) const {
504 return cast<CheckValueTypeMatcher>(M)->TypeName == TypeName;
505 }
506 virtual unsigned getHashImpl() const;
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000507};
508
509
510
Chris Lattner9a515172010-02-25 02:04:40 +0000511/// CheckComplexPatMatcher - This node runs the specified ComplexPattern on
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000512/// the current node.
Chris Lattner9a515172010-02-25 02:04:40 +0000513class CheckComplexPatMatcher : public Matcher {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000514 const ComplexPattern &Pattern;
515public:
Chris Lattner9a515172010-02-25 02:04:40 +0000516 CheckComplexPatMatcher(const ComplexPattern &pattern)
517 : Matcher(CheckComplexPat), Pattern(pattern) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000518
Chris Lattnerdc429fd2010-02-17 00:31:50 +0000519 const ComplexPattern &getPattern() const { return Pattern; }
520
Chris Lattner9a515172010-02-25 02:04:40 +0000521 static inline bool classof(const Matcher *N) {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000522 return N->getKind() == CheckComplexPat;
523 }
524
Chris Lattner136ab782010-02-25 06:49:58 +0000525private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000526 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000527 virtual bool isEqualImpl(const Matcher *M) const {
528 return &cast<CheckComplexPatMatcher>(M)->Pattern == &Pattern;
529 }
530 virtual unsigned getHashImpl() const {
531 return (unsigned)(intptr_t)&Pattern;
532 }
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000533};
534
Chris Lattner9a515172010-02-25 02:04:40 +0000535/// CheckAndImmMatcher - This checks to see if the current node is an 'and'
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000536/// with something equivalent to the specified immediate.
Chris Lattner9a515172010-02-25 02:04:40 +0000537class CheckAndImmMatcher : public Matcher {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000538 int64_t Value;
539public:
Chris Lattner9a515172010-02-25 02:04:40 +0000540 CheckAndImmMatcher(int64_t value)
541 : Matcher(CheckAndImm), Value(value) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000542
543 int64_t getValue() const { return Value; }
544
Chris Lattner9a515172010-02-25 02:04:40 +0000545 static inline bool classof(const Matcher *N) {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000546 return N->getKind() == CheckAndImm;
547 }
548
Chris Lattner136ab782010-02-25 06:49:58 +0000549private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000550 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000551 virtual bool isEqualImpl(const Matcher *M) const {
552 return cast<CheckAndImmMatcher>(M)->Value == Value;
553 }
554 virtual unsigned getHashImpl() const { return Value; }
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000555};
556
Chris Lattner9a515172010-02-25 02:04:40 +0000557/// CheckOrImmMatcher - This checks to see if the current node is an 'and'
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000558/// with something equivalent to the specified immediate.
Chris Lattner9a515172010-02-25 02:04:40 +0000559class CheckOrImmMatcher : public Matcher {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000560 int64_t Value;
561public:
Chris Lattner9a515172010-02-25 02:04:40 +0000562 CheckOrImmMatcher(int64_t value)
563 : Matcher(CheckOrImm), Value(value) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000564
565 int64_t getValue() const { return Value; }
566
Chris Lattner9a515172010-02-25 02:04:40 +0000567 static inline bool classof(const Matcher *N) {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000568 return N->getKind() == CheckOrImm;
569 }
570
Chris Lattner136ab782010-02-25 06:49:58 +0000571private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000572 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000573 virtual bool isEqualImpl(const Matcher *M) const {
574 return cast<CheckOrImmMatcher>(M)->Value == Value;
575 }
576 virtual unsigned getHashImpl() const { return Value; }
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000577};
Chris Lattner9de39482010-02-16 06:10:58 +0000578
Chris Lattner9a515172010-02-25 02:04:40 +0000579/// CheckFoldableChainNodeMatcher - This checks to see if the current node
Chris Lattnerc6dc8f62010-02-16 19:15:55 +0000580/// (which defines a chain operand) is safe to fold into a larger pattern.
Chris Lattner9a515172010-02-25 02:04:40 +0000581class CheckFoldableChainNodeMatcher : public Matcher {
Chris Lattner9de39482010-02-16 06:10:58 +0000582public:
Chris Lattner9a515172010-02-25 02:04:40 +0000583 CheckFoldableChainNodeMatcher()
584 : Matcher(CheckFoldableChainNode) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000585
Chris Lattner9a515172010-02-25 02:04:40 +0000586 static inline bool classof(const Matcher *N) {
Chris Lattnerc6dc8f62010-02-16 19:15:55 +0000587 return N->getKind() == CheckFoldableChainNode;
Chris Lattner9de39482010-02-16 06:10:58 +0000588 }
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000589
Chris Lattner136ab782010-02-25 06:49:58 +0000590private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000591 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000592 virtual bool isEqualImpl(const Matcher *M) const { return true; }
593 virtual unsigned getHashImpl() const { return 0; }
Chris Lattner9de39482010-02-16 06:10:58 +0000594};
595
Chris Lattner9a515172010-02-25 02:04:40 +0000596/// CheckChainCompatibleMatcher - Verify that the current node's chain
Chris Lattner283adcb2010-02-17 06:23:39 +0000597/// operand is 'compatible' with the specified recorded node's.
Chris Lattner9a515172010-02-25 02:04:40 +0000598class CheckChainCompatibleMatcher : public Matcher {
Chris Lattner283adcb2010-02-17 06:23:39 +0000599 unsigned PreviousOp;
600public:
Chris Lattner9a515172010-02-25 02:04:40 +0000601 CheckChainCompatibleMatcher(unsigned previousop)
602 : Matcher(CheckChainCompatible), PreviousOp(previousop) {}
Chris Lattner283adcb2010-02-17 06:23:39 +0000603
604 unsigned getPreviousOp() const { return PreviousOp; }
605
Chris Lattner9a515172010-02-25 02:04:40 +0000606 static inline bool classof(const Matcher *N) {
Chris Lattner283adcb2010-02-17 06:23:39 +0000607 return N->getKind() == CheckChainCompatible;
608 }
609
Chris Lattner136ab782010-02-25 06:49:58 +0000610private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000611 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000612 virtual bool isEqualImpl(const Matcher *M) const {
Chris Lattner50c0b362010-02-26 08:06:02 +0000613 return cast<CheckChainCompatibleMatcher>(M)->PreviousOp == PreviousOp;
Chris Lattner136ab782010-02-25 06:49:58 +0000614 }
615 virtual unsigned getHashImpl() const { return PreviousOp; }
Chris Lattner283adcb2010-02-17 06:23:39 +0000616};
617
Chris Lattner9a515172010-02-25 02:04:40 +0000618/// EmitIntegerMatcher - This creates a new TargetConstant.
619class EmitIntegerMatcher : public Matcher {
Chris Lattnerddfb5222010-02-18 22:03:03 +0000620 int64_t Val;
621 MVT::SimpleValueType VT;
622public:
Chris Lattner9a515172010-02-25 02:04:40 +0000623 EmitIntegerMatcher(int64_t val, MVT::SimpleValueType vt)
624 : Matcher(EmitInteger), Val(val), VT(vt) {}
Chris Lattner283adcb2010-02-17 06:23:39 +0000625
Chris Lattner7043e0a2010-02-19 07:49:56 +0000626 int64_t getValue() const { return Val; }
Chris Lattnerddfb5222010-02-18 22:03:03 +0000627 MVT::SimpleValueType getVT() const { return VT; }
628
Chris Lattner9a515172010-02-25 02:04:40 +0000629 static inline bool classof(const Matcher *N) {
Chris Lattnerddfb5222010-02-18 22:03:03 +0000630 return N->getKind() == EmitInteger;
631 }
632
Chris Lattner136ab782010-02-25 06:49:58 +0000633private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000634 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000635 virtual bool isEqualImpl(const Matcher *M) const {
636 return cast<EmitIntegerMatcher>(M)->Val == Val &&
637 cast<EmitIntegerMatcher>(M)->VT == VT;
638 }
639 virtual unsigned getHashImpl() const { return (Val << 4) | VT; }
Chris Lattnerddfb5222010-02-18 22:03:03 +0000640};
Chris Lattner3163ca52010-02-21 03:22:59 +0000641
Chris Lattner9a515172010-02-25 02:04:40 +0000642/// EmitStringIntegerMatcher - A target constant whose value is represented
Chris Lattner3163ca52010-02-21 03:22:59 +0000643/// by a string.
Chris Lattner9a515172010-02-25 02:04:40 +0000644class EmitStringIntegerMatcher : public Matcher {
Chris Lattner3163ca52010-02-21 03:22:59 +0000645 std::string Val;
646 MVT::SimpleValueType VT;
647public:
Chris Lattner9a515172010-02-25 02:04:40 +0000648 EmitStringIntegerMatcher(const std::string &val, MVT::SimpleValueType vt)
649 : Matcher(EmitStringInteger), Val(val), VT(vt) {}
Chris Lattner3163ca52010-02-21 03:22:59 +0000650
651 const std::string &getValue() const { return Val; }
652 MVT::SimpleValueType getVT() const { return VT; }
653
Chris Lattner9a515172010-02-25 02:04:40 +0000654 static inline bool classof(const Matcher *N) {
Chris Lattner3163ca52010-02-21 03:22:59 +0000655 return N->getKind() == EmitStringInteger;
656 }
657
Chris Lattner136ab782010-02-25 06:49:58 +0000658private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000659 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000660 virtual bool isEqualImpl(const Matcher *M) const {
661 return cast<EmitStringIntegerMatcher>(M)->Val == Val &&
662 cast<EmitStringIntegerMatcher>(M)->VT == VT;
663 }
664 virtual unsigned getHashImpl() const;
Chris Lattner3163ca52010-02-21 03:22:59 +0000665};
Chris Lattnerddfb5222010-02-18 22:03:03 +0000666
Chris Lattner9a515172010-02-25 02:04:40 +0000667/// EmitRegisterMatcher - This creates a new TargetConstant.
668class EmitRegisterMatcher : public Matcher {
Chris Lattnerddfb5222010-02-18 22:03:03 +0000669 /// Reg - The def for the register that we're emitting. If this is null, then
670 /// this is a reference to zero_reg.
671 Record *Reg;
672 MVT::SimpleValueType VT;
673public:
Chris Lattner9a515172010-02-25 02:04:40 +0000674 EmitRegisterMatcher(Record *reg, MVT::SimpleValueType vt)
675 : Matcher(EmitRegister), Reg(reg), VT(vt) {}
Chris Lattnerddfb5222010-02-18 22:03:03 +0000676
677 Record *getReg() const { return Reg; }
678 MVT::SimpleValueType getVT() const { return VT; }
679
Chris Lattner9a515172010-02-25 02:04:40 +0000680 static inline bool classof(const Matcher *N) {
Chris Lattnerddfb5222010-02-18 22:03:03 +0000681 return N->getKind() == EmitRegister;
682 }
683
Chris Lattner136ab782010-02-25 06:49:58 +0000684private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000685 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000686 virtual bool isEqualImpl(const Matcher *M) const {
687 return cast<EmitRegisterMatcher>(M)->Reg == Reg &&
688 cast<EmitRegisterMatcher>(M)->VT == VT;
689 }
690 virtual unsigned getHashImpl() const {
691 return ((unsigned)(intptr_t)Reg) << 4 | VT;
692 }
Chris Lattnerddfb5222010-02-18 22:03:03 +0000693};
Chris Lattner3163ca52010-02-21 03:22:59 +0000694
Chris Lattner9a515172010-02-25 02:04:40 +0000695/// EmitConvertToTargetMatcher - Emit an operation that reads a specified
Chris Lattner3163ca52010-02-21 03:22:59 +0000696/// recorded node and converts it from being a ISD::Constant to
697/// ISD::TargetConstant, likewise for ConstantFP.
Chris Lattner9a515172010-02-25 02:04:40 +0000698class EmitConvertToTargetMatcher : public Matcher {
Chris Lattner3163ca52010-02-21 03:22:59 +0000699 unsigned Slot;
700public:
Chris Lattner9a515172010-02-25 02:04:40 +0000701 EmitConvertToTargetMatcher(unsigned slot)
702 : Matcher(EmitConvertToTarget), Slot(slot) {}
Chris Lattner3163ca52010-02-21 03:22:59 +0000703
704 unsigned getSlot() const { return Slot; }
705
Chris Lattner9a515172010-02-25 02:04:40 +0000706 static inline bool classof(const Matcher *N) {
Chris Lattner3163ca52010-02-21 03:22:59 +0000707 return N->getKind() == EmitConvertToTarget;
708 }
709
Chris Lattner136ab782010-02-25 06:49:58 +0000710private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000711 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000712 virtual bool isEqualImpl(const Matcher *M) const {
713 return cast<EmitConvertToTargetMatcher>(M)->Slot == Slot;
714 }
715 virtual unsigned getHashImpl() const { return Slot; }
Chris Lattner3163ca52010-02-21 03:22:59 +0000716};
717
Chris Lattner9a515172010-02-25 02:04:40 +0000718/// EmitMergeInputChainsMatcher - Emit a node that merges a list of input
Chris Lattner3163ca52010-02-21 03:22:59 +0000719/// chains together with a token factor. The list of nodes are the nodes in the
720/// matched pattern that have chain input/outputs. This node adds all input
721/// chains of these nodes if they are not themselves a node in the pattern.
Chris Lattner9a515172010-02-25 02:04:40 +0000722class EmitMergeInputChainsMatcher : public Matcher {
Chris Lattner3163ca52010-02-21 03:22:59 +0000723 SmallVector<unsigned, 3> ChainNodes;
724public:
Chris Lattner9a515172010-02-25 02:04:40 +0000725 EmitMergeInputChainsMatcher(const unsigned *nodes, unsigned NumNodes)
726 : Matcher(EmitMergeInputChains), ChainNodes(nodes, nodes+NumNodes) {}
Chris Lattner3163ca52010-02-21 03:22:59 +0000727
728 unsigned getNumNodes() const { return ChainNodes.size(); }
729
730 unsigned getNode(unsigned i) const {
731 assert(i < ChainNodes.size());
732 return ChainNodes[i];
733 }
734
Chris Lattner9a515172010-02-25 02:04:40 +0000735 static inline bool classof(const Matcher *N) {
Chris Lattner3163ca52010-02-21 03:22:59 +0000736 return N->getKind() == EmitMergeInputChains;
737 }
738
Chris Lattner136ab782010-02-25 06:49:58 +0000739private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000740 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000741 virtual bool isEqualImpl(const Matcher *M) const {
742 return cast<EmitMergeInputChainsMatcher>(M)->ChainNodes == ChainNodes;
743 }
744 virtual unsigned getHashImpl() const;
Chris Lattner3163ca52010-02-21 03:22:59 +0000745};
746
Chris Lattner9a515172010-02-25 02:04:40 +0000747/// EmitCopyToRegMatcher - Emit a CopyToReg node from a value to a physreg,
Chris Lattner3163ca52010-02-21 03:22:59 +0000748/// pushing the chain and flag results.
749///
Chris Lattner9a515172010-02-25 02:04:40 +0000750class EmitCopyToRegMatcher : public Matcher {
Chris Lattner3163ca52010-02-21 03:22:59 +0000751 unsigned SrcSlot; // Value to copy into the physreg.
752 Record *DestPhysReg;
753public:
Chris Lattner9a515172010-02-25 02:04:40 +0000754 EmitCopyToRegMatcher(unsigned srcSlot, Record *destPhysReg)
755 : Matcher(EmitCopyToReg), SrcSlot(srcSlot), DestPhysReg(destPhysReg) {}
Chris Lattner3163ca52010-02-21 03:22:59 +0000756
757 unsigned getSrcSlot() const { return SrcSlot; }
758 Record *getDestPhysReg() const { return DestPhysReg; }
759
Chris Lattner9a515172010-02-25 02:04:40 +0000760 static inline bool classof(const Matcher *N) {
Chris Lattner3163ca52010-02-21 03:22:59 +0000761 return N->getKind() == EmitCopyToReg;
762 }
763
Chris Lattner136ab782010-02-25 06:49:58 +0000764private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000765 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000766 virtual bool isEqualImpl(const Matcher *M) const {
767 return cast<EmitCopyToRegMatcher>(M)->SrcSlot == SrcSlot &&
768 cast<EmitCopyToRegMatcher>(M)->DestPhysReg == DestPhysReg;
769 }
770 virtual unsigned getHashImpl() const {
771 return SrcSlot ^ ((unsigned)(intptr_t)DestPhysReg << 4);
772 }
Chris Lattner3163ca52010-02-21 03:22:59 +0000773};
774
775
776
Chris Lattner9a515172010-02-25 02:04:40 +0000777/// EmitNodeXFormMatcher - Emit an operation that runs an SDNodeXForm on a
Chris Lattner3163ca52010-02-21 03:22:59 +0000778/// recorded node and records the result.
Chris Lattner9a515172010-02-25 02:04:40 +0000779class EmitNodeXFormMatcher : public Matcher {
Chris Lattner3163ca52010-02-21 03:22:59 +0000780 unsigned Slot;
781 Record *NodeXForm;
782public:
Chris Lattner9a515172010-02-25 02:04:40 +0000783 EmitNodeXFormMatcher(unsigned slot, Record *nodeXForm)
784 : Matcher(EmitNodeXForm), Slot(slot), NodeXForm(nodeXForm) {}
Chris Lattner3163ca52010-02-21 03:22:59 +0000785
786 unsigned getSlot() const { return Slot; }
787 Record *getNodeXForm() const { return NodeXForm; }
788
Chris Lattner9a515172010-02-25 02:04:40 +0000789 static inline bool classof(const Matcher *N) {
Chris Lattner3163ca52010-02-21 03:22:59 +0000790 return N->getKind() == EmitNodeXForm;
791 }
792
Chris Lattner136ab782010-02-25 06:49:58 +0000793private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000794 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000795 virtual bool isEqualImpl(const Matcher *M) const {
796 return cast<EmitNodeXFormMatcher>(M)->Slot == Slot &&
797 cast<EmitNodeXFormMatcher>(M)->NodeXForm == NodeXForm;
798 }
799 virtual unsigned getHashImpl() const {
800 return Slot ^ ((unsigned)(intptr_t)NodeXForm << 4);
801 }
Chris Lattner3163ca52010-02-21 03:22:59 +0000802};
Chris Lattnerddfb5222010-02-18 22:03:03 +0000803
Chris Lattner9a515172010-02-25 02:04:40 +0000804/// EmitNodeMatcher - This signals a successful match and generates a node.
805class EmitNodeMatcher : public Matcher {
Chris Lattner3163ca52010-02-21 03:22:59 +0000806 std::string OpcodeName;
807 const SmallVector<MVT::SimpleValueType, 3> VTs;
808 const SmallVector<unsigned, 6> Operands;
809 bool HasChain, HasFlag, HasMemRefs;
Chris Lattnerddfb5222010-02-18 22:03:03 +0000810
Chris Lattner3163ca52010-02-21 03:22:59 +0000811 /// NumFixedArityOperands - If this is a fixed arity node, this is set to -1.
812 /// If this is a varidic node, this is set to the number of fixed arity
813 /// operands in the root of the pattern. The rest are appended to this node.
814 int NumFixedArityOperands;
815public:
Chris Lattner9a515172010-02-25 02:04:40 +0000816 EmitNodeMatcher(const std::string &opcodeName,
Chris Lattner136ab782010-02-25 06:49:58 +0000817 const MVT::SimpleValueType *vts, unsigned numvts,
818 const unsigned *operands, unsigned numops,
819 bool hasChain, bool hasFlag, bool hasmemrefs,
820 int numfixedarityoperands)
Chris Lattner9a515172010-02-25 02:04:40 +0000821 : Matcher(EmitNode), OpcodeName(opcodeName),
Chris Lattner3163ca52010-02-21 03:22:59 +0000822 VTs(vts, vts+numvts), Operands(operands, operands+numops),
823 HasChain(hasChain), HasFlag(hasFlag), HasMemRefs(hasmemrefs),
824 NumFixedArityOperands(numfixedarityoperands) {}
825
826 const std::string &getOpcodeName() const { return OpcodeName; }
827
828 unsigned getNumVTs() const { return VTs.size(); }
829 MVT::SimpleValueType getVT(unsigned i) const {
830 assert(i < VTs.size());
831 return VTs[i];
832 }
833
834 unsigned getNumOperands() const { return Operands.size(); }
835 unsigned getOperand(unsigned i) const {
836 assert(i < Operands.size());
837 return Operands[i];
838 }
839
840 bool hasChain() const { return HasChain; }
841 bool hasFlag() const { return HasFlag; }
842 bool hasMemRefs() const { return HasMemRefs; }
843 int getNumFixedArityOperands() const { return NumFixedArityOperands; }
Chris Lattnerddfb5222010-02-18 22:03:03 +0000844
Chris Lattner9a515172010-02-25 02:04:40 +0000845 static inline bool classof(const Matcher *N) {
Chris Lattnerddfb5222010-02-18 22:03:03 +0000846 return N->getKind() == EmitNode;
847 }
848
Chris Lattner136ab782010-02-25 06:49:58 +0000849private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000850 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000851 virtual bool isEqualImpl(const Matcher *M) const;
852 virtual unsigned getHashImpl() const;
Chris Lattnerddfb5222010-02-18 22:03:03 +0000853};
Chris Lattnerb085ea12010-02-21 06:03:07 +0000854
Chris Lattner9a515172010-02-25 02:04:40 +0000855/// MarkFlagResultsMatcher - This node indicates which non-root nodes in the
856/// pattern produce flags. This allows CompleteMatchMatcher to update them
Chris Lattner69f60c82010-02-24 05:33:42 +0000857/// with the output flag of the resultant code.
Chris Lattner9a515172010-02-25 02:04:40 +0000858class MarkFlagResultsMatcher : public Matcher {
Chris Lattner69f60c82010-02-24 05:33:42 +0000859 SmallVector<unsigned, 3> FlagResultNodes;
860public:
Chris Lattner9a515172010-02-25 02:04:40 +0000861 MarkFlagResultsMatcher(const unsigned *nodes, unsigned NumNodes)
862 : Matcher(MarkFlagResults), FlagResultNodes(nodes, nodes+NumNodes) {}
Chris Lattner69f60c82010-02-24 05:33:42 +0000863
864 unsigned getNumNodes() const { return FlagResultNodes.size(); }
865
866 unsigned getNode(unsigned i) const {
867 assert(i < FlagResultNodes.size());
868 return FlagResultNodes[i];
869 }
870
Chris Lattner9a515172010-02-25 02:04:40 +0000871 static inline bool classof(const Matcher *N) {
Chris Lattner69f60c82010-02-24 05:33:42 +0000872 return N->getKind() == MarkFlagResults;
873 }
874
Chris Lattner136ab782010-02-25 06:49:58 +0000875private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000876 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000877 virtual bool isEqualImpl(const Matcher *M) const {
878 return cast<MarkFlagResultsMatcher>(M)->FlagResultNodes == FlagResultNodes;
879 }
880 virtual unsigned getHashImpl() const;
Chris Lattner69f60c82010-02-24 05:33:42 +0000881};
882
Chris Lattner9a515172010-02-25 02:04:40 +0000883/// CompleteMatchMatcher - Complete a match by replacing the results of the
Chris Lattnerb085ea12010-02-21 06:03:07 +0000884/// pattern with the newly generated nodes. This also prints a comment
885/// indicating the source and dest patterns.
Chris Lattner9a515172010-02-25 02:04:40 +0000886class CompleteMatchMatcher : public Matcher {
Chris Lattnerb085ea12010-02-21 06:03:07 +0000887 SmallVector<unsigned, 2> Results;
Chris Lattner3163ca52010-02-21 03:22:59 +0000888 const PatternToMatch &Pattern;
889public:
Chris Lattner9a515172010-02-25 02:04:40 +0000890 CompleteMatchMatcher(const unsigned *results, unsigned numresults,
Chris Lattnerb085ea12010-02-21 06:03:07 +0000891 const PatternToMatch &pattern)
Chris Lattner9a515172010-02-25 02:04:40 +0000892 : Matcher(CompleteMatch), Results(results, results+numresults),
Chris Lattnerb085ea12010-02-21 06:03:07 +0000893 Pattern(pattern) {}
894
895 unsigned getNumResults() const { return Results.size(); }
896 unsigned getResult(unsigned R) const { return Results[R]; }
Chris Lattner3163ca52010-02-21 03:22:59 +0000897 const PatternToMatch &getPattern() const { return Pattern; }
898
Chris Lattner9a515172010-02-25 02:04:40 +0000899 static inline bool classof(const Matcher *N) {
Chris Lattnerb085ea12010-02-21 06:03:07 +0000900 return N->getKind() == CompleteMatch;
Chris Lattner3163ca52010-02-21 03:22:59 +0000901 }
902
Chris Lattner136ab782010-02-25 06:49:58 +0000903private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000904 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000905 virtual bool isEqualImpl(const Matcher *M) const {
906 return cast<CompleteMatchMatcher>(M)->Results == Results &&
907 &cast<CompleteMatchMatcher>(M)->Pattern == &Pattern;
908 }
909 virtual unsigned getHashImpl() const;
Chris Lattner3163ca52010-02-21 03:22:59 +0000910};
Chris Lattnerddfb5222010-02-18 22:03:03 +0000911
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000912} // end namespace llvm
913
914#endif