DAGISelMatcher.h revision bb08d89298630834c41c40841ee3df5d3f830ce2
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    // Clear the high bit so we don't conflict with tombstones etc.
104    return ((getHashImpl() << 4) ^ getKind()) & (~0U>>1);
105  }
106
107  void print(raw_ostream &OS, unsigned indent = 0) const;
108  void dump() const;
109protected:
110  virtual void printImpl(raw_ostream &OS, unsigned indent) const = 0;
111  virtual bool isEqualImpl(const Matcher *M) const = 0;
112  virtual unsigned getHashImpl() const = 0;
113};
114
115/// ScopeMatcher - This attempts to match each of its children to find the first
116/// one that successfully matches.  If one child fails, it tries the next child.
117/// If none of the children match then this check fails.  It never has a 'next'.
118class ScopeMatcher : public Matcher {
119  SmallVector<Matcher*, 4> Children;
120public:
121  ScopeMatcher(Matcher *const *children, unsigned numchildren)
122    : Matcher(Scope), Children(children, children+numchildren) {
123  }
124  virtual ~ScopeMatcher();
125
126  unsigned getNumChildren() const { return Children.size(); }
127
128  Matcher *getChild(unsigned i) { return Children[i]; }
129  const Matcher *getChild(unsigned i) const { return Children[i]; }
130
131  void resetChild(unsigned i, Matcher *N) {
132    delete Children[i];
133    Children[i] = N;
134  }
135
136  Matcher *takeChild(unsigned i) {
137    Matcher *Res = Children[i];
138    Children[i] = 0;
139    return Res;
140  }
141
142  void setNumChildren(unsigned NC) {
143    if (NC < Children.size()) {
144      // delete any children we're about to lose pointers to.
145      for (unsigned i = NC, e = Children.size(); i != e; ++i)
146        delete Children[i];
147    }
148    Children.resize(NC);
149  }
150
151  static inline bool classof(const Matcher *N) {
152    return N->getKind() == Scope;
153  }
154
155private:
156  virtual void printImpl(raw_ostream &OS, unsigned indent) const;
157  virtual bool isEqualImpl(const Matcher *M) const { return false; }
158  virtual unsigned getHashImpl() const { return 12312; }
159};
160
161/// RecordMatcher - Save the current node in the operand list.
162class RecordMatcher : public Matcher {
163  /// WhatFor - This is a string indicating why we're recording this.  This
164  /// should only be used for comment generation not anything semantic.
165  std::string WhatFor;
166public:
167  RecordMatcher(const std::string &whatfor)
168    : Matcher(RecordNode), WhatFor(whatfor) {}
169
170  const std::string &getWhatFor() const { return WhatFor; }
171
172  static inline bool classof(const Matcher *N) {
173    return N->getKind() == RecordNode;
174  }
175
176private:
177  virtual void printImpl(raw_ostream &OS, unsigned indent) const;
178  virtual bool isEqualImpl(const Matcher *M) const { return true; }
179  virtual unsigned getHashImpl() const { return 0; }
180};
181
182/// RecordChildMatcher - Save a numbered child of the current node, or fail
183/// the match if it doesn't exist.  This is logically equivalent to:
184///    MoveChild N + RecordNode + MoveParent.
185class RecordChildMatcher : public Matcher {
186  unsigned ChildNo;
187
188  /// WhatFor - This is a string indicating why we're recording this.  This
189  /// should only be used for comment generation not anything semantic.
190  std::string WhatFor;
191public:
192  RecordChildMatcher(unsigned childno, const std::string &whatfor)
193  : Matcher(RecordChild), ChildNo(childno), WhatFor(whatfor) {}
194
195  unsigned getChildNo() const { return ChildNo; }
196  const std::string &getWhatFor() const { return WhatFor; }
197
198  static inline bool classof(const Matcher *N) {
199    return N->getKind() == RecordChild;
200  }
201
202private:
203  virtual void printImpl(raw_ostream &OS, unsigned indent) const;
204  virtual bool isEqualImpl(const Matcher *M) const {
205    return cast<RecordChildMatcher>(M)->getChildNo() == getChildNo();
206  }
207  virtual unsigned getHashImpl() const { return getChildNo(); }
208};
209
210/// RecordMemRefMatcher - Save the current node's memref.
211class RecordMemRefMatcher : public Matcher {
212public:
213  RecordMemRefMatcher() : Matcher(RecordMemRef) {}
214
215  static inline bool classof(const Matcher *N) {
216    return N->getKind() == RecordMemRef;
217  }
218
219private:
220  virtual void printImpl(raw_ostream &OS, unsigned indent) const;
221  virtual bool isEqualImpl(const Matcher *M) const { return true; }
222  virtual unsigned getHashImpl() const { return 0; }
223};
224
225
226/// CaptureFlagInputMatcher - If the current record has a flag input, record
227/// it so that it is used as an input to the generated code.
228class CaptureFlagInputMatcher : public Matcher {
229public:
230  CaptureFlagInputMatcher() : Matcher(CaptureFlagInput) {}
231
232  static inline bool classof(const Matcher *N) {
233    return N->getKind() == CaptureFlagInput;
234  }
235
236private:
237  virtual void printImpl(raw_ostream &OS, unsigned indent) const;
238  virtual bool isEqualImpl(const Matcher *M) const { return true; }
239  virtual unsigned getHashImpl() const { return 0; }
240};
241
242/// MoveChildMatcher - This tells the interpreter to move into the
243/// specified child node.
244class MoveChildMatcher : public Matcher {
245  unsigned ChildNo;
246public:
247  MoveChildMatcher(unsigned childNo) : Matcher(MoveChild), ChildNo(childNo) {}
248
249  unsigned getChildNo() const { return ChildNo; }
250
251  static inline bool classof(const Matcher *N) {
252    return N->getKind() == MoveChild;
253  }
254
255private:
256  virtual void printImpl(raw_ostream &OS, unsigned indent) const;
257  virtual bool isEqualImpl(const Matcher *M) const {
258    return cast<MoveChildMatcher>(M)->getChildNo() == getChildNo();
259  }
260  virtual unsigned getHashImpl() const { return getChildNo(); }
261};
262
263/// MoveParentMatcher - This tells the interpreter to move to the parent
264/// of the current node.
265class MoveParentMatcher : public Matcher {
266public:
267  MoveParentMatcher() : Matcher(MoveParent) {}
268
269  static inline bool classof(const Matcher *N) {
270    return N->getKind() == MoveParent;
271  }
272
273private:
274  virtual void printImpl(raw_ostream &OS, unsigned indent) const;
275  virtual bool isEqualImpl(const Matcher *M) const { return true; }
276  virtual unsigned getHashImpl() const { return 0; }
277};
278
279/// CheckSameMatcher - This checks to see if this node is exactly the same
280/// node as the specified match that was recorded with 'Record'.  This is used
281/// when patterns have the same name in them, like '(mul GPR:$in, GPR:$in)'.
282class CheckSameMatcher : public Matcher {
283  unsigned MatchNumber;
284public:
285  CheckSameMatcher(unsigned matchnumber)
286    : Matcher(CheckSame), MatchNumber(matchnumber) {}
287
288  unsigned getMatchNumber() const { return MatchNumber; }
289
290  static inline bool classof(const Matcher *N) {
291    return N->getKind() == CheckSame;
292  }
293
294private:
295  virtual void printImpl(raw_ostream &OS, unsigned indent) const;
296  virtual bool isEqualImpl(const Matcher *M) const {
297    return cast<CheckSameMatcher>(M)->getMatchNumber() == getMatchNumber();
298  }
299  virtual unsigned getHashImpl() const { return getMatchNumber(); }
300};
301
302/// CheckPatternPredicateMatcher - This checks the target-specific predicate
303/// to see if the entire pattern is capable of matching.  This predicate does
304/// not take a node as input.  This is used for subtarget feature checks etc.
305class CheckPatternPredicateMatcher : public Matcher {
306  std::string Predicate;
307public:
308  CheckPatternPredicateMatcher(StringRef predicate)
309    : Matcher(CheckPatternPredicate), Predicate(predicate) {}
310
311  StringRef getPredicate() const { return Predicate; }
312
313  static inline bool classof(const Matcher *N) {
314    return N->getKind() == CheckPatternPredicate;
315  }
316
317private:
318  virtual void printImpl(raw_ostream &OS, unsigned indent) const;
319  virtual bool isEqualImpl(const Matcher *M) const {
320    return cast<CheckPatternPredicateMatcher>(M)->getPredicate() == Predicate;
321  }
322  virtual unsigned getHashImpl() const;
323};
324
325/// CheckPredicateMatcher - This checks the target-specific predicate to
326/// see if the node is acceptable.
327class CheckPredicateMatcher : public Matcher {
328  StringRef PredName;
329public:
330  CheckPredicateMatcher(StringRef predname)
331    : Matcher(CheckPredicate), PredName(predname) {}
332
333  StringRef getPredicateName() const { return PredName; }
334
335  static inline bool classof(const Matcher *N) {
336    return N->getKind() == CheckPredicate;
337  }
338
339private:
340  virtual void printImpl(raw_ostream &OS, unsigned indent) const;
341  virtual bool isEqualImpl(const Matcher *M) const {
342    return cast<CheckPredicateMatcher>(M)->PredName == PredName;
343  }
344  virtual unsigned getHashImpl() const;
345};
346
347
348/// CheckOpcodeMatcher - This checks to see if the current node has the
349/// specified opcode, if not it fails to match.
350class CheckOpcodeMatcher : public Matcher {
351  StringRef OpcodeName;
352public:
353  CheckOpcodeMatcher(StringRef opcodename)
354    : Matcher(CheckOpcode), OpcodeName(opcodename) {}
355
356  StringRef getOpcodeName() const { return OpcodeName; }
357
358  static inline bool classof(const Matcher *N) {
359    return N->getKind() == CheckOpcode;
360  }
361
362private:
363  virtual void printImpl(raw_ostream &OS, unsigned indent) const;
364  virtual bool isEqualImpl(const Matcher *M) const {
365    return cast<CheckOpcodeMatcher>(M)->OpcodeName == OpcodeName;
366  }
367  virtual unsigned getHashImpl() const;
368};
369
370/// CheckMultiOpcodeMatcher - This checks to see if the current node has one
371/// of the specified opcode, if not it fails to match.
372class CheckMultiOpcodeMatcher : public Matcher {
373  SmallVector<StringRef, 4> OpcodeNames;
374public:
375  CheckMultiOpcodeMatcher(const StringRef *opcodes, unsigned numops)
376    : Matcher(CheckMultiOpcode), OpcodeNames(opcodes, opcodes+numops) {}
377
378  unsigned getNumOpcodeNames() const { return OpcodeNames.size(); }
379  StringRef getOpcodeName(unsigned i) const { return OpcodeNames[i]; }
380
381  static inline bool classof(const Matcher *N) {
382    return N->getKind() == CheckMultiOpcode;
383  }
384
385private:
386  virtual void printImpl(raw_ostream &OS, unsigned indent) const;
387  virtual bool isEqualImpl(const Matcher *M) const {
388    return cast<CheckMultiOpcodeMatcher>(M)->OpcodeNames == OpcodeNames;
389  }
390  virtual unsigned getHashImpl() const;
391};
392
393
394
395/// CheckTypeMatcher - This checks to see if the current node has the
396/// specified type, if not it fails to match.
397class CheckTypeMatcher : public Matcher {
398  MVT::SimpleValueType Type;
399public:
400  CheckTypeMatcher(MVT::SimpleValueType type)
401    : Matcher(CheckType), Type(type) {}
402
403  MVT::SimpleValueType getType() const { return Type; }
404
405  static inline bool classof(const Matcher *N) {
406    return N->getKind() == CheckType;
407  }
408
409private:
410  virtual void printImpl(raw_ostream &OS, unsigned indent) const;
411  virtual bool isEqualImpl(const Matcher *M) const {
412    return cast<CheckTypeMatcher>(M)->Type == Type;
413  }
414  virtual unsigned getHashImpl() const { return Type; }
415};
416
417/// CheckChildTypeMatcher - This checks to see if a child node has the
418/// specified type, if not it fails to match.
419class CheckChildTypeMatcher : public Matcher {
420  unsigned ChildNo;
421  MVT::SimpleValueType Type;
422public:
423  CheckChildTypeMatcher(unsigned childno, MVT::SimpleValueType type)
424    : Matcher(CheckChildType), ChildNo(childno), Type(type) {}
425
426  unsigned getChildNo() const { return ChildNo; }
427  MVT::SimpleValueType getType() const { return Type; }
428
429  static inline bool classof(const Matcher *N) {
430    return N->getKind() == CheckChildType;
431  }
432
433private:
434  virtual void printImpl(raw_ostream &OS, unsigned indent) const;
435  virtual bool isEqualImpl(const Matcher *M) const {
436    return cast<CheckChildTypeMatcher>(M)->ChildNo == ChildNo &&
437           cast<CheckChildTypeMatcher>(M)->Type == Type;
438  }
439  virtual unsigned getHashImpl() const { return (Type << 3) | ChildNo; }
440};
441
442
443/// CheckIntegerMatcher - This checks to see if the current node is a
444/// ConstantSDNode with the specified integer value, if not it fails to match.
445class CheckIntegerMatcher : public Matcher {
446  int64_t Value;
447public:
448  CheckIntegerMatcher(int64_t value)
449    : Matcher(CheckInteger), Value(value) {}
450
451  int64_t getValue() const { return Value; }
452
453  static inline bool classof(const Matcher *N) {
454    return N->getKind() == CheckInteger;
455  }
456
457private:
458  virtual void printImpl(raw_ostream &OS, unsigned indent) const;
459  virtual bool isEqualImpl(const Matcher *M) const {
460    return cast<CheckIntegerMatcher>(M)->Value == Value;
461  }
462  virtual unsigned getHashImpl() const { return Value; }
463};
464
465/// CheckCondCodeMatcher - This checks to see if the current node is a
466/// CondCodeSDNode with the specified condition, if not it fails to match.
467class CheckCondCodeMatcher : public Matcher {
468  StringRef CondCodeName;
469public:
470  CheckCondCodeMatcher(StringRef condcodename)
471    : Matcher(CheckCondCode), CondCodeName(condcodename) {}
472
473  StringRef getCondCodeName() const { return CondCodeName; }
474
475  static inline bool classof(const Matcher *N) {
476    return N->getKind() == CheckCondCode;
477  }
478
479private:
480  virtual void printImpl(raw_ostream &OS, unsigned indent) const;
481  virtual bool isEqualImpl(const Matcher *M) const {
482    return cast<CheckCondCodeMatcher>(M)->CondCodeName == CondCodeName;
483  }
484  virtual unsigned getHashImpl() const;
485};
486
487/// CheckValueTypeMatcher - This checks to see if the current node is a
488/// VTSDNode with the specified type, if not it fails to match.
489class CheckValueTypeMatcher : public Matcher {
490  StringRef TypeName;
491public:
492  CheckValueTypeMatcher(StringRef type_name)
493    : Matcher(CheckValueType), TypeName(type_name) {}
494
495  StringRef getTypeName() const { return TypeName; }
496
497  static inline bool classof(const Matcher *N) {
498    return N->getKind() == CheckValueType;
499  }
500
501private:
502  virtual void printImpl(raw_ostream &OS, unsigned indent) const;
503  virtual bool isEqualImpl(const Matcher *M) const {
504    return cast<CheckValueTypeMatcher>(M)->TypeName == TypeName;
505  }
506  virtual unsigned getHashImpl() const;
507};
508
509
510
511/// CheckComplexPatMatcher - This node runs the specified ComplexPattern on
512/// the current node.
513class CheckComplexPatMatcher : public Matcher {
514  const ComplexPattern &Pattern;
515public:
516  CheckComplexPatMatcher(const ComplexPattern &pattern)
517    : Matcher(CheckComplexPat), Pattern(pattern) {}
518
519  const ComplexPattern &getPattern() const { return Pattern; }
520
521  static inline bool classof(const Matcher *N) {
522    return N->getKind() == CheckComplexPat;
523  }
524
525private:
526  virtual void printImpl(raw_ostream &OS, unsigned indent) const;
527  virtual bool isEqualImpl(const Matcher *M) const {
528    return &cast<CheckComplexPatMatcher>(M)->Pattern == &Pattern;
529  }
530  virtual unsigned getHashImpl() const {
531    return (unsigned)(intptr_t)&Pattern;
532  }
533};
534
535/// CheckAndImmMatcher - This checks to see if the current node is an 'and'
536/// with something equivalent to the specified immediate.
537class CheckAndImmMatcher : public Matcher {
538  int64_t Value;
539public:
540  CheckAndImmMatcher(int64_t value)
541    : Matcher(CheckAndImm), Value(value) {}
542
543  int64_t getValue() const { return Value; }
544
545  static inline bool classof(const Matcher *N) {
546    return N->getKind() == CheckAndImm;
547  }
548
549private:
550  virtual void printImpl(raw_ostream &OS, unsigned indent) const;
551  virtual bool isEqualImpl(const Matcher *M) const {
552    return cast<CheckAndImmMatcher>(M)->Value == Value;
553  }
554  virtual unsigned getHashImpl() const { return Value; }
555};
556
557/// CheckOrImmMatcher - This checks to see if the current node is an 'and'
558/// with something equivalent to the specified immediate.
559class CheckOrImmMatcher : public Matcher {
560  int64_t Value;
561public:
562  CheckOrImmMatcher(int64_t value)
563    : Matcher(CheckOrImm), Value(value) {}
564
565  int64_t getValue() const { return Value; }
566
567  static inline bool classof(const Matcher *N) {
568    return N->getKind() == CheckOrImm;
569  }
570
571private:
572  virtual void printImpl(raw_ostream &OS, unsigned indent) const;
573  virtual bool isEqualImpl(const Matcher *M) const {
574    return cast<CheckOrImmMatcher>(M)->Value == Value;
575  }
576  virtual unsigned getHashImpl() const { return Value; }
577};
578
579/// CheckFoldableChainNodeMatcher - This checks to see if the current node
580/// (which defines a chain operand) is safe to fold into a larger pattern.
581class CheckFoldableChainNodeMatcher : public Matcher {
582public:
583  CheckFoldableChainNodeMatcher()
584    : Matcher(CheckFoldableChainNode) {}
585
586  static inline bool classof(const Matcher *N) {
587    return N->getKind() == CheckFoldableChainNode;
588  }
589
590private:
591  virtual void printImpl(raw_ostream &OS, unsigned indent) const;
592  virtual bool isEqualImpl(const Matcher *M) const { return true; }
593  virtual unsigned getHashImpl() const { return 0; }
594};
595
596/// CheckChainCompatibleMatcher - Verify that the current node's chain
597/// operand is 'compatible' with the specified recorded node's.
598class CheckChainCompatibleMatcher : public Matcher {
599  unsigned PreviousOp;
600public:
601  CheckChainCompatibleMatcher(unsigned previousop)
602    : Matcher(CheckChainCompatible), PreviousOp(previousop) {}
603
604  unsigned getPreviousOp() const { return PreviousOp; }
605
606  static inline bool classof(const Matcher *N) {
607    return N->getKind() == CheckChainCompatible;
608  }
609
610private:
611  virtual void printImpl(raw_ostream &OS, unsigned indent) const;
612  virtual bool isEqualImpl(const Matcher *M) const {
613    return cast<CheckChainCompatibleMatcher>(M)->PreviousOp == PreviousOp;
614  }
615  virtual unsigned getHashImpl() const { return PreviousOp; }
616};
617
618/// EmitIntegerMatcher - This creates a new TargetConstant.
619class EmitIntegerMatcher : public Matcher {
620  int64_t Val;
621  MVT::SimpleValueType VT;
622public:
623  EmitIntegerMatcher(int64_t val, MVT::SimpleValueType vt)
624    : Matcher(EmitInteger), Val(val), VT(vt) {}
625
626  int64_t getValue() const { return Val; }
627  MVT::SimpleValueType getVT() const { return VT; }
628
629  static inline bool classof(const Matcher *N) {
630    return N->getKind() == EmitInteger;
631  }
632
633private:
634  virtual void printImpl(raw_ostream &OS, unsigned indent) const;
635  virtual bool isEqualImpl(const Matcher *M) const {
636    return cast<EmitIntegerMatcher>(M)->Val == Val &&
637           cast<EmitIntegerMatcher>(M)->VT == VT;
638  }
639  virtual unsigned getHashImpl() const { return (Val << 4) | VT; }
640};
641
642/// EmitStringIntegerMatcher - A target constant whose value is represented
643/// by a string.
644class EmitStringIntegerMatcher : public Matcher {
645  std::string Val;
646  MVT::SimpleValueType VT;
647public:
648  EmitStringIntegerMatcher(const std::string &val, MVT::SimpleValueType vt)
649    : Matcher(EmitStringInteger), Val(val), VT(vt) {}
650
651  const std::string &getValue() const { return Val; }
652  MVT::SimpleValueType getVT() const { return VT; }
653
654  static inline bool classof(const Matcher *N) {
655    return N->getKind() == EmitStringInteger;
656  }
657
658private:
659  virtual void printImpl(raw_ostream &OS, unsigned indent) const;
660  virtual bool isEqualImpl(const Matcher *M) const {
661    return cast<EmitStringIntegerMatcher>(M)->Val == Val &&
662           cast<EmitStringIntegerMatcher>(M)->VT == VT;
663  }
664  virtual unsigned getHashImpl() const;
665};
666
667/// EmitRegisterMatcher - This creates a new TargetConstant.
668class EmitRegisterMatcher : public Matcher {
669  /// Reg - The def for the register that we're emitting.  If this is null, then
670  /// this is a reference to zero_reg.
671  Record *Reg;
672  MVT::SimpleValueType VT;
673public:
674  EmitRegisterMatcher(Record *reg, MVT::SimpleValueType vt)
675    : Matcher(EmitRegister), Reg(reg), VT(vt) {}
676
677  Record *getReg() const { return Reg; }
678  MVT::SimpleValueType getVT() const { return VT; }
679
680  static inline bool classof(const Matcher *N) {
681    return N->getKind() == EmitRegister;
682  }
683
684private:
685  virtual void printImpl(raw_ostream &OS, unsigned indent) const;
686  virtual bool isEqualImpl(const Matcher *M) const {
687    return cast<EmitRegisterMatcher>(M)->Reg == Reg &&
688           cast<EmitRegisterMatcher>(M)->VT == VT;
689  }
690  virtual unsigned getHashImpl() const {
691    return ((unsigned)(intptr_t)Reg) << 4 | VT;
692  }
693};
694
695/// EmitConvertToTargetMatcher - Emit an operation that reads a specified
696/// recorded node and converts it from being a ISD::Constant to
697/// ISD::TargetConstant, likewise for ConstantFP.
698class EmitConvertToTargetMatcher : public Matcher {
699  unsigned Slot;
700public:
701  EmitConvertToTargetMatcher(unsigned slot)
702    : Matcher(EmitConvertToTarget), Slot(slot) {}
703
704  unsigned getSlot() const { return Slot; }
705
706  static inline bool classof(const Matcher *N) {
707    return N->getKind() == EmitConvertToTarget;
708  }
709
710private:
711  virtual void printImpl(raw_ostream &OS, unsigned indent) const;
712  virtual bool isEqualImpl(const Matcher *M) const {
713    return cast<EmitConvertToTargetMatcher>(M)->Slot == Slot;
714  }
715  virtual unsigned getHashImpl() const { return Slot; }
716};
717
718/// EmitMergeInputChainsMatcher - Emit a node that merges a list of input
719/// chains together with a token factor.  The list of nodes are the nodes in the
720/// matched pattern that have chain input/outputs.  This node adds all input
721/// chains of these nodes if they are not themselves a node in the pattern.
722class EmitMergeInputChainsMatcher : public Matcher {
723  SmallVector<unsigned, 3> ChainNodes;
724public:
725  EmitMergeInputChainsMatcher(const unsigned *nodes, unsigned NumNodes)
726    : Matcher(EmitMergeInputChains), ChainNodes(nodes, nodes+NumNodes) {}
727
728  unsigned getNumNodes() const { return ChainNodes.size(); }
729
730  unsigned getNode(unsigned i) const {
731    assert(i < ChainNodes.size());
732    return ChainNodes[i];
733  }
734
735  static inline bool classof(const Matcher *N) {
736    return N->getKind() == EmitMergeInputChains;
737  }
738
739private:
740  virtual void printImpl(raw_ostream &OS, unsigned indent) const;
741  virtual bool isEqualImpl(const Matcher *M) const {
742    return cast<EmitMergeInputChainsMatcher>(M)->ChainNodes == ChainNodes;
743  }
744  virtual unsigned getHashImpl() const;
745};
746
747/// EmitCopyToRegMatcher - Emit a CopyToReg node from a value to a physreg,
748/// pushing the chain and flag results.
749///
750class EmitCopyToRegMatcher : public Matcher {
751  unsigned SrcSlot; // Value to copy into the physreg.
752  Record *DestPhysReg;
753public:
754  EmitCopyToRegMatcher(unsigned srcSlot, Record *destPhysReg)
755    : Matcher(EmitCopyToReg), SrcSlot(srcSlot), DestPhysReg(destPhysReg) {}
756
757  unsigned getSrcSlot() const { return SrcSlot; }
758  Record *getDestPhysReg() const { return DestPhysReg; }
759
760  static inline bool classof(const Matcher *N) {
761    return N->getKind() == EmitCopyToReg;
762  }
763
764private:
765  virtual void printImpl(raw_ostream &OS, unsigned indent) const;
766  virtual bool isEqualImpl(const Matcher *M) const {
767    return cast<EmitCopyToRegMatcher>(M)->SrcSlot == SrcSlot &&
768           cast<EmitCopyToRegMatcher>(M)->DestPhysReg == DestPhysReg;
769  }
770  virtual unsigned getHashImpl() const {
771    return SrcSlot ^ ((unsigned)(intptr_t)DestPhysReg << 4);
772  }
773};
774
775
776
777/// EmitNodeXFormMatcher - Emit an operation that runs an SDNodeXForm on a
778/// recorded node and records the result.
779class EmitNodeXFormMatcher : public Matcher {
780  unsigned Slot;
781  Record *NodeXForm;
782public:
783  EmitNodeXFormMatcher(unsigned slot, Record *nodeXForm)
784    : Matcher(EmitNodeXForm), Slot(slot), NodeXForm(nodeXForm) {}
785
786  unsigned getSlot() const { return Slot; }
787  Record *getNodeXForm() const { return NodeXForm; }
788
789  static inline bool classof(const Matcher *N) {
790    return N->getKind() == EmitNodeXForm;
791  }
792
793private:
794  virtual void printImpl(raw_ostream &OS, unsigned indent) const;
795  virtual bool isEqualImpl(const Matcher *M) const {
796    return cast<EmitNodeXFormMatcher>(M)->Slot == Slot &&
797           cast<EmitNodeXFormMatcher>(M)->NodeXForm == NodeXForm;
798  }
799  virtual unsigned getHashImpl() const {
800    return Slot ^ ((unsigned)(intptr_t)NodeXForm << 4);
801  }
802};
803
804/// EmitNodeMatcher - This signals a successful match and generates a node.
805class EmitNodeMatcher : public Matcher {
806  std::string OpcodeName;
807  const SmallVector<MVT::SimpleValueType, 3> VTs;
808  const SmallVector<unsigned, 6> Operands;
809  bool HasChain, HasFlag, HasMemRefs;
810
811  /// NumFixedArityOperands - If this is a fixed arity node, this is set to -1.
812  /// If this is a varidic node, this is set to the number of fixed arity
813  /// operands in the root of the pattern.  The rest are appended to this node.
814  int NumFixedArityOperands;
815public:
816  EmitNodeMatcher(const std::string &opcodeName,
817                  const MVT::SimpleValueType *vts, unsigned numvts,
818                  const unsigned *operands, unsigned numops,
819                  bool hasChain, bool hasFlag, bool hasmemrefs,
820                  int numfixedarityoperands)
821    : Matcher(EmitNode), OpcodeName(opcodeName),
822      VTs(vts, vts+numvts), Operands(operands, operands+numops),
823      HasChain(hasChain), HasFlag(hasFlag), HasMemRefs(hasmemrefs),
824      NumFixedArityOperands(numfixedarityoperands) {}
825
826  const std::string &getOpcodeName() const { return OpcodeName; }
827
828  unsigned getNumVTs() const { return VTs.size(); }
829  MVT::SimpleValueType getVT(unsigned i) const {
830    assert(i < VTs.size());
831    return VTs[i];
832  }
833
834  unsigned getNumOperands() const { return Operands.size(); }
835  unsigned getOperand(unsigned i) const {
836    assert(i < Operands.size());
837    return Operands[i];
838  }
839
840  bool hasChain() const { return HasChain; }
841  bool hasFlag() const { return HasFlag; }
842  bool hasMemRefs() const { return HasMemRefs; }
843  int getNumFixedArityOperands() const { return NumFixedArityOperands; }
844
845  static inline bool classof(const Matcher *N) {
846    return N->getKind() == EmitNode;
847  }
848
849private:
850  virtual void printImpl(raw_ostream &OS, unsigned indent) const;
851  virtual bool isEqualImpl(const Matcher *M) const;
852  virtual unsigned getHashImpl() const;
853};
854
855/// MarkFlagResultsMatcher - This node indicates which non-root nodes in the
856/// pattern produce flags.  This allows CompleteMatchMatcher to update them
857/// with the output flag of the resultant code.
858class MarkFlagResultsMatcher : public Matcher {
859  SmallVector<unsigned, 3> FlagResultNodes;
860public:
861  MarkFlagResultsMatcher(const unsigned *nodes, unsigned NumNodes)
862    : Matcher(MarkFlagResults), FlagResultNodes(nodes, nodes+NumNodes) {}
863
864  unsigned getNumNodes() const { return FlagResultNodes.size(); }
865
866  unsigned getNode(unsigned i) const {
867    assert(i < FlagResultNodes.size());
868    return FlagResultNodes[i];
869  }
870
871  static inline bool classof(const Matcher *N) {
872    return N->getKind() == MarkFlagResults;
873  }
874
875private:
876  virtual void printImpl(raw_ostream &OS, unsigned indent) const;
877  virtual bool isEqualImpl(const Matcher *M) const {
878    return cast<MarkFlagResultsMatcher>(M)->FlagResultNodes == FlagResultNodes;
879  }
880  virtual unsigned getHashImpl() const;
881};
882
883/// CompleteMatchMatcher - Complete a match by replacing the results of the
884/// pattern with the newly generated nodes.  This also prints a comment
885/// indicating the source and dest patterns.
886class CompleteMatchMatcher : public Matcher {
887  SmallVector<unsigned, 2> Results;
888  const PatternToMatch &Pattern;
889public:
890  CompleteMatchMatcher(const unsigned *results, unsigned numresults,
891                           const PatternToMatch &pattern)
892  : Matcher(CompleteMatch), Results(results, results+numresults),
893    Pattern(pattern) {}
894
895  unsigned getNumResults() const { return Results.size(); }
896  unsigned getResult(unsigned R) const { return Results[R]; }
897  const PatternToMatch &getPattern() const { return Pattern; }
898
899  static inline bool classof(const Matcher *N) {
900    return N->getKind() == CompleteMatch;
901  }
902
903private:
904  virtual void printImpl(raw_ostream &OS, unsigned indent) const;
905  virtual bool isEqualImpl(const Matcher *M) const {
906    return cast<CompleteMatchMatcher>(M)->Results == Results &&
907          &cast<CompleteMatchMatcher>(M)->Pattern == &Pattern;
908  }
909  virtual unsigned getHashImpl() const;
910};
911
912} // end namespace llvm
913
914#endif
915