blob: 88d55b6ab4d740564e9aea44a2815e9bf1e5f934 [file] [log] [blame]
Chris Lattnerda272d12010-02-15 08:04:42 +00001//===- 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 Lattner785d16f2010-02-17 02:16:19 +000013#include "llvm/ADT/SmallVector.h"
Chris Lattnerda272d12010-02-15 08:04:42 +000014#include "llvm/ADT/StringMap.h"
15using namespace llvm;
16
17namespace {
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 Lattner785d16f2010-02-17 02:16:19 +000033 /// 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 Lattner8ef9c792010-02-18 02:49:24 +000038 MatcherNode *Matcher;
Chris Lattner785d16f2010-02-17 02:16:19 +000039
40 /// CurPredicate - As we emit matcher nodes, this points to the latest check
Chris Lattnerbd8227f2010-02-18 02:53:41 +000041 /// which should have future checks stuck into its Next position.
Chris Lattner8ef9c792010-02-18 02:49:24 +000042 MatcherNode *CurPredicate;
Chris Lattnerda272d12010-02-15 08:04:42 +000043 public:
44 MatcherGen(const PatternToMatch &pattern, const CodeGenDAGPatterns &cgp);
45
46 ~MatcherGen() {
47 delete PatWithNoTypes;
48 }
49
50 void EmitMatcherCode();
Chris Lattnerb49985a2010-02-18 06:47:49 +000051 void EmitResultCode();
Chris Lattnerda272d12010-02-15 08:04:42 +000052
Chris Lattner8ef9c792010-02-18 02:49:24 +000053 MatcherNode *GetMatcher() const { return Matcher; }
54 MatcherNode *GetCurPredicate() const { return CurPredicate; }
Chris Lattnerda272d12010-02-15 08:04:42 +000055 private:
Chris Lattner8ef9c792010-02-18 02:49:24 +000056 void AddMatcherNode(MatcherNode *NewNode);
Chris Lattnerda272d12010-02-15 08:04:42 +000057 void InferPossibleTypes();
Chris Lattnerb49985a2010-02-18 06:47:49 +000058
59 // Matcher Generation.
Chris Lattnerda272d12010-02-15 08:04:42 +000060 void EmitMatchCode(const TreePatternNode *N, TreePatternNode *NodeNoTypes);
61 void EmitLeafMatchCode(const TreePatternNode *N);
62 void EmitOperatorMatchCode(const TreePatternNode *N,
63 TreePatternNode *NodeNoTypes);
Chris Lattnerb49985a2010-02-18 06:47:49 +000064
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 Lattnerda272d12010-02-15 08:04:42 +000072 };
73
74} // end anon namespace.
75
76MatcherGen::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.
103void 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 Lattner8ef9c792010-02-18 02:49:24 +0000121void MatcherGen::AddMatcherNode(MatcherNode *NewNode) {
Chris Lattnerda272d12010-02-15 08:04:42 +0000122 if (CurPredicate != 0)
Chris Lattnerbd8227f2010-02-18 02:53:41 +0000123 CurPredicate->setNext(NewNode);
Chris Lattnerda272d12010-02-15 08:04:42 +0000124 else
125 Matcher = NewNode;
126 CurPredicate = NewNode;
127}
128
129
Chris Lattnerb49985a2010-02-18 06:47:49 +0000130//===----------------------------------------------------------------------===//
131// Pattern Match Generation
132//===----------------------------------------------------------------------===//
Chris Lattnerda272d12010-02-15 08:04:42 +0000133
134/// EmitLeafMatchCode - Generate matching code for leaf nodes.
135void 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 Lattnerc2676b22010-02-17 00:11:30 +0000163 // 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 Lattner53a2f602010-02-16 23:16:25 +0000166 errs() << "We expect complex pattern uses to have names: " << *N << "\n";
167 exit(1);
168 }
169
Chris Lattnerda272d12010-02-15 08:04:42 +0000170 // Handle complex pattern.
171 const ComplexPattern &CP = CGP.getComplexPattern(LeafRec);
Chris Lattner8dc4f2b2010-02-17 06:08:25 +0000172 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 Lattner9a747f12010-02-17 06:23:39 +0000182
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 Lattner8dc4f2b2010-02-17 06:08:25 +0000192 }
193 return;
Chris Lattnerda272d12010-02-15 08:04:42 +0000194 }
195
196 errs() << "Unknown leaf kind: " << *N << "\n";
197 abort();
198}
199
200void 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 Lattner6bc1b512010-02-16 19:19:58 +0000240 if (N->NodeHasProperty(SDNPHasChain, CGP)) {
Chris Lattner2f7ecde2010-02-17 01:34:15 +0000241 // Record the input chain, which is always input #0 of the SDNode.
242 AddMatcherNode(new MoveChildMatcherNode(0));
Chris Lattner2f7ecde2010-02-17 01:34:15 +0000243 AddMatcherNode(new RecordMatcherNode("'" + N->getOperator()->getName() +
244 "' input chain"));
Chris Lattner785d16f2010-02-17 02:16:19 +0000245
246 // Remember all of the input chains our pattern will match.
247 InputChains.push_back(NextRecordedOperandNo);
248 ++NextRecordedOperandNo;
Chris Lattner2f7ecde2010-02-17 01:34:15 +0000249 AddMatcherNode(new MoveParentMatcherNode());
Chris Lattner785d16f2010-02-17 02:16:19 +0000250
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 Lattner9a747f12010-02-17 06:23:39 +0000256 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 Lattner785d16f2010-02-17 02:16:19 +0000263
Chris Lattner2f7ecde2010-02-17 01:34:15 +0000264 // Don't look at the input chain when matching the tree pattern to the
265 // SDNode.
Chris Lattnerda272d12010-02-15 08:04:42 +0000266 OpNo = 1;
267
Chris Lattner6bc1b512010-02-16 19:19:58 +0000268 // 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 Lattnere39650a2010-02-16 06:10:58 +0000291 // 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 Lattner21390d72010-02-16 19:15:55 +0000312 AddMatcherNode(new CheckFoldableChainNodeMatcherNode());
Chris Lattnere39650a2010-02-16 06:10:58 +0000313 }
Chris Lattnerda272d12010-02-15 08:04:42 +0000314 }
315
Chris Lattnerda272d12010-02-15 08:04:42 +0000316 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 Lattnerc642b842010-02-17 01:27:29 +0000319 AddMatcherNode(new MoveChildMatcherNode(OpNo));
Chris Lattnerda272d12010-02-15 08:04:42 +0000320 EmitMatchCode(N->getChild(i), NodeNoTypes->getChild(i));
321 AddMatcherNode(new MoveParentMatcherNode());
322 }
323}
324
325
326void 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 Lattnerda272d12010-02-15 08:04:42 +0000337 // 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 Lattnere609a512010-02-17 00:31:50 +0000342 VarMapEntry = NextRecordedOperandNo+1;
343
344 unsigned NumRecorded;
Chris Lattner53a2f602010-02-16 23:16:25 +0000345
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 Lattnere609a512010-02-17 00:31:50 +0000351 NumRecorded = AM->getNumOperands()-1;
352
353 if (AM->hasProperty(SDNPHasChain))
354 NumRecorded += 2; // Input and output chains.
Chris Lattner53a2f602010-02-16 23:16:25 +0000355 } else {
356 // If it is a normal named node, we must emit a 'Record' opcode.
Chris Lattnerc642b842010-02-17 01:27:29 +0000357 AddMatcherNode(new RecordMatcherNode("$" + N->getName()));
Chris Lattnere609a512010-02-17 00:31:50 +0000358 NumRecorded = 1;
Chris Lattner53a2f602010-02-16 23:16:25 +0000359 }
Chris Lattnere609a512010-02-17 00:31:50 +0000360 NextRecordedOperandNo += NumRecorded;
Chris Lattner53a2f602010-02-16 23:16:25 +0000361
Chris Lattnerda272d12010-02-15 08:04:42 +0000362 } 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
382void 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 Lattnerb49985a2010-02-18 06:47:49 +0000394//===----------------------------------------------------------------------===//
395// Node Result Generation
396//===----------------------------------------------------------------------===//
397
398void MatcherGen::EmitResultLeafAsOperand(const TreePatternNode *N,
399 SmallVectorImpl<unsigned> &ResultOps) {
Chris Lattner845c0422010-02-18 22:03:03 +0000400 assert(N->isLeaf() && "Must be a leaf");
401
402 if (IntInit *II = dynamic_cast<IntInit*>(N->getLeafValue())) {
403 AddMatcherNode(new EmitIntegerMatcherNode(II->getValue(),N->getTypeNum(0)));
404 //ResultOps.push_back(TmpNode(TmpNo++));
405 return;
406 }
407
408 // If this is an explicit register reference, handle it.
409 if (DefInit *DI = dynamic_cast<DefInit*>(N->getLeafValue())) {
410 if (DI->getDef()->isSubClassOf("Register")) {
411 AddMatcherNode(new EmitRegisterMatcherNode(DI->getDef(),
412 N->getTypeNum(0)));
413 //ResultOps.push_back(TmpNode(TmpNo++));
414 return;
415 }
416
417 if (DI->getDef()->getName() == "zero_reg") {
418 AddMatcherNode(new EmitRegisterMatcherNode(0, N->getTypeNum(0)));
419 //ResultOps.push_back(TmpNode(TmpNo++));
420 return;
421 }
422
423#if 0
424 if (DI->getDef()->isSubClassOf("RegisterClass")) {
425 // Handle a reference to a register class. This is used
426 // in COPY_TO_SUBREG instructions.
427 // FIXME: Implement.
428 }
429#endif
430 }
431
Chris Lattnerb49985a2010-02-18 06:47:49 +0000432 errs() << "unhandled leaf node: \n";
433 N->dump();
434}
435
436void MatcherGen::EmitResultInstructionAsOperand(const TreePatternNode *N,
437 SmallVectorImpl<unsigned> &ResultOps) {
438 Record *Op = N->getOperator();
439 const CodeGenTarget &CGT = CGP.getTargetInfo();
440 CodeGenInstruction &II = CGT.getInstruction(Op->getName());
441 const DAGInstruction &Inst = CGP.getInstruction(Op);
442
443 // FIXME: Handle (set x, (foo))
444
445 if (II.isVariadic) // FIXME: Handle variadic instructions.
446 return AddMatcherNode(new EmitNodeMatcherNode(Pattern));
447
448 // FIXME: Handle OptInFlag, HasInFlag, HasOutFlag
449 // FIXME: Handle Chains.
450 unsigned NumResults = Inst.getNumResults();
451
452
453 // Loop over all of the operands of the instruction pattern, emitting code
454 // to fill them all in. The node 'N' usually has number children equal to
455 // the number of input operands of the instruction. However, in cases
456 // where there are predicate operands for an instruction, we need to fill
457 // in the 'execute always' values. Match up the node operands to the
458 // instruction operands to do this.
459 SmallVector<unsigned, 8> Ops;
460 for (unsigned ChildNo = 0, InstOpNo = NumResults, e = II.OperandList.size();
461 InstOpNo != e; ++InstOpNo) {
462
463 // Determine what to emit for this operand.
464 Record *OperandNode = II.OperandList[InstOpNo].Rec;
465 if ((OperandNode->isSubClassOf("PredicateOperand") ||
466 OperandNode->isSubClassOf("OptionalDefOperand")) &&
467 !CGP.getDefaultOperand(OperandNode).DefaultOps.empty()) {
468 // This is a predicate or optional def operand; emit the
469 // 'default ops' operands.
470 const DAGDefaultOperand &DefaultOp =
471 CGP.getDefaultOperand(II.OperandList[InstOpNo].Rec);
472 for (unsigned i = 0, e = DefaultOp.DefaultOps.size(); i != e; ++i)
473 EmitResultOperand(DefaultOp.DefaultOps[i], Ops);
474 continue;
475 }
476
477 // Otherwise this is a normal operand or a predicate operand without
478 // 'execute always'; emit it.
479 EmitResultOperand(N->getChild(ChildNo), Ops);
480 ++ChildNo;
481 }
482
483 // FIXME: Chain.
484 // FIXME: Flag
485
486
487
488 return;
489}
490
491void MatcherGen::EmitResultOperand(const TreePatternNode *N,
492 SmallVectorImpl<unsigned> &ResultOps) {
493 // This is something selected from the pattern we matched.
494 if (!N->getName().empty()) {
495 //errs() << "unhandled named node: \n";
496 //N->dump();
497 return;
498 }
499
500 if (N->isLeaf())
501 return EmitResultLeafAsOperand(N, ResultOps);
502
503 Record *OpRec = N->getOperator();
504 if (OpRec->isSubClassOf("Instruction"))
505 return EmitResultInstructionAsOperand(N, ResultOps);
506 if (OpRec->isSubClassOf("SDNodeXForm"))
507 // FIXME: implement.
508 return;
509 errs() << "Unknown result node to emit code for: " << *N << '\n';
510 throw std::string("Unknown node in result pattern!");
511}
512
513void MatcherGen::EmitResultCode() {
514 // FIXME: Handle Ops.
515 // FIXME: Ops should be vector of "ResultValue> which is either an index into
516 // the results vector is is a temp result.
517 SmallVector<unsigned, 8> Ops;
518 EmitResultOperand(Pattern.getDstPattern(), Ops);
519 //AddMatcherNode(new EmitNodeMatcherNode(Pattern));
520}
521
522
Chris Lattnerda272d12010-02-15 08:04:42 +0000523MatcherNode *llvm::ConvertPatternToMatcher(const PatternToMatch &Pattern,
524 const CodeGenDAGPatterns &CGP) {
525 MatcherGen Gen(Pattern, CGP);
526
527 // Generate the code for the matcher.
528 Gen.EmitMatcherCode();
529
530 // If the match succeeds, then we generate Pattern.
Chris Lattnerb49985a2010-02-18 06:47:49 +0000531 Gen.EmitResultCode();
Chris Lattnerda272d12010-02-15 08:04:42 +0000532
533 // Unconditional match.
Chris Lattnerb49985a2010-02-18 06:47:49 +0000534 return Gen.GetMatcher();
Chris Lattnerda272d12010-02-15 08:04:42 +0000535}
536
537
538