Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 1 | //===- DAGISelMatcherGen.cpp - Matcher generator --------------------------===// |
| 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 | |
| 10 | #include "DAGISelMatcher.h" |
| 11 | #include "CodeGenDAGPatterns.h" |
| 12 | #include "Record.h" |
Chris Lattner | 785d16f | 2010-02-17 02:16:19 +0000 | [diff] [blame] | 13 | #include "llvm/ADT/SmallVector.h" |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/StringMap.h" |
| 15 | using namespace llvm; |
| 16 | |
| 17 | namespace { |
| 18 | class MatcherGen { |
| 19 | const PatternToMatch &Pattern; |
| 20 | const CodeGenDAGPatterns &CGP; |
| 21 | |
| 22 | /// PatWithNoTypes - This is a clone of Pattern.getSrcPattern() that starts |
| 23 | /// out with all of the types removed. This allows us to insert type checks |
| 24 | /// as we scan the tree. |
| 25 | TreePatternNode *PatWithNoTypes; |
| 26 | |
| 27 | /// VariableMap - A map from variable names ('$dst') to the recorded operand |
| 28 | /// number that they were captured as. These are biased by 1 to make |
| 29 | /// insertion easier. |
| 30 | StringMap<unsigned> VariableMap; |
| 31 | unsigned NextRecordedOperandNo; |
| 32 | |
Chris Lattner | 785d16f | 2010-02-17 02:16:19 +0000 | [diff] [blame] | 33 | /// InputChains - This maintains the position in the recorded nodes array of |
| 34 | /// all of the recorded input chains. |
| 35 | SmallVector<unsigned, 2> InputChains; |
| 36 | |
| 37 | /// Matcher - This is the top level of the generated matcher, the result. |
Chris Lattner | 8ef9c79 | 2010-02-18 02:49:24 +0000 | [diff] [blame] | 38 | MatcherNode *Matcher; |
Chris Lattner | 785d16f | 2010-02-17 02:16:19 +0000 | [diff] [blame] | 39 | |
| 40 | /// CurPredicate - As we emit matcher nodes, this points to the latest check |
Chris Lattner | bd8227f | 2010-02-18 02:53:41 +0000 | [diff] [blame] | 41 | /// which should have future checks stuck into its Next position. |
Chris Lattner | 8ef9c79 | 2010-02-18 02:49:24 +0000 | [diff] [blame] | 42 | MatcherNode *CurPredicate; |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 43 | public: |
| 44 | MatcherGen(const PatternToMatch &pattern, const CodeGenDAGPatterns &cgp); |
| 45 | |
| 46 | ~MatcherGen() { |
| 47 | delete PatWithNoTypes; |
| 48 | } |
| 49 | |
| 50 | void EmitMatcherCode(); |
Chris Lattner | b49985a | 2010-02-18 06:47:49 +0000 | [diff] [blame^] | 51 | void EmitResultCode(); |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 52 | |
Chris Lattner | 8ef9c79 | 2010-02-18 02:49:24 +0000 | [diff] [blame] | 53 | MatcherNode *GetMatcher() const { return Matcher; } |
| 54 | MatcherNode *GetCurPredicate() const { return CurPredicate; } |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 55 | private: |
Chris Lattner | 8ef9c79 | 2010-02-18 02:49:24 +0000 | [diff] [blame] | 56 | void AddMatcherNode(MatcherNode *NewNode); |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 57 | void InferPossibleTypes(); |
Chris Lattner | b49985a | 2010-02-18 06:47:49 +0000 | [diff] [blame^] | 58 | |
| 59 | // Matcher Generation. |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 60 | void EmitMatchCode(const TreePatternNode *N, TreePatternNode *NodeNoTypes); |
| 61 | void EmitLeafMatchCode(const TreePatternNode *N); |
| 62 | void EmitOperatorMatchCode(const TreePatternNode *N, |
| 63 | TreePatternNode *NodeNoTypes); |
Chris Lattner | b49985a | 2010-02-18 06:47:49 +0000 | [diff] [blame^] | 64 | |
| 65 | // Result Code Generation. |
| 66 | void EmitResultOperand(const TreePatternNode *N, |
| 67 | SmallVectorImpl<unsigned> &ResultOps); |
| 68 | void EmitResultLeafAsOperand(const TreePatternNode *N, |
| 69 | SmallVectorImpl<unsigned> &ResultOps); |
| 70 | void EmitResultInstructionAsOperand(const TreePatternNode *N, |
| 71 | SmallVectorImpl<unsigned> &ResultOps); |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 72 | }; |
| 73 | |
| 74 | } // end anon namespace. |
| 75 | |
| 76 | MatcherGen::MatcherGen(const PatternToMatch &pattern, |
| 77 | const CodeGenDAGPatterns &cgp) |
| 78 | : Pattern(pattern), CGP(cgp), NextRecordedOperandNo(0), |
| 79 | Matcher(0), CurPredicate(0) { |
| 80 | // We need to produce the matcher tree for the patterns source pattern. To do |
| 81 | // this we need to match the structure as well as the types. To do the type |
| 82 | // matching, we want to figure out the fewest number of type checks we need to |
| 83 | // emit. For example, if there is only one integer type supported by a |
| 84 | // target, there should be no type comparisons at all for integer patterns! |
| 85 | // |
| 86 | // To figure out the fewest number of type checks needed, clone the pattern, |
| 87 | // remove the types, then perform type inference on the pattern as a whole. |
| 88 | // If there are unresolved types, emit an explicit check for those types, |
| 89 | // apply the type to the tree, then rerun type inference. Iterate until all |
| 90 | // types are resolved. |
| 91 | // |
| 92 | PatWithNoTypes = Pattern.getSrcPattern()->clone(); |
| 93 | PatWithNoTypes->RemoveAllTypes(); |
| 94 | |
| 95 | // If there are types that are manifestly known, infer them. |
| 96 | InferPossibleTypes(); |
| 97 | } |
| 98 | |
| 99 | /// InferPossibleTypes - As we emit the pattern, we end up generating type |
| 100 | /// checks and applying them to the 'PatWithNoTypes' tree. As we do this, we |
| 101 | /// want to propagate implied types as far throughout the tree as possible so |
| 102 | /// that we avoid doing redundant type checks. This does the type propagation. |
| 103 | void MatcherGen::InferPossibleTypes() { |
| 104 | // TP - Get *SOME* tree pattern, we don't care which. It is only used for |
| 105 | // diagnostics, which we know are impossible at this point. |
| 106 | TreePattern &TP = *CGP.pf_begin()->second; |
| 107 | |
| 108 | try { |
| 109 | bool MadeChange = true; |
| 110 | while (MadeChange) |
| 111 | MadeChange = PatWithNoTypes->ApplyTypeConstraints(TP, |
| 112 | true/*Ignore reg constraints*/); |
| 113 | } catch (...) { |
| 114 | errs() << "Type constraint application shouldn't fail!"; |
| 115 | abort(); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | |
| 120 | /// AddMatcherNode - Add a matcher node to the current graph we're building. |
Chris Lattner | 8ef9c79 | 2010-02-18 02:49:24 +0000 | [diff] [blame] | 121 | void MatcherGen::AddMatcherNode(MatcherNode *NewNode) { |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 122 | if (CurPredicate != 0) |
Chris Lattner | bd8227f | 2010-02-18 02:53:41 +0000 | [diff] [blame] | 123 | CurPredicate->setNext(NewNode); |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 124 | else |
| 125 | Matcher = NewNode; |
| 126 | CurPredicate = NewNode; |
| 127 | } |
| 128 | |
| 129 | |
Chris Lattner | b49985a | 2010-02-18 06:47:49 +0000 | [diff] [blame^] | 130 | //===----------------------------------------------------------------------===// |
| 131 | // Pattern Match Generation |
| 132 | //===----------------------------------------------------------------------===// |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 133 | |
| 134 | /// EmitLeafMatchCode - Generate matching code for leaf nodes. |
| 135 | void MatcherGen::EmitLeafMatchCode(const TreePatternNode *N) { |
| 136 | assert(N->isLeaf() && "Not a leaf?"); |
| 137 | // Direct match against an integer constant. |
| 138 | if (IntInit *II = dynamic_cast<IntInit*>(N->getLeafValue())) |
| 139 | return AddMatcherNode(new CheckIntegerMatcherNode(II->getValue())); |
| 140 | |
| 141 | DefInit *DI = dynamic_cast<DefInit*>(N->getLeafValue()); |
| 142 | if (DI == 0) { |
| 143 | errs() << "Unknown leaf kind: " << *DI << "\n"; |
| 144 | abort(); |
| 145 | } |
| 146 | |
| 147 | Record *LeafRec = DI->getDef(); |
| 148 | if (// Handle register references. Nothing to do here, they always match. |
| 149 | LeafRec->isSubClassOf("RegisterClass") || |
| 150 | LeafRec->isSubClassOf("PointerLikeRegClass") || |
| 151 | LeafRec->isSubClassOf("Register") || |
| 152 | // Place holder for SRCVALUE nodes. Nothing to do here. |
| 153 | LeafRec->getName() == "srcvalue") |
| 154 | return; |
| 155 | |
| 156 | if (LeafRec->isSubClassOf("ValueType")) |
| 157 | return AddMatcherNode(new CheckValueTypeMatcherNode(LeafRec->getName())); |
| 158 | |
| 159 | if (LeafRec->isSubClassOf("CondCode")) |
| 160 | return AddMatcherNode(new CheckCondCodeMatcherNode(LeafRec->getName())); |
| 161 | |
| 162 | if (LeafRec->isSubClassOf("ComplexPattern")) { |
Chris Lattner | c2676b2 | 2010-02-17 00:11:30 +0000 | [diff] [blame] | 163 | // We can't model ComplexPattern uses that don't have their name taken yet. |
| 164 | // The OPC_CheckComplexPattern operation implicitly records the results. |
| 165 | if (N->getName().empty()) { |
Chris Lattner | 53a2f60 | 2010-02-16 23:16:25 +0000 | [diff] [blame] | 166 | errs() << "We expect complex pattern uses to have names: " << *N << "\n"; |
| 167 | exit(1); |
| 168 | } |
| 169 | |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 170 | // Handle complex pattern. |
| 171 | const ComplexPattern &CP = CGP.getComplexPattern(LeafRec); |
Chris Lattner | 8dc4f2b | 2010-02-17 06:08:25 +0000 | [diff] [blame] | 172 | AddMatcherNode(new CheckComplexPatMatcherNode(CP)); |
| 173 | |
| 174 | // If the complex pattern has a chain, then we need to keep track of the |
| 175 | // fact that we just recorded a chain input. The chain input will be |
| 176 | // matched as the last operand of the predicate if it was successful. |
| 177 | if (CP.hasProperty(SDNPHasChain)) { |
| 178 | // It is the last operand recorded. |
| 179 | assert(NextRecordedOperandNo > 1 && |
| 180 | "Should have recorded input/result chains at least!"); |
| 181 | InputChains.push_back(NextRecordedOperandNo-1); |
Chris Lattner | 9a747f1 | 2010-02-17 06:23:39 +0000 | [diff] [blame] | 182 | |
| 183 | // IF we need to check chains, do so, see comment for |
| 184 | // "NodeHasProperty(SDNPHasChain" below. |
| 185 | if (InputChains.size() > 1) { |
| 186 | // FIXME: This is broken, we should eliminate this nonsense completely, |
| 187 | // but we want to produce the same selections that the old matcher does |
| 188 | // for now. |
| 189 | unsigned PrevOp = InputChains[InputChains.size()-2]; |
| 190 | AddMatcherNode(new CheckChainCompatibleMatcherNode(PrevOp)); |
| 191 | } |
Chris Lattner | 8dc4f2b | 2010-02-17 06:08:25 +0000 | [diff] [blame] | 192 | } |
| 193 | return; |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | errs() << "Unknown leaf kind: " << *N << "\n"; |
| 197 | abort(); |
| 198 | } |
| 199 | |
| 200 | void MatcherGen::EmitOperatorMatchCode(const TreePatternNode *N, |
| 201 | TreePatternNode *NodeNoTypes) { |
| 202 | assert(!N->isLeaf() && "Not an operator?"); |
| 203 | const SDNodeInfo &CInfo = CGP.getSDNodeInfo(N->getOperator()); |
| 204 | |
| 205 | // If this is an 'and R, 1234' where the operation is AND/OR and the RHS is |
| 206 | // a constant without a predicate fn that has more that one bit set, handle |
| 207 | // this as a special case. This is usually for targets that have special |
| 208 | // handling of certain large constants (e.g. alpha with it's 8/16/32-bit |
| 209 | // handling stuff). Using these instructions is often far more efficient |
| 210 | // than materializing the constant. Unfortunately, both the instcombiner |
| 211 | // and the dag combiner can often infer that bits are dead, and thus drop |
| 212 | // them from the mask in the dag. For example, it might turn 'AND X, 255' |
| 213 | // into 'AND X, 254' if it knows the low bit is set. Emit code that checks |
| 214 | // to handle this. |
| 215 | if ((N->getOperator()->getName() == "and" || |
| 216 | N->getOperator()->getName() == "or") && |
| 217 | N->getChild(1)->isLeaf() && N->getChild(1)->getPredicateFns().empty()) { |
| 218 | if (IntInit *II = dynamic_cast<IntInit*>(N->getChild(1)->getLeafValue())) { |
| 219 | if (!isPowerOf2_32(II->getValue())) { // Don't bother with single bits. |
| 220 | if (N->getOperator()->getName() == "and") |
| 221 | AddMatcherNode(new CheckAndImmMatcherNode(II->getValue())); |
| 222 | else |
| 223 | AddMatcherNode(new CheckOrImmMatcherNode(II->getValue())); |
| 224 | |
| 225 | // Match the LHS of the AND as appropriate. |
| 226 | AddMatcherNode(new MoveChildMatcherNode(0)); |
| 227 | EmitMatchCode(N->getChild(0), NodeNoTypes->getChild(0)); |
| 228 | AddMatcherNode(new MoveParentMatcherNode()); |
| 229 | return; |
| 230 | } |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | // Check that the current opcode lines up. |
| 235 | AddMatcherNode(new CheckOpcodeMatcherNode(CInfo.getEnumName())); |
| 236 | |
| 237 | // If this node has a chain, then the chain is operand #0 is the SDNode, and |
| 238 | // the child numbers of the node are all offset by one. |
| 239 | unsigned OpNo = 0; |
Chris Lattner | 6bc1b51 | 2010-02-16 19:19:58 +0000 | [diff] [blame] | 240 | if (N->NodeHasProperty(SDNPHasChain, CGP)) { |
Chris Lattner | 2f7ecde | 2010-02-17 01:34:15 +0000 | [diff] [blame] | 241 | // Record the input chain, which is always input #0 of the SDNode. |
| 242 | AddMatcherNode(new MoveChildMatcherNode(0)); |
Chris Lattner | 2f7ecde | 2010-02-17 01:34:15 +0000 | [diff] [blame] | 243 | AddMatcherNode(new RecordMatcherNode("'" + N->getOperator()->getName() + |
| 244 | "' input chain")); |
Chris Lattner | 785d16f | 2010-02-17 02:16:19 +0000 | [diff] [blame] | 245 | |
| 246 | // Remember all of the input chains our pattern will match. |
| 247 | InputChains.push_back(NextRecordedOperandNo); |
| 248 | ++NextRecordedOperandNo; |
Chris Lattner | 2f7ecde | 2010-02-17 01:34:15 +0000 | [diff] [blame] | 249 | AddMatcherNode(new MoveParentMatcherNode()); |
Chris Lattner | 785d16f | 2010-02-17 02:16:19 +0000 | [diff] [blame] | 250 | |
| 251 | // If this is the second (e.g. indbr(load) or store(add(load))) or third |
| 252 | // input chain (e.g. (store (add (load, load))) from msp430) we need to make |
| 253 | // sure that folding the chain won't induce cycles in the DAG. This could |
| 254 | // happen if there were an intermediate node between the indbr and load, for |
| 255 | // example. |
Chris Lattner | 9a747f1 | 2010-02-17 06:23:39 +0000 | [diff] [blame] | 256 | if (InputChains.size() > 1) { |
| 257 | // FIXME: This is broken, we should eliminate this nonsense completely, |
| 258 | // but we want to produce the same selections that the old matcher does |
| 259 | // for now. |
| 260 | unsigned PrevOp = InputChains[InputChains.size()-2]; |
| 261 | AddMatcherNode(new CheckChainCompatibleMatcherNode(PrevOp)); |
| 262 | } |
Chris Lattner | 785d16f | 2010-02-17 02:16:19 +0000 | [diff] [blame] | 263 | |
Chris Lattner | 2f7ecde | 2010-02-17 01:34:15 +0000 | [diff] [blame] | 264 | // Don't look at the input chain when matching the tree pattern to the |
| 265 | // SDNode. |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 266 | OpNo = 1; |
| 267 | |
Chris Lattner | 6bc1b51 | 2010-02-16 19:19:58 +0000 | [diff] [blame] | 268 | // If this node is not the root and the subtree underneath it produces a |
| 269 | // chain, then the result of matching the node is also produce a chain. |
| 270 | // Beyond that, this means that we're also folding (at least) the root node |
| 271 | // into the node that produce the chain (for example, matching |
| 272 | // "(add reg, (load ptr))" as a add_with_memory on X86). This is |
| 273 | // problematic, if the 'reg' node also uses the load (say, its chain). |
| 274 | // Graphically: |
| 275 | // |
| 276 | // [LD] |
| 277 | // ^ ^ |
| 278 | // | \ DAG's like cheese. |
| 279 | // / | |
| 280 | // / [YY] |
| 281 | // | ^ |
| 282 | // [XX]--/ |
| 283 | // |
| 284 | // It would be invalid to fold XX and LD. In this case, folding the two |
| 285 | // nodes together would induce a cycle in the DAG, making it a 'cyclic DAG' |
| 286 | // To prevent this, we emit a dynamic check for legality before allowing |
| 287 | // this to be folded. |
| 288 | // |
| 289 | const TreePatternNode *Root = Pattern.getSrcPattern(); |
| 290 | if (N != Root) { // Not the root of the pattern. |
Chris Lattner | e39650a | 2010-02-16 06:10:58 +0000 | [diff] [blame] | 291 | // If there is a node between the root and this node, then we definitely |
| 292 | // need to emit the check. |
| 293 | bool NeedCheck = !Root->hasChild(N); |
| 294 | |
| 295 | // If it *is* an immediate child of the root, we can still need a check if |
| 296 | // the root SDNode has multiple inputs. For us, this means that it is an |
| 297 | // intrinsic, has multiple operands, or has other inputs like chain or |
| 298 | // flag). |
| 299 | if (!NeedCheck) { |
| 300 | const SDNodeInfo &PInfo = CGP.getSDNodeInfo(Root->getOperator()); |
| 301 | NeedCheck = |
| 302 | Root->getOperator() == CGP.get_intrinsic_void_sdnode() || |
| 303 | Root->getOperator() == CGP.get_intrinsic_w_chain_sdnode() || |
| 304 | Root->getOperator() == CGP.get_intrinsic_wo_chain_sdnode() || |
| 305 | PInfo.getNumOperands() > 1 || |
| 306 | PInfo.hasProperty(SDNPHasChain) || |
| 307 | PInfo.hasProperty(SDNPInFlag) || |
| 308 | PInfo.hasProperty(SDNPOptInFlag); |
| 309 | } |
| 310 | |
| 311 | if (NeedCheck) |
Chris Lattner | 21390d7 | 2010-02-16 19:15:55 +0000 | [diff] [blame] | 312 | AddMatcherNode(new CheckFoldableChainNodeMatcherNode()); |
Chris Lattner | e39650a | 2010-02-16 06:10:58 +0000 | [diff] [blame] | 313 | } |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 314 | } |
| 315 | |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 316 | for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i, ++OpNo) { |
| 317 | // Get the code suitable for matching this child. Move to the child, check |
| 318 | // it then move back to the parent. |
Chris Lattner | c642b84 | 2010-02-17 01:27:29 +0000 | [diff] [blame] | 319 | AddMatcherNode(new MoveChildMatcherNode(OpNo)); |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 320 | EmitMatchCode(N->getChild(i), NodeNoTypes->getChild(i)); |
| 321 | AddMatcherNode(new MoveParentMatcherNode()); |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | |
| 326 | void MatcherGen::EmitMatchCode(const TreePatternNode *N, |
| 327 | TreePatternNode *NodeNoTypes) { |
| 328 | // If N and NodeNoTypes don't agree on a type, then this is a case where we |
| 329 | // need to do a type check. Emit the check, apply the tyep to NodeNoTypes and |
| 330 | // reinfer any correlated types. |
| 331 | if (NodeNoTypes->getExtTypes() != N->getExtTypes()) { |
| 332 | AddMatcherNode(new CheckTypeMatcherNode(N->getTypeNum(0))); |
| 333 | NodeNoTypes->setTypes(N->getExtTypes()); |
| 334 | InferPossibleTypes(); |
| 335 | } |
| 336 | |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 337 | // If this node has a name associated with it, capture it in VariableMap. If |
| 338 | // we already saw this in the pattern, emit code to verify dagness. |
| 339 | if (!N->getName().empty()) { |
| 340 | unsigned &VarMapEntry = VariableMap[N->getName()]; |
| 341 | if (VarMapEntry == 0) { |
Chris Lattner | e609a51 | 2010-02-17 00:31:50 +0000 | [diff] [blame] | 342 | VarMapEntry = NextRecordedOperandNo+1; |
| 343 | |
| 344 | unsigned NumRecorded; |
Chris Lattner | 53a2f60 | 2010-02-16 23:16:25 +0000 | [diff] [blame] | 345 | |
| 346 | // If this is a complex pattern, the match operation for it will |
| 347 | // implicitly record all of the outputs of it (which may be more than |
| 348 | // one). |
| 349 | if (const ComplexPattern *AM = N->getComplexPatternInfo(CGP)) { |
| 350 | // Record the right number of operands. |
Chris Lattner | e609a51 | 2010-02-17 00:31:50 +0000 | [diff] [blame] | 351 | NumRecorded = AM->getNumOperands()-1; |
| 352 | |
| 353 | if (AM->hasProperty(SDNPHasChain)) |
| 354 | NumRecorded += 2; // Input and output chains. |
Chris Lattner | 53a2f60 | 2010-02-16 23:16:25 +0000 | [diff] [blame] | 355 | } else { |
| 356 | // If it is a normal named node, we must emit a 'Record' opcode. |
Chris Lattner | c642b84 | 2010-02-17 01:27:29 +0000 | [diff] [blame] | 357 | AddMatcherNode(new RecordMatcherNode("$" + N->getName())); |
Chris Lattner | e609a51 | 2010-02-17 00:31:50 +0000 | [diff] [blame] | 358 | NumRecorded = 1; |
Chris Lattner | 53a2f60 | 2010-02-16 23:16:25 +0000 | [diff] [blame] | 359 | } |
Chris Lattner | e609a51 | 2010-02-17 00:31:50 +0000 | [diff] [blame] | 360 | NextRecordedOperandNo += NumRecorded; |
Chris Lattner | 53a2f60 | 2010-02-16 23:16:25 +0000 | [diff] [blame] | 361 | |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 362 | } else { |
| 363 | // If we get here, this is a second reference to a specific name. Since |
| 364 | // we already have checked that the first reference is valid, we don't |
| 365 | // have to recursively match it, just check that it's the same as the |
| 366 | // previously named thing. |
| 367 | AddMatcherNode(new CheckSameMatcherNode(VarMapEntry-1)); |
| 368 | return; |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | // If there are node predicates for this node, generate their checks. |
| 373 | for (unsigned i = 0, e = N->getPredicateFns().size(); i != e; ++i) |
| 374 | AddMatcherNode(new CheckPredicateMatcherNode(N->getPredicateFns()[i])); |
| 375 | |
| 376 | if (N->isLeaf()) |
| 377 | EmitLeafMatchCode(N); |
| 378 | else |
| 379 | EmitOperatorMatchCode(N, NodeNoTypes); |
| 380 | } |
| 381 | |
| 382 | void MatcherGen::EmitMatcherCode() { |
| 383 | // If the pattern has a predicate on it (e.g. only enabled when a subtarget |
| 384 | // feature is around, do the check). |
| 385 | if (!Pattern.getPredicateCheck().empty()) |
| 386 | AddMatcherNode(new |
| 387 | CheckPatternPredicateMatcherNode(Pattern.getPredicateCheck())); |
| 388 | |
| 389 | // Emit the matcher for the pattern structure and types. |
| 390 | EmitMatchCode(Pattern.getSrcPattern(), PatWithNoTypes); |
| 391 | } |
| 392 | |
| 393 | |
Chris Lattner | b49985a | 2010-02-18 06:47:49 +0000 | [diff] [blame^] | 394 | //===----------------------------------------------------------------------===// |
| 395 | // Node Result Generation |
| 396 | //===----------------------------------------------------------------------===// |
| 397 | |
| 398 | void MatcherGen::EmitResultLeafAsOperand(const TreePatternNode *N, |
| 399 | SmallVectorImpl<unsigned> &ResultOps) { |
| 400 | errs() << "unhandled leaf node: \n"; |
| 401 | N->dump(); |
| 402 | } |
| 403 | |
| 404 | void MatcherGen::EmitResultInstructionAsOperand(const TreePatternNode *N, |
| 405 | SmallVectorImpl<unsigned> &ResultOps) { |
| 406 | Record *Op = N->getOperator(); |
| 407 | const CodeGenTarget &CGT = CGP.getTargetInfo(); |
| 408 | CodeGenInstruction &II = CGT.getInstruction(Op->getName()); |
| 409 | const DAGInstruction &Inst = CGP.getInstruction(Op); |
| 410 | |
| 411 | // FIXME: Handle (set x, (foo)) |
| 412 | |
| 413 | if (II.isVariadic) // FIXME: Handle variadic instructions. |
| 414 | return AddMatcherNode(new EmitNodeMatcherNode(Pattern)); |
| 415 | |
| 416 | // FIXME: Handle OptInFlag, HasInFlag, HasOutFlag |
| 417 | // FIXME: Handle Chains. |
| 418 | unsigned NumResults = Inst.getNumResults(); |
| 419 | |
| 420 | |
| 421 | // Loop over all of the operands of the instruction pattern, emitting code |
| 422 | // to fill them all in. The node 'N' usually has number children equal to |
| 423 | // the number of input operands of the instruction. However, in cases |
| 424 | // where there are predicate operands for an instruction, we need to fill |
| 425 | // in the 'execute always' values. Match up the node operands to the |
| 426 | // instruction operands to do this. |
| 427 | SmallVector<unsigned, 8> Ops; |
| 428 | for (unsigned ChildNo = 0, InstOpNo = NumResults, e = II.OperandList.size(); |
| 429 | InstOpNo != e; ++InstOpNo) { |
| 430 | |
| 431 | // Determine what to emit for this operand. |
| 432 | Record *OperandNode = II.OperandList[InstOpNo].Rec; |
| 433 | if ((OperandNode->isSubClassOf("PredicateOperand") || |
| 434 | OperandNode->isSubClassOf("OptionalDefOperand")) && |
| 435 | !CGP.getDefaultOperand(OperandNode).DefaultOps.empty()) { |
| 436 | // This is a predicate or optional def operand; emit the |
| 437 | // 'default ops' operands. |
| 438 | const DAGDefaultOperand &DefaultOp = |
| 439 | CGP.getDefaultOperand(II.OperandList[InstOpNo].Rec); |
| 440 | for (unsigned i = 0, e = DefaultOp.DefaultOps.size(); i != e; ++i) |
| 441 | EmitResultOperand(DefaultOp.DefaultOps[i], Ops); |
| 442 | continue; |
| 443 | } |
| 444 | |
| 445 | // Otherwise this is a normal operand or a predicate operand without |
| 446 | // 'execute always'; emit it. |
| 447 | EmitResultOperand(N->getChild(ChildNo), Ops); |
| 448 | ++ChildNo; |
| 449 | } |
| 450 | |
| 451 | // FIXME: Chain. |
| 452 | // FIXME: Flag |
| 453 | |
| 454 | |
| 455 | |
| 456 | return; |
| 457 | } |
| 458 | |
| 459 | void MatcherGen::EmitResultOperand(const TreePatternNode *N, |
| 460 | SmallVectorImpl<unsigned> &ResultOps) { |
| 461 | // This is something selected from the pattern we matched. |
| 462 | if (!N->getName().empty()) { |
| 463 | //errs() << "unhandled named node: \n"; |
| 464 | //N->dump(); |
| 465 | return; |
| 466 | } |
| 467 | |
| 468 | if (N->isLeaf()) |
| 469 | return EmitResultLeafAsOperand(N, ResultOps); |
| 470 | |
| 471 | Record *OpRec = N->getOperator(); |
| 472 | if (OpRec->isSubClassOf("Instruction")) |
| 473 | return EmitResultInstructionAsOperand(N, ResultOps); |
| 474 | if (OpRec->isSubClassOf("SDNodeXForm")) |
| 475 | // FIXME: implement. |
| 476 | return; |
| 477 | errs() << "Unknown result node to emit code for: " << *N << '\n'; |
| 478 | throw std::string("Unknown node in result pattern!"); |
| 479 | } |
| 480 | |
| 481 | void MatcherGen::EmitResultCode() { |
| 482 | // FIXME: Handle Ops. |
| 483 | // FIXME: Ops should be vector of "ResultValue> which is either an index into |
| 484 | // the results vector is is a temp result. |
| 485 | SmallVector<unsigned, 8> Ops; |
| 486 | EmitResultOperand(Pattern.getDstPattern(), Ops); |
| 487 | //AddMatcherNode(new EmitNodeMatcherNode(Pattern)); |
| 488 | } |
| 489 | |
| 490 | |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 491 | MatcherNode *llvm::ConvertPatternToMatcher(const PatternToMatch &Pattern, |
| 492 | const CodeGenDAGPatterns &CGP) { |
| 493 | MatcherGen Gen(Pattern, CGP); |
| 494 | |
| 495 | // Generate the code for the matcher. |
| 496 | Gen.EmitMatcherCode(); |
| 497 | |
| 498 | // If the match succeeds, then we generate Pattern. |
Chris Lattner | b49985a | 2010-02-18 06:47:49 +0000 | [diff] [blame^] | 499 | Gen.EmitResultCode(); |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 500 | |
| 501 | // Unconditional match. |
Chris Lattner | b49985a | 2010-02-18 06:47:49 +0000 | [diff] [blame^] | 502 | return Gen.GetMatcher(); |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | |
| 506 | |