blob: 947cfe7f6dd37f0c7bcd69d7a22f2db2a5832d93 [file] [log] [blame]
Chris Lattnerfe718932008-01-06 01:10:31 +00001//===- CodeGenDAGPatterns.h - Read DAG patterns from .td file ---*- C++ -*-===//
Chris Lattner6cefb772008-01-05 22:25:12 +00002//
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//
Chris Lattnerfe718932008-01-06 01:10:31 +000010// This file declares the CodeGenDAGPatterns class, which is used to read and
Chris Lattner6cefb772008-01-05 22:25:12 +000011// represent the patterns present in a .td file for instructions.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef CODEGEN_DAGPATTERNS_H
16#define CODEGEN_DAGPATTERNS_H
17
Chris Lattner6cefb772008-01-05 22:25:12 +000018#include "CodeGenTarget.h"
19#include "CodeGenIntrinsics.h"
Chris Lattner2cacec52010-03-15 06:00:16 +000020#include "llvm/ADT/SmallVector.h"
21#include "llvm/ADT/StringMap.h"
Chris Lattnere14d2e22010-03-19 01:07:44 +000022#include <set>
23#include <algorithm>
24#include <vector>
25#include <map>
Chris Lattner6cefb772008-01-05 22:25:12 +000026
27namespace llvm {
28 class Record;
29 struct Init;
30 class ListInit;
31 class DagInit;
32 class SDNodeInfo;
33 class TreePattern;
34 class TreePatternNode;
Chris Lattnerfe718932008-01-06 01:10:31 +000035 class CodeGenDAGPatterns;
Chris Lattner6cefb772008-01-05 22:25:12 +000036 class ComplexPattern;
37
Owen Andersone50ed302009-08-10 22:56:29 +000038/// EEVT::DAGISelGenValueType - These are some extended forms of
Owen Anderson825b72b2009-08-11 20:47:22 +000039/// MVT::SimpleValueType that we use as lattice values during type inference.
Bob Wilsoncdfa01b2009-08-29 05:53:25 +000040/// The existing MVT iAny, fAny and vAny types suffice to represent
41/// arbitrary integer, floating-point, and vector types, so only an unknown
42/// value is needed.
Owen Andersone50ed302009-08-10 22:56:29 +000043namespace EEVT {
Chris Lattner2cacec52010-03-15 06:00:16 +000044 /// TypeSet - This is either empty if it's completely unknown, or holds a set
45 /// of types. It is used during type inference because register classes can
46 /// have multiple possible types and we don't know which one they get until
47 /// type inference is complete.
48 ///
49 /// TypeSet can have three states:
50 /// Vector is empty: The type is completely unknown, it can be any valid
51 /// target type.
52 /// Vector has multiple constrained types: (e.g. v4i32 + v4f32) it is one
53 /// of those types only.
54 /// Vector has one concrete type: The type is completely known.
55 ///
56 class TypeSet {
Chris Lattner774ce292010-03-19 17:41:26 +000057 SmallVector<MVT::SimpleValueType, 4> TypeVec;
Chris Lattner2cacec52010-03-15 06:00:16 +000058 public:
59 TypeSet() {}
60 TypeSet(MVT::SimpleValueType VT, TreePattern &TP);
61 TypeSet(const std::vector<MVT::SimpleValueType> &VTList);
62
63 bool isCompletelyUnknown() const { return TypeVec.empty(); }
64
65 bool isConcrete() const {
66 if (TypeVec.size() != 1) return false;
67 unsigned char T = TypeVec[0]; (void)T;
68 assert(T < MVT::LAST_VALUETYPE || T == MVT::iPTR || T == MVT::iPTRAny);
69 return true;
70 }
71
72 MVT::SimpleValueType getConcrete() const {
73 assert(isConcrete() && "Type isn't concrete yet");
74 return (MVT::SimpleValueType)TypeVec[0];
75 }
76
77 bool isDynamicallyResolved() const {
78 return getConcrete() == MVT::iPTR || getConcrete() == MVT::iPTRAny;
79 }
80
81 const SmallVectorImpl<MVT::SimpleValueType> &getTypeList() const {
82 assert(!TypeVec.empty() && "Not a type list!");
83 return TypeVec;
84 }
85
Chris Lattner6c6ba362010-03-18 23:15:10 +000086 bool isVoid() const {
87 return TypeVec.size() == 1 && TypeVec[0] == MVT::isVoid;
88 }
89
Chris Lattner2cacec52010-03-15 06:00:16 +000090 /// hasIntegerTypes - Return true if this TypeSet contains any integer value
91 /// types.
92 bool hasIntegerTypes() const;
93
94 /// hasFloatingPointTypes - Return true if this TypeSet contains an fAny or
95 /// a floating point value type.
96 bool hasFloatingPointTypes() const;
97
98 /// hasVectorTypes - Return true if this TypeSet contains a vector value
99 /// type.
100 bool hasVectorTypes() const;
101
102 /// getName() - Return this TypeSet as a string.
103 std::string getName() const;
104
105 /// MergeInTypeInfo - This merges in type information from the specified
106 /// argument. If 'this' changes, it returns true. If the two types are
107 /// contradictory (e.g. merge f32 into i32) then this throws an exception.
108 bool MergeInTypeInfo(const EEVT::TypeSet &InVT, TreePattern &TP);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000109
Chris Lattner2cacec52010-03-15 06:00:16 +0000110 bool MergeInTypeInfo(MVT::SimpleValueType InVT, TreePattern &TP) {
111 return MergeInTypeInfo(EEVT::TypeSet(InVT, TP), TP);
112 }
Chris Lattner6cefb772008-01-05 22:25:12 +0000113
Chris Lattner2cacec52010-03-15 06:00:16 +0000114 /// Force this type list to only contain integer types.
115 bool EnforceInteger(TreePattern &TP);
Bob Wilson36e3e662009-08-12 22:30:59 +0000116
Chris Lattner2cacec52010-03-15 06:00:16 +0000117 /// Force this type list to only contain floating point types.
118 bool EnforceFloatingPoint(TreePattern &TP);
119
120 /// EnforceScalar - Remove all vector types from this type list.
121 bool EnforceScalar(TreePattern &TP);
122
123 /// EnforceVector - Remove all non-vector types from this type list.
124 bool EnforceVector(TreePattern &TP);
125
126 /// EnforceSmallerThan - 'this' must be a smaller VT than Other. Update
127 /// this an other based on this information.
128 bool EnforceSmallerThan(EEVT::TypeSet &Other, TreePattern &TP);
129
130 /// EnforceVectorEltTypeIs - 'this' is now constrainted to be a vector type
131 /// whose element is VT.
132 bool EnforceVectorEltTypeIs(MVT::SimpleValueType VT, TreePattern &TP);
133
134 bool operator!=(const TypeSet &RHS) const { return TypeVec != RHS.TypeVec; }
135 bool operator==(const TypeSet &RHS) const { return TypeVec == RHS.TypeVec; }
Chris Lattner5a9b8fb2010-03-19 04:54:36 +0000136
137 private:
138 /// FillWithPossibleTypes - Set to all legal types and return true, only
Chris Lattner774ce292010-03-19 17:41:26 +0000139 /// valid on completely unknown type sets. If Pred is non-null, only MVTs
140 /// that pass the predicate are added.
141 bool FillWithPossibleTypes(TreePattern &TP,
142 bool (*Pred)(MVT::SimpleValueType) = 0,
143 const char *PredicateName = 0);
Chris Lattner2cacec52010-03-15 06:00:16 +0000144 };
Chris Lattner6cefb772008-01-05 22:25:12 +0000145}
146
Scott Michel327d0652008-03-05 17:49:05 +0000147/// Set type used to track multiply used variables in patterns
148typedef std::set<std::string> MultipleUseVarSet;
149
Chris Lattner6cefb772008-01-05 22:25:12 +0000150/// SDTypeConstraint - This is a discriminated union of constraints,
151/// corresponding to the SDTypeConstraint tablegen class in Target.td.
152struct SDTypeConstraint {
153 SDTypeConstraint(Record *R);
154
155 unsigned OperandNo; // The operand # this constraint applies to.
156 enum {
Bob Wilson36e3e662009-08-12 22:30:59 +0000157 SDTCisVT, SDTCisPtrTy, SDTCisInt, SDTCisFP, SDTCisVec, SDTCisSameAs,
Nate Begeman9008ca62009-04-27 18:41:29 +0000158 SDTCisVTSmallerThanOp, SDTCisOpSmallerThanOp, SDTCisEltOfVec
Chris Lattner6cefb772008-01-05 22:25:12 +0000159 } ConstraintType;
160
161 union { // The discriminated union.
162 struct {
Chris Lattner2cacec52010-03-15 06:00:16 +0000163 MVT::SimpleValueType VT;
Chris Lattner6cefb772008-01-05 22:25:12 +0000164 } SDTCisVT_Info;
165 struct {
166 unsigned OtherOperandNum;
167 } SDTCisSameAs_Info;
168 struct {
169 unsigned OtherOperandNum;
170 } SDTCisVTSmallerThanOp_Info;
171 struct {
172 unsigned BigOperandNum;
173 } SDTCisOpSmallerThanOp_Info;
174 struct {
175 unsigned OtherOperandNum;
Nate Begemanb5af3342008-02-09 01:37:05 +0000176 } SDTCisEltOfVec_Info;
Chris Lattner6cefb772008-01-05 22:25:12 +0000177 } x;
178
179 /// ApplyTypeConstraint - Given a node in a pattern, apply this type
180 /// constraint to the nodes operands. This returns true if it makes a
181 /// change, false otherwise. If a type contradiction is found, throw an
182 /// exception.
183 bool ApplyTypeConstraint(TreePatternNode *N, const SDNodeInfo &NodeInfo,
184 TreePattern &TP) const;
185
186 /// getOperandNum - Return the node corresponding to operand #OpNo in tree
187 /// N, which has NumResults results.
188 TreePatternNode *getOperandNum(unsigned OpNo, TreePatternNode *N,
189 unsigned NumResults) const;
190};
191
192/// SDNodeInfo - One of these records is created for each SDNode instance in
193/// the target .td file. This represents the various dag nodes we will be
194/// processing.
195class SDNodeInfo {
196 Record *Def;
197 std::string EnumName;
198 std::string SDClassName;
199 unsigned Properties;
200 unsigned NumResults;
201 int NumOperands;
202 std::vector<SDTypeConstraint> TypeConstraints;
203public:
204 SDNodeInfo(Record *R); // Parse the specified record.
205
206 unsigned getNumResults() const { return NumResults; }
207 int getNumOperands() const { return NumOperands; }
208 Record *getRecord() const { return Def; }
209 const std::string &getEnumName() const { return EnumName; }
210 const std::string &getSDClassName() const { return SDClassName; }
211
212 const std::vector<SDTypeConstraint> &getTypeConstraints() const {
213 return TypeConstraints;
214 }
215
Chris Lattner22579812010-02-28 00:22:30 +0000216 /// getKnownType - If the type constraints on this node imply a fixed type
217 /// (e.g. all stores return void, etc), then return it as an
Chris Lattneraac5b5b2010-03-19 01:14:27 +0000218 /// MVT::SimpleValueType. Otherwise, return MVT::Other.
219 MVT::SimpleValueType getKnownType() const;
Chris Lattner22579812010-02-28 00:22:30 +0000220
Chris Lattner6cefb772008-01-05 22:25:12 +0000221 /// hasProperty - Return true if this node has the specified property.
222 ///
223 bool hasProperty(enum SDNP Prop) const { return Properties & (1 << Prop); }
224
225 /// ApplyTypeConstraints - Given a node in a pattern, apply the type
226 /// constraints for this node to the operands of the node. This returns
227 /// true if it makes a change, false otherwise. If a type contradiction is
228 /// found, throw an exception.
229 bool ApplyTypeConstraints(TreePatternNode *N, TreePattern &TP) const {
230 bool MadeChange = false;
231 for (unsigned i = 0, e = TypeConstraints.size(); i != e; ++i)
232 MadeChange |= TypeConstraints[i].ApplyTypeConstraint(N, *this, TP);
233 return MadeChange;
234 }
235};
236
237/// FIXME: TreePatternNode's can be shared in some cases (due to dag-shaped
238/// patterns), and as such should be ref counted. We currently just leak all
239/// TreePatternNode objects!
240class TreePatternNode {
Chris Lattner2cacec52010-03-15 06:00:16 +0000241 /// The type of this node. Before and during type inference, this may be a
242 /// set of possible types. After (successful) type inference, this is a
243 /// single type.
244 EEVT::TypeSet Type;
Chris Lattner6cefb772008-01-05 22:25:12 +0000245
246 /// Operator - The Record for the operator if this is an interior node (not
247 /// a leaf).
248 Record *Operator;
249
250 /// Val - The init value (e.g. the "GPRC" record, or "7") for a leaf.
251 ///
252 Init *Val;
253
254 /// Name - The name given to this node with the :$foo notation.
255 ///
256 std::string Name;
257
Dan Gohman0540e172008-10-15 06:17:21 +0000258 /// PredicateFns - The predicate functions to execute on this node to check
259 /// for a match. If this list is empty, no predicate is involved.
260 std::vector<std::string> PredicateFns;
Chris Lattner6cefb772008-01-05 22:25:12 +0000261
262 /// TransformFn - The transformation function to execute on this node before
263 /// it can be substituted into the resulting instruction on a pattern match.
264 Record *TransformFn;
265
266 std::vector<TreePatternNode*> Children;
267public:
268 TreePatternNode(Record *Op, const std::vector<TreePatternNode*> &Ch)
Chris Lattner2cacec52010-03-15 06:00:16 +0000269 : Operator(Op), Val(0), TransformFn(0), Children(Ch) { }
Chris Lattner6cefb772008-01-05 22:25:12 +0000270 TreePatternNode(Init *val) // leaf ctor
Chris Lattner2cacec52010-03-15 06:00:16 +0000271 : Operator(0), Val(val), TransformFn(0) {
Chris Lattner6cefb772008-01-05 22:25:12 +0000272 }
273 ~TreePatternNode();
274
275 const std::string &getName() const { return Name; }
276 void setName(const std::string &N) { Name = N; }
277
278 bool isLeaf() const { return Val != 0; }
Chris Lattner2cacec52010-03-15 06:00:16 +0000279
280 // Type accessors.
281 MVT::SimpleValueType getType() const { return Type.getConcrete(); }
282 const EEVT::TypeSet &getExtType() const { return Type; }
283 EEVT::TypeSet &getExtType() { return Type; }
284 void setType(const EEVT::TypeSet &T) { Type = T; }
285
286 bool hasTypeSet() const { return Type.isConcrete(); }
287 bool isTypeCompletelyUnknown() const { return Type.isCompletelyUnknown(); }
288 bool isTypeDynamicallyResolved() const { return Type.isDynamicallyResolved();}
Chris Lattner6cefb772008-01-05 22:25:12 +0000289
290 Init *getLeafValue() const { assert(isLeaf()); return Val; }
291 Record *getOperator() const { assert(!isLeaf()); return Operator; }
292
293 unsigned getNumChildren() const { return Children.size(); }
294 TreePatternNode *getChild(unsigned N) const { return Children[N]; }
295 void setChild(unsigned i, TreePatternNode *N) {
296 Children[i] = N;
297 }
Chris Lattnere39650a2010-02-16 06:10:58 +0000298
299 /// hasChild - Return true if N is any of our children.
300 bool hasChild(const TreePatternNode *N) const {
301 for (unsigned i = 0, e = Children.size(); i != e; ++i)
302 if (Children[i] == N) return true;
303 return false;
304 }
Chris Lattnere67bde52008-01-06 05:36:50 +0000305
Chris Lattner47661322010-02-14 22:22:58 +0000306 const std::vector<std::string> &getPredicateFns() const {return PredicateFns;}
Dan Gohman0540e172008-10-15 06:17:21 +0000307 void clearPredicateFns() { PredicateFns.clear(); }
308 void setPredicateFns(const std::vector<std::string> &Fns) {
309 assert(PredicateFns.empty() && "Overwriting non-empty predicate list!");
310 PredicateFns = Fns;
311 }
312 void addPredicateFn(const std::string &Fn) {
313 assert(!Fn.empty() && "Empty predicate string!");
314 if (std::find(PredicateFns.begin(), PredicateFns.end(), Fn) ==
315 PredicateFns.end())
316 PredicateFns.push_back(Fn);
317 }
Chris Lattner6cefb772008-01-05 22:25:12 +0000318
319 Record *getTransformFn() const { return TransformFn; }
320 void setTransformFn(Record *Fn) { TransformFn = Fn; }
321
Chris Lattnere67bde52008-01-06 05:36:50 +0000322 /// getIntrinsicInfo - If this node corresponds to an intrinsic, return the
323 /// CodeGenIntrinsic information for it, otherwise return a null pointer.
324 const CodeGenIntrinsic *getIntrinsicInfo(const CodeGenDAGPatterns &CDP) const;
Evan Cheng6bd95672008-06-16 20:29:38 +0000325
Chris Lattner47661322010-02-14 22:22:58 +0000326 /// getComplexPatternInfo - If this node corresponds to a ComplexPattern,
327 /// return the ComplexPattern information, otherwise return null.
328 const ComplexPattern *
329 getComplexPatternInfo(const CodeGenDAGPatterns &CGP) const;
330
331 /// NodeHasProperty - Return true if this node has the specified property.
Chris Lattner751d5aa2010-02-14 22:33:49 +0000332 bool NodeHasProperty(SDNP Property, const CodeGenDAGPatterns &CGP) const;
Chris Lattner47661322010-02-14 22:22:58 +0000333
334 /// TreeHasProperty - Return true if any node in this tree has the specified
335 /// property.
Chris Lattner751d5aa2010-02-14 22:33:49 +0000336 bool TreeHasProperty(SDNP Property, const CodeGenDAGPatterns &CGP) const;
Chris Lattner47661322010-02-14 22:22:58 +0000337
Evan Cheng6bd95672008-06-16 20:29:38 +0000338 /// isCommutativeIntrinsic - Return true if the node is an intrinsic which is
339 /// marked isCommutative.
340 bool isCommutativeIntrinsic(const CodeGenDAGPatterns &CDP) const;
Chris Lattnere67bde52008-01-06 05:36:50 +0000341
Daniel Dunbar1a551802009-07-03 00:10:29 +0000342 void print(raw_ostream &OS) const;
Chris Lattner6cefb772008-01-05 22:25:12 +0000343 void dump() const;
344
345public: // Higher level manipulation routines.
346
347 /// clone - Return a new copy of this tree.
348 ///
349 TreePatternNode *clone() const;
Chris Lattner47661322010-02-14 22:22:58 +0000350
351 /// RemoveAllTypes - Recursively strip all the types of this tree.
352 void RemoveAllTypes();
Chris Lattner6cefb772008-01-05 22:25:12 +0000353
354 /// isIsomorphicTo - Return true if this node is recursively isomorphic to
355 /// the specified node. For this comparison, all of the state of the node
356 /// is considered, except for the assigned name. Nodes with differing names
357 /// that are otherwise identical are considered isomorphic.
Scott Michel327d0652008-03-05 17:49:05 +0000358 bool isIsomorphicTo(const TreePatternNode *N,
359 const MultipleUseVarSet &DepVars) const;
Chris Lattner6cefb772008-01-05 22:25:12 +0000360
361 /// SubstituteFormalArguments - Replace the formal arguments in this tree
362 /// with actual values specified by ArgMap.
363 void SubstituteFormalArguments(std::map<std::string,
364 TreePatternNode*> &ArgMap);
365
366 /// InlinePatternFragments - If this pattern refers to any pattern
367 /// fragments, inline them into place, giving us a pattern without any
368 /// PatFrag references.
369 TreePatternNode *InlinePatternFragments(TreePattern &TP);
370
Bob Wilson6c01ca92009-01-05 17:23:09 +0000371 /// ApplyTypeConstraints - Apply all of the type constraints relevant to
Chris Lattner6cefb772008-01-05 22:25:12 +0000372 /// this node and its children in the tree. This returns true if it makes a
373 /// change, false otherwise. If a type contradiction is found, throw an
374 /// exception.
375 bool ApplyTypeConstraints(TreePattern &TP, bool NotRegisters);
376
377 /// UpdateNodeType - Set the node type of N to VT if VT contains
378 /// information. If N already contains a conflicting type, then throw an
379 /// exception. This returns true if any information was updated.
380 ///
Chris Lattner2cacec52010-03-15 06:00:16 +0000381 bool UpdateNodeType(const EEVT::TypeSet &InTy, TreePattern &TP) {
382 return Type.MergeInTypeInfo(InTy, TP);
383 }
384
385 bool UpdateNodeType(MVT::SimpleValueType InTy, TreePattern &TP) {
386 return Type.MergeInTypeInfo(EEVT::TypeSet(InTy, TP), TP);
Chris Lattner6cefb772008-01-05 22:25:12 +0000387 }
388
389 /// ContainsUnresolvedType - Return true if this tree contains any
390 /// unresolved types.
391 bool ContainsUnresolvedType() const {
Chris Lattner2cacec52010-03-15 06:00:16 +0000392 if (!hasTypeSet()) return true;
Chris Lattner6cefb772008-01-05 22:25:12 +0000393 for (unsigned i = 0, e = getNumChildren(); i != e; ++i)
394 if (getChild(i)->ContainsUnresolvedType()) return true;
395 return false;
396 }
397
398 /// canPatternMatch - If it is impossible for this pattern to match on this
399 /// target, fill in Reason and return false. Otherwise, return true.
Dan Gohmanee4fa192008-04-03 00:02:49 +0000400 bool canPatternMatch(std::string &Reason, const CodeGenDAGPatterns &CDP);
Chris Lattner6cefb772008-01-05 22:25:12 +0000401};
402
Chris Lattner383fed92010-02-14 21:10:33 +0000403inline raw_ostream &operator<<(raw_ostream &OS, const TreePatternNode &TPN) {
404 TPN.print(OS);
405 return OS;
406}
407
Chris Lattner6cefb772008-01-05 22:25:12 +0000408
409/// TreePattern - Represent a pattern, used for instructions, pattern
410/// fragments, etc.
411///
412class TreePattern {
413 /// Trees - The list of pattern trees which corresponds to this pattern.
414 /// Note that PatFrag's only have a single tree.
415 ///
416 std::vector<TreePatternNode*> Trees;
417
Chris Lattner2cacec52010-03-15 06:00:16 +0000418 /// NamedNodes - This is all of the nodes that have names in the trees in this
419 /// pattern.
420 StringMap<SmallVector<TreePatternNode*,1> > NamedNodes;
421
Chris Lattner6cefb772008-01-05 22:25:12 +0000422 /// TheRecord - The actual TableGen record corresponding to this pattern.
423 ///
424 Record *TheRecord;
425
426 /// Args - This is a list of all of the arguments to this pattern (for
427 /// PatFrag patterns), which are the 'node' markers in this pattern.
428 std::vector<std::string> Args;
429
430 /// CDP - the top-level object coordinating this madness.
431 ///
Chris Lattnerfe718932008-01-06 01:10:31 +0000432 CodeGenDAGPatterns &CDP;
Chris Lattner6cefb772008-01-05 22:25:12 +0000433
434 /// isInputPattern - True if this is an input pattern, something to match.
435 /// False if this is an output pattern, something to emit.
436 bool isInputPattern;
437public:
438
439 /// TreePattern constructor - Parse the specified DagInits into the
440 /// current record.
441 TreePattern(Record *TheRec, ListInit *RawPat, bool isInput,
Chris Lattnerfe718932008-01-06 01:10:31 +0000442 CodeGenDAGPatterns &ise);
Chris Lattner6cefb772008-01-05 22:25:12 +0000443 TreePattern(Record *TheRec, DagInit *Pat, bool isInput,
Chris Lattnerfe718932008-01-06 01:10:31 +0000444 CodeGenDAGPatterns &ise);
Chris Lattner6cefb772008-01-05 22:25:12 +0000445 TreePattern(Record *TheRec, TreePatternNode *Pat, bool isInput,
Chris Lattnerfe718932008-01-06 01:10:31 +0000446 CodeGenDAGPatterns &ise);
Chris Lattner6cefb772008-01-05 22:25:12 +0000447
448 /// getTrees - Return the tree patterns which corresponds to this pattern.
449 ///
450 const std::vector<TreePatternNode*> &getTrees() const { return Trees; }
451 unsigned getNumTrees() const { return Trees.size(); }
452 TreePatternNode *getTree(unsigned i) const { return Trees[i]; }
453 TreePatternNode *getOnlyTree() const {
454 assert(Trees.size() == 1 && "Doesn't have exactly one pattern!");
455 return Trees[0];
456 }
Chris Lattner2cacec52010-03-15 06:00:16 +0000457
458 const StringMap<SmallVector<TreePatternNode*,1> > &getNamedNodesMap() {
459 if (NamedNodes.empty())
460 ComputeNamedNodes();
461 return NamedNodes;
462 }
Chris Lattner6cefb772008-01-05 22:25:12 +0000463
464 /// getRecord - Return the actual TableGen record corresponding to this
465 /// pattern.
466 ///
467 Record *getRecord() const { return TheRecord; }
468
469 unsigned getNumArgs() const { return Args.size(); }
470 const std::string &getArgName(unsigned i) const {
471 assert(i < Args.size() && "Argument reference out of range!");
472 return Args[i];
473 }
474 std::vector<std::string> &getArgList() { return Args; }
475
Chris Lattnerfe718932008-01-06 01:10:31 +0000476 CodeGenDAGPatterns &getDAGPatterns() const { return CDP; }
Chris Lattner6cefb772008-01-05 22:25:12 +0000477
478 /// InlinePatternFragments - If this pattern refers to any pattern
479 /// fragments, inline them into place, giving us a pattern without any
480 /// PatFrag references.
481 void InlinePatternFragments() {
482 for (unsigned i = 0, e = Trees.size(); i != e; ++i)
483 Trees[i] = Trees[i]->InlinePatternFragments(*this);
484 }
485
486 /// InferAllTypes - Infer/propagate as many types throughout the expression
Jim Grosbachda4231f2009-03-26 16:17:51 +0000487 /// patterns as possible. Return true if all types are inferred, false
Chris Lattner6cefb772008-01-05 22:25:12 +0000488 /// otherwise. Throw an exception if a type contradiction is found.
Chris Lattner2cacec52010-03-15 06:00:16 +0000489 bool InferAllTypes(const StringMap<SmallVector<TreePatternNode*,1> >
490 *NamedTypes=0);
Chris Lattner6cefb772008-01-05 22:25:12 +0000491
492 /// error - Throw an exception, prefixing it with information about this
493 /// pattern.
494 void error(const std::string &Msg) const;
495
Daniel Dunbar1a551802009-07-03 00:10:29 +0000496 void print(raw_ostream &OS) const;
Chris Lattner6cefb772008-01-05 22:25:12 +0000497 void dump() const;
498
499private:
500 TreePatternNode *ParseTreePattern(DagInit *DI);
Chris Lattner2cacec52010-03-15 06:00:16 +0000501 void ComputeNamedNodes();
502 void ComputeNamedNodes(TreePatternNode *N);
Chris Lattner6cefb772008-01-05 22:25:12 +0000503};
504
505/// DAGDefaultOperand - One of these is created for each PredicateOperand
506/// or OptionalDefOperand that has a set ExecuteAlways / DefaultOps field.
507struct DAGDefaultOperand {
508 std::vector<TreePatternNode*> DefaultOps;
509};
510
511class DAGInstruction {
512 TreePattern *Pattern;
513 std::vector<Record*> Results;
514 std::vector<Record*> Operands;
515 std::vector<Record*> ImpResults;
516 std::vector<Record*> ImpOperands;
517 TreePatternNode *ResultPattern;
518public:
519 DAGInstruction(TreePattern *TP,
520 const std::vector<Record*> &results,
521 const std::vector<Record*> &operands,
522 const std::vector<Record*> &impresults,
523 const std::vector<Record*> &impoperands)
524 : Pattern(TP), Results(results), Operands(operands),
525 ImpResults(impresults), ImpOperands(impoperands),
526 ResultPattern(0) {}
527
Chris Lattnerf1ab4f12008-01-06 01:52:22 +0000528 const TreePattern *getPattern() const { return Pattern; }
Chris Lattner6cefb772008-01-05 22:25:12 +0000529 unsigned getNumResults() const { return Results.size(); }
530 unsigned getNumOperands() const { return Operands.size(); }
531 unsigned getNumImpResults() const { return ImpResults.size(); }
532 unsigned getNumImpOperands() const { return ImpOperands.size(); }
533 const std::vector<Record*>& getImpResults() const { return ImpResults; }
534
535 void setResultPattern(TreePatternNode *R) { ResultPattern = R; }
536
537 Record *getResult(unsigned RN) const {
538 assert(RN < Results.size());
539 return Results[RN];
540 }
541
542 Record *getOperand(unsigned ON) const {
543 assert(ON < Operands.size());
544 return Operands[ON];
545 }
546
547 Record *getImpResult(unsigned RN) const {
548 assert(RN < ImpResults.size());
549 return ImpResults[RN];
550 }
551
552 Record *getImpOperand(unsigned ON) const {
553 assert(ON < ImpOperands.size());
554 return ImpOperands[ON];
555 }
556
557 TreePatternNode *getResultPattern() const { return ResultPattern; }
558};
559
Chris Lattnerfe718932008-01-06 01:10:31 +0000560/// PatternToMatch - Used by CodeGenDAGPatterns to keep tab of patterns
Chris Lattner6cefb772008-01-05 22:25:12 +0000561/// processed to produce isel.
Chris Lattnerb49985a2010-02-18 06:47:49 +0000562class PatternToMatch {
563public:
Chris Lattner6cefb772008-01-05 22:25:12 +0000564 PatternToMatch(ListInit *preds,
565 TreePatternNode *src, TreePatternNode *dst,
566 const std::vector<Record*> &dstregs,
Chris Lattner117ccb72010-03-01 22:09:11 +0000567 unsigned complexity, unsigned uid)
568 : Predicates(preds), SrcPattern(src), DstPattern(dst),
569 Dstregs(dstregs), AddedComplexity(complexity), ID(uid) {}
Chris Lattner6cefb772008-01-05 22:25:12 +0000570
571 ListInit *Predicates; // Top level predicate conditions to match.
572 TreePatternNode *SrcPattern; // Source pattern to match.
573 TreePatternNode *DstPattern; // Resulting pattern.
574 std::vector<Record*> Dstregs; // Physical register defs being matched.
575 unsigned AddedComplexity; // Add to matching pattern complexity.
Chris Lattner117ccb72010-03-01 22:09:11 +0000576 unsigned ID; // Unique ID for the record.
Chris Lattner6cefb772008-01-05 22:25:12 +0000577
578 ListInit *getPredicates() const { return Predicates; }
579 TreePatternNode *getSrcPattern() const { return SrcPattern; }
580 TreePatternNode *getDstPattern() const { return DstPattern; }
581 const std::vector<Record*> &getDstRegs() const { return Dstregs; }
582 unsigned getAddedComplexity() const { return AddedComplexity; }
Dan Gohman22bb3112008-08-22 00:20:26 +0000583
584 std::string getPredicateCheck() const;
Chris Lattner6cefb772008-01-05 22:25:12 +0000585};
586
Daniel Dunbar6f5cc822009-08-23 09:47:37 +0000587// Deterministic comparison of Record*.
588struct RecordPtrCmp {
589 bool operator()(const Record *LHS, const Record *RHS) const;
590};
Chris Lattner6cefb772008-01-05 22:25:12 +0000591
Chris Lattnerfe718932008-01-06 01:10:31 +0000592class CodeGenDAGPatterns {
Chris Lattner6cefb772008-01-05 22:25:12 +0000593 RecordKeeper &Records;
594 CodeGenTarget Target;
595 std::vector<CodeGenIntrinsic> Intrinsics;
Dale Johannesen49de9822009-02-05 01:49:45 +0000596 std::vector<CodeGenIntrinsic> TgtIntrinsics;
Chris Lattner6cefb772008-01-05 22:25:12 +0000597
Daniel Dunbar6f5cc822009-08-23 09:47:37 +0000598 std::map<Record*, SDNodeInfo, RecordPtrCmp> SDNodes;
599 std::map<Record*, std::pair<Record*, std::string>, RecordPtrCmp> SDNodeXForms;
600 std::map<Record*, ComplexPattern, RecordPtrCmp> ComplexPatterns;
601 std::map<Record*, TreePattern*, RecordPtrCmp> PatternFragments;
602 std::map<Record*, DAGDefaultOperand, RecordPtrCmp> DefaultOperands;
603 std::map<Record*, DAGInstruction, RecordPtrCmp> Instructions;
Chris Lattner6cefb772008-01-05 22:25:12 +0000604
605 // Specific SDNode definitions:
606 Record *intrinsic_void_sdnode;
607 Record *intrinsic_w_chain_sdnode, *intrinsic_wo_chain_sdnode;
608
609 /// PatternsToMatch - All of the things we are matching on the DAG. The first
610 /// value is the pattern to match, the second pattern is the result to
611 /// emit.
612 std::vector<PatternToMatch> PatternsToMatch;
613public:
Chris Lattnerfe718932008-01-06 01:10:31 +0000614 CodeGenDAGPatterns(RecordKeeper &R);
615 ~CodeGenDAGPatterns();
Chris Lattner6cefb772008-01-05 22:25:12 +0000616
Dan Gohmanee4fa192008-04-03 00:02:49 +0000617 CodeGenTarget &getTargetInfo() { return Target; }
Chris Lattner6cefb772008-01-05 22:25:12 +0000618 const CodeGenTarget &getTargetInfo() const { return Target; }
619
620 Record *getSDNodeNamed(const std::string &Name) const;
621
622 const SDNodeInfo &getSDNodeInfo(Record *R) const {
623 assert(SDNodes.count(R) && "Unknown node!");
624 return SDNodes.find(R)->second;
625 }
626
Chris Lattner443e3f92008-01-05 22:54:53 +0000627 // Node transformation lookups.
628 typedef std::pair<Record*, std::string> NodeXForm;
629 const NodeXForm &getSDNodeTransform(Record *R) const {
Chris Lattner6cefb772008-01-05 22:25:12 +0000630 assert(SDNodeXForms.count(R) && "Invalid transform!");
631 return SDNodeXForms.find(R)->second;
632 }
633
Benjamin Kramer5b9e7ef2009-08-23 10:39:21 +0000634 typedef std::map<Record*, NodeXForm, RecordPtrCmp>::const_iterator
635 nx_iterator;
Chris Lattner443e3f92008-01-05 22:54:53 +0000636 nx_iterator nx_begin() const { return SDNodeXForms.begin(); }
637 nx_iterator nx_end() const { return SDNodeXForms.end(); }
638
639
Chris Lattner6cefb772008-01-05 22:25:12 +0000640 const ComplexPattern &getComplexPattern(Record *R) const {
641 assert(ComplexPatterns.count(R) && "Unknown addressing mode!");
642 return ComplexPatterns.find(R)->second;
643 }
644
645 const CodeGenIntrinsic &getIntrinsic(Record *R) const {
646 for (unsigned i = 0, e = Intrinsics.size(); i != e; ++i)
647 if (Intrinsics[i].TheDef == R) return Intrinsics[i];
Dale Johannesen49de9822009-02-05 01:49:45 +0000648 for (unsigned i = 0, e = TgtIntrinsics.size(); i != e; ++i)
649 if (TgtIntrinsics[i].TheDef == R) return TgtIntrinsics[i];
Chris Lattner6cefb772008-01-05 22:25:12 +0000650 assert(0 && "Unknown intrinsic!");
651 abort();
652 }
653
654 const CodeGenIntrinsic &getIntrinsicInfo(unsigned IID) const {
Dale Johannesen49de9822009-02-05 01:49:45 +0000655 if (IID-1 < Intrinsics.size())
656 return Intrinsics[IID-1];
657 if (IID-Intrinsics.size()-1 < TgtIntrinsics.size())
658 return TgtIntrinsics[IID-Intrinsics.size()-1];
659 assert(0 && "Bad intrinsic ID!");
Evan Cheng32c1a4c2009-02-09 03:07:24 +0000660 abort();
Chris Lattner6cefb772008-01-05 22:25:12 +0000661 }
662
663 unsigned getIntrinsicID(Record *R) const {
664 for (unsigned i = 0, e = Intrinsics.size(); i != e; ++i)
665 if (Intrinsics[i].TheDef == R) return i;
Dale Johannesen49de9822009-02-05 01:49:45 +0000666 for (unsigned i = 0, e = TgtIntrinsics.size(); i != e; ++i)
667 if (TgtIntrinsics[i].TheDef == R) return i + Intrinsics.size();
Chris Lattner6cefb772008-01-05 22:25:12 +0000668 assert(0 && "Unknown intrinsic!");
669 abort();
670 }
671
Chris Lattnerb49985a2010-02-18 06:47:49 +0000672 const DAGDefaultOperand &getDefaultOperand(Record *R) const {
Chris Lattner6cefb772008-01-05 22:25:12 +0000673 assert(DefaultOperands.count(R) &&"Isn't an analyzed default operand!");
674 return DefaultOperands.find(R)->second;
675 }
676
677 // Pattern Fragment information.
678 TreePattern *getPatternFragment(Record *R) const {
679 assert(PatternFragments.count(R) && "Invalid pattern fragment request!");
680 return PatternFragments.find(R)->second;
681 }
Benjamin Kramer5b9e7ef2009-08-23 10:39:21 +0000682 typedef std::map<Record*, TreePattern*, RecordPtrCmp>::const_iterator
683 pf_iterator;
Chris Lattner6cefb772008-01-05 22:25:12 +0000684 pf_iterator pf_begin() const { return PatternFragments.begin(); }
685 pf_iterator pf_end() const { return PatternFragments.end(); }
686
687 // Patterns to match information.
Chris Lattner60d81392008-01-05 22:30:17 +0000688 typedef std::vector<PatternToMatch>::const_iterator ptm_iterator;
689 ptm_iterator ptm_begin() const { return PatternsToMatch.begin(); }
690 ptm_iterator ptm_end() const { return PatternsToMatch.end(); }
Chris Lattner6cefb772008-01-05 22:25:12 +0000691
692
693
694 const DAGInstruction &getInstruction(Record *R) const {
695 assert(Instructions.count(R) && "Unknown instruction!");
696 return Instructions.find(R)->second;
697 }
698
699 Record *get_intrinsic_void_sdnode() const {
700 return intrinsic_void_sdnode;
701 }
702 Record *get_intrinsic_w_chain_sdnode() const {
703 return intrinsic_w_chain_sdnode;
704 }
705 Record *get_intrinsic_wo_chain_sdnode() const {
706 return intrinsic_wo_chain_sdnode;
707 }
708
Jakob Stoklund Olesen11ee5082009-10-15 18:50:03 +0000709 bool hasTargetIntrinsics() { return !TgtIntrinsics.empty(); }
710
Chris Lattner6cefb772008-01-05 22:25:12 +0000711private:
712 void ParseNodeInfo();
Chris Lattner443e3f92008-01-05 22:54:53 +0000713 void ParseNodeTransforms();
Chris Lattner6cefb772008-01-05 22:25:12 +0000714 void ParseComplexPatterns();
Chris Lattnerdc32f982008-01-05 22:43:57 +0000715 void ParsePatternFragments();
Chris Lattner6cefb772008-01-05 22:25:12 +0000716 void ParseDefaultOperands();
717 void ParseInstructions();
718 void ParsePatterns();
Dan Gohmanee4fa192008-04-03 00:02:49 +0000719 void InferInstructionFlags();
Chris Lattner6cefb772008-01-05 22:25:12 +0000720 void GenerateVariants();
721
Chris Lattner25b6f912010-02-23 06:16:51 +0000722 void AddPatternToMatch(const TreePattern *Pattern, const PatternToMatch &PTM);
Chris Lattner6cefb772008-01-05 22:25:12 +0000723 void FindPatternInputsAndOutputs(TreePattern *I, TreePatternNode *Pat,
724 std::map<std::string,
725 TreePatternNode*> &InstInputs,
726 std::map<std::string,
727 TreePatternNode*> &InstResults,
728 std::vector<Record*> &InstImpInputs,
729 std::vector<Record*> &InstImpResults);
730};
731} // end namespace llvm
732
733#endif