DAGISelMatcher.h revision 58aa834d31c2c0f378c8de5ec0450c2ab5568a2a
1//===- 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
13#include "llvm/CodeGen/ValueTypes.h"
14#include "llvm/ADT/OwningPtr.h"
15#include "llvm/ADT/StringRef.h"
16#include "llvm/ADT/SmallVector.h"
17#include "llvm/Support/Casting.h"
18
19namespace llvm {
20  class CodeGenDAGPatterns;
21  class Matcher;
22  class PatternToMatch;
23  class raw_ostream;
24  class ComplexPattern;
25  class Record;
26
27Matcher *ConvertPatternToMatcher(const PatternToMatch &Pattern,
28                                 const CodeGenDAGPatterns &CGP);
29Matcher *OptimizeMatcher(Matcher *Matcher);
30void EmitMatcherTable(const Matcher *Matcher, raw_ostream &OS);
31
32
33/// Matcher - Base class for all the the DAG ISel Matcher representation
34/// nodes.
35class Matcher {
36  // The next matcher node that is executed after this one.  Null if this is the
37  // last stage of a match.
38  OwningPtr<Matcher> Next;
39public:
40  enum KindTy {
41    // Matcher state manipulation.
42    Scope,                // Push a checking scope.
43    RecordNode,           // Record the current node.
44    RecordChild,          // Record a child of the current node.
45    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.
49
50    // Predicate checking.
51    CheckSame,            // Fail if not same as prev match.
52    CheckPatternPredicate,
53    CheckPredicate,       // Fail if node predicate fails.
54    CheckOpcode,          // Fail if not opcode.
55    CheckMultiOpcode,     // Fail if not in opcode list.
56    CheckType,            // Fail if not correct type.
57    CheckChildType,       // Fail if child has wrong type.
58    CheckInteger,         // Fail if wrong val.
59    CheckCondCode,        // Fail if not condcode.
60    CheckValueType,
61    CheckComplexPat,
62    CheckAndImm,
63    CheckOrImm,
64    CheckFoldableChainNode,
65    CheckChainCompatible,
66
67    // Node creation/emisssion.
68    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
76    MarkFlagResults,      // Indicate which interior nodes have flag results.
77    CompleteMatch         // Finish a match and update the results.
78  };
79  const KindTy Kind;
80
81protected:
82  Matcher(KindTy K) : Kind(K) {}
83public:
84  virtual ~Matcher() {}
85
86  KindTy getKind() const { return Kind; }
87
88  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(); }
92
93  OwningPtr<Matcher> &getNextPtr() { return Next; }
94
95  static inline bool classof(const Matcher *) { return true; }
96
97  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
106  virtual void print(raw_ostream &OS, unsigned indent = 0) const = 0;
107  void dump() const;
108protected:
109  void printNext(raw_ostream &OS, unsigned indent) const;
110  virtual bool isEqualImpl(const Matcher *M) const = 0;
111  virtual unsigned getHashImpl() const = 0;
112};
113
114/// ScopeMatcher - This pushes a failure scope on the stack and evaluates
115/// 'Check'.  If 'Check' fails to match, it pops its scope and continues on to
116/// 'Next'.
117class ScopeMatcher : public Matcher {
118  OwningPtr<Matcher> Check;
119public:
120  ScopeMatcher(Matcher *check = 0, Matcher *next = 0)
121    : Matcher(Scope), Check(check) {
122    setNext(next);
123  }
124
125  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; }
129
130  static inline bool classof(const Matcher *N) {
131    return N->getKind() == Scope;
132  }
133
134private:
135  virtual void print(raw_ostream &OS, unsigned indent = 0) const;
136  virtual bool isEqualImpl(const Matcher *M) const { return false; }
137  virtual unsigned getHashImpl() const { return 0; }
138};
139
140/// RecordMatcher - Save the current node in the operand list.
141class RecordMatcher : public Matcher {
142  /// 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;
145public:
146  RecordMatcher(const std::string &whatfor)
147    : Matcher(RecordNode), WhatFor(whatfor) {}
148
149  const std::string &getWhatFor() const { return WhatFor; }
150
151  static inline bool classof(const Matcher *N) {
152    return N->getKind() == RecordNode;
153  }
154
155private:
156  virtual void print(raw_ostream &OS, unsigned indent = 0) const;
157  virtual bool isEqualImpl(const Matcher *M) const { return true; }
158  virtual unsigned getHashImpl() const { return 0; }
159};
160
161/// RecordChildMatcher - Save a numbered child of the current node, or fail
162/// the match if it doesn't exist.  This is logically equivalent to:
163///    MoveChild N + RecordNode + MoveParent.
164class RecordChildMatcher : public Matcher {
165  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:
171  RecordChildMatcher(unsigned childno, const std::string &whatfor)
172  : Matcher(RecordChild), ChildNo(childno), WhatFor(whatfor) {}
173
174  unsigned getChildNo() const { return ChildNo; }
175  const std::string &getWhatFor() const { return WhatFor; }
176
177  static inline bool classof(const Matcher *N) {
178    return N->getKind() == RecordChild;
179  }
180
181private:
182  virtual void print(raw_ostream &OS, unsigned indent = 0) const;
183  virtual bool isEqualImpl(const Matcher *M) const {
184    return cast<RecordChildMatcher>(M)->getChildNo() == getChildNo();
185  }
186  virtual unsigned getHashImpl() const { return getChildNo(); }
187};
188
189/// RecordMemRefMatcher - Save the current node's memref.
190class RecordMemRefMatcher : public Matcher {
191public:
192  RecordMemRefMatcher() : Matcher(RecordMemRef) {}
193
194  static inline bool classof(const Matcher *N) {
195    return N->getKind() == RecordMemRef;
196  }
197
198private:
199  virtual void print(raw_ostream &OS, unsigned indent = 0) const;
200  virtual bool isEqualImpl(const Matcher *M) const { return true; }
201  virtual unsigned getHashImpl() const { return 0; }
202};
203
204
205/// CaptureFlagInputMatcher - If the current record has a flag input, record
206/// it so that it is used as an input to the generated code.
207class CaptureFlagInputMatcher : public Matcher {
208public:
209  CaptureFlagInputMatcher() : Matcher(CaptureFlagInput) {}
210
211  static inline bool classof(const Matcher *N) {
212    return N->getKind() == CaptureFlagInput;
213  }
214
215private:
216  virtual void print(raw_ostream &OS, unsigned indent = 0) const;
217  virtual bool isEqualImpl(const Matcher *M) const { return true; }
218  virtual unsigned getHashImpl() const { return 0; }
219};
220
221/// MoveChildMatcher - This tells the interpreter to move into the
222/// specified child node.
223class MoveChildMatcher : public Matcher {
224  unsigned ChildNo;
225public:
226  MoveChildMatcher(unsigned childNo) : Matcher(MoveChild), ChildNo(childNo) {}
227
228  unsigned getChildNo() const { return ChildNo; }
229
230  static inline bool classof(const Matcher *N) {
231    return N->getKind() == MoveChild;
232  }
233
234private:
235  virtual void print(raw_ostream &OS, unsigned indent = 0) const;
236  virtual bool isEqualImpl(const Matcher *M) const {
237    return cast<MoveChildMatcher>(M)->getChildNo() == getChildNo();
238  }
239  virtual unsigned getHashImpl() const { return getChildNo(); }
240};
241
242/// MoveParentMatcher - This tells the interpreter to move to the parent
243/// of the current node.
244class MoveParentMatcher : public Matcher {
245public:
246  MoveParentMatcher() : Matcher(MoveParent) {}
247
248  static inline bool classof(const Matcher *N) {
249    return N->getKind() == MoveParent;
250  }
251
252private:
253  virtual void print(raw_ostream &OS, unsigned indent = 0) const;
254  virtual bool isEqualImpl(const Matcher *M) const { return true; }
255  virtual unsigned getHashImpl() const { return 0; }
256};
257
258/// CheckSameMatcher - This checks to see if this node is exactly the same
259/// 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)'.
261class CheckSameMatcher : public Matcher {
262  unsigned MatchNumber;
263public:
264  CheckSameMatcher(unsigned matchnumber)
265    : Matcher(CheckSame), MatchNumber(matchnumber) {}
266
267  unsigned getMatchNumber() const { return MatchNumber; }
268
269  static inline bool classof(const Matcher *N) {
270    return N->getKind() == CheckSame;
271  }
272
273private:
274  virtual void print(raw_ostream &OS, unsigned indent = 0) const;
275  virtual bool isEqualImpl(const Matcher *M) const {
276    return cast<CheckSameMatcher>(M)->getMatchNumber() == getMatchNumber();
277  }
278  virtual unsigned getHashImpl() const { return getMatchNumber(); }
279};
280
281/// CheckPatternPredicateMatcher - This checks the target-specific predicate
282/// 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.
284class CheckPatternPredicateMatcher : public Matcher {
285  std::string Predicate;
286public:
287  CheckPatternPredicateMatcher(StringRef predicate)
288    : Matcher(CheckPatternPredicate), Predicate(predicate) {}
289
290  StringRef getPredicate() const { return Predicate; }
291
292  static inline bool classof(const Matcher *N) {
293    return N->getKind() == CheckPatternPredicate;
294  }
295
296private:
297  virtual void print(raw_ostream &OS, unsigned indent = 0) const;
298  virtual bool isEqualImpl(const Matcher *M) const {
299    return cast<CheckPatternPredicateMatcher>(M)->getPredicate() == Predicate;
300  }
301  virtual unsigned getHashImpl() const;
302};
303
304/// CheckPredicateMatcher - This checks the target-specific predicate to
305/// see if the node is acceptable.
306class CheckPredicateMatcher : public Matcher {
307  StringRef PredName;
308public:
309  CheckPredicateMatcher(StringRef predname)
310    : Matcher(CheckPredicate), PredName(predname) {}
311
312  StringRef getPredicateName() const { return PredName; }
313
314  static inline bool classof(const Matcher *N) {
315    return N->getKind() == CheckPredicate;
316  }
317
318private:
319  virtual void print(raw_ostream &OS, unsigned indent = 0) const;
320  virtual bool isEqualImpl(const Matcher *M) const {
321    return cast<CheckPredicateMatcher>(M)->PredName == PredName;
322  }
323  virtual unsigned getHashImpl() const;
324};
325
326
327/// CheckOpcodeMatcher - This checks to see if the current node has the
328/// specified opcode, if not it fails to match.
329class CheckOpcodeMatcher : public Matcher {
330  StringRef OpcodeName;
331public:
332  CheckOpcodeMatcher(StringRef opcodename)
333    : Matcher(CheckOpcode), OpcodeName(opcodename) {}
334
335  StringRef getOpcodeName() const { return OpcodeName; }
336
337  static inline bool classof(const Matcher *N) {
338    return N->getKind() == CheckOpcode;
339  }
340
341private:
342  virtual void print(raw_ostream &OS, unsigned indent = 0) const;
343  virtual bool isEqualImpl(const Matcher *M) const {
344    return cast<CheckOpcodeMatcher>(M)->OpcodeName == OpcodeName;
345  }
346  virtual unsigned getHashImpl() const;
347};
348
349/// CheckMultiOpcodeMatcher - This checks to see if the current node has one
350/// of the specified opcode, if not it fails to match.
351class CheckMultiOpcodeMatcher : public Matcher {
352  SmallVector<StringRef, 4> OpcodeNames;
353public:
354  CheckMultiOpcodeMatcher(const StringRef *opcodes, unsigned numops)
355    : Matcher(CheckMultiOpcode), OpcodeNames(opcodes, opcodes+numops) {}
356
357  unsigned getNumOpcodeNames() const { return OpcodeNames.size(); }
358  StringRef getOpcodeName(unsigned i) const { return OpcodeNames[i]; }
359
360  static inline bool classof(const Matcher *N) {
361    return N->getKind() == CheckMultiOpcode;
362  }
363
364private:
365  virtual void print(raw_ostream &OS, unsigned indent = 0) const;
366  virtual bool isEqualImpl(const Matcher *M) const {
367    return cast<CheckMultiOpcodeMatcher>(M)->OpcodeNames == OpcodeNames;
368  }
369  virtual unsigned getHashImpl() const;
370};
371
372
373
374/// CheckTypeMatcher - This checks to see if the current node has the
375/// specified type, if not it fails to match.
376class CheckTypeMatcher : public Matcher {
377  MVT::SimpleValueType Type;
378public:
379  CheckTypeMatcher(MVT::SimpleValueType type)
380    : Matcher(CheckType), Type(type) {}
381
382  MVT::SimpleValueType getType() const { return Type; }
383
384  static inline bool classof(const Matcher *N) {
385    return N->getKind() == CheckType;
386  }
387
388private:
389  virtual void print(raw_ostream &OS, unsigned indent = 0) const;
390  virtual bool isEqualImpl(const Matcher *M) const {
391    return cast<CheckTypeMatcher>(this)->Type == Type;
392  }
393  virtual unsigned getHashImpl() const { return Type; }
394};
395
396/// CheckChildTypeMatcher - This checks to see if a child node has the
397/// specified type, if not it fails to match.
398class CheckChildTypeMatcher : public Matcher {
399  unsigned ChildNo;
400  MVT::SimpleValueType Type;
401public:
402  CheckChildTypeMatcher(unsigned childno, MVT::SimpleValueType type)
403    : Matcher(CheckChildType), ChildNo(childno), Type(type) {}
404
405  unsigned getChildNo() const { return ChildNo; }
406  MVT::SimpleValueType getType() const { return Type; }
407
408  static inline bool classof(const Matcher *N) {
409    return N->getKind() == CheckChildType;
410  }
411
412private:
413  virtual void print(raw_ostream &OS, unsigned indent = 0) const;
414  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; }
419};
420
421
422/// CheckIntegerMatcher - This checks to see if the current node is a
423/// ConstantSDNode with the specified integer value, if not it fails to match.
424class CheckIntegerMatcher : public Matcher {
425  int64_t Value;
426public:
427  CheckIntegerMatcher(int64_t value)
428    : Matcher(CheckInteger), Value(value) {}
429
430  int64_t getValue() const { return Value; }
431
432  static inline bool classof(const Matcher *N) {
433    return N->getKind() == CheckInteger;
434  }
435
436private:
437  virtual void print(raw_ostream &OS, unsigned indent = 0) const;
438  virtual bool isEqualImpl(const Matcher *M) const {
439    return cast<CheckIntegerMatcher>(M)->Value == Value;
440  }
441  virtual unsigned getHashImpl() const { return Value; }
442};
443
444/// CheckCondCodeMatcher - This checks to see if the current node is a
445/// CondCodeSDNode with the specified condition, if not it fails to match.
446class CheckCondCodeMatcher : public Matcher {
447  StringRef CondCodeName;
448public:
449  CheckCondCodeMatcher(StringRef condcodename)
450    : Matcher(CheckCondCode), CondCodeName(condcodename) {}
451
452  StringRef getCondCodeName() const { return CondCodeName; }
453
454  static inline bool classof(const Matcher *N) {
455    return N->getKind() == CheckCondCode;
456  }
457
458private:
459  virtual void print(raw_ostream &OS, unsigned indent = 0) const;
460  virtual bool isEqualImpl(const Matcher *M) const {
461    return cast<CheckCondCodeMatcher>(M)->CondCodeName == CondCodeName;
462  }
463  virtual unsigned getHashImpl() const;
464};
465
466/// CheckValueTypeMatcher - This checks to see if the current node is a
467/// VTSDNode with the specified type, if not it fails to match.
468class CheckValueTypeMatcher : public Matcher {
469  StringRef TypeName;
470public:
471  CheckValueTypeMatcher(StringRef type_name)
472    : Matcher(CheckValueType), TypeName(type_name) {}
473
474  StringRef getTypeName() const { return TypeName; }
475
476  static inline bool classof(const Matcher *N) {
477    return N->getKind() == CheckValueType;
478  }
479
480private:
481  virtual void print(raw_ostream &OS, unsigned indent = 0) const;
482  virtual bool isEqualImpl(const Matcher *M) const {
483    return cast<CheckValueTypeMatcher>(M)->TypeName == TypeName;
484  }
485  virtual unsigned getHashImpl() const;
486};
487
488
489
490/// CheckComplexPatMatcher - This node runs the specified ComplexPattern on
491/// the current node.
492class CheckComplexPatMatcher : public Matcher {
493  const ComplexPattern &Pattern;
494public:
495  CheckComplexPatMatcher(const ComplexPattern &pattern)
496    : Matcher(CheckComplexPat), Pattern(pattern) {}
497
498  const ComplexPattern &getPattern() const { return Pattern; }
499
500  static inline bool classof(const Matcher *N) {
501    return N->getKind() == CheckComplexPat;
502  }
503
504private:
505  virtual void print(raw_ostream &OS, unsigned indent = 0) const;
506  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  }
512};
513
514/// CheckAndImmMatcher - This checks to see if the current node is an 'and'
515/// with something equivalent to the specified immediate.
516class CheckAndImmMatcher : public Matcher {
517  int64_t Value;
518public:
519  CheckAndImmMatcher(int64_t value)
520    : Matcher(CheckAndImm), Value(value) {}
521
522  int64_t getValue() const { return Value; }
523
524  static inline bool classof(const Matcher *N) {
525    return N->getKind() == CheckAndImm;
526  }
527
528private:
529  virtual void print(raw_ostream &OS, unsigned indent = 0) const;
530  virtual bool isEqualImpl(const Matcher *M) const {
531    return cast<CheckAndImmMatcher>(M)->Value == Value;
532  }
533  virtual unsigned getHashImpl() const { return Value; }
534};
535
536/// CheckOrImmMatcher - This checks to see if the current node is an 'and'
537/// with something equivalent to the specified immediate.
538class CheckOrImmMatcher : public Matcher {
539  int64_t Value;
540public:
541  CheckOrImmMatcher(int64_t value)
542    : Matcher(CheckOrImm), Value(value) {}
543
544  int64_t getValue() const { return Value; }
545
546  static inline bool classof(const Matcher *N) {
547    return N->getKind() == CheckOrImm;
548  }
549
550private:
551  virtual void print(raw_ostream &OS, unsigned indent = 0) const;
552  virtual bool isEqualImpl(const Matcher *M) const {
553    return cast<CheckOrImmMatcher>(M)->Value == Value;
554  }
555  virtual unsigned getHashImpl() const { return Value; }
556};
557
558/// CheckFoldableChainNodeMatcher - This checks to see if the current node
559/// (which defines a chain operand) is safe to fold into a larger pattern.
560class CheckFoldableChainNodeMatcher : public Matcher {
561public:
562  CheckFoldableChainNodeMatcher()
563    : Matcher(CheckFoldableChainNode) {}
564
565  static inline bool classof(const Matcher *N) {
566    return N->getKind() == CheckFoldableChainNode;
567  }
568
569private:
570  virtual void print(raw_ostream &OS, unsigned indent = 0) const;
571  virtual bool isEqualImpl(const Matcher *M) const { return true; }
572  virtual unsigned getHashImpl() const { return 0; }
573};
574
575/// CheckChainCompatibleMatcher - Verify that the current node's chain
576/// operand is 'compatible' with the specified recorded node's.
577class CheckChainCompatibleMatcher : public Matcher {
578  unsigned PreviousOp;
579public:
580  CheckChainCompatibleMatcher(unsigned previousop)
581    : Matcher(CheckChainCompatible), PreviousOp(previousop) {}
582
583  unsigned getPreviousOp() const { return PreviousOp; }
584
585  static inline bool classof(const Matcher *N) {
586    return N->getKind() == CheckChainCompatible;
587  }
588
589private:
590  virtual void print(raw_ostream &OS, unsigned indent = 0) const;
591  virtual bool isEqualImpl(const Matcher *M) const {
592    return cast<CheckChainCompatibleMatcher>(this)->PreviousOp == PreviousOp;
593  }
594  virtual unsigned getHashImpl() const { return PreviousOp; }
595};
596
597/// EmitIntegerMatcher - This creates a new TargetConstant.
598class EmitIntegerMatcher : public Matcher {
599  int64_t Val;
600  MVT::SimpleValueType VT;
601public:
602  EmitIntegerMatcher(int64_t val, MVT::SimpleValueType vt)
603    : Matcher(EmitInteger), Val(val), VT(vt) {}
604
605  int64_t getValue() const { return Val; }
606  MVT::SimpleValueType getVT() const { return VT; }
607
608  static inline bool classof(const Matcher *N) {
609    return N->getKind() == EmitInteger;
610  }
611
612private:
613  virtual void print(raw_ostream &OS, unsigned indent = 0) const;
614  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; }
619};
620
621/// EmitStringIntegerMatcher - A target constant whose value is represented
622/// by a string.
623class EmitStringIntegerMatcher : public Matcher {
624  std::string Val;
625  MVT::SimpleValueType VT;
626public:
627  EmitStringIntegerMatcher(const std::string &val, MVT::SimpleValueType vt)
628    : Matcher(EmitStringInteger), Val(val), VT(vt) {}
629
630  const std::string &getValue() const { return Val; }
631  MVT::SimpleValueType getVT() const { return VT; }
632
633  static inline bool classof(const Matcher *N) {
634    return N->getKind() == EmitStringInteger;
635  }
636
637private:
638  virtual void print(raw_ostream &OS, unsigned indent = 0) const;
639  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;
644};
645
646/// EmitRegisterMatcher - This creates a new TargetConstant.
647class EmitRegisterMatcher : public Matcher {
648  /// 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:
653  EmitRegisterMatcher(Record *reg, MVT::SimpleValueType vt)
654    : Matcher(EmitRegister), Reg(reg), VT(vt) {}
655
656  Record *getReg() const { return Reg; }
657  MVT::SimpleValueType getVT() const { return VT; }
658
659  static inline bool classof(const Matcher *N) {
660    return N->getKind() == EmitRegister;
661  }
662
663private:
664  virtual void print(raw_ostream &OS, unsigned indent = 0) const;
665  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  }
672};
673
674/// EmitConvertToTargetMatcher - Emit an operation that reads a specified
675/// recorded node and converts it from being a ISD::Constant to
676/// ISD::TargetConstant, likewise for ConstantFP.
677class EmitConvertToTargetMatcher : public Matcher {
678  unsigned Slot;
679public:
680  EmitConvertToTargetMatcher(unsigned slot)
681    : Matcher(EmitConvertToTarget), Slot(slot) {}
682
683  unsigned getSlot() const { return Slot; }
684
685  static inline bool classof(const Matcher *N) {
686    return N->getKind() == EmitConvertToTarget;
687  }
688
689private:
690  virtual void print(raw_ostream &OS, unsigned indent = 0) const;
691  virtual bool isEqualImpl(const Matcher *M) const {
692    return cast<EmitConvertToTargetMatcher>(M)->Slot == Slot;
693  }
694  virtual unsigned getHashImpl() const { return Slot; }
695};
696
697/// EmitMergeInputChainsMatcher - Emit a node that merges a list of input
698/// 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.
701class EmitMergeInputChainsMatcher : public Matcher {
702  SmallVector<unsigned, 3> ChainNodes;
703public:
704  EmitMergeInputChainsMatcher(const unsigned *nodes, unsigned NumNodes)
705    : Matcher(EmitMergeInputChains), ChainNodes(nodes, nodes+NumNodes) {}
706
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
714  static inline bool classof(const Matcher *N) {
715    return N->getKind() == EmitMergeInputChains;
716  }
717
718private:
719  virtual void print(raw_ostream &OS, unsigned indent = 0) const;
720  virtual bool isEqualImpl(const Matcher *M) const {
721    return cast<EmitMergeInputChainsMatcher>(M)->ChainNodes == ChainNodes;
722  }
723  virtual unsigned getHashImpl() const;
724};
725
726/// EmitCopyToRegMatcher - Emit a CopyToReg node from a value to a physreg,
727/// pushing the chain and flag results.
728///
729class EmitCopyToRegMatcher : public Matcher {
730  unsigned SrcSlot; // Value to copy into the physreg.
731  Record *DestPhysReg;
732public:
733  EmitCopyToRegMatcher(unsigned srcSlot, Record *destPhysReg)
734    : Matcher(EmitCopyToReg), SrcSlot(srcSlot), DestPhysReg(destPhysReg) {}
735
736  unsigned getSrcSlot() const { return SrcSlot; }
737  Record *getDestPhysReg() const { return DestPhysReg; }
738
739  static inline bool classof(const Matcher *N) {
740    return N->getKind() == EmitCopyToReg;
741  }
742
743private:
744  virtual void print(raw_ostream &OS, unsigned indent = 0) const;
745  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  }
752};
753
754
755
756/// EmitNodeXFormMatcher - Emit an operation that runs an SDNodeXForm on a
757/// recorded node and records the result.
758class EmitNodeXFormMatcher : public Matcher {
759  unsigned Slot;
760  Record *NodeXForm;
761public:
762  EmitNodeXFormMatcher(unsigned slot, Record *nodeXForm)
763    : Matcher(EmitNodeXForm), Slot(slot), NodeXForm(nodeXForm) {}
764
765  unsigned getSlot() const { return Slot; }
766  Record *getNodeXForm() const { return NodeXForm; }
767
768  static inline bool classof(const Matcher *N) {
769    return N->getKind() == EmitNodeXForm;
770  }
771
772private:
773  virtual void print(raw_ostream &OS, unsigned indent = 0) const;
774  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  }
781};
782
783/// EmitNodeMatcher - This signals a successful match and generates a node.
784class EmitNodeMatcher : public Matcher {
785  std::string OpcodeName;
786  const SmallVector<MVT::SimpleValueType, 3> VTs;
787  const SmallVector<unsigned, 6> Operands;
788  bool HasChain, HasFlag, HasMemRefs;
789
790  /// 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:
795  EmitNodeMatcher(const std::string &opcodeName,
796                  const MVT::SimpleValueType *vts, unsigned numvts,
797                  const unsigned *operands, unsigned numops,
798                  bool hasChain, bool hasFlag, bool hasmemrefs,
799                  int numfixedarityoperands)
800    : Matcher(EmitNode), OpcodeName(opcodeName),
801      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; }
823
824  static inline bool classof(const Matcher *N) {
825    return N->getKind() == EmitNode;
826  }
827
828private:
829  virtual void print(raw_ostream &OS, unsigned indent = 0) const;
830  virtual bool isEqualImpl(const Matcher *M) const;
831  virtual unsigned getHashImpl() const;
832};
833
834/// MarkFlagResultsMatcher - This node indicates which non-root nodes in the
835/// pattern produce flags.  This allows CompleteMatchMatcher to update them
836/// with the output flag of the resultant code.
837class MarkFlagResultsMatcher : public Matcher {
838  SmallVector<unsigned, 3> FlagResultNodes;
839public:
840  MarkFlagResultsMatcher(const unsigned *nodes, unsigned NumNodes)
841    : Matcher(MarkFlagResults), FlagResultNodes(nodes, nodes+NumNodes) {}
842
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
850  static inline bool classof(const Matcher *N) {
851    return N->getKind() == MarkFlagResults;
852  }
853
854private:
855  virtual void print(raw_ostream &OS, unsigned indent = 0) const;
856  virtual bool isEqualImpl(const Matcher *M) const {
857    return cast<MarkFlagResultsMatcher>(M)->FlagResultNodes == FlagResultNodes;
858  }
859  virtual unsigned getHashImpl() const;
860};
861
862/// CompleteMatchMatcher - Complete a match by replacing the results of the
863/// pattern with the newly generated nodes.  This also prints a comment
864/// indicating the source and dest patterns.
865class CompleteMatchMatcher : public Matcher {
866  SmallVector<unsigned, 2> Results;
867  const PatternToMatch &Pattern;
868public:
869  CompleteMatchMatcher(const unsigned *results, unsigned numresults,
870                           const PatternToMatch &pattern)
871  : Matcher(CompleteMatch), Results(results, results+numresults),
872    Pattern(pattern) {}
873
874  unsigned getNumResults() const { return Results.size(); }
875  unsigned getResult(unsigned R) const { return Results[R]; }
876  const PatternToMatch &getPattern() const { return Pattern; }
877
878  static inline bool classof(const Matcher *N) {
879    return N->getKind() == CompleteMatch;
880  }
881
882private:
883  virtual void print(raw_ostream &OS, unsigned indent = 0) const;
884  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;
889};
890
891} // end namespace llvm
892
893#endif
894