blob: caf053285d91a4fb5916a26f0d48f19cbb6abf47 [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 {
57 SmallVector<MVT::SimpleValueType, 2> TypeVec;
58 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
139 /// valid on completely unknown type sets
140 bool FillWithPossibleTypes(TreePattern &TP);
Chris Lattner2cacec52010-03-15 06:00:16 +0000141 };
Chris Lattner6cefb772008-01-05 22:25:12 +0000142}
143
Scott Michel327d0652008-03-05 17:49:05 +0000144/// Set type used to track multiply used variables in patterns
145typedef std::set<std::string> MultipleUseVarSet;
146
Chris Lattner6cefb772008-01-05 22:25:12 +0000147/// SDTypeConstraint - This is a discriminated union of constraints,
148/// corresponding to the SDTypeConstraint tablegen class in Target.td.
149struct SDTypeConstraint {
150 SDTypeConstraint(Record *R);
151
152 unsigned OperandNo; // The operand # this constraint applies to.
153 enum {
Bob Wilson36e3e662009-08-12 22:30:59 +0000154 SDTCisVT, SDTCisPtrTy, SDTCisInt, SDTCisFP, SDTCisVec, SDTCisSameAs,
Nate Begeman9008ca62009-04-27 18:41:29 +0000155 SDTCisVTSmallerThanOp, SDTCisOpSmallerThanOp, SDTCisEltOfVec
Chris Lattner6cefb772008-01-05 22:25:12 +0000156 } ConstraintType;
157
158 union { // The discriminated union.
159 struct {
Chris Lattner2cacec52010-03-15 06:00:16 +0000160 MVT::SimpleValueType VT;
Chris Lattner6cefb772008-01-05 22:25:12 +0000161 } SDTCisVT_Info;
162 struct {
163 unsigned OtherOperandNum;
164 } SDTCisSameAs_Info;
165 struct {
166 unsigned OtherOperandNum;
167 } SDTCisVTSmallerThanOp_Info;
168 struct {
169 unsigned BigOperandNum;
170 } SDTCisOpSmallerThanOp_Info;
171 struct {
172 unsigned OtherOperandNum;
Nate Begemanb5af3342008-02-09 01:37:05 +0000173 } SDTCisEltOfVec_Info;
Chris Lattner6cefb772008-01-05 22:25:12 +0000174 } x;
175
176 /// ApplyTypeConstraint - Given a node in a pattern, apply this type
177 /// constraint to the nodes operands. This returns true if it makes a
178 /// change, false otherwise. If a type contradiction is found, throw an
179 /// exception.
180 bool ApplyTypeConstraint(TreePatternNode *N, const SDNodeInfo &NodeInfo,
181 TreePattern &TP) const;
182
183 /// getOperandNum - Return the node corresponding to operand #OpNo in tree
184 /// N, which has NumResults results.
185 TreePatternNode *getOperandNum(unsigned OpNo, TreePatternNode *N,
186 unsigned NumResults) const;
187};
188
189/// SDNodeInfo - One of these records is created for each SDNode instance in
190/// the target .td file. This represents the various dag nodes we will be
191/// processing.
192class SDNodeInfo {
193 Record *Def;
194 std::string EnumName;
195 std::string SDClassName;
196 unsigned Properties;
197 unsigned NumResults;
198 int NumOperands;
199 std::vector<SDTypeConstraint> TypeConstraints;
200public:
201 SDNodeInfo(Record *R); // Parse the specified record.
202
203 unsigned getNumResults() const { return NumResults; }
204 int getNumOperands() const { return NumOperands; }
205 Record *getRecord() const { return Def; }
206 const std::string &getEnumName() const { return EnumName; }
207 const std::string &getSDClassName() const { return SDClassName; }
208
209 const std::vector<SDTypeConstraint> &getTypeConstraints() const {
210 return TypeConstraints;
211 }
212
Chris Lattner22579812010-02-28 00:22:30 +0000213 /// getKnownType - If the type constraints on this node imply a fixed type
214 /// (e.g. all stores return void, etc), then return it as an
Chris Lattneraac5b5b2010-03-19 01:14:27 +0000215 /// MVT::SimpleValueType. Otherwise, return MVT::Other.
216 MVT::SimpleValueType getKnownType() const;
Chris Lattner22579812010-02-28 00:22:30 +0000217
Chris Lattner6cefb772008-01-05 22:25:12 +0000218 /// hasProperty - Return true if this node has the specified property.
219 ///
220 bool hasProperty(enum SDNP Prop) const { return Properties & (1 << Prop); }
221
222 /// ApplyTypeConstraints - Given a node in a pattern, apply the type
223 /// constraints for this node to the operands of the node. This returns
224 /// true if it makes a change, false otherwise. If a type contradiction is
225 /// found, throw an exception.
226 bool ApplyTypeConstraints(TreePatternNode *N, TreePattern &TP) const {
227 bool MadeChange = false;
228 for (unsigned i = 0, e = TypeConstraints.size(); i != e; ++i)
229 MadeChange |= TypeConstraints[i].ApplyTypeConstraint(N, *this, TP);
230 return MadeChange;
231 }
232};
233
234/// FIXME: TreePatternNode's can be shared in some cases (due to dag-shaped
235/// patterns), and as such should be ref counted. We currently just leak all
236/// TreePatternNode objects!
237class TreePatternNode {
Chris Lattner2cacec52010-03-15 06:00:16 +0000238 /// The type of this node. Before and during type inference, this may be a
239 /// set of possible types. After (successful) type inference, this is a
240 /// single type.
241 EEVT::TypeSet Type;
Chris Lattner6cefb772008-01-05 22:25:12 +0000242
243 /// Operator - The Record for the operator if this is an interior node (not
244 /// a leaf).
245 Record *Operator;
246
247 /// Val - The init value (e.g. the "GPRC" record, or "7") for a leaf.
248 ///
249 Init *Val;
250
251 /// Name - The name given to this node with the :$foo notation.
252 ///
253 std::string Name;
254
Dan Gohman0540e172008-10-15 06:17:21 +0000255 /// PredicateFns - The predicate functions to execute on this node to check
256 /// for a match. If this list is empty, no predicate is involved.
257 std::vector<std::string> PredicateFns;
Chris Lattner6cefb772008-01-05 22:25:12 +0000258
259 /// TransformFn - The transformation function to execute on this node before
260 /// it can be substituted into the resulting instruction on a pattern match.
261 Record *TransformFn;
262
263 std::vector<TreePatternNode*> Children;
264public:
265 TreePatternNode(Record *Op, const std::vector<TreePatternNode*> &Ch)
Chris Lattner2cacec52010-03-15 06:00:16 +0000266 : Operator(Op), Val(0), TransformFn(0), Children(Ch) { }
Chris Lattner6cefb772008-01-05 22:25:12 +0000267 TreePatternNode(Init *val) // leaf ctor
Chris Lattner2cacec52010-03-15 06:00:16 +0000268 : Operator(0), Val(val), TransformFn(0) {
Chris Lattner6cefb772008-01-05 22:25:12 +0000269 }
270 ~TreePatternNode();
271
272 const std::string &getName() const { return Name; }
273 void setName(const std::string &N) { Name = N; }
274
275 bool isLeaf() const { return Val != 0; }
Chris Lattner2cacec52010-03-15 06:00:16 +0000276
277 // Type accessors.
278 MVT::SimpleValueType getType() const { return Type.getConcrete(); }
279 const EEVT::TypeSet &getExtType() const { return Type; }
280 EEVT::TypeSet &getExtType() { return Type; }
281 void setType(const EEVT::TypeSet &T) { Type = T; }
282
283 bool hasTypeSet() const { return Type.isConcrete(); }
284 bool isTypeCompletelyUnknown() const { return Type.isCompletelyUnknown(); }
285 bool isTypeDynamicallyResolved() const { return Type.isDynamicallyResolved();}
Chris Lattner6cefb772008-01-05 22:25:12 +0000286
287 Init *getLeafValue() const { assert(isLeaf()); return Val; }
288 Record *getOperator() const { assert(!isLeaf()); return Operator; }
289
290 unsigned getNumChildren() const { return Children.size(); }
291 TreePatternNode *getChild(unsigned N) const { return Children[N]; }
292 void setChild(unsigned i, TreePatternNode *N) {
293 Children[i] = N;
294 }
Chris Lattnere39650a2010-02-16 06:10:58 +0000295
296 /// hasChild - Return true if N is any of our children.
297 bool hasChild(const TreePatternNode *N) const {
298 for (unsigned i = 0, e = Children.size(); i != e; ++i)
299 if (Children[i] == N) return true;
300 return false;
301 }
Chris Lattnere67bde52008-01-06 05:36:50 +0000302
Chris Lattner47661322010-02-14 22:22:58 +0000303 const std::vector<std::string> &getPredicateFns() const {return PredicateFns;}
Dan Gohman0540e172008-10-15 06:17:21 +0000304 void clearPredicateFns() { PredicateFns.clear(); }
305 void setPredicateFns(const std::vector<std::string> &Fns) {
306 assert(PredicateFns.empty() && "Overwriting non-empty predicate list!");
307 PredicateFns = Fns;
308 }
309 void addPredicateFn(const std::string &Fn) {
310 assert(!Fn.empty() && "Empty predicate string!");
311 if (std::find(PredicateFns.begin(), PredicateFns.end(), Fn) ==
312 PredicateFns.end())
313 PredicateFns.push_back(Fn);
314 }
Chris Lattner6cefb772008-01-05 22:25:12 +0000315
316 Record *getTransformFn() const { return TransformFn; }
317 void setTransformFn(Record *Fn) { TransformFn = Fn; }
318
Chris Lattnere67bde52008-01-06 05:36:50 +0000319 /// getIntrinsicInfo - If this node corresponds to an intrinsic, return the
320 /// CodeGenIntrinsic information for it, otherwise return a null pointer.
321 const CodeGenIntrinsic *getIntrinsicInfo(const CodeGenDAGPatterns &CDP) const;
Evan Cheng6bd95672008-06-16 20:29:38 +0000322
Chris Lattner47661322010-02-14 22:22:58 +0000323 /// getComplexPatternInfo - If this node corresponds to a ComplexPattern,
324 /// return the ComplexPattern information, otherwise return null.
325 const ComplexPattern *
326 getComplexPatternInfo(const CodeGenDAGPatterns &CGP) const;
327
328 /// NodeHasProperty - Return true if this node has the specified property.
Chris Lattner751d5aa2010-02-14 22:33:49 +0000329 bool NodeHasProperty(SDNP Property, const CodeGenDAGPatterns &CGP) const;
Chris Lattner47661322010-02-14 22:22:58 +0000330
331 /// TreeHasProperty - Return true if any node in this tree has the specified
332 /// property.
Chris Lattner751d5aa2010-02-14 22:33:49 +0000333 bool TreeHasProperty(SDNP Property, const CodeGenDAGPatterns &CGP) const;
Chris Lattner47661322010-02-14 22:22:58 +0000334
Evan Cheng6bd95672008-06-16 20:29:38 +0000335 /// isCommutativeIntrinsic - Return true if the node is an intrinsic which is
336 /// marked isCommutative.
337 bool isCommutativeIntrinsic(const CodeGenDAGPatterns &CDP) const;
Chris Lattnere67bde52008-01-06 05:36:50 +0000338
Daniel Dunbar1a551802009-07-03 00:10:29 +0000339 void print(raw_ostream &OS) const;
Chris Lattner6cefb772008-01-05 22:25:12 +0000340 void dump() const;
341
342public: // Higher level manipulation routines.
343
344 /// clone - Return a new copy of this tree.
345 ///
346 TreePatternNode *clone() const;
Chris Lattner47661322010-02-14 22:22:58 +0000347
348 /// RemoveAllTypes - Recursively strip all the types of this tree.
349 void RemoveAllTypes();
Chris Lattner6cefb772008-01-05 22:25:12 +0000350
351 /// isIsomorphicTo - Return true if this node is recursively isomorphic to
352 /// the specified node. For this comparison, all of the state of the node
353 /// is considered, except for the assigned name. Nodes with differing names
354 /// that are otherwise identical are considered isomorphic.
Scott Michel327d0652008-03-05 17:49:05 +0000355 bool isIsomorphicTo(const TreePatternNode *N,
356 const MultipleUseVarSet &DepVars) const;
Chris Lattner6cefb772008-01-05 22:25:12 +0000357
358 /// SubstituteFormalArguments - Replace the formal arguments in this tree
359 /// with actual values specified by ArgMap.
360 void SubstituteFormalArguments(std::map<std::string,
361 TreePatternNode*> &ArgMap);
362
363 /// InlinePatternFragments - If this pattern refers to any pattern
364 /// fragments, inline them into place, giving us a pattern without any
365 /// PatFrag references.
366 TreePatternNode *InlinePatternFragments(TreePattern &TP);
367
Bob Wilson6c01ca92009-01-05 17:23:09 +0000368 /// ApplyTypeConstraints - Apply all of the type constraints relevant to
Chris Lattner6cefb772008-01-05 22:25:12 +0000369 /// this node and its children in the tree. This returns true if it makes a
370 /// change, false otherwise. If a type contradiction is found, throw an
371 /// exception.
372 bool ApplyTypeConstraints(TreePattern &TP, bool NotRegisters);
373
374 /// UpdateNodeType - Set the node type of N to VT if VT contains
375 /// information. If N already contains a conflicting type, then throw an
376 /// exception. This returns true if any information was updated.
377 ///
Chris Lattner2cacec52010-03-15 06:00:16 +0000378 bool UpdateNodeType(const EEVT::TypeSet &InTy, TreePattern &TP) {
379 return Type.MergeInTypeInfo(InTy, TP);
380 }
381
382 bool UpdateNodeType(MVT::SimpleValueType InTy, TreePattern &TP) {
383 return Type.MergeInTypeInfo(EEVT::TypeSet(InTy, TP), TP);
Chris Lattner6cefb772008-01-05 22:25:12 +0000384 }
385
386 /// ContainsUnresolvedType - Return true if this tree contains any
387 /// unresolved types.
388 bool ContainsUnresolvedType() const {
Chris Lattner2cacec52010-03-15 06:00:16 +0000389 if (!hasTypeSet()) return true;
Chris Lattner6cefb772008-01-05 22:25:12 +0000390 for (unsigned i = 0, e = getNumChildren(); i != e; ++i)
391 if (getChild(i)->ContainsUnresolvedType()) return true;
392 return false;
393 }
394
395 /// canPatternMatch - If it is impossible for this pattern to match on this
396 /// target, fill in Reason and return false. Otherwise, return true.
Dan Gohmanee4fa192008-04-03 00:02:49 +0000397 bool canPatternMatch(std::string &Reason, const CodeGenDAGPatterns &CDP);
Chris Lattner6cefb772008-01-05 22:25:12 +0000398};
399
Chris Lattner383fed92010-02-14 21:10:33 +0000400inline raw_ostream &operator<<(raw_ostream &OS, const TreePatternNode &TPN) {
401 TPN.print(OS);
402 return OS;
403}
404
Chris Lattner6cefb772008-01-05 22:25:12 +0000405
406/// TreePattern - Represent a pattern, used for instructions, pattern
407/// fragments, etc.
408///
409class TreePattern {
410 /// Trees - The list of pattern trees which corresponds to this pattern.
411 /// Note that PatFrag's only have a single tree.
412 ///
413 std::vector<TreePatternNode*> Trees;
414
Chris Lattner2cacec52010-03-15 06:00:16 +0000415 /// NamedNodes - This is all of the nodes that have names in the trees in this
416 /// pattern.
417 StringMap<SmallVector<TreePatternNode*,1> > NamedNodes;
418
Chris Lattner6cefb772008-01-05 22:25:12 +0000419 /// TheRecord - The actual TableGen record corresponding to this pattern.
420 ///
421 Record *TheRecord;
422
423 /// Args - This is a list of all of the arguments to this pattern (for
424 /// PatFrag patterns), which are the 'node' markers in this pattern.
425 std::vector<std::string> Args;
426
427 /// CDP - the top-level object coordinating this madness.
428 ///
Chris Lattnerfe718932008-01-06 01:10:31 +0000429 CodeGenDAGPatterns &CDP;
Chris Lattner6cefb772008-01-05 22:25:12 +0000430
431 /// isInputPattern - True if this is an input pattern, something to match.
432 /// False if this is an output pattern, something to emit.
433 bool isInputPattern;
434public:
435
436 /// TreePattern constructor - Parse the specified DagInits into the
437 /// current record.
438 TreePattern(Record *TheRec, ListInit *RawPat, bool isInput,
Chris Lattnerfe718932008-01-06 01:10:31 +0000439 CodeGenDAGPatterns &ise);
Chris Lattner6cefb772008-01-05 22:25:12 +0000440 TreePattern(Record *TheRec, DagInit *Pat, bool isInput,
Chris Lattnerfe718932008-01-06 01:10:31 +0000441 CodeGenDAGPatterns &ise);
Chris Lattner6cefb772008-01-05 22:25:12 +0000442 TreePattern(Record *TheRec, TreePatternNode *Pat, bool isInput,
Chris Lattnerfe718932008-01-06 01:10:31 +0000443 CodeGenDAGPatterns &ise);
Chris Lattner6cefb772008-01-05 22:25:12 +0000444
445 /// getTrees - Return the tree patterns which corresponds to this pattern.
446 ///
447 const std::vector<TreePatternNode*> &getTrees() const { return Trees; }
448 unsigned getNumTrees() const { return Trees.size(); }
449 TreePatternNode *getTree(unsigned i) const { return Trees[i]; }
450 TreePatternNode *getOnlyTree() const {
451 assert(Trees.size() == 1 && "Doesn't have exactly one pattern!");
452 return Trees[0];
453 }
Chris Lattner2cacec52010-03-15 06:00:16 +0000454
455 const StringMap<SmallVector<TreePatternNode*,1> > &getNamedNodesMap() {
456 if (NamedNodes.empty())
457 ComputeNamedNodes();
458 return NamedNodes;
459 }
Chris Lattner6cefb772008-01-05 22:25:12 +0000460
461 /// getRecord - Return the actual TableGen record corresponding to this
462 /// pattern.
463 ///
464 Record *getRecord() const { return TheRecord; }
465
466 unsigned getNumArgs() const { return Args.size(); }
467 const std::string &getArgName(unsigned i) const {
468 assert(i < Args.size() && "Argument reference out of range!");
469 return Args[i];
470 }
471 std::vector<std::string> &getArgList() { return Args; }
472
Chris Lattnerfe718932008-01-06 01:10:31 +0000473 CodeGenDAGPatterns &getDAGPatterns() const { return CDP; }
Chris Lattner6cefb772008-01-05 22:25:12 +0000474
475 /// InlinePatternFragments - If this pattern refers to any pattern
476 /// fragments, inline them into place, giving us a pattern without any
477 /// PatFrag references.
478 void InlinePatternFragments() {
479 for (unsigned i = 0, e = Trees.size(); i != e; ++i)
480 Trees[i] = Trees[i]->InlinePatternFragments(*this);
481 }
482
483 /// InferAllTypes - Infer/propagate as many types throughout the expression
Jim Grosbachda4231f2009-03-26 16:17:51 +0000484 /// patterns as possible. Return true if all types are inferred, false
Chris Lattner6cefb772008-01-05 22:25:12 +0000485 /// otherwise. Throw an exception if a type contradiction is found.
Chris Lattner2cacec52010-03-15 06:00:16 +0000486 bool InferAllTypes(const StringMap<SmallVector<TreePatternNode*,1> >
487 *NamedTypes=0);
Chris Lattner6cefb772008-01-05 22:25:12 +0000488
489 /// error - Throw an exception, prefixing it with information about this
490 /// pattern.
491 void error(const std::string &Msg) const;
492
Daniel Dunbar1a551802009-07-03 00:10:29 +0000493 void print(raw_ostream &OS) const;
Chris Lattner6cefb772008-01-05 22:25:12 +0000494 void dump() const;
495
496private:
497 TreePatternNode *ParseTreePattern(DagInit *DI);
Chris Lattner2cacec52010-03-15 06:00:16 +0000498 void ComputeNamedNodes();
499 void ComputeNamedNodes(TreePatternNode *N);
Chris Lattner6cefb772008-01-05 22:25:12 +0000500};
501
502/// DAGDefaultOperand - One of these is created for each PredicateOperand
503/// or OptionalDefOperand that has a set ExecuteAlways / DefaultOps field.
504struct DAGDefaultOperand {
505 std::vector<TreePatternNode*> DefaultOps;
506};
507
508class DAGInstruction {
509 TreePattern *Pattern;
510 std::vector<Record*> Results;
511 std::vector<Record*> Operands;
512 std::vector<Record*> ImpResults;
513 std::vector<Record*> ImpOperands;
514 TreePatternNode *ResultPattern;
515public:
516 DAGInstruction(TreePattern *TP,
517 const std::vector<Record*> &results,
518 const std::vector<Record*> &operands,
519 const std::vector<Record*> &impresults,
520 const std::vector<Record*> &impoperands)
521 : Pattern(TP), Results(results), Operands(operands),
522 ImpResults(impresults), ImpOperands(impoperands),
523 ResultPattern(0) {}
524
Chris Lattnerf1ab4f12008-01-06 01:52:22 +0000525 const TreePattern *getPattern() const { return Pattern; }
Chris Lattner6cefb772008-01-05 22:25:12 +0000526 unsigned getNumResults() const { return Results.size(); }
527 unsigned getNumOperands() const { return Operands.size(); }
528 unsigned getNumImpResults() const { return ImpResults.size(); }
529 unsigned getNumImpOperands() const { return ImpOperands.size(); }
530 const std::vector<Record*>& getImpResults() const { return ImpResults; }
531
532 void setResultPattern(TreePatternNode *R) { ResultPattern = R; }
533
534 Record *getResult(unsigned RN) const {
535 assert(RN < Results.size());
536 return Results[RN];
537 }
538
539 Record *getOperand(unsigned ON) const {
540 assert(ON < Operands.size());
541 return Operands[ON];
542 }
543
544 Record *getImpResult(unsigned RN) const {
545 assert(RN < ImpResults.size());
546 return ImpResults[RN];
547 }
548
549 Record *getImpOperand(unsigned ON) const {
550 assert(ON < ImpOperands.size());
551 return ImpOperands[ON];
552 }
553
554 TreePatternNode *getResultPattern() const { return ResultPattern; }
555};
556
Chris Lattnerfe718932008-01-06 01:10:31 +0000557/// PatternToMatch - Used by CodeGenDAGPatterns to keep tab of patterns
Chris Lattner6cefb772008-01-05 22:25:12 +0000558/// processed to produce isel.
Chris Lattnerb49985a2010-02-18 06:47:49 +0000559class PatternToMatch {
560public:
Chris Lattner6cefb772008-01-05 22:25:12 +0000561 PatternToMatch(ListInit *preds,
562 TreePatternNode *src, TreePatternNode *dst,
563 const std::vector<Record*> &dstregs,
Chris Lattner117ccb72010-03-01 22:09:11 +0000564 unsigned complexity, unsigned uid)
565 : Predicates(preds), SrcPattern(src), DstPattern(dst),
566 Dstregs(dstregs), AddedComplexity(complexity), ID(uid) {}
Chris Lattner6cefb772008-01-05 22:25:12 +0000567
568 ListInit *Predicates; // Top level predicate conditions to match.
569 TreePatternNode *SrcPattern; // Source pattern to match.
570 TreePatternNode *DstPattern; // Resulting pattern.
571 std::vector<Record*> Dstregs; // Physical register defs being matched.
572 unsigned AddedComplexity; // Add to matching pattern complexity.
Chris Lattner117ccb72010-03-01 22:09:11 +0000573 unsigned ID; // Unique ID for the record.
Chris Lattner6cefb772008-01-05 22:25:12 +0000574
575 ListInit *getPredicates() const { return Predicates; }
576 TreePatternNode *getSrcPattern() const { return SrcPattern; }
577 TreePatternNode *getDstPattern() const { return DstPattern; }
578 const std::vector<Record*> &getDstRegs() const { return Dstregs; }
579 unsigned getAddedComplexity() const { return AddedComplexity; }
Dan Gohman22bb3112008-08-22 00:20:26 +0000580
581 std::string getPredicateCheck() const;
Chris Lattner6cefb772008-01-05 22:25:12 +0000582};
583
Daniel Dunbar6f5cc822009-08-23 09:47:37 +0000584// Deterministic comparison of Record*.
585struct RecordPtrCmp {
586 bool operator()(const Record *LHS, const Record *RHS) const;
587};
Chris Lattner6cefb772008-01-05 22:25:12 +0000588
Chris Lattnerfe718932008-01-06 01:10:31 +0000589class CodeGenDAGPatterns {
Chris Lattner6cefb772008-01-05 22:25:12 +0000590 RecordKeeper &Records;
591 CodeGenTarget Target;
592 std::vector<CodeGenIntrinsic> Intrinsics;
Dale Johannesen49de9822009-02-05 01:49:45 +0000593 std::vector<CodeGenIntrinsic> TgtIntrinsics;
Chris Lattner6cefb772008-01-05 22:25:12 +0000594
Daniel Dunbar6f5cc822009-08-23 09:47:37 +0000595 std::map<Record*, SDNodeInfo, RecordPtrCmp> SDNodes;
596 std::map<Record*, std::pair<Record*, std::string>, RecordPtrCmp> SDNodeXForms;
597 std::map<Record*, ComplexPattern, RecordPtrCmp> ComplexPatterns;
598 std::map<Record*, TreePattern*, RecordPtrCmp> PatternFragments;
599 std::map<Record*, DAGDefaultOperand, RecordPtrCmp> DefaultOperands;
600 std::map<Record*, DAGInstruction, RecordPtrCmp> Instructions;
Chris Lattner6cefb772008-01-05 22:25:12 +0000601
602 // Specific SDNode definitions:
603 Record *intrinsic_void_sdnode;
604 Record *intrinsic_w_chain_sdnode, *intrinsic_wo_chain_sdnode;
605
606 /// PatternsToMatch - All of the things we are matching on the DAG. The first
607 /// value is the pattern to match, the second pattern is the result to
608 /// emit.
609 std::vector<PatternToMatch> PatternsToMatch;
610public:
Chris Lattnerfe718932008-01-06 01:10:31 +0000611 CodeGenDAGPatterns(RecordKeeper &R);
612 ~CodeGenDAGPatterns();
Chris Lattner6cefb772008-01-05 22:25:12 +0000613
Dan Gohmanee4fa192008-04-03 00:02:49 +0000614 CodeGenTarget &getTargetInfo() { return Target; }
Chris Lattner6cefb772008-01-05 22:25:12 +0000615 const CodeGenTarget &getTargetInfo() const { return Target; }
616
617 Record *getSDNodeNamed(const std::string &Name) const;
618
619 const SDNodeInfo &getSDNodeInfo(Record *R) const {
620 assert(SDNodes.count(R) && "Unknown node!");
621 return SDNodes.find(R)->second;
622 }
623
Chris Lattner443e3f92008-01-05 22:54:53 +0000624 // Node transformation lookups.
625 typedef std::pair<Record*, std::string> NodeXForm;
626 const NodeXForm &getSDNodeTransform(Record *R) const {
Chris Lattner6cefb772008-01-05 22:25:12 +0000627 assert(SDNodeXForms.count(R) && "Invalid transform!");
628 return SDNodeXForms.find(R)->second;
629 }
630
Benjamin Kramer5b9e7ef2009-08-23 10:39:21 +0000631 typedef std::map<Record*, NodeXForm, RecordPtrCmp>::const_iterator
632 nx_iterator;
Chris Lattner443e3f92008-01-05 22:54:53 +0000633 nx_iterator nx_begin() const { return SDNodeXForms.begin(); }
634 nx_iterator nx_end() const { return SDNodeXForms.end(); }
635
636
Chris Lattner6cefb772008-01-05 22:25:12 +0000637 const ComplexPattern &getComplexPattern(Record *R) const {
638 assert(ComplexPatterns.count(R) && "Unknown addressing mode!");
639 return ComplexPatterns.find(R)->second;
640 }
641
642 const CodeGenIntrinsic &getIntrinsic(Record *R) const {
643 for (unsigned i = 0, e = Intrinsics.size(); i != e; ++i)
644 if (Intrinsics[i].TheDef == R) return Intrinsics[i];
Dale Johannesen49de9822009-02-05 01:49:45 +0000645 for (unsigned i = 0, e = TgtIntrinsics.size(); i != e; ++i)
646 if (TgtIntrinsics[i].TheDef == R) return TgtIntrinsics[i];
Chris Lattner6cefb772008-01-05 22:25:12 +0000647 assert(0 && "Unknown intrinsic!");
648 abort();
649 }
650
651 const CodeGenIntrinsic &getIntrinsicInfo(unsigned IID) const {
Dale Johannesen49de9822009-02-05 01:49:45 +0000652 if (IID-1 < Intrinsics.size())
653 return Intrinsics[IID-1];
654 if (IID-Intrinsics.size()-1 < TgtIntrinsics.size())
655 return TgtIntrinsics[IID-Intrinsics.size()-1];
656 assert(0 && "Bad intrinsic ID!");
Evan Cheng32c1a4c2009-02-09 03:07:24 +0000657 abort();
Chris Lattner6cefb772008-01-05 22:25:12 +0000658 }
659
660 unsigned getIntrinsicID(Record *R) const {
661 for (unsigned i = 0, e = Intrinsics.size(); i != e; ++i)
662 if (Intrinsics[i].TheDef == R) return i;
Dale Johannesen49de9822009-02-05 01:49:45 +0000663 for (unsigned i = 0, e = TgtIntrinsics.size(); i != e; ++i)
664 if (TgtIntrinsics[i].TheDef == R) return i + Intrinsics.size();
Chris Lattner6cefb772008-01-05 22:25:12 +0000665 assert(0 && "Unknown intrinsic!");
666 abort();
667 }
668
Chris Lattnerb49985a2010-02-18 06:47:49 +0000669 const DAGDefaultOperand &getDefaultOperand(Record *R) const {
Chris Lattner6cefb772008-01-05 22:25:12 +0000670 assert(DefaultOperands.count(R) &&"Isn't an analyzed default operand!");
671 return DefaultOperands.find(R)->second;
672 }
673
674 // Pattern Fragment information.
675 TreePattern *getPatternFragment(Record *R) const {
676 assert(PatternFragments.count(R) && "Invalid pattern fragment request!");
677 return PatternFragments.find(R)->second;
678 }
Benjamin Kramer5b9e7ef2009-08-23 10:39:21 +0000679 typedef std::map<Record*, TreePattern*, RecordPtrCmp>::const_iterator
680 pf_iterator;
Chris Lattner6cefb772008-01-05 22:25:12 +0000681 pf_iterator pf_begin() const { return PatternFragments.begin(); }
682 pf_iterator pf_end() const { return PatternFragments.end(); }
683
684 // Patterns to match information.
Chris Lattner60d81392008-01-05 22:30:17 +0000685 typedef std::vector<PatternToMatch>::const_iterator ptm_iterator;
686 ptm_iterator ptm_begin() const { return PatternsToMatch.begin(); }
687 ptm_iterator ptm_end() const { return PatternsToMatch.end(); }
Chris Lattner6cefb772008-01-05 22:25:12 +0000688
689
690
691 const DAGInstruction &getInstruction(Record *R) const {
692 assert(Instructions.count(R) && "Unknown instruction!");
693 return Instructions.find(R)->second;
694 }
695
696 Record *get_intrinsic_void_sdnode() const {
697 return intrinsic_void_sdnode;
698 }
699 Record *get_intrinsic_w_chain_sdnode() const {
700 return intrinsic_w_chain_sdnode;
701 }
702 Record *get_intrinsic_wo_chain_sdnode() const {
703 return intrinsic_wo_chain_sdnode;
704 }
705
Jakob Stoklund Olesen11ee5082009-10-15 18:50:03 +0000706 bool hasTargetIntrinsics() { return !TgtIntrinsics.empty(); }
707
Chris Lattner6cefb772008-01-05 22:25:12 +0000708private:
709 void ParseNodeInfo();
Chris Lattner443e3f92008-01-05 22:54:53 +0000710 void ParseNodeTransforms();
Chris Lattner6cefb772008-01-05 22:25:12 +0000711 void ParseComplexPatterns();
Chris Lattnerdc32f982008-01-05 22:43:57 +0000712 void ParsePatternFragments();
Chris Lattner6cefb772008-01-05 22:25:12 +0000713 void ParseDefaultOperands();
714 void ParseInstructions();
715 void ParsePatterns();
Dan Gohmanee4fa192008-04-03 00:02:49 +0000716 void InferInstructionFlags();
Chris Lattner6cefb772008-01-05 22:25:12 +0000717 void GenerateVariants();
718
Chris Lattner25b6f912010-02-23 06:16:51 +0000719 void AddPatternToMatch(const TreePattern *Pattern, const PatternToMatch &PTM);
Chris Lattner6cefb772008-01-05 22:25:12 +0000720 void FindPatternInputsAndOutputs(TreePattern *I, TreePatternNode *Pat,
721 std::map<std::string,
722 TreePatternNode*> &InstInputs,
723 std::map<std::string,
724 TreePatternNode*> &InstResults,
725 std::vector<Record*> &InstImpInputs,
726 std::vector<Record*> &InstImpResults);
727};
728} // end namespace llvm
729
730#endif