llvm.org GIT mirror llvm / 676d731
- Clean up tablegen dag isel generator code. - Clean up the code generated by tablegen: * AddToISelQueue now takes one argument. * ComplexPattern matching condition can now be shared. * Eliminate passing unnecessary arguments to emit routines. * Eliminate some unneeded SDOperand declarations in select routines. * Other minor clean ups. - This reduces foot print slightly: X86ISelDAGToDAG.o is reduced from 971k to 823k. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@29892 91177308-0d34-0410-b5e6-96231b3b80d8 Evan Cheng 13 years ago
2 changed file(s) with 248 addition(s) and 245 deletion(s). Raw diff Collapse all Expand all
19081908 }
19091909 }
19101910
1911
19121911 // NodeIsComplexPattern - return true if N is a leaf node and a subclass of
19131912 // ComplexPattern.
19141913 static bool NodeIsComplexPattern(TreePatternNode *N)
21192118 std::vector > FoldedChains;
21202119 std::set Duplicates;
21212120
2122 /// GeneratedCode - This is the buffer that we emit code to. The first bool
2121 /// GeneratedCode - This is the buffer that we emit code to. The first int
21232122 /// indicates whether this is an exit predicate (something that should be
2124 /// tested, and if true, the match fails) [when true] or normal code to emit
2125 /// [when false].
2126 std::vector > &GeneratedCode;
2123 /// tested, and if true, the match fails) [when 1], or normal code to emit
2124 /// [when 0], or initialization code to emit [when 2].
2125 std::vector > &GeneratedCode;
21272126 /// GeneratedDecl - This is the set of all SDOperand declarations needed for
21282127 /// the set of patterns for each top-level opcode.
21292128 std::set > &GeneratedDecl;
21392138
21402139 void emitCheck(const std::string &S) {
21412140 if (!S.empty())
2142 GeneratedCode.push_back(std::make_pair(true, S));
2141 GeneratedCode.push_back(std::make_pair(1, S));
21432142 }
21442143 void emitCode(const std::string &S) {
21452144 if (!S.empty())
2146 GeneratedCode.push_back(std::make_pair(false, S));
2145 GeneratedCode.push_back(std::make_pair(0, S));
2146 }
2147 void emitInit(const std::string &S) {
2148 if (!S.empty())
2149 GeneratedCode.push_back(std::make_pair(2, S));
21472150 }
21482151 void emitDecl(const std::string &S, unsigned T=0) {
21492152 assert(!S.empty() && "Invalid declaration");
21602163 public:
21612164 PatternCodeEmitter(DAGISelEmitter &ise, ListInit *preds,
21622165 TreePatternNode *pattern, TreePatternNode *instr,
2163 std::vectorbool, std::string> > &gc,
2166 std::vectorunsigned, std::string> > &gc,
21642167 std::set > &gd,
21652168 std::vector &to,
21662169 std::vector &tv)
21672170 : ISE(ise), Predicates(preds), Pattern(pattern), Instruction(instr),
2168 GeneratedCode(gc), GeneratedDecl(gd), TargetOpcodes(to), TargetVTs(tv),
2171 GeneratedCode(gc), GeneratedDecl(gd),
2172 TargetOpcodes(to), TargetVTs(tv),
21692173 TmpNo(0), OpcNo(0), VTNo(0) {}
21702174
21712175 /// EmitMatchCode - Emit a matcher for N, going to the label for PatternNo
22892293 else
22902294 FoundChain = true;
22912295 ChainName = "Chain" + ChainSuffix;
2292 emitDecl(ChainName);
2293 emitCode(ChainName + " = " + RootName +
2296 emitInit("SDOperand " + ChainName + " = " + RootName +
22942297 ".getOperand(0);");
22952298 }
22962299 }
23112314 }
23122315 }
23132316
2317 const ComplexPattern *CP;
23142318 for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i, ++OpNo) {
2315 emitDecl(RootName + utostr(OpNo));
2316 emitCode(RootName + utostr(OpNo) + " = " +
2319 emitInit("SDOperand " + RootName + utostr(OpNo) + " = " +
23172320 RootName + ".getOperand(" +utostr(OpNo) + ");");
2318 TreePatternNode *Child = N->getChild(i);
2319
2321
2322 TreePatternNode *Child = N->getChild(i);
23202323 if (!Child->isLeaf()) {
23212324 // If it's not a leaf, recursively match.
23222325 const SDNodeInfo &CInfo = ISE.getSDNodeInfo(Child->getOperator());
23532356 } else if (LeafRec->isSubClassOf("Register")) {
23542357 // Handle register references.
23552358 } else if (LeafRec->isSubClassOf("ComplexPattern")) {
2356 // Handle complex pattern. Nothing to do here.
2359 // Handle complex pattern.
2360 CP = NodeGetComplexPattern(Child, ISE);
2361 std::string Fn = CP->getSelectFunc();
2362 unsigned NumOps = CP->getNumOperands();
2363 for (unsigned i = 0; i < NumOps; ++i) {
2364 emitDecl("CPTmp" + utostr(i));
2365 emitCode("SDOperand CPTmp" + utostr(i) + ";");
2366 }
2367
2368 std::string Code = Fn + "(" + RootName + utostr(OpNo);
2369 for (unsigned i = 0; i < NumOps; i++)
2370 Code += ", CPTmp" + utostr(i);
2371 emitCheck(Code + ")");
23572372 } else if (LeafRec->getName() == "srcvalue") {
23582373 // Place holder for SRCVALUE nodes. Nothing to do here.
23592374 } else if (LeafRec->isSubClassOf("ValueType")) {
23882403 }
23892404 }
23902405
2406 // Handle cases when root is a complex pattern.
2407 if (isRoot && N->isLeaf() && (CP = NodeGetComplexPattern(N, ISE))) {
2408 std::string Fn = CP->getSelectFunc();
2409 unsigned NumOps = CP->getNumOperands();
2410 for (unsigned i = 0; i < NumOps; ++i) {
2411 emitDecl("CPTmp" + utostr(i));
2412 emitCode("SDOperand CPTmp" + utostr(i) + ";");
2413 }
2414
2415 std::string Code = Fn + "(" + RootName;
2416 for (unsigned i = 0; i < NumOps; i++)
2417 Code += ", CPTmp" + utostr(i);
2418 emitCheck(Code + ")");
2419 }
2420
23912421 // If there is a node predicate for this, emit the call.
23922422 if (!N->getPredicateFn().empty())
23932423 emitCheck(N->getPredicateFn() + "(" + RootName + ".Val)");
23952425
23962426 /// EmitResultCode - Emit the action for a pattern. Now that it has matched
23972427 /// we actually have to build a DAG!
2398 std::pair
2399 EmitResultCode(TreePatternNode *N, bool &RetSelected, bool LikeLeaf = false,
2400 bool isRoot = false) {
2428 std::vector
2429 EmitResultCode(TreePatternNode *N, bool RetSelected,
2430 bool InFlagDecled, bool ResNodeDecled,
2431 bool LikeLeaf = false, bool isRoot = false) {
2432 // List of arguments of getTargetNode() or SelectNodeTo().
2433 std::vector NodeOps;
24012434 // This is something selected from the pattern we matched.
24022435 if (!N->getName().empty()) {
24032436 std::string &Val = VariableMap[N->getName()];
24052438 "Variable referenced but not defined and not caught earlier!");
24062439 if (Val[0] == 'T' && Val[1] == 'm' && Val[2] == 'p') {
24072440 // Already selected this operand, just return the tmpval.
2408 return std::make_pair(1, atoi(Val.c_str()+3));
2441 NodeOps.push_back(Val);
2442 return NodeOps;
24092443 }
24102444
24112445 const ComplexPattern *CP;
24122446 unsigned ResNo = TmpNo++;
2413 unsigned NumRes = 1;
24142447 if (!N->isLeaf() && N->getOperator()->getName() == "imm") {
24152448 assert(N->getExtTypes().size() == 1 && "Multiple types not handled!");
24162449 std::string CastType;
24222455 case MVT::i32: CastType = "unsigned"; break;
24232456 case MVT::i64: CastType = "uint64_t"; break;
24242457 }
2425 emitDecl("Tmp" + utostr(ResNo));
2426 emitCode("Tmp" + utostr(ResNo) +
2458 emitCode("SDOperand Tmp" + utostr(ResNo) +
24272459 " = CurDAG->getTargetConstant(((" + CastType +
24282460 ") cast(" + Val + ")->getValue()), " +
24292461 getEnumName(N->getTypeNum(0)) + ");");
2462 NodeOps.push_back("Tmp" + utostr(ResNo));
2463 // Add Tmp to VariableMap, so that we don't multiply select this
2464 // value if used multiple times by this pattern result.
2465 Val = "Tmp"+utostr(ResNo);
24302466 } else if (!N->isLeaf() && N->getOperator()->getName() == "texternalsym"){
24312467 Record *Op = OperatorMap[N->getName()];
24322468 // Transform ExternalSymbol to TargetExternalSymbol
24332469 if (Op && Op->getName() == "externalsym") {
2434 emitDecl("Tmp" + utostr(ResNo));
2435 emitCode("Tmp" + utostr(ResNo) + " = CurDAG->getTarget"
2470 emitCode("SDOperand Tmp" + utostr(ResNo) + " = CurDAG->getTarget"
24362471 "ExternalSymbol(cast(" +
24372472 Val + ")->getSymbol(), " +
24382473 getEnumName(N->getTypeNum(0)) + ");");
2474 NodeOps.push_back("Tmp" + utostr(ResNo));
2475 // Add Tmp to VariableMap, so that we don't multiply select this
2476 // value if used multiple times by this pattern result.
2477 Val = "Tmp"+utostr(ResNo);
24392478 } else {
2440 emitDecl("Tmp" + utostr(ResNo));
2441 emitCode("Tmp" + utostr(ResNo) + " = " + Val + ";");
2479 NodeOps.push_back(Val);
24422480 }
24432481 } else if (!N->isLeaf() && N->getOperator()->getName() == "tglobaladdr") {
24442482 Record *Op = OperatorMap[N->getName()];
24452483 // Transform GlobalAddress to TargetGlobalAddress
24462484 if (Op && Op->getName() == "globaladdr") {
2447 emitDecl("Tmp" + utostr(ResNo));
2448 emitCode("Tmp" + utostr(ResNo) + " = CurDAG->getTarget"
2485 emitCode("SDOperand Tmp" + utostr(ResNo) + " = CurDAG->getTarget"
24492486 "GlobalAddress(cast(" + Val +
24502487 ")->getGlobal(), " + getEnumName(N->getTypeNum(0)) +
24512488 ");");
2489 NodeOps.push_back("Tmp" + utostr(ResNo));
2490 // Add Tmp to VariableMap, so that we don't multiply select this
2491 // value if used multiple times by this pattern result.
2492 Val = "Tmp"+utostr(ResNo);
24522493 } else {
2453 emitDecl("Tmp" + utostr(ResNo));
2454 emitCode("Tmp" + utostr(ResNo) + " = " + Val + ";");
2494 NodeOps.push_back(Val);
24552495 }
24562496 } else if (!N->isLeaf() && N->getOperator()->getName() == "texternalsym"){
2457 emitDecl("Tmp" + utostr(ResNo));
2458 emitCode("Tmp" + utostr(ResNo) + " = " + Val + ";");
2497 NodeOps.push_back(Val);
2498 // Add Tmp to VariableMap, so that we don't multiply select this
2499 // value if used multiple times by this pattern result.
2500 Val = "Tmp"+utostr(ResNo);
24592501 } else if (!N->isLeaf() && N->getOperator()->getName() == "tconstpool") {
2460 emitDecl("Tmp" + utostr(ResNo));
2461 emitCode("Tmp" + utostr(ResNo) + " = " + Val + ";");
2502 NodeOps.push_back(Val);
2503 // Add Tmp to VariableMap, so that we don't multiply select this
2504 // value if used multiple times by this pattern result.
2505 Val = "Tmp"+utostr(ResNo);
24622506 } else if (N->isLeaf() && (CP = NodeGetComplexPattern(N, ISE))) {
24632507 std::string Fn = CP->getSelectFunc();
2464 NumRes = CP->getNumOperands();
2465 for (unsigned i = 0; i < NumRes; ++i)
2466 emitDecl("CPTmp" + utostr(i+ResNo));
2467
2468 std::string Code = Fn + "(" + Val;
2469 for (unsigned i = 0; i < NumRes; i++)
2470 Code += ", CPTmp" + utostr(i + ResNo);
2471 emitCheck(Code + ")");
2472
2473 for (unsigned i = 0; i < NumRes; ++i) {
2474 emitDecl("Tmp" + utostr(i+ResNo));
2475 emitCode("AddToQueue(Tmp" + utostr(i+ResNo) + ", CPTmp" +
2476 utostr(i+ResNo) + ");");
2477 }
2478
2479 TmpNo = ResNo + NumRes;
2508 for (unsigned i = 0; i < CP->getNumOperands(); ++i) {
2509 emitCode("AddToISelQueue(CPTmp" + utostr(i) + ");");
2510 NodeOps.push_back("CPTmp" + utostr(i));
2511 }
24802512 } else {
2481 emitDecl("Tmp" + utostr(ResNo));
2482 // This node, probably wrapped in a SDNodeXForms, behaves like a leaf
2513 // This node, probably wrapped in a SDNodeXForm, behaves like a leaf
24832514 // node even if it isn't one. Don't select it.
2484 if (LikeLeaf)
2485 emitCode("Tmp" + utostr(ResNo) + " = " + Val + ";");
2486 else {
2487 emitCode("AddToQueue(Tmp" + utostr(ResNo) + ", " + Val + ");");
2515 if (!LikeLeaf) {
2516 emitCode("AddToISelQueue(" + Val + ");");
24882517 if (isRoot && N->isLeaf()) {
2489 emitCode("ReplaceUses(N, Tmp" + utostr(ResNo) + ");");
2490 emitCode("Result = Tmp" + utostr(ResNo) + ";");
2518 emitCode("ReplaceUses(N, " + Val + ");");
2519 emitCode("Result = " + Val + ";");
24912520 emitCode("return NULL;");
24922521 }
24932522 }
2494 }
2495 // Add Tmp to VariableMap, so that we don't multiply select this
2496 // value if used multiple times by this pattern result.
2497 Val = "Tmp"+utostr(ResNo);
2498 return std::make_pair(NumRes, ResNo);
2523 NodeOps.push_back(Val);
2524 }
2525 return NodeOps;
24992526 }
25002527 if (N->isLeaf()) {
25012528 // If this is an explicit register reference, handle it.
25022529 if (DefInit *DI = dynamic_cast(N->getLeafValue())) {
25032530 unsigned ResNo = TmpNo++;
25042531 if (DI->getDef()->isSubClassOf("Register")) {
2505 emitDecl("Tmp" + utostr(ResNo));
2506 emitCode("Tmp" + utostr(ResNo) + " = CurDAG->getRegister(" +
2532 emitCode("SDOperand Tmp" + utostr(ResNo) + " = CurDAG->getRegister(" +
25072533 ISE.getQualifiedName(DI->getDef()) + ", " +
25082534 getEnumName(N->getTypeNum(0)) + ");");
2509 return std::make_pair(1, ResNo);
2535 NodeOps.push_back("Tmp" + utostr(ResNo));
2536 return NodeOps;
25102537 }
25112538 } else if (IntInit *II = dynamic_cast(N->getLeafValue())) {
25122539 unsigned ResNo = TmpNo++;
25132540 assert(N->getExtTypes().size() == 1 && "Multiple types not handled!");
2514 emitDecl("Tmp" + utostr(ResNo));
2515 emitCode("Tmp" + utostr(ResNo) +
2541 emitCode("SDOperand Tmp" + utostr(ResNo) +
25162542 " = CurDAG->getTargetConstant(" + itostr(II->getValue()) +
25172543 ", " + getEnumName(N->getTypeNum(0)) + ");");
2518 return std::make_pair(1, ResNo);
2544 NodeOps.push_back("Tmp" + utostr(ResNo));
2545 return NodeOps;
25192546 }
25202547
25212548 #ifndef NDEBUG
25222549 N->dump();
25232550 #endif
25242551 assert(0 && "Unknown leaf type!");
2525 return std::make_pair(1, ~0U);
2552 return NodeOps;
25262553 }
25272554
25282555 Record *Op = N->getOperator();
25512578 bool InputHasChain = isRoot &&
25522579 NodeHasProperty(Pattern, SDNodeInfo::SDNPHasChain, ISE);
25532580
2554 if (NodeHasInFlag || NodeHasOutFlag || NodeHasOptInFlag || HasImpInputs)
2555 emitDecl("InFlag");
25562581 if (NodeHasOptInFlag) {
2557 emitDecl("HasInFlag", 2);
2558 emitCode("HasInFlag = "
2582 emitCode("bool HasInFlag = "
25592583 "(N.getOperand(N.getNumOperands()-1).getValueType() == MVT::Flag);");
25602584 }
25612585 if (HasVarOps)
25692593 PatResults++;
25702594 }
25712595
2572 // Determine operand emission order. Complex pattern first.
2573 std::vector > EmitOrder;
2574 std::vectorpair >::iterator OI;
2596 std::vectorstring> AllOps;
25752597 for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i) {
2576 TreePatternNode *Child = N->getChild(i);
2577 if (i == 0) {
2578 EmitOrder.push_back(std::make_pair(i, Child));
2579 OI = EmitOrder.begin();
2580 } else if (NodeIsComplexPattern(Child)) {
2581 OI = EmitOrder.insert(OI, std::make_pair(i, Child));
2582 } else {
2583 EmitOrder.push_back(std::make_pair(i, Child));
2584 }
2585 }
2586
2587 // Emit all of the operands.
2588 std::vector > NumTemps(EmitOrder.size());
2589 for (unsigned i = 0, e = EmitOrder.size(); i != e; ++i) {
2590 unsigned OpOrder = EmitOrder[i].first;
2591 TreePatternNode *Child = EmitOrder[i].second;
2592 std::pair NumTemp =
2593 EmitResultCode(Child, RetSelected);
2594 NumTemps[OpOrder] = NumTemp;
2595 }
2596
2597 // List all the operands in the right order.
2598 std::vector Ops;
2599 for (unsigned i = 0, e = NumTemps.size(); i != e; i++) {
2600 for (unsigned j = 0; j < NumTemps[i].first; j++)
2601 Ops.push_back(NumTemps[i].second + j);
2598 std::vector Ops = EmitResultCode(N->getChild(i),
2599 RetSelected, InFlagDecled, ResNodeDecled);
2600 AllOps.insert(AllOps.end(), Ops.begin(), Ops.end());
26022601 }
26032602
26042603 // Emit all the chain and CopyToReg stuff.
26052604 bool ChainEmitted = NodeHasChain;
26062605 if (NodeHasChain)
2607 emitCode("AddToQueue(" + ChainName + ", " + ChainName + ");");
2606 emitCode("AddToISelQueue(" + ChainName + ");");
26082607 if (NodeHasInFlag || HasImpInputs)
2609 EmitInFlagSelectCode(Pattern, "N", ChainEmitted, true);
2608 EmitInFlagSelectCode(Pattern, "N", ChainEmitted,
2609 InFlagDecled, ResNodeDecled, true);
26102610 if (NodeHasOptInFlag) {
2611 emitCode("if (HasInFlag)");
2612 emitCode(" AddToQueue(InFlag, N.getOperand(N.getNumOperands()-1));");
2611 if (!InFlagDecled) {
2612 emitCode("SDOperand InFlag(0, 0);");
2613 InFlagDecled = true;
2614 }
2615 emitCode("if (HasInFlag) {");
2616 emitCode(" InFlag = N.getOperand(N.getNumOperands()-1);");
2617 emitCode(" AddToISelQueue(InFlag);");
2618 emitCode("}");
26132619 }
26142620
26152621 unsigned NumResults = Inst.getNumResults();
26212627 std::string NodeName;
26222628 if (!isRoot) {
26232629 NodeName = "Tmp" + utostr(ResNo);
2624 emitDecl(NodeName);
2625 Code2 = NodeName + " = SDOperand(";
2630 Code2 = "SDOperand " + NodeName + " = SDOperand(";
26262631 } else {
26272632 NodeName = "ResNode";
2628 emitDecl(NodeName, true);
2629 Code2 = NodeName + " = ";
2633 if (!ResNodeDecled)
2634 Code2 = "SDNode *" + NodeName + " = ";
2635 else
2636 Code2 = NodeName + " = ";
26302637 }
26312638 Code = "CurDAG->getTargetNode(Opc" + utostr(OpcNo);
26322639 emitOpcode(II.Namespace + "::" + II.TheDef->getName());
26432650 Code += ", MVT::Flag";
26442651
26452652 // Inputs.
2646 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
2653 for (unsigned i = 0, e = AllOps.size(); i != e; ++i) {
2654 std::string OpName = AllOps[i];
26472655 if (HasVarOps)
2648 emitCode("Ops.push_back(Tmp" + utostr(Ops[i]) + ");");
2656 emitCode("Ops.push_back(" + OpName + ");");
26492657 else
2650 Code += ", Tmp" + utostr(Ops[i]);
2651 }
2658 Code += ", " + OpName;
2659 }
2660 //AllOps.clear();
26522661
26532662 if (HasVarOps) {
26542663 if (NodeHasInFlag || HasImpInputs)
26602669 else
26612670 emitCode("for (unsigned i = 2, e = N.getNumOperands(); "
26622671 "i != e; ++i) {");
2663 emitCode(" SDOperand VarOp(0, 0);");
2664 emitCode(" AddToQueue(VarOp, N.getOperand(i));");
2665 emitCode(" Ops.push_back(VarOp);");
2672 emitCode(" AddToISelQueue(N.getOperand(i));");
2673 emitCode(" Ops.push_back(N.getOperand(i));");
26662674 emitCode("}");
26672675 }
26682676
26732681 Code += ", " + ChainName;
26742682 }
26752683 if (NodeHasInFlag || HasImpInputs) {
2676 if (HasVarOps)
2684 if (!InFlagDecled) {
2685 emitCode("SDOperand InFlag(0, 0);");
2686 InFlagDecled = true;
2687 }
2688 if (HasVarOps) {
26772689 emitCode("Ops.push_back(InFlag);");
2678 else
2690 } else
26792691 Code += ", InFlag";
26802692 } else if (NodeHasOptInFlag && HasVarOps) {
2693 if (!InFlagDecled) {
2694 emitCode("SDOperand InFlag(0, 0);");
2695 InFlagDecled = true;
2696 }
26812697 emitCode("if (HasInFlag)");
26822698 emitCode(" Ops.push_back(InFlag);");
26832699 }
26842700
26852701 if (HasVarOps)
26862702 Code += ", &Ops[0], Ops.size()";
2687 else if (NodeHasOptInFlag)
2703 else if (NodeHasOptInFlag) {
26882704 Code = "HasInFlag ? " + Code + ", InFlag) : " + Code;
2705 }
26892706
26902707 if (!isRoot)
26912708 Code += "), 0";
27002717 emitCode(ChainName + " = SDOperand(" + NodeName +
27012718 ", " + utostr(PatResults) + ");");
27022719
2703 if (!isRoot)
2704 return std::make_pair(1, ResNo);
2720 if (!isRoot) {
2721 NodeOps.push_back("Tmp" + utostr(ResNo));
2722 return NodeOps;
2723 }
27052724
27062725 bool NeedReplace = false;
2707 if (NodeHasOutFlag)
2708 emitCode("InFlag = SDOperand(ResNode, " +
2709 utostr(NumResults + (unsigned)NodeHasChain) + ");");
2710
2711 if (HasImpResults && EmitCopyFromRegs(N, ChainEmitted)) {
2726 if (NodeHasOutFlag) {
2727 if (!InFlagDecled) {
2728 emitCode("SDOperand InFlag = SDOperand(ResNode, " +
2729 utostr(NumResults + (unsigned)NodeHasChain) + ");");
2730 InFlagDecled = true;
2731 } else
2732 emitCode("InFlag = SDOperand(ResNode, " +
2733 utostr(NumResults + (unsigned)NodeHasChain) + ");");
2734 }
2735
2736 if (HasImpResults && EmitCopyFromRegs(N, ResNodeDecled, ChainEmitted)) {
27122737 emitCode("ReplaceUses(SDOperand(N.Val, 0), SDOperand(ResNode, 0));");
27132738 NumResults = 1;
27142739 }
27792804 else
27802805 emitCode("return NULL;");
27812806 } else {
2782 // If this instruction is the root, and if there is only one use of it,
2783 // use SelectNodeTo instead of getTargetNode to avoid an allocation.
2784 emitCode("if (N.Val->hasOneUse()) {");
2785 std::string Code = " Result = CurDAG->SelectNodeTo(N.Val, Opc" +
2807 std::string Code = "Result = CurDAG->SelectNodeTo(N.Val, Opc" +
27862808 utostr(OpcNo);
27872809 if (N->getTypeNum(0) != MVT::isVoid)
27882810 Code += ", VT" + utostr(VTNo);
27892811 if (NodeHasOutFlag)
27902812 Code += ", MVT::Flag";
2791 for (unsigned i = 0, e = Ops.size(); i != e; ++i)
2792 Code += ", Tmp" + utostr(Ops[i]);
2813 for (unsigned i = 0, e = AllOps.size(); i != e; ++i)
2814 Code += ", " + AllOps[i];
2815 //AllOps.clear();
27932816 if (NodeHasInFlag || HasImpInputs)
27942817 Code += ", InFlag";
27952818 emitCode(Code + ");");
2796 emitCode("} else {");
2797 emitDecl("ResNode", 1);
2798 Code = " ResNode = CurDAG->getTargetNode(Opc" + utostr(OpcNo);
2819 emitCode("return Result.Val;");
27992820 emitOpcode(II.Namespace + "::" + II.TheDef->getName());
2800 if (N->getTypeNum(0) != MVT::isVoid) {
2801 Code += ", VT" + utostr(VTNo);
2821 if (N->getTypeNum(0) != MVT::isVoid)
28022822 emitVT(getEnumName(N->getTypeNum(0)));
2803 }
2804 if (NodeHasOutFlag)
2805 Code += ", MVT::Flag";
2806 for (unsigned i = 0, e = Ops.size(); i != e; ++i)
2807 Code += ", Tmp" + utostr(Ops[i]);
2808 if (NodeHasInFlag || HasImpInputs)
2809 Code += ", InFlag";
2810 emitCode(Code + ");");
2811 emitCode(" Result = SDOperand(ResNode, 0);");
2812 emitCode("}");
2813 emitCode("return Result.Val;");
2814 }
2815
2816 return std::make_pair(1, ResNo);
2823 }
2824
2825 return NodeOps;
28172826 } else if (Op->isSubClassOf("SDNodeXForm")) {
28182827 assert(N->getNumChildren() == 1 && "node xform should have one child!");
28192828 // PatLeaf node - the operand may or may not be a leaf node. But it should
28202829 // behave like one.
2821 unsigned OpVal = EmitResultCode(N->getChild(0), RetSelected, true).second;
2830 std::vector Ops =
2831 EmitResultCode(N->getChild(0), RetSelected, InFlagDecled,
2832 ResNodeDecled, true);
28222833 unsigned ResNo = TmpNo++;
2823 emitDecl("Tmp" + utostr(ResNo));
2824 emitCode("Tmp" + utostr(ResNo) + " = Transform_" + Op->getName()
2825 + "(Tmp" + utostr(OpVal) + ".Val);");
2834 emitCode("SDOperand Tmp" + utostr(ResNo) + " = Transform_" + Op->getName()
2835 + "(" + Ops.back() + ".Val);");
2836 NodeOps.push_back("Tmp" + utostr(ResNo));
28262837 if (isRoot) {
2827 //emitCode("ReplaceUses(N, Tmp" + utostr(ResNo) + ");");
28282838 emitCode("Result = Tmp" + utostr(ResNo) + ";");
28292839 emitCode("return Result.Val;");
28302840 }
2831 return std::make_pair(1, ResNo);
2841 return NodeOps;
28322842 } else {
28332843 N->dump();
28342844 std::cerr << "\n";
28662876 /// EmitInFlagSelectCode - Emit the flag operands for the DAG that is
28672877 /// being built.
28682878 void EmitInFlagSelectCode(TreePatternNode *N, const std::string &RootName,
2869 bool &ChainEmitted, bool isRoot = false) {
2879 bool &ChainEmitted, bool &InFlagDecled,
2880 bool &ResNodeDecled, bool isRoot = false) {
28702881 const CodeGenTarget &T = ISE.getTargetInfo();
28712882 unsigned OpNo =
28722883 (unsigned) NodeHasProperty(N, SDNodeInfo::SDNPHasChain, ISE);
28742885 for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i, ++OpNo) {
28752886 TreePatternNode *Child = N->getChild(i);
28762887 if (!Child->isLeaf()) {
2877 EmitInFlagSelectCode(Child, RootName + utostr(OpNo), ChainEmitted);
2888 EmitInFlagSelectCode(Child, RootName + utostr(OpNo), ChainEmitted,
2889 InFlagDecled, ResNodeDecled);
28782890 } else {
28792891 if (DefInit *DI = dynamic_cast(Child->getLeafValue())) {
28802892 if (!Child->getName().empty()) {
28882900 if (RR->isSubClassOf("Register")) {
28892901 MVT::ValueType RVT = getRegisterValueType(RR, T);
28902902 if (RVT == MVT::Flag) {
2891 emitCode("AddToQueue(InFlag, " + RootName + utostr(OpNo) + ");");
2903 if (!InFlagDecled) {
2904 emitCode("SDOperand InFlag = " + RootName + utostr(OpNo) + ";");
2905 InFlagDecled = true;
2906 } else
2907 emitCode("InFlag = " + RootName + utostr(OpNo) + ";");
2908 emitCode("AddToISelQueue(InFlag);");
28922909 } else {
28932910 if (!ChainEmitted) {
2894 emitDecl("Chain");
2895 emitCode("Chain = CurDAG->getEntryNode();");
2911 emitCode("SDOperand Chain = CurDAG->getEntryNode();");
28962912 ChainName = "Chain";
28972913 ChainEmitted = true;
28982914 }
2899 emitCode("AddToQueue(" + RootName + utostr(OpNo) + ", " +
2900 RootName + utostr(OpNo) + ");");
2901 emitCode("ResNode = CurDAG->getCopyToReg(" + ChainName +
2915 emitCode("AddToISelQueue(" + RootName + utostr(OpNo) + ");");
2916 if (!InFlagDecled) {
2917 emitCode("SDOperand InFlag(0, 0);");
2918 InFlagDecled = true;
2919 }
2920 std::string Decl = (!ResNodeDecled) ? "SDNode *" : "";
2921 emitCode(Decl + "ResNode = CurDAG->getCopyToReg(" + ChainName +
29022922 ", CurDAG->getRegister(" + ISE.getQualifiedName(RR) +
29032923 ", " + getEnumName(RVT) + "), " +
29042924 RootName + utostr(OpNo) + ", InFlag).Val;");
2925 ResNodeDecled = true;
29052926 emitCode(ChainName + " = SDOperand(ResNode, 0);");
29062927 emitCode("InFlag = SDOperand(ResNode, 1);");
29072928 }
29102931 }
29112932 }
29122933
2913 if (HasInFlag)
2914 emitCode("AddToQueue(InFlag, " + RootName +
2915 ".getOperand(" + utostr(OpNo) + "));");
2934 if (HasInFlag) {
2935 if (!InFlagDecled) {
2936 emitCode("SDOperand InFlag = " + RootName +
2937 ".getOperand(" + utostr(OpNo) + ");");
2938 InFlagDecled = true;
2939 } else
2940 emitCode("InFlag = " + RootName +
2941 ".getOperand(" + utostr(OpNo) + ");");
2942 emitCode("AddToISelQueue(InFlag);");
2943 }
29162944 }
29172945
29182946 /// EmitCopyFromRegs - Emit code to copy result to physical registers
29192947 /// as specified by the instruction. It returns true if any copy is
29202948 /// emitted.
2921 bool EmitCopyFromRegs(TreePatternNode *N, bool &ChainEmitted) {
2949 bool EmitCopyFromRegs(TreePatternNode *N, bool &ResNodeDecled,
2950 bool &ChainEmitted) {
29222951 bool RetVal = false;
29232952 Record *Op = N->getOperator();
29242953 if (Op->isSubClassOf("Instruction")) {
29312960 MVT::ValueType RVT = getRegisterValueType(RR, CGT);
29322961 if (RVT != MVT::Flag) {
29332962 if (!ChainEmitted) {
2934 emitDecl("Chain");
2935 emitCode("Chain = CurDAG->getEntryNode();");
2963 emitCode("SDOperand Chain = CurDAG->getEntryNode();");
29362964 ChainEmitted = true;
29372965 ChainName = "Chain";
29382966 }
2939 emitCode("ResNode = CurDAG->getCopyFromReg(" + ChainName +
2967 std::string Decl = (!ResNodeDecled) ? "SDNode *" : "";
2968 emitCode(Decl + "ResNode = CurDAG->getCopyFromReg(" + ChainName +
29402969 ", " + ISE.getQualifiedName(RR) + ", " + getEnumName(RVT) +
29412970 ", InFlag).Val;");
2971 ResNodeDecled = true;
29422972 emitCode(ChainName + " = SDOperand(ResNode, 1);");
29432973 emitCode("InFlag = SDOperand(ResNode, 2);");
29442974 RetVal = true;
29542984 /// stream to match the pattern, and generate the code for the match if it
29552985 /// succeeds. Returns true if the pattern is not guaranteed to match.
29562986 void DAGISelEmitter::GenerateCodeForPattern(PatternToMatch &Pattern,
2957 std::vector > &GeneratedCode,
2958 std::set > &GeneratedDecl,
2987 std::vector > &GeneratedCode,
2988 std::set > &GeneratedDecl,
29592989 std::vector &TargetOpcodes,
29602990 std::vector &TargetVTs) {
29612991 PatternCodeEmitter Emitter(*this, Pattern.getPredicates(),
30043034 // otherwise we are done.
30053035 } while (Emitter.InsertOneTypeCheck(Pat, Pattern.getSrcPattern(), "N", true));
30063036
3007 bool RetSelected = false;
3008 Emitter.EmitResultCode(Pattern.getDstPattern(), RetSelected, false, true);
3037 Emitter.EmitResultCode(Pattern.getDstPattern(),
3038 false, false, false, false, true);
30093039 delete Pat;
30103040 }
30113041
30133043 /// a line causes any of them to be empty, remove them and return true when
30143044 /// done.
30153045 static bool EraseCodeLine(std::vector
3016 std::vectorbool, std::string> > > >
3046 std::vectorunsigned, std::string> > > >
30173047 &Patterns) {
30183048 bool ErasedPatterns = false;
30193049 for (unsigned i = 0, e = Patterns.size(); i != e; ++i) {
30303060 /// EmitPatterns - Emit code for at least one pattern, but try to group common
30313061 /// code together between the patterns.
30323062 void DAGISelEmitter::EmitPatterns(std::vector
3033 std::vector, std::string> > > >
3063 std::vector, std::string> > > >
30343064 &Patterns, unsigned Indent,
30353065 std::ostream &OS) {
3036 typedef std::pair<bool, std::string> CodeLine;
3066 typedef std::pair<unsigned, std::string> CodeLine;
30373067 typedef std::vector CodeList;
30383068 typedef std::vector > PatternList;
30393069
30693099 << " size = "
30703100 << getResultPatternSize(Pattern.getDstPattern(), *this) << "\n";
30713101 }
3072 if (!FirstCodeLine.first) {
3102 if (FirstCodeLine.first != 1) {
30733103 OS << std::string(Indent, ' ') << "{\n";
30743104 Indent += 2;
30753105 }
30763106 EmitPatterns(Shared, Indent, OS);
3077 if (!FirstCodeLine.first) {
3107 if (FirstCodeLine.first != 1) {
30783108 Indent -= 2;
30793109 OS << std::string(Indent, ' ') << "}\n";
30803110 }
31013131 // Remove this code from all of the patterns that share it.
31023132 bool ErasedPatterns = EraseCodeLine(Patterns);
31033133
3104 bool isPredicate = FirstCodeLine.first;
3134 bool isPredicate = FirstCodeLine.first == 1;
31053135
31063136 // Otherwise, every pattern in the list has this line. Emit it.
31073137 if (!isPredicate) {
31133143 // If the next code line is another predicate, and if all of the pattern
31143144 // in this group share the same next line, emit it inline now. Do this
31153145 // until we run out of common predicates.
3116 while (!ErasedPatterns && Patterns.back().second.back().first) {
3146 while (!ErasedPatterns && Patterns.back().second.back().first == 1) {
31173147 // Check that all of fhe patterns in Patterns end with the same predicate.
31183148 bool AllEndWithSamePredicate = true;
31193149 for (unsigned i = 0, e = Patterns.size(); i != e; ++i)
32373267 ++II) {
32383268 MVT::ValueType OpVT = II->first;
32393269 std::vector &Patterns = II->second;
3240 typedef std::vector > CodeList;
3241 typedef std::vectorbool, std::string> >::iterator CodeListI;
3270 typedef std::vectorunsigned, std::string> > CodeList;
3271 typedef std::vector >::iterator CodeListI;
32423272
32433273 std::vector > CodeForPatterns;
32443274 std::vector > PatternOpcodes;
32453275 std::vector > PatternVTs;
32463276 std::vector > > PatternDecls;
3247 std::set > AllGenDecls;
32483277 for (unsigned i = 0, e = Patterns.size(); i != e; ++i) {
32493278 CodeList GeneratedCode;
32503279 std::set > GeneratedDecl;
32523281 std::vector TargetVTs;
32533282 GenerateCodeForPattern(*Patterns[i], GeneratedCode, GeneratedDecl,
32543283 TargetOpcodes, TargetVTs);
3255 for (std::set >::iterator
3256 si = GeneratedDecl.begin(), se = GeneratedDecl.end(); si!=se; ++si)
3257 AllGenDecls.insert(*si);
32583284 CodeForPatterns.push_back(std::make_pair(Patterns[i], GeneratedCode));
32593285 PatternDecls.push_back(GeneratedDecl);
32603286 PatternOpcodes.push_back(TargetOpcodes);
32693295 mightNotMatch = false;
32703296
32713297 for (unsigned j = 0, e = GeneratedCode.size(); j != e; ++j) {
3272 if (GeneratedCode[j].first) { // predicate.
3298 if (GeneratedCode[j].first == 1) { // predicate.
32733299 mightNotMatch = true;
32743300 break;
32753301 }
32793305 // patterns after it CANNOT ever match. Error out.
32803306 if (mightNotMatch == false && i != CodeForPatterns.size()-1) {
32813307 std::cerr << "Pattern '";
3282 CodeForPatterns[i+1].first->getSrcPattern()->print(std::cerr);
3308 CodeForPatterns[i].first->getSrcPattern()->print(std::cerr);
32833309 std::cerr << "' is impossible to select!\n";
32843310 exit(1);
32853311 }
32933319 std::vector &TargetOpcodes = PatternOpcodes[i];
32943320 std::vector &TargetVTs = PatternVTs[i];
32953321 std::set > Decls = PatternDecls[i];
3322 std::vector AddedInits;
32963323 int CodeSize = (int)GeneratedCode.size();
32973324 int LastPred = -1;
32983325 for (int j = CodeSize-1; j >= 0; --j) {
3299 if (GeneratedCode[j].first) {
3326 if (LastPred == -1 && GeneratedCode[j].first == 1)
33003327 LastPred = j;
3301 break;
3302 }
3303 }
3304
3305 std::string CalleeDecls;
3328 else if (LastPred != -1 && GeneratedCode[j].first == 2)
3329 AddedInits.push_back(GeneratedCode[j].second);
3330 }
3331
33063332 std::string CalleeCode = "(SDOperand &Result, const SDOperand &N";
33073333 std::string CallerCode = "(Result, N";
33083334 for (unsigned j = 0, e = TargetOpcodes.size(); j != e; ++j) {
33163342 for (std::set >::iterator
33173343 I = Decls.begin(), E = Decls.end(); I != E; ++I) {
33183344 std::string Name = I->second;
3319 if (I->first == 0) {
3320 if (Name == "InFlag" ||
3321 (Name.size() > 3 &&
3322 Name[0] == 'T' && Name[1] == 'm' && Name[2] == 'p')) {
3323 CalleeDecls += " SDOperand " + Name + "(0, 0);\n";
3324 continue;
3325 }
3326 CalleeCode += ", SDOperand &" + Name;
3327 CallerCode += ", " + Name;
3328 } else if (I->first == 1) {
3329 if (Name == "ResNode") {
3330 CalleeDecls += " SDNode *" + Name + " = NULL;\n";
3331 continue;
3332 }
3333 CalleeCode += ", SDNode *" + Name;
3334 CallerCode += ", " + Name;
3335 } else {
3336 CalleeCode += ", bool " + Name;
3337 CallerCode += ", " + Name;
3338 }
3345 CalleeCode += ", SDOperand &" + Name;
3346 CallerCode += ", " + Name;
33393347 }
33403348 CallerCode += ");";
33413349 CalleeCode += ") ";
33423350 // Prevent emission routines from being inlined to reduce selection
33433351 // routines stack frame sizes.
33443352 CalleeCode += "NOINLINE ";
3345 CalleeCode += "{\n" + CalleeDecls;
3346 for (int j = LastPred+1; j < CodeSize; ++j)
3347 CalleeCode += " " + GeneratedCode[j].second + '\n';
3353 CalleeCode += "{\n";
3354
3355 for (std::vector::const_reverse_iterator
3356 I = AddedInits.rbegin(), E = AddedInits.rend(); I != E; ++I)
3357 CalleeCode += " " + *I + "\n";
3358
3359 for (int j = LastPred+1; j < CodeSize; ++j) {
3360 std::string code = GeneratedCode[j].second;
3361 // if (!AddedDecls.count(code))
3362 CalleeCode += " " + code + "\n";
3363 }
33483364 for (int j = LastPred+1; j < CodeSize; ++j)
33493365 GeneratedCode.pop_back();
33503366 CalleeCode += "}\n";
33813397
33823398 OS << "SDNode *Select_" << OpName << (OpVTStr != "" ? "_" : "")
33833399 << OpVTStr << "(SDOperand &Result, const SDOperand &N) {\n";
3384
3385 // Print all declarations.
3386 for (std::set >::iterator
3387 I = AllGenDecls.begin(), E = AllGenDecls.end(); I != E; ++I)
3388 if (I->first == 0)
3389 OS << " SDOperand " << I->second << "(0, 0);\n";
3390 else if (I->first == 1)
3391 OS << " SDNode *" << I->second << " = NULL;\n";
3392 else
3393 OS << " bool " << I->second << " = false;\n";
33943400
33953401 // Loop through and reverse all of the CodeList vectors, as we will be
33963402 // accessing them from their logical front, but accessing the end of a
34313437 // Emit boilerplate.
34323438 OS << "SDNode *Select_INLINEASM(SDOperand& Result, SDOperand N) {\n"
34333439 << " std::vector Ops(N.Val->op_begin(), N.Val->op_end());\n"
3434 << " AddToQueue(Ops[0], N.getOperand(0)); // Select the chain.\n\n"
3440 << " AddToISelQueue(N.getOperand(0)); // Select the chain.\n\n"
34353441 << " // Select the flag operand.\n"
34363442 << " if (Ops.back().getValueType() == MVT::Flag)\n"
3437 << " AddToQueue(Ops.back(), Ops.back());\n"
3443 << " AddToISelQueue(Ops.back());\n"
34383444 << " SelectInlineAsmMemoryOperands(Ops, *CurDAG);\n"
34393445 << " std::vector VTs;\n"
34403446 << " VTs.push_back(MVT::Other);\n"
34693475 << " }\n"
34703476 << " case ISD::AssertSext:\n"
34713477 << " case ISD::AssertZext: {\n"
3472 << " AddToQueue(Result, N.getOperand(0));\n"
3473 << " ReplaceUses(N, Result);\n"
3478 << " AddToISelQueue(N.getOperand(0));\n"
3479 << " ReplaceUses(N, N.getOperand(0));\n"
34743480 << " return NULL;\n"
34753481 << " }\n"
34763482 << " case ISD::TokenFactor:\n"
34773483 << " case ISD::CopyFromReg:\n"
34783484 << " case ISD::CopyToReg: {\n"
3479 << " for (unsigned i = 0, e = N.getNumOperands(); i != e; ++i) {\n"
3480 << " SDOperand Dummy;\n"
3481 << " AddToQueue(Dummy, N.getOperand(i));\n"
3482 << " }\n"
3485 << " for (unsigned i = 0, e = N.getNumOperands(); i != e; ++i)\n"
3486 << " AddToISelQueue(N.getOperand(i));\n"
34833487 << " Result = N;\n"
34843488 << " return NULL;\n"
34853489 << " }\n"
36003604 OS << " return ISelSelected[Id / 8] & (1 << (Id % 8));\n";
36013605 OS << "}\n\n";
36023606
3603 OS << "void AddToQueue(SDOperand &Result, SDOperand N) NOINLINE {\n";
3604 OS << " Result = N;\n";
3607 OS << "void AddToISelQueue(SDOperand N) NOINLINE {\n";
36053608 OS << " int Id = N.Val->getNodeId();\n";
36063609 OS << " if (Id != -1 && !isQueued(Id)) {\n";
36073610 OS << " ISelQueue.push_back(N.Val);\n";
519519 std::vector &InstImpInputs,
520520 std::vector &InstImpResults);
521521 void GenerateCodeForPattern(PatternToMatch &Pattern,
522 std::vector, std::string> > &GeneratedCode,
522 std::vector, std::string> > &GeneratedCode,
523523 std::set > &GeneratedDecl,
524524 std::vector &TargetOpcodes,
525525 std::vector &TargetVTs);
526526 void EmitPatterns(std::vector
527 std::vector, std::string> > > > &Patterns,
527 std::vector, std::string> > > > &Patterns,
528528 unsigned Indent, std::ostream &OS);
529529 void EmitInstructionSelector(std::ostream &OS);
530530 };