Chris Lattner | 4a24c64 | 2005-09-03 01:14:03 +0000 | [diff] [blame] | 1 | //===- DAGISelEmitter.h - Generate an instruction selector ------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 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. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This tablegen backend emits a DAG instruction selector. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #ifndef DAGISEL_EMITTER_H |
| 15 | #define DAGISEL_EMITTER_H |
| 16 | |
| 17 | #include "TableGenBackend.h" |
| 18 | #include "CodeGenTarget.h" |
| 19 | |
| 20 | namespace llvm { |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 21 | class Record; |
| 22 | class Init; |
| 23 | class DagInit; |
| 24 | class TreePattern; |
| 25 | class DAGISelEmitter; |
Chris Lattner | 4a24c64 | 2005-09-03 01:14:03 +0000 | [diff] [blame] | 26 | |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 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. |
| 33 | MVT::ValueType Ty; |
| 34 | |
| 35 | /// Operator - The Record for the operator if this is an interior node (not |
| 36 | /// a leaf). |
| 37 | Record *Operator; |
| 38 | |
| 39 | /// Val - The init value (e.g. the "GPRC" record, or "7") for a leaf. |
| 40 | /// |
| 41 | Init *Val; |
| 42 | |
| 43 | /// Name - The name given to this node with the :$foo notation. |
| 44 | /// |
| 45 | std::string Name; |
| 46 | |
| 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; |
| 50 | |
| 51 | std::vector<TreePatternNode*> Children; |
| 52 | public: |
| 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) {} |
| 57 | ~TreePatternNode(); |
| 58 | |
| 59 | const std::string &getName() const { return Name; } |
| 60 | void setName(const std::string &N) { Name = N; } |
| 61 | |
| 62 | bool isLeaf() const { return Val != 0; } |
| 63 | MVT::ValueType getType() const { return Ty; } |
| 64 | void setType(MVT::ValueType VT) { Ty = VT; } |
| 65 | |
| 66 | Init *getLeafValue() const { assert(isLeaf()); return Val; } |
| 67 | Record *getOperator() const { assert(!isLeaf()); return Operator; } |
| 68 | |
| 69 | unsigned getNumChildren() const { return Children.size(); } |
| 70 | TreePatternNode *getChild(unsigned N) const { return Children[N]; } |
| 71 | void setChild(unsigned i, TreePatternNode *N) { |
| 72 | Children[i] = N; |
| 73 | } |
| 74 | |
| 75 | const std::string &getPredicateFn() const { return PredicateFn; } |
| 76 | void setPredicateFn(const std::string &Fn) { PredicateFn = Fn; } |
| 77 | |
| 78 | void print(std::ostream &OS) const; |
| 79 | void dump() const; |
| 80 | |
| 81 | public: // Higher level manipulation routines. |
| 82 | |
| 83 | /// clone - Return a new copy of this tree. |
| 84 | /// |
| 85 | TreePatternNode *clone() const; |
| 86 | |
| 87 | void SubstituteFormalArguments(std::map<std::string, |
| 88 | TreePatternNode*> &ArgMap); |
| 89 | |
| 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); |
| 94 | |
| 95 | }; |
| 96 | |
| 97 | |
| 98 | /// TreePattern - Represent a pattern of one form or another. Currently, two |
| 99 | /// types of patterns are possible: Instructions and PatFrags. |
| 100 | /// |
| 101 | class TreePattern { |
| 102 | public: |
| 103 | enum PatternType { |
| 104 | PatFrag, Instruction |
| 105 | }; |
| 106 | private: |
| 107 | /// PTy - The type of pattern this is. |
| 108 | /// |
| 109 | PatternType PTy; |
| 110 | |
| 111 | /// Trees - The list of pattern trees which corresponds to this pattern. |
| 112 | /// Note that PatFrag's only have a single tree. |
| 113 | /// |
| 114 | std::vector<TreePatternNode*> Trees; |
| 115 | |
| 116 | /// TheRecord - The actual TableGen record corresponding to this pattern. |
| 117 | /// |
| 118 | Record *TheRecord; |
| 119 | |
| 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; |
| 123 | |
| 124 | /// ISE - the DAG isel emitter coordinating this madness. |
| 125 | /// |
| 126 | DAGISelEmitter &ISE; |
| 127 | public: |
| 128 | |
| 129 | /// TreePattern constructor - Parse the specified DagInits into the |
| 130 | /// current record. |
| 131 | TreePattern(PatternType pty, Record *TheRec, |
| 132 | const std::vector<DagInit *> &RawPat, DAGISelEmitter &ise); |
| 133 | |
| 134 | /// getPatternType - Return what flavor of Record this pattern originated from |
| 135 | /// |
| 136 | PatternType getPatternType() const { return PTy; } |
| 137 | |
| 138 | /// getTrees - Return the tree patterns which corresponds to this pattern. |
| 139 | /// |
| 140 | const std::vector<TreePatternNode*> &getTrees() const { return Trees; } |
| 141 | |
| 142 | /// getRecord - Return the actual TableGen record corresponding to this |
| 143 | /// pattern. |
| 144 | /// |
| 145 | Record *getRecord() const { return TheRecord; } |
| 146 | |
| 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!"); |
| 150 | return Args[i]; |
| 151 | } |
| 152 | |
| 153 | DAGISelEmitter &getDAGISelEmitter() const { return ISE; } |
| 154 | |
| 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); |
| 161 | } |
| 162 | |
| 163 | /// error - Throw an exception, prefixing it with information about this |
| 164 | /// pattern. |
| 165 | void error(const std::string &Msg) const; |
| 166 | |
| 167 | void print(std::ostream &OS) const; |
| 168 | void dump() const; |
| 169 | |
| 170 | private: |
| 171 | MVT::ValueType getIntrinsicType(Record *R) const; |
| 172 | TreePatternNode *ParseTreePattern(DagInit *DI); |
| 173 | }; |
| 174 | |
| 175 | |
| 176 | |
Chris Lattner | 4a24c64 | 2005-09-03 01:14:03 +0000 | [diff] [blame] | 177 | /// InstrSelectorEmitter - The top-level class which coordinates construction |
| 178 | /// and emission of the instruction selector. |
| 179 | /// |
| 180 | class DAGISelEmitter : public TableGenBackend { |
| 181 | RecordKeeper &Records; |
| 182 | CodeGenTarget Target; |
| 183 | |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 184 | std::map<Record*, TreePattern*> PatternFragments; |
| 185 | std::vector<TreePattern*> Instructions; |
Chris Lattner | 4a24c64 | 2005-09-03 01:14:03 +0000 | [diff] [blame] | 186 | public: |
| 187 | DAGISelEmitter(RecordKeeper &R) : Records(R) {} |
| 188 | |
| 189 | // run - Output the isel, returning true on failure. |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 190 | void run(std::ostream &OS); |
| 191 | |
| 192 | TreePattern *getPatternFragment(Record *R) const { |
| 193 | assert(PatternFragments.count(R) && "Invalid pattern fragment request!"); |
| 194 | return PatternFragments.find(R)->second; |
| 195 | } |
| 196 | |
| 197 | private: |
| 198 | void ParseAndResolvePatternFragments(std::ostream &OS); |
| 199 | void ParseAndResolveInstructions(); |
| 200 | void EmitInstructionSelector(std::ostream &OS); |
Chris Lattner | 4a24c64 | 2005-09-03 01:14:03 +0000 | [diff] [blame] | 201 | }; |
| 202 | |
| 203 | } // End llvm namespace |
| 204 | |
| 205 | #endif |