blob: 68132219bce6bee8562993885ad522a841ee289d [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 {
103 return (getHashImpl() << 4) ^ getKind();
104 }
105
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000106 virtual void print(raw_ostream &OS, unsigned indent = 0) const = 0;
107 void dump() const;
Chris Lattner3b31c982010-02-18 02:49:24 +0000108protected:
Chris Lattner78039ec2010-02-18 02:53:41 +0000109 void printNext(raw_ostream &OS, unsigned indent) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000110 virtual bool isEqualImpl(const Matcher *M) const = 0;
111 virtual unsigned getHashImpl() const = 0;
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000112};
113
Chris Lattner9a515172010-02-25 02:04:40 +0000114/// ScopeMatcher - This pushes a failure scope on the stack and evaluates
Chris Lattnerac10e4f2010-02-25 01:56:48 +0000115/// 'Check'. If 'Check' fails to match, it pops its scope and continues on to
116/// 'Next'.
Chris Lattner9a515172010-02-25 02:04:40 +0000117class ScopeMatcher : public Matcher {
118 OwningPtr<Matcher> Check;
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000119public:
Chris Lattner9a515172010-02-25 02:04:40 +0000120 ScopeMatcher(Matcher *check = 0, Matcher *next = 0)
121 : Matcher(Scope), Check(check) {
Chris Lattner78039ec2010-02-18 02:53:41 +0000122 setNext(next);
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000123 }
124
Chris Lattner9a515172010-02-25 02:04:40 +0000125 Matcher *getCheck() { return Check.get(); }
126 const Matcher *getCheck() const { return Check.get(); }
127 void setCheck(Matcher *N) { Check.reset(N); }
128 OwningPtr<Matcher> &getCheckPtr() { return Check; }
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000129
Chris Lattner9a515172010-02-25 02:04:40 +0000130 static inline bool classof(const Matcher *N) {
Chris Lattnerac10e4f2010-02-25 01:56:48 +0000131 return N->getKind() == Scope;
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000132 }
133
Chris Lattner136ab782010-02-25 06:49:58 +0000134private:
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000135 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000136 virtual bool isEqualImpl(const Matcher *M) const { return false; }
137 virtual unsigned getHashImpl() const { return 0; }
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000138};
139
Chris Lattner9a515172010-02-25 02:04:40 +0000140/// RecordMatcher - Save the current node in the operand list.
141class RecordMatcher : public Matcher {
Chris Lattner56cf88d2010-02-17 01:03:09 +0000142 /// WhatFor - This is a string indicating why we're recording this. This
143 /// should only be used for comment generation not anything semantic.
144 std::string WhatFor;
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000145public:
Chris Lattner9a515172010-02-25 02:04:40 +0000146 RecordMatcher(const std::string &whatfor)
147 : Matcher(RecordNode), WhatFor(whatfor) {}
Chris Lattner56cf88d2010-02-17 01:03:09 +0000148
Chris Lattner086ca522010-02-17 01:27:29 +0000149 const std::string &getWhatFor() const { return WhatFor; }
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000150
Chris Lattner9a515172010-02-25 02:04:40 +0000151 static inline bool classof(const Matcher *N) {
Chris Lattnerddfb5222010-02-18 22:03:03 +0000152 return N->getKind() == RecordNode;
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000153 }
154
Chris Lattner136ab782010-02-25 06:49:58 +0000155private:
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000156 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000157 virtual bool isEqualImpl(const Matcher *M) const { return true; }
158 virtual unsigned getHashImpl() const { return 0; }
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000159};
160
Chris Lattner9a515172010-02-25 02:04:40 +0000161/// RecordChildMatcher - Save a numbered child of the current node, or fail
Chris Lattner3ee1bc42010-02-24 07:31:45 +0000162/// the match if it doesn't exist. This is logically equivalent to:
163/// MoveChild N + RecordNode + MoveParent.
Chris Lattner9a515172010-02-25 02:04:40 +0000164class RecordChildMatcher : public Matcher {
Chris Lattner3ee1bc42010-02-24 07:31:45 +0000165 unsigned ChildNo;
166
167 /// WhatFor - This is a string indicating why we're recording this. This
168 /// should only be used for comment generation not anything semantic.
169 std::string WhatFor;
170public:
Chris Lattner9a515172010-02-25 02:04:40 +0000171 RecordChildMatcher(unsigned childno, const std::string &whatfor)
172 : Matcher(RecordChild), ChildNo(childno), WhatFor(whatfor) {}
Chris Lattner3ee1bc42010-02-24 07:31:45 +0000173
174 unsigned getChildNo() const { return ChildNo; }
175 const std::string &getWhatFor() const { return WhatFor; }
176
Chris Lattner9a515172010-02-25 02:04:40 +0000177 static inline bool classof(const Matcher *N) {
Chris Lattner3ee1bc42010-02-24 07:31:45 +0000178 return N->getKind() == RecordChild;
179 }
180
Chris Lattner136ab782010-02-25 06:49:58 +0000181private:
Chris Lattner3ee1bc42010-02-24 07:31:45 +0000182 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000183 virtual bool isEqualImpl(const Matcher *M) const {
184 return cast<RecordChildMatcher>(M)->getChildNo() == getChildNo();
185 }
186 virtual unsigned getHashImpl() const { return getChildNo(); }
Chris Lattner3ee1bc42010-02-24 07:31:45 +0000187};
188
Chris Lattner9a515172010-02-25 02:04:40 +0000189/// RecordMemRefMatcher - Save the current node's memref.
190class RecordMemRefMatcher : public Matcher {
Chris Lattner3163ca52010-02-21 03:22:59 +0000191public:
Chris Lattner9a515172010-02-25 02:04:40 +0000192 RecordMemRefMatcher() : Matcher(RecordMemRef) {}
Chris Lattner3163ca52010-02-21 03:22:59 +0000193
Chris Lattner9a515172010-02-25 02:04:40 +0000194 static inline bool classof(const Matcher *N) {
Chris Lattner3163ca52010-02-21 03:22:59 +0000195 return N->getKind() == RecordMemRef;
196 }
197
Chris Lattner136ab782010-02-25 06:49:58 +0000198private:
Chris Lattner3163ca52010-02-21 03:22:59 +0000199 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000200 virtual bool isEqualImpl(const Matcher *M) const { return true; }
201 virtual unsigned getHashImpl() const { return 0; }
Chris Lattner3163ca52010-02-21 03:22:59 +0000202};
203
204
Chris Lattner9a515172010-02-25 02:04:40 +0000205/// CaptureFlagInputMatcher - If the current record has a flag input, record
Chris Lattner3163ca52010-02-21 03:22:59 +0000206/// it so that it is used as an input to the generated code.
Chris Lattner9a515172010-02-25 02:04:40 +0000207class CaptureFlagInputMatcher : public Matcher {
Chris Lattner3163ca52010-02-21 03:22:59 +0000208public:
Chris Lattner9a515172010-02-25 02:04:40 +0000209 CaptureFlagInputMatcher() : Matcher(CaptureFlagInput) {}
Chris Lattner3163ca52010-02-21 03:22:59 +0000210
Chris Lattner9a515172010-02-25 02:04:40 +0000211 static inline bool classof(const Matcher *N) {
Chris Lattner3163ca52010-02-21 03:22:59 +0000212 return N->getKind() == CaptureFlagInput;
213 }
214
Chris Lattner136ab782010-02-25 06:49:58 +0000215private:
Chris Lattner3163ca52010-02-21 03:22:59 +0000216 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000217 virtual bool isEqualImpl(const Matcher *M) const { return true; }
218 virtual unsigned getHashImpl() const { return 0; }
Chris Lattner3163ca52010-02-21 03:22:59 +0000219};
220
Chris Lattner9a515172010-02-25 02:04:40 +0000221/// MoveChildMatcher - This tells the interpreter to move into the
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000222/// specified child node.
Chris Lattner9a515172010-02-25 02:04:40 +0000223class MoveChildMatcher : public Matcher {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000224 unsigned ChildNo;
225public:
Chris Lattner9a515172010-02-25 02:04:40 +0000226 MoveChildMatcher(unsigned childNo) : Matcher(MoveChild), ChildNo(childNo) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000227
228 unsigned getChildNo() const { return ChildNo; }
229
Chris Lattner9a515172010-02-25 02:04:40 +0000230 static inline bool classof(const Matcher *N) {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000231 return N->getKind() == MoveChild;
232 }
233
Chris Lattner136ab782010-02-25 06:49:58 +0000234private:
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000235 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000236 virtual bool isEqualImpl(const Matcher *M) const {
237 return cast<MoveChildMatcher>(M)->getChildNo() == getChildNo();
238 }
239 virtual unsigned getHashImpl() const { return getChildNo(); }
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000240};
241
Chris Lattner9a515172010-02-25 02:04:40 +0000242/// MoveParentMatcher - This tells the interpreter to move to the parent
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000243/// of the current node.
Chris Lattner9a515172010-02-25 02:04:40 +0000244class MoveParentMatcher : public Matcher {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000245public:
Chris Lattner9a515172010-02-25 02:04:40 +0000246 MoveParentMatcher() : Matcher(MoveParent) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000247
Chris Lattner9a515172010-02-25 02:04:40 +0000248 static inline bool classof(const Matcher *N) {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000249 return N->getKind() == MoveParent;
250 }
251
Chris Lattner136ab782010-02-25 06:49:58 +0000252private:
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000253 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000254 virtual bool isEqualImpl(const Matcher *M) const { return true; }
255 virtual unsigned getHashImpl() const { return 0; }
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000256};
257
Chris Lattner9a515172010-02-25 02:04:40 +0000258/// CheckSameMatcher - This checks to see if this node is exactly the same
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000259/// node as the specified match that was recorded with 'Record'. This is used
260/// when patterns have the same name in them, like '(mul GPR:$in, GPR:$in)'.
Chris Lattner9a515172010-02-25 02:04:40 +0000261class CheckSameMatcher : public Matcher {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000262 unsigned MatchNumber;
263public:
Chris Lattner9a515172010-02-25 02:04:40 +0000264 CheckSameMatcher(unsigned matchnumber)
Chris Lattner136ab782010-02-25 06:49:58 +0000265 : Matcher(CheckSame), MatchNumber(matchnumber) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000266
267 unsigned getMatchNumber() const { return MatchNumber; }
268
Chris Lattner9a515172010-02-25 02:04:40 +0000269 static inline bool classof(const Matcher *N) {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000270 return N->getKind() == CheckSame;
271 }
272
Chris Lattner136ab782010-02-25 06:49:58 +0000273private:
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000274 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000275 virtual bool isEqualImpl(const Matcher *M) const {
276 return cast<CheckSameMatcher>(M)->getMatchNumber() == getMatchNumber();
277 }
278 virtual unsigned getHashImpl() const { return getMatchNumber(); }
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000279};
280
Chris Lattner9a515172010-02-25 02:04:40 +0000281/// CheckPatternPredicateMatcher - This checks the target-specific predicate
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000282/// to see if the entire pattern is capable of matching. This predicate does
283/// not take a node as input. This is used for subtarget feature checks etc.
Chris Lattner9a515172010-02-25 02:04:40 +0000284class CheckPatternPredicateMatcher : public Matcher {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000285 std::string Predicate;
286public:
Chris Lattner9a515172010-02-25 02:04:40 +0000287 CheckPatternPredicateMatcher(StringRef predicate)
Chris Lattner136ab782010-02-25 06:49:58 +0000288 : Matcher(CheckPatternPredicate), Predicate(predicate) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000289
290 StringRef getPredicate() const { return Predicate; }
291
Chris Lattner9a515172010-02-25 02:04:40 +0000292 static inline bool classof(const Matcher *N) {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000293 return N->getKind() == CheckPatternPredicate;
294 }
295
Chris Lattner136ab782010-02-25 06:49:58 +0000296private:
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000297 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000298 virtual bool isEqualImpl(const Matcher *M) const {
299 return cast<CheckPatternPredicateMatcher>(M)->getPredicate() == Predicate;
300 }
301 virtual unsigned getHashImpl() const;
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000302};
303
Chris Lattner9a515172010-02-25 02:04:40 +0000304/// CheckPredicateMatcher - This checks the target-specific predicate to
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000305/// see if the node is acceptable.
Chris Lattner9a515172010-02-25 02:04:40 +0000306class CheckPredicateMatcher : public Matcher {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000307 StringRef PredName;
308public:
Chris Lattner9a515172010-02-25 02:04:40 +0000309 CheckPredicateMatcher(StringRef predname)
310 : Matcher(CheckPredicate), PredName(predname) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000311
312 StringRef getPredicateName() const { return PredName; }
313
Chris Lattner9a515172010-02-25 02:04:40 +0000314 static inline bool classof(const Matcher *N) {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000315 return N->getKind() == CheckPredicate;
316 }
317
Chris Lattner136ab782010-02-25 06:49:58 +0000318private:
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000319 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000320 virtual bool isEqualImpl(const Matcher *M) const {
321 return cast<CheckPredicateMatcher>(M)->PredName == PredName;
322 }
323 virtual unsigned getHashImpl() const;
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000324};
325
326
Chris Lattner9a515172010-02-25 02:04:40 +0000327/// CheckOpcodeMatcher - This checks to see if the current node has the
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000328/// specified opcode, if not it fails to match.
Chris Lattner9a515172010-02-25 02:04:40 +0000329class CheckOpcodeMatcher : public Matcher {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000330 StringRef OpcodeName;
331public:
Chris Lattner9a515172010-02-25 02:04:40 +0000332 CheckOpcodeMatcher(StringRef opcodename)
333 : Matcher(CheckOpcode), OpcodeName(opcodename) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000334
335 StringRef getOpcodeName() const { return OpcodeName; }
336
Chris Lattner9a515172010-02-25 02:04:40 +0000337 static inline bool classof(const Matcher *N) {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000338 return N->getKind() == CheckOpcode;
339 }
340
Chris Lattner136ab782010-02-25 06:49:58 +0000341private:
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000342 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000343 virtual bool isEqualImpl(const Matcher *M) const {
344 return cast<CheckOpcodeMatcher>(M)->OpcodeName == OpcodeName;
345 }
346 virtual unsigned getHashImpl() const;
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000347};
348
Chris Lattner9a515172010-02-25 02:04:40 +0000349/// CheckMultiOpcodeMatcher - This checks to see if the current node has one
Chris Lattnerf58c08e2010-02-22 22:30:37 +0000350/// of the specified opcode, if not it fails to match.
Chris Lattner9a515172010-02-25 02:04:40 +0000351class CheckMultiOpcodeMatcher : public Matcher {
Chris Lattnerf58c08e2010-02-22 22:30:37 +0000352 SmallVector<StringRef, 4> OpcodeNames;
353public:
Chris Lattner9a515172010-02-25 02:04:40 +0000354 CheckMultiOpcodeMatcher(const StringRef *opcodes, unsigned numops)
355 : Matcher(CheckMultiOpcode), OpcodeNames(opcodes, opcodes+numops) {}
Chris Lattnerf58c08e2010-02-22 22:30:37 +0000356
357 unsigned getNumOpcodeNames() const { return OpcodeNames.size(); }
358 StringRef getOpcodeName(unsigned i) const { return OpcodeNames[i]; }
359
Chris Lattner9a515172010-02-25 02:04:40 +0000360 static inline bool classof(const Matcher *N) {
Chris Lattnerf58c08e2010-02-22 22:30:37 +0000361 return N->getKind() == CheckMultiOpcode;
362 }
363
Chris Lattner136ab782010-02-25 06:49:58 +0000364private:
Chris Lattnerf58c08e2010-02-22 22:30:37 +0000365 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000366 virtual bool isEqualImpl(const Matcher *M) const {
367 return cast<CheckMultiOpcodeMatcher>(M)->OpcodeNames == OpcodeNames;
368 }
369 virtual unsigned getHashImpl() const;
Chris Lattnerf58c08e2010-02-22 22:30:37 +0000370};
371
372
373
Chris Lattner9a515172010-02-25 02:04:40 +0000374/// CheckTypeMatcher - This checks to see if the current node has the
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000375/// specified type, if not it fails to match.
Chris Lattner9a515172010-02-25 02:04:40 +0000376class CheckTypeMatcher : public Matcher {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000377 MVT::SimpleValueType Type;
378public:
Chris Lattner9a515172010-02-25 02:04:40 +0000379 CheckTypeMatcher(MVT::SimpleValueType type)
380 : Matcher(CheckType), Type(type) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000381
382 MVT::SimpleValueType getType() const { return Type; }
383
Chris Lattner9a515172010-02-25 02:04:40 +0000384 static inline bool classof(const Matcher *N) {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000385 return N->getKind() == CheckType;
386 }
387
Chris Lattner136ab782010-02-25 06:49:58 +0000388private:
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000389 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000390 virtual bool isEqualImpl(const Matcher *M) const {
391 return cast<CheckTypeMatcher>(this)->Type == Type;
392 }
393 virtual unsigned getHashImpl() const { return Type; }
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000394};
Chris Lattner3c664b32010-02-24 20:15:25 +0000395
Chris Lattner9a515172010-02-25 02:04:40 +0000396/// CheckChildTypeMatcher - This checks to see if a child node has the
Chris Lattner3c664b32010-02-24 20:15:25 +0000397/// specified type, if not it fails to match.
Chris Lattner9a515172010-02-25 02:04:40 +0000398class CheckChildTypeMatcher : public Matcher {
Chris Lattner3c664b32010-02-24 20:15:25 +0000399 unsigned ChildNo;
400 MVT::SimpleValueType Type;
401public:
Chris Lattner9a515172010-02-25 02:04:40 +0000402 CheckChildTypeMatcher(unsigned childno, MVT::SimpleValueType type)
403 : Matcher(CheckChildType), ChildNo(childno), Type(type) {}
Chris Lattner3c664b32010-02-24 20:15:25 +0000404
405 unsigned getChildNo() const { return ChildNo; }
406 MVT::SimpleValueType getType() const { return Type; }
407
Chris Lattner9a515172010-02-25 02:04:40 +0000408 static inline bool classof(const Matcher *N) {
Chris Lattner3c664b32010-02-24 20:15:25 +0000409 return N->getKind() == CheckChildType;
410 }
411
Chris Lattner136ab782010-02-25 06:49:58 +0000412private:
Chris Lattner3c664b32010-02-24 20:15:25 +0000413 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000414 virtual bool isEqualImpl(const Matcher *M) const {
415 return cast<CheckChildTypeMatcher>(M)->ChildNo == ChildNo &&
416 cast<CheckChildTypeMatcher>(M)->Type == Type;
417 }
418 virtual unsigned getHashImpl() const { return (Type << 3) | ChildNo; }
Chris Lattner3c664b32010-02-24 20:15:25 +0000419};
420
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000421
Chris Lattner9a515172010-02-25 02:04:40 +0000422/// CheckIntegerMatcher - This checks to see if the current node is a
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000423/// ConstantSDNode with the specified integer value, if not it fails to match.
Chris Lattner9a515172010-02-25 02:04:40 +0000424class CheckIntegerMatcher : public Matcher {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000425 int64_t Value;
426public:
Chris Lattner9a515172010-02-25 02:04:40 +0000427 CheckIntegerMatcher(int64_t value)
428 : Matcher(CheckInteger), Value(value) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000429
430 int64_t getValue() const { return Value; }
431
Chris Lattner9a515172010-02-25 02:04:40 +0000432 static inline bool classof(const Matcher *N) {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000433 return N->getKind() == CheckInteger;
434 }
435
Chris Lattner136ab782010-02-25 06:49:58 +0000436private:
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000437 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000438 virtual bool isEqualImpl(const Matcher *M) const {
439 return cast<CheckIntegerMatcher>(M)->Value == Value;
440 }
441 virtual unsigned getHashImpl() const { return Value; }
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000442};
443
Chris Lattner9a515172010-02-25 02:04:40 +0000444/// CheckCondCodeMatcher - This checks to see if the current node is a
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000445/// CondCodeSDNode with the specified condition, if not it fails to match.
Chris Lattner9a515172010-02-25 02:04:40 +0000446class CheckCondCodeMatcher : public Matcher {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000447 StringRef CondCodeName;
448public:
Chris Lattner9a515172010-02-25 02:04:40 +0000449 CheckCondCodeMatcher(StringRef condcodename)
450 : Matcher(CheckCondCode), CondCodeName(condcodename) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000451
452 StringRef getCondCodeName() const { return CondCodeName; }
453
Chris Lattner9a515172010-02-25 02:04:40 +0000454 static inline bool classof(const Matcher *N) {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000455 return N->getKind() == CheckCondCode;
456 }
457
Chris Lattner136ab782010-02-25 06:49:58 +0000458private:
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000459 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000460 virtual bool isEqualImpl(const Matcher *M) const {
461 return cast<CheckCondCodeMatcher>(M)->CondCodeName == CondCodeName;
462 }
463 virtual unsigned getHashImpl() const;
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000464};
465
Chris Lattner9a515172010-02-25 02:04:40 +0000466/// CheckValueTypeMatcher - This checks to see if the current node is a
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000467/// VTSDNode with the specified type, if not it fails to match.
Chris Lattner9a515172010-02-25 02:04:40 +0000468class CheckValueTypeMatcher : public Matcher {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000469 StringRef TypeName;
470public:
Chris Lattner9a515172010-02-25 02:04:40 +0000471 CheckValueTypeMatcher(StringRef type_name)
472 : Matcher(CheckValueType), TypeName(type_name) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000473
474 StringRef getTypeName() const { return TypeName; }
475
Chris Lattner9a515172010-02-25 02:04:40 +0000476 static inline bool classof(const Matcher *N) {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000477 return N->getKind() == CheckValueType;
478 }
479
Chris Lattner136ab782010-02-25 06:49:58 +0000480private:
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000481 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000482 virtual bool isEqualImpl(const Matcher *M) const {
483 return cast<CheckValueTypeMatcher>(M)->TypeName == TypeName;
484 }
485 virtual unsigned getHashImpl() const;
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000486};
487
488
489
Chris Lattner9a515172010-02-25 02:04:40 +0000490/// CheckComplexPatMatcher - This node runs the specified ComplexPattern on
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000491/// the current node.
Chris Lattner9a515172010-02-25 02:04:40 +0000492class CheckComplexPatMatcher : public Matcher {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000493 const ComplexPattern &Pattern;
494public:
Chris Lattner9a515172010-02-25 02:04:40 +0000495 CheckComplexPatMatcher(const ComplexPattern &pattern)
496 : Matcher(CheckComplexPat), Pattern(pattern) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000497
Chris Lattnerdc429fd2010-02-17 00:31:50 +0000498 const ComplexPattern &getPattern() const { return Pattern; }
499
Chris Lattner9a515172010-02-25 02:04:40 +0000500 static inline bool classof(const Matcher *N) {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000501 return N->getKind() == CheckComplexPat;
502 }
503
Chris Lattner136ab782010-02-25 06:49:58 +0000504private:
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000505 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000506 virtual bool isEqualImpl(const Matcher *M) const {
507 return &cast<CheckComplexPatMatcher>(M)->Pattern == &Pattern;
508 }
509 virtual unsigned getHashImpl() const {
510 return (unsigned)(intptr_t)&Pattern;
511 }
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000512};
513
Chris Lattner9a515172010-02-25 02:04:40 +0000514/// CheckAndImmMatcher - This checks to see if the current node is an 'and'
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000515/// with something equivalent to the specified immediate.
Chris Lattner9a515172010-02-25 02:04:40 +0000516class CheckAndImmMatcher : public Matcher {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000517 int64_t Value;
518public:
Chris Lattner9a515172010-02-25 02:04:40 +0000519 CheckAndImmMatcher(int64_t value)
520 : Matcher(CheckAndImm), Value(value) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000521
522 int64_t getValue() const { return Value; }
523
Chris Lattner9a515172010-02-25 02:04:40 +0000524 static inline bool classof(const Matcher *N) {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000525 return N->getKind() == CheckAndImm;
526 }
527
Chris Lattner136ab782010-02-25 06:49:58 +0000528private:
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000529 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000530 virtual bool isEqualImpl(const Matcher *M) const {
531 return cast<CheckAndImmMatcher>(M)->Value == Value;
532 }
533 virtual unsigned getHashImpl() const { return Value; }
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000534};
535
Chris Lattner9a515172010-02-25 02:04:40 +0000536/// CheckOrImmMatcher - This checks to see if the current node is an 'and'
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000537/// with something equivalent to the specified immediate.
Chris Lattner9a515172010-02-25 02:04:40 +0000538class CheckOrImmMatcher : public Matcher {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000539 int64_t Value;
540public:
Chris Lattner9a515172010-02-25 02:04:40 +0000541 CheckOrImmMatcher(int64_t value)
542 : Matcher(CheckOrImm), Value(value) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000543
544 int64_t getValue() const { return Value; }
545
Chris Lattner9a515172010-02-25 02:04:40 +0000546 static inline bool classof(const Matcher *N) {
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000547 return N->getKind() == CheckOrImm;
548 }
549
Chris Lattner136ab782010-02-25 06:49:58 +0000550private:
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000551 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000552 virtual bool isEqualImpl(const Matcher *M) const {
553 return cast<CheckOrImmMatcher>(M)->Value == Value;
554 }
555 virtual unsigned getHashImpl() const { return Value; }
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000556};
Chris Lattner9de39482010-02-16 06:10:58 +0000557
Chris Lattner9a515172010-02-25 02:04:40 +0000558/// CheckFoldableChainNodeMatcher - This checks to see if the current node
Chris Lattnerc6dc8f62010-02-16 19:15:55 +0000559/// (which defines a chain operand) is safe to fold into a larger pattern.
Chris Lattner9a515172010-02-25 02:04:40 +0000560class CheckFoldableChainNodeMatcher : public Matcher {
Chris Lattner9de39482010-02-16 06:10:58 +0000561public:
Chris Lattner9a515172010-02-25 02:04:40 +0000562 CheckFoldableChainNodeMatcher()
563 : Matcher(CheckFoldableChainNode) {}
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000564
Chris Lattner9a515172010-02-25 02:04:40 +0000565 static inline bool classof(const Matcher *N) {
Chris Lattnerc6dc8f62010-02-16 19:15:55 +0000566 return N->getKind() == CheckFoldableChainNode;
Chris Lattner9de39482010-02-16 06:10:58 +0000567 }
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000568
Chris Lattner136ab782010-02-25 06:49:58 +0000569private:
Chris Lattner9de39482010-02-16 06:10:58 +0000570 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000571 virtual bool isEqualImpl(const Matcher *M) const { return true; }
572 virtual unsigned getHashImpl() const { return 0; }
Chris Lattner9de39482010-02-16 06:10:58 +0000573};
574
Chris Lattner9a515172010-02-25 02:04:40 +0000575/// CheckChainCompatibleMatcher - Verify that the current node's chain
Chris Lattner283adcb2010-02-17 06:23:39 +0000576/// operand is 'compatible' with the specified recorded node's.
Chris Lattner9a515172010-02-25 02:04:40 +0000577class CheckChainCompatibleMatcher : public Matcher {
Chris Lattner283adcb2010-02-17 06:23:39 +0000578 unsigned PreviousOp;
579public:
Chris Lattner9a515172010-02-25 02:04:40 +0000580 CheckChainCompatibleMatcher(unsigned previousop)
581 : Matcher(CheckChainCompatible), PreviousOp(previousop) {}
Chris Lattner283adcb2010-02-17 06:23:39 +0000582
583 unsigned getPreviousOp() const { return PreviousOp; }
584
Chris Lattner9a515172010-02-25 02:04:40 +0000585 static inline bool classof(const Matcher *N) {
Chris Lattner283adcb2010-02-17 06:23:39 +0000586 return N->getKind() == CheckChainCompatible;
587 }
588
Chris Lattner136ab782010-02-25 06:49:58 +0000589private:
Chris Lattner283adcb2010-02-17 06:23:39 +0000590 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000591 virtual bool isEqualImpl(const Matcher *M) const {
592 return cast<CheckChainCompatibleMatcher>(this)->PreviousOp == PreviousOp;
593 }
594 virtual unsigned getHashImpl() const { return PreviousOp; }
Chris Lattner283adcb2010-02-17 06:23:39 +0000595};
596
Chris Lattner9a515172010-02-25 02:04:40 +0000597/// EmitIntegerMatcher - This creates a new TargetConstant.
598class EmitIntegerMatcher : public Matcher {
Chris Lattnerddfb5222010-02-18 22:03:03 +0000599 int64_t Val;
600 MVT::SimpleValueType VT;
601public:
Chris Lattner9a515172010-02-25 02:04:40 +0000602 EmitIntegerMatcher(int64_t val, MVT::SimpleValueType vt)
603 : Matcher(EmitInteger), Val(val), VT(vt) {}
Chris Lattner283adcb2010-02-17 06:23:39 +0000604
Chris Lattner7043e0a2010-02-19 07:49:56 +0000605 int64_t getValue() const { return Val; }
Chris Lattnerddfb5222010-02-18 22:03:03 +0000606 MVT::SimpleValueType getVT() const { return VT; }
607
Chris Lattner9a515172010-02-25 02:04:40 +0000608 static inline bool classof(const Matcher *N) {
Chris Lattnerddfb5222010-02-18 22:03:03 +0000609 return N->getKind() == EmitInteger;
610 }
611
Chris Lattner136ab782010-02-25 06:49:58 +0000612private:
Chris Lattnerddfb5222010-02-18 22:03:03 +0000613 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000614 virtual bool isEqualImpl(const Matcher *M) const {
615 return cast<EmitIntegerMatcher>(M)->Val == Val &&
616 cast<EmitIntegerMatcher>(M)->VT == VT;
617 }
618 virtual unsigned getHashImpl() const { return (Val << 4) | VT; }
Chris Lattnerddfb5222010-02-18 22:03:03 +0000619};
Chris Lattner3163ca52010-02-21 03:22:59 +0000620
Chris Lattner9a515172010-02-25 02:04:40 +0000621/// EmitStringIntegerMatcher - A target constant whose value is represented
Chris Lattner3163ca52010-02-21 03:22:59 +0000622/// by a string.
Chris Lattner9a515172010-02-25 02:04:40 +0000623class EmitStringIntegerMatcher : public Matcher {
Chris Lattner3163ca52010-02-21 03:22:59 +0000624 std::string Val;
625 MVT::SimpleValueType VT;
626public:
Chris Lattner9a515172010-02-25 02:04:40 +0000627 EmitStringIntegerMatcher(const std::string &val, MVT::SimpleValueType vt)
628 : Matcher(EmitStringInteger), Val(val), VT(vt) {}
Chris Lattner3163ca52010-02-21 03:22:59 +0000629
630 const std::string &getValue() const { return Val; }
631 MVT::SimpleValueType getVT() const { return VT; }
632
Chris Lattner9a515172010-02-25 02:04:40 +0000633 static inline bool classof(const Matcher *N) {
Chris Lattner3163ca52010-02-21 03:22:59 +0000634 return N->getKind() == EmitStringInteger;
635 }
636
Chris Lattner136ab782010-02-25 06:49:58 +0000637private:
Chris Lattner3163ca52010-02-21 03:22:59 +0000638 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000639 virtual bool isEqualImpl(const Matcher *M) const {
640 return cast<EmitStringIntegerMatcher>(M)->Val == Val &&
641 cast<EmitStringIntegerMatcher>(M)->VT == VT;
642 }
643 virtual unsigned getHashImpl() const;
Chris Lattner3163ca52010-02-21 03:22:59 +0000644};
Chris Lattnerddfb5222010-02-18 22:03:03 +0000645
Chris Lattner9a515172010-02-25 02:04:40 +0000646/// EmitRegisterMatcher - This creates a new TargetConstant.
647class EmitRegisterMatcher : public Matcher {
Chris Lattnerddfb5222010-02-18 22:03:03 +0000648 /// Reg - The def for the register that we're emitting. If this is null, then
649 /// this is a reference to zero_reg.
650 Record *Reg;
651 MVT::SimpleValueType VT;
652public:
Chris Lattner9a515172010-02-25 02:04:40 +0000653 EmitRegisterMatcher(Record *reg, MVT::SimpleValueType vt)
654 : Matcher(EmitRegister), Reg(reg), VT(vt) {}
Chris Lattnerddfb5222010-02-18 22:03:03 +0000655
656 Record *getReg() const { return Reg; }
657 MVT::SimpleValueType getVT() const { return VT; }
658
Chris Lattner9a515172010-02-25 02:04:40 +0000659 static inline bool classof(const Matcher *N) {
Chris Lattnerddfb5222010-02-18 22:03:03 +0000660 return N->getKind() == EmitRegister;
661 }
662
Chris Lattner136ab782010-02-25 06:49:58 +0000663private:
Chris Lattnerddfb5222010-02-18 22:03:03 +0000664 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000665 virtual bool isEqualImpl(const Matcher *M) const {
666 return cast<EmitRegisterMatcher>(M)->Reg == Reg &&
667 cast<EmitRegisterMatcher>(M)->VT == VT;
668 }
669 virtual unsigned getHashImpl() const {
670 return ((unsigned)(intptr_t)Reg) << 4 | VT;
671 }
Chris Lattnerddfb5222010-02-18 22:03:03 +0000672};
Chris Lattner3163ca52010-02-21 03:22:59 +0000673
Chris Lattner9a515172010-02-25 02:04:40 +0000674/// EmitConvertToTargetMatcher - Emit an operation that reads a specified
Chris Lattner3163ca52010-02-21 03:22:59 +0000675/// recorded node and converts it from being a ISD::Constant to
676/// ISD::TargetConstant, likewise for ConstantFP.
Chris Lattner9a515172010-02-25 02:04:40 +0000677class EmitConvertToTargetMatcher : public Matcher {
Chris Lattner3163ca52010-02-21 03:22:59 +0000678 unsigned Slot;
679public:
Chris Lattner9a515172010-02-25 02:04:40 +0000680 EmitConvertToTargetMatcher(unsigned slot)
681 : Matcher(EmitConvertToTarget), Slot(slot) {}
Chris Lattner3163ca52010-02-21 03:22:59 +0000682
683 unsigned getSlot() const { return Slot; }
684
Chris Lattner9a515172010-02-25 02:04:40 +0000685 static inline bool classof(const Matcher *N) {
Chris Lattner3163ca52010-02-21 03:22:59 +0000686 return N->getKind() == EmitConvertToTarget;
687 }
688
Chris Lattner136ab782010-02-25 06:49:58 +0000689private:
Chris Lattner3163ca52010-02-21 03:22:59 +0000690 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000691 virtual bool isEqualImpl(const Matcher *M) const {
692 return cast<EmitConvertToTargetMatcher>(M)->Slot == Slot;
693 }
694 virtual unsigned getHashImpl() const { return Slot; }
Chris Lattner3163ca52010-02-21 03:22:59 +0000695};
696
Chris Lattner9a515172010-02-25 02:04:40 +0000697/// EmitMergeInputChainsMatcher - Emit a node that merges a list of input
Chris Lattner3163ca52010-02-21 03:22:59 +0000698/// chains together with a token factor. The list of nodes are the nodes in the
699/// matched pattern that have chain input/outputs. This node adds all input
700/// chains of these nodes if they are not themselves a node in the pattern.
Chris Lattner9a515172010-02-25 02:04:40 +0000701class EmitMergeInputChainsMatcher : public Matcher {
Chris Lattner3163ca52010-02-21 03:22:59 +0000702 SmallVector<unsigned, 3> ChainNodes;
703public:
Chris Lattner9a515172010-02-25 02:04:40 +0000704 EmitMergeInputChainsMatcher(const unsigned *nodes, unsigned NumNodes)
705 : Matcher(EmitMergeInputChains), ChainNodes(nodes, nodes+NumNodes) {}
Chris Lattner3163ca52010-02-21 03:22:59 +0000706
707 unsigned getNumNodes() const { return ChainNodes.size(); }
708
709 unsigned getNode(unsigned i) const {
710 assert(i < ChainNodes.size());
711 return ChainNodes[i];
712 }
713
Chris Lattner9a515172010-02-25 02:04:40 +0000714 static inline bool classof(const Matcher *N) {
Chris Lattner3163ca52010-02-21 03:22:59 +0000715 return N->getKind() == EmitMergeInputChains;
716 }
717
Chris Lattner136ab782010-02-25 06:49:58 +0000718private:
Chris Lattner3163ca52010-02-21 03:22:59 +0000719 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000720 virtual bool isEqualImpl(const Matcher *M) const {
721 return cast<EmitMergeInputChainsMatcher>(M)->ChainNodes == ChainNodes;
722 }
723 virtual unsigned getHashImpl() const;
Chris Lattner3163ca52010-02-21 03:22:59 +0000724};
725
Chris Lattner9a515172010-02-25 02:04:40 +0000726/// EmitCopyToRegMatcher - Emit a CopyToReg node from a value to a physreg,
Chris Lattner3163ca52010-02-21 03:22:59 +0000727/// pushing the chain and flag results.
728///
Chris Lattner9a515172010-02-25 02:04:40 +0000729class EmitCopyToRegMatcher : public Matcher {
Chris Lattner3163ca52010-02-21 03:22:59 +0000730 unsigned SrcSlot; // Value to copy into the physreg.
731 Record *DestPhysReg;
732public:
Chris Lattner9a515172010-02-25 02:04:40 +0000733 EmitCopyToRegMatcher(unsigned srcSlot, Record *destPhysReg)
734 : Matcher(EmitCopyToReg), SrcSlot(srcSlot), DestPhysReg(destPhysReg) {}
Chris Lattner3163ca52010-02-21 03:22:59 +0000735
736 unsigned getSrcSlot() const { return SrcSlot; }
737 Record *getDestPhysReg() const { return DestPhysReg; }
738
Chris Lattner9a515172010-02-25 02:04:40 +0000739 static inline bool classof(const Matcher *N) {
Chris Lattner3163ca52010-02-21 03:22:59 +0000740 return N->getKind() == EmitCopyToReg;
741 }
742
Chris Lattner136ab782010-02-25 06:49:58 +0000743private:
Chris Lattner3163ca52010-02-21 03:22:59 +0000744 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000745 virtual bool isEqualImpl(const Matcher *M) const {
746 return cast<EmitCopyToRegMatcher>(M)->SrcSlot == SrcSlot &&
747 cast<EmitCopyToRegMatcher>(M)->DestPhysReg == DestPhysReg;
748 }
749 virtual unsigned getHashImpl() const {
750 return SrcSlot ^ ((unsigned)(intptr_t)DestPhysReg << 4);
751 }
Chris Lattner3163ca52010-02-21 03:22:59 +0000752};
753
754
755
Chris Lattner9a515172010-02-25 02:04:40 +0000756/// EmitNodeXFormMatcher - Emit an operation that runs an SDNodeXForm on a
Chris Lattner3163ca52010-02-21 03:22:59 +0000757/// recorded node and records the result.
Chris Lattner9a515172010-02-25 02:04:40 +0000758class EmitNodeXFormMatcher : public Matcher {
Chris Lattner3163ca52010-02-21 03:22:59 +0000759 unsigned Slot;
760 Record *NodeXForm;
761public:
Chris Lattner9a515172010-02-25 02:04:40 +0000762 EmitNodeXFormMatcher(unsigned slot, Record *nodeXForm)
763 : Matcher(EmitNodeXForm), Slot(slot), NodeXForm(nodeXForm) {}
Chris Lattner3163ca52010-02-21 03:22:59 +0000764
765 unsigned getSlot() const { return Slot; }
766 Record *getNodeXForm() const { return NodeXForm; }
767
Chris Lattner9a515172010-02-25 02:04:40 +0000768 static inline bool classof(const Matcher *N) {
Chris Lattner3163ca52010-02-21 03:22:59 +0000769 return N->getKind() == EmitNodeXForm;
770 }
771
Chris Lattner136ab782010-02-25 06:49:58 +0000772private:
Chris Lattner3163ca52010-02-21 03:22:59 +0000773 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000774 virtual bool isEqualImpl(const Matcher *M) const {
775 return cast<EmitNodeXFormMatcher>(M)->Slot == Slot &&
776 cast<EmitNodeXFormMatcher>(M)->NodeXForm == NodeXForm;
777 }
778 virtual unsigned getHashImpl() const {
779 return Slot ^ ((unsigned)(intptr_t)NodeXForm << 4);
780 }
Chris Lattner3163ca52010-02-21 03:22:59 +0000781};
Chris Lattnerddfb5222010-02-18 22:03:03 +0000782
Chris Lattner9a515172010-02-25 02:04:40 +0000783/// EmitNodeMatcher - This signals a successful match and generates a node.
784class EmitNodeMatcher : public Matcher {
Chris Lattner3163ca52010-02-21 03:22:59 +0000785 std::string OpcodeName;
786 const SmallVector<MVT::SimpleValueType, 3> VTs;
787 const SmallVector<unsigned, 6> Operands;
788 bool HasChain, HasFlag, HasMemRefs;
Chris Lattnerddfb5222010-02-18 22:03:03 +0000789
Chris Lattner3163ca52010-02-21 03:22:59 +0000790 /// NumFixedArityOperands - If this is a fixed arity node, this is set to -1.
791 /// If this is a varidic node, this is set to the number of fixed arity
792 /// operands in the root of the pattern. The rest are appended to this node.
793 int NumFixedArityOperands;
794public:
Chris Lattner9a515172010-02-25 02:04:40 +0000795 EmitNodeMatcher(const std::string &opcodeName,
Chris Lattner136ab782010-02-25 06:49:58 +0000796 const MVT::SimpleValueType *vts, unsigned numvts,
797 const unsigned *operands, unsigned numops,
798 bool hasChain, bool hasFlag, bool hasmemrefs,
799 int numfixedarityoperands)
Chris Lattner9a515172010-02-25 02:04:40 +0000800 : Matcher(EmitNode), OpcodeName(opcodeName),
Chris Lattner3163ca52010-02-21 03:22:59 +0000801 VTs(vts, vts+numvts), Operands(operands, operands+numops),
802 HasChain(hasChain), HasFlag(hasFlag), HasMemRefs(hasmemrefs),
803 NumFixedArityOperands(numfixedarityoperands) {}
804
805 const std::string &getOpcodeName() const { return OpcodeName; }
806
807 unsigned getNumVTs() const { return VTs.size(); }
808 MVT::SimpleValueType getVT(unsigned i) const {
809 assert(i < VTs.size());
810 return VTs[i];
811 }
812
813 unsigned getNumOperands() const { return Operands.size(); }
814 unsigned getOperand(unsigned i) const {
815 assert(i < Operands.size());
816 return Operands[i];
817 }
818
819 bool hasChain() const { return HasChain; }
820 bool hasFlag() const { return HasFlag; }
821 bool hasMemRefs() const { return HasMemRefs; }
822 int getNumFixedArityOperands() const { return NumFixedArityOperands; }
Chris Lattnerddfb5222010-02-18 22:03:03 +0000823
Chris Lattner9a515172010-02-25 02:04:40 +0000824 static inline bool classof(const Matcher *N) {
Chris Lattnerddfb5222010-02-18 22:03:03 +0000825 return N->getKind() == EmitNode;
826 }
827
Chris Lattner136ab782010-02-25 06:49:58 +0000828private:
Chris Lattnerddfb5222010-02-18 22:03:03 +0000829 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000830 virtual bool isEqualImpl(const Matcher *M) const;
831 virtual unsigned getHashImpl() const;
Chris Lattnerddfb5222010-02-18 22:03:03 +0000832};
Chris Lattnerb085ea12010-02-21 06:03:07 +0000833
Chris Lattner9a515172010-02-25 02:04:40 +0000834/// MarkFlagResultsMatcher - This node indicates which non-root nodes in the
835/// pattern produce flags. This allows CompleteMatchMatcher to update them
Chris Lattner69f60c82010-02-24 05:33:42 +0000836/// with the output flag of the resultant code.
Chris Lattner9a515172010-02-25 02:04:40 +0000837class MarkFlagResultsMatcher : public Matcher {
Chris Lattner69f60c82010-02-24 05:33:42 +0000838 SmallVector<unsigned, 3> FlagResultNodes;
839public:
Chris Lattner9a515172010-02-25 02:04:40 +0000840 MarkFlagResultsMatcher(const unsigned *nodes, unsigned NumNodes)
841 : Matcher(MarkFlagResults), FlagResultNodes(nodes, nodes+NumNodes) {}
Chris Lattner69f60c82010-02-24 05:33:42 +0000842
843 unsigned getNumNodes() const { return FlagResultNodes.size(); }
844
845 unsigned getNode(unsigned i) const {
846 assert(i < FlagResultNodes.size());
847 return FlagResultNodes[i];
848 }
849
Chris Lattner9a515172010-02-25 02:04:40 +0000850 static inline bool classof(const Matcher *N) {
Chris Lattner69f60c82010-02-24 05:33:42 +0000851 return N->getKind() == MarkFlagResults;
852 }
853
Chris Lattner136ab782010-02-25 06:49:58 +0000854private:
Chris Lattner69f60c82010-02-24 05:33:42 +0000855 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000856 virtual bool isEqualImpl(const Matcher *M) const {
857 return cast<MarkFlagResultsMatcher>(M)->FlagResultNodes == FlagResultNodes;
858 }
859 virtual unsigned getHashImpl() const;
Chris Lattner69f60c82010-02-24 05:33:42 +0000860};
861
Chris Lattner9a515172010-02-25 02:04:40 +0000862/// CompleteMatchMatcher - Complete a match by replacing the results of the
Chris Lattnerb085ea12010-02-21 06:03:07 +0000863/// pattern with the newly generated nodes. This also prints a comment
864/// indicating the source and dest patterns.
Chris Lattner9a515172010-02-25 02:04:40 +0000865class CompleteMatchMatcher : public Matcher {
Chris Lattnerb085ea12010-02-21 06:03:07 +0000866 SmallVector<unsigned, 2> Results;
Chris Lattner3163ca52010-02-21 03:22:59 +0000867 const PatternToMatch &Pattern;
868public:
Chris Lattner9a515172010-02-25 02:04:40 +0000869 CompleteMatchMatcher(const unsigned *results, unsigned numresults,
Chris Lattnerb085ea12010-02-21 06:03:07 +0000870 const PatternToMatch &pattern)
Chris Lattner9a515172010-02-25 02:04:40 +0000871 : Matcher(CompleteMatch), Results(results, results+numresults),
Chris Lattnerb085ea12010-02-21 06:03:07 +0000872 Pattern(pattern) {}
873
874 unsigned getNumResults() const { return Results.size(); }
875 unsigned getResult(unsigned R) const { return Results[R]; }
Chris Lattner3163ca52010-02-21 03:22:59 +0000876 const PatternToMatch &getPattern() const { return Pattern; }
877
Chris Lattner9a515172010-02-25 02:04:40 +0000878 static inline bool classof(const Matcher *N) {
Chris Lattnerb085ea12010-02-21 06:03:07 +0000879 return N->getKind() == CompleteMatch;
Chris Lattner3163ca52010-02-21 03:22:59 +0000880 }
881
Chris Lattner136ab782010-02-25 06:49:58 +0000882private:
Chris Lattner3163ca52010-02-21 03:22:59 +0000883 virtual void print(raw_ostream &OS, unsigned indent = 0) const;
Chris Lattner136ab782010-02-25 06:49:58 +0000884 virtual bool isEqualImpl(const Matcher *M) const {
885 return cast<CompleteMatchMatcher>(M)->Results == Results &&
886 &cast<CompleteMatchMatcher>(M)->Pattern == &Pattern;
887 }
888 virtual unsigned getHashImpl() const;
Chris Lattner3163ca52010-02-21 03:22:59 +0000889};
Chris Lattnerddfb5222010-02-18 22:03:03 +0000890
Chris Lattnere7d6e3c2010-02-15 08:04:42 +0000891} // end namespace llvm
892
893#endif