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
22 #include "CodeGenTarget.h"
23 #include "CodeGenIntrinsics.h"
32 class TreePatternNode;
33 class CodeGenDAGPatterns;
36 /// EEVT::DAGISelGenValueType - These are some extended forms of
37 /// MVT::SimpleValueType that we use as lattice values during type inference.
38 /// The existing MVT iAny, fAny and vAny types suffice to represent
39 /// arbitrary integer, floating-point, and vector types, so only an unknown
42 enum DAGISelGenValueType {
43 isUnknown = MVT::LAST_VALUETYPE
46 /// isExtIntegerInVTs - Return true if the specified extended value type
47 /// vector contains iAny or an integer value type.
48 bool isExtIntegerInVTs(const std::vector<unsigned char> &EVTs);
50 /// isExtFloatingPointInVTs - Return true if the specified extended value
51 /// type vector contains fAny or a FP value type.
52 bool isExtFloatingPointInVTs(const std::vector<unsigned char> &EVTs);
54 /// isExtVectorinVTs - Return true if the specified extended value type
55 /// vector contains vAny or a vector value type.
56 bool isExtVectorInVTs(const std::vector<unsigned char> &EVTs);
59 /// Set type used to track multiply used variables in patterns
60 typedef std::set<std::string> MultipleUseVarSet;
62 /// SDTypeConstraint - This is a discriminated union of constraints,
63 /// corresponding to the SDTypeConstraint tablegen class in Target.td.
64 struct SDTypeConstraint {
65 SDTypeConstraint(Record *R);
67 unsigned OperandNo; // The operand # this constraint applies to.
69 SDTCisVT, SDTCisPtrTy, SDTCisInt, SDTCisFP, SDTCisVec, SDTCisSameAs,
70 SDTCisVTSmallerThanOp, SDTCisOpSmallerThanOp, SDTCisEltOfVec
73 union { // The discriminated union.
78 unsigned OtherOperandNum;
81 unsigned OtherOperandNum;
82 } SDTCisVTSmallerThanOp_Info;
84 unsigned BigOperandNum;
85 } SDTCisOpSmallerThanOp_Info;
87 unsigned OtherOperandNum;
88 } SDTCisEltOfVec_Info;
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
95 bool ApplyTypeConstraint(TreePatternNode *N, const SDNodeInfo &NodeInfo,
96 TreePattern &TP) const;
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;
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
109 std::string EnumName;
110 std::string SDClassName;
114 std::vector<SDTypeConstraint> TypeConstraints;
116 SDNodeInfo(Record *R); // Parse the specified record.
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; }
124 const std::vector<SDTypeConstraint> &getTypeConstraints() const {
125 return TypeConstraints;
128 /// hasProperty - Return true if this node has the specified property.
130 bool hasProperty(enum SDNP Prop) const { return Properties & (1 << Prop); }
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);
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!
147 class TreePatternNode {
148 /// The inferred type for this node, or EEVT::isUnknown if it hasn't
149 /// been determined yet. This is a std::vector because during inference
150 /// there may be multiple possible types.
151 std::vector<unsigned char> Types;
153 /// Operator - The Record for the operator if this is an interior node (not
157 /// Val - The init value (e.g. the "GPRC" record, or "7") for a leaf.
161 /// Name - The name given to this node with the :$foo notation.
165 /// 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;
169 /// TransformFn - The transformation function to execute on this node before
170 /// it can be substituted into the resulting instruction on a pattern match.
173 std::vector<TreePatternNode*> Children;
175 TreePatternNode(Record *Op, const std::vector<TreePatternNode*> &Ch)
176 : Types(), Operator(Op), Val(0), TransformFn(0),
177 Children(Ch) { Types.push_back(EEVT::isUnknown); }
178 TreePatternNode(Init *val) // leaf ctor
179 : Types(), Operator(0), Val(val), TransformFn(0) {
180 Types.push_back(EEVT::isUnknown);
184 const std::string &getName() const { return Name; }
185 void setName(const std::string &N) { Name = N; }
187 bool isLeaf() const { return Val != 0; }
188 bool hasTypeSet() const {
189 return (Types[0] < MVT::LAST_VALUETYPE) || (Types[0] == MVT::iPTR) ||
190 (Types[0] == MVT::iPTRAny);
192 bool isTypeCompletelyUnknown() const {
193 return Types[0] == EEVT::isUnknown;
195 bool isTypeDynamicallyResolved() const {
196 return (Types[0] == MVT::iPTR) || (Types[0] == MVT::iPTRAny);
198 MVT::SimpleValueType getTypeNum(unsigned Num) const {
199 assert(hasTypeSet() && "Doesn't have a type yet!");
200 assert(Types.size() > Num && "Type num out of range!");
201 return (MVT::SimpleValueType)Types[Num];
203 unsigned char getExtTypeNum(unsigned Num) const {
204 assert(Types.size() > Num && "Extended type num out of range!");
207 const std::vector<unsigned char> &getExtTypes() const { return Types; }
208 void setTypes(const std::vector<unsigned char> &T) { Types = T; }
209 void removeTypes() { Types = std::vector<unsigned char>(1, EEVT::isUnknown); }
211 Init *getLeafValue() const { assert(isLeaf()); return Val; }
212 Record *getOperator() const { assert(!isLeaf()); return Operator; }
214 unsigned getNumChildren() const { return Children.size(); }
215 TreePatternNode *getChild(unsigned N) const { return Children[N]; }
216 void setChild(unsigned i, TreePatternNode *N) {
220 const std::vector<std::string> &getPredicateFns() const { return PredicateFns; }
221 void clearPredicateFns() { PredicateFns.clear(); }
222 void setPredicateFns(const std::vector<std::string> &Fns) {
223 assert(PredicateFns.empty() && "Overwriting non-empty predicate list!");
226 void addPredicateFn(const std::string &Fn) {
227 assert(!Fn.empty() && "Empty predicate string!");
228 if (std::find(PredicateFns.begin(), PredicateFns.end(), Fn) ==
230 PredicateFns.push_back(Fn);
233 Record *getTransformFn() const { return TransformFn; }
234 void setTransformFn(Record *Fn) { TransformFn = Fn; }
236 /// getIntrinsicInfo - If this node corresponds to an intrinsic, return the
237 /// CodeGenIntrinsic information for it, otherwise return a null pointer.
238 const CodeGenIntrinsic *getIntrinsicInfo(const CodeGenDAGPatterns &CDP) const;
240 /// isCommutativeIntrinsic - Return true if the node is an intrinsic which is
241 /// marked isCommutative.
242 bool isCommutativeIntrinsic(const CodeGenDAGPatterns &CDP) const;
244 void print(raw_ostream &OS) const;
247 public: // Higher level manipulation routines.
249 /// clone - Return a new copy of this tree.
251 TreePatternNode *clone() const;
253 /// isIsomorphicTo - Return true if this node is recursively isomorphic to
254 /// the specified node. For this comparison, all of the state of the node
255 /// is considered, except for the assigned name. Nodes with differing names
256 /// that are otherwise identical are considered isomorphic.
257 bool isIsomorphicTo(const TreePatternNode *N,
258 const MultipleUseVarSet &DepVars) const;
260 /// SubstituteFormalArguments - Replace the formal arguments in this tree
261 /// with actual values specified by ArgMap.
262 void SubstituteFormalArguments(std::map<std::string,
263 TreePatternNode*> &ArgMap);
265 /// InlinePatternFragments - If this pattern refers to any pattern
266 /// fragments, inline them into place, giving us a pattern without any
267 /// PatFrag references.
268 TreePatternNode *InlinePatternFragments(TreePattern &TP);
270 /// ApplyTypeConstraints - Apply all of the type constraints relevant to
271 /// this node and its children in the tree. This returns true if it makes a
272 /// change, false otherwise. If a type contradiction is found, throw an
274 bool ApplyTypeConstraints(TreePattern &TP, bool NotRegisters);
276 /// UpdateNodeType - Set the node type of N to VT if VT contains
277 /// information. If N already contains a conflicting type, then throw an
278 /// exception. This returns true if any information was updated.
280 bool UpdateNodeType(const std::vector<unsigned char> &ExtVTs,
282 bool UpdateNodeType(unsigned char ExtVT, TreePattern &TP) {
283 std::vector<unsigned char> ExtVTs(1, ExtVT);
284 return UpdateNodeType(ExtVTs, TP);
287 /// ContainsUnresolvedType - Return true if this tree contains any
288 /// unresolved types.
289 bool ContainsUnresolvedType() const {
290 if (!hasTypeSet() && !isTypeDynamicallyResolved()) return true;
291 for (unsigned i = 0, e = getNumChildren(); i != e; ++i)
292 if (getChild(i)->ContainsUnresolvedType()) return true;
296 /// canPatternMatch - If it is impossible for this pattern to match on this
297 /// target, fill in Reason and return false. Otherwise, return true.
298 bool canPatternMatch(std::string &Reason, const CodeGenDAGPatterns &CDP);
301 inline raw_ostream &operator<<(raw_ostream &OS, const TreePatternNode &TPN) {
307 /// TreePattern - Represent a pattern, used for instructions, pattern
311 /// Trees - The list of pattern trees which corresponds to this pattern.
312 /// Note that PatFrag's only have a single tree.
314 std::vector<TreePatternNode*> Trees;
316 /// TheRecord - The actual TableGen record corresponding to this pattern.
320 /// Args - This is a list of all of the arguments to this pattern (for
321 /// PatFrag patterns), which are the 'node' markers in this pattern.
322 std::vector<std::string> Args;
324 /// CDP - the top-level object coordinating this madness.
326 CodeGenDAGPatterns &CDP;
328 /// isInputPattern - True if this is an input pattern, something to match.
329 /// False if this is an output pattern, something to emit.
333 /// TreePattern constructor - Parse the specified DagInits into the
335 TreePattern(Record *TheRec, ListInit *RawPat, bool isInput,
336 CodeGenDAGPatterns &ise);
337 TreePattern(Record *TheRec, DagInit *Pat, bool isInput,
338 CodeGenDAGPatterns &ise);
339 TreePattern(Record *TheRec, TreePatternNode *Pat, bool isInput,
340 CodeGenDAGPatterns &ise);
342 /// getTrees - Return the tree patterns which corresponds to this pattern.
344 const std::vector<TreePatternNode*> &getTrees() const { return Trees; }
345 unsigned getNumTrees() const { return Trees.size(); }
346 TreePatternNode *getTree(unsigned i) const { return Trees[i]; }
347 TreePatternNode *getOnlyTree() const {
348 assert(Trees.size() == 1 && "Doesn't have exactly one pattern!");
352 /// getRecord - Return the actual TableGen record corresponding to this
355 Record *getRecord() const { return TheRecord; }
357 unsigned getNumArgs() const { return Args.size(); }
358 const std::string &getArgName(unsigned i) const {
359 assert(i < Args.size() && "Argument reference out of range!");
362 std::vector<std::string> &getArgList() { return Args; }
364 CodeGenDAGPatterns &getDAGPatterns() const { return CDP; }
366 /// InlinePatternFragments - If this pattern refers to any pattern
367 /// fragments, inline them into place, giving us a pattern without any
368 /// PatFrag references.
369 void InlinePatternFragments() {
370 for (unsigned i = 0, e = Trees.size(); i != e; ++i)
371 Trees[i] = Trees[i]->InlinePatternFragments(*this);
374 /// InferAllTypes - Infer/propagate as many types throughout the expression
375 /// patterns as possible. Return true if all types are inferred, false
376 /// otherwise. Throw an exception if a type contradiction is found.
377 bool InferAllTypes();
379 /// error - Throw an exception, prefixing it with information about this
381 void error(const std::string &Msg) const;
383 void print(raw_ostream &OS) const;
387 TreePatternNode *ParseTreePattern(DagInit *DI);
390 /// DAGDefaultOperand - One of these is created for each PredicateOperand
391 /// or OptionalDefOperand that has a set ExecuteAlways / DefaultOps field.
392 struct DAGDefaultOperand {
393 std::vector<TreePatternNode*> DefaultOps;
396 class DAGInstruction {
397 TreePattern *Pattern;
398 std::vector<Record*> Results;
399 std::vector<Record*> Operands;
400 std::vector<Record*> ImpResults;
401 std::vector<Record*> ImpOperands;
402 TreePatternNode *ResultPattern;
404 DAGInstruction(TreePattern *TP,
405 const std::vector<Record*> &results,
406 const std::vector<Record*> &operands,
407 const std::vector<Record*> &impresults,
408 const std::vector<Record*> &impoperands)
409 : Pattern(TP), Results(results), Operands(operands),
410 ImpResults(impresults), ImpOperands(impoperands),
413 const TreePattern *getPattern() const { return Pattern; }
414 unsigned getNumResults() const { return Results.size(); }
415 unsigned getNumOperands() const { return Operands.size(); }
416 unsigned getNumImpResults() const { return ImpResults.size(); }
417 unsigned getNumImpOperands() const { return ImpOperands.size(); }
418 const std::vector<Record*>& getImpResults() const { return ImpResults; }
420 void setResultPattern(TreePatternNode *R) { ResultPattern = R; }
422 Record *getResult(unsigned RN) const {
423 assert(RN < Results.size());
427 Record *getOperand(unsigned ON) const {
428 assert(ON < Operands.size());
432 Record *getImpResult(unsigned RN) const {
433 assert(RN < ImpResults.size());
434 return ImpResults[RN];
437 Record *getImpOperand(unsigned ON) const {
438 assert(ON < ImpOperands.size());
439 return ImpOperands[ON];
442 TreePatternNode *getResultPattern() const { return ResultPattern; }
445 /// PatternToMatch - Used by CodeGenDAGPatterns to keep tab of patterns
446 /// processed to produce isel.
447 struct PatternToMatch {
448 PatternToMatch(ListInit *preds,
449 TreePatternNode *src, TreePatternNode *dst,
450 const std::vector<Record*> &dstregs,
451 unsigned complexity):
452 Predicates(preds), SrcPattern(src), DstPattern(dst), Dstregs(dstregs),
453 AddedComplexity(complexity) {}
455 ListInit *Predicates; // Top level predicate conditions to match.
456 TreePatternNode *SrcPattern; // Source pattern to match.
457 TreePatternNode *DstPattern; // Resulting pattern.
458 std::vector<Record*> Dstregs; // Physical register defs being matched.
459 unsigned AddedComplexity; // Add to matching pattern complexity.
461 ListInit *getPredicates() const { return Predicates; }
462 TreePatternNode *getSrcPattern() const { return SrcPattern; }
463 TreePatternNode *getDstPattern() const { return DstPattern; }
464 const std::vector<Record*> &getDstRegs() const { return Dstregs; }
465 unsigned getAddedComplexity() const { return AddedComplexity; }
467 std::string getPredicateCheck() const;
470 // Deterministic comparison of Record*.
471 struct RecordPtrCmp {
472 bool operator()(const Record *LHS, const Record *RHS) const;
475 class CodeGenDAGPatterns {
476 RecordKeeper &Records;
477 CodeGenTarget Target;
478 std::vector<CodeGenIntrinsic> Intrinsics;
479 std::vector<CodeGenIntrinsic> TgtIntrinsics;
481 std::map<Record*, SDNodeInfo, RecordPtrCmp> SDNodes;
482 std::map<Record*, std::pair<Record*, std::string>, RecordPtrCmp> SDNodeXForms;
483 std::map<Record*, ComplexPattern, RecordPtrCmp> ComplexPatterns;
484 std::map<Record*, TreePattern*, RecordPtrCmp> PatternFragments;
485 std::map<Record*, DAGDefaultOperand, RecordPtrCmp> DefaultOperands;
486 std::map<Record*, DAGInstruction, RecordPtrCmp> Instructions;
488 // Specific SDNode definitions:
489 Record *intrinsic_void_sdnode;
490 Record *intrinsic_w_chain_sdnode, *intrinsic_wo_chain_sdnode;
492 /// PatternsToMatch - All of the things we are matching on the DAG. The first
493 /// value is the pattern to match, the second pattern is the result to
495 std::vector<PatternToMatch> PatternsToMatch;
497 CodeGenDAGPatterns(RecordKeeper &R);
498 ~CodeGenDAGPatterns();
500 CodeGenTarget &getTargetInfo() { return Target; }
501 const CodeGenTarget &getTargetInfo() const { return Target; }
503 Record *getSDNodeNamed(const std::string &Name) const;
505 const SDNodeInfo &getSDNodeInfo(Record *R) const {
506 assert(SDNodes.count(R) && "Unknown node!");
507 return SDNodes.find(R)->second;
510 // Node transformation lookups.
511 typedef std::pair<Record*, std::string> NodeXForm;
512 const NodeXForm &getSDNodeTransform(Record *R) const {
513 assert(SDNodeXForms.count(R) && "Invalid transform!");
514 return SDNodeXForms.find(R)->second;
517 typedef std::map<Record*, NodeXForm, RecordPtrCmp>::const_iterator
519 nx_iterator nx_begin() const { return SDNodeXForms.begin(); }
520 nx_iterator nx_end() const { return SDNodeXForms.end(); }
523 const ComplexPattern &getComplexPattern(Record *R) const {
524 assert(ComplexPatterns.count(R) && "Unknown addressing mode!");
525 return ComplexPatterns.find(R)->second;
528 const CodeGenIntrinsic &getIntrinsic(Record *R) const {
529 for (unsigned i = 0, e = Intrinsics.size(); i != e; ++i)
530 if (Intrinsics[i].TheDef == R) return Intrinsics[i];
531 for (unsigned i = 0, e = TgtIntrinsics.size(); i != e; ++i)
532 if (TgtIntrinsics[i].TheDef == R) return TgtIntrinsics[i];
533 assert(0 && "Unknown intrinsic!");
537 const CodeGenIntrinsic &getIntrinsicInfo(unsigned IID) const {
538 if (IID-1 < Intrinsics.size())
539 return Intrinsics[IID-1];
540 if (IID-Intrinsics.size()-1 < TgtIntrinsics.size())
541 return TgtIntrinsics[IID-Intrinsics.size()-1];
542 assert(0 && "Bad intrinsic ID!");
546 unsigned getIntrinsicID(Record *R) const {
547 for (unsigned i = 0, e = Intrinsics.size(); i != e; ++i)
548 if (Intrinsics[i].TheDef == R) return i;
549 for (unsigned i = 0, e = TgtIntrinsics.size(); i != e; ++i)
550 if (TgtIntrinsics[i].TheDef == R) return i + Intrinsics.size();
551 assert(0 && "Unknown intrinsic!");
555 const DAGDefaultOperand &getDefaultOperand(Record *R) {
556 assert(DefaultOperands.count(R) &&"Isn't an analyzed default operand!");
557 return DefaultOperands.find(R)->second;
560 // Pattern Fragment information.
561 TreePattern *getPatternFragment(Record *R) const {
562 assert(PatternFragments.count(R) && "Invalid pattern fragment request!");
563 return PatternFragments.find(R)->second;
565 typedef std::map<Record*, TreePattern*, RecordPtrCmp>::const_iterator
567 pf_iterator pf_begin() const { return PatternFragments.begin(); }
568 pf_iterator pf_end() const { return PatternFragments.end(); }
570 // Patterns to match information.
571 typedef std::vector<PatternToMatch>::const_iterator ptm_iterator;
572 ptm_iterator ptm_begin() const { return PatternsToMatch.begin(); }
573 ptm_iterator ptm_end() const { return PatternsToMatch.end(); }
577 const DAGInstruction &getInstruction(Record *R) const {
578 assert(Instructions.count(R) && "Unknown instruction!");
579 return Instructions.find(R)->second;
582 Record *get_intrinsic_void_sdnode() const {
583 return intrinsic_void_sdnode;
585 Record *get_intrinsic_w_chain_sdnode() const {
586 return intrinsic_w_chain_sdnode;
588 Record *get_intrinsic_wo_chain_sdnode() const {
589 return intrinsic_wo_chain_sdnode;
592 bool hasTargetIntrinsics() { return !TgtIntrinsics.empty(); }
595 void ParseNodeInfo();
596 void ParseNodeTransforms();
597 void ParseComplexPatterns();
598 void ParsePatternFragments();
599 void ParseDefaultOperands();
600 void ParseInstructions();
601 void ParsePatterns();
602 void InferInstructionFlags();
603 void GenerateVariants();
605 void FindPatternInputsAndOutputs(TreePattern *I, TreePatternNode *Pat,
606 std::map<std::string,
607 TreePatternNode*> &InstInputs,
608 std::map<std::string,
609 TreePatternNode*> &InstResults,
610 std::vector<Record*> &InstImpInputs,
611 std::vector<Record*> &InstImpResults);
613 } // end namespace llvm