blob: f983f5a133b34d8f20083e613d07921554c8f23a [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 Lattner086af322010-02-27 08:11:15 +0000514 virtual bool isContradictoryImpl(const Matcher *M) const;
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000515};
516
Chris Lattner9a515172010-02-25 02:04:40 +0000517/// CheckCondCodeMatcher - This checks to see if the current node is a
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000518/// CondCodeSDNode with the specified condition, if not it fails to match.
Chris Lattner9a515172010-02-25 02:04:40 +0000519class CheckCondCodeMatcher : public Matcher {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000520 StringRef CondCodeName;
521public:
Chris Lattner9a515172010-02-25 02:04:40 +0000522 CheckCondCodeMatcher(StringRef condcodename)
523 : Matcher(CheckCondCode), CondCodeName(condcodename) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000524
525 StringRef getCondCodeName() const { return CondCodeName; }
526
Chris Lattner9a515172010-02-25 02:04:40 +0000527 static inline bool classof(const Matcher *N) {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000528 return N->getKind() == CheckCondCode;
529 }
530
Chris Lattnerf4950d02010-02-27 06:22:57 +0000531 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
532
Chris Lattner136ab782010-02-25 06:49:58 +0000533private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000534 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000535 virtual bool isEqualImpl(const Matcher *M) const {
536 return cast<CheckCondCodeMatcher>(M)->CondCodeName == CondCodeName;
537 }
538 virtual unsigned getHashImpl() const;
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000539};
540
Chris Lattner9a515172010-02-25 02:04:40 +0000541/// CheckValueTypeMatcher - This checks to see if the current node is a
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000542/// VTSDNode with the specified type, if not it fails to match.
Chris Lattner9a515172010-02-25 02:04:40 +0000543class CheckValueTypeMatcher : public Matcher {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000544 StringRef TypeName;
545public:
Chris Lattner9a515172010-02-25 02:04:40 +0000546 CheckValueTypeMatcher(StringRef type_name)
547 : Matcher(CheckValueType), TypeName(type_name) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000548
549 StringRef getTypeName() const { return TypeName; }
550
Chris Lattner9a515172010-02-25 02:04:40 +0000551 static inline bool classof(const Matcher *N) {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000552 return N->getKind() == CheckValueType;
553 }
554
Chris Lattnerf4950d02010-02-27 06:22:57 +0000555 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
556
Chris Lattner136ab782010-02-25 06:49:58 +0000557private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000558 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000559 virtual bool isEqualImpl(const Matcher *M) const {
560 return cast<CheckValueTypeMatcher>(M)->TypeName == TypeName;
561 }
562 virtual unsigned getHashImpl() const;
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000563};
564
565
566
Chris Lattner9a515172010-02-25 02:04:40 +0000567/// CheckComplexPatMatcher - This node runs the specified ComplexPattern on
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000568/// the current node.
Chris Lattner9a515172010-02-25 02:04:40 +0000569class CheckComplexPatMatcher : public Matcher {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000570 const ComplexPattern &Pattern;
571public:
Chris Lattner9a515172010-02-25 02:04:40 +0000572 CheckComplexPatMatcher(const ComplexPattern &pattern)
573 : Matcher(CheckComplexPat), Pattern(pattern) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000574
Chris Lattnerdc429fd2010-02-17 00:31:50 +0000575 const ComplexPattern &getPattern() const { return Pattern; }
576
Chris Lattner9a515172010-02-25 02:04:40 +0000577 static inline bool classof(const Matcher *N) {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000578 return N->getKind() == CheckComplexPat;
579 }
580
Chris Lattnerf4950d02010-02-27 06:22:57 +0000581 // Not safe to move a pattern predicate past a complex pattern.
582 virtual bool isSafeToReorderWithPatternPredicate() const { return false; }
583
Chris Lattner136ab782010-02-25 06:49:58 +0000584private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000585 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000586 virtual bool isEqualImpl(const Matcher *M) const {
587 return &cast<CheckComplexPatMatcher>(M)->Pattern == &Pattern;
588 }
589 virtual unsigned getHashImpl() const {
590 return (unsigned)(intptr_t)&Pattern;
591 }
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000592};
593
Chris Lattner9a515172010-02-25 02:04:40 +0000594/// CheckAndImmMatcher - This checks to see if the current node is an 'and'
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000595/// with something equivalent to the specified immediate.
Chris Lattner9a515172010-02-25 02:04:40 +0000596class CheckAndImmMatcher : public Matcher {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000597 int64_t Value;
598public:
Chris Lattner9a515172010-02-25 02:04:40 +0000599 CheckAndImmMatcher(int64_t value)
600 : Matcher(CheckAndImm), Value(value) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000601
602 int64_t getValue() const { return Value; }
603
Chris Lattner9a515172010-02-25 02:04:40 +0000604 static inline bool classof(const Matcher *N) {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000605 return N->getKind() == CheckAndImm;
606 }
607
Chris Lattnerf4950d02010-02-27 06:22:57 +0000608 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
609
Chris Lattner136ab782010-02-25 06:49:58 +0000610private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000611 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000612 virtual bool isEqualImpl(const Matcher *M) const {
613 return cast<CheckAndImmMatcher>(M)->Value == Value;
614 }
615 virtual unsigned getHashImpl() const { return Value; }
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000616};
617
Chris Lattner9a515172010-02-25 02:04:40 +0000618/// CheckOrImmMatcher - This checks to see if the current node is an 'and'
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000619/// with something equivalent to the specified immediate.
Chris Lattner9a515172010-02-25 02:04:40 +0000620class CheckOrImmMatcher : public Matcher {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000621 int64_t Value;
622public:
Chris Lattner9a515172010-02-25 02:04:40 +0000623 CheckOrImmMatcher(int64_t value)
624 : Matcher(CheckOrImm), Value(value) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000625
626 int64_t getValue() const { return Value; }
627
Chris Lattner9a515172010-02-25 02:04:40 +0000628 static inline bool classof(const Matcher *N) {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000629 return N->getKind() == CheckOrImm;
630 }
631
Chris Lattnerf4950d02010-02-27 06:22:57 +0000632 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
633
Chris Lattner136ab782010-02-25 06:49:58 +0000634private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000635 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000636 virtual bool isEqualImpl(const Matcher *M) const {
637 return cast<CheckOrImmMatcher>(M)->Value == Value;
638 }
639 virtual unsigned getHashImpl() const { return Value; }
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000640};
Chris Lattner9de39482010-02-16 06:10:58 +0000641
Chris Lattner9a515172010-02-25 02:04:40 +0000642/// CheckFoldableChainNodeMatcher - This checks to see if the current node
Chris Lattnerc6dc8f62010-02-16 19:15:55 +0000643/// (which defines a chain operand) is safe to fold into a larger pattern.
Chris Lattner9a515172010-02-25 02:04:40 +0000644class CheckFoldableChainNodeMatcher : public Matcher {
Chris Lattner9de39482010-02-16 06:10:58 +0000645public:
Chris Lattner9a515172010-02-25 02:04:40 +0000646 CheckFoldableChainNodeMatcher()
647 : Matcher(CheckFoldableChainNode) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000648
Chris Lattner9a515172010-02-25 02:04:40 +0000649 static inline bool classof(const Matcher *N) {
Chris Lattnerc6dc8f62010-02-16 19:15:55 +0000650 return N->getKind() == CheckFoldableChainNode;
Chris Lattner9de39482010-02-16 06:10:58 +0000651 }
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000652
Chris Lattnerf4950d02010-02-27 06:22:57 +0000653 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
654
Chris Lattner136ab782010-02-25 06:49:58 +0000655private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000656 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000657 virtual bool isEqualImpl(const Matcher *M) const { return true; }
658 virtual unsigned getHashImpl() const { return 0; }
Chris Lattner9de39482010-02-16 06:10:58 +0000659};
660
Chris Lattner9a515172010-02-25 02:04:40 +0000661/// CheckChainCompatibleMatcher - Verify that the current node's chain
Chris Lattner283adcb2010-02-17 06:23:39 +0000662/// operand is 'compatible' with the specified recorded node's.
Chris Lattner9a515172010-02-25 02:04:40 +0000663class CheckChainCompatibleMatcher : public Matcher {
Chris Lattner283adcb2010-02-17 06:23:39 +0000664 unsigned PreviousOp;
665public:
Chris Lattner9a515172010-02-25 02:04:40 +0000666 CheckChainCompatibleMatcher(unsigned previousop)
667 : Matcher(CheckChainCompatible), PreviousOp(previousop) {}
Chris Lattner283adcb2010-02-17 06:23:39 +0000668
669 unsigned getPreviousOp() const { return PreviousOp; }
670
Chris Lattner9a515172010-02-25 02:04:40 +0000671 static inline bool classof(const Matcher *N) {
Chris Lattner283adcb2010-02-17 06:23:39 +0000672 return N->getKind() == CheckChainCompatible;
673 }
674
Chris Lattnerf4950d02010-02-27 06:22:57 +0000675 virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
676
Chris Lattner136ab782010-02-25 06:49:58 +0000677private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000678 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000679 virtual bool isEqualImpl(const Matcher *M) const {
Chris Lattner50c0b362010-02-26 08:06:02 +0000680 return cast<CheckChainCompatibleMatcher>(M)->PreviousOp == PreviousOp;
Chris Lattner136ab782010-02-25 06:49:58 +0000681 }
682 virtual unsigned getHashImpl() const { return PreviousOp; }
Chris Lattner283adcb2010-02-17 06:23:39 +0000683};
684
Chris Lattner9a515172010-02-25 02:04:40 +0000685/// EmitIntegerMatcher - This creates a new TargetConstant.
686class EmitIntegerMatcher : public Matcher {
Chris Lattnerddfb5222010-02-18 22:03:03 +0000687 int64_t Val;
688 MVT::SimpleValueType VT;
689public:
Chris Lattner9a515172010-02-25 02:04:40 +0000690 EmitIntegerMatcher(int64_t val, MVT::SimpleValueType vt)
691 : Matcher(EmitInteger), Val(val), VT(vt) {}
Chris Lattner283adcb2010-02-17 06:23:39 +0000692
Chris Lattner7043e0a2010-02-19 07:49:56 +0000693 int64_t getValue() const { return Val; }
Chris Lattnerddfb5222010-02-18 22:03:03 +0000694 MVT::SimpleValueType getVT() const { return VT; }
695
Chris Lattner9a515172010-02-25 02:04:40 +0000696 static inline bool classof(const Matcher *N) {
Chris Lattnerddfb5222010-02-18 22:03:03 +0000697 return N->getKind() == EmitInteger;
698 }
699
Chris Lattner136ab782010-02-25 06:49:58 +0000700private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000701 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000702 virtual bool isEqualImpl(const Matcher *M) const {
703 return cast<EmitIntegerMatcher>(M)->Val == Val &&
704 cast<EmitIntegerMatcher>(M)->VT == VT;
705 }
706 virtual unsigned getHashImpl() const { return (Val << 4) | VT; }
Chris Lattnerddfb5222010-02-18 22:03:03 +0000707};
Chris Lattner3163ca52010-02-21 03:22:59 +0000708
Chris Lattner9a515172010-02-25 02:04:40 +0000709/// EmitStringIntegerMatcher - A target constant whose value is represented
Chris Lattner3163ca52010-02-21 03:22:59 +0000710/// by a string.
Chris Lattner9a515172010-02-25 02:04:40 +0000711class EmitStringIntegerMatcher : public Matcher {
Chris Lattner3163ca52010-02-21 03:22:59 +0000712 std::string Val;
713 MVT::SimpleValueType VT;
714public:
Chris Lattner9a515172010-02-25 02:04:40 +0000715 EmitStringIntegerMatcher(const std::string &val, MVT::SimpleValueType vt)
716 : Matcher(EmitStringInteger), Val(val), VT(vt) {}
Chris Lattner3163ca52010-02-21 03:22:59 +0000717
718 const std::string &getValue() const { return Val; }
719 MVT::SimpleValueType getVT() const { return VT; }
720
Chris Lattner9a515172010-02-25 02:04:40 +0000721 static inline bool classof(const Matcher *N) {
Chris Lattner3163ca52010-02-21 03:22:59 +0000722 return N->getKind() == EmitStringInteger;
723 }
724
Chris Lattner136ab782010-02-25 06:49:58 +0000725private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000726 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000727 virtual bool isEqualImpl(const Matcher *M) const {
728 return cast<EmitStringIntegerMatcher>(M)->Val == Val &&
729 cast<EmitStringIntegerMatcher>(M)->VT == VT;
730 }
731 virtual unsigned getHashImpl() const;
Chris Lattner3163ca52010-02-21 03:22:59 +0000732};
Chris Lattnerddfb5222010-02-18 22:03:03 +0000733
Chris Lattner9a515172010-02-25 02:04:40 +0000734/// EmitRegisterMatcher - This creates a new TargetConstant.
735class EmitRegisterMatcher : public Matcher {
Chris Lattnerddfb5222010-02-18 22:03:03 +0000736 /// Reg - The def for the register that we're emitting. If this is null, then
737 /// this is a reference to zero_reg.
738 Record *Reg;
739 MVT::SimpleValueType VT;
740public:
Chris Lattner9a515172010-02-25 02:04:40 +0000741 EmitRegisterMatcher(Record *reg, MVT::SimpleValueType vt)
742 : Matcher(EmitRegister), Reg(reg), VT(vt) {}
Chris Lattnerddfb5222010-02-18 22:03:03 +0000743
744 Record *getReg() const { return Reg; }
745 MVT::SimpleValueType getVT() const { return VT; }
746
Chris Lattner9a515172010-02-25 02:04:40 +0000747 static inline bool classof(const Matcher *N) {
Chris Lattnerddfb5222010-02-18 22:03:03 +0000748 return N->getKind() == EmitRegister;
749 }
750
Chris Lattner136ab782010-02-25 06:49:58 +0000751private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000752 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000753 virtual bool isEqualImpl(const Matcher *M) const {
754 return cast<EmitRegisterMatcher>(M)->Reg == Reg &&
755 cast<EmitRegisterMatcher>(M)->VT == VT;
756 }
757 virtual unsigned getHashImpl() const {
758 return ((unsigned)(intptr_t)Reg) << 4 | VT;
759 }
Chris Lattnerddfb5222010-02-18 22:03:03 +0000760};
Chris Lattner3163ca52010-02-21 03:22:59 +0000761
Chris Lattner9a515172010-02-25 02:04:40 +0000762/// EmitConvertToTargetMatcher - Emit an operation that reads a specified
Chris Lattner3163ca52010-02-21 03:22:59 +0000763/// recorded node and converts it from being a ISD::Constant to
764/// ISD::TargetConstant, likewise for ConstantFP.
Chris Lattner9a515172010-02-25 02:04:40 +0000765class EmitConvertToTargetMatcher : public Matcher {
Chris Lattner3163ca52010-02-21 03:22:59 +0000766 unsigned Slot;
767public:
Chris Lattner9a515172010-02-25 02:04:40 +0000768 EmitConvertToTargetMatcher(unsigned slot)
769 : Matcher(EmitConvertToTarget), Slot(slot) {}
Chris Lattner3163ca52010-02-21 03:22:59 +0000770
771 unsigned getSlot() const { return Slot; }
772
Chris Lattner9a515172010-02-25 02:04:40 +0000773 static inline bool classof(const Matcher *N) {
Chris Lattner3163ca52010-02-21 03:22:59 +0000774 return N->getKind() == EmitConvertToTarget;
775 }
776
Chris Lattner136ab782010-02-25 06:49:58 +0000777private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000778 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000779 virtual bool isEqualImpl(const Matcher *M) const {
780 return cast<EmitConvertToTargetMatcher>(M)->Slot == Slot;
781 }
782 virtual unsigned getHashImpl() const { return Slot; }
Chris Lattner3163ca52010-02-21 03:22:59 +0000783};
784
Chris Lattner9a515172010-02-25 02:04:40 +0000785/// EmitMergeInputChainsMatcher - Emit a node that merges a list of input
Chris Lattner3163ca52010-02-21 03:22:59 +0000786/// chains together with a token factor. The list of nodes are the nodes in the
787/// matched pattern that have chain input/outputs. This node adds all input
788/// chains of these nodes if they are not themselves a node in the pattern.
Chris Lattner9a515172010-02-25 02:04:40 +0000789class EmitMergeInputChainsMatcher : public Matcher {
Chris Lattner3163ca52010-02-21 03:22:59 +0000790 SmallVector<unsigned, 3> ChainNodes;
791public:
Chris Lattner9a515172010-02-25 02:04:40 +0000792 EmitMergeInputChainsMatcher(const unsigned *nodes, unsigned NumNodes)
793 : Matcher(EmitMergeInputChains), ChainNodes(nodes, nodes+NumNodes) {}
Chris Lattner3163ca52010-02-21 03:22:59 +0000794
795 unsigned getNumNodes() const { return ChainNodes.size(); }
796
797 unsigned getNode(unsigned i) const {
798 assert(i < ChainNodes.size());
799 return ChainNodes[i];
800 }
801
Chris Lattner9a515172010-02-25 02:04:40 +0000802 static inline bool classof(const Matcher *N) {
Chris Lattner3163ca52010-02-21 03:22:59 +0000803 return N->getKind() == EmitMergeInputChains;
804 }
805
Chris Lattner136ab782010-02-25 06:49:58 +0000806private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000807 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000808 virtual bool isEqualImpl(const Matcher *M) const {
809 return cast<EmitMergeInputChainsMatcher>(M)->ChainNodes == ChainNodes;
810 }
811 virtual unsigned getHashImpl() const;
Chris Lattner3163ca52010-02-21 03:22:59 +0000812};
813
Chris Lattner9a515172010-02-25 02:04:40 +0000814/// EmitCopyToRegMatcher - Emit a CopyToReg node from a value to a physreg,
Chris Lattner3163ca52010-02-21 03:22:59 +0000815/// pushing the chain and flag results.
816///
Chris Lattner9a515172010-02-25 02:04:40 +0000817class EmitCopyToRegMatcher : public Matcher {
Chris Lattner3163ca52010-02-21 03:22:59 +0000818 unsigned SrcSlot; // Value to copy into the physreg.
819 Record *DestPhysReg;
820public:
Chris Lattner9a515172010-02-25 02:04:40 +0000821 EmitCopyToRegMatcher(unsigned srcSlot, Record *destPhysReg)
822 : Matcher(EmitCopyToReg), SrcSlot(srcSlot), DestPhysReg(destPhysReg) {}
Chris Lattner3163ca52010-02-21 03:22:59 +0000823
824 unsigned getSrcSlot() const { return SrcSlot; }
825 Record *getDestPhysReg() const { return DestPhysReg; }
826
Chris Lattner9a515172010-02-25 02:04:40 +0000827 static inline bool classof(const Matcher *N) {
Chris Lattner3163ca52010-02-21 03:22:59 +0000828 return N->getKind() == EmitCopyToReg;
829 }
830
Chris Lattner136ab782010-02-25 06:49:58 +0000831private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000832 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000833 virtual bool isEqualImpl(const Matcher *M) const {
834 return cast<EmitCopyToRegMatcher>(M)->SrcSlot == SrcSlot &&
835 cast<EmitCopyToRegMatcher>(M)->DestPhysReg == DestPhysReg;
836 }
837 virtual unsigned getHashImpl() const {
838 return SrcSlot ^ ((unsigned)(intptr_t)DestPhysReg << 4);
839 }
Chris Lattner3163ca52010-02-21 03:22:59 +0000840};
841
842
843
Chris Lattner9a515172010-02-25 02:04:40 +0000844/// EmitNodeXFormMatcher - Emit an operation that runs an SDNodeXForm on a
Chris Lattner3163ca52010-02-21 03:22:59 +0000845/// recorded node and records the result.
Chris Lattner9a515172010-02-25 02:04:40 +0000846class EmitNodeXFormMatcher : public Matcher {
Chris Lattner3163ca52010-02-21 03:22:59 +0000847 unsigned Slot;
848 Record *NodeXForm;
849public:
Chris Lattner9a515172010-02-25 02:04:40 +0000850 EmitNodeXFormMatcher(unsigned slot, Record *nodeXForm)
851 : Matcher(EmitNodeXForm), Slot(slot), NodeXForm(nodeXForm) {}
Chris Lattner3163ca52010-02-21 03:22:59 +0000852
853 unsigned getSlot() const { return Slot; }
854 Record *getNodeXForm() const { return NodeXForm; }
855
Chris Lattner9a515172010-02-25 02:04:40 +0000856 static inline bool classof(const Matcher *N) {
Chris Lattner3163ca52010-02-21 03:22:59 +0000857 return N->getKind() == EmitNodeXForm;
858 }
859
Chris Lattner136ab782010-02-25 06:49:58 +0000860private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000861 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000862 virtual bool isEqualImpl(const Matcher *M) const {
863 return cast<EmitNodeXFormMatcher>(M)->Slot == Slot &&
864 cast<EmitNodeXFormMatcher>(M)->NodeXForm == NodeXForm;
865 }
866 virtual unsigned getHashImpl() const {
867 return Slot ^ ((unsigned)(intptr_t)NodeXForm << 4);
868 }
Chris Lattner3163ca52010-02-21 03:22:59 +0000869};
Chris Lattnerddfb5222010-02-18 22:03:03 +0000870
Chris Lattner9a515172010-02-25 02:04:40 +0000871/// EmitNodeMatcher - This signals a successful match and generates a node.
872class EmitNodeMatcher : public Matcher {
Chris Lattner3163ca52010-02-21 03:22:59 +0000873 std::string OpcodeName;
874 const SmallVector<MVT::SimpleValueType, 3> VTs;
875 const SmallVector<unsigned, 6> Operands;
876 bool HasChain, HasFlag, HasMemRefs;
Chris Lattnerddfb5222010-02-18 22:03:03 +0000877
Chris Lattner3163ca52010-02-21 03:22:59 +0000878 /// NumFixedArityOperands - If this is a fixed arity node, this is set to -1.
879 /// If this is a varidic node, this is set to the number of fixed arity
880 /// operands in the root of the pattern. The rest are appended to this node.
881 int NumFixedArityOperands;
882public:
Chris Lattner9a515172010-02-25 02:04:40 +0000883 EmitNodeMatcher(const std::string &opcodeName,
Chris Lattner136ab782010-02-25 06:49:58 +0000884 const MVT::SimpleValueType *vts, unsigned numvts,
885 const unsigned *operands, unsigned numops,
886 bool hasChain, bool hasFlag, bool hasmemrefs,
887 int numfixedarityoperands)
Chris Lattner9a515172010-02-25 02:04:40 +0000888 : Matcher(EmitNode), OpcodeName(opcodeName),
Chris Lattner3163ca52010-02-21 03:22:59 +0000889 VTs(vts, vts+numvts), Operands(operands, operands+numops),
890 HasChain(hasChain), HasFlag(hasFlag), HasMemRefs(hasmemrefs),
891 NumFixedArityOperands(numfixedarityoperands) {}
892
893 const std::string &getOpcodeName() const { return OpcodeName; }
894
895 unsigned getNumVTs() const { return VTs.size(); }
896 MVT::SimpleValueType getVT(unsigned i) const {
897 assert(i < VTs.size());
898 return VTs[i];
899 }
900
901 unsigned getNumOperands() const { return Operands.size(); }
902 unsigned getOperand(unsigned i) const {
903 assert(i < Operands.size());
904 return Operands[i];
905 }
906
907 bool hasChain() const { return HasChain; }
908 bool hasFlag() const { return HasFlag; }
909 bool hasMemRefs() const { return HasMemRefs; }
910 int getNumFixedArityOperands() const { return NumFixedArityOperands; }
Chris Lattnerddfb5222010-02-18 22:03:03 +0000911
Chris Lattner9a515172010-02-25 02:04:40 +0000912 static inline bool classof(const Matcher *N) {
Chris Lattnerddfb5222010-02-18 22:03:03 +0000913 return N->getKind() == EmitNode;
914 }
915
Chris Lattner136ab782010-02-25 06:49:58 +0000916private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000917 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000918 virtual bool isEqualImpl(const Matcher *M) const;
919 virtual unsigned getHashImpl() const;
Chris Lattnerddfb5222010-02-18 22:03:03 +0000920};
Chris Lattnerb085ea12010-02-21 06:03:07 +0000921
Chris Lattner9a515172010-02-25 02:04:40 +0000922/// MarkFlagResultsMatcher - This node indicates which non-root nodes in the
923/// pattern produce flags. This allows CompleteMatchMatcher to update them
Chris Lattner69f60c82010-02-24 05:33:42 +0000924/// with the output flag of the resultant code.
Chris Lattner9a515172010-02-25 02:04:40 +0000925class MarkFlagResultsMatcher : public Matcher {
Chris Lattner69f60c82010-02-24 05:33:42 +0000926 SmallVector<unsigned, 3> FlagResultNodes;
927public:
Chris Lattner9a515172010-02-25 02:04:40 +0000928 MarkFlagResultsMatcher(const unsigned *nodes, unsigned NumNodes)
929 : Matcher(MarkFlagResults), FlagResultNodes(nodes, nodes+NumNodes) {}
Chris Lattner69f60c82010-02-24 05:33:42 +0000930
931 unsigned getNumNodes() const { return FlagResultNodes.size(); }
932
933 unsigned getNode(unsigned i) const {
934 assert(i < FlagResultNodes.size());
935 return FlagResultNodes[i];
936 }
937
Chris Lattner9a515172010-02-25 02:04:40 +0000938 static inline bool classof(const Matcher *N) {
Chris Lattner69f60c82010-02-24 05:33:42 +0000939 return N->getKind() == MarkFlagResults;
940 }
941
Chris Lattner136ab782010-02-25 06:49:58 +0000942private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000943 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000944 virtual bool isEqualImpl(const Matcher *M) const {
945 return cast<MarkFlagResultsMatcher>(M)->FlagResultNodes == FlagResultNodes;
946 }
947 virtual unsigned getHashImpl() const;
Chris Lattner69f60c82010-02-24 05:33:42 +0000948};
949
Chris Lattner9a515172010-02-25 02:04:40 +0000950/// CompleteMatchMatcher - Complete a match by replacing the results of the
Chris Lattnerb085ea12010-02-21 06:03:07 +0000951/// pattern with the newly generated nodes. This also prints a comment
952/// indicating the source and dest patterns.
Chris Lattner9a515172010-02-25 02:04:40 +0000953class CompleteMatchMatcher : public Matcher {
Chris Lattnerb085ea12010-02-21 06:03:07 +0000954 SmallVector<unsigned, 2> Results;
Chris Lattner3163ca52010-02-21 03:22:59 +0000955 const PatternToMatch &Pattern;
956public:
Chris Lattner9a515172010-02-25 02:04:40 +0000957 CompleteMatchMatcher(const unsigned *results, unsigned numresults,
Chris Lattnerb085ea12010-02-21 06:03:07 +0000958 const PatternToMatch &pattern)
Chris Lattner9a515172010-02-25 02:04:40 +0000959 : Matcher(CompleteMatch), Results(results, results+numresults),
Chris Lattnerb085ea12010-02-21 06:03:07 +0000960 Pattern(pattern) {}
961
962 unsigned getNumResults() const { return Results.size(); }
963 unsigned getResult(unsigned R) const { return Results[R]; }
Chris Lattner3163ca52010-02-21 03:22:59 +0000964 const PatternToMatch &getPattern() const { return Pattern; }
965
Chris Lattner9a515172010-02-25 02:04:40 +0000966 static inline bool classof(const Matcher *N) {
Chris Lattnerb085ea12010-02-21 06:03:07 +0000967 return N->getKind() == CompleteMatch;
Chris Lattner3163ca52010-02-21 03:22:59 +0000968 }
969
Chris Lattner136ab782010-02-25 06:49:58 +0000970private:
Chris Lattner392b1bf2010-02-25 06:53:39 +0000971 virtual void printImpl(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000972 virtual bool isEqualImpl(const Matcher *M) const {
973 return cast<CompleteMatchMatcher>(M)->Results == Results &&
974 &cast<CompleteMatchMatcher>(M)->Pattern == &Pattern;
975 }
976 virtual unsigned getHashImpl() const;
Chris Lattner3163ca52010-02-21 03:22:59 +0000977};
Chris Lattnerddfb5222010-02-18 22:03:03 +0000978
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000979} // end namespace llvm
980
981#endif