1 //===- CodeGenDAGPatterns.h - Read DAG patterns from .td file ---*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file declares the CodeGenDAGPatterns class, which is used to read and
11 // represent the patterns present in a .td file for instructions.
13 //===----------------------------------------------------------------------===//
15 #ifndef CODEGEN_DAGPATTERNS_H
16 #define CODEGEN_DAGPATTERNS_H
18 #include "CodeGenIntrinsics.h"
19 #include "CodeGenTarget.h"
20 #include "llvm/ADT/SmallVector.h"
21 #include "llvm/ADT/StringMap.h"
22 #include "llvm/Support/ErrorHandling.h"
35 class TreePatternNode;
36 class CodeGenDAGPatterns;
39 /// EEVT::DAGISelGenValueType - These are some extended forms of
40 /// MVT::SimpleValueType that we use as lattice values during type inference.
41 /// The existing MVT iAny, fAny and vAny types suffice to represent
42 /// arbitrary integer, floating-point, and vector types, so only an unknown
45 /// TypeSet - This is either empty if it's completely unknown, or holds a set
46 /// of types. It is used during type inference because register classes can
47 /// have multiple possible types and we don't know which one they get until
48 /// type inference is complete.
50 /// TypeSet can have three states:
51 /// Vector is empty: The type is completely unknown, it can be any valid
53 /// Vector has multiple constrained types: (e.g. v4i32 + v4f32) it is one
54 /// of those types only.
55 /// Vector has one concrete type: The type is completely known.
58 SmallVector<MVT::SimpleValueType, 4> TypeVec;
61 TypeSet(MVT::SimpleValueType VT, TreePattern &TP);
62 TypeSet(ArrayRef<MVT::SimpleValueType> VTList);
64 bool isCompletelyUnknown() const { return TypeVec.empty(); }
66 bool isConcrete() const {
67 if (TypeVec.size() != 1) return false;
68 unsigned char T = TypeVec[0]; (void)T;
69 assert(T < MVT::LAST_VALUETYPE || T == MVT::iPTR || T == MVT::iPTRAny);
73 MVT::SimpleValueType getConcrete() const {
74 assert(isConcrete() && "Type isn't concrete yet");
75 return (MVT::SimpleValueType)TypeVec[0];
78 bool isDynamicallyResolved() const {
79 return getConcrete() == MVT::iPTR || getConcrete() == MVT::iPTRAny;
82 const SmallVectorImpl<MVT::SimpleValueType> &getTypeList() const {
83 assert(!TypeVec.empty() && "Not a type list!");
88 return TypeVec.size() == 1 && TypeVec[0] == MVT::isVoid;
91 /// hasIntegerTypes - Return true if this TypeSet contains any integer value
93 bool hasIntegerTypes() const;
95 /// hasFloatingPointTypes - Return true if this TypeSet contains an fAny or
96 /// a floating point value type.
97 bool hasFloatingPointTypes() const;
99 /// hasVectorTypes - Return true if this TypeSet contains a vector value
101 bool hasVectorTypes() const;
103 /// getName() - Return this TypeSet as a string.
104 std::string getName() const;
106 /// MergeInTypeInfo - This merges in type information from the specified
107 /// argument. If 'this' changes, it returns true. If the two types are
108 /// contradictory (e.g. merge f32 into i32) then this flags an error.
109 bool MergeInTypeInfo(const EEVT::TypeSet &InVT, TreePattern &TP);
111 bool MergeInTypeInfo(MVT::SimpleValueType InVT, TreePattern &TP) {
112 return MergeInTypeInfo(EEVT::TypeSet(InVT, TP), TP);
115 /// Force this type list to only contain integer types.
116 bool EnforceInteger(TreePattern &TP);
118 /// Force this type list to only contain floating point types.
119 bool EnforceFloatingPoint(TreePattern &TP);
121 /// EnforceScalar - Remove all vector types from this type list.
122 bool EnforceScalar(TreePattern &TP);
124 /// EnforceVector - Remove all non-vector types from this type list.
125 bool EnforceVector(TreePattern &TP);
127 /// EnforceSmallerThan - 'this' must be a smaller VT than Other. Update
128 /// this an other based on this information.
129 bool EnforceSmallerThan(EEVT::TypeSet &Other, TreePattern &TP);
131 /// EnforceVectorEltTypeIs - 'this' is now constrainted to be a vector type
132 /// whose element is VT.
133 bool EnforceVectorEltTypeIs(EEVT::TypeSet &VT, TreePattern &TP);
135 /// EnforceVectorSubVectorTypeIs - 'this' is now constrainted to
136 /// be a vector type VT.
137 bool EnforceVectorSubVectorTypeIs(EEVT::TypeSet &VT, TreePattern &TP);
139 bool operator!=(const TypeSet &RHS) const { return TypeVec != RHS.TypeVec; }
140 bool operator==(const TypeSet &RHS) const { return TypeVec == RHS.TypeVec; }
143 /// FillWithPossibleTypes - Set to all legal types and return true, only
144 /// valid on completely unknown type sets. If Pred is non-null, only MVTs
145 /// that pass the predicate are added.
146 bool FillWithPossibleTypes(TreePattern &TP,
147 bool (*Pred)(MVT::SimpleValueType) = 0,
148 const char *PredicateName = 0);
152 /// Set type used to track multiply used variables in patterns
153 typedef std::set<std::string> MultipleUseVarSet;
155 /// SDTypeConstraint - This is a discriminated union of constraints,
156 /// corresponding to the SDTypeConstraint tablegen class in Target.td.
157 struct SDTypeConstraint {
158 SDTypeConstraint(Record *R);
160 unsigned OperandNo; // The operand # this constraint applies to.
162 SDTCisVT, SDTCisPtrTy, SDTCisInt, SDTCisFP, SDTCisVec, SDTCisSameAs,
163 SDTCisVTSmallerThanOp, SDTCisOpSmallerThanOp, SDTCisEltOfVec,
167 union { // The discriminated union.
169 MVT::SimpleValueType VT;
172 unsigned OtherOperandNum;
175 unsigned OtherOperandNum;
176 } SDTCisVTSmallerThanOp_Info;
178 unsigned BigOperandNum;
179 } SDTCisOpSmallerThanOp_Info;
181 unsigned OtherOperandNum;
182 } SDTCisEltOfVec_Info;
184 unsigned OtherOperandNum;
185 } SDTCisSubVecOfVec_Info;
188 /// ApplyTypeConstraint - Given a node in a pattern, apply this type
189 /// constraint to the nodes operands. This returns true if it makes a
190 /// change, false otherwise. If a type contradiction is found, an error
192 bool ApplyTypeConstraint(TreePatternNode *N, const SDNodeInfo &NodeInfo,
193 TreePattern &TP) const;
196 /// SDNodeInfo - One of these records is created for each SDNode instance in
197 /// the target .td file. This represents the various dag nodes we will be
201 std::string EnumName;
202 std::string SDClassName;
206 std::vector<SDTypeConstraint> TypeConstraints;
208 SDNodeInfo(Record *R); // Parse the specified record.
210 unsigned getNumResults() const { return NumResults; }
212 /// getNumOperands - This is the number of operands required or -1 if
214 int getNumOperands() const { return NumOperands; }
215 Record *getRecord() const { return Def; }
216 const std::string &getEnumName() const { return EnumName; }
217 const std::string &getSDClassName() const { return SDClassName; }
219 const std::vector<SDTypeConstraint> &getTypeConstraints() const {
220 return TypeConstraints;
223 /// getKnownType - If the type constraints on this node imply a fixed type
224 /// (e.g. all stores return void, etc), then return it as an
225 /// MVT::SimpleValueType. Otherwise, return MVT::Other.
226 MVT::SimpleValueType getKnownType(unsigned ResNo) const;
228 /// hasProperty - Return true if this node has the specified property.
230 bool hasProperty(enum SDNP Prop) const { return Properties & (1 << Prop); }
232 /// ApplyTypeConstraints - Given a node in a pattern, apply the type
233 /// constraints for this node to the operands of the node. This returns
234 /// true if it makes a change, false otherwise. If a type contradiction is
235 /// found, an error is flagged.
236 bool ApplyTypeConstraints(TreePatternNode *N, TreePattern &TP) const {
237 bool MadeChange = false;
238 for (unsigned i = 0, e = TypeConstraints.size(); i != e; ++i)
239 MadeChange |= TypeConstraints[i].ApplyTypeConstraint(N, *this, TP);
244 /// TreePredicateFn - This is an abstraction that represents the predicates on
245 /// a PatFrag node. This is a simple one-word wrapper around a pointer to
246 /// provide nice accessors.
247 class TreePredicateFn {
248 /// PatFragRec - This is the TreePattern for the PatFrag that we
249 /// originally came from.
250 TreePattern *PatFragRec;
252 /// TreePredicateFn constructor. Here 'N' is a subclass of PatFrag.
253 TreePredicateFn(TreePattern *N);
256 TreePattern *getOrigPatFragRecord() const { return PatFragRec; }
258 /// isAlwaysTrue - Return true if this is a noop predicate.
259 bool isAlwaysTrue() const;
261 bool isImmediatePattern() const { return !getImmCode().empty(); }
263 /// getImmediatePredicateCode - Return the code that evaluates this pattern if
264 /// this is an immediate predicate. It is an error to call this on a
265 /// non-immediate pattern.
266 std::string getImmediatePredicateCode() const {
267 std::string Result = getImmCode();
268 assert(!Result.empty() && "Isn't an immediate pattern!");
273 bool operator==(const TreePredicateFn &RHS) const {
274 return PatFragRec == RHS.PatFragRec;
277 bool operator!=(const TreePredicateFn &RHS) const { return !(*this == RHS); }
279 /// Return the name to use in the generated code to reference this, this is
280 /// "Predicate_foo" if from a pattern fragment "foo".
281 std::string getFnName() const;
283 /// getCodeToRunOnSDNode - Return the code for the function body that
284 /// evaluates this predicate. The argument is expected to be in "Node",
285 /// not N. This handles casting and conversion to a concrete node type as
287 std::string getCodeToRunOnSDNode() const;
290 std::string getPredCode() const;
291 std::string getImmCode() const;
295 /// FIXME: TreePatternNode's can be shared in some cases (due to dag-shaped
296 /// patterns), and as such should be ref counted. We currently just leak all
297 /// TreePatternNode objects!
298 class TreePatternNode {
299 /// The type of each node result. Before and during type inference, each
300 /// result may be a set of possible types. After (successful) type inference,
301 /// each is a single concrete type.
302 SmallVector<EEVT::TypeSet, 1> Types;
304 /// Operator - The Record for the operator if this is an interior node (not
308 /// Val - The init value (e.g. the "GPRC" record, or "7") for a leaf.
312 /// Name - The name given to this node with the :$foo notation.
316 /// PredicateFns - The predicate functions to execute on this node to check
317 /// for a match. If this list is empty, no predicate is involved.
318 std::vector<TreePredicateFn> PredicateFns;
320 /// TransformFn - The transformation function to execute on this node before
321 /// it can be substituted into the resulting instruction on a pattern match.
324 std::vector<TreePatternNode*> Children;
326 TreePatternNode(Record *Op, const std::vector<TreePatternNode*> &Ch,
328 : Operator(Op), Val(0), TransformFn(0), Children(Ch) {
329 Types.resize(NumResults);
331 TreePatternNode(Init *val, unsigned NumResults) // leaf ctor
332 : Operator(0), Val(val), TransformFn(0) {
333 Types.resize(NumResults);
337 bool hasName() const { return !Name.empty(); }
338 const std::string &getName() const { return Name; }
339 void setName(StringRef N) { Name.assign(N.begin(), N.end()); }
341 bool isLeaf() const { return Val != 0; }
344 unsigned getNumTypes() const { return Types.size(); }
345 MVT::SimpleValueType getType(unsigned ResNo) const {
346 return Types[ResNo].getConcrete();
348 const SmallVectorImpl<EEVT::TypeSet> &getExtTypes() const { return Types; }
349 const EEVT::TypeSet &getExtType(unsigned ResNo) const { return Types[ResNo]; }
350 EEVT::TypeSet &getExtType(unsigned ResNo) { return Types[ResNo]; }
351 void setType(unsigned ResNo, const EEVT::TypeSet &T) { Types[ResNo] = T; }
353 bool hasTypeSet(unsigned ResNo) const {
354 return Types[ResNo].isConcrete();
356 bool isTypeCompletelyUnknown(unsigned ResNo) const {
357 return Types[ResNo].isCompletelyUnknown();
359 bool isTypeDynamicallyResolved(unsigned ResNo) const {
360 return Types[ResNo].isDynamicallyResolved();
363 Init *getLeafValue() const { assert(isLeaf()); return Val; }
364 Record *getOperator() const { assert(!isLeaf()); return Operator; }
366 unsigned getNumChildren() const { return Children.size(); }
367 TreePatternNode *getChild(unsigned N) const { return Children[N]; }
368 void setChild(unsigned i, TreePatternNode *N) {
372 /// hasChild - Return true if N is any of our children.
373 bool hasChild(const TreePatternNode *N) const {
374 for (unsigned i = 0, e = Children.size(); i != e; ++i)
375 if (Children[i] == N) return true;
379 bool hasAnyPredicate() const { return !PredicateFns.empty(); }
381 const std::vector<TreePredicateFn> &getPredicateFns() const {
384 void clearPredicateFns() { PredicateFns.clear(); }
385 void setPredicateFns(const std::vector<TreePredicateFn> &Fns) {
386 assert(PredicateFns.empty() && "Overwriting non-empty predicate list!");
389 void addPredicateFn(const TreePredicateFn &Fn) {
390 assert(!Fn.isAlwaysTrue() && "Empty predicate string!");
391 if (std::find(PredicateFns.begin(), PredicateFns.end(), Fn) ==
393 PredicateFns.push_back(Fn);
396 Record *getTransformFn() const { return TransformFn; }
397 void setTransformFn(Record *Fn) { TransformFn = Fn; }
399 /// getIntrinsicInfo - If this node corresponds to an intrinsic, return the
400 /// CodeGenIntrinsic information for it, otherwise return a null pointer.
401 const CodeGenIntrinsic *getIntrinsicInfo(const CodeGenDAGPatterns &CDP) const;
403 /// getComplexPatternInfo - If this node corresponds to a ComplexPattern,
404 /// return the ComplexPattern information, otherwise return null.
405 const ComplexPattern *
406 getComplexPatternInfo(const CodeGenDAGPatterns &CGP) const;
408 /// NodeHasProperty - Return true if this node has the specified property.
409 bool NodeHasProperty(SDNP Property, const CodeGenDAGPatterns &CGP) const;
411 /// TreeHasProperty - Return true if any node in this tree has the specified
413 bool TreeHasProperty(SDNP Property, const CodeGenDAGPatterns &CGP) const;
415 /// isCommutativeIntrinsic - Return true if the node is an intrinsic which is
416 /// marked isCommutative.
417 bool isCommutativeIntrinsic(const CodeGenDAGPatterns &CDP) const;
419 void print(raw_ostream &OS) const;
422 public: // Higher level manipulation routines.
424 /// clone - Return a new copy of this tree.
426 TreePatternNode *clone() const;
428 /// RemoveAllTypes - Recursively strip all the types of this tree.
429 void RemoveAllTypes();
431 /// isIsomorphicTo - Return true if this node is recursively isomorphic to
432 /// the specified node. For this comparison, all of the state of the node
433 /// is considered, except for the assigned name. Nodes with differing names
434 /// that are otherwise identical are considered isomorphic.
435 bool isIsomorphicTo(const TreePatternNode *N,
436 const MultipleUseVarSet &DepVars) const;
438 /// SubstituteFormalArguments - Replace the formal arguments in this tree
439 /// with actual values specified by ArgMap.
440 void SubstituteFormalArguments(std::map<std::string,
441 TreePatternNode*> &ArgMap);
443 /// InlinePatternFragments - If this pattern refers to any pattern
444 /// fragments, inline them into place, giving us a pattern without any
445 /// PatFrag references.
446 TreePatternNode *InlinePatternFragments(TreePattern &TP);
448 /// ApplyTypeConstraints - Apply all of the type constraints relevant to
449 /// this node and its children in the tree. This returns true if it makes a
450 /// change, false otherwise. If a type contradiction is found, flag an error.
451 bool ApplyTypeConstraints(TreePattern &TP, bool NotRegisters);
453 /// UpdateNodeType - Set the node type of N to VT if VT contains
454 /// information. If N already contains a conflicting type, then flag an
455 /// error. This returns true if any information was updated.
457 bool UpdateNodeType(unsigned ResNo, const EEVT::TypeSet &InTy,
459 return Types[ResNo].MergeInTypeInfo(InTy, TP);
462 bool UpdateNodeType(unsigned ResNo, MVT::SimpleValueType InTy,
464 return Types[ResNo].MergeInTypeInfo(EEVT::TypeSet(InTy, TP), TP);
467 // Update node type with types inferred from an instruction operand or result
468 // def from the ins/outs lists.
469 // Return true if the type changed.
470 bool UpdateNodeTypeFromInst(unsigned ResNo, Record *Operand, TreePattern &TP);
472 /// ContainsUnresolvedType - Return true if this tree contains any
473 /// unresolved types.
474 bool ContainsUnresolvedType() const {
475 for (unsigned i = 0, e = Types.size(); i != e; ++i)
476 if (!Types[i].isConcrete()) return true;
478 for (unsigned i = 0, e = getNumChildren(); i != e; ++i)
479 if (getChild(i)->ContainsUnresolvedType()) return true;
483 /// canPatternMatch - If it is impossible for this pattern to match on this
484 /// target, fill in Reason and return false. Otherwise, return true.
485 bool canPatternMatch(std::string &Reason, const CodeGenDAGPatterns &CDP);
488 inline raw_ostream &operator<<(raw_ostream &OS, const TreePatternNode &TPN) {
494 /// TreePattern - Represent a pattern, used for instructions, pattern
498 /// Trees - The list of pattern trees which corresponds to this pattern.
499 /// Note that PatFrag's only have a single tree.
501 std::vector<TreePatternNode*> Trees;
503 /// NamedNodes - This is all of the nodes that have names in the trees in this
505 StringMap<SmallVector<TreePatternNode*,1> > NamedNodes;
507 /// TheRecord - The actual TableGen record corresponding to this pattern.
511 /// Args - This is a list of all of the arguments to this pattern (for
512 /// PatFrag patterns), which are the 'node' markers in this pattern.
513 std::vector<std::string> Args;
515 /// CDP - the top-level object coordinating this madness.
517 CodeGenDAGPatterns &CDP;
519 /// isInputPattern - True if this is an input pattern, something to match.
520 /// False if this is an output pattern, something to emit.
523 /// hasError - True if the currently processed nodes have unresolvable types
524 /// or other non-fatal errors
528 /// TreePattern constructor - Parse the specified DagInits into the
530 TreePattern(Record *TheRec, ListInit *RawPat, bool isInput,
531 CodeGenDAGPatterns &ise);
532 TreePattern(Record *TheRec, DagInit *Pat, bool isInput,
533 CodeGenDAGPatterns &ise);
534 TreePattern(Record *TheRec, TreePatternNode *Pat, bool isInput,
535 CodeGenDAGPatterns &ise);
537 /// getTrees - Return the tree patterns which corresponds to this pattern.
539 const std::vector<TreePatternNode*> &getTrees() const { return Trees; }
540 unsigned getNumTrees() const { return Trees.size(); }
541 TreePatternNode *getTree(unsigned i) const { return Trees[i]; }
542 TreePatternNode *getOnlyTree() const {
543 assert(Trees.size() == 1 && "Doesn't have exactly one pattern!");
547 const StringMap<SmallVector<TreePatternNode*,1> > &getNamedNodesMap() {
548 if (NamedNodes.empty())
553 /// getRecord - Return the actual TableGen record corresponding to this
556 Record *getRecord() const { return TheRecord; }
558 unsigned getNumArgs() const { return Args.size(); }
559 const std::string &getArgName(unsigned i) const {
560 assert(i < Args.size() && "Argument reference out of range!");
563 std::vector<std::string> &getArgList() { return Args; }
565 CodeGenDAGPatterns &getDAGPatterns() const { return CDP; }
567 /// InlinePatternFragments - If this pattern refers to any pattern
568 /// fragments, inline them into place, giving us a pattern without any
569 /// PatFrag references.
570 void InlinePatternFragments() {
571 for (unsigned i = 0, e = Trees.size(); i != e; ++i)
572 Trees[i] = Trees[i]->InlinePatternFragments(*this);
575 /// InferAllTypes - Infer/propagate as many types throughout the expression
576 /// patterns as possible. Return true if all types are inferred, false
577 /// otherwise. Bail out if a type contradiction is found.
578 bool InferAllTypes(const StringMap<SmallVector<TreePatternNode*,1> >
581 /// error - If this is the first error in the current resolution step,
582 /// print it and set the error flag. Otherwise, continue silently.
583 void error(const std::string &Msg);
584 bool hasError() const {
591 void print(raw_ostream &OS) const;
595 TreePatternNode *ParseTreePattern(Init *DI, StringRef OpName);
596 void ComputeNamedNodes();
597 void ComputeNamedNodes(TreePatternNode *N);
600 /// DAGDefaultOperand - One of these is created for each OperandWithDefaultOps
601 /// that has a set ExecuteAlways / DefaultOps field.
602 struct DAGDefaultOperand {
603 std::vector<TreePatternNode*> DefaultOps;
606 class DAGInstruction {
607 TreePattern *Pattern;
608 std::vector<Record*> Results;
609 std::vector<Record*> Operands;
610 std::vector<Record*> ImpResults;
611 TreePatternNode *ResultPattern;
613 DAGInstruction(TreePattern *TP,
614 const std::vector<Record*> &results,
615 const std::vector<Record*> &operands,
616 const std::vector<Record*> &impresults)
617 : Pattern(TP), Results(results), Operands(operands),
618 ImpResults(impresults), ResultPattern(0) {}
620 TreePattern *getPattern() const { return Pattern; }
621 unsigned getNumResults() const { return Results.size(); }
622 unsigned getNumOperands() const { return Operands.size(); }
623 unsigned getNumImpResults() const { return ImpResults.size(); }
624 const std::vector<Record*>& getImpResults() const { return ImpResults; }
626 void setResultPattern(TreePatternNode *R) { ResultPattern = R; }
628 Record *getResult(unsigned RN) const {
629 assert(RN < Results.size());
633 Record *getOperand(unsigned ON) const {
634 assert(ON < Operands.size());
638 Record *getImpResult(unsigned RN) const {
639 assert(RN < ImpResults.size());
640 return ImpResults[RN];
643 TreePatternNode *getResultPattern() const { return ResultPattern; }
646 /// PatternToMatch - Used by CodeGenDAGPatterns to keep tab of patterns
647 /// processed to produce isel.
648 class PatternToMatch {
650 PatternToMatch(Record *srcrecord, ListInit *preds,
651 TreePatternNode *src, TreePatternNode *dst,
652 const std::vector<Record*> &dstregs,
653 unsigned complexity, unsigned uid)
654 : SrcRecord(srcrecord), Predicates(preds), SrcPattern(src), DstPattern(dst),
655 Dstregs(dstregs), AddedComplexity(complexity), ID(uid) {}
657 Record *SrcRecord; // Originating Record for the pattern.
658 ListInit *Predicates; // Top level predicate conditions to match.
659 TreePatternNode *SrcPattern; // Source pattern to match.
660 TreePatternNode *DstPattern; // Resulting pattern.
661 std::vector<Record*> Dstregs; // Physical register defs being matched.
662 unsigned AddedComplexity; // Add to matching pattern complexity.
663 unsigned ID; // Unique ID for the record.
665 Record *getSrcRecord() const { return SrcRecord; }
666 ListInit *getPredicates() const { return Predicates; }
667 TreePatternNode *getSrcPattern() const { return SrcPattern; }
668 TreePatternNode *getDstPattern() const { return DstPattern; }
669 const std::vector<Record*> &getDstRegs() const { return Dstregs; }
670 unsigned getAddedComplexity() const { return AddedComplexity; }
672 std::string getPredicateCheck() const;
674 /// Compute the complexity metric for the input pattern. This roughly
675 /// corresponds to the number of nodes that are covered.
676 unsigned getPatternComplexity(const CodeGenDAGPatterns &CGP) const;
679 class CodeGenDAGPatterns {
680 RecordKeeper &Records;
681 CodeGenTarget Target;
682 std::vector<CodeGenIntrinsic> Intrinsics;
683 std::vector<CodeGenIntrinsic> TgtIntrinsics;
685 std::map<Record*, SDNodeInfo, LessRecordByID> SDNodes;
686 std::map<Record*, std::pair<Record*, std::string>, LessRecordByID> SDNodeXForms;
687 std::map<Record*, ComplexPattern, LessRecordByID> ComplexPatterns;
688 std::map<Record*, TreePattern*, LessRecordByID> PatternFragments;
689 std::map<Record*, DAGDefaultOperand, LessRecordByID> DefaultOperands;
690 std::map<Record*, DAGInstruction, LessRecordByID> Instructions;
692 // Specific SDNode definitions:
693 Record *intrinsic_void_sdnode;
694 Record *intrinsic_w_chain_sdnode, *intrinsic_wo_chain_sdnode;
696 /// PatternsToMatch - All of the things we are matching on the DAG. The first
697 /// value is the pattern to match, the second pattern is the result to
699 std::vector<PatternToMatch> PatternsToMatch;
701 CodeGenDAGPatterns(RecordKeeper &R);
702 ~CodeGenDAGPatterns();
704 CodeGenTarget &getTargetInfo() { return Target; }
705 const CodeGenTarget &getTargetInfo() const { return Target; }
707 Record *getSDNodeNamed(const std::string &Name) const;
709 const SDNodeInfo &getSDNodeInfo(Record *R) const {
710 assert(SDNodes.count(R) && "Unknown node!");
711 return SDNodes.find(R)->second;
714 // Node transformation lookups.
715 typedef std::pair<Record*, std::string> NodeXForm;
716 const NodeXForm &getSDNodeTransform(Record *R) const {
717 assert(SDNodeXForms.count(R) && "Invalid transform!");
718 return SDNodeXForms.find(R)->second;
721 typedef std::map<Record*, NodeXForm, LessRecordByID>::const_iterator
723 nx_iterator nx_begin() const { return SDNodeXForms.begin(); }
724 nx_iterator nx_end() const { return SDNodeXForms.end(); }
727 const ComplexPattern &getComplexPattern(Record *R) const {
728 assert(ComplexPatterns.count(R) && "Unknown addressing mode!");
729 return ComplexPatterns.find(R)->second;
732 const CodeGenIntrinsic &getIntrinsic(Record *R) const {
733 for (unsigned i = 0, e = Intrinsics.size(); i != e; ++i)
734 if (Intrinsics[i].TheDef == R) return Intrinsics[i];
735 for (unsigned i = 0, e = TgtIntrinsics.size(); i != e; ++i)
736 if (TgtIntrinsics[i].TheDef == R) return TgtIntrinsics[i];
737 llvm_unreachable("Unknown intrinsic!");
740 const CodeGenIntrinsic &getIntrinsicInfo(unsigned IID) const {
741 if (IID-1 < Intrinsics.size())
742 return Intrinsics[IID-1];
743 if (IID-Intrinsics.size()-1 < TgtIntrinsics.size())
744 return TgtIntrinsics[IID-Intrinsics.size()-1];
745 llvm_unreachable("Bad intrinsic ID!");
748 unsigned getIntrinsicID(Record *R) const {
749 for (unsigned i = 0, e = Intrinsics.size(); i != e; ++i)
750 if (Intrinsics[i].TheDef == R) return i;
751 for (unsigned i = 0, e = TgtIntrinsics.size(); i != e; ++i)
752 if (TgtIntrinsics[i].TheDef == R) return i + Intrinsics.size();
753 llvm_unreachable("Unknown intrinsic!");
756 const DAGDefaultOperand &getDefaultOperand(Record *R) const {
757 assert(DefaultOperands.count(R) &&"Isn't an analyzed default operand!");
758 return DefaultOperands.find(R)->second;
761 // Pattern Fragment information.
762 TreePattern *getPatternFragment(Record *R) const {
763 assert(PatternFragments.count(R) && "Invalid pattern fragment request!");
764 return PatternFragments.find(R)->second;
766 TreePattern *getPatternFragmentIfRead(Record *R) const {
767 if (!PatternFragments.count(R)) return 0;
768 return PatternFragments.find(R)->second;
771 typedef std::map<Record*, TreePattern*, LessRecordByID>::const_iterator
773 pf_iterator pf_begin() const { return PatternFragments.begin(); }
774 pf_iterator pf_end() const { return PatternFragments.end(); }
776 // Patterns to match information.
777 typedef std::vector<PatternToMatch>::const_iterator ptm_iterator;
778 ptm_iterator ptm_begin() const { return PatternsToMatch.begin(); }
779 ptm_iterator ptm_end() const { return PatternsToMatch.end(); }
783 const DAGInstruction &getInstruction(Record *R) const {
784 assert(Instructions.count(R) && "Unknown instruction!");
785 return Instructions.find(R)->second;
788 Record *get_intrinsic_void_sdnode() const {
789 return intrinsic_void_sdnode;
791 Record *get_intrinsic_w_chain_sdnode() const {
792 return intrinsic_w_chain_sdnode;
794 Record *get_intrinsic_wo_chain_sdnode() const {
795 return intrinsic_wo_chain_sdnode;
798 bool hasTargetIntrinsics() { return !TgtIntrinsics.empty(); }
801 void ParseNodeInfo();
802 void ParseNodeTransforms();
803 void ParseComplexPatterns();
804 void ParsePatternFragments();
805 void ParseDefaultOperands();
806 void ParseInstructions();
807 void ParsePatterns();
808 void InferInstructionFlags();
809 void GenerateVariants();
810 void VerifyInstructionFlags();
812 void AddPatternToMatch(TreePattern *Pattern, const PatternToMatch &PTM);
813 void FindPatternInputsAndOutputs(TreePattern *I, TreePatternNode *Pat,
814 std::map<std::string,
815 TreePatternNode*> &InstInputs,
816 std::map<std::string,
817 TreePatternNode*> &InstResults,
818 std::vector<Record*> &InstImpResults);
820 } // end namespace llvm