llvm.org GIT mirror llvm / 0ee7cff
Implement a couple of new (important) features. 1. If an operation has to be int or fp and the target only supports one int or fp type, relize that the op has to have that type. 2. If a target has operations on multiple types, do not emit matching code for patterns involving those operators, since we do not emit the code to check for them yet. This prevents PPC from generating FP ops currently. Also move some code around into more logical places. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@23724 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Lattner 14 years ago
2 changed file(s) with 129 addition(s) and 51 deletion(s). Raw diff Collapse all Expand all
7474 TP.error(N->getOperator()->getName() + " node requires exactly " +
7575 itostr(NodeInfo.getNumOperands()) + " operands!");
7676 }
77
78 const CodeGenTarget &CGT = TP.getDAGISelEmitter().getTargetInfo();
7779
7880 TreePatternNode *NodeToApply = getOperandNum(OperandNo, N, NumResults);
7981
8284 case SDTCisVT:
8385 // Operand must be a particular type.
8486 return NodeToApply->UpdateNodeType(x.SDTCisVT_Info.VT, TP);
85 case SDTCisInt:
87 case SDTCisInt: {
8688 if (NodeToApply->hasTypeSet() && !MVT::isInteger(NodeToApply->getType()))
8789 NodeToApply->UpdateNodeType(MVT::i1, TP); // throw an error.
8890
89 // FIXME: can tell from the target if there is only one Int type supported.
91 // If there is only one integer type supported, this must be it.
92 const std::vector &VTs = CGT.getLegalValueTypes();
93 MVT::ValueType VT = MVT::LAST_VALUETYPE;
94 for (unsigned i = 0, e = VTs.size(); i != e; ++i)
95 if (MVT::isInteger(VTs[i])) {
96 if (VT == MVT::LAST_VALUETYPE)
97 VT = VTs[i]; // First integer type we've found.
98 else {
99 VT = MVT::LAST_VALUETYPE;
100 break;
101 }
102 }
103
104 // If we found exactly one supported integer type, apply it.
105 if (VT != MVT::LAST_VALUETYPE)
106 return NodeToApply->UpdateNodeType(VT, TP);
90107 return false;
91 case SDTCisFP:
108 }
109 case SDTCisFP: {
92110 if (NodeToApply->hasTypeSet() &&
93111 !MVT::isFloatingPoint(NodeToApply->getType()))
94112 NodeToApply->UpdateNodeType(MVT::f32, TP); // throw an error.
95 // FIXME: can tell from the target if there is only one FP type supported.
113
114 // If there is only one FP type supported, this must be it.
115 const std::vector &VTs = CGT.getLegalValueTypes();
116 MVT::ValueType VT = MVT::LAST_VALUETYPE;
117 for (unsigned i = 0, e = VTs.size(); i != e; ++i)
118 if (MVT::isFloatingPoint(VTs[i])) {
119 if (VT == MVT::LAST_VALUETYPE)
120 VT = VTs[i]; // First integer type we've found.
121 else {
122 VT = MVT::LAST_VALUETYPE;
123 break;
124 }
125 }
126
127 // If we found exactly one supported FP type, apply it.
128 if (VT != MVT::LAST_VALUETYPE)
129 return NodeToApply->UpdateNodeType(VT, TP);
96130 return false;
131 }
97132 case SDTCisSameAs: {
98133 TreePatternNode *OtherNode =
99134 getOperandNum(x.SDTCisSameAs_Info.OtherOperandNum, N, NumResults);
343378 return FragTree;
344379 }
345380
381 /// getIntrinsicType - Check to see if the specified record has an intrinsic
382 /// type which should be applied to it. This infer the type of register
383 /// references from the register file information, for example.
384 ///
385 static MVT::ValueType getIntrinsicType(Record *R, bool NotRegisters, TreePattern &TP) {
386 // Check to see if this is a register or a register class...
387 if (R->isSubClassOf("RegisterClass")) {
388 if (NotRegisters) return MVT::LAST_VALUETYPE;
389 return getValueType(R->getValueAsDef("RegType"));
390 } else if (R->isSubClassOf("PatFrag")) {
391 // Pattern fragment types will be resolved when they are inlined.
392 return MVT::LAST_VALUETYPE;
393 } else if (R->isSubClassOf("Register")) {
394 assert(0 && "Explicit registers not handled here yet!\n");
395 return MVT::LAST_VALUETYPE;
396 } else if (R->isSubClassOf("ValueType")) {
397 // Using a VTSDNode.
398 return MVT::Other;
399 } else if (R->getName() == "node") {
400 // Placeholder.
401 return MVT::LAST_VALUETYPE;
402 }
403
404 TP.error("Unknown node flavor used in pattern: " + R->getName());
405 return MVT::Other;
406 }
407
346408 /// ApplyTypeConstraints - Apply all of the type constraints relevent to
347409 /// this node and its children in the tree. This returns true if it makes a
348410 /// change, false otherwise. If a type contradiction is found, throw an
349411 /// exception.
350 bool TreePatternNode::ApplyTypeConstraints(TreePattern &TP) {
351 if (isLeaf()) return false;
412 bool TreePatternNode::ApplyTypeConstraints(TreePattern &TP, bool NotRegisters) {
413 if (isLeaf()) {
414 if (DefInit *DI = dynamic_cast(getLeafValue()))
415 // If it's a regclass or something else known, include the type.
416 return UpdateNodeType(getIntrinsicType(DI->getDef(), NotRegisters, TP),
417 TP);
418 return false;
419 }
352420
353421 // special handling for set, which isn't really an SDNode.
354422 if (getOperator()->getName() == "set") {
355423 assert (getNumChildren() == 2 && "Only handle 2 operand set's for now!");
356 bool MadeChange = getChild(0)->ApplyTypeConstraints(TP);
357 MadeChange |= getChild(1)->ApplyTypeConstraints(TP);
424 bool MadeChange = getChild(0)->ApplyTypeConstraints(TP, NotRegisters);
425 MadeChange |= getChild(1)->ApplyTypeConstraints(TP, NotRegisters);
358426
359427 // Types of operands must match.
360428 MadeChange |= getChild(0)->UpdateNodeType(getChild(1)->getType(), TP);
366434
367435 bool MadeChange = NI.ApplyTypeConstraints(this, TP);
368436 for (unsigned i = 0, e = getNumChildren(); i != e; ++i)
369 MadeChange |= getChild(i)->ApplyTypeConstraints(TP);
437 MadeChange |= getChild(i)->ApplyTypeConstraints(TP, NotRegisters);
370438 return MadeChange;
371439 } else if (getOperator()->isSubClassOf("Instruction")) {
372440 const DAGInstruction &Inst =
382450 utostr(getNumChildren()) + " operands!");
383451 for (unsigned i = 0, e = getNumChildren(); i != e; ++i) {
384452 MadeChange |= getChild(i)->UpdateNodeType(Inst.getOperandType(i), TP);
385 MadeChange |= getChild(i)->ApplyTypeConstraints(TP);
453 MadeChange |= getChild(i)->ApplyTypeConstraints(TP, NotRegisters);
386454 }
387455 return MadeChange;
388456 } else {
455523 throw "In " + TheRecord->getName() + ": " + Msg;
456524 }
457525
458 /// getIntrinsicType - Check to see if the specified record has an intrinsic
459 /// type which should be applied to it. This infer the type of register
460 /// references from the register file information, for example.
461 ///
462 MVT::ValueType TreePattern::getIntrinsicType(Record *R) const {
463 // Check to see if this is a register or a register class...
464 if (R->isSubClassOf("RegisterClass"))
465 return getValueType(R->getValueAsDef("RegType"));
466 else if (R->isSubClassOf("PatFrag")) {
467 // Pattern fragment types will be resolved when they are inlined.
468 return MVT::LAST_VALUETYPE;
469 } else if (R->isSubClassOf("Register")) {
470 assert(0 && "Explicit registers not handled here yet!\n");
471 return MVT::LAST_VALUETYPE;
472 } else if (R->isSubClassOf("ValueType")) {
473 // Using a VTSDNode.
474 return MVT::Other;
475 } else if (R->getName() == "node") {
476 // Placeholder.
477 return MVT::LAST_VALUETYPE;
478 }
479
480 error("Unknown node flavor used in pattern: " + R->getName());
481 return MVT::Other;
482 }
483
484526 TreePatternNode *TreePattern::ParseTreePattern(DagInit *Dag) {
485527 Record *Operator = Dag->getNodeType();
486528
488530 // If the operator is a ValueType, then this must be "type cast" of a leaf
489531 // node.
490532 if (Dag->getNumArgs() != 1)
491 error("Type cast only valid for a leaf node!");
533 error("Type cast only takes one operand!");
492534
493535 Init *Arg = Dag->getArg(0);
494536 TreePatternNode *New;
503545 }
504546
505547 New = new TreePatternNode(DI);
506 // If it's a regclass or something else known, set the type.
507 New->setType(getIntrinsicType(DI->getDef()));
508548 } else if (DagInit *DI = dynamic_cast(Arg)) {
509549 New = ParseTreePattern(DI);
510550 } else {
545585 Node->setName(Dag->getArgName(i));
546586 Children.push_back(Node);
547587
548 // If it's a regclass or something else known, set the type.
549 Node->setType(getIntrinsicType(R));
550
551588 // Input argument?
552589 if (R->getName() == "node") {
553590 if (Dag->getArgName(i).empty())
572609 while (MadeChange) {
573610 MadeChange = false;
574611 for (unsigned i = 0, e = Trees.size(); i != e; ++i)
575 MadeChange |= Trees[i]->ApplyTypeConstraints(*this);
612 MadeChange |= Trees[i]->ApplyTypeConstraints(*this, false);
576613 }
577614
578615 bool HasUnresolvedTypes = false;
15161553 }
15171554 }
15181555
1556 /// RemoveAllTypes - A quick recursive walk over a pattern which removes all
1557 /// type information from it.
1558 static void RemoveAllTypes(TreePatternNode *N) {
1559 N->setType(MVT::LAST_VALUETYPE);
1560 if (!N->isLeaf())
1561 for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i)
1562 RemoveAllTypes(N->getChild(i));
1563 }
15191564
15201565 /// EmitCodeForPattern - Given a pattern to match, emit code to the specified
15211566 /// stream to match the pattern, and generate the code for the match if it
15361581 std::map VariableMap;
15371582 EmitMatchForPattern(Pattern.first, "N", VariableMap, PatternNo, OS);
15381583
1539 unsigned TmpNo = 0;
1540 unsigned Res = CodeGenPatternResult(Pattern.second, TmpNo, VariableMap, OS);
1541
1542 // Add the result to the map if it has multiple uses.
1543 OS << " if (!N.Val->hasOneUse()) CodeGenMap[N] = Tmp" << Res << ";\n";
1544 OS << " return Tmp" << Res << ";\n";
1584 // TP - Get *SOME* tree pattern, we don't care which.
1585 TreePattern &TP = *PatternFragments.begin()->second;
1586
1587 // At this point, we know that we structurally match the pattern, but the
1588 // types of the nodes may not match. Figure out the fewest number of type
1589 // comparisons we need to emit. For example, if there is only one integer
1590 // type supported by a target, there should be no type comparisons at all for
1591 // integer patterns!
1592 //
1593 // To figure out the fewest number of type checks needed, clone the pattern,
1594 // remove the types, then perform type inference on the pattern as a whole.
1595 // If there are unresolved types, emit an explicit check for those types,
1596 // apply the type to the tree, then rerun type inference. Iterate until all
1597 // types are resolved.
1598 //
1599 TreePatternNode *Pat = Pattern.first->clone();
1600 RemoveAllTypes(Pat);
1601 bool MadeChange = true;
1602 try {
1603 while (MadeChange)
1604 MadeChange = Pat->ApplyTypeConstraints(TP,true/*Ignore reg constraints*/);
1605 } catch (...) {
1606 assert(0 && "Error: could not find consistent types for something we"
1607 " already decided was ok!");
1608 abort();
1609 }
1610
1611 if (!Pat->ContainsUnresolvedType()) {
1612 unsigned TmpNo = 0;
1613 unsigned Res = CodeGenPatternResult(Pattern.second, TmpNo, VariableMap, OS);
1614
1615 // Add the result to the map if it has multiple uses.
1616 OS << " if (!N.Val->hasOneUse()) CodeGenMap[N] = Tmp" << Res << ";\n";
1617 OS << " return Tmp" << Res << ";\n";
1618 }
1619
1620 delete Pat;
1621
15451622 OS << " }\n P" << PatternNo << "Fail:\n";
15461623 }
15471624
193193 /// this node and its children in the tree. This returns true if it makes a
194194 /// change, false otherwise. If a type contradiction is found, throw an
195195 /// exception.
196 bool ApplyTypeConstraints(TreePattern &TP);
196 bool ApplyTypeConstraints(TreePattern &TP, bool NotRegisters);
197197
198198 /// UpdateNodeType - Set the node type of N to VT if VT contains
199199 /// information. If N already contains a conflicting type, then throw an
289289 void dump() const;
290290
291291 private:
292 MVT::ValueType getIntrinsicType(Record *R) const;
293292 TreePatternNode *ParseTreePattern(DagInit *DI);
294293 };
295294
351350
352351 // run - Output the isel, returning true on failure.
353352 void run(std::ostream &OS);
353
354 const CodeGenTarget &getTargetInfo() const { return Target; }
354355
355356 const SDNodeInfo &getSDNodeInfo(Record *R) const {
356357 assert(SDNodes.count(R) && "Unknown node!");