Chris Lattner | fe71893 | 2008-01-06 01:10:31 +0000 | [diff] [blame] | 1 | //===- CodeGenDAGPatterns.h - Read DAG patterns from .td file ---*- C++ -*-===// |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Chris Lattner | fe71893 | 2008-01-06 01:10:31 +0000 | [diff] [blame] | 10 | // This file declares the CodeGenDAGPatterns class, which is used to read and |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 11 | // represent the patterns present in a .td file for instructions. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #ifndef CODEGEN_DAGPATTERNS_H |
| 16 | #define CODEGEN_DAGPATTERNS_H |
| 17 | |
Scott Michel | 327d065 | 2008-03-05 17:49:05 +0000 | [diff] [blame] | 18 | #include <set> |
Dan Gohman | 0540e17 | 2008-10-15 06:17:21 +0000 | [diff] [blame] | 19 | #include <algorithm> |
| 20 | #include <vector> |
Scott Michel | 327d065 | 2008-03-05 17:49:05 +0000 | [diff] [blame] | 21 | |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 22 | #include "CodeGenTarget.h" |
| 23 | #include "CodeGenIntrinsics.h" |
| 24 | |
| 25 | namespace llvm { |
| 26 | class Record; |
| 27 | struct Init; |
| 28 | class ListInit; |
| 29 | class DagInit; |
| 30 | class SDNodeInfo; |
| 31 | class TreePattern; |
| 32 | class TreePatternNode; |
Chris Lattner | fe71893 | 2008-01-06 01:10:31 +0000 | [diff] [blame] | 33 | class CodeGenDAGPatterns; |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 34 | class ComplexPattern; |
| 35 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 36 | /// EEVT::DAGISelGenValueType - These are some extended forms of |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 37 | /// MVT::SimpleValueType that we use as lattice values during type inference. |
Bob Wilson | cdfa01b | 2009-08-29 05:53:25 +0000 | [diff] [blame^] | 38 | /// The existing MVT iAny, fAny and vAny types suffice to represent |
| 39 | /// arbitrary integer, floating-point, and vector types, so only an unknown |
| 40 | /// value is needed. |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 41 | namespace EEVT { |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 42 | enum DAGISelGenValueType { |
Bob Wilson | cdfa01b | 2009-08-29 05:53:25 +0000 | [diff] [blame^] | 43 | isUnknown = MVT::LAST_VALUETYPE |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 44 | }; |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 45 | |
Bob Wilson | 36e3e66 | 2009-08-12 22:30:59 +0000 | [diff] [blame] | 46 | /// isExtIntegerInVTs - Return true if the specified extended value type |
Bob Wilson | cdfa01b | 2009-08-29 05:53:25 +0000 | [diff] [blame^] | 47 | /// vector contains iAny or an integer value type. |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 48 | bool isExtIntegerInVTs(const std::vector<unsigned char> &EVTs); |
| 49 | |
Bob Wilson | 36e3e66 | 2009-08-12 22:30:59 +0000 | [diff] [blame] | 50 | /// isExtFloatingPointInVTs - Return true if the specified extended value |
Bob Wilson | cdfa01b | 2009-08-29 05:53:25 +0000 | [diff] [blame^] | 51 | /// type vector contains fAny or a FP value type. |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 52 | bool isExtFloatingPointInVTs(const std::vector<unsigned char> &EVTs); |
Bob Wilson | 36e3e66 | 2009-08-12 22:30:59 +0000 | [diff] [blame] | 53 | |
| 54 | /// isExtVectorinVTs - Return true if the specified extended value type |
Bob Wilson | cdfa01b | 2009-08-29 05:53:25 +0000 | [diff] [blame^] | 55 | /// vector contains vAny or a vector value type. |
Bob Wilson | 36e3e66 | 2009-08-12 22:30:59 +0000 | [diff] [blame] | 56 | bool isExtVectorInVTs(const std::vector<unsigned char> &EVTs); |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 57 | } |
| 58 | |
Scott Michel | 327d065 | 2008-03-05 17:49:05 +0000 | [diff] [blame] | 59 | /// Set type used to track multiply used variables in patterns |
| 60 | typedef std::set<std::string> MultipleUseVarSet; |
| 61 | |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 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); |
| 66 | |
| 67 | unsigned OperandNo; // The operand # this constraint applies to. |
| 68 | enum { |
Bob Wilson | 36e3e66 | 2009-08-12 22:30:59 +0000 | [diff] [blame] | 69 | SDTCisVT, SDTCisPtrTy, SDTCisInt, SDTCisFP, SDTCisVec, SDTCisSameAs, |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 70 | SDTCisVTSmallerThanOp, SDTCisOpSmallerThanOp, SDTCisEltOfVec |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 71 | } ConstraintType; |
| 72 | |
| 73 | union { // The discriminated union. |
| 74 | struct { |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 75 | unsigned char VT; |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 76 | } SDTCisVT_Info; |
| 77 | struct { |
| 78 | unsigned OtherOperandNum; |
| 79 | } SDTCisSameAs_Info; |
| 80 | struct { |
| 81 | unsigned OtherOperandNum; |
| 82 | } SDTCisVTSmallerThanOp_Info; |
| 83 | struct { |
| 84 | unsigned BigOperandNum; |
| 85 | } SDTCisOpSmallerThanOp_Info; |
| 86 | struct { |
| 87 | unsigned OtherOperandNum; |
Nate Begeman | b5af334 | 2008-02-09 01:37:05 +0000 | [diff] [blame] | 88 | } SDTCisEltOfVec_Info; |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 89 | } x; |
| 90 | |
| 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 |
| 94 | /// exception. |
| 95 | bool ApplyTypeConstraint(TreePatternNode *N, const SDNodeInfo &NodeInfo, |
| 96 | TreePattern &TP) const; |
| 97 | |
| 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; |
| 102 | }; |
| 103 | |
| 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 |
| 106 | /// processing. |
| 107 | class SDNodeInfo { |
| 108 | Record *Def; |
| 109 | std::string EnumName; |
| 110 | std::string SDClassName; |
| 111 | unsigned Properties; |
| 112 | unsigned NumResults; |
| 113 | int NumOperands; |
| 114 | std::vector<SDTypeConstraint> TypeConstraints; |
| 115 | public: |
| 116 | SDNodeInfo(Record *R); // Parse the specified record. |
| 117 | |
| 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; } |
| 123 | |
| 124 | const std::vector<SDTypeConstraint> &getTypeConstraints() const { |
| 125 | return TypeConstraints; |
| 126 | } |
| 127 | |
| 128 | /// hasProperty - Return true if this node has the specified property. |
| 129 | /// |
| 130 | bool hasProperty(enum SDNP Prop) const { return Properties & (1 << Prop); } |
| 131 | |
| 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); |
| 140 | return MadeChange; |
| 141 | } |
| 142 | }; |
| 143 | |
| 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 { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 148 | /// The inferred type for this node, or EEVT::isUnknown if it hasn't |
Dan Gohman | e1ca7c4 | 2009-03-31 16:46:45 +0000 | [diff] [blame] | 149 | /// been determined yet. This is a std::vector because during inference |
| 150 | /// there may be multiple possible types. |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 151 | std::vector<unsigned char> Types; |
| 152 | |
| 153 | /// Operator - The Record for the operator if this is an interior node (not |
| 154 | /// a leaf). |
| 155 | Record *Operator; |
| 156 | |
| 157 | /// Val - The init value (e.g. the "GPRC" record, or "7") for a leaf. |
| 158 | /// |
| 159 | Init *Val; |
| 160 | |
| 161 | /// Name - The name given to this node with the :$foo notation. |
| 162 | /// |
| 163 | std::string Name; |
| 164 | |
Dan Gohman | 0540e17 | 2008-10-15 06:17:21 +0000 | [diff] [blame] | 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; |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 168 | |
| 169 | /// TransformFn - The transformation function to execute on this node before |
| 170 | /// it can be substituted into the resulting instruction on a pattern match. |
| 171 | Record *TransformFn; |
| 172 | |
| 173 | std::vector<TreePatternNode*> Children; |
| 174 | public: |
| 175 | TreePatternNode(Record *Op, const std::vector<TreePatternNode*> &Ch) |
| 176 | : Types(), Operator(Op), Val(0), TransformFn(0), |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 177 | Children(Ch) { Types.push_back(EEVT::isUnknown); } |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 178 | TreePatternNode(Init *val) // leaf ctor |
| 179 | : Types(), Operator(0), Val(val), TransformFn(0) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 180 | Types.push_back(EEVT::isUnknown); |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 181 | } |
| 182 | ~TreePatternNode(); |
| 183 | |
| 184 | const std::string &getName() const { return Name; } |
| 185 | void setName(const std::string &N) { Name = N; } |
| 186 | |
| 187 | bool isLeaf() const { return Val != 0; } |
| 188 | bool hasTypeSet() const { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 189 | return (Types[0] < MVT::LAST_VALUETYPE) || (Types[0] == MVT::iPTR) || |
| 190 | (Types[0] == MVT::iPTRAny); |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 191 | } |
| 192 | bool isTypeCompletelyUnknown() const { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 193 | return Types[0] == EEVT::isUnknown; |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 194 | } |
| 195 | bool isTypeDynamicallyResolved() const { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 196 | return (Types[0] == MVT::iPTR) || (Types[0] == MVT::iPTRAny); |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 197 | } |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 198 | MVT::SimpleValueType getTypeNum(unsigned Num) const { |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 199 | assert(hasTypeSet() && "Doesn't have a type yet!"); |
| 200 | assert(Types.size() > Num && "Type num out of range!"); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 201 | return (MVT::SimpleValueType)Types[Num]; |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 202 | } |
| 203 | unsigned char getExtTypeNum(unsigned Num) const { |
| 204 | assert(Types.size() > Num && "Extended type num out of range!"); |
| 205 | return Types[Num]; |
| 206 | } |
| 207 | const std::vector<unsigned char> &getExtTypes() const { return Types; } |
| 208 | void setTypes(const std::vector<unsigned char> &T) { Types = T; } |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 209 | void removeTypes() { Types = std::vector<unsigned char>(1, EEVT::isUnknown); } |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 210 | |
| 211 | Init *getLeafValue() const { assert(isLeaf()); return Val; } |
| 212 | Record *getOperator() const { assert(!isLeaf()); return Operator; } |
| 213 | |
| 214 | unsigned getNumChildren() const { return Children.size(); } |
| 215 | TreePatternNode *getChild(unsigned N) const { return Children[N]; } |
| 216 | void setChild(unsigned i, TreePatternNode *N) { |
| 217 | Children[i] = N; |
| 218 | } |
Chris Lattner | e67bde5 | 2008-01-06 05:36:50 +0000 | [diff] [blame] | 219 | |
Dan Gohman | 0540e17 | 2008-10-15 06:17:21 +0000 | [diff] [blame] | 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!"); |
| 224 | PredicateFns = Fns; |
| 225 | } |
| 226 | void addPredicateFn(const std::string &Fn) { |
| 227 | assert(!Fn.empty() && "Empty predicate string!"); |
| 228 | if (std::find(PredicateFns.begin(), PredicateFns.end(), Fn) == |
| 229 | PredicateFns.end()) |
| 230 | PredicateFns.push_back(Fn); |
| 231 | } |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 232 | |
| 233 | Record *getTransformFn() const { return TransformFn; } |
| 234 | void setTransformFn(Record *Fn) { TransformFn = Fn; } |
| 235 | |
Chris Lattner | e67bde5 | 2008-01-06 05:36:50 +0000 | [diff] [blame] | 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; |
Evan Cheng | 6bd9567 | 2008-06-16 20:29:38 +0000 | [diff] [blame] | 239 | |
| 240 | /// isCommutativeIntrinsic - Return true if the node is an intrinsic which is |
| 241 | /// marked isCommutative. |
| 242 | bool isCommutativeIntrinsic(const CodeGenDAGPatterns &CDP) const; |
Chris Lattner | e67bde5 | 2008-01-06 05:36:50 +0000 | [diff] [blame] | 243 | |
Daniel Dunbar | 1a55180 | 2009-07-03 00:10:29 +0000 | [diff] [blame] | 244 | void print(raw_ostream &OS) const; |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 245 | void dump() const; |
| 246 | |
| 247 | public: // Higher level manipulation routines. |
| 248 | |
| 249 | /// clone - Return a new copy of this tree. |
| 250 | /// |
| 251 | TreePatternNode *clone() const; |
| 252 | |
| 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. |
Scott Michel | 327d065 | 2008-03-05 17:49:05 +0000 | [diff] [blame] | 257 | bool isIsomorphicTo(const TreePatternNode *N, |
| 258 | const MultipleUseVarSet &DepVars) const; |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 259 | |
| 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); |
| 264 | |
| 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); |
| 269 | |
Bob Wilson | 6c01ca9 | 2009-01-05 17:23:09 +0000 | [diff] [blame] | 270 | /// ApplyTypeConstraints - Apply all of the type constraints relevant to |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 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 |
| 273 | /// exception. |
| 274 | bool ApplyTypeConstraints(TreePattern &TP, bool NotRegisters); |
| 275 | |
| 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. |
| 279 | /// |
| 280 | bool UpdateNodeType(const std::vector<unsigned char> &ExtVTs, |
| 281 | TreePattern &TP); |
| 282 | bool UpdateNodeType(unsigned char ExtVT, TreePattern &TP) { |
| 283 | std::vector<unsigned char> ExtVTs(1, ExtVT); |
| 284 | return UpdateNodeType(ExtVTs, TP); |
| 285 | } |
| 286 | |
| 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; |
| 293 | return false; |
| 294 | } |
| 295 | |
| 296 | /// canPatternMatch - If it is impossible for this pattern to match on this |
| 297 | /// target, fill in Reason and return false. Otherwise, return true. |
Dan Gohman | ee4fa19 | 2008-04-03 00:02:49 +0000 | [diff] [blame] | 298 | bool canPatternMatch(std::string &Reason, const CodeGenDAGPatterns &CDP); |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 299 | }; |
| 300 | |
| 301 | |
| 302 | /// TreePattern - Represent a pattern, used for instructions, pattern |
| 303 | /// fragments, etc. |
| 304 | /// |
| 305 | class TreePattern { |
| 306 | /// Trees - The list of pattern trees which corresponds to this pattern. |
| 307 | /// Note that PatFrag's only have a single tree. |
| 308 | /// |
| 309 | std::vector<TreePatternNode*> Trees; |
| 310 | |
| 311 | /// TheRecord - The actual TableGen record corresponding to this pattern. |
| 312 | /// |
| 313 | Record *TheRecord; |
| 314 | |
| 315 | /// Args - This is a list of all of the arguments to this pattern (for |
| 316 | /// PatFrag patterns), which are the 'node' markers in this pattern. |
| 317 | std::vector<std::string> Args; |
| 318 | |
| 319 | /// CDP - the top-level object coordinating this madness. |
| 320 | /// |
Chris Lattner | fe71893 | 2008-01-06 01:10:31 +0000 | [diff] [blame] | 321 | CodeGenDAGPatterns &CDP; |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 322 | |
| 323 | /// isInputPattern - True if this is an input pattern, something to match. |
| 324 | /// False if this is an output pattern, something to emit. |
| 325 | bool isInputPattern; |
| 326 | public: |
| 327 | |
| 328 | /// TreePattern constructor - Parse the specified DagInits into the |
| 329 | /// current record. |
| 330 | TreePattern(Record *TheRec, ListInit *RawPat, bool isInput, |
Chris Lattner | fe71893 | 2008-01-06 01:10:31 +0000 | [diff] [blame] | 331 | CodeGenDAGPatterns &ise); |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 332 | TreePattern(Record *TheRec, DagInit *Pat, bool isInput, |
Chris Lattner | fe71893 | 2008-01-06 01:10:31 +0000 | [diff] [blame] | 333 | CodeGenDAGPatterns &ise); |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 334 | TreePattern(Record *TheRec, TreePatternNode *Pat, bool isInput, |
Chris Lattner | fe71893 | 2008-01-06 01:10:31 +0000 | [diff] [blame] | 335 | CodeGenDAGPatterns &ise); |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 336 | |
| 337 | /// getTrees - Return the tree patterns which corresponds to this pattern. |
| 338 | /// |
| 339 | const std::vector<TreePatternNode*> &getTrees() const { return Trees; } |
| 340 | unsigned getNumTrees() const { return Trees.size(); } |
| 341 | TreePatternNode *getTree(unsigned i) const { return Trees[i]; } |
| 342 | TreePatternNode *getOnlyTree() const { |
| 343 | assert(Trees.size() == 1 && "Doesn't have exactly one pattern!"); |
| 344 | return Trees[0]; |
| 345 | } |
| 346 | |
| 347 | /// getRecord - Return the actual TableGen record corresponding to this |
| 348 | /// pattern. |
| 349 | /// |
| 350 | Record *getRecord() const { return TheRecord; } |
| 351 | |
| 352 | unsigned getNumArgs() const { return Args.size(); } |
| 353 | const std::string &getArgName(unsigned i) const { |
| 354 | assert(i < Args.size() && "Argument reference out of range!"); |
| 355 | return Args[i]; |
| 356 | } |
| 357 | std::vector<std::string> &getArgList() { return Args; } |
| 358 | |
Chris Lattner | fe71893 | 2008-01-06 01:10:31 +0000 | [diff] [blame] | 359 | CodeGenDAGPatterns &getDAGPatterns() const { return CDP; } |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 360 | |
| 361 | /// InlinePatternFragments - If this pattern refers to any pattern |
| 362 | /// fragments, inline them into place, giving us a pattern without any |
| 363 | /// PatFrag references. |
| 364 | void InlinePatternFragments() { |
| 365 | for (unsigned i = 0, e = Trees.size(); i != e; ++i) |
| 366 | Trees[i] = Trees[i]->InlinePatternFragments(*this); |
| 367 | } |
| 368 | |
| 369 | /// InferAllTypes - Infer/propagate as many types throughout the expression |
Jim Grosbach | da4231f | 2009-03-26 16:17:51 +0000 | [diff] [blame] | 370 | /// patterns as possible. Return true if all types are inferred, false |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 371 | /// otherwise. Throw an exception if a type contradiction is found. |
| 372 | bool InferAllTypes(); |
| 373 | |
| 374 | /// error - Throw an exception, prefixing it with information about this |
| 375 | /// pattern. |
| 376 | void error(const std::string &Msg) const; |
| 377 | |
Daniel Dunbar | 1a55180 | 2009-07-03 00:10:29 +0000 | [diff] [blame] | 378 | void print(raw_ostream &OS) const; |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 379 | void dump() const; |
| 380 | |
| 381 | private: |
| 382 | TreePatternNode *ParseTreePattern(DagInit *DI); |
| 383 | }; |
| 384 | |
| 385 | /// DAGDefaultOperand - One of these is created for each PredicateOperand |
| 386 | /// or OptionalDefOperand that has a set ExecuteAlways / DefaultOps field. |
| 387 | struct DAGDefaultOperand { |
| 388 | std::vector<TreePatternNode*> DefaultOps; |
| 389 | }; |
| 390 | |
| 391 | class DAGInstruction { |
| 392 | TreePattern *Pattern; |
| 393 | std::vector<Record*> Results; |
| 394 | std::vector<Record*> Operands; |
| 395 | std::vector<Record*> ImpResults; |
| 396 | std::vector<Record*> ImpOperands; |
| 397 | TreePatternNode *ResultPattern; |
| 398 | public: |
| 399 | DAGInstruction(TreePattern *TP, |
| 400 | const std::vector<Record*> &results, |
| 401 | const std::vector<Record*> &operands, |
| 402 | const std::vector<Record*> &impresults, |
| 403 | const std::vector<Record*> &impoperands) |
| 404 | : Pattern(TP), Results(results), Operands(operands), |
| 405 | ImpResults(impresults), ImpOperands(impoperands), |
| 406 | ResultPattern(0) {} |
| 407 | |
Chris Lattner | f1ab4f1 | 2008-01-06 01:52:22 +0000 | [diff] [blame] | 408 | const TreePattern *getPattern() const { return Pattern; } |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 409 | unsigned getNumResults() const { return Results.size(); } |
| 410 | unsigned getNumOperands() const { return Operands.size(); } |
| 411 | unsigned getNumImpResults() const { return ImpResults.size(); } |
| 412 | unsigned getNumImpOperands() const { return ImpOperands.size(); } |
| 413 | const std::vector<Record*>& getImpResults() const { return ImpResults; } |
| 414 | |
| 415 | void setResultPattern(TreePatternNode *R) { ResultPattern = R; } |
| 416 | |
| 417 | Record *getResult(unsigned RN) const { |
| 418 | assert(RN < Results.size()); |
| 419 | return Results[RN]; |
| 420 | } |
| 421 | |
| 422 | Record *getOperand(unsigned ON) const { |
| 423 | assert(ON < Operands.size()); |
| 424 | return Operands[ON]; |
| 425 | } |
| 426 | |
| 427 | Record *getImpResult(unsigned RN) const { |
| 428 | assert(RN < ImpResults.size()); |
| 429 | return ImpResults[RN]; |
| 430 | } |
| 431 | |
| 432 | Record *getImpOperand(unsigned ON) const { |
| 433 | assert(ON < ImpOperands.size()); |
| 434 | return ImpOperands[ON]; |
| 435 | } |
| 436 | |
| 437 | TreePatternNode *getResultPattern() const { return ResultPattern; } |
| 438 | }; |
| 439 | |
Chris Lattner | fe71893 | 2008-01-06 01:10:31 +0000 | [diff] [blame] | 440 | /// PatternToMatch - Used by CodeGenDAGPatterns to keep tab of patterns |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 441 | /// processed to produce isel. |
| 442 | struct PatternToMatch { |
| 443 | PatternToMatch(ListInit *preds, |
| 444 | TreePatternNode *src, TreePatternNode *dst, |
| 445 | const std::vector<Record*> &dstregs, |
| 446 | unsigned complexity): |
| 447 | Predicates(preds), SrcPattern(src), DstPattern(dst), Dstregs(dstregs), |
| 448 | AddedComplexity(complexity) {}; |
| 449 | |
| 450 | ListInit *Predicates; // Top level predicate conditions to match. |
| 451 | TreePatternNode *SrcPattern; // Source pattern to match. |
| 452 | TreePatternNode *DstPattern; // Resulting pattern. |
| 453 | std::vector<Record*> Dstregs; // Physical register defs being matched. |
| 454 | unsigned AddedComplexity; // Add to matching pattern complexity. |
| 455 | |
| 456 | ListInit *getPredicates() const { return Predicates; } |
| 457 | TreePatternNode *getSrcPattern() const { return SrcPattern; } |
| 458 | TreePatternNode *getDstPattern() const { return DstPattern; } |
| 459 | const std::vector<Record*> &getDstRegs() const { return Dstregs; } |
| 460 | unsigned getAddedComplexity() const { return AddedComplexity; } |
Dan Gohman | 22bb311 | 2008-08-22 00:20:26 +0000 | [diff] [blame] | 461 | |
| 462 | std::string getPredicateCheck() const; |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 463 | }; |
| 464 | |
Daniel Dunbar | 6f5cc82 | 2009-08-23 09:47:37 +0000 | [diff] [blame] | 465 | // Deterministic comparison of Record*. |
| 466 | struct RecordPtrCmp { |
| 467 | bool operator()(const Record *LHS, const Record *RHS) const; |
| 468 | }; |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 469 | |
Chris Lattner | fe71893 | 2008-01-06 01:10:31 +0000 | [diff] [blame] | 470 | class CodeGenDAGPatterns { |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 471 | RecordKeeper &Records; |
| 472 | CodeGenTarget Target; |
| 473 | std::vector<CodeGenIntrinsic> Intrinsics; |
Dale Johannesen | 49de982 | 2009-02-05 01:49:45 +0000 | [diff] [blame] | 474 | std::vector<CodeGenIntrinsic> TgtIntrinsics; |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 475 | |
Daniel Dunbar | 6f5cc82 | 2009-08-23 09:47:37 +0000 | [diff] [blame] | 476 | std::map<Record*, SDNodeInfo, RecordPtrCmp> SDNodes; |
| 477 | std::map<Record*, std::pair<Record*, std::string>, RecordPtrCmp> SDNodeXForms; |
| 478 | std::map<Record*, ComplexPattern, RecordPtrCmp> ComplexPatterns; |
| 479 | std::map<Record*, TreePattern*, RecordPtrCmp> PatternFragments; |
| 480 | std::map<Record*, DAGDefaultOperand, RecordPtrCmp> DefaultOperands; |
| 481 | std::map<Record*, DAGInstruction, RecordPtrCmp> Instructions; |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 482 | |
| 483 | // Specific SDNode definitions: |
| 484 | Record *intrinsic_void_sdnode; |
| 485 | Record *intrinsic_w_chain_sdnode, *intrinsic_wo_chain_sdnode; |
| 486 | |
| 487 | /// PatternsToMatch - All of the things we are matching on the DAG. The first |
| 488 | /// value is the pattern to match, the second pattern is the result to |
| 489 | /// emit. |
| 490 | std::vector<PatternToMatch> PatternsToMatch; |
| 491 | public: |
Chris Lattner | fe71893 | 2008-01-06 01:10:31 +0000 | [diff] [blame] | 492 | CodeGenDAGPatterns(RecordKeeper &R); |
| 493 | ~CodeGenDAGPatterns(); |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 494 | |
Dan Gohman | ee4fa19 | 2008-04-03 00:02:49 +0000 | [diff] [blame] | 495 | CodeGenTarget &getTargetInfo() { return Target; } |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 496 | const CodeGenTarget &getTargetInfo() const { return Target; } |
| 497 | |
| 498 | Record *getSDNodeNamed(const std::string &Name) const; |
| 499 | |
| 500 | const SDNodeInfo &getSDNodeInfo(Record *R) const { |
| 501 | assert(SDNodes.count(R) && "Unknown node!"); |
| 502 | return SDNodes.find(R)->second; |
| 503 | } |
| 504 | |
Chris Lattner | 443e3f9 | 2008-01-05 22:54:53 +0000 | [diff] [blame] | 505 | // Node transformation lookups. |
| 506 | typedef std::pair<Record*, std::string> NodeXForm; |
| 507 | const NodeXForm &getSDNodeTransform(Record *R) const { |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 508 | assert(SDNodeXForms.count(R) && "Invalid transform!"); |
| 509 | return SDNodeXForms.find(R)->second; |
| 510 | } |
| 511 | |
Benjamin Kramer | 5b9e7ef | 2009-08-23 10:39:21 +0000 | [diff] [blame] | 512 | typedef std::map<Record*, NodeXForm, RecordPtrCmp>::const_iterator |
| 513 | nx_iterator; |
Chris Lattner | 443e3f9 | 2008-01-05 22:54:53 +0000 | [diff] [blame] | 514 | nx_iterator nx_begin() const { return SDNodeXForms.begin(); } |
| 515 | nx_iterator nx_end() const { return SDNodeXForms.end(); } |
| 516 | |
| 517 | |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 518 | const ComplexPattern &getComplexPattern(Record *R) const { |
| 519 | assert(ComplexPatterns.count(R) && "Unknown addressing mode!"); |
| 520 | return ComplexPatterns.find(R)->second; |
| 521 | } |
| 522 | |
| 523 | const CodeGenIntrinsic &getIntrinsic(Record *R) const { |
| 524 | for (unsigned i = 0, e = Intrinsics.size(); i != e; ++i) |
| 525 | if (Intrinsics[i].TheDef == R) return Intrinsics[i]; |
Dale Johannesen | 49de982 | 2009-02-05 01:49:45 +0000 | [diff] [blame] | 526 | for (unsigned i = 0, e = TgtIntrinsics.size(); i != e; ++i) |
| 527 | if (TgtIntrinsics[i].TheDef == R) return TgtIntrinsics[i]; |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 528 | assert(0 && "Unknown intrinsic!"); |
| 529 | abort(); |
| 530 | } |
| 531 | |
| 532 | const CodeGenIntrinsic &getIntrinsicInfo(unsigned IID) const { |
Dale Johannesen | 49de982 | 2009-02-05 01:49:45 +0000 | [diff] [blame] | 533 | if (IID-1 < Intrinsics.size()) |
| 534 | return Intrinsics[IID-1]; |
| 535 | if (IID-Intrinsics.size()-1 < TgtIntrinsics.size()) |
| 536 | return TgtIntrinsics[IID-Intrinsics.size()-1]; |
| 537 | assert(0 && "Bad intrinsic ID!"); |
Evan Cheng | 32c1a4c | 2009-02-09 03:07:24 +0000 | [diff] [blame] | 538 | abort(); |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 539 | } |
| 540 | |
| 541 | unsigned getIntrinsicID(Record *R) const { |
| 542 | for (unsigned i = 0, e = Intrinsics.size(); i != e; ++i) |
| 543 | if (Intrinsics[i].TheDef == R) return i; |
Dale Johannesen | 49de982 | 2009-02-05 01:49:45 +0000 | [diff] [blame] | 544 | for (unsigned i = 0, e = TgtIntrinsics.size(); i != e; ++i) |
| 545 | if (TgtIntrinsics[i].TheDef == R) return i + Intrinsics.size(); |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 546 | assert(0 && "Unknown intrinsic!"); |
| 547 | abort(); |
| 548 | } |
| 549 | |
| 550 | const DAGDefaultOperand &getDefaultOperand(Record *R) { |
| 551 | assert(DefaultOperands.count(R) &&"Isn't an analyzed default operand!"); |
| 552 | return DefaultOperands.find(R)->second; |
| 553 | } |
| 554 | |
| 555 | // Pattern Fragment information. |
| 556 | TreePattern *getPatternFragment(Record *R) const { |
| 557 | assert(PatternFragments.count(R) && "Invalid pattern fragment request!"); |
| 558 | return PatternFragments.find(R)->second; |
| 559 | } |
Benjamin Kramer | 5b9e7ef | 2009-08-23 10:39:21 +0000 | [diff] [blame] | 560 | typedef std::map<Record*, TreePattern*, RecordPtrCmp>::const_iterator |
| 561 | pf_iterator; |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 562 | pf_iterator pf_begin() const { return PatternFragments.begin(); } |
| 563 | pf_iterator pf_end() const { return PatternFragments.end(); } |
| 564 | |
| 565 | // Patterns to match information. |
Chris Lattner | 60d8139 | 2008-01-05 22:30:17 +0000 | [diff] [blame] | 566 | typedef std::vector<PatternToMatch>::const_iterator ptm_iterator; |
| 567 | ptm_iterator ptm_begin() const { return PatternsToMatch.begin(); } |
| 568 | ptm_iterator ptm_end() const { return PatternsToMatch.end(); } |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 569 | |
| 570 | |
| 571 | |
| 572 | const DAGInstruction &getInstruction(Record *R) const { |
| 573 | assert(Instructions.count(R) && "Unknown instruction!"); |
| 574 | return Instructions.find(R)->second; |
| 575 | } |
| 576 | |
| 577 | Record *get_intrinsic_void_sdnode() const { |
| 578 | return intrinsic_void_sdnode; |
| 579 | } |
| 580 | Record *get_intrinsic_w_chain_sdnode() const { |
| 581 | return intrinsic_w_chain_sdnode; |
| 582 | } |
| 583 | Record *get_intrinsic_wo_chain_sdnode() const { |
| 584 | return intrinsic_wo_chain_sdnode; |
| 585 | } |
| 586 | |
| 587 | private: |
| 588 | void ParseNodeInfo(); |
Chris Lattner | 443e3f9 | 2008-01-05 22:54:53 +0000 | [diff] [blame] | 589 | void ParseNodeTransforms(); |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 590 | void ParseComplexPatterns(); |
Chris Lattner | dc32f98 | 2008-01-05 22:43:57 +0000 | [diff] [blame] | 591 | void ParsePatternFragments(); |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 592 | void ParseDefaultOperands(); |
| 593 | void ParseInstructions(); |
| 594 | void ParsePatterns(); |
Dan Gohman | ee4fa19 | 2008-04-03 00:02:49 +0000 | [diff] [blame] | 595 | void InferInstructionFlags(); |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 596 | void GenerateVariants(); |
| 597 | |
| 598 | void FindPatternInputsAndOutputs(TreePattern *I, TreePatternNode *Pat, |
| 599 | std::map<std::string, |
| 600 | TreePatternNode*> &InstInputs, |
| 601 | std::map<std::string, |
| 602 | TreePatternNode*> &InstResults, |
| 603 | std::vector<Record*> &InstImpInputs, |
| 604 | std::vector<Record*> &InstImpResults); |
| 605 | }; |
| 606 | } // end namespace llvm |
| 607 | |
| 608 | #endif |