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; |
Chris Lattner | 3270760 | 2005-09-08 23:22:48 +0000 | [diff] [blame^] | 24 | class SDNodeInfo; |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 25 | class TreePattern; |
Chris Lattner | 3270760 | 2005-09-08 23:22:48 +0000 | [diff] [blame^] | 26 | class TreePatternNode; |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 27 | class DAGISelEmitter; |
Chris Lattner | ca559d0 | 2005-09-08 21:03:01 +0000 | [diff] [blame] | 28 | |
Chris Lattner | 33c92e9 | 2005-09-08 21:27:15 +0000 | [diff] [blame] | 29 | /// SDTypeConstraint - This is a discriminated union of constraints, |
| 30 | /// corresponding to the SDTypeConstraint tablegen class in Target.td. |
| 31 | struct SDTypeConstraint { |
| 32 | SDTypeConstraint(Record *R); |
| 33 | |
| 34 | unsigned OperandNo; // The operand # this constraint applies to. |
| 35 | enum { |
| 36 | SDTCisVT, SDTCisInt, SDTCisFP, SDTCisSameAs, SDTCisVTSmallerThanOp |
| 37 | } ConstraintType; |
| 38 | |
| 39 | union { // The discriminated union. |
| 40 | struct { |
| 41 | MVT::ValueType VT; |
| 42 | } SDTCisVT_Info; |
| 43 | struct { |
| 44 | unsigned OtherOperandNum; |
| 45 | } SDTCisSameAs_Info; |
| 46 | struct { |
| 47 | unsigned OtherOperandNum; |
| 48 | } SDTCisVTSmallerThanOp_Info; |
| 49 | } x; |
Chris Lattner | 3270760 | 2005-09-08 23:22:48 +0000 | [diff] [blame^] | 50 | |
| 51 | /// ApplyTypeConstraint - Given a node in a pattern, apply this type |
| 52 | /// constraint to the nodes operands. This returns true if it makes a |
| 53 | /// change, false otherwise. If a type contradiction is found, throw an |
| 54 | /// exception. |
| 55 | bool ApplyTypeConstraint(TreePatternNode *N, const SDNodeInfo &NodeInfo, |
| 56 | TreePattern &TP) const; |
| 57 | |
| 58 | /// getOperandNum - Return the node corresponding to operand #OpNo in tree |
| 59 | /// N, which has NumResults results. |
| 60 | TreePatternNode *getOperandNum(unsigned OpNo, TreePatternNode *N, |
| 61 | unsigned NumResults) const; |
Chris Lattner | 33c92e9 | 2005-09-08 21:27:15 +0000 | [diff] [blame] | 62 | }; |
| 63 | |
Chris Lattner | ca559d0 | 2005-09-08 21:03:01 +0000 | [diff] [blame] | 64 | /// SDNodeInfo - One of these records is created for each SDNode instance in |
| 65 | /// the target .td file. This represents the various dag nodes we will be |
| 66 | /// processing. |
| 67 | class SDNodeInfo { |
| 68 | Record *Def; |
| 69 | std::string EnumName; |
| 70 | std::string SDClassName; |
Chris Lattner | 3270760 | 2005-09-08 23:22:48 +0000 | [diff] [blame^] | 71 | unsigned NumResults; |
| 72 | int NumOperands; |
Chris Lattner | 33c92e9 | 2005-09-08 21:27:15 +0000 | [diff] [blame] | 73 | std::vector<SDTypeConstraint> TypeConstraints; |
Chris Lattner | ca559d0 | 2005-09-08 21:03:01 +0000 | [diff] [blame] | 74 | public: |
| 75 | SDNodeInfo(Record *R); // Parse the specified record. |
| 76 | |
Chris Lattner | 3270760 | 2005-09-08 23:22:48 +0000 | [diff] [blame^] | 77 | unsigned getNumResults() const { return NumResults; } |
Chris Lattner | 33c92e9 | 2005-09-08 21:27:15 +0000 | [diff] [blame] | 78 | int getNumOperands() const { return NumOperands; } |
Chris Lattner | ca559d0 | 2005-09-08 21:03:01 +0000 | [diff] [blame] | 79 | Record *getRecord() const { return Def; } |
| 80 | const std::string &getEnumName() const { return EnumName; } |
| 81 | const std::string &getSDClassName() const { return SDClassName; } |
Chris Lattner | 33c92e9 | 2005-09-08 21:27:15 +0000 | [diff] [blame] | 82 | |
Chris Lattner | 3270760 | 2005-09-08 23:22:48 +0000 | [diff] [blame^] | 83 | const std::vector<SDTypeConstraint> &getTypeConstraints() const { |
Chris Lattner | 33c92e9 | 2005-09-08 21:27:15 +0000 | [diff] [blame] | 84 | return TypeConstraints; |
| 85 | } |
Chris Lattner | 3270760 | 2005-09-08 23:22:48 +0000 | [diff] [blame^] | 86 | |
| 87 | /// ApplyTypeConstraints - Given a node in a pattern, apply the type |
| 88 | /// constraints for this node to the operands of the node. This returns |
| 89 | /// true if it makes a change, false otherwise. If a type contradiction is |
| 90 | /// found, throw an exception. |
| 91 | bool ApplyTypeConstraints(TreePatternNode *N, TreePattern &TP) const { |
| 92 | bool MadeChange = false; |
| 93 | for (unsigned i = 0, e = TypeConstraints.size(); i != e; ++i) |
| 94 | MadeChange |= TypeConstraints[i].ApplyTypeConstraint(N, *this, TP); |
| 95 | return MadeChange; |
| 96 | } |
Chris Lattner | ca559d0 | 2005-09-08 21:03:01 +0000 | [diff] [blame] | 97 | }; |
Chris Lattner | 4a24c64 | 2005-09-03 01:14:03 +0000 | [diff] [blame] | 98 | |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 99 | /// FIXME: TreePatternNode's can be shared in some cases (due to dag-shaped |
| 100 | /// patterns), and as such should be ref counted. We currently just leak all |
| 101 | /// TreePatternNode objects! |
| 102 | class TreePatternNode { |
| 103 | /// The inferred type for this node, or MVT::LAST_VALUETYPE if it hasn't |
| 104 | /// been determined yet. |
| 105 | MVT::ValueType Ty; |
| 106 | |
| 107 | /// Operator - The Record for the operator if this is an interior node (not |
| 108 | /// a leaf). |
| 109 | Record *Operator; |
| 110 | |
| 111 | /// Val - The init value (e.g. the "GPRC" record, or "7") for a leaf. |
| 112 | /// |
| 113 | Init *Val; |
| 114 | |
| 115 | /// Name - The name given to this node with the :$foo notation. |
| 116 | /// |
| 117 | std::string Name; |
| 118 | |
| 119 | /// PredicateFn - The predicate function to execute on this node to check |
| 120 | /// for a match. If this string is empty, no predicate is involved. |
| 121 | std::string PredicateFn; |
| 122 | |
| 123 | std::vector<TreePatternNode*> Children; |
| 124 | public: |
| 125 | TreePatternNode(Record *Op, const std::vector<TreePatternNode*> &Ch) |
| 126 | : Ty(MVT::LAST_VALUETYPE), Operator(Op), Val(0), Children(Ch) {} |
| 127 | TreePatternNode(Init *val) // leaf ctor |
| 128 | : Ty(MVT::LAST_VALUETYPE), Operator(0), Val(val) {} |
| 129 | ~TreePatternNode(); |
| 130 | |
| 131 | const std::string &getName() const { return Name; } |
| 132 | void setName(const std::string &N) { Name = N; } |
| 133 | |
| 134 | bool isLeaf() const { return Val != 0; } |
Chris Lattner | 3270760 | 2005-09-08 23:22:48 +0000 | [diff] [blame^] | 135 | bool hasTypeSet() const { return Ty != MVT::LAST_VALUETYPE; } |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 136 | MVT::ValueType getType() const { return Ty; } |
| 137 | void setType(MVT::ValueType VT) { Ty = VT; } |
| 138 | |
| 139 | Init *getLeafValue() const { assert(isLeaf()); return Val; } |
| 140 | Record *getOperator() const { assert(!isLeaf()); return Operator; } |
| 141 | |
| 142 | unsigned getNumChildren() const { return Children.size(); } |
| 143 | TreePatternNode *getChild(unsigned N) const { return Children[N]; } |
| 144 | void setChild(unsigned i, TreePatternNode *N) { |
| 145 | Children[i] = N; |
| 146 | } |
| 147 | |
| 148 | const std::string &getPredicateFn() const { return PredicateFn; } |
| 149 | void setPredicateFn(const std::string &Fn) { PredicateFn = Fn; } |
| 150 | |
| 151 | void print(std::ostream &OS) const; |
| 152 | void dump() const; |
| 153 | |
| 154 | public: // Higher level manipulation routines. |
| 155 | |
| 156 | /// clone - Return a new copy of this tree. |
| 157 | /// |
| 158 | TreePatternNode *clone() const; |
| 159 | |
Chris Lattner | 3270760 | 2005-09-08 23:22:48 +0000 | [diff] [blame^] | 160 | /// SubstituteFormalArguments - Replace the formal arguments in this tree |
| 161 | /// with actual values specified by ArgMap. |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 162 | void SubstituteFormalArguments(std::map<std::string, |
| 163 | TreePatternNode*> &ArgMap); |
| 164 | |
| 165 | /// InlinePatternFragments - If this pattern refers to any pattern |
| 166 | /// fragments, inline them into place, giving us a pattern without any |
| 167 | /// PatFrag references. |
| 168 | TreePatternNode *InlinePatternFragments(TreePattern &TP); |
Chris Lattner | 3270760 | 2005-09-08 23:22:48 +0000 | [diff] [blame^] | 169 | |
| 170 | /// ApplyTypeConstraints - Apply all of the type constraints relevent to |
| 171 | /// this node and its children in the tree. This returns true if it makes a |
| 172 | /// change, false otherwise. If a type contradiction is found, throw an |
| 173 | /// exception. |
| 174 | bool ApplyTypeConstraints(TreePattern &TP); |
| 175 | |
| 176 | /// UpdateNodeType - Set the node type of N to VT if VT contains |
| 177 | /// information. If N already contains a conflicting type, then throw an |
| 178 | /// exception. This returns true if any information was updated. |
| 179 | /// |
| 180 | bool UpdateNodeType(MVT::ValueType VT, TreePattern &TP); |
| 181 | |
| 182 | /// ContainsUnresolvedType - Return true if this tree contains any |
| 183 | /// unresolved types. |
| 184 | bool ContainsUnresolvedType() const { |
| 185 | if (Ty == MVT::LAST_VALUETYPE) return true; |
| 186 | for (unsigned i = 0, e = getNumChildren(); i != e; ++i) |
| 187 | if (getChild(i)->ContainsUnresolvedType()) return true; |
| 188 | return false; |
| 189 | } |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 190 | }; |
| 191 | |
| 192 | |
| 193 | /// TreePattern - Represent a pattern of one form or another. Currently, two |
| 194 | /// types of patterns are possible: Instructions and PatFrags. |
| 195 | /// |
| 196 | class TreePattern { |
| 197 | public: |
| 198 | enum PatternType { |
| 199 | PatFrag, Instruction |
| 200 | }; |
| 201 | private: |
| 202 | /// PTy - The type of pattern this is. |
| 203 | /// |
| 204 | PatternType PTy; |
| 205 | |
| 206 | /// Trees - The list of pattern trees which corresponds to this pattern. |
| 207 | /// Note that PatFrag's only have a single tree. |
| 208 | /// |
| 209 | std::vector<TreePatternNode*> Trees; |
| 210 | |
| 211 | /// TheRecord - The actual TableGen record corresponding to this pattern. |
| 212 | /// |
| 213 | Record *TheRecord; |
| 214 | |
| 215 | /// Args - This is a list of all of the arguments to this pattern (for |
| 216 | /// PatFrag patterns), which are the 'node' markers in this pattern. |
| 217 | std::vector<std::string> Args; |
| 218 | |
| 219 | /// ISE - the DAG isel emitter coordinating this madness. |
| 220 | /// |
| 221 | DAGISelEmitter &ISE; |
| 222 | public: |
| 223 | |
| 224 | /// TreePattern constructor - Parse the specified DagInits into the |
| 225 | /// current record. |
| 226 | TreePattern(PatternType pty, Record *TheRec, |
| 227 | const std::vector<DagInit *> &RawPat, DAGISelEmitter &ise); |
| 228 | |
| 229 | /// getPatternType - Return what flavor of Record this pattern originated from |
| 230 | /// |
| 231 | PatternType getPatternType() const { return PTy; } |
| 232 | |
| 233 | /// getTrees - Return the tree patterns which corresponds to this pattern. |
| 234 | /// |
| 235 | const std::vector<TreePatternNode*> &getTrees() const { return Trees; } |
| 236 | |
| 237 | /// getRecord - Return the actual TableGen record corresponding to this |
| 238 | /// pattern. |
| 239 | /// |
| 240 | Record *getRecord() const { return TheRecord; } |
| 241 | |
| 242 | unsigned getNumArgs() const { return Args.size(); } |
| 243 | const std::string &getArgName(unsigned i) const { |
| 244 | assert(i < Args.size() && "Argument reference out of range!"); |
| 245 | return Args[i]; |
| 246 | } |
| 247 | |
| 248 | DAGISelEmitter &getDAGISelEmitter() const { return ISE; } |
| 249 | |
| 250 | /// InlinePatternFragments - If this pattern refers to any pattern |
| 251 | /// fragments, inline them into place, giving us a pattern without any |
| 252 | /// PatFrag references. |
| 253 | void InlinePatternFragments() { |
| 254 | for (unsigned i = 0, e = Trees.size(); i != e; ++i) |
| 255 | Trees[i] = Trees[i]->InlinePatternFragments(*this); |
| 256 | } |
| 257 | |
Chris Lattner | 3270760 | 2005-09-08 23:22:48 +0000 | [diff] [blame^] | 258 | /// InferAllTypes - Infer/propagate as many types throughout the expression |
| 259 | /// patterns as possible. Return true if all types are infered, false |
| 260 | /// otherwise. Throw an exception if a type contradiction is found. |
| 261 | bool InferAllTypes(); |
| 262 | |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 263 | /// error - Throw an exception, prefixing it with information about this |
| 264 | /// pattern. |
| 265 | void error(const std::string &Msg) const; |
| 266 | |
| 267 | void print(std::ostream &OS) const; |
| 268 | void dump() const; |
| 269 | |
| 270 | private: |
| 271 | MVT::ValueType getIntrinsicType(Record *R) const; |
| 272 | TreePatternNode *ParseTreePattern(DagInit *DI); |
| 273 | }; |
| 274 | |
| 275 | |
| 276 | |
Chris Lattner | 4a24c64 | 2005-09-03 01:14:03 +0000 | [diff] [blame] | 277 | /// InstrSelectorEmitter - The top-level class which coordinates construction |
| 278 | /// and emission of the instruction selector. |
| 279 | /// |
| 280 | class DAGISelEmitter : public TableGenBackend { |
| 281 | RecordKeeper &Records; |
| 282 | CodeGenTarget Target; |
| 283 | |
Chris Lattner | ca559d0 | 2005-09-08 21:03:01 +0000 | [diff] [blame] | 284 | std::map<Record*, SDNodeInfo> SDNodes; |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 285 | std::map<Record*, TreePattern*> PatternFragments; |
| 286 | std::vector<TreePattern*> Instructions; |
Chris Lattner | 4a24c64 | 2005-09-03 01:14:03 +0000 | [diff] [blame] | 287 | public: |
| 288 | DAGISelEmitter(RecordKeeper &R) : Records(R) {} |
| 289 | |
| 290 | // run - Output the isel, returning true on failure. |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 291 | void run(std::ostream &OS); |
Chris Lattner | ca559d0 | 2005-09-08 21:03:01 +0000 | [diff] [blame] | 292 | |
| 293 | const SDNodeInfo &getSDNodeInfo(Record *R) const { |
| 294 | assert(SDNodes.count(R) && "Unknown node!"); |
| 295 | return SDNodes.find(R)->second; |
| 296 | } |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 297 | |
| 298 | TreePattern *getPatternFragment(Record *R) const { |
| 299 | assert(PatternFragments.count(R) && "Invalid pattern fragment request!"); |
| 300 | return PatternFragments.find(R)->second; |
| 301 | } |
| 302 | |
| 303 | private: |
Chris Lattner | ca559d0 | 2005-09-08 21:03:01 +0000 | [diff] [blame] | 304 | void ParseNodeInfo(); |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 305 | void ParseAndResolvePatternFragments(std::ostream &OS); |
| 306 | void ParseAndResolveInstructions(); |
| 307 | void EmitInstructionSelector(std::ostream &OS); |
Chris Lattner | 4a24c64 | 2005-09-03 01:14:03 +0000 | [diff] [blame] | 308 | }; |
| 309 | |
| 310 | } // End llvm namespace |
| 311 | |
| 312 | #endif |