blob: d87f73781619a4775e960207949099d67559ccac [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 {
Chris Lattnerc94fa4c2010-02-19 00:27:40 +000018 /// ResultVal - When generating new nodes for the result of a pattern match,
19 /// this value is used to represent an input to the node. Result values can
20 /// either be an input that is 'recorded' in the RecordedNodes array by the
21 /// matcher or it can be a temporary value created by the emitter for things
22 /// like constants.
23 class ResultVal {
Chris Lattner853b9192010-02-19 00:33:13 +000024 unsigned Number;
Chris Lattnerc94fa4c2010-02-19 00:27:40 +000025 public:
Chris Lattner853b9192010-02-19 00:33:13 +000026 static ResultVal get(unsigned N) {
Chris Lattnerc94fa4c2010-02-19 00:27:40 +000027 ResultVal R;
28 R.Number = N;
Chris Lattnerc94fa4c2010-02-19 00:27:40 +000029 return R;
30 }
31
Chris Lattner853b9192010-02-19 00:33:13 +000032 unsigned getNumber() const {
Chris Lattnerc94fa4c2010-02-19 00:27:40 +000033 return Number;
34 }
35 };
36
37
Chris Lattnerda272d12010-02-15 08:04:42 +000038 class MatcherGen {
39 const PatternToMatch &Pattern;
40 const CodeGenDAGPatterns &CGP;
41
42 /// PatWithNoTypes - This is a clone of Pattern.getSrcPattern() that starts
43 /// out with all of the types removed. This allows us to insert type checks
44 /// as we scan the tree.
45 TreePatternNode *PatWithNoTypes;
46
47 /// VariableMap - A map from variable names ('$dst') to the recorded operand
48 /// number that they were captured as. These are biased by 1 to make
49 /// insertion easier.
50 StringMap<unsigned> VariableMap;
Chris Lattnerc94fa4c2010-02-19 00:27:40 +000051
52 /// NextRecordedOperandNo - As we emit opcodes to record matched values in
53 /// the RecordedNodes array, this keeps track of which slot will be next to
54 /// record into.
Chris Lattnerda272d12010-02-15 08:04:42 +000055 unsigned NextRecordedOperandNo;
56
Chris Lattner785d16f2010-02-17 02:16:19 +000057 /// InputChains - This maintains the position in the recorded nodes array of
58 /// all of the recorded input chains.
59 SmallVector<unsigned, 2> InputChains;
60
61 /// Matcher - This is the top level of the generated matcher, the result.
Chris Lattner8ef9c792010-02-18 02:49:24 +000062 MatcherNode *Matcher;
Chris Lattner785d16f2010-02-17 02:16:19 +000063
64 /// CurPredicate - As we emit matcher nodes, this points to the latest check
Chris Lattnerbd8227f2010-02-18 02:53:41 +000065 /// which should have future checks stuck into its Next position.
Chris Lattner8ef9c792010-02-18 02:49:24 +000066 MatcherNode *CurPredicate;
Chris Lattnerda272d12010-02-15 08:04:42 +000067 public:
68 MatcherGen(const PatternToMatch &pattern, const CodeGenDAGPatterns &cgp);
69
70 ~MatcherGen() {
71 delete PatWithNoTypes;
72 }
73
74 void EmitMatcherCode();
Chris Lattnerb49985a2010-02-18 06:47:49 +000075 void EmitResultCode();
Chris Lattnerda272d12010-02-15 08:04:42 +000076
Chris Lattner8ef9c792010-02-18 02:49:24 +000077 MatcherNode *GetMatcher() const { return Matcher; }
78 MatcherNode *GetCurPredicate() const { return CurPredicate; }
Chris Lattnerda272d12010-02-15 08:04:42 +000079 private:
Chris Lattner8ef9c792010-02-18 02:49:24 +000080 void AddMatcherNode(MatcherNode *NewNode);
Chris Lattnerda272d12010-02-15 08:04:42 +000081 void InferPossibleTypes();
Chris Lattnerb49985a2010-02-18 06:47:49 +000082
83 // Matcher Generation.
Chris Lattnerda272d12010-02-15 08:04:42 +000084 void EmitMatchCode(const TreePatternNode *N, TreePatternNode *NodeNoTypes);
85 void EmitLeafMatchCode(const TreePatternNode *N);
86 void EmitOperatorMatchCode(const TreePatternNode *N,
87 TreePatternNode *NodeNoTypes);
Chris Lattnerb49985a2010-02-18 06:47:49 +000088
89 // Result Code Generation.
90 void EmitResultOperand(const TreePatternNode *N,
Chris Lattnerc94fa4c2010-02-19 00:27:40 +000091 SmallVectorImpl<ResultVal> &ResultOps);
Chris Lattnerb49985a2010-02-18 06:47:49 +000092 void EmitResultLeafAsOperand(const TreePatternNode *N,
Chris Lattnerc94fa4c2010-02-19 00:27:40 +000093 SmallVectorImpl<ResultVal> &ResultOps);
Chris Lattnerb49985a2010-02-18 06:47:49 +000094 void EmitResultInstructionAsOperand(const TreePatternNode *N,
Chris Lattnerc94fa4c2010-02-19 00:27:40 +000095 SmallVectorImpl<ResultVal> &ResultOps);
Chris Lattnerda272d12010-02-15 08:04:42 +000096 };
97
98} // end anon namespace.
99
100MatcherGen::MatcherGen(const PatternToMatch &pattern,
101 const CodeGenDAGPatterns &cgp)
Chris Lattner853b9192010-02-19 00:33:13 +0000102: Pattern(pattern), CGP(cgp), NextRecordedOperandNo(0),
Chris Lattnerda272d12010-02-15 08:04:42 +0000103 Matcher(0), CurPredicate(0) {
104 // We need to produce the matcher tree for the patterns source pattern. To do
105 // this we need to match the structure as well as the types. To do the type
106 // matching, we want to figure out the fewest number of type checks we need to
107 // emit. For example, if there is only one integer type supported by a
108 // target, there should be no type comparisons at all for integer patterns!
109 //
110 // To figure out the fewest number of type checks needed, clone the pattern,
111 // remove the types, then perform type inference on the pattern as a whole.
112 // If there are unresolved types, emit an explicit check for those types,
113 // apply the type to the tree, then rerun type inference. Iterate until all
114 // types are resolved.
115 //
116 PatWithNoTypes = Pattern.getSrcPattern()->clone();
117 PatWithNoTypes->RemoveAllTypes();
118
119 // If there are types that are manifestly known, infer them.
120 InferPossibleTypes();
121}
122
123/// InferPossibleTypes - As we emit the pattern, we end up generating type
124/// checks and applying them to the 'PatWithNoTypes' tree. As we do this, we
125/// want to propagate implied types as far throughout the tree as possible so
126/// that we avoid doing redundant type checks. This does the type propagation.
127void MatcherGen::InferPossibleTypes() {
128 // TP - Get *SOME* tree pattern, we don't care which. It is only used for
129 // diagnostics, which we know are impossible at this point.
130 TreePattern &TP = *CGP.pf_begin()->second;
131
132 try {
133 bool MadeChange = true;
134 while (MadeChange)
135 MadeChange = PatWithNoTypes->ApplyTypeConstraints(TP,
136 true/*Ignore reg constraints*/);
137 } catch (...) {
138 errs() << "Type constraint application shouldn't fail!";
139 abort();
140 }
141}
142
143
144/// AddMatcherNode - Add a matcher node to the current graph we're building.
Chris Lattner8ef9c792010-02-18 02:49:24 +0000145void MatcherGen::AddMatcherNode(MatcherNode *NewNode) {
Chris Lattnerda272d12010-02-15 08:04:42 +0000146 if (CurPredicate != 0)
Chris Lattnerbd8227f2010-02-18 02:53:41 +0000147 CurPredicate->setNext(NewNode);
Chris Lattnerda272d12010-02-15 08:04:42 +0000148 else
149 Matcher = NewNode;
150 CurPredicate = NewNode;
151}
152
153
Chris Lattnerb49985a2010-02-18 06:47:49 +0000154//===----------------------------------------------------------------------===//
155// Pattern Match Generation
156//===----------------------------------------------------------------------===//
Chris Lattnerda272d12010-02-15 08:04:42 +0000157
158/// EmitLeafMatchCode - Generate matching code for leaf nodes.
159void MatcherGen::EmitLeafMatchCode(const TreePatternNode *N) {
160 assert(N->isLeaf() && "Not a leaf?");
161 // Direct match against an integer constant.
162 if (IntInit *II = dynamic_cast<IntInit*>(N->getLeafValue()))
163 return AddMatcherNode(new CheckIntegerMatcherNode(II->getValue()));
164
165 DefInit *DI = dynamic_cast<DefInit*>(N->getLeafValue());
166 if (DI == 0) {
167 errs() << "Unknown leaf kind: " << *DI << "\n";
168 abort();
169 }
170
171 Record *LeafRec = DI->getDef();
172 if (// Handle register references. Nothing to do here, they always match.
173 LeafRec->isSubClassOf("RegisterClass") ||
174 LeafRec->isSubClassOf("PointerLikeRegClass") ||
175 LeafRec->isSubClassOf("Register") ||
176 // Place holder for SRCVALUE nodes. Nothing to do here.
177 LeafRec->getName() == "srcvalue")
178 return;
179
180 if (LeafRec->isSubClassOf("ValueType"))
181 return AddMatcherNode(new CheckValueTypeMatcherNode(LeafRec->getName()));
182
183 if (LeafRec->isSubClassOf("CondCode"))
184 return AddMatcherNode(new CheckCondCodeMatcherNode(LeafRec->getName()));
185
186 if (LeafRec->isSubClassOf("ComplexPattern")) {
Chris Lattnerc2676b22010-02-17 00:11:30 +0000187 // We can't model ComplexPattern uses that don't have their name taken yet.
188 // The OPC_CheckComplexPattern operation implicitly records the results.
189 if (N->getName().empty()) {
Chris Lattner53a2f602010-02-16 23:16:25 +0000190 errs() << "We expect complex pattern uses to have names: " << *N << "\n";
191 exit(1);
192 }
193
Chris Lattnerda272d12010-02-15 08:04:42 +0000194 // Handle complex pattern.
195 const ComplexPattern &CP = CGP.getComplexPattern(LeafRec);
Chris Lattner8dc4f2b2010-02-17 06:08:25 +0000196 AddMatcherNode(new CheckComplexPatMatcherNode(CP));
197
198 // If the complex pattern has a chain, then we need to keep track of the
199 // fact that we just recorded a chain input. The chain input will be
200 // matched as the last operand of the predicate if it was successful.
201 if (CP.hasProperty(SDNPHasChain)) {
202 // It is the last operand recorded.
203 assert(NextRecordedOperandNo > 1 &&
204 "Should have recorded input/result chains at least!");
205 InputChains.push_back(NextRecordedOperandNo-1);
Chris Lattner9a747f12010-02-17 06:23:39 +0000206
207 // IF we need to check chains, do so, see comment for
208 // "NodeHasProperty(SDNPHasChain" below.
209 if (InputChains.size() > 1) {
210 // FIXME: This is broken, we should eliminate this nonsense completely,
211 // but we want to produce the same selections that the old matcher does
212 // for now.
213 unsigned PrevOp = InputChains[InputChains.size()-2];
214 AddMatcherNode(new CheckChainCompatibleMatcherNode(PrevOp));
215 }
Chris Lattner8dc4f2b2010-02-17 06:08:25 +0000216 }
217 return;
Chris Lattnerda272d12010-02-15 08:04:42 +0000218 }
219
220 errs() << "Unknown leaf kind: " << *N << "\n";
221 abort();
222}
223
224void MatcherGen::EmitOperatorMatchCode(const TreePatternNode *N,
225 TreePatternNode *NodeNoTypes) {
226 assert(!N->isLeaf() && "Not an operator?");
227 const SDNodeInfo &CInfo = CGP.getSDNodeInfo(N->getOperator());
228
229 // If this is an 'and R, 1234' where the operation is AND/OR and the RHS is
230 // a constant without a predicate fn that has more that one bit set, handle
231 // this as a special case. This is usually for targets that have special
232 // handling of certain large constants (e.g. alpha with it's 8/16/32-bit
233 // handling stuff). Using these instructions is often far more efficient
234 // than materializing the constant. Unfortunately, both the instcombiner
235 // and the dag combiner can often infer that bits are dead, and thus drop
236 // them from the mask in the dag. For example, it might turn 'AND X, 255'
237 // into 'AND X, 254' if it knows the low bit is set. Emit code that checks
238 // to handle this.
239 if ((N->getOperator()->getName() == "and" ||
240 N->getOperator()->getName() == "or") &&
241 N->getChild(1)->isLeaf() && N->getChild(1)->getPredicateFns().empty()) {
242 if (IntInit *II = dynamic_cast<IntInit*>(N->getChild(1)->getLeafValue())) {
243 if (!isPowerOf2_32(II->getValue())) { // Don't bother with single bits.
244 if (N->getOperator()->getName() == "and")
245 AddMatcherNode(new CheckAndImmMatcherNode(II->getValue()));
246 else
247 AddMatcherNode(new CheckOrImmMatcherNode(II->getValue()));
248
249 // Match the LHS of the AND as appropriate.
250 AddMatcherNode(new MoveChildMatcherNode(0));
251 EmitMatchCode(N->getChild(0), NodeNoTypes->getChild(0));
252 AddMatcherNode(new MoveParentMatcherNode());
253 return;
254 }
255 }
256 }
257
258 // Check that the current opcode lines up.
259 AddMatcherNode(new CheckOpcodeMatcherNode(CInfo.getEnumName()));
260
261 // If this node has a chain, then the chain is operand #0 is the SDNode, and
262 // the child numbers of the node are all offset by one.
263 unsigned OpNo = 0;
Chris Lattner6bc1b512010-02-16 19:19:58 +0000264 if (N->NodeHasProperty(SDNPHasChain, CGP)) {
Chris Lattner2f7ecde2010-02-17 01:34:15 +0000265 // Record the input chain, which is always input #0 of the SDNode.
266 AddMatcherNode(new MoveChildMatcherNode(0));
Chris Lattner2f7ecde2010-02-17 01:34:15 +0000267 AddMatcherNode(new RecordMatcherNode("'" + N->getOperator()->getName() +
268 "' input chain"));
Chris Lattner785d16f2010-02-17 02:16:19 +0000269
270 // Remember all of the input chains our pattern will match.
271 InputChains.push_back(NextRecordedOperandNo);
272 ++NextRecordedOperandNo;
Chris Lattner2f7ecde2010-02-17 01:34:15 +0000273 AddMatcherNode(new MoveParentMatcherNode());
Chris Lattner785d16f2010-02-17 02:16:19 +0000274
275 // If this is the second (e.g. indbr(load) or store(add(load))) or third
276 // input chain (e.g. (store (add (load, load))) from msp430) we need to make
277 // sure that folding the chain won't induce cycles in the DAG. This could
278 // happen if there were an intermediate node between the indbr and load, for
279 // example.
Chris Lattner9a747f12010-02-17 06:23:39 +0000280 if (InputChains.size() > 1) {
281 // FIXME: This is broken, we should eliminate this nonsense completely,
282 // but we want to produce the same selections that the old matcher does
283 // for now.
284 unsigned PrevOp = InputChains[InputChains.size()-2];
285 AddMatcherNode(new CheckChainCompatibleMatcherNode(PrevOp));
286 }
Chris Lattner785d16f2010-02-17 02:16:19 +0000287
Chris Lattner2f7ecde2010-02-17 01:34:15 +0000288 // Don't look at the input chain when matching the tree pattern to the
289 // SDNode.
Chris Lattnerda272d12010-02-15 08:04:42 +0000290 OpNo = 1;
291
Chris Lattner6bc1b512010-02-16 19:19:58 +0000292 // If this node is not the root and the subtree underneath it produces a
293 // chain, then the result of matching the node is also produce a chain.
294 // Beyond that, this means that we're also folding (at least) the root node
295 // into the node that produce the chain (for example, matching
296 // "(add reg, (load ptr))" as a add_with_memory on X86). This is
297 // problematic, if the 'reg' node also uses the load (say, its chain).
298 // Graphically:
299 //
300 // [LD]
301 // ^ ^
302 // | \ DAG's like cheese.
303 // / |
304 // / [YY]
305 // | ^
306 // [XX]--/
307 //
308 // It would be invalid to fold XX and LD. In this case, folding the two
309 // nodes together would induce a cycle in the DAG, making it a 'cyclic DAG'
310 // To prevent this, we emit a dynamic check for legality before allowing
311 // this to be folded.
312 //
313 const TreePatternNode *Root = Pattern.getSrcPattern();
314 if (N != Root) { // Not the root of the pattern.
Chris Lattnere39650a2010-02-16 06:10:58 +0000315 // If there is a node between the root and this node, then we definitely
316 // need to emit the check.
317 bool NeedCheck = !Root->hasChild(N);
318
319 // If it *is* an immediate child of the root, we can still need a check if
320 // the root SDNode has multiple inputs. For us, this means that it is an
321 // intrinsic, has multiple operands, or has other inputs like chain or
322 // flag).
323 if (!NeedCheck) {
324 const SDNodeInfo &PInfo = CGP.getSDNodeInfo(Root->getOperator());
325 NeedCheck =
326 Root->getOperator() == CGP.get_intrinsic_void_sdnode() ||
327 Root->getOperator() == CGP.get_intrinsic_w_chain_sdnode() ||
328 Root->getOperator() == CGP.get_intrinsic_wo_chain_sdnode() ||
329 PInfo.getNumOperands() > 1 ||
330 PInfo.hasProperty(SDNPHasChain) ||
331 PInfo.hasProperty(SDNPInFlag) ||
332 PInfo.hasProperty(SDNPOptInFlag);
333 }
334
335 if (NeedCheck)
Chris Lattner21390d72010-02-16 19:15:55 +0000336 AddMatcherNode(new CheckFoldableChainNodeMatcherNode());
Chris Lattnere39650a2010-02-16 06:10:58 +0000337 }
Chris Lattnerda272d12010-02-15 08:04:42 +0000338 }
339
Chris Lattnerda272d12010-02-15 08:04:42 +0000340 for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i, ++OpNo) {
341 // Get the code suitable for matching this child. Move to the child, check
342 // it then move back to the parent.
Chris Lattnerc642b842010-02-17 01:27:29 +0000343 AddMatcherNode(new MoveChildMatcherNode(OpNo));
Chris Lattnerda272d12010-02-15 08:04:42 +0000344 EmitMatchCode(N->getChild(i), NodeNoTypes->getChild(i));
345 AddMatcherNode(new MoveParentMatcherNode());
346 }
347}
348
349
350void MatcherGen::EmitMatchCode(const TreePatternNode *N,
351 TreePatternNode *NodeNoTypes) {
352 // If N and NodeNoTypes don't agree on a type, then this is a case where we
353 // need to do a type check. Emit the check, apply the tyep to NodeNoTypes and
354 // reinfer any correlated types.
355 if (NodeNoTypes->getExtTypes() != N->getExtTypes()) {
356 AddMatcherNode(new CheckTypeMatcherNode(N->getTypeNum(0)));
357 NodeNoTypes->setTypes(N->getExtTypes());
358 InferPossibleTypes();
359 }
360
Chris Lattnerda272d12010-02-15 08:04:42 +0000361 // If this node has a name associated with it, capture it in VariableMap. If
362 // we already saw this in the pattern, emit code to verify dagness.
363 if (!N->getName().empty()) {
364 unsigned &VarMapEntry = VariableMap[N->getName()];
365 if (VarMapEntry == 0) {
Chris Lattnere609a512010-02-17 00:31:50 +0000366 VarMapEntry = NextRecordedOperandNo+1;
367
368 unsigned NumRecorded;
Chris Lattner53a2f602010-02-16 23:16:25 +0000369
370 // If this is a complex pattern, the match operation for it will
371 // implicitly record all of the outputs of it (which may be more than
372 // one).
373 if (const ComplexPattern *AM = N->getComplexPatternInfo(CGP)) {
374 // Record the right number of operands.
Chris Lattnere609a512010-02-17 00:31:50 +0000375 NumRecorded = AM->getNumOperands()-1;
376
377 if (AM->hasProperty(SDNPHasChain))
378 NumRecorded += 2; // Input and output chains.
Chris Lattner53a2f602010-02-16 23:16:25 +0000379 } else {
380 // If it is a normal named node, we must emit a 'Record' opcode.
Chris Lattnerc642b842010-02-17 01:27:29 +0000381 AddMatcherNode(new RecordMatcherNode("$" + N->getName()));
Chris Lattnere609a512010-02-17 00:31:50 +0000382 NumRecorded = 1;
Chris Lattner53a2f602010-02-16 23:16:25 +0000383 }
Chris Lattnere609a512010-02-17 00:31:50 +0000384 NextRecordedOperandNo += NumRecorded;
Chris Lattner53a2f602010-02-16 23:16:25 +0000385
Chris Lattnerda272d12010-02-15 08:04:42 +0000386 } else {
387 // If we get here, this is a second reference to a specific name. Since
388 // we already have checked that the first reference is valid, we don't
389 // have to recursively match it, just check that it's the same as the
390 // previously named thing.
391 AddMatcherNode(new CheckSameMatcherNode(VarMapEntry-1));
392 return;
393 }
394 }
395
396 // If there are node predicates for this node, generate their checks.
397 for (unsigned i = 0, e = N->getPredicateFns().size(); i != e; ++i)
398 AddMatcherNode(new CheckPredicateMatcherNode(N->getPredicateFns()[i]));
399
400 if (N->isLeaf())
401 EmitLeafMatchCode(N);
402 else
403 EmitOperatorMatchCode(N, NodeNoTypes);
404}
405
406void MatcherGen::EmitMatcherCode() {
407 // If the pattern has a predicate on it (e.g. only enabled when a subtarget
408 // feature is around, do the check).
409 if (!Pattern.getPredicateCheck().empty())
410 AddMatcherNode(new
411 CheckPatternPredicateMatcherNode(Pattern.getPredicateCheck()));
412
413 // Emit the matcher for the pattern structure and types.
414 EmitMatchCode(Pattern.getSrcPattern(), PatWithNoTypes);
415}
416
417
Chris Lattnerb49985a2010-02-18 06:47:49 +0000418//===----------------------------------------------------------------------===//
419// Node Result Generation
420//===----------------------------------------------------------------------===//
421
422void MatcherGen::EmitResultLeafAsOperand(const TreePatternNode *N,
Chris Lattnerc94fa4c2010-02-19 00:27:40 +0000423 SmallVectorImpl<ResultVal> &ResultOps){
Chris Lattner845c0422010-02-18 22:03:03 +0000424 assert(N->isLeaf() && "Must be a leaf");
425
426 if (IntInit *II = dynamic_cast<IntInit*>(N->getLeafValue())) {
427 AddMatcherNode(new EmitIntegerMatcherNode(II->getValue(),N->getTypeNum(0)));
Chris Lattner853b9192010-02-19 00:33:13 +0000428 ResultOps.push_back(ResultVal::get(NextRecordedOperandNo++));
Chris Lattner845c0422010-02-18 22:03:03 +0000429 return;
430 }
431
432 // If this is an explicit register reference, handle it.
433 if (DefInit *DI = dynamic_cast<DefInit*>(N->getLeafValue())) {
434 if (DI->getDef()->isSubClassOf("Register")) {
435 AddMatcherNode(new EmitRegisterMatcherNode(DI->getDef(),
436 N->getTypeNum(0)));
Chris Lattner853b9192010-02-19 00:33:13 +0000437 ResultOps.push_back(ResultVal::get(NextRecordedOperandNo++));
Chris Lattner845c0422010-02-18 22:03:03 +0000438 return;
439 }
440
441 if (DI->getDef()->getName() == "zero_reg") {
442 AddMatcherNode(new EmitRegisterMatcherNode(0, N->getTypeNum(0)));
Chris Lattner853b9192010-02-19 00:33:13 +0000443 ResultOps.push_back(ResultVal::get(NextRecordedOperandNo++));
Chris Lattner845c0422010-02-18 22:03:03 +0000444 return;
445 }
446
447#if 0
448 if (DI->getDef()->isSubClassOf("RegisterClass")) {
449 // Handle a reference to a register class. This is used
450 // in COPY_TO_SUBREG instructions.
451 // FIXME: Implement.
452 }
453#endif
454 }
455
Chris Lattnerb49985a2010-02-18 06:47:49 +0000456 errs() << "unhandled leaf node: \n";
457 N->dump();
458}
459
460void MatcherGen::EmitResultInstructionAsOperand(const TreePatternNode *N,
Chris Lattnerc94fa4c2010-02-19 00:27:40 +0000461 SmallVectorImpl<ResultVal> &ResultOps){
Chris Lattnerb49985a2010-02-18 06:47:49 +0000462 Record *Op = N->getOperator();
463 const CodeGenTarget &CGT = CGP.getTargetInfo();
464 CodeGenInstruction &II = CGT.getInstruction(Op->getName());
465 const DAGInstruction &Inst = CGP.getInstruction(Op);
466
467 // FIXME: Handle (set x, (foo))
468
469 if (II.isVariadic) // FIXME: Handle variadic instructions.
470 return AddMatcherNode(new EmitNodeMatcherNode(Pattern));
471
472 // FIXME: Handle OptInFlag, HasInFlag, HasOutFlag
473 // FIXME: Handle Chains.
474 unsigned NumResults = Inst.getNumResults();
475
476
477 // Loop over all of the operands of the instruction pattern, emitting code
478 // to fill them all in. The node 'N' usually has number children equal to
479 // the number of input operands of the instruction. However, in cases
480 // where there are predicate operands for an instruction, we need to fill
481 // in the 'execute always' values. Match up the node operands to the
482 // instruction operands to do this.
Chris Lattnerc94fa4c2010-02-19 00:27:40 +0000483 SmallVector<ResultVal, 8> Ops;
Chris Lattnerb49985a2010-02-18 06:47:49 +0000484 for (unsigned ChildNo = 0, InstOpNo = NumResults, e = II.OperandList.size();
485 InstOpNo != e; ++InstOpNo) {
486
487 // Determine what to emit for this operand.
488 Record *OperandNode = II.OperandList[InstOpNo].Rec;
489 if ((OperandNode->isSubClassOf("PredicateOperand") ||
490 OperandNode->isSubClassOf("OptionalDefOperand")) &&
491 !CGP.getDefaultOperand(OperandNode).DefaultOps.empty()) {
492 // This is a predicate or optional def operand; emit the
493 // 'default ops' operands.
494 const DAGDefaultOperand &DefaultOp =
495 CGP.getDefaultOperand(II.OperandList[InstOpNo].Rec);
496 for (unsigned i = 0, e = DefaultOp.DefaultOps.size(); i != e; ++i)
497 EmitResultOperand(DefaultOp.DefaultOps[i], Ops);
498 continue;
499 }
500
501 // Otherwise this is a normal operand or a predicate operand without
502 // 'execute always'; emit it.
503 EmitResultOperand(N->getChild(ChildNo), Ops);
504 ++ChildNo;
505 }
506
507 // FIXME: Chain.
508 // FIXME: Flag
509
510
511
512 return;
513}
514
515void MatcherGen::EmitResultOperand(const TreePatternNode *N,
Chris Lattnerc94fa4c2010-02-19 00:27:40 +0000516 SmallVectorImpl<ResultVal> &ResultOps) {
Chris Lattnerb49985a2010-02-18 06:47:49 +0000517 // This is something selected from the pattern we matched.
518 if (!N->getName().empty()) {
519 //errs() << "unhandled named node: \n";
520 //N->dump();
521 return;
522 }
523
524 if (N->isLeaf())
525 return EmitResultLeafAsOperand(N, ResultOps);
526
527 Record *OpRec = N->getOperator();
528 if (OpRec->isSubClassOf("Instruction"))
529 return EmitResultInstructionAsOperand(N, ResultOps);
530 if (OpRec->isSubClassOf("SDNodeXForm"))
531 // FIXME: implement.
532 return;
533 errs() << "Unknown result node to emit code for: " << *N << '\n';
534 throw std::string("Unknown node in result pattern!");
535}
536
537void MatcherGen::EmitResultCode() {
538 // FIXME: Handle Ops.
539 // FIXME: Ops should be vector of "ResultValue> which is either an index into
540 // the results vector is is a temp result.
Chris Lattnerc94fa4c2010-02-19 00:27:40 +0000541 SmallVector<ResultVal, 8> Ops;
Chris Lattnerb49985a2010-02-18 06:47:49 +0000542 EmitResultOperand(Pattern.getDstPattern(), Ops);
543 //AddMatcherNode(new EmitNodeMatcherNode(Pattern));
544}
545
546
Chris Lattnerda272d12010-02-15 08:04:42 +0000547MatcherNode *llvm::ConvertPatternToMatcher(const PatternToMatch &Pattern,
548 const CodeGenDAGPatterns &CGP) {
549 MatcherGen Gen(Pattern, CGP);
550
551 // Generate the code for the matcher.
552 Gen.EmitMatcherCode();
553
554 // If the match succeeds, then we generate Pattern.
Chris Lattnerb49985a2010-02-18 06:47:49 +0000555 Gen.EmitResultCode();
Chris Lattnerda272d12010-02-15 08:04:42 +0000556
557 // Unconditional match.
Chris Lattnerb49985a2010-02-18 06:47:49 +0000558 return Gen.GetMatcher();
Chris Lattnerda272d12010-02-15 08:04:42 +0000559}
560
561
562