blob: f1f7bca1a8f57550fa10eeb548f595b60b68b9a7 [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"
24
25namespace llvm {
26 class Record;
27 struct Init;
28 class ListInit;
29 class DagInit;
30 class SDNodeInfo;
31 class TreePattern;
32 class TreePatternNode;
Chris Lattnerfe718932008-01-06 01:10:31 +000033 class CodeGenDAGPatterns;
Chris Lattner6cefb772008-01-05 22:25:12 +000034 class ComplexPattern;
35
Owen Andersone50ed302009-08-10 22:56:29 +000036/// EEVT::DAGISelGenValueType - These are some extended forms of
Owen Anderson825b72b2009-08-11 20:47:22 +000037/// MVT::SimpleValueType that we use as lattice values during type inference.
Bob Wilsoncdfa01b2009-08-29 05:53:25 +000038/// The existing MVT iAny, fAny and vAny types suffice to represent
39/// arbitrary integer, floating-point, and vector types, so only an unknown
40/// value is needed.
Owen Andersone50ed302009-08-10 22:56:29 +000041namespace EEVT {
Chris Lattner6cefb772008-01-05 22:25:12 +000042 enum DAGISelGenValueType {
Bob Wilsoncdfa01b2009-08-29 05:53:25 +000043 isUnknown = MVT::LAST_VALUETYPE
Chris Lattner6cefb772008-01-05 22:25:12 +000044 };
Duncan Sands83ec4b62008-06-06 12:08:01 +000045
Bob Wilson36e3e662009-08-12 22:30:59 +000046 /// isExtIntegerInVTs - Return true if the specified extended value type
Bob Wilsoncdfa01b2009-08-29 05:53:25 +000047 /// vector contains iAny or an integer value type.
Chris Lattner6cefb772008-01-05 22:25:12 +000048 bool isExtIntegerInVTs(const std::vector<unsigned char> &EVTs);
49
Bob Wilson36e3e662009-08-12 22:30:59 +000050 /// isExtFloatingPointInVTs - Return true if the specified extended value
Bob Wilsoncdfa01b2009-08-29 05:53:25 +000051 /// type vector contains fAny or a FP value type.
Chris Lattner6cefb772008-01-05 22:25:12 +000052 bool isExtFloatingPointInVTs(const std::vector<unsigned char> &EVTs);
Bob Wilson36e3e662009-08-12 22:30:59 +000053
54 /// isExtVectorinVTs - Return true if the specified extended value type
Bob Wilsoncdfa01b2009-08-29 05:53:25 +000055 /// vector contains vAny or a vector value type.
Bob Wilson36e3e662009-08-12 22:30:59 +000056 bool isExtVectorInVTs(const std::vector<unsigned char> &EVTs);
Chris Lattner6cefb772008-01-05 22:25:12 +000057}
58
Scott Michel327d0652008-03-05 17:49:05 +000059/// Set type used to track multiply used variables in patterns
60typedef std::set<std::string> MultipleUseVarSet;
61
Chris Lattner6cefb772008-01-05 22:25:12 +000062/// SDTypeConstraint - This is a discriminated union of constraints,
63/// corresponding to the SDTypeConstraint tablegen class in Target.td.
64struct SDTypeConstraint {
65 SDTypeConstraint(Record *R);
66
67 unsigned OperandNo; // The operand # this constraint applies to.
68 enum {
Bob Wilson36e3e662009-08-12 22:30:59 +000069 SDTCisVT, SDTCisPtrTy, SDTCisInt, SDTCisFP, SDTCisVec, SDTCisSameAs,
Nate Begeman9008ca62009-04-27 18:41:29 +000070 SDTCisVTSmallerThanOp, SDTCisOpSmallerThanOp, SDTCisEltOfVec
Chris Lattner6cefb772008-01-05 22:25:12 +000071 } ConstraintType;
72
73 union { // The discriminated union.
74 struct {
Duncan Sands83ec4b62008-06-06 12:08:01 +000075 unsigned char VT;
Chris Lattner6cefb772008-01-05 22:25:12 +000076 } SDTCisVT_Info;
77 struct {
78 unsigned OtherOperandNum;
79 } SDTCisSameAs_Info;
80 struct {
81 unsigned OtherOperandNum;
82 } SDTCisVTSmallerThanOp_Info;
83 struct {
84 unsigned BigOperandNum;
85 } SDTCisOpSmallerThanOp_Info;
86 struct {
87 unsigned OtherOperandNum;
Nate Begemanb5af3342008-02-09 01:37:05 +000088 } SDTCisEltOfVec_Info;
Chris Lattner6cefb772008-01-05 22:25:12 +000089 } x;
90
91 /// ApplyTypeConstraint - Given a node in a pattern, apply this type
92 /// constraint to the nodes operands. This returns true if it makes a
93 /// change, false otherwise. If a type contradiction is found, throw an
94 /// exception.
95 bool ApplyTypeConstraint(TreePatternNode *N, const SDNodeInfo &NodeInfo,
96 TreePattern &TP) const;
97
98 /// getOperandNum - Return the node corresponding to operand #OpNo in tree
99 /// N, which has NumResults results.
100 TreePatternNode *getOperandNum(unsigned OpNo, TreePatternNode *N,
101 unsigned NumResults) const;
102};
103
104/// SDNodeInfo - One of these records is created for each SDNode instance in
105/// the target .td file. This represents the various dag nodes we will be
106/// processing.
107class SDNodeInfo {
108 Record *Def;
109 std::string EnumName;
110 std::string SDClassName;
111 unsigned Properties;
112 unsigned NumResults;
113 int NumOperands;
114 std::vector<SDTypeConstraint> TypeConstraints;
115public:
116 SDNodeInfo(Record *R); // Parse the specified record.
117
118 unsigned getNumResults() const { return NumResults; }
119 int getNumOperands() const { return NumOperands; }
120 Record *getRecord() const { return Def; }
121 const std::string &getEnumName() const { return EnumName; }
122 const std::string &getSDClassName() const { return SDClassName; }
123
124 const std::vector<SDTypeConstraint> &getTypeConstraints() const {
125 return TypeConstraints;
126 }
127
128 /// hasProperty - Return true if this node has the specified property.
129 ///
130 bool hasProperty(enum SDNP Prop) const { return Properties & (1 << Prop); }
131
132 /// ApplyTypeConstraints - Given a node in a pattern, apply the type
133 /// constraints for this node to the operands of the node. This returns
134 /// true if it makes a change, false otherwise. If a type contradiction is
135 /// found, throw an exception.
136 bool ApplyTypeConstraints(TreePatternNode *N, TreePattern &TP) const {
137 bool MadeChange = false;
138 for (unsigned i = 0, e = TypeConstraints.size(); i != e; ++i)
139 MadeChange |= TypeConstraints[i].ApplyTypeConstraint(N, *this, TP);
140 return MadeChange;
141 }
142};
143
144/// FIXME: TreePatternNode's can be shared in some cases (due to dag-shaped
145/// patterns), and as such should be ref counted. We currently just leak all
146/// TreePatternNode objects!
147class TreePatternNode {
Owen Andersone50ed302009-08-10 22:56:29 +0000148 /// The inferred type for this node, or EEVT::isUnknown if it hasn't
Dan Gohmane1ca7c42009-03-31 16:46:45 +0000149 /// been determined yet. This is a std::vector because during inference
150 /// there may be multiple possible types.
Chris Lattner6cefb772008-01-05 22:25:12 +0000151 std::vector<unsigned char> Types;
152
153 /// Operator - The Record for the operator if this is an interior node (not
154 /// a leaf).
155 Record *Operator;
156
157 /// Val - The init value (e.g. the "GPRC" record, or "7") for a leaf.
158 ///
159 Init *Val;
160
161 /// Name - The name given to this node with the :$foo notation.
162 ///
163 std::string Name;
164
Dan Gohman0540e172008-10-15 06:17:21 +0000165 /// PredicateFns - The predicate functions to execute on this node to check
166 /// for a match. If this list is empty, no predicate is involved.
167 std::vector<std::string> PredicateFns;
Chris Lattner6cefb772008-01-05 22:25:12 +0000168
169 /// TransformFn - The transformation function to execute on this node before
170 /// it can be substituted into the resulting instruction on a pattern match.
171 Record *TransformFn;
172
173 std::vector<TreePatternNode*> Children;
174public:
175 TreePatternNode(Record *Op, const std::vector<TreePatternNode*> &Ch)
176 : Types(), Operator(Op), Val(0), TransformFn(0),
Owen Andersone50ed302009-08-10 22:56:29 +0000177 Children(Ch) { Types.push_back(EEVT::isUnknown); }
Chris Lattner6cefb772008-01-05 22:25:12 +0000178 TreePatternNode(Init *val) // leaf ctor
179 : Types(), Operator(0), Val(val), TransformFn(0) {
Owen Andersone50ed302009-08-10 22:56:29 +0000180 Types.push_back(EEVT::isUnknown);
Chris Lattner6cefb772008-01-05 22:25:12 +0000181 }
182 ~TreePatternNode();
183
184 const std::string &getName() const { return Name; }
185 void setName(const std::string &N) { Name = N; }
186
187 bool isLeaf() const { return Val != 0; }
188 bool hasTypeSet() const {
Owen Anderson825b72b2009-08-11 20:47:22 +0000189 return (Types[0] < MVT::LAST_VALUETYPE) || (Types[0] == MVT::iPTR) ||
190 (Types[0] == MVT::iPTRAny);
Chris Lattner6cefb772008-01-05 22:25:12 +0000191 }
192 bool isTypeCompletelyUnknown() const {
Owen Andersone50ed302009-08-10 22:56:29 +0000193 return Types[0] == EEVT::isUnknown;
Chris Lattner6cefb772008-01-05 22:25:12 +0000194 }
195 bool isTypeDynamicallyResolved() const {
Owen Anderson825b72b2009-08-11 20:47:22 +0000196 return (Types[0] == MVT::iPTR) || (Types[0] == MVT::iPTRAny);
Chris Lattner6cefb772008-01-05 22:25:12 +0000197 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000198 MVT::SimpleValueType getTypeNum(unsigned Num) const {
Chris Lattner6cefb772008-01-05 22:25:12 +0000199 assert(hasTypeSet() && "Doesn't have a type yet!");
200 assert(Types.size() > Num && "Type num out of range!");
Owen Anderson825b72b2009-08-11 20:47:22 +0000201 return (MVT::SimpleValueType)Types[Num];
Chris Lattner6cefb772008-01-05 22:25:12 +0000202 }
203 unsigned char getExtTypeNum(unsigned Num) const {
204 assert(Types.size() > Num && "Extended type num out of range!");
205 return Types[Num];
206 }
207 const std::vector<unsigned char> &getExtTypes() const { return Types; }
208 void setTypes(const std::vector<unsigned char> &T) { Types = T; }
Owen Andersone50ed302009-08-10 22:56:29 +0000209 void removeTypes() { Types = std::vector<unsigned char>(1, EEVT::isUnknown); }
Chris Lattner6cefb772008-01-05 22:25:12 +0000210
211 Init *getLeafValue() const { assert(isLeaf()); return Val; }
212 Record *getOperator() const { assert(!isLeaf()); return Operator; }
213
214 unsigned getNumChildren() const { return Children.size(); }
215 TreePatternNode *getChild(unsigned N) const { return Children[N]; }
216 void setChild(unsigned i, TreePatternNode *N) {
217 Children[i] = N;
218 }
Chris Lattnere39650a2010-02-16 06:10:58 +0000219
220 /// hasChild - Return true if N is any of our children.
221 bool hasChild(const TreePatternNode *N) const {
222 for (unsigned i = 0, e = Children.size(); i != e; ++i)
223 if (Children[i] == N) return true;
224 return false;
225 }
Chris Lattnere67bde52008-01-06 05:36:50 +0000226
Chris Lattner47661322010-02-14 22:22:58 +0000227 const std::vector<std::string> &getPredicateFns() const {return PredicateFns;}
Dan Gohman0540e172008-10-15 06:17:21 +0000228 void clearPredicateFns() { PredicateFns.clear(); }
229 void setPredicateFns(const std::vector<std::string> &Fns) {
230 assert(PredicateFns.empty() && "Overwriting non-empty predicate list!");
231 PredicateFns = Fns;
232 }
233 void addPredicateFn(const std::string &Fn) {
234 assert(!Fn.empty() && "Empty predicate string!");
235 if (std::find(PredicateFns.begin(), PredicateFns.end(), Fn) ==
236 PredicateFns.end())
237 PredicateFns.push_back(Fn);
238 }
Chris Lattner6cefb772008-01-05 22:25:12 +0000239
240 Record *getTransformFn() const { return TransformFn; }
241 void setTransformFn(Record *Fn) { TransformFn = Fn; }
242
Chris Lattnere67bde52008-01-06 05:36:50 +0000243 /// getIntrinsicInfo - If this node corresponds to an intrinsic, return the
244 /// CodeGenIntrinsic information for it, otherwise return a null pointer.
245 const CodeGenIntrinsic *getIntrinsicInfo(const CodeGenDAGPatterns &CDP) const;
Evan Cheng6bd95672008-06-16 20:29:38 +0000246
Chris Lattner47661322010-02-14 22:22:58 +0000247 /// getComplexPatternInfo - If this node corresponds to a ComplexPattern,
248 /// return the ComplexPattern information, otherwise return null.
249 const ComplexPattern *
250 getComplexPatternInfo(const CodeGenDAGPatterns &CGP) const;
251
252 /// NodeHasProperty - Return true if this node has the specified property.
Chris Lattner751d5aa2010-02-14 22:33:49 +0000253 bool NodeHasProperty(SDNP Property, const CodeGenDAGPatterns &CGP) const;
Chris Lattner47661322010-02-14 22:22:58 +0000254
255 /// TreeHasProperty - Return true if any node in this tree has the specified
256 /// property.
Chris Lattner751d5aa2010-02-14 22:33:49 +0000257 bool TreeHasProperty(SDNP Property, const CodeGenDAGPatterns &CGP) const;
Chris Lattner47661322010-02-14 22:22:58 +0000258
Evan Cheng6bd95672008-06-16 20:29:38 +0000259 /// isCommutativeIntrinsic - Return true if the node is an intrinsic which is
260 /// marked isCommutative.
261 bool isCommutativeIntrinsic(const CodeGenDAGPatterns &CDP) const;
Chris Lattnere67bde52008-01-06 05:36:50 +0000262
Daniel Dunbar1a551802009-07-03 00:10:29 +0000263 void print(raw_ostream &OS) const;
Chris Lattner6cefb772008-01-05 22:25:12 +0000264 void dump() const;
265
266public: // Higher level manipulation routines.
267
268 /// clone - Return a new copy of this tree.
269 ///
270 TreePatternNode *clone() const;
Chris Lattner47661322010-02-14 22:22:58 +0000271
272 /// RemoveAllTypes - Recursively strip all the types of this tree.
273 void RemoveAllTypes();
Chris Lattner6cefb772008-01-05 22:25:12 +0000274
275 /// isIsomorphicTo - Return true if this node is recursively isomorphic to
276 /// the specified node. For this comparison, all of the state of the node
277 /// is considered, except for the assigned name. Nodes with differing names
278 /// that are otherwise identical are considered isomorphic.
Scott Michel327d0652008-03-05 17:49:05 +0000279 bool isIsomorphicTo(const TreePatternNode *N,
280 const MultipleUseVarSet &DepVars) const;
Chris Lattner6cefb772008-01-05 22:25:12 +0000281
282 /// SubstituteFormalArguments - Replace the formal arguments in this tree
283 /// with actual values specified by ArgMap.
284 void SubstituteFormalArguments(std::map<std::string,
285 TreePatternNode*> &ArgMap);
286
287 /// InlinePatternFragments - If this pattern refers to any pattern
288 /// fragments, inline them into place, giving us a pattern without any
289 /// PatFrag references.
290 TreePatternNode *InlinePatternFragments(TreePattern &TP);
291
Bob Wilson6c01ca92009-01-05 17:23:09 +0000292 /// ApplyTypeConstraints - Apply all of the type constraints relevant to
Chris Lattner6cefb772008-01-05 22:25:12 +0000293 /// this node and its children in the tree. This returns true if it makes a
294 /// change, false otherwise. If a type contradiction is found, throw an
295 /// exception.
296 bool ApplyTypeConstraints(TreePattern &TP, bool NotRegisters);
297
298 /// UpdateNodeType - Set the node type of N to VT if VT contains
299 /// information. If N already contains a conflicting type, then throw an
300 /// exception. This returns true if any information was updated.
301 ///
302 bool UpdateNodeType(const std::vector<unsigned char> &ExtVTs,
303 TreePattern &TP);
304 bool UpdateNodeType(unsigned char ExtVT, TreePattern &TP) {
305 std::vector<unsigned char> ExtVTs(1, ExtVT);
306 return UpdateNodeType(ExtVTs, TP);
307 }
308
309 /// ContainsUnresolvedType - Return true if this tree contains any
310 /// unresolved types.
311 bool ContainsUnresolvedType() const {
312 if (!hasTypeSet() && !isTypeDynamicallyResolved()) return true;
313 for (unsigned i = 0, e = getNumChildren(); i != e; ++i)
314 if (getChild(i)->ContainsUnresolvedType()) return true;
315 return false;
316 }
317
318 /// canPatternMatch - If it is impossible for this pattern to match on this
319 /// target, fill in Reason and return false. Otherwise, return true.
Dan Gohmanee4fa192008-04-03 00:02:49 +0000320 bool canPatternMatch(std::string &Reason, const CodeGenDAGPatterns &CDP);
Chris Lattner6cefb772008-01-05 22:25:12 +0000321};
322
Chris Lattner383fed92010-02-14 21:10:33 +0000323inline raw_ostream &operator<<(raw_ostream &OS, const TreePatternNode &TPN) {
324 TPN.print(OS);
325 return OS;
326}
327
Chris Lattner6cefb772008-01-05 22:25:12 +0000328
329/// TreePattern - Represent a pattern, used for instructions, pattern
330/// fragments, etc.
331///
332class TreePattern {
333 /// Trees - The list of pattern trees which corresponds to this pattern.
334 /// Note that PatFrag's only have a single tree.
335 ///
336 std::vector<TreePatternNode*> Trees;
337
338 /// TheRecord - The actual TableGen record corresponding to this pattern.
339 ///
340 Record *TheRecord;
341
342 /// Args - This is a list of all of the arguments to this pattern (for
343 /// PatFrag patterns), which are the 'node' markers in this pattern.
344 std::vector<std::string> Args;
345
346 /// CDP - the top-level object coordinating this madness.
347 ///
Chris Lattnerfe718932008-01-06 01:10:31 +0000348 CodeGenDAGPatterns &CDP;
Chris Lattner6cefb772008-01-05 22:25:12 +0000349
350 /// isInputPattern - True if this is an input pattern, something to match.
351 /// False if this is an output pattern, something to emit.
352 bool isInputPattern;
353public:
354
355 /// TreePattern constructor - Parse the specified DagInits into the
356 /// current record.
357 TreePattern(Record *TheRec, ListInit *RawPat, bool isInput,
Chris Lattnerfe718932008-01-06 01:10:31 +0000358 CodeGenDAGPatterns &ise);
Chris Lattner6cefb772008-01-05 22:25:12 +0000359 TreePattern(Record *TheRec, DagInit *Pat, bool isInput,
Chris Lattnerfe718932008-01-06 01:10:31 +0000360 CodeGenDAGPatterns &ise);
Chris Lattner6cefb772008-01-05 22:25:12 +0000361 TreePattern(Record *TheRec, TreePatternNode *Pat, bool isInput,
Chris Lattnerfe718932008-01-06 01:10:31 +0000362 CodeGenDAGPatterns &ise);
Chris Lattner6cefb772008-01-05 22:25:12 +0000363
364 /// getTrees - Return the tree patterns which corresponds to this pattern.
365 ///
366 const std::vector<TreePatternNode*> &getTrees() const { return Trees; }
367 unsigned getNumTrees() const { return Trees.size(); }
368 TreePatternNode *getTree(unsigned i) const { return Trees[i]; }
369 TreePatternNode *getOnlyTree() const {
370 assert(Trees.size() == 1 && "Doesn't have exactly one pattern!");
371 return Trees[0];
372 }
373
374 /// getRecord - Return the actual TableGen record corresponding to this
375 /// pattern.
376 ///
377 Record *getRecord() const { return TheRecord; }
378
379 unsigned getNumArgs() const { return Args.size(); }
380 const std::string &getArgName(unsigned i) const {
381 assert(i < Args.size() && "Argument reference out of range!");
382 return Args[i];
383 }
384 std::vector<std::string> &getArgList() { return Args; }
385
Chris Lattnerfe718932008-01-06 01:10:31 +0000386 CodeGenDAGPatterns &getDAGPatterns() const { return CDP; }
Chris Lattner6cefb772008-01-05 22:25:12 +0000387
388 /// InlinePatternFragments - If this pattern refers to any pattern
389 /// fragments, inline them into place, giving us a pattern without any
390 /// PatFrag references.
391 void InlinePatternFragments() {
392 for (unsigned i = 0, e = Trees.size(); i != e; ++i)
393 Trees[i] = Trees[i]->InlinePatternFragments(*this);
394 }
395
396 /// InferAllTypes - Infer/propagate as many types throughout the expression
Jim Grosbachda4231f2009-03-26 16:17:51 +0000397 /// patterns as possible. Return true if all types are inferred, false
Chris Lattner6cefb772008-01-05 22:25:12 +0000398 /// otherwise. Throw an exception if a type contradiction is found.
399 bool InferAllTypes();
400
401 /// error - Throw an exception, prefixing it with information about this
402 /// pattern.
403 void error(const std::string &Msg) const;
404
Daniel Dunbar1a551802009-07-03 00:10:29 +0000405 void print(raw_ostream &OS) const;
Chris Lattner6cefb772008-01-05 22:25:12 +0000406 void dump() const;
407
408private:
409 TreePatternNode *ParseTreePattern(DagInit *DI);
410};
411
412/// DAGDefaultOperand - One of these is created for each PredicateOperand
413/// or OptionalDefOperand that has a set ExecuteAlways / DefaultOps field.
414struct DAGDefaultOperand {
415 std::vector<TreePatternNode*> DefaultOps;
416};
417
418class DAGInstruction {
419 TreePattern *Pattern;
420 std::vector<Record*> Results;
421 std::vector<Record*> Operands;
422 std::vector<Record*> ImpResults;
423 std::vector<Record*> ImpOperands;
424 TreePatternNode *ResultPattern;
425public:
426 DAGInstruction(TreePattern *TP,
427 const std::vector<Record*> &results,
428 const std::vector<Record*> &operands,
429 const std::vector<Record*> &impresults,
430 const std::vector<Record*> &impoperands)
431 : Pattern(TP), Results(results), Operands(operands),
432 ImpResults(impresults), ImpOperands(impoperands),
433 ResultPattern(0) {}
434
Chris Lattnerf1ab4f12008-01-06 01:52:22 +0000435 const TreePattern *getPattern() const { return Pattern; }
Chris Lattner6cefb772008-01-05 22:25:12 +0000436 unsigned getNumResults() const { return Results.size(); }
437 unsigned getNumOperands() const { return Operands.size(); }
438 unsigned getNumImpResults() const { return ImpResults.size(); }
439 unsigned getNumImpOperands() const { return ImpOperands.size(); }
440 const std::vector<Record*>& getImpResults() const { return ImpResults; }
441
442 void setResultPattern(TreePatternNode *R) { ResultPattern = R; }
443
444 Record *getResult(unsigned RN) const {
445 assert(RN < Results.size());
446 return Results[RN];
447 }
448
449 Record *getOperand(unsigned ON) const {
450 assert(ON < Operands.size());
451 return Operands[ON];
452 }
453
454 Record *getImpResult(unsigned RN) const {
455 assert(RN < ImpResults.size());
456 return ImpResults[RN];
457 }
458
459 Record *getImpOperand(unsigned ON) const {
460 assert(ON < ImpOperands.size());
461 return ImpOperands[ON];
462 }
463
464 TreePatternNode *getResultPattern() const { return ResultPattern; }
465};
466
Chris Lattnerfe718932008-01-06 01:10:31 +0000467/// PatternToMatch - Used by CodeGenDAGPatterns to keep tab of patterns
Chris Lattner6cefb772008-01-05 22:25:12 +0000468/// processed to produce isel.
469struct PatternToMatch {
470 PatternToMatch(ListInit *preds,
471 TreePatternNode *src, TreePatternNode *dst,
472 const std::vector<Record*> &dstregs,
473 unsigned complexity):
474 Predicates(preds), SrcPattern(src), DstPattern(dst), Dstregs(dstregs),
Daniel Dunbare8542732009-12-09 02:58:09 +0000475 AddedComplexity(complexity) {}
Chris Lattner6cefb772008-01-05 22:25:12 +0000476
477 ListInit *Predicates; // Top level predicate conditions to match.
478 TreePatternNode *SrcPattern; // Source pattern to match.
479 TreePatternNode *DstPattern; // Resulting pattern.
480 std::vector<Record*> Dstregs; // Physical register defs being matched.
481 unsigned AddedComplexity; // Add to matching pattern complexity.
482
483 ListInit *getPredicates() const { return Predicates; }
484 TreePatternNode *getSrcPattern() const { return SrcPattern; }
485 TreePatternNode *getDstPattern() const { return DstPattern; }
486 const std::vector<Record*> &getDstRegs() const { return Dstregs; }
487 unsigned getAddedComplexity() const { return AddedComplexity; }
Dan Gohman22bb3112008-08-22 00:20:26 +0000488
489 std::string getPredicateCheck() const;
Chris Lattner6cefb772008-01-05 22:25:12 +0000490};
491
Daniel Dunbar6f5cc822009-08-23 09:47:37 +0000492// Deterministic comparison of Record*.
493struct RecordPtrCmp {
494 bool operator()(const Record *LHS, const Record *RHS) const;
495};
Chris Lattner6cefb772008-01-05 22:25:12 +0000496
Chris Lattnerfe718932008-01-06 01:10:31 +0000497class CodeGenDAGPatterns {
Chris Lattner6cefb772008-01-05 22:25:12 +0000498 RecordKeeper &Records;
499 CodeGenTarget Target;
500 std::vector<CodeGenIntrinsic> Intrinsics;
Dale Johannesen49de9822009-02-05 01:49:45 +0000501 std::vector<CodeGenIntrinsic> TgtIntrinsics;
Chris Lattner6cefb772008-01-05 22:25:12 +0000502
Daniel Dunbar6f5cc822009-08-23 09:47:37 +0000503 std::map<Record*, SDNodeInfo, RecordPtrCmp> SDNodes;
504 std::map<Record*, std::pair<Record*, std::string>, RecordPtrCmp> SDNodeXForms;
505 std::map<Record*, ComplexPattern, RecordPtrCmp> ComplexPatterns;
506 std::map<Record*, TreePattern*, RecordPtrCmp> PatternFragments;
507 std::map<Record*, DAGDefaultOperand, RecordPtrCmp> DefaultOperands;
508 std::map<Record*, DAGInstruction, RecordPtrCmp> Instructions;
Chris Lattner6cefb772008-01-05 22:25:12 +0000509
510 // Specific SDNode definitions:
511 Record *intrinsic_void_sdnode;
512 Record *intrinsic_w_chain_sdnode, *intrinsic_wo_chain_sdnode;
513
514 /// PatternsToMatch - All of the things we are matching on the DAG. The first
515 /// value is the pattern to match, the second pattern is the result to
516 /// emit.
517 std::vector<PatternToMatch> PatternsToMatch;
518public:
Chris Lattnerfe718932008-01-06 01:10:31 +0000519 CodeGenDAGPatterns(RecordKeeper &R);
520 ~CodeGenDAGPatterns();
Chris Lattner6cefb772008-01-05 22:25:12 +0000521
Dan Gohmanee4fa192008-04-03 00:02:49 +0000522 CodeGenTarget &getTargetInfo() { return Target; }
Chris Lattner6cefb772008-01-05 22:25:12 +0000523 const CodeGenTarget &getTargetInfo() const { return Target; }
524
525 Record *getSDNodeNamed(const std::string &Name) const;
526
527 const SDNodeInfo &getSDNodeInfo(Record *R) const {
528 assert(SDNodes.count(R) && "Unknown node!");
529 return SDNodes.find(R)->second;
530 }
531
Chris Lattner443e3f92008-01-05 22:54:53 +0000532 // Node transformation lookups.
533 typedef std::pair<Record*, std::string> NodeXForm;
534 const NodeXForm &getSDNodeTransform(Record *R) const {
Chris Lattner6cefb772008-01-05 22:25:12 +0000535 assert(SDNodeXForms.count(R) && "Invalid transform!");
536 return SDNodeXForms.find(R)->second;
537 }
538
Benjamin Kramer5b9e7ef2009-08-23 10:39:21 +0000539 typedef std::map<Record*, NodeXForm, RecordPtrCmp>::const_iterator
540 nx_iterator;
Chris Lattner443e3f92008-01-05 22:54:53 +0000541 nx_iterator nx_begin() const { return SDNodeXForms.begin(); }
542 nx_iterator nx_end() const { return SDNodeXForms.end(); }
543
544
Chris Lattner6cefb772008-01-05 22:25:12 +0000545 const ComplexPattern &getComplexPattern(Record *R) const {
546 assert(ComplexPatterns.count(R) && "Unknown addressing mode!");
547 return ComplexPatterns.find(R)->second;
548 }
549
550 const CodeGenIntrinsic &getIntrinsic(Record *R) const {
551 for (unsigned i = 0, e = Intrinsics.size(); i != e; ++i)
552 if (Intrinsics[i].TheDef == R) return Intrinsics[i];
Dale Johannesen49de9822009-02-05 01:49:45 +0000553 for (unsigned i = 0, e = TgtIntrinsics.size(); i != e; ++i)
554 if (TgtIntrinsics[i].TheDef == R) return TgtIntrinsics[i];
Chris Lattner6cefb772008-01-05 22:25:12 +0000555 assert(0 && "Unknown intrinsic!");
556 abort();
557 }
558
559 const CodeGenIntrinsic &getIntrinsicInfo(unsigned IID) const {
Dale Johannesen49de9822009-02-05 01:49:45 +0000560 if (IID-1 < Intrinsics.size())
561 return Intrinsics[IID-1];
562 if (IID-Intrinsics.size()-1 < TgtIntrinsics.size())
563 return TgtIntrinsics[IID-Intrinsics.size()-1];
564 assert(0 && "Bad intrinsic ID!");
Evan Cheng32c1a4c2009-02-09 03:07:24 +0000565 abort();
Chris Lattner6cefb772008-01-05 22:25:12 +0000566 }
567
568 unsigned getIntrinsicID(Record *R) const {
569 for (unsigned i = 0, e = Intrinsics.size(); i != e; ++i)
570 if (Intrinsics[i].TheDef == R) return i;
Dale Johannesen49de9822009-02-05 01:49:45 +0000571 for (unsigned i = 0, e = TgtIntrinsics.size(); i != e; ++i)
572 if (TgtIntrinsics[i].TheDef == R) return i + Intrinsics.size();
Chris Lattner6cefb772008-01-05 22:25:12 +0000573 assert(0 && "Unknown intrinsic!");
574 abort();
575 }
576
577 const DAGDefaultOperand &getDefaultOperand(Record *R) {
578 assert(DefaultOperands.count(R) &&"Isn't an analyzed default operand!");
579 return DefaultOperands.find(R)->second;
580 }
581
582 // Pattern Fragment information.
583 TreePattern *getPatternFragment(Record *R) const {
584 assert(PatternFragments.count(R) && "Invalid pattern fragment request!");
585 return PatternFragments.find(R)->second;
586 }
Benjamin Kramer5b9e7ef2009-08-23 10:39:21 +0000587 typedef std::map<Record*, TreePattern*, RecordPtrCmp>::const_iterator
588 pf_iterator;
Chris Lattner6cefb772008-01-05 22:25:12 +0000589 pf_iterator pf_begin() const { return PatternFragments.begin(); }
590 pf_iterator pf_end() const { return PatternFragments.end(); }
591
592 // Patterns to match information.
Chris Lattner60d81392008-01-05 22:30:17 +0000593 typedef std::vector<PatternToMatch>::const_iterator ptm_iterator;
594 ptm_iterator ptm_begin() const { return PatternsToMatch.begin(); }
595 ptm_iterator ptm_end() const { return PatternsToMatch.end(); }
Chris Lattner6cefb772008-01-05 22:25:12 +0000596
597
598
599 const DAGInstruction &getInstruction(Record *R) const {
600 assert(Instructions.count(R) && "Unknown instruction!");
601 return Instructions.find(R)->second;
602 }
603
604 Record *get_intrinsic_void_sdnode() const {
605 return intrinsic_void_sdnode;
606 }
607 Record *get_intrinsic_w_chain_sdnode() const {
608 return intrinsic_w_chain_sdnode;
609 }
610 Record *get_intrinsic_wo_chain_sdnode() const {
611 return intrinsic_wo_chain_sdnode;
612 }
613
Jakob Stoklund Olesen11ee5082009-10-15 18:50:03 +0000614 bool hasTargetIntrinsics() { return !TgtIntrinsics.empty(); }
615
Chris Lattner6cefb772008-01-05 22:25:12 +0000616private:
617 void ParseNodeInfo();
Chris Lattner443e3f92008-01-05 22:54:53 +0000618 void ParseNodeTransforms();
Chris Lattner6cefb772008-01-05 22:25:12 +0000619 void ParseComplexPatterns();
Chris Lattnerdc32f982008-01-05 22:43:57 +0000620 void ParsePatternFragments();
Chris Lattner6cefb772008-01-05 22:25:12 +0000621 void ParseDefaultOperands();
622 void ParseInstructions();
623 void ParsePatterns();
Dan Gohmanee4fa192008-04-03 00:02:49 +0000624 void InferInstructionFlags();
Chris Lattner6cefb772008-01-05 22:25:12 +0000625 void GenerateVariants();
626
627 void FindPatternInputsAndOutputs(TreePattern *I, TreePatternNode *Pat,
628 std::map<std::string,
629 TreePatternNode*> &InstInputs,
630 std::map<std::string,
631 TreePatternNode*> &InstResults,
632 std::vector<Record*> &InstImpInputs,
633 std::vector<Record*> &InstImpResults);
634};
635} // end namespace llvm
636
637#endif