llvm.org GIT mirror llvm / ee8d5c3
TableGen: permit non-leaf ComplexPattern uses This allows the results of a ComplexPattern check to be distributed to separate named Operands, instead of the current system where all results must apply (and match perfectly) with a single Operand. For example, if "some_addrmode" is a ComplexPattern producing two results, you can write: def : Pat<(load (some_addrmode GPR64:$base, imm:$offset)), (INST GPR64:$base, imm:$offset)>; This should allow neater instruction definitions in TableGen that don't put all possible aspects of addressing into a single operand, but are still usable with relatively simple C++ CodeGen idioms. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@209206 91177308-0d34-0410-b5e6-96231b3b80d8 Tim Northover 5 years ago
4 changed file(s) with 176 addition(s) and 65 deletion(s). Raw diff Collapse all Expand all
20212021
20222022 // mem[bhw](Rs+#0) = [clrbit|setbit](#U5)
20232023 let AddedComplexity = 225 in
2024 def : Pat <(stOp (OpNode (ldOp addrPred:$addr), immPred:$bitend),
2025 addrPred:$addr),
2026 (MI IntRegs:$addr, #0, (xformFunc immPred:$bitend))>;
2024 def : Pat <(stOp (OpNode (ldOp (addrPred IntRegs:$addr, extPred:$offset)),
2025 immPred:$bitend),
2026 (addrPred (i32 IntRegs:$addr), extPred:$offset)),
2027 (MI IntRegs:$addr, extPred:$offset, (xformFunc immPred:$bitend))>;
20272028 }
20282029
20292030 multiclass MemOpi_bitExtType {
20672068 PatLeaf extPred, InstHexagon MI, SDNode OpNode> {
20682069 let AddedComplexity = 141 in
20692070 // mem[bhw](Rs+#0) [+-&|]= Rt
2070 def : Pat <(stOp (OpNode (ldOp addrPred:$addr), (i32 IntRegs:$addend)),
2071 addrPred:$addr),
2072 (MI IntRegs:$addr, #0, (i32 IntRegs:$addend) )>;
2071 def : Pat <(stOp (OpNode (ldOp (addrPred IntRegs:$addr, extPred:$offset)),
2072 (i32 IntRegs:$addend)),
2073 (addrPred (i32 IntRegs:$addr), extPred:$offset)),
2074 (MI IntRegs:$addr, extPred:$offset, (i32 IntRegs:$addend) )>;
20732075
20742076 // mem[bhw](Rs+#U6:[012]) [+-&|]= Rt
20752077 let AddedComplexity = 150 in
737737 // specified. To get best possible pattern match we'll need to dynamically
738738 // calculate the complexity of all patterns a dag can potentially map to.
739739 const ComplexPattern *AM = P->getComplexPatternInfo(CGP);
740 if (AM)
740 if (AM) {
741741 Size += AM->getNumOperands() * 3;
742
743 // We don't want to count any children twice, so return early.
744 return Size;
745 }
742746
743747 // If this node has some predicate function that must match, it adds to the
744748 // complexity of this node.
11211125 if (Operator->isSubClassOf("ValueType"))
11221126 return 1; // A type-cast of one result.
11231127
1128 if (Operator->isSubClassOf("ComplexPattern"))
1129 return 1;
1130
11241131 Operator->dump();
11251132 errs() << "Unhandled node in GetNumNodeResults\n";
11261133 exit(1);
14241431 return EEVT::TypeSet(); // Unknown.
14251432 }
14261433
1434 if (R->isSubClassOf("Operand"))
1435 return EEVT::TypeSet(getValueType(R->getValueAsDef("Type")));
1436
14271437 TP.error("Unknown node flavor used in pattern: " + R->getName());
14281438 return EEVT::TypeSet(MVT::Other, TP);
14291439 }
14461456 /// return the ComplexPattern information, otherwise return null.
14471457 const ComplexPattern *
14481458 TreePatternNode::getComplexPatternInfo(const CodeGenDAGPatterns &CGP) const {
1449 if (!isLeaf()) return nullptr;
1450
1451 DefInit *DI = dyn_cast(getLeafValue());
1452 if (DI && DI->getDef()->isSubClassOf("ComplexPattern"))
1453 return &CGP.getComplexPattern(DI->getDef());
1454 return nullptr;
1459 Record *Rec;
1460 if (isLeaf()) {
1461 DefInit *DI = dyn_cast(getLeafValue());
1462 if (!DI)
1463 return nullptr;
1464 Rec = DI->getDef();
1465 } else
1466 Rec = getOperator();
1467
1468 if (!Rec->isSubClassOf("ComplexPattern"))
1469 return nullptr;
1470 return &CGP.getComplexPattern(Rec);
1471 }
1472
1473 unsigned TreePatternNode::getNumMIResults(const CodeGenDAGPatterns &CGP) const {
1474 // A ComplexPattern specifically declares how many results it fills in.
1475 if (const ComplexPattern *CP = getComplexPatternInfo(CGP))
1476 return CP->getNumOperands();
1477
1478 // If MIOperandInfo is specified, that gives the count.
1479 if (isLeaf()) {
1480 DefInit *DI = dyn_cast(getLeafValue());
1481 if (DI && DI->getDef()->isSubClassOf("Operand")) {
1482 DagInit *MIOps = DI->getDef()->getValueAsDag("MIOperandInfo");
1483 if (MIOps->getNumArgs())
1484 return MIOps->getNumArgs();
1485 }
1486 }
1487
1488 // Otherwise there is just one result.
1489 return 1;
14551490 }
14561491
14571492 /// NodeHasProperty - Return true if this node has the specified property.
17241759 return MadeChange;
17251760 }
17261761
1762 if (getOperator()->isSubClassOf("ComplexPattern")) {
1763 bool MadeChange = false;
1764
1765 for (unsigned i = 0; i < getNumChildren(); ++i)
1766 MadeChange |= getChild(i)->ApplyTypeConstraints(TP, NotRegisters);
1767
1768 return MadeChange;
1769 }
1770
17271771 assert(getOperator()->isSubClassOf("SDNodeXForm") && "Unknown node type!");
17281772
17291773 // Node transforms always take one operand.
17791823 // TODO:
17801824 return true;
17811825 }
1826
1827 if (getOperator()->isSubClassOf("ComplexPattern"))
1828 return true;
17821829
17831830 // If this node is a commutative operator, check that the LHS isn't an
17841831 // immediate.
19261973 !Operator->isSubClassOf("Instruction") &&
19271974 !Operator->isSubClassOf("SDNodeXForm") &&
19281975 !Operator->isSubClassOf("Intrinsic") &&
1976 !Operator->isSubClassOf("ComplexPattern") &&
19291977 Operator->getName() != "set" &&
19301978 Operator->getName() != "implicit")
19311979 error("Unrecognized node '" + Operator->getName() + "'!");
19792027
19802028 TreePatternNode *IIDNode = new TreePatternNode(IntInit::get(IID), 1);
19812029 Children.insert(Children.begin(), IIDNode);
2030 }
2031
2032 if (Operator->isSubClassOf("ComplexPattern")) {
2033 for (unsigned i = 0; i < Children.size(); ++i) {
2034 TreePatternNode *Child = Children[i];
2035
2036 if (Child->getName().empty())
2037 error("All arguments to a ComplexPattern must be named");
2038
2039 // Check that the ComplexPattern uses are consistent: "(MY_PAT $a, $b)"
2040 // and "(MY_PAT $b, $a)" should not be allowed in the same pattern;
2041 // neither should "(MY_PAT_1 $a, $b)" and "(MY_PAT_2 $a, $b)".
2042 auto OperandId = std::make_pair(Operator, i);
2043 auto PrevOp = ComplexPatternOperands.find(Child->getName());
2044 if (PrevOp != ComplexPatternOperands.end()) {
2045 if (PrevOp->getValue() != OperandId)
2046 error("All ComplexPattern operands must appear consistently: "
2047 "in the same order in just one ComplexPattern instance.");
2048 } else
2049 ComplexPatternOperands[Child->getName()] = OperandId;
2050 }
19822051 }
19832052
19842053 unsigned NumResults = GetNumNodeResults(Operator, CDP);
25522621 return;
25532622 }
25542623
2555 // Get information about the SDNode for the operator.
2556 const SDNodeInfo &OpInfo = CDP.getSDNodeInfo(N->getOperator());
2557
25582624 // Notice properties of the node.
2559 if (OpInfo.hasProperty(SDNPMayStore)) mayStore = true;
2560 if (OpInfo.hasProperty(SDNPMayLoad)) mayLoad = true;
2561 if (OpInfo.hasProperty(SDNPSideEffect)) hasSideEffects = true;
2562 if (OpInfo.hasProperty(SDNPVariadic)) isVariadic = true;
2625 if (N->NodeHasProperty(SDNPMayStore, CDP)) mayStore = true;
2626 if (N->NodeHasProperty(SDNPMayLoad, CDP)) mayLoad = true;
2627 if (N->NodeHasProperty(SDNPSideEffect, CDP)) hasSideEffects = true;
2628 if (N->NodeHasProperty(SDNPVariadic, CDP)) isVariadic = true;
25632629
25642630 if (const CodeGenIntrinsic *IntInfo = N->getIntrinsicInfo(CDP)) {
25652631 // If this is an intrinsic, analyze it.
34333499 std::vector &OutVariants,
34343500 CodeGenDAGPatterns &CDP,
34353501 const MultipleUseVarSet &DepVars) {
3436 // We cannot permute leaves.
3437 if (N->isLeaf()) {
3502 // We cannot permute leaves or ComplexPattern uses.
3503 if (N->isLeaf() || N->getOperator()->isSubClassOf("ComplexPattern")) {
34383504 OutVariants.push_back(N);
34393505 return;
34403506 }
408408 const ComplexPattern *
409409 getComplexPatternInfo(const CodeGenDAGPatterns &CGP) const;
410410
411 /// Returns the number of MachineInstr operands that would be produced by this
412 /// node if it mapped directly to an output Instruction's
413 /// operand. ComplexPattern specifies this explicitly; MIOperandInfo gives it
414 /// for Operands; otherwise 1.
415 unsigned getNumMIResults(const CodeGenDAGPatterns &CGP) const;
416
411417 /// NodeHasProperty - Return true if this node has the specified property.
412418 bool NodeHasProperty(SDNP Property, const CodeGenDAGPatterns &CGP) const;
413419
526532 /// hasError - True if the currently processed nodes have unresolvable types
527533 /// or other non-fatal errors
528534 bool HasError;
535
536 /// It's important that the usage of operands in ComplexPatterns is
537 /// consistent: each named operand can be defined by at most one
538 /// ComplexPattern. This records the ComplexPattern instance and the operand
539 /// number for each operand encountered in a ComplexPattern to aid in that
540 /// check.
541 StringMap> ComplexPatternOperands;
529542 public:
530543
531544 /// TreePattern constructor - Parse the specified DagInits into the
6161 /// insertion easier.
6262 StringMap VariableMap;
6363
64 /// This maintains the recorded operand number that OPC_CheckComplexPattern
65 /// drops each sub-operand into. We don't want to insert these into
66 /// VariableMap because that leads to identity checking if they are
67 /// encountered multiple times. Biased by 1 like VariableMap for
68 /// consistency.
69 StringMap NamedComplexPatternOperands;
70
6471 /// NextRecordedOperandNo - As we emit opcodes to record matched values in
6572 /// the RecordedNodes array, this keeps track of which slot will be next to
6673 /// record into.
7582 SmallVector MatchedGlueResultNodes;
7683
7784 /// MatchedComplexPatterns - This maintains a list of all of the
78 /// ComplexPatterns that we need to check. The patterns are known to have
79 /// names which were recorded. The second element of each pair is the first
80 /// slot number that the OPC_CheckComplexPat opcode drops the matched
81 /// results into.
85 /// ComplexPatterns that we need to check. The second element of each pair
86 /// is the recorded operand number of the input node.
8287 SmallVector
8388 unsigned>, 2> MatchedComplexPatterns;
8489
113118 void EmitLeafMatchCode(const TreePatternNode *N);
114119 void EmitOperatorMatchCode(const TreePatternNode *N,
115120 TreePatternNode *NodeNoTypes);
121
122 /// If this is the first time a node with unique identifier Name has been
123 /// seen, record it. Otherwise, emit a check to make sure this is the same
124 /// node. Returns true if this is the first encounter.
125 bool recordUniqueNode(std::string Name);
116126
117127 // Result Code Generation.
118128 unsigned getNamedArgumentSlot(StringRef Name) {
265275
266276 // Remember this ComplexPattern so that we can emit it after all the other
267277 // structural matches are done.
268 MatchedComplexPatterns.push_back(std::make_pair(N, 0));
278 unsigned InputOperand = VariableMap[N->getName()] - 1;
279 MatchedComplexPatterns.push_back(std::make_pair(N, InputOperand));
269280 return;
270281 }
271282
276287 void MatcherGen::EmitOperatorMatchCode(const TreePatternNode *N,
277288 TreePatternNode *NodeNoTypes) {
278289 assert(!N->isLeaf() && "Not an operator?");
290
291 if (N->getOperator()->isSubClassOf("ComplexPattern")) {
292 // The "name" of a non-leaf complex pattern (MY_PAT $op1, $op2) is
293 // "MY_PAT:op1:op2". We should already have validated that the uses are
294 // consistent.
295 std::string PatternName = N->getOperator()->getName();
296 for (unsigned i = 0; i < N->getNumChildren(); ++i) {
297 PatternName += ":";
298 PatternName += N->getChild(i)->getName();
299 }
300
301 if (recordUniqueNode(PatternName)) {
302 auto NodeAndOpNum = std::make_pair(N, NextRecordedOperandNo - 1);
303 MatchedComplexPatterns.push_back(NodeAndOpNum);
304 }
305
306 return;
307 }
308
279309 const SDNodeInfo &CInfo = CGP.getSDNodeInfo(N->getOperator());
280310
281311 // If this is an 'and R, 1234' where the operation is AND/OR and the RHS is
414444 }
415445 }
416446
447 bool MatcherGen::recordUniqueNode(std::string Name) {
448 unsigned &VarMapEntry = VariableMap[Name];
449 if (VarMapEntry == 0) {
450 // If it is a named node, we must emit a 'Record' opcode.
451 AddMatcher(new RecordMatcher("$" + Name, NextRecordedOperandNo));
452 VarMapEntry = ++NextRecordedOperandNo;
453 return true;
454 }
455
456 // If we get here, this is a second reference to a specific name. Since
457 // we already have checked that the first reference is valid, we don't
458 // have to recursively match it, just check that it's the same as the
459 // previously named thing.
460 AddMatcher(new CheckSameMatcher(VarMapEntry-1));
461 return false;
462 }
417463
418464 void MatcherGen::EmitMatchCode(const TreePatternNode *N,
419465 TreePatternNode *NodeNoTypes) {
431477
432478 // If this node has a name associated with it, capture it in VariableMap. If
433479 // we already saw this in the pattern, emit code to verify dagness.
434 if (!N->getName().empty()) {
435 unsigned &VarMapEntry = VariableMap[N->getName()];
436 if (VarMapEntry == 0) {
437 // If it is a named node, we must emit a 'Record' opcode.
438 AddMatcher(new RecordMatcher("$" + N->getName(), NextRecordedOperandNo));
439 VarMapEntry = ++NextRecordedOperandNo;
440 } else {
441 // If we get here, this is a second reference to a specific name. Since
442 // we already have checked that the first reference is valid, we don't
443 // have to recursively match it, just check that it's the same as the
444 // previously named thing.
445 AddMatcher(new CheckSameMatcher(VarMapEntry-1));
480 if (!N->getName().empty())
481 if (!recordUniqueNode(N->getName()))
446482 return;
447 }
448 }
449483
450484 if (N->isLeaf())
451485 EmitLeafMatchCode(N);
496530 const TreePatternNode *N = MatchedComplexPatterns[i].first;
497531
498532 // Remember where the results of this match get stuck.
499 MatchedComplexPatterns[i].second = NextRecordedOperandNo;
533 if (N->isLeaf()) {
534 NamedComplexPatternOperands[N->getName()] = NextRecordedOperandNo + 1;
535 } else {
536 unsigned CurOp = NextRecordedOperandNo;
537 for (unsigned i = 0; i < N->getNumChildren(); ++i) {
538 NamedComplexPatternOperands[N->getChild(i)->getName()] = CurOp + 1;
539 CurOp += N->getChild(i)->getNumMIResults(CGP);
540 }
541 }
500542
501543 // Get the slot we recorded the value in from the name on the node.
502 unsigned RecNodeEntry = VariableMap[N->getName()];
503 assert(!N->getName().empty() && RecNodeEntry &&
504 "Complex pattern should have a name and slot");
505 --RecNodeEntry; // Entries in VariableMap are biased.
506
507 const ComplexPattern &CP =
508 CGP.getComplexPattern(((DefInit*)N->getLeafValue())->getDef());
544 unsigned RecNodeEntry = MatchedComplexPatterns[i].second;
545
546 const ComplexPattern &CP = *N->getComplexPatternInfo(CGP);
509547
510548 // Emit a CheckComplexPat operation, which does the match (aborting if it
511549 // fails) and pushes the matched operands onto the recorded nodes list.
542580 SmallVectorImpl &ResultOps){
543581 assert(!N->getName().empty() && "Operand not named!");
544582
545 // A reference to a complex pattern gets all of the results of the complex
546 // pattern's match.
547 if (const ComplexPattern *CP = N->getComplexPatternInfo(CGP)) {
548 unsigned SlotNo = 0;
549 for (unsigned i = 0, e = MatchedComplexPatterns.size(); i != e; ++i)
550 if (MatchedComplexPatterns[i].first->getName() == N->getName()) {
551 SlotNo = MatchedComplexPatterns[i].second;
552 break;
553 }
554 assert(SlotNo != 0 && "Didn't get a slot number assigned?");
555
556 // The first slot entry is the node itself, the subsequent entries are the
557 // matched values.
558 for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i)
559 ResultOps.push_back(SlotNo+i);
583 if (unsigned SlotNo = NamedComplexPatternOperands[N->getName()]) {
584 // Complex operands have already been completely selected, just find the
585 // right slot ant add the arguments directly.
586 for (unsigned i = 0; i < N->getNumMIResults(CGP); ++i)
587 ResultOps.push_back(SlotNo - 1 + i);
588
560589 return;
561590 }
562591
574603 }
575604 }
576605
577 ResultOps.push_back(SlotNo);
606 for (unsigned i = 0; i < N->getNumMIResults(CGP); ++i)
607 ResultOps.push_back(SlotNo + i);
578608 }
579609
580610 void MatcherGen::EmitResultLeafAsOperand(const TreePatternNode *N,