llvm.org GIT mirror llvm / 85dbe1a
Initial support for multi-result patterns: 1. [(set GR32:$dst, (add GR32:$src1, GR32:$src2)), (modify EFLAGS)] This indicates the source pattern expects the instruction would produce 2 values. The first is the result of the addition. The second is an implicit definition in register EFLAGS. 2. def : Pat<(parallel (addc GR32:$src1, GR32:$src2), (modify EFLAGS)), ()> Similar to #1 except this is used for def : Pat patterns. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@41897 91177308-0d34-0410-b5e6-96231b3b80d8 Evan Cheng 13 years ago
3 changed file(s) with 102 addition(s) and 57 deletion(s). Raw diff Collapse all Expand all
196196 }
197197
198198 def set;
199 def modify;
200 def parallel;
199201 def node;
200202 def srcvalue;
201203
690690 MadeChange |= UpdateNodeType(MVT::isVoid, TP);
691691 }
692692 return MadeChange;
693 } else if (getOperator()->getName() == "modify" ||
694 getOperator()->getName() == "parallel") {
695 bool MadeChange = false;
696 for (unsigned i = 0; i < getNumChildren(); ++i)
697 MadeChange = getChild(i)->ApplyTypeConstraints(TP, NotRegisters);
698 MadeChange |= UpdateNodeType(MVT::isVoid, TP);
699 return MadeChange;
693700 } else if (getOperator() == ISE.get_intrinsic_void_sdnode() ||
694701 getOperator() == ISE.get_intrinsic_w_chain_sdnode() ||
695702 getOperator() == ISE.get_intrinsic_wo_chain_sdnode()) {
967974 !Operator->isSubClassOf("Instruction") &&
968975 !Operator->isSubClassOf("SDNodeXForm") &&
969976 !Operator->isSubClassOf("Intrinsic") &&
970 Operator->getName() != "set")
977 Operator->getName() != "set" &&
978 Operator->getName() != "modify" &&
979 Operator->getName() != "parallel")
971980 error("Unrecognized node '" + Operator->getName() + "'!");
972981
973982 // Check to see if this is something that is illegal in an input pattern.
13751384 if (!isUse && Pat->getTransformFn())
13761385 I->error("Cannot specify a transform function for a non-input value!");
13771386 return;
1387 } else if (Pat->getOperator()->getName() == "modify") {
1388 for (unsigned i = 0, e = Pat->getNumChildren(); i != e; ++i) {
1389 TreePatternNode *Dest = Pat->getChild(i);
1390 if (!Dest->isLeaf())
1391 I->error("modify value should be a register!");
1392
1393 DefInit *Val = dynamic_cast(Dest->getLeafValue());
1394 if (!Val || !Val->getDef()->isSubClassOf("Register"))
1395 I->error("modify value should be a register!");
1396 InstImpResults.push_back(Val->getDef());
1397 }
1398 return;
13781399 } else if (Pat->getOperator()->getName() != "set") {
13791400 // If this is not a set, verify that the children nodes are not void typed,
13801401 // and recurse.
14231444 InstResults[Dest->getName()] = Dest;
14241445 } else if (Val->getDef()->isSubClassOf("Register")) {
14251446 InstImpResults.push_back(Val->getDef());
1426 ;
14271447 } else {
14281448 I->error("set destination should be a register!");
14291449 }
16201640 ResultPattern->setTypes(Res0Node->getExtTypes());
16211641
16221642 // Create and insert the instruction.
1643 // FIXME: InstImpResults and InstImpInputs should not be part of
1644 // DAGInstruction.
16231645 DAGInstruction TheInst(I, Results, Operands, InstImpResults, InstImpInputs);
16241646 Instructions.insert(std::make_pair(I->getRecord(), TheInst));
16251647
16421664 TreePattern *I = TheInst.getPattern();
16431665 if (I == 0) continue; // No pattern.
16441666
1645 if (I->getNumTrees() != 1) {
1646 cerr << "CANNOT HANDLE: " << I->getRecord()->getName() << " yet!";
1647 continue;
1648 }
1667 // FIXME: Assume only the first tree is the pattern. The others are clobber
1668 // nodes.
16491669 TreePatternNode *Pattern = I->getTree(0);
16501670 TreePatternNode *SrcPattern;
16511671 if (Pattern->getOperator()->getName() == "set") {
16631683 TreePatternNode *DstPattern = TheInst.getResultPattern();
16641684 PatternsToMatch.
16651685 push_back(PatternToMatch(Instr->getValueAsListInit("Predicates"),
1666 SrcPattern, DstPattern,
1686 SrcPattern, DstPattern, TheInst.getImpResults(),
16671687 Instr->getValueAsInt("AddedComplexity")));
16681688 }
16691689 }
16731693
16741694 for (unsigned i = 0, e = Patterns.size(); i != e; ++i) {
16751695 DagInit *Tree = Patterns[i]->getValueAsDag("PatternToMatch");
1676 TreePattern *Pattern = new TreePattern(Patterns[i], Tree, true, *this);
1696 DefInit *OpDef = dynamic_cast(Tree->getOperator());
1697 Record *Operator = OpDef->getDef();
1698 TreePattern *Pattern;
1699 if (Operator->getName() != "parallel")
1700 Pattern = new TreePattern(Patterns[i], Tree, true, *this);
1701 else {
1702 std::vector Values;
1703 for (unsigned j = 0, ee = Tree->getNumArgs(); j != ee; ++j)
1704 Values.push_back(Tree->getArg(j));
1705 ListInit *LI = new ListInit(Values);
1706 Pattern = new TreePattern(Patterns[i], LI, true, *this);
1707 }
16771708
16781709 // Inline pattern fragments into it.
16791710 Pattern->InlinePatternFragments();
17061737 // resolve cases where the input type is known to be a pointer type (which
17071738 // is considered resolved), but the result knows it needs to be 32- or
17081739 // 64-bits. Infer the other way for good measure.
1709 IterateInference = Pattern->getOnlyTree()->
1710 UpdateNodeType(Result->getOnlyTree()->getExtTypes(), *Result);
1711 IterateInference |= Result->getOnlyTree()->
1712 UpdateNodeType(Pattern->getOnlyTree()->getExtTypes(), *Result);
1740 IterateInference = Pattern->getTree(0)->
1741 UpdateNodeType(Result->getTree(0)->getExtTypes(), *Result);
1742 IterateInference |= Result->getTree(0)->
1743 UpdateNodeType(Pattern->getTree(0)->getExtTypes(), *Result);
17131744 } while (IterateInference);
17141745
17151746 // Verify that we inferred enough types that we can do something with the
17201751 Result->error("Could not infer all types in pattern result!");
17211752
17221753 // Validate that the input pattern is correct.
1723 {
1724 std::map InstInputs;
1725 std::map InstResults;
1726 std::vector InstImpInputs;
1727 std::vector InstImpResults;
1728 FindPatternInputsAndOutputs(Pattern, Pattern->getOnlyTree(),
1754 std::map InstInputs;
1755 std::map InstResults;
1756 std::vector InstImpInputs;
1757 std::vector InstImpResults;
1758 for (unsigned j = 0, ee = Pattern->getNumTrees(); j != ee; ++j)
1759 FindPatternInputsAndOutputs(Pattern, Pattern->getTree(j),
17291760 InstInputs, InstResults,
17301761 InstImpInputs, InstImpResults);
1731 }
17321762
17331763 // Promote the xform function to be an explicit node if set.
1764 TreePatternNode *DstPattern = Result->getOnlyTree();
17341765 std::vector ResultNodeOperands;
1735 TreePatternNode *DstPattern = Result->getOnlyTree();
17361766 for (unsigned ii = 0, ee = DstPattern->getNumChildren(); ii != ee; ++ii) {
17371767 TreePatternNode *OpNode = DstPattern->getChild(ii);
17381768 if (Record *Xform = OpNode->getTransformFn()) {
17521782 Temp.InferAllTypes();
17531783
17541784 std::string Reason;
1755 if (!Pattern->getOnlyTree()->canPatternMatch(Reason, *this))
1785 if (!Pattern->getTree(0)->canPatternMatch(Reason, *this))
17561786 Pattern->error("Pattern can never match: " + Reason);
17571787
17581788 PatternsToMatch.
17591789 push_back(PatternToMatch(Patterns[i]->getValueAsListInit("Predicates"),
1760 Pattern->getOnlyTree(),
1761 Temp.getOnlyTree(),
1790 Pattern->getTree(0),
1791 Temp.getOnlyTree(), InstImpResults,
17621792 Patterns[i]->getValueAsInt("AddedComplexity")));
17631793 }
17641794 }
20162046 PatternsToMatch.
20172047 push_back(PatternToMatch(PatternsToMatch[i].getPredicates(),
20182048 Variant, PatternsToMatch[i].getDstPattern(),
2049 PatternsToMatch[i].getDstRegs(),
20192050 PatternsToMatch[i].getAddedComplexity()));
20202051 }
20212052
26162647 /// EmitResultCode - Emit the action for a pattern. Now that it has matched
26172648 /// we actually have to build a DAG!
26182649 std::vector
2619 EmitResultCode(TreePatternNode *N, bool RetSelected,
2650 EmitResultCode(TreePatternNode *N, std::vector DstRegs,
2651 bool RetSelected,
26202652 bool InFlagDecled, bool ResNodeDecled,
26212653 bool LikeLeaf = false, bool isRoot = false) {
26222654 // List of arguments of getTargetNode() or SelectNodeTo().
27572789 CodeGenInstruction &II = CGT.getInstruction(Op->getName());
27582790 const DAGInstruction &Inst = ISE.getInstruction(Op);
27592791 TreePattern *InstPat = Inst.getPattern();
2792 // FIXME: Assume actual pattern comes before "modify".
27602793 TreePatternNode *InstPatNode =
2761 isRoot ? (InstPat ? InstPat->getOnlyTree() : Pattern)
2762 : (InstPat ? InstPat->getOnlyTree() : NULL);
2794 isRoot ? (InstPat ? InstPat->getTree(0) : Pattern)
2795 : (InstPat ? InstPat->getTree(0) : NULL);
27632796 if (InstPatNode && InstPatNode->getOperator()->getName() == "set") {
27642797 InstPatNode = InstPatNode->getChild(InstPatNode->getNumChildren()-1);
27652798 }
27662799 bool HasVarOps = isRoot && II.hasVariableNumberOfOperands;
2800 // FIXME: fix how we deal with physical register operands.
27672801 bool HasImpInputs = isRoot && Inst.getNumImpOperands() > 0;
2768 bool HasImpResults = isRoot && Inst.getNumImpResults() > 0;
2802 bool HasImpResults = isRoot && DstRegs.size() > 0;
27692803 bool NodeHasOptInFlag = isRoot &&
27702804 PatternHasProperty(Pattern, SDNPOptInFlag, ISE);
27712805 bool NodeHasInFlag = isRoot &&
27772811 bool InputHasChain = isRoot &&
27782812 NodeHasProperty(Pattern, SDNPHasChain, ISE);
27792813 unsigned NumResults = Inst.getNumResults();
2814 unsigned NumDstRegs = HasImpResults ? DstRegs.size() : 0;
27802815
27812816 if (NodeHasOptInFlag) {
27822817 emitCode("bool HasInFlag = "
27862821 emitCode("SmallVector Ops" + utostr(OpcNo) + ";");
27872822
27882823 // How many results is this pattern expected to produce?
2789 unsigned PatResults = 0;
2824 unsigned NumPatResults = 0;
27902825 for (unsigned i = 0, e = Pattern->getExtTypes().size(); i != e; i++) {
27912826 MVT::ValueType VT = Pattern->getTypeNum(i);
27922827 if (VT != MVT::isVoid && VT != MVT::Flag)
2793 PatResults++;
2828 NumPatResults++;
27942829 }
27952830
27962831 if (OrigChains.size() > 0) {
28312866 if ((!OperandNode->isSubClassOf("PredicateOperand") &&
28322867 !OperandNode->isSubClassOf("OptionalDefOperand")) ||
28332868 ISE.getDefaultOperand(OperandNode).DefaultOps.empty()) {
2834 Ops = EmitResultCode(N->getChild(ChildNo), RetSelected,
2869 Ops = EmitResultCode(N->getChild(ChildNo), DstRegs, RetSelected,
28352870 InFlagDecled, ResNodeDecled);
28362871 AllOps.insert(AllOps.end(), Ops.begin(), Ops.end());
28372872 ++ChildNo;
28412876 const DAGDefaultOperand &DefaultOp =
28422877 ISE.getDefaultOperand(II.OperandList[InstOpNo].Rec);
28432878 for (unsigned i = 0, e = DefaultOp.DefaultOps.size(); i != e; ++i) {
2844 Ops = EmitResultCode(DefaultOp.DefaultOps[i], RetSelected,
2879 Ops = EmitResultCode(DefaultOp.DefaultOps[i], DstRegs, RetSelected,
28452880 InFlagDecled, ResNodeDecled);
28462881 AllOps.insert(AllOps.end(), Ops.begin(), Ops.end());
28472882 NumEAInputs += Ops.size();
28872922 Code2 = NodeName + " = ";
28882923 }
28892924
2890 Code = "CurDAG->getTargetNode(Opc" + utostr(OpcNo);
2925 Code += "CurDAG->getTargetNode(Opc" + utostr(OpcNo);
28912926 unsigned OpsNo = OpcNo;
28922927 emitOpcode(II.Namespace + "::" + II.TheDef->getName());
28932928
28992934 }
29002935 // Add types for implicit results in physical registers, scheduler will
29012936 // care of adding copyfromreg nodes.
2902 if (HasImpResults) {
2903 for (unsigned i = 0, e = Inst.getNumImpResults(); i < e; i++) {
2904 Record *RR = Inst.getImpResult(i);
2905 if (RR->isSubClassOf("Register")) {
2906 MVT::ValueType RVT = getRegisterValueType(RR, CGT);
2907 Code += ", " + getEnumName(RVT);
2908 ++NumResults;
2909 }
2937 for (unsigned i = 0; i < NumDstRegs; i++) {
2938 Record *RR = DstRegs[i];
2939 if (RR->isSubClassOf("Register")) {
2940 MVT::ValueType RVT = getRegisterValueType(RR, CGT);
2941 Code += ", " + getEnumName(RVT);
29102942 }
29112943 }
29122944 if (NodeHasChain)
29602992 Code += ", &Ops" + utostr(OpsNo) + "[0], Ops" + utostr(OpsNo) +
29612993 ".size()";
29622994 } else if (NodeHasInFlag || NodeHasOptInFlag || HasImpInputs)
2963 AllOps.push_back("InFlag");
2995 AllOps.push_back("InFlag");
29642996
29652997 unsigned NumOps = AllOps.size();
29662998 if (NumOps) {
29923024 // Remember which op produces the chain.
29933025 if (!isRoot)
29943026 emitCode(ChainName + " = SDOperand(" + NodeName +
2995 ".Val, " + utostr(PatResults) + ");");
3027 ".Val, " + utostr(NumResults+NumDstRegs) + ");");
29963028 else
29973029 emitCode(ChainName + " = SDOperand(" + NodeName +
2998 ", " + utostr(PatResults) + ");");
3030 ", " + utostr(NumResults+NumDstRegs) + ");");
29993031
30003032 if (!isRoot) {
30013033 NodeOps.push_back("Tmp" + utostr(ResNo));
30063038 if (NodeHasOutFlag) {
30073039 if (!InFlagDecled) {
30083040 emitCode("SDOperand InFlag(ResNode, " +
3009 utostr(NumResults + (unsigned)NodeHasChain) + ");");
3041 utostr(NumResults+NumDstRegs+(unsigned)NodeHasChain) + ");");
30103042 InFlagDecled = true;
30113043 } else
30123044 emitCode("InFlag = SDOperand(ResNode, " +
3013 utostr(NumResults + (unsigned)NodeHasChain) + ");");
3045 utostr(NumResults+NumDstRegs+(unsigned)NodeHasChain) + ");");
30143046 }
30153047
30163048 if (FoldedChains.size() > 0) {
30193051 emitCode("ReplaceUses(SDOperand(" +
30203052 FoldedChains[j].first + ".Val, " +
30213053 utostr(FoldedChains[j].second) + "), SDOperand(ResNode, " +
3022 utostr(NumResults) + "));");
3054 utostr(NumResults+NumDstRegs) + "));");
30233055 NeedReplace = true;
30243056 }
30253057
30263058 if (NodeHasOutFlag) {
30273059 emitCode("ReplaceUses(SDOperand(N.Val, " +
3028 utostr(PatResults + (unsigned)InputHasChain) +"), InFlag);");
3060 utostr(NumPatResults + (unsigned)InputHasChain) +"), InFlag);");
30293061 NeedReplace = true;
30303062 }
30313063
30323064 if (NeedReplace) {
3033 for (unsigned i = 0; i < NumResults; i++)
3065 for (unsigned i = 0; i < NumPatResults; i++)
30343066 emitCode("ReplaceUses(SDOperand(N.Val, " +
30353067 utostr(i) + "), SDOperand(ResNode, " + utostr(i) + "));");
30363068 if (InputHasChain)
30373069 emitCode("ReplaceUses(SDOperand(N.Val, " +
3038 utostr(PatResults) + "), SDOperand(" + ChainName + ".Val, "
3070 utostr(NumPatResults) + "), SDOperand(" + ChainName + ".Val, "
30393071 + ChainName + ".ResNo" + "));");
30403072 } else
30413073 RetSelected = true;
30463078 } else if (InputHasChain && !NodeHasChain) {
30473079 // One of the inner node produces a chain.
30483080 if (NodeHasOutFlag)
3049 emitCode("ReplaceUses(SDOperand(N.Val, " + utostr(PatResults+1) +
3081 emitCode("ReplaceUses(SDOperand(N.Val, " + utostr(NumPatResults+1) +
30503082 "), SDOperand(ResNode, N.ResNo-1));");
3051 for (unsigned i = 0; i < PatResults; ++i)
3083 for (unsigned i = 0; i < NumPatResults; ++i)
30523084 emitCode("ReplaceUses(SDOperand(N.Val, " + utostr(i) +
30533085 "), SDOperand(ResNode, " + utostr(i) + "));");
3054 emitCode("ReplaceUses(SDOperand(N.Val, " + utostr(PatResults) +
3086 emitCode("ReplaceUses(SDOperand(N.Val, " + utostr(NumPatResults) +
30553087 "), " + ChainName + ");");
30563088 RetSelected = false;
30573089 }
31003132 // PatLeaf node - the operand may or may not be a leaf node. But it should
31013133 // behave like one.
31023134 std::vector Ops =
3103 EmitResultCode(N->getChild(0), RetSelected, InFlagDecled,
3135 EmitResultCode(N->getChild(0), DstRegs, RetSelected, InFlagDecled,
31043136 ResNodeDecled, true);
31053137 unsigned ResNo = TmpNo++;
31063138 emitCode("SDOperand Tmp" + utostr(ResNo) + " = Transform_" + Op->getName()
32663298 // otherwise we are done.
32673299 } while (Emitter.InsertOneTypeCheck(Pat, Pattern.getSrcPattern(), "N", true));
32683300
3269 Emitter.EmitResultCode(Pattern.getDstPattern(),
3301 Emitter.EmitResultCode(Pattern.getDstPattern(), Pattern.getDstRegs(),
32703302 false, false, false, false, true);
32713303 delete Pat;
32723304 }
39233955 OS << " setSelected(F.Val->getNodeId());\n";
39243956 OS << " RemoveKilled();\n";
39253957 OS << "}\n";
3926 OS << "inline void ReplaceUses(SDNode *F, SDNode *T) {\n";
3927 OS << " CurDAG->ReplaceAllUsesWith(F, T, &ISelKilled);\n";
3958 OS << "void ReplaceUses(SDNode *F, SDNode *T) DISABLE_INLINE {\n";
3959 OS << " unsigned NumVals = F->getNumValues();\n";
3960 OS << " if (NumVals < T->getNumValues()) {\n";
3961 OS << " for (unsigned i = 0; i < NumVals; ++i)\n";
3962 OS << " CurDAG->ReplaceAllUsesOfValueWith(SDOperand(F, i), "
3963 << "SDOperand(T, i), ISelKilled);\n";
3964 OS << " } else {\n";
3965 OS << " CurDAG->ReplaceAllUsesWith(F, T, &ISelKilled);\n";
3966 OS << " }\n";
39283967 OS << " setSelected(F->getNodeId());\n";
39293968 OS << " RemoveKilled();\n";
39303969 OS << "}\n\n";
370370 unsigned getNumOperands() const { return Operands.size(); }
371371 unsigned getNumImpResults() const { return ImpResults.size(); }
372372 unsigned getNumImpOperands() const { return ImpOperands.size(); }
373 const std::vector& getImpResults() const { return ImpResults; }
373374
374375 void setResultPattern(TreePatternNode *R) { ResultPattern = R; }
375376
401402 struct PatternToMatch {
402403 PatternToMatch(ListInit *preds,
403404 TreePatternNode *src, TreePatternNode *dst,
405 const std::vector &dstregs,
404406 unsigned complexity):
405 Predicates(preds), SrcPattern(src), DstPattern(dst),
407 Predicates(preds), SrcPattern(src), DstPattern(dst), Dstregs(dstregs),
406408 AddedComplexity(complexity) {};
407409
408410 ListInit *Predicates; // Top level predicate conditions to match.
409411 TreePatternNode *SrcPattern; // Source pattern to match.
410412 TreePatternNode *DstPattern; // Resulting pattern.
413 std::vector Dstregs; // Physical register defs being matched.
411414 unsigned AddedComplexity; // Add to matching pattern complexity.
412415
413416 ListInit *getPredicates() const { return Predicates; }
414417 TreePatternNode *getSrcPattern() const { return SrcPattern; }
415418 TreePatternNode *getDstPattern() const { return DstPattern; }
419 const std::vector &getDstRegs() const { return Dstregs; }
416420 unsigned getAddedComplexity() const { return AddedComplexity; }
417421 };
418422