blob: 44f82fe68469a4184eee1489b4ff512db0924515 [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
Scott Michel327d0652008-03-05 17:49:05 +000018#include <set>
Dan Gohman0540e172008-10-15 06:17:21 +000019#include <algorithm>
20#include <vector>
Scott Michel327d0652008-03-05 17:49:05 +000021
Chris Lattner6cefb772008-01-05 22:25:12 +000022#include "CodeGenTarget.h"
23#include "CodeGenIntrinsics.h"
Chris Lattner2cacec52010-03-15 06:00:16 +000024#include "llvm/ADT/SmallVector.h"
25#include "llvm/ADT/StringMap.h"
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 Lattner6cefb772008-01-05 22:25:12 +000044 enum DAGISelGenValueType {
Chris Lattner2cacec52010-03-15 06:00:16 +000045 // FIXME: Remove EEVT::isUnknown!
Bob Wilsoncdfa01b2009-08-29 05:53:25 +000046 isUnknown = MVT::LAST_VALUETYPE
Chris Lattner6cefb772008-01-05 22:25:12 +000047 };
Chris Lattner2cacec52010-03-15 06:00:16 +000048
49 /// TypeSet - This is either empty if it's completely unknown, or holds a set
50 /// of types. It is used during type inference because register classes can
51 /// have multiple possible types and we don't know which one they get until
52 /// type inference is complete.
53 ///
54 /// TypeSet can have three states:
55 /// Vector is empty: The type is completely unknown, it can be any valid
56 /// target type.
57 /// Vector has multiple constrained types: (e.g. v4i32 + v4f32) it is one
58 /// of those types only.
59 /// Vector has one concrete type: The type is completely known.
60 ///
61 class TypeSet {
62 SmallVector<MVT::SimpleValueType, 2> TypeVec;
63 public:
64 TypeSet() {}
65 TypeSet(MVT::SimpleValueType VT, TreePattern &TP);
66 TypeSet(const std::vector<MVT::SimpleValueType> &VTList);
67
68 bool isCompletelyUnknown() const { return TypeVec.empty(); }
69
70 bool isConcrete() const {
71 if (TypeVec.size() != 1) return false;
72 unsigned char T = TypeVec[0]; (void)T;
73 assert(T < MVT::LAST_VALUETYPE || T == MVT::iPTR || T == MVT::iPTRAny);
74 return true;
75 }
76
77 MVT::SimpleValueType getConcrete() const {
78 assert(isConcrete() && "Type isn't concrete yet");
79 return (MVT::SimpleValueType)TypeVec[0];
80 }
81
82 bool isDynamicallyResolved() const {
83 return getConcrete() == MVT::iPTR || getConcrete() == MVT::iPTRAny;
84 }
85
86 const SmallVectorImpl<MVT::SimpleValueType> &getTypeList() const {
87 assert(!TypeVec.empty() && "Not a type list!");
88 return TypeVec;
89 }
90
91 /// hasIntegerTypes - Return true if this TypeSet contains any integer value
92 /// types.
93 bool hasIntegerTypes() const;
94
95 /// hasFloatingPointTypes - Return true if this TypeSet contains an fAny or
96 /// a floating point value type.
97 bool hasFloatingPointTypes() const;
98
99 /// hasVectorTypes - Return true if this TypeSet contains a vector value
100 /// type.
101 bool hasVectorTypes() const;
102
103 /// getName() - Return this TypeSet as a string.
104 std::string getName() const;
105
106 /// MergeInTypeInfo - This merges in type information from the specified
107 /// argument. If 'this' changes, it returns true. If the two types are
108 /// contradictory (e.g. merge f32 into i32) then this throws an exception.
109 bool MergeInTypeInfo(const EEVT::TypeSet &InVT, TreePattern &TP);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000110
Chris Lattner2cacec52010-03-15 06:00:16 +0000111 bool MergeInTypeInfo(MVT::SimpleValueType InVT, TreePattern &TP) {
112 return MergeInTypeInfo(EEVT::TypeSet(InVT, TP), TP);
113 }
Chris Lattner6cefb772008-01-05 22:25:12 +0000114
Chris Lattner2cacec52010-03-15 06:00:16 +0000115 /// Force this type list to only contain integer types.
116 bool EnforceInteger(TreePattern &TP);
Bob Wilson36e3e662009-08-12 22:30:59 +0000117
Chris Lattner2cacec52010-03-15 06:00:16 +0000118 /// Force this type list to only contain floating point types.
119 bool EnforceFloatingPoint(TreePattern &TP);
120
121 /// EnforceScalar - Remove all vector types from this type list.
122 bool EnforceScalar(TreePattern &TP);
123
124 /// EnforceVector - Remove all non-vector types from this type list.
125 bool EnforceVector(TreePattern &TP);
126
127 /// EnforceSmallerThan - 'this' must be a smaller VT than Other. Update
128 /// this an other based on this information.
129 bool EnforceSmallerThan(EEVT::TypeSet &Other, TreePattern &TP);
130
131 /// EnforceVectorEltTypeIs - 'this' is now constrainted to be a vector type
132 /// whose element is VT.
133 bool EnforceVectorEltTypeIs(MVT::SimpleValueType VT, TreePattern &TP);
134
135 bool operator!=(const TypeSet &RHS) const { return TypeVec != RHS.TypeVec; }
136 bool operator==(const TypeSet &RHS) const { return TypeVec == RHS.TypeVec; }
137 };
Chris Lattner6cefb772008-01-05 22:25:12 +0000138}
139
Scott Michel327d0652008-03-05 17:49:05 +0000140/// Set type used to track multiply used variables in patterns
141typedef std::set<std::string> MultipleUseVarSet;
142
Chris Lattner6cefb772008-01-05 22:25:12 +0000143/// SDTypeConstraint - This is a discriminated union of constraints,
144/// corresponding to the SDTypeConstraint tablegen class in Target.td.
145struct SDTypeConstraint {
146 SDTypeConstraint(Record *R);
147
148 unsigned OperandNo; // The operand # this constraint applies to.
149 enum {
Bob Wilson36e3e662009-08-12 22:30:59 +0000150 SDTCisVT, SDTCisPtrTy, SDTCisInt, SDTCisFP, SDTCisVec, SDTCisSameAs,
Nate Begeman9008ca62009-04-27 18:41:29 +0000151 SDTCisVTSmallerThanOp, SDTCisOpSmallerThanOp, SDTCisEltOfVec
Chris Lattner6cefb772008-01-05 22:25:12 +0000152 } ConstraintType;
153
154 union { // The discriminated union.
155 struct {
Chris Lattner2cacec52010-03-15 06:00:16 +0000156 MVT::SimpleValueType VT;
Chris Lattner6cefb772008-01-05 22:25:12 +0000157 } SDTCisVT_Info;
158 struct {
159 unsigned OtherOperandNum;
160 } SDTCisSameAs_Info;
161 struct {
162 unsigned OtherOperandNum;
163 } SDTCisVTSmallerThanOp_Info;
164 struct {
165 unsigned BigOperandNum;
166 } SDTCisOpSmallerThanOp_Info;
167 struct {
168 unsigned OtherOperandNum;
Nate Begemanb5af3342008-02-09 01:37:05 +0000169 } SDTCisEltOfVec_Info;
Chris Lattner6cefb772008-01-05 22:25:12 +0000170 } x;
171
172 /// ApplyTypeConstraint - Given a node in a pattern, apply this type
173 /// constraint to the nodes operands. This returns true if it makes a
174 /// change, false otherwise. If a type contradiction is found, throw an
175 /// exception.
176 bool ApplyTypeConstraint(TreePatternNode *N, const SDNodeInfo &NodeInfo,
177 TreePattern &TP) const;
178
179 /// getOperandNum - Return the node corresponding to operand #OpNo in tree
180 /// N, which has NumResults results.
181 TreePatternNode *getOperandNum(unsigned OpNo, TreePatternNode *N,
182 unsigned NumResults) const;
183};
184
185/// SDNodeInfo - One of these records is created for each SDNode instance in
186/// the target .td file. This represents the various dag nodes we will be
187/// processing.
188class SDNodeInfo {
189 Record *Def;
190 std::string EnumName;
191 std::string SDClassName;
192 unsigned Properties;
193 unsigned NumResults;
194 int NumOperands;
195 std::vector<SDTypeConstraint> TypeConstraints;
196public:
197 SDNodeInfo(Record *R); // Parse the specified record.
198
199 unsigned getNumResults() const { return NumResults; }
200 int getNumOperands() const { return NumOperands; }
201 Record *getRecord() const { return Def; }
202 const std::string &getEnumName() const { return EnumName; }
203 const std::string &getSDClassName() const { return SDClassName; }
204
205 const std::vector<SDTypeConstraint> &getTypeConstraints() const {
206 return TypeConstraints;
207 }
208
Chris Lattner22579812010-02-28 00:22:30 +0000209 /// getKnownType - If the type constraints on this node imply a fixed type
210 /// (e.g. all stores return void, etc), then return it as an
211 /// MVT::SimpleValueType. Otherwise, return EEVT::isUnknown.
212 unsigned getKnownType() const;
213
Chris Lattner6cefb772008-01-05 22:25:12 +0000214 /// hasProperty - Return true if this node has the specified property.
215 ///
216 bool hasProperty(enum SDNP Prop) const { return Properties & (1 << Prop); }
217
218 /// ApplyTypeConstraints - Given a node in a pattern, apply the type
219 /// constraints for this node to the operands of the node. This returns
220 /// true if it makes a change, false otherwise. If a type contradiction is
221 /// found, throw an exception.
222 bool ApplyTypeConstraints(TreePatternNode *N, TreePattern &TP) const {
223 bool MadeChange = false;
224 for (unsigned i = 0, e = TypeConstraints.size(); i != e; ++i)
225 MadeChange |= TypeConstraints[i].ApplyTypeConstraint(N, *this, TP);
226 return MadeChange;
227 }
228};
229
230/// FIXME: TreePatternNode's can be shared in some cases (due to dag-shaped
231/// patterns), and as such should be ref counted. We currently just leak all
232/// TreePatternNode objects!
233class TreePatternNode {
Chris Lattner2cacec52010-03-15 06:00:16 +0000234 /// The type of this node. Before and during type inference, this may be a
235 /// set of possible types. After (successful) type inference, this is a
236 /// single type.
237 EEVT::TypeSet Type;
Chris Lattner6cefb772008-01-05 22:25:12 +0000238
239 /// Operator - The Record for the operator if this is an interior node (not
240 /// a leaf).
241 Record *Operator;
242
243 /// Val - The init value (e.g. the "GPRC" record, or "7") for a leaf.
244 ///
245 Init *Val;
246
247 /// Name - The name given to this node with the :$foo notation.
248 ///
249 std::string Name;
250
Dan Gohman0540e172008-10-15 06:17:21 +0000251 /// PredicateFns - The predicate functions to execute on this node to check
252 /// for a match. If this list is empty, no predicate is involved.
253 std::vector<std::string> PredicateFns;
Chris Lattner6cefb772008-01-05 22:25:12 +0000254
255 /// TransformFn - The transformation function to execute on this node before
256 /// it can be substituted into the resulting instruction on a pattern match.
257 Record *TransformFn;
258
259 std::vector<TreePatternNode*> Children;
260public:
261 TreePatternNode(Record *Op, const std::vector<TreePatternNode*> &Ch)
Chris Lattner2cacec52010-03-15 06:00:16 +0000262 : Operator(Op), Val(0), TransformFn(0), Children(Ch) { }
Chris Lattner6cefb772008-01-05 22:25:12 +0000263 TreePatternNode(Init *val) // leaf ctor
Chris Lattner2cacec52010-03-15 06:00:16 +0000264 : Operator(0), Val(val), TransformFn(0) {
Chris Lattner6cefb772008-01-05 22:25:12 +0000265 }
266 ~TreePatternNode();
267
268 const std::string &getName() const { return Name; }
269 void setName(const std::string &N) { Name = N; }
270
271 bool isLeaf() const { return Val != 0; }
Chris Lattner2cacec52010-03-15 06:00:16 +0000272
273 // Type accessors.
274 MVT::SimpleValueType getType() const { return Type.getConcrete(); }
275 const EEVT::TypeSet &getExtType() const { return Type; }
276 EEVT::TypeSet &getExtType() { return Type; }
277 void setType(const EEVT::TypeSet &T) { Type = T; }
278
279 bool hasTypeSet() const { return Type.isConcrete(); }
280 bool isTypeCompletelyUnknown() const { return Type.isCompletelyUnknown(); }
281 bool isTypeDynamicallyResolved() const { return Type.isDynamicallyResolved();}
Chris Lattner6cefb772008-01-05 22:25:12 +0000282
283 Init *getLeafValue() const { assert(isLeaf()); return Val; }
284 Record *getOperator() const { assert(!isLeaf()); return Operator; }
285
286 unsigned getNumChildren() const { return Children.size(); }
287 TreePatternNode *getChild(unsigned N) const { return Children[N]; }
288 void setChild(unsigned i, TreePatternNode *N) {
289 Children[i] = N;
290 }
Chris Lattnere39650a2010-02-16 06:10:58 +0000291
292 /// hasChild - Return true if N is any of our children.
293 bool hasChild(const TreePatternNode *N) const {
294 for (unsigned i = 0, e = Children.size(); i != e; ++i)
295 if (Children[i] == N) return true;
296 return false;
297 }
Chris Lattnere67bde52008-01-06 05:36:50 +0000298
Chris Lattner47661322010-02-14 22:22:58 +0000299 const std::vector<std::string> &getPredicateFns() const {return PredicateFns;}
Dan Gohman0540e172008-10-15 06:17:21 +0000300 void clearPredicateFns() { PredicateFns.clear(); }
301 void setPredicateFns(const std::vector<std::string> &Fns) {
302 assert(PredicateFns.empty() && "Overwriting non-empty predicate list!");
303 PredicateFns = Fns;
304 }
305 void addPredicateFn(const std::string &Fn) {
306 assert(!Fn.empty() && "Empty predicate string!");
307 if (std::find(PredicateFns.begin(), PredicateFns.end(), Fn) ==
308 PredicateFns.end())
309 PredicateFns.push_back(Fn);
310 }
Chris Lattner6cefb772008-01-05 22:25:12 +0000311
312 Record *getTransformFn() const { return TransformFn; }
313 void setTransformFn(Record *Fn) { TransformFn = Fn; }
314
Chris Lattnere67bde52008-01-06 05:36:50 +0000315 /// getIntrinsicInfo - If this node corresponds to an intrinsic, return the
316 /// CodeGenIntrinsic information for it, otherwise return a null pointer.
317 const CodeGenIntrinsic *getIntrinsicInfo(const CodeGenDAGPatterns &CDP) const;
Evan Cheng6bd95672008-06-16 20:29:38 +0000318
Chris Lattner47661322010-02-14 22:22:58 +0000319 /// getComplexPatternInfo - If this node corresponds to a ComplexPattern,
320 /// return the ComplexPattern information, otherwise return null.
321 const ComplexPattern *
322 getComplexPatternInfo(const CodeGenDAGPatterns &CGP) const;
323
324 /// NodeHasProperty - Return true if this node has the specified property.
Chris Lattner751d5aa2010-02-14 22:33:49 +0000325 bool NodeHasProperty(SDNP Property, const CodeGenDAGPatterns &CGP) const;
Chris Lattner47661322010-02-14 22:22:58 +0000326
327 /// TreeHasProperty - Return true if any node in this tree has the specified
328 /// property.
Chris Lattner751d5aa2010-02-14 22:33:49 +0000329 bool TreeHasProperty(SDNP Property, const CodeGenDAGPatterns &CGP) const;
Chris Lattner47661322010-02-14 22:22:58 +0000330
Evan Cheng6bd95672008-06-16 20:29:38 +0000331 /// isCommutativeIntrinsic - Return true if the node is an intrinsic which is
332 /// marked isCommutative.
333 bool isCommutativeIntrinsic(const CodeGenDAGPatterns &CDP) const;
Chris Lattnere67bde52008-01-06 05:36:50 +0000334
Daniel Dunbar1a551802009-07-03 00:10:29 +0000335 void print(raw_ostream &OS) const;
Chris Lattner6cefb772008-01-05 22:25:12 +0000336 void dump() const;
337
338public: // Higher level manipulation routines.
339
340 /// clone - Return a new copy of this tree.
341 ///
342 TreePatternNode *clone() const;
Chris Lattner47661322010-02-14 22:22:58 +0000343
344 /// RemoveAllTypes - Recursively strip all the types of this tree.
345 void RemoveAllTypes();
Chris Lattner6cefb772008-01-05 22:25:12 +0000346
347 /// isIsomorphicTo - Return true if this node is recursively isomorphic to
348 /// the specified node. For this comparison, all of the state of the node
349 /// is considered, except for the assigned name. Nodes with differing names
350 /// that are otherwise identical are considered isomorphic.
Scott Michel327d0652008-03-05 17:49:05 +0000351 bool isIsomorphicTo(const TreePatternNode *N,
352 const MultipleUseVarSet &DepVars) const;
Chris Lattner6cefb772008-01-05 22:25:12 +0000353
354 /// SubstituteFormalArguments - Replace the formal arguments in this tree
355 /// with actual values specified by ArgMap.
356 void SubstituteFormalArguments(std::map<std::string,
357 TreePatternNode*> &ArgMap);
358
359 /// InlinePatternFragments - If this pattern refers to any pattern
360 /// fragments, inline them into place, giving us a pattern without any
361 /// PatFrag references.
362 TreePatternNode *InlinePatternFragments(TreePattern &TP);
363
Bob Wilson6c01ca92009-01-05 17:23:09 +0000364 /// ApplyTypeConstraints - Apply all of the type constraints relevant to
Chris Lattner6cefb772008-01-05 22:25:12 +0000365 /// this node and its children in the tree. This returns true if it makes a
366 /// change, false otherwise. If a type contradiction is found, throw an
367 /// exception.
368 bool ApplyTypeConstraints(TreePattern &TP, bool NotRegisters);
369
370 /// UpdateNodeType - Set the node type of N to VT if VT contains
371 /// information. If N already contains a conflicting type, then throw an
372 /// exception. This returns true if any information was updated.
373 ///
Chris Lattner2cacec52010-03-15 06:00:16 +0000374 bool UpdateNodeType(const EEVT::TypeSet &InTy, TreePattern &TP) {
375 return Type.MergeInTypeInfo(InTy, TP);
376 }
377
378 bool UpdateNodeType(MVT::SimpleValueType InTy, TreePattern &TP) {
379 return Type.MergeInTypeInfo(EEVT::TypeSet(InTy, TP), TP);
Chris Lattner6cefb772008-01-05 22:25:12 +0000380 }
381
382 /// ContainsUnresolvedType - Return true if this tree contains any
383 /// unresolved types.
384 bool ContainsUnresolvedType() const {
Chris Lattner2cacec52010-03-15 06:00:16 +0000385 if (!hasTypeSet()) return true;
Chris Lattner6cefb772008-01-05 22:25:12 +0000386 for (unsigned i = 0, e = getNumChildren(); i != e; ++i)
387 if (getChild(i)->ContainsUnresolvedType()) return true;
388 return false;
389 }
390
391 /// canPatternMatch - If it is impossible for this pattern to match on this
392 /// target, fill in Reason and return false. Otherwise, return true.
Dan Gohmanee4fa192008-04-03 00:02:49 +0000393 bool canPatternMatch(std::string &Reason, const CodeGenDAGPatterns &CDP);
Chris Lattner6cefb772008-01-05 22:25:12 +0000394};
395
Chris Lattner383fed92010-02-14 21:10:33 +0000396inline raw_ostream &operator<<(raw_ostream &OS, const TreePatternNode &TPN) {
397 TPN.print(OS);
398 return OS;
399}
400
Chris Lattner6cefb772008-01-05 22:25:12 +0000401
402/// TreePattern - Represent a pattern, used for instructions, pattern
403/// fragments, etc.
404///
405class TreePattern {
406 /// Trees - The list of pattern trees which corresponds to this pattern.
407 /// Note that PatFrag's only have a single tree.
408 ///
409 std::vector<TreePatternNode*> Trees;
410
Chris Lattner2cacec52010-03-15 06:00:16 +0000411 /// NamedNodes - This is all of the nodes that have names in the trees in this
412 /// pattern.
413 StringMap<SmallVector<TreePatternNode*,1> > NamedNodes;
414
Chris Lattner6cefb772008-01-05 22:25:12 +0000415 /// TheRecord - The actual TableGen record corresponding to this pattern.
416 ///
417 Record *TheRecord;
418
419 /// Args - This is a list of all of the arguments to this pattern (for
420 /// PatFrag patterns), which are the 'node' markers in this pattern.
421 std::vector<std::string> Args;
422
423 /// CDP - the top-level object coordinating this madness.
424 ///
Chris Lattnerfe718932008-01-06 01:10:31 +0000425 CodeGenDAGPatterns &CDP;
Chris Lattner6cefb772008-01-05 22:25:12 +0000426
427 /// isInputPattern - True if this is an input pattern, something to match.
428 /// False if this is an output pattern, something to emit.
429 bool isInputPattern;
430public:
431
432 /// TreePattern constructor - Parse the specified DagInits into the
433 /// current record.
434 TreePattern(Record *TheRec, ListInit *RawPat, bool isInput,
Chris Lattnerfe718932008-01-06 01:10:31 +0000435 CodeGenDAGPatterns &ise);
Chris Lattner6cefb772008-01-05 22:25:12 +0000436 TreePattern(Record *TheRec, DagInit *Pat, bool isInput,
Chris Lattnerfe718932008-01-06 01:10:31 +0000437 CodeGenDAGPatterns &ise);
Chris Lattner6cefb772008-01-05 22:25:12 +0000438 TreePattern(Record *TheRec, TreePatternNode *Pat, bool isInput,
Chris Lattnerfe718932008-01-06 01:10:31 +0000439 CodeGenDAGPatterns &ise);
Chris Lattner6cefb772008-01-05 22:25:12 +0000440
441 /// getTrees - Return the tree patterns which corresponds to this pattern.
442 ///
443 const std::vector<TreePatternNode*> &getTrees() const { return Trees; }
444 unsigned getNumTrees() const { return Trees.size(); }
445 TreePatternNode *getTree(unsigned i) const { return Trees[i]; }
446 TreePatternNode *getOnlyTree() const {
447 assert(Trees.size() == 1 && "Doesn't have exactly one pattern!");
448 return Trees[0];
449 }
Chris Lattner2cacec52010-03-15 06:00:16 +0000450
451 const StringMap<SmallVector<TreePatternNode*,1> > &getNamedNodesMap() {
452 if (NamedNodes.empty())
453 ComputeNamedNodes();
454 return NamedNodes;
455 }
Chris Lattner6cefb772008-01-05 22:25:12 +0000456
457 /// getRecord - Return the actual TableGen record corresponding to this
458 /// pattern.
459 ///
460 Record *getRecord() const { return TheRecord; }
461
462 unsigned getNumArgs() const { return Args.size(); }
463 const std::string &getArgName(unsigned i) const {
464 assert(i < Args.size() && "Argument reference out of range!");
465 return Args[i];
466 }
467 std::vector<std::string> &getArgList() { return Args; }
468
Chris Lattnerfe718932008-01-06 01:10:31 +0000469 CodeGenDAGPatterns &getDAGPatterns() const { return CDP; }
Chris Lattner6cefb772008-01-05 22:25:12 +0000470
471 /// InlinePatternFragments - If this pattern refers to any pattern
472 /// fragments, inline them into place, giving us a pattern without any
473 /// PatFrag references.
474 void InlinePatternFragments() {
475 for (unsigned i = 0, e = Trees.size(); i != e; ++i)
476 Trees[i] = Trees[i]->InlinePatternFragments(*this);
477 }
478
479 /// InferAllTypes - Infer/propagate as many types throughout the expression
Jim Grosbachda4231f2009-03-26 16:17:51 +0000480 /// patterns as possible. Return true if all types are inferred, false
Chris Lattner6cefb772008-01-05 22:25:12 +0000481 /// otherwise. Throw an exception if a type contradiction is found.
Chris Lattner2cacec52010-03-15 06:00:16 +0000482 bool InferAllTypes(const StringMap<SmallVector<TreePatternNode*,1> >
483 *NamedTypes=0);
Chris Lattner6cefb772008-01-05 22:25:12 +0000484
485 /// error - Throw an exception, prefixing it with information about this
486 /// pattern.
487 void error(const std::string &Msg) const;
488
Daniel Dunbar1a551802009-07-03 00:10:29 +0000489 void print(raw_ostream &OS) const;
Chris Lattner6cefb772008-01-05 22:25:12 +0000490 void dump() const;
491
492private:
493 TreePatternNode *ParseTreePattern(DagInit *DI);
Chris Lattner2cacec52010-03-15 06:00:16 +0000494 void ComputeNamedNodes();
495 void ComputeNamedNodes(TreePatternNode *N);
Chris Lattner6cefb772008-01-05 22:25:12 +0000496};
497
498/// DAGDefaultOperand - One of these is created for each PredicateOperand
499/// or OptionalDefOperand that has a set ExecuteAlways / DefaultOps field.
500struct DAGDefaultOperand {
501 std::vector<TreePatternNode*> DefaultOps;
502};
503
504class DAGInstruction {
505 TreePattern *Pattern;
506 std::vector<Record*> Results;
507 std::vector<Record*> Operands;
508 std::vector<Record*> ImpResults;
509 std::vector<Record*> ImpOperands;
510 TreePatternNode *ResultPattern;
511public:
512 DAGInstruction(TreePattern *TP,
513 const std::vector<Record*> &results,
514 const std::vector<Record*> &operands,
515 const std::vector<Record*> &impresults,
516 const std::vector<Record*> &impoperands)
517 : Pattern(TP), Results(results), Operands(operands),
518 ImpResults(impresults), ImpOperands(impoperands),
519 ResultPattern(0) {}
520
Chris Lattnerf1ab4f12008-01-06 01:52:22 +0000521 const TreePattern *getPattern() const { return Pattern; }
Chris Lattner6cefb772008-01-05 22:25:12 +0000522 unsigned getNumResults() const { return Results.size(); }
523 unsigned getNumOperands() const { return Operands.size(); }
524 unsigned getNumImpResults() const { return ImpResults.size(); }
525 unsigned getNumImpOperands() const { return ImpOperands.size(); }
526 const std::vector<Record*>& getImpResults() const { return ImpResults; }
527
528 void setResultPattern(TreePatternNode *R) { ResultPattern = R; }
529
530 Record *getResult(unsigned RN) const {
531 assert(RN < Results.size());
532 return Results[RN];
533 }
534
535 Record *getOperand(unsigned ON) const {
536 assert(ON < Operands.size());
537 return Operands[ON];
538 }
539
540 Record *getImpResult(unsigned RN) const {
541 assert(RN < ImpResults.size());
542 return ImpResults[RN];
543 }
544
545 Record *getImpOperand(unsigned ON) const {
546 assert(ON < ImpOperands.size());
547 return ImpOperands[ON];
548 }
549
550 TreePatternNode *getResultPattern() const { return ResultPattern; }
551};
552
Chris Lattnerfe718932008-01-06 01:10:31 +0000553/// PatternToMatch - Used by CodeGenDAGPatterns to keep tab of patterns
Chris Lattner6cefb772008-01-05 22:25:12 +0000554/// processed to produce isel.
Chris Lattnerb49985a2010-02-18 06:47:49 +0000555class PatternToMatch {
556public:
Chris Lattner6cefb772008-01-05 22:25:12 +0000557 PatternToMatch(ListInit *preds,
558 TreePatternNode *src, TreePatternNode *dst,
559 const std::vector<Record*> &dstregs,
Chris Lattner117ccb72010-03-01 22:09:11 +0000560 unsigned complexity, unsigned uid)
561 : Predicates(preds), SrcPattern(src), DstPattern(dst),
562 Dstregs(dstregs), AddedComplexity(complexity), ID(uid) {}
Chris Lattner6cefb772008-01-05 22:25:12 +0000563
564 ListInit *Predicates; // Top level predicate conditions to match.
565 TreePatternNode *SrcPattern; // Source pattern to match.
566 TreePatternNode *DstPattern; // Resulting pattern.
567 std::vector<Record*> Dstregs; // Physical register defs being matched.
568 unsigned AddedComplexity; // Add to matching pattern complexity.
Chris Lattner117ccb72010-03-01 22:09:11 +0000569 unsigned ID; // Unique ID for the record.
Chris Lattner6cefb772008-01-05 22:25:12 +0000570
571 ListInit *getPredicates() const { return Predicates; }
572 TreePatternNode *getSrcPattern() const { return SrcPattern; }
573 TreePatternNode *getDstPattern() const { return DstPattern; }
574 const std::vector<Record*> &getDstRegs() const { return Dstregs; }
575 unsigned getAddedComplexity() const { return AddedComplexity; }
Dan Gohman22bb3112008-08-22 00:20:26 +0000576
577 std::string getPredicateCheck() const;
Chris Lattner6cefb772008-01-05 22:25:12 +0000578};
579
Daniel Dunbar6f5cc822009-08-23 09:47:37 +0000580// Deterministic comparison of Record*.
581struct RecordPtrCmp {
582 bool operator()(const Record *LHS, const Record *RHS) const;
583};
Chris Lattner6cefb772008-01-05 22:25:12 +0000584
Chris Lattnerfe718932008-01-06 01:10:31 +0000585class CodeGenDAGPatterns {
Chris Lattner6cefb772008-01-05 22:25:12 +0000586 RecordKeeper &Records;
587 CodeGenTarget Target;
588 std::vector<CodeGenIntrinsic> Intrinsics;
Dale Johannesen49de9822009-02-05 01:49:45 +0000589 std::vector<CodeGenIntrinsic> TgtIntrinsics;
Chris Lattner6cefb772008-01-05 22:25:12 +0000590
Daniel Dunbar6f5cc822009-08-23 09:47:37 +0000591 std::map<Record*, SDNodeInfo, RecordPtrCmp> SDNodes;
592 std::map<Record*, std::pair<Record*, std::string>, RecordPtrCmp> SDNodeXForms;
593 std::map<Record*, ComplexPattern, RecordPtrCmp> ComplexPatterns;
594 std::map<Record*, TreePattern*, RecordPtrCmp> PatternFragments;
595 std::map<Record*, DAGDefaultOperand, RecordPtrCmp> DefaultOperands;
596 std::map<Record*, DAGInstruction, RecordPtrCmp> Instructions;
Chris Lattner6cefb772008-01-05 22:25:12 +0000597
598 // Specific SDNode definitions:
599 Record *intrinsic_void_sdnode;
600 Record *intrinsic_w_chain_sdnode, *intrinsic_wo_chain_sdnode;
601
602 /// PatternsToMatch - All of the things we are matching on the DAG. The first
603 /// value is the pattern to match, the second pattern is the result to
604 /// emit.
605 std::vector<PatternToMatch> PatternsToMatch;
606public:
Chris Lattnerfe718932008-01-06 01:10:31 +0000607 CodeGenDAGPatterns(RecordKeeper &R);
608 ~CodeGenDAGPatterns();
Chris Lattner6cefb772008-01-05 22:25:12 +0000609
Dan Gohmanee4fa192008-04-03 00:02:49 +0000610 CodeGenTarget &getTargetInfo() { return Target; }
Chris Lattner6cefb772008-01-05 22:25:12 +0000611 const CodeGenTarget &getTargetInfo() const { return Target; }
612
613 Record *getSDNodeNamed(const std::string &Name) const;
614
615 const SDNodeInfo &getSDNodeInfo(Record *R) const {
616 assert(SDNodes.count(R) && "Unknown node!");
617 return SDNodes.find(R)->second;
618 }
619
Chris Lattner443e3f92008-01-05 22:54:53 +0000620 // Node transformation lookups.
621 typedef std::pair<Record*, std::string> NodeXForm;
622 const NodeXForm &getSDNodeTransform(Record *R) const {
Chris Lattner6cefb772008-01-05 22:25:12 +0000623 assert(SDNodeXForms.count(R) && "Invalid transform!");
624 return SDNodeXForms.find(R)->second;
625 }
626
Benjamin Kramer5b9e7ef2009-08-23 10:39:21 +0000627 typedef std::map<Record*, NodeXForm, RecordPtrCmp>::const_iterator
628 nx_iterator;
Chris Lattner443e3f92008-01-05 22:54:53 +0000629 nx_iterator nx_begin() const { return SDNodeXForms.begin(); }
630 nx_iterator nx_end() const { return SDNodeXForms.end(); }
631
632
Chris Lattner6cefb772008-01-05 22:25:12 +0000633 const ComplexPattern &getComplexPattern(Record *R) const {
634 assert(ComplexPatterns.count(R) && "Unknown addressing mode!");
635 return ComplexPatterns.find(R)->second;
636 }
637
638 const CodeGenIntrinsic &getIntrinsic(Record *R) const {
639 for (unsigned i = 0, e = Intrinsics.size(); i != e; ++i)
640 if (Intrinsics[i].TheDef == R) return Intrinsics[i];
Dale Johannesen49de9822009-02-05 01:49:45 +0000641 for (unsigned i = 0, e = TgtIntrinsics.size(); i != e; ++i)
642 if (TgtIntrinsics[i].TheDef == R) return TgtIntrinsics[i];
Chris Lattner6cefb772008-01-05 22:25:12 +0000643 assert(0 && "Unknown intrinsic!");
644 abort();
645 }
646
647 const CodeGenIntrinsic &getIntrinsicInfo(unsigned IID) const {
Dale Johannesen49de9822009-02-05 01:49:45 +0000648 if (IID-1 < Intrinsics.size())
649 return Intrinsics[IID-1];
650 if (IID-Intrinsics.size()-1 < TgtIntrinsics.size())
651 return TgtIntrinsics[IID-Intrinsics.size()-1];
652 assert(0 && "Bad intrinsic ID!");
Evan Cheng32c1a4c2009-02-09 03:07:24 +0000653 abort();
Chris Lattner6cefb772008-01-05 22:25:12 +0000654 }
655
656 unsigned getIntrinsicID(Record *R) const {
657 for (unsigned i = 0, e = Intrinsics.size(); i != e; ++i)
658 if (Intrinsics[i].TheDef == R) return i;
Dale Johannesen49de9822009-02-05 01:49:45 +0000659 for (unsigned i = 0, e = TgtIntrinsics.size(); i != e; ++i)
660 if (TgtIntrinsics[i].TheDef == R) return i + Intrinsics.size();
Chris Lattner6cefb772008-01-05 22:25:12 +0000661 assert(0 && "Unknown intrinsic!");
662 abort();
663 }
664
Chris Lattnerb49985a2010-02-18 06:47:49 +0000665 const DAGDefaultOperand &getDefaultOperand(Record *R) const {
Chris Lattner6cefb772008-01-05 22:25:12 +0000666 assert(DefaultOperands.count(R) &&"Isn't an analyzed default operand!");
667 return DefaultOperands.find(R)->second;
668 }
669
670 // Pattern Fragment information.
671 TreePattern *getPatternFragment(Record *R) const {
672 assert(PatternFragments.count(R) && "Invalid pattern fragment request!");
673 return PatternFragments.find(R)->second;
674 }
Benjamin Kramer5b9e7ef2009-08-23 10:39:21 +0000675 typedef std::map<Record*, TreePattern*, RecordPtrCmp>::const_iterator
676 pf_iterator;
Chris Lattner6cefb772008-01-05 22:25:12 +0000677 pf_iterator pf_begin() const { return PatternFragments.begin(); }
678 pf_iterator pf_end() const { return PatternFragments.end(); }
679
680 // Patterns to match information.
Chris Lattner60d81392008-01-05 22:30:17 +0000681 typedef std::vector<PatternToMatch>::const_iterator ptm_iterator;
682 ptm_iterator ptm_begin() const { return PatternsToMatch.begin(); }
683 ptm_iterator ptm_end() const { return PatternsToMatch.end(); }
Chris Lattner6cefb772008-01-05 22:25:12 +0000684
685
686
687 const DAGInstruction &getInstruction(Record *R) const {
688 assert(Instructions.count(R) && "Unknown instruction!");
689 return Instructions.find(R)->second;
690 }
691
692 Record *get_intrinsic_void_sdnode() const {
693 return intrinsic_void_sdnode;
694 }
695 Record *get_intrinsic_w_chain_sdnode() const {
696 return intrinsic_w_chain_sdnode;
697 }
698 Record *get_intrinsic_wo_chain_sdnode() const {
699 return intrinsic_wo_chain_sdnode;
700 }
701
Jakob Stoklund Olesen11ee5082009-10-15 18:50:03 +0000702 bool hasTargetIntrinsics() { return !TgtIntrinsics.empty(); }
703
Chris Lattner6cefb772008-01-05 22:25:12 +0000704private:
705 void ParseNodeInfo();
Chris Lattner443e3f92008-01-05 22:54:53 +0000706 void ParseNodeTransforms();
Chris Lattner6cefb772008-01-05 22:25:12 +0000707 void ParseComplexPatterns();
Chris Lattnerdc32f982008-01-05 22:43:57 +0000708 void ParsePatternFragments();
Chris Lattner6cefb772008-01-05 22:25:12 +0000709 void ParseDefaultOperands();
710 void ParseInstructions();
711 void ParsePatterns();
Dan Gohmanee4fa192008-04-03 00:02:49 +0000712 void InferInstructionFlags();
Chris Lattner6cefb772008-01-05 22:25:12 +0000713 void GenerateVariants();
714
Chris Lattner25b6f912010-02-23 06:16:51 +0000715 void AddPatternToMatch(const TreePattern *Pattern, const PatternToMatch &PTM);
Chris Lattner6cefb772008-01-05 22:25:12 +0000716 void FindPatternInputsAndOutputs(TreePattern *I, TreePatternNode *Pat,
717 std::map<std::string,
718 TreePatternNode*> &InstInputs,
719 std::map<std::string,
720 TreePatternNode*> &InstResults,
721 std::vector<Record*> &InstImpInputs,
722 std::vector<Record*> &InstImpResults);
723};
724} // end namespace llvm
725
726#endif