blob: 7c2791d4406e8b8d040f307d64c022e9b6ab70fd [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.
Owen Andersone50ed302009-08-10 22:56:29 +000038namespace EEVT {
Chris Lattner6cefb772008-01-05 22:25:12 +000039 enum DAGISelGenValueType {
Owen Anderson825b72b2009-08-11 20:47:22 +000040 isFP = MVT::LAST_VALUETYPE,
Chris Lattner6cefb772008-01-05 22:25:12 +000041 isInt,
42 isUnknown
43 };
Duncan Sands83ec4b62008-06-06 12:08:01 +000044
Chris Lattner6cefb772008-01-05 22:25:12 +000045 /// isExtIntegerVT - Return true if the specified extended value type vector
46 /// contains isInt or an integer value type.
47 bool isExtIntegerInVTs(const std::vector<unsigned char> &EVTs);
48
49 /// isExtFloatingPointVT - Return true if the specified extended value type
50 /// vector contains isFP or a FP value type.
51 bool isExtFloatingPointInVTs(const std::vector<unsigned char> &EVTs);
52}
53
Scott Michel327d0652008-03-05 17:49:05 +000054/// Set type used to track multiply used variables in patterns
55typedef std::set<std::string> MultipleUseVarSet;
56
Chris Lattner6cefb772008-01-05 22:25:12 +000057/// SDTypeConstraint - This is a discriminated union of constraints,
58/// corresponding to the SDTypeConstraint tablegen class in Target.td.
59struct SDTypeConstraint {
60 SDTypeConstraint(Record *R);
61
62 unsigned OperandNo; // The operand # this constraint applies to.
63 enum {
64 SDTCisVT, SDTCisPtrTy, SDTCisInt, SDTCisFP, SDTCisSameAs,
Nate Begeman9008ca62009-04-27 18:41:29 +000065 SDTCisVTSmallerThanOp, SDTCisOpSmallerThanOp, SDTCisEltOfVec
Chris Lattner6cefb772008-01-05 22:25:12 +000066 } ConstraintType;
67
68 union { // The discriminated union.
69 struct {
Duncan Sands83ec4b62008-06-06 12:08:01 +000070 unsigned char VT;
Chris Lattner6cefb772008-01-05 22:25:12 +000071 } SDTCisVT_Info;
72 struct {
73 unsigned OtherOperandNum;
74 } SDTCisSameAs_Info;
75 struct {
76 unsigned OtherOperandNum;
77 } SDTCisVTSmallerThanOp_Info;
78 struct {
79 unsigned BigOperandNum;
80 } SDTCisOpSmallerThanOp_Info;
81 struct {
82 unsigned OtherOperandNum;
Nate Begemanb5af3342008-02-09 01:37:05 +000083 } SDTCisEltOfVec_Info;
Chris Lattner6cefb772008-01-05 22:25:12 +000084 } x;
85
86 /// ApplyTypeConstraint - Given a node in a pattern, apply this type
87 /// constraint to the nodes operands. This returns true if it makes a
88 /// change, false otherwise. If a type contradiction is found, throw an
89 /// exception.
90 bool ApplyTypeConstraint(TreePatternNode *N, const SDNodeInfo &NodeInfo,
91 TreePattern &TP) const;
92
93 /// getOperandNum - Return the node corresponding to operand #OpNo in tree
94 /// N, which has NumResults results.
95 TreePatternNode *getOperandNum(unsigned OpNo, TreePatternNode *N,
96 unsigned NumResults) const;
97};
98
99/// SDNodeInfo - One of these records is created for each SDNode instance in
100/// the target .td file. This represents the various dag nodes we will be
101/// processing.
102class SDNodeInfo {
103 Record *Def;
104 std::string EnumName;
105 std::string SDClassName;
106 unsigned Properties;
107 unsigned NumResults;
108 int NumOperands;
109 std::vector<SDTypeConstraint> TypeConstraints;
110public:
111 SDNodeInfo(Record *R); // Parse the specified record.
112
113 unsigned getNumResults() const { return NumResults; }
114 int getNumOperands() const { return NumOperands; }
115 Record *getRecord() const { return Def; }
116 const std::string &getEnumName() const { return EnumName; }
117 const std::string &getSDClassName() const { return SDClassName; }
118
119 const std::vector<SDTypeConstraint> &getTypeConstraints() const {
120 return TypeConstraints;
121 }
122
123 /// hasProperty - Return true if this node has the specified property.
124 ///
125 bool hasProperty(enum SDNP Prop) const { return Properties & (1 << Prop); }
126
127 /// ApplyTypeConstraints - Given a node in a pattern, apply the type
128 /// constraints for this node to the operands of the node. This returns
129 /// true if it makes a change, false otherwise. If a type contradiction is
130 /// found, throw an exception.
131 bool ApplyTypeConstraints(TreePatternNode *N, TreePattern &TP) const {
132 bool MadeChange = false;
133 for (unsigned i = 0, e = TypeConstraints.size(); i != e; ++i)
134 MadeChange |= TypeConstraints[i].ApplyTypeConstraint(N, *this, TP);
135 return MadeChange;
136 }
137};
138
139/// FIXME: TreePatternNode's can be shared in some cases (due to dag-shaped
140/// patterns), and as such should be ref counted. We currently just leak all
141/// TreePatternNode objects!
142class TreePatternNode {
Owen Andersone50ed302009-08-10 22:56:29 +0000143 /// The inferred type for this node, or EEVT::isUnknown if it hasn't
Dan Gohmane1ca7c42009-03-31 16:46:45 +0000144 /// been determined yet. This is a std::vector because during inference
145 /// there may be multiple possible types.
Chris Lattner6cefb772008-01-05 22:25:12 +0000146 std::vector<unsigned char> Types;
147
148 /// Operator - The Record for the operator if this is an interior node (not
149 /// a leaf).
150 Record *Operator;
151
152 /// Val - The init value (e.g. the "GPRC" record, or "7") for a leaf.
153 ///
154 Init *Val;
155
156 /// Name - The name given to this node with the :$foo notation.
157 ///
158 std::string Name;
159
Dan Gohman0540e172008-10-15 06:17:21 +0000160 /// PredicateFns - The predicate functions to execute on this node to check
161 /// for a match. If this list is empty, no predicate is involved.
162 std::vector<std::string> PredicateFns;
Chris Lattner6cefb772008-01-05 22:25:12 +0000163
164 /// TransformFn - The transformation function to execute on this node before
165 /// it can be substituted into the resulting instruction on a pattern match.
166 Record *TransformFn;
167
168 std::vector<TreePatternNode*> Children;
169public:
170 TreePatternNode(Record *Op, const std::vector<TreePatternNode*> &Ch)
171 : Types(), Operator(Op), Val(0), TransformFn(0),
Owen Andersone50ed302009-08-10 22:56:29 +0000172 Children(Ch) { Types.push_back(EEVT::isUnknown); }
Chris Lattner6cefb772008-01-05 22:25:12 +0000173 TreePatternNode(Init *val) // leaf ctor
174 : Types(), Operator(0), Val(val), TransformFn(0) {
Owen Andersone50ed302009-08-10 22:56:29 +0000175 Types.push_back(EEVT::isUnknown);
Chris Lattner6cefb772008-01-05 22:25:12 +0000176 }
177 ~TreePatternNode();
178
179 const std::string &getName() const { return Name; }
180 void setName(const std::string &N) { Name = N; }
181
182 bool isLeaf() const { return Val != 0; }
183 bool hasTypeSet() const {
Owen Anderson825b72b2009-08-11 20:47:22 +0000184 return (Types[0] < MVT::LAST_VALUETYPE) || (Types[0] == MVT::iPTR) ||
185 (Types[0] == MVT::iPTRAny);
Chris Lattner6cefb772008-01-05 22:25:12 +0000186 }
187 bool isTypeCompletelyUnknown() const {
Owen Andersone50ed302009-08-10 22:56:29 +0000188 return Types[0] == EEVT::isUnknown;
Chris Lattner6cefb772008-01-05 22:25:12 +0000189 }
190 bool isTypeDynamicallyResolved() const {
Owen Anderson825b72b2009-08-11 20:47:22 +0000191 return (Types[0] == MVT::iPTR) || (Types[0] == MVT::iPTRAny);
Chris Lattner6cefb772008-01-05 22:25:12 +0000192 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000193 MVT::SimpleValueType getTypeNum(unsigned Num) const {
Chris Lattner6cefb772008-01-05 22:25:12 +0000194 assert(hasTypeSet() && "Doesn't have a type yet!");
195 assert(Types.size() > Num && "Type num out of range!");
Owen Anderson825b72b2009-08-11 20:47:22 +0000196 return (MVT::SimpleValueType)Types[Num];
Chris Lattner6cefb772008-01-05 22:25:12 +0000197 }
198 unsigned char getExtTypeNum(unsigned Num) const {
199 assert(Types.size() > Num && "Extended type num out of range!");
200 return Types[Num];
201 }
202 const std::vector<unsigned char> &getExtTypes() const { return Types; }
203 void setTypes(const std::vector<unsigned char> &T) { Types = T; }
Owen Andersone50ed302009-08-10 22:56:29 +0000204 void removeTypes() { Types = std::vector<unsigned char>(1, EEVT::isUnknown); }
Chris Lattner6cefb772008-01-05 22:25:12 +0000205
206 Init *getLeafValue() const { assert(isLeaf()); return Val; }
207 Record *getOperator() const { assert(!isLeaf()); return Operator; }
208
209 unsigned getNumChildren() const { return Children.size(); }
210 TreePatternNode *getChild(unsigned N) const { return Children[N]; }
211 void setChild(unsigned i, TreePatternNode *N) {
212 Children[i] = N;
213 }
Chris Lattnere67bde52008-01-06 05:36:50 +0000214
Dan Gohman0540e172008-10-15 06:17:21 +0000215 const std::vector<std::string> &getPredicateFns() const { return PredicateFns; }
216 void clearPredicateFns() { PredicateFns.clear(); }
217 void setPredicateFns(const std::vector<std::string> &Fns) {
218 assert(PredicateFns.empty() && "Overwriting non-empty predicate list!");
219 PredicateFns = Fns;
220 }
221 void addPredicateFn(const std::string &Fn) {
222 assert(!Fn.empty() && "Empty predicate string!");
223 if (std::find(PredicateFns.begin(), PredicateFns.end(), Fn) ==
224 PredicateFns.end())
225 PredicateFns.push_back(Fn);
226 }
Chris Lattner6cefb772008-01-05 22:25:12 +0000227
228 Record *getTransformFn() const { return TransformFn; }
229 void setTransformFn(Record *Fn) { TransformFn = Fn; }
230
Chris Lattnere67bde52008-01-06 05:36:50 +0000231 /// getIntrinsicInfo - If this node corresponds to an intrinsic, return the
232 /// CodeGenIntrinsic information for it, otherwise return a null pointer.
233 const CodeGenIntrinsic *getIntrinsicInfo(const CodeGenDAGPatterns &CDP) const;
Evan Cheng6bd95672008-06-16 20:29:38 +0000234
235 /// isCommutativeIntrinsic - Return true if the node is an intrinsic which is
236 /// marked isCommutative.
237 bool isCommutativeIntrinsic(const CodeGenDAGPatterns &CDP) const;
Chris Lattnere67bde52008-01-06 05:36:50 +0000238
Daniel Dunbar1a551802009-07-03 00:10:29 +0000239 void print(raw_ostream &OS) const;
Chris Lattner6cefb772008-01-05 22:25:12 +0000240 void dump() const;
241
242public: // Higher level manipulation routines.
243
244 /// clone - Return a new copy of this tree.
245 ///
246 TreePatternNode *clone() const;
247
248 /// isIsomorphicTo - Return true if this node is recursively isomorphic to
249 /// the specified node. For this comparison, all of the state of the node
250 /// is considered, except for the assigned name. Nodes with differing names
251 /// that are otherwise identical are considered isomorphic.
Scott Michel327d0652008-03-05 17:49:05 +0000252 bool isIsomorphicTo(const TreePatternNode *N,
253 const MultipleUseVarSet &DepVars) const;
Chris Lattner6cefb772008-01-05 22:25:12 +0000254
255 /// SubstituteFormalArguments - Replace the formal arguments in this tree
256 /// with actual values specified by ArgMap.
257 void SubstituteFormalArguments(std::map<std::string,
258 TreePatternNode*> &ArgMap);
259
260 /// InlinePatternFragments - If this pattern refers to any pattern
261 /// fragments, inline them into place, giving us a pattern without any
262 /// PatFrag references.
263 TreePatternNode *InlinePatternFragments(TreePattern &TP);
264
Bob Wilson6c01ca92009-01-05 17:23:09 +0000265 /// ApplyTypeConstraints - Apply all of the type constraints relevant to
Chris Lattner6cefb772008-01-05 22:25:12 +0000266 /// this node and its children in the tree. This returns true if it makes a
267 /// change, false otherwise. If a type contradiction is found, throw an
268 /// exception.
269 bool ApplyTypeConstraints(TreePattern &TP, bool NotRegisters);
270
271 /// UpdateNodeType - Set the node type of N to VT if VT contains
272 /// information. If N already contains a conflicting type, then throw an
273 /// exception. This returns true if any information was updated.
274 ///
275 bool UpdateNodeType(const std::vector<unsigned char> &ExtVTs,
276 TreePattern &TP);
277 bool UpdateNodeType(unsigned char ExtVT, TreePattern &TP) {
278 std::vector<unsigned char> ExtVTs(1, ExtVT);
279 return UpdateNodeType(ExtVTs, TP);
280 }
281
282 /// ContainsUnresolvedType - Return true if this tree contains any
283 /// unresolved types.
284 bool ContainsUnresolvedType() const {
285 if (!hasTypeSet() && !isTypeDynamicallyResolved()) return true;
286 for (unsigned i = 0, e = getNumChildren(); i != e; ++i)
287 if (getChild(i)->ContainsUnresolvedType()) return true;
288 return false;
289 }
290
291 /// canPatternMatch - If it is impossible for this pattern to match on this
292 /// target, fill in Reason and return false. Otherwise, return true.
Dan Gohmanee4fa192008-04-03 00:02:49 +0000293 bool canPatternMatch(std::string &Reason, const CodeGenDAGPatterns &CDP);
Chris Lattner6cefb772008-01-05 22:25:12 +0000294};
295
296
297/// TreePattern - Represent a pattern, used for instructions, pattern
298/// fragments, etc.
299///
300class TreePattern {
301 /// Trees - The list of pattern trees which corresponds to this pattern.
302 /// Note that PatFrag's only have a single tree.
303 ///
304 std::vector<TreePatternNode*> Trees;
305
306 /// TheRecord - The actual TableGen record corresponding to this pattern.
307 ///
308 Record *TheRecord;
309
310 /// Args - This is a list of all of the arguments to this pattern (for
311 /// PatFrag patterns), which are the 'node' markers in this pattern.
312 std::vector<std::string> Args;
313
314 /// CDP - the top-level object coordinating this madness.
315 ///
Chris Lattnerfe718932008-01-06 01:10:31 +0000316 CodeGenDAGPatterns &CDP;
Chris Lattner6cefb772008-01-05 22:25:12 +0000317
318 /// isInputPattern - True if this is an input pattern, something to match.
319 /// False if this is an output pattern, something to emit.
320 bool isInputPattern;
321public:
322
323 /// TreePattern constructor - Parse the specified DagInits into the
324 /// current record.
325 TreePattern(Record *TheRec, ListInit *RawPat, bool isInput,
Chris Lattnerfe718932008-01-06 01:10:31 +0000326 CodeGenDAGPatterns &ise);
Chris Lattner6cefb772008-01-05 22:25:12 +0000327 TreePattern(Record *TheRec, DagInit *Pat, bool isInput,
Chris Lattnerfe718932008-01-06 01:10:31 +0000328 CodeGenDAGPatterns &ise);
Chris Lattner6cefb772008-01-05 22:25:12 +0000329 TreePattern(Record *TheRec, TreePatternNode *Pat, bool isInput,
Chris Lattnerfe718932008-01-06 01:10:31 +0000330 CodeGenDAGPatterns &ise);
Chris Lattner6cefb772008-01-05 22:25:12 +0000331
332 /// getTrees - Return the tree patterns which corresponds to this pattern.
333 ///
334 const std::vector<TreePatternNode*> &getTrees() const { return Trees; }
335 unsigned getNumTrees() const { return Trees.size(); }
336 TreePatternNode *getTree(unsigned i) const { return Trees[i]; }
337 TreePatternNode *getOnlyTree() const {
338 assert(Trees.size() == 1 && "Doesn't have exactly one pattern!");
339 return Trees[0];
340 }
341
342 /// getRecord - Return the actual TableGen record corresponding to this
343 /// pattern.
344 ///
345 Record *getRecord() const { return TheRecord; }
346
347 unsigned getNumArgs() const { return Args.size(); }
348 const std::string &getArgName(unsigned i) const {
349 assert(i < Args.size() && "Argument reference out of range!");
350 return Args[i];
351 }
352 std::vector<std::string> &getArgList() { return Args; }
353
Chris Lattnerfe718932008-01-06 01:10:31 +0000354 CodeGenDAGPatterns &getDAGPatterns() const { return CDP; }
Chris Lattner6cefb772008-01-05 22:25:12 +0000355
356 /// InlinePatternFragments - If this pattern refers to any pattern
357 /// fragments, inline them into place, giving us a pattern without any
358 /// PatFrag references.
359 void InlinePatternFragments() {
360 for (unsigned i = 0, e = Trees.size(); i != e; ++i)
361 Trees[i] = Trees[i]->InlinePatternFragments(*this);
362 }
363
364 /// InferAllTypes - Infer/propagate as many types throughout the expression
Jim Grosbachda4231f2009-03-26 16:17:51 +0000365 /// patterns as possible. Return true if all types are inferred, false
Chris Lattner6cefb772008-01-05 22:25:12 +0000366 /// otherwise. Throw an exception if a type contradiction is found.
367 bool InferAllTypes();
368
369 /// error - Throw an exception, prefixing it with information about this
370 /// pattern.
371 void error(const std::string &Msg) const;
372
Daniel Dunbar1a551802009-07-03 00:10:29 +0000373 void print(raw_ostream &OS) const;
Chris Lattner6cefb772008-01-05 22:25:12 +0000374 void dump() const;
375
376private:
377 TreePatternNode *ParseTreePattern(DagInit *DI);
378};
379
380/// DAGDefaultOperand - One of these is created for each PredicateOperand
381/// or OptionalDefOperand that has a set ExecuteAlways / DefaultOps field.
382struct DAGDefaultOperand {
383 std::vector<TreePatternNode*> DefaultOps;
384};
385
386class DAGInstruction {
387 TreePattern *Pattern;
388 std::vector<Record*> Results;
389 std::vector<Record*> Operands;
390 std::vector<Record*> ImpResults;
391 std::vector<Record*> ImpOperands;
392 TreePatternNode *ResultPattern;
393public:
394 DAGInstruction(TreePattern *TP,
395 const std::vector<Record*> &results,
396 const std::vector<Record*> &operands,
397 const std::vector<Record*> &impresults,
398 const std::vector<Record*> &impoperands)
399 : Pattern(TP), Results(results), Operands(operands),
400 ImpResults(impresults), ImpOperands(impoperands),
401 ResultPattern(0) {}
402
Chris Lattnerf1ab4f12008-01-06 01:52:22 +0000403 const TreePattern *getPattern() const { return Pattern; }
Chris Lattner6cefb772008-01-05 22:25:12 +0000404 unsigned getNumResults() const { return Results.size(); }
405 unsigned getNumOperands() const { return Operands.size(); }
406 unsigned getNumImpResults() const { return ImpResults.size(); }
407 unsigned getNumImpOperands() const { return ImpOperands.size(); }
408 const std::vector<Record*>& getImpResults() const { return ImpResults; }
409
410 void setResultPattern(TreePatternNode *R) { ResultPattern = R; }
411
412 Record *getResult(unsigned RN) const {
413 assert(RN < Results.size());
414 return Results[RN];
415 }
416
417 Record *getOperand(unsigned ON) const {
418 assert(ON < Operands.size());
419 return Operands[ON];
420 }
421
422 Record *getImpResult(unsigned RN) const {
423 assert(RN < ImpResults.size());
424 return ImpResults[RN];
425 }
426
427 Record *getImpOperand(unsigned ON) const {
428 assert(ON < ImpOperands.size());
429 return ImpOperands[ON];
430 }
431
432 TreePatternNode *getResultPattern() const { return ResultPattern; }
433};
434
Chris Lattnerfe718932008-01-06 01:10:31 +0000435/// PatternToMatch - Used by CodeGenDAGPatterns to keep tab of patterns
Chris Lattner6cefb772008-01-05 22:25:12 +0000436/// processed to produce isel.
437struct PatternToMatch {
438 PatternToMatch(ListInit *preds,
439 TreePatternNode *src, TreePatternNode *dst,
440 const std::vector<Record*> &dstregs,
441 unsigned complexity):
442 Predicates(preds), SrcPattern(src), DstPattern(dst), Dstregs(dstregs),
443 AddedComplexity(complexity) {};
444
445 ListInit *Predicates; // Top level predicate conditions to match.
446 TreePatternNode *SrcPattern; // Source pattern to match.
447 TreePatternNode *DstPattern; // Resulting pattern.
448 std::vector<Record*> Dstregs; // Physical register defs being matched.
449 unsigned AddedComplexity; // Add to matching pattern complexity.
450
451 ListInit *getPredicates() const { return Predicates; }
452 TreePatternNode *getSrcPattern() const { return SrcPattern; }
453 TreePatternNode *getDstPattern() const { return DstPattern; }
454 const std::vector<Record*> &getDstRegs() const { return Dstregs; }
455 unsigned getAddedComplexity() const { return AddedComplexity; }
Dan Gohman22bb3112008-08-22 00:20:26 +0000456
457 std::string getPredicateCheck() const;
Chris Lattner6cefb772008-01-05 22:25:12 +0000458};
459
460
Chris Lattnerfe718932008-01-06 01:10:31 +0000461class CodeGenDAGPatterns {
Chris Lattner6cefb772008-01-05 22:25:12 +0000462 RecordKeeper &Records;
463 CodeGenTarget Target;
464 std::vector<CodeGenIntrinsic> Intrinsics;
Dale Johannesen49de9822009-02-05 01:49:45 +0000465 std::vector<CodeGenIntrinsic> TgtIntrinsics;
Chris Lattner6cefb772008-01-05 22:25:12 +0000466
467 std::map<Record*, SDNodeInfo> SDNodes;
468 std::map<Record*, std::pair<Record*, std::string> > SDNodeXForms;
469 std::map<Record*, ComplexPattern> ComplexPatterns;
470 std::map<Record*, TreePattern*> PatternFragments;
471 std::map<Record*, DAGDefaultOperand> DefaultOperands;
472 std::map<Record*, DAGInstruction> Instructions;
473
474 // Specific SDNode definitions:
475 Record *intrinsic_void_sdnode;
476 Record *intrinsic_w_chain_sdnode, *intrinsic_wo_chain_sdnode;
477
478 /// PatternsToMatch - All of the things we are matching on the DAG. The first
479 /// value is the pattern to match, the second pattern is the result to
480 /// emit.
481 std::vector<PatternToMatch> PatternsToMatch;
482public:
Chris Lattnerfe718932008-01-06 01:10:31 +0000483 CodeGenDAGPatterns(RecordKeeper &R);
484 ~CodeGenDAGPatterns();
Chris Lattner6cefb772008-01-05 22:25:12 +0000485
Dan Gohmanee4fa192008-04-03 00:02:49 +0000486 CodeGenTarget &getTargetInfo() { return Target; }
Chris Lattner6cefb772008-01-05 22:25:12 +0000487 const CodeGenTarget &getTargetInfo() const { return Target; }
488
489 Record *getSDNodeNamed(const std::string &Name) const;
490
491 const SDNodeInfo &getSDNodeInfo(Record *R) const {
492 assert(SDNodes.count(R) && "Unknown node!");
493 return SDNodes.find(R)->second;
494 }
495
Chris Lattner443e3f92008-01-05 22:54:53 +0000496 // Node transformation lookups.
497 typedef std::pair<Record*, std::string> NodeXForm;
498 const NodeXForm &getSDNodeTransform(Record *R) const {
Chris Lattner6cefb772008-01-05 22:25:12 +0000499 assert(SDNodeXForms.count(R) && "Invalid transform!");
500 return SDNodeXForms.find(R)->second;
501 }
502
Chris Lattner443e3f92008-01-05 22:54:53 +0000503 typedef std::map<Record*, NodeXForm>::const_iterator nx_iterator;
504 nx_iterator nx_begin() const { return SDNodeXForms.begin(); }
505 nx_iterator nx_end() const { return SDNodeXForms.end(); }
506
507
Chris Lattner6cefb772008-01-05 22:25:12 +0000508 const ComplexPattern &getComplexPattern(Record *R) const {
509 assert(ComplexPatterns.count(R) && "Unknown addressing mode!");
510 return ComplexPatterns.find(R)->second;
511 }
512
513 const CodeGenIntrinsic &getIntrinsic(Record *R) const {
514 for (unsigned i = 0, e = Intrinsics.size(); i != e; ++i)
515 if (Intrinsics[i].TheDef == R) return Intrinsics[i];
Dale Johannesen49de9822009-02-05 01:49:45 +0000516 for (unsigned i = 0, e = TgtIntrinsics.size(); i != e; ++i)
517 if (TgtIntrinsics[i].TheDef == R) return TgtIntrinsics[i];
Chris Lattner6cefb772008-01-05 22:25:12 +0000518 assert(0 && "Unknown intrinsic!");
519 abort();
520 }
521
522 const CodeGenIntrinsic &getIntrinsicInfo(unsigned IID) const {
Dale Johannesen49de9822009-02-05 01:49:45 +0000523 if (IID-1 < Intrinsics.size())
524 return Intrinsics[IID-1];
525 if (IID-Intrinsics.size()-1 < TgtIntrinsics.size())
526 return TgtIntrinsics[IID-Intrinsics.size()-1];
527 assert(0 && "Bad intrinsic ID!");
Evan Cheng32c1a4c2009-02-09 03:07:24 +0000528 abort();
Chris Lattner6cefb772008-01-05 22:25:12 +0000529 }
530
531 unsigned getIntrinsicID(Record *R) const {
532 for (unsigned i = 0, e = Intrinsics.size(); i != e; ++i)
533 if (Intrinsics[i].TheDef == R) return i;
Dale Johannesen49de9822009-02-05 01:49:45 +0000534 for (unsigned i = 0, e = TgtIntrinsics.size(); i != e; ++i)
535 if (TgtIntrinsics[i].TheDef == R) return i + Intrinsics.size();
Chris Lattner6cefb772008-01-05 22:25:12 +0000536 assert(0 && "Unknown intrinsic!");
537 abort();
538 }
539
540 const DAGDefaultOperand &getDefaultOperand(Record *R) {
541 assert(DefaultOperands.count(R) &&"Isn't an analyzed default operand!");
542 return DefaultOperands.find(R)->second;
543 }
544
545 // Pattern Fragment information.
546 TreePattern *getPatternFragment(Record *R) const {
547 assert(PatternFragments.count(R) && "Invalid pattern fragment request!");
548 return PatternFragments.find(R)->second;
549 }
550 typedef std::map<Record*, TreePattern*>::const_iterator pf_iterator;
551 pf_iterator pf_begin() const { return PatternFragments.begin(); }
552 pf_iterator pf_end() const { return PatternFragments.end(); }
553
554 // Patterns to match information.
Chris Lattner60d81392008-01-05 22:30:17 +0000555 typedef std::vector<PatternToMatch>::const_iterator ptm_iterator;
556 ptm_iterator ptm_begin() const { return PatternsToMatch.begin(); }
557 ptm_iterator ptm_end() const { return PatternsToMatch.end(); }
Chris Lattner6cefb772008-01-05 22:25:12 +0000558
559
560
561 const DAGInstruction &getInstruction(Record *R) const {
562 assert(Instructions.count(R) && "Unknown instruction!");
563 return Instructions.find(R)->second;
564 }
565
566 Record *get_intrinsic_void_sdnode() const {
567 return intrinsic_void_sdnode;
568 }
569 Record *get_intrinsic_w_chain_sdnode() const {
570 return intrinsic_w_chain_sdnode;
571 }
572 Record *get_intrinsic_wo_chain_sdnode() const {
573 return intrinsic_wo_chain_sdnode;
574 }
575
576private:
577 void ParseNodeInfo();
Chris Lattner443e3f92008-01-05 22:54:53 +0000578 void ParseNodeTransforms();
Chris Lattner6cefb772008-01-05 22:25:12 +0000579 void ParseComplexPatterns();
Chris Lattnerdc32f982008-01-05 22:43:57 +0000580 void ParsePatternFragments();
Chris Lattner6cefb772008-01-05 22:25:12 +0000581 void ParseDefaultOperands();
582 void ParseInstructions();
583 void ParsePatterns();
Dan Gohmanee4fa192008-04-03 00:02:49 +0000584 void InferInstructionFlags();
Chris Lattner6cefb772008-01-05 22:25:12 +0000585 void GenerateVariants();
586
587 void FindPatternInputsAndOutputs(TreePattern *I, TreePatternNode *Pat,
588 std::map<std::string,
589 TreePatternNode*> &InstInputs,
590 std::map<std::string,
591 TreePatternNode*> &InstResults,
592 std::vector<Record*> &InstImpInputs,
593 std::vector<Record*> &InstImpResults);
594};
595} // end namespace llvm
596
597#endif