blob: 0a1362ab2494fa2a1a4fde57c37869ce8c7266b9 [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.
Chris Lattner66fb9d22010-03-24 00:01:16 +0000132 bool EnforceVectorEltTypeIs(EEVT::TypeSet &VT, TreePattern &TP);
Chris Lattner2cacec52010-03-15 06:00:16 +0000133
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;
Chris Lattner6cefb772008-01-05 22:25:12 +0000185};
186
187/// SDNodeInfo - One of these records is created for each SDNode instance in
188/// the target .td file. This represents the various dag nodes we will be
189/// processing.
190class SDNodeInfo {
191 Record *Def;
192 std::string EnumName;
193 std::string SDClassName;
194 unsigned Properties;
195 unsigned NumResults;
196 int NumOperands;
197 std::vector<SDTypeConstraint> TypeConstraints;
198public:
199 SDNodeInfo(Record *R); // Parse the specified record.
200
201 unsigned getNumResults() const { return NumResults; }
Chris Lattner2a22cdc2010-03-28 08:48:47 +0000202
203 /// getNumOperands - This is the number of operands required or -1 if
204 /// variadic.
Chris Lattner6cefb772008-01-05 22:25:12 +0000205 int getNumOperands() const { return NumOperands; }
206 Record *getRecord() const { return Def; }
207 const std::string &getEnumName() const { return EnumName; }
208 const std::string &getSDClassName() const { return SDClassName; }
209
210 const std::vector<SDTypeConstraint> &getTypeConstraints() const {
211 return TypeConstraints;
212 }
213
Chris Lattner22579812010-02-28 00:22:30 +0000214 /// getKnownType - If the type constraints on this node imply a fixed type
215 /// (e.g. all stores return void, etc), then return it as an
Chris Lattneraac5b5b2010-03-19 01:14:27 +0000216 /// MVT::SimpleValueType. Otherwise, return MVT::Other.
Chris Lattner084df622010-03-24 00:41:19 +0000217 MVT::SimpleValueType getKnownType(unsigned ResNo) const;
Chris Lattner22579812010-02-28 00:22:30 +0000218
Chris Lattner6cefb772008-01-05 22:25:12 +0000219 /// hasProperty - Return true if this node has the specified property.
220 ///
221 bool hasProperty(enum SDNP Prop) const { return Properties & (1 << Prop); }
222
223 /// ApplyTypeConstraints - Given a node in a pattern, apply the type
224 /// constraints for this node to the operands of the node. This returns
225 /// true if it makes a change, false otherwise. If a type contradiction is
226 /// found, throw an exception.
227 bool ApplyTypeConstraints(TreePatternNode *N, TreePattern &TP) const {
228 bool MadeChange = false;
229 for (unsigned i = 0, e = TypeConstraints.size(); i != e; ++i)
230 MadeChange |= TypeConstraints[i].ApplyTypeConstraint(N, *this, TP);
231 return MadeChange;
232 }
233};
234
235/// FIXME: TreePatternNode's can be shared in some cases (due to dag-shaped
236/// patterns), and as such should be ref counted. We currently just leak all
237/// TreePatternNode objects!
238class TreePatternNode {
Chris Lattnerd7349192010-03-19 21:37:09 +0000239 /// The type of each node result. Before and during type inference, each
240 /// result may be a set of possible types. After (successful) type inference,
241 /// each is a single concrete type.
242 SmallVector<EEVT::TypeSet, 1> Types;
Chris Lattner6cefb772008-01-05 22:25:12 +0000243
244 /// Operator - The Record for the operator if this is an interior node (not
245 /// a leaf).
246 Record *Operator;
247
248 /// Val - The init value (e.g. the "GPRC" record, or "7") for a leaf.
249 ///
250 Init *Val;
251
252 /// Name - The name given to this node with the :$foo notation.
253 ///
254 std::string Name;
255
Dan Gohman0540e172008-10-15 06:17:21 +0000256 /// PredicateFns - The predicate functions to execute on this node to check
257 /// for a match. If this list is empty, no predicate is involved.
258 std::vector<std::string> PredicateFns;
Chris Lattner6cefb772008-01-05 22:25:12 +0000259
260 /// TransformFn - The transformation function to execute on this node before
261 /// it can be substituted into the resulting instruction on a pattern match.
262 Record *TransformFn;
263
264 std::vector<TreePatternNode*> Children;
265public:
Chris Lattnerd7349192010-03-19 21:37:09 +0000266 TreePatternNode(Record *Op, const std::vector<TreePatternNode*> &Ch,
267 unsigned NumResults)
268 : Operator(Op), Val(0), TransformFn(0), Children(Ch) {
269 Types.resize(NumResults);
270 }
271 TreePatternNode(Init *val, unsigned NumResults) // leaf ctor
Chris Lattner2cacec52010-03-15 06:00:16 +0000272 : Operator(0), Val(val), TransformFn(0) {
Chris Lattnerd7349192010-03-19 21:37:09 +0000273 Types.resize(NumResults);
Chris Lattner6cefb772008-01-05 22:25:12 +0000274 }
275 ~TreePatternNode();
276
277 const std::string &getName() const { return Name; }
Chris Lattnerc2173052010-03-28 06:50:34 +0000278 void setName(StringRef N) { Name.assign(N.begin(), N.end()); }
Chris Lattner6cefb772008-01-05 22:25:12 +0000279
280 bool isLeaf() const { return Val != 0; }
Chris Lattner2cacec52010-03-15 06:00:16 +0000281
282 // Type accessors.
Chris Lattnerd7349192010-03-19 21:37:09 +0000283 unsigned getNumTypes() const { return Types.size(); }
284 MVT::SimpleValueType getType(unsigned ResNo) const {
285 return Types[ResNo].getConcrete();
286 }
287 const SmallVectorImpl<EEVT::TypeSet> &getExtTypes() const { return Types; }
288 const EEVT::TypeSet &getExtType(unsigned ResNo) const { return Types[ResNo]; }
289 EEVT::TypeSet &getExtType(unsigned ResNo) { return Types[ResNo]; }
290 void setType(unsigned ResNo, const EEVT::TypeSet &T) { Types[ResNo] = T; }
Chris Lattner2cacec52010-03-15 06:00:16 +0000291
Chris Lattnerd7349192010-03-19 21:37:09 +0000292 bool hasTypeSet(unsigned ResNo) const {
293 return Types[ResNo].isConcrete();
294 }
295 bool isTypeCompletelyUnknown(unsigned ResNo) const {
296 return Types[ResNo].isCompletelyUnknown();
297 }
298 bool isTypeDynamicallyResolved(unsigned ResNo) const {
299 return Types[ResNo].isDynamicallyResolved();
300 }
Chris Lattner6cefb772008-01-05 22:25:12 +0000301
302 Init *getLeafValue() const { assert(isLeaf()); return Val; }
303 Record *getOperator() const { assert(!isLeaf()); return Operator; }
304
305 unsigned getNumChildren() const { return Children.size(); }
306 TreePatternNode *getChild(unsigned N) const { return Children[N]; }
307 void setChild(unsigned i, TreePatternNode *N) {
308 Children[i] = N;
309 }
Chris Lattnere39650a2010-02-16 06:10:58 +0000310
311 /// hasChild - Return true if N is any of our children.
312 bool hasChild(const TreePatternNode *N) const {
313 for (unsigned i = 0, e = Children.size(); i != e; ++i)
314 if (Children[i] == N) return true;
315 return false;
316 }
Chris Lattnere67bde52008-01-06 05:36:50 +0000317
Chris Lattner47661322010-02-14 22:22:58 +0000318 const std::vector<std::string> &getPredicateFns() const {return PredicateFns;}
Dan Gohman0540e172008-10-15 06:17:21 +0000319 void clearPredicateFns() { PredicateFns.clear(); }
320 void setPredicateFns(const std::vector<std::string> &Fns) {
321 assert(PredicateFns.empty() && "Overwriting non-empty predicate list!");
322 PredicateFns = Fns;
323 }
324 void addPredicateFn(const std::string &Fn) {
325 assert(!Fn.empty() && "Empty predicate string!");
326 if (std::find(PredicateFns.begin(), PredicateFns.end(), Fn) ==
327 PredicateFns.end())
328 PredicateFns.push_back(Fn);
329 }
Chris Lattner6cefb772008-01-05 22:25:12 +0000330
331 Record *getTransformFn() const { return TransformFn; }
332 void setTransformFn(Record *Fn) { TransformFn = Fn; }
333
Chris Lattnere67bde52008-01-06 05:36:50 +0000334 /// getIntrinsicInfo - If this node corresponds to an intrinsic, return the
335 /// CodeGenIntrinsic information for it, otherwise return a null pointer.
336 const CodeGenIntrinsic *getIntrinsicInfo(const CodeGenDAGPatterns &CDP) const;
Evan Cheng6bd95672008-06-16 20:29:38 +0000337
Chris Lattner47661322010-02-14 22:22:58 +0000338 /// getComplexPatternInfo - If this node corresponds to a ComplexPattern,
339 /// return the ComplexPattern information, otherwise return null.
340 const ComplexPattern *
341 getComplexPatternInfo(const CodeGenDAGPatterns &CGP) const;
342
343 /// NodeHasProperty - Return true if this node has the specified property.
Chris Lattner751d5aa2010-02-14 22:33:49 +0000344 bool NodeHasProperty(SDNP Property, const CodeGenDAGPatterns &CGP) const;
Chris Lattner47661322010-02-14 22:22:58 +0000345
346 /// TreeHasProperty - Return true if any node in this tree has the specified
347 /// property.
Chris Lattner751d5aa2010-02-14 22:33:49 +0000348 bool TreeHasProperty(SDNP Property, const CodeGenDAGPatterns &CGP) const;
Chris Lattner47661322010-02-14 22:22:58 +0000349
Evan Cheng6bd95672008-06-16 20:29:38 +0000350 /// isCommutativeIntrinsic - Return true if the node is an intrinsic which is
351 /// marked isCommutative.
352 bool isCommutativeIntrinsic(const CodeGenDAGPatterns &CDP) const;
Chris Lattnere67bde52008-01-06 05:36:50 +0000353
Daniel Dunbar1a551802009-07-03 00:10:29 +0000354 void print(raw_ostream &OS) const;
Chris Lattner6cefb772008-01-05 22:25:12 +0000355 void dump() const;
356
357public: // Higher level manipulation routines.
358
359 /// clone - Return a new copy of this tree.
360 ///
361 TreePatternNode *clone() const;
Chris Lattner47661322010-02-14 22:22:58 +0000362
363 /// RemoveAllTypes - Recursively strip all the types of this tree.
364 void RemoveAllTypes();
Chris Lattner6cefb772008-01-05 22:25:12 +0000365
366 /// isIsomorphicTo - Return true if this node is recursively isomorphic to
367 /// the specified node. For this comparison, all of the state of the node
368 /// is considered, except for the assigned name. Nodes with differing names
369 /// that are otherwise identical are considered isomorphic.
Scott Michel327d0652008-03-05 17:49:05 +0000370 bool isIsomorphicTo(const TreePatternNode *N,
371 const MultipleUseVarSet &DepVars) const;
Chris Lattner6cefb772008-01-05 22:25:12 +0000372
373 /// SubstituteFormalArguments - Replace the formal arguments in this tree
374 /// with actual values specified by ArgMap.
375 void SubstituteFormalArguments(std::map<std::string,
376 TreePatternNode*> &ArgMap);
377
378 /// InlinePatternFragments - If this pattern refers to any pattern
379 /// fragments, inline them into place, giving us a pattern without any
380 /// PatFrag references.
381 TreePatternNode *InlinePatternFragments(TreePattern &TP);
382
Bob Wilson6c01ca92009-01-05 17:23:09 +0000383 /// ApplyTypeConstraints - Apply all of the type constraints relevant to
Chris Lattner6cefb772008-01-05 22:25:12 +0000384 /// this node and its children in the tree. This returns true if it makes a
385 /// change, false otherwise. If a type contradiction is found, throw an
386 /// exception.
387 bool ApplyTypeConstraints(TreePattern &TP, bool NotRegisters);
388
389 /// UpdateNodeType - Set the node type of N to VT if VT contains
390 /// information. If N already contains a conflicting type, then throw an
391 /// exception. This returns true if any information was updated.
392 ///
Chris Lattnerd7349192010-03-19 21:37:09 +0000393 bool UpdateNodeType(unsigned ResNo, const EEVT::TypeSet &InTy,
394 TreePattern &TP) {
395 return Types[ResNo].MergeInTypeInfo(InTy, TP);
Chris Lattner2cacec52010-03-15 06:00:16 +0000396 }
397
Chris Lattnerd7349192010-03-19 21:37:09 +0000398 bool UpdateNodeType(unsigned ResNo, MVT::SimpleValueType InTy,
399 TreePattern &TP) {
400 return Types[ResNo].MergeInTypeInfo(EEVT::TypeSet(InTy, TP), TP);
Chris Lattner6cefb772008-01-05 22:25:12 +0000401 }
402
403 /// ContainsUnresolvedType - Return true if this tree contains any
404 /// unresolved types.
405 bool ContainsUnresolvedType() const {
Chris Lattnerd7349192010-03-19 21:37:09 +0000406 for (unsigned i = 0, e = Types.size(); i != e; ++i)
407 if (!Types[i].isConcrete()) return true;
408
Chris Lattner6cefb772008-01-05 22:25:12 +0000409 for (unsigned i = 0, e = getNumChildren(); i != e; ++i)
410 if (getChild(i)->ContainsUnresolvedType()) return true;
411 return false;
412 }
413
414 /// canPatternMatch - If it is impossible for this pattern to match on this
415 /// target, fill in Reason and return false. Otherwise, return true.
Dan Gohmanee4fa192008-04-03 00:02:49 +0000416 bool canPatternMatch(std::string &Reason, const CodeGenDAGPatterns &CDP);
Chris Lattner6cefb772008-01-05 22:25:12 +0000417};
418
Chris Lattner383fed92010-02-14 21:10:33 +0000419inline raw_ostream &operator<<(raw_ostream &OS, const TreePatternNode &TPN) {
420 TPN.print(OS);
421 return OS;
422}
423
Chris Lattner6cefb772008-01-05 22:25:12 +0000424
425/// TreePattern - Represent a pattern, used for instructions, pattern
426/// fragments, etc.
427///
428class TreePattern {
429 /// Trees - The list of pattern trees which corresponds to this pattern.
430 /// Note that PatFrag's only have a single tree.
431 ///
432 std::vector<TreePatternNode*> Trees;
433
Chris Lattner2cacec52010-03-15 06:00:16 +0000434 /// NamedNodes - This is all of the nodes that have names in the trees in this
435 /// pattern.
436 StringMap<SmallVector<TreePatternNode*,1> > NamedNodes;
437
Chris Lattner6cefb772008-01-05 22:25:12 +0000438 /// TheRecord - The actual TableGen record corresponding to this pattern.
439 ///
440 Record *TheRecord;
441
442 /// Args - This is a list of all of the arguments to this pattern (for
443 /// PatFrag patterns), which are the 'node' markers in this pattern.
444 std::vector<std::string> Args;
445
446 /// CDP - the top-level object coordinating this madness.
447 ///
Chris Lattnerfe718932008-01-06 01:10:31 +0000448 CodeGenDAGPatterns &CDP;
Chris Lattner6cefb772008-01-05 22:25:12 +0000449
450 /// isInputPattern - True if this is an input pattern, something to match.
451 /// False if this is an output pattern, something to emit.
452 bool isInputPattern;
453public:
454
455 /// TreePattern constructor - Parse the specified DagInits into the
456 /// current record.
457 TreePattern(Record *TheRec, ListInit *RawPat, bool isInput,
Chris Lattnerfe718932008-01-06 01:10:31 +0000458 CodeGenDAGPatterns &ise);
Chris Lattner6cefb772008-01-05 22:25:12 +0000459 TreePattern(Record *TheRec, DagInit *Pat, bool isInput,
Chris Lattnerfe718932008-01-06 01:10:31 +0000460 CodeGenDAGPatterns &ise);
Chris Lattner6cefb772008-01-05 22:25:12 +0000461 TreePattern(Record *TheRec, TreePatternNode *Pat, bool isInput,
Chris Lattnerfe718932008-01-06 01:10:31 +0000462 CodeGenDAGPatterns &ise);
Chris Lattner6cefb772008-01-05 22:25:12 +0000463
464 /// getTrees - Return the tree patterns which corresponds to this pattern.
465 ///
466 const std::vector<TreePatternNode*> &getTrees() const { return Trees; }
467 unsigned getNumTrees() const { return Trees.size(); }
468 TreePatternNode *getTree(unsigned i) const { return Trees[i]; }
469 TreePatternNode *getOnlyTree() const {
470 assert(Trees.size() == 1 && "Doesn't have exactly one pattern!");
471 return Trees[0];
472 }
Chris Lattner2cacec52010-03-15 06:00:16 +0000473
474 const StringMap<SmallVector<TreePatternNode*,1> > &getNamedNodesMap() {
475 if (NamedNodes.empty())
476 ComputeNamedNodes();
477 return NamedNodes;
478 }
Chris Lattner6cefb772008-01-05 22:25:12 +0000479
480 /// getRecord - Return the actual TableGen record corresponding to this
481 /// pattern.
482 ///
483 Record *getRecord() const { return TheRecord; }
484
485 unsigned getNumArgs() const { return Args.size(); }
486 const std::string &getArgName(unsigned i) const {
487 assert(i < Args.size() && "Argument reference out of range!");
488 return Args[i];
489 }
490 std::vector<std::string> &getArgList() { return Args; }
491
Chris Lattnerfe718932008-01-06 01:10:31 +0000492 CodeGenDAGPatterns &getDAGPatterns() const { return CDP; }
Chris Lattner6cefb772008-01-05 22:25:12 +0000493
494 /// InlinePatternFragments - If this pattern refers to any pattern
495 /// fragments, inline them into place, giving us a pattern without any
496 /// PatFrag references.
497 void InlinePatternFragments() {
498 for (unsigned i = 0, e = Trees.size(); i != e; ++i)
499 Trees[i] = Trees[i]->InlinePatternFragments(*this);
500 }
501
502 /// InferAllTypes - Infer/propagate as many types throughout the expression
Jim Grosbachda4231f2009-03-26 16:17:51 +0000503 /// patterns as possible. Return true if all types are inferred, false
Chris Lattner6cefb772008-01-05 22:25:12 +0000504 /// otherwise. Throw an exception if a type contradiction is found.
Chris Lattner2cacec52010-03-15 06:00:16 +0000505 bool InferAllTypes(const StringMap<SmallVector<TreePatternNode*,1> >
506 *NamedTypes=0);
Chris Lattner6cefb772008-01-05 22:25:12 +0000507
508 /// error - Throw an exception, prefixing it with information about this
509 /// pattern.
510 void error(const std::string &Msg) const;
511
Daniel Dunbar1a551802009-07-03 00:10:29 +0000512 void print(raw_ostream &OS) const;
Chris Lattner6cefb772008-01-05 22:25:12 +0000513 void dump() const;
514
515private:
Chris Lattnerc2173052010-03-28 06:50:34 +0000516 TreePatternNode *ParseTreePattern(Init *DI, StringRef OpName);
Chris Lattner2cacec52010-03-15 06:00:16 +0000517 void ComputeNamedNodes();
518 void ComputeNamedNodes(TreePatternNode *N);
Chris Lattner6cefb772008-01-05 22:25:12 +0000519};
520
521/// DAGDefaultOperand - One of these is created for each PredicateOperand
522/// or OptionalDefOperand that has a set ExecuteAlways / DefaultOps field.
523struct DAGDefaultOperand {
524 std::vector<TreePatternNode*> DefaultOps;
525};
526
527class DAGInstruction {
528 TreePattern *Pattern;
529 std::vector<Record*> Results;
530 std::vector<Record*> Operands;
531 std::vector<Record*> ImpResults;
Chris Lattner6cefb772008-01-05 22:25:12 +0000532 TreePatternNode *ResultPattern;
533public:
534 DAGInstruction(TreePattern *TP,
535 const std::vector<Record*> &results,
536 const std::vector<Record*> &operands,
Chris Lattner62bcec82010-04-20 06:28:43 +0000537 const std::vector<Record*> &impresults)
Chris Lattner6cefb772008-01-05 22:25:12 +0000538 : Pattern(TP), Results(results), Operands(operands),
Chris Lattner62bcec82010-04-20 06:28:43 +0000539 ImpResults(impresults), ResultPattern(0) {}
Chris Lattner6cefb772008-01-05 22:25:12 +0000540
Chris Lattnerf1ab4f12008-01-06 01:52:22 +0000541 const TreePattern *getPattern() const { return Pattern; }
Chris Lattner6cefb772008-01-05 22:25:12 +0000542 unsigned getNumResults() const { return Results.size(); }
543 unsigned getNumOperands() const { return Operands.size(); }
544 unsigned getNumImpResults() const { return ImpResults.size(); }
Chris Lattner6cefb772008-01-05 22:25:12 +0000545 const std::vector<Record*>& getImpResults() const { return ImpResults; }
546
547 void setResultPattern(TreePatternNode *R) { ResultPattern = R; }
548
549 Record *getResult(unsigned RN) const {
550 assert(RN < Results.size());
551 return Results[RN];
552 }
553
554 Record *getOperand(unsigned ON) const {
555 assert(ON < Operands.size());
556 return Operands[ON];
557 }
558
559 Record *getImpResult(unsigned RN) const {
560 assert(RN < ImpResults.size());
561 return ImpResults[RN];
562 }
563
Chris Lattner6cefb772008-01-05 22:25:12 +0000564 TreePatternNode *getResultPattern() const { return ResultPattern; }
565};
566
Chris Lattnerfe718932008-01-06 01:10:31 +0000567/// PatternToMatch - Used by CodeGenDAGPatterns to keep tab of patterns
Chris Lattner6cefb772008-01-05 22:25:12 +0000568/// processed to produce isel.
Chris Lattnerb49985a2010-02-18 06:47:49 +0000569class PatternToMatch {
570public:
Chris Lattner6cefb772008-01-05 22:25:12 +0000571 PatternToMatch(ListInit *preds,
572 TreePatternNode *src, TreePatternNode *dst,
573 const std::vector<Record*> &dstregs,
Chris Lattner117ccb72010-03-01 22:09:11 +0000574 unsigned complexity, unsigned uid)
575 : Predicates(preds), SrcPattern(src), DstPattern(dst),
576 Dstregs(dstregs), AddedComplexity(complexity), ID(uid) {}
Chris Lattner6cefb772008-01-05 22:25:12 +0000577
578 ListInit *Predicates; // Top level predicate conditions to match.
579 TreePatternNode *SrcPattern; // Source pattern to match.
580 TreePatternNode *DstPattern; // Resulting pattern.
581 std::vector<Record*> Dstregs; // Physical register defs being matched.
582 unsigned AddedComplexity; // Add to matching pattern complexity.
Chris Lattner117ccb72010-03-01 22:09:11 +0000583 unsigned ID; // Unique ID for the record.
Chris Lattner6cefb772008-01-05 22:25:12 +0000584
585 ListInit *getPredicates() const { return Predicates; }
586 TreePatternNode *getSrcPattern() const { return SrcPattern; }
587 TreePatternNode *getDstPattern() const { return DstPattern; }
588 const std::vector<Record*> &getDstRegs() const { return Dstregs; }
589 unsigned getAddedComplexity() const { return AddedComplexity; }
Dan Gohman22bb3112008-08-22 00:20:26 +0000590
591 std::string getPredicateCheck() const;
Chris Lattner48e86db2010-03-29 01:40:38 +0000592
593 /// Compute the complexity metric for the input pattern. This roughly
594 /// corresponds to the number of nodes that are covered.
595 unsigned getPatternComplexity(const CodeGenDAGPatterns &CGP) const;
Chris Lattner6cefb772008-01-05 22:25:12 +0000596};
597
Daniel Dunbar6f5cc822009-08-23 09:47:37 +0000598// Deterministic comparison of Record*.
599struct RecordPtrCmp {
600 bool operator()(const Record *LHS, const Record *RHS) const;
601};
Chris Lattner6cefb772008-01-05 22:25:12 +0000602
Chris Lattnerfe718932008-01-06 01:10:31 +0000603class CodeGenDAGPatterns {
Chris Lattner6cefb772008-01-05 22:25:12 +0000604 RecordKeeper &Records;
605 CodeGenTarget Target;
606 std::vector<CodeGenIntrinsic> Intrinsics;
Dale Johannesen49de9822009-02-05 01:49:45 +0000607 std::vector<CodeGenIntrinsic> TgtIntrinsics;
Chris Lattner6cefb772008-01-05 22:25:12 +0000608
Daniel Dunbar6f5cc822009-08-23 09:47:37 +0000609 std::map<Record*, SDNodeInfo, RecordPtrCmp> SDNodes;
610 std::map<Record*, std::pair<Record*, std::string>, RecordPtrCmp> SDNodeXForms;
611 std::map<Record*, ComplexPattern, RecordPtrCmp> ComplexPatterns;
612 std::map<Record*, TreePattern*, RecordPtrCmp> PatternFragments;
613 std::map<Record*, DAGDefaultOperand, RecordPtrCmp> DefaultOperands;
614 std::map<Record*, DAGInstruction, RecordPtrCmp> Instructions;
Chris Lattner6cefb772008-01-05 22:25:12 +0000615
616 // Specific SDNode definitions:
617 Record *intrinsic_void_sdnode;
618 Record *intrinsic_w_chain_sdnode, *intrinsic_wo_chain_sdnode;
619
620 /// PatternsToMatch - All of the things we are matching on the DAG. The first
621 /// value is the pattern to match, the second pattern is the result to
622 /// emit.
623 std::vector<PatternToMatch> PatternsToMatch;
624public:
Chris Lattnerfe718932008-01-06 01:10:31 +0000625 CodeGenDAGPatterns(RecordKeeper &R);
626 ~CodeGenDAGPatterns();
Chris Lattner6cefb772008-01-05 22:25:12 +0000627
Dan Gohmanee4fa192008-04-03 00:02:49 +0000628 CodeGenTarget &getTargetInfo() { return Target; }
Chris Lattner6cefb772008-01-05 22:25:12 +0000629 const CodeGenTarget &getTargetInfo() const { return Target; }
630
631 Record *getSDNodeNamed(const std::string &Name) const;
632
633 const SDNodeInfo &getSDNodeInfo(Record *R) const {
634 assert(SDNodes.count(R) && "Unknown node!");
635 return SDNodes.find(R)->second;
636 }
637
Chris Lattner443e3f92008-01-05 22:54:53 +0000638 // Node transformation lookups.
639 typedef std::pair<Record*, std::string> NodeXForm;
640 const NodeXForm &getSDNodeTransform(Record *R) const {
Chris Lattner6cefb772008-01-05 22:25:12 +0000641 assert(SDNodeXForms.count(R) && "Invalid transform!");
642 return SDNodeXForms.find(R)->second;
643 }
644
Benjamin Kramer5b9e7ef2009-08-23 10:39:21 +0000645 typedef std::map<Record*, NodeXForm, RecordPtrCmp>::const_iterator
646 nx_iterator;
Chris Lattner443e3f92008-01-05 22:54:53 +0000647 nx_iterator nx_begin() const { return SDNodeXForms.begin(); }
648 nx_iterator nx_end() const { return SDNodeXForms.end(); }
649
650
Chris Lattner6cefb772008-01-05 22:25:12 +0000651 const ComplexPattern &getComplexPattern(Record *R) const {
652 assert(ComplexPatterns.count(R) && "Unknown addressing mode!");
653 return ComplexPatterns.find(R)->second;
654 }
655
656 const CodeGenIntrinsic &getIntrinsic(Record *R) const {
657 for (unsigned i = 0, e = Intrinsics.size(); i != e; ++i)
658 if (Intrinsics[i].TheDef == R) return Intrinsics[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 TgtIntrinsics[i];
Chris Lattner6cefb772008-01-05 22:25:12 +0000661 assert(0 && "Unknown intrinsic!");
662 abort();
663 }
664
665 const CodeGenIntrinsic &getIntrinsicInfo(unsigned IID) const {
Dale Johannesen49de9822009-02-05 01:49:45 +0000666 if (IID-1 < Intrinsics.size())
667 return Intrinsics[IID-1];
668 if (IID-Intrinsics.size()-1 < TgtIntrinsics.size())
669 return TgtIntrinsics[IID-Intrinsics.size()-1];
670 assert(0 && "Bad intrinsic ID!");
Evan Cheng32c1a4c2009-02-09 03:07:24 +0000671 abort();
Chris Lattner6cefb772008-01-05 22:25:12 +0000672 }
673
674 unsigned getIntrinsicID(Record *R) const {
675 for (unsigned i = 0, e = Intrinsics.size(); i != e; ++i)
676 if (Intrinsics[i].TheDef == R) return i;
Dale Johannesen49de9822009-02-05 01:49:45 +0000677 for (unsigned i = 0, e = TgtIntrinsics.size(); i != e; ++i)
678 if (TgtIntrinsics[i].TheDef == R) return i + Intrinsics.size();
Chris Lattner6cefb772008-01-05 22:25:12 +0000679 assert(0 && "Unknown intrinsic!");
680 abort();
681 }
682
Chris Lattnerb49985a2010-02-18 06:47:49 +0000683 const DAGDefaultOperand &getDefaultOperand(Record *R) const {
Chris Lattner6cefb772008-01-05 22:25:12 +0000684 assert(DefaultOperands.count(R) &&"Isn't an analyzed default operand!");
685 return DefaultOperands.find(R)->second;
686 }
687
688 // Pattern Fragment information.
689 TreePattern *getPatternFragment(Record *R) const {
690 assert(PatternFragments.count(R) && "Invalid pattern fragment request!");
691 return PatternFragments.find(R)->second;
692 }
Chris Lattnerd7349192010-03-19 21:37:09 +0000693 TreePattern *getPatternFragmentIfRead(Record *R) const {
694 if (!PatternFragments.count(R)) return 0;
695 return PatternFragments.find(R)->second;
696 }
697
Benjamin Kramer5b9e7ef2009-08-23 10:39:21 +0000698 typedef std::map<Record*, TreePattern*, RecordPtrCmp>::const_iterator
699 pf_iterator;
Chris Lattner6cefb772008-01-05 22:25:12 +0000700 pf_iterator pf_begin() const { return PatternFragments.begin(); }
701 pf_iterator pf_end() const { return PatternFragments.end(); }
702
703 // Patterns to match information.
Chris Lattner60d81392008-01-05 22:30:17 +0000704 typedef std::vector<PatternToMatch>::const_iterator ptm_iterator;
705 ptm_iterator ptm_begin() const { return PatternsToMatch.begin(); }
706 ptm_iterator ptm_end() const { return PatternsToMatch.end(); }
Chris Lattner6cefb772008-01-05 22:25:12 +0000707
708
709
710 const DAGInstruction &getInstruction(Record *R) const {
711 assert(Instructions.count(R) && "Unknown instruction!");
712 return Instructions.find(R)->second;
713 }
714
715 Record *get_intrinsic_void_sdnode() const {
716 return intrinsic_void_sdnode;
717 }
718 Record *get_intrinsic_w_chain_sdnode() const {
719 return intrinsic_w_chain_sdnode;
720 }
721 Record *get_intrinsic_wo_chain_sdnode() const {
722 return intrinsic_wo_chain_sdnode;
723 }
724
Jakob Stoklund Olesen11ee5082009-10-15 18:50:03 +0000725 bool hasTargetIntrinsics() { return !TgtIntrinsics.empty(); }
726
Chris Lattner6cefb772008-01-05 22:25:12 +0000727private:
728 void ParseNodeInfo();
Chris Lattner443e3f92008-01-05 22:54:53 +0000729 void ParseNodeTransforms();
Chris Lattner6cefb772008-01-05 22:25:12 +0000730 void ParseComplexPatterns();
Chris Lattnerdc32f982008-01-05 22:43:57 +0000731 void ParsePatternFragments();
Chris Lattner6cefb772008-01-05 22:25:12 +0000732 void ParseDefaultOperands();
733 void ParseInstructions();
734 void ParsePatterns();
Dan Gohmanee4fa192008-04-03 00:02:49 +0000735 void InferInstructionFlags();
Chris Lattner6cefb772008-01-05 22:25:12 +0000736 void GenerateVariants();
737
Chris Lattner25b6f912010-02-23 06:16:51 +0000738 void AddPatternToMatch(const TreePattern *Pattern, const PatternToMatch &PTM);
Chris Lattner6cefb772008-01-05 22:25:12 +0000739 void FindPatternInputsAndOutputs(TreePattern *I, TreePatternNode *Pat,
740 std::map<std::string,
741 TreePatternNode*> &InstInputs,
742 std::map<std::string,
743 TreePatternNode*> &InstResults,
Chris Lattner6cefb772008-01-05 22:25:12 +0000744 std::vector<Record*> &InstImpResults);
745};
746} // end namespace llvm
747
748#endif