blob: b4de0e4fcf01ec7f4aa39f51b6a2eadeb160972a [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 Lattnerddfd5ab2010-02-27 07:49:13 +0000113 /// isContradictory - Return true of these two matchers could never match on
114 /// the same node.
115 bool isContradictory(const Matcher *Other) const {
116 // Since this predicate is reflexive, we canonicalize the ordering so that
117 // we always match a node against nodes with kinds that are greater or equal
118 // to them. For example, we'll pass in a CheckType node as an argument to
119 // the CheckOpcode method, not the other way around.
120 if (getKind() < Other->getKind())
121 return isContradictoryImpl(Other);
122 return Other->isContradictoryImpl(this);
123 }
124
Chris Lattner392b1bf2010-02-25 06:53:39 +0000125 void print(raw_ostream &OS, unsigned indent = 0) const;
Chris Lattnerddfd5ab2010-02-27 07:49:13 +0000126 void printOne(raw_ostream &OS) const;
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000127 void dump() const;
Chris Lattner3b31c982010-02-18 02:49:24 +0000128protected:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000129 virtual void printImpl(raw_ostream &OS, unsigned indent) const = 0;
Chris Lattner136ab782010-02-25 06:49:58 +0000130 virtual bool isEqualImpl(const Matcher *M) const = 0;
131 virtual unsigned getHashImpl() const = 0;
Chris Lattnerddfd5ab2010-02-27 07:49:13 +0000132 virtual bool isContradictoryImpl(const Matcher *M) const { return false; }
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000133};
134
Chris Lattner42363662010-02-25 19:00:39 +0000135/// ScopeMatcher - This attempts to match each of its children to find the first
136/// one that successfully matches. If one child fails, it tries the next child.
137/// If none of the children match then this check fails. It never has a 'next'.
Chris Lattner9a515172010-02-25 02:04:40 +0000138class ScopeMatcher : public Matcher {
Chris Lattner42363662010-02-25 19:00:39 +0000139 SmallVector<Matcher*, 4> Children;
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000140public:
Chris Lattner42363662010-02-25 19:00:39 +0000141 ScopeMatcher(Matcher *const *children, unsigned numchildren)
142 : Matcher(Scope), Children(children, children+numchildren) {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000143 }
Chris Lattner42363662010-02-25 19:00:39 +0000144 virtual ~ScopeMatcher();
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000145
Chris Lattner42363662010-02-25 19:00:39 +0000146 unsigned getNumChildren() const { return Children.size(); }
147
148 Matcher *getChild(unsigned i) { return Children[i]; }
149 const Matcher *getChild(unsigned i) const { return Children[i]; }
150
151 void resetChild(unsigned i, Matcher *N) {
152 delete Children[i];
153 Children[i] = N;
154 }
155
156 Matcher *takeChild(unsigned i) {
157 Matcher *Res = Children[i];
158 Children[i] = 0;
159 return Res;
160 }
Chris Lattner54ee7362010-02-26 07:35:27 +0000161
162 void setNumChildren(unsigned NC) {
163 if (NC < Children.size()) {
164 // delete any children we're about to lose pointers to.
165 for (unsigned i = NC, e = Children.size(); i != e; ++i)
166 delete Children[i];
167 }
168 Children.resize(NC);
169 }
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000170
Chris Lattner9a515172010-02-25 02:04:40 +0000171 static inline bool classof(const Matcher *N) {
Chris Lattnerac10e4f2010-02-25 01:56:48 +0000172 return N->getKind() == Scope;
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000173 }
174
Chris Lattner136ab782010-02-25 06:49:58 +0000175private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000176 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000177 virtual bool isEqualImpl(const Matcher *M) const { return false; }
Chris Lattner42363662010-02-25 19:00:39 +0000178 virtual unsigned getHashImpl() const { return 12312; }
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000179};
180
Chris Lattner9a515172010-02-25 02:04:40 +0000181/// RecordMatcher - Save the current node in the operand list.
182class RecordMatcher : public Matcher {
Chris Lattner56cf88d2010-02-17 01:03:09 +0000183 /// WhatFor - This is a string indicating why we're recording this. This
184 /// should only be used for comment generation not anything semantic.
185 std::string WhatFor;
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000186public:
Chris Lattner9a515172010-02-25 02:04:40 +0000187 RecordMatcher(const std::string &whatfor)
188 : Matcher(RecordNode), WhatFor(whatfor) {}
Chris Lattner56cf88d2010-02-17 01:03:09 +0000189
Chris Lattner086ca522010-02-17 01:27:29 +0000190 const std::string &getWhatFor() const { return WhatFor; }
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000191
Chris Lattner9a515172010-02-25 02:04:40 +0000192 static inline bool classof(const Matcher *N) {
Chris Lattnerddfb5222010-02-18 22:03:03 +0000193 return N->getKind() == RecordNode;
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000194 }
195
Chris Lattnerf4950d02010-02-27 06:22:57 +0000196 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
Chris Lattner136ab782010-02-25 06:49:58 +0000197private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000198 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000199 virtual bool isEqualImpl(const Matcher *M) const { return true; }
200 virtual unsigned getHashImpl() const { return 0; }
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000201};
202
Chris Lattner9a515172010-02-25 02:04:40 +0000203/// RecordChildMatcher - Save a numbered child of the current node, or fail
Chris Lattner3ee1bc42010-02-24 07:31:45 +0000204/// the match if it doesn't exist. This is logically equivalent to:
205/// MoveChild N + RecordNode + MoveParent.
Chris Lattner9a515172010-02-25 02:04:40 +0000206class RecordChildMatcher : public Matcher {
Chris Lattner3ee1bc42010-02-24 07:31:45 +0000207 unsigned ChildNo;
208
209 /// WhatFor - This is a string indicating why we're recording this. This
210 /// should only be used for comment generation not anything semantic.
211 std::string WhatFor;
212public:
Chris Lattner9a515172010-02-25 02:04:40 +0000213 RecordChildMatcher(unsigned childno, const std::string &whatfor)
214 : Matcher(RecordChild), ChildNo(childno), WhatFor(whatfor) {}
Chris Lattner3ee1bc42010-02-24 07:31:45 +0000215
216 unsigned getChildNo() const { return ChildNo; }
217 const std::string &getWhatFor() const { return WhatFor; }
218
Chris Lattner9a515172010-02-25 02:04:40 +0000219 static inline bool classof(const Matcher *N) {
Chris Lattner3ee1bc42010-02-24 07:31:45 +0000220 return N->getKind() == RecordChild;
221 }
222
Chris Lattnerf4950d02010-02-27 06:22:57 +0000223 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
224
Chris Lattner136ab782010-02-25 06:49:58 +0000225private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000226 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000227 virtual bool isEqualImpl(const Matcher *M) const {
228 return cast<RecordChildMatcher>(M)->getChildNo() == getChildNo();
229 }
230 virtual unsigned getHashImpl() const { return getChildNo(); }
Chris Lattner3ee1bc42010-02-24 07:31:45 +0000231};
232
Chris Lattner9a515172010-02-25 02:04:40 +0000233/// RecordMemRefMatcher - Save the current node's memref.
234class RecordMemRefMatcher : public Matcher {
Chris Lattner3163ca52010-02-21 03:22:59 +0000235public:
Chris Lattner9a515172010-02-25 02:04:40 +0000236 RecordMemRefMatcher() : Matcher(RecordMemRef) {}
Chris Lattner3163ca52010-02-21 03:22:59 +0000237
Chris Lattner9a515172010-02-25 02:04:40 +0000238 static inline bool classof(const Matcher *N) {
Chris Lattner3163ca52010-02-21 03:22:59 +0000239 return N->getKind() == RecordMemRef;
240 }
241
Chris Lattnerf4950d02010-02-27 06:22:57 +0000242 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
243
Chris Lattner136ab782010-02-25 06:49:58 +0000244private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000245 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000246 virtual bool isEqualImpl(const Matcher *M) const { return true; }
247 virtual unsigned getHashImpl() const { return 0; }
Chris Lattner3163ca52010-02-21 03:22:59 +0000248};
249
250
Chris Lattner9a515172010-02-25 02:04:40 +0000251/// CaptureFlagInputMatcher - If the current record has a flag input, record
Chris Lattner3163ca52010-02-21 03:22:59 +0000252/// it so that it is used as an input to the generated code.
Chris Lattner9a515172010-02-25 02:04:40 +0000253class CaptureFlagInputMatcher : public Matcher {
Chris Lattner3163ca52010-02-21 03:22:59 +0000254public:
Chris Lattner9a515172010-02-25 02:04:40 +0000255 CaptureFlagInputMatcher() : Matcher(CaptureFlagInput) {}
Chris Lattner3163ca52010-02-21 03:22:59 +0000256
Chris Lattner9a515172010-02-25 02:04:40 +0000257 static inline bool classof(const Matcher *N) {
Chris Lattner3163ca52010-02-21 03:22:59 +0000258 return N->getKind() == CaptureFlagInput;
259 }
260
Chris Lattnerf4950d02010-02-27 06:22:57 +0000261 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
262
Chris Lattner136ab782010-02-25 06:49:58 +0000263private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000264 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000265 virtual bool isEqualImpl(const Matcher *M) const { return true; }
266 virtual unsigned getHashImpl() const { return 0; }
Chris Lattner3163ca52010-02-21 03:22:59 +0000267};
268
Chris Lattner9a515172010-02-25 02:04:40 +0000269/// MoveChildMatcher - This tells the interpreter to move into the
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000270/// specified child node.
Chris Lattner9a515172010-02-25 02:04:40 +0000271class MoveChildMatcher : public Matcher {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000272 unsigned ChildNo;
273public:
Chris Lattner9a515172010-02-25 02:04:40 +0000274 MoveChildMatcher(unsigned childNo) : Matcher(MoveChild), ChildNo(childNo) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000275
276 unsigned getChildNo() const { return ChildNo; }
277
Chris Lattner9a515172010-02-25 02:04:40 +0000278 static inline bool classof(const Matcher *N) {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000279 return N->getKind() == MoveChild;
280 }
281
Chris Lattnerf4950d02010-02-27 06:22:57 +0000282 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
283
Chris Lattner136ab782010-02-25 06:49:58 +0000284private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000285 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000286 virtual bool isEqualImpl(const Matcher *M) const {
287 return cast<MoveChildMatcher>(M)->getChildNo() == getChildNo();
288 }
289 virtual unsigned getHashImpl() const { return getChildNo(); }
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000290};
291
Chris Lattner9a515172010-02-25 02:04:40 +0000292/// MoveParentMatcher - This tells the interpreter to move to the parent
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000293/// of the current node.
Chris Lattner9a515172010-02-25 02:04:40 +0000294class MoveParentMatcher : public Matcher {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000295public:
Chris Lattner9a515172010-02-25 02:04:40 +0000296 MoveParentMatcher() : Matcher(MoveParent) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000297
Chris Lattner9a515172010-02-25 02:04:40 +0000298 static inline bool classof(const Matcher *N) {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000299 return N->getKind() == MoveParent;
300 }
301
Chris Lattnerf4950d02010-02-27 06:22:57 +0000302 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
303
Chris Lattner136ab782010-02-25 06:49:58 +0000304private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000305 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000306 virtual bool isEqualImpl(const Matcher *M) const { return true; }
307 virtual unsigned getHashImpl() const { return 0; }
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000308};
309
Chris Lattner9a515172010-02-25 02:04:40 +0000310/// CheckSameMatcher - This checks to see if this node is exactly the same
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000311/// node as the specified match that was recorded with 'Record'. This is used
312/// when patterns have the same name in them, like '(mul GPR:$in, GPR:$in)'.
Chris Lattner9a515172010-02-25 02:04:40 +0000313class CheckSameMatcher : public Matcher {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000314 unsigned MatchNumber;
315public:
Chris Lattner9a515172010-02-25 02:04:40 +0000316 CheckSameMatcher(unsigned matchnumber)
Chris Lattner136ab782010-02-25 06:49:58 +0000317 : Matcher(CheckSame), MatchNumber(matchnumber) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000318
319 unsigned getMatchNumber() const { return MatchNumber; }
320
Chris Lattner9a515172010-02-25 02:04:40 +0000321 static inline bool classof(const Matcher *N) {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000322 return N->getKind() == CheckSame;
323 }
324
Chris Lattnerf4950d02010-02-27 06:22:57 +0000325 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
326
Chris Lattner136ab782010-02-25 06:49:58 +0000327private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000328 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000329 virtual bool isEqualImpl(const Matcher *M) const {
330 return cast<CheckSameMatcher>(M)->getMatchNumber() == getMatchNumber();
331 }
332 virtual unsigned getHashImpl() const { return getMatchNumber(); }
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000333};
334
Chris Lattner9a515172010-02-25 02:04:40 +0000335/// CheckPatternPredicateMatcher - This checks the target-specific predicate
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000336/// to see if the entire pattern is capable of matching. This predicate does
337/// not take a node as input. This is used for subtarget feature checks etc.
Chris Lattner9a515172010-02-25 02:04:40 +0000338class CheckPatternPredicateMatcher : public Matcher {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000339 std::string Predicate;
340public:
Chris Lattner9a515172010-02-25 02:04:40 +0000341 CheckPatternPredicateMatcher(StringRef predicate)
Chris Lattner136ab782010-02-25 06:49:58 +0000342 : Matcher(CheckPatternPredicate), Predicate(predicate) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000343
344 StringRef getPredicate() const { return Predicate; }
345
Chris Lattner9a515172010-02-25 02:04:40 +0000346 static inline bool classof(const Matcher *N) {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000347 return N->getKind() == CheckPatternPredicate;
348 }
349
Chris Lattnerf4950d02010-02-27 06:22:57 +0000350 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
351
Chris Lattner136ab782010-02-25 06:49:58 +0000352private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000353 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000354 virtual bool isEqualImpl(const Matcher *M) const {
355 return cast<CheckPatternPredicateMatcher>(M)->getPredicate() == Predicate;
356 }
357 virtual unsigned getHashImpl() const;
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000358};
359
Chris Lattner9a515172010-02-25 02:04:40 +0000360/// CheckPredicateMatcher - This checks the target-specific predicate to
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000361/// see if the node is acceptable.
Chris Lattner9a515172010-02-25 02:04:40 +0000362class CheckPredicateMatcher : public Matcher {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000363 StringRef PredName;
364public:
Chris Lattner9a515172010-02-25 02:04:40 +0000365 CheckPredicateMatcher(StringRef predname)
366 : Matcher(CheckPredicate), PredName(predname) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000367
368 StringRef getPredicateName() const { return PredName; }
369
Chris Lattner9a515172010-02-25 02:04:40 +0000370 static inline bool classof(const Matcher *N) {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000371 return N->getKind() == CheckPredicate;
372 }
373
Chris Lattnerf4950d02010-02-27 06:22:57 +0000374 // TODO: Ok?
375 //virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
376
Chris Lattner136ab782010-02-25 06:49:58 +0000377private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000378 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000379 virtual bool isEqualImpl(const Matcher *M) const {
380 return cast<CheckPredicateMatcher>(M)->PredName == PredName;
381 }
382 virtual unsigned getHashImpl() const;
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000383};
384
385
Chris Lattner9a515172010-02-25 02:04:40 +0000386/// CheckOpcodeMatcher - This checks to see if the current node has the
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000387/// specified opcode, if not it fails to match.
Chris Lattner9a515172010-02-25 02:04:40 +0000388class CheckOpcodeMatcher : public Matcher {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000389 StringRef OpcodeName;
390public:
Chris Lattner9a515172010-02-25 02:04:40 +0000391 CheckOpcodeMatcher(StringRef opcodename)
392 : Matcher(CheckOpcode), OpcodeName(opcodename) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000393
394 StringRef getOpcodeName() const { return OpcodeName; }
395
Chris Lattner9a515172010-02-25 02:04:40 +0000396 static inline bool classof(const Matcher *N) {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000397 return N->getKind() == CheckOpcode;
398 }
399
Chris Lattnerf4950d02010-02-27 06:22:57 +0000400 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
401
Chris Lattner136ab782010-02-25 06:49:58 +0000402private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000403 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000404 virtual bool isEqualImpl(const Matcher *M) const {
405 return cast<CheckOpcodeMatcher>(M)->OpcodeName == OpcodeName;
406 }
407 virtual unsigned getHashImpl() const;
Chris Lattnerddfd5ab2010-02-27 07:49:13 +0000408 virtual bool isContradictoryImpl(const Matcher *M) const;
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000409};
410
Chris Lattner9a515172010-02-25 02:04:40 +0000411/// CheckMultiOpcodeMatcher - This checks to see if the current node has one
Chris Lattnerf58c08e2010-02-22 22:30:37 +0000412/// of the specified opcode, if not it fails to match.
Chris Lattner9a515172010-02-25 02:04:40 +0000413class CheckMultiOpcodeMatcher : public Matcher {
Chris Lattnerf58c08e2010-02-22 22:30:37 +0000414 SmallVector<StringRef, 4> OpcodeNames;
415public:
Chris Lattner9a515172010-02-25 02:04:40 +0000416 CheckMultiOpcodeMatcher(const StringRef *opcodes, unsigned numops)
417 : Matcher(CheckMultiOpcode), OpcodeNames(opcodes, opcodes+numops) {}
Chris Lattnerf58c08e2010-02-22 22:30:37 +0000418
419 unsigned getNumOpcodeNames() const { return OpcodeNames.size(); }
420 StringRef getOpcodeName(unsigned i) const { return OpcodeNames[i]; }
421
Chris Lattner9a515172010-02-25 02:04:40 +0000422 static inline bool classof(const Matcher *N) {
Chris Lattnerf58c08e2010-02-22 22:30:37 +0000423 return N->getKind() == CheckMultiOpcode;
424 }
425
Chris Lattnerf4950d02010-02-27 06:22:57 +0000426 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
427
Chris Lattner136ab782010-02-25 06:49:58 +0000428private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000429 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000430 virtual bool isEqualImpl(const Matcher *M) const {
431 return cast<CheckMultiOpcodeMatcher>(M)->OpcodeNames == OpcodeNames;
432 }
433 virtual unsigned getHashImpl() const;
Chris Lattnerf58c08e2010-02-22 22:30:37 +0000434};
435
436
437
Chris Lattner9a515172010-02-25 02:04:40 +0000438/// CheckTypeMatcher - This checks to see if the current node has the
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000439/// specified type, if not it fails to match.
Chris Lattner9a515172010-02-25 02:04:40 +0000440class CheckTypeMatcher : public Matcher {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000441 MVT::SimpleValueType Type;
442public:
Chris Lattner9a515172010-02-25 02:04:40 +0000443 CheckTypeMatcher(MVT::SimpleValueType type)
444 : Matcher(CheckType), Type(type) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000445
446 MVT::SimpleValueType getType() const { return Type; }
447
Chris Lattner9a515172010-02-25 02:04:40 +0000448 static inline bool classof(const Matcher *N) {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000449 return N->getKind() == CheckType;
450 }
451
Chris Lattnerf4950d02010-02-27 06:22:57 +0000452 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
453
Chris Lattner136ab782010-02-25 06:49:58 +0000454private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000455 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000456 virtual bool isEqualImpl(const Matcher *M) const {
Chris Lattner2d0e0792010-02-26 08:05:36 +0000457 return cast<CheckTypeMatcher>(M)->Type == Type;
Chris Lattner136ab782010-02-25 06:49:58 +0000458 }
459 virtual unsigned getHashImpl() const { return Type; }
Chris Lattnerddfd5ab2010-02-27 07:49:13 +0000460 virtual bool isContradictoryImpl(const Matcher *M) const;
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000461};
Chris Lattner3c664b32010-02-24 20:15:25 +0000462
Chris Lattner9a515172010-02-25 02:04:40 +0000463/// CheckChildTypeMatcher - This checks to see if a child node has the
Chris Lattner3c664b32010-02-24 20:15:25 +0000464/// specified type, if not it fails to match.
Chris Lattner9a515172010-02-25 02:04:40 +0000465class CheckChildTypeMatcher : public Matcher {
Chris Lattner3c664b32010-02-24 20:15:25 +0000466 unsigned ChildNo;
467 MVT::SimpleValueType Type;
468public:
Chris Lattner9a515172010-02-25 02:04:40 +0000469 CheckChildTypeMatcher(unsigned childno, MVT::SimpleValueType type)
470 : Matcher(CheckChildType), ChildNo(childno), Type(type) {}
Chris Lattner3c664b32010-02-24 20:15:25 +0000471
472 unsigned getChildNo() const { return ChildNo; }
473 MVT::SimpleValueType getType() const { return Type; }
474
Chris Lattner9a515172010-02-25 02:04:40 +0000475 static inline bool classof(const Matcher *N) {
Chris Lattner3c664b32010-02-24 20:15:25 +0000476 return N->getKind() == CheckChildType;
477 }
478
Chris Lattnerf4950d02010-02-27 06:22:57 +0000479 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
480
Chris Lattner136ab782010-02-25 06:49:58 +0000481private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000482 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000483 virtual bool isEqualImpl(const Matcher *M) const {
484 return cast<CheckChildTypeMatcher>(M)->ChildNo == ChildNo &&
485 cast<CheckChildTypeMatcher>(M)->Type == Type;
486 }
487 virtual unsigned getHashImpl() const { return (Type << 3) | ChildNo; }
Chris Lattnerddfd5ab2010-02-27 07:49:13 +0000488 virtual bool isContradictoryImpl(const Matcher *M) const;
Chris Lattner3c664b32010-02-24 20:15:25 +0000489};
490
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000491
Chris Lattner9a515172010-02-25 02:04:40 +0000492/// CheckIntegerMatcher - This checks to see if the current node is a
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000493/// ConstantSDNode with the specified integer value, if not it fails to match.
Chris Lattner9a515172010-02-25 02:04:40 +0000494class CheckIntegerMatcher : public Matcher {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000495 int64_t Value;
496public:
Chris Lattner9a515172010-02-25 02:04:40 +0000497 CheckIntegerMatcher(int64_t value)
498 : Matcher(CheckInteger), Value(value) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000499
500 int64_t getValue() const { return Value; }
501
Chris Lattner9a515172010-02-25 02:04:40 +0000502 static inline bool classof(const Matcher *N) {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000503 return N->getKind() == CheckInteger;
504 }
505
Chris Lattnerf4950d02010-02-27 06:22:57 +0000506 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
507
Chris Lattner136ab782010-02-25 06:49:58 +0000508private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000509 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000510 virtual bool isEqualImpl(const Matcher *M) const {
511 return cast<CheckIntegerMatcher>(M)->Value == Value;
512 }
513 virtual unsigned getHashImpl() const { return Value; }
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000514};
515
Chris Lattner9a515172010-02-25 02:04:40 +0000516/// CheckCondCodeMatcher - This checks to see if the current node is a
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000517/// CondCodeSDNode with the specified condition, if not it fails to match.
Chris Lattner9a515172010-02-25 02:04:40 +0000518class CheckCondCodeMatcher : public Matcher {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000519 StringRef CondCodeName;
520public:
Chris Lattner9a515172010-02-25 02:04:40 +0000521 CheckCondCodeMatcher(StringRef condcodename)
522 : Matcher(CheckCondCode), CondCodeName(condcodename) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000523
524 StringRef getCondCodeName() const { return CondCodeName; }
525
Chris Lattner9a515172010-02-25 02:04:40 +0000526 static inline bool classof(const Matcher *N) {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000527 return N->getKind() == CheckCondCode;
528 }
529
Chris Lattnerf4950d02010-02-27 06:22:57 +0000530 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
531
Chris Lattner136ab782010-02-25 06:49:58 +0000532private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000533 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000534 virtual bool isEqualImpl(const Matcher *M) const {
535 return cast<CheckCondCodeMatcher>(M)->CondCodeName == CondCodeName;
536 }
537 virtual unsigned getHashImpl() const;
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000538};
539
Chris Lattner9a515172010-02-25 02:04:40 +0000540/// CheckValueTypeMatcher - This checks to see if the current node is a
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000541/// VTSDNode with the specified type, if not it fails to match.
Chris Lattner9a515172010-02-25 02:04:40 +0000542class CheckValueTypeMatcher : public Matcher {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000543 StringRef TypeName;
544public:
Chris Lattner9a515172010-02-25 02:04:40 +0000545 CheckValueTypeMatcher(StringRef type_name)
546 : Matcher(CheckValueType), TypeName(type_name) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000547
548 StringRef getTypeName() const { return TypeName; }
549
Chris Lattner9a515172010-02-25 02:04:40 +0000550 static inline bool classof(const Matcher *N) {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000551 return N->getKind() == CheckValueType;
552 }
553
Chris Lattnerf4950d02010-02-27 06:22:57 +0000554 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
555
Chris Lattner136ab782010-02-25 06:49:58 +0000556private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000557 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000558 virtual bool isEqualImpl(const Matcher *M) const {
559 return cast<CheckValueTypeMatcher>(M)->TypeName == TypeName;
560 }
561 virtual unsigned getHashImpl() const;
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000562};
563
564
565
Chris Lattner9a515172010-02-25 02:04:40 +0000566/// CheckComplexPatMatcher - This node runs the specified ComplexPattern on
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000567/// the current node.
Chris Lattner9a515172010-02-25 02:04:40 +0000568class CheckComplexPatMatcher : public Matcher {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000569 const ComplexPattern &Pattern;
570public:
Chris Lattner9a515172010-02-25 02:04:40 +0000571 CheckComplexPatMatcher(const ComplexPattern &pattern)
572 : Matcher(CheckComplexPat), Pattern(pattern) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000573
Chris Lattnerdc429fd2010-02-17 00:31:50 +0000574 const ComplexPattern &getPattern() const { return Pattern; }
575
Chris Lattner9a515172010-02-25 02:04:40 +0000576 static inline bool classof(const Matcher *N) {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000577 return N->getKind() == CheckComplexPat;
578 }
579
Chris Lattnerf4950d02010-02-27 06:22:57 +0000580 // Not safe to move a pattern predicate past a complex pattern.
581 virtual bool isSafeToReorderWithPatternPredicate() const { return false; }
582
Chris Lattner136ab782010-02-25 06:49:58 +0000583private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000584 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000585 virtual bool isEqualImpl(const Matcher *M) const {
586 return &cast<CheckComplexPatMatcher>(M)->Pattern == &Pattern;
587 }
588 virtual unsigned getHashImpl() const {
589 return (unsigned)(intptr_t)&Pattern;
590 }
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000591};
592
Chris Lattner9a515172010-02-25 02:04:40 +0000593/// CheckAndImmMatcher - This checks to see if the current node is an 'and'
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000594/// with something equivalent to the specified immediate.
Chris Lattner9a515172010-02-25 02:04:40 +0000595class CheckAndImmMatcher : public Matcher {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000596 int64_t Value;
597public:
Chris Lattner9a515172010-02-25 02:04:40 +0000598 CheckAndImmMatcher(int64_t value)
599 : Matcher(CheckAndImm), Value(value) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000600
601 int64_t getValue() const { return Value; }
602
Chris Lattner9a515172010-02-25 02:04:40 +0000603 static inline bool classof(const Matcher *N) {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000604 return N->getKind() == CheckAndImm;
605 }
606
Chris Lattnerf4950d02010-02-27 06:22:57 +0000607 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
608
Chris Lattner136ab782010-02-25 06:49:58 +0000609private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000610 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000611 virtual bool isEqualImpl(const Matcher *M) const {
612 return cast<CheckAndImmMatcher>(M)->Value == Value;
613 }
614 virtual unsigned getHashImpl() const { return Value; }
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000615};
616
Chris Lattner9a515172010-02-25 02:04:40 +0000617/// CheckOrImmMatcher - This checks to see if the current node is an 'and'
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000618/// with something equivalent to the specified immediate.
Chris Lattner9a515172010-02-25 02:04:40 +0000619class CheckOrImmMatcher : public Matcher {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000620 int64_t Value;
621public:
Chris Lattner9a515172010-02-25 02:04:40 +0000622 CheckOrImmMatcher(int64_t value)
623 : Matcher(CheckOrImm), Value(value) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000624
625 int64_t getValue() const { return Value; }
626
Chris Lattner9a515172010-02-25 02:04:40 +0000627 static inline bool classof(const Matcher *N) {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000628 return N->getKind() == CheckOrImm;
629 }
630
Chris Lattnerf4950d02010-02-27 06:22:57 +0000631 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
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<CheckOrImmMatcher>(M)->Value == Value;
637 }
638 virtual unsigned getHashImpl() const { return Value; }
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000639};
Chris Lattner9de39482010-02-16 06:10:58 +0000640
Chris Lattner9a515172010-02-25 02:04:40 +0000641/// CheckFoldableChainNodeMatcher - This checks to see if the current node
Chris Lattnerc6dc8f62010-02-16 19:15:55 +0000642/// (which defines a chain operand) is safe to fold into a larger pattern.
Chris Lattner9a515172010-02-25 02:04:40 +0000643class CheckFoldableChainNodeMatcher : public Matcher {
Chris Lattner9de39482010-02-16 06:10:58 +0000644public:
Chris Lattner9a515172010-02-25 02:04:40 +0000645 CheckFoldableChainNodeMatcher()
646 : Matcher(CheckFoldableChainNode) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000647
Chris Lattner9a515172010-02-25 02:04:40 +0000648 static inline bool classof(const Matcher *N) {
Chris Lattnerc6dc8f62010-02-16 19:15:55 +0000649 return N->getKind() == CheckFoldableChainNode;
Chris Lattner9de39482010-02-16 06:10:58 +0000650 }
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000651
Chris Lattnerf4950d02010-02-27 06:22:57 +0000652 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
653
Chris Lattner136ab782010-02-25 06:49:58 +0000654private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000655 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000656 virtual bool isEqualImpl(const Matcher *M) const { return true; }
657 virtual unsigned getHashImpl() const { return 0; }
Chris Lattner9de39482010-02-16 06:10:58 +0000658};
659
Chris Lattner9a515172010-02-25 02:04:40 +0000660/// CheckChainCompatibleMatcher - Verify that the current node's chain
Chris Lattner283adcb2010-02-17 06:23:39 +0000661/// operand is 'compatible' with the specified recorded node's.
Chris Lattner9a515172010-02-25 02:04:40 +0000662class CheckChainCompatibleMatcher : public Matcher {
Chris Lattner283adcb2010-02-17 06:23:39 +0000663 unsigned PreviousOp;
664public:
Chris Lattner9a515172010-02-25 02:04:40 +0000665 CheckChainCompatibleMatcher(unsigned previousop)
666 : Matcher(CheckChainCompatible), PreviousOp(previousop) {}
Chris Lattner283adcb2010-02-17 06:23:39 +0000667
668 unsigned getPreviousOp() const { return PreviousOp; }
669
Chris Lattner9a515172010-02-25 02:04:40 +0000670 static inline bool classof(const Matcher *N) {
Chris Lattner283adcb2010-02-17 06:23:39 +0000671 return N->getKind() == CheckChainCompatible;
672 }
673
Chris Lattnerf4950d02010-02-27 06:22:57 +0000674 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
675
Chris Lattner136ab782010-02-25 06:49:58 +0000676private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000677 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000678 virtual bool isEqualImpl(const Matcher *M) const {
Chris Lattner50c0b362010-02-26 08:06:02 +0000679 return cast<CheckChainCompatibleMatcher>(M)->PreviousOp == PreviousOp;
Chris Lattner136ab782010-02-25 06:49:58 +0000680 }
681 virtual unsigned getHashImpl() const { return PreviousOp; }
Chris Lattner283adcb2010-02-17 06:23:39 +0000682};
683
Chris Lattner9a515172010-02-25 02:04:40 +0000684/// EmitIntegerMatcher - This creates a new TargetConstant.
685class EmitIntegerMatcher : public Matcher {
Chris Lattnerddfb5222010-02-18 22:03:03 +0000686 int64_t Val;
687 MVT::SimpleValueType VT;
688public:
Chris Lattner9a515172010-02-25 02:04:40 +0000689 EmitIntegerMatcher(int64_t val, MVT::SimpleValueType vt)
690 : Matcher(EmitInteger), Val(val), VT(vt) {}
Chris Lattner283adcb2010-02-17 06:23:39 +0000691
Chris Lattner7043e0a2010-02-19 07:49:56 +0000692 int64_t getValue() const { return Val; }
Chris Lattnerddfb5222010-02-18 22:03:03 +0000693 MVT::SimpleValueType getVT() const { return VT; }
694
Chris Lattner9a515172010-02-25 02:04:40 +0000695 static inline bool classof(const Matcher *N) {
Chris Lattnerddfb5222010-02-18 22:03:03 +0000696 return N->getKind() == EmitInteger;
697 }
698
Chris Lattner136ab782010-02-25 06:49:58 +0000699private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000700 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000701 virtual bool isEqualImpl(const Matcher *M) const {
702 return cast<EmitIntegerMatcher>(M)->Val == Val &&
703 cast<EmitIntegerMatcher>(M)->VT == VT;
704 }
705 virtual unsigned getHashImpl() const { return (Val << 4) | VT; }
Chris Lattnerddfb5222010-02-18 22:03:03 +0000706};
Chris Lattner3163ca52010-02-21 03:22:59 +0000707
Chris Lattner9a515172010-02-25 02:04:40 +0000708/// EmitStringIntegerMatcher - A target constant whose value is represented
Chris Lattner3163ca52010-02-21 03:22:59 +0000709/// by a string.
Chris Lattner9a515172010-02-25 02:04:40 +0000710class EmitStringIntegerMatcher : public Matcher {
Chris Lattner3163ca52010-02-21 03:22:59 +0000711 std::string Val;
712 MVT::SimpleValueType VT;
713public:
Chris Lattner9a515172010-02-25 02:04:40 +0000714 EmitStringIntegerMatcher(const std::string &val, MVT::SimpleValueType vt)
715 : Matcher(EmitStringInteger), Val(val), VT(vt) {}
Chris Lattner3163ca52010-02-21 03:22:59 +0000716
717 const std::string &getValue() const { return Val; }
718 MVT::SimpleValueType getVT() const { return VT; }
719
Chris Lattner9a515172010-02-25 02:04:40 +0000720 static inline bool classof(const Matcher *N) {
Chris Lattner3163ca52010-02-21 03:22:59 +0000721 return N->getKind() == EmitStringInteger;
722 }
723
Chris Lattner136ab782010-02-25 06:49:58 +0000724private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000725 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000726 virtual bool isEqualImpl(const Matcher *M) const {
727 return cast<EmitStringIntegerMatcher>(M)->Val == Val &&
728 cast<EmitStringIntegerMatcher>(M)->VT == VT;
729 }
730 virtual unsigned getHashImpl() const;
Chris Lattner3163ca52010-02-21 03:22:59 +0000731};
Chris Lattnerddfb5222010-02-18 22:03:03 +0000732
Chris Lattner9a515172010-02-25 02:04:40 +0000733/// EmitRegisterMatcher - This creates a new TargetConstant.
734class EmitRegisterMatcher : public Matcher {
Chris Lattnerddfb5222010-02-18 22:03:03 +0000735 /// Reg - The def for the register that we're emitting. If this is null, then
736 /// this is a reference to zero_reg.
737 Record *Reg;
738 MVT::SimpleValueType VT;
739public:
Chris Lattner9a515172010-02-25 02:04:40 +0000740 EmitRegisterMatcher(Record *reg, MVT::SimpleValueType vt)
741 : Matcher(EmitRegister), Reg(reg), VT(vt) {}
Chris Lattnerddfb5222010-02-18 22:03:03 +0000742
743 Record *getReg() const { return Reg; }
744 MVT::SimpleValueType getVT() const { return VT; }
745
Chris Lattner9a515172010-02-25 02:04:40 +0000746 static inline bool classof(const Matcher *N) {
Chris Lattnerddfb5222010-02-18 22:03:03 +0000747 return N->getKind() == EmitRegister;
748 }
749
Chris Lattner136ab782010-02-25 06:49:58 +0000750private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000751 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000752 virtual bool isEqualImpl(const Matcher *M) const {
753 return cast<EmitRegisterMatcher>(M)->Reg == Reg &&
754 cast<EmitRegisterMatcher>(M)->VT == VT;
755 }
756 virtual unsigned getHashImpl() const {
757 return ((unsigned)(intptr_t)Reg) << 4 | VT;
758 }
Chris Lattnerddfb5222010-02-18 22:03:03 +0000759};
Chris Lattner3163ca52010-02-21 03:22:59 +0000760
Chris Lattner9a515172010-02-25 02:04:40 +0000761/// EmitConvertToTargetMatcher - Emit an operation that reads a specified
Chris Lattner3163ca52010-02-21 03:22:59 +0000762/// recorded node and converts it from being a ISD::Constant to
763/// ISD::TargetConstant, likewise for ConstantFP.
Chris Lattner9a515172010-02-25 02:04:40 +0000764class EmitConvertToTargetMatcher : public Matcher {
Chris Lattner3163ca52010-02-21 03:22:59 +0000765 unsigned Slot;
766public:
Chris Lattner9a515172010-02-25 02:04:40 +0000767 EmitConvertToTargetMatcher(unsigned slot)
768 : Matcher(EmitConvertToTarget), Slot(slot) {}
Chris Lattner3163ca52010-02-21 03:22:59 +0000769
770 unsigned getSlot() const { return Slot; }
771
Chris Lattner9a515172010-02-25 02:04:40 +0000772 static inline bool classof(const Matcher *N) {
Chris Lattner3163ca52010-02-21 03:22:59 +0000773 return N->getKind() == EmitConvertToTarget;
774 }
775
Chris Lattner136ab782010-02-25 06:49:58 +0000776private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000777 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000778 virtual bool isEqualImpl(const Matcher *M) const {
779 return cast<EmitConvertToTargetMatcher>(M)->Slot == Slot;
780 }
781 virtual unsigned getHashImpl() const { return Slot; }
Chris Lattner3163ca52010-02-21 03:22:59 +0000782};
783
Chris Lattner9a515172010-02-25 02:04:40 +0000784/// EmitMergeInputChainsMatcher - Emit a node that merges a list of input
Chris Lattner3163ca52010-02-21 03:22:59 +0000785/// chains together with a token factor. The list of nodes are the nodes in the
786/// matched pattern that have chain input/outputs. This node adds all input
787/// chains of these nodes if they are not themselves a node in the pattern.
Chris Lattner9a515172010-02-25 02:04:40 +0000788class EmitMergeInputChainsMatcher : public Matcher {
Chris Lattner3163ca52010-02-21 03:22:59 +0000789 SmallVector<unsigned, 3> ChainNodes;
790public:
Chris Lattner9a515172010-02-25 02:04:40 +0000791 EmitMergeInputChainsMatcher(const unsigned *nodes, unsigned NumNodes)
792 : Matcher(EmitMergeInputChains), ChainNodes(nodes, nodes+NumNodes) {}
Chris Lattner3163ca52010-02-21 03:22:59 +0000793
794 unsigned getNumNodes() const { return ChainNodes.size(); }
795
796 unsigned getNode(unsigned i) const {
797 assert(i < ChainNodes.size());
798 return ChainNodes[i];
799 }
800
Chris Lattner9a515172010-02-25 02:04:40 +0000801 static inline bool classof(const Matcher *N) {
Chris Lattner3163ca52010-02-21 03:22:59 +0000802 return N->getKind() == EmitMergeInputChains;
803 }
804
Chris Lattner136ab782010-02-25 06:49:58 +0000805private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000806 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000807 virtual bool isEqualImpl(const Matcher *M) const {
808 return cast<EmitMergeInputChainsMatcher>(M)->ChainNodes == ChainNodes;
809 }
810 virtual unsigned getHashImpl() const;
Chris Lattner3163ca52010-02-21 03:22:59 +0000811};
812
Chris Lattner9a515172010-02-25 02:04:40 +0000813/// EmitCopyToRegMatcher - Emit a CopyToReg node from a value to a physreg,
Chris Lattner3163ca52010-02-21 03:22:59 +0000814/// pushing the chain and flag results.
815///
Chris Lattner9a515172010-02-25 02:04:40 +0000816class EmitCopyToRegMatcher : public Matcher {
Chris Lattner3163ca52010-02-21 03:22:59 +0000817 unsigned SrcSlot; // Value to copy into the physreg.
818 Record *DestPhysReg;
819public:
Chris Lattner9a515172010-02-25 02:04:40 +0000820 EmitCopyToRegMatcher(unsigned srcSlot, Record *destPhysReg)
821 : Matcher(EmitCopyToReg), SrcSlot(srcSlot), DestPhysReg(destPhysReg) {}
Chris Lattner3163ca52010-02-21 03:22:59 +0000822
823 unsigned getSrcSlot() const { return SrcSlot; }
824 Record *getDestPhysReg() const { return DestPhysReg; }
825
Chris Lattner9a515172010-02-25 02:04:40 +0000826 static inline bool classof(const Matcher *N) {
Chris Lattner3163ca52010-02-21 03:22:59 +0000827 return N->getKind() == EmitCopyToReg;
828 }
829
Chris Lattner136ab782010-02-25 06:49:58 +0000830private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000831 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000832 virtual bool isEqualImpl(const Matcher *M) const {
833 return cast<EmitCopyToRegMatcher>(M)->SrcSlot == SrcSlot &&
834 cast<EmitCopyToRegMatcher>(M)->DestPhysReg == DestPhysReg;
835 }
836 virtual unsigned getHashImpl() const {
837 return SrcSlot ^ ((unsigned)(intptr_t)DestPhysReg << 4);
838 }
Chris Lattner3163ca52010-02-21 03:22:59 +0000839};
840
841
842
Chris Lattner9a515172010-02-25 02:04:40 +0000843/// EmitNodeXFormMatcher - Emit an operation that runs an SDNodeXForm on a
Chris Lattner3163ca52010-02-21 03:22:59 +0000844/// recorded node and records the result.
Chris Lattner9a515172010-02-25 02:04:40 +0000845class EmitNodeXFormMatcher : public Matcher {
Chris Lattner3163ca52010-02-21 03:22:59 +0000846 unsigned Slot;
847 Record *NodeXForm;
848public:
Chris Lattner9a515172010-02-25 02:04:40 +0000849 EmitNodeXFormMatcher(unsigned slot, Record *nodeXForm)
850 : Matcher(EmitNodeXForm), Slot(slot), NodeXForm(nodeXForm) {}
Chris Lattner3163ca52010-02-21 03:22:59 +0000851
852 unsigned getSlot() const { return Slot; }
853 Record *getNodeXForm() const { return NodeXForm; }
854
Chris Lattner9a515172010-02-25 02:04:40 +0000855 static inline bool classof(const Matcher *N) {
Chris Lattner3163ca52010-02-21 03:22:59 +0000856 return N->getKind() == EmitNodeXForm;
857 }
858
Chris Lattner136ab782010-02-25 06:49:58 +0000859private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000860 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000861 virtual bool isEqualImpl(const Matcher *M) const {
862 return cast<EmitNodeXFormMatcher>(M)->Slot == Slot &&
863 cast<EmitNodeXFormMatcher>(M)->NodeXForm == NodeXForm;
864 }
865 virtual unsigned getHashImpl() const {
866 return Slot ^ ((unsigned)(intptr_t)NodeXForm << 4);
867 }
Chris Lattner3163ca52010-02-21 03:22:59 +0000868};
Chris Lattnerddfb5222010-02-18 22:03:03 +0000869
Chris Lattner9a515172010-02-25 02:04:40 +0000870/// EmitNodeMatcher - This signals a successful match and generates a node.
871class EmitNodeMatcher : public Matcher {
Chris Lattner3163ca52010-02-21 03:22:59 +0000872 std::string OpcodeName;
873 const SmallVector<MVT::SimpleValueType, 3> VTs;
874 const SmallVector<unsigned, 6> Operands;
875 bool HasChain, HasFlag, HasMemRefs;
Chris Lattnerddfb5222010-02-18 22:03:03 +0000876
Chris Lattner3163ca52010-02-21 03:22:59 +0000877 /// NumFixedArityOperands - If this is a fixed arity node, this is set to -1.
878 /// If this is a varidic node, this is set to the number of fixed arity
879 /// operands in the root of the pattern. The rest are appended to this node.
880 int NumFixedArityOperands;
881public:
Chris Lattner9a515172010-02-25 02:04:40 +0000882 EmitNodeMatcher(const std::string &opcodeName,
Chris Lattner136ab782010-02-25 06:49:58 +0000883 const MVT::SimpleValueType *vts, unsigned numvts,
884 const unsigned *operands, unsigned numops,
885 bool hasChain, bool hasFlag, bool hasmemrefs,
886 int numfixedarityoperands)
Chris Lattner9a515172010-02-25 02:04:40 +0000887 : Matcher(EmitNode), OpcodeName(opcodeName),
Chris Lattner3163ca52010-02-21 03:22:59 +0000888 VTs(vts, vts+numvts), Operands(operands, operands+numops),
889 HasChain(hasChain), HasFlag(hasFlag), HasMemRefs(hasmemrefs),
890 NumFixedArityOperands(numfixedarityoperands) {}
891
892 const std::string &getOpcodeName() const { return OpcodeName; }
893
894 unsigned getNumVTs() const { return VTs.size(); }
895 MVT::SimpleValueType getVT(unsigned i) const {
896 assert(i < VTs.size());
897 return VTs[i];
898 }
899
900 unsigned getNumOperands() const { return Operands.size(); }
901 unsigned getOperand(unsigned i) const {
902 assert(i < Operands.size());
903 return Operands[i];
904 }
905
906 bool hasChain() const { return HasChain; }
907 bool hasFlag() const { return HasFlag; }
908 bool hasMemRefs() const { return HasMemRefs; }
909 int getNumFixedArityOperands() const { return NumFixedArityOperands; }
Chris Lattnerddfb5222010-02-18 22:03:03 +0000910
Chris Lattner9a515172010-02-25 02:04:40 +0000911 static inline bool classof(const Matcher *N) {
Chris Lattnerddfb5222010-02-18 22:03:03 +0000912 return N->getKind() == EmitNode;
913 }
914
Chris Lattner136ab782010-02-25 06:49:58 +0000915private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000916 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000917 virtual bool isEqualImpl(const Matcher *M) const;
918 virtual unsigned getHashImpl() const;
Chris Lattnerddfb5222010-02-18 22:03:03 +0000919};
Chris Lattnerb085ea12010-02-21 06:03:07 +0000920
Chris Lattner9a515172010-02-25 02:04:40 +0000921/// MarkFlagResultsMatcher - This node indicates which non-root nodes in the
922/// pattern produce flags. This allows CompleteMatchMatcher to update them
Chris Lattner69f60c82010-02-24 05:33:42 +0000923/// with the output flag of the resultant code.
Chris Lattner9a515172010-02-25 02:04:40 +0000924class MarkFlagResultsMatcher : public Matcher {
Chris Lattner69f60c82010-02-24 05:33:42 +0000925 SmallVector<unsigned, 3> FlagResultNodes;
926public:
Chris Lattner9a515172010-02-25 02:04:40 +0000927 MarkFlagResultsMatcher(const unsigned *nodes, unsigned NumNodes)
928 : Matcher(MarkFlagResults), FlagResultNodes(nodes, nodes+NumNodes) {}
Chris Lattner69f60c82010-02-24 05:33:42 +0000929
930 unsigned getNumNodes() const { return FlagResultNodes.size(); }
931
932 unsigned getNode(unsigned i) const {
933 assert(i < FlagResultNodes.size());
934 return FlagResultNodes[i];
935 }
936
Chris Lattner9a515172010-02-25 02:04:40 +0000937 static inline bool classof(const Matcher *N) {
Chris Lattner69f60c82010-02-24 05:33:42 +0000938 return N->getKind() == MarkFlagResults;
939 }
940
Chris Lattner136ab782010-02-25 06:49:58 +0000941private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000942 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000943 virtual bool isEqualImpl(const Matcher *M) const {
944 return cast<MarkFlagResultsMatcher>(M)->FlagResultNodes == FlagResultNodes;
945 }
946 virtual unsigned getHashImpl() const;
Chris Lattner69f60c82010-02-24 05:33:42 +0000947};
948
Chris Lattner9a515172010-02-25 02:04:40 +0000949/// CompleteMatchMatcher - Complete a match by replacing the results of the
Chris Lattnerb085ea12010-02-21 06:03:07 +0000950/// pattern with the newly generated nodes. This also prints a comment
951/// indicating the source and dest patterns.
Chris Lattner9a515172010-02-25 02:04:40 +0000952class CompleteMatchMatcher : public Matcher {
Chris Lattnerb085ea12010-02-21 06:03:07 +0000953 SmallVector<unsigned, 2> Results;
Chris Lattner3163ca52010-02-21 03:22:59 +0000954 const PatternToMatch &Pattern;
955public:
Chris Lattner9a515172010-02-25 02:04:40 +0000956 CompleteMatchMatcher(const unsigned *results, unsigned numresults,
Chris Lattnerb085ea12010-02-21 06:03:07 +0000957 const PatternToMatch &pattern)
Chris Lattner9a515172010-02-25 02:04:40 +0000958 : Matcher(CompleteMatch), Results(results, results+numresults),
Chris Lattnerb085ea12010-02-21 06:03:07 +0000959 Pattern(pattern) {}
960
961 unsigned getNumResults() const { return Results.size(); }
962 unsigned getResult(unsigned R) const { return Results[R]; }
Chris Lattner3163ca52010-02-21 03:22:59 +0000963 const PatternToMatch &getPattern() const { return Pattern; }
964
Chris Lattner9a515172010-02-25 02:04:40 +0000965 static inline bool classof(const Matcher *N) {
Chris Lattnerb085ea12010-02-21 06:03:07 +0000966 return N->getKind() == CompleteMatch;
Chris Lattner3163ca52010-02-21 03:22:59 +0000967 }
968
Chris Lattner136ab782010-02-25 06:49:58 +0000969private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000970 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000971 virtual bool isEqualImpl(const Matcher *M) const {
972 return cast<CompleteMatchMatcher>(M)->Results == Results &&
973 &cast<CompleteMatchMatcher>(M)->Pattern == &Pattern;
974 }
975 virtual unsigned getHashImpl() const;
Chris Lattner3163ca52010-02-21 03:22:59 +0000976};
Chris Lattnerddfb5222010-02-18 22:03:03 +0000977
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000978} // end namespace llvm
979
980#endif