blob: 9af98f77f3576a5f2c76418e000b51c88c8e29da [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 Lattnerf4950d02010-02-27 06:22:57 +0000107 /// isSafeToReorderWithPatternPredicate - Return true if it is safe to sink a
108 /// PatternPredicate node past this one.
109 virtual bool isSafeToReorderWithPatternPredicate() const {
110 return false;
111 }
112
Chris Lattner392b1bf2010-02-25 06:53:39 +0000113 void print(raw_ostream &OS, unsigned indent = 0) const;
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000114 void dump() const;
Chris Lattner3b31c982010-02-18 02:49:24 +0000115protected:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000116 virtual void printImpl(raw_ostream &OS, unsigned indent) const = 0;
Chris Lattner136ab782010-02-25 06:49:58 +0000117 virtual bool isEqualImpl(const Matcher *M) const = 0;
118 virtual unsigned getHashImpl() const = 0;
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000119};
120
Chris Lattner42363662010-02-25 19:00:39 +0000121/// ScopeMatcher - This attempts to match each of its children to find the first
122/// one that successfully matches. If one child fails, it tries the next child.
123/// If none of the children match then this check fails. It never has a 'next'.
Chris Lattner9a515172010-02-25 02:04:40 +0000124class ScopeMatcher : public Matcher {
Chris Lattner42363662010-02-25 19:00:39 +0000125 SmallVector<Matcher*, 4> Children;
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000126public:
Chris Lattner42363662010-02-25 19:00:39 +0000127 ScopeMatcher(Matcher *const *children, unsigned numchildren)
128 : Matcher(Scope), Children(children, children+numchildren) {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000129 }
Chris Lattner42363662010-02-25 19:00:39 +0000130 virtual ~ScopeMatcher();
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000131
Chris Lattner42363662010-02-25 19:00:39 +0000132 unsigned getNumChildren() const { return Children.size(); }
133
134 Matcher *getChild(unsigned i) { return Children[i]; }
135 const Matcher *getChild(unsigned i) const { return Children[i]; }
136
137 void resetChild(unsigned i, Matcher *N) {
138 delete Children[i];
139 Children[i] = N;
140 }
141
142 Matcher *takeChild(unsigned i) {
143 Matcher *Res = Children[i];
144 Children[i] = 0;
145 return Res;
146 }
Chris Lattner54ee7362010-02-26 07:35:27 +0000147
148 void setNumChildren(unsigned NC) {
149 if (NC < Children.size()) {
150 // delete any children we're about to lose pointers to.
151 for (unsigned i = NC, e = Children.size(); i != e; ++i)
152 delete Children[i];
153 }
154 Children.resize(NC);
155 }
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000156
Chris Lattner9a515172010-02-25 02:04:40 +0000157 static inline bool classof(const Matcher *N) {
Chris Lattnerac10e4f2010-02-25 01:56:48 +0000158 return N->getKind() == Scope;
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000159 }
160
Chris Lattner136ab782010-02-25 06:49:58 +0000161private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000162 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000163 virtual bool isEqualImpl(const Matcher *M) const { return false; }
Chris Lattner42363662010-02-25 19:00:39 +0000164 virtual unsigned getHashImpl() const { return 12312; }
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000165};
166
Chris Lattner9a515172010-02-25 02:04:40 +0000167/// RecordMatcher - Save the current node in the operand list.
168class RecordMatcher : public Matcher {
Chris Lattner56cf88d2010-02-17 01:03:09 +0000169 /// WhatFor - This is a string indicating why we're recording this. This
170 /// should only be used for comment generation not anything semantic.
171 std::string WhatFor;
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000172public:
Chris Lattner9a515172010-02-25 02:04:40 +0000173 RecordMatcher(const std::string &whatfor)
174 : Matcher(RecordNode), WhatFor(whatfor) {}
Chris Lattner56cf88d2010-02-17 01:03:09 +0000175
Chris Lattner086ca522010-02-17 01:27:29 +0000176 const std::string &getWhatFor() const { return WhatFor; }
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000177
Chris Lattner9a515172010-02-25 02:04:40 +0000178 static inline bool classof(const Matcher *N) {
Chris Lattnerddfb5222010-02-18 22:03:03 +0000179 return N->getKind() == RecordNode;
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000180 }
181
Chris Lattnerf4950d02010-02-27 06:22:57 +0000182 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
Chris Lattner136ab782010-02-25 06:49:58 +0000183private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000184 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000185 virtual bool isEqualImpl(const Matcher *M) const { return true; }
186 virtual unsigned getHashImpl() const { return 0; }
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000187};
188
Chris Lattner9a515172010-02-25 02:04:40 +0000189/// RecordChildMatcher - Save a numbered child of the current node, or fail
Chris Lattner3ee1bc42010-02-24 07:31:45 +0000190/// the match if it doesn't exist. This is logically equivalent to:
191/// MoveChild N + RecordNode + MoveParent.
Chris Lattner9a515172010-02-25 02:04:40 +0000192class RecordChildMatcher : public Matcher {
Chris Lattner3ee1bc42010-02-24 07:31:45 +0000193 unsigned ChildNo;
194
195 /// WhatFor - This is a string indicating why we're recording this. This
196 /// should only be used for comment generation not anything semantic.
197 std::string WhatFor;
198public:
Chris Lattner9a515172010-02-25 02:04:40 +0000199 RecordChildMatcher(unsigned childno, const std::string &whatfor)
200 : Matcher(RecordChild), ChildNo(childno), WhatFor(whatfor) {}
Chris Lattner3ee1bc42010-02-24 07:31:45 +0000201
202 unsigned getChildNo() const { return ChildNo; }
203 const std::string &getWhatFor() const { return WhatFor; }
204
Chris Lattner9a515172010-02-25 02:04:40 +0000205 static inline bool classof(const Matcher *N) {
Chris Lattner3ee1bc42010-02-24 07:31:45 +0000206 return N->getKind() == RecordChild;
207 }
208
Chris Lattnerf4950d02010-02-27 06:22:57 +0000209 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
210
Chris Lattner136ab782010-02-25 06:49:58 +0000211private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000212 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000213 virtual bool isEqualImpl(const Matcher *M) const {
214 return cast<RecordChildMatcher>(M)->getChildNo() == getChildNo();
215 }
216 virtual unsigned getHashImpl() const { return getChildNo(); }
Chris Lattner3ee1bc42010-02-24 07:31:45 +0000217};
218
Chris Lattner9a515172010-02-25 02:04:40 +0000219/// RecordMemRefMatcher - Save the current node's memref.
220class RecordMemRefMatcher : public Matcher {
Chris Lattner3163ca52010-02-21 03:22:59 +0000221public:
Chris Lattner9a515172010-02-25 02:04:40 +0000222 RecordMemRefMatcher() : Matcher(RecordMemRef) {}
Chris Lattner3163ca52010-02-21 03:22:59 +0000223
Chris Lattner9a515172010-02-25 02:04:40 +0000224 static inline bool classof(const Matcher *N) {
Chris Lattner3163ca52010-02-21 03:22:59 +0000225 return N->getKind() == RecordMemRef;
226 }
227
Chris Lattnerf4950d02010-02-27 06:22:57 +0000228 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
229
Chris Lattner136ab782010-02-25 06:49:58 +0000230private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000231 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000232 virtual bool isEqualImpl(const Matcher *M) const { return true; }
233 virtual unsigned getHashImpl() const { return 0; }
Chris Lattner3163ca52010-02-21 03:22:59 +0000234};
235
236
Chris Lattner9a515172010-02-25 02:04:40 +0000237/// CaptureFlagInputMatcher - If the current record has a flag input, record
Chris Lattner3163ca52010-02-21 03:22:59 +0000238/// it so that it is used as an input to the generated code.
Chris Lattner9a515172010-02-25 02:04:40 +0000239class CaptureFlagInputMatcher : public Matcher {
Chris Lattner3163ca52010-02-21 03:22:59 +0000240public:
Chris Lattner9a515172010-02-25 02:04:40 +0000241 CaptureFlagInputMatcher() : Matcher(CaptureFlagInput) {}
Chris Lattner3163ca52010-02-21 03:22:59 +0000242
Chris Lattner9a515172010-02-25 02:04:40 +0000243 static inline bool classof(const Matcher *N) {
Chris Lattner3163ca52010-02-21 03:22:59 +0000244 return N->getKind() == CaptureFlagInput;
245 }
246
Chris Lattnerf4950d02010-02-27 06:22:57 +0000247 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
248
Chris Lattner136ab782010-02-25 06:49:58 +0000249private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000250 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000251 virtual bool isEqualImpl(const Matcher *M) const { return true; }
252 virtual unsigned getHashImpl() const { return 0; }
Chris Lattner3163ca52010-02-21 03:22:59 +0000253};
254
Chris Lattner9a515172010-02-25 02:04:40 +0000255/// MoveChildMatcher - This tells the interpreter to move into the
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000256/// specified child node.
Chris Lattner9a515172010-02-25 02:04:40 +0000257class MoveChildMatcher : public Matcher {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000258 unsigned ChildNo;
259public:
Chris Lattner9a515172010-02-25 02:04:40 +0000260 MoveChildMatcher(unsigned childNo) : Matcher(MoveChild), ChildNo(childNo) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000261
262 unsigned getChildNo() const { return ChildNo; }
263
Chris Lattner9a515172010-02-25 02:04:40 +0000264 static inline bool classof(const Matcher *N) {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000265 return N->getKind() == MoveChild;
266 }
267
Chris Lattnerf4950d02010-02-27 06:22:57 +0000268 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
269
Chris Lattner136ab782010-02-25 06:49:58 +0000270private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000271 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000272 virtual bool isEqualImpl(const Matcher *M) const {
273 return cast<MoveChildMatcher>(M)->getChildNo() == getChildNo();
274 }
275 virtual unsigned getHashImpl() const { return getChildNo(); }
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000276};
277
Chris Lattner9a515172010-02-25 02:04:40 +0000278/// MoveParentMatcher - This tells the interpreter to move to the parent
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000279/// of the current node.
Chris Lattner9a515172010-02-25 02:04:40 +0000280class MoveParentMatcher : public Matcher {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000281public:
Chris Lattner9a515172010-02-25 02:04:40 +0000282 MoveParentMatcher() : Matcher(MoveParent) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000283
Chris Lattner9a515172010-02-25 02:04:40 +0000284 static inline bool classof(const Matcher *N) {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000285 return N->getKind() == MoveParent;
286 }
287
Chris Lattnerf4950d02010-02-27 06:22:57 +0000288 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
289
Chris Lattner136ab782010-02-25 06:49:58 +0000290private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000291 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000292 virtual bool isEqualImpl(const Matcher *M) const { return true; }
293 virtual unsigned getHashImpl() const { return 0; }
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000294};
295
Chris Lattner9a515172010-02-25 02:04:40 +0000296/// CheckSameMatcher - This checks to see if this node is exactly the same
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000297/// node as the specified match that was recorded with 'Record'. This is used
298/// when patterns have the same name in them, like '(mul GPR:$in, GPR:$in)'.
Chris Lattner9a515172010-02-25 02:04:40 +0000299class CheckSameMatcher : public Matcher {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000300 unsigned MatchNumber;
301public:
Chris Lattner9a515172010-02-25 02:04:40 +0000302 CheckSameMatcher(unsigned matchnumber)
Chris Lattner136ab782010-02-25 06:49:58 +0000303 : Matcher(CheckSame), MatchNumber(matchnumber) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000304
305 unsigned getMatchNumber() const { return MatchNumber; }
306
Chris Lattner9a515172010-02-25 02:04:40 +0000307 static inline bool classof(const Matcher *N) {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000308 return N->getKind() == CheckSame;
309 }
310
Chris Lattnerf4950d02010-02-27 06:22:57 +0000311 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
312
Chris Lattner136ab782010-02-25 06:49:58 +0000313private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000314 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000315 virtual bool isEqualImpl(const Matcher *M) const {
316 return cast<CheckSameMatcher>(M)->getMatchNumber() == getMatchNumber();
317 }
318 virtual unsigned getHashImpl() const { return getMatchNumber(); }
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000319};
320
Chris Lattner9a515172010-02-25 02:04:40 +0000321/// CheckPatternPredicateMatcher - This checks the target-specific predicate
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000322/// to see if the entire pattern is capable of matching. This predicate does
323/// not take a node as input. This is used for subtarget feature checks etc.
Chris Lattner9a515172010-02-25 02:04:40 +0000324class CheckPatternPredicateMatcher : public Matcher {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000325 std::string Predicate;
326public:
Chris Lattner9a515172010-02-25 02:04:40 +0000327 CheckPatternPredicateMatcher(StringRef predicate)
Chris Lattner136ab782010-02-25 06:49:58 +0000328 : Matcher(CheckPatternPredicate), Predicate(predicate) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000329
330 StringRef getPredicate() const { return Predicate; }
331
Chris Lattner9a515172010-02-25 02:04:40 +0000332 static inline bool classof(const Matcher *N) {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000333 return N->getKind() == CheckPatternPredicate;
334 }
335
Chris Lattnerf4950d02010-02-27 06:22:57 +0000336 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
337
Chris Lattner136ab782010-02-25 06:49:58 +0000338private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000339 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000340 virtual bool isEqualImpl(const Matcher *M) const {
341 return cast<CheckPatternPredicateMatcher>(M)->getPredicate() == Predicate;
342 }
343 virtual unsigned getHashImpl() const;
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000344};
345
Chris Lattner9a515172010-02-25 02:04:40 +0000346/// CheckPredicateMatcher - This checks the target-specific predicate to
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000347/// see if the node is acceptable.
Chris Lattner9a515172010-02-25 02:04:40 +0000348class CheckPredicateMatcher : public Matcher {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000349 StringRef PredName;
350public:
Chris Lattner9a515172010-02-25 02:04:40 +0000351 CheckPredicateMatcher(StringRef predname)
352 : Matcher(CheckPredicate), PredName(predname) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000353
354 StringRef getPredicateName() const { return PredName; }
355
Chris Lattner9a515172010-02-25 02:04:40 +0000356 static inline bool classof(const Matcher *N) {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000357 return N->getKind() == CheckPredicate;
358 }
359
Chris Lattnerf4950d02010-02-27 06:22:57 +0000360 // TODO: Ok?
361 //virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
362
Chris Lattner136ab782010-02-25 06:49:58 +0000363private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000364 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000365 virtual bool isEqualImpl(const Matcher *M) const {
366 return cast<CheckPredicateMatcher>(M)->PredName == PredName;
367 }
368 virtual unsigned getHashImpl() const;
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000369};
370
371
Chris Lattner9a515172010-02-25 02:04:40 +0000372/// CheckOpcodeMatcher - This checks to see if the current node has the
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000373/// specified opcode, if not it fails to match.
Chris Lattner9a515172010-02-25 02:04:40 +0000374class CheckOpcodeMatcher : public Matcher {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000375 StringRef OpcodeName;
376public:
Chris Lattner9a515172010-02-25 02:04:40 +0000377 CheckOpcodeMatcher(StringRef opcodename)
378 : Matcher(CheckOpcode), OpcodeName(opcodename) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000379
380 StringRef getOpcodeName() const { return OpcodeName; }
381
Chris Lattner9a515172010-02-25 02:04:40 +0000382 static inline bool classof(const Matcher *N) {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000383 return N->getKind() == CheckOpcode;
384 }
385
Chris Lattnerf4950d02010-02-27 06:22:57 +0000386 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
387
Chris Lattner136ab782010-02-25 06:49:58 +0000388private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000389 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000390 virtual bool isEqualImpl(const Matcher *M) const {
391 return cast<CheckOpcodeMatcher>(M)->OpcodeName == OpcodeName;
392 }
393 virtual unsigned getHashImpl() const;
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000394};
395
Chris Lattner9a515172010-02-25 02:04:40 +0000396/// CheckMultiOpcodeMatcher - This checks to see if the current node has one
Chris Lattnerf58c08e2010-02-22 22:30:37 +0000397/// of the specified opcode, if not it fails to match.
Chris Lattner9a515172010-02-25 02:04:40 +0000398class CheckMultiOpcodeMatcher : public Matcher {
Chris Lattnerf58c08e2010-02-22 22:30:37 +0000399 SmallVector<StringRef, 4> OpcodeNames;
400public:
Chris Lattner9a515172010-02-25 02:04:40 +0000401 CheckMultiOpcodeMatcher(const StringRef *opcodes, unsigned numops)
402 : Matcher(CheckMultiOpcode), OpcodeNames(opcodes, opcodes+numops) {}
Chris Lattnerf58c08e2010-02-22 22:30:37 +0000403
404 unsigned getNumOpcodeNames() const { return OpcodeNames.size(); }
405 StringRef getOpcodeName(unsigned i) const { return OpcodeNames[i]; }
406
Chris Lattner9a515172010-02-25 02:04:40 +0000407 static inline bool classof(const Matcher *N) {
Chris Lattnerf58c08e2010-02-22 22:30:37 +0000408 return N->getKind() == CheckMultiOpcode;
409 }
410
Chris Lattnerf4950d02010-02-27 06:22:57 +0000411 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
412
Chris Lattner136ab782010-02-25 06:49:58 +0000413private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000414 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000415 virtual bool isEqualImpl(const Matcher *M) const {
416 return cast<CheckMultiOpcodeMatcher>(M)->OpcodeNames == OpcodeNames;
417 }
418 virtual unsigned getHashImpl() const;
Chris Lattnerf58c08e2010-02-22 22:30:37 +0000419};
420
421
422
Chris Lattner9a515172010-02-25 02:04:40 +0000423/// CheckTypeMatcher - This checks to see if the current node has the
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000424/// specified type, if not it fails to match.
Chris Lattner9a515172010-02-25 02:04:40 +0000425class CheckTypeMatcher : public Matcher {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000426 MVT::SimpleValueType Type;
427public:
Chris Lattner9a515172010-02-25 02:04:40 +0000428 CheckTypeMatcher(MVT::SimpleValueType type)
429 : Matcher(CheckType), Type(type) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000430
431 MVT::SimpleValueType getType() const { return Type; }
432
Chris Lattner9a515172010-02-25 02:04:40 +0000433 static inline bool classof(const Matcher *N) {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000434 return N->getKind() == CheckType;
435 }
436
Chris Lattnerf4950d02010-02-27 06:22:57 +0000437 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
438
Chris Lattner136ab782010-02-25 06:49:58 +0000439private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000440 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000441 virtual bool isEqualImpl(const Matcher *M) const {
Chris Lattner2d0e0792010-02-26 08:05:36 +0000442 return cast<CheckTypeMatcher>(M)->Type == Type;
Chris Lattner136ab782010-02-25 06:49:58 +0000443 }
444 virtual unsigned getHashImpl() const { return Type; }
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000445};
Chris Lattner3c664b32010-02-24 20:15:25 +0000446
Chris Lattner9a515172010-02-25 02:04:40 +0000447/// CheckChildTypeMatcher - This checks to see if a child node has the
Chris Lattner3c664b32010-02-24 20:15:25 +0000448/// specified type, if not it fails to match.
Chris Lattner9a515172010-02-25 02:04:40 +0000449class CheckChildTypeMatcher : public Matcher {
Chris Lattner3c664b32010-02-24 20:15:25 +0000450 unsigned ChildNo;
451 MVT::SimpleValueType Type;
452public:
Chris Lattner9a515172010-02-25 02:04:40 +0000453 CheckChildTypeMatcher(unsigned childno, MVT::SimpleValueType type)
454 : Matcher(CheckChildType), ChildNo(childno), Type(type) {}
Chris Lattner3c664b32010-02-24 20:15:25 +0000455
456 unsigned getChildNo() const { return ChildNo; }
457 MVT::SimpleValueType getType() const { return Type; }
458
Chris Lattner9a515172010-02-25 02:04:40 +0000459 static inline bool classof(const Matcher *N) {
Chris Lattner3c664b32010-02-24 20:15:25 +0000460 return N->getKind() == CheckChildType;
461 }
462
Chris Lattnerf4950d02010-02-27 06:22:57 +0000463 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
464
Chris Lattner136ab782010-02-25 06:49:58 +0000465private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000466 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000467 virtual bool isEqualImpl(const Matcher *M) const {
468 return cast<CheckChildTypeMatcher>(M)->ChildNo == ChildNo &&
469 cast<CheckChildTypeMatcher>(M)->Type == Type;
470 }
471 virtual unsigned getHashImpl() const { return (Type << 3) | ChildNo; }
Chris Lattner3c664b32010-02-24 20:15:25 +0000472};
473
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000474
Chris Lattner9a515172010-02-25 02:04:40 +0000475/// CheckIntegerMatcher - This checks to see if the current node is a
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000476/// ConstantSDNode with the specified integer value, if not it fails to match.
Chris Lattner9a515172010-02-25 02:04:40 +0000477class CheckIntegerMatcher : public Matcher {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000478 int64_t Value;
479public:
Chris Lattner9a515172010-02-25 02:04:40 +0000480 CheckIntegerMatcher(int64_t value)
481 : Matcher(CheckInteger), Value(value) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000482
483 int64_t getValue() const { return Value; }
484
Chris Lattner9a515172010-02-25 02:04:40 +0000485 static inline bool classof(const Matcher *N) {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000486 return N->getKind() == CheckInteger;
487 }
488
Chris Lattnerf4950d02010-02-27 06:22:57 +0000489 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
490
Chris Lattner136ab782010-02-25 06:49:58 +0000491private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000492 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000493 virtual bool isEqualImpl(const Matcher *M) const {
494 return cast<CheckIntegerMatcher>(M)->Value == Value;
495 }
496 virtual unsigned getHashImpl() const { return Value; }
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000497};
498
Chris Lattner9a515172010-02-25 02:04:40 +0000499/// CheckCondCodeMatcher - This checks to see if the current node is a
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000500/// CondCodeSDNode with the specified condition, if not it fails to match.
Chris Lattner9a515172010-02-25 02:04:40 +0000501class CheckCondCodeMatcher : public Matcher {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000502 StringRef CondCodeName;
503public:
Chris Lattner9a515172010-02-25 02:04:40 +0000504 CheckCondCodeMatcher(StringRef condcodename)
505 : Matcher(CheckCondCode), CondCodeName(condcodename) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000506
507 StringRef getCondCodeName() const { return CondCodeName; }
508
Chris Lattner9a515172010-02-25 02:04:40 +0000509 static inline bool classof(const Matcher *N) {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000510 return N->getKind() == CheckCondCode;
511 }
512
Chris Lattnerf4950d02010-02-27 06:22:57 +0000513 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
514
Chris Lattner136ab782010-02-25 06:49:58 +0000515private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000516 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000517 virtual bool isEqualImpl(const Matcher *M) const {
518 return cast<CheckCondCodeMatcher>(M)->CondCodeName == CondCodeName;
519 }
520 virtual unsigned getHashImpl() const;
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000521};
522
Chris Lattner9a515172010-02-25 02:04:40 +0000523/// CheckValueTypeMatcher - This checks to see if the current node is a
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000524/// VTSDNode with the specified type, if not it fails to match.
Chris Lattner9a515172010-02-25 02:04:40 +0000525class CheckValueTypeMatcher : public Matcher {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000526 StringRef TypeName;
527public:
Chris Lattner9a515172010-02-25 02:04:40 +0000528 CheckValueTypeMatcher(StringRef type_name)
529 : Matcher(CheckValueType), TypeName(type_name) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000530
531 StringRef getTypeName() const { return TypeName; }
532
Chris Lattner9a515172010-02-25 02:04:40 +0000533 static inline bool classof(const Matcher *N) {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000534 return N->getKind() == CheckValueType;
535 }
536
Chris Lattnerf4950d02010-02-27 06:22:57 +0000537 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
538
Chris Lattner136ab782010-02-25 06:49:58 +0000539private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000540 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000541 virtual bool isEqualImpl(const Matcher *M) const {
542 return cast<CheckValueTypeMatcher>(M)->TypeName == TypeName;
543 }
544 virtual unsigned getHashImpl() const;
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000545};
546
547
548
Chris Lattner9a515172010-02-25 02:04:40 +0000549/// CheckComplexPatMatcher - This node runs the specified ComplexPattern on
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000550/// the current node.
Chris Lattner9a515172010-02-25 02:04:40 +0000551class CheckComplexPatMatcher : public Matcher {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000552 const ComplexPattern &Pattern;
553public:
Chris Lattner9a515172010-02-25 02:04:40 +0000554 CheckComplexPatMatcher(const ComplexPattern &pattern)
555 : Matcher(CheckComplexPat), Pattern(pattern) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000556
Chris Lattnerdc429fd2010-02-17 00:31:50 +0000557 const ComplexPattern &getPattern() const { return Pattern; }
558
Chris Lattner9a515172010-02-25 02:04:40 +0000559 static inline bool classof(const Matcher *N) {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000560 return N->getKind() == CheckComplexPat;
561 }
562
Chris Lattnerf4950d02010-02-27 06:22:57 +0000563 // Not safe to move a pattern predicate past a complex pattern.
564 virtual bool isSafeToReorderWithPatternPredicate() const { return false; }
565
Chris Lattner136ab782010-02-25 06:49:58 +0000566private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000567 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000568 virtual bool isEqualImpl(const Matcher *M) const {
569 return &cast<CheckComplexPatMatcher>(M)->Pattern == &Pattern;
570 }
571 virtual unsigned getHashImpl() const {
572 return (unsigned)(intptr_t)&Pattern;
573 }
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000574};
575
Chris Lattner9a515172010-02-25 02:04:40 +0000576/// CheckAndImmMatcher - This checks to see if the current node is an 'and'
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000577/// with something equivalent to the specified immediate.
Chris Lattner9a515172010-02-25 02:04:40 +0000578class CheckAndImmMatcher : public Matcher {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000579 int64_t Value;
580public:
Chris Lattner9a515172010-02-25 02:04:40 +0000581 CheckAndImmMatcher(int64_t value)
582 : Matcher(CheckAndImm), Value(value) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000583
584 int64_t getValue() const { return Value; }
585
Chris Lattner9a515172010-02-25 02:04:40 +0000586 static inline bool classof(const Matcher *N) {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000587 return N->getKind() == CheckAndImm;
588 }
589
Chris Lattnerf4950d02010-02-27 06:22:57 +0000590 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
591
Chris Lattner136ab782010-02-25 06:49:58 +0000592private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000593 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000594 virtual bool isEqualImpl(const Matcher *M) const {
595 return cast<CheckAndImmMatcher>(M)->Value == Value;
596 }
597 virtual unsigned getHashImpl() const { return Value; }
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000598};
599
Chris Lattner9a515172010-02-25 02:04:40 +0000600/// CheckOrImmMatcher - This checks to see if the current node is an 'and'
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000601/// with something equivalent to the specified immediate.
Chris Lattner9a515172010-02-25 02:04:40 +0000602class CheckOrImmMatcher : public Matcher {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000603 int64_t Value;
604public:
Chris Lattner9a515172010-02-25 02:04:40 +0000605 CheckOrImmMatcher(int64_t value)
606 : Matcher(CheckOrImm), Value(value) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000607
608 int64_t getValue() const { return Value; }
609
Chris Lattner9a515172010-02-25 02:04:40 +0000610 static inline bool classof(const Matcher *N) {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000611 return N->getKind() == CheckOrImm;
612 }
613
Chris Lattnerf4950d02010-02-27 06:22:57 +0000614 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
615
Chris Lattner136ab782010-02-25 06:49:58 +0000616private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000617 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000618 virtual bool isEqualImpl(const Matcher *M) const {
619 return cast<CheckOrImmMatcher>(M)->Value == Value;
620 }
621 virtual unsigned getHashImpl() const { return Value; }
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000622};
Chris Lattner9de39482010-02-16 06:10:58 +0000623
Chris Lattner9a515172010-02-25 02:04:40 +0000624/// CheckFoldableChainNodeMatcher - This checks to see if the current node
Chris Lattnerc6dc8f62010-02-16 19:15:55 +0000625/// (which defines a chain operand) is safe to fold into a larger pattern.
Chris Lattner9a515172010-02-25 02:04:40 +0000626class CheckFoldableChainNodeMatcher : public Matcher {
Chris Lattner9de39482010-02-16 06:10:58 +0000627public:
Chris Lattner9a515172010-02-25 02:04:40 +0000628 CheckFoldableChainNodeMatcher()
629 : Matcher(CheckFoldableChainNode) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000630
Chris Lattner9a515172010-02-25 02:04:40 +0000631 static inline bool classof(const Matcher *N) {
Chris Lattnerc6dc8f62010-02-16 19:15:55 +0000632 return N->getKind() == CheckFoldableChainNode;
Chris Lattner9de39482010-02-16 06:10:58 +0000633 }
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000634
Chris Lattnerf4950d02010-02-27 06:22:57 +0000635 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
636
Chris Lattner136ab782010-02-25 06:49:58 +0000637private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000638 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000639 virtual bool isEqualImpl(const Matcher *M) const { return true; }
640 virtual unsigned getHashImpl() const { return 0; }
Chris Lattner9de39482010-02-16 06:10:58 +0000641};
642
Chris Lattner9a515172010-02-25 02:04:40 +0000643/// CheckChainCompatibleMatcher - Verify that the current node's chain
Chris Lattner283adcb2010-02-17 06:23:39 +0000644/// operand is 'compatible' with the specified recorded node's.
Chris Lattner9a515172010-02-25 02:04:40 +0000645class CheckChainCompatibleMatcher : public Matcher {
Chris Lattner283adcb2010-02-17 06:23:39 +0000646 unsigned PreviousOp;
647public:
Chris Lattner9a515172010-02-25 02:04:40 +0000648 CheckChainCompatibleMatcher(unsigned previousop)
649 : Matcher(CheckChainCompatible), PreviousOp(previousop) {}
Chris Lattner283adcb2010-02-17 06:23:39 +0000650
651 unsigned getPreviousOp() const { return PreviousOp; }
652
Chris Lattner9a515172010-02-25 02:04:40 +0000653 static inline bool classof(const Matcher *N) {
Chris Lattner283adcb2010-02-17 06:23:39 +0000654 return N->getKind() == CheckChainCompatible;
655 }
656
Chris Lattnerf4950d02010-02-27 06:22:57 +0000657 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
658
Chris Lattner136ab782010-02-25 06:49:58 +0000659private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000660 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000661 virtual bool isEqualImpl(const Matcher *M) const {
Chris Lattner50c0b362010-02-26 08:06:02 +0000662 return cast<CheckChainCompatibleMatcher>(M)->PreviousOp == PreviousOp;
Chris Lattner136ab782010-02-25 06:49:58 +0000663 }
664 virtual unsigned getHashImpl() const { return PreviousOp; }
Chris Lattner283adcb2010-02-17 06:23:39 +0000665};
666
Chris Lattner9a515172010-02-25 02:04:40 +0000667/// EmitIntegerMatcher - This creates a new TargetConstant.
668class EmitIntegerMatcher : public Matcher {
Chris Lattnerddfb5222010-02-18 22:03:03 +0000669 int64_t Val;
670 MVT::SimpleValueType VT;
671public:
Chris Lattner9a515172010-02-25 02:04:40 +0000672 EmitIntegerMatcher(int64_t val, MVT::SimpleValueType vt)
673 : Matcher(EmitInteger), Val(val), VT(vt) {}
Chris Lattner283adcb2010-02-17 06:23:39 +0000674
Chris Lattner7043e0a2010-02-19 07:49:56 +0000675 int64_t getValue() const { return Val; }
Chris Lattnerddfb5222010-02-18 22:03:03 +0000676 MVT::SimpleValueType getVT() const { return VT; }
677
Chris Lattner9a515172010-02-25 02:04:40 +0000678 static inline bool classof(const Matcher *N) {
Chris Lattnerddfb5222010-02-18 22:03:03 +0000679 return N->getKind() == EmitInteger;
680 }
681
Chris Lattner136ab782010-02-25 06:49:58 +0000682private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000683 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000684 virtual bool isEqualImpl(const Matcher *M) const {
685 return cast<EmitIntegerMatcher>(M)->Val == Val &&
686 cast<EmitIntegerMatcher>(M)->VT == VT;
687 }
688 virtual unsigned getHashImpl() const { return (Val << 4) | VT; }
Chris Lattnerddfb5222010-02-18 22:03:03 +0000689};
Chris Lattner3163ca52010-02-21 03:22:59 +0000690
Chris Lattner9a515172010-02-25 02:04:40 +0000691/// EmitStringIntegerMatcher - A target constant whose value is represented
Chris Lattner3163ca52010-02-21 03:22:59 +0000692/// by a string.
Chris Lattner9a515172010-02-25 02:04:40 +0000693class EmitStringIntegerMatcher : public Matcher {
Chris Lattner3163ca52010-02-21 03:22:59 +0000694 std::string Val;
695 MVT::SimpleValueType VT;
696public:
Chris Lattner9a515172010-02-25 02:04:40 +0000697 EmitStringIntegerMatcher(const std::string &val, MVT::SimpleValueType vt)
698 : Matcher(EmitStringInteger), Val(val), VT(vt) {}
Chris Lattner3163ca52010-02-21 03:22:59 +0000699
700 const std::string &getValue() const { return Val; }
701 MVT::SimpleValueType getVT() const { return VT; }
702
Chris Lattner9a515172010-02-25 02:04:40 +0000703 static inline bool classof(const Matcher *N) {
Chris Lattner3163ca52010-02-21 03:22:59 +0000704 return N->getKind() == EmitStringInteger;
705 }
706
Chris Lattner136ab782010-02-25 06:49:58 +0000707private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000708 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000709 virtual bool isEqualImpl(const Matcher *M) const {
710 return cast<EmitStringIntegerMatcher>(M)->Val == Val &&
711 cast<EmitStringIntegerMatcher>(M)->VT == VT;
712 }
713 virtual unsigned getHashImpl() const;
Chris Lattner3163ca52010-02-21 03:22:59 +0000714};
Chris Lattnerddfb5222010-02-18 22:03:03 +0000715
Chris Lattner9a515172010-02-25 02:04:40 +0000716/// EmitRegisterMatcher - This creates a new TargetConstant.
717class EmitRegisterMatcher : public Matcher {
Chris Lattnerddfb5222010-02-18 22:03:03 +0000718 /// Reg - The def for the register that we're emitting. If this is null, then
719 /// this is a reference to zero_reg.
720 Record *Reg;
721 MVT::SimpleValueType VT;
722public:
Chris Lattner9a515172010-02-25 02:04:40 +0000723 EmitRegisterMatcher(Record *reg, MVT::SimpleValueType vt)
724 : Matcher(EmitRegister), Reg(reg), VT(vt) {}
Chris Lattnerddfb5222010-02-18 22:03:03 +0000725
726 Record *getReg() const { return Reg; }
727 MVT::SimpleValueType getVT() const { return VT; }
728
Chris Lattner9a515172010-02-25 02:04:40 +0000729 static inline bool classof(const Matcher *N) {
Chris Lattnerddfb5222010-02-18 22:03:03 +0000730 return N->getKind() == EmitRegister;
731 }
732
Chris Lattner136ab782010-02-25 06:49:58 +0000733private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000734 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000735 virtual bool isEqualImpl(const Matcher *M) const {
736 return cast<EmitRegisterMatcher>(M)->Reg == Reg &&
737 cast<EmitRegisterMatcher>(M)->VT == VT;
738 }
739 virtual unsigned getHashImpl() const {
740 return ((unsigned)(intptr_t)Reg) << 4 | VT;
741 }
Chris Lattnerddfb5222010-02-18 22:03:03 +0000742};
Chris Lattner3163ca52010-02-21 03:22:59 +0000743
Chris Lattner9a515172010-02-25 02:04:40 +0000744/// EmitConvertToTargetMatcher - Emit an operation that reads a specified
Chris Lattner3163ca52010-02-21 03:22:59 +0000745/// recorded node and converts it from being a ISD::Constant to
746/// ISD::TargetConstant, likewise for ConstantFP.
Chris Lattner9a515172010-02-25 02:04:40 +0000747class EmitConvertToTargetMatcher : public Matcher {
Chris Lattner3163ca52010-02-21 03:22:59 +0000748 unsigned Slot;
749public:
Chris Lattner9a515172010-02-25 02:04:40 +0000750 EmitConvertToTargetMatcher(unsigned slot)
751 : Matcher(EmitConvertToTarget), Slot(slot) {}
Chris Lattner3163ca52010-02-21 03:22:59 +0000752
753 unsigned getSlot() const { return Slot; }
754
Chris Lattner9a515172010-02-25 02:04:40 +0000755 static inline bool classof(const Matcher *N) {
Chris Lattner3163ca52010-02-21 03:22:59 +0000756 return N->getKind() == EmitConvertToTarget;
757 }
758
Chris Lattner136ab782010-02-25 06:49:58 +0000759private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000760 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000761 virtual bool isEqualImpl(const Matcher *M) const {
762 return cast<EmitConvertToTargetMatcher>(M)->Slot == Slot;
763 }
764 virtual unsigned getHashImpl() const { return Slot; }
Chris Lattner3163ca52010-02-21 03:22:59 +0000765};
766
Chris Lattner9a515172010-02-25 02:04:40 +0000767/// EmitMergeInputChainsMatcher - Emit a node that merges a list of input
Chris Lattner3163ca52010-02-21 03:22:59 +0000768/// chains together with a token factor. The list of nodes are the nodes in the
769/// matched pattern that have chain input/outputs. This node adds all input
770/// chains of these nodes if they are not themselves a node in the pattern.
Chris Lattner9a515172010-02-25 02:04:40 +0000771class EmitMergeInputChainsMatcher : public Matcher {
Chris Lattner3163ca52010-02-21 03:22:59 +0000772 SmallVector<unsigned, 3> ChainNodes;
773public:
Chris Lattner9a515172010-02-25 02:04:40 +0000774 EmitMergeInputChainsMatcher(const unsigned *nodes, unsigned NumNodes)
775 : Matcher(EmitMergeInputChains), ChainNodes(nodes, nodes+NumNodes) {}
Chris Lattner3163ca52010-02-21 03:22:59 +0000776
777 unsigned getNumNodes() const { return ChainNodes.size(); }
778
779 unsigned getNode(unsigned i) const {
780 assert(i < ChainNodes.size());
781 return ChainNodes[i];
782 }
783
Chris Lattner9a515172010-02-25 02:04:40 +0000784 static inline bool classof(const Matcher *N) {
Chris Lattner3163ca52010-02-21 03:22:59 +0000785 return N->getKind() == EmitMergeInputChains;
786 }
787
Chris Lattner136ab782010-02-25 06:49:58 +0000788private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000789 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000790 virtual bool isEqualImpl(const Matcher *M) const {
791 return cast<EmitMergeInputChainsMatcher>(M)->ChainNodes == ChainNodes;
792 }
793 virtual unsigned getHashImpl() const;
Chris Lattner3163ca52010-02-21 03:22:59 +0000794};
795
Chris Lattner9a515172010-02-25 02:04:40 +0000796/// EmitCopyToRegMatcher - Emit a CopyToReg node from a value to a physreg,
Chris Lattner3163ca52010-02-21 03:22:59 +0000797/// pushing the chain and flag results.
798///
Chris Lattner9a515172010-02-25 02:04:40 +0000799class EmitCopyToRegMatcher : public Matcher {
Chris Lattner3163ca52010-02-21 03:22:59 +0000800 unsigned SrcSlot; // Value to copy into the physreg.
801 Record *DestPhysReg;
802public:
Chris Lattner9a515172010-02-25 02:04:40 +0000803 EmitCopyToRegMatcher(unsigned srcSlot, Record *destPhysReg)
804 : Matcher(EmitCopyToReg), SrcSlot(srcSlot), DestPhysReg(destPhysReg) {}
Chris Lattner3163ca52010-02-21 03:22:59 +0000805
806 unsigned getSrcSlot() const { return SrcSlot; }
807 Record *getDestPhysReg() const { return DestPhysReg; }
808
Chris Lattner9a515172010-02-25 02:04:40 +0000809 static inline bool classof(const Matcher *N) {
Chris Lattner3163ca52010-02-21 03:22:59 +0000810 return N->getKind() == EmitCopyToReg;
811 }
812
Chris Lattner136ab782010-02-25 06:49:58 +0000813private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000814 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000815 virtual bool isEqualImpl(const Matcher *M) const {
816 return cast<EmitCopyToRegMatcher>(M)->SrcSlot == SrcSlot &&
817 cast<EmitCopyToRegMatcher>(M)->DestPhysReg == DestPhysReg;
818 }
819 virtual unsigned getHashImpl() const {
820 return SrcSlot ^ ((unsigned)(intptr_t)DestPhysReg << 4);
821 }
Chris Lattner3163ca52010-02-21 03:22:59 +0000822};
823
824
825
Chris Lattner9a515172010-02-25 02:04:40 +0000826/// EmitNodeXFormMatcher - Emit an operation that runs an SDNodeXForm on a
Chris Lattner3163ca52010-02-21 03:22:59 +0000827/// recorded node and records the result.
Chris Lattner9a515172010-02-25 02:04:40 +0000828class EmitNodeXFormMatcher : public Matcher {
Chris Lattner3163ca52010-02-21 03:22:59 +0000829 unsigned Slot;
830 Record *NodeXForm;
831public:
Chris Lattner9a515172010-02-25 02:04:40 +0000832 EmitNodeXFormMatcher(unsigned slot, Record *nodeXForm)
833 : Matcher(EmitNodeXForm), Slot(slot), NodeXForm(nodeXForm) {}
Chris Lattner3163ca52010-02-21 03:22:59 +0000834
835 unsigned getSlot() const { return Slot; }
836 Record *getNodeXForm() const { return NodeXForm; }
837
Chris Lattner9a515172010-02-25 02:04:40 +0000838 static inline bool classof(const Matcher *N) {
Chris Lattner3163ca52010-02-21 03:22:59 +0000839 return N->getKind() == EmitNodeXForm;
840 }
841
Chris Lattner136ab782010-02-25 06:49:58 +0000842private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000843 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000844 virtual bool isEqualImpl(const Matcher *M) const {
845 return cast<EmitNodeXFormMatcher>(M)->Slot == Slot &&
846 cast<EmitNodeXFormMatcher>(M)->NodeXForm == NodeXForm;
847 }
848 virtual unsigned getHashImpl() const {
849 return Slot ^ ((unsigned)(intptr_t)NodeXForm << 4);
850 }
Chris Lattner3163ca52010-02-21 03:22:59 +0000851};
Chris Lattnerddfb5222010-02-18 22:03:03 +0000852
Chris Lattner9a515172010-02-25 02:04:40 +0000853/// EmitNodeMatcher - This signals a successful match and generates a node.
854class EmitNodeMatcher : public Matcher {
Chris Lattner3163ca52010-02-21 03:22:59 +0000855 std::string OpcodeName;
856 const SmallVector<MVT::SimpleValueType, 3> VTs;
857 const SmallVector<unsigned, 6> Operands;
858 bool HasChain, HasFlag, HasMemRefs;
Chris Lattnerddfb5222010-02-18 22:03:03 +0000859
Chris Lattner3163ca52010-02-21 03:22:59 +0000860 /// NumFixedArityOperands - If this is a fixed arity node, this is set to -1.
861 /// If this is a varidic node, this is set to the number of fixed arity
862 /// operands in the root of the pattern. The rest are appended to this node.
863 int NumFixedArityOperands;
864public:
Chris Lattner9a515172010-02-25 02:04:40 +0000865 EmitNodeMatcher(const std::string &opcodeName,
Chris Lattner136ab782010-02-25 06:49:58 +0000866 const MVT::SimpleValueType *vts, unsigned numvts,
867 const unsigned *operands, unsigned numops,
868 bool hasChain, bool hasFlag, bool hasmemrefs,
869 int numfixedarityoperands)
Chris Lattner9a515172010-02-25 02:04:40 +0000870 : Matcher(EmitNode), OpcodeName(opcodeName),
Chris Lattner3163ca52010-02-21 03:22:59 +0000871 VTs(vts, vts+numvts), Operands(operands, operands+numops),
872 HasChain(hasChain), HasFlag(hasFlag), HasMemRefs(hasmemrefs),
873 NumFixedArityOperands(numfixedarityoperands) {}
874
875 const std::string &getOpcodeName() const { return OpcodeName; }
876
877 unsigned getNumVTs() const { return VTs.size(); }
878 MVT::SimpleValueType getVT(unsigned i) const {
879 assert(i < VTs.size());
880 return VTs[i];
881 }
882
883 unsigned getNumOperands() const { return Operands.size(); }
884 unsigned getOperand(unsigned i) const {
885 assert(i < Operands.size());
886 return Operands[i];
887 }
888
889 bool hasChain() const { return HasChain; }
890 bool hasFlag() const { return HasFlag; }
891 bool hasMemRefs() const { return HasMemRefs; }
892 int getNumFixedArityOperands() const { return NumFixedArityOperands; }
Chris Lattnerddfb5222010-02-18 22:03:03 +0000893
Chris Lattner9a515172010-02-25 02:04:40 +0000894 static inline bool classof(const Matcher *N) {
Chris Lattnerddfb5222010-02-18 22:03:03 +0000895 return N->getKind() == EmitNode;
896 }
897
Chris Lattner136ab782010-02-25 06:49:58 +0000898private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000899 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000900 virtual bool isEqualImpl(const Matcher *M) const;
901 virtual unsigned getHashImpl() const;
Chris Lattnerddfb5222010-02-18 22:03:03 +0000902};
Chris Lattnerb085ea12010-02-21 06:03:07 +0000903
Chris Lattner9a515172010-02-25 02:04:40 +0000904/// MarkFlagResultsMatcher - This node indicates which non-root nodes in the
905/// pattern produce flags. This allows CompleteMatchMatcher to update them
Chris Lattner69f60c82010-02-24 05:33:42 +0000906/// with the output flag of the resultant code.
Chris Lattner9a515172010-02-25 02:04:40 +0000907class MarkFlagResultsMatcher : public Matcher {
Chris Lattner69f60c82010-02-24 05:33:42 +0000908 SmallVector<unsigned, 3> FlagResultNodes;
909public:
Chris Lattner9a515172010-02-25 02:04:40 +0000910 MarkFlagResultsMatcher(const unsigned *nodes, unsigned NumNodes)
911 : Matcher(MarkFlagResults), FlagResultNodes(nodes, nodes+NumNodes) {}
Chris Lattner69f60c82010-02-24 05:33:42 +0000912
913 unsigned getNumNodes() const { return FlagResultNodes.size(); }
914
915 unsigned getNode(unsigned i) const {
916 assert(i < FlagResultNodes.size());
917 return FlagResultNodes[i];
918 }
919
Chris Lattner9a515172010-02-25 02:04:40 +0000920 static inline bool classof(const Matcher *N) {
Chris Lattner69f60c82010-02-24 05:33:42 +0000921 return N->getKind() == MarkFlagResults;
922 }
923
Chris Lattner136ab782010-02-25 06:49:58 +0000924private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000925 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000926 virtual bool isEqualImpl(const Matcher *M) const {
927 return cast<MarkFlagResultsMatcher>(M)->FlagResultNodes == FlagResultNodes;
928 }
929 virtual unsigned getHashImpl() const;
Chris Lattner69f60c82010-02-24 05:33:42 +0000930};
931
Chris Lattner9a515172010-02-25 02:04:40 +0000932/// CompleteMatchMatcher - Complete a match by replacing the results of the
Chris Lattnerb085ea12010-02-21 06:03:07 +0000933/// pattern with the newly generated nodes. This also prints a comment
934/// indicating the source and dest patterns.
Chris Lattner9a515172010-02-25 02:04:40 +0000935class CompleteMatchMatcher : public Matcher {
Chris Lattnerb085ea12010-02-21 06:03:07 +0000936 SmallVector<unsigned, 2> Results;
Chris Lattner3163ca52010-02-21 03:22:59 +0000937 const PatternToMatch &Pattern;
938public:
Chris Lattner9a515172010-02-25 02:04:40 +0000939 CompleteMatchMatcher(const unsigned *results, unsigned numresults,
Chris Lattnerb085ea12010-02-21 06:03:07 +0000940 const PatternToMatch &pattern)
Chris Lattner9a515172010-02-25 02:04:40 +0000941 : Matcher(CompleteMatch), Results(results, results+numresults),
Chris Lattnerb085ea12010-02-21 06:03:07 +0000942 Pattern(pattern) {}
943
944 unsigned getNumResults() const { return Results.size(); }
945 unsigned getResult(unsigned R) const { return Results[R]; }
Chris Lattner3163ca52010-02-21 03:22:59 +0000946 const PatternToMatch &getPattern() const { return Pattern; }
947
Chris Lattner9a515172010-02-25 02:04:40 +0000948 static inline bool classof(const Matcher *N) {
Chris Lattnerb085ea12010-02-21 06:03:07 +0000949 return N->getKind() == CompleteMatch;
Chris Lattner3163ca52010-02-21 03:22:59 +0000950 }
951
Chris Lattner136ab782010-02-25 06:49:58 +0000952private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000953 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000954 virtual bool isEqualImpl(const Matcher *M) const {
955 return cast<CompleteMatchMatcher>(M)->Results == Results &&
956 &cast<CompleteMatchMatcher>(M)->Pattern == &Pattern;
957 }
958 virtual unsigned getHashImpl() const;
Chris Lattner3163ca52010-02-21 03:22:59 +0000959};
Chris Lattnerddfb5222010-02-18 22:03:03 +0000960
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000961} // end namespace llvm
962
963#endif