Lots of improvements to the new dagisel emitter. This gets it to
[oota-llvm.git] / utils / TableGen / DAGISelMatcher.h
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
19 namespace llvm {
20   class CodeGenDAGPatterns;
21   class MatcherNode;
22   class PatternToMatch;
23   class raw_ostream;
24   class ComplexPattern;
25   class Record;
26
27 MatcherNode *ConvertPatternToMatcher(const PatternToMatch &Pattern,
28                                      const CodeGenDAGPatterns &CGP);
29
30 void EmitMatcherTable(const MatcherNode *Matcher, raw_ostream &OS);
31
32   
33 /// MatcherNode - Base class for all the the DAG ISel Matcher representation
34 /// nodes.
35 class MatcherNode {
36   // The next matcher node that is executed after this one.  Null if this is the
37   // last stage of a match.
38   OwningPtr<MatcherNode> Next;
39 public:
40   enum KindTy {
41     // Matcher state manipulation.
42     Push,                 // Push a checking scope.
43     RecordNode,           // Record the current node.
44     RecordMemRef,         // Record the memref in the current node.
45     CaptureFlagInput,     // If the current node has an input flag, save it.
46     MoveChild,            // Move current node to specified child.
47     MoveParent,           // Move current node to parent.
48     
49     // Predicate checking.
50     CheckSame,            // Fail if not same as prev match.
51     CheckPatternPredicate,
52     CheckPredicate,       // Fail if node predicate fails.
53     CheckOpcode,          // Fail if not opcode.
54     CheckType,            // Fail if not correct type.
55     CheckInteger,         // Fail if wrong val.
56     CheckCondCode,        // Fail if not condcode.
57     CheckValueType,
58     CheckComplexPat,
59     CheckAndImm,
60     CheckOrImm,
61     CheckFoldableChainNode,
62     CheckChainCompatible,
63     
64     // Node creation/emisssion.
65     EmitInteger,          // Create a TargetConstant
66     EmitStringInteger,    // Create a TargetConstant from a string.
67     EmitRegister,         // Create a register.
68     EmitConvertToTarget,  // Convert a imm/fpimm to target imm/fpimm
69     EmitMergeInputChains, // Merge together a chains for an input.
70     EmitCopyToReg,        // Emit a copytoreg into a physreg.
71     EmitNode,             // Create a DAG node
72     EmitNodeXForm,        // Run a SDNodeXForm
73     PatternMarker         // Comment for printing.
74   };
75   const KindTy Kind;
76
77 protected:
78   MatcherNode(KindTy K) : Kind(K) {}
79 public:
80   virtual ~MatcherNode() {}
81   
82   KindTy getKind() const { return Kind; }
83
84   MatcherNode *getNext() { return Next.get(); }
85   const MatcherNode *getNext() const { return Next.get(); }
86   void setNext(MatcherNode *C) { Next.reset(C); }
87   
88   static inline bool classof(const MatcherNode *) { return true; }
89   
90   virtual void print(raw_ostream &OS, unsigned indent = 0) const = 0;
91   void dump() const;
92 protected:
93   void printNext(raw_ostream &OS, unsigned indent) const;
94 };
95   
96 /// PushMatcherNode - This pushes a failure scope on the stack and evaluates
97 /// 'Next'.  If 'Next' fails to match, it pops its scope and attempts to
98 /// match 'Failure'.
99 class PushMatcherNode : public MatcherNode {
100   OwningPtr<MatcherNode> Failure;
101 public:
102   PushMatcherNode(MatcherNode *next = 0, MatcherNode *failure = 0)
103     : MatcherNode(Push), Failure(failure) {
104     setNext(next);
105   }
106   
107   MatcherNode *getFailure() { return Failure.get(); }
108   const MatcherNode *getFailure() const { return Failure.get(); }
109   void setFailure(MatcherNode *N) { Failure.reset(N); }
110
111   static inline bool classof(const MatcherNode *N) {
112     return N->getKind() == Push;
113   }
114   
115   virtual void print(raw_ostream &OS, unsigned indent = 0) const;
116 };
117
118 /// RecordMatcherNode - Save the current node in the operand list.
119 class RecordMatcherNode : public MatcherNode {
120   /// WhatFor - This is a string indicating why we're recording this.  This
121   /// should only be used for comment generation not anything semantic.
122   std::string WhatFor;
123 public:
124   RecordMatcherNode(const std::string &whatfor)
125     : MatcherNode(RecordNode), WhatFor(whatfor) {}
126   
127   const std::string &getWhatFor() const { return WhatFor; }
128   
129   static inline bool classof(const MatcherNode *N) {
130     return N->getKind() == RecordNode;
131   }
132   
133   virtual void print(raw_ostream &OS, unsigned indent = 0) const;
134 };
135   
136 /// RecordMemRefMatcherNode - Save the current node's memref.
137 class RecordMemRefMatcherNode : public MatcherNode {
138 public:
139   RecordMemRefMatcherNode() : MatcherNode(RecordMemRef) {}
140   
141   static inline bool classof(const MatcherNode *N) {
142     return N->getKind() == RecordMemRef;
143   }
144   
145   virtual void print(raw_ostream &OS, unsigned indent = 0) const;
146 };
147
148   
149 /// CaptureFlagInputMatcherNode - If the current record has a flag input, record
150 /// it so that it is used as an input to the generated code.
151 class CaptureFlagInputMatcherNode : public MatcherNode {
152 public:
153   CaptureFlagInputMatcherNode()
154     : MatcherNode(CaptureFlagInput) {}
155   
156   static inline bool classof(const MatcherNode *N) {
157     return N->getKind() == CaptureFlagInput;
158   }
159   
160   virtual void print(raw_ostream &OS, unsigned indent = 0) const;
161 };
162   
163 /// MoveChildMatcherNode - This tells the interpreter to move into the
164 /// specified child node.
165 class MoveChildMatcherNode : public MatcherNode {
166   unsigned ChildNo;
167 public:
168   MoveChildMatcherNode(unsigned childNo)
169   : MatcherNode(MoveChild), ChildNo(childNo) {}
170   
171   unsigned getChildNo() const { return ChildNo; }
172   
173   static inline bool classof(const MatcherNode *N) {
174     return N->getKind() == MoveChild;
175   }
176   
177   virtual void print(raw_ostream &OS, unsigned indent = 0) const;
178 };
179   
180 /// MoveParentMatcherNode - This tells the interpreter to move to the parent
181 /// of the current node.
182 class MoveParentMatcherNode : public MatcherNode {
183 public:
184   MoveParentMatcherNode()
185   : MatcherNode(MoveParent) {}
186   
187   static inline bool classof(const MatcherNode *N) {
188     return N->getKind() == MoveParent;
189   }
190   
191   virtual void print(raw_ostream &OS, unsigned indent = 0) const;
192 };
193
194 /// CheckSameMatcherNode - This checks to see if this node is exactly the same
195 /// node as the specified match that was recorded with 'Record'.  This is used
196 /// when patterns have the same name in them, like '(mul GPR:$in, GPR:$in)'.
197 class CheckSameMatcherNode : public MatcherNode {
198   unsigned MatchNumber;
199 public:
200   CheckSameMatcherNode(unsigned matchnumber)
201   : MatcherNode(CheckSame), MatchNumber(matchnumber) {}
202   
203   unsigned getMatchNumber() const { return MatchNumber; }
204   
205   static inline bool classof(const MatcherNode *N) {
206     return N->getKind() == CheckSame;
207   }
208   
209   virtual void print(raw_ostream &OS, unsigned indent = 0) const;
210 };
211   
212 /// CheckPatternPredicateMatcherNode - This checks the target-specific predicate
213 /// to see if the entire pattern is capable of matching.  This predicate does
214 /// not take a node as input.  This is used for subtarget feature checks etc.
215 class CheckPatternPredicateMatcherNode : public MatcherNode {
216   std::string Predicate;
217 public:
218   CheckPatternPredicateMatcherNode(StringRef predicate)
219   : MatcherNode(CheckPatternPredicate), Predicate(predicate) {}
220   
221   StringRef getPredicate() const { return Predicate; }
222   
223   static inline bool classof(const MatcherNode *N) {
224     return N->getKind() == CheckPatternPredicate;
225   }
226   
227   virtual void print(raw_ostream &OS, unsigned indent = 0) const;
228 };
229   
230 /// CheckPredicateMatcherNode - This checks the target-specific predicate to
231 /// see if the node is acceptable.
232 class CheckPredicateMatcherNode : public MatcherNode {
233   StringRef PredName;
234 public:
235   CheckPredicateMatcherNode(StringRef predname)
236     : MatcherNode(CheckPredicate), PredName(predname) {}
237   
238   StringRef getPredicateName() const { return PredName; }
239
240   static inline bool classof(const MatcherNode *N) {
241     return N->getKind() == CheckPredicate;
242   }
243   
244   virtual void print(raw_ostream &OS, unsigned indent = 0) const;
245 };
246   
247   
248 /// CheckOpcodeMatcherNode - This checks to see if the current node has the
249 /// specified opcode, if not it fails to match.
250 class CheckOpcodeMatcherNode : public MatcherNode {
251   StringRef OpcodeName;
252 public:
253   CheckOpcodeMatcherNode(StringRef opcodename)
254     : MatcherNode(CheckOpcode), OpcodeName(opcodename) {}
255   
256   StringRef getOpcodeName() const { return OpcodeName; }
257   
258   static inline bool classof(const MatcherNode *N) {
259     return N->getKind() == CheckOpcode;
260   }
261   
262   virtual void print(raw_ostream &OS, unsigned indent = 0) const;
263 };
264   
265 /// CheckTypeMatcherNode - This checks to see if the current node has the
266 /// specified type, if not it fails to match.
267 class CheckTypeMatcherNode : public MatcherNode {
268   MVT::SimpleValueType Type;
269 public:
270   CheckTypeMatcherNode(MVT::SimpleValueType type)
271     : MatcherNode(CheckType), Type(type) {}
272   
273   MVT::SimpleValueType getType() const { return Type; }
274   
275   static inline bool classof(const MatcherNode *N) {
276     return N->getKind() == CheckType;
277   }
278   
279   virtual void print(raw_ostream &OS, unsigned indent = 0) const;
280 };
281
282 /// CheckIntegerMatcherNode - This checks to see if the current node is a
283 /// ConstantSDNode with the specified integer value, if not it fails to match.
284 class CheckIntegerMatcherNode : public MatcherNode {
285   int64_t Value;
286 public:
287   CheckIntegerMatcherNode(int64_t value)
288     : MatcherNode(CheckInteger), Value(value) {}
289   
290   int64_t getValue() const { return Value; }
291   
292   static inline bool classof(const MatcherNode *N) {
293     return N->getKind() == CheckInteger;
294   }
295   
296   virtual void print(raw_ostream &OS, unsigned indent = 0) const;
297 };
298   
299 /// CheckCondCodeMatcherNode - This checks to see if the current node is a
300 /// CondCodeSDNode with the specified condition, if not it fails to match.
301 class CheckCondCodeMatcherNode : public MatcherNode {
302   StringRef CondCodeName;
303 public:
304   CheckCondCodeMatcherNode(StringRef condcodename)
305   : MatcherNode(CheckCondCode), CondCodeName(condcodename) {}
306   
307   StringRef getCondCodeName() const { return CondCodeName; }
308   
309   static inline bool classof(const MatcherNode *N) {
310     return N->getKind() == CheckCondCode;
311   }
312   
313   virtual void print(raw_ostream &OS, unsigned indent = 0) const;
314 };
315   
316 /// CheckValueTypeMatcherNode - This checks to see if the current node is a
317 /// VTSDNode with the specified type, if not it fails to match.
318 class CheckValueTypeMatcherNode : public MatcherNode {
319   StringRef TypeName;
320 public:
321   CheckValueTypeMatcherNode(StringRef type_name)
322   : MatcherNode(CheckValueType), TypeName(type_name) {}
323   
324   StringRef getTypeName() const { return TypeName; }
325
326   static inline bool classof(const MatcherNode *N) {
327     return N->getKind() == CheckValueType;
328   }
329   
330   virtual void print(raw_ostream &OS, unsigned indent = 0) const;
331 };
332   
333   
334   
335 /// CheckComplexPatMatcherNode - This node runs the specified ComplexPattern on
336 /// the current node.
337 class CheckComplexPatMatcherNode : public MatcherNode {
338   const ComplexPattern &Pattern;
339 public:
340   CheckComplexPatMatcherNode(const ComplexPattern &pattern)
341   : MatcherNode(CheckComplexPat), Pattern(pattern) {}
342   
343   const ComplexPattern &getPattern() const { return Pattern; }
344   
345   static inline bool classof(const MatcherNode *N) {
346     return N->getKind() == CheckComplexPat;
347   }
348   
349   virtual void print(raw_ostream &OS, unsigned indent = 0) const;
350 };
351   
352 /// CheckAndImmMatcherNode - This checks to see if the current node is an 'and'
353 /// with something equivalent to the specified immediate.
354 class CheckAndImmMatcherNode : public MatcherNode {
355   int64_t Value;
356 public:
357   CheckAndImmMatcherNode(int64_t value)
358   : MatcherNode(CheckAndImm), Value(value) {}
359   
360   int64_t getValue() const { return Value; }
361   
362   static inline bool classof(const MatcherNode *N) {
363     return N->getKind() == CheckAndImm;
364   }
365   
366   virtual void print(raw_ostream &OS, unsigned indent = 0) const;
367 };
368
369 /// CheckOrImmMatcherNode - This checks to see if the current node is an 'and'
370 /// with something equivalent to the specified immediate.
371 class CheckOrImmMatcherNode : public MatcherNode {
372   int64_t Value;
373 public:
374   CheckOrImmMatcherNode(int64_t value)
375     : MatcherNode(CheckOrImm), Value(value) {}
376   
377   int64_t getValue() const { return Value; }
378
379   static inline bool classof(const MatcherNode *N) {
380     return N->getKind() == CheckOrImm;
381   }
382   
383   virtual void print(raw_ostream &OS, unsigned indent = 0) const;
384 };
385
386 /// CheckFoldableChainNodeMatcherNode - This checks to see if the current node
387 /// (which defines a chain operand) is safe to fold into a larger pattern.
388 class CheckFoldableChainNodeMatcherNode : public MatcherNode {
389 public:
390   CheckFoldableChainNodeMatcherNode()
391     : MatcherNode(CheckFoldableChainNode) {}
392   
393   static inline bool classof(const MatcherNode *N) {
394     return N->getKind() == CheckFoldableChainNode;
395   }
396   
397   virtual void print(raw_ostream &OS, unsigned indent = 0) const;
398 };
399
400 /// CheckChainCompatibleMatcherNode - Verify that the current node's chain
401 /// operand is 'compatible' with the specified recorded node's.
402 class CheckChainCompatibleMatcherNode : public MatcherNode {
403   unsigned PreviousOp;
404 public:
405   CheckChainCompatibleMatcherNode(unsigned previousop)
406     : MatcherNode(CheckChainCompatible), PreviousOp(previousop) {}
407   
408   unsigned getPreviousOp() const { return PreviousOp; }
409   
410   static inline bool classof(const MatcherNode *N) {
411     return N->getKind() == CheckChainCompatible;
412   }
413   
414   virtual void print(raw_ostream &OS, unsigned indent = 0) const;
415 };
416   
417 /// EmitIntegerMatcherNode - This creates a new TargetConstant.
418 class EmitIntegerMatcherNode : public MatcherNode {
419   int64_t Val;
420   MVT::SimpleValueType VT;
421 public:
422   EmitIntegerMatcherNode(int64_t val, MVT::SimpleValueType vt)
423   : MatcherNode(EmitInteger), Val(val), VT(vt) {}
424   
425   int64_t getValue() const { return Val; }
426   MVT::SimpleValueType getVT() const { return VT; }
427   
428   static inline bool classof(const MatcherNode *N) {
429     return N->getKind() == EmitInteger;
430   }
431   
432   virtual void print(raw_ostream &OS, unsigned indent = 0) const;
433 };
434
435 /// EmitStringIntegerMatcherNode - A target constant whose value is represented
436 /// by a string.
437 class EmitStringIntegerMatcherNode : public MatcherNode {
438   std::string Val;
439   MVT::SimpleValueType VT;
440 public:
441   EmitStringIntegerMatcherNode(const std::string &val, MVT::SimpleValueType vt)
442     : MatcherNode(EmitStringInteger), Val(val), VT(vt) {}
443   
444   const std::string &getValue() const { return Val; }
445   MVT::SimpleValueType getVT() const { return VT; }
446   
447   static inline bool classof(const MatcherNode *N) {
448     return N->getKind() == EmitStringInteger;
449   }
450   
451   virtual void print(raw_ostream &OS, unsigned indent = 0) const;
452 };
453   
454 /// EmitRegisterMatcherNode - This creates a new TargetConstant.
455 class EmitRegisterMatcherNode : public MatcherNode {
456   /// Reg - The def for the register that we're emitting.  If this is null, then
457   /// this is a reference to zero_reg.
458   Record *Reg;
459   MVT::SimpleValueType VT;
460 public:
461   EmitRegisterMatcherNode(Record *reg, MVT::SimpleValueType vt)
462     : MatcherNode(EmitRegister), Reg(reg), VT(vt) {}
463   
464   Record *getReg() const { return Reg; }
465   MVT::SimpleValueType getVT() const { return VT; }
466   
467   static inline bool classof(const MatcherNode *N) {
468     return N->getKind() == EmitRegister;
469   }
470   
471   virtual void print(raw_ostream &OS, unsigned indent = 0) const;
472 };
473
474 /// EmitConvertToTargetMatcherNode - Emit an operation that reads a specified
475 /// recorded node and converts it from being a ISD::Constant to
476 /// ISD::TargetConstant, likewise for ConstantFP.
477 class EmitConvertToTargetMatcherNode : public MatcherNode {
478   unsigned Slot;
479 public:
480   EmitConvertToTargetMatcherNode(unsigned slot)
481     : MatcherNode(EmitConvertToTarget), Slot(slot) {}
482   
483   unsigned getSlot() const { return Slot; }
484   
485   static inline bool classof(const MatcherNode *N) {
486     return N->getKind() == EmitConvertToTarget;
487   }
488   
489   virtual void print(raw_ostream &OS, unsigned indent = 0) const;
490 };
491   
492 /// EmitMergeInputChainsMatcherNode - Emit a node that merges a list of input
493 /// chains together with a token factor.  The list of nodes are the nodes in the
494 /// matched pattern that have chain input/outputs.  This node adds all input
495 /// chains of these nodes if they are not themselves a node in the pattern.
496 class EmitMergeInputChainsMatcherNode : public MatcherNode {
497   SmallVector<unsigned, 3> ChainNodes;
498 public:
499   EmitMergeInputChainsMatcherNode(const unsigned *nodes, unsigned NumNodes)
500   : MatcherNode(EmitMergeInputChains), ChainNodes(nodes, nodes+NumNodes) {}
501   
502   unsigned getNumNodes() const { return ChainNodes.size(); }
503   
504   unsigned getNode(unsigned i) const {
505     assert(i < ChainNodes.size());
506     return ChainNodes[i];
507   }  
508   
509   static inline bool classof(const MatcherNode *N) {
510     return N->getKind() == EmitMergeInputChains;
511   }
512   
513   virtual void print(raw_ostream &OS, unsigned indent = 0) const;
514 };
515   
516 /// EmitCopyToRegMatcherNode - Emit a CopyToReg node from a value to a physreg,
517 /// pushing the chain and flag results.
518 ///
519 class EmitCopyToRegMatcherNode : public MatcherNode {
520   unsigned SrcSlot; // Value to copy into the physreg.
521   Record *DestPhysReg;
522 public:
523   EmitCopyToRegMatcherNode(unsigned srcSlot, Record *destPhysReg)
524   : MatcherNode(EmitCopyToReg), SrcSlot(srcSlot), DestPhysReg(destPhysReg) {}
525   
526   unsigned getSrcSlot() const { return SrcSlot; }
527   Record *getDestPhysReg() const { return DestPhysReg; }
528   
529   static inline bool classof(const MatcherNode *N) {
530     return N->getKind() == EmitCopyToReg;
531   }
532   
533   virtual void print(raw_ostream &OS, unsigned indent = 0) const;
534 };
535   
536     
537   
538 /// EmitNodeXFormMatcherNode - Emit an operation that runs an SDNodeXForm on a
539 /// recorded node and records the result.
540 class EmitNodeXFormMatcherNode : public MatcherNode {
541   unsigned Slot;
542   Record *NodeXForm;
543 public:
544   EmitNodeXFormMatcherNode(unsigned slot, Record *nodeXForm)
545   : MatcherNode(EmitNodeXForm), Slot(slot), NodeXForm(nodeXForm) {}
546   
547   unsigned getSlot() const { return Slot; }
548   Record *getNodeXForm() const { return NodeXForm; }
549   
550   static inline bool classof(const MatcherNode *N) {
551     return N->getKind() == EmitNodeXForm;
552   }
553   
554   virtual void print(raw_ostream &OS, unsigned indent = 0) const;
555 };
556   
557 /// EmitNodeMatcherNode - This signals a successful match and generates a node.
558 class EmitNodeMatcherNode : public MatcherNode {
559   std::string OpcodeName;
560   const SmallVector<MVT::SimpleValueType, 3> VTs;
561   const SmallVector<unsigned, 6> Operands;
562   bool HasChain, HasFlag, HasMemRefs;
563   
564   /// NumFixedArityOperands - If this is a fixed arity node, this is set to -1.
565   /// If this is a varidic node, this is set to the number of fixed arity
566   /// operands in the root of the pattern.  The rest are appended to this node.
567   int NumFixedArityOperands;
568 public:
569   EmitNodeMatcherNode(const std::string &opcodeName,
570                       const MVT::SimpleValueType *vts, unsigned numvts,
571                       const unsigned *operands, unsigned numops,
572                       bool hasChain, bool hasFlag, bool hasmemrefs,
573                       int numfixedarityoperands)
574     : MatcherNode(EmitNode), OpcodeName(opcodeName),
575       VTs(vts, vts+numvts), Operands(operands, operands+numops),
576       HasChain(hasChain), HasFlag(hasFlag), HasMemRefs(hasmemrefs),
577       NumFixedArityOperands(numfixedarityoperands) {}
578   
579   const std::string &getOpcodeName() const { return OpcodeName; }
580   
581   unsigned getNumVTs() const { return VTs.size(); }
582   MVT::SimpleValueType getVT(unsigned i) const {
583     assert(i < VTs.size());
584     return VTs[i];
585   }
586   
587   unsigned getNumOperands() const { return Operands.size(); }
588   unsigned getOperand(unsigned i) const {
589     assert(i < Operands.size());
590     return Operands[i];
591   }  
592   
593   bool hasChain() const { return HasChain; }
594   bool hasFlag() const { return HasFlag; }
595   bool hasMemRefs() const { return HasMemRefs; }
596   int getNumFixedArityOperands() const { return NumFixedArityOperands; }
597   
598   static inline bool classof(const MatcherNode *N) {
599     return N->getKind() == EmitNode;
600   }
601   
602   virtual void print(raw_ostream &OS, unsigned indent = 0) const;
603 };
604
605 /// PatternMarkerMatcherNode - This prints as a comment indicating the source
606 /// and dest patterns.
607 class PatternMarkerMatcherNode : public MatcherNode {
608   const PatternToMatch &Pattern;
609 public:
610   PatternMarkerMatcherNode(const PatternToMatch &pattern)
611   : MatcherNode(PatternMarker), Pattern(pattern) {}
612   
613   const PatternToMatch &getPattern() const { return Pattern; }
614   
615   static inline bool classof(const MatcherNode *N) {
616     return N->getKind() == PatternMarker;
617   }
618   
619   virtual void print(raw_ostream &OS, unsigned indent = 0) const;
620 };
621  
622   
623 } // end namespace llvm
624
625 #endif