llvm.org GIT mirror llvm / 8d4ccf0
Reverting back to original 1.8 version so I can manually merge in patch. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@29584 91177308-0d34-0410-b5e6-96231b3b80d8 Tanya Lattner 13 years ago
1 changed file(s) with 519 addition(s) and 414 deletion(s). Raw diff Collapse all Expand all
18281828 // If this node is commutative, consider the commuted order.
18291829 if (NodeInfo.hasProperty(SDNodeInfo::SDNPCommutative)) {
18301830 assert(N->getNumChildren()==2 &&"Commutative but doesn't have 2 children!");
1831 // Don't count children which are actually register references.
1832 unsigned NC = 0;
1833 for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i) {
1834 TreePatternNode *Child = N->getChild(i);
1835 if (Child->isLeaf())
1836 if (DefInit *DI = dynamic_cast(Child->getLeafValue())) {
1837 Record *RR = DI->getDef();
1838 if (RR->isSubClassOf("Register"))
1839 continue;
1840 }
1841 NC++;
1842 }
18431831 // Consider the commuted order.
1844 if (NC == 2)
1845 CombineChildVariants(N, ChildVariants[1], ChildVariants[0],
1846 OutVariants, ISE);
1832 CombineChildVariants(N, ChildVariants[1], ChildVariants[0],
1833 OutVariants, ISE);
18471834 }
18481835 }
18491836
21182105 // Names of all the folded nodes which produce chains.
21192106 std::vector > FoldedChains;
21202107 std::set Duplicates;
2108 /// These nodes are being marked "in-flight" so they cannot be folded.
2109 std::vector InflightNodes;
21212110
21222111 /// GeneratedCode - This is the buffer that we emit code to. The first bool
21232112 /// indicates whether this is an exit predicate (something that should be
21332122 std::vector &TargetVTs;
21342123
21352124 std::string ChainName;
2125 bool DoReplace;
21362126 unsigned TmpNo;
21372127 unsigned OpcNo;
21382128 unsigned VTNo;
21632153 std::vector > &gc,
21642154 std::set > &gd,
21652155 std::vector &to,
2166 std::vector &tv)
2156 std::vector &tv,
2157 bool dorep)
21672158 : ISE(ise), Predicates(preds), Pattern(pattern), Instruction(instr),
21682159 GeneratedCode(gc), GeneratedDecl(gd), TargetOpcodes(to), TargetVTs(tv),
2169 TmpNo(0), OpcNo(0), VTNo(0) {}
2160 DoReplace(dorep), TmpNo(0), OpcNo(0), VTNo(0) {}
21702161
21712162 /// EmitMatchCode - Emit a matcher for N, going to the label for PatternNo
21722163 /// if the match fails. At this point, we already know that the opcode for N
22332224 bool HasChain = PatternHasProperty(N, SDNodeInfo::SDNPHasChain, ISE);
22342225 bool HasOutFlag = PatternHasProperty(N, SDNodeInfo::SDNPOutFlag, ISE);
22352226 bool EmittedUseCheck = false;
2227 bool EmittedSlctedCheck = false;
22362228 if (HasChain) {
22372229 if (NodeHasChain)
22382230 OpNo = 1;
22392231 if (!isRoot) {
22402232 const SDNodeInfo &CInfo = ISE.getSDNodeInfo(N->getOperator());
2233 // Not in flight?
2234 emitCheck("InFlightSet.count(" + RootName + ".Val) == 0");
22412235 // Multiple uses of actual result?
22422236 emitCheck(RootName + ".hasOneUse()");
22432237 EmittedUseCheck = true;
2238 // hasOneUse() check is not strong enough. If the original node has
2239 // already been selected, it may have been replaced with another.
2240 for (unsigned j = 0; j != CInfo.getNumResults(); j++)
2241 emitCheck("!CodeGenMap.count(" + RootName + ".getValue(" + utostr(j) +
2242 "))");
2243
2244 EmittedSlctedCheck = true;
22442245 if (NodeHasChain) {
22452246 // FIXME: Don't fold if 1) the parent node writes a flag, 2) the node
22462247 // has a chain use.
22782279 PInfo.hasProperty(SDNodeInfo::SDNPHasChain) ||
22792280 PInfo.hasProperty(SDNodeInfo::SDNPInFlag) ||
22802281 PInfo.hasProperty(SDNodeInfo::SDNPOptInFlag))
2281 emitCheck("CanBeFoldedBy(" + RootName + ".Val, " + ParentName +
2282 ".Val)");
2282 if (PInfo.getNumOperands() > 1) {
2283 emitCheck("!isNonImmUse(" + ParentName + ".Val, " + RootName +
2284 ".Val)");
2285 } else {
2286 emitCheck("(" + ParentName + ".getNumOperands() == 1 || !" +
2287 "isNonImmUse(" + ParentName + ".Val, " + RootName +
2288 ".Val))");
2289 }
22832290 }
22842291 }
22852292
23092316 // Multiple uses of actual result?
23102317 emitCheck(RootName + ".hasOneUse()");
23112318 }
2319 if (!EmittedSlctedCheck)
2320 // hasOneUse() check is not strong enough. If the original node has
2321 // already been selected, it may have been replaced with another.
2322 for (unsigned j = 0; j < CInfo.getNumResults(); j++)
2323 emitCheck("!CodeGenMap.count(" + RootName + ".getValue(" + utostr(j) +
2324 "))");
23122325 }
23132326
23142327 for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i, ++OpNo) {
24652478 for (unsigned i = 0; i < NumRes; ++i)
24662479 emitDecl("CPTmp" + utostr(i+ResNo));
24672480
2468 std::string Code = Fn + "(" + Val;
2481 std::string Code = "bool Match = " + Fn + "(" + Val;
24692482 for (unsigned i = 0; i < NumRes; i++)
24702483 Code += ", CPTmp" + utostr(i + ResNo);
2471 emitCheck(Code + ")");
2472
2484 emitCode(Code + ");");
2485 if (InflightNodes.size()) {
2486 // Remove the in-flight nodes if the ComplexPattern does not match!
2487 emitCode("if (!Match) {");
2488 for (std::vector::iterator AI = InflightNodes.begin(),
2489 AE = InflightNodes.end(); AI != AE; ++AI)
2490 emitCode(" SelectionDAG::RemoveInFlightSetEntry(InFlightSet, " +
2491 *AI + ".Val);");
2492 emitCode("}");
2493 }
2494
2495 emitCheck("Match");
2496
2497 for (unsigned i = 0; i < NumRes; ++i) {
2498 emitCode("SelectionDAG::InsertInFlightSetEntry(InFlightSet, CPTmp" +
2499 utostr(i+ResNo) + ".Val);");
2500 InflightNodes.push_back("CPTmp" + utostr(i+ResNo));
2501 }
24732502 for (unsigned i = 0; i < NumRes; ++i) {
24742503 emitDecl("Tmp" + utostr(i+ResNo));
2475 emitCode("AddToQueue(Tmp" + utostr(i+ResNo) + ", CPTmp" +
2504 emitCode("Select(Tmp" + utostr(i+ResNo) + ", CPTmp" +
24762505 utostr(i+ResNo) + ");");
24772506 }
24782507
24842513 if (LikeLeaf)
24852514 emitCode("Tmp" + utostr(ResNo) + " = " + Val + ";");
24862515 else {
2487 emitCode("AddToQueue(Tmp" + utostr(ResNo) + ", " + Val + ");");
2488 if (isRoot && N->isLeaf()) {
2489 emitCode("ReplaceUses(N, Tmp" + utostr(ResNo) + ");");
2490 emitCode("Result = Tmp" + utostr(ResNo) + ";");
2491 emitCode("return;");
2492 }
2516 emitCode("Select(Tmp" + utostr(ResNo) + ", " + Val + ");");
2517 }
2518
2519 if (isRoot && N->isLeaf()) {
2520 emitCode("Result = Tmp" + utostr(ResNo) + ";");
2521 emitCode("return;");
24932522 }
24942523 }
24952524 // Add Tmp to VariableMap, so that we don't multiply select this
25842613 }
25852614 }
25862615
2616 // Make sure these operands which would be selected won't be folded while
2617 // the isel traverses the DAG upward.
2618 for (unsigned i = 0, e = EmitOrder.size(); i != e; ++i) {
2619 TreePatternNode *Child = EmitOrder[i].second;
2620 if (!Child->getName().empty()) {
2621 std::string &Val = VariableMap[Child->getName()];
2622 assert(!Val.empty() &&
2623 "Variable referenced but not defined and not caught earlier!");
2624 if (Child->isLeaf() && !NodeGetComplexPattern(Child, ISE)) {
2625 emitCode("SelectionDAG::InsertInFlightSetEntry(InFlightSet, " +
2626 Val + ".Val);");
2627 InflightNodes.push_back(Val);
2628 }
2629 }
2630 }
2631
25872632 // Emit all of the operands.
25882633 std::vector > NumTemps(EmitOrder.size());
25892634 for (unsigned i = 0, e = EmitOrder.size(); i != e; ++i) {
26032648 // Emit all the chain and CopyToReg stuff.
26042649 bool ChainEmitted = NodeHasChain;
26052650 if (NodeHasChain)
2606 emitCode("AddToQueue(" + ChainName + ", " + ChainName + ");");
2651 emitCode("Select(" + ChainName + ", " + ChainName + ");");
26072652 if (NodeHasInFlag || HasImpInputs)
26082653 EmitInFlagSelectCode(Pattern, "N", ChainEmitted, true);
26092654 if (NodeHasOptInFlag) {
26102655 emitCode("if (HasInFlag)");
2611 emitCode(" AddToQueue(InFlag, N.getOperand(N.getNumOperands()-1));");
2656 emitCode(" Select(InFlag, N.getOperand(N.getNumOperands()-1));");
2657 }
2658
2659 if (isRoot) {
2660 // The operands have been selected. Remove them from InFlightSet.
2661 for (std::vector::iterator AI = InflightNodes.begin(),
2662 AE = InflightNodes.end(); AI != AE; ++AI)
2663 emitCode("SelectionDAG::RemoveInFlightSetEntry(InFlightSet, " +
2664 *AI + ".Val);");
26122665 }
26132666
26142667 unsigned NumResults = Inst.getNumResults();
26602713 emitCode("for (unsigned i = 2, e = N.getNumOperands(); "
26612714 "i != e; ++i) {");
26622715 emitCode(" SDOperand VarOp(0, 0);");
2663 emitCode(" AddToQueue(VarOp, N.getOperand(i));");
2716 emitCode(" Select(VarOp, N.getOperand(i));");
26642717 emitCode(" Ops.push_back(VarOp);");
26652718 emitCode("}");
26662719 }
26822735 }
26832736
26842737 if (HasVarOps)
2685 Code += ", &Ops[0], Ops.size()";
2738 Code += ", Ops";
26862739 else if (NodeHasOptInFlag)
26872740 Code = "HasInFlag ? " + Code + ", InFlag) : " + Code;
26882741
27032756 return std::make_pair(1, ResNo);
27042757
27052758 for (unsigned i = 0; i < NumResults; i++)
2706 emitCode("ReplaceUses(SDOperand(N.Val, " +
2707 utostr(i) + "), SDOperand(ResNode, " + utostr(i) + "));");
2759 emitCode("SelectionDAG::InsertISelMapEntry(CodeGenMap, N.Val, " +
2760 utostr(i) + ", ResNode, " + utostr(i) + ");");
27082761
27092762 if (NodeHasOutFlag)
27102763 emitCode("InFlag = SDOperand(ResNode, " +
27112764 utostr(NumResults + (unsigned)NodeHasChain) + ");");
27122765
27132766 if (HasImpResults && EmitCopyFromRegs(N, ChainEmitted)) {
2714 emitCode("ReplaceUses(SDOperand(N.Val, 0), SDOperand(ResNode, 0));");
2767 emitCode("SelectionDAG::InsertISelMapEntry(CodeGenMap, N.Val, "
2768 "0, ResNode, 0);");
27152769 NumResults = 1;
27162770 }
27172771
2718 if (InputHasChain)
2719 emitCode("ReplaceUses(SDOperand(N.Val, " +
2720 utostr(PatResults) + "), SDOperand(" + ChainName + ".Val, " +
2721 ChainName + ".ResNo" + "));");
2772 if (InputHasChain) {
2773 emitCode("SelectionDAG::InsertISelMapEntry(CodeGenMap, N.Val, " +
2774 utostr(PatResults) + ", " + ChainName + ".Val, " +
2775 ChainName + ".ResNo" + ");");
2776 if (DoReplace)
2777 emitCode("if (N.ResNo == 0) AddHandleReplacement(N.Val, " +
2778 utostr(PatResults) + ", " + ChainName + ".Val, " +
2779 ChainName + ".ResNo" + ");");
2780 }
27222781
27232782 if (FoldedChains.size() > 0) {
27242783 std::string Code;
27252784 for (unsigned j = 0, e = FoldedChains.size(); j < e; j++)
2726 emitCode("ReplaceUses(SDOperand(" +
2785 emitCode("SelectionDAG::InsertISelMapEntry(CodeGenMap, " +
27272786 FoldedChains[j].first + ".Val, " +
2728 utostr(FoldedChains[j].second) + "), SDOperand(ResNode, " +
2729 utostr(NumResults) + "));");
2787 utostr(FoldedChains[j].second) + ", ResNode, " +
2788 utostr(NumResults) + ");");
2789
2790 for (unsigned j = 0, e = FoldedChains.size(); j < e; j++) {
2791 std::string Code =
2792 FoldedChains[j].first + ".Val, " +
2793 utostr(FoldedChains[j].second) + ", ";
2794 emitCode("AddHandleReplacement(" + Code + "ResNode, " +
2795 utostr(NumResults) + ");");
2796 }
27302797 }
27312798
27322799 if (NodeHasOutFlag)
2733 emitCode("ReplaceUses(SDOperand(N.Val, " +
2734 utostr(PatResults + (unsigned)InputHasChain) +"), InFlag);");
2800 emitCode("SelectionDAG::InsertISelMapEntry(CodeGenMap, N.Val, " +
2801 utostr(PatResults + (unsigned)InputHasChain) +
2802 ", InFlag.Val, InFlag.ResNo);");
27352803
27362804 // User does not expect the instruction would produce a chain!
27372805 bool AddedChain = NodeHasChain && !InputHasChain;
27882856 if (NodeHasInFlag || HasImpInputs)
27892857 Code += ", InFlag";
27902858 emitCode(Code + ");");
2791 emitCode(" ReplaceUses(N, SDOperand(ResNode, 0));");
2859 emitCode(" SelectionDAG::InsertISelMapEntry(CodeGenMap, N.Val, N.ResNo"
2860 ", ResNode, 0);");
27922861 emitCode(" Result = SDOperand(ResNode, 0);");
27932862 emitCode("}");
27942863 }
28062875 emitCode("Tmp" + utostr(ResNo) + " = Transform_" + Op->getName()
28072876 + "(Tmp" + utostr(OpVal) + ".Val);");
28082877 if (isRoot) {
2809 emitCode("ReplaceUses(N, Tmp" + utostr(ResNo) + ");");
2878 emitCode("SelectionDAG::InsertISelMapEntry(CodeGenMap, N.Val,"
2879 "N.ResNo, Tmp" + utostr(ResNo) + ".Val, Tmp" +
2880 utostr(ResNo) + ".ResNo);");
28102881 emitCode("Result = Tmp" + utostr(ResNo) + ";");
28112882 emitCode("return;");
28122883 }
28232894 /// 'Pat' may be missing types. If we find an unresolved type to add a check
28242895 /// for, this returns true otherwise false if Pat has all types.
28252896 bool InsertOneTypeCheck(TreePatternNode *Pat, TreePatternNode *Other,
2826 const std::string &Prefix, bool isRoot = false) {
2897 const std::string &Prefix) {
28272898 // Did we find one?
28282899 if (Pat->getExtTypes() != Other->getExtTypes()) {
28292900 // Move a type over from 'other' to 'pat'.
28302901 Pat->setTypes(Other->getExtTypes());
2831 // The top level node type is checked outside of the select function.
2832 if (!isRoot)
2833 emitCheck(Prefix + ".Val->getValueType(0) == " +
2834 getName(Pat->getTypeNum(0)));
2902 emitCheck(Prefix + ".Val->getValueType(0) == " +
2903 getName(Pat->getTypeNum(0)));
28352904 return true;
28362905 }
28372906
28702939 if (RR->isSubClassOf("Register")) {
28712940 MVT::ValueType RVT = getRegisterValueType(RR, T);
28722941 if (RVT == MVT::Flag) {
2873 emitCode("AddToQueue(InFlag, " + RootName + utostr(OpNo) + ");");
2942 emitCode("Select(InFlag, " + RootName + utostr(OpNo) + ");");
28742943 } else {
28752944 if (!ChainEmitted) {
28762945 emitDecl("Chain");
28782947 ChainName = "Chain";
28792948 ChainEmitted = true;
28802949 }
2881 emitCode("AddToQueue(" + RootName + utostr(OpNo) + ", " +
2950 emitCode("Select(" + RootName + utostr(OpNo) + ", " +
28822951 RootName + utostr(OpNo) + ");");
28832952 emitCode("ResNode = CurDAG->getCopyToReg(" + ChainName +
28842953 ", CurDAG->getRegister(" + ISE.getQualifiedName(RR) +
28932962 }
28942963
28952964 if (HasInFlag)
2896 emitCode("AddToQueue(InFlag, " + RootName +
2965 emitCode("Select(InFlag, " + RootName +
28972966 ".getOperand(" + utostr(OpNo) + "));");
28982967 }
28992968
29393008 std::vector > &GeneratedCode,
29403009 std::set > &GeneratedDecl,
29413010 std::vector &TargetOpcodes,
2942 std::vector &TargetVTs) {
3011 std::vector &TargetVTs,
3012 bool DoReplace) {
29433013 PatternCodeEmitter Emitter(*this, Pattern.getPredicates(),
29443014 Pattern.getSrcPattern(), Pattern.getDstPattern(),
29453015 GeneratedCode, GeneratedDecl,
2946 TargetOpcodes, TargetVTs);
3016 TargetOpcodes, TargetVTs,
3017 DoReplace);
29473018
29483019 // Emit the matcher, capturing named arguments in VariableMap.
29493020 bool FoundChain = false;
29833054 // Insert a check for an unresolved type and add it to the tree. If we find
29843055 // an unresolved type to add a check for, this returns true and we iterate,
29853056 // otherwise we are done.
2986 } while (Emitter.InsertOneTypeCheck(Pat, Pattern.getSrcPattern(), "N", true));
3057 } while (Emitter.InsertOneTypeCheck(Pat, Pattern.getSrcPattern(), "N"));
29873058
29883059 Emitter.EmitResultCode(Pattern.getDstPattern(), false, true /*the root*/);
29893060 delete Pat;
30703141 OS << std::string(Indent, ' ') << "// Pattern complexity = "
30713142 << getPatternSize(Pattern.getSrcPattern(), *this) + AddedComplexity
30723143 << " cost = "
3073 << getResultPatternCost(Pattern.getDstPattern(), *this)
3074 << " size = "
3075 << getResultPatternSize(Pattern.getDstPattern(), *this) << "\n";
3144 << getResultPatternCost(Pattern.getDstPattern(), *this) << "\n";
30763145 }
30773146 EmitPatterns(Other, Indent, OS);
30783147 return;
31703239 }
31713240 }
31723241 }
3173
3174 // For each opcode, there might be multiple select functions, one per
3175 // ValueType of the node (or its first operand if it doesn't produce a
3176 // non-chain result.
3177 std::map > OpcodeVTMap;
3178
3242
31793243 // Emit one Select_* method for each top-level opcode. We do this instead of
31803244 // emitting one giant switch statement to support compilers where this will
31813245 // result in the recursive functions taking less stack space.
31843248 E = PatternsByOpcode.end(); PBOI != E; ++PBOI) {
31853249 const std::string &OpName = PBOI->first->getName();
31863250 const SDNodeInfo &OpcodeInfo = getSDNodeInfo(PBOI->first);
3187 std::vector &PatternsOfOp = PBOI->second;
3188 assert(!PatternsOfOp.empty() && "No patterns but map has entry?");
3189
3251 bool OptSlctOrder =
3252 (OpcodeInfo.hasProperty(SDNodeInfo::SDNPHasChain) &&
3253 OpcodeInfo.getNumResults() > 0);
3254 std::vector &Patterns = PBOI->second;
3255 assert(!Patterns.empty() && "No patterns but map has entry?");
3256
31903257 // We want to emit all of the matching code now. However, we want to emit
31913258 // the matches in order of minimal cost. Sort the patterns so the least
31923259 // cost one is at the start.
3193 std::stable_sort(PatternsOfOp.begin(), PatternsOfOp.end(),
3260 std::stable_sort(Patterns.begin(), Patterns.end(),
31943261 PatternSortingPredicate(*this));
31953262
3196 // Split them into groups by type.
3197 std::map > PatternsByType;
3198 for (unsigned i = 0, e = PatternsOfOp.size(); i != e; ++i) {
3199 PatternToMatch *Pat = PatternsOfOp[i];
3200 TreePatternNode *SrcPat = Pat->getSrcPattern();
3201 if (OpcodeInfo.getNumResults() == 0 && SrcPat->getNumChildren() > 0)
3202 SrcPat = SrcPat->getChild(0);
3203 MVT::ValueType VT = SrcPat->getTypeNum(0);
3204 std::map >::iterator TI =
3205 PatternsByType.find(VT);
3206 if (TI != PatternsByType.end())
3207 TI->second.push_back(Pat);
3208 else {
3209 std::vector PVec;
3210 PVec.push_back(Pat);
3211 PatternsByType.insert(std::make_pair(VT, PVec));
3212 }
3213 }
3214
3215 for (std::map >::iterator
3216 II = PatternsByType.begin(), EE = PatternsByType.end(); II != EE;
3217 ++II) {
3218 MVT::ValueType OpVT = II->first;
3219 std::vector &Patterns = II->second;
3220 typedef std::vector > CodeList;
3221 typedef std::vector >::iterator CodeListI;
3222
3223 std::vector > CodeForPatterns;
3224 std::vector > PatternOpcodes;
3225 std::vector > PatternVTs;
3226 std::vector > > PatternDecls;
3227 std::set > AllGenDecls;
3228 for (unsigned i = 0, e = Patterns.size(); i != e; ++i) {
3229 CodeList GeneratedCode;
3230 std::set > GeneratedDecl;
3231 std::vector TargetOpcodes;
3232 std::vector TargetVTs;
3233 GenerateCodeForPattern(*Patterns[i], GeneratedCode, GeneratedDecl,
3234 TargetOpcodes, TargetVTs);
3235 for (std::set >::iterator
3236 si = GeneratedDecl.begin(), se = GeneratedDecl.end(); si!=se; ++si)
3237 AllGenDecls.insert(*si);
3238 CodeForPatterns.push_back(std::make_pair(Patterns[i], GeneratedCode));
3239 PatternDecls.push_back(GeneratedDecl);
3240 PatternOpcodes.push_back(TargetOpcodes);
3241 PatternVTs.push_back(TargetVTs);
3242 }
3243
3244 // Scan the code to see if all of the patterns are reachable and if it is
3245 // possible that the last one might not match.
3246 bool mightNotMatch = true;
3247 for (unsigned i = 0, e = CodeForPatterns.size(); i != e; ++i) {
3248 CodeList &GeneratedCode = CodeForPatterns[i].second;
3249 mightNotMatch = false;
3250
3251 for (unsigned j = 0, e = GeneratedCode.size(); j != e; ++j) {
3252 if (GeneratedCode[j].first) { // predicate.
3253 mightNotMatch = true;
3254 break;
3263 typedef std::vector > CodeList;
3264 typedef std::vector >::iterator CodeListI;
3265
3266 std::vector > CodeForPatterns;
3267 std::vector > PatternOpcodes;
3268 std::vector > PatternVTs;
3269 std::vector > > PatternDecls;
3270 std::set > AllGenDecls;
3271 for (unsigned i = 0, e = Patterns.size(); i != e; ++i) {
3272 CodeList GeneratedCode;
3273 std::set > GeneratedDecl;
3274 std::vector TargetOpcodes;
3275 std::vector TargetVTs;
3276 GenerateCodeForPattern(*Patterns[i], GeneratedCode, GeneratedDecl,
3277 TargetOpcodes, TargetVTs, OptSlctOrder);
3278 for (std::set >::iterator
3279 si = GeneratedDecl.begin(), se = GeneratedDecl.end(); si!=se; ++si)
3280 AllGenDecls.insert(*si);
3281 CodeForPatterns.push_back(std::make_pair(Patterns[i], GeneratedCode));
3282 PatternDecls.push_back(GeneratedDecl);
3283 PatternOpcodes.push_back(TargetOpcodes);
3284 PatternVTs.push_back(TargetVTs);
3285 }
3286
3287 // Scan the code to see if all of the patterns are reachable and if it is
3288 // possible that the last one might not match.
3289 bool mightNotMatch = true;
3290 for (unsigned i = 0, e = CodeForPatterns.size(); i != e; ++i) {
3291 CodeList &GeneratedCode = CodeForPatterns[i].second;
3292 mightNotMatch = false;
3293
3294 for (unsigned j = 0, e = GeneratedCode.size(); j != e; ++j) {
3295 if (GeneratedCode[j].first) { // predicate.
3296 mightNotMatch = true;
3297 break;
3298 }
3299 }
3300
3301 // If this pattern definitely matches, and if it isn't the last one, the
3302 // patterns after it CANNOT ever match. Error out.
3303 if (mightNotMatch == false && i != CodeForPatterns.size()-1) {
3304 std::cerr << "Pattern '";
3305 CodeForPatterns[i+1].first->getSrcPattern()->print(OS);
3306 std::cerr << "' is impossible to select!\n";
3307 exit(1);
3308 }
3309 }
3310
3311 // Factor target node emission code (emitted by EmitResultCode) into
3312 // separate functions. Uniquing and share them among all instruction
3313 // selection routines.
3314 for (unsigned i = 0, e = CodeForPatterns.size(); i != e; ++i) {
3315 CodeList &GeneratedCode = CodeForPatterns[i].second;
3316 std::vector &TargetOpcodes = PatternOpcodes[i];
3317 std::vector &TargetVTs = PatternVTs[i];
3318 std::set > Decls = PatternDecls[i];
3319 int CodeSize = (int)GeneratedCode.size();
3320 int LastPred = -1;
3321 for (int j = CodeSize-1; j >= 0; --j) {
3322 if (GeneratedCode[j].first) {
3323 LastPred = j;
3324 break;
3325 }
3326 }
3327
3328 std::string CalleeDecls;
3329 std::string CalleeCode = "(SDOperand &Result, SDOperand &N";
3330 std::string CallerCode = "(Result, N";
3331 for (unsigned j = 0, e = TargetOpcodes.size(); j != e; ++j) {
3332 CalleeCode += ", unsigned Opc" + utostr(j);
3333 CallerCode += ", " + TargetOpcodes[j];
3334 }
3335 for (unsigned j = 0, e = TargetVTs.size(); j != e; ++j) {
3336 CalleeCode += ", MVT::ValueType VT" + utostr(j);
3337 CallerCode += ", " + TargetVTs[j];
3338 }
3339 for (std::set >::iterator
3340 I = Decls.begin(), E = Decls.end(); I != E; ++I) {
3341 std::string Name = I->second;
3342 if (I->first == 0) {
3343 if (Name == "InFlag" ||
3344 (Name.size() > 3 &&
3345 Name[0] == 'T' && Name[1] == 'm' && Name[2] == 'p')) {
3346 CalleeDecls += " SDOperand " + Name + "(0, 0);\n";
3347 continue;
32553348 }
3256 }
3257
3258 // If this pattern definitely matches, and if it isn't the last one, the
3259 // patterns after it CANNOT ever match. Error out.
3260 if (mightNotMatch == false && i != CodeForPatterns.size()-1) {
3261 std::cerr << "Pattern '";
3262 CodeForPatterns[i+1].first->getSrcPattern()->print(std::cerr);
3263 std::cerr << "' is impossible to select!\n";
3264 exit(1);
3265 }
3266 }
3267
3268 // Factor target node emission code (emitted by EmitResultCode) into
3269 // separate functions. Uniquing and share them among all instruction
3270 // selection routines.
3271 for (unsigned i = 0, e = CodeForPatterns.size(); i != e; ++i) {
3272 CodeList &GeneratedCode = CodeForPatterns[i].second;
3273 std::vector &TargetOpcodes = PatternOpcodes[i];
3274 std::vector &TargetVTs = PatternVTs[i];
3275 std::set > Decls = PatternDecls[i];
3276 int CodeSize = (int)GeneratedCode.size();
3277 int LastPred = -1;
3278 for (int j = CodeSize-1; j >= 0; --j) {
3279 if (GeneratedCode[j].first) {
3280 LastPred = j;
3281 break;
3349 CalleeCode += ", SDOperand &" + Name;
3350 CallerCode += ", " + Name;
3351 } else if (I->first == 1) {
3352 if (Name == "ResNode") {
3353 CalleeDecls += " SDNode *" + Name + " = NULL;\n";
3354 continue;
32823355 }
3283 }
3284
3285 std::string CalleeDecls;
3286 std::string CalleeCode = "(SDOperand &Result, const SDOperand &N";
3287 std::string CallerCode = "(Result, N";
3288 for (unsigned j = 0, e = TargetOpcodes.size(); j != e; ++j) {
3289 CalleeCode += ", unsigned Opc" + utostr(j);
3290 CallerCode += ", " + TargetOpcodes[j];
3291 }
3292 for (unsigned j = 0, e = TargetVTs.size(); j != e; ++j) {
3293 CalleeCode += ", MVT::ValueType VT" + utostr(j);
3294 CallerCode += ", " + TargetVTs[j];
3295 }
3296 for (std::set >::iterator
3297 I = Decls.begin(), E = Decls.end(); I != E; ++I) {
3298 std::string Name = I->second;
3299 if (I->first == 0) {
3300 if (Name == "InFlag" ||
3301 (Name.size() > 3 &&
3302 Name[0] == 'T' && Name[1] == 'm' && Name[2] == 'p')) {
3303 CalleeDecls += " SDOperand " + Name + "(0, 0);\n";
3304 continue;
3305 }
3306 CalleeCode += ", SDOperand &" + Name;
3307 CallerCode += ", " + Name;
3308 } else if (I->first == 1) {
3309 if (Name == "ResNode") {
3310 CalleeDecls += " SDNode *" + Name + " = NULL;\n";
3311 continue;
3312 }
3313 CalleeCode += ", SDNode *" + Name;
3314 CallerCode += ", " + Name;
3315 } else {
3316 CalleeCode += ", bool " + Name;
3317 CallerCode += ", " + Name;
3318 }
3319 }
3320 CallerCode += ");";
3321 CalleeCode += ") ";
3322 // Prevent emission routines from being inlined to reduce selection
3323 // routines stack frame sizes.
3324 CalleeCode += "NOINLINE ";
3325 CalleeCode += "{\n" + CalleeDecls;
3326 for (int j = LastPred+1; j < CodeSize; ++j)
3327 CalleeCode += " " + GeneratedCode[j].second + '\n';
3328 for (int j = LastPred+1; j < CodeSize; ++j)
3329 GeneratedCode.pop_back();
3330 CalleeCode += "}\n";
3331
3332 // Uniquing the emission routines.
3333 unsigned EmitFuncNum;
3334 std::map::iterator EFI =
3335 EmitFunctions.find(CalleeCode);
3336 if (EFI != EmitFunctions.end()) {
3337 EmitFuncNum = EFI->second;
3356 CalleeCode += ", SDNode *" + Name;
3357 CallerCode += ", " + Name;
33383358 } else {
3339 EmitFuncNum = EmitFunctions.size();
3340 EmitFunctions.insert(std::make_pair(CalleeCode, EmitFuncNum));
3341 OS << "void " << "Emit_" << utostr(EmitFuncNum) << CalleeCode;
3342 }
3343
3344 // Replace the emission code within selection routines with calls to the
3345 // emission functions.
3346 CallerCode = "Emit_" + utostr(EmitFuncNum) + CallerCode;
3347 GeneratedCode.push_back(std::make_pair(false, CallerCode));
3348 GeneratedCode.push_back(std::make_pair(false, "return;"));
3349 }
3350
3351 // Print function.
3352 std::string OpVTStr = (OpVT != MVT::isVoid && OpVT != MVT::iPTR)
3353 ? getEnumName(OpVT).substr(5) : "" ;
3354 std::map >::iterator OpVTI =
3355 OpcodeVTMap.find(OpName);
3356 if (OpVTI == OpcodeVTMap.end()) {
3357 std::vector VTSet;
3358 VTSet.push_back(OpVTStr);
3359 OpcodeVTMap.insert(std::make_pair(OpName, VTSet));
3360 } else
3361 OpVTI->second.push_back(OpVTStr);
3362
3363 OS << "void Select_" << OpName << (OpVTStr != "" ? "_" : "")
3364 << OpVTStr << "(SDOperand &Result, const SDOperand &N) {\n";
3365
3366 // Print all declarations.
3367 for (std::set >::iterator
3368 I = AllGenDecls.begin(), E = AllGenDecls.end(); I != E; ++I)
3369 if (I->first == 0)
3370 OS << " SDOperand " << I->second << "(0, 0);\n";
3371 else if (I->first == 1)
3372 OS << " SDNode *" << I->second << " = NULL;\n";
3373 else
3374 OS << " bool " << I->second << " = false;\n";
3375
3376 // Loop through and reverse all of the CodeList vectors, as we will be
3377 // accessing them from their logical front, but accessing the end of a
3378 // vector is more efficient.
3379 for (unsigned i = 0, e = CodeForPatterns.size(); i != e; ++i) {
3380 CodeList &GeneratedCode = CodeForPatterns[i].second;
3381 std::reverse(GeneratedCode.begin(), GeneratedCode.end());
3382 }
3383
3384 // Next, reverse the list of patterns itself for the same reason.
3385 std::reverse(CodeForPatterns.begin(), CodeForPatterns.end());
3386
3387 // Emit all of the patterns now, grouped together to share code.
3388 EmitPatterns(CodeForPatterns, 2, OS);
3389
3390 // If the last pattern has predicates (which could fail) emit code to catch
3391 // the case where nothing handles a pattern.
3392 if (mightNotMatch) {
3393 OS << " std::cerr << \"Cannot yet select: \";\n";
3394 if (OpcodeInfo.getEnumName() != "ISD::INTRINSIC_W_CHAIN" &&
3395 OpcodeInfo.getEnumName() != "ISD::INTRINSIC_WO_CHAIN" &&
3396 OpcodeInfo.getEnumName() != "ISD::INTRINSIC_VOID") {
3397 OS << " N.Val->dump(CurDAG);\n";
3398 } else {
3399 OS << " unsigned iid = cast(N.getOperand("
3400 "N.getOperand(0).getValueType() == MVT::Other))->getValue();\n"
3401 << " std::cerr << \"intrinsic %\"<< "
3402 "Intrinsic::getName((Intrinsic::ID)iid);\n";
3403 }
3404 OS << " std::cerr << '\\n';\n"
3405 << " abort();\n";
3406 }
3407 OS << "}\n\n";
3408 }
3359 CalleeCode += ", bool " + Name;
3360 CallerCode += ", " + Name;
3361 }
3362 }
3363 CallerCode += ");";
3364 CalleeCode += ") ";
3365 // Prevent emission routines from being inlined to reduce selection
3366 // routines stack frame sizes.
3367 CalleeCode += "NOINLINE ";
3368 CalleeCode += "{\n" + CalleeDecls;
3369 for (int j = LastPred+1; j < CodeSize; ++j)
3370 CalleeCode += " " + GeneratedCode[j].second + '\n';
3371 for (int j = LastPred+1; j < CodeSize; ++j)
3372 GeneratedCode.pop_back();
3373 CalleeCode += "}\n";
3374
3375 // Uniquing the emission routines.
3376 unsigned EmitFuncNum;
3377 std::map::iterator EFI =
3378 EmitFunctions.find(CalleeCode);
3379 if (EFI != EmitFunctions.end()) {
3380 EmitFuncNum = EFI->second;
3381 } else {
3382 EmitFuncNum = EmitFunctions.size();
3383 EmitFunctions.insert(std::make_pair(CalleeCode, EmitFuncNum));
3384 OS << "void " << "Emit_" << utostr(EmitFuncNum) << CalleeCode;
3385 }
3386
3387 // Replace the emission code within selection routines with calls to the
3388 // emission functions.
3389 CallerCode = "Emit_" + utostr(EmitFuncNum) + CallerCode;
3390 GeneratedCode.push_back(std::make_pair(false, CallerCode));
3391 GeneratedCode.push_back(std::make_pair(false, "return;"));
3392 }
3393
3394 // Print function.
3395 OS << "void Select_" << OpName << "(SDOperand &Result, SDOperand N) {\n";
3396 if (OptSlctOrder) {
3397 OS << " if (N.ResNo == " << OpcodeInfo.getNumResults()
3398 << " && N.getValue(0).hasOneUse()) {\n"
3399 << " SDOperand Dummy = "
3400 << "CurDAG->getNode(ISD::HANDLENODE, MVT::Other, N);\n"
3401 << " SelectionDAG::InsertISelMapEntry(CodeGenMap, N.Val, "
3402 << OpcodeInfo.getNumResults() << ", Dummy.Val, 0);\n"
3403 << " SelectionDAG::InsertISelMapEntry(HandleMap, N.Val, "
3404 << OpcodeInfo.getNumResults() << ", Dummy.Val, 0);\n"
3405 << " Result = Dummy;\n"
3406 << " return;\n"
3407 << " }\n";
3408 }
3409
3410 // Print all declarations.
3411 for (std::set >::iterator
3412 I = AllGenDecls.begin(), E = AllGenDecls.end(); I != E; ++I)
3413 if (I->first == 0)
3414 OS << " SDOperand " << I->second << "(0, 0);\n";
3415 else if (I->first == 1)
3416 OS << " SDNode *" << I->second << " = NULL;\n";
3417 else
3418 OS << " bool " << I->second << " = false;\n";
3419
3420 // Loop through and reverse all of the CodeList vectors, as we will be
3421 // accessing them from their logical front, but accessing the end of a
3422 // vector is more efficient.
3423 for (unsigned i = 0, e = CodeForPatterns.size(); i != e; ++i) {
3424 CodeList &GeneratedCode = CodeForPatterns[i].second;
3425 std::reverse(GeneratedCode.begin(), GeneratedCode.end());
3426 }
3427
3428 // Next, reverse the list of patterns itself for the same reason.
3429 std::reverse(CodeForPatterns.begin(), CodeForPatterns.end());
3430
3431 // Emit all of the patterns now, grouped together to share code.
3432 EmitPatterns(CodeForPatterns, 2, OS);
3433
3434 // If the last pattern has predicates (which could fail) emit code to catch
3435 // the case where nothing handles a pattern.
3436 if (mightNotMatch) {
3437 OS << " std::cerr << \"Cannot yet select: \";\n";
3438 if (OpcodeInfo.getEnumName() != "ISD::INTRINSIC_W_CHAIN" &&
3439 OpcodeInfo.getEnumName() != "ISD::INTRINSIC_WO_CHAIN" &&
3440 OpcodeInfo.getEnumName() != "ISD::INTRINSIC_VOID") {
3441 OS << " N.Val->dump(CurDAG);\n";
3442 } else {
3443 OS << " unsigned iid = cast(N.getOperand("
3444 "N.getOperand(0).getValueType() == MVT::Other))->getValue();\n"
3445 << " std::cerr << \"intrinsic %\"<< "
3446 "Intrinsic::getName((Intrinsic::ID)iid);\n";
3447 }
3448 OS << " std::cerr << '\\n';\n"
3449 << " abort();\n";
3450 }
3451 OS << "}\n\n";
34093452 }
34103453
34113454 // Emit boilerplate.
34123455 OS << "void Select_INLINEASM(SDOperand& Result, SDOperand N) {\n"
34133456 << " std::vector Ops(N.Val->op_begin(), N.Val->op_end());\n"
3414 << " AddToQueue(Ops[0], N.getOperand(0)); // Select the chain.\n\n"
3457 << " Select(Ops[0], N.getOperand(0)); // Select the chain.\n\n"
34153458 << " // Select the flag operand.\n"
34163459 << " if (Ops.back().getValueType() == MVT::Flag)\n"
3417 << " AddToQueue(Ops.back(), Ops.back());\n"
3460 << " Select(Ops.back(), Ops.back());\n"
34183461 << " SelectInlineAsmMemoryOperands(Ops, *CurDAG);\n"
34193462 << " std::vector VTs;\n"
34203463 << " VTs.push_back(MVT::Other);\n"
34213464 << " VTs.push_back(MVT::Flag);\n"
3422 << " SDOperand New = CurDAG->getNode(ISD::INLINEASM, VTs, &Ops[0], "
3423 "Ops.size());\n"
3424 << " ReplaceUses(SDOperand(N.Val, 0), New);\n"
3425 << " ReplaceUses(SDOperand(N.Val, 1), SDOperand(New.Val, 1));\n"
3465 << " SDOperand New = CurDAG->getNode(ISD::INLINEASM, VTs, Ops);\n"
3466 << " SelectionDAG::InsertISelMapEntry(CodeGenMap, N.Val, 0, New.Val, 0);\n"
3467 << " SelectionDAG::InsertISelMapEntry(CodeGenMap, N.Val, 1, New.Val, 1);\n"
34263468 << " Result = New.getValue(N.ResNo);\n"
34273469 << " return;\n"
34283470 << "}\n\n";
34343476 << "INSTRUCTION_LIST_END)) {\n"
34353477 << " Result = N;\n"
34363478 << " return; // Already selected.\n"
3479 << " }\n\n"
3480 << " std::map::iterator CGMI = CodeGenMap.find(N);\n"
3481 << " if (CGMI != CodeGenMap.end()) {\n"
3482 << " Result = CGMI->second;\n"
3483 << " return;\n"
34373484 << " }\n\n"
34383485 << " switch (N.getOpcode()) {\n"
34393486 << " default: break;\n"
34513498 << " }\n"
34523499 << " case ISD::AssertSext:\n"
34533500 << " case ISD::AssertZext: {\n"
3454 << " AddToQueue(Result, N.getOperand(0));\n"
3455 << " ReplaceUses(N, Result);\n"
3501 << " SDOperand Tmp0;\n"
3502 << " Select(Tmp0, N.getOperand(0));\n"
3503 << " if (!N.Val->hasOneUse())\n"
3504 << " SelectionDAG::InsertISelMapEntry(CodeGenMap, N.Val, N.ResNo, "
3505 << "Tmp0.Val, Tmp0.ResNo);\n"
3506 << " Result = Tmp0;\n"
34563507 << " return;\n"
34573508 << " }\n"
34583509 << " case ISD::TokenFactor:\n"
3459 << " case ISD::CopyFromReg:\n"
3510 << " if (N.getNumOperands() == 2) {\n"
3511 << " SDOperand Op0, Op1;\n"
3512 << " Select(Op0, N.getOperand(0));\n"
3513 << " Select(Op1, N.getOperand(1));\n"
3514 << " Result = \n"
3515 << " CurDAG->getNode(ISD::TokenFactor, MVT::Other, Op0, Op1);\n"
3516 << " SelectionDAG::InsertISelMapEntry(CodeGenMap, N.Val, N.ResNo, "
3517 << "Result.Val, Result.ResNo);\n"
3518 << " } else {\n"
3519 << " std::vector Ops;\n"
3520 << " for (unsigned i = 0, e = N.getNumOperands(); i != e; ++i) {\n"
3521 << " SDOperand Val;\n"
3522 << " Select(Val, N.getOperand(i));\n"
3523 << " Ops.push_back(Val);\n"
3524 << " }\n"
3525 << " Result = \n"
3526 << " CurDAG->getNode(ISD::TokenFactor, MVT::Other, Ops);\n"
3527 << " SelectionDAG::InsertISelMapEntry(CodeGenMap, N.Val, N.ResNo, "
3528 << "Result.Val, Result.ResNo);\n"
3529 << " }\n"
3530 << " return;\n"
3531 << " case ISD::CopyFromReg: {\n"
3532 << " SDOperand Chain;\n"
3533 << " Select(Chain, N.getOperand(0));\n"
3534 << " unsigned Reg = cast(N.getOperand(1))->getReg();\n"
3535 << " MVT::ValueType VT = N.Val->getValueType(0);\n"
3536 << " if (N.Val->getNumValues() == 2) {\n"
3537 << " if (Chain == N.getOperand(0)) {\n"
3538 << " Result = N; // No change\n"
3539 << " return;\n"
3540 << " }\n"
3541 << " SDOperand New = CurDAG->getCopyFromReg(Chain, Reg, VT);\n"
3542 << " SelectionDAG::InsertISelMapEntry(CodeGenMap, N.Val, 0, "
3543 << "New.Val, 0);\n"
3544 << " SelectionDAG::InsertISelMapEntry(CodeGenMap, N.Val, 1, "
3545 << "New.Val, 1);\n"
3546 << " Result = New.getValue(N.ResNo);\n"
3547 << " return;\n"
3548 << " } else {\n"
3549 << " SDOperand Flag;\n"
3550 << " if (N.getNumOperands() == 3) Select(Flag, N.getOperand(2));\n"
3551 << " if (Chain == N.getOperand(0) &&\n"
3552 << " (N.getNumOperands() == 2 || Flag == N.getOperand(2))) {\n"
3553 << " Result = N; // No change\n"
3554 << " return;\n"
3555 << " }\n"
3556 << " SDOperand New = CurDAG->getCopyFromReg(Chain, Reg, VT, Flag);\n"
3557 << " SelectionDAG::InsertISelMapEntry(CodeGenMap, N.Val, 0, "
3558 << "New.Val, 0);\n"
3559 << " SelectionDAG::InsertISelMapEntry(CodeGenMap, N.Val, 1, "
3560 << "New.Val, 1);\n"
3561 << " SelectionDAG::InsertISelMapEntry(CodeGenMap, N.Val, 2, "
3562 << "New.Val, 2);\n"
3563 << " Result = New.getValue(N.ResNo);\n"
3564 << " return;\n"
3565 << " }\n"
3566 << " }\n"
34603567 << " case ISD::CopyToReg: {\n"
3461 << " for (unsigned i = 0, e = N.getNumOperands(); i != e; ++i) {\n"
3462 << " SDOperand Dummy;\n"
3463 << " AddToQueue(Dummy, N.getOperand(i));\n"
3568 << " SDOperand Chain;\n"
3569 << " Select(Chain, N.getOperand(0));\n"
3570 << " unsigned Reg = cast(N.getOperand(1))->getReg();\n"
3571 << " SDOperand Val;\n"
3572 << " Select(Val, N.getOperand(2));\n"
3573 << " Result = N;\n"
3574 << " if (N.Val->getNumValues() == 1) {\n"
3575 << " if (Chain != N.getOperand(0) || Val != N.getOperand(2))\n"
3576 << " Result = CurDAG->getCopyToReg(Chain, Reg, Val);\n"
3577 << " SelectionDAG::InsertISelMapEntry(CodeGenMap, N.Val, 0, "
3578 << "Result.Val, 0);\n"
3579 << " } else {\n"
3580 << " SDOperand Flag(0, 0);\n"
3581 << " if (N.getNumOperands() == 4) Select(Flag, N.getOperand(3));\n"
3582 << " if (Chain != N.getOperand(0) || Val != N.getOperand(2) ||\n"
3583 << " (N.getNumOperands() == 4 && Flag != N.getOperand(3)))\n"
3584 << " Result = CurDAG->getCopyToReg(Chain, Reg, Val, Flag);\n"
3585 << " SelectionDAG::InsertISelMapEntry(CodeGenMap, N.Val, 0, "
3586 << "Result.Val, 0);\n"
3587 << " SelectionDAG::InsertISelMapEntry(CodeGenMap, N.Val, 1, "
3588 << "Result.Val, 1);\n"
3589 << " Result = Result.getValue(N.ResNo);\n"
34643590 << " }\n"
3465 << " Result = N;\n"
34663591 << " return;\n"
34673592 << " }\n"
34683593 << " case ISD::INLINEASM: Select_INLINEASM(Result, N); return;\n";
34743599 CompareByRecordName>::iterator PBOI = PatternsByOpcode.begin(),
34753600 E = PatternsByOpcode.end(); PBOI != E; ++PBOI) {
34763601 const SDNodeInfo &OpcodeInfo = getSDNodeInfo(PBOI->first);
3477 const std::string &OpName = PBOI->first->getName();
3478 // Potentially multiple versions of select for this opcode. One for each
3479 // ValueType of the node (or its first true operand if it doesn't produce a
3480 // result.
3481 std::map >::iterator OpVTI =
3482 OpcodeVTMap.find(OpName);
3483 std::vector &OpVTs = OpVTI->second;
3484 OS << " case " << OpcodeInfo.getEnumName() << ": {\n";
3485 if (OpVTs.size() == 1) {
3486 std::string &VTStr = OpVTs[0];
3487 OS << " Select_" << OpName
3488 << (VTStr != "" ? "_" : "") << VTStr << "(Result, N);\n";
3489 } else {
3490 if (OpcodeInfo.getNumResults())
3491 OS << " MVT::ValueType NVT = N.Val->getValueType(0);\n";
3492 else if (OpcodeInfo.hasProperty(SDNodeInfo::SDNPHasChain))
3493 OS << " MVT::ValueType NVT = (N.getNumOperands() > 1) ?"
3494 << " N.getOperand(1).Val->getValueType(0) : MVT::isVoid;\n";
3495 else
3496 OS << " MVT::ValueType NVT = (N.getNumOperands() > 0) ?"
3497 << " N.getOperand(0).Val->getValueType(0) : MVT::isVoid;\n";
3498 int ElseCase = -1;
3499 bool First = true;
3500 for (unsigned i = 0, e = OpVTs.size(); i < e; ++i) {
3501 std::string &VTStr = OpVTs[i];
3502 if (VTStr == "") {
3503 ElseCase = i;
3504 continue;
3505 }
3506 OS << (First ? " if" : " else if")
3507 << " (NVT == MVT::" << VTStr << ")\n"
3508 << " Select_" << OpName
3509 << "_" << VTStr << "(Result, N);\n";
3510 First = false;
3511 }
3512 if (ElseCase != -1)
3513 OS << " else\n" << " Select_" << OpName << "(Result, N);\n";
3514 else
3515 OS << " else\n" << " break;\n";
3516 }
3517 OS << " return;\n";
3518 OS << " }\n";
3602 OS << " case " << OpcodeInfo.getEnumName() << ": "
3603 << std::string(std::max(0, int(24-OpcodeInfo.getEnumName().size())), ' ')
3604 << "Select_" << PBOI->first->getName() << "(Result, N); return;\n";
35193605 }
35203606
35213607 OS << " } // end of big switch.\n\n"
35463632 OS << "#if defined(__GNUC__) && \\\n";
35473633 OS << " ((__GNUC__ > 3) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 4)))\n";
35483634 OS << "#define NOINLINE __attribute__((noinline))\n";
3549 OS << "#else\n";
3550 OS << "#define NOINLINE\n";
35513635 OS << "#endif\n\n";
35523636
3553 OS << "// Instruction selector priority queue:\n"
3554 << "std::vector ISelQueue;\n";
3555 OS << "/// Keep track of nodes which have already been added to queue.\n"
3556 << "unsigned char *ISelQueued;\n";
3557 OS << "/// Keep track of nodes which have already been selected.\n"
3558 << "unsigned char *ISelSelected;\n";
3559 OS << "/// Dummy parameter to ReplaceAllUsesOfValueWith().\n"
3560 << "std::vector ISelKilled;\n\n";
3561
3562 OS << "/// Sorting functions for the selection queue.\n"
3563 << "struct isel_sort : public std::binary_function"
3564 << " {\n"
3565 << " bool operator()(const SDNode* left, const SDNode* right) "
3566 << "const {\n"
3567 << " return (left->getNodeId() > right->getNodeId());\n"
3568 << " }\n"
3569 << "};\n\n";
3570
3571 OS << "inline void setQueued(int Id) {\n";
3572 OS << " ISelQueued[Id / 8] |= 1 << (Id % 8);\n";
3637 OS << "// Instance var to keep track of multiply used nodes that have \n"
3638 << "// already been selected.\n"
3639 << "std::map CodeGenMap;\n";
3640
3641 OS << "// Instance var to keep track of mapping of chain generating nodes\n"
3642 << "// and their place handle nodes.\n";
3643 OS << "std::map HandleMap;\n";
3644 OS << "// Instance var to keep track of mapping of place handle nodes\n"
3645 << "// and their replacement nodes.\n";
3646 OS << "std::map ReplaceMap;\n";
3647 OS << "// Keep track of nodes that are currently being selecte and therefore\n"
3648 << "// should not be folded.\n";
3649 OS << "std::set InFlightSet;\n";
3650
3651 OS << "\n";
3652 OS << "static void findNonImmUse(SDNode* Use, SDNode* Def, bool &found, "
3653 << "std::set &Visited) {\n";
3654 OS << " if (found || !Visited.insert(Use).second) return;\n";
3655 OS << " for (unsigned i = 0, e = Use->getNumOperands(); i != e; ++i) {\n";
3656 OS << " SDNode *N = Use->getOperand(i).Val;\n";
3657 OS << " if (N != Def) {\n";
3658 OS << " findNonImmUse(N, Def, found, Visited);\n";
3659 OS << " } else {\n";
3660 OS << " found = true;\n";
3661 OS << " break;\n";
3662 OS << " }\n";
3663 OS << " }\n";
35733664 OS << "}\n";
3574 OS << "inline bool isQueued(int Id) {\n";
3575 OS << " return ISelQueued[Id / 8] & (1 << (Id % 8));\n";
3665
3666 OS << "\n";
3667 OS << "static bool isNonImmUse(SDNode* Use, SDNode* Def) {\n";
3668 OS << " std::set Visited;\n";
3669 OS << " bool found = false;\n";
3670 OS << " for (unsigned i = 0, e = Use->getNumOperands(); i != e; ++i) {\n";
3671 OS << " SDNode *N = Use->getOperand(i).Val;\n";
3672 OS << " if (N != Def) {\n";
3673 OS << " findNonImmUse(N, Def, found, Visited);\n";
3674 OS << " if (found) break;\n";
3675 OS << " }\n";
3676 OS << " }\n";
3677 OS << " return found;\n";
35763678 OS << "}\n";
3577 OS << "inline void setSelected(int Id) {\n";
3578 OS << " ISelSelected[Id / 8] |= 1 << (Id % 8);\n";
3679
3680 OS << "\n";
3681 OS << "// AddHandleReplacement - Note the pending replacement node for a\n"
3682 << "// handle node in ReplaceMap.\n";
3683 OS << "void AddHandleReplacement(SDNode *H, unsigned HNum, SDNode *R, "
3684 << "unsigned RNum) {\n";
3685 OS << " SDOperand N(H, HNum);\n";
3686 OS << " std::map::iterator HMI = HandleMap.find(N);\n";
3687 OS << " if (HMI != HandleMap.end()) {\n";
3688 OS << " ReplaceMap[HMI->second] = SDOperand(R, RNum);\n";
3689 OS << " HandleMap.erase(N);\n";
3690 OS << " }\n";
35793691 OS << "}\n";
3580 OS << "inline bool isSelected(int Id) {\n";
3581 OS << " return ISelSelected[Id / 8] & (1 << (Id % 8));\n";
3582 OS << "}\n\n";
3583
3584 OS << "inline void AddToQueue(SDOperand &Result, SDOperand N) {\n";
3585 OS << " Result = N;\n";
3586 OS << " int Id = N.Val->getNodeId();\n";
3587 OS << " if (Id != -1 && !isQueued(Id)) {\n";
3588 OS << " ISelQueue.push_back(N.Val);\n";
3589 OS << " std::push_heap(ISelQueue.begin(), ISelQueue.end(), isel_sort());\n";
3590 OS << " setQueued(Id);\n";
3692
3693 OS << "\n";
3694 OS << "// SelectDanglingHandles - Select replacements for all `dangling`\n";
3695 OS << "// handles.Some handles do not yet have replacements because the\n";
3696 OS << "// nodes they replacements have only dead readers.\n";
3697 OS << "void SelectDanglingHandles() {\n";
3698 OS << " for (std::map::iterator I = "
3699 << "HandleMap.begin(),\n"
3700 << " E = HandleMap.end(); I != E; ++I) {\n";
3701 OS << " SDOperand N = I->first;\n";
3702 OS << " SDOperand R;\n";
3703 OS << " Select(R, N.getValue(0));\n";
3704 OS << " AddHandleReplacement(N.Val, N.ResNo, R.Val, R.ResNo);\n";
35913705 OS << " }\n";
3592 OS << "}\n\n";
3593
3594 OS << "inline void RemoveKilled() {\n";
3595 OS << " unsigned NumKilled = ISelKilled.size();\n";
3596 OS << " if (NumKilled) {\n";
3597 OS << " for (unsigned i = 0; i != NumKilled; ++i) {\n";
3598 OS << " SDNode *Temp = ISelKilled[i];\n";
3599 OS << " std::remove(ISelQueue.begin(), ISelQueue.end(), Temp);\n";
3600 OS << " };\n";
3601 OS << " std::make_heap(ISelQueue.begin(), ISelQueue.end(), isel_sort());\n";
3602 OS << " ISelKilled.clear();\n";
3706 OS << "}\n";
3707 OS << "\n";
3708 OS << "// ReplaceHandles - Replace all the handles with the real target\n";
3709 OS << "// specific nodes.\n";
3710 OS << "void ReplaceHandles() {\n";
3711 OS << " for (std::map::iterator I = "
3712 << "ReplaceMap.begin(),\n"
3713 << " E = ReplaceMap.end(); I != E; ++I) {\n";
3714 OS << " SDOperand From = I->first;\n";
3715 OS << " SDOperand To = I->second;\n";
3716 OS << " for (SDNode::use_iterator UI = From.Val->use_begin(), "
3717 << "E = From.Val->use_end(); UI != E; ++UI) {\n";
3718 OS << " SDNode *Use = *UI;\n";
3719 OS << " std::vector Ops;\n";
3720 OS << " for (unsigned i = 0, e = Use->getNumOperands(); i != e; ++i){\n";
3721 OS << " SDOperand O = Use->getOperand(i);\n";
3722 OS << " if (O.Val == From.Val)\n";
3723 OS << " Ops.push_back(To);\n";
3724 OS << " else\n";
3725 OS << " Ops.push_back(O);\n";
3726 OS << " }\n";
3727 OS << " SDOperand U = SDOperand(Use, 0);\n";
3728 OS << " CurDAG->UpdateNodeOperands(U, Ops);\n";
3729 OS << " }\n";
36033730 OS << " }\n";
3604 OS << "}\n\n";
3605
3606 OS << "inline void ReplaceUses(SDOperand F, SDOperand T) {\n";
3607 OS << " CurDAG->ReplaceAllUsesOfValueWith(F, T, ISelKilled);\n";
3608 OS << " setSelected(F.Val->getNodeId());\n";
3609 OS << " RemoveKilled();\n";
3610 OS << "}\n\n";
3611
3731 OS << "}\n";
3732
3733 OS << "\n";
36123734 OS << "// SelectRoot - Top level entry to DAG isel.\n";
3613 OS << "SDOperand SelectRoot(SDOperand Root) {\n";
3614 OS << " SelectRootInit();\n";
3615 OS << " unsigned NumBytes = (DAGSize + 7) / 8;\n";
3616 OS << " ISelQueued = new unsigned char[NumBytes];\n";
3617 OS << " ISelSelected = new unsigned char[NumBytes];\n";
3618 OS << " memset(ISelQueued, 0, NumBytes);\n";
3619 OS << " memset(ISelSelected, 0, NumBytes);\n";
3620 OS << "\n";
3735 OS << "SDOperand SelectRoot(SDOperand N) {\n";
36213736 OS << " SDOperand ResNode;\n";
3622 OS << " Select(ResNode, Root);\n";
3623 OS << " while (!ISelQueue.empty()) {\n";
3624 OS << " SDOperand Tmp;\n";
3625 OS << " SDNode *Node = ISelQueue.front();\n";
3626 OS << " std::pop_heap(ISelQueue.begin(), ISelQueue.end(), isel_sort());\n";
3627 OS << " ISelQueue.pop_back();\n";
3628 OS << " if (!isSelected(Node->getNodeId()))\n";
3629 OS << " Select(Tmp, SDOperand(Node, 0));\n";
3630 OS << " }\n";
3631 OS << "\n";
3632 OS << " delete[] ISelQueued;\n";
3633 OS << " ISelQueued = NULL;\n";
3634 OS << " delete[] ISelSelected;\n";
3635 OS << " ISelSelected = NULL;\n";
3737 OS << " Select(ResNode, N);\n";
3738 OS << " SelectDanglingHandles();\n";
3739 OS << " ReplaceHandles();\n";
3740 OS << " ReplaceMap.clear();\n";
36363741 OS << " return ResNode;\n";
36373742 OS << "}\n";
36383743