1 //===- DAGISelMatcherGen.cpp - Matcher generator --------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "DAGISelMatcher.h"
11 #include "CodeGenDAGPatterns.h"
12 #include "CodeGenRegisters.h"
13 #include "llvm/ADT/DenseMap.h"
14 #include "llvm/ADT/SmallVector.h"
15 #include "llvm/ADT/StringMap.h"
16 #include "llvm/TableGen/Error.h"
17 #include "llvm/TableGen/Record.h"
22 /// getRegisterValueType - Look up and return the ValueType of the specified
23 /// register. If the register is a member of multiple register classes which
24 /// have different associated types, return MVT::Other.
25 static MVT::SimpleValueType getRegisterValueType(Record *R,
26 const CodeGenTarget &T) {
28 MVT::SimpleValueType VT = MVT::Other;
29 const CodeGenRegister *Reg = T.getRegBank().getReg(R);
30 ArrayRef<CodeGenRegisterClass*> RCs = T.getRegBank().getRegClasses();
32 for (unsigned rc = 0, e = RCs.size(); rc != e; ++rc) {
33 const CodeGenRegisterClass &RC = *RCs[rc];
34 if (!RC.contains(Reg))
39 VT = RC.getValueTypeNum(0);
43 // If this occurs in multiple register classes, they all have to agree.
44 assert(VT == RC.getValueTypeNum(0));
52 const PatternToMatch &Pattern;
53 const CodeGenDAGPatterns &CGP;
55 /// PatWithNoTypes - This is a clone of Pattern.getSrcPattern() that starts
56 /// out with all of the types removed. This allows us to insert type checks
57 /// as we scan the tree.
58 TreePatternNode *PatWithNoTypes;
60 /// VariableMap - A map from variable names ('$dst') to the recorded operand
61 /// number that they were captured as. These are biased by 1 to make
63 StringMap<unsigned> VariableMap;
65 /// NextRecordedOperandNo - As we emit opcodes to record matched values in
66 /// the RecordedNodes array, this keeps track of which slot will be next to
68 unsigned NextRecordedOperandNo;
70 /// MatchedChainNodes - This maintains the position in the recorded nodes
71 /// array of all of the recorded input nodes that have chains.
72 SmallVector<unsigned, 2> MatchedChainNodes;
74 /// MatchedGlueResultNodes - This maintains the position in the recorded
75 /// nodes array of all of the recorded input nodes that have glue results.
76 SmallVector<unsigned, 2> MatchedGlueResultNodes;
78 /// MatchedComplexPatterns - This maintains a list of all of the
79 /// ComplexPatterns that we need to check. The patterns are known to have
80 /// names which were recorded. The second element of each pair is the first
81 /// slot number that the OPC_CheckComplexPat opcode drops the matched
83 SmallVector<std::pair<const TreePatternNode*,
84 unsigned>, 2> MatchedComplexPatterns;
86 /// PhysRegInputs - List list has an entry for each explicitly specified
87 /// physreg input to the pattern. The first elt is the Register node, the
88 /// second is the recorded slot number the input pattern match saved it in.
89 SmallVector<std::pair<Record*, unsigned>, 2> PhysRegInputs;
91 /// Matcher - This is the top level of the generated matcher, the result.
94 /// CurPredicate - As we emit matcher nodes, this points to the latest check
95 /// which should have future checks stuck into its Next position.
96 Matcher *CurPredicate;
98 MatcherGen(const PatternToMatch &pattern, const CodeGenDAGPatterns &cgp);
101 delete PatWithNoTypes;
104 bool EmitMatcherCode(unsigned Variant);
105 void EmitResultCode();
107 Matcher *GetMatcher() const { return TheMatcher; }
109 void AddMatcher(Matcher *NewNode);
110 void InferPossibleTypes();
112 // Matcher Generation.
113 void EmitMatchCode(const TreePatternNode *N, TreePatternNode *NodeNoTypes);
114 void EmitLeafMatchCode(const TreePatternNode *N);
115 void EmitOperatorMatchCode(const TreePatternNode *N,
116 TreePatternNode *NodeNoTypes);
118 // Result Code Generation.
119 unsigned getNamedArgumentSlot(StringRef Name) {
120 unsigned VarMapEntry = VariableMap[Name];
121 assert(VarMapEntry != 0 &&
122 "Variable referenced but not defined and not caught earlier!");
123 return VarMapEntry-1;
126 /// GetInstPatternNode - Get the pattern for an instruction.
127 const TreePatternNode *GetInstPatternNode(const DAGInstruction &Ins,
128 const TreePatternNode *N);
130 void EmitResultOperand(const TreePatternNode *N,
131 SmallVectorImpl<unsigned> &ResultOps);
132 void EmitResultOfNamedOperand(const TreePatternNode *N,
133 SmallVectorImpl<unsigned> &ResultOps);
134 void EmitResultLeafAsOperand(const TreePatternNode *N,
135 SmallVectorImpl<unsigned> &ResultOps);
136 void EmitResultInstructionAsOperand(const TreePatternNode *N,
137 SmallVectorImpl<unsigned> &ResultOps);
138 void EmitResultSDNodeXFormAsOperand(const TreePatternNode *N,
139 SmallVectorImpl<unsigned> &ResultOps);
142 } // end anon namespace.
144 MatcherGen::MatcherGen(const PatternToMatch &pattern,
145 const CodeGenDAGPatterns &cgp)
146 : Pattern(pattern), CGP(cgp), NextRecordedOperandNo(0),
147 TheMatcher(0), CurPredicate(0) {
148 // We need to produce the matcher tree for the patterns source pattern. To do
149 // this we need to match the structure as well as the types. To do the type
150 // matching, we want to figure out the fewest number of type checks we need to
151 // emit. For example, if there is only one integer type supported by a
152 // target, there should be no type comparisons at all for integer patterns!
154 // To figure out the fewest number of type checks needed, clone the pattern,
155 // remove the types, then perform type inference on the pattern as a whole.
156 // If there are unresolved types, emit an explicit check for those types,
157 // apply the type to the tree, then rerun type inference. Iterate until all
158 // types are resolved.
160 PatWithNoTypes = Pattern.getSrcPattern()->clone();
161 PatWithNoTypes->RemoveAllTypes();
163 // If there are types that are manifestly known, infer them.
164 InferPossibleTypes();
167 /// InferPossibleTypes - As we emit the pattern, we end up generating type
168 /// checks and applying them to the 'PatWithNoTypes' tree. As we do this, we
169 /// want to propagate implied types as far throughout the tree as possible so
170 /// that we avoid doing redundant type checks. This does the type propagation.
171 void MatcherGen::InferPossibleTypes() {
172 // TP - Get *SOME* tree pattern, we don't care which. It is only used for
173 // diagnostics, which we know are impossible at this point.
174 TreePattern &TP = *CGP.pf_begin()->second;
176 bool MadeChange = true;
178 MadeChange = PatWithNoTypes->ApplyTypeConstraints(TP,
179 true/*Ignore reg constraints*/);
183 /// AddMatcher - Add a matcher node to the current graph we're building.
184 void MatcherGen::AddMatcher(Matcher *NewNode) {
185 if (CurPredicate != 0)
186 CurPredicate->setNext(NewNode);
188 TheMatcher = NewNode;
189 CurPredicate = NewNode;
193 //===----------------------------------------------------------------------===//
194 // Pattern Match Generation
195 //===----------------------------------------------------------------------===//
197 /// EmitLeafMatchCode - Generate matching code for leaf nodes.
198 void MatcherGen::EmitLeafMatchCode(const TreePatternNode *N) {
199 assert(N->isLeaf() && "Not a leaf?");
201 // Direct match against an integer constant.
202 if (IntInit *II = dyn_cast<IntInit>(N->getLeafValue())) {
203 // If this is the root of the dag we're matching, we emit a redundant opcode
204 // check to ensure that this gets folded into the normal top-level
206 if (N == Pattern.getSrcPattern()) {
207 const SDNodeInfo &NI = CGP.getSDNodeInfo(CGP.getSDNodeNamed("imm"));
208 AddMatcher(new CheckOpcodeMatcher(NI));
211 return AddMatcher(new CheckIntegerMatcher(II->getValue()));
214 DefInit *DI = dyn_cast<DefInit>(N->getLeafValue());
216 errs() << "Unknown leaf kind: " << *N << "\n";
220 Record *LeafRec = DI->getDef();
221 if (// Handle register references. Nothing to do here, they always match.
222 LeafRec->isSubClassOf("RegisterClass") ||
223 LeafRec->isSubClassOf("RegisterOperand") ||
224 LeafRec->isSubClassOf("PointerLikeRegClass") ||
225 LeafRec->isSubClassOf("SubRegIndex") ||
226 // Place holder for SRCVALUE nodes. Nothing to do here.
227 LeafRec->getName() == "srcvalue")
230 // If we have a physreg reference like (mul gpr:$src, EAX) then we need to
231 // record the register
232 if (LeafRec->isSubClassOf("Register")) {
233 AddMatcher(new RecordMatcher("physreg input "+LeafRec->getName(),
234 NextRecordedOperandNo));
235 PhysRegInputs.push_back(std::make_pair(LeafRec, NextRecordedOperandNo++));
239 if (LeafRec->isSubClassOf("ValueType"))
240 return AddMatcher(new CheckValueTypeMatcher(LeafRec->getName()));
242 if (LeafRec->isSubClassOf("CondCode"))
243 return AddMatcher(new CheckCondCodeMatcher(LeafRec->getName()));
245 if (LeafRec->isSubClassOf("ComplexPattern")) {
246 // We can't model ComplexPattern uses that don't have their name taken yet.
247 // The OPC_CheckComplexPattern operation implicitly records the results.
248 if (N->getName().empty()) {
249 errs() << "We expect complex pattern uses to have names: " << *N << "\n";
253 // Remember this ComplexPattern so that we can emit it after all the other
254 // structural matches are done.
255 MatchedComplexPatterns.push_back(std::make_pair(N, 0));
259 errs() << "Unknown leaf kind: " << *N << "\n";
263 void MatcherGen::EmitOperatorMatchCode(const TreePatternNode *N,
264 TreePatternNode *NodeNoTypes) {
265 assert(!N->isLeaf() && "Not an operator?");
266 const SDNodeInfo &CInfo = CGP.getSDNodeInfo(N->getOperator());
268 // If this is an 'and R, 1234' where the operation is AND/OR and the RHS is
269 // a constant without a predicate fn that has more that one bit set, handle
270 // this as a special case. This is usually for targets that have special
271 // handling of certain large constants (e.g. alpha with it's 8/16/32-bit
272 // handling stuff). Using these instructions is often far more efficient
273 // than materializing the constant. Unfortunately, both the instcombiner
274 // and the dag combiner can often infer that bits are dead, and thus drop
275 // them from the mask in the dag. For example, it might turn 'AND X, 255'
276 // into 'AND X, 254' if it knows the low bit is set. Emit code that checks
278 if ((N->getOperator()->getName() == "and" ||
279 N->getOperator()->getName() == "or") &&
280 N->getChild(1)->isLeaf() && N->getChild(1)->getPredicateFns().empty() &&
281 N->getPredicateFns().empty()) {
282 if (IntInit *II = dyn_cast<IntInit>(N->getChild(1)->getLeafValue())) {
283 if (!isPowerOf2_32(II->getValue())) { // Don't bother with single bits.
284 // If this is at the root of the pattern, we emit a redundant
285 // CheckOpcode so that the following checks get factored properly under
286 // a single opcode check.
287 if (N == Pattern.getSrcPattern())
288 AddMatcher(new CheckOpcodeMatcher(CInfo));
290 // Emit the CheckAndImm/CheckOrImm node.
291 if (N->getOperator()->getName() == "and")
292 AddMatcher(new CheckAndImmMatcher(II->getValue()));
294 AddMatcher(new CheckOrImmMatcher(II->getValue()));
296 // Match the LHS of the AND as appropriate.
297 AddMatcher(new MoveChildMatcher(0));
298 EmitMatchCode(N->getChild(0), NodeNoTypes->getChild(0));
299 AddMatcher(new MoveParentMatcher());
305 // Check that the current opcode lines up.
306 AddMatcher(new CheckOpcodeMatcher(CInfo));
308 // If this node has memory references (i.e. is a load or store), tell the
309 // interpreter to capture them in the memref array.
310 if (N->NodeHasProperty(SDNPMemOperand, CGP))
311 AddMatcher(new RecordMemRefMatcher());
313 // If this node has a chain, then the chain is operand #0 is the SDNode, and
314 // the child numbers of the node are all offset by one.
316 if (N->NodeHasProperty(SDNPHasChain, CGP)) {
317 // Record the node and remember it in our chained nodes list.
318 AddMatcher(new RecordMatcher("'" + N->getOperator()->getName() +
320 NextRecordedOperandNo));
321 // Remember all of the input chains our pattern will match.
322 MatchedChainNodes.push_back(NextRecordedOperandNo++);
324 // Don't look at the input chain when matching the tree pattern to the
328 // If this node is not the root and the subtree underneath it produces a
329 // chain, then the result of matching the node is also produce a chain.
330 // Beyond that, this means that we're also folding (at least) the root node
331 // into the node that produce the chain (for example, matching
332 // "(add reg, (load ptr))" as a add_with_memory on X86). This is
333 // problematic, if the 'reg' node also uses the load (say, its chain).
338 // | \ DAG's like cheese.
344 // It would be invalid to fold XX and LD. In this case, folding the two
345 // nodes together would induce a cycle in the DAG, making it a 'cyclic DAG'
346 // To prevent this, we emit a dynamic check for legality before allowing
347 // this to be folded.
349 const TreePatternNode *Root = Pattern.getSrcPattern();
350 if (N != Root) { // Not the root of the pattern.
351 // If there is a node between the root and this node, then we definitely
352 // need to emit the check.
353 bool NeedCheck = !Root->hasChild(N);
355 // If it *is* an immediate child of the root, we can still need a check if
356 // the root SDNode has multiple inputs. For us, this means that it is an
357 // intrinsic, has multiple operands, or has other inputs like chain or
360 const SDNodeInfo &PInfo = CGP.getSDNodeInfo(Root->getOperator());
362 Root->getOperator() == CGP.get_intrinsic_void_sdnode() ||
363 Root->getOperator() == CGP.get_intrinsic_w_chain_sdnode() ||
364 Root->getOperator() == CGP.get_intrinsic_wo_chain_sdnode() ||
365 PInfo.getNumOperands() > 1 ||
366 PInfo.hasProperty(SDNPHasChain) ||
367 PInfo.hasProperty(SDNPInGlue) ||
368 PInfo.hasProperty(SDNPOptInGlue);
372 AddMatcher(new CheckFoldableChainNodeMatcher());
376 // If this node has an output glue and isn't the root, remember it.
377 if (N->NodeHasProperty(SDNPOutGlue, CGP) &&
378 N != Pattern.getSrcPattern()) {
379 // TODO: This redundantly records nodes with both glues and chains.
381 // Record the node and remember it in our chained nodes list.
382 AddMatcher(new RecordMatcher("'" + N->getOperator()->getName() +
383 "' glue output node",
384 NextRecordedOperandNo));
385 // Remember all of the nodes with output glue our pattern will match.
386 MatchedGlueResultNodes.push_back(NextRecordedOperandNo++);
389 // If this node is known to have an input glue or if it *might* have an input
390 // glue, capture it as the glue input of the pattern.
391 if (N->NodeHasProperty(SDNPOptInGlue, CGP) ||
392 N->NodeHasProperty(SDNPInGlue, CGP))
393 AddMatcher(new CaptureGlueInputMatcher());
395 for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i, ++OpNo) {
396 // Get the code suitable for matching this child. Move to the child, check
397 // it then move back to the parent.
398 AddMatcher(new MoveChildMatcher(OpNo));
399 EmitMatchCode(N->getChild(i), NodeNoTypes->getChild(i));
400 AddMatcher(new MoveParentMatcher());
405 void MatcherGen::EmitMatchCode(const TreePatternNode *N,
406 TreePatternNode *NodeNoTypes) {
407 // If N and NodeNoTypes don't agree on a type, then this is a case where we
408 // need to do a type check. Emit the check, apply the tyep to NodeNoTypes and
409 // reinfer any correlated types.
410 SmallVector<unsigned, 2> ResultsToTypeCheck;
412 for (unsigned i = 0, e = NodeNoTypes->getNumTypes(); i != e; ++i) {
413 if (NodeNoTypes->getExtType(i) == N->getExtType(i)) continue;
414 NodeNoTypes->setType(i, N->getExtType(i));
415 InferPossibleTypes();
416 ResultsToTypeCheck.push_back(i);
419 // If this node has a name associated with it, capture it in VariableMap. If
420 // we already saw this in the pattern, emit code to verify dagness.
421 if (!N->getName().empty()) {
422 unsigned &VarMapEntry = VariableMap[N->getName()];
423 if (VarMapEntry == 0) {
424 // If it is a named node, we must emit a 'Record' opcode.
425 AddMatcher(new RecordMatcher("$" + N->getName(), NextRecordedOperandNo));
426 VarMapEntry = ++NextRecordedOperandNo;
428 // If we get here, this is a second reference to a specific name. Since
429 // we already have checked that the first reference is valid, we don't
430 // have to recursively match it, just check that it's the same as the
431 // previously named thing.
432 AddMatcher(new CheckSameMatcher(VarMapEntry-1));
438 EmitLeafMatchCode(N);
440 EmitOperatorMatchCode(N, NodeNoTypes);
442 // If there are node predicates for this node, generate their checks.
443 for (unsigned i = 0, e = N->getPredicateFns().size(); i != e; ++i)
444 AddMatcher(new CheckPredicateMatcher(N->getPredicateFns()[i]));
446 for (unsigned i = 0, e = ResultsToTypeCheck.size(); i != e; ++i)
447 AddMatcher(new CheckTypeMatcher(N->getType(ResultsToTypeCheck[i]),
448 ResultsToTypeCheck[i]));
451 /// EmitMatcherCode - Generate the code that matches the predicate of this
452 /// pattern for the specified Variant. If the variant is invalid this returns
453 /// true and does not generate code, if it is valid, it returns false.
454 bool MatcherGen::EmitMatcherCode(unsigned Variant) {
455 // If the root of the pattern is a ComplexPattern and if it is specified to
456 // match some number of root opcodes, these are considered to be our variants.
457 // Depending on which variant we're generating code for, emit the root opcode
459 if (const ComplexPattern *CP =
460 Pattern.getSrcPattern()->getComplexPatternInfo(CGP)) {
461 const std::vector<Record*> &OpNodes = CP->getRootNodes();
462 assert(!OpNodes.empty() &&"Complex Pattern must specify what it can match");
463 if (Variant >= OpNodes.size()) return true;
465 AddMatcher(new CheckOpcodeMatcher(CGP.getSDNodeInfo(OpNodes[Variant])));
467 if (Variant != 0) return true;
470 // Emit the matcher for the pattern structure and types.
471 EmitMatchCode(Pattern.getSrcPattern(), PatWithNoTypes);
473 // If the pattern has a predicate on it (e.g. only enabled when a subtarget
474 // feature is around, do the check).
475 if (!Pattern.getPredicateCheck().empty())
476 AddMatcher(new CheckPatternPredicateMatcher(Pattern.getPredicateCheck()));
478 // Now that we've completed the structural type match, emit any ComplexPattern
479 // checks (e.g. addrmode matches). We emit this after the structural match
480 // because they are generally more expensive to evaluate and more difficult to
482 for (unsigned i = 0, e = MatchedComplexPatterns.size(); i != e; ++i) {
483 const TreePatternNode *N = MatchedComplexPatterns[i].first;
485 // Remember where the results of this match get stuck.
486 MatchedComplexPatterns[i].second = NextRecordedOperandNo;
488 // Get the slot we recorded the value in from the name on the node.
489 unsigned RecNodeEntry = VariableMap[N->getName()];
490 assert(!N->getName().empty() && RecNodeEntry &&
491 "Complex pattern should have a name and slot");
492 --RecNodeEntry; // Entries in VariableMap are biased.
494 const ComplexPattern &CP =
495 CGP.getComplexPattern(((DefInit*)N->getLeafValue())->getDef());
497 // Emit a CheckComplexPat operation, which does the match (aborting if it
498 // fails) and pushes the matched operands onto the recorded nodes list.
499 AddMatcher(new CheckComplexPatMatcher(CP, RecNodeEntry,
500 N->getName(), NextRecordedOperandNo));
502 // Record the right number of operands.
503 NextRecordedOperandNo += CP.getNumOperands();
504 if (CP.hasProperty(SDNPHasChain)) {
505 // If the complex pattern has a chain, then we need to keep track of the
506 // fact that we just recorded a chain input. The chain input will be
507 // matched as the last operand of the predicate if it was successful.
508 ++NextRecordedOperandNo; // Chained node operand.
510 // It is the last operand recorded.
511 assert(NextRecordedOperandNo > 1 &&
512 "Should have recorded input/result chains at least!");
513 MatchedChainNodes.push_back(NextRecordedOperandNo-1);
516 // TODO: Complex patterns can't have output glues, if they did, we'd want
524 //===----------------------------------------------------------------------===//
525 // Node Result Generation
526 //===----------------------------------------------------------------------===//
528 void MatcherGen::EmitResultOfNamedOperand(const TreePatternNode *N,
529 SmallVectorImpl<unsigned> &ResultOps){
530 assert(!N->getName().empty() && "Operand not named!");
532 // A reference to a complex pattern gets all of the results of the complex
534 if (const ComplexPattern *CP = N->getComplexPatternInfo(CGP)) {
536 for (unsigned i = 0, e = MatchedComplexPatterns.size(); i != e; ++i)
537 if (MatchedComplexPatterns[i].first->getName() == N->getName()) {
538 SlotNo = MatchedComplexPatterns[i].second;
541 assert(SlotNo != 0 && "Didn't get a slot number assigned?");
543 // The first slot entry is the node itself, the subsequent entries are the
545 for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i)
546 ResultOps.push_back(SlotNo+i);
550 unsigned SlotNo = getNamedArgumentSlot(N->getName());
552 // If this is an 'imm' or 'fpimm' node, make sure to convert it to the target
553 // version of the immediate so that it doesn't get selected due to some other
556 StringRef OperatorName = N->getOperator()->getName();
557 if (OperatorName == "imm" || OperatorName == "fpimm") {
558 AddMatcher(new EmitConvertToTargetMatcher(SlotNo));
559 ResultOps.push_back(NextRecordedOperandNo++);
564 ResultOps.push_back(SlotNo);
567 void MatcherGen::EmitResultLeafAsOperand(const TreePatternNode *N,
568 SmallVectorImpl<unsigned> &ResultOps) {
569 assert(N->isLeaf() && "Must be a leaf");
571 if (IntInit *II = dyn_cast<IntInit>(N->getLeafValue())) {
572 AddMatcher(new EmitIntegerMatcher(II->getValue(), N->getType(0)));
573 ResultOps.push_back(NextRecordedOperandNo++);
577 // If this is an explicit register reference, handle it.
578 if (DefInit *DI = dyn_cast<DefInit>(N->getLeafValue())) {
579 Record *Def = DI->getDef();
580 if (Def->isSubClassOf("Register")) {
581 const CodeGenRegister *Reg =
582 CGP.getTargetInfo().getRegBank().getReg(Def);
583 AddMatcher(new EmitRegisterMatcher(Reg, N->getType(0)));
584 ResultOps.push_back(NextRecordedOperandNo++);
588 if (Def->getName() == "zero_reg") {
589 AddMatcher(new EmitRegisterMatcher(0, N->getType(0)));
590 ResultOps.push_back(NextRecordedOperandNo++);
594 // Handle a reference to a register class. This is used
595 // in COPY_TO_SUBREG instructions.
596 if (Def->isSubClassOf("RegisterOperand"))
597 Def = Def->getValueAsDef("RegClass");
598 if (Def->isSubClassOf("RegisterClass")) {
599 std::string Value = getQualifiedName(Def) + "RegClassID";
600 AddMatcher(new EmitStringIntegerMatcher(Value, MVT::i32));
601 ResultOps.push_back(NextRecordedOperandNo++);
605 // Handle a subregister index. This is used for INSERT_SUBREG etc.
606 if (Def->isSubClassOf("SubRegIndex")) {
607 std::string Value = getQualifiedName(Def);
608 AddMatcher(new EmitStringIntegerMatcher(Value, MVT::i32));
609 ResultOps.push_back(NextRecordedOperandNo++);
614 errs() << "unhandled leaf node: \n";
618 /// GetInstPatternNode - Get the pattern for an instruction.
620 const TreePatternNode *MatcherGen::
621 GetInstPatternNode(const DAGInstruction &Inst, const TreePatternNode *N) {
622 const TreePattern *InstPat = Inst.getPattern();
624 // FIXME2?: Assume actual pattern comes before "implicit".
625 TreePatternNode *InstPatNode;
627 InstPatNode = InstPat->getTree(0);
628 else if (/*isRoot*/ N == Pattern.getDstPattern())
629 InstPatNode = Pattern.getSrcPattern();
633 if (InstPatNode && !InstPatNode->isLeaf() &&
634 InstPatNode->getOperator()->getName() == "set")
635 InstPatNode = InstPatNode->getChild(InstPatNode->getNumChildren()-1);
641 mayInstNodeLoadOrStore(const TreePatternNode *N,
642 const CodeGenDAGPatterns &CGP) {
643 Record *Op = N->getOperator();
644 const CodeGenTarget &CGT = CGP.getTargetInfo();
645 CodeGenInstruction &II = CGT.getInstruction(Op);
646 return II.mayLoad || II.mayStore;
650 numNodesThatMayLoadOrStore(const TreePatternNode *N,
651 const CodeGenDAGPatterns &CGP) {
655 Record *OpRec = N->getOperator();
656 if (!OpRec->isSubClassOf("Instruction"))
660 if (mayInstNodeLoadOrStore(N, CGP))
663 for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i)
664 Count += numNodesThatMayLoadOrStore(N->getChild(i), CGP);
670 EmitResultInstructionAsOperand(const TreePatternNode *N,
671 SmallVectorImpl<unsigned> &OutputOps) {
672 Record *Op = N->getOperator();
673 const CodeGenTarget &CGT = CGP.getTargetInfo();
674 CodeGenInstruction &II = CGT.getInstruction(Op);
675 const DAGInstruction &Inst = CGP.getInstruction(Op);
677 // If we can, get the pattern for the instruction we're generating. We derive
678 // a variety of information from this pattern, such as whether it has a chain.
680 // FIXME2: This is extremely dubious for several reasons, not the least of
681 // which it gives special status to instructions with patterns that Pat<>
682 // nodes can't duplicate.
683 const TreePatternNode *InstPatNode = GetInstPatternNode(Inst, N);
685 // NodeHasChain - Whether the instruction node we're creating takes chains.
686 bool NodeHasChain = InstPatNode &&
687 InstPatNode->TreeHasProperty(SDNPHasChain, CGP);
689 // Instructions which load and store from memory should have a chain,
690 // regardless of whether they happen to have an internal pattern saying so.
691 if (Pattern.getSrcPattern()->TreeHasProperty(SDNPHasChain, CGP)
692 && (II.hasCtrlDep || II.mayLoad || II.mayStore || II.canFoldAsLoad ||
696 bool isRoot = N == Pattern.getDstPattern();
698 // TreeHasOutGlue - True if this tree has glue.
699 bool TreeHasInGlue = false, TreeHasOutGlue = false;
701 const TreePatternNode *SrcPat = Pattern.getSrcPattern();
702 TreeHasInGlue = SrcPat->TreeHasProperty(SDNPOptInGlue, CGP) ||
703 SrcPat->TreeHasProperty(SDNPInGlue, CGP);
705 // FIXME2: this is checking the entire pattern, not just the node in
706 // question, doing this just for the root seems like a total hack.
707 TreeHasOutGlue = SrcPat->TreeHasProperty(SDNPOutGlue, CGP);
710 // NumResults - This is the number of results produced by the instruction in
712 unsigned NumResults = Inst.getNumResults();
714 // Loop over all of the operands of the instruction pattern, emitting code
715 // to fill them all in. The node 'N' usually has number children equal to
716 // the number of input operands of the instruction. However, in cases
717 // where there are predicate operands for an instruction, we need to fill
718 // in the 'execute always' values. Match up the node operands to the
719 // instruction operands to do this.
720 SmallVector<unsigned, 8> InstOps;
721 for (unsigned ChildNo = 0, InstOpNo = NumResults, e = II.Operands.size();
722 InstOpNo != e; ++InstOpNo) {
724 // Determine what to emit for this operand.
725 Record *OperandNode = II.Operands[InstOpNo].Rec;
726 if (OperandNode->isSubClassOf("OperandWithDefaultOps") &&
727 !CGP.getDefaultOperand(OperandNode).DefaultOps.empty()) {
728 // This is a predicate or optional def operand; emit the
729 // 'default ops' operands.
730 const DAGDefaultOperand &DefaultOp
731 = CGP.getDefaultOperand(OperandNode);
732 for (unsigned i = 0, e = DefaultOp.DefaultOps.size(); i != e; ++i)
733 EmitResultOperand(DefaultOp.DefaultOps[i], InstOps);
737 const TreePatternNode *Child = N->getChild(ChildNo);
739 // Otherwise this is a normal operand or a predicate operand without
740 // 'execute always'; emit it.
741 unsigned BeforeAddingNumOps = InstOps.size();
742 EmitResultOperand(Child, InstOps);
743 assert(InstOps.size() > BeforeAddingNumOps && "Didn't add any operands");
745 // If the operand is an instruction and it produced multiple results, just
746 // take the first one.
747 if (!Child->isLeaf() && Child->getOperator()->isSubClassOf("Instruction"))
748 InstOps.resize(BeforeAddingNumOps+1);
753 // If this node has input glue or explicitly specified input physregs, we
754 // need to add chained and glued copyfromreg nodes and materialize the glue
756 if (isRoot && !PhysRegInputs.empty()) {
757 // Emit all of the CopyToReg nodes for the input physical registers. These
758 // occur in patterns like (mul:i8 AL:i8, GR8:i8:$src).
759 for (unsigned i = 0, e = PhysRegInputs.size(); i != e; ++i)
760 AddMatcher(new EmitCopyToRegMatcher(PhysRegInputs[i].second,
761 PhysRegInputs[i].first));
762 // Even if the node has no other glue inputs, the resultant node must be
763 // glued to the CopyFromReg nodes we just generated.
764 TreeHasInGlue = true;
767 // Result order: node results, chain, glue
769 // Determine the result types.
770 SmallVector<MVT::SimpleValueType, 4> ResultVTs;
771 for (unsigned i = 0, e = N->getNumTypes(); i != e; ++i)
772 ResultVTs.push_back(N->getType(i));
774 // If this is the root instruction of a pattern that has physical registers in
775 // its result pattern, add output VTs for them. For example, X86 has:
776 // (set AL, (mul ...))
777 // This also handles implicit results like:
779 if (isRoot && !Pattern.getDstRegs().empty()) {
780 // If the root came from an implicit def in the instruction handling stuff,
782 Record *HandledReg = 0;
783 if (II.HasOneImplicitDefWithKnownVT(CGT) != MVT::Other)
784 HandledReg = II.ImplicitDefs[0];
786 for (unsigned i = 0; i != Pattern.getDstRegs().size(); ++i) {
787 Record *Reg = Pattern.getDstRegs()[i];
788 if (!Reg->isSubClassOf("Register") || Reg == HandledReg) continue;
789 ResultVTs.push_back(getRegisterValueType(Reg, CGT));
793 // If this is the root of the pattern and the pattern we're matching includes
794 // a node that is variadic, mark the generated node as variadic so that it
795 // gets the excess operands from the input DAG.
796 int NumFixedArityOperands = -1;
798 (Pattern.getSrcPattern()->NodeHasProperty(SDNPVariadic, CGP)))
799 NumFixedArityOperands = Pattern.getSrcPattern()->getNumChildren();
801 // If this is the root node and multiple matched nodes in the input pattern
802 // have MemRefs in them, have the interpreter collect them and plop them onto
803 // this node. If there is just one node with MemRefs, leave them on that node
804 // even if it is not the root.
806 // FIXME3: This is actively incorrect for result patterns with multiple
807 // memory-referencing instructions.
808 bool PatternHasMemOperands =
809 Pattern.getSrcPattern()->TreeHasProperty(SDNPMemOperand, CGP);
811 bool NodeHasMemRefs = false;
812 if (PatternHasMemOperands) {
813 unsigned NumNodesThatLoadOrStore =
814 numNodesThatMayLoadOrStore(Pattern.getDstPattern(), CGP);
815 bool NodeIsUniqueLoadOrStore = mayInstNodeLoadOrStore(N, CGP) &&
816 NumNodesThatLoadOrStore == 1;
818 NodeIsUniqueLoadOrStore || (isRoot && (mayInstNodeLoadOrStore(N, CGP) ||
819 NumNodesThatLoadOrStore != 1));
822 assert((!ResultVTs.empty() || TreeHasOutGlue || NodeHasChain) &&
823 "Node has no result");
825 AddMatcher(new EmitNodeMatcher(II.Namespace+"::"+II.TheDef->getName(),
826 ResultVTs.data(), ResultVTs.size(),
827 InstOps.data(), InstOps.size(),
828 NodeHasChain, TreeHasInGlue, TreeHasOutGlue,
829 NodeHasMemRefs, NumFixedArityOperands,
830 NextRecordedOperandNo));
832 // The non-chain and non-glue results of the newly emitted node get recorded.
833 for (unsigned i = 0, e = ResultVTs.size(); i != e; ++i) {
834 if (ResultVTs[i] == MVT::Other || ResultVTs[i] == MVT::Glue) break;
835 OutputOps.push_back(NextRecordedOperandNo++);
840 EmitResultSDNodeXFormAsOperand(const TreePatternNode *N,
841 SmallVectorImpl<unsigned> &ResultOps) {
842 assert(N->getOperator()->isSubClassOf("SDNodeXForm") && "Not SDNodeXForm?");
845 SmallVector<unsigned, 8> InputOps;
847 // FIXME2: Could easily generalize this to support multiple inputs and outputs
848 // to the SDNodeXForm. For now we just support one input and one output like
849 // the old instruction selector.
850 assert(N->getNumChildren() == 1);
851 EmitResultOperand(N->getChild(0), InputOps);
853 // The input currently must have produced exactly one result.
854 assert(InputOps.size() == 1 && "Unexpected input to SDNodeXForm");
856 AddMatcher(new EmitNodeXFormMatcher(InputOps[0], N->getOperator()));
857 ResultOps.push_back(NextRecordedOperandNo++);
860 void MatcherGen::EmitResultOperand(const TreePatternNode *N,
861 SmallVectorImpl<unsigned> &ResultOps) {
862 // This is something selected from the pattern we matched.
863 if (!N->getName().empty())
864 return EmitResultOfNamedOperand(N, ResultOps);
867 return EmitResultLeafAsOperand(N, ResultOps);
869 Record *OpRec = N->getOperator();
870 if (OpRec->isSubClassOf("Instruction"))
871 return EmitResultInstructionAsOperand(N, ResultOps);
872 if (OpRec->isSubClassOf("SDNodeXForm"))
873 return EmitResultSDNodeXFormAsOperand(N, ResultOps);
874 errs() << "Unknown result node to emit code for: " << *N << '\n';
875 PrintFatalError("Unknown node in result pattern!");
878 void MatcherGen::EmitResultCode() {
879 // Patterns that match nodes with (potentially multiple) chain inputs have to
880 // merge them together into a token factor. This informs the generated code
881 // what all the chained nodes are.
882 if (!MatchedChainNodes.empty())
883 AddMatcher(new EmitMergeInputChainsMatcher
884 (MatchedChainNodes.data(), MatchedChainNodes.size()));
886 // Codegen the root of the result pattern, capturing the resulting values.
887 SmallVector<unsigned, 8> Ops;
888 EmitResultOperand(Pattern.getDstPattern(), Ops);
890 // At this point, we have however many values the result pattern produces.
891 // However, the input pattern might not need all of these. If there are
892 // excess values at the end (such as implicit defs of condition codes etc)
893 // just lop them off. This doesn't need to worry about glue or chains, just
896 unsigned NumSrcResults = Pattern.getSrcPattern()->getNumTypes();
898 // If the pattern also has (implicit) results, count them as well.
899 if (!Pattern.getDstRegs().empty()) {
900 // If the root came from an implicit def in the instruction handling stuff,
902 Record *HandledReg = 0;
903 const TreePatternNode *DstPat = Pattern.getDstPattern();
904 if (!DstPat->isLeaf() &&DstPat->getOperator()->isSubClassOf("Instruction")){
905 const CodeGenTarget &CGT = CGP.getTargetInfo();
906 CodeGenInstruction &II = CGT.getInstruction(DstPat->getOperator());
908 if (II.HasOneImplicitDefWithKnownVT(CGT) != MVT::Other)
909 HandledReg = II.ImplicitDefs[0];
912 for (unsigned i = 0; i != Pattern.getDstRegs().size(); ++i) {
913 Record *Reg = Pattern.getDstRegs()[i];
914 if (!Reg->isSubClassOf("Register") || Reg == HandledReg) continue;
919 assert(Ops.size() >= NumSrcResults && "Didn't provide enough results");
920 Ops.resize(NumSrcResults);
922 // If the matched pattern covers nodes which define a glue result, emit a node
923 // that tells the matcher about them so that it can update their results.
924 if (!MatchedGlueResultNodes.empty())
925 AddMatcher(new MarkGlueResultsMatcher(MatchedGlueResultNodes.data(),
926 MatchedGlueResultNodes.size()));
928 AddMatcher(new CompleteMatchMatcher(Ops.data(), Ops.size(), Pattern));
932 /// ConvertPatternToMatcher - Create the matcher for the specified pattern with
933 /// the specified variant. If the variant number is invalid, this returns null.
934 Matcher *llvm::ConvertPatternToMatcher(const PatternToMatch &Pattern,
936 const CodeGenDAGPatterns &CGP) {
937 MatcherGen Gen(Pattern, CGP);
939 // Generate the code for the matcher.
940 if (Gen.EmitMatcherCode(Variant))
943 // FIXME2: Kill extra MoveParent commands at the end of the matcher sequence.
944 // FIXME2: Split result code out to another table, and make the matcher end
945 // with an "Emit <index>" command. This allows result generation stuff to be
946 // shared and factored?
948 // If the match succeeds, then we generate Pattern.
949 Gen.EmitResultCode();
951 // Unconditional match.
952 return Gen.GetMatcher();