1 //===- DAGISelEmitter.h - Generate an instruction selector ------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by Chris Lattner and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This tablegen backend emits a DAG instruction selector.
12 //===----------------------------------------------------------------------===//
14 #ifndef DAGISEL_EMITTER_H
15 #define DAGISEL_EMITTER_H
17 #include "TableGenBackend.h"
18 #include "CodeGenTarget.h"
27 /// FIXME: TreePatternNode's can be shared in some cases (due to dag-shaped
28 /// patterns), and as such should be ref counted. We currently just leak all
29 /// TreePatternNode objects!
30 class TreePatternNode {
31 /// The inferred type for this node, or MVT::LAST_VALUETYPE if it hasn't
32 /// been determined yet.
35 /// Operator - The Record for the operator if this is an interior node (not
39 /// Val - The init value (e.g. the "GPRC" record, or "7") for a leaf.
43 /// Name - The name given to this node with the :$foo notation.
47 /// PredicateFn - The predicate function to execute on this node to check
48 /// for a match. If this string is empty, no predicate is involved.
49 std::string PredicateFn;
51 std::vector<TreePatternNode*> Children;
53 TreePatternNode(Record *Op, const std::vector<TreePatternNode*> &Ch)
54 : Ty(MVT::LAST_VALUETYPE), Operator(Op), Val(0), Children(Ch) {}
55 TreePatternNode(Init *val) // leaf ctor
56 : Ty(MVT::LAST_VALUETYPE), Operator(0), Val(val) {}
59 const std::string &getName() const { return Name; }
60 void setName(const std::string &N) { Name = N; }
62 bool isLeaf() const { return Val != 0; }
63 MVT::ValueType getType() const { return Ty; }
64 void setType(MVT::ValueType VT) { Ty = VT; }
66 Init *getLeafValue() const { assert(isLeaf()); return Val; }
67 Record *getOperator() const { assert(!isLeaf()); return Operator; }
69 unsigned getNumChildren() const { return Children.size(); }
70 TreePatternNode *getChild(unsigned N) const { return Children[N]; }
71 void setChild(unsigned i, TreePatternNode *N) {
75 const std::string &getPredicateFn() const { return PredicateFn; }
76 void setPredicateFn(const std::string &Fn) { PredicateFn = Fn; }
78 void print(std::ostream &OS) const;
81 public: // Higher level manipulation routines.
83 /// clone - Return a new copy of this tree.
85 TreePatternNode *clone() const;
87 void SubstituteFormalArguments(std::map<std::string,
88 TreePatternNode*> &ArgMap);
90 /// InlinePatternFragments - If this pattern refers to any pattern
91 /// fragments, inline them into place, giving us a pattern without any
92 /// PatFrag references.
93 TreePatternNode *InlinePatternFragments(TreePattern &TP);
98 /// TreePattern - Represent a pattern of one form or another. Currently, two
99 /// types of patterns are possible: Instructions and PatFrags.
107 /// PTy - The type of pattern this is.
111 /// Trees - The list of pattern trees which corresponds to this pattern.
112 /// Note that PatFrag's only have a single tree.
114 std::vector<TreePatternNode*> Trees;
116 /// TheRecord - The actual TableGen record corresponding to this pattern.
120 /// Args - This is a list of all of the arguments to this pattern (for
121 /// PatFrag patterns), which are the 'node' markers in this pattern.
122 std::vector<std::string> Args;
124 /// ISE - the DAG isel emitter coordinating this madness.
129 /// TreePattern constructor - Parse the specified DagInits into the
131 TreePattern(PatternType pty, Record *TheRec,
132 const std::vector<DagInit *> &RawPat, DAGISelEmitter &ise);
134 /// getPatternType - Return what flavor of Record this pattern originated from
136 PatternType getPatternType() const { return PTy; }
138 /// getTrees - Return the tree patterns which corresponds to this pattern.
140 const std::vector<TreePatternNode*> &getTrees() const { return Trees; }
142 /// getRecord - Return the actual TableGen record corresponding to this
145 Record *getRecord() const { return TheRecord; }
147 unsigned getNumArgs() const { return Args.size(); }
148 const std::string &getArgName(unsigned i) const {
149 assert(i < Args.size() && "Argument reference out of range!");
153 DAGISelEmitter &getDAGISelEmitter() const { return ISE; }
155 /// InlinePatternFragments - If this pattern refers to any pattern
156 /// fragments, inline them into place, giving us a pattern without any
157 /// PatFrag references.
158 void InlinePatternFragments() {
159 for (unsigned i = 0, e = Trees.size(); i != e; ++i)
160 Trees[i] = Trees[i]->InlinePatternFragments(*this);
163 /// error - Throw an exception, prefixing it with information about this
165 void error(const std::string &Msg) const;
167 void print(std::ostream &OS) const;
171 MVT::ValueType getIntrinsicType(Record *R) const;
172 TreePatternNode *ParseTreePattern(DagInit *DI);
177 /// InstrSelectorEmitter - The top-level class which coordinates construction
178 /// and emission of the instruction selector.
180 class DAGISelEmitter : public TableGenBackend {
181 RecordKeeper &Records;
182 CodeGenTarget Target;
184 std::map<Record*, TreePattern*> PatternFragments;
185 std::vector<TreePattern*> Instructions;
187 DAGISelEmitter(RecordKeeper &R) : Records(R) {}
189 // run - Output the isel, returning true on failure.
190 void run(std::ostream &OS);
192 TreePattern *getPatternFragment(Record *R) const {
193 assert(PatternFragments.count(R) && "Invalid pattern fragment request!");
194 return PatternFragments.find(R)->second;
198 void ParseAndResolvePatternFragments(std::ostream &OS);
199 void ParseAndResolveInstructions();
200 void EmitInstructionSelector(std::ostream &OS);
203 } // End llvm namespace